Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add version of listen which doesn't run new CFRunLoop #76

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 57 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,10 @@ mod macos;
#[cfg(target_os = "macos")]
pub use crate::macos::Keyboard;
#[cfg(target_os = "macos")]
use crate::macos::{display_size as _display_size, listen as _listen, simulate as _simulate};
use crate::macos::{
display_size as _display_size, listen as _listen, listen_non_blocking as _listen_non_blocking,
simulate as _simulate,
};

#[cfg(target_os = "linux")]
mod linux;
Expand Down Expand Up @@ -272,6 +275,59 @@ where
_listen(callback)
}

/// Listening to global events. Caveat: On MacOS, you require the listen
/// loop needs to be the primary app (no fork before) and need to have accessibility
/// settings enabled.
///
/// ```no_run
/// use rdev::{listen_non_blocking, Event};
/// use cacao::{
/// macos::{
/// menu::{Menu, MenuItem},
/// window::Window,
/// App, AppDelegate,
/// },
/// notification_center::Dispatcher,
/// view::View,
/// };
///
/// #[derive(Default)]
/// pub struct BaseApp {
/// pub window: Window,
/// }
///
/// fn callback(event: Event) {
/// println!("My callback {:?}", event);
/// match event.name{
/// Some(string) => println!("User wrote {:?}", string),
/// None => ()
/// }
/// }
///
/// impl AppDelegate for BaseApp {
/// fn did_finish_launching(&self) {
/// self.window.set_title("RDev");
/// self.window.show();
///
/// // This will not block
/// if let Err(error) = listen(callback) {
/// println!("Error: {:?}", error)
/// }
/// }
///
/// fn should_terminate_after_last_window_closed(&self) -> bool {
/// false
/// }
/// }
/// ```
#[cfg(target_os = "macos")]
pub fn listen_non_blocking<T>(callback: T) -> Result<(), ListenError>
where
T: FnMut(Event) + 'static,
{
_listen_non_blocking(callback)
}

/// Sending some events
///
/// ```no_run
Expand Down
33 changes: 33 additions & 0 deletions src/macos/listen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,36 @@ where
}
Ok(())
}


#[link(name = "Cocoa", kind = "framework")]
pub fn listen_non_blocking<T>(callback: T) -> Result<(), ListenError>
where
T: FnMut(Event) + 'static,
{
unsafe {
GLOBAL_CALLBACK = Some(Box::new(callback));
let _pool = NSAutoreleasePool::new(nil);
let tap = CGEventTapCreate(
CGEventTapLocation::HID, // HID, Session, AnnotatedSession,
kCGHeadInsertEventTap,
CGEventTapOption::ListenOnly,
kCGEventMaskForAllEvents,
raw_callback,
nil,
);
if tap.is_null() {
return Err(ListenError::EventTapError);
}
let _loop = CFMachPortCreateRunLoopSource(nil, tap, 0);
if _loop.is_null() {
return Err(ListenError::LoopSourceError);
}

let current_loop = CFRunLoopGetCurrent();
CFRunLoopAddSource(current_loop, _loop, kCFRunLoopCommonModes);

CGEventTapEnable(tap, true);
}
Ok(())
}
1 change: 1 addition & 0 deletions src/macos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ pub use crate::macos::display::display_size;
pub use crate::macos::grab::grab;
pub use crate::macos::keyboard::Keyboard;
pub use crate::macos::listen::listen;
pub use crate::macos::listen::listen_non_blocking;
pub use crate::macos::simulate::simulate;