Created
November 25, 2015 22:57
-
-
Save heftig/b67b0bf48cb7fb0fc4e4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from gi.repository import GLib, Gio | |
# --- | |
def handle_lock(): | |
print("Lock") | |
def handle_unlock(): | |
print("Unlock") | |
def handle_sleep(): | |
print("Sleep") | |
def handle_resume(): | |
print("Resume") | |
# --- | |
class Manager(Gio.DBusProxy): | |
def __init__(self): | |
Gio.DBusProxy.__init__(self, g_bus_type=Gio.BusType.SYSTEM, g_name="org.freedesktop.login1", | |
g_object_path="/org/freedesktop/login1", g_interface_name="org.freedesktop.login1.Manager") | |
self.init(None) | |
class Session(Gio.DBusProxy): | |
def __init__(self, session_id): | |
session_path = Manager().GetSession("(s)", str(session_id)) | |
Gio.DBusProxy.__init__(self, g_bus_type=Gio.BusType.SYSTEM, g_name="org.freedesktop.login1", | |
g_object_path=session_path, g_interface_name="org.freedesktop.login1.Session") | |
self.init(None) | |
# --- | |
class SleepHandler(Manager): | |
def __init__(self): | |
Manager.__init__(self) | |
self.inhibit() | |
def inhibit(self): | |
self.fd = self.Inhibit("(ssss)", "sleep", "sleep.py", "do something during suspend", | |
"delay") | |
def do_g_signal(self, sender, signal, params): | |
if signal == "PrepareForSleep": | |
p = params.unpack() | |
if p[0]: | |
# About to go to sleep | |
handle_sleep() | |
os.close(self.fd) | |
else: | |
# Woke up from sleep | |
self.inhibit() | |
handle_resume() | |
return True | |
Manager.do_g_signal(self, sender, signal, params) | |
class LockHandler(Session): | |
def do_g_signal(self, sender, signal, params): | |
if signal == "Lock": | |
handle_lock() | |
return True | |
if signal == "Unlock": | |
handle_unlock() | |
return True | |
Session.do_g_signal(self, sender, signal, params) | |
# --- | |
sleep = SleepHandler() | |
lock = LockHandler(os.environ["XDG_SESSION_ID"]) | |
GLib.MainLoop().run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment