DBus/Saver.pm

filter: Statementstmt 91.3% 21 / 23 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod n/a totaltotal 93.5% 29 / 31 CC 1 SCAR 0.4
CC
1
Cov
91%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« DBus/Logind.pm ↑ Summary
1 package App::XScreenSaver::DBus::Saver;
2 1 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use experimental qw(signatures postderef);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use curry;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Log::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use IPC::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 0 ▶use App::XScreenSaver::DBus::SaverProxy;
Subroutine BEGIN: called CC=1 SCAR=2.6
10 our $VERSION = '1.0.6'; # VERSION
11 # ABSTRACT: implements the "idle inhibition" protocol
12  
13  
14 has reactor => ( is => 'ro', required => 1 );
15  
16  
17 has bus => ( is => 'lazy', builder => sub { Net::DBus->session() } );
18  
19  
20 has dbus_srv => (
21 is => 'lazy',
22 builder => sub { shift->bus->get_service('org.freedesktop.DBus') },
23 );
24  
25  
26 has dbus_obj => (
27 is => 'lazy',
28 builder => sub { shift->dbus_srv->get_object('/org/freedesktop/DBus') },
29 );
30  
31  
32 has service => (
33 is => 'lazy',
34 builder => sub {
35 # this is the service name
36 shift->bus->export_service('org.freedesktop.ScreenSaver');
37 },
38 );
39  
40  
41 has paths => (
42 is => 'ro',
43 default => sub { [qw(/ScreenSaver /org/freedesktop/ScreenSaver)] },
44 );
45  
46  
47 has log => ( is => 'lazy', builder => sub { Log::Any->get_logger } );
48  
49 has _proxies => ( is => 'rw' );
50 has _prod_id => ( is => 'rw' );
51 has _inhibits => ( is => 'rw', default => sub { +{} } );
52  
53  
54 sub start($self) {
55 # export to dbus
56 $self->_proxies([ map {
57 App::XScreenSaver::DBus::SaverProxy->new(
58 $self->service,
59 $_,
60 $self,
61 )
62 } $self->paths->@* ]);
63  
64 $self->_prod_id(
65 $self->reactor->add_timeout(
66 60_000,
67 Net::DBus::Callback->new(
68 method => $self->curry::weak::_prod_screensaver
69 ),
70 0, # this means "don't call my yet"
71 ),
72 );
73  
74 $self->dbus_obj->connect_to_signal(
75 'NameOwnerChanged',
76 $self->curry::weak::_name_owner_changed,
77 );
78  
79 return;
80 }
81  
82 sub Inhibit($self,$name,$reason,$sender) {
83 my $cookie;
84 do {
85 $cookie = int(rand(2**31))
86 } until !exists $self->_inhibits->{$cookie};
87  
88 $self->_inhibits->{$cookie} = [ $name, $reason, $sender ];
89  
90 $self->log->debugf(
91 '<%s> (%s) stops screensaver for <%s> (cookie %d) - %d active',
92 $name, $sender, $reason, $cookie, scalar(keys $self->_inhibits->%*),
93 );
94  
95 # that 1 means "start calling me"
96 $self->reactor->toggle_timeout($self->_prod_id, 1);
97  
98 return $cookie;
99 }
100  
101 sub UnInhibit($self,$cookie,$this_sender) {
102 my $inhibit = delete $self->_inhibits->{$cookie}
103 or return;
104 my ($name, $reason, $sender) = @$inhibit;
105  
106 $self->log->debugf(
107 '<%s> (was %s, is %s) resumed screensaver for <%s> (cookie %d) - %d left',
108 $name, $sender, $this_sender, $reason, $cookie, scalar(keys $self->_inhibits->%*),
109 );
110  
111 # if there's no more inhibitions, stop prodding the screen saver
112 $self->reactor->toggle_timeout($self->_prod_id, 0)
113 unless $self->_inhibits->%*;
114  
115 return;
116 }
117  
118 sub _name_owner_changed($self,$bus_name,$old,$new) {
119 $self->log->tracef('<%s> changed from <%s> to <%s>',
120 $bus_name, $old, $new);
121  
122 for my $cookie (sort keys $self->_inhibits->%*) {
123 my ($name, $reason, $sender) = @{$self->_inhibits->{$cookie}};
124 # is this inhibit from that bus name?
125 next unless $sender && $sender eq $bus_name;
126 # did the bus client just disconnect?
127 next unless $old && !$new;
128  
129 # if so, remove the inhibit
130 my $inhibit = delete $self->_inhibits->{$cookie};
131  
132 $self->log->debugf(
133 '<%s> (%s) disconnected from the bus (it stopped screensaver for <%s>, cookie %d) - %d left',
134 $name, $bus_name, $reason, $cookie, scalar(keys $self->_inhibits->%*),
135 );
136 }
137  
138 # if there's no more inhibitions, stop prodding the screen saver
139 $self->reactor->toggle_timeout($self->_prod_id, 0)
140 unless $self->_inhibits->%*;
141 }
142  
143 sub _prod_screensaver($self) {
144 $self->log->debug('prodding xscreensaver');
145 my ($out, $err);
146 IPC::Run::run(
147 [qw(xscreensaver-command -deactivate)],
148 \undef, \$out, \$err,
149 );
150 $self->log->tracef('xscreensaver-command -deactivate said <%s>',$out);
151 $self->log->warnf('xscreensaver-command -deactivate errored <%s>',$err)
152 if $err;
153 }
154  
155 1;
156  
157 __END__
« DBus/Logind.pm ↑ Summary