blib/lib/Schedule/Chronic/Constraint/Xscreensaver.pm untested

filter: Statementstmt 0.0% 0 / 71 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 132 CC 15 SCAR 31.0
CC
15
Cov
0%
init
42.0
met
30.0
state
30.0
CRAP 22.3
1 ##
2 ## Xscreensaver constraint
3 ## Author: Vipul Ved Prakash <mail@vipul.net>.
4 ## $Id: Xscreensaver.pm,v 1.5 2004/08/15 21:10:03 hackworth Exp $
5 ##
6  
7  
8 package Schedule::Chronic::Constraint::Xscreensaver;
9 use Schedule::Chronic::Base;
10 use Schedule::Chronic::Timer;
11 use IO::File;
12 use IO::Poll;
13 use Data::Dumper;
14 use base qw(Schedule::Chronic::Base);
15  
16  
17 sub new {
18
19 my ($class) = @_;
20  
21 return bless {
22 active => 10,
23 wait => 5,
24 timer => new Schedule::Chronic::Timer ('down'),
25 }, $class;
26  
27 }
28  
29  
30 sub init {
31  
32 my ($self, $schedule, $task, $logger, $active) = @_;
33 return unless $self;
34  
35 $$self{schedule} = $schedule if $schedule;
36 $$self{task} = $task if $task;
37 $$self{active} = $active if $active;
38 $$self{logger} = $logger;
39  
40 my $xscrn = $self->which("xscreensaver-command");
41 return unless $xscrn;
42  
43 $$self{watch} = new IO::File;
44 $$self{pid} = $$self{watch}->open("$xscrn -watch |");
45  
46 $$self{watch}->blocking(0);
47 $$self{poll} = new IO::Poll;
48 $$self{poll}->mask($$self{watch} => POLLIN);
49  
50 return $self;
51  
52 }
53  
54  
55 sub met {
56  
57 my ($self) = @_;
58  
59 my $state = $self->state();
60  
61 # $state values:
62 # 0 means no change
63 # 1 means toggled to locked
64 # 2 means toggled to unlocked
65  
66 my @states = ('NO CHANGE', 'LOCKED', 'UNLOCKED');
67  
68 $self->debug(" xscreensaver state = $states[$state]");
69  
70 if ($$self{timer}->running() and $state == 0) {
71
72 # We are still in locked mode.
73  
74 if ($$self{timer}->get() <= 0) {
75 return 1;
76 } else {
77 $$self{wait} = $$self{timer}->get();
78 $self->debug(" xscreensaver locked, " . $$self{timer}->get() . " seconds remain.");
79 return 0;
80 }
81  
82 }
83  
84 if ($$self{timer}->running() and $state == 2) {
85  
86 # In unlocked mode.
87  
88 $$self{wait} = 5;
89 $$self{timer}->stop();
90 return 0;
91  
92 }
93  
94  
95 if (!$$self{timer}->running() and $state == 1) {
96  
97 # Toggled into locked mode.
98  
99 $$self{timer}->set($$self{active});
100 $$self{wait} = 0;
101 return 0;
102  
103 }
104  
105 return 0;
106  
107 }
108  
109  
110 sub state {
111  
112 my ($self) = @_;
113  
114 my $ready = $$self{poll}->poll(0.005);
115 return 0 unless $ready;
116  
117 my @readers = $$self{poll}->handles(POLLIN);
118
119 my $watch = $readers[0];
120 my $state;
121 return 0 unless $watch;
122 sysread($watch, $state, 256);
123  
124 # If we lock and unlock rapidly, we can get both LOCK and
125 # UNBLANK notifications in one read. Due to this we look
126 # negative look-ahead assertions to ensure LOCK is not
127 # followed by UNBLABK and vice versa.
128  
129 my $state_i = 0;
130  
131 if ($state =~ m/LOCK.*?(?!UNBLANK)/s) {
132 $state_i = 1;
133 }
134  
135 if ($state =~ m/UNBLANK.*?(?!LOCK)/s) {
136 $state_i = 2;
137 }
138  
139 return $state_i;
140  
141 }
142  
143  
144 sub wait {
145  
146 return $_[0]->{wait};
147  
148 }
149  
150  
151 sub DESTROY {
152  
153 my $self = shift;
154  
155 # We must cleanup by killing off the xscreensaver-command
156 # process. Since the process only writes to STDOUT when a
157 # state change happens, closing the pipe will not result in
158 # a SIGPIPE till the next state change occurs, and
159 # concequently our process will wait() on the child process
160 # to terminate. We are going to send a SIGTERM to the
161 # process manually when we are ready to shutdown.
162  
163 kill(9, $$self{pid});
164 $$self{watch}->close() if $$self{watch};
165  
166 }
167  
168  
169 1;
170