summaryrefslogtreecommitdiff
path: root/lib/App/XScreenSaver/DBus/Lock.pm
blob: 22410f21e3a9f98393fe200dfd61df4639fcd96c (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
package App::XScreenSaver::DBus::Lock; 
use v5.20;
use Moo;
use experimental qw(signatures postderef);
use curry;
use Net::DBus;
use IPC::Run;
use Log::Any;
# VERSION 
# ABSTRACT: implements the logind "session lock" protocol 
 
=head1 SYNOPSIS
 
    use Net::DBus::Reactor;
    use App::XScreenSaver::DBus::Lock;
    my $is = App::XScreenSaver::DBus::Lock->new;
    $is->start;
 
    Net::DBus::Reactor->new->run;
 
=attr C<bus>
 
the DBus system bus
 
=cut
 
has bus => ( is => 'lazy'builder => sub { Net::DBus->system() } );
 
=attr C<logind_srv>
 
the (e)logind DBus service
 
=cut
 
has logind_srv => (
    is => 'lazy',
    builder => sub { shift->bus->get_service('org.freedesktop.login1') },
);
 
=attr C<logind_obj>
 
the (e)logind DBus object
 
=cut
 
has logind_obj => (
    is => 'lazy',
    builder => sub { shift->logind_srv->get_object('/org/freedesktop/login1') },
);
 
=attr C<session_obj>
 
the (e)logind session DBus object
 
=cut
 
has session_obj => (
    is => 'lazy',
    builder => sub($self) {
        my $session_path = $self->logind_obj->GetSessionByPID($$);
        return $self->logind_srv->get_object($session_path);
    },
);
 
=attr C<log>
 
a logger
 
=cut
 
has log => ( is => 'lazy'builder => sub { Log::Any->get_logger } );
 
=method C<start>
 
starts listening to the C<Lock> and C<Unlock> signals from the
session, and activates the screen saver
 
=cut
 
sub start($self) {
    $self->session_obj->connect_to_signal(
        'Lock',
        $self->curry::weak::_lock,
    );
    $self->session_obj->connect_to_signal(
        'Unlock',
        $self->curry::weak::_unlock,
    );
    return;
}
 
sub _lock($self) {
    $self->_xscreensaver_command('-lock');
}
 
sub _unlock($self) {
    $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;