我需要在以下情况下运行自定义命令:
- 计算机空闲 X 分钟(未按下任何键)
- 按下按键
我实际上想要实现的是键盘的省电功能。
我想要使用:
g413-led -a 000000
g413-led -a FFFFFF
使用以下方式打开/关闭键盘背光https://github.com/MatMoul/g810-led。
第一个相当简单,使用以下之一:
- 希德勒
- 打印空闲
- 自动锁
请指教是否有更好、更优化的方法?
但是,按下按键时再次点亮它或从空闲状态恢复怎么办?我想每次按下按键时运行命令有点过头了。是否有省电选项事件?显示器省电如何工作?
我可以重复使用相同的事件/超时吗?
答案1
我想到解决方案是DBUS 事件。
我首先运行dbus-monitor
以查看当监视器进入睡眠状态或从睡眠状态唤醒时将触发哪些事件:
signal time=1515274499.244468 sender=org.freedesktop.DBus -> destination=:1.394 serial=2 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=NameAcquired
string ":1.394"
signal time=1515274499.244502 sender=org.freedesktop.DBus -> destination=:1.394 serial=4 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=NameLost
string ":1.394"
signal time=1515274559.337480 sender=:1.6 -> destination=:1.2 serial=29015 path=/org/gnome/Mutter/IdleMonitor/Core; interface=org.gnome.Mutter.IdleMonitor; member=WatchFired
uint32 440
signal time=1515274559.337878 sender=:1.2 -> destination=(null destination) serial=3288 path=/org/gnome/SessionManager/Presence; interface=org.gnome.SessionManager.Presence; member=StatusChanged
uint32 3
signal time=1515274559.338267 sender=:1.2 -> destination=(null destination) serial=3290 path=/org/gnome/SessionManager/Presence; interface=org.freedesktop.DBus.Properties; member=PropertiesChanged
string "org.gnome.SessionManager.Presence"
array [
dict entry(
string "status"
variant uint32 3
)
]
array [
]
signal time=1515274569.389778 sender=:1.6 -> destination=(null destination) serial=29018 path=/org/gnome/Shell; interface=org.gnome.Shell.Extensions; member=ExtensionStatusChanged
string "[email protected]"
int32 2
string ""
signal time=1515274569.398929 sender=:1.6 -> destination=(null destination) serial=29019 path=/org/gnome/Shell; interface=org.gnome.Shell.Extensions; member=ExtensionStatusChanged
string "[email protected]"
int32 2
string ""
signal time=1515274569.399633 sender=org.freedesktop.DBus -> destination=:1.6 serial=5195 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=NameLost
string "org.kde.StatusNotifierWatcher"
signal time=1515274569.399660 sender=org.freedesktop.DBus -> destination=(null destination) serial=850 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=NameOwnerChanged
string "org.kde.StatusNotifierWatcher"
string ":1.6"
string ""
signal time=1515274569.404274 sender=:1.6 -> destination=(null destination) serial=29022 path=/org/gnome/Shell; interface=org.gnome.Shell.Extensions; member=ExtensionStatusChanged
string "[email protected]"
int32 2
string ""
signal time=1515274569.442137 sender=:1.6 -> destination=(null destination) serial=29025 path=/org/gnome/Shell; interface=org.gnome.Shell.Extensions; member=ExtensionStatusChanged
string "[email protected]"
int32 2
string ""
signal time=1515274569.455035 sender=:1.6 -> destination=(null destination) serial=29027 path=/org/gnome/ScreenSaver; interface=org.gnome.ScreenSaver; member=ActiveChanged
boolean true
signal time=1515274569.657300 sender=:1.6 -> destination=:1.42 serial=29037 path=/org/gnome/Mutter/IdleMonitor/Core; interface=org.gnome.Mutter.IdleMonitor; member=WatchFired
uint32 491
signal time=1515274569.657404 sender=:1.6 -> destination=(null destination) serial=29038 path=/org/gnome/Mutter/DisplayConfig; interface=org.freedesktop.DBus.Properties; member=PropertiesChanged
string "org.gnome.Mutter.DisplayConfig"
array [
dict entry(
string "PowerSaveMode"
variant int32 3
)
]
array [
]
signal time=1515276608.243100 sender=:1.6 -> destination=:1.2 serial=29039 path=/org/gnome/Mutter/IdleMonitor/Core; interface=org.gnome.Mutter.IdleMonitor; member=WatchFired
uint32 489
signal time=1515276608.243573 sender=:1.2 -> destination=(null destination) serial=3291 path=/org/gnome/SessionManager/Presence; interface=org.gnome.SessionManager.Presence; member=StatusChanged
uint32 0
signal time=1515276608.243843 sender=:1.2 -> destination=(null destination) serial=3292 path=/org/gnome/SessionManager/Presence; interface=org.freedesktop.DBus.Properties; member=PropertiesChanged
string "org.gnome.SessionManager.Presence"
array [
dict entry(
string "status"
variant uint32 0
)
]
array [
]
signal time=1515276608.244032 sender=:1.6 -> destination=:1.42 serial=29040 path=/org/gnome/Mutter/IdleMonitor/Core; interface=org.gnome.Mutter.IdleMonitor; member=WatchFired
uint32 494
signal time=1515276608.270459 sender=:1.6 -> destination=(null destination) serial=29042 path=/org/gnome/Mutter/DisplayConfig; interface=org.freedesktop.DBus.Properties; member=PropertiesChanged
string "org.gnome.Mutter.DisplayConfig"
array [
dict entry(
string "PowerSaveMode"
variant int32 0
)
]
array [
]
signal time=1515276623.523917 sender=:1.6 -> destination=:1.42 serial=29043 path=/org/gnome/Mutter/IdleMonitor/Core; interface=org.gnome.Mutter.IdleMonitor; member=WatchFired
uint32 491
signal time=1515276623.554451 sender=:1.6 -> destination=(null destination) serial=29047 path=/org/gnome/Mutter/DisplayConfig; interface=org.freedesktop.DBus.Properties; member=PropertiesChanged
string "org.gnome.Mutter.DisplayConfig"
array [
dict entry(
string "PowerSaveMode"
variant int32 3
)
]
array [
]
signal time=1515276668.529836 sender=:1.6 -> destination=:1.2 serial=29048 path=/org/gnome/Mutter/IdleMonitor/Core; interface=org.gnome.Mutter.IdleMonitor; member=WatchFired
uint32 440
signal time=1515276668.530236 sender=:1.2 -> destination=(null destination) serial=3293 path=/org/gnome/SessionManager/Presence; interface=org.gnome.SessionManager.Presence; member=StatusChanged
uint32 3
signal time=1515276668.530565 sender=:1.2 -> destination=(null destination) serial=3295 path=/org/gnome/SessionManager/Presence; interface=org.freedesktop.DBus.Properties; member=PropertiesChanged
string "org.gnome.SessionManager.Presence"
array [
dict entry(
string "status"
variant uint32 3
)
]
array [
]
signal time=1515279067.394398 sender=:1.6 -> destination=:1.2 serial=29051 path=/org/gnome/Mutter/IdleMonitor/Core; interface=org.gnome.Mutter.IdleMonitor; member=WatchFired
uint32 498
signal time=1515279067.394982 sender=:1.2 -> destination=(null destination) serial=3296 path=/org/gnome/SessionManager/Presence; interface=org.gnome.SessionManager.Presence; member=StatusChanged
uint32 0
signal time=1515279067.395139 sender=:1.2 -> destination=(null destination) serial=3297 path=/org/gnome/SessionManager/Presence; interface=org.freedesktop.DBus.Properties; member=PropertiesChanged
string "org.gnome.SessionManager.Presence"
array [
dict entry(
string "status"
variant uint32 0
)
]
array [
]
signal time=1515279067.395662 sender=:1.6 -> destination=:1.42 serial=29052 path=/org/gnome/Mutter/IdleMonitor/Core; interface=org.gnome.Mutter.IdleMonitor; member=WatchFired
uint32 496
signal time=1515279067.414904 sender=:1.6 -> destination=(null destination) serial=29054 path=/org/gnome/Mutter/DisplayConfig; interface=org.freedesktop.DBus.Properties; member=PropertiesChanged
string "org.gnome.Mutter.DisplayConfig"
array [
dict entry(
string "PowerSaveMode"
variant int32 0
)
]
array [
]
signal time=1515279076.828255 sender=:1.6 -> destination=(null destination) serial=29063 path=/org/gnome/Shell; interface=org.gnome.Shell.Extensions; member=ExtensionStatusChanged
string "[email protected]"
int32 1
string ""
signal time=1515279076.829775 sender=org.freedesktop.DBus -> destination=(null destination) serial=851 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=NameOwnerChanged
string "org.kde.StatusNotifierWatcher"
string ""
string ":1.6"
signal time=1515279076.829812 sender=org.freedesktop.DBus -> destination=:1.6 serial=5215 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=NameAcquired
string "org.kde.StatusNotifierWatcher"
signal time=1515279076.829824 sender=:1.6 -> destination=(null destination) serial=29066 path=/org/gnome/Shell; interface=org.gnome.Shell.Extensions; member=ExtensionStatusChanged
string "[email protected]"
int32 1
string ""
signal time=1515279076.841923 sender=:1.6 -> destination=(null destination) serial=29067 path=/org/gnome/Shell; interface=org.gnome.Shell.Extensions; member=ExtensionStatusChanged
string "[email protected]"
int32 1
string ""
signal time=1515279076.844396 sender=:1.6 -> destination=(null destination) serial=29068 path=/org/gnome/Shell; interface=org.gnome.Shell.Extensions; member=ExtensionStatusChanged
string "[email protected]"
int32 1
string ""
signal time=1515279076.851323 sender=:1.6 -> destination=(null destination) serial=29069 path=/org/gnome/ScreenSaver; interface=org.gnome.ScreenSaver; member=ActiveChanged
boolean false
signal time=1515279076.896958 sender=:1.6 -> destination=(null destination) serial=29218 path=/StatusNotifierWatcher; interface=org.kde.StatusNotifierWatcher; member=StatusNotifierItemRegistered
string "/"
signal time=1515279076.921110 sender=:1.38 -> destination=(null destination) serial=2735 path=/org/gnome/evolution/dataserver/CalendarView/1887/93; interface=org.gnome.evolution.dataserver.CalendarView; member=complete
array [
string ""
string ""
]
signal time=1515279076.924727 sender=:1.34 -> destination=(null destination) serial=2701 path=/org/gnome/evolution/dataserver/CalendarView/1870/92; interface=org.gnome.evolution.dataserver.CalendarView; member=complete
array [
string ""
string ""
]
signal time=1515279077.686970 sender=:1.6 -> destination=(null destination) serial=30014 path=/StatusNotifierWatcher; interface=org.freedesktop.DBus.Properties; member=PropertiesChanged
string "org.kde.StatusNotifierWatcher"
array [
dict entry(
string "RegisteredStatusNotifierItems"
variant array [
string ":1.66/org/ayatana/NotificationItem/software_update_available"
]
)
]
array [
我首先尝试去实现org.gnome.Mutter.IdleMonitor
,但是uint32
值却不断增加,不知道为什么?
我已使用 StatusChanged 进行解决,以下是最终脚本:
#!/bin/bash
# Turn keyboard backlight on/off using https://github.com/MatMoul/g810-led
g413-led -a FFFFFF # always turn on on startup (it turns off for some reason on reboot)
dbus-monitor --session "type=signal,interface=org.gnome.SessionManager.Presence,member=StatusChanged" |
while read x; do
case "$x" in
*"uint32 3"*) g413-led -a 000000;;
*"uint32 0"*) g413-led -a FFFFFF;;
esac
done
屏幕保护程序事件也可以起作用:
g413-led -a FFFFFF # always turn on on startup (it turns off for some reason on reboot)
dbus-monitor --session "type='signal',interface='org.gnome.ScreenSaver'" |
while read x; do
case "$x" in
*"boolean true"*) g413-led -a 000000;;
*"boolean false"*) g413-led -a FFFFFF;;
esac
done
当然,你可以输入任何你想要的命令。与这个脚本相比,第一个脚本的优点是它在用户登录前会点亮键盘,这样可以更轻松地输入密码。
唯一的缺点是键盘在用户登录后会亮起,而不是在按下按键时亮起,但我可以忍受。
我正在尝试:
dbus-monitor --session "type='signal',interface='org.freedesktop.DBus.Properties',path='/org/gnome/Mutter/DisplayConfig'" | grep 'variant int32' |
while read x; do
case "$x" in
*"variant int32 3"*) g413-led -a 000000;;
*"variant int32 0"*) g413-led -a FFFFFF;;
esac
done
但由于某种原因它没有起作用,可能是 grep 问题。
答案2
微小的后台进程
对于一个简单的问题,您的设置似乎相当复杂。我会做的只是运行一个微小的后台进程,每 2 秒左右检查一次空闲时间,如果空闲时间超过限制,则采取一种操作,如果切换到低于设置的空闲时间,则采取另一种操作。
剧本:
#!/usr/bin/env python3
import time
import subprocess
# set idle time (seconds)
t = 60
# set commands
on_idle = ["g413", "-led", "-a", "000000"]
on_active = ["g413", "-led", "-a", "FFFFFF"]
def set_state(cmd):
subprocess.Popen(cmd)
def get_idle():
return int(subprocess.check_output("xprintidle").decode("utf-8").strip())/1000
idle1 = 0
while True:
time.sleep(2)
idle2 = get_idle()
# if idle time exceeds (passes) the limit, run one command
if all([idle2 >= t, idle1 < t]):
set_state(on_idle)
# if idle time switches to below (passes) the limit, run another command
elif all([idle2 <= t, idle1 > t]):
set_state(on_active)
idle1 = idle2
使用
确保
xprintidle
已安装sudo apt install xprintidle
将脚本复制到一个空文件中,另存为
switch_light.py
设置所需的空闲时间以在以下时间后采取行动:
# set idle time (seconds) t = 60
通过命令运行:
python3 /path/to/switch_light.py
如果一切正常,请将其添加到启动应用程序。
笔记)
由于我没有你的键盘,所以无法测试它,但用其他命令测试后,它可以很好地完成工作。
您可以将脚本与任何其他命令一起使用:
# set commands on_idle = ["g413", "-led", "-a", "000000"] on_active = ["g413", "-led", "-a", "FFFFFF"]
只要确保像示例中那样分离参数即可。
如果你只想要超过空闲时间(或相反),只需注释掉:
# if idle time switches to below (passes) the limit, run another elif all([idle2 <= t, idle1 > t]): set_state(on_active)