mirror of
https://github.com/LGUG2Z/komorebi.git
synced 2026-11-16 05:41:16 +01:00
fix(input_event_listener): turn the event tap back on and make it again
The tap that sees clicks and key presses for the reaper and for the end of a drag was never looked after. The system turns a tap off when it is too slow to answer and tells the callback with an event that is not an event, which was treated as a click, and a tap that did not survive sleep was never missed. Either way komorebi stopped noticing windows that close without a notification and drags that end without one, with nothing in the log. The callback now only says that it was turned off, since anything slow done there is what gets a tap turned off, and the main loop turns it on again at once. Every five seconds it also asks whether the tap is there and on, turns it on or makes it again as the answer says, and tries to make one if there never was one, since the permission may have been granted since. Not being able to make the tap is logged, once. The decision is a function of two booleans with tests. That the system really does disable the tap, and what sleep does to it, is not something that was seen on a device.
This commit is contained in:
1 parent
e23dc085f9
commit
767af481fd
2 files changed
+128
-2
No files matched your search
@@ -18,6 +18,43 @@ use objc2_core_graphics::CGEventTapProxy;
|
||||
use objc2_core_graphics::CGEventType;
|
||||
use std::ffi::c_void;
|
||||
use std::ptr::NonNull;
|
||||
use std::sync::atomic::AtomicBool;
|
||||
use std::sync::atomic::Ordering;
|
||||
|
||||
/// Set by the callback when the system has turned the tap off, which it does to a tap that is
|
||||
/// too slow to answer, or when the user asks it to. The callback cannot do anything about it from
|
||||
/// where it is, so it says so here for the main loop, which can.
|
||||
static TAP_DISABLED: AtomicBool = AtomicBool::new(false);
|
||||
|
||||
/// Whether the tap not being created has been said, so that it is said once and not every time
|
||||
/// that another try is made.
|
||||
static CREATION_FAILURE_LOGGED: AtomicBool = AtomicBool::new(false);
|
||||
|
||||
/// Whether the system has turned the tap off since this was last asked.
|
||||
pub fn take_disabled() -> bool {
|
||||
TAP_DISABLED.swap(false, Ordering::Relaxed)
|
||||
}
|
||||
|
||||
/// What to do about the tap, from what is known of it.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum TapAction {
|
||||
/// It is there and on.
|
||||
Nothing,
|
||||
/// It is there and off, which is something that the system does, and it only needs turning on.
|
||||
Enable,
|
||||
/// It is gone, which happens across sleep without a word, and has to be made again.
|
||||
Rebuild,
|
||||
}
|
||||
|
||||
/// What to do about a tap that is `valid` or not and `enabled` or not.
|
||||
#[must_use]
|
||||
pub fn tap_action(valid: bool, enabled: bool) -> TapAction {
|
||||
match (valid, enabled) {
|
||||
(false, _) => TapAction::Rebuild,
|
||||
(true, false) => TapAction::Enable,
|
||||
(true, true) => TapAction::Nothing,
|
||||
}
|
||||
}
|
||||
|
||||
pub struct InputEventListener {
|
||||
port: CFRetained<CFMachPort>,
|
||||
@@ -29,6 +66,17 @@ extern "C-unwind" fn callback(
|
||||
mut event_ref: NonNull<CGEvent>,
|
||||
_listener: *mut c_void,
|
||||
) -> *mut CGEvent {
|
||||
// these are not events that anyone did anything for, but the system saying that it has turned
|
||||
// the tap off, and what comes back from the callback is ignored. Anything slow done here is
|
||||
// what gets a tap turned off, so all that is done is to say so
|
||||
if event_type == CGEventType::TapDisabledByTimeout
|
||||
|| event_type == CGEventType::TapDisabledByUserInput
|
||||
{
|
||||
TAP_DISABLED.store(true, Ordering::Relaxed);
|
||||
|
||||
return event_ref.as_ptr();
|
||||
}
|
||||
|
||||
// this should cover both clicking the close button and closing out something
|
||||
// by using cmd+w or cmd+q since some apps don't send events on close
|
||||
reaper::send_notification(ReaperNotification::MouseUpKeyUp);
|
||||
@@ -108,8 +156,38 @@ impl InputEventListener {
|
||||
}
|
||||
}
|
||||
|
||||
if port.is_none() && !CREATION_FAILURE_LOGGED.swap(true, Ordering::Relaxed) {
|
||||
tracing::error!(
|
||||
"could not create the event tap, so clicks and key presses will not be seen: is \
|
||||
the Accessibility permission granted? It is tried again every few seconds"
|
||||
);
|
||||
}
|
||||
|
||||
port.map(|port| Self { port })
|
||||
}
|
||||
|
||||
/// Look after the tap: turn it on if it has been turned off, and make it again if it has gone.
|
||||
///
|
||||
/// The system turns a tap off when it is too slow, and a tap does not survive sleep without
|
||||
/// telling anyone, so this is for the main loop to do now and again, and whenever
|
||||
/// [`take_disabled`] says that it has been turned off.
|
||||
pub fn ensure_enabled(&mut self, run_loop: &CFRunLoop) {
|
||||
match tap_action(self.port.is_valid(), CGEvent::tap_is_enabled(&self.port)) {
|
||||
TapAction::Nothing => {}
|
||||
TapAction::Enable => {
|
||||
tracing::warn!("the event tap was turned off, turning it on again");
|
||||
CGEvent::tap_enable(&self.port, true);
|
||||
}
|
||||
TapAction::Rebuild => {
|
||||
tracing::warn!("the event tap is gone, making it again");
|
||||
|
||||
if let Some(listener) = Self::init(run_loop) {
|
||||
// the old one is taken down as it goes
|
||||
*self = listener;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for InputEventListener {
|
||||
@@ -118,3 +196,32 @@ impl Drop for InputEventListener {
|
||||
CGEvent::tap_enable(&self.port, false);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn a_tap_that_is_there_and_on_is_left_alone() {
|
||||
assert_eq!(tap_action(true, true), TapAction::Nothing);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_tap_that_was_turned_off_is_turned_on() {
|
||||
assert_eq!(tap_action(true, false), TapAction::Enable);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_tap_that_is_gone_is_made_again_whatever_it_says_about_being_on() {
|
||||
assert_eq!(tap_action(false, false), TapAction::Rebuild);
|
||||
assert_eq!(tap_action(false, true), TapAction::Rebuild);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn being_turned_off_is_noticed_once() {
|
||||
TAP_DISABLED.store(true, Ordering::Relaxed);
|
||||
|
||||
assert!(take_disabled());
|
||||
assert!(!take_disabled());
|
||||
}
|
||||
}
|
||||
+21
-2
@@ -10,6 +10,7 @@ use komorebi::HOME_DIR;
|
||||
use komorebi::border_manager;
|
||||
use komorebi::core::pathext::replace_env_in_path;
|
||||
use komorebi::display_reconfiguration_listener::DisplayReconfigurationListener;
|
||||
use komorebi::input_event_listener;
|
||||
use komorebi::input_event_listener::InputEventListener;
|
||||
use komorebi::monitor_reconciliator;
|
||||
use komorebi::notification_center_listener::NotificationCenterListener;
|
||||
@@ -41,12 +42,17 @@ use std::sync::Arc;
|
||||
use std::sync::atomic::AtomicBool;
|
||||
use std::sync::atomic::Ordering;
|
||||
use std::sync::mpsc;
|
||||
use std::time::Duration;
|
||||
use std::time::Instant;
|
||||
use sysinfo::Process;
|
||||
use sysinfo::ProcessesToUpdate;
|
||||
use tracing_appender::non_blocking::WorkerGuard;
|
||||
use tracing_subscriber::EnvFilter;
|
||||
use tracing_subscriber::layer::SubscriberExt;
|
||||
|
||||
/// How often the event tap is looked at when the system has not said that it turned it off
|
||||
const TAP_CHECK_INTERVAL: Duration = Duration::from_secs(5);
|
||||
|
||||
fn check_permissions() -> eyre::Result<()> {
|
||||
unsafe {
|
||||
// check for screen capture access - this is needed to read window titles
|
||||
@@ -226,7 +232,8 @@ fn main() -> eyre::Result<()> {
|
||||
let _display_reconfiguration_listener = DisplayReconfigurationListener::init();
|
||||
|
||||
let run_loop = CFRunLoop::current().ok_or_eyre("couldn't get CFRunLoop::current")?;
|
||||
let _input_listener = InputEventListener::init(&run_loop);
|
||||
let mut input_listener = InputEventListener::init(&run_loop);
|
||||
let mut last_tap_check = Instant::now();
|
||||
|
||||
#[cfg(feature = "deadlock_detection")]
|
||||
detect_deadlocks();
|
||||
@@ -271,7 +278,7 @@ fn main() -> eyre::Result<()> {
|
||||
|
||||
wm.lock().retile_all(false)?;
|
||||
|
||||
border_manager::listen_for_notifications(wm.clone(), CoreFoundationRunLoop(run_loop));
|
||||
border_manager::listen_for_notifications(wm.clone(), CoreFoundationRunLoop(run_loop.clone()));
|
||||
theme_manager::listen_for_notifications();
|
||||
monitor_reconciliator::listen_for_notifications(wm.clone())?;
|
||||
reaper::listen_for_notifications(wm.clone());
|
||||
@@ -308,6 +315,18 @@ fn main() -> eyre::Result<()> {
|
||||
break;
|
||||
}
|
||||
|
||||
// the tap is looked after when the system says that it turned it off, and every so often
|
||||
// regardless, since there is nobody to say when it is lost across sleep
|
||||
if input_event_listener::take_disabled() || last_tap_check.elapsed() >= TAP_CHECK_INTERVAL {
|
||||
last_tap_check = Instant::now();
|
||||
|
||||
match input_listener.as_mut() {
|
||||
Some(listener) => listener.ensure_enabled(&run_loop),
|
||||
// there may be permission now that there was not
|
||||
None => input_listener = InputEventListener::init(&run_loop),
|
||||
}
|
||||
}
|
||||
|
||||
autoreleasepool(|_| {
|
||||
// process NSApplication events explicitly - this is what makes display
|
||||
// reconfiguration callbacks work on tahoe
|
||||
|
||||
Reference in new issue
Block a user