//! The desktop's portals (`org.freedesktop.portal.*`) over a private bus //! connection. `PortalBus` registers Hex with the portal and answers //! requests; `Portal` is the GlobalShortcuts client and //! `linux_remote_desktop` the RemoteDesktop one. //! //! `Portal` binds Hex's shortcuts in one session and plain Escape in a //! second session only while something records, and reports their presses //! and releases. Dropping a bus closes its sessions, and the desktop //! releases the keys. Each bus runs on its own one-thread tokio runtime, so //! the portal's signals queue while the caller sleeps. use std::collections::{HashMap, VecDeque}; use std::io::Write; use std::path::{Path, PathBuf}; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::mpsc::{Receiver, RecvTimeoutError, channel}; use std::time::{Duration, Instant}; use color_eyre::eyre::{Result, WrapErr, eyre}; use futures_util::StreamExt; use zbus::{ Connection, DBusError, MatchRule, MessageStream, OwnedObjectPath, OwnedVariant, Variant, message, }; const DESKTOP: &str = "org.freedesktop.portal.Desktop"; const DESKTOP_PATH: &str = "/org/freedesktop/portal/desktop"; const SHORTCUTS: &str = "org.freedesktop.portal.GlobalShortcuts"; const REQUEST: &str = "org.freedesktop.portal.Request"; const SESSION: &str = "org.freedesktop.portal.Session"; /// The portal names host apps by their desktop entry, `hex.desktop`. const APP_ID: &str = "hex"; const CALL_TIMEOUT: Duration = Duration::from_secs(5); /// A request can wait for the user to approve a desktop dialog. const RESPONSE_TIMEOUT: Duration = Duration::from_secs(60); /// How often a pending request checks for cancellation. const STOP_POLL: Duration = Duration::from_millis(50); const ESCAPE_ID: &str = "cancel"; const HELD_ESCAPE_ID: &str = "cancel-held"; /// Whether a pressed id is one of the Escape session's. pub(crate) fn is_escape(id: &str) -> bool { id == ESCAPE_ID || id == HELD_ESCAPE_ID } /// One shortcut Hex asks the desktop for. pub(crate) struct Shortcut { pub(crate) id: &'static str, pub(crate) description: &'static str, /// XDG shortcuts format, such as `CTRL+ALT+space`. pub(crate) trigger: String, } #[derive(Debug, PartialEq)] pub(crate) enum Event { Pressed(String), Released(String), /// The user changed Hex's shortcuts in the desktop: the trigger each /// one now has, by id, or `None` when it has none. Changed(HashMap>), /// The desktop closed the session. Closed, } pub(crate) type Results = HashMap; /// A portal signal, by the session or request path it names. enum Signal { Session(String, Event), Response(String, u32, Results), } fn parse(message: &zbus::Message) -> Option { let header = message.header(); let path = header.path()?.to_string(); let body = message.body(); match (header.interface()?.as_str(), header.member()?.as_str()) { (SHORTCUTS, member @ ("Activated" | "Deactivated")) => { let (session, id, _, _): (OwnedObjectPath, String, u64, Results) = body.deserialize().ok()?; let event = if member == "Activated" { Event::Pressed(id) } else { Event::Released(id) }; Some(Signal::Session(session.to_string(), event)) } (SHORTCUTS, "ShortcutsChanged") => { let (session, shortcuts): (OwnedObjectPath, Vec<(String, Results)>) = body.deserialize().ok()?; Some(Signal::Session( session.to_string(), Event::Changed(triggers(shortcuts)), )) } (SESSION, "Closed") => Some(Signal::Session(path, Event::Closed)), (REQUEST, "Response") => { let (code, results): (u32, Results) = body.deserialize().ok()?; Some(Signal::Response(path, code, results)) } _ => None, } } /// Each shortcut's trigger description. An empty one (KDE's for a shortcut /// it keeps unbound) means no trigger. fn triggers(shortcuts: Vec<(String, Results)>) -> HashMap> { shortcuts .into_iter() .map(|(id, options)| { let trigger = options .get("trigger_description") .and_then(|trigger| <&str>::try_from(trigger).ok()) .map(str::trim) .filter(|trigger| !trigger.is_empty()) .map(str::to_owned); (id, trigger) }) .collect() } /// An unknown method or interface: an older portal without that call. fn unknown(error: &zbus::Error) -> bool { let name = match error { zbus::Error::MethodError(name, _, _) => name.to_string(), zbus::Error::FDO(error) => error.name().to_string(), _ => return false, }; name.starts_with("org.freedesktop.DBus.Error.Unknown") } /// A private session-bus connection registered with the portal as Hex. pub(crate) struct PortalBus { runtime: tokio::runtime::Runtime, connection: Connection, signals: Receiver, /// Session signals that arrived while a request waited. pending: VecDeque<(String, Event)>, /// This connection's unique name as request paths spell it. sender: String, next_token: u32, /// The portal interface this bus serves and its version. interface: &'static str, pub(crate) version: u32, } impl PortalBus { /// `None` when the session has no portal offering `interface`. pub(crate) fn open(interface: &'static str, stop: &AtomicBool) -> Result> { let runtime = tokio::runtime::Builder::new_multi_thread() .worker_threads(1) .thread_name("hex-portal") .enable_all() .build() .wrap_err("could not start the portal runtime")?; let (signals, received) = channel(); let connected = runtime.block_on(async { let connection = tokio::time::timeout(CALL_TIMEOUT, Connection::session()) .await .ok()? .ok()?; // Subscribed before any request so a fast answer cannot be missed. let rule = MatchRule::builder() .msg_type(message::Type::Signal) .path_namespace(DESKTOP_PATH) .build() .ok()?; let mut stream = MessageStream::for_match_rule(rule, &connection, None) .await .ok()?; tokio::spawn(async move { while let Some(Ok(message)) = stream.next().await { if let Some(signal) = parse(&message) && signals.send(signal).is_err() { break; } } }); Some(connection) }); let Some(connection) = connected else { return Ok(None); }; let Some(sender) = connection .unique_name() .map(|name| name.trim_start_matches(':').replace('.', "_")) else { return Ok(None); }; let mut bus = Self { runtime, connection, signals: received, pending: VecDeque::new(), sender, next_token: 0, interface, version: 0, }; match bus.call::<_, OwnedVariant>( DESKTOP_PATH, "org.freedesktop.DBus.Properties", "Get", &(interface, "version"), ) { Ok(version) => bus.version = u32::try_from(&version).unwrap_or(1), Err(error) => { tracing::info!(%error, interface, "the desktop has no such portal"); return Ok(None); } } let written = ensure_desktop_entry()?; // Host apps have no sandbox identity; older portals without the // registry derive one from the systemd unit instead. if let Err(error) = bus.call::<_, ()>( DESKTOP_PATH, "org.freedesktop.host.portal.Registry", "Register", &(APP_ID, HashMap::<&str, Variant<'_>>::new()), ) && !unknown(&error) { // The portal finds the app id through hex.desktop and the caller's // /proc entry, which setgid wrappers such as newgrp make unreadable. let fix = match written { Some(entry) => format!( "Hex wrote {}; restart xdg-desktop-portal (systemctl --user restart xdg-desktop-portal) so it reads it", entry.display() ), None => "run Hex without setuid or setgid wrappers such as newgrp".into(), }; return Err(eyre!( "the desktop portal could not identify Hex ({error}); {fix}" )); } if stop.load(Ordering::Acquire) { return Err(eyre!("portal registration canceled")); } Ok(Some(bus)) } pub(crate) fn call( &self, path: &str, interface: &str, method: &str, body: &B, ) -> zbus::Result where B: serde::Serialize + zbus::Type, R: for<'d> serde::Deserialize<'d> + zbus::Type, { self.runtime.block_on(async { let reply = tokio::time::timeout( CALL_TIMEOUT, self.connection .call_method(Some(DESKTOP), path, Some(interface), method, body), ) .await .map_err(|_| zbus::Error::Failure(format!("{method} timed out")))??; reply.body().deserialize() }) } /// Calls a method of this bus's interface on the portal object. pub(crate) fn call_portal(&self, method: &str, body: &B) -> Result<()> where B: serde::Serialize + zbus::Type, { self.call::<_, ()>(DESKTOP_PATH, self.interface, method, body) .wrap_err_with(|| format!("the {} portal refused {method}", self.interface)) } pub(crate) fn token(&mut self) -> String { self.next_token += 1; format!("hex{}_{}", std::process::id(), self.next_token) } /// Calls a portal method that answers through a Request's Response signal. pub(crate) fn request( &mut self, method: &str, body: impl FnOnce(&str) -> B, stop: &AtomicBool, ) -> Result where B: serde::Serialize + zbus::Type, { let token = self.token(); let path = format!("{DESKTOP_PATH}/request/{}/{token}", self.sender); let interface = self.interface; self.call::<_, OwnedObjectPath>(DESKTOP_PATH, interface, method, &body(&token)) .wrap_err_with(|| format!("the {interface} portal refused {method}"))?; let deadline = Instant::now() + RESPONSE_TIMEOUT; loop { match self.signals.recv_timeout(STOP_POLL) { Ok(Signal::Response(answered, code, results)) if answered == path => { return match code { 0 => Ok(results), 1 => Err(eyre!("the {interface} request {method} was dismissed")), _ => Err(eyre!("the {interface} portal could not complete {method}")), }; } Ok(Signal::Session(session, event)) => self.pending.push_back((session, event)), Ok(Signal::Response(..)) | Err(RecvTimeoutError::Timeout) => {} Err(RecvTimeoutError::Disconnected) => { return Err(eyre!("the portal connection closed")); } } if stop.load(Ordering::Acquire) { return Err(eyre!("the {interface} request {method} was canceled")); } if Instant::now() >= deadline { return Err(eyre!("the {interface} portal did not answer {method}")); } } } pub(crate) fn create_session(&mut self, stop: &AtomicBool) -> Result { let session_token = self.token(); let results = self.request( "CreateSession", |token| { (HashMap::from([ ("handle_token", Variant::from(token.to_owned())), ("session_handle_token", Variant::from(session_token.clone())), ]),) }, stop, )?; // Portals send the handle as a string, as the spec says, or as an object path. results .get("session_handle") .and_then(|handle| match <&str>::try_from(handle) { Ok(handle) => OwnedObjectPath::try_from(handle.to_owned()).ok(), Err(_) => handle .try_clone() .ok() .and_then(|handle| OwnedObjectPath::try_from(handle).ok()), }) .ok_or_else(|| eyre!("the {} portal returned no session", self.interface)) } pub(crate) fn close_session(&self, session: &str) { if let Err(error) = self.call::<_, ()>(session, SESSION, "Close", &()) { tracing::warn!(%error, "could not close a portal session"); } } /// The session signals that arrived since the last call. pub(crate) fn events(&mut self) -> Vec<(String, Event)> { let mut queued = std::mem::take(&mut self.pending); queued.extend(self.signals.try_iter().filter_map(|signal| match signal { Signal::Session(session, event) => Some((session, event)), Signal::Response(..) => None, })); queued.into() } } impl Drop for PortalBus { fn drop(&mut self) { // The portal closes every session of a vanished connection. let connection = self.connection.clone(); let _ = self .runtime .block_on(async { tokio::time::timeout(CALL_TIMEOUT, connection.close()).await }); } } /// The portal identifies a host app by its desktop entry. A loose binary /// has none, so Hex writes a minimal `hex.desktop` that runs it, marked /// managed so an install may replace it. Returns the entry it wrote. fn ensure_desktop_entry() -> Result> { let Some(home) = dirs::data_dir() else { return Ok(None); }; let system = std::env::var("XDG_DATA_DIRS") .ok() .filter(|dirs| !dirs.is_empty()) .unwrap_or_else(|| "/usr/local/share:/usr/share".into()); let installed = std::iter::once(home.clone()) .chain( system .split(':') .filter(|dir| !dir.is_empty()) .map(PathBuf::from), ) .any(|dir| dir.join("applications/hex.desktop").exists()); if installed { return Ok(None); } let executable = std::env::current_exe().wrap_err("could not find Hex's executable")?; let applications = home.join("applications"); let entry = applications.join("hex.desktop"); write_desktop_entry(&applications, &entry, &executable) .wrap_err_with(|| format!("could not write {}", entry.display()))?; tracing::info!(entry = %entry.display(), "wrote a desktop entry for the portal"); Ok(Some(entry)) } fn write_desktop_entry(applications: &Path, entry: &Path, executable: &Path) -> Result<()> { let executable = executable .to_str() .ok_or_else(|| eyre!("Hex's executable path is not UTF-8"))?; std::fs::create_dir_all(applications)?; let staged = applications.join(format!(".hex.desktop.{}", std::process::id())); let mut file = std::fs::File::create(&staged)?; file.write_all(desktop_entry(executable).as_bytes())?; file.sync_all()?; std::fs::rename(&staged, entry)?; Ok(()) } fn desktop_entry(executable: &str) -> String { format!( "[Desktop Entry]\nType=Application\nName=Hex\nComment=Local voice dictation\nExec={}\nIcon=audio-input-microphone\nTerminal=false\nCategories=Utility;Accessibility;\nX-Hex-Managed=true\n", exec_argument(executable) ) } /// One quoted Exec argument: quoting escapes, then string escapes, as the /// Desktop Entry spec reads them, with `%` doubled for field codes. fn exec_argument(argument: &str) -> String { let mut quoted = String::from("\""); for character in argument.chars() { match character { '"' | '`' | '$' => { quoted.push_str("\\\\"); quoted.push(character); } '\\' => quoted.push_str("\\\\\\\\"), '%' => quoted.push_str("%%"), _ => quoted.push(character), } } quoted.push('"'); quoted } /// The GlobalShortcuts client. pub(crate) struct Portal { bus: PortalBus, session: Option, escape: Option, } impl Portal { /// `None` when the session has no shortcuts portal. pub(crate) fn connect(stop: &AtomicBool) -> Result> { Ok(PortalBus::open(SHORTCUTS, stop)?.map(|bus| Self { bus, session: None, escape: None, })) } /// Whether the desktop can show its dialog for Hex's shortcuts /// (`ConfigureShortcuts`, version 2). pub(crate) fn configurable(&self) -> bool { self.bus.version >= 2 } /// Opens the desktop's dialog for Hex's shortcuts; changes arrive as /// `Event::Changed`. pub(crate) fn configure(&self) -> Result<()> { let session = self .session .as_ref() .ok_or_else(|| eyre!("no shortcuts are bound"))?; self.bus.call_portal( "ConfigureShortcuts", &(session, "", HashMap::<&str, Variant<'_>>::new()), ) } /// Binds Hex's shortcuts and returns the trigger the desktop describes /// for each one it bound, by id. pub(crate) fn bind( &mut self, shortcuts: &[Shortcut], stop: &AtomicBool, ) -> Result>> { let session = self.bus.create_session(stop)?; self.session = Some(session.clone()); self.bind_session(session, shortcuts, stop) } /// Binds Escape in its own session, and `held`, Escape with the /// recording shortcut's modifiers, for a press while that shortcut is /// still down. False when the desktop refused plain Escape. pub(crate) fn bind_escape(&mut self, held: Option, stop: &AtomicBool) -> Result { let session = self.bus.create_session(stop)?; self.escape = Some(session.clone()); let mut shortcuts = vec![Shortcut { id: ESCAPE_ID, description: "Cancel dictation", trigger: "Escape".into(), }]; shortcuts.extend(held.map(|trigger| Shortcut { id: HELD_ESCAPE_ID, description: "Cancel dictation while holding its shortcut", trigger, })); let granted = self.bind_session(session, &shortcuts, stop)?; Ok(granted.get(ESCAPE_ID).is_some_and(Option::is_some)) } pub(crate) fn release_escape(&mut self) { if let Some(session) = self.escape.take() { self.bus.close_session(&session); } } /// The presses, releases, and changes that arrived since the last call. pub(crate) fn events(&mut self) -> Vec { let escape = self.escape.as_ref().map(|session| session.as_str()); let shortcuts = self.session.as_ref().map(|session| session.as_str()); self.bus .events() .into_iter() .filter_map(|(session, event)| { if escape == Some(session.as_str()) { return matches!(event, Event::Pressed(_) | Event::Released(_)) .then_some(event); } (shortcuts == Some(session.as_str())).then_some(event) }) .collect() } fn bind_session( &mut self, session: OwnedObjectPath, shortcuts: &[Shortcut], stop: &AtomicBool, ) -> Result>> { let requested = shortcuts .iter() .map(|shortcut| { ( shortcut.id, HashMap::from([ ("description", Variant::from(shortcut.description)), ( "preferred_trigger", Variant::from(shortcut.trigger.as_str()), ), ]), ) }) .collect::>(); let handle = session.into_inner(); let results = self.bus.request( "BindShortcuts", |token| { ( handle, requested, "", HashMap::from([("handle_token", Variant::from(token.to_owned()))]), ) }, stop, )?; let bound = results .get("shortcuts") .and_then(|shortcuts| shortcuts.try_clone().ok()) .and_then(|shortcuts| Vec::<(String, Results)>::try_from(shortcuts).ok()) .unwrap_or_default(); Ok(triggers(bound)) } } /// A desktop's trigger description as Settings shows it. GNOME describes a /// GTK accelerator (`Press F9`); other desktops send readable text. pub(crate) fn readable_trigger(description: &str) -> String { let description = description.trim(); let accelerator = description.strip_prefix("Press ").unwrap_or(description); if accelerator.is_empty() || accelerator.contains([' ', '+']) { return description.to_string(); } let mut parts = Vec::new(); let mut rest = accelerator; while let Some(after) = rest.strip_prefix('<') { let Some((name, after)) = after.split_once('>') else { return description.to_string(); }; parts.push(match name.to_ascii_lowercase().as_str() { "ctrl" | "control" | "primary" => "Ctrl".to_string(), "alt" | "mod1" => "Alt".to_string(), "shift" => "Shift".to_string(), "super" | "logo" | "meta" => "Super".to_string(), other => other.to_string(), }); rest = after; } parts.push(match rest { "space" => "Space".to_string(), key if key.chars().count() == 1 => key.to_uppercase(), key => key.to_string(), }); parts.join("+") } #[cfg(test)] mod tests { use super::*; #[test] fn a_loose_binary_gets_a_quoted_exec_line() { assert_eq!( exec_argument("/home/a/Downloads/hex"), "\"/home/a/Downloads/hex\"" ); assert_eq!( exec_argument("/tmp/my $dir/100%/a\"b\\c"), r#""/tmp/my \\$dir/100%%/a\\"b\\\\c""# ); assert!(desktop_entry("/opt/hex").contains("\nExec=\"/opt/hex\"\n")); } #[test] fn an_empty_trigger_description_is_no_trigger() { let described = |trigger: &str| { HashMap::from([( "trigger_description".to_string(), OwnedVariant::try_from(Variant::from(trigger)).unwrap(), )]) }; let triggers = triggers(vec![ ("dictation".into(), described("")), ("paste-last".into(), described("Ctrl+Alt+V")), ("voice-action".into(), Results::new()), ]); assert_eq!(triggers["dictation"], None); assert_eq!(triggers["paste-last"].as_deref(), Some("Ctrl+Alt+V")); assert_eq!(triggers["voice-action"], None); } #[test] fn gtk_accelerators_read_like_the_shortcut_rows() { assert_eq!(readable_trigger("Press F9"), "Ctrl+Alt+F9"); assert_eq!(readable_trigger("Press space"), "Alt+Space"); assert_eq!(readable_trigger("a"), "Shift+Super+A"); assert_eq!(readable_trigger("Press F13"), "F13"); assert_eq!(readable_trigger("Press minus"), "Ctrl+minus"); assert_eq!(readable_trigger("Ctrl+Alt+F9"), "Ctrl+Alt+F9"); assert_eq!(readable_trigger("Press