summaryrefslogtreecommitdiff
path: root/lib/App/XScreenSaver/DBus/Logind.pm
blob: 0c2abee8daf0c61c7f575a6a47440a0166989c06 (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
package App::XScreenSaver::DBus::Logind; 
use v5.20;
use Moo;
use experimental qw(signatures postderef);
use curry;
use Net::DBus;
use IPC::Run;
use Log::Any;
our $VERSION = '1.0.3'# VERSION 
# ABSTRACT: implements the logind "inhibitor locks" and "session lock" protocols 
 
 
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 session_obj => (
    is => 'lazy',
    builder => sub($self) {
        my $session_path = $self->logind_obj->GetSessionByPID($$);
        return $self->logind_srv->get_object($session_path);
    },
);
 
 
has inhibit_fd => ( is => 'rwp' );
 
 
has log => ( is => 'lazy'builder => sub { Log::Any->get_logger } );
 
 
sub start($self) {
    $self->logind_obj->connect_to_signal(
        'PrepareForSleep',
        $self->curry::weak::_going_to_sleep,
    );
    $self->session_obj->connect_to_signal(
        'Lock',
        $self->curry::weak::_lock,
    );
    $self->session_obj->connect_to_signal(
        'Unlock',
        $self->curry::weak::_unlock,
    );
    $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',
        )
    );
    $self->log->debugf('got logind inhibit fd %d',$self->inhibit_fd);
    return;
}
 
sub _going_to_sleep($self,$before) {
    if ($before) {
        $self->log->debug('locking');
        $self->_xscreensaver_command('-suspend');
        $self->log->debug('locked');
        $self->_set_inhibit_fd(undef);
    }
    else {
        $self->log->debug('woken up');
        $self->_xscreensaver_command('-deactivate');
        $self->_inhibit();
    }
    return;
}
 
sub _lock($self) {
    $self->log->debugf('locking the screen');
    $self->_xscreensaver_command('-lock');
}
 
sub _unlock($self) {
    $self->log->debugf('unlocking the screen');
    $self->_xscreensaver_command('-deactivate');
}
 
sub _xscreensaver_command($self,$command) {
    my ($out$err);
    IPC::Run::run(
        ['xscreensaver-command',$command],
        \undef, \$out, \$err,
    );
    $self->log->tracef('xscreensaver-command %s said <%s>',$command,$out);
    $self->log->warnf('xscreensaver-command %s errored <%s>',$command,$err)
        if $err;
}
 
1;
 
__END__
 
=pod
 
=encoding UTF-8
 
=head1 NAME
 
App::XScreenSaver::DBus::Logind - implements the logind "inhibitor locks" and "session lock" protocols
 
=head1 VERSION
 
version 1.0.3
 
=head1 SYNOPSIS
 
    use Net::DBus::Reactor;
    use App::XScreenSaver::DBus::Logind;
    my $is = App::XScreenSaver::DBus::Logind->new;
    $is->start;
 
    Net::DBus::Reactor->new->run;
 
=head1 ATTRIBUTES
 
=head2 C<bus>
 
the DBus system bus
 
=head2 C<logind_srv>
 
the (e)logind DBus service
 
=head2 C<logind_obj>
 
the (e)logind DBus object
 
=head2 C<session_obj>
 
the (e)logind session DBus object
 
=head2 C<inhibit_fd>
 
the file descriptor that logind gives us when we ask for a lock; we
close it to release the lock
 
=head2 C<log>
 
a logger
 
=head1 METHODS
 
=head2 C<start>
 
starts listening to the C<PrepareForSleep> signal from (e)logind, and
takes the lock
 
=head1 AUTHOR
 
Gianni Ceccarelli <dakkar@thenautilus.net>
 
=head1 COPYRIGHT AND LICENSE
 
This software is Copyright (c) 2021 by Gianni Ceccarelli <dakkar@thenautilus.net>.
 
This is free software, licensed under:
 
  The GNU Affero General Public License, Version 3, November 2007
 
=cut