Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

fix(cua-driver): refuse background window-move drags on Windows (#2559)

ngnichtel68b8943c5c70
diff --git a/libs/cua-driver/rust/crates/platform-windows/src/input/mouse.rs b/libs/cua-driver/rust/crates/platform-windows/src/input/mouse.rs
index 6b9aec6c7d..0bc83e52f6 100644
--- a/libs/cua-driver/rust/crates/platform-windows/src/input/mouse.rs
+++ b/libs/cua-driver/rust/crates/platform-windows/src/input/mouse.rs
@@ -950,6 +950,87 @@ pub fn move_cursor_desktop(x: i32, y: i32) -> Result<()> {
.map_err(|error| anyhow::anyhow!("SetCursorPos({x}, {y}) failed: {error}"))
}
+/// Classify a `WM_NCHITTEST` result into the non-client regions where a
+/// posted-message drag can NEVER work: the OS handles caption and border
+/// drags with its interactive move/resize modal loop
+/// (`WM_NCLBUTTONDOWN` → `DefWindowProc` → `WM_SYSCOMMAND SC_MOVE/SC_SIZE`),
+/// which only real pointer input off the system input queue can drive.
+/// Posted `WM_MOUSEMOVE` streams are simply discarded, so background
+/// delivery must refuse with `background_unavailable` instead of reporting
+/// a success that moved nothing.
+pub fn classify_nc_move_resize_hit(hit: isize) -> Option<&'static str> {
+ // Constant values per winuser.h; kept as literals because the windows
+ // crate exposes hit-test codes as u32 while SendMessage returns LRESULT.
+ match hit {
+ 2 => Some("caption / title bar"), // HTCAPTION
+ 4 => Some("size box"), // HTGROWBOX / HTSIZE
+ 10..=17 => Some("resize border"), // HTLEFT..HTBOTTOMRIGHT
+ _ => None,
+ }
+}
+
+/// Screen-coordinate `WM_NCHITTEST` against the drag target's top-level
+/// window. Returns the human-readable region name when the point falls on a
+/// caption / resize border (the regions a posted drag cannot actuate), else
+/// `None`. Uses `SendMessageTimeoutW(SMTO_ABORTIFHUNG)` so a hung target
+/// cannot wedge the daemon; timeouts fail open (`None`) — the posted drag
+/// then proceeds exactly as before this check existed.
+pub fn non_client_move_resize_hit(root: u64, sx: i32, sy: i32) -> Option<&'static str> {
+ use windows::Win32::UI::WindowsAndMessaging::{
+ SendMessageTimeoutW, SMTO_ABORTIFHUNG, WM_NCHITTEST,
+ };
+ if root == 0 {
+ return None;
+ }
+ let hwnd = HWND(root as *mut _);
+ // Hit-test the top-level frame: the caption belongs to the root window
+ // even when the caller resolved a child HWND as the drag target.
+ let top = unsafe { GetAncestor(hwnd, GA_ROOT) };
+ let top = if top.0.is_null() { hwnd } else { top };
+ // WM_NCHITTEST takes SCREEN coordinates packed in LPARAM (signed 16-bit
+ // each — correct for any virtual-desktop position within ±32767).
+ let lp = LPARAM((((sy as i16 as u16 as u32) << 16) | (sx as i16 as u16 as u32)) as isize);
+ let mut result: usize = 0;
+ let ok = unsafe {
+ SendMessageTimeoutW(
+ top,
+ WM_NCHITTEST,
+ WPARAM(0),
+ lp,
+ SMTO_ABORTIFHUNG,
+ 200, // ms — a live window answers this in microseconds
+ Some(&mut result),
+ )
+ };
+ if ok.0 == 0 {
+ return None; // timed out / hung target: fail open, post as before
+ }
+ classify_nc_move_resize_hit(result as isize)
+}
+
+#[cfg(test)]
+mod nc_hit_tests {
+ use super::classify_nc_move_resize_hit;
+
+ #[test]
+ fn caption_and_borders_refuse_posted_drags_client_area_does_not() {
+ assert_eq!(
+ classify_nc_move_resize_hit(2), // HTCAPTION
+ Some("caption / title bar")
+ );
+ assert_eq!(classify_nc_move_resize_hit(4), Some("size box")); // HTGROWBOX
+ for border in 10..=17 {
+ // HTLEFT..HTBOTTOMRIGHT
+ assert_eq!(classify_nc_move_resize_hit(border), Some("resize border"));
+ }
+ assert_eq!(classify_nc_move_resize_hit(1), None); // HTCLIENT
+ assert_eq!(classify_nc_move_resize_hit(0), None); // HTNOWHERE
+ assert_eq!(classify_nc_move_resize_hit(3), None); // HTSYSMENU (click, not drag-move)
+ assert_eq!(classify_nc_move_resize_hit(-1), None); // HTERROR
+ assert_eq!(classify_nc_move_resize_hit(18), None); // HTBORDER (non-resizable edge)
+ }
+}
+
#[cfg(test)]
mod wheel_tests {
use super::{posted_press_message, wheel_mouse_data, WHEEL_DELTA};
diff --git a/libs/cua-driver/rust/crates/platform-windows/src/tools/impl_.rs b/libs/cua-driver/rust/crates/platform-windows/src/tools/impl_.rs
index 701a22867e..b599b94d05 100644
--- a/libs/cua-driver/rust/crates/platform-windows/src/tools/impl_.rs
+++ b/libs/cua-driver/rust/crates/platform-windows/src/tools/impl_.rs
@@ -6224,7 +6224,10 @@ impl Tool for DragTool {
name: "drag".into(),
description: "Press-drag-release gesture from (from_x, from_y) to (to_x, to_y) in window-local screenshot pixels. \
duration_ms (default 500) is the wall-clock budget; steps (default 20) interpolates intermediate \
- WM_MOUSEMOVE events along the path. No focus steal. After a zoom call, pass from_zoom=true.".into(),
+ WM_MOUSEMOVE events along the path. No focus steal. After a zoom call, pass from_zoom=true. \
+ Drags that start on a window caption / title bar or resize border (i.e. moving or resizing the \
+ window itself) cannot be delivered in the background — the OS move/resize loop needs real pointer \
+ input — so they return background_unavailable; re-issue those with delivery_mode:\"foreground\".".into(),
input_schema: json!({"type":"object","required":["from_x","from_y","to_x","to_y"],"properties":{
"session": cua_driver_core::tool_schema::session_schema(),
"scope":{"type":"string","enum":["window","desktop"],"description":"Use desktop with no pid/window_id for screen-absolute coordinates."},
@@ -6486,6 +6489,33 @@ impl Tool for DragTool {
};
}
+ // Background posted-message drags cannot move or resize a top-level
+ // window: a caption/border drag is handled by the OS's interactive
+ // move/resize modal loop, which only real pointer input can drive —
+ // the posted WM_MOUSEMOVE stream is discarded and the "success" is a
+ // verified no-op. Hit-test the start point and refuse with the
+ // structured background_unavailable error so callers escalate to
+ // delivery_mode:"foreground" per the documented ladder instead of
+ // trusting a drag that did nothing.
+ let nc_hit = tokio::task::spawn_blocking(move || {
+ crate::input::mouse::non_client_move_resize_hit(hwnd, sx_from, sy_from)
+ })
+ .await
+ .ok()
+ .flatten();
+ if let Some(region) = nc_hit {
+ return crate::input::delivery::background_unavailable_error_with_cause(
+ hwnd,
+ EventKind::MouseMove,
+ format!(
+ "the drag starts on the window's {region} (WM_NCHITTEST), and moving \
+ or resizing a top-level window requires the OS interactive \
+ move/resize loop, which posted mouse messages cannot drive. \
+ Re-issue the same drag with delivery_mode:\"foreground\"."
+ ),
+ );
+ }
+
// Pin the agent-cursor overlay above the drag target so the synthetic
// cursor stays sandwiched at z+1 of the dragged window for the full
// path. Drag stays within a single HWND, so one pin at the start is