Rum*_*esh 13 xrandr scripts multiple-monitors events udev
usr/local/bin/
当我将外部显示器连接到我的笔记本电脑时,我正在尝试运行位于 中的脚本。我试图添加一条新udev
规则,但没有奏效。我在/etc/udev/rules.d
名为vga-monitor-connect.rules
. 该文件的内容是
SUBSYSTEM=="drm", ACTION=="change", RUN+="/usr/local/bin/panel-fix"
Run Code Online (Sandbox Code Playgroud)
我从这个答案中得到了这条线
在网上搜索后,我也尝试了以下规则
KERNEL=="card0", SUBSYSTEM=="drm", ENV{DISPLAY}=":0", ENV{XAUTHORITY}="/home/rumesh/.Xauthority", RUN+="/usr/local/bin/panel-fix"
Run Code Online (Sandbox Code Playgroud)
然而这也不起作用。
我已经手动运行了脚本,我可以确认它可以工作,所以我的脚本没有问题。
我也想说清楚,我知道的不多,udev
所以我使用的规则可能是错误的。如果有人知道我的问题的正确规则,请留下答案。
我的显卡是Intel GM965集成芯片组
另一种解决方案是运行一个很小的后台脚本。在后台运行下面的脚本,我无法测量处理器负载的任何增加。
无论何时连接或断开第二个屏幕,这是运行脚本或任何其他命令的一种简单方便的方法。
xrandr
(注意“connected”后面的空格以防止与“disconnected”错误匹配)。每个事件代表一个连接的屏幕。#!/usr/bin/env python3
import subprocess
import time
#--- set both commands (connect / disconnect) below
connect_command = "gedit"
disconnect_command = ""
#---
def get(cmd): return subprocess.check_output(cmd).decode("utf-8")
# - to count the occurrenc of " connected "
def count_screens(xr): return xr.count(" connected ")
# - to run the connect / disconnect command(s)
def run_command(cmd): subprocess.Popen(["/bin/bash", "-c", cmd])
# first count
xr1 = None
while True:
time.sleep(5)
# second count
xr2 = count_screens(get(["xrandr"]))
# check if there is a change in the screen state
if xr2 != xr1:
print("change")
if xr2 == 2:
# command to run if connected (two screens)
run_command(connect_command)
elif xr2 == 1:
# command to run if disconnected (one screen)
# uncomment run_command(disconnect_command) to enable, then also comment out pass
pass
# run_command(disconnect_command)
# set the second count as initial state for the next loop
xr1 = xr2
Run Code Online (Sandbox Code Playgroud)
connect_screen.py
在 head 部分,将命令设置为在连接上运行(我以“gedit”为例,注意引号)。同样,也可以在断开连接时设置命令。否则就这样离开disconnect_command = ""
。
如果您确实使用了 disconnect- 命令,也请取消注释该行:
run_command(disconnect_command)
Run Code Online (Sandbox Code Playgroud)
并注释掉这一行:
pass
Run Code Online (Sandbox Code Playgroud)
如脚本中所示
如果一切正常,请将其添加到您的启动应用程序中:Dash > Startup Applications > 添加命令:
/bin/bash -c "sleep 15&&python3 /path/to/connect_screen.py"
Run Code Online (Sandbox Code Playgroud)
这sleep 15
是在脚本开始运行之前使桌面完全启动。只想确认一下。
休息时间sleep 15
一般应该有效,但由于每个系统的启动时间不同,可能需要进行一些试验才能找到正确的休息时间。通过少量添加,脚本变得“智能”,并xrandr
在启动实际脚本之前等待命令成功。如果你使用下面的版本,你只需要添加命令:
python3 /path/to/connect_screen.py
Run Code Online (Sandbox Code Playgroud)
到您的启动应用程序。进一步的用法和上面的版本完全一样。
run_command(disconnect_command)
Run Code Online (Sandbox Code Playgroud)