Best way to get notified on screen lock/unlock

My home automation system needs to know if my computer screen is locked or unlocked to properly operate lighting in my office. In win10 it’s a set of events (logon/logoff/screen lock/screen unlock), very easy to set up.

My understanding is that screen lock & unlock happens in Dom0? Is there a way to get notified when this happens? What’s the best way?

Yes, it is either dom0 or sys-gui. Thus, the proper way is to implement a script that monitors xscreensaver events (xscreensaver-command -watch) and then instructs a network-enabled qube (either via qvm-run or qrexec service) to take appropriate action.

1 Like

Ok, so just in case anyone else needs this, that’s how I did it.

  1. As instructed above, I monitor the output of xscreensaver-command -watch and send mosquitto_pub command to a VM that has local connectivity (wg-tailscale):
#!/bin/bash

while read line
do
  [[ $line =~ ^LOCK ]] && STATE='locked'
  [[ $line =~ ^UNBLANK ]] && STATE='unlocked'

  CMD=(qvm-run -p wg-tailscale "'"mosquitto_pub -h my_mqtt_host -t qubes-comupter-name/lock_status -m $STATE"'")
  echo "${CMD[@]}"|sh
done < <(/usr/bin/xscreensaver-command -watch)

(I know echo ...|sh is ugly, but my brain fried while trying to figure out another way)

  1. To make it auto-start I created this qubes-monitor-screenlock.desktop file in .config/autostart of dom0:
[Desktop Entry]
Type=Application
Name=ScreenLockMon
Exec=/home/chuck/screenlock_monitor.sh
StartupNotify=true
Terminal=false