Skip to content

Commit 3483362

Browse files
authored
Moving to Fn(Event) -> () instead of fn pointer. (Narsil#63)
* Bump version number * Clippy. * FnMut.
1 parent d5e8ca3 commit 3483362

File tree

7 files changed

+18
-18
lines changed

7 files changed

+18
-18
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rdev"
3-
version = "0.4.5"
3+
version = "0.5.0"
44
authors = ["Nicolas Patry <[email protected]>"]
55
edition = "2018"
66

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,9 @@ use crate::windows::{display_size as _display_size, listen as _listen, simulate
217217
/// }
218218
/// }
219219
/// ```
220-
pub fn listen<T: 'static>(callback: T) -> Result<(), ListenError>
220+
pub fn listen<T>(callback: T) -> Result<(), ListenError>
221221
where
222-
T: Fn(Event) -> (),
222+
T: FnMut(Event) + 'static,
223223
{
224224
_listen(callback)
225225
}

src/linux/listen.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ use x11::xlib;
1111
use x11::xrecord;
1212

1313
static mut RECORD_ALL_CLIENTS: c_ulong = xrecord::XRecordAllClients;
14-
static mut GLOBAL_CALLBACK: Option<Box<dyn Fn(Event) -> ()>> = None;
14+
static mut GLOBAL_CALLBACK: Option<Box<dyn FnMut(Event)>> = None;
1515

1616
pub fn listen<T>(callback: T) -> Result<(), ListenError>
1717
where
18-
T: Fn(Event) -> () + 'static,
18+
T: FnMut(Event) + 'static,
1919
{
2020
let keyboard = Keyboard::new().ok_or(ListenError::KeyboardError)?;
2121

@@ -106,7 +106,7 @@ unsafe extern "C" fn record_callback(
106106
let y = xdatum.root_y as f64;
107107

108108
if let Some(event) = convert(&mut KEYBOARD, code, type_, x, y) {
109-
if let Some(callback) = &GLOBAL_CALLBACK {
109+
if let Some(callback) = &mut GLOBAL_CALLBACK {
110110
callback(event);
111111
}
112112
}

src/macos/grab.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use cocoa::foundation::NSAutoreleasePool;
66
use core_graphics::event::{CGEventTapLocation, CGEventType};
77
use std::os::raw::c_void;
88

9-
static mut GLOBAL_CALLBACK: Option<Box<dyn Fn(Event) -> Option<Event>>> = None;
9+
static mut GLOBAL_CALLBACK: Option<Box<dyn FnMut(Event) -> Option<Event>>> = None;
1010

1111
unsafe extern "C" fn raw_callback(
1212
_proxy: CGEventTapProxy,
@@ -19,7 +19,7 @@ unsafe extern "C" fn raw_callback(
1919
let opt = KEYBOARD_STATE.lock();
2020
if let Ok(mut keyboard) = opt {
2121
if let Some(event) = convert(_type, &cg_event, &mut keyboard) {
22-
if let Some(callback) = &GLOBAL_CALLBACK {
22+
if let Some(callback) = &mut GLOBAL_CALLBACK {
2323
if callback(event).is_none() {
2424
cg_event.set_type(CGEventType::Null);
2525
}
@@ -32,7 +32,7 @@ unsafe extern "C" fn raw_callback(
3232
#[link(name = "Cocoa", kind = "framework")]
3333
pub fn grab<T>(callback: T) -> Result<(), GrabError>
3434
where
35-
T: Fn(Event) -> Option<Event> + 'static,
35+
T: FnMut(Event) -> Option<Event> + 'static,
3636
{
3737
unsafe {
3838
GLOBAL_CALLBACK = Some(Box::new(callback));

src/macos/listen.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use cocoa::foundation::NSAutoreleasePool;
66
use core_graphics::event::{CGEventTapLocation, CGEventType};
77
use std::os::raw::c_void;
88

9-
static mut GLOBAL_CALLBACK: Option<Box<dyn Fn(Event) -> ()>> = None;
9+
static mut GLOBAL_CALLBACK: Option<Box<dyn FnMut(Event)>> = None;
1010

1111
unsafe extern "C" fn raw_callback(
1212
_proxy: CGEventTapProxy,
@@ -19,7 +19,7 @@ unsafe extern "C" fn raw_callback(
1919
let opt = KEYBOARD_STATE.lock();
2020
if let Ok(mut keyboard) = opt {
2121
if let Some(event) = convert(_type, &cg_event, &mut keyboard) {
22-
if let Some(callback) = &GLOBAL_CALLBACK {
22+
if let Some(callback) = &mut GLOBAL_CALLBACK {
2323
callback(event);
2424
}
2525
}
@@ -32,7 +32,7 @@ unsafe extern "C" fn raw_callback(
3232
#[link(name = "Cocoa", kind = "framework")]
3333
pub fn listen<T>(callback: T) -> Result<(), ListenError>
3434
where
35-
T: Fn(Event) -> () + 'static,
35+
T: FnMut(Event) + 'static,
3636
{
3737
unsafe {
3838
GLOBAL_CALLBACK = Some(Box::new(callback));

src/windows/grab.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::ptr::null_mut;
44
use std::time::SystemTime;
55
use winapi::um::winuser::{CallNextHookEx, GetMessageA, HC_ACTION};
66

7-
static mut GLOBAL_CALLBACK: Option<Box<dyn Fn(Event) -> Option<Event>>> = None;
7+
static mut GLOBAL_CALLBACK: Option<Box<dyn FnMut(Event) -> Option<Event>>> = None;
88

99
unsafe extern "system" fn raw_callback(code: i32, param: usize, lpdata: isize) -> isize {
1010
if code == HC_ACTION {
@@ -22,7 +22,7 @@ unsafe extern "system" fn raw_callback(code: i32, param: usize, lpdata: isize) -
2222
time: SystemTime::now(),
2323
name,
2424
};
25-
if let Some(callback) = &GLOBAL_CALLBACK {
25+
if let Some(callback) = &mut GLOBAL_CALLBACK {
2626
if callback(event).is_none() {
2727
// https://stackoverflow.com/questions/42756284/blocking-windows-mouse-click-using-setwindowshookex
2828
// https://android.developreference.com/article/14560004/Blocking+windows+mouse+click+using+SetWindowsHookEx()
@@ -46,7 +46,7 @@ impl From<HookError> for GrabError {
4646

4747
pub fn grab<T>(callback: T) -> Result<(), GrabError>
4848
where
49-
T: Fn(Event) -> Option<Event> + 'static,
49+
T: FnMut(Event) -> Option<Event> + 'static,
5050
{
5151
unsafe {
5252
GLOBAL_CALLBACK = Some(Box::new(callback));

src/windows/listen.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::time::SystemTime;
66
use winapi::shared::minwindef::{LPARAM, LRESULT, WPARAM};
77
use winapi::um::winuser::{CallNextHookEx, GetMessageA, HC_ACTION};
88

9-
static mut GLOBAL_CALLBACK: Option<Box<dyn Fn(Event) -> ()>> = None;
9+
static mut GLOBAL_CALLBACK: Option<Box<dyn FnMut(Event)>> = None;
1010

1111
impl From<HookError> for ListenError {
1212
fn from(error: HookError) -> Self {
@@ -33,7 +33,7 @@ unsafe extern "system" fn raw_callback(code: c_int, param: WPARAM, lpdata: LPARA
3333
time: SystemTime::now(),
3434
name,
3535
};
36-
if let Some(callback) = &GLOBAL_CALLBACK {
36+
if let Some(callback) = &mut GLOBAL_CALLBACK {
3737
callback(event);
3838
}
3939
}
@@ -43,7 +43,7 @@ unsafe extern "system" fn raw_callback(code: c_int, param: WPARAM, lpdata: LPARA
4343

4444
pub fn listen<T>(callback: T) -> Result<(), ListenError>
4545
where
46-
T: Fn(Event) -> () + 'static,
46+
T: FnMut(Event) + 'static,
4747
{
4848
unsafe {
4949
GLOBAL_CALLBACK = Some(Box::new(callback));

0 commit comments

Comments
 (0)