I am trying to implement a dbus listener in Python, that executes a function, whenever the session is locked and another function, when the session is unlocked. Basically, I want to turn the following command into a Python program (obviously, that command doesn't have a callback):
gdbus monitor -y -d org.freedesktop.login1 | grep LockedHint
This command returns the following:
/org/freedesktop/login1/session/_32: org.freedesktop.DBus.Properties.PropertiesChanged ('org.freedesktop.login1.Session', {'LockedHint': <true>}, @as [])
/org/freedesktop/login1/session/_32: org.freedesktop.DBus.Properties.PropertiesChanged ('org.freedesktop.login1.Session', {'LockedHint': <false>}, @as [])
The Python MWE is here. However, it doesn't get any messages (tried starting it with sudo and as the user itself):
#!/usr/bin/env python
from datetime import datetime
import dbus
import gobject
from dbus.mainloop.glib import DBusGMainLoop
def locker_callback(*args):
print "%s Lock%s" % (datetime.now().ctime(), args)
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
locker_callback, # callback function
'LockedHint', # signal name
'org.freedesktop.DBus.Properties.PropertiesChanged', # interface
'org.freedesktop.login1' # bus name
)
loop = gobject.MainLoop()
loop.run()
Just for reference, I run xfce on Ubuntu 18.04, with XScreenSaver.