-
Notifications
You must be signed in to change notification settings - Fork 8
/
macos.rs
130 lines (106 loc) · 3.88 KB
/
macos.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
use std::{error, fmt};
use core_graphics::{
event::{CGEvent, CGEventTapLocation, CGEventType, CGMouseButton, ScrollEventUnit},
event_source::{CGEventSource, CGEventSourceStateID},
geometry::CGPoint,
};
use crate::types::{keys::*, Point};
impl From<CGPoint> for Point {
fn from(other: CGPoint) -> Point {
Point {
x: other.x as _,
y: other.y as _,
}
}
}
impl Into<CGPoint> for Point {
fn into(self) -> CGPoint {
CGPoint::new(self.x as _, self.y as _)
}
}
#[derive(Debug)]
pub enum Error<'a> {
CGEventNotCreated,
CGEventSourceStateInvalid,
InvalidButtonStr(&'a str),
}
impl<'a> error::Error for Error<'a> {}
impl<'a> fmt::Display for Error<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::CGEventNotCreated => write!(f, "CGEvent could not be created"),
Error::CGEventSourceStateInvalid => write!(f, "invalid CGEventSourceStateID"),
Error::InvalidButtonStr(button) => write!(f, "invalid button str: {:?}", button),
}
}
}
pub struct Mouse;
impl Mouse {
fn event_source<'a>() -> Result<CGEventSource, Error<'a>> {
Ok(
CGEventSource::new(CGEventSourceStateID::CombinedSessionState)
.or(Err(Error::CGEventSourceStateInvalid))?,
)
}
pub fn new() -> Mouse {
Mouse
}
pub fn move_to(&self, x: i32, y: i32) -> Result<(), Box<dyn error::Error>> {
let point = CGPoint::new(x as _, y as _);
CGEvent::new_mouse_event(
Self::event_source()?,
CGEventType::MouseMoved,
point,
CGMouseButton::Left, // ignored
)
.or(Err(Error::CGEventNotCreated))?
.post(CGEventTapLocation::HID);
Ok(())
}
pub fn press<'a>(&self, button: &'a Keys) -> Result<(), Box<dyn error::Error + 'a>> {
let (event_type, mouse_button) = match button {
Keys::LEFT => Ok((CGEventType::LeftMouseDown, CGMouseButton::Left)),
Keys::MIDDLE => Ok((CGEventType::OtherMouseDown, CGMouseButton::Center)),
Keys::RIGHT => Ok((CGEventType::RightMouseDown, CGMouseButton::Right)),
_ => Err(Box::new(Error::InvalidButtonStr(""))),
}?;
CGEvent::new_mouse_event(
Self::event_source()?,
event_type,
self.get_position()?.into(),
mouse_button,
)
.or(Err(Error::CGEventNotCreated))?
.post(CGEventTapLocation::HID);
Ok(())
}
pub fn release<'a>(&self, button: &'a Keys) -> Result<(), Box<dyn error::Error + 'a>> {
let (event_type, mouse_button) = match button {
Keys::LEFT => Ok((CGEventType::LeftMouseUp, CGMouseButton::Left)),
Keys::WHEEL | Keys::MIDDLE => Ok((CGEventType::OtherMouseUp, CGMouseButton::Center)),
Keys::RIGHT => Ok((CGEventType::RightMouseUp, CGMouseButton::Right)),
_ => Err(Box::new(Error::InvalidButtonStr(""))),
}?;
CGEvent::new_mouse_event(
Self::event_source()?,
event_type,
self.get_position()?.into(),
mouse_button,
)
.or(Err(Error::CGEventNotCreated))?
.post(CGEventTapLocation::HID);
Ok(())
}
pub fn get_position(&self) -> Result<Point, Box<dyn error::Error>> {
Ok(CGEvent::new(Self::event_source()?)
.or(Err(Error::CGEventNotCreated))?
.location()
.into())
}
pub fn wheel(&self, delta: i32) -> Result<(), Box<dyn error::Error>> {
CGEvent::new_scroll_event(Self::event_source()?, ScrollEventUnit::LINE, 1, delta, 0, 0)
.or(Err(Error::CGEventNotCreated))?
.post(CGEventTapLocation::HID);
Ok(())
}
}