//! Native Linux X11 Input Injection Engine via XTest #[cfg(target_os = "linux")] extern "C" { fn x11_mouse_move(x: libc::c_int, y: libc::c_int) -> libc::c_int; fn x11_mouse_relative(dx: libc::c_int, dy: libc::c_int) -> libc::c_int; fn x11_mouse_button(button: libc::c_int, is_press: libc::c_int) -> libc::c_int; 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; fn x11_type_utf8(text: *const libc::c_char) -> 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_type_utf8(_: *const libc::c_char) -> libc::c_int { 0 } #[cfg(not(target_os = "linux"))] unsafe fn x11_mouse_move(_: libc::c_int, _: libc::c_int) -> libc::c_int { 0 } #[cfg(not(target_os = "linux"))] unsafe fn x11_mouse_button(_: libc::c_int, _: libc::c_int) -> libc::c_int { 0 } #[cfg(not(target_os = "linux"))] unsafe fn x11_key_event(_: libc::c_int, _: libc::c_int) -> libc::c_int { 0 } #[cfg(not(target_os = "linux"))] 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)>, relative: (i64,i64), scroll: (i64,i64), } #[cfg(not(target_os = "linux"))] 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 } /// 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 } /// Mobile viewers in touch mode send a finger as a left-button drag at absolute positions (observed: /// Android sends press, a stream of mouse_move_absolute, release), which on a desktop selects text /// instead of scrolling. By default (`Gesture`) the host decides per press: moving past `SLOP` before /// `HOLD` scrolls smoothly (content follows the finger, also in terminals), holding still selects /// like a mouse drag, and a short press is a click. `UU_TOUCH_INPUT=touch` replays drags as one finger /// on the portal touchscreen instead; `=0` keeps mouse buttons. `at` is the last absolute position; /// relative motion (the viewers' trackpad mode) clears it and keeps mouse buttons. #[derive(Default)] struct TouchDrag { mode: TouchMode, at: Option<(i32,i32)>, down: bool, finger: Finger } #[derive(Clone, Copy, Debug, Default, PartialEq)] enum TouchMode { #[default] Mouse, Gesture, Touch } /// A mobile left press in gesture mode: not decided yet, scrolling, or selecting with button 1 down. #[derive(Clone, Copy, Debug, Default, PartialEq)] enum Finger { #[default] Up, Pending { start: (i32,i32), at: std::time::Instant }, Scroll { last: (i32,i32) }, Select } /// Pixels a finger may wander before a press becomes a scroll, and how long it holds still to select. const SLOP: i32 = 12; const HOLD: std::time::Duration = std::time::Duration::from_millis(450); /// What an absolute position does to the finger: the next state and a scroll from→to or a pointer move. #[derive(Debug, PartialEq)] enum FingerStep { Scroll((i32,i32),(i32,i32)), Move(i32,i32), Stay } fn finger_motion(finger: Finger, to: (i32,i32)) -> (Finger, FingerStep) { match finger { Finger::Pending { start, .. } if (to.0-start.0).abs().max((to.1-start.1).abs()) > SLOP => (Finger::Scroll { last: to }, FingerStep::Scroll(start, to)), Finger::Pending { .. } => (finger, FingerStep::Stay), Finger::Scroll { last } => (Finger::Scroll { last: to }, FingerStep::Scroll(last, to)), Finger::Select | Finger::Up => (finger, FingerStep::Move(to.0, to.1)), } } /// A pending press held still for `HOLD`: where the selection starts. fn finger_held(finger: Finger, now: std::time::Instant) -> Option<(i32,i32)> { match finger { Finger::Pending { start, at } if now.duration_since(at) >= HOLD => Some(start), _ => None } } /// Viewer platform from the connect header `controller_platform` (website enum: 1 windows, 2 android, /// 3 ios, 4 macos, 5 tv). #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] pub enum Controller { #[default] Other, Windows, Mac, Mobile, Ios } impl Controller { /// Touch semantics cover both mobile platforms; the iOS RPC 7 screen-off encoding is its own quirk. pub fn is_mobile(self) -> bool { matches!(self, Self::Mobile | Self::Ios) } pub fn from_platform(platform: Option<&serde_json::Value>) -> Self { let Some(v) = platform else { return Self::Other }; let text = v.as_str().map(str::to_ascii_lowercase); match v.as_i64().or_else(|| text.as_deref().and_then(|s| s.parse().ok())) { Some(4) => Self::Mac, Some(3) => Self::Ios, Some(2) => Self::Mobile, Some(1) => Self::Windows, _ if text.as_deref().is_some_and(|s| s.contains("mac") || s.contains("darwin")) => Self::Mac, _ if text.as_deref().is_some_and(|s| s.contains("ios") || s.contains("iphone") || s.contains("ipad")) => Self::Ios, _ if text.as_deref().is_some_and(|s| s.contains("android")) => Self::Mobile, _ if text.as_deref().is_some_and(|s| s.contains("windows")) => Self::Windows, _ => Self::Other, } } } /// Mac viewer with 「使用 macOS 快捷键」 off sends Cmd as VK_LWIN (verified: Cmd 0x5B, Option 0xA4, /// Ctrl 0xA2). Cmd is held back until we know whether it starts a Mac shortcut. #[derive(Default)] struct CmdState { held: bool, pending: bool, passthrough: bool, combos: std::collections::BTreeMap } pub struct InputInjector { mac_controller: std::sync::atomic::AtomicBool, cmd: std::sync::Mutex, serial:std::sync::Mutex<()>, owner:std::sync::Mutex, shortcuts: std::sync::Mutex, uinput: Option, uinput_pointer: bool, screen: std::sync::Mutex<(u32,u32)>, portal_failures: std::sync::atomic::AtomicUsize, portal_outcomes: [std::sync::atomic::AtomicUsize; 3], portal:std::sync::Mutex>>, backend: crate::desktop_backend::DesktopBackend, pending: std::sync::Mutex, held: std::sync::Mutex, touch: std::sync::Mutex, pointer_wake: tokio::sync::Notify } fn active_output_count(json: &[u8]) -> Option { let data: serde_json::Value = serde_json::from_slice(json).ok()?; Some(data.get("outputs")?.as_array()?.iter() .filter(|o| o["enabled"] == true && o["connected"] == true).count()) } fn single_portal_output() -> bool { if cfg!(test) { return false; } let monitors = crate::portal::monitors(); if !monitors.is_empty() { return monitors.len() == 1; } // KDE has no Mutter monitor API. Query once at injector creation, never during input. std::process::Command::new("timeout").args(["2s", "kscreen-doctor", "-j"]).output().ok() .filter(|output| output.status.success()) .and_then(|output| active_output_count(&output.stdout)) == Some(1) } /// The desktop injector opened uinput devices. The privacy screen needs them: KDE ignores portal /// input on its virtual-output share, so without uinput nobody could operate the desktop. static UINPUT_READY: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false); pub fn uinput_ready() -> bool { UINPUT_READY.load(std::sync::atomic::Ordering::Acquire) } impl InputInjector { pub fn new() -> Self { 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 }; if uinput.is_some() { UINPUT_READY.store(true, std::sync::atomic::Ordering::Release); } let uinput_pointer = uinput.is_some() && (std::env::var("UU_INPUT_BACKEND").as_deref() == Ok("uinput") || (backend == crate::desktop_backend::DesktopBackend::Portal && single_portal_output())); if uinput_pointer { log::info!("Input pointer backend: uinput (motion, buttons and wheel)"); } else if backend == crate::desktop_backend::DesktopBackend::Portal { log::info!("Input pointer backend: portal (monitor-local coordinates)"); } Self { mac_controller: std::sync::atomic::AtomicBool::new(false), cmd: std::sync::Mutex::new(CmdState::default()), serial:std::sync::Mutex::new(()), owner:std::sync::Mutex::new(DesktopOwner::default()), shortcuts: std::sync::Mutex::new(Shortcuts::default()), uinput, uinput_pointer, 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, pending: std::sync::Mutex::new(PendingPointer::default()), held:std::sync::Mutex::new(HeldInput::default()), touch:std::sync::Mutex::new(TouchDrag::default()), pointer_wake: tokio::sync::Notify::new() } } pub fn bind_portal(&self,session:Option>) { log::info!("Desktop portal input binding updated: bound={}", session.is_some()); *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); pending.relative = (0,0); self.pointer_wake.notify_one(); } } pub fn mouse_relative(&self, dx: i32, dy: i32) { if dx.unsigned_abs() <= 65535 && dy.unsigned_abs() <= 65535 { let mut pending = self.pending.lock().unwrap(); pending.relative.0 = (pending.relative.0 + i64::from(dx)).clamp(-65535,65535); pending.relative.1 = (pending.relative.1 + i64::from(dy)).clamp(-65535,65535); self.pointer_wake.notify_one(); } } pub fn mouse_scroll(&self, dx: i32, dy: i32) { // Official Mac normal wheel=120; precise trackpads use smaller values. let mut pending = self.pending.lock().unwrap(); pending.scroll.0 = (pending.scroll.0 + i64::from(dx)).clamp(-12000,12000); pending.scroll.1 = (pending.scroll.1 + i64::from(dy)).clamp(-12000,12000); self.pointer_wake.notify_one(); } pub fn start_pointer_worker(self: &std::sync::Arc) -> tokio::task::JoinHandle<()> { let input = self.clone(); tokio::spawn(async move { // Parked until pointer work exists: a fixed 4 ms tick burned ~250 wakeups/s // (≈3% CPU with pow-heavy flush math) even with no viewer connected. // notify_one() stores a permit, so an event racing the park check cannot be lost. let mut tick = tokio::time::interval(std::time::Duration::from_millis(4)); tick.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip); loop { input.pointer_wake.notified().await; // The first event is immediate; later events coalesce until the next tick. tick.reset(); loop { input.flush_pointer(); tick.tick().await; if input.pointer_idle() { break; } } } }) } /// Whether the worker may park: nothing accumulated, no full scroll notch waiting and no /// touch gesture in flight (`Pending` needs ticks until its hold resolves, `Scroll`/`Select` /// and `down` until release). Sub-notch scroll residue cannot inject alone, so it parks. fn pointer_idle(&self) -> bool { let pending = self.pending.lock().unwrap(); let touch = self.touch.lock().unwrap(); pending.absolute.is_none() && pending.relative == (0,0) && pending.scroll.0 / 120 == 0 && pending.scroll.1 / 120 == 0 && !touch.down && touch.finger == Finger::Up } 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(); let motion = std::mem::take(&mut *pending); pending.scroll = (motion.scroll.0 % 120, motion.scroll.1 % 120); let mut touch = self.touch.lock().unwrap(); if let Some((x,y)) = motion.absolute { if touch.down { self.inject_touch(1,x,y); } else { let (finger, step) = finger_motion(touch.finger, (x,y)); touch.finger = finger; match step { FingerStep::Scroll(from,to) => { self.inject_scroll(from,to,false); }, FingerStep::Move(x,y) => { self.inject_move(x,y,false); }, FingerStep::Stay => {}, } } touch.at = Some((x,y)); } // Held still: select from where the finger went down, like a mouse drag. if let Some((x,y)) = finger_held(touch.finger, std::time::Instant::now()) { self.inject_move(x,y,false); touch.finger = if self.inject_button(1,true) == 0 { Finger::Select } else { Finger::Up }; } drop(touch); if motion.relative != (0,0) { self.touch.lock().unwrap().at = None; self.inject_move(motion.relative.0 as i32,motion.relative.1 as i32,true); } for (value, positive, negative) in [(motion.scroll.1,4,5),(motion.scroll.0,7,6)] { let count = value / 120; let button = if count > 0 { positive } else { negative }; for _ in 0..count.unsigned_abs().min(100) { self.inject_button(button,true); self.inject_button(button,false); } } } pub fn wire_mouse_button(&self, button: i32, is_press: bool) { if let Some(mapped) = crate::input_mapping::mouse_button_to_x11(button) { self.mouse_button(mapped,is_press); } else { static UNKNOWN: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); if UNKNOWN.fetch_add(1, std::sync::atomic::Ordering::Relaxed) < 16 { log::warn!("Unsupported desktop wire mouse button: {}", button); } } } pub fn mouse_button(&self, button: i32, is_press: bool) { let _serial=self.serial.lock().unwrap(); // The worker cannot consume the wake and flush until this transition is published. self.pointer_wake.notify_one(); self.flush_pointer_locked(); if button == 1 && self.touch_button(is_press) { return; } let result = self.inject_button(button,is_press); if result == 0 { let mut held = self.held.lock().unwrap(); if is_press { held.buttons.insert(button); } else { held.buttons.remove(&button); } } else if self.backend != crate::desktop_backend::DesktopBackend::Portal { log::warn!("Desktop mouse injection failed: {}",result); } static BUTTON_AUDIT:std::sync::atomic::AtomicUsize=std::sync::atomic::AtomicUsize::new(0); if self.backend==crate::desktop_backend::DesktopBackend::X11 && BUTTON_AUDIT.fetch_add(1,std::sync::atomic::Ordering::Relaxed)<24 { let (mut x,mut y,mut mask)=(0,0,0); let query=unsafe {x11_pointer_state(&mut x,&mut y,&mut mask)}; log::info!("Pointer button audit: button={} down={} result={} query={} x={} y={} mask={}",button,is_press,result,query,x,y,mask); } } /// 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 } /// The current desktop controller's platform (connect header controller_platform). /// Mobile drags follow `UU_TOUCH_INPUT`: unset = gesture, `touch` = portal touchscreen, `0` = mouse. pub fn set_controller(&self, controller: Controller) { self.mac_controller.store(controller == Controller::Mac, std::sync::atomic::Ordering::Relaxed); *self.cmd.lock().unwrap() = CmdState::default(); let mut touch = self.touch.lock().unwrap(); self.lift_finger(&mut touch); let mode = if !controller.is_mobile() || self.uinput_pointer { TouchMode::Mouse } else { match std::env::var("UU_TOUCH_INPUT").as_deref() { Ok("0") => TouchMode::Mouse, Ok("touch") => TouchMode::Touch, _ => TouchMode::Gesture } }; *touch = TouchDrag { mode, ..TouchDrag::default() }; } /// Ends whatever a finger holds: the touch point, a selection's button, a scroll gesture. fn lift_finger(&self, touch: &mut TouchDrag) { if std::mem::take(&mut touch.down) { self.inject_touch(2,0,0); } match std::mem::take(&mut touch.finger) { Finger::Select => { self.inject_button(1,false); }, Finger::Scroll { last } => { self.inject_scroll(last,last,true); }, Finger::Pending { .. } | Finger::Up => {}, } } /// Left button of a mobile controller: a finger down at the last absolute position, or up. /// False leaves the button to the mouse path (no touchscreen, trackpad mode, other viewers). fn touch_button(&self, pressed: bool) -> bool { let mut touch = self.touch.lock().unwrap(); if touch.mode == TouchMode::Gesture { return self.gesture_button(&mut touch, pressed) } if !pressed { if !touch.down { return false; } touch.down = false; self.inject_touch(2,0,0); return true; } if touch.down { return true; } let Some((x,y)) = touch.at.filter(|_| touch.mode == TouchMode::Touch) else { return false }; let supported = self.backend == crate::desktop_backend::DesktopBackend::Portal && self.portal.lock().unwrap().as_ref().is_some_and(|portal| portal.touch_supported()); static REPORTED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false); if !REPORTED.swap(true, std::sync::atomic::Ordering::Relaxed) { log::info!("Mobile controller drag input: touchscreen={supported}"); } if !supported { self.offer_touch_consent(); } touch.down = supported && self.inject_touch(0,x,y) == 0; touch.down } /// Gesture mode (portal only, for its smooth scroll): a press waits for `finger_motion`/`finger_held` /// to decide; releasing an undecided press is a click. fn gesture_button(&self, touch: &mut TouchDrag, pressed: bool) -> bool { if !pressed { return match std::mem::take(&mut touch.finger) { Finger::Up => false, Finger::Pending { start, .. } => { self.inject_move(start.0,start.1,false); if self.inject_button(1,true) == 0 { self.inject_button(1,false); } true }, finger => { touch.finger = finger; self.lift_finger(touch); true }, }; } if touch.finger != Finger::Up { return true } // Gesture scrolling is portal-only; on the privacy screen's virtual share drags stay mouse drags. let bound = self.backend == crate::desktop_backend::DesktopBackend::Portal && self.portal.lock().unwrap().is_some() && !self.virtual_uinput(); let Some(start) = touch.at.filter(|_| bound) else { return false }; static REPORTED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false); if !REPORTED.swap(true, std::sync::atomic::Ordering::Relaxed) { log::info!("Mobile controller drag input: gesture (drag scrolls, hold selects)"); } touch.finger = Finger::Pending { start, at: std::time::Instant::now() }; true } /// Content follows the finger: moving it up scrolls down. fn inject_scroll(&self, from: (i32,i32), to: (i32,i32), finish: bool) -> i32 { let Some(portal) = self.portal.lock().unwrap().clone() else { return -1 }; self.portal_result("scroll", portal.scroll(f64::from(from.0 - to.0), f64::from(from.1 - to.1), finish)) } /// The portal offers a touchscreen the restored consent lacks: ask the host once per run, in the /// background (GNOME's share dialog, visible to the phone too); drags stay mouse drags meanwhile. fn offer_touch_consent(&self) { static ASKED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false); let Some(portal) = self.portal.lock().unwrap().clone() else { return }; if ASKED.swap(true, std::sync::atomic::Ordering::Relaxed) { return } std::thread::spawn(move || match portal.upgrade_touch() { Ok(true) => log::info!("Touch consent upgraded: mobile drags are touch from now on"), Ok(false) => log::info!("Touch consent upgrade not needed or not offered by the portal"), Err(reason) => log::info!("Touch consent upgrade not granted: {reason}"), }); } fn inject_touch(&self, phase: i32, x: i32, y: i32) -> i32 { let Some(portal) = self.portal.lock().unwrap().clone() else { return -1 }; self.portal_result("touch", portal.touch(phase, f64::from(x), f64::from(y))) } fn terminal_focused(&self) -> bool { self.backend == crate::desktop_backend::DesktopBackend::X11 && std::env::var_os("DISPLAY").is_some() && !std::env::var("UU_TERMINAL_SHORTCUTS").is_ok_and(|v| v == "0") && unsafe { x11_terminal_shortcut_state() } & 1 != 0 } /// Mac habits for a Mac viewer with 「使用 macOS 快捷键」 off: Cmd+C/V/A/F/X/Z (the keys that option /// lists) become Ctrl+key, and in a focused terminal Cmd+C/V become Ctrl+Shift+C/V (copy/paste) /// while Ctrl+C still interrupts. Other Cmd combos, and Cmd alone, stay Super. Returns true when handled. fn mac_cmd(&self, key: i32, is_press: bool) -> bool { const SUPER_L: u64 = 0xffeb; const CONTROL_L: u64 = 0xffe3; const SHIFT_L: u64 = 0xffe1; let mut st = self.cmd.lock().unwrap(); if matches!(key, 0x5b | 0x5c) { if is_press { st.held = true; st.pending = true; st.passthrough = false; } else { if st.passthrough { self.inject_key(SUPER_L, false); } else if st.pending { self.inject_key(SUPER_L, true); self.inject_key(SUPER_L, false); } st.held = false; st.pending = false; st.passthrough = false; } return true; } if !is_press { let Some(shifted) = st.combos.remove(&key) else { return false }; self.key_symbol_event(key, false); if shifted { self.inject_key(SHIFT_L, false); } self.inject_key(CONTROL_L, false); return true; } if !st.held { return false; } // Shift/Option/Ctrl pressed with Cmd (e.g. Cmd+Shift+Z) must not decide the combo yet. if matches!(key, 0x10..=0x12 | 0xa0..=0xa5) { return false; } if st.combos.contains_key(&key) { self.key_symbol_event(key, true); return true; } // autorepeat if matches!(key, 0x41 | 0x43 | 0x46 | 0x56 | 0x58 | 0x5a) && !st.passthrough { st.pending = false; let shifted = matches!(key, 0x43 | 0x56) && self.terminal_focused(); self.inject_key(CONTROL_L, true); if shifted { self.inject_key(SHIFT_L, true); } self.key_symbol_event(key, true); st.combos.insert(key, shifted); static NOTED: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); if NOTED.fetch_add(1, std::sync::atomic::Ordering::Relaxed) < 4 { log::info!("Mac shortcut: Cmd combo sent as Ctrl{}", if shifted { "+Shift (terminal)" } else { "" }); } return true; } if st.pending { self.inject_key(SUPER_L, true); st.pending = false; st.passthrough = true; } false } pub fn windows_key_event(&self, key: i32, is_press: bool) { let _serial=self.serial.lock().unwrap(); if self.mac_controller.load(std::sync::atomic::Ordering::Relaxed) && self.mac_cmd(key, is_press) { return; } if self.terminal_shift(key, is_press) { const SHIFT_L: u64 = 0xffe1; if is_press { 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); if AUDIT.fetch_add(1,std::sync::atomic::Ordering::Relaxed) < 8 { log::info!("Desktop keyboard injection outcome: result={result}"); } // KP_0..KP_9 type digits only with NumLock on (#46); record the server's NumLock state, not the key. static NUMPAD: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); if (0x60..=0x6f).contains(&key) && is_press && self.backend==crate::desktop_backend::DesktopBackend::X11 && NUMPAD.fetch_add(1,std::sync::atomic::Ordering::Relaxed) < 4 { let (mut x,mut y,mut mask)=(0,0,0u32); let state=unsafe { x11_pointer_state(&mut x,&mut y,&mut mask) }; log::info!("Desktop numpad key: result={result} numlock={}", state==0 && mask & 0x10 != 0); } if result == 0 { let mut held=self.held.lock().unwrap(); if is_press {held.keys.insert(symbol);} else {held.keys.remove(&symbol);} } } } pub fn release_held(&self) { let _serial=self.serial.lock().unwrap(); self.discard_pointer(); { let mut touch=self.touch.lock().unwrap(); self.lift_finger(&mut touch); } let mut held=self.held.lock().unwrap(); 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 if self.backend==crate::desktop_backend::DesktopBackend::Portal && self.portal.lock().unwrap().is_none() { // Nothing is bound yet (e.g. the first locked connection): no input can be held. } else { // Portal/Wayland cannot query global state. Sweep every key/button // that this host can emit through the currently bound session. let keys:std::collections::BTreeSet<_>=(0..=255).filter_map(crate::input_mapping::windows_vk_to_keysym) .chain(held.keys.iter().copied()).collect(); // A dead Portal bus can time out per call. Stop on the first // failure rather than blocking teardown for hundreds of calls; // the next connected session retries the complete sweep. let buttons_ok=[1,2,3,8,9].into_iter().all(|button|self.inject_button(button,false)==0); let keys_ok=buttons_ok && keys.into_iter().all(|key|self.inject_key(key,false)==0); if !keys_ok {log::warn!("Desktop input reset incomplete: backend unavailable");} } *held=HeldInput::default(); *self.shortcuts.lock().unwrap()=Shortcuts::default(); *self.cmd.lock().unwrap()=CmdState::default(); } // Per-injector budgets cover missing bindings, locked sessions, stale handles // and D-Bus errors. Never include keys, coordinates, text or portal tokens. fn portal_result(&self, operation: &str, result: Result<(), String>) -> i32 { use std::sync::atomic::Ordering::Relaxed; let slot = match operation { "motion" => 0, "button" => 1, _ => 2 }; if self.portal_outcomes[slot].fetch_add(1, Relaxed) < 16 { log::info!("Portal input outcome: operation={} success={}", operation, result.is_ok()); } match result { Ok(()) => 0, Err(reason) => { let index = self.portal_failures.fetch_add(1, Relaxed); if index < 16 { log::warn!("Portal input injection failed: operation={} reason={} failure={}/16{}", operation, reason, index + 1, if index == 15 { " (further failures suppressed)" } else { "" }); } -1 } } } 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 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)); } let (w,h) = *self.screen.lock().unwrap(); if w > 1 && h > 1 { let (ax, ay) = self.privacy_absolute(x, y, (w, h)).unwrap_or_else(|| { let scale = |v: i32, extent: u32| (i64::from(v).clamp(0, i64::from(extent - 1)) * i64::from(crate::uinput::ABS_MAX) / i64::from(extent - 1)) as i32; (scale(x, w), scale(y, h)) }); return Self::uinput_result(uinput.move_absolute(ax, ay)); } return -1; } } // uinput ABS axes span the compositor's whole desktop. A captured output // uses its own pixel space; bind absolute motion to that Wayland output, // including when uinput remains in use for keyboard/buttons/relative input. // Only a compositor without wlr-virtual-pointer (-2) falls through to the uinput // absolute pointer; an out-of-range point must not land on another output. if !relative && self.backend==crate::desktop_backend::DesktopBackend::Wayland { let result=crate::wayland::move_pointer(x,y,false); if result!=-2 { return result; } } // Portal (GNOME/KDE): absolute motion is addressed to the shared stream, i.e. the captured // monitor; a uinput tablet would stretch the point across every monitor of the desktop. if !relative && self.backend==crate::desktop_backend::DesktopBackend::Portal { // Privacy screen: KDE accepts absolute motion for a virtual-output stream but does not // move the pointer. The physical outputs are disabled then, so the uinput tablet's // desktop-wide axes cover exactly the virtual output. if !self.virtual_uinput() { if let Some(portal)=self.portal.lock().unwrap().as_ref() { return self.portal_result("motion", portal.move_pointer(f64::from(x),f64::from(y),false)); } } } // X11 multi-screen: the point is inside the selected monitor; XTest and uinput span the root. let placement=if relative || self.backend!=crate::desktop_backend::DesktopBackend::X11 {None} else {crate::wayland::x11_placement()}; let (x,y)=placement.map_or((x,y),|(ox,oy)|(x+ox,y+oy)); if let Some(uinput)=&self.uinput { if relative { return Self::uinput_result(uinput.move_relative(x,y)); } let (w,h)=placement.and_then(|_|crate::capture::x11_last_size()).unwrap_or(*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; if let Some((ax, ay)) = self.privacy_absolute(x, y, (w, h)) { return Self::uinput_result(uinput.move_absolute(ax, ay)); } 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) } } } /// Privacy picture mode keeps the physical outputs on, so uinput's axes span them too: aim the /// stream point inside the virtual output's region of the layout. fn privacy_absolute(&self, x: i32, y: i32, (w, h): (u32, u32)) -> Option<(i32, i32)> { let [rx, ry, rw, rh] = crate::privacy_screen::pointer_region().filter(|_| self.virtual_uinput())?; let abs = |v: i32, extent: u32, o: f64, span: f64| ((o + f64::from(v.clamp(0, extent as i32 - 1)) / f64::from(extent - 1) * span) * f64::from(crate::uinput::ABS_MAX)).round() as i32; Some((abs(x, w, rx, rw), abs(y, h, ry, rh))) } /// Privacy screen: KDE ignores portal pointer events (motion and buttons) on a virtual-output /// share, so the pointer uses uinput, whose axes then span just the virtual output. fn virtual_uinput(&self)->bool { self.uinput.is_some() && self.portal.lock().unwrap().as_ref().is_some_and(|p| p.is_virtual()) } fn inject_button(&self,button:i32,pressed:bool)->i32 { // 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(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)) } } } /// Set clipboard content for text injection on Wayland / Portal. /// Tries Portal RemoteDesktop.SetSelection first, then wl-copy as fallback. fn set_clipboard_text(&self, text: &str) -> bool { if text.is_empty() || text.len() > crate::clipboard::MAX_TEXT { return false; } // In unit tests, never invoke external clipboard binaries (wl-copy) // or alter the system clipboard to prevent side effects on developer desktops. #[cfg(test)] { let _ = text; false } #[cfg(not(test))] { // 1. Try Portal clipboard if session is bound and active if let Some(portal) = self.portal.lock().unwrap().as_ref() { if portal.clipboard_set(text).is_ok() { return true; } } // 2. Try wl-copy in Wayland session if std::env::var_os("WAYLAND_DISPLAY").is_some() { if let Ok(mut child) = std::process::Command::new("wl-copy") .args(["--type", "text/plain;charset=utf-8"]) .stdin(std::process::Stdio::piped()) .stdout(std::process::Stdio::null()) .stderr(std::process::Stdio::null()) .spawn() { if let Some(mut stdin) = child.stdin.take() { use std::io::Write; let _ = stdin.write_all(text.as_bytes()); } if let Ok(status) = child.wait() { if status.success() { return true; } } } } false } } /// Injects text by copying it to the clipboard and simulating a paste shortcut. /// Uses Shift+Insert by default (broadest Linux compatibility across GUI apps and terminals), /// or Ctrl+V if configured via UU_WAYLAND_PASTE_KEY=ctrl_v. fn paste_text_via_clipboard(&self, text: &str) -> bool { if !self.set_clipboard_text(text) { return false; } // Give the clipboard daemon / compositor a brief moment to register the selection. std::thread::sleep(std::time::Duration::from_millis(25)); let use_ctrl_v = std::env::var("UU_WAYLAND_PASTE_KEY") .is_ok_and(|v| v.eq_ignore_ascii_case("ctrl_v") || v.eq_ignore_ascii_case("ctrl+v")); if use_ctrl_v { // Control_L (0xffe3) + 'v' (0x76) self.inject_key(0xffe3, true); std::thread::sleep(std::time::Duration::from_millis(15)); self.inject_key(0x76, true); std::thread::sleep(std::time::Duration::from_millis(20)); self.inject_key(0x76, false); std::thread::sleep(std::time::Duration::from_millis(15)); self.inject_key(0xffe3, false); } else { // Shift_L (0xffe1) + Insert (0xff63) self.inject_key(0xffe1, true); std::thread::sleep(std::time::Duration::from_millis(15)); self.inject_key(0xff63, true); std::thread::sleep(std::time::Duration::from_millis(20)); self.inject_key(0xff63, false); std::thread::sleep(std::time::Duration::from_millis(15)); self.inject_key(0xffe1, false); } // Brief delay allowing the target application to consume the paste event std::thread::sleep(std::time::Duration::from_millis(25)); true } fn emit_uinput_text(&self, plan: &[crate::uinput::TextKey]) -> usize { let Some(devices) = self.uinput.as_ref() else { return 0 }; let (mut typed, mut shift) = (0, 0); for key in plan { if key.code == 0 || key.code > 248 || key.shift > 248 { break; } if shift != key.shift { if shift != 0 { if Self::uinput_result(devices.key(shift, false)) != 0 { break; } shift = 0; } if key.shift != 0 { if Self::uinput_result(devices.key(key.shift, true)) != 0 { break; } shift = key.shift; } } let pressed = Self::uinput_result(devices.key(key.code, true)) == 0; let released = Self::uinput_result(devices.key(key.code, false)) == 0; if pressed { typed += 1; } if !pressed || !released { break; } } if shift != 0 { Self::uinput_result(devices.key(shift, false)); } typed } fn type_uinput_text(&self, text: &str) -> usize { self.type_uinput_text_when(text, || { uinput_layout_types_ascii(&std::env::var("XDG_CURRENT_DESKTOP").unwrap_or_default(), gnome_layout_types_ascii) }) } fn type_uinput_text_when(&self, text: &str, layout_allowed: impl FnOnce() -> bool) -> usize { if self.uinput.is_none() || !self.held.lock().unwrap().keys.is_empty() { return 0; } // XKB can still report US while IBus consumes physical keys for composition. // Leave committed text intact for the existing native/clipboard fallback. if !layout_allowed() { return 0; } self.emit_uinput_text(&crate::uinput::text_plan(text)) } /// Mobile input-method text (`text_input`). X11 types directly. Native Wayland uses the text /// keyboard's Unicode keysyms first: uinput sends physical keycodes, which a running input /// method composes as pinyin, so "hello" would become candidates instead of text. uinput stays /// as the fallback when the keymap path is unavailable. Portal and other Wayland environments /// inject via clipboard paste (Shift+Insert). /// Set UU_WAYLAND_TEXT=0 to disable Wayland text injection. pub fn text_input(&self, text: &str) { let _serial=self.serial.lock().unwrap(); if self.backend != crate::desktop_backend::DesktopBackend::X11 { if std::env::var("UU_WAYLAND_TEXT").is_ok_and(|v| v == "0") { return; } if self.backend == crate::desktop_backend::DesktopBackend::Wayland && crate::wayland::type_utf8(text) >= 0 { log::info!("Desktop text input: chars={} result=keysyms", text.chars().count()); return; } let typed = self.type_uinput_text(text); if typed > 0 { log::info!("Desktop text input (uinput): chars={typed}"); } if typed == text.len() { return; } // The uinput planner consumes ASCII only, so the character count is also a byte offset. let rest = &text[typed..]; return self.desktop_text_input(rest); } self.desktop_text_input(text); } fn desktop_text_input(&self, text: &str) { let Ok(c_text)=std::ffi::CString::new(text.replace('\0',"")) else { return }; if self.backend==crate::desktop_backend::DesktopBackend::X11 { let typed=unsafe { x11_type_utf8(c_text.as_ptr()) }; log::info!("Desktop text input: chars={} result={}", text.chars().count(), typed); } else if self.backend==crate::desktop_backend::DesktopBackend::Wayland { // Escape hatch: disable Wayland text typing/pasting entirely without rebuilding. if std::env::var("UU_WAYLAND_TEXT").is_ok_and(|v|v=="0") { return; } let typed=crate::wayland::type_utf8(text); if typed>=0 { log::info!("Desktop text input: chars={} result={}", text.chars().count(), typed); return; } // Fallback to clipboard paste if keymap has no spare keycodes or native typing failed if self.paste_text_via_clipboard(text) { log::info!("Desktop text input (Wayland clipboard paste fallback): chars={} result=ok", text.chars().count()); return; } static WARNED:std::sync::atomic::AtomicBool=std::sync::atomic::AtomicBool::new(false); if !WARNED.swap(true,std::sync::atomic::Ordering::Relaxed) { log::warn!("Mobile text input unavailable on the Wayland backend (native and clipboard fallback failed)"); } } else if self.backend==crate::desktop_backend::DesktopBackend::Portal { if std::env::var("UU_WAYLAND_TEXT").is_ok_and(|v|v=="0") { return; } // Shift+Insert pastes the PRIMARY selection in VTE terminals, not the clipboard just set: // type what the active layout has as keys and paste only the rest. let typed=self.type_portal_keysyms(text); if typed==text.chars().count() { log::info!("Desktop text input (Portal keysyms): chars={typed}"); return; } let rest:String=text.chars().skip(typed).collect(); if self.paste_text_via_clipboard(&rest) { log::info!("Desktop text input (Portal clipboard paste): chars={} typed={typed} result=ok", text.chars().count()); return; } static WARNED:std::sync::atomic::AtomicBool=std::sync::atomic::AtomicBool::new(false); if !WARNED.swap(true,std::sync::atomic::Ordering::Relaxed) { log::warn!("Portal clipboard text paste failed: ensure clipboard permission or wl-copy is available"); } } else { static WARNED:std::sync::atomic::AtomicBool=std::sync::atomic::AtomicBool::new(false); if !WARNED.swap(true,std::sync::atomic::Ordering::Relaxed) { log::warn!("Mobile text input is not supported on the {:?} backend yet", self.backend); } } } /// Types `text` as portal keysyms when it is all printable ASCII (or newline/tab) and GNOME's /// active input source is a Latin xkb layout; returns how many characters were typed. fn type_portal_keysyms(&self, text: &str) -> usize { let Some(symbols)=ascii_keysyms(text) else { return 0 }; if !gnome_layout_types_ascii() { return 0; } if self.virtual_uinput() { return self.type_uinput_ascii(&symbols); } let Some(portal)=self.portal.lock().unwrap().clone() else { return 0 }; symbols.iter().take_while(|&&symbol| portal.type_keysym(symbol).is_ok()).count() } /// Privacy screen: KDE ignores portal keysyms on a virtual-output share. Type through uinput /// on the US layout the keysym path already requires, adding Shift for shifted characters. fn type_uinput_ascii(&self, symbols: &[u32]) -> usize { const SHIFTED: &str = "~!@#$%^&*()_+{}|:\"<>?"; const BASE: &str = "`1234567890-=[]\\;',./"; symbols.iter().take_while(|&&symbol| { let c=char::from_u32(symbol).unwrap_or('\0'); let (base, shift)=if c.is_ascii_uppercase() { (c.to_ascii_lowercase() as u64, true) } else if let Some(i)=SHIFTED.find(c) { (BASE.as_bytes()[i] as u64, true) } else { (u64::from(symbol), false) }; if crate::uinput::keysym_to_evdev(base).is_none() { return false; } if shift && self.inject_key(0xffe1, true)!=0 { return false; } let ok=self.inject_key(base, true)==0 && self.inject_key(base, false)==0; if shift { self.inject_key(0xffe1, false); } ok }).count() } 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 }) }; } } #[cfg(test)] mod motion_tests { use super::*; #[test] fn coalesces_absolute_history_to_latest_position() { let input = InputInjector::new(); input.mouse_absolute_normalized(0.1,0.1,(1920,1080)); input.mouse_absolute_normalized(0.9,0.9,(1920,1080)); assert_eq!(input.pending.lock().unwrap().absolute,Some((1727,971))); } #[test] fn preserves_relative_distance_after_latest_absolute() { let input = InputInjector::new(); input.mouse_relative(10,20); input.mouse_absolute_normalized(0.5,0.5,(1920,1080)); input.mouse_relative(3,-2); input.mouse_relative(4,5); let state = input.pending.lock().unwrap(); assert_eq!(state.absolute,Some((960,540))); assert_eq!(state.relative,(7,3)); } #[test] fn button_transition_flushes_motion_and_teardown_discards_pending() { let input = InputInjector::new(); input.mouse_absolute_normalized(0.5,0.5,(1920,1080)); input.mouse_button(1,true); assert!(input.pending.lock().unwrap().absolute.is_none()); input.mouse_relative(1,2); input.discard_pointer(); assert_eq!(input.pending.lock().unwrap().relative,(0,0)); } #[test] fn parks_when_pointer_state_is_idle() { let input = InputInjector::new(); assert!(input.pointer_idle()); input.mouse_relative(5,5); assert!(!input.pointer_idle()); input.flush_pointer(); assert!(input.pointer_idle()); input.mouse_scroll(0,125); input.flush_pointer(); assert!(input.pointer_idle(), "sub-notch scroll residue cannot inject alone"); } #[test] fn live_touch_gesture_stays_busy_until_it_resolves() { let input = InputInjector::new(); input.touch.lock().unwrap().finger = Finger::Pending { start: (5,5), at: std::time::Instant::now() }; assert!(!input.pointer_idle()); input.touch.lock().unwrap().finger = Finger::Scroll { last: (9,9) }; assert!(!input.pointer_idle()); *input.touch.lock().unwrap() = TouchDrag::default(); assert!(input.pointer_idle()); } #[test] fn button_wake_waits_for_the_serialized_transition() { use futures_util::FutureExt; let input = std::sync::Arc::new(InputInjector::without_desktop()); let serial = input.serial.lock().unwrap(); let ready = std::sync::Arc::new(std::sync::Barrier::new(2)); let pressing = input.clone(); let started = ready.clone(); let button = std::thread::spawn(move || { started.wait(); pressing.mouse_button(1,true); }); ready.wait(); let runtime = tokio::runtime::Builder::new_current_thread().enable_time().build().unwrap(); let premature = runtime.block_on(async { tokio::time::timeout(std::time::Duration::from_millis(50), input.pointer_wake.notified()).await.is_ok() }); drop(serial); button.join().unwrap(); assert!(!premature, "a parked worker must not wake before the button owns the serial lock"); assert!(input.pointer_wake.notified().now_or_never().is_some()); } #[tokio::test(start_paused = true)] async fn worker_coalesces_one_ms_motion_on_four_ms_ticks() { let input = std::sync::Arc::new(InputInjector::without_desktop()); let worker = input.start_pointer_worker(); input.mouse_relative(1,0); tokio::task::yield_now().await; assert_eq!(input.portal_input_counts_for_test()[0],1, "first motion is immediate"); for ms in 1..=12 { tokio::time::advance(std::time::Duration::from_millis(1)).await; tokio::task::yield_now().await; input.mouse_relative(1,0); tokio::task::yield_now().await; assert_eq!(input.portal_input_counts_for_test()[0],1+ms/4, "motion at {ms} ms bypassed the cadence"); } tokio::time::advance(std::time::Duration::from_millis(4)).await; tokio::task::yield_now().await; assert_eq!(input.portal_input_counts_for_test()[0],5); assert_eq!(input.pending.lock().unwrap().relative,(0,0)); worker.abort(); assert!(worker.await.unwrap_err().is_cancelled()); } #[tokio::test(start_paused = true)] async fn worker_parks_and_resets_the_tick_after_a_quiet_period() { let input = std::sync::Arc::new(InputInjector::without_desktop()); let worker = input.start_pointer_worker(); tokio::task::yield_now().await; // Without a notification, raw queued state must remain untouched while parked. input.pending.lock().unwrap().relative = (1,0); tokio::time::advance(std::time::Duration::from_secs(1)).await; tokio::task::yield_now().await; assert_eq!(input.portal_input_counts_for_test()[0],0); input.mouse_relative(1,0); tokio::task::yield_now().await; assert_eq!(input.portal_input_counts_for_test()[0],1); tokio::time::advance(std::time::Duration::from_millis(4)).await; tokio::task::yield_now().await; input.pending.lock().unwrap().relative = (1,0); tokio::time::advance(std::time::Duration::from_millis(997)).await; tokio::task::yield_now().await; assert_eq!(input.portal_input_counts_for_test()[0],1, "idle ticks must stop after activity"); input.mouse_relative(1,0); tokio::task::yield_now().await; assert_eq!(input.portal_input_counts_for_test()[0],2); input.mouse_relative(1,0); tokio::task::yield_now().await; assert_eq!(input.portal_input_counts_for_test()[0],2, "a stale interval must not flush a second time immediately"); tokio::time::advance(std::time::Duration::from_millis(4)).await; tokio::task::yield_now().await; assert_eq!(input.portal_input_counts_for_test()[0],3); worker.abort(); assert!(worker.await.unwrap_err().is_cancelled()); } } #[cfg(all(test, target_os = "linux"))] mod kernel_pointer_tests { use super::*; #[test] fn committed_text_with_gnome_ime_emits_no_uinput_keys() { for source in ["[('ibus', 'libpinyin'), ('xkb', 'us')]", "@a(ss) []"] { let (devices, mut keyboard, _) = crate::uinput::Devices::recording_for_test(); let mut input = InputInjector::without_desktop(); input.uinput = Some(devices); let checked = std::cell::Cell::new(false); let text = "fixture"; let typed = input.type_uinput_text_when(text, || { checked.set(true); uinput_layout_types_ascii("GNOME", || first_source_types_ascii(source)) }); assert!(checked.get()); assert_eq!(typed, 0); assert!(crate::uinput::Devices::recorded_events_for_test(&mut keyboard).is_empty()); } } #[test] fn text_plan_emits_balanced_modifiers_without_portal_or_clipboard() { let (devices, mut keyboard, _) = crate::uinput::Devices::recording_for_test(); let mut input = InputInjector::without_desktop(); input.uinput = Some(devices); let plan = [ crate::uinput::TextKey { code:30, shift:42 }, crate::uinput::TextKey { code:48, shift:42 }, crate::uinput::TextKey { code:2, shift:0 }, ]; assert_eq!(input.emit_uinput_text(&plan), 3); let keys: Vec<_> = crate::uinput::Devices::recorded_events_for_test(&mut keyboard).into_iter() .filter(|event| event.0 == 1).collect(); assert_eq!(keys, [(1,42,1),(1,30,1),(1,30,0),(1,48,1),(1,48,0),(1,42,0),(1,2,1),(1,2,0)]); assert_eq!(input.portal_input_counts_for_test(), [0; 3]); } #[test] fn partial_text_plan_releases_shift_and_does_not_disturb_held_keys() { let (devices, mut keyboard, _) = crate::uinput::Devices::recording_for_test(); let mut input = InputInjector::without_desktop(); input.uinput = Some(devices); assert_eq!(input.emit_uinput_text(&[ crate::uinput::TextKey { code:30, shift:42 }, crate::uinput::TextKey { code:0, shift:0 }, ]), 1); let keys: Vec<_> = crate::uinput::Devices::recorded_events_for_test(&mut keyboard).into_iter() .filter(|event| event.0 == 1).collect(); assert_eq!(keys, [(1,42,1),(1,30,1),(1,30,0),(1,42,0)]); input.held.lock().unwrap().keys.insert(0xffe3); assert_eq!(input.type_uinput_text("fixture"), 0); assert_eq!(input.held.lock().unwrap().keys.len(), 1); } #[test] #[ignore = "requires the current X11/XWayland layout; records events without desktop injection"] fn committed_ascii_text_bypasses_portal_with_live_keymap() { let fixture = "aAzZ09!@#$%^&*()_+-=[]{};:'\",.<>/?\\|`~\t\n"; assert_eq!(crate::uinput::text_plan(fixture).len(), fixture.len(), "fixture not available in idle host keymap"); let (devices, mut keyboard, _) = crate::uinput::Devices::recording_for_test(); let mut input = InputInjector::without_desktop(); input.uinput = Some(devices); let input = std::sync::Arc::new(input); let binding = input.portal.lock().unwrap(); let worker_input = input.clone(); let (done, received) = std::sync::mpsc::channel(); let worker = std::thread::spawn(move || { worker_input.text_input(fixture); let _ = done.send(()); }); // On failure panic with the binding held: poisoning prevents a fallback from touching the clipboard. assert!(received.recv_timeout(std::time::Duration::from_secs(2)).is_ok(), "text path waited for Portal/clipboard"); drop(binding); worker.join().unwrap(); let keys = crate::uinput::Devices::recorded_events_for_test(&mut keyboard); let down = keys.iter().filter(|e| e.0 == 1 && e.2 == 1).count(); let up = keys.iter().filter(|e| e.0 == 1 && e.2 == 0).count(); assert!(down >= fixture.len()); assert_eq!(down, up); assert_eq!(input.portal_input_counts_for_test(), [0; 3]); } #[test] fn full_uinput_bypasses_portal_binding_and_preserves_move_click_order() { for controller in [Controller::Other, Controller::Mobile, Controller::Ios] { let (devices, mut keyboard, mut pointer) = crate::uinput::Devices::recording_for_test(); let mut input = InputInjector::without_desktop(); input.uinput = Some(devices); input.uinput_pointer = true; let input = std::sync::Arc::new(input); // A blocked Portal must not stall any kernel-routed input. The recording // files exercise the real event writer without typing or clicking on the desktop. let binding = input.portal.lock().unwrap(); let worker_input = input.clone(); let (done, received) = std::sync::mpsc::channel(); let worker = std::thread::spawn(move || { worker_input.set_controller(controller); assert_eq!(worker_input.touch.lock().unwrap().mode, TouchMode::Mouse); worker_input.windows_key_event(65, true); worker_input.windows_key_event(65, false); worker_input.mouse_absolute_normalized(1.0, 0.0, (3072, 1920)); worker_input.wire_mouse_button(1, true); worker_input.wire_mouse_button(1, false); worker_input.mouse_scroll(120, 240); worker_input.flush_pointer(); let _ = done.send(()); }); let completed = received.recv_timeout(std::time::Duration::from_secs(1)); drop(binding); worker.join().unwrap(); assert!(completed.is_ok(), "kernel input waited for a Portal binding"); assert_eq!(input.portal_input_counts_for_test(), [0; 3]); let keys = crate::uinput::Devices::recorded_events_for_test(&mut keyboard); assert_eq!(keys, [(1, 30, 1), (0, 0, 0), (1, 30, 0), (0, 0, 0)]); let mouse = crate::uinput::Devices::recorded_events_for_test(&mut pointer); assert_eq!(&mouse[..7], &[ (3, 0, 65535), (3, 1, 0), (0, 0, 0), (1, 0x110, 1), (0, 0, 0), (1, 0x110, 0), (0, 0, 0), ]); assert_eq!(mouse.iter().filter(|e| e.0 == 2 && e.1 == 8).map(|e| e.2).sum::(), 2); assert_eq!(mouse.iter().filter(|e| e.0 == 2 && e.1 == 6).map(|e| e.2).sum::(), 1); } } #[test] fn full_uinput_clamps_coordinates_and_keeps_relative_motion_on_kernel() { let (devices, mut keyboard, mut pointer) = crate::uinput::Devices::recording_for_test(); let mut input = InputInjector::without_desktop(); input.uinput = Some(devices); input.uinput_pointer = true; *input.screen.lock().unwrap() = (3072, 1920); assert_eq!(input.inject_move(-5, 4000, false), 0); assert_eq!(crate::uinput::Devices::recorded_events_for_test(&mut pointer), [(3, 0, 0), (3, 1, 65535), (0, 0, 0)]); assert_eq!(input.inject_move(4, -3, true), 0); assert_eq!(crate::uinput::Devices::recorded_events_for_test(&mut keyboard), [(2, 0, 4), (2, 1, -3), (0, 0, 0)]); assert_eq!(input.portal_input_counts_for_test(), [0; 3]); } #[test] fn kde_output_count_ignores_disabled_and_disconnected_outputs() { assert_eq!(active_output_count(br#"{"outputs":[{"enabled":true,"connected":true},{"enabled":false,"connected":true}]}"#), Some(1)); assert_eq!(active_output_count(br#"{"outputs":[{"enabled":true,"connected":true},{"enabled":true,"connected":true}]}"#), Some(2)); assert_eq!(active_output_count(br#"{"outputs":[{"enabled":true,"connected":false}]}"#), Some(0)); assert_eq!(active_output_count(br#"{}"#), None); assert_eq!(active_output_count(b"invalid"), None); } } #[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::*; #[test] fn precise_scroll_accumulates_sub_notch_remainder() { let input = InputInjector::new(); input.mouse_scroll(0, 2); input.flush_pointer(); assert_eq!(input.pending.lock().unwrap().scroll,(0,2)); input.mouse_scroll(0,118); input.flush_pointer(); assert_eq!(input.pending.lock().unwrap().scroll,(0,0)); input.mouse_scroll(-125,-125); input.flush_pointer(); 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,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)->std::sync::Arc { 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 is_current(&self) -> bool { let owner = self.input.owner.lock().unwrap(); owner.owns(self.id) } /// Call only inside with_current or with_input, so a superseded peer cannot change bindings. pub fn bind_portal(&self, portal: Option>) { self.input.bind_portal(portal); } pub fn release_input(&self) { self.input.release_held(); } 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();} } /// Lifecycle updates survive a transient disconnect, but serialize with replacement and shutdown. pub fn with_current(&self, action:impl FnOnce()) { let owner=self.input.owner.lock().unwrap(); if owner.owns(self.id) {action();} } 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)] impl InputInjector { pub fn portal_input_counts_for_test(&self) -> [usize; 3] { std::array::from_fn(|i| self.portal_outcomes[i].load(std::sync::atomic::Ordering::Relaxed)) } pub fn without_desktop() -> Self { Self { mac_controller:std::sync::atomic::AtomicBool::new(false), cmd:std::sync::Mutex::new(CmdState::default()), serial:std::sync::Mutex::new(()), owner:std::sync::Mutex::new(DesktopOwner::default()), shortcuts:std::sync::Mutex::new(Shortcuts::default()), uinput:None, uinput_pointer:false, 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::DesktopBackend::Portal, pending:std::sync::Mutex::new(PendingPointer::default()), held:std::sync::Mutex::new(HeldInput::default()), touch:std::sync::Mutex::new(TouchDrag::default()), pointer_wake: tokio::sync::Notify::new() } } } /// Keysyms for mobile IME text: printable ASCII is its own keysym, newline is Return, tab is Tab. fn ascii_keysyms(text: &str) -> Option> { text.chars().map(|c| match c { '\n' => Some(0xff0d), '\t' => Some(0xff09), ' '..='~' => Some(c as u32), _ => None }).collect() } /// Whether an `org.gnome.desktop.input-sources` source list starts with an xkb layout that has /// printable ASCII in its first group. Mutter drops keysyms the active layout lacks, and an IBus /// engine (pinyin, ...) would compose the keys instead. fn first_source_types_ascii(sources: &str) -> bool { let Some(start)=sources.find('(') else { return false }; let Some(end)=sources[start..].find(')') else { return false }; let fields:Vec<&str>=sources[start+1..start+end].split(',').map(|f| f.trim().trim_matches('\'')).collect(); let [kind, layout]=fields[..] else { return false }; let (base, variant)=layout.split_once('+').unwrap_or((layout, "")); const NON_LATIN: &[&str]=&["af","am","ara","bd","bg","bt","by","eg","et","ge","gr","il","in","iq","ir","kg","kh","kz","la","lk","mk","mm","mn","mv","np","ru","sy","th","tj","ua"]; kind=="xkb" && !NON_LATIN.contains(&base) && !variant.contains("cyrillic") && (base!="rs" || variant.contains("latin")) } fn uinput_layout_types_ascii(desktop: &str, gnome_layout: impl FnOnce() -> bool) -> bool { !desktop.split(':').any(|name| name.eq_ignore_ascii_case("gnome")) || gnome_layout() } /// GNOME's active input source is the first of `mru-sources` (else of `sources`). Cached for 2 s. fn gnome_layout_types_ascii() -> bool { #[cfg(test)] { false } #[cfg(not(test))] { static CACHE: std::sync::Mutex> = std::sync::Mutex::new(None); let mut cache=CACHE.lock().unwrap(); if let Some((at,value))=*cache { if at.elapsed()serde_json::Value { let output=std::process::Command::new("hyprctl").args(["cursorpos","-j"]).output().unwrap(); assert!(output.status.success());serde_json::from_slice(&output.stdout).unwrap() } struct Restore(serde_json::Value); impl Drop for Restore { fn drop(&mut self) { let _=std::process::Command::new("hyprctl").args(["dispatch","movecursor",&self.0["x"].to_string(),&self.0["y"].to_string()]).output(); } } let _restore=Restore(cursor()); crate::wayland::init().unwrap(); let input=InputInjector::new(); assert!(input.uinput.is_some(),"test must exercise the uinput/native hybrid path"); // libinput discovers the newly created virtual devices asynchronously. std::thread::sleep(std::time::Duration::from_secs(1)); let output=std::process::Command::new("hyprctl").args(["monitors","-j"]).output().unwrap(); let monitors:Vec=serde_json::from_slice(&output.stdout).unwrap(); let outputs=crate::wayland::outputs();assert!(outputs.len()>=2,"needs at least two outputs"); for (id,output) in outputs.iter().enumerate().chain(outputs.iter().enumerate().rev()) { crate::wayland::select(id).unwrap(); let monitor=monitors.iter().find(|m|m["name"].as_str()==Some(output.name.as_str())).unwrap(); let scale=monitor["scale"].as_f64().unwrap(); for (x,y) in [(0.1,0.1),(0.5,0.5),(0.9,0.9),(0.1,0.9),(0.9,0.1)] { let size=(output.width,output.height); let pixels=crate::input_mapping::normalized_pointer(x,y,size.0,size.1).unwrap(); assert_eq!(input.backend,crate::desktop_backend::DesktopBackend::Wayland); input.mouse_absolute_normalized(x,y,size);input.flush_pointer(); let expected=(monitor["x"].as_f64().unwrap()+f64::from(pixels.0)/scale, monitor["y"].as_f64().unwrap()+f64::from(pixels.1)/scale); // Motion is asynchronous; observe its first arrival instead of a // later position potentially changed by another input device. let deadline=std::time::Instant::now()+std::time::Duration::from_millis(250); loop { let actual=cursor(); let matched=(actual["x"].as_f64().unwrap()-expected.0).abs()<=2.0 && (actual["y"].as_f64().unwrap()-expected.1).abs()<=2.0; if matched {break;} assert!(std::time::Instant::now()=2,"needs two X11 monitors"); for (id,output) in outputs.iter().enumerate().chain(outputs.iter().enumerate().rev()) { crate::wayland::select(id).unwrap(); for (x,y) in [(0.0,0.0),(0.5,0.5),(0.999,0.999),(0.1,0.9)] { let size=(output.width,output.height); let pixels=crate::input_mapping::normalized_pointer(x,y,size.0,size.1).unwrap(); input.mouse_absolute_normalized(x,y,size);input.flush_pointer(); let (mut ax,mut ay,mut mask)=(0,0,0); assert_eq!(unsafe {x11_pointer_state(&mut ax,&mut ay,&mut mask)},0); assert_eq!((ax,ay),(output.x+pixels.0,output.y+pixels.1),"monitor={} normalized=({x},{y})",output.name); } } crate::wayland::select(0).unwrap(); } } #[cfg(test)] mod clipboard_paste_text_tests { use super::*; #[test] fn rejects_empty_and_oversized_text() { let input = InputInjector::new(); assert!(!input.set_clipboard_text("")); let oversized = "a".repeat(crate::clipboard::MAX_TEXT + 1); assert!(!input.set_clipboard_text(&oversized)); } #[test] fn text_input_handles_portal_backend_safely_without_side_effects() { let mut input = InputInjector::new(); input.backend = crate::desktop_backend::DesktopBackend::Portal; // In unit tests, set_clipboard_text avoids external commands so this // will not mutate the host's actual clipboard. input.text_input("测试输入法文字"); } #[test] fn text_input_respects_disable_env() { let mut input = InputInjector::new(); input.backend = crate::desktop_backend::DesktopBackend::Portal; std::env::set_var("UU_WAYLAND_TEXT", "0"); input.text_input("测试输入法文字"); std::env::remove_var("UU_WAYLAND_TEXT"); } }