# 6.6.使用 Xwayland

在安装 Wayland 时，除非 Wayland 是在没有 X11 支持的情况下构建的，否则 `Xwayland` 二进制文件应该已经安装。如果 `/usr/local/bin/Xwayland` 文件不存在，请使用以下命令安装它：

```sh
# pkg install xwayland
```

> **注意**
>
> 推荐使用 Xwayland 的开发版本，并且它很可能与 Wayland 包一起安装。每个合成器都有启用或禁用此功能的方法。

安装 `Xwayland` 后，需要在所选合成器中进行配置。对于 Wayfire，在 `wayfire.ini` 文件中需要添加以下行：

```ini
xwayland = true
```

对于 Sway 合成器，`Xwayland` 应该默认启用。即使如此，建议手动在 `~/.config/sway/config` 中添加以下配置行：

```ini
xwayland enable
```

最后，对于 Hikari，不需要做任何更改。默认情况下，`Xwayland` 的支持已内建。如果需要禁用该支持，可以从 Ports 重新构建该包，并在此时禁用 `Xwayland` 支持。

完成这些更改后，启动合成器并通过键绑定执行一个终端。在此终端中，执行 `env` 命令并搜索 `DISPLAY` 变量。如果合成器能够正确启动 `Xwayland` X 服务器，则这些环境变量应该类似于以下内容：

```sh
% env | grep DISPLAY
```

```sh
WAYLAND_DISPLAY=wayland-1
DISPLAY=:0
```

在此输出中，显示了默认的 Wayland 显示器和为 `Xwayland` 服务器设置的显示器。验证 `Xwayland` 是否正常工作的另一种方法是安装并测试一个小的包：\[x11/eyes]，并检查输出。如果 `xeyes` 应用程序启动并且眼睛跟随鼠标指针，则说明 `Xwayland` 正常工作。如果出现如下错误，说明在 `Xwayland` 初始化期间发生了问题，可能需要重新安装：

```sh
Error: Cannot open display wayland-0
```

> **警告**
>
> Wayland 的一个安全特性是，在没有运行 X 服务器的情况下，不会有其他网络监听程序。待启用 `Xwayland`，此安全特性不再适用于系统。

对于某些合成器，如 Wayfire，`Xwayland` 可能无法正确启动。因此，`env` 会显示以下 `DISPLAY` 环境变量信息：

```sh
% env | grep DISPLAY
```

```sh
DISPLAY=wayland-1
WAYLAND_DISPLAY=wayland-1
```

尽管已经安装并配置了 `Xwayland`，X11 应用程序仍然无法启动，并会出现显示问题。为了解决这个问题，需要验证是否已经有一个通过 UNIX 套接字运行的 `Xwayland` 实例。首先，检查 `sockstat` 的输出并搜索 X11-unix：

```sh
% sockstat | grep x11
```

输出应该类似于以下信息：

```sh
trhodes  Xwayland   2734  8  stream /tmp/.X11-unix/X0
trhodes  Xwayland   2734  9  stream /tmp/.X11-unix/X0
trhodes  Xwayland   2734  10 stream /tmp/.X11-unix/X0
trhodes  Xwayland   2734  27 stream /tmp/.X11-unix/X0_
trhodes  Xwayland   2734  28 stream /tmp/.X11-unix/X0
```

这表明存在一个 X11 套接字。可以进一步通过在合成器下的终端仿真器中尝试手动执行 `Xwayland` 来验证这一点：

```sh
% Xwayland
```

如果已经有一个 X11 套接字可用，用户会看到以下错误：

```sh
(EE)
Fatal server error:
(EE) Server is already active for display 0
	If this server is no longer running, remove /tmp/.X0-lock
	and start again.
(EE)
```

由于已经有一个活跃的 X 显示器（显示号为 0），因此环境变量设置不正确。要修复此问题，可以将 `DISPLAY` 环境变量更改为 `:0` 并重新尝试执行该应用程序。以下示例使用 [mail/claws-mail](https://cgit.freebsd.org/ports/tree/mail/claws-mail/) 作为需要 `Xwayland` 服务的应用程序：

```sh
export DISPLAY=:0
```

做出此更改后，[mail/claws-mail](https://cgit.freebsd.org/ports/tree/mail/claws-mail/) 应用程序应该能够使用 `Xwayland` 启动并按预期工作。


---

# Agent Instructions: 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-6-zhang-freebsd-zhong-de-wayland/6.6.shi-yong-xwayland.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.
