summaryrefslogtreecommitdiff
path: root/lib/App/XScreenSaver/DBus/InhibitSleep.pm
blob: 44f84bc3569d8916f1d7eb2b61240a8ed4ebae0a (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
package App::XScreenSaver::DBus::InhibitSleep; 
use Moo;
use experimental 'signatures';
use curry;
use Net::DBus;
use Log::Any;
 
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' );
 
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->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');
        system(qw(xscreensaver-command -suspend));
        $self->log->debug('locked');
        $self->_set_inhibit_fd(undef);
    }
    else {
        $self->log->debug('woken up');
        system(qw(xscreensaver-command -deactivate));
        $self->inhibit();
    }
    return;
}
 
1;