mirror of
https://github.com/VallierDev/uu-remote-linux.git
synced 2026-11-16 16:50:41 +01:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
03f611811c | ||
|
|
1b7ec0a94f | ||
|
|
89d209e7df | ||
|
|
b5cc5ed736 | ||
|
|
15bb35573d | ||
|
|
6dda2e16a4 | ||
|
|
a01fd39f06 | ||
|
|
46589b3fac | ||
|
|
708d9de2de | ||
|
|
ebe6a7e19a | ||
|
|
09f7169f62 | ||
|
|
9125e51350 | ||
|
|
0da79a45eb | ||
|
|
d461ef9bed | ||
|
|
f123f0ba05 | ||
|
|
90c33e173c | ||
|
|
ac0f2bf2c6 | ||
|
|
8a8a4c165f | ||
|
|
7beb3aecf0 | ||
|
|
26d4812480 | ||
|
|
49edf1d389 | ||
|
|
ddbc75bbf6 | ||
|
|
9fbe7d63c5 | ||
|
|
3a78491f25 | ||
|
|
fc8c10cde9 | ||
|
|
125baa8c8b | ||
|
|
c5aa238f73 | ||
|
|
3673d37c1e | ||
|
|
59a45e092b | ||
|
|
a870f7d041 | ||
|
|
367be30a0f |
No files matched your search
@@ -26,6 +26,7 @@ jobs:
|
||||
name=uu-remote-linux-${GITHUB_REF_NAME}-x86_64-linux
|
||||
mkdir -p "$name"
|
||||
cp target/release/uuremote-server LICENSE README.md README.en.md config.example.json "$name/"
|
||||
cp -r contrib "$name/"
|
||||
tar czf "$name.tar.gz" "$name"
|
||||
sha256sum "$name.tar.gz" > "$name.tar.gz.sha256"
|
||||
- uses: softprops/action-gh-release@v2
|
||||
|
||||
@@ -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/
|
||||
@@ -104,8 +104,11 @@ viewer does not reconnect them automatically.
|
||||
|---|---|
|
||||
| `~/.config/uuremote/config.json` | user_id, device_id, client_id, system_id, machine_guid, token, csrf; optional `sign_key`, `ffmpeg_root`. `$UU_CONFIG` overrides the path |
|
||||
| `~/.config/uuremote/host_settings.json` | persisted 安全 settings: auto_lock, keep_mute, mute_permanent |
|
||||
| `UU_DESKTOP_BACKEND` | `auto` (default) / `x11` / `wayland` / `portal` |
|
||||
| `UU_VIDEO_ENCODER=cpu` | force libx264 instead of NVENC |
|
||||
| `UU_DESKTOP_BACKEND` | `auto` (default) / `x11` / `wayland` / `portal` / `kms` (experimental feature) / `none` (headless: no display; desktop offers ignored; tool peers get a nominal 1920x1080 screen + Allow permissions, which the port-mapping viewer requires) |
|
||||
| `UU_INPUT_BACKEND` | `auto` (uinput on Wayland/portal when `/dev/uinput` is accessible, udev `uaccess` rule in README) / `uinput` / `native`. X11 keeps XTest (held-key autorepeat handling) |
|
||||
| `UU_TERMINAL_SHORTCUTS=0` | disable Ctrl+V → Ctrl+Shift+V in focused X11 terminals (Mac Cmd and Ctrl both arrive as VK_LCONTROL) |
|
||||
| `contrib/virtual-desktop/` | Xvfb + session (`UU_VIRTUAL_SESSION`, `UU_VIRTUAL_BUS=user|private`) for monitorless hosts; serve it with `DISPLAY=:99` |
|
||||
| `UU_VIDEO_ENCODER` | `cpu`: libx264; `vaapi`: explicit Intel/AMD; `nvenc`: force; unset: NVENC→CPU only if `/proc/driver/nvidia/version` or `/dev/nvidia0` exists, else CPU (#10) |
|
||||
| `UU_FFMPEG_ROOT` | FFmpeg prefix (`bin/ffmpeg`, libs in `lib/x86_64-linux-gnu`) |
|
||||
| `UU_AUDIO=0`, `UU_AUDIO_SOURCE` | disable sound / choose the Pulse source (default `@DEFAULT_MONITOR@`) |
|
||||
| `UU_LOGIN_WINDOW=0` | don't auto-open the login window |
|
||||
@@ -123,7 +126,7 @@ every 60 s.
|
||||
| `src/webrtc.rs` | peer connection, data channels, CONTROL dispatch, initialization messages |
|
||||
| `src/video.rs`, `src/capture_setting.rs` | FFmpeg H.264 pipeline; viewer quality/fps/resolution settings |
|
||||
| `src/audio.rs` | Pulse/PipeWire monitor → Opus audio track |
|
||||
| `src/input.rs`, `src/input_mapping.rs` | input injection (X11 XTest / Wayland / portal), Windows VK → keysym |
|
||||
| `src/input.rs`, `src/input_mapping.rs`, `src/uinput.rs` | input injection (X11 XTest / Wayland / portal / uinput), Windows VK → keysym → evdev |
|
||||
| `src/cursor.rs` | pointer shape mirroring (X11) |
|
||||
| `src/clipboard*.rs` | text clipboard bridge and protocol |
|
||||
| `src/file_trans.rs` | FileTransferFtp RPC |
|
||||
@@ -196,6 +199,9 @@ started a second full host server instead, so check that a subcommand exists in
|
||||
- Remote keys repeat at high RTT because key-up arrives after the X autorepeat delay. XTEST repeats through the
|
||||
master keyboard; the engine disables global autorepeat only while a remote key is held.
|
||||
- Interactive shells ignore SIGTERM: the server HUPs/KILLs PTY sessions on shutdown.
|
||||
- An explicit `DISPLAY` that cannot be opened is an error; the X11 engine falls back to `:0`/`:1` only when DISPLAY is unset.
|
||||
- systemd/D-Bus-activated apps (GNOME Terminal, portals) run with the user manager's environment, not the caller's:
|
||||
a private `dbus-run-session` bus does not bring them onto a virtual display.
|
||||
- GTK windows stop repainting while GNOME is locked or the monitor is off; test UI on Xvfb or after unlock.
|
||||
- logind `unlock-session` by the session owner clears the GNOME shield in ~3 s.
|
||||
- GNOME closes the portal session on lock; capture/input must share one portal session and be rebuilt after
|
||||
@@ -218,3 +224,67 @@ started a second full host server instead, so check that a subcommand exists in
|
||||
- New protocol behavior: cite the evidence (observed field numbers, official log strings) in a comment.
|
||||
- Update README.md / README.en.md when user-visible features or limitations change, and add
|
||||
`docs/release-notes/<tag>.md` before tagging a release.
|
||||
|
||||
### VAAPI (opt-in / 显式启用)
|
||||
|
||||
`UU_VIDEO_ENCODER=vaapi` selects Intel/AMD H.264 VAAPI; `UU_VAAPI_DEVICE` defaults to
|
||||
`/dev/dri/renderD128` (render-node permissions required). No automatic VAAPI selection.
|
||||
Both FFmpeg process and `--features inproc-av` upload NV12 frames to a VAAPI frame pool.
|
||||
VBR uses the viewer bitrate preset and CQ-derived minimum QP, no B-frames, async depth 1,
|
||||
AUD and the existing GOP/VFR timing. Unsupported device/driver/initialization falls back to libx264
|
||||
with a warning. Accepted on Intel UHD 770 (iHD) with the default FFmpeg process pipeline and the official Mac
|
||||
viewer (presets, fps, scaling, reconnect; no fallback, ~40% less CPU than libx264). AMD and the `inproc-av`
|
||||
VAAPI path are not hardware-verified.
|
||||
|
||||
### Virtual audio / 虚拟声卡
|
||||
|
||||
The viewer's 静音运行 switch creates the null sink `uu-remote-sink`,
|
||||
moves streams from the original default output and captures `uu-remote-sink.monitor`.
|
||||
New output goes to this sink; streams explicitly pinned to other outputs are untouched.
|
||||
The last desktop session restores the previous default output and unloads the module.
|
||||
`mute_permanent` retains the physical mute policy, but still releases the virtual sink.
|
||||
A private recovery record beside the config and a process lock recover crash leftovers on startup.
|
||||
`pactl` is used when installed (PulseAudio / PipeWire-pulse); plain PipeWire systems without it (default
|
||||
Ubuntu desktop) use `pw-cli`/`wpctl` for the same sink and WirePlumber moves playing streams. Accepted on
|
||||
Ubuntu 24.04 with the official Mac viewer. When routing is unavailable, the existing mute/monitor path remains.
|
||||
Changing the switch restarts only the scoped audio encoder; normal non-muted capture is unchanged.
|
||||
`virtual-audio-probe` temporarily routes output for five seconds and restores it (local diagnostic).
|
||||
|
||||
### Virtual desktop user bus
|
||||
|
||||
The supervisor imports DISPLAY and XAUTHORITY into the linger user bus, waits for authenticated
|
||||
X11 readiness, and keeps its cleanup trap alive. The sample drop-in uses `%t/uu-virtual-99.Xauthority`.
|
||||
Use a dedicated headless account. See the [setup and official Mac acceptance checklist](contrib/virtual-desktop/ACCEPTANCE.md).
|
||||
|
||||
### Arch Linux / Omarchy
|
||||
|
||||
```sh
|
||||
sudo pacman -S --needed base-devel rust pkgconf clang ffmpeg libx11 libxext libxtst libxfixes gtk3 \
|
||||
wayland libxkbcommon wl-clipboard libpipewire glib2 libpulse
|
||||
cargo test --locked --features wayland
|
||||
cargo build --release --features wayland
|
||||
```
|
||||
|
||||
Omarchy uses Hyprland: run in its user session, select `UU_DESKTOP_BACKEND=wayland`,
|
||||
and retain `WAYLAND_DISPLAY` / `XDG_RUNTIME_DIR`. This requires wlr-screencopy and the
|
||||
virtual input protocols supported by Hyprland; other compositors may need `--features wayland,portal`.
|
||||
For uinput, load `uinput` and apply the uaccess rule above; seat ACLs require an active login.
|
||||
For unattended accounts, explicitly restrict `/dev/uinput` access to a dedicated group.
|
||||
This permission can inject into the whole seat, including lock/greeter screens.
|
||||
`UU_INPUT_BACKEND=uinput` requests kernel devices; verify startup actually reports uinput.
|
||||
Optional VAAPI: `intel-media-driver` (modern Intel) or `libva-mesa-driver` (AMD), `libva-utils`
|
||||
for `vainfo`. Optional KMS: `libdrm`. Virtual Xfce: `xorg-server-xvfb xorg-xauth xorg-xdpyinfo
|
||||
xfce4-session xfce4-panel xfdesktop xfwm4 gnome-terminal`.
|
||||
Rolling Arch FFmpeg may be newer than supported `inproc-av` (FFmpeg 6.x); use the default process
|
||||
pipeline or build against a separate FFmpeg 6 prefix. Do not downgrade the system FFmpeg.
|
||||
User-bus headless setup and the official Mac checklist: [virtual desktop acceptance](contrib/virtual-desktop/ACCEPTANCE.md).
|
||||
|
||||
### Experimental KMS / 实验后端
|
||||
|
||||
`--features kms` plus explicit `UU_DESKTOP_BACKEND=kms` enables a limited linear-framebuffer
|
||||
prototype; `UU_KMS_DEVICE=/dev/dri/card0`. Requires libdrm headers and deliberately granted
|
||||
`cap_sys_admin+ep`, plus card-node access. It is never automatically selected or granted privileges.
|
||||
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.
|
||||
No official "no desktop" error code was found for headless desktop offers, so the existing offer behavior is retained.
|
||||
Generated
+56
-1
@@ -111,6 +111,12 @@ dependencies = [
|
||||
"rustversion",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "as-raw-xcb-connection"
|
||||
version = "1.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "175571dd1d178ced59193a6fc02dde1b972eb0bc56c892cde9beeceac5bf0f6b"
|
||||
|
||||
[[package]]
|
||||
name = "asn1-rs"
|
||||
version = "0.5.2"
|
||||
@@ -801,6 +807,16 @@ dependencies = [
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "gethostname"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1bd49230192a3797a9a4d6abe9b3eed6f7fa4c8a8a4947977c6f80025f92cbd8"
|
||||
dependencies = [
|
||||
"rustix",
|
||||
"windows-link",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "getrandom"
|
||||
version = "0.2.17"
|
||||
@@ -1210,6 +1226,12 @@ dependencies = [
|
||||
"windows-link",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "linux-raw-sys"
|
||||
version = "0.12.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "32a66949e030da00e8c7d4434b251670a91556f4144941d37452769c25d58a53"
|
||||
|
||||
[[package]]
|
||||
name = "litemap"
|
||||
version = "0.8.3"
|
||||
@@ -1885,6 +1907,19 @@ dependencies = [
|
||||
"nom",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustix"
|
||||
version = "1.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "891efababe418670775f199f0d233d84843c227a0949a883ce15b37c78d6629d"
|
||||
dependencies = [
|
||||
"bitflags 2.13.2",
|
||||
"errno",
|
||||
"libc",
|
||||
"linux-raw-sys",
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustls"
|
||||
version = "0.21.12"
|
||||
@@ -2614,7 +2649,7 @@ checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
|
||||
|
||||
[[package]]
|
||||
name = "uu-remote-linux"
|
||||
version = "0.1.4"
|
||||
version = "0.1.10"
|
||||
dependencies = [
|
||||
"base64 0.22.1",
|
||||
"bytes",
|
||||
@@ -2636,6 +2671,7 @@ dependencies = [
|
||||
"tokio",
|
||||
"tokio-tungstenite",
|
||||
"webrtc",
|
||||
"x11rb",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -3094,6 +3130,25 @@ version = "0.6.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3ad82d2a33cdc9674dc7465672f271e096168fcdbe0f799d9e6db8c5892679dc"
|
||||
|
||||
[[package]]
|
||||
name = "x11rb"
|
||||
version = "0.13.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9993aa5be5a26815fe2c3eacfc1fde061fc1a1f094bf1ad2a18bf9c495dd7414"
|
||||
dependencies = [
|
||||
"as-raw-xcb-connection",
|
||||
"gethostname",
|
||||
"libc",
|
||||
"rustix",
|
||||
"x11rb-protocol",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "x11rb-protocol"
|
||||
version = "0.13.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ea6fc2961e4ef194dcbfe56bb845534d0dc8098940c7e5c012a258bfec6701bd"
|
||||
|
||||
[[package]]
|
||||
name = "x25519-dalek"
|
||||
version = "2.0.1"
|
||||
|
||||
+5
-2
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "uu-remote-linux"
|
||||
version = "0.1.4"
|
||||
version = "0.1.10"
|
||||
edition = "2021"
|
||||
authors = ["uu-remote-linux contributors"]
|
||||
description = "Unofficial Linux host for NetEase UU Remote: control a Linux desktop from the official clients"
|
||||
@@ -16,10 +16,12 @@ path = "src/main.rs"
|
||||
[features]
|
||||
default = []
|
||||
wayland = []
|
||||
# Experimental direct linear framebuffer capture; never enabled in release builds.
|
||||
kms = []
|
||||
portal = []
|
||||
# In-process capture and encoding through FFmpeg 6 libraries (libavcodec/libavdevice dev packages).
|
||||
# Off by default so release binaries do not link the build machine's libav.
|
||||
inproc-av = ["dep:ffmpeg-next"]
|
||||
inproc-av = ["dep:ffmpeg-next", "dep:x11rb"]
|
||||
|
||||
[dependencies]
|
||||
tokio = { version = "1", features = ["full"] }
|
||||
@@ -41,6 +43,7 @@ log = "0.4"
|
||||
qrcode = { version = "0.14", default-features = false }
|
||||
env_logger = "0.11"
|
||||
ffmpeg-next = { version = "6.1", optional = true, default-features = false, features = ["codec", "format", "device", "software-scaling", "software-resampling"] }
|
||||
x11rb = { version = "0.13", features = ["damage", "shm", "allow-unsafe-code"], optional = true }
|
||||
|
||||
[build-dependencies]
|
||||
cc = "1.0"
|
||||
|
||||
+130
-6
@@ -33,7 +33,8 @@
|
||||
|---|---|
|
||||
| QR login | GTK login window (or a terminal QR code with `login --no-gui`); expired codes refresh automatically; the service opens the login window by itself when credentials are missing or expired |
|
||||
| Remote desktop | H.264 video, NVENC hardware encoding on NVIDIA GPUs, libx264 otherwise; the viewer draws its local pointer, so there is no double cursor |
|
||||
| Mouse / keyboard | Absolute and relative motion, clicks, wheel, keys; no spurious key repeat on high-latency links |
|
||||
| Mouse / keyboard | Absolute and relative motion, clicks, wheel, keys; no spurious key repeat on high-latency links; optional uinput virtual devices on Wayland (layout independent, see below) |
|
||||
| Terminal shortcuts | Mac viewers send both Cmd and Ctrl as Ctrl. In a focused terminal window Ctrl+V / Cmd+V become Ctrl+Shift+V (paste); Ctrl+C always interrupts, copy with Cmd+Shift+C (X11) |
|
||||
| Pointer shape | Remote cursor shape is mirrored to the viewer (X11) |
|
||||
| Clipboard | Two-way text sync |
|
||||
| Quality / frame rate / resolution | Auto, Original 20M, Ultra 8M, HD 2M, custom; 30/60/90/144 fps; "other resolutions" are scaled by the encoder, the host monitor is left untouched |
|
||||
@@ -46,6 +47,13 @@
|
||||
| System actions | Lock, show desktop, task manager (system monitor), reboot, shut down |
|
||||
| Port mapping | The official "port mapping" tool: forwards a local port on the viewer to an address reachable from the Linux host (e.g. 127.0.0.1:22); `UU_PORT_MAPPING=0` disables it |
|
||||
| Reconnects | Every tool window has its own connection; they do not disturb each other |
|
||||
| Headless servers | Goes online without a monitor with terminal, file transfer and port mapping; an Xvfb virtual desktop gives a full remote desktop (see below) |
|
||||
|
||||
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
|
||||
|
||||
@@ -62,10 +70,18 @@ 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 portal backend does not support clipboard, screen wake or
|
||||
dynamic resolution yet. On the first connection the desktop asks for "Remote Desktop / Screen Sharing"
|
||||
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.
|
||||
|
||||
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 |
|
||||
@@ -77,7 +93,7 @@ permission; the permission is remembered once granted.
|
||||
| Privacy screen on the host | Not implemented |
|
||||
| Microphone (viewer audio played on the host) | Not implemented (needs a virtual sound card) |
|
||||
| Plugins such as the WoW queue notifier | Not supported; the client is told that no plugins exist |
|
||||
| Access before login (greeter / no user logged in) | Not supported; only an already logged-in desktop session can be controlled |
|
||||
| Access before login (greeter / no user logged in) | The greeter cannot be controlled; without a logged-in user use headless mode or a virtual desktop (see below) |
|
||||
| Relay connections on restrictive networks | Uses the ICE servers provided by the UU cloud; complex network setups are not thoroughly tested |
|
||||
| Acting as a viewer (controlling other machines from Linux) | Out of scope |
|
||||
|
||||
@@ -149,6 +165,48 @@ journalctl --user -u uuremote-native.service -f # online once you see availabi
|
||||
|
||||
If you have not logged in yet, the service opens the QR login window on your desktop.
|
||||
|
||||
### 5. uinput virtual keyboard/mouse on Wayland (recommended)
|
||||
|
||||
On Wayland (especially GNOME/KDE via the portal) the default input path needs portal consent and some keyboard layouts
|
||||
drop symbols such as the underscore. Once the logged-in user may access `/dev/uinput`, the host switches to kernel
|
||||
virtual devices that send physical key codes:
|
||||
|
||||
```sh
|
||||
echo 'KERNEL=="uinput", SUBSYSTEM=="misc", OPTIONS+="static_node=uinput", TAG+="uaccess"' | \
|
||||
sudo tee /etc/udev/rules.d/60-uu-remote-linux-uinput.rules
|
||||
sudo udevadm control --reload && sudo udevadm trigger --name-match=uinput
|
||||
```
|
||||
|
||||
`uaccess` grants access only to the user sitting at the machine. `UU_INPUT_BACKEND=uinput|native` forces a backend.
|
||||
X11 keeps XTest by default because it handles held keys correctly on high-latency links.
|
||||
|
||||
## Servers without a monitor
|
||||
|
||||
**Headless mode** (terminal, file transfer and port mapping only): add `Environment=UU_DESKTOP_BACKEND=none` to the
|
||||
service and run `loginctl enable-linger $USER`, so the systemd user manager (with `XDG_RUNTIME_DIR` and the user D-Bus)
|
||||
starts at boot without a login. The viewer's remote desktop keeps showing "connecting" in this mode; that is expected.
|
||||
Log in once with `uuremote-server login --no-gui` (QR in the terminal).
|
||||
|
||||
**Virtual desktop**: for a full remote desktop, run a desktop session (Xfce recommended) on an Xvfb screen:
|
||||
|
||||
```sh
|
||||
sudo apt install xvfb xauth x11-utils dbus-x11 dbus-user-session xfce4 # or another session, chosen with UU_VIRTUAL_SESSION
|
||||
install -Dm755 contrib/virtual-desktop/uu-virtual-desktop ~/.local/bin/uu-virtual-desktop
|
||||
install -Dm644 contrib/virtual-desktop/uu-virtual-desktop.service ~/.config/systemd/user/uu-virtual-desktop.service
|
||||
install -Dm644 contrib/virtual-desktop/virtual-display.conf ~/.config/systemd/user/uuremote-native.service.d/virtual-display.conf
|
||||
loginctl enable-linger $USER
|
||||
systemctl --user daemon-reload && systemctl --user enable --now uu-virtual-desktop.service
|
||||
systemctl --user restart uuremote-native.service
|
||||
```
|
||||
|
||||
The virtual screen is `:99`, 1920x1080 by default. Xvfb has no GPU acceleration, so it is a little less smooth than a
|
||||
real display; NVENC is still used for encoding when an NVIDIA GPU is present. The script exports `DISPLAY` to the
|
||||
systemd user session so D-Bus-activated programs open on the virtual screen; on a machine that also runs a real desktop
|
||||
session set `UU_VIRTUAL_BUS=private` so the real session is not affected.
|
||||
|
||||
To make a GPU drive a real (non-Xvfb) desktop without a monitor, use a dummy HDMI plug or load an EDID file with the
|
||||
kernel parameter `drm.edid_firmware=`, then install as for a normal X11 desktop.
|
||||
|
||||
## Configuration
|
||||
|
||||
Common environment variables:
|
||||
@@ -156,8 +214,10 @@ Common environment variables:
|
||||
| Variable | Purpose |
|
||||
|---|---|
|
||||
| `UU_CONFIG` | Config file path (default `~/.config/uuremote/config.json`) |
|
||||
| `UU_DESKTOP_BACKEND` | `auto` / `x11` / `wayland` / `portal` |
|
||||
| `UU_VIDEO_ENCODER=cpu` | Force CPU encoding |
|
||||
| `UU_DESKTOP_BACKEND` | `auto` / `x11` / `wayland` / `portal` / `kms` (experimental feature) / `none` (headless) |
|
||||
| `UU_INPUT_BACKEND` | `auto` (default: uinput on Wayland when accessible) / `uinput` / `native` |
|
||||
| `UU_TERMINAL_SHORTCUTS=0` | Disable the Ctrl+V paste rewrite in terminal windows |
|
||||
| `UU_VIDEO_ENCODER` | `cpu`: libx264; `vaapi`: opt-in Intel/AMD; `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_LOGIN_WINDOW=0` | Do not open the login window automatically when credentials are missing |
|
||||
@@ -216,3 +276,67 @@ Released under the [GPL-3.0](LICENSE): you may use, modify and redistribute it,
|
||||
- The NetEase UU Remote team, for a remote tool that is a pleasure to use.
|
||||
- [webrtc-rs](https://github.com/webrtc-rs/webrtc), FFmpeg, GTK and the other open-source projects this builds on.
|
||||
- Most of the code was written with the help of Claude and Codex.
|
||||
|
||||
### VAAPI (opt-in / 显式启用)
|
||||
|
||||
`UU_VIDEO_ENCODER=vaapi` selects Intel/AMD H.264 VAAPI; `UU_VAAPI_DEVICE` defaults to
|
||||
`/dev/dri/renderD128` (render-node permissions required). No automatic VAAPI selection.
|
||||
Both FFmpeg process and `--features inproc-av` upload NV12 frames to a VAAPI frame pool.
|
||||
VBR uses the viewer bitrate preset and CQ-derived minimum QP, no B-frames, async depth 1,
|
||||
AUD and the existing GOP/VFR timing. Unsupported device/driver/initialization falls back to libx264
|
||||
with a warning. Accepted on Intel UHD 770 (iHD) with the default FFmpeg process pipeline and the official Mac
|
||||
viewer (presets, fps, scaling, reconnect; no fallback, ~40% less CPU than libx264). AMD and the `inproc-av`
|
||||
VAAPI path are not hardware-verified.
|
||||
|
||||
### Virtual audio / 虚拟声卡
|
||||
|
||||
The viewer's 静音运行 switch creates the null sink `uu-remote-sink`,
|
||||
moves streams from the original default output and captures `uu-remote-sink.monitor`.
|
||||
New output goes to this sink; streams explicitly pinned to other outputs are untouched.
|
||||
The last desktop session restores the previous default output and unloads the module.
|
||||
`mute_permanent` retains the physical mute policy, but still releases the virtual sink.
|
||||
A private recovery record beside the config and a process lock recover crash leftovers on startup.
|
||||
`pactl` is used when installed (PulseAudio / PipeWire-pulse); plain PipeWire systems without it (default
|
||||
Ubuntu desktop) use `pw-cli`/`wpctl` for the same sink and WirePlumber moves playing streams. Accepted on
|
||||
Ubuntu 24.04 with the official Mac viewer. When routing is unavailable, the existing mute/monitor path remains.
|
||||
Changing the switch restarts only the scoped audio encoder; normal non-muted capture is unchanged.
|
||||
`virtual-audio-probe` temporarily routes output for five seconds and restores it (local diagnostic).
|
||||
|
||||
### Virtual desktop user bus
|
||||
|
||||
The supervisor imports DISPLAY and XAUTHORITY into the linger user bus, waits for authenticated
|
||||
X11 readiness, and keeps its cleanup trap alive. The sample drop-in uses `%t/uu-virtual-99.Xauthority`.
|
||||
Use a dedicated headless account. See the [setup and official Mac acceptance checklist](contrib/virtual-desktop/ACCEPTANCE.md).
|
||||
|
||||
### Arch Linux / Omarchy
|
||||
|
||||
```sh
|
||||
sudo pacman -S --needed base-devel rust pkgconf clang ffmpeg libx11 libxext libxtst libxfixes gtk3 \
|
||||
wayland libxkbcommon wl-clipboard libpipewire glib2 libpulse
|
||||
cargo test --locked --features wayland
|
||||
cargo build --release --features wayland
|
||||
```
|
||||
|
||||
Omarchy uses Hyprland: run in its user session, select `UU_DESKTOP_BACKEND=wayland`,
|
||||
and retain `WAYLAND_DISPLAY` / `XDG_RUNTIME_DIR`. This requires wlr-screencopy and the
|
||||
virtual input protocols supported by Hyprland; other compositors may need `--features wayland,portal`.
|
||||
For uinput, load `uinput` and apply the uaccess rule above; seat ACLs require an active login.
|
||||
For unattended accounts, explicitly restrict `/dev/uinput` access to a dedicated group.
|
||||
This permission can inject into the whole seat, including lock/greeter screens.
|
||||
`UU_INPUT_BACKEND=uinput` requests kernel devices; verify startup actually reports uinput.
|
||||
Optional VAAPI: `intel-media-driver` (modern Intel) or `libva-mesa-driver` (AMD), `libva-utils`
|
||||
for `vainfo`. Optional KMS: `libdrm`. Virtual Xfce: `xorg-server-xvfb xorg-xauth xorg-xdpyinfo
|
||||
xfce4-session xfce4-panel xfdesktop xfwm4 gnome-terminal`.
|
||||
Rolling Arch FFmpeg may be newer than supported `inproc-av` (FFmpeg 6.x); use the default process
|
||||
pipeline or build against a separate FFmpeg 6 prefix. Do not downgrade the system FFmpeg.
|
||||
User-bus headless setup and the official Mac checklist: [virtual desktop acceptance](contrib/virtual-desktop/ACCEPTANCE.md).
|
||||
|
||||
### Experimental KMS / 实验后端
|
||||
|
||||
`--features kms` plus explicit `UU_DESKTOP_BACKEND=kms` enables a limited linear-framebuffer
|
||||
prototype; `UU_KMS_DEVICE=/dev/dri/card0`. Requires libdrm headers and deliberately granted
|
||||
`cap_sys_admin+ep`, plus card-node access. It is never automatically selected or granted privileges.
|
||||
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.
|
||||
No official "no desktop" error code was found for headless desktop offers, so the existing offer behavior is retained.
|
||||
@@ -29,7 +29,8 @@
|
||||
|---|---|
|
||||
| 扫码登录 | GTK 登录窗口(或 `login --no-gui` 终端二维码);二维码过期自动刷新;登录凭证失效时服务会自动弹出登录窗口 |
|
||||
| 远程桌面 | H.264 视频,NVIDIA 显卡用 NVENC 硬件编码,否则用 libx264;查看端绘制本地鼠标,不会出现双光标 |
|
||||
| 鼠标 / 键盘 | 绝对/相对移动、点击、滚轮、按键;修复了网络延迟导致的按键连发 |
|
||||
| 鼠标 / 键盘 | 绝对/相对移动、点击、滚轮、按键;修复了网络延迟导致的按键连发;Wayland 下可用 uinput 虚拟键鼠(不依赖键盘布局,见下文) |
|
||||
| 终端快捷键 | Mac 控制端的 Cmd 和 Ctrl 到被控端都是 Ctrl。焦点在终端窗口时,Ctrl+V / Cmd+V 自动转成 Ctrl+Shift+V 粘贴;Ctrl+C 始终是中断,复制请用 Cmd+Shift+C(X11) |
|
||||
| 鼠标指针样式 | 远端指针形状实时同步(X11) |
|
||||
| 剪贴板 | 双向文本同步 |
|
||||
| 画质 / 帧率 / 分辨率 | 自动、原画 20M、超清 8M、高清 2M、自定义;30/60/90/144 帧;「其他分辨率」由编码器缩放,不改动主机显示器 |
|
||||
@@ -42,6 +43,11 @@
|
||||
| 系统操作 | 锁屏、显示桌面、任务管理器(系统监视器)、重启、关机 |
|
||||
| 端口映射 | 官方「端口映射」工具:把控制端本地端口转发到 Linux 能访问到的地址(如 127.0.0.1:22);`UU_PORT_MAPPING=0` 可关闭 |
|
||||
| 多次重连 | 每个工具窗口独立连接,互不影响 |
|
||||
| 无头服务器 | 没有显示器也能上线,提供终端、文件传输和端口映射;也可以用 Xvfb 虚拟桌面提供完整远程桌面(见下文) |
|
||||
|
||||
桌面连接建立、结束及服务启动时会清理遗留输入状态;旧连接的迟到事件不能影响新的桌面连接。
|
||||
X11 清理当前已认证显示上的按下状态(包括无法区分来源的实际按键);uinput 仅清理本服务虚拟设备,
|
||||
Portal 仅作用于本服务获授权的会话。文件传输、终端等独立工具连接不触发桌面松键。
|
||||
|
||||
## 支持的 Linux 桌面
|
||||
|
||||
@@ -57,7 +63,9 @@
|
||||
除 GNOME(X11)外,其余桌面目前都是在虚拟机里测试的。如果你在真机上遇到问题,欢迎[提交 issue](../../issues),
|
||||
请附上发行版、桌面环境、显卡型号和 `journalctl --user -u uuremote-native.service` 里的相关日志。
|
||||
|
||||
Wayland 下的限制:只支持单屏、固定分辨率;portal 后端暂不支持剪贴板、唤醒屏幕和动态分辨率。
|
||||
Wayland 下的限制:只支持单屏、固定分辨率;Hyprland 原生后端默认使用双缓冲流水线 shm screencopy,控制端帧率设置会同步到采集节拍和编码器,但实际帧率仍受合成器、渲染和读回速度限制(旧串行路径在 2560x1600 曾测得约 16~22 帧;流水线只在虚拟机里测量过,不代表实机保证);portal 后端暂不支持唤醒屏幕和动态分辨率;剪贴板只在控制端的远控窗口处于前台时同步。
|
||||
|
||||
原生 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 帧。
|
||||
首次连接时,桌面会弹窗请求「远程桌面 / 屏幕共享」授权,同意后会记住授权。
|
||||
|
||||
## 尚未实现 / 不支持
|
||||
@@ -71,7 +79,7 @@ Wayland 下的限制:只支持单屏、固定分辨率;portal 后端暂不
|
||||
| 被控端防窥模式 | 未实现 |
|
||||
| 麦克风(控制端声音传到被控端) | 未实现(需要虚拟声卡) |
|
||||
| 魔兽世界排队提醒等插件 | 不支持,会明确告诉客户端「无插件」 |
|
||||
| 登录前(登录界面 / 无人登录)访问 | 不支持,只能控制已登录的桌面会话 |
|
||||
| 登录前(登录界面 / 无人登录)访问 | 不支持控制登录界面;没有人登录时可以用无头模式或虚拟桌面(见下文) |
|
||||
| 跨运营商 / 受限网络的中继连接 | 已接入官方下发的 ICE 服务器,但复杂网络环境未充分测试 |
|
||||
| 作为控制端(在 Linux 上控制别的电脑) | 不在本项目范围 |
|
||||
|
||||
@@ -142,6 +150,45 @@ journalctl --user -u uuremote-native.service -f # 看到 availability=Some("re
|
||||
|
||||
如果还没有登录,服务会自动在桌面上弹出扫码窗口。
|
||||
|
||||
### 5. Wayland 下使用 uinput 虚拟键鼠(推荐)
|
||||
|
||||
Wayland(尤其是 GNOME/KDE portal)下,默认的键鼠注入要经过 portal 授权,部分键盘布局下还会丢符号(比如下划线)。
|
||||
允许当前登录用户访问 `/dev/uinput` 后,服务会自动改用内核级虚拟键盘和鼠标,发送物理按键码:
|
||||
|
||||
```sh
|
||||
echo 'KERNEL=="uinput", SUBSYSTEM=="misc", OPTIONS+="static_node=uinput", TAG+="uaccess"' | \
|
||||
sudo tee /etc/udev/rules.d/60-uu-remote-linux-uinput.rules
|
||||
sudo udevadm control --reload && sudo udevadm trigger --name-match=uinput
|
||||
```
|
||||
|
||||
`uaccess` 只把权限给当前坐在这台机器前的登录用户。`UU_INPUT_BACKEND=uinput|native` 可以强制指定后端。
|
||||
X11 默认继续用 XTest,因为它能正确处理网络延迟下按住不放的按键。
|
||||
|
||||
## 没有显示器的服务器
|
||||
|
||||
**无头模式**:只要终端、文件传输和端口映射,不需要图形界面。在服务里加 `Environment=UU_DESKTOP_BACKEND=none`,
|
||||
再执行 `loginctl enable-linger $USER`,这样开机后不用登录,systemd 用户服务(以及 `XDG_RUNTIME_DIR`、用户 D-Bus)也会自动启动。
|
||||
无头模式下控制端点「远程桌面」会一直显示正在连接,这是正常的。首次登录用 `uuremote-server login --no-gui` 在终端里扫码。
|
||||
|
||||
**虚拟桌面**:需要完整的远程桌面时,用 Xvfb 创建一块虚拟屏幕,再在上面跑一个桌面环境(推荐 Xfce):
|
||||
|
||||
```sh
|
||||
sudo apt install xvfb xauth x11-utils dbus-x11 dbus-user-session xfce4 # 或其他桌面环境,用 UU_VIRTUAL_SESSION 指定
|
||||
install -Dm755 contrib/virtual-desktop/uu-virtual-desktop ~/.local/bin/uu-virtual-desktop
|
||||
install -Dm644 contrib/virtual-desktop/uu-virtual-desktop.service ~/.config/systemd/user/uu-virtual-desktop.service
|
||||
install -Dm644 contrib/virtual-desktop/virtual-display.conf ~/.config/systemd/user/uuremote-native.service.d/virtual-display.conf
|
||||
loginctl enable-linger $USER
|
||||
systemctl --user daemon-reload && systemctl --user enable --now uu-virtual-desktop.service
|
||||
systemctl --user restart uuremote-native.service
|
||||
```
|
||||
|
||||
虚拟屏幕默认是 `:99`、1920x1080。Xvfb 没有 GPU 加速,流畅度比真实显示器略差;有 NVIDIA 显卡时视频仍然用 NVENC 编码。
|
||||
脚本会把 `DISPLAY` 导入 systemd 用户会话,这样通过 D-Bus 启动的程序也会显示在虚拟屏幕上;
|
||||
如果这台机器同时还有真实的桌面会话,请设 `UU_VIRTUAL_BUS=private`,避免影响真实桌面。
|
||||
|
||||
想让 NVIDIA/AMD 显卡在不接显示器时也正常输出画面(而不是用 Xvfb),可以用假显示器插头(HDMI 诱骗器),
|
||||
或者用内核参数 `drm.edid_firmware=` 加载 EDID 文件,再按普通 X11 桌面的方式安装。
|
||||
|
||||
## 配置
|
||||
|
||||
常用环境变量:
|
||||
@@ -149,8 +196,10 @@ journalctl --user -u uuremote-native.service -f # 看到 availability=Some("re
|
||||
| 变量 | 作用 |
|
||||
|---|---|
|
||||
| `UU_CONFIG` | 配置文件路径(默认 `~/.config/uuremote/config.json`) |
|
||||
| `UU_DESKTOP_BACKEND` | `auto` / `x11` / `wayland` / `portal` |
|
||||
| `UU_VIDEO_ENCODER=cpu` | 强制使用 CPU 编码 |
|
||||
| `UU_DESKTOP_BACKEND` | `auto` / `x11` / `wayland` / `portal` / `kms`(实验性特性) / `none`(无头模式) |
|
||||
| `UU_INPUT_BACKEND` | `auto`(默认,Wayland 下可用时用 uinput)/ `uinput` / `native` |
|
||||
| `UU_TERMINAL_SHORTCUTS=0` | 关闭终端窗口里的 Ctrl+V 粘贴转换 |
|
||||
| `UU_VIDEO_ENCODER` | `cpu` 强制 CPU;`vaapi` 显式启用 Intel/AMD 硬编;`nvenc` 强制 NVENC;未设置时检测到 NVIDIA 驱动才用 NVENC,否则用 CPU |
|
||||
| `UU_FFMPEG_ROOT` | 使用指定目录下的 FFmpeg |
|
||||
| `UU_AUDIO=0` | 关闭声音转发;`UU_AUDIO_SOURCE` 指定 PulseAudio 采集源 |
|
||||
| `UU_LOGIN_WINDOW=0` | 凭证缺失时不自动弹出登录窗口 |
|
||||
@@ -201,3 +250,58 @@ flowchart LR
|
||||
- 网易 UU 远程团队:做了一个好用的远程工具。
|
||||
- [webrtc-rs](https://github.com/webrtc-rs/webrtc)、FFmpeg、GTK 等开源项目。
|
||||
- 本项目的大部分代码在 Claude 和 Codex 的协助下完成。
|
||||
|
||||
### VAAPI 硬件编码(显式启用)
|
||||
|
||||
设置 `UU_VIDEO_ENCODER=vaapi` 使用 Intel/AMD H.264 VAAPI;`UU_VAAPI_DEVICE` 默认
|
||||
`/dev/dri/renderD128`,运行用户需要 render 节点权限。不会自动选择 VAAPI;设备或驱动初始化
|
||||
失败会明确警告并回退 libx264。外部 FFmpeg 和 `--features inproc-av` 均先转 NV12 再上传硬件帧。
|
||||
VBR 使用控制端码率预设及 CQ 对应的最小 QP,无 B 帧、async depth=1,并保留现有 GOP/VFR 规则。
|
||||
Intel 核显(UHD 770,iHD 驱动)已实机验收:默认的外部 FFmpeg 管线用官方 Mac 控制端测试了各档画质、帧率、缩放和重连,全程硬件编码、无回退,CPU 占用比 libx264 低约 40%。**AMD 显卡和 `--features inproc-av` 管线的 VAAPI 尚未实机验证。**
|
||||
|
||||
### 静音运行与虚拟声卡
|
||||
|
||||
控制端开启「静音运行」后,程序创建虚拟输出 `uu-remote-sink`,保存原默认输出,
|
||||
迁移其播放流并采集 `uu-remote-sink.monitor`。新播放也走虚拟输出;其他输出上的流不迁移。
|
||||
最后一个桌面连接结束后恢复原默认输出并卸载模块;`mute_permanent` 仍保留原物理静音策略。
|
||||
恢复记录保存在配置旁,权限 0600;进程锁避免重复实例抢占,异常退出后下次启动会恢复遗留路由。
|
||||
有 `pactl` 时用它(PulseAudio / PipeWire-pulse);没有 `pactl` 的纯 PipeWire 系统(如默认的 Ubuntu 桌面)
|
||||
自动改用 `pw-cli`/`wpctl` 创建同名虚拟输出,正在播放的声音由 WirePlumber 自动迁移。已在 Ubuntu 24.04 实机上用官方 Mac 控制端验收。
|
||||
路由失败时保留原静音/monitor 方案。开关变化仅重启当前音频编码器,未开启时采集方式保持不变。
|
||||
本地诊断命令 `virtual-audio-probe` 会临时切换输出五秒并恢复。
|
||||
|
||||
### 虚拟桌面的用户总线
|
||||
|
||||
默认 `UU_VIRTUAL_BUS=user`:监督脚本将 DISPLAY/XAUTHORITY 导入 linger 用户总线,等待经过认证的
|
||||
X11 就绪后通知 systemd;退出时清理 Xvfb。示例主机 drop-in 使用 `%t/uu-virtual-99.Xauthority`。
|
||||
请使用没有真实图形会话的专用账号;参见[设置与官方 Mac 验收清单](contrib/virtual-desktop/ACCEPTANCE.md)。
|
||||
|
||||
### Arch Linux / Omarchy
|
||||
|
||||
```sh
|
||||
sudo pacman -S --needed base-devel rust pkgconf clang ffmpeg libx11 libxext libxtst libxfixes gtk3 \
|
||||
wayland libxkbcommon wl-clipboard libpipewire glib2 libpulse
|
||||
cargo test --locked --features wayland
|
||||
cargo build --release --features wayland
|
||||
```
|
||||
|
||||
Omarchy 使用 Hyprland:在其用户会话中运行,设置 `UU_DESKTOP_BACKEND=wayland`,保留
|
||||
`WAYLAND_DISPLAY` 和 `XDG_RUNTIME_DIR`。其他合成器可能需要 `--features wayland,portal`。
|
||||
uinput 需加载内核模块并应用上文 uaccess 规则;无人登录账号需要专用组的受限授权。
|
||||
该权限能向整个 seat(包括锁屏/登录界面)注入输入。设置 `UU_INPUT_BACKEND=uinput` 后,
|
||||
仍须确认启动日志实际选择了 uinput。
|
||||
|
||||
VAAPI 可选包:Intel 新显卡用 `intel-media-driver`,AMD 用 `libva-mesa-driver`,`libva-utils` 提供
|
||||
`vainfo`。KMS 依赖 `libdrm`。虚拟 Xfce 依赖 `xorg-server-xvfb xorg-xauth xorg-xdpyinfo
|
||||
xfce4-session xfce4-panel xfdesktop xfwm4 gnome-terminal`。
|
||||
滚动 Arch 的 FFmpeg 可能超出 `inproc-av` 支持的 6.x;请使用默认进程方案或独立的 FFmpeg 6 前缀,
|
||||
不要降级系统 FFmpeg。
|
||||
|
||||
### 实验性 KMS 截屏
|
||||
|
||||
编译 `--features kms` 后显式设置 `UU_DESKTOP_BACKEND=kms`;`UU_KMS_DEVICE` 默认
|
||||
`/dev/dri/card0`。需要 libdrm、card 节点访问权及主动授予的 `cap_sys_admin+ep`。
|
||||
程序不会自动启用或授权;此 capability 权限很大,此 capability 权限很大:它作用于整个联网的服务进程,也能看到登录界面/锁屏等原本受授权保护的画面;只在可丢弃的机器上、对审查过的二进制使用,用 `getcap` 检查,`sudo setcap -r <路径>` 撤销。目前在虚拟机里登录界面截图全黑,不支持锁屏/登录界面。
|
||||
当前仅为线性帧缓冲原型:VM 的 GDM 截图仍是黑屏,**没有通过登录界面视觉验收**,不承诺锁屏可用。
|
||||
键鼠需 uinput;没有桌面会话时不提供剪贴板。
|
||||
无头模式下桌面连接没有找到官方的「无桌面」错误码,因此保持原有 offer 行为。
|
||||
@@ -9,6 +9,16 @@ fn main() {
|
||||
|
||||
let target_os = std::env::var("CARGO_CFG_TARGET_OS").unwrap_or_default();
|
||||
if target_os == "linux" {
|
||||
if std::env::var_os("CARGO_FEATURE_KMS").is_some() {
|
||||
println!("cargo:rerun-if-changed=c_engine/kms_engine.c");
|
||||
let output=std::process::Command::new("pkg-config").args(["--cflags","libdrm"]).output().expect("KMS requires pkg-config and libdrm development headers");
|
||||
assert!(output.status.success(), "KMS requires libdrm development headers");
|
||||
let mut build=cc::Build::new();
|
||||
build.file("c_engine/kms_engine.c");
|
||||
for flag in String::from_utf8_lossy(&output.stdout).split_whitespace() { build.flag(flag); }
|
||||
build.compile("uu_kms_engine");
|
||||
println!("cargo:rustc-link-lib=drm");
|
||||
}
|
||||
cc::Build::new()
|
||||
.file("c_engine/x11_engine.c")
|
||||
.file("c_engine/clipboard.c")
|
||||
@@ -62,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")] {
|
||||
@@ -80,4 +90,3 @@ fn main() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
/* Opt-in KMS prototype: read active primary-plane linear XRGB/ARGB buffers.
|
||||
* No modesetting, DRM master acquisition, authentication or content logging.
|
||||
*/
|
||||
#include <xf86drm.h>
|
||||
#include <xf86drmMode.h>
|
||||
#include <drm_fourcc.h>
|
||||
#include <sys/mman.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
struct uu_kms { int fd; uint32_t crtc, plane, width, height; };
|
||||
static uint32_t primary_plane(int fd, uint32_t crtc_id) {
|
||||
drmModePlaneRes *planes=drmModeGetPlaneResources(fd);
|
||||
if (!planes) return 0;
|
||||
uint32_t selected=0;
|
||||
for (uint32_t i=0;i<planes->count_planes && !selected;i++) {
|
||||
drmModePlane *plane=drmModeGetPlane(fd,planes->planes[i]);
|
||||
if (plane && plane->crtc_id==crtc_id && plane->fb_id) {
|
||||
drmModeObjectProperties *props=drmModeObjectGetProperties(fd,plane->plane_id,DRM_MODE_OBJECT_PLANE);
|
||||
if (props) {
|
||||
for (uint32_t j=0;j<props->count_props;j++) {
|
||||
drmModePropertyRes *prop=drmModeGetProperty(fd,props->props[j]);
|
||||
if (prop && strcmp(prop->name,"type")==0 && props->prop_values[j]==DRM_PLANE_TYPE_PRIMARY) selected=plane->plane_id;
|
||||
if (prop) drmModeFreeProperty(prop);
|
||||
}
|
||||
drmModeFreeObjectProperties(props);
|
||||
}
|
||||
}
|
||||
if (plane) drmModeFreePlane(plane);
|
||||
}
|
||||
drmModeFreePlaneResources(planes);
|
||||
return selected;
|
||||
}
|
||||
static void close_handles(int fd, drmModeFB2 *fb) {
|
||||
for (int i=0;i<4;i++) {
|
||||
if (!fb->handles[i]) continue;
|
||||
int duplicate=0;
|
||||
for (int j=0;j<i;j++) if (fb->handles[j]==fb->handles[i]) duplicate=1;
|
||||
if (!duplicate) { struct drm_gem_close c={.handle=fb->handles[i]}; drmIoctl(fd,DRM_IOCTL_GEM_CLOSE,&c); }
|
||||
}
|
||||
}
|
||||
void uu_kms_close(struct uu_kms *s) { if (s) { close(s->fd); free(s); } }
|
||||
struct uu_kms *uu_kms_open(const char *device, uint32_t *w, uint32_t *h) {
|
||||
int fd=open(device,O_RDWR|O_CLOEXEC);
|
||||
if (fd<0) return NULL;
|
||||
if (drmSetClientCap(fd,DRM_CLIENT_CAP_UNIVERSAL_PLANES,1)<0) { close(fd); return NULL; }
|
||||
drmModeRes *res=drmModeGetResources(fd);
|
||||
if (!res) { close(fd); return NULL; }
|
||||
struct uu_kms *s=NULL;
|
||||
for (int i=0;i<res->count_crtcs;i++) {
|
||||
drmModeCrtc *crtc=drmModeGetCrtc(fd,res->crtcs[i]);
|
||||
uint32_t plane=crtc ? primary_plane(fd,crtc->crtc_id) : 0;
|
||||
if (crtc && plane && crtc->mode_valid && crtc->width && crtc->height && crtc->width<=16384 && crtc->height<=16384) {
|
||||
s=calloc(1,sizeof(*s));
|
||||
if (s) { s->fd=fd; s->crtc=crtc->crtc_id; s->plane=plane; s->width=crtc->width; s->height=crtc->height; *w=s->width; *h=s->height; }
|
||||
}
|
||||
if (crtc) drmModeFreeCrtc(crtc);
|
||||
if (s) break;
|
||||
}
|
||||
drmModeFreeResources(res);
|
||||
if (!s) close(fd);
|
||||
return s;
|
||||
}
|
||||
int uu_kms_read(struct uu_kms *s, uint8_t *out, size_t length) {
|
||||
drmModeCrtc *crtc=drmModeGetCrtc(s->fd,s->crtc);
|
||||
if (!crtc) return -1;
|
||||
drmModePlane *plane=drmModeGetPlane(s->fd,s->plane);
|
||||
uint32_t id=plane ? plane->fb_id : 0;
|
||||
if (plane) drmModeFreePlane(plane);
|
||||
int valid=crtc->mode_valid && crtc->width==s->width && crtc->height==s->height;
|
||||
drmModeFreeCrtc(crtc);
|
||||
if (!valid || !id) return -2; /* mode changed: caller must reopen */
|
||||
drmModeFB2 *fb=drmModeGetFB2(s->fd,id);
|
||||
if (!fb) return -3;
|
||||
int result=-4;
|
||||
if (!fb->handles[0]) { result=-5; goto done; } /* DRM master or CAP_SYS_ADMIN needed */
|
||||
if (fb->width!=s->width || fb->height!=s->height ||
|
||||
(fb->pixel_format!=DRM_FORMAT_XRGB8888 && fb->pixel_format!=DRM_FORMAT_ARGB8888) ||
|
||||
((fb->flags & DRM_MODE_FB_MODIFIERS) && fb->modifier!=DRM_FORMAT_MOD_LINEAR) ||
|
||||
fb->handles[1] || fb->pitches[0] < s->width*4 || length!=(size_t)s->width*s->height*4) goto done;
|
||||
struct drm_mode_map_dumb map={.handle=fb->handles[0]};
|
||||
if (drmIoctl(s->fd,DRM_IOCTL_MODE_MAP_DUMB,&map)<0) { result=-6; goto done; }
|
||||
size_t bytes=(size_t)fb->pitches[0]*fb->height+fb->offsets[0];
|
||||
void *data=mmap(NULL,bytes,PROT_READ,MAP_SHARED,s->fd,map.offset);
|
||||
if (data==MAP_FAILED) { result=-7; goto done; }
|
||||
for (uint32_t y=0;y<s->height;y++) {
|
||||
uint8_t *row=out+(size_t)y*s->width*4;
|
||||
memcpy(row,(uint8_t*)data+fb->offsets[0]+(size_t)y*fb->pitches[0],s->width*4);
|
||||
for (uint32_t x=0;x<s->width;x++) row[x*4+3]=255;
|
||||
}
|
||||
munmap(data,bytes);
|
||||
result=0;
|
||||
done:
|
||||
close_handles(s->fd,fb);
|
||||
drmModeFreeFB2(fb);
|
||||
return result;
|
||||
}
|
||||
@@ -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>
|
||||
+236
-36
@@ -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"
|
||||
@@ -21,6 +24,28 @@ static struct wl_display *display;
|
||||
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 +60,24 @@ 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;
|
||||
void *pixels;
|
||||
size_t length;
|
||||
uint32_t width,height,stride,format,flags;
|
||||
int done,failed,submitted;
|
||||
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;
|
||||
};
|
||||
static struct frame frames[2];
|
||||
static unsigned pending;
|
||||
|
||||
static uint32_t timestamp(void) {
|
||||
struct timespec t; clock_gettime(CLOCK_MONOTONIC,&t);
|
||||
@@ -82,6 +120,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 +142,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 +155,160 @@ 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 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->buffer) wl_buffer_destroy(frame->buffer);
|
||||
if(frame->dmabuf_used && frame->pixels) gbm.bo_unmap(frame->bo,frame->map_data);
|
||||
else if(frame->pixels) munmap(frame->pixels,frame->length);
|
||||
if(frame->bo) { if(frame->bo_fd>=0)close(frame->bo_fd); gbm.bo_destroy(frame->bo); }
|
||||
memset(frame,0,sizeof(*frame));
|
||||
}
|
||||
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;
|
||||
if (fd<0 || ftruncate(fd,(off_t)length)!=0) { if(fd>=0)close(fd); frame_fail(frame); return; }
|
||||
frame->pixels=mmap(NULL,length,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
|
||||
if (frame->pixels==MAP_FAILED) { frame->pixels=NULL; close(fd); frame_fail(frame); return; }
|
||||
frame->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);
|
||||
frame->buffer=wl_shm_pool_create_buffer(pool,0,w,h,s,fmt);
|
||||
wl_shm_pool_destroy(pool); close(fd);
|
||||
/* 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->buffer); 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;
|
||||
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) { 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->buffer && !frame->failed) {
|
||||
zwlr_screencopy_frame_v1_copy(frame->handle,frame->buffer); 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;
|
||||
wl_buffer_destroy(frame->buffer);
|
||||
munmap(frame->pixels,frame->length); frame->pixels=NULL; frame->length=0;
|
||||
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;
|
||||
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"); 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->buffer ||
|
||||
!(dmabuf_linear_formats&dma_format_bit(frame->dma_format))) return 0;
|
||||
/* 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"); return 0;}
|
||||
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,©_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,©_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);
|
||||
for(unsigned i=0;i<2;i++) frame_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;
|
||||
/* Disconnect drops every proxy; failure never leaves an initialized backend. */
|
||||
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;
|
||||
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);
|
||||
wl_registry_add_listener(registry,®istry_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) {
|
||||
@@ -203,18 +355,41 @@ 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;
|
||||
for(unsigned i=0;i<2;i++) frame_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]);
|
||||
pthread_mutex_unlock(&lock); return result;
|
||||
}
|
||||
int uu_wl_move(int32_t x,int32_t y,int relative) {
|
||||
@@ -275,3 +450,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;
|
||||
}
|
||||
+71
-2
@@ -9,6 +9,8 @@
|
||||
#include <sys/ipc.h>
|
||||
#include <X11/Xlib.h>
|
||||
#include <X11/Xutil.h>
|
||||
#include <X11/Xatom.h>
|
||||
#include <strings.h>
|
||||
#include <X11/extensions/XShm.h>
|
||||
#include <X11/extensions/XTest.h>
|
||||
#include <X11/extensions/Xfixes.h>
|
||||
@@ -58,9 +60,12 @@ int x11_init(const char *display_name) {
|
||||
g_display = XOpenDisplay(display_name);
|
||||
} else {
|
||||
const char *configured = getenv("DISPLAY");
|
||||
/* An explicit DISPLAY that fails must not silently fall back to another (physical) screen. */
|
||||
if (configured && *configured) g_display = XOpenDisplay(configured);
|
||||
if (!g_display) g_display = XOpenDisplay(":0");
|
||||
if (!g_display) g_display = XOpenDisplay(":1");
|
||||
else {
|
||||
g_display = XOpenDisplay(":0");
|
||||
if (!g_display) g_display = XOpenDisplay(":1");
|
||||
}
|
||||
}
|
||||
if (!g_display) {
|
||||
return -1;
|
||||
@@ -190,6 +195,70 @@ 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
|
||||
* connection so it never races the capture/input connection. */
|
||||
static Display *g_probe = NULL;
|
||||
|
||||
static int class_is_terminal(const char *value) {
|
||||
static const char *names[] = { "gnome-terminal", "gnome-terminal-server", "org.gnome.Console", "kgx", "konsole",
|
||||
"xfce4-terminal", "mate-terminal", "lxterminal", "qterminal", "terminator", "tilix", "xterm", "uxterm",
|
||||
"urxvt", "rxvt", "alacritty", "kitty", "wezterm", "wezterm-gui", "foot", "st-256color", "terminology",
|
||||
"guake", "tilda", "deepin-terminal", "ptyxis", "io.elementary.terminal", "cool-retro-term", NULL };
|
||||
if (!value) return 0;
|
||||
for (int i = 0; names[i]; i++) if (strcasecmp(value, names[i]) == 0) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Returns 1 when the focused window belongs to a terminal emulator, else 0. */
|
||||
int x11_terminal_shortcut_state(void) {
|
||||
if (!g_probe && !(g_probe = XOpenDisplay(NULL))) return 0;
|
||||
Window focus = 0; int revert = 0;
|
||||
XGetInputFocus(g_probe, &focus, &revert);
|
||||
if (focus == None || focus == PointerRoot) return 0;
|
||||
Window cur = focus;
|
||||
for (int depth = 0; depth < 12 && cur; depth++) {
|
||||
XClassHint hint = {0};
|
||||
if (XGetClassHint(g_probe, cur, &hint)) {
|
||||
int terminal = class_is_terminal(hint.res_name) || class_is_terminal(hint.res_class);
|
||||
if (hint.res_name) XFree(hint.res_name);
|
||||
if (hint.res_class) XFree(hint.res_class);
|
||||
return terminal;
|
||||
}
|
||||
Window root, parent, *children = NULL; unsigned int n = 0;
|
||||
if (!XQueryTree(g_probe, cur, &root, &parent, &children, &n)) return 0;
|
||||
if (children) XFree(children);
|
||||
if (parent == root) return 0;
|
||||
cur = parent;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void x11_close() {
|
||||
if (!g_display) return;
|
||||
if (g_use_shm) {
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
# Virtual user-bus desktop acceptance
|
||||
|
||||
Install Xvfb, xauth, xdpyinfo, Xfce and dbus-user-session (Debian) or dbus-broker (Arch).
|
||||
The default is `UU_VIRTUAL_BUS=user`; use it on a dedicated headless account with no real
|
||||
GUI session. Its D-Bus and systemd activation environment is shared by all that user's apps.
|
||||
`private` isolates ordinary D-Bus activation but cannot redirect systemd-activated programs.
|
||||
|
||||
1. Install the script in `~/.local/bin`, the unit in `~/.config/systemd/user`, and
|
||||
`virtual-display.conf` in the host service's `.service.d/` directory.
|
||||
2. `sudo loginctl enable-linger "$USER"`; set `UU_VIRTUAL_SESSION=startxfce4` in the desktop
|
||||
unit. `systemctl --user daemon-reload`; enable both units. A different display needs a
|
||||
matching `DISPLAY` and `XAUTHORITY=%t/uu-virtual-N.Xauthority` in the host drop-in.
|
||||
3. Reboot a disposable VM with graphical login disabled. Before logging into a desktop,
|
||||
check both user units are active, Xvfb is `:99`, no real GUI login session exists, and
|
||||
host availability is `ready`. The desktop unit uses notify readiness after authenticated
|
||||
X11 access succeeds, so the host does not race the X socket.
|
||||
4. `DISPLAY=:99 XAUTHORITY="$XDG_RUNTIME_DIR/uu-virtual-99.Xauthority" xdpyinfo` must work.
|
||||
`gnome-terminal` launched over the user bus must open on `:99`. Check its window via
|
||||
`xdotool search --class '[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.
|
||||
Executable
+89
@@ -0,0 +1,89 @@
|
||||
#!/bin/sh
|
||||
# Virtual desktop for a monitorless Linux host: Xvfb + a desktop session on the user bus (or an isolated private bus).
|
||||
# uuremote-server then serves it with DISPLAY=$UU_VIRTUAL_DISPLAY (see README in this folder).
|
||||
# UU_VIRTUAL_DISPLAY X display number (default :99)
|
||||
# UU_VIRTUAL_SIZE screen size (default 1920x1080)
|
||||
# UU_VIRTUAL_SESSION session command (default: first of startxfce4, mate-session, startlxqt, openbox-session, xterm)
|
||||
# UU_VIRTUAL_BUS user (default): the systemd user bus (`loginctl enable-linger`) gets DISPLAY, so
|
||||
# D-Bus/systemd-activated apps (GNOME terminal, portals, ...) open on the virtual screen;
|
||||
# private: own dbus-run-session bus, for machines that also run a real desktop session
|
||||
set -eu
|
||||
display="${UU_VIRTUAL_DISPLAY:-:99}"
|
||||
size="${UU_VIRTUAL_SIZE:-1920x1080}"
|
||||
session="${UU_VIRTUAL_SESSION:-}"
|
||||
if [ -z "$session" ]; then
|
||||
for candidate in startxfce4 mate-session startlxqt openbox-session xterm; do
|
||||
if command -v "$candidate" >/dev/null 2>&1; then session="$candidate"; break; fi
|
||||
done
|
||||
fi
|
||||
[ -n "$session" ] || { echo "no desktop session found; set UU_VIRTUAL_SESSION" >&2; exit 1; }
|
||||
command -v Xvfb >/dev/null || { echo "Xvfb not installed (apt install xvfb)" >&2; exit 1; }
|
||||
bus="${UU_VIRTUAL_BUS:-user}"
|
||||
case "$bus" in user|private) ;; *) echo "UU_VIRTUAL_BUS must be user or private" >&2; exit 1 ;; esac
|
||||
command -v xauth >/dev/null || { echo "xauth not installed" >&2; exit 1; }
|
||||
command -v xdpyinfo >/dev/null || { echo "xdpyinfo not installed" >&2; exit 1; }
|
||||
case "$display" in :[0-9]*) ;; *) echo "UU_VIRTUAL_DISPLAY must be a local display number" >&2; exit 1 ;; esac
|
||||
number="${display#:}"
|
||||
case "$number" in *[!0-9]*|'') echo "invalid display number" >&2; exit 1 ;; esac
|
||||
socket="/tmp/.X11-unix/X$number"
|
||||
# A live server owns the display; a dead one's leftovers (killed Xvfb) are cleaned up by Xvfb itself.
|
||||
lock="/tmp/.X$number-lock"
|
||||
if [ -e "$socket" ] || [ -e "$lock" ]; then
|
||||
owner=$(tr -d ' \n' < "$lock" 2>/dev/null || :)
|
||||
case "$owner" in ''|*[!0-9]*) owner= ;; esac
|
||||
if [ -z "$owner" ] || kill -0 "$owner" 2>/dev/null; then echo "display already in use" >&2; exit 1; fi
|
||||
echo "removing stale display $display left by dead process $owner" >&2
|
||||
rm -f "$lock" "$socket" 2>/dev/null || { echo "cannot remove stale display files" >&2; exit 1; }
|
||||
fi
|
||||
umask 077
|
||||
export XDG_RUNTIME_DIR="${XDG_RUNTIME_DIR:-/run/user/$(id -u)}"
|
||||
authority="$XDG_RUNTIME_DIR/uu-virtual-$number.Xauthority"
|
||||
# The display socket was checked above; replace a stale authority after a service crash.
|
||||
[ ! -L "$authority" ] || { echo "refusing a symlink authority" >&2; exit 1; }
|
||||
temporary=$(mktemp "$XDG_RUNTIME_DIR/uu-virtual-xauth.XXXXXX")
|
||||
xvfb=
|
||||
child=
|
||||
cleanup() {
|
||||
trap - EXIT INT TERM
|
||||
[ -z "$child" ] || kill "$child" 2>/dev/null || :
|
||||
[ -z "$xvfb" ] || kill "$xvfb" 2>/dev/null || :
|
||||
[ -z "$xvfb" ] || wait "$xvfb" 2>/dev/null || :
|
||||
rm -f "$authority" "$temporary"
|
||||
}
|
||||
trap cleanup EXIT
|
||||
trap 'exit 143' TERM
|
||||
trap 'exit 130' INT
|
||||
cookie=$(od -An -N16 -tx1 /dev/urandom | tr -d ' \n')
|
||||
xauth -f "$temporary" add "$display" MIT-MAGIC-COOKIE-1 "$cookie"
|
||||
unset cookie
|
||||
mv "$temporary" "$authority"
|
||||
export XAUTHORITY="$authority"
|
||||
|
||||
Xvfb "$display" -screen 0 "${size}x24" -auth "$authority" -nolisten tcp -noreset &
|
||||
xvfb=$!
|
||||
export DISPLAY="$display"
|
||||
unset WAYLAND_DISPLAY SESSION_MANAGER
|
||||
export XDG_SESSION_TYPE=x11
|
||||
ready=false
|
||||
for _ in $(seq 50); do
|
||||
kill -0 "$xvfb" 2>/dev/null || break
|
||||
if xdpyinfo >/dev/null 2>&1; then ready=true; break; fi
|
||||
sleep 0.1
|
||||
done
|
||||
[ "$ready" = true ] || { echo "Xvfb did not become ready" >&2; exit 1; }
|
||||
if [ "$bus" = user ]; then
|
||||
# Linger creates the user manager and its bus without a graphical login.
|
||||
: "${XDG_RUNTIME_DIR:=/run/user/$(id -u)}"
|
||||
export XDG_RUNTIME_DIR
|
||||
export DBUS_SESSION_BUS_ADDRESS="unix:path=$XDG_RUNTIME_DIR/bus"
|
||||
[ -S "$XDG_RUNTIME_DIR/bus" ] || { echo "user bus unavailable; enable loginctl enable-linger" >&2; exit 1; }
|
||||
systemctl --user unset-environment WAYLAND_DISPLAY
|
||||
dbus-update-activation-environment --systemd DISPLAY XAUTHORITY XDG_SESSION_TYPE
|
||||
# Keep the supervising shell alive: exec would discard the Xvfb cleanup trap.
|
||||
sh -c "$session" &
|
||||
else
|
||||
dbus-run-session -- sh -c "$session" &
|
||||
fi
|
||||
child=$!
|
||||
if [ -n "${NOTIFY_SOCKET:-}" ]; then systemd-notify --ready; fi
|
||||
wait "$child"
|
||||
@@ -0,0 +1,19 @@
|
||||
[Unit]
|
||||
Description=Virtual X desktop for uu-remote-linux (Xvfb + session bus)
|
||||
After=dbus.socket
|
||||
|
||||
[Service]
|
||||
Type=notify
|
||||
NotifyAccess=all
|
||||
TimeoutStartSec=30
|
||||
TimeoutStopSec=10
|
||||
KillMode=control-group
|
||||
# Environment=UU_VIRTUAL_SESSION=startxfce4
|
||||
# Environment=UU_VIRTUAL_SIZE=1920x1080
|
||||
# Environment=UU_VIRTUAL_BUS=private
|
||||
ExecStart=%h/.local/bin/uu-virtual-desktop
|
||||
Restart=always
|
||||
RestartSec=5
|
||||
|
||||
[Install]
|
||||
WantedBy=default.target
|
||||
@@ -0,0 +1,11 @@
|
||||
# ~/.config/systemd/user/uuremote-native.service.d/virtual-display.conf
|
||||
# Serve the virtual desktop instead of a physical display.
|
||||
[Unit]
|
||||
Requires=uu-virtual-desktop.service
|
||||
After=uu-virtual-desktop.service
|
||||
|
||||
[Service]
|
||||
Environment=DISPLAY=:99
|
||||
Environment=UU_DESKTOP_BACKEND=x11
|
||||
Environment=XAUTHORITY=%t/uu-virtual-99.Xauthority
|
||||
UnsetEnvironment=WAYLAND_DISPLAY
|
||||
@@ -0,0 +1,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.**
|
||||
@@ -0,0 +1,22 @@
|
||||
修复长时间远控约 20 分钟后被断开的问题。/ Fixes sessions being dropped after ~20 minutes.
|
||||
|
||||
### 修复 / Fixed
|
||||
- **远控约 20 分钟后被云端断开**:官方客户端会定期(约每 5 分钟)通过信令刷新房间的重连凭证(`refresh_reconnect_key`),
|
||||
之前没有实现,云端大约 20 分钟后就会回收房间并断开连接。现在被控端会按官方节奏刷新;凭证只保存在内存中,不写日志。
|
||||
在测试虚拟机里长连接稳定运行 44 分钟以上(已越过原来的断开点),在 X11 实机上确认刷新被云端接受。
|
||||
Sessions were released by the cloud after ~20 minutes because the room's reconnect key was never refreshed. The host now
|
||||
refreshes it like the official clients (~every 5 minutes); the key stays in memory and is never logged.
|
||||
- 旧的信令连接在重连后会被正确关闭。/ Stale signaling connections are closed after a reconnect.
|
||||
|
||||
### 其他 / Other
|
||||
- 可选的进程内编码路径支持 XDamage:桌面静止时几乎不占 CPU 和带宽(`--features inproc-av`,默认关闭)(#7,感谢 @ferstar)。
|
||||
XDamage capture on the opt-in in-process path (#7, thanks @ferstar).
|
||||
- Wayland(portal)下双向剪贴板已在虚拟机中验收通过。注意:官方 Mac 客户端只在远控窗口处于前台(获得焦点)时接收被控端的剪贴板。
|
||||
Two-way clipboard on the portal backend passes in the VM; the official Mac viewer only accepts the host clipboard while the
|
||||
remote window has focus.
|
||||
- 更多信令诊断日志(不含任何凭证)。/ More signaling diagnostics (no credentials).
|
||||
|
||||
### 下载 / Download
|
||||
`uu-remote-linux-v0.1.5-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35)。
|
||||
|
||||
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||
@@ -0,0 +1,28 @@
|
||||
无头服务器、虚拟桌面、uinput 输入和终端快捷键。/ Headless servers, virtual desktop, uinput input and terminal shortcuts.
|
||||
|
||||
### 新增 / Added
|
||||
- **无头模式** `UU_DESKTOP_BACKEND=none`:没有显示器、没有图形会话也能上线,提供终端、文件传输和端口映射。
|
||||
配合 `loginctl enable-linger` 可以开机自动上线。已在实机上用官方 Mac 客户端验收(端口映射传输 10 MB 校验一致)。
|
||||
Headless mode: online without a display; terminal, file transfer and port mapping verified with the official Mac viewer.
|
||||
- **虚拟桌面** `contrib/virtual-desktop/`:用 Xvfb 创建虚拟屏幕并启动桌面会话,没有显示器的服务器也能使用完整的远程桌面。
|
||||
实机验收了画面和键鼠;默认的「用户总线」模式(把 `DISPLAY` 导入 systemd 用户会话)尚未在纯无头机器上验证。
|
||||
Virtual desktop scripts (Xvfb + session). Video and input verified; the default user-bus mode is not yet verified on a
|
||||
truly monitorless machine.
|
||||
- **uinput 虚拟键鼠**(参考 Sunshine):Wayland/portal 下,只要当前用户能访问 `/dev/uinput`(README 里有一条 udev 规则),
|
||||
就自动改用内核级虚拟键盘和鼠标,发送物理按键码,不依赖键盘布局,也不需要 portal 输入授权。应能解决 #3(下划线被吞)。
|
||||
`UU_INPUT_BACKEND=auto|uinput|native`;X11 继续默认用 XTest。
|
||||
uinput keyboard/mouse on Wayland (layout independent, no portal input consent); should fix #3.
|
||||
- **终端快捷键**:Mac 控制端的 Cmd 和 Ctrl 到被控端都是 Ctrl。焦点在终端窗口时,Ctrl+V / Cmd+V 自动转成 Ctrl+Shift+V 粘贴;
|
||||
Ctrl+C 始终是中断,复制用 Cmd+Shift+C(X11,`UU_TERMINAL_SHORTCUTS=0` 关闭)。
|
||||
Terminal-aware paste: Ctrl+V/Cmd+V paste in terminal windows, Ctrl+C always interrupts.
|
||||
|
||||
### 修复 / Fixed
|
||||
- 没有桌面时(无头模式,或 portal 后端还没授权时)打开端口映射会被控制端立即断开:现在工具连接会收到屏幕信息和「允许」权限。
|
||||
Port mapping was aborted by the viewer when no desktop was available.
|
||||
- 明确设置的 `DISPLAY` 打不开时,不再悄悄改用 `:0`/`:1`(会截到真实屏幕),而是报错。
|
||||
An explicit DISPLAY that cannot be opened no longer falls back to the physical screen.
|
||||
|
||||
### 下载 / Download
|
||||
`uu-remote-linux-v0.1.6-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/virtual-desktop/`。
|
||||
|
||||
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||
@@ -0,0 +1,31 @@
|
||||
VAAPI 硬件编码、虚拟声卡、无人登录的虚拟桌面和 Arch/Omarchy 支持。/ VAAPI encoding, virtual audio sink, linger virtual desktop, Arch/Omarchy.
|
||||
|
||||
### 新增 / Added
|
||||
- **VAAPI 硬件编码(Intel/AMD)**:`UU_VIDEO_ENCODER=vaapi`(`UU_VAAPI_DEVICE` 默认 `/dev/dri/renderD128`),需要显式开启,不会自动选择;
|
||||
初始化失败时自动回退 CPU 编码。✅ Intel UHD 770(iHD 驱动)实机验收:用官方 Mac 控制端测试了各档画质、30/60 帧、缩放和重连,
|
||||
全程硬件编码,CPU 占用比 libx264 低约 40%。⚠️ AMD 显卡、`--features inproc-av` 管线的 VAAPI 尚未实机验证。
|
||||
Opt-in VAAPI with CPU fallback. Accepted on Intel UHD 770; AMD and the inproc-av path are not hardware-verified.
|
||||
- **虚拟声卡(静音运行)**:开启「静音运行」时创建虚拟输出 `uu-remote-sink`,把声音转到它上面采集,本机扬声器静音、控制端照常有声;
|
||||
结束后恢复原默认输出;异常退出后下次启动自动清理。有 `pactl` 时用它,没有时(默认的 Ubuntu 桌面)用 PipeWire 自带的 `pw-cli`/`wpctl`。
|
||||
✅ Ubuntu 24.04(PipeWire 1.0.5)上用官方 Mac 控制端验收:开关、断开恢复、多工具连接,以及强杀后的恢复。
|
||||
Virtual null sink while muted (pactl or PipeWire-native). Accepted on Ubuntu 24.04 with the official Mac viewer.
|
||||
- **虚拟桌面改进**:默认使用 linger 用户总线,并导入 `DISPLAY`/`XAUTHORITY`,D-Bus 激活的程序(如 GNOME Terminal)也会出现在虚拟屏幕上;
|
||||
独立的 X 认证 cookie、`Type=notify` 就绪通知、退出清理;Xvfb 被强杀后残留的显示器文件会自动清理。
|
||||
✅ 虚拟机里验证了无人登录开机上线、GNOME Terminal 出现在 `:99`、键鼠;残留清理在实机验证。
|
||||
⚠️ 无人登录场景下尚未用官方控制端完成验收(清单见 `contrib/virtual-desktop/ACCEPTANCE.md`)。
|
||||
- **Arch Linux / Omarchy**:README 增加 pacman 依赖和 Omarchy(Hyprland)说明。✅ 虚拟机里 Hyprland 截屏、uinput 键鼠、剪贴板通过。
|
||||
⚠️ 尚未在真实 Omarchy 上用官方控制端验收;相对鼠标位移的精确像素没有验证。
|
||||
- **KMS 截屏(实验原型,默认不编译)**:`--features kms` + `UU_DESKTOP_BACKEND=kms`,需要 `cap_sys_admin`。
|
||||
❌ 虚拟机里登录界面截图全黑,视觉验收未通过;**不支持**锁屏/登录界面,发布包里也不包含这个功能。
|
||||
|
||||
### 修复 / Fixed
|
||||
- 虚拟声卡恢复时,如果原默认声卡已经不存在(比如 USB 声卡被拔掉),不再留下虚拟输出作为默认。
|
||||
- 「静音运行」相关的日志会区分虚拟输出和真实声卡的静音状态。
|
||||
|
||||
### 未改变 / Unchanged
|
||||
- 无头模式下打开远程桌面仍会显示「正在重新连接」:没找到官方的「无桌面」错误码,不挪用其他错误码。
|
||||
|
||||
### 下载 / Download
|
||||
`uu-remote-linux-v0.1.7-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
|
||||
|
||||
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||
@@ -0,0 +1,20 @@
|
||||
修复大分辨率 CPU 编码时画面周期性变灰,以及非 NVIDIA 机器上每次建流都白跑一次 NVENC。/ Fixes grey keyframes on large CPU-encoded desktops and needless NVENC attempts on non-NVIDIA hosts.
|
||||
|
||||
### 修复 / Fixed
|
||||
- **大分辨率 CPU 编码画面周期性变灰**(#8,感谢 @liangjiahui01 的详细分析):libx264 的 VBV 缓冲原来最多 500 kbit,
|
||||
3296×2036 这类大屏的关键帧放不下,x264 只能输出近乎纯灰的关键帧再逐步补清晰。现在 CPU 编码的缓冲改为 1 秒的码率
|
||||
(500k~8000k),NVENC 和 VAAPI 不变。用 3296×2036 静态画面、高清 2M 档复现验证:原来每个关键帧都变灰,修复后不再出现。
|
||||
x264 VBV buffer is now one second of bitrate; NVENC/VAAPI unchanged.
|
||||
- **非 NVIDIA 机器每次建流都先试一次 NVENC**(#10,感谢 @hiclaw):`ffmpeg -encoders` 在没有 N 卡时也会列出 `h264_nvenc`,
|
||||
所以以前 AMD/Intel 机器上每次连接或切换画质都会先启动一个注定失败的 NVENC 编码。现在未设置 `UU_VIDEO_ENCODER` 时,
|
||||
只有检测到 NVIDIA 驱动才尝试 NVENC,否则直接用 CPU 编码;新增 `UU_VIDEO_ENCODER=nvenc` 可以强制使用。
|
||||
NVENC is only tried when an NVIDIA driver is present; `UU_VIDEO_ENCODER=nvenc` forces it.
|
||||
|
||||
### 文档 / Docs
|
||||
- README 的 Wayland 限制里写明:Hyprland 原生后端通过 shm screencopy 截屏,帧率受合成器限制(2560×1600 实测约 16~22 帧),
|
||||
控制端的帧率选项不生效(#9,后续会尝试 dmabuf 零拷贝)。
|
||||
|
||||
### 下载 / Download
|
||||
`uu-remote-linux-v0.1.8-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
|
||||
|
||||
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||
@@ -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.**
|
||||
+3
-2
@@ -81,9 +81,9 @@ pub async fn stream_audio(pc: Arc<RTCPeerConnection>, track: Arc<TrackLocalStati
|
||||
if closed(&pc) { return; }
|
||||
tokio::time::sleep(Duration::from_millis(100)).await;
|
||||
}
|
||||
let source = std::env::var("UU_AUDIO_SOURCE").unwrap_or_else(|_| "@DEFAULT_MONITOR@".into());
|
||||
let mut failures = 0u32;
|
||||
while !closed(&pc) {
|
||||
let source = crate::virtual_audio::source();
|
||||
let (tx, mut rx) = tokio::sync::mpsc::channel::<bytes::Bytes>(25);
|
||||
let (stopper, job) = match start(&source, tx) {
|
||||
Ok(started) => started,
|
||||
@@ -101,7 +101,7 @@ pub async fn stream_audio(pc: Arc<RTCPeerConnection>, track: Arc<TrackLocalStati
|
||||
}
|
||||
packets += 1;
|
||||
},
|
||||
_ = health.tick() => if closed(&pc) { break },
|
||||
_ = health.tick() => if closed(&pc) || crate::virtual_audio::source() != source { break },
|
||||
}
|
||||
}
|
||||
// Dropping the receiver and the stopper ends the encoder at its next packet.
|
||||
@@ -109,6 +109,7 @@ pub async fn stream_audio(pc: Arc<RTCPeerConnection>, track: Arc<TrackLocalStati
|
||||
drop(stopper);
|
||||
let result = job.await.unwrap_or_else(|e| Err(e.to_string()));
|
||||
if closed(&pc) { break; }
|
||||
if crate::virtual_audio::source() != source { failures=0; continue; }
|
||||
log::warn!("Desktop audio stopped after {packets} packets: {:?}", result.err());
|
||||
failures = if packets > 250 { 0 } else { failures + 1 };
|
||||
if failures >= 5 { log::warn!("Desktop audio disabled for this connection"); return; }
|
||||
|
||||
+13
-3
@@ -46,6 +46,7 @@ pub struct ScreenCapture {
|
||||
pub init_error: Option<String>,
|
||||
pub portal: Option<std::sync::Arc<crate::portal::PortalSession>>,
|
||||
pub display: String,
|
||||
kms: Option<crate::kms::Capture>,
|
||||
pub width: u32,
|
||||
pub height: u32,
|
||||
buffer: Mutex<Vec<u8>>,
|
||||
@@ -54,15 +55,21 @@ pub struct ScreenCapture {
|
||||
impl ScreenCapture {
|
||||
pub fn new() -> Result<Self, String> {
|
||||
let backend=crate::desktop_backend::detect()?;
|
||||
if backend == crate::desktop_backend::DesktopBackend::Kms {
|
||||
let kms=crate::kms::Capture::open()?;
|
||||
let (width,height)=(kms.width,kms.height);
|
||||
return Ok(Self { backend,init_error:None,portal:None,display:"kms:linear".into(),width,height,
|
||||
buffer:Mutex::new(vec![0;width as usize*height as usize*4]),kms:Some(kms) });
|
||||
}
|
||||
if backend == crate::desktop_backend::DesktopBackend::Portal {
|
||||
let session=std::sync::Arc::new(crate::portal::PortalSession::open()?);
|
||||
let (width,height)=(session.width,session.height);
|
||||
return Ok(Self { backend,init_error:None,portal:Some(session),display:"wayland:portal".into(),width,height,
|
||||
return Ok(Self { backend,init_error:None,portal:Some(session),kms:None,display:"wayland:portal".into(),width,height,
|
||||
buffer:Mutex::new(vec![0;(width as usize)*(height as usize)*4]) });
|
||||
}
|
||||
if backend == crate::desktop_backend::DesktopBackend::Wayland {
|
||||
let (width,height)=crate::wayland::init()?;
|
||||
return Ok(Self { backend, init_error:None, portal:None, display:"wayland:native".into(), width,height,
|
||||
return Ok(Self { backend, init_error:None, portal:None, kms:None, display:"wayland:native".into(), width,height,
|
||||
buffer:Mutex::new(vec![0;(width as usize)*(height as usize)*4]) });
|
||||
}
|
||||
let ret = unsafe { x11_init(std::ptr::null()) };
|
||||
@@ -90,6 +97,7 @@ impl ScreenCapture {
|
||||
backend,
|
||||
init_error:None,
|
||||
portal:None,
|
||||
kms:None,
|
||||
display,
|
||||
width: w as u32,
|
||||
height: h as u32,
|
||||
@@ -100,13 +108,14 @@ impl ScreenCapture {
|
||||
/// Metadata for non-desktop tools. Never advertise a usable fake screen.
|
||||
pub fn unavailable(error:String)->Self {
|
||||
Self { backend:crate::desktop_backend::detect().unwrap_or(crate::desktop_backend::DesktopBackend::X11),
|
||||
init_error:Some(error), portal:None, display:String::new(), width:0,height:0,buffer:Mutex::new(Vec::new()) }
|
||||
init_error:Some(error), portal:None, kms:None, display:String::new(), width:0,height:0,buffer:Mutex::new(Vec::new()) }
|
||||
}
|
||||
|
||||
/// 截取一帧 BGRA 图像
|
||||
pub fn capture_frame(&self) -> Result<Vec<u8>, String> {
|
||||
if let Some(error)=&self.init_error { return Err(error.clone()); }
|
||||
let mut buf = self.buffer.lock().unwrap();
|
||||
if let Some(kms)=&self.kms { kms.capture(&mut buf)?; return Ok(buf.clone()); }
|
||||
if let Some(session)=&self.portal { session.capture(&mut buf)?; return Ok(buf.clone()); }
|
||||
if self.backend == crate::desktop_backend::DesktopBackend::Wayland {
|
||||
crate::wayland::capture(&mut buf)?;
|
||||
@@ -132,6 +141,7 @@ impl Drop for ScreenCapture {
|
||||
|
||||
/// Keep the display visible only while a peer is streaming; never unlock it.
|
||||
pub fn keep_display_awake() -> Result<(), String> {
|
||||
if crate::desktop_backend::detect()? == crate::desktop_backend::DesktopBackend::Kms { return Ok(()); }
|
||||
if crate::desktop_backend::detect()? == crate::desktop_backend::DesktopBackend::Portal { return Ok(()); }
|
||||
if crate::desktop_backend::detect()? == crate::desktop_backend::DesktopBackend::Wayland {
|
||||
return crate::wayland::wake();
|
||||
|
||||
+1
-1
@@ -20,7 +20,7 @@ extern "C" {
|
||||
|
||||
pub fn start(display: String) -> ClipboardBridge {
|
||||
if display == "wayland:native" { return start_wayland(); }
|
||||
if display == "wayland:portal" {
|
||||
if display == "wayland:portal" || display.starts_with("kms:") {
|
||||
let (incoming,_)=watch::channel(None);let (_,outgoing)=watch::channel(None);
|
||||
let (sender,ready)=tokio::sync::oneshot::channel();let _=sender.send(false);
|
||||
return ClipboardBridge {incoming,outgoing,ready};
|
||||
|
||||
+21
-16
@@ -4,9 +4,13 @@ pub fn run() -> Result<(),String> {
|
||||
if std::fs::read_to_string("/proc/sys/kernel/hostname").unwrap_or_default().trim()!="uu-hyprland-test" {
|
||||
return Err("Clipboard acceptance is restricted to the isolated VM".into());
|
||||
}
|
||||
let session=Arc::new(crate::portal::PortalSession::open()?);
|
||||
let bridge=crate::clipboard::start_portal(session.clone());
|
||||
if !bridge.ready.blocking_recv().unwrap_or(false) { return Err("Portal clipboard permission not granted".into()); }
|
||||
let screen=crate::capture::ScreenCapture::new()?;
|
||||
let input=Arc::new(crate::input::InputInjector::new());
|
||||
input.bind_portal(screen.portal.clone());
|
||||
let pointer=input.start_pointer_worker();
|
||||
let bridge=if let Some(session)=screen.portal.clone() { crate::clipboard::start_portal(session) }
|
||||
else { crate::clipboard::start(screen.display.clone()) };
|
||||
if !bridge.ready.blocking_recv().unwrap_or(false) { return Err("Desktop clipboard permission not granted".into()); }
|
||||
let mut child=Command::new("python3").arg("/home/uuremote/clipboard-acceptance.py")
|
||||
.env("GDK_BACKEND","wayland").env("GSK_RENDERER","cairo")
|
||||
.stdin(Stdio::piped()).stdout(Stdio::piped()).stderr(Stdio::null()).spawn().map_err(|_|"Start GTK clipboard fixture")?;
|
||||
@@ -29,13 +33,14 @@ pub fn run() -> Result<(),String> {
|
||||
bridge.incoming.send_replace(Some(text.clone()));
|
||||
let deadline=Instant::now()+Duration::from_secs(6);let mut matched=false;
|
||||
while Instant::now()<deadline { if check("read",&text)? {matched=true;break;} std::thread::sleep(Duration::from_millis(100)); }
|
||||
if !matched { return Err(format!("Portal clipboard incoming {name} mismatch")); }
|
||||
if !matched { return Err(format!("Desktop clipboard incoming {name} mismatch")); }
|
||||
std::thread::sleep(Duration::from_millis(300));
|
||||
if outgoing.has_changed().unwrap_or(false) { return Err("Portal clipboard echoed its own selection".into()); }
|
||||
println!("Portal clipboard incoming {name}: passed; no echo");
|
||||
if outgoing.has_changed().unwrap_or(false) { return Err("Desktop clipboard echoed its own selection".into()); }
|
||||
println!("Desktop clipboard incoming {name}: passed; no echo");
|
||||
}
|
||||
session.move_pointer(f64::from(session.width)/2.0,f64::from(session.height)/2.0,false)?;
|
||||
session.button(1,true)?; session.button(1,false)?;
|
||||
input.mouse_absolute_normalized(0.5,0.5,(screen.width,screen.height));
|
||||
std::thread::sleep(Duration::from_millis(100));
|
||||
input.mouse_button(1,true); input.mouse_button(1,false);
|
||||
std::thread::sleep(Duration::from_millis(300));
|
||||
let external="UU outgoing 中文🚀\nsecond line";
|
||||
if !check("set",external)? { return Err("GTK clipboard ownership failed".into()); }
|
||||
@@ -44,25 +49,25 @@ pub fn run() -> Result<(),String> {
|
||||
if outgoing.has_changed().unwrap_or(false) && outgoing.borrow_and_update().as_deref()==Some(external) {matched=true;break;}
|
||||
std::thread::sleep(Duration::from_millis(100));
|
||||
}
|
||||
if !matched { return Err("Portal clipboard outgoing Unicode mismatch".into()); }
|
||||
println!("Portal clipboard outgoing unicode: passed");
|
||||
if !matched { return Err("Desktop clipboard outgoing Unicode mismatch".into()); }
|
||||
println!("Desktop clipboard outgoing unicode: passed");
|
||||
bridge.incoming.send_replace(Some("x".repeat(crate::clipboard::MAX_TEXT+1)));
|
||||
std::thread::sleep(Duration::from_millis(400));
|
||||
if !check("read",external)? { return Err("Oversized text altered native clipboard".into()); }
|
||||
println!("Portal clipboard incoming oversized rejection: passed");
|
||||
println!("Desktop clipboard incoming oversized rejection: passed");
|
||||
if !check("set",&"y".repeat(crate::clipboard::MAX_TEXT+1))? { return Err("GTK oversized fixture setup failed".into()); }
|
||||
std::thread::sleep(Duration::from_secs(3));
|
||||
if outgoing.has_changed().unwrap_or(false) { return Err("Portal clipboard published oversized native text".into()); }
|
||||
println!("Portal clipboard outgoing oversized rejection: passed");
|
||||
if outgoing.has_changed().unwrap_or(false) { return Err("Desktop clipboard published oversized native text".into()); }
|
||||
println!("Desktop clipboard outgoing oversized rejection: passed");
|
||||
if !check("set","")? { return Err("GTK empty fixture setup failed".into()); }
|
||||
let deadline=Instant::now()+Duration::from_secs(6);let mut matched=false;
|
||||
while Instant::now()<deadline {
|
||||
if outgoing.has_changed().unwrap_or(false) && outgoing.borrow_and_update().as_deref()==Some("") {matched=true;break;}
|
||||
std::thread::sleep(Duration::from_millis(100));
|
||||
}
|
||||
if !matched { return Err("Portal clipboard outgoing empty text mismatch".into()); }
|
||||
println!("Portal clipboard outgoing empty: passed");
|
||||
if !matched { return Err("Desktop clipboard outgoing empty text mismatch".into()); }
|
||||
println!("Desktop clipboard outgoing empty: passed");
|
||||
drop(writer); Ok(())
|
||||
})();
|
||||
let _=child.kill();let _=child.wait();result
|
||||
let _=child.kill();let _=child.wait();pointer.abort();input.release_held();result
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
//! Damage-driven X11 capture for the in-process encoder: XDamage reports when the root window changed,
|
||||
//! XShm copies it out as 32-bit BGRX. Pure Rust over x11rb; only the SysV shared-memory calls use libc.
|
||||
use std::os::unix::io::AsRawFd;
|
||||
use std::time::{Duration, Instant};
|
||||
use x11rb::connection::{Connection, RequestConnection};
|
||||
use x11rb::protocol::damage::{self, ConnectionExt as _, ReportLevel};
|
||||
use x11rb::protocol::shm::{self, ConnectionExt as _};
|
||||
use x11rb::protocol::xproto::Window;
|
||||
use x11rb::rust_connection::RustConnection;
|
||||
use x11rb::NONE;
|
||||
|
||||
/// X11 ZPixmap image format.
|
||||
const Z_PIXMAP: u8 = 2;
|
||||
|
||||
pub struct DamageCapture {
|
||||
conn: RustConnection,
|
||||
root: Window,
|
||||
damage: damage::Damage,
|
||||
damage_event: u8,
|
||||
width: u16,
|
||||
height: u16,
|
||||
seg: shm::Seg,
|
||||
shm_ptr: *mut u8,
|
||||
len: usize,
|
||||
}
|
||||
|
||||
fn text(error: impl std::fmt::Display) -> String { error.to_string() }
|
||||
|
||||
impl DamageCapture {
|
||||
/// Connects to `display` and sets up XDamage and XShm. Errors name the missing extension or failed step.
|
||||
pub fn open(display: &str) -> Result<Self, String> {
|
||||
let (conn, screen_num) = RustConnection::connect(Some(display)).map_err(text)?;
|
||||
let damage_info = conn.extension_information(damage::X11_EXTENSION_NAME).map_err(text)?
|
||||
.ok_or("XDamage extension is missing")?;
|
||||
if conn.extension_information(shm::X11_EXTENSION_NAME).map_err(text)?.is_none() {
|
||||
return Err("XShm extension is missing".into());
|
||||
}
|
||||
conn.damage_query_version(1, 1).map_err(text)?.reply().map_err(text)?;
|
||||
|
||||
let setup = conn.setup();
|
||||
let screen = setup.roots.get(screen_num).ok_or("X screen not found")?;
|
||||
let (root, width, height, depth) = (screen.root, screen.width_in_pixels, screen.height_in_pixels, screen.root_depth);
|
||||
// The frame layout assumes packed 32-bit pixels; anything else would shear the picture.
|
||||
let format = setup.pixmap_formats.iter().find(|f| f.depth == depth).ok_or("no pixmap format for the root depth")?;
|
||||
if format.bits_per_pixel != 32 { return Err(format!("root depth {depth} is not 32 bits per pixel")); }
|
||||
let len = width as usize * height as usize * 4;
|
||||
|
||||
// NonEmpty: one event each time the root goes from clean to damaged; re-armed in `wait`.
|
||||
let damage = conn.generate_id().map_err(text)?;
|
||||
conn.damage_create(damage, root, ReportLevel::NON_EMPTY).map_err(text)?.check().map_err(text)?;
|
||||
|
||||
// Nothing below can fail without also releasing the local mapping, so every error path is covered here.
|
||||
let seg = conn.generate_id().map_err(text)?;
|
||||
let shmid = unsafe { libc::shmget(libc::IPC_PRIVATE, len, libc::IPC_CREAT | 0o600) };
|
||||
if shmid < 0 { return Err("shmget failed".into()); }
|
||||
let shm_ptr = unsafe { libc::shmat(shmid, std::ptr::null(), 0) } as *mut u8;
|
||||
if shm_ptr as isize == -1 {
|
||||
unsafe { libc::shmctl(shmid, libc::IPC_RMID, std::ptr::null_mut()) };
|
||||
return Err("shmat failed".into());
|
||||
}
|
||||
let attach = conn.shm_attach(seg, shmid as u32, false).map_err(text).and_then(|c| c.check().map_err(text));
|
||||
// Marked for removal either way; the kernel frees it after the last detach.
|
||||
unsafe { libc::shmctl(shmid, libc::IPC_RMID, std::ptr::null_mut()) };
|
||||
if let Err(error) = attach {
|
||||
unsafe { libc::shmdt(shm_ptr as *const libc::c_void) };
|
||||
return Err(error);
|
||||
}
|
||||
Ok(Self { conn, root, damage, damage_event: damage_info.first_event, width, height, seg, shm_ptr, len })
|
||||
}
|
||||
|
||||
pub fn size(&self) -> (u32, u32) { (self.width as u32, self.height as u32) }
|
||||
|
||||
/// Waits up to `timeout` for the screen to change. Returns `true` on change, `false` on timeout.
|
||||
pub fn wait(&mut self, timeout: Duration) -> Result<bool, String> {
|
||||
let deadline = Instant::now() + timeout;
|
||||
loop {
|
||||
let mut changed = false;
|
||||
while let Some(event) = self.conn.poll_for_raw_event().map_err(text)? {
|
||||
// XDamageNotify is the extension's first event; the wire code keeps the high bit for synthetic events.
|
||||
if event[0] & 0x7f == self.damage_event { changed = true; }
|
||||
}
|
||||
if changed {
|
||||
self.conn.damage_subtract(self.damage, NONE, NONE).map_err(text)?;
|
||||
self.conn.flush().map_err(text)?;
|
||||
return Ok(true);
|
||||
}
|
||||
let remaining = deadline.saturating_duration_since(Instant::now());
|
||||
if remaining.is_zero() { return Ok(false); }
|
||||
let mut pfd = libc::pollfd { fd: self.conn.stream().as_raw_fd(), events: libc::POLLIN, revents: 0 };
|
||||
match unsafe { libc::poll(&mut pfd, 1, remaining.as_millis().max(1) as i32) } {
|
||||
ready if ready < 0 => return Err("poll on the X connection failed".into()),
|
||||
// Readable but not yet a whole event: poll_for_raw_event would return None and poll would
|
||||
// fire again at once, spinning a core. Block for the event instead.
|
||||
ready if ready > 0 => {
|
||||
let event = self.conn.wait_for_raw_event().map_err(text)?;
|
||||
if event[0] & 0x7f == self.damage_event { changed = true; }
|
||||
if changed {
|
||||
self.conn.damage_subtract(self.damage, NONE, NONE).map_err(text)?;
|
||||
self.conn.flush().map_err(text)?;
|
||||
return Ok(true);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Copies the root into the shared segment and returns it as rows of `width * 4` bytes.
|
||||
/// The slice is valid until the next call.
|
||||
pub fn capture(&mut self) -> Result<&[u8], String> {
|
||||
self.conn.shm_get_image(self.root, 0, 0, self.width, self.height, !0, Z_PIXMAP, self.seg, 0)
|
||||
.map_err(text)?.reply().map_err(text)?;
|
||||
Ok(unsafe { std::slice::from_raw_parts(self.shm_ptr, self.len) })
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for DamageCapture {
|
||||
fn drop(&mut self) {
|
||||
let _ = self.conn.damage_destroy(self.damage);
|
||||
let _ = self.conn.shm_detach(self.seg);
|
||||
let _ = self.conn.flush();
|
||||
unsafe { libc::shmdt(self.shm_ptr as *const libc::c_void) };
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,10 @@
|
||||
//! Select the host desktop, not the presence of an XWayland DISPLAY socket.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum DesktopBackend { X11, Wayland, Portal }
|
||||
pub enum DesktopBackend { X11, Wayland, Portal, Kms }
|
||||
|
||||
/// `UU_DESKTOP_BACKEND=none`: headless host — no display is opened; terminal, file transfer and
|
||||
/// port mapping stay available (servers without a monitor or logged-in session).
|
||||
pub fn headless() -> bool { std::env::var("UU_DESKTOP_BACKEND").is_ok_and(|v| v == "none") }
|
||||
|
||||
pub fn detect() -> Result<DesktopBackend, String> {
|
||||
let backend=select(std::env::var("UU_DESKTOP_BACKEND").ok().as_deref(),
|
||||
@@ -15,13 +19,15 @@ fn select(explicit: Option<&str>, session: Option<&str>, socket: Option<&str>) -
|
||||
match explicit {
|
||||
Some("x11") => Ok(DesktopBackend::X11),
|
||||
Some("wayland") => Ok(DesktopBackend::Wayland),
|
||||
Some("kms") => Ok(DesktopBackend::Kms),
|
||||
Some("portal") => Ok(DesktopBackend::Portal),
|
||||
Some("none") => Ok(DesktopBackend::X11), // never opened, see headless()
|
||||
None | Some("auto") => {
|
||||
if session == Some("wayland") || socket.is_some_and(|s| !s.is_empty()) {
|
||||
Ok(DesktopBackend::Wayland)
|
||||
} else { Ok(DesktopBackend::X11) }
|
||||
},
|
||||
Some(_) => Err("UU_DESKTOP_BACKEND must be auto, x11, wayland or portal".into()),
|
||||
Some(_) => Err("UU_DESKTOP_BACKEND must be auto, x11, wayland, portal, kms or none".into()),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,6 +40,7 @@ mod tests {
|
||||
assert_eq!(select(None,None,Some("wayland-1")),Ok(DesktopBackend::Wayland));
|
||||
assert_eq!(select(None,Some("x11"),None),Ok(DesktopBackend::X11));
|
||||
assert_eq!(select(Some("x11"),Some("wayland"),Some("wayland-0")),Ok(DesktopBackend::X11));
|
||||
assert_eq!(select(Some("kms"),Some("wayland"),Some("wayland-0")),Ok(DesktopBackend::Kms));
|
||||
assert!(select(Some("typo"),None,None).is_err());
|
||||
}
|
||||
}
|
||||
+165
-25
@@ -12,6 +12,8 @@ use ff::util::picture;
|
||||
use ff::{codec, encoder, frame, software::scaling, Dictionary, Packet, Rational};
|
||||
use tokio::sync::mpsc;
|
||||
|
||||
use crate::damage_capture::DamageCapture;
|
||||
|
||||
pub struct Config {
|
||||
pub display: String,
|
||||
pub input: (u32, u32),
|
||||
@@ -22,7 +24,9 @@ pub struct Config {
|
||||
pub target_bps: i64,
|
||||
pub max_bps: i64,
|
||||
pub buffer_bits: i64,
|
||||
pub gpu: bool,
|
||||
pub encoder: crate::video::Encoder,
|
||||
/// Capture on XDamage changes (XShm) instead of x11grab; `UU_CAPTURE=grab` turns it off.
|
||||
pub damage: bool,
|
||||
}
|
||||
|
||||
/// `UU_VIDEO_PIPELINE=process` keeps the original ffmpeg child-process path.
|
||||
@@ -30,6 +34,11 @@ pub fn disabled() -> bool {
|
||||
std::env::var("UU_VIDEO_PIPELINE").is_ok_and(|v| v == "process")
|
||||
}
|
||||
|
||||
/// Damage-driven capture is on unless `UU_CAPTURE=grab` asks for x11grab.
|
||||
pub fn damage_enabled() -> bool {
|
||||
std::env::var("UU_CAPTURE").map_or(true, |v| v != "grab")
|
||||
}
|
||||
|
||||
/// In-process capture is used unless the process path is requested, or a bundled FFmpeg prefix
|
||||
/// (`UU_FFMPEG_ROOT` / config `ffmpeg_root`) is set: that build was chosen on purpose, so keep using it.
|
||||
pub fn selected() -> bool {
|
||||
@@ -62,34 +71,19 @@ fn drain(encoder: &mut encoder::Video, packet: &mut Packet, units: &mpsc::Sender
|
||||
}
|
||||
}
|
||||
|
||||
/// Runs until `stop` is set or the receiver is dropped. A set `idr` flag forces the next frame to be an IDR.
|
||||
pub fn run(config: Config, units: mpsc::Sender<Vec<u8>>, stop: Arc<AtomicBool>, idr: Arc<AtomicBool>) -> Result<(), String> {
|
||||
ensure_init()?;
|
||||
let name = CString::new("x11grab").map_err(|e| e.to_string())?;
|
||||
let raw = unsafe { ff::sys::av_find_input_format(name.as_ptr()) };
|
||||
if raw.is_null() { return Err("x11grab input is not available in this FFmpeg build".into()); }
|
||||
let input_format = Format::Input(unsafe { ff::format::format::Input::wrap(raw as *mut _) });
|
||||
let mut capture_options = Dictionary::new();
|
||||
capture_options.set("framerate", &config.fps.to_string());
|
||||
capture_options.set("video_size", &format!("{}x{}", config.input.0, config.input.1));
|
||||
// The viewer draws its own pointer; do not bake a second one into the video.
|
||||
capture_options.set("draw_mouse", "0");
|
||||
let mut input = ff::format::open_with(&config.display, &input_format, capture_options).map_err(text)?.input();
|
||||
|
||||
let stream = input.streams().best(ff::media::Type::Video).ok_or("X11 capture has no video stream")?;
|
||||
let index = stream.index();
|
||||
let mut decoder = codec::context::Context::from_parameters(stream.parameters()).map_err(text)?.decoder().video().map_err(text)?;
|
||||
|
||||
/// Opens the H.264 encoder for `config`. Returns the encoder, its input pixel format and output size.
|
||||
fn open_encoder(config: &Config) -> Result<(encoder::Video, Pixel, u32, u32), String> {
|
||||
let (width, height) = config.output.unwrap_or(config.input);
|
||||
let codec = encoder::find_by_name(if config.gpu { "h264_nvenc" } else { "libx264" })
|
||||
let codec = encoder::find_by_name(config.encoder.name())
|
||||
.ok_or("H.264 encoder is not available in this FFmpeg build")?;
|
||||
// Allocate for this codec so FFmpeg fills its defaults; libx264 rejects a context allocated without one.
|
||||
let mut context = unsafe { codec::context::Context::wrap(ff::sys::avcodec_alloc_context3(codec.as_ptr()), None) }.encoder().video().map_err(text)?;
|
||||
context.set_width(width);
|
||||
context.set_height(height);
|
||||
// NVENC takes packed BGR0 directly, so the GPU path skips the CPU colour conversion to YUV420P.
|
||||
let pixel = if config.gpu { Pixel::BGRZ } else { Pixel::YUV420P };
|
||||
context.set_format(pixel);
|
||||
let pixel = match config.encoder { crate::video::Encoder::Nvenc => Pixel::BGRZ, crate::video::Encoder::Vaapi => Pixel::NV12, _ => Pixel::YUV420P };
|
||||
context.set_format(if config.encoder == crate::video::Encoder::Vaapi { Pixel::VAAPI } else { pixel });
|
||||
if config.encoder == crate::video::Encoder::Vaapi { attach_vaapi(&mut context, width, height)?; }
|
||||
context.set_time_base(Rational::new(1, config.fps as i32));
|
||||
context.set_frame_rate(Some(Rational::new(config.fps as i32, 1)));
|
||||
context.set_gop(config.gop);
|
||||
@@ -105,7 +99,10 @@ pub fn run(config: Config, units: mpsc::Sender<Vec<u8>>, stop: Arc<AtomicBool>,
|
||||
let cq = config.cq.to_string();
|
||||
let gop = config.gop.to_string();
|
||||
let mut options = Dictionary::new();
|
||||
if config.gpu {
|
||||
if config.encoder == crate::video::Encoder::Vaapi {
|
||||
for (key,value) in [("profile", "constrained_baseline"), ("rc_mode", "VBR"), ("async_depth", "1"), ("aud", "1")] { options.set(key,value); }
|
||||
options.set("qmin", &cq);
|
||||
} else if config.encoder == crate::video::Encoder::Nvenc {
|
||||
for (key, value) in [("preset", "p4"), ("tune", "ull"), ("profile", "baseline"), ("rc", "vbr"),
|
||||
("rc-lookahead", "0"), ("zerolatency", "1"), ("delay", "0"), ("aud", "1"), ("forced-idr", "1")] {
|
||||
options.set(key, value);
|
||||
@@ -119,7 +116,74 @@ pub fn run(config: Config, units: mpsc::Sender<Vec<u8>>, stop: Arc<AtomicBool>,
|
||||
options.set("crf", &cq);
|
||||
options.set("x264-params", &x264);
|
||||
}
|
||||
let mut encoder = context.open_as_with(codec, options).map_err(text)?;
|
||||
let encoder = context.open_as_with(codec, options).map_err(text)?;
|
||||
Ok((encoder, pixel, width, height))
|
||||
}
|
||||
|
||||
// Each codec owns a reference to its hardware frame pool. Every error path releases
|
||||
// the temporary device/pool refs; AVCodecContext releases its own ref on drop.
|
||||
fn attach_vaapi(context: &mut encoder::video::Video, width: u32, height: u32) -> Result<(), String> {
|
||||
use ff::sys::*;
|
||||
let device_name = CString::new(crate::video::vaapi_device()).map_err(|_| "Invalid VAAPI device")?;
|
||||
unsafe {
|
||||
let mut device = std::ptr::null_mut();
|
||||
let result = av_hwdevice_ctx_create(&mut device, AVHWDeviceType::AV_HWDEVICE_TYPE_VAAPI, device_name.as_ptr(), std::ptr::null_mut(), 0);
|
||||
if result < 0 { return Err(format!("VAAPI device initialization failed: {}", text(ff::Error::from(result)))); }
|
||||
let mut pool = av_hwframe_ctx_alloc(device);
|
||||
av_buffer_unref(&mut device);
|
||||
if pool.is_null() { return Err("VAAPI frame pool allocation failed".into()); }
|
||||
let frames = (*pool).data as *mut AVHWFramesContext;
|
||||
(*frames).format = AVPixelFormat::AV_PIX_FMT_VAAPI;
|
||||
(*frames).sw_format = AVPixelFormat::AV_PIX_FMT_NV12;
|
||||
(*frames).width = width as i32;
|
||||
(*frames).height = height as i32;
|
||||
(*frames).initial_pool_size = 8;
|
||||
let result = av_hwframe_ctx_init(pool);
|
||||
if result < 0 { av_buffer_unref(&mut pool); return Err(format!("VAAPI frame pool initialization failed: {}", text(ff::Error::from(result)))); }
|
||||
(*context.as_mut_ptr()).hw_frames_ctx = pool;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn send_video_frame(encoder: &mut encoder::Video, frame: &frame::Video, kind: crate::video::Encoder) -> Result<(), String> {
|
||||
if kind != crate::video::Encoder::Vaapi { return encoder.send_frame(frame).map_err(text); }
|
||||
let mut uploaded = frame::Video::empty();
|
||||
unsafe {
|
||||
let result = ff::sys::av_hwframe_get_buffer((*encoder.as_ptr()).hw_frames_ctx, uploaded.as_mut_ptr(), 0);
|
||||
if result < 0 { return Err(text(ff::Error::from(result))); }
|
||||
let result = ff::sys::av_hwframe_transfer_data(uploaded.as_mut_ptr(), frame.as_ptr(), 0);
|
||||
if result < 0 { return Err(text(ff::Error::from(result))); }
|
||||
let result = ff::sys::av_frame_copy_props(uploaded.as_mut_ptr(), frame.as_ptr());
|
||||
if result < 0 { return Err(text(ff::Error::from(result))); }
|
||||
}
|
||||
encoder.send_frame(&uploaded).map_err(text)
|
||||
}
|
||||
|
||||
/// Runs until `stop` is set or the receiver is dropped. A set `idr` flag forces the next frame to be an IDR.
|
||||
pub fn run(config: Config, units: mpsc::Sender<Vec<u8>>, stop: Arc<AtomicBool>, idr: Arc<AtomicBool>) -> Result<(), String> {
|
||||
ensure_init()?;
|
||||
if config.damage {
|
||||
match DamageCapture::open(&config.display) {
|
||||
Ok(capture) => return run_damage(config, capture, units, stop, idr),
|
||||
Err(error) => log::warn!("Damage-driven capture unavailable, using x11grab: {error}"),
|
||||
}
|
||||
}
|
||||
let name = CString::new("x11grab").map_err(|e| e.to_string())?;
|
||||
let raw = unsafe { ff::sys::av_find_input_format(name.as_ptr()) };
|
||||
if raw.is_null() { return Err("x11grab input is not available in this FFmpeg build".into()); }
|
||||
let input_format = Format::Input(unsafe { ff::format::format::Input::wrap(raw as *mut _) });
|
||||
let mut capture_options = Dictionary::new();
|
||||
capture_options.set("framerate", &config.fps.to_string());
|
||||
capture_options.set("video_size", &format!("{}x{}", config.input.0, config.input.1));
|
||||
// The viewer draws its own pointer; do not bake a second one into the video.
|
||||
capture_options.set("draw_mouse", "0");
|
||||
let mut input = ff::format::open_with(&config.display, &input_format, capture_options).map_err(text)?.input();
|
||||
|
||||
let stream = input.streams().best(ff::media::Type::Video).ok_or("X11 capture has no video stream")?;
|
||||
let index = stream.index();
|
||||
let mut decoder = codec::context::Context::from_parameters(stream.parameters()).map_err(text)?.decoder().video().map_err(text)?;
|
||||
|
||||
let (mut encoder, pixel, width, height) = open_encoder(&config)?;
|
||||
|
||||
let mut scaler: Option<(scaling::Context, (Pixel, u32, u32))> = None;
|
||||
let mut decoded = frame::Video::empty();
|
||||
@@ -163,12 +227,88 @@ pub fn run(config: Config, units: mpsc::Sender<Vec<u8>>, stop: Arc<AtomicBool>,
|
||||
|
||||
frame.set_pts(Some(slot));
|
||||
frame.set_kind(if idr.swap(false, Ordering::AcqRel) { picture::Type::I } else { picture::Type::None });
|
||||
encoder.send_frame(frame).map_err(text)?;
|
||||
send_video_frame(&mut encoder, frame, config.encoder)?;
|
||||
if !drain(&mut encoder, &mut packet, &units)? { return Ok(()); }
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Damage-driven loop. Damage only marks the picture dirty; a dirty picture is captured and encoded on
|
||||
/// the next fixed frame tick, so the stream keeps a steady cadence while the screen moves. The viewer's
|
||||
/// jitter buffer grows with irregular arrivals, and irregular sends were what raised its latency.
|
||||
/// An idle screen sends a keepalive about once a second so the viewer still sees traffic.
|
||||
fn run_damage(config: Config, mut capture: DamageCapture, units: mpsc::Sender<Vec<u8>>, stop: Arc<AtomicBool>, idr: Arc<AtomicBool>) -> Result<(), String> {
|
||||
let (mut encoder, pixel, width, height) = open_encoder(&config)?;
|
||||
let (cap_width, cap_height) = capture.size();
|
||||
let mut captured = frame::Video::new(Pixel::BGRZ, cap_width, cap_height);
|
||||
let mut converted = frame::Video::new(pixel, width, height);
|
||||
let mut scaler = None;
|
||||
let mut packet = Packet::empty();
|
||||
let started = Instant::now();
|
||||
let frame_interval = Duration::from_nanos(1_000_000_000 / u64::from(config.fps));
|
||||
// Keepalive gaps are random between 15 and 25 fps: a steady 1 fps reads as a stall to the viewer.
|
||||
let mut seed = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map_or(1, |d| d.subsec_nanos()) | 1;
|
||||
let mut keepalive = random_keepalive(&mut seed);
|
||||
let mut dirty = true;
|
||||
let mut next_tick = Instant::now();
|
||||
let mut last_send = Instant::now();
|
||||
let mut last_slot: i64 = -1;
|
||||
loop {
|
||||
if stop.load(Ordering::Relaxed) { return Ok(()); }
|
||||
let deadline = if dirty { next_tick } else { last_send + keepalive };
|
||||
let wait = deadline.saturating_duration_since(Instant::now());
|
||||
// A change marks the picture dirty and falls through, so a client that damages the root constantly
|
||||
// cannot keep an overdue tick waiting.
|
||||
if capture.wait(wait.max(Duration::from_millis(1)))? { dirty = true; }
|
||||
let now = Instant::now();
|
||||
if dirty && now < next_tick { continue; }
|
||||
if !dirty && now < last_send + keepalive { continue; }
|
||||
|
||||
let source = capture.capture()?;
|
||||
let bytes_per_line = cap_width as usize * 4;
|
||||
let stride = captured.stride(0);
|
||||
let row = bytes_per_line.min(stride);
|
||||
for (y, src) in source.chunks_exact(bytes_per_line).enumerate() {
|
||||
captured.data_mut(0)[y * stride..y * stride + row].copy_from_slice(&src[..row]);
|
||||
}
|
||||
|
||||
let slot = ((started.elapsed().as_micros() * u128::from(config.fps) + 500_000) / 1_000_000) as i64;
|
||||
let slot = slot.max(last_slot + 1);
|
||||
last_slot = slot;
|
||||
|
||||
let frame = if pixel == Pixel::BGRZ && width == cap_width && height == cap_height {
|
||||
&mut captured
|
||||
} else {
|
||||
if scaler.is_none() {
|
||||
scaler = Some(scaling::Context::get(Pixel::BGRZ, cap_width, cap_height, pixel, width, height, scaling::Flags::BILINEAR).map_err(text)?);
|
||||
}
|
||||
if let Some(context) = scaler.as_mut() { context.run(&captured, &mut converted).map_err(text)?; }
|
||||
&mut converted
|
||||
};
|
||||
frame.set_pts(Some(slot));
|
||||
frame.set_kind(if idr.swap(false, Ordering::AcqRel) { picture::Type::I } else { picture::Type::None });
|
||||
send_video_frame(&mut encoder, frame, config.encoder)?;
|
||||
if !drain(&mut encoder, &mut packet, &units)? { return Ok(()); }
|
||||
let now = Instant::now();
|
||||
if dirty {
|
||||
// Keep the tick phase; after a long idle, restart the phase from now instead of bursting to catch up.
|
||||
let next = next_tick + frame_interval;
|
||||
next_tick = if next > now { next } else { now + frame_interval };
|
||||
dirty = false;
|
||||
}
|
||||
last_send = now;
|
||||
keepalive = random_keepalive(&mut seed);
|
||||
}
|
||||
}
|
||||
|
||||
/// Picks the next keepalive gap in 40–67 ms (15–25 fps) with a small xorshift generator.
|
||||
fn random_keepalive(seed: &mut u32) -> Duration {
|
||||
*seed ^= *seed << 13;
|
||||
*seed ^= *seed >> 17;
|
||||
*seed ^= *seed << 5;
|
||||
Duration::from_micros(40_000 + u64::from(*seed % 26_700))
|
||||
}
|
||||
|
||||
/// Encodes a PulseAudio/PipeWire source to Opus in this process. Each Opus packet is sent as is.
|
||||
pub fn run_audio(source: &str, units: mpsc::Sender<bytes::Bytes>, stop: Arc<AtomicBool>) -> Result<(), String> {
|
||||
ensure_init()?;
|
||||
|
||||
+252
-5
@@ -8,7 +8,11 @@ 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"))]
|
||||
unsafe fn x11_terminal_shortcut_state() -> libc::c_int { 0 }
|
||||
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
unsafe fn x11_mouse_move(_: libc::c_int, _: libc::c_int) -> libc::c_int { 0 }
|
||||
@@ -23,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)>,
|
||||
@@ -33,11 +40,22 @@ struct PendingPointer {
|
||||
unsafe fn x11_pointer_state(_: *mut libc::c_int,_:*mut libc::c_int,_:*mut libc::c_uint)->libc::c_int {-1}
|
||||
#[derive(Default)]
|
||||
struct HeldInput { buttons: std::collections::BTreeSet<i32>, keys: std::collections::BTreeSet<u64> }
|
||||
pub struct InputInjector { portal_failures: std::sync::atomic::AtomicUsize, portal_outcomes: [std::sync::atomic::AtomicUsize; 3], portal:std::sync::Mutex<Option<std::sync::Arc<crate::portal::PortalSession>>>, backend: crate::desktop_backend::DesktopBackend, pending: std::sync::Mutex<PendingPointer>, held: std::sync::Mutex<HeldInput> }
|
||||
/// Held modifier VKs (bit 0 Ctrl, 1 Shift, 2 Alt, 3 Win) and keys that got a synthetic Shift.
|
||||
#[derive(Default)]
|
||||
struct Shortcuts { modifiers: u8, shifted: std::collections::BTreeSet<i32> }
|
||||
|
||||
pub struct InputInjector { 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 {
|
||||
Self { portal_failures: std::sync::atomic::AtomicUsize::new(0), portal_outcomes: std::array::from_fn(|_| std::sync::atomic::AtomicUsize::new(0)), portal:std::sync::Mutex::new(None), backend:crate::desktop_backend::detect().unwrap_or(crate::desktop_backend::DesktopBackend::X11), pending: std::sync::Mutex::new(PendingPointer::default()), held:std::sync::Mutex::new(HeldInput::default()) }
|
||||
let backend = crate::desktop_backend::detect().unwrap_or(crate::desktop_backend::DesktopBackend::X11);
|
||||
let uinput = if crate::uinput::wanted(backend) {
|
||||
match crate::uinput::Devices::open() {
|
||||
Ok(devices) => { log::info!("Input backend: uinput (kernel virtual keyboard/pointer)"); Some(devices) }
|
||||
Err(e) => { log::warn!("uinput unavailable ({}); using the desktop input path. See README for the udev rule.", e.kind()); None }
|
||||
}
|
||||
} else { None };
|
||||
Self { 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>>) {
|
||||
@@ -45,10 +63,12 @@ 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);
|
||||
}
|
||||
|
||||
pub fn mouse_absolute_normalized(&self, x: f64, y: f64, size: (u32,u32)) {
|
||||
*self.screen.lock().unwrap() = size;
|
||||
if let Some(position) = crate::input_mapping::normalized_pointer(x,y,size.0,size.1) {
|
||||
let mut pending = self.pending.lock().unwrap();
|
||||
pending.absolute = Some(position);
|
||||
@@ -78,6 +98,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();
|
||||
@@ -103,7 +127,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();
|
||||
@@ -117,7 +142,48 @@ impl InputInjector {
|
||||
}
|
||||
}
|
||||
|
||||
/// Mac viewers send Cmd+V as Ctrl+V (verified: Cmd and Ctrl both arrive as VK_LCONTROL), which in
|
||||
/// a Linux terminal quotes the next key instead of pasting. In a focused terminal Ctrl+V becomes
|
||||
/// Ctrl+Shift+V. Ctrl+C is never translated: it must always interrupt, and copy is Cmd+Shift+C
|
||||
/// (= Ctrl+Shift+C). X11 only; `UU_TERMINAL_SHORTCUTS=0` disables.
|
||||
fn terminal_shift(&self, key: i32, is_press: bool) -> bool {
|
||||
let bit = match key { 0x11 | 0xa2 | 0xa3 => 1, 0x10 | 0xa0 | 0xa1 => 2, 0x12 | 0xa4 | 0xa5 => 4, 0x5b | 0x5c => 8, _ => 0 };
|
||||
let mut shortcuts = self.shortcuts.lock().unwrap();
|
||||
if bit != 0 {
|
||||
if is_press { shortcuts.modifiers |= bit } else { shortcuts.modifiers &= !bit }
|
||||
return false;
|
||||
}
|
||||
if !is_press { return shortcuts.shifted.remove(&key); }
|
||||
if key != 0x56 || shortcuts.modifiers != 1 { return false; }
|
||||
if std::env::var("UU_TERMINAL_SHORTCUTS").is_ok_and(|v| v == "0") || std::env::var_os("DISPLAY").is_none() { return false; }
|
||||
let shift = unsafe { x11_terminal_shortcut_state() } & 1 != 0;
|
||||
if shift {
|
||||
shortcuts.shifted.insert(key);
|
||||
static NOTED: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
|
||||
if NOTED.fetch_add(1, std::sync::atomic::Ordering::Relaxed) < 4 {
|
||||
log::info!("Terminal shortcut: Ctrl+V sent as Ctrl+Shift+V");
|
||||
}
|
||||
}
|
||||
shift
|
||||
}
|
||||
|
||||
pub fn windows_key_event(&self, key: i32, is_press: bool) {
|
||||
let _serial=self.serial.lock().unwrap();
|
||||
if self.terminal_shift(key, is_press) {
|
||||
const SHIFT_L: u64 = 0xffe1;
|
||||
if is_press {
|
||||
self.inject_key(SHIFT_L, true);
|
||||
self.key_symbol_event(key, true);
|
||||
} else {
|
||||
self.key_symbol_event(key, false);
|
||||
self.inject_key(SHIFT_L, false);
|
||||
}
|
||||
return;
|
||||
}
|
||||
self.key_symbol_event(key, is_press);
|
||||
}
|
||||
|
||||
fn key_symbol_event(&self, key: i32, is_press: bool) {
|
||||
if let Some(symbol) = crate::input_mapping::windows_vk_to_keysym(key) {
|
||||
let result = self.inject_key(symbol,is_press);
|
||||
static AUDIT: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
|
||||
@@ -132,11 +198,29 @@ 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();
|
||||
}
|
||||
// Per-injector budgets cover missing bindings, locked sessions, stale handles
|
||||
// and D-Bus errors. Never include keys, coordinates, text or portal tokens.
|
||||
@@ -158,25 +242,54 @@ impl InputInjector {
|
||||
}
|
||||
}
|
||||
}
|
||||
fn uinput_result(result: std::io::Result<()>) -> i32 {
|
||||
match result {
|
||||
Ok(()) => 0,
|
||||
Err(e) => {
|
||||
static FAILURES: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
|
||||
if FAILURES.fetch_add(1, std::sync::atomic::Ordering::Relaxed) < 8 { log::warn!("uinput injection failed: {}", e.kind()); }
|
||||
-1
|
||||
}
|
||||
}
|
||||
}
|
||||
fn inject_move(&self,x:i32,y:i32,relative:bool)->i32 {
|
||||
if let Some(uinput)=&self.uinput {
|
||||
if relative { return Self::uinput_result(uinput.move_relative(x,y)); }
|
||||
let (w,h)=*self.screen.lock().unwrap();
|
||||
if w > 1 && h > 1 {
|
||||
let scale = |v: i32, extent: u32| (i64::from(v) * i64::from(crate::uinput::ABS_MAX) / i64::from(extent - 1)) as i32;
|
||||
return Self::uinput_result(uinput.move_absolute(scale(x,w),scale(y,h)));
|
||||
}
|
||||
}
|
||||
if self.backend==crate::desktop_backend::DesktopBackend::Kms { return -1; }
|
||||
if let Some(portal)=self.portal.lock().unwrap().as_ref() { return self.portal_result("motion", portal.move_pointer(f64::from(x),f64::from(y),relative)); }
|
||||
if self.backend==crate::desktop_backend::DesktopBackend::Portal {return self.portal_result("motion", Err("Portal input session is not bound".into()));}
|
||||
if self.backend==crate::desktop_backend::DesktopBackend::Wayland { crate::wayland::move_pointer(x,y,relative) }
|
||||
else if relative { unsafe { x11_mouse_relative(x,y) } } else { unsafe { x11_mouse_move(x,y) } }
|
||||
}
|
||||
fn inject_button(&self,button:i32,pressed:bool)->i32 {
|
||||
if let Some(uinput)=&self.uinput {
|
||||
if let Some((v,h))=crate::uinput::wheel_step(button) { return if pressed { Self::uinput_result(uinput.wheel(v,h)) } else { 0 }; }
|
||||
if let Some(code)=crate::uinput::button_code(button) { return Self::uinput_result(uinput.button(code,pressed)); }
|
||||
}
|
||||
if self.backend==crate::desktop_backend::DesktopBackend::Kms { return -1; }
|
||||
if let Some(portal)=self.portal.lock().unwrap().as_ref() { return self.portal_result("button", portal.button(button,pressed)); }
|
||||
if self.backend==crate::desktop_backend::DesktopBackend::Portal {return self.portal_result("button", Err("Portal input session is not bound".into()));}
|
||||
if self.backend==crate::desktop_backend::DesktopBackend::Wayland { crate::wayland::button(button,pressed) }
|
||||
else { unsafe { x11_mouse_button(button,i32::from(pressed)) } }
|
||||
}
|
||||
fn inject_key(&self,symbol:u64,pressed:bool)->i32 {
|
||||
if let Some(uinput)=&self.uinput {
|
||||
if let Some(code)=crate::uinput::keysym_to_evdev(symbol) { return Self::uinput_result(uinput.key(code,pressed)); }
|
||||
}
|
||||
if self.backend==crate::desktop_backend::DesktopBackend::Kms { return -1; }
|
||||
if let Some(portal)=self.portal.lock().unwrap().as_ref() { return self.portal_result("keyboard", portal.key(symbol,pressed)); }
|
||||
if self.backend==crate::desktop_backend::DesktopBackend::Portal {return self.portal_result("keyboard", Err("Portal input session is not bound".into()));}
|
||||
if self.backend==crate::desktop_backend::DesktopBackend::Wayland { crate::wayland::key(symbol,pressed) }
|
||||
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 }) };
|
||||
}
|
||||
@@ -213,6 +326,29 @@ mod motion_tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod shortcut_tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn modifiers_are_tracked_and_non_terminal_keys_pass_through() {
|
||||
let input = InputInjector::new();
|
||||
assert!(!input.terminal_shift(0x11, true));
|
||||
assert_eq!(input.shortcuts.lock().unwrap().modifiers, 1);
|
||||
// No X display in tests: Ctrl+V is not translated.
|
||||
assert!(!input.terminal_shift(0x56, true));
|
||||
assert!(!input.terminal_shift(0x56, false));
|
||||
assert!(!input.terminal_shift(0x11, false));
|
||||
assert_eq!(input.shortcuts.lock().unwrap().modifiers, 0);
|
||||
// A translated key keeps its synthetic Shift until its own release.
|
||||
input.shortcuts.lock().unwrap().shifted.insert(0x56);
|
||||
assert!(input.terminal_shift(0x56, false));
|
||||
assert!(!input.terminal_shift(0x56, false));
|
||||
// Ctrl+C is never translated.
|
||||
input.terminal_shift(0x11, true);
|
||||
assert!(!input.terminal_shift(0x43, true));
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod scroll_tests {
|
||||
use super::*;
|
||||
@@ -227,3 +363,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"));
|
||||
}
|
||||
}
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
//! Experimental linear KMS framebuffer capture. Never selected automatically.
|
||||
#[cfg(all(feature="kms",target_os="linux"))]
|
||||
mod imp {
|
||||
use std::ffi::CString;
|
||||
use std::sync::Mutex;
|
||||
extern "C" {
|
||||
fn uu_kms_open(device:*const libc::c_char,w:*mut u32,h:*mut u32)->*mut libc::c_void;
|
||||
fn uu_kms_read(handle:*mut libc::c_void,out:*mut u8,length:usize)->i32;
|
||||
fn uu_kms_close(handle:*mut libc::c_void);
|
||||
}
|
||||
struct Handle(*mut libc::c_void);
|
||||
// The FD/CRTC handle is exclusively accessed through Capture's mutex.
|
||||
unsafe impl Send for Handle {}
|
||||
impl Drop for Handle { fn drop(&mut self) { unsafe { uu_kms_close(self.0) }; } }
|
||||
pub struct Capture { handle:Mutex<Handle>, pub width:u32, pub height:u32 }
|
||||
impl Capture {
|
||||
pub fn open()->Result<Self,String> {
|
||||
let device=CString::new(std::env::var("UU_KMS_DEVICE").unwrap_or_else(|_|"/dev/dri/card0".into())).map_err(|_|"Invalid KMS device")?;
|
||||
let (mut width,mut height)=(0,0);
|
||||
let handle=unsafe { uu_kms_open(device.as_ptr(),&mut width,&mut height) };
|
||||
if handle.is_null() { return Err("KMS: cannot open an active CRTC; check card permissions and connected output".into()); }
|
||||
let capture=Self { handle:Mutex::new(Handle(handle)), width,height };
|
||||
// Do not advertise a desktop until we have actually read the buffer.
|
||||
let mut frame=vec![0; width as usize*height as usize*4];
|
||||
capture.capture(&mut frame)?;
|
||||
if frame.chunks_exact(4).all(|p| p[..3]==[0,0,0]) {
|
||||
log::warn!("KMS initial frame is uniformly black; GPU buffer visibility is unverified (see KMS limitations)");
|
||||
}
|
||||
Ok(capture)
|
||||
}
|
||||
pub fn capture(&self,output:&mut [u8])->Result<(),String> {
|
||||
if output.len()!=self.width as usize*self.height as usize*4 { return Err("KMS: invalid frame dimensions".into()); }
|
||||
let handle=self.handle.lock().unwrap();
|
||||
match unsafe { uu_kms_read(handle.0,output.as_mut_ptr(),output.len()) } {
|
||||
0=>Ok(()), -2=>Err("KMS mode changed; restart host to refresh desktop geometry".into()),
|
||||
-5=>Err("KMS framebuffer handles unavailable; requires cap_sys_admin+ep".into()),
|
||||
code=>Err(format!("KMS capture failed ({code}); prototype needs a mappable linear XRGB/ARGB dumb buffer")),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#[cfg(all(feature="kms",target_os="linux"))]
|
||||
pub use imp::Capture;
|
||||
#[cfg(not(all(feature="kms",target_os="linux")))]
|
||||
pub struct Capture { pub width:u32, pub height:u32 }
|
||||
#[cfg(not(all(feature="kms",target_os="linux")))]
|
||||
impl Capture {
|
||||
pub fn open()->Result<Self,String> { Err("KMS requires Linux and --features kms".into()) }
|
||||
pub fn capture(&self,_:&mut [u8])->Result<(),String> { Err("KMS requires Linux and --features kms".into()) }
|
||||
}
|
||||
+71
-3
@@ -12,8 +12,10 @@ mod login;
|
||||
mod login_ui;
|
||||
mod session_lock;
|
||||
mod capture;
|
||||
mod kms;
|
||||
mod file_trans;
|
||||
mod input;
|
||||
mod uinput;
|
||||
mod input_mapping;
|
||||
mod port_mapping;
|
||||
mod power;
|
||||
@@ -22,9 +24,12 @@ mod terminal;
|
||||
mod webrtc;
|
||||
mod video;
|
||||
#[cfg(feature = "inproc-av")]
|
||||
mod damage_capture;
|
||||
#[cfg(feature = "inproc-av")]
|
||||
mod inproc_encoder;
|
||||
mod capture_setting;
|
||||
mod audio;
|
||||
mod virtual_audio;
|
||||
mod system_actions;
|
||||
mod capability;
|
||||
mod protocol;
|
||||
@@ -52,7 +57,7 @@ use webrtc::WebRtcSession;
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();
|
||||
// An unknown argument must never fall through to starting a second full host server.
|
||||
const SUBCOMMANDS: [&str; 9] = ["login", "unlock-check", "desktop-probe", "desktop-video-probe", "desktop-audio-probe", "wayland-capture",
|
||||
const SUBCOMMANDS: [&str; 11] = ["desktop-input-probe", "virtual-audio-probe", "login", "unlock-check", "desktop-probe", "desktop-video-probe", "desktop-audio-probe", "wayland-capture",
|
||||
"wayland-input-probe", "clipboard-acceptance", "portal-acceptance"];
|
||||
if let Some(arg) = std::env::args().nth(1).filter(|a| !SUBCOMMANDS.contains(&a.as_str())) {
|
||||
let usage = format!("usage: uuremote-server [{}]\n (no argument: run the host)", SUBCOMMANDS.join(" | "));
|
||||
@@ -161,6 +166,48 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let path=std::env::args().nth(2);
|
||||
return video::probe((screen.width,screen.height),screen.display.clone(),screen.portal.clone(),path).await.map_err(|e|e.into());
|
||||
}
|
||||
if std::env::args().nth(1).as_deref() == Some("desktop-input-probe") {
|
||||
if std::fs::read_to_string("/proc/sys/kernel/hostname").unwrap_or_default().trim()!="uu-hyprland-test" {
|
||||
return Err("Input acceptance is restricted to the isolated VM".into());
|
||||
}
|
||||
let screen=ScreenCapture::new()?;
|
||||
let input=Arc::new(InputInjector::new());
|
||||
input.bind_portal(screen.portal.clone());
|
||||
let worker=input.start_pointer_worker();
|
||||
// libinput discovers newly created uinput devices asynchronously.
|
||||
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
|
||||
input.mouse_absolute_normalized(0.25,0.25,(screen.width,screen.height));
|
||||
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
|
||||
input.mouse_absolute_normalized(0.5,0.5,(screen.width,screen.height));
|
||||
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
|
||||
input.mouse_button(1,true); input.mouse_button(1,false);
|
||||
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
|
||||
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");
|
||||
return Ok(());
|
||||
}
|
||||
if std::env::args().nth(1).as_deref() == Some("wayland-input-probe") {
|
||||
return wayland::input_probe().map_err(|e|e.into());
|
||||
}
|
||||
@@ -193,6 +240,16 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
if std::env::args().nth(1).as_deref() == Some("unlock-check") {
|
||||
return session_lock::check_from_stdin().map_err(|e| e.into());
|
||||
}
|
||||
if std::env::args().nth(1).as_deref() == Some("virtual-audio-probe") {
|
||||
virtual_audio::init();
|
||||
if !virtual_audio::enable() { return Err("Virtual audio probe setup failed".into()); }
|
||||
println!("Virtual audio probe active");
|
||||
tokio::time::sleep(std::time::Duration::from_secs(5)).await;
|
||||
virtual_audio::disable();
|
||||
if virtual_audio::active() { return Err("Virtual audio probe restore incomplete".into()); }
|
||||
println!("Virtual audio probe ended");
|
||||
return Ok(());
|
||||
}
|
||||
if std::env::args().nth(1).as_deref() == Some("login") {
|
||||
// Interactive: register this host and bind it to an account via QR scan
|
||||
// (GTK window when available; `login --no-gui` keeps it terminal-only).
|
||||
@@ -200,9 +257,13 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
if login::run(gui).await.is_err() { std::process::exit(1); } // error already shown
|
||||
return Ok(());
|
||||
}
|
||||
virtual_audio::init();
|
||||
let _audio_cleanup=virtual_audio::Cleanup;
|
||||
log::info!("Starting uu-remote-linux host v{}...", env!("CARGO_PKG_VERSION"));
|
||||
|
||||
let mut capture = Arc::new((if desktop_backend::detect()?==desktop_backend::DesktopBackend::Portal
|
||||
let mut capture = Arc::new((if desktop_backend::headless() {
|
||||
Err("headless mode (UU_DESKTOP_BACKEND=none)".to_owned())
|
||||
} else if desktop_backend::detect()?==desktop_backend::DesktopBackend::Portal
|
||||
&& std::env::var("UU_PORTAL_PREAUTHORIZE").ok().as_deref()!=Some("1") {
|
||||
Err("Portal screen access requires a desktop connection and local consent".to_owned())
|
||||
} else { ScreenCapture::new() }).unwrap_or_else(|error| {
|
||||
@@ -210,13 +271,16 @@ 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();
|
||||
tokio::spawn(async move {
|
||||
if let Ok(mut term)=tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate()) {
|
||||
term.recv().await; shutdown_input.release_held(); terminal::shutdown_all(); std::process::exit(0);
|
||||
tokio::select! { _=term.recv()=>{}, _=tokio::signal::ctrl_c()=>{} }
|
||||
shutdown_input.shutdown(); virtual_audio::disable(); terminal::shutdown_all(); std::process::exit(0);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -356,6 +420,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
}
|
||||
let stale_portal=capture.portal.as_ref().is_some_and(|session|!session.is_alive())
|
||||
&& crate::session_lock::locked_now() == Some(false);
|
||||
if desktop && desktop_backend::headless() {
|
||||
log::info!("Desktop connection ignored: headless mode (terminal, file transfer and port mapping remain available)");
|
||||
continue;
|
||||
}
|
||||
let session_capture=if desktop && (capture.init_error.is_some() || stale_portal) {
|
||||
if stale_portal {
|
||||
input.release_held();input.bind_portal(None);
|
||||
|
||||
+122
-8
@@ -47,6 +47,51 @@ pub enum SignalingEvent {
|
||||
|
||||
pub struct SignalingClient {
|
||||
tx_outgoing: mpsc::Sender<String>,
|
||||
// Belongs to this room only; fresh-room reconnect must not reuse it.
|
||||
_reconnect_key: Arc<std::sync::Mutex<Option<String>>>,
|
||||
}
|
||||
|
||||
fn refreshed_key(ack:&Value)->Option<String> {
|
||||
let values=ack.as_array()?;
|
||||
if values.first()?.as_str()? != "success" { return None; }
|
||||
let key=values.get(1)?.get("reconnect_key")?.as_str()?;
|
||||
(!key.is_empty() && key.len()<=16384).then(||key.to_owned())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod refresh_tests {
|
||||
use super::refreshed_key;
|
||||
use serde_json::json;
|
||||
#[test]
|
||||
fn accepts_only_successful_complete_reconnect_key_ack() {
|
||||
assert_eq!(refreshed_key(&json!(["success",{"reconnect_key":"synthetic-key"}])),Some("synthetic-key".into()));
|
||||
for ack in [json!(["failure",{"reconnect_key":"synthetic-key"}]),json!(["success"]),json!(["success",{"reconnect_key":""}]),json!(["success",{"reconnect_key":123}]),json!({"reconnect_key":"synthetic-key"})] {
|
||||
assert!(refreshed_key(&ack).is_none());
|
||||
}
|
||||
}
|
||||
#[tokio::test]
|
||||
async fn closes_obsolete_socket_even_if_peer_retains_client() {
|
||||
use futures_util::{SinkExt,StreamExt};
|
||||
use tokio_tungstenite::tungstenite::Message;
|
||||
let listener=tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let address=listener.local_addr().unwrap();
|
||||
let server=tokio::spawn(async move {
|
||||
let (tcp,_)=listener.accept().await.unwrap();
|
||||
let mut socket=tokio_tungstenite::accept_async(tcp).await.unwrap();
|
||||
socket.send(Message::Text("0{}".into())).await.unwrap();
|
||||
assert_eq!(socket.next().await.unwrap().unwrap(),Message::Text("40".into()));
|
||||
socket.send(Message::Text("40{}".into())).await.unwrap();
|
||||
assert!(socket.next().await.unwrap().unwrap().into_text().unwrap().contains("room_info"));
|
||||
matches!(socket.next().await,None|Some(Err(_))|Some(Ok(Message::Close(_))))
|
||||
});
|
||||
let (sender,mut events)=tokio::sync::mpsc::channel(4);
|
||||
let retained=super::SignalingClient::connect(&format!("ws://{address}"),"synthetic-only",sender).await.unwrap();
|
||||
assert!(matches!(events.recv().await,Some(super::SignalingEvent::Connected)));
|
||||
drop(events);
|
||||
assert!(tokio::time::timeout(std::time::Duration::from_secs(2),server).await.unwrap().unwrap());
|
||||
// Retain it until after the server sees closure, just like PC callbacks.
|
||||
drop(retained);
|
||||
}
|
||||
}
|
||||
|
||||
impl SignalingClient {
|
||||
@@ -79,7 +124,7 @@ impl SignalingClient {
|
||||
let (tx_outgoing, mut rx_outgoing) = mpsc::channel::<String>(32);
|
||||
|
||||
// 发送协程
|
||||
tokio::spawn(async move {
|
||||
let writer=tokio::spawn(async move {
|
||||
while let Some(msg) = rx_outgoing.recv().await {
|
||||
if let Err(e) = write.send(Message::Text(msg)).await {
|
||||
log::warn!("WS send error: {}", e);
|
||||
@@ -89,23 +134,66 @@ impl SignalingClient {
|
||||
});
|
||||
|
||||
let tx_out_clone = tx_outgoing.clone();
|
||||
static GENERATION: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);
|
||||
let generation=GENERATION.fetch_add(1,std::sync::atomic::Ordering::Relaxed);
|
||||
let reconnect_key=Arc::new(std::sync::Mutex::new(None));
|
||||
let refreshed=reconnect_key.clone();
|
||||
|
||||
// 接收与状态机协程
|
||||
tokio::spawn(async move {
|
||||
let mut req_counter = 10;
|
||||
let started=std::time::Instant::now();
|
||||
let mut last_ping=started;
|
||||
let mut pings=0u64;
|
||||
let mut pong_queued=0u64;
|
||||
let mut connected=false;
|
||||
let mut next_refresh=tokio::time::Instant::now()+std::time::Duration::from_secs(30);
|
||||
let mut refresh_acks=std::collections::VecDeque::new();
|
||||
let mut accepted_refresh=0;
|
||||
// Socket.IO binary event awaiting attachments: (event name, header JSON).
|
||||
let mut pending_binary: Option<(String, Value)> = None;
|
||||
while let Some(msg_res) = read.next().await {
|
||||
loop {
|
||||
let msg_res=tokio::select! {
|
||||
_=event_sender.closed()=>break,
|
||||
// Official ControlledSession RefreshSignalTokenLoop: first
|
||||
// 30s, success every ~300s, failure retry after 30s. Native
|
||||
// libstreamer 0x885774 / 0x885e58; emit has NO payload args.
|
||||
_=tokio::time::sleep_until(next_refresh), if connected => {
|
||||
req_counter+=1;
|
||||
refresh_acks.push_back(req_counter);
|
||||
if refresh_acks.len()>16 { refresh_acks.pop_front(); }
|
||||
let packet=format!("42{}[\"refresh_reconnect_key\"]",req_counter);
|
||||
if tx_out_clone.send(packet).await.is_err() {
|
||||
let _=event_sender.send(SignalingEvent::Disconnected).await;break;
|
||||
}
|
||||
next_refresh=tokio::time::Instant::now()+std::time::Duration::from_secs(30);
|
||||
log::info!("Signaling reconnect-key refresh requested: generation={} age_s={}",generation,started.elapsed().as_secs());
|
||||
continue;
|
||||
},
|
||||
msg=read.next()=>match msg {
|
||||
Some(msg)=>msg,
|
||||
None=>{let _=event_sender.send(SignalingEvent::Disconnected).await;break;}
|
||||
},
|
||||
};
|
||||
match msg_res {
|
||||
Ok(Message::Text(txt)) => {
|
||||
// Engine.IO 报文处理
|
||||
if txt.starts_with('0') {
|
||||
if let Ok(open)=serde_json::from_str::<Value>(&txt[1..]) {
|
||||
log::info!("Signaling heartbeat config: generation={} ping_interval_ms={:?} ping_timeout_ms={:?}",generation,open.get("pingInterval").and_then(Value::as_u64),open.get("pingTimeout").and_then(Value::as_u64));
|
||||
}
|
||||
// Open packet -> 发送 Socket.IO connect
|
||||
let _ = tx_out_clone.send("40".into()).await;
|
||||
} else if txt == "2" {
|
||||
// Ping -> 回复 Pong
|
||||
let _ = tx_out_clone.send("3".into()).await;
|
||||
let gap=last_ping.elapsed().as_secs();last_ping=std::time::Instant::now();pings+=1;
|
||||
if tx_out_clone.send("3".into()).await.is_ok() { pong_queued+=1; }
|
||||
if pings==1 || pings%12==0 {
|
||||
log::info!("Signaling heartbeat progress: generation={} age_s={} pings={} pong_queued={} gap_s={}",generation,started.elapsed().as_secs(),pings,pong_queued,gap);
|
||||
}
|
||||
} else if txt.starts_with("40") {
|
||||
connected=true;
|
||||
next_refresh=tokio::time::Instant::now()+std::time::Duration::from_secs(30);
|
||||
// Socket.IO connected -> 发送 room_info 请求
|
||||
req_counter += 1;
|
||||
let room_req = format!("42{}[\"room_info\"]", req_counter);
|
||||
@@ -113,9 +201,23 @@ impl SignalingClient {
|
||||
let _ = event_sender.send(SignalingEvent::Connected).await;
|
||||
} else if txt.starts_with("43") {
|
||||
// Socket.IO ack 回调
|
||||
let payload = &txt[txt.find('[').unwrap_or(2)..];
|
||||
let begin=txt.find('[').unwrap_or(2);
|
||||
let ack_id=txt[2..begin].parse::<u64>().ok();
|
||||
let payload = &txt[begin..];
|
||||
if let Ok(v) = serde_json::from_str::<Value>(payload) {
|
||||
let _ = event_sender.send(SignalingEvent::RoomInfoAck(v)).await;
|
||||
if let Some(id)=ack_id.filter(|id|refresh_acks.contains(id)) {
|
||||
refresh_acks.retain(|pending|*pending!=id);
|
||||
let key=refreshed_key(&v);
|
||||
let success=key.is_some();
|
||||
// Accept a delayed retry response, but never replace
|
||||
// a newer key with an older successful response.
|
||||
if success && id>accepted_refresh {
|
||||
accepted_refresh=id;
|
||||
*refreshed.lock().unwrap()=key;
|
||||
next_refresh=tokio::time::Instant::now()+std::time::Duration::from_secs(300);
|
||||
}
|
||||
log::info!("Signaling reconnect-key refresh acknowledged: generation={} age_s={} success={}",generation,started.elapsed().as_secs(),success);
|
||||
} else { let _ = event_sender.send(SignalingEvent::RoomInfoAck(v)).await; }
|
||||
}
|
||||
} else if txt.starts_with("45") || txt.starts_with("46") {
|
||||
// Socket.IO binary event/ack: header text, attachments follow as binary frames.
|
||||
@@ -170,14 +272,16 @@ impl SignalingClient {
|
||||
}
|
||||
}
|
||||
"released" => {
|
||||
log::info!("Cloud peer event: generation={} event=released age_s={} pings={} last_ping_gap_s={} ice_id_present={}",generation,started.elapsed().as_secs(),pings,last_ping.elapsed().as_secs(),data.get("ice_id").is_some());
|
||||
let _ = event_sender
|
||||
.send(SignalingEvent::Released(data.clone()))
|
||||
.await;
|
||||
}
|
||||
"left" | "leave" => {
|
||||
log::info!("Cloud peer event: generation={} event={} age_s={} pings={} last_ping_gap_s={} ice_id_present={}",generation,event_name,started.elapsed().as_secs(),pings,last_ping.elapsed().as_secs(),data.get("ice_id").is_some());
|
||||
let ice_id = data.get("ice_id").and_then(|v| v.as_str()).unwrap_or("");
|
||||
if ice_id.is_empty() {
|
||||
log::warn!("Room expired from NetEase cloud (leave event). Triggering room renewal...");
|
||||
log::warn!("Cloud room leave without peer identifier; renewing signaling (expiry cause unknown)");
|
||||
let _ = event_sender.send(SignalingEvent::Disconnected).await;
|
||||
} else {
|
||||
let _ = event_sender
|
||||
@@ -212,16 +316,26 @@ impl SignalingClient {
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(Message::Close(_)) | Err(_) => {
|
||||
Ok(Message::Close(frame)) => {
|
||||
log::warn!("Signaling socket closed: generation={} age_s={} close_code={:?} pings={} last_ping_gap_s={}",generation,started.elapsed().as_secs(),frame.map(|f|u16::from(f.code)),pings,last_ping.elapsed().as_secs());
|
||||
let _ = event_sender.send(SignalingEvent::Disconnected).await;
|
||||
break;
|
||||
}
|
||||
Err(_) => {
|
||||
log::warn!("Signaling socket receive failed: generation={} age_s={} pings={} last_ping_gap_s={}",generation,started.elapsed().as_secs(),pings,last_ping.elapsed().as_secs());
|
||||
let _ = event_sender.send(SignalingEvent::Disconnected).await;
|
||||
break;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
log::info!("Signaling receiver stopped: generation={} age_s={}",generation,started.elapsed().as_secs());
|
||||
// Live peer callbacks can retain the old SignalingClient sender.
|
||||
// Explicitly drop its write half too when this room stops reading.
|
||||
writer.abort();
|
||||
});
|
||||
|
||||
Ok(Self { tx_outgoing })
|
||||
Ok(Self { tx_outgoing, _reconnect_key:reconnect_key })
|
||||
}
|
||||
|
||||
pub async fn emit_event(&self, event: &str, payload: serde_json::Value) -> Result<(), String> {
|
||||
|
||||
+10
-5
@@ -113,10 +113,14 @@ pub fn args_shape(args: &[u8]) -> String {
|
||||
}
|
||||
|
||||
fn mute_for_session(state: &mut HostState) {
|
||||
if state.restore.is_none() { state.restore = host_muted(); }
|
||||
set_host_muted(true);
|
||||
if state.restore.is_none() {
|
||||
state.restore = host_muted();
|
||||
set_host_muted(true);
|
||||
}
|
||||
crate::virtual_audio::enable();
|
||||
}
|
||||
fn restore_mute(state: &mut HostState) {
|
||||
crate::virtual_audio::disable();
|
||||
if let Some(previous) = state.restore.take() { set_host_muted(previous); }
|
||||
}
|
||||
|
||||
@@ -143,20 +147,20 @@ impl PeerActions {
|
||||
let on = toggle_value(value, args);
|
||||
state.settings.keep_mute = on;
|
||||
if on { mute_for_session(&mut state); } else { restore_mute(&mut state); }
|
||||
log::info!("System action mute (action {}): on={} muted={:?}", action, on, host_muted());
|
||||
log::info!("System action mute (action {}): on={} virtual_sink={} default_muted={:?}", action, on, crate::virtual_audio::active(), host_muted());
|
||||
}
|
||||
// Keep the host muted after the session (远程结束后,恢复被控端声音设置 unchecked).
|
||||
MUTE_PERMANENT => {
|
||||
let on = toggle_value(value, args);
|
||||
state.settings.mute_permanent = on;
|
||||
if on { state.settings.keep_mute = true; mute_for_session(&mut state); }
|
||||
log::info!("System action mute_permanent: on={} muted={:?}", on, host_muted());
|
||||
log::info!("System action mute_permanent: on={} virtual_sink={} default_muted={:?}", on, crate::virtual_audio::active(), host_muted());
|
||||
}
|
||||
MUTE_RESTORE => {
|
||||
state.settings.keep_mute = false;
|
||||
state.settings.mute_permanent = false;
|
||||
restore_mute(&mut state);
|
||||
log::info!("System action mute_restore: muted={:?}", host_muted());
|
||||
log::info!("System action mute_restore: virtual_sink={} default_muted={:?}", crate::virtual_audio::active(), host_muted());
|
||||
}
|
||||
SET_AUTO_LOCK => {
|
||||
// Observed: enabling 远程结束后自动锁屏 sends value 2 and disabling value 1.
|
||||
@@ -188,6 +192,7 @@ impl PeerActions {
|
||||
let mut state = state().lock().unwrap();
|
||||
state.desktop_sessions = state.desktop_sessions.saturating_sub(1);
|
||||
if state.desktop_sessions > 0 { return; }
|
||||
crate::virtual_audio::disable();
|
||||
if !state.settings.mute_permanent { restore_mute(&mut state); }
|
||||
if state.settings.auto_lock {
|
||||
log::info!("Auto lock after remote session: ok={}", lock_session());
|
||||
|
||||
+56
-21
@@ -104,7 +104,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,7 +128,13 @@ 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()) });
|
||||
let mut reader = std::fs::File::from(session.master.try_clone().map_err(|e| format!("pty dup failed: {e}"))?);
|
||||
@@ -207,12 +213,14 @@ impl TerminalConn {
|
||||
let (cols, rows) = dims(&v);
|
||||
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); }
|
||||
}
|
||||
@@ -241,7 +249,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 +277,51 @@ 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 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)));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+292
@@ -0,0 +1,292 @@
|
||||
//! Kernel-level virtual keyboard and pointer through /dev/uinput (as Sunshine does).
|
||||
//!
|
||||
//! Works the same under X11, every Wayland compositor and the greeter/lock screen, needs no
|
||||
//! portal permission and sends physical keycodes, so the compositor's keysym → key lookup
|
||||
//! (which can pick an AltGr level for `minus` and drop `_`, issue #3) is not involved.
|
||||
//! Access: a udev rule `KERNEL=="uinput", SUBSYSTEM=="misc", OPTIONS+="static_node=uinput",
|
||||
//! TAG+="uaccess"` grants the logged-in seat user access (see README).
|
||||
//!
|
||||
//! Devices: one keyboard + relative mouse, one absolute pointer (ABS_X/ABS_Y 0..=65535 with
|
||||
//! buttons and wheels, like a QEMU tablet). Mixing relative and absolute axes in one device
|
||||
//! confuses libinput.
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
mod imp {
|
||||
use std::fs::{File, OpenOptions};
|
||||
use std::io::Write;
|
||||
use std::os::unix::fs::OpenOptionsExt;
|
||||
use std::os::unix::io::AsRawFd;
|
||||
use std::sync::Mutex;
|
||||
|
||||
const EV_SYN: u16 = 0x00;
|
||||
const EV_KEY: u16 = 0x01;
|
||||
const EV_REL: u16 = 0x02;
|
||||
const EV_ABS: u16 = 0x03;
|
||||
const SYN_REPORT: u16 = 0;
|
||||
const REL_X: u16 = 0x00;
|
||||
const REL_Y: u16 = 0x01;
|
||||
const REL_HWHEEL: u16 = 0x06;
|
||||
const REL_WHEEL: u16 = 0x08;
|
||||
const ABS_X: u16 = 0x00;
|
||||
const ABS_Y: u16 = 0x01;
|
||||
pub const BTN_LEFT: u16 = 0x110;
|
||||
pub const BTN_RIGHT: u16 = 0x111;
|
||||
pub const BTN_MIDDLE: u16 = 0x112;
|
||||
pub const BTN_SIDE: u16 = 0x113;
|
||||
pub const BTN_EXTRA: u16 = 0x114;
|
||||
pub const ABS_MAX: i32 = 65535;
|
||||
|
||||
const UI_SET_EVBIT: libc::c_ulong = 0x4004_5564;
|
||||
const UI_SET_KEYBIT: libc::c_ulong = 0x4004_5565;
|
||||
const UI_SET_RELBIT: libc::c_ulong = 0x4004_5566;
|
||||
const UI_SET_ABSBIT: libc::c_ulong = 0x4004_5567;
|
||||
const UI_DEV_CREATE: libc::c_ulong = 0x5501;
|
||||
const UI_DEV_DESTROY: libc::c_ulong = 0x5502;
|
||||
const UI_DEV_SETUP: libc::c_ulong = 0x405c_5503;
|
||||
const UI_ABS_SETUP: libc::c_ulong = 0x401c_5504;
|
||||
|
||||
#[repr(C)]
|
||||
struct InputId { bustype: u16, vendor: u16, product: u16, version: u16 }
|
||||
#[repr(C)]
|
||||
struct UinputSetup { id: InputId, name: [u8; 80], ff_effects_max: u32 }
|
||||
#[repr(C)]
|
||||
struct AbsInfo { value: i32, minimum: i32, maximum: i32, fuzz: i32, flat: i32, resolution: i32 }
|
||||
#[repr(C)]
|
||||
struct UinputAbsSetup { code: u16, _pad: u16, absinfo: AbsInfo }
|
||||
#[repr(C)]
|
||||
struct InputEvent { time: libc::timeval, kind: u16, code: u16, value: i32 }
|
||||
|
||||
struct Device(File);
|
||||
impl Drop for Device {
|
||||
fn drop(&mut self) { unsafe { libc::ioctl(self.0.as_raw_fd(), UI_DEV_DESTROY); } }
|
||||
}
|
||||
impl Device {
|
||||
fn ioctl(&self, request: libc::c_ulong, value: libc::c_ulong) -> std::io::Result<()> {
|
||||
if unsafe { libc::ioctl(self.0.as_raw_fd(), request, value) } < 0 { Err(std::io::Error::last_os_error()) } else { Ok(()) }
|
||||
}
|
||||
fn create(name: &str, product: u16, configure: impl FnOnce(&Device) -> std::io::Result<()>) -> std::io::Result<Self> {
|
||||
let file = OpenOptions::new().write(true).custom_flags(libc::O_NONBLOCK).open("/dev/uinput")?;
|
||||
let device = Device(file);
|
||||
configure(&device)?;
|
||||
let mut setup = UinputSetup { id: InputId { bustype: 0x06 /* BUS_VIRTUAL */, vendor: 0x1d6b, product, version: 1 }, name: [0; 80], ff_effects_max: 0 };
|
||||
setup.name[..name.len().min(79)].copy_from_slice(&name.as_bytes()[..name.len().min(79)]);
|
||||
if unsafe { libc::ioctl(device.0.as_raw_fd(), UI_DEV_SETUP, &setup) } < 0 { return Err(std::io::Error::last_os_error()); }
|
||||
device.ioctl(UI_DEV_CREATE, 0)?;
|
||||
Ok(device)
|
||||
}
|
||||
fn abs(&self, code: u16) -> std::io::Result<()> {
|
||||
self.ioctl(UI_SET_ABSBIT, code.into())?;
|
||||
let setup = UinputAbsSetup { code, _pad: 0, absinfo: AbsInfo { value: 0, minimum: 0, maximum: ABS_MAX, fuzz: 0, flat: 0, resolution: 0 } };
|
||||
if unsafe { libc::ioctl(self.0.as_raw_fd(), UI_ABS_SETUP, &setup) } < 0 { return Err(std::io::Error::last_os_error()); }
|
||||
Ok(())
|
||||
}
|
||||
fn emit(&mut self, events: &[(u16, u16, i32)]) -> std::io::Result<()> {
|
||||
let zero = libc::timeval { tv_sec: 0, tv_usec: 0 };
|
||||
let mut buffer = Vec::with_capacity((events.len() + 1) * std::mem::size_of::<InputEvent>());
|
||||
for &(kind, code, value) in events.iter().chain(std::iter::once(&(EV_SYN, SYN_REPORT, 0))) {
|
||||
let event = InputEvent { time: zero, kind, code, value };
|
||||
let bytes = unsafe { std::slice::from_raw_parts((&event as *const InputEvent).cast::<u8>(), std::mem::size_of::<InputEvent>()) };
|
||||
buffer.extend_from_slice(bytes);
|
||||
}
|
||||
self.0.write_all(&buffer)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Devices { keyboard: Mutex<Device>, pointer: Mutex<Device> }
|
||||
|
||||
impl Devices {
|
||||
pub fn open() -> std::io::Result<Self> {
|
||||
let keyboard = Device::create("uu-remote-linux keyboard", 1, |d| {
|
||||
d.ioctl(UI_SET_EVBIT, EV_KEY.into())?;
|
||||
for code in 1..=248u16 { d.ioctl(UI_SET_KEYBIT, code.into())?; }
|
||||
// Relative motion (games / pointer lock) lives with the keyboard device.
|
||||
d.ioctl(UI_SET_EVBIT, EV_REL.into())?;
|
||||
d.ioctl(UI_SET_RELBIT, REL_X.into())?;
|
||||
d.ioctl(UI_SET_RELBIT, REL_Y.into())?;
|
||||
for button in [BTN_LEFT, BTN_RIGHT, BTN_MIDDLE] { d.ioctl(UI_SET_KEYBIT, button.into())?; }
|
||||
Ok(())
|
||||
})?;
|
||||
let pointer = Device::create("uu-remote-linux pointer", 2, |d| {
|
||||
d.ioctl(UI_SET_EVBIT, EV_KEY.into())?;
|
||||
for button in [BTN_LEFT, BTN_RIGHT, BTN_MIDDLE, BTN_SIDE, BTN_EXTRA] { d.ioctl(UI_SET_KEYBIT, button.into())?; }
|
||||
d.ioctl(UI_SET_EVBIT, EV_REL.into())?;
|
||||
d.ioctl(UI_SET_RELBIT, REL_WHEEL.into())?;
|
||||
d.ioctl(UI_SET_RELBIT, REL_HWHEEL.into())?;
|
||||
d.ioctl(UI_SET_EVBIT, EV_ABS.into())?;
|
||||
d.abs(ABS_X)?;
|
||||
d.abs(ABS_Y)
|
||||
})?;
|
||||
Ok(Self { keyboard: Mutex::new(keyboard), pointer: Mutex::new(pointer) })
|
||||
}
|
||||
pub fn key(&self, code: u16, pressed: bool) -> std::io::Result<()> {
|
||||
self.keyboard.lock().unwrap().emit(&[(EV_KEY, code, i32::from(pressed))])
|
||||
}
|
||||
pub fn 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))])
|
||||
}
|
||||
pub fn move_relative(&self, dx: i32, dy: i32) -> std::io::Result<()> {
|
||||
self.keyboard.lock().unwrap().emit(&[(EV_REL, REL_X, dx), (EV_REL, REL_Y, dy)])
|
||||
}
|
||||
pub fn button(&self, code: u16, pressed: bool) -> std::io::Result<()> {
|
||||
self.pointer.lock().unwrap().emit(&[(EV_KEY, code, i32::from(pressed))])
|
||||
}
|
||||
pub fn wheel(&self, vertical: i32, horizontal: i32) -> std::io::Result<()> {
|
||||
let mut events = Vec::new();
|
||||
if vertical != 0 { events.push((EV_REL, REL_WHEEL, vertical)); }
|
||||
if horizontal != 0 { events.push((EV_REL, REL_HWHEEL, horizontal)); }
|
||||
if events.is_empty() { return Ok(()); }
|
||||
self.pointer.lock().unwrap().emit(&events)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(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"))]
|
||||
mod imp {
|
||||
pub const BTN_LEFT: u16 = 0x110;
|
||||
pub const BTN_RIGHT: u16 = 0x111;
|
||||
pub const BTN_MIDDLE: u16 = 0x112;
|
||||
pub const BTN_SIDE: u16 = 0x113;
|
||||
pub const BTN_EXTRA: u16 = 0x114;
|
||||
pub const ABS_MAX: i32 = 65535;
|
||||
pub struct Devices;
|
||||
impl Devices {
|
||||
pub fn open() -> std::io::Result<Self> { Err(std::io::Error::new(std::io::ErrorKind::Unsupported, "uinput requires Linux")) }
|
||||
pub fn 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(()) }
|
||||
pub fn button(&self, _: u16, _: bool) -> std::io::Result<()> { Ok(()) }
|
||||
pub fn wheel(&self, _: i32, _: i32) -> std::io::Result<()> { Ok(()) }
|
||||
}
|
||||
}
|
||||
|
||||
pub use imp::{Devices, ABS_MAX, BTN_EXTRA, BTN_LEFT, BTN_MIDDLE, BTN_RIGHT, BTN_SIDE};
|
||||
|
||||
/// X11 button ordinal (1 left, 2 middle, 3 right, 8/9 side) → evdev button code.
|
||||
pub fn button_code(x11_button: i32) -> Option<u16> {
|
||||
match x11_button { 1 => Some(BTN_LEFT), 2 => Some(BTN_MIDDLE), 3 => Some(BTN_RIGHT), 8 => Some(BTN_SIDE), 9 => Some(BTN_EXTRA), _ => None }
|
||||
}
|
||||
|
||||
/// X11 wheel "buttons" 4/5 (vertical) and 6/7 (horizontal) → (REL_WHEEL, REL_HWHEEL) steps.
|
||||
pub fn wheel_step(x11_button: i32) -> Option<(i32, i32)> {
|
||||
match x11_button { 4 => Some((1, 0)), 5 => Some((-1, 0)), 6 => Some((0, -1)), 7 => Some((0, 1)), _ => None }
|
||||
}
|
||||
|
||||
/// Base keysym (as produced by input_mapping::windows_vk_to_keysym) → physical evdev keycode
|
||||
/// at its US-layout position. The compositor then applies the user's own layout, exactly as
|
||||
/// for a hardware keyboard.
|
||||
pub fn keysym_to_evdev(keysym: u64) -> Option<u16> {
|
||||
const LETTERS: [u16; 26] = [30, 48, 46, 32, 18, 33, 34, 35, 23, 36, 37, 38, 50, 49, 24, 25, 16, 19, 31, 20, 22, 47, 17, 45, 21, 44];
|
||||
const KEYPAD: [u16; 10] = [82, 79, 80, 81, 75, 76, 77, 71, 72, 73];
|
||||
let code = match keysym {
|
||||
0x61..=0x7a => LETTERS[(keysym - 0x61) as usize],
|
||||
0x31..=0x39 => (keysym - 0x31) as u16 + 2, // 1..9
|
||||
0x30 => 11,
|
||||
0xffb0..=0xffb9 => KEYPAD[(keysym - 0xffb0) as usize],
|
||||
0xffbe..=0xffc7 => (keysym - 0xffbe) as u16 + 59, // F1..F10
|
||||
0xffc8 => 87, 0xffc9 => 88, // F11, F12
|
||||
0xffca..=0xffd5 => (keysym - 0xffca) as u16 + 183, // F13..F24
|
||||
0xff0b => 76, // Clear: keypad 5 without NumLock, as on PC keyboards
|
||||
0xff08 => 14, 0xff09 => 15, 0xff0d => 28, 0xff1b => 1, 0x20 => 57,
|
||||
0xff13 => 119, 0xffe5 => 58, 0xff7f => 69, 0xff14 => 70, 0xff61 => 99,
|
||||
0xffe1 => 42, 0xffe2 => 54, 0xffe3 => 29, 0xffe4 => 97, 0xffe9 => 56, 0xffea => 100,
|
||||
0xffeb => 125, 0xffec => 126, 0xff67 => 127,
|
||||
0xff55 => 104, 0xff56 => 109, 0xff57 => 107, 0xff50 => 102,
|
||||
0xff51 => 105, 0xff52 => 103, 0xff53 => 106, 0xff54 => 108,
|
||||
0xff63 => 110, 0xffff => 111,
|
||||
0xffaa => 55, 0xffab => 78, 0xffac => 121, 0xffad => 74, 0xffae => 83, 0xffaf => 98, 0xff8d => 96,
|
||||
0x3b => 39, 0x3d => 13, 0x2c => 51, 0x2d => 12, 0x2e => 52, 0x2f => 53, 0x60 => 41,
|
||||
0x5b => 26, 0x5c => 43, 0x5d => 27, 0x27 => 40,
|
||||
0x1008ff12 => 113, 0x1008ff11 => 114, 0x1008ff13 => 115,
|
||||
0x1008ff17 => 163, 0x1008ff16 => 165, 0x1008ff15 => 166, 0x1008ff14 => 164,
|
||||
_ => return None,
|
||||
};
|
||||
Some(code)
|
||||
}
|
||||
|
||||
/// Input backend policy: `UU_INPUT_BACKEND=uinput` forces uinput, `native` disables it,
|
||||
/// `auto` (default) uses uinput for Wayland/portal sessions when /dev/uinput is accessible
|
||||
/// and keeps the verified XTest path on X11.
|
||||
pub fn wanted(backend: crate::desktop_backend::DesktopBackend) -> bool {
|
||||
match std::env::var("UU_INPUT_BACKEND").as_deref() {
|
||||
Ok("uinput") => true,
|
||||
Ok("native") => false,
|
||||
_ => backend != crate::desktop_backend::DesktopBackend::X11,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn maps_keysyms_to_physical_us_positions() {
|
||||
assert_eq!(keysym_to_evdev(0x61), Some(30)); // a
|
||||
assert_eq!(keysym_to_evdev(0x7a), Some(44)); // z
|
||||
assert_eq!(keysym_to_evdev(0x30), Some(11)); // 0
|
||||
assert_eq!(keysym_to_evdev(0x31), Some(2)); // 1
|
||||
assert_eq!(keysym_to_evdev(0x2d), Some(12)); // minus: Shift+minus gives '_' (#3)
|
||||
assert_eq!(keysym_to_evdev(0xffe1), Some(42)); // Shift_L
|
||||
assert_eq!(keysym_to_evdev(0xffc9), Some(88)); // F12
|
||||
assert_eq!(keysym_to_evdev(0xffb5), Some(76)); // KP_5
|
||||
assert_eq!(keysym_to_evdev(0x12345), None);
|
||||
}
|
||||
#[test]
|
||||
fn every_mapped_windows_vk_has_a_physical_key() {
|
||||
let missing: Vec<i32> = (0..=0xff).filter(|vk| {
|
||||
crate::input_mapping::windows_vk_to_keysym(*vk).is_some_and(|ks| keysym_to_evdev(ks).is_none())
|
||||
}).collect();
|
||||
assert!(missing.is_empty(), "VKs without evdev mapping: {missing:x?}");
|
||||
}
|
||||
#[test]
|
||||
fn buttons_and_wheel() {
|
||||
assert_eq!(button_code(1), Some(BTN_LEFT));
|
||||
assert_eq!(button_code(3), Some(BTN_RIGHT));
|
||||
assert_eq!(wheel_step(4), Some((1, 0)));
|
||||
assert_eq!(wheel_step(7), Some((0, 1)));
|
||||
assert_eq!(button_code(4), None);
|
||||
}
|
||||
}
|
||||
+125
-24
@@ -53,9 +53,36 @@ struct CompleteFrame {
|
||||
|
||||
const RTP_CLOCK: u64 = 90_000;
|
||||
|
||||
/// `UU_VIDEO_ENCODER=cpu` keeps the virtual CPU display entirely off the GPU.
|
||||
pub fn gpu_encoder_requested() -> bool {
|
||||
std::env::var("UU_VIDEO_ENCODER").map_or(true, |v| v != "cpu")
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub(crate) enum Encoder { Cpu, Nvenc, Vaapi }
|
||||
impl Encoder {
|
||||
pub(crate) fn requested() -> Self { Self::from_setting(std::env::var("UU_VIDEO_ENCODER").ok().as_deref(), nvidia_present()) }
|
||||
/// Unset/auto tries NVENC only with an NVIDIA driver loaded: `ffmpeg -encoders` lists h264_nvenc
|
||||
/// on any machine, so without this every stream start first ran a doomed NVENC process (#10).
|
||||
fn from_setting(value: Option<&str>, nvidia: bool) -> Self {
|
||||
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" } }
|
||||
}
|
||||
fn nvidia_present() -> bool {
|
||||
std::path::Path::new("/proc/driver/nvidia/version").exists() || std::path::Path::new("/dev/nvidia0").exists()
|
||||
}
|
||||
pub(crate) fn vaapi_device() -> String { std::env::var("UU_VAAPI_DEVICE").unwrap_or_else(|_| "/dev/dri/renderD128".into()) }
|
||||
|
||||
/// NV12 conversion must precede upload; scaling runs on software frames, before hwupload.
|
||||
fn vaapi_filter(output: Option<(u32, u32)>) -> String {
|
||||
let scale = output.map_or(String::new(), |(w,h)| format!("scale={w}:{h}:flags=bilinear,"));
|
||||
format!("{scale}format=nv12,hwupload")
|
||||
}
|
||||
fn vaapi_options(settings: &VideoSettings, gop: &str) -> Vec<String> {
|
||||
let (maxrate, buffer) = rate_args(settings, 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",
|
||||
"-rc_mode", "VBR", "-b:v", &settings.max_kbps().map_or("16M".into(), |k| format!("{}k",k*3/4)),
|
||||
"-maxrate", &maxrate, "-bufsize", &buffer, "-qmin", &settings.cq().to_string(), "-async_depth", "1", "-aud", "1"]
|
||||
.into_iter().map(str::to_owned).collect()
|
||||
}
|
||||
|
||||
/// Round UP (+1ns float guard) so webrtc's floor(duration*90000) yields exactly `ticks`.
|
||||
@@ -121,12 +148,13 @@ async fn read_encoder(
|
||||
settings: VideoSettings, sequence: Arc<std::sync::atomic::AtomicU64>, idr: Arc<AtomicBool>,
|
||||
) -> Result<(), String> {
|
||||
// The sequence is shared so a CPU fallback or settings restart continues it (skip statistics stay correct).
|
||||
if !gpu_encoder_requested() { return read_encoder_with(size,display,portal,latest,false,sequence,settings,idr).await; }
|
||||
match read_encoder_with(size,display.clone(),portal.clone(),latest.clone(),true,sequence.clone(),settings,idr.clone()).await {
|
||||
let encoder = Encoder::requested();
|
||||
if encoder == Encoder::Cpu { return read_encoder_with(size,display,portal,latest,Encoder::Cpu,sequence,settings,idr).await; }
|
||||
match read_encoder_with(size,display.clone(),portal.clone(),latest.clone(),encoder,sequence.clone(),settings,idr.clone()).await {
|
||||
Ok(())=>Ok(()),
|
||||
Err(error) if !latest.is_closed()=>{
|
||||
log::warn!("NVENC stream unavailable; switching scoped encoder to CPU: {}",error);
|
||||
read_encoder_with(size,display,portal,latest,false,sequence,settings,idr).await
|
||||
log::warn!("{} stream unavailable; switching scoped encoder to CPU: {}",encoder.name(),error);
|
||||
read_encoder_with(size,display,portal,latest,Encoder::Cpu,sequence,settings,idr).await
|
||||
},
|
||||
Err(error)=>Err(error),
|
||||
}
|
||||
@@ -160,17 +188,29 @@ async fn read_inprocess(
|
||||
|
||||
/// Rate-control arguments: constant quality capped at 30M (自动), or capped at the viewer's
|
||||
/// chosen bitrate. The VBV buffer stays at most 500k so a cap never adds queueing latency.
|
||||
fn rate_args(settings: &VideoSettings) -> (String, String) {
|
||||
/// VBV buffer in kbit. NVENC ull and VAAPI keep the small low-latency buffer; libx264 needs room for a
|
||||
/// whole IDR frame, otherwise large desktops get near-grey keyframes that sharpen over the GOP (#8).
|
||||
/// Half a second of bitrate still starved repeated 0.5 s-GOP keyframes at 3296x2036/2M; one second did not.
|
||||
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 rate_args(settings: &VideoSettings, encoder: Encoder) -> (String, String) {
|
||||
let max_kbps = settings.max_kbps().unwrap_or(30_000);
|
||||
let buffer_kbits = (max_kbps / 8).clamp(100, 500);
|
||||
(format!("{max_kbps}k"), format!("{buffer_kbits}k"))
|
||||
(format!("{max_kbps}k"), format!("{}k", buffer_kbits(max_kbps, encoder)))
|
||||
}
|
||||
|
||||
async fn read_encoder_with(
|
||||
size: (u32,u32),display:String,portal:Option<Arc<crate::portal::PortalSession>>,latest:mpsc::Sender<Arc<CompleteFrame>>,gpu:bool,sequence:Arc<std::sync::atomic::AtomicU64>,
|
||||
mut settings: VideoSettings, #[cfg_attr(not(feature = "inproc-av"), allow(unused_variables))] idr: Arc<AtomicBool>,
|
||||
size: (u32,u32),display:String,portal:Option<Arc<crate::portal::PortalSession>>,latest:mpsc::Sender<Arc<CompleteFrame>>,encoder:Encoder,sequence:Arc<std::sync::atomic::AtomicU64>,
|
||||
settings: VideoSettings, #[cfg_attr(not(feature = "inproc-av"), allow(unused_variables))] idr: Arc<AtomicBool>,
|
||||
)->Result<(),String>{
|
||||
let wayland=display.starts_with("wayland:");
|
||||
let kms=display.starts_with("kms:");
|
||||
let wayland=kms || display.starts_with("wayland:");
|
||||
let kms_capture=if kms {
|
||||
let capture=tokio::task::spawn_blocking(crate::kms::Capture::open).await.map_err(|_|"KMS worker failed")??;
|
||||
if (capture.width,capture.height)!=size { return Err("KMS mode changed; restart host to refresh desktop geometry".into()); }
|
||||
Some(Arc::new(capture))
|
||||
} else { None };
|
||||
#[cfg(feature = "inproc-av")]
|
||||
if !wayland && portal.is_none() && crate::inproc_encoder::selected() {
|
||||
let max_kbps = settings.max_kbps().unwrap_or(30_000) as i64;
|
||||
@@ -178,28 +218,28 @@ async fn read_encoder_with(
|
||||
display: display.clone(), input: size, output: settings.output, fps: u32::from(settings.fps),
|
||||
gop: gop_frames(settings.fps, true) as u32, cq: settings.cq() as u32,
|
||||
target_bps: settings.max_kbps().map_or(16_000, |k| i64::from(k) * 3 / 4) * 1000,
|
||||
max_bps: max_kbps * 1000, buffer_bits: (max_kbps / 8).clamp(100, 500) * 1000, gpu,
|
||||
max_bps: max_kbps * 1000, buffer_bits: i64::from(buffer_kbits(max_kbps as u32, encoder)) * 1000, encoder,
|
||||
damage: crate::inproc_encoder::damage_enabled(),
|
||||
};
|
||||
log::info!("Desktop encoder: display={} source={}x{} output={:?} fps={} cq={} maxrate={}k encoder={} pipeline=in-process",
|
||||
display,size.0,size.1,settings.output,settings.fps,config.cq,max_kbps,if gpu{"h264_nvenc"}else{"libx264"});
|
||||
display,size.0,size.1,settings.output,settings.fps,config.cq,max_kbps,encoder.name());
|
||||
return read_inprocess(config, latest, sequence, idr).await;
|
||||
}
|
||||
let size = format!("{}x{}", size.0, size.1);
|
||||
// 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);
|
||||
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));
|
||||
log::info!("Desktop encoder: display={} source={} output={:?} fps={} cq={} maxrate={} encoder={} low_latency=true",display,size,settings.output,settings.fps,cq,maxrate,if gpu{"h264_nvenc"}else{"libx264"});
|
||||
log::info!("Desktop encoder: display={} source={} output={:?} fps={} cq={} maxrate={} encoder={} low_latency=true",display,size,settings.output,settings.fps,cq,maxrate,encoder.name());
|
||||
// A bundled FFmpeg prefix (config ffmpeg_root / UU_FFMPEG_ROOT) or `ffmpeg` on PATH.
|
||||
let mut command=match crate::config::ffmpeg_root() {
|
||||
Some(root) => { let mut c=Command::new(root.join("bin/ffmpeg")); c.env("LD_LIBRARY_PATH",root.join("lib/x86_64-linux-gnu")); c }
|
||||
None => Command::new("ffmpeg"),
|
||||
};
|
||||
command.args(["-hide_banner","-loglevel","error","-nostdin"]);
|
||||
if encoder == Encoder::Vaapi { command.args(["-vaapi_device", &vaapi_device()]); }
|
||||
if wayland {
|
||||
command.args(["-f","rawvideo","-pixel_format","bgra","-video_size",&size,
|
||||
"-framerate",&fps,"-i","pipe:0","-an"]);
|
||||
@@ -209,10 +249,14 @@ async fn read_encoder_with(
|
||||
"-video_size",&size,"-i",&display,"-an"]);
|
||||
}
|
||||
// 其他分辨率: scale the captured desktop (the host display mode is left untouched).
|
||||
if let Some((width, height)) = settings.output {
|
||||
if encoder == Encoder::Vaapi {
|
||||
command.args(["-vf", &vaapi_filter(settings.output)]);
|
||||
} else if let Some((width, height)) = settings.output {
|
||||
command.args(["-vf", &format!("scale={width}:{height}:flags=bilinear")]);
|
||||
}
|
||||
if gpu{
|
||||
if encoder == Encoder::Vaapi {
|
||||
command.args(vaapi_options(&settings, &gop));
|
||||
} else if encoder == Encoder::Nvenc {
|
||||
command.args(["-c:v","h264_nvenc","-preset","p4","-tune","ull",
|
||||
"-profile:v","baseline","-pix_fmt","yuv420p","-g",&gop,"-bf","0",
|
||||
"-rc","vbr","-cq",&cq,"-b:v",&target,"-maxrate",&maxrate,"-bufsize",&bufsize,
|
||||
@@ -236,7 +280,23 @@ async fn read_encoder_with(
|
||||
.stderr(std::process::Stdio::inherit())
|
||||
.spawn().map_err(|e| format!("Start scoped encoder: {e}"))?;
|
||||
// Scope capture to this encoder. Dropping either task kills its child.
|
||||
let _capture=if let Some(session)=portal {
|
||||
let _capture=if let Some(capture)=kms_capture {
|
||||
let mut sink=child.stdin.take().ok_or("Encoder stdin missing")?;
|
||||
Some(EncoderTask(tokio::spawn(async move {
|
||||
use tokio::io::AsyncWriteExt;
|
||||
let mut tick=tokio::time::interval(Duration::from_nanos(1_000_000_000/u64::from(settings.fps)));
|
||||
tick.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
|
||||
loop {
|
||||
tick.tick().await;
|
||||
let capture=capture.clone();
|
||||
let bytes=tokio::task::spawn_blocking(move || {
|
||||
let mut bytes=vec![0;capture.width as usize*capture.height as usize*4];
|
||||
capture.capture(&mut bytes)?; Ok::<_,String>(bytes)
|
||||
}).await.map_err(|_|"KMS worker failed")??;
|
||||
sink.write_all(&bytes).await.map_err(|_|"KMS encoder pipe failed")?;
|
||||
}
|
||||
})))
|
||||
} else if let Some(session)=portal {
|
||||
let mut sink=child.stdin.take().ok_or("Encoder stdin missing")?;
|
||||
Some(EncoderTask(tokio::spawn(async move {
|
||||
use tokio::io::AsyncWriteExt;
|
||||
@@ -261,7 +321,7 @@ async fn read_encoder_with(
|
||||
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")
|
||||
let mut capture=wayland_capture_command(&executable,settings.fps)
|
||||
.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")?;
|
||||
@@ -297,6 +357,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
|
||||
@@ -311,7 +377,9 @@ 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())?;
|
||||
let settings=VideoSettings {fps,..VideoSettings::default()};
|
||||
let _reader=EncoderTask(tokio::spawn(read_encoder(size,display,portal,tx,settings,sequence,Arc::new(AtomicBool::new(false)))));
|
||||
let mut count=0;
|
||||
let mut bytes=0;
|
||||
// UU_PROBE_FRAMES lengthens the sample for baseline measurements; default is 5 frames.
|
||||
@@ -401,7 +469,40 @@ 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::*;
|
||||
#[test]
|
||||
fn x264_buffer_fits_keyframes() {
|
||||
assert_eq!(buffer_kbits(2000, Encoder::Nvenc), 250);
|
||||
assert_eq!(buffer_kbits(2000, Encoder::Vaapi), 250);
|
||||
assert_eq!(buffer_kbits(2000, Encoder::Cpu), 2000);
|
||||
assert_eq!(buffer_kbits(300, Encoder::Cpu), 500);
|
||||
assert_eq!(buffer_kbits(30_000, Encoder::Cpu), 8000);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn vaapi_is_explicit_and_upload_follows_scale() {
|
||||
assert_eq!(Encoder::from_setting(None, true), Encoder::Nvenc);
|
||||
assert_eq!(Encoder::from_setting(None, false), Encoder::Cpu);
|
||||
assert_eq!(Encoder::from_setting(Some("nvenc"), false), Encoder::Nvenc);
|
||||
assert_eq!(Encoder::from_setting(Some("cpu"), true), Encoder::Cpu);
|
||||
assert_eq!(Encoder::from_setting(Some("vaapi"), true), Encoder::Vaapi);
|
||||
assert_eq!(vaapi_filter(None), "format=nv12,hwupload");
|
||||
assert_eq!(vaapi_filter(Some((1280,720))), "scale=1280:720:flags=bilinear,format=nv12,hwupload");
|
||||
let options = vaapi_options(&VideoSettings::default(), "30");
|
||||
for pair in [["-g","30"], ["-bf","0"], ["-async_depth","1"], ["-aud","1"]] {
|
||||
assert!(options.windows(2).any(|w| w[0] == pair[0] && w[1] == pair[1]));
|
||||
}
|
||||
assert!(!options.iter().any(|v| v == "-r" || v == "-pix_fmt"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn encoder_restart_stops_old_producer_before_starting_new_chain() {
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
|
||||
@@ -0,0 +1,181 @@
|
||||
//! Scoped PulseAudio / PipeWire-pulse routing. Never inspect or log audio payloads.
|
||||
//! `pactl` is used when present; plain PipeWire installs without it (default Ubuntu desktop)
|
||||
//! use `pw-cli`/`pw-dump`/`wpctl`, where WirePlumber moves default-following streams itself.
|
||||
use std::process::{Command, Stdio};
|
||||
use std::sync::{Mutex, OnceLock};
|
||||
use std::os::fd::AsRawFd;
|
||||
const SINK: &str = "uu-remote-sink";
|
||||
const MONITOR: &str = "uu-remote-sink.monitor";
|
||||
#[derive(serde::Serialize, serde::Deserialize)]
|
||||
struct Route { previous: String, module: Option<u32>, #[serde(default)] pipewire: bool }
|
||||
struct State { route: Option<Route>, _lock: std::fs::File }
|
||||
static STATE: OnceLock<Mutex<Option<State>>> = OnceLock::new();
|
||||
fn path() -> std::path::PathBuf { crate::config::path().with_file_name("virtual_audio.json") }
|
||||
fn pactl(args: &[&str]) -> Result<String, ()> { run("pactl", args) }
|
||||
fn run(program: &str, args: &[&str]) -> Result<String, ()> {
|
||||
let out = Command::new(program).args(args).env("LC_ALL","C").stdin(Stdio::null()).stderr(Stdio::null()).output().map_err(|_| ())?;
|
||||
if !out.status.success() { return Err(()); }
|
||||
String::from_utf8(out.stdout).map(|s|s.trim().to_owned()).map_err(|_| ())
|
||||
}
|
||||
fn pipewire_only() -> bool { pactl(&["info"]).is_err() && run("wpctl",&["status"]).is_ok() }
|
||||
/// Node ids whose node.name is `name`; `null_sink` restricts to null-audio-sink nodes (ownership).
|
||||
fn pw_nodes(dump: &str, name: &str, null_sink: bool) -> Vec<u32> {
|
||||
let Ok(serde_json::Value::Array(objects)) = serde_json::from_str(dump) else { return Vec::new() };
|
||||
objects.iter().filter_map(|o| {
|
||||
let props = o.get("info")?.get("props")?;
|
||||
if props.get("node.name")?.as_str()? != name { return None; }
|
||||
if null_sink && props.get("factory.name").and_then(|v| v.as_str()) != Some("support.null-audio-sink") { return None; }
|
||||
u32::try_from(o.get("id")?.as_u64()?).ok()
|
||||
}).collect()
|
||||
}
|
||||
fn pw_node(name: &str, null_sink: bool) -> Option<u32> { pw_nodes(&run("pw-dump",&["Node"]).ok()?, name, null_sink).first().copied() }
|
||||
fn default_sink(pipewire: bool) -> Result<String, ()> {
|
||||
if !pipewire { return pactl(&["get-default-sink"]); }
|
||||
run("wpctl",&["inspect","@DEFAULT_AUDIO_SINK@"])?.lines()
|
||||
.find_map(|l| l.trim().trim_start_matches("* ").strip_prefix("node.name = ").map(|v| v.trim_matches('"').to_owned())).ok_or(())
|
||||
}
|
||||
fn set_default(pipewire: bool, name: &str) -> Result<(), ()> {
|
||||
if !pipewire { return pactl(&["set-default-sink",name]).map(|_| ()); }
|
||||
run("wpctl",&["set-default",&pw_node(name,false).ok_or(())?.to_string()]).map(|_| ())
|
||||
}
|
||||
fn owned_modules(list: &str) -> Vec<u32> {
|
||||
list.lines().filter_map(|line| {
|
||||
let mut fields = line.split('\t');
|
||||
let id = fields.next()?.parse().ok()?;
|
||||
if fields.next()? != "module-null-sink" { return None; }
|
||||
fields.next()?.split_whitespace().any(|arg| arg == "sink_name=uu-remote-sink").then_some(id)
|
||||
}).collect()
|
||||
}
|
||||
fn sink_id(name: &str) -> Option<String> {
|
||||
pactl(&["list","short","sinks"]).ok()?.lines().find_map(|line| {
|
||||
let mut f=line.split_whitespace(); let id=f.next()?; (f.next()? == name).then(|| id.to_owned())
|
||||
})
|
||||
}
|
||||
fn move_inputs(from: &str, to: &str) {
|
||||
let Some(id)=sink_id(from) else { return };
|
||||
if let Ok(list)=pactl(&["list","short","sink-inputs"]) {
|
||||
for line in list.lines() {
|
||||
let mut f=line.split_whitespace();
|
||||
if let (Some(input),Some(sink))=(f.next(),f.next()) {
|
||||
if sink==id { let _=pactl(&["move-sink-input",input,to]); }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
fn persist(route: &Route) -> Result<(), ()> {
|
||||
use std::os::unix::fs::OpenOptionsExt;
|
||||
use std::io::Write;
|
||||
let temp=path().with_extension("json.new");
|
||||
let mut file=std::fs::OpenOptions::new().write(true).create(true).truncate(true).mode(0o600).open(&temp).map_err(|_| ())?;
|
||||
file.write_all(&serde_json::to_vec(route).map_err(|_| ())?).map_err(|_| ())?;
|
||||
file.sync_all().map_err(|_| ())?;
|
||||
std::fs::rename(temp,path()).map_err(|_| ())
|
||||
}
|
||||
fn recover(route: &Route) -> Result<(), ()> {
|
||||
// Do not overwrite a default explicitly changed by the user during remote control.
|
||||
// The previous output may be gone (unplugged USB card): still unload our sink so the server
|
||||
// picks another default instead of leaving every stream on a null sink.
|
||||
let pw=route.pipewire;
|
||||
if default_sink(pw).is_ok_and(|s| s == SINK) && set_default(pw,&route.previous).is_err() {
|
||||
log::warn!("Previous default audio output unavailable; the sound server will choose a new default");
|
||||
}
|
||||
if pw {
|
||||
for id in pw_nodes(&run("pw-dump",&["Node"])?,SINK,true) { run("pw-cli",&["destroy",&id.to_string()])?; }
|
||||
} else {
|
||||
move_inputs(SINK,&route.previous);
|
||||
let modules=owned_modules(&pactl(&["list","short","modules"])?);
|
||||
for id in modules { pactl(&["unload-module",&id.to_string()])?; }
|
||||
}
|
||||
let _=std::fs::remove_file(path());
|
||||
Ok(())
|
||||
}
|
||||
fn state() -> &'static Mutex<Option<State>> {
|
||||
STATE.get_or_init(|| {
|
||||
use std::os::unix::fs::OpenOptionsExt;
|
||||
let init=(|| {
|
||||
std::fs::create_dir_all(path().parent().ok_or(())?).map_err(|_| ())?;
|
||||
let lock=std::fs::OpenOptions::new().read(true).write(true).create(true).truncate(false).mode(0o600)
|
||||
.open(path().with_extension("lock")).map_err(|_| ())?;
|
||||
if unsafe { libc::flock(lock.as_raw_fd(),libc::LOCK_EX|libc::LOCK_NB) } != 0 { return Err(()); }
|
||||
let route=if path().exists() {
|
||||
let old:Route=serde_json::from_slice(&std::fs::read(path()).map_err(|_| ())?).map_err(|_| ())?;
|
||||
if recover(&old).is_err() { Some(old) } else { None }
|
||||
} else { None };
|
||||
Ok(State { route, _lock:lock })
|
||||
})();
|
||||
Mutex::new(init.ok())
|
||||
})
|
||||
}
|
||||
/// Called on startup even if mute is disabled, to recover a previous crash.
|
||||
pub fn init() { let _=state(); }
|
||||
pub fn enable() -> bool {
|
||||
let mut guard=state().lock().unwrap();
|
||||
let Some(state)=guard.as_mut() else { return false };
|
||||
if state.route.is_some() { return pactl(&["get-default-sink"]).is_ok_and(|s|s==SINK); }
|
||||
let result=(|| {
|
||||
let pipewire=pipewire_only();
|
||||
let previous=default_sink(pipewire)?;
|
||||
if previous==SINK { return Err(()); }
|
||||
// Refuse to claim another instance's or manually-created sink.
|
||||
if if pipewire { pw_node(SINK,false).is_some() } else { sink_id(SINK).is_some() } { return Err(()); }
|
||||
let mut route=Route { previous, module:None, pipewire };
|
||||
persist(&route)?; // Save before loading: also covers a crash between load and ID persistence.
|
||||
state.route=Some(Route { previous:route.previous.clone(), module:None, pipewire });
|
||||
let id=if pipewire {
|
||||
// object.linger keeps the node after pw-cli exits; recover() destroys it.
|
||||
run("pw-cli",&["create-node","adapter","{ factory.name=support.null-audio-sink node.name=uu-remote-sink \
|
||||
node.description=UU-Remote media.class=Audio/Sink object.linger=true audio.position=[FL FR] }"])?;
|
||||
let mut node=None;
|
||||
for _ in 0..20 { node=pw_node(SINK,true); if node.is_some() { break; } std::thread::sleep(std::time::Duration::from_millis(50)); }
|
||||
node.ok_or(())?
|
||||
} else {
|
||||
pactl(&["load-module","module-null-sink","sink_name=uu-remote-sink","sink_properties=device.description=UU-Remote"])?.parse().map_err(|_| ())?
|
||||
};
|
||||
route.module=Some(id);
|
||||
persist(&route)?;
|
||||
state.route=Some(route);
|
||||
set_default(pipewire,SINK)?;
|
||||
if !pipewire { move_inputs(&state.route.as_ref().ok_or(())?.previous,SINK); }
|
||||
Ok(())
|
||||
})();
|
||||
if result.is_err() {
|
||||
if let Some(route)=state.route.as_ref() { if recover(route).is_ok() { state.route=None; } }
|
||||
log::warn!("Virtual audio routing unavailable; using the existing mute/monitor path");
|
||||
}
|
||||
result.is_ok()
|
||||
}
|
||||
pub fn disable() {
|
||||
let mut guard=state().lock().unwrap();
|
||||
if let Some(state)=guard.as_mut() {
|
||||
if let Some(route)=state.route.as_ref() {
|
||||
if recover(route).is_ok() { state.route=None; }
|
||||
else { log::warn!("Virtual audio restore incomplete; recovery record retained"); }
|
||||
}
|
||||
}
|
||||
}
|
||||
pub fn active() -> bool { state().lock().unwrap().as_ref().is_some_and(|s|s.route.is_some()) }
|
||||
pub fn source() -> String {
|
||||
if state().lock().unwrap().as_ref().is_some_and(|s|s.route.as_ref().is_some_and(|r|r.module.is_some())) { MONITOR.into() }
|
||||
else { std::env::var("UU_AUDIO_SOURCE").unwrap_or_else(|_| "@DEFAULT_MONITOR@".into()) }
|
||||
}
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test] fn recovery_only_claims_exact_null_sink() {
|
||||
let modules="1\tmodule-null-sink\tsink_name=uu-remote-sink\t0\n2\tmodule-null-sink\tsink_name=uu-remote-sink-other\t0\n3\tmodule-other\tsink_name=uu-remote-sink\t0\n";
|
||||
assert_eq!(owned_modules(modules),vec![1]);
|
||||
}
|
||||
#[test] fn pipewire_recovery_only_claims_owned_null_sink() {
|
||||
let dump=r#"[{"id":5,"info":{"props":{"node.name":"uu-remote-sink","factory.name":"support.null-audio-sink"}}},
|
||||
{"id":6,"info":{"props":{"node.name":"uu-remote-sink","factory.name":"api.alsa.pcm.sink"}}},
|
||||
{"id":7,"info":{"props":{"node.name":"other","factory.name":"support.null-audio-sink"}}},{"id":8}]"#;
|
||||
assert_eq!(pw_nodes(dump,"uu-remote-sink",true),vec![5]);
|
||||
assert_eq!(pw_nodes(dump,"uu-remote-sink",false),vec![5,6]);
|
||||
let old:Route=serde_json::from_str(r#"{"previous":"a","module":3}"#).unwrap();
|
||||
assert!(!old.pipewire);
|
||||
}
|
||||
}
|
||||
|
||||
/// Also restore on ordinary returns from the host; SIGKILL is recovered next startup.
|
||||
pub struct Cleanup;
|
||||
impl Drop for Cleanup { fn drop(&mut self) { disable(); } }
|
||||
+27
-1
@@ -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"))]
|
||||
@@ -58,7 +65,8 @@ pub fn capture_stdout() -> Result<(),String> {
|
||||
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 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));
|
||||
loop {
|
||||
let start=std::time::Instant::now();
|
||||
capture(&mut frame)?;
|
||||
@@ -67,6 +75,24 @@ pub fn capture_stdout() -> Result<(),String> {
|
||||
}
|
||||
}
|
||||
|
||||
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());}
|
||||
}
|
||||
}
|
||||
|
||||
/// Use only in an isolated acceptance desktop; emits fixed test events, no logs.
|
||||
pub fn input_probe()->Result<(),String> {
|
||||
let (w,h)=init()?;
|
||||
|
||||
+29
-6
@@ -86,6 +86,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 +144,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,7 +154,10 @@ 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();
|
||||
@@ -198,7 +202,11 @@ impl WebRtcSession {
|
||||
feedback_counts.observe(packet.as_any());
|
||||
if packet.as_any().is::<webrtc::rtcp::payload_feedbacks::picture_loss_indication::PictureLossIndication>()
|
||||
|| packet.as_any().is::<webrtc::rtcp::payload_feedbacks::full_intra_request::FullIntraRequest>() {
|
||||
idr_on_feedback.store(true, std::sync::atomic::Ordering::Release);
|
||||
// Dedupe only while an IDR is pending; the encoder clears the flag when it emits one,
|
||||
// so a lost IDR does not block the next recovery request.
|
||||
if !idr_on_feedback.load(std::sync::atomic::Ordering::Acquire) {
|
||||
idr_on_feedback.store(true, std::sync::atomic::Ordering::Release);
|
||||
}
|
||||
}
|
||||
if let Some(report) = packet.as_any().downcast_ref::<webrtc::rtcp::receiver_report::ReceiverReport>() {
|
||||
reports += 1;
|
||||
@@ -323,6 +331,7 @@ 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();
|
||||
@@ -344,6 +353,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>>();
|
||||
{
|
||||
@@ -434,18 +450,22 @@ impl WebRtcSession {
|
||||
let next_seq = || sequence.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
|
||||
// Default file-transfer folder: the desktop user's home directory.
|
||||
let home = std::env::var("HOME").unwrap_or_else(|_| "/".into());
|
||||
let desktop_permission=if screen.init_error.is_none() {
|
||||
// Tool peers are permitted without a desktop; the port-mapping viewer aborts on Reject.
|
||||
let desktop_permission=if screen.init_error.is_none() || !desktop {
|
||||
crate::system_metrics::Permission::Allow
|
||||
} else { crate::system_metrics::Permission::Reject };
|
||||
// Without a desktop (headless/portal before consent) the port-mapping tool still waits
|
||||
// for ScreenSources/CaptureChange and aborts otherwise: tool peers get a nominal screen.
|
||||
let screen_size=if screen.init_error.is_none() { (screen.width, screen.height) } else { (1920, 1080) };
|
||||
let messages = [
|
||||
(8, crate::initialization::encode_capture_change(next_seq(), timestamp, 0, 0, "Linux desktop")),
|
||||
(13, Ok(crate::initialization::envelope(next_seq(), timestamp, 13, &init_actions.metrics_payload(desktop_sound, desktop_permission)))),
|
||||
(26, Ok(crate::initialization::envelope(next_seq(), timestamp, 26,
|
||||
&crate::system_metrics::remote_download_path(&home)))),
|
||||
(7, crate::capture_setting::screen_sources(next_seq(), timestamp, (screen.width, screen.height), init_video.borrow().output, "Linux desktop")),
|
||||
(7, crate::capture_setting::screen_sources(next_seq(), timestamp, screen_size, init_video.borrow().output, "Linux desktop")),
|
||||
];
|
||||
for (field, message) in messages {
|
||||
if screen.init_error.is_some() && matches!(field,7|8) { continue; }
|
||||
if desktop && screen.init_error.is_some() && matches!(field,7|8) { continue; }
|
||||
let message = match message {
|
||||
Ok(message) => message,
|
||||
Err(e) => { log::error!("CONTROL initialization encode failed: {}", e); return; }
|
||||
@@ -490,6 +510,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();
|
||||
@@ -752,6 +773,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),
|
||||
@@ -762,6 +784,7 @@ impl WebRtcSession {
|
||||
ControlInput::MouseClick {button} => {input_worker.wire_mouse_button(*button,true);input_worker.wire_mouse_button(*button,false);},
|
||||
_ => {}
|
||||
}
|
||||
});}
|
||||
|
||||
},
|
||||
_ => {}
|
||||
|
||||
+15
-1
@@ -6,6 +6,17 @@ final class MarkerCheck: NSObject {
|
||||
let kde = NSTextField(labelWithString: "KDE → Mac: not checked")
|
||||
let copied = NSTextField(labelWithString: "Mac marker: not copied")
|
||||
let formats = NSTextField(labelWithString: "Known text formats: not checked")
|
||||
// The official viewer drops remote format offers before ACK unless its
|
||||
// remote window is key (viewer 0x100380f20 -> isKeyWindow). A delayed
|
||||
// marker-only comparison lets it stay key throughout negotiation.
|
||||
@objc func armGnome() {
|
||||
gnome.stringValue = "GNOME → Mac: armed; return to UU and copy VM marker"
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + 15) { self.compareGnome() }
|
||||
}
|
||||
@objc func armKde() {
|
||||
kde.stringValue = "KDE → Mac: armed; return to UU and copy VM marker"
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + 15) { self.compareKde() }
|
||||
}
|
||||
@objc func copyMarker() {
|
||||
NSPasteboard.general.clearContents()
|
||||
let ok = NSPasteboard.general.setString("UU-Mac-to-VM 中文🙂", forType: .string)
|
||||
@@ -32,14 +43,17 @@ final class MarkerCheck: NSObject {
|
||||
NSButton(title: title, target: self, action: action)
|
||||
}
|
||||
func show() {
|
||||
let window = NSWindow(contentRect: NSRect(x: 0,y: 0,width: 500,height: 360), styleMask: [.titled,.closable], backing: .buffered, defer: false)
|
||||
let window = NSWindow(contentRect: NSRect(x: 0,y: 0,width: 620,height: 500), styleMask: [.titled,.closable], backing: .buffered, defer: false)
|
||||
window.title = "UU synthetic clipboard acceptance"
|
||||
window.isReleasedWhenClosed = false
|
||||
let stack = NSStackView(views: [
|
||||
NSTextField(labelWithString: "Only fixed test-marker matches are displayed."),
|
||||
NSTextField(labelWithString: "Keep the UU remote window key until format ACK / text request."),
|
||||
button("Copy Mac test marker", #selector(copyMarker)), copied,
|
||||
button("Compare GNOME VM marker", #selector(compareGnome)), gnome,
|
||||
button("Compare KDE VM marker", #selector(compareKde)), kde, formats,
|
||||
button("Arm GNOME comparison in 15 seconds", #selector(armGnome)),
|
||||
button("Arm KDE comparison in 15 seconds", #selector(armKde)),
|
||||
button("Quit test fixture", #selector(quit))])
|
||||
stack.orientation = .vertical; stack.spacing = 14
|
||||
stack.translatesAutoresizingMaskIntoConstraints = false
|
||||
|
||||
Vendored
+7
-3
@@ -1,5 +1,5 @@
|
||||
"""Hold an ephemeral idle inhibition only in the isolated QEMU acceptance VM."""
|
||||
import pathlib,subprocess,signal,threading,sys
|
||||
import pathlib,subprocess,signal,threading,sys,os
|
||||
assert pathlib.Path('/proc/sys/kernel/hostname').read_text().strip()=='uu-hyprland-test'
|
||||
assert subprocess.check_output(['systemd-detect-virt'],text=True).strip()=='qemu'
|
||||
import gi
|
||||
@@ -7,7 +7,11 @@ gi.require_version('Gio','2.0')
|
||||
from gi.repository import Gio,GLib
|
||||
seconds=min(4200,max(1,int(sys.argv[1]) if len(sys.argv)>1 else 3600))
|
||||
bus=Gio.bus_get_sync(Gio.BusType.SESSION,None)
|
||||
reply=bus.call_sync('org.freedesktop.ScreenSaver','/ScreenSaver','org.freedesktop.ScreenSaver','Inhibit',GLib.Variant('(ss)',('UU isolated acceptance','Timed live-connection acceptance')),GLib.VariantType.new('(u)'),Gio.DBusCallFlags.NONE,3000,None)
|
||||
gnome='GNOME' in os.environ.get('XDG_CURRENT_DESKTOP','').upper().split(':')
|
||||
destination='org.gnome.SessionManager' if gnome else 'org.freedesktop.ScreenSaver'
|
||||
object_path='/org/gnome/SessionManager' if gnome else '/ScreenSaver'
|
||||
parameters=GLib.Variant('(susu)',('UU isolated acceptance',0,'Timed live-connection acceptance',8)) if gnome else GLib.Variant('(ss)',('UU isolated acceptance','Timed live-connection acceptance'))
|
||||
reply=bus.call_sync(destination,object_path,destination,'Inhibit',parameters,GLib.VariantType.new('(u)'),Gio.DBusCallFlags.NONE,3000,None)
|
||||
cookie=reply.unpack()[0]
|
||||
stop=threading.Event()
|
||||
signal.signal(signal.SIGTERM,lambda *_:stop.set())
|
||||
@@ -16,6 +20,6 @@ print('VM timed idle inhibition acquired',flush=True)
|
||||
try:stop.wait(seconds)
|
||||
finally:
|
||||
try:
|
||||
bus.call_sync('org.freedesktop.ScreenSaver','/ScreenSaver','org.freedesktop.ScreenSaver','UnInhibit',GLib.Variant('(u)',(cookie,)),None,Gio.DBusCallFlags.NONE,3000,None)
|
||||
bus.call_sync(destination,object_path,destination,'Uninhibit' if gnome else 'UnInhibit',GLib.Variant('(u)',(cookie,)),None,Gio.DBusCallFlags.NONE,3000,None)
|
||||
print('VM timed idle inhibition released',flush=True)
|
||||
except GLib.Error:print('VM idle inhibition connection closed',flush=True)
|
||||
Reference in new issue
Block a user