From a2b0207c28d6bfbddf7b71b572769be17a5f1a56 Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Tue, 16 Sep 2025 18:10:03 -0700 Subject: [PATCH] feat(wm): add core graphics api wrapper module --- komorebi/src/core_graphics/error.rs | 48 +++++++++++++++++++++++++++++ komorebi/src/core_graphics/mod.rs | 43 ++++++++++++++++++++++++++ komorebi/src/lib.rs | 1 + komorebi/src/main.rs | 19 ++---------- 4 files changed, 94 insertions(+), 17 deletions(-) create mode 100644 komorebi/src/core_graphics/error.rs create mode 100644 komorebi/src/core_graphics/mod.rs diff --git a/komorebi/src/core_graphics/error.rs b/komorebi/src/core_graphics/error.rs new file mode 100644 index 00000000..29907e24 --- /dev/null +++ b/komorebi/src/core_graphics/error.rs @@ -0,0 +1,48 @@ +use objc2_core_graphics::CGError; + +#[derive(thiserror::Error, Debug)] +pub enum CoreGraphicsError { + #[error("CGError::Success")] + Success, + #[error("CGError::Failure")] + Failure, + #[error("CGError::IllegalArgument")] + IllegalArgument, + #[error("CGError::InvalidConnection")] + InvalidConnection, + #[error("CGError::InvalidContext")] + InvalidContext, + #[error("CGError::CannotComplete")] + CannotComplete, + #[error("CGError::NotImplemented")] + NotImplemented, + #[error("CGError::RangeCheck")] + RangeCheck, + #[error("CGError::TypeCheck")] + TypeCheck, + #[error("CGError::InvalidOperation")] + InvalidOperation, + #[error("CGError::NoneAvailable")] + NoneAvailable, + #[error("CGError::Unknown {0}")] + Unknown(i32), +} + +impl From for CoreGraphicsError { + fn from(value: CGError) -> Self { + match value { + CGError::Success => Self::Success, + CGError::Failure => Self::Failure, + CGError::IllegalArgument => Self::IllegalArgument, + CGError::InvalidConnection => Self::InvalidConnection, + CGError::InvalidContext => Self::InvalidContext, + CGError::CannotComplete => Self::CannotComplete, + CGError::NotImplemented => Self::NotImplemented, + CGError::RangeCheck => Self::RangeCheck, + CGError::TypeCheck => Self::TypeCheck, + CGError::InvalidOperation => Self::InvalidOperation, + CGError::NoneAvailable => Self::NoneAvailable, + error => Self::Unknown(error.0), + } + } +} diff --git a/komorebi/src/core_graphics/mod.rs b/komorebi/src/core_graphics/mod.rs new file mode 100644 index 00000000..fbecd3e6 --- /dev/null +++ b/komorebi/src/core_graphics/mod.rs @@ -0,0 +1,43 @@ +use crate::core_graphics::error::CoreGraphicsError; +use objc2_core_foundation::CFArray; +use objc2_core_foundation::CFRetained; +use objc2_core_graphics::CGGetOnlineDisplayList; +use objc2_core_graphics::CGWindowListCopyWindowInfo; +use objc2_core_graphics::CGWindowListOption; +use objc2_core_graphics::kCGNullWindowID; + +pub mod error; + +pub struct CoreGraphicsApi; + +impl CoreGraphicsApi { + pub fn connected_display_ids() -> Result, CoreGraphicsError> { + let mut displays: Vec = Vec::with_capacity(16); + let mut display_count = 0; + + unsafe { + match CoreGraphicsError::from(CGGetOnlineDisplayList( + displays.capacity() as u32, + displays.as_mut_ptr(), + &mut display_count, + )) { + CoreGraphicsError::Success => { + displays.set_len(display_count as usize); + Ok(displays) + } + error => Err(error), + } + } + } + + pub fn window_list_info() -> Option> { + unsafe { + CGWindowListCopyWindowInfo( + // this is still way too many bogus windows + CGWindowListOption::OptionOnScreenOnly | CGWindowListOption::ExcludeDesktopElements, + // required when using OnScreenOnly + kCGNullWindowID, + ) + } + } +} diff --git a/komorebi/src/lib.rs b/komorebi/src/lib.rs index 342c7905..67920701 100644 --- a/komorebi/src/lib.rs +++ b/komorebi/src/lib.rs @@ -7,6 +7,7 @@ use std::ptr::NonNull; pub mod accessibility; pub mod application; +pub mod core_graphics; pub mod layout; pub mod rect; pub mod window; diff --git a/komorebi/src/main.rs b/komorebi/src/main.rs index 9432ea3e..8a8d9abf 100644 --- a/komorebi/src/main.rs +++ b/komorebi/src/main.rs @@ -4,23 +4,19 @@ use color_eyre::eyre; use color_eyre::eyre::eyre; use komorebi::application::Application; use komorebi::cf_array_as; +use komorebi::core_graphics::CoreGraphicsApi; use komorebi::layout; use komorebi::rect::Rect; use komorebi::window::WindowInfo; use objc2::rc::autoreleasepool; use objc2_application_services::AXIsProcessTrusted; -use objc2_core_foundation::CFArray; use objc2_core_foundation::CFDictionary; -use objc2_core_foundation::CFRetained; 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 objc2_core_graphics::CGWindowListCopyWindowInfo; -use objc2_core_graphics::CGWindowListOption; -use objc2_core_graphics::kCGNullWindowID; use std::collections::HashMap; use std::sync::Arc; use std::sync::atomic::AtomicBool; @@ -106,7 +102,7 @@ fn main() -> eyre::Result<()> { let run_loop = CFRunLoop::current().ok_or(eyre!("couldn't get CFRunLoop::current"))?; - if let Some(window_info_list) = list_window_info() { + if let Some(window_info_list) = CoreGraphicsApi::window_list_info() { tracing::info!("{} windows found", window_info_list.len()); let mut windows = vec![]; @@ -208,14 +204,3 @@ fn main() -> eyre::Result<()> { Ok(()) } - -fn list_window_info() -> Option> { - unsafe { - CGWindowListCopyWindowInfo( - // this is still way too many bogus windows - CGWindowListOption::OptionOnScreenOnly | CGWindowListOption::ExcludeDesktopElements, - // required when using OnScreenOnly - kCGNullWindowID, - ) - } -}