unity - 如何偵測螢幕是否鎖定?

unity - 如何偵測螢幕是否鎖定?

這兩種方法僅在鎖定的螢幕變為空白後才起作用;但有時也會失敗,當由於某種原因畫面不空白時...

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 https://unix.stackexchange.com/questions/80143/how-to-create-a-daemon-which-would-be-listening-to-dbus-and-fire-script-on-messa

答案1

水瓶座力量的答案似乎效果很好。以下是我可能對他的解決方案進行的一些補充。

只查詢鎖定狀態

如果您只需要一行程式碼來查詢鎖定狀態,則如果已鎖定,則應評估為 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強制日期程序忽略您的時區的開關。

答案2

螢幕實際上是被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 ()

答案3

我有一個類似的問題這裡

我得到的幫助與 Aquarius Power 之前所說的類似,除了它包含在 bash scrip 守護進程中,可以在後台運行..我發現它非常有幫助。所以,看看我的問題並回答,看看這是否也對您有幫助。

相關內容