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): stop a batched text before rebinding keycodes the client has not confirmed reading (#5244)

injaneity6d82b3b1b218
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 2a58ac666b..7a0cd50dae 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
@@ -3433,7 +3433,7 @@ fn type_text_in_spare_batches<G>(
mapping: &GetKeyboardMappingReply,
mut remap: impl FnMut(char, &mut GetKeyboardMappingReply) -> Result<(u8, Option<G>)>,
mut send: impl FnMut(u8, bool) -> Result<()>,
- mut finish: impl FnMut(),
+ mut finish: impl FnMut() -> bool,
) -> Result<()> {
let total = text.chars().count();
let spares = match mapping.keysyms_per_keycode as usize {
@@ -3477,7 +3477,11 @@ fn type_text_in_spare_batches<G>(
send(keycode, needs_shift)?;
typed += 1;
}
- finish();
+ if !finish() && rest.peek().is_some() {
+ bail!(
+ "typed {typed} of {total} characters: the client has not confirmed reading the keys on the spare keycodes the next characters need"
+ );
+ }
}
Ok(())
}
@@ -3543,7 +3547,7 @@ pub fn send_type_text_with_delay(xid: u64, text: &str, inter_char_ms: u64) -> Re
}
Ok(())
},
- || *client.borrow_mut() = TextClient::new(&conn, Some(window)),
+ || client.borrow_mut().release(),
)?;
// Deliver the final release before this short-lived connection closes.
conn.get_input_focus()?.reply()?;
@@ -3588,7 +3592,7 @@ pub fn send_type_text_xtest(text: &str) -> Result<()> {
sleep(Duration::from_millis(KEY_DELAY_MS));
Ok(())
},
- || *client.borrow_mut() = TextClient::new(&conn, None),
+ || client.borrow_mut().release(),
)?;
// Round-trip so the server delivers the final character's key events before
// this short-lived connection drops (see send_key_xtest — keyboard XTEST
@@ -4221,7 +4225,7 @@ mod key_name_alias_tests {
log.borrow_mut().push(("send", keycode));
Ok(())
},
- || {},
+ || true,
)
.unwrap();
assert_eq!(
@@ -4263,13 +4267,48 @@ mod key_name_alias_tests {
events.push(keycode);
Ok(())
},
- || {},
+ || true,
)
.unwrap_err();
assert_eq!(error.to_string(), "typed 2 of 3 characters: keymap changed");
assert_eq!(events, [9, 8]);
}
+ #[test]
+ fn text_batches_stop_before_rebinding_keycodes_the_client_has_not_read() {
+ let mapping = GetKeyboardMappingReply {
+ keysyms_per_keycode: 2,
+ sequence: 0,
+ keysyms: vec!['a' as u32, 'A' as u32, 0, 0],
+ };
+ for (text, result) in [
+ ("你", Ok(vec![9])),
+ ("你好", Err("typed 1 of 2 characters: the client has not confirmed reading the keys on the spare keycodes the next characters need")),
+ ] {
+ let mut events = Vec::new();
+ let typed = type_text_in_spare_batches(
+ text,
+ &mapping,
+ |ch, mapping| {
+ reserve_spare_keycode(
+ mapping,
+ super::mpx_keyboard::keysym_for_char(ch),
+ |_, _, _, _| Ok(()),
+ )
+ .map(|(keycode, guard)| (keycode, Some(guard)))
+ },
+ |keycode, _| {
+ events.push(keycode);
+ Ok(())
+ },
+ || false,
+ )
+ .map(|()| events)
+ .map_err(|error| error.to_string());
+ assert_eq!(typed, result.map_err(String::from), "{text}");
+ }
+ }
+
#[test]
fn shifted_column_glyphs_need_shift_and_base_glyphs_do_not() {
let per = 2usize;
@@ -4491,14 +4530,21 @@ impl<'a> TextClient<'a> {
self.unanswered.push(window);
}
}
-}
-impl Drop for TextClient<'_> {
- fn drop(&mut self) {
- for window in std::mem::take(&mut self.sent) {
+ fn release(&mut self) -> bool {
+ let sent = std::mem::take(&mut self.sent);
+ for &window in &sent {
self.ping(window);
}
self.borrowed.clear();
+ let unanswered = std::mem::take(&mut self.unanswered);
+ !sent.iter().any(|window| unanswered.contains(window))
+ }
+}
+
+impl Drop for TextClient<'_> {
+ fn drop(&mut self) {
+ self.release();
}
}
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 0c4247dcd7..556edc5abe 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
@@ -678,6 +678,62 @@ fn xtest_text_restores_the_keycode_of_a_client_that_never_answers() -> Result<()
Ok(())
}
+#[test]
+#[ignore = "requires an isolated X11 display with XTEST"]
+fn xtest_text_rebinds_a_spare_keycode_only_after_the_client_confirms() -> Result<()> {
+ let (conn, screen) = connect()?;
+ let (root, protocols, ping) = ping_window(&conn, screen)?;
+ let spare = spare_keycodes(&conn)?;
+ let (&free, others) = spare
+ .split_last()
+ .expect("the test display has no spare keycode");
+ let per = conn
+ .get_keyboard_mapping(free, 1)?
+ .reply()?
+ .keysyms_per_keycode;
+ let bind = |keysym: u32| -> Result<()> {
+ for &keycode in others {
+ conn.change_keyboard_mapping(1, keycode, per, &vec![keysym; usize::from(per)])?;
+ }
+ conn.get_input_focus()?.reply()?;
+ Ok(())
+ };
+ bind(0x7e1)?;
+ let silent = type_with_pings(
+ &conn,
+ "\u{4f60}\u{597d}",
+ protocols,
+ ping,
+ |_| Ok(()),
+ |_, _| Ok(()),
+ )?;
+ let after_silent = spare_keycodes(&conn)?;
+ let answered = type_with_pings(
+ &conn,
+ "\u{4f60}\u{597d}",
+ protocols,
+ ping,
+ |_| Ok(()),
+ |data, _| pong(&conn, root, protocols, data),
+ )?;
+ bind(0)?;
+ clear_active_window(&conn, root)?;
+
+ let error = silent
+ .result
+ .expect_err("rebound a keycode the client had not confirmed");
+ assert!(
+ error.to_string().starts_with("typed 1 of 2 characters"),
+ "{error:#}"
+ );
+ assert_eq!(silent.presses, vec![free]);
+ assert_eq!(after_silent, vec![free]);
+ answered.result?;
+ assert_eq!(answered.presses, vec![free, free]);
+ assert_eq!(spare_keycodes(&conn)?, spare);
+ Ok(())
+}
+
fn take_ping(conn: &RustConnection, window: Window, protocols: Atom, ping: Atom) -> Result<()> {
conn.change_property32(
PropMode::REPLACE,