summaryrefslogtreecommitdiff
path: root/xscreensaver-logind
blob: e12ed26073597d7388d360bcef64c5fb3fe3c9ec (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
#!/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;
 
    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',
            )
        );
        return;
    }
 
    sub going_to_sleep($self,$before) {
        if ($before) {
            system(qw(xscreensaver-command -suspend));
            $self->_set_inhibit_fd(undef);
        }
        else {
            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;
    }
 
    sub Inhibit($self,$name,$reason) {
        return $self->{__inhibit_cb}->($name,$reason);
    }
 
    sub Uninhibit($self,$cookie) {
        return $self->{__uninhibit_cb}->($cookie);
    }
};
 
package Saver {
    use Moo;
    use experimental 'signatures';
    use curry;
 
    has reactor => ( is => 'ro'required => 1 );
    has bus => ( is => 'lazy'builder => sub { Net::DBus->session() } );
    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) {
        my $cookie;
        do {
            $cookie = int(rand(2**31))
        until !exists $self->_inhibits->{$cookie};
        $self->_inhibits->{$cookie} = [ $name$reason ];
 
        $self->reactor->toggle_timeout($self->_prod_id, 1);
 
        return $cookie;
    }
 
    sub uninhibit($self,$cookie) {
        my $inhibit = delete $self->_inhibits->{$cookie}
            or return;
 
        $self->reactor->toggle_timeout($self->_prod_id, 0)
            unless $self->_inhibits->%*;
 
        return;
    }
 
    sub prod_screensaver($self) {
        system(qw(xscreensaver-command -deactivate));
    }
};
 
use Net::DBus::Reactor;
 
my $reactor = Net::DBus::Reactor->main();
 
my $sleep = SleepInhibit->new();
$sleep->start();
 
my $saver = Saver->new(reactor => $reactor);
$saver->start();
 
$reactor->run();