unity-如何检测屏幕是否被锁定?


9

这两种方法只有在锁定的屏幕变黑后才起作用;但有时由于某些原因屏幕不黑屏时,它们也会失败...

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

qdbus也尝试过:

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

但是同样失败了。

我只是发现实际上锁定屏幕的人是Unity!

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

相关问题:
https : //unix.stackexchange.com/questions/28181/run-script-on-screen-lock-unlock /unix/80143/how-to-create-a-守护程序将要听dbus并在message上发送脚本

Answers:


6

Aquarius Power的答案似乎效果很好。这是我可能会对他的解决方案做的一些补充。

仅查询锁定状态

如果您只需要单线查询锁定状态,则锁定时应评估为true,如果解锁则应评估为false。

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

查询锁状态自上次状态更改以来的跟踪时间

现在,如果您需要跟踪屏幕被锁定的时间,则可以采取其他方法。

#!/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
}

本质上,此脚本监视屏幕锁定状态的变化。发生更改时,时间和状态将转储到文件中。如果愿意,可以手动读取此文件,也可以使用我编写的功能。

如果要时间戳而不是秒数,请尝试:

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

不要忘记-u强迫日期程序忽略时区的开关。


终于可以测试了,谢谢!那就是为什么当我回答自己时,我喜欢不接受自己的回答,所以以后可能会出现更好的事情:)
Aquarius Power

1
对于grep -ioP "(true)|(false)",这也适用:grep -oE "true|false"
wjandrea

5

屏幕实际上是被Unity锁定的,我们需要使用 gdbus

gdbus monitor -e -d com.canonical.Unity -o /com/canonical/Unity/Session

这将在锁定时显示,例如:

/com/canonical/Unity/Session: com.canonical.Unity.Session.LockRequested ()
/com/canonical/Unity/Session: com.canonical.Unity.Session.Locked ()
/com/canonical/Unity/Session: com.canonical.Unity.Session.UnlockRequested ()
/com/canonical/Unity/Session: com.canonical.Unity.Session.Unlocked ()

我不是很熟悉,但是如何在没有监视的情况下执行此操作?就像只是查询?
Noitidart

1
我发现的唯一选择是对其进行监视(也许有一种方法可以查询,但是尽管尝试了却找不到),我想这是因为锁定屏幕的那一刻没有存储在任何地方,所以监视器会是显示何时发生的唯一方法。
Aquarius Power

我的意思是,也可以尝试,如果找到,告诉我或添加答案:D
Aquarius Power

啊哈哈,好吧,请尝试用Windows操作系统的人在Linux上找到它不是很好:P我只是在编码一些跨操作系统的东西,所以在互联网上搜索。
Noitidart


2

我在这里有一个类似的问题

我得到的帮助与Aquarius Power之前说过的相似,除了它包含在可以在后台运行的bash scrip守护程序中。.我发现它非常有用。因此,请查看我的问题和答案,看看这是否对您也有帮助。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.