//! The focused application and web page on Linux, for dictation modes. //! X11 reports the active window's process and title. On Wayland the //! compositor's foreign-toplevel list (wlroots compositors, niri, //! Hyprland) reports the activated window's app ID and title; desktops //! without it (GNOME, KDE) report no focused window, and modes fall back to //! the global settings. AT-SPI then finds that window's page address. use std::collections::HashMap; use std::sync::mpsc; use std::time::Duration; use wayland_client::backend::ObjectId; use wayland_client::globals::{GlobalListContents, registry_queue_init}; use wayland_client::protocol::wl_registry::{self, WlRegistry}; use wayland_client::{Connection, Dispatch, Proxy, QueueHandle, event_created_child}; use wayland_protocols_wlr::foreign_toplevel::v1::client::zwlr_foreign_toplevel_handle_v1::{ self, ZwlrForeignToplevelHandleV1, }; use wayland_protocols_wlr::foreign_toplevel::v1::client::zwlr_foreign_toplevel_manager_v1::{ self, EVT_TOPLEVEL_OPCODE, ZwlrForeignToplevelManagerV1, }; use x11rb::connection::Connection as _; use x11rb::protocol::xproto::{AtomEnum, ConnectionExt, Window}; use crate::dictation::context::ContextSnapshot; use crate::linux::atspi::WindowHint; /// The focused window as the display server reports it. #[derive(Debug, Default)] struct FocusedWindow { pid: Option, app_id: Option, title: Option, } /// The focused window's application, and its page when `read_page` says /// processing uses it (`ModeRuntime::reads_page`). AT-SPI is asked only for /// the page, or for the process of a window the display server names no /// other way; its walk runs before the paste, for up to its deadline. pub(crate) fn capture(read_page: bool) -> ContextSnapshot { begin_capture(read_page).finish() } #[derive(Default)] pub(crate) struct PendingContext { snapshot: ContextSnapshot, accessible: Option>>, read_page: bool, needs_process: bool, } impl PendingContext { pub(crate) fn finish(self) -> ContextSnapshot { let accessible = self.accessible.and_then(|receiver| { receiver .recv_timeout(Duration::from_millis(500)) .ok() .flatten() }); ContextSnapshot { application: self .needs_process .then(|| { accessible .as_ref() .and_then(|window| window.pid) .and_then(process_name) }) .flatten() .or(self.snapshot.application), browser_url: accessible .filter(|_| self.read_page) .and_then(|window| window.url), ..self.snapshot } } } /// Pin the window at stop; its bounded accessibility read overlaps inference /// and never looks up a later foreground window from the output queue. pub(crate) fn begin_capture(read_page: bool) -> PendingContext { let window = if crate::linux::session::LinuxSession::detect().is_wayland() { wayland_focused_window() } else { x11_focused_window() }; let Some(window) = window else { return PendingContext::default(); }; let desktop_name = window .app_id .as_deref() .and_then(crate::application_catalog::desktop_application_name); let needs_process = desktop_name.is_none() && window.pid.is_none(); let accessible = (read_page || needs_process) .then(|| { let hint = WindowHint { pid: window.pid, title: window.title.clone(), }; let (sender, receiver) = mpsc::channel(); std::thread::Builder::new() .name("linux-atspi-context".into()) .spawn(move || { let _ = sender.send(crate::linux::atspi::focused_window(&hint)); }) .ok() .map(|_| receiver) }) .flatten(); let application = desktop_name .or_else(|| window.pid.and_then(process_name)) .or_else(|| window.app_id.as_deref().map(str::to_lowercase)); PendingContext { snapshot: ContextSnapshot { application, window_title: window.title, ..Default::default() }, accessible, read_page, needs_process, } } fn x11_focused_window() -> Option { let (connection, screen) = x11rb::connect(None).ok()?; let root = connection.setup().roots.get(screen)?.root; let active = intern(&connection, b"_NET_ACTIVE_WINDOW")?; let pid = intern(&connection, b"_NET_WM_PID")?; let name = intern(&connection, b"_NET_WM_NAME")?; let utf8 = intern(&connection, b"UTF8_STRING")?; let window = property(&connection, root, active, AtomEnum::WINDOW.into())?; if window == 0 { return None; } Some(FocusedWindow { pid: property(&connection, window, pid, AtomEnum::CARDINAL.into()), app_id: None, title: text_property(&connection, window, name, utf8), }) } fn intern(connection: &impl x11rb::connection::Connection, name: &[u8]) -> Option { Some(connection.intern_atom(true, name).ok()?.reply().ok()?.atom) } fn property( connection: &impl x11rb::connection::Connection, window: Window, property: u32, kind: u32, ) -> Option { connection .get_property(false, window, property, kind, 0, 1) .ok()? .reply() .ok()? .value32()? .next() } fn text_property( connection: &impl x11rb::connection::Connection, window: Window, property: u32, kind: u32, ) -> Option { let reply = connection .get_property(false, window, property, kind, 0, 1024) .ok()? .reply() .ok()?; String::from_utf8(reply.value) .ok() .filter(|title| !title.is_empty()) } #[derive(Default)] struct Toplevel { app_id: Option, title: Option, activated: bool, } #[derive(Default)] struct Toplevels(HashMap); impl Dispatch for Toplevels { fn event( _: &mut Self, _: &WlRegistry, _: wl_registry::Event, _: &GlobalListContents, _: &Connection, _: &QueueHandle, ) { } } impl Dispatch for Toplevels { fn event( _: &mut Self, _: &ZwlrForeignToplevelManagerV1, _: zwlr_foreign_toplevel_manager_v1::Event, _: &(), _: &Connection, _: &QueueHandle, ) { } event_created_child!(Toplevels, ZwlrForeignToplevelManagerV1, [ EVT_TOPLEVEL_OPCODE => (ZwlrForeignToplevelHandleV1, ()), ]); } impl Dispatch for Toplevels { fn event( toplevels: &mut Self, handle: &ZwlrForeignToplevelHandleV1, event: zwlr_foreign_toplevel_handle_v1::Event, _: &(), _: &Connection, _: &QueueHandle, ) { let toplevel = toplevels.0.entry(handle.id()).or_default(); match event { zwlr_foreign_toplevel_handle_v1::Event::AppId { app_id } => { toplevel.app_id = Some(app_id); } zwlr_foreign_toplevel_handle_v1::Event::Title { title } => { toplevel.title = Some(title); } zwlr_foreign_toplevel_handle_v1::Event::State { state } => { toplevel.activated = state .as_chunks::<4>() .0 .iter() .map(|bytes| u32::from_ne_bytes(*bytes)) .any(|state| state == zwlr_foreign_toplevel_handle_v1::State::Activated as u32); } zwlr_foreign_toplevel_handle_v1::Event::Closed => { toplevels.0.remove(&handle.id()); handle.destroy(); } _ => {} } } } /// The activated toplevel from `zwlr_foreign_toplevel_manager_v1`, or /// `None` when the compositor doesn't offer it. fn wayland_focused_window() -> Option { let connection = Connection::connect_to_env().ok()?; let (globals, mut queue) = registry_queue_init::(&connection).ok()?; let manager: ZwlrForeignToplevelManagerV1 = globals.bind(&queue.handle(), 1..=3, ()).ok()?; let mut toplevels = Toplevels::default(); // The first roundtrip announces each toplevel, the second delivers // their app IDs, titles, and states. queue.roundtrip(&mut toplevels).ok()?; queue.roundtrip(&mut toplevels).ok()?; manager.stop(); let focused = toplevels .0 .into_values() .find(|toplevel| toplevel.activated)?; Some(FocusedWindow { pid: None, app_id: focused.app_id.filter(|app_id| !app_id.is_empty()), title: focused.title.filter(|title| !title.is_empty()), }) } /// The process's executable name, lowercased, as the application catalog /// names applications ("code", "firefox"). fn process_name(pid: u32) -> Option { let executable = std::fs::read_link(format!("/proc/{pid}/exe")).ok(); executable .as_deref() .and_then(|path| path.file_name()) .and_then(|name| name.to_str()) .map(str::to_owned) .or_else(|| std::fs::read_to_string(format!("/proc/{pid}/comm")).ok()) .map(|name| name.trim().to_lowercase()) .filter(|name| !name.is_empty()) } #[cfg(test)] mod tests { use super::*; #[test] fn queued_context_keeps_the_stopped_window_and_its_page() { let (sender, receiver) = mpsc::channel(); let pending = PendingContext { snapshot: ContextSnapshot { application: Some("editor".into()), window_title: Some("Draft".into()), ..Default::default() }, accessible: Some(receiver), read_page: true, needs_process: false, }; sender .send(Some(crate::linux::atspi::AccessibleWindow { pid: None, url: Some(url::Url::parse("https://example.com/draft").unwrap()), })) .unwrap(); let context = pending.finish(); assert_eq!(context.application.as_deref(), Some("editor")); assert_eq!(context.window_title.as_deref(), Some("Draft")); assert_eq!(context.browser_host(), Some("example.com")); } #[test] fn a_failed_page_read_keeps_the_stopped_application() { let (sender, receiver) = mpsc::channel(); drop(sender); let pending = PendingContext { snapshot: ContextSnapshot { application: Some("editor".into()), ..Default::default() }, accessible: Some(receiver), read_page: true, needs_process: false, }; let context = pending.finish(); assert_eq!(context.application.as_deref(), Some("editor")); assert_eq!(context.browser_host(), None); } #[test] fn the_current_process_has_a_name() { assert!(super::process_name(std::process::id()).is_some()); } /// Manual probe: `cargo test linux_context_reads_the_focused_window -- /// --ignored --nocapture` prints what modes would see right now. #[test] #[ignore] fn linux_context_reads_the_focused_window() { let started = std::time::Instant::now(); println!("{:?} in {:?}", super::capture(true), started.elapsed()); } }