diff --git a/Cargo.toml b/Cargo.toml index 2b6cc838..32cceafa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,9 @@ keywords = ["input", "mouse", "testing", "keyboard", "automation"] categories = ["development-tools::testing", "api-bindings", "hardware-support"] license = "MIT" +[dependencies] +lazy_static = "1.4.0" + [target.'cfg(target_os = "macos")'.dependencies] cocoa = "0.20" core-graphics = {version = "0.19.0", features = ["highsierra"]} diff --git a/src/lib.rs b/src/lib.rs index 05b411e5..263d1970 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -81,7 +81,7 @@ use crate::macos::{display_size as _display_size, listen as _listen, simulate as mod linux; #[cfg(target_os = "linux")] -use crate::linux::{display_size as _display_size, listen as _listen, simulate as _simulate}; +use crate::linux::{display_size as _display_size, listen as _listen, get_recv as _get_recv, simulate as _simulate}; #[cfg(target_os = "windows")] mod windows; @@ -112,6 +112,11 @@ pub fn listen(callback: Callback) { _listen(callback) } +use std::sync::{Mutex, mpsc::Receiver}; +pub fn get_recv() -> &'static Mutex> { + _get_recv() +} + /// Sending some events /// /// ```no_run diff --git a/src/linux/listen.rs b/src/linux/listen.rs index 1f8bacd7..1b9a3940 100644 --- a/src/linux/listen.rs +++ b/src/linux/listen.rs @@ -11,11 +11,26 @@ use std::time::SystemTime; use x11::xlib; use x11::xrecord; +use std::sync::Mutex; +use std::sync::mpsc::{channel, Sender, Receiver}; +use lazy_static::lazy_static; + fn default_callback(event: Event) { + GLOBAL_CHANNEL.0.lock().unwrap().send(event.clone()); println!("Default : Event {:?}", event); } static mut GLOBAL_CALLBACK: Callback = &default_callback; +lazy_static! { + static ref GLOBAL_CHANNEL: (Mutex>, Mutex>) = { + let (send, recv) = channel(); + (Mutex::new(send), Mutex::new(recv)) + }; +} + +pub fn get_recv() -> &'static Mutex> { + &GLOBAL_CHANNEL.1 +} pub fn listen(callback: Callback) { unsafe { diff --git a/src/linux/mod.rs b/src/linux/mod.rs index 3d7b93fc..4a38557a 100644 --- a/src/linux/mod.rs +++ b/src/linux/mod.rs @@ -10,3 +10,4 @@ mod simulate; pub use crate::linux::display::display_size; pub use crate::linux::listen::listen; pub use crate::linux::simulate::simulate; +pub use crate::linux::listen::get_recv; diff --git a/src/rdev.rs b/src/rdev.rs index 995a9b88..0f1efd99 100644 --- a/src/rdev.rs +++ b/src/rdev.rs @@ -15,7 +15,7 @@ pub struct SimulateError; /// a different value too. /// Careful, on Windows KpReturn does not exist, it' s strictly equivalent to Return, also Keypad keys /// get modified if NumLock is Off and ARE pagedown and so on. -#[derive(Debug)] +#[derive(Debug, Copy, Clone)] pub enum Key { /// Alt key on Linux and Windows (option key on macOS) Alt, @@ -129,7 +129,7 @@ pub enum Key { } /// Standard mouse buttons -#[derive(Debug)] +#[derive(Debug, Copy, Clone)] pub enum Button { Left, Right, @@ -139,7 +139,7 @@ pub enum Button { /// In order to manage different OS, the current EventType choices is a mix&match /// to account for all possible events. -#[derive(Debug)] +#[derive(Debug, Clone)] pub enum EventType { /// The keys correspond to a standard qwerty layout, they don't correspond /// To the actual letter a user would use, that requires some layout logic to be added. @@ -168,7 +168,7 @@ pub enum EventType { /// on the OS layout and keyboard state machinery. /// Caveat: Dead keys don't function on Linux(X11) yet. You will receive None for /// a dead key, and the raw letter instead of accentuated letter instead. -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct Event { pub time: SystemTime, pub name: Option,