Skip to content

Instantly share code, notes, and snippets.

@harish2704
Forked from darkxanter/dbus_handler.py
Last active April 21, 2018 09:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harish2704/dc0d1759f9cf98dcbdd12e82f577747d to your computer and use it in GitHub Desktop.
Save harish2704/dc0d1759f9cf98dcbdd12e82f577747d to your computer and use it in GitHub Desktop.
Python DBus handle hibernate, sleep and resume
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'xanter'
#from datetime import datetime
import signal
import time
import dbus
import gobject
import urllib2
import os
from dbus.mainloop.glib import DBusGMainLoop
def handle_sleep(mode):
if mode:
print("Sleep")
# TODO: allow to use custom commands
os.system('i3lock')
else:
print("Resume")
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
)
loop = gobject.MainLoop()
loop.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment