Saturday, April 27, 2024
 Popular · Latest · Hot · Upcoming
7
rated 0 times [  7] [ 0]  / answers: 1 / hits: 5960  / 2 Years ago, fri, october 28, 2022, 1:54:45

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


More From » unity

 Answers
2

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 %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 %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.


[#23871] Saturday, October 29, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ionodest

Total Points: 252
Total Questions: 122
Total Answers: 100

Location: Liechtenstein
Member since Tue, Apr 27, 2021
3 Years ago
;