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 client before restoring a borrowed keycode (#5172)

injaneity7e6db51b078e
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 c899caddbe..2a58ac666b 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
@@ -34,12 +34,13 @@ pub use mpx_keyboard::{
use anyhow::{anyhow, bail, Context, Result};
use evdev::uinput::VirtualDevice;
use evdev::{AttributeSet, EventType, InputEvent, Key, RelativeAxisType};
+use std::cell::RefCell;
use std::collections::HashMap;
use std::ffi::{CStr, CString};
use std::fs;
use std::panic::{catch_unwind, AssertUnwindSafe};
use std::ptr;
-use std::sync::atomic::{AtomicU64, Ordering};
+use std::sync::atomic::{AtomicU32, AtomicU64, Ordering};
use std::sync::{Arc, Mutex, OnceLock};
use std::thread::sleep;
use std::time::Duration;
@@ -50,6 +51,8 @@ use x11rb::rust_connection::RustConnection;
const CLICK_DELAY_MS: u64 = 35;
const DOUBLE_CLICK_DELAY_MS: u64 = 50;
const KEY_DELAY_MS: u64 = 10;
+const PING_TIMEOUT: Duration = Duration::from_secs(1);
+static PING_SERIAL: AtomicU32 = AtomicU32::new(1);
#[derive(Clone, Debug)]
pub struct VirtualPointerDrag {
@@ -3430,6 +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(),
) -> Result<()> {
let total = text.chars().count();
let spares = match mapping.keysyms_per_keycode as usize {
@@ -3473,6 +3477,7 @@ fn type_text_in_spare_batches<G>(
send(keycode, needs_shift)?;
typed += 1;
}
+ finish();
}
Ok(())
}
@@ -3483,18 +3488,13 @@ pub fn send_type_text_with_delay(xid: u64, text: &str, inter_char_ms: u64) -> Re
let window = xid as u32;
let root = conn.setup().roots[0].root;
let mapping = conn.get_keyboard_mapping(8, 248)?.reply()?;
+ let client = RefCell::new(TextClient::new(&conn, Some(window)));
type_text_in_spare_batches(
text,
&mapping,
- |ch, mapping| {
- keycode_for_keysym(
- &conn,
- mapping,
- mpx_keyboard::keysym_for_char(ch),
- &ch.to_string(),
- )
- },
+ |ch, mapping| text_keycode(&mut client.borrow_mut(), mapping, ch),
|keycode, needs_shift| {
+ client.borrow_mut().record_recipient(keycode);
let state = if needs_shift {
KeyButMask::SHIFT
} else {
@@ -3543,6 +3543,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)),
)?;
// Deliver the final release before this short-lived connection closes.
conn.get_input_focus()?.reply()?;
@@ -3568,18 +3569,13 @@ pub fn send_type_text_xtest(text: &str) -> Result<()> {
.get(..kpm)
.and_then(|s| s.iter().copied().find(|&k| k != 0))
.unwrap_or(50);
+ let client = RefCell::new(TextClient::new(&conn, None));
type_text_in_spare_batches(
text,
&mapping,
- |ch, mapping| {
- keycode_for_keysym(
- &conn,
- mapping,
- mpx_keyboard::keysym_for_char(ch),
- &ch.to_string(),
- )
- },
+ |ch, mapping| text_keycode(&mut client.borrow_mut(), mapping, ch),
|keycode, needs_shift| {
+ client.borrow_mut().record_recipient(keycode);
if needs_shift {
conn.xtest_fake_input(KEY_PRESS_EVENT, shift_kc, 0, x11rb::NONE, 0, 0, 0)?;
}
@@ -3592,6 +3588,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),
)?;
// Round-trip so the server delivers the final character's key events before
// this short-lived connection drops (see send_key_xtest — keyboard XTEST
@@ -4188,84 +4185,9 @@ pub(crate) fn key_name_to_keysym(key: &str) -> Result<u32> {
mod key_name_alias_tests {
use super::{
char_to_keycode_shift, key_name_to_keysym, reserve_spare_keycode,
- type_text_in_spare_batches, type_text_with_preflight, GetKeyboardMappingReply,
+ type_text_in_spare_batches, GetKeyboardMappingReply,
};
- #[test]
- fn text_preflight_failure_leaves_target_and_events_unchanged() {
- let mapping = GetKeyboardMappingReply {
- keysyms_per_keycode: 2,
- sequence: 0,
- keysyms: vec!['a' as u32, 'A' as u32],
- };
- for text in ["約ab", "aa約", "a約a"] {
- let mut target = String::from("existing");
- let mut events = Vec::new();
- let result = type_text_with_preflight::<()>(
- text,
- &mapping,
- |_, _| anyhow::bail!("no spare keycode"),
- |keycode, shift| {
- events.extend([keycode, keycode]);
- target.push(if shift { 'A' } else { 'a' });
- Ok(())
- },
- );
- assert!(result
- .unwrap_err()
- .to_string()
- .starts_with("typed 0 of 3 characters"));
- assert!(events.is_empty());
- assert_eq!(target, "existing");
- }
- }
-
- #[test]
- fn text_preflight_requires_a_spare_for_each_distinct_unmapped_character() {
- let mapping = GetKeyboardMappingReply {
- keysyms_per_keycode: 2,
- sequence: 0,
- keysyms: vec!['a' as u32, 'A' as u32, 0, 0],
- };
- let mut target = String::from("existing");
- let mut events = Vec::new();
- let error = type_text_with_preflight::<()>(
- "a你好",
- &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);
- target.push('a');
- Ok(())
- },
- )
- .unwrap_err();
- assert_eq!(
- error.to_string(),
- format!(
- "typed 0 of 3 characters: the text needs 2 spare keycodes: no spare keycode available to remap keysym 0x{:X}",
- super::mpx_keyboard::keysym_for_char('好')
- )
- );
- assert!(events.is_empty());
- assert_eq!(target, "existing");
- }
-
- struct Restore<'a>(&'a std::cell::Cell<usize>);
-
- impl Drop for Restore<'_> {
- fn drop(&mut self) {
- self.0.set(self.0.get() + 1);
- }
- }
-
#[test]
fn text_batches_types_more_distinct_unmapped_characters_than_spares() {
use std::cell::RefCell;
@@ -4299,6 +4221,7 @@ mod key_name_alias_tests {
log.borrow_mut().push(("send", keycode));
Ok(())
},
+ || {},
)
.unwrap();
assert_eq!(
@@ -4340,111 +4263,13 @@ mod key_name_alias_tests {
events.push(keycode);
Ok(())
},
+ || {},
)
.unwrap_err();
assert_eq!(error.to_string(), "typed 2 of 3 characters: keymap changed");
assert_eq!(events, [9, 8]);
}
- #[test]
- fn text_preflight_restores_every_remap_after_success_and_failure() {
- let mapping = GetKeyboardMappingReply {
- keysyms_per_keycode: 2,
- sequence: 0,
- keysyms: vec!['a' as u32, 'A' as u32, 0, 0, 0, 0],
- };
- for (text, fail_send, ok, sent) in [
- ("你好你a", false, true, 4),
- ("你好约", false, false, 0),
- ("你好", true, false, 1),
- ] {
- let restored = std::cell::Cell::new(0);
- let mut events = 0;
- let result = type_text_with_preflight(
- text,
- &mapping,
- |ch, mapping| {
- reserve_spare_keycode(
- mapping,
- super::mpx_keyboard::keysym_for_char(ch),
- |_, _, _, _| Ok(Restore(&restored)),
- )
- .map(|(keycode, guard)| (keycode, Some(guard)))
- },
- |_, _| {
- events += 1;
- if fail_send {
- anyhow::bail!("send failed");
- }
- Ok(())
- },
- );
- assert_eq!(result.is_ok(), ok, "{text}");
- drop(result);
- assert_eq!(restored.get(), 2, "{text}");
- assert_eq!(events, sent, "{text}");
- }
- }
-
- #[test]
- fn text_preflight_preserves_shift_order_and_reuses_remaps() {
- let mapping = GetKeyboardMappingReply {
- keysyms_per_keycode: 2,
- sequence: 0,
- keysyms: vec!['a' as u32, 'A' as u32, 0, 0, 0, 0],
- };
- let mut remaps = 0;
- let mut events = Vec::new();
- type_text_with_preflight::<()>(
- "aA你好你",
- &mapping,
- |ch, mapping| {
- remaps += 1;
- reserve_spare_keycode(
- mapping,
- super::mpx_keyboard::keysym_for_char(ch),
- |_, _, _, _| Ok(()),
- )
- .map(|(keycode, guard)| (keycode, Some(guard)))
- },
- |keycode, shift| {
- events.push((keycode, shift));
- Ok(())
- },
- )
- .unwrap();
- assert_eq!(remaps, 2);
- assert_eq!(
- events,
- [(8, false), (8, true), (10, false), (9, false), (10, false)]
- );
- }
-
- #[test]
- fn text_preflight_preserves_keys_with_symbols_in_later_columns() {
- let mapping = GetKeyboardMappingReply {
- keysyms_per_keycode: 4,
- sequence: 0,
- keysyms: vec!['a' as u32, 'A' as u32, 'b' as u32, 0],
- };
- let mut events = Vec::new();
- type_text_with_preflight::<()>(
- "bAa",
- &mapping,
- |ch, mapping| {
- assert_eq!(ch, 'b');
- assert!(mapping.keysyms.contains(&(ch as u32)));
- Ok((8, None))
- },
- |keycode, shift| {
- events.push((keycode, shift));
- Ok(())
- },
- )
- .unwrap();
- assert_eq!(events, [(8, false), (8, true), (8, false)]);
- }
-
#[test]
fn shifted_column_glyphs_need_shift_and_base_glyphs_do_not() {
let per = 2usize;
@@ -4597,6 +4422,159 @@ fn keycode_for_keysym<'a>(
Ok((keycode, Some(guard)))
}
+fn text_keycode(
+ client: &mut TextClient<'_>,
+ mapping: &mut GetKeyboardMappingReply,
+ ch: char,
+) -> Result<(u8, Option<()>)> {
+ let keysym = mpx_keyboard::keysym_for_char(ch);
+ let (keycode, guard) = keycode_for_keysym(client.conn, mapping, keysym, &ch.to_string())?;
+ let Some(guard) = guard else {
+ return Ok((keycode, None));
+ };
+ client.borrowed.push(guard);
+ if let Some(window) = client.recipient() {
+ client.ping(window);
+ }
+ Ok((keycode, Some(())))
+}
+
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+enum ClientSync {
+ Confirmed,
+ Unsupported,
+ Unanswered,
+}
+
+struct TextClient<'a> {
+ conn: &'a RustConnection,
+ target: Option<Window>,
+ borrowed: Vec<RemappedKeycode<'a>>,
+ sent: Vec<Window>,
+ unanswered: Vec<Window>,
+}
+
+impl<'a> TextClient<'a> {
+ fn new(conn: &'a RustConnection, target: Option<Window>) -> Self {
+ Self {
+ conn,
+ target,
+ borrowed: Vec::new(),
+ sent: Vec::new(),
+ unanswered: Vec::new(),
+ }
+ }
+
+ fn recipient(&self) -> Option<Window> {
+ self.target.or_else(|| {
+ let cookie = self.conn.get_input_focus().ok()?;
+ cookie.reply().ok().map(|reply| reply.focus)
+ })
+ }
+
+ fn record_recipient(&mut self, keycode: u8) {
+ if !self.borrowed.iter().any(|guard| guard.keycode == keycode) {
+ return;
+ }
+ if let Some(window) = self.recipient() {
+ if !self.sent.contains(&window) {
+ self.sent.push(window);
+ }
+ }
+ }
+
+ fn ping(&mut self, window: Window) {
+ if self.unanswered.contains(&window) {
+ return;
+ }
+ if sync_client(self.conn, Some(window)) == ClientSync::Unanswered {
+ self.unanswered.push(window);
+ }
+ }
+}
+
+impl Drop for TextClient<'_> {
+ fn drop(&mut self) {
+ for window in std::mem::take(&mut self.sent) {
+ self.ping(window);
+ }
+ self.borrowed.clear();
+ }
+}
+
+fn sync_client(conn: &RustConnection, window: Option<Window>) -> ClientSync {
+ let atom = |name: &str| {
+ conn.intern_atom(false, name.as_bytes())
+ .ok()?
+ .reply()
+ .ok()
+ .map(|reply| reply.atom)
+ };
+ let (Some(window), Some(protocols), Some(ping)) =
+ (window, atom("WM_PROTOCOLS"), atom("_NET_WM_PING"))
+ else {
+ return ClientSync::Unsupported;
+ };
+ let Some(window) = ping_window(conn, window, protocols, ping) else {
+ return ClientSync::Unsupported;
+ };
+ let root = conn.setup().roots[0].root;
+ let listen = ChangeWindowAttributesAux::new().event_mask(EventMask::SUBSTRUCTURE_NOTIFY);
+ let serial = PING_SERIAL.fetch_add(1, Ordering::Relaxed).max(1);
+ let event = ClientMessageEvent::new(32, window, protocols, [ping, serial, window, 0, 0]);
+ if conn.change_window_attributes(root, &listen).is_err()
+ || conn
+ .send_event(false, window, EventMask::NO_EVENT, event)
+ .is_err()
+ || conn.flush().is_err()
+ {
+ return ClientSync::Unanswered;
+ }
+ let deadline = std::time::Instant::now() + PING_TIMEOUT;
+ while std::time::Instant::now() < deadline {
+ match conn.poll_for_event() {
+ Ok(Some(x11rb::protocol::Event::ClientMessage(pong)))
+ if pong.type_ == protocols
+ && pong.data.as_data32()[..3] == [ping, serial, window] =>
+ {
+ return ClientSync::Confirmed;
+ }
+ Ok(Some(_)) => {}
+ Ok(None) => sleep(Duration::from_millis(1)),
+ Err(_) => return ClientSync::Unanswered,
+ }
+ }
+ ClientSync::Unanswered
+}
+
+fn ping_window(
+ conn: &RustConnection,
+ focus: Window,
+ protocols: Atom,
+ ping: Atom,
+) -> Option<Window> {
+ if focus == x11rb::NONE || focus == u32::from(InputFocus::POINTER_ROOT) {
+ return None;
+ }
+ let client = !conn.setup().resource_id_mask;
+ let mut window = focus;
+ while window != x11rb::NONE && window & client == focus & client {
+ let protocols = conn
+ .get_property(false, window, protocols, AtomEnum::ATOM, 0, 64)
+ .ok()?
+ .reply()
+ .ok()?;
+ if protocols
+ .value32()
+ .is_some_and(|mut atoms| atoms.any(|atom| atom == ping))
+ {
+ return Some(window);
+ }
+ window = conn.query_tree(window).ok()?.reply().ok()?.parent;
+ }
+ None
+}
+
fn modifiers_to_state(modifiers: &[&str]) -> KeyButMask {
let mut state = 0u16;
for m in modifiers {
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 918922c1b3..0c4247dcd7 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
@@ -11,6 +11,7 @@ use x11rb::connection::Connection;
use x11rb::protocol::xproto::*;
use x11rb::protocol::Event;
use x11rb::rust_connection::RustConnection;
+use x11rb::wrapper::ConnectionExt as _;
/// Connects to the test display while one connection stays open for the
/// whole test binary. An X server started without `-noreset` (plain
@@ -468,3 +469,386 @@ fn xtest_text_gives_each_missing_character_its_own_keycode() -> Result<()> {
assert_eq!(presses[0], presses[2], "{presses:?}");
Ok(())
}
+
+fn ping_window(conn: &RustConnection, screen: usize) -> Result<(Window, Atom, Atom)> {
+ let root = conn.setup().roots[screen].root;
+ let window = focused_input_window(conn, screen)?;
+ let atom = |name: &str| -> Result<Atom> {
+ Ok(conn.intern_atom(false, name.as_bytes())?.reply()?.atom)
+ };
+ let (protocols, ping, active) = (
+ atom("WM_PROTOCOLS")?,
+ atom("_NET_WM_PING")?,
+ atom("_NET_ACTIVE_WINDOW")?,
+ );
+ conn.change_property32(
+ PropMode::REPLACE,
+ window,
+ protocols,
+ AtomEnum::ATOM,
+ &[ping],
+ )?;
+ conn.change_property32(PropMode::REPLACE, root, active, AtomEnum::WINDOW, &[window])?;
+ conn.get_input_focus()?.reply()?;
+ Ok((root, protocols, ping))
+}
+
+fn clear_active_window(conn: &RustConnection, root: Window) -> Result<()> {
+ let active = conn
+ .intern_atom(false, b"_NET_ACTIVE_WINDOW")?
+ .reply()?
+ .atom;
+ conn.delete_property(root, active)?;
+ conn.get_input_focus()?.reply()?;
+ Ok(())
+}
+
+fn pong(conn: &RustConnection, root: Window, protocols: Atom, data: [u32; 5]) -> Result<()> {
+ let event = ClientMessageEvent::new(32, root, protocols, data);
+ conn.send_event(
+ false,
+ root,
+ EventMask::SUBSTRUCTURE_NOTIFY | EventMask::SUBSTRUCTURE_REDIRECT,
+ event,
+ )?;
+ conn.flush()?;
+ Ok(())
+}
+
+fn keysyms_of(conn: &RustConnection, keycode: u8) -> Result<Vec<u32>> {
+ Ok(conn.get_keyboard_mapping(keycode, 1)?.reply()?.keysyms)
+}
+
+struct Typed {
+ result: Result<()>,
+ pings: Vec<[u32; 5]>,
+ presses: Vec<u8>,
+ elapsed: Duration,
+}
+
+fn type_with_pings(
+ conn: &RustConnection,
+ text: &'static str,
+ protocols: Atom,
+ ping: Atom,
+ mut on_press: impl FnMut(u8) -> Result<()>,
+ mut on_ping: impl FnMut([u32; 5], &[u8]) -> Result<()>,
+) -> Result<Typed> {
+ let start = Instant::now();
+ let typing = std::thread::spawn(move || send_type_text_xtest(text));
+ let deadline = start + Duration::from_secs(5);
+ let (mut pings, mut presses) = (Vec::new(), Vec::new());
+ while !typing.is_finished() && Instant::now() < deadline {
+ match conn.poll_for_event()? {
+ Some(Event::KeyPress(event)) => {
+ presses.push(event.detail);
+ on_press(event.detail)?;
+ }
+ Some(Event::ClientMessage(event))
+ if event.type_ == protocols && event.data.as_data32()[0] == ping =>
+ {
+ let data = event.data.as_data32();
+ pings.push(data);
+ on_ping(data, &presses)?;
+ }
+ Some(Event::Error(error)) => bail!("X11 observer error: {error:?}"),
+ _ => std::thread::sleep(Duration::from_millis(1)),
+ }
+ }
+ let result = typing.join().expect("typing thread panicked");
+ let elapsed = start.elapsed();
+ conn.get_input_focus()?.reply()?;
+ while let Some(event) = conn.poll_for_event()? {
+ if let Event::KeyPress(event) = event {
+ presses.push(event.detail);
+ }
+ }
+ Ok(Typed {
+ result,
+ pings,
+ presses,
+ elapsed,
+ })
+}
+
+const NI: u32 = 0x0100_4f60;
+
+#[test]
+#[ignore = "requires an isolated X11 display with XTEST"]
+fn xtest_text_restores_a_borrowed_keycode_only_after_the_client_answers_a_ping() -> Result<()> {
+ let (conn, screen) = connect()?;
+ let (root, protocols, ping) = ping_window(&conn, screen)?;
+ let before = spare_keycodes(&conn)?;
+ let typed = type_with_pings(
+ &conn,
+ "\u{4f60}",
+ protocols,
+ ping,
+ |_| Ok(()),
+ |data, presses| {
+ std::thread::sleep(Duration::from_millis(200));
+ if let Some(&keycode) = presses.last() {
+ assert!(
+ keysyms_of(&conn, keycode)?.contains(&NI),
+ "keycode {keycode} restored before the pong"
+ );
+ }
+ pong(&conn, root, protocols, data)
+ },
+ )?;
+ clear_active_window(&conn, root)?;
+ typed.result?;
+ assert_eq!(typed.presses.len(), 1, "{:?}", typed.presses);
+ assert_eq!(
+ typed.pings.len(),
+ 2,
+ "a ping after the binding and one before the restore"
+ );
+ assert_ne!(typed.pings[0][1], 0);
+ assert_ne!(typed.pings[0][1], typed.pings[1][1], "pings share a serial");
+ assert_eq!(spare_keycodes(&conn)?, before);
+ Ok(())
+}
+
+#[test]
+#[ignore = "requires an isolated X11 display with XTEST"]
+fn xtest_text_ignores_a_late_reply_to_an_earlier_ping() -> Result<()> {
+ let (conn, screen) = connect()?;
+ let (root, protocols, ping) = ping_window(&conn, screen)?;
+ let before = spare_keycodes(&conn)?;
+ let mut earlier: Option<[u32; 5]> = None;
+ let typed = type_with_pings(
+ &conn,
+ "\u{4f60}",
+ protocols,
+ ping,
+ |_| Ok(()),
+ |data, presses| {
+ let Some(stale) = earlier else {
+ earlier = Some(data);
+ return pong(&conn, root, protocols, data);
+ };
+ pong(&conn, root, protocols, stale)?;
+ std::thread::sleep(Duration::from_millis(200));
+ let keycode = *presses
+ .last()
+ .expect("the key arrives before the last ping");
+ assert!(
+ keysyms_of(&conn, keycode)?.contains(&NI),
+ "keycode {keycode} restored on the reply to an earlier ping"
+ );
+ pong(&conn, root, protocols, data)
+ },
+ )?;
+ clear_active_window(&conn, root)?;
+ typed.result?;
+ assert_eq!(typed.pings.len(), 2);
+ assert_eq!(spare_keycodes(&conn)?, before);
+ Ok(())
+}
+
+#[test]
+#[ignore = "requires an isolated X11 display with XTEST"]
+fn xtest_text_restores_the_keycode_of_a_client_that_never_answers() -> Result<()> {
+ let (conn, screen) = connect()?;
+ let (root, protocols, ping) = ping_window(&conn, screen)?;
+ let before = spare_keycodes(&conn)?;
+ let typed = type_with_pings(
+ &conn,
+ "\u{4f60}",
+ protocols,
+ ping,
+ |_| Ok(()),
+ |_, _| Ok(()),
+ )?;
+ clear_active_window(&conn, root)?;
+ typed.result?;
+ assert_eq!(typed.presses.len(), 1, "{:?}", typed.presses);
+ assert_eq!(spare_keycodes(&conn)?, before);
+ assert_eq!(
+ typed.pings.len(),
+ 1,
+ "a client that missed a ping is pinged again"
+ );
+ assert!(
+ typed.elapsed < Duration::from_millis(2500),
+ "typing took {:?}",
+ typed.elapsed
+ );
+ Ok(())
+}
+
+fn take_ping(conn: &RustConnection, window: Window, protocols: Atom, ping: Atom) -> Result<()> {
+ conn.change_property32(
+ PropMode::REPLACE,
+ window,
+ protocols,
+ AtomEnum::ATOM,
+ &[ping],
+ )?;
+ conn.get_input_focus()?.reply()?;
+ Ok(())
+}
+
+#[test]
+#[ignore = "requires an isolated X11 display with XTEST"]
+fn xtest_text_syncs_with_the_window_that_got_the_keys_after_a_focus_change() -> Result<()> {
+ let (conn, screen) = connect()?;
+ let (root, protocols, ping) = ping_window(&conn, screen)?;
+ let first = conn.get_input_focus()?.reply()?.focus;
+ let second = input_window(&conn, root, 0, 120)?;
+ take_ping(&conn, second, protocols, ping)?;
+ let before = spare_keycodes(&conn)?;
+ let typed = type_with_pings(
+ &conn,
+ "\u{4f60}\u{597d}",
+ protocols,
+ ping,
+ |_| Ok(()),
+ |data, _| {
+ if data[2] == first {
+ conn.set_input_focus(InputFocus::PARENT, second, x11rb::CURRENT_TIME)?;
+ conn.get_input_focus()?.reply()?;
+ }
+ pong(&conn, root, protocols, data)
+ },
+ )?;
+ clear_active_window(&conn, root)?;
+ typed.result?;
+ let pinged: Vec<Window> = typed.pings.iter().map(|data| data[2]).collect();
+ assert_eq!(
+ pinged,
+ [first, second, second],
+ "a ping per binding to the focused window, then the restore waits for the window that got the keys"
+ );
+ assert_eq!(spare_keycodes(&conn)?, before);
+ Ok(())
+}
+
+#[test]
+#[ignore = "requires an isolated X11 display with XTEST"]
+fn xtest_text_pings_the_ancestor_of_a_focused_child_window() -> Result<()> {
+ let (conn, screen) = connect()?;
+ let (root, protocols, ping) = ping_window(&conn, screen)?;
+ let parent = conn.get_input_focus()?.reply()?.focus;
+ let child = input_window(&conn, parent, 10, 10)?;
+ conn.set_input_focus(InputFocus::PARENT, child, x11rb::CURRENT_TIME)?;
+ assert_eq!(conn.get_input_focus()?.reply()?.focus, child);
+ clear_active_window(&conn, root)?;
+ let before = spare_keycodes(&conn)?;
+ let typed = type_with_pings(
+ &conn,
+ "\u{4f60}",
+ protocols,
+ ping,
+ |_| Ok(()),
+ |data, _| pong(&conn, root, protocols, data),
+ )?;
+ typed.result?;
+ assert_eq!(typed.presses.len(), 1, "{:?}", typed.presses);
+ assert!(!typed.pings.is_empty());
+ assert!(
+ typed.pings.iter().all(|data| data[2] == parent),
+ "{:?}",
+ typed.pings
+ );
+ assert_eq!(spare_keycodes(&conn)?, before);
+ Ok(())
+}
+
+#[test]
+#[ignore = "requires an isolated X11 display with XTEST"]
+fn xtest_text_syncs_with_the_window_that_got_the_keys_when_focus_moves_after_them() -> Result<()> {
+ let (conn, screen) = connect()?;
+ let (root, protocols, ping) = ping_window(&conn, screen)?;
+ let first = conn.get_input_focus()?.reply()?.focus;
+ let second = input_window(&conn, root, 0, 120)?;
+ take_ping(&conn, second, protocols, ping)?;
+ let before = spare_keycodes(&conn)?;
+ let typed = type_with_pings(
+ &conn,
+ "\u{4f60}aaaaaaaaaa",
+ protocols,
+ ping,
+ |_| {
+ conn.set_input_focus(InputFocus::PARENT, second, x11rb::CURRENT_TIME)?;
+ conn.flush()?;
+ Ok(())
+ },
+ |data, _| pong(&conn, root, protocols, data),
+ )?;
+ clear_active_window(&conn, root)?;
+ typed.result?;
+ let pinged: Vec<Window> = typed.pings.iter().map(|data| data[2]).collect();
+ assert_eq!(pinged, [first, first]);
+ assert_eq!(spare_keycodes(&conn)?, before);
+ Ok(())
+}
+
+struct DisplayWithoutXtest {
+ server: std::process::Child,
+ previous: Option<std::ffi::OsString>,
+}
+
+impl DisplayWithoutXtest {
+ fn start() -> Result<(Self, RustConnection, usize)> {
+ let mut server = std::process::Command::new("Xvfb")
+ .args(["-displayfd", "1", "-extension", "XTEST", "-nolisten", "tcp"])
+ .stdout(std::process::Stdio::piped())
+ .spawn()?;
+ let mut number = String::new();
+ std::io::BufRead::read_line(
+ &mut std::io::BufReader::new(server.stdout.take().expect("piped stdout")),
+ &mut number,
+ )?;
+ let display = format!(":{}", number.trim());
+ let previous = std::env::var_os("DISPLAY");
+ std::env::set_var("DISPLAY", &display);
+ let (conn, screen) = x11rb::connect(Some(&display))?;
+ Ok((Self { server, previous }, conn, screen))
+ }
+}
+
+impl Drop for DisplayWithoutXtest {
+ fn drop(&mut self) {
+ match self.previous.take() {
+ Some(display) => std::env::set_var("DISPLAY", display),
+ None => std::env::remove_var("DISPLAY"),
+ }
+ let _ = self.server.kill();
+ let _ = self.server.wait();
+ }
+}
+
+#[test]
+#[ignore = "requires Xvfb"]
+fn xtest_text_syncs_before_restoring_when_sending_a_key_fails() -> Result<()> {
+ let (_display, conn, screen) = DisplayWithoutXtest::start()?;
+ let (root, protocols, ping) = ping_window(&conn, screen)?;
+ let before = spare_keycodes(&conn)?;
+ let typed = type_with_pings(
+ &conn,
+ "\u{4f60}",
+ protocols,
+ ping,
+ |_| Ok(()),
+ |data, _| {
+ std::thread::sleep(Duration::from_millis(200));
+ assert_ne!(
+ spare_keycodes(&conn)?,
+ before,
+ "keycode restored before the pong"
+ );
+ pong(&conn, root, protocols, data)
+ },
+ )?;
+ assert!(typed.result.is_err(), "typing without XTEST succeeded");
+ assert!(typed.presses.is_empty(), "{:?}", typed.presses);
+ assert_eq!(
+ typed.pings.len(),
+ 2,
+ "a ping after the binding and one before the restore"
+ );
+ assert_eq!(spare_keycodes(&conn)?, before);
+ Ok(())
+}