forked from lodestone/macpaste
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmacpaste.c
313 lines (274 loc) · 9.42 KB
/
macpaste.c
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
// Public Domain License 2016
//
// Simulate right-handed unix/linux X11 middle-mouse-click copy and paste.
//
// References:
// http://stackoverflow.com/questions/3134901/mouse-tracking-daemon
// http://stackoverflow.com/questions/2379867/simulating-key-press-events-in-mac-os-x#2380280
//
// Compile with:
// gcc -framework ApplicationServices -o macpaste macpaste.c
//
// Start with:
// ./macpaste
//
// Terminate with Ctrl+C
#include <stdbool.h>
#include <sys/time.h> // gettimeofday
#include <search.h>
#include <ApplicationServices/ApplicationServices.h>
#include <Carbon/Carbon.h> // kVK_ANSI_*
char gIsDragging = 0;
long long gPrevClickTime = 0;
long long gCurClickTime = 0;
CGEventTapLocation gTapA = kCGAnnotatedSessionEventTap;
CGEventTapLocation gTapH = kCGHIDEventTap;
int gCommandKey = kCGEventFlagMaskCommand;
bool gVerbose = false;
bool gSkipLookups = true;
struct lookup {
bool skipWindow;
bool noFocus;
} lookup;
#define DOUBLE_CLICK_MILLIS 500
#define MAX_WINDOW_NAME_SIZE 400
long long now() {
struct timeval te;
gettimeofday(&te, NULL);
long long milliseconds = te.tv_sec*1000LL + te.tv_usec/1000; // caculate milliseconds
return milliseconds;
}
static bool getWindowUnderMouse(CGPoint *mouse, char *buf, size_t buf_len) {
int layer;
CFArrayRef windowList = CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly,
kCGNullWindowID);
CFIndex numWindows = CFArrayGetCount(windowList);
for(int i = 0; i < (int)numWindows; i++) {
CFDictionaryRef info = (CFDictionaryRef)CFArrayGetValueAtIndex(windowList, i);
CFStringRef appName = (CFStringRef)CFDictionaryGetValue(info, kCGWindowOwnerName);
CFNumberGetValue(
(CFNumberRef)CFDictionaryGetValue(info, kCGWindowLayer),
kCFNumberIntType, &layer);
if (appName != 0) {
CFStringGetCString(appName, buf, buf_len, kCFStringEncodingUTF8);
if (layer == 0) {
CGRect rect;
CFDictionaryRef bounds = (CFDictionaryRef)CFDictionaryGetValue(info,
kCGWindowBounds);
if(bounds) {
CGRectMakeWithDictionaryRepresentation(bounds, &rect);
if (mouse->x >= rect.origin.x &&
mouse->y >= rect.origin.y &&
mouse->x < rect.origin.x + rect.size.width &&
mouse->y < rect.origin.y + rect.size.height) {
CFRelease(windowList);
return true;
}
}
}
}
}
buf[0] = 0;
CFRelease(windowList);
return false;
}
#define GET_WINDOW_BOOL(field) \
char buffer[MAX_WINDOW_NAME_SIZE]; \
if (!getWindowUnderMouse(mouse, buffer, sizeof(buffer))) { \
printf("no window\n"); \
return false; \
} \
ENTRY e; \
ENTRY *ep; \
e.key = buffer; \
ep = hsearch(e, FIND); \
if (NULL == ep) { \
return false; \
} \
struct lookup *le = ep->data; \
if (gVerbose) { \
printf("%s: " #field " %d\n", buffer, le->field); \
} \
return le->field
static bool isSkipWindow(CGPoint *mouse) {
GET_WINDOW_BOOL(skipWindow);
}
static bool isNoFocusWindow(CGPoint *mouse) {
GET_WINDOW_BOOL(noFocus);
}
#undef GET_WINDOW_BOOL
static void paste(CGEventRef event) {
// Mouse click to focus and position insertion cursor.
CGPoint mouseLocation = CGEventGetLocation(event);
if (!isNoFocusWindow(&mouseLocation)) {
CGEventRef mouseClickDown = CGEventCreateMouseEvent(NULL, kCGEventLeftMouseDown,
mouseLocation,
kCGMouseButtonLeft);
CGEventRef mouseClickUp = CGEventCreateMouseEvent(NULL, kCGEventLeftMouseUp, mouseLocation,
kCGMouseButtonLeft);
CGEventPost(gTapH, mouseClickDown);
CGEventPost(gTapH, mouseClickUp);
CFRelease(mouseClickDown);
CFRelease(mouseClickUp);
}
if (isSkipWindow(&mouseLocation)) {
return;
}
// Allow click events time to position cursor before pasting.
usleep(1000);
// Paste.
CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateCombinedSessionState);
CGEventRef kbdEventPasteDown = CGEventCreateKeyboardEvent(source, kVK_ANSI_V, 1);
CGEventRef kbdEventPasteUp = CGEventCreateKeyboardEvent(source, kVK_ANSI_V, 0);
//CGEventSetFlags( kbdEventPasteDown, kCGEventFlagMaskCommand);
CGEventSetFlags(kbdEventPasteDown, gCommandKey);
CGEventPost(gTapA, kbdEventPasteDown);
CGEventPost(gTapA, kbdEventPasteUp);
CFRelease(kbdEventPasteDown);
CFRelease(kbdEventPasteUp);
CFRelease(source);
}
static void copy(CGEventRef event) {
CGPoint mouseLocation = CGEventGetLocation(event);
if (isSkipWindow(&mouseLocation)) {
return;
}
CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateCombinedSessionState);
CGEventRef kbdEventDown = CGEventCreateKeyboardEvent(source, kVK_ANSI_C, 1);
CGEventRef kbdEventUp = CGEventCreateKeyboardEvent(source, kVK_ANSI_C, 0);
//CGEventSetFlags(kbdEventDown, kCGEventFlagMaskCommand);
CGEventSetFlags(kbdEventDown, gCommandKey);
CGEventPost(gTapA, kbdEventDown);
CGEventPost(gTapA, kbdEventUp);
CFRelease(kbdEventDown);
CFRelease(kbdEventUp);
CFRelease(source);
}
static void recordClickTime() {
gPrevClickTime = gCurClickTime;
gCurClickTime = now();
}
static char isDoubleClickSpeed() {
return (gCurClickTime - gPrevClickTime) < DOUBLE_CLICK_MILLIS;
}
static char isDoubleClick() {
return isDoubleClickSpeed();
}
static CGEventRef mouseCallback (
CGEventTapProxy proxy,
CGEventType type,
CGEventRef event,
void * refcon
) {
switch (type) {
case kCGEventOtherMouseDown:
paste(event);
break;
case kCGEventLeftMouseDown:
recordClickTime();
break;
case kCGEventLeftMouseUp:
if (isDoubleClick() || gIsDragging) {
copy(event);
}
gIsDragging = 0;
break;
case kCGEventLeftMouseDragged:
gIsDragging = 1;
break;
default:
break;
}
// Pass on the event, we must not modify it anyway, we are a listener
return event;
}
int main (int argc, char **argv) {
CGEventMask emask;
CFMachPortRef myEventTap;
CFRunLoopSourceRef eventTapRLSrc;
if (argc > 1) {
if (0 == hcreate(argc + 10)) {
printf("Couldn't create hash table\n");
return -1;
}
int opt;
ENTRY e;
ENTRY *ep;
while ((opt = getopt(argc, argv, "vcn:s:")) != -1) {
switch (opt) {
case 'v':
gVerbose = true;
break;
case 'c':
gCommandKey = kCGEventFlagMaskControl;
printf("Using ctrl instead of cmd\n");
break;
#define SET_ARG(arg, not_arg) \
if (gSkipLookups) { \
gSkipLookups = false; \
} \
e.key = strdup(optarg); \
ep = hsearch(e, FIND); \
if (NULL == ep) { \
struct lookup *le = malloc(sizeof(lookup)); \
if (NULL == le) { \
printf("Couldn't allocate lookup entry\n"); \
return -1; \
} \
le->arg = true; \
le->not_arg = false; \
e.key = strdup(optarg); \
e.data = le; \
ep = hsearch(e, ENTER); \
if (NULL == ep) { \
printf("Failed to insert lookup entry for '%s'\n", optarg); \
return -1; \
} \
} else { \
struct lookup *le = ep->data; \
le->arg= true; \
}
case 'n':
printf("Won't focus window '%s'\n", optarg);
SET_ARG(noFocus, skipWindow);
break;
case 's':
printf("Will skip window '%s'\n", optarg);
SET_ARG(skipWindow, noFocus);
break;
#undef SET_ARG
}
}
}
printf("Quit from command-line foreground with Ctrl+C\n");
// We want "other" mouse button click-release, such as middle or exotic.
emask = CGEventMaskBit(kCGEventOtherMouseDown) |
CGEventMaskBit(kCGEventLeftMouseDown) |
CGEventMaskBit(kCGEventLeftMouseUp) |
CGEventMaskBit(kCGEventLeftMouseDragged);
// Create the Tap
myEventTap = CGEventTapCreate(
kCGSessionEventTap, // Catch all events for current user session
kCGTailAppendEventTap, // Append to end of EventTap list
kCGEventTapOptionListenOnly, // We only listen, we don't modify
emask,
& mouseCallback,
NULL // We need no extra data in the callback
);
// Create a RunLoop Source for it
eventTapRLSrc = CFMachPortCreateRunLoopSource(
kCFAllocatorDefault,
myEventTap,
0
);
// Add the source to the current RunLoop
CFRunLoopAddSource(
CFRunLoopGetCurrent(),
eventTapRLSrc,
kCFRunLoopDefaultMode
);
// Keep the RunLoop running forever
CFRunLoopRun();
// Not reached (RunLoop above never stops running)
return 0;
}