From fe90ee6cd3d260f6160eb435c4dac9fd5bb6caca Mon Sep 17 00:00:00 2001 From: Mariano Abad Date: Wed, 23 Sep 2026 22:20:00 -0300 Subject: [PATCH] input: remember the last absolute peer move and forget it on any other move The DRM cursor calibration subtracts a cursor plane origin from the point this process injected, so the point has to be the post-remap uinput one and nothing else: LATEST_PEER_INPUT_CURSOR stores an X-server coordinate on the relative arm. The new sample carries an Instant, a sequence number (two measurements against one sample are not independent) and the layout generation it was mapped against. A relative delta and a move this process makes itself forget it. display_service exposes the existing drift flag to readers. --- src/server/display_service.rs | 15 ++++++ src/server/input_service.rs | 98 ++++++++++++++++++++++++++++++++++- 2 files changed, 112 insertions(+), 1 deletion(-) diff --git a/src/server/display_service.rs b/src/server/display_service.rs index 16798b277..1e7f403f4 100644 --- a/src/server/display_service.rs +++ b/src/server/display_service.rs @@ -136,6 +136,21 @@ impl WaylandLayout { #[cfg(target_os = "linux")] static WAYLAND_LAYOUT_DRIFTED: AtomicBool = AtomicBool::new(false); +/// True while the layout has drifted from the session-init baseline AND the matching uinput +/// range was applied, which is the condition `remap_wayland_uinput_coord` remaps under. While +/// it holds, an injected point has been moved onto the live layout and no longer lives in the +/// layout the DRM cursor calibration resolved its geometry from, so the calibration declines +/// to measure. It is a secondary guard: the snapshot generation is what stops a measurement +/// once the layout moved, and this covers the window in which the remap is active and the +/// promotion that re-baselines is still owed. Both are written by the poll below, so a layout +/// change is seen up to one check interval late; inside that window the bitmap bound in the +/// measurement is the only guard, and a value that passes it is replaced by the next confirmed +/// pair. A failed range apply stores false, and then nothing is remapped either. +#[cfg(all(target_os = "linux", feature = "drm"))] +pub(crate) fn wayland_layout_drifted() -> bool { + WAYLAND_LAYOUT_DRIFTED.load(Ordering::Relaxed) +} + #[cfg(target_os = "linux")] pub(super) fn set_wayland_uinput_rect(rect: (i32, i32, i32, i32)) { WAYLAND_UINPUT_RECT.lock().unwrap().rect = Some(rect); diff --git a/src/server/input_service.rs b/src/server/input_service.rs index 3bf1ac664..7c2d0231f 100644 --- a/src/server/input_service.rs +++ b/src/server/input_service.rs @@ -1077,6 +1077,58 @@ fn fix_modifiers(modifiers: &[EnumOrUnknown], en: &mut Enigo, ck: i3 } } +/// The last ABSOLUTE peer-injected pointer position, in the post-remap uinput layout space, with +/// when it was injected, a sequence number and the wayland snapshot generation it was mapped +/// against. Deliberately NOT `LATEST_PEER_INPUT_CURSOR`: its relative arm stores +/// `get_cursor_pos()`, an X-server coordinate that never went through the layout remap, and the +/// DRM cursor calibration subtracts a cursor-plane position from this, so the two spaces must +/// match. Only the uinput absolute path writes here. +#[cfg(all(target_os = "linux", feature = "drm"))] +static LATEST_PEER_ABS_POS: std::sync::Mutex> = + std::sync::Mutex::new(None); +#[cfg(all(target_os = "linux", feature = "drm"))] +static PEER_ABS_SEQ: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0); + +/// One absolute peer sample, as the calibration reads it. +#[cfg(all(target_os = "linux", feature = "drm"))] +#[derive(Clone, Copy, Debug, PartialEq)] +pub(crate) struct PeerAbsSample { + pub pos: (i32, i32), + pub age_ms: u64, + /// Distinct per injected move. Two measurements taken against the same sample are not + /// independent, whatever the cursor plane did in between. + pub seq: u64, + /// The layout generation the point was mapped against. + pub gen: u64, +} + +/// The peer moved the pointer to an absolute, post-remap position. +#[cfg(all(target_os = "linux", feature = "drm"))] +pub(crate) fn note_peer_absolute_move(x: i32, y: i32) { + let seq = PEER_ABS_SEQ.fetch_add(1, Ordering::Relaxed) + 1; + let gen = scrap::wayland::display::wayland_snapshot_generation(); + *LATEST_PEER_ABS_POS.lock().unwrap() = Some(((x, y), std::time::Instant::now(), seq, gen)); +} + +/// The pointer moved by a path that has no post-remap absolute position to offer: a relative +/// delta from the peer, or a move this process made on its own. The last sample no longer says +/// where the pointer is; forget it, the next absolute move restores it. +#[cfg(all(target_os = "linux", feature = "drm"))] +pub(crate) fn note_pointer_moved_without_absolute_sample() { + *LATEST_PEER_ABS_POS.lock().unwrap() = None; +} + +#[cfg(all(target_os = "linux", feature = "drm"))] +pub(crate) fn last_peer_abs_sample() -> Option { + let (pos, at, seq, gen) = (*LATEST_PEER_ABS_POS.lock().unwrap())?; + Some(PeerAbsSample { + pos, + age_ms: at.elapsed().as_millis() as u64, + seq, + gen, + }) +} + // Update time to avoid send cursor position event to the peer. // See `run_pos` --> `set_cursor_position` --> `exclude` #[inline] @@ -1249,7 +1301,9 @@ pub fn handle_mouse_simulation_(evt: &MouseEvent, conn: i32) { // told at session init. If the compositor has since moved a monitor, correct // them onto the current layout. https://github.com/rustdesk/rustdesk/issues/15601 #[cfg(target_os = "linux")] - let (mx, my) = if wayland_use_uinput() { + let uinput = wayland_use_uinput(); + #[cfg(target_os = "linux")] + let (mx, my) = if uinput { super::display_service::remap_wayland_uinput_coord(evt.x, evt.y) } else { (evt.x, evt.y) @@ -1257,6 +1311,11 @@ pub fn handle_mouse_simulation_(evt: &MouseEvent, conn: i32) { #[cfg(not(target_os = "linux"))] let (mx, my) = (evt.x, evt.y); en.mouse_move_to(mx, my); + // Only the uinput point is in the layout space the calibration measures in. + #[cfg(all(target_os = "linux", feature = "drm"))] + if uinput { + note_peer_absolute_move(mx, my); + } *LATEST_PEER_INPUT_CURSOR.lock().unwrap() = Input { conn, time: get_time(), @@ -1279,6 +1338,10 @@ pub fn handle_mouse_simulation_(evt: &MouseEvent, conn: i32) { let dy = evt .y .clamp(-MAX_RELATIVE_MOUSE_DELTA, MAX_RELATIVE_MOUSE_DELTA); + // The absolute sample stops describing the pointer the moment a delta lands. Drop it + // BEFORE the move, so no frame in between can measure the plane against it. + #[cfg(all(target_os = "linux", feature = "drm"))] + note_pointer_moved_without_absolute_sample(); en.mouse_move_relative(dx, dy); // Get actual cursor position after relative movement for tracking if let Some((x, y)) = crate::get_cursor_pos() { @@ -2496,6 +2559,9 @@ impl TemporaryMouseMoveHandle { let thread_handle = std::thread::spawn(move || { log::debug!("TemporaryMouseMoveHandle thread started"); for (x, y) in rx { + // Not the peer's position: the calibration must not subtract from it. + #[cfg(feature = "drm")] + note_pointer_moved_without_absolute_sample(); ENIGO.lock().unwrap().mouse_move_to(x, y); } log::debug!("TemporaryMouseMoveHandle thread exiting"); @@ -2724,3 +2790,33 @@ mod cursor_shape_tests { ); } } + +#[cfg(all(test, target_os = "linux", feature = "drm"))] +mod peer_abs_sample_tests { + use super::*; + + // The two writers share one static and libtest runs tests concurrently. + static SERIAL: std::sync::Mutex<()> = std::sync::Mutex::new(()); + + #[test] + fn a_move_without_an_absolute_sample_forgets_the_last_one() { + let _serial = SERIAL.lock().unwrap_or_else(|p| p.into_inner()); + note_peer_absolute_move(20, 30); + let s = last_peer_abs_sample().expect("an absolute move leaves a sample"); + assert_eq!(s.pos, (20, 30)); + assert!(s.age_ms < 1_000); + note_pointer_moved_without_absolute_sample(); + assert_eq!(last_peer_abs_sample(), None); + } + + #[test] + fn every_absolute_move_is_a_new_sample() { + let _serial = SERIAL.lock().unwrap_or_else(|p| p.into_inner()); + note_peer_absolute_move(1, 1); + let a = last_peer_abs_sample().unwrap().seq; + note_peer_absolute_move(1, 1); + let b = last_peer_abs_sample().unwrap().seq; + assert!(b > a, "same point, distinct sample"); + note_pointer_moved_without_absolute_sample(); + } +}