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.

feat(cua-driver-rs)(linux): enable Chromium/Electron accessibility at serve startup (#1930)

Francesco Bonacci7df344d4eda8
diff --git a/docs/content/docs/cua-driver/guide/getting-started/linux.mdx b/docs/content/docs/cua-driver/guide/getting-started/linux.mdx
index 668e3e0beb..41295d3451 100644
--- a/docs/content/docs/cua-driver/guide/getting-started/linux.mdx
+++ b/docs/content/docs/cua-driver/guide/getting-started/linux.mdx
@@ -59,6 +59,12 @@ If the probe fails, install the AT-SPI service and enable accessibility:
You don't need accessibility enabled _system-wide_ — it needs to be on for the user session that's running Cua Driver. Most desktop environments toggle this automatically when an accessibility client connects.
+### Electron, Chromium, and Chrome apps
+
+Chromium-based apps — Chrome, and everything built on Electron or CEF (VS Code, Slack, Discord, Obsidian, including the AppImage builds) — keep their accessibility tree switched off until they detect that an assistive technology is present, so by default `get_window_state` would see an empty tree for them. When you run `cua-driver serve`, the daemon flips the session's `org.a11y.Status` flags on (the same signal a screen reader sets), which makes Chromium build its full AT-SPI tree — retroactively, for apps already open, with no relaunch and no per-app flag. GTK and Qt read the same signal, so their bridges warm up too.
+
+This is on by default. To leave your session's accessibility status untouched, start the daemon with `CUA_DRIVER_RS_DISABLE_A11Y_ADVERTISE=1`.
+
<Callout type="info">
**Why AT-SPI and not raw X11 properties?** Many Linux apps don't expose their UI structure through
X11 window manager hints — only through AT-SPI. Without AT-SPI, `click` would have to fall back to
diff --git a/libs/cua-driver/rust/crates/cua-driver/src/main.rs b/libs/cua-driver/rust/crates/cua-driver/src/main.rs
index 81a6770202..207ce9fe77 100644
--- a/libs/cua-driver/rust/crates/cua-driver/src/main.rs
+++ b/libs/cua-driver/rust/crates/cua-driver/src/main.rs
@@ -771,6 +771,10 @@ fn build_registry(cursor_cfg: cursor_overlay::CursorConfig) -> cua_driver_core::
cua_driver_core::video::set_video_backend_factory(
Box::new(cua_driver_core::video_ffmpeg::FfmpegVideoBackendFactory),
);
+ // Turn on Chromium/Electron (and GTK/Qt) accessibility for the session
+ // so their AT-SPI trees are visible to get_window_state. Best-effort and
+ // idempotent; only on the serve path, not for short-lived CLI calls.
+ platform_linux::a11y::ensure_chromium_accessibility_enabled();
{ let mut r = platform_linux::register_tools_with_cursor(cursor_cfg, compat); check_update_tool::register_into(&mut r); r }
}
#[cfg(not(any(target_os = "windows", target_os = "linux")))]
diff --git a/libs/cua-driver/rust/crates/platform-linux/src/a11y.rs b/libs/cua-driver/rust/crates/platform-linux/src/a11y.rs
new file mode 100644
index 0000000000..5a96af06d9
--- /dev/null
+++ b/libs/cua-driver/rust/crates/platform-linux/src/a11y.rs
@@ -0,0 +1,120 @@
+//! Switch on Chromium / Electron accessibility for the whole desktop session.
+//!
+//! Chromium — and therefore every Electron, CEF, and Chrome-based app — ships
+//! its accessibility tree disabled and only builds it once it believes an
+//! assistive technology is listening. It decides that by watching the
+//! freedesktop accessibility status published on the session bus: the
+//! `org.a11y.Bus` service exposes an `org.a11y.Status` interface whose
+//! `ScreenReaderEnabled` property is the signal. Until that property is true a
+//! Chromium window registers either nothing or an empty application on the
+//! AT-SPI registry, so [`crate::atspi`] walks an empty tree and
+//! `get_window_state` reports the app as having no elements — it is invisible
+//! to the driver. Electron apps shipped as AppImages behave identically; they
+//! embed the same Chromium.
+//!
+//! A real screen reader (Orca) turns the tree on simply by writing that status
+//! property when it starts. We do the same once, at daemon startup. Because the
+//! status object lives on the session-scoped accessibility-bus launcher rather
+//! than in our process, the flag is session-wide, outlives us, and takes effect
+//! retroactively on apps that are already running — no relaunch and no
+//! per-application command-line flag. GTK and Qt gate their own AT-SPI bridges
+//! on the companion `IsEnabled` property, so setting it warms those toolkits
+//! too.
+//!
+//! Everything here is best-effort. A session without an accessibility bus (some
+//! headless or minimal setups) just yields an error we log and ignore; enabling
+//! accessibility must never be able to fail daemon startup.
+
+use std::sync::Once;
+
+use anyhow::{anyhow, Context};
+use atspi::zbus;
+
+/// Well-known session-bus name of the freedesktop accessibility-bus launcher,
+/// which also carries the session's accessibility status.
+const ACCESSIBILITY_BUS_SERVICE: &str = "org.a11y.Bus";
+/// Object on [`ACCESSIBILITY_BUS_SERVICE`] exposing `org.a11y.Status`.
+const ACCESSIBILITY_BUS_OBJECT: &str = "/org/a11y/bus";
+/// Interface holding the session's accessibility-enabled / screen-reader flags.
+const ACCESSIBILITY_STATUS_INTERFACE: &str = "org.a11y.Status";
+/// Property Chromium watches to decide whether to build its AT-SPI tree.
+const SCREEN_READER_ENABLED_PROPERTY: &str = "ScreenReaderEnabled";
+/// Companion property GTK/Qt watch to load their AT-SPI bridges.
+const ACCESSIBILITY_IS_ENABLED_PROPERTY: &str = "IsEnabled";
+
+/// Advertise an assistive technology to the session exactly once per daemon
+/// process, so Chromium/Electron (including Electron AppImages), GTK, and Qt
+/// expose their accessibility trees to [`crate::atspi`]. Idempotent and
+/// best-effort: repeated calls do nothing, and any failure is logged and
+/// swallowed so it can never block startup.
+pub fn ensure_chromium_accessibility_enabled() {
+ static ADVERTISED: Once = Once::new();
+ ADVERTISED.call_once(|| {
+ // Opt-out for the rare session that wants its accessibility status left
+ // untouched (e.g. one already driven by a real screen reader the user
+ // configured deliberately).
+ if std::env::var_os("CUA_DRIVER_RS_DISABLE_A11Y_ADVERTISE").is_some() {
+ tracing::debug!(
+ "CUA_DRIVER_RS_DISABLE_A11Y_ADVERTISE set; leaving session \
+ accessibility status untouched"
+ );
+ return;
+ }
+ if let Err(error) = advertise_screen_reader_to_session() {
+ tracing::debug!(
+ "skipped advertising accessibility to the session \
+ (Chromium/Electron trees may stay empty): {error:#}"
+ );
+ }
+ });
+}
+
+fn advertise_screen_reader_to_session() -> anyhow::Result<()> {
+ // The daemon's tokio runtime is already driving this thread when the tool
+ // registry is built, and `block_on` panics if called from within a runtime.
+ // Run the one-shot bus work on a dedicated OS thread that owns a small
+ // runtime of its own, then join it — no nesting, torn down once it returns.
+ std::thread::Builder::new()
+ .name("cua-a11y-advertise".into())
+ .spawn(|| {
+ let runtime = tokio::runtime::Builder::new_current_thread()
+ .enable_all()
+ .build()?;
+ runtime.block_on(advertise_screen_reader())
+ })
+ .context("spawning the accessibility-advertise thread")?
+ .join()
+ .map_err(|_| anyhow!("accessibility-advertise thread panicked"))?
+}
+
+async fn advertise_screen_reader() -> anyhow::Result<()> {
+ let session_bus = zbus::Connection::session().await?;
+ let status = zbus::Proxy::new(
+ &session_bus,
+ ACCESSIBILITY_BUS_SERVICE,
+ ACCESSIBILITY_BUS_OBJECT,
+ ACCESSIBILITY_STATUS_INTERFACE,
+ )
+ .await?;
+
+ // Don't clobber a screen reader the user is already running: only write when
+ // a flag is currently false, so an active Orca session stays authoritative
+ // and we avoid emitting a redundant PropertiesChanged.
+ if !is_flag_set(&status, SCREEN_READER_ENABLED_PROPERTY).await {
+ status
+ .set_property(SCREEN_READER_ENABLED_PROPERTY, true)
+ .await?;
+ }
+ if !is_flag_set(&status, ACCESSIBILITY_IS_ENABLED_PROPERTY).await {
+ status
+ .set_property(ACCESSIBILITY_IS_ENABLED_PROPERTY, true)
+ .await?;
+ }
+ Ok(())
+}
+
+/// Read a boolean status property, treating an unreadable property as unset so
+/// the caller falls through to writing it.
+async fn is_flag_set(status: &zbus::Proxy<'_>, property: &str) -> bool {
+ status.get_property::<bool>(property).await.unwrap_or(false)
+}
diff --git a/libs/cua-driver/rust/crates/platform-linux/src/atspi/native.rs b/libs/cua-driver/rust/crates/platform-linux/src/atspi/native.rs
index a67edb413d..ddc2d42b0f 100644
--- a/libs/cua-driver/rust/crates/platform-linux/src/atspi/native.rs
+++ b/libs/cua-driver/rust/crates/platform-linux/src/atspi/native.rs
@@ -616,11 +616,35 @@ pub fn set_value(pid: u32, idx: usize, value: &str) -> Result<()> {
.await
.map_err(|e| anyhow!("interface proxies unavailable: {e}"))?;
- if target.has_editable {
- if let Ok(et) = proxies.editable_text().await {
- if et.set_text_contents(value).await.unwrap_or(false) {
- return Ok(());
- }
+ // EditableText write. We don't gate on the cached `has_editable` flag:
+ // GTK4 (and similar toolkits) only advertise the EditableText interface
+ // on a widget once it holds keyboard focus, so the interface list
+ // captured during the unfocused tree walk can be missing it even though
+ // the element is a real editable text box. GrabFocus first (internal
+ // widget focus, no window activation — same trick as `type_text`'s
+ // EditableText path), then resolve the EditableText proxy live over
+ // D-Bus and try to write. If the proxy genuinely isn't there the
+ // `editable_text()` resolve fails and we fall through to Value below.
+ if target.has_component {
+ if let Ok(comp) = proxies.component().await {
+ let _ = call(comp.grab_focus()).await;
+ }
+ }
+ if let Ok(et) = proxies.editable_text().await {
+ // Replace whole contents (parity with the Windows/macOS set_value,
+ // which overwrite rather than insert at the caret).
+ if et.set_text_contents(value).await.unwrap_or(false) {
+ return Ok(());
+ }
+ // Some toolkits reject SetTextContents but accept an insert at the
+ // caret offset; clear-then-insert as a fallback.
+ let off = match proxies.text().await {
+ Ok(tp) => tp.caret_offset().await.unwrap_or(0),
+ Err(_) => 0,
+ };
+ let len = value.chars().count() as i32;
+ if et.insert_text(off, value, len).await.unwrap_or(false) {
+ return Ok(());
}
}
if target.has_value {
diff --git a/libs/cua-driver/rust/crates/platform-linux/src/lib.rs b/libs/cua-driver/rust/crates/platform-linux/src/lib.rs
index 8fb7dafa2e..9cd2dcef49 100644
--- a/libs/cua-driver/rust/crates/platform-linux/src/lib.rs
+++ b/libs/cua-driver/rust/crates/platform-linux/src/lib.rs
@@ -37,6 +37,9 @@ pub mod capture;
#[cfg(target_os = "linux")]
pub mod atspi;
+#[cfg(target_os = "linux")]
+pub mod a11y;
+
#[cfg(target_os = "linux")]
pub mod wayland;