monitor backlight won't turn off, wrote a daemon to force it on / off

dave selby dave6502 at googlemail.com
Mon May 4 07:25:36 UTC 2009


Hi All,

I have a system where the monitor will blank but absolutely will not turn off.

I have checked xorg.conf for DPMS, tweaked configs and googled etc and
came to realize I am not alone.
I came across http://www.shallowsky.com/linux/x-screen-blanking.html
which shed some light but did not want to
use the suggested workaround so I have written 'backlight_daemon.py'

It checks 'xset -q' every couple of seconds and if it finds that the
monitor should be off, it 'vbetool dpms off''s to
force the issue, reports what it is doing to syslog :)

It needs to be run as root, I start it by adding

@reboot /usr/local/bin/backlight_daemon.py

to my roots crontab. Its easier than setting up a /etc/init.d/...

Anyhow it solved my problem and I wanted to share it with you guys,

Cheers

Dave





#!/usr/bin/env python

# Copyright 2008 David Selby dave6502<SPAM>@googlemail.com

# backlight_daemon is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# kmotion is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with backlight_daemon. If not, see <http://www.gnu.org/licenses/>.


import os, sys, pwd, time, syslog
from subprocess import * # breaking habit of a lifetime !


def main():
    # the backlight daemon
    log_it('Starting ...')
    while True:
        user = find_user()
        check_change(user)


def find_user():
    # wait for a valid user who can 'xset -q -display:0.0', return that user
    while True:
        users = [i[0] for i in pwd.getpwall() if i[2] > 500 or i[2] == 0]
        for user in users:
            pipe = Popen(['sudo -u %s xset -q -display :0.0' % user],
shell=True, stdout=PIPE, stderr=PIPE, close_fds=True)
            if pipe.communicate()[1] == '':
                log_it('Selected user \'%s\'' % user)
                return user
        time.sleep(10)


def check_change(user):
    # check 'xset -q' and execute 'vbetool dpms on|off'
    monitor_on = False # system starting so monitor assumed to be off
    while True:
        pipe = Popen(['sudo -u %s xset -q -display :0.0' % user],
shell=True, stdout=PIPE, stderr=PIPE, close_fds=True)
        xset_q, xset_err = pipe.communicate()
        if xset_err != '':
            break # 'user' no longer valid

        if monitor_on and xset_q.find('Monitor is Off') != -1:
            log_it('Forcing monitor backlight off')
            # sudo to set env variables for root in this shell
            Popen(['sudo vbetool dpms off'], shell=True, close_fds=True)
            monitor_on = False

        if not monitor_on and xset_q.find('Monitor is On') != -1:
            log_it('Forcing monitor backlight on')
            # sudo to set env variables for root in this shell
            Popen(['sudo vbetool dpms on'], shell=True, close_fds=True)
            monitor_on = True

        time.sleep(2)


def log_it(msg):
    # log 'msg' to syslog
    syslog.openlog('backlight_daemon', syslog.LOG_PID)
    syslog.syslog(msg)
    syslog.closelog()


main()





-- 

Please avoid sending me Word or PowerPoint attachments.
See http://www.gnu.org/philosophy/no-word-attachments.html
-------------- next part --------------
A non-text attachment was scrubbed...
Name: backlight_daemon.py
Type: text/x-python
Size: 2666 bytes
Desc: not available
URL: <https://lists.ubuntu.com/archives/ubuntu-users/attachments/20090504/57b97931/attachment.py>


More information about the ubuntu-users mailing list