Skip to content

Commit

Permalink
Update script daemon_is_screen_locked.py for Ubuntu 18
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonidBeynenson committed Jun 14, 2022
1 parent 9741561 commit eb4e74f
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions daemon_is_screen_locked.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3.8
import os, sys, time

import daemon
Expand All @@ -9,28 +9,40 @@
f.write("")

with daemon.DaemonContext():
cmd = subprocess.Popen(["gdbus monitor -e -d com.canonical.Unity -o /com/canonical/Unity/Session"],
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()
print line
line = line.decode("utf-8")
print(line)

if "com.canonical.Unity.Session.Unlocked" in line:
log_string = "Unlocked\n"
elif "com.canonical.Unity.Session.Locked" in 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:
log_string = ""

should_write = False
prepare = False

if log_string:
if should_write:
with open(log_file, 'w') as f:
f.write(log_string)
print(f" - wrote {log_string}")

except Exception:
import traceback
Expand Down

0 comments on commit eb4e74f

Please sign in to comment.