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
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.
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.
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:
Don't forget the
-u
switch which forces the date program to ignore your timezone.the screen is actually locked by Unity, and, we need to use
gdbus
this will show when it got locked like:
I had a similar question here
and the help I got was similar to what Aquarius Power said before, except that it was included in a bash scrip daemon, that can run in the background.. I found it very helpful. so, have a look at my Question, and answer, and see if this helps you also.