我需要在用户不活动一段时间后运行脚本。为此,我使用了很棒的程序xautolock
。
现在,我还需要当活动恢复时运行另一个脚本(例如移动鼠标或按下某个键)。
我怎么做?任何类型的反 xautolock 吗? xautoUNlock?
或者,也许,任何其他简单的如何抓住“闲散”停止的那一刻?
答案1
我编写了以下脚本来检查空闲状态并相应地使用 systemd-logind 更新会话空闲状态。 (通常这将由会话管理器完成,但我不运行会话管理器。)这让 systemd 自动将系统置于睡眠状态(基于和IdleAction
),在确定系统是否处于运行状态时同时考虑 X11 和 ssh 登录。闲置的。部分基于IdleActionSec
/etc/systemd/logind.conf
这和这。
您可以修改 login1_idle 类来运行您想要的任何脚本。
#!/usr/bin/python
import ctypes
import os
import dbus
import time
# Timing parameters, in s
idle_threshold = 60 # Amount of idle time required to consider things truly idle
check_interval = 50 # How often to check the X server idle indication
class login1_idle:
def __init__(self):
self.flag = 0
bus = dbus.SystemBus()
seat = bus.get_object('org.freedesktop.login1',
'/org/freedesktop/login1/seat/'
+ os.environ['XDG_SEAT'])
active_session = seat.Get('org.freedesktop.login1.Seat',
'ActiveSession',
dbus_interface='org.freedesktop.DBus.Properties')
session_obj_path = active_session[1]
self.session = bus.get_object('org.freedesktop.login1', session_obj_path);
def on_busy(self):
self.flag = 0
self.session.SetIdleHint(False,
dbus_interface='org.freedesktop.login1.Session')
def on_idle(self):
self.flag = 1
self.session.SetIdleHint(True,
dbus_interface='org.freedesktop.login1.Session')
class XScreenSaverInfo(ctypes.Structure):
""" typedef struct { ... } XScreenSaverInfo; """
_fields_ = [('window', ctypes.c_ulong), # screen saver window
('state', ctypes.c_int), # off,on,disabled
('kind', ctypes.c_int), # blanked,internal,external
('since', ctypes.c_ulong), # milliseconds
('idle', ctypes.c_ulong), # milliseconds
('event_mask', ctypes.c_ulong)] # events
xlib = ctypes.cdll.LoadLibrary('libX11.so')
display = xlib.XOpenDisplay(os.environ['DISPLAY'])
xss = ctypes.cdll.LoadLibrary('libXss.so.1')
xss.XScreenSaverAllocInfo.restype = ctypes.POINTER(XScreenSaverInfo)
xssinfo = xss.XScreenSaverAllocInfo()
idle = login1_idle()
while True:
xss.XScreenSaverQueryInfo(display, xlib.XDefaultRootWindow(display), xssinfo)
#print "idle: %d ms" % xssinfo.contents.idle
if xssinfo.contents.idle > idle_threshold * 1000 and not idle.flag:
print time.strftime('%c') + " doing on_idle"
idle.on_idle()
elif xssinfo.contents.idle < idle_threshold * 1000 and idle.flag:
print time.strftime('%c') + " doing on_busy"
idle.on_busy()
#print idle.session.Get('org.freedesktop.login1.Session',
# 'IdleHint',
# dbus_interface='org.freedesktop.DBus.Properties')
time.sleep(check_interval)
C/bash 实现的另一个类似问题是系统空闲时和再次活动时运行命令。