| 1 | package App::XScreenSaver::DBus::Logind; | ||
| 2 | 1 | ▶ | use v5.20; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 1 | ▶ | use Moo; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use experimental qw(signatures postderef); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use curry; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 0 | ▶ | use Net::DBus; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 7 | use IPC::Run; | ||
| 8 | use Log::Any; | ||
| 9 | our $VERSION = '1.0.6'; # VERSION | ||
| 10 | # ABSTRACT: implements the logind "inhibitor locks" and "session lock" protocols | ||
| 11 | |||
| 12 | |||
| 13 | has bus => ( is => 'lazy', builder => sub { Net::DBus->system() } ); | ||
| 14 | |||
| 15 | |||
| 16 | has logind_srv => ( | ||
| 17 | is => 'lazy', | ||
| 18 | builder => sub { shift->bus->get_service('org.freedesktop.login1') }, | ||
| 19 | ); | ||
| 20 | |||
| 21 | |||
| 22 | has logind_obj => ( | ||
| 23 | is => 'lazy', | ||
| 24 | builder => sub { shift->logind_srv->get_object('/org/freedesktop/login1') }, | ||
| 25 | ); | ||
| 26 | |||
| 27 | |||
| 28 | has session_obj => ( | ||
| 29 | is => 'lazy', | ||
| 30 | builder => sub($self) { | ||
| 31 | my $session_path = $self->logind_obj->GetSessionByPID($$); | ||
| 32 | return $self->logind_srv->get_object($session_path); | ||
| 33 | }, | ||
| 34 | ); | ||
| 35 | |||
| 36 | |||
| 37 | has inhibit_fd => ( is => 'rwp' ); | ||
| 38 | |||
| 39 | |||
| 40 | has log => ( is => 'lazy', builder => sub { Log::Any->get_logger } ); | ||
| 41 | |||
| 42 | |||
| 43 | sub start($self) { | ||
| 44 | $self->logind_obj->connect_to_signal( | ||
| 45 | 'PrepareForSleep', | ||
| 46 | $self->curry::weak::_going_to_sleep, | ||
| 47 | ); | ||
| 48 | $self->session_obj->connect_to_signal( | ||
| 49 | 'Lock', | ||
| 50 | $self->curry::weak::_lock, | ||
| 51 | ); | ||
| 52 | $self->session_obj->connect_to_signal( | ||
| 53 | 'Unlock', | ||
| 54 | $self->curry::weak::_unlock, | ||
| 55 | ); | ||
| 56 | $self->_inhibit(); | ||
| 57 | return; | ||
| 58 | } | ||
| 59 | |||
| 60 | sub _inhibit($self) { | ||
| 61 | return if $self->inhibit_fd; | ||
| 62 | $self->_set_inhibit_fd( | ||
| 63 | $self->logind_obj->Inhibit( | ||
| 64 | 'sleep', | ||
| 65 | 'xscreensaver','locking before sleep', | ||
| 66 | 'delay', | ||
| 67 | ) | ||
| 68 | ); | ||
| 69 | $self->log->debugf('got logind inhibit fd %d',$self->inhibit_fd); | ||
| 70 | return; | ||
| 71 | } | ||
| 72 | |||
| 73 | sub _uninhibit($self) { | ||
| 74 | return unless $self->inhibit_fd; | ||
| 75 | |||
| 76 | # logind/dbus gives us a file *descriptor* (i.e. a number), | ||
| 77 | # but `close` wants a file *handle*; this `open` creates the | ||
| 78 | # handle without opening extra descriptors | ||
| 79 | open my $fh, '+<&=', $self->inhibit_fd; | ||
| 80 | close $fh; | ||
| 81 | $self->_set_inhibit_fd(undef); | ||
| 82 | } | ||
| 83 | |||
| 84 | sub _going_to_sleep($self,$before) { | ||
| 85 | if ($before) { | ||
| 86 | $self->log->debug('locking'); | ||
| 87 | $self->_xscreensaver_command('-suspend'); | ||
| 88 | $self->log->debug('locked'); | ||
| 89 | $self->_uninhibit(); | ||
| 90 | } | ||
| 91 | else { | ||
| 92 | $self->log->debug('woken up'); | ||
| 93 | $self->_xscreensaver_command('-deactivate'); | ||
| 94 | $self->_inhibit(); | ||
| 95 | } | ||
| 96 | return; | ||
| 97 | } | ||
| 98 | |||
| 99 | sub _lock($self) { | ||
| 100 | $self->log->debugf('locking the screen'); | ||
| 101 | $self->_xscreensaver_command('-lock'); | ||
| 102 | } | ||
| 103 | |||
| 104 | sub _unlock($self) { | ||
| 105 | $self->log->debugf('unlocking the screen'); | ||
| 106 | $self->_xscreensaver_command('-deactivate'); | ||
| 107 | } | ||
| 108 | |||
| 109 | sub _xscreensaver_command($self,$command) { | ||
| 110 | my ($out, $err); | ||
| 111 | IPC::Run::run( | ||
| 112 | ['xscreensaver-command',$command], | ||
| 113 | \undef, \$out, \$err, | ||
| 114 | ); | ||
| 115 | $self->log->tracef('xscreensaver-command %s said <%s>',$command,$out); | ||
| 116 | $self->log->warnf('xscreensaver-command %s errored <%s>',$command,$err) | ||
| 117 | if $err; | ||
| 118 | } | ||
| 119 | |||
| 120 | 1; | ||
| 121 | |||
| 122 | __END__ | ||