1use std::collections::{HashMap, HashSet, VecDeque};
4use std::sync::atomic::{AtomicU64, Ordering};
5use std::sync::Mutex;
6
7use rayon::prelude::*;
8use xa11y_core::selector::{Combinator, SelectorSegment};
9use xa11y_core::{
10 ElementData, Error, Provider, Rect, Result, Role, Selector, StateSet, Subscription, Toggled,
11};
12use zbus::blocking::{Connection, Proxy};
13
14static NEXT_HANDLE: AtomicU64 = AtomicU64::new(1);
16
17fn state_attr_to_string(name: &str, s: &StateSet) -> Option<String> {
20 match name {
21 "enabled" => Some(s.enabled.to_string()),
22 "visible" => Some(s.visible.to_string()),
23 "focused" => Some(s.focused.to_string()),
24 "focusable" => Some(s.focusable.to_string()),
25 "selected" => Some(s.selected.to_string()),
26 "editable" => Some(s.editable.to_string()),
27 "modal" => Some(s.modal.to_string()),
28 "required" => Some(s.required.to_string()),
29 "busy" => Some(s.busy.to_string()),
30 "expanded" => s.expanded.map(|b| b.to_string()),
31 "checked" => s.checked.map(|c| {
32 match c {
33 Toggled::On => "on",
34 Toggled::Off => "off",
35 Toggled::Mixed => "mixed",
36 }
37 .to_string()
38 }),
39 _ => None,
40 }
41}
42
43pub struct LinuxProvider {
45 a11y_bus: Connection,
46 handle_cache: Mutex<HashMap<u64, AccessibleRef>>,
48 action_indices: Mutex<HashMap<u64, HashMap<String, i32>>>,
51}
52
53#[derive(Debug, Clone)]
55pub(crate) struct AccessibleRef {
56 pub(crate) bus_name: String,
57 pub(crate) path: String,
58}
59
60impl LinuxProvider {
61 pub fn new() -> Result<Self> {
66 let a11y_bus = Self::connect_a11y_bus()?;
67 Ok(Self {
68 a11y_bus,
69 handle_cache: Mutex::new(HashMap::new()),
70 action_indices: Mutex::new(HashMap::new()),
71 })
72 }
73
74 pub(crate) fn connect_a11y_bus() -> Result<Connection> {
75 if let Ok(session) = Connection::session() {
79 let proxy = Proxy::new(&session, "org.a11y.Bus", "/org/a11y/bus", "org.a11y.Bus")
80 .map_err(|e| Error::Platform {
81 code: -1,
82 message: format!("Failed to create a11y bus proxy: {}", e),
83 })?;
84
85 if let Ok(addr_reply) = proxy.call_method("GetAddress", &()) {
86 if let Ok(address) = addr_reply.body().deserialize::<String>() {
87 if let Ok(addr) = zbus::Address::try_from(address.as_str()) {
88 if let Ok(Ok(conn)) =
89 zbus::blocking::connection::Builder::address(addr).map(|b| b.build())
90 {
91 return Ok(conn);
92 }
93 }
94 }
95 }
96
97 return Ok(session);
99 }
100
101 Connection::session().map_err(|e| Error::Platform {
102 code: -1,
103 message: format!("Failed to connect to D-Bus session bus: {}", e),
104 })
105 }
106
107 fn make_proxy(&self, bus_name: &str, path: &str, interface: &str) -> Result<Proxy<'_>> {
108 zbus::blocking::proxy::Builder::<Proxy>::new(&self.a11y_bus)
111 .destination(bus_name.to_owned())
112 .map_err(|e| Error::Platform {
113 code: -1,
114 message: format!("Failed to set proxy destination: {}", e),
115 })?
116 .path(path.to_owned())
117 .map_err(|e| Error::Platform {
118 code: -1,
119 message: format!("Failed to set proxy path: {}", e),
120 })?
121 .interface(interface.to_owned())
122 .map_err(|e| Error::Platform {
123 code: -1,
124 message: format!("Failed to set proxy interface: {}", e),
125 })?
126 .cache_properties(zbus::proxy::CacheProperties::No)
127 .build()
128 .map_err(|e| Error::Platform {
129 code: -1,
130 message: format!("Failed to create proxy: {}", e),
131 })
132 }
133
134 fn has_interface(&self, aref: &AccessibleRef, iface: &str) -> bool {
137 let proxy = match self.make_proxy(&aref.bus_name, &aref.path, "org.a11y.atspi.Accessible") {
138 Ok(p) => p,
139 Err(_) => return false,
140 };
141 let reply = match proxy.call_method("GetInterfaces", &()) {
142 Ok(r) => r,
143 Err(_) => return false,
144 };
145 let interfaces: Vec<String> = match reply.body().deserialize() {
146 Ok(v) => v,
147 Err(_) => return false,
148 };
149 interfaces.iter().any(|i| i.contains(iface))
150 }
151
152 fn get_role_number(&self, aref: &AccessibleRef) -> Result<u32> {
154 let proxy = self.make_proxy(&aref.bus_name, &aref.path, "org.a11y.atspi.Accessible")?;
155 let reply = proxy
156 .call_method("GetRole", &())
157 .map_err(|e| Error::Platform {
158 code: -1,
159 message: format!("GetRole failed: {}", e),
160 })?;
161 reply
162 .body()
163 .deserialize::<u32>()
164 .map_err(|e| Error::Platform {
165 code: -1,
166 message: format!("GetRole deserialize failed: {}", e),
167 })
168 }
169
170 fn get_role_name(&self, aref: &AccessibleRef) -> Result<String> {
172 let proxy = self.make_proxy(&aref.bus_name, &aref.path, "org.a11y.atspi.Accessible")?;
173 let reply = proxy
174 .call_method("GetRoleName", &())
175 .map_err(|e| Error::Platform {
176 code: -1,
177 message: format!("GetRoleName failed: {}", e),
178 })?;
179 reply
180 .body()
181 .deserialize::<String>()
182 .map_err(|e| Error::Platform {
183 code: -1,
184 message: format!("GetRoleName deserialize failed: {}", e),
185 })
186 }
187
188 fn check_chromium_a11y_enabled(
205 &self,
206 parent: &AccessibleRef,
207 role_hint: Option<Role>,
208 ) -> Result<()> {
209 let app_root = AccessibleRef {
210 bus_name: parent.bus_name.clone(),
211 path: "/org/a11y/atspi/accessible/root".to_string(),
212 };
213 let toolkit = match self
214 .make_proxy(
215 &app_root.bus_name,
216 &app_root.path,
217 "org.a11y.atspi.Application",
218 )
219 .ok()
220 .and_then(|proxy| proxy.get_property::<String>("ToolkitName").ok())
221 {
222 Some(t) => t,
223 None => return Ok(()),
224 };
225 if !toolkit.eq_ignore_ascii_case("Chromium") {
226 return Ok(());
227 }
228 let role = role_hint.unwrap_or_else(|| self.resolve_role(parent));
229 if role != Role::Window {
230 return Ok(());
231 }
232 let app_name = self.get_name(&app_root).unwrap_or_default();
233 Err(Error::AccessibilityNotEnabled {
234 app: app_name,
235 instructions: "Chromium/Electron app exposes an empty accessibility tree on Linux. \
236 Relaunch with `--force-renderer-accessibility` (or set the env var \
237 `ACCESSIBILITY_ENABLED=1`) so the renderer accessibility bridge is initialised."
238 .to_string(),
239 })
240 }
241
242 fn get_name(&self, aref: &AccessibleRef) -> Result<String> {
244 let proxy = self.make_proxy(&aref.bus_name, &aref.path, "org.a11y.atspi.Accessible")?;
245 proxy
246 .get_property::<String>("Name")
247 .map_err(|e| Error::Platform {
248 code: -1,
249 message: format!("Get Name property failed: {}", e),
250 })
251 }
252
253 fn get_description(&self, aref: &AccessibleRef) -> Result<String> {
255 let proxy = self.make_proxy(&aref.bus_name, &aref.path, "org.a11y.atspi.Accessible")?;
256 proxy
257 .get_property::<String>("Description")
258 .map_err(|e| Error::Platform {
259 code: -1,
260 message: format!("Get Description property failed: {}", e),
261 })
262 }
263
264 fn get_atspi_children(&self, aref: &AccessibleRef) -> Result<Vec<AccessibleRef>> {
268 let proxy = self.make_proxy(&aref.bus_name, &aref.path, "org.a11y.atspi.Accessible")?;
269 let reply = proxy
270 .call_method("GetChildren", &())
271 .map_err(|e| Error::Platform {
272 code: -1,
273 message: format!("GetChildren failed: {}", e),
274 })?;
275 let children: Vec<(String, zbus::zvariant::OwnedObjectPath)> =
276 reply.body().deserialize().map_err(|e| Error::Platform {
277 code: -1,
278 message: format!("GetChildren deserialize failed: {}", e),
279 })?;
280 Ok(children
281 .into_iter()
282 .map(|(bus_name, path)| AccessibleRef {
283 bus_name,
284 path: path.to_string(),
285 })
286 .collect())
287 }
288
289 fn get_state(&self, aref: &AccessibleRef) -> Result<Vec<u32>> {
291 let proxy = self.make_proxy(&aref.bus_name, &aref.path, "org.a11y.atspi.Accessible")?;
292 let reply = proxy
293 .call_method("GetState", &())
294 .map_err(|e| Error::Platform {
295 code: -1,
296 message: format!("GetState failed: {}", e),
297 })?;
298 reply
299 .body()
300 .deserialize::<Vec<u32>>()
301 .map_err(|e| Error::Platform {
302 code: -1,
303 message: format!("GetState deserialize failed: {}", e),
304 })
305 }
306
307 fn is_multi_line(&self, aref: &AccessibleRef) -> bool {
313 let state_bits = self.get_state(aref).unwrap_or_default();
314 let bits: u64 = if state_bits.len() >= 2 {
315 (state_bits[0] as u64) | ((state_bits[1] as u64) << 32)
316 } else if state_bits.len() == 1 {
317 state_bits[0] as u64
318 } else {
319 0
320 };
321 const MULTI_LINE: u64 = 1 << 17;
323 (bits & MULTI_LINE) != 0
324 }
325
326 fn get_extents(&self, aref: &AccessibleRef) -> Option<Rect> {
330 if !self.has_interface(aref, "Component") {
331 return None;
332 }
333 let proxy = self
334 .make_proxy(&aref.bus_name, &aref.path, "org.a11y.atspi.Component")
335 .ok()?;
336 let reply = proxy.call_method("GetExtents", &(0u32,)).ok()?;
339 let (x, y, w, h): (i32, i32, i32, i32) = reply.body().deserialize().ok()?;
340 if w <= 0 && h <= 0 {
341 return None;
342 }
343 Some(Rect {
344 x,
345 y,
346 width: w.max(0) as u32,
347 height: h.max(0) as u32,
348 })
349 }
350
351 fn get_actions(&self, aref: &AccessibleRef) -> (Vec<String>, HashMap<String, i32>) {
357 let mut actions = Vec::new();
358 let mut indices = HashMap::new();
359
360 if let Ok(proxy) = self.make_proxy(&aref.bus_name, &aref.path, "org.a11y.atspi.Action") {
362 let n_actions = proxy
364 .get_property::<i32>("NActions")
365 .or_else(|_| proxy.get_property::<u32>("NActions").map(|n| n as i32))
366 .unwrap_or(0);
367 for i in 0..n_actions {
368 if let Ok(reply) = proxy.call_method("GetName", &(i,)) {
369 if let Ok(name) = reply.body().deserialize::<String>() {
370 if let Some(action_name) = map_atspi_action_name(&name) {
371 if !actions.contains(&action_name) {
372 indices.insert(action_name.clone(), i);
373 actions.push(action_name);
374 }
375 }
376 }
377 }
378 }
379 }
380
381 (actions, indices)
389 }
390
391 fn is_gtk_toolkit(&self, aref: &AccessibleRef) -> bool {
398 let app_root = AccessibleRef {
399 bus_name: aref.bus_name.clone(),
400 path: "/org/a11y/atspi/accessible/root".to_string(),
401 };
402 self.make_proxy(
403 &app_root.bus_name,
404 &app_root.path,
405 "org.a11y.atspi.Application",
406 )
407 .ok()
408 .and_then(|proxy| proxy.get_property::<String>("ToolkitName").ok())
409 .map(|t| t.eq_ignore_ascii_case("GTK"))
410 .unwrap_or(false)
411 }
412
413 fn find_gtk_press_fallback(
426 &self,
427 outer: &AccessibleRef,
428 outer_name: &str,
429 ) -> Option<(AccessibleRef, i32)> {
430 let mut queue: VecDeque<(AccessibleRef, u32)> = VecDeque::new();
431 queue.push_back((outer.clone(), 0));
432 let mut visited: usize = 0;
433 let mut shallowest_depth: Option<u32> = None;
434 let mut hits: Vec<(AccessibleRef, i32)> = Vec::new();
435
436 while let Some((node, depth)) = queue.pop_front() {
437 if let Some(best) = shallowest_depth {
439 if depth > best {
440 continue;
441 }
442 }
443 if visited > GTK_FALLBACK_MAX_NODES {
444 break;
445 }
446
447 let role_name = if depth == 0 {
448 String::new()
449 } else {
450 visited += 1;
451 self.get_role_name(&node).unwrap_or_default().to_lowercase()
452 };
453
454 if depth > 0 && is_actionable_atspi_role(&role_name) {
455 if let Some(idx) = self.gtk_fallback_pick(&node, outer_name) {
456 match shallowest_depth {
457 Some(d) if depth < d => {
458 shallowest_depth = Some(depth);
459 hits.clear();
460 hits.push((node.clone(), idx));
461 }
462 Some(d) if depth == d => hits.push((node.clone(), idx)),
463 Some(_) => {}
464 None => {
465 shallowest_depth = Some(depth);
466 hits.push((node.clone(), idx));
467 }
468 }
469 }
470 }
471
472 let stop_descending = depth >= GTK_FALLBACK_MAX_DEPTH
477 || (depth > 0 && is_never_descend_atspi_role(&role_name));
478 if stop_descending {
479 continue;
480 }
481 if let Ok(children) = self.get_atspi_children(&node) {
482 for c in children {
483 queue.push_back((c, depth + 1));
484 }
485 }
486 }
487
488 if hits.len() == 1 {
489 Some(hits.into_iter().next().unwrap())
490 } else {
491 None
492 }
493 }
494
495 fn gtk_fallback_pick(&self, aref: &AccessibleRef, outer_name: &str) -> Option<i32> {
498 let (_, index_map) = self.get_actions(aref);
499 let idx = *index_map.get("press")?;
500 if !self.is_showing_visible_sensitive(aref) {
501 return None;
502 }
503 if !outer_name.is_empty() {
507 let inner_name = self.get_name(aref).unwrap_or_default();
508 if !inner_name.is_empty() && inner_name != outer_name {
509 return None;
510 }
511 }
512 Some(idx)
513 }
514
515 fn is_showing_visible_sensitive(&self, aref: &AccessibleRef) -> bool {
520 let raw = self.get_state(aref).unwrap_or_default();
521 let bits: u64 = if raw.len() >= 2 {
522 (raw[0] as u64) | ((raw[1] as u64) << 32)
523 } else if raw.len() == 1 {
524 raw[0] as u64
525 } else {
526 0
527 };
528 const SENSITIVE: u64 = 1 << 24;
529 const SHOWING: u64 = 1 << 25;
530 const VISIBLE: u64 = 1 << 30;
531 (bits & (SHOWING | VISIBLE | SENSITIVE)) == (SHOWING | VISIBLE | SENSITIVE)
532 }
533
534 fn get_value(&self, aref: &AccessibleRef) -> Option<String> {
537 let text_value = self.get_text_content(aref);
541 if text_value.is_some() {
542 return text_value;
543 }
544 if let Ok(proxy) = self.make_proxy(&aref.bus_name, &aref.path, "org.a11y.atspi.Value") {
546 if let Ok(val) = proxy.get_property::<f64>("CurrentValue") {
547 return Some(val.to_string());
548 }
549 }
550 None
551 }
552
553 fn get_text_content(&self, aref: &AccessibleRef) -> Option<String> {
555 let proxy = self
556 .make_proxy(&aref.bus_name, &aref.path, "org.a11y.atspi.Text")
557 .ok()?;
558 let char_count: i32 = proxy.get_property("CharacterCount").ok()?;
559 if char_count > 0 {
560 let reply = proxy.call_method("GetText", &(0i32, char_count)).ok()?;
561 let text: String = reply.body().deserialize().ok()?;
562 if !text.is_empty() {
563 return Some(text);
564 }
565 }
566 None
567 }
568
569 fn cache_element(&self, aref: AccessibleRef) -> u64 {
571 let handle = NEXT_HANDLE.fetch_add(1, Ordering::Relaxed);
572 self.handle_cache
573 .lock()
574 .unwrap_or_else(|e| e.into_inner())
575 .insert(handle, aref);
576 handle
577 }
578
579 fn get_cached(&self, handle: u64) -> Result<AccessibleRef> {
581 self.handle_cache
582 .lock()
583 .unwrap_or_else(|e| e.into_inner())
584 .get(&handle)
585 .cloned()
586 .ok_or(Error::ElementStale {
587 selector: format!("handle:{}", handle),
588 })
589 }
590
591 fn build_element_data(&self, aref: &AccessibleRef, pid: Option<u32>) -> ElementData {
596 let role_name = self.get_role_name(aref).unwrap_or_default();
597 let role_num = self.get_role_number(aref).unwrap_or(0);
598 let role = {
599 let by_name = if !role_name.is_empty() {
600 map_atspi_role(&role_name)
601 } else {
602 Role::Unknown
603 };
604 let coarse = if by_name != Role::Unknown {
605 by_name
606 } else {
607 map_atspi_role_number(role_num)
608 };
609 if coarse == Role::TextArea && !self.is_multi_line(aref) {
610 Role::TextField
611 } else {
612 coarse
613 }
614 };
615
616 let (
620 ((mut name, value), description),
621 (
622 (states, bounds),
623 ((actions, action_index_map), (numeric_value, min_value, max_value)),
624 ),
625 ) = rayon::join(
626 || {
627 rayon::join(
628 || {
629 let name = self.get_name(aref).ok().filter(|s| !s.is_empty());
630 let value = if role_has_value(role) {
631 self.get_value(aref)
632 } else {
633 None
634 };
635 (name, value)
636 },
637 || self.get_description(aref).ok().filter(|s| !s.is_empty()),
638 )
639 },
640 || {
641 rayon::join(
642 || {
643 rayon::join(
644 || self.parse_states(aref, role),
645 || {
646 if role != Role::Application {
647 self.get_extents(aref)
648 } else {
649 None
650 }
651 },
652 )
653 },
654 || {
655 rayon::join(
656 || {
657 if role_has_actions(role) {
658 self.get_actions(aref)
659 } else {
660 (vec![], HashMap::new())
661 }
662 },
663 || {
664 if matches!(
665 role,
666 Role::Slider
667 | Role::ProgressBar
668 | Role::ScrollBar
669 | Role::SpinButton
670 ) {
671 if let Ok(proxy) = self.make_proxy(
672 &aref.bus_name,
673 &aref.path,
674 "org.a11y.atspi.Value",
675 ) {
676 (
677 proxy.get_property::<f64>("CurrentValue").ok(),
678 proxy.get_property::<f64>("MinimumValue").ok(),
679 proxy.get_property::<f64>("MaximumValue").ok(),
680 )
681 } else {
682 (None, None, None)
683 }
684 } else {
685 (None, None, None)
686 }
687 },
688 )
689 },
690 )
691 },
692 );
693
694 if name.is_none() && role == Role::StaticText {
697 if let Some(ref v) = value {
698 name = Some(v.clone());
699 }
700 }
701
702 let raw = {
703 let raw_role = if role_name.is_empty() {
704 format!("role_num:{}", role_num)
705 } else {
706 role_name
707 };
708 {
709 let mut raw = HashMap::new();
710 raw.insert("atspi_role".into(), serde_json::Value::String(raw_role));
711 raw.insert(
712 "bus_name".into(),
713 serde_json::Value::String(aref.bus_name.clone()),
714 );
715 raw.insert(
716 "object_path".into(),
717 serde_json::Value::String(aref.path.clone()),
718 );
719 raw
720 }
721 };
722
723 let handle = self.cache_element(aref.clone());
724 if !action_index_map.is_empty() {
725 self.action_indices
726 .lock()
727 .unwrap_or_else(|e| e.into_inner())
728 .insert(handle, action_index_map);
729 }
730
731 ElementData {
732 role,
733 name,
734 value,
735 description,
736 bounds,
737 actions,
738 states,
739 numeric_value,
740 min_value,
741 max_value,
742 pid,
743 stable_id: Some(aref.path.clone()),
744 raw,
745 handle,
746 }
747 }
748
749 fn get_atspi_parent(&self, aref: &AccessibleRef) -> Result<Option<AccessibleRef>> {
751 let proxy = self.make_proxy(
753 &aref.bus_name,
754 &aref.path,
755 "org.freedesktop.DBus.Properties",
756 )?;
757 let reply = proxy
758 .call_method("Get", &("org.a11y.atspi.Accessible", "Parent"))
759 .map_err(|e| Error::Platform {
760 code: -1,
761 message: format!("Get Parent property failed: {}", e),
762 })?;
763 let variant: zbus::zvariant::OwnedValue =
765 reply.body().deserialize().map_err(|e| Error::Platform {
766 code: -1,
767 message: format!("Parent deserialize variant failed: {}", e),
768 })?;
769 let (bus, path): (String, zbus::zvariant::OwnedObjectPath) =
770 zbus::zvariant::Value::from(variant).try_into().map_err(
771 |e: zbus::zvariant::Error| Error::Platform {
772 code: -1,
773 message: format!("Parent deserialize struct failed: {}", e),
774 },
775 )?;
776 let path_str = path.as_str();
777 if path_str == "/org/a11y/atspi/null" || bus.is_empty() || path_str.is_empty() {
778 return Ok(None);
779 }
780 if path_str == "/org/a11y/atspi/accessible/root" {
782 return Ok(None);
783 }
784 Ok(Some(AccessibleRef {
785 bus_name: bus,
786 path: path_str.to_string(),
787 }))
788 }
789
790 fn parse_states(&self, aref: &AccessibleRef, role: Role) -> StateSet {
792 let state_bits = self.get_state(aref).unwrap_or_default();
793
794 let bits: u64 = if state_bits.len() >= 2 {
796 (state_bits[0] as u64) | ((state_bits[1] as u64) << 32)
797 } else if state_bits.len() == 1 {
798 state_bits[0] as u64
799 } else {
800 0
801 };
802
803 const BUSY: u64 = 1 << 3;
805 const CHECKED: u64 = 1 << 4;
806 const EDITABLE: u64 = 1 << 7;
807 const ENABLED: u64 = 1 << 8;
808 const EXPANDABLE: u64 = 1 << 9;
809 const EXPANDED: u64 = 1 << 10;
810 const FOCUSABLE: u64 = 1 << 11;
811 const FOCUSED: u64 = 1 << 12;
812 const MODAL: u64 = 1 << 16;
813 const SELECTED: u64 = 1 << 23;
814 const SENSITIVE: u64 = 1 << 24;
815 const SHOWING: u64 = 1 << 25;
816 const VISIBLE: u64 = 1 << 30;
817 const INDETERMINATE: u64 = 1 << 32;
818 const REQUIRED: u64 = 1 << 33;
819
820 let enabled = (bits & ENABLED) != 0 || (bits & SENSITIVE) != 0;
821 let visible = (bits & VISIBLE) != 0 || (bits & SHOWING) != 0;
822
823 let checked = match role {
824 Role::CheckBox | Role::RadioButton | Role::MenuItem | Role::Switch => {
825 if (bits & INDETERMINATE) != 0 {
826 Some(Toggled::Mixed)
827 } else if (bits & CHECKED) != 0 {
828 Some(Toggled::On)
829 } else {
830 Some(Toggled::Off)
831 }
832 }
833 _ => None,
834 };
835
836 let expanded = if (bits & EXPANDABLE) != 0 {
837 Some((bits & EXPANDED) != 0)
838 } else {
839 None
840 };
841
842 StateSet {
843 enabled,
844 visible,
845 focused: (bits & FOCUSED) != 0,
846 checked,
847 selected: (bits & SELECTED) != 0,
848 expanded,
849 editable: (bits & EDITABLE) != 0,
850 focusable: (bits & FOCUSABLE) != 0,
851 modal: (bits & MODAL) != 0,
852 required: (bits & REQUIRED) != 0,
853 busy: (bits & BUSY) != 0,
854 }
855 }
856
857 pub(crate) fn find_app_by_pid(&self, pid: u32) -> Result<AccessibleRef> {
862 let registry = AccessibleRef {
863 bus_name: "org.a11y.atspi.Registry".to_string(),
864 path: "/org/a11y/atspi/accessible/root".to_string(),
865 };
866 let children = self.get_atspi_children(®istry)?;
867
868 for child in &children {
869 if child.path == "/org/a11y/atspi/null" {
870 continue;
871 }
872 if let Ok(proxy) =
874 self.make_proxy(&child.bus_name, &child.path, "org.a11y.atspi.Application")
875 {
876 if let Ok(app_pid) = proxy.get_property::<i32>("Id") {
877 if app_pid as u32 == pid {
878 return Ok(child.clone());
879 }
880 }
881 }
882 if let Some(app_pid) = self.get_dbus_pid(&child.bus_name) {
884 if app_pid == pid {
885 return Ok(child.clone());
886 }
887 }
888 }
889
890 Err(Error::Platform {
891 code: -1,
892 message: format!("No application found with PID {}", pid),
893 })
894 }
895
896 fn get_dbus_pid(&self, bus_name: &str) -> Option<u32> {
898 let proxy = self
899 .make_proxy(
900 "org.freedesktop.DBus",
901 "/org/freedesktop/DBus",
902 "org.freedesktop.DBus",
903 )
904 .ok()?;
905 let reply = proxy
906 .call_method("GetConnectionUnixProcessID", &(bus_name,))
907 .ok()?;
908 let pid: u32 = reply.body().deserialize().ok()?;
909 if pid > 0 {
910 Some(pid)
911 } else {
912 None
913 }
914 }
915
916 fn do_atspi_action_by_index(&self, aref: &AccessibleRef, index: i32) -> Result<()> {
918 let proxy = self.make_proxy(&aref.bus_name, &aref.path, "org.a11y.atspi.Action")?;
919 proxy
920 .call_method("DoAction", &(index,))
921 .map_err(|e| Error::Platform {
922 code: -1,
923 message: format!("DoAction({}) failed: {}", index, e),
924 })?;
925 Ok(())
926 }
927
928 fn get_action_index(&self, handle: u64, action: &str) -> Result<i32> {
930 self.action_indices
931 .lock()
932 .unwrap_or_else(|e| e.into_inner())
933 .get(&handle)
934 .and_then(|map| map.get(action).copied())
935 .ok_or_else(|| Error::ActionNotSupported {
936 action: action.to_string(),
937 role: Role::Unknown, })
939 }
940
941 fn get_app_pid(&self, aref: &AccessibleRef) -> Option<u32> {
943 if let Ok(proxy) = self.make_proxy(&aref.bus_name, &aref.path, "org.a11y.atspi.Application")
945 {
946 if let Ok(pid) = proxy.get_property::<i32>("Id") {
947 if pid > 0 {
948 return Some(pid as u32);
949 }
950 }
951 }
952
953 if let Ok(proxy) = self.make_proxy(
955 "org.freedesktop.DBus",
956 "/org/freedesktop/DBus",
957 "org.freedesktop.DBus",
958 ) {
959 if let Ok(reply) =
960 proxy.call_method("GetConnectionUnixProcessID", &(aref.bus_name.as_str(),))
961 {
962 if let Ok(pid) = reply.body().deserialize::<u32>() {
963 if pid > 0 {
964 return Some(pid);
965 }
966 }
967 }
968 }
969
970 None
971 }
972
973 fn resolve_role(&self, aref: &AccessibleRef) -> Role {
975 let role_name = self.get_role_name(aref).unwrap_or_default();
976 let by_name = if !role_name.is_empty() {
977 map_atspi_role(&role_name)
978 } else {
979 Role::Unknown
980 };
981 let coarse = if by_name != Role::Unknown {
982 by_name
983 } else {
984 let role_num = self.get_role_number(aref).unwrap_or(0);
986 map_atspi_role_number(role_num)
987 };
988 if coarse == Role::TextArea && !self.is_multi_line(aref) {
990 Role::TextField
991 } else {
992 coarse
993 }
994 }
995
996 fn matches_ref(
1010 &self,
1011 aref: &AccessibleRef,
1012 simple: &xa11y_core::selector::SimpleSelector,
1013 ) -> bool {
1014 let needs_role = simple.role.is_some()
1017 || simple
1018 .filters
1019 .iter()
1020 .any(|f| matches!(f.attr.as_str(), "role" | "checked"));
1021 let role = if needs_role {
1022 Some(self.resolve_role(aref))
1023 } else {
1024 None
1025 };
1026
1027 if let Some(ref role_match) = simple.role {
1028 match role_match {
1029 xa11y_core::selector::RoleMatch::Normalized(expected) => {
1030 if role != Some(*expected) {
1031 return false;
1032 }
1033 }
1034 xa11y_core::selector::RoleMatch::Platform(platform_role) => {
1035 let raw_role = self.get_role_name(aref).unwrap_or_default();
1036 if raw_role != *platform_role {
1037 return false;
1038 }
1039 }
1040 }
1041 }
1042
1043 let mut state_set: Option<StateSet> = None;
1044
1045 for filter in &simple.filters {
1046 let attr = filter.attr.as_str();
1047 let resolved: Option<Option<String>> = match attr {
1048 "role" => Some(role.map(|r| r.to_snake_case().to_string())),
1049 "name" => {
1050 let name = self.get_name(aref).ok().filter(|s| !s.is_empty());
1051 let resolved = if name.is_none() && role == Some(Role::StaticText) {
1054 self.get_value(aref)
1055 } else {
1056 name
1057 };
1058 Some(resolved)
1059 }
1060 "value" => Some(self.get_value(aref)),
1061 "description" => Some(self.get_description(aref).ok().filter(|s| !s.is_empty())),
1062 "enabled" | "visible" | "focused" | "focusable" | "selected" | "editable"
1063 | "modal" | "required" | "busy" | "expanded" | "checked" => {
1064 let s = state_set.get_or_insert_with(|| {
1065 self.parse_states(aref, role.unwrap_or(Role::Unknown))
1069 });
1070 Some(state_attr_to_string(attr, s))
1071 }
1072 _ => None, };
1074
1075 match resolved {
1076 Some(value) => {
1077 if !xa11y_core::selector::match_op(&filter.op, &filter.value, value.as_deref())
1078 {
1079 return false;
1080 }
1081 }
1082 None => {
1083 let pid = None; let data = self.build_element_data(aref, pid);
1091 return xa11y_core::selector::matches_simple(&data, simple);
1092 }
1093 }
1094 }
1095
1096 true
1097 }
1098
1099 fn collect_matching_refs(
1105 &self,
1106 parent: &AccessibleRef,
1107 simple: &xa11y_core::selector::SimpleSelector,
1108 depth: u32,
1109 max_depth: u32,
1110 limit: Option<usize>,
1111 ) -> Result<Vec<AccessibleRef>> {
1112 if depth > max_depth {
1113 return Ok(vec![]);
1114 }
1115
1116 let children = self.get_atspi_children(parent)?;
1117
1118 let parent_is_registry = parent.bus_name == "org.a11y.atspi.Registry";
1126 let mut to_search: Vec<AccessibleRef> = Vec::new();
1127 for child in children {
1128 if child.path == "/org/a11y/atspi/null"
1129 || child.bus_name.is_empty()
1130 || child.path.is_empty()
1131 {
1132 continue;
1133 }
1134
1135 if !parent_is_registry {
1136 let child_role = self.get_role_name(&child).unwrap_or_default();
1137 if child_role == "application" {
1138 let grandchildren = self.get_atspi_children(&child).unwrap_or_default();
1139 for gc in grandchildren {
1140 if gc.path == "/org/a11y/atspi/null"
1141 || gc.bus_name.is_empty()
1142 || gc.path.is_empty()
1143 {
1144 continue;
1145 }
1146 let gc_role = self.get_role_name(&gc).unwrap_or_default();
1147 if gc_role == "application" {
1148 continue;
1149 }
1150 to_search.push(gc);
1151 }
1152 continue;
1153 }
1154 }
1155 to_search.push(child);
1156 }
1157
1158 if to_search.is_empty() {
1161 self.check_chromium_a11y_enabled(parent, None)?;
1162 }
1163
1164 let per_child: Vec<(Vec<AccessibleRef>, Option<Error>)> = to_search
1173 .par_iter()
1174 .map(|child| {
1175 let mut child_results = Vec::new();
1176 if self.matches_ref(child, simple) {
1177 child_results.push(child.clone());
1178 }
1179 match self.collect_matching_refs(child, simple, depth + 1, max_depth, limit) {
1180 Ok(sub) => {
1181 child_results.extend(sub);
1182 (child_results, None)
1183 }
1184 Err(e @ Error::AccessibilityNotEnabled { .. }) => (Vec::new(), Some(e)),
1185 Err(_) => (child_results, None),
1186 }
1187 })
1188 .collect();
1189
1190 let mut results = Vec::new();
1195 for (batch, maybe_err) in per_child {
1196 if let Some(err) = maybe_err {
1197 return Err(err);
1198 }
1199 for r in batch {
1200 results.push(r);
1201 if let Some(limit) = limit {
1202 if results.len() >= limit {
1203 return Ok(results);
1204 }
1205 }
1206 }
1207 }
1208 Ok(results)
1209 }
1210}
1211
1212impl Provider for LinuxProvider {
1213 fn get_children(&self, element: Option<&ElementData>) -> Result<Vec<ElementData>> {
1214 match element {
1215 None => {
1216 let registry = AccessibleRef {
1218 bus_name: "org.a11y.atspi.Registry".to_string(),
1219 path: "/org/a11y/atspi/accessible/root".to_string(),
1220 };
1221 let children = self.get_atspi_children(®istry)?;
1222
1223 let valid: Vec<(&AccessibleRef, String)> = children
1225 .iter()
1226 .filter(|c| c.path != "/org/a11y/atspi/null")
1227 .filter_map(|c| {
1228 let name = self.get_name(c).unwrap_or_default();
1229 if name.is_empty() {
1230 None
1231 } else {
1232 Some((c, name))
1233 }
1234 })
1235 .collect();
1236
1237 let results: Vec<ElementData> = valid
1238 .par_iter()
1239 .map(|(child, app_name)| {
1240 let pid = self.get_app_pid(child);
1241 let mut data = self.build_element_data(child, pid);
1242 data.name = Some(app_name.clone());
1243 data
1244 })
1245 .collect();
1246
1247 Ok(results)
1248 }
1249 Some(element_data) => {
1250 let aref = self.get_cached(element_data.handle)?;
1251 let children = self.get_atspi_children(&aref).unwrap_or_default();
1252 let pid = element_data.pid;
1253
1254 let mut to_build: Vec<AccessibleRef> = Vec::new();
1257 for child_ref in &children {
1258 if child_ref.path == "/org/a11y/atspi/null"
1259 || child_ref.bus_name.is_empty()
1260 || child_ref.path.is_empty()
1261 {
1262 continue;
1263 }
1264 let child_role = self.get_role_name(child_ref).unwrap_or_default();
1265 if child_role == "application" {
1266 let grandchildren = self.get_atspi_children(child_ref).unwrap_or_default();
1267 for gc_ref in grandchildren {
1268 if gc_ref.path == "/org/a11y/atspi/null"
1269 || gc_ref.bus_name.is_empty()
1270 || gc_ref.path.is_empty()
1271 {
1272 continue;
1273 }
1274 let gc_role = self.get_role_name(&gc_ref).unwrap_or_default();
1275 if gc_role == "application" {
1276 continue;
1277 }
1278 to_build.push(gc_ref);
1279 }
1280 continue;
1281 }
1282 to_build.push(child_ref.clone());
1283 }
1284
1285 if to_build.is_empty() {
1286 self.check_chromium_a11y_enabled(&aref, Some(element_data.role))?;
1287 }
1288
1289 let results: Vec<ElementData> = to_build
1290 .par_iter()
1291 .map(|r| self.build_element_data(r, pid))
1292 .collect();
1293
1294 Ok(results)
1295 }
1296 }
1297 }
1298
1299 fn find_elements(
1300 &self,
1301 root: Option<&ElementData>,
1302 selector: &Selector,
1303 limit: Option<usize>,
1304 max_depth: Option<u32>,
1305 ) -> Result<Vec<ElementData>> {
1306 if selector.segments.is_empty() {
1307 return Ok(vec![]);
1308 }
1309
1310 let max_depth_val = max_depth.unwrap_or(xa11y_core::MAX_TREE_DEPTH);
1311
1312 let first = &selector.segments[0].simple;
1315
1316 let phase1_limit = if selector.segments.len() == 1 {
1317 limit
1318 } else {
1319 None
1320 };
1321 let phase1_limit = match (phase1_limit, first.nth) {
1322 (Some(l), Some(n)) => Some(l.max(n)),
1323 (_, Some(n)) => Some(n),
1324 (l, None) => l,
1325 };
1326
1327 let phase1_depth = if root.is_none()
1329 && matches!(
1330 first.role,
1331 Some(xa11y_core::selector::RoleMatch::Normalized(
1332 Role::Application
1333 ))
1334 ) {
1335 0
1336 } else {
1337 max_depth_val
1338 };
1339
1340 let start_ref = match root {
1341 None => AccessibleRef {
1342 bus_name: "org.a11y.atspi.Registry".to_string(),
1343 path: "/org/a11y/atspi/accessible/root".to_string(),
1344 },
1345 Some(el) => self.get_cached(el.handle)?,
1346 };
1347
1348 let mut matching_refs =
1349 self.collect_matching_refs(&start_ref, first, 0, phase1_depth, phase1_limit)?;
1350
1351 let pid_from_root = root.and_then(|r| r.pid);
1352
1353 if selector.segments.len() == 1 {
1355 if let Some(nth) = first.nth {
1356 if nth <= matching_refs.len() {
1357 let aref = &matching_refs[nth - 1];
1358 let pid = if root.is_none() {
1359 self.get_app_pid(aref)
1360 .or_else(|| self.get_dbus_pid(&aref.bus_name))
1361 } else {
1362 pid_from_root
1363 };
1364 return Ok(vec![self.build_element_data(aref, pid)]);
1365 } else {
1366 return Ok(vec![]);
1367 }
1368 }
1369
1370 if let Some(limit) = limit {
1371 matching_refs.truncate(limit);
1372 }
1373
1374 let is_root_search = root.is_none();
1375 return Ok(matching_refs
1376 .par_iter()
1377 .map(|aref| {
1378 let pid = if is_root_search {
1379 self.get_app_pid(aref)
1380 .or_else(|| self.get_dbus_pid(&aref.bus_name))
1381 } else {
1382 pid_from_root
1383 };
1384 self.build_element_data(aref, pid)
1385 })
1386 .collect());
1387 }
1388
1389 let is_root_search = root.is_none();
1392 let mut candidates: Vec<ElementData> = matching_refs
1393 .par_iter()
1394 .map(|aref| {
1395 let pid = if is_root_search {
1396 self.get_app_pid(aref)
1397 .or_else(|| self.get_dbus_pid(&aref.bus_name))
1398 } else {
1399 pid_from_root
1400 };
1401 self.build_element_data(aref, pid)
1402 })
1403 .collect();
1404
1405 for segment in &selector.segments[1..] {
1406 let mut next_candidates = Vec::new();
1407 for candidate in &candidates {
1408 match segment.combinator {
1409 Combinator::Child => {
1410 let children = self.get_children(Some(candidate))?;
1411 for child in children {
1412 if xa11y_core::selector::matches_simple(&child, &segment.simple) {
1413 next_candidates.push(child);
1414 }
1415 }
1416 }
1417 Combinator::Descendant => {
1418 let sub_selector = Selector {
1419 segments: vec![SelectorSegment {
1420 combinator: Combinator::Root,
1421 simple: segment.simple.clone(),
1422 }],
1423 };
1424 let mut sub_results = xa11y_core::selector::find_elements_in_tree(
1425 |el| self.get_children(el),
1426 Some(candidate),
1427 &sub_selector,
1428 None,
1429 Some(max_depth_val),
1430 )?;
1431 next_candidates.append(&mut sub_results);
1432 }
1433 Combinator::Root => unreachable!(),
1434 }
1435 }
1436 let mut seen = HashSet::new();
1437 next_candidates.retain(|e| seen.insert(e.handle));
1438 candidates = next_candidates;
1439 }
1440
1441 if let Some(nth) = selector.segments.last().and_then(|s| s.simple.nth) {
1443 if nth <= candidates.len() {
1444 candidates = vec![candidates.remove(nth - 1)];
1445 } else {
1446 candidates.clear();
1447 }
1448 }
1449
1450 if let Some(limit) = limit {
1451 candidates.truncate(limit);
1452 }
1453
1454 Ok(candidates)
1455 }
1456
1457 fn get_parent(&self, element: &ElementData) -> Result<Option<ElementData>> {
1458 let aref = self.get_cached(element.handle)?;
1459 match self.get_atspi_parent(&aref)? {
1460 Some(parent_ref) => {
1461 let data = self.build_element_data(&parent_ref, element.pid);
1462 Ok(Some(data))
1463 }
1464 None => Ok(None),
1465 }
1466 }
1467
1468 fn press(&self, element: &ElementData) -> Result<()> {
1469 let target = self.get_cached(element.handle)?;
1470 if let Ok(index) = self.get_action_index(element.handle, "press") {
1472 return self.do_atspi_action_by_index(&target, index);
1473 }
1474 if element.role == Role::Button && self.is_gtk_toolkit(&target) {
1493 let outer_name = self.get_name(&target).unwrap_or_default();
1494 if let Some((inner, index)) = self.find_gtk_press_fallback(&target, &outer_name) {
1495 return self.do_atspi_action_by_index(&inner, index);
1496 }
1497 }
1498 Err(Error::ActionNotSupported {
1499 action: "press".to_string(),
1500 role: element.role,
1501 })
1502 }
1503
1504 fn focus(&self, element: &ElementData) -> Result<()> {
1505 let target = self.get_cached(element.handle)?;
1506 if let Ok(proxy) =
1510 self.make_proxy(&target.bus_name, &target.path, "org.a11y.atspi.Component")
1511 {
1512 if let Ok(reply) = proxy.call_method("GrabFocus", &()) {
1513 if let Ok(true) = reply.body().deserialize::<bool>() {
1515 return Ok(());
1516 }
1517 }
1519 }
1520 if let Ok(index) = self.get_action_index(element.handle, "focus") {
1521 return self.do_atspi_action_by_index(&target, index);
1522 }
1523 Err(Error::ActionNotSupported {
1524 action: "focus".to_string(),
1525 role: element.role,
1526 })
1527 }
1528
1529 fn blur(&self, element: &ElementData) -> Result<()> {
1530 let target = self.get_cached(element.handle)?;
1531 if let Some(parent_ref) = self.get_atspi_parent(&target)? {
1535 if parent_ref.path != "/org/a11y/atspi/null" {
1536 let p = self.make_proxy(
1537 &parent_ref.bus_name,
1538 &parent_ref.path,
1539 "org.a11y.atspi.Component",
1540 )?;
1541 p.call_method("GrabFocus", &())
1542 .map_err(|e| Error::Platform {
1543 code: -1,
1544 message: format!("Component.GrabFocus on parent failed: {}", e),
1545 })?;
1546 return Ok(());
1547 }
1548 }
1549 Err(Error::ActionNotSupported {
1550 action: "blur".to_string(),
1551 role: element.role,
1552 })
1553 }
1554
1555 fn toggle(&self, element: &ElementData) -> Result<()> {
1556 let target = self.get_cached(element.handle)?;
1557 let index = self
1558 .get_action_index(element.handle, "toggle")
1559 .map_err(|_| Error::ActionNotSupported {
1560 action: "toggle".to_string(),
1561 role: element.role,
1562 })?;
1563 self.do_atspi_action_by_index(&target, index)
1564 }
1565
1566 fn select(&self, element: &ElementData) -> Result<()> {
1567 let target = self.get_cached(element.handle)?;
1568 let index = self
1569 .get_action_index(element.handle, "select")
1570 .map_err(|_| Error::ActionNotSupported {
1571 action: "select".to_string(),
1572 role: element.role,
1573 })?;
1574 self.do_atspi_action_by_index(&target, index)
1575 }
1576
1577 fn expand(&self, element: &ElementData) -> Result<()> {
1578 let target = self.get_cached(element.handle)?;
1579 let index = self
1580 .get_action_index(element.handle, "expand")
1581 .map_err(|_| Error::ActionNotSupported {
1582 action: "expand".to_string(),
1583 role: element.role,
1584 })?;
1585 self.do_atspi_action_by_index(&target, index)
1586 }
1587
1588 fn collapse(&self, element: &ElementData) -> Result<()> {
1589 let target = self.get_cached(element.handle)?;
1590 let index = self
1591 .get_action_index(element.handle, "collapse")
1592 .map_err(|_| Error::ActionNotSupported {
1593 action: "collapse".to_string(),
1594 role: element.role,
1595 })?;
1596 self.do_atspi_action_by_index(&target, index)
1597 }
1598
1599 fn show_menu(&self, element: &ElementData) -> Result<()> {
1600 let target = self.get_cached(element.handle)?;
1601 let index = self
1602 .get_action_index(element.handle, "show_menu")
1603 .map_err(|_| Error::ActionNotSupported {
1604 action: "show_menu".to_string(),
1605 role: element.role,
1606 })?;
1607 self.do_atspi_action_by_index(&target, index)
1608 }
1609
1610 fn increment(&self, element: &ElementData) -> Result<()> {
1611 let target = self.get_cached(element.handle)?;
1612 if let Ok(index) = self.get_action_index(element.handle, "increment") {
1614 return self.do_atspi_action_by_index(&target, index);
1615 }
1616 let proxy = self.make_proxy(&target.bus_name, &target.path, "org.a11y.atspi.Value")?;
1617 let current: f64 = proxy
1618 .get_property("CurrentValue")
1619 .map_err(|e| Error::Platform {
1620 code: -1,
1621 message: format!("Value.CurrentValue failed: {}", e),
1622 })?;
1623 let step: f64 = proxy.get_property("MinimumIncrement").unwrap_or(1.0);
1624 let step = if step <= 0.0 { 1.0 } else { step };
1625 proxy
1626 .set_property("CurrentValue", current + step)
1627 .map_err(|e| Error::Platform {
1628 code: -1,
1629 message: format!("Value.SetCurrentValue failed: {}", e),
1630 })
1631 }
1632
1633 fn decrement(&self, element: &ElementData) -> Result<()> {
1634 let target = self.get_cached(element.handle)?;
1635 if let Ok(index) = self.get_action_index(element.handle, "decrement") {
1636 return self.do_atspi_action_by_index(&target, index);
1637 }
1638 let proxy = self.make_proxy(&target.bus_name, &target.path, "org.a11y.atspi.Value")?;
1639 let current: f64 = proxy
1640 .get_property("CurrentValue")
1641 .map_err(|e| Error::Platform {
1642 code: -1,
1643 message: format!("Value.CurrentValue failed: {}", e),
1644 })?;
1645 let step: f64 = proxy.get_property("MinimumIncrement").unwrap_or(1.0);
1646 let step = if step <= 0.0 { 1.0 } else { step };
1647 proxy
1648 .set_property("CurrentValue", current - step)
1649 .map_err(|e| Error::Platform {
1650 code: -1,
1651 message: format!("Value.SetCurrentValue failed: {}", e),
1652 })
1653 }
1654
1655 fn scroll_into_view(&self, element: &ElementData) -> Result<()> {
1656 let target = self.get_cached(element.handle)?;
1657 let proxy = self.make_proxy(&target.bus_name, &target.path, "org.a11y.atspi.Component")?;
1658 proxy
1659 .call_method("ScrollTo", &(0u32,))
1660 .map_err(|e| Error::Platform {
1661 code: -1,
1662 message: format!("ScrollTo failed: {}", e),
1663 })?;
1664 Ok(())
1665 }
1666
1667 fn set_value(&self, element: &ElementData, value: &str) -> Result<()> {
1668 let target = self.get_cached(element.handle)?;
1669 let proxy = self
1670 .make_proxy(
1671 &target.bus_name,
1672 &target.path,
1673 "org.a11y.atspi.EditableText",
1674 )
1675 .map_err(|_| Error::TextValueNotSupported)?;
1676 if proxy.call_method("SetTextContents", &(value,)).is_ok() {
1678 return Ok(());
1679 }
1680 let classify_editable_text_error = |op: &str, e: zbus::Error| -> Error {
1686 let msg = e.to_string();
1687 if msg.contains("UnknownMethod") || msg.contains("UnknownInterface") {
1688 Error::TextValueNotSupported
1689 } else {
1690 Error::Platform {
1691 code: -1,
1692 message: format!("EditableText.{} failed: {}", op, msg),
1693 }
1694 }
1695 };
1696 if let Err(e) = proxy.call_method("DeleteText", &(0i32, i32::MAX)) {
1697 return Err(classify_editable_text_error("DeleteText", e));
1698 }
1699 if let Err(e) = proxy.call_method("InsertText", &(0i32, value, value.len() as i32)) {
1700 return Err(classify_editable_text_error("InsertText", e));
1701 }
1702 Ok(())
1703 }
1704
1705 fn set_numeric_value(&self, element: &ElementData, value: f64) -> Result<()> {
1706 let target = self.get_cached(element.handle)?;
1707 let proxy = self.make_proxy(&target.bus_name, &target.path, "org.a11y.atspi.Value")?;
1708 proxy
1709 .set_property("CurrentValue", value)
1710 .map_err(|e| Error::Platform {
1711 code: -1,
1712 message: format!("SetValue failed: {}", e),
1713 })
1714 }
1715
1716 fn type_text(&self, element: &ElementData, text: &str) -> Result<()> {
1717 let target = self.get_cached(element.handle)?;
1718 let text_proxy = self.make_proxy(&target.bus_name, &target.path, "org.a11y.atspi.Text");
1721 let insert_pos = text_proxy
1722 .as_ref()
1723 .ok()
1724 .and_then(|p| p.get_property::<i32>("CaretOffset").ok())
1725 .unwrap_or(-1); let proxy = self
1728 .make_proxy(
1729 &target.bus_name,
1730 &target.path,
1731 "org.a11y.atspi.EditableText",
1732 )
1733 .map_err(|_| Error::TextValueNotSupported)?;
1734 let pos = if insert_pos >= 0 {
1735 insert_pos
1736 } else {
1737 i32::MAX
1738 };
1739 proxy
1740 .call_method("InsertText", &(pos, text, text.len() as i32))
1741 .map_err(|e| Error::Platform {
1742 code: -1,
1743 message: format!("EditableText.InsertText failed: {}", e),
1744 })?;
1745 Ok(())
1746 }
1747
1748 fn set_text_selection(&self, element: &ElementData, start: u32, end: u32) -> Result<()> {
1749 let target = self.get_cached(element.handle)?;
1750 let proxy = self.make_proxy(&target.bus_name, &target.path, "org.a11y.atspi.Text")?;
1751 proxy
1752 .call_method("SetSelection", &(0i32, start as i32, end as i32))
1753 .map_err(|e| Error::Platform {
1754 code: -1,
1755 message: format!("Text.SetSelection failed: {}", e),
1756 })?;
1757 Ok(())
1758 }
1759
1760 fn perform_action(&self, element: &ElementData, action: &str) -> Result<()> {
1761 match action {
1762 "press" => self.press(element),
1763 "focus" => self.focus(element),
1764 "blur" => self.blur(element),
1765 "toggle" => self.toggle(element),
1766 "select" => self.select(element),
1767 "expand" => self.expand(element),
1768 "collapse" => self.collapse(element),
1769 "show_menu" => self.show_menu(element),
1770 "increment" => self.increment(element),
1771 "decrement" => self.decrement(element),
1772 "scroll_into_view" => self.scroll_into_view(element),
1773 _ => Err(Error::ActionNotSupported {
1774 action: action.to_string(),
1775 role: element.role,
1776 }),
1777 }
1778 }
1779
1780 fn subscribe(&self, element: &ElementData) -> Result<Subscription> {
1781 let pid = element.pid.ok_or(Error::Platform {
1782 code: -1,
1783 message: "Element has no PID for subscribe".to_string(),
1784 })?;
1785 let app_name = element.name.clone().unwrap_or_default();
1786 crate::events::subscribe_for_pid(self, pid, app_name)
1787 }
1788}
1789
1790fn role_has_value(role: Role) -> bool {
1793 !matches!(
1794 role,
1795 Role::Application
1796 | Role::Window
1797 | Role::Dialog
1798 | Role::Group
1799 | Role::MenuBar
1800 | Role::Toolbar
1801 | Role::TabGroup
1802 | Role::SplitGroup
1803 | Role::Table
1804 | Role::TableRow
1805 | Role::Separator
1806 )
1807}
1808
1809fn role_has_actions(role: Role) -> bool {
1812 matches!(
1813 role,
1814 Role::Button
1815 | Role::CheckBox
1816 | Role::RadioButton
1817 | Role::MenuItem
1818 | Role::Link
1819 | Role::ComboBox
1820 | Role::TextField
1821 | Role::TextArea
1822 | Role::SpinButton
1823 | Role::Tab
1824 | Role::TreeItem
1825 | Role::ListItem
1826 | Role::ScrollBar
1827 | Role::Slider
1828 | Role::Menu
1829 | Role::Image
1830 | Role::Unknown
1831 )
1832}
1833
1834pub(crate) fn map_atspi_role(role_name: &str) -> Role {
1836 match role_name.to_lowercase().as_str() {
1837 "application" => Role::Application,
1838 "window" | "frame" => Role::Window,
1839 "dialog" | "file chooser" => Role::Dialog,
1840 "alert" | "notification" => Role::Alert,
1841 "push button" | "push button menu" => Role::Button,
1842 "toggle button" => Role::Switch,
1843 "check box" | "check menu item" => Role::CheckBox,
1844 "radio button" | "radio menu item" => Role::RadioButton,
1845 "entry" | "password text" => Role::TextField,
1846 "spin button" | "spinbutton" => Role::SpinButton,
1847 "text" | "textbox" => Role::TextArea,
1851 "label" | "static" | "caption" => Role::StaticText,
1852 "combo box" | "combobox" => Role::ComboBox,
1853 "list" | "list box" | "listbox" => Role::List,
1855 "list item" => Role::ListItem,
1856 "menu" => Role::Menu,
1857 "menu item" | "tearoff menu item" => Role::MenuItem,
1858 "menu bar" => Role::MenuBar,
1859 "page tab" => Role::Tab,
1860 "page tab list" => Role::TabGroup,
1861 "table" | "tree table" => Role::Table,
1862 "table row" => Role::TableRow,
1863 "table cell" | "table column header" | "table row header" => Role::TableCell,
1864 "tool bar" => Role::Toolbar,
1865 "scroll bar" => Role::ScrollBar,
1866 "slider" => Role::Slider,
1867 "image" | "icon" | "desktop icon" => Role::Image,
1868 "link" => Role::Link,
1869 "panel" | "section" | "form" | "filler" | "viewport" | "scroll pane" => Role::Group,
1870 "progress bar" => Role::ProgressBar,
1871 "tree item" => Role::TreeItem,
1872 "document web" | "document frame" => Role::WebArea,
1873 "heading" => Role::Heading,
1874 "separator" => Role::Separator,
1875 "split pane" => Role::SplitGroup,
1876 "tooltip" | "tool tip" => Role::Tooltip,
1877 "status bar" | "statusbar" => Role::Status,
1878 "landmark" | "navigation" => Role::Navigation,
1879 _ => xa11y_core::unknown_role(role_name),
1880 }
1881}
1882
1883pub(crate) fn map_atspi_role_number(role: u32) -> Role {
1886 match role {
1887 2 => Role::Alert, 7 => Role::CheckBox, 8 => Role::CheckBox, 11 => Role::ComboBox, 16 => Role::Dialog, 19 => Role::Dialog, 20 => Role::Group, 23 => Role::Window, 26 => Role::Image, 27 => Role::Image, 29 => Role::StaticText, 31 => Role::List, 32 => Role::ListItem, 33 => Role::Menu, 34 => Role::MenuBar, 35 => Role::MenuItem, 37 => Role::Tab, 38 => Role::TabGroup, 39 => Role::Group, 40 => Role::TextField, 42 => Role::ProgressBar, 43 => Role::Button, 44 => Role::RadioButton, 45 => Role::RadioButton, 48 => Role::ScrollBar, 49 => Role::Group, 50 => Role::Separator, 51 => Role::Slider, 52 => Role::SpinButton, 53 => Role::SplitGroup, 55 => Role::Table, 56 => Role::TableCell, 57 => Role::TableCell, 58 => Role::TableCell, 61 => Role::TextArea, 62 => Role::Switch, 63 => Role::Toolbar, 65 => Role::Group, 66 => Role::Table, 67 => Role::Unknown, 68 => Role::Group, 69 => Role::Window, 75 => Role::Application, 78 => Role::TextArea, 79 => Role::TextField, 82 => Role::WebArea, 83 => Role::Heading, 85 => Role::Group, 86 => Role::Group, 87 => Role::Group, 88 => Role::Link, 90 => Role::TableRow, 91 => Role::TreeItem, 95 => Role::WebArea, 97 => Role::List, 98 => Role::List, 93 => Role::Tooltip, 101 => Role::Alert, 116 => Role::StaticText, 129 => Role::Button, _ => xa11y_core::unknown_role(&format!("AT-SPI role number {role}")),
1949 }
1950}
1951
1952const GTK_FALLBACK_MAX_DEPTH: u32 = 3;
1956
1957const GTK_FALLBACK_MAX_NODES: usize = 200;
1960
1961fn is_actionable_atspi_role(role: &str) -> bool {
1966 matches!(
1967 role,
1968 "push button"
1969 | "toggle button"
1970 | "check box"
1971 | "radio button"
1972 | "menu item"
1973 | "check menu item"
1974 | "radio menu item"
1975 | "link"
1976 | "page tab"
1977 | "tab"
1978 | "list item"
1979 | "tree item"
1980 )
1981}
1982
1983fn is_never_descend_atspi_role(role: &str) -> bool {
1988 matches!(
1989 role,
1990 "label" | "separator" | "image" | "icon" | "static" | "caption"
1991 )
1992}
1993
1994fn map_atspi_action_name(action_name: &str) -> Option<String> {
2008 let lower = action_name.to_lowercase();
2017 let collapsed: String = lower.chars().filter(|c| !matches!(c, '_' | ' ')).collect();
2018 let canonical = match collapsed.as_str() {
2019 "click" | "activate" | "press" | "invoke" | "dodefault" => "press",
2020 "toggle" | "check" | "uncheck" => "toggle",
2021 "expand" | "open" => "expand",
2022 "collapse" | "close" => "collapse",
2023 "select" => "select",
2024 "menu" | "showmenu" | "showcontextmenu" | "contextmenu" | "popup" => "show_menu",
2025 "increment" => "increment",
2026 "decrement" => "decrement",
2027 _ => return None,
2028 };
2029 Some(canonical.to_string())
2030}
2031
2032#[cfg(test)]
2033mod tests {
2034 use super::*;
2035
2036 #[test]
2037 fn test_role_mapping() {
2038 assert_eq!(map_atspi_role("push button"), Role::Button);
2039 assert_eq!(map_atspi_role("toggle button"), Role::Switch);
2040 assert_eq!(map_atspi_role("check box"), Role::CheckBox);
2041 assert_eq!(map_atspi_role("entry"), Role::TextField);
2042 assert_eq!(map_atspi_role("label"), Role::StaticText);
2043 assert_eq!(map_atspi_role("window"), Role::Window);
2044 assert_eq!(map_atspi_role("frame"), Role::Window);
2045 assert_eq!(map_atspi_role("dialog"), Role::Dialog);
2046 assert_eq!(map_atspi_role("combo box"), Role::ComboBox);
2047 assert_eq!(map_atspi_role("slider"), Role::Slider);
2048 assert_eq!(map_atspi_role("panel"), Role::Group);
2049 assert_eq!(map_atspi_role("unknown_thing"), Role::Unknown);
2050 }
2051
2052 #[test]
2053 fn test_numeric_role_mapping() {
2054 assert_eq!(map_atspi_role_number(62), Role::Switch);
2057 assert_eq!(map_atspi_role_number(43), Role::Button); assert_eq!(map_atspi_role_number(7), Role::CheckBox);
2060 assert_eq!(map_atspi_role_number(67), Role::Unknown); }
2062
2063 #[test]
2064 fn test_action_name_mapping() {
2065 assert_eq!(map_atspi_action_name("click"), Some("press".to_string()));
2066 assert_eq!(map_atspi_action_name("activate"), Some("press".to_string()));
2067 assert_eq!(map_atspi_action_name("press"), Some("press".to_string()));
2068 assert_eq!(map_atspi_action_name("invoke"), Some("press".to_string()));
2069 assert_eq!(
2071 map_atspi_action_name("doDefault"),
2072 Some("press".to_string())
2073 );
2074 assert_eq!(
2075 map_atspi_action_name("do_default"),
2076 Some("press".to_string())
2077 );
2078 assert_eq!(map_atspi_action_name("toggle"), Some("toggle".to_string()));
2079 assert_eq!(map_atspi_action_name("check"), Some("toggle".to_string()));
2080 assert_eq!(map_atspi_action_name("uncheck"), Some("toggle".to_string()));
2081 assert_eq!(map_atspi_action_name("expand"), Some("expand".to_string()));
2082 assert_eq!(map_atspi_action_name("open"), Some("expand".to_string()));
2083 assert_eq!(
2084 map_atspi_action_name("collapse"),
2085 Some("collapse".to_string())
2086 );
2087 assert_eq!(map_atspi_action_name("close"), Some("collapse".to_string()));
2088 assert_eq!(map_atspi_action_name("select"), Some("select".to_string()));
2089 assert_eq!(map_atspi_action_name("menu"), Some("show_menu".to_string()));
2090 assert_eq!(
2091 map_atspi_action_name("showmenu"),
2092 Some("show_menu".to_string())
2093 );
2094 assert_eq!(
2095 map_atspi_action_name("popup"),
2096 Some("show_menu".to_string())
2097 );
2098 assert_eq!(
2099 map_atspi_action_name("show menu"),
2100 Some("show_menu".to_string())
2101 );
2102 assert_eq!(
2105 map_atspi_action_name("showContextMenu"),
2106 Some("show_menu".to_string())
2107 );
2108 assert_eq!(
2109 map_atspi_action_name("show_context_menu"),
2110 Some("show_menu".to_string())
2111 );
2112 assert_eq!(
2113 map_atspi_action_name("increment"),
2114 Some("increment".to_string())
2115 );
2116 assert_eq!(
2117 map_atspi_action_name("decrement"),
2118 Some("decrement".to_string())
2119 );
2120 assert_eq!(map_atspi_action_name("foobar"), None);
2121 }
2122
2123 #[test]
2126 fn test_action_name_aliases_roundtrip() {
2127 let atspi_names = [
2128 "click",
2129 "activate",
2130 "press",
2131 "invoke",
2132 "doDefault",
2133 "do_default",
2134 "toggle",
2135 "check",
2136 "uncheck",
2137 "expand",
2138 "open",
2139 "collapse",
2140 "close",
2141 "select",
2142 "menu",
2143 "showmenu",
2144 "showContextMenu",
2145 "show_context_menu",
2146 "popup",
2147 "show menu",
2148 "increment",
2149 "decrement",
2150 ];
2151 for name in atspi_names {
2152 let canonical = map_atspi_action_name(name).unwrap_or_else(|| {
2153 panic!("AT-SPI2 name {:?} should map to a canonical name", name)
2154 });
2155 let back = map_atspi_action_name(&canonical)
2157 .unwrap_or_else(|| panic!("canonical {:?} should map back to itself", canonical));
2158 assert_eq!(
2159 canonical, back,
2160 "AT-SPI2 {:?} -> {:?} -> {:?} (expected {:?})",
2161 name, canonical, back, canonical
2162 );
2163 }
2164 }
2165
2166 #[test]
2168 fn test_action_name_case_insensitive() {
2169 assert_eq!(map_atspi_action_name("Click"), Some("press".to_string()));
2170 assert_eq!(map_atspi_action_name("TOGGLE"), Some("toggle".to_string()));
2171 assert_eq!(
2172 map_atspi_action_name("Increment"),
2173 Some("increment".to_string())
2174 );
2175 }
2176
2177 #[test]
2182 fn test_gtk_fallback_actionable_roles() {
2183 for role in [
2184 "push button",
2185 "toggle button",
2186 "check box",
2187 "radio button",
2188 "menu item",
2189 "link",
2190 "tab",
2191 "list item",
2192 "tree item",
2193 ] {
2194 assert!(
2195 is_actionable_atspi_role(role),
2196 "{role:?} should be actionable"
2197 );
2198 }
2199 }
2200
2201 #[test]
2206 fn test_gtk_fallback_non_actionable_roles() {
2207 for role in [
2208 "label",
2209 "panel",
2210 "filler",
2211 "section",
2212 "group",
2213 "image",
2214 "separator",
2215 "static",
2216 "frame",
2217 "window",
2218 ] {
2219 assert!(
2220 !is_actionable_atspi_role(role),
2221 "{role:?} must not be treated as actionable"
2222 );
2223 }
2224 }
2225
2226 #[test]
2230 fn test_gtk_fallback_never_descend_roles() {
2231 for role in ["label", "separator", "image", "icon", "static", "caption"] {
2232 assert!(
2233 is_never_descend_atspi_role(role),
2234 "{role:?} must block BFS descent"
2235 );
2236 }
2237 for role in ["panel", "filler", "section", "group", "frame"] {
2240 assert!(
2241 !is_never_descend_atspi_role(role),
2242 "container role {role:?} must remain descendable"
2243 );
2244 }
2245 }
2246}