From f67de7cc7da33d6476402d428dc9bcfb361a06a8 Mon Sep 17 00:00:00 2001 From: "Mr. Error" Date: Thu, 5 Nov 2026 03:22:02 +0800 Subject: [PATCH] Privacy: recover the input devices after a reboot renumbers them (#173) * fix(privacy): match recorded input devices after a reboot The recovery record stores KWin's `eventN` sysNames, but udev renumbers them between boots. A record replayed after a reboot (privacy was on at shutdown) then set `enabled` on a device path that no longer exists; the restore failed, kept the record, and every later switch failed at the same step. The device that had been blocked could also stay disabled. Records now keep name/product/vendor next to the sysName, and a restore matches the current device by that identity, falling back to the exact sysName for older string-only records. A device that no longer exists is skipped with a log line instead of failing the restore, so the record, the guard and the outputs are cleaned up; a device that exists but cannot be enabled still fails and keeps the record for the next attempt. Verified by replaying records with the release build on KDE Plasma 6.8: a stale `event14` for a mouse now on `event12` is re-enabled and the record removed; a record for a missing device logs the skip and restores normally. `cargo test --features wayland,portal`: 330 passed, 10 ignored. * fix(privacy): restore every device sharing an identity - enable every current device whose identity matches a record entry, deduplicated by sysName: two record entries can describe one device, and one identity can cover several (identical keyboards, or a receiver exposing several event nodes under the same name) - a failed property read during restore is an error instead of a missing device, so the record and the guard stay for the next attempt; blocking keeps the lenient read - the gone-device warning logs a count only, matching the log-privacy rule * fix(privacy): re-enable readable devices before reporting incomplete enumeration A failed property read during restore no longer aborts enumeration before set_sys runs: the readable matching devices are re-enabled first, then the incomplete read is returned as an error so the record and the guard stay for another attempt. Blocking still skips unreadable devices. --- src/privacy_screen/kde/input.rs | 184 +++++++++++++++++++++++++++++--- src/privacy_screen/kde/mod.rs | 7 +- 2 files changed, 176 insertions(+), 15 deletions(-) diff --git a/src/privacy_screen/kde/input.rs b/src/privacy_screen/kde/input.rs index c32044b..542be3d 100644 --- a/src/privacy_screen/kde/input.rs +++ b/src/privacy_screen/kde/input.rs @@ -1,24 +1,87 @@ //! Local input devices through KWin's InputDevice D-Bus interface (sysName `eventN`). KWin saves //! the enabled flag, which is why the controller runs the guard. +//! +//! `eventN` comes from udev enumeration and can change between boots, while a recovery record may +//! be replayed after a reboot. Records therefore store the name/product/vendor KWin reports next +//! to the sysName, and a restore matches the current device by that identity. use super::super::tool::{gdbus, variant}; +use serde::{Deserialize, Serialize}; fn call(object: &str, method: &str, args: &[&str]) -> Result { gdbus("org.kde.KWin", object, method, args) } + +/// One blocked device. `Legacy` is a record written before identities were stored. +#[derive(Clone, Debug, Serialize, Deserialize)] +#[serde(untagged)] +pub enum Device { + Named { sys: String, name: String, product: u32, vendor: u32 }, + Legacy(String), +} + +impl Device { + pub fn sys(&self) -> &str { + match self { Self::Named { sys, .. } | Self::Legacy(sys) => sys } + } + fn name(&self) -> &str { + match self { Self::Named { name, .. } => name, Self::Legacy(sys) => sys } + } + fn identity(&self) -> Option<(&str, u32, u32)> { + match self { Self::Named { name, product, vendor, .. } => Some((name, *product, *vendor)), Self::Legacy(_) => None } + } +} + +/// `'key': ` carries a type prefix; strings and booleans do not. +fn number(text: &str, key: &str) -> Option { variant(text, key)?.split_whitespace().last()?.parse().ok() } + +#[derive(Clone, Copy, Default)] +struct Flags { keyboard: bool, pointer: bool, touch: bool, switch: bool, enabled: bool } + +/// Every device KWin lists, blocked or not: restore must find a device that is still disabled. +/// The flag says whether a property read failed. A restore re-enables the readable devices, then +/// returns the flag as an error so its record and the guard stay for another attempt; blocking +/// skips such a device, which is all it can do. +fn enumerate() -> Result<(Vec<(Device, Flags)>, bool), String> { + let list = call("/org/kde/KWin/InputDevice", "org.freedesktop.DBus.Properties.Get", &["org.kde.KWin.InputDeviceManager", "devicesSysNames"])?; + let mut devices = Vec::new(); + let mut incomplete = false; + for sys in list.split('\'').skip(1).step_by(2).filter(|n| n.strip_prefix("event").is_some_and(|d| !d.is_empty() && d.bytes().all(|b| b.is_ascii_digit()))) { + let text = match call(&format!("/org/kde/KWin/InputDevice/{sys}"), "org.freedesktop.DBus.Properties.GetAll", &["org.kde.KWin.InputDevice"]) { + Ok(text) => text, + Err(_) => { incomplete = true; continue; } + }; + let yes = |key| variant(&text, key) == Some("true"); + devices.push((Device::Named { + sys: sys.to_owned(), + name: variant(&text, "name").unwrap_or_default().to_owned(), + product: number(&text, "product").unwrap_or(0), + vendor: number(&text, "vendor").unwrap_or(0), + }, Flags { keyboard: yes("keyboard"), pointer: yes("pointer"), touch: yes("touch"), switch: yes("switchDevice"), enabled: yes("enabled") })); + } + Ok((devices, incomplete)) +} + /// Keyboards, pointers and touch to block: everything except our own uinput devices, the power /// button and switches (lid). Software devices fed by local hardware (gesture daemons, ydotoold) /// are blocked too. -pub fn local() -> Result, String> { - let list = call("/org/kde/KWin/InputDevice", "org.freedesktop.DBus.Properties.Get", &["org.kde.KWin.InputDeviceManager", "devicesSysNames"])?; - let mut blocked = Vec::new(); - for sys in list.split('\'').skip(1).step_by(2).filter(|n| n.strip_prefix("event").is_some_and(|d| !d.is_empty() && d.bytes().all(|b| b.is_ascii_digit()))) { - let Ok(text) = call(&format!("/org/kde/KWin/InputDevice/{sys}"), "org.freedesktop.DBus.Properties.GetAll", &["org.kde.KWin.InputDevice"]) else { continue }; - let yes = |key| variant(&text, key) == Some("true"); - let name = variant(&text, "name").unwrap_or_default(); - if (yes("keyboard") || yes("pointer") || yes("touch")) && !yes("switchDevice") && yes("enabled") - && !name.starts_with("uu-remote-linux") && name != "Power Button" { blocked.push(sys.to_owned()); } - } - Ok(blocked) +pub fn local() -> Result, String> { + Ok(enumerate()?.0.into_iter().filter(|(d, f)| { + (f.keyboard || f.pointer || f.touch) && !f.switch && f.enabled + && !d.name().starts_with("uu-remote-linux") && d.name() != "Power Button" + }).map(|(d, _)| d).collect()) } -pub fn set(devices: &[String], enabled: bool) -> Result<(), String> { + +/// Every current device matching `wanted`: same name/product/vendor, or the exact sysName for a +/// legacy record. Several devices can share one identity (two identical keyboards, or one receiver +/// exposing several event nodes under the same name), so callers must handle every match. +fn matching<'a>(devices: &'a [Device], wanted: &Device) -> impl Iterator + 'a { + let identity = wanted.identity().map(|(name, product, vendor)| (name.to_owned(), product, vendor)); + let sys = wanted.sys().to_owned(); + devices.iter().filter(move |d| match (&identity, d.identity()) { + (Some((name, product, vendor)), Some((current, p, v))) => current == name && p == *product && v == *vendor, + _ => d.sys() == sys, + }) +} + +fn set_sys<'a>(devices: impl Iterator, enabled: bool) -> Result<(), String> { let value = if enabled { "" } else { "" }; let mut failure = None; for sys in devices { @@ -28,3 +91,100 @@ pub fn set(devices: &[String], enabled: bool) -> Result<(), String> { } failure.map_or(Ok(()), Err) } + +pub fn set(devices: &[Device], enabled: bool) -> Result<(), String> { + set_sys(devices.iter().map(Device::sys), enabled) +} + +/// Current sysNames to re-enable and how many record entries have no current match. +fn restore_targets<'a>(now: &'a [Device], recorded: &[Device]) -> (Vec<&'a str>, usize) { + let mut targets: Vec<&str> = Vec::new(); + let mut gone = 0; + for wanted in recorded { + let mut found = false; + for current in matching(now, wanted) { + found = true; + if !targets.contains(¤t.sys()) { targets.push(current.sys()); } + } + if !found { gone += 1; } + } + (targets, gone) +} + +/// Re-enables every current device matching a record entry, deduplicated by sysName: two record +/// entries can describe the same device, and one identity can cover several devices. A recorded +/// device that no longer exists is skipped: its `eventN` may belong to a different device after a +/// reboot, and failing the whole restore would keep the recovery record forever. A device that +/// exists but cannot be enabled still fails the restore, so the record and the guard stay for the +/// next attempt. +pub fn restore(devices: &[Device]) -> Result<(), String> { + if devices.is_empty() { return Ok(()); } + let (found, incomplete) = enumerate()?; + let now: Vec = found.into_iter().map(|(d, _)| d).collect(); + let (targets, gone) = restore_targets(&now, devices); + // Counts only: device names and sysNames are not logged (AGENTS.md). + if gone > 0 { log::warn!("Privacy restore: {gone} recorded device(s) are gone; skipping"); } + // Re-enable the readable devices first: one unreadable device must not keep every other one + // off. The error afterwards keeps the record and the guard for another attempt. + set_sys(targets.into_iter(), true)?; + if incomplete { return Err("input device properties could not be read; retrying later".into()); } + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn named(sys: &str, name: &str, product: u32, vendor: u32) -> Device { + Device::Named { sys: sys.into(), name: name.into(), product, vendor } + } + + #[test] + fn parses_numbered_variants() { + let text = "({'name': <'Logitech G304'>, 'product': , 'vendor': },)"; + assert_eq!(number(text, "product"), Some(16500)); + assert_eq!(number(text, "vendor"), Some(1133)); + assert_eq!(number(text, "missing"), None); + } + + #[test] + fn reads_legacy_and_named_records() { + let legacy: Vec = serde_json::from_str(r#"["event5","event6"]"#).unwrap(); + assert!(matches!(&legacy[0], Device::Legacy(sys) if sys == "event5")); + let named: Device = serde_json::from_str(r#"{"sys":"event12","name":"Logitech G304","product":16500,"vendor":1133}"#).unwrap(); + assert_eq!(named.identity(), Some(("Logitech G304", 16500, 1133))); + } + + #[test] + fn matches_a_renumbered_device_by_identity() { + let mouse = named("event12", "Logitech G304", 16500, 1133); + let keyboard = named("event3", "AT Translated Set 2 keyboard", 1, 1); + let recorded = [named("event14", "Logitech G304", 16500, 1133)]; + let now = [keyboard, mouse]; + assert_eq!(super::restore_targets(&now, &recorded), (vec!["event12"], 0)); + } + + #[test] + fn enables_every_device_sharing_an_identity_once() { + let first = named("event12", "Logitech G304", 16500, 1133); + let second = named("event15", "Logitech G304", 16500, 1133); + let now = [first, second]; + // Two record entries for one identity, as a receiver exposing several event nodes produces. + let recorded = [named("event14", "Logitech G304", 16500, 1133), named("event16", "Logitech G304", 16500, 1133)]; + assert_eq!(super::restore_targets(&now, &recorded), (vec!["event12", "event15"], 0)); + } + + #[test] + fn a_missing_device_counts_as_gone() { + let recorded = [named("event14", "Logitech G304", 16500, 1133)]; + let now = [named("event3", "AT Translated Set 2 keyboard", 1, 1)]; + assert_eq!(super::restore_targets(&now, &recorded), (Vec::new(), 1)); + } + + #[test] + fn a_legacy_record_matches_the_exact_sysname() { + let recorded = [Device::Legacy("event5".into())]; + let now = [named("event5", "AT Translated Set 2 keyboard", 1, 1)]; + assert_eq!(super::restore_targets(&now, &recorded), (vec!["event5"], 0)); + } +} diff --git a/src/privacy_screen/kde/mod.rs b/src/privacy_screen/kde/mod.rs index 25a4f2b..6efc828 100644 --- a/src/privacy_screen/kde/mod.rs +++ b/src/privacy_screen/kde/mod.rs @@ -16,7 +16,7 @@ pub const NAME: &str = "kde"; pub struct Kde; #[derive(Default, serde::Serialize, serde::Deserialize)] -struct Data { outputs: Vec, #[serde(default)] devices: Vec } +struct Data { outputs: Vec, #[serde(default)] devices: Vec } impl Kde { pub fn detected() -> bool { @@ -84,8 +84,9 @@ impl Backend for Kde { 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 - // controller keeps the recovery record and the guard for the next attempt. - let devices = input::set(&data.devices, true).map_err(|e| format!("enable input devices: {e}")); + // 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}")); // 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).