diff --git a/komorebi/src/application.rs b/komorebi/src/application.rs index 52db02ec..d9cc66df 100644 --- a/komorebi/src/application.rs +++ b/komorebi/src/application.rs @@ -13,10 +13,10 @@ use crate::accessibility::notification_constants::kAXApplicationShownNotificatio use crate::accessibility::notification_constants::kAXMainWindowChangedNotification; use crate::accessibility::notification_constants::kAXUIElementDestroyedNotification; use crate::accessibility::notification_constants::kAXWindowCreatedNotification; -use crate::ax_event_listener::event_tx; use crate::window::Window; use crate::window_manager_event::SystemNotification; use crate::window_manager_event::WindowManagerEvent; +use crate::window_manager_event_listener; use objc2_application_services::AXObserver; use objc2_application_services::AXUIElement; use objc2_core_foundation::CFArray; @@ -76,13 +76,11 @@ unsafe extern "C-unwind" fn application_observer_callback( None, ) { - if let Err(error) = event_tx().send(event) { - tracing::error!("failed to send window manager event: {error}"); - } else { - tracing::debug!( - "notification: {notification}, process: {process_id}, name: \"{name}\"", - ); - } + tracing::debug!( + "notification: {notification}, process: {process_id}, name: \"{name}\"", + ); + + window_manager_event_listener::send_notification(event); } } } diff --git a/komorebi/src/input_event_listener.rs b/komorebi/src/input_event_listener.rs index f6f466a1..f43e03b1 100644 --- a/komorebi/src/input_event_listener.rs +++ b/komorebi/src/input_event_listener.rs @@ -1,5 +1,11 @@ +use crate::accessibility::AccessibilityApi; +use crate::macos_api::MacosApi; use crate::reaper; use crate::reaper::ReaperNotification; +use crate::window_manager_event::ManualNotification; +use crate::window_manager_event::SystemNotification; +use crate::window_manager_event::WindowManagerEvent; +use crate::window_manager_event_listener; use objc2_core_foundation::CFMachPort; use objc2_core_foundation::CFRetained; use objc2_core_foundation::CFRunLoop; @@ -24,6 +30,22 @@ extern "C-unwind" fn callback( _listener: *mut c_void, ) -> *mut CGEvent { reaper::send_notification(ReaperNotification::MouseUpKeyUp); + if let Some(element) = MacosApi::foreground_window() { + let mut pid = 0; + unsafe { + element.pid(NonNull::from_mut(&mut pid)); + } + + if pid != 0 + && let Some(event) = WindowManagerEvent::from_system_notification( + SystemNotification::Manual(ManualNotification::ShowOnInputEvent), + pid, + AccessibilityApi::window_id(&element).ok(), + ) + { + window_manager_event_listener::send_notification(event); + } + } unsafe { event_ref.as_mut() } } impl InputEventListener { diff --git a/komorebi/src/lib.rs b/komorebi/src/lib.rs index d8502480..5fda1262 100644 --- a/komorebi/src/lib.rs +++ b/komorebi/src/lib.rs @@ -39,7 +39,6 @@ pub mod ring; pub mod accessibility; pub mod app_kit_notification_constants; pub mod application; -pub mod ax_event_listener; pub mod container; pub mod core; pub mod core_graphics; @@ -59,6 +58,7 @@ pub mod static_config; pub mod window; pub mod window_manager; pub mod window_manager_event; +pub mod window_manager_event_listener; pub mod workspace; lazy_static! { @@ -89,23 +89,12 @@ lazy_static! { static ref REGEX_IDENTIFIERS: Arc>> = Arc::new(Mutex::new(HashMap::new())); static ref MANAGE_IDENTIFIERS: Arc>> = Arc::new(Mutex::new(vec![])); - static ref IGNORE_IDENTIFIERS: Arc>> = Arc::new(Mutex::new(vec![ - MatchingRule::Simple(IdWithIdentifier { - kind: ApplicationIdentifier::Class, - id: String::from("OPContainerClass"), - matching_strategy: Option::from(MatchingStrategy::Equals), - }), - MatchingRule::Simple(IdWithIdentifier { - kind: ApplicationIdentifier::Class, - id: String::from("IHWindowClass"), - matching_strategy: Option::from(MatchingStrategy::Equals), - }), - MatchingRule::Simple(IdWithIdentifier { + static ref IGNORE_IDENTIFIERS: Arc>> = + Arc::new(Mutex::new(vec![MatchingRule::Simple(IdWithIdentifier { kind: ApplicationIdentifier::Exe, - id: String::from("komorebi-bar.exe"), + id: String::from("Spotlight"), matching_strategy: Option::from(MatchingStrategy::Equals), - }) - ])); + })])); static ref SESSION_FLOATING_APPLICATIONS: Arc>> = Arc::new(Mutex::new(Vec::new())); static ref FLOATING_APPLICATIONS: Arc>> = @@ -120,6 +109,8 @@ lazy_static! { "Finder".to_string(), "Ghostty".to_string() ])); + static ref DO_NOT_OBSERVE_APPLICATIONS: Arc>> = + Arc::new(Mutex::new(vec!["Spotlight".to_string(),])); } shadow_rs::shadow!(build); diff --git a/komorebi/src/main.rs b/komorebi/src/main.rs index 89fbf972..f260cc94 100644 --- a/komorebi/src/main.rs +++ b/komorebi/src/main.rs @@ -7,7 +7,6 @@ use color_eyre::eyre::OptionExt; use komorebi::DATA_DIR; use komorebi::HOME_DIR; use komorebi::UPDATE_MONITOR_WORK_AREAS; -use komorebi::ax_event_listener; use komorebi::core::pathext::replace_env_in_path; use komorebi::display_reconfiguration_listener::DisplayReconfigurationListener; use komorebi::input_event_listener::InputEventListener; @@ -19,6 +18,7 @@ use komorebi::process_event::listen_for_events; use komorebi::reaper; use komorebi::static_config::StaticConfig; use komorebi::window_manager::WindowManager; +use komorebi::window_manager_event_listener; use objc2::rc::autoreleasepool; use objc2_application_services::AXIsProcessTrusted; use objc2_core_foundation::CFRunLoop; @@ -213,14 +213,14 @@ fn main() -> eyre::Result<()> { Arc::new(Mutex::new(StaticConfig::preload( config, - ax_event_listener::event_rx(), + window_manager_event_listener::event_rx(), None, &run_loop, )?)) } else { Arc::new(Mutex::new(WindowManager::new( &run_loop, - ax_event_listener::event_rx(), + window_manager_event_listener::event_rx(), None, )?)) }; diff --git a/komorebi/src/notification_center_listener.rs b/komorebi/src/notification_center_listener.rs index f82b5d29..7f71b1c6 100644 --- a/komorebi/src/notification_center_listener.rs +++ b/komorebi/src/notification_center_listener.rs @@ -1,8 +1,8 @@ use crate::app_kit_notification_constants::AppKitWorkspaceNotification; use crate::application::Application; -use crate::ax_event_listener::event_tx; use crate::window_manager_event::SystemNotification; use crate::window_manager_event::WindowManagerEvent; +use crate::window_manager_event_listener; use objc2::AnyThread; use objc2::define_class; use objc2::msg_send; @@ -74,9 +74,8 @@ define_class! { process_id, window_id, ) - && let Err(error) = event_tx().send(event) { - tracing::error!("failed to send window manager event: {error}"); + window_manager_event_listener::send_notification(event); }; } } diff --git a/komorebi/src/process_event.rs b/komorebi/src/process_event.rs index d5ddb390..66c1e518 100644 --- a/komorebi/src/process_event.rs +++ b/komorebi/src/process_event.rs @@ -4,15 +4,16 @@ use crate::accessibility::AccessibilityApi; use crate::accessibility::error::AccessibilityApiError; use crate::accessibility::error::AccessibilityError; use crate::accessibility::notification_constants::AccessibilityNotification; -use crate::ax_event_listener::event_tx; use crate::core::config_generation::MatchingRule; use crate::core::default_layout::DefaultLayout; use crate::core::layout::Layout; use crate::macos_api::MacosApi; use crate::window::Window; use crate::window_manager::WindowManager; +use crate::window_manager_event::ManualNotification; use crate::window_manager_event::SystemNotification; use crate::window_manager_event::WindowManagerEvent; +use crate::window_manager_event_listener; use crate::workspace::WorkspaceLayer; use color_eyre::eyre; use parking_lot::Mutex; @@ -72,11 +73,18 @@ impl WindowManager { return Ok(()); } - tracing::info!( - "processing event: {event} for process {} with notification {}", - event.process_id(), - event.notification(), - ); + // don't want to spam logs for manually triggered hacks triggered + // from the input listener + if !matches!( + event, + WindowManagerEvent::Show(SystemNotification::Manual(_), _) + ) { + tracing::info!( + "processing event: {event} for process {} with notification {}", + event.process_id(), + event.notification(), + ); + } self.enforce_workspace_rules()?; @@ -113,10 +121,19 @@ impl WindowManager { )) => { // this means we have closed the 1st tab, so we need this window object to be reaped // and for the new 1st tab to be the key element of the window struct - event_tx().send(WindowManagerEvent::Show( - notification, - event.process_id(), - ))?; + if let Some(event) = + WindowManagerEvent::from_system_notification( + SystemNotification::Manual( + ManualNotification::ShowOnFocusChangeFirstTabDestroyed, + ), + event.process_id(), + Some(window_id), + ) + { + window_manager_event_listener::send_notification( + event, + ); + } first_tab_destroyed = true; @@ -215,10 +232,19 @@ impl WindowManager { // if this fails, the app was probably open but windowless when komorebi // launched, so the window hasn't been registered - we should treat it // as a "Show" event - event_tx().send(WindowManagerEvent::Show( - notification, - event.process_id(), - ))? + if let Some(event) = + WindowManagerEvent::from_system_notification( + SystemNotification::Manual( + ManualNotification::ShowOnFocusChangeWindowlessAppRestored, + ), + event.process_id(), + Some(window_id), + ) + { + window_manager_event_listener::send_notification( + event, + ); + } } } @@ -297,8 +323,16 @@ impl WindowManager { drop(tabbed_applications); if workspace.contains_window(window_id) { + if !matches!( + event, + WindowManagerEvent::Show(SystemNotification::Manual(_), _) + ) { + // don't want to spam logs for manually triggered hacks triggered + // from the input listener + tracing::debug!("ignoring show event for window already on workspace"); + } + // ignore bogus show events - tracing::debug!("ignoring show event for window already on workspace"); create = false; } diff --git a/komorebi/src/window.rs b/komorebi/src/window.rs index 3a51e9ff..8c32f202 100644 --- a/komorebi/src/window.rs +++ b/komorebi/src/window.rs @@ -26,7 +26,6 @@ use crate::accessibility::notification_constants::kAXWindowMiniaturizedNotificat use crate::accessibility::notification_constants::kAXWindowMovedNotification; use crate::accessibility::notification_constants::kAXWindowResizedNotification; use crate::application::Application; -use crate::ax_event_listener::event_tx; use crate::cf_dictionary_value; use crate::core::ApplicationIdentifier; use crate::core::config_generation::IdWithIdentifier; @@ -39,6 +38,7 @@ use crate::macos_api::MacosApi; use crate::reaper; use crate::window_manager_event::SystemNotification; use crate::window_manager_event::WindowManagerEvent; +use crate::window_manager_event_listener; use color_eyre::eyre; use color_eyre::eyre::OptionExt; use objc2::__framework_prelude::Retained; @@ -115,14 +115,12 @@ unsafe extern "C-unwind" fn window_observer_callback( window_id, ) { - if let Err(error) = event_tx().send(event) { - tracing::error!("failed to send window manager event: {error}"); - } else { - tracing::debug!( - "notification: {}, process: {process_id}, name: \"{name}\"", - notification, - ); - } + tracing::debug!( + "notification: {}, process: {process_id}, name: \"{name}\"", + notification, + ); + + window_manager_event_listener::send_notification(event); } } } diff --git a/komorebi/src/window_manager_event.rs b/komorebi/src/window_manager_event.rs index 0cb79b3d..386a8af3 100644 --- a/komorebi/src/window_manager_event.rs +++ b/komorebi/src/window_manager_event.rs @@ -11,6 +11,14 @@ use strum::Display; pub enum SystemNotification { Accessibility(AccessibilityNotification), AppKitWorkspace(AppKitWorkspaceNotification), + Manual(ManualNotification), +} + +#[derive(Debug, Copy, Clone, Serialize, Deserialize, Display)] +pub enum ManualNotification { + ShowOnInputEvent, + ShowOnFocusChangeFirstTabDestroyed, + ShowOnFocusChangeWindowlessAppRestored, } #[derive(Debug, Copy, Clone, Serialize, Deserialize, Display)] @@ -45,7 +53,14 @@ impl WindowManagerEvent { | SystemNotification::Accessibility(AccessibilityNotification::AXApplicationShown) | SystemNotification::AppKitWorkspace( AppKitWorkspaceNotification::NSWorkspaceDidLaunchApplicationNotification, - ) => Some(WindowManagerEvent::Show(notification, process_id)), + ) + | SystemNotification::Manual( + ManualNotification::ShowOnFocusChangeWindowlessAppRestored, + ) + | SystemNotification::Manual(ManualNotification::ShowOnFocusChangeFirstTabDestroyed) + | SystemNotification::Manual(ManualNotification::ShowOnInputEvent) => { + Some(WindowManagerEvent::Show(notification, process_id)) + } SystemNotification::Accessibility(AccessibilityNotification::AXUIElementDestroyed) | SystemNotification::AppKitWorkspace( AppKitWorkspaceNotification::NSWorkspaceDidTerminateApplicationNotification, @@ -88,6 +103,7 @@ impl WindowManagerEvent { | WindowManagerEvent::Restore(n, _, _) => match n { SystemNotification::Accessibility(a) => a.to_string(), SystemNotification::AppKitWorkspace(a) => a.to_string(), + SystemNotification::Manual(m) => m.to_string(), }, } } diff --git a/komorebi/src/ax_event_listener.rs b/komorebi/src/window_manager_event_listener.rs similarity index 68% rename from komorebi/src/ax_event_listener.rs rename to komorebi/src/window_manager_event_listener.rs index 2afdd702..555e3641 100644 --- a/komorebi/src/ax_event_listener.rs +++ b/komorebi/src/window_manager_event_listener.rs @@ -10,10 +10,16 @@ fn channel() -> &'static (Sender, Receiver Sender { +fn event_tx() -> Sender { channel().0.clone() } pub fn event_rx() -> Receiver { channel().1.clone() } + +pub fn send_notification(notification: WindowManagerEvent) { + if event_tx().try_send(notification).is_err() { + tracing::warn!("channel is full; dropping notification") + } +}