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): normalize list_windows z-index parity (#2172)

injaneitye0f18dce88fb
diff --git a/libs/cua-driver/rust/crates/platform-linux/src/x11/mod.rs b/libs/cua-driver/rust/crates/platform-linux/src/x11/mod.rs
index 90e944ade3..0bf5a01fd6 100644
--- a/libs/cua-driver/rust/crates/platform-linux/src/x11/mod.rs
+++ b/libs/cua-driver/rust/crates/platform-linux/src/x11/mod.rs
@@ -93,12 +93,12 @@ fn list_windows_inner(filter_pid: Option<u32>) -> Result<Vec<WindowInfo>> {
let screen = &conn.setup().roots[screen_num];
let root = screen.root;
- // Get _NET_CLIENT_LIST_STACKING (or fallback to _NET_CLIENT_LIST).
+ // Prefer _NET_CLIENT_LIST_STACKING; an unordered client list reports null z_index.
let (windows, source) = get_window_list(&conn, root)?;
let owners = OwnerResolver::new(&conn);
let mut result = Vec::new();
- for (z_index, xid) in windows.into_iter().enumerate() {
+ for xid in windows {
let pid = owners.owner_pid(xid);
if let Some(fp) = filter_pid {
if pid != Some(fp) {
@@ -137,9 +137,10 @@ fn list_windows_inner(filter_pid: Option<u32>) -> Result<Vec<WindowInfo>> {
app_name,
title,
is_on_screen,
- // EWMH stacking lists are bottom-to-top, so the enumeration index
- // already follows the shared "higher z_index is frontmost" contract.
- z_index: Some(z_index),
+ // Stacking sources are bottom-to-top, so the position among the
+ // listed windows follows the shared "higher z_index is frontmost"
+ // contract and stays contiguous after filtering.
+ z_index: source.is_stacked().then_some(result.len()),
x,
y,
width: w,
@@ -152,17 +153,26 @@ fn list_windows_inner(filter_pid: Option<u32>) -> Result<Vec<WindowInfo>> {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum WindowSource {
+ /// Managed clients in bottom-to-top stacking order.
ClientList,
+ /// `_NET_CLIENT_LIST`: managed clients in mapping order, not stacking order.
+ UnorderedClientList,
RootChildren,
}
+impl WindowSource {
+ fn is_stacked(self) -> bool {
+ self != WindowSource::UnorderedClientList
+ }
+}
+
/// A window the window manager manages is listed even without a title: GTK
/// message dialogs (a LibreOffice or GIMP confirmation) are untitled, and one
/// is often the modal window holding the focus. Without a window manager the
/// title is the only hint that a mapped root child is an application window
/// rather than a popup or tooltip.
fn window_is_listable(source: WindowSource, title: &str) -> bool {
- source == WindowSource::ClientList || !title.trim().is_empty()
+ source != WindowSource::RootChildren || !title.trim().is_empty()
}
pub(crate) fn get_window_list(
@@ -170,8 +180,11 @@ pub(crate) fn get_window_list(
root: Window,
) -> Result<(Vec<Window>, WindowSource)> {
let mut ewmh = false;
- let atom_names = ["_NET_CLIENT_LIST_STACKING", "_NET_CLIENT_LIST"];
- for name in &atom_names {
+ let atoms = [
+ ("_NET_CLIENT_LIST_STACKING", WindowSource::ClientList),
+ ("_NET_CLIENT_LIST", WindowSource::UnorderedClientList),
+ ];
+ for (name, source) in atoms {
if let Ok(atom) = get_atom(conn, name) {
if let Ok(reply) = conn
.get_property(false, root, atom, AtomEnum::WINDOW, 0, u32::MAX)?
@@ -183,7 +196,7 @@ pub(crate) fn get_window_list(
.map(|iter| iter.collect())
.unwrap_or_default();
if client_list_property(reply.type_, windows.as_slice()).is_some() {
- return Ok((windows, WindowSource::ClientList));
+ return Ok((windows, source));
}
}
}
@@ -956,6 +969,7 @@ mod tests {
fn untitled_windows_are_listed_only_when_the_window_manager_manages_them() {
assert!(window_is_listable(WindowSource::ClientList, ""));
assert!(window_is_listable(WindowSource::ClientList, "Save Changes"));
+ assert!(window_is_listable(WindowSource::UnorderedClientList, ""));
assert!(window_is_listable(
WindowSource::RootChildren,
"Save Changes"
@@ -963,6 +977,13 @@ mod tests {
assert!(!window_is_listable(WindowSource::RootChildren, " "));
}
+ #[test]
+ fn only_the_unordered_client_list_lacks_stacking_order() {
+ assert!(WindowSource::ClientList.is_stacked());
+ assert!(WindowSource::RootChildren.is_stacked());
+ assert!(!WindowSource::UnorderedClientList.is_stacked());
+ }
+
#[test]
fn query_tree_fallback_only_lists_viewable_windows() {
assert!(fallback_window_is_listable(MapState::VIEWABLE));
diff --git a/libs/cua-driver/rust/crates/platform-macos/src/tools/list_windows.rs b/libs/cua-driver/rust/crates/platform-macos/src/tools/list_windows.rs
index 1c7449e479..c026f66916 100644
--- a/libs/cua-driver/rust/crates/platform-macos/src/tools/list_windows.rs
+++ b/libs/cua-driver/rust/crates/platform-macos/src/tools/list_windows.rs
@@ -76,6 +76,7 @@ impl Tool for ListWindowsTool {
});
}
crate::windows::retain_ax_reachable(&mut windows, current_space_id);
+ crate::windows::normalize_z_indices(&mut windows);
let windows_json: Vec<Value> = windows.iter().map(window_record_json).collect();
@@ -128,6 +129,21 @@ pub(super) fn window_record_json(w: &crate::windows::WindowInfo) -> Value {
mod tests {
use super::*;
+ #[test]
+ fn filtered_windows_receive_contiguous_zero_based_z_indices() {
+ let mut windows = vec![
+ window(1, 100, "A", "front", true),
+ window(2, 200, "B", "middle", true),
+ window(3, 100, "A", "back", true),
+ ];
+ windows.retain(|window| window.pid == 100);
+
+ crate::windows::normalize_z_indices(&mut windows);
+
+ let z: Vec<_> = windows.iter().map(|window| window.z_index).collect();
+ assert_eq!(z, [1, 0]);
+ }
+
#[test]
fn window_record_includes_observed_z_index() {
let window = crate::windows::WindowInfo {
diff --git a/libs/cua-driver/rust/crates/platform-macos/src/windows.rs b/libs/cua-driver/rust/crates/platform-macos/src/windows.rs
index 7c2841fdb9..8e52886f3d 100644
--- a/libs/cua-driver/rust/crates/platform-macos/src/windows.rs
+++ b/libs/cua-driver/rust/crates/platform-macos/src/windows.rs
@@ -220,10 +220,9 @@ fn enumerate_windows(
}
let raw: CFArray<CFTypeRef> = unsafe { CFArray::wrap_under_create_rule(raw_ref as _) };
- let total = raw.len() as usize;
let mut results = Vec::new();
- for (idx, item) in raw.iter().enumerate() {
+ for item in raw.iter() {
let item = *item;
// Each item should be a CFDictionary.
let dict_type = CFDictionary::<*const c_void, *const c_void>::type_id();
@@ -323,9 +322,6 @@ fn enumerate_windows(
})
};
- // z_index: CGWindowList front-to-back → assign reverse index.
- let z_index = cua_driver_core::window_target::z_index_from_front_to_back(total, idx);
-
results.push(WindowInfo {
window_id,
pid,
@@ -333,7 +329,7 @@ fn enumerate_windows(
title,
bounds,
layer,
- z_index,
+ z_index: 0,
is_on_screen,
current_space_id: None,
on_current_space: None,
@@ -341,6 +337,8 @@ fn enumerate_windows(
});
}
+ normalize_z_indices(&mut results);
+
if let Some(query) = &space_query {
for window in &mut results {
let space_ids = query.window_space_ids(window.window_id);
@@ -357,6 +355,16 @@ fn enumerate_windows(
}
}
+/// Number a front-to-back window list with the shared `z_index` contract.
+/// Run it after every filter so the returned values stay contiguous.
+pub(crate) fn normalize_z_indices(windows: &mut [WindowInfo]) {
+ let total = windows.len();
+ for (position, window) in windows.iter_mut().enumerate() {
+ window.z_index =
+ cua_driver_core::window_target::z_index_from_front_to_back(total, position);
+ }
+}
+
fn apply_window_space_metadata(
window: &mut WindowInfo,
space_ids: Option<Vec<u64>>,