AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 505681
Accepted
Aquarius Power
Aquarius Power
Asked: 2014-08-02 17:44:37 +0800 CST2014-08-02 17:44:37 +0800 CST 2014-08-02 17:44:37 +0800 CST

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

  • 772

这两个只有在被锁定的屏幕变黑后才有效;但他们有时也会失败,当出于任何原因屏幕不空白时......

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-将监听 dbus 和消息上的脚本的守护进程

unity
  • 3 3 个回答
  • 5530 Views

3 个回答

  • Voted
  1. Best Answer
    b_laoshi
    2017-03-09T00:18:17+08:002017-03-09T00:18:17+08:00

    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强制日期程序忽略您的时区的开关。

    • 7
  2. Aquarius Power
    2014-08-02T22:18:02+08:002014-08-02T22:18:02+08:00

    屏幕实际上被 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 ()
    
    • 5
  3. Sverre
    2014-10-10T23:10:12+08:002014-10-10T23:10:12+08:00

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

    我得到的帮助与 Aquarius Power 之前所说的类似,只是它包含在一个 bash 脚本守护进程中,可以在后台运行。我发现它非常有帮助。所以,看看我的问题和答案,看看这是否对你也有帮助。

    • 2

相关问题

  • 如何将 Web 应用程序放入 Unity Launcher?

  • Ubuntu 上网本 10.10 中没有 Alt+F2?

  • Unity 中的 gnome-do 样式键盘快捷键

  • 在哪里提交 Unity 的错误/愿望清单?

  • Unity 启动器——它可以作为单独的包提供吗?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve