Repository navigation
Expand file tree
/
Copy pathmod.rs
More file actions
357 lines (318 loc) · 12.8 KB
/
Copy pathmod.rs
File metadata and controls
357 lines (318 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
use crate::{common::*, public::*};
use once_cell::sync::Lazy;
use std::{
ffi::{c_int, c_short, c_ulong, c_ushort},
mem::{size_of, MaybeUninit},
ptr::null_mut,
sync::atomic::AtomicPtr,
};
use windows::Win32::{
Foundation::{LPARAM, LRESULT, WPARAM},
UI::{
Input::KeyboardAndMouse::{
GetAsyncKeyState, GetKeyState, MapVirtualKeyW, SendInput, INPUT, INPUT_0,
INPUT_KEYBOARD, INPUT_MOUSE, KEYBDINPUT, KEYBD_EVENT_FLAGS, KEYEVENTF_KEYUP,
KEYEVENTF_SCANCODE, MAP_VIRTUAL_KEY_TYPE, MOUSEEVENTF_HWHEEL, MOUSEEVENTF_LEFTDOWN,
MOUSEEVENTF_LEFTUP, MOUSEEVENTF_MIDDLEDOWN, MOUSEEVENTF_MIDDLEUP, MOUSEEVENTF_XUP,
MOUSEEVENTF_RIGHTDOWN, MOUSEEVENTF_RIGHTUP, MOUSEEVENTF_XDOWN, MOUSEEVENTF_WHEEL, MOUSEINPUT,
MOUSE_EVENT_FLAGS, VIRTUAL_KEY,
},
WindowsAndMessaging::{
CallNextHookEx, GetCursorPos, GetMessageW, KillTimer, SetCursorPos, SetTimer,
SetWindowsHookExW, UnhookWindowsHookEx, HHOOK, KBDLLHOOKSTRUCT, MSG, MSLLHOOKSTRUCT,
WH_KEYBOARD_LL, WH_MOUSE_LL, WINDOWS_HOOK_ID, WM_KEYDOWN, WM_KEYUP, WM_LBUTTONDOWN,
WM_LBUTTONUP, WM_MBUTTONDOWN, WM_MBUTTONUP, WM_MOUSEWHEEL, WM_RBUTTONDOWN,
WM_RBUTTONUP, WM_SYSKEYDOWN, WM_SYSKEYUP, WM_XBUTTONDOWN, WM_XBUTTONUP, XBUTTON1,
XBUTTON2,
},
},
};
mod inputs;
static KEYBD_HHOOK: Lazy<AtomicPtr<HHOOK>> = Lazy::new(AtomicPtr::default);
static MOUSE_HHOOK: Lazy<AtomicPtr<HHOOK>> = Lazy::new(AtomicPtr::default);
impl KeybdKey {
/// Returns true if a given `KeybdKey` is currently pressed (in the down position).
pub fn is_pressed(self) -> bool {
(unsafe { GetAsyncKeyState(u64::from(self) as i32) } >> 15) != 0
}
/// Presses a given `KeybdKey`. Note: this means the key will remain in the down
/// position. You must manually call release to create a full 'press'.
pub fn press(self) {
send_keybd_input(KEYEVENTF_SCANCODE, self);
}
/// Releases a given `KeybdKey`. This means the key would be in the up position.
pub fn release(self) {
send_keybd_input(KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP, self);
}
/// Returns true if a keyboard key which supports toggling (ScrollLock, NumLock,
/// CapsLock) is on.
pub fn is_toggled(self) -> bool {
unsafe { GetKeyState(u64::from(self) as i32) & 15 != 0 }
}
}
impl MouseButton {
/// Returns true if a given `MouseButton` is currently pressed (in the down position).
pub fn is_pressed(self) -> bool {
(unsafe { GetAsyncKeyState(u32::from(self) as i32) } >> 15) != 0
}
/// Presses a given `MouseButton`. Note: this means the button will remain in the down
/// position. You must manually call release to create a full 'click'.
pub fn press(self) {
match self {
MouseButton::LeftButton => send_mouse_input(MOUSEEVENTF_LEFTDOWN, 0, 0, 0),
MouseButton::RightButton => send_mouse_input(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0),
MouseButton::MiddleButton => send_mouse_input(MOUSEEVENTF_MIDDLEDOWN, 0, 0, 0),
MouseButton::X1Button => send_mouse_input(MOUSEEVENTF_XDOWN, 0x0001, 0, 0),
MouseButton::X2Button => send_mouse_input(MOUSEEVENTF_XDOWN, 0x0002, 0, 0),
_ => {}
}
}
/// Releases a given `MouseButton`. This means the button would be in the up position.
pub fn release(self) {
match self {
MouseButton::LeftButton => send_mouse_input(MOUSEEVENTF_LEFTUP, 0, 0, 0),
MouseButton::RightButton => send_mouse_input(MOUSEEVENTF_RIGHTUP, 0, 0, 0),
MouseButton::MiddleButton => send_mouse_input(MOUSEEVENTF_MIDDLEUP, 0, 0, 0),
MouseButton::X1Button => send_mouse_input(MOUSEEVENTF_XUP, 0x0001, 0, 0),
MouseButton::X2Button => send_mouse_input(MOUSEEVENTF_XUP, 0x0002, 0, 0),
_ => {}
}
}
}
impl MouseCursor {
pub fn pos() -> (i32, i32) {
let mut point = MaybeUninit::uninit();
unsafe { GetCursorPos(point.as_mut_ptr()).unwrap() };
let point = unsafe { point.assume_init() };
(point.x, point.y)
}
/// Moves the mouse relative to its current position by a given amount of pixels.
pub fn move_rel(dx: i32, dy: i32) {
let (x, y) = Self::pos();
Self::move_abs(x + dx, y + dy);
}
/// Moves the mouse to a given position based on absolute coordinates. The top left
/// corner of the screen is (0, 0).
pub fn move_abs(x: i32, y: i32) {
unsafe {
SetCursorPos(x, y).unwrap();
}
}
}
impl MouseWheel {
/// Scrolls the mouse wheel vertically by a given amount of "wheel clicks".
pub fn scroll_ver(dwheel: i32) {
send_mouse_input(MOUSEEVENTF_WHEEL, dwheel * 120, 0, 0);
}
/// Scrolls the mouse wheel horizontally by a given amount of "wheel clicks".
pub fn scroll_hor(dwheel: i32) {
send_mouse_input(MOUSEEVENTF_HWHEEL, dwheel * 120, 0, 0);
}
/// Scrolls the mouse wheel vertically by a given amount.
pub fn scroll_ver_unscaled(dwheel: i32) {
send_mouse_input(MOUSEEVENTF_WHEEL, dwheel, 0, 0);
}
/// Scrolls the mouse wheel horizontally by a given amount.
pub fn scroll_hor_unscaled(dwheel: i32) {
send_mouse_input(MOUSEEVENTF_HWHEEL, dwheel, 0, 0);
}
}
/// Starts listening for bound input events (otionally stopping when binds are removed).
pub fn handle_input_events(auto_stop: bool) {
if !MOUSE_BINDS.lock().unwrap().is_empty() {
set_hook(WH_MOUSE_LL, &MOUSE_HHOOK, mouse_proc);
};
if !KEYBD_BINDS.lock().unwrap().is_empty() {
set_hook(WH_KEYBOARD_LL, &KEYBD_HHOOK, keybd_proc);
};
let timer_id = unsafe { SetTimer(None, 0, 100, None) };
HANDLE_EVENTS.store(true, Ordering::Relaxed);
while should_continue(auto_stop) {
let mut msg: MSG = unsafe { MaybeUninit::zeroed().assume_init() };
unsafe { GetMessageW(&mut msg, None, 0, 0) };
}
let _ = unsafe { KillTimer(None, timer_id) };
}
unsafe extern "system" fn keybd_proc(code: c_int, w_param: WPARAM, l_param: LPARAM) -> LRESULT {
if KEYBD_BINDS.lock().unwrap().is_empty() {
unset_hook(&KEYBD_HHOOK);
} else if w_param.0 as u32 == WM_KEYDOWN || w_param.0 as u32 == WM_SYSKEYDOWN {
if let Some(bind) = KEYBD_BINDS
.lock()
.unwrap()
.get_mut(&KeybdKey::from(u64::from(
(*(l_param.0 as *const KBDLLHOOKSTRUCT)).vkCode,
)))
{
match bind {
Bind::Normal(cb) => {
let cb = Arc::clone(cb);
spawn(move || cb());
}
Bind::Release(_) => {}
Bind::Block(cb) => {
let cb = Arc::clone(cb);
spawn(move || cb());
return LRESULT(1);
}
Bind::Blockable(cb) => {
if let BlockInput::Block = cb() {
return LRESULT(1);
}
}
}
}
} else if w_param.0 as u32 == WM_KEYUP || w_param.0 as u32 == WM_SYSKEYUP {
if let Some(bind) = KEYBD_RELEASE_BINDS
.lock()
.unwrap()
.get_mut(&KeybdKey::from(u64::from(
(*(l_param.0 as *const KBDLLHOOKSTRUCT)).vkCode,
)))
{
match bind {
Bind::Release(cb) => {
let cb = Arc::clone(cb);
spawn(move || cb());
}
Bind::Normal(_) | Bind::Block(_) | Bind::Blockable(_) => {}
}
}
}
return CallNextHookEx(None, code, w_param, l_param);
}
// Replacement for missing conversions in windows crate
type DWORD = c_ulong;
type WORD = c_ushort;
fn hiword(l: DWORD) -> WORD {
((l >> 16) & 0xffff) as WORD
}
fn hiword_signed(l: DWORD) -> c_short {
((l >> 16) & 0xffff) as c_short
}
unsafe extern "system" fn mouse_proc(code: c_int, w_param: WPARAM, l_param: LPARAM) -> LRESULT {
if MOUSE_BINDS.lock().unwrap().is_empty() {
unset_hook(&MOUSE_HHOOK);
} else if let Some((event, key_up)) = identify_event(w_param, l_param) {
if key_up {
if let Some(bind) = MOUSE_RELEASE_BINDS.lock().unwrap().get_mut(&event) {
match bind {
Bind::Release(cb) => {
let cb = Arc::clone(cb);
spawn(move || cb());
}
Bind::Normal(_) | Bind::Block(_) | Bind::Blockable(_) => {}
}
}
} else if let Some(bind) = MOUSE_BINDS.lock().unwrap().get_mut(&event) {
match bind {
Bind::Normal(cb) => {
let cb = Arc::clone(cb);
spawn(move || cb());
}
Bind::Block(cb) => {
let cb = Arc::clone(cb);
spawn(move || cb());
return LRESULT(1);
}
Bind::Blockable(cb) => {
if let BlockInput::Block = cb() {
return LRESULT(1);
}
}
Bind::Release(_) => {}
}
};
}
return CallNextHookEx(None, code, w_param, l_param);
unsafe fn identify_event(w_param: WPARAM, l_param: LPARAM) -> Option<(MouseButton, bool)> {
match w_param.0 as u32 {
WM_LBUTTONDOWN | WM_LBUTTONUP => {
Some((MouseButton::LeftButton, w_param.0 as u32 == WM_LBUTTONUP))
}
WM_RBUTTONDOWN | WM_RBUTTONUP => {
Some((MouseButton::RightButton, w_param.0 as u32 == WM_RBUTTONUP))
}
WM_MBUTTONDOWN | WM_MBUTTONUP => {
Some((MouseButton::MiddleButton, w_param.0 as u32 == WM_MBUTTONUP))
}
WM_XBUTTONDOWN | WM_XBUTTONUP => {
let llhs = &*(l_param.0 as *const MSLLHOOKSTRUCT);
match hiword(llhs.mouseData) {
XBUTTON1 => Some((MouseButton::X1Button, w_param.0 as u32 == WM_XBUTTONUP)),
XBUTTON2 => Some((MouseButton::X2Button, w_param.0 as u32 == WM_XBUTTONUP)),
_ => None,
}
}
WM_MOUSEWHEEL => {
let llhs = &*(l_param.0 as *const MSLLHOOKSTRUCT);
let delta = hiword_signed(llhs.mouseData);
/*
"If the message is WM_MOUSEWHEEL, the high-order word of this member is the wheel delta.
The low-order word is reserved. A positive value indicates that the wheel was rotated
forward, away from the user; a negative value indicates that the wheel was rotated
backward, toward the user. One wheel click is defined as WHEEL_DELTA, which is 120."
In practice, the value received is always either 120 or -120.
*/
if delta >= 0 {
Some((MouseButton::MousewheelUp, false))
} else {
Some((MouseButton::MousewheelDown, false))
}
}
_ => None,
}
}
}
fn set_hook(
hook_id: WINDOWS_HOOK_ID,
hook_ptr: &AtomicPtr<HHOOK>,
hook_proc: unsafe extern "system" fn(c_int, WPARAM, LPARAM) -> LRESULT,
) {
hook_ptr.store(
unsafe { &mut SetWindowsHookExW(hook_id, Some(hook_proc), None, 0).unwrap() },
Ordering::Relaxed,
);
}
fn unset_hook(hook_ptr: &AtomicPtr<HHOOK>) {
if !hook_ptr.load(Ordering::Relaxed).is_null() {
unsafe { UnhookWindowsHookEx(*hook_ptr.load(Ordering::Relaxed)).unwrap() };
hook_ptr.store(null_mut(), Ordering::Relaxed);
}
}
fn send_mouse_input(flags: MOUSE_EVENT_FLAGS, data: i32, dx: i32, dy: i32) {
let mouse: MOUSEINPUT = MOUSEINPUT {
dx,
dy,
mouseData: data,
dwFlags: flags,
time: 0,
dwExtraInfo: 0,
};
let mut input_u: INPUT_0 = unsafe { std::mem::zeroed() };
input_u.mi = mouse;
let input = INPUT {
r#type: INPUT_MOUSE,
Anonymous: input_u,
};
unsafe { SendInput(&[input], size_of::<INPUT>() as c_int) };
}
fn send_keybd_input(flags: KEYBD_EVENT_FLAGS, key_code: KeybdKey) {
let keybd: KEYBDINPUT = unsafe {
KEYBDINPUT {
wVk: VIRTUAL_KEY(0),
wScan: MapVirtualKeyW(u64::from(key_code) as u32, MAP_VIRTUAL_KEY_TYPE(0)) as u16,
dwFlags: flags,
time: 0,
dwExtraInfo: 0,
}
};
// We need an "empty" winapi struct to union-ize
let mut input_u: INPUT_0 = unsafe { std::mem::zeroed() };
input_u.ki = keybd;
let input = INPUT {
r#type: INPUT_KEYBOARD,
Anonymous: input_u,
};
unsafe { SendInput(&[input], size_of::<INPUT>() as c_int) };
}