diff --git a/komorebi/src/skylight.rs b/komorebi/src/skylight.rs index 0e09142f..0c4d1b92 100644 --- a/komorebi/src/skylight.rs +++ b/komorebi/src/skylight.rs @@ -2,6 +2,17 @@ // // Read operations (CGS*) are used for querying window server state. // Write operations (SLS*) are used for animation screen update batching. +// +// What is only nice to have is not linked but looked up when it is first wanted (see +// `set_front_process`), so that a symbol that goes away does not stop komorebi from starting. +use std::ffi::CStr; +use std::ffi::c_char; +use std::ffi::c_void; +use std::ptr::NonNull; +use std::sync::OnceLock; +use std::sync::atomic::AtomicU32; +use std::sync::atomic::Ordering; + unsafe extern "C" { // Read operations - these APIs are undocumented but stable - their usage should not disrupt the window manager. pub fn CGSMainConnectionID() -> i32; @@ -66,6 +77,145 @@ impl Drop for BatchedUpdates { } } +unsafe extern "C" { + // `RTLD_DEFAULT` is what is asked for here: everything the process has loaded + fn dlsym(handle: *mut c_void, symbol: *const c_char) -> *mut c_void; +} + +const RTLD_DEFAULT: *mut c_void = -2isize as *mut c_void; + +/// Asks for a request to be treated as one that the user made: one from a process that is not in +/// the front is otherwise compared with the last input of the user, and turned away as expired if +/// there has been any since the request was stamped, which for komorebi is when it started. +const USER_GENERATED: u32 = 0x200; + +/// How many requests in a row the window server may refuse before nothing is asked of it again +const MAX_CONSECUTIVE_FAILURES: u32 = 3; + +#[repr(C)] +#[derive(Debug, Default)] +struct ProcessSerialNumber { + high: u32, + low: u32, +} + +type GetProcessForPid = unsafe extern "C" fn(pid: i32, psn: *mut ProcessSerialNumber) -> i32; +type SetFrontProcessWithOptions = + unsafe extern "C" fn(psn: *const ProcessSerialNumber, window_id: u32, options: u32) -> i32; + +struct FrontProcessApi { + get_process_for_pid: GetProcessForPid, + set_front_process_with_options: SetFrontProcessWithOptions, +} + +struct FrontProcess { + api: Option, + failures: AtomicU32, +} + +fn lookup(name: &CStr) -> Option> { + NonNull::new(unsafe { dlsym(RTLD_DEFAULT, name.as_ptr()) }) +} + +impl FrontProcess { + fn resolve() -> Self { + let get_process_for_pid = lookup(c"GetProcessForPID"); + let set_front_process_with_options = lookup(c"_SLPSSetFrontProcessWithOptions"); + + let api = match (get_process_for_pid, set_front_process_with_options) { + (Some(get_process_for_pid), Some(set_front_process_with_options)) => unsafe { + Some(FrontProcessApi { + get_process_for_pid: std::mem::transmute::<*mut c_void, GetProcessForPid>( + get_process_for_pid.as_ptr(), + ), + set_front_process_with_options: std::mem::transmute::< + *mut c_void, + SetFrontProcessWithOptions, + >( + set_front_process_with_options.as_ptr() + ), + }) + }, + _ => { + tracing::warn!( + "the window server's way of bringing an application to the front was not found, activating applications the usual way instead" + ); + None + } + }; + + Self { + api, + failures: AtomicU32::new(0), + } + } + + fn request(&self, process_id: i32, window_id: u32) -> bool { + let Some(api) = &self.api else { + return false; + }; + + if self.failures.load(Ordering::Relaxed) >= MAX_CONSECUTIVE_FAILURES { + return false; + } + + // a process that is on its way out has no serial number, which is for the usual way to + // make of what it will, and is nothing to hold against the window server + let mut psn = ProcessSerialNumber::default(); + let status = unsafe { (api.get_process_for_pid)(process_id, &mut psn) }; + if status != 0 { + tracing::debug!("no process serial number for process {process_id}: {status}"); + return false; + } + + let status = + unsafe { (api.set_front_process_with_options)(&psn, window_id, USER_GENERATED) }; + if status == 0 { + self.failures.store(0, Ordering::Relaxed); + return true; + } + + let failures = self.failures.fetch_add(1, Ordering::Relaxed) + 1; + if failures >= MAX_CONSECUTIVE_FAILURES { + tracing::warn!( + "the window server refused to bring process {process_id} to the front: {status}, and {failures} times in a row, so applications are activated the usual way from now on" + ); + } else { + tracing::warn!( + "the window server refused to bring process {process_id} to the front: {status}" + ); + } + + false + } +} + +/// Bring the application of a window to the front, the way a click on it would. +/// +/// Activating an application from here, which is not the one in front, is a request that is +/// stamped with the time komorebi started, and the window server turns it away as expired once the +/// user has typed or clicked anything since: after that the application that was in front stays +/// there, and focus only moves in komorebi. This asks the window server directly, as a request +/// of the user's. +/// +/// It is a private API that is looked up the first time it is wanted, and what it says is only +/// believed for as long as it keeps saying yes. This is for what is nice to have and not more: +/// `false` is for anything that did not work, whether the symbols are gone, the process is gone, or +/// the window server has refused three times in a row, and is for the caller to do what it always +/// did. It cannot tell a symbol that now takes other arguments from one that does what it did. +pub fn set_front_process(process_id: i32, window_id: u32) -> bool { + // there is no window server to ask when testing + if cfg!(test) { + return false; + } + + static FRONT_PROCESS: OnceLock = OnceLock::new(); + + FRONT_PROCESS + .get_or_init(FrontProcess::resolve) + .request(process_id, window_id) +} + #[cfg(test)] mod tests { use super::*; @@ -112,4 +262,147 @@ mod tests { drop(outer); assert_eq!(open(), 0); } + + extern "C" fn serial_number_found(pid: i32, psn: *mut ProcessSerialNumber) -> i32 { + unsafe { (*psn).low = pid as u32 }; + 0 + } + + extern "C" fn no_serial_number(_pid: i32, _psn: *mut ProcessSerialNumber) -> i32 { + -600 + } + + fn front_process( + get_process_for_pid: GetProcessForPid, + set_front_process_with_options: SetFrontProcessWithOptions, + ) -> FrontProcess { + FrontProcess { + api: Some(FrontProcessApi { + get_process_for_pid, + set_front_process_with_options, + }), + failures: AtomicU32::new(0), + } + } + + #[test] + fn a_symbol_that_is_not_there_is_not_found() { + assert!(lookup(c"komorebi_is_not_a_symbol_of_any_library").is_none()); + } + + // Not run with the others, because it is for finding out whether an update of macOS has taken + // the symbols away, which komorebi is meant to carry on through: `cargo test -- --ignored` + #[test] + #[ignore = "needs the real symbols, to find out whether they are still there"] + fn the_symbols_are_there() { + let front_process = FrontProcess::resolve(); + assert!(front_process.api.is_some()); + + // and the first of them is for this process, which it knows + let api = front_process.api.unwrap(); + let mut psn = ProcessSerialNumber::default(); + let status = unsafe { (api.get_process_for_pid)(std::process::id() as i32, &mut psn) }; + assert_eq!(status, 0); + assert!(psn.low != 0); + } + + #[test] + fn without_the_symbols_nothing_is_asked_and_nothing_goes_wrong() { + let front_process = FrontProcess { + api: None, + failures: AtomicU32::new(0), + }; + + assert!(!front_process.request(1, 2)); + } + + #[test] + fn nothing_is_asked_of_the_window_server_when_testing() { + assert!(!set_front_process(1, 2)); + } + + #[test] + fn a_request_that_is_accepted_is_made_for_the_user_and_for_the_window() { + static CALLS: AtomicU32 = AtomicU32::new(0); + + extern "C" fn accepts( + psn: *const ProcessSerialNumber, + window_id: u32, + options: u32, + ) -> i32 { + assert_eq!(unsafe { (*psn).low }, 79469); + assert_eq!(window_id, 27576); + assert_eq!(options, USER_GENERATED); + CALLS.fetch_add(1, Ordering::Relaxed); + 0 + } + + let front_process = front_process(serial_number_found, accepts); + + assert!(front_process.request(79469, 27576)); + assert_eq!(CALLS.load(Ordering::Relaxed), 1); + } + + #[test] + fn a_process_without_a_serial_number_is_left_to_the_usual_way_and_is_not_held_against_it() { + static CALLS: AtomicU32 = AtomicU32::new(0); + + extern "C" fn counts(_: *const ProcessSerialNumber, _: u32, _: u32) -> i32 { + CALLS.fetch_add(1, Ordering::Relaxed); + 0 + } + + let front_process = front_process(no_serial_number, counts); + + for _ in 0..MAX_CONSECUTIVE_FAILURES * 2 { + assert!(!front_process.request(1, 2)); + } + + assert_eq!(CALLS.load(Ordering::Relaxed), 0); + assert_eq!(front_process.failures.load(Ordering::Relaxed), 0); + } + + #[test] + fn a_window_server_that_keeps_refusing_is_not_asked_again() { + static CALLS: AtomicU32 = AtomicU32::new(0); + + extern "C" fn refuses(_: *const ProcessSerialNumber, _: u32, _: u32) -> i32 { + CALLS.fetch_add(1, Ordering::Relaxed); + 1000 + } + + let front_process = front_process(serial_number_found, refuses); + + for _ in 0..MAX_CONSECUTIVE_FAILURES { + assert!(!front_process.request(1, 2)); + } + assert_eq!(CALLS.load(Ordering::Relaxed), MAX_CONSECUTIVE_FAILURES); + + for _ in 0..10 { + assert!(!front_process.request(1, 2)); + } + assert_eq!(CALLS.load(Ordering::Relaxed), MAX_CONSECUTIVE_FAILURES); + } + + #[test] + fn a_refusal_now_and_then_does_not_turn_it_off() { + static CALLS: AtomicU32 = AtomicU32::new(0); + + // refuses every other request + extern "C" fn refuses_every_other(_: *const ProcessSerialNumber, _: u32, _: u32) -> i32 { + if CALLS.fetch_add(1, Ordering::Relaxed).is_multiple_of(2) { + 1000 + } else { + 0 + } + } + + let front_process = front_process(serial_number_found, refuses_every_other); + + for round in 0..20 { + assert_eq!(front_process.request(1, 2), round % 2 == 1); + } + + assert_eq!(CALLS.load(Ordering::Relaxed), 20); + } } diff --git a/komorebi/src/window.rs b/komorebi/src/window.rs index 4569d8de..b2055a77 100644 --- a/komorebi/src/window.rs +++ b/komorebi/src/window.rs @@ -58,6 +58,7 @@ use crate::hidden_frame_bottom_right; use crate::macos_api::MacosApi; use crate::reaper; use crate::reaper::ReaperNotification; +use crate::skylight; use crate::window_manager_event::SystemNotification; use crate::window_manager_event::WindowManagerEvent; use crate::window_manager_event_listener; @@ -735,16 +736,23 @@ impl Window { } pub fn focus(&self, mouse_follows_focus: bool) -> Result<(), LibraryError> { - match self.running_application() { - Ok(running_application) => { - running_application.activateWithOptions(NSApplicationActivationOptions::empty()); - } - Err(error) => { - tracing::warn!( - "failed to get running application for {} ({:?}): {error}", - self.application.process_id, - self.application.name() - ); + // Activating the application is turned away by the window server as an expired request once + // the user has typed or clicked since komorebi started, and the application in front stays + // there, so the window server is asked directly first. That is only for as long as it + // works: when it does not, this is the way it always was. + if !skylight::set_front_process(self.application.process_id, self.id) { + match self.running_application() { + Ok(running_application) => { + running_application + .activateWithOptions(NSApplicationActivationOptions::empty()); + } + Err(error) => { + tracing::warn!( + "failed to get running application for {} ({:?}): {error}", + self.application.process_id, + self.application.name() + ); + } } }