forge
Explore
Log in

Don't you dare go Hollow

Implement org.freedesktop.a11y.PointerLocator

Browse files at this commit

e83850f048a2682df2987be528e9f3fb4531ad05Ivan MolodetskikhParent: a528ada
Lets the screen readers query the pointer position.
src/dbus/freedesktop_a11y.rsmodified+266 -28
@@ -12,14 +12,27 @@ use smithay::backend::input::{KeyState, Keycode};
12 use smithay::input::keyboard::{xkb, Keysym};12 use smithay::input::keyboard::{xkb, Keysym};
13 use zbus::blocking::object_server::InterfaceRef;13 use zbus::blocking::object_server::InterfaceRef;
14 use zbus::fdo::{self, RequestNameFlags};14 use zbus::fdo::{self, RequestNameFlags};
15-use zbus::interface;
16 use zbus::message::Header;15 use zbus::message::Header;
17 use zbus::names::{BusName, OwnedUniqueName, UniqueName};16 use zbus::names::{BusName, OwnedUniqueName, UniqueName};
18 use zbus::object_server::SignalEmitter;17 use zbus::object_server::SignalEmitter;
19-use zbus::zvariant::NoneValue;18+use zbus::zvariant::{NoneValue, OwnedObjectPath, SerializeDict, Type, Value};
19+use zbus::{interface, DBusError};
20 20
21-use super::Start;21+use crate::niri::{PointContents, State};
22-use crate::niri::State;22+use crate::utils::get_credentials_for_surface;
23+
24+pub struct Manager {
25+ pub keyboard_monitor: KeyboardMonitor,
26+ pub pointer_locator: PointerLocator,
27+}
28+
29+pub enum A11yManagerToNiri {
30+ QueryPointer,
31+}
32+
33+pub enum NiriToA11yManager {
34+ PointerContents(Option<(PointerAppData, f64, f64)>),
35+}
23 36
24 #[derive(Debug, Default)]37 #[derive(Debug, Default)]
25 struct Data {38 struct Data {
@@ -38,7 +51,7 @@ struct Client {
38 keystrokes: Vec<(Keysym, u32)>,51 keystrokes: Vec<(Keysym, u32)>,
39 }52 }
40 53
41-#[derive(Clone)]54+#[derive(Clone, Default)]
42 pub struct KeyboardMonitor {55 pub struct KeyboardMonitor {
43 data: Arc<Mutex<Data>>,56 data: Arc<Mutex<Data>>,
44 iface: Arc<OnceLock<InterfaceRef<Self>>>,57 iface: Arc<OnceLock<InterfaceRef<Self>>>,
@@ -55,6 +68,39 @@ pub enum KbMonBlock {
55 Block,68 Block,
56 }69 }
57 70
71+#[derive(Debug, Default)]
72+struct PointerData {
73+ /// Clients to be notified of pointer position change.
74+ clients: HashSet<OwnedUniqueName>,
75+}
76+
77+#[derive(Debug, Default, SerializeDict, Type, Value)]
78+#[zvariant(signature = "dict")]
79+pub struct PointerAppData {
80+ // For the future: other impls send either pid or the two other properties, not both.
81+ pid: Option<i32>,
82+ app_dbus_name: Option<String>,
83+ toplevel_object_path: Option<OwnedObjectPath>,
84+}
85+
86+#[derive(Clone)]
87+pub struct PointerLocator {
88+ data: Arc<Mutex<PointerData>>,
89+ iface: Arc<OnceLock<InterfaceRef<Self>>>,
90+ to_niri: calloop::channel::Sender<A11yManagerToNiri>,
91+ from_niri: async_channel::Receiver<NiriToA11yManager>,
92+}
93+
94+#[derive(DBusError, Debug)]
95+#[zbus(prefix = "org.freedesktop.a11y")]
96+enum A11yError {
97+ #[zbus(error)]
98+ ZBus(zbus::Error),
99+ UnknownToplevel,
100+ // Catch-all.
101+ Failed(String),
102+}
103+
58 /// Interface for monitoring of keyboard input by assistive technologies.104 /// Interface for monitoring of keyboard input by assistive technologies.
59 ///105 ///
60 /// This interface is used by assistive technologies to monitor keyboard input of the compositor.106 /// This interface is used by assistive technologies to monitor keyboard input of the compositor.
@@ -204,14 +250,6 @@ impl KeyboardMonitor {
204 }250 }
205 251
206 impl KeyboardMonitor {252 impl KeyboardMonitor {
207- #[allow(clippy::new_without_default)]
208- pub fn new() -> Self {
209- Self {
210- data: Arc::new(Mutex::new(Data::default())),
211- iface: Arc::new(OnceLock::new()),
212- }
213- }
214-
215 #[allow(clippy::too_many_arguments)]253 #[allow(clippy::too_many_arguments)]
216 pub fn process_key(254 pub fn process_key(
217 &self,255 &self,
@@ -366,9 +404,109 @@ impl Client {
366 }404 }
367 }405 }
368 406
407+/// Interface for locating the pointer position in the accessibility tree.
408+///
409+/// This interface is used by assistive technologies to query the pointer position in the
410+/// accessibility tree. The compositor is expected to listen on the well-known bus name
411+/// "org.freedesktop.a11y.Manager" at the object path "/org/freedesktop/a11y/Manager".
412+#[interface(name = "org.freedesktop.a11y.PointerLocator")]
413+impl PointerLocator {
414+ // Queries the a11y details about the toplevel beneath the mouse pointer, together with the
415+ // relative position of the mouse pointer on the surface. The most up-to-date coordinates as
416+ // known to the compositor are returned by this method call.
417+ //
418+ // Screen readers may use the given information to further query the accessibility tree of the
419+ // specific application, in order to identify the application-side accessible elements directly
420+ // underneath the mouse pointer.
421+ //
422+ // This request will also schedule the emission of a following "PointerPositionChanged" signal,
423+ // whenever mouse pointer motion would happen.
424+ //
425+ // The "data" argument is a generic container, so far the only handled keys are relevant to
426+ // AT-SPI:
427+ //
428+ // - "app-dbus-name" (type: s): Application D-Bus name in the a11y D-Bus
429+ // - "toplevel-object-path" (type: o): Object path to the toplevel, in the given D-Bus name.
430+ //
431+ // If this data is not provided, the focused client will be considered to be the desktop
432+ // environment itself, and the provided coordinates will be relative to its origin point.
433+ //
434+ // A org.freedesktop.a11y.UnknownToplevel error will be returned if the client under the pointer
435+ // is non-introspectable.
436+ async fn query_pointer(
437+ &self,
438+ #[zbus(header)] hdr: Header<'_>,
439+ ) -> Result<(PointerAppData, f64, f64), A11yError> {
440+ let Some(sender) = hdr.sender() else {
441+ return Err(A11yError::Failed("no sender".to_owned()));
442+ };
443+ let sender = OwnedUniqueName::from(sender.to_owned());
444+
445+ if let Err(err) = self.to_niri.send(A11yManagerToNiri::QueryPointer) {
446+ warn!("error sending message to niri: {err:?}");
447+ return Err(A11yError::Failed("internal error".to_owned()));
448+ }
449+
450+ let rv = match self.from_niri.recv().await {
451+ Ok(NiriToA11yManager::PointerContents(Some((data, x, y)))) => Ok((data, x, y)),
452+ Ok(NiriToA11yManager::PointerContents(None)) => Err(A11yError::UnknownToplevel),
453+ Err(err) => {
454+ warn!("error receiving message from niri: {err:?}");
455+ Err(A11yError::Failed("internal error".to_owned()))
456+ }
457+ };
458+
459+ // Subscribe the client after all asynchronous operations. Otherwise it's possible for the
460+ // pointer to move and the signal to be emitted before returning from this first
461+ // QueryPointer request.
462+ let mut data = self.data.lock().unwrap();
463+ if !data.clients.contains(&sender) {
464+ trace!("enabling pointer position notifications for {sender}");
465+ data.clients.insert(sender);
466+ }
467+
468+ rv
469+ }
470+
471+ // This signal is emitted once when two conditions meet:
472+ // 1. A "QueryPointer" method call happened before
473+ // 2. Pointer motion happened after replying to that method call
474+ //
475+ // The purpose of the signal is being used as a throttling mechanism, so that a11y pointer
476+ // handling is independent of device specifics like frequency rate at which input is handled.
477+ //
478+ // The expected response for a screen reader to this signal is scheduling a following
479+ // "QueryPointer" method call.
480+ #[zbus(signal)]
481+ pub async fn pointer_position_changed(ctxt: &SignalEmitter<'_>) -> zbus::Result<()>;
482+}
483+
484+impl PointerLocator {
485+ fn notify_pointer_position_changed(&self) {
486+ let mut ctxt = self.iface.get().unwrap().signal_emitter().clone();
487+
488+ let mut data = self.data.lock().unwrap();
489+ for name in &data.clients {
490+ let _span = tracy_client::span!("emitting pointer_position_changed");
491+
492+ ctxt = ctxt.set_destination(BusName::Unique(name.as_ref()));
493+ let ctxt = &ctxt;
494+ async_io::block_on(async move {
495+ if let Err(err) = PointerLocator::pointer_position_changed(ctxt).await {
496+ warn!("error emitting pointer_position_changed: {err:?}");
497+ }
498+ });
499+ }
500+
501+ // They will need to QueryPointer again to subscribe for the next signal.
502+ data.clients.clear();
503+ }
504+}
505+
369 async fn monitor_disappeared_clients(506 async fn monitor_disappeared_clients(
370 conn: &zbus::Connection,507 conn: &zbus::Connection,
371 data: Arc<Mutex<Data>>,508 data: Arc<Mutex<Data>>,
509+ pointer_data: Arc<Mutex<PointerData>>,
372 ) -> anyhow::Result<()> {510 ) -> anyhow::Result<()> {
373 let proxy = fdo::DBusProxy::new(conn)511 let proxy = fdo::DBusProxy::new(conn)
374 .await512 .await
@@ -392,9 +530,15 @@ async fn monitor_disappeared_clients(
392 trace!("keyboard monitor client disconnected: {name}");530 trace!("keyboard monitor client disconnected: {name}");
393 531
394 let name = OwnedUniqueName::from(name.to_owned());532 let name = OwnedUniqueName::from(name.to_owned());
395- let mut data = data.lock().unwrap();533+ {
396- data.clients.remove(&name);534+ let mut data = data.lock().unwrap();
397- data.rebuild_grabbed_mods();535+ data.clients.remove(&name);
536+ data.rebuild_grabbed_mods();
537+ }
538+ {
539+ let mut data = pointer_data.lock().unwrap();
540+ data.clients.remove(&name);
541+ }
398 } else {542 } else {
399 error!("non-null new_owner should've been filtered out");543 error!("non-null new_owner should've been filtered out");
400 }544 }
@@ -403,27 +547,56 @@ async fn monitor_disappeared_clients(
403 Ok(())547 Ok(())
404 }548 }
405 549
406-impl Start for KeyboardMonitor {550+impl Manager {
407- fn start(self) -> anyhow::Result<zbus::blocking::Connection> {551+ pub fn new(
408- let data = self.data.clone();552+ to_niri: calloop::channel::Sender<A11yManagerToNiri>,
553+ from_niri: async_channel::Receiver<NiriToA11yManager>,
554+ ) -> Self {
555+ Self {
556+ keyboard_monitor: KeyboardMonitor::default(),
557+ pointer_locator: PointerLocator {
558+ data: Default::default(),
559+ iface: Default::default(),
560+ to_niri,
561+ from_niri,
562+ },
563+ }
564+ }
409 565
566+ pub fn start(&self) -> anyhow::Result<zbus::blocking::Connection> {
410 let conn = zbus::blocking::Connection::session()?;567 let conn = zbus::blocking::Connection::session()?;
411 let flags = RequestNameFlags::AllowReplacement568 let flags = RequestNameFlags::AllowReplacement
412 | RequestNameFlags::ReplaceExisting569 | RequestNameFlags::ReplaceExisting
413 | RequestNameFlags::DoNotQueue;570 | RequestNameFlags::DoNotQueue;
414 571
415- conn.object_server()572+ conn.object_server().at(
416- .at("/org/freedesktop/a11y/Manager", self.clone())?;573+ "/org/freedesktop/a11y/Manager",
574+ self.keyboard_monitor.clone(),
575+ )?;
576+ conn.object_server().at(
577+ "/org/freedesktop/a11y/Manager",
578+ self.pointer_locator.clone(),
579+ )?;
417 conn.request_name_with_flags("org.freedesktop.a11y.Manager", flags)?;580 conn.request_name_with_flags("org.freedesktop.a11y.Manager", flags)?;
418 581
419 let iface = conn582 let iface = conn
420 .object_server()583 .object_server()
421 .interface("/org/freedesktop/a11y/Manager")?;584 .interface("/org/freedesktop/a11y/Manager")?;
422- let _ = self.iface.set(iface);585+ let _ = self.keyboard_monitor.iface.set(iface);
586+
587+ let iface = conn
588+ .object_server()
589+ .interface("/org/freedesktop/a11y/Manager")?;
590+ let _ = self.pointer_locator.iface.set(iface);
591+
592+ let data = self.keyboard_monitor.data.clone();
593+ let pointer_data = self.pointer_locator.data.clone();
423 594
424 let async_conn = conn.inner().clone();595 let async_conn = conn.inner().clone();
425 let future = async move {596 let future = async move {
426- if let Err(err) = monitor_disappeared_clients(&async_conn, data.clone()).await {597+ if let Err(err) =
598+ monitor_disappeared_clients(&async_conn, data.clone(), pointer_data.clone()).await
599+ {
427 warn!("error monitoring keyboard monitor clients: {err:?}");600 warn!("error monitoring keyboard monitor clients: {err:?}");
428 601
429 // Since the monitor is now broken, prevent any further communication.602 // Since the monitor is now broken, prevent any further communication.
@@ -431,9 +604,15 @@ impl Start for KeyboardMonitor {
431 warn!("error closing connection: {err:?}");604 warn!("error closing connection: {err:?}");
432 }605 }
433 606
434- let mut data = data.lock().unwrap();607+ {
435- data.clients.clear();608+ let mut data = data.lock().unwrap();
436- data.rebuild_grabbed_mods();609+ data.clients.clear();
610+ data.rebuild_grabbed_mods();
611+ }
612+ {
613+ let mut data = pointer_data.lock().unwrap();
614+ data.clients.clear();
615+ }
437 }616 }
438 };617 };
439 let task = conn618 let task = conn
@@ -453,7 +632,7 @@ impl State {
453 keycode: Keycode,632 keycode: Keycode,
454 state: KeyState,633 state: KeyState,
455 ) -> KbMonBlock {634 ) -> KbMonBlock {
456- if self.niri.a11y_keyboard_monitor.is_none() {635+ if self.niri.a11y_manager.is_none() {
457 return KbMonBlock::Pass;636 return KbMonBlock::Pass;
458 }637 }
459 638
@@ -475,9 +654,68 @@ impl State {
475 let repeat_delay = Duration::from_millis(u64::from(config.input.keyboard.repeat_delay));654 let repeat_delay = Duration::from_millis(u64::from(config.input.keyboard.repeat_delay));
476 let released = state == KeyState::Released;655 let released = state == KeyState::Released;
477 656
478- let Some(monitor) = &self.niri.a11y_keyboard_monitor else {657+ let Some(manager) = &self.niri.a11y_manager else {
479 return KbMonBlock::Pass;658 return KbMonBlock::Pass;
480 };659 };
660+ let monitor = &manager.keyboard_monitor;
481 monitor.process_key(repeat_delay, time, keycode, released, mods, keysym, unichar)661 monitor.process_key(repeat_delay, time, keycode, released, mods, keysym, unichar)
482 }662 }
663+
664+ pub fn a11y_notify_pointer_motion(&mut self) {
665+ let Some(manager) = &self.niri.a11y_manager else {
666+ return;
667+ };
668+
669+ manager.pointer_locator.notify_pointer_position_changed();
670+ }
671+
672+ pub fn on_a11y_manager_msg(
673+ &mut self,
674+ to_a11y: &async_channel::Sender<NiriToA11yManager>,
675+ msg: A11yManagerToNiri,
676+ ) {
677+ let A11yManagerToNiri::QueryPointer = msg;
678+ let _span = tracy_client::span!("QueryPointer");
679+
680+ let pointer = &self.niri.seat.get_pointer().unwrap();
681+ let pointer_pos = pointer.current_location();
682+
683+ // Grabs can modify pointer focus, but here let's ignore them. I'm not entirely sure what's
684+ // expected by a11y users though.
685+ let contents = match &self.niri.pointer_contents {
686+ PointContents {
687+ surface: Some((surface, surface_pos)),
688+ ..
689+ } => {
690+ if let Some(credentials) = get_credentials_for_surface(surface) {
691+ // The current definition of the protocol expects buffer-relative pointer
692+ // coordinates. I don't think this is correct, I opened a discussion here:
693+ // https://gitlab.gnome.org/GNOME/mutter/-/work_items/4919
694+ let pos_within_surface = pointer_pos - *surface_pos;
695+
696+ let data = PointerAppData {
697+ pid: Some(credentials.pid),
698+ // FIXME: fill these in from xdg_dbus_annotation when it's merged.
699+ // https://gitlab.freedesktop.org/wayland/wayland-protocols/-/merge_requests/493
700+ app_dbus_name: None,
701+ toplevel_object_path: None,
702+ };
703+
704+ Some((data, pos_within_surface.x, pos_within_surface.y))
705+ } else {
706+ // Client is not introspectable.
707+ None
708+ }
709+ }
710+ _ => {
711+ // The focused client is niri itself.
712+ Some((PointerAppData::default(), pointer_pos.x, pointer_pos.y))
713+ }
714+ };
715+
716+ let msg = NiriToA11yManager::PointerContents(contents);
717+ if let Err(err) = to_a11y.send_blocking(msg) {
718+ warn!("error sending pointer contents to a11y manager: {err:?}");
719+ }
720+ }
483 }721 }
src/dbus/mod.rsmodified+18 -6
@@ -17,7 +17,6 @@ pub mod mutter_screen_cast;
17 #[cfg(feature = "xdp-gnome-screencast")]17 #[cfg(feature = "xdp-gnome-screencast")]
18 use mutter_screen_cast::ScreenCast;18 use mutter_screen_cast::ScreenCast;
19 19
20-use self::freedesktop_a11y::KeyboardMonitor;
21 use self::freedesktop_screensaver::ScreenSaver;20 use self::freedesktop_screensaver::ScreenSaver;
22 use self::gnome_shell_introspect::Introspect;21 use self::gnome_shell_introspect::Introspect;
23 use self::mutter_display_config::DisplayConfig;22 use self::mutter_display_config::DisplayConfig;
@@ -38,7 +37,7 @@ pub struct DBusServers {
38 pub conn_screen_cast: Option<Connection>,37 pub conn_screen_cast: Option<Connection>,
39 pub conn_login1: Option<Connection>,38 pub conn_login1: Option<Connection>,
40 pub conn_locale1: Option<Connection>,39 pub conn_locale1: Option<Connection>,
41- pub conn_keyboard_monitor: Option<Connection>,40+ pub conn_a11y_manager: Option<Connection>,
42 }41 }
43 42
44 impl DBusServers {43 impl DBusServers {
@@ -131,10 +130,23 @@ impl DBusServers {
131 dbus.conn_screen_cast = try_start(screen_cast);130 dbus.conn_screen_cast = try_start(screen_cast);
132 }131 }
133 132
134- let keyboard_monitor = KeyboardMonitor::new();133+ let (to_niri, from_a11y) = calloop::channel::channel();
135- if let Some(x) = try_start(keyboard_monitor.clone()) {134+ let (to_a11y, from_niri) = async_channel::unbounded();
136- dbus.conn_keyboard_monitor = Some(x);135+ niri.event_loop
137- niri.a11y_keyboard_monitor = Some(keyboard_monitor);136+ .insert_source(from_a11y, move |event, _, state| match event {
137+ calloop::channel::Event::Msg(msg) => state.on_a11y_manager_msg(&to_a11y, msg),
138+ calloop::channel::Event::Closed => (),
139+ })
140+ .unwrap();
141+ let a11y_manager = freedesktop_a11y::Manager::new(to_niri, from_niri);
142+ match a11y_manager.start() {
143+ Ok(conn) => {
144+ dbus.conn_a11y_manager = Some(conn);
145+ niri.a11y_manager = Some(a11y_manager);
146+ }
147+ Err(err) => {
148+ warn!("error starting a11y manager: {err:?}");
149+ }
138 }150 }
139 }151 }
140 152
src/input/mod.rsmodified+8 -0
@@ -2650,6 +2650,10 @@ impl State {
2650 }2650 }
2651 }2651 }
2652 2652
2653+ // Notify a11y.
2654+ #[cfg(feature = "dbus")]
2655+ self.a11y_notify_pointer_motion();
2656+
2653 // Redraw to update the cursor position.2657 // Redraw to update the cursor position.
2654 // FIXME: redraw only outputs overlapping the cursor.2658 // FIXME: redraw only outputs overlapping the cursor.
2655 self.niri.queue_redraw_all();2659 self.niri.queue_redraw_all();
@@ -2742,6 +2746,10 @@ impl State {
2742 }2746 }
2743 }2747 }
2744 2748
2749+ // Notify a11y.
2750+ #[cfg(feature = "dbus")]
2751+ self.a11y_notify_pointer_motion();
2752+
2745 // Redraw to update the cursor position.2753 // Redraw to update the cursor position.
2746 // FIXME: redraw only outputs overlapping the cursor.2754 // FIXME: redraw only outputs overlapping the cursor.
2747 self.niri.queue_redraw_all();2755 self.niri.queue_redraw_all();
src/niri.rsmodified+2 -2
@@ -403,7 +403,7 @@ pub struct Niri {
403 #[cfg(feature = "dbus")]403 #[cfg(feature = "dbus")]
404 pub dbus: Option<crate::dbus::DBusServers>,404 pub dbus: Option<crate::dbus::DBusServers>,
405 #[cfg(feature = "dbus")]405 #[cfg(feature = "dbus")]
406- pub a11y_keyboard_monitor: Option<crate::dbus::freedesktop_a11y::KeyboardMonitor>,406+ pub a11y_manager: Option<crate::dbus::freedesktop_a11y::Manager>,
407 #[cfg(feature = "dbus")]407 #[cfg(feature = "dbus")]
408 pub a11y: A11y,408 pub a11y: A11y,
409 #[cfg(feature = "dbus")]409 #[cfg(feature = "dbus")]
@@ -2625,7 +2625,7 @@ impl Niri {
2625 #[cfg(feature = "dbus")]2625 #[cfg(feature = "dbus")]
2626 dbus: None,2626 dbus: None,
2627 #[cfg(feature = "dbus")]2627 #[cfg(feature = "dbus")]
2628- a11y_keyboard_monitor: None,2628+ a11y_manager: None,
2629 #[cfg(feature = "dbus")]2629 #[cfg(feature = "dbus")]
2630 a11y,2630 a11y,
2631 #[cfg(feature = "dbus")]2631 #[cfg(feature = "dbus")]