mirror of
https://github.com/VallierDev/uu-remote-linux.git
synced 2026-11-16 16:50:41 +01:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
46589b3fac | ||
|
|
708d9de2de | ||
|
|
ebe6a7e19a | ||
|
|
09f7169f62 | ||
|
|
9125e51350 | ||
|
|
0da79a45eb | ||
|
|
d461ef9bed | ||
|
|
f123f0ba05 | ||
|
|
90c33e173c | ||
|
|
ac0f2bf2c6 | ||
|
|
8a8a4c165f | ||
|
|
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,8 +104,11 @@ 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_VIDEO_ENCODER=cpu` | force libx264 instead of NVENC |
|
||||
| `UU_DESKTOP_BACKEND` | `auto` (default) / `x11` / `wayland` / `portal` / `kms` (experimental feature) / `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`: libx264; `vaapi`: explicit Intel/AMD; unset: NVENC→CPU |
|
||||
| `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@`) |
|
||||
| `UU_LOGIN_WINDOW=0` | don't auto-open the login window |
|
||||
@@ -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
|
||||
@@ -218,3 +224,68 @@ started a second full host server instead, so check that a subcommand exists in
|
||||
- New protocol behavior: cite the evidence (observed field numbers, official log strings) in a comment.
|
||||
- Update README.md / README.en.md when user-visible features or limitations change, and add
|
||||
`docs/release-notes/<tag>.md` before tagging a release.
|
||||
|
||||
### VAAPI (opt-in / 显式启用)
|
||||
|
||||
`UU_VIDEO_ENCODER=vaapi` selects Intel/AMD H.264 VAAPI; `UU_VAAPI_DEVICE` defaults to
|
||||
`/dev/dri/renderD128` (render-node permissions required). No automatic VAAPI selection.
|
||||
Both FFmpeg process and `--features inproc-av` upload NV12 frames to a VAAPI frame pool.
|
||||
VBR uses the viewer bitrate preset and CQ-derived minimum QP, no B-frames, async depth 1,
|
||||
AUD and the existing GOP/VFR timing. Unsupported device/driver/initialization falls back to libx264
|
||||
with a warning. Accepted on Intel UHD 770 (iHD) with the default FFmpeg process pipeline and the official Mac
|
||||
viewer (presets, fps, scaling, reconnect; no fallback, ~40% less CPU than libx264). AMD and the `inproc-av`
|
||||
VAAPI path are not hardware-verified.
|
||||
|
||||
### Virtual audio / 虚拟声卡
|
||||
|
||||
The viewer's 静音运行 switch creates the null sink `uu-remote-sink`,
|
||||
moves streams from the original default output and captures `uu-remote-sink.monitor`.
|
||||
New output goes to this sink; streams explicitly pinned to other outputs are untouched.
|
||||
The last desktop session restores the previous default output and unloads the module.
|
||||
`mute_permanent` retains the physical mute policy, but still releases the virtual sink.
|
||||
A private recovery record beside the config and a process lock recover crash leftovers on startup.
|
||||
`pactl` is used when installed (PulseAudio / PipeWire-pulse); plain PipeWire systems without it (default
|
||||
Ubuntu desktop) use `pw-cli`/`wpctl` for the same sink and WirePlumber moves playing streams. Accepted on
|
||||
Ubuntu 24.04 with the official Mac viewer. When routing is unavailable, the existing mute/monitor path remains.
|
||||
Changing the switch restarts only the scoped audio encoder; normal non-muted capture is unchanged.
|
||||
`virtual-audio-probe` temporarily routes output for five seconds and restores it (local diagnostic).
|
||||
|
||||
### Virtual desktop user bus
|
||||
|
||||
The supervisor imports DISPLAY and XAUTHORITY into the linger user bus, waits for authenticated
|
||||
X11 readiness, and keeps its cleanup trap alive. The sample drop-in uses `%t/uu-virtual-99.Xauthority`.
|
||||
Use a dedicated headless account. See the [setup and official Mac acceptance checklist](contrib/virtual-desktop/ACCEPTANCE.md).
|
||||
|
||||
### Arch Linux / Omarchy
|
||||
|
||||
```sh
|
||||
sudo pacman -S --needed base-devel rust pkgconf clang ffmpeg libx11 libxext libxtst libxfixes gtk3 \
|
||||
wayland libxkbcommon wl-clipboard libpipewire glib2 libpulse
|
||||
cargo test --locked --features wayland
|
||||
cargo build --release --features wayland
|
||||
```
|
||||
|
||||
Omarchy uses Hyprland: run in its user session, select `UU_DESKTOP_BACKEND=wayland`,
|
||||
and retain `WAYLAND_DISPLAY` / `XDG_RUNTIME_DIR`. This requires wlr-screencopy and the
|
||||
virtual input protocols supported by Hyprland; other compositors may need `--features wayland,portal`.
|
||||
For uinput, load `uinput` and apply the uaccess rule above; seat ACLs require an active login.
|
||||
For unattended accounts, explicitly restrict `/dev/uinput` access to a dedicated group.
|
||||
This permission can inject into the whole seat, including lock/greeter screens.
|
||||
`UU_INPUT_BACKEND=uinput` requests kernel devices; verify startup actually reports uinput.
|
||||
Optional VAAPI: `intel-media-driver` (modern Intel) or `libva-mesa-driver` (AMD), `libva-utils`
|
||||
for `vainfo`. Optional KMS: `libdrm`. Virtual Xfce: `xorg-server-xvfb xorg-xauth xorg-xdpyinfo
|
||||
xfce4-session xfce4-panel xfdesktop xfwm4 gnome-terminal`.
|
||||
Rolling Arch FFmpeg may be newer than supported `inproc-av` (FFmpeg 6.x); use the default process
|
||||
pipeline or build against a separate FFmpeg 6 prefix. Do not downgrade the system FFmpeg.
|
||||
User-bus headless setup and the official Mac checklist: [virtual desktop acceptance](contrib/virtual-desktop/ACCEPTANCE.md).
|
||||
|
||||
### Experimental KMS / 实验后端
|
||||
|
||||
`--features kms` plus explicit `UU_DESKTOP_BACKEND=kms` enables a limited linear-framebuffer
|
||||
prototype; `UU_KMS_DEVICE=/dev/dri/card0`. Requires libdrm headers and deliberately granted
|
||||
`cap_sys_admin+ep`, plus card-node access. It is never automatically selected or granted privileges.
|
||||
Read the [KMS limitations, permission risk and revocation steps](docs/validation/kms.md) before use.
|
||||
The VM GDM greeter capture was solid black (failed visual acceptance); lock/greeter support and Intel/AMD hardware remain unverified. Use uinput for input; clipboard
|
||||
is unavailable without a desktop session. No default release behavior changes.
|
||||
For headless desktop offers, [official error evidence remains incomplete](docs/validation/headless-error-evidence.md);
|
||||
the existing offer behavior is retained.
|
||||
Generated
+1
-1
@@ -2649,7 +2649,7 @@ checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
|
||||
|
||||
[[package]]
|
||||
name = "uu-remote-linux"
|
||||
version = "0.1.5"
|
||||
version = "0.1.7"
|
||||
dependencies = [
|
||||
"base64 0.22.1",
|
||||
"bytes",
|
||||
|
||||
+3
-1
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "uu-remote-linux"
|
||||
version = "0.1.5"
|
||||
version = "0.1.7"
|
||||
edition = "2021"
|
||||
authors = ["uu-remote-linux contributors"]
|
||||
description = "Unofficial Linux host for NetEase UU Remote: control a Linux desktop from the official clients"
|
||||
@@ -16,6 +16,8 @@ path = "src/main.rs"
|
||||
[features]
|
||||
default = []
|
||||
wayland = []
|
||||
# Experimental direct linear framebuffer capture; never enabled in release builds.
|
||||
kms = []
|
||||
portal = []
|
||||
# In-process capture and encoding through FFmpeg 6 libraries (libavcodec/libavdevice dev packages).
|
||||
# Off by default so release binaries do not link the build machine's libav.
|
||||
|
||||
+115
-4
@@ -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 xauth x11-utils dbus-x11 dbus-user-session 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,8 +200,10 @@ Common environment variables:
|
||||
| Variable | Purpose |
|
||||
|---|---|
|
||||
| `UU_CONFIG` | Config file path (default `~/.config/uuremote/config.json`) |
|
||||
| `UU_DESKTOP_BACKEND` | `auto` / `x11` / `wayland` / `portal` |
|
||||
| `UU_VIDEO_ENCODER=cpu` | Force CPU encoding |
|
||||
| `UU_DESKTOP_BACKEND` | `auto` / `x11` / `wayland` / `portal` / `kms` (experimental feature) / `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`: libx264; `vaapi`: opt-in Intel/AMD; unset: existing NVENC→CPU |
|
||||
| `UU_FFMPEG_ROOT` | Use FFmpeg from this prefix |
|
||||
| `UU_AUDIO=0` | Disable sound; `UU_AUDIO_SOURCE` picks the PulseAudio source |
|
||||
| `UU_LOGIN_WINDOW=0` | Do not open the login window automatically when credentials are missing |
|
||||
@@ -216,3 +262,68 @@ Released under the [GPL-3.0](LICENSE): you may use, modify and redistribute it,
|
||||
- The NetEase UU Remote team, for a remote tool that is a pleasure to use.
|
||||
- [webrtc-rs](https://github.com/webrtc-rs/webrtc), FFmpeg, GTK and the other open-source projects this builds on.
|
||||
- Most of the code was written with the help of Claude and Codex.
|
||||
|
||||
### VAAPI (opt-in / 显式启用)
|
||||
|
||||
`UU_VIDEO_ENCODER=vaapi` selects Intel/AMD H.264 VAAPI; `UU_VAAPI_DEVICE` defaults to
|
||||
`/dev/dri/renderD128` (render-node permissions required). No automatic VAAPI selection.
|
||||
Both FFmpeg process and `--features inproc-av` upload NV12 frames to a VAAPI frame pool.
|
||||
VBR uses the viewer bitrate preset and CQ-derived minimum QP, no B-frames, async depth 1,
|
||||
AUD and the existing GOP/VFR timing. Unsupported device/driver/initialization falls back to libx264
|
||||
with a warning. Accepted on Intel UHD 770 (iHD) with the default FFmpeg process pipeline and the official Mac
|
||||
viewer (presets, fps, scaling, reconnect; no fallback, ~40% less CPU than libx264). AMD and the `inproc-av`
|
||||
VAAPI path are not hardware-verified.
|
||||
|
||||
### Virtual audio / 虚拟声卡
|
||||
|
||||
The viewer's 静音运行 switch creates the null sink `uu-remote-sink`,
|
||||
moves streams from the original default output and captures `uu-remote-sink.monitor`.
|
||||
New output goes to this sink; streams explicitly pinned to other outputs are untouched.
|
||||
The last desktop session restores the previous default output and unloads the module.
|
||||
`mute_permanent` retains the physical mute policy, but still releases the virtual sink.
|
||||
A private recovery record beside the config and a process lock recover crash leftovers on startup.
|
||||
`pactl` is used when installed (PulseAudio / PipeWire-pulse); plain PipeWire systems without it (default
|
||||
Ubuntu desktop) use `pw-cli`/`wpctl` for the same sink and WirePlumber moves playing streams. Accepted on
|
||||
Ubuntu 24.04 with the official Mac viewer. When routing is unavailable, the existing mute/monitor path remains.
|
||||
Changing the switch restarts only the scoped audio encoder; normal non-muted capture is unchanged.
|
||||
`virtual-audio-probe` temporarily routes output for five seconds and restores it (local diagnostic).
|
||||
|
||||
### Virtual desktop user bus
|
||||
|
||||
The supervisor imports DISPLAY and XAUTHORITY into the linger user bus, waits for authenticated
|
||||
X11 readiness, and keeps its cleanup trap alive. The sample drop-in uses `%t/uu-virtual-99.Xauthority`.
|
||||
Use a dedicated headless account. See the [setup and official Mac acceptance checklist](contrib/virtual-desktop/ACCEPTANCE.md).
|
||||
|
||||
### Arch Linux / Omarchy
|
||||
|
||||
```sh
|
||||
sudo pacman -S --needed base-devel rust pkgconf clang ffmpeg libx11 libxext libxtst libxfixes gtk3 \
|
||||
wayland libxkbcommon wl-clipboard libpipewire glib2 libpulse
|
||||
cargo test --locked --features wayland
|
||||
cargo build --release --features wayland
|
||||
```
|
||||
|
||||
Omarchy uses Hyprland: run in its user session, select `UU_DESKTOP_BACKEND=wayland`,
|
||||
and retain `WAYLAND_DISPLAY` / `XDG_RUNTIME_DIR`. This requires wlr-screencopy and the
|
||||
virtual input protocols supported by Hyprland; other compositors may need `--features wayland,portal`.
|
||||
For uinput, load `uinput` and apply the uaccess rule above; seat ACLs require an active login.
|
||||
For unattended accounts, explicitly restrict `/dev/uinput` access to a dedicated group.
|
||||
This permission can inject into the whole seat, including lock/greeter screens.
|
||||
`UU_INPUT_BACKEND=uinput` requests kernel devices; verify startup actually reports uinput.
|
||||
Optional VAAPI: `intel-media-driver` (modern Intel) or `libva-mesa-driver` (AMD), `libva-utils`
|
||||
for `vainfo`. Optional KMS: `libdrm`. Virtual Xfce: `xorg-server-xvfb xorg-xauth xorg-xdpyinfo
|
||||
xfce4-session xfce4-panel xfdesktop xfwm4 gnome-terminal`.
|
||||
Rolling Arch FFmpeg may be newer than supported `inproc-av` (FFmpeg 6.x); use the default process
|
||||
pipeline or build against a separate FFmpeg 6 prefix. Do not downgrade the system FFmpeg.
|
||||
User-bus headless setup and the official Mac checklist: [virtual desktop acceptance](contrib/virtual-desktop/ACCEPTANCE.md).
|
||||
|
||||
### Experimental KMS / 实验后端
|
||||
|
||||
`--features kms` plus explicit `UU_DESKTOP_BACKEND=kms` enables a limited linear-framebuffer
|
||||
prototype; `UU_KMS_DEVICE=/dev/dri/card0`. Requires libdrm headers and deliberately granted
|
||||
`cap_sys_admin+ep`, plus card-node access. It is never automatically selected or granted privileges.
|
||||
Read the [KMS limitations, permission risk and revocation steps](docs/validation/kms.md) before use.
|
||||
The VM GDM greeter capture was solid black (failed visual acceptance); lock/greeter support and Intel/AMD hardware remain unverified. Use uinput for input; clipboard
|
||||
is unavailable without a desktop session. No default release behavior changes.
|
||||
For headless desktop offers, [official error evidence remains incomplete](docs/validation/headless-error-evidence.md);
|
||||
the existing offer behavior is retained.
|
||||
@@ -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 xauth x11-utils dbus-x11 dbus-user-session 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,8 +190,10 @@ journalctl --user -u uuremote-native.service -f # 看到 availability=Some("re
|
||||
| 变量 | 作用 |
|
||||
|---|---|
|
||||
| `UU_CONFIG` | 配置文件路径(默认 `~/.config/uuremote/config.json`) |
|
||||
| `UU_DESKTOP_BACKEND` | `auto` / `x11` / `wayland` / `portal` |
|
||||
| `UU_VIDEO_ENCODER=cpu` | 强制使用 CPU 编码 |
|
||||
| `UU_DESKTOP_BACKEND` | `auto` / `x11` / `wayland` / `portal` / `kms`(实验性特性) / `none`(无头模式) |
|
||||
| `UU_INPUT_BACKEND` | `auto`(默认,Wayland 下可用时用 uinput)/ `uinput` / `native` |
|
||||
| `UU_TERMINAL_SHORTCUTS=0` | 关闭终端窗口里的 Ctrl+V 粘贴转换 |
|
||||
| `UU_VIDEO_ENCODER` | `cpu` 强制 CPU;`vaapi` 显式启用 Intel/AMD 硬编;未设置时保留 NVENC→CPU |
|
||||
| `UU_FFMPEG_ROOT` | 使用指定目录下的 FFmpeg |
|
||||
| `UU_AUDIO=0` | 关闭声音转发;`UU_AUDIO_SOURCE` 指定 PulseAudio 采集源 |
|
||||
| `UU_LOGIN_WINDOW=0` | 凭证缺失时不自动弹出登录窗口 |
|
||||
@@ -201,3 +244,58 @@ flowchart LR
|
||||
- 网易 UU 远程团队:做了一个好用的远程工具。
|
||||
- [webrtc-rs](https://github.com/webrtc-rs/webrtc)、FFmpeg、GTK 等开源项目。
|
||||
- 本项目的大部分代码在 Claude 和 Codex 的协助下完成。
|
||||
|
||||
### VAAPI 硬件编码(显式启用)
|
||||
|
||||
设置 `UU_VIDEO_ENCODER=vaapi` 使用 Intel/AMD H.264 VAAPI;`UU_VAAPI_DEVICE` 默认
|
||||
`/dev/dri/renderD128`,运行用户需要 render 节点权限。不会自动选择 VAAPI;设备或驱动初始化
|
||||
失败会明确警告并回退 libx264。外部 FFmpeg 和 `--features inproc-av` 均先转 NV12 再上传硬件帧。
|
||||
VBR 使用控制端码率预设及 CQ 对应的最小 QP,无 B 帧、async depth=1,并保留现有 GOP/VFR 规则。
|
||||
Intel 核显(UHD 770,iHD 驱动)已实机验收:默认的外部 FFmpeg 管线用官方 Mac 控制端测试了各档画质、帧率、缩放和重连,全程硬件编码、无回退,CPU 占用比 libx264 低约 40%。**AMD 显卡和 `--features inproc-av` 管线的 VAAPI 尚未实机验证。**
|
||||
|
||||
### 静音运行与虚拟声卡
|
||||
|
||||
控制端开启「静音运行」后,程序创建虚拟输出 `uu-remote-sink`,保存原默认输出,
|
||||
迁移其播放流并采集 `uu-remote-sink.monitor`。新播放也走虚拟输出;其他输出上的流不迁移。
|
||||
最后一个桌面连接结束后恢复原默认输出并卸载模块;`mute_permanent` 仍保留原物理静音策略。
|
||||
恢复记录保存在配置旁,权限 0600;进程锁避免重复实例抢占,异常退出后下次启动会恢复遗留路由。
|
||||
有 `pactl` 时用它(PulseAudio / PipeWire-pulse);没有 `pactl` 的纯 PipeWire 系统(如默认的 Ubuntu 桌面)
|
||||
自动改用 `pw-cli`/`wpctl` 创建同名虚拟输出,正在播放的声音由 WirePlumber 自动迁移。已在 Ubuntu 24.04 实机上用官方 Mac 控制端验收。
|
||||
路由失败时保留原静音/monitor 方案。开关变化仅重启当前音频编码器,未开启时采集方式保持不变。
|
||||
本地诊断命令 `virtual-audio-probe` 会临时切换输出五秒并恢复。
|
||||
|
||||
### 虚拟桌面的用户总线
|
||||
|
||||
默认 `UU_VIRTUAL_BUS=user`:监督脚本将 DISPLAY/XAUTHORITY 导入 linger 用户总线,等待经过认证的
|
||||
X11 就绪后通知 systemd;退出时清理 Xvfb。示例主机 drop-in 使用 `%t/uu-virtual-99.Xauthority`。
|
||||
请使用没有真实图形会话的专用账号;参见[设置与官方 Mac 验收清单](contrib/virtual-desktop/ACCEPTANCE.md)。
|
||||
|
||||
### Arch Linux / Omarchy
|
||||
|
||||
```sh
|
||||
sudo pacman -S --needed base-devel rust pkgconf clang ffmpeg libx11 libxext libxtst libxfixes gtk3 \
|
||||
wayland libxkbcommon wl-clipboard libpipewire glib2 libpulse
|
||||
cargo test --locked --features wayland
|
||||
cargo build --release --features wayland
|
||||
```
|
||||
|
||||
Omarchy 使用 Hyprland:在其用户会话中运行,设置 `UU_DESKTOP_BACKEND=wayland`,保留
|
||||
`WAYLAND_DISPLAY` 和 `XDG_RUNTIME_DIR`。其他合成器可能需要 `--features wayland,portal`。
|
||||
uinput 需加载内核模块并应用上文 uaccess 规则;无人登录账号需要专用组的受限授权。
|
||||
该权限能向整个 seat(包括锁屏/登录界面)注入输入。设置 `UU_INPUT_BACKEND=uinput` 后,
|
||||
仍须确认启动日志实际选择了 uinput。
|
||||
|
||||
VAAPI 可选包:Intel 新显卡用 `intel-media-driver`,AMD 用 `libva-mesa-driver`,`libva-utils` 提供
|
||||
`vainfo`。KMS 依赖 `libdrm`。虚拟 Xfce 依赖 `xorg-server-xvfb xorg-xauth xorg-xdpyinfo
|
||||
xfce4-session xfce4-panel xfdesktop xfwm4 gnome-terminal`。
|
||||
滚动 Arch 的 FFmpeg 可能超出 `inproc-av` 支持的 6.x;请使用默认进程方案或独立的 FFmpeg 6 前缀,
|
||||
不要降级系统 FFmpeg。当前 Arch/Hyprland VM 验证结果见[开发验证记录](docs/validation/v017-results.md)。
|
||||
|
||||
### 实验性 KMS 截屏
|
||||
|
||||
编译 `--features kms` 后显式设置 `UU_DESKTOP_BACKEND=kms`;`UU_KMS_DEVICE` 默认
|
||||
`/dev/dri/card0`。需要 libdrm、card 节点访问权及主动授予的 `cap_sys_admin+ep`。
|
||||
程序不会自动启用或授权;此 capability 权限很大,先阅读[限制、安全风险及撤销步骤](docs/validation/kms.md)。
|
||||
当前仅为线性帧缓冲原型:VM 的 GDM 截图仍是黑屏,**没有通过登录界面视觉验收**,不承诺锁屏可用。
|
||||
键鼠需 uinput;没有桌面会话时不提供剪贴板。
|
||||
无头桌面错误码的[官方证据仍不足](docs/validation/headless-error-evidence.md),因此保持原有 offer 行为。
|
||||
@@ -9,6 +9,16 @@ fn main() {
|
||||
|
||||
let target_os = std::env::var("CARGO_CFG_TARGET_OS").unwrap_or_default();
|
||||
if target_os == "linux" {
|
||||
if std::env::var_os("CARGO_FEATURE_KMS").is_some() {
|
||||
println!("cargo:rerun-if-changed=c_engine/kms_engine.c");
|
||||
let output=std::process::Command::new("pkg-config").args(["--cflags","libdrm"]).output().expect("KMS requires pkg-config and libdrm development headers");
|
||||
assert!(output.status.success(), "KMS requires libdrm development headers");
|
||||
let mut build=cc::Build::new();
|
||||
build.file("c_engine/kms_engine.c");
|
||||
for flag in String::from_utf8_lossy(&output.stdout).split_whitespace() { build.flag(flag); }
|
||||
build.compile("uu_kms_engine");
|
||||
println!("cargo:rustc-link-lib=drm");
|
||||
}
|
||||
cc::Build::new()
|
||||
.file("c_engine/x11_engine.c")
|
||||
.file("c_engine/clipboard.c")
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
/* Opt-in KMS prototype: read active primary-plane linear XRGB/ARGB buffers.
|
||||
* No modesetting, DRM master acquisition, authentication or content logging.
|
||||
*/
|
||||
#include <xf86drm.h>
|
||||
#include <xf86drmMode.h>
|
||||
#include <drm_fourcc.h>
|
||||
#include <sys/mman.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
struct uu_kms { int fd; uint32_t crtc, plane, width, height; };
|
||||
static uint32_t primary_plane(int fd, uint32_t crtc_id) {
|
||||
drmModePlaneRes *planes=drmModeGetPlaneResources(fd);
|
||||
if (!planes) return 0;
|
||||
uint32_t selected=0;
|
||||
for (uint32_t i=0;i<planes->count_planes && !selected;i++) {
|
||||
drmModePlane *plane=drmModeGetPlane(fd,planes->planes[i]);
|
||||
if (plane && plane->crtc_id==crtc_id && plane->fb_id) {
|
||||
drmModeObjectProperties *props=drmModeObjectGetProperties(fd,plane->plane_id,DRM_MODE_OBJECT_PLANE);
|
||||
if (props) {
|
||||
for (uint32_t j=0;j<props->count_props;j++) {
|
||||
drmModePropertyRes *prop=drmModeGetProperty(fd,props->props[j]);
|
||||
if (prop && strcmp(prop->name,"type")==0 && props->prop_values[j]==DRM_PLANE_TYPE_PRIMARY) selected=plane->plane_id;
|
||||
if (prop) drmModeFreeProperty(prop);
|
||||
}
|
||||
drmModeFreeObjectProperties(props);
|
||||
}
|
||||
}
|
||||
if (plane) drmModeFreePlane(plane);
|
||||
}
|
||||
drmModeFreePlaneResources(planes);
|
||||
return selected;
|
||||
}
|
||||
static void close_handles(int fd, drmModeFB2 *fb) {
|
||||
for (int i=0;i<4;i++) {
|
||||
if (!fb->handles[i]) continue;
|
||||
int duplicate=0;
|
||||
for (int j=0;j<i;j++) if (fb->handles[j]==fb->handles[i]) duplicate=1;
|
||||
if (!duplicate) { struct drm_gem_close c={.handle=fb->handles[i]}; drmIoctl(fd,DRM_IOCTL_GEM_CLOSE,&c); }
|
||||
}
|
||||
}
|
||||
void uu_kms_close(struct uu_kms *s) { if (s) { close(s->fd); free(s); } }
|
||||
struct uu_kms *uu_kms_open(const char *device, uint32_t *w, uint32_t *h) {
|
||||
int fd=open(device,O_RDWR|O_CLOEXEC);
|
||||
if (fd<0) return NULL;
|
||||
if (drmSetClientCap(fd,DRM_CLIENT_CAP_UNIVERSAL_PLANES,1)<0) { close(fd); return NULL; }
|
||||
drmModeRes *res=drmModeGetResources(fd);
|
||||
if (!res) { close(fd); return NULL; }
|
||||
struct uu_kms *s=NULL;
|
||||
for (int i=0;i<res->count_crtcs;i++) {
|
||||
drmModeCrtc *crtc=drmModeGetCrtc(fd,res->crtcs[i]);
|
||||
uint32_t plane=crtc ? primary_plane(fd,crtc->crtc_id) : 0;
|
||||
if (crtc && plane && crtc->mode_valid && crtc->width && crtc->height && crtc->width<=16384 && crtc->height<=16384) {
|
||||
s=calloc(1,sizeof(*s));
|
||||
if (s) { s->fd=fd; s->crtc=crtc->crtc_id; s->plane=plane; s->width=crtc->width; s->height=crtc->height; *w=s->width; *h=s->height; }
|
||||
}
|
||||
if (crtc) drmModeFreeCrtc(crtc);
|
||||
if (s) break;
|
||||
}
|
||||
drmModeFreeResources(res);
|
||||
if (!s) close(fd);
|
||||
return s;
|
||||
}
|
||||
int uu_kms_read(struct uu_kms *s, uint8_t *out, size_t length) {
|
||||
drmModeCrtc *crtc=drmModeGetCrtc(s->fd,s->crtc);
|
||||
if (!crtc) return -1;
|
||||
drmModePlane *plane=drmModeGetPlane(s->fd,s->plane);
|
||||
uint32_t id=plane ? plane->fb_id : 0;
|
||||
if (plane) drmModeFreePlane(plane);
|
||||
int valid=crtc->mode_valid && crtc->width==s->width && crtc->height==s->height;
|
||||
drmModeFreeCrtc(crtc);
|
||||
if (!valid || !id) return -2; /* mode changed: caller must reopen */
|
||||
drmModeFB2 *fb=drmModeGetFB2(s->fd,id);
|
||||
if (!fb) return -3;
|
||||
int result=-4;
|
||||
if (!fb->handles[0]) { result=-5; goto done; } /* DRM master or CAP_SYS_ADMIN needed */
|
||||
if (fb->width!=s->width || fb->height!=s->height ||
|
||||
(fb->pixel_format!=DRM_FORMAT_XRGB8888 && fb->pixel_format!=DRM_FORMAT_ARGB8888) ||
|
||||
((fb->flags & DRM_MODE_FB_MODIFIERS) && fb->modifier!=DRM_FORMAT_MOD_LINEAR) ||
|
||||
fb->handles[1] || fb->pitches[0] < s->width*4 || length!=(size_t)s->width*s->height*4) goto done;
|
||||
struct drm_mode_map_dumb map={.handle=fb->handles[0]};
|
||||
if (drmIoctl(s->fd,DRM_IOCTL_MODE_MAP_DUMB,&map)<0) { result=-6; goto done; }
|
||||
size_t bytes=(size_t)fb->pitches[0]*fb->height+fb->offsets[0];
|
||||
void *data=mmap(NULL,bytes,PROT_READ,MAP_SHARED,s->fd,map.offset);
|
||||
if (data==MAP_FAILED) { result=-7; goto done; }
|
||||
for (uint32_t y=0;y<s->height;y++) {
|
||||
uint8_t *row=out+(size_t)y*s->width*4;
|
||||
memcpy(row,(uint8_t*)data+fb->offsets[0]+(size_t)y*fb->pitches[0],s->width*4);
|
||||
for (uint32_t x=0;x<s->width;x++) row[x*4+3]=255;
|
||||
}
|
||||
munmap(data,bytes);
|
||||
result=0;
|
||||
done:
|
||||
close_handles(s->fd,fb);
|
||||
drmModeFreeFB2(fb);
|
||||
return result;
|
||||
}
|
||||
+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) {
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
# Virtual user-bus desktop acceptance
|
||||
|
||||
Install Xvfb, xauth, xdpyinfo, Xfce and dbus-user-session (Debian) or dbus-broker (Arch).
|
||||
The default is `UU_VIRTUAL_BUS=user`; use it on a dedicated headless account with no real
|
||||
GUI session. Its D-Bus and systemd activation environment is shared by all that user's apps.
|
||||
`private` isolates ordinary D-Bus activation but cannot redirect systemd-activated programs.
|
||||
|
||||
1. Install the script in `~/.local/bin`, the unit in `~/.config/systemd/user`, and
|
||||
`virtual-display.conf` in the host service's `.service.d/` directory.
|
||||
2. `sudo loginctl enable-linger "$USER"`; set `UU_VIRTUAL_SESSION=startxfce4` in the desktop
|
||||
unit. `systemctl --user daemon-reload`; enable both units. A different display needs a
|
||||
matching `DISPLAY` and `XAUTHORITY=%t/uu-virtual-N.Xauthority` in the host drop-in.
|
||||
3. Reboot a disposable VM with graphical login disabled. Before logging into a desktop,
|
||||
check both user units are active, Xvfb is `:99`, no real GUI login session exists, and
|
||||
host availability is `ready`. The desktop unit uses notify readiness after authenticated
|
||||
X11 access succeeds, so the host does not race the X socket.
|
||||
4. `DISPLAY=:99 XAUTHORITY="$XDG_RUNTIME_DIR/uu-virtual-99.Xauthority" xdpyinfo` must work.
|
||||
`gnome-terminal` launched over the user bus must open on `:99`. Check its window via
|
||||
`xdotool search --class gnome-terminal-server`; do not log terminal text.
|
||||
5. Restart/stop the desktop unit: its Xvfb, desktop children and authority file must be
|
||||
removed. A stale pre-existing display is rejected. The TCP X11 listener is disabled.
|
||||
|
||||
## Official Mac client checklist (human acceptance)
|
||||
|
||||
- [ ] Select the isolated VM device; confirm its identity before connecting.
|
||||
- [ ] After VM boot, connect without a physical/graphical login; Xfce is visible.
|
||||
- [ ] Open GNOME Terminal through its application launcher; it appears on the same `:99` desktop.
|
||||
- [ ] Move/click/drag, scroll, type a synthetic test marker and verify key releases (no repeats).
|
||||
- [ ] Verify both clipboard directions using synthetic text; keep the remote window focused.
|
||||
- [ ] Toggle 静音运行 during playback: remote sound remains audible; VM physical output is silent.
|
||||
- [ ] Disconnect/reconnect; confirm desktop and applications remain usable.
|
||||
- [ ] End the last session: audio default/modules restore; auto-lock follows the saved setting.
|
||||
- [ ] Repeat after reboot with no GUI login. Do not enter production credentials in test fixtures.
|
||||
Executable
+89
@@ -0,0 +1,89 @@
|
||||
#!/bin/sh
|
||||
# Virtual desktop for a monitorless Linux host: Xvfb + a desktop session on the user bus (or an isolated private 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; }
|
||||
bus="${UU_VIRTUAL_BUS:-user}"
|
||||
case "$bus" in user|private) ;; *) echo "UU_VIRTUAL_BUS must be user or private" >&2; exit 1 ;; esac
|
||||
command -v xauth >/dev/null || { echo "xauth not installed" >&2; exit 1; }
|
||||
command -v xdpyinfo >/dev/null || { echo "xdpyinfo not installed" >&2; exit 1; }
|
||||
case "$display" in :[0-9]*) ;; *) echo "UU_VIRTUAL_DISPLAY must be a local display number" >&2; exit 1 ;; esac
|
||||
number="${display#:}"
|
||||
case "$number" in *[!0-9]*|'') echo "invalid display number" >&2; exit 1 ;; esac
|
||||
socket="/tmp/.X11-unix/X$number"
|
||||
# A live server owns the display; a dead one's leftovers (killed Xvfb) are cleaned up by Xvfb itself.
|
||||
lock="/tmp/.X$number-lock"
|
||||
if [ -e "$socket" ] || [ -e "$lock" ]; then
|
||||
owner=$(tr -d ' \n' < "$lock" 2>/dev/null || :)
|
||||
case "$owner" in ''|*[!0-9]*) owner= ;; esac
|
||||
if [ -z "$owner" ] || kill -0 "$owner" 2>/dev/null; then echo "display already in use" >&2; exit 1; fi
|
||||
echo "removing stale display $display left by dead process $owner" >&2
|
||||
rm -f "$lock" "$socket" 2>/dev/null || { echo "cannot remove stale display files" >&2; exit 1; }
|
||||
fi
|
||||
umask 077
|
||||
export XDG_RUNTIME_DIR="${XDG_RUNTIME_DIR:-/run/user/$(id -u)}"
|
||||
authority="$XDG_RUNTIME_DIR/uu-virtual-$number.Xauthority"
|
||||
# The display socket was checked above; replace a stale authority after a service crash.
|
||||
[ ! -L "$authority" ] || { echo "refusing a symlink authority" >&2; exit 1; }
|
||||
temporary=$(mktemp "$XDG_RUNTIME_DIR/uu-virtual-xauth.XXXXXX")
|
||||
xvfb=
|
||||
child=
|
||||
cleanup() {
|
||||
trap - EXIT INT TERM
|
||||
[ -z "$child" ] || kill "$child" 2>/dev/null || :
|
||||
[ -z "$xvfb" ] || kill "$xvfb" 2>/dev/null || :
|
||||
[ -z "$xvfb" ] || wait "$xvfb" 2>/dev/null || :
|
||||
rm -f "$authority" "$temporary"
|
||||
}
|
||||
trap cleanup EXIT
|
||||
trap 'exit 143' TERM
|
||||
trap 'exit 130' INT
|
||||
cookie=$(od -An -N16 -tx1 /dev/urandom | tr -d ' \n')
|
||||
xauth -f "$temporary" add "$display" MIT-MAGIC-COOKIE-1 "$cookie"
|
||||
unset cookie
|
||||
mv "$temporary" "$authority"
|
||||
export XAUTHORITY="$authority"
|
||||
|
||||
Xvfb "$display" -screen 0 "${size}x24" -auth "$authority" -nolisten tcp -noreset &
|
||||
xvfb=$!
|
||||
export DISPLAY="$display"
|
||||
unset WAYLAND_DISPLAY SESSION_MANAGER
|
||||
export XDG_SESSION_TYPE=x11
|
||||
ready=false
|
||||
for _ in $(seq 50); do
|
||||
kill -0 "$xvfb" 2>/dev/null || break
|
||||
if xdpyinfo >/dev/null 2>&1; then ready=true; break; fi
|
||||
sleep 0.1
|
||||
done
|
||||
[ "$ready" = true ] || { echo "Xvfb did not become ready" >&2; exit 1; }
|
||||
if [ "$bus" = user ]; then
|
||||
# Linger creates the user manager and its bus without a graphical login.
|
||||
: "${XDG_RUNTIME_DIR:=/run/user/$(id -u)}"
|
||||
export XDG_RUNTIME_DIR
|
||||
export DBUS_SESSION_BUS_ADDRESS="unix:path=$XDG_RUNTIME_DIR/bus"
|
||||
[ -S "$XDG_RUNTIME_DIR/bus" ] || { echo "user bus unavailable; enable loginctl enable-linger" >&2; exit 1; }
|
||||
systemctl --user unset-environment WAYLAND_DISPLAY
|
||||
dbus-update-activation-environment --systemd DISPLAY XAUTHORITY XDG_SESSION_TYPE
|
||||
# Keep the supervising shell alive: exec would discard the Xvfb cleanup trap.
|
||||
sh -c "$session" &
|
||||
else
|
||||
dbus-run-session -- sh -c "$session" &
|
||||
fi
|
||||
child=$!
|
||||
if [ -n "${NOTIFY_SOCKET:-}" ]; then systemd-notify --ready; fi
|
||||
wait "$child"
|
||||
@@ -0,0 +1,19 @@
|
||||
[Unit]
|
||||
Description=Virtual X desktop for uu-remote-linux (Xvfb + session bus)
|
||||
After=dbus.socket
|
||||
|
||||
[Service]
|
||||
Type=notify
|
||||
NotifyAccess=all
|
||||
TimeoutStartSec=30
|
||||
TimeoutStopSec=10
|
||||
KillMode=control-group
|
||||
# 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,11 @@
|
||||
# ~/.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
|
||||
Environment=XAUTHORITY=%t/uu-virtual-99.Xauthority
|
||||
UnsetEnvironment=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.**
|
||||
@@ -0,0 +1,31 @@
|
||||
VAAPI 硬件编码、虚拟声卡、无人登录的虚拟桌面和 Arch/Omarchy 支持。/ VAAPI encoding, virtual audio sink, linger virtual desktop, Arch/Omarchy.
|
||||
|
||||
### 新增 / Added
|
||||
- **VAAPI 硬件编码(Intel/AMD)**:`UU_VIDEO_ENCODER=vaapi`(`UU_VAAPI_DEVICE` 默认 `/dev/dri/renderD128`),需要显式开启,不会自动选择;
|
||||
初始化失败时自动回退 CPU 编码。✅ Intel UHD 770(iHD 驱动)实机验收:用官方 Mac 控制端测试了各档画质、30/60 帧、缩放和重连,
|
||||
全程硬件编码,CPU 占用比 libx264 低约 40%。⚠️ AMD 显卡、`--features inproc-av` 管线的 VAAPI 尚未实机验证。
|
||||
Opt-in VAAPI with CPU fallback. Accepted on Intel UHD 770; AMD and the inproc-av path are not hardware-verified.
|
||||
- **虚拟声卡(静音运行)**:开启「静音运行」时创建虚拟输出 `uu-remote-sink`,把声音转到它上面采集,本机扬声器静音、控制端照常有声;
|
||||
结束后恢复原默认输出;异常退出后下次启动自动清理。有 `pactl` 时用它,没有时(默认的 Ubuntu 桌面)用 PipeWire 自带的 `pw-cli`/`wpctl`。
|
||||
✅ Ubuntu 24.04(PipeWire 1.0.5)上用官方 Mac 控制端验收:开关、断开恢复、多工具连接,以及强杀后的恢复。
|
||||
Virtual null sink while muted (pactl or PipeWire-native). Accepted on Ubuntu 24.04 with the official Mac viewer.
|
||||
- **虚拟桌面改进**:默认使用 linger 用户总线,并导入 `DISPLAY`/`XAUTHORITY`,D-Bus 激活的程序(如 GNOME Terminal)也会出现在虚拟屏幕上;
|
||||
独立的 X 认证 cookie、`Type=notify` 就绪通知、退出清理;Xvfb 被强杀后残留的显示器文件会自动清理。
|
||||
✅ 虚拟机里验证了无人登录开机上线、GNOME Terminal 出现在 `:99`、键鼠;残留清理在实机验证。
|
||||
⚠️ 无人登录场景下尚未用官方控制端完成验收(清单见 `contrib/virtual-desktop/ACCEPTANCE.md`)。
|
||||
- **Arch Linux / Omarchy**:README 增加 pacman 依赖和 Omarchy(Hyprland)说明。✅ 虚拟机里 Hyprland 截屏、uinput 键鼠、剪贴板通过。
|
||||
⚠️ 尚未在真实 Omarchy 上用官方控制端验收;相对鼠标位移的精确像素没有验证。
|
||||
- **KMS 截屏(实验原型,默认不编译)**:`--features kms` + `UU_DESKTOP_BACKEND=kms`,需要 `cap_sys_admin`。
|
||||
❌ 虚拟机里登录界面截图全黑,视觉验收未通过;**不支持**锁屏/登录界面,发布包里也不包含这个功能。说明见 `docs/validation/kms.md`。
|
||||
|
||||
### 修复 / Fixed
|
||||
- 虚拟声卡恢复时,如果原默认声卡已经不存在(比如 USB 声卡被拔掉),不再留下虚拟输出作为默认。
|
||||
- 「静音运行」相关的日志会区分虚拟输出和真实声卡的静音状态。
|
||||
|
||||
### 未改变 / Unchanged
|
||||
- 无头模式下打开远程桌面仍会显示「正在重新连接」:没找到官方的「无桌面」错误码,不挪用其他错误码。
|
||||
|
||||
### 下载 / Download
|
||||
`uu-remote-linux-v0.1.7-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
|
||||
|
||||
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||
+3
-2
@@ -81,9 +81,9 @@ pub async fn stream_audio(pc: Arc<RTCPeerConnection>, track: Arc<TrackLocalStati
|
||||
if closed(&pc) { return; }
|
||||
tokio::time::sleep(Duration::from_millis(100)).await;
|
||||
}
|
||||
let source = std::env::var("UU_AUDIO_SOURCE").unwrap_or_else(|_| "@DEFAULT_MONITOR@".into());
|
||||
let mut failures = 0u32;
|
||||
while !closed(&pc) {
|
||||
let source = crate::virtual_audio::source();
|
||||
let (tx, mut rx) = tokio::sync::mpsc::channel::<bytes::Bytes>(25);
|
||||
let (stopper, job) = match start(&source, tx) {
|
||||
Ok(started) => started,
|
||||
@@ -101,7 +101,7 @@ pub async fn stream_audio(pc: Arc<RTCPeerConnection>, track: Arc<TrackLocalStati
|
||||
}
|
||||
packets += 1;
|
||||
},
|
||||
_ = health.tick() => if closed(&pc) { break },
|
||||
_ = health.tick() => if closed(&pc) || crate::virtual_audio::source() != source { break },
|
||||
}
|
||||
}
|
||||
// Dropping the receiver and the stopper ends the encoder at its next packet.
|
||||
@@ -109,6 +109,7 @@ pub async fn stream_audio(pc: Arc<RTCPeerConnection>, track: Arc<TrackLocalStati
|
||||
drop(stopper);
|
||||
let result = job.await.unwrap_or_else(|e| Err(e.to_string()));
|
||||
if closed(&pc) { break; }
|
||||
if crate::virtual_audio::source() != source { failures=0; continue; }
|
||||
log::warn!("Desktop audio stopped after {packets} packets: {:?}", result.err());
|
||||
failures = if packets > 250 { 0 } else { failures + 1 };
|
||||
if failures >= 5 { log::warn!("Desktop audio disabled for this connection"); return; }
|
||||
|
||||
+13
-3
@@ -46,6 +46,7 @@ pub struct ScreenCapture {
|
||||
pub init_error: Option<String>,
|
||||
pub portal: Option<std::sync::Arc<crate::portal::PortalSession>>,
|
||||
pub display: String,
|
||||
kms: Option<crate::kms::Capture>,
|
||||
pub width: u32,
|
||||
pub height: u32,
|
||||
buffer: Mutex<Vec<u8>>,
|
||||
@@ -54,15 +55,21 @@ pub struct ScreenCapture {
|
||||
impl ScreenCapture {
|
||||
pub fn new() -> Result<Self, String> {
|
||||
let backend=crate::desktop_backend::detect()?;
|
||||
if backend == crate::desktop_backend::DesktopBackend::Kms {
|
||||
let kms=crate::kms::Capture::open()?;
|
||||
let (width,height)=(kms.width,kms.height);
|
||||
return Ok(Self { backend,init_error:None,portal:None,display:"kms:linear".into(),width,height,
|
||||
buffer:Mutex::new(vec![0;width as usize*height as usize*4]),kms:Some(kms) });
|
||||
}
|
||||
if backend == crate::desktop_backend::DesktopBackend::Portal {
|
||||
let session=std::sync::Arc::new(crate::portal::PortalSession::open()?);
|
||||
let (width,height)=(session.width,session.height);
|
||||
return Ok(Self { backend,init_error:None,portal:Some(session),display:"wayland:portal".into(),width,height,
|
||||
return Ok(Self { backend,init_error:None,portal:Some(session),kms:None,display:"wayland:portal".into(),width,height,
|
||||
buffer:Mutex::new(vec![0;(width as usize)*(height as usize)*4]) });
|
||||
}
|
||||
if backend == crate::desktop_backend::DesktopBackend::Wayland {
|
||||
let (width,height)=crate::wayland::init()?;
|
||||
return Ok(Self { backend, init_error:None, portal:None, display:"wayland:native".into(), width,height,
|
||||
return Ok(Self { backend, init_error:None, portal:None, kms:None, display:"wayland:native".into(), width,height,
|
||||
buffer:Mutex::new(vec![0;(width as usize)*(height as usize)*4]) });
|
||||
}
|
||||
let ret = unsafe { x11_init(std::ptr::null()) };
|
||||
@@ -90,6 +97,7 @@ impl ScreenCapture {
|
||||
backend,
|
||||
init_error:None,
|
||||
portal:None,
|
||||
kms:None,
|
||||
display,
|
||||
width: w as u32,
|
||||
height: h as u32,
|
||||
@@ -100,13 +108,14 @@ impl ScreenCapture {
|
||||
/// Metadata for non-desktop tools. Never advertise a usable fake screen.
|
||||
pub fn unavailable(error:String)->Self {
|
||||
Self { backend:crate::desktop_backend::detect().unwrap_or(crate::desktop_backend::DesktopBackend::X11),
|
||||
init_error:Some(error), portal:None, display:String::new(), width:0,height:0,buffer:Mutex::new(Vec::new()) }
|
||||
init_error:Some(error), portal:None, kms:None, display:String::new(), width:0,height:0,buffer:Mutex::new(Vec::new()) }
|
||||
}
|
||||
|
||||
/// 截取一帧 BGRA 图像
|
||||
pub fn capture_frame(&self) -> Result<Vec<u8>, String> {
|
||||
if let Some(error)=&self.init_error { return Err(error.clone()); }
|
||||
let mut buf = self.buffer.lock().unwrap();
|
||||
if let Some(kms)=&self.kms { kms.capture(&mut buf)?; return Ok(buf.clone()); }
|
||||
if let Some(session)=&self.portal { session.capture(&mut buf)?; return Ok(buf.clone()); }
|
||||
if self.backend == crate::desktop_backend::DesktopBackend::Wayland {
|
||||
crate::wayland::capture(&mut buf)?;
|
||||
@@ -132,6 +141,7 @@ impl Drop for ScreenCapture {
|
||||
|
||||
/// Keep the display visible only while a peer is streaming; never unlock it.
|
||||
pub fn keep_display_awake() -> Result<(), String> {
|
||||
if crate::desktop_backend::detect()? == crate::desktop_backend::DesktopBackend::Kms { return Ok(()); }
|
||||
if crate::desktop_backend::detect()? == crate::desktop_backend::DesktopBackend::Portal { return Ok(()); }
|
||||
if crate::desktop_backend::detect()? == crate::desktop_backend::DesktopBackend::Wayland {
|
||||
return crate::wayland::wake();
|
||||
|
||||
+1
-1
@@ -20,7 +20,7 @@ extern "C" {
|
||||
|
||||
pub fn start(display: String) -> ClipboardBridge {
|
||||
if display == "wayland:native" { return start_wayland(); }
|
||||
if display == "wayland:portal" {
|
||||
if display == "wayland:portal" || display.starts_with("kms:") {
|
||||
let (incoming,_)=watch::channel(None);let (_,outgoing)=watch::channel(None);
|
||||
let (sender,ready)=tokio::sync::oneshot::channel();let _=sender.send(false);
|
||||
return ClipboardBridge {incoming,outgoing,ready};
|
||||
|
||||
+21
-16
@@ -4,9 +4,13 @@ pub fn run() -> Result<(),String> {
|
||||
if std::fs::read_to_string("/proc/sys/kernel/hostname").unwrap_or_default().trim()!="uu-hyprland-test" {
|
||||
return Err("Clipboard acceptance is restricted to the isolated VM".into());
|
||||
}
|
||||
let session=Arc::new(crate::portal::PortalSession::open()?);
|
||||
let bridge=crate::clipboard::start_portal(session.clone());
|
||||
if !bridge.ready.blocking_recv().unwrap_or(false) { return Err("Portal clipboard permission not granted".into()); }
|
||||
let screen=crate::capture::ScreenCapture::new()?;
|
||||
let input=Arc::new(crate::input::InputInjector::new());
|
||||
input.bind_portal(screen.portal.clone());
|
||||
let pointer=input.start_pointer_worker();
|
||||
let bridge=if let Some(session)=screen.portal.clone() { crate::clipboard::start_portal(session) }
|
||||
else { crate::clipboard::start(screen.display.clone()) };
|
||||
if !bridge.ready.blocking_recv().unwrap_or(false) { return Err("Desktop clipboard permission not granted".into()); }
|
||||
let mut child=Command::new("python3").arg("/home/uuremote/clipboard-acceptance.py")
|
||||
.env("GDK_BACKEND","wayland").env("GSK_RENDERER","cairo")
|
||||
.stdin(Stdio::piped()).stdout(Stdio::piped()).stderr(Stdio::null()).spawn().map_err(|_|"Start GTK clipboard fixture")?;
|
||||
@@ -29,13 +33,14 @@ pub fn run() -> Result<(),String> {
|
||||
bridge.incoming.send_replace(Some(text.clone()));
|
||||
let deadline=Instant::now()+Duration::from_secs(6);let mut matched=false;
|
||||
while Instant::now()<deadline { if check("read",&text)? {matched=true;break;} std::thread::sleep(Duration::from_millis(100)); }
|
||||
if !matched { return Err(format!("Portal clipboard incoming {name} mismatch")); }
|
||||
if !matched { return Err(format!("Desktop clipboard incoming {name} mismatch")); }
|
||||
std::thread::sleep(Duration::from_millis(300));
|
||||
if outgoing.has_changed().unwrap_or(false) { return Err("Portal clipboard echoed its own selection".into()); }
|
||||
println!("Portal clipboard incoming {name}: passed; no echo");
|
||||
if outgoing.has_changed().unwrap_or(false) { return Err("Desktop clipboard echoed its own selection".into()); }
|
||||
println!("Desktop clipboard incoming {name}: passed; no echo");
|
||||
}
|
||||
session.move_pointer(f64::from(session.width)/2.0,f64::from(session.height)/2.0,false)?;
|
||||
session.button(1,true)?; session.button(1,false)?;
|
||||
input.mouse_absolute_normalized(0.5,0.5,(screen.width,screen.height));
|
||||
std::thread::sleep(Duration::from_millis(100));
|
||||
input.mouse_button(1,true); input.mouse_button(1,false);
|
||||
std::thread::sleep(Duration::from_millis(300));
|
||||
let external="UU outgoing 中文🚀\nsecond line";
|
||||
if !check("set",external)? { return Err("GTK clipboard ownership failed".into()); }
|
||||
@@ -44,25 +49,25 @@ pub fn run() -> Result<(),String> {
|
||||
if outgoing.has_changed().unwrap_or(false) && outgoing.borrow_and_update().as_deref()==Some(external) {matched=true;break;}
|
||||
std::thread::sleep(Duration::from_millis(100));
|
||||
}
|
||||
if !matched { return Err("Portal clipboard outgoing Unicode mismatch".into()); }
|
||||
println!("Portal clipboard outgoing unicode: passed");
|
||||
if !matched { return Err("Desktop clipboard outgoing Unicode mismatch".into()); }
|
||||
println!("Desktop clipboard outgoing unicode: passed");
|
||||
bridge.incoming.send_replace(Some("x".repeat(crate::clipboard::MAX_TEXT+1)));
|
||||
std::thread::sleep(Duration::from_millis(400));
|
||||
if !check("read",external)? { return Err("Oversized text altered native clipboard".into()); }
|
||||
println!("Portal clipboard incoming oversized rejection: passed");
|
||||
println!("Desktop clipboard incoming oversized rejection: passed");
|
||||
if !check("set",&"y".repeat(crate::clipboard::MAX_TEXT+1))? { return Err("GTK oversized fixture setup failed".into()); }
|
||||
std::thread::sleep(Duration::from_secs(3));
|
||||
if outgoing.has_changed().unwrap_or(false) { return Err("Portal clipboard published oversized native text".into()); }
|
||||
println!("Portal clipboard outgoing oversized rejection: passed");
|
||||
if outgoing.has_changed().unwrap_or(false) { return Err("Desktop clipboard published oversized native text".into()); }
|
||||
println!("Desktop clipboard outgoing oversized rejection: passed");
|
||||
if !check("set","")? { return Err("GTK empty fixture setup failed".into()); }
|
||||
let deadline=Instant::now()+Duration::from_secs(6);let mut matched=false;
|
||||
while Instant::now()<deadline {
|
||||
if outgoing.has_changed().unwrap_or(false) && outgoing.borrow_and_update().as_deref()==Some("") {matched=true;break;}
|
||||
std::thread::sleep(Duration::from_millis(100));
|
||||
}
|
||||
if !matched { return Err("Portal clipboard outgoing empty text mismatch".into()); }
|
||||
println!("Portal clipboard outgoing empty: passed");
|
||||
if !matched { return Err("Desktop clipboard outgoing empty text mismatch".into()); }
|
||||
println!("Desktop clipboard outgoing empty: passed");
|
||||
drop(writer); Ok(())
|
||||
})();
|
||||
let _=child.kill();let _=child.wait();result
|
||||
let _=child.kill();let _=child.wait();pointer.abort();input.release_held();result
|
||||
}
|
||||
@@ -1,6 +1,10 @@
|
||||
//! Select the host desktop, not the presence of an XWayland DISPLAY socket.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum DesktopBackend { X11, Wayland, Portal }
|
||||
pub enum DesktopBackend { X11, Wayland, Portal, Kms }
|
||||
|
||||
/// `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(),
|
||||
@@ -15,13 +19,15 @@ fn select(explicit: Option<&str>, session: Option<&str>, socket: Option<&str>) -
|
||||
match explicit {
|
||||
Some("x11") => Ok(DesktopBackend::X11),
|
||||
Some("wayland") => Ok(DesktopBackend::Wayland),
|
||||
Some("kms") => Ok(DesktopBackend::Kms),
|
||||
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, kms or none".into()),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,6 +40,7 @@ mod tests {
|
||||
assert_eq!(select(None,None,Some("wayland-1")),Ok(DesktopBackend::Wayland));
|
||||
assert_eq!(select(None,Some("x11"),None),Ok(DesktopBackend::X11));
|
||||
assert_eq!(select(Some("x11"),Some("wayland"),Some("wayland-0")),Ok(DesktopBackend::X11));
|
||||
assert_eq!(select(Some("kms"),Some("wayland"),Some("wayland-0")),Ok(DesktopBackend::Kms));
|
||||
assert!(select(Some("typo"),None,None).is_err());
|
||||
}
|
||||
}
|
||||
+50
-7
@@ -24,7 +24,7 @@ pub struct Config {
|
||||
pub target_bps: i64,
|
||||
pub max_bps: i64,
|
||||
pub buffer_bits: i64,
|
||||
pub gpu: bool,
|
||||
pub encoder: crate::video::Encoder,
|
||||
/// Capture on XDamage changes (XShm) instead of x11grab; `UU_CAPTURE=grab` turns it off.
|
||||
pub damage: bool,
|
||||
}
|
||||
@@ -74,15 +74,16 @@ fn drain(encoder: &mut encoder::Video, packet: &mut Packet, units: &mpsc::Sender
|
||||
/// Opens the H.264 encoder for `config`. Returns the encoder, its input pixel format and output size.
|
||||
fn open_encoder(config: &Config) -> Result<(encoder::Video, Pixel, u32, u32), String> {
|
||||
let (width, height) = config.output.unwrap_or(config.input);
|
||||
let codec = encoder::find_by_name(if config.gpu { "h264_nvenc" } else { "libx264" })
|
||||
let codec = encoder::find_by_name(config.encoder.name())
|
||||
.ok_or("H.264 encoder is not available in this FFmpeg build")?;
|
||||
// Allocate for this codec so FFmpeg fills its defaults; libx264 rejects a context allocated without one.
|
||||
let mut context = unsafe { codec::context::Context::wrap(ff::sys::avcodec_alloc_context3(codec.as_ptr()), None) }.encoder().video().map_err(text)?;
|
||||
context.set_width(width);
|
||||
context.set_height(height);
|
||||
// NVENC takes packed BGR0 directly, so the GPU path skips the CPU colour conversion to YUV420P.
|
||||
let pixel = if config.gpu { Pixel::BGRZ } else { Pixel::YUV420P };
|
||||
context.set_format(pixel);
|
||||
let pixel = match config.encoder { crate::video::Encoder::Nvenc => Pixel::BGRZ, crate::video::Encoder::Vaapi => Pixel::NV12, _ => Pixel::YUV420P };
|
||||
context.set_format(if config.encoder == crate::video::Encoder::Vaapi { Pixel::VAAPI } else { pixel });
|
||||
if config.encoder == crate::video::Encoder::Vaapi { attach_vaapi(&mut context, width, height)?; }
|
||||
context.set_time_base(Rational::new(1, config.fps as i32));
|
||||
context.set_frame_rate(Some(Rational::new(config.fps as i32, 1)));
|
||||
context.set_gop(config.gop);
|
||||
@@ -98,7 +99,10 @@ fn open_encoder(config: &Config) -> Result<(encoder::Video, Pixel, u32, u32), St
|
||||
let cq = config.cq.to_string();
|
||||
let gop = config.gop.to_string();
|
||||
let mut options = Dictionary::new();
|
||||
if config.gpu {
|
||||
if config.encoder == crate::video::Encoder::Vaapi {
|
||||
for (key,value) in [("profile", "constrained_baseline"), ("rc_mode", "VBR"), ("async_depth", "1"), ("aud", "1")] { options.set(key,value); }
|
||||
options.set("qmin", &cq);
|
||||
} else if config.encoder == crate::video::Encoder::Nvenc {
|
||||
for (key, value) in [("preset", "p4"), ("tune", "ull"), ("profile", "baseline"), ("rc", "vbr"),
|
||||
("rc-lookahead", "0"), ("zerolatency", "1"), ("delay", "0"), ("aud", "1"), ("forced-idr", "1")] {
|
||||
options.set(key, value);
|
||||
@@ -116,6 +120,45 @@ fn open_encoder(config: &Config) -> Result<(encoder::Video, Pixel, u32, u32), St
|
||||
Ok((encoder, pixel, width, height))
|
||||
}
|
||||
|
||||
// Each codec owns a reference to its hardware frame pool. Every error path releases
|
||||
// the temporary device/pool refs; AVCodecContext releases its own ref on drop.
|
||||
fn attach_vaapi(context: &mut encoder::video::Video, width: u32, height: u32) -> Result<(), String> {
|
||||
use ff::sys::*;
|
||||
let device_name = CString::new(crate::video::vaapi_device()).map_err(|_| "Invalid VAAPI device")?;
|
||||
unsafe {
|
||||
let mut device = std::ptr::null_mut();
|
||||
let result = av_hwdevice_ctx_create(&mut device, AVHWDeviceType::AV_HWDEVICE_TYPE_VAAPI, device_name.as_ptr(), std::ptr::null_mut(), 0);
|
||||
if result < 0 { return Err(format!("VAAPI device initialization failed: {}", text(ff::Error::from(result)))); }
|
||||
let mut pool = av_hwframe_ctx_alloc(device);
|
||||
av_buffer_unref(&mut device);
|
||||
if pool.is_null() { return Err("VAAPI frame pool allocation failed".into()); }
|
||||
let frames = (*pool).data as *mut AVHWFramesContext;
|
||||
(*frames).format = AVPixelFormat::AV_PIX_FMT_VAAPI;
|
||||
(*frames).sw_format = AVPixelFormat::AV_PIX_FMT_NV12;
|
||||
(*frames).width = width as i32;
|
||||
(*frames).height = height as i32;
|
||||
(*frames).initial_pool_size = 8;
|
||||
let result = av_hwframe_ctx_init(pool);
|
||||
if result < 0 { av_buffer_unref(&mut pool); return Err(format!("VAAPI frame pool initialization failed: {}", text(ff::Error::from(result)))); }
|
||||
(*context.as_mut_ptr()).hw_frames_ctx = pool;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn send_video_frame(encoder: &mut encoder::Video, frame: &frame::Video, kind: crate::video::Encoder) -> Result<(), String> {
|
||||
if kind != crate::video::Encoder::Vaapi { return encoder.send_frame(frame).map_err(text); }
|
||||
let mut uploaded = frame::Video::empty();
|
||||
unsafe {
|
||||
let result = ff::sys::av_hwframe_get_buffer((*encoder.as_ptr()).hw_frames_ctx, uploaded.as_mut_ptr(), 0);
|
||||
if result < 0 { return Err(text(ff::Error::from(result))); }
|
||||
let result = ff::sys::av_hwframe_transfer_data(uploaded.as_mut_ptr(), frame.as_ptr(), 0);
|
||||
if result < 0 { return Err(text(ff::Error::from(result))); }
|
||||
let result = ff::sys::av_frame_copy_props(uploaded.as_mut_ptr(), frame.as_ptr());
|
||||
if result < 0 { return Err(text(ff::Error::from(result))); }
|
||||
}
|
||||
encoder.send_frame(&uploaded).map_err(text)
|
||||
}
|
||||
|
||||
/// Runs until `stop` is set or the receiver is dropped. A set `idr` flag forces the next frame to be an IDR.
|
||||
pub fn run(config: Config, units: mpsc::Sender<Vec<u8>>, stop: Arc<AtomicBool>, idr: Arc<AtomicBool>) -> Result<(), String> {
|
||||
ensure_init()?;
|
||||
@@ -184,7 +227,7 @@ pub fn run(config: Config, units: mpsc::Sender<Vec<u8>>, stop: Arc<AtomicBool>,
|
||||
|
||||
frame.set_pts(Some(slot));
|
||||
frame.set_kind(if idr.swap(false, Ordering::AcqRel) { picture::Type::I } else { picture::Type::None });
|
||||
encoder.send_frame(frame).map_err(text)?;
|
||||
send_video_frame(&mut encoder, frame, config.encoder)?;
|
||||
if !drain(&mut encoder, &mut packet, &units)? { return Ok(()); }
|
||||
}
|
||||
Ok(())
|
||||
@@ -244,7 +287,7 @@ fn run_damage(config: Config, mut capture: DamageCapture, units: mpsc::Sender<Ve
|
||||
};
|
||||
frame.set_pts(Some(slot));
|
||||
frame.set_kind(if idr.swap(false, Ordering::AcqRel) { picture::Type::I } else { picture::Type::None });
|
||||
encoder.send_frame(frame).map_err(text)?;
|
||||
send_video_frame(&mut encoder, frame, config.encoder)?;
|
||||
if !drain(&mut encoder, &mut packet, &units)? { return Ok(()); }
|
||||
let now = Instant::now();
|
||||
if dirty {
|
||||
|
||||
+108
-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,47 @@ 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 self.backend==crate::desktop_backend::DesktopBackend::Kms { return -1; }
|
||||
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 self.backend==crate::desktop_backend::DesktopBackend::Kms { return -1; }
|
||||
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 self.backend==crate::desktop_backend::DesktopBackend::Kms { return -1; }
|
||||
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 +296,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::*;
|
||||
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
//! Experimental linear KMS framebuffer capture. Never selected automatically.
|
||||
#[cfg(all(feature="kms",target_os="linux"))]
|
||||
mod imp {
|
||||
use std::ffi::CString;
|
||||
use std::sync::Mutex;
|
||||
extern "C" {
|
||||
fn uu_kms_open(device:*const libc::c_char,w:*mut u32,h:*mut u32)->*mut libc::c_void;
|
||||
fn uu_kms_read(handle:*mut libc::c_void,out:*mut u8,length:usize)->i32;
|
||||
fn uu_kms_close(handle:*mut libc::c_void);
|
||||
}
|
||||
struct Handle(*mut libc::c_void);
|
||||
// The FD/CRTC handle is exclusively accessed through Capture's mutex.
|
||||
unsafe impl Send for Handle {}
|
||||
impl Drop for Handle { fn drop(&mut self) { unsafe { uu_kms_close(self.0) }; } }
|
||||
pub struct Capture { handle:Mutex<Handle>, pub width:u32, pub height:u32 }
|
||||
impl Capture {
|
||||
pub fn open()->Result<Self,String> {
|
||||
let device=CString::new(std::env::var("UU_KMS_DEVICE").unwrap_or_else(|_|"/dev/dri/card0".into())).map_err(|_|"Invalid KMS device")?;
|
||||
let (mut width,mut height)=(0,0);
|
||||
let handle=unsafe { uu_kms_open(device.as_ptr(),&mut width,&mut height) };
|
||||
if handle.is_null() { return Err("KMS: cannot open an active CRTC; check card permissions and connected output".into()); }
|
||||
let capture=Self { handle:Mutex::new(Handle(handle)), width,height };
|
||||
// Do not advertise a desktop until we have actually read the buffer.
|
||||
let mut frame=vec![0; width as usize*height as usize*4];
|
||||
capture.capture(&mut frame)?;
|
||||
if frame.chunks_exact(4).all(|p| p[..3]==[0,0,0]) {
|
||||
log::warn!("KMS initial frame is uniformly black; GPU buffer visibility is unverified (see KMS limitations)");
|
||||
}
|
||||
Ok(capture)
|
||||
}
|
||||
pub fn capture(&self,output:&mut [u8])->Result<(),String> {
|
||||
if output.len()!=self.width as usize*self.height as usize*4 { return Err("KMS: invalid frame dimensions".into()); }
|
||||
let handle=self.handle.lock().unwrap();
|
||||
match unsafe { uu_kms_read(handle.0,output.as_mut_ptr(),output.len()) } {
|
||||
0=>Ok(()), -2=>Err("KMS mode changed; restart host to refresh desktop geometry".into()),
|
||||
-5=>Err("KMS framebuffer handles unavailable; requires cap_sys_admin+ep".into()),
|
||||
code=>Err(format!("KMS capture failed ({code}); prototype needs a mappable linear XRGB/ARGB dumb buffer")),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#[cfg(all(feature="kms",target_os="linux"))]
|
||||
pub use imp::Capture;
|
||||
#[cfg(not(all(feature="kms",target_os="linux")))]
|
||||
pub struct Capture { pub width:u32, pub height:u32 }
|
||||
#[cfg(not(all(feature="kms",target_os="linux")))]
|
||||
impl Capture {
|
||||
pub fn open()->Result<Self,String> { Err("KMS requires Linux and --features kms".into()) }
|
||||
pub fn capture(&self,_:&mut [u8])->Result<(),String> { Err("KMS requires Linux and --features kms".into()) }
|
||||
}
|
||||
+48
-3
@@ -12,8 +12,10 @@ mod login;
|
||||
mod login_ui;
|
||||
mod session_lock;
|
||||
mod capture;
|
||||
mod kms;
|
||||
mod file_trans;
|
||||
mod input;
|
||||
mod uinput;
|
||||
mod input_mapping;
|
||||
mod port_mapping;
|
||||
mod power;
|
||||
@@ -27,6 +29,7 @@ mod damage_capture;
|
||||
mod inproc_encoder;
|
||||
mod capture_setting;
|
||||
mod audio;
|
||||
mod virtual_audio;
|
||||
mod system_actions;
|
||||
mod capability;
|
||||
mod protocol;
|
||||
@@ -54,7 +57,7 @@ use webrtc::WebRtcSession;
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();
|
||||
// An unknown argument must never fall through to starting a second full host server.
|
||||
const SUBCOMMANDS: [&str; 9] = ["login", "unlock-check", "desktop-probe", "desktop-video-probe", "desktop-audio-probe", "wayland-capture",
|
||||
const SUBCOMMANDS: [&str; 11] = ["desktop-input-probe", "virtual-audio-probe", "login", "unlock-check", "desktop-probe", "desktop-video-probe", "desktop-audio-probe", "wayland-capture",
|
||||
"wayland-input-probe", "clipboard-acceptance", "portal-acceptance"];
|
||||
if let Some(arg) = std::env::args().nth(1).filter(|a| !SUBCOMMANDS.contains(&a.as_str())) {
|
||||
let usage = format!("usage: uuremote-server [{}]\n (no argument: run the host)", SUBCOMMANDS.join(" | "));
|
||||
@@ -163,6 +166,30 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let path=std::env::args().nth(2);
|
||||
return video::probe((screen.width,screen.height),screen.display.clone(),screen.portal.clone(),path).await.map_err(|e|e.into());
|
||||
}
|
||||
if std::env::args().nth(1).as_deref() == Some("desktop-input-probe") {
|
||||
if std::fs::read_to_string("/proc/sys/kernel/hostname").unwrap_or_default().trim()!="uu-hyprland-test" {
|
||||
return Err("Input acceptance is restricted to the isolated VM".into());
|
||||
}
|
||||
let screen=ScreenCapture::new()?;
|
||||
let input=Arc::new(InputInjector::new());
|
||||
input.bind_portal(screen.portal.clone());
|
||||
let worker=input.start_pointer_worker();
|
||||
// libinput discovers newly created uinput devices asynchronously.
|
||||
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
|
||||
input.mouse_absolute_normalized(0.25,0.25,(screen.width,screen.height));
|
||||
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
|
||||
input.mouse_absolute_normalized(0.5,0.5,(screen.width,screen.height));
|
||||
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
|
||||
input.mouse_button(1,true); input.mouse_button(1,false);
|
||||
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
|
||||
input.mouse_relative(32,0);
|
||||
tokio::time::sleep(std::time::Duration::from_millis(200)).await;
|
||||
for key in b"UUWAYLAND" { input.windows_key_event(i32::from(*key),true); input.windows_key_event(i32::from(*key),false); }
|
||||
tokio::time::sleep(std::time::Duration::from_millis(300)).await;
|
||||
worker.abort(); input.release_held();
|
||||
println!("Desktop test events sent; verify the GTK acceptance report");
|
||||
return Ok(());
|
||||
}
|
||||
if std::env::args().nth(1).as_deref() == Some("wayland-input-probe") {
|
||||
return wayland::input_probe().map_err(|e|e.into());
|
||||
}
|
||||
@@ -195,6 +222,16 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
if std::env::args().nth(1).as_deref() == Some("unlock-check") {
|
||||
return session_lock::check_from_stdin().map_err(|e| e.into());
|
||||
}
|
||||
if std::env::args().nth(1).as_deref() == Some("virtual-audio-probe") {
|
||||
virtual_audio::init();
|
||||
if !virtual_audio::enable() { return Err("Virtual audio probe setup failed".into()); }
|
||||
println!("Virtual audio probe active");
|
||||
tokio::time::sleep(std::time::Duration::from_secs(5)).await;
|
||||
virtual_audio::disable();
|
||||
if virtual_audio::active() { return Err("Virtual audio probe restore incomplete".into()); }
|
||||
println!("Virtual audio probe ended");
|
||||
return Ok(());
|
||||
}
|
||||
if std::env::args().nth(1).as_deref() == Some("login") {
|
||||
// Interactive: register this host and bind it to an account via QR scan
|
||||
// (GTK window when available; `login --no-gui` keeps it terminal-only).
|
||||
@@ -202,9 +239,13 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
if login::run(gui).await.is_err() { std::process::exit(1); } // error already shown
|
||||
return Ok(());
|
||||
}
|
||||
virtual_audio::init();
|
||||
let _audio_cleanup=virtual_audio::Cleanup;
|
||||
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| {
|
||||
@@ -218,7 +259,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let shutdown_input=input.clone();
|
||||
tokio::spawn(async move {
|
||||
if let Ok(mut term)=tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate()) {
|
||||
term.recv().await; shutdown_input.release_held(); terminal::shutdown_all(); std::process::exit(0);
|
||||
term.recv().await; virtual_audio::disable(); shutdown_input.release_held(); terminal::shutdown_all(); std::process::exit(0);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -358,6 +399,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);
|
||||
|
||||
+10
-5
@@ -113,10 +113,14 @@ pub fn args_shape(args: &[u8]) -> String {
|
||||
}
|
||||
|
||||
fn mute_for_session(state: &mut HostState) {
|
||||
if state.restore.is_none() { state.restore = host_muted(); }
|
||||
set_host_muted(true);
|
||||
if state.restore.is_none() {
|
||||
state.restore = host_muted();
|
||||
set_host_muted(true);
|
||||
}
|
||||
crate::virtual_audio::enable();
|
||||
}
|
||||
fn restore_mute(state: &mut HostState) {
|
||||
crate::virtual_audio::disable();
|
||||
if let Some(previous) = state.restore.take() { set_host_muted(previous); }
|
||||
}
|
||||
|
||||
@@ -143,20 +147,20 @@ impl PeerActions {
|
||||
let on = toggle_value(value, args);
|
||||
state.settings.keep_mute = on;
|
||||
if on { mute_for_session(&mut state); } else { restore_mute(&mut state); }
|
||||
log::info!("System action mute (action {}): on={} muted={:?}", action, on, host_muted());
|
||||
log::info!("System action mute (action {}): on={} virtual_sink={} default_muted={:?}", action, on, crate::virtual_audio::active(), host_muted());
|
||||
}
|
||||
// Keep the host muted after the session (远程结束后,恢复被控端声音设置 unchecked).
|
||||
MUTE_PERMANENT => {
|
||||
let on = toggle_value(value, args);
|
||||
state.settings.mute_permanent = on;
|
||||
if on { state.settings.keep_mute = true; mute_for_session(&mut state); }
|
||||
log::info!("System action mute_permanent: on={} muted={:?}", on, host_muted());
|
||||
log::info!("System action mute_permanent: on={} virtual_sink={} default_muted={:?}", on, crate::virtual_audio::active(), host_muted());
|
||||
}
|
||||
MUTE_RESTORE => {
|
||||
state.settings.keep_mute = false;
|
||||
state.settings.mute_permanent = false;
|
||||
restore_mute(&mut state);
|
||||
log::info!("System action mute_restore: muted={:?}", host_muted());
|
||||
log::info!("System action mute_restore: virtual_sink={} default_muted={:?}", crate::virtual_audio::active(), host_muted());
|
||||
}
|
||||
SET_AUTO_LOCK => {
|
||||
// Observed: enabling 远程结束后自动锁屏 sends value 2 and disabling value 1.
|
||||
@@ -188,6 +192,7 @@ impl PeerActions {
|
||||
let mut state = state().lock().unwrap();
|
||||
state.desktop_sessions = state.desktop_sessions.saturating_sub(1);
|
||||
if state.desktop_sessions > 0 { return; }
|
||||
crate::virtual_audio::disable();
|
||||
if !state.settings.mute_permanent { restore_mute(&mut state); }
|
||||
if state.settings.auto_lock {
|
||||
log::info!("Auto lock after remote session: ok={}", lock_session());
|
||||
|
||||
+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);
|
||||
}
|
||||
}
|
||||
+78
-15
@@ -53,9 +53,30 @@ struct CompleteFrame {
|
||||
|
||||
const RTP_CLOCK: u64 = 90_000;
|
||||
|
||||
/// `UU_VIDEO_ENCODER=cpu` keeps the virtual CPU display entirely off the GPU.
|
||||
pub fn gpu_encoder_requested() -> bool {
|
||||
std::env::var("UU_VIDEO_ENCODER").map_or(true, |v| v != "cpu")
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub(crate) enum Encoder { Cpu, Nvenc, Vaapi }
|
||||
impl Encoder {
|
||||
pub(crate) fn requested() -> Self { Self::from_setting(std::env::var("UU_VIDEO_ENCODER").ok().as_deref()) }
|
||||
fn from_setting(value: Option<&str>) -> Self {
|
||||
match value { Some("cpu") => Self::Cpu, Some("vaapi") => Self::Vaapi, _ => Self::Nvenc }
|
||||
}
|
||||
pub(crate) fn name(self) -> &'static str { match self { Self::Cpu => "libx264", Self::Nvenc => "h264_nvenc", Self::Vaapi => "h264_vaapi" } }
|
||||
}
|
||||
pub(crate) fn vaapi_device() -> String { std::env::var("UU_VAAPI_DEVICE").unwrap_or_else(|_| "/dev/dri/renderD128".into()) }
|
||||
|
||||
/// NV12 conversion must precede upload; scaling runs on software frames, before hwupload.
|
||||
fn vaapi_filter(output: Option<(u32, u32)>) -> String {
|
||||
let scale = output.map_or(String::new(), |(w,h)| format!("scale={w}:{h}:flags=bilinear,"));
|
||||
format!("{scale}format=nv12,hwupload")
|
||||
}
|
||||
fn vaapi_options(settings: &VideoSettings, gop: &str) -> Vec<String> {
|
||||
let (maxrate, buffer) = rate_args(settings);
|
||||
// QVBR is not uniformly available on Intel/AMD drivers. VBR + qmin sets a quality
|
||||
// floor while retaining the viewer's bitrate cap; unsupported drivers fall back to CPU.
|
||||
["-c:v", "h264_vaapi", "-profile:v", "constrained_baseline", "-g", gop, "-bf", "0",
|
||||
"-rc_mode", "VBR", "-b:v", &settings.max_kbps().map_or("16M".into(), |k| format!("{}k",k*3/4)),
|
||||
"-maxrate", &maxrate, "-bufsize", &buffer, "-qmin", &settings.cq().to_string(), "-async_depth", "1", "-aud", "1"]
|
||||
.into_iter().map(str::to_owned).collect()
|
||||
}
|
||||
|
||||
/// Round UP (+1ns float guard) so webrtc's floor(duration*90000) yields exactly `ticks`.
|
||||
@@ -121,12 +142,13 @@ async fn read_encoder(
|
||||
settings: VideoSettings, sequence: Arc<std::sync::atomic::AtomicU64>, idr: Arc<AtomicBool>,
|
||||
) -> Result<(), String> {
|
||||
// The sequence is shared so a CPU fallback or settings restart continues it (skip statistics stay correct).
|
||||
if !gpu_encoder_requested() { return read_encoder_with(size,display,portal,latest,false,sequence,settings,idr).await; }
|
||||
match read_encoder_with(size,display.clone(),portal.clone(),latest.clone(),true,sequence.clone(),settings,idr.clone()).await {
|
||||
let encoder = Encoder::requested();
|
||||
if encoder == Encoder::Cpu { return read_encoder_with(size,display,portal,latest,Encoder::Cpu,sequence,settings,idr).await; }
|
||||
match read_encoder_with(size,display.clone(),portal.clone(),latest.clone(),encoder,sequence.clone(),settings,idr.clone()).await {
|
||||
Ok(())=>Ok(()),
|
||||
Err(error) if !latest.is_closed()=>{
|
||||
log::warn!("NVENC stream unavailable; switching scoped encoder to CPU: {}",error);
|
||||
read_encoder_with(size,display,portal,latest,false,sequence,settings,idr).await
|
||||
log::warn!("{} stream unavailable; switching scoped encoder to CPU: {}",encoder.name(),error);
|
||||
read_encoder_with(size,display,portal,latest,Encoder::Cpu,sequence,settings,idr).await
|
||||
},
|
||||
Err(error)=>Err(error),
|
||||
}
|
||||
@@ -167,10 +189,16 @@ fn rate_args(settings: &VideoSettings) -> (String, String) {
|
||||
}
|
||||
|
||||
async fn read_encoder_with(
|
||||
size: (u32,u32),display:String,portal:Option<Arc<crate::portal::PortalSession>>,latest:mpsc::Sender<Arc<CompleteFrame>>,gpu:bool,sequence:Arc<std::sync::atomic::AtomicU64>,
|
||||
size: (u32,u32),display:String,portal:Option<Arc<crate::portal::PortalSession>>,latest:mpsc::Sender<Arc<CompleteFrame>>,encoder:Encoder,sequence:Arc<std::sync::atomic::AtomicU64>,
|
||||
mut settings: VideoSettings, #[cfg_attr(not(feature = "inproc-av"), allow(unused_variables))] idr: Arc<AtomicBool>,
|
||||
)->Result<(),String>{
|
||||
let wayland=display.starts_with("wayland:");
|
||||
let kms=display.starts_with("kms:");
|
||||
let wayland=kms || display.starts_with("wayland:");
|
||||
let kms_capture=if kms {
|
||||
let capture=tokio::task::spawn_blocking(crate::kms::Capture::open).await.map_err(|_|"KMS worker failed")??;
|
||||
if (capture.width,capture.height)!=size { return Err("KMS mode changed; restart host to refresh desktop geometry".into()); }
|
||||
Some(Arc::new(capture))
|
||||
} else { None };
|
||||
#[cfg(feature = "inproc-av")]
|
||||
if !wayland && portal.is_none() && crate::inproc_encoder::selected() {
|
||||
let max_kbps = settings.max_kbps().unwrap_or(30_000) as i64;
|
||||
@@ -178,11 +206,11 @@ async fn read_encoder_with(
|
||||
display: display.clone(), input: size, output: settings.output, fps: u32::from(settings.fps),
|
||||
gop: gop_frames(settings.fps, true) as u32, cq: settings.cq() as u32,
|
||||
target_bps: settings.max_kbps().map_or(16_000, |k| i64::from(k) * 3 / 4) * 1000,
|
||||
max_bps: max_kbps * 1000, buffer_bits: (max_kbps / 8).clamp(100, 500) * 1000, gpu,
|
||||
max_bps: max_kbps * 1000, buffer_bits: (max_kbps / 8).clamp(100, 500) * 1000, encoder,
|
||||
damage: crate::inproc_encoder::damage_enabled(),
|
||||
};
|
||||
log::info!("Desktop encoder: display={} source={}x{} output={:?} fps={} cq={} maxrate={}k encoder={} pipeline=in-process",
|
||||
display,size.0,size.1,settings.output,settings.fps,config.cq,max_kbps,if gpu{"h264_nvenc"}else{"libx264"});
|
||||
display,size.0,size.1,settings.output,settings.fps,config.cq,max_kbps,encoder.name());
|
||||
return read_inprocess(config, latest, sequence, idr).await;
|
||||
}
|
||||
let size = format!("{}x{}", size.0, size.1);
|
||||
@@ -194,13 +222,14 @@ async fn read_encoder_with(
|
||||
let (maxrate, bufsize) = rate_args(&settings);
|
||||
let cq = settings.cq().to_string();
|
||||
let target = settings.max_kbps().map_or("16M".to_string(), |k| format!("{}k", k * 3 / 4));
|
||||
log::info!("Desktop encoder: display={} source={} output={:?} fps={} cq={} maxrate={} encoder={} low_latency=true",display,size,settings.output,settings.fps,cq,maxrate,if gpu{"h264_nvenc"}else{"libx264"});
|
||||
log::info!("Desktop encoder: display={} source={} output={:?} fps={} cq={} maxrate={} encoder={} low_latency=true",display,size,settings.output,settings.fps,cq,maxrate,encoder.name());
|
||||
// A bundled FFmpeg prefix (config ffmpeg_root / UU_FFMPEG_ROOT) or `ffmpeg` on PATH.
|
||||
let mut command=match crate::config::ffmpeg_root() {
|
||||
Some(root) => { let mut c=Command::new(root.join("bin/ffmpeg")); c.env("LD_LIBRARY_PATH",root.join("lib/x86_64-linux-gnu")); c }
|
||||
None => Command::new("ffmpeg"),
|
||||
};
|
||||
command.args(["-hide_banner","-loglevel","error","-nostdin"]);
|
||||
if encoder == Encoder::Vaapi { command.args(["-vaapi_device", &vaapi_device()]); }
|
||||
if wayland {
|
||||
command.args(["-f","rawvideo","-pixel_format","bgra","-video_size",&size,
|
||||
"-framerate",&fps,"-i","pipe:0","-an"]);
|
||||
@@ -210,10 +239,14 @@ async fn read_encoder_with(
|
||||
"-video_size",&size,"-i",&display,"-an"]);
|
||||
}
|
||||
// 其他分辨率: scale the captured desktop (the host display mode is left untouched).
|
||||
if let Some((width, height)) = settings.output {
|
||||
if encoder == Encoder::Vaapi {
|
||||
command.args(["-vf", &vaapi_filter(settings.output)]);
|
||||
} else if let Some((width, height)) = settings.output {
|
||||
command.args(["-vf", &format!("scale={width}:{height}:flags=bilinear")]);
|
||||
}
|
||||
if gpu{
|
||||
if encoder == Encoder::Vaapi {
|
||||
command.args(vaapi_options(&settings, &gop));
|
||||
} else if encoder == Encoder::Nvenc {
|
||||
command.args(["-c:v","h264_nvenc","-preset","p4","-tune","ull",
|
||||
"-profile:v","baseline","-pix_fmt","yuv420p","-g",&gop,"-bf","0",
|
||||
"-rc","vbr","-cq",&cq,"-b:v",&target,"-maxrate",&maxrate,"-bufsize",&bufsize,
|
||||
@@ -237,7 +270,23 @@ async fn read_encoder_with(
|
||||
.stderr(std::process::Stdio::inherit())
|
||||
.spawn().map_err(|e| format!("Start scoped encoder: {e}"))?;
|
||||
// Scope capture to this encoder. Dropping either task kills its child.
|
||||
let _capture=if let Some(session)=portal {
|
||||
let _capture=if let Some(capture)=kms_capture {
|
||||
let mut sink=child.stdin.take().ok_or("Encoder stdin missing")?;
|
||||
Some(EncoderTask(tokio::spawn(async move {
|
||||
use tokio::io::AsyncWriteExt;
|
||||
let mut tick=tokio::time::interval(Duration::from_nanos(1_000_000_000/u64::from(settings.fps)));
|
||||
tick.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
|
||||
loop {
|
||||
tick.tick().await;
|
||||
let capture=capture.clone();
|
||||
let bytes=tokio::task::spawn_blocking(move || {
|
||||
let mut bytes=vec![0;capture.width as usize*capture.height as usize*4];
|
||||
capture.capture(&mut bytes)?; Ok::<_,String>(bytes)
|
||||
}).await.map_err(|_|"KMS worker failed")??;
|
||||
sink.write_all(&bytes).await.map_err(|_|"KMS encoder pipe failed")?;
|
||||
}
|
||||
})))
|
||||
} else if let Some(session)=portal {
|
||||
let mut sink=child.stdin.take().ok_or("Encoder stdin missing")?;
|
||||
Some(EncoderTask(tokio::spawn(async move {
|
||||
use tokio::io::AsyncWriteExt;
|
||||
@@ -403,6 +452,20 @@ pub async fn stream_desktop(pc: Arc<RTCPeerConnection>, track: Arc<TrackLocalSta
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn vaapi_is_explicit_and_upload_follows_scale() {
|
||||
assert_eq!(Encoder::from_setting(None), Encoder::Nvenc);
|
||||
assert_eq!(Encoder::from_setting(Some("cpu")), Encoder::Cpu);
|
||||
assert_eq!(Encoder::from_setting(Some("vaapi")), Encoder::Vaapi);
|
||||
assert_eq!(vaapi_filter(None), "format=nv12,hwupload");
|
||||
assert_eq!(vaapi_filter(Some((1280,720))), "scale=1280:720:flags=bilinear,format=nv12,hwupload");
|
||||
let options = vaapi_options(&VideoSettings::default(), "30");
|
||||
for pair in [["-g","30"], ["-bf","0"], ["-async_depth","1"], ["-aud","1"]] {
|
||||
assert!(options.windows(2).any(|w| w[0] == pair[0] && w[1] == pair[1]));
|
||||
}
|
||||
assert!(!options.iter().any(|v| v == "-r" || v == "-pix_fmt"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn encoder_restart_stops_old_producer_before_starting_new_chain() {
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
|
||||
@@ -0,0 +1,181 @@
|
||||
//! Scoped PulseAudio / PipeWire-pulse routing. Never inspect or log audio payloads.
|
||||
//! `pactl` is used when present; plain PipeWire installs without it (default Ubuntu desktop)
|
||||
//! use `pw-cli`/`pw-dump`/`wpctl`, where WirePlumber moves default-following streams itself.
|
||||
use std::process::{Command, Stdio};
|
||||
use std::sync::{Mutex, OnceLock};
|
||||
use std::os::fd::AsRawFd;
|
||||
const SINK: &str = "uu-remote-sink";
|
||||
const MONITOR: &str = "uu-remote-sink.monitor";
|
||||
#[derive(serde::Serialize, serde::Deserialize)]
|
||||
struct Route { previous: String, module: Option<u32>, #[serde(default)] pipewire: bool }
|
||||
struct State { route: Option<Route>, _lock: std::fs::File }
|
||||
static STATE: OnceLock<Mutex<Option<State>>> = OnceLock::new();
|
||||
fn path() -> std::path::PathBuf { crate::config::path().with_file_name("virtual_audio.json") }
|
||||
fn pactl(args: &[&str]) -> Result<String, ()> { run("pactl", args) }
|
||||
fn run(program: &str, args: &[&str]) -> Result<String, ()> {
|
||||
let out = Command::new(program).args(args).env("LC_ALL","C").stdin(Stdio::null()).stderr(Stdio::null()).output().map_err(|_| ())?;
|
||||
if !out.status.success() { return Err(()); }
|
||||
String::from_utf8(out.stdout).map(|s|s.trim().to_owned()).map_err(|_| ())
|
||||
}
|
||||
fn pipewire_only() -> bool { pactl(&["info"]).is_err() && run("wpctl",&["status"]).is_ok() }
|
||||
/// Node ids whose node.name is `name`; `null_sink` restricts to null-audio-sink nodes (ownership).
|
||||
fn pw_nodes(dump: &str, name: &str, null_sink: bool) -> Vec<u32> {
|
||||
let Ok(serde_json::Value::Array(objects)) = serde_json::from_str(dump) else { return Vec::new() };
|
||||
objects.iter().filter_map(|o| {
|
||||
let props = o.get("info")?.get("props")?;
|
||||
if props.get("node.name")?.as_str()? != name { return None; }
|
||||
if null_sink && props.get("factory.name").and_then(|v| v.as_str()) != Some("support.null-audio-sink") { return None; }
|
||||
u32::try_from(o.get("id")?.as_u64()?).ok()
|
||||
}).collect()
|
||||
}
|
||||
fn pw_node(name: &str, null_sink: bool) -> Option<u32> { pw_nodes(&run("pw-dump",&["Node"]).ok()?, name, null_sink).first().copied() }
|
||||
fn default_sink(pipewire: bool) -> Result<String, ()> {
|
||||
if !pipewire { return pactl(&["get-default-sink"]); }
|
||||
run("wpctl",&["inspect","@DEFAULT_AUDIO_SINK@"])?.lines()
|
||||
.find_map(|l| l.trim().trim_start_matches("* ").strip_prefix("node.name = ").map(|v| v.trim_matches('"').to_owned())).ok_or(())
|
||||
}
|
||||
fn set_default(pipewire: bool, name: &str) -> Result<(), ()> {
|
||||
if !pipewire { return pactl(&["set-default-sink",name]).map(|_| ()); }
|
||||
run("wpctl",&["set-default",&pw_node(name,false).ok_or(())?.to_string()]).map(|_| ())
|
||||
}
|
||||
fn owned_modules(list: &str) -> Vec<u32> {
|
||||
list.lines().filter_map(|line| {
|
||||
let mut fields = line.split('\t');
|
||||
let id = fields.next()?.parse().ok()?;
|
||||
if fields.next()? != "module-null-sink" { return None; }
|
||||
fields.next()?.split_whitespace().any(|arg| arg == "sink_name=uu-remote-sink").then_some(id)
|
||||
}).collect()
|
||||
}
|
||||
fn sink_id(name: &str) -> Option<String> {
|
||||
pactl(&["list","short","sinks"]).ok()?.lines().find_map(|line| {
|
||||
let mut f=line.split_whitespace(); let id=f.next()?; (f.next()? == name).then(|| id.to_owned())
|
||||
})
|
||||
}
|
||||
fn move_inputs(from: &str, to: &str) {
|
||||
let Some(id)=sink_id(from) else { return };
|
||||
if let Ok(list)=pactl(&["list","short","sink-inputs"]) {
|
||||
for line in list.lines() {
|
||||
let mut f=line.split_whitespace();
|
||||
if let (Some(input),Some(sink))=(f.next(),f.next()) {
|
||||
if sink==id { let _=pactl(&["move-sink-input",input,to]); }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
fn persist(route: &Route) -> Result<(), ()> {
|
||||
use std::os::unix::fs::OpenOptionsExt;
|
||||
use std::io::Write;
|
||||
let temp=path().with_extension("json.new");
|
||||
let mut file=std::fs::OpenOptions::new().write(true).create(true).truncate(true).mode(0o600).open(&temp).map_err(|_| ())?;
|
||||
file.write_all(&serde_json::to_vec(route).map_err(|_| ())?).map_err(|_| ())?;
|
||||
file.sync_all().map_err(|_| ())?;
|
||||
std::fs::rename(temp,path()).map_err(|_| ())
|
||||
}
|
||||
fn recover(route: &Route) -> Result<(), ()> {
|
||||
// Do not overwrite a default explicitly changed by the user during remote control.
|
||||
// The previous output may be gone (unplugged USB card): still unload our sink so the server
|
||||
// picks another default instead of leaving every stream on a null sink.
|
||||
let pw=route.pipewire;
|
||||
if default_sink(pw).is_ok_and(|s| s == SINK) && set_default(pw,&route.previous).is_err() {
|
||||
log::warn!("Previous default audio output unavailable; the sound server will choose a new default");
|
||||
}
|
||||
if pw {
|
||||
for id in pw_nodes(&run("pw-dump",&["Node"])?,SINK,true) { run("pw-cli",&["destroy",&id.to_string()])?; }
|
||||
} else {
|
||||
move_inputs(SINK,&route.previous);
|
||||
let modules=owned_modules(&pactl(&["list","short","modules"])?);
|
||||
for id in modules { pactl(&["unload-module",&id.to_string()])?; }
|
||||
}
|
||||
let _=std::fs::remove_file(path());
|
||||
Ok(())
|
||||
}
|
||||
fn state() -> &'static Mutex<Option<State>> {
|
||||
STATE.get_or_init(|| {
|
||||
use std::os::unix::fs::OpenOptionsExt;
|
||||
let init=(|| {
|
||||
std::fs::create_dir_all(path().parent().ok_or(())?).map_err(|_| ())?;
|
||||
let lock=std::fs::OpenOptions::new().read(true).write(true).create(true).truncate(false).mode(0o600)
|
||||
.open(path().with_extension("lock")).map_err(|_| ())?;
|
||||
if unsafe { libc::flock(lock.as_raw_fd(),libc::LOCK_EX|libc::LOCK_NB) } != 0 { return Err(()); }
|
||||
let route=if path().exists() {
|
||||
let old:Route=serde_json::from_slice(&std::fs::read(path()).map_err(|_| ())?).map_err(|_| ())?;
|
||||
if recover(&old).is_err() { Some(old) } else { None }
|
||||
} else { None };
|
||||
Ok(State { route, _lock:lock })
|
||||
})();
|
||||
Mutex::new(init.ok())
|
||||
})
|
||||
}
|
||||
/// Called on startup even if mute is disabled, to recover a previous crash.
|
||||
pub fn init() { let _=state(); }
|
||||
pub fn enable() -> bool {
|
||||
let mut guard=state().lock().unwrap();
|
||||
let Some(state)=guard.as_mut() else { return false };
|
||||
if state.route.is_some() { return pactl(&["get-default-sink"]).is_ok_and(|s|s==SINK); }
|
||||
let result=(|| {
|
||||
let pipewire=pipewire_only();
|
||||
let previous=default_sink(pipewire)?;
|
||||
if previous==SINK { return Err(()); }
|
||||
// Refuse to claim another instance's or manually-created sink.
|
||||
if if pipewire { pw_node(SINK,false).is_some() } else { sink_id(SINK).is_some() } { return Err(()); }
|
||||
let mut route=Route { previous, module:None, pipewire };
|
||||
persist(&route)?; // Save before loading: also covers a crash between load and ID persistence.
|
||||
state.route=Some(Route { previous:route.previous.clone(), module:None, pipewire });
|
||||
let id=if pipewire {
|
||||
// object.linger keeps the node after pw-cli exits; recover() destroys it.
|
||||
run("pw-cli",&["create-node","adapter","{ factory.name=support.null-audio-sink node.name=uu-remote-sink \
|
||||
node.description=UU-Remote media.class=Audio/Sink object.linger=true audio.position=[FL FR] }"])?;
|
||||
let mut node=None;
|
||||
for _ in 0..20 { node=pw_node(SINK,true); if node.is_some() { break; } std::thread::sleep(std::time::Duration::from_millis(50)); }
|
||||
node.ok_or(())?
|
||||
} else {
|
||||
pactl(&["load-module","module-null-sink","sink_name=uu-remote-sink","sink_properties=device.description=UU-Remote"])?.parse().map_err(|_| ())?
|
||||
};
|
||||
route.module=Some(id);
|
||||
persist(&route)?;
|
||||
state.route=Some(route);
|
||||
set_default(pipewire,SINK)?;
|
||||
if !pipewire { move_inputs(&state.route.as_ref().ok_or(())?.previous,SINK); }
|
||||
Ok(())
|
||||
})();
|
||||
if result.is_err() {
|
||||
if let Some(route)=state.route.as_ref() { if recover(route).is_ok() { state.route=None; } }
|
||||
log::warn!("Virtual audio routing unavailable; using the existing mute/monitor path");
|
||||
}
|
||||
result.is_ok()
|
||||
}
|
||||
pub fn disable() {
|
||||
let mut guard=state().lock().unwrap();
|
||||
if let Some(state)=guard.as_mut() {
|
||||
if let Some(route)=state.route.as_ref() {
|
||||
if recover(route).is_ok() { state.route=None; }
|
||||
else { log::warn!("Virtual audio restore incomplete; recovery record retained"); }
|
||||
}
|
||||
}
|
||||
}
|
||||
pub fn active() -> bool { state().lock().unwrap().as_ref().is_some_and(|s|s.route.is_some()) }
|
||||
pub fn source() -> String {
|
||||
if state().lock().unwrap().as_ref().is_some_and(|s|s.route.as_ref().is_some_and(|r|r.module.is_some())) { MONITOR.into() }
|
||||
else { std::env::var("UU_AUDIO_SOURCE").unwrap_or_else(|_| "@DEFAULT_MONITOR@".into()) }
|
||||
}
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test] fn recovery_only_claims_exact_null_sink() {
|
||||
let modules="1\tmodule-null-sink\tsink_name=uu-remote-sink\t0\n2\tmodule-null-sink\tsink_name=uu-remote-sink-other\t0\n3\tmodule-other\tsink_name=uu-remote-sink\t0\n";
|
||||
assert_eq!(owned_modules(modules),vec![1]);
|
||||
}
|
||||
#[test] fn pipewire_recovery_only_claims_owned_null_sink() {
|
||||
let dump=r#"[{"id":5,"info":{"props":{"node.name":"uu-remote-sink","factory.name":"support.null-audio-sink"}}},
|
||||
{"id":6,"info":{"props":{"node.name":"uu-remote-sink","factory.name":"api.alsa.pcm.sink"}}},
|
||||
{"id":7,"info":{"props":{"node.name":"other","factory.name":"support.null-audio-sink"}}},{"id":8}]"#;
|
||||
assert_eq!(pw_nodes(dump,"uu-remote-sink",true),vec![5]);
|
||||
assert_eq!(pw_nodes(dump,"uu-remote-sink",false),vec![5,6]);
|
||||
let old:Route=serde_json::from_str(r#"{"previous":"a","module":3}"#).unwrap();
|
||||
assert!(!old.pipewire);
|
||||
}
|
||||
}
|
||||
|
||||
/// Also restore on ordinary returns from the host; SIGKILL is recovered next startup.
|
||||
pub struct Cleanup;
|
||||
impl Drop for Cleanup { fn drop(&mut self) { disable(); } }
|
||||
+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