-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdaemon_is_screen_locked.py
executable file
·51 lines (42 loc) · 1.48 KB
/
daemon_is_screen_locked.py
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
#!/usr/bin/env python3.8
import os, sys, time
import daemon
import subprocess
log_file = os.path.abspath(sys.argv[1])
with open(log_file, 'w') as f:
f.write("")
with daemon.DaemonContext():
cmd = subprocess.Popen(["dbus-monitor --session \"type='signal',interface='org.gnome.ScreenSaver'\""],
shell=True, stdout=subprocess.PIPE)
prepare = False
should_write = False
log_string = ""
while True:
time.sleep(0.1)
# with open(log_file, 'a') as f:
# f.write("========\n")
try:
line = cmd.stdout.readline()
line = line.decode("utf-8")
print(line)
if "path=/org/gnome/ScreenSaver; interface=org.gnome.ScreenSaver; member=ActiveChanged" in line:
prepare = True
print(" - set prepare")
continue
if prepare and "boolean true" in line:
log_string = "Locked\n"
should_write = True
elif prepare and "boolean false" in line:
log_string = "Unlocked\n"
should_write = True
else:
should_write = False
prepare = False
if should_write:
with open(log_file, 'w') as f:
f.write(log_string)
print(f" - wrote {log_string}")
except Exception:
import traceback
with open(log_file+".err", 'a') as f:
traceback.print_exc(None, f);