Created
February 8, 2016 20:35
-
-
Save lexelby/1c5066c2bdcaf1235b6b 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
| #!/usr/bin/env python | |
| import os | |
| import dbus | |
| import gobject | |
| from dbus.mainloop.glib import DBusGMainLoop | |
| def hook(name): | |
| os.system("/bin/run-parts " + os.path.expanduser("~/hooks/%s.d/") % name) | |
| def handle_sleep(*args): | |
| print "sleep hook:", args | |
| if args[0]: | |
| hook("sleep") | |
| else: | |
| hook("resume") | |
| def handle_lock(*args): | |
| print "lock hook:", args | |
| if args[0]: | |
| hook("lock") | |
| def handle_lid(*args): | |
| if args[1]['LidIsClosed']: | |
| print "lid:", args | |
| hook("lid") | |
| def handle_bluetooth_headset(*args): | |
| if args[1]['Connected'] and args[0] == "org.bluez.MediaControl1": | |
| print "headset:", args | |
| hook("headset-connected") | |
| DBusGMainLoop(set_as_default=True) # integrate into gobject main loop | |
| bus = dbus.SystemBus() # connect to system wide dbus | |
| bus.add_signal_receiver( # define the signal to listen to | |
| handle_sleep, # callback function | |
| 'PrepareForSleep', # signal name | |
| 'org.freedesktop.login1.Manager', # interface | |
| 'org.freedesktop.login1' # bus name | |
| ) | |
| bus.add_signal_receiver( | |
| handle_lid, | |
| 'PropertiesChanged', | |
| 'org.freedesktop.DBus.Properties', | |
| path='/org/freedesktop/UPower', | |
| ) | |
| bus.add_signal_receiver( | |
| handle_bluetooth_headset, | |
| 'PropertiesChanged', | |
| 'org.freedesktop.DBus.Properties', | |
| path="/org/bluez/hci0/dev_00_1A_7D_11_13_53" | |
| ) | |
| bus = dbus.SessionBus() | |
| bus.add_signal_receiver( | |
| handle_lock, | |
| 'ActiveChanged', | |
| 'org.freedesktop.ScreenSaver', | |
| path='/org/freedesktop/ScreenSaver', | |
| ) | |
| loop = gobject.MainLoop() | |
| loop.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment