From e5468dc0362aaaaab672739f641f575da96db4b1 Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Sat, 27 Sep 2025 13:26:30 -0700 Subject: [PATCH] feat(wm): ignore events on other spaces This commit ensures that events and commands on spaces other than the one that komorebi was started on will be ignored. --- komorebi/src/accessibility/private.rs | 2 +- komorebi/src/lib.rs | 8 ++++++++ komorebi/src/main.rs | 5 ----- komorebi/src/process_command.rs | 9 +++++++++ komorebi/src/process_event.rs | 9 +++++++++ komorebi/src/skylight.rs | 6 ++++++ komorebi/src/static_config.rs | 2 ++ komorebi/src/window_manager.rs | 5 ++++- komorebic/src/main.rs | 26 +++++++++++++++++++++----- 9 files changed, 60 insertions(+), 12 deletions(-) create mode 100644 komorebi/src/skylight.rs diff --git a/komorebi/src/accessibility/private.rs b/komorebi/src/accessibility/private.rs index 5e1eab7c..2c6318d5 100644 --- a/komorebi/src/accessibility/private.rs +++ b/komorebi/src/accessibility/private.rs @@ -6,5 +6,5 @@ use objc2_core_graphics::CGWindowID; // https://github.com/nikitabobko/AeroSpace?tab=readme-ov-file#project-values unsafe extern "C" { /// Extract `window_id` from an AXUIElement. - pub(crate) fn _AXUIElementGetWindow(elem: &AXUIElement, window_id: *mut CGWindowID) -> AXError; + pub fn _AXUIElementGetWindow(elem: &AXUIElement, window_id: *mut CGWindowID) -> AXError; } diff --git a/komorebi/src/lib.rs b/komorebi/src/lib.rs index 58118b10..6c58f538 100644 --- a/komorebi/src/lib.rs +++ b/komorebi/src/lib.rs @@ -38,6 +38,7 @@ use std::collections::HashMap; use std::io::Write; use std::ops::Deref; use std::os::unix::net::UnixStream; +use std::panic; use std::path::PathBuf; use std::ptr::NonNull; use std::sync::Arc; @@ -64,6 +65,7 @@ pub mod notification_center_listener; pub mod process_command; pub mod process_event; pub mod reaper; +pub mod skylight; pub mod state; pub mod static_config; pub mod window; @@ -149,6 +151,12 @@ shadow_rs::shadow!(build); pub static DEFAULT_WORKSPACE_PADDING: AtomicI32 = AtomicI32::new(5); pub static DEFAULT_CONTAINER_PADDING: AtomicI32 = AtomicI32::new(5); +#[must_use] +pub fn current_space_id() -> Option { + panic::catch_unwind(|| unsafe { skylight::CGSGetActiveSpace(skylight::CGSMainConnectionID()) }) + .ok() +} + #[derive(Clone, Debug, Serialize, Deserialize)] #[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] #[serde(untagged)] diff --git a/komorebi/src/main.rs b/komorebi/src/main.rs index 1d8256d2..faa6b574 100644 --- a/komorebi/src/main.rs +++ b/komorebi/src/main.rs @@ -26,8 +26,6 @@ use objc2::rc::autoreleasepool; use objc2_application_services::AXIsProcessTrusted; use objc2_core_foundation::CFRunLoop; use objc2_core_foundation::kCFRunLoopDefaultMode; -use objc2_core_graphics::CGDisplayBounds; -use objc2_core_graphics::CGMainDisplayID; use objc2_core_graphics::CGPreflightScreenCaptureAccess; use objc2_core_graphics::CGRequestScreenCaptureAccess; use parking_lot::Mutex; @@ -217,9 +215,6 @@ fn main() -> eyre::Result<()> { let _notification_center_listener = NotificationCenterListener::init(); let _display_reconfiguration_listener = DisplayReconfigurationListener::init(); - let display_size = unsafe { CGDisplayBounds(CGMainDisplayID()) }; - tracing::info!("display size for main display is: {:?}", display_size); - let run_loop = CFRunLoop::current().ok_or_eyre("couldn't get CFRunLoop::current")?; let _input_listener = InputEventListener::init(&run_loop); diff --git a/komorebi/src/process_command.rs b/komorebi/src/process_command.rs index 7e4a3746..8389d2fe 100644 --- a/komorebi/src/process_command.rs +++ b/komorebi/src/process_command.rs @@ -29,6 +29,7 @@ use crate::core::layout::Layout; use crate::core::operation_direction::OperationDirection; use crate::core::rect::Rect; use crate::core_graphics::CoreGraphicsApi; +use crate::current_space_id; use crate::macos_api::MacosApi; use crate::monitor::MonitorInformation; use crate::notify_subscribers; @@ -107,6 +108,14 @@ impl WindowManager { message: SocketMessage, mut reply: impl std::io::Write, ) -> eyre::Result<()> { + if let Some(space_id) = &self.space_id + && let Some(current_space_id) = current_space_id() + && *space_id != current_space_id + { + tracing::trace!("ignoring events and commands while not on space {space_id}"); + return Ok(()); + } + tracing::info!("processing command: {message}"); #[allow(clippy::useless_asref)] diff --git a/komorebi/src/process_event.rs b/komorebi/src/process_event.rs index cb6b0418..7322c25d 100644 --- a/komorebi/src/process_event.rs +++ b/komorebi/src/process_event.rs @@ -17,6 +17,7 @@ use crate::core::default_layout::DefaultLayout; use crate::core::layout::Layout; use crate::core::operation_direction::OperationDirection; use crate::core::rect::Rect; +use crate::current_space_id; use crate::macos_api::MacosApi; use crate::notify_subscribers; use crate::state::State; @@ -68,6 +69,14 @@ impl WindowManager { return Ok(()); } + if let Some(space_id) = &self.space_id + && let Some(current_space_id) = current_space_id() + && *space_id != current_space_id + { + tracing::trace!("ignoring events and commands while not on space {space_id}"); + return Ok(()); + } + let mut rule_debug = RuleDebug::default(); let mut should_manage = true; diff --git a/komorebi/src/skylight.rs b/komorebi/src/skylight.rs new file mode 100644 index 00000000..7d9c423f --- /dev/null +++ b/komorebi/src/skylight.rs @@ -0,0 +1,6 @@ +// Only read operations are permitted, and their usage and potential failure must +// never disrupt the window manager +unsafe extern "C" { + pub fn CGSMainConnectionID() -> i32; + pub fn CGSGetActiveSpace(cid: i32) -> u64; +} diff --git a/komorebi/src/static_config.rs b/komorebi/src/static_config.rs index b4eff49f..5f1f331a 100644 --- a/komorebi/src/static_config.rs +++ b/komorebi/src/static_config.rs @@ -28,6 +28,7 @@ use crate::core::default_layout::LayoutOptions; use crate::core::layout::Layout; use crate::core::pathext::ResolvedPathBuf; use crate::core::rect::Rect; +use crate::current_space_id; use crate::monitor; use crate::monitor::Monitor; use crate::monitor_reconciliator; @@ -1126,6 +1127,7 @@ impl StaticConfig { applications: Default::default(), incoming_events: incoming, command_listener: listener, + space_id: current_space_id(), is_paused: false, // virtual_desktop_id: current_virtual_desktop(), work_area_offset: value.global_work_area_offset, diff --git a/komorebi/src/window_manager.rs b/komorebi/src/window_manager.rs index 22ed3bdc..6e6ea273 100644 --- a/komorebi/src/window_manager.rs +++ b/komorebi/src/window_manager.rs @@ -23,6 +23,7 @@ use crate::core::default_layout::DefaultLayout; use crate::core::layout::Layout; use crate::core::operation_direction::OperationDirection; use crate::core::rect::Rect; +use crate::current_space_id; use crate::lockable_sequence::Lockable; use crate::macos_api::MacosApi; use crate::monitor::Monitor; @@ -63,6 +64,7 @@ pub struct WindowManager { pub applications: HashMap, pub run_loop: CoreFoundationRunLoop, pub command_listener: UnixListener, + pub space_id: Option, pub is_paused: bool, pub resize_delta: i32, pub hotwatch: Hotwatch, @@ -145,6 +147,7 @@ impl WindowManager { applications: Default::default(), run_loop: CoreFoundationRunLoop(run_loop.clone()), command_listener: listener, + space_id: current_space_id(), is_paused: false, resize_delta: 50, hotwatch: Hotwatch::new()?, @@ -1136,7 +1139,7 @@ impl WindowManager { tracing::info!("removing window"); if self.focused_container()?.windows().len() == 1 { - eyre::bail!("a container must have at least one window"); + bail!("a container must have at least one window"); } let workspace = self.focused_workspace_mut()?; diff --git a/komorebic/src/main.rs b/komorebic/src/main.rs index 7223a45f..05062699 100644 --- a/komorebic/src/main.rs +++ b/komorebic/src/main.rs @@ -1249,18 +1249,34 @@ fn main() -> eyre::Result<()> { current_exe.pop(); let komorebi_exe = current_exe.join("komorebi"); - println!("\nPlease grant Accessibility permissions to \"{}\"", komorebi_exe.display()); + println!( + "\nPlease grant Accessibility permissions to \"{}\"", + komorebi_exe.display() + ); println!("Accessibility permissions are required for komorebi to to manage windows"); - Command::new("open").arg("x-apple.systempreferences:com.apple.preference.security?Privacy_Accessibility").spawn()?; + Command::new("open") + .arg( + "x-apple.systempreferences:com.apple.preference.security?Privacy_Accessibility", + ) + .spawn()?; println!("\nHit return when done..."); std::io::stdout().flush()?; let mut input = String::new(); std::io::stdin().read_line(&mut input)?; - println!("Please grant Screen Recording permissions to \"{}\"", komorebi_exe.display()); - println!("Screen Recording permissions are required to for komorebi to read window titles"); - Command::new("open").arg("x-apple.systempreferences:com.apple.preference.security?Privacy_ScreenCapture").spawn()?; + println!( + "Please grant Screen Recording permissions to \"{}\"", + komorebi_exe.display() + ); + println!( + "Screen Recording permissions are required to for komorebi to read window titles" + ); + Command::new("open") + .arg( + "x-apple.systempreferences:com.apple.preference.security?Privacy_ScreenCapture", + ) + .spawn()?; println!("\nHit return when done..."); std::io::stdout().flush()?;