From 21e9e690aec7e22b91f4cfa6b54ab1758a09b91c Mon Sep 17 00:00:00 2001 From: "Mr. Error" Date: Mon, 9 Nov 2026 08:36:37 +0800 Subject: [PATCH] feat(portal): follow the viewer's mouse mode with the embedded cursor (#203) * feat(portal): follow the viewer's mouse mode with the embedded cursor The portal stream hides the compositor cursor. A viewer that drives the pointer with relative motion (UU's controlled-end mouse, also the Mac trackpad mode) has no absolute position to draw its own pointer at, and the game's cursor never reaches the picture, so the mouse can be moved and clicked but not seen. Request cursor_mode=embedded while relative motion arrives, and keep it hidden while absolute motion arrives: there the viewer draws its own pointer and an embedded copy would trail it. The portal reads cursor_mode only when the session is created, so switch by rebuilding the session with the saved consent, the same path the privacy screen uses. A run has to last five events and 200 ms before it counts, so a burst at connect or an alternating controller does not rebuild; a failed rebuild backs off for five seconds. The rebuilt session is checked against the monitor the previous one captured, so a restored consent cannot silently move the share. The engine keeps the mode for later opens (recovery, privacy, monitor switches) and skips the switch when an older libuu-portal.so lacks uu_portal_set_cursor_mode. UU_PORTAL_CURSOR_MODE=embedded sets the startup default. * fix(portal): keep the cursor rebuild off locks and held input Do not rebuild while the desktop is locked, and cancel an open that is already waiting when it locks: GNOME closes the portal session on lock, so the request would wait for consent that cannot be answered while holding `recovery` against the post-unlock recovery. Keys and buttons held through the portal input path are dropped when the session is replaced, so wait for their release. Monitor selection takes the same `recovery` lock, so it cannot install a stale source over the cursor rebuild or the other way round, and the rebuilt session is revalidated against the source it started from. * fix(portal): carry the clipboard across cursor rebuilds The engine keeps the viewer's text only in the session that provided it, and the host re-sends it only for a new viewer update. A replacement session therefore had no clipboard ownership, so changing the mouse mode made previously synchronized text unpasteable until the viewer sent it again. Offer the text to the replacement before the old session closes, but only while that session still owns the selection: a local copy that has taken the clipboard since is left alone. Touch consent upgrades replace the handle too, so they take the same `recovery` lock as recovery, monitor selection and the cursor rebuild and cannot install a handle opened under an older cursor mode. * fix(portal): serialize clipboard writes with the cursor swap The clipboard bridge calls clipboard_set from its own thread. Installing the replacement handle and then carrying the old text over left a window where an update could land on the new session and be overwritten by the older text, with no pending update left to restore it. Take a clipboard write lock before the handle is installed and hold it until the carry-over finishes; clipboard_set takes the same lock. A write that starts before the swap is included in the text carried over, a write that starts after waits and wins. * fix(portal): cancel a waiting rebuild when the desktop ends The desktop ends its share when the last controller leaves (end_idle), but an open that is still waiting for consent is not the installed handle, so it survived that shutdown and installed a live share afterwards. Bump an end generation in Session::end(), snapshot it before the open, and cancel the request when it moves. The generation is checked again under the handle lock before installing, so an end that races the swap either aborts the rebuild or ends the handle it just installed. * fix(portal): keep session swaps consistent with the viewer's state Addresses the ownership race reported on the previous commit and the paths that share its failure mode. uu_portal_clipboard_set publishes the new text before SetSelection so a transfer during the call serves it, restores the old text when the call fails, and ignores a stale revision so a newer owner wins. The engine reports the cursor mode it actually requested and the modes the backend advertised; the host follows that instead of assuming the request was applied and stops retrying where an embedded cursor is unavailable. The end generation is sampled before the rebuild queues for `recovery`, a rebuild refuses to start while the session is no longer alive, and cancellation now covers virtual output opens. Portal-bound keys and buttons are tracked separately, so an input held through the portal defers the swap on every routing path, and the check is repeated after the open. Every handle replacement (privacy screen, monitor switch, touch consent, recovery after lock or stream loss) carries the viewer's clipboard under the clipboard write lock, and the rebuilt session is checked against the captured geometry. * fix(portal): stop the swap bookkeeping from outliving a deferred rebuild open_inner publishes the replacement's cursor mode before the swap. When held input defers the rebuild, that session is never installed, so the host belief described the discarded handle and later motion looked like it already matched. Restore the belief from the installed session on every abort path. The extra portal releases in release_held now stop at the first failure like the sweep above; with a dead portal bus each remaining input waited out the three-second timeout while the input and desktop-owner locks were held. * fix(portal): invalidate clipboard ownership when a session ends A closed session stops receiving owner changes, so a cached clipboard_owns survived into the recovery paths and could republish old viewer text over a newer local copy. Clear the claim in uu_portal_end and the Closed handler, and let uu_portal_clipboard_copy refuse a source that is no longer alive. Mark the embedded cursor unavailable when the engine did not apply it: with AvailableCursorModes unreadable the session opens hidden, and retrying on every motion run rebuilt it every 200 ms without changing the mode. * fix(portal): retire held portal input when the devices are replaced A key or button held through the native portal path is recorded when its injection succeeds. If the session dies first, the key-up fails and the record survives; recovery then installs new devices while the tracker still says an input is held, so pointer-mode detection keeps deferring. Keep the tracker in module state and clear it whenever the portal replaces or ends its devices: the rebuild, both recovery paths and Session::end. * fix(portal): scope held-input retirement to the retired handle A press delivered to the replacement after the swap could be recorded while the old handle was still being retired, and the unconditional reset then deleted it. Hold the recording lock from before the swap until the reset finishes, so only inputs sent to the retired devices are dropped. A portal touchscreen contact was not tracked at all: the swap retired the active contact while TouchDrag stayed down, so the following move reached a session that never saw the press. Track contact down/up and include it in the deferral state. * fix(portal): serialize touch injection with handle replacement inject_touch wrote the contact state outside the recording lock the handle swap holds, so a contact delivered to the replacement between the swap and the reset could be cleared, and the following phase-1 would fail on new devices that never saw the press. Take the same lock across the injection and the state update. * fix(portal): decide the swap under the held-input lock The final held-input check ran before install_replacement() took the serial lock, so an input could be injected and recorded against the old handle while the install waited for the clipboard lock. The install then retired the records and swapped, destroying a contact the new devices never saw. Repeat the check under the serial lock that recording uses, and defer the rebuild when it hits. * fix(portal): hold the input lock across native injection, refresh privacy geometry inject_button and inject_key injected through the portal and only took the recording lock afterwards, so a swap could take the lock, see no held input and replace the devices that had just received the press. Take the serial lock across the injection and its record, as the touch path does; the record helpers no longer take it themselves. A cursor rebuild that replaces the privacy virtual output invalidates the cached pointer region: recompute it from the settled layout so absolute input keeps landing inside the captured share. --- AGENTS.md | 1 + README.en.md | 1 + README.md | 1 + c_engine/portal_clipboard.inc | 39 ++- c_engine/portal_engine.c | 42 +++- c_engine/portal_engine.h | 9 + src/input.rs | 83 ++++++- src/portal.rs | 431 ++++++++++++++++++++++++++++++++-- src/privacy_screen/backend.rs | 3 + src/privacy_screen/kde/mod.rs | 8 + src/privacy_screen/mod.rs | 12 + 11 files changed, 592 insertions(+), 38 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index d284217..53b9fff 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -127,6 +127,7 @@ viewer does not reconnect them automatically. | `UU_NVENC_RGB=0` / `=1` | NVENC 4:2:0 without scaling takes packed BGRA/BGR0 directly (no CPU swscale). Default: on, except when a connected DRM connector belongs to an nvidia-bound card (desktop composited on the encoding GPU; #16 measured 82.6 vs 98 fps at 4K). `0` forces CPU conversion, `1` forces the direct path | | `UU_TOUCH_INPUT` | Android/iOS controllers (connect header `controller_platform` 2/3) send a finger as a left-button drag at absolute positions. Default on the portal backend (gesture): per press, moving past 12 px within 450 ms scrolls (`NotifyPointerAxis` / EIS scroll, content follows the finger, `finish` on release), holding still selects with button 1 down, a short press clicks; needs no touchscreen consent. `touch`: replay as one finger on the portal touchscreen (`NotifyTouch*` / EIS touch; requested where `AvailableDeviceTypes` offers it; consents saved before touch support restore keyboard+pointer only, so the first mobile drag asks once per run in the background via `PortalSession::upgrade_touch`, swapped in only with the touchscreen granted for the same monitor; the saved consent is never removed first). `0`: plain mouse drags | | `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_PORTAL_CURSOR_MODE` | portal capture startup cursor mode: `embedded` draws the compositor pointer into the stream, default hidden. `input::inject_move` reports relative/absolute motion; `portal::note_pointer_motion` flips the mode after a 200 ms run of one kind and rebuilds the session via `Session::set_cursor_embedded`, because the portal only reads `cursor_mode` in `SelectSources`. Relative motion gives the viewer no position to draw its own pointer at, so the stream must carry one; absolute motion means the viewer draws its own and an embedded copy would trail it | | Mac viewer Cmd | With 「使用 macOS 快捷键」 off the Mac viewer sends Cmd=VK_LWIN 0x5B, Option=0xA4, Ctrl=0xA2; connect header `controller_platform`=4 is macOS. For Mac controllers Cmd is held back: Cmd+C/V/A/F/X/Z → Ctrl+key (terminal: Cmd+C/V → Ctrl+Shift+C/V), other combos/alone → Super | | `UU_TERMINAL_SHORTCUTS=0` | disable Ctrl+V → Ctrl+Shift+V in focused X11 terminals (Mac Cmd and Ctrl both arrive as VK_LCONTROL) | | `contrib/virtual-desktop/` | Xvfb + session (`UU_VIRTUAL_SESSION`, `UU_VIRTUAL_BUS=user|private`) for monitorless hosts; serve it with `DISPLAY=:99` | diff --git a/README.en.md b/README.en.md index b23a191..4f5027d 100644 --- a/README.en.md +++ b/README.en.md @@ -394,6 +394,7 @@ Common environment variables: | `UU_X11_OUTPUT` | Initial X11 monitor name (`xrandr --listmonitors`); default: the primary | | `UU_WAYLAND_NO_VIRTUAL_KEYBOARD=1` | Native Wayland skips the virtual keyboard (e.g. Denial reserves it for its input method); keys go through uinput. Without wlr-virtual-pointer the pointer also uses uinput (#72) | | `UU_INPUT_BACKEND` | `auto` (default: uinput on Wayland when accessible) / `uinput` / `native` | +| `UU_PORTAL_CURSOR_MODE` | Startup cursor mode: `embedded` draws the compositor pointer into the stream; hidden by default. The host switches at runtime as the viewer sends relative or absolute motion. A switch rebuilds the portal session and interrupts picture and input for a moment | | `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 or `auto`: a background probe (one synthetic IDR per candidate, 3 s each). With an NVIDIA driver the order is NVENC then CPU; without one the only candidate is CPU and the probe is skipped. A session that starts before the probe finishes uses the previous rule (NVENC if NVIDIA is present, else CPU). On a self-built `--features inproc-av` binary, a process-only failure of NVENC when the in-process engine would try it first on X11 is logged `native_unprobed` and keeps the previous NVENC choice (Wayland, portal, and KMS fall back to CPU). The skipped CPU-only probe is logged `not probed (only candidate)`. A successful probe and that CPU-only skip stay cached while NVIDIA presence matches the report. A later desktop connection whose presence differs starts one background re-probe and still uses the cached encoder until the new result is stored. A hardware-candidate failure (NVENC failed, CPU chosen) is kept for 10 minutes; the next desktop connection after that starts the same re-probe and still uses the cached encoder until the new result is stored. Explicit `cpu` / `vaapi` / `nvenc` does not start the probe. Another failure restarts the 10 minutes. `native_unprobed` is not a temporary failure. A non-`auto` `video_encoder` in `host_settings.json` wins. VAAPI stays out of auto until #33 | | `UU_VAAPI_BUFFER_MS` | VAAPI VBV buffer in ms (10-1000, VAAPI only); raise it when keyframes are squeezed into a blur at high resolution | diff --git a/README.md b/README.md index 52560ff..5210b39 100644 --- a/README.md +++ b/README.md @@ -357,6 +357,7 @@ systemctl --user restart uuremote-native.service | `UU_X11_OUTPUT` | X11 默认采集的显示器名称(`xrandr --listmonitors`),默认主显示器 | | `UU_WAYLAND_NO_VIRTUAL_KEYBOARD=1` | 原生 Wayland 不创建虚拟键盘(如 Denial 只把它给自己的输入法),键盘改走 uinput。合成器没有 wlr-virtual-pointer 时鼠标也自动改走 uinput(#72) | | `UU_INPUT_BACKEND` | `auto`(默认,Wayland 下可用时用 uinput)/ `uinput` / `native` | +| `UU_PORTAL_CURSOR_MODE` | 启动时的光标模式:`embedded` 让合成器把指针画进抓屏流,默认隐藏。运行中按客户端发来的相对/绝对位移自动切换;每次切换重建一次 portal 会话,画面和输入短暂中断 | | `UU_TERMINAL_SHORTCUTS=0` | 关闭终端窗口里的 Ctrl+V 粘贴转换 | | `UU_VIDEO_ENCODER` | `cpu` 强制 CPU;`vaapi` 显式启用 Intel/AMD 硬编;`nvenc` 强制 NVENC;未设置或 `auto` 时后台探测(每个候选编码一帧,限时 3 秒)。有 NVIDIA 驱动时顺序是 NVENC → CPU;没有 NVIDIA 时只有 CPU,不探测。探测未完成就建流则沿用旧规则(有 NVIDIA 用 NVENC,否则 CPU)。自行编译的 `--features inproc-av` 上,仅 X11 会先走进程内引擎:外部 FFmpeg 探测失败时记为 `native_unprobed`,沿用旧规则(NVENC);Wayland、portal、KMS 回退 CPU。只有 CPU 时跳过探测,记为 `not probed (only candidate)`。探测成功和这种只含 CPU 的跳过会缓存到 NVIDIA 是否存在发生变化为止(`/proc/driver/nvidia/version` 或 `/dev/nvidia0`)。之后的桌面连接在后台再探测一次(不挡住这一路,仍用缓存的编码器)。NVENC 探测失败、选用 CPU 时只缓存 10 分钟;到期后下一次桌面连接同样在后台再探测一次,再次失败则重新计 10 分钟。显式 `cpu` / `vaapi` / `nvenc` 不启动探测。`native_unprobed` 不算失败缓存。优先级低于 `host_settings.json` 里非 `auto` 的 `video_encoder`。VAAPI 要等 #33 解决后才会进入 auto | | `UU_VAAPI_BUFFER_MS` | VAAPI 的 VBV 缓冲(10–1000 ms,仅 `UU_VIDEO_ENCODER=vaapi` 时生效);高分辨率下关键帧被压糊时用它换稳定画质 | diff --git a/c_engine/portal_clipboard.inc b/c_engine/portal_clipboard.inc index 4b2b852..32693ea 100644 --- a/c_engine/portal_clipboard.inc +++ b/c_engine/portal_clipboard.inc @@ -67,6 +67,7 @@ static void selection_owner(GDBusConnection *bus,const gchar *sender,const gchar pthread_mutex_lock(&p->lock); g_free(p->clipboard_mime); p->clipboard_mime=selected; p->clipboard_changed=!owner && selected!=NULL; p->clipboard_revision++; + p->clipboard_owns=owner?1:0; pthread_mutex_unlock(&p->lock); if (types) g_variant_unref(types); } @@ -100,16 +101,46 @@ int uu_portal_clipboard_set(struct uu_portal *p,const uint8_t *text,size_t lengt if (!uu_portal_clipboard_ready(p) || length>CLIP_LIMIT || !g_utf8_validate((const char *)text,(gssize)length,NULL)) return -1; char *copy=g_malloc(length+1); memcpy(copy,text,length); copy[length]=0; pthread_mutex_lock(&p->lock); - g_free(p->clipboard_text); p->clipboard_text=copy; p->clipboard_length=length; - p->clipboard_changed=0; + /* Serve the new text while the portal announces it, and restore the old one on failure. */ + char *old_text=p->clipboard_text; size_t old_length=p->clipboard_length; + uint64_t revision=p->clipboard_revision; + p->clipboard_text=copy; p->clipboard_length=length; p->clipboard_changed=0; pthread_mutex_unlock(&p->lock); const char *types[]={"text/plain;charset=utf-8","text/plain",NULL}; GVariantBuilder options; g_variant_builder_init(&options,G_VARIANT_TYPE_VARDICT); g_variant_builder_add(&options,"{sv}","mime_types",g_variant_new_strv(types,-1)); GVariant *reply=g_dbus_connection_call_sync(p->bus,DEST,PATH,CLIPBOARD,"SetSelection", g_variant_new("(o@a{sv})",p->session,g_variant_builder_end(&options)),NULL,G_DBUS_CALL_FLAGS_NONE,2000,NULL,NULL); - if (!reply) return -1; - g_variant_unref(reply); return 0; + if (!reply) { + pthread_mutex_lock(&p->lock); + g_free(p->clipboard_text); + p->clipboard_text=old_text; p->clipboard_length=old_length; + pthread_mutex_unlock(&p->lock); + return -1; + } + g_variant_unref(reply); + g_free(old_text); + pthread_mutex_lock(&p->lock); + /* Another owner may have been reported meanwhile; its newer state must win. */ + if (p->clipboard_revision==revision) p->clipboard_owns=1; + pthread_mutex_unlock(&p->lock); + return 0; +} +/* Hand the text this session is providing to a replacement session, so a session swap (cursor + * mode, privacy screen, monitor switch) does not drop the viewer's clipboard. Transfers only + * while this session still owns the selection. Returns 0 transferred, 1 nothing to transfer. */ +int uu_portal_clipboard_copy(struct uu_portal *from, struct uu_portal *to) { + if (!from || !to) return -1; + /* A session that ended cannot know whether a newer owner took the selection since. */ + if (!uu_portal_alive(from)) return 1; + pthread_mutex_lock(&from->lock); + char *copy=(from->clipboard_owns && from->clipboard_text)?g_memdup2(from->clipboard_text,from->clipboard_length+1):NULL; + size_t length=from->clipboard_length; + pthread_mutex_unlock(&from->lock); + if (!copy) return 1; + int result=uu_portal_clipboard_set(to,(const uint8_t *)copy,length); + g_free(copy); + return result==0?0:-1; } int uu_portal_clipboard_poll(struct uu_portal *p,uint8_t *out,size_t capacity) { if (!out || !uu_portal_clipboard_ready(p) || capacitycursor_applied : 0; } +/* Cursor modes the backend advertised for the last session; 0 when the property was unreadable. */ +int uu_portal_cursor_modes(struct uu_portal *p) { return p ? p->cursor_available : 0; } static void initialize_pipewire(void) { pw_init(NULL, NULL); } static GVariant *empty_options(void) { return g_variant_new_array(G_VARIANT_TYPE("{sv}"), NULL, 0); } static int open_cancelled(struct uu_portal *p) { @@ -89,10 +97,10 @@ static int open_cancelled(struct uu_portal *p) { } static int wait_for_first_frame(struct uu_portal *p); #include "portal_eis.inc" -/* A uint32 RemoteDesktop property (version, AvailableDeviceTypes); 0 when unreadable. */ -static guint remote_property(struct uu_portal *p, const char *name) { +/* A uint32 portal property; 0 when unreadable. */ +static guint interface_property(struct uu_portal *p, const char *interface, const char *name) { GVariant *reply = g_dbus_connection_call_sync(p->bus, DEST, PATH, - "org.freedesktop.DBus.Properties", "Get", g_variant_new("(ss)", REMOTE, name), + "org.freedesktop.DBus.Properties", "Get", g_variant_new("(ss)", interface, name), G_VARIANT_TYPE("(v)"), G_DBUS_CALL_FLAGS_NONE, 3000, NULL, NULL); guint result = 0; if (reply) { @@ -102,6 +110,10 @@ static guint remote_property(struct uu_portal *p, const char *name) { } return result; } +/* A uint32 RemoteDesktop property (version, AvailableDeviceTypes). */ +static guint remote_property(struct uu_portal *p, const char *name) { + return interface_property(p, REMOTE, name); +} static void fail(struct uu_portal *p, int error) { pthread_mutex_lock(&p->lock); if (!p->error) p->error = error; @@ -179,6 +191,8 @@ static void session_closed(GDBusConnection *bus, const gchar *sender, const gcha const gchar *interface, const gchar *signal, GVariant *parameters, gpointer data) { (void)bus; (void)sender; (void)path; (void)interface; (void)signal; (void)parameters; struct uu_portal *p = data; + /* A closed session no longer tracks owner changes, so its cached claim is stale. */ + pthread_mutex_lock(&p->lock); p->clipboard_owns=0; pthread_mutex_unlock(&p->lock); atomic_store(&p->closed, 1); fail(p, 9); } static void *run_dbus(void *data) { @@ -658,7 +672,23 @@ static struct uu_portal *open_persist(const char *restore, void (*save_token)(co /* 1 monitor; 4 virtual (KDE creates a KWin virtual output for the stream). */ g_variant_builder_add(&options, "{sv}", "types", g_variant_new_uint32(source_types)); g_variant_builder_add(&options, "{sv}", "multiple", g_variant_new_boolean(FALSE)); - /* Hidden cursor avoids duplicate pointer; supported since ScreenCast v1. */ + /* Hidden cursor avoids a duplicate pointer in absolute mode: the viewer draws its own where + * it last sent motion. Relative mode has no such position, so the game's cursor has to come + * from the compositor. The host flips this at runtime through uu_portal_set_cursor_mode(); + * UU_PORTAL_CURSOR_MODE=embedded is the startup default. Only sent when the backend + * advertises embedded, since an unsupported value closes the session. */ + int cursor_mode = atomic_load(&cursor_mode_override); + if (cursor_mode < 0) { + const char *wanted = g_getenv("UU_PORTAL_CURSOR_MODE"); + cursor_mode = (wanted && !g_ascii_strcasecmp(wanted, "embedded")) ? 2 : 1; + } + /* The host reads these back so its belief matches what the session actually requested. */ + p->cursor_available = (int)interface_property(p, SCREEN, "AvailableCursorModes"); + p->cursor_applied = 1; + if (cursor_mode == 2 && (p->cursor_available & 2)) { + g_variant_builder_add(&options, "{sv}", "cursor_mode", g_variant_new_uint32(2)); + p->cursor_applied = 2; + } results = request(p, SCREEN, "SelectSources", &options, 1, error); if (!results) goto done; g_variant_unref(results); @@ -1032,6 +1062,8 @@ int uu_portal_monitors(char *out, size_t length) { * captures then report a closed stream until the handle is replaced. */ void uu_portal_end(struct uu_portal *p) { if (!p || atomic_exchange(&p->closed, 1)) return; + /* A closed session no longer tracks owner changes, so its cached claim is stale. */ + pthread_mutex_lock(&p->lock); p->clipboard_owns=0; pthread_mutex_unlock(&p->lock); if (p->bus && p->session) { GVariant *reply = g_dbus_connection_call_sync(p->bus, DEST, p->session, SESSION, "Close", NULL, NULL, G_DBUS_CALL_FLAGS_NONE, 3000, NULL, NULL); diff --git a/c_engine/portal_engine.h b/c_engine/portal_engine.h index 186e760..1b775e0 100644 --- a/c_engine/portal_engine.h +++ b/c_engine/portal_engine.h @@ -18,10 +18,19 @@ struct uu_portal *uu_portal_open_persist_cancellable(const char *restore, void ( /* Select the capture transport before connecting, including on same-process recovery. */ struct uu_portal *uu_portal_open_persist_capture(const char *restore, void (*save_token)(const char *, void *), void *save_data, uu_portal_cancelled_fn cancelled, void *cancel_data, int prefer_dmabuf, int *error, uint32_t *width, uint32_t *height); struct uu_portal *uu_portal_open_restore(const char *restore, int *error, uint32_t *width, uint32_t *height); +/* Cursor mode for subsequent sessions: 1 hidden, 2 embedded (the compositor draws the pointer + * into the stream). -1 restores the UU_PORTAL_CURSOR_MODE default. The host switches this at + * runtime because the viewer cannot draw a pointer of its own during relative motion. */ +void uu_portal_set_cursor_mode(int mode); +/* What the last session requested and what the backend offered, for host bookkeeping. */ +int uu_portal_cursor_mode(struct uu_portal *portal); +int uu_portal_cursor_modes(struct uu_portal *portal); const char *uu_portal_restore_token(struct uu_portal *portal); void uu_portal_close(struct uu_portal *portal); int uu_portal_clipboard_ready(struct uu_portal *portal); int uu_portal_clipboard_set(struct uu_portal *portal, const uint8_t *text, size_t length); +/* Offers the text `from` still owns to `to`; 0 transferred, 1 nothing to transfer. */ +int uu_portal_clipboard_copy(struct uu_portal *from, struct uu_portal *to); int uu_portal_clipboard_poll(struct uu_portal *portal, uint8_t *text, size_t capacity); int uu_portal_alive(struct uu_portal *portal); int uu_portal_capture_alive(struct uu_portal *portal); diff --git a/src/input.rs b/src/input.rs index cf3da09..9e7175f 100644 --- a/src/input.rs +++ b/src/input.rs @@ -43,6 +43,28 @@ 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, keys: std::collections::BTreeSet } +/// Inputs held through the portal session are dropped when that session is replaced; the +/// cursor-mode rebuild waits until they are released. uinput devices survive the swap. +static PORTAL_INPUT_HELD: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false); +/// An active contact on the portal touchscreen is also lost when the session is replaced. +static PORTAL_TOUCH_HELD: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false); +pub fn portal_input_held() -> bool { + PORTAL_INPUT_HELD.load(std::sync::atomic::Ordering::Relaxed) || PORTAL_TOUCH_HELD.load(std::sync::atomic::Ordering::Relaxed) +} +/// Inputs currently bound to the portal session; a replacement tears those devices down. +static PORTAL_HELD: std::sync::Mutex = std::sync::Mutex::new(HeldInput { buttons: std::collections::BTreeSet::new(), keys: std::collections::BTreeSet::new() }); +/// Serializes recording an input with retiring the records of a replaced handle. +static PORTAL_HELD_SERIAL: std::sync::Mutex<()> = std::sync::Mutex::new(()); +pub fn lock_portal_held() -> std::sync::MutexGuard<'static, ()> { + PORTAL_HELD_SERIAL.lock().unwrap_or_else(std::sync::PoisonError::into_inner) +} +/// The portal replaced its native devices: records of held portal inputs no longer apply. +/// The caller holds `lock_portal_held()`, so an input recorded against the new handle stays. +pub fn portal_session_replaced() { + if let Ok(mut held)=PORTAL_HELD.lock() { *held=HeldInput::default(); } + PORTAL_INPUT_HELD.store(false, std::sync::atomic::Ordering::Relaxed); + PORTAL_TOUCH_HELD.store(false, std::sync::atomic::Ordering::Relaxed); +} /// 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 } @@ -388,8 +410,20 @@ impl InputInjector { }); } fn inject_touch(&self, phase: i32, x: i32, y: i32) -> i32 { + // Serialize the contact with handle replacement: a contact sent to the new devices let Some(portal) = self.portal.lock().unwrap().clone() else { return -1 }; - self.portal_result("touch", portal.touch(phase, f64::from(x), f64::from(y))) + // Serialize with handle replacement while the contact is sent; portal then recording + // lock, the same order the button and key recorders use. + let _serial = lock_portal_held(); + let result = self.portal_result("touch", portal.touch(phase, f64::from(x), f64::from(y))); + if result == 0 { + match phase { + 0 => PORTAL_TOUCH_HELD.store(true, std::sync::atomic::Ordering::Relaxed), + 2 => PORTAL_TOUCH_HELD.store(false, std::sync::atomic::Ordering::Relaxed), + _ => {} + } + } + result } fn terminal_focused(&self) -> bool { @@ -504,6 +538,16 @@ impl InputInjector { if !keys_ok {log::warn!("Desktop input reset incomplete: backend unavailable");} } *held=HeldInput::default(); + // Inputs bound to the portal session are not covered by the uinput release above. + let (portal_buttons, portal_keys) = { + let _serial=lock_portal_held(); + let mut pending=PORTAL_HELD.lock().unwrap(); + (std::mem::take(&mut pending.buttons), std::mem::take(&mut pending.keys)) + }; + // Stop on the first failure like the sweep above: each call can wait out the portal timeout. + let buttons_ok = portal_buttons.into_iter().all(|button| self.inject_button(button,false)==0); + if buttons_ok { for key in portal_keys { if self.inject_key(key,false)!=0 { break; } } } + PORTAL_INPUT_HELD.store(false,std::sync::atomic::Ordering::Relaxed); *self.shortcuts.lock().unwrap()=Shortcuts::default(); *self.cmd.lock().unwrap()=CmdState::default(); } @@ -537,7 +581,22 @@ impl InputInjector { } } } + /// Inputs bound to the portal session are dropped when that session is replaced. + fn record_portal_button(&self, button: i32, pressed: bool) { + // Callers hold the serial lock across the injection they are recording. + let mut held = PORTAL_HELD.lock().unwrap(); + if pressed { held.buttons.insert(button); } else { held.buttons.remove(&button); } + PORTAL_INPUT_HELD.store(!held.buttons.is_empty() || !held.keys.is_empty(), std::sync::atomic::Ordering::Relaxed); + } + fn record_portal_key(&self, symbol: u64, pressed: bool) { + let mut held = PORTAL_HELD.lock().unwrap(); + if pressed { held.keys.insert(symbol); } else { held.keys.remove(&symbol); } + PORTAL_INPUT_HELD.store(!held.buttons.is_empty() || !held.keys.is_empty(), std::sync::atomic::Ordering::Relaxed); + } fn inject_move(&self,x:i32,y:i32,relative:bool)->i32 { + // The viewer's mouse mode decides whether the stream must carry the pointer (see + // portal::note_pointer_motion); only the portal backend can change that. + if self.backend==crate::desktop_backend::DesktopBackend::Portal { crate::portal::note_pointer_motion(relative); } if self.uinput_pointer && self.backend != crate::desktop_backend::DesktopBackend::X11 { if let Some(uinput) = &self.uinput { if relative { return Self::uinput_result(uinput.move_relative(x,y)); } @@ -607,14 +666,25 @@ impl InputInjector { // Portal: clicks follow absolute motion on the same session, so a tap cannot land // before the compositor has moved the pointer there (uinput is a separate queue). if !self.uinput_pointer && self.backend==crate::desktop_backend::DesktopBackend::Portal && !self.virtual_uinput() { - if let Some(portal)=self.portal.lock().unwrap().as_ref() { return self.portal_result("button", portal.button(button,pressed)); } + if let Some(portal)=self.portal.lock().unwrap().as_ref() { + // The injection and its record must both predate or both postdate a swap. + let _serial=lock_portal_held(); + let result=self.portal_result("button", portal.button(button,pressed)); + if result==0 { self.record_portal_button(button,pressed); } + return result; + } } if let Some(uinput)=&self.uinput { if let Some((v,h))=crate::uinput::wheel_step(button) { return if pressed { Self::uinput_result(uinput.wheel(v,h)) } else { 0 }; } if let Some(code)=crate::uinput::button_code(button) { return Self::uinput_result(uinput.button(code,pressed)); } } if self.backend==crate::desktop_backend::DesktopBackend::Kms { return -1; } - if let Some(portal)=self.portal.lock().unwrap().as_ref() { return self.portal_result("button", portal.button(button,pressed)); } + if let Some(portal)=self.portal.lock().unwrap().as_ref() { + let _serial=lock_portal_held(); + let result=self.portal_result("button", portal.button(button,pressed)); + if result==0 { self.record_portal_button(button,pressed); } + return result; + } 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)) } } @@ -624,7 +694,12 @@ impl InputInjector { if let Some(code)=crate::uinput::keysym_to_evdev(symbol) { return Self::uinput_result(uinput.key(code,pressed)); } } if self.backend==crate::desktop_backend::DesktopBackend::Kms { return -1; } - if let Some(portal)=self.portal.lock().unwrap().as_ref() { return self.portal_result("keyboard", portal.key(symbol,pressed)); } + if let Some(portal)=self.portal.lock().unwrap().as_ref() { + let _serial=lock_portal_held(); + let result=self.portal_result("keyboard", portal.key(symbol,pressed)); + if result==0 { self.record_portal_key(symbol,pressed); } + return result; + } 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)) } } diff --git a/src/portal.rs b/src/portal.rs index 1852c61..c957a9a 100644 --- a/src/portal.rs +++ b/src/portal.rs @@ -42,9 +42,13 @@ mod native { fn uu_portal_open_persist_cancellable(restore: *const libc::c_char, save: extern "C" fn(*const libc::c_char, *mut c_void), save_data: *mut c_void, cancelled: extern "C" fn(*mut c_void) -> i32, cancel_data: *mut c_void, error: *mut i32, width: *mut u32, height: *mut u32) -> *mut c_void; fn uu_portal_open_persist_capture(restore: *const libc::c_char, save: extern "C" fn(*const libc::c_char, *mut c_void), save_data: *mut c_void, cancelled: Option i32>, cancel_data: *mut c_void, prefer_dmabuf: i32, error: *mut i32, width: *mut u32, height: *mut u32) -> *mut c_void; fn uu_portal_open_detailed(restore: *const libc::c_char, save: extern "C" fn(*const libc::c_char, *mut c_void, i32, i32, i32), save_data: *mut c_void, cancelled: Option i32>, cancel_data: *mut c_void, prefer_dmabuf: i32, source_types: u32, error: *mut i32, width: *mut u32, height: *mut u32) -> *mut c_void; + fn uu_portal_set_cursor_mode(mode: i32); + fn uu_portal_cursor_mode(portal: *mut c_void) -> i32; + fn uu_portal_cursor_modes(portal: *mut c_void) -> i32; fn uu_portal_clipboard_supported(portal: *mut c_void) -> i32; fn uu_portal_clipboard_ready(portal: *mut c_void) -> i32; fn uu_portal_clipboard_set(portal: *mut c_void, text: *const u8, length: usize) -> i32; + fn uu_portal_clipboard_copy(from: *mut c_void, to: *mut c_void) -> i32; fn uu_portal_clipboard_poll(portal: *mut c_void, text: *mut u8, capacity: usize) -> i32; fn uu_portal_close(portal: *mut c_void); fn uu_portal_alive(portal: *mut c_void) -> i32; @@ -83,6 +87,8 @@ mod native { } // Portal preparation and native teardown can wait for consent or D-Bus. // Only the swap may hold the mutex shared with input and clipboard. + // Only the unit tests swap a test handle now; production swaps carry the clipboard. + #[cfg(test)] fn replace_handle(current: &Mutex, locked: MutexGuard<'_, T>, prepare: impl FnOnce() -> Result, String>) -> Result { drop(locked); let Some(replacement) = prepare()? else { return Ok(false); }; @@ -93,6 +99,24 @@ mod native { drop(old); Ok(true) } + /// Like `replace_handle`, but carries the viewer's clipboard text before the old session + /// closes. Clipboard writers wait from before the swap until the carry-over finishes. + fn replace_handle_carrying(current: &Mutex>, clipboard_write: &Mutex<()>, locked: MutexGuard<'_, Arc>, prepare: impl FnOnce() -> Result>, String>) -> Result { + drop(locked); + let Some(replacement) = prepare()? else { return Ok(false); }; + let _clipboard = clipboard_write.lock().map_err(|_| "Portal clipboard lock poisoned")?; + let _serial = crate::input::lock_portal_held(); + let (old, installed) = { + let mut handle = current.lock().map_err(|_| "Portal session lock poisoned")?; + let old = std::mem::replace(&mut *handle, replacement); + (old, Arc::clone(&*handle)) + }; + unsafe { uu_portal_clipboard_copy(old.0.as_ptr(), installed.0.as_ptr()) }; + drop(old); + // The replaced devices took any held portal input with them. + crate::input::portal_session_replaced(); + Ok(true) + } #[derive(Default)] struct Recovery { retry_at: Option, failed_until: Option, denied: bool } impl Recovery { @@ -118,7 +142,7 @@ mod native { was_locked.store(false, std::sync::atomic::Ordering::Release); } } - pub struct Session { handle: Mutex>, locked: tokio::sync::watch::Receiver>, was_locked: std::sync::atomic::AtomicBool, recovery: Mutex, size: std::sync::atomic::AtomicU64, monitor: Mutex>, virtual_output: std::sync::atomic::AtomicBool } + pub struct Session { handle: Mutex>, locked: tokio::sync::watch::Receiver>, was_locked: std::sync::atomic::AtomicBool, recovery: Mutex, size: std::sync::atomic::AtomicU64, monitor: Mutex>, virtual_output: std::sync::atomic::AtomicBool, clipboard_write: Mutex<()>, end_generation: Arc } impl Session { fn take_lock_observation(&self) -> Result { // A recovery in progress must see the lock even if another capture @@ -139,6 +163,44 @@ mod native { Ok(()) } } + /// Swap `replacement` in, carrying the viewer's clipboard text before the old session + /// closes. `expected` rejects a desktop end that raced the open. + fn install_replacement(&self, replacement: Self, expected: Option, defer_held: bool, refresh_privacy: bool) -> Result<(u32, u32), String> { + let size = replacement.size(); + let next = replacement.handle.into_inner().map_err(|_| "Portal session lock poisoned".to_string())?; + let clipboard = self.clipboard_write.lock().map_err(|_| "Portal clipboard lock poisoned".to_string())?; + // Records made against the new handle must postdate the reset below. + let serial = crate::input::lock_portal_held(); + // Checked under the serial lock: an input recorded after an earlier check would + // otherwise be destroyed with the session it was sent to. + if defer_held && crate::input::portal_input_held() { return Err("input held during the swap".into()); } + let mut handle = self.handle.lock().map_err(|_| "Portal session lock poisoned".to_string())?; + if expected.is_some_and(|generation| self.end_generation.load(std::sync::atomic::Ordering::Acquire) != generation) { + return Err("desktop session ended during the rebuild".into()); + } + self.size.store(pack(size.0, size.1), std::sync::atomic::Ordering::Release); + let previous_handle = std::mem::replace(&mut *handle, next); + let installed = Arc::clone(&*handle); + drop(handle); + if unsafe { uu_portal_clipboard_copy(previous_handle.0.as_ptr(), installed.0.as_ptr()) } < 0 { + log::warn!("Portal clipboard ownership could not be carried over"); + } + drop(clipboard); + drop(previous_handle); + // The replaced devices took any held portal input with them. + crate::input::portal_session_replaced(); + drop(serial); + // A replaced virtual output can move in the layout; absolute input needs its new share. + if refresh_privacy { crate::privacy_screen::refresh_pointer_region(); } + Ok(size) + } + /// Point the host and the engine at the cursor mode the installed session really has. + fn restore_installed_cursor_mode(&self, fallback: bool) { + let installed = self.handle.lock().ok().map(|handle| unsafe { uu_portal_cursor_mode(handle.0.as_ptr()) }).unwrap_or(0); + let embedded = if installed > 0 { installed == 2 } else { fallback }; + unsafe { uu_portal_set_cursor_mode(if embedded { 2 } else { 1 }); } + super::CURSOR_MODE.store(if embedded { 2 } else { 1 }, std::sync::atomic::Ordering::Release); + } pub fn is_alive(&self) -> bool { self.handle.lock().is_ok_and(|handle| unsafe { uu_portal_alive(handle.0.as_ptr()) != 0 }) } @@ -218,7 +280,8 @@ mod native { prefer_dmabuf = true; } let pointer = if detailed { - let cancel = cancelled.filter(|_| !virtual_output); + // Virtual shares can wait for consent too; cancel them like monitor opens. + let cancel = cancelled; let callback = cancel.map(|_| is_cancelled as extern "C" fn(*mut c_void) -> i32); let cancel_data = cancel.map_or(std::ptr::null_mut(), |value| value as *const _ as *mut c_void); unsafe { uu_portal_open_detailed( @@ -266,7 +329,14 @@ mod native { if wanted.is_some() && granted != wanted { log::warn!("Portal granted a different monitor than requested"); } // Keep a freshly granted compositor frame regardless of lock state. Discarding it // before new damage can leave a static desktop permanently blank (#2). - Ok(Self { handle: Mutex::new(Arc::new(Handle(handle))), locked: crate::session_lock::watch_locked(), was_locked: std::sync::atomic::AtomicBool::new(false), recovery: Mutex::new(Recovery::default()), size: pack(width, height).into(), monitor: Mutex::new(granted), virtual_output: virtual_output.into() }) + let handle = Arc::new(Handle(handle)); + // The engine records what it actually requested; a backend can refuse the mode the + // environment or a runtime switch asked for. + let available = unsafe { uu_portal_cursor_modes(handle.0.as_ptr()) }; + let applied = unsafe { uu_portal_cursor_mode(handle.0.as_ptr()) }; + if applied > 0 { super::CURSOR_MODE.store(if applied == 2 { 2 } else { 1 }, std::sync::atomic::Ordering::Release); } + if available > 0 { super::CURSOR_EMBED.store(if available & 2 != 0 { 2 } else { 1 }, std::sync::atomic::Ordering::Release); } + Ok(Self { handle: Mutex::new(handle), locked: crate::session_lock::watch_locked(), was_locked: std::sync::atomic::AtomicBool::new(false), recovery: Mutex::new(Recovery::default()), size: pack(width, height).into(), monitor: Mutex::new(granted), virtual_output: virtual_output.into(), clipboard_write: Mutex::new(()), end_generation: Arc::new(std::sync::atomic::AtomicU64::new(0)) }) } /// Frame pixels of the monitor the stream currently follows. pub fn size(&self) -> (u32, u32) { @@ -276,7 +346,13 @@ mod native { /// End the compositor session (screen-share indicator off); the next desktop /// connection reopens it with the saved consent. pub fn end(&self) { + // Invalidate an open that is still waiting for consent: it must not install a share + // after the desktop was ended (viewer disconnect, idle shutdown, capture teardown). + self.end_generation.fetch_add(1, std::sync::atomic::Ordering::AcqRel); if let Ok(handle) = self.handle.lock() { unsafe { uu_portal_end(handle.0.as_ptr()) }; } + // The ended session's native devices are gone; forget the inputs bound to them. + let _serial = crate::input::lock_portal_held(); + crate::input::portal_session_replaced(); } /// Mutter connector of the monitor this session captures, when several are advertised. pub fn monitor(&self) -> Option { self.monitor.lock().ok().and_then(|m| m.clone()) } @@ -284,19 +360,15 @@ mod native { /// time GNOME asks on the host) and swap it in, so every holder of this Arc follows. pub fn switch_to(&self, connector: &str) -> Result<(), String> { if self.monitor().as_deref() == Some(connector) { return Ok(()); } + // Both this and the cursor-mode rebuild replace the handle: serialize them so the + // later open cannot install a stale source over the other. + let _recovery = self.recovery.lock().map_err(|_| "Portal recovery lock poisoned")?; let replacement = Self::open_inner(None, Some(connector), false, false).map_err(|error| error.to_string())?; if replacement.monitor().as_deref() != Some(connector) { return Err("Another monitor was shared; choose this monitor in the host dialog".into()); } - let size = replacement.size(); - let next = replacement.handle.into_inner().map_err(|_| "Portal session lock poisoned")?; - let previous = { - let mut handle = self.handle.lock().map_err(|_| "Portal session lock poisoned")?; - self.size.store(pack(size.0, size.1), std::sync::atomic::Ordering::Release); - std::mem::replace(&mut *handle, next) - }; + self.install_replacement(replacement, None, false, false)?; if let Ok(mut monitor) = self.monitor.lock() { *monitor = Some(connector.to_owned()); } - drop(previous); Ok(()) } pub fn is_virtual(&self) -> bool { self.virtual_output.load(std::sync::atomic::Ordering::Acquire) } @@ -311,18 +383,116 @@ mod native { // exists (seen live: 3968x1280 = panel + virtual), so end the virtual share first. if !on { self.end(); } let replacement = Self::open_inner(None, monitor.as_deref(), false, on).map_err(|error| error.to_string())?; - let size = replacement.size(); - let next = replacement.handle.into_inner().map_err(|_| "Portal session lock poisoned")?; - let previous = { - let mut handle = self.handle.lock().map_err(|_| "Portal session lock poisoned")?; - self.size.store(pack(size.0, size.1), std::sync::atomic::Ordering::Release); - self.virtual_output.store(on, std::sync::atomic::Ordering::Release); - std::mem::replace(&mut *handle, next) - }; - drop(previous); + let size = self.install_replacement(replacement, None, false, false)?; + self.virtual_output.store(on, std::sync::atomic::Ordering::Release); log::info!("Portal capture source: virtual={on} size={}x{}", size.0, size.1); Ok(()) } + /// Ask the compositor to draw the pointer into the stream (embedded) or keep it out + /// (hidden). The portal only reads this when the session is created, so swap in a fresh + /// session with the saved consent; every holder of this Arc follows the new handle. + /// Ok(true): the mode changed. Ok(false): nothing to do, or the desktop is locked and the + /// next unlocked motion retries. + pub fn set_cursor_embedded(&self, embedded: bool) -> Result { + use std::sync::atomic::{AtomicBool, Ordering}; + if super::cursor_embedded() == embedded { return Ok(false); } + // Sample before waiting for `recovery`: an end that happens while this worker is + // queued must invalidate the rebuild below. + let generation = self.end_generation.load(Ordering::Acquire); + if *self.locked.borrow() != Some(false) { return Ok(false); } + // A backend without the embedded cursor mode never applies the request; remember it + // so motion does not spawn a rebuild every few events. + let modes = self.handle.lock().map(|handle| unsafe { uu_portal_cursor_modes(handle.0.as_ptr()) }).unwrap_or(0); + if modes > 0 && modes & 2 == 0 { super::CURSOR_EMBED.store(1, Ordering::Release); return Ok(false); } + if modes & 2 != 0 { super::CURSOR_EMBED.store(2, Ordering::Release); } + let _recovery = self.recovery.lock().map_err(|_| "Portal recovery lock poisoned")?; + if super::cursor_embedded() == embedded { return Ok(false); } + if *self.locked.borrow() != Some(false) { return Ok(false); } + // The session this rebuild replaces must still be running: a viewer that left or an + // idle shutdown ends it, and a replacement opened afterwards would share with nobody. + if !self.is_alive() { return Ok(false); } + if crate::input::portal_input_held() { return Ok(false); } + let previous = super::cursor_embedded(); + let virtual_output = self.is_virtual(); + let monitor = self.monitor(); + // A lock or desktop end during the open must cancel the consent request, not wait + // out its timeout. + let cancel = Arc::new(AtomicBool::new(false)); + let stop = Arc::new(AtomicBool::new(false)); + let watcher = { + let locked = self.locked.clone(); + let end_generation = self.end_generation.clone(); + let cancel = cancel.clone(); + let stop = stop.clone(); + std::thread::Builder::new().name("portal-cursor-lock".into()).spawn(move || { + while !stop.load(Ordering::Acquire) { + if *locked.borrow() != Some(false) || end_generation.load(Ordering::Acquire) != generation { + cancel.store(true, Ordering::Release); + return; + } + std::thread::sleep(std::time::Duration::from_millis(50)); + } + }).ok() + }; + unsafe { uu_portal_set_cursor_mode(if embedded { 2 } else { 1 }); } + let result = (|| -> Result, String> { + let replacement = Self::open_inner(Some(&cancel), monitor.as_deref(), false, virtual_output) + .map_err(|error| error.to_string())?; + if *self.locked.borrow() != Some(false) { return Err("desktop locked during the rebuild".into()); } + if self.end_generation.load(Ordering::Acquire) != generation { + return Err("desktop session ended during the rebuild".into()); + } + // A source change that finished meanwhile must not be undone by this swap. + if self.is_virtual() != virtual_output || (!virtual_output && self.monitor() != monitor) { + return Err("capture source changed during the rebuild".into()); + } + // A restored consent must not silently move the share to another monitor. + if !virtual_output && monitor.is_some() && replacement.monitor() != monitor { + return Err("another monitor was shared; choose this monitor in the host dialog".into()); + } + // KDE does not report the connector, so a size change is the signal that the + // replacement follows another output. + self.check_geometry(&replacement).map_err(|error| error.to_string())?; + // A key or button can go down while the open waits for consent. + if crate::input::portal_input_held() { return Ok(None); } + let size = match self.install_replacement(replacement, Some(generation), true, virtual_output) { + Ok(size) => size, + Err(error) if error == "input held during the swap" => return Ok(None), + Err(error) => return Err(error), + }; + let applied = self.handle.lock().ok().map(|handle| unsafe { uu_portal_cursor_mode(handle.0.as_ptr()) }).unwrap_or(0); + Ok(Some((size, applied))) + })(); + stop.store(true, Ordering::Release); + drop(watcher); + match result { + Ok(Some(((width, height), applied))) => { + let applied_embedded = applied == 2; + super::CURSOR_MODE.store(if applied_embedded { 2 } else { 1 }, Ordering::Release); + if applied_embedded != embedded { + // The backend refused the mode (or cannot report support); retrying would + // rebuild the session every motion run without changing anything. + super::CURSOR_EMBED.store(1, Ordering::Release); + unsafe { uu_portal_set_cursor_mode(applied) }; + log::info!("Portal backend did not apply the embedded cursor"); + return Ok(false); + } + log::info!("Portal cursor mode: embedded={embedded} size={width}x{height}"); + Ok(true) + } + Ok(None) => { + // Deferred (input went down during the open): open_inner published the + // replacement's mode, but that session is never installed. + self.restore_installed_cursor_mode(previous); + Ok(false) + } + Err(error) => { + // Leave the engine on the mode the installed session actually has. + self.restore_installed_cursor_mode(previous); + Err(error) + } + } + } pub fn open() -> Result { Self::open_inner(None, None, false, false).map_err(|error| error.to_string()) } @@ -337,6 +507,8 @@ mod native { pub fn upgrade_touch(&self) -> Result { let available = self.handle.lock().is_ok_and(|handle| unsafe { uu_portal_touch_available(handle.0.as_ptr()) } != 0); if !available || self.touch_supported() { return Ok(false); } + // Serialize with recovery and the cursor-mode rebuild: all replace the handle. + let _recovery = self.recovery.lock().map_err(|_| "Portal recovery lock poisoned")?; log::info!("Portal touch consent: asking the host again for the touchscreen"); let monitor = self.monitor(); let replacement = Self::open_inner(None, monitor.as_deref(), true, self.is_virtual()).map_err(|error| error.to_string())?; @@ -344,9 +516,7 @@ mod native { if replacement.monitor() != monitor { return Err("another monitor was shared".into()); } if !replacement.touch_supported() { return Err("the touchscreen was not granted".into()); } self.check_geometry(&replacement).map_err(|error| error.to_string())?; - let next = replacement.handle.into_inner().map_err(|_| "Portal session lock poisoned")?; - let previous = std::mem::replace(&mut *self.handle.lock().map_err(|_| "Portal session lock poisoned")?, next); - drop(previous); + self.install_replacement(replacement, None, false, self.is_virtual())?; Ok(true) } pub fn open_cancellable(cancelled: &std::sync::atomic::AtomicBool) -> Result { @@ -381,9 +551,19 @@ mod native { let result = super::open_then_install(cancelled, || Self::open_inner(Some(cancelled), monitor.as_deref(), false, virtual_output), |replacement| { self.check_geometry(&replacement)?; let replacement_handle = replacement.handle.into_inner().map_err(|_| super::PortalError { code: 3 })?; + let clipboard = self.clipboard_write.lock().map_err(|_| super::PortalError { code: 3 })?; + let serial = crate::input::lock_portal_held(); let mut handle = self.handle.lock().map_err(|_| super::PortalError { code: 3 })?; if cancelled.load(Ordering::Acquire) { return Err(super::PortalError { code: 13 }); } - *handle = replacement_handle; + let previous_handle = std::mem::replace(&mut *handle, replacement_handle); + let installed = Arc::clone(&*handle); + drop(handle); + unsafe { uu_portal_clipboard_copy(previous_handle.0.as_ptr(), installed.0.as_ptr()) }; + drop(clipboard); + drop(previous_handle); + // The replaced devices took any held portal input with them. + crate::input::portal_session_replaced(); + drop(serial); clear_pause_for_unlocked_replacement(&self.was_locked, *self.locked.borrow()); log::info!("Portal capture session restored after closure"); Ok(()) @@ -474,7 +654,7 @@ mod native { Err(TryLockError::WouldBlock) => return Ok(blank_changed(frame, version)), Err(TryLockError::Poisoned(_)) => return Err("Portal recovery lock poisoned".into()), }; - if replace_handle(&self.handle, handle, || { + if replace_handle_carrying(&self.handle, &self.clipboard_write, handle, || { { let handle = self.handle.lock().map_err(|_| "Portal session lock poisoned")?; if unsafe { uu_portal_capture_alive(handle.0.as_ptr()) } != 0 { @@ -546,6 +726,9 @@ mod native { } pub fn clipboard_set(&self, text: &str) -> Result<(), String> { if text.len()>crate::clipboard::MAX_TEXT || *self.locked.borrow()!=Some(false) { return Err("Portal clipboard unavailable".into()); } + // Serialize with the cursor-session swap: it carries the old text over before + // releasing this lock, so a update that arrives during the swap wins afterwards. + let _write=self.clipboard_write.lock().map_err(|_| "Portal clipboard lock poisoned")?; let handle=self.handle.lock().map_err(|_| "Portal session lock poisoned")?; if unsafe { uu_portal_clipboard_set(handle.0.as_ptr(),text.as_ptr(),text.len()) }==0 {Ok(())} else {Err("Portal clipboard ownership failed".into())} } @@ -624,6 +807,8 @@ mod native { recovery: Mutex::new(Recovery::default()), size: pack(physical.0, physical.1).into(), monitor: Mutex::new(None), virtual_output: false.into(), + clipboard_write: Mutex::new(()), + end_generation: Arc::new(std::sync::atomic::AtomicU64::new(0)), })) } @@ -920,6 +1105,7 @@ impl PortalSession { pub fn end(&self) {} pub fn switch_to(&self, _: &str) -> Result<(), String> { Err(error(15)) } pub fn set_virtual(&self, _: bool) -> Result<(), String> { Err(error(15)) } + pub fn set_cursor_embedded(&self, _: bool) -> Result { Err(error(15)) } pub fn is_virtual(&self) -> bool { false } } #[cfg(not(all(target_os = "linux", feature = "portal")))] @@ -980,6 +1166,128 @@ pub fn switch_active(connector: &str) -> Result<(), String> { .ok_or("Portal desktop unavailable")?; session.switch_to(connector) } +/// Whether the capture stream carries the compositor's pointer. 0 is "not asked yet"; the +/// startup answer has to match what the engine reads from UU_PORTAL_CURSOR_MODE. +static CURSOR_MODE: std::sync::atomic::AtomicU8 = std::sync::atomic::AtomicU8::new(0); +/// Embedded cursor support reported by a session: 0 unknown, 1 unavailable, 2 available. +static CURSOR_EMBED: std::sync::atomic::AtomicU8 = std::sync::atomic::AtomicU8::new(0); +fn cursor_embedded() -> bool { + use std::sync::atomic::Ordering; + match CURSOR_MODE.load(Ordering::Acquire) { + 0 => { + let embedded = std::env::var("UU_PORTAL_CURSOR_MODE").is_ok_and(|value| value.eq_ignore_ascii_case("embedded")); + CURSOR_MODE.store(if embedded { 2 } else { 1 }, Ordering::Release); + embedded + } + mode => mode == 2, + } +} +/// Ask the running session for a stream with (or without) the compositor cursor. +pub fn set_cursor_embedded(embedded: bool) -> Result { + let session = ACTIVE.lock().unwrap_or_else(std::sync::PoisonError::into_inner).as_ref().and_then(std::sync::Weak::upgrade) + .ok_or("Portal desktop unavailable")?; + session.set_cursor_embedded(embedded) +} +/// A release package can carry an older libuu-portal.so without the runtime cursor switch; +/// rebuilding the session would be wasted work there. +fn cursor_switch_available() -> bool { + #[cfg(all(target_os = "linux", feature = "portal", feature = "engine-plugins"))] + { + static AVAILABLE: std::sync::OnceLock = std::sync::OnceLock::new(); + *AVAILABLE.get_or_init(|| crate::engine_plugin::symbol(crate::engine_plugin::Engine::Portal, "uu_portal_set_cursor_mode\0") != 0) + } + #[cfg(not(all(target_os = "linux", feature = "portal", feature = "engine-plugins")))] + { cfg!(all(target_os = "linux", feature = "portal")) } +} +/// Relative motion has no position the viewer could draw a cursor at, so the stream has to carry +/// one. Absolute motion means the viewer draws its own and an embedded copy would trail it. Both +/// need a fresh portal session, so wait for a run of the same kind before rebuilding it. +struct PointerMode { + candidate: Option, + streak: u32, + first: Option, + switching: bool, + blocked_until: Option, +} +static POINTER_MODE: std::sync::Mutex = std::sync::Mutex::new(PointerMode { + candidate: None, streak: 0, first: None, switching: false, blocked_until: None, +}); +const POINTER_STREAK: u32 = 5; +/// A burst at connect must not rebuild the session, so the run has to span time as well. +const POINTER_WINDOW: std::time::Duration = std::time::Duration::from_millis(200); +/// After a failed rebuild the consent may be gone; do not retry on the next few events. +const POINTER_COOLDOWN: std::time::Duration = std::time::Duration::from_secs(5); +fn pointer_decision(state: &mut PointerMode, current: bool, relative: bool, now: std::time::Instant) -> Option { + if current == relative { + state.candidate = None; + state.streak = 0; + state.first = None; + return None; + } + if state.switching || state.blocked_until.is_some_and(|until| now < until) { return None; } + match state.candidate { + Some(candidate) if candidate == relative => { + state.streak += 1; + if state.streak >= POINTER_STREAK + && state.first.is_some_and(|first| now.duration_since(first) >= POINTER_WINDOW) { + return Some(relative); + } + } + _ => { + state.candidate = Some(relative); + state.streak = 1; + state.first = Some(now); + } + } + None +} +pub fn note_pointer_motion(relative: bool) { + if !cursor_switch_available() { return; } + // The backend does not advertise an embedded cursor: nothing to switch to. + if CURSOR_EMBED.load(std::sync::atomic::Ordering::Acquire) == 1 { return; } + // A swap closes the portal input devices; do not drop keys or buttons the viewer holds. + if crate::input::portal_input_held() { return; } + let decision = { + let Ok(mut state) = POINTER_MODE.lock() else { return; }; + let decision = pointer_decision(&mut state, cursor_embedded(), relative, std::time::Instant::now()); + if decision.is_some() { + state.switching = true; + state.candidate = None; + state.streak = 0; + state.first = None; + } + decision + }; + let Some(target) = decision else { return; }; + let spawned = std::thread::Builder::new().name("portal-cursor".into()).spawn(move || { + let result = set_cursor_embedded(target); + let mut state = POINTER_MODE.lock().unwrap_or_else(std::sync::PoisonError::into_inner); + state.switching = false; + state.candidate = None; + state.streak = 0; + state.first = None; + match result { + Ok(true) => { + state.blocked_until = None; + drop(state); + log::info!("Cursor mode follows the pointer: embedded={target}"); + } + Ok(false) => { + // Locked, or something else changed the mode first; the next motion retries. + state.blocked_until = None; + drop(state); + } + Err(error) => { + state.blocked_until = Some(std::time::Instant::now() + POINTER_COOLDOWN); + drop(state); + log::warn!("Portal cursor switch failed: {error}"); + } + } + }); + if spawned.is_err() { + if let Ok(mut state) = POINTER_MODE.lock() { state.switching = false; } + } +} pub struct Monitor { pub connector: String, pub width: u32, pub height: u32, pub x: i32, pub y: i32, pub primary: bool } #[cfg(all(target_os = "linux", feature = "portal"))] pub use native::monitors; @@ -1065,3 +1373,76 @@ mod tests { assert_eq!(*handle.lock().unwrap(), 11); } } + +#[cfg(test)] +mod pointer_mode_tests { + use super::{pointer_decision, PointerMode, POINTER_COOLDOWN, POINTER_WINDOW}; + use std::time::{Duration, Instant}; + + fn state() -> PointerMode { + PointerMode { candidate: None, streak: 0, first: None, switching: false, blocked_until: None } + } + + #[test] + fn a_connect_burst_does_not_rebuild_the_session() { + let mut state = state(); + let start = Instant::now(); + for step in 0..20 { + assert_eq!(pointer_decision(&mut state, true, false, start + Duration::from_millis(step)), None); + } + } + + #[test] + fn a_sustained_change_decides_once_the_run_spans_the_window() { + let mut state = state(); + let start = Instant::now(); + let mut decided = None; + for step in 0..40 { + let now = start + Duration::from_millis(step * 10); + if let Some(value) = pointer_decision(&mut state, true, false, now) { + assert!(now.duration_since(start) >= POINTER_WINDOW); + decided = Some(value); + break; + } + } + assert_eq!(decided, Some(false)); + } + + #[test] + fn an_opposite_event_restarts_the_run() { + let mut state = state(); + let start = Instant::now(); + for step in 0..10 { pointer_decision(&mut state, true, false, start + Duration::from_millis(step)); } + pointer_decision(&mut state, true, true, start + Duration::from_millis(10)); + for step in 11..19 { + assert_eq!(pointer_decision(&mut state, true, false, start + Duration::from_millis(step)), None); + } + } + + #[test] + fn a_failed_switch_waits_out_the_cooldown() { + let mut state = state(); + let start = Instant::now(); + state.blocked_until = Some(start + POINTER_COOLDOWN); + for step in 0..40 { + assert_eq!(pointer_decision(&mut state, true, false, start + Duration::from_millis(step * 10)), None); + } + let after = start + POINTER_COOLDOWN + Duration::from_millis(1); + let mut decided = None; + for step in 0..30 { + let now = after + Duration::from_millis(step * 10); + if let Some(value) = pointer_decision(&mut state, true, false, now) { decided = Some(value); break; } + } + assert_eq!(decided, Some(false)); + } + + #[test] + fn a_switch_in_flight_ignores_more_events() { + let mut state = state(); + let start = Instant::now(); + state.switching = true; + for step in 0..40 { + assert_eq!(pointer_decision(&mut state, true, false, start + Duration::from_millis(step * 10)), None); + } + } +} diff --git a/src/privacy_screen/backend.rs b/src/privacy_screen/backend.rs index 8f51e2c..c2f588f 100644 --- a/src/privacy_screen/backend.rs +++ b/src/privacy_screen/backend.rs @@ -20,6 +20,9 @@ pub trait Backend: Send + Sync { fn enter(&self, mode: Mode, journal: &mut Journal) -> Result; /// Undo `data` from `enter`. Also used for crash recovery, where no desktop stream exists. fn leave(&self, data: &serde_json::Value) -> Result<(), String>; + /// Recompute `Entered::pointer_region` after the virtual output was recreated. None keeps + /// the stored region (backends without a virtual output, or a layout not settled yet). + fn refresh_pointer_region(&self) -> Option<[f64; 4]> { None } } /// The backend for this session, if the desktop has one. diff --git a/src/privacy_screen/kde/mod.rs b/src/privacy_screen/kde/mod.rs index 30e4426..304a387 100644 --- a/src/privacy_screen/kde/mod.rs +++ b/src/privacy_screen/kde/mod.rs @@ -88,6 +88,14 @@ impl Backend for Kde { log::info!("Privacy KDE: outputs={} input_devices={}", data.outputs.len(), data.devices.len()); Ok(entered) } + fn refresh_pointer_region(&self) -> Option<[f64; 4]> { + // The old virtual output disappears with the replaced session; wait for the layout to + // settle on the new one before taking its share of the bounding box. + let layout = outputs::layout().ok()?; + let virtuals: Vec<_> = layout.iter().filter(|o| o.name.starts_with("Virtual")).collect(); + let target = match virtuals.as_slice() { [only] => *only, _ => return None }; + outputs::region(&layout, &target.name) + } fn leave(&self, data: &serde_json::Value) -> Result<(), String> { let data: Data = serde_json::from_value(data.clone()).map_err(|_| "invalid KDE recovery data")?; // KWin persists the device flag: a failure is returned (after restoring the outputs) so the diff --git a/src/privacy_screen/mod.rs b/src/privacy_screen/mod.rs index 2b677aa..af55ab5 100644 --- a/src/privacy_screen/mod.rs +++ b/src/privacy_screen/mod.rs @@ -48,6 +48,18 @@ pub fn state_message(seq: u64, timestamp: u64) -> Vec { } /// See `backend::Entered::pointer_region`. pub fn pointer_region() -> Option<[f64; 4]> { *POINTER.lock().ok()? } +/// A session rebuild replaced the virtual output: refresh the region absolute input maps into. +pub fn refresh_pointer_region() { + if !active() { return; } + for _ in 0..10 { + if let Some(region) = backend::detect().and_then(backend::Backend::refresh_pointer_region) { + if let Ok(mut pointer) = POINTER.lock() { *pointer = Some(region); } + return; + } + std::thread::sleep(std::time::Duration::from_millis(50)); + } + log::warn!("Privacy pointer region not refreshed: virtual output layout did not settle"); +} fn publish(active: Option<&Active>) { ACTIVE.store(active.is_some(), Ordering::Relaxed);