Created
June 10, 2018 23:48
-
-
Save Gaff/fab083771d316c783370c8582d0ab5f2 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 python3 | |
from pydbus import SystemBus | |
from gi.repository import GLib | |
from functools import partial | |
import logging | |
import os | |
TIMEOUT = 900 | |
event = 0 | |
logging.basicConfig(level=logging.DEBUG) | |
bus = SystemBus() | |
def handlePropertiesChanged(dbus, _busname, properties, _dunno): | |
# We only care about Locked Events | |
if(not 'LockedHint' in properties): | |
return | |
lock = properties['LockedHint'] | |
logging.debug( "%s - %s" % (dbus._path, lock )) | |
# Sorry | |
global event | |
if(lock): | |
if(event == 0): | |
logging.debug("Starting timer") | |
event = GLib.timeout_add_seconds(TIMEOUT, handleTimeout) | |
else: | |
if(event != 0): | |
logging.debug("Cancelling timer") | |
GLib.source_remove(event) | |
event = 0 | |
def handleTimeout(): | |
logging.warning("User session idle for a long time.") | |
event = 0 | |
os.system("sudo -n poweroff") | |
def findAndRegisterSessions(): | |
login = bus.get(".login1") | |
# Find the dbus path for all the existing sessions. | |
sessions = [x[4] for x in login.ListSessions()] | |
# Listen to sessions. (This begs the question how should we unsubscribe?) | |
for sess in sessions: | |
sdb = bus.get(".login1", sess) | |
# All sessions or just active? Hmm. | |
#if(sdb.Active): | |
logging.debug( "registering - %s" % (sdb._path)) | |
sdb.onPropertiesChanged = partial(handlePropertiesChanged, sdb) | |
def main(): | |
findAndRegisterSessions() | |
GLib.MainLoop().run() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment