-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdarwin.go
More file actions
191 lines (152 loc) · 4.44 KB
/
Copy pathdarwin.go
File metadata and controls
191 lines (152 loc) · 4.44 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
//go:build darwin
package osctrl
/*
#cgo LDFLAGS: -framework ApplicationServices
#import <ApplicationServices/ApplicationServices.h>
static int GetNativeCursorPosition(double *x, double *y) {
CGEventRef event = CGEventCreate(NULL);
if (event == NULL) {
return 0;
}
CGPoint point = CGEventGetLocation(event);
*x = point.x;
*y = point.y;
CFRelease(event);
return 1;
}
// Warp the cursor position natively
static void NativeSetCursor(double x, double y) {
CGPoint pt = CGPointMake(x, y);
CGWarpMouseCursorPosition(pt);
CGAssociateMouseAndMouseCursorPosition(true);
}
// Fetch current cursor location for posting relative down/up events
static CGPoint GetCurrentPoint() {
CGEventRef event = CGEventCreate(NULL);
CGPoint pt = CGPointMake(0, 0);
if (event != NULL) {
pt = CGEventGetLocation(event);
CFRelease(event);
}
return pt;
}
// Post mouse button down/up events using primitive types
static void NativeMouseEvent(uint32_t type, uint32_t button) {
CGPoint pt = GetCurrentPoint();
CGEventRef event = CGEventCreateMouseEvent(
NULL,
(CGEventType)type,
pt,
(CGMouseButton)button
);
if (event != NULL) {
CGEventPost(kCGHIDEventTap, event);
CFRelease(event);
}
}
// Post keyboard press down/up events
static void NativeKeyEvent(CGKeyCode keycode, bool keyDown) {
CGEventRef event = CGEventCreateKeyboardEvent(NULL, keycode, keyDown);
if (event != NULL) {
CGEventPost(kCGHIDEventTap, event);
CFRelease(event);
}
}
*/
import "C"
import (
"github.com/shuffle/shuffle-shared"
"os"
"time"
"fmt"
)
var debug bool = os.Getenv("DEBUG") == "1"
// GetCursorPositionMacos returns the current cursor position in global screen
// coordinates using native macOS CoreGraphics. The origin (0,0) is the top-left of the
// primary display; coordinates increase right and down.
func GetCursorPositionMacos() (shuffle.Position, error) {
var x, y C.double
if ok := C.GetNativeCursorPosition(&x, &y); ok == 0 {
return shuffle.Position{}, fmt.Errorf("failed to retrieve cursor position via CoreGraphics")
}
return shuffle.Position{
X: float64(x),
Y: float64(y),
}, nil
}
//func remoteControlBatch(batch shuffle.RemoteControlActionBatch) error {
// return errors.New(fmt.Sprintf("remote control not implemented for %s. Verified for Windows only.", runtime.GOOS))
//}
func remoteControlBatch(batch shuffle.RemoteControlActionBatch) error {
for _, a := range batch.Actions {
remoteControlExecute(a)
}
return nil
}
func remoteControlExecute(a shuffle.RemoteControl) {
switch a.Op {
// -------- Mouse --------
case "mouse.move":
x := getInt(a.Params, "x")
y := getInt(a.Params, "y")
setCursor(x, y)
case "mouse.click":
x := getInt(a.Params, "x")
y := getInt(a.Params, "y")
button := getString(a.Params, "button")
delay := getInt(a.Params, "delay_ms")
setCursor(x, y)
time.Sleep(time.Duration(delay) * time.Millisecond)
mouseDown(button)
time.Sleep(50 * time.Millisecond)
mouseUp(button)
case "mouse.drag":
fx := getInt(a.Params, "from_x")
fy := getInt(a.Params, "from_y")
tx := getInt(a.Params, "to_x")
ty := getInt(a.Params, "to_y")
button := getString(a.Params, "button")
setCursor(fx, fy)
time.Sleep(50 * time.Millisecond)
mouseDown(button)
time.Sleep(50 * time.Millisecond)
setCursor(tx, ty)
time.Sleep(50 * time.Millisecond)
mouseUp(button)
// -------- Keyboard --------
case "keyboard.press":
key := getInt(a.Params, "key")
keyPress(uint16(key))
// -------- Utility --------
case "system.wait":
ms := getInt(a.Params, "ms")
time.Sleep(time.Duration(ms) * time.Millisecond)
}
}
func setCursor(x, y int) {
C.NativeSetCursor(C.double(x), C.double(y))
}
func mouseDown(button string) {
btn := C.kCGMouseButtonLeft
evtType := C.kCGEventLeftMouseDown
if button == "right" {
btn = C.kCGMouseButtonRight
evtType = C.kCGEventRightMouseDown
}
C.NativeMouseEvent(C.uint32_t(evtType), C.uint32_t(btn))
}
func mouseUp(button string) {
btn := C.kCGMouseButtonLeft
evtType := C.CGEventType(C.kCGEventLeftMouseUp)
if button == "right" {
btn = C.kCGMouseButtonRight
evtType = C.CGEventType(C.kCGEventRightMouseUp)
}
C.NativeMouseEvent(C.uint32_t(evtType), C.uint32_t(btn))
}
func keyPress(vk uint16) {
// macOS uses CGKeyCode (virtual keycodes)
C.NativeKeyEvent(C.CGKeyCode(vk), true)
time.Sleep(30 * time.Millisecond)
C.NativeKeyEvent(C.CGKeyCode(vk), false)
}