//! 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; use crate::desktop_host::{ DesktopAction, DesktopListenerSnapshot, DesktopShortcuts, DesktopTranscriptionSnapshot, }; use crate::desktop_i18n::tr_fill; use crate::events::{EventReader, TranscriptPhase, VoiceEvent}; use crate::history::History; use crate::transcription_models::{ModelPreparationStage, TranscriptionModelId}; use crate::transcription_preparation::TranscriptionPreparation; use crate::windows_dictation::voice_action_model; 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, } impl RunningListener { fn stop_and_join(self) { self.stop.store(true, Ordering::Relaxed); let _ = self.worker.join(); } } /// 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, 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: String, error: Option, settings: AppSettings, history: Option, last_dictation: Arc>>, indicator: crate::windows_rune_hud::WindowsIndicatorSender, mode_runtime: Arc>, transformations: Arc, personal_commands: Option, personal_commands_checked_at: Instant, voice_action_model: Arc>>, opencode_catalog_rx: Option>>, preparation: TranscriptionPreparation, updater: crate::updater::Updater, } /// Names the event that marks the running Hex app in this login session. const APP_INSTANCE_EVENT: &str = r"Local\anomaly.hex.app"; /// Ownership of the one Hex app in this login session. /// /// 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: Vec = APP_INSTANCE_EVENT.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); } } // 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::windows_rune_hud::WindowsIndicatorSender, ) -> Self { crate::desktop_i18n::apply(settings.ui_language.as_deref()); 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 mode_runtime = Arc::new(RwLock::new( crate::dictation_modes::ModeRuntime::from_settings(&settings), )); let voice_action_model = Arc::new(RwLock::new(voice_action_model(&settings))); let mut host = Self { event_path, event_reader, activity, last_completed_at, listener: None, session_before_start: None, awaiting_session_start: false, listen_when_ready: false, listen_after_setup: false, status: "Disabled".into(), error: settings_error, settings, history, last_dictation: Arc::new(Mutex::new(None)), indicator, mode_runtime, transformations: Arc::new(crate::personal_commands::TransformationClient::default()), personal_commands: None, personal_commands_checked_at: Instant::now(), voice_action_model, opencode_catalog_rx: None, preparation: TranscriptionPreparation::default(), updater: crate::updater::Updater::start(), }; crate::transcription_models::delete_set_aside(); host.refresh_personal_commands_host(); host.adopt_default_voice_action_model(); // The microphone stays cold until first-run setup finishes. if host.settings.enable_on_launch { if crate::onboarding::completion_recorded() { host.start(); } else { host.listen_after_setup = true; } } host } /// 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(); } } Request::ChooseModel { model, language } => self.choose_model(model, language), Request::CancelModel => self.preparation.cancel(), Request::Quit => return true, Request::ReloadSettings => match AppSettings::load() { Ok(settings) => self.apply_settings(&settings), Err(error) => tracing::warn!(%error, "could not reload settings"), }, Request::CaptureShortcutFor(_) | Request::CancelShortcut | Request::SetShortcut { .. } => {} } false } fn dispatch(&mut self, action: DesktopAction) -> bool { match action { DesktopAction::StartListening => self.start(), DesktopAction::StopListening => self.stop(), DesktopAction::ClearError => self.error = None, DesktopAction::CheckForUpdates | DesktopAction::OpenUpdate | DesktopAction::RestartIntoUpdate | DesktopAction::ConfirmUpdateSwitch | DesktopAction::WindowFocused => match self.updater.act(&action) { // Ends the app so the new version can take the named event. Ok(quit) if quit => { self.stop(); return true; } Ok(_) => {} Err(error) => { self.error = Some(tr_fill("Could not restart Hex: {}", &format!("{error:#}"))); } }, // Windows saves shortcuts from the window. DesktopAction::SetDictationShortcut(_) => {} } false } fn choose_model(&mut self, model: TranscriptionModelId, language: String) { let definition = crate::transcription_models::definition(model); let selection = crate::transcription_models::TranscriptionSelection { model, language, // Hints persist across models that can use them. recognition_hints: if definition.supports_recognition_hints { self.settings.transcription.recognition_hints.clone() } else { String::new() }, }; self.preparation.start(selection, false); } fn model_ready(&self) -> bool { let model = crate::transcription_models::definition(self.settings.transcription.model); crate::transcription_models::is_installed(model, &self.settings.transcription.language) && crate::transcription_models::is_verified(model) } pub(crate) fn start(&mut self) { self.listen_when_ready = true; if self.is_running() { return; } if !self.model_ready() { self.status = "Model required".into(); 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 event_path = self.event_path.clone(); let selection = self.runtime_selection(); let config = crate::windows_dictation::WindowsDictationConfig { swap_failures: Some(swap_failure_sender), 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(), voice_action_model: self.voice_action_model.clone(), ..crate::windows_dictation::WindowsDictationConfig::from_settings(&self.settings) }; let worker = std::thread::Builder::new() .name("windows-listener".into()) .spawn(move || { let result = crate::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, }); self.session_before_start = self.activity.session_started_at; self.awaiting_session_start = true; self.status = "Enabling…".into(); self.error = None; } Err(error) => { self.status = "Couldn't enable".into(); self.error = Some(tr_fill("Could not start listener: {}", &error.to_string())); } } } pub(crate) fn stop(&mut self) { self.listen_when_ready = false; self.signal_stop("Disabling…"); } fn is_running(&self) -> bool { self.listener.is_some() } fn signal_stop(&mut self, status: &str) { if let Some(listener) = &self.listener { listener.stop.store(true, Ordering::Relaxed); self.status = status.into(); } } /// 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) { let previous = std::mem::replace(&mut self.settings, settings.clone()); let current = settings; crate::feedback::set_enabled(current.sound_effects); crate::feedback::set_volume(current.sound_effect_volume.clamp(0.0, 1.0)); 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.voice_action != current.voice_action { *self .voice_action_model .write() .unwrap_or_else(|error| error.into_inner()) = voice_action_model(current); self.adopt_default_voice_action_model(); } 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(); } } /// With an enabled voice action and no chosen model, voice actions use /// OpenCode's default model; settings keep "default" so the choice /// follows OpenCode. fn adopt_default_voice_action_model(&mut self) { if self.settings.voice_action.enabled && self.settings.voice_action.model.is_none() && self.opencode_catalog_rx.is_none() && crate::dictation_processor::opencode_installed() { let (sender, receiver) = mpsc::channel(); self.opencode_catalog_rx = Some(receiver); let _ = std::thread::Builder::new() .name("windows-opencode-catalog".into()) .spawn(move || { let _ = sender.send(crate::dictation_processor::load_model_catalog()); }); } } fn poll_opencode_catalog(&mut self) { let Some(receiver) = &self.opencode_catalog_rx else { return; }; let catalog = match receiver.try_recv() { Ok(catalog) => catalog, Err(TryRecvError::Empty) => return, Err(TryRecvError::Disconnected) => { Err(eyre!("the OpenCode catalog worker stopped unexpectedly")) } }; self.opencode_catalog_rx = None; match catalog { Ok(catalog) => { if self.settings.voice_action.model.is_none() && let Some(default) = catalog .default_key .as_ref() .and_then(|key| catalog.models.iter().find(|choice| &choice.key == key)) { *self .voice_action_model .write() .unwrap_or_else(|error| error.into_inner()) = Some(default.model()); } } Err(error) => tracing::warn!(%error, "could not load the OpenCode model catalog"), } } /// The selection with recognition hints stripped when the model cannot /// use them, so validation never rejects a load because hints linger /// from a Whisper session. fn runtime_selection(&self) -> crate::transcription_models::TranscriptionSelection { let mut selection = self.settings.transcription.clone(); if !crate::transcription_models::definition(selection.model).supports_recognition_hints { selection.recognition_hints = String::new(); } selection } /// 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::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 candidate.save() { Ok(()) => self.apply_settings(&candidate), Err(error) => tracing::warn!(%error, "could not restore the running model setting"), } self.preparation.set_error(Some(failed.error)); } /// Hosts the personal transformations workspace while any mode uses a /// transformation or the workspace exists. fn refresh_personal_commands_host(&mut self) { let enabled = crate::app_settings::transformations_used(&self.settings.dictation_processing) || crate::personal_commands::workspace_configured(); match (enabled, self.personal_commands.is_some()) { (true, false) => { self.personal_commands = crate::personal_commands::PersonalCommands::start( crate::commands::CommandConfig::new(), self.transformations.clone(), ); } (false, true) => self.personal_commands = None, _ => {} } } pub(crate) fn refresh(&mut self) { if let Some(result) = self.preparation.poll() { let result = result.and_then(|selection| { crate::transcription_models::validate(&selection).map_err(|e| e.to_string())?; let mut candidate = self.settings.clone(); candidate.remember_transcription(selection); candidate.save().map_err(|e| format!("{e:#}"))?; self.apply_settings(&candidate); Ok(()) }); if let Err(error) = result { self.preparation.set_error(Some(error)); } } 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 && crate::onboarding::completion_recorded() { self.listen_after_setup = false; self.start(); } self.poll_opencode_catalog(); // 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(); 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 = "Disabled".into(); start_listener = self.listen_when_ready; } Err(error) => { self.status = "Couldn't enable".into(); self.error = Some(error); } } } } if start_listener { self.start(); } 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.into(); } } pub(crate) fn publish(&self) -> Published { let preparation = self.preparation.status(); Published { listener: DesktopListenerSnapshot { running: self.is_running(), status: self.status.clone(), warning: visible_failure(&self.activity, self.last_completed_at), shortcuts: DesktopShortcuts::Registered, }, operation_error: self.error.clone(), transcription: DesktopTranscriptionSnapshot { downloaded_bytes: preparation.downloaded_bytes, error: preparation.error, preparation_stage: preparation .model .map(|_| ModelPreparationStage::Downloading), selection: self.settings.transcription.clone(), preparing: preparation.model, }, update_status: self.updater.status(), settings: self.settings.clone(), } } } impl Drop for WindowsDesktopHost { fn drop(&mut self) { self.updater.stop(); if let Some(listener) = self.listener.take() { listener.stop_and_join(); } } } /// When the current session's newest dictation completed. `events` is /// oldest first, as `EventReader` keeps them. fn last_completed_at<'a>(events: impl DoubleEndedIterator) -> Option { for event in events.rev() { match event { VoiceEvent::SessionStarted { .. } => return None, VoiceEvent::Transcript { timestamp_ms, phase: TranscriptPhase::Completed, text, .. } if !text.trim().is_empty() => return Some(*timestamp_ms), _ => {} } } None } /// The newest failure, hidden once a later dictation completed, so a silent /// failure still reaches the Settings window without sticking around. fn visible_failure(activity: &DesktopActivity, last_completed_at: Option) -> Option { activity .last_failure .as_ref() .filter(|(failed_at, _)| last_completed_at.is_none_or(|completed| completed < *failed_at)) .map(|(_, message)| message.clone()) } #[cfg(test)] mod tests { use super::*; fn transcript(timestamp_ms: u64, text: &str) -> VoiceEvent { VoiceEvent::Transcript { timestamp_ms, phase: TranscriptPhase::Completed, latency_ms: 10, text: text.into(), } } #[test] fn the_newest_completion_is_scoped_to_the_current_session() { let events = [ transcript(1, "Old session"), VoiceEvent::SessionStarted { timestamp_ms: 2 }, ]; assert_eq!(last_completed_at(events.iter()), None); let events = [ VoiceEvent::SessionStarted { timestamp_ms: 2 }, transcript(3, "First"), transcript(4, "Second"), transcript(5, " "), ]; assert_eq!(last_completed_at(events.iter()), Some(4)); } #[test] fn newest_failure_shows_until_a_later_transcript_completes() { let activity = DesktopActivity { last_failure: Some((5, "OpenCode generation failed".into())), ..DesktopActivity::default() }; assert_eq!( visible_failure(&activity, Some(4)).as_deref(), Some("OpenCode generation failed") ); assert_eq!(visible_failure(&activity, Some(6)), None); } #[test] fn voice_action_model_keys_split_into_provider_and_id() { let mut settings = AppSettings::default(); assert!(voice_action_model(&settings).is_none()); settings.voice_action.model = Some("openai/gpt-5.6-sol".into()); settings.voice_action.variant = Some("fast".into()); let model = voice_action_model(&settings).unwrap(); assert_eq!(model.provider, "openai"); assert_eq!(model.id, "gpt-5.6-sol"); assert_eq!(model.variant.as_deref(), Some("fast")); } }