> For the complete documentation index, see [llms.txt](https://handbook.bsdcn.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://handbook.bsdcn.org/di-5-zhang-xwindow-xi-tong/5.5.-xorg-pei-zhi.md).

# 5.5.配置 X.org

**摘要**：如果显示器或输入设备的默认设置不符合要求，[桌面](/di-8-zhang-zhuo-mian-huan-jing/8.1.-gai-shu.md) 提供了图形界面进行配置，或者也可以手动配置。

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**

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

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

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

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

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

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

```sh
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**

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

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

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

```sh
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**

```sh
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(1)](https://man.freebsd.org/cgi/man.cgi?query=xrandr\&sektion=1\&format=html) 不带参数，可查看视频输出和检测到的显示模式：

```sh
% xrandr
```

输出示例：

```sh
Screen 0: minimum 320 x 200, current 2560 x 960, maximum 8192 x 8192
LVDS-1 connected 1280x800+0+0 (normal left inverted right x axis y axis) 261mm x 163mm
   1280x800      59.99*+  59.81    59.91    50.00
   1280x720      59.86    59.74
   1024x768      60.00
   1024x576      59.90    59.82
   960x540       59.63    59.82
   800x600       60.32    56.25
   864x486       59.92    59.57
   640x480       59.94
   720x405       59.51    58.99
   640x360       59.84    59.32
VGA-1 connected primary 1280x960+1280+0 (normal left inverted right x axis y axis) 410mm x 257mm
   1280x1024     75.02    60.02
   1440x900      74.98    60.07
   1280x960      60.00*
   1280x800      74.93    59.81
   1152x864      75.00
   1024x768      75.03    70.07    60.00
   832x624       74.55
   800x600       72.19    75.00    60.32    56.25
   640x480       75.00    72.81    66.67    59.94
   720x400       70.08
HDMI-1 disconnected (normal left inverted right x axis y axis)
DP-1 disconnected (normal left inverted right x axis y axis)
HDMI-2 disconnected (normal left inverted right x axis y axis)
DP-2 disconnected (normal left inverted right x axis y axis)
DP-3 disconnected (normal left inverted right x axis y axis)
```

此输出显示 `VGA-1` 输出正在以约 60 Hz 的刷新率显示 1280x960 像素的屏幕分辨率。`LVDS-1` 作为辅助显示器，以约 60 Hz 的刷新率显示 1280x800 像素的屏幕分辨率。`HDMI-1`、`HDMI-2`、`DP-1`、`DP-2` 和 `DP-3` 接口未连接显示器。

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

```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**

```sh
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 分类中还提供了旧版、轻量的独立输入设备驱动 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](/di-8-zhang-zhuo-mian-huan-jing/8.2.-zhuo-mian-huan-jing.md)）提供图形界面设置参数，请先检查是否可用，再考虑手动编辑配置文件。

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

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

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


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://handbook.bsdcn.org/di-5-zhang-xwindow-xi-tong/5.5.-xorg-pei-zhi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
