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): wait for the keymap restore before closing the connection (#4981)

injaneity160ac57df22d
diff --git a/libs/cua-driver/rust/crates/platform-linux/src/input/mod.rs b/libs/cua-driver/rust/crates/platform-linux/src/input/mod.rs
index 12c2bb6047..d1cd425be4 100644
--- a/libs/cua-driver/rust/crates/platform-linux/src/input/mod.rs
+++ b/libs/cua-driver/rust/crates/platform-linux/src/input/mod.rs
@@ -4143,17 +4143,13 @@ pub(crate) struct RemappedKeycode<'a> {
impl Drop for RemappedKeycode<'_> {
fn drop(&mut self) {
- // Best-effort restore: re-install the original keysyms for this keycode
- // and flush. Errors are swallowed deliberately — Drop must not panic in
- // the daemon, and the worst case of a failed restore is a single spare
- // keycode left mapped (it was unused to begin with), never a crash.
let _ = self.conn.change_keyboard_mapping(
1,
self.keycode,
self.keysyms_per_keycode,
&self.original_keysyms,
);
- let _ = self.conn.flush();
+ let _ = self.conn.get_input_focus().map(|cookie| cookie.reply());
}
}
diff --git a/libs/cua-driver/rust/crates/platform-linux/tests/key_input_x11.rs b/libs/cua-driver/rust/crates/platform-linux/tests/key_input_x11.rs
index 2b12dd6074..60fcd1534c 100644
--- a/libs/cua-driver/rust/crates/platform-linux/tests/key_input_x11.rs
+++ b/libs/cua-driver/rust/crates/platform-linux/tests/key_input_x11.rs
@@ -4,7 +4,9 @@ use std::sync::OnceLock;
use std::time::{Duration, Instant};
use anyhow::{bail, Result};
-use platform_linux::input::{send_click, send_key, send_key_at, send_key_xtest, send_type_text};
+use platform_linux::input::{
+ send_click, send_key, send_key_at, send_key_xtest, send_type_text, send_type_text_xtest,
+};
use x11rb::connection::Connection;
use x11rb::protocol::xproto::*;
use x11rb::protocol::Event;
@@ -113,6 +115,27 @@ fn assert_no_button_events(conn: &RustConnection, label: &str) -> Result<()> {
Ok(())
}
+fn spare_keycodes(conn: &RustConnection) -> Result<Vec<u8>> {
+ let setup = conn.setup();
+ let (min, max) = (setup.min_keycode, setup.max_keycode);
+ let mapping = conn.get_keyboard_mapping(min, max - min + 1)?.reply()?;
+ let per = usize::from(mapping.keysyms_per_keycode);
+ Ok(mapping
+ .keysyms
+ .chunks(per)
+ .enumerate()
+ .filter(|(_, keysyms)| keysyms.iter().all(|&keysym| keysym == 0))
+ .map(|(index, _)| min + index as u8)
+ .collect())
+}
+
+fn focused_input_window(conn: &RustConnection, screen: usize) -> Result<Window> {
+ let window = input_window(conn, conn.setup().roots[screen].root, 0, 0)?;
+ conn.set_input_focus(InputFocus::PARENT, window, x11rb::CURRENT_TIME)?;
+ assert_eq!(conn.get_input_focus()?.reply()?.focus, window);
+ Ok(window)
+}
+
#[test]
#[ignore = "requires an isolated X11 display"]
fn background_click_delivers_complete_sequence_to_press_recipient_without_focus_change(
@@ -279,3 +302,18 @@ fn background_keys_deliver_complete_sequences_without_changing_focus() -> Result
assert_eq!(conn.get_input_focus()?.reply()?.focus, sentinel);
Ok(())
}
+
+#[test]
+#[ignore = "requires an isolated X11 display with XTEST"]
+fn xtest_text_restores_the_spare_keycodes_it_borrows() -> Result<()> {
+ let (conn, screen) = connect()?;
+ focused_input_window(&conn, screen)?;
+ let before = spare_keycodes(&conn)?;
+ assert!(!before.is_empty(), "the test display has no spare keycode");
+ for text in ["\u{4f60}", "\u{597d}", "\u{4e16}"] {
+ send_type_text_xtest(text)?;
+ keyboard_events(&conn, 2)?;
+ assert_eq!(spare_keycodes(&conn)?, before, "after typing {text}");
+ }
+ Ok(())
+}