mirror of
https://github.com/VallierDev/uu-remote-linux.git
synced 2026-11-16 16:50:41 +01:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7beb3aecf0 | ||
|
|
26d4812480 | ||
|
|
49edf1d389 |
No files matched your search
@@ -26,6 +26,7 @@ jobs:
|
||||
name=uu-remote-linux-${GITHUB_REF_NAME}-x86_64-linux
|
||||
mkdir -p "$name"
|
||||
cp target/release/uuremote-server LICENSE README.md README.en.md config.example.json "$name/"
|
||||
cp -r contrib "$name/"
|
||||
tar czf "$name.tar.gz" "$name"
|
||||
sha256sum "$name.tar.gz" > "$name.tar.gz.sha256"
|
||||
- uses: softprops/action-gh-release@v2
|
||||
|
||||
@@ -104,7 +104,10 @@ viewer does not reconnect them automatically.
|
||||
|---|---|
|
||||
| `~/.config/uuremote/config.json` | user_id, device_id, client_id, system_id, machine_guid, token, csrf; optional `sign_key`, `ffmpeg_root`. `$UU_CONFIG` overrides the path |
|
||||
| `~/.config/uuremote/host_settings.json` | persisted 安全 settings: auto_lock, keep_mute, mute_permanent |
|
||||
| `UU_DESKTOP_BACKEND` | `auto` (default) / `x11` / `wayland` / `portal` |
|
||||
| `UU_DESKTOP_BACKEND` | `auto` (default) / `x11` / `wayland` / `portal` / `none` (headless: no display; desktop offers ignored; tool peers get a nominal 1920x1080 screen + Allow permissions, which the port-mapping viewer requires) |
|
||||
| `UU_INPUT_BACKEND` | `auto` (uinput on Wayland/portal when `/dev/uinput` is accessible, udev `uaccess` rule in README) / `uinput` / `native`. X11 keeps XTest (held-key autorepeat handling) |
|
||||
| `UU_TERMINAL_SHORTCUTS=0` | disable Ctrl+V → Ctrl+Shift+V in focused X11 terminals (Mac Cmd and Ctrl both arrive as VK_LCONTROL) |
|
||||
| `contrib/virtual-desktop/` | Xvfb + session (`UU_VIRTUAL_SESSION`, `UU_VIRTUAL_BUS=user|private`) for monitorless hosts; serve it with `DISPLAY=:99` |
|
||||
| `UU_VIDEO_ENCODER=cpu` | force libx264 instead of NVENC |
|
||||
| `UU_FFMPEG_ROOT` | FFmpeg prefix (`bin/ffmpeg`, libs in `lib/x86_64-linux-gnu`) |
|
||||
| `UU_AUDIO=0`, `UU_AUDIO_SOURCE` | disable sound / choose the Pulse source (default `@DEFAULT_MONITOR@`) |
|
||||
@@ -123,7 +126,7 @@ every 60 s.
|
||||
| `src/webrtc.rs` | peer connection, data channels, CONTROL dispatch, initialization messages |
|
||||
| `src/video.rs`, `src/capture_setting.rs` | FFmpeg H.264 pipeline; viewer quality/fps/resolution settings |
|
||||
| `src/audio.rs` | Pulse/PipeWire monitor → Opus audio track |
|
||||
| `src/input.rs`, `src/input_mapping.rs` | input injection (X11 XTest / Wayland / portal), Windows VK → keysym |
|
||||
| `src/input.rs`, `src/input_mapping.rs`, `src/uinput.rs` | input injection (X11 XTest / Wayland / portal / uinput), Windows VK → keysym → evdev |
|
||||
| `src/cursor.rs` | pointer shape mirroring (X11) |
|
||||
| `src/clipboard*.rs` | text clipboard bridge and protocol |
|
||||
| `src/file_trans.rs` | FileTransferFtp RPC |
|
||||
@@ -196,6 +199,9 @@ started a second full host server instead, so check that a subcommand exists in
|
||||
- Remote keys repeat at high RTT because key-up arrives after the X autorepeat delay. XTEST repeats through the
|
||||
master keyboard; the engine disables global autorepeat only while a remote key is held.
|
||||
- Interactive shells ignore SIGTERM: the server HUPs/KILLs PTY sessions on shutdown.
|
||||
- An explicit `DISPLAY` that cannot be opened is an error; the X11 engine falls back to `:0`/`:1` only when DISPLAY is unset.
|
||||
- systemd/D-Bus-activated apps (GNOME Terminal, portals) run with the user manager's environment, not the caller's:
|
||||
a private `dbus-run-session` bus does not bring them onto a virtual display.
|
||||
- GTK windows stop repainting while GNOME is locked or the monitor is off; test UI on Xvfb or after unlock.
|
||||
- logind `unlock-session` by the session owner clears the GNOME shield in ~3 s.
|
||||
- GNOME closes the portal session on lock; capture/input must share one portal session and be rebuilt after
|
||||
|
||||
Generated
+1
-1
@@ -2649,7 +2649,7 @@ checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
|
||||
|
||||
[[package]]
|
||||
name = "uu-remote-linux"
|
||||
version = "0.1.5"
|
||||
version = "0.1.6"
|
||||
dependencies = [
|
||||
"base64 0.22.1",
|
||||
"bytes",
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "uu-remote-linux"
|
||||
version = "0.1.5"
|
||||
version = "0.1.6"
|
||||
edition = "2021"
|
||||
authors = ["uu-remote-linux contributors"]
|
||||
description = "Unofficial Linux host for NetEase UU Remote: control a Linux desktop from the official clients"
|
||||
|
||||
+49
-3
@@ -33,7 +33,8 @@
|
||||
|---|---|
|
||||
| QR login | GTK login window (or a terminal QR code with `login --no-gui`); expired codes refresh automatically; the service opens the login window by itself when credentials are missing or expired |
|
||||
| Remote desktop | H.264 video, NVENC hardware encoding on NVIDIA GPUs, libx264 otherwise; the viewer draws its local pointer, so there is no double cursor |
|
||||
| Mouse / keyboard | Absolute and relative motion, clicks, wheel, keys; no spurious key repeat on high-latency links |
|
||||
| Mouse / keyboard | Absolute and relative motion, clicks, wheel, keys; no spurious key repeat on high-latency links; optional uinput virtual devices on Wayland (layout independent, see below) |
|
||||
| Terminal shortcuts | Mac viewers send both Cmd and Ctrl as Ctrl. In a focused terminal window Ctrl+V / Cmd+V become Ctrl+Shift+V (paste); Ctrl+C always interrupts, copy with Cmd+Shift+C (X11) |
|
||||
| Pointer shape | Remote cursor shape is mirrored to the viewer (X11) |
|
||||
| Clipboard | Two-way text sync |
|
||||
| Quality / frame rate / resolution | Auto, Original 20M, Ultra 8M, HD 2M, custom; 30/60/90/144 fps; "other resolutions" are scaled by the encoder, the host monitor is left untouched |
|
||||
@@ -46,6 +47,7 @@
|
||||
| System actions | Lock, show desktop, task manager (system monitor), reboot, shut down |
|
||||
| Port mapping | The official "port mapping" tool: forwards a local port on the viewer to an address reachable from the Linux host (e.g. 127.0.0.1:22); `UU_PORT_MAPPING=0` disables it |
|
||||
| Reconnects | Every tool window has its own connection; they do not disturb each other |
|
||||
| Headless servers | Goes online without a monitor with terminal, file transfer and port mapping; an Xvfb virtual desktop gives a full remote desktop (see below) |
|
||||
|
||||
## Supported Linux desktops
|
||||
|
||||
@@ -77,7 +79,7 @@ permission; the permission is remembered once granted.
|
||||
| Privacy screen on the host | Not implemented |
|
||||
| Microphone (viewer audio played on the host) | Not implemented (needs a virtual sound card) |
|
||||
| Plugins such as the WoW queue notifier | Not supported; the client is told that no plugins exist |
|
||||
| Access before login (greeter / no user logged in) | Not supported; only an already logged-in desktop session can be controlled |
|
||||
| Access before login (greeter / no user logged in) | The greeter cannot be controlled; without a logged-in user use headless mode or a virtual desktop (see below) |
|
||||
| Relay connections on restrictive networks | Uses the ICE servers provided by the UU cloud; complex network setups are not thoroughly tested |
|
||||
| Acting as a viewer (controlling other machines from Linux) | Out of scope |
|
||||
|
||||
@@ -149,6 +151,48 @@ journalctl --user -u uuremote-native.service -f # online once you see availabi
|
||||
|
||||
If you have not logged in yet, the service opens the QR login window on your desktop.
|
||||
|
||||
### 5. uinput virtual keyboard/mouse on Wayland (recommended)
|
||||
|
||||
On Wayland (especially GNOME/KDE via the portal) the default input path needs portal consent and some keyboard layouts
|
||||
drop symbols such as the underscore. Once the logged-in user may access `/dev/uinput`, the host switches to kernel
|
||||
virtual devices that send physical key codes:
|
||||
|
||||
```sh
|
||||
echo 'KERNEL=="uinput", SUBSYSTEM=="misc", OPTIONS+="static_node=uinput", TAG+="uaccess"' | \
|
||||
sudo tee /etc/udev/rules.d/60-uu-remote-linux-uinput.rules
|
||||
sudo udevadm control --reload && sudo udevadm trigger --name-match=uinput
|
||||
```
|
||||
|
||||
`uaccess` grants access only to the user sitting at the machine. `UU_INPUT_BACKEND=uinput|native` forces a backend.
|
||||
X11 keeps XTest by default because it handles held keys correctly on high-latency links.
|
||||
|
||||
## Servers without a monitor
|
||||
|
||||
**Headless mode** (terminal, file transfer and port mapping only): add `Environment=UU_DESKTOP_BACKEND=none` to the
|
||||
service and run `loginctl enable-linger $USER`, so the systemd user manager (with `XDG_RUNTIME_DIR` and the user D-Bus)
|
||||
starts at boot without a login. The viewer's remote desktop keeps showing "connecting" in this mode; that is expected.
|
||||
Log in once with `uuremote-server login --no-gui` (QR in the terminal).
|
||||
|
||||
**Virtual desktop**: for a full remote desktop, run a desktop session (Xfce recommended) on an Xvfb screen:
|
||||
|
||||
```sh
|
||||
sudo apt install xvfb dbus-x11 xfce4 # or another session, chosen with UU_VIRTUAL_SESSION
|
||||
install -Dm755 contrib/virtual-desktop/uu-virtual-desktop ~/.local/bin/uu-virtual-desktop
|
||||
install -Dm644 contrib/virtual-desktop/uu-virtual-desktop.service ~/.config/systemd/user/uu-virtual-desktop.service
|
||||
install -Dm644 contrib/virtual-desktop/virtual-display.conf ~/.config/systemd/user/uuremote-native.service.d/virtual-display.conf
|
||||
loginctl enable-linger $USER
|
||||
systemctl --user daemon-reload && systemctl --user enable --now uu-virtual-desktop.service
|
||||
systemctl --user restart uuremote-native.service
|
||||
```
|
||||
|
||||
The virtual screen is `:99`, 1920x1080 by default. Xvfb has no GPU acceleration, so it is a little less smooth than a
|
||||
real display; NVENC is still used for encoding when an NVIDIA GPU is present. The script exports `DISPLAY` to the
|
||||
systemd user session so D-Bus-activated programs open on the virtual screen; on a machine that also runs a real desktop
|
||||
session set `UU_VIRTUAL_BUS=private` so the real session is not affected.
|
||||
|
||||
To make a GPU drive a real (non-Xvfb) desktop without a monitor, use a dummy HDMI plug or load an EDID file with the
|
||||
kernel parameter `drm.edid_firmware=`, then install as for a normal X11 desktop.
|
||||
|
||||
## Configuration
|
||||
|
||||
Common environment variables:
|
||||
@@ -156,7 +200,9 @@ Common environment variables:
|
||||
| Variable | Purpose |
|
||||
|---|---|
|
||||
| `UU_CONFIG` | Config file path (default `~/.config/uuremote/config.json`) |
|
||||
| `UU_DESKTOP_BACKEND` | `auto` / `x11` / `wayland` / `portal` |
|
||||
| `UU_DESKTOP_BACKEND` | `auto` / `x11` / `wayland` / `portal` / `none` (headless) |
|
||||
| `UU_INPUT_BACKEND` | `auto` (default: uinput on Wayland when accessible) / `uinput` / `native` |
|
||||
| `UU_TERMINAL_SHORTCUTS=0` | Disable the Ctrl+V paste rewrite in terminal windows |
|
||||
| `UU_VIDEO_ENCODER=cpu` | Force CPU encoding |
|
||||
| `UU_FFMPEG_ROOT` | Use FFmpeg from this prefix |
|
||||
| `UU_AUDIO=0` | Disable sound; `UU_AUDIO_SOURCE` picks the PulseAudio source |
|
||||
|
||||
@@ -29,7 +29,8 @@
|
||||
|---|---|
|
||||
| 扫码登录 | GTK 登录窗口(或 `login --no-gui` 终端二维码);二维码过期自动刷新;登录凭证失效时服务会自动弹出登录窗口 |
|
||||
| 远程桌面 | H.264 视频,NVIDIA 显卡用 NVENC 硬件编码,否则用 libx264;查看端绘制本地鼠标,不会出现双光标 |
|
||||
| 鼠标 / 键盘 | 绝对/相对移动、点击、滚轮、按键;修复了网络延迟导致的按键连发 |
|
||||
| 鼠标 / 键盘 | 绝对/相对移动、点击、滚轮、按键;修复了网络延迟导致的按键连发;Wayland 下可用 uinput 虚拟键鼠(不依赖键盘布局,见下文) |
|
||||
| 终端快捷键 | Mac 控制端的 Cmd 和 Ctrl 到被控端都是 Ctrl。焦点在终端窗口时,Ctrl+V / Cmd+V 自动转成 Ctrl+Shift+V 粘贴;Ctrl+C 始终是中断,复制请用 Cmd+Shift+C(X11) |
|
||||
| 鼠标指针样式 | 远端指针形状实时同步(X11) |
|
||||
| 剪贴板 | 双向文本同步 |
|
||||
| 画质 / 帧率 / 分辨率 | 自动、原画 20M、超清 8M、高清 2M、自定义;30/60/90/144 帧;「其他分辨率」由编码器缩放,不改动主机显示器 |
|
||||
@@ -42,6 +43,7 @@
|
||||
| 系统操作 | 锁屏、显示桌面、任务管理器(系统监视器)、重启、关机 |
|
||||
| 端口映射 | 官方「端口映射」工具:把控制端本地端口转发到 Linux 能访问到的地址(如 127.0.0.1:22);`UU_PORT_MAPPING=0` 可关闭 |
|
||||
| 多次重连 | 每个工具窗口独立连接,互不影响 |
|
||||
| 无头服务器 | 没有显示器也能上线,提供终端、文件传输和端口映射;也可以用 Xvfb 虚拟桌面提供完整远程桌面(见下文) |
|
||||
|
||||
## 支持的 Linux 桌面
|
||||
|
||||
@@ -71,7 +73,7 @@ Wayland 下的限制:只支持单屏、固定分辨率;portal 后端暂不
|
||||
| 被控端防窥模式 | 未实现 |
|
||||
| 麦克风(控制端声音传到被控端) | 未实现(需要虚拟声卡) |
|
||||
| 魔兽世界排队提醒等插件 | 不支持,会明确告诉客户端「无插件」 |
|
||||
| 登录前(登录界面 / 无人登录)访问 | 不支持,只能控制已登录的桌面会话 |
|
||||
| 登录前(登录界面 / 无人登录)访问 | 不支持控制登录界面;没有人登录时可以用无头模式或虚拟桌面(见下文) |
|
||||
| 跨运营商 / 受限网络的中继连接 | 已接入官方下发的 ICE 服务器,但复杂网络环境未充分测试 |
|
||||
| 作为控制端(在 Linux 上控制别的电脑) | 不在本项目范围 |
|
||||
|
||||
@@ -142,6 +144,45 @@ journalctl --user -u uuremote-native.service -f # 看到 availability=Some("re
|
||||
|
||||
如果还没有登录,服务会自动在桌面上弹出扫码窗口。
|
||||
|
||||
### 5. Wayland 下使用 uinput 虚拟键鼠(推荐)
|
||||
|
||||
Wayland(尤其是 GNOME/KDE portal)下,默认的键鼠注入要经过 portal 授权,部分键盘布局下还会丢符号(比如下划线)。
|
||||
允许当前登录用户访问 `/dev/uinput` 后,服务会自动改用内核级虚拟键盘和鼠标,发送物理按键码:
|
||||
|
||||
```sh
|
||||
echo 'KERNEL=="uinput", SUBSYSTEM=="misc", OPTIONS+="static_node=uinput", TAG+="uaccess"' | \
|
||||
sudo tee /etc/udev/rules.d/60-uu-remote-linux-uinput.rules
|
||||
sudo udevadm control --reload && sudo udevadm trigger --name-match=uinput
|
||||
```
|
||||
|
||||
`uaccess` 只把权限给当前坐在这台机器前的登录用户。`UU_INPUT_BACKEND=uinput|native` 可以强制指定后端。
|
||||
X11 默认继续用 XTest,因为它能正确处理网络延迟下按住不放的按键。
|
||||
|
||||
## 没有显示器的服务器
|
||||
|
||||
**无头模式**:只要终端、文件传输和端口映射,不需要图形界面。在服务里加 `Environment=UU_DESKTOP_BACKEND=none`,
|
||||
再执行 `loginctl enable-linger $USER`,这样开机后不用登录,systemd 用户服务(以及 `XDG_RUNTIME_DIR`、用户 D-Bus)也会自动启动。
|
||||
无头模式下控制端点「远程桌面」会一直显示正在连接,这是正常的。首次登录用 `uuremote-server login --no-gui` 在终端里扫码。
|
||||
|
||||
**虚拟桌面**:需要完整的远程桌面时,用 Xvfb 创建一块虚拟屏幕,再在上面跑一个桌面环境(推荐 Xfce):
|
||||
|
||||
```sh
|
||||
sudo apt install xvfb dbus-x11 xfce4 # 或其他桌面环境,用 UU_VIRTUAL_SESSION 指定
|
||||
install -Dm755 contrib/virtual-desktop/uu-virtual-desktop ~/.local/bin/uu-virtual-desktop
|
||||
install -Dm644 contrib/virtual-desktop/uu-virtual-desktop.service ~/.config/systemd/user/uu-virtual-desktop.service
|
||||
install -Dm644 contrib/virtual-desktop/virtual-display.conf ~/.config/systemd/user/uuremote-native.service.d/virtual-display.conf
|
||||
loginctl enable-linger $USER
|
||||
systemctl --user daemon-reload && systemctl --user enable --now uu-virtual-desktop.service
|
||||
systemctl --user restart uuremote-native.service
|
||||
```
|
||||
|
||||
虚拟屏幕默认是 `:99`、1920x1080。Xvfb 没有 GPU 加速,流畅度比真实显示器略差;有 NVIDIA 显卡时视频仍然用 NVENC 编码。
|
||||
脚本会把 `DISPLAY` 导入 systemd 用户会话,这样通过 D-Bus 启动的程序也会显示在虚拟屏幕上;
|
||||
如果这台机器同时还有真实的桌面会话,请设 `UU_VIRTUAL_BUS=private`,避免影响真实桌面。
|
||||
|
||||
想让 NVIDIA/AMD 显卡在不接显示器时也正常输出画面(而不是用 Xvfb),可以用假显示器插头(HDMI 诱骗器),
|
||||
或者用内核参数 `drm.edid_firmware=` 加载 EDID 文件,再按普通 X11 桌面的方式安装。
|
||||
|
||||
## 配置
|
||||
|
||||
常用环境变量:
|
||||
@@ -149,7 +190,9 @@ journalctl --user -u uuremote-native.service -f # 看到 availability=Some("re
|
||||
| 变量 | 作用 |
|
||||
|---|---|
|
||||
| `UU_CONFIG` | 配置文件路径(默认 `~/.config/uuremote/config.json`) |
|
||||
| `UU_DESKTOP_BACKEND` | `auto` / `x11` / `wayland` / `portal` |
|
||||
| `UU_DESKTOP_BACKEND` | `auto` / `x11` / `wayland` / `portal` / `none`(无头模式) |
|
||||
| `UU_INPUT_BACKEND` | `auto`(默认,Wayland 下可用时用 uinput)/ `uinput` / `native` |
|
||||
| `UU_TERMINAL_SHORTCUTS=0` | 关闭终端窗口里的 Ctrl+V 粘贴转换 |
|
||||
| `UU_VIDEO_ENCODER=cpu` | 强制使用 CPU 编码 |
|
||||
| `UU_FFMPEG_ROOT` | 使用指定目录下的 FFmpeg |
|
||||
| `UU_AUDIO=0` | 关闭声音转发;`UU_AUDIO_SOURCE` 指定 PulseAudio 采集源 |
|
||||
|
||||
+47
-2
@@ -9,6 +9,8 @@
|
||||
#include <sys/ipc.h>
|
||||
#include <X11/Xlib.h>
|
||||
#include <X11/Xutil.h>
|
||||
#include <X11/Xatom.h>
|
||||
#include <strings.h>
|
||||
#include <X11/extensions/XShm.h>
|
||||
#include <X11/extensions/XTest.h>
|
||||
#include <X11/extensions/Xfixes.h>
|
||||
@@ -58,9 +60,12 @@ int x11_init(const char *display_name) {
|
||||
g_display = XOpenDisplay(display_name);
|
||||
} else {
|
||||
const char *configured = getenv("DISPLAY");
|
||||
/* An explicit DISPLAY that fails must not silently fall back to another (physical) screen. */
|
||||
if (configured && *configured) g_display = XOpenDisplay(configured);
|
||||
if (!g_display) g_display = XOpenDisplay(":0");
|
||||
if (!g_display) g_display = XOpenDisplay(":1");
|
||||
else {
|
||||
g_display = XOpenDisplay(":0");
|
||||
if (!g_display) g_display = XOpenDisplay(":1");
|
||||
}
|
||||
}
|
||||
if (!g_display) {
|
||||
return -1;
|
||||
@@ -190,6 +195,46 @@ int x11_key_event(int keycode, int is_press) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ---- Terminal-aware paste (Mac viewer Cmd+V arrives as Ctrl+V) ------------------------------
|
||||
* Linux terminals paste with Ctrl+Shift+V; Ctrl+V quotes the next key (a paste then shows as
|
||||
* "^[[200~..."). Reports whether the focused window is a terminal emulator. Uses its own
|
||||
* connection so it never races the capture/input connection. */
|
||||
static Display *g_probe = NULL;
|
||||
|
||||
static int class_is_terminal(const char *value) {
|
||||
static const char *names[] = { "gnome-terminal", "gnome-terminal-server", "org.gnome.Console", "kgx", "konsole",
|
||||
"xfce4-terminal", "mate-terminal", "lxterminal", "qterminal", "terminator", "tilix", "xterm", "uxterm",
|
||||
"urxvt", "rxvt", "alacritty", "kitty", "wezterm", "wezterm-gui", "foot", "st-256color", "terminology",
|
||||
"guake", "tilda", "deepin-terminal", "ptyxis", "io.elementary.terminal", "cool-retro-term", NULL };
|
||||
if (!value) return 0;
|
||||
for (int i = 0; names[i]; i++) if (strcasecmp(value, names[i]) == 0) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Returns 1 when the focused window belongs to a terminal emulator, else 0. */
|
||||
int x11_terminal_shortcut_state(void) {
|
||||
if (!g_probe && !(g_probe = XOpenDisplay(NULL))) return 0;
|
||||
Window focus = 0; int revert = 0;
|
||||
XGetInputFocus(g_probe, &focus, &revert);
|
||||
if (focus == None || focus == PointerRoot) return 0;
|
||||
Window cur = focus;
|
||||
for (int depth = 0; depth < 12 && cur; depth++) {
|
||||
XClassHint hint = {0};
|
||||
if (XGetClassHint(g_probe, cur, &hint)) {
|
||||
int terminal = class_is_terminal(hint.res_name) || class_is_terminal(hint.res_class);
|
||||
if (hint.res_name) XFree(hint.res_name);
|
||||
if (hint.res_class) XFree(hint.res_class);
|
||||
return terminal;
|
||||
}
|
||||
Window root, parent, *children = NULL; unsigned int n = 0;
|
||||
if (!XQueryTree(g_probe, cur, &root, &parent, &children, &n)) return 0;
|
||||
if (children) XFree(children);
|
||||
if (parent == root) return 0;
|
||||
cur = parent;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void x11_close() {
|
||||
if (!g_display) return;
|
||||
if (g_use_shm) {
|
||||
|
||||
Executable
+40
@@ -0,0 +1,40 @@
|
||||
#!/bin/sh
|
||||
# Virtual desktop for a monitorless Linux host: Xvfb + a desktop session on its own D-Bus session bus.
|
||||
# uuremote-server then serves it with DISPLAY=$UU_VIRTUAL_DISPLAY (see README in this folder).
|
||||
# UU_VIRTUAL_DISPLAY X display number (default :99)
|
||||
# UU_VIRTUAL_SIZE screen size (default 1920x1080)
|
||||
# UU_VIRTUAL_SESSION session command (default: first of startxfce4, mate-session, startlxqt, openbox-session, xterm)
|
||||
# UU_VIRTUAL_BUS user (default): the systemd user bus (`loginctl enable-linger`) gets DISPLAY, so
|
||||
# D-Bus/systemd-activated apps (GNOME terminal, portals, ...) open on the virtual screen;
|
||||
# private: own dbus-run-session bus, for machines that also run a real desktop session
|
||||
set -eu
|
||||
display="${UU_VIRTUAL_DISPLAY:-:99}"
|
||||
size="${UU_VIRTUAL_SIZE:-1920x1080}"
|
||||
session="${UU_VIRTUAL_SESSION:-}"
|
||||
if [ -z "$session" ]; then
|
||||
for candidate in startxfce4 mate-session startlxqt openbox-session xterm; do
|
||||
if command -v "$candidate" >/dev/null 2>&1; then session="$candidate"; break; fi
|
||||
done
|
||||
fi
|
||||
[ -n "$session" ] || { echo "no desktop session found; set UU_VIRTUAL_SESSION" >&2; exit 1; }
|
||||
command -v Xvfb >/dev/null || { echo "Xvfb not installed (apt install xvfb)" >&2; exit 1; }
|
||||
command -v dbus-run-session >/dev/null || { echo "dbus-run-session not installed (apt install dbus)" >&2; exit 1; }
|
||||
|
||||
Xvfb "$display" -screen 0 "${size}x24" -nolisten tcp -noreset &
|
||||
xvfb=$!
|
||||
trap 'kill "$xvfb" 2>/dev/null' EXIT INT TERM
|
||||
socket="/tmp/.X11-unix/X${display#:}"
|
||||
for _ in $(seq 50); do [ -S "$socket" ] && break; sleep 0.1; done
|
||||
[ -S "$socket" ] || { echo "Xvfb did not start on $display" >&2; exit 1; }
|
||||
|
||||
export DISPLAY="$display"
|
||||
unset WAYLAND_DISPLAY XAUTHORITY
|
||||
export XDG_SESSION_TYPE=x11
|
||||
if [ "${UU_VIRTUAL_BUS:-user}" = user ]; then
|
||||
# Activated services inherit the user manager's environment, not this process's: export DISPLAY there.
|
||||
dbus-update-activation-environment --systemd DISPLAY XDG_SESSION_TYPE
|
||||
exec sh -c "$session"
|
||||
else
|
||||
# Private bus: independent of the user manager; activation through systemd may still escape to the user bus.
|
||||
dbus-run-session -- sh -c "$session"
|
||||
fi
|
||||
@@ -0,0 +1,14 @@
|
||||
[Unit]
|
||||
Description=Virtual X desktop for uu-remote-linux (Xvfb + session bus)
|
||||
After=dbus.socket
|
||||
|
||||
[Service]
|
||||
# Environment=UU_VIRTUAL_SESSION=startxfce4
|
||||
# Environment=UU_VIRTUAL_SIZE=1920x1080
|
||||
# Environment=UU_VIRTUAL_BUS=private
|
||||
ExecStart=%h/.local/bin/uu-virtual-desktop
|
||||
Restart=always
|
||||
RestartSec=5
|
||||
|
||||
[Install]
|
||||
WantedBy=default.target
|
||||
@@ -0,0 +1,10 @@
|
||||
# ~/.config/systemd/user/uuremote-native.service.d/virtual-display.conf
|
||||
# Serve the virtual desktop instead of a physical display.
|
||||
[Unit]
|
||||
Requires=uu-virtual-desktop.service
|
||||
After=uu-virtual-desktop.service
|
||||
|
||||
[Service]
|
||||
Environment=DISPLAY=:99
|
||||
Environment=UU_DESKTOP_BACKEND=x11
|
||||
UnsetEnvironment=XAUTHORITY WAYLAND_DISPLAY
|
||||
@@ -0,0 +1,28 @@
|
||||
无头服务器、虚拟桌面、uinput 输入和终端快捷键。/ Headless servers, virtual desktop, uinput input and terminal shortcuts.
|
||||
|
||||
### 新增 / Added
|
||||
- **无头模式** `UU_DESKTOP_BACKEND=none`:没有显示器、没有图形会话也能上线,提供终端、文件传输和端口映射。
|
||||
配合 `loginctl enable-linger` 可以开机自动上线。已在实机上用官方 Mac 客户端验收(端口映射传输 10 MB 校验一致)。
|
||||
Headless mode: online without a display; terminal, file transfer and port mapping verified with the official Mac viewer.
|
||||
- **虚拟桌面** `contrib/virtual-desktop/`:用 Xvfb 创建虚拟屏幕并启动桌面会话,没有显示器的服务器也能使用完整的远程桌面。
|
||||
实机验收了画面和键鼠;默认的「用户总线」模式(把 `DISPLAY` 导入 systemd 用户会话)尚未在纯无头机器上验证。
|
||||
Virtual desktop scripts (Xvfb + session). Video and input verified; the default user-bus mode is not yet verified on a
|
||||
truly monitorless machine.
|
||||
- **uinput 虚拟键鼠**(参考 Sunshine):Wayland/portal 下,只要当前用户能访问 `/dev/uinput`(README 里有一条 udev 规则),
|
||||
就自动改用内核级虚拟键盘和鼠标,发送物理按键码,不依赖键盘布局,也不需要 portal 输入授权。应能解决 #3(下划线被吞)。
|
||||
`UU_INPUT_BACKEND=auto|uinput|native`;X11 继续默认用 XTest。
|
||||
uinput keyboard/mouse on Wayland (layout independent, no portal input consent); should fix #3.
|
||||
- **终端快捷键**:Mac 控制端的 Cmd 和 Ctrl 到被控端都是 Ctrl。焦点在终端窗口时,Ctrl+V / Cmd+V 自动转成 Ctrl+Shift+V 粘贴;
|
||||
Ctrl+C 始终是中断,复制用 Cmd+Shift+C(X11,`UU_TERMINAL_SHORTCUTS=0` 关闭)。
|
||||
Terminal-aware paste: Ctrl+V/Cmd+V paste in terminal windows, Ctrl+C always interrupts.
|
||||
|
||||
### 修复 / Fixed
|
||||
- 没有桌面时(无头模式,或 portal 后端还没授权时)打开端口映射会被控制端立即断开:现在工具连接会收到屏幕信息和「允许」权限。
|
||||
Port mapping was aborted by the viewer when no desktop was available.
|
||||
- 明确设置的 `DISPLAY` 打不开时,不再悄悄改用 `:0`/`:1`(会截到真实屏幕),而是报错。
|
||||
An explicit DISPLAY that cannot be opened no longer falls back to the physical screen.
|
||||
|
||||
### 下载 / Download
|
||||
`uu-remote-linux-v0.1.6-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/virtual-desktop/`。
|
||||
|
||||
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||
@@ -2,6 +2,10 @@
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum DesktopBackend { X11, Wayland, Portal }
|
||||
|
||||
/// `UU_DESKTOP_BACKEND=none`: headless host — no display is opened; terminal, file transfer and
|
||||
/// port mapping stay available (servers without a monitor or logged-in session).
|
||||
pub fn headless() -> bool { std::env::var("UU_DESKTOP_BACKEND").is_ok_and(|v| v == "none") }
|
||||
|
||||
pub fn detect() -> Result<DesktopBackend, String> {
|
||||
let backend=select(std::env::var("UU_DESKTOP_BACKEND").ok().as_deref(),
|
||||
std::env::var("XDG_SESSION_TYPE").ok().as_deref(),
|
||||
@@ -16,12 +20,13 @@ fn select(explicit: Option<&str>, session: Option<&str>, socket: Option<&str>) -
|
||||
Some("x11") => Ok(DesktopBackend::X11),
|
||||
Some("wayland") => Ok(DesktopBackend::Wayland),
|
||||
Some("portal") => Ok(DesktopBackend::Portal),
|
||||
Some("none") => Ok(DesktopBackend::X11), // never opened, see headless()
|
||||
None | Some("auto") => {
|
||||
if session == Some("wayland") || socket.is_some_and(|s| !s.is_empty()) {
|
||||
Ok(DesktopBackend::Wayland)
|
||||
} else { Ok(DesktopBackend::X11) }
|
||||
},
|
||||
Some(_) => Err("UU_DESKTOP_BACKEND must be auto, x11, wayland or portal".into()),
|
||||
Some(_) => Err("UU_DESKTOP_BACKEND must be auto, x11, wayland, portal or none".into()),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+105
-2
@@ -8,7 +8,10 @@ extern "C" {
|
||||
fn x11_pointer_state(x:*mut libc::c_int,y:*mut libc::c_int,mask:*mut libc::c_uint)->libc::c_int;
|
||||
fn x11_key_symbol(symbol: libc::c_ulong, is_press: libc::c_int) -> libc::c_int;
|
||||
fn x11_key_event(keycode: libc::c_int, is_press: libc::c_int) -> libc::c_int;
|
||||
fn x11_terminal_shortcut_state() -> libc::c_int;
|
||||
}
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
unsafe fn x11_terminal_shortcut_state() -> libc::c_int { 0 }
|
||||
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
unsafe fn x11_mouse_move(_: libc::c_int, _: libc::c_int) -> libc::c_int { 0 }
|
||||
@@ -33,11 +36,22 @@ struct PendingPointer {
|
||||
unsafe fn x11_pointer_state(_: *mut libc::c_int,_:*mut libc::c_int,_:*mut libc::c_uint)->libc::c_int {-1}
|
||||
#[derive(Default)]
|
||||
struct HeldInput { buttons: std::collections::BTreeSet<i32>, keys: std::collections::BTreeSet<u64> }
|
||||
pub struct InputInjector { portal_failures: std::sync::atomic::AtomicUsize, portal_outcomes: [std::sync::atomic::AtomicUsize; 3], portal:std::sync::Mutex<Option<std::sync::Arc<crate::portal::PortalSession>>>, backend: crate::desktop_backend::DesktopBackend, pending: std::sync::Mutex<PendingPointer>, held: std::sync::Mutex<HeldInput> }
|
||||
/// Held modifier VKs (bit 0 Ctrl, 1 Shift, 2 Alt, 3 Win) and keys that got a synthetic Shift.
|
||||
#[derive(Default)]
|
||||
struct Shortcuts { modifiers: u8, shifted: std::collections::BTreeSet<i32> }
|
||||
|
||||
pub struct InputInjector { shortcuts: std::sync::Mutex<Shortcuts>, uinput: Option<crate::uinput::Devices>, screen: std::sync::Mutex<(u32,u32)>, portal_failures: std::sync::atomic::AtomicUsize, portal_outcomes: [std::sync::atomic::AtomicUsize; 3], portal:std::sync::Mutex<Option<std::sync::Arc<crate::portal::PortalSession>>>, backend: crate::desktop_backend::DesktopBackend, pending: std::sync::Mutex<PendingPointer>, held: std::sync::Mutex<HeldInput> }
|
||||
|
||||
impl InputInjector {
|
||||
pub fn new() -> Self {
|
||||
Self { portal_failures: std::sync::atomic::AtomicUsize::new(0), portal_outcomes: std::array::from_fn(|_| std::sync::atomic::AtomicUsize::new(0)), portal:std::sync::Mutex::new(None), backend:crate::desktop_backend::detect().unwrap_or(crate::desktop_backend::DesktopBackend::X11), pending: std::sync::Mutex::new(PendingPointer::default()), held:std::sync::Mutex::new(HeldInput::default()) }
|
||||
let backend = crate::desktop_backend::detect().unwrap_or(crate::desktop_backend::DesktopBackend::X11);
|
||||
let uinput = if crate::uinput::wanted(backend) {
|
||||
match crate::uinput::Devices::open() {
|
||||
Ok(devices) => { log::info!("Input backend: uinput (kernel virtual keyboard/pointer)"); Some(devices) }
|
||||
Err(e) => { log::warn!("uinput unavailable ({}); using the desktop input path. See README for the udev rule.", e.kind()); None }
|
||||
}
|
||||
} else { None };
|
||||
Self { shortcuts: std::sync::Mutex::new(Shortcuts::default()), uinput, screen: std::sync::Mutex::new((0,0)), portal_failures: std::sync::atomic::AtomicUsize::new(0), portal_outcomes: std::array::from_fn(|_| std::sync::atomic::AtomicUsize::new(0)), portal:std::sync::Mutex::new(None), backend:crate::desktop_backend::detect().unwrap_or(crate::desktop_backend::DesktopBackend::X11), pending: std::sync::Mutex::new(PendingPointer::default()), held:std::sync::Mutex::new(HeldInput::default()) }
|
||||
}
|
||||
|
||||
pub fn bind_portal(&self,session:Option<std::sync::Arc<crate::portal::PortalSession>>) {
|
||||
@@ -49,6 +63,7 @@ impl InputInjector {
|
||||
}
|
||||
|
||||
pub fn mouse_absolute_normalized(&self, x: f64, y: f64, size: (u32,u32)) {
|
||||
*self.screen.lock().unwrap() = size;
|
||||
if let Some(position) = crate::input_mapping::normalized_pointer(x,y,size.0,size.1) {
|
||||
let mut pending = self.pending.lock().unwrap();
|
||||
pending.absolute = Some(position);
|
||||
@@ -117,7 +132,47 @@ impl InputInjector {
|
||||
}
|
||||
}
|
||||
|
||||
/// Mac viewers send Cmd+V as Ctrl+V (verified: Cmd and Ctrl both arrive as VK_LCONTROL), which in
|
||||
/// a Linux terminal quotes the next key instead of pasting. In a focused terminal Ctrl+V becomes
|
||||
/// Ctrl+Shift+V. Ctrl+C is never translated: it must always interrupt, and copy is Cmd+Shift+C
|
||||
/// (= Ctrl+Shift+C). X11 only; `UU_TERMINAL_SHORTCUTS=0` disables.
|
||||
fn terminal_shift(&self, key: i32, is_press: bool) -> bool {
|
||||
let bit = match key { 0x11 | 0xa2 | 0xa3 => 1, 0x10 | 0xa0 | 0xa1 => 2, 0x12 | 0xa4 | 0xa5 => 4, 0x5b | 0x5c => 8, _ => 0 };
|
||||
let mut shortcuts = self.shortcuts.lock().unwrap();
|
||||
if bit != 0 {
|
||||
if is_press { shortcuts.modifiers |= bit } else { shortcuts.modifiers &= !bit }
|
||||
return false;
|
||||
}
|
||||
if !is_press { return shortcuts.shifted.remove(&key); }
|
||||
if key != 0x56 || shortcuts.modifiers != 1 { return false; }
|
||||
if std::env::var("UU_TERMINAL_SHORTCUTS").is_ok_and(|v| v == "0") || std::env::var_os("DISPLAY").is_none() { return false; }
|
||||
let shift = unsafe { x11_terminal_shortcut_state() } & 1 != 0;
|
||||
if shift {
|
||||
shortcuts.shifted.insert(key);
|
||||
static NOTED: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
|
||||
if NOTED.fetch_add(1, std::sync::atomic::Ordering::Relaxed) < 4 {
|
||||
log::info!("Terminal shortcut: Ctrl+V sent as Ctrl+Shift+V");
|
||||
}
|
||||
}
|
||||
shift
|
||||
}
|
||||
|
||||
pub fn windows_key_event(&self, key: i32, is_press: bool) {
|
||||
if self.terminal_shift(key, is_press) {
|
||||
const SHIFT_L: u64 = 0xffe1;
|
||||
if is_press {
|
||||
self.inject_key(SHIFT_L, true);
|
||||
self.key_symbol_event(key, true);
|
||||
} else {
|
||||
self.key_symbol_event(key, false);
|
||||
self.inject_key(SHIFT_L, false);
|
||||
}
|
||||
return;
|
||||
}
|
||||
self.key_symbol_event(key, is_press);
|
||||
}
|
||||
|
||||
fn key_symbol_event(&self, key: i32, is_press: bool) {
|
||||
if let Some(symbol) = crate::input_mapping::windows_vk_to_keysym(key) {
|
||||
let result = self.inject_key(symbol,is_press);
|
||||
static AUDIT: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
|
||||
@@ -158,19 +213,44 @@ impl InputInjector {
|
||||
}
|
||||
}
|
||||
}
|
||||
fn uinput_result(result: std::io::Result<()>) -> i32 {
|
||||
match result {
|
||||
Ok(()) => 0,
|
||||
Err(e) => {
|
||||
static FAILURES: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
|
||||
if FAILURES.fetch_add(1, std::sync::atomic::Ordering::Relaxed) < 8 { log::warn!("uinput injection failed: {}", e.kind()); }
|
||||
-1
|
||||
}
|
||||
}
|
||||
}
|
||||
fn inject_move(&self,x:i32,y:i32,relative:bool)->i32 {
|
||||
if let Some(uinput)=&self.uinput {
|
||||
if relative { return Self::uinput_result(uinput.move_relative(x,y)); }
|
||||
let (w,h)=*self.screen.lock().unwrap();
|
||||
if w > 1 && h > 1 {
|
||||
let scale = |v: i32, extent: u32| (i64::from(v) * i64::from(crate::uinput::ABS_MAX) / i64::from(extent - 1)) as i32;
|
||||
return Self::uinput_result(uinput.move_absolute(scale(x,w),scale(y,h)));
|
||||
}
|
||||
}
|
||||
if let Some(portal)=self.portal.lock().unwrap().as_ref() { return self.portal_result("motion", portal.move_pointer(f64::from(x),f64::from(y),relative)); }
|
||||
if self.backend==crate::desktop_backend::DesktopBackend::Portal {return self.portal_result("motion", Err("Portal input session is not bound".into()));}
|
||||
if self.backend==crate::desktop_backend::DesktopBackend::Wayland { crate::wayland::move_pointer(x,y,relative) }
|
||||
else if relative { unsafe { x11_mouse_relative(x,y) } } else { unsafe { x11_mouse_move(x,y) } }
|
||||
}
|
||||
fn inject_button(&self,button:i32,pressed:bool)->i32 {
|
||||
if let Some(uinput)=&self.uinput {
|
||||
if let Some((v,h))=crate::uinput::wheel_step(button) { return if pressed { Self::uinput_result(uinput.wheel(v,h)) } else { 0 }; }
|
||||
if let Some(code)=crate::uinput::button_code(button) { return Self::uinput_result(uinput.button(code,pressed)); }
|
||||
}
|
||||
if let Some(portal)=self.portal.lock().unwrap().as_ref() { return self.portal_result("button", portal.button(button,pressed)); }
|
||||
if self.backend==crate::desktop_backend::DesktopBackend::Portal {return self.portal_result("button", Err("Portal input session is not bound".into()));}
|
||||
if self.backend==crate::desktop_backend::DesktopBackend::Wayland { crate::wayland::button(button,pressed) }
|
||||
else { unsafe { x11_mouse_button(button,i32::from(pressed)) } }
|
||||
}
|
||||
fn inject_key(&self,symbol:u64,pressed:bool)->i32 {
|
||||
if let Some(uinput)=&self.uinput {
|
||||
if let Some(code)=crate::uinput::keysym_to_evdev(symbol) { return Self::uinput_result(uinput.key(code,pressed)); }
|
||||
}
|
||||
if let Some(portal)=self.portal.lock().unwrap().as_ref() { return self.portal_result("keyboard", portal.key(symbol,pressed)); }
|
||||
if self.backend==crate::desktop_backend::DesktopBackend::Portal {return self.portal_result("keyboard", Err("Portal input session is not bound".into()));}
|
||||
if self.backend==crate::desktop_backend::DesktopBackend::Wayland { crate::wayland::key(symbol,pressed) }
|
||||
@@ -213,6 +293,29 @@ mod motion_tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod shortcut_tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn modifiers_are_tracked_and_non_terminal_keys_pass_through() {
|
||||
let input = InputInjector::new();
|
||||
assert!(!input.terminal_shift(0x11, true));
|
||||
assert_eq!(input.shortcuts.lock().unwrap().modifiers, 1);
|
||||
// No X display in tests: Ctrl+V is not translated.
|
||||
assert!(!input.terminal_shift(0x56, true));
|
||||
assert!(!input.terminal_shift(0x56, false));
|
||||
assert!(!input.terminal_shift(0x11, false));
|
||||
assert_eq!(input.shortcuts.lock().unwrap().modifiers, 0);
|
||||
// A translated key keeps its synthetic Shift until its own release.
|
||||
input.shortcuts.lock().unwrap().shifted.insert(0x56);
|
||||
assert!(input.terminal_shift(0x56, false));
|
||||
assert!(!input.terminal_shift(0x56, false));
|
||||
// Ctrl+C is never translated.
|
||||
input.terminal_shift(0x11, true);
|
||||
assert!(!input.terminal_shift(0x43, true));
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod scroll_tests {
|
||||
use super::*;
|
||||
|
||||
+8
-1
@@ -14,6 +14,7 @@ mod session_lock;
|
||||
mod capture;
|
||||
mod file_trans;
|
||||
mod input;
|
||||
mod uinput;
|
||||
mod input_mapping;
|
||||
mod port_mapping;
|
||||
mod power;
|
||||
@@ -204,7 +205,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
}
|
||||
log::info!("Starting uu-remote-linux host v{}...", env!("CARGO_PKG_VERSION"));
|
||||
|
||||
let mut capture = Arc::new((if desktop_backend::detect()?==desktop_backend::DesktopBackend::Portal
|
||||
let mut capture = Arc::new((if desktop_backend::headless() {
|
||||
Err("headless mode (UU_DESKTOP_BACKEND=none)".to_owned())
|
||||
} else if desktop_backend::detect()?==desktop_backend::DesktopBackend::Portal
|
||||
&& std::env::var("UU_PORTAL_PREAUTHORIZE").ok().as_deref()!=Some("1") {
|
||||
Err("Portal screen access requires a desktop connection and local consent".to_owned())
|
||||
} else { ScreenCapture::new() }).unwrap_or_else(|error| {
|
||||
@@ -358,6 +361,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
}
|
||||
let stale_portal=capture.portal.as_ref().is_some_and(|session|!session.is_alive())
|
||||
&& crate::session_lock::locked_now() == Some(false);
|
||||
if desktop && desktop_backend::headless() {
|
||||
log::info!("Desktop connection ignored: headless mode (terminal, file transfer and port mapping remain available)");
|
||||
continue;
|
||||
}
|
||||
let session_capture=if desktop && (capture.init_error.is_some() || stale_portal) {
|
||||
if stale_portal {
|
||||
input.release_held();input.bind_portal(None);
|
||||
|
||||
+247
@@ -0,0 +1,247 @@
|
||||
//! Kernel-level virtual keyboard and pointer through /dev/uinput (as Sunshine does).
|
||||
//!
|
||||
//! Works the same under X11, every Wayland compositor and the greeter/lock screen, needs no
|
||||
//! portal permission and sends physical keycodes, so the compositor's keysym → key lookup
|
||||
//! (which can pick an AltGr level for `minus` and drop `_`, issue #3) is not involved.
|
||||
//! Access: a udev rule `KERNEL=="uinput", SUBSYSTEM=="misc", OPTIONS+="static_node=uinput",
|
||||
//! TAG+="uaccess"` grants the logged-in seat user access (see README).
|
||||
//!
|
||||
//! Devices: one keyboard + relative mouse, one absolute pointer (ABS_X/ABS_Y 0..=65535 with
|
||||
//! buttons and wheels, like a QEMU tablet). Mixing relative and absolute axes in one device
|
||||
//! confuses libinput.
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
mod imp {
|
||||
use std::fs::{File, OpenOptions};
|
||||
use std::io::Write;
|
||||
use std::os::unix::fs::OpenOptionsExt;
|
||||
use std::os::unix::io::AsRawFd;
|
||||
use std::sync::Mutex;
|
||||
|
||||
const EV_SYN: u16 = 0x00;
|
||||
const EV_KEY: u16 = 0x01;
|
||||
const EV_REL: u16 = 0x02;
|
||||
const EV_ABS: u16 = 0x03;
|
||||
const SYN_REPORT: u16 = 0;
|
||||
const REL_X: u16 = 0x00;
|
||||
const REL_Y: u16 = 0x01;
|
||||
const REL_HWHEEL: u16 = 0x06;
|
||||
const REL_WHEEL: u16 = 0x08;
|
||||
const ABS_X: u16 = 0x00;
|
||||
const ABS_Y: u16 = 0x01;
|
||||
pub const BTN_LEFT: u16 = 0x110;
|
||||
pub const BTN_RIGHT: u16 = 0x111;
|
||||
pub const BTN_MIDDLE: u16 = 0x112;
|
||||
pub const BTN_SIDE: u16 = 0x113;
|
||||
pub const BTN_EXTRA: u16 = 0x114;
|
||||
pub const ABS_MAX: i32 = 65535;
|
||||
|
||||
const UI_SET_EVBIT: libc::c_ulong = 0x4004_5564;
|
||||
const UI_SET_KEYBIT: libc::c_ulong = 0x4004_5565;
|
||||
const UI_SET_RELBIT: libc::c_ulong = 0x4004_5566;
|
||||
const UI_SET_ABSBIT: libc::c_ulong = 0x4004_5567;
|
||||
const UI_DEV_CREATE: libc::c_ulong = 0x5501;
|
||||
const UI_DEV_DESTROY: libc::c_ulong = 0x5502;
|
||||
const UI_DEV_SETUP: libc::c_ulong = 0x405c_5503;
|
||||
const UI_ABS_SETUP: libc::c_ulong = 0x401c_5504;
|
||||
|
||||
#[repr(C)]
|
||||
struct InputId { bustype: u16, vendor: u16, product: u16, version: u16 }
|
||||
#[repr(C)]
|
||||
struct UinputSetup { id: InputId, name: [u8; 80], ff_effects_max: u32 }
|
||||
#[repr(C)]
|
||||
struct AbsInfo { value: i32, minimum: i32, maximum: i32, fuzz: i32, flat: i32, resolution: i32 }
|
||||
#[repr(C)]
|
||||
struct UinputAbsSetup { code: u16, _pad: u16, absinfo: AbsInfo }
|
||||
#[repr(C)]
|
||||
struct InputEvent { time: libc::timeval, kind: u16, code: u16, value: i32 }
|
||||
|
||||
struct Device(File);
|
||||
impl Drop for Device {
|
||||
fn drop(&mut self) { unsafe { libc::ioctl(self.0.as_raw_fd(), UI_DEV_DESTROY); } }
|
||||
}
|
||||
impl Device {
|
||||
fn ioctl(&self, request: libc::c_ulong, value: libc::c_ulong) -> std::io::Result<()> {
|
||||
if unsafe { libc::ioctl(self.0.as_raw_fd(), request, value) } < 0 { Err(std::io::Error::last_os_error()) } else { Ok(()) }
|
||||
}
|
||||
fn create(name: &str, product: u16, configure: impl FnOnce(&Device) -> std::io::Result<()>) -> std::io::Result<Self> {
|
||||
let file = OpenOptions::new().write(true).custom_flags(libc::O_NONBLOCK).open("/dev/uinput")?;
|
||||
let device = Device(file);
|
||||
configure(&device)?;
|
||||
let mut setup = UinputSetup { id: InputId { bustype: 0x06 /* BUS_VIRTUAL */, vendor: 0x1d6b, product, version: 1 }, name: [0; 80], ff_effects_max: 0 };
|
||||
setup.name[..name.len().min(79)].copy_from_slice(&name.as_bytes()[..name.len().min(79)]);
|
||||
if unsafe { libc::ioctl(device.0.as_raw_fd(), UI_DEV_SETUP, &setup) } < 0 { return Err(std::io::Error::last_os_error()); }
|
||||
device.ioctl(UI_DEV_CREATE, 0)?;
|
||||
Ok(device)
|
||||
}
|
||||
fn abs(&self, code: u16) -> std::io::Result<()> {
|
||||
self.ioctl(UI_SET_ABSBIT, code.into())?;
|
||||
let setup = UinputAbsSetup { code, _pad: 0, absinfo: AbsInfo { value: 0, minimum: 0, maximum: ABS_MAX, fuzz: 0, flat: 0, resolution: 0 } };
|
||||
if unsafe { libc::ioctl(self.0.as_raw_fd(), UI_ABS_SETUP, &setup) } < 0 { return Err(std::io::Error::last_os_error()); }
|
||||
Ok(())
|
||||
}
|
||||
fn emit(&mut self, events: &[(u16, u16, i32)]) -> std::io::Result<()> {
|
||||
let zero = libc::timeval { tv_sec: 0, tv_usec: 0 };
|
||||
let mut buffer = Vec::with_capacity((events.len() + 1) * std::mem::size_of::<InputEvent>());
|
||||
for &(kind, code, value) in events.iter().chain(std::iter::once(&(EV_SYN, SYN_REPORT, 0))) {
|
||||
let event = InputEvent { time: zero, kind, code, value };
|
||||
let bytes = unsafe { std::slice::from_raw_parts((&event as *const InputEvent).cast::<u8>(), std::mem::size_of::<InputEvent>()) };
|
||||
buffer.extend_from_slice(bytes);
|
||||
}
|
||||
self.0.write_all(&buffer)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Devices { keyboard: Mutex<Device>, pointer: Mutex<Device> }
|
||||
|
||||
impl Devices {
|
||||
pub fn open() -> std::io::Result<Self> {
|
||||
let keyboard = Device::create("uu-remote-linux keyboard", 1, |d| {
|
||||
d.ioctl(UI_SET_EVBIT, EV_KEY.into())?;
|
||||
for code in 1..=248u16 { d.ioctl(UI_SET_KEYBIT, code.into())?; }
|
||||
// Relative motion (games / pointer lock) lives with the keyboard device.
|
||||
d.ioctl(UI_SET_EVBIT, EV_REL.into())?;
|
||||
d.ioctl(UI_SET_RELBIT, REL_X.into())?;
|
||||
d.ioctl(UI_SET_RELBIT, REL_Y.into())?;
|
||||
for button in [BTN_LEFT, BTN_RIGHT, BTN_MIDDLE] { d.ioctl(UI_SET_KEYBIT, button.into())?; }
|
||||
Ok(())
|
||||
})?;
|
||||
let pointer = Device::create("uu-remote-linux pointer", 2, |d| {
|
||||
d.ioctl(UI_SET_EVBIT, EV_KEY.into())?;
|
||||
for button in [BTN_LEFT, BTN_RIGHT, BTN_MIDDLE, BTN_SIDE, BTN_EXTRA] { d.ioctl(UI_SET_KEYBIT, button.into())?; }
|
||||
d.ioctl(UI_SET_EVBIT, EV_REL.into())?;
|
||||
d.ioctl(UI_SET_RELBIT, REL_WHEEL.into())?;
|
||||
d.ioctl(UI_SET_RELBIT, REL_HWHEEL.into())?;
|
||||
d.ioctl(UI_SET_EVBIT, EV_ABS.into())?;
|
||||
d.abs(ABS_X)?;
|
||||
d.abs(ABS_Y)
|
||||
})?;
|
||||
Ok(Self { keyboard: Mutex::new(keyboard), pointer: Mutex::new(pointer) })
|
||||
}
|
||||
pub fn key(&self, code: u16, pressed: bool) -> std::io::Result<()> {
|
||||
self.keyboard.lock().unwrap().emit(&[(EV_KEY, code, i32::from(pressed))])
|
||||
}
|
||||
pub fn move_absolute(&self, x: i32, y: i32) -> std::io::Result<()> {
|
||||
self.pointer.lock().unwrap().emit(&[(EV_ABS, ABS_X, x.clamp(0, ABS_MAX)), (EV_ABS, ABS_Y, y.clamp(0, ABS_MAX))])
|
||||
}
|
||||
pub fn move_relative(&self, dx: i32, dy: i32) -> std::io::Result<()> {
|
||||
self.keyboard.lock().unwrap().emit(&[(EV_REL, REL_X, dx), (EV_REL, REL_Y, dy)])
|
||||
}
|
||||
pub fn button(&self, code: u16, pressed: bool) -> std::io::Result<()> {
|
||||
self.pointer.lock().unwrap().emit(&[(EV_KEY, code, i32::from(pressed))])
|
||||
}
|
||||
pub fn wheel(&self, vertical: i32, horizontal: i32) -> std::io::Result<()> {
|
||||
let mut events = Vec::new();
|
||||
if vertical != 0 { events.push((EV_REL, REL_WHEEL, vertical)); }
|
||||
if horizontal != 0 { events.push((EV_REL, REL_HWHEEL, horizontal)); }
|
||||
if events.is_empty() { return Ok(()); }
|
||||
self.pointer.lock().unwrap().emit(&events)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
mod imp {
|
||||
pub const BTN_LEFT: u16 = 0x110;
|
||||
pub const BTN_RIGHT: u16 = 0x111;
|
||||
pub const BTN_MIDDLE: u16 = 0x112;
|
||||
pub const BTN_SIDE: u16 = 0x113;
|
||||
pub const BTN_EXTRA: u16 = 0x114;
|
||||
pub const ABS_MAX: i32 = 65535;
|
||||
pub struct Devices;
|
||||
impl Devices {
|
||||
pub fn open() -> std::io::Result<Self> { Err(std::io::Error::new(std::io::ErrorKind::Unsupported, "uinput requires Linux")) }
|
||||
pub fn key(&self, _: u16, _: bool) -> std::io::Result<()> { Ok(()) }
|
||||
pub fn move_absolute(&self, _: i32, _: i32) -> std::io::Result<()> { Ok(()) }
|
||||
pub fn move_relative(&self, _: i32, _: i32) -> std::io::Result<()> { Ok(()) }
|
||||
pub fn button(&self, _: u16, _: bool) -> std::io::Result<()> { Ok(()) }
|
||||
pub fn wheel(&self, _: i32, _: i32) -> std::io::Result<()> { Ok(()) }
|
||||
}
|
||||
}
|
||||
|
||||
pub use imp::{Devices, ABS_MAX, BTN_EXTRA, BTN_LEFT, BTN_MIDDLE, BTN_RIGHT, BTN_SIDE};
|
||||
|
||||
/// X11 button ordinal (1 left, 2 middle, 3 right, 8/9 side) → evdev button code.
|
||||
pub fn button_code(x11_button: i32) -> Option<u16> {
|
||||
match x11_button { 1 => Some(BTN_LEFT), 2 => Some(BTN_MIDDLE), 3 => Some(BTN_RIGHT), 8 => Some(BTN_SIDE), 9 => Some(BTN_EXTRA), _ => None }
|
||||
}
|
||||
|
||||
/// X11 wheel "buttons" 4/5 (vertical) and 6/7 (horizontal) → (REL_WHEEL, REL_HWHEEL) steps.
|
||||
pub fn wheel_step(x11_button: i32) -> Option<(i32, i32)> {
|
||||
match x11_button { 4 => Some((1, 0)), 5 => Some((-1, 0)), 6 => Some((0, -1)), 7 => Some((0, 1)), _ => None }
|
||||
}
|
||||
|
||||
/// Base keysym (as produced by input_mapping::windows_vk_to_keysym) → physical evdev keycode
|
||||
/// at its US-layout position. The compositor then applies the user's own layout, exactly as
|
||||
/// for a hardware keyboard.
|
||||
pub fn keysym_to_evdev(keysym: u64) -> Option<u16> {
|
||||
const LETTERS: [u16; 26] = [30, 48, 46, 32, 18, 33, 34, 35, 23, 36, 37, 38, 50, 49, 24, 25, 16, 19, 31, 20, 22, 47, 17, 45, 21, 44];
|
||||
const KEYPAD: [u16; 10] = [82, 79, 80, 81, 75, 76, 77, 71, 72, 73];
|
||||
let code = match keysym {
|
||||
0x61..=0x7a => LETTERS[(keysym - 0x61) as usize],
|
||||
0x31..=0x39 => (keysym - 0x31) as u16 + 2, // 1..9
|
||||
0x30 => 11,
|
||||
0xffb0..=0xffb9 => KEYPAD[(keysym - 0xffb0) as usize],
|
||||
0xffbe..=0xffc7 => (keysym - 0xffbe) as u16 + 59, // F1..F10
|
||||
0xffc8 => 87, 0xffc9 => 88, // F11, F12
|
||||
0xffca..=0xffd5 => (keysym - 0xffca) as u16 + 183, // F13..F24
|
||||
0xff0b => 76, // Clear: keypad 5 without NumLock, as on PC keyboards
|
||||
0xff08 => 14, 0xff09 => 15, 0xff0d => 28, 0xff1b => 1, 0x20 => 57,
|
||||
0xff13 => 119, 0xffe5 => 58, 0xff7f => 69, 0xff14 => 70, 0xff61 => 99,
|
||||
0xffe1 => 42, 0xffe2 => 54, 0xffe3 => 29, 0xffe4 => 97, 0xffe9 => 56, 0xffea => 100,
|
||||
0xffeb => 125, 0xffec => 126, 0xff67 => 127,
|
||||
0xff55 => 104, 0xff56 => 109, 0xff57 => 107, 0xff50 => 102,
|
||||
0xff51 => 105, 0xff52 => 103, 0xff53 => 106, 0xff54 => 108,
|
||||
0xff63 => 110, 0xffff => 111,
|
||||
0xffaa => 55, 0xffab => 78, 0xffac => 121, 0xffad => 74, 0xffae => 83, 0xffaf => 98, 0xff8d => 96,
|
||||
0x3b => 39, 0x3d => 13, 0x2c => 51, 0x2d => 12, 0x2e => 52, 0x2f => 53, 0x60 => 41,
|
||||
0x5b => 26, 0x5c => 43, 0x5d => 27, 0x27 => 40,
|
||||
0x1008ff12 => 113, 0x1008ff11 => 114, 0x1008ff13 => 115,
|
||||
0x1008ff17 => 163, 0x1008ff16 => 165, 0x1008ff15 => 166, 0x1008ff14 => 164,
|
||||
_ => return None,
|
||||
};
|
||||
Some(code)
|
||||
}
|
||||
|
||||
/// Input backend policy: `UU_INPUT_BACKEND=uinput` forces uinput, `native` disables it,
|
||||
/// `auto` (default) uses uinput for Wayland/portal sessions when /dev/uinput is accessible
|
||||
/// and keeps the verified XTest path on X11.
|
||||
pub fn wanted(backend: crate::desktop_backend::DesktopBackend) -> bool {
|
||||
match std::env::var("UU_INPUT_BACKEND").as_deref() {
|
||||
Ok("uinput") => true,
|
||||
Ok("native") => false,
|
||||
_ => backend != crate::desktop_backend::DesktopBackend::X11,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn maps_keysyms_to_physical_us_positions() {
|
||||
assert_eq!(keysym_to_evdev(0x61), Some(30)); // a
|
||||
assert_eq!(keysym_to_evdev(0x7a), Some(44)); // z
|
||||
assert_eq!(keysym_to_evdev(0x30), Some(11)); // 0
|
||||
assert_eq!(keysym_to_evdev(0x31), Some(2)); // 1
|
||||
assert_eq!(keysym_to_evdev(0x2d), Some(12)); // minus: Shift+minus gives '_' (#3)
|
||||
assert_eq!(keysym_to_evdev(0xffe1), Some(42)); // Shift_L
|
||||
assert_eq!(keysym_to_evdev(0xffc9), Some(88)); // F12
|
||||
assert_eq!(keysym_to_evdev(0xffb5), Some(76)); // KP_5
|
||||
assert_eq!(keysym_to_evdev(0x12345), None);
|
||||
}
|
||||
#[test]
|
||||
fn every_mapped_windows_vk_has_a_physical_key() {
|
||||
let missing: Vec<i32> = (0..=0xff).filter(|vk| {
|
||||
crate::input_mapping::windows_vk_to_keysym(*vk).is_some_and(|ks| keysym_to_evdev(ks).is_none())
|
||||
}).collect();
|
||||
assert!(missing.is_empty(), "VKs without evdev mapping: {missing:x?}");
|
||||
}
|
||||
#[test]
|
||||
fn buttons_and_wheel() {
|
||||
assert_eq!(button_code(1), Some(BTN_LEFT));
|
||||
assert_eq!(button_code(3), Some(BTN_RIGHT));
|
||||
assert_eq!(wheel_step(4), Some((1, 0)));
|
||||
assert_eq!(wheel_step(7), Some((0, 1)));
|
||||
assert_eq!(button_code(4), None);
|
||||
}
|
||||
}
|
||||
+7
-3
@@ -438,18 +438,22 @@ impl WebRtcSession {
|
||||
let next_seq = || sequence.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
|
||||
// Default file-transfer folder: the desktop user's home directory.
|
||||
let home = std::env::var("HOME").unwrap_or_else(|_| "/".into());
|
||||
let desktop_permission=if screen.init_error.is_none() {
|
||||
// Tool peers are permitted without a desktop; the port-mapping viewer aborts on Reject.
|
||||
let desktop_permission=if screen.init_error.is_none() || !desktop {
|
||||
crate::system_metrics::Permission::Allow
|
||||
} else { crate::system_metrics::Permission::Reject };
|
||||
// Without a desktop (headless/portal before consent) the port-mapping tool still waits
|
||||
// for ScreenSources/CaptureChange and aborts otherwise: tool peers get a nominal screen.
|
||||
let screen_size=if screen.init_error.is_none() { (screen.width, screen.height) } else { (1920, 1080) };
|
||||
let messages = [
|
||||
(8, crate::initialization::encode_capture_change(next_seq(), timestamp, 0, 0, "Linux desktop")),
|
||||
(13, Ok(crate::initialization::envelope(next_seq(), timestamp, 13, &init_actions.metrics_payload(desktop_sound, desktop_permission)))),
|
||||
(26, Ok(crate::initialization::envelope(next_seq(), timestamp, 26,
|
||||
&crate::system_metrics::remote_download_path(&home)))),
|
||||
(7, crate::capture_setting::screen_sources(next_seq(), timestamp, (screen.width, screen.height), init_video.borrow().output, "Linux desktop")),
|
||||
(7, crate::capture_setting::screen_sources(next_seq(), timestamp, screen_size, init_video.borrow().output, "Linux desktop")),
|
||||
];
|
||||
for (field, message) in messages {
|
||||
if screen.init_error.is_some() && matches!(field,7|8) { continue; }
|
||||
if desktop && screen.init_error.is_some() && matches!(field,7|8) { continue; }
|
||||
let message = match message {
|
||||
Ok(message) => message,
|
||||
Err(e) => { log::error!("CONTROL initialization encode failed: {}", e); return; }
|
||||
|
||||
Reference in new issue
Block a user