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
use std::os::raw::c_void;

type CFTypeRef = *const c_void;
type CGEventRef = *const c_void;
type CGEventSourceRef = *const c_void;

#[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 CFRelease(cf: CFTypeRef);
}

pub struct Mouse;

impl Mouse {
    /// Returns the current mouse location.
    /// ```
    /// use readmouse::Mouse;
    /// loop {
    ///   println!("State of Up key: {}, ", Mouse::location();
    /// }
    /// ```
    #[inline(always)]
    pub fn location() -> (f64, f64) {
        unsafe {
            let event = CGEventCreate(std::ptr::null());
            let CGPoint { x, y } = CGEventGetLocation(event);
            CFRelease(event);
            (x, y)
        }
    }
}