//! The Windows listener host, which outlives the window while Hex hides in //! the tray, and the single-instance claim for the login session. The tray //! and the window are `windows_rune`. use std::path::PathBuf; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::mpsc::{self, Receiver, TryRecvError}; use std::sync::{Arc, Mutex, RwLock}; use std::thread::JoinHandle; use std::time::{Duration, Instant}; use color_eyre::Result; use color_eyre::eyre::eyre; use windows_sys::Win32::Foundation::{ CloseHandle, ERROR_ALREADY_EXISTS, GetLastError, HANDLE, WAIT_OBJECT_0, }; use windows_sys::Win32::System::Threading::{ CreateEventW, INFINITE, SetEvent, WaitForSingleObject, }; use windows_sys::Win32::UI::WindowsAndMessaging::{ASFW_ANY, AllowSetForegroundWindow}; use crate::app_settings::AppSettings; use crate::desktop::activity::{DesktopActivity, last_completed_at}; use crate::desktop::events::EventReader; use crate::desktop::host::{ DesktopAction, DesktopListenerSnapshot, DesktopShortcuts, DesktopTranscriptionSnapshot, ListenerStatus, }; use crate::desktop::i18n::tr_fill; use crate::history::History; use crate::transcription::models::TranscriptionModelId; use crate::transcription::preparation::TranscriptionPreparation; use crate::windows::rune::{Published, Request}; type ListenerResult = std::result::Result<(), String>; struct RunningListener { stop: Arc, result: Receiver, worker: JoinHandle<()>, /// Saved settings the listener applies in place. updates: mpsc::Sender>, swap_failures: Receiver, /// The tray's and `hex` commands, handled as the shortcut would be. commands: mpsc::Sender, } impl RunningListener { fn stop_and_join(self) { self.stop.store(true, Ordering::Relaxed); let _ = self.worker.join(); } } #[derive(Clone, Copy, Debug, Eq, PartialEq)] enum AutomaticSwitch { Keyboard, App, } /// The Windows runtime: owns the listener, model preparation, OpenCode /// catalog, personal transformations, and self-updater. pub(crate) struct WindowsDesktopHost { event_path: PathBuf, event_reader: EventReader, activity: DesktopActivity, /// When the current session's newest dictation completed. last_completed_at: Option, /// When this host started, so a previous session's failure is ignored. started_at: u64, listener: Option, session_before_start: Option, awaiting_session_start: bool, listen_when_ready: bool, /// Enable on launch waits for first-run setup. listen_after_setup: bool, status: ListenerStatus, error: Option, settings: AppSettings, history: Option, last_dictation: Arc>>, indicator: crate::rune_hud::HudSender, mode_runtime: Arc>, transformations: Arc, personal_commands: Option, personal_commands_checked_at: Instant, preparation: TranscriptionPreparation, /// The preparation Enable started to check the selected model. Only this /// one is canceled by Disable; a model download is left running. checking: bool, keyboard_switcher: crate::transcription::keyboard_model_switching::Switcher, /// Labs › Switch model with app: the foreground app's mode choice. app_switcher: crate::transcription::app_model_switching::Switcher, /// Present only while keyboard model switching is on. input_sources: Option, /// Which automatic switcher owns the in-flight preparation, if any. automatic_preparation: Option, updater: crate::updater::Updater, /// The newest foreground window of another app, where the tray's Paste /// last dictation goes once its menu has taken the foreground. foreground: ForeignWindow, /// The local API's `/dictations` controls, for whichever listener runs. dictations: crate::dictation::service::DictationQueue, } /// Names the event that marks the running Hex app in this login session. const APP_INSTANCE_EVENT: &str = r"Local\anomaly.hex.app"; /// The data root `HEX_APPLICATION_SUPPORT_DIR` names, unless it is the /// default one, which keeps the default event. fn custom_root() -> Option { let canonical = |path: std::path::PathBuf| path.canonicalize().unwrap_or(path); let root = canonical(std::env::var_os("HEX_APPLICATION_SUPPORT_DIR")?.into()); let default = dirs::data_dir().map(|data| canonical(data.join("hex"))); (default.as_ref() != Some(&root)).then_some(root) } /// The instance event for a data root: [`APP_INSTANCE_EVENT`] for the /// default one, and its own event for another root that /// `HEX_APPLICATION_SUPPORT_DIR` names, so a copy of Hex with its own data, /// such as a benchmark, neither signals nor yields to the person's Hex. fn instance_event_name(custom_root: Option<&std::path::Path>) -> String { use sha2::{Digest, Sha256}; match custom_root { None => APP_INSTANCE_EVENT.into(), Some(root) => { // Windows paths compare without case. let path = root.to_string_lossy().to_lowercase(); let digest = Sha256::digest(path.as_bytes()); let hash: String = digest[..8] .iter() .map(|byte| format!("{byte:02x}")) .collect(); format!("{APP_INSTANCE_EVENT}.{hash}") } } } /// Ownership of the one Hex app in this login session (per data root). /// /// Windows does not bring a running app forward when it is launched again, so /// a Start menu launch while the login item runs in the tray would start a /// second app whose listener cannot take `windows-listener.lock`. The first /// app process creates a named auto-reset event and keeps it for its /// lifetime; a later launch signals it to show the window and exits. pub(crate) struct AppInstance(HANDLE); impl AppInstance { /// Returns `None` when a Hex app already runs in this session, after /// asking it to show its window unless this launch is a hidden (login) /// launch, which must never pop up an app that is already running. pub(crate) fn claim(start_hidden: bool) -> Result> { let name = instance_event_name(custom_root().as_deref()); let name: Vec = name.encode_utf16().chain([0]).collect(); // SAFETY: `name` is a NUL-terminated UTF-16 string that outlives the call. let event = unsafe { CreateEventW(std::ptr::null(), 0, 0, name.as_ptr()) }; if event.is_null() { return Err(eyre!( "could not create the Hex instance event: {}", std::io::Error::last_os_error() )); } // SAFETY: read immediately after the call whose result it describes. if unsafe { GetLastError() } != ERROR_ALREADY_EXISTS { return Ok(Some(Self(event))); } if !start_hidden { tracing::info!("Hex is already running; showing its window"); // SAFETY: `event` is a valid handle owned by this function. A // launched app may take the foreground; pass that right on so // the running app can activate its window. unsafe { AllowSetForegroundWindow(ASFW_ANY); SetEvent(event); } } else { // A hidden launch (`hex start`, `hex restart`, a login launch) // only yields, so nothing else records that it came and went: // say why here, or a lost relaunch leaves no trace at all. tracing::info!("Hex is already running; this hidden launch exits"); } // SAFETY: `event` is a valid handle owned by this function. unsafe { CloseHandle(event) }; Ok(None) } /// Calls `on_launch` whenever a later launch signals the event, until it /// returns false. The handle stays open until the process exits, keeping /// this process the session's Hex app. pub(crate) fn forward_launches(self, on_launch: impl Fn() -> bool + Send + 'static) { let event = self.0 as usize; let spawned = std::thread::Builder::new() .name("hex-relaunch".into()) .spawn(move || { // SAFETY: the handle is never closed while the process runs. while unsafe { WaitForSingleObject(event as HANDLE, INFINITE) } == WAIT_OBJECT_0 { if !on_launch() { return; } } }); if let Err(error) = spawned { tracing::warn!(%error, "relaunching Hex will not show the running window"); } } } impl WindowsDesktopHost { pub(crate) fn new( event_path: PathBuf, settings: AppSettings, settings_error: Option, history: Option, indicator: crate::rune_hud::HudSender, dictations: crate::dictation::service::DictationQueue, processing: crate::dictation::modes::ProcessingState, ) -> Self { crate::desktop::i18n::apply(settings.ui_language.as_deref()); // The Sounds sheet also plays with the listener stopped and volume Off. // Preloading decodes the sounds without opening a device. crate::feedback::set_enabled(settings.sound_effects); crate::feedback::set_volume(settings.sound_effect_volume); crate::feedback::set_sound_set(settings.sound_theme); if let Err(error) = crate::feedback::preload() { tracing::warn!(%error, "Windows recording feedback is unavailable"); } let mut event_reader = EventReader::open(&event_path); let mut activity = DesktopActivity::default(); activity.refresh(&mut event_reader); let last_completed_at = last_completed_at(event_reader.events().iter()); let started_at = crate::desktop::events::now_ms(); // Known before the first poll, so launching keeps the saved model // instead of applying the current layout's mapping. let keyboard_switcher = crate::transcription::keyboard_model_switching::Switcher::new( settings.keyboard_model_switching.clone(), settings .keyboard_model_switching .enabled .then(crate::keyboard_input_sources::current) .flatten(), ); // Known before the first poll, so launching keeps the saved model // instead of applying the frontmost app's mode choice. let app_switcher = crate::transcription::app_model_switching::Switcher::new( settings.labs.app_model_switching, settings .labs .app_model_switching .then(|| crate::transcription::app_model_switching::startup_choice(&settings)) .flatten(), ); let mut host = Self { event_path, event_reader, activity, last_completed_at, started_at, listener: None, session_before_start: None, awaiting_session_start: false, listen_when_ready: false, listen_after_setup: false, status: ListenerStatus::Disabled, error: settings_error, settings, history, last_dictation: Arc::new(Mutex::new(None)), indicator, mode_runtime: processing.modes, transformations: processing.transformations, personal_commands: None, personal_commands_checked_at: Instant::now(), preparation: TranscriptionPreparation::default(), checking: false, keyboard_switcher, app_switcher, input_sources: None, automatic_preparation: None, updater: crate::updater::Updater::start(), foreground: ForeignWindow::default(), dictations, }; crate::transcription::models::delete_set_aside(); host.refresh_personal_commands_host(); // The microphone stays cold until first-run setup is ready. if host.settings.enable_on_launch { if host.setup_done() { host.start(); } else { host.listen_after_setup = true; } } host } /// The host's retained-history store; one store per process. pub(crate) fn history(&self) -> Option { self.history.clone() } /// Setup is ready, so the tour's practice can dictate, or the tour /// finished without a model. fn setup_done(&self) -> bool { crate::desktop::onboarding::status(&self.settings.transcription).ready() || crate::desktop::onboarding::completion_recorded() } /// Handles one window or tray request; returns `true` when Hex should quit. pub(crate) fn handle(&mut self, request: Request) -> bool { match request { Request::Desktop(action) => return self.dispatch(action), Request::ToggleListening => { if self.is_running() { self.stop(); } else { self.start(); } self.remember_listening(self.listen_when_ready); } Request::ChooseModel { model, language } => self.choose_model(model, language), Request::CancelModel => { self.manual_choice(); self.checking = false; self.preparation.cancel(); if !self.is_running() && self.status == ListenerStatus::Enabling { self.status = ListenerStatus::ModelRequired; } } Request::PasteLast => { if let Some(listener) = &self.listener { self.foreground.activate(); if listener .commands .send(crate::windows::dictation::ListenerCommand::PasteLast) .is_ok() { crate::windows::dictation::wake(); } } } Request::Quit => return true, Request::ReloadSettings => self.reload_settings(), Request::CaptureShortcutFor(_) | Request::CancelShortcut | Request::SetShortcut { .. } => {} } false } fn reload_settings(&mut self) { match self.settings.refreshed() { Ok(Some(settings)) => self.apply_settings(&settings), Ok(None) => {} Err(error) => tracing::warn!(%error, "could not reload settings"), } } fn dispatch(&mut self, action: DesktopAction) -> bool { match action { DesktopAction::StartListening => { self.start(); self.remember_listening(true); } DesktopAction::StopListening => { self.stop(); self.remember_listening(false); } DesktopAction::ClearError => self.error = None, DesktopAction::CheckForUpdates | DesktopAction::OpenUpdate | DesktopAction::RestartIntoUpdate | DesktopAction::ConfirmUpdateSwitch | DesktopAction::WindowFocused => { if let Err(error) = self.updater.act(&action) { self.error = Some(tr_fill("Could not restart Hex: {}", &format!("{error:#}"))); } } // Windows saves shortcuts from the window. DesktopAction::SetDictationShortcut(_) | DesktopAction::ConfigureShortcuts => {} } false } fn choose_model(&mut self, model: TranscriptionModelId, language: String) { self.manual_choice(); self.checking = false; let selection = crate::transcription::models::TranscriptionSelection { model, language }; self.preparation.start(selection, false); } /// A layout change not yet polled counts as before this choice, so it /// cannot later undo it. fn manual_choice(&mut self) { if let Some(source) = self .input_sources .as_mut() .and_then(|watcher| watcher.poll()) { self.keyboard_switcher .source_changed(source, Instant::now()); } self.keyboard_switcher.manual_choice(); self.app_switcher.manual_choice(); self.supersede_automatic(); } /// A layout being cycled through cancels only automatic work; a manual /// operation is superseded once the debounced choice is due. fn supersede_automatic(&mut self) { if let Some(source) = self.automatic_preparation.take() { self.preparation.cancel(); match source { AutomaticSwitch::Keyboard => self.keyboard_switcher.cancel_applying(), AutomaticSwitch::App => self.app_switcher.cancel_applying(), } } } /// Follows the focused window's layout while switching is on, and starts /// the installed-only preparation of a mapping once it is due. fn switch_with_keyboard(&mut self) { let enabled = self.settings.keyboard_model_switching.enabled; if enabled != self.input_sources.is_some() { self.input_sources = enabled.then(Default::default); } if let Some(source) = self .input_sources .as_mut() .and_then(|watcher| watcher.poll()) { tracing::info!( source = source.as_deref().unwrap_or("none"), "keyboard input source changed" ); if self .keyboard_switcher .source_changed(source, Instant::now()) { self.supersede_automatic(); } } let Some(choice) = self.keyboard_switcher.take_due(Instant::now()) else { return; }; let selection = crate::transcription::models::TranscriptionSelection { language: choice.language, ..self.settings.transcription_for_model(choice.model) }; if selection.model != self.settings.transcription.model || selection.language != self.settings.transcription.language { if self.automatic_preparation == Some(AutomaticSwitch::App) { self.app_switcher.cancel_applying(); } self.checking = false; self.preparation.start(selection, true); self.automatic_preparation = Some(AutomaticSwitch::Keyboard); } else { // The current model stays, but an older manual preparation must // not replace it. self.keyboard_switcher.commit(); self.checking = false; if self.automatic_preparation.take() == Some(AutomaticSwitch::App) { self.app_switcher.cancel_applying(); } self.preparation.cancel(); } } /// Follows the foreground app while switching is on, and starts the /// installed-only preparation of a mode's speech choice once it is due. /// Hex's own window and a moment with no window in front name no mode, /// so they leave the last match and any switch it scheduled alone. The /// host cannot see a listener recording, so a due switch is held only /// while the shared engine's dictation streams or finishes run. fn switch_with_app(&mut self) { if !self.settings.labs.app_model_switching { return; } let application = foreground_other_process_stem(); let now = Instant::now(); if self.app_switcher.observe( crate::transcription::app_model_switching::Observation::frontmost( application.as_deref(), None, ), |application, _| { crate::transcription::app_model_switching::mode_choice( &self.settings, application, None, ) }, now, ) { self.supersede_automatic(); } if crate::transcription::engine::dictation_busy() { return; } let Some(choice) = self .app_switcher .take_due(now, &self.settings.transcription) else { return; }; let selection = crate::transcription::models::TranscriptionSelection { language: choice.language, ..self.settings.transcription_for_model(choice.model) }; if self.automatic_preparation == Some(AutomaticSwitch::Keyboard) { self.keyboard_switcher.cancel_applying(); } self.checking = false; self.preparation.start(selection, true); self.automatic_preparation = Some(AutomaticSwitch::App); } fn model_ready(&self) -> bool { crate::desktop::onboarding::model_ready(&self.runtime_selection()) } /// Verifies the selected model's file when it is here but unverified /// (another app's catalog download), using the same installed-only /// preparation as a model switch. The first check hashes the file, so it /// runs off the UI thread. No file means no check: Enable says Model /// required, as before. fn check_selected_model(&mut self) -> bool { let selection = self.runtime_selection(); let Ok(model) = crate::transcription::models::validate(&selection) else { return false; }; if !crate::transcription::models::needs_check(&model, &selection.language) { return false; } self.checking = true; self.preparation.start(selection, true); self.status = ListenerStatus::Enabling; true } pub(crate) fn start(&mut self) { self.listen_when_ready = true; if self.is_running() { return; } let selection = self.runtime_selection(); if !self.model_ready() { // Only a preparation for the selected model (its check, or a // download of it) is worth waiting for. Anything else keeps the // old answer and is left running. let preparing = self.preparation.status().model; if preparing == Some(selection.model) { self.status = ListenerStatus::Enabling; } else { if preparing.is_none() && self.check_selected_model() { return; } self.status = ListenerStatus::ModelRequired; } return; } let stop = Arc::new(AtomicBool::new(false)); let worker_stop = stop.clone(); let (result_sender, result_receiver) = mpsc::channel(); let (updates, update_receiver) = mpsc::channel(); let (swap_failure_sender, swap_failures) = mpsc::channel(); let (commands, command_receiver) = mpsc::channel(); let event_path = self.event_path.clone(); let config = crate::windows::dictation::WindowsDictationConfig { swap_failures: Some(swap_failure_sender), commands: Some(command_receiver), dictations: self.dictations.clone(), history: self.history.clone(), last_dictation: self.last_dictation.clone(), indicator: Some(self.indicator.clone()), mode_runtime: self.mode_runtime.clone(), transformations: self.transformations.clone(), ..crate::windows::dictation::WindowsDictationConfig::from_settings(&self.settings) }; let worker = std::thread::Builder::new() .name("windows-listener".into()) .spawn(move || { let result = crate::desktop::instance::acquire("windows-listener").and_then(|_instance| { crate::windows::dictation::run( &event_path, &selection, config, update_receiver, &worker_stop, ) }); let _ = result_sender.send(result.map_err(|error| format!("{error:#}"))); }); match worker { Ok(worker) => { self.listener = Some(RunningListener { stop, result: result_receiver, worker, updates, swap_failures, commands, }); self.session_before_start = self.activity.session_started_at; self.awaiting_session_start = true; self.status = ListenerStatus::Enabling; self.error = None; } Err(error) => { self.status = ListenerStatus::CouldNotEnable; self.error = Some(tr_fill("Could not start listener: {}", &error.to_string())); } } } /// Enable and Disable also decide whether Hex listens at its next launch, /// so a disabled Hex stays disabled after a restart (#575). fn remember_listening(&mut self, enabled: bool) { if self.settings.enable_on_launch == enabled { return; } let mut candidate = self.settings.clone(); candidate.enable_on_launch = enabled; match save_listening(&self.settings, &candidate) { Ok(saved) => self.apply_settings(&saved), Err(error) => tracing::warn!(%error, "could not save Enable on launch"), } } pub(crate) fn stop(&mut self) { self.listen_when_ready = false; if self.listener.is_some() { self.signal_stop(ListenerStatus::Disabling); return; } // Disable during a model check: stop the check too, and never leave // the listener card on Enabling with nothing to finish. if self.status == ListenerStatus::Enabling { // Only Enable's own check is canceled; a model download the // person started keeps running. if self.checking { self.preparation.cancel(); self.preparation.set_error(None); self.checking = false; } self.status = ListenerStatus::Disabled; } } fn is_running(&self) -> bool { self.listener.is_some() } fn signal_stop(&mut self, status: ListenerStatus) { if let Some(listener) = &self.listener { listener.stop.store(true, Ordering::Relaxed); self.status = status; } } /// Applies the settings the shared window saved. Shared state the /// listener reads changes in place; anything the listener captured at /// start restarts a running listener. fn apply_settings(&mut self, settings: &AppSettings) { if self .keyboard_switcher .configure(settings.keyboard_model_switching.clone(), Instant::now()) { self.supersede_automatic(); } let previous = std::mem::replace(&mut self.settings, settings.clone()); if self .app_switcher .configure(settings.labs.app_model_switching) { self.supersede_automatic(); } let current = settings; crate::feedback::set_enabled(current.sound_effects); crate::feedback::set_volume(current.sound_effect_volume.clamp(0.0, 1.0)); crate::feedback::set_sound_set(current.sound_theme); if previous.indicator_position != current.indicator_position { self.indicator.set_position(current.indicator_position); } if previous.history_retention != current.history_retention && let Some(history) = &self.history && let Err(error) = history.set_retention(current.history_retention) { tracing::warn!(%error, "could not update history retention"); } let modes_changed = previous.dictation_processing != current.dictation_processing; if modes_changed { *self .mode_runtime .write() .unwrap_or_else(|error| error.into_inner()) = crate::dictation::modes::ModeRuntime::from_settings(current); } if previous.ui_language != current.ui_language { crate::desktop::i18n::apply(current.ui_language.as_deref()); } if modes_changed { self.refresh_personal_commands_host(); } let model_changed = previous.transcription.model != current.transcription.model || previous.transcription.language != current.transcription.language; // A running listener applies them in place, so listening never stops // for a settings change. if let Some(listener) = &self.listener { let _ = listener.updates.send(Box::new(current.clone())); } else if model_changed && self.listen_when_ready { // A listener waiting for a model starts once one is chosen. self.start(); } } /// The saved selection, or the default model while it names an import /// that can't run. fn runtime_selection(&self) -> crate::transcription::models::TranscriptionSelection { crate::transcription::custom_models::runnable(&self.settings.transcription) } /// The listener could not load a newly chosen model and still runs the /// previous one. Settings go back to it, so they never name a model that /// is not running and the picker never offers to delete the one in use. fn keep_running_model(&mut self, failed: crate::transcription::local_transcriber::SwapFailed) { // A choice made after the failed one stands. if self.runtime_selection() != failed.wanted { return; } let mut candidate = self.settings.clone(); candidate.remember_transcription(failed.kept); match self.settings.update(&candidate) { Ok(saved) => self.apply_settings(&saved), Err(error) => tracing::warn!(%error, "could not restore the running model setting"), } self.preparation.set_error(Some(failed.error)); } /// Hosts the personal transformations workspace under the shared policy. fn refresh_personal_commands_host(&mut self) { let enabled = crate::personal_commands::host_needed( crate::personal_commands::custom_transformations_selected( &self.settings.dictation_processing, ), ); match (enabled, self.personal_commands.is_some()) { (true, false) => { self.personal_commands = crate::personal_commands::PersonalCommands::start(self.transformations.clone()); } (false, true) => self.personal_commands = None, _ => {} } } /// True once Restart to update has handed off to the new version, after /// the updater's final check; Hex then quits so it can take the named /// event. pub(crate) fn update_restarted(&mut self) -> bool { match self.updater.take_restart() { Some(Ok(())) => { self.stop(); true } Some(Err(error)) => { self.error = Some(tr_fill("Could not restart Hex: {}", &format!("{error:#}"))); false } None => false, } } pub(crate) fn refresh(&mut self) { self.switch_with_keyboard(); self.switch_with_app(); let mut checked = false; if let Some(result) = self.preparation.poll() { let automatic = self.automatic_preparation.take(); self.checking = false; let result = result.and_then(|selection| { crate::transcription::models::validate(&selection).map_err(|e| e.to_string())?; // Only a preparation that changes what Hex runs replaces the // saved choice: a check of that same model, or of a lost // import's fallback, leaves the saved selection alone. if selection == self.runtime_selection() { return Ok(()); } let mut candidate = self.settings.clone(); candidate.remember_transcription(selection); let saved = self .settings .update(&candidate) .map_err(|e| format!("{e:#}"))?; self.apply_settings(&saved); Ok(()) }); match result { Ok(()) => { match automatic { Some(AutomaticSwitch::Keyboard) => self.keyboard_switcher.commit(), Some(AutomaticSwitch::App) => self.app_switcher.commit(), None => {} } checked = true; if self.status == ListenerStatus::Enabling && !self.model_ready() { // One check per Enable: a check that finished without // making the model ready is not retried by itself. self.status = ListenerStatus::ModelRequired; } } Err(error) => { match automatic { Some(AutomaticSwitch::Keyboard) => { self.keyboard_switcher.fail(Instant::now()) } Some(AutomaticSwitch::App) => self.app_switcher.fail(Instant::now()), None => {} } // An Enable that stopped to check a model failed to reach // listening; say so instead of Enabling… forever. if self.listen_when_ready && !self.is_running() && self.status == ListenerStatus::Enabling { self.status = ListenerStatus::CouldNotEnable; } self.preparation.set_error(Some(error)); } } } // A listener waiting on its model starts once the check it needed // passed (a model found on this computer is adopted here). if checked && self.listen_when_ready && !self.is_running() && self.model_ready() { self.start(); } // A check canceled while waiting (a switch or an explicit cancel) // must not leave the host on Enabling with nothing pending. if self.listen_when_ready && !self.is_running() && self.status == ListenerStatus::Enabling && self.preparation.status().model.is_none() { self.start(); } let swap_failures = self .listener .as_ref() .map(|listener| listener.swap_failures.try_iter().collect::>()) .unwrap_or_default(); for failed in swap_failures { self.keep_running_model(failed); } if self.listen_after_setup && self.setup_done() { self.listen_after_setup = false; self.start(); } // Settings may set up the workspace at any time. if self.personal_commands_checked_at.elapsed() >= Duration::from_secs(2) { self.personal_commands_checked_at = Instant::now(); if !cfg!(test) { self.reload_settings(); } self.refresh_personal_commands_host(); } let mut start_listener = false; if let Some(listener) = &self.listener { let finished = match listener.result.try_recv() { Ok(result) => Some(result), Err(TryRecvError::Empty) => None, Err(TryRecvError::Disconnected) => { Some(Err("listener worker stopped unexpectedly".into())) } }; if let Some(result) = finished { self.awaiting_session_start = false; if let Some(listener) = self.listener.take() { let _ = listener.worker.join(); } match result { Ok(()) => { self.status = ListenerStatus::Disabled; start_listener = self.listen_when_ready; } Err(error) => { self.status = ListenerStatus::CouldNotEnable; self.error = Some(error); } } } } if start_listener { self.start(); } self.foreground.track(); self.activity.refresh(&mut self.event_reader); self.last_completed_at = last_completed_at(self.event_reader.events().iter()); if self.awaiting_session_start && self.activity.session_started_at != self.session_before_start { self.awaiting_session_start = false; } if self.is_running() && !self.awaiting_session_start && let Some(status) = self.activity.state_label() { self.status = status; } } /// Where `hex` dictation commands go: the running listener, once its /// session started (a listener still loading its model would replay /// them late). pub(crate) fn listener_commands( &self, ) -> Option> { self.listener .as_ref() .filter(|_| !self.awaiting_session_start) .map(|listener| listener.commands.clone()) } pub(crate) fn publish(&self) -> Published { let preparation = self.preparation.status(); Published { activity: self.activity.state, listener: DesktopListenerSnapshot { running: self.is_running(), warming: self.is_running() && self.awaiting_session_start, status: self.status, warning: self.indicator.failure(), last_failure: self .activity .visible_failure_since(self.started_at, self.last_completed_at), notice: None, shortcuts: DesktopShortcuts::Registered, shortcuts_configurable: false, }, operation_error: self.error.clone(), transcription: DesktopTranscriptionSnapshot { downloaded_bytes: preparation.downloaded_bytes, error: preparation.error, preparation_stage: preparation.stage, selection: self.settings.transcription.clone(), preparing: preparation.model, // The listener shares its engine with this process, so its // last finished command says where the model runs. backend: crate::transcription::engine::TranscriptionEngine::shared() .and_then(|engine| engine.held_backend()), }, update_status: self.updater.status(), update_notes: self.updater.notes(), update_blocked: self.updater.blocked(), settings: self.settings.clone(), state: self.activity.state.filter(|_| self.is_running()), } } } impl Drop for WindowsDesktopHost { fn drop(&mut self) { self.updater.stop(); if let Some(listener) = self.listener.take() { listener.stop_and_join(); } } } /// The foreground application's executable stem, or `None` when Hex owns the /// foreground window or none is focused: Labs › Switch model with app matches /// applications only, never Hex's own window. pub(crate) fn foreground_other_process_stem() -> Option { use windows_sys::Win32::System::Threading::GetCurrentProcessId; use windows_sys::Win32::UI::WindowsAndMessaging::{ GetForegroundWindow, GetWindowThreadProcessId, }; // SAFETY: plain queries; a null window owns no process. unsafe { let window = GetForegroundWindow(); if window.is_null() { return None; } let mut process_id = 0; GetWindowThreadProcessId(window, &mut process_id); if process_id == GetCurrentProcessId() { return None; } let path = crate::windows::input::window_process_path(window)?; Some(path.file_stem()?.to_string_lossy().to_lowercase()) } } /// The newest foreground window another process owns, polled with the /// runtime's refresh. The desktop and taskbars are skipped: a paste there is /// lost, so bringing one forward is never what Paste Last wants. #[derive(Default)] struct ForeignWindow(Option); impl ForeignWindow { fn track(&mut self) { use windows_sys::Win32::System::Threading::GetCurrentProcessId; use windows_sys::Win32::UI::WindowsAndMessaging::{ GetForegroundWindow, GetWindowThreadProcessId, }; // SAFETY: plain queries; a null window owns no process. unsafe { let window = GetForegroundWindow(); if window.is_null() { return; } let mut process_id = 0; GetWindowThreadProcessId(window, &mut process_id); if process_id != GetCurrentProcessId() && !crate::windows::paste::is_shell_window(window) { self.0 = Some(window as isize); } } } /// Brings that window back to the foreground, where a paste lands. fn activate(&self) { use windows_sys::Win32::UI::WindowsAndMessaging::{IsWindow, SetForegroundWindow}; let Some(window) = self.0 else { return }; let window = window as windows_sys::Win32::Foundation::HWND; // SAFETY: `IsWindow` vets a handle that may have closed since. unsafe { if IsWindow(window) != 0 { SetForegroundWindow(window); } } } } #[cfg(not(test))] fn save_listening(base: &AppSettings, settings: &AppSettings) -> Result { base.update(settings) } /// Tests never write the person's `settings.json`. #[cfg(test)] fn save_listening(_: &AppSettings, settings: &AppSettings) -> Result { Ok(settings.clone()) } #[cfg(test)] mod tests { use super::*; #[test] fn an_isolated_data_root_has_its_own_instance_event() { assert_eq!(instance_event_name(None), APP_INSTANCE_EVENT); let isolated = instance_event_name(Some(std::path::Path::new(r"C:\bench\home\hex"))); assert!(isolated.starts_with(r"Local\anomaly.hex.app.")); assert_eq!(isolated.len(), APP_INSTANCE_EVENT.len() + 17); assert_eq!( isolated, instance_event_name(Some(std::path::Path::new(r"c:\Bench\Home\HEX"))) ); assert_ne!( isolated, instance_event_name(Some(std::path::Path::new(r"C:\bench\other\hex"))) ); } }