这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ jobs:
- name: Run tests
run: ${{matrix.test}}
- name: Linter
run: cargo clippy --verbose
run: cargo clippy --verbose -- -Dwarnings
44 changes: 18 additions & 26 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@
//! ```no_run
//! use rdev::{listen, Event};
//!
//! fn main() {
//! // This will block.
//! if let Err(error) = listen(callback) {
//! println!("Error: {:?}", error)
//! }
//! // This will block.
//! if let Err(error) = listen(callback) {
//! println!("Error: {:?}", error)
//! }
//!
//! fn callback(event: Event) {
Expand Down Expand Up @@ -47,30 +45,26 @@
//! thread::sleep(delay);
//! }
//!
//! fn main() {
//! send(&EventType::KeyPress(Key::KeyS));
//! send(&EventType::KeyRelease(Key::KeyS));
//! send(&EventType::KeyPress(Key::KeyS));
//! send(&EventType::KeyRelease(Key::KeyS));
//!
//! send(&EventType::MouseMove { x: 0.0, y: 0.0 });
//! send(&EventType::MouseMove { x: 400.0, y: 400.0 });
//! send(&EventType::ButtonPress(Button::Left));
//! send(&EventType::ButtonRelease(Button::Right));
//! send(&EventType::Wheel {
//! delta_x: 0,
//! delta_y: 1,
//! });
//! }
//! send(&EventType::MouseMove { x: 0.0, y: 0.0 });
//! send(&EventType::MouseMove { x: 400.0, y: 400.0 });
//! send(&EventType::ButtonPress(Button::Left));
//! send(&EventType::ButtonRelease(Button::Right));
//! send(&EventType::Wheel {
//! delta_x: 0,
//! delta_y: 1,
//! });
//! ```
//! Getting the main screen size
//!
//! ```no_run
//! use rdev::{display_size};
//!
//! fn main() {
//! let (w, h) = display_size();
//! assert!(w > 0);
//! assert!(h > 0);
//! }
//! let (w, h) = display_size();
//! assert!(w > 0);
//! assert!(h > 0);
//! ```
mod rdev;
pub use crate::rdev::{Button, Callback, Event, EventType, Key, ListenError, SimulateError};
Expand Down Expand Up @@ -159,10 +153,8 @@ pub fn simulate(event_type: &EventType) -> Result<(), SimulateError> {
/// ```no_run
/// use rdev::{display_size};
///
/// fn main() {
/// let (w, h )= display_size();
/// println!("My screen size : {:?}x{:?}", w, h);
/// }
/// let (w, h )= display_size();
/// println!("My screen size : {:?}x{:?}", w, h);
/// ```
pub fn display_size() -> (u64, u64) {
_display_size()
Expand Down
6 changes: 3 additions & 3 deletions src/linux/keycodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ const KP8: u32 = 80;
const KP9: u32 = 81;
const KP_DELETE: u32 = 91;

pub fn code_from_key(key: &Key) -> Option<u32> {
pub fn code_from_key(key: Key) -> Option<u32> {
match key {
Key::Alt => Some(ALT),
Key::AltGr => Some(ALT_GR),
Expand Down Expand Up @@ -209,7 +209,7 @@ pub fn code_from_key(key: &Key) -> Option<u32> {
Key::Kp8 => Some(KP8),
Key::Kp9 => Some(KP9),
Key::KpDelete => Some(KP_DELETE),
Key::Unknown(code) => Some(*code),
Key::Unknown(code) => Some(code),
_ => None,
}
}
Expand Down Expand Up @@ -330,7 +330,7 @@ mod test {
fn test_reversible() {
for code in 0..65636 {
let key = key_from_code(code);
if let Some(code2) = code_from_key(&key) {
if let Some(code2) = code_from_key(key) {
assert_eq!(code, code2)
} else {
assert!(false, "We could not convert back code: {:?}", code);
Expand Down
4 changes: 2 additions & 2 deletions src/linux/simulate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ static FALSE: i32 = 0;
unsafe fn send_native(event_type: &EventType, display: *mut xlib::Display) -> Result<(), ()> {
match event_type {
EventType::KeyPress(key) => {
if let Some(code) = code_from_key(key) {
if let Some(code) = code_from_key(*key) {
xtest::XTestFakeKeyEvent(display, code, TRUE, 0);
return Ok(());
}
Err(())
}
EventType::KeyRelease(key) => {
if let Some(code) = code_from_key(key) {
if let Some(code) = code_from_key(*key) {
xtest::XTestFakeKeyEvent(display, code, FALSE, 0);
return Ok(());
}
Expand Down
2 changes: 1 addition & 1 deletion src/macos/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ use core_graphics::display::CGDisplay;

pub fn display_size() -> (u64, u64) {
let main = CGDisplay::main();
return (main.pixels_wide(), main.pixels_high());
(main.pixels_wide(), main.pixels_high())
}
10 changes: 5 additions & 5 deletions src/macos/keyboard_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ extern "C" {
dead_key_state: *mut u32,
max_length: UniCharCount,
actual_length: *mut UniCharCount,
unicode_string: *mut [u16],
unicode_string: *mut c_void,
) -> OSStatus;
fn LMGetKbdType() -> u32;
static kTISPropertyUnicodeKeyLayoutData: *mut c_void;
Expand Down Expand Up @@ -71,10 +71,10 @@ impl KeyboardState {
modifier_state,
kb_type,
kUCKeyTranslateDeadKeysBit,
&mut self.dead_state, // deadKeyState
4, // max string length
&mut length as *mut UniCharCount, // actual string length
&mut buff as *mut [UniChar], // unicode string
&mut self.dead_state, // deadKeyState
4, // max string length
&mut length as *mut UniCharCount, // actual string length
&mut buff as *mut _ as *mut c_void, // unicode string
);
CFRelease(keyboard);

Expand Down
6 changes: 3 additions & 3 deletions src/macos/keycodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ const COMMA: u32 = 43;
const DOT: u32 = 47;
const SLASH: u32 = 44;

pub fn code_from_key(key: &Key) -> Option<u32> {
pub fn code_from_key(key: Key) -> Option<u32> {
match key {
Key::Alt => Some(ALT),
Key::AltGr => Some(ALT_GR),
Expand Down Expand Up @@ -160,7 +160,7 @@ pub fn code_from_key(key: &Key) -> Option<u32> {
Key::Dot => Some(DOT),
Key::Slash => Some(SLASH),
Key::Function => Some(FUNCTION),
Key::Unknown(code) => Some(*code),
Key::Unknown(code) => Some(code),
_ => None,
}
}
Expand Down Expand Up @@ -255,7 +255,7 @@ mod test {
fn test_reversible() {
for code in 0..65636 {
let key = key_from_code(code);
if let Some(code2) = code_from_key(&key) {
if let Some(code2) = code_from_key(key) {
assert_eq!(code, code2)
} else {
assert!(false, "We could not convert back code: {:?}", code);
Expand Down
15 changes: 11 additions & 4 deletions src/macos/listen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ type CFRunLoopRef = id;
type CFRunLoopMode = id;
type CGEventTapProxy = id;

type CGEventRef = CGEvent;

// https://developer.apple.com/documentation/coregraphics/cgeventtapplacement?language=objc
type CGEventTapPlacement = u32;
#[allow(non_upper_case_globals)]
Expand Down Expand Up @@ -44,6 +46,7 @@ pub const kCGEventMaskForAllEvents: u64 = (1 << CGEventType::LeftMouseDown as u6
#[cfg(target_os = "macos")]
#[link(name = "Cocoa", kind = "framework")]
extern "C" {
#[allow(improper_ctypes)]
fn CGEventTapCreate(
tap: CGEventTapLocation,
place: CGEventTapPlacement,
Expand All @@ -68,9 +71,9 @@ extern "C" {
type QCallback = unsafe extern "C" fn(
proxy: CGEventTapProxy,
_type: CGEventType,
cg_event: CGEvent,
cg_event: CGEventRef,
user_info: *mut c_void,
) -> CGEvent;
) -> CGEventRef;

fn default_callback(event: Event) {
println!("Default {:?}", event)
Expand Down Expand Up @@ -146,12 +149,16 @@ unsafe fn convert(
unsafe extern "C" fn raw_callback(
_proxy: CGEventTapProxy,
_type: CGEventType,
cg_event: CGEvent,
cg_event: CGEventRef,
_user_info: *mut c_void,
) -> CGEvent {
) -> CGEventRef {
// println!("Event ref {:?}", cg_event_ptr);
// let cg_event: CGEvent = transmute_copy::<*mut c_void, CGEvent>(&cg_event_ptr);
if let Some(event) = convert(_type, &cg_event, &mut KEYBOARD_STATE) {
GLOBAL_CALLBACK(event);
}
// println!("Event ref END {:?}", cg_event_ptr);
// cg_event_ptr
cg_event
}

Expand Down
4 changes: 2 additions & 2 deletions src/macos/simulate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ unsafe fn convert_native_with_source(
) -> Result<CGEvent, ()> {
match event_type {
EventType::KeyPress(key) => {
if let Some(code) = code_from_key(key) {
if let Some(code) = code_from_key(*key) {
return CGEvent::new_keyboard_event(source, code as CGKeyCode, true);
}
Err(())
}
EventType::KeyRelease(key) => {
if let Some(code) = code_from_key(key) {
if let Some(code) = code_from_key(*key) {
return CGEvent::new_keyboard_event(source, code as CGKeyCode, false);
}
Err(())
Expand Down
6 changes: 3 additions & 3 deletions src/windows/keycodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ const KP8: u16 = 104;
const KP9: u16 = 105;
const KP_DELETE: u16 = 110;

pub fn code_from_key(key: &Key) -> Option<u16> {
pub fn code_from_key(key: Key) -> Option<u16> {
match key {
Key::Alt => Some(ALT),
Key::AltGr => Some(ALT_GR),
Expand Down Expand Up @@ -211,7 +211,7 @@ pub fn code_from_key(key: &Key) -> Option<u16> {
Key::Kp8 => Some(KP8),
Key::Kp9 => Some(KP9),
Key::KpDelete => Some(KP_DELETE),
Key::Unknown(code) => Some(*code as u16),
Key::Unknown(code) => Some(code as u16),
_ => None,
}
}
Expand Down Expand Up @@ -332,7 +332,7 @@ mod test {
fn test_reversible() {
for code in 0..65535 {
let key = key_from_code(code);
if let Some(code2) = code_from_key(&key) {
if let Some(code2) = code_from_key(key) {
assert_eq!(code, code2)
} else {
assert!(false, "We could not convert back code: {:?}", code);
Expand Down
4 changes: 2 additions & 2 deletions src/windows/simulate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ fn keyboard_event(flags: u32, vk: u16, scan: u16) -> Result<(), SimulateError> {
pub fn simulate(event_type: &EventType) -> Result<(), SimulateError> {
match event_type {
EventType::KeyPress(key) => {
if let Some(code) = code_from_key(key) {
if let Some(code) = code_from_key(*key) {
keyboard_event(KEYEVENTF_KEYDOWN, code, 0)
} else {
Err(SimulateError)
}
}
EventType::KeyRelease(key) => {
if let Some(code) = code_from_key(key) {
if let Some(code) = code_from_key(*key) {
keyboard_event(KEYEVENTF_KEYUP, code, 0)
} else {
Err(SimulateError)
Expand Down