mirror of
https://github.com/LGUG2Z/komorebi.git
synced 2026-11-20 07:31:39 +01:00
feat(wm): add reaper and input event listener
This commit adds a reaper module which gets notified whenever an AXUIElement is invalid (so far this is notified when a set_position call on a Window fails), and an input event listener which somewhat coarsely emits an event whenever a mouse-up or key-up is registered. This is to handle Apple applications like Notes and Mail which don't fire any events at all when a window is closed. Pretty gross. Will see if this can be improved in the future.
This commit is contained in:
1 parent
c45090c5c5
commit
d5919112d9
10 files changed
+220
-23
No files matched your search
+24
-16
@@ -131,22 +131,30 @@ impl Application {
|
||||
.unwrap_or_else(|| String::from("<NO NAME FOUND>"))
|
||||
);
|
||||
|
||||
match AccessibilityApi::add_observer_to_run_loop(
|
||||
&self.observer,
|
||||
&self.element,
|
||||
NOTIFICATIONS,
|
||||
run_loop,
|
||||
) {
|
||||
Ok(_) => {
|
||||
self.is_observable = true;
|
||||
}
|
||||
Err(error) => {
|
||||
tracing::warn!(
|
||||
"failed to register observer for process {} ({}): {error}",
|
||||
self.process_id,
|
||||
self.name()
|
||||
.unwrap_or_else(|| String::from("<NO NAME FOUND>"))
|
||||
);
|
||||
let mut retries = 5;
|
||||
|
||||
while retries > 0 {
|
||||
match AccessibilityApi::add_observer_to_run_loop(
|
||||
&self.observer,
|
||||
&self.element,
|
||||
NOTIFICATIONS,
|
||||
run_loop,
|
||||
) {
|
||||
Ok(_) => {
|
||||
self.is_observable = true;
|
||||
break;
|
||||
}
|
||||
Err(error) => {
|
||||
// Chromium apps are still gross on macOS too
|
||||
retries -= 1;
|
||||
std::thread::sleep(std::time::Duration::from_millis(100));
|
||||
tracing::warn!(
|
||||
"failed to register observer for process {} ({}): {error}, {retries} retries left",
|
||||
self.process_id,
|
||||
self.name()
|
||||
.unwrap_or_else(|| String::from("<NO NAME FOUND>"))
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
use crate::reaper;
|
||||
use crate::reaper::ReaperNotification;
|
||||
use objc2_core_foundation::CFMachPort;
|
||||
use objc2_core_foundation::CFRetained;
|
||||
use objc2_core_foundation::CFRunLoop;
|
||||
use objc2_core_foundation::kCFRunLoopDefaultMode;
|
||||
use objc2_core_graphics::CGEvent;
|
||||
use objc2_core_graphics::CGEventTapLocation;
|
||||
use objc2_core_graphics::CGEventTapOptions;
|
||||
use objc2_core_graphics::CGEventTapPlacement;
|
||||
use objc2_core_graphics::CGEventTapProxy;
|
||||
use objc2_core_graphics::CGEventType;
|
||||
use std::ffi::c_void;
|
||||
use std::ptr::NonNull;
|
||||
|
||||
pub struct InputEventListener {
|
||||
port: CFRetained<CFMachPort>,
|
||||
}
|
||||
|
||||
extern "C-unwind" fn callback(
|
||||
_: CGEventTapProxy,
|
||||
_event_type: CGEventType,
|
||||
mut event_ref: NonNull<CGEvent>,
|
||||
_listener: *mut c_void,
|
||||
) -> *mut CGEvent {
|
||||
reaper::send_notification(ReaperNotification::MouseUpKeyUp);
|
||||
unsafe { event_ref.as_mut() }
|
||||
}
|
||||
impl InputEventListener {
|
||||
pub fn init(run_loop: &CFRunLoop) -> Option<Self> {
|
||||
let mouse_event_mask = (1 << CGEventType::LeftMouseUp.0) | (1 << CGEventType::KeyUp.0);
|
||||
let mut port = None;
|
||||
|
||||
unsafe {
|
||||
let tap_port = CGEvent::tap_create(
|
||||
CGEventTapLocation::HIDEventTap,
|
||||
CGEventTapPlacement::HeadInsertEventTap,
|
||||
CGEventTapOptions::Default,
|
||||
mouse_event_mask,
|
||||
Some(callback),
|
||||
std::ptr::null_mut(),
|
||||
);
|
||||
|
||||
if let Some(tap_port) = &tap_port {
|
||||
port = Some(tap_port.clone());
|
||||
}
|
||||
|
||||
match CFMachPort::new_run_loop_source(None, tap_port.as_deref(), 0) {
|
||||
None => {}
|
||||
Some(source) => {
|
||||
CFRunLoop::add_source(run_loop, Some(&source), kCFRunLoopDefaultMode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
port.map(|port| Self { port })
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for InputEventListener {
|
||||
fn drop(&mut self) {
|
||||
CFMachPort::invalidate(&self.port);
|
||||
unsafe {
|
||||
CGEvent::tap_enable(&self.port, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -36,6 +36,7 @@ mod container;
|
||||
pub mod core;
|
||||
pub mod core_graphics;
|
||||
pub mod display_reconfiguration_listener;
|
||||
pub mod input_event_listener;
|
||||
pub mod lockable_sequence;
|
||||
pub mod macos_api;
|
||||
pub mod monitor;
|
||||
@@ -43,6 +44,7 @@ pub mod monitor_reconciliator;
|
||||
pub mod notification_center_listener;
|
||||
pub mod process_command;
|
||||
pub mod process_event;
|
||||
pub mod reaper;
|
||||
pub mod window;
|
||||
pub mod window_manager;
|
||||
pub mod window_manager_event;
|
||||
|
||||
@@ -6,11 +6,13 @@ use komorebi::DATA_DIR;
|
||||
use komorebi::UPDATE_MONITOR_WORK_AREAS;
|
||||
use komorebi::ax_event_listener;
|
||||
use komorebi::display_reconfiguration_listener::DisplayReconfigurationListener;
|
||||
use komorebi::input_event_listener::InputEventListener;
|
||||
use komorebi::macos_api::MacosApi;
|
||||
use komorebi::monitor_reconciliator;
|
||||
use komorebi::notification_center_listener::NotificationCenterListener;
|
||||
use komorebi::process_command::listen_for_commands;
|
||||
use komorebi::process_event::listen_for_events;
|
||||
use komorebi::reaper;
|
||||
use komorebi::window_manager::WindowManager;
|
||||
use objc2::rc::autoreleasepool;
|
||||
use objc2_application_services::AXIsProcessTrusted;
|
||||
@@ -111,17 +113,21 @@ fn main() -> eyre::Result<()> {
|
||||
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);
|
||||
|
||||
let wm = Arc::new(Mutex::new(WindowManager::new(
|
||||
&run_loop,
|
||||
ax_event_listener::event_rx(),
|
||||
None,
|
||||
)?));
|
||||
|
||||
wm.lock().init()?;
|
||||
wm.lock().update_focused_workspace(true, true)?;
|
||||
|
||||
listen_for_commands(wm.clone());
|
||||
listen_for_events(wm.clone());
|
||||
monitor_reconciliator::listen_for_notifications(wm.clone())?;
|
||||
reaper::listen_for_notifications(wm.clone());
|
||||
|
||||
let quit_ctrlc = Arc::new(AtomicBool::new(false));
|
||||
let quit_thread = quit_ctrlc.clone();
|
||||
|
||||
@@ -165,8 +165,8 @@ impl NotificationCenterListener {
|
||||
None,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
observer
|
||||
observer
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
use crate::accessibility::AccessibilityApi;
|
||||
use crate::accessibility::notification_constants::AccessibilityNotification;
|
||||
use crate::ax_event_listener::event_tx;
|
||||
use crate::core::default_layout::DefaultLayout;
|
||||
use crate::core::layout::Layout;
|
||||
use crate::window::Window;
|
||||
@@ -83,7 +84,15 @@ impl WindowManager {
|
||||
window.focus(false)?;
|
||||
}
|
||||
} else {
|
||||
workspace.focus_container_by_window(window_id)?;
|
||||
// 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
|
||||
if workspace.focus_container_by_window(window_id).is_err() {
|
||||
event_tx().send(WindowManagerEvent::Show(
|
||||
notification,
|
||||
event.process_id(),
|
||||
))?
|
||||
}
|
||||
}
|
||||
|
||||
workspace.layer = WorkspaceLayer::Tiling;
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
use crate::window_manager::WindowManager;
|
||||
use crossbeam_channel::Receiver;
|
||||
use crossbeam_channel::Sender;
|
||||
use parking_lot::Mutex;
|
||||
use std::sync::Arc;
|
||||
use std::sync::OnceLock;
|
||||
|
||||
pub enum ReaperNotification {
|
||||
InvalidWindow(u32),
|
||||
MouseUpKeyUp,
|
||||
}
|
||||
|
||||
static CHANNEL: OnceLock<(Sender<ReaperNotification>, Receiver<ReaperNotification>)> =
|
||||
OnceLock::new();
|
||||
|
||||
pub fn channel() -> &'static (Sender<ReaperNotification>, Receiver<ReaperNotification>) {
|
||||
CHANNEL.get_or_init(|| crossbeam_channel::bounded(50))
|
||||
}
|
||||
|
||||
fn event_tx() -> Sender<ReaperNotification> {
|
||||
channel().0.clone()
|
||||
}
|
||||
|
||||
fn event_rx() -> Receiver<ReaperNotification> {
|
||||
channel().1.clone()
|
||||
}
|
||||
|
||||
pub fn send_notification(notification: ReaperNotification) {
|
||||
if event_tx().try_send(notification).is_err() {
|
||||
tracing::warn!("channel is full; dropping notification")
|
||||
}
|
||||
}
|
||||
|
||||
pub fn listen_for_notifications(wm: Arc<Mutex<WindowManager>>) {
|
||||
std::thread::spawn(move || {
|
||||
loop {
|
||||
match handle_notifications(wm.clone()) {
|
||||
Ok(()) => {
|
||||
tracing::warn!("restarting finished thread");
|
||||
}
|
||||
Err(error) => {
|
||||
tracing::warn!("restarting failed thread: {}", error);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
fn handle_notifications(wm: Arc<Mutex<WindowManager>>) -> color_eyre::Result<()> {
|
||||
tracing::info!("listening");
|
||||
|
||||
let receiver = event_rx();
|
||||
|
||||
for notification in receiver {
|
||||
let mut wm = wm.lock();
|
||||
|
||||
match notification {
|
||||
ReaperNotification::InvalidWindow(window_id) => {
|
||||
let mut should_update = false;
|
||||
for monitor in wm.monitors_mut() {
|
||||
for workspace in monitor.workspaces_mut() {
|
||||
for container in workspace.containers_mut() {
|
||||
container.windows_mut().retain(|w| {
|
||||
if w.id == window_id {
|
||||
should_update = true;
|
||||
tracing::info!("reaping window: {window_id}");
|
||||
}
|
||||
|
||||
w.id != window_id
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If an invalid window was cleaned up, we update the workspace
|
||||
wm.update_focused_workspace(false, false)?;
|
||||
}
|
||||
ReaperNotification::MouseUpKeyUp => {
|
||||
// this first one will do a "nothing" update to check if any windows failed
|
||||
// to have their positions set - the failures will trigger an InvalidWindow
|
||||
// notification
|
||||
// TODO: maybe replace this with something a bit more lightweight
|
||||
wm.update_focused_workspace(false, false)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
+17
-2
@@ -25,6 +25,8 @@ use crate::core::rect::Rect;
|
||||
use crate::core_graphics::CoreGraphicsApi;
|
||||
use crate::hidden_frame_bottom_left;
|
||||
use crate::macos_api::MacosApi;
|
||||
use crate::reaper;
|
||||
use crate::reaper::ReaperNotification;
|
||||
use crate::window_manager_event::SystemNotification;
|
||||
use crate::window_manager_event::WindowManagerEvent;
|
||||
use objc2_app_kit::NSApplicationActivationOptions;
|
||||
@@ -284,8 +286,21 @@ impl Window {
|
||||
}
|
||||
|
||||
pub fn set_position(&self, rect: &Rect) -> Result<(), AccessibilityError> {
|
||||
self.set_point(CGPoint::new(rect.left as CGFloat, rect.top as CGFloat))?;
|
||||
self.set_size(CGSize::new(rect.right as CGFloat, rect.bottom as CGFloat))
|
||||
match self.set_point(CGPoint::new(rect.left as CGFloat, rect.top as CGFloat)) {
|
||||
Ok(_) => {}
|
||||
Err(error) => {
|
||||
reaper::send_notification(ReaperNotification::InvalidWindow(self.id));
|
||||
return Err(error);
|
||||
}
|
||||
}
|
||||
|
||||
match self.set_size(CGSize::new(rect.right as CGFloat, rect.bottom as CGFloat)) {
|
||||
Ok(_) => Ok(()),
|
||||
Err(error) => {
|
||||
reaper::send_notification(ReaperNotification::InvalidWindow(self.id));
|
||||
Err(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn focus(&self, mouse_follows_focus: bool) -> Result<(), LibraryError> {
|
||||
|
||||
@@ -510,8 +510,6 @@ impl WindowManager {
|
||||
follow_focus: bool,
|
||||
trigger_focus: bool,
|
||||
) -> eyre::Result<()> {
|
||||
tracing::info!("updating");
|
||||
|
||||
let offset = self.work_area_offset;
|
||||
let mouse_follows_focus = self.mouse_follows_focus;
|
||||
|
||||
|
||||
@@ -33,6 +33,9 @@ impl WindowManagerEvent {
|
||||
SystemNotification::Accessibility(AccessibilityNotification::AXMainWindowChanged)
|
||||
| SystemNotification::Accessibility(
|
||||
AccessibilityNotification::AXApplicationActivated,
|
||||
)
|
||||
| SystemNotification::AppKitWorkspace(
|
||||
AppKitWorkspaceNotification::NSWorkspaceDidActivateApplicationNotification,
|
||||
) => Some(WindowManagerEvent::FocusChange(
|
||||
notification,
|
||||
process_id,
|
||||
|
||||
Reference in new issue
Block a user