-
Notifications
You must be signed in to change notification settings - Fork 0
/
systemd-lock-handler
executable file
·58 lines (51 loc) · 1.87 KB
/
systemd-lock-handler
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/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()