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;
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 log => ( is => 'lazy', builder => sub { Log::Any->get_logger } );
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;