summaryrefslogtreecommitdiff
path: root/xscreensaver-dbus
blob: 002c0d9336a461ad1e2a011e08be2d90116e5ab7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#!/home/dakkar/perl5/perlbrew/perls/perl-5.32.0/bin/perl 
use strict;
use warnings;
 
=pod
 
see https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=781961
 
https://www.freedesktop.org/wiki/Software/systemd/inhibit/
 
https://stackoverflow.com/questions/460140/is-there-a-decent-way-to-inhibit-screensavers-in-linux
 
https://github.com/mato/xscreensaver-systemd/blob/master/xscreensaver-systemd.c
 
=cut
 
package SleepInhibit {
    use Moo;
    use experimental 'signatures';
    use curry;
    use Net::DBus;
    use Log::Any '$log';
 
    has bus => ( is => 'lazy'builder => sub { Net::DBus->system() } );
    has logind_srv => (
        is => 'lazy',
        builder => sub { shift->bus->get_service('org.freedesktop.login1') },
    );
    has logind_obj => (
        is => 'lazy',
        builder => sub { shift->logind_srv->get_object('/org/freedesktop/login1') },
    );
 
    has inhibit_fd => ( is => 'rwp' );
 
    sub start($self) {
        $self->logind_obj->connect_to_signal(
            'PrepareForSleep',
            $self->curry::weak::going_to_sleep,
        );
        $self->inhibit();
        return;
    }
 
    sub inhibit($self) {
        return if $self->inhibit_fd;
        $self->_set_inhibit_fd(
            $self->logind_obj->Inhibit(
                'sleep',
                'xscreensaver','locking before sleep',
                'delay',
            )
        );
        $log->debugf('got logind inhibit fd %d',$self->inhibit_fd);
        return;
    }
 
    sub going_to_sleep($self,$before) {
        if ($before) {
            $log->debug('locking');
            system(qw(xscreensaver-command -suspend));
            $log->debug('locked');
            $self->_set_inhibit_fd(undef);
        }
        else {
            $log->debug('woken up');
            system(qw(xscreensaver-command -deactivate));
            $self->inhibit();
        }
        return;
    }
};
 
package SaverImpl {
    use strict;
    use warnings;
    use experimental 'signatures';
    # this is the interface name 
    use Net::DBus::Exporter qw(org.freedesktop.ScreenSaver);
    use parent 'Net::DBus::Object';
 
    dbus_method('Inhibit',['string','string'],['uint32']);
    dbus_method('UnInhibit',['uint32'],[]);
 
    sub new($class,$service,$path,$inhibit_cb,$uninhibit_cb) {
        my $self = $class->SUPER::new($service$path);
        bless $self$class;
        $self->{__inhibit_cb} = $inhibit_cb;
        $self->{__uninhibit_cb} = $uninhibit_cb;
        return $self;
    }
 
    our $_message;
    sub _dispatch_object($self,$connection,$message,@etc) {
        local $_message = $message;
        return $self->SUPER::_dispatch_object($connection,$message,@etc);
    }
 
    sub Inhibit($self,$name,$reason) {
        return $self->{__inhibit_cb}->($name,$reason,$_message);
    }
 
    sub UnInhibit($self,$cookie) {
        return $self->{__uninhibit_cb}->($cookie,$_message);
    }
};
 
package Saver {
    use Moo;
    use experimental 'signatures';
    use curry;
    use Log::Any '$log';
    use Try::Tiny;
 
    has reactor => ( is => 'ro'required => 1 );
    has bus => ( is => 'lazy'builder => sub { Net::DBus->session() } );
    has dbus_srv => ( is => 'lazy'builder => sub { shift->bus->get_service('org.freedesktop.DBus') });
    has dbus_obj => ( is => 'lazy'builder => sub { shift->dbus_srv->get_object('/org/freedesktop/DBus') });
 
    has service => (
        is => 'lazy',
        builder => sub {
            # this is the service name 
            shift->bus->export_service('org.freedesktop.ScreenSaver');
        },
    );
    has paths => (
        is => 'ro',
        default => sub { [qw(/ScreenSaver /org/freedesktop/ScreenSaver)] },
    );
 
    has _impls => ( is => 'rw' );
    has _prod_id => ( is => 'rw' );
    has _inhibits => ( is => 'rw'default => sub { +{} } );
 
    sub start($self) {
        my $inhibit_cb = $self->curry::weak::inhibit;
        my $uninhibit_cb = $self->curry::weak::uninhibit;
 
        $self->_impls([ map {
            SaverImpl->new(
                $self->service,
                $_,
                $inhibit_cb,
                $uninhibit_cb,
            )
        $self->paths->@* ]);
 
        $self->_prod_id(
            $self->reactor->add_timeout(
                60_000,
                Net::DBus::Callback->new(
                    method => $self->curry::weak::prod_screensaver
                ),
                0,
            ),
        );
 
        return;
    }
 
    sub inhibit($self,$name,$reason,$message) {
        my $sender_pid = try {
            return $self->dbus_obj->GetConnectionUnixProcessID(
                $message->get_sender,
            );
        } catch {
            $log->warning('problems getting PID of inhibitor',{error=>$_});
            return undef;
        };
 
        my $cookie;
        do {
            $cookie = int(rand(2**31))
        until !exists $self->_inhibits->{$cookie};
 
        $self->_inhibits->{$cookie} = [ $name$reason$sender_pid ];
 
        $log->debugf(
            '<%s> stops screensaver for <%s> (cookie %d) - %d active',
            $name$reason$cookiescalar(keys $self->_inhibits->%*),
        );
        $self->reactor->toggle_timeout($self->_prod_id, 1);
 
        return $cookie;
    }
 
    sub uninhibit($self,$cookie,$message) {
        my $inhibit = delete $self->_inhibits->{$cookie}
            or return;
        my ($name$reason$sender_pid) = @$inhibit;
 
        $log->debugf(
            '<%s> resumed screensaver for <%s> (cookie %d) - %d left',
            $name$reason$cookiescalar(keys $self->_inhibits->%*),
        );
 
        $self->reactor->toggle_timeout($self->_prod_id, 0)
            unless $self->_inhibits->%*;
 
        return;
    }
 
    sub prod_screensaver($self) {
        for my $cookie (sort keys $self->_inhibits->%*) {
            my ($name$reason$sender_pid) = @{$self->_inhibits->{$cookie}};
            next if kill 0, $sender_pid;
            # uh oh, caller is dead 
            my $inhibit = delete $self->_inhibits->{$cookie};
 
            $log->debugf(
                '<%s> is dead (it stopped screensaver for <%s>, cookie %d) - %d left',
                $name$reason$cookiescalar(keys $self->_inhibits->%*),
            );
        }
 
        unless ($self->_inhibits->%*) {
            $self->reactor->toggle_timeout($self->_prod_id, 0);
            return;
        }
 
        $log->debug('prodding xscreensaver');
        system(qw(xscreensaver-command -deactivate));
    }
};
 
use Net::DBus::Reactor;
use Log::Any::Adapter Stdout => ( log_level => 'debug' );
 
my $reactor = Net::DBus::Reactor->main();
 
my $sleep = SleepInhibit->new();
$sleep->start();
 
my $saver = Saver->new(reactor => $reactor);
$saver->start();
 
$reactor->run();