Fixed bug in setbg
[clearscm.git] / bin / monitorXScreenSaver.pl
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4
5 =pod
6
7 =head1 NAME $RCSfile: monitorXScreenSaver.pl,v $
8
9 Monitor xscreensaver and forcefully turn off screens when blanked. Note I had a
10 problem before where the screens wouled unblank and not go into powersave mode.
11 I think my new system doesn't have this problem anymore but I still run this.
12
13 I'd like to augment this so that after blanking of the screen and some time
14 thereafter the system would suspend. Suspending the system doesn't always work
15 well. It doesn't work well on my MacBook running Ubuntu but it does seem to work
16 OK on my Thelio Desktop from System76. But I have not implemented this yet.
17
18 =head1 VERSION
19
20 =over
21
22 =item Author
23
24 Andrew DeFaria <Andrew@DeFaria.com>
25
26 =item Revision
27
28 $Revision: 1.0 $
29
30 =item Created:
31
32 Fri 09 Apr 2021 10:50:28 AM PDT
33
34 =item Modified:
35
36 $Date: $
37
38 =back
39
40 =head1 SYNOPSIS
41
42  Usage: monitorXscreenSaver.pl [-u|sage] [-h|elp] [-v|erbose] [-de|bug]
43                                [-[no]da|emon] [-l|ogpath]
44
45  Where:
46    -u|sage           Print this usage
47    -h|elp            Detailed help
48    -v|erbose:        Verbose mode
49    -d|ebug:          Print debug messages
50    -l|ogpath <path>: Path to logfile (Default: /var/local/log)
51    -da|emon          Run in daemon mode (Default: -daemon)
52
53 =head1 DESCRIPTION
54
55 This script will monitor the xscreensaver process and forcefully powersave the
56 monitors when blanked.
57
58 =cut
59
60 use FindBin;
61 use Getopt::Long;
62 use Pod::Usage;
63
64 use lib "$FindBin::Bin/lib";
65
66 use Display;
67 use Logger;
68 use Utils;
69
70 my %opts = (
71   usage   => sub { pod2usage },
72   help    => sub { pod2usage(-verbose => 2)},
73   verbose => sub { set_verbose },
74   debug   => sub { set_debug },
75   daemon  => 1,
76   logpath => '/var/local/log',
77 );
78
79 my ($xscreensaver, $log);
80
81 sub interrupt() {
82   $log->msg("$FindBin::Script shutdown");
83
84   # For Perl::Critic
85   return;
86 } # interrupt
87
88 ## Main
89 GetOptions(
90   \%opts,
91   'usage',
92   'help',
93   'verbose',
94   'debug',
95   'daemon!',
96   'logpath',
97 ) or pod2usage;
98
99 $SIG{INT} = \&interrupt;
100
101 $log = Logger->new(
102   path        => $opts{logpath},
103   timestamped => 1,
104   append      => $opts{append},
105 );
106
107 my $locked = 0;
108
109 $| = 1;
110
111 $log->msg('Started monitoring XScreenSaver');
112
113 if ($opts{daemon}) {
114   # Perl complains if we reference $DB::OUT only once
115   no warnings;
116   EnterDaemonMode unless defined $DB::OUT or get_debug;
117   use warnings;
118 } # if
119
120 open $xscreensaver, '-|', 'xscreensaver-command -watch'
121   or $log->err("Unable to start xscreensaver-command -watch - $!", 1);
122
123 while (<$xscreensaver>) {
124   $log->dbug("Received: $_");
125
126   if (/^LOCK/) {
127     $log->msg('Locked screen');
128     $locked = 1;
129
130     my $cmd = 'xset dpms force off';
131
132     $log->dbug("Calling $cmd");
133     system $cmd;
134     my $status = $?;
135
136     $log->dbug("Returned from $cmd");
137
138     if ($status == 0) {
139       $log->dbug('Success');
140     } else {
141       $log->err("Unable to call $cmd- $!");
142     } # if
143   } elsif ($locked and /^UNBLANK/) {
144     $log->msg('Unlocked screen');
145     $locked = 0;
146   } # if
147 } # while