Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env perl
use warnings;
use strict;
use Net::DBus;
use Net::DBus::Reactor;
use POSIX;
my $inhibit_fd;
my $logind_mgr;
sub pre_suspend {
system("/usr/bin/psd", "sync");
}
sub take_inhibit {
if (!defined $inhibit_fd) {
$inhibit_fd = $logind_mgr->Inhibit("sleep", "psd", "psd wants to sync", "delay");
}
}
sub drop_inhibit {
if (defined $inhibit_fd) {
POSIX::close($inhibit_fd);
$inhibit_fd = undef;
}
}
$logind_mgr = Net::DBus->system()
->get_service("org.freedesktop.login1")
->get_object("/org/freedesktop/login1");
$logind_mgr->connect_to_signal("PrepareForSleep", sub {
my ($suspending) = @_;
if ($suspending) {
pre_suspend();
drop_inhibit();
} else {
take_inhibit();
}
});
take_inhibit();
Net::DBus::Reactor->main()->run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment