From 46d5ea4a1d89a2013ddc3f62b011cab960fd97af Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Fri, 2 May 2025 17:49:27 -0700 Subject: [PATCH] refactor(wm): log errors when allow_set_foreground_window fails This startup Win32 API call can sporadically fail, so this commit adds some retry logic and logging of errors every time it fails. If it crosses the retry threshold, the application will exit (because you can't really have a tiling window manager running that doesn't let you set the foreground window). --- komorebi/src/main.rs | 23 ++++++++++++++++++++++- komorebi/src/process_event.rs | 2 +- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/komorebi/src/main.rs b/komorebi/src/main.rs index b6d0707a..75c56395 100644 --- a/komorebi/src/main.rs +++ b/komorebi/src/main.rs @@ -17,6 +17,7 @@ use std::time::Duration; use clap::Parser; use clap::ValueEnum; +use color_eyre::eyre::anyhow; use color_eyre::Result; use crossbeam_utils::Backoff; use komorebi::animation::AnimationEngine; @@ -193,8 +194,28 @@ fn main() -> Result<()> { let opts: Opts = Opts::parse(); CUSTOM_FFM.store(opts.focus_follows_mouse, Ordering::SeqCst); + let mut set_foreground_window_retries = 5; + let mut set_foreground_window_succeeded = false; + let process_id = WindowsApi::current_process_id(); - WindowsApi::allow_set_foreground_window(process_id)?; + while set_foreground_window_retries > 0 && !set_foreground_window_succeeded { + match WindowsApi::allow_set_foreground_window(process_id) { + Ok(_) => { + set_foreground_window_succeeded = true; + } + Err(error) => { + tracing::error!("{error}"); + set_foreground_window_retries -= 1; + } + } + + if set_foreground_window_retries == 0 { + return Err(anyhow!( + "failed call to AllowSetForegroundWindow after 5 retries" + )); + } + } + WindowsApi::set_process_dpi_awareness_context()?; let session_id = WindowsApi::process_id_to_session_id()?; diff --git a/komorebi/src/process_event.rs b/komorebi/src/process_event.rs index 1d653da2..a821ec85 100644 --- a/komorebi/src/process_event.rs +++ b/komorebi/src/process_event.rs @@ -163,7 +163,7 @@ impl WindowManager { // to be consumed by integrating gui applications like bars to know // when to show visual components associated with komorebi's virtual // desktop - tracing::error!("notifying subscribers that we are back on komorebi's associated virtual desktop"); + tracing::debug!("notifying subscribers that we are back on komorebi's associated virtual desktop"); notify_subscribers( Notification { event: NotificationEvent::VirtualDesktop(