Skip to content

Instantly share code, notes, and snippets.

@grawity
Last active December 11, 2017 16:49
Show Gist options
  • Save grawity/a10ee46d7ff58048d483 to your computer and use it in GitHub Desktop.
Save grawity/a10ee46d7ff58048d483 to your computer and use it in GitHub Desktop.
#!/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