From 59931e19d18fac4452dc311210029d501338fcc4 Mon Sep 17 00:00:00 2001 From: rustdesk Date: Thu, 24 Sep 2026 23:34:10 +0800 Subject: [PATCH] cursor: let the macOS seed report a change, and the content name the cursor macOS named a cursor by a fingerprint of its size, hotspot and two pixels near the hotspot. It collides between standard cursors: the open and the closed hand give one value, and the arrows with a badge (copy, not allowed, contextual menu, disappearing item) another, so a switch between them was never sent, or a cached shape of the other was. CGSCurrentCursorSeed already says when the cursor changed; get_cursor now reports the seed, and the shape is named by its content, as on every platform. The fingerprint is gone. get_cursor_data checks the seed before and after the capture. The seed was taken before capturing, so a capture that found the cursor changed again lost the change; it now leaves it for the next poll. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01EZ49AbZJYfm8NTp5yDPMab --- src/platform/macos.rs | 68 +++++++++++-------------------------------- src/platform/mod.rs | 11 +++++++ 2 files changed, 28 insertions(+), 51 deletions(-) diff --git a/src/platform/macos.rs b/src/platform/macos.rs index 82bcf9d6f..62832dac3 100644 --- a/src/platform/macos.rs +++ b/src/platform/macos.rs @@ -559,6 +559,9 @@ pub fn get_cursor() -> ResultType> { autoreleasepool(|| unsafe_get_cursor()) } +// The seed names a change of cursor, not a cursor: the shape is named by its content once +// captured, as on every platform. A fingerprint of its size, hotspot and two pixels named the open +// and the closed hand alike, and the arrows with a badge, so a switch between them went unsent. fn unsafe_get_cursor() -> ResultType> { unsafe { let seed = CGSCurrentCursorSeed(); @@ -566,9 +569,8 @@ fn unsafe_get_cursor() -> ResultType> { return Ok(None); } LATEST_SEED = seed; + Ok(Some(seed as u32 as u64)) } - let c = get_cursor_id()?; - Ok(Some(c.1)) } pub fn reset_input_cache() { @@ -577,53 +579,6 @@ pub fn reset_input_cache() { } } -fn get_cursor_id() -> ResultType<(id, u64)> { - unsafe { - let c: id = msg_send![class!(NSCursor), currentSystemCursor]; - if c == nil { - bail!("Failed to call [NSCursor currentSystemCursor]"); - } - let hotspot: NSPoint = msg_send![c, hotSpot]; - let img: id = msg_send![c, image]; - if img == nil { - bail!("Failed to call [NSCursor image]"); - } - let size: NSSize = msg_send![img, size]; - let tif: id = msg_send![img, TIFFRepresentation]; - if tif == nil { - bail!("Failed to call [NSImage TIFFRepresentation]"); - } - let rep: id = msg_send![class!(NSBitmapImageRep), imageRepWithData: tif]; - if rep == nil { - bail!("Failed to call [NSBitmapImageRep imageRepWithData]"); - } - let rep_size: NSSize = msg_send![rep, size]; - let mut hcursor = - size.width + size.height + hotspot.x + hotspot.y + rep_size.width + rep_size.height; - let x = (rep_size.width * hotspot.x / size.width) as usize; - let y = (rep_size.height * hotspot.y / size.height) as usize; - for i in 0..2 { - let mut x2 = x + i; - if x2 >= rep_size.width as usize { - x2 = rep_size.width as usize - 1; - } - let mut y2 = y + i; - if y2 >= rep_size.height as usize { - y2 = rep_size.height as usize - 1; - } - let color: id = msg_send![rep, colorAtX:x2 y:y2]; - if color != nil { - let r: f64 = msg_send![color, redComponent]; - let g: f64 = msg_send![color, greenComponent]; - let b: f64 = msg_send![color, blueComponent]; - let a: f64 = msg_send![color, alphaComponent]; - hcursor += (r + g + b + a) * (255 << i) as f64; - } - } - Ok((c, hcursor as _)) - } -} - pub fn get_cursor_data(hcursor: u64) -> ResultType { autoreleasepool(|| unsafe_get_cursor_data(hcursor)) } @@ -631,10 +586,17 @@ pub fn get_cursor_data(hcursor: u64) -> ResultType { // https://github.com/stweil/OSXvnc/blob/master/OSXvnc-server/mousecursor.c fn unsafe_get_cursor_data(hcursor: u64) -> ResultType { unsafe { - let (c, hcursor2) = get_cursor_id()?; - if hcursor != hcursor2 { + // Captured only while the cursor is the change `hcursor` names. One that changed again is + // left for get_cursor to report again, or the change would be lost. + let changed = || CGSCurrentCursorSeed() as u32 as u64 != hcursor; + if changed() { + LATEST_SEED = 0; bail!("cursor changed"); } + let c: id = msg_send![class!(NSCursor), currentSystemCursor]; + if c == nil { + bail!("Failed to call [NSCursor currentSystemCursor]"); + } let hotspot: NSPoint = msg_send![c, hotSpot]; let img: id = msg_send![c, image]; let size: NSSize = msg_send![img, size]; @@ -674,6 +636,10 @@ fn unsafe_get_cursor_data(hcursor: u64) -> ResultType { colors.push((a * 255.) as _); } } + if changed() { + LATEST_SEED = 0; + bail!("cursor changed"); + } Ok(CursorData { id: hcursor, colors: colors.into(), diff --git a/src/platform/mod.rs b/src/platform/mod.rs index 7a7ddc035..a92dc3677 100644 --- a/src/platform/mod.rs +++ b/src/platform/mod.rs @@ -228,6 +228,17 @@ mod tests { #[cfg(target_os = "macos")] macos::is_process_trusted(false); } + // macOS reports a change of cursor, and a capture that finds the cursor changed again + // leaves the change to be reported once more, not lost. + #[cfg(target_os = "macos")] + { + macos::reset_input_cache(); + let change = get_cursor().unwrap().expect("a change after a reset"); + assert_eq!(get_cursor().unwrap(), None, "nothing changed since"); + assert!(get_cursor_data(change).is_ok()); + assert!(get_cursor_data(change.wrapping_add(1)).is_err()); + assert!(get_cursor().unwrap().is_some(), "the change is reported again"); + } } #[test] fn test_get_cursor_pos() {