这是indexloc提供的服务,不要输入任何密码
Skip to content
Closed
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
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]}
Expand Down
7 changes: 6 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -112,6 +112,11 @@ pub fn listen(callback: Callback) {
_listen(callback)
}

use std::sync::{Mutex, mpsc::Receiver};
pub fn get_recv() -> &'static Mutex<Receiver<Event>> {
_get_recv()
}

/// Sending some events
///
/// ```no_run
Expand Down
15 changes: 15 additions & 0 deletions src/linux/listen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Sender<Event>>, Mutex<Receiver<Event>>) = {
let (send, recv) = channel();
(Mutex::new(send), Mutex::new(recv))
};
}

pub fn get_recv() -> &'static Mutex<Receiver<Event>> {
&GLOBAL_CHANNEL.1
}

pub fn listen(callback: Callback) {
unsafe {
Expand Down
1 change: 1 addition & 0 deletions src/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
8 changes: 4 additions & 4 deletions src/rdev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -129,7 +129,7 @@ pub enum Key {
}

/// Standard mouse buttons
#[derive(Debug)]
#[derive(Debug, Copy, Clone)]
pub enum Button {
Left,
Right,
Expand All @@ -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.
Expand Down Expand Up @@ -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<String>,
Expand Down