如何设置登录脚本来运行 xrandr?

如何设置登录脚本来运行 xrandr?

我使用 3 个显示器,其中 2 个侧面显示器是旋转的。

我无法成功配置xorg.conf旋转 2 个显示器,但我找到了一个xrandr可以在登录管理器启动后使用的命令:

xrandr --output DisplayPort-1 --rotate right --left-of DisplayPort-0
xrandr --output DisplayPort-2 --rotate left --right-of DisplayPort-0

目前,我在登录后手动运行这些命令。

我应该把它们放在哪里,以便我的显示器在登录时旋转。另外,我可以在全球范围内进行吗?这样,当我以其他用户身份登录时,监视器就会轮换。或者每个用户登录后都需要一个用户脚本来执行此操作吗?

答案1

我使用的是 2 个屏幕布局,与您的布局有点相似,我的布局是右侧的常规屏幕和左侧的纵向屏幕。通过我的设置,X 在我的 2 个屏幕上完美运行。

这是我针对您自己的情况提出的建议(很难测试,因为我没有相同的屏幕,也没有 3 个屏幕),但这应该足以让您获得有效的 X 设置。

将以下文件放入/etc/X11/xorg.conf.d/

30-screen-dport0.conf
30-screen-dport1.conf
30-screen-dport2.conf

包含以下内容:

30-屏幕-dportcenter.conf

Section "Monitor"
  Identifier   "DisplayPort-0"
  Option       "Primary" "true"
  Option       "PreferredMode"   "3840x2160" # Adapt this if you resolution is not the same
  Option       "Position"        "1200 0" 
EndSection

Section "Screen"
  Identifier   "DPC"
  Device       "nVidia" # here you choose your driver
  Monitor      "DisplayPort-0"
EndSection

30-screen-dportleft.conf

Section "Monitor"
  Identifier   "DisplayPort-1 "
  Option       "LeftOf" "DisplayPort-0"
  Option       "Rotate" "left" 
  Option       "PreferredMode"   "1920x1200"
  Option       "Position"        "0 0"
EndSection

Section "Screen"
  Identifier   "DPL"
  Device       "nVidia"
  Monitor      "DisplayPort-1"
EndSection

30-screen-dportright.conf

Section "Monitor"
  Identifier   "DisplayPort-2"
  Option       "RightOf" "DisplayPort-0"
  Option       "Rotate" "right" 
  Option       "PreferredMode"   "1920x1200"
  Option       "Position"        "5040 0" # 1200 + 3840
EndSection

Section "Screen"
  Identifier   "DPR"
  Device       "nVidia"
  Monitor      "DisplayPort-2"
EndSection

90-服务器布局.conf

Section "ServerLayout"
    Identifier   "Main"
    Screen       0 "DPL"
    Screen       1 "DPC"
    Screen       2 "DPR
EndSection

Xserver 的坐标按以下方式工作

 0                 X
+ -----------------> X-axis 
|0
|
|
|
|
| 
| Y
V Y-axis  

nVidia 标识符是对视频卡的引用,该视频卡定义在名为

20-nvidia.conf

Section "Device"
  Identifier  "nVidia"
  Driver      "nouveau"
  Option      "AccelMethod"  "sna"
  Option      "GLXVBlank"    "true"
  # Need to flag this as only referring to one output on the card
  Screen      0

EndSection

答案2

我使用用户态 systemd 服务(完整路径 = $HOME/.config/systemd/user/set-display.service)来处理使用 XFCE 且不会自动填充屏幕的 QEMU VM 上的类似情况(由于 conky 而缩短):

[Unit]
Description=Set Display Resolution

[Service]
ExecStartPre=/bin/sleep 5
Type=oneshot
ExecStart=/usr/bin/xrandr --output Virtual-0 --mode 1499x996

[Install]
WantedBy=default.target

我使用 sleep 命令来确保 DE 已完全加载;我的 SSD 上的虚拟机总是可以在 5 秒或更短的时间内正常运行,而 HDD 虚拟机有时需要更多时间。只需创建一个运行两个 xrandr 命令的脚本,将其替换为执行开始=(类似ExecStart=/path/to/your/script),然后启用它(systemctl --user enable set-display)。

更新:在 Debian Buster 上作为系统服务进行测试不起作用,即使在登录后手动运行该服务(以 root 和普通用户身份尝试)也是如此。

答案3

您必须将这两行放入登录管理器或窗口管理器启动时执行的文件之一中。哪个文件完全取决于您使用的登录管理器和窗口管理器以及您的系统本地设置。

例如,如果您使用slim登录管理器,您应该将启动命令放入login_cmd配置中指定的文件中。以 Debian 为例:

$ grep '^login_cmd ' /etc/slim.conf
login_cmd           exec /bin/bash -login /etc/X11/Xsession %session

但在 Slackware 上:

$ grep '^login_cmd ' /etc/slim.conf
login_cmd           exec /bin/bash -login ~/.xinitrc %session

在 Debian 上,我认为最好的地方是在 /etc/X11/Xsession.d 中创建一个新文件,其中包含90xrandr以下内容:

xrandr --output DisplayPort-1 --rotate right --left-of DisplayPort-0
xrandr --output DisplayPort-2 --rotate left --right-of DisplayPort-0

您不必使其可执行或添加 shebang,因为它的来源是/etc/X11/Xsession

# use run-parts to source every file in the session directory; we source
# instead of executing so that the variables and functions defined above
# are available to the scripts, and so that they can pass variables to each
# other
SESSIONFILES=$(run-parts --list $SYSSESSIONDIR)
if [ -n "$SESSIONFILES" ]; then
  set +e
  for SESSIONFILE in $SESSIONFILES; do
    . $SESSIONFILE
  done
  set -e
fi

另请注意,login_cmd正在运行用户登录,因此您必须slim在每次启动时使用默认监视器设置。

答案4

从命令行(如果您使用的是 nano,否则替换为 vim)

sudo nano /etc/xdg/autostart/rotate-screen-1.desktop

然后粘贴以下内容并保存:

[Desktop Entry]
Version=1.0
Encoding=UTF-8
Type=Application
Name=Xrandr Screen 1 Rotation
Icon=preferences-desktop-screensaver
Exec=xrandr --output DisplayPort-1 --rotate right --left-of DisplayPort-0
Terminal=false
OnlyShowIn=LXDE;OPENBOX;GNOME;KDE;
Categories=System;Security;Utility;
StartupNotify=false
X-KDE-autostart-after=panel
X-OPENBOX-Autostart-enabled=true

然后第二个屏幕也一样

sudo nano /etc/xdg/autostart/rotate-screen-2.desktop

然后粘贴以下内容并保存:

[Desktop Entry]
Version=1.0
Encoding=UTF-8
Type=Application
Name=Xrandr Screen 2 Rotation
Icon=preferences-desktop-screensaver
Exec=xrandr --output DisplayPort-2 --rotate left --right-of DisplayPort-0
Terminal=false
OnlyShowIn=LXDE;OPENBOX;GNOME;KDE;
Categories=System;Security;Utility;
StartupNotify=false
X-KDE-autostart-after=panel
X-OPENBOX-Autostart-enabled=true

重启后屏幕应自动旋转

相关内容