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): never borrow keycode 8 for a Linux text character (#5595)

injaneity039fa6486d89
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 6337bff668..3b1c7b0255 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
@@ -3639,7 +3639,8 @@ fn type_text_in_spare_batches<G>(
per => mapping
.keysyms
.chunks(per)
- .filter(|syms| syms.iter().all(|&s| s == 0))
+ .enumerate()
+ .filter(|&(i, syms)| is_spare_keycode(8 + i, syms))
.count(),
};
let mut rest = text.chars().peekable();
@@ -4645,6 +4646,50 @@ mod key_name_alias_tests {
);
}
+ #[test]
+ fn spare_keycodes_skip_keycode_8() {
+ let mut mapping = GetKeyboardMappingReply {
+ keysyms_per_keycode: 2,
+ sequence: 0,
+ keysyms: vec![0, 0, 0, 0, 'a' as u32, 'A' as u32],
+ };
+ let mut reserve = || reserve_spare_keycode(&mut mapping, 0x100_4f60, |_, _, _, _| Ok(()));
+ assert_eq!(reserve().unwrap().0, 9);
+ assert_eq!(
+ reserve().unwrap_err().to_string(),
+ "no spare keycode available to remap keysym 0x1004F60"
+ );
+ }
+
+ #[test]
+ fn text_batches_do_not_count_keycode_8_as_spare() {
+ let mapping = GetKeyboardMappingReply {
+ keysyms_per_keycode: 2,
+ sequence: 0,
+ keysyms: vec![0, 0, 0, 0, 'a' as u32, 'A' as u32],
+ };
+ let mut events = Vec::new();
+ type_text_in_spare_batches(
+ "你好",
+ &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(())
+ },
+ || true,
+ )
+ .unwrap();
+ assert_eq!(events, [9, 9]);
+ }
+
#[test]
fn text_batches_reports_progress_when_a_later_batch_fails() {
let mapping = GetKeyboardMappingReply {
@@ -4785,6 +4830,14 @@ impl Drop for RemappedKeycode<'_> {
}
}
+/// Whether a text may borrow `keycode`: every keysym slot is NoSymbol (0) — i.e.
+/// completely unused — so borrowing it cannot clobber a real key. Keycode 8 is
+/// evdev KEY_RESERVED, which a virtual keyboard cannot send, so it is never
+/// borrowed.
+fn is_spare_keycode(keycode: usize, keysyms: &[u32]) -> bool {
+ keycode > 8 && keysyms.iter().all(|&s| s == 0)
+}
+
/// Reserve a fully unused keycode for `keysym`, calling `bind` before updating
/// the local mapping. Returns the keycode and the value produced by `bind`.
fn reserve_spare_keycode<G>(
@@ -4797,15 +4850,14 @@ fn reserve_spare_keycode<G>(
bail!("empty keyboard mapping; cannot remap keysym 0x{keysym:X}");
}
- // Find a keycode whose every keysym slot is NoSymbol (0) — i.e. completely
- // unused — so borrowing it cannot clobber a real key. Scan high-to-low:
- // high keycodes are far likelier to be free than the low, populated ones.
+ // Scan high-to-low: high keycodes are far likelier to be free than the
+ // low, populated ones.
let spare = mapping
.keysyms
.chunks(per)
.enumerate()
.rev()
- .find(|(_, syms)| syms.iter().all(|&s| s == 0))
+ .find(|&(i, syms)| is_spare_keycode(8 + i, syms))
.map(|(i, _)| (8 + i) as u8)
.ok_or_else(|| anyhow!("no spare keycode available to remap keysym 0x{keysym:X}"))?;
diff --git a/libs/cua-driver/rust/crates/platform-linux/src/input/mpx_keyboard.rs b/libs/cua-driver/rust/crates/platform-linux/src/input/mpx_keyboard.rs
index bd9795d7a8..32e008231c 100644
--- a/libs/cua-driver/rust/crates/platform-linux/src/input/mpx_keyboard.rs
+++ b/libs/cua-driver/rust/crates/platform-linux/src/input/mpx_keyboard.rs
@@ -883,7 +883,7 @@ mod tests {
.keysyms
.iter_mut()
.filter(|sym| **sym == 0)
- .skip(2)
+ .skip(4)
.for_each(|sym| *sym = 1);
let restored = std::cell::Cell::new(0);
let error = plan_text_with_fallback(&keymap, Some(50), "aé€a", |ch, mapping| {