Skip to content

Commit

Permalink
feat: add stop_listen for mac
Browse files Browse the repository at this point in the history
  • Loading branch information
HuakunShen committed Jan 4, 2025
1 parent cb9a29e commit e43c6a2
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 7 deletions.
25 changes: 25 additions & 0 deletions examples/simply_listen.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use rdev::{listen, Event, EventType};

fn callback(event: Event) {
match event.event_type {
EventType::KeyPress(_key) | EventType::KeyRelease(_key) => {
println!("User wrote {:?}", event.unicode);
}
_ => (),
}
}

fn main() {
// This will block.
use std::thread;
use std::time::Duration;
let handle = thread::spawn(|| {
if let Err(error) = listen(callback) {
println!("Error: {:?}", error)
}
});
thread::sleep(Duration::from_secs(5));
rdev::stop_listen().unwrap();
let _ = handle.join();
println!("Done");
}
16 changes: 12 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ pub use crate::codes_conv::*;
pub use keycodes::android::{
code_from_key as android_keycode_from_key, key_from_code as android_key_from_code,
};
pub use keycodes::chrome::{
code_from_key as chrome_keycode_from_key, key_from_code as chrome_key_from_code,
};
pub use keycodes::linux::{
code_from_key as linux_keycode_from_key, key_from_code as linux_key_from_code,
};
Expand All @@ -252,14 +255,14 @@ pub use keycodes::windows::{
get_win_key, key_from_code as win_key_from_keycode, key_from_scancode as win_key_from_scancode,
scancode_from_key as win_scancode_from_key,
};
pub use keycodes::chrome::{
code_from_key as chrome_keycode_from_key, key_from_code as chrome_key_from_code,
};

#[cfg(target_os = "macos")]
pub use crate::keycodes::macos::{code_from_key, key_from_code, virtual_keycodes::*};
#[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, simulate as _simulate,
stop_listen as _stop_listen,
};
#[cfg(target_os = "macos")]
pub use crate::macos::{set_is_main_thread, Keyboard, VirtualInput};
#[cfg(target_os = "macos")]
Expand Down Expand Up @@ -312,6 +315,11 @@ where
_listen(callback)
}

#[cfg(target_os = "macos")]
pub fn stop_listen() -> Result<(), ListenError> {
_stop_listen()
}

/// Sending some events
///
/// ```no_run
Expand Down
18 changes: 16 additions & 2 deletions src/macos/listen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ unsafe extern "C" fn raw_callback(
cg_event
}

static mut CUR_LOOP: CFRunLoopSourceRef = std::ptr::null_mut();

pub fn listen<T>(callback: T) -> Result<(), ListenError>
where
T: FnMut(Event) + 'static,
Expand Down Expand Up @@ -59,11 +61,23 @@ where
return Err(ListenError::LoopSourceError);
}

let current_loop = CFRunLoopGetMain();
CFRunLoopAddSource(current_loop, _loop, kCFRunLoopCommonModes);
CUR_LOOP = CFRunLoopGetCurrent() as _;
CFRunLoopAddSource(CUR_LOOP, _loop, kCFRunLoopCommonModes);
// let current_loop = CFRunLoopGetMain();
// CFRunLoopAddSource(current_loop, _loop, kCFRunLoopCommonModes);

CGEventTapEnable(tap, true);
CFRunLoopRun();
}
Ok(())
}

pub fn stop_listen() -> Result<(), ListenError> {
unsafe {
if !CUR_LOOP.is_null() {
CFRunLoopStop(CUR_LOOP);
CUR_LOOP = std::ptr::null_mut();
}
}
Ok(())
}
2 changes: 1 addition & 1 deletion src/macos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub use crate::macos::common::{map_keycode, set_is_main_thread};
pub use crate::macos::display::display_size;
pub use crate::macos::grab::{exit_grab, grab, is_grabbed};
pub use crate::macos::keyboard::Keyboard;
pub use crate::macos::listen::listen;
pub use crate::macos::listen::{listen, stop_listen};
pub use crate::macos::simulate::{
set_keyboard_extra_info, set_mouse_extra_info, simulate, VirtualInput,
};

0 comments on commit e43c6a2

Please sign in to comment.