Compare commits

...
43 Commits
Author SHA1 Message Date
VallierDevandClaude Opus 5.5 fb09b60b8a Release v0.1.24
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-16 18:09:38 +08:00
GuanchengLiandClaude Code 37504c7fe8 Adapt FFmpeg output timing args to detected capabilities (#22)
* Adapt FFmpeg output timing args to detected capabilities

Probe the selected FFmpeg (PATH or UU_FFMPEG_ROOT/config ffmpeg_root)
once per process with `-h full` and cache the result per executable
environment (program + LD_LIBRARY_PATH + PATH). FFmpeg 5+ gets
`-fps_mode vfr -enc_time_base 1/<fps>`; older builds such as Ubuntu
22.04's FFmpeg 4.4 fall back to `-vsync 0`. Probe failures, timeouts
and unparseable help output all take the legacy path, and no output
side `-r` is ever emitted. HEVC availability probing stays independent.

Verified on FFmpeg 4.4.2: 20 video tests pass and desktop-video-probe
emits 5 H.264 frames with the first frame an IDR.

Co-Authored-By: Claude Code <[email protected]>

* Keep modern timing args when the FFmpeg probe fails

A failed probe (spawn error, non-zero exit, timeout, or no parseable
help text) previously fell back to -vsync 0, but -vsync is deprecated
since FFmpeg 5.1 and a newer build may reject it, killing the encoder.
Legacy is now selected only from a completed help dump that visibly
lacks -fps_mode/-enc_time_base; everything else keeps
-fps_mode vfr -enc_time_base 1/fps. Also fix the encoder comment: the
same-slot drop belongs to vfr on a 1/fps timebase, while -vsync 0
passes catch-up frames through (a known degradation on FFmpeg 4.x).

Co-Authored-By: Claude Code <[email protected]>

---------

Co-authored-by: Claude Code <[email protected]>
2026-10-16 18:09:13 +08:00
VallierDevandClaude Opus 5.5 41ba1e9440 Log whether an offer's ice_id equals the be-controlled ice_id (boolean only)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-16 17:17:54 +08:00
VallierDevandClaude Opus 5.5 1e4bec4514 Log offer SDP structure (attribute names, extmap URIs, payload counts only)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-16 15:57:49 +08:00
VallierDevandClaude Opus 5.5 b4b872878f Log the structure of incoming SOAC offers (keys, types, lengths only)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-16 15:44:14 +08:00
VallierDevandClaude Opus 5.5 b5b5120ef7 Release v0.1.23
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-16 13:26:02 +08:00
VallierDevandClaude Opus 5.5 5028ba1663 Test: pair only UDP/IPv4 host candidates in the codec negotiation test
Pairing every host interface (VPNs, IPv6) let ICE exceed the 10 s connect
timeout when the suite ran in parallel; the suite now finishes in 5 s.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-16 13:25:53 +08:00
VallierDevandClaude Opus 5.5 6058185d1f desktop-video-probe: UU_PROBE_QUALITY selects a viewer bitrate preset
The probe always used Auto (CRF), so the ABR path of the bitrate presets
(#15) could not be measured without a viewer.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-16 13:24:31 +08:00
VallierDevandClaude Opus 5.5 6882b76801 Wayland: free cached dma-buf on SHM fallback; recapture after a stall
Review follow-ups to #20: the copy-failure fallback cleared frame state
but no longer released cached dma-buf buffers, and after a backpressure
stall the engine's prefetched frame was as old as the stall. Release
v0.1.22.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-16 12:36:06 +08:00
VallierDevandClaude Opus 5.5 145bf9d25b Release v0.1.21
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-16 12:31:38 +08:00
wenmou e603761aa8 Video: reuse capture buffers, cache unchanged frames and propagate send backpressure (#20)
* Wayland: reuse DMA-BUF capture buffers and release protocol objects

* Video: cache unchanged frames and propagate send backpressure
2026-10-16 12:31:00 +08:00
VallierDevandClaude Opus 5.5 a28800ba44 Trust system certificates and show transport error causes (#21)
reqwest used only the bundled webpki roots, so TLS-intercepting proxies
with a custom CA (SSL_CERT_FILE) failed with a bare "error sending
request". Enable native roots (rustls-native-certs honors SSL_CERT_FILE
and SSL_CERT_DIR) and report the error source chain with URL user-info
and query strings removed. Release v0.1.20.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-16 12:27:19 +08:00
VallierDevandClaude Opus 5.5 c080b5f653 Do not report a viewer-closed desktop stream as an error
The final send after the viewer closes fails with "codec is not supported
by remote", which read like an encoder negotiation bug.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-16 11:47:58 +08:00
VallierDevandClaude Opus 5.5 c5ba87f3ad Log streamer_data element shapes and control id relation
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-16 11:45:04 +08:00
VallierDevandClaude Opus 5.5 edaa27fe6b Log the structure of connect requests (numbers, types, lengths only)
Documents what the viewer and cloud send in be-controlled (ConnectOptions
nesting, streamer_data and header value types) without rendering string
or byte contents, large numbers or identifiers.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-16 11:34:12 +08:00
VallierDevandClaude Opus 5.5 d630fc4128 Release v0.1.19
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-16 11:24:51 +08:00
VallierDevandClaude Opus 5.5 aeaf818c39 Signaling: treat a connection without pings as dead and reconnect (#19)
Only a close frame or read error ended the signaling loop, so a half-open
socket after suspend or a NAT drop left the host believing it was online.
Per Engine.IO 4, report Disconnected when no ping arrives within the
server's pingInterval + pingTimeout so the existing reconnect loop runs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-16 11:24:51 +08:00
VallierDevandClaude Opus 5.5 d5e9007dd6 Release v0.1.18
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-16 11:16:49 +08:00
VallierDevandGemini 4 Argon f876ee1ff3 Wayland: reuse per-slot SHM buffers across screencopy frames
Retain memfd, mmap, wl_shm_pool, and wl_buffer per pipeline slot when
dimensions, stride, and format remain unchanged, avoiding 33 MB 4K
allocations and unmaps on every frame. Rebuild on geometry/format
changes or aborted in-flight copies, and release all SHM resources on
reset/shutdown.

Co-Authored-By: Gemini 4 Argon <[email protected]>
2026-10-16 11:01:26 +08:00
VallierDevandClaude Opus 5.5 6cdc177b9e Wayland capture: pace from buffer availability; log capture exit
Review follow-ups to f0506bc: a stalled encoder read no longer shortens the
next capture interval, and the capture process exit status is logged
because nothing awaits its task.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-16 11:01:26 +08:00
VallierDevandClaude Opus 5.5 567662a576 X11: pick up desktop resizes between connections (#18)
The root geometry and XShm image were read once at startup, so after a
RandR or xrdp resize every new connection asked x11grab for the old size
and stalled on "transferring picture". Query the root geometry before
each desktop connection and rebuild the capture when it changed; guard
capture with a lock and reject a stale-sized buffer instead of
overflowing it. The HEVC fallback log now includes the actual failure.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-16 10:56:48 +08:00
VallierDevandClaude Opus 5.5 f0506bcf48 Wayland: pipe capture straight into the encoder and overlap capture with writes; fix terminal races
The native Wayland session relayed each 33 MB 4K frame through this process
and the capture loop wrote synchronously, so a slow encoder read delayed the
next screencopy request (~65 fps at a 144 fps request versus 87-92 fps for
bare capture, #16). Hand the capture stdout to FFmpeg directly, double-buffer
capture against the pipe write on a separate thread, and enlarge the pipe.

Terminal: hold the size lock across the ioctl so the delayed redraw restore
cannot reapply a stale size, and record scrollback and live output under the
sink lock so a reattach never replays a chunk that is also sent live.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-16 10:29:09 +08:00
VallierDevandClaude Opus 5.5 3d08d81b0d Log per-window frame counts in latest-frame stats
sent/skipped are cumulative while max_* are per-window peaks, which was
easy to misread (#16). Add window_s, window_sent and window_skipped.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-16 09:46:10 +08:00
VallierDevandClaude Opus 5.5 5e92abe373 Release v0.1.17
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-16 01:30:24 +08:00
VallierDevandClaude Opus 5.5 b63bedd4dc Repaint full-screen terminal programs on reattach; ABR for libx265 bitrate presets
Reattaching replays the scrollback tail, which starts mid-redraw for
full-screen programs (tmux, vim, TUIs), and an unchanged size raises no
SIGWINCH. Shrink the PTY by one row and restore the latest requested size
after a pause so the program repaints (#17).

Apply the #15 ABR change to the libx265 path as well, clarify the
non-wayland build error, and document the Wayland systemd unit and uinput
module autoload (#16).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-16 01:30:09 +08:00
wenmou 9c1ae123db Wayland: keep capturing without output power management; x264 ABR for bitrate presets (#15)
* Wayland: keep capturing when output power management is unavailable

keep_display_awake() runs on the desktop video start path, so a compositor\nwithout wlr-output-power-management made uu_wl_wake() return -1 and aborted\nthe whole desktop stream. Treat the missing protocol as nothing to wake.

* x264: average bitrate for the bitrate presets so 4K keyframes fit

Capped CRF plus VBV cannot fit a large IDR frame at 4K and a low cap, so\nkeyframes came out near-grey. NVENC and VAAPI already carry a target bitrate;\ngive x264 the same and keep CRF only for automatic quality.
2026-10-16 01:30:00 +08:00
VallierDevandClaude Opus 5.5 708c770b19 Support H.265 (HEVC), selected by the host when both sides support it
One video track carries H.264 or H.265 (RFC 7798 payloader); the host
announces its encoders with CONTROL Message 18 CodecNegotiation and picks
H.265 when the viewer negotiated it (the official Mac viewer offers H265
first; CaptureSettingRequest field 13 only reports the current codec).
hevc_nvenc / libx265 / hevc_vaapi with fallback to software HEVC, then
H.264; UU_VIDEO_CODEC=h264 forces H.264. True color 4:4:4 applies to H.265.
Verified with the official Mac viewer on NVENC.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-16 00:13:54 +08:00
VallierDevandClaude Opus 5.5 ff84aa4fd2 Support true color (H.264 4:4:4) and log viewer decoder capabilities
The viewer sends CaptureSettingRequest field 7 = 3 when 真彩 is enabled
(ff_capture_setting versions are cumulative, so our version 4 already
implies chroma sampling). Encode yuv444p with NVENC high444p / x264
high444 and report the encoded chroma in CaptureSettingSync; VAAPI and
inproc stay 4:2:0. Log ConnectOptions decoder_cap_list (numbers only).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-15 23:58:54 +08:00
VallierDevandClaude Opus 5.5 4df95ea271 Release v0.1.14
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-15 23:12:28 +08:00
cnetst be7dba4669 Fix black terminal after reconnecting with a large scrollback (#14) 2026-10-15 23:12:09 +08:00
VallierDevandClaude Opus 5.5 61dfb2edf5 Release v0.1.13
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-15 23:02:08 +08:00
VallierDevandClaude Opus 5.5 d4483e2bbd Tray icon (AppIndicator via dlopen, GtkStatusIcon fallback); show restarts the service
Tray menu: open main window, allow switch (synced), quit. Quit stops the
systemd user unit (found via /proc/self/cgroup) so Restart=always does not
bring it back; `uuremote-server show` starts the installed unit when the host
is not running. SIGUSR1 handler is installed before the pid file is written.
Verified on Ubuntu 24.04 GNOME (X11).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-15 23:02:08 +08:00
VallierDevandClaude Opus 5.5 0de74566a9 README: separate the Linux login screen limit from UU account sign-in
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-15 22:54:27 +08:00
VallierDevandClaude Opus 5.5 edee53a022 Release v0.1.12
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-15 22:50:24 +08:00
VallierDevandClaude Opus 5.5 fd3caf0d15 Mac shortcuts for macOS viewers (Cmd held back, terminal-aware)
With the viewer option 「使用 macOS 快捷键」 off, Cmd arrives as VK_LWIN
(verified: Cmd 0x5B, Option 0xA4, Ctrl 0xA2; controller_platform 4 = macOS).
For macOS controllers Cmd+C/V/A/F/X/Z become Ctrl+key, Cmd+C/V become
Ctrl+Shift+C/V in a focused terminal, Ctrl+C keeps interrupting; other
combos and Cmd alone stay Super. Verified with the official Mac viewer.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-15 22:50:24 +08:00
VallierDevandClaude Opus 5.5 ce7906510f Test: read the assist PTY banner until complete (flaky single read on CI)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-15 22:26:35 +08:00
VallierDevandClaude Opus 5.5 73462ee57f Release v0.1.11
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-15 22:22:08 +08:00
VallierDevandClaude Opus 5.5 bc683b3cef README: screenshot of the new main window (ID, code and account redacted)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-15 22:19:44 +08:00
VallierDevandClaude Opus 5.5 94039c092a Remote assistance (experimental) and unified host window
Like the official client: share a device ID + verification code; the other
person connects from 开始协助. Guest identity before sign-in, account identity
after (same share ID). One GTK window: 本设备 (allow switch, ID/code with copy,
verification modes incl. custom code, rotation schedule, copy & share) and
账号 (QR before sign-in, masked account + sign out after). Logged-in hosts keep
the window hidden; `uuremote-server show` / the desktop launcher opens it.
App icon and .desktop entry in contrib/desktop.

Protocol from the official Windows lite host; guest flow, wrong-code rejection,
rotation, switching off, sign-in/sign-out verified with the official Mac
viewer. Signed-in assistance from another account is not yet verified.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-15 22:17:25 +08:00
VallierDevandClaude Opus 5.5 03f611811c Release v0.1.10
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-15 20:53:59 +08:00
cnetst 1b7ec0a94f Fix terminal session replies for Windows and iOS clients (#12) 2026-10-15 20:53:22 +08:00
VallierDevandClaude Opus 5.5 89d209e7df Release v0.1.9
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-15 19:59:31 +08:00
VallierDevandClaude Opus 5.5 b5cc5ed736 Release input at desktop session boundaries; pipelined native Wayland capture
Squashed from the Codex v0.1.9 branch (reviewed):
- Clear stale input when a desktop session starts or ends and at service
  start/stop: X11 releases every pressed key/button on the display (a stuck
  XTest button blocked all clicks after a VM reboot), uinput resets its own
  devices, Wayland/portal sweep the keys this host can emit. A per-desktop
  lease stops late callbacks of an old connection from touching a new one;
  file/terminal/port-mapping peers never release desktop input.
- Native Wayland (--features wayland): double-buffered screencopy pipeline,
  viewer frame rate drives the capture rate, opt-in dma-buf (UU_WAYLAND_DMABUF=1,
  linear only, falls back to SHM). VM-measured only.
- Internal QA records (docs/validation, contrib/tests) removed from the public
  tree and ignored; they stay in the private project.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-15 19:45:12 +08:00
56 changed files with 5014 additions and 740 deletions

No files matched your search

+5
View File
@@ -1,3 +1,8 @@
target/
*.log
.DS_Store
# Internal QA records and handoffs stay private (kept under the parent project analysis/)
/docs/validation/
/docs/ACTIVE_HANDOFF.md
/contrib/tests/
+36 -7
View File
@@ -106,12 +106,15 @@ viewer does not reconnect them automatically.
| `~/.config/uuremote/host_settings.json` | persisted 安全 settings: auto_lock, keep_mute, mute_permanent |
| `UU_DESKTOP_BACKEND` | `auto` (default) / `x11` / `wayland` / `portal` / `kms` (experimental feature) / `none` (headless: no display; desktop offers ignored; tool peers get a nominal 1920x1080 screen + Allow permissions, which the port-mapping viewer requires) |
| `UU_INPUT_BACKEND` | `auto` (uinput on Wayland/portal when `/dev/uinput` is accessible, udev `uaccess` rule in README) / `uinput` / `native`. X11 keeps XTest (held-key autorepeat handling) |
| Mac viewer Cmd | With 「使用 macOS 快捷键」 off the Mac viewer sends Cmd=VK_LWIN 0x5B, Option=0xA4, Ctrl=0xA2; connect header `controller_platform`=4 is macOS. For Mac controllers Cmd is held back: Cmd+C/V/A/F/X/Z → Ctrl+key (terminal: Cmd+C/V → Ctrl+Shift+C/V), other combos/alone → Super |
| `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; `nvenc`: force; unset: NVENC→CPU only if `/proc/driver/nvidia/version` or `/dev/nvidia0` exists, else CPU (#10) |
| `UU_VIDEO_CODEC=h264` | force H.264 even when the viewer negotiated H.265 (default: H.265 when both sides support it) |
| `UU_FFMPEG_ROOT` | FFmpeg prefix (`bin/ffmpeg`, libs in `lib/x86_64-linux-gnu`) |
| `UU_AUDIO=0`, `UU_AUDIO_SOURCE` | disable sound / choose the Pulse source (default `@DEFAULT_MONITOR@`) |
| `UU_LOGIN_WINDOW=0` | don't auto-open the login window |
| `UU_REMOTE_ASSIST=0`, `UU_ASSIST_ALLOW=1`, `UU_ASSIST_UI=tty`, `UU_ASSIST_DEBUG=1` | disable assistance / allow at start / terminal UI for `assist` / include the server's short error message in assist errors |
Token has no expiry field; API code 1120 (HTTP 422) means invalid/expired → re-login. The config is re-read
every 60 s.
@@ -124,7 +127,7 @@ every 60 s.
| `src/signaling.rs` | Engine.IO v4 / Socket.IO client; `be-controlled` binary attachment = ConnectOptions (field 1 capture_type) |
| `src/auth.rs`, `src/config.rs`, `src/login.rs`, `src/login_ui.rs` | signed REST client, config, device init + QR login, GTK window |
| `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`, `src/video_track.rs`, `src/hevc.rs` | FFmpeg H.264/H.265 pipeline, one video track for both codecs (RFC 7798 HEVC payloader); viewer quality/fps/resolution/chroma settings |
| `src/audio.rs` | Pulse/PipeWire monitor → Opus audio track |
| `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) |
@@ -138,10 +141,10 @@ every 60 s.
| `src/desktop_backend.rs`, `src/wayland.rs`, `src/portal*.rs` | backend selection, Hyprland, GNOME/KDE portal |
| `c_engine/x11_engine.c`, `c_engine/clipboard.c` | X11 capture/input/selection |
| `c_engine/wayland_engine.c`, `c_engine/portal_engine.c` | Wayland and portal engines |
| `c_engine/login_window.c` | GTK3 login window |
| `src/assist.rs`, `c_engine/assist_window.c` | remote assistance (guest + account) and the unified GTK main window (assist + account/QR card) |
Diagnostic subcommands: `login [--no-gui]`, `unlock-check` (password on stdin, prints only accepted/rejected),
`desktop-probe [new.png]`, `desktop-video-probe`, `wayland-input-probe`, `clipboard-acceptance`,
`desktop-probe [new.png]`, `desktop-video-probe` (`UU_PROBE_FPS`, `UU_PROBE_FRAMES`, `UU_PROBE_QUALITY` 1-5 = viewer bitrate preset, 5 auto), `wayland-input-probe`, `clipboard-acceptance`,
`portal-acceptance`, `help`. Unknown arguments exit with code 2. Builds up to v0.1.0 did **not** reject them and
started a second full host server instead, so check that a subcommand exists in the binary you run.
@@ -162,7 +165,17 @@ started a second full host server instead, so check that a subcommand exists in
- CaptureSettingRequest: field 1 is an **fps option index** (1=30, 2=60, 3=90); the real frame rate is
field 18 `fpsCount`. Field 2 quality: 1 Fast, 2 General (高清 2M), 3 HD (超清 8M), 4 Bluray (原画 20M),
5 Auto, 6 Custom. Resolution: 16/17 pixel size or 5/6, 10 resolution_type (3 = follow remote).
Reply `RpcResponse{2 CaptureSettingResponse{}}` (no errors = applied) and send Message 25 CaptureSettingSync.
Field 7 `chorma_format` (ChromaFormat 1 = 4:2:0, 3 = 4:4:4) is sent when 真彩 is toggled; ff_capture_setting versions
are cumulative (3 CHROMA_SAMPLING, 4 CUSTOM_BITRATE, 5 DPI_SCALE, 6 HDR). 4:4:4 = NVENC `high444p` / x264 `high444`
with `yuv444p`; VAAPI and inproc stay 4:2:0.
Reply `RpcResponse{2 CaptureSettingResponse{}}` (no errors = applied) and send Message 25 CaptureSettingSync
(field 3 = the chroma actually encoded).
- ConnectOptions field 4 `decoder_cap_list` = DecoderCap {1 fps, 2 codec_type (1 H264, 2 H265), 3 width, 4 height,
5 chroma_format}. The Mac viewer lists H.264 and H.265, each 4:2:0 and 4:4:4, up to 3840x2160@120.
- Codec choice is the host's (official negotiateCodec): the Mac viewer offers H265 before H264 in SDP and
CaptureSettingRequest field 13 only reports its current codec. The host sends CONTROL Message 18
CodecNegotiation {1 EncoderCap {1 capture_mode=1, 2 fps, 3 codec_type, 4 width, 5 height}} at CONTROL open and
selects H.265 when both m-line codecs are bound (`UU_VIDEO_CODEC=h264` forces H.264).
- SystemMetrics (Message 13): GeneralMetric `2 system_sound_support` is an enum, **2 = supported** (1 = not
supported); SettingMetric 1 keep_mute, 2 mute_permanent, 3 auto_lock, 5 file_transfer_save_location,
8/9/10 permission states (2 = allow).
@@ -189,6 +202,23 @@ started a second full host server instead, so check that a subcommand exists in
`/qrcode/login/status` (1 wait, 2 scanned, 4 confirmed); `/login/by_qrcode` → user token. QR expires after
~5 min with HTTP 400 code 1190 (not fatal: generate a new one).
- Remote assistance (recipient side), live-verified with the official Mac viewer; protocol from the official
Windows lite host (GameViewerLite: GameViewerServer + GameViewerLiteInner). Guest (not signed in) and account
identities use parallel endpoints:
| step | guest | signed in |
|---|---|---|
| identity | register the Windows device, then `POST /api/v1/guest/create` (422/1001 if unregistered) | account token |
| room | `POST /api/v1/guest/room/create` **before** share info (else 400/2004) | the host's own room |
| ID + code owner | `POST /api/v1/guest/share/info` → `connect_id` | `GET /api/v1/device/share/info` (POST = 405) |
| withdraw | `POST /api/v1/guest/share/cancel` | `POST /api/v1/device/share/cancel` |
Cloud pushes arrive as `bmsg_push`: `get_control_mode {control_id}` → `POST …/room/share/upload_control_mode`
`{control_id, allow_control, control_mode: by_password}` (guest v1, account v2); the viewer only asks for the code
after this. `remote_control {control_id, salt}` → `upload/sign` (guest v1) / `upload_sign` (account v2)
`{control_id, can_remote_control, sign: sha256hex(salt+code), backup_sign}`. Temporary-only: sign=temp code,
empty backup; both: backup=custom; custom-only: sign=custom. The share ID stays the same across sign-in/out.
Same-account devices cannot assist each other (viewer: 无法对同账号的设备发起远程协助). On a signed-in host never gate
the owner's offers on the assist switch; the cloud already gates assist peers.
## 8. Known pitfalls
- webrtc-rs `Sample.duration` sets the gap to the **next** frame. FFmpeg 6.1 rejects `-r` together with
@@ -284,8 +314,7 @@ User-bus headless setup and the official Mac checklist: [virtual desktop accepta
`--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.
This capability is broad: it applies to the whole network-facing process and can expose greeter/lock screens that portal consent would hide. Use it only on a disposable machine with a reviewed binary; inspect with `getcap`, revoke with `sudo setcap -r <path>`. A VM greeter capture was solid black, so lock/greeter screens are not supported.
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.
No official "no desktop" error code was found for headless desktop offers, so the existing offer behavior is retained.
Generated
+71 -1
View File
@@ -382,6 +382,22 @@ version = "0.9.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2459377285ad874054d797f3ccebf984978aa39129f6eafde5cdc8315b612f8"
[[package]]
name = "core-foundation"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b2a6cd9ae233e7f62ba4e9353e81a88df7fc8a5987b8d445b4d90c879bd156f6"
dependencies = [
"core-foundation-sys",
"libc",
]
[[package]]
name = "core-foundation-sys"
version = "0.8.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b"
[[package]]
name = "cpufeatures"
version = "0.2.17"
@@ -969,6 +985,7 @@ dependencies = [
"hyper",
"hyper-util",
"rustls 0.23.45",
"rustls-native-certs",
"tokio",
"tokio-rustls",
"tower-service",
@@ -1417,6 +1434,12 @@ version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c08d65885ee38876c4f86fa503fb49d7b507c2b62552df7c70b2fce627e06381"
[[package]]
name = "openssl-probe"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7c87def4c32ab89d880effc9e097653c8da5d6ef28e6b539d313baaacfbafcbe"
[[package]]
name = "p256"
version = "0.13.2"
@@ -1797,6 +1820,7 @@ dependencies = [
"pin-project-lite",
"quinn",
"rustls 0.23.45",
"rustls-native-certs",
"rustls-pki-types",
"serde",
"serde_json",
@@ -1946,6 +1970,18 @@ dependencies = [
"zeroize",
]
[[package]]
name = "rustls-native-certs"
version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dab5152771c58876a2146916e53e35057e1a4dfa2b9df0f0305b07f611fdea4d"
dependencies = [
"openssl-probe",
"rustls-pki-types",
"schannel",
"security-framework",
]
[[package]]
name = "rustls-pki-types"
version = "1.15.1"
@@ -1989,6 +2025,15 @@ version = "1.0.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9774ba4a74de5f7b1c1451ed6cd5285a32eddb5cccb8cc655a4e50009e06477f"
[[package]]
name = "schannel"
version = "0.1.29"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "91c1b7e4904c873ef0710c1f407dde2e6287de2bebc1bbbf7d430bb7cbffd939"
dependencies = [
"windows-sys 0.61.2",
]
[[package]]
name = "scopeguard"
version = "1.2.0"
@@ -2031,6 +2076,29 @@ dependencies = [
"zeroize",
]
[[package]]
name = "security-framework"
version = "3.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b7f4bc775c73d9a02cde8bf7b2ec4c9d12743edf609006c7facc23998404cd1d"
dependencies = [
"bitflags 2.13.2",
"core-foundation",
"core-foundation-sys",
"libc",
"security-framework-sys",
]
[[package]]
name = "security-framework-sys"
version = "2.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ce2691df843ecc5d231c0b14ece2acc3efb62c0a398c7e1d875f3983ce020e3"
dependencies = [
"core-foundation-sys",
"libc",
]
[[package]]
name = "semver"
version = "1.0.28"
@@ -2649,8 +2717,9 @@ checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
[[package]]
name = "uu-remote-linux"
version = "0.1.8"
version = "0.1.24"
dependencies = [
"async-trait",
"base64 0.22.1",
"bytes",
"cc",
@@ -2664,6 +2733,7 @@ dependencies = [
"log",
"nix 0.29.0",
"qrcode",
"rand 0.8.8",
"reqwest",
"serde",
"serde_json",
+4 -2
View File
@@ -1,6 +1,6 @@
[package]
name = "uu-remote-linux"
version = "0.1.8"
version = "0.1.24"
edition = "2021"
authors = ["uu-remote-linux contributors"]
description = "Unofficial Linux host for NetEase UU Remote: control a Linux desktop from the official clients"
@@ -29,13 +29,15 @@ tokio-tungstenite = { version = "0.23", features = ["rustls-tls-webpki-roots"] }
futures-util = "0.3"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
reqwest = { version = "0.12", default-features = false, features = ["json", "rustls-tls"] }
reqwest = { version = "0.12", default-features = false, features = ["json", "rustls-tls", "rustls-tls-native-roots"] }
hmac = "0.12"
sha2 = "0.10"
hex = "0.4"
base64 = "0.22"
flate2 = "1.0"
bytes = "1"
async-trait = "0.1"
rand = "0.8"
webrtc = "0.10"
libc = "0.2"
nix = { version = "0.29", features = ["term", "process", "fs", "ioctl", "signal"] }
+75 -10
View File
@@ -12,7 +12,7 @@
> If NetEase considers this project infringing or inappropriate, please DM me on X
> **[@ai_fengshou](https://x.com/ai_fengshou)** and I will take it down right away.
<p align="center"><img src="docs/images/login-window.png" width="560" alt="QR login window on the Linux host"></p>
<p align="center"><img src="docs/images/main-window.png" width="620" alt="Host main window: remote assistance and account"></p>
<p align="center"><sub>The QR login window shown on first run (QR code blurred). The UI is in Chinese, like the official app.</sub></p>
<!-- Screenshot placeholder: the Linux desktop controlled from the official client. Take your own
@@ -31,10 +31,11 @@
| Feature | Notes |
|---|---|
| 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 |
| Main window and QR login | One window with "This device" (remote assistance) and "Account": a QR code before sign-in, the account and a sign-out button after; `login --no-gui` prints the QR in a terminal |
| Remote assistance (experimental) | Like the official client: share the **device ID + verification code**; the other person enters them under 开始协助 in UU Remote. Works before sign-in (guest identity) and after; see below |
| 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; 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) |
| Mac shortcuts | **Recommended**: turn off 「使用 macOS 快捷键」 (use macOS shortcuts) in the Mac viewer's remote-window menu; then Cmd+C/V/A/F/X/Z work as on a Mac (Cmd+C/V become Ctrl+Shift+C/V in terminals) and Ctrl+C always interrupts. With the option on, the viewer sends Cmd as Ctrl: in a terminal Ctrl+V / Cmd+V paste, Ctrl+C interrupts, copy is Cmd+Shift+C (X11) |
| Pointer shape | Remote cursor shape is mirrored to the viewer (X11) |
| Clipboard | Two-way text sync |
| Quality / frame rate / resolution | Auto, Original 20M, Ultra 8M, HD 2M, custom; 30/60/90/144 fps; "other resolutions" are scaled by the encoder, the host monitor is left untouched |
@@ -49,6 +50,12 @@
| 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) |
Desktop connection boundaries and host startup clear stale input; delayed events
from an old desktop cannot affect its replacement. X11 clears pressed state on
the authenticated display (including physical keys whose origin it cannot
distinguish); uinput clears only this host's virtual devices, and Portal only its
authorized session. Independent file/terminal tool peers do not reset input.
## Supported Linux desktops
| Desktop | Display protocol | Status | Build |
@@ -64,22 +71,32 @@ 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
`journalctl --user -u uuremote-native.service`.
Wayland limits: single screen, fixed resolution. The native Hyprland backend captures via shm screencopy, so the frame rate is bounded by the compositor (about 16–22 fps measured at 2560x1600) and the viewer fps option has no effect. The portal backend does not support screen wake or
Wayland limits: single screen, fixed resolution. The native Hyprland backend now pipelines two shm screencopy buffers and passes the viewer's requested fps to both capture pacing and the encoder. Actual fps remains limited by the compositor, rendering and readback (the old serial path measured about 16–22 fps at 2560x1600; the pipeline is VM-measured only, not a hardware guarantee). The portal backend does not support screen wake or
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.
The native Wayland backend needs `wlr-output-power-management` from the compositor to wake a turned-off screen; without it only wake is unavailable — capture and encoding are unaffected.
Native Wayland experiments: `UU_WAYLAND_PIPELINE=0` disables pipelining for comparison.
`UU_WAYLAND_DMABUF=1` explicitly tries dma-buf → GBM CPU mapping (off by default;
requires runtime libgbm; `UU_WAYLAND_DMABUF_DEVICE` overrides `/dev/dri/renderD128`).
Only single-plane 8-bit RGB with an advertised linear modifier is supported;
failure falls back to SHM. The virgl VM verified fallback only: successful import,
mapping and hardware acceleration remain unverified. This is not zero-copy VAAPI;
`UU_VIDEO_ENCODER=vaapi` still uploads CPU frames through FFmpeg.
## Not implemented / not supported
| Feature | Status |
|---|---|
| Switching between multiple monitors | In progress |
| True color (4:4:4) | Not implemented; not declared in the handshake, so the client does not enable it |
| True color (4:4:4) | Supported (v0.1.15): the default FFmpeg pipeline with NVENC / libx264 encodes H.264 High 4:4:4; VAAPI and `inproc-av` stay 4:2:0 and report that to the client |
| HDR | Not implemented |
| Super screen / virtual display | Not implemented |
| Privacy screen on the host | Not implemented |
| Microphone (viewer audio played on the host) | Not implemented (needs a virtual sound card) |
| Plugins such as the WoW queue notifier | Not supported; the client is told that no plugins exist |
| Access before login (greeter / no user logged in) | The greeter cannot be controlled; without a logged-in user use headless mode or a virtual desktop (see below) |
| Linux login screen | The system login screen (before anyone logs in to the Linux desktop) cannot be controlled yet; use headless mode or a virtual desktop (see below). This is about the Linux login: **without a UU account** the host can now be used through remote assistance |
| 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 |
@@ -166,6 +183,37 @@ 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.
## Remote assistance and the main window (experimental)
As in the official client, this host shows a **device ID** and a **verification code**; whoever you give them to can
connect from 开始协助 (remote assistance) in UU Remote.
- **Not signed in**: the main window opens at start, offers remote assistance as a guest and shows the sign-in QR code.
Signing in switches to account mode automatically.
- **Signed in**: the host is in your device list and remote assistance keeps working (same device ID). A background
service keeps the window hidden; open it with `uuremote-server show` or the "UU Remote Linux Host" launcher.
Closing the window only hides it.
- **Tray icon**: a background host shows a tray icon with Open main window, the allow switch and Quit. GNOME needs
the AppIndicator extension (bundled on Ubuntu); Xfce / Cinnamon / KDE work as is. Quit stops `uuremote-native.service`;
the launcher starts it again.
- **Settings** (as in the official client): allow switch (off by default); verification by temporary code, custom code
(8–16 letters and digits) or either; temporary code rotation after each session / hourly / 12 h / 24 h / never;
copy buttons for the ID and the code. Stored in the 0600 config.
- The code is only shown in the local window, never logged. Without a desktop, `uuremote-server assist` shows it on the
terminal (`allow` / `deny` / `reset` / `mode …` / `refresh …` / `off`).
- **Security**: the ID and code give full control (desktop, files, terminal). Share them only with people you trust.
Optional launcher icon:
```sh
install -Dm644 contrib/desktop/uu-remote-linux.svg ~/.local/share/icons/hicolor/scalable/apps/uu-remote-linux.svg
install -Dm644 contrib/desktop/uu-remote-linux.desktop ~/.local/share/applications/uu-remote-linux.desktop
```
Verified with the official Mac viewer: guest connections, wrong-code rejection, code rotation, switching assistance off,
signing in and out. **Remote assistance while signed in has not yet been tested from a different account** (devices of
the same account cannot assist each other, an official restriction).
## Servers without a monitor
**Headless mode** (terminal, file transfer and port mapping only): add `Environment=UU_DESKTOP_BACKEND=none` to the
@@ -206,12 +254,16 @@ Common environment variables:
| `UU_VIDEO_ENCODER` | `cpu`: libx264; `vaapi`: opt-in Intel/AMD; `nvenc`: force NVENC; unset: NVENC only when an NVIDIA driver is loaded, else CPU |
| `UU_FFMPEG_ROOT` | Use FFmpeg from this prefix |
| `UU_AUDIO=0` | Disable sound; `UU_AUDIO_SOURCE` picks the PulseAudio source |
| `UU_REMOTE_ASSIST=0` | Disable remote assistance (no guest assistance before sign-in either) |
| `UU_ASSIST_ALLOW=1` | Allow remote assistance at start (overrides the saved switch) |
| `UU_LOGIN_WINDOW=0` | Do not open the login window automatically when credentials are missing |
| `UU_PORT_MAPPING=0` | Disable port mapping (the official host's controlled-side switch) |
Host settings such as "lock after remote session" and "mute host" are stored in
`~/.config/uuremote/host_settings.json`.
With external FFmpeg CPU encoding, the bitrate presets (Original / Ultra / HD / custom) use average bitrate (ABR) to avoid gray keyframes at 4K and low bitrates; Auto keeps constant rate factor (CRF).
## Let an AI agent install or develop it
If you use Claude Code, Codex, Cursor or a similar coding agent, have it read [AGENTS.md](AGENTS.md) first.
@@ -263,6 +315,16 @@ Released under the [GPL-3.0](LICENSE): you may use, modify and redistribute it,
- [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.
### H.265 / HEVC
When both sides support H.265 the host uses it (as the official host does: the host picks the codec from both
capability lists; the official Mac viewer lists H265 first in its SDP). The first desktop connection probes a
synthetic frame and announces encoder support with CodecNegotiation. `hevc_nvenc`, `libx265` and explicit
`UU_VIDEO_ENCODER=vaapi` (`hevc_vaapi`); hardware failure falls back to software HEVC, then to H.264.
`UU_VIDEO_CODEC=h264` forces H.264. 8-bit; true color 4:4:4 uses hevc_nvenc `rext` / libx265 `main444-8`.
Verified with the official Mac viewer on an RTX 3090 (hevc_nvenc, including 4:4:4); libx265, hevc_vaapi and
other viewers are not hardware-verified.
### VAAPI (opt-in / 显式启用)
`UU_VIDEO_ENCODER=vaapi` selects Intel/AMD H.264 VAAPI; `UU_VAAPI_DEVICE` defaults to
@@ -306,10 +368,14 @@ 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 uinput, load `uinput` (at boot: `echo uinput | sudo tee /etc/modules-load.d/uinput.conf`) 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.
In Hyprland / uwsm sessions `default.target` may start before the compositor exports `WAYLAND_DISPLAY`;
tie the unit to the graphical session instead (community-verified autostart on Omarchy):
`After=` and `PartOf=graphical-session.target`, `ConditionEnvironment=WAYLAND_DISPLAY`,
`Environment=UU_DESKTOP_BACKEND=wayland` (no `DISPLAY`), and `WantedBy=graphical-session.target`.
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`.
@@ -322,8 +388,7 @@ User-bus headless setup and the official Mac checklist: [virtual desktop accepta
`--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.
This capability is broad: it applies to the whole network-facing process and can expose greeter/lock screens that portal consent would hide. Use it only on a disposable machine with a reviewed binary; inspect with `getcap`, revoke with `sudo setcap -r <path>`. A VM greeter capture was solid black, so lock/greeter screens are not supported.
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.
No official "no desktop" error code was found for headless desktop offers, so the existing offer behavior is retained.
+69 -10
View File
@@ -10,7 +10,7 @@
> 这是个人兴趣项目,**与网易无关,也不是官方客户端**。如果网易认为本项目侵权或有不妥,
> 请在推特私信我 **[@ai_fengshou](https://x.com/ai_fengshou)**,我会第一时间自行下架。
<p align="center"><img src="docs/images/login-window.png" width="560" alt="Linux 主机扫码登录窗口"></p>
<p align="center"><img src="docs/images/main-window.png" width="620" alt="Linux 主机主窗口:远程协助与账号"></p>
<p align="center"><sub>首次运行时弹出的扫码登录窗口(二维码已打码)</sub></p>
<!-- 截图占位:在官方客户端里远控 Linux 桌面的效果图。请自行截图并去掉账号、设备名等信息后放到
@@ -27,10 +27,11 @@
| 功能 | 说明 |
|---|---|
| 扫码登录 | GTK 登录窗口(或 `login --no-gui` 终端二维码);二维码过期自动刷新;登录凭证失效时服务会自动弹出登录窗口 |
| 主窗口与扫码登录 | 一个窗口包含「本设备」(远程协助)和「账号」两部分:未登录时显示扫码二维码,登录后显示账号并可退出登录;也可用 `login --no-gui` 在终端扫码 |
| 远程协助(实验性) | 像官方客户端一样,把**设备 ID + 验证码**发给别人,对方在 UU「开始协助」里输入即可连接;登录前(访客身份)和登录后都能用,见下文 |
| 远程桌面 | H.264 视频,NVIDIA 显卡用 NVENC 硬件编码,否则用 libx264;查看端绘制本地鼠标,不会出现双光标 |
| 鼠标 / 键盘 | 绝对/相对移动、点击、滚轮、按键;修复了网络延迟导致的按键连发;Wayland 下可用 uinput 虚拟键鼠(不依赖键盘布局,见下文) |
| 终端快捷键 | Mac 控制端的 Cmd 和 Ctrl 到被控端都是 Ctrl。焦点在终端窗口时,Ctrl+V / Cmd+V 自动转成 Ctrl+Shift+V 粘贴;Ctrl+C 始终是中断,复制请用 Cmd+Shift+C(X11) |
| Mac 快捷键 | **推荐**:在 Mac 远控窗口菜单里关闭「使用 macOS 快捷键」,之后和在 Mac 上一样:Cmd+C/V/A/F/X/Z 是复制、粘贴、全选、查找、剪切、撤销(终端里 Cmd+C/V 自动变成 Ctrl+Shift+C/V),Ctrl+C 在终端里始终是中断。保持开启时,客户端会把 Cmd 和 Ctrl 都当成 Ctrl 发送:终端里 Ctrl+V / Cmd+V 自动转成粘贴,Ctrl+C 是中断,复制用 Cmd+Shift+C(X11) |
| 鼠标指针样式 | 远端指针形状实时同步(X11) |
| 剪贴板 | 双向文本同步 |
| 画质 / 帧率 / 分辨率 | 自动、原画 20M、超清 8M、高清 2M、自定义;30/60/90/144 帧;「其他分辨率」由编码器缩放,不改动主机显示器 |
@@ -45,6 +46,10 @@
| 多次重连 | 每个工具窗口独立连接,互不影响 |
| 无头服务器 | 没有显示器也能上线,提供终端、文件传输和端口映射;也可以用 Xvfb 虚拟桌面提供完整远程桌面(见下文) |
桌面连接建立、结束及服务启动时会清理遗留输入状态;旧连接的迟到事件不能影响新的桌面连接。
X11 清理当前已认证显示上的按下状态(包括无法区分来源的实际按键);uinput 仅清理本服务虚拟设备,
Portal 仅作用于本服务获授权的会话。文件传输、终端等独立工具连接不触发桌面松键。
## 支持的 Linux 桌面
| 桌面环境 | 显示协议 | 状态 | 编译方式 |
@@ -59,7 +64,11 @@
除 GNOME(X11)外,其余桌面目前都是在虚拟机里测试的。如果你在真机上遇到问题,欢迎[提交 issue](../../issues),
请附上发行版、桌面环境、显卡型号和 `journalctl --user -u uuremote-native.service` 里的相关日志。
Wayland 下的限制:只支持单屏、固定分辨率;Hyprland 原生后端通过 shm screencopy 截屏,帧率受合成器限制(2560x1600 实测约 16~22 帧),控制端的帧率选项不生效;portal 后端暂不支持唤醒屏幕和动态分辨率;剪贴板只在控制端的远控窗口处于前台时同步。
Wayland 下的限制:只支持单屏、固定分辨率;Hyprland 原生后端默认使用双缓冲流水线 shm screencopy,控制端帧率设置会同步到采集节拍和编码器,但实际帧率仍受合成器、渲染和读回速度限制(旧串行路径在 2560x1600 曾测得约 16~22 帧;流水线只在虚拟机里测量过,不代表实机保证);portal 后端暂不支持唤醒屏幕和动态分辨率;剪贴板只在控制端的远控窗口处于前台时同步。
原生 Wayland 后端需要合成器提供 `wlr-output-power-management` 才能唤醒已关闭的屏幕;缺少该协议不影响采集和编码。
原生 Wayland 实验选项:`UU_WAYLAND_PIPELINE=0` 可关闭流水线做对照。`UU_WAYLAND_DMABUF=1` 显式尝试 dma-buf→GBM CPU 映射(默认关闭,需要运行时 libgbm;可用 `UU_WAYLAND_DMABUF_DEVICE` 指定 render node,默认 `/dev/dri/renderD128`)。仅支持显式线性修饰符、单平面 8-bit RGB,失败回退 SHM;virgl VM 中只验证了回退,成功导入/映射和硬件加速均未验证。这不是零拷贝 VAAPI 路径;选择 `UU_VIDEO_ENCODER=vaapi` 时仍由 FFmpeg 上传 CPU 帧。
首次连接时,桌面会弹窗请求「远程桌面 / 屏幕共享」授权,同意后会记住授权。
## 尚未实现 / 不支持
@@ -67,13 +76,13 @@ Wayland 下的限制:只支持单屏、固定分辨率;Hyprland 原生后端
| 功能 | 状态 |
|---|---|
| 多显示器切换 | 开发中 |
| 真彩(4:4:4) | 未实现,握手时不声明,客户端不会启用 |
| 真彩(4:4:4) | 支持(v0.1.15):NVENC / libx264 的默认 FFmpeg 管线输出 H.264 High 4:4:4;VAAPI 和 `inproc-av` 管线仍为 4:2:0,并如实回报给客户端 |
| HDR | 未实现 |
| 超级屏 / 虚拟显示器 | 未实现 |
| 被控端防窥模式 | 未实现 |
| 麦克风(控制端声音传到被控端) | 未实现(需要虚拟声卡) |
| 魔兽世界排队提醒等插件 | 不支持,会明确告诉客户端「无插件」 |
| 登录前(登录界面 / 无人登录)访问 | 不支持控制登录界面;没有人登录时可以用无头模式或虚拟桌面(见下文) |
| Linux 系统登录界面 | 还不支持控制开机后的系统登录界面(还没有人登录 Linux 桌面时);这种情况可以用无头模式或虚拟桌面(见下文)。注意:这里说的是 Linux 系统登录,**不登录 UU 账号**现在可以通过「远程协助」使用 |
| 跨运营商 / 受限网络的中继连接 | 已接入官方下发的 ICE 服务器,但复杂网络环境未充分测试 |
| 作为控制端(在 Linux 上控制别的电脑) | 不在本项目范围 |
@@ -158,6 +167,27 @@ sudo udevadm control --reload && sudo udevadm trigger --name-match=uinput
`uaccess` 只把权限给当前坐在这台机器前的登录用户。`UU_INPUT_BACKEND=uinput|native` 可以强制指定后端。
X11 默认继续用 XTest,因为它能正确处理网络延迟下按住不放的按键。
## 远程协助与主窗口(实验性)
和官方客户端的「远程协助」一样:本机显示**设备 ID** 和**验证码**,把它们发给信任的人,对方在 UU 远程的「开始协助」里输入即可控制本机。
- **未登录**:启动后直接显示主窗口,用访客身份提供远程协助,同时显示扫码登录二维码。扫码登录后自动切换到账号模式。
- **已登录**:本机出现在你的设备列表里,可以直接连;远程协助也照常可用(同一设备 ID)。服务在后台运行时窗口默认隐藏,用 `uuremote-server show` 或应用菜单里的「UU 远程 Linux 主机」打开。关闭窗口只是隐藏,不会停止服务。
- **托盘图标**:后台运行时顶栏/任务栏有托盘图标,菜单里可以「打开主窗口」、切换「允许他人远程协助」、「退出」。GNOME 需要 AppIndicator 扩展(Ubuntu 自带),Xfce / Cinnamon / KDE 直接可用。从托盘退出会停止 `uuremote-native.service`,之后从应用菜单打开会重新启动服务。
- **设置**(与官方一致):「允许他人远程协助」开关(默认关闭);验证方式:仅临时验证码 / 仅自定义验证码(8–16 位,需同时包含字母和数字) / 两种都可以;临时验证码自动刷新:每次远控结束 / 每小时 / 每 12 小时 / 每 24 小时 / 不刷新;ID 和验证码旁各有「复制」按钮。设置保存在本机配置文件(0600)。
- **只在本机窗口显示**:验证码不会写进日志。没有图形界面时,单独运行 `uuremote-server assist` 会在终端显示(`allow` / `deny` / `reset` / `mode …` / `refresh …` / `off`)。
- **安全**:拿到 ID 和验证码的人可以完全控制这台电脑(桌面、文件、终端)。只发给信任的人,用完关掉开关或刷新验证码。
安装应用菜单图标(可选):
```sh
install -Dm644 contrib/desktop/uu-remote-linux.svg ~/.local/share/icons/hicolor/scalable/apps/uu-remote-linux.svg
install -Dm644 contrib/desktop/uu-remote-linux.desktop ~/.local/share/applications/uu-remote-linux.desktop
```
验证情况:访客身份下的连接、错误验证码拒绝、用完换码、关闭后不可连接、登录与退出登录的切换,已用官方 Mac 客户端实测通过。
**登录状态下的远程协助尚未用另一个账号实测**(同一账号的设备不能互相发起远程协助,这是官方限制)。
## 没有显示器的服务器
**无头模式**:只要终端、文件传输和端口映射,不需要图形界面。在服务里加 `Environment=UU_DESKTOP_BACKEND=none`,
@@ -197,10 +227,14 @@ systemctl --user restart uuremote-native.service
| `UU_FFMPEG_ROOT` | 使用指定目录下的 FFmpeg |
| `UU_AUDIO=0` | 关闭声音转发;`UU_AUDIO_SOURCE` 指定 PulseAudio 采集源 |
| `UU_LOGIN_WINDOW=0` | 凭证缺失时不自动弹出登录窗口 |
| `UU_REMOTE_ASSIST=0` | 关闭远程协助(未登录时也不再以访客身份提供协助) |
| `UU_ASSIST_ALLOW=1` | 启动时就允许他人远程协助(覆盖窗口里保存的开关状态) |
| `UU_PORT_MAPPING=0` | 关闭端口映射(对应官方被控端的开关) |
「远程结束后自动锁屏」「静音运行」等设置保存在 `~/.config/uuremote/host_settings.json`。
选择码率档(原画 / 超清 / 高清 / 自定义)时,外部 FFmpeg 的 CPU 编码以平均码率(ABR)为目标,避免 4K 低码率下关键帧发灰;「自动」档仍使用恒定质量(CRF)。
## 让 AI 帮你安装或开发
如果你用 Claude Code、Codex、Cursor 等 AI 编程工具,让它先阅读 [AGENTS.md](AGENTS.md)。
@@ -245,6 +279,14 @@ flowchart LR
- [webrtc-rs](https://github.com/webrtc-rs/webrtc)、FFmpeg、GTK 等开源项目。
- 本项目的大部分代码在 Claude 和 Codex 的协助下完成。
### H.265 / HEVC
控制端和主机都支持 H.265 时,主机自动使用 H.265(与官方主机一致,由主机按双方能力选择;官方 Mac 客户端在 SDP
中优先列出 H.265)。首次桌面连接会用合成帧探测实际编码能力,并通过 CodecNegotiation 告知客户端。支持 `hevc_nvenc`、
`libx265`、显式 `UU_VIDEO_ENCODER=vaapi` 的 `hevc_vaapi`;硬件失败先回落软件 HEVC,再回落 H.264。
`UU_VIDEO_CODEC=h264` 强制使用 H.264。8-bit;真彩 4:4:4 使用 hevc_nvenc `rext` / libx265 `main444-8`。
已用官方 Mac 客户端在 RTX 3090(hevc_nvenc,含 4:4:4)实机验收;libx265、hevc_vaapi 及其他客户端尚未实机验证。
### VAAPI 硬件编码(显式启用)
设置 `UU_VIDEO_ENCODER=vaapi` 使用 Intel/AMD H.264 VAAPI;`UU_VAAPI_DEVICE` 默认
@@ -281,21 +323,38 @@ cargo build --release --features wayland
Omarchy 使用 Hyprland:在其用户会话中运行,设置 `UU_DESKTOP_BACKEND=wayland`,保留
`WAYLAND_DISPLAY` 和 `XDG_RUNTIME_DIR`。其他合成器可能需要 `--features wayland,portal`。
uinput 需加载内核模块并应用上文 uaccess 规则;无人登录账号需要专用组的受限授权。
uinput 需加载内核模块(开机自动加载:`echo uinput | sudo tee /etc/modules-load.d/uinput.conf`)并应用上文 uaccess 规则;无人登录账号需要专用组的受限授权。
该权限能向整个 seat(包括锁屏/登录界面)注入输入。设置 `UU_INPUT_BACKEND=uinput` 后,
仍须确认启动日志实际选择了 uinput。
Hyprland / uwsm 等会话中,`default.target` 可能早于合成器导出 `WAYLAND_DISPLAY`。systemd 单元可改为随图形会话启动
(社区在 Omarchy 上验证过开机自启):
```ini
[Unit]
After=graphical-session.target
PartOf=graphical-session.target
ConditionEnvironment=WAYLAND_DISPLAY
[Service]
Environment=UU_DESKTOP_BACKEND=wayland
# 其余同上文 X11 示例,去掉 DISPLAY
[Install]
WantedBy=graphical-session.target
```
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)。
不要降级系统 FFmpeg。
### 实验性 KMS 截屏
编译 `--features kms` 后显式设置 `UU_DESKTOP_BACKEND=kms`;`UU_KMS_DEVICE` 默认
`/dev/dri/card0`。需要 libdrm、card 节点访问权及主动授予的 `cap_sys_admin+ep`。
程序不会自动启用或授权;此 capability 权限很大,先阅读[限制、安全风险及撤销步骤](docs/validation/kms.md)。
程序不会自动启用或授权;此 capability 权限很大,此 capability 权限很大:它作用于整个联网的服务进程,也能看到登录界面/锁屏等原本受授权保护的画面;只在可丢弃的机器上、对审查过的二进制使用,用 `getcap` 检查,`sudo setcap -r <路径>` 撤销。目前在虚拟机里登录界面截图全黑,不支持锁屏/登录界面。
当前仅为线性帧缓冲原型:VM 的 GDM 截图仍是黑屏,**没有通过登录界面视觉验收**,不承诺锁屏可用。
键鼠需 uinput;没有桌面会话时不提供剪贴板。
无头桌面错误码的[官方证据仍不足](docs/validation/headless-error-evidence.md),因此保持原有 offer 行为。
无头模式下桌面连接没有找到官方的「无桌面」错误码,因此保持原有 offer 行为。
+3 -4
View File
@@ -2,7 +2,7 @@ fn main() {
println!("cargo:rerun-if-changed=c_engine/portal_clipboard.inc");
println!("cargo:rerun-if-changed=c_engine/x11_engine.c");
println!("cargo:rerun-if-changed=c_engine/clipboard.c");
println!("cargo:rerun-if-changed=c_engine/login_window.c");
println!("cargo:rerun-if-changed=c_engine/assist_window.c");
println!("cargo:rerun-if-changed=c_engine/pam_check.c");
println!("cargo:rerun-if-env-changed=UU_NO_GTK");
println!("cargo:rustc-check-cfg=cfg(uu_gtk)");
@@ -36,7 +36,7 @@ fn main() {
match (std::env::var_os("UU_NO_GTK").is_none()).then(|| gtk("--cflags").zip(gtk("--libs"))).flatten() {
Some((cflags, libs)) => {
let mut build = cc::Build::new();
build.file("c_engine/login_window.c").flag_if_supported("-Wno-deprecated-declarations");
build.file("c_engine/assist_window.c").flag_if_supported("-Wno-deprecated-declarations");
for flag in cflags.split_whitespace() { build.flag(flag); }
build.compile("uu_login_window");
for flag in libs.split_whitespace() {
@@ -72,7 +72,7 @@ fn main() {
let mut build = cc::Build::new();
build.flag_if_supported("-Wno-unused-parameter"); // protocol listener signatures
build.file("c_engine/wayland_engine.c").include(&out);
for name in ["wlr-screencopy-unstable-v1", "wlr-virtual-pointer-unstable-v1", "virtual-keyboard-unstable-v1", "wlr-output-power-management-unstable-v1"] {
for name in ["wlr-screencopy-unstable-v1", "wlr-virtual-pointer-unstable-v1", "virtual-keyboard-unstable-v1", "wlr-output-power-management-unstable-v1", "linux-dmabuf-unstable-v1"] {
let xml = format!("c_engine/wayland-protocols/{name}.xml");
println!("cargo:rerun-if-changed={xml}");
for (mode, suffix) in [("client-header", "client-protocol.h"), ("private-code", "protocol.c")] {
@@ -90,4 +90,3 @@ fn main() {
}
}
}
+644
View File
@@ -0,0 +1,644 @@
// GTK3 window for guest remote assistance (`uuremote-server assist`). Layout follows the official
// "本设备" card (allow switch, device ID, temporary code with show/hide, copy & share) without any
// NetEase assets. GTK runs on its own thread; Rust pushes state through g_idle_add and polls the
// user's commands. The code is only drawn in this local window and copied on explicit request.
#include <gtk/gtk.h>
#include <pthread.h>
#include <stdatomic.h>
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>
static const char *CSS =
"window { background-color: #f5f6f8; }\n"
"* { font-family: 'Noto Sans CJK SC', 'Noto Sans SC', sans-serif; }\n"
".card { background-color: #ffffff; border-radius: 10px; border: 1px solid #e5e7eb; }\n"
".heading { font-size: 20px; font-weight: 700; color: #1d1d1f; }\n"
".key { font-size: 14px; color: #4b5563; }\n"
".big { font-size: 32px; font-weight: 800; color: #111111; }\n"
".note { font-size: 13px; color: #9ca3af; }\n"
".hint { font-size: 13px; color: #6b7280; }\n"
".footer { font-size: 12px; color: #9ca3af; }\n"
"button.flat { padding: 4px 8px; }\n"
"button.share { background-image: none; background-color: #ffffff; border: 1px solid #d1d5db;"
" border-radius: 6px; padding: 6px 14px; font-size: 14px; }\n"
"separator { background-color: #e5e7eb; min-height: 1px; }\n"
".user { font-size: 22px; font-weight: 700; color: #111111; }\n"
".status { font-size: 13px; color: #2f6bff; }\n"
".status.ok { color: #16a34a; }\n"
".status.error { color: #dc2626; }\n";
enum { CMD_NONE = 0, CMD_ALLOW = 1, CMD_DENY = 2, CMD_RESET = 3, CMD_STOP = 4, CMD_VERIFY = 5, CMD_REFRESH = 6, CMD_CUSTOM = 7, CMD_LOGOUT = 8, CMD_QUIT = 9 };
enum { VERIFY_TEMPORARY = 0, VERIFY_CUSTOM = 1, VERIFY_BOTH = 2 };
typedef struct { char *id; char *code; int allowed, verify, refresh, has_custom; } State;
typedef struct { int cmd; char arg[40]; } Cmd;
// Small FIFO so quick successive UI actions (e.g. set code, then mode) are all delivered.
static Cmd queue[16];
static int q_head, q_len;
static pthread_mutex_t q_lock = PTHREAD_MUTEX_INITIALIZER;
static atomic_int closed;
static pthread_t ui_thread;
static int ui_started, init_state; // 0 pending, 1 ok, -1 failed
static pthread_mutex_t init_lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t init_cond = PTHREAD_COND_INITIALIZER;
static GtkWidget *window, *allow_switch, *id_label, *code_label, *eye_button, *share_button, *hint_label, *verify_combo, *note_label;
static GtkWidget *refresh_items[5], *footer_label;
// Account card: QR login before sign-in, user info (+ logout) after.
static GtkWidget *assist_card, *account_card, *account_stack, *qr_stack, *qr_image, *login_status, *user_label;
static int show_assist = 1, show_account;
// Tray (host service only): AppIndicator via dlopen (no build dependency), else GtkStatusIcon.
static GtkWidget *tray_menu, *tray_allow;
static int suppress_tray;
static int cur_verify, cur_refresh, cur_has_custom;
static char current_id[64], current_code[64];
static int code_visible = 1, suppress_switch, start_hidden, close_hides;
static GtkWidget *label(const char *text, const char *cls) {
GtkWidget *l = gtk_label_new(text);
gtk_style_context_add_class(gtk_widget_get_style_context(l), cls);
gtk_widget_set_halign(l, GTK_ALIGN_START);
return l;
}
static void post_arg(int cmd, const char *arg) {
pthread_mutex_lock(&q_lock);
if (q_len < 16) {
Cmd *c = &queue[(q_head + q_len) % 16];
c->cmd = cmd; g_strlcpy(c->arg, arg ? arg : "", sizeof c->arg); q_len++;
}
pthread_mutex_unlock(&q_lock);
}
static void post(int cmd) { post_arg(cmd, NULL); }
static void post_int(int cmd, int v) { char b[8]; g_snprintf(b, sizeof b, "%d", v); post_arg(cmd, b); }
// 8-16 ASCII letters/digits including both, as the official custom code dialog requires.
static int valid_custom(const char *c) {
size_t n = strlen(c); int alpha = 0, digit = 0;
if (n < 8 || n > 16) return 0;
for (size_t i = 0; i < n; i++) {
if (g_ascii_isalpha(c[i])) alpha = 1; else if (g_ascii_isdigit(c[i])) digit = 1; else return 0;
}
return alpha && digit;
}
static void on_custom_changed(GtkEditable *e, gpointer ok) {
gtk_widget_set_sensitive(GTK_WIDGET(ok), valid_custom(gtk_entry_get_text(GTK_ENTRY(e))));
}
// Returns 1 when a new custom code was entered and posted.
static int ask_custom_code(void) {
GtkWidget *d = gtk_dialog_new_with_buttons("设置自定义验证码", GTK_WINDOW(window), GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
"取消", GTK_RESPONSE_CANCEL, NULL);
GtkWidget *ok = gtk_dialog_add_button(GTK_DIALOG(d), "确定", GTK_RESPONSE_OK);
gtk_widget_set_sensitive(ok, FALSE);
GtkWidget *area = gtk_dialog_get_content_area(GTK_DIALOG(d));
gtk_container_set_border_width(GTK_CONTAINER(area), 16);
gtk_box_set_spacing(GTK_BOX(area), 8);
GtkWidget *entry = gtk_entry_new();
gtk_entry_set_max_length(GTK_ENTRY(entry), 16);
gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
gtk_entry_set_placeholder_text(GTK_ENTRY(entry), "请输入自定义验证码");
g_signal_connect(entry, "changed", G_CALLBACK(on_custom_changed), ok);
gtk_box_pack_start(GTK_BOX(area), entry, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(area), label("8-16 位,需同时包含字母和数字;不会自动刷新,请妥善保管", "note"), FALSE, FALSE, 0);
gtk_widget_show_all(d);
int done = 0;
if (gtk_dialog_run(GTK_DIALOG(d)) == GTK_RESPONSE_OK) {
const char *code = gtk_entry_get_text(GTK_ENTRY(entry));
if (valid_custom(code)) { post_arg(CMD_CUSTOM, code); done = 1; }
}
gtk_widget_destroy(d);
return done;
}
static int suppress_combo;
static void on_verify_changed(GtkComboBox *c, gpointer d) {
(void)d;
if (suppress_combo) return;
int v = gtk_combo_box_get_active(c);
if (v != VERIFY_TEMPORARY && !cur_has_custom && !ask_custom_code()) {
suppress_combo = 1; gtk_combo_box_set_active(c, cur_verify); suppress_combo = 0;
return;
}
post_int(CMD_VERIFY, v);
}
static void on_set_custom(GtkButton *b, gpointer d) { (void)b; (void)d; ask_custom_code(); }
static int suppress_refresh;
static void on_refresh_item(GtkCheckMenuItem *item, gpointer data) {
if (suppress_refresh || !gtk_check_menu_item_get_active(item)) return;
post_int(CMD_REFRESH, GPOINTER_TO_INT(data));
}
static void on_refresh_now(GtkMenuItem *i, gpointer d) { (void)i; (void)d; post(CMD_RESET); }
static const char *REFRESH_NOTES[5] = { "每次远控结束刷新", "每小时自动刷新", "每 12 小时自动刷新", "每 24 小时自动刷新", "不自动刷新" };
static void render_code(void) {
if (!code_label) return;
gtk_label_set_text(GTK_LABEL(code_label), current_code[0] ? (code_visible ? current_code : "********") : "--------");
gtk_button_set_label(GTK_BUTTON(eye_button), code_visible ? "隐藏" : "显示");
}
static void copy_text(const char *text, const char *hint) {
if (!text || !text[0]) return;
gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD), text, -1);
gtk_label_set_text(GTK_LABEL(hint_label), hint);
}
// The ID is copied without the display spaces so it pastes straight into the viewer's ID field.
static void on_copy_id(GtkButton *b, gpointer d) {
(void)b; (void)d;
char digits[64]; size_t n = 0;
for (const char *p = current_id; *p && n + 1 < sizeof digits; p++) if (*p != ' ') digits[n++] = *p;
digits[n] = 0;
copy_text(digits, "已复制设备 ID");
}
static void on_copy_code(GtkButton *b, gpointer d) { (void)b; (void)d; copy_text(current_code, "已复制验证码,请只发给信任的人"); }
static void on_eye(GtkButton *b, gpointer d) { (void)b; (void)d; code_visible = !code_visible; render_code(); }
static void on_share(GtkButton *b, gpointer d) {
(void)b; (void)d;
if (!current_id[0] || !current_code[0]) return;
char *text = g_strdup_printf("UU 远程协助\n设备 ID:%s\n验证码:%s", current_id, current_code);
gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD), text, -1);
g_free(text);
gtk_label_set_text(GTK_LABEL(hint_label), "已复制设备 ID 和验证码,请只发给信任的人");
}
static gboolean on_switch(GtkSwitch *s, gboolean state, gpointer d) {
(void)s; (void)d;
if (!suppress_switch) post(state ? CMD_ALLOW : CMD_DENY);
return FALSE;
}
// Host service: the close button only hides the window (assistance keeps its own on/off switch).
static gboolean on_delete(GtkWidget *w, GdkEvent *e, gpointer d) {
(void)e; (void)d;
if (!close_hides) return FALSE;
gtk_widget_hide(w);
return TRUE;
}
static void on_destroy(GtkWidget *w, gpointer d) {
(void)w; (void)d;
window = code_label = NULL;
atomic_store(&closed, 1);
post(CMD_STOP);
gtk_main_quit();
}
static gboolean draw_avatar(GtkWidget *w, cairo_t *cr, gpointer d) {
(void)w; (void)d;
double s = 64;
cairo_arc(cr, s / 2, s / 2, s / 2 - 1, 0, 2 * G_PI);
cairo_set_source_rgb(cr, 0.84, 0.90, 1.0); cairo_fill_preserve(cr);
cairo_set_source_rgb(cr, 0.36, 0.55, 1.0); cairo_set_line_width(cr, 2); cairo_stroke(cr);
cairo_save(cr);
cairo_arc(cr, s / 2, s / 2, s / 2 - 2, 0, 2 * G_PI); cairo_clip(cr);
cairo_set_source_rgb(cr, 0.27, 0.51, 1.0);
cairo_arc(cr, s / 2, s * 0.38, s * 0.16, 0, 2 * G_PI); cairo_fill(cr);
cairo_arc(cr, s / 2, s * 0.92, s * 0.32, G_PI, 2 * G_PI); cairo_fill(cr);
cairo_restore(cr);
return TRUE;
}
static void on_logout(GtkButton *b, gpointer d) {
(void)b; (void)d;
GtkWidget *dlg = gtk_message_dialog_new(GTK_WINDOW(window), GTK_DIALOG_MODAL, GTK_MESSAGE_QUESTION, GTK_BUTTONS_OK_CANCEL,
"退出登录后,本机将从你的设备列表中下线,远程协助会改用访客身份。确定退出吗?");
if (gtk_dialog_run(GTK_DIALOG(dlg)) == GTK_RESPONSE_OK) {
post(CMD_LOGOUT);
gtk_label_set_text(GTK_LABEL(user_label), "正在退出登录…");
}
gtk_widget_destroy(dlg);
}
static GtkWidget *build_account_card(void) {
GtkWidget *card = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
gtk_style_context_add_class(gtk_widget_get_style_context(card), "card");
GtkWidget *header = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 10);
gtk_container_set_border_width(GTK_CONTAINER(header), 20);
gtk_box_pack_start(GTK_BOX(header), label("账号", "heading"), FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(card), header, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(card), gtk_separator_new(GTK_ORIENTATION_HORIZONTAL), FALSE, FALSE, 0);
account_stack = gtk_stack_new();
gtk_stack_set_transition_type(GTK_STACK(account_stack), GTK_STACK_TRANSITION_TYPE_CROSSFADE);
gtk_stack_set_vhomogeneous(GTK_STACK(account_stack), FALSE); // user page is shorter than the QR page
// Not signed in: QR code + guidance.
GtkWidget *qr_page = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 24);
gtk_container_set_border_width(GTK_CONTAINER(qr_page), 20);
qr_stack = gtk_stack_new();
gtk_widget_set_size_request(qr_stack, 180, 180);
GtkWidget *loading = gtk_box_new(GTK_ORIENTATION_VERTICAL, 8);
gtk_widget_set_valign(loading, GTK_ALIGN_CENTER);
GtkWidget *spinner = gtk_spinner_new();
gtk_spinner_start(GTK_SPINNER(spinner));
gtk_box_pack_start(GTK_BOX(loading), spinner, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(loading), label("正在获取二维码…", "note"), FALSE, FALSE, 0);
gtk_stack_add_named(GTK_STACK(qr_stack), loading, "loading");
qr_image = gtk_image_new();
gtk_stack_add_named(GTK_STACK(qr_stack), qr_image, "qr");
gtk_box_pack_start(GTK_BOX(qr_page), qr_stack, FALSE, FALSE, 0);
GtkWidget *qr_text = gtk_box_new(GTK_ORIENTATION_VERTICAL, 8);
gtk_widget_set_valign(qr_text, GTK_ALIGN_CENTER);
gtk_box_pack_start(GTK_BOX(qr_text), label("扫码登录", "heading"), FALSE, FALSE, 0);
GtkWidget *cap = label("请使用网易 UU 远程 App 扫一扫登录。\n登录后本机会出现在你的设备列表中,可直接远程控制;\n远程协助在登录前后都可以使用。", "hint");
gtk_label_set_xalign(GTK_LABEL(cap), 0);
gtk_box_pack_start(GTK_BOX(qr_text), cap, FALSE, FALSE, 0);
login_status = label("", "status");
gtk_label_set_line_wrap(GTK_LABEL(login_status), TRUE);
gtk_box_pack_start(GTK_BOX(qr_text), login_status, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(qr_page), qr_text, TRUE, TRUE, 0);
gtk_stack_add_named(GTK_STACK(account_stack), qr_page, "qr");
// Signed in: avatar + account name + logout.
GtkWidget *user_page = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 16);
gtk_container_set_border_width(GTK_CONTAINER(user_page), 20);
GtkWidget *avatar = gtk_drawing_area_new();
gtk_widget_set_size_request(avatar, 64, 64);
gtk_widget_set_valign(avatar, GTK_ALIGN_CENTER);
g_signal_connect(avatar, "draw", G_CALLBACK(draw_avatar), NULL);
gtk_box_pack_start(GTK_BOX(user_page), avatar, FALSE, FALSE, 0);
GtkWidget *user_text = gtk_box_new(GTK_ORIENTATION_VERTICAL, 4);
gtk_widget_set_valign(user_text, GTK_ALIGN_CENTER);
user_label = label("", "user");
gtk_box_pack_start(GTK_BOX(user_text), user_label, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(user_text), label("已登录 · 本机已出现在你的设备列表中", "hint"), FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(user_page), user_text, TRUE, TRUE, 0);
GtkWidget *logout = gtk_button_new_with_label("退出登录");
gtk_style_context_add_class(gtk_widget_get_style_context(logout), "share");
gtk_widget_set_valign(logout, GTK_ALIGN_CENTER);
g_signal_connect(logout, "clicked", G_CALLBACK(on_logout), NULL);
gtk_box_pack_end(GTK_BOX(user_page), logout, FALSE, FALSE, 0);
gtk_stack_add_named(GTK_STACK(account_stack), user_page, "user");
gtk_box_pack_start(GTK_BOX(card), account_stack, FALSE, FALSE, 0);
return card;
}
static void present_window(void) { if (window) { gtk_widget_show(window); gtk_window_present(GTK_WINDOW(window)); } }
static void on_tray_open(GtkMenuItem *i, gpointer d) { (void)i; (void)d; present_window(); }
static void on_tray_allow(GtkCheckMenuItem *i, gpointer d) {
(void)d;
if (!suppress_tray) post(gtk_check_menu_item_get_active(i) ? CMD_ALLOW : CMD_DENY);
}
static void on_tray_quit(GtkMenuItem *i, gpointer d) {
(void)i; (void)d;
GtkWidget *dlg = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_QUESTION, GTK_BUTTONS_OK_CANCEL,
"退出后本机将下线:设备列表和远程协助都无法连接,直到再次启动。确定退出吗?");
gtk_window_set_title(GTK_WINDOW(dlg), "UU 远程 Linux 主机");
if (gtk_dialog_run(GTK_DIALOG(dlg)) == GTK_RESPONSE_OK) post(CMD_QUIT);
gtk_widget_destroy(dlg);
}
static void on_status_activate(GtkStatusIcon *i, gpointer d) { (void)i; (void)d; present_window(); }
static void on_status_menu(GtkStatusIcon *i, guint button, guint time, gpointer d) {
(void)i; (void)button; (void)time; (void)d;
gtk_menu_popup_at_pointer(GTK_MENU(tray_menu), NULL);
}
static void build_tray(void) {
tray_menu = gtk_menu_new();
GtkWidget *open = gtk_menu_item_new_with_label("打开主窗口");
g_signal_connect(open, "activate", G_CALLBACK(on_tray_open), NULL);
tray_allow = gtk_check_menu_item_new_with_label("允许他人远程协助");
g_signal_connect(tray_allow, "toggled", G_CALLBACK(on_tray_allow), NULL);
GtkWidget *quit = gtk_menu_item_new_with_label("退出");
g_signal_connect(quit, "activate", G_CALLBACK(on_tray_quit), NULL);
gtk_menu_shell_append(GTK_MENU_SHELL(tray_menu), open);
gtk_menu_shell_append(GTK_MENU_SHELL(tray_menu), tray_allow);
gtk_menu_shell_append(GTK_MENU_SHELL(tray_menu), gtk_separator_menu_item_new());
gtk_menu_shell_append(GTK_MENU_SHELL(tray_menu), quit);
gtk_widget_show_all(tray_menu);
const char *icon = gtk_icon_theme_has_icon(gtk_icon_theme_get_default(), "uu-remote-linux") ? "uu-remote-linux" : "computer";
// StatusNotifierItem (GNOME AppIndicator extension, KDE): libayatana-appindicator3 / libappindicator3.
const char *libs[] = { "libayatana-appindicator3.so.1", "libappindicator3.so.1" };
for (int i = 0; i < 2; i++) {
void *h = dlopen(libs[i], RTLD_LAZY | RTLD_LOCAL);
if (!h) continue;
void *(*ind_new)(const char *, const char *, int) = (void *(*)(const char *, const char *, int))dlsym(h, "app_indicator_new");
void (*set_status)(void *, int) = (void (*)(void *, int))dlsym(h, "app_indicator_set_status");
void (*set_menu)(void *, GtkMenu *) = (void (*)(void *, GtkMenu *))dlsym(h, "app_indicator_set_menu");
void (*set_title)(void *, const char *) = (void (*)(void *, const char *))dlsym(h, "app_indicator_set_title");
if (ind_new && set_status && set_menu) {
void *ind = ind_new("uu-remote-linux", icon, 0 /* APPLICATION_STATUS */);
if (set_title) set_title(ind, "UU 远程 Linux 主机");
set_menu(ind, GTK_MENU(tray_menu));
set_status(ind, 1 /* ACTIVE */);
return; // keep the library loaded for the indicator's lifetime
}
dlclose(h);
}
// XEmbed system tray (Xfce, Cinnamon, MATE...).
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
GtkStatusIcon *status = gtk_status_icon_new_from_icon_name(icon);
gtk_status_icon_set_tooltip_text(status, "UU 远程 Linux 主机");
gtk_status_icon_set_title(status, "UU 远程 Linux 主机");
g_signal_connect(status, "activate", G_CALLBACK(on_status_activate), NULL);
g_signal_connect(status, "popup-menu", G_CALLBACK(on_status_menu), NULL);
G_GNUC_END_IGNORE_DEPRECATIONS
}
static void build_window(void) {
GtkCssProvider *css = gtk_css_provider_new();
gtk_css_provider_load_from_data(css, CSS, -1, NULL);
gtk_style_context_add_provider_for_screen(gdk_screen_get_default(), GTK_STYLE_PROVIDER(css),
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
g_object_unref(css);
gtk_window_set_default_icon_name("uu-remote-linux"); // contrib/desktop icon, when installed
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "UU 远程 Linux 主机(非官方)");
gtk_window_set_default_size(GTK_WINDOW(window), 760, -1);
gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
g_signal_connect(window, "delete-event", G_CALLBACK(on_delete), NULL);
g_signal_connect(window, "destroy", G_CALLBACK(on_destroy), NULL);
GtkWidget *root = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10);
gtk_container_set_border_width(GTK_CONTAINER(root), 24);
gtk_container_add(GTK_CONTAINER(window), root);
GtkWidget *card = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
gtk_style_context_add_class(gtk_widget_get_style_context(card), "card");
gtk_box_pack_start(GTK_BOX(root), card, FALSE, FALSE, 0);
assist_card = card;
// Header: 本设备 ... 允许他人远程协助 [switch]
GtkWidget *header = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 10);
gtk_container_set_border_width(GTK_CONTAINER(header), 20);
gtk_box_pack_start(GTK_BOX(header), label("本设备", "heading"), FALSE, FALSE, 0);
allow_switch = gtk_switch_new();
gtk_widget_set_valign(allow_switch, GTK_ALIGN_CENTER);
g_signal_connect(allow_switch, "state-set", G_CALLBACK(on_switch), NULL);
gtk_box_pack_end(GTK_BOX(header), allow_switch, FALSE, FALSE, 0);
gtk_box_pack_end(GTK_BOX(header), label("允许他人远程协助", "key"), FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(card), header, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(card), gtk_separator_new(GTK_ORIENTATION_HORIZONTAL), FALSE, FALSE, 0);
// Body: ID | code + controls
GtkWidget *body = gtk_grid_new();
gtk_container_set_border_width(GTK_CONTAINER(body), 20);
gtk_grid_set_column_spacing(GTK_GRID(body), 48);
gtk_grid_set_row_spacing(GTK_GRID(body), 6);
gtk_grid_attach(GTK_GRID(body), label("本设备 ID", "key"), 0, 0, 1, 1);
GtkWidget *id_row = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 8);
id_label = label("获取中…", "big");
gtk_label_set_selectable(GTK_LABEL(id_label), TRUE);
gtk_box_pack_start(GTK_BOX(id_row), id_label, FALSE, FALSE, 0);
GtkWidget *copy_id = gtk_button_new_with_label("复制");
gtk_style_context_add_class(gtk_widget_get_style_context(copy_id), "flat");
gtk_widget_set_valign(copy_id, GTK_ALIGN_CENTER);
g_signal_connect(copy_id, "clicked", G_CALLBACK(on_copy_id), NULL);
gtk_box_pack_start(GTK_BOX(id_row), copy_id, FALSE, FALSE, 0);
gtk_grid_attach(GTK_GRID(body), id_row, 0, 1, 1, 1);
GtkWidget *verify_row = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 6);
gtk_box_pack_start(GTK_BOX(verify_row), label("验证方式:", "key"), FALSE, FALSE, 0);
verify_combo = gtk_combo_box_text_new();
gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(verify_combo), "仅使用临时验证码");
gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(verify_combo), "仅使用自定义验证码");
gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(verify_combo), "同时使用两种验证码");
gtk_combo_box_set_active(GTK_COMBO_BOX(verify_combo), 0);
g_signal_connect(verify_combo, "changed", G_CALLBACK(on_verify_changed), NULL);
gtk_box_pack_start(GTK_BOX(verify_row), verify_combo, FALSE, FALSE, 0);
gtk_grid_attach(GTK_GRID(body), verify_row, 1, 0, 1, 1);
GtkWidget *code_row = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 8);
code_label = label("--------", "big");
gtk_box_pack_start(GTK_BOX(code_row), code_label, FALSE, FALSE, 0);
eye_button = gtk_button_new_with_label("隐藏");
gtk_style_context_add_class(gtk_widget_get_style_context(eye_button), "flat");
g_signal_connect(eye_button, "clicked", G_CALLBACK(on_eye), NULL);
// Gear menu: 立即刷新 / 自动刷新 ▸ (每次远控结束, 每小时, 每 12 小时, 每 24 小时, 关闭) / 设置自定义验证码
GtkWidget *menu = gtk_menu_new();
GtkWidget *now = gtk_menu_item_new_with_label("立即刷新");
g_signal_connect(now, "activate", G_CALLBACK(on_refresh_now), NULL);
gtk_menu_shell_append(GTK_MENU_SHELL(menu), now);
GtkWidget *auto_item = gtk_menu_item_new_with_label("自动刷新");
GtkWidget *sub = gtk_menu_new();
const char *names[5] = { "每次远控结束", "每小时", "每 12 小时", "每 24 小时", "关闭" };
GSList *group = NULL;
for (int i = 0; i < 5; i++) {
refresh_items[i] = gtk_radio_menu_item_new_with_label(group, names[i]);
group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(refresh_items[i]));
g_signal_connect(refresh_items[i], "toggled", G_CALLBACK(on_refresh_item), GINT_TO_POINTER(i));
gtk_menu_shell_append(GTK_MENU_SHELL(sub), refresh_items[i]);
}
gtk_menu_item_set_submenu(GTK_MENU_ITEM(auto_item), sub);
gtk_menu_shell_append(GTK_MENU_SHELL(menu), auto_item);
GtkWidget *custom_item = gtk_menu_item_new_with_label("设置自定义验证码…");
g_signal_connect(custom_item, "activate", G_CALLBACK(on_set_custom), NULL);
gtk_menu_shell_append(GTK_MENU_SHELL(menu), custom_item);
gtk_widget_show_all(menu);
GtkWidget *gear = gtk_menu_button_new();
gtk_button_set_image(GTK_BUTTON(gear), gtk_image_new_from_icon_name("emblem-system-symbolic", GTK_ICON_SIZE_BUTTON));
gtk_widget_set_tooltip_text(gear, "刷新设置");
gtk_menu_button_set_popup(GTK_MENU_BUTTON(gear), menu);
GtkWidget *copy_code = gtk_button_new_with_label("复制");
gtk_style_context_add_class(gtk_widget_get_style_context(copy_code), "flat");
gtk_widget_set_valign(copy_code, GTK_ALIGN_CENTER);
g_signal_connect(copy_code, "clicked", G_CALLBACK(on_copy_code), NULL);
gtk_widget_set_valign(eye_button, GTK_ALIGN_CENTER);
gtk_widget_set_valign(gear, GTK_ALIGN_CENTER);
gtk_box_pack_start(GTK_BOX(code_row), copy_code, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(code_row), eye_button, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(code_row), gear, FALSE, FALSE, 0);
gtk_grid_attach(GTK_GRID(body), code_row, 1, 1, 1, 1);
note_label = label(REFRESH_NOTES[0], "note");
gtk_grid_attach(GTK_GRID(body), note_label, 1, 2, 1, 1);
share_button = gtk_button_new_with_label("复制并分享");
gtk_style_context_add_class(gtk_widget_get_style_context(share_button), "share");
gtk_widget_set_valign(share_button, GTK_ALIGN_CENTER);
g_signal_connect(share_button, "clicked", G_CALLBACK(on_share), NULL);
gtk_grid_attach(GTK_GRID(body), share_button, 2, 1, 1, 1);
gtk_box_pack_start(GTK_BOX(card), body, FALSE, FALSE, 0);
hint_label = label("", "hint");
gtk_box_pack_start(GTK_BOX(root), hint_label, FALSE, FALSE, 0);
account_card = build_account_card();
gtk_widget_set_margin_top(account_card, 6);
gtk_box_pack_start(GTK_BOX(root), account_card, FALSE, FALSE, 0);
footer_label = label("开源 Linux 被控端 · 非网易官方客户端 · 关闭窗口即停止远程协助", "footer");
gtk_box_pack_end(GTK_BOX(root), footer_label, FALSE, FALSE, 0);
if (close_hides) gtk_label_set_text(GTK_LABEL(footer_label), "开源 Linux 被控端 · 非网易官方客户端 · 关闭窗口后远程协助仍在后台运行");
render_code();
if (close_hides) build_tray();
gtk_widget_show_all(gtk_bin_get_child(GTK_BIN(window)));
gtk_widget_set_visible(assist_card, show_assist);
gtk_widget_set_visible(hint_label, show_assist);
gtk_widget_set_visible(account_card, show_account);
if (!start_hidden) gtk_widget_show(window);
}
static void *ui_main(void *arg) {
(void)arg;
int ok = gtk_init_check(NULL, NULL);
if (ok) build_window();
pthread_mutex_lock(&init_lock);
init_state = ok ? 1 : -1;
pthread_cond_signal(&init_cond);
pthread_mutex_unlock(&init_lock);
if (ok) gtk_main();
else atomic_store(&closed, 1);
return NULL;
}
// Returns 0 when the window was created, -1 when no display is usable. hidden: create without
// showing (host service, opened later with uu_assist_ui_present); hide_on_close: closing hides.
static gboolean apply_sections(gpointer d) {
(void)d;
if (window) {
gtk_widget_set_visible(assist_card, show_assist); gtk_widget_set_visible(hint_label, show_assist);
gtk_widget_set_visible(account_card, show_account);
}
return G_SOURCE_REMOVE;
}
static int ui_start(int hidden, int hide_on_close);
int uu_assist_ui_start(int hidden, int hide_on_close) {
if (ui_started) { show_assist = 1; g_idle_add(apply_sections, NULL); return init_state == 1 ? 0 : -1; }
show_assist = 1;
return ui_start(hidden, hide_on_close);
}
// Reveals the account card on the shared window; standalone `login` opens it with the account card only.
int uu_login_ui_start(const char *name, const char *os, const char *cpu, const char *gpu) {
(void)name; (void)os; (void)cpu; (void)gpu;
show_account = 1;
if (ui_started) { g_idle_add(apply_sections, NULL); return init_state == 1 ? 0 : -1; }
show_assist = 0;
return ui_start(0, 0);
}
void uu_ui_show_account(void) { show_account = 1; if (ui_started && init_state == 1) g_idle_add(apply_sections, NULL); }
static int ui_start(int hidden, int hide_on_close) {
if (ui_started) return init_state == 1 ? 0 : -1;
start_hidden = hidden; close_hides = hide_on_close;
if (pthread_create(&ui_thread, NULL, ui_main, NULL) != 0) return -1;
ui_started = 1;
pthread_mutex_lock(&init_lock);
while (init_state == 0) pthread_cond_wait(&init_cond, &init_lock);
pthread_mutex_unlock(&init_lock);
if (init_state != 1) { pthread_join(ui_thread, NULL); return -1; }
return 0;
}
static gboolean apply_state(gpointer data) {
State *s = data;
if (window) {
g_strlcpy(current_id, s->id, sizeof current_id);
g_strlcpy(current_code, s->code, sizeof current_code);
gtk_label_set_text(GTK_LABEL(id_label), current_id[0] ? current_id : "获取中…");
render_code();
suppress_switch = 1;
gtk_switch_set_active(GTK_SWITCH(allow_switch), s->allowed);
suppress_switch = 0;
if (tray_allow) { suppress_tray = 1; gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(tray_allow), s->allowed); suppress_tray = 0; }
cur_verify = s->verify; cur_refresh = s->refresh; cur_has_custom = s->has_custom;
suppress_combo = 1; gtk_combo_box_set_active(GTK_COMBO_BOX(verify_combo), s->verify); suppress_combo = 0;
if (s->refresh >= 0 && s->refresh < 5) {
suppress_refresh = 1; gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(refresh_items[s->refresh]), TRUE); suppress_refresh = 0;
}
gtk_label_set_text(GTK_LABEL(note_label), s->verify == VERIFY_CUSTOM ? "自定义验证码不会自动刷新"
: s->verify == VERIFY_BOTH ? "也可使用自定义验证码连接" : REFRESH_NOTES[s->refresh >= 0 && s->refresh < 5 ? s->refresh : 0]);
gtk_widget_set_sensitive(share_button, current_id[0] && current_code[0]);
gtk_label_set_text(GTK_LABEL(hint_label), s->allowed
? "对方在 UU 远程「开始协助」中输入设备 ID 和验证码即可连接"
: "已关闭:他人暂时无法远程协助本机,打开右上角开关后才能连接");
}
g_free(s->id); g_free(s->code); g_free(s);
return G_SOURCE_REMOVE;
}
// id is already formatted for display (e.g. "123 456 789").
void uu_assist_ui_set(const char *id, const char *code, int allowed, int verify, int refresh, int has_custom) {
if (!ui_started || init_state != 1) return;
State *s = g_new0(State, 1);
s->id = g_strdup(id ? id : ""); s->code = g_strdup(code ? code : ""); s->allowed = allowed;
s->verify = verify; s->refresh = refresh; s->has_custom = has_custom;
g_idle_add(apply_state, s);
}
// Pops the next user command (see CMD_*), copying its argument into arg.
int uu_assist_ui_take_command(char *arg, size_t len) {
int cmd = CMD_NONE;
if (arg && len) arg[0] = 0;
pthread_mutex_lock(&q_lock);
if (q_len) {
Cmd *c = &queue[q_head];
cmd = c->cmd; if (arg && len) g_strlcpy(arg, c->arg, len);
memset(c->arg, 0, sizeof c->arg); // drop the custom code copy promptly
q_head = (q_head + 1) % 16; q_len--;
}
pthread_mutex_unlock(&q_lock);
return cmd;
}
int uu_assist_ui_closed(void) { return atomic_load(&closed); }
static gboolean apply_present(gpointer data) {
(void)data;
if (window) { gtk_widget_show(window); gtk_window_present(GTK_WINDOW(window)); }
return G_SOURCE_REMOVE;
}
void uu_assist_ui_present(void) { if (ui_started && init_state == 1 && !atomic_load(&closed)) g_idle_add(apply_present, NULL); }
static gboolean apply_close(gpointer data) { (void)data; if (window) gtk_widget_destroy(window); return G_SOURCE_REMOVE; }
void uu_assist_ui_close(void) { if (ui_started && init_state == 1 && !atomic_load(&closed)) g_idle_add(apply_close, NULL); }
// ---- Account card API (QR login / signed-in user); same names as the former login window ----
typedef struct { unsigned char *rgba; int edge; } QrUpdate;
typedef struct { char *text; int kind; } StatusUpdate;
static gboolean apply_qr(gpointer data) {
QrUpdate *u = data;
if (window) {
GdkPixbuf *pb = gdk_pixbuf_new_from_data(u->rgba, GDK_COLORSPACE_RGB, TRUE, 8, u->edge, u->edge, u->edge * 4, NULL, NULL);
GdkPixbuf *scaled = gdk_pixbuf_scale_simple(pb, 180, 180, GDK_INTERP_NEAREST);
gtk_image_set_from_pixbuf(GTK_IMAGE(qr_image), scaled);
g_object_unref(scaled); g_object_unref(pb);
gtk_stack_set_visible_child_name(GTK_STACK(qr_stack), "qr");
gtk_stack_set_visible_child_name(GTK_STACK(account_stack), "qr");
}
g_free(u->rgba); g_free(u);
return G_SOURCE_REMOVE;
}
void uu_login_ui_set_qr(const unsigned char *rgba, int edge) {
if (!ui_started || init_state != 1 || atomic_load(&closed) || edge <= 0) return;
QrUpdate *u = g_new(QrUpdate, 1);
u->rgba = g_memdup2(rgba, (gsize)edge * edge * 4); u->edge = edge;
g_idle_add(apply_qr, u);
}
static gboolean apply_login_status(gpointer data) {
StatusUpdate *u = data;
if (window) {
GtkStyleContext *ctx = gtk_widget_get_style_context(login_status);
gtk_style_context_remove_class(ctx, "ok"); gtk_style_context_remove_class(ctx, "error");
if (u->kind == 1) gtk_style_context_add_class(ctx, "ok");
if (u->kind == 2) gtk_style_context_add_class(ctx, "error");
gtk_label_set_text(GTK_LABEL(login_status), u->text);
}
g_free(u->text); g_free(u);
return G_SOURCE_REMOVE;
}
void uu_login_ui_set_status(const char *text, int kind) {
if (!ui_started || init_state != 1 || atomic_load(&closed)) return;
StatusUpdate *u = g_new(StatusUpdate, 1);
u->text = g_strdup(text ? text : ""); u->kind = kind;
g_idle_add(apply_login_status, u);
}
static gboolean apply_user(gpointer data) {
if (window) {
gtk_label_set_text(GTK_LABEL(user_label), data);
gtk_stack_set_visible_child_name(GTK_STACK(account_stack), "user");
show_account = 1; apply_sections(NULL);
}
g_free(data);
return G_SOURCE_REMOVE;
}
// Signed in: show the user page with this (already masked) account name.
void uu_ui_set_user(const char *name) {
if (!ui_started || init_state != 1) return;
g_idle_add(apply_user, g_strdup(name ? name : ""));
}
void uu_login_ui_set_done(const char *text) { uu_ui_set_user(text); }
int uu_login_ui_closed(void) { return atomic_load(&closed); }
void uu_login_ui_close(void) { uu_assist_ui_close(); }
-321
View File
@@ -1,321 +0,0 @@
// GTK3 QR login window for `uuremote-server login`. The window runs its own GTK main loop on a
// dedicated thread; the Rust login flow pushes QR images and status text through g_idle_add.
// Layout follows the official login window (QR on the left), but uses no NetEase assets: the
// mark is drawn with cairo and the right column shows this host's hardware summary instead of SMS.
#include <gtk/gtk.h>
#include <pthread.h>
#include <stdatomic.h>
#include <stdlib.h>
#include <string.h>
static const char *CSS =
"window { background-image: linear-gradient(165deg, #d6e8ff 0%, #eaf3ff 40%, #ffffff 100%); }\n"
"* { font-family: 'Noto Sans CJK SC', 'Noto Sans SC', sans-serif; }\n"
".title { font-size: 26px; font-weight: 800; color: #111111; }\n"
".subtitle { font-size: 13px; color: #6b7280; }\n"
".heading { font-size: 19px; font-weight: 700; color: #1d1d1f; }\n"
".card { background-color: #ffffff; border-radius: 10px; padding: 14px; }\n"
".caption { font-size: 14px; color: #555555; }\n"
".status { font-size: 13px; color: #2f6bff; }\n"
".status.ok { color: #16a34a; }\n"
".status.error { color: #dc2626; }\n"
".key { font-size: 13px; color: #8a8f98; }\n"
".value { font-size: 13px; color: #222222; }\n"
".note { font-size: 12px; color: #6b7280; }\n"
".done-mark { font-size: 72px; color: #16a34a; }\n"
".done-text { font-size: 17px; font-weight: 700; color: #1d1d1f; }\n"
".footer { font-size: 12px; color: #9ca3af; }\n"
"button.primary { background-image: none; background-color: #2f6bff; color: #ffffff; border: none;"
" border-radius: 6px; padding: 6px 28px; font-size: 15px; }\n"
"button.primary:hover { background-color: #1f5ae6; }\n"
"separator { background-color: #dfe5ee; min-width: 1px; }\n";
typedef struct { unsigned char *rgba; int edge; } QrUpdate;
typedef struct { char *text; int kind; } StatusUpdate;
static atomic_int closed;
static pthread_t ui_thread;
static int ui_started;
static pthread_mutex_t init_lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t init_cond = PTHREAD_COND_INITIALIZER;
static int init_state; // 0 pending, 1 ok, -1 failed
static char *info[4];
static GtkWidget *window, *stack, *qr_image, *status_label, *done_label, *done_button;
static gboolean draw_mark(GtkWidget *w, cairo_t *cr, gpointer data) {
(void)w; (void)data;
cairo_pattern_t *g = cairo_pattern_create_linear(0, 0, 44, 44);
cairo_pattern_add_color_stop_rgb(g, 0, 0.13, 0.83, 0.93);
cairo_pattern_add_color_stop_rgb(g, 1, 0.18, 0.42, 1.0);
double r = 11, s = 44;
cairo_new_sub_path(cr);
cairo_arc(cr, s - r, r, r, -G_PI / 2, 0);
cairo_arc(cr, s - r, s - r, r, 0, G_PI / 2);
cairo_arc(cr, r, s - r, r, G_PI / 2, G_PI);
cairo_arc(cr, r, r, r, G_PI, 3 * G_PI / 2);
cairo_close_path(cr);
cairo_set_source(cr, g);
cairo_fill(cr);
cairo_pattern_destroy(g);
cairo_set_source_rgb(cr, 1, 1, 1);
cairo_select_font_face(cr, "sans-serif", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
cairo_set_font_size(cr, 19);
cairo_text_extents_t e;
cairo_text_extents(cr, "UU", &e);
cairo_move_to(cr, (s - e.width) / 2 - e.x_bearing, (s - e.height) / 2 - e.y_bearing);
cairo_show_text(cr, "UU");
return TRUE;
}
static GtkWidget *label(const char *text, const char *cls) {
GtkWidget *l = gtk_label_new(text);
gtk_style_context_add_class(gtk_widget_get_style_context(l), cls);
return l;
}
static void info_row(GtkGrid *grid, int row, const char *key, const char *value) {
GtkWidget *k = label(key, "key"), *v = label(value && *value ? value : "-", "value");
gtk_widget_set_halign(k, GTK_ALIGN_START);
gtk_widget_set_valign(k, GTK_ALIGN_START);
gtk_widget_set_halign(v, GTK_ALIGN_START);
gtk_label_set_xalign(GTK_LABEL(v), 0);
gtk_label_set_line_wrap(GTK_LABEL(v), TRUE);
gtk_label_set_max_width_chars(GTK_LABEL(v), 26);
gtk_grid_attach(grid, k, 0, row, 1, 1);
gtk_grid_attach(grid, v, 1, row, 1, 1);
}
static void on_destroy(GtkWidget *w, gpointer data) {
(void)w; (void)data;
atomic_store(&closed, 1);
window = NULL;
gtk_main_quit();
}
static void on_done_clicked(GtkButton *b, gpointer data) {
(void)b; (void)data;
if (window) gtk_widget_destroy(window);
}
static void build_window(void) {
GtkCssProvider *css = gtk_css_provider_new();
gtk_css_provider_load_from_data(css, CSS, -1, NULL);
gtk_style_context_add_provider_for_screen(gdk_screen_get_default(), GTK_STYLE_PROVIDER(css),
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
g_object_unref(css);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "UU 远程 Linux 主机 - 登录");
gtk_window_set_default_size(GTK_WINDOW(window), 720, 520);
gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
g_signal_connect(window, "destroy", G_CALLBACK(on_destroy), NULL);
GtkWidget *root = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
gtk_container_set_border_width(GTK_CONTAINER(root), 24);
gtk_container_add(GTK_CONTAINER(window), root);
GtkWidget *header = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 12);
gtk_widget_set_halign(header, GTK_ALIGN_CENTER);
GtkWidget *mark = gtk_drawing_area_new();
gtk_widget_set_size_request(mark, 44, 44);
gtk_widget_set_valign(mark, GTK_ALIGN_CENTER);
g_signal_connect(mark, "draw", G_CALLBACK(draw_mark), NULL);
GtkWidget *titles = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
GtkWidget *title = label("UU 远程 · Linux 主机", "title");
gtk_widget_set_halign(title, GTK_ALIGN_START);
GtkWidget *subtitle = label("登录后,本机会出现在你的 UU 远程设备列表中", "subtitle");
gtk_widget_set_halign(subtitle, GTK_ALIGN_START);
gtk_box_pack_start(GTK_BOX(titles), title, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(titles), subtitle, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(header), mark, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(header), titles, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(root), header, FALSE, FALSE, 4);
GtkWidget *columns = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
gtk_box_set_homogeneous(GTK_BOX(columns), FALSE);
gtk_widget_set_margin_top(columns, 22);
gtk_box_pack_start(GTK_BOX(root), columns, TRUE, TRUE, 0);
// Left: QR login.
GtkWidget *left = gtk_box_new(GTK_ORIENTATION_VERTICAL, 12);
gtk_widget_set_size_request(left, 330, -1);
gtk_box_pack_start(GTK_BOX(left), label("扫码登录", "heading"), FALSE, FALSE, 0);
GtkWidget *card = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
gtk_style_context_add_class(gtk_widget_get_style_context(card), "card");
gtk_widget_set_halign(card, GTK_ALIGN_CENTER);
gtk_widget_set_size_request(card, 252, 252);
stack = gtk_stack_new();
gtk_stack_set_transition_type(GTK_STACK(stack), GTK_STACK_TRANSITION_TYPE_CROSSFADE);
GtkWidget *loading = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10);
gtk_widget_set_valign(loading, GTK_ALIGN_CENTER);
GtkWidget *spinner = gtk_spinner_new();
gtk_spinner_start(GTK_SPINNER(spinner));
gtk_widget_set_size_request(spinner, 32, 32);
gtk_box_pack_start(GTK_BOX(loading), spinner, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(loading), label("正在获取二维码…", "caption"), FALSE, FALSE, 0);
gtk_stack_add_named(GTK_STACK(stack), loading, "loading");
qr_image = gtk_image_new();
gtk_stack_add_named(GTK_STACK(stack), qr_image, "qr");
GtkWidget *done = gtk_box_new(GTK_ORIENTATION_VERTICAL, 4);
gtk_widget_set_valign(done, GTK_ALIGN_CENTER);
gtk_box_pack_start(GTK_BOX(done), label("✓", "done-mark"), FALSE, FALSE, 0);
done_label = label("登录成功", "done-text");
gtk_label_set_justify(GTK_LABEL(done_label), GTK_JUSTIFY_CENTER);
gtk_label_set_line_wrap(GTK_LABEL(done_label), TRUE);
gtk_box_pack_start(GTK_BOX(done), done_label, FALSE, FALSE, 0);
gtk_stack_add_named(GTK_STACK(stack), done, "done");
gtk_box_pack_start(GTK_BOX(card), stack, TRUE, TRUE, 0);
gtk_box_pack_start(GTK_BOX(left), card, FALSE, FALSE, 0);
GtkWidget *caption = label("请使用网易 UU 远程 App 扫一扫\n可快速登录", "caption");
gtk_label_set_justify(GTK_LABEL(caption), GTK_JUSTIFY_CENTER);
gtk_box_pack_start(GTK_BOX(left), caption, FALSE, FALSE, 0);
status_label = label("", "status");
gtk_label_set_line_wrap(GTK_LABEL(status_label), TRUE);
gtk_label_set_justify(GTK_LABEL(status_label), GTK_JUSTIFY_CENTER);
gtk_label_set_max_width_chars(GTK_LABEL(status_label), 34);
gtk_box_pack_start(GTK_BOX(left), status_label, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(columns), left, TRUE, TRUE, 0);
GtkWidget *sep = gtk_separator_new(GTK_ORIENTATION_VERTICAL);
gtk_widget_set_margin_top(sep, 6);
gtk_widget_set_margin_bottom(sep, 24);
gtk_box_pack_start(GTK_BOX(columns), sep, FALSE, FALSE, 0);
// Right: what will be registered.
GtkWidget *right = gtk_box_new(GTK_ORIENTATION_VERTICAL, 14);
gtk_widget_set_margin_start(right, 30);
gtk_widget_set_size_request(right, 300, -1);
GtkWidget *rh = label("本机信息", "heading");
gtk_box_pack_start(GTK_BOX(right), rh, FALSE, FALSE, 0);
GtkWidget *grid = gtk_grid_new();
gtk_grid_set_row_spacing(GTK_GRID(grid), 12);
gtk_grid_set_column_spacing(GTK_GRID(grid), 16);
gtk_widget_set_margin_top(grid, 10);
const char *keys[4] = {"设备名称", "操作系统", "处理器", "显卡"};
for (int i = 0; i < 4; i++) info_row(GTK_GRID(grid), i, keys[i], info[i]);
gtk_box_pack_start(GTK_BOX(right), grid, FALSE, FALSE, 0);
GtkWidget *note = label("登录后可在 Mac / Windows / 手机上的官方 UU 远程客户端中连接本机的桌面、文件传输和终端。"
"登录信息只保存在本机配置文件中。", "note");
gtk_label_set_line_wrap(GTK_LABEL(note), TRUE);
gtk_label_set_max_width_chars(GTK_LABEL(note), 30);
gtk_label_set_xalign(GTK_LABEL(note), 0);
gtk_widget_set_margin_top(note, 10);
gtk_box_pack_start(GTK_BOX(right), note, FALSE, FALSE, 0);
done_button = gtk_button_new_with_label("完成");
gtk_style_context_add_class(gtk_widget_get_style_context(done_button), "primary");
gtk_widget_set_halign(done_button, GTK_ALIGN_START);
gtk_widget_set_no_show_all(done_button, TRUE);
g_signal_connect(done_button, "clicked", G_CALLBACK(on_done_clicked), NULL);
gtk_box_pack_end(GTK_BOX(right), done_button, FALSE, FALSE, 20);
gtk_box_pack_start(GTK_BOX(columns), right, TRUE, TRUE, 0);
GtkWidget *footer = label("开源 Linux 主机端 · 非网易官方客户端", "footer");
gtk_box_pack_end(GTK_BOX(root), footer, FALSE, FALSE, 0);
gtk_widget_show_all(window);
}
static void *ui_main(void *arg) {
(void)arg;
int ok = gtk_init_check(NULL, NULL);
if (ok) build_window();
pthread_mutex_lock(&init_lock);
init_state = ok ? 1 : -1;
pthread_cond_signal(&init_cond);
pthread_mutex_unlock(&init_lock);
if (ok) gtk_main();
else atomic_store(&closed, 1);
return NULL;
}
// Returns 0 when the window is shown, -1 when no display is usable.
int uu_login_ui_start(const char *name, const char *os, const char *cpu, const char *gpu) {
if (ui_started) return init_state == 1 ? 0 : -1;
const char *src[4] = {name, os, cpu, gpu};
for (int i = 0; i < 4; i++) info[i] = g_strdup(src[i] ? src[i] : "");
if (pthread_create(&ui_thread, NULL, ui_main, NULL) != 0) return -1;
ui_started = 1;
pthread_mutex_lock(&init_lock);
while (init_state == 0) pthread_cond_wait(&init_cond, &init_lock);
pthread_mutex_unlock(&init_lock);
if (init_state != 1) { pthread_join(ui_thread, NULL); return -1; }
return 0;
}
static gboolean apply_qr(gpointer data) {
QrUpdate *u = data;
if (window) {
GdkPixbuf *pb = gdk_pixbuf_new_from_data(u->rgba, GDK_COLORSPACE_RGB, TRUE, 8, u->edge, u->edge, u->edge * 4,
(GdkPixbufDestroyNotify)g_free, NULL);
gtk_image_set_from_pixbuf(GTK_IMAGE(qr_image), pb);
g_object_unref(pb);
gtk_stack_set_visible_child_name(GTK_STACK(stack), "qr");
} else g_free(u->rgba);
g_free(u);
return G_SOURCE_REMOVE;
}
void uu_login_ui_set_qr(const unsigned char *rgba, int edge) {
if (atomic_load(&closed) || edge <= 0) return;
QrUpdate *u = g_new(QrUpdate, 1);
u->rgba = g_memdup2(rgba, (gsize)edge * edge * 4);
u->edge = edge;
g_idle_add(apply_qr, u);
}
static gboolean apply_status(gpointer data) {
StatusUpdate *u = data;
if (window) {
GtkStyleContext *ctx = gtk_widget_get_style_context(status_label);
gtk_style_context_remove_class(ctx, "ok");
gtk_style_context_remove_class(ctx, "error");
if (u->kind == 1) gtk_style_context_add_class(ctx, "ok");
if (u->kind == 2) gtk_style_context_add_class(ctx, "error");
gtk_label_set_text(GTK_LABEL(status_label), u->text);
if (u->kind == 2 && !gtk_image_get_pixbuf(GTK_IMAGE(qr_image)))
gtk_widget_hide(gtk_stack_get_child_by_name(GTK_STACK(stack), "loading"));
}
g_free(u->text);
g_free(u);
return G_SOURCE_REMOVE;
}
// kind: 0 info, 1 success, 2 error.
void uu_login_ui_set_status(const char *text, int kind) {
if (atomic_load(&closed)) return;
StatusUpdate *u = g_new(StatusUpdate, 1);
u->text = g_strdup(text ? text : "");
u->kind = kind;
g_idle_add(apply_status, u);
}
static gboolean apply_done(gpointer data) {
if (window) {
gtk_label_set_text(GTK_LABEL(done_label), data);
gtk_stack_set_visible_child_name(GTK_STACK(stack), "done");
gtk_widget_show(done_button);
gtk_widget_grab_focus(done_button);
}
g_free(data);
return G_SOURCE_REMOVE;
}
void uu_login_ui_set_done(const char *text) {
if (atomic_load(&closed)) return;
g_idle_add(apply_done, g_strdup(text ? text : ""));
}
int uu_login_ui_closed(void) { return atomic_load(&closed); }
static gboolean apply_close(gpointer data) {
(void)data;
if (window) gtk_widget_destroy(window);
return G_SOURCE_REMOVE;
}
void uu_login_ui_close(void) {
if (!ui_started) return;
if (!atomic_load(&closed)) g_idle_add(apply_close, NULL);
pthread_join(ui_thread, NULL);
ui_started = 0;
}
+12 -1
View File
@@ -51,6 +51,7 @@ struct uu_portal {
uint8_t *frame;
size_t frame_length;
uint64_t sequence;
uint64_t revision;
};
#include "portal_clipboard.inc"
@@ -182,6 +183,7 @@ static void copy_video_buffer(struct uu_portal *p, struct spa_buffer *spa) {
memset(p->frame, 0, p->frame_length);
for (size_t pixel = 0; pixel < p->frame_length / 4; pixel++) p->frame[pixel * 4 + 3] = 255;
p->sequence++;
p->revision++;
return;
}
struct spa_meta_region *crop = spa_buffer_find_meta_data(spa, SPA_META_VideoCrop, sizeof(*crop));
@@ -203,6 +205,7 @@ static void copy_video_buffer(struct uu_portal *p, struct spa_buffer *spa) {
}
}
p->sequence++;
p->revision++;
}
}
}
@@ -418,14 +421,22 @@ void uu_portal_reset_frame(struct uu_portal *p) {
if (!p) return;
pthread_mutex_lock(&p->lock);
p->sequence = 0;
p->revision++;
if (p->frame) memset(p->frame, 0, p->frame_length);
pthread_mutex_unlock(&p->lock);
}
int uu_portal_capture(struct uu_portal *p, uint8_t *frame, size_t length) {
return uu_portal_capture_since(p, frame, length, NULL);
}
int uu_portal_capture_since(struct uu_portal *p, uint8_t *frame, size_t length, uint64_t *revision) {
if (!p || !frame) return 11;
pthread_mutex_lock(&p->lock);
int result = atomic_load(&p->closed) ? 9 : p->error ? p->error : !p->sequence ? 10 : length != p->frame_length ? 11 : 0;
if (!result) memcpy(frame, p->frame, length);
if (!result) {
if (revision && *revision == p->revision) result = 12;
else memcpy(frame, p->frame, length);
if (revision) *revision = p->revision;
}
pthread_mutex_unlock(&p->lock); return result;
}
int uu_portal_alive(struct uu_portal *p) {
+1
View File
@@ -16,6 +16,7 @@ int uu_portal_clipboard_poll(struct uu_portal *portal, uint8_t *text, size_t cap
int uu_portal_alive(struct uu_portal *portal);
void uu_portal_reset_frame(struct uu_portal *portal);
int uu_portal_capture(struct uu_portal *portal, uint8_t *bgra, size_t length);
int uu_portal_capture_since(struct uu_portal *portal, uint8_t *bgra, size_t length, uint64_t *revision);
int uu_portal_move(struct uu_portal *portal, double x, double y, int relative);
int uu_portal_button(struct uu_portal *portal, int button, int pressed);
int uu_portal_key(struct uu_portal *portal, uint32_t keysym, int pressed);
@@ -0,0 +1,589 @@
<?xml version="1.0" encoding="UTF-8"?>
<protocol name="linux_dmabuf_unstable_v1">
<copyright>
Copyright © 2014, 2015 Collabora, Ltd.
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice (including the next
paragraph) shall be included in all copies or substantial portions of the
Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
</copyright>
<interface name="zwp_linux_dmabuf_v1" version="5">
<description summary="factory for creating dmabuf-based wl_buffers">
Following the interfaces from:
https://www.khronos.org/registry/egl/extensions/EXT/EGL_EXT_image_dma_buf_import.txt
https://www.khronos.org/registry/EGL/extensions/EXT/EGL_EXT_image_dma_buf_import_modifiers.txt
and the Linux DRM sub-system's AddFb2 ioctl.
This interface offers ways to create generic dmabuf-based wl_buffers.
Clients can use the get_surface_feedback request to get dmabuf feedback
for a particular surface. If the client wants to retrieve feedback not
tied to a surface, they can use the get_default_feedback request.
The following are required from clients:
- Clients must ensure that either all data in the dma-buf is
coherent for all subsequent read access or that coherency is
correctly handled by the underlying kernel-side dma-buf
implementation.
- Don't make any more attachments after sending the buffer to the
compositor. Making more attachments later increases the risk of
the compositor not being able to use (re-import) an existing
dmabuf-based wl_buffer.
The underlying graphics stack must ensure the following:
- The dmabuf file descriptors relayed to the server will stay valid
for the whole lifetime of the wl_buffer. This means the server may
at any time use those fds to import the dmabuf into any kernel
sub-system that might accept it.
However, when the underlying graphics stack fails to deliver the
promise, because of e.g. a device hot-unplug which raises internal
errors, after the wl_buffer has been successfully created the
compositor must not raise protocol errors to the client when dmabuf
import later fails.
To create a wl_buffer from one or more dmabufs, a client creates a
zwp_linux_dmabuf_params_v1 object with a zwp_linux_dmabuf_v1.create_params
request. All planes required by the intended format are added with
the 'add' request. Finally, a 'create' or 'create_immed' request is
issued, which has the following outcome depending on the import success.
The 'create' request,
- on success, triggers a 'created' event which provides the final
wl_buffer to the client.
- on failure, triggers a 'failed' event to convey that the server
cannot use the dmabufs received from the client.
For the 'create_immed' request,
- on success, the server immediately imports the added dmabufs to
create a wl_buffer. No event is sent from the server in this case.
- on failure, the server can choose to either:
- terminate the client by raising a fatal error.
- mark the wl_buffer as failed, and send a 'failed' event to the
client. If the client uses a failed wl_buffer as an argument to any
request, the behaviour is compositor implementation-defined.
For all DRM formats and unless specified in another protocol extension,
pre-multiplied alpha is used for pixel values.
Unless specified otherwise in another protocol extension, implicit
synchronization is used. In other words, compositors and clients must
wait and signal fences implicitly passed via the DMA-BUF's reservation
mechanism.
Disclaimer: This protocol extension has been marked stable. This copy is
no longer used and only retained for backwards compatibility. The
canonical version can be found in the stable/ directory.
</description>
<request name="destroy" type="destructor">
<description summary="unbind the factory">
Objects created through this interface, especially wl_buffers, will
remain valid.
</description>
</request>
<request name="create_params">
<description summary="create a temporary object for buffer parameters">
This temporary object is used to collect multiple dmabuf handles into
a single batch to create a wl_buffer. It can only be used once and
should be destroyed after a 'created' or 'failed' event has been
received.
</description>
<arg name="params_id" type="new_id" interface="zwp_linux_buffer_params_v1"
summary="the new temporary"/>
</request>
<event name="format">
<description summary="supported buffer format">
This event advertises one buffer format that the server supports.
All the supported formats are advertised once when the client
binds to this interface. A roundtrip after binding guarantees
that the client has received all supported formats.
For the definition of the format codes, see the
zwp_linux_buffer_params_v1::create request.
Starting version 4, the format event is deprecated and must not be
sent by compositors. Instead, use get_default_feedback or
get_surface_feedback.
</description>
<arg name="format" type="uint" summary="DRM_FORMAT code"/>
</event>
<event name="modifier" since="3">
<description summary="supported buffer format modifier">
This event advertises the formats that the server supports, along with
the modifiers supported for each format. All the supported modifiers
for all the supported formats are advertised once when the client
binds to this interface. A roundtrip after binding guarantees that
the client has received all supported format-modifier pairs.
For legacy support, DRM_FORMAT_MOD_INVALID (that is, modifier_hi ==
0x00ffffff and modifier_lo == 0xffffffff) is allowed in this event.
It indicates that the server can support the format with an implicit
modifier. When a plane has DRM_FORMAT_MOD_INVALID as its modifier, it
is as if no explicit modifier is specified. The effective modifier
will be derived from the dmabuf.
A compositor that sends valid modifiers and DRM_FORMAT_MOD_INVALID for
a given format supports both explicit modifiers and implicit modifiers.
For the definition of the format and modifier codes, see the
zwp_linux_buffer_params_v1::create and zwp_linux_buffer_params_v1::add
requests.
Starting version 4, the modifier event is deprecated and must not be
sent by compositors. Instead, use get_default_feedback or
get_surface_feedback.
</description>
<arg name="format" type="uint" summary="DRM_FORMAT code"/>
<arg name="modifier_hi" type="uint"
summary="high 32 bits of layout modifier"/>
<arg name="modifier_lo" type="uint"
summary="low 32 bits of layout modifier"/>
</event>
<!-- Version 4 additions -->
<request name="get_default_feedback" since="4">
<description summary="get default feedback">
This request creates a new wp_linux_dmabuf_feedback object not bound
to a particular surface. This object will deliver feedback about dmabuf
parameters to use if the client doesn't support per-surface feedback
(see get_surface_feedback).
</description>
<arg name="id" type="new_id" interface="zwp_linux_dmabuf_feedback_v1"/>
</request>
<request name="get_surface_feedback" since="4">
<description summary="get feedback for a surface">
This request creates a new wp_linux_dmabuf_feedback object for the
specified wl_surface. This object will deliver feedback about dmabuf
parameters to use for buffers attached to this surface.
If the surface is destroyed before the wp_linux_dmabuf_feedback object,
the feedback object becomes inert.
</description>
<arg name="id" type="new_id" interface="zwp_linux_dmabuf_feedback_v1"/>
<arg name="surface" type="object" interface="wl_surface"/>
</request>
</interface>
<interface name="zwp_linux_buffer_params_v1" version="5">
<description summary="parameters for creating a dmabuf-based wl_buffer">
This temporary object is a collection of dmabufs and other
parameters that together form a single logical buffer. The temporary
object may eventually create one wl_buffer unless cancelled by
destroying it before requesting 'create'.
Single-planar formats only require one dmabuf, however
multi-planar formats may require more than one dmabuf. For all
formats, an 'add' request must be called once per plane (even if the
underlying dmabuf fd is identical).
You must use consecutive plane indices ('plane_idx' argument for 'add')
from zero to the number of planes used by the drm_fourcc format code.
All planes required by the format must be given exactly once, but can
be given in any order. Each plane index can be set only once.
</description>
<enum name="error">
<entry name="already_used" value="0"
summary="the dmabuf_batch object has already been used to create a wl_buffer"/>
<entry name="plane_idx" value="1"
summary="plane index out of bounds"/>
<entry name="plane_set" value="2"
summary="the plane index was already set"/>
<entry name="incomplete" value="3"
summary="missing or too many planes to create a buffer"/>
<entry name="invalid_format" value="4"
summary="format not supported"/>
<entry name="invalid_dimensions" value="5"
summary="invalid width or height"/>
<entry name="out_of_bounds" value="6"
summary="offset + stride * height goes out of dmabuf bounds"/>
<entry name="invalid_wl_buffer" value="7"
summary="invalid wl_buffer resulted from importing dmabufs via
the create_immed request on given buffer_params"/>
</enum>
<request name="destroy" type="destructor">
<description summary="delete this object, used or not">
Cleans up the temporary data sent to the server for dmabuf-based
wl_buffer creation.
</description>
</request>
<request name="add">
<description summary="add a dmabuf to the temporary set">
This request adds one dmabuf to the set in this
zwp_linux_buffer_params_v1.
The 64-bit unsigned value combined from modifier_hi and modifier_lo
is the dmabuf layout modifier. DRM AddFB2 ioctl calls this the
fb modifier, which is defined in drm_mode.h of Linux UAPI.
This is an opaque token. Drivers use this token to express tiling,
compression, etc. driver-specific modifications to the base format
defined by the DRM fourcc code.
Starting from version 4, the invalid_format protocol error is sent if
the format + modifier pair was not advertised as supported.
Starting from version 5, the invalid_format protocol error is sent if
all planes don't use the same modifier.
This request raises the PLANE_IDX error if plane_idx is too large.
The error PLANE_SET is raised if attempting to set a plane that
was already set.
</description>
<arg name="fd" type="fd" summary="dmabuf fd"/>
<arg name="plane_idx" type="uint" summary="plane index"/>
<arg name="offset" type="uint" summary="offset in bytes"/>
<arg name="stride" type="uint" summary="stride in bytes"/>
<arg name="modifier_hi" type="uint"
summary="high 32 bits of layout modifier"/>
<arg name="modifier_lo" type="uint"
summary="low 32 bits of layout modifier"/>
</request>
<enum name="flags" bitfield="true">
<entry name="y_invert" value="1" summary="contents are y-inverted"/>
<entry name="interlaced" value="2" summary="content is interlaced"/>
<entry name="bottom_first" value="4" summary="bottom field first"/>
</enum>
<request name="create">
<description summary="create a wl_buffer from the given dmabufs">
This asks for creation of a wl_buffer from the added dmabuf
buffers. The wl_buffer is not created immediately but returned via
the 'created' event if the dmabuf sharing succeeds. The sharing
may fail at runtime for reasons a client cannot predict, in
which case the 'failed' event is triggered.
The 'format' argument is a DRM_FORMAT code, as defined by the
libdrm's drm_fourcc.h. The Linux kernel's DRM sub-system is the
authoritative source on how the format codes should work.
The 'flags' is a bitfield of the flags defined in enum "flags".
'y_invert' means the that the image needs to be y-flipped.
Flag 'interlaced' means that the frame in the buffer is not
progressive as usual, but interlaced. An interlaced buffer as
supported here must always contain both top and bottom fields.
The top field always begins on the first pixel row. The temporal
ordering between the two fields is top field first, unless
'bottom_first' is specified. It is undefined whether 'bottom_first'
is ignored if 'interlaced' is not set.
This protocol does not convey any information about field rate,
duration, or timing, other than the relative ordering between the
two fields in one buffer. A compositor may have to estimate the
intended field rate from the incoming buffer rate. It is undefined
whether the time of receiving wl_surface.commit with a new buffer
attached, applying the wl_surface state, wl_surface.frame callback
trigger, presentation, or any other point in the compositor cycle
is used to measure the frame or field times. There is no support
for detecting missed or late frames/fields/buffers either, and
there is no support whatsoever for cooperating with interlaced
compositor output.
The composited image quality resulting from the use of interlaced
buffers is explicitly undefined. A compositor may use elaborate
hardware features or software to deinterlace and create progressive
output frames from a sequence of interlaced input buffers, or it
may produce substandard image quality. However, compositors that
cannot guarantee reasonable image quality in all cases are recommended
to just reject all interlaced buffers.
Any argument errors, including non-positive width or height,
mismatch between the number of planes and the format, bad
format, bad offset or stride, may be indicated by fatal protocol
errors: INCOMPLETE, INVALID_FORMAT, INVALID_DIMENSIONS,
OUT_OF_BOUNDS.
Dmabuf import errors in the server that are not obvious client
bugs are returned via the 'failed' event as non-fatal. This
allows attempting dmabuf sharing and falling back in the client
if it fails.
This request can be sent only once in the object's lifetime, after
which the only legal request is destroy. This object should be
destroyed after issuing a 'create' request. Attempting to use this
object after issuing 'create' raises ALREADY_USED protocol error.
It is not mandatory to issue 'create'. If a client wants to
cancel the buffer creation, it can just destroy this object.
</description>
<arg name="width" type="int" summary="base plane width in pixels"/>
<arg name="height" type="int" summary="base plane height in pixels"/>
<arg name="format" type="uint" summary="DRM_FORMAT code"/>
<arg name="flags" type="uint" enum="flags" summary="see enum flags"/>
</request>
<event name="created">
<description summary="buffer creation succeeded">
This event indicates that the attempted buffer creation was
successful. It provides the new wl_buffer referencing the dmabuf(s).
Upon receiving this event, the client should destroy the
zwp_linux_buffer_params_v1 object.
</description>
<arg name="buffer" type="new_id" interface="wl_buffer"
summary="the newly created wl_buffer"/>
</event>
<event name="failed">
<description summary="buffer creation failed">
This event indicates that the attempted buffer creation has
failed. It usually means that one of the dmabuf constraints
has not been fulfilled.
Upon receiving this event, the client should destroy the
zwp_linux_buffer_params_v1 object.
</description>
</event>
<request name="create_immed" since="2">
<description summary="immediately create a wl_buffer from the given
dmabufs">
This asks for immediate creation of a wl_buffer by importing the
added dmabufs.
In case of import success, no event is sent from the server, and the
wl_buffer is ready to be used by the client.
Upon import failure, either of the following may happen, as seen fit
by the implementation:
- the client is terminated with one of the following fatal protocol
errors:
- INCOMPLETE, INVALID_FORMAT, INVALID_DIMENSIONS, OUT_OF_BOUNDS,
in case of argument errors such as mismatch between the number
of planes and the format, bad format, non-positive width or
height, or bad offset or stride.
- INVALID_WL_BUFFER, in case the cause for failure is unknown or
platform specific.
- the server creates an invalid wl_buffer, marks it as failed and
sends a 'failed' event to the client. The result of using this
invalid wl_buffer as an argument in any request by the client is
defined by the compositor implementation.
This takes the same arguments as a 'create' request, and obeys the
same restrictions.
</description>
<arg name="buffer_id" type="new_id" interface="wl_buffer"
summary="id for the newly created wl_buffer"/>
<arg name="width" type="int" summary="base plane width in pixels"/>
<arg name="height" type="int" summary="base plane height in pixels"/>
<arg name="format" type="uint" summary="DRM_FORMAT code"/>
<arg name="flags" type="uint" enum="flags" summary="see enum flags"/>
</request>
</interface>
<interface name="zwp_linux_dmabuf_feedback_v1" version="5">
<description summary="dmabuf feedback">
This object advertises dmabuf parameters feedback. This includes the
preferred devices and the supported formats/modifiers.
The parameters are sent once when this object is created and whenever they
change. The done event is always sent once after all parameters have been
sent. When a single parameter changes, all parameters are re-sent by the
compositor.
Compositors can re-send the parameters when the current client buffer
allocations are sub-optimal. Compositors should not re-send the
parameters if re-allocating the buffers would not result in a more optimal
configuration. In particular, compositors should avoid sending the exact
same parameters multiple times in a row.
The tranche_target_device and tranche_formats events are grouped by
tranches of preference. For each tranche, a tranche_target_device, one
tranche_flags and one or more tranche_formats events are sent, followed
by a tranche_done event finishing the list. The tranches are sent in
descending order of preference. All formats and modifiers in the same
tranche have the same preference.
To send parameters, the compositor sends one main_device event, tranches
(each consisting of one tranche_target_device event, one tranche_flags
event, tranche_formats events and then a tranche_done event), then one
done event.
</description>
<request name="destroy" type="destructor">
<description summary="destroy the feedback object">
Using this request a client can tell the server that it is not going to
use the wp_linux_dmabuf_feedback object anymore.
</description>
</request>
<event name="done">
<description summary="all feedback has been sent">
This event is sent after all parameters of a wp_linux_dmabuf_feedback
object have been sent.
This allows changes to the wp_linux_dmabuf_feedback parameters to be
seen as atomic, even if they happen via multiple events.
</description>
</event>
<event name="format_table">
<description summary="format and modifier table">
This event provides a file descriptor which can be memory-mapped to
access the format and modifier table.
The table contains a tightly packed array of consecutive format +
modifier pairs. Each pair is 16 bytes wide. It contains a format as a
32-bit unsigned integer, followed by 4 bytes of unused padding, and a
modifier as a 64-bit unsigned integer. The native endianness is used.
The client must map the file descriptor in read-only private mode.
Compositors are not allowed to mutate the table file contents once this
event has been sent. Instead, compositors must create a new, separate
table file and re-send feedback parameters. Compositors are allowed to
store duplicate format + modifier pairs in the table.
</description>
<arg name="fd" type="fd" summary="table file descriptor"/>
<arg name="size" type="uint" summary="table size, in bytes"/>
</event>
<event name="main_device">
<description summary="preferred main device">
This event advertises the main device that the server prefers to use
when direct scan-out to the target device isn't possible. The
advertised main device may be different for each
wp_linux_dmabuf_feedback object, and may change over time.
There is exactly one main device. The compositor must send at least
one preference tranche with tranche_target_device equal to main_device.
Clients need to create buffers that the main device can import and
read from, otherwise creating the dmabuf wl_buffer will fail (see the
wp_linux_buffer_params.create and create_immed requests for details).
The main device will also likely be kept active by the compositor,
so clients can use it instead of waking up another device for power
savings.
In general the device is a DRM node. The DRM node type (primary vs.
render) is unspecified. Clients must not rely on the compositor sending
a particular node type. Clients cannot check two devices for equality
by comparing the dev_t value.
If explicit modifiers are not supported and the client performs buffer
allocations on a different device than the main device, then the client
must force the buffer to have a linear layout.
</description>
<arg name="device" type="array" summary="device dev_t value"/>
</event>
<event name="tranche_done">
<description summary="a preference tranche has been sent">
This event splits tranche_target_device and tranche_formats events in
preference tranches. It is sent after a set of tranche_target_device
and tranche_formats events; it represents the end of a tranche. The
next tranche will have a lower preference.
</description>
</event>
<event name="tranche_target_device">
<description summary="target device">
This event advertises the target device that the server prefers to use
for a buffer created given this tranche. The advertised target device
may be different for each preference tranche, and may change over time.
There is exactly one target device per tranche.
The target device may be a scan-out device, for example if the
compositor prefers to directly scan-out a buffer created given this
tranche. The target device may be a rendering device, for example if
the compositor prefers to texture from said buffer.
The client can use this hint to allocate the buffer in a way that makes
it accessible from the target device, ideally directly. The buffer must
still be accessible from the main device, either through direct import
or through a potentially more expensive fallback path. If the buffer
can't be directly imported from the main device then clients must be
prepared for the compositor changing the tranche priority or making
wl_buffer creation fail (see the wp_linux_buffer_params.create and
create_immed requests for details).
If the device is a DRM node, the DRM node type (primary vs. render) is
unspecified. Clients must not rely on the compositor sending a
particular node type. Clients cannot check two devices for equality by
comparing the dev_t value.
This event is tied to a preference tranche, see the tranche_done event.
</description>
<arg name="device" type="array" summary="device dev_t value"/>
</event>
<event name="tranche_formats">
<description summary="supported buffer format modifier">
This event advertises the format + modifier combinations that the
compositor supports.
It carries an array of indices, each referring to a format + modifier
pair in the last received format table (see the format_table event).
Each index is a 16-bit unsigned integer in native endianness.
For legacy support, DRM_FORMAT_MOD_INVALID is an allowed modifier.
It indicates that the server can support the format with an implicit
modifier. When a buffer has DRM_FORMAT_MOD_INVALID as its modifier, it
is as if no explicit modifier is specified. The effective modifier
will be derived from the dmabuf.
A compositor that sends valid modifiers and DRM_FORMAT_MOD_INVALID for
a given format supports both explicit modifiers and implicit modifiers.
Compositors must not send duplicate format + modifier pairs within the
same tranche or across two different tranches with the same target
device and flags.
This event is tied to a preference tranche, see the tranche_done event.
For the definition of the format and modifier codes, see the
wp_linux_buffer_params.create request.
</description>
<arg name="indices" type="array" summary="array of 16-bit indexes"/>
</event>
<enum name="tranche_flags" bitfield="true">
<entry name="scanout" value="1" summary="direct scan-out tranche"/>
</enum>
<event name="tranche_flags">
<description summary="tranche flags">
This event sets tranche-specific flags.
The scanout flag is a hint that direct scan-out may be attempted by the
compositor on the target device if the client appropriately allocates a
buffer. How to allocate a buffer that can be scanned out on the target
device is implementation-defined.
This event is tied to a preference tranche, see the tranche_done event.
</description>
<arg name="flags" type="uint" enum="tranche_flags" summary="tranche flags"/>
</event>
</interface>
</protocol>
+304 -42
View File
@@ -11,6 +11,9 @@
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <dlfcn.h>
#include "linux-dmabuf-unstable-v1-client-protocol.h"
#include "wlr-screencopy-unstable-v1-client-protocol.h"
#include "wlr-virtual-pointer-unstable-v1-client-protocol.h"
#include "virtual-keyboard-unstable-v1-client-protocol.h"
@@ -18,9 +21,32 @@
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
static struct wl_display *display;
static struct wl_registry *registry;
static struct wl_shm *shm;
static struct wl_seat *seat;
static struct zwlr_screencopy_manager_v1 *copy_manager;
static struct zwp_linux_dmabuf_v1 *dmabuf;
static int dmabuf_requested,dmabuf_disabled,dmabuf_reported;
static unsigned dmabuf_linear_formats;
/* GBM is loaded only for explicit opt-in; default builds need no GBM headers. */
struct gbm_device;
struct gbm_bo;
static struct {
void *library;
int fd;
struct gbm_device *device;
struct gbm_device *(*create_device)(int);
void (*device_destroy)(struct gbm_device*);
struct gbm_bo *(*bo_create)(struct gbm_device*,uint32_t,uint32_t,uint32_t,uint32_t);
void (*bo_destroy)(struct gbm_bo*);
int (*bo_get_fd)(struct gbm_bo*);
int (*bo_get_plane_count)(struct gbm_bo*);
uint32_t (*bo_get_stride)(struct gbm_bo*);
uint32_t (*bo_get_offset)(struct gbm_bo*,int);
uint64_t (*bo_get_modifier)(struct gbm_bo*);
void *(*bo_map)(struct gbm_bo*,uint32_t,uint32_t,uint32_t,uint32_t,uint32_t,uint32_t*,void**);
void (*bo_unmap)(struct gbm_bo*,void*);
} gbm={.fd=-1};
static struct zwlr_virtual_pointer_manager_v1 *pointer_manager;
static struct zwp_virtual_keyboard_manager_v1 *keyboard_manager;
static struct zwlr_virtual_pointer_v1 *pointer;
@@ -35,11 +61,28 @@ static unsigned char held_keys[768];
struct output { struct wl_output *handle; char name[128]; int transform,scale; uint32_t width,height; };
static struct output outputs[32];
static unsigned output_count, selected;
static uint32_t width,height,stride,format,flags;
static int done,failed,initialized;
static struct wl_buffer *buffer;
static void *pixels;
static size_t pixel_length;
static uint32_t width,height;
static int initialized;
/* Independent slots let the compositor fill the next frame during delivery. */
struct frame {
struct zwlr_screencopy_frame_v1 *handle;
struct wl_buffer *buffer,*shm_buffer;
struct wl_shm_pool *shm_pool;
void *pixels,*shm_pixels;
size_t shm_length;
int shm_fd,shm_busy;
uint32_t shm_width,shm_height,shm_stride,shm_format;
uint32_t width,height,stride,format,flags;
int done,failed,submitted,released;
struct zwp_linux_buffer_params_v1 *params;
struct gbm_bo *bo;
int bo_fd,dmabuf_used;
void *map_data;
uint32_t dma_format,dma_width,dma_height;
uint32_t bo_format,bo_width,bo_height;
};
static struct frame frames[2]={{.shm_fd=-1,.bo_fd=-1},{.shm_fd=-1,.bo_fd=-1}};
static unsigned pending;
static uint32_t timestamp(void) {
struct timespec t; clock_gettime(CLOCK_MONOTONIC,&t);
@@ -82,6 +125,20 @@ static void output_scale(void*d,struct wl_output*o,int32_t scale) { if(scale>0 &
static void output_name(void*d,struct wl_output*o,const char*name) { snprintf(((struct output*)d)->name,128,"%s",name); }
static void output_description(void*d,struct wl_output*o,const char*desc) {}
static const struct wl_output_listener output_listener={output_geometry,output_mode,output_done,output_scale,output_name,output_description};
static unsigned dma_format_bit(uint32_t format) {
switch(format) {
case 0x34325258: return 1; /* DRM_FORMAT_XRGB8888 */
case 0x34325241: return 2; /* DRM_FORMAT_ARGB8888 */
case 0x34324258: return 4; /* DRM_FORMAT_XBGR8888 */
case 0x34324241: return 8; /* DRM_FORMAT_ABGR8888 */
default: return 0;
}
}
static void dma_format(void*d,struct zwp_linux_dmabuf_v1*b,uint32_t fmt) {}
static void dma_modifier(void*d,struct zwp_linux_dmabuf_v1*b,uint32_t fmt,uint32_t hi,uint32_t lo) {
if(!hi && !lo) dmabuf_linear_formats|=dma_format_bit(fmt);
}
static const struct zwp_linux_dmabuf_v1_listener dma_listener={dma_format,dma_modifier};
static void global(void*d,struct wl_registry*r,uint32_t id,const char*interface,uint32_t version) {
if (!strcmp(interface,"wl_shm")) shm=wl_registry_bind(r,id,&wl_shm_interface,1);
else if (!strcmp(interface,"wl_seat") && !seat) seat=wl_registry_bind(r,id,&wl_seat_interface,1);
@@ -90,6 +147,9 @@ static void global(void*d,struct wl_registry*r,uint32_t id,const char*interface,
o->scale=1;
o->handle=wl_registry_bind(r,id,&wl_output_interface,version<4?version:4);
wl_output_add_listener(o->handle,&output_listener,o);
} else if (!strcmp(interface,"zwp_linux_dmabuf_v1") && dmabuf_requested && version>=3) {
dmabuf=wl_registry_bind(r,id,&zwp_linux_dmabuf_v1_interface,3);
zwp_linux_dmabuf_v1_add_listener(dmabuf,&dma_listener,NULL);
} else if (!strcmp(interface,"zwlr_screencopy_manager_v1")) copy_manager=wl_registry_bind(r,id,&zwlr_screencopy_manager_v1_interface,version<3?version:3);
else if (!strcmp(interface,"zwlr_virtual_pointer_manager_v1") && version>=2) pointer_manager=wl_registry_bind(r,id,&zwlr_virtual_pointer_manager_v1_interface,2);
else if (!strcmp(interface,"zwp_virtual_keyboard_manager_v1")) keyboard_manager=wl_registry_bind(r,id,&zwp_virtual_keyboard_manager_v1_interface,1);
@@ -100,63 +160,214 @@ static const struct wl_registry_listener registry_listener={global,global_remove
static void power_mode(void*d,struct zwlr_output_power_v1*p,uint32_t mode) {}
static void power_failure(void*d,struct zwlr_output_power_v1*p) { power_failed=1; }
static const struct zwlr_output_power_v1_listener power_listener={power_mode,power_failure};
static void dma_disable(const char *reason) {
/* reason is always a fixed diagnostic tag, never driver/user text. */
if(!dmabuf_disabled) fprintf(stderr,"Wayland dma-buf unavailable; falling back to SHM (%s)\n",reason);
dmabuf_disabled=1;
}
static void dma_init(void) {
if(!dmabuf_requested) return;
if(!dmabuf) {dma_disable("protocol"); return;}
if(!dmabuf_linear_formats) {dma_disable("linear-format"); return;}
gbm.library=dlopen("libgbm.so.1",RTLD_NOW|RTLD_LOCAL);
if(!gbm.library) {dma_disable("gbm-library"); return;}
#define GBM_LOAD(field,name) do { *(void**)(&gbm.field)=dlsym(gbm.library,name); if(!gbm.field) {dma_disable("gbm-symbol"); return;} } while(0)
GBM_LOAD(create_device,"gbm_create_device"); GBM_LOAD(device_destroy,"gbm_device_destroy");
GBM_LOAD(bo_create,"gbm_bo_create"); GBM_LOAD(bo_destroy,"gbm_bo_destroy");
GBM_LOAD(bo_get_fd,"gbm_bo_get_fd"); GBM_LOAD(bo_get_plane_count,"gbm_bo_get_plane_count");
GBM_LOAD(bo_get_stride,"gbm_bo_get_stride"); GBM_LOAD(bo_get_offset,"gbm_bo_get_offset");
GBM_LOAD(bo_get_modifier,"gbm_bo_get_modifier"); GBM_LOAD(bo_map,"gbm_bo_map"); GBM_LOAD(bo_unmap,"gbm_bo_unmap");
#undef GBM_LOAD
const char *device=getenv("UU_WAYLAND_DMABUF_DEVICE");
gbm.fd=open(device && *device ? device:"/dev/dri/renderD128",O_RDWR|O_CLOEXEC);
if(gbm.fd<0 || !(gbm.device=gbm.create_device(gbm.fd))) dma_disable("render-device");
}
static void shm_clear(struct frame *frame) {
if(frame->shm_buffer) wl_buffer_destroy(frame->shm_buffer);
if(frame->shm_pool) wl_shm_pool_destroy(frame->shm_pool);
if(frame->shm_pixels) munmap(frame->shm_pixels,frame->shm_length);
if(frame->shm_fd>=0) close(frame->shm_fd);
frame->shm_buffer=NULL; frame->shm_pool=NULL; frame->shm_pixels=NULL;
frame->shm_length=0; frame->shm_fd=-1; frame->shm_busy=0;
frame->shm_width=frame->shm_height=frame->shm_stride=frame->shm_format=0;
}
static void dma_clear(struct frame *frame) {
if(frame->buffer) wl_buffer_destroy(frame->buffer);
if(frame->dmabuf_used && frame->pixels) {
gbm.bo_unmap(frame->bo,frame->map_data); frame->pixels=NULL; frame->map_data=NULL;
}
if(frame->bo) { if(frame->bo_fd>=0)close(frame->bo_fd); gbm.bo_destroy(frame->bo); }
frame->buffer=NULL; frame->bo=NULL; frame->bo_fd=-1; frame->dmabuf_used=frame->released=0;
frame->bo_format=frame->bo_width=frame->bo_height=0;
}
static void frame_clear(struct frame *frame) {
if(frame->handle) zwlr_screencopy_frame_v1_destroy(frame->handle);
if(frame->params) zwp_linux_buffer_params_v1_destroy(frame->params);
if(frame->dmabuf_used && frame->pixels) gbm.bo_unmap(frame->bo,frame->map_data);
if(frame->shm_busy) shm_clear(frame);
frame->handle=NULL; frame->params=NULL;
frame->pixels=NULL; frame->map_data=NULL; frame->dmabuf_used=0;
frame->width=frame->height=frame->stride=frame->format=frame->flags=0;
frame->done=frame->failed=frame->submitted=0;
frame->dma_format=frame->dma_width=frame->dma_height=0;
}
static void buffer_release(void*d,struct wl_buffer*b) { ((struct frame*)d)->released=1; }
static const struct wl_buffer_listener buffer_listener={buffer_release};
static void frame_fail(struct frame *frame) { frame->failed=frame->done=frame->submitted=1; }
static void copy_buffer(void*d,struct zwlr_screencopy_frame_v1*f,uint32_t fmt,uint32_t w,uint32_t h,uint32_t s) {
format=fmt; width=w; height=h; stride=s;
struct frame *frame=d;
frame->format=fmt; frame->width=w; frame->height=h; frame->stride=s;
if (w==0 || h==0 || w>16384 || h>16384 || s<w*4 || s>131072 || (uint64_t)s*h>256*1024*1024 ||
(fmt!=WL_SHM_FORMAT_XRGB8888 && fmt!=WL_SHM_FORMAT_ARGB8888 &&
fmt!=WL_SHM_FORMAT_XBGR8888 && fmt!=WL_SHM_FORMAT_ABGR8888)) { failed=done=1; return; }
fmt!=WL_SHM_FORMAT_XBGR8888 && fmt!=WL_SHM_FORMAT_ABGR8888)) { frame_fail(frame); return; }
size_t length=(size_t)s*h;
if (buffer) { wl_buffer_destroy(buffer); buffer=NULL; }
if (pixels) { munmap(pixels,pixel_length); pixels=NULL; }
int fd=memfd_create("uuremote-wayland-frame",MFD_CLOEXEC);
if (fd<0 || ftruncate(fd,(off_t)length)!=0) { if(fd>=0)close(fd); failed=done=1; return; }
pixels=mmap(NULL,length,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
if (pixels==MAP_FAILED) { pixels=NULL; close(fd); failed=done=1; return; }
pixel_length=length;
struct wl_shm_pool *pool=wl_shm_create_pool(shm,fd,(int32_t)length);
buffer=wl_shm_pool_create_buffer(pool,0,w,h,s,fmt);
wl_shm_pool_destroy(pool); close(fd);
if (frame->shm_busy || !frame->shm_buffer || !frame->shm_pool || !frame->shm_pixels || frame->shm_fd<0 ||
frame->shm_length!=length || frame->shm_width!=w || frame->shm_height!=h ||
frame->shm_stride!=s || frame->shm_format!=fmt) {
shm_clear(frame);
int fd=memfd_create("uuremote-wayland-frame",MFD_CLOEXEC);
if (fd<0 || ftruncate(fd,(off_t)length)!=0) { if(fd>=0)close(fd); frame_fail(frame); return; }
void *pixels=mmap(NULL,length,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
if (pixels==MAP_FAILED) { close(fd); frame_fail(frame); return; }
struct wl_shm_pool *pool=wl_shm_create_pool(shm,fd,(int32_t)length);
if (!pool) { munmap(pixels,length); close(fd); frame_fail(frame); return; }
struct wl_buffer *buffer=wl_shm_pool_create_buffer(pool,0,w,h,s,fmt);
if (!buffer) { wl_shm_pool_destroy(pool); munmap(pixels,length); close(fd); frame_fail(frame); return; }
frame->shm_fd=fd; frame->shm_pixels=pixels; frame->shm_length=length;
frame->shm_pool=pool; frame->shm_buffer=buffer;
frame->shm_width=w; frame->shm_height=h; frame->shm_stride=s; frame->shm_format=fmt;
}
frame->pixels=frame->shm_pixels;
/* copy is valid immediately for SHM, including frame versions <3. */
if (zwlr_screencopy_frame_v1_get_version(f)<3) zwlr_screencopy_frame_v1_copy(f,buffer);
if (zwlr_screencopy_frame_v1_get_version(f)<3) {
zwlr_screencopy_frame_v1_copy(f,frame->shm_buffer); frame->shm_busy=frame->submitted=1;
}
}
static void copy_flags(void*d,struct zwlr_screencopy_frame_v1*f,uint32_t value) { flags=value; }
static void copy_ready(void*d,struct zwlr_screencopy_frame_v1*f,uint32_t hi,uint32_t lo,uint32_t ns) { done=1; }
static void copy_failed(void*d,struct zwlr_screencopy_frame_v1*f) { failed=done=1; }
static void copy_flags(void*d,struct zwlr_screencopy_frame_v1*f,uint32_t value) { ((struct frame*)d)->flags=value; }
static void copy_ready(void*d,struct zwlr_screencopy_frame_v1*f,uint32_t hi,uint32_t lo,uint32_t ns) {
struct frame *frame=d;
frame->shm_busy=0;
if(frame->dmabuf_used) {
/* GBM READ mapping synchronizes readback, possibly via a staging buffer. */
uint32_t stride=0;
frame->pixels=gbm.bo_map(frame->bo,0,0,frame->width,frame->height,1,&stride,&frame->map_data);
if(frame->pixels==MAP_FAILED) frame->pixels=NULL;
if(!frame->pixels || stride<frame->width*4 || stride>131072 || (uint64_t)stride*frame->height>256*1024*1024) {
dma_disable("cpu-map"); frame_fail(frame); return;
}
frame->stride=stride;
if(!dmabuf_reported) {fprintf(stderr,"Wayland dma-buf CPU mapping active (not zero-copy)\n"); dmabuf_reported=1;}
}
frame->done=1;
}
static void copy_failed(void*d,struct zwlr_screencopy_frame_v1*f) { ((struct frame*)d)->shm_busy=0; frame_fail(d); }
static void copy_damage(void*d,struct zwlr_screencopy_frame_v1*f,uint32_t x,uint32_t y,uint32_t w,uint32_t h) {}
static void copy_dmabuf(void*d,struct zwlr_screencopy_frame_v1*f,uint32_t fmt,uint32_t w,uint32_t h) {}
static void copy_buffer_done(void*d,struct zwlr_screencopy_frame_v1*f) { if(buffer && !failed) zwlr_screencopy_frame_v1_copy(f,buffer); else failed=done=1; }
static void copy_dmabuf(void*d,struct zwlr_screencopy_frame_v1*f,uint32_t fmt,uint32_t w,uint32_t h) {
struct frame *frame=d;
if(w && h && w<=16384 && h<=16384 && (uint64_t)w*h*4<=256*1024*1024 && dma_format_bit(fmt)) {
frame->dma_format=fmt; frame->dma_width=w; frame->dma_height=h;
}
}
static void submit_shm(struct frame *frame) {
if(frame->shm_buffer && !frame->shm_busy && !frame->failed) {
zwlr_screencopy_frame_v1_copy(frame->handle,frame->shm_buffer); frame->shm_busy=frame->submitted=1;
} else frame_fail(frame);
}
static void dma_created(void*d,struct zwp_linux_buffer_params_v1*params,struct wl_buffer*buffer) {
struct frame *frame=d;
zwp_linux_buffer_params_v1_destroy(params); frame->params=NULL;
frame->pixels=NULL;
frame->buffer=buffer; frame->dmabuf_used=1;
frame->width=frame->dma_width; frame->height=frame->dma_height;
frame->format=frame->dma_format==0x34325258 ? WL_SHM_FORMAT_XRGB8888:
frame->dma_format==0x34325241 ? WL_SHM_FORMAT_ARGB8888:frame->dma_format;
wl_buffer_add_listener(buffer,&buffer_listener,frame);
frame->released=0;
zwlr_screencopy_frame_v1_copy(frame->handle,buffer); frame->submitted=1;
}
static void dma_failed(void*d,struct zwp_linux_buffer_params_v1*params) {
struct frame *frame=d;
zwp_linux_buffer_params_v1_destroy(params); frame->params=NULL;
dma_disable("import"); dma_clear(frame); submit_shm(frame);
}
static const struct zwp_linux_buffer_params_v1_listener dma_params_listener={dma_created,dma_failed};
static int dma_start(struct frame *frame) {
if(!dmabuf_requested || dmabuf_disabled || !gbm.device || frame->failed || !frame->shm_buffer ||
!(dmabuf_linear_formats&dma_format_bit(frame->dma_format))) return 0;
if(frame->buffer && frame->bo && frame->released && frame->bo_width==frame->dma_width &&
frame->bo_height==frame->dma_height && frame->bo_format==frame->dma_format) {
frame->pixels=NULL; frame->dmabuf_used=1; frame->released=0;
frame->width=frame->dma_width; frame->height=frame->dma_height;
frame->format=frame->dma_format==0x34325258 ? WL_SHM_FORMAT_XRGB8888:
frame->dma_format==0x34325241 ? WL_SHM_FORMAT_ARGB8888:frame->dma_format;
zwlr_screencopy_frame_v1_copy(frame->handle,frame->buffer); frame->submitted=1; return 1;
}
dma_clear(frame);
/* Single-plane linear RGB only; unsupported imports are asynchronous failures,
never create_immed protocol errors. No privilege or device probing changes. */
frame->bo=gbm.bo_create(gbm.device,frame->dma_width,frame->dma_height,frame->dma_format,(1u<<2)|(1u<<4));
frame->bo_fd=-1;
if(!frame->bo) {dma_disable("allocation"); return 0;}
uint32_t stride=gbm.bo_get_stride(frame->bo);
if(gbm.bo_get_plane_count(frame->bo)!=1 || gbm.bo_get_modifier(frame->bo)!=0 ||
stride<frame->dma_width*4 || stride>131072 || (uint64_t)stride*frame->dma_height>256*1024*1024 ||
(frame->bo_fd=gbm.bo_get_fd(frame->bo))<0) {dma_disable("layout-or-export"); dma_clear(frame); return 0;}
frame->bo_format=frame->dma_format; frame->bo_width=frame->dma_width; frame->bo_height=frame->dma_height;
frame->params=zwp_linux_dmabuf_v1_create_params(dmabuf);
zwp_linux_buffer_params_v1_add_listener(frame->params,&dma_params_listener,frame);
zwp_linux_buffer_params_v1_add(frame->params,frame->bo_fd,0,gbm.bo_get_offset(frame->bo,0),stride,0,0);
zwp_linux_buffer_params_v1_create(frame->params,frame->dma_width,frame->dma_height,frame->dma_format,0);
return 1;
}
static void copy_buffer_done(void*d,struct zwlr_screencopy_frame_v1*f) {
struct frame *frame=d;
if(!dma_start(frame)) submit_shm(frame);
}
static const struct zwlr_screencopy_frame_v1_listener copy_listener={copy_buffer,copy_flags,copy_ready,copy_failed,copy_damage,copy_dmabuf,copy_buffer_done};
static int capture(void) {
done=failed=0; flags=0;
if(buffer) { wl_buffer_destroy(buffer); buffer=NULL; }
static void request_frame(struct frame *frame) {
/* Initial milestone: video excludes cursor, viewer draws its own pointer. */
struct zwlr_screencopy_frame_v1 *f=zwlr_screencopy_manager_v1_capture_output(copy_manager,0,outputs[selected].handle);
zwlr_screencopy_frame_v1_add_listener(f,&copy_listener,NULL);
int result=dispatch_until(&done);
zwlr_screencopy_frame_v1_destroy(f);
return result==0 && !failed && pixels ? 0:-1;
frame->handle=zwlr_screencopy_manager_v1_capture_output(copy_manager,0,outputs[selected].handle);
zwlr_screencopy_frame_v1_add_listener(frame->handle,&copy_listener,frame);
}
static void reset(void) {
if (key_state) xkb_state_unref(key_state);
if (keymap) xkb_keymap_unref(keymap);
if (xkb) xkb_context_unref(xkb);
if (pixels) munmap(pixels,pixel_length);
/* Disconnect drops every proxy; failure never leaves an initialized backend. */
for(unsigned i=0;i<2;i++) { frame_clear(&frames[i]); dma_clear(&frames[i]); shm_clear(&frames[i]); }
if(gbm.device) gbm.device_destroy(gbm.device);
if(gbm.fd>=0) close(gbm.fd);
if(gbm.library) dlclose(gbm.library);
memset(&gbm,0,sizeof(gbm)); gbm.fd=-1;
if(keyboard) zwp_virtual_keyboard_v1_destroy(keyboard);
if(pointer) zwlr_virtual_pointer_v1_destroy(pointer);
if(power) zwlr_output_power_v1_destroy(power);
if(keyboard_manager) zwp_virtual_keyboard_manager_v1_destroy(keyboard_manager);
if(pointer_manager) zwlr_virtual_pointer_manager_v1_destroy(pointer_manager);
if(power_manager) zwlr_output_power_manager_v1_destroy(power_manager);
if(copy_manager) zwlr_screencopy_manager_v1_destroy(copy_manager);
if(dmabuf) zwp_linux_dmabuf_v1_destroy(dmabuf);
for(unsigned i=0;i<output_count;i++) if(outputs[i].handle) wl_output_destroy(outputs[i].handle);
if(seat) wl_seat_destroy(seat);
if(shm) wl_shm_destroy(shm);
if(registry) wl_registry_destroy(registry);
if (display) wl_display_disconnect(display);
display=NULL; shm=NULL; seat=NULL; copy_manager=NULL; pointer_manager=NULL; keyboard_manager=NULL;
pointer=NULL; keyboard=NULL; xkb=NULL; keymap=NULL; key_state=NULL; buffer=NULL; pixels=NULL;
display=NULL; registry=NULL; shm=NULL; seat=NULL; copy_manager=NULL; pointer_manager=NULL; keyboard_manager=NULL;
pointer=NULL; keyboard=NULL; xkb=NULL; keymap=NULL; key_state=NULL;
power_manager=NULL; power=NULL; power_failed=0;
pixel_length=0; output_count=0; initialized=0; memset(outputs,0,sizeof(outputs));
dmabuf=NULL; dmabuf_requested=dmabuf_disabled=dmabuf_reported=0; dmabuf_linear_formats=0;
pending=0; output_count=0; initialized=0; memset(outputs,0,sizeof(outputs));
memset(held_keys,0,sizeof(held_keys));
}
int uu_wl_init(int32_t *w,int32_t *h) {
pthread_mutex_lock(&lock);
if(initialized) { *w=width; *h=height; pthread_mutex_unlock(&lock); return 0; }
int result=-1;
const char *dma=getenv("UU_WAYLAND_DMABUF"); dmabuf_requested=dma && !strcmp(dma,"1");
display=wl_display_connect(NULL); if (!display) goto end;
struct wl_registry *registry=wl_display_get_registry(display);
registry=wl_display_get_registry(display);
wl_registry_add_listener(registry,&registry_listener,NULL);
if(sync_display()!=0 || sync_display()!=0) goto end;
dma_init();
if(!shm || !seat || !copy_manager || !pointer_manager || !keyboard_manager || !output_count) goto end;
const char *name=getenv("UU_WAYLAND_OUTPUT"); selected=0;
if(name && *name) {
@@ -195,6 +406,8 @@ end:
}
int uu_wl_wake(void) {
pthread_mutex_lock(&lock); int result=-1;
/* Output power management is optional; missing it must not block screencopy. */
if(initialized && !power) result=0;
if(initialized && power && !power_failed) {
zwlr_output_power_v1_set_mode(power,ZWLR_OUTPUT_POWER_V1_MODE_ON);
if(sync_display()==0 && !power_failed) result=0;
@@ -203,18 +416,42 @@ int uu_wl_wake(void) {
}
int uu_wl_capture(uint8_t *out,size_t length) {
pthread_mutex_lock(&lock);
int result=-1;
if(initialized && capture()==0 && length==(size_t)width*height*4) {
int result=-1,retried=0;
retry:;
struct frame *frame=&frames[pending];
if(!initialized || length!=(size_t)width*height*4) goto end;
if(!frame->handle) request_frame(frame);
if(dispatch_until(&frame->done)!=0) goto end;
if(frame->failed && frame->dmabuf_used && !retried) {
/* Mapping/copy failure retries once on SHM; connection errors do not. */
dma_disable("copy"); retried=1;
/* dma-buf stays disabled, so its cached buffers are never reused. */
for(unsigned i=0;i<2;i++) { frame_clear(&frames[i]); dma_clear(&frames[i]); }
pending=0; goto retry;
}
if(frame->failed || !frame->pixels || frame->width!=width || frame->height!=height) goto end;
zwlr_screencopy_frame_v1_destroy(frame->handle); frame->handle=NULL;
const char *pipeline=getenv("UU_WAYLAND_PIPELINE");
if(!pipeline || strcmp(pipeline,"0")) {
struct frame *next=&frames[1-pending];
request_frame(next);
/* Submit the next GPU copy before copying/delivering these CPU pixels. */
if(dispatch_until(&next->submitted)!=0 || wl_display_flush(display)<0) goto end;
}
{
for(uint32_t y=0;y<height;y++) {
uint32_t source=(flags&ZWLR_SCREENCOPY_FRAME_V1_FLAGS_Y_INVERT)?height-y-1:y;
uint32_t source=(frame->flags&ZWLR_SCREENCOPY_FRAME_V1_FLAGS_Y_INVERT)?height-y-1:y;
uint8_t *row=out+(size_t)y*width*4;
memcpy(row,(uint8_t*)pixels+(size_t)source*stride,width*4);
if(format==WL_SHM_FORMAT_XBGR8888 || format==WL_SHM_FORMAT_ABGR8888) {
memcpy(row,(uint8_t*)frame->pixels+(size_t)source*frame->stride,width*4);
if(frame->format==WL_SHM_FORMAT_XBGR8888 || frame->format==WL_SHM_FORMAT_ABGR8888) {
for(uint32_t x=0;x<width;x++) { uint8_t red=row[x*4]; row[x*4]=row[x*4+2]; row[x*4+2]=red; }
}
}
result=0;
}
frame_clear(frame); pending=1-pending;
end:
if(result!=0) for(unsigned i=0;i<2;i++) { frame_clear(&frames[i]); dma_clear(&frames[i]); }
pthread_mutex_unlock(&lock); return result;
}
int uu_wl_move(int32_t x,int32_t y,int relative) {
@@ -275,3 +512,28 @@ int uu_wl_key(uint64_t symbol,int pressed) {
}
pthread_mutex_unlock(&lock); return result;
}
int uu_wl_release_all(void) {
pthread_mutex_lock(&lock);
int result=-1;
if(initialized) {
/* Sweep this client's virtual keyboard directly: a keysym lookup can
fail for unmapped OEM/keypad symbols before reaching held modifiers. */
for(xkb_keycode_t code=xkb_keymap_min_keycode(keymap);code<=xkb_keymap_max_keycode(keymap);code++) {
if(code<8 || code>=sizeof(held_keys) || !xkb_keymap_key_get_name(keymap,code)) continue;
zwp_virtual_keyboard_v1_key(keyboard,timestamp(),code-8,WL_KEYBOARD_KEY_STATE_RELEASED);
if(held_keys[code]) xkb_state_update_key(key_state,code,XKB_KEY_UP);
}
memset(held_keys,0,sizeof(held_keys));
zwp_virtual_keyboard_v1_modifiers(keyboard,
xkb_state_serialize_mods(key_state,XKB_STATE_MODS_DEPRESSED),
xkb_state_serialize_mods(key_state,XKB_STATE_MODS_LATCHED),
xkb_state_serialize_mods(key_state,XKB_STATE_MODS_LOCKED),
xkb_state_serialize_layout(key_state,XKB_STATE_LAYOUT_EFFECTIVE));
for(unsigned code=0x110;code<=0x114;code++)
zwlr_virtual_pointer_v1_button(pointer,timestamp(),code,WL_POINTER_BUTTON_STATE_RELEASED);
zwlr_virtual_pointer_v1_frame(pointer);
result=wl_display_flush(display)<0?-1:0;
}
pthread_mutex_unlock(&lock);return result;
}
+94 -45
View File
@@ -16,6 +16,7 @@
#include <X11/extensions/Xfixes.h>
#include <stdint.h>
#include <poll.h>
#include <pthread.h>
static Display *g_display = NULL;
static Window g_root = 0;
@@ -27,6 +28,8 @@ static int g_depth = 0;
static XShmSegmentInfo g_shminfo;
static XImage *g_shm_image = NULL;
static int g_use_shm = 0;
/* Guards the capture geometry and SHM image against a resize between connections. */
static pthread_mutex_t g_capture_lock = PTHREAD_MUTEX_INITIALIZER;
static unsigned char g_injected_down[32]; // keycodes currently held by the remote viewer
static int g_injected_count = 0;
static int g_restore_repeat = 0;
@@ -52,6 +55,34 @@ static void track_injected_key(int keycode, int is_press) {
}
}
static void shm_open_image(void) {
g_use_shm = 0;
if (!XShmQueryExtension(g_display)) return;
g_shm_image = XShmCreateImage(g_display, DefaultVisual(g_display, g_screen),
g_depth, ZPixmap, NULL, &g_shminfo, g_width, g_height);
if (!g_shm_image) return;
g_shminfo.shmid = shmget(IPC_PRIVATE, g_shm_image->bytes_per_line * g_shm_image->height, IPC_CREAT | 0777);
if (g_shminfo.shmid == -1) return;
g_shminfo.shmaddr = g_shm_image->data = (char *)shmat(g_shminfo.shmid, 0, 0);
g_shminfo.readOnly = False;
if (g_shminfo.shmaddr != (char *)-1 && XShmAttach(g_display, &g_shminfo)) {
XSync(g_display, False);
g_use_shm = 1;
}
shmctl(g_shminfo.shmid, IPC_RMID, 0); // 标记删除,进程退出时自动回收;失败时也不遗留段
}
static void shm_close_image(void) {
if (g_use_shm) XShmDetach(g_display, &g_shminfo);
if (g_shm_image) {
g_shm_image->data = NULL; /* shm memory is released by shmdt, not XDestroyImage */
XDestroyImage(g_shm_image);
g_shm_image = NULL;
}
if (g_shminfo.shmaddr && g_shminfo.shmaddr != (char *)-1) shmdt(g_shminfo.shmaddr);
memset(&g_shminfo, 0, sizeof(g_shminfo));
g_use_shm = 0;
}
int x11_init(const char *display_name) {
if (g_display) return 0;
@@ -77,59 +108,60 @@ int x11_init(const char *display_name) {
g_height = DisplayHeight(g_display, g_screen);
g_depth = DefaultDepth(g_display, g_screen);
// 尝试初始化 XShm
if (XShmQueryExtension(g_display)) {
g_shm_image = XShmCreateImage(g_display, DefaultVisual(g_display, g_screen),
g_depth, ZPixmap, NULL, &g_shminfo, g_width, g_height);
if (g_shm_image) {
g_shminfo.shmid = shmget(IPC_PRIVATE, g_shm_image->bytes_per_line * g_shm_image->height, IPC_CREAT | 0777);
if (g_shminfo.shmid != -1) {
g_shminfo.shmaddr = g_shm_image->data = (char *)shmat(g_shminfo.shmid, 0, 0);
g_shminfo.readOnly = False;
if (g_shminfo.shmaddr != (char *)-1) {
if (XShmAttach(g_display, &g_shminfo)) {
XSync(g_display, False);
shmctl(g_shminfo.shmid, IPC_RMID, 0); // 标记删除,进程退出时自动回收
g_use_shm = 1;
}
}
}
}
}
shm_open_image();
return 0;
}
int x11_get_dimensions(int *out_w, int *out_h) {
if (!g_display) return -1;
pthread_mutex_lock(&g_capture_lock);
*out_w = g_width;
*out_h = g_height;
pthread_mutex_unlock(&g_capture_lock);
return 0;
}
/* The startup size goes stale when RandR/xrdp resizes the root window. Query the
* server and rebuild the SHM image on change. Returns 1 if changed, 0 if not, <0 on error. */
int x11_refresh_geometry(void) {
if (!g_display) return -1;
Window root; int x, y; unsigned int w, h, border, depth;
if (!XGetGeometry(g_display, g_root, &root, &x, &y, &w, &h, &border, &depth) || !w || !h) return -2;
pthread_mutex_lock(&g_capture_lock);
int changed = (int)w != g_width || (int)h != g_height;
if (changed) {
shm_close_image();
g_width = (int)w; g_height = (int)h;
shm_open_image();
}
pthread_mutex_unlock(&g_capture_lock);
return changed;
}
/**
* 截取一帧画面(RGBA/BGRA 格式)
* @param out_buffer 输出缓冲区,大小必须至少为 width * height * 4
*/
/* *out_w/*out_h carry the caller's buffer geometry in; a mismatch (desktop resized
* since that buffer was sized) returns -4 instead of overflowing it. */
int x11_capture_frame(unsigned char *out_buffer, int *out_w, int *out_h) {
if (!g_display) return -1;
pthread_mutex_lock(&g_capture_lock);
int result = 0;
if (*out_w != g_width || *out_h != g_height) result = -4;
else if (g_use_shm && g_shm_image) {
if (!XShmGetImage(g_display, g_root, g_shm_image, 0, 0, AllPlanes)) result = -2;
else memcpy(out_buffer, g_shm_image->data, (size_t)g_width * g_height * 4);
} else {
// Fallback: XGetImage
XImage *img = XGetImage(g_display, g_root, 0, 0, g_width, g_height, AllPlanes, ZPixmap);
if (!img) result = -3;
else { memcpy(out_buffer, img->data, (size_t)g_width * g_height * 4); XDestroyImage(img); }
}
*out_w = g_width;
*out_h = g_height;
if (g_use_shm && g_shm_image) {
if (!XShmGetImage(g_display, g_root, g_shm_image, 0, 0, AllPlanes)) {
return -2;
}
memcpy(out_buffer, g_shm_image->data, g_width * g_height * 4);
return 0;
}
// Fallback: XGetImage
XImage *img = XGetImage(g_display, g_root, 0, 0, g_width, g_height, AllPlanes, ZPixmap);
if (!img) return -3;
memcpy(out_buffer, img->data, g_width * g_height * 4);
XDestroyImage(img);
return 0;
pthread_mutex_unlock(&g_capture_lock);
return result;
}
// ============ 原生输入注入 ============
@@ -195,6 +227,30 @@ int x11_key_event(int keycode, int is_press) {
return 0;
}
/* Session boundaries must also recover XTEST state left by a previous process.
* Only query/release on this authenticated display; never log key state. */
int x11_release_all(void) {
if (!g_display) return -1;
XSync(g_display, False);
char keys[32];
XQueryKeymap(g_display, keys);
for (int code = 8; code < 256; code++) {
if (((unsigned char)keys[code >> 3] | g_injected_down[code >> 3]) & (1u << (code & 7)))
XTestFakeKeyEvent(g_display, code, False, CurrentTime);
}
/* Includes side buttons; wheel buttons are stateless and need no release. */
unsigned char mapping[256];
int buttons = XGetPointerMapping(g_display, mapping, sizeof(mapping));
for (int button = 1; button <= buttons && button <= 9; button++) {
if (button <= 3 || button >= 8) XTestFakeButtonEvent(g_display, button, False, CurrentTime);
}
if (g_restore_repeat) XAutoRepeatOn(g_display);
memset(g_injected_down, 0, sizeof(g_injected_down));
g_injected_count = g_restore_repeat = 0;
XSync(g_display, False);
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
@@ -237,16 +293,9 @@ int x11_terminal_shortcut_state(void) {
void x11_close() {
if (!g_display) return;
if (g_use_shm) {
XShmDetach(g_display, &g_shminfo);
if (g_shm_image) {
XDestroyImage(g_shm_image);
g_shm_image = NULL;
}
if (g_shminfo.shmaddr && g_shminfo.shmaddr != (char *)-1) {
shmdt(g_shminfo.shmaddr);
}
}
pthread_mutex_lock(&g_capture_lock);
shm_close_image();
pthread_mutex_unlock(&g_capture_lock);
XCloseDisplay(g_display);
g_display = NULL;
}
+13
View File
@@ -0,0 +1,13 @@
[Desktop Entry]
Type=Application
Name=UU 远程 Linux 主机
Name[en]=UU Remote Linux Host
GenericName=Remote assistance host
Comment=非官方 UU 远程被控端:远程协助与扫码登录
Comment[en]=Unofficial NetEase UU Remote host: remote assistance and QR sign-in
Exec=uuremote-server show
Icon=uu-remote-linux
Terminal=false
Categories=Network;RemoteAccess;
Keywords=remote;assist;UU;远程;协助;
StartupNotify=false
+13
View File
@@ -0,0 +1,13 @@
<svg xmlns="http://www.w3.org/2000/svg" width="128" height="128" viewBox="0 0 128 128">
<!-- uu-remote-linux app icon (original artwork, GPL-3.0 with the project): a desktop being reached remotely. -->
<defs>
<linearGradient id="bg" x1="0" y1="0" x2="1" y2="1">
<stop offset="0" stop-color="#22d3ee"/>
<stop offset="1" stop-color="#2563eb"/>
</linearGradient>
</defs>
<rect x="4" y="4" width="120" height="120" rx="28" fill="url(#bg)"/>
<rect x="26" y="32" width="76" height="52" rx="7" fill="none" stroke="#ffffff" stroke-width="7"/>
<path d="M52 98h24M64 84v14" stroke="#ffffff" stroke-width="7" stroke-linecap="round"/>
<path d="M46 70l22-22M54 48h14v14" fill="none" stroke="#ffffff" stroke-width="7" stroke-linecap="round" stroke-linejoin="round"/>
</svg>

After

Width:  |  Height:  |  Size: 794 B

+3 -13
View File
@@ -16,18 +16,8 @@ GUI session. Its D-Bus and systemd activation environment is shared by all that
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.
`xdotool search --class '[Gg]nome.[Tt]erminal|gnome-terminal-server'` (window
classes vary by GNOME Terminal version; the server process name is not always
its window class). Do not log window titles or 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.
Binary file not shown.

Before

Width:  |  Height:  |  Size: 84 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 51 KiB

+18
View File
@@ -0,0 +1,18 @@
修复 iOS 和 Windows 客户端无法打开终端。/ Fixes the terminal for iOS and Windows viewers.
### 修复 / Fixed
- **iOS 和 Windows 客户端打开终端失败**(#12,感谢 @cnetst):iOS 提示「创建失败」,Windows 提示「连接已断开」。
终端协议的应答和官方实现不一致:会话列表改为直接返回数组并保留请求编号;客户端指定会话编号时沿用该编号;
「打开成功」的应答保证先于终端输出发送。
✅ 实机验收:官方 iOS 和 Mac 客户端都能正常创建终端、执行命令,后连接的客户端能看到并接着使用已有的终端会话。
Terminal replies now match the official host (bare-array session list, client session ids, OPEN_RESP first).
Verified with the official iOS and Mac viewers.
### 说明 / Note
- 同一时间只能有一台控制设备连接同一个被控端:另一台设备连上时,先连接的会被云端断开,这是网易 UU 的服务端行为,与官方被控端一致。
The UU cloud allows one controlling device at a time; a new controller disconnects the previous one (official behavior).
### 下载 / Download
`uu-remote-linux-v0.1.10-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
+27
View File
@@ -0,0 +1,27 @@
远程协助(实验性)、统一主窗口和应用图标。/ Remote assistance (experimental), a unified main window and an app icon.
<p align="center"><img src="https://raw.githubusercontent.com/VallierDev/uu-remote-linux/v0.1.11/docs/images/main-window.png" width="560" alt="main window"></p>
### 新增 / Added
- **远程协助(实验性)**:和官方客户端一样,把「设备 ID + 验证码」发给别人,对方在 UU 远程的「开始协助」里输入即可连接本机。
- **未登录也能用**(访客身份);**登录后同样可用**,设备 ID 不变。
- 「允许他人远程协助」开关(默认关闭);验证方式:仅临时验证码 / 仅自定义验证码(8–16 位字母+数字)/ 两种都可以;
临时验证码刷新:每次远控结束 / 每小时 / 每 12 小时 / 每 24 小时 / 不刷新;ID 和验证码各有「复制」按钮,另有「复制并分享」。
- 协议参照官方 Windows 精简版被控端实现。✅ 用官方 Mac 客户端实测:访客连接、错误验证码被拒绝、用完自动换码、关闭后不可连接、
登录与退出登录的切换。⚠️ **登录状态下用另一个账号发起远程协助尚未实测**(同账号设备之间不能互相协助,这是官方限制)。
- 安全:拿到 ID 和验证码的人可以完全控制这台电脑,只发给信任的人;验证码只显示在本机窗口,不写入日志。
Remote assistance like the official client (device ID + code), before and after sign-in. Verified with the official
Mac viewer except signed-in assistance from another account.
- **统一主窗口**:一个窗口包含「本设备」(远程协助)和「账号」:未登录时显示扫码二维码,登录后显示账号(手机号打码)和「退出登录」。
扫码登录或退出登录后,主机会自动切换模式。已登录的后台服务默认隐藏窗口,用 `uuremote-server show` 或应用菜单打开;关闭窗口只是隐藏。
One window with remote assistance and the account (QR before sign-in, account + sign out after).
- **应用图标和启动项**(#13):`contrib/desktop/` 里有图标和 `.desktop` 文件,安装后应用菜单里会出现「UU 远程 Linux 主机」。
App icon and desktop launcher.
### 修复 / Fixed
- 服务模式下扫码登录时,二维码(登录凭证)不再打印到标准输出(会进入系统日志)。/ The QR is no longer printed to a service's stdout.
### 下载 / Download
`uu-remote-linux-v0.1.11-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
+15
View File
@@ -0,0 +1,15 @@
Mac 快捷键和在 Mac 上一样用。/ Mac shortcuts work as on a Mac.
### 新增 / Added
- **Mac 快捷键**:在 Mac 远控窗口菜单里**关闭「使用 macOS 快捷键」**后:
- Cmd+C / V / A / F / X / Z 是复制、粘贴、全选、查找、剪切、撤销(Cmd+Shift+Z 等组合同样可用);
- 在终端里 Cmd+C / Cmd+V 自动变成终端的复制、粘贴(Ctrl+Shift+C / V),**Ctrl+C 始终是中断**;
- 其他 Cmd 组合键和单独按 Cmd 仍然是 Super(Windows 键)。
只对 Mac 控制端生效(按连接请求里的控制端平台判断),Windows 和手机控制端不受影响。✅ 已用官方 Mac 客户端在 X11 实机验收。
保持该选项开启时行为和以前一样(客户端会把 Cmd 当成 Ctrl 发送)。
With 「使用 macOS 快捷键」 off, Cmd shortcuts behave as on a Mac (terminal-aware copy/paste), only for macOS viewers.
### 下载 / Download
`uu-remote-linux-v0.1.12-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
+17
View File
@@ -0,0 +1,17 @@
托盘图标。/ Tray icon.
### 新增 / Added
- **托盘图标**:后台运行时,顶栏/任务栏会显示托盘图标,菜单里有「打开主窗口」、「允许他人远程协助」(与主窗口开关同步)和「退出」。
- GNOME 通过 AppIndicator 显示(Ubuntu 自带扩展;运行时加载 libayatana-appindicator,无需额外编译依赖);Xfce / Cinnamon / KDE 等使用系统托盘。
- 「退出」会停止 `uuremote-native.service`(不会被自动重启);之后从应用菜单「UU 远程 Linux 主机」打开,会重新启动服务并显示主窗口。
✅ Ubuntu 24.04 GNOME(X11)实机验收:图标、菜单、开关同步、退出、从应用菜单重新启动。⚠️ Xfce 等系统托盘路径尚未实机验收。
Tray icon with open / allow / quit; Quit stops the user service and the launcher restarts it. Verified on Ubuntu GNOME.
### 改进 / Improved
- `uuremote-server show`:主机没在运行时,优先启动已安装的 `uuremote-native.service`,而不是另起一个不受管理的进程。
- 修复启动时的一个时序问题:先注册窗口唤起信号再写入 PID 文件,避免过早的 `show` 意外结束主机进程。
### 下载 / Download
`uu-remote-linux-v0.1.13-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
+13
View File
@@ -0,0 +1,13 @@
修复手机切回终端时黑屏。/ Fixes a black terminal after returning to it on mobile.
### 修复 / Fixed
- **终端历史输出较多时,重连后黑屏**(#14,感谢 @cnetst):手机客户端切到后台再回到同一个终端时,如果之前积累的输出较多,
会整屏黑屏,连新建终端也没有输出,只能完全退出 App 重连。原因是重连时把最多 256 KB 的历史输出作为一条消息发送,
超过了 WebRTC 数据通道单条消息 64 KB 的上限,发送失败后整个终端连接卡住。现在历史输出按 16 KB 分段发送,顺序不变。
✅ 已用官方手机客户端在 3090 实机验收。
Terminal scrollback is replayed in 16 KiB chunks instead of one message over the 64 KiB SCTP limit. Verified on mobile.
### 下载 / Download
`uu-remote-linux-v0.1.14-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
+20
View File
@@ -0,0 +1,20 @@
支持真彩(4:4:4)。/ True color (4:4:4) support.
### 新功能 / Added
- **真彩(4:4:4)**:在客户端画面菜单打开「真彩」后,主机改用 H.264 High 4:4:4 编码,彩色细字(红字、链接、
代码高亮)边缘不再发虚;关闭后切回 4:2:0。适用于默认的 FFmpeg 管线(NVENC 或 libx264);VAAPI 和
`--features inproc-av` 管线仍为 4:2:0,并如实告知客户端。
✅ 已用官方 Mac 客户端在 RTX 3090(NVENC)上实机验证:开关来回切换正常、画面正常解码。
When 真彩 is enabled the host encodes H.264 High 4:4:4 (default FFmpeg pipeline with NVENC or libx264); VAAPI and
inproc-av stay 4:2:0 and report it. Verified with the official Mac viewer on NVENC (toggling, decoding).
- 日志记录客户端声明的解码能力(编码类型、色度、最大分辨率、帧率,仅数字),便于排查画质问题。
The log now shows the viewer's declared decoder capabilities (numbers only).
### 说明 / Notes
- 4:4:4 码率会略高于 4:2:0;在缩放到较低分辨率时差别不明显,建议配合「跟随远端」分辨率使用。
4:4:4 costs some bitrate; the difference is hardly visible when the stream is scaled down.
### 下载 / Download
`uu-remote-linux-v0.1.15-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
+22
View File
@@ -0,0 +1,22 @@
支持 H.265(HEVC)。/ H.265 (HEVC) support.
### 新功能 / Added
- **H.265**:控制端和主机都支持时自动使用 H.265,同等画质下码率更低。与官方主机一致,由主机按双方能力选择编码
(官方 Mac 客户端没有手动切换编码的菜单)。连接时主机会告知客户端自己能编哪些格式(CodecNegotiation)。
支持 `hevc_nvenc`、`libx265` 和显式 VAAPI 的 `hevc_vaapi`;硬件失败先回落软件 HEVC,再回落 H.264。
需要时用 `UU_VIDEO_CODEC=h264` 强制使用 H.264。
- 真彩(4:4:4)同样适用于 H.265(hevc_nvenc `rext` / libx265 `main444-8`)。
✅ 已用官方 Mac 客户端在 RTX 3090(hevc_nvenc,含 4:4:4)实机验收:画面正常、重连正常、无丢帧。
⚠️ libx265、hevc_vaapi、Windows/手机客户端尚未实机验证;如有问题请设置 `UU_VIDEO_CODEC=h264` 并提 issue。
The host now uses H.265 when both sides support it (the host picks the codec, like the official host), announces
its encoders with CodecNegotiation, and falls back to software HEVC, then H.264. `UU_VIDEO_CODEC=h264` forces H.264.
Verified with the official Mac viewer on NVENC including 4:4:4; other encoders and viewers are unverified.
### 修复 / Fixed
- 日志新增客户端 offer 中的编码列表与最终协商结果(仅编码名),便于排查画面问题。
The log shows offered and negotiated video codecs (names only).
### 下载 / Download
`uu-remote-linux-v0.1.16-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
+24
View File
@@ -0,0 +1,24 @@
修复终端接回错位、低码率 CPU 编码灰屏、niri 无法建流。/ Terminal reattach, low-bitrate CPU encoding and niri fixes.
### 修复 / Fixed
- **终端接回后全屏程序画面错位**(#17):接回已有会话时,主机先把终端行数临时减一、稍后恢复,触发 tmux、vim、
Claude Code 等全屏程序整屏重画。
⚠️ 已通过单元测试;尚未用 Android 客户端实机验证,如仍错位请在 #17 反馈。
Reattaching a terminal session now nudges the PTY size so full-screen programs repaint. Unit-tested; not yet
verified with the Android viewer.
- **低码率档周期性灰屏**(#15,感谢 @mengdehong):外部 FFmpeg 的 CPU 编码(libx264,以及同样处理的 libx265)在
码率档(原画 / 超清 / 高清 / 自定义)改用平均码率(ABR),避免 4K 低码率下关键帧耗尽缓冲而发灰;「自动」档仍用 CRF。
libx264 修复由贡献者在 Android 客户端实测;libx265 只验证了参数可用。
CPU encoding uses ABR for bitrate presets (CRF for Auto), fixing gray keyframes at 4K and low bitrates.
- **niri 等合成器无法建流**(#15):原生 Wayland 后端在合成器没有 `wlr-output-power-management` 时跳过唤醒屏幕,
不再中断采集。Capture continues when the compositor lacks output power management.
### 文档 / Docs
- Wayland 会话的 systemd 单元写法、uinput 模块开机加载(#16,感谢 @meotao 的实机测量与建议);未编入 `wayland`
特性时的报错改为明确提示重新编译。
Wayland systemd unit, uinput autoload, and a clearer error when the binary lacks the `wayland` feature.
### 下载 / Download
`uu-remote-linux-v0.1.17-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
+24
View File
@@ -0,0 +1,24 @@
修复分辨率变化后无法出画面;Wayland 采集提速;终端接回修复。/ Resolution change fix, faster Wayland capture, terminal fixes.
### 修复 / Fixed
- **服务运行中桌面分辨率变化后,新连接一直停在「正在传输画面」**(#18,感谢 @liangjiahui01):每次建立桌面连接前重新读取
X11 桌面尺寸,变化时重建截屏;截屏尺寸不符时报明确错误,不再误报「HEVC unavailable」。
已在隔离 Xvfb 上用 `xrandr` 改尺寸验证;xrdp 场景未实测。连接期间改分辨率暂不支持(需重连)。
X11 now re-reads the desktop size before each connection, so a RandR/xrdp resize no longer stalls new connections.
- **终端接回**(#17 后续):修复延迟恢复尺寸可能覆盖新尺寸的竞态,以及接回时同一段输出既在回放里又被实时重发的问题。
Fixed a size-restore race and duplicated output on terminal reattach.
### 改进 / Improved
- **原生 Wayland 采集**(#16,感谢 @meotao 的实机测量):采集进程直接写入 FFmpeg,不再经主进程中转 4K 帧;采集与写管道
双线程交替,管道缓冲扩大;SHM 截屏缓冲复用,不再每帧分配。
⚠️ 只做了编译和单元测试,没有在真机上测到提速效果;请求高帧率时的实际帧率欢迎在 #16 反馈。
Native Wayland: capture pipes straight into FFmpeg, overlaps capture with writes and reuses SHM buffers. Not yet
measured on real hardware.
- 视频统计日志新增本窗口的 `window_s`、`window_sent`、`window_skipped`(原 `sent`/`skipped` 仍为累计值)。
The latest-frame stats line adds per-window counts.
### 下载 / Download
`uu-remote-linux-v0.1.18-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
Wayland 后端需自行用 `--features wayland` 编译。
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
+14
View File
@@ -0,0 +1,14 @@
信令断线检测:主机休眠恢复或网络悄悄断开后自动重新上线。/ Detect dead signaling connections and reconnect.
### 修复 / Fixed
- **主机显示在线但控制端看不到、只能手动重启服务**(#19,感谢 @Keinns):之前只有收到关闭帧或读错误才判定信令断开;
系统休眠恢复、NAT 超时等造成的半开连接会让主机一直以为自己在线。现在按 Engine.IO 协议,超过服务器下发的
`pingInterval + pingTimeout`(目前约 33 秒)没收到心跳就判定断线,走已有的自动重连。日志:`Signaling heartbeat lost`。
已用模拟的「不发心跳」信令服务器单元测试验证;真实休眠恢复场景未实测。
Signaling now treats a connection without pings for pingInterval + pingTimeout as dead and reconnects.
Unit-tested against a silent mock server; real suspend/resume not yet measured.
### 下载 / Download
`uu-remote-linux-v0.1.19-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
+16
View File
@@ -0,0 +1,16 @@
登录诊断与系统证书支持。/ Login diagnostics and system certificate support.
### 修复 / Fixed
- **企业代理 / 自定义 CA 环境下登录失败**(#21,感谢 @liuji10000qian-svg):除内置根证书外,现在也信任系统证书库,
并遵循 `SSL_CERT_FILE` / `SSL_CERT_DIR`。会做 TLS 拦截的代理(使用自签 CA)此前会导致设备注册请求失败。
Trust the system certificate store and honor `SSL_CERT_FILE` / `SSL_CERT_DIR` in addition to the bundled roots.
- **网络请求失败时只显示「error sending request」**:错误信息现在包含底层原因链(证书、代理、DNS、超时等),
并去掉 URL 中的用户名密码(如代理凭据)和查询参数。
Transport errors now include their cause chain, with URL credentials and query strings removed.
⚠️ 自定义 CA / 代理环境未实测,请在 #21 反馈新版本的错误信息。
### 下载 / Download
`uu-remote-linux-v0.1.20-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
+17
View File
@@ -0,0 +1,17 @@
视频管线减少重复分配和过量编码。/ Fewer allocations and less wasted encoding in the video pipeline.
### 改进 / Improved(#20,感谢 @mengdehong)
- 采集到发送之间最多 4 帧在途:网络发不出去时采集端自动放慢,不再白白编码无法送出的帧。
贡献者实验(发送受阻场景):编码 CPU 耗时降低约 88%。
- 原生 Wayland 可选的 dma-buf 路径、Portal / KMS 路径复用采集缓冲;Portal 同一帧不重复复制。
- X11 进程内编码(`inproc-av`,需自行编译)缓存静态画面的采集与转色结果:静态画面每帧 CPU 降低约 40%。
- 原生 Wayland 关闭时完整释放协议对象。
⚠️ 以上性能数据来自贡献者在隔离 headless Sway / Xvfb 中的合成画面实验,未在维护者的真机上复测。
预编译的 X11 包默认走外部 FFmpeg 管线,受影响的主要是 Wayland / Portal / KMS / `inproc-av` 自编译用户。
Performance figures come from the contributor's headless Sway/Xvfb experiments; not re-measured on maintainer hardware.
### 下载 / Download
`uu-remote-linux-v0.1.21-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
+15
View File
@@ -0,0 +1,15 @@
v0.1.21 视频管线改动的两处修正。/ Two follow-up fixes to the v0.1.21 video pipeline changes.
### 修复 / Fixed
- 原生 Wayland 的 dma-buf 映射失败、回退到 SHM 时,已缓存的 dma-buf 缓冲没有释放(最多两个槽位,到会话结束才回收)。
Release cached dma-buf buffers when capture falls back to SHM.
- 原生 Wayland 在发送受阻(背压)解除后的第一帧可能是受阻前预取的旧画面;现在等待超过两个帧间隔时会重新采集。
After a backpressure stall the first native Wayland frame is captured fresh instead of reusing the stale prefetch.
两处均由代码审查发现,只做了编译和单元测试,未在真机复现。
Both found in review; compile- and unit-tested only.
### 下载 / Download
`uu-remote-linux-v0.1.22-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
+14
View File
@@ -0,0 +1,14 @@
测量工具:`desktop-video-probe` 可选码率档。/ Probe bitrate presets.
### 新增 / Added
- `desktop-video-probe` 支持 `UU_PROBE_QUALITY=1..5`,对应控制端画质档(1 极速 1M、2 流畅 2M、3 高清 8M、4 蓝光 20M、5 自动)。
码率档走平均码率(ABR)路径,便于不用控制端复测 #15 的灰屏修复(#9 的建议)。
`desktop-video-probe` accepts `UU_PROBE_QUALITY=1..5` (viewer bitrate presets; 5 = Auto/CRF).
### 其他 / Other
- 修复一个在多网卡机器上偶发超时的编码协商单元测试。
### 下载 / Download
`uu-remote-linux-v0.1.23-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
+17
View File
@@ -0,0 +1,17 @@
支持 Ubuntu 22.04 自带的 FFmpeg 4.4。/ Works with Ubuntu 22.04's FFmpeg 4.4.
### 改进 / Improved
- **自动适配 FFmpeg 版本**(#22,感谢 @Guancheng-Li):启动编码前检测所用 FFmpeg 支持的参数。FFmpeg 5 及以上继续使用
`-fps_mode vfr -enc_time_base 1/<fps>`;FFmpeg 4.x(如 Ubuntu 22.04)改用 `-vsync 0`。检测失败时保持新版参数。
FFmpeg 4.x 下采集卡顿后的补帧会全部编码(新版会丢弃),属于已知降级。贡献者已在 FFmpeg 4.4.2 上验证。
The host probes the FFmpeg it uses and falls back to `-vsync 0` only on FFmpeg 4.x; probe failures keep the modern arguments.
### 其他 / Other
- 新增几条只记结构的诊断日志(收到的 SOAC offer 结构、SDP 段落属性名、ice_id 是否一致),用于排查与官方控制端的互通,
不记录 SDP 内容或任何 ID。
Structure-only diagnostics for incoming offers (no SDP contents or ids).
### 下载 / Download
`uu-remote-linux-v0.1.24-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
+1 -1
View File
@@ -16,7 +16,7 @@ VAAPI 硬件编码、虚拟声卡、无人登录的虚拟桌面和 Arch/Omarchy
- **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 声卡被拔掉),不再留下虚拟输出作为默认。
+25
View File
@@ -0,0 +1,25 @@
修复连接断开或重启后鼠标/按键卡住,Hyprland 截屏改为流水线。/ Fixes stuck mouse buttons/keys after disconnects or restarts; pipelined Hyprland capture.
### 修复 / Fixed
- **鼠标键或按键卡在按下状态,导致之后点击全部无效**:如果远程连接在按住鼠标或按键时断开,或者服务重启,按下状态可能残留,
之后的点击和输入都会出错。现在桌面连接建立、断开以及服务启动/退出时,会自动松开所有按住的键和鼠标键
(X11 松开显示器上所有按下的键;uinput 重置本服务的虚拟设备;Wayland/portal 松开本服务能发送的所有键)。
旧连接迟到的事件不会影响新连接;文件传输、终端、端口映射这些工具连接不会触发松键。
✅ X11 实机(官方 Mac 控制端,按住鼠标时关闭远控窗口后重连)和虚拟机验收通过;portal 只做了编译和单元测试。
Input is released at desktop session boundaries and at service start/stop. Verified on X11 hardware; portal unit-tested only.
注意:X11 下会同时松开本机键盘上正在按住的键。/ On X11 this also releases keys held on the local keyboard.
### 改进 / Improved
- **Hyprland 原生后端(`--features wayland`)**:截屏改为双缓冲流水线,控制端选择的帧率会同步到截屏和编码
(#9)。可选 `UU_WAYLAND_DMABUF=1` 尝试 dma-buf 映射(默认关闭,只支持线性缓冲,失败回退 shm);`UU_WAYLAND_PIPELINE=0` 关闭流水线。
⚠️ 只在虚拟机里测量过,实机帧率提升尚未验证,实际帧率仍受合成器限制。发布包(X11 版)不含此功能。
Pipelined screencopy and viewer-selected fps for the native Wayland backend (VM-measured only).
- **AMD VAAPI**:#9 的报告者在 AMD Phoenix 上实测 v0.1.7 的 `UU_VIDEO_ENCODER=vaapi` 有效,消除了高分辨率下的顿挫感。
AMD VAAPI confirmed by a user on AMD Phoenix.
- 无人登录的虚拟桌面(`contrib/virtual-desktop`)在虚拟机里用官方 Mac 控制端验收了画面、键鼠、剪贴板(虚拟机→Mac)、
静音运行的声音和会话结束后的恢复。/ Linger virtual desktop accepted in a VM with the official Mac viewer.
### 下载 / Download
`uu-remote-linux-v0.1.9-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
+536
View File
@@ -0,0 +1,536 @@
//! Opt-in guest assistance. Protocol sources: AGENTS.md "Remote assistance".
//! Secret values never implement Display/Debug or enter diagnostics.
use crate::auth::{Credentials, RoomData, UUAuthClient};
use serde_json::{json, Value};
use sha2::{Digest, Sha256};
use std::io::{Read, Write};
use std::os::unix::{fs::OpenOptionsExt, io::AsRawFd};
const CREATE: &str = "/api/v1/guest/create";
const INFO: &str = "/api/v1/guest/share/info";
const CANCEL: &str = "/api/v1/guest/share/cancel";
const ROOM: &str = "/api/v1/guest/room/create";
const SIGN: &str = "/api/v1/guest/room/share/upload/sign";
const CONTROL_MODE: &str = "/api/v1/guest/room/share/upload_control_mode";
// Logged-in host: the same flow on the account identity (official Windows host NrdDevice/NrdBusinessEntry).
const ACCOUNT_INFO: &str = "/api/v1/device/share/info";
const ACCOUNT_CANCEL: &str = "/api/v1/device/share/cancel";
const ACCOUNT_SIGN: &str = "/api/v2/room/share/upload_sign";
const ACCOUNT_CONTROL_MODE: &str = "/api/v2/room/share/upload_control_mode";
const ALPHABET: &[u8] = b"ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
/// Guest mode uses its own device/config identity and never consumes an account token.
pub fn configure() -> Result<(), String> {
if ["UU_USER_ID", "UU_TOKEN", "UU_CSRF"].iter().any(|k| std::env::var_os(k).is_some_and(|v| !v.is_empty())) {
return Err("assist mode cannot use account credential environment variables".into());
}
if std::env::var_os("UU_CONFIG").is_none() {
if crate::config::read_fresh()?.as_ref().is_some_and(|v| ["token", "user_id"].iter()
.any(|k| v.get(k).and_then(Value::as_str).is_some_and(|s| !s.is_empty()))) {
return Err("account identity already configured; guest assist needs an explicit separate UU_CONFIG".into());
}
let path = crate::config::path().with_file_name("assist").join("config.json");
std::env::set_var("UU_CONFIG", path);
}
if crate::config::read_fresh()?.as_ref().is_some_and(|v| ["token", "user_id", "csrf"].iter()
.any(|k| v.get(k).and_then(Value::as_str).is_some_and(|s| !s.is_empty()))) {
return Err("assist requires a separate config without account credentials".into());
}
Ok(())
}
/// Official RecipientConfigModel.verificationType: temporary only, custom only, or both.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Verify { Temporary = 0, Custom = 1, Both = 2 }
impl Verify {
fn parse(v: &str) -> Option<Self> { match v { "temporary" => Some(Self::Temporary), "custom" => Some(Self::Custom), "both" => Some(Self::Both), _ => None } }
fn name(self) -> &'static str { match self { Self::Temporary => "temporary", Self::Custom => "custom", Self::Both => "both" } }
fn from_index(i: i64) -> Option<Self> { match i { 0 => Some(Self::Temporary), 1 => Some(Self::Custom), 2 => Some(Self::Both), _ => None } }
}
/// Official RecipientCodeRefreshType: after each session (closeConnect), hourly, 12 h, 24 h, never.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Refresh { Session = 0, Hour = 1, Hour12 = 2, Hour24 = 3, Never = 4 }
impl Refresh {
fn parse(v: &str) -> Option<Self> { match v { "session" => Some(Self::Session), "1h" => Some(Self::Hour), "12h" => Some(Self::Hour12), "24h" => Some(Self::Hour24), "never" => Some(Self::Never), _ => None } }
fn name(self) -> &'static str { match self { Self::Session => "session", Self::Hour => "1h", Self::Hour12 => "12h", Self::Hour24 => "24h", Self::Never => "never" } }
fn from_index(i: i64) -> Option<Self> { [Self::Session, Self::Hour, Self::Hour12, Self::Hour24, Self::Never].get(usize::try_from(i).ok()?).copied() }
fn period(self) -> Option<std::time::Duration> {
match self { Self::Hour => Some(3600), Self::Hour12 => Some(12 * 3600), Self::Hour24 => Some(24 * 3600), _ => None }.map(std::time::Duration::from_secs)
}
}
/// Mainland mobile numbers are shown like the official client: 186******08.
pub fn display_account(name: &str) -> String {
if name.len() == 11 && name.bytes().all(|b| b.is_ascii_digit()) { format!("{}******{}", &name[..3], &name[9..]) } else { name.to_owned() }
}
/// 8-16 ASCII letters/digits including both (official RecipientCustomCodeView rules).
fn valid_custom(code: &str) -> bool {
(8..=16).contains(&code.len()) && code.bytes().all(|b| b.is_ascii_alphanumeric())
&& code.bytes().any(|b| b.is_ascii_alphabetic()) && code.bytes().any(|b| b.is_ascii_digit())
}
pub enum Command { Allow, Deny, Reset, Stop, Verify(Verify), Refresh(Refresh), Custom(String), Logout, Quit }
#[cfg(uu_gtk)]
extern "C" {
fn uu_assist_ui_start(hidden: libc::c_int, hide_on_close: libc::c_int) -> libc::c_int;
fn uu_assist_ui_present();
fn uu_ui_set_user(name: *const libc::c_char);
fn uu_assist_ui_set(id: *const libc::c_char, code: *const libc::c_char, allowed: libc::c_int, verify: libc::c_int, refresh: libc::c_int, has_custom: libc::c_int);
fn uu_assist_ui_take_command(arg: *mut libc::c_char, len: libc::size_t) -> libc::c_int;
fn uu_assist_ui_close();
}
/// "243829726" -> "243 829 726", as the official client shows device IDs.
fn display_id(id: &str) -> String {
if id.is_empty() || !id.bytes().all(|b| b.is_ascii_digit()) { return id.to_owned(); }
id.as_bytes().chunks(3).map(|c| String::from_utf8_lossy(c).into_owned()).collect::<Vec<_>>().join(" ")
}
/// Where the ID/code are shown and commands come from: a local GTK window (c_engine/assist_window.c)
/// when a display is available, otherwise /dev/tty, which bypasses stdout/stderr, journals,
/// redirected output and the logger. Without either, refuse startup rather than disclose the code.
pub enum LocalUi { Tty(tokio::io::unix::AsyncFd<std::fs::File>), #[cfg(uu_gtk)] Window }
impl LocalUi {
/// Standalone `assist`: visible window (closing it stops assistance), else /dev/tty.
pub fn open() -> Result<Self, String> { Self::open_visible(false) }
/// `hide_on_close`: the not-logged-in host service (window stays reachable with `uuremote-server show`).
pub fn open_visible(hide_on_close: bool) -> Result<Self, String> {
#[cfg(not(uu_gtk))] let _ = hide_on_close;
#[cfg(uu_gtk)]
if std::env::var("UU_ASSIST_UI").ok().as_deref() != Some("tty") && Self::display() && unsafe { uu_assist_ui_start(0, i32::from(hide_on_close)) } == 0 {
return Ok(Self::Window);
}
let tty = std::fs::OpenOptions::new().read(true).write(true)
.custom_flags(libc::O_NONBLOCK | libc::O_NOCTTY).open("/dev/tty")
.map_err(|_| "assist requires a desktop window or a local controlling terminal; no code was printed")?;
if unsafe { libc::isatty(tty.as_raw_fd()) } != 1 { return Err("assist output is not a terminal".into()); }
Ok(Self::Tty(tokio::io::unix::AsyncFd::new(tty).map_err(|_| "assist terminal registration failed")?))
}
fn display() -> bool { std::env::var_os("DISPLAY").is_some() || std::env::var_os("WAYLAND_DISPLAY").is_some() }
/// Host service: a hidden window opened on demand (`uuremote-server show`); closing only hides it.
/// No terminal fallback: a background service has nowhere safe to print the code.
pub fn open_service() -> Result<Self, String> {
#[cfg(uu_gtk)]
if Self::display() && unsafe { uu_assist_ui_start(1, 1) } == 0 { return Ok(Self::Window); }
Err("remote assistance needs a desktop window for the host service".into())
}
/// Account card: signed-in user (name already masked for display).
pub fn set_user(&self, name: &str) {
#[cfg(uu_gtk)]
if matches!(self, Self::Window) { let name = std::ffi::CString::new(name).unwrap_or_default(); unsafe { uu_ui_set_user(name.as_ptr()) } }
#[cfg(not(uu_gtk))] let _ = name;
}
pub fn present(&self) {
#[cfg(uu_gtk)]
if matches!(self, Self::Window) { unsafe { uu_assist_ui_present() } }
}
fn show(&self, id: &str, code: &str, allowed: bool, verify: Verify, refresh: Refresh, has_custom: bool) -> Result<(), String> {
#[cfg(not(uu_gtk))] let _ = has_custom;
match self {
Self::Tty(tty) => {
// Bound/control-character-validated server identifier; no terminal escape injection.
let mut tty = tty.get_ref();
write!(tty, "\n远程协助 / Remote assistance\n设备 ID: {}\n临时验证码: {code}\n允许他人远程协助: {allowed}\n验证方式: {} 自动刷新: {}\n仅向可信的人提供验证码。allow=允许,deny=不允许,reset=换码,mode temporary|custom|both,refresh session|1h|12h|24h|never,off=关闭并撤销\n", display_id(id), verify.name(), refresh.name())
.map_err(|_| "assist terminal display failed".into())
}
#[cfg(uu_gtk)]
Self::Window => {
let (id, code) = (std::ffi::CString::new(display_id(id)).unwrap_or_default(), std::ffi::CString::new(code).unwrap_or_default());
unsafe { uu_assist_ui_set(id.as_ptr(), code.as_ptr(), i32::from(allowed), verify as i32, refresh as i32, i32::from(has_custom)) };
Ok(())
}
}
}
pub async fn command(&self) -> Command {
match self {
Self::Tty(tty) => Self::tty_command(tty).await,
#[cfg(uu_gtk)]
Self::Window => loop {
let mut arg = [0 as libc::c_char; 40];
let cmd = unsafe { uu_assist_ui_take_command(arg.as_mut_ptr(), arg.len()) };
let text = unsafe { std::ffi::CStr::from_ptr(arg.as_ptr()) }.to_string_lossy().into_owned();
let index = text.parse::<i64>().unwrap_or(-1);
match cmd {
1 => return Command::Allow, 2 => return Command::Deny, 3 => return Command::Reset, 4 => return Command::Stop,
5 => if let Some(v) = Verify::from_index(index) { return Command::Verify(v) },
6 => if let Some(r) = Refresh::from_index(index) { return Command::Refresh(r) },
7 => return Command::Custom(text),
8 => return Command::Logout,
9 => return Command::Quit,
_ => tokio::time::sleep(std::time::Duration::from_millis(150)).await,
}
},
}
}
async fn tty_command(tty: &tokio::io::unix::AsyncFd<std::fs::File>) -> Command {
let mut line = Vec::new();
loop {
let mut ready = match tty.readable().await { Ok(v) => v, Err(_) => return Command::Stop };
let result = ready.try_io(|fd| { let mut byte = [0u8]; let mut tty = fd.get_ref(); tty.read(&mut byte).map(|n| (n, byte[0])) });
match result {
Ok(Ok((0, _))) | Ok(Err(_)) => return Command::Stop,
Ok(Ok((_, byte))) if byte == b'\n' => {
let text = String::from_utf8_lossy(&line).trim().to_owned();
let cmd = match text.split_once(' ') {
Some(("mode", v)) => Verify::parse(v).map(Command::Verify),
Some(("refresh", v)) => Refresh::parse(v).map(Command::Refresh),
_ => match text.as_str() { "allow" => Some(Command::Allow), "deny" => Some(Command::Deny), "reset" => Some(Command::Reset), "off" | "q" => Some(Command::Stop), _ => None },
};
line.clear(); if let Some(cmd) = cmd { return cmd; }
}
Ok(Ok((_, byte))) => { if line.len() < 64 { line.push(byte); } },
Err(_) => {},
}
}
}
}
impl Drop for LocalUi {
fn drop(&mut self) {
#[cfg(uu_gtk)]
if matches!(self, Self::Window) { unsafe { uu_assist_ui_close() } }
}
}
fn response(status: u16, value: &Value) -> Result<&Value, String> {
let code = value.get("code").and_then(Value::as_i64).unwrap_or(-1);
if status == 200 && code == 0 { return Ok(value.get("data").unwrap_or(&Value::Null)); }
// No server message, validation payload, headers, URLs, token or code in errors. UU_ASSIST_DEBUG=1 adds
// the server's short human-readable message (bounded) for diagnosing rejected guest calls.
let message = (std::env::var("UU_ASSIST_DEBUG").ok().as_deref() == Some("1"))
.then(|| value.get("message").or_else(|| value.get("msg")).and_then(Value::as_str)
.map(|m| m.chars().filter(|c| !c.is_control()).take(120).collect::<String>()))
.flatten().map(|m| format!(" message={m:?}")).unwrap_or_default();
Err(format!("assist API rejected request: http={status} code={code}{message}"))
}
fn text(value: &Value, key: &str) -> Result<String, String> {
value.get(key).and_then(Value::as_str).filter(|s| !s.is_empty() && s.len() <= 16384)
.map(str::to_owned).ok_or_else(|| "assist response missing required string".into())
}
fn share_id(value: &Value) -> Result<String, String> {
let id = text(value, "connect_id")?;
if id.len() > 128 || !id.bytes().all(|c| c.is_ascii_alphanumeric() || c == b'-') {
return Err("assist response invalid device identifier".into());
}
Ok(id)
}
fn generate_code() -> Result<String, String> {
let mut random = std::fs::File::open("/dev/urandom").map_err(|_| "assist random source unavailable")?;
let mut code = String::new();
// Rejection sampling avoids modulo bias (alphabet has 32 entries).
let limit = 256 / ALPHABET.len() * ALPHABET.len();
while code.len() < 8 {
let mut byte = [0u8]; random.read_exact(&mut byte).map_err(|_| "assist random source failed")?;
if (byte[0] as usize) < limit { code.push(ALPHABET[byte[0] as usize % ALPHABET.len()] as char); }
}
Ok(code)
}
fn digest(salt: &str, code: &str) -> String {
let mut hash = Sha256::new(); hash.update(salt.as_bytes()); hash.update(code.as_bytes()); hex::encode(hash.finalize())
}
/// Official: temporary-only signs the temporary code with an empty backup; both adds the custom code
/// as `backup_sign`; custom-only puts the custom code in the primary `sign`.
fn sign_body(control_id: &str, salt: &str, temporary: &str, custom: Option<&str>, verify: Verify, allow: bool) -> Value {
let (primary, backup) = match (verify, custom) {
(Verify::Custom, Some(c)) => (c, None),
(Verify::Both, Some(c)) => (temporary, Some(c)),
_ => (temporary, None),
};
json!({"can_remote_control": allow, "control_id": control_id,
"sign": digest(salt, primary), "backup_sign": backup.map(|c| digest(salt, c)).unwrap_or_default()})
}
fn challenge(value: &Value) -> Option<(&str, &str)> {
let id = value.get("control_id")?.as_str()?; let salt = value.get("salt")?.as_str()?;
(!id.is_empty() && id.len() <= 256 && !salt.is_empty() && salt.len() <= 4096).then_some((id, salt))
}
#[derive(Default)]
struct RetryBackoff(u64);
impl RetryBackoff {
fn next(&mut self) -> std::time::Duration { self.0 = (self.0 + 15).min(99); std::time::Duration::from_secs(self.0) }
}
pub struct Assist {
_lock: std::fs::File,
pub renewed: bool,
device: Credentials,
pub client: std::sync::Arc<UUAuthClient>,
ui: std::sync::Arc<LocalUi>,
code: String,
custom: Option<String>,
id: String,
pub allowed: bool,
verify: Verify,
refresh: Refresh,
code_at: std::time::Instant,
/// Logged-in host: account endpoints and the host's own room (no guest identity).
account: bool,
}
impl Assist {
pub async fn start(shutdown: &mut tokio::sync::watch::Receiver<bool>, service: bool) -> Result<Self, String> {
let ui = std::sync::Arc::new(LocalUi::open_visible(service)?);
let lock = Self::lock()?;
// guest/create rejects an unregistered device (HTTP 422 code 1001, verified live): register the
// Windows device identity first, exactly as account login does (no account involved).
let (config, device) = crate::login::prepare_device().await.map_err(|_| "assist device registration failed")?;
let (custom, verify, refresh, allowed) = Self::settings(&config)?;
let mut retry = RetryBackoff::default();
let client = loop {
if *shutdown.borrow() { return Err("assist stopped".into()); }
let result = tokio::select! {
result = Self::authenticate(&device) => result,
_ = shutdown.changed() => return Err("assist stopped".into()),
_ = ui.command() => return Err("assist stopped before guest identity was available".into()),
};
match result { Ok(client) => break client, Err(error) => {
let delay = retry.next();
// `error` carries only HTTP status / API code numbers or a fixed transport label.
log::warn!("Guest identity request failed: {error}; retry_s={}", delay.as_secs());
tokio::select! {
_ = tokio::time::sleep(delay) => {},
_ = shutdown.changed() => return Err("assist stopped".into()),
_ = ui.command() => return Err("assist stopped before guest identity was available".into()),
}
}}
};
let this = Self { _lock: lock, renewed: false, device, client: std::sync::Arc::new(client), ui, code: generate_code()?, custom, id: String::new(),
allowed, verify, refresh, code_at: std::time::Instant::now(), account: false };
// guest/share/info answers 400/2004 until a guest room exists (verified live), so the ID/code
// are published right after the first room(), which serve() requests before signaling.
Ok(this)
}
fn settings(config: &Value) -> Result<(Option<String>, Verify, Refresh, bool), String> {
let custom = config.get("assist_custom_code").and_then(Value::as_str).map(str::to_owned);
if custom.as_deref().is_some_and(|c| !valid_custom(c)) {
return Err("assist_custom_code requires 8-16 ASCII letters/digits, including both".into());
}
let setting = |key: &str| config.get(key).and_then(Value::as_str).map(str::to_owned);
let verify = setting("assist_verify").as_deref().and_then(Verify::parse).filter(|v| *v == Verify::Temporary || custom.is_some()).unwrap_or(Verify::Temporary);
let refresh = setting("assist_refresh").as_deref().and_then(Refresh::parse).unwrap_or(Refresh::Session);
// Official default is "not allowed"; the switch state persists like isAllowRecipient.
let allowed = std::env::var("UU_ASSIST_ALLOW").ok().as_deref() == Some("1")
|| config.get("assist_allow").and_then(Value::as_bool) == Some(true);
Ok((custom, verify, refresh, allowed))
}
fn lock() -> Result<std::fs::File, String> {
let lock_path = crate::config::path().with_file_name("assist.lock");
if let Some(dir) = lock_path.parent() { std::fs::create_dir_all(dir).map_err(|_| "assist config directory unavailable")?; }
let lock = std::fs::OpenOptions::new().create(true).write(true).mode(0o600).custom_flags(libc::O_NOFOLLOW).open(lock_path)
.map_err(|_| "assist lock unavailable")?;
if unsafe { libc::flock(lock.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB) } != 0 { return Err("another assist host uses this config".into()); }
Ok(lock)
}
/// Remote assistance on a logged-in host, shown in the service's hidden window.
pub fn start_account(client: std::sync::Arc<UUAuthClient>, device: Credentials) -> Result<Self, String> {
let lock = Self::lock()?;
let ui = std::sync::Arc::new(LocalUi::open_service()?);
let config = crate::config::read_fresh()?.unwrap_or_else(|| json!({}));
let (custom, verify, refresh, allowed) = Self::settings(&config)?;
let this = Self { _lock: lock, renewed: false, device, client, ui, code: generate_code()?, custom, id: String::new(),
allowed, verify, refresh, code_at: std::time::Instant::now(), account: true };
this.show()?;
Ok(this)
}
pub fn is_account(&self) -> bool { self.account }
/// Logged-in host: account name for the account card (saved at login, else /api/v1/user/info).
pub async fn load_account_name(&self) {
let saved = crate::config::read_fresh().ok().flatten()
.and_then(|c| c.get("account_name").and_then(Value::as_str).map(str::to_owned)).filter(|n| !n.is_empty());
let name = match saved {
Some(name) => name,
None => match self.client.assist_get("/api/v1/user/info").await {
Ok((200, v)) => v.get("data").and_then(|d| ["nickname", "mobile", "phone"].iter()
.find_map(|k| d.get(*k).and_then(Value::as_str).filter(|s| !s.is_empty()).map(str::to_owned)))
.unwrap_or_else(|| self.device.user_id.clone()),
_ => self.device.user_id.clone(),
},
};
self.ui.set_user(&display_account(&name));
}
/// Sign out: official /api/v1/user/logout (best effort), withdraw the share, drop local credentials.
pub async fn logout(&mut self) {
let _ = self.client.assist_post("/api/v1/user/logout", None).await;
let _ = self.cancel().await;
self.save_many(&[("token", Value::Null), ("csrf", Value::Null), ("user_id", Value::Null), ("account_name", Value::Null)]);
log::info!("Signed out; restarting without an account");
}
pub fn ui(&self) -> std::sync::Arc<LocalUi> { self.ui.clone() }
fn path(&self, guest: &'static str) -> &'static str {
if !self.account { return guest; }
match guest { INFO => ACCOUNT_INFO, CANCEL => ACCOUNT_CANCEL, SIGN => ACCOUNT_SIGN, CONTROL_MODE => ACCOUNT_CONTROL_MODE, other => other }
}
/// Logged-in host: publish the share ID/code once its own room exists (same ordering as guests).
pub async fn ensure_published(&mut self) -> Result<(), String> {
if self.id.is_empty() { self.publish().await } else { Ok(()) }
}
async fn authenticate(device: &Credentials) -> Result<UUAuthClient, String> {
let client = UUAuthClient::new(device.clone());
let (status, value) = client.assist_post(CREATE, None).await?;
let data = response(status, &value)?;
Ok(UUAuthClient::new_guest(device.clone(), text(data, "guest_id")?, text(data, "token")?))
}
async fn post(&mut self, path: &'static str, body: Option<&Value>) -> Result<Value, String> {
let path = self.path(path);
let (mut status, mut value) = if path == ACCOUNT_INFO { self.client.assist_get(path).await? } else { self.client.assist_post(path, body).await? };
if self.account { return response(status, &value).map(Clone::clone); }
// Official wrapper refreshes on 1120 and retries once, rather than on a guessed TTL.
if value.get("code").and_then(Value::as_i64) == Some(crate::auth::TOKEN_INVALID) {
self.client = std::sync::Arc::new(Self::authenticate(&self.device).await?);
self.renewed = true;
(status, value) = self.client.assist_post(path, body).await?;
}
response(status, &value).map(Clone::clone)
}
async fn publish(&mut self) -> Result<(), String> {
self.id = share_id(&self.post(INFO, None).await?)?;
self.show()
}
pub async fn room(&mut self) -> Result<RoomData, String> {
let value = self.post(ROOM, None).await?;
if self.renewed || self.id.is_empty() {
if let Err(error) = self.publish().await { let _ = self.cancel().await; return Err(error); }
self.renewed = false;
}
serde_json::from_value(value).map_err(|_| "assist room response malformed".into())
}
pub async fn challenge(&mut self, value: &Value) -> Result<(), String> {
let (control, salt) = challenge(value).ok_or("assist challenge malformed")?;
self.post(SIGN, Some(&sign_body(control, salt, &self.code, self.custom.as_deref(), self.verify, self.allowed))).await?;
Ok(())
}
/// Answer the cloud's `get_control_mode` push (data {control_id}). Official Windows host
/// (GameViewerServer RemoteAssistCode::respRemoteGetControlMode -> NrdBusinessEntry::uploadControlMode)
/// posts {control_id, allow_control, control_mode: by_password|by_confirmation|password_confirmation}.
/// Only the temporary/custom code mode is implemented, so this host reports `by_password`.
pub async fn control_mode(&mut self, value: &Value) -> Result<(), String> {
let control = value.get("control_id").and_then(Value::as_str).filter(|s| !s.is_empty() && s.len() <= 256)
.ok_or("assist control mode request malformed")?;
self.post(CONTROL_MODE, Some(&json!({"control_id": control, "allow_control": self.allowed, "control_mode": "by_password"}))).await.map(|_| ())
}
/// The code the controller types: the custom code in custom-only mode, else the temporary one.
fn show(&self) -> Result<(), String> {
let code = match (self.verify, &self.custom) { (Verify::Custom, Some(c)) => c.as_str(), _ => self.code.as_str() };
self.ui.show(&self.id, code, self.allowed, self.verify, self.refresh, self.custom.is_some())
}
/// Saves one assist setting into the owner's 0600 config (custom code included, never logged).
fn save_many(&self, entries: &[(&str, Value)]) {
let mut cfg = std::fs::read(crate::config::path()).ok().and_then(|b| serde_json::from_slice::<Value>(&b).ok())
.filter(Value::is_object).unwrap_or_else(|| json!({}));
if let Some(map) = cfg.as_object_mut() { for (k, v) in entries { if v.is_null() { map.remove(*k); } else { map.insert((*k).to_owned(), v.clone()); } } }
if crate::login::write_config(&cfg).is_err() { log::warn!("Account settings could not be saved"); }
}
fn save(&self, key: &str, value: Value) {
// Re-read the file: config::file() is a startup snapshot that predates device registration,
// so writing it back would drop device_id (and earlier saves) and change the device ID.
let mut cfg = std::fs::read(crate::config::path()).ok().and_then(|b| serde_json::from_slice::<Value>(&b).ok())
.filter(Value::is_object).unwrap_or_else(|| json!({}));
cfg[key] = value;
if crate::login::write_config(&cfg).is_err() { log::warn!("Assist setting could not be saved: key={key}"); }
}
pub fn reset(&mut self) -> Result<(), String> { self.code = generate_code()?; self.code_at = std::time::Instant::now(); self.show() }
pub fn allow(&mut self) -> Result<(), String> { self.allowed = true; self.save("assist_allow", json!(true)); self.show() }
pub fn deny(&mut self) -> Result<(), String> { self.allowed = false; self.save("assist_allow", json!(false)); self.show() }
pub fn set_verify(&mut self, verify: Verify) -> Result<(), String> {
if verify != Verify::Temporary && self.custom.is_none() { return self.show(); } // UI asks for a code first
self.verify = verify; self.save("assist_verify", json!(verify.name())); self.show()
}
pub fn set_refresh(&mut self, refresh: Refresh) -> Result<(), String> {
self.refresh = refresh; self.code_at = std::time::Instant::now(); self.save("assist_refresh", json!(refresh.name())); self.show()
}
pub fn set_custom(&mut self, code: String) -> Result<(), String> {
if !valid_custom(&code) { return self.show(); }
self.custom = Some(code.clone()); self.save("assist_custom_code", json!(code));
if self.verify == Verify::Temporary { self.verify = Verify::Custom; self.save("assist_verify", json!(self.verify.name())); }
self.show()
}
/// After the last desktop session: rotate only in "每次远控结束" mode.
pub fn session_ended(&mut self) -> Result<(), String> { if self.refresh == Refresh::Session { self.reset() } else { Ok(()) } }
/// Periodic rotation for the hourly/12 h/24 h settings.
pub fn tick(&mut self) -> Result<(), String> {
match self.refresh.period() { Some(p) if self.code_at.elapsed() >= p => self.reset(), _ => Ok(()) }
}
pub fn commands(&self, shutdown: tokio::sync::watch::Sender<bool>) -> (tokio::sync::mpsc::Receiver<Command>, tokio::task::JoinHandle<()>) {
let (tx, rx) = tokio::sync::mpsc::channel(4);
let ui = self.ui.clone();
let task = tokio::spawn(async move {
loop {
let command = tokio::select! { command = ui.command() => command, _ = tx.closed() => return };
if matches!(command, Command::Stop) { let _ = shutdown.send(true); return; }
match tx.try_send(command) {
Ok(()) | Err(tokio::sync::mpsc::error::TrySendError::Full(_)) => {},
Err(tokio::sync::mpsc::error::TrySendError::Closed(_)) => return,
}
}
});
(rx, task)
}
pub async fn cancel(&mut self) -> Result<(), String> { self.post(CANCEL, None).await.map(|_| ()) }
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn local_terminal_display_and_explicit_commands() {
// Private PTY pair only, never the user's actual terminal; all values synthetic.
let pair = nix::pty::openpty(None, None).unwrap();
let slave = std::fs::File::from(pair.slave);
unsafe { libc::fcntl(slave.as_raw_fd(), libc::F_SETFL, libc::O_NONBLOCK); }
let ui = LocalUi::Tty(tokio::io::unix::AsyncFd::new(slave).unwrap());
let mut master = std::fs::File::from(pair.master);
ui.show("fixture-123", "FIXTURE8", false, Verify::Temporary, Refresh::Session, false).unwrap();
// A PTY may deliver the banner in several chunks: read until complete (bounded).
let mut text = String::new();
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(3);
let complete = |t: &str| t.contains("fixture-123") && t.contains("FIXTURE8") && t.contains("false") && t.contains("off=");
while !complete(&text) && std::time::Instant::now() < deadline {
let mut bytes = [0u8; 1024];
match master.read(&mut bytes) { Ok(n) if n > 0 => text.push_str(&String::from_utf8_lossy(&bytes[..n])), _ => break }
}
assert!(complete(&text));
for (input, expected) in [(b"unknown\nallow\n".as_slice(), 0), (b"reset\n".as_slice(), 1), (b"off\n".as_slice(), 2)] {
master.write_all(input).unwrap();
let command = tokio::time::timeout(std::time::Duration::from_secs(2), ui.command()).await.unwrap();
assert!(matches!((command, expected), (Command::Allow, 0) | (Command::Reset, 1) | (Command::Stop, 2)));
}
}
#[test]
fn official_challenge_sign_and_shape() {
// Synthetic fixture, independently calculated SHA256("saltABC234XY").
let body = sign_body("fixture-control", "salt", "ABC234XY", None, Verify::Temporary, false);
assert_eq!(body.as_object().unwrap().len(), 4);
assert_eq!(body["can_remote_control"], false);
assert_eq!(body["control_id"], "fixture-control");
assert_eq!(body["sign"], "dc747ee8adb0e591f5ff4ab6cabcaf3a0ec312851580f225d4ca140766229c94");
assert_eq!(body["backup_sign"], "");
let both = sign_body("fixture", "salt", "ABC234XY", Some("test1234"), Verify::Both, true);
assert_ne!(both["sign"], both["backup_sign"]);
assert_eq!(both["can_remote_control"], true);
}
#[test]
fn rejects_missing_or_unbounded_challenges_and_terminal_injection() {
for value in [json!({}), json!({"control_id":"x", "salt":""}), json!({"control_id":12,"salt":"s"}), json!({"control_id":"x","salt":"s".repeat(4097)})] {
assert!(challenge(&value).is_none());
}
assert!(challenge(&json!({"control_id":"fixture", "salt":"fixture"})).is_some());
assert!(share_id(&json!({"connect_id":"fixture-123"})).is_ok());
for id in ["", "\u{1b}[2J", "a\nb", "x\u{7}"] { assert!(share_id(&json!({"connect_id":id})).is_err()); }
}
#[test]
fn responses_do_not_disclose_untrusted_server_errors() {
let error = response(422, &json!({"code":1120,"msg":"synthetic-secret","data":{"token":"synthetic-secret"}})).unwrap_err();
assert!(!error.contains("synthetic-secret"));
assert!(response(200, &json!({"code":0})).unwrap().is_null());
assert!(text(&Value::Null, "token").is_err());
assert_eq!(text(&json!({"guest_id":"fixture-guest"}), "guest_id").unwrap(), "fixture-guest");
}
#[test]
fn official_guest_failure_retry_schedule() {
let mut retry = RetryBackoff::default();
let actual: Vec<_> = (0..9).map(|_| retry.next().as_secs()).collect();
assert_eq!(actual, [15,30,45,60,75,90,99,99,99]);
}
#[test]
fn secure_temporary_code_format() {
for _ in 0..64 { let code = generate_code().unwrap(); assert_eq!(code.len(),8); assert!(code.bytes().all(|b| ALPHABET.contains(&b))); }
}
}
+122 -6
View File
@@ -73,7 +73,7 @@ pub struct ApiResponse<T> {
pub data: Option<T>,
}
#[derive(Debug, Clone, Deserialize)]
#[derive(Clone, Deserialize)]
pub struct RoomData {
pub signaling_server: String,
pub token: String,
@@ -81,9 +81,44 @@ pub struct RoomData {
pub ws_connect_timeout_ms: Option<u64>,
}
impl std::fmt::Debug for RoomData {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_str("RoomData { .. }") }
}
pub struct UUAuthClient {
pub creds: Credentials,
http: reqwest::Client,
guest_id: Option<String>,
}
/// Transport failures name only the outer request; the cause (certificate, proxy, DNS,
/// timeout) lives in the source chain. URL user-info (proxy credentials) and query
/// strings are removed before the chain is shown.
pub fn transport_error(error: &(dyn std::error::Error + 'static)) -> String {
let mut parts = vec![error.to_string()];
let mut source = error.source();
while let Some(cause) = source {
let text = cause.to_string();
if parts.last() != Some(&text) { parts.push(text); }
source = cause.source();
if parts.len() >= 8 { break; }
}
redact_urls(&parts.join(": "))
}
fn redact_urls(text: &str) -> String {
let mut out = String::with_capacity(text.len());
let mut rest = text;
while let Some(i) = rest.find("://") {
let (head, tail) = rest.split_at(i + 3);
out.push_str(head);
let end = tail.find(|c: char| c.is_whitespace() || c == ')' || c == '"' || c == '\'').unwrap_or(tail.len());
let url = &tail[..end];
let url = url.rsplit_once('@').map_or(url, |(_, host)| host);
out.push_str(url.split(['?', '#']).next().unwrap_or(""));
rest = &tail[end..];
}
out.push_str(rest);
out
}
impl UUAuthClient {
@@ -92,7 +127,16 @@ impl UUAuthClient {
.timeout(std::time::Duration::from_secs(12))
.build()
.unwrap();
Self { creds, http }
Self { creds, http, guest_id: None }
}
/// Explicit guest identity: never inherits account Authorization/cookies.
/// Official ApiDefaultHeaders 0x1003853ac/0x1003853f8 (see assist evidence).
pub fn new_guest(mut device: Credentials, guest_id: String, token: String) -> Self {
device.user_id.clear(); device.csrf.clear(); device.token = token;
let mut client = Self::new(device);
client.guest_id = Some(guest_id);
client
}
/// 计算网易 NRD HMAC-SHA256 签名
@@ -147,6 +191,7 @@ impl UUAuthClient {
("x-param-system-id", &self.creds.system_id), ("x-param-user-id", &self.creds.user_id)] {
if !value.is_empty() { param_map.insert(key.into(), value.clone()); }
}
if let Some(id) = &self.guest_id { param_map.insert("x-param-guest-id".into(), id.clone()); }
param_map.insert("x-param-ts".into(), ts);
let sign = self.sign_request(method, path, &param_map, body_str);
@@ -160,10 +205,12 @@ impl UUAuthClient {
);
if !self.creds.token.is_empty() {
if let Ok(v) = HeaderValue::from_str(&format!("Bearer {}", self.creds.token)) { headers.insert("Authorization", v); }
if let Ok(v) = HeaderValue::from_str(&format!("nrd_access_token={}; nrd_csrf_access_token={}", self.creds.token, self.creds.csrf)) {
headers.insert("Cookie", v);
if self.guest_id.is_none() {
if let Ok(v) = HeaderValue::from_str(&format!("nrd_access_token={}; nrd_csrf_access_token={}", self.creds.token, self.creds.csrf)) {
headers.insert("Cookie", v);
}
if let Ok(v) = HeaderValue::from_str(&self.creds.csrf) { headers.insert("X-CSRF-TOKEN", v); }
}
if let Ok(v) = HeaderValue::from_str(&self.creds.csrf) { headers.insert("X-CSRF-TOKEN", v); }
}
for (k, v) in param_map {
@@ -183,12 +230,40 @@ impl UUAuthClient {
let body_str = serde_json::to_string(body).map_err(|e| e.to_string())?;
let resp = self.http.post(format!("{}{}", BASE_URL, path))
.headers(self.build_headers("POST", path, &body_str)).body(body_str)
.send().await.map_err(|e| format!("HTTP post {path} failed: {e}"))?;
.send().await.map_err(|e| format!("HTTP post {path} failed: {}", transport_error(&e)))?;
let status = resp.status().as_u16();
let value = resp.json().await.unwrap_or(serde_json::Value::Null);
Ok((status, value))
}
/// Guest APIs use requestPlain for empty requests; sign the actual empty body.
/// Errors intentionally exclude response text and URL (both can contain secrets).
pub async fn assist_post(&self, path: &str, body: Option<&serde_json::Value>) -> Result<(u16, serde_json::Value), String> {
self.assist_send(self.assist_request(path, body)?).await
}
/// Account share info is a GET (POST answers 405, verified live).
pub async fn assist_get(&self, path: &str) -> Result<(u16, serde_json::Value), String> {
let request = self.http.get(format!("{BASE_URL}{path}")).headers(self.build_headers("GET", path, "")).build()
.map_err(|_| "assist request construction failed")?;
self.assist_send(request).await
}
async fn assist_send(&self, request: reqwest::Request) -> Result<(u16, serde_json::Value), String> {
let response = self.http.execute(request)
.await.map_err(|e| format!("assist request transport failed: {}", transport_error(&e)))?;
let status = response.status().as_u16();
let bytes = response.bytes().await.map_err(|_| "assist response read failed")?;
if bytes.len() > 65536 { return Err("assist response too large".into()); }
let value = serde_json::from_slice(&bytes).map_err(|_| "assist response malformed")?;
Ok((status, value))
}
fn assist_request(&self, path: &str, body: Option<&serde_json::Value>) -> Result<reqwest::Request, String> {
let body = body.map(serde_json::to_string).transpose().map_err(|_| "assist request encoding failed")?.unwrap_or_default();
self.http.post(format!("{BASE_URL}{path}"))
.headers(self.build_headers("POST", path, &body)).body(body).build()
.map_err(|_| "assist request construction failed".into())
}
/// 向网易服务器请求创建房间,获取最新的信令服务器与鉴权 Token
pub async fn create_room(&self) -> Result<RoomData, RoomError> {
let (status, body) = self.post_json("/api/v1/room/create", &serde_json::json!({})).await.map_err(RoomError::Other)?;
@@ -231,3 +306,44 @@ mod room_lifetime_tests {
assert_eq!(room_token_lifetime("h.invalid.s",1000),None);
}
}
#[cfg(test)]
mod guest_tests {
use super::*;
#[test]
fn official_request_plain_body_and_sanitized_room_debug() {
let client = UUAuthClient::new_guest(Credentials { sign_key: "fixture-key".into(), ..Default::default() },
"fixture-guest".into(), "fixture-token".into());
let request = client.assist_request("/api/v1/guest/create", None).unwrap();
assert_eq!(request.method(), reqwest::Method::POST);
assert_eq!(request.body().unwrap().as_bytes(), Some(&b""[..]));
let other = client.assist_request("/api/v1/guest/create", Some(&serde_json::json!({}))).unwrap();
assert_eq!(other.body().unwrap().as_bytes(), Some(&b"{}"[..]));
assert_ne!(request.headers()["x-param-sign"], other.headers()["x-param-sign"]);
let room: RoomData = serde_json::from_value(serde_json::json!({"signaling_server":"wss://fixture.invalid", "token":"synthetic-room-secret"})).unwrap();
assert!(!format!("{room:?}").contains("synthetic-room-secret"));
assert!(serde_json::from_value::<RoomData>(serde_json::json!({"token":42})).is_err());
}
#[test]
fn guest_signs_guest_id_and_never_inherits_account_identity() {
let account = Credentials { user_id: "fixture-account".into(), token: "fixture-account-token".into(),
csrf: "fixture-account-csrf".into(), client_id: "fixture-client".into(),
device_id: "fixture-device".into(), sign_key: "fixture-sign-key".into(), ..Default::default() };
let client = UUAuthClient::new_guest(account, "fixture-guest".into(), "fixture-guest-token".into());
let headers = client.build_headers("POST", "/api/v1/guest/room/create", "");
assert_eq!(headers["authorization"], "Bearer fixture-guest-token");
assert_eq!(headers["x-param-guest-id"], "fixture-guest");
for key in ["x-param-user-id", "cookie", "x-csrf-token"] { assert!(!headers.contains_key(key)); }
let params: BTreeMap<String,String> = headers.iter().filter_map(|(key, value)|
key.as_str().starts_with("x-param-").then(|| (key.to_string(), value.to_str().unwrap().to_owned()))).collect();
assert_eq!(headers["x-param-sign"], client.sign_request("POST", "/api/v1/guest/room/create", &params, ""));
let mut without_guest = params; without_guest.remove("x-param-guest-id");
assert_ne!(headers["x-param-sign"], client.sign_request("POST", "/api/v1/guest/room/create", &without_guest, ""));
}
#[test]
fn transport_error_redacts_proxy_credentials_and_queries() {
let text = super::redact_urls("proxy http://user:[email protected]:3128 failed for (https://api.example/p?token=abc)");
assert_eq!(text, "proxy http://proxy.example:3128 failed for (https://api.example/p)");
}
}
+18 -6
View File
@@ -27,17 +27,20 @@ pub fn h264_device_capability(
fps: i32,
codec_impl: i32,
) -> Value {
device_capability(client_id, ice_id, display_id, fps, codec_impl, false)
}
pub fn device_capability(client_id: &str, ice_id: &str, display_id: i32, fps: i32, codec_impl: i32, hevc: bool) -> Value {
let mut codecs = vec![json!({"video_codec": VIDEO_CODEC_H264, "chroma_sampling": 1,
"codec_impl": codec_impl, "bit_depth": 8})];
// Official VideoCodecCapability::SerializeJson uses the same fields for CodecType H265=2.
if hevc { codecs.push(json!({"video_codec": 2, "chroma_sampling": 1, "codec_impl": codec_impl, "bit_depth": 8})); }
json!({
"client_id": client_id,
"type": "device_capability",
"data": {
"display_info": [{"id": display_id, "fps": fps, "type": 0, "hdr": 0}],
"video_codec_capability": [{
"video_codec": VIDEO_CODEC_H264,
"chroma_sampling": 1,
"codec_impl": codec_impl,
"bit_depth": 8
}],
"video_codec_capability": codecs,
"ice_id": ice_id
}
})
@@ -47,6 +50,15 @@ pub fn h264_device_capability(
mod tests {
use super::*;
#[test]
fn hevc_capability_uses_official_codec_enum_and_420_only() {
let payload = device_capability("client-example", "ice-example", 0, 60, 5, true);
assert_eq!(payload["data"]["video_codec_capability"], json!([
{"video_codec":1,"chroma_sampling":1,"codec_impl":5,"bit_depth":8},
{"video_codec":2,"chroma_sampling":1,"codec_impl":5,"bit_depth":8}]));
assert_eq!(device_capability("c", "i", 0, 60, 5, false)["data"]["video_codec_capability"].as_array().unwrap().len(), 1);
}
#[test]
fn ice_finished_keeps_string_identifiers_and_exact_fields() {
let payload = ice_finished("ice-example", "control-example");
+19 -5
View File
@@ -6,6 +6,7 @@ use std::sync::Mutex;
extern "C" {
fn x11_init(display_name: *const libc::c_char) -> libc::c_int;
fn x11_get_dimensions(out_w: *mut libc::c_int, out_h: *mut libc::c_int) -> libc::c_int;
fn x11_refresh_geometry() -> libc::c_int;
fn x11_capture_frame(
out_buffer: *mut libc::c_uchar,
out_w: *mut libc::c_int,
@@ -37,10 +38,22 @@ unsafe fn x11_capture_frame(
}
#[cfg(not(target_os = "linux"))]
unsafe fn x11_close() {}
#[cfg(not(target_os = "linux"))]
unsafe fn x11_refresh_geometry() -> libc::c_int { 0 }
#[cfg(not(target_os = "linux"))]
unsafe fn x11_display_name() -> *const libc::c_char { b":0\0".as_ptr().cast() }
/// The X11 root can be resized while the host runs (RandR, xrdp reconnects); the
/// geometry read at startup then makes x11grab reject every new connection.
pub fn x11_resized() -> bool {
match unsafe { x11_refresh_geometry() } {
1 => true,
0 => false,
code => { log::warn!("X11 geometry query failed with code {code}"); false }
}
}
pub struct ScreenCapture {
pub backend: crate::desktop_backend::DesktopBackend,
pub init_error: Option<String>,
@@ -59,18 +72,18 @@ impl ScreenCapture {
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) });
buffer:Mutex::new(Vec::new()),kms:Some(kms) });
}
if backend == crate::desktop_backend::DesktopBackend::Portal {
let session=std::sync::Arc::new(crate::portal::PortalSession::open()?);
let (width,height)=(session.width,session.height);
return Ok(Self { backend,init_error:None,portal:Some(session),kms:None,display:"wayland:portal".into(),width,height,
buffer:Mutex::new(vec![0;(width as usize)*(height as usize)*4]) });
buffer:Mutex::new(Vec::new()) });
}
if backend == crate::desktop_backend::DesktopBackend::Wayland {
let (width,height)=crate::wayland::init()?;
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::new()) });
}
let ret = unsafe { x11_init(std::ptr::null()) };
if ret != 0 {
@@ -85,8 +98,7 @@ impl ScreenCapture {
return Err(format!("Invalid screen dimensions: {}x{}", w, h));
}
let buf_size = (w * h * 4) as usize;
let buffer = Mutex::new(vec![0u8; buf_size]);
let buffer = Mutex::new(Vec::new());
log::info!("ScreenCapture initialized: {}x{}", w, h);
let display_ptr = unsafe { x11_display_name() };
@@ -115,6 +127,7 @@ impl ScreenCapture {
pub fn capture_frame(&self) -> Result<Vec<u8>, String> {
if let Some(error)=&self.init_error { return Err(error.clone()); }
let mut buf = self.buffer.lock().unwrap();
buf.resize(self.width as usize * self.height as usize * 4, 0);
if let Some(kms)=&self.kms { kms.capture(&mut buf)?; return Ok(buf.clone()); }
if let Some(session)=&self.portal { session.capture(&mut buf)?; return Ok(buf.clone()); }
if self.backend == crate::desktop_backend::DesktopBackend::Wayland {
@@ -126,6 +139,7 @@ impl ScreenCapture {
let ret = unsafe { x11_capture_frame(buf.as_mut_ptr(), &mut w, &mut h) };
if ret != 0 {
if ret == -4 { return Err(format!("desktop resized to {w}x{h}; reconnect to capture the new size")); }
return Err(format!("x11_capture_frame failed with code {}", ret));
}
+111 -5
View File
@@ -19,24 +19,40 @@ pub const QUALITY_AUTO: u64 = 5;
pub const QUALITY_CUSTOM: u64 = 6;
/// FeatureFlag.ff_capture_setting we declare: fps, preset quality and custom bitrate.
/// (Chroma 4:4:4, DPI scaling and HDR are higher versions and are not implemented.)
/// Versions are cumulative (official FF_CaptureSetting enum: 3 CHROMA_SAMPLING, 4 CUSTOM_BITRATE,
/// 5 DPI_SCALE, 6 HDR), so 4 also tells the viewer we accept a chroma format (真彩).
pub const FEATURE_VERSION: u64 = 4;
/// Official GvPb_ChromaFormat values (CaptureSettingRequest field 7, CaptureSettingSync field 3).
pub const CHROMA_420: u64 = 1;
pub const CHROMA_444: u64 = 3;
const MIN_FPS: u32 = 5;
const MAX_FPS: u32 = 144;
/// Official GvPb_CodecType, CaptureSettingRequest.codec_type (field 13).
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub enum VideoCodec { #[default] H264, H265 }
impl VideoCodec {
pub fn wire(self) -> u64 { match self { Self::H264 => 1, Self::H265 => 2 } }
pub fn mime(self) -> &'static str { match self { Self::H264 => "video/H264", Self::H265 => "video/H265" } }
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct VideoSettings {
pub fps: u32,
pub codec: VideoCodec,
pub quality: u64,
/// Viewer max_custom_bitrate (only used for QUALITY_CUSTOM): Mbit/s, or kbit/s above 1000.
pub custom_mbps: u32,
/// Scaled stream size (其他分辨率); None streams the native desktop size.
pub output: Option<(u32, u32)>,
/// CHROMA_420 or CHROMA_444 (真彩); 4:4:4 only when the encoder pipeline supports it.
pub chroma: u64,
}
impl Default for VideoSettings {
fn default() -> Self { Self { fps: 60, quality: QUALITY_AUTO, custom_mbps: 0, output: None } }
fn default() -> Self { Self { codec: VideoCodec::H264, fps: 60, quality: QUALITY_AUTO, custom_mbps: 0, output: None, chroma: CHROMA_420 } }
}
/// Selectable smaller stream sizes with the desktop's aspect ratio (even dimensions).
@@ -95,6 +111,21 @@ fn fps_from_option(value: u64) -> Option<u64> {
/// Applies a capture-setting RPC to `settings` and returns the RpcResponse payload (Message 22),
/// or None when the RPC is not a capture/fps/quality setting.
pub fn handle_rpc(rpc: &[WireField], settings: &watch::Sender<VideoSettings>, native: (u32, u32)) -> Option<Vec<u8>> {
handle_rpc_with_codecs(rpc, settings, native, true, false)
}
/// Initial codec once the m-line is negotiated: H.265 when both sides support it (the Mac viewer
/// lists H265 first in its offer), unless UU_VIDEO_CODEC=h264.
pub fn preferred_codec(h264: bool, h265: bool) -> VideoCodec {
let forced_h264 = std::env::var("UU_VIDEO_CODEC").is_ok_and(|v| v.eq_ignore_ascii_case("h264"));
if h265 && !(forced_h264 && h264) { VideoCodec::H265 } else { VideoCodec::H264 }
}
pub fn handle_rpc_with_codecs(rpc: &[WireField], settings: &watch::Sender<VideoSettings>, native: (u32, u32), h264: bool, h265: bool) -> Option<Vec<u8>> {
apply_rpc(rpc, settings, native, h264, h265, crate::video::full_chroma_available())
}
fn apply_rpc(rpc: &[WireField], settings: &watch::Sender<VideoSettings>, native: (u32, u32), h264: bool, h265: bool, full_chroma: bool) -> Option<Vec<u8>> {
let header = message(rpc, 1).unwrap_or(&[]);
let mut next = *settings.borrow();
if let Some(request) = message(rpc, 2) {
@@ -102,10 +133,21 @@ pub fn handle_rpc(rpc: &[WireField], settings: &watch::Sender<VideoSettings>, na
// Numbers only: fps, quality, bitrate, resolution and flags carry no user content.
log::info!("Capture setting request: {:?}", fields.iter().filter_map(|f| match f.value {
WireValue::Varint(v) => Some((f.number, v)), _ => None }).collect::<Vec<_>>());
// codec_type 13 (1 H264, 2 H265): like the official host (negotiateCodec), the host picks the
// codec from both capability lists; the Mac viewer sends its current codec (1 while receiving
// H.264), so only an explicit H.265 request switches, never one absent from the m-line.
if number(&fields, 13) == Some(2) && h265 { next.codec = VideoCodec::H265; }
// Field 1 is the viewer's fps option index (1 = 30, 2 = 60, ...); 18 fpsCount is the frame rate.
if let Some(fps) = number(&fields, 18).filter(|v| *v > 0).or_else(|| number(&fields, 1).and_then(fps_from_option)) { next.fps = fps as u32; }
if let Some(quality) = number(&fields, 2).filter(|v| *v > 0) { next.quality = quality; }
if let Some(bitrate) = number(&fields, 8).filter(|v| *v > 0) { next.custom_mbps = bitrate as u32; }
// Field 7 chorma_format (sic): 3 = 4:4:4 when 真彩 is on, 1 = 4:2:0. The Sync reports what is encoded.
match number(&fields, 7) {
Some(CHROMA_444) if full_chroma => next.chroma = CHROMA_444,
Some(CHROMA_444) => { log::info!("Chroma 4:4:4 requested; this encoder pipeline only supports 4:2:0"); next.chroma = CHROMA_420; }
Some(CHROMA_420) => next.chroma = CHROMA_420,
_ => {}
}
// ChooseResolutionType 3 = follow remote (native); otherwise pixel size, else logical size.
if number(&fields, 10) == Some(3) { next.output = None; }
else if let Some((w, h)) = number(&fields, 16).zip(number(&fields, 17)).or_else(|| number(&fields, 5).zip(number(&fields, 6))) {
@@ -129,7 +171,7 @@ pub fn handle_rpc(rpc: &[WireField], settings: &watch::Sender<VideoSettings>, na
}
next.fps = next.fps.clamp(MIN_FPS, MAX_FPS);
settings.send_if_modified(|current| if *current != next {
log::info!("Desktop video settings: fps={} quality={} max_kbps={:?} output={:?}", next.fps, next.quality, next.max_kbps(), next.output);
log::info!("Desktop video settings: fps={} quality={} max_kbps={:?} output={:?} chroma={}", next.fps, next.quality, next.max_kbps(), next.output, next.chroma);
*current = next; true
} else { false });
let mut out = Vec::new();
@@ -156,7 +198,7 @@ pub fn sync_payload(settings: &VideoSettings, native: (u32, u32)) -> Vec<u8> {
let mut out = Vec::new();
put_varint(1, u64::from(settings.fps), &mut out);
put_varint(2, settings.quality, &mut out);
put_varint(3, 1, &mut out); // ChromaFormat_420
put_varint(3, settings.chroma, &mut out);
if settings.custom_mbps > 0 { put_varint(4, u64::from(settings.custom_mbps), &mut out); }
put_varint(5, u64::from(size.0), &mut out);
put_varint(6, u64::from(size.1), &mut out);
@@ -165,6 +207,23 @@ pub fn sync_payload(settings: &VideoSettings, native: (u32, u32)) -> Vec<u8> {
out
}
/// Message 18 CodecNegotiation {1 encoder_cap_list: EncoderCap {1 capture_mode (1 DESKTOP), 2 fps,
/// 3 codec_type, 4 width, 5 height}} (official Windows host descriptor). The viewer's own list is
/// ConnectOptions field 4 decoder_cap_list; it only selects a codec the host announces here.
pub fn codec_negotiation(size: (u32, u32), fps: u32, hevc: bool) -> Vec<u8> {
let mut out = Vec::new();
for codec in [Some(VideoCodec::H264), hevc.then_some(VideoCodec::H265)].into_iter().flatten() {
let mut cap = Vec::new();
put_varint(1, 1, &mut cap);
put_varint(2, u64::from(fps), &mut cap);
put_varint(3, codec.wire(), &mut cap);
put_varint(4, u64::from(size.0), &mut cap);
put_varint(5, u64::from(size.1), &mut cap);
put_bytes(1, &cap, &mut out);
}
out
}
#[cfg(test)]
mod tests {
use super::*;
@@ -187,7 +246,7 @@ mod tests {
put_varint(18, 30, &mut body);
let request = rpc(77, 2, &body);
let response = handle_rpc(&decode_wire(&request).unwrap(), &tx, (1920, 1080)).unwrap();
assert_eq!(*rx.borrow(), VideoSettings { fps: 30, quality: QUALITY_GENERAL, custom_mbps: 0, output: None });
assert_eq!(*rx.borrow(), VideoSettings { codec: VideoCodec::H264, fps: 30, quality: QUALITY_GENERAL, custom_mbps: 0, output: None, chroma: CHROMA_420 });
assert_eq!(rx.borrow().max_kbps(), Some(2_000));
let fields = decode_wire(&response).unwrap();
assert_eq!(message(&fields, 1), Some(&[8u8, 77][..]));
@@ -208,6 +267,29 @@ mod tests {
assert!(handle_rpc(&decode_wire(&rpc(3, 9, &[])).unwrap(), &tx, (1920, 1080)).is_none());
}
#[test]
fn true_color_request_selects_444_only_when_the_encoder_supports_it() {
let (tx, rx) = watch::channel(VideoSettings::default());
// Observed Mac viewer request with 真彩 on: field 7 = 3.
let request = rpc(5, 2, &[8, 2, 16, 4, 56, 3, 144, 1, 60]);
apply_rpc(&decode_wire(&request).unwrap(), &tx, (1920, 1080), true, false, false).unwrap();
assert_eq!(rx.borrow().chroma, CHROMA_420);
apply_rpc(&decode_wire(&request).unwrap(), &tx, (1920, 1080), true, false, true).unwrap();
assert_eq!(rx.borrow().chroma, CHROMA_444);
assert_eq!(number(&decode_wire(&sync_payload(&rx.borrow(), (1920, 1080))).unwrap(), 3), Some(CHROMA_444));
apply_rpc(&decode_wire(&rpc(6, 2, &[56, 1])).unwrap(), &tx, (1920, 1080), true, false, true).unwrap();
assert_eq!(rx.borrow().chroma, CHROMA_420);
}
#[test]
fn codec_negotiation_lists_h264_then_optional_h265() {
assert_eq!(codec_negotiation((1920, 1080), 60, false), [10, 12, 8, 1, 16, 60, 24, 1, 32, 128, 15, 40, 184, 8]);
let both = codec_negotiation((1920, 1080), 60, true);
let caps: Vec<_> = decode_wire(&both).unwrap().iter().filter_map(|f| match f.value { WireValue::Bytes(b) => Some(b), _ => None })
.map(|b| number(&decode_wire(b).unwrap(), 3)).collect();
assert_eq!(caps, [Some(1), Some(2)]);
}
#[test]
fn resolution_requests_scale_below_native_only() {
let (tx, rx) = watch::channel(VideoSettings::default());
@@ -235,3 +317,27 @@ mod tests {
assert_eq!(number(&fields, 10), Some(1080));
}
}
#[cfg(test)]
mod codec_tests {
use super::*;
#[test]
fn codec_is_opt_in_and_requires_negotiated_support() {
let (tx, rx) = watch::channel(VideoSettings::default());
let rpc = |value| decode_wire(if value == 2 { &[18, 2, 104, 2] } else { &[18, 2, 104, 1] }).unwrap();
assert_eq!(rx.borrow().codec, VideoCodec::H264);
handle_rpc_with_codecs(&rpc(2), &tx, (1920,1080), true, false).unwrap();
assert_eq!(rx.borrow().codec, VideoCodec::H264);
handle_rpc_with_codecs(&rpc(2), &tx, (1920,1080), true, true).unwrap();
assert_eq!(rx.borrow().codec, VideoCodec::H265);
handle_rpc_with_codecs(&rpc(1), &tx, (1920,1080), false, true).unwrap();
assert_eq!(rx.borrow().codec, VideoCodec::H265); // unnegotiated H264 is never sent
// An unrelated FPS RPC must preserve the explicit codec selection.
handle_rpc_with_codecs(&decode_wire(&[26,2,8,1]).unwrap(), &tx, (1920,1080), true, true).unwrap();
assert_eq!(rx.borrow().codec, VideoCodec::H265);
// The viewer reports its current codec (1) in every request; that must not undo H.265.
handle_rpc_with_codecs(&rpc(1), &tx, (1920,1080), true, true).unwrap();
assert_eq!(rx.borrow().codec, VideoCodec::H265);
assert_eq!(preferred_codec(true, false), VideoCodec::H264);
}
}
+72
View File
@@ -0,0 +1,72 @@
//! RFC 7798 §4.4.1 / §4.4.3, single-layer non-interleaved HEVC (no DONL).
//! Parameter sets travel in-band before each IDR; no aggregation is required.
use bytes::Bytes;
pub fn payload(mtu: usize, annex_b: &[u8]) -> Result<Vec<Bytes>, String> {
if mtu < 4 { return Err("HEVC payload MTU too small".into()); }
let mut starts = Vec::new();
let mut i = 0;
while i + 3 <= annex_b.len() {
let prefix = if annex_b[i..].starts_with(&[0, 0, 0, 1]) { 4 }
else if annex_b[i..].starts_with(&[0, 0, 1]) { 3 } else { i += 1; continue; };
starts.push((i, i + prefix)); i += prefix;
}
if starts.is_empty() { return Err("HEVC access unit has no Annex-B start code".into()); }
let mut out = Vec::new();
for (index, &(_, start)) in starts.iter().enumerate() {
let mut end = starts.get(index + 1).map_or(annex_b.len(), |s| s.0);
while end > start && annex_b[end - 1] == 0 { end -= 1; } // trailing_zero_8bits
let nal = &annex_b[start..end];
if nal.len() < 2 || nal[0] & 0x80 != 0 || nal[1] & 7 == 0 {
return Err("Invalid HEVC NAL header".into());
}
let kind = (nal[0] >> 1) & 63;
if kind >= 48 { return Err("HEVC Annex-B contains RTP packet type".into()); }
if nal.len() <= mtu { out.push(Bytes::copy_from_slice(nal)); continue; }
let chunks = nal[2..].chunks(mtu - 3);
let last = chunks.len() - 1;
for (n, chunk) in chunks.enumerate() {
let mut fu = Vec::with_capacity(3 + chunk.len());
// Keep F, LayerId and TID, replacing only the NAL type with FU=49.
fu.extend_from_slice(&[(nal[0] & 0x81) | (49 << 1), nal[1],
kind | if n == 0 { 0x80 } else { 0 } | if n == last { 0x40 } else { 0 }]);
fu.extend_from_slice(chunk); out.push(fu.into());
}
}
Ok(out)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parameter_sets_and_small_nals_remain_in_order_without_start_codes() {
assert_eq!(payload(1200, &[0,0,0,1,64,1,9,0,0,1,66,1,8,0,0,1,38,1,7]).unwrap(),
vec![Bytes::from_static(&[64,1,9]), Bytes::from_static(&[66,1,8]), Bytes::from_static(&[38,1,7])]);
}
#[test]
fn fu_round_trip_preserves_header_payload_and_mtu_boundaries() {
for mtu in [4, 5, 7, 1190] {
let header = [0x27, 0x2b]; // IDR type19, nonzero layer and temporal id
let data: Vec<u8> = (0..2501).map(|i| (i % 251 + 1) as u8).collect();
let mut unit = vec![0,0,0,1]; unit.extend(header); unit.extend(&data);
let packets = payload(mtu, &unit).unwrap();
assert!(packets.len() > 1);
let mut rebuilt = vec![(packets[0][0] & 0x81) | ((packets[0][2] & 63) << 1), packets[0][1]];
for (i, p) in packets.iter().enumerate() {
assert!(p.len() <= mtu); assert_eq!((p[0] >> 1) & 63, 49);
assert_eq!(p[2] & 0x80 != 0, i == 0);
assert_eq!(p[2] & 0x40 != 0, i + 1 == packets.len());
rebuilt.extend_from_slice(&p[3..]);
}
assert_eq!(&rebuilt[..2], &header); assert_eq!(&rebuilt[2..], &data);
}
}
#[test]
fn rejects_invalid_headers_and_too_small_mtu() {
for bytes in [&[0,0,1,64][..], &[0,0,1,64,0][..], &[0,0,1,128,1][..], &[64,1][..]] {
assert!(payload(1200, bytes).is_err());
}
assert!(payload(3, &[0,0,1,64,1]).is_err());
}
}
+77 -26
View File
@@ -1,4 +1,4 @@
//! In-process X11 capture and H.264 encoding through libavdevice/libavcodec.
//! In-process X11 capture and H.264/HEVC encoding through libavdevice/libavcodec.
//! It replaces the `ffmpeg` child process with the same codec options, so the viewer
//! still receives the same Annex-B access units (AUD, SPS/PPS, IDR, P).
use std::ffi::CString;
@@ -11,6 +11,8 @@ use ff::ChannelLayout;
use ff::util::picture;
use ff::{codec, encoder, frame, software::scaling, Dictionary, Packet, Rational};
use tokio::sync::mpsc;
use tokio::sync::{Semaphore, OwnedSemaphorePermit};
use std::collections::VecDeque;
use crate::damage_capture::DamageCapture;
@@ -25,6 +27,7 @@ pub struct Config {
pub max_bps: i64,
pub buffer_bits: i64,
pub encoder: crate::video::Encoder,
pub codec: crate::capture_setting::VideoCodec,
/// Capture on XDamage changes (XShm) instead of x11grab; `UU_CAPTURE=grab` turns it off.
pub damage: bool,
}
@@ -57,13 +60,38 @@ fn ensure_init() -> Result<(), String> {
result
}
/// Sends one access unit per encoder packet. Returns `Ok(false)` when the receiver is gone.
fn drain(encoder: &mut encoder::Video, packet: &mut Packet, units: &mpsc::Sender<Vec<u8>>) -> Result<bool, String> {
pub struct EncodedUnit { pub data: Vec<u8>, pub permit: OwnedSemaphorePermit, pub completed_at: Instant }
fn acquire(budget: &Arc<Semaphore>, units: &mpsc::Sender<EncodedUnit>) -> Option<OwnedSemaphorePermit> {
tokio::runtime::Handle::current().block_on(async {
tokio::select! {
permit = budget.clone().acquire_owned() => permit.ok(),
_ = units.closed() => None,
}
})
}
#[cfg(test)]
mod budget_tests {
use super::*;
#[tokio::test]
async fn closing_encoder_output_wakes_a_blocked_capture() {
let budget=Arc::new(Semaphore::new(0));
let (tx,rx)=mpsc::channel(1);
let worker=tokio::task::spawn_blocking(move || acquire(&budget,&tx));
tokio::task::yield_now().await;
drop(rx);
assert!(tokio::time::timeout(Duration::from_secs(1),worker).await.unwrap().unwrap().is_none());
}
}
fn drain(encoder: &mut encoder::Video, packet: &mut Packet, units: &mpsc::Sender<EncodedUnit>, permits: &mut VecDeque<OwnedSemaphorePermit>) -> Result<bool, String> {
loop {
match encoder.receive_packet(packet) {
Ok(()) => {
let data = packet.data().unwrap_or_default().to_vec();
if units.blocking_send(data).is_err() { return Ok(false); }
let permit = permits.pop_front().ok_or("Encoder returned an unbudgeted access unit")?;
if units.blocking_send(EncodedUnit {data, permit, completed_at: Instant::now()}).is_err() { return Ok(false); }
}
Err(ff::Error::Other { errno }) if errno == libc::EAGAIN => return Ok(true),
Err(error) => return Err(text(error)),
@@ -71,11 +99,11 @@ fn drain(encoder: &mut encoder::Video, packet: &mut Packet, units: &mpsc::Sender
}
}
/// Opens the H.264 encoder for `config`. Returns the encoder, its input pixel format and output size.
/// Opens the selected video encoder for `config`. Returns the encoder, its input pixel format and output size.
fn open_encoder(config: &Config) -> Result<(encoder::Video, Pixel, u32, u32), String> {
let (width, height) = config.output.unwrap_or(config.input);
let codec = encoder::find_by_name(config.encoder.name())
.ok_or("H.264 encoder is not available in this FFmpeg build")?;
let codec = encoder::find_by_name(config.encoder.name_for(config.codec))
.ok_or_else(|| format!("{} is not available in this FFmpeg build", config.encoder.name_for(config.codec)))?;
// Allocate for this codec so FFmpeg fills its defaults; libx264 rejects a context allocated without one.
let mut context = unsafe { codec::context::Context::wrap(ff::sys::avcodec_alloc_context3(codec.as_ptr()), None) }.encoder().video().map_err(text)?;
context.set_width(width);
@@ -99,15 +127,22 @@ fn open_encoder(config: &Config) -> Result<(encoder::Video, Pixel, u32, u32), St
let cq = config.cq.to_string();
let gop = config.gop.to_string();
let mut options = Dictionary::new();
let hevc = config.codec == crate::capture_setting::VideoCodec::H265;
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); }
for (key,value) in [("profile", if hevc { "main" } else { "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", if hevc { "main" } else { "baseline" }), ("rc", "vbr"),
("rc-lookahead", "0"), ("zerolatency", "1"), ("delay", "0"), ("aud", "1"), ("forced-idr", "1")] {
options.set(key, value);
}
options.set("cq", &cq);
} else if hevc {
for (key, value) in [("preset", "superfast"), ("tune", "zerolatency"), ("profile", "main"), ("threads", "4"), ("forced-idr", "1")] {
options.set(key, value);
}
options.set("crf", &cq);
options.set("x265-params", &crate::video::x265_params(&gop));
} else {
let x264 = format!("aud=1:repeat-headers=1:scenecut=0:keyint={gop}:min-keyint={gop}");
for (key, value) in [("preset", "superfast"), ("tune", "zerolatency"), ("profile", "baseline"), ("threads", "4")] {
@@ -160,11 +195,11 @@ fn send_video_frame(encoder: &mut encoder::Video, frame: &frame::Video, kind: cr
}
/// 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> {
pub fn run(config: Config, units: mpsc::Sender<EncodedUnit>, stop: Arc<AtomicBool>, idr: Arc<AtomicBool>, budget: Arc<Semaphore>) -> Result<(), String> {
ensure_init()?;
if config.damage {
match DamageCapture::open(&config.display) {
Ok(capture) => return run_damage(config, capture, units, stop, idr),
Ok(capture) => return run_damage(config, capture, units, stop, idr, budget),
Err(error) => log::warn!("Damage-driven capture unavailable, using x11grab: {error}"),
}
}
@@ -187,14 +222,18 @@ pub fn run(config: Config, units: mpsc::Sender<Vec<u8>>, stop: Arc<AtomicBool>,
let mut scaler: Option<(scaling::Context, (Pixel, u32, u32))> = None;
let mut decoded = frame::Video::empty();
let mut converted = frame::Video::new(pixel, width, height);
let mut converted = frame::Video::empty();
let mut packet = Packet::empty();
let started = Instant::now();
let frame_interval = Duration::from_nanos(1_000_000_000 / u64::from(config.fps));
let mut last_at: Option<Duration> = None;
let mut last_slot: i64 = -1;
for (stream, input_packet) in input.packets() {
let mut permits = VecDeque::with_capacity(4);
let mut packets = input.packets();
loop {
if stop.load(Ordering::Relaxed) { return Ok(()); }
let Some(permit) = acquire(&budget, &units) else { return Ok(()); };
let Some((stream, input_packet)) = packets.next() else { return Ok(()); };
if stream.index() != index { continue; }
decoder.send_packet(&input_packet).map_err(text)?;
match decoder.receive_frame(&mut decoded) {
@@ -228,20 +267,19 @@ pub fn run(config: Config, units: mpsc::Sender<Vec<u8>>, stop: Arc<AtomicBool>,
frame.set_pts(Some(slot));
frame.set_kind(if idr.swap(false, Ordering::AcqRel) { picture::Type::I } else { picture::Type::None });
send_video_frame(&mut encoder, frame, config.encoder)?;
if !drain(&mut encoder, &mut packet, &units)? { return Ok(()); }
permits.push_back(permit);
if !drain(&mut encoder, &mut packet, &units, &mut permits)? { return 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> {
fn run_damage(config: Config, mut capture: DamageCapture, units: mpsc::Sender<EncodedUnit>, stop: Arc<AtomicBool>, idr: Arc<AtomicBool>, budget: Arc<Semaphore>) -> 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 converted = frame::Video::empty();
let mut scaler = None;
let mut packet = Packet::empty();
let started = Instant::now();
@@ -253,6 +291,9 @@ fn run_damage(config: Config, mut capture: DamageCapture, units: mpsc::Sender<Ve
let mut next_tick = Instant::now();
let mut last_send = Instant::now();
let mut last_slot: i64 = -1;
let mut permits = VecDeque::with_capacity(4);
let locked = crate::session_lock::watch_locked();
let mut last_locked = *locked.borrow();
loop {
if stop.load(Ordering::Relaxed) { return Ok(()); }
let deadline = if dirty { next_tick } else { last_send + keepalive };
@@ -263,13 +304,20 @@ fn run_damage(config: Config, mut capture: DamageCapture, units: mpsc::Sender<Ve
let now = Instant::now();
if dirty && now < next_tick { continue; }
if !dirty && now < last_send + keepalive { continue; }
let Some(permit) = acquire(&budget, &units) else { return Ok(()); };
if stop.load(Ordering::Relaxed) { return Ok(()); }
dirty |= capture.wait(Duration::ZERO)?;
let state = *locked.borrow();
if state != last_locked { dirty = true; last_locked = state; }
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]);
if dirty {
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;
@@ -282,13 +330,16 @@ fn run_damage(config: Config, mut capture: DamageCapture, units: mpsc::Sender<Ve
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)?; }
if dirty {
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(()); }
permits.push_back(permit);
if !drain(&mut encoder, &mut packet, &units, &mut permits)? { 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.
+205 -5
View File
@@ -8,6 +8,7 @@ extern "C" {
fn x11_pointer_state(x:*mut libc::c_int,y:*mut libc::c_int,mask:*mut libc::c_uint)->libc::c_int;
fn x11_key_symbol(symbol: libc::c_ulong, is_press: libc::c_int) -> libc::c_int;
fn x11_key_event(keycode: libc::c_int, is_press: libc::c_int) -> libc::c_int;
fn x11_release_all() -> libc::c_int;
fn x11_terminal_shortcut_state() -> libc::c_int;
}
#[cfg(not(target_os = "linux"))]
@@ -26,6 +27,9 @@ unsafe fn x11_key_symbol(_: libc::c_ulong, _: libc::c_int) -> libc::c_int { 0 }
#[cfg(not(target_os = "linux"))]
unsafe fn x11_mouse_relative(_: libc::c_int, _: libc::c_int) -> libc::c_int {0}
#[cfg(not(target_os="linux"))]
unsafe fn x11_release_all()->i32 {0}
#[derive(Default)]
struct PendingPointer {
absolute: Option<(i32,i32)>,
@@ -39,8 +43,12 @@ struct HeldInput { buttons: std::collections::BTreeSet<i32>, keys: std::collecti
/// 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> }
/// Mac viewer with 「使用 macOS 快捷键」 off sends Cmd as VK_LWIN (verified: Cmd 0x5B, Option 0xA4,
/// Ctrl 0xA2). Cmd is held back until we know whether it starts a Mac shortcut.
#[derive(Default)]
struct CmdState { held: bool, pending: bool, passthrough: bool, combos: std::collections::BTreeMap<i32, bool> }
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> }
pub struct InputInjector { mac_controller: std::sync::atomic::AtomicBool, cmd: std::sync::Mutex<CmdState>, serial:std::sync::Mutex<()>, owner:std::sync::Mutex<DesktopOwner>, shortcuts: std::sync::Mutex<Shortcuts>, uinput: Option<crate::uinput::Devices>, screen: std::sync::Mutex<(u32,u32)>, portal_failures: std::sync::atomic::AtomicUsize, portal_outcomes: [std::sync::atomic::AtomicUsize; 3], portal:std::sync::Mutex<Option<std::sync::Arc<crate::portal::PortalSession>>>, backend: crate::desktop_backend::DesktopBackend, pending: std::sync::Mutex<PendingPointer>, held: std::sync::Mutex<HeldInput> }
impl InputInjector {
pub fn new() -> Self {
@@ -51,7 +59,7 @@ impl InputInjector {
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()) }
Self { mac_controller: std::sync::atomic::AtomicBool::new(false), cmd: std::sync::Mutex::new(CmdState::default()), serial:std::sync::Mutex::new(()), owner:std::sync::Mutex::new(DesktopOwner::default()), 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>>) {
@@ -59,6 +67,7 @@ impl InputInjector {
*self.portal.lock().unwrap()=session;
}
pub fn mouse_move(&self, x: i32, y: i32) {
let _serial=self.serial.lock().unwrap();
self.inject_move(x,y,false);
}
@@ -93,6 +102,10 @@ impl InputInjector {
}
pub fn discard_pointer(&self) { *self.pending.lock().unwrap() = PendingPointer::default(); }
pub fn flush_pointer(&self) {
let _serial=self.serial.lock().unwrap();
self.flush_pointer_locked();
}
fn flush_pointer_locked(&self) {
// Hold the queue lock through injection so an older timer tick cannot
// overwrite a newer position flushed before a button transition.
let mut pending = self.pending.lock().unwrap();
@@ -118,7 +131,8 @@ impl InputInjector {
}
}
pub fn mouse_button(&self, button: i32, is_press: bool) {
self.flush_pointer();
let _serial=self.serial.lock().unwrap();
self.flush_pointer_locked();
let result = self.inject_button(button,is_press);
if result == 0 {
let mut held = self.held.lock().unwrap();
@@ -157,7 +171,62 @@ impl InputInjector {
shift
}
/// The current desktop controller is the macOS viewer (connect header controller_platform).
pub fn set_mac_controller(&self, mac: bool) {
self.mac_controller.store(mac, std::sync::atomic::Ordering::Relaxed);
*self.cmd.lock().unwrap() = CmdState::default();
}
fn terminal_focused(&self) -> bool {
self.backend == crate::desktop_backend::DesktopBackend::X11 && std::env::var_os("DISPLAY").is_some()
&& !std::env::var("UU_TERMINAL_SHORTCUTS").is_ok_and(|v| v == "0")
&& unsafe { x11_terminal_shortcut_state() } & 1 != 0
}
/// Mac habits for a Mac viewer with 「使用 macOS 快捷键」 off: Cmd+C/V/A/F/X/Z (the keys that option
/// lists) become Ctrl+key, and in a focused terminal Cmd+C/V become Ctrl+Shift+C/V (copy/paste)
/// while Ctrl+C still interrupts. Other Cmd combos, and Cmd alone, stay Super. Returns true when handled.
fn mac_cmd(&self, key: i32, is_press: bool) -> bool {
const SUPER_L: u64 = 0xffeb; const CONTROL_L: u64 = 0xffe3; const SHIFT_L: u64 = 0xffe1;
let mut st = self.cmd.lock().unwrap();
if matches!(key, 0x5b | 0x5c) {
if is_press { st.held = true; st.pending = true; st.passthrough = false; }
else {
if st.passthrough { self.inject_key(SUPER_L, false); }
else if st.pending { self.inject_key(SUPER_L, true); self.inject_key(SUPER_L, false); }
st.held = false; st.pending = false; st.passthrough = false;
}
return true;
}
if !is_press {
let Some(shifted) = st.combos.remove(&key) else { return false };
self.key_symbol_event(key, false);
if shifted { self.inject_key(SHIFT_L, false); }
self.inject_key(CONTROL_L, false);
return true;
}
if !st.held { return false; }
// Shift/Option/Ctrl pressed with Cmd (e.g. Cmd+Shift+Z) must not decide the combo yet.
if matches!(key, 0x10..=0x12 | 0xa0..=0xa5) { return false; }
if st.combos.contains_key(&key) { self.key_symbol_event(key, true); return true; } // autorepeat
if matches!(key, 0x41 | 0x43 | 0x46 | 0x56 | 0x58 | 0x5a) && !st.passthrough {
st.pending = false;
let shifted = matches!(key, 0x43 | 0x56) && self.terminal_focused();
self.inject_key(CONTROL_L, true);
if shifted { self.inject_key(SHIFT_L, true); }
self.key_symbol_event(key, true);
st.combos.insert(key, shifted);
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!("Mac shortcut: Cmd combo sent as Ctrl{}", if shifted { "+Shift (terminal)" } else { "" }); }
return true;
}
if st.pending { self.inject_key(SUPER_L, true); st.pending = false; st.passthrough = true; }
false
}
pub fn windows_key_event(&self, key: i32, is_press: bool) {
let _serial=self.serial.lock().unwrap();
if self.mac_controller.load(std::sync::atomic::Ordering::Relaxed) && self.mac_cmd(key, is_press) { return; }
if self.terminal_shift(key, is_press) {
const SHIFT_L: u64 = 0xffe1;
if is_press {
@@ -187,11 +256,30 @@ impl InputInjector {
}
pub fn release_held(&self) {
let _serial=self.serial.lock().unwrap();
self.discard_pointer();
let mut held=self.held.lock().unwrap();
for button in &held.buttons {self.inject_button(*button,false);}
for key in &held.keys {self.inject_key(*key,false);}
if let Some(uinput)=&self.uinput {
Self::uinput_result(uinput.release_all());
} else if self.backend==crate::desktop_backend::DesktopBackend::X11 {
unsafe {x11_release_all();}
} else if self.backend==crate::desktop_backend::DesktopBackend::Wayland && self.portal.lock().unwrap().is_none() {
crate::wayland::release_all();
} else {
// Portal/Wayland cannot query global state. Sweep every key/button
// that this host can emit through the currently bound session.
let keys:std::collections::BTreeSet<_>=(0..=255).filter_map(crate::input_mapping::windows_vk_to_keysym)
.chain(held.keys.iter().copied()).collect();
// A dead Portal bus can time out per call. Stop on the first
// failure rather than blocking teardown for hundreds of calls;
// the next connected session retries the complete sweep.
let buttons_ok=[1,2,3,8,9].into_iter().all(|button|self.inject_button(button,false)==0);
let keys_ok=buttons_ok && keys.into_iter().all(|key|self.inject_key(key,false)==0);
if !keys_ok {log::warn!("Desktop input reset incomplete: backend unavailable");}
}
*held=HeldInput::default();
*self.shortcuts.lock().unwrap()=Shortcuts::default();
*self.cmd.lock().unwrap()=CmdState::default();
}
// Per-injector budgets cover missing bindings, locked sessions, stale handles
// and D-Bus errors. Never include keys, coordinates, text or portal tokens.
@@ -260,6 +348,7 @@ impl InputInjector {
else { unsafe { x11_key_symbol(symbol as libc::c_ulong,i32::from(pressed)) } }
}
pub fn key_event(&self, keycode: i32, is_press: bool) {
let _serial=self.serial.lock().unwrap();
if self.backend!=crate::desktop_backend::DesktopBackend::X11 { return; }
unsafe { x11_key_event(keycode, if is_press { 1 } else { 0 }) };
}
@@ -333,3 +422,114 @@ mod scroll_tests {
assert_eq!(input.pending.lock().unwrap().scroll,(-5,-5));
}
}
/// Only the newest desktop lease may inject or release input. Tool peers never
/// obtain a lease; delayed callbacks from an old desktop cannot clear a new one.
#[derive(Default)]
struct DesktopOwner { next:u64, current:u64, active:bool, stopped:bool }
impl DesktopOwner {
fn replace(&mut self)->u64 {self.next+=1;self.current=self.next;self.active=false;self.current}
fn owns(&self,id:u64)->bool {id!=0 && self.current==id}
}
pub struct DesktopInputSession {input:std::sync::Arc<InputInjector>,id:u64}
impl InputInjector {
pub fn shutdown(&self) {
let mut owner=self.owner.lock().unwrap();
owner.current=0;owner.active=false;owner.stopped=true;
self.release_held();
}
pub fn desktop_session(self:&std::sync::Arc<Self>)->std::sync::Arc<DesktopInputSession> {
let mut owner=self.owner.lock().unwrap();
let id=if owner.stopped {0} else {self.release_held();owner.replace()};
std::sync::Arc::new(DesktopInputSession {input:self.clone(),id})
}
}
impl DesktopInputSession {
pub fn connected(&self) {
let mut owner=self.input.owner.lock().unwrap();
if owner.owns(self.id) && !owner.active {self.input.release_held();owner.active=true;}
}
pub fn disconnected(&self) {
let mut owner=self.input.owner.lock().unwrap();
if owner.owns(self.id) && owner.active {owner.active=false;self.input.release_held();}
}
pub fn with_input(&self, action:impl FnOnce()) {
let owner=self.input.owner.lock().unwrap();
if owner.owns(self.id) && owner.active {action();}
}
}
impl Drop for DesktopInputSession {fn drop(&mut self) {self.disconnected();}}
#[cfg(test)]
mod lifecycle_tests {
use super::*;
use std::sync::{Arc,atomic::{AtomicUsize,Ordering}};
#[test]
fn cleanup_discards_motion_shortcuts_and_untracked_input_ledger() {
let input=Arc::new(InputInjector::new());
input.held.lock().unwrap().keys.insert(0xffe1);
input.held.lock().unwrap().buttons.insert(1);
input.shortcuts.lock().unwrap().modifiers=2;
input.shortcuts.lock().unwrap().shifted.insert(0x56);
input.mouse_relative(32,0);
input.mouse_scroll(0,1);
input.release_held();
assert!(input.held.lock().unwrap().keys.is_empty());
assert!(input.held.lock().unwrap().buttons.is_empty());
assert_eq!(input.pending.lock().unwrap().relative,(0,0));
assert_eq!(input.pending.lock().unwrap().scroll,(0,0));
assert_eq!(input.shortcuts.lock().unwrap().modifiers,0);
assert!(input.shortcuts.lock().unwrap().shifted.is_empty());
input.release_held(); // idempotent even with an empty ledger
}
#[test]
fn disconnected_or_replaced_desktop_cannot_inject_or_clear_new_input() {
let input=Arc::new(InputInjector::new());
let count=AtomicUsize::new(0);
let old=input.desktop_session();
old.with_input(||{count.fetch_add(1,Ordering::Relaxed);});
assert_eq!(count.load(Ordering::Relaxed),0);
old.connected();
old.with_input(||{count.fetch_add(1,Ordering::Relaxed);});
old.disconnected();
old.with_input(||{count.fetch_add(1,Ordering::Relaxed);});
assert_eq!(count.load(Ordering::Relaxed),1);
input.held.lock().unwrap().buttons.insert(2);
old.disconnected(); // repeated close states must not release later local activity
assert!(input.held.lock().unwrap().buttons.contains(&2));
old.connected(); // same peer can reconnect
assert!(input.held.lock().unwrap().buttons.is_empty());
let new=input.desktop_session();
new.connected();
input.held.lock().unwrap().buttons.insert(1);
old.disconnected();old.connected();drop(old);
assert!(input.held.lock().unwrap().buttons.contains(&1));
new.with_input(||{count.fetch_add(1,Ordering::Relaxed);});
assert_eq!(count.load(Ordering::Relaxed),2);
drop(new);
assert!(input.held.lock().unwrap().buttons.is_empty());
let last=input.desktop_session();last.connected();
input.shutdown();last.connected();
last.with_input(||panic!("shutdown cannot be undone by a stale callback"));
let later=input.desktop_session();later.connected();
later.with_input(||panic!("shutdown must reject newly arriving offers too"));
}
#[test]
fn input_gate_serializes_disconnect_against_inflight_event() {
let input=Arc::new(InputInjector::new());
let session=input.desktop_session();session.connected();
let (started,ready)=std::sync::mpsc::channel();
let (finish,resume)=std::sync::mpsc::channel();
let worker=session.clone();
let event=std::thread::spawn(move ||worker.with_input(|| {
started.send(()).unwrap();resume.recv().unwrap();
worker.input.held.lock().unwrap().buttons.insert(1);
}));
ready.recv().unwrap();
let ending=session.clone();
let teardown=std::thread::spawn(move ||ending.disconnected());
finish.send(()).unwrap();event.join().unwrap();teardown.join().unwrap();
assert!(input.held.lock().unwrap().buttons.is_empty());
session.with_input(||panic!("late event after disconnect"));
}
}
+26 -7
View File
@@ -143,7 +143,7 @@ fn qr_rgba(content: &str, scale: usize, quiet: usize) -> Result<(Vec<u8>, usize)
Ok((rgba, edge))
}
fn write_config(cfg: &Value) -> Result<std::path::PathBuf, String> {
pub(crate) fn write_config(cfg: &Value) -> Result<std::path::PathBuf, String> {
use std::io::Write;
use std::os::unix::fs::{OpenOptionsExt, PermissionsExt};
let path = crate::config::path();
@@ -227,8 +227,8 @@ pub async fn run(gui: bool) -> Result<(), String> {
result.map(|_| ())
}
/// Returns the account nickname (or user id) on success.
async fn flow(ui: &LoginUi) -> Result<String, String> {
/// Local installation identifiers only; guest creation does not require device/init.
pub fn prepare_identity() -> Result<(Value, Credentials), String> {
let mut cfg = crate::config::file()?.cloned().unwrap_or_else(|| json!({}));
let get = |cfg: &Value, k: &str| std::env::var(format!("UU_{}", k.to_uppercase())).ok().filter(|v| !v.is_empty())
.or_else(|| cfg.get(k).and_then(|v| v.as_str()).map(str::to_owned)).unwrap_or_default();
@@ -237,8 +237,16 @@ async fn flow(ui: &LoginUi) -> Result<String, String> {
for (key, make) in [("client_id", random_uuid as fn() -> String), ("system_id", || random_uuid().to_uppercase()), ("machine_guid", machine_guid)] {
if get(&cfg, key).is_empty() { cfg[key] = json!(make()); }
}
let mut creds = Credentials { client_id: get(&cfg, "client_id"), system_id: get(&cfg, "system_id"), sign_key, ..Default::default() };
let creds = Credentials { client_id: get(&cfg, "client_id"), system_id: get(&cfg, "system_id"), device_id: get(&cfg, "device_id"), sign_key, ..Default::default() };
write_config(&cfg)?;
Ok((cfg, creds))
}
pub async fn prepare_device() -> Result<(Value, Credentials), String> {
let (mut cfg, mut creds) = prepare_identity()?;
let get = |cfg: &Value, k: &str| std::env::var(format!("UU_{}", k.to_uppercase())).ok().filter(|v| !v.is_empty())
.or_else(|| cfg.get(k).and_then(|v| v.as_str()).map(str::to_owned)).unwrap_or_default();
if get(&cfg, "device_id").is_empty() {
let body = json!({
"name": hostname(), "client_id": creds.client_id, "system_id": creds.system_id, "machine_guid": get(&cfg, "machine_guid"),
@@ -248,11 +256,17 @@ async fn flow(ui: &LoginUi) -> Result<String, String> {
let (status, resp) = UUAuthClient::new(creds.clone()).post_json("/api/v1/device/windows/init", &body).await?;
let d = data("device init", status, &resp)?;
let id = d.get("device_id").and_then(|v| v.as_str()).ok_or("device init returned no device_id")?;
println!("Registered device: name={} id={}", d.get("alias").and_then(|v| v.as_str()).unwrap_or(""), id);
log::info!("Registered local device identity");
cfg["device_id"] = json!(id);
write_config(&cfg)?; // keep the device even if the QR step is abandoned
}
creds.device_id = get(&cfg, "device_id");
Ok((cfg, creds))
}
/// Returns the account nickname (or user id) on success.
pub(crate) async fn flow(ui: &LoginUi) -> Result<String, String> {
let (mut cfg, creds) = prepare_device().await?;
let client = UUAuthClient::new(creds.clone());
loop {
@@ -274,7 +288,7 @@ async fn flow(ui: &LoginUi) -> Result<String, String> {
let mut file=std::fs::OpenOptions::new().write(true).create(true).truncate(true).mode(0o600).open(path).map_err(|e|e.to_string())?;
file.write_all(&crate::cursor::encode_png(edge as u32,edge as u32,&rgba)).map_err(|e|e.to_string())?;
}
println!("\nScan with the UU Remote mobile app (网易UU远程 App 扫码登录):\n{}", render_qr(&url)?);
if !ui.quiet() { println!("\nScan with the UU Remote mobile app (网易UU远程 App 扫码登录):\n{}", render_qr(&url)?); }
// Status is a long poll: send the state already shown (server enum 1..4; observed:
// 1 waiting, 2 scanned, 4 after the phone confirmed); the server answers when it changes.
let mut login_status = 1i64;
@@ -315,8 +329,13 @@ async fn flow(ui: &LoginUi) -> Result<String, String> {
cfg["token"] = json!(token);
cfg["user_id"] = json!(user);
cfg["csrf"] = json!(csrf_from_token(token).ok_or("token carries no csrf claim")?);
let path = write_config(&cfg)?;
let nickname = d.get("nickname").and_then(|v| v.as_str()).unwrap_or("");
if !nickname.is_empty() { cfg["account_name"] = json!(nickname); }
// Keep settings saved meanwhile (e.g. from the assist card): merge into the fresh file.
if let Ok(Some(Value::Object(fresh))) = crate::config::read_fresh() {
for (k, v) in fresh { if cfg.get(&k).is_none() { cfg[k] = v; } }
}
let path = write_config(&cfg)?;
println!("Logged in as {} ({}); saved to {}", nickname, user, path.display());
return Ok(if nickname.is_empty() { user.to_string() } else { nickname.to_string() });
}
+14 -4
View File
@@ -15,13 +15,13 @@ extern "C" {
#[derive(Clone, Copy)]
pub enum Tone { Info = 0, Ok = 1, Error = 2 }
pub struct LoginUi { shown: bool }
pub struct LoginUi { shown: bool, attached: bool }
#[cfg(uu_gtk)]
fn cstr(s: &str) -> std::ffi::CString { std::ffi::CString::new(s.replace('\0', "")).unwrap_or_default() }
impl LoginUi {
pub fn none() -> Self { LoginUi { shown: false } }
pub fn none() -> Self { LoginUi { shown: false, attached: false } }
/// Opens the window when built with GTK and a display is present; falls back silently otherwise.
#[allow(unused_variables)]
@@ -30,13 +30,23 @@ impl LoginUi {
if std::env::var_os("DISPLAY").is_some() || std::env::var_os("WAYLAND_DISPLAY").is_some() {
let (n, o, c, g) = (cstr(name), cstr(os), cstr(cpu), cstr(gpu));
if unsafe { uu_login_ui_start(n.as_ptr(), o.as_ptr(), c.as_ptr(), g.as_ptr()) } == 0 {
return LoginUi { shown: true };
return LoginUi { shown: true, attached: false };
}
eprintln!("Login window unavailable; using the terminal QR code");
}
LoginUi::none()
}
/// Account card of the host's own main window (already open): the QR stays in the window only
/// (a service's stdout is the journal and the QR URL is a login ticket), and dropping this
/// handle never closes the shared window.
pub fn attached() -> Self {
#[cfg(uu_gtk)]
{ unsafe { uu_login_ui_start(std::ptr::null(), std::ptr::null(), std::ptr::null(), std::ptr::null()) }; LoginUi { shown: true, attached: true } }
#[cfg(not(uu_gtk))]
LoginUi { shown: false, attached: true }
}
pub fn quiet(&self) -> bool { self.attached }
pub fn shown(&self) -> bool { self.shown }
#[allow(unused_variables)]
@@ -73,6 +83,6 @@ impl LoginUi {
impl Drop for LoginUi {
fn drop(&mut self) {
#[cfg(uu_gtk)]
if self.shown { unsafe { uu_login_ui_close() } }
if self.shown && !self.attached { unsafe { uu_login_ui_close() } }
}
}
+327 -54
View File
@@ -7,6 +7,7 @@ mod network_diagnostics;
mod session_ice;
mod wayland;
mod auth;
mod assist;
mod config;
mod login;
mod login_ui;
@@ -23,6 +24,8 @@ mod signaling;
mod terminal;
mod webrtc;
mod video;
mod video_track;
mod hevc;
#[cfg(feature = "inproc-av")]
mod damage_capture;
#[cfg(feature = "inproc-av")]
@@ -53,18 +56,103 @@ use std::sync::Arc;
use tokio::sync::{mpsc, Mutex};
use webrtc::WebRtcSession;
/// Set before a normal return when the host must come back in the other mode (signed in / out).
static RESTART: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
/// Re-executes this host; the new instance opens its window so the user sees the result.
fn restart_host() -> ! {
use std::os::unix::process::CommandExt;
let error = std::process::Command::new("/proc/self/exe").args(std::env::args_os().skip(1)).env("UU_SHOW_WINDOW", "1").exec();
eprintln!("restart failed: {error}");
std::process::exit(1);
}
/// systemd user unit running this process (from /proc/self/cgroup), e.g. "uuremote-native.service".
fn systemd_unit() -> Option<String> {
std::env::var_os("INVOCATION_ID")?;
let cgroup = std::fs::read_to_string("/proc/self/cgroup").ok()?;
cgroup.lines().find_map(|l| l.rsplit('/').next().filter(|u| u.ends_with(".service")).map(str::to_owned))
}
fn display_present() -> bool { std::env::var_os("DISPLAY").is_some() || std::env::var_os("WAYLAND_DISPLAY").is_some() }
/// SIGUSR1 (`uuremote-server show`) opens the host window.
fn present_on_usr1(ui: std::sync::Arc<assist::LocalUi>) {
// Install the handler before publishing the pid: SIGUSR1's default action terminates the process.
let Ok(mut usr1) = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::user_defined1()) else { return };
write_host_pid();
tokio::spawn(async move { while usr1.recv().await.is_some() { ui.present(); } });
}
fn host_pid_path() -> std::path::PathBuf { config::path().with_file_name("host.pid") }
/// Lets `uuremote-server show` find this host service to open its window (SIGUSR1).
fn write_host_pid() {
use std::os::unix::fs::OpenOptionsExt;
use std::io::Write;
if let Ok(mut f) = std::fs::OpenOptions::new().write(true).create(true).truncate(true).mode(0o600).open(host_pid_path()) {
let _ = write!(f, "{}", std::process::id());
}
}
/// `uuremote-server show` (desktop launcher): open the running host's window, or start the host in the
/// foreground with its window when none runs (e.g. no systemd unit installed yet).
fn show_running_host() -> Result<(), Box<dyn std::error::Error>> {
let running = std::fs::read_to_string(host_pid_path()).ok().and_then(|s| s.trim().parse::<i32>().ok()).filter(|p| *p > 1)
.filter(|pid| {
let exe = std::fs::read_link(format!("/proc/{pid}/exe")).ok();
let own = std::env::current_exe().ok();
exe.is_some() && exe.as_ref().and_then(|p| p.file_name()) == own.as_ref().and_then(|p| p.file_name())
});
if let Some(pid) = running {
if unsafe { libc::kill(pid, libc::SIGUSR1) } == 0 { return Ok(()); }
}
// Stopped from the tray but installed as a user service: start the service (not a second,
// unmanaged host), wait for it to publish its pid, then ask it to show the window.
let unit = std::env::var("UU_SERVICE_UNIT").unwrap_or_else(|_| "uuremote-native.service".into());
let installed = std::process::Command::new("systemctl").args(["--user", "cat", &unit])
.stdout(std::process::Stdio::null()).stderr(std::process::Stdio::null()).status().is_ok_and(|s| s.success());
if installed && std::process::Command::new("systemctl").args(["--user", "start", &unit]).status().is_ok_and(|s| s.success()) {
let before = std::fs::metadata(host_pid_path()).and_then(|m| m.modified()).ok();
for _ in 0..60 {
std::thread::sleep(std::time::Duration::from_millis(500));
let fresh = std::fs::metadata(host_pid_path()).and_then(|m| m.modified()).ok().is_some_and(|t| Some(t) != before);
let pid = std::fs::read_to_string(host_pid_path()).ok().and_then(|s| s.trim().parse::<i32>().ok());
if let (true, Some(pid)) = (fresh, pid) {
std::thread::sleep(std::time::Duration::from_millis(500)); // signal handler installed right after the pid
if unsafe { libc::kill(pid, libc::SIGUSR1) } == 0 { return Ok(()); }
}
}
return Err("the UU remote host service started but did not open its window".into());
}
use std::os::unix::process::CommandExt;
Err(Box::new(std::process::Command::new("/proc/self/exe").env("UU_SHOW_WINDOW", "1").exec()))
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();
// Not signed in, with a desktop: one window offers remote assistance (guest identity) and the QR
// login together; signing in restarts the host in account mode (assistance continues there).
let unified = std::env::args().nth(1).is_none() && display_present()
&& std::env::var("UU_REMOTE_ASSIST").ok().as_deref() != Some("0") && std::env::var("UU_LOGIN_WINDOW").ok().as_deref() != Some("0")
&& config::read_fresh().ok().flatten().and_then(|c| c.get("token").and_then(|t| t.as_str()).map(|t| !t.is_empty())) != Some(true)
&& ["UU_USER_ID", "UU_TOKEN", "UU_CSRF"].iter().all(|k| std::env::var_os(k).map_or(true, |v| v.is_empty()));
let assist_enabled = unified || std::env::args().nth(1).as_deref() == Some("assist")
|| (std::env::args().nth(1).is_none() && std::env::var("UU_REMOTE_ASSIST").ok().as_deref() == Some("1"));
if assist_enabled {
// HTTP/WS dependency trace logging can expose Authorization or room headers.
env_logger::Builder::new().filter_level(log::LevelFilter::Off)
.filter_module("uuremote_server", log::LevelFilter::Info).init();
} else { env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init(); }
// An unknown argument must never fall through to starting a second full host server.
const SUBCOMMANDS: [&str; 11] = ["desktop-input-probe", "virtual-audio-probe", "login", "unlock-check", "desktop-probe", "desktop-video-probe", "desktop-audio-probe", "wayland-capture",
const SUBCOMMANDS: [&str; 13] = ["show", "assist", "desktop-input-probe", "virtual-audio-probe", "login", "unlock-check", "desktop-probe", "desktop-video-probe", "desktop-audio-probe", "wayland-capture",
"wayland-input-probe", "clipboard-acceptance", "portal-acceptance"];
if let Some(arg) = std::env::args().nth(1).filter(|a| !SUBCOMMANDS.contains(&a.as_str())) {
let usage = format!("usage: uuremote-server [{}]\n (no argument: run the host)", SUBCOMMANDS.join(" | "));
if matches!(arg.as_str(), "help" | "-h" | "--help") { println!("{usage}"); return Ok(()); }
eprintln!("unknown argument: {arg}\n{usage}");
eprintln!("unknown argument\n{usage}");
std::process::exit(2);
}
if std::env::args().nth(1).as_deref() == Some("show") { return show_running_host(); }
if assist_enabled && !unified {
if std::env::args().len() > 2 { return Err("assist accepts no additional arguments; configure it with a private UU_CONFIG".into()); }
assist::configure()?;
}
if std::env::args().nth(1).as_deref() == Some("clipboard-acceptance") {
return tokio::task::spawn_blocking(clipboard_probe::run).await?.map_err(Into::into);
}
@@ -182,9 +270,27 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
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;
let measure=std::env::var("UU_INPUT_MEASURE").is_ok_and(|v|v=="1");
let cursor=|| -> Result<serde_json::Value,Box<dyn std::error::Error>> {
let output=std::process::Command::new("hyprctl").args(["-j","cursorpos"]).output()?;
if !output.status.success() { return Err("Cursor measurement unavailable".into()); }
Ok(serde_json::from_slice(&output.stdout)?)
};
let before=if measure {Some(cursor()?)} else {None};
input.mouse_relative(32,0);
tokio::time::sleep(std::time::Duration::from_millis(200)).await;
if let Some(before)=before {
println!("Relative pointer measurement: {}",serde_json::json!({"before":before,"after":cursor()?,"requested_delta":[32,0]}));
}
for key in b"UUWAYLAND" { input.windows_key_event(i32::from(*key),true); input.windows_key_event(i32::from(*key),false); }
if std::env::var("UU_INPUT_SYMBOLS").is_ok_and(|v|v=="1") {
// Fixed synthetic symbols exercise the same VK+modifier path as CONTROL.
for (key,shift) in [(0xbd,true),(0xbb,true),(0xbd,false)] {
if shift {input.windows_key_event(0x10,true);}
input.windows_key_event(key,true); input.windows_key_event(key,false);
if shift {input.windows_key_event(0x10,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");
@@ -243,7 +349,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let _audio_cleanup=virtual_audio::Cleanup;
log::info!("Starting uu-remote-linux host v{}...", env!("CARGO_PKG_VERSION"));
let mut capture = Arc::new((if desktop_backend::headless() {
let 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") {
@@ -253,34 +359,125 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
ScreenCapture::unavailable(error)
}));
let input = Arc::new(InputInjector::new());
input.bind_portal(capture.portal.clone());
input.release_held(); // recover stale input before accepting any peer
let _pointer_worker = input.start_pointer_worker();
#[cfg(target_os="linux")]
{
let shutdown_input=input.clone();
let (shutdown_tx, mut shutdown) = tokio::sync::watch::channel(false);
let shutdown_input = input.clone();
let signal_tx = shutdown_tx.clone();
tokio::spawn(async move {
#[cfg(target_os="linux")]
{
let mut term = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate()).expect("SIGTERM handler");
tokio::select! { _ = term.recv() => {}, _ = tokio::signal::ctrl_c() => {} }
}
#[cfg(not(target_os="linux"))]
let _ = tokio::signal::ctrl_c().await;
if !assist_enabled { shutdown_input.shutdown(); virtual_audio::disable(); terminal::shutdown_all(); std::process::exit(0); }
let _ = signal_tx.send(true);
});
let mut assist = if assist_enabled { Some(assist::Assist::start(&mut shutdown, unified).await?) } else { None };
if let Some(session) = assist.as_ref().filter(|_| unified) {
present_on_usr1(session.ui());
let login_done = shutdown_tx.clone();
tokio::spawn(async move {
if let Ok(mut term)=tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate()) {
term.recv().await; virtual_audio::disable(); shutdown_input.release_held(); terminal::shutdown_all(); std::process::exit(0);
let ui = login_ui::LoginUi::attached();
loop {
match login::flow(&ui).await {
Ok(_) => { RESTART.store(true, std::sync::atomic::Ordering::SeqCst); let _ = login_done.send(true); return; }
Err(error) => { log::warn!("QR login failed: {error}; retrying"); tokio::time::sleep(std::time::Duration::from_secs(5)).await; }
}
}
});
}
let creds = match config::file().and_then(Credentials::load) {
Ok(creds) => creds,
Err(e) => {
log::warn!("Not logged in: {e} ({} or UU_* environment variables)", config::path().display());
login::wait_for_credentials(None).await
let webrtc_sessions: Arc<Mutex<HashMap<String, WebRtcSession>>> = Arc::new(Mutex::new(HashMap::new()));
let (mut commands, command_task) = match assist.as_ref() {
Some(s) => { let (rx, task) = s.commands(shutdown_tx); (rx, Some(task)) },
None => (tokio::sync::mpsc::channel(1).1, None),
};
let mut immediate_shutdown = shutdown.clone();
let result = tokio::select! {
result = serve(capture, input.clone(), &mut shutdown, &mut assist, &mut commands, webrtc_sessions.clone()) => result,
_ = immediate_shutdown.changed() => Ok(()),
};
if let Some(task) = command_task { task.abort(); }
// One exit path for commands, signals, transport failures and UI failures.
if let Some(session) = assist.as_mut() { session.allowed = false; }
for (_, old) in webrtc_sessions.lock().await.drain() { let _ = old.pc.close().await; }
virtual_audio::disable(); input.shutdown(); terminal::shutdown_all();
// Switching identity (signed in / out) restarts at once and the new identity republishes the same
// device share: logout already withdrew it, and after a QR login the cloud moved it to the account
// (a guest cancel then fails). Only a real stop withdraws here.
let switching = RESTART.load(std::sync::atomic::Ordering::SeqCst);
let revoke = match assist.as_mut() { Some(s) if !switching => s.cancel().await, _ => Ok(()) };
if revoke.is_err() { log::error!("Cloud assistance revocation failed; host is closed locally"); }
result?;
if RESTART.load(std::sync::atomic::Ordering::SeqCst) { restart_host(); }
revoke.map_err(|_| "assist disabled locally, but cloud cancel failed; cloud revocation is unverified")?;
Ok(())
}
async fn serve(mut capture: Arc<ScreenCapture>, input: Arc<InputInjector>, shutdown: &mut tokio::sync::watch::Receiver<bool>,
assist: &mut Option<assist::Assist>, commands: &mut mpsc::Receiver<assist::Command>, webrtc_sessions: Arc<Mutex<HashMap<String, WebRtcSession>>>) -> Result<(), Box<dyn std::error::Error>> {
let mut auth = if let Some(session) = assist.as_ref() { session.client.clone() } else {
let creds = match config::file().and_then(Credentials::load) {
Ok(creds) => creds,
Err(e) => {
log::warn!("Not logged in: {e} ({} or UU_* environment variables)", config::path().display());
login::wait_for_credentials(None).await
}
};
let client = Arc::new(UUAuthClient::new(creds.clone()));
// Logged-in host: remote assistance (ID + code) alongside the device list, like the official
// client. Shown in a hidden window opened with `uuremote-server show`; UU_REMOTE_ASSIST=0 disables.
if std::env::var("UU_REMOTE_ASSIST").ok().as_deref() != Some("0") {
match assist::Assist::start_account(client.clone(), creds) {
Ok(session) => {
let (rx, _task) = session.commands(tokio::sync::watch::channel(false).0);
*commands = rx;
session.load_account_name().await;
present_on_usr1(session.ui());
if std::env::var("UU_SHOW_WINDOW").ok().as_deref() == Some("1") { session.ui().present(); }
std::env::remove_var("UU_SHOW_WINDOW");
log::info!("Remote assistance ready on the logged-in host (window: uuremote-server show)");
*assist = Some(session);
}
Err(error) => log::info!("Remote assistance unavailable on this host: {error}"),
}
}
client
};
let mut auth = Arc::new(UUAuthClient::new(creds));
// One peer per viewer connection (desktop, file transfer, terminal...), keyed by ice_id.
let webrtc_sessions: Arc<Mutex<HashMap<String, WebRtcSession>>> = Arc::new(Mutex::new(HashMap::new()));
let mut next_capture_type: Option<(u64,crate::session_ice::SessionIce)> = None;
let mut capture_types: HashMap<String, (u64,crate::session_ice::SessionIce)> = HashMap::new();
let mut mac_by_ice: HashMap<String, bool> = HashMap::new();
let mut next_mac = false;
loop {
if *shutdown.borrow() {
return Ok(());
}
log::info!("Requesting fresh room credentials from NetEase cloud...");
let room_data = match auth.create_room().await {
Ok(data) => data,
let room_result = tokio::select! {
result = async {
match assist.as_mut() {
Some(session) if !session.is_account() => {
let result = session.room().await.map_err(auth::RoomError::Other);
auth = session.client.clone(); result
}
_ => auth.create_room().await,
}
} => result,
_ = shutdown.changed() => return Ok(()),
};
let room_data = match room_result {
Ok(data) => {
if let Some(session) = assist.as_mut().filter(|s| s.is_account()) {
if let Err(error) = session.ensure_published().await { log::warn!("Remote assistance share info failed: {error}"); }
}
data
}
Err(auth::RoomError::LoginExpired) => {
// Like the official client: an expired token needs a new QR login, not retries.
log::error!("UU login is invalid or expired; a new QR login is required");
@@ -295,20 +492,15 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
}
};
log::info!(
"Acquired room signaling target: {}",
room_data.signaling_server
);
log::info!("Acquired room signaling credentials");
let (tx_event, mut rx_event) = mpsc::channel::<SignalingEvent>(64);
let tx_timer = tx_event.clone();
let signaling_client = match SignalingClient::connect(
&room_data.signaling_server,
&room_data.token,
tx_event,
)
.await
{
let connected = tokio::select! {
result = SignalingClient::connect(&room_data.signaling_server, &room_data.token, tx_event) => result,
_ = shutdown.changed() => return Ok(()),
};
let signaling_client = match connected {
Ok(cli) => {
let renewal_peers=webrtc_sessions.clone();
tokio::spawn(async move {
@@ -330,27 +522,88 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
Arc::new(cli)
}
Err(e) => {
log::error!("Failed to connect signaling: {}. Retrying in 5s...", e);
let _ = e;
log::error!("Failed to connect signaling; retrying in 5s (details omitted)");
tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
continue;
}
};
// 事件处理主循环
while let Some(evt) = rx_event.recv().await {
// Guest display/control commands never use stdout, stderr or logs.
let mut assist_tick = tokio::time::interval(std::time::Duration::from_secs(1));
loop {
let event = tokio::select! {
biased;
_ = shutdown.changed() => Some(SignalingEvent::Stop),
command = commands.recv(), if assist.is_some() => Some(SignalingEvent::AssistCommand(command.unwrap_or(assist::Command::Stop))),
_ = assist_tick.tick(), if assist.is_some() => Some(SignalingEvent::AssistTick),
event = rx_event.recv() => event,
};
let Some(evt) = event else { break; };
match evt {
SignalingEvent::Stop | SignalingEvent::AssistCommand(assist::Command::Stop) => {
return Ok(());
}
SignalingEvent::AssistCommand(assist::Command::Allow) => { if let Some(s) = assist.as_mut() { s.allow()?; } }
SignalingEvent::AssistCommand(assist::Command::Deny) => { if let Some(s) = assist.as_mut() { s.deny()?; } }
SignalingEvent::AssistCommand(assist::Command::Verify(v)) => { if let Some(s) = assist.as_mut() { s.set_verify(v)?; } }
SignalingEvent::AssistCommand(assist::Command::Refresh(r)) => { if let Some(s) = assist.as_mut() { s.set_refresh(r)?; } }
SignalingEvent::AssistCommand(assist::Command::Custom(c)) => { if let Some(s) = assist.as_mut() { s.set_custom(c)?; } }
SignalingEvent::AssistCommand(assist::Command::Quit) => {
// Tray 退出: under systemd (Restart=always) stop the unit itself, else just exit.
log::info!("Quit requested from the tray");
if let Some(unit) = systemd_unit() {
let _ = std::process::Command::new("systemctl").args(["--user", "stop", "--no-block", &unit]).status();
}
return Ok(());
}
SignalingEvent::AssistCommand(assist::Command::Logout) => {
if let Some(s) = assist.as_mut().filter(|s| s.is_account()) {
s.logout().await;
RESTART.store(true, std::sync::atomic::Ordering::SeqCst);
return Ok(());
}
}
SignalingEvent::AssistCommand(assist::Command::Reset) => { if let Some(s) = assist.as_mut() { s.reset()?; } }
SignalingEvent::AssistTick => {
let mut sessions = webrtc_sessions.lock().await;
let ended: Vec<_> = sessions.iter().filter(|(_, s)| matches!(s.pc.connection_state(),
::webrtc::peer_connection::peer_connection_state::RTCPeerConnectionState::Closed |
::webrtc::peer_connection::peer_connection_state::RTCPeerConnectionState::Failed)).map(|(id, _)| id.clone()).collect();
let mut desktop_ended = false;
for id in ended { if let Some(s) = sessions.remove(&id) { desktop_ended |= s.desktop; let _ = s.pc.close().await; } }
if desktop_ended && !sessions.values().any(|s| s.desktop) { if let Some(s) = assist.as_mut() { s.session_ended()?; } }
if let Some(s) = assist.as_mut() { s.tick()?; }
}
SignalingEvent::Connected => {
log::info!("Signaling handshake established.");
}
SignalingEvent::RoomInfoAck(val) => {
log::info!(
"RoomInfo acknowledged by NetEase server: room_id={}",
val.get("room_id")
.and_then(|v| v.as_str())
.unwrap_or("unknown")
);
}
SignalingEvent::RoomInfoAck(_) => { log::info!("RoomInfo acknowledged by NetEase server"); }
SignalingEvent::DeviceInfoChanged(val) => {
if assist.is_some() {
// Shape only (type value + key names), never field values: guest challenges carry salts/ids.
let keys = |v: Option<&serde_json::Value>| v.and_then(|v| v.as_object()).map(|o| o.keys().cloned().collect::<Vec<_>>()).unwrap_or_default();
log::debug!("Assist bmsg_push shape: type={:?} keys={:?} data_keys={:?}",
val.get("type").and_then(serde_json::Value::as_str), keys(Some(&val)), keys(val.get("data")));
}
if val.get("type").and_then(serde_json::Value::as_str) == Some("get_control_mode") {
if let Some(s) = assist.as_mut() {
match s.control_mode(val.get("data").unwrap_or(&serde_json::Value::Null)).await {
Ok(()) => log::info!("Assist control mode reported"),
Err(error) => log::warn!("Assist control mode report failed: {error}"),
}
}
continue;
}
if val.get("type").and_then(serde_json::Value::as_str) == Some("remote_control") {
if let Some(s) = assist.as_mut() {
if let Err(error) = s.challenge(val.get("data").unwrap_or(&serde_json::Value::Null)).await {
log::warn!("Assist challenge failed: {error}");
}
if s.renewed { break; }
}
continue;
}
log::info!(
"Device status synchronized: status={:?}, availability={:?}",
val.get("data")
@@ -361,7 +614,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.and_then(|s| s.as_str())
);
}
SignalingEvent::ConnectRequest { capture_type, ice_id, transport } => {
SignalingEvent::ConnectRequest { capture_type, ice_id, transport, controller_mac } => {
if mac_by_ice.len()>=32 { mac_by_ice.clear(); }
match ice_id.clone() { Some(id) => { mac_by_ice.insert(id, controller_mac); } None => next_mac = controller_mac }
// Pending cloud credentials live only within this room; cap
// requests that never receive an offer rather than retain them.
if capture_types.len()>=32 { capture_types.clear(); }
@@ -373,23 +628,27 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
}
SignalingEvent::Soac(soac) => {
log::info!(
"SOAC signaling received: type={}, ice_id={:?}",
soac.msg_type,
soac.ice_id
);
log::info!("SOAC signaling received: offer={} candidate={}", soac.msg_type == "offer", soac.msg_type == "candidate");
// Guest host: every peer is an assist peer. A logged-in host must keep serving its owner;
// assist peers there are already gated by the cloud (allow_control / code check).
if assist.as_ref().is_some_and(|s| !s.is_account() && !s.allowed) { continue; }
let ice = soac.ice_id.as_ref().map(|v| v.as_str().map(str::to_owned).unwrap_or_else(|| v.to_string())).unwrap_or_default();
let mut sessions = webrtc_sessions.lock().await;
// Drop peers that already ended.
let ended: Vec<String> = sessions.iter().filter(|(_, s)| matches!(s.pc.connection_state(),
::webrtc::peer_connection::peer_connection_state::RTCPeerConnectionState::Closed |
::webrtc::peer_connection::peer_connection_state::RTCPeerConnectionState::Failed)).map(|(k, _)| k.clone()).collect();
for k in ended { if let Some(old) = sessions.remove(&k) { let _ = old.pc.close().await; } }
let mut desktop_ended = false;
for k in ended { if let Some(old) = sessions.remove(&k) { desktop_ended |= old.desktop; let _ = old.pc.close().await; } }
if desktop_ended && !sessions.values().any(|s| s.desktop) { if let Some(s) = assist.as_mut() { s.session_ended()?; } }
if let Some(s) = assist.as_mut() { s.tick()?; }
if soac.msg_type == "offer" {
let (capture_type,transport) = capture_types.remove(&ice).or_else(|| next_capture_type.take()).unwrap_or((1,crate::session_ice::SessionIce::default()));
// CaptureType: 0 unknown, 1 desktop, 2 window; 5 file transfer, 8 terminal, 9 port mapping.
let desktop = capture_type <= 2;
let controller_mac = mac_by_ice.remove(&ice).unwrap_or(next_mac);
if desktop { input.set_mac_controller(controller_mac); }
log::info!("Offer for connection: capture_type={} desktop={} active_peers={}", capture_type, desktop, sessions.len());
if let Some(old) = sessions.remove(&ice) { let _ = old.pc.close().await; }
if desktop {
@@ -403,7 +662,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
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 resized=desktop && capture.init_error.is_none() && capture.backend==desktop_backend::DesktopBackend::X11
&& tokio::task::spawn_blocking(crate::capture::x11_resized).await.unwrap_or(false);
if resized { log::info!("Desktop geometry changed since startup; reinitializing capture"); }
let session_capture=if desktop && (capture.init_error.is_some() || stale_portal || resized) {
if stale_portal {
input.release_held();input.bind_portal(None);
capture=Arc::new(ScreenCapture::unavailable("Portal authorization expired".into()));
@@ -418,7 +680,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
// current desktop's authorized Portal input session.
if desktop { input.bind_portal(session_capture.portal.clone()); }
match WebRtcSession::new(session_capture, input.clone(), signaling_client.clone(), desktop, transport).await {
Ok(sess) => match sess.handle_offer(&soac).await {
Ok(sess) => {
// Register before awaiting SDP: shutdown can always close
// a peer whose answer may already be queued to the viewer.
sessions.insert(ice.clone(), sess);
let offer = sessions.get(&ice).expect("registered peer").handle_offer(&soac).await;
match offer {
Ok(answer_sdp) => {
log::info!("Local SDP Answer generated, emitting soac response...");
let resp = SoacMessage {
@@ -433,15 +700,17 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
app_control_id: soac.app_control_id.clone(),
};
if let Err(e) = signaling_client.emit_soac(resp).await { log::error!("Answer signaling send failed: {}", e); }
sessions.insert(ice, sess);
}
Err(e) => { log::error!("Failed to handle offer: {}", e); let _ = sess.pc.close().await; }
},
Err(e) => log::error!("Create peer failed: {}", e),
Err(_) => {
log::error!("Failed to handle offer (details omitted)");
if let Some(session) = sessions.remove(&ice) { let _ = session.pc.close().await; }
}
}},
Err(_) => log::error!("Create peer failed (details omitted)"),
}
} else if soac.msg_type == "candidate" {
if let Some(sess) = sessions.get(&ice) {
if let Err(e) = sess.add_candidate(&soac).await { log::error!("Remote ICE candidate failed: {}", e); }
if let Err(e) = sess.add_candidate(&soac).await { let _ = e; log::error!("Remote ICE candidate failed (details omitted)"); }
} else {
log::warn!("Remote ICE candidate for unknown connection");
}
@@ -457,11 +726,15 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
_ => sessions.keys().cloned().collect(),
};
log::info!("Remote controller disconnected: keys={:?} ice_matched={} closing={}", keys, ice.as_ref().is_some_and(|i| sessions.contains_key(i)), targets.len());
let mut desktop_ended = false;
for k in targets {
if let Some(old) = sessions.remove(&k) {
desktop_ended |= old.desktop;
if let Err(e) = old.pc.close().await { log::warn!("Close released peer failed: {}", e); }
}
}
if desktop_ended && !sessions.values().any(|s| s.desktop) { if let Some(s) = assist.as_mut() { s.session_ended()?; } }
if let Some(s) = assist.as_mut() { s.tick()?; }
}
SignalingEvent::Disconnected => {
capture_types.clear();
+25 -8
View File
@@ -1,6 +1,8 @@
//! GNOME/KDE portal session. Capture and input must share this Arc; do not
//! recreate it in an encoder subprocess (that would request consent again).
use std::sync::Arc;
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct FrameVersion(usize, u64);
#[cfg(unix)]
#[path = "portal_permissions.rs"]
mod permissions;
@@ -17,7 +19,7 @@ mod native {
fn uu_portal_close(portal: *mut c_void);
fn uu_portal_alive(portal: *mut c_void) -> i32;
fn uu_portal_reset_frame(portal: *mut c_void);
fn uu_portal_capture(portal: *mut c_void, bgra: *mut u8, length: usize) -> i32;
fn uu_portal_capture_since(portal: *mut c_void, bgra: *mut u8, length: usize, revision: *mut u64) -> i32;
fn uu_portal_move(portal: *mut c_void, x: f64, y: f64, relative: i32) -> i32;
fn uu_portal_button(portal: *mut c_void, button: i32, pressed: i32) -> i32;
fn uu_portal_key(portal: *mut c_void, keysym: u32, pressed: i32) -> i32;
@@ -26,6 +28,10 @@ mod native {
// The engine owns callback threads; public calls are serialized by Mutex.
unsafe impl Send for Handle {}
impl Drop for Handle { fn drop(&mut self) { unsafe { uu_portal_close(self.0.as_ptr()) } } }
fn blank_changed(frame: &mut [u8], version: &mut Option<super::FrameVersion>) -> bool {
if *version == Some(super::FrameVersion(0, 0)) { return false; }
super::blank(frame); *version = Some(super::FrameVersion(0, 0)); true
}
pub struct Session { handle: Mutex<Handle>, locked: tokio::sync::watch::Receiver<Option<bool>>, was_locked: std::sync::atomic::AtomicBool, retry_at: Mutex<Option<std::time::Instant>>, pub width: u32, pub height: u32 }
impl Session {
pub fn is_alive(&self) -> bool {
@@ -56,6 +62,10 @@ mod native {
Ok(Self { handle: Mutex::new(Handle(handle)), locked: crate::session_lock::watch_locked(), was_locked: std::sync::atomic::AtomicBool::new(false), retry_at: Mutex::new(None), width, height })
}
pub fn capture(&self, frame: &mut [u8]) -> Result<(), String> {
self.capture_changed(frame, &mut None).map(|_| ())
}
pub fn capture_changed(&self, frame: &mut [u8], version: &mut Option<super::FrameVersion>) -> Result<bool, String> {
if frame.len() != self.width as usize * self.height as usize * 4 { return Err(super::error(11)); }
let mut handle = self.handle.lock().map_err(|_| "Portal session lock poisoned")?;
let state = *self.locked.borrow();
if state != Some(false) {
@@ -64,30 +74,30 @@ mod native {
self.was_locked.store(true, std::sync::atomic::Ordering::Release);
unsafe { uu_portal_reset_frame(handle.0.as_ptr()) };
}
super::blank(frame); return Ok(());
return Ok(blank_changed(frame, version));
}
if self.was_locked.swap(false, std::sync::atomic::Ordering::AcqRel) {
// First unlocked capture after a real lock: drop frames captured while locked.
unsafe { uu_portal_reset_frame(handle.0.as_ptr()) };
super::blank(frame); return Ok(());
return Ok(blank_changed(frame, version));
}
if unsafe { uu_portal_alive(handle.0.as_ptr()) } == 0 {
// GNOME can close the stream before the periodic lock watcher updates.
if crate::session_lock::locked_now() != Some(false) {
self.was_locked.store(true, std::sync::atomic::Ordering::Release);
super::blank(frame); return Ok(());
return Ok(blank_changed(frame, version));
}
// logind's LockedHint can lag compositor stream closure. Give it a
// full watcher interval before attempting another permission session.
let mut retry = self.retry_at.lock().map_err(|_| "Portal recovery lock poisoned")?;
let deadline = retry.get_or_insert_with(|| std::time::Instant::now() + std::time::Duration::from_secs(1));
if std::time::Instant::now() < *deadline { super::blank(frame); return Ok(()); }
if std::time::Instant::now() < *deadline { return Ok(blank_changed(frame, version)); }
let replacement = match Self::open() {
Ok(value) => value,
Err(error) => {
log::warn!("Portal capture recovery deferred: {error}");
*retry = Some(std::time::Instant::now() + std::time::Duration::from_secs(5));
super::blank(frame); return Ok(());
return Ok(blank_changed(frame, version));
}
};
*retry = None;
@@ -97,8 +107,14 @@ mod native {
*handle = replacement.handle.into_inner().map_err(|_| "Portal session lock poisoned")?;
log::info!("Portal capture session restored after closure");
}
let code = unsafe { uu_portal_capture(handle.0.as_ptr(), frame.as_mut_ptr(), frame.len()) };
if code == 10 || code == 9 { super::blank(frame); Ok(()) } else { super::check(code) }
let identity = handle.0.as_ptr() as usize;
let mut revision = version.filter(|v| v.0 == identity).map_or(0, |v| v.1);
let code = unsafe { uu_portal_capture_since(handle.0.as_ptr(), frame.as_mut_ptr(), frame.len(), &mut revision) };
match code {
0 | 12 => { *version = Some(super::FrameVersion(identity, revision)); Ok(code == 0) }
9 | 10 => Ok(blank_changed(frame, version)),
_ => Err(super::error(code)),
}
}
pub fn clipboard_supported(&self) -> bool {
@@ -166,6 +182,7 @@ impl PortalSession {
pub fn is_alive(&self) -> bool { false }
pub fn open() -> Result<Self, String> { Err("Portal backend requires Linux and --features portal".into()) }
pub fn capture(&self, _: &mut [u8]) -> Result<(), String> { Err(error(2)) }
pub fn capture_changed(&self, _: &mut [u8], _: &mut Option<FrameVersion>) -> Result<bool, String> { Err(error(2)) }
pub fn move_pointer(&self, _: f64, _: f64, _: bool) -> Result<(), String> { Err(error(2)) }
pub fn button(&self, _: i32, _: bool) -> Result<(), String> { Err(error(2)) }
pub fn key(&self, _: u64, _: bool) -> Result<(), String> { Err(error(2)) }
+76
View File
@@ -75,6 +75,66 @@ pub fn decode_wire(input: &[u8]) -> Result<Vec<WireField<'_>>, WireError> {
Ok(fields)
}
/// Structure-only rendering for diagnostics: field numbers, wire types, lengths and small
/// varints (enums, sizes, fps). String and byte contents are never rendered.
pub fn wire_shape(data: &[u8], depth: u8) -> String {
let Ok(fields) = decode_wire(data) else { return format!("bytes({})", data.len()) };
let parts: Vec<String> = fields.iter().map(|f| match f.value {
WireValue::Varint(u64::MAX) => format!("{}=-1", f.number),
WireValue::Varint(v) if v <= 0xFFFF => format!("{}={v}", f.number),
WireValue::Varint(_) => format!("{}=big", f.number),
WireValue::Fixed32(_) => format!("{}:f32", f.number),
WireValue::Fixed64(_) => format!("{}:f64", f.number),
WireValue::Bytes(b) => format!("{}:{}", f.number, bytes_shape(b, depth)),
}).collect();
format!("{{{}}}", parts.join(","))
}
fn bytes_shape(b: &[u8], depth: u8) -> String {
let printable = !b.is_empty() && std::str::from_utf8(b).is_ok_and(|t| t.chars().all(|c| !c.is_control()));
if printable { return format!("str({})", b.len()); }
if depth > 0 && !b.is_empty() && decode_wire(b).is_ok_and(|f| !f.is_empty() && f.iter().all(|x| (1..=128).contains(&x.number))) {
return wire_shape(b, depth - 1);
}
format!("bytes({})", b.len())
}
/// JSON counterpart: keys and value types; strings as lengths (or nested JSON/base64-protobuf
/// shape), numbers only when small. Values that could identify a user are never rendered.
pub fn json_shape(value: &serde_json::Value, depth: u8) -> String {
use base64::Engine;
use serde_json::Value;
match value {
Value::Null => "null".into(),
Value::Bool(b) => b.to_string(),
Value::Number(n) => match n.as_i64() { Some(v) if v.abs() <= 0xFFFF => v.to_string(), _ => "num".into() },
Value::String(t) => {
if depth > 0 {
if let Ok(inner @ (Value::Object(_) | Value::Array(_))) = serde_json::from_str::<Value>(t) {
return format!("json{}", json_shape(&inner, depth - 1));
}
if t.len() >= 8 && t.len() % 4 == 0 {
if let Ok(raw) = base64::engine::general_purpose::STANDARD.decode(t) {
if decode_wire(&raw).is_ok_and(|f| !f.is_empty()) { return format!("b64{}", wire_shape(&raw, depth - 1)); }
}
}
}
format!("str({})", t.len())
}
Value::Array(items) => match items.first() {
Some(first) if depth > 0 => format!("[{}x{}]", items.len(), json_shape(first, depth - 1)),
_ => format!("[{}]", items.len()),
},
Value::Object(map) if depth > 0 => {
let parts: Vec<String> = map.iter().take(48).map(|(k, v)| {
let key: String = k.chars().filter(|c| c.is_ascii_alphanumeric() || *c == '_').take(40).collect();
format!("{key}:{}", json_shape(v, depth - 1))
}).collect();
format!("{{{}}}", parts.join(","))
}
Value::Object(map) => format!("{{{} keys}}", map.len()),
}
}
#[cfg(test)]
mod tests {
use super::*;
@@ -105,4 +165,20 @@ mod tests {
assert_eq!(decode_wire(&[11]), Err(WireError::UnsupportedWireType(3)));
assert_eq!(decode_wire(&[255, 255, 255, 255, 31]), Err(WireError::InvalidField));
}
#[test]
fn shapes_never_render_contents() {
// {1:1, 2:-1, 4:{1:60,2:2}, 9:"device-secret", 12:big}
let mut data = vec![0x08, 0x01, 0x10];
data.extend([0xff; 9]); data.push(0x01);
data.extend([0x22, 0x04, 0x08, 0x3c, 0x10, 0x02]);
data.extend([0x4a, 0x0d]); data.extend(b"device-secret");
data.extend([0x60, 0x80, 0x80, 0x80, 0x80, 0x10]);
let shape = super::wire_shape(&data, 3);
assert_eq!(shape, "{1=1,2=-1,4:{1=60,2=2},9:str(13),12=big}");
let header = serde_json::json!({"streamer_data": "{\"token\":\"secret-value\",\"n\":3}", "city": "Somewhere", "id": 123456789});
let shape = super::json_shape(&header, 4);
assert!(!shape.contains("secret") && !shape.contains("Somewhere") && !shape.contains("123456789"), "{shape}");
assert!(shape.contains("streamer_data:json{n:3,token:str(12)}"), "{shape}");
}
}
+81 -5
View File
@@ -32,13 +32,16 @@ pub struct SoacMessage {
}
pub enum SignalingEvent {
Stop,
AssistCommand(crate::assist::Command),
AssistTick,
Connected,
RoomInfoAck(Value),
DeviceInfoChanged(Value),
BeControlled(Value),
/// be-controlled with its binary app_data (ConnectOptions): capture type and the
/// ice_id it announces, when present.
ConnectRequest { capture_type: u64, ice_id: Option<String>, transport:crate::session_ice::SessionIce },
ConnectRequest { capture_type: u64, ice_id: Option<String>, transport:crate::session_ice::SessionIce, controller_mac: bool },
Soac(SoacMessage),
Released(Value),
Left(Value),
@@ -92,6 +95,30 @@ mod refresh_tests {
// Retain it until after the server sees closure, just like PC callbacks.
drop(retained);
}
#[tokio::test]
async fn silent_half_open_socket_is_reported_disconnected() {
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 (hold_tx,hold_rx)=tokio::sync::oneshot::channel::<()>();
tokio::spawn(async move {
let (tcp,_)=listener.accept().await.unwrap();
let mut socket=tokio_tungstenite::accept_async(tcp).await.unwrap();
// Shortest accepted window (clamped to 5 s); the server then never pings.
socket.send(Message::Text(r#"0{"pingInterval":1000,"pingTimeout":1000}"#.into())).await.unwrap();
let _=socket.next().await;
socket.send(Message::Text("40{}".into())).await.unwrap();
let _=hold_rx.await; // keep the TCP connection open but silent
});
let (sender,mut events)=tokio::sync::mpsc::channel(8);
let _client=super::SignalingClient::connect(&format!("ws://{address}"),"synthetic-only",sender).await.unwrap();
let disconnected=tokio::time::timeout(std::time::Duration::from_secs(8),async {
loop { if matches!(events.recv().await,Some(super::SignalingEvent::Disconnected)|None) { return; } }
}).await;
assert!(disconnected.is_ok(),"a socket without pings must be reported dead");
drop(hold_tx);
}
}
impl SignalingClient {
@@ -119,7 +146,7 @@ impl SignalingClient {
.await
.map_err(|e| format!("WS connect failed: {}", e))?;
log::info!("Connected to Engine.IO / Socket.IO at {}", ws_url);
log::info!("Connected to Engine.IO / Socket.IO");
let (mut write, mut read) = ws_stream.split();
let (tx_outgoing, mut rx_outgoing) = mpsc::channel::<String>(32);
@@ -127,7 +154,8 @@ impl SignalingClient {
let writer=tokio::spawn(async move {
while let Some(msg) = rx_outgoing.recv().await {
if let Err(e) = write.send(Message::Text(msg)).await {
log::warn!("WS send error: {}", e);
let _ = e;
log::warn!("WS send failed (details omitted)");
break;
}
}
@@ -152,9 +180,21 @@ impl SignalingClient {
let mut accepted_refresh=0;
// Socket.IO binary event awaiting attachments: (event name, header JSON).
let mut pending_binary: Option<(String, Value)> = None;
// Relation check only: the be-controlled ice_id versus the next offer's data.ice_id.
let mut last_connect_ice: Option<String> = None;
// Engine.IO 4: a client treats the connection as dead when no ping arrives within
// pingInterval + pingTimeout. A half-open TCP socket (suspend, NAT drop) otherwise
// blocks the read forever and the host silently stays offline.
let mut ping_window=std::time::Duration::from_secs(25+20);
let mut ping_deadline=tokio::time::Instant::now()+ping_window;
loop {
let msg_res=tokio::select! {
_=event_sender.closed()=>break,
_=tokio::time::sleep_until(ping_deadline) => {
log::warn!("Signaling heartbeat lost: 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;
break;
},
// Official ControlledSession RefreshSignalTokenLoop: first
// 30s, success every ~300s, failure retry after 30s. Native
// libstreamer 0x885774 / 0x885e58; emit has NO payload args.
@@ -181,12 +221,17 @@ impl SignalingClient {
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));
if let (Some(interval),Some(timeout))=(open.get("pingInterval").and_then(Value::as_u64),open.get("pingTimeout").and_then(Value::as_u64)) {
ping_window=std::time::Duration::from_millis(interval.saturating_add(timeout).clamp(5_000,300_000));
ping_deadline=tokio::time::Instant::now()+ping_window;
}
}
// Open packet -> 发送 Socket.IO connect
let _ = tx_out_clone.send("40".into()).await;
} else if txt == "2" {
// Ping -> 回复 Pong
let gap=last_ping.elapsed().as_secs();last_ping=std::time::Instant::now();pings+=1;
ping_deadline=tokio::time::Instant::now()+ping_window;
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);
@@ -251,6 +296,13 @@ impl SignalingClient {
.await;
}
"soac" => {
// Offers only: structure (keys, types, lengths), never SDP text or ids.
let kind = data.get("data").unwrap_or(data).get("type").and_then(Value::as_str);
if kind == Some("offer") {
log::info!("SOAC offer shape: {}", crate::protocol::json_shape(data, 3));
let offer_ice = data.get("data").unwrap_or(data).get("ice_id").and_then(Value::as_str);
log::info!("SOAC offer ice_id equals be-controlled ice_id: {:?}", last_connect_ice.as_deref().zip(offer_ice).map(|(a, b)| a == b));
}
let (target_val, client_id_opt) = if let Some(inner) = data.get("data") {
(inner.clone(), data.get("client_id").and_then(|v| v.as_str()).map(|s| s.to_string()))
} else {
@@ -267,7 +319,8 @@ impl SignalingClient {
.await;
}
Err(e) => {
log::error!("Failed to deserialize SoacMessage: error={}, raw={}", e, data);
let _ = e;
log::error!("Failed to deserialize SoacMessage (payload omitted)");
}
}
}
@@ -312,7 +365,30 @@ impl SignalingClient {
let ice_id = ["ice_id", "iceId"].iter().find_map(|k| header.get(*k).or_else(|| header.get("data").and_then(|d| d.get(*k))))
.and_then(|v| v.as_str()).map(str::to_owned);
log::info!("be-controlled connect request: capture_type={} header_keys={:?} ice_id_present={}", capture_type, keys, ice_id.is_some());
let _ = event_sender.send(SignalingEvent::ConnectRequest { capture_type, ice_id, transport:crate::session_ice::SessionIce::from_header(&header) }).await;
last_connect_ice = ice_id.clone();
// Structure only (numbers, types, lengths): documents what the viewer and cloud send.
let header_shape: String = crate::protocol::json_shape(&header, 7).chars().take(8000).collect();
log::info!("Connect request shape: app_data={} header={}", crate::protocol::wire_shape(data, 3), header_shape);
// Relation only, never the ids themselves.
let control_id = header.get("streamer_data").and_then(Value::as_str)
.and_then(|t| serde_json::from_str::<Value>(t).ok())
.and_then(|v| v.get("control_id").and_then(Value::as_str).map(str::to_owned));
let app_control_id = header.get("app_control_id").and_then(Value::as_str);
log::info!("Connect request ids: streamer_control_id_equals_app_control_id={:?}",
control_id.as_deref().zip(app_control_id).map(|(a, b)| a == b));
// ConnectOptions 4 decoder_cap_list: DecoderCap {1 fps, 2 codec_type (1 H264, 2 H265),
// 3 width, 4 height, 5 chroma_format (1 4:2:0, 3 4:4:4)}. Numbers only.
let caps: Vec<Vec<(u32, u64)>> = fields.iter().flatten().filter(|f| f.number == 4).filter_map(|f| match f.value {
crate::protocol::WireValue::Bytes(b) => crate::protocol::decode_wire(b).ok().map(|cap| cap.iter().filter_map(|x| match x.value {
crate::protocol::WireValue::Varint(v) => Some((x.number, v)), _ => None }).collect()),
_ => None }).collect();
if !caps.is_empty() { log::info!("Viewer decoder caps (fps,codec,w,h,chroma fields): {:?}", caps); }
// Viewer platform (website enum: 1 windows, 2 android, 3 ios, 4 macos, 5 tv).
let platform = header.get("controller_platform").or_else(|| header.get("data").and_then(|d| d.get("controller_platform")));
let controller_mac = platform.is_some_and(|v| v.as_i64() == Some(4)
|| v.as_str().is_some_and(|s| s == "4" || s.to_ascii_lowercase().contains("mac") || s.to_ascii_lowercase().contains("darwin")));
log::info!("Controller platform: {} mac={}", platform.map(|v| v.to_string()).unwrap_or_default().chars().take(24).collect::<String>(), controller_mac);
let _ = event_sender.send(SignalingEvent::ConnectRequest { capture_type, ice_id, transport:crate::session_ice::SessionIce::from_header(&header), controller_mac }).await;
}
}
}
+158 -33
View File
@@ -34,6 +34,7 @@ const MAGIC: &[u8; 4] = b"TERM";
const HEADER: usize = 16;
const MAX_PAYLOAD: usize = 0x10_0000;
const SCROLLBACK: usize = 256 * 1024;
const OUTPUT_CHUNK: usize = 16 * 1024;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Frame { pub kind: u8, pub flags: [u8; 2], pub session: u32, pub payload: Vec<u8> }
@@ -70,9 +71,24 @@ struct Session {
pid: i32,
sink: Mutex<Option<UnboundedSender<Vec<u8>>>>,
scrollback: Mutex<VecDeque<u8>>,
size: Mutex<(u16, u16)>,
}
impl Session {
/// Lock order sink → scrollback matches OPEN, so a reattach snapshot sees each
/// chunk either in the replay or live, never both.
fn record_output(&self, data: &[u8]) {
let mut sink = self.sink.lock().unwrap();
{
let mut sb = self.scrollback.lock().unwrap();
sb.extend(data);
let excess = sb.len().saturating_sub(SCROLLBACK);
sb.drain(..excess);
}
if let Some(tx) = sink.as_ref() {
if tx.send(encode(STDOUT, self.id, data)).is_err() { *sink = None; }
}
}
fn emit(&self, kind: u8, payload: &[u8]) -> bool {
let mut sink = self.sink.lock().unwrap();
if let Some(tx) = sink.as_ref() {
@@ -81,10 +97,31 @@ impl Session {
}
false
}
// The size lock is held across the ioctl so a delayed restore cannot reapply a stale size.
fn resize(&self, cols: u16, rows: u16) {
let mut size = self.size.lock().unwrap();
*size = (cols, rows);
self.set_winsize(cols, rows);
}
fn set_winsize(&self, cols: u16, rows: u16) {
let win = libc::winsize { ws_row: rows, ws_col: cols, ws_xpixel: 0, ws_ypixel: 0 };
unsafe { libc::ioctl(self.master.as_raw_fd(), libc::TIOCSWINSZ as _, &win); }
}
/// Full-screen programs repaint only on SIGWINCH, which an unchanged size never raises:
/// shrink by one row, then restore the latest requested size after a pause.
fn force_redraw(self: &Arc<Self>) {
{
let size = self.size.lock().unwrap();
if size.1 <= 1 { return; }
self.set_winsize(size.0, size.1 - 1);
}
let s = self.clone();
std::thread::spawn(move || {
std::thread::sleep(std::time::Duration::from_millis(150));
let size = s.size.lock().unwrap();
s.set_winsize(size.0, size.1);
});
}
fn write(&self, data: &[u8]) {
let mut off = 0;
while off < data.len() {
@@ -104,7 +141,7 @@ static NEXT_ID: AtomicU32 = AtomicU32::new(1);
fn now_ms() -> u64 { std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap_or_default().as_millis() as u64 }
fn spawn(cols: u16, rows: u16, shell: Option<&str>) -> Result<Arc<Session>, String> {
fn spawn(requested_id: u32, cols: u16, rows: u16, shell: Option<&str>) -> Result<Arc<Session>, String> {
use std::os::unix::process::CommandExt;
let win = nix::pty::Winsize { ws_row: rows.max(1), ws_col: cols.max(1), ws_xpixel: 0, ws_ypixel: 0 };
let pty = nix::pty::openpty(Some(&win), None).map_err(|e| format!("openpty failed: {e}"))?;
@@ -128,25 +165,23 @@ fn spawn(cols: u16, rows: u16, shell: Option<&str>) -> Result<Arc<Session>, Stri
}
let mut child = cmd.spawn().map_err(|e| format!("shell spawn failed: {e}"))?;
drop(slave);
let id = NEXT_ID.fetch_add(1, Ordering::Relaxed);
// Official Mac 4.42.0 (TerminalManager.open) allocates an ID only for OPEN(0).
let id = if requested_id != 0 { requested_id } else {
loop {
let id = NEXT_ID.fetch_add(1, Ordering::Relaxed);
if id != 0 && !registry().lock().unwrap().contains_key(&id) { break id; }
}
};
let session = Arc::new(Session { id, shell: shell.clone(), created_ms: now_ms(), pid: child.id() as i32,
master: pty.master, sink: Mutex::new(None), scrollback: Mutex::new(VecDeque::new()) });
master: pty.master, sink: Mutex::new(None), scrollback: Mutex::new(VecDeque::new()), size: Mutex::new((cols, rows)) });
let mut reader = std::fs::File::from(session.master.try_clone().map_err(|e| format!("pty dup failed: {e}"))?);
let worker = session.clone();
std::thread::Builder::new().name(format!("uu-term-{id}")).spawn(move || {
let mut buf = vec![0u8; 16 * 1024];
let mut buf = vec![0u8; OUTPUT_CHUNK];
loop {
match reader.read(&mut buf) {
Ok(0) | Err(_) => break,
Ok(n) => {
{
let mut sb = worker.scrollback.lock().unwrap();
sb.extend(&buf[..n]);
let excess = sb.len().saturating_sub(SCROLLBACK);
sb.drain(..excess);
}
worker.emit(STDOUT, &buf[..n]);
}
Ok(n) => worker.record_output(&buf[..n]),
}
}
let code = child.wait().ok().and_then(|s| s.code()).unwrap_or(-1);
@@ -205,16 +240,23 @@ impl TerminalConn {
OPEN => {
let v = json(&frame.payload);
let (cols, rows) = dims(&v);
let reattach = existing.is_some();
let session = match existing {
Some(s) => { s.resize(cols, rows); Ok(s) }
None => spawn(cols, rows, v.get("shell").and_then(|s| s.as_str())),
None => spawn(frame.session, cols, rows, v.get("shell").and_then(|s| s.as_str())),
};
match session {
Ok(s) => {
*s.sink.lock().unwrap() = Some(self.out.clone());
// Queue the acknowledgement before the PTY reader can emit live output.
let mut sink = s.sink.lock().unwrap();
self.send(OPEN_RESP, s.id, &reply(0, "ok"));
*sink = Some(self.out.clone());
let replay: Vec<u8> = s.scrollback.lock().unwrap().iter().copied().collect();
if !replay.is_empty() { self.send(STDOUT, s.id, &replay); }
// A full scrollback exceeds SCTP's 64 KiB outbound message limit.
for chunk in replay.chunks(OUTPUT_CHUNK) { self.send(STDOUT, s.id, chunk); }
drop(sink);
// The replay tail starts mid-redraw for full-screen programs.
if reattach { s.force_redraw(); }
}
Err(e) => { log::warn!("Terminal open failed: {}", e); self.send(OPEN_RESP, frame.session, &reply(1, &e)); }
}
@@ -241,7 +283,8 @@ impl TerminalConn {
"state": if attached { "running" } else { "detached" }, "created_at_ms": s.created_ms})
}).collect();
log::info!("Terminal list sessions: count={}", sessions.len());
self.send(LIST_RESP, 0, serde_json::json!({"code": 0, "message": "ok", "sessions": sessions}).to_string().as_bytes());
// Official Mac 4.42.0 (handleListSessionsReq) sends a bare array with the request's header ID.
self.send(LIST_RESP, frame.session, serde_json::Value::Array(sessions).to_string().as_bytes());
}
UNLOCK_REQ => self.send(UNLOCK_RESP, frame.session, &reply(0, "ok")),
_ => self.send(ERROR, frame.session, &reply(1, "unsupported frame")),
@@ -268,25 +311,107 @@ mod tests {
assert_eq!(signal_number(b"whatever"), libc::SIGINT);
}
#[test]
fn open_spawns_shell_and_streams_output() {
fn list_reply_is_an_array_and_preserves_request_id() {
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
let conn = TerminalConn::new(tx);
conn.handle(&encode(OPEN, 0, br#"{"cols":100,"rows":30,"shell":"/bin/sh"}"#));
let resp = decode(&rx.blocking_recv().unwrap()).unwrap();
assert_eq!(resp.kind, OPEN_RESP);
assert_eq!(json(&resp.payload)["code"], 0);
conn.handle(&encode(STDIN, resp.session, b"echo uu-term-ok; exit 3\n"));
let mut seen = Vec::new();
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10);
let mut exit = None;
while std::time::Instant::now() < deadline && exit.is_none() {
if let Ok(m) = rx.try_recv() {
let f = decode(&m).unwrap();
if f.kind == STDOUT { seen.extend(f.payload) } else if f.kind == EXIT { exit = Some(json(&f.payload)["exit_code"].clone()) }
} else { std::thread::sleep(std::time::Duration::from_millis(20)); }
for request in [0, 7, u32::MAX] {
conn.handle(&encode(LIST_REQ, request, &[]));
let response = decode(&rx.try_recv().unwrap()).unwrap();
assert_eq!(response.kind, LIST_RESP);
assert_eq!(response.session, request);
assert!(json(&response.payload).is_array());
}
}
#[test]
fn force_redraw_shrinks_then_restores_latest_size() {
let pty = nix::pty::openpty(None, None).unwrap();
let session = Arc::new(Session { id: u32::MAX - 2, shell: "/bin/sh".into(), created_ms: now_ms(),
master: pty.master, pid: 0, sink: Mutex::new(None),
scrollback: Mutex::new(VecDeque::new()), size: Mutex::new((80, 24)) });
let rows = |s: &Session| {
let mut win = libc::winsize { ws_row: 0, ws_col: 0, ws_xpixel: 0, ws_ypixel: 0 };
unsafe { libc::ioctl(s.master.as_raw_fd(), libc::TIOCGWINSZ as _, &mut win); }
(win.ws_col, win.ws_row)
};
session.resize(100, 30);
session.force_redraw();
assert_eq!(rows(&session), (100, 29));
session.resize(120, 40);
std::thread::sleep(std::time::Duration::from_millis(400));
assert_eq!(rows(&session), (120, 40), "restore must use the latest requested size");
}
#[test]
fn reconnect_replays_large_history_in_bounded_frames() {
let pty = nix::pty::openpty(None, None).unwrap();
let id = u32::MAX - 1;
let history: Vec<u8> = (0..SCROLLBACK).map(|n| (n % 251) as u8).collect();
let session = Arc::new(Session { id, shell: "/bin/sh".into(), created_ms: now_ms(),
master: pty.master, pid: 0, sink: Mutex::new(None),
scrollback: Mutex::new(history.iter().copied().collect()), size: Mutex::new((80, 24)) });
registry().lock().unwrap().insert(id, session.clone());
struct Cleanup(u32);
impl Drop for Cleanup { fn drop(&mut self) { registry().lock().unwrap().remove(&self.0); } }
let _cleanup = Cleanup(id);
for _ in 0..2 {
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
let conn = TerminalConn::new(tx);
conn.handle(&encode(OPEN, id, br#"{"cols":80,"rows":24}"#));
let opened = decode(&rx.try_recv().unwrap()).unwrap();
assert_eq!(opened.kind, OPEN_RESP);
assert_eq!(opened.session, id);
let mut replay = Vec::new();
while replay.len() < history.len() {
let packet = rx.try_recv().unwrap();
// SCTP's default outbound limit includes the TERM header.
assert!(packet.len() <= 65536, "terminal replay exceeds the SCTP message limit");
let frame = decode(&packet).unwrap();
assert_eq!(frame.kind, STDOUT);
assert_eq!(frame.session, id);
replay.extend(frame.payload);
}
assert!(replay == history, "terminal replay must preserve every byte in order");
assert!(session.emit(STDOUT, b"live output"));
assert_eq!(decode(&rx.try_recv().unwrap()).unwrap().payload, b"live output");
conn.handle(&encode(LIST_REQ, 7, &[]));
assert_eq!(decode(&rx.try_recv().unwrap()).unwrap().kind, LIST_RESP);
conn.handle(&encode(DETACH, id, &[]));
}
}
#[test]
fn open_spawns_shell_and_streams_output() {
for requested_id in [0, u32::MAX] {
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
let conn = TerminalConn::new(tx);
conn.handle(&encode(OPEN, requested_id, br#"{"cols":100,"rows":30,"shell":"/bin/sh"}"#));
let resp = decode(&rx.blocking_recv().unwrap()).unwrap();
assert_eq!(resp.kind, OPEN_RESP);
assert_eq!(json(&resp.payload)["code"], 0);
assert_ne!(resp.session, 0);
if requested_id != 0 { assert_eq!(resp.session, requested_id); }
let (list_tx, mut list_rx) = tokio::sync::mpsc::unbounded_channel();
TerminalConn::new(list_tx).handle(&encode(LIST_REQ, 7, &[]));
let list = decode(&list_rx.try_recv().unwrap()).unwrap();
let sessions: Vec<serde_json::Value> = serde_json::from_slice(&list.payload).unwrap();
let session = sessions.iter().find(|s| s["session_id"] == resp.session).unwrap();
assert_eq!(session["shell"], "/bin/sh");
assert_eq!(session["state"], "running");
conn.handle(&encode(STDIN, resp.session, b"echo uu-term-ok; exit 3\n"));
let mut seen = Vec::new();
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10);
let mut exit = None;
while std::time::Instant::now() < deadline && exit.is_none() {
if let Ok(m) = rx.try_recv() {
let f = decode(&m).unwrap();
assert_eq!(f.session, resp.session);
if f.kind == STDOUT { seen.extend(f.payload) } else if f.kind == EXIT { exit = Some(json(&f.payload)["exit_code"].clone()) }
} else { std::thread::sleep(std::time::Duration::from_millis(20)); }
}
assert!(String::from_utf8_lossy(&seen).contains("uu-term-ok"));
assert_eq!(exit, Some(serde_json::json!(3)));
}
assert!(String::from_utf8_lossy(&seen).contains("uu-term-ok"));
assert_eq!(exit, Some(serde_json::json!(3)));
}
}
+45
View File
@@ -121,6 +121,16 @@ mod imp {
pub fn key(&self, code: u16, pressed: bool) -> std::io::Result<()> {
self.keyboard.lock().unwrap().emit(&[(EV_KEY, code, i32::from(pressed))])
}
pub fn release_all(&self) -> std::io::Result<()> {
// Release every supported code on our own devices, even if the
// userspace ledger missed a press. Never touch physical devices.
let keys:Vec<_>=(1..=248).map(|code|(EV_KEY,code,0))
.chain([BTN_LEFT,BTN_RIGHT,BTN_MIDDLE].into_iter().map(|code|(EV_KEY,code,0))).collect();
let buttons:Vec<_>=[BTN_LEFT,BTN_RIGHT,BTN_MIDDLE,BTN_SIDE,BTN_EXTRA].into_iter().map(|code|(EV_KEY,code,0)).collect();
let keyboard=self.keyboard.lock().unwrap().emit(&keys);
let pointer=self.pointer.lock().unwrap().emit(&buttons);
keyboard.and(pointer)
}
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))])
}
@@ -138,6 +148,40 @@ mod imp {
self.pointer.lock().unwrap().emit(&events)
}
}
#[cfg(test)]
mod reset_acceptance {
use super::*;
fn snapshot(device:&Device)->[u8;96] {
let mut name=[0u8;80];
let request=0x8000_0000u64 | (80<<16) | (b'U' as u64)<<8 | 44;
assert!(unsafe {libc::ioctl(device.0.as_raw_fd(),request as libc::c_ulong,name.as_mut_ptr())}>=0);
let end=name.iter().position(|v|*v==0).unwrap();
let directory=std::path::Path::new("/sys/devices/virtual/input").join(std::str::from_utf8(&name[..end]).unwrap());
let event=std::fs::read_dir(directory).unwrap().filter_map(Result::ok)
.find(|e|e.file_name().to_string_lossy().starts_with("event")).unwrap();
let file=File::open(std::path::Path::new("/dev/input").join(event.file_name())).unwrap();
let mut state=[0u8;96];
let request=0x8000_0000u64 | (96<<16) | (b'E' as u64)<<8 | 0x18;
assert!(unsafe {libc::ioctl(file.as_raw_fd(),request as libc::c_ulong,state.as_mut_ptr())}>=0);
state
}
#[test]
#[ignore="requires isolated QEMU VM uinput and evdev permissions"]
fn kernel_devices_release_even_without_an_inputinjector_ledger() {
assert_eq!(std::fs::read_to_string("/proc/sys/kernel/hostname").unwrap().trim(),"uu-hyprland-test");
assert_eq!(std::process::Command::new("systemd-detect-virt").output().unwrap().stdout,b"qemu\n");
let devices=Devices::open().unwrap();
std::thread::sleep(std::time::Duration::from_millis(500));
devices.key(42,true).unwrap();devices.key(30,true).unwrap();
devices.button(BTN_LEFT,true).unwrap();
assert!(snapshot(&devices.keyboard.lock().unwrap()).iter().any(|v|*v!=0));
assert!(snapshot(&devices.pointer.lock().unwrap()).iter().any(|v|*v!=0));
devices.release_all().unwrap();
assert!(snapshot(&devices.keyboard.lock().unwrap()).iter().all(|v|*v==0));
assert!(snapshot(&devices.pointer.lock().unwrap()).iter().all(|v|*v==0));
}
}
}
#[cfg(not(target_os = "linux"))]
@@ -151,6 +195,7 @@ mod imp {
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 release_all(&self) -> std::io::Result<()> { Ok(()) }
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(()) }
+543 -96
View File
@@ -1,14 +1,16 @@
use std::sync::Arc;
use std::collections::HashMap;
use std::ffi::OsString;
use std::path::PathBuf;
use std::sync::{Arc, Mutex, OnceLock};
use std::sync::atomic::AtomicBool;
use std::time::{Duration, Instant};
use tokio::io::AsyncReadExt;
use tokio::process::Command;
use tokio::sync::mpsc;
use tokio::sync::{mpsc, Semaphore, OwnedSemaphorePermit};
use webrtc::media::Sample;
use webrtc::peer_connection::peer_connection_state::RTCPeerConnectionState;
use webrtc::peer_connection::RTCPeerConnection;
use webrtc::track::track_local::track_local_static_sample::TrackLocalStaticSample;
use crate::capture_setting::VideoSettings;
use crate::capture_setting::{VideoCodec, VideoSettings};
/// Default frame rate; the viewer's 帧率 setting overrides it per connection.
pub const FPS: u64 = 60;
@@ -28,27 +30,46 @@ fn gop_frames(fps: u32, in_process: bool) -> u64 {
}
const MAX_ACCESS_UNIT: usize = 8 * 1024 * 1024;
fn nal_offsets(bytes: &[u8], kind: u8) -> Vec<usize> {
fn nal_offsets_for(bytes: &[u8], kind: u8, codec: VideoCodec) -> Vec<usize> {
let mut result = Vec::new();
let mut i = 0;
while i + 3 < bytes.len() {
let prefix = if bytes[i..].starts_with(&[0, 0, 0, 1]) { 4 }
else if bytes[i..].starts_with(&[0, 0, 1]) { 3 } else { i += 1; continue };
if i + prefix < bytes.len() && bytes[i + prefix] & 31 == kind { result.push(i); }
let header = i + prefix;
let matches = match codec {
VideoCodec::H264 => header < bytes.len() && bytes[header] & 31 == kind,
// HEVC has a two-byte header: forbidden_zero_bit=0, temporal_id_plus1 != 0.
VideoCodec::H265 => header + 1 < bytes.len() && bytes[header] & 128 == 0
&& bytes[header + 1] & 7 != 0 && (bytes[header] >> 1) & 63 == kind,
};
if matches { result.push(i); }
i += prefix;
}
result
}
fn aud_offsets(bytes: &[u8]) -> Vec<usize> { nal_offsets(bytes, 9) }
fn is_independent_frame(bytes: &[u8]) -> bool {
!nal_offsets(bytes, 5).is_empty() && nal_offsets(bytes, 1).is_empty()
fn aud_offsets_for(bytes: &[u8], codec: VideoCodec) -> Vec<usize> {
nal_offsets_for(bytes, if codec == VideoCodec::H265 { 35 } else { 9 }, codec)
}
fn is_independent_frame_for(bytes: &[u8], codec: VideoCodec) -> bool {
match codec {
VideoCodec::H264 => !nal_offsets_for(bytes, 5, codec).is_empty() && nal_offsets_for(bytes, 1, codec).is_empty(),
// BLA/IDR/CRA are IRAP (RFC 7798 §4.1); no leading pictures with our no-B-frame encoder.
VideoCodec::H265 => (16..=21).any(|kind| !nal_offsets_for(bytes, kind, codec).is_empty())
&& !(0..16).any(|kind| !nal_offsets_for(bytes, kind, codec).is_empty()),
}
}
#[cfg(test)]
fn aud_offsets(bytes: &[u8]) -> Vec<usize> { aud_offsets_for(bytes, VideoCodec::H264) }
fn is_independent_frame(bytes: &[u8]) -> bool { is_independent_frame_for(bytes, VideoCodec::H264) }
#[derive(Debug)]
struct CompleteFrame {
codec: VideoCodec,
sequence: u64,
completed_at: Instant,
data: Vec<u8>,
_permit: Option<OwnedSemaphorePermit>,
}
const RTP_CLOCK: u64 = 90_000;
@@ -63,11 +84,35 @@ impl Encoder {
match value { Some("cpu") => Self::Cpu, Some("vaapi") => Self::Vaapi, Some("nvenc") => Self::Nvenc,
_ => if nvidia { Self::Nvenc } else { Self::Cpu } }
}
pub(crate) fn name(self) -> &'static str { match self { Self::Cpu => "libx264", Self::Nvenc => "h264_nvenc", Self::Vaapi => "h264_vaapi" } }
pub(crate) fn name_for(self, codec: VideoCodec) -> &'static str {
match (self, codec) {
(Self::Cpu, VideoCodec::H264) => "libx264", (Self::Cpu, VideoCodec::H265) => "libx265",
(Self::Nvenc, VideoCodec::H264) => "h264_nvenc", (Self::Nvenc, VideoCodec::H265) => "hevc_nvenc",
(Self::Vaapi, VideoCodec::H264) => "h264_vaapi", (Self::Vaapi, VideoCodec::H265) => "hevc_vaapi",
}
}
}
fn nvidia_present() -> bool {
std::path::Path::new("/proc/driver/nvidia/version").exists() || std::path::Path::new("/dev/nvidia0").exists()
}
/// 真彩 (4:4:4) needs the FFmpeg process pipeline with NVENC or libx264 (h264_vaapi and the
/// in-process encoder are 4:2:0 only).
pub fn full_chroma_available() -> bool {
#[cfg(feature = "inproc-av")]
if crate::inproc_encoder::selected() { return false; }
Encoder::requested() != Encoder::Vaapi
}
/// (-profile:v, -pix_fmt) for NVENC / libx264 / libx265: 4:4:4 (真彩) needs a 4:4:4 profile.
fn chroma_args(settings: &VideoSettings, encoder: Encoder) -> (&'static str, &'static str) {
if settings.chroma != crate::capture_setting::CHROMA_444 { return (profile_for(settings.codec, false), "yuv420p"); }
match (settings.codec, encoder) {
(VideoCodec::H264, Encoder::Nvenc) => ("high444p", "yuv444p"),
(VideoCodec::H264, _) => ("high444", "yuv444p"),
(VideoCodec::H265, Encoder::Nvenc) => ("rext", "yuv444p"),
(VideoCodec::H265, _) => ("main444-8", "yuv444p"),
}
}
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.
@@ -79,12 +124,187 @@ fn vaapi_options(settings: &VideoSettings, gop: &str) -> Vec<String> {
let (maxrate, buffer) = rate_args(settings, Encoder::Vaapi);
// 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",
["-c:v", Encoder::Vaapi.name_for(settings.codec), "-profile:v", profile_for(settings.codec, true), "-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()
}
fn profile_for(codec: VideoCodec, vaapi: bool) -> &'static str {
match codec { VideoCodec::H265 => "main", VideoCodec::H264 => if vaapi { "constrained_baseline" } else { "baseline" } }
}
pub(crate) fn x265_params(gop: &str) -> String {
// Explicit no-B/no-lookahead avoids delayed frames; repeat parameter sets at each IRAP.
format!("aud=1:repeat-headers=1:scenecut=0:keyint={gop}:min-keyint={gop}:open-gop=0:bframes=0:rc-lookahead=0:frame-threads=1:pools=4")
}
fn cpu_hevc_options(settings: &VideoSettings, gop: &str) -> Vec<String> {
let (maxrate, buffer) = rate_args(settings, Encoder::Cpu);
let (profile, pix_fmt) = chroma_args(settings, Encoder::Cpu);
let mut options = ["-c:v", "libx265", "-preset", "superfast", "-tune", "zerolatency", "-profile:v", profile,
"-pix_fmt", pix_fmt, "-g", gop, "-bf", "0", "-threads", "4",
"-maxrate", &maxrate, "-bufsize", &buffer, "-x265-params", &x265_params(gop)]
.into_iter().map(str::to_owned).collect::<Vec<_>>();
// Same as libx264: capped CRF starves repeated keyframes, so bitrate presets use ABR.
match settings.max_kbps() {
Some(k) => options.extend(["-b:v".into(), format!("{}k", k * 3 / 4)]),
None => options.extend(["-crf".into(), settings.cq().to_string()]),
}
options
}
fn encoder_options(settings: &VideoSettings, encoder: Encoder, gop: &str) -> Vec<String> {
let (maxrate, bufsize) = rate_args(settings, encoder);
let cq = settings.cq().to_string();
let target = settings.max_kbps().map_or("16M".to_string(), |k| format!("{}k", k * 3 / 4));
if encoder == Encoder::Vaapi { return vaapi_options(settings, gop); }
let (profile, pix_fmt) = chroma_args(settings, encoder);
if encoder == Encoder::Nvenc {
return ["-c:v", encoder.name_for(settings.codec), "-preset", "p4", "-tune", "ull",
"-profile:v", profile, "-pix_fmt", pix_fmt, "-g", gop, "-bf", "0",
"-rc", "vbr", "-cq", &cq, "-b:v", &target, "-maxrate", &maxrate, "-bufsize", &bufsize,
"-rc-lookahead", "0", "-zerolatency", "1", "-delay", "0", "-aud", "1"]
.into_iter().map(str::to_owned).collect();
}
if settings.codec == VideoCodec::H265 { return cpu_hevc_options(settings, gop); }
let params = format!("aud=1:repeat-headers=1:scenecut=0:keyint={gop}:min-keyint={gop}");
let mut options = vec!["-c:v", "libx264", "-preset", "superfast", "-tune", "zerolatency",
"-profile:v", profile, "-pix_fmt", pix_fmt, "-g", gop, "-threads", "4",
"-maxrate", &maxrate, "-bufsize", &bufsize, "-x264-params", &params];
// Capped CRF starves repeated 4K keyframes; use ABR for bitrate presets.
if settings.max_kbps().is_some() { options.extend(["-b:v", &target]); }
else { options.extend(["-crf", &cq]); }
options.into_iter().map(str::to_owned).collect()
}
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
struct FfmpegSpec {
program: PathBuf,
ld_library_path: Option<OsString>,
path: Option<OsString>,
}
impl FfmpegSpec {
fn command(&self) -> Command {
let mut command = Command::new(&self.program);
if let Some(path) = &self.ld_library_path { command.env("LD_LIBRARY_PATH", path); }
if let Some(path) = &self.path { command.env("PATH", path); }
command
}
}
fn ffmpeg_spec() -> FfmpegSpec {
match crate::config::ffmpeg_root() {
Some(root) => FfmpegSpec {
program: root.join("bin/ffmpeg"),
ld_library_path: Some(root.join("lib/x86_64-linux-gnu").into_os_string()),
path: None,
},
None => FfmpegSpec {
program: PathBuf::from("ffmpeg"),
ld_library_path: None,
path: std::env::var_os("PATH"),
},
}
}
fn ffmpeg_command() -> Command { ffmpeg_spec().command() }
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum FfmpegTimingMode { Modern, Legacy }
fn help_has_option(help: &str, option: &str) -> bool {
help.lines().filter_map(|line| line.split_whitespace().next()).any(|token| {
token == option || token.starts_with(&format!("{option}:") ) || token.starts_with(&format!("{option}["))
})
}
fn timing_mode_from_help(help: &str) -> FfmpegTimingMode {
if help_has_option(help, "-fps_mode") && help_has_option(help, "-enc_time_base") {
FfmpegTimingMode::Modern
} else {
FfmpegTimingMode::Legacy
}
}
fn timing_args(mode: FfmpegTimingMode, fps: u32) -> Vec<String> {
match mode {
FfmpegTimingMode::Modern => vec!["-fps_mode".into(), "vfr".into(), "-enc_time_base".into(), format!("1/{fps}")],
FfmpegTimingMode::Legacy => vec!["-vsync".into(), "0".into()],
}
}
/// Legacy is chosen only from a completed help dump that visibly lacks the
/// modern options. A failed probe (spawn error, non-zero exit, timeout, or no
/// parseable help text) must keep the modern args: `-vsync` is deprecated
/// since FFmpeg 5.1 and may not be accepted by newer builds, so falling back
/// to it on a failed probe would kill the encoder there — worse than guessing.
fn timing_mode_from_probe(success: bool, stdout: &[u8], stderr: &[u8]) -> FfmpegTimingMode {
let mut help = Vec::with_capacity(stdout.len() + stderr.len());
help.extend_from_slice(stdout);
help.extend_from_slice(stderr);
if !success || help.is_empty() { return FfmpegTimingMode::Modern; }
timing_mode_from_help(&String::from_utf8_lossy(&help))
}
async fn probe_timing_mode(spec: &FfmpegSpec) -> FfmpegTimingMode {
let mut command = spec.command();
command.args(["-hide_banner", "-nostdin", "-h", "full"])
.kill_on_drop(true)
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped());
match tokio::time::timeout(Duration::from_secs(3), command.output()).await {
Ok(Ok(output)) => timing_mode_from_probe(output.status.success(), &output.stdout, &output.stderr),
_ => timing_mode_from_probe(false, &[], &[]),
}
}
fn timing_cache() -> &'static Mutex<HashMap<FfmpegSpec, Arc<tokio::sync::OnceCell<FfmpegTimingMode>>>> {
static CACHE: OnceLock<Mutex<HashMap<FfmpegSpec, Arc<tokio::sync::OnceCell<FfmpegTimingMode>>>>> = OnceLock::new();
CACHE.get_or_init(|| Mutex::new(HashMap::new()))
}
fn timing_cell(
cache: &Mutex<HashMap<FfmpegSpec, Arc<tokio::sync::OnceCell<FfmpegTimingMode>>>>,
spec: &FfmpegSpec,
) -> Arc<tokio::sync::OnceCell<FfmpegTimingMode>> {
let mut cache = cache.lock().expect("FFmpeg timing cache poisoned");
cache.entry(spec.clone()).or_insert_with(|| Arc::new(tokio::sync::OnceCell::const_new())).clone()
}
async fn ffmpeg_timing_mode(spec: &FfmpegSpec) -> FfmpegTimingMode {
let cell = timing_cell(timing_cache(), spec);
*cell.get_or_init(|| probe_timing_mode(spec)).await
}
/// A compiled-in encoder name does not prove that its driver can initialize.
/// Probe one synthetic 4:2:0 frame once per process before advertising HEVC.
pub async fn hevc_available() -> bool {
static AVAILABLE: tokio::sync::OnceCell<bool> = tokio::sync::OnceCell::const_new();
*AVAILABLE.get_or_init(|| async {
let requested = Encoder::requested();
let mut candidates = vec![requested];
if requested != Encoder::Cpu { candidates.push(Encoder::Cpu); }
for encoder in candidates {
let settings = VideoSettings { codec: VideoCodec::H265, ..Default::default() };
let mut command = ffmpeg_command();
command.args(["-hide_banner", "-loglevel", "error", "-nostdin"]);
if encoder == Encoder::Vaapi { command.args(["-vaapi_device", &vaapi_device()]); }
command.args(["-f", "lavfi", "-i", "color=black:size=256x256:rate=1", "-frames:v", "1", "-an"]);
if encoder == Encoder::Vaapi { command.args(["-vf", &vaapi_filter(None)]); }
command.args(encoder_options(&settings, encoder, "30"));
command.args(["-f", "hevc", "pipe:1"]).kill_on_drop(true).stderr(std::process::Stdio::null());
if let Ok(Ok(output)) = tokio::time::timeout(Duration::from_secs(5), command.output()).await {
if output.status.success() && is_independent_frame_for(&output.stdout, VideoCodec::H265) {
log::info!("HEVC encoder probe: {} ok", encoder.name_for(VideoCodec::H265));
return true;
}
}
}
log::info!("HEVC encoder probe: unavailable");
false
}).await
}
/// Round UP (+1ns float guard) so webrtc's floor(duration*90000) yields exactly `ticks`.
fn ticks_to_duration(ticks: u64) -> Duration {
Duration::from_nanos((ticks * 1_000_000_000 + RTP_CLOCK - 1) / RTP_CLOCK + 1)
@@ -115,7 +335,7 @@ struct DependencyGate {needs_idr:bool}
impl Default for DependencyGate {fn default()->Self{Self{needs_idr:true}}}
impl DependencyGate{
fn submit(&mut self,tx:&mpsc::Sender<Arc<CompleteFrame>>,frame:Arc<CompleteFrame>){
if self.needs_idr&&!is_independent_frame(&frame.data){return;}
if self.needs_idr&&!is_independent_frame_for(&frame.data, frame.codec){return;}
match tx.try_send(frame){
Ok(())=>self.needs_idr=false,
Err(mpsc::error::TrySendError::Full(_))=>self.needs_idr=true,
@@ -145,16 +365,16 @@ impl Drop for StopOnDrop { fn drop(&mut self) { self.0.store(true, std::sync::at
async fn read_encoder(
size: (u32, u32), display: String, portal:Option<Arc<crate::portal::PortalSession>>,
latest: mpsc::Sender<Arc<CompleteFrame>>,
settings: VideoSettings, sequence: Arc<std::sync::atomic::AtomicU64>, idr: Arc<AtomicBool>,
settings: VideoSettings, sequence: Arc<std::sync::atomic::AtomicU64>, idr: Arc<AtomicBool>, budget: Arc<Semaphore>,
) -> Result<(), String> {
// The sequence is shared so a CPU fallback or settings restart continues it (skip statistics stay correct).
let encoder = Encoder::requested();
if encoder == Encoder::Cpu { return read_encoder_with(size,display,portal,latest,Encoder::Cpu,sequence,settings,idr).await; }
match read_encoder_with(size,display.clone(),portal.clone(),latest.clone(),encoder,sequence.clone(),settings,idr.clone()).await {
if encoder == Encoder::Cpu { return read_encoder_with(size,display,portal,latest,Encoder::Cpu,sequence,settings,idr,budget).await; }
match read_encoder_with(size,display.clone(),portal.clone(),latest.clone(),encoder,sequence.clone(),settings,idr.clone(),budget.clone()).await {
Ok(())=>Ok(()),
Err(error) if !latest.is_closed()=>{
log::warn!("{} stream unavailable; switching scoped encoder to CPU: {}",encoder.name(),error);
read_encoder_with(size,display,portal,latest,Encoder::Cpu,sequence,settings,idr).await
log::warn!("{} stream unavailable; switching scoped encoder to CPU: {}",encoder.name_for(settings.codec),error);
read_encoder_with(size,display,portal,latest,Encoder::Cpu,sequence,settings,idr,budget).await
},
Err(error)=>Err(error),
}
@@ -164,20 +384,21 @@ async fn read_encoder(
#[cfg(feature = "inproc-av")]
async fn read_inprocess(
config: crate::inproc_encoder::Config, latest: mpsc::Sender<Arc<CompleteFrame>>,
sequence: Arc<std::sync::atomic::AtomicU64>, idr: Arc<AtomicBool>,
sequence: Arc<std::sync::atomic::AtomicU64>, idr: Arc<AtomicBool>, budget: Arc<Semaphore>,
) -> Result<(), String> {
let (units_tx, mut units) = mpsc::channel::<Vec<u8>>(4);
let codec = config.codec;
let (units_tx, mut units) = mpsc::channel::<crate::inproc_encoder::EncodedUnit>(4);
let stop = Arc::new(AtomicBool::new(false));
// If this future is dropped (reader task aborted), the blocking encoder stops at its next packet.
let _stop_on_drop = StopOnDrop(stop.clone());
let job = tokio::task::spawn_blocking({
let stop = stop.clone();
move || crate::inproc_encoder::run(config, units_tx, stop, idr)
move || crate::inproc_encoder::run(config, units_tx, stop, idr, budget)
});
let mut gate = DependencyGate::default();
while let Some(data) = units.recv().await {
while let Some(unit) = units.recv().await {
let sequence = sequence.fetch_add(1, std::sync::atomic::Ordering::Relaxed) + 1;
gate.submit(&latest, Arc::new(CompleteFrame { sequence, completed_at: Instant::now(), data }));
gate.submit(&latest, Arc::new(CompleteFrame { codec, sequence, completed_at: unit.completed_at, data: unit.data, _permit: Some(unit.permit) }));
if latest.is_closed() { break; }
}
// Dropping the receiver also unblocks the encoder if it is waiting to send.
@@ -195,17 +416,24 @@ fn buffer_kbits(max_kbps: u32, encoder: Encoder) -> u32 {
if encoder == Encoder::Cpu { max_kbps.clamp(500, 8000) } else { (max_kbps / 8).clamp(100, 500) }
}
fn buffer_for(settings: &VideoSettings, encoder: Encoder) -> u32 {
if settings.codec == VideoCodec::H265 && encoder == Encoder::Cpu {
settings.max_kbps().unwrap_or(30_000) // one second of HEVC bitrate, including high presets
} else { buffer_kbits(settings.max_kbps().unwrap_or(30_000), encoder) }
}
fn rate_args(settings: &VideoSettings, encoder: Encoder) -> (String, String) {
let max_kbps = settings.max_kbps().unwrap_or(30_000);
(format!("{max_kbps}k"), format!("{}k", buffer_kbits(max_kbps, encoder)))
(format!("{max_kbps}k"), format!("{}k", buffer_for(settings, encoder)))
}
async fn read_encoder_with(
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>,
settings: VideoSettings, #[cfg_attr(not(feature = "inproc-av"), allow(unused_variables))] idr: Arc<AtomicBool>, budget: Arc<Semaphore>,
)->Result<(),String>{
let kms=display.starts_with("kms:");
let wayland=kms || display.starts_with("wayland:");
let (permits_tx,mut permits_rx)=mpsc::channel::<OwnedSemaphorePermit>(4);
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()); }
@@ -218,28 +446,23 @@ async fn read_encoder_with(
display: display.clone(), input: size, output: settings.output, fps: u32::from(settings.fps),
gop: gop_frames(settings.fps, true) as u32, cq: settings.cq() as u32,
target_bps: settings.max_kbps().map_or(16_000, |k| i64::from(k) * 3 / 4) * 1000,
max_bps: max_kbps * 1000, buffer_bits: i64::from(buffer_kbits(max_kbps as u32, encoder)) * 1000, encoder,
max_bps: max_kbps * 1000, buffer_bits: i64::from(buffer_for(&settings, encoder)) * 1000, encoder, codec: settings.codec,
damage: crate::inproc_encoder::damage_enabled(),
};
log::info!("Desktop encoder: display={} source={}x{} output={:?} fps={} cq={} maxrate={}k encoder={} pipeline=in-process",
display,size.0,size.1,settings.output,settings.fps,config.cq,max_kbps,encoder.name());
return read_inprocess(config, latest, sequence, idr).await;
display,size.0,size.1,settings.output,settings.fps,config.cq,max_kbps,encoder.name_for(settings.codec));
return read_inprocess(config, latest, sequence, idr, budget).await;
}
let size = format!("{}x{}", size.0, size.1);
// The separate wayland-capture process produces frames at its own fixed rate.
if wayland && portal.is_none() { settings.fps = FPS as u32; }
let fps = settings.fps.to_string();
// Keep the IDR interval at 2 s whatever the frame rate.
let gop = gop_frames(settings.fps, false).to_string();
let (maxrate, bufsize) = rate_args(&settings, encoder);
let (maxrate, _) = rate_args(&settings, encoder);
let cq = settings.cq().to_string();
let target = settings.max_kbps().map_or("16M".to_string(), |k| format!("{}k", k * 3 / 4));
log::info!("Desktop encoder: display={} source={} output={:?} fps={} cq={} maxrate={} encoder={} low_latency=true",display,size,settings.output,settings.fps,cq,maxrate,encoder.name());
log::info!("Desktop encoder: display={} source={} output={:?} fps={} cq={} maxrate={} encoder={} pix_fmt={} low_latency=true",display,size,settings.output,settings.fps,cq,maxrate,encoder.name_for(settings.codec),if encoder == Encoder::Vaapi { "nv12" } else { chroma_args(&settings, encoder).1 });
// A bundled FFmpeg prefix (config ffmpeg_root / UU_FFMPEG_ROOT) or `ffmpeg` on PATH.
let mut command=match crate::config::ffmpeg_root() {
Some(root) => { let mut c=Command::new(root.join("bin/ffmpeg")); c.env("LD_LIBRARY_PATH",root.join("lib/x86_64-linux-gnu")); c }
None => Command::new("ffmpeg"),
};
let ffmpeg = ffmpeg_spec();
let mut command = ffmpeg.command();
command.args(["-hide_banner","-loglevel","error","-nostdin"]);
if encoder == Encoder::Vaapi { command.args(["-vaapi_device", &vaapi_device()]); }
if wayland {
@@ -256,27 +479,35 @@ async fn read_encoder_with(
} else if let Some((width, height)) = settings.output {
command.args(["-vf", &format!("scale={width}:{height}:flags=bilinear")]);
}
if encoder == Encoder::Vaapi {
command.args(vaapi_options(&settings, &gop));
} else if encoder == Encoder::Nvenc {
command.args(["-c:v","h264_nvenc","-preset","p4","-tune","ull",
"-profile:v","baseline","-pix_fmt","yuv420p","-g",&gop,"-bf","0",
"-rc","vbr","-cq",&cq,"-b:v",&target,"-maxrate",&maxrate,"-bufsize",&bufsize,
"-rc-lookahead","0","-zerolatency","1","-delay","0","-aud","1"]);
}else{
let x264 = format!("aud=1:repeat-headers=1:scenecut=0:keyint={gop}:min-keyint={gop}");
command.args([
"-c:v", "libx264", "-preset", "superfast", "-tune", "zerolatency",
"-profile:v", "baseline", "-pix_fmt", "yuv420p", "-g", &gop, "-threads", "4",
"-crf", &cq, "-maxrate", &maxrate, "-bufsize", &bufsize,
"-x264-params", &x264]);
}
command.args(encoder_options(&settings, encoder, &gop));
// x11grab paces against an absolute schedule; after a capture stall it grabs
// back-to-back to "catch up" (measured 69 frames in 200ms). vfr on a 1/FPS
// timebase drops those same-slot frames instead of encoding a burst.
let time_base = format!("1/{fps}");
if wayland { command.stdin(std::process::Stdio::piped()); }
let mut child=command.args(["-fps_mode","vfr","-enc_time_base",&time_base,"-f","h264","pipe:1"])
// timebase drops those same-slot frames instead of encoding a burst. The
// legacy -vsync 0 path (FFmpeg 4.x) passes catch-up frames straight through
// and encodes the whole burst; a known degradation on old FFmpeg.
// Native Wayland: the capture process writes straight into the encoder's stdin;
// relaying 33 MB 4K frames through this process cost a full extra copy per frame.
let mut native_capture=None;
if wayland && kms_capture.is_none() && portal.is_none() {
// Linux keeps the running executable inode accessible after a scoped
// development/release replacement; current_exe may name a deleted path.
#[cfg(target_os="linux")]
let executable=std::path::PathBuf::from("/proc/self/exe");
#[cfg(not(target_os="linux"))]
let executable=std::env::current_exe().map_err(|e|format!("Capture executable: {e}"))?;
let mut capture=wayland_capture_command(&executable,settings.fps)
.env("UU_WAYLAND_DEMAND","1")
.kill_on_drop(true).stdin(std::process::Stdio::piped()).stdout(std::process::Stdio::piped()).stderr(std::process::Stdio::inherit())
.spawn().map_err(|e|format!("Start Wayland capture: {e}"))?;
let source:std::process::Stdio=capture.stdout.take().ok_or("Capture stdout missing")?
.try_into().map_err(|e|format!("Capture pipe: {e}"))?;
command.stdin(source);
native_capture=Some(capture);
} else if wayland { command.stdin(std::process::Stdio::piped()); }
let timing = timing_args(ffmpeg_timing_mode(&ffmpeg).await, settings.fps);
let mut output_args = timing;
output_args.extend(["-f".into(), if settings.codec == VideoCodec::H265 { "hevc".into() } else { "h264".into() }, "pipe:1".into()]);
let mut child=command.args(output_args)
.kill_on_drop(true)
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::inherit())
@@ -288,11 +519,13 @@ async fn read_encoder_with(
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);
let mut bytes=vec![0;capture.width as usize*capture.height as usize*4];
loop {
tick.tick().await;
let permit=budget.clone().acquire_owned().await.map_err(|_|"Encoder stopped")?;
permits_tx.send(permit).await.map_err(|_|"Encoder stopped")?;
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];
bytes=tokio::task::spawn_blocking(move || {
capture.capture(&mut bytes)?; Ok::<_,String>(bytes)
}).await.map_err(|_|"KMS worker failed")??;
sink.write_all(&bytes).await.map_err(|_|"KMS encoder pipe failed")?;
@@ -305,33 +538,42 @@ async fn read_encoder_with(
let interval=Duration::from_nanos(1_000_000_000/u64::from(settings.fps));
let mut tick=tokio::time::interval(interval);
tick.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
let mut frame=vec![0;(session.width as usize)*(session.height as usize)*4];
let mut version=None;
loop {
tick.tick().await;
let permit=budget.clone().acquire_owned().await.map_err(|_|"Encoder stopped")?;
permits_tx.send(permit).await.map_err(|_|"Encoder stopped")?;
let session=session.clone();
let frame=tokio::task::spawn_blocking(move || {
let mut frame=vec![0;(session.width as usize)*(session.height as usize)*4];
session.capture(&mut frame)?;
Ok::<_,String>(frame)
(frame,version)=tokio::task::spawn_blocking(move || {
session.capture_changed(&mut frame,&mut version)?;
Ok::<_,String>((frame,version))
}).await.map_err(|e|format!("Portal capture worker: {e}"))??;
sink.write_all(&frame).await.map_err(|e|format!("Portal encoder pipe: {e}"))?;
}
})))
} else if wayland {
// Linux keeps the running executable inode accessible after a scoped
// development/release replacement; current_exe may name a deleted path.
#[cfg(target_os="linux")]
let executable=std::path::PathBuf::from("/proc/self/exe");
#[cfg(not(target_os="linux"))]
let executable=std::env::current_exe().map_err(|e|format!("Capture executable: {e}"))?;
let mut capture=Command::new(executable).arg("wayland-capture")
.kill_on_drop(true).stdout(std::process::Stdio::piped()).stderr(std::process::Stdio::inherit())
.spawn().map_err(|e|format!("Start Wayland capture: {e}"))?;
let mut source=capture.stdout.take().ok_or("Capture stdout missing")?;
let mut sink=child.stdin.take().ok_or("Encoder stdin missing")?;
} else if let Some(mut capture)=native_capture {
let mut demand=capture.stdin.take().ok_or("Capture stdin missing")?;
Some(EncoderTask(tokio::spawn(async move {
let result=tokio::io::copy(&mut source,&mut sink).await;
drop(capture);
result.map(|_|()).map_err(|e|format!("Wayland frame pipe: {e}"))
use tokio::io::AsyncWriteExt;
tokio::select! {
result=async {
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 {
let permit=budget.clone().acquire_owned().await.map_err(|_|"Encoder stopped")?;
tick.tick().await;
permits_tx.send(permit).await.map_err(|_|"Encoder stopped")?;
demand.write_all(&[1]).await.map_err(|_|"Capture demand pipe closed")?;
}
} => result,
status=capture.wait() => {
let status=status.map_err(|e|format!("Wayland capture: {e}"))?;
// Nothing awaits this task; log so the cause survives the generic encoder EOF.
log::warn!("Wayland capture exited: {status}");
Err(format!("Wayland capture exited: {status}"))
},
}
})))
} else { None };
let mut stdout = child.stdout.take().ok_or("Encoder stdout missing")?;
@@ -344,14 +586,15 @@ async fn read_encoder_with(
pending.extend_from_slice(&chunk[..count]);
// A chunk can include the next AUD, so split before enforcing the bound.
loop {
let aud = aud_offsets(&pending);
let aud = aud_offsets_for(&pending, settings.codec);
if aud.len() < 2 { break; }
let next = aud[1];
if next > MAX_ACCESS_UNIT { return Err("Encoder access unit exceeds limit".into()); }
let frame: Vec<u8> = pending.drain(..next).collect();
let sequence = sequence.fetch_add(1, std::sync::atomic::Ordering::Relaxed) + 1;
let permit=if wayland {Some(permits_rx.recv().await.ok_or("Capture producer stopped")?)} else {None};
gate.submit(&latest, Arc::new(CompleteFrame {
sequence, completed_at: Instant::now(), data: frame,
codec: settings.codec, sequence, completed_at: Instant::now(), data: frame, _permit: permit,
}));
if latest.is_closed() { return Ok(()); }
}
@@ -359,6 +602,12 @@ async fn read_encoder_with(
}
}
fn wayland_capture_command(executable:&std::path::Path,fps:u32)->Command {
let mut command=Command::new(executable);
command.arg("wayland-capture").env("UU_WAYLAND_FPS",fps.to_string());
command
}
fn transport_closed(pc: &RTCPeerConnection) -> bool {
matches!(pc.connection_state(), RTCPeerConnectionState::Closed | RTCPeerConnectionState::Failed)
|| pc.sctp().state() == webrtc::sctp_transport::sctp_transport_state::RTCSctpTransportState::Closed
@@ -373,7 +622,15 @@ pub async fn probe(size:(u32,u32),display:String,portal:Option<Arc<crate::portal
};
let (tx,mut rx)=mpsc::channel(4);
let sequence=Arc::new(std::sync::atomic::AtomicU64::new(0));
let _reader=EncoderTask(tokio::spawn(read_encoder(size,display,portal,tx,VideoSettings::default(),sequence,Arc::new(AtomicBool::new(false)))));
let fps=crate::wayland::capture_fps(std::env::var("UU_PROBE_FPS").ok().as_deref())?;
// UU_PROBE_QUALITY selects a viewer preset (1 fast 1M, 2 general 2M, 3 HD 8M, 4 bluray 20M,
// 5 auto); bitrate presets exercise the capped/ABR encoder path instead of CRF.
let quality=match std::env::var("UU_PROBE_QUALITY").ok().as_deref() {
None=>crate::capture_setting::QUALITY_AUTO,
Some(v)=>v.parse::<u64>().ok().filter(|q|(1..=5).contains(q)).ok_or("UU_PROBE_QUALITY must be 1-5")?,
};
let settings=VideoSettings {fps,quality,..VideoSettings::default()};
let _reader=EncoderTask(tokio::spawn(read_encoder(size,display,portal,tx,settings,sequence,Arc::new(AtomicBool::new(false)),Arc::new(Semaphore::new(4)))));
let mut count=0;
let mut bytes=0;
// UU_PROBE_FRAMES lengthens the sample for baseline measurements; default is 5 frames.
@@ -391,8 +648,8 @@ pub async fn probe(size:(u32,u32),display:String,portal:Option<Arc<crate::portal
Ok(())
}
pub async fn stream_desktop(pc: Arc<RTCPeerConnection>, track: Arc<TrackLocalStaticSample>, size: (u32, u32), display: String, portal:Option<Arc<crate::portal::PortalSession>>,
mut settings: tokio::sync::watch::Receiver<VideoSettings>, idr: Arc<AtomicBool>) -> Result<(), String> {
pub async fn stream_desktop(pc: Arc<RTCPeerConnection>, track: Arc<crate::video_track::VideoTrack>, size: (u32, u32), display: String, portal:Option<Arc<crate::portal::PortalSession>>,
mut settings: tokio::sync::watch::Receiver<VideoSettings>, applied: Arc<tokio::sync::watch::Sender<VideoSettings>>, idr: Arc<AtomicBool>) -> Result<(), String> {
loop {
if transport_closed(&pc) { return Ok(()); }
if pc.connection_state() == RTCPeerConnectionState::Connected
@@ -401,9 +658,15 @@ pub async fn stream_desktop(pc: Arc<RTCPeerConnection>, track: Arc<TrackLocalSta
}
crate::capture::keep_display_awake()?;
let (latest, mut receiver) = mpsc::channel(4);
let budget=Arc::new(Semaphore::new(4));
let sequence = Arc::new(std::sync::atomic::AtomicU64::new(0));
let current = *settings.borrow_and_update();
let mut reader = EncoderTask(tokio::spawn(read_encoder(size, display.clone(), portal.clone(), latest.clone(), current, sequence.clone(), idr.clone())));
let mut current = *settings.borrow_and_update();
if !track.supports(current.codec) && track.supports(VideoCodec::H265) {
current.codec = VideoCodec::H265; // H265-only remote video offer.
applied.send_replace(current);
settings.borrow_and_update();
}
let mut reader = EncoderTask(tokio::spawn(read_encoder(size, display.clone(), portal.clone(), latest.clone(), current, sequence.clone(), idr.clone(), budget.clone())));
let mut previous = None;
let mut timeline = RtpTimeline { fps: u64::from(current.fps), ..Default::default() };
let mut settings_open = true;
@@ -414,21 +677,47 @@ pub async fn stream_desktop(pc: Arc<RTCPeerConnection>, track: Arc<TrackLocalSta
let mut health = tokio::time::interval(Duration::from_millis(200));
health.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
let mut stats = Instant::now();
loop {
// sent/skipped are cumulative; window_* and max_* cover the interval since the last stats line.
let (mut window_frames, mut window_skipped) = (0u64, 0u64);
let mut send_interrupted=false;
'stream: loop {
let next = *settings.borrow_and_update();
if next != current || send_interrupted {
let discard = next.codec != current.codec || send_interrupted;
current = next;
timeline.fps = u64::from(next.fps);
if discard {
reader.0.abort();
let _ = (&mut reader.0).await;
while receiver.try_recv().is_ok() {}
reader.0 = tokio::spawn(read_encoder(size, display.clone(), portal.clone(), latest.clone(), next, sequence.clone(), idr.clone(), budget.clone()));
} else {
reader.restart(read_encoder(size, display.clone(), portal.clone(), latest.clone(), next, sequence.clone(), idr.clone(), budget.clone())).await;
}
send_interrupted=false;
}
tokio::select! {
result = &mut reader.0 => return result.map_err(|e| format!("Encoder reader task: {e}"))?,
result = &mut reader.0 => {
let result = result.map_err(|e| format!("Encoder reader task: {e}"))?;
if let (Err(error), VideoCodec::H265, true) = (&result, current.codec, track.supports(VideoCodec::H264)) {
log::warn!("HEVC stream failed ({error}); falling back to negotiated H264");
applied.send_modify(|value| value.codec = VideoCodec::H264);
current = *settings.borrow_and_update();
while receiver.try_recv().is_ok() {}
reader.0 = tokio::spawn(read_encoder(size, display.clone(), portal.clone(), latest.clone(), current, sequence.clone(), idr.clone(), budget.clone()));
continue;
}
return result;
},
_ = health.tick() => { if transport_closed(&pc) { return Ok(()); } },
changed = settings.changed(), if settings_open => {
if changed.is_err() { settings_open = false; continue; }
// Restart the encoder with the viewer's new 帧率/画质; it begins with an IDR, and frames
// still queued from the old encoder remain a valid chain before it.
let next = *settings.borrow_and_update();
timeline.fps = u64::from(next.fps);
reader.restart(read_encoder(size, display.clone(), portal.clone(), latest.clone(), next, sequence.clone(), idr.clone())).await;
if changed.is_err() { settings_open = false; }
continue;
},
frame = receiver.recv() => {
// A closed queue means the reader ended; its JoinHandle carries the reason.
let Some(frame) = frame else { return (&mut reader.0).await.map_err(|e| format!("Encoder reader task: {e}"))?; };
if frame.codec != current.codec { continue; }
let duration = timeline.duration_after(frame.completed_at);
if let Some(last) = previous { skipped += frame.sequence.saturating_sub(last + 1); }
max_age_ms = max_age_ms.max(frame.completed_at.elapsed().as_millis());
@@ -437,13 +726,17 @@ pub async fn stream_desktop(pc: Arc<RTCPeerConnection>, track: Arc<TrackLocalSta
data: frame.data.clone().into(), duration, ..Default::default()
};
let extensions = [crate::rtp_metadata::capture_index(0), crate::rtp_metadata::new_frame()];
let send = track.write_sample_with_extensions(&sample, &extensions);
let send = track.write_sample_with_extensions(frame.codec, &sample, &extensions);
let send_started=Instant::now();
tokio::pin!(send);
loop {
tokio::select! {
result = &mut send => { result.map_err(|e| format!("Video send: {e}"))?; break; },
_ = health.tick() => { if transport_closed(&pc) { return Ok(()); } },
changed = settings.changed(), if settings_open => {
if changed.is_err() { settings_open=false; }
else if *settings.borrow()!=current { send_interrupted=true; continue 'stream; }
},
}
}
previous = Some(frame.sequence);
@@ -451,7 +744,9 @@ pub async fn stream_desktop(pc: Arc<RTCPeerConnection>, track: Arc<TrackLocalSta
frames += 1;
if stats.elapsed() >= Duration::from_secs(30) {
crate::capture::keep_display_awake()?;
log::info!("Desktop latest-frame stats: sent={} skipped={} latest_seq={} max_slot_age_ms={} max_send_ms={}", frames, skipped, frame.sequence, max_age_ms,max_send_ms);
log::info!("Desktop latest-frame stats: sent={} skipped={} latest_seq={} window_s={} window_sent={} window_skipped={} max_slot_age_ms={} max_send_ms={}",
frames, skipped, frame.sequence, stats.elapsed().as_secs(), frames - window_frames, skipped - window_skipped, max_age_ms, max_send_ms);
(window_frames, window_skipped) = (frames, skipped);
stats = Instant::now();
max_age_ms = 0;
max_send_ms = 0;
@@ -463,7 +758,105 @@ pub async fn stream_desktop(pc: Arc<RTCPeerConnection>, track: Arc<TrackLocalSta
#[cfg(test)]
mod tests {
#[test]
fn native_wayland_child_uses_requested_capture_rate() {
for fps in [5,30,60,90,144] {
let command=super::wayland_capture_command(std::path::Path::new("uuremote-server"),fps);
let value=fps.to_string();
assert!(command.as_std().get_envs().any(|(k,v)|k=="UU_WAYLAND_FPS" && v==Some(std::ffi::OsStr::new(&value))));
}
}
use super::*;
#[tokio::test]
async fn frame_budget_spans_queued_sending_and_restarted_encoders() {
let budget=Arc::new(Semaphore::new(2));
let (tx,mut rx)=mpsc::channel(4);
let mut gate=DependencyGate::default();
for sequence in 1..=2 {
let permit=budget.clone().acquire_owned().await.unwrap();
gate.submit(&tx,Arc::new(CompleteFrame {codec:VideoCodec::H264,sequence,completed_at:Instant::now(),
data:vec![0,0,1,5,42],_permit:Some(permit)}));
}
let sending=rx.recv().await.unwrap();
assert_eq!(budget.available_permits(),0);
let restarted=tokio::spawn(budget.clone().acquire_owned());
tokio::task::yield_now().await;
assert!(!restarted.is_finished());
drop(sending);
let permit=tokio::time::timeout(Duration::from_secs(1),restarted).await.unwrap().unwrap().unwrap();
assert_eq!(budget.available_permits(),0);
drop(rx);
assert_eq!(budget.available_permits(),1);
drop(permit);
assert_eq!(budget.available_permits(),2);
}
#[test]
fn timing_help_requires_both_modern_options() {
let modern = " -fps_mode mode video sync method\n -enc_time_base ratio set encoder timebase";
assert_eq!(timing_mode_from_help(modern), FfmpegTimingMode::Modern);
assert_eq!(timing_mode_from_help(" -vsync parameter\n"), FfmpegTimingMode::Legacy);
assert_eq!(timing_mode_from_help(" -fps_mode mode\n"), FfmpegTimingMode::Legacy);
assert_eq!(timing_mode_from_help(" -enc_time_base ratio\n"), FfmpegTimingMode::Legacy);
assert_eq!(timing_mode_from_help(" --not-fps_mode-test\n"), FfmpegTimingMode::Legacy);
}
#[test]
fn timing_args_match_ffmpeg_generation() {
assert_eq!(timing_args(FfmpegTimingMode::Modern, 60),
vec!["-fps_mode", "vfr", "-enc_time_base", "1/60"]);
assert_eq!(timing_args(FfmpegTimingMode::Legacy, 30), vec!["-vsync", "0"]);
assert!(!timing_args(FfmpegTimingMode::Modern, 60).contains(&"-r".to_owned()));
}
#[test]
fn timing_probe_failure_keeps_modern_args() {
let modern = b" -fps_mode mode\n -enc_time_base ratio\n";
// Legacy is only confirmed from a completed help dump that lacks the options.
assert_eq!(timing_mode_from_probe(true, b"", b" -fps_mode mode\n"), FfmpegTimingMode::Legacy);
// Any probe failure keeps the modern args (-vsync is deprecated on new FFmpeg).
assert_eq!(timing_mode_from_probe(false, modern, &[]), FfmpegTimingMode::Modern);
assert_eq!(timing_mode_from_probe(false, &[], &[]), FfmpegTimingMode::Modern);
assert_eq!(timing_mode_from_probe(true, &[], &[]), FfmpegTimingMode::Modern);
assert_eq!(timing_mode_from_probe(true, modern, &[]), FfmpegTimingMode::Modern);
}
#[tokio::test]
async fn timing_cache_initializer_runs_once_for_concurrent_requests() {
use std::sync::atomic::{AtomicUsize, Ordering};
let cell = Arc::new(tokio::sync::OnceCell::new());
let calls = Arc::new(AtomicUsize::new(0));
let first = cell.clone();
let first_calls = calls.clone();
let second = cell.clone();
let second_calls = calls.clone();
let (first_mode, second_mode) = tokio::join!(
async move { *first.get_or_init(|| async {
first_calls.fetch_add(1, Ordering::SeqCst);
tokio::task::yield_now().await;
FfmpegTimingMode::Legacy
}).await },
async move { *second.get_or_init(|| async {
second_calls.fetch_add(1, Ordering::SeqCst);
tokio::task::yield_now().await;
FfmpegTimingMode::Modern
}).await },
);
assert_eq!(first_mode, second_mode);
assert_eq!(calls.load(Ordering::SeqCst), 1);
}
#[test]
fn ffmpeg_specs_separate_executable_environments() {
let path = FfmpegSpec { program: PathBuf::from("ffmpeg"), ld_library_path: None,
path: Some(OsString::from("/one")) };
let other_path = FfmpegSpec { program: PathBuf::from("ffmpeg"), ld_library_path: None,
path: Some(OsString::from("/two")) };
let bundled = FfmpegSpec { program: PathBuf::from("/opt/ffmpeg/bin/ffmpeg"),
ld_library_path: Some(OsString::from("/opt/ffmpeg/lib/x86_64-linux-gnu")), path: None };
assert_ne!(path, other_path);
assert_ne!(path, bundled);
}
#[test]
fn x264_buffer_fits_keyframes() {
assert_eq!(buffer_kbits(2000, Encoder::Nvenc), 250);
@@ -501,8 +894,8 @@ mod tests {
let (tx, mut rx) = mpsc::channel(4);
let old_tx = tx.clone();
let (started_tx, started_rx) = tokio::sync::oneshot::channel();
let frame = |sequence, idr| Arc::new(CompleteFrame {
sequence, completed_at: Instant::now(),
let frame = |sequence, idr| Arc::new(CompleteFrame { _permit: None,
sequence, codec: VideoCodec::H264, completed_at: Instant::now(),
data: vec![0, 0, 1, 9, 240, 0, 0, 1, if idr { 5 } else { 1 }, 42],
});
let mut reader = EncoderTask(tokio::spawn(async move {
@@ -596,7 +989,7 @@ mod tests {
async fn bounded_queue_never_sends_broken_prediction_chain_after_overload(){
let(tx,mut rx)=mpsc::channel(1);
let mut gate=DependencyGate::default();
let frame=|sequence,idr|Arc::new(CompleteFrame{sequence,completed_at:Instant::now(),data:vec![0,0,1,9,240,0,0,1,if idr{5}else{1},42]});
let frame=|sequence,idr|Arc::new(CompleteFrame{_permit:None,sequence,codec:VideoCodec::H264,completed_at:Instant::now(),data:vec![0,0,1,9,240,0,0,1,if idr{5}else{1},42]});
gate.submit(&tx,frame(1,false));assert!(rx.try_recv().is_err()); // no IDR yet
gate.submit(&tx,frame(2,true));
gate.submit(&tx,frame(3,false)); // queue full: P3 lost
@@ -606,6 +999,60 @@ mod tests {
gate.submit(&tx,frame(31,true));assert_eq!(rx.recv().await.unwrap().sequence,31);
gate.submit(&tx,frame(32,false));assert_eq!(rx.recv().await.unwrap().sequence,32);
}
#[tokio::test]
#[ignore = "requires installed FFmpeg with a working HEVC encoder"]
async fn hevc_capability_probe_initializes_real_encoder() {
assert!(hevc_available().await);
}
#[test]
fn hevc_annex_b_boundaries_and_irap_use_two_byte_headers() {
let bytes = [0,0,0,1,70,1,80,0,0,1,64,1,42,0,0,1,38,1,42,0,0,1,70,1,80];
assert_eq!(aud_offsets_for(&bytes, VideoCodec::H265), vec![0,19]);
assert!(is_independent_frame_for(&bytes[..19], VideoCodec::H265));
for kind in 16..=21 {
assert!(is_independent_frame_for(&[0,0,1,kind << 1,1,42], VideoCodec::H265));
}
assert!(!is_independent_frame_for(&[0,0,1,2,1,42], VideoCodec::H265));
assert!(!is_independent_frame_for(&[0,0,1,38], VideoCodec::H265));
assert!(!is_independent_frame_for(&[0,0,1,38,0,42], VideoCodec::H265));
assert!(!is_independent_frame_for(&[0,0,1,166,1,42], VideoCodec::H265));
assert!(!is_independent_frame_for(&[0,0,1,38,1,42,0,0,1,2,1,42], VideoCodec::H265));
}
#[tokio::test]
async fn hevc_dependency_gate_recovers_only_at_irap_after_overflow() {
let (tx, mut rx) = mpsc::channel(1);
let mut gate = DependencyGate::default();
let frame = |sequence, irap| Arc::new(CompleteFrame { _permit: None, codec: VideoCodec::H265,
sequence, completed_at: Instant::now(), data: vec![0,0,1,if irap { 38 } else { 2 },1,42] });
gate.submit(&tx, frame(1, false));
assert!(rx.try_recv().is_err());
gate.submit(&tx, frame(2, true));
gate.submit(&tx, frame(3, false));
assert_eq!(rx.recv().await.unwrap().sequence, 2);
gate.submit(&tx, frame(4, false));
assert!(rx.try_recv().is_err());
gate.submit(&tx, frame(5, true));
assert_eq!(rx.recv().await.unwrap().sequence, 5);
}
#[test]
fn hevc_options_keep_420_low_latency_and_existing_rate_policy() {
let settings = VideoSettings { codec: VideoCodec::H265, ..Default::default() };
let cpu = cpu_hevc_options(&settings, "30");
for pair in [["-c:v", "libx265"], ["-profile:v", "main"], ["-pix_fmt", "yuv420p"],
["-bf", "0"], ["-bufsize", "30000k"]] {
assert!(cpu.windows(2).any(|window| window == pair));
}
let params = x265_params("30");
for option in ["aud=1", "repeat-headers=1", "bframes=0", "rc-lookahead=0", "keyint=30", "min-keyint=30"] {
assert!(params.split(':').any(|value| value == option));
}
let vaapi = vaapi_options(&settings, "30");
for pair in [["-c:v", "hevc_vaapi"], ["-profile:v", "main"], ["-bf", "0"], ["-bufsize", "500k"]] {
assert!(vaapi.windows(2).any(|window| window == pair));
}
assert_eq!(Encoder::Nvenc.name_for(VideoCodec::H265), "hevc_nvenc");
assert_eq!(Encoder::Cpu.name_for(VideoCodec::H264), "libx264");
}
#[test]
fn nvenc_idr_with_parameter_sets_is_independent(){
// AUD, SPS, PPS, IDR slice as h264_nvenc emits every GOP (verified on the 3090).
+177
View File
@@ -0,0 +1,177 @@
//! One negotiated video m-line/SSRC, with codec-specific payload types.
//! A single RTP sequence/timestamp survives format changes (never two independent SSRC timelines).
use std::{any::Any, sync::atomic::{AtomicBool, Ordering}};
use async_trait::async_trait;
use tokio::sync::Mutex;
use webrtc::{error::{Error, Result}, media::Sample, rtp::{packet::Packet, header::Header, packetizer::Payloader}};
use webrtc::rtp_transceiver::rtp_codec::{RTCRtpCodecCapability, RTCRtpCodecParameters, RTPCodecType};
use webrtc::track::track_local::{TrackLocal, TrackLocalContext, track_local_static_rtp::TrackLocalStaticRTP};
use crate::capture_setting::VideoCodec;
/// Official SDK GetSupportedFormats constructs H265 with an empty parameter map.
pub fn register_hevc(media: &mut webrtc::api::media_engine::MediaEngine) -> Result<()> {
media.register_codec(RTCRtpCodecParameters { capability: RTCRtpCodecCapability {
mime_type: VideoCodec::H265.mime().into(), clock_rate: 90_000,
rtcp_feedback: vec![webrtc::rtp_transceiver::RTCPFeedback { typ: "nack".into(), parameter: String::new() },
webrtc::rtp_transceiver::RTCPFeedback { typ: "nack".into(), parameter: "pli".into() }],
..Default::default() }, payload_type: 126, ..Default::default() }, RTPCodecType::Video)
}
#[derive(Debug)]
struct Timeline { sequence: u16, timestamp: u32, h264: webrtc::rtp::codecs::h264::H264Payloader }
#[derive(Debug)]
pub struct VideoTrack {
h264: TrackLocalStaticRTP, h265: TrackLocalStaticRTP,
has_h264: AtomicBool, has_h265: AtomicBool, timeline: Mutex<Timeline>,
progress: tokio::sync::watch::Sender<(u64, VideoCodec)>,
}
impl VideoTrack {
pub fn new() -> Self {
let track = |codec: VideoCodec| TrackLocalStaticRTP::new(
RTCRtpCodecCapability { mime_type: codec.mime().into(), clock_rate: 90_000, ..Default::default() },
"video_0".into(), "video_0".into());
let (progress, _) = tokio::sync::watch::channel((0, VideoCodec::H264));
Self { progress, h264: track(VideoCodec::H264), h265: track(VideoCodec::H265), has_h264: AtomicBool::new(false),
has_h265: AtomicBool::new(false), timeline: Mutex::new(Timeline { sequence: rand::random(), timestamp: rand::random(), h264: Default::default() }) }
}
pub fn supports(&self, codec: VideoCodec) -> bool {
match codec { VideoCodec::H264 => &self.has_h264, VideoCodec::H265 => &self.has_h265 }.load(Ordering::Acquire)
}
pub fn revision(&self) -> u64 { self.progress.borrow().0 }
/// Format-change RPC success follows a transmitted frame, including a negotiated fallback.
pub async fn wait_for_applied(&self, since: u64, settings: &tokio::sync::watch::Sender<crate::capture_setting::VideoSettings>) -> bool {
let mut progress = self.progress.subscribe();
tokio::time::timeout(std::time::Duration::from_secs(10), async {
loop {
let (revision, codec) = *progress.borrow_and_update();
if revision > since && codec == settings.borrow().codec { return true; }
if progress.changed().await.is_err() { return false; }
}
}).await.unwrap_or(false)
}
pub async fn write_sample_with_extensions(&self, codec: VideoCodec, sample: &Sample, extensions: &[webrtc::rtp::extension::HeaderExtension]) -> Result<()> {
if !self.supports(codec) { return Err(Error::ErrUnsupportedCodec); }
let track = match codec { VideoCodec::H264 => &self.h264, VideoCodec::H265 => &self.h265 };
if track.all_binding_paused().await { return Ok(()); }
let mut state = self.timeline.lock().await;
// Leave room for the RTP header and negotiated UU extensions below the 1200 byte MTU.
let payloads = match codec {
VideoCodec::H264 => state.h264.payload(1160, &sample.data).map_err(Error::Rtp)?,
VideoCodec::H265 => crate::hevc::payload(1160, &sample.data).map_err(Error::new)?,
};
let count = payloads.len();
let mut complete = count > 0;
let timestamp = state.timestamp;
state.timestamp = state.timestamp.wrapping_add((sample.duration.as_secs_f64() * 90_000.0) as u32);
for (index, payload) in payloads.into_iter().enumerate() {
let packet = Packet { header: Header { version: 2, sequence_number: state.sequence, timestamp,
marker: index + 1 == count, ..Default::default() }, payload };
state.sequence = state.sequence.wrapping_add(1);
complete &= track.write_rtp_with_extensions(&packet, extensions).await? > 0;
}
if complete { self.progress.send_modify(|value| { value.0 += 1; value.1 = codec; }); }
Ok(())
}
}
#[async_trait]
impl TrackLocal for VideoTrack {
async fn bind(&self, context: &TrackLocalContext) -> Result<RTCRtpCodecParameters> {
let h264 = self.h264.bind(context).await.ok();
let h265 = self.h265.bind(context).await.ok();
self.has_h264.store(h264.is_some(), Ordering::Release);
self.has_h265.store(h265.is_some(), Ordering::Release);
log::info!("Video track negotiated: h264={} h265={}", h264.is_some(), h265.is_some());
h264.or(h265).ok_or(Error::ErrUnsupportedCodec)
}
async fn unbind(&self, context: &TrackLocalContext) -> Result<()> {
if self.has_h264.swap(false, Ordering::AcqRel) { self.h264.unbind(context).await?; }
if self.has_h265.swap(false, Ordering::AcqRel) { self.h265.unbind(context).await?; }
Ok(())
}
fn id(&self) -> &str { "video_0" }
fn stream_id(&self) -> &str { "video_0" }
fn kind(&self) -> RTPCodecType { RTPCodecType::Video }
fn as_any(&self) -> &dyn Any { self }
}
#[cfg(test)]
mod tests {
use super::*;
use webrtc::{api::{APIBuilder, media_engine::MediaEngine}, peer_connection::configuration::RTCConfiguration};
async fn negotiate(h264: bool, hevc: bool) {
let mut remote_media = MediaEngine::default();
if h264 { remote_media.register_codec(RTCRtpCodecParameters { capability: RTCRtpCodecCapability {
mime_type: VideoCodec::H264.mime().into(), clock_rate: 90_000,
sdp_fmtp_line: "level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42e01f".into(), ..Default::default()
}, payload_type: 102, ..Default::default() }, RTPCodecType::Video).unwrap(); }
if hevc { register_hevc(&mut remote_media).unwrap(); }
let mut setting = webrtc::api::setting_engine::SettingEngine::default();
setting.set_ice_multicast_dns_mode(webrtc::ice::mdns::MulticastDnsMode::Disabled);
// UDP over IPv4 only: pairing every host interface (VPNs, IPv6) made ICE exceed the
// connect timeout when the whole suite runs in parallel.
setting.set_network_types(vec![webrtc::ice::network_type::NetworkType::Udp4]);
let remote = APIBuilder::new().with_setting_engine(setting.clone()).with_media_engine(remote_media).build().new_peer_connection(RTCConfiguration::default()).await.unwrap();
remote.add_transceiver_from_kind(RTPCodecType::Video, None).await.unwrap();
let mut media = MediaEngine::default(); media.register_default_codecs().unwrap(); register_hevc(&mut media).unwrap();
let host = APIBuilder::new().with_setting_engine(setting).with_media_engine(media).build().new_peer_connection(RTCConfiguration::default()).await.unwrap();
let (packets_tx, mut packets_rx) = tokio::sync::mpsc::unbounded_channel();
remote.on_track(Box::new(move |remote_track, _, _| {
let packets_tx = packets_tx.clone();
Box::pin(async move {
tokio::spawn(async move {
while let Ok((packet, _)) = remote_track.read_rtp().await {
if packets_tx.send(packet).is_err() { break; }
}
});
})
}));
let track = std::sync::Arc::new(VideoTrack::new());
host.add_track(track.clone()).await.unwrap();
let offer = remote.create_offer(None).await.unwrap();
let mut gathered = remote.gathering_complete_promise().await;
remote.set_local_description(offer).await.unwrap();
gathered.recv().await;
host.set_remote_description(remote.local_description().await.unwrap()).await.unwrap();
let answer = host.create_answer(None).await.unwrap();
assert_eq!(answer.sdp.contains("H265/90000"), hevc);
assert_eq!(answer.sdp.contains("H264/90000"), h264);
let mut gathered = host.gathering_complete_promise().await;
host.set_local_description(answer).await.unwrap();
gathered.recv().await;
remote.set_remote_description(host.local_description().await.unwrap()).await.unwrap();
tokio::time::timeout(std::time::Duration::from_secs(10), async {
while !track.supports(VideoCodec::H264) && !track.supports(VideoCodec::H265) { tokio::time::sleep(std::time::Duration::from_millis(10)).await; }
}).await.unwrap();
assert_eq!(track.supports(VideoCodec::H264), h264);
assert_eq!(track.supports(VideoCodec::H265), hevc);
tokio::time::timeout(std::time::Duration::from_secs(30), async {
while host.connection_state() != webrtc::peer_connection::peer_connection_state::RTCPeerConnectionState::Connected {
tokio::time::sleep(std::time::Duration::from_millis(10)).await;
}
}).await.unwrap();
let codecs = if !h264 { vec![VideoCodec::H265] } else if hevc { vec![VideoCodec::H264, VideoCodec::H265, VideoCodec::H264] } else { vec![VideoCodec::H264] };
let mut previous: Option<Packet> = None;
for codec in codecs {
let data = match codec { VideoCodec::H264 => bytes::Bytes::from_static(&[0,0,1,65,1,2]),
VideoCodec::H265 => bytes::Bytes::from_static(&[0,0,1,38,1,2]) };
let (settings, _) = tokio::sync::watch::channel(crate::capture_setting::VideoSettings { codec, ..Default::default() });
let applied = track.wait_for_applied(track.revision(), &settings);
tokio::pin!(applied);
assert!(tokio::time::timeout(std::time::Duration::from_millis(1), &mut applied).await.is_err());
track.write_sample_with_extensions(codec, &Sample { data, duration: std::time::Duration::from_millis(20), ..Default::default() }, &[]).await.unwrap();
assert!(applied.await);
let packet = tokio::time::timeout(std::time::Duration::from_secs(3), packets_rx.recv()).await.unwrap().unwrap();
assert_eq!(packet.header.payload_type, if codec == VideoCodec::H264 { 102 } else { 126 });
assert!(packet.header.marker);
if let Some(previous) = previous {
assert_eq!(packet.header.ssrc, previous.header.ssrc);
assert_eq!(packet.header.sequence_number, previous.header.sequence_number.wrapping_add(1));
assert_eq!(packet.header.timestamp, previous.header.timestamp.wrapping_add(1800));
}
previous = Some(packet);
}
host.close().await.unwrap(); remote.close().await.unwrap();
}
#[tokio::test]
async fn negotiated_codecs_only_and_legacy_h264_offer() { negotiate(true, false).await; negotiate(true, true).await; negotiate(false, true).await; }
}
+63 -7
View File
@@ -7,6 +7,13 @@ extern "C" {
fn uu_wl_button(button: i32, pressed: i32) -> i32;
fn uu_wl_key(symbol: u64, pressed: i32) -> i32;
fn uu_wl_wake() -> i32;
fn uu_wl_release_all() -> i32;
}
pub fn release_all()->i32 {
#[cfg(all(target_os="linux",feature="wayland"))]
{return unsafe {uu_wl_release_all()};}
#[cfg(not(all(target_os="linux",feature="wayland")))]
-1
}
pub fn init() -> Result<(u32,u32),String> {
#[cfg(all(target_os="linux", feature="wayland"))]
@@ -19,7 +26,7 @@ pub fn init() -> Result<(u32,u32),String> {
return Ok((w as u32,h as u32));
}
#[cfg(not(all(target_os="linux", feature="wayland")))]
Err("Wayland backend requires Linux and a build with --features wayland".into())
Err("this build was compiled without the wayland feature; rebuild with --features wayland".into())
}
pub fn capture(buffer:&mut [u8]) -> Result<(),String> {
#[cfg(all(target_os="linux", feature="wayland"))]
@@ -53,17 +60,66 @@ pub fn key(symbol:u64,pressed:bool)->i32 {
}
/// A separate process owns capture; encoder cancellation closes its pipe.
/// Capture and the pipe write run on separate threads with two frame buffers, so a
/// slow encoder read no longer delays the next screencopy request.
pub fn capture_stdout() -> Result<(),String> {
use std::io::Write;
use std::io::{Read,Write};
let (w,h)=init()?;
let mut frame=vec![0u8;(w as usize)*(h as usize)*4];
let mut stdout=std::io::stdout().lock();
let interval=std::time::Duration::from_nanos(1_000_000_000/crate::video::FPS);
let size=(w as usize)*(h as usize)*4;
let fps=capture_fps(std::env::var("UU_WAYLAND_FPS").ok().as_deref())?;
let interval=std::time::Duration::from_nanos(1_000_000_000/u64::from(fps));
// A larger pipe absorbs more of each frame per write; failure keeps the default.
#[cfg(target_os="linux")]
let _=unsafe { libc::fcntl(1, libc::F_SETPIPE_SZ, 1<<20) };
let (full_tx,full_rx)=std::sync::mpsc::sync_channel::<Vec<u8>>(1);
let (free_tx,free_rx)=std::sync::mpsc::sync_channel::<Vec<u8>>(2);
for _ in 0..2 { free_tx.send(vec![0u8;size]).map_err(|_|"Capture buffers")?; }
let writer=std::thread::Builder::new().name("uu-wl-write".into()).spawn(move || -> Result<(),String> {
let mut stdout=std::io::stdout().lock();
for frame in full_rx {
stdout.write_all(&frame).map_err(|e|format!("Capture pipe: {e}"))?;
stdout.flush().map_err(|e|format!("Capture pipe: {e}"))?;
if free_tx.send(frame).is_err() { break; }
}
Ok(())
}).map_err(|e|format!("Capture writer: {e}"))?;
let demand=std::env::var("UU_WAYLAND_DEMAND").is_ok_and(|v|v=="1");
let mut stdin=std::io::stdin().lock();
let mut delivered=std::time::Instant::now();
loop {
if demand { stdin.read_exact(&mut [0]).map_err(|_|"Capture demand pipe closed")?; }
// Time from buffer availability: a slow encoder read must not shorten the next interval.
let Ok(mut frame)=free_rx.recv() else { break };
let start=std::time::Instant::now();
// The engine prefetches the next frame on delivery; after a backpressure stall that
// prefetch is as old as the stall, so drop it and capture the current screen.
if demand && start.duration_since(delivered)>interval*2 { capture(&mut frame)?; }
capture(&mut frame)?;
stdout.write_all(&frame).map_err(|e|format!("Capture pipe: {e}"))?;
if let Some(left)=interval.checked_sub(start.elapsed()) { std::thread::sleep(left); }
delivered=std::time::Instant::now();
if full_tx.send(frame).is_err() { break; }
if !demand {
if let Some(left)=interval.checked_sub(start.elapsed()) { std::thread::sleep(left); }
}
}
drop(full_tx);
writer.join().map_err(|_|"Capture writer panicked")?
}
pub(crate) fn capture_fps(value:Option<&str>)->Result<u32,String> {
match value {
None=>Ok(crate::video::FPS as u32),
Some(value)=>value.parse::<u32>().ok().filter(|fps|(5..=144).contains(fps))
.ok_or_else(||"UU_WAYLAND_FPS must be between 5 and 144".to_owned()),
}
}
#[cfg(test)]
mod tests {
#[test]
fn capture_rate_accepts_viewer_range_and_rejects_invalid_values() {
assert_eq!(super::capture_fps(None),Ok(60));
for fps in [5,15,30,60,90,144] {assert_eq!(super::capture_fps(Some(&fps.to_string())),Ok(fps));}
for value in ["0","4","145","-1","NaN",""] {assert!(super::capture_fps(Some(value)).is_err());}
}
}
+84 -16
View File
@@ -53,6 +53,9 @@ impl WebRtcSession {
m.register_default_codecs()
.map_err(|e| format!("Register standard codecs failed: {}", e))?;
let hevc = desktop && crate::video::hevc_available().await;
if hevc { crate::video_track::register_hevc(&mut m).map_err(|e| format!("Register HEVC failed: {e}"))?; }
m.register_header_extension(
webrtc::rtp_transceiver::rtp_codec::RTCRtpHeaderExtensionCapability { uri: crate::rtp_metadata::CAPTURE_INDEX_URI.into() },
webrtc::rtp_transceiver::rtp_codec::RTPCodecType::Video, None,
@@ -86,6 +89,7 @@ impl WebRtcSession {
.map_err(|e| format!("Create PeerConnection failed: {}", e))?,
);
let input_session=desktop.then(||input.desktop_session());
let context = Arc::new(Mutex::new(SessionContext::default()));
crate::network_diagnostics::start(Arc::downgrade(&pc));
@@ -143,7 +147,7 @@ impl WebRtcSession {
}));
let connected_context = context.clone();
let connection_input=input.clone();
let connection_input=input_session.clone();
let connected_signal = signaling_client.clone();
let notified = Arc::new(std::sync::atomic::AtomicBool::new(false));
pc.on_peer_connection_state_change(Box::new(move |state| {
@@ -153,12 +157,15 @@ impl WebRtcSession {
let notified = notified.clone();
Box::pin(async move {
log::info!("PeerConnection State changed: {:?}", state);
if matches!(state,RTCPeerConnectionState::Closed|RTCPeerConnectionState::Failed|RTCPeerConnectionState::Disconnected) {input.release_held();}
if let Some(session)=input.as_ref() {
if state==RTCPeerConnectionState::Connected {session.connected();}
if matches!(state,RTCPeerConnectionState::Closed|RTCPeerConnectionState::Failed|RTCPeerConnectionState::Disconnected) {session.disconnected();}
}
if state == RTCPeerConnectionState::Connected && !notified.swap(true, std::sync::atomic::Ordering::AcqRel) {
tokio::spawn(async move {
let ctx = ctx.lock().await.clone();
if let (Some(client), Some(ice)) = (&ctx.client_id, ctx.ice_id.as_ref().and_then(|v| v.as_str())) {
let payload = crate::capability::h264_device_capability(client, ice, 0, crate::video::FPS as i32, 5);
let payload = crate::capability::device_capability(client, ice, 0, crate::video::FPS as i32, 5, hevc);
if let Err(e) = sig.emit_event(crate::capability::FORWARD_SETTING_EVENT, payload).await {
log::error!("Device capability notification failed: {}", e);
}
@@ -175,14 +182,7 @@ impl WebRtcSession {
}));
// 注册视频轨
let video_track = Arc::new(TrackLocalStaticSample::new(
RTCRtpCodecCapability {
mime_type: "video/H264".to_owned(),
..Default::default()
},
"video_0".to_owned(),
"video_0".to_owned(),
));
let video_track = Arc::new(crate::video_track::VideoTrack::new());
// Set by PLI/FIR; the desktop encoder answers with an IDR on its next frame.
let idr_request = Arc::new(std::sync::atomic::AtomicBool::new(false));
@@ -270,10 +270,18 @@ impl WebRtcSession {
}
let portal=capture.portal.clone();
// File/terminal/port-mapping connections carry no desktop video.
let control_video_track = video_track.clone();
let stream_settings = video_settings.clone();
if desktop {
let state_pc = streaming_pc.clone();
tokio::spawn(async move {
if let Err(e) = crate::video::stream_desktop(streaming_pc, video_track, capture_size, capture_display, portal, video_settings_rx, idr_request).await {
log::error!("Live desktop stream stopped: {}", e);
if let Err(e) = crate::video::stream_desktop(streaming_pc, video_track, capture_size, capture_display, portal, video_settings_rx, stream_settings, idr_request).await {
// A send racing the viewer's close fails with a misleading track error.
if matches!(state_pc.connection_state(), RTCPeerConnectionState::Closed | RTCPeerConnectionState::Failed) {
log::info!("Live desktop stream ended: peer connection closed ({})", e);
} else {
log::error!("Live desktop stream stopped: {}", e);
}
}
});
} else {
@@ -327,12 +335,14 @@ impl WebRtcSession {
log::info!("New DataChannel received: {}", label);
let input_inst = input_clone.clone();
let channel_input_session=input_session.clone();
let screen = control_capture.clone();
let out_sequence = control_sequence.clone();
let shared_clipboard_ready = text_clipboard_ready.clone();
let peer_files = peer_files.clone();
let peer_port_mapping = port_mapping.clone();
let peer_video_settings = video_settings.clone();
let peer_video_track = control_video_track.clone();
let peer_actions = peer_actions.clone();
if label == "FILE_DATA_CHANNEL" { *file_channel.lock().unwrap() = Some(d.clone()); }
@@ -348,6 +358,13 @@ impl WebRtcSession {
let clipboard_assembly = Arc::new(std::sync::Mutex::new(crate::clipboard_protocol::Assembly::default()));
let rich_state=Arc::new(std::sync::Mutex::new(crate::clipboard_rich::State::default()));
let clipboard_enabled = shared_clipboard_ready.clone();
if label=="CONTROL_DATA_CHANNEL" {
let closing=channel_input_session.clone();
d.on_close(Box::new(move || {
let closing=closing.clone();
Box::pin(async move {if let Some(session)=closing {session.disconnected();}})
}));
}
// Ordered outbound queue for protocol replies produced outside this callback.
let (outbound, mut outbound_rx) = tokio::sync::mpsc::unbounded_channel::<Vec<u8>>();
{
@@ -377,9 +394,11 @@ impl WebRtcSession {
let rich_outgoing=rich_state.clone();
let open_actions = channel_actions.clone();
let open_video = channel_video_settings.clone();
let open_track = peer_video_track.clone();
d.on_open(Box::new(move || {
let init_actions = open_actions.clone();
let init_video = open_video.clone();
let init_track = open_track.clone();
let channel = channel.clone();
let sequence = initial_sequence.clone();
let screen = screen.clone();
@@ -450,9 +469,18 @@ impl WebRtcSession {
(13, Ok(crate::initialization::envelope(next_seq(), timestamp, 13, &init_actions.metrics_payload(desktop_sound, desktop_permission)))),
(26, Ok(crate::initialization::envelope(next_seq(), timestamp, 26,
&crate::system_metrics::remote_download_path(&home)))),
(18, Ok(crate::initialization::envelope(next_seq(), timestamp, 18,
&crate::capture_setting::codec_negotiation(screen_size, crate::video::FPS as u32, init_track.supports(crate::capture_setting::VideoCodec::H265))))),
(7, crate::capture_setting::screen_sources(next_seq(), timestamp, screen_size, init_video.borrow().output, "Linux desktop")),
];
if desktop && screen.init_error.is_none() {
let codec = crate::capture_setting::preferred_codec(init_track.supports(crate::capture_setting::VideoCodec::H264),
init_track.supports(crate::capture_setting::VideoCodec::H265));
init_video.send_if_modified(|current| if current.codec != codec {
log::info!("Desktop codec selected: {:?}", codec); current.codec = codec; true } else { false });
}
for (field, message) in messages {
if (!desktop || screen.init_error.is_some()) && field == 18 { continue; }
if desktop && screen.init_error.is_some() && matches!(field,7|8) { continue; }
let message = match message {
Ok(message) => message,
@@ -498,6 +526,7 @@ impl WebRtcSession {
d.on_message(Box::new(move |msg: DataChannelMessage| {
let channel_name = lbl.clone();
let input_worker = inp.clone();
let message_input_session=channel_input_session.clone();
let terminal_worker = terminal_conn.clone();
let rpc_out = outbound.clone();
let reply_chan = d_reply.clone();
@@ -509,6 +538,7 @@ impl WebRtcSession {
let file_worker = file_session.clone();
let mapping = peer_port_mapping.clone();
let video_settings = channel_video_settings.clone();
let negotiated_track = peer_video_track.clone();
let actions = channel_actions.clone();
Box::pin(async move {
@@ -519,8 +549,17 @@ impl WebRtcSession {
use crate::protocol::WireValue;
let rpc = fields.iter().find_map(|f| match (f.number, &f.value) { (21, WireValue::Bytes(b)) => Some(*b), _ => None });
let rpc_fields = rpc.and_then(|r| crate::protocol::decode_wire(r).ok());
let output_before = video_settings.borrow().output;
if let Some(response) = rpc_fields.as_deref().and_then(|r| crate::capture_setting::handle_rpc(r, &video_settings, input_size)) {
let before = *video_settings.borrow();
let output_before = before.output;
let revision = negotiated_track.revision();
if let Some(response) = rpc_fields.as_deref().and_then(|r| crate::capture_setting::handle_rpc_with_codecs(r, &video_settings, input_size, negotiated_track.supports(crate::capture_setting::VideoCodec::H264), negotiated_track.supports(crate::capture_setting::VideoCodec::H265))) {
let codec_changed = video_settings.borrow().codec != before.codec;
if codec_changed && !negotiated_track.wait_for_applied(revision, &video_settings).await {
// Official host replies after startVideoCaptureAndSendRpcResponse.
// Do not fabricate a codec error number or acknowledge an encoder that never started.
log::warn!("Codec switch did not produce a transmitted frame; success reply withheld");
return;
}
let ts = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap_or_default().as_secs();
let next = || out_sequence.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
let _ = rpc_out.send(crate::initialization::envelope(next(), ts, 22, &response));
@@ -760,6 +799,7 @@ impl WebRtcSession {
if RECEIPTS[slot].fetch_add(1, std::sync::atomic::Ordering::Relaxed) < 16 {
log::info!("CONTROL input received: kind={}", kind);
}
if let Some(session)=message_input_session.as_ref() {session.with_input(|| {
match &event {
ControlInput::MouseAbsolute {x,y} => input_worker.mouse_absolute_normalized(*x,*y,input_size),
ControlInput::MouseRelative {dx,dy} => input_worker.mouse_relative(*dx,*dy),
@@ -770,6 +810,7 @@ impl WebRtcSession {
ControlInput::MouseClick {button} => {input_worker.wire_mouse_button(*button,true);input_worker.wire_mouse_button(*button,false);},
_ => {}
}
});}
},
_ => {}
@@ -824,7 +865,7 @@ impl WebRtcSession {
ctx.client_id = soac.client_id.clone();
ctx.ice_id = soac.ice_id.clone();
ctx.app_control_id = soac.app_control_id.clone();
log::info!("Updated WebRTC session context: client_id={:?}, ice_id={:?}", ctx.client_id, ctx.ice_id);
log::info!("Updated WebRTC session context: client_id_present={} ice_id_present={}", ctx.client_id.is_some(), ctx.ice_id.is_some());
}
let mut sdp_str = soac.sdp.clone().unwrap_or_default();
if sdp_str.is_empty() {
@@ -850,6 +891,33 @@ impl WebRtcSession {
};
log::info!("Offer x-uuremote-quic: {}", quic_summary);
log::info!("Offer includes UU capture-index extension: {}", sdp_str.lines().any(|l| l.starts_with("a=extmap:") && l.contains(crate::rtp_metadata::CAPTURE_INDEX_URI)));
let mut codecs: Vec<&str> = sdp_str.lines().filter_map(|l| l.strip_prefix("a=rtpmap:")).filter_map(|l| l.split_whitespace().nth(1))
.filter_map(|c| c.split('/').next()).collect();
codecs.dedup();
log::info!("Offer codecs: {:?}", codecs);
// SDP structure only: attribute names per section and public extmap URIs; never ufrag,
// password, fingerprint, candidate or fmtp values.
let mut sections: Vec<String> = Vec::new();
let mut current = String::from("session");
let mut names: std::collections::BTreeSet<String> = Default::default();
let mut flush = |current: &str, names: &mut std::collections::BTreeSet<String>, sections: &mut Vec<String>| {
sections.push(format!("{current}[{}]", names.iter().cloned().collect::<Vec<_>>().join(",")));
names.clear();
};
for line in sdp_str.lines() {
if let Some(m) = line.strip_prefix("m=") {
flush(&current, &mut names, &mut sections);
let mut parts = m.split_whitespace();
let kind = parts.next().unwrap_or("?");
current = format!("{kind}/{}pt", parts.skip(2).count());
} else if let Some(a) = line.strip_prefix("a=") {
let name = a.split([':', ' ']).next().unwrap_or("");
if name == "extmap" { if let Some(uri) = a.split_whitespace().nth(1) { names.insert(format!("extmap={}", uri.rsplit('/').next().unwrap_or(uri))); } }
else if !name.is_empty() && name.len() <= 32 { names.insert(name.to_owned()); }
}
}
flush(&current, &mut names, &mut sections);
log::info!("Offer SDP structure: {}", sections.join(" | "));
let offer = RTCSessionDescription::offer(sdp_str)
.map_err(|e| format!("Invalid offer: {}", e))?;