Multihead

出自 ArchWiki

多头显示多屏幕多显示器多监视器 代表将多个显示设备连接到单台计算机的设置。本文概述了几种多头显示设置方法,并提供了一些配置示例。

注意: 本文使用的术语非常具体,以避免混淆
  • Monitor 指的是物理显示设备,例如 LCD 面板。
  • Screen 指的是 X 窗口屏幕(即:连接到 displaymonitor)。
  • Display 指的是同时使用的 screens 集合,这些屏幕显示单个桌面的一部分(您可以在单个 display 中的所有 screens 之间拖动窗口)。

历史背景

X 窗口系统 (X, X11) 是大多数提供图形用户界面 (GUI) 的 Unix/Linux 计算机的底层图形用户界面。它于 1984 年在 MIT 开发。经过大约 35 年的开发、调整、新功能和想法,它通常被认为是有点臃肿。在早期开发期间,常见的配置是单个运行的 X,它在分时系统上为 X 终端提供单独的视图。如今,X 通常在台式机或笔记本电脑上提供单个屏幕。

注意: 仍然存在一种罕见的配置,通常称为Zaphod 显示,它允许多个用户使用单台计算机,每个人都拥有一组独立的显示器、鼠标和键盘,就像他们在使用单独的计算机一样,但每个座位的成本更低。

简而言之,使用 X 配置 GUI 的方法有很多。当使用现代版本时,有时您甚至可以在有限或无需配置的情况下完成。在过去几年中,有人吹嘘 X 是自配置的。当然,一个好的经验法则是配置越少越好 - 也就是说,只配置默认设置错误的内容

RandR

RandRRotate and Resize,旋转和调整大小)是 X 窗口系统 的扩展,它允许客户端动态更改(例如,调整大小、旋转、反射)屏幕。在大多数情况下,它可以完全取代旧的 Xinerama 设置。请参阅此解释,了解为什么 RandR 比 Xinerama 更好。

RandR 可以通过 xrandr 工具、arandr 为当前会话配置,或通过 xorg.conf 文件持久配置。

注意: 配置同一件事有多种方法,您可能需要稍微尝试一下才能找到最佳配置。

使用 xrandr 进行配置

注意: 本节假定您已阅读 xrandr 页面,了解有关 xrandr 的基本信息。

您可以相对于彼此(使用 --right-of--left-of--above--below 选项)或通过绝对坐标(使用 --pos 选项;请注意,在这种情况下,您通常需要知道显示器的分辨率)排列屏幕。有关详细信息,请参阅 xrandr(1)。下面描述了一些常用设置。

VGA1 在 HDMI1 左侧,使用首选分辨率

$ xrandr --output VGA1 --auto --output HDMI1 --auto --right-of VGA1

--right-of 将上一个屏幕 (HDMI1) 放置在指定屏幕 (VGA1) 的右侧。

VGA1 在 HDMI1 右侧,使用固定分辨率

$ xrandr --output VGA1 --mode 1024x768 --pos 1920x0 --output HDMI1 --mode 1920x1080 --pos 0x0

$ xrandr --output VGA1 --mode 1024x768 --output HDMI1 --mode 1920x1080 --left-of VGA1

--left-of 将上一个屏幕 (HDMI1) 放置在指定屏幕 (VGA1) 的左侧。

将屏幕合并为虚拟显示器

自 randr 1.5 版本以来,可以将显示器合并为一个虚拟显示器。这是 Xinerama 功能的更新版本,适用于开源驱动程序,并且不需要重启 Xorg。某些桌面环境尚不支持此功能。Openbox 已经过测试,可以与此功能一起使用。

通过执行 xrandr --listmonitors 获取显示器列表

0: +*DisplayPort-4 1920/518x1200/324+1920+0  DisplayPort-4
1: +DisplayPort-3 1920/518x1200/324+0+0  DisplayPort-3
2: +HDMI-A-0 1920/518x1200/324+3840+0  HDMI-A-0

创建虚拟显示器 xrandr --setmonitor SomeName auto DisplayPort-4,DisplayPort-3,HDMI-A-0auto 确定虚拟显示器的大小,将其设置为 auto 将自动创建正确的显示器阵列大小。此命令中的显示器顺序无关紧要,显示器需要在执行此命令之后或之前正确重新排列。

有关更详细的说明,请参阅此页面

使用 xorg.conf 进行配置

这与使用 xrandr 类似,每个屏幕都需要单独的 Monitor 部分。作为 Identifier,使用与 xrandr -q 报告的值相同的值(即,使用 Identifier "VGA1" 而不是 --output VGA1)。

示例:使用相对坐标的双显示器配置

/etc/X11/xorg.conf.d/10-monitor.conf
Section "Monitor"
    Identifier  "VGA1"
    Option      "Primary" "true"
EndSection

Section "Monitor"
    Identifier  "HDMI1"
    Option      "LeftOf" "VGA1"
EndSection

示例:使用相对坐标和自定义分辨率的双显示器配置

可以通过运行 $ xrandr -q 命令找到每个显示器的 ID,并且应在 Device 部分内将其定义为 Monitor-<ID>

请参阅Xrandr#添加未检测到的分辨率

/etc/X11/xorg.conf.d/10-monitor.conf
Section "Monitor"
  Identifier "DVI"
  Modeline "1680x1050_60.00"  146.25  1680 1784 1960 2240  1050 1053 1059 1089 -hsync +vsync
  Option "PreferredMode" "1680x1050_60.00"
  Option "LeftOf" "DP"
  Option "DPMS" "true"
EndSection

Section "Monitor"
  Identifier "DP"
  Modeline "1920x1080_60.00"  173.00  1920 2048 2248 2576  1080 1083 1088 1120 -hsync +vsync
  Option "PreferredMode" "1920x1080_60.00"
  Option "RightOf" "DVI"
  Option "DPMS" "true"
EndSection

Section "Screen"
  Identifier "Screen0"
  Device "Radeon" # e.g. Radeon, Intel, nvidia
  Monitor "DP"
  DefaultDepth 24
  SubSection "Display"
    Depth 24
    Virtual 3600 2130 # 1920 + 1680 (3600), 1080 + 1050 (2130)
  EndSubSection
EndSection
/etc/X11/xorg.conf.d/20-radeon.conf
Section "Device"
  Identifier "Radeon"
  Driver "radeon"
  Option "Monitor-DVI-0" "DVI" # use DVI-0 as DVI
  Option "Monitor-DisplayPort-0" "DP"
EndSection

示例:使用绝对坐标的双显示器配置

/etc/X11/xorg.conf.d/10-monitor.conf
Section "Monitor"
    Identifier  "VGA1"
    Option      "PreferredMode" "1024x768"
    Option      "Position" "1920 312"
EndSection

Section "Monitor"
    Identifier  "HDMI1"
    Option      "PreferredMode" "1920x1080"
    Option      "Position" "0 0"
EndSection

没有负坐标,设置中最左侧和最高可能的目标点为 0,0

(0,0)-----------------+ 
|                     |(1920,312)---+
|     1920 x 1080     ||            |
|        HDMI1        || 1024 x 768 |
|                     ||    VGA1    |
+---------------------++------------+

动态显示配置

以下选项允许您自动检测何时连接了新显示器,然后根据该显示器更改布局。这对于经常在需要不同设置的多种不同环境中工作的笔记本电脑用户非常有用。

  • autorandr 允许您根据连接的显示硬件将当前的 xrandr 配置保存为配置文件。
  • monsAURmons-gitAUR 是一个 shell 脚本,用于使用 xrandr 快速管理双显示器显示。

独立屏幕

这是使用 X 配置多显示器的原始方法,它已经存在了几十年。每个物理显示器都分配为一个 X 屏幕,虽然您可以在它们之间移动鼠标,但它们或多或少是独立的。

通常,X 显示器具有单个标识符,例如 :0,在 DISPLAY 环境变量中设置;但在这种配置中,每个屏幕都有不同的 $DISPLAY 值。第一个屏幕是 :0.0,第二个是 :0.1,依此类推。

使用此配置,除了少数特殊程序(如 GIMP 和 Emacs,它们具有多屏幕支持)之外,无法在屏幕之间移动窗口。对于大多数程序,您必须在启动时更改 DISPLAY 环境变量,才能使程序出现在另一个屏幕上

# Launch a terminal on the second screen
$ DISPLAY=:0.1 urxvt &

或者,如果您在每个屏幕上都有一个终端,则启动程序将继承 DISPLAY 值,并出现在启动它们的同一屏幕上。但是,在屏幕之间移动应用程序涉及到关闭它,然后在另一个屏幕上重新打开它。

以这种方式工作确实具有某些优势。例如,如果在一个屏幕上弹出窗口,如果您正在另一个屏幕上工作,它们不会从您那里窃取焦点 - 每个屏幕都非常独立。

TwinView

TwinView 是 NVIDIA 的扩展,它使连接到显卡的两个显示器显示为单个屏幕。TwinView 提供 Xinerama 扩展,以便应用程序知道连接了两个显示器,因此它与 Xinerama 不兼容。但是,如果您只有两个显示器,并且它们都连接到同一张 NVIDIA 显卡,则 TwinView 和 Xinerama 之间几乎没有区别(尽管在这种情况下,TwinView 可能会提供稍微更好的性能。)

如果您希望连接两个以上的显示器或连接到其他显卡的显示器,则需要使用 Xinerama 而不是 TwinView。同样,截至 2012 年 4 月,两个显示器必须处于相同的方向 - 您不能一个处于横向模式,另一个处于纵向模式。

过去,TwinView 是在使用 NVIDIA 显卡时获得 OpenGL 加速的唯一方法,同时能够拖动屏幕之间的窗口。但是,即使在使用 Xinerama 时,现代版本的 NVIDIA 闭源驱动程序也能够提供 OpenGL 加速。

有关配置示例,请参阅 NVIDIA#TwinView

Xinerama

Xinerama 是实现真正的多头 X 的旧方法。Xinerama 将所有显示器组合成一个屏幕 (:0),从而可以在屏幕之间拖动窗口。

Xinerama 通过自定义 X 配置文件进行配置。还有一个名为 WideGuy 的 GUI 工具,可以更轻松地切换 Xinerama。请注意,要使用 WideGuy,您仍然需要具有 ServerLayout 部分的 Xorg 配置。

以下是一些 X 配置示例

这是一个 ServerLayout 部分,它控制每个显示器相对于其他显示器的位置。

/etc/X11/xorg.conf.d/90-serverlayout.conf
Section "ServerLayout"
  Identifier   "Main"
  Screen       0 "Primary"
  Screen       1 "DellPortraitLeft" RightOf "Primary"
  Screen       2 "Wacom" RightOf "DellPortraitLeft"
  Screen       3 "U2412" LeftOf "Primary"
  Option         "Xinerama" "1"  # enable XINERAMA extension.  Default is disabled.
EndSection

上面部分中的每个 Screen 都在单独的文件中定义,例如这个文件

/etc/X11/xorg.conf.d/30-screen-dell2001.conf
# Define the monitor's physical specs
Section "Monitor"
  Identifier   "Dell 2001FP"
  VertRefresh  60
  Option  "dpms"  "on"

  # Modelines are probably unnecessary these days, but it does give you fine-grained control

  # 1600x1200 @ 60.00 Hz (GTF) hsync: 74.52 kHz; pclk: 160.96 MHz
  Modeline "1600x1200"  160.96  1600 1704 1880 2160  1200 1201 1204 1242  -HSync +Vsync
EndSection

# Define a screen that uses the above monitor.  Note the Monitor value matches the above
# Identifier value and the Device value matches one of the video cards defined below
# (the card and connector this monitor is actually plugged in to.)
Section "Screen"
  Identifier   "DellPortraitLeft"
  Device       "GeForce 8600GTb"
  Monitor      "Dell 2001FP"
  DefaultDepth 24
  SubSection "Display"
    Depth     24
    Modes     "1600x1200"
    ViewPort  0 0
    Virtual   1600 1200
  EndSubsection

  # This screen is in portrait mode
  Option "Rotate" "left"
EndSection

您需要为每个 monitor 创建一个 Device 部分,即双头显卡将有两个 Device 部分。以下示例显示如何配置两个显卡,每个显卡提供两个输出,总共四个显示器。

/etc/X11/xorg.conf.d/20-nvidia.conf
# First head of first video card in the system
Section "Device"
  Identifier  "GeForce 8600GT"
  Driver      "nvidia"

  # If you have multiple video cards, the BusID controls which one this definition refers
  # to.  You can omit it if you only have one card.
  BusID       "PCI:1:0:0"

  # Need to flag this as only referring to one output on the card
  Screen      0

  # For nVidia devices, this controls which connector the monitor is connected to.
  Option      "UseDisplayDevice"   "DFP-0"

  # We want control!
  Option      "DynamicTwinView"    "FALSE"

  # Various performance and configuration options
  Option      "AddARGBGLXVisuals"  "true"
  Option      "UseEDIDDpi"         "false"
  Option      "DPI"                "96 x 96"
  Option      "Coolbits"           "1"
EndSection

# Second head of same video card (note different Identifier but same BusID.)  We can omit
# the UseDisplayDevice option this time as it will pick whichever one is remaining.
Section "Device"
  Identifier  "GeForce 8600GTb"
  Driver      "nvidia"
  BusID       "PCI:1:0:0"
  # This is the second output on this card
  Screen      1

  # Same config options for all cards
  Option      "AddARGBGLXVisuals"  "true"
  Option      "UseEDIDDpi"         "false"
  Option      "DPI"                "96 x 96"
  Option      "Coolbits"           "1"
  Option      "DynamicTwinView"    "FALSE"
EndSection

# First head of second video card, note different BusID.
Section "Device"
  Identifier  "G210"
  Driver      "nvidia"
  BusID       "PCI:2:0:0"
  Screen      0

  # Same config options for all cards
  Option      "AddARGBGLXVisuals"  "true"
  Option      "UseEDIDDpi"         "false"
  Option      "DPI"                "96 x 96"
  Option      "Coolbits"           "1"
  Option      "DynamicTwinView"    "FALSE"
EndSection

# Second head of second video card.  Output connector is set here, which means the previous
# Device will use the other connector, whatever it may be.
Section "Device"
  Identifier  "G210b"
  Driver      "nvidia"
  BusID       "PCI:2:0:0"
  Screen      1
  Option      "UseDisplayDevice"   "DFP-1"

  # Same config options for all cards
  Option      "AddARGBGLXVisuals"  "true"
  Option      "UseEDIDDpi"         "false"
  Option      "DPI"                "96 x 96"
  Option      "Coolbits"           "1"
  Option      "DynamicTwinView"    "FALSE"
EndSection

参见