# 5.5.配置 X.org

**摘要**：如果显示器或输入设备的默认设置不符合要求，[桌面](https://docs.freebsd.org/en/books/handbook/desktop/#desktop-synopsis) 提供了图形界面进行配置，或者也可以手动配置。

X.org 服务器支持大多数常见的图形处理器、显示器和输入设备。首先应尝试使用默认设置。本小节概述其配置方法。

## 5.5.1. X.org 配置文件

过去，X.org 服务器通过 **/usr/local/etc/X11/** 下的文件进行配置。这种方式仍然被支持用于特殊情况，但可能与动态自动配置冲突。

除非自动配置失败，否则不要在 **xorg.conf** 中创建配置，也不要运行 `Xorg -configure`。

X.org 服务器会在多个目录中查找配置文件。**/usr/local/etc/X11/** 是 FreeBSD 上推荐的配置文件目录。使用此目录有助于将应用程序文件与操作系统文件分开管理。

使用多个文件分别配置具体设置，比传统的单一 **xorg.conf** 更加方便。这些文件存储在子目录 **/usr/local/etc/X11/xorg.conf.d/** 中。

## 5.5.2. 配置显卡

直接渲染允许同时无缝使用独立显卡（dGPU）和集成显卡（iGPU），称为 PRIME。驱动程序会在需要时自动将高强度任务卸载到 dGPU，并在可能时关闭其电源。

在 PRIME 中使用更强大的 GPU 启动应用程序时，使用环境变量 `DRI_PRIME=1`。

如果多个图形驱动冲突，可在 **/usr/local/etc/X11/xorg.conf.d/** 目录中指定图形处理器的驱动。

**Intel® 驱动配置示例：**

**/usr/local/etc/X11/xorg.conf.d/20-intel.conf**

```ini
Section "Device"
	Identifier "Card0"
	Driver     "intel"
EndSection
```

**AMD® 驱动配置示例：**

**/usr/local/etc/X11/xorg.conf.d/20-radeon.conf**

```ini
Section "Device"
	Identifier "Card0"
	Driver     "radeon"
EndSection
```

**NVIDIA® 驱动配置示例：**

**/usr/local/etc/X11/xorg.conf.d/20-nvidia.conf**

```ini
Section "Device"
	Identifier "Card0"
	Driver     "nvidia-modeset"
EndSection
```

> **技巧**
>
> [x11/nvidia-xconfig](https://cgit.freebsd.org/ports/tree/x11/nvidia-xconfig/) 也可用于对 NVIDIA 驱动的配置选项进行基本控制。

**SCFB 驱动配置示例：**

**/usr/local/etc/X11/xorg.conf.d/20-scfb.conf**

```ini
Section "Device"
	Identifier "Card0"
	Driver     "scfb"
EndSection
```

**VESA 驱动配置示例：**

**/usr/local/etc/X11/xorg.conf.d/20-vesa.conf**

```ini
Section "Device"
	Identifier "Card0"
	Driver     "vesa"
EndSection
```

配置多显卡时，可添加 `BusID`。执行以下命令显示显卡总线 ID 列表：

```sh
% pciconf -lv | grep -B3 display
```

示例输出：

```sh
vgapci0@pci0:0:2:0:     class=0x030000 rev=0x0c hdr=0x00 vendor=0x8086 device=0x46a6 subvendor=0x1028 subdevice=0x0b29
    vendor     = 'Intel Corporation'
    device     = 'Alder Lake-P GT2 [Iris Xe Graphics]'
    class      = display
--
vgapci0@pci0:1:0:0:     class=0x030200 rev=0xa1 hdr=0x00 vendor=0x10de device=0x25b9 subvendor=0x1028 subdevice=0x0b29
    vendor     = 'NVIDIA Corporation'
    device     = 'GA107GLM [RTX A1000 Laptop GPU]'
    class      = display
```

**示例：同时配置 Intel® 和 NVIDIA® 驱动**

**/usr/local/etc/X11/xorg.conf.d/20-drivers.conf**

```ini
Section "Device"
	Identifier "Card0"
	Driver     "intel"
	BusID     "pci0:0:2:0"
EndSection

Section "Device"
	Identifier "Card1"
	Driver     "nvidia-modeset"
	BusID     "pci0:0:2:1"
EndSection
```

## 5.5.3. 配置显示器

几乎所有显示器都支持扩展显示识别数据（`EDID`）标准。X.org 使用 `EDID` 与显示器通信，检测支持的分辨率和刷新率，然后选择最合适的组合。

其他显示器支持的分辨率可在 X 服务器启动后，通过 [xrandr(1)](https://man.freebsd.org/cgi/man.cgi?query=xrandr\&sektion=1\&format=html) 原子方式选择，或在 X.org 配置文件中设置。

### 5.5.3.1. 使用 RandR（调整分辨率与方向）

在 X 会话中运行 `xrandr` 不带参数，可查看视频输出和检测到的显示模式：

```sh
% xrandr
```

示例输出显示 `VGA-1` 输出使用分辨率 1280x960，刷新率约 60Hz；`LVDS-1` 为辅助显示器，分辨率 1280x800，刷新率约 60Hz。

可用 [xrandr(1)](https://man.freebsd.org/cgi/man.cgi?query=xrandr\&sektion=1\&format=html) 命令切换其他显示模式，例如切换到 1280x720 分辨率，刷新率 60Hz：

```sh
% xrandr --output LVDS-1 --mode 1280x720 --rate 60
```

> **技巧**
>
> 启动 X 时黑屏通常可通过在初始化过程中添加 `xrandr --auto` 这一步骤解决。

### 5.5.3.2. 使用 X.org 配置文件

可在配置文件中设置显示器参数。

**示例：将屏幕分辨率设置为 1024x768**

**/usr/local/etc/X11/xorg.conf.d/10-monitor.conf**

```ini
Section "Screen"
	Identifier "Screen0"
	Device     "Card0"
	SubSection "Display"
	Modes      "1024x768"
	EndSubSection
EndSection
```

## 5.5.4. 输入设备配置

X.org 服务器提供了 [x11/libinput](https://cgit.freebsd.org/ports/tree/x11/libinput/) 库，统一支持触控、指点和键盘设备。默认情况下，该库会自动加载。

各设备的设置可通过桌面 GUI 调整，或使用 [xinput](https://cgit.freebsd.org/ports/tree/x11/xinput/) 和 [setxkbmap](https://cgit.freebsd.org/ports/tree/x11/setxkbmap/) 手动配置。

对于旧版轻量输入设备，可使用 [x11-drivers](https://cgit.freebsd.org/ports/tree/x11-drivers/) 中的 x11/xf86-input-\[foo] 驱动，但需手动配置 X.org 服务器。

### 5.5.4.1. 使用原子输入配置

使用 [libinput(4)](https://man.freebsd.org/cgi/man.cgi?query=libinput\&sektion=4\&format=html) 支持的设备，可通过桌面提供的图形工具，或在运行时使用 [x11/xinput](https://cgit.freebsd.org/ports/tree/x11/xinput/) 和 [x11/setxkbmap](https://cgit.freebsd.org/ports/tree/x11/setxkbmap/) 进行原子配置。

查看 libinput 当前附加设备：

```sh
$ xinput
```

输出示例：

```sh
⎡ Virtual core pointer                    	id=2	[master pointer  (3)]
⎜   ↳ Virtual core XTEST pointer              	id=4	[slave  pointer  (2)]
⎜   ↳ System mouse                            	id=7	[slave  pointer  (2)]
⎜   ↳ VEN_0488:00 0488:1031 Mouse             	id=11	[slave  pointer  (2)]
⎜   ↳ VEN_0488:00 0488:1031 TouchPad          	id=12	[slave  pointer  (2)]
⎣ Virtual core keyboard                   	id=3	[master keyboard (2)]
    ↳ Virtual core XTEST keyboard             	id=5	[slave  keyboard (3)]
    ↳ System keyboard multiplexer             	id=6	[slave  keyboard (3)]
    ↳ Power Button                            	id=8	[slave  keyboard (3)]
    ↳ Sleep Button                            	id=9	[slave  keyboard (3)]
    ↳ AT keyboard                             	id=10	[slave  keyboard (3)]
```

所有支持的设置以属性形式提供，可原子方式列出和设置。指点设备属性多，键盘一般无需调整。

自定义键盘布局请参考 [setxkbmap(1)](https://man.freebsd.org/cgi/man.cgi?query=setxkbmap\&sektion=1\&format=html)。

配置完成后，将命令添加至 X 初始化脚本，如 **\~/.Xsession** 或 **\~/.xinitrc**。

### 5.5.4.2. 使用 X.org 配置文件

> **技巧**
>
> 部分桌面环境（如 [KDE Plasma](https://docs.freebsd.org/en/books/handbook/desktop/#kde-environment)）提供图形界面设置参数，请先检查是否可用，再考虑手动编辑配置文件。

**示例：手动配置键盘布局**

**/usr/local/etc/X11/xorg.conf.d/00-keyboard.conf**

```ini
Section "InputClass"
        Identifier "Keyboard1"
        MatchIsKeyboard "on"
        Option "XkbLayout" "es, fr"
        Option "XkbModel" "pc104"
        Option "XkbVariant" ",qwerty"
        Option "XkbOptions" "grp:win_space_toggle"
EndSection
```
