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

비슷한 질문이 있었어요여기

그리고 제가 받은 도움은 백그라운드에서 실행될 수 있는 bash 스크립 데몬에 포함되어 있다는 점을 제외하면 이전에 Aquarius Power가 말한 것과 비슷했습니다. 매우 도움이 되었습니다. 그러니 내 질문과 답변을 살펴보고 이것이 도움이 되는지 확인해 보세요.

관련 정보