Might as well answer this although it's not my answer (my boss provided me with the solution) to help others with the same need (or curious minds).
tl;dr: Use D-Bus to get lockscreen's actived/deactived signals.
It seems D-Bus messaging is widely supported by GNOME apps, and the ScreenSaver app specifically, and it seems that the gnome-screensaver app is the official locking app for Gnome shell.
So, to test it out, I just ran dbus-monitor and get the response:
dbus-monitor --session "type='signal',interface='org.gnome.ScreenSaver'"
I've written a little python script to log this activity into a file:
#!/usr/bin/env python
from datetime import datetime
import os
import pwd
import subprocess
import time
LOG_FILE = os.path.expanduser('~/hours_log.csv')
cmd = subprocess.Popen(["dbus-monitor \"type='signal',interface="
"'org.gnome.ScreenSaver'\""], shell=True,
stdout=subprocess.PIPE)
running = 0
while 1:
time.sleep(0.1)
if running:
output = cmd.stdout.readline()
status = 'unlocked' if 'true' in output else 'locked'
new_line = "{time} {user} {status} the screen\n".format(
time=datetime.now().ctime(),
user=pwd.getpwuid(os.getuid())[0],
status=status
)
with open(LOG_FILE, 'a') as f:
f.write(new_line)
running = 0
line = cmd.stdout.readline()
if "ActiveChange" in line and 'org.gnome.ScreenSaver' in line:
running = 1
I put this in a
.sh
file and added it to Gnome's startup applications. Does what it says on the tin, thx – aross – 2017-12-18T14:34:42.400