diff --git a/komorebi/src/animation/engine.rs b/komorebi/src/animation/engine.rs index ce4c3182..64782215 100644 --- a/komorebi/src/animation/engine.rs +++ b/komorebi/src/animation/engine.rs @@ -131,8 +131,7 @@ impl AnimationEngine { ANIMATION_MANAGER.lock().start(key.as_str()); } - let target_frame_time = - Duration::from_millis(1000 / crate::settings::animation_fps()); + let target_frame_time = Duration::from_millis(1000 / crate::settings::animation_fps()); let animation_start = Instant::now(); let mut stats = FrameStats::default(); diff --git a/komorebi/src/daemon.rs b/komorebi/src/daemon.rs index 81ad851c..3492bcd0 100644 --- a/komorebi/src/daemon.rs +++ b/komorebi/src/daemon.rs @@ -15,13 +15,13 @@ use std::sync::atomic::Ordering; #[cfg(feature = "deadlock_detection")] use std::time::Duration; +use crate::animation::AnimationEngine; +use crate::replace_env_in_path; use clap::Parser; use clap::ValueEnum; use color_eyre::eyre; use color_eyre::eyre::bail; use crossbeam_utils::Backoff; -use crate::animation::AnimationEngine; -use crate::replace_env_in_path; use parking_lot::Mutex; #[cfg(feature = "deadlock_detection")] use parking_lot::deadlock; diff --git a/komorebi/src/lib.rs b/komorebi/src/lib.rs index c6e2b36d..a93adca1 100644 --- a/komorebi/src/lib.rs +++ b/komorebi/src/lib.rs @@ -3,11 +3,11 @@ #[macro_use] extern crate komorebi_collections; -pub mod daemon; pub mod animation; pub mod border_manager; pub mod cloaked; pub mod com; +pub mod daemon; pub use komorebi_collections::ring; pub mod container; pub mod core; @@ -15,13 +15,13 @@ pub mod focus_manager; pub mod live_resize; pub use komorebi_collections::Lockable; pub use komorebi_collections::lockable_sequence; +pub mod lock_holder; pub mod monitor; pub mod monitor_reconciliator; pub mod pan; pub mod process_command; pub mod process_event; pub mod process_movement; -pub mod lock_holder; pub mod reaper; pub mod scrolling; pub mod set_window_position; @@ -268,6 +268,10 @@ pub static DEFAULT_RESIZE_DELTA: i32 = komorebi_settings::shared::DEFAULT_RESIZE pub static DEFAULT_MOUSE_FOLLOWS_FOCUS: bool = komorebi_settings::shared::DEFAULT_MOUSE_FOLLOWS_FOCUS; +/// Counts the commands the window manager has processed. An answer computed from the rule lists +/// outside the lock (see `process_event::ManageCheck`) is only used if no command, which is what +/// changes those lists, has run since. +pub static COMMAND_GENERATION: AtomicU64 = AtomicU64::new(0); pub static INITIAL_CONFIGURATION_LOADED: AtomicBool = AtomicBool::new(false); pub static CUSTOM_FFM: AtomicBool = AtomicBool::new(false); pub static SESSION_ID: AtomicU32 = AtomicU32::new(0); diff --git a/komorebi/src/process_command.rs b/komorebi/src/process_command.rs index ffa6db6d..2ee253f3 100644 --- a/komorebi/src/process_command.rs +++ b/komorebi/src/process_command.rs @@ -313,6 +313,15 @@ impl WindowManager { ) -> eyre::Result<()> { let _held = crate::lock_holder::held(format!("command {message}")); let command_started = std::time::Instant::now(); + // when this command is over, an event answer computed before it is not used (see + // `COMMAND_GENERATION`); the bump comes after the command has changed what it changes + struct BumpGeneration; + impl Drop for BumpGeneration { + fn drop(&mut self) { + crate::COMMAND_GENERATION.fetch_add(1, Ordering::SeqCst); + } + } + let _bump_generation = BumpGeneration; if let Some(virtual_desktop_id) = &self.virtual_desktop_id && let Some(id) = current_virtual_desktop() && id != *virtual_desktop_id @@ -1443,10 +1452,12 @@ impl WindowManager { }); } SocketMessage::Transparency(enable) => { - crate::settings::settings().update(|settings| settings.shared.transparency.enabled = enable); + crate::settings::settings() + .update(|settings| settings.shared.transparency.enabled = enable); } SocketMessage::TransparencyAlpha(alpha) => { - crate::settings::settings().update(|settings| settings.shared.transparency.alpha = alpha); + crate::settings::settings() + .update(|settings| settings.shared.transparency.alpha = alpha); } SocketMessage::StackbarMode(mode) => { STACKBAR_MODE.store(mode); diff --git a/komorebi/src/process_event.rs b/komorebi/src/process_event.rs index 562200d4..36b11b67 100644 --- a/komorebi/src/process_event.rs +++ b/komorebi/src/process_event.rs @@ -122,10 +122,14 @@ pub fn listen_for_events(wm: Arc>) { tracing::info!("listening"); loop { if let Ok(event) = receiver.recv() { + // Win32 calls on the event's window and rule matching need no window manager state, so + // they are not done with the lock held (they took seconds for a window that had + // just been created) + let check = ManageCheck::compute(event); let waiting = std::time::Instant::now(); let mut guard = crate::lock_holder::lock(&wm, "process_event"); let handling = std::time::Instant::now(); - let result = guard.process_event(event); + let result = guard.process_event_checked(event, check); let (waited, handled) = (handling.duration_since(waiting), handling.elapsed()); if waited.max(handled) >= crate::lock_holder::SLOW { tracing::info!( @@ -157,23 +161,74 @@ pub fn listen_for_events(wm: Arc>) { }); } +/// Whether an event's window is to be managed, found before the window manager is locked +pub struct ManageCheck { + should_manage: eyre::Result, + rule_debug: RuleDebug, + started: std::time::Instant, + generation: u64, +} + +impl ManageCheck { + /// Asks `Window::should_manage` for the window of the event. It reads the window, the rule + /// lists (in their documented lock order) and settings, never the window manager. + pub fn compute(event: WindowManagerEvent) -> Self { + let generation = crate::COMMAND_GENERATION.load(std::sync::atomic::Ordering::SeqCst); + let started = std::time::Instant::now(); + let mut rule_debug = RuleDebug::default(); + let should_manage = event.window().should_manage(Some(event), &mut rule_debug); + tracing::debug!( + "process_event: should_manage took {} ms", + started.elapsed().as_millis() + ); + + Self { + should_manage, + rule_debug, + started, + generation, + } + } + + /// Whether the answer can still be used: no command (which is what changes the rule lists) + /// has been processed since it was computed + fn is_current(&self) -> bool { + self.generation == crate::COMMAND_GENERATION.load(std::sync::atomic::Ordering::SeqCst) + } +} + impl WindowManager { - #[allow(clippy::too_many_lines, clippy::cognitive_complexity)] - #[tracing::instrument(skip(self, event), fields(event = event.title(), winevent = event.winevent(), hwnd = event.hwnd()))] pub fn process_event(&mut self, event: WindowManagerEvent) -> eyre::Result<()> { + self.process_event_checked(event, ManageCheck::compute(event)) + } + + #[allow(clippy::too_many_lines, clippy::cognitive_complexity)] + #[tracing::instrument(skip(self, event, check), fields(event = event.title(), winevent = event.winevent(), hwnd = event.hwnd()))] + pub fn process_event_checked( + &mut self, + event: WindowManagerEvent, + check: ManageCheck, + ) -> eyre::Result<()> { if self.is_paused { tracing::trace!("ignoring while paused"); return Ok(()); } - let mut rule_debug = RuleDebug::default(); - - let should_manage_started = std::time::Instant::now(); - let should_manage = event.window().should_manage(Some(event), &mut rule_debug)?; - tracing::debug!( - "process_event: should_manage took {} ms", - should_manage_started.elapsed().as_millis() - ); + // A command may have changed the rule lists while this event waited for the lock: the + // answer is then asked again, here, as it used to be. The window itself may have changed + // in the wait too; the handlers below already cope with a window that is gone or changed, + // as they did for an event that sat in the queue before the lock was taken. + let check = if check.is_current() { + tracing::debug!( + "process_event: should_manage answered {} ms before the lock was held", + check.started.elapsed().as_millis() + ); + check + } else { + ManageCheck::compute(event) + }; + let rule_debug = check.rule_debug; + let should_manage = check.should_manage?; // All event handlers below this point should only be processed if the event is // related to a window that should be managed by the WindowManager. diff --git a/komorebi/src/static_config.rs b/komorebi/src/static_config.rs index 1bd86d53..dc7208f3 100644 --- a/komorebi/src/static_config.rs +++ b/komorebi/src/static_config.rs @@ -311,12 +311,8 @@ impl From<&WindowManager> for StaticConfig { border_offset: Option::from(border_manager::BORDER_OFFSET.load(Ordering::SeqCst)), border: Option::from(border_manager::BORDER_ENABLED.load(Ordering::SeqCst)), border_colours, - transparency: Option::from( - crate::settings::transparency_enabled(), - ), - transparency_alpha: Option::from( - crate::settings::transparency_alpha(), - ), + transparency: Option::from(crate::settings::transparency_enabled()), + transparency_alpha: Option::from(crate::settings::transparency_alpha()), transparency_ignore_rules: None, border_style: Option::from(STYLE.load()), #[allow(deprecated)] diff --git a/komorebi/src/window.rs b/komorebi/src/window.rs index 3c1a7a13..6da1e310 100644 --- a/komorebi/src/window.rs +++ b/komorebi/src/window.rs @@ -404,7 +404,6 @@ impl Window { // enabled, duration and style from one snapshot if let Some((duration, style)) = crate::settings::animation_for(GhostDispatcher::PREFIX) { - let render_dispatcher = GhostDispatcher::new(self.hwnd, window_rect, *layout, top, style); @@ -585,8 +584,9 @@ impl Window { pub fn transparent(self) -> eyre::Result<()> { // enabled, duration and style from one snapshot - if let Some((duration, style)) = crate::settings::animation_for(TransparencyRenderDispatcher::PREFIX) { - + if let Some((duration, style)) = + crate::settings::animation_for(TransparencyRenderDispatcher::PREFIX) + { let render_dispatcher = TransparencyRenderDispatcher::new( self.hwnd, false, @@ -600,17 +600,15 @@ impl Window { let mut ex_style = self.ex_style()?; ex_style.insert(ExtendedWindowStyle::LAYERED); self.update_ex_style(&ex_style)?; - WindowsApi::set_transparent( - self.hwnd, - crate::settings::transparency_alpha(), - ) + WindowsApi::set_transparent(self.hwnd, crate::settings::transparency_alpha()) } } pub fn opaque(self) -> eyre::Result<()> { // enabled, duration and style from one snapshot - if let Some((duration, style)) = crate::settings::animation_for(TransparencyRenderDispatcher::PREFIX) { - + if let Some((duration, style)) = + crate::settings::animation_for(TransparencyRenderDispatcher::PREFIX) + { let render_dispatcher = TransparencyRenderDispatcher::new( self.hwnd, true,