-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathdarwin.rs
453 lines (414 loc) · 14.2 KB
/
darwin.rs
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
///
/// This module contains the mouse action functions
/// for the darwin systems (MacOS)
/// Uses the CoreGraphics (a.k.a Quartz) framework
///
use crate::common::{CallbackId, MouseActions, MouseButton, MouseEvent, ScrollDirection};
use crate::error::Error;
use std::collections::HashMap;
use std::os::raw::{c_double, c_int, c_long, c_uint, c_ulong, c_void};
use std::ptr::null_mut;
use std::sync::Mutex;
use std::thread;
static mut TAP_EVENT_REF: Option<CFTypeRef> = None;
static mut CALLBACKS: Option<Mutex<HashMap<CallbackId, Box<dyn Fn(&MouseEvent) + Send>>>> = None;
#[derive(Clone)]
pub struct DarwinMouseManager {
callback_counter: CallbackId,
is_listening: bool,
}
impl DarwinMouseManager {
pub fn new() -> Self {
DarwinMouseManager {
callback_counter: 0,
is_listening: false,
}
}
fn create_mouse_event(
&self,
event_type: CGEventType,
mouse_button: CGMouseButton,
) -> Result<(), Error> {
let (pos_x, pos_y) = self.get_position()?;
let position = CGPoint {
x: pos_x as c_double,
y: pos_y as c_double,
};
unsafe {
let event = CGEventCreateMouseEvent(null_mut(), event_type, position, mouse_button);
if event == null_mut() {
return Err(Error::CGCouldNotCreateEvent);
}
CGEventPost(CGEventTapLocation::CGHIDEventTap, event);
CFRelease(event as CFTypeRef);
}
Ok(())
}
fn create_scroll_wheel_event(
&self,
distance: c_int,
direction: &ScrollDirection,
) -> Result<(), Error> {
unsafe {
let event = match direction {
ScrollDirection::Up | ScrollDirection::Down => CGEventCreateScrollWheelEvent(
null_mut(),
CGScrollEventUnit::Line,
2,
distance,
0,
),
ScrollDirection::Right | ScrollDirection::Left => CGEventCreateScrollWheelEvent(
null_mut(),
CGScrollEventUnit::Line,
2,
0,
distance,
),
};
if event == null_mut() {
return Err(Error::CGCouldNotCreateEvent);
}
CGEventPost(CGEventTapLocation::CGHIDEventTap, event);
CFRelease(event as CFTypeRef);
}
Ok(())
}
fn start_listener(&mut self) -> Result<(), Error> {
thread::spawn(move || {
unsafe extern "C" fn mouse_on_event_callback(
_proxy: *const c_void,
event_type: CGEventType,
cg_event: CGEventRef,
_user_info: *mut c_void,
) -> CGEventRef {
// Construct the library's MouseEvent
let mouse_event = match event_type {
CGEventType::LeftMouseDown => Some(MouseEvent::Press(MouseButton::Left)),
CGEventType::LeftMouseUp => Some(MouseEvent::Release(MouseButton::Left)),
CGEventType::RightMouseDown => Some(MouseEvent::Press(MouseButton::Right)),
CGEventType::RightMouseUp => Some(MouseEvent::Release(MouseButton::Right)),
CGEventType::OtherMouseDown => Some(MouseEvent::Press(MouseButton::Middle)),
CGEventType::OtherMouseUp => Some(MouseEvent::Release(MouseButton::Middle)),
CGEventType::MouseMoved => {
let point = CGEventGetLocation(cg_event);
Some(MouseEvent::AbsoluteMove(point.x as i32, point.y as i32))
}
CGEventType::ScrollWheel => {
// CGEventField::scrollWheelEventPointDeltaAxis1 = 96
// CGEventField::scrollWheelEventPointDeltaAxis2 = 97
let delta_y = CGEventGetIntegerValueField(cg_event, 96);
let delta_x = CGEventGetIntegerValueField(cg_event, 97);
if delta_y > 0 {
Some(MouseEvent::Scroll(ScrollDirection::Up))
} else if delta_y < 0 {
Some(MouseEvent::Scroll(ScrollDirection::Down))
} else if delta_x < 0 {
Some(MouseEvent::Scroll(ScrollDirection::Right))
} else if delta_x > 0 {
Some(MouseEvent::Scroll(ScrollDirection::Left))
} else {
// Probably axis3 wheel scrolled
None
}
}
_ => None,
};
match (mouse_event, &mut CALLBACKS) {
(Some(event), Some(callbacks)) => {
for callback in callbacks.lock().unwrap().values() {
callback(&event);
}
}
_ => {}
}
cg_event
}
unsafe {
// Create the mouse listener hook
TAP_EVENT_REF = Some(CGEventTapCreate(
CGEventTapLocation::CGHIDEventTap,
CGEventTapPlacement::HeadInsertEventTap,
CGEventTapOption::ListenOnly as u32,
(1 << CGEventType::LeftMouseDown as u64)
+ (1 << CGEventType::LeftMouseUp as u64)
+ (1 << CGEventType::RightMouseDown as u64)
+ (1 << CGEventType::RightMouseUp as u64)
+ (1 << CGEventType::OtherMouseDown as u64)
+ (1 << CGEventType::OtherMouseUp as u64)
+ (1 << CGEventType::MouseMoved as u64)
+ (1 << CGEventType::ScrollWheel as u64),
Some(mouse_on_event_callback),
null_mut(),
));
let loop_source =
CFMachPortCreateRunLoopSource(null_mut(), TAP_EVENT_REF.unwrap(), 0);
let current_loop = CFRunLoopGetCurrent();
CFRunLoopAddSource(current_loop, loop_source, kCFRunLoopDefaultMode);
CGEventTapEnable(TAP_EVENT_REF.unwrap(), true);
CFRunLoopRun();
}
});
Ok(())
}
}
impl Default for DarwinMouseManager {
fn default() -> Self {
Self::new()
}
}
impl Drop for DarwinMouseManager {
fn drop(&mut self) {
unsafe {
match TAP_EVENT_REF {
Some(event_ref) => {
// Release the tap event
CFRelease(event_ref);
TAP_EVENT_REF = None;
}
None => {}
}
}
}
}
impl MouseActions for DarwinMouseManager {
fn move_to(&self, x: usize, y: usize) -> Result<(), Error> {
let cg_point = CGPoint {
x: x as f64,
y: y as f64,
};
unsafe {
let result = CGWarpMouseCursorPosition(cg_point);
if result != CGError::Success {
return Err(Error::CustomError(
"Failed to move the mouse, CGError is not Success".to_string(),
));
}
};
Ok(())
}
fn move_relative(&self, x_offset: i32, y_offset: i32) -> Result<(), Error> {
let (x, y) = self.get_position()?;
self.move_to((x + x_offset) as usize, (y + y_offset) as usize)
}
fn get_position(&self) -> Result<(i32, i32), Error> {
unsafe {
let event = CGEventCreate(null_mut());
if event == null_mut() {
return Err(Error::CGCouldNotCreateEvent);
}
let cursor = CGEventGetLocation(event);
CFRelease(event as CFTypeRef);
return Ok((cursor.x as i32, cursor.y as i32));
}
}
fn press_button(&self, button: &MouseButton) -> Result<(), Error> {
let (event_type, mouse_button) = match button {
MouseButton::Left => (CGEventType::LeftMouseDown, CGMouseButton::Left),
MouseButton::Middle => (CGEventType::OtherMouseDown, CGMouseButton::Center),
MouseButton::Right => (CGEventType::RightMouseDown, CGMouseButton::Right),
};
self.create_mouse_event(event_type, mouse_button)?;
Ok(())
}
fn release_button(&self, button: &MouseButton) -> Result<(), Error> {
let (event_type, mouse_button) = match button {
MouseButton::Left => (CGEventType::LeftMouseUp, CGMouseButton::Left),
MouseButton::Middle => (CGEventType::OtherMouseUp, CGMouseButton::Center),
MouseButton::Right => (CGEventType::RightMouseUp, CGMouseButton::Right),
};
self.create_mouse_event(event_type, mouse_button)
}
fn click_button(&self, button: &MouseButton) -> Result<(), Error> {
self.press_button(button)?;
self.release_button(button)
}
fn scroll_wheel(&self, direction: &ScrollDirection) -> Result<(), Error> {
let distance = match direction {
ScrollDirection::Up | ScrollDirection::Left => 5,
ScrollDirection::Down | ScrollDirection::Right => -5,
};
self.create_scroll_wheel_event(distance, direction)
}
fn hook(&mut self, callback: Box<dyn Fn(&MouseEvent) + Send>) -> Result<CallbackId, Error> {
if !self.is_listening {
self.start_listener()?;
self.is_listening = true;
}
let id = self.callback_counter;
unsafe {
match &mut CALLBACKS {
Some(callbacks) => {
callbacks.lock().unwrap().insert(id, callback);
}
None => {
initialize_callbacks();
return self.hook(callback);
}
}
}
self.callback_counter += 1;
Ok(id)
}
fn unhook(&mut self, callback_id: CallbackId) -> Result<(), Error> {
unsafe {
match &mut CALLBACKS {
Some(callbacks) => match callbacks.lock().unwrap().remove(&callback_id) {
Some(_) => Ok(()),
None => Err(Error::UnhookFailed),
},
None => {
initialize_callbacks();
self.unhook(callback_id)
}
}
}
}
fn unhook_all(&mut self) -> Result<(), Error> {
unsafe {
match &mut CALLBACKS {
Some(callbacks) => {
callbacks.lock().unwrap().clear();
}
None => {
initialize_callbacks();
return self.unhook_all();
}
}
}
Ok(())
}
}
fn initialize_callbacks() {
unsafe {
match CALLBACKS {
Some(_) => {}
None => {
CALLBACKS = Some(Mutex::new(HashMap::new()));
}
}
}
}
/// CoreGraphics type definitions
#[allow(dead_code)]
#[derive(PartialEq, Eq)]
#[repr(C)]
enum CGError {
CannotComplete = 1004,
Failure = 1000,
IllegalArgument = 1001,
InvalidConnection = 1002,
InvalidContext = 1003,
InvalidOperation = 1010,
NoneAvailable = 1011,
NotImplemented = 1006,
RangeCheck = 1007,
Success = 0,
TypeCheck = 1008,
}
#[repr(C)]
pub struct CGPoint {
x: c_double,
y: c_double,
}
enum CGEventSource {}
enum CGEvent {}
type CGEventSourceRef = *mut CGEventSource;
type CGEventRef = *mut CGEvent;
type CFTypeRef = *const c_void;
type CGEventMask = c_ulong;
#[repr(C)]
enum CGEventType {
LeftMouseDown = 1,
LeftMouseUp = 2,
RightMouseDown = 3,
RightMouseUp = 4,
MouseMoved = 5,
_LeftMouseDragged = 6,
_RightMouseDragged = 7,
ScrollWheel = 22,
OtherMouseDown = 25,
OtherMouseUp = 26,
_OtherMouseDragged = 27,
}
#[repr(C)]
enum CGMouseButton {
Left = 0,
Right = 1,
Center = 2,
}
#[repr(C)]
enum CGEventTapLocation {
CGHIDEventTap = 0,
_CGSessionEventTap = 1,
_CGAnnotatedSessionEventTap = 2,
}
#[repr(C)]
enum CGScrollEventUnit {
_Pixel = 0,
Line = 1,
}
#[repr(C)]
enum CGEventTapPlacement {
HeadInsertEventTap = 0,
_TailAppendEventTap = 1,
}
#[repr(C)]
enum CGEventTapOption {
_Default = 0,
ListenOnly = 1,
}
type CGEventTapCallback = Option<
unsafe extern "C" fn(
proxy: *const c_void,
event_type: CGEventType,
cg_event: CGEventRef,
user_info: *mut c_void,
) -> CGEventRef,
>;
#[link(name = "CoreGraphics", kind = "framework")]
extern "C" {
fn CGWarpMouseCursorPosition(new_cursor_position: CGPoint) -> CGError;
fn CGEventCreate(source: CGEventSourceRef) -> CGEventRef;
fn CGEventGetLocation(event: CGEventRef) -> CGPoint;
fn CGEventCreateMouseEvent(
source: CGEventSourceRef,
mouse_type: CGEventType,
mouse_cursor_position: CGPoint,
mouse_button: CGMouseButton,
) -> CGEventRef;
fn CGEventCreateScrollWheelEvent(
source: CGEventSourceRef,
units: CGScrollEventUnit,
// Number of scroll directions/wheels, maximum is 3
wheel_count: c_int,
// Vertical wheel movement distance
wheel1: c_int,
// Horizontal wheel movement distance
wheel2: c_int,
) -> CGEventRef;
fn CGEventPost(tap: CGEventTapLocation, event: CGEventRef);
fn CGEventTapCreate(
tap: CGEventTapLocation,
place: CGEventTapPlacement,
options: c_uint,
eventsOfInterest: CGEventMask,
callback: CGEventTapCallback,
refcon: *mut c_void,
) -> CFTypeRef;
fn CGEventTapEnable(tap: *const c_void, enable: bool);
fn CGEventGetIntegerValueField(event: CGEventRef, field: c_uint) -> c_long;
}
#[link(name = "CoreFoundation", kind = "framework")]
extern "C" {
static kCFRunLoopDefaultMode: *const c_void;
fn CFRelease(cf: CFTypeRef);
fn CFMachPortCreateRunLoopSource(
allocator: *mut c_void,
tap: *const c_void,
order: c_ulong,
) -> *mut c_void;
fn CFRunLoopGetCurrent() -> *mut c_void;
fn CFRunLoopAddSource(rl: *mut c_void, source: *mut c_void, mode: *const c_void);
fn CFRunLoopRun();
}