LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   How to write a daemon that polls X DPMS settings? (https://www.linuxquestions.org/questions/linux-software-2/how-to-write-a-daemon-that-polls-x-dpms-settings-722714/)

hollywoodb 04-30-2009 09:15 AM

How to write a daemon that polls X DPMS settings?
 
I'm fed up with gnome-power-manager, and I've resorted to using xset to make sure my LCD turns off after a specified amount of time.

I disable gnome-screensaver and set gnome-power-manager to blank the screen after 11 minutes. I then use xset to set DPMS Suspend and PowerOff times to 12 and 14 minutes, respectively. I do this because on my laptop Gnome 2.26 (and any previous version I've run in the past five years) rarely if ever actually turns off my LCD backlight.

This works great, the problem is that sometimes Gnome resets my DPMS Standby, Suspend, and PowerOff times to 0, 0, 0. I have a launcher on my panel now that runs `xset dpms 0 720 840` which I click before I leave my system so that I know the LCD will blank, but it isn't the most elegant solution.

Now I'm fairly handy with Perl and shell scripting, but I have no idea how to write a daemon-type utility to do any polling.

Basically I want to have a (user-run) daemon that starts with Gnome. That part I know how to do. What I don't know how to do is poll for current DPMS times. I can probably make it work by either having it poll every few minutes for what the current DPMS times are, or else having it poll for DPMS time changes. Whenever the times get reset or changed from 0, 720, 840, I want to run xset again.

So basically, how do I poll for X DPMS time values? Is there a better way than using regular expressions to parse the output of `xset q`?

hollywoodb 04-30-2009 11:48 AM

It isn't pretty, but here's what I built during my lunch break:
Code:

#!/usr/bin/perl

use strict;

my $standbyTime = 0;
my $suspendTime = 720;
my $pwroffTime = 840;
my $pollTime = 300;
my $xsetOutput;

if ($ARGV[0] eq "-h" || $ARGV[0] eq "--help") {
    print "\n";
    print "xset_poll.pl is a script that makes sure that X DPMS settings\n";
    print "do not get reset.\n";
    print "\n";
    print "xset_poll.pl will check DPMS settings once, unless it is given\n";
    print "the -d command line argument, which causes it to run as a daemon.\n";
    print "\n";
    print "xset_poll.pl options can be set by editing the variables at the\n";
    print "top of the script.\n";
    print "\n";
    exit(0);
}   

do {
    $xsetOutput = `xset q`;
    if ( $xsetOutput =~ m/.*DPMS.*\n\s*Standby:\s+(\d+)\s*Suspend:\s+(\d+)\s*Off:\s+(\d+)/g ) {
        # $1 is DPMS Standby time in seconds
        # $2 is DPMS Suspend time in seconds
        # $3 is DPMS Off time in seconds
        if ( $1 != $standbyTime || $2 != $suspendTime || $3 != $pwroffTime ) {
            system("xset dpms $standbyTime $suspendTime $pwroffTime");
        }
    }
    if ($ARGV[0] eq "-d") {
        sleep($pollTime);
    }
} while ($ARGV[0] eq "-d")



All times are GMT -5. The time now is 08:36 AM.