Ubuntu – unity – how to detect if the screen is locked

detectlock-screenscreensaverunity

both of these only work after the screen that was locked gets blanked; but they sometimes fail also, when for any reason the screen doesnt blanks…

gnome-screensaver-command --query
gnome-screensaver-command --time

I tried with qdbus also:

qdbus org.gnome.ScreenSaver /org/gnome/ScreenSaver org.gnome.ScreenSaver.GetActiveTime

but it equally failed.

I just found that who actually locks the screen is Unity!

qdbus com.canonical.Unity /com/canonical/Unity/Session com.canonical.Unity.Session.Lock

Related questions:
https://unix.stackexchange.com/questions/28181/run-script-on-screen-lock-unlock
https://unix.stackexchange.com/questions/80143/how-to-create-a-daemon-which-would-be-listening-to-dbus-and-fire-script-on-messa

Best Answer

Aquarius Power's answer seems to work quite well. Here are some additions I might make to his solution.

Only querying lock state

If you simply need a one-liner to query the lock state, this should evaluate to true if locked and false if unlocked.

isLocked=$(gdbus call -e -d com.canonical.Unity -o /com/canonical/Unity/Session -m com.canonical.Unity.Session.IsLocked | grep -ioP "(true)|(false)")

Querying lock state and track time since last change in state

Now if you need to keep track of how long the screen has been locked you might want to take a different approach.

#!/bin/bash
# To implement this, you can put this at the top of a bash script or you can run
# it the subshell in a separate process and pull the functions into other scripts.

# We need a file to keep track of variable inside subshell the file will contain
# two elements, the state and timestamp of time changed, separated by a tab.
# A timestamp of 0 indicates that the state has not changed since we started
# polling for changes and therefore, the time lapsed in the current state is
# unknown.
vars="/tmp/lock-state"

# start watching the screen lock state
(
    # set the initial value for lock state
    [ "$(gdbus call -e -d com.canonical.Unity -o /com/canonical/Unity/Session -m com.canonical.Unity.Session.IsLocked | grep -ioP "(true)|(false)")" == "true" ] && state="locked" || state="unlocked"
    printf "%s\t%d" $state 0 > "$vars"
    
    # start watching changes in state
    gdbus monitor -e -d com.canonical.Unity -o /com/canonical/Unity/Session | while read line
    do 
        state=$(grep -ioP "((un)?locked)" <<< "$line")
        # If the line read denotes a change in state, save it to a file with timestamp for access outside this subshell
        [ "$state" != "" ] && printf "%s\t%d" ${state,,} $(date +%s)> "$vars"
    done
) & # don't wait for this subshell to finish

# Get the current state from the vars exported in the subshell
function getState {
    echo $(cut -f1 "$vars")
}

# Get the time in seconds that has passed since the state last changed
function getSecondsElapsed {
    if [ $(cut -f2 "$vars") -ne 0 ]; then
        echo $(($(date +%s)-$(cut -f2 "$vars")))
    else
        echo "unknown"
    fi
}

Essentially, this script watches for changes in the lock state of the screen. When changes take place, the time and state is dumped to a file. You can read this file manually if you like or use the functions I wrote.

If you want a timestamp rather than the number of seconds, try:

date -ud @$(getSecondsElapsed) | grep -oP "(\d{2}:){2}\d{2}"

Don't forget the -u switch which forces the date program to ignore your timezone.