// Copyright 2023 The AccessKit Authors. All rights reserved. // Licensed under the Apache License, Version 2.0 (found in // the LICENSE-APACHE file) or the MIT license (found in // the LICENSE-MIT file), at your option. use accesskit::{ActivationHandler, DeactivationHandler}; use accesskit_atspi_common::{Adapter as AdapterImpl, AppContext, Event}; #[cfg(not(feature = "tokio"))] use async_channel::{Receiver, Sender}; use atspi::proxy::bus::StatusProxy; #[cfg(not(feature = "tokio"))] use futures_util::{StreamExt, pin_mut as pin, select}; use std::{ sync::{Arc, Mutex, OnceLock, RwLock}, thread, }; #[cfg(feature = "tokio")] use tokio::{ pin, select, sync::mpsc::{UnboundedReceiver as Receiver, UnboundedSender as Sender}, }; #[cfg(feature = "tokio")] use tokio_stream::{StreamExt, wrappers::UnboundedReceiverStream}; use zbus::{Connection, connection::Builder, proxy::PropertyChanged}; use crate::{ adapter::{AdapterState, Callback, Message}, atspi::{Bus, map_or_ignoring_recoverable_error, zbus_error_is_unrecoverable}, util::block_on, }; static APP_CONTEXT: OnceLock>> = OnceLock::new(); static MESSAGES: OnceLock> = OnceLock::new(); fn app_name() -> Option { std::env::current_exe().ok().and_then(|path| { path.file_name() .map(|name| name.to_string_lossy().to_string()) }) } pub(crate) fn get_or_init_app_context<'a>() -> &'a Arc> { APP_CONTEXT.get_or_init(|| AppContext::new(app_name())) } pub(crate) fn get_or_init_messages() -> Sender { MESSAGES .get_or_init(|| { #[cfg(not(feature = "tokio"))] let (tx, rx) = async_channel::unbounded(); #[cfg(feature = "tokio")] let (tx, rx) = tokio::sync::mpsc::unbounded_channel(); thread::spawn(|| { // zbus 6's default runtime drives its connection tasks itself. // Only this AccessKit event loop remains on our dedicated thread. block_on(async { if let Ok(session_bus) = Builder::session().build().await { if let Err(error) = run_event_loop(session_bus, rx).await { if zbus_error_is_unrecoverable(&error) { panic!("Accessibility event loop failed: {error}"); } } } }) }); tx }) .clone() } struct AdapterEntry { id: usize, activation_handler: Box, deactivation_handler: Box, state: Arc>, } fn activate_adapter(entry: &mut AdapterEntry) { let mut state = entry.state.lock().unwrap(); if let AdapterState::Inactive { is_window_focused, root_window_bounds, action_handler, } = &*state { *state = match entry.activation_handler.request_initial_tree() { Some(initial_state) => { let r#impl = AdapterImpl::with_wrapped_action_handler( entry.id, get_or_init_app_context(), Callback::new(), initial_state, *is_window_focused, *root_window_bounds, Arc::clone(action_handler), ); AdapterState::Active(r#impl) } None => AdapterState::Pending { is_window_focused: *is_window_focused, root_window_bounds: *root_window_bounds, action_handler: Arc::clone(action_handler), }, }; } } fn deactivate_adapter(entry: &mut AdapterEntry) { let mut state = entry.state.lock().unwrap(); match &*state { AdapterState::Inactive { .. } => (), AdapterState::Pending { is_window_focused, root_window_bounds, action_handler, } => { *state = AdapterState::Inactive { is_window_focused: *is_window_focused, root_window_bounds: *root_window_bounds, action_handler: Arc::clone(action_handler), }; drop(state); entry.deactivation_handler.deactivate_accessibility(); } AdapterState::Active(r#impl) => { *state = AdapterState::Inactive { is_window_focused: r#impl.is_window_focused(), root_window_bounds: r#impl.root_window_bounds(), action_handler: r#impl.wrapped_action_handler(), }; drop(state); entry.deactivation_handler.deactivate_accessibility(); } } } async fn bus_after_status_change( change: Option>, session_bus: &Connection, ) -> zbus::Result> { let enabled = match change { Some(change) => change.get().await?, None => false, }; if enabled { map_or_ignoring_recoverable_error(Bus::new(session_bus).await, None, Some) } else { Ok(None) } } fn sync_adapters(adapters: &mut [AdapterEntry], atspi_bus: &Option) { let active = atspi_bus.is_some(); for entry in adapters { if active { activate_adapter(entry); } else { deactivate_adapter(entry); } } } async fn run_event_loop( session_bus: Connection, rx: Receiver, ) -> zbus::Result<()> { let status = StatusProxy::new(&session_bus).await?; let changes = status.receive_is_enabled_changed().await.fuse(); pin!(changes); #[cfg(not(feature = "tokio"))] let messages = rx.fuse(); #[cfg(feature = "tokio")] let messages = UnboundedReceiverStream::new(rx).fuse(); pin!(messages); let mut atspi_bus = None; let mut adapters: Vec = Vec::new(); loop { select! { change = changes.next() => { atspi_bus = bus_after_status_change(change, &session_bus).await?; sync_adapters(&mut adapters, &atspi_bus); } message = messages.next() => { if let Some(message) = message { process_adapter_message(&atspi_bus, &mut adapters, message).await?; } } } } } async fn process_adapter_message( atspi_bus: &Option, adapters: &mut Vec, message: Message, ) -> zbus::Result<()> { match message { Message::AddAdapter { id, activation_handler, deactivation_handler, state, } => { adapters.push(AdapterEntry { id, activation_handler, deactivation_handler, state, }); if atspi_bus.is_some() { let entry = adapters.last_mut().unwrap(); activate_adapter(entry); } } Message::RemoveAdapter { id } => { if let Ok(index) = adapters.binary_search_by(|entry| entry.id.cmp(&id)) { adapters.remove(index); } } Message::RegisterInterfaces { node, interfaces } => { if let Some(bus) = atspi_bus { bus.register_interfaces(node, interfaces).await? } } Message::UnregisterInterfaces { adapter_id, node_id, interfaces, } => { if let Some(bus) = atspi_bus { bus.unregister_interfaces(adapter_id, node_id, interfaces) .await? } } Message::EmitEvent { adapter_id, event: Event::Object { target, event }, } => { if let Some(bus) = atspi_bus { bus.emit_object_event(adapter_id, target, event).await? } } Message::EmitEvent { adapter_id, event: Event::Window { target, name, event, }, } => { if let Some(bus) = atspi_bus { bus.emit_window_event(adapter_id, target, name, event) .await?; } } Message::EmitEvent { event: Event::Cache(_), .. } => unreachable!("cache events are sent as EmitCacheAdd/EmitCacheRemove"), Message::EmitCacheAdd { node } => { if let Some(bus) = atspi_bus { bus.emit_cache_add(node).await?; } } Message::EmitCacheRemove { adapter_id, node_id, } => { if let Some(bus) = atspi_bus { bus.emit_cache_remove(adapter_id, node_id).await?; } } } Ok(()) }