禁用不插电显示器 (xrandr)

19 linux multiple-monitors xorg xrandr

我有一台带两个视频输出的笔记本电脑,我在家时使用(HDMI1、VGA1)。为了启用它们,我这样做:

xrandr --output HDMI1 --right-of LVDS1 --auto
xrandr --output LVDS1 --off
xrandr --output VGA1 --right-of HDMI1 --auto
Run Code Online (Sandbox Code Playgroud)

当我想上班时,我带着笔记本电脑,但首先运行以下命令:

xrandr --output VGA1 --off
xrandr --output LVDS1 --left-of HDMI1 --auto
xrandr --output HDMI1 --off
Run Code Online (Sandbox Code Playgroud)

然后这让我的笔记本电脑显示器像它应该的那样处于活动状态。

我遇到的问题是有时我不记得在我的电脑上班之前禁用两个屏幕。当我到达时,我尝试了--output和 的各种组合,--off但我无法重新启用我的屏幕。

这是我xrandr在没有显示任何内容的情况下运行的输出:

Screen 0: minimum 320 x 200, current 3840 x 1080, maximum 8192 x 8192
LVDS1 connected (normal left inverted right x axis y axis)
   1366x768       60.0 +
   1024x768       60.0··
   800x600        60.3     56.2··
   640x480        59.9··
VGA1 disconnected 1920x1080+1920+0 (normal left inverted right x axis y axis) 0mm x 0mm
HDMI1 disconnected 1920x1080+0+0 (normal left inverted right x axis y axis) 0mm x 0mm
DP1 disconnected (normal left inverted right x axis y axis)
  1920x1080 (0x4c)  148.5MHz
        h: width  1920 start 2008 end 2052 total 2200 skew    0 clock   67.5KHz
        v: height 1080 start 1084 end 1089 total 1125           clock   60.0Hz
Run Code Online (Sandbox Code Playgroud)

我尝试过的几乎每个命令都返回:

xrandr: Configure crtc 2 failed
X Error of failed request:  BadMatch (invalid parameter attributes)
  Major opcode of failed request:  140 (RANDR)
  Minor opcode of failed request:  21 (RRSetCrtcConfig)
  Serial number of failed request:  40
  Current serial number in output stream:  40
Run Code Online (Sandbox Code Playgroud)

就像两个显示器没有放弃它们的 CRTC 一样,因为我的硬件只支持 2 个,所以它被锁定,直到我插入这些显示器并禁用它们。

小智 13

您可以将所有配置放在一个命令中,例如:

xrandr --output VGA1 --off --output HDMI1 --off --output LVDS1 --left-of HDMI1 --auto
Run Code Online (Sandbox Code Playgroud)

这应该可以完成工作,因为很难编写命令(太长),您可以创建一个脚本来测试当前连接的屏幕并进行所需的设置。(你可以添加一个快捷键)

if [ -z `xrandr --query | grep "HDMI1 connected"` ]
then
    xrandr --output DP2 --off --output DP1 --off --output HDMI2 --off \
        --output HDMI1 --off \
        --output LVDS1 --mode 1366x768 --pos 0x0 --rotate normal \
        --output VGA1 --off
else
    xrandr --output DP2 --off --output DP1 --off --output HDMI2 --off \
        --output HDMI1 --mode 1920x1080 --pos 0x0 --rotate normal --primary \
        --output LVDS1 --off --output VGA1 --off
fi
Run Code Online (Sandbox Code Playgroud)

这不是一个花哨的脚本,但可能对你有用。


小智 1

不幸的是,拔掉屏幕时似乎没有生成事件。使用脚本 poll xrandr 相当繁重,但您可以查看 /sys/class/drm/*/status 并轮询这些文件,然后在状态从“已连接”更改为其他状态(或完全消失)时采取所需的 xrandr 操作。