Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.
Rust agentai-agentapplecomputer-usecomputer-use-agentcontainerizationcuadesktop-automationhacktoberfestlumemacosmanusoperatorswiftvirtualizationvirtualization-frameworkwindowswindows-sandbox
fix(cua-driver): restore the prior X11 foreground after foreground type_text (#6768)
injaneity0358eb27237a
diff --git a/libs/cua-driver/rust/crates/cua-driver-e2e/tests/x11_foreground_restore_linux_test.rs b/libs/cua-driver/rust/crates/cua-driver-e2e/tests/x11_foreground_restore_linux_test.rsnew file mode 100644index 0000000000..62c6c23670--- /dev/null+++ b/libs/cua-driver/rust/crates/cua-driver-e2e/tests/x11_foreground_restore_linux_test.rs@@ -0,0 +1,233 @@+// SPDX-License-Identifier: MIT+// Copyright (c) 2026 Cua AI, Inc.++//! Linux/X11 `type_text` with `delivery_mode:"foreground"` activates the+//! target, types, then gives the active window back to the one the user had+//! (CUA-3663).+//!+//! The test process maps two titled toplevels: `prior`, which it activates+//! first, and `target`, which listens for key presses. The oracles are the+//! target connection's own KeyPress events (fixture state) and the window+//! manager's `_NET_ACTIVE_WINDOW` before and after the call (focus).+//!+//! #[ignore] (needs an X11 display with a window manager). Run:+//! cargo test -p cua-driver-e2e --test x11_foreground_restore_linux_test -- --ignored --nocapture --test-threads=1++#![cfg(target_os = "linux")]++use std::time::{Duration, Instant};++use cua_driver_testkit::e2e::{+ execute_case, recording_evidence, CaseSpec, Delivery, DriverRoute, Observation, OracleKind,+ Scope, Targeting,+};+use cua_driver_testkit::{Driver, McpDriver};+use x11rb::connection::Connection;+use x11rb::protocol::xproto::{+ AtomEnum, ClientMessageEvent, ConnectionExt as _, CreateWindowAux, EventMask, PropMode, Window,+ WindowClass,+};+use x11rb::protocol::Event;+use x11rb::rust_connection::RustConnection;+use x11rb::wrapper::ConnectionExt as _;++struct Desktop {+ conn: RustConnection,+ root: Window,+ net_active_window: u32,+}++impl Desktop {+ fn connect() -> Self {+ let (conn, screen_num) =+ RustConnection::connect(None).expect("connect to the X11 display under test");+ let root = conn.setup().roots[screen_num].root;+ let net_active_window = conn+ .intern_atom(false, b"_NET_ACTIVE_WINDOW")+ .expect("intern _NET_ACTIVE_WINDOW")+ .reply()+ .expect("intern _NET_ACTIVE_WINDOW reply")+ .atom;+ Self {+ conn,+ root,+ net_active_window,+ }+ }++ fn map(&self, title: &str, x: i16) -> Window {+ let screen = &self.conn.setup().roots[0];+ let window = self.conn.generate_id().expect("allocate X11 window id");+ self.conn+ .create_window(+ x11rb::COPY_DEPTH_FROM_PARENT,+ window,+ self.root,+ x,+ 80,+ 360,+ 240,+ 0,+ WindowClass::INPUT_OUTPUT,+ x11rb::COPY_FROM_PARENT,+ &CreateWindowAux::new()+ .background_pixel(screen.white_pixel)+ .event_mask(EventMask::KEY_PRESS | EventMask::EXPOSURE),+ )+ .expect("create X11 toplevel");+ self.conn+ .change_property8(+ PropMode::REPLACE,+ window,+ AtomEnum::WM_NAME,+ AtomEnum::STRING,+ title.as_bytes(),+ )+ .expect("set WM_NAME");+ let net_wm_pid = self+ .conn+ .intern_atom(false, b"_NET_WM_PID")+ .expect("intern _NET_WM_PID")+ .reply()+ .expect("intern _NET_WM_PID reply")+ .atom;+ self.conn+ .change_property32(+ PropMode::REPLACE,+ window,+ net_wm_pid,+ AtomEnum::CARDINAL,+ &[std::process::id()],+ )+ .expect("set _NET_WM_PID");+ self.conn.map_window(window).expect("map X11 toplevel");+ self.conn.flush().expect("flush X11 requests");+ window+ }++ fn active(&self) -> Option<Window> {+ self.conn+ .get_property(+ false,+ self.root,+ self.net_active_window,+ AtomEnum::WINDOW,+ 0,+ 1,+ )+ .ok()?+ .reply()+ .ok()?+ .value32()?+ .next()+ .filter(|w| *w != 0)+ }++ /// Ask the window manager to activate `window` as a pager would.+ fn activate(&self, window: Window) {+ let event = ClientMessageEvent::new(32, window, self.net_active_window, [2, 0, 0, 0, 0]);+ self.conn+ .send_event(+ false,+ self.root,+ EventMask::SUBSTRUCTURE_REDIRECT | EventMask::SUBSTRUCTURE_NOTIFY,+ event,+ )+ .expect("send _NET_ACTIVE_WINDOW");+ self.conn.flush().expect("flush activation");+ }++ fn wait_active(&self, window: Window) -> bool {+ let deadline = Instant::now() + Duration::from_secs(5);+ while Instant::now() < deadline {+ if self.active() == Some(window) {+ return true;+ }+ std::thread::sleep(Duration::from_millis(50));+ }+ false+ }++ fn key_presses(&self, window: Window) -> usize {+ let mut presses = 0;+ while let Ok(Some(event)) = self.conn.poll_for_event() {+ if matches!(event, Event::KeyPress(press) if press.event == window) {+ presses += 1;+ }+ }+ presses+ }+}++#[test]+#[ignore]+fn x11_foreground_type_text_restores_the_prior_active_window() {+ if std::env::var_os("DISPLAY").is_none() || std::env::var_os("WAYLAND_DISPLAY").is_some() {+ if std::env::var_os("CUA_TEST_REQUIRE_FIXTURES").is_some() {+ panic!("the X11 foreground restore case requires a native X11 session");+ }+ eprintln!("[x11-foreground-restore] not a native X11 session; skipping");+ return;+ }+ let cell_id = "linux-xlib-foreground-restore-type-text-px-foreground";+ let case = CaseSpec::delivered(+ cell_id,+ "x11-bare-client",+ "xlib",+ "type_text",+ Targeting::Px,+ Delivery::Foreground,+ Scope::Window,+ DriverRoute::LinuxXTest,+ vec![OracleKind::FixtureState, OracleKind::Focus],+ );+ execute_case(case, |evidence| {+ let mut driver = McpDriver::spawn_named(cell_id).expect("start source-built Linux driver");+ *evidence = recording_evidence(driver.recording_dir());+ let desktop = Desktop::connect();+ let target = desktop.map("CuaForegroundRestoreTarget", 80);+ let prior = desktop.map("CuaForegroundRestorePrior", 520);+ desktop.activate(prior);+ assert!(+ desktop.wait_active(prior),+ "fixture precondition: the prior window never became active"+ );+ desktop.key_presses(target);++ driver.start_behavior_recording();+ let typed = driver.call(+ "type_text",+ serde_json::json!({+ "pid": std::process::id(),+ "window_id": u64::from(target),+ "text": "abc",+ "delivery_mode": "foreground"+ }),+ );+ assert!(+ !typed.is_error(),+ "foreground type_text failed: {}",+ typed.text()+ );++ let deadline = Instant::now() + Duration::from_secs(3);+ let mut presses = 0;+ while presses < 3 && Instant::now() < deadline {+ presses += desktop.key_presses(target);+ std::thread::sleep(Duration::from_millis(50));+ }+ assert!(+ presses >= 3,+ "the target received {presses} of 3 key presses: {}",+ typed.text()+ );+ assert_eq!(+ desktop.active(),+ Some(prior),+ "foreground type_text left the active window on the target \+ instead of the prior window: {}",+ typed.text()+ );+ Observation::delivered_with_fixture_state(vec![OracleKind::Focus])+ });+}diff --git a/libs/cua-driver/rust/crates/platform-linux/src/input/focus_guard.rs b/libs/cua-driver/rust/crates/platform-linux/src/input/focus_guard.rsindex 70c7befc15..220eb3b710 100644--- a/libs/cua-driver/rust/crates/platform-linux/src/input/focus_guard.rs+++ b/libs/cua-driver/rust/crates/platform-linux/src/input/focus_guard.rs@@ -521,16 +521,19 @@ impl FocusSnapshot {})}- fn diff(&self, x: &X) -> Vec<String> {+ /// What moved since capture. PointerRoot (1) / None (0) focus and a+ /// cleared active window are transient while the WM moves focus, so they+ /// are skipped unless `strict`: the foreground restore owes back exactly+ /// the saved focus.+ fn diff(&self, x: &X, strict: bool) -> Vec<String> {let mut changes = Vec::new();let (focus, _) = x.core_focus();- // PointerRoot (1) / None (0) are transient states while the WM moves- // focus; the settle loop sees the final value.- if focus > 1 && focus != self.core_focus {+ let known = |now: bool, saved: bool| now || (strict && saved);+ if known(focus > 1, self.core_focus > 1) && focus != self.core_focus {changes.push(format!("focus 0x{:x}->0x{:x}", self.core_focus, focus));}let active = x.active_window();- if active != self.active && active.is_some() {+ if active != self.active && known(active.is_some(), self.active.is_some()) {changes.push(format!("active 0x{:x}->0x{:x}",self.active.unwrap_or(0),@@ -570,7 +573,7 @@ impl FocusSnapshot {}/// The window that held the focus at capture time (active, else core).- fn previous_focus_window(&self) -> Option<Window> {+ pub(crate) fn previous_focus_window(&self) -> Option<Window> {self.active.or_else(|| (self.core_focus > 1).then_some(self.core_focus))}@@ -592,10 +595,29 @@ impl FocusSnapshot {target_pid: Option<u32>,settle_watch: bool,) -> FocusGuardReport {+ self.restore_guarded(target_pid, settle_watch, true)+ .unwrap_or_default()+ }++ /// The foreground transaction's restore: `target_pid` attributes a new+ /// popup's grab, but the same-app rule is off, since a foreground action+ /// gives the focus back even to a prior window of the target's process.+ /// `None` when the display could not be opened to restore.+ pub(crate) fn restore_after_foreground(+ &self,+ target_pid: Option<u32>,+ ) -> Option<FocusGuardReport> {+ self.restore_guarded(target_pid, false, false)+ }++ fn restore_guarded(+ &self,+ target_pid: Option<u32>,+ settle_watch: bool,+ same_app: bool,+ ) -> Option<FocusGuardReport> {let started = Instant::now();- let Ok(x) = X::open() else {- return FocusGuardReport::default();- };+ let x = X::open().ok()?;let mut report = FocusGuardReport::default();// Settle watch: stop at the first observed change. A new top-level@@ -610,7 +632,7 @@ impl FocusSnapshot {started};let mut extended = false;- let mut changes = self.diff(&x);+ let mut changes = self.diff(&x, !same_app);let mut new_clients = self.new_clients(&x);let mut own_window = self.own_new_window(&x, target_pid, &new_clients);if !settle_watch && !new_clients.is_empty() && own_window.is_none() {@@ -621,7 +643,7 @@ impl FocusSnapshot {std::thread::sleep(SETTLE_POLL.min(watch_until.saturating_duration_since(Instant::now())),);- changes = self.diff(&x);+ changes = self.diff(&x, !same_app);new_clients = self.new_clients(&x);own_window = self.own_new_window(&x, target_pid, &new_clients);if !extended && !new_clients.is_empty() && own_window.is_none() {@@ -631,10 +653,12 @@ impl FocusSnapshot {}// Popups opened by the action: an open GTK/VCL menu grabs the keyboard.+ // The driver's own agent-cursor overlay grabs nothing.let new_popups: Vec<Window> = x.mapped_popups().difference(&self.popups).copied()+ .filter(|w| !crate::overlay::is_x11_overlay_window(*w)).collect();if !new_popups.is_empty() {report.grab_held_by = new_popups@@ -646,7 +670,7 @@ impl FocusSnapshot {if changes.is_empty() {report.same_app_window = own_window;report.elapsed_ms = started.elapsed().as_millis() as u64;- return report;+ return Some(report);}report.changed = true;report.changes = changes;@@ -674,7 +698,7 @@ impl FocusSnapshot {&& x.active_window() != self.active;match guard_restore(closed.is_some(),- target_pid,+ target_pid.filter(|_| same_app),self.previous_owner,new_owner.filter(|_| !wm_only),report.grab_held_by.is_some(),@@ -687,7 +711,7 @@ impl FocusSnapshot {focused: true,});report.elapsed_ms = started.elapsed().as_millis() as u64;- return report;+ return Some(report);}GuardRestore::SameAppDialog => {let window = focus_window.unwrap_or(0);@@ -697,11 +721,11 @@ impl FocusSnapshot {focused: true,});report.elapsed_ms = started.elapsed().as_millis() as u64;- return report;+ return Some(report);}GuardRestore::GrabHeld => {report.elapsed_ms = started.elapsed().as_millis() as u64;- return report;+ return Some(report);}GuardRestore::Restore => {}}@@ -717,7 +741,7 @@ impl FocusSnapshot {std::thread::sleep(RESTORE_POLL.min(deadline.saturating_duration_since(Instant::now())),);- let now = self.diff(&x);+ let now = self.diff(&x, !same_app);if now.is_empty() {stable += 1;if stable >= STABLE_POLLS {@@ -740,7 +764,7 @@ impl FocusSnapshot {);}report.elapsed_ms = started.elapsed().as_millis() as u64;- report+ Some(report)}/// The topmost freshly mapped toplevel of the target pid, when the targetdiff --git a/libs/cua-driver/rust/crates/platform-linux/src/input/foreground.rs b/libs/cua-driver/rust/crates/platform-linux/src/input/foreground.rsindex 7987911bc5..fb246632da 100644--- a/libs/cua-driver/rust/crates/platform-linux/src/input/foreground.rs+++ b/libs/cua-driver/rust/crates/platform-linux/src/input/foreground.rs@@ -7,10 +7,11 @@//! A foreground action behaves like a user at the keyboard: activate + raise the//! target toplevel, wait until the window manager confirms that it is the//! active window AND that the core X input focus sits inside it, then inject-//! real input (XTest) and report where focus ended up. The target is left-//! active afterwards — restoring the previously active window would pop down-//! any menu the click just opened and would move keyboard focus away from the-//! widget a following `type_text` needs.+//! real input (XTest) and report where focus ended up. Pointer and key actions+//! leave the target active afterwards — restoring the previously active window+//! would pop down any menu the click just opened and would move keyboard focus+//! away from the widget a following `type_text` needs. `type_text` restores+//! the prior active window once its text is in.//!//! Everything here is bounded: the activation/confirmation phase and the//! post-check run on watchdog threads with hard deadlines, so a wedged Xdiff --git a/libs/cua-driver/rust/crates/platform-linux/src/interaction/mod.rs b/libs/cua-driver/rust/crates/platform-linux/src/interaction/mod.rsindex 5f256e6477..0bc0b44343 100644--- a/libs/cua-driver/rust/crates/platform-linux/src/interaction/mod.rs+++ b/libs/cua-driver/rust/crates/platform-linux/src/interaction/mod.rs@@ -50,9 +50,9 @@ pub fn pointer_gesture_restore() -> RestoreMode {/// Run `body` with the exact `pid`/`window_id` pair focused. On Wayland a/// compositor adapter confirms the pair and restores the prior focus/// afterwards; global input is refused when no adapter can confirm it. On-/// X11 [`with_x11_foreground`] activates the window and leaves it active.-/// [`RestoreMode::LeaveFocused`] runs `body` as it is: it activates the-/// target itself and leaves it focused.+/// X11 [`with_x11_foreground`] activates the window and restores the prior+/// focus afterwards. [`RestoreMode::LeaveFocused`] runs `body` as it is: it+/// activates the target itself and leaves it focused.pub fn with_target_foreground<T>(pid: u32,window_id: u64,@@ -68,7 +68,7 @@ pub fn with_target_foreground<T>(return body();}if std::env::var_os("WAYLAND_DISPLAY").is_none() {- return with_x11_foreground(window_id, ForegroundOptions::pointer(), body)+ return with_x11_foreground(window_id, ForegroundOptions::pointer(), mode, body).map(|(value, _report)| value);}let body = || {@@ -144,16 +144,65 @@ pub fn with_target_foreground<T>(/// The X11 foreground transaction (`delivery_mode:"foreground"`): activate/// `xid` and read back that it holds the active window and input focus,/// admit the lock for window input, run `body`, then report where focus and-/// the target process's windows ended up. The target is left active.+/// the target process's windows ended up. [`RestoreMode::RestorePrevious`]+/// then gives the focus back to the window that held it before, even when+/// activation or the body failed, since focus may have moved either way;+/// [`RestoreMode::LeaveFocused`] leaves the target active.pub fn with_x11_foreground<T>(xid: u64,opts: ForegroundOptions,+ mode: RestoreMode,body: impl FnOnce() -> Result<T>,) -> Result<(T, ForegroundReport)> {- let activated = crate::input::foreground::activate(xid, opts)?;- admit_lock(policy::Scope::WindowInput, facts::Fact::Known(true))?;- let value = body()?;- Ok((value, activated.report()))+ let (_transaction, prior) = match mode {+ RestoreMode::RestorePrevious => {+ let held = hold_transaction()?;+ let Some(prior) = crate::input::focus_guard::FocusSnapshot::capture() else {+ anyhow::bail!(+ "{}: the prior X11 focus could not be read, so nothing was focused",+ crate::input::foreground::CODE_UNAVAILABLE+ );+ };+ // No window held the focus before: there is nothing to give back.+ (Some(held), prior.previous_focus_window().map(|_| prior))+ }+ RestoreMode::LeaveFocused => (None, None),+ };+ // Read before the body, which may close the target.+ let target_pid = prior+ .as_ref()+ .and_then(|_| crate::x11::window_pid(xid).or(opts.target_pid));+ let ran = crate::input::foreground::activate(xid, opts).and_then(|activated| {+ admit_lock(policy::Scope::WindowInput, facts::Fact::Known(true))?;+ let value = body()?;+ Ok((value, activated.report()))+ });+ // Judged on whether the focus came back, not on the background guard's+ // outcome order: a focused new dialog of the target is not a restore.+ // Only a prior window the body closed leaves nothing to restore.+ let unrestored = prior.and_then(|prior| match prior.restore_after_foreground(target_pid) {+ None => Some("the display could not be opened".to_owned()),+ Some(r) if r.changed && !r.restored && r.closed_window.is_none() => Some(format!(+ "{} ({})",+ if r.grab_held_by.is_some() {+ "grab_held"+ } else {+ "not_restored"+ },+ r.changes.join(", ")+ )),+ Some(_) => None,+ });+ match (ran, unrestored) {+ (ran, None) => ran,+ (Ok(_), Some(why)) => {+ anyhow::bail!("the input was sent, but the prior focus could not be restored: {why}")+ }+ (Err(error), Some(why)) => {+ let note = format!("{error}; the prior focus also could not be restored: {why}");+ Err(error.context(note))+ }+ }}/// How long a compositor may take to apply and report a focus change.diff --git a/libs/cua-driver/rust/crates/platform-linux/src/overlay.rs b/libs/cua-driver/rust/crates/platform-linux/src/overlay.rsindex e6ed28556f..b8991ca956 100644--- a/libs/cua-driver/rust/crates/platform-linux/src/overlay.rs+++ b/libs/cua-driver/rust/crates/platform-linux/src/overlay.rs@@ -185,6 +185,15 @@ fn should_start_x11_overlay(wayland_display_present: bool) -> bool {/// desktop capture ask it to hide a visible overlay.static X11_OVERLAY_LIVE: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);+/// The XID of the X11 agent-cursor overlay window, 0 before it exists. The+/// focus guard skips it: it is override-redirect but grabs nothing.+static X11_OVERLAY_WINDOW: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0);++/// Whether `window` is this process's X11 agent-cursor overlay.+pub(crate) fn is_x11_overlay_window(window: u32) -> bool {+ window != 0 && X11_OVERLAY_WINDOW.load(std::sync::atomic::Ordering::Acquire) == window+}+/// Whether an X11 overlay window exists that a desktop capture must hide.pub(crate) fn x11_overlay_live() -> bool {X11_OVERLAY_LIVE.load(std::sync::atomic::Ordering::Acquire)@@ -299,6 +308,8 @@ impl X11OverlayThreadCleanup {#[cfg(target_os = "linux")]impl Drop for X11OverlayThreadCleanup {fn drop(&mut self) {+ // The XID is free for another client once this connection closes.+ X11_OVERLAY_WINDOW.store(0, std::sync::atomic::Ordering::Release);// Disconnect first so an animator racing teardown cannot enqueue after// the waiter sweep. Registrations before release are swept below;// registrations after release observe a disconnected channel and cancel@@ -1507,6 +1518,7 @@ fn run_overlay_thread(cfg: CursorConfig, rx: std::sync::mpsc::Receiver<OverlayMs// Create the overlay window.let win = conn.generate_id().unwrap();+ X11_OVERLAY_WINDOW.store(win, std::sync::atomic::Ordering::Release);let win_aux = CreateWindowAux::new().background_pixel(0).border_pixel(0)diff --git a/libs/cua-driver/rust/crates/platform-linux/src/tools/click.rs b/libs/cua-driver/rust/crates/platform-linux/src/tools/click.rsindex c2c9373a9c..9c37699557 100644--- a/libs/cua-driver/rust/crates/platform-linux/src/tools/click.rs+++ b/libs/cua-driver/rust/crates/platform-linux/src/tools/click.rs@@ -551,6 +551,7 @@ impl ClickTool {crate::interaction::with_x11_foreground(xid,crate::input::ForegroundOptions::pointer(),+ crate::interaction::RestoreMode::LeaveFocused,|| {crate::input::send_click_xtest_desktop_with_modifiers(sx.round() as i32,@@ -645,6 +646,7 @@ impl ClickTool {crate::interaction::with_x11_foreground(xid,crate::input::ForegroundOptions::pointer(),+ crate::interaction::RestoreMode::LeaveFocused,|| crate::atspi::perform_action_observed(&observed_for_ax),)},@@ -1355,6 +1357,7 @@ impl Tool for ClickTool {crate::interaction::with_x11_foreground(xid2,crate::input::ForegroundOptions::pointer(),+ crate::interaction::RestoreMode::LeaveFocused,|| {crate::input::send_click_xtest_desktop_with_modifiers(sx.round() as i32,@@ -1724,6 +1727,7 @@ impl Tool for ClickTool {crate::interaction::with_x11_foreground(xid,crate::input::ForegroundOptions::pointer(),+ crate::interaction::RestoreMode::LeaveFocused,|| inject(true),).map(|((path, route, guard), report)| (path, Some(report), route, guard))diff --git a/libs/cua-driver/rust/crates/platform-linux/src/tools/double_click.rs b/libs/cua-driver/rust/crates/platform-linux/src/tools/double_click.rsindex 56800dd904..2f6d662d0f 100644--- a/libs/cua-driver/rust/crates/platform-linux/src/tools/double_click.rs+++ b/libs/cua-driver/rust/crates/platform-linux/src/tools/double_click.rs@@ -169,6 +169,7 @@ impl Tool for DoubleClickTool {return crate::interaction::with_x11_foreground(xid,crate::input::ForegroundOptions::pointer(),+ crate::interaction::RestoreMode::LeaveFocused,|| {let (sx, sy) =window_local_to_screen(xid, lxi as f64, lyi as f64)?;@@ -296,6 +297,7 @@ impl Tool for DoubleClickTool {return crate::interaction::with_x11_foreground(xid,crate::input::ForegroundOptions::pointer(),+ crate::interaction::RestoreMode::LeaveFocused,|| {// Real XTest double-click at the screen point — synthetic// XSendEvent button events are dropped by GTK/Qt and the MPXdiff --git a/libs/cua-driver/rust/crates/platform-linux/src/tools/drag.rs b/libs/cua-driver/rust/crates/platform-linux/src/tools/drag.rsindex 874998476d..598ff02b0b 100644--- a/libs/cua-driver/rust/crates/platform-linux/src/tools/drag.rs+++ b/libs/cua-driver/rust/crates/platform-linux/src/tools/drag.rs@@ -694,6 +694,7 @@ impl Tool for DragTool {crate::interaction::with_x11_foreground(xid,crate::input::ForegroundOptions::pointer(),+ crate::interaction::RestoreMode::LeaveFocused,|| {crate::input::send_drag_xtest_desktop_with_modifiers(screen_from_x.round() as i32,diff --git a/libs/cua-driver/rust/crates/platform-linux/src/tools/hotkey.rs b/libs/cua-driver/rust/crates/platform-linux/src/tools/hotkey.rsindex 9d20fe3a85..5c85af0d92 100644--- a/libs/cua-driver/rust/crates/platform-linux/src/tools/hotkey.rs+++ b/libs/cua-driver/rust/crates/platform-linux/src/tools/hotkey.rs@@ -441,6 +441,7 @@ impl Tool for HotkeyTool {return crate::interaction::with_x11_foreground(xid,crate::input::ForegroundOptions::keyboard(),+ crate::interaction::RestoreMode::LeaveFocused,|| crate::input::send_key_xtest(&key, &m),).map(|((), report)| KeyRoute::Foreground(report));diff --git a/libs/cua-driver/rust/crates/platform-linux/src/tools/press_key.rs b/libs/cua-driver/rust/crates/platform-linux/src/tools/press_key.rsindex 805b6569fc..49ecc7ebbc 100644--- a/libs/cua-driver/rust/crates/platform-linux/src/tools/press_key.rs+++ b/libs/cua-driver/rust/crates/platform-linux/src/tools/press_key.rs@@ -429,6 +429,7 @@ impl Tool for PressKeyTool {return crate::interaction::with_x11_foreground(xid,crate::input::ForegroundOptions::keyboard(),+ crate::interaction::RestoreMode::LeaveFocused,|| {if let Some(element) = &token {let element_index = element.index;diff --git a/libs/cua-driver/rust/crates/platform-linux/src/tools/right_click.rs b/libs/cua-driver/rust/crates/platform-linux/src/tools/right_click.rsindex e4e4dc7ca5..c6502c721e 100644--- a/libs/cua-driver/rust/crates/platform-linux/src/tools/right_click.rs+++ b/libs/cua-driver/rust/crates/platform-linux/src/tools/right_click.rs@@ -163,6 +163,7 @@ impl Tool for RightClickTool {return crate::interaction::with_x11_foreground(xid,crate::input::ForegroundOptions::pointer(),+ crate::interaction::RestoreMode::LeaveFocused,|| {let (sx, sy) =window_local_to_screen(xid, lxi as f64, lyi as f64)?;@@ -284,6 +285,7 @@ impl Tool for RightClickTool {return crate::interaction::with_x11_foreground(xid,crate::input::ForegroundOptions::pointer(),+ crate::interaction::RestoreMode::LeaveFocused,|| {// Real XTest right-click at the screen point (synthetic// XSendEvent is dropped by GTK/Qt; MPX needs /dev/uinput).diff --git a/libs/cua-driver/rust/crates/platform-linux/src/tools/scroll.rs b/libs/cua-driver/rust/crates/platform-linux/src/tools/scroll.rsindex 814721deec..489d0464ca 100644--- a/libs/cua-driver/rust/crates/platform-linux/src/tools/scroll.rs+++ b/libs/cua-driver/rust/crates/platform-linux/src/tools/scroll.rs@@ -805,6 +805,7 @@ impl Tool for ScrollTool {crate::interaction::with_x11_foreground(xid,crate::input::ForegroundOptions::pointer(),+ crate::interaction::RestoreMode::LeaveFocused,|| {crate::input::send_click_xtest_desktop(point.0 as i32,diff --git a/libs/cua-driver/rust/crates/platform-linux/src/tools/set_value.rs b/libs/cua-driver/rust/crates/platform-linux/src/tools/set_value.rsindex 2b2b1bf050..2620e5feee 100644--- a/libs/cua-driver/rust/crates/platform-linux/src/tools/set_value.rs+++ b/libs/cua-driver/rust/crates/platform-linux/src/tools/set_value.rs@@ -182,6 +182,7 @@ impl Tool for SetValueTool {crate::interaction::with_x11_foreground(win,crate::input::ForegroundOptions::pointer(),+ crate::interaction::RestoreMode::LeaveFocused,|| {crate::input::send_click_xtest_desktop(sx.round() as i32,@@ -289,6 +290,7 @@ fn pick_from_popup(crate::interaction::with_x11_foreground(win,crate::input::ForegroundOptions::keyboard(),+ crate::interaction::RestoreMode::LeaveFocused,|| crate::input::send_key_xtest("Escape", &[]),).map(|_| ())@@ -322,6 +324,7 @@ fn pick_from_popup(crate::interaction::with_x11_foreground(win,crate::input::ForegroundOptions::pointer(),+ crate::interaction::RestoreMode::LeaveFocused,|| crate::input::send_click_xtest_desktop(sx.round() as i32, sy.round() as i32, 1, 1),)?;open_and_pick()?;diff --git a/libs/cua-driver/rust/crates/platform-linux/src/tools/type_text.rs b/libs/cua-driver/rust/crates/platform-linux/src/tools/type_text.rsindex 3e865e955a..2af4fdc3ac 100644--- a/libs/cua-driver/rust/crates/platform-linux/src/tools/type_text.rs+++ b/libs/cua-driver/rust/crates/platform-linux/src/tools/type_text.rs@@ -769,9 +769,13 @@ impl Tool for TypeTextTool {return Ok(("pty", None));}if foreground {- crate::interaction::with_x11_foreground(xid, crate::input::ForegroundOptions::pointer(), || {- crate::input::send_type_text_xtest(&text_t)- }).map(|(value, _report)| value)?;+ crate::interaction::with_x11_foreground(+ xid,+ crate::input::ForegroundOptions::pointer(),+ crate::interaction::RestoreMode::RestorePrevious,+ || crate::input::send_type_text_xtest(&text_t),+ )+ .map(|(value, _report)| value)?;return Ok(("key_events_fg", None));}// Background: real key events through the XI2 virtual master@@ -811,6 +815,7 @@ impl Tool for TypeTextTool {crate::interaction::with_x11_foreground(xid,crate::input::ForegroundOptions::keyboard(),+ crate::interaction::RestoreMode::RestorePrevious,|| {if let Some(element) = &element {if !crate::atspi::focus_token(pid, element)? {@@ -934,6 +939,7 @@ impl Tool for TypeTextTool {crate::interaction::with_x11_foreground(xid,crate::input::ForegroundOptions::pointer(),+ crate::interaction::RestoreMode::RestorePrevious,|| crate::input::send_type_text_xtest(&text_f),).map(|(value, _report)| value)@@ -1038,9 +1044,13 @@ impl Tool for TypeTextTool {return Ok(("key_events", None));}if delivery.is_foreground() {- crate::interaction::with_x11_foreground(xid, crate::input::ForegroundOptions::pointer(), || {- crate::input::send_type_text_xtest(&text)- }).map(|(value, _report)| value)?;+ crate::interaction::with_x11_foreground(+ xid,+ crate::input::ForegroundOptions::pointer(),+ crate::interaction::RestoreMode::RestorePrevious,+ || crate::input::send_type_text_xtest(&text),+ )+ .map(|(value, _report)| value)?;return Ok(("key_events_fg", None));}// No a11y editable, no pty: real key events through the XI2 virtualdiff --git a/scripts/ci/linux/run-rust-e2e.sh b/scripts/ci/linux/run-rust-e2e.shindex 764f190eb9..d4768c7eab 100755--- a/scripts/ci/linux/run-rust-e2e.sh+++ b/scripts/ci/linux/run-rust-e2e.sh@@ -398,6 +398,10 @@ if [[ "${SUITE}" == capture || "${SUITE}" == all ]]; thencargo test -p cua-driver-e2e "${CARGO_DRIVER_FEATURE_ARGS[@]}" \--test x11_unpublished_pid_linux_test -- \--ignored --nocapture --test-threads=1+ run_test x11-foreground-restore \+ cargo test -p cua-driver-e2e "${CARGO_DRIVER_FEATURE_ARGS[@]}" \+ --test x11_foreground_restore_linux_test -- \+ --ignored --nocapture --test-threads=1elselimitation="The unpublished-_NET_WM_PID case maps a bare X11 client and runs in the canonical X11 lane."jq -n \