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
#![allow(unused)]
#![allow(dead_code)]
use std::os::raw::c_void;
type CFTypeRef = *const c_void;
type CGEventRef = *const c_void;
type CGEventSourceRef = *const c_void;
type CGMouseButton = Mouse;
#[repr(i32)]
enum CGEventSourceStateID {
Private = -1,
Combined = 0,
System = 1,
}
#[derive(Clone, Copy)]
#[repr(C)]
struct CGPoint {
x: f64,
y: f64,
}
#[link(name = "AppKit", kind = "framework")]
extern "C" {
fn CGEventCreate(source: CGEventSourceRef) -> CGEventRef;
fn CGEventGetLocation(event: CGEventRef) -> CGPoint;
fn CGEventSourceButtonState(stateID: CGEventSourceStateID, button: CGMouseButton) -> bool;
fn CFRelease(cf: CFTypeRef);
}
#[derive(Clone, Copy)]
#[repr(u16)]
pub enum Mouse {
Left,
Right,
Center,
}
impl Mouse {
#[inline(always)]
pub fn location() -> (f64, f64) {
unsafe {
let event = CGEventCreate(std::ptr::null());
let CGPoint { x, y } = CGEventGetLocation(event);
CFRelease(event);
(x, y)
}
}
#[inline(always)]
pub fn is_pressed(self) -> bool {
unsafe { CGEventSourceButtonState(CGEventSourceStateID::Combined, self) }
}
}