Compare commits

...
24 Commits
Author SHA1 Message Date
VallierDevandClaude Opus 5.5 46589b3fac Release v0.1.7
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-15 11:24:22 +08:00
VallierDevandClaude Opus 5.5 708d9de2de Docs: record Intel VAAPI hardware acceptance; AMD and inproc VAAPI remain unverified
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-15 11:22:34 +08:00
VallierDevandClaude Opus 5.5 ebe6a7e19a Clarify mute logs with the virtual sink; document the PipeWire fallback
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-15 11:22:20 +08:00
VallierDevandClaude Opus 5.5 09f7169f62 Virtual audio without pactl: PipeWire-native null sink
Default Ubuntu desktops ship PipeWire/WirePlumber without pulseaudio-utils,
so the pactl-only route silently fell back to plain muting. When pactl is
missing, create the null sink with pw-cli (object.linger), switch the default
with wpctl and destroy only our own null-audio-sink node on restore;
WirePlumber moves default-following streams itself. Recovery records carry
the backend (old records default to pactl).

Verified on Ubuntu 24.04 (PipeWire 1.0.5, WirePlumber 0.4.17): a playing
stream moves to the sink and back, the monitor is readable, SIGKILL leftovers
are recovered on the next start.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-15 11:16:11 +08:00
VallierDevandClaude Opus 5.5 9125e51350 Review fixes: tolerate a vanished default sink, recover stale virtual displays
- virtual_audio: when the previous default output no longer exists, still
  unload the null sink instead of keeping all output on it.
- uu-virtual-desktop: a socket/lock left by a dead Xvfb no longer blocks the
  service forever (Restart=always loop); a live owner is still refused.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-15 11:03:42 +08:00
VallierDev 0da79a45eb Record VM acceptance results and add guarded synthetic audio fixture 2026-10-15 10:56:54 +08:00
VallierDev d461ef9bed Add opt-in linear KMS capture prototype with explicit security limitations 2026-10-15 10:49:12 +08:00
VallierDev f123f0ba05 Document Arch and Omarchy setup and exercise native desktop input and clipboard 2026-10-15 10:48:45 +08:00
VallierDev 90c33e173c Supervise authenticated virtual desktops on the linger user bus 2026-10-15 10:48:07 +08:00
VallierDev ac0f2bf2c6 Route muted sessions through a recoverable Pulse null sink 2026-10-15 10:47:31 +08:00
VallierDev 8a8a4c165f Add explicit VAAPI encoding with CPU fallback in both pipelines 2026-10-15 10:28:24 +08:00
VallierDevandClaude Opus 5.5 7beb3aecf0 Release v0.1.6
Docs for headless mode, virtual desktop, uinput udev rule and terminal
shortcuts; release tarball ships contrib/.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-15 10:15:59 +08:00
VallierDevandClaude Opus 5.5 26d4812480 Headless mode, virtual desktop scripts, tool peers without a desktop
- UU_DESKTOP_BACKEND=none: host goes online without a display; terminal, file
  transfer and port mapping stay available, desktop offers are ignored.
- Tool peers (file/terminal/port mapping) get a nominal ScreenSources/CaptureChange
  and Allow permissions when no desktop is available: the port-mapping viewer
  aborts otherwise (also affected portal hosts before local consent).
- x11_engine: an explicit DISPLAY that cannot be opened no longer falls back to
  :0/:1 (would silently capture the physical screen).
- contrib/virtual-desktop: Xvfb + desktop session on the systemd user bus
  (or a private dbus-run-session bus) and a uuremote-native drop-in.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-15 10:13:58 +08:00
VallierDevandClaude Opus 5.5 49edf1d389 uinput input backend and terminal-aware paste
- uinput.rs: kernel virtual keyboard + absolute pointer (Sunshine-style),
  physical evdev keycodes so the compositor's keysym lookup cannot drop
  shifted symbols (#3). Auto on Wayland/portal when /dev/uinput is
  accessible (udev uaccess rule), XTest kept on X11 (it suppresses
  autorepeat for held keys over latency). UU_INPUT_BACKEND=auto|uinput|native.
- Mac viewers send Cmd+V as Ctrl+V (verified: Cmd and Ctrl both arrive as
  VK_LCONTROL). In a focused X11 terminal Ctrl+V becomes Ctrl+Shift+V so it
  pastes instead of quoting the next key; Ctrl+C always interrupts (copy is
  Cmd+Shift+C). UU_TERMINAL_SHORTCUTS=0 disables.

Verified on the 3090 with the official Mac viewer: paste, interrupt,
uinput typing incl. underscores.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-15 09:49:03 +08:00
VallierDevandClaude Opus 5.5 ddbc75bbf6 Release v0.1.5
Room reconnect-key keepalive (fixes ~20 min cloud release), XDamage on
the opt-in in-process path, portal clipboard documented.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-15 08:27:42 +08:00
VallierDev 9fbe7d63c5 Recognize delayed reconnect-key refresh acknowledgements without regressing keys 2026-10-15 08:25:12 +08:00
VallierDev 3a78491f25 Close stale signaling writers even when live peer callbacks retain the sender 2026-10-15 08:25:12 +08:00
VallierDev fc8c10cde9 Refresh official room reconnect key periodically and stop obsolete signaling receivers 2026-10-15 08:25:12 +08:00
VallierDev 125baa8c8b Use GNOME session idle inhibition during isolated long-connection acceptance 2026-10-15 08:25:12 +08:00
VallierDev c5aa238f73 Distinguish cloud release events and trace signaling heartbeat progress without credentials 2026-10-15 08:25:12 +08:00
VallierDev 3673d37c1e Keep official viewer focus during marker-only reverse clipboard acceptance 2026-10-15 08:25:12 +08:00
ferstar 59a45e092b Address review on the XDamage capture PR
- Viewer PLI/FIR: dedupe only while an IDR is pending; drop the 1 s gap so a lost IDR cannot block recovery.
- XShm: release the local mapping on every attach failure; mark the segment for removal in all cases.
- Damage events no longer defer an overdue capture.
- After idle, the next tick is one frame after now instead of now.
2026-10-15 08:22:14 +08:00
ferstar a870f7d041 Limit viewer keyframe requests to one per second and none while one is pending
Accepted PLI/FIR feedback now goes through KeyframeGate: at most one request per
second, and none while the previous request still waits for the encoder.
2026-10-15 08:22:14 +08:00
ferstar 367be30a0f Capture only changed frames with XDamage on the in-process path
The in-process encoder can now capture through XShm and XDamage in pure Rust
(x11rb) instead of libav's x11grab. Damage marks the picture dirty; a dirty
picture is captured and encoded on a fixed frame tick so the stream keeps a
steady cadence. An idle screen sends a keepalive every 40-67 ms (15-25 fps,
random) so the viewer does not treat a static desktop as stalled.

- src/damage_capture.rs: XDamage NonEmpty reports and XShm copy-out
- src/inproc_encoder.rs: run_damage loop; x11grab stays as the fallback
- UU_CAPTURE=grab forces the x11grab path
- x11rb is an optional dependency, enabled by the inproc-av feature only

Measured on a static desktop, CPU drops from about 12% to about 1% versus
x11grab. Moving content costs about the same CPU as x11grab.
2026-10-15 08:22:14 +08:00
34 changed files with 1963 additions and 109 deletions

No files matched your search

+1
View File
@@ -26,6 +26,7 @@ jobs:
name=uu-remote-linux-${GITHUB_REF_NAME}-x86_64-linux name=uu-remote-linux-${GITHUB_REF_NAME}-x86_64-linux
mkdir -p "$name" mkdir -p "$name"
cp target/release/uuremote-server LICENSE README.md README.en.md config.example.json "$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" tar czf "$name.tar.gz" "$name"
sha256sum "$name.tar.gz" > "$name.tar.gz.sha256" sha256sum "$name.tar.gz" > "$name.tar.gz.sha256"
- uses: softprops/action-gh-release@v2 - uses: softprops/action-gh-release@v2
+74 -3
View File
@@ -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/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 | | `~/.config/uuremote/host_settings.json` | persisted 安全 settings: auto_lock, keep_mute, mute_permanent |
| `UU_DESKTOP_BACKEND` | `auto` (default) / `x11` / `wayland` / `portal` | | `UU_DESKTOP_BACKEND` | `auto` (default) / `x11` / `wayland` / `portal` / `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_VIDEO_ENCODER=cpu` | force libx264 instead of NVENC | | `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_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_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 | | `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/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/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/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/cursor.rs` | pointer shape mirroring (X11) |
| `src/clipboard*.rs` | text clipboard bridge and protocol | | `src/clipboard*.rs` | text clipboard bridge and protocol |
| `src/file_trans.rs` | FileTransferFtp RPC | | `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 - 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. 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. - 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. - 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. - 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 - 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. - 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 - Update README.md / README.en.md when user-visible features or limitations change, and add
`docs/release-notes/<tag>.md` before tagging a release. `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
+56 -1
View File
@@ -111,6 +111,12 @@ dependencies = [
"rustversion", "rustversion",
] ]
[[package]]
name = "as-raw-xcb-connection"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "175571dd1d178ced59193a6fc02dde1b972eb0bc56c892cde9beeceac5bf0f6b"
[[package]] [[package]]
name = "asn1-rs" name = "asn1-rs"
version = "0.5.2" version = "0.5.2"
@@ -801,6 +807,16 @@ dependencies = [
"zeroize", "zeroize",
] ]
[[package]]
name = "gethostname"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1bd49230192a3797a9a4d6abe9b3eed6f7fa4c8a8a4947977c6f80025f92cbd8"
dependencies = [
"rustix",
"windows-link",
]
[[package]] [[package]]
name = "getrandom" name = "getrandom"
version = "0.2.17" version = "0.2.17"
@@ -1210,6 +1226,12 @@ dependencies = [
"windows-link", "windows-link",
] ]
[[package]]
name = "linux-raw-sys"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32a66949e030da00e8c7d4434b251670a91556f4144941d37452769c25d58a53"
[[package]] [[package]]
name = "litemap" name = "litemap"
version = "0.8.3" version = "0.8.3"
@@ -1885,6 +1907,19 @@ dependencies = [
"nom", "nom",
] ]
[[package]]
name = "rustix"
version = "1.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "891efababe418670775f199f0d233d84843c227a0949a883ce15b37c78d6629d"
dependencies = [
"bitflags 2.13.2",
"errno",
"libc",
"linux-raw-sys",
"windows-sys 0.61.2",
]
[[package]] [[package]]
name = "rustls" name = "rustls"
version = "0.21.12" version = "0.21.12"
@@ -2614,7 +2649,7 @@ checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
[[package]] [[package]]
name = "uu-remote-linux" name = "uu-remote-linux"
version = "0.1.4" version = "0.1.7"
dependencies = [ dependencies = [
"base64 0.22.1", "base64 0.22.1",
"bytes", "bytes",
@@ -2636,6 +2671,7 @@ dependencies = [
"tokio", "tokio",
"tokio-tungstenite", "tokio-tungstenite",
"webrtc", "webrtc",
"x11rb",
] ]
[[package]] [[package]]
@@ -3094,6 +3130,25 @@ version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ad82d2a33cdc9674dc7465672f271e096168fcdbe0f799d9e6db8c5892679dc" checksum = "3ad82d2a33cdc9674dc7465672f271e096168fcdbe0f799d9e6db8c5892679dc"
[[package]]
name = "x11rb"
version = "0.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9993aa5be5a26815fe2c3eacfc1fde061fc1a1f094bf1ad2a18bf9c495dd7414"
dependencies = [
"as-raw-xcb-connection",
"gethostname",
"libc",
"rustix",
"x11rb-protocol",
]
[[package]]
name = "x11rb-protocol"
version = "0.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ea6fc2961e4ef194dcbfe56bb845534d0dc8098940c7e5c012a258bfec6701bd"
[[package]] [[package]]
name = "x25519-dalek" name = "x25519-dalek"
version = "2.0.1" version = "2.0.1"
+5 -2
View File
@@ -1,6 +1,6 @@
[package] [package]
name = "uu-remote-linux" name = "uu-remote-linux"
version = "0.1.4" version = "0.1.7"
edition = "2021" edition = "2021"
authors = ["uu-remote-linux contributors"] authors = ["uu-remote-linux contributors"]
description = "Unofficial Linux host for NetEase UU Remote: control a Linux desktop from the official clients" description = "Unofficial Linux host for NetEase UU Remote: control a Linux desktop from the official clients"
@@ -16,10 +16,12 @@ path = "src/main.rs"
[features] [features]
default = [] default = []
wayland = [] wayland = []
# Experimental direct linear framebuffer capture; never enabled in release builds.
kms = []
portal = [] portal = []
# In-process capture and encoding through FFmpeg 6 libraries (libavcodec/libavdevice dev packages). # 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. # Off by default so release binaries do not link the build machine's libav.
inproc-av = ["dep:ffmpeg-next"] inproc-av = ["dep:ffmpeg-next", "dep:x11rb"]
[dependencies] [dependencies]
tokio = { version = "1", features = ["full"] } tokio = { version = "1", features = ["full"] }
@@ -41,6 +43,7 @@ log = "0.4"
qrcode = { version = "0.14", default-features = false } qrcode = { version = "0.14", default-features = false }
env_logger = "0.11" env_logger = "0.11"
ffmpeg-next = { version = "6.1", optional = true, default-features = false, features = ["codec", "format", "device", "software-scaling", "software-resampling"] } ffmpeg-next = { version = "6.1", optional = true, default-features = false, features = ["codec", "format", "device", "software-scaling", "software-resampling"] }
x11rb = { version = "0.13", features = ["damage", "shm", "allow-unsafe-code"], optional = true }
[build-dependencies] [build-dependencies]
cc = "1.0" cc = "1.0"
+117 -6
View File
@@ -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 | | 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 | | 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) | | Pointer shape | Remote cursor shape is mirrored to the viewer (X11) |
| Clipboard | Two-way text sync | | 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 | | 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 | | 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 | | 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 | | 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 ## Supported Linux desktops
@@ -62,8 +64,8 @@ Apart from GNOME on X11, these desktops have been tested in virtual machines so
real hardware, please [open an issue](../../issues) with your distribution, desktop, GPU and the relevant lines from real hardware, please [open an issue](../../issues) with your distribution, desktop, GPU and the relevant lines from
`journalctl --user -u uuremote-native.service`. `journalctl --user -u uuremote-native.service`.
Wayland limits: single screen, fixed resolution. The portal backend does not support clipboard, screen wake or Wayland limits: single screen, fixed resolution. The portal backend does not support screen wake or
dynamic resolution yet. On the first connection the desktop asks for "Remote Desktop / Screen Sharing" dynamic resolution yet; the clipboard syncs while the viewer's remote window has focus. On the first connection the desktop asks for "Remote Desktop / Screen Sharing"
permission; the permission is remembered once granted. permission; the permission is remembered once granted.
## Not implemented / not supported ## Not implemented / not supported
@@ -77,7 +79,7 @@ permission; the permission is remembered once granted.
| Privacy screen on the host | Not implemented | | Privacy screen on the host | Not implemented |
| Microphone (viewer audio played on the host) | Not implemented (needs a virtual sound card) | | 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 | | 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 | | 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 | | 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. 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 ## Configuration
Common environment variables: Common environment variables:
@@ -156,8 +200,10 @@ Common environment variables:
| Variable | Purpose | | Variable | Purpose |
|---|---| |---|---|
| `UU_CONFIG` | Config file path (default `~/.config/uuremote/config.json`) | | `UU_CONFIG` | Config file path (default `~/.config/uuremote/config.json`) |
| `UU_DESKTOP_BACKEND` | `auto` / `x11` / `wayland` / `portal` | | `UU_DESKTOP_BACKEND` | `auto` / `x11` / `wayland` / `portal` / `kms` (experimental feature) / `none` (headless) |
| `UU_VIDEO_ENCODER=cpu` | Force CPU encoding | | `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_FFMPEG_ROOT` | Use FFmpeg from this prefix |
| `UU_AUDIO=0` | Disable sound; `UU_AUDIO_SOURCE` picks the PulseAudio source | | `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 | | `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. - 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. - [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. - 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.
+103 -5
View File
@@ -29,7 +29,8 @@
|---|---| |---|---|
| 扫码登录 | GTK 登录窗口(或 `login --no-gui` 终端二维码);二维码过期自动刷新;登录凭证失效时服务会自动弹出登录窗口 | | 扫码登录 | GTK 登录窗口(或 `login --no-gui` 终端二维码);二维码过期自动刷新;登录凭证失效时服务会自动弹出登录窗口 |
| 远程桌面 | H.264 视频,NVIDIA 显卡用 NVENC 硬件编码,否则用 libx264;查看端绘制本地鼠标,不会出现双光标 | | 远程桌面 | 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) | | 鼠标指针样式 | 远端指针形状实时同步(X11) |
| 剪贴板 | 双向文本同步 | | 剪贴板 | 双向文本同步 |
| 画质 / 帧率 / 分辨率 | 自动、原画 20M、超清 8M、高清 2M、自定义;30/60/90/144 帧;「其他分辨率」由编码器缩放,不改动主机显示器 | | 画质 / 帧率 / 分辨率 | 自动、原画 20M、超清 8M、高清 2M、自定义;30/60/90/144 帧;「其他分辨率」由编码器缩放,不改动主机显示器 |
@@ -42,6 +43,7 @@
| 系统操作 | 锁屏、显示桌面、任务管理器(系统监视器)、重启、关机 | | 系统操作 | 锁屏、显示桌面、任务管理器(系统监视器)、重启、关机 |
| 端口映射 | 官方「端口映射」工具:把控制端本地端口转发到 Linux 能访问到的地址(如 127.0.0.1:22);`UU_PORT_MAPPING=0` 可关闭 | | 端口映射 | 官方「端口映射」工具:把控制端本地端口转发到 Linux 能访问到的地址(如 127.0.0.1:22);`UU_PORT_MAPPING=0` 可关闭 |
| 多次重连 | 每个工具窗口独立连接,互不影响 | | 多次重连 | 每个工具窗口独立连接,互不影响 |
| 无头服务器 | 没有显示器也能上线,提供终端、文件传输和端口映射;也可以用 Xvfb 虚拟桌面提供完整远程桌面(见下文) |
## 支持的 Linux 桌面 ## 支持的 Linux 桌面
@@ -57,7 +59,7 @@
除 GNOME(X11)外,其余桌面目前都是在虚拟机里测试的。如果你在真机上遇到问题,欢迎[提交 issue](../../issues), 除 GNOME(X11)外,其余桌面目前都是在虚拟机里测试的。如果你在真机上遇到问题,欢迎[提交 issue](../../issues),
请附上发行版、桌面环境、显卡型号和 `journalctl --user -u uuremote-native.service` 里的相关日志。 请附上发行版、桌面环境、显卡型号和 `journalctl --user -u uuremote-native.service` 里的相关日志。
Wayland 下的限制:只支持单屏、固定分辨率;portal 后端暂不支持剪贴板、唤醒屏幕和动态分辨率。 Wayland 下的限制:只支持单屏、固定分辨率;portal 后端暂不支持唤醒屏幕和动态分辨率;剪贴板只在控制端的远控窗口处于前台时同步。
首次连接时,桌面会弹窗请求「远程桌面 / 屏幕共享」授权,同意后会记住授权。 首次连接时,桌面会弹窗请求「远程桌面 / 屏幕共享」授权,同意后会记住授权。
## 尚未实现 / 不支持 ## 尚未实现 / 不支持
@@ -71,7 +73,7 @@ Wayland 下的限制:只支持单屏、固定分辨率;portal 后端暂不
| 被控端防窥模式 | 未实现 | | 被控端防窥模式 | 未实现 |
| 麦克风(控制端声音传到被控端) | 未实现(需要虚拟声卡) | | 麦克风(控制端声音传到被控端) | 未实现(需要虚拟声卡) |
| 魔兽世界排队提醒等插件 | 不支持,会明确告诉客户端「无插件」 | | 魔兽世界排队提醒等插件 | 不支持,会明确告诉客户端「无插件」 |
| 登录前(登录界面 / 无人登录)访问 | 不支持,只能控制已登录的桌面会话 | | 登录前(登录界面 / 无人登录)访问 | 不支持控制登录界面;没有人登录时可以用无头模式或虚拟桌面(见下文) |
| 跨运营商 / 受限网络的中继连接 | 已接入官方下发的 ICE 服务器,但复杂网络环境未充分测试 | | 跨运营商 / 受限网络的中继连接 | 已接入官方下发的 ICE 服务器,但复杂网络环境未充分测试 |
| 作为控制端(在 Linux 上控制别的电脑) | 不在本项目范围 | | 作为控制端(在 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_CONFIG` | 配置文件路径(默认 `~/.config/uuremote/config.json`) |
| `UU_DESKTOP_BACKEND` | `auto` / `x11` / `wayland` / `portal` | | `UU_DESKTOP_BACKEND` | `auto` / `x11` / `wayland` / `portal` / `kms`(实验性特性) / `none`(无头模式) |
| `UU_VIDEO_ENCODER=cpu` | 强制使用 CPU 编码 | | `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_FFMPEG_ROOT` | 使用指定目录下的 FFmpeg |
| `UU_AUDIO=0` | 关闭声音转发;`UU_AUDIO_SOURCE` 指定 PulseAudio 采集源 | | `UU_AUDIO=0` | 关闭声音转发;`UU_AUDIO_SOURCE` 指定 PulseAudio 采集源 |
| `UU_LOGIN_WINDOW=0` | 凭证缺失时不自动弹出登录窗口 | | `UU_LOGIN_WINDOW=0` | 凭证缺失时不自动弹出登录窗口 |
@@ -201,3 +244,58 @@ flowchart LR
- 网易 UU 远程团队:做了一个好用的远程工具。 - 网易 UU 远程团队:做了一个好用的远程工具。
- [webrtc-rs](https://github.com/webrtc-rs/webrtc)、FFmpeg、GTK 等开源项目。 - [webrtc-rs](https://github.com/webrtc-rs/webrtc)、FFmpeg、GTK 等开源项目。
- 本项目的大部分代码在 Claude 和 Codex 的协助下完成。 - 本项目的大部分代码在 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 行为。
+10
View File
@@ -9,6 +9,16 @@ fn main() {
let target_os = std::env::var("CARGO_CFG_TARGET_OS").unwrap_or_default(); let target_os = std::env::var("CARGO_CFG_TARGET_OS").unwrap_or_default();
if target_os == "linux" { 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() cc::Build::new()
.file("c_engine/x11_engine.c") .file("c_engine/x11_engine.c")
.file("c_engine/clipboard.c") .file("c_engine/clipboard.c")
+100
View File
@@ -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
View File
@@ -9,6 +9,8 @@
#include <sys/ipc.h> #include <sys/ipc.h>
#include <X11/Xlib.h> #include <X11/Xlib.h>
#include <X11/Xutil.h> #include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <strings.h>
#include <X11/extensions/XShm.h> #include <X11/extensions/XShm.h>
#include <X11/extensions/XTest.h> #include <X11/extensions/XTest.h>
#include <X11/extensions/Xfixes.h> #include <X11/extensions/Xfixes.h>
@@ -58,9 +60,12 @@ int x11_init(const char *display_name) {
g_display = XOpenDisplay(display_name); g_display = XOpenDisplay(display_name);
} else { } else {
const char *configured = getenv("DISPLAY"); 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 (configured && *configured) g_display = XOpenDisplay(configured);
if (!g_display) g_display = XOpenDisplay(":0"); else {
if (!g_display) g_display = XOpenDisplay(":1"); g_display = XOpenDisplay(":0");
if (!g_display) g_display = XOpenDisplay(":1");
}
} }
if (!g_display) { if (!g_display) {
return -1; return -1;
@@ -190,6 +195,46 @@ int x11_key_event(int keycode, int is_press) {
return 0; 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() { void x11_close() {
if (!g_display) return; if (!g_display) return;
if (g_use_shm) { if (g_use_shm) {
+33
View File
@@ -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.
+89
View File
@@ -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
+22
View File
@@ -0,0 +1,22 @@
修复长时间远控约 20 分钟后被断开的问题。/ Fixes sessions being dropped after ~20 minutes.
### 修复 / Fixed
- **远控约 20 分钟后被云端断开**:官方客户端会定期(约每 5 分钟)通过信令刷新房间的重连凭证(`refresh_reconnect_key`),
之前没有实现,云端大约 20 分钟后就会回收房间并断开连接。现在被控端会按官方节奏刷新;凭证只保存在内存中,不写日志。
在测试虚拟机里长连接稳定运行 44 分钟以上(已越过原来的断开点),在 X11 实机上确认刷新被云端接受。
Sessions were released by the cloud after ~20 minutes because the room's reconnect key was never refreshed. The host now
refreshes it like the official clients (~every 5 minutes); the key stays in memory and is never logged.
- 旧的信令连接在重连后会被正确关闭。/ Stale signaling connections are closed after a reconnect.
### 其他 / Other
- 可选的进程内编码路径支持 XDamage:桌面静止时几乎不占 CPU 和带宽(`--features inproc-av`,默认关闭)(#7,感谢 @ferstar)。
XDamage capture on the opt-in in-process path (#7, thanks @ferstar).
- Wayland(portal)下双向剪贴板已在虚拟机中验收通过。注意:官方 Mac 客户端只在远控窗口处于前台(获得焦点)时接收被控端的剪贴板。
Two-way clipboard on the portal backend passes in the VM; the official Mac viewer only accepts the host clipboard while the
remote window has focus.
- 更多信令诊断日志(不含任何凭证)。/ More signaling diagnostics (no credentials).
### 下载 / Download
`uu-remote-linux-v0.1.5-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35)。
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
+28
View File
@@ -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.**
+31
View File
@@ -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
View File
@@ -81,9 +81,9 @@ pub async fn stream_audio(pc: Arc<RTCPeerConnection>, track: Arc<TrackLocalStati
if closed(&pc) { return; } if closed(&pc) { return; }
tokio::time::sleep(Duration::from_millis(100)).await; 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; let mut failures = 0u32;
while !closed(&pc) { while !closed(&pc) {
let source = crate::virtual_audio::source();
let (tx, mut rx) = tokio::sync::mpsc::channel::<bytes::Bytes>(25); let (tx, mut rx) = tokio::sync::mpsc::channel::<bytes::Bytes>(25);
let (stopper, job) = match start(&source, tx) { let (stopper, job) = match start(&source, tx) {
Ok(started) => started, Ok(started) => started,
@@ -101,7 +101,7 @@ pub async fn stream_audio(pc: Arc<RTCPeerConnection>, track: Arc<TrackLocalStati
} }
packets += 1; 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. // 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); drop(stopper);
let result = job.await.unwrap_or_else(|e| Err(e.to_string())); let result = job.await.unwrap_or_else(|e| Err(e.to_string()));
if closed(&pc) { break; } if closed(&pc) { break; }
if crate::virtual_audio::source() != source { failures=0; continue; }
log::warn!("Desktop audio stopped after {packets} packets: {:?}", result.err()); log::warn!("Desktop audio stopped after {packets} packets: {:?}", result.err());
failures = if packets > 250 { 0 } else { failures + 1 }; failures = if packets > 250 { 0 } else { failures + 1 };
if failures >= 5 { log::warn!("Desktop audio disabled for this connection"); return; } if failures >= 5 { log::warn!("Desktop audio disabled for this connection"); return; }
+13 -3
View File
@@ -46,6 +46,7 @@ pub struct ScreenCapture {
pub init_error: Option<String>, pub init_error: Option<String>,
pub portal: Option<std::sync::Arc<crate::portal::PortalSession>>, pub portal: Option<std::sync::Arc<crate::portal::PortalSession>>,
pub display: String, pub display: String,
kms: Option<crate::kms::Capture>,
pub width: u32, pub width: u32,
pub height: u32, pub height: u32,
buffer: Mutex<Vec<u8>>, buffer: Mutex<Vec<u8>>,
@@ -54,15 +55,21 @@ pub struct ScreenCapture {
impl ScreenCapture { impl ScreenCapture {
pub fn new() -> Result<Self, String> { pub fn new() -> Result<Self, String> {
let backend=crate::desktop_backend::detect()?; 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 { if backend == crate::desktop_backend::DesktopBackend::Portal {
let session=std::sync::Arc::new(crate::portal::PortalSession::open()?); let session=std::sync::Arc::new(crate::portal::PortalSession::open()?);
let (width,height)=(session.width,session.height); 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]) }); buffer:Mutex::new(vec![0;(width as usize)*(height as usize)*4]) });
} }
if backend == crate::desktop_backend::DesktopBackend::Wayland { if backend == crate::desktop_backend::DesktopBackend::Wayland {
let (width,height)=crate::wayland::init()?; 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]) }); buffer:Mutex::new(vec![0;(width as usize)*(height as usize)*4]) });
} }
let ret = unsafe { x11_init(std::ptr::null()) }; let ret = unsafe { x11_init(std::ptr::null()) };
@@ -90,6 +97,7 @@ impl ScreenCapture {
backend, backend,
init_error:None, init_error:None,
portal:None, portal:None,
kms:None,
display, display,
width: w as u32, width: w as u32,
height: h as u32, height: h as u32,
@@ -100,13 +108,14 @@ impl ScreenCapture {
/// Metadata for non-desktop tools. Never advertise a usable fake screen. /// Metadata for non-desktop tools. Never advertise a usable fake screen.
pub fn unavailable(error:String)->Self { pub fn unavailable(error:String)->Self {
Self { backend:crate::desktop_backend::detect().unwrap_or(crate::desktop_backend::DesktopBackend::X11), 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 图像 /// 截取一帧 BGRA 图像
pub fn capture_frame(&self) -> Result<Vec<u8>, String> { pub fn capture_frame(&self) -> Result<Vec<u8>, String> {
if let Some(error)=&self.init_error { return Err(error.clone()); } if let Some(error)=&self.init_error { return Err(error.clone()); }
let mut buf = self.buffer.lock().unwrap(); 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 let Some(session)=&self.portal { session.capture(&mut buf)?; return Ok(buf.clone()); }
if self.backend == crate::desktop_backend::DesktopBackend::Wayland { if self.backend == crate::desktop_backend::DesktopBackend::Wayland {
crate::wayland::capture(&mut buf)?; 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. /// Keep the display visible only while a peer is streaming; never unlock it.
pub fn keep_display_awake() -> Result<(), String> { 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::Portal { return Ok(()); }
if crate::desktop_backend::detect()? == crate::desktop_backend::DesktopBackend::Wayland { if crate::desktop_backend::detect()? == crate::desktop_backend::DesktopBackend::Wayland {
return crate::wayland::wake(); return crate::wayland::wake();
+1 -1
View File
@@ -20,7 +20,7 @@ extern "C" {
pub fn start(display: String) -> ClipboardBridge { pub fn start(display: String) -> ClipboardBridge {
if display == "wayland:native" { return start_wayland(); } 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 (incoming,_)=watch::channel(None);let (_,outgoing)=watch::channel(None);
let (sender,ready)=tokio::sync::oneshot::channel();let _=sender.send(false); let (sender,ready)=tokio::sync::oneshot::channel();let _=sender.send(false);
return ClipboardBridge {incoming,outgoing,ready}; return ClipboardBridge {incoming,outgoing,ready};
+21 -16
View File
@@ -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" { 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()); return Err("Clipboard acceptance is restricted to the isolated VM".into());
} }
let session=Arc::new(crate::portal::PortalSession::open()?); let screen=crate::capture::ScreenCapture::new()?;
let bridge=crate::clipboard::start_portal(session.clone()); let input=Arc::new(crate::input::InputInjector::new());
if !bridge.ready.blocking_recv().unwrap_or(false) { return Err("Portal clipboard permission not granted".into()); } 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") let mut child=Command::new("python3").arg("/home/uuremote/clipboard-acceptance.py")
.env("GDK_BACKEND","wayland").env("GSK_RENDERER","cairo") .env("GDK_BACKEND","wayland").env("GSK_RENDERER","cairo")
.stdin(Stdio::piped()).stdout(Stdio::piped()).stderr(Stdio::null()).spawn().map_err(|_|"Start GTK clipboard fixture")?; .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())); bridge.incoming.send_replace(Some(text.clone()));
let deadline=Instant::now()+Duration::from_secs(6);let mut matched=false; 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)); } 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)); std::thread::sleep(Duration::from_millis(300));
if outgoing.has_changed().unwrap_or(false) { return Err("Portal clipboard echoed its own selection".into()); } if outgoing.has_changed().unwrap_or(false) { return Err("Desktop clipboard echoed its own selection".into()); }
println!("Portal clipboard incoming {name}: passed; no echo"); println!("Desktop clipboard incoming {name}: passed; no echo");
} }
session.move_pointer(f64::from(session.width)/2.0,f64::from(session.height)/2.0,false)?; input.mouse_absolute_normalized(0.5,0.5,(screen.width,screen.height));
session.button(1,true)?; session.button(1,false)?; std::thread::sleep(Duration::from_millis(100));
input.mouse_button(1,true); input.mouse_button(1,false);
std::thread::sleep(Duration::from_millis(300)); std::thread::sleep(Duration::from_millis(300));
let external="UU outgoing 中文🚀\nsecond line"; let external="UU outgoing 中文🚀\nsecond line";
if !check("set",external)? { return Err("GTK clipboard ownership failed".into()); } 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;} 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)); std::thread::sleep(Duration::from_millis(100));
} }
if !matched { return Err("Portal clipboard outgoing Unicode mismatch".into()); } if !matched { return Err("Desktop clipboard outgoing Unicode mismatch".into()); }
println!("Portal clipboard outgoing unicode: passed"); println!("Desktop clipboard outgoing unicode: passed");
bridge.incoming.send_replace(Some("x".repeat(crate::clipboard::MAX_TEXT+1))); bridge.incoming.send_replace(Some("x".repeat(crate::clipboard::MAX_TEXT+1)));
std::thread::sleep(Duration::from_millis(400)); std::thread::sleep(Duration::from_millis(400));
if !check("read",external)? { return Err("Oversized text altered native clipboard".into()); } 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()); } 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)); std::thread::sleep(Duration::from_secs(3));
if outgoing.has_changed().unwrap_or(false) { return Err("Portal clipboard published oversized native text".into()); } if outgoing.has_changed().unwrap_or(false) { return Err("Desktop clipboard published oversized native text".into()); }
println!("Portal clipboard outgoing oversized rejection: passed"); println!("Desktop clipboard outgoing oversized rejection: passed");
if !check("set","")? { return Err("GTK empty fixture setup failed".into()); } if !check("set","")? { return Err("GTK empty fixture setup failed".into()); }
let deadline=Instant::now()+Duration::from_secs(6);let mut matched=false; let deadline=Instant::now()+Duration::from_secs(6);let mut matched=false;
while Instant::now()<deadline { while Instant::now()<deadline {
if outgoing.has_changed().unwrap_or(false) && outgoing.borrow_and_update().as_deref()==Some("") {matched=true;break;} if outgoing.has_changed().unwrap_or(false) && outgoing.borrow_and_update().as_deref()==Some("") {matched=true;break;}
std::thread::sleep(Duration::from_millis(100)); std::thread::sleep(Duration::from_millis(100));
} }
if !matched { return Err("Portal clipboard outgoing empty text mismatch".into()); } if !matched { return Err("Desktop clipboard outgoing empty text mismatch".into()); }
println!("Portal clipboard outgoing empty: passed"); println!("Desktop clipboard outgoing empty: passed");
drop(writer); Ok(()) drop(writer); Ok(())
})(); })();
let _=child.kill();let _=child.wait();result let _=child.kill();let _=child.wait();pointer.abort();input.release_held();result
} }
+124
View File
@@ -0,0 +1,124 @@
//! Damage-driven X11 capture for the in-process encoder: XDamage reports when the root window changed,
//! XShm copies it out as 32-bit BGRX. Pure Rust over x11rb; only the SysV shared-memory calls use libc.
use std::os::unix::io::AsRawFd;
use std::time::{Duration, Instant};
use x11rb::connection::{Connection, RequestConnection};
use x11rb::protocol::damage::{self, ConnectionExt as _, ReportLevel};
use x11rb::protocol::shm::{self, ConnectionExt as _};
use x11rb::protocol::xproto::Window;
use x11rb::rust_connection::RustConnection;
use x11rb::NONE;
/// X11 ZPixmap image format.
const Z_PIXMAP: u8 = 2;
pub struct DamageCapture {
conn: RustConnection,
root: Window,
damage: damage::Damage,
damage_event: u8,
width: u16,
height: u16,
seg: shm::Seg,
shm_ptr: *mut u8,
len: usize,
}
fn text(error: impl std::fmt::Display) -> String { error.to_string() }
impl DamageCapture {
/// Connects to `display` and sets up XDamage and XShm. Errors name the missing extension or failed step.
pub fn open(display: &str) -> Result<Self, String> {
let (conn, screen_num) = RustConnection::connect(Some(display)).map_err(text)?;
let damage_info = conn.extension_information(damage::X11_EXTENSION_NAME).map_err(text)?
.ok_or("XDamage extension is missing")?;
if conn.extension_information(shm::X11_EXTENSION_NAME).map_err(text)?.is_none() {
return Err("XShm extension is missing".into());
}
conn.damage_query_version(1, 1).map_err(text)?.reply().map_err(text)?;
let setup = conn.setup();
let screen = setup.roots.get(screen_num).ok_or("X screen not found")?;
let (root, width, height, depth) = (screen.root, screen.width_in_pixels, screen.height_in_pixels, screen.root_depth);
// The frame layout assumes packed 32-bit pixels; anything else would shear the picture.
let format = setup.pixmap_formats.iter().find(|f| f.depth == depth).ok_or("no pixmap format for the root depth")?;
if format.bits_per_pixel != 32 { return Err(format!("root depth {depth} is not 32 bits per pixel")); }
let len = width as usize * height as usize * 4;
// NonEmpty: one event each time the root goes from clean to damaged; re-armed in `wait`.
let damage = conn.generate_id().map_err(text)?;
conn.damage_create(damage, root, ReportLevel::NON_EMPTY).map_err(text)?.check().map_err(text)?;
// Nothing below can fail without also releasing the local mapping, so every error path is covered here.
let seg = conn.generate_id().map_err(text)?;
let shmid = unsafe { libc::shmget(libc::IPC_PRIVATE, len, libc::IPC_CREAT | 0o600) };
if shmid < 0 { return Err("shmget failed".into()); }
let shm_ptr = unsafe { libc::shmat(shmid, std::ptr::null(), 0) } as *mut u8;
if shm_ptr as isize == -1 {
unsafe { libc::shmctl(shmid, libc::IPC_RMID, std::ptr::null_mut()) };
return Err("shmat failed".into());
}
let attach = conn.shm_attach(seg, shmid as u32, false).map_err(text).and_then(|c| c.check().map_err(text));
// Marked for removal either way; the kernel frees it after the last detach.
unsafe { libc::shmctl(shmid, libc::IPC_RMID, std::ptr::null_mut()) };
if let Err(error) = attach {
unsafe { libc::shmdt(shm_ptr as *const libc::c_void) };
return Err(error);
}
Ok(Self { conn, root, damage, damage_event: damage_info.first_event, width, height, seg, shm_ptr, len })
}
pub fn size(&self) -> (u32, u32) { (self.width as u32, self.height as u32) }
/// Waits up to `timeout` for the screen to change. Returns `true` on change, `false` on timeout.
pub fn wait(&mut self, timeout: Duration) -> Result<bool, String> {
let deadline = Instant::now() + timeout;
loop {
let mut changed = false;
while let Some(event) = self.conn.poll_for_raw_event().map_err(text)? {
// XDamageNotify is the extension's first event; the wire code keeps the high bit for synthetic events.
if event[0] & 0x7f == self.damage_event { changed = true; }
}
if changed {
self.conn.damage_subtract(self.damage, NONE, NONE).map_err(text)?;
self.conn.flush().map_err(text)?;
return Ok(true);
}
let remaining = deadline.saturating_duration_since(Instant::now());
if remaining.is_zero() { return Ok(false); }
let mut pfd = libc::pollfd { fd: self.conn.stream().as_raw_fd(), events: libc::POLLIN, revents: 0 };
match unsafe { libc::poll(&mut pfd, 1, remaining.as_millis().max(1) as i32) } {
ready if ready < 0 => return Err("poll on the X connection failed".into()),
// Readable but not yet a whole event: poll_for_raw_event would return None and poll would
// fire again at once, spinning a core. Block for the event instead.
ready if ready > 0 => {
let event = self.conn.wait_for_raw_event().map_err(text)?;
if event[0] & 0x7f == self.damage_event { changed = true; }
if changed {
self.conn.damage_subtract(self.damage, NONE, NONE).map_err(text)?;
self.conn.flush().map_err(text)?;
return Ok(true);
}
}
_ => {}
}
}
}
/// Copies the root into the shared segment and returns it as rows of `width * 4` bytes.
/// The slice is valid until the next call.
pub fn capture(&mut self) -> Result<&[u8], String> {
self.conn.shm_get_image(self.root, 0, 0, self.width, self.height, !0, Z_PIXMAP, self.seg, 0)
.map_err(text)?.reply().map_err(text)?;
Ok(unsafe { std::slice::from_raw_parts(self.shm_ptr, self.len) })
}
}
impl Drop for DamageCapture {
fn drop(&mut self) {
let _ = self.conn.damage_destroy(self.damage);
let _ = self.conn.shm_detach(self.seg);
let _ = self.conn.flush();
unsafe { libc::shmdt(self.shm_ptr as *const libc::c_void) };
}
}
+9 -2
View File
@@ -1,6 +1,10 @@
//! Select the host desktop, not the presence of an XWayland DISPLAY socket. //! Select the host desktop, not the presence of an XWayland DISPLAY socket.
#[derive(Clone, Copy, Debug, PartialEq, Eq)] #[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> { pub fn detect() -> Result<DesktopBackend, String> {
let backend=select(std::env::var("UU_DESKTOP_BACKEND").ok().as_deref(), 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 { match explicit {
Some("x11") => Ok(DesktopBackend::X11), Some("x11") => Ok(DesktopBackend::X11),
Some("wayland") => Ok(DesktopBackend::Wayland), Some("wayland") => Ok(DesktopBackend::Wayland),
Some("kms") => Ok(DesktopBackend::Kms),
Some("portal") => Ok(DesktopBackend::Portal), Some("portal") => Ok(DesktopBackend::Portal),
Some("none") => Ok(DesktopBackend::X11), // never opened, see headless()
None | Some("auto") => { None | Some("auto") => {
if session == Some("wayland") || socket.is_some_and(|s| !s.is_empty()) { if session == Some("wayland") || socket.is_some_and(|s| !s.is_empty()) {
Ok(DesktopBackend::Wayland) Ok(DesktopBackend::Wayland)
} else { Ok(DesktopBackend::X11) } } 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,None,Some("wayland-1")),Ok(DesktopBackend::Wayland));
assert_eq!(select(None,Some("x11"),None),Ok(DesktopBackend::X11)); 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("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()); assert!(select(Some("typo"),None,None).is_err());
} }
} }
+165 -25
View File
@@ -12,6 +12,8 @@ use ff::util::picture;
use ff::{codec, encoder, frame, software::scaling, Dictionary, Packet, Rational}; use ff::{codec, encoder, frame, software::scaling, Dictionary, Packet, Rational};
use tokio::sync::mpsc; use tokio::sync::mpsc;
use crate::damage_capture::DamageCapture;
pub struct Config { pub struct Config {
pub display: String, pub display: String,
pub input: (u32, u32), pub input: (u32, u32),
@@ -22,7 +24,9 @@ pub struct Config {
pub target_bps: i64, pub target_bps: i64,
pub max_bps: i64, pub max_bps: i64,
pub buffer_bits: 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,
} }
/// `UU_VIDEO_PIPELINE=process` keeps the original ffmpeg child-process path. /// `UU_VIDEO_PIPELINE=process` keeps the original ffmpeg child-process path.
@@ -30,6 +34,11 @@ pub fn disabled() -> bool {
std::env::var("UU_VIDEO_PIPELINE").is_ok_and(|v| v == "process") std::env::var("UU_VIDEO_PIPELINE").is_ok_and(|v| v == "process")
} }
/// Damage-driven capture is on unless `UU_CAPTURE=grab` asks for x11grab.
pub fn damage_enabled() -> bool {
std::env::var("UU_CAPTURE").map_or(true, |v| v != "grab")
}
/// In-process capture is used unless the process path is requested, or a bundled FFmpeg prefix /// In-process capture is used unless the process path is requested, or a bundled FFmpeg prefix
/// (`UU_FFMPEG_ROOT` / config `ffmpeg_root`) is set: that build was chosen on purpose, so keep using it. /// (`UU_FFMPEG_ROOT` / config `ffmpeg_root`) is set: that build was chosen on purpose, so keep using it.
pub fn selected() -> bool { pub fn selected() -> bool {
@@ -62,34 +71,19 @@ fn drain(encoder: &mut encoder::Video, packet: &mut Packet, units: &mpsc::Sender
} }
} }
/// Runs until `stop` is set or the receiver is dropped. A set `idr` flag forces the next frame to be an IDR. /// Opens the H.264 encoder for `config`. Returns the encoder, its input pixel format and output size.
pub fn run(config: Config, units: mpsc::Sender<Vec<u8>>, stop: Arc<AtomicBool>, idr: Arc<AtomicBool>) -> Result<(), String> { fn open_encoder(config: &Config) -> Result<(encoder::Video, Pixel, u32, u32), String> {
ensure_init()?;
let name = CString::new("x11grab").map_err(|e| e.to_string())?;
let raw = unsafe { ff::sys::av_find_input_format(name.as_ptr()) };
if raw.is_null() { return Err("x11grab input is not available in this FFmpeg build".into()); }
let input_format = Format::Input(unsafe { ff::format::format::Input::wrap(raw as *mut _) });
let mut capture_options = Dictionary::new();
capture_options.set("framerate", &config.fps.to_string());
capture_options.set("video_size", &format!("{}x{}", config.input.0, config.input.1));
// The viewer draws its own pointer; do not bake a second one into the video.
capture_options.set("draw_mouse", "0");
let mut input = ff::format::open_with(&config.display, &input_format, capture_options).map_err(text)?.input();
let stream = input.streams().best(ff::media::Type::Video).ok_or("X11 capture has no video stream")?;
let index = stream.index();
let mut decoder = codec::context::Context::from_parameters(stream.parameters()).map_err(text)?.decoder().video().map_err(text)?;
let (width, height) = config.output.unwrap_or(config.input); 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")?; .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. // 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)?; 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_width(width);
context.set_height(height); context.set_height(height);
// NVENC takes packed BGR0 directly, so the GPU path skips the CPU colour conversion to YUV420P. // 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 }; let pixel = match config.encoder { crate::video::Encoder::Nvenc => Pixel::BGRZ, crate::video::Encoder::Vaapi => Pixel::NV12, _ => Pixel::YUV420P };
context.set_format(pixel); 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_time_base(Rational::new(1, config.fps as i32));
context.set_frame_rate(Some(Rational::new(config.fps as i32, 1))); context.set_frame_rate(Some(Rational::new(config.fps as i32, 1)));
context.set_gop(config.gop); context.set_gop(config.gop);
@@ -105,7 +99,10 @@ pub fn run(config: Config, units: mpsc::Sender<Vec<u8>>, stop: Arc<AtomicBool>,
let cq = config.cq.to_string(); let cq = config.cq.to_string();
let gop = config.gop.to_string(); let gop = config.gop.to_string();
let mut options = Dictionary::new(); 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"), for (key, value) in [("preset", "p4"), ("tune", "ull"), ("profile", "baseline"), ("rc", "vbr"),
("rc-lookahead", "0"), ("zerolatency", "1"), ("delay", "0"), ("aud", "1"), ("forced-idr", "1")] { ("rc-lookahead", "0"), ("zerolatency", "1"), ("delay", "0"), ("aud", "1"), ("forced-idr", "1")] {
options.set(key, value); options.set(key, value);
@@ -119,7 +116,74 @@ pub fn run(config: Config, units: mpsc::Sender<Vec<u8>>, stop: Arc<AtomicBool>,
options.set("crf", &cq); options.set("crf", &cq);
options.set("x264-params", &x264); options.set("x264-params", &x264);
} }
let mut encoder = context.open_as_with(codec, options).map_err(text)?; let encoder = context.open_as_with(codec, options).map_err(text)?;
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()?;
if config.damage {
match DamageCapture::open(&config.display) {
Ok(capture) => return run_damage(config, capture, units, stop, idr),
Err(error) => log::warn!("Damage-driven capture unavailable, using x11grab: {error}"),
}
}
let name = CString::new("x11grab").map_err(|e| e.to_string())?;
let raw = unsafe { ff::sys::av_find_input_format(name.as_ptr()) };
if raw.is_null() { return Err("x11grab input is not available in this FFmpeg build".into()); }
let input_format = Format::Input(unsafe { ff::format::format::Input::wrap(raw as *mut _) });
let mut capture_options = Dictionary::new();
capture_options.set("framerate", &config.fps.to_string());
capture_options.set("video_size", &format!("{}x{}", config.input.0, config.input.1));
// The viewer draws its own pointer; do not bake a second one into the video.
capture_options.set("draw_mouse", "0");
let mut input = ff::format::open_with(&config.display, &input_format, capture_options).map_err(text)?.input();
let stream = input.streams().best(ff::media::Type::Video).ok_or("X11 capture has no video stream")?;
let index = stream.index();
let mut decoder = codec::context::Context::from_parameters(stream.parameters()).map_err(text)?.decoder().video().map_err(text)?;
let (mut encoder, pixel, width, height) = open_encoder(&config)?;
let mut scaler: Option<(scaling::Context, (Pixel, u32, u32))> = None; let mut scaler: Option<(scaling::Context, (Pixel, u32, u32))> = None;
let mut decoded = frame::Video::empty(); let mut decoded = frame::Video::empty();
@@ -163,12 +227,88 @@ pub fn run(config: Config, units: mpsc::Sender<Vec<u8>>, stop: Arc<AtomicBool>,
frame.set_pts(Some(slot)); frame.set_pts(Some(slot));
frame.set_kind(if idr.swap(false, Ordering::AcqRel) { picture::Type::I } else { picture::Type::None }); 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(()); } if !drain(&mut encoder, &mut packet, &units)? { return Ok(()); }
} }
Ok(()) Ok(())
} }
/// Damage-driven loop. Damage only marks the picture dirty; a dirty picture is captured and encoded on
/// the next fixed frame tick, so the stream keeps a steady cadence while the screen moves. The viewer's
/// jitter buffer grows with irregular arrivals, and irregular sends were what raised its latency.
/// An idle screen sends a keepalive about once a second so the viewer still sees traffic.
fn run_damage(config: Config, mut capture: DamageCapture, units: mpsc::Sender<Vec<u8>>, stop: Arc<AtomicBool>, idr: Arc<AtomicBool>) -> Result<(), String> {
let (mut encoder, pixel, width, height) = open_encoder(&config)?;
let (cap_width, cap_height) = capture.size();
let mut captured = frame::Video::new(Pixel::BGRZ, cap_width, cap_height);
let mut converted = frame::Video::new(pixel, width, height);
let mut scaler = None;
let mut packet = Packet::empty();
let started = Instant::now();
let frame_interval = Duration::from_nanos(1_000_000_000 / u64::from(config.fps));
// Keepalive gaps are random between 15 and 25 fps: a steady 1 fps reads as a stall to the viewer.
let mut seed = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map_or(1, |d| d.subsec_nanos()) | 1;
let mut keepalive = random_keepalive(&mut seed);
let mut dirty = true;
let mut next_tick = Instant::now();
let mut last_send = Instant::now();
let mut last_slot: i64 = -1;
loop {
if stop.load(Ordering::Relaxed) { return Ok(()); }
let deadline = if dirty { next_tick } else { last_send + keepalive };
let wait = deadline.saturating_duration_since(Instant::now());
// A change marks the picture dirty and falls through, so a client that damages the root constantly
// cannot keep an overdue tick waiting.
if capture.wait(wait.max(Duration::from_millis(1)))? { dirty = true; }
let now = Instant::now();
if dirty && now < next_tick { continue; }
if !dirty && now < last_send + keepalive { continue; }
let source = capture.capture()?;
let bytes_per_line = cap_width as usize * 4;
let stride = captured.stride(0);
let row = bytes_per_line.min(stride);
for (y, src) in source.chunks_exact(bytes_per_line).enumerate() {
captured.data_mut(0)[y * stride..y * stride + row].copy_from_slice(&src[..row]);
}
let slot = ((started.elapsed().as_micros() * u128::from(config.fps) + 500_000) / 1_000_000) as i64;
let slot = slot.max(last_slot + 1);
last_slot = slot;
let frame = if pixel == Pixel::BGRZ && width == cap_width && height == cap_height {
&mut captured
} else {
if scaler.is_none() {
scaler = Some(scaling::Context::get(Pixel::BGRZ, cap_width, cap_height, pixel, width, height, scaling::Flags::BILINEAR).map_err(text)?);
}
if let Some(context) = scaler.as_mut() { context.run(&captured, &mut converted).map_err(text)?; }
&mut converted
};
frame.set_pts(Some(slot));
frame.set_kind(if idr.swap(false, Ordering::AcqRel) { picture::Type::I } else { picture::Type::None });
send_video_frame(&mut encoder, frame, config.encoder)?;
if !drain(&mut encoder, &mut packet, &units)? { return Ok(()); }
let now = Instant::now();
if dirty {
// Keep the tick phase; after a long idle, restart the phase from now instead of bursting to catch up.
let next = next_tick + frame_interval;
next_tick = if next > now { next } else { now + frame_interval };
dirty = false;
}
last_send = now;
keepalive = random_keepalive(&mut seed);
}
}
/// Picks the next keepalive gap in 40–67 ms (15–25 fps) with a small xorshift generator.
fn random_keepalive(seed: &mut u32) -> Duration {
*seed ^= *seed << 13;
*seed ^= *seed >> 17;
*seed ^= *seed << 5;
Duration::from_micros(40_000 + u64::from(*seed % 26_700))
}
/// Encodes a PulseAudio/PipeWire source to Opus in this process. Each Opus packet is sent as is. /// Encodes a PulseAudio/PipeWire source to Opus in this process. Each Opus packet is sent as is.
pub fn run_audio(source: &str, units: mpsc::Sender<bytes::Bytes>, stop: Arc<AtomicBool>) -> Result<(), String> { pub fn run_audio(source: &str, units: mpsc::Sender<bytes::Bytes>, stop: Arc<AtomicBool>) -> Result<(), String> {
ensure_init()?; ensure_init()?;
+108 -2
View File
@@ -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_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_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_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"))] #[cfg(not(target_os = "linux"))]
unsafe fn x11_mouse_move(_: libc::c_int, _: libc::c_int) -> libc::c_int { 0 } 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} unsafe fn x11_pointer_state(_: *mut libc::c_int,_:*mut libc::c_int,_:*mut libc::c_uint)->libc::c_int {-1}
#[derive(Default)] #[derive(Default)]
struct HeldInput { buttons: std::collections::BTreeSet<i32>, keys: std::collections::BTreeSet<u64> } 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 { impl InputInjector {
pub fn new() -> Self { 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>>) { 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)) { 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) { if let Some(position) = crate::input_mapping::normalized_pointer(x,y,size.0,size.1) {
let mut pending = self.pending.lock().unwrap(); let mut pending = self.pending.lock().unwrap();
pending.absolute = Some(position); 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) { 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) { if let Some(symbol) = crate::input_mapping::windows_vk_to_keysym(key) {
let result = self.inject_key(symbol,is_press); let result = self.inject_key(symbol,is_press);
static AUDIT: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); 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 { 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 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::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) } 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) } } 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 { 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 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::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) } if self.backend==crate::desktop_backend::DesktopBackend::Wayland { crate::wayland::button(button,pressed) }
else { unsafe { x11_mouse_button(button,i32::from(pressed)) } } else { unsafe { x11_mouse_button(button,i32::from(pressed)) } }
} }
fn inject_key(&self,symbol:u64,pressed:bool)->i32 { 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 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::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) } 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)] #[cfg(test)]
mod scroll_tests { mod scroll_tests {
use super::*; use super::*;
+50
View File
@@ -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()) }
}
+50 -3
View File
@@ -12,8 +12,10 @@ mod login;
mod login_ui; mod login_ui;
mod session_lock; mod session_lock;
mod capture; mod capture;
mod kms;
mod file_trans; mod file_trans;
mod input; mod input;
mod uinput;
mod input_mapping; mod input_mapping;
mod port_mapping; mod port_mapping;
mod power; mod power;
@@ -22,9 +24,12 @@ mod terminal;
mod webrtc; mod webrtc;
mod video; mod video;
#[cfg(feature = "inproc-av")] #[cfg(feature = "inproc-av")]
mod damage_capture;
#[cfg(feature = "inproc-av")]
mod inproc_encoder; mod inproc_encoder;
mod capture_setting; mod capture_setting;
mod audio; mod audio;
mod virtual_audio;
mod system_actions; mod system_actions;
mod capability; mod capability;
mod protocol; mod protocol;
@@ -52,7 +57,7 @@ use webrtc::WebRtcSession;
async fn main() -> Result<(), Box<dyn std::error::Error>> { async fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init(); 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. // 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"]; "wayland-input-probe", "clipboard-acceptance", "portal-acceptance"];
if let Some(arg) = std::env::args().nth(1).filter(|a| !SUBCOMMANDS.contains(&a.as_str())) { 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(" | ")); let usage = format!("usage: uuremote-server [{}]\n (no argument: run the host)", SUBCOMMANDS.join(" | "));
@@ -161,6 +166,30 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let path=std::env::args().nth(2); 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()); 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") { if std::env::args().nth(1).as_deref() == Some("wayland-input-probe") {
return wayland::input_probe().map_err(|e|e.into()); return wayland::input_probe().map_err(|e|e.into());
} }
@@ -193,6 +222,16 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
if std::env::args().nth(1).as_deref() == Some("unlock-check") { if std::env::args().nth(1).as_deref() == Some("unlock-check") {
return session_lock::check_from_stdin().map_err(|e| e.into()); 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") { if std::env::args().nth(1).as_deref() == Some("login") {
// Interactive: register this host and bind it to an account via QR scan // Interactive: register this host and bind it to an account via QR scan
// (GTK window when available; `login --no-gui` keeps it terminal-only). // (GTK window when available; `login --no-gui` keeps it terminal-only).
@@ -200,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 if login::run(gui).await.is_err() { std::process::exit(1); } // error already shown
return Ok(()); return Ok(());
} }
virtual_audio::init();
let _audio_cleanup=virtual_audio::Cleanup;
log::info!("Starting uu-remote-linux host v{}...", env!("CARGO_PKG_VERSION")); 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") { && std::env::var("UU_PORTAL_PREAUTHORIZE").ok().as_deref()!=Some("1") {
Err("Portal screen access requires a desktop connection and local consent".to_owned()) Err("Portal screen access requires a desktop connection and local consent".to_owned())
} else { ScreenCapture::new() }).unwrap_or_else(|error| { } else { ScreenCapture::new() }).unwrap_or_else(|error| {
@@ -216,7 +259,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let shutdown_input=input.clone(); let shutdown_input=input.clone();
tokio::spawn(async move { tokio::spawn(async move {
if let Ok(mut term)=tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate()) { 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);
} }
}); });
} }
@@ -356,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()) let stale_portal=capture.portal.as_ref().is_some_and(|session|!session.is_alive())
&& crate::session_lock::locked_now() == Some(false); && 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) { let session_capture=if desktop && (capture.init_error.is_some() || stale_portal) {
if stale_portal { if stale_portal {
input.release_held();input.bind_portal(None); input.release_held();input.bind_portal(None);
+122 -8
View File
@@ -47,6 +47,51 @@ pub enum SignalingEvent {
pub struct SignalingClient { pub struct SignalingClient {
tx_outgoing: mpsc::Sender<String>, tx_outgoing: mpsc::Sender<String>,
// Belongs to this room only; fresh-room reconnect must not reuse it.
_reconnect_key: Arc<std::sync::Mutex<Option<String>>>,
}
fn refreshed_key(ack:&Value)->Option<String> {
let values=ack.as_array()?;
if values.first()?.as_str()? != "success" { return None; }
let key=values.get(1)?.get("reconnect_key")?.as_str()?;
(!key.is_empty() && key.len()<=16384).then(||key.to_owned())
}
#[cfg(test)]
mod refresh_tests {
use super::refreshed_key;
use serde_json::json;
#[test]
fn accepts_only_successful_complete_reconnect_key_ack() {
assert_eq!(refreshed_key(&json!(["success",{"reconnect_key":"synthetic-key"}])),Some("synthetic-key".into()));
for ack in [json!(["failure",{"reconnect_key":"synthetic-key"}]),json!(["success"]),json!(["success",{"reconnect_key":""}]),json!(["success",{"reconnect_key":123}]),json!({"reconnect_key":"synthetic-key"})] {
assert!(refreshed_key(&ack).is_none());
}
}
#[tokio::test]
async fn closes_obsolete_socket_even_if_peer_retains_client() {
use futures_util::{SinkExt,StreamExt};
use tokio_tungstenite::tungstenite::Message;
let listener=tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let address=listener.local_addr().unwrap();
let server=tokio::spawn(async move {
let (tcp,_)=listener.accept().await.unwrap();
let mut socket=tokio_tungstenite::accept_async(tcp).await.unwrap();
socket.send(Message::Text("0{}".into())).await.unwrap();
assert_eq!(socket.next().await.unwrap().unwrap(),Message::Text("40".into()));
socket.send(Message::Text("40{}".into())).await.unwrap();
assert!(socket.next().await.unwrap().unwrap().into_text().unwrap().contains("room_info"));
matches!(socket.next().await,None|Some(Err(_))|Some(Ok(Message::Close(_))))
});
let (sender,mut events)=tokio::sync::mpsc::channel(4);
let retained=super::SignalingClient::connect(&format!("ws://{address}"),"synthetic-only",sender).await.unwrap();
assert!(matches!(events.recv().await,Some(super::SignalingEvent::Connected)));
drop(events);
assert!(tokio::time::timeout(std::time::Duration::from_secs(2),server).await.unwrap().unwrap());
// Retain it until after the server sees closure, just like PC callbacks.
drop(retained);
}
} }
impl SignalingClient { impl SignalingClient {
@@ -79,7 +124,7 @@ impl SignalingClient {
let (tx_outgoing, mut rx_outgoing) = mpsc::channel::<String>(32); let (tx_outgoing, mut rx_outgoing) = mpsc::channel::<String>(32);
// 发送协程 // 发送协程
tokio::spawn(async move { let writer=tokio::spawn(async move {
while let Some(msg) = rx_outgoing.recv().await { while let Some(msg) = rx_outgoing.recv().await {
if let Err(e) = write.send(Message::Text(msg)).await { if let Err(e) = write.send(Message::Text(msg)).await {
log::warn!("WS send error: {}", e); log::warn!("WS send error: {}", e);
@@ -89,23 +134,66 @@ impl SignalingClient {
}); });
let tx_out_clone = tx_outgoing.clone(); let tx_out_clone = tx_outgoing.clone();
static GENERATION: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);
let generation=GENERATION.fetch_add(1,std::sync::atomic::Ordering::Relaxed);
let reconnect_key=Arc::new(std::sync::Mutex::new(None));
let refreshed=reconnect_key.clone();
// 接收与状态机协程 // 接收与状态机协程
tokio::spawn(async move { tokio::spawn(async move {
let mut req_counter = 10; let mut req_counter = 10;
let started=std::time::Instant::now();
let mut last_ping=started;
let mut pings=0u64;
let mut pong_queued=0u64;
let mut connected=false;
let mut next_refresh=tokio::time::Instant::now()+std::time::Duration::from_secs(30);
let mut refresh_acks=std::collections::VecDeque::new();
let mut accepted_refresh=0;
// Socket.IO binary event awaiting attachments: (event name, header JSON). // Socket.IO binary event awaiting attachments: (event name, header JSON).
let mut pending_binary: Option<(String, Value)> = None; let mut pending_binary: Option<(String, Value)> = None;
while let Some(msg_res) = read.next().await { loop {
let msg_res=tokio::select! {
_=event_sender.closed()=>break,
// Official ControlledSession RefreshSignalTokenLoop: first
// 30s, success every ~300s, failure retry after 30s. Native
// libstreamer 0x885774 / 0x885e58; emit has NO payload args.
_=tokio::time::sleep_until(next_refresh), if connected => {
req_counter+=1;
refresh_acks.push_back(req_counter);
if refresh_acks.len()>16 { refresh_acks.pop_front(); }
let packet=format!("42{}[\"refresh_reconnect_key\"]",req_counter);
if tx_out_clone.send(packet).await.is_err() {
let _=event_sender.send(SignalingEvent::Disconnected).await;break;
}
next_refresh=tokio::time::Instant::now()+std::time::Duration::from_secs(30);
log::info!("Signaling reconnect-key refresh requested: generation={} age_s={}",generation,started.elapsed().as_secs());
continue;
},
msg=read.next()=>match msg {
Some(msg)=>msg,
None=>{let _=event_sender.send(SignalingEvent::Disconnected).await;break;}
},
};
match msg_res { match msg_res {
Ok(Message::Text(txt)) => { Ok(Message::Text(txt)) => {
// Engine.IO 报文处理 // Engine.IO 报文处理
if txt.starts_with('0') { if txt.starts_with('0') {
if let Ok(open)=serde_json::from_str::<Value>(&txt[1..]) {
log::info!("Signaling heartbeat config: generation={} ping_interval_ms={:?} ping_timeout_ms={:?}",generation,open.get("pingInterval").and_then(Value::as_u64),open.get("pingTimeout").and_then(Value::as_u64));
}
// Open packet -> 发送 Socket.IO connect // Open packet -> 发送 Socket.IO connect
let _ = tx_out_clone.send("40".into()).await; let _ = tx_out_clone.send("40".into()).await;
} else if txt == "2" { } else if txt == "2" {
// Ping -> 回复 Pong // Ping -> 回复 Pong
let _ = tx_out_clone.send("3".into()).await; let gap=last_ping.elapsed().as_secs();last_ping=std::time::Instant::now();pings+=1;
if tx_out_clone.send("3".into()).await.is_ok() { pong_queued+=1; }
if pings==1 || pings%12==0 {
log::info!("Signaling heartbeat progress: generation={} age_s={} pings={} pong_queued={} gap_s={}",generation,started.elapsed().as_secs(),pings,pong_queued,gap);
}
} else if txt.starts_with("40") { } else if txt.starts_with("40") {
connected=true;
next_refresh=tokio::time::Instant::now()+std::time::Duration::from_secs(30);
// Socket.IO connected -> 发送 room_info 请求 // Socket.IO connected -> 发送 room_info 请求
req_counter += 1; req_counter += 1;
let room_req = format!("42{}[\"room_info\"]", req_counter); let room_req = format!("42{}[\"room_info\"]", req_counter);
@@ -113,9 +201,23 @@ impl SignalingClient {
let _ = event_sender.send(SignalingEvent::Connected).await; let _ = event_sender.send(SignalingEvent::Connected).await;
} else if txt.starts_with("43") { } else if txt.starts_with("43") {
// Socket.IO ack 回调 // Socket.IO ack 回调
let payload = &txt[txt.find('[').unwrap_or(2)..]; let begin=txt.find('[').unwrap_or(2);
let ack_id=txt[2..begin].parse::<u64>().ok();
let payload = &txt[begin..];
if let Ok(v) = serde_json::from_str::<Value>(payload) { if let Ok(v) = serde_json::from_str::<Value>(payload) {
let _ = event_sender.send(SignalingEvent::RoomInfoAck(v)).await; if let Some(id)=ack_id.filter(|id|refresh_acks.contains(id)) {
refresh_acks.retain(|pending|*pending!=id);
let key=refreshed_key(&v);
let success=key.is_some();
// Accept a delayed retry response, but never replace
// a newer key with an older successful response.
if success && id>accepted_refresh {
accepted_refresh=id;
*refreshed.lock().unwrap()=key;
next_refresh=tokio::time::Instant::now()+std::time::Duration::from_secs(300);
}
log::info!("Signaling reconnect-key refresh acknowledged: generation={} age_s={} success={}",generation,started.elapsed().as_secs(),success);
} else { let _ = event_sender.send(SignalingEvent::RoomInfoAck(v)).await; }
} }
} else if txt.starts_with("45") || txt.starts_with("46") { } else if txt.starts_with("45") || txt.starts_with("46") {
// Socket.IO binary event/ack: header text, attachments follow as binary frames. // Socket.IO binary event/ack: header text, attachments follow as binary frames.
@@ -170,14 +272,16 @@ impl SignalingClient {
} }
} }
"released" => { "released" => {
log::info!("Cloud peer event: generation={} event=released age_s={} pings={} last_ping_gap_s={} ice_id_present={}",generation,started.elapsed().as_secs(),pings,last_ping.elapsed().as_secs(),data.get("ice_id").is_some());
let _ = event_sender let _ = event_sender
.send(SignalingEvent::Released(data.clone())) .send(SignalingEvent::Released(data.clone()))
.await; .await;
} }
"left" | "leave" => { "left" | "leave" => {
log::info!("Cloud peer event: generation={} event={} age_s={} pings={} last_ping_gap_s={} ice_id_present={}",generation,event_name,started.elapsed().as_secs(),pings,last_ping.elapsed().as_secs(),data.get("ice_id").is_some());
let ice_id = data.get("ice_id").and_then(|v| v.as_str()).unwrap_or(""); let ice_id = data.get("ice_id").and_then(|v| v.as_str()).unwrap_or("");
if ice_id.is_empty() { if ice_id.is_empty() {
log::warn!("Room expired from NetEase cloud (leave event). Triggering room renewal..."); log::warn!("Cloud room leave without peer identifier; renewing signaling (expiry cause unknown)");
let _ = event_sender.send(SignalingEvent::Disconnected).await; let _ = event_sender.send(SignalingEvent::Disconnected).await;
} else { } else {
let _ = event_sender let _ = event_sender
@@ -212,16 +316,26 @@ impl SignalingClient {
} }
} }
} }
Ok(Message::Close(_)) | Err(_) => { Ok(Message::Close(frame)) => {
log::warn!("Signaling socket closed: generation={} age_s={} close_code={:?} pings={} last_ping_gap_s={}",generation,started.elapsed().as_secs(),frame.map(|f|u16::from(f.code)),pings,last_ping.elapsed().as_secs());
let _ = event_sender.send(SignalingEvent::Disconnected).await;
break;
}
Err(_) => {
log::warn!("Signaling socket receive failed: generation={} age_s={} pings={} last_ping_gap_s={}",generation,started.elapsed().as_secs(),pings,last_ping.elapsed().as_secs());
let _ = event_sender.send(SignalingEvent::Disconnected).await; let _ = event_sender.send(SignalingEvent::Disconnected).await;
break; break;
} }
_ => {} _ => {}
} }
} }
log::info!("Signaling receiver stopped: generation={} age_s={}",generation,started.elapsed().as_secs());
// Live peer callbacks can retain the old SignalingClient sender.
// Explicitly drop its write half too when this room stops reading.
writer.abort();
}); });
Ok(Self { tx_outgoing }) Ok(Self { tx_outgoing, _reconnect_key:reconnect_key })
} }
pub async fn emit_event(&self, event: &str, payload: serde_json::Value) -> Result<(), String> { pub async fn emit_event(&self, event: &str, payload: serde_json::Value) -> Result<(), String> {
+10 -5
View File
@@ -113,10 +113,14 @@ pub fn args_shape(args: &[u8]) -> String {
} }
fn mute_for_session(state: &mut HostState) { fn mute_for_session(state: &mut HostState) {
if state.restore.is_none() { state.restore = host_muted(); } if state.restore.is_none() {
set_host_muted(true); state.restore = host_muted();
set_host_muted(true);
}
crate::virtual_audio::enable();
} }
fn restore_mute(state: &mut HostState) { fn restore_mute(state: &mut HostState) {
crate::virtual_audio::disable();
if let Some(previous) = state.restore.take() { set_host_muted(previous); } if let Some(previous) = state.restore.take() { set_host_muted(previous); }
} }
@@ -143,20 +147,20 @@ impl PeerActions {
let on = toggle_value(value, args); let on = toggle_value(value, args);
state.settings.keep_mute = on; state.settings.keep_mute = on;
if on { mute_for_session(&mut state); } else { restore_mute(&mut state); } 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). // Keep the host muted after the session (远程结束后,恢复被控端声音设置 unchecked).
MUTE_PERMANENT => { MUTE_PERMANENT => {
let on = toggle_value(value, args); let on = toggle_value(value, args);
state.settings.mute_permanent = on; state.settings.mute_permanent = on;
if on { state.settings.keep_mute = true; mute_for_session(&mut state); } 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 => { MUTE_RESTORE => {
state.settings.keep_mute = false; state.settings.keep_mute = false;
state.settings.mute_permanent = false; state.settings.mute_permanent = false;
restore_mute(&mut state); 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 => { SET_AUTO_LOCK => {
// Observed: enabling 远程结束后自动锁屏 sends value 2 and disabling value 1. // Observed: enabling 远程结束后自动锁屏 sends value 2 and disabling value 1.
@@ -188,6 +192,7 @@ impl PeerActions {
let mut state = state().lock().unwrap(); let mut state = state().lock().unwrap();
state.desktop_sessions = state.desktop_sessions.saturating_sub(1); state.desktop_sessions = state.desktop_sessions.saturating_sub(1);
if state.desktop_sessions > 0 { return; } if state.desktop_sessions > 0 { return; }
crate::virtual_audio::disable();
if !state.settings.mute_permanent { restore_mute(&mut state); } if !state.settings.mute_permanent { restore_mute(&mut state); }
if state.settings.auto_lock { if state.settings.auto_lock {
log::info!("Auto lock after remote session: ok={}", lock_session()); log::info!("Auto lock after remote session: ok={}", lock_session());
+247
View File
@@ -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);
}
}
+79 -15
View File
@@ -53,9 +53,30 @@ struct CompleteFrame {
const RTP_CLOCK: u64 = 90_000; const RTP_CLOCK: u64 = 90_000;
/// `UU_VIDEO_ENCODER=cpu` keeps the virtual CPU display entirely off the GPU. #[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub fn gpu_encoder_requested() -> bool { pub(crate) enum Encoder { Cpu, Nvenc, Vaapi }
std::env::var("UU_VIDEO_ENCODER").map_or(true, |v| v != "cpu") 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`. /// 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>, settings: VideoSettings, sequence: Arc<std::sync::atomic::AtomicU64>, idr: Arc<AtomicBool>,
) -> Result<(), String> { ) -> Result<(), String> {
// The sequence is shared so a CPU fallback or settings restart continues it (skip statistics stay correct). // 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; } let encoder = Encoder::requested();
match read_encoder_with(size,display.clone(),portal.clone(),latest.clone(),true,sequence.clone(),settings,idr.clone()).await { 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(()), Ok(())=>Ok(()),
Err(error) if !latest.is_closed()=>{ Err(error) if !latest.is_closed()=>{
log::warn!("NVENC stream unavailable; switching scoped encoder to CPU: {}",error); log::warn!("{} stream unavailable; switching scoped encoder to CPU: {}",encoder.name(),error);
read_encoder_with(size,display,portal,latest,false,sequence,settings,idr).await read_encoder_with(size,display,portal,latest,Encoder::Cpu,sequence,settings,idr).await
}, },
Err(error)=>Err(error), Err(error)=>Err(error),
} }
@@ -167,10 +189,16 @@ fn rate_args(settings: &VideoSettings) -> (String, String) {
} }
async fn read_encoder_with( 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>, mut settings: VideoSettings, #[cfg_attr(not(feature = "inproc-av"), allow(unused_variables))] idr: Arc<AtomicBool>,
)->Result<(),String>{ )->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")] #[cfg(feature = "inproc-av")]
if !wayland && portal.is_none() && crate::inproc_encoder::selected() { if !wayland && portal.is_none() && crate::inproc_encoder::selected() {
let max_kbps = settings.max_kbps().unwrap_or(30_000) as i64; let max_kbps = settings.max_kbps().unwrap_or(30_000) as i64;
@@ -178,10 +206,11 @@ async fn read_encoder_with(
display: display.clone(), input: size, output: settings.output, fps: u32::from(settings.fps), 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, 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, 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", 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; return read_inprocess(config, latest, sequence, idr).await;
} }
let size = format!("{}x{}", size.0, size.1); let size = format!("{}x{}", size.0, size.1);
@@ -193,13 +222,14 @@ async fn read_encoder_with(
let (maxrate, bufsize) = rate_args(&settings); let (maxrate, bufsize) = rate_args(&settings);
let cq = settings.cq().to_string(); let cq = settings.cq().to_string();
let target = settings.max_kbps().map_or("16M".to_string(), |k| format!("{}k", k * 3 / 4)); 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. // A bundled FFmpeg prefix (config ffmpeg_root / UU_FFMPEG_ROOT) or `ffmpeg` on PATH.
let mut command=match crate::config::ffmpeg_root() { 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 } 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"), None => Command::new("ffmpeg"),
}; };
command.args(["-hide_banner","-loglevel","error","-nostdin"]); command.args(["-hide_banner","-loglevel","error","-nostdin"]);
if encoder == Encoder::Vaapi { command.args(["-vaapi_device", &vaapi_device()]); }
if wayland { if wayland {
command.args(["-f","rawvideo","-pixel_format","bgra","-video_size",&size, command.args(["-f","rawvideo","-pixel_format","bgra","-video_size",&size,
"-framerate",&fps,"-i","pipe:0","-an"]); "-framerate",&fps,"-i","pipe:0","-an"]);
@@ -209,10 +239,14 @@ async fn read_encoder_with(
"-video_size",&size,"-i",&display,"-an"]); "-video_size",&size,"-i",&display,"-an"]);
} }
// 其他分辨率: scale the captured desktop (the host display mode is left untouched). // 其他分辨率: 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")]); 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", command.args(["-c:v","h264_nvenc","-preset","p4","-tune","ull",
"-profile:v","baseline","-pix_fmt","yuv420p","-g",&gop,"-bf","0", "-profile:v","baseline","-pix_fmt","yuv420p","-g",&gop,"-bf","0",
"-rc","vbr","-cq",&cq,"-b:v",&target,"-maxrate",&maxrate,"-bufsize",&bufsize, "-rc","vbr","-cq",&cq,"-b:v",&target,"-maxrate",&maxrate,"-bufsize",&bufsize,
@@ -236,7 +270,23 @@ async fn read_encoder_with(
.stderr(std::process::Stdio::inherit()) .stderr(std::process::Stdio::inherit())
.spawn().map_err(|e| format!("Start scoped encoder: {e}"))?; .spawn().map_err(|e| format!("Start scoped encoder: {e}"))?;
// Scope capture to this encoder. Dropping either task kills its child. // 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")?; let mut sink=child.stdin.take().ok_or("Encoder stdin missing")?;
Some(EncoderTask(tokio::spawn(async move { Some(EncoderTask(tokio::spawn(async move {
use tokio::io::AsyncWriteExt; use tokio::io::AsyncWriteExt;
@@ -402,6 +452,20 @@ pub async fn stream_desktop(pc: Arc<RTCPeerConnection>, track: Arc<TrackLocalSta
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; 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] #[tokio::test]
async fn encoder_restart_stops_old_producer_before_starting_new_chain() { async fn encoder_restart_stops_old_producer_before_starting_new_chain() {
use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::atomic::{AtomicBool, Ordering};
+181
View File
@@ -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(); } }
+12 -4
View File
@@ -198,7 +198,11 @@ impl WebRtcSession {
feedback_counts.observe(packet.as_any()); feedback_counts.observe(packet.as_any());
if packet.as_any().is::<webrtc::rtcp::payload_feedbacks::picture_loss_indication::PictureLossIndication>() if packet.as_any().is::<webrtc::rtcp::payload_feedbacks::picture_loss_indication::PictureLossIndication>()
|| packet.as_any().is::<webrtc::rtcp::payload_feedbacks::full_intra_request::FullIntraRequest>() { || packet.as_any().is::<webrtc::rtcp::payload_feedbacks::full_intra_request::FullIntraRequest>() {
idr_on_feedback.store(true, std::sync::atomic::Ordering::Release); // Dedupe only while an IDR is pending; the encoder clears the flag when it emits one,
// so a lost IDR does not block the next recovery request.
if !idr_on_feedback.load(std::sync::atomic::Ordering::Acquire) {
idr_on_feedback.store(true, std::sync::atomic::Ordering::Release);
}
} }
if let Some(report) = packet.as_any().downcast_ref::<webrtc::rtcp::receiver_report::ReceiverReport>() { if let Some(report) = packet.as_any().downcast_ref::<webrtc::rtcp::receiver_report::ReceiverReport>() {
reports += 1; reports += 1;
@@ -434,18 +438,22 @@ impl WebRtcSession {
let next_seq = || sequence.fetch_add(1, std::sync::atomic::Ordering::Relaxed); let next_seq = || sequence.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
// Default file-transfer folder: the desktop user's home directory. // Default file-transfer folder: the desktop user's home directory.
let home = std::env::var("HOME").unwrap_or_else(|_| "/".into()); 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 crate::system_metrics::Permission::Allow
} else { crate::system_metrics::Permission::Reject }; } 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 = [ let messages = [
(8, crate::initialization::encode_capture_change(next_seq(), timestamp, 0, 0, "Linux desktop")), (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)))), (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, (26, Ok(crate::initialization::envelope(next_seq(), timestamp, 26,
&crate::system_metrics::remote_download_path(&home)))), &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 { 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 { let message = match message {
Ok(message) => message, Ok(message) => message,
Err(e) => { log::error!("CONTROL initialization encode failed: {}", e); return; } Err(e) => { log::error!("CONTROL initialization encode failed: {}", e); return; }
+15 -1
View File
@@ -6,6 +6,17 @@ final class MarkerCheck: NSObject {
let kde = NSTextField(labelWithString: "KDE → Mac: not checked") let kde = NSTextField(labelWithString: "KDE → Mac: not checked")
let copied = NSTextField(labelWithString: "Mac marker: not copied") let copied = NSTextField(labelWithString: "Mac marker: not copied")
let formats = NSTextField(labelWithString: "Known text formats: not checked") let formats = NSTextField(labelWithString: "Known text formats: not checked")
// The official viewer drops remote format offers before ACK unless its
// remote window is key (viewer 0x100380f20 -> isKeyWindow). A delayed
// marker-only comparison lets it stay key throughout negotiation.
@objc func armGnome() {
gnome.stringValue = "GNOME → Mac: armed; return to UU and copy VM marker"
DispatchQueue.main.asyncAfter(deadline: .now() + 15) { self.compareGnome() }
}
@objc func armKde() {
kde.stringValue = "KDE → Mac: armed; return to UU and copy VM marker"
DispatchQueue.main.asyncAfter(deadline: .now() + 15) { self.compareKde() }
}
@objc func copyMarker() { @objc func copyMarker() {
NSPasteboard.general.clearContents() NSPasteboard.general.clearContents()
let ok = NSPasteboard.general.setString("UU-Mac-to-VM 中文🙂", forType: .string) let ok = NSPasteboard.general.setString("UU-Mac-to-VM 中文🙂", forType: .string)
@@ -32,14 +43,17 @@ final class MarkerCheck: NSObject {
NSButton(title: title, target: self, action: action) NSButton(title: title, target: self, action: action)
} }
func show() { func show() {
let window = NSWindow(contentRect: NSRect(x: 0,y: 0,width: 500,height: 360), styleMask: [.titled,.closable], backing: .buffered, defer: false) let window = NSWindow(contentRect: NSRect(x: 0,y: 0,width: 620,height: 500), styleMask: [.titled,.closable], backing: .buffered, defer: false)
window.title = "UU synthetic clipboard acceptance" window.title = "UU synthetic clipboard acceptance"
window.isReleasedWhenClosed = false window.isReleasedWhenClosed = false
let stack = NSStackView(views: [ let stack = NSStackView(views: [
NSTextField(labelWithString: "Only fixed test-marker matches are displayed."), NSTextField(labelWithString: "Only fixed test-marker matches are displayed."),
NSTextField(labelWithString: "Keep the UU remote window key until format ACK / text request."),
button("Copy Mac test marker", #selector(copyMarker)), copied, button("Copy Mac test marker", #selector(copyMarker)), copied,
button("Compare GNOME VM marker", #selector(compareGnome)), gnome, button("Compare GNOME VM marker", #selector(compareGnome)), gnome,
button("Compare KDE VM marker", #selector(compareKde)), kde, formats, button("Compare KDE VM marker", #selector(compareKde)), kde, formats,
button("Arm GNOME comparison in 15 seconds", #selector(armGnome)),
button("Arm KDE comparison in 15 seconds", #selector(armKde)),
button("Quit test fixture", #selector(quit))]) button("Quit test fixture", #selector(quit))])
stack.orientation = .vertical; stack.spacing = 14 stack.orientation = .vertical; stack.spacing = 14
stack.translatesAutoresizingMaskIntoConstraints = false stack.translatesAutoresizingMaskIntoConstraints = false
+7 -3
View File
@@ -1,5 +1,5 @@
"""Hold an ephemeral idle inhibition only in the isolated QEMU acceptance VM.""" """Hold an ephemeral idle inhibition only in the isolated QEMU acceptance VM."""
import pathlib,subprocess,signal,threading,sys import pathlib,subprocess,signal,threading,sys,os
assert pathlib.Path('/proc/sys/kernel/hostname').read_text().strip()=='uu-hyprland-test' assert pathlib.Path('/proc/sys/kernel/hostname').read_text().strip()=='uu-hyprland-test'
assert subprocess.check_output(['systemd-detect-virt'],text=True).strip()=='qemu' assert subprocess.check_output(['systemd-detect-virt'],text=True).strip()=='qemu'
import gi import gi
@@ -7,7 +7,11 @@ gi.require_version('Gio','2.0')
from gi.repository import Gio,GLib from gi.repository import Gio,GLib
seconds=min(4200,max(1,int(sys.argv[1]) if len(sys.argv)>1 else 3600)) seconds=min(4200,max(1,int(sys.argv[1]) if len(sys.argv)>1 else 3600))
bus=Gio.bus_get_sync(Gio.BusType.SESSION,None) bus=Gio.bus_get_sync(Gio.BusType.SESSION,None)
reply=bus.call_sync('org.freedesktop.ScreenSaver','/ScreenSaver','org.freedesktop.ScreenSaver','Inhibit',GLib.Variant('(ss)',('UU isolated acceptance','Timed live-connection acceptance')),GLib.VariantType.new('(u)'),Gio.DBusCallFlags.NONE,3000,None) gnome='GNOME' in os.environ.get('XDG_CURRENT_DESKTOP','').upper().split(':')
destination='org.gnome.SessionManager' if gnome else 'org.freedesktop.ScreenSaver'
object_path='/org/gnome/SessionManager' if gnome else '/ScreenSaver'
parameters=GLib.Variant('(susu)',('UU isolated acceptance',0,'Timed live-connection acceptance',8)) if gnome else GLib.Variant('(ss)',('UU isolated acceptance','Timed live-connection acceptance'))
reply=bus.call_sync(destination,object_path,destination,'Inhibit',parameters,GLib.VariantType.new('(u)'),Gio.DBusCallFlags.NONE,3000,None)
cookie=reply.unpack()[0] cookie=reply.unpack()[0]
stop=threading.Event() stop=threading.Event()
signal.signal(signal.SIGTERM,lambda *_:stop.set()) signal.signal(signal.SIGTERM,lambda *_:stop.set())
@@ -16,6 +20,6 @@ print('VM timed idle inhibition acquired',flush=True)
try:stop.wait(seconds) try:stop.wait(seconds)
finally: finally:
try: try:
bus.call_sync('org.freedesktop.ScreenSaver','/ScreenSaver','org.freedesktop.ScreenSaver','UnInhibit',GLib.Variant('(u)',(cookie,)),None,Gio.DBusCallFlags.NONE,3000,None) bus.call_sync(destination,object_path,destination,'Uninhibit' if gnome else 'UnInhibit',GLib.Variant('(u)',(cookie,)),None,Gio.DBusCallFlags.NONE,3000,None)
print('VM timed idle inhibition released',flush=True) print('VM timed idle inhibition released',flush=True)
except GLib.Error:print('VM idle inhibition connection closed',flush=True) except GLib.Error:print('VM idle inhibition connection closed',flush=True)