App-XScreenSaver-DBus

 view release on metacpan or  search on metacpan

lib/App/XScreenSaver/DBus/Logind.pm  view on Meta::CPAN

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 _uninhibit($self) {
    return unless $self->inhibit_fd;

    # logind/dbus gives us a file *descriptor* (i.e. a number),
    # but `close` wants a file *handle*; this `open` creates the
    # handle without opening extra descriptors
    open my $fh, '+<&=', $self->inhibit_fd;
    close $fh;
    $self->_set_inhibit_fd(undef);
}

sub _going_to_sleep($self,$before) {
    if ($before) {
        $self->log->debug('locking');
        $self->_xscreensaver_command('-suspend');
        $self->log->debug('locked');
        $self->_uninhibit();
    }
    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.6

=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>



( run in 2.481 seconds using v1.01-cache-2.11-cpan-40ba7b3775d )