Skip to content

Commit aef0afc

Browse files
committed
New formatting.
1 parent 4c77be6 commit aef0afc

File tree

16 files changed

+273
-243
lines changed

16 files changed

+273
-243
lines changed

examples/grab.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rdev::{grab, Event, EventType, Key};
1+
use rdev::{Event, EventType, Key, grab};
22

33
fn main() {
44
// This will block.

examples/listen.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rdev::{listen, Event};
1+
use rdev::{Event, listen};
22

33
fn main() {
44
// This will block.

examples/simulate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rdev::{simulate, Button, EventType, Key, SimulateError};
1+
use rdev::{Button, EventType, Key, SimulateError, simulate};
22
use std::{
33
thread,
44
time::{self, Duration},

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,9 @@ pub use crate::rdev::{
226226
#[cfg(target_os = "macos")]
227227
mod macos;
228228
#[cfg(target_os = "macos")]
229-
use crate::macos::{display_size as _display_size, listen as _listen, simulate as _simulate};
229+
pub use crate::macos::{Keyboard, set_is_main_thread};
230230
#[cfg(target_os = "macos")]
231-
pub use crate::macos::{set_is_main_thread, Keyboard};
231+
use crate::macos::{display_size as _display_size, listen as _listen, simulate as _simulate};
232232

233233
#[cfg(all(target_family = "unix", not(target_os = "macos")))]
234234
mod linux;

src/linux/wayland/grab.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ use super::keyboard::Keyboard;
33
use crate::rdev::{Button, Event, EventType, GrabError, Key, KeyboardState};
44
use epoll::ControlOptions::{EPOLL_CTL_ADD, EPOLL_CTL_DEL};
55
use evdev_rs::{
6-
enums::{EventCode, EV_KEY, EV_REL},
76
Device, InputEvent, UInputDevice,
7+
enums::{EV_KEY, EV_REL, EventCode},
88
};
99
use inotify::{Inotify, WatchMask};
1010
use std::ffi::{OsStr, OsString};
11-
use std::fs::{read_dir, File};
11+
use std::fs::{File, read_dir};
1212
use std::io;
1313
use std::os::unix::{
1414
ffi::OsStrExt,

src/linux/wayland/keycodes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ decl_keycodes_uinput!(
265265

266266
#[cfg(test)]
267267
mod test {
268-
use super::{code_from_key, key_from_code, key_from_ukey, ukey_from_key, UKey};
268+
use super::{UKey, code_from_key, key_from_code, key_from_ukey, ukey_from_key};
269269
#[test]
270270
fn test_reversible() {
271271
for code in 0..65636 {

src/linux/wayland/listen.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ use super::keyboard::Keyboard;
33
use super::keycodes::key_from_code;
44
use crate::rdev::{Event, KeyboardState, ListenError};
55
use crate::{Button, EventType};
6+
use input::event::PointerEvent;
67
use input::event::keyboard::{KeyState, KeyboardEventTrait};
78
use input::event::pointer::{Axis, ButtonState};
8-
use input::event::PointerEvent;
99
use input::{Event as LibEvent, Libinput, LibinputInterface};
1010
use libc::{O_RDONLY, O_RDWR, O_WRONLY};
1111
use std::fs::{File, OpenOptions};

src/linux/wayland/simulate.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
use crate::SimulateError;
12
use crate::linux::wayland::keycodes::ukey_from_key;
23
use crate::rdev::{Button, EventType};
3-
use crate::SimulateError;
44
use input_linux::{
55
EventKind, EventTime, InputEvent, InputId, Key as UKey, KeyEvent, KeyState, RelativeAxis,
66
RelativeEvent, SynchronizeEvent, SynchronizeKind, UInputHandle,
77
};
8-
use libc::{input_event, O_NONBLOCK};
8+
use libc::{O_NONBLOCK, input_event};
99
use std::fs::{File, OpenOptions};
1010
use std::os::unix::fs::OpenOptionsExt;
1111
use std::sync::{LazyLock, Mutex};

src/linux/x11/grab.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ use super::keyboard::Keyboard;
33
use crate::rdev::{Button, Event, EventType, GrabError, Key, KeyboardState};
44
use epoll::ControlOptions::{EPOLL_CTL_ADD, EPOLL_CTL_DEL};
55
use evdev_rs::{
6-
enums::{EventCode, EV_KEY, EV_REL},
76
Device, InputEvent, UInputDevice,
7+
enums::{EV_KEY, EV_REL, EventCode},
88
};
99
use inotify::{Inotify, WatchMask};
1010
use std::ffi::{OsStr, OsString};
11-
use std::fs::{read_dir, File};
11+
use std::fs::{File, read_dir};
1212
use std::io;
1313
use std::os::unix::{
1414
ffi::OsStrExt,

src/windows/common.rs

Lines changed: 101 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use winapi::shared::ntdef::LONG;
1111
use winapi::shared::windef::HHOOK;
1212
use winapi::um::errhandlingapi::GetLastError;
1313
use winapi::um::winuser::{
14-
SetWindowsHookExA, KBDLLHOOKSTRUCT, MSLLHOOKSTRUCT, WHEEL_DELTA, WH_KEYBOARD_LL, WH_MOUSE_LL,
14+
KBDLLHOOKSTRUCT, MSLLHOOKSTRUCT, SetWindowsHookExA, WH_KEYBOARD_LL, WH_MOUSE_LL, WHEEL_DELTA,
1515
WM_KEYDOWN, WM_KEYUP, WM_LBUTTONDOWN, WM_LBUTTONUP, WM_MBUTTONDOWN, WM_MBUTTONUP,
1616
WM_MOUSEHWHEEL, WM_MOUSEMOVE, WM_MOUSEWHEEL, WM_RBUTTONDOWN, WM_RBUTTONUP, WM_SYSKEYDOWN,
1717
WM_SYSKEYUP, WM_XBUTTONDOWN, WM_XBUTTONUP,
@@ -24,104 +24,120 @@ lazy_static! {
2424
pub(crate) static ref KEYBOARD: Mutex<Keyboard> = Mutex::new(Keyboard::new().unwrap());
2525
}
2626

27-
pub unsafe fn get_code(lpdata: LPARAM) -> DWORD { unsafe {
28-
let kb = *(lpdata as *const KBDLLHOOKSTRUCT);
29-
kb.vkCode
30-
}}
31-
pub unsafe fn get_scan_code(lpdata: LPARAM) -> DWORD { unsafe {
32-
let kb = *(lpdata as *const KBDLLHOOKSTRUCT);
33-
kb.scanCode
34-
}}
35-
pub unsafe fn get_point(lpdata: LPARAM) -> (LONG, LONG) { unsafe {
36-
let mouse = *(lpdata as *const MSLLHOOKSTRUCT);
37-
(mouse.pt.x, mouse.pt.y)
38-
}}
27+
pub unsafe fn get_code(lpdata: LPARAM) -> DWORD {
28+
unsafe {
29+
let kb = *(lpdata as *const KBDLLHOOKSTRUCT);
30+
kb.vkCode
31+
}
32+
}
33+
pub unsafe fn get_scan_code(lpdata: LPARAM) -> DWORD {
34+
unsafe {
35+
let kb = *(lpdata as *const KBDLLHOOKSTRUCT);
36+
kb.scanCode
37+
}
38+
}
39+
pub unsafe fn get_point(lpdata: LPARAM) -> (LONG, LONG) {
40+
unsafe {
41+
let mouse = *(lpdata as *const MSLLHOOKSTRUCT);
42+
(mouse.pt.x, mouse.pt.y)
43+
}
44+
}
3945
// https://docs.microsoft.com/en-us/previous-versions/windows/desktop/legacy/ms644986(v=vs.85)
4046
/// confusingly, this function returns a WORD (unsigned), but may be
4147
/// interpreted as either signed or unsigned depending on context
42-
pub unsafe fn get_delta(lpdata: LPARAM) -> WORD { unsafe {
43-
let mouse = *(lpdata as *const MSLLHOOKSTRUCT);
44-
HIWORD(mouse.mouseData)
45-
}}
46-
pub unsafe fn get_button_code(lpdata: LPARAM) -> WORD { unsafe {
47-
let mouse = *(lpdata as *const MSLLHOOKSTRUCT);
48-
HIWORD(mouse.mouseData)
49-
}}
48+
pub unsafe fn get_delta(lpdata: LPARAM) -> WORD {
49+
unsafe {
50+
let mouse = *(lpdata as *const MSLLHOOKSTRUCT);
51+
HIWORD(mouse.mouseData)
52+
}
53+
}
54+
pub unsafe fn get_button_code(lpdata: LPARAM) -> WORD {
55+
unsafe {
56+
let mouse = *(lpdata as *const MSLLHOOKSTRUCT);
57+
HIWORD(mouse.mouseData)
58+
}
59+
}
5060

51-
pub unsafe fn convert(param: WPARAM, lpdata: LPARAM) -> Option<EventType> { unsafe {
52-
match param.try_into() {
53-
Ok(WM_KEYDOWN) | Ok(WM_SYSKEYDOWN) => {
54-
let code = get_code(lpdata);
55-
let key = key_from_code(code as u16);
56-
Some(EventType::KeyPress(key))
57-
}
58-
Ok(WM_KEYUP) | Ok(WM_SYSKEYUP) => {
59-
let code = get_code(lpdata);
60-
let key = key_from_code(code as u16);
61-
Some(EventType::KeyRelease(key))
61+
pub unsafe fn convert(param: WPARAM, lpdata: LPARAM) -> Option<EventType> {
62+
unsafe {
63+
match param.try_into() {
64+
Ok(WM_KEYDOWN) | Ok(WM_SYSKEYDOWN) => {
65+
let code = get_code(lpdata);
66+
let key = key_from_code(code as u16);
67+
Some(EventType::KeyPress(key))
68+
}
69+
Ok(WM_KEYUP) | Ok(WM_SYSKEYUP) => {
70+
let code = get_code(lpdata);
71+
let key = key_from_code(code as u16);
72+
Some(EventType::KeyRelease(key))
73+
}
74+
Ok(WM_LBUTTONDOWN) => Some(EventType::ButtonPress(Button::Left)),
75+
Ok(WM_LBUTTONUP) => Some(EventType::ButtonRelease(Button::Left)),
76+
Ok(WM_MBUTTONDOWN) => Some(EventType::ButtonPress(Button::Middle)),
77+
Ok(WM_MBUTTONUP) => Some(EventType::ButtonRelease(Button::Middle)),
78+
Ok(WM_RBUTTONDOWN) => Some(EventType::ButtonPress(Button::Right)),
79+
Ok(WM_RBUTTONUP) => Some(EventType::ButtonRelease(Button::Right)),
80+
Ok(WM_XBUTTONDOWN) => {
81+
let code = get_button_code(lpdata) as u8;
82+
Some(EventType::ButtonPress(Button::Unknown(code)))
83+
}
84+
Ok(WM_XBUTTONUP) => {
85+
let code = get_button_code(lpdata) as u8;
86+
Some(EventType::ButtonRelease(Button::Unknown(code)))
87+
}
88+
Ok(WM_MOUSEMOVE) => {
89+
let (x, y) = get_point(lpdata);
90+
Some(EventType::MouseMove {
91+
x: x as f64,
92+
y: y as f64,
93+
})
94+
}
95+
Ok(WM_MOUSEWHEEL) => {
96+
let delta = get_delta(lpdata) as c_short;
97+
Some(EventType::Wheel {
98+
delta_x: 0,
99+
delta_y: (delta / WHEEL_DELTA) as i64,
100+
})
101+
}
102+
Ok(WM_MOUSEHWHEEL) => {
103+
let delta = get_delta(lpdata) as c_short;
104+
Some(EventType::Wheel {
105+
delta_x: (delta / WHEEL_DELTA) as i64,
106+
delta_y: 0,
107+
})
108+
}
109+
_ => None,
62110
}
63-
Ok(WM_LBUTTONDOWN) => Some(EventType::ButtonPress(Button::Left)),
64-
Ok(WM_LBUTTONUP) => Some(EventType::ButtonRelease(Button::Left)),
65-
Ok(WM_MBUTTONDOWN) => Some(EventType::ButtonPress(Button::Middle)),
66-
Ok(WM_MBUTTONUP) => Some(EventType::ButtonRelease(Button::Middle)),
67-
Ok(WM_RBUTTONDOWN) => Some(EventType::ButtonPress(Button::Right)),
68-
Ok(WM_RBUTTONUP) => Some(EventType::ButtonRelease(Button::Right)),
69-
Ok(WM_XBUTTONDOWN) => {
70-
let code = get_button_code(lpdata) as u8;
71-
Some(EventType::ButtonPress(Button::Unknown(code)))
72-
}
73-
Ok(WM_XBUTTONUP) => {
74-
let code = get_button_code(lpdata) as u8;
75-
Some(EventType::ButtonRelease(Button::Unknown(code)))
76-
}
77-
Ok(WM_MOUSEMOVE) => {
78-
let (x, y) = get_point(lpdata);
79-
Some(EventType::MouseMove {
80-
x: x as f64,
81-
y: y as f64,
82-
})
83-
}
84-
Ok(WM_MOUSEWHEEL) => {
85-
let delta = get_delta(lpdata) as c_short;
86-
Some(EventType::Wheel {
87-
delta_x: 0,
88-
delta_y: (delta / WHEEL_DELTA) as i64,
89-
})
90-
}
91-
Ok(WM_MOUSEHWHEEL) => {
92-
let delta = get_delta(lpdata) as c_short;
93-
Some(EventType::Wheel {
94-
delta_x: (delta / WHEEL_DELTA) as i64,
95-
delta_y: 0,
96-
})
97-
}
98-
_ => None,
99111
}
100-
}}
112+
}
101113

102114
type RawCallback = unsafe extern "system" fn(code: c_int, param: WPARAM, lpdata: LPARAM) -> LRESULT;
103115
pub enum HookError {
104116
Mouse(DWORD),
105117
Key(DWORD),
106118
}
107119

108-
pub unsafe fn set_key_hook(callback: RawCallback) -> Result<(), HookError> { unsafe {
109-
let hook = SetWindowsHookExA(WH_KEYBOARD_LL, Some(callback), null_mut(), 0);
120+
pub unsafe fn set_key_hook(callback: RawCallback) -> Result<(), HookError> {
121+
unsafe {
122+
let hook = SetWindowsHookExA(WH_KEYBOARD_LL, Some(callback), null_mut(), 0);
110123

111-
if hook.is_null() {
112-
let error = GetLastError();
113-
return Err(HookError::Key(error));
124+
if hook.is_null() {
125+
let error = GetLastError();
126+
return Err(HookError::Key(error));
127+
}
128+
HOOK = hook;
129+
Ok(())
114130
}
115-
HOOK = hook;
116-
Ok(())
117-
}}
131+
}
118132

119-
pub unsafe fn set_mouse_hook(callback: RawCallback) -> Result<(), HookError> { unsafe {
120-
let hook = SetWindowsHookExA(WH_MOUSE_LL, Some(callback), null_mut(), 0);
121-
if hook.is_null() {
122-
let error = GetLastError();
123-
return Err(HookError::Mouse(error));
133+
pub unsafe fn set_mouse_hook(callback: RawCallback) -> Result<(), HookError> {
134+
unsafe {
135+
let hook = SetWindowsHookExA(WH_MOUSE_LL, Some(callback), null_mut(), 0);
136+
if hook.is_null() {
137+
let error = GetLastError();
138+
return Err(HookError::Mouse(error));
139+
}
140+
HOOK = hook;
141+
Ok(())
124142
}
125-
HOOK = hook;
126-
Ok(())
127-
}}
143+
}

0 commit comments

Comments
 (0)