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

Stop listen #139

Open
wants to merge 6 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
14 changes: 12 additions & 2 deletions 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, simulate as _simulate,
stop_listen as _stop_listen,
};

#[cfg(target_os = "linux")]
mod linux;
Expand All @@ -242,7 +245,10 @@ mod windows;
#[cfg(target_os = "windows")]
pub use crate::windows::Keyboard;
#[cfg(target_os = "windows")]
use crate::windows::{display_size as _display_size, listen as _listen, simulate as _simulate};
use crate::windows::{
display_size as _display_size, listen as _listen, simulate as _simulate,
stop_listen as _stop_listen,
};

/// 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
Expand Down Expand Up @@ -272,6 +278,10 @@ where
_listen(callback)
}

pub fn stop_listen() {
_stop_listen();
}

/// Sending some events
///
/// ```no_run
Expand Down
2 changes: 1 addition & 1 deletion src/macos/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ extern "C" {
pub fn CFRunLoopGetCurrent() -> CFRunLoopRef;
pub fn CGEventTapEnable(tap: CFMachPortRef, enable: bool);
pub fn CFRunLoopRun();

pub fn CFRunLoopStop(rl: CFRunLoopRef);
pub static kCFRunLoopCommonModes: CFRunLoopMode;

}
Expand Down
17 changes: 16 additions & 1 deletion src/macos/listen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ unsafe extern "C" fn raw_callback(
let opt = KEYBOARD_STATE.lock();
if let Ok(mut keyboard) = opt {
if let Some(event) = convert(_type, &cg_event, &mut keyboard) {
if let Some(callback) = &mut GLOBAL_CALLBACK {
if let Some(ref mut callback) = GLOBAL_CALLBACK {
callback(event);
}
}
Expand Down Expand Up @@ -59,7 +59,22 @@ where
CFRunLoopAddSource(current_loop, _loop, kCFRunLoopCommonModes);

CGEventTapEnable(tap, true);
STOP_LOOP = Some(Box::new(move || {
CFRunLoopStop(current_loop);
}));
CFRunLoopRun();
}
Ok(())
}

pub fn stop_listen() {
unsafe {
if let Some(stop_loop) = STOP_LOOP.as_ref() {
stop_loop();
STOP_LOOP = None;
}
}
}

type DynFn = dyn Fn() + 'static;
pub static mut STOP_LOOP: Option<Box<DynFn>> = None;
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::stop_listen;
pub use crate::macos::simulate::simulate;
30 changes: 27 additions & 3 deletions src/windows/listen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use crate::rdev::{Event, EventType, ListenError};
use crate::windows::common::{convert, set_key_hook, set_mouse_hook, HookError, HOOK, KEYBOARD};
use std::os::raw::c_int;
use std::ptr::null_mut;
use std::sync::mpsc;
use std::time::SystemTime;
use winapi::shared::minwindef::{LPARAM, LRESULT, WPARAM};
use winapi::um::winuser::{CallNextHookEx, GetMessageA, HC_ACTION};
use winapi::um::winuser::{CallNextHookEx, PeekMessageA, HC_ACTION};

static mut GLOBAL_CALLBACK: Option<Box<dyn FnMut(Event)>> = None;

Expand Down Expand Up @@ -33,7 +34,7 @@ unsafe extern "system" fn raw_callback(code: c_int, param: WPARAM, lpdata: LPARA
time: SystemTime::now(),
name,
};
if let Some(callback) = &mut GLOBAL_CALLBACK {
if let Some(ref mut callback) = GLOBAL_CALLBACK {
callback(event);
}
}
Expand All @@ -50,7 +51,30 @@ where
set_key_hook(raw_callback)?;
set_mouse_hook(raw_callback)?;

GetMessageA(null_mut(), null_mut(), 0, 0);
let (sender, receiver) = mpsc::channel();
STOP_LOOP = Some(Box::new(move || {
sender.send(true).unwrap();
}));
loop {
if let Ok(stop_listen) = receiver.try_recv() {
if stop_listen {
break;
}
}
PeekMessageA(null_mut(), null_mut(), 0, 0, 0);
}
}
Ok(())
}

pub fn stop_listen() {
unsafe {
if let Some(stop_loop) = STOP_LOOP.as_ref() {
stop_loop();
STOP_LOOP = None;
}
}
}

type DynFn = dyn Fn() + 'static;
pub static mut STOP_LOOP: Option<Box<DynFn>> = None;
1 change: 1 addition & 0 deletions src/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ pub use crate::windows::display::display_size;
pub use crate::windows::grab::grab;
pub use crate::windows::keyboard::Keyboard;
pub use crate::windows::listen::listen;
pub use crate::windows::listen::stop_listen;
pub use crate::windows::simulate::simulate;
23 changes: 23 additions & 0 deletions tests/stop_listen.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use rdev::{listen, stop_listen};
use serial_test::serial;
use std::{
thread::{self, spawn},
time::Duration,
};
#[test]
#[serial]
fn test_stop() {
eprintln!("hello");
spawn(|| {
if let Err(error) = listen(|event| {
println!("My callback {:?}", event);
}) {
println!("Error: {:?}", error)
}
});
thread::sleep(Duration::from_secs(5));
spawn(|| {
stop_listen();
});
thread::sleep(Duration::from_secs(5));
}