From c489f3056fa0ef62f96b2313b6cac332b3e7e877 Mon Sep 17 00:00:00 2001 From: "Mr. Error" Date: Sun, 8 Nov 2026 20:16:35 +0800 Subject: [PATCH] fix(privacy): keep new windows and desktop pets on the virtual output (#201) --- docs/privacy-screen.md | 7 +++ src/privacy_screen/kde/kwin.rs | 81 ++++++++++++++++++++++++++++++---- src/privacy_screen/kde/mod.rs | 11 ++++- 3 files changed, 89 insertions(+), 10 deletions(-) diff --git a/docs/privacy-screen.md b/docs/privacy-screen.md index 25c7cc7..a5f0e2f 100644 --- a/docs/privacy-screen.md +++ b/docs/privacy-screen.md @@ -30,6 +30,13 @@ is aimed at the virtual output's region. Needs GTK3. `portal-permissions/kde-virtual`)。窗口会被 KWin 移到虚拟屏,恢复后位置可能变化。 The virtual size is KDE's (1920x1080 by default; the first share may ask on the host, later ones reuse `portal-permissions/kde-virtual`). KWin moves windows to it; positions may change on restore. +- 壁纸模式下新出现的窗口也会被送到虚拟输出:KWin 按它认为"当前活动"的屏幕放置新窗口,而那块 + 屏幕可能是遮罩下的物理屏,远端就看不到。主机在防窥期间保持加载一个 KWin 脚本,新窗口和换了 + 屏幕的窗口都会被送回虚拟输出,恢复时卸载。 + In wallpaper mode, windows that appear later are also sent to the virtual output: KWin places a + new window on the screen it considers active, which may be one under the cover, where the viewer + cannot see it. The host keeps a KWin script loaded for the privacy session that sends new windows + and windows that changed screen back to the virtual output, and unloads it on restore. - KDE 不处理虚拟输出共享上的 portal 输入,防窥期间指针、按键和文字改走 uinput(需要 `/dev/uinput` 权限,见 README);手机拖动手势在此期间按普通鼠标拖动处理。 KDE ignores portal input on a virtual-output share, so pointer, keys and text use uinput while the diff --git a/src/privacy_screen/kde/kwin.rs b/src/privacy_screen/kde/kwin.rs index b411a3f..2551e4c 100644 --- a/src/privacy_screen/kde/kwin.rs +++ b/src/privacy_screen/kde/kwin.rs @@ -1,12 +1,28 @@ -//! One-shot KWin scripts (org.kde.kwin.Scripting). +//! KWin scripts (org.kde.kwin.Scripting): one-shot ones, plus the new-window keeper that picture +//! mode leaves loaded for the whole privacy session. use super::super::tool::gdbus; +const ONE_SHOT: &str = "uu-privacy"; +const KEEP_WINDOWS: &str = "uu-privacy-windows"; + fn call(object: &str, method: &str, args: &[&str]) -> Result { gdbus("org.kde.KWin", object, method, args) } -fn script(js: &str) -> Result<(), String> { +/// Output names go into a JS string literal. +fn escape(output: &str) -> String { output.replace('\\', "\\\\").replace('"', "\\\"") } +/// Windows that belong on the captured output. Desktop pets and tool palettes use toolbar/utility +/// windows, which `normalWindow` misses; panels, the desktop and transient popups stay where they +/// are, so the layout of the physical outputs is unchanged. +const MOVABLE: &str = r#"function movable(w) { + if (!w || w.desktopWindow || w.dock) return false; + if (w.tooltip || w.popupMenu || w.dropdownMenu || w.comboBox || w.dndIcon) return false; + return w.normalWindow || w.dialog || w.toolbar || w.utility || w.splash; +}"#; +/// Loads `js` under `name` and runs it. `keep` leaves it loaded with its signal connections alive; +/// otherwise it is unloaded after the run, as the one-shot scripts expect. +fn script(js: &str, name: &str, keep: bool) -> Result<(), String> { let dir = std::env::var_os("XDG_RUNTIME_DIR").map(std::path::PathBuf::from).ok_or("missing runtime directory")?; - let file = dir.join(format!("uu-privacy-{}.js", std::process::id())); + let file = dir.join(format!("uu-privacy-{name}-{}.js", std::process::id())); std::fs::write(&file, js).map_err(|e| e.kind().to_string())?; - let name = "uu-privacy"; + // A script left loaded by a host that died would keep running with a stale target. let _ = call("/Scripting", "org.kde.kwin.Scripting.unloadScript", &[name]); let result = call("/Scripting", "org.kde.kwin.Scripting.loadScript", &[&file.to_string_lossy(), name]).and_then(|out| { let id: String = out.chars().filter(char::is_ascii_digit).collect(); @@ -14,14 +30,52 @@ fn script(js: &str) -> Result<(), String> { call(&format!("/Scripting/Script{id}"), "org.kde.kwin.Script.run", &[]) }); std::thread::sleep(std::time::Duration::from_millis(300)); - let _ = call("/Scripting", "org.kde.kwin.Scripting.unloadScript", &[name]); + // A failed run must not leave a half-started keeper behind. + if !keep || result.is_err() { let _ = call("/Scripting", "org.kde.kwin.Scripting.unloadScript", &[name]); } let _ = std::fs::remove_file(file); result.map(|_| ()) } -/// Sends every normal window and dialog to `output` (a name from kscreen-doctor, file-name safe). +/// Sends every movable window to `output` (a name from kscreen-doctor). pub fn move_windows_to(output: &str) -> Result<(), String> { - script(&format!(r#"const v = workspace.screens.find(o => o.name == "{output}"); -if (v) for (const w of workspace.windowList()) if (w.normalWindow || w.dialog) workspace.sendClientToScreen(w, v);"#)) + let output = escape(output); + script(&format!(r#"{movable} +const v = workspace.screens.find(o => o.name == "{output}"); +if (v) for (const w of workspace.windowList()) if (movable(w)) workspace.sendClientToScreen(w, v);"#, movable = MOVABLE), ONE_SHOT, false) +} +/// Keeps windows on `output`. Picture mode turns the physical outputs back on as secondaries, and +/// KWin then places a new window on the screen it considers active, which can be one hidden under +/// the cover: the viewer would never see it. Every window is checked when it appears and whenever +/// its geometry changes, so an application that places itself on a physical output is sent back. +/// `frameGeometryChanged` is the trigger because KWin 6.8 does not emit `outputChanged` for +/// `sendClientToScreen` moves (verified live). Unloaded by `unload_keep_windows` on restore. +pub fn keep_windows_on(output: &str) -> Result<(), String> { + let output = escape(output); + script(&format!(r#"{movable} +function keep(w) {{ + if (!movable(w)) return; + // The cover windows belong on the physical outputs. + if (w.resourceClass === "uuremote-server" || (w.caption || "").indexOf("uu-remote-linux privacy") === 0) return; + const screen = workspace.screens.find(o => o.name === "{output}"); + if (!screen || (w.output && w.output.name === "{output}")) return; + workspace.sendClientToScreen(w, screen); +}} +function watch(w) {{ + if (!w) return; + // One window that cannot be moved or observed must not stop the others. + try {{ + keep(w); + w.frameGeometryChanged.connect(function () {{ keep(w); }}); + }} catch (e) {{}} +}} +// New windows first: a throwing sweep must not leave them unhandled. +workspace.windowAdded.connect(watch); +workspace.windowList().forEach(watch); +"#, movable = MOVABLE), KEEP_WINDOWS, true) +} +/// Stops following new windows. A name that is not loaded (screen-off mode, a restarted KWin +/// session) reports false without an error. +pub fn unload_keep_windows() -> Result<(), String> { + call("/Scripting", "org.kde.kwin.Scripting.unloadScript", &[KEEP_WINDOWS]).map(|_| ()) } /// Hides the cover window from the task manager. GTK's skip-taskbar hint is an X11 hint and KWin /// ignores it on Wayland, so the cover showed up as an unidentified window with the theme's @@ -30,5 +84,14 @@ if (v) for (const w of workspace.windowList()) if (w.normalWindow || w.dialog) w /// duplicate captions, so match the title as a prefix. pub fn hide_cover_from_taskbar() -> Result<(), String> { script(r#"for (const w of workspace.windowList()) -if (w.resourceClass === "uuremote-server" && w.caption.indexOf("uu-remote-linux privacy") === 0) w.skipTaskbar = true;"#) +if (w.resourceClass === "uuremote-server" && w.caption.indexOf("uu-remote-linux privacy") === 0) w.skipTaskbar = true;"#, ONE_SHOT, false) +} + +#[cfg(test)] +mod tests { + #[test] + fn escapes_output_names_for_the_script() { + assert_eq!(super::escape("DP-3"), "DP-3"); + assert_eq!(super::escape(r#"a"b\c"#), r#"a\"b\\c"#); + } } diff --git a/src/privacy_screen/kde/mod.rs b/src/privacy_screen/kde/mod.rs index 6efc828..30e4426 100644 --- a/src/privacy_screen/kde/mod.rs +++ b/src/privacy_screen/kde/mod.rs @@ -5,7 +5,8 @@ //! physical outputs before the virtual one exists (KWin applies stored configs on creation), //! re-enable them before the virtual output goes away, and never disable the virtual output //! itself (KWin bug 527747 persists that and breaks later virtual shares). -//! Picture mode then turns the physical outputs back on as secondary ones and covers them. +//! Picture mode then turns the physical outputs back on as secondary ones and covers them, and a +//! second KWin script keeps windows that appear later on the virtual output too. mod input; mod kwin; mod outputs; @@ -39,6 +40,12 @@ fn wallpaper() -> Option { /// secondary outputs, keep the windows on the virtual one, and cover the physical ones. fn cover(data: &Data) -> Result<[f64; 4], String> { let virtual_name = outputs::layout()?.into_iter().map(|o| o.name).find(|n| !data.outputs.contains(n)).ok_or("virtual output not found")?; + // Loaded before the physical outputs come back: from then on KWin places a new window on the + // screen it considers active, which may be one that is about to be covered. + match kwin::keep_windows_on(&virtual_name) { + Ok(()) => log::info!("Privacy new-window keeper: loaded"), + Err(e) => log::warn!("Privacy new-window keeper: {e}"), + } outputs::set(&data.outputs, true).map_err(|e| format!("enable outputs: {e}"))?; if let Err(e) = outputs::set_primary(&virtual_name) { log::warn!("Privacy virtual output priority: {e}"); } kwin::move_windows_to(&virtual_name).map_err(|e| format!("move windows: {e}"))?; @@ -87,6 +94,8 @@ impl Backend for Kde { // controller keeps the recovery record and the guard for the next attempt. A device that // no longer exists is skipped, so a record replayed after a reboot cannot wedge recovery. let devices = input::restore(&data.devices).map_err(|e| format!("enable input devices: {e}")); + // Stop following new windows before the virtual output goes away. + if let Err(e) = kwin::unload_keep_windows() { log::warn!("Privacy new-window keeper: {e}"); } // Physical outputs first: switching back opens a monitor share, which needs them enabled. // The wait also matters: a share opened before KWin applies the mode got the virtual // output's geometry (seen live: 1920x1080 instead of the panel's 3072x1920).