Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 59 lines (51 sloc) 1.87 KB
#!/usr/bin/env python
# systemd-lock-handler -- proxy between systemd-logind's "Lock" signal and your
# favourite screen lock command
from __future__ import print_function
import os, sys, dbus, dbus.mainloop.glib
from gi.repository import GLib
def trace(*args):
global arg0
print("%s:" % arg0, *args)
def setup_signal(signal_handler):
global session_id
bus = dbus.SystemBus()
manager = bus.get_object("org.freedesktop.login1", "/org/freedesktop/login1")
# yecch
manager = dbus.Interface(manager, "org.freedesktop.login1.Manager")
session_path = manager.GetSession(session_id)
session = bus.get_object("org.freedesktop.login1", session_path)
session.connect_to_signal("Lock", signal_handler)
def handler_dbus_fdo():
trace("locking session using DBus")
bus = dbus.SessionBus()
screensaver = bus.get_object("org.freedesktop.ScreenSaver", "/ScreenSaver")
screensaver.Lock()
def handler_external():
global lock_command
trace("locking session using %r" % lock_command[0])
os.spawnvp(os.P_NOWAIT, lock_command[0], lock_command)
def main():
global arg0, lock_command, session_id
arg0 = sys.argv[0].split("/")[-1]
lock_command = sys.argv[1:] or ["--dbus"]
try:
session_id = os.environ["XDG_SESSION_ID"]
except KeyError:
print("error: $XDG_SESSION_ID not set; are you using pam_systemd?",
file=sys.stderr)
sys.exit(1)
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
if lock_command == ["--dbus"]:
trace("using freedesktop.org DBus API")
setup_signal(handler_dbus_fdo)
else:
trace("using external command %r" % lock_command[0])
setup_signal(handler_external)
trace("waiting for lock signals on session %s" % session_id)
try:
loop = GLib.MainLoop()
loop.run()
except KeyboardInterrupt:
sys.exit(0)
main()
You can’t perform that action at this time.