-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickMacro.c
More file actions
413 lines (348 loc) · 8.91 KB
/
quickMacro.c
File metadata and controls
413 lines (348 loc) · 8.91 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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/* simple and fast x11 macro program *\
|* Copyright (C) 2024 Andrew Charles Marino *|
|* *|
|* This program is free software: you can redistribute it and/or modify *|
|* it under the terms of the GNU General Public License as published by *|
|* the Free Software Foundation, either version 3 of the License, or *|
|* (at your option) any later version. *|
|* *|
|* This program is distributed in the hope that it will be useful, *|
|* but WITHOUT ANY WARRANTY; without even the implied warranty of *|
|* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *|
|* GNU General Public License for more details. *|
|* *|
|* You should have received a copy of the GNU General Public License *|
\* along with this program. If not, see <https://www.gnu.org/licenses/>.*/
#define _POSIX_C_SOURCE 200809L
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <getopt.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <X11/Xlib.h>
#include <X11/extensions/XInput2.h>
#include <X11/extensions/XTest.h>
#define EPLAYRECORD die(22, "cannot play and record at the same time")
#define ERECORDLOOP die(22, "cannot loop while recording")
/* config */
/* keycode of start/end key */
const KeyCode QUITKEY = 134;
/* end config */
enum Type {
BUTTON,
KEY,
MOVE,
};
struct Event {
enum Type type;
Time time;
int keyCodeOrButtonOrX;
int yOrIsPress;
};
enum State {
NONE,
PLAY,
RECORD,
};
const char* shortopts = "hvlrp";
const struct option longopts[] = {
{"help", no_argument, NULL, 'h'},
{"version", no_argument, NULL, 'v'},
{"loop", no_argument, NULL, 'l'},
{"record", no_argument, NULL, 'r'},
{"play", no_argument, NULL, 'p'},
{NULL, 0, NULL, 0},
};
int xiOpcode;
void die(int ecode, const char* message, ...);
void* exitPlay(void* dpy);
enum State handleArgs(int argc, char** argv, int* loop);
int isfile(char* file);
void play(FILE* file, Display* dpy);
void record(FILE* file, Display* dpy);
void usage(void);
void version(void);
int
main(int argc, char** argv)
{
Display* dpy;
FILE* file;
int loop, dummy;
pthread_t thid;
enum State state;
state = NONE;
loop = 0;
state = handleArgs(argc, argv, &loop);
if (optind >= argc)
die(22, "no file specified");
if (state == PLAY && !isfile(argv[optind]))
die(21, "file specified is not a regular file");
file = fopen(argv[optind], state == RECORD ? "w" : "r");
if (file == NULL)
die (errno, "file open error: %s", strerror(errno));
if ((dpy = XOpenDisplay(NULL)) == NULL)
die(11, "falure connecting to X Server");
if (!XQueryExtension(dpy, "XInputExtension", &xiOpcode, &dummy, &dummy))
die(11, "X Input extension not available");
if (!XQueryExtension(dpy, "XTEST", &dummy, &dummy, &dummy))
die(11, "XTEST not available");
switch (state) {
case NONE:
usage();
die(22, "missing mode");
break;
case RECORD:
record(file, dpy);
break;
case PLAY:
if ((errno = pthread_create(&thid, NULL, exitPlay, dpy)) != 0)
die(errno, "quit thread creation failed: %s", strerror(errno));
pthread_detach(thid);
if (loop) {
while (1) {
play(file, dpy);
rewind(file);
}
} else
play(file, dpy);
break;
}
fclose(file);
XCloseDisplay(dpy);
return 0;
}
void
die(int ecode, const char* message, ...)
{
char messagep[strlen(message) + 13];
va_list ap;
strcpy(messagep, "quickMacro: ");
strcpy(messagep + 12, message);
va_start(ap, message);
vfprintf(stderr, messagep, ap);
va_end(ap);
exit(ecode);
}
void*
exitPlay(void* dpy)
{
XEvent ev;
XGenericEventCookie* cookie;
XIEventMask mask;
XIRawEvent* rawEv;
cookie = (XGenericEventCookie*)&ev.xcookie;
mask.deviceid = XIAllMasterDevices;
mask.mask_len = XIMaskLen(XI_LASTEVENT);
mask.mask = calloc(mask.mask_len, sizeof(char));
XISetMask(mask.mask, XI_RawKeyPress);
XISelectEvents(dpy, DefaultRootWindow(dpy), &mask, 1);
XSync(dpy, False);
free(mask.mask);
while (1) {
XNextEvent(dpy, &ev);
if (XGetEventData(dpy, cookie) && cookie->type == GenericEvent &&
cookie->extension == xiOpcode && cookie->evtype == XI_RawKeyPress) {
rawEv = cookie->data;
if (rawEv->detail == QUITKEY)
exit(0);
}
XFreeEventData(dpy, cookie);
}
}
enum State
handleArgs(int argc, char** argv, int* loop)
{
char ch;
enum State state;
state = NONE;
while ((ch = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) {
switch (ch) {
case 'h':
usage();
exit(0);
case 'v':
version();
exit(0);
case 'l':
if (state == RECORD) {
usage();
ERECORDLOOP;
}
*loop = 1;
break;
case 'p':
if (state == RECORD) {
usage();
EPLAYRECORD;
}
state = PLAY;
break;
case 'r':
if (state == PLAY) {
usage();
EPLAYRECORD;
}
if (*loop) {
usage();
ERECORDLOOP;
}
state = RECORD;
break;
case '?':
usage();
die(22, "unknown argument");
break;
default:
usage();
exit(0);
}
}
return state;
}
int
isfile(char* file)
{
struct stat file_stat;
stat(file, &file_stat);
return S_ISREG(file_stat.st_mode);
}
void
play(FILE* file, Display* dpy)
{
struct Event event;
unsigned long long time;
struct timespec ts;
time = 0;
while (fread(&event, sizeof(struct Event), 1, file)) {
switch (event.type) {
case BUTTON:
XTestFakeButtonEvent(dpy, event.keyCodeOrButtonOrX, event.yOrIsPress,
event.time);
break;
case KEY:
XTestFakeKeyEvent(dpy, event.keyCodeOrButtonOrX, event.yOrIsPress,
event.time);
break;
case MOVE:
XTestFakeMotionEvent(dpy, -1, event.keyCodeOrButtonOrX,
event.yOrIsPress, event.time);
break;
default:
die(-1, "unknown event type");
}
time += event.time;
}
ts.tv_sec = time / 1000;
ts.tv_nsec = (time % 1000) * 1000000;
nanosleep(&ts, NULL);
return;
}
void
record(FILE* file, Display* dpy)
{
struct Event event;
Time offset;
XEvent ev;
XGenericEventCookie* cookie;
XIDeviceEvent* devEv;
XIEventMask mask[2];
XIRawEvent* rawEv;
cookie = (XGenericEventCookie*)&ev.xcookie;
mask[0].deviceid = XIAllMasterDevices;
mask[0].mask_len = XIMaskLen(XI_LASTEVENT);
mask[0].mask = calloc(mask[0].mask_len, sizeof(char));
XISetMask(mask[0].mask, XI_RawKeyPress);
XISetMask(mask[0].mask, XI_RawKeyRelease);
XISetMask(mask[0].mask, XI_RawButtonPress);
XISetMask(mask[0].mask, XI_RawButtonRelease);
mask[1].deviceid = XIAllDevices;
mask[1].mask_len = XIMaskLen(XI_LASTEVENT);
mask[1].mask = calloc(mask[1].mask_len, sizeof(char));
XISetMask(mask[1].mask, XI_Motion);
XISelectEvents(dpy, DefaultRootWindow(dpy), mask, 2);
XSync(dpy, False);
free(mask[0].mask);
free(mask[1].mask);
while (1) {
XNextEvent(dpy, &ev);
if (XGetEventData(dpy, cookie) && cookie->type == GenericEvent &&
cookie->extension == xiOpcode && cookie->evtype == XI_RawKeyRelease) {
rawEv = cookie->data;
if (rawEv->detail == QUITKEY) {
offset = rawEv->time;
break;
}
}
XFreeEventData(dpy, cookie);
}
while (1) {
XNextEvent(dpy, &ev);
if (XGetEventData(dpy, cookie) && cookie->type == GenericEvent &&
cookie->extension == xiOpcode) {
switch (cookie->evtype) {
case XI_RawKeyPress:
case XI_RawKeyRelease:
rawEv = cookie->data;
if (rawEv->detail == QUITKEY) {
XFreeEventData(dpy, cookie);
return;
}
event.type = KEY;
break;
case XI_RawButtonPress:
case XI_RawButtonRelease:
rawEv = cookie->data;
event.type = BUTTON;
break;
case XI_Motion:
devEv = cookie->data;
event.type = MOVE;
break;
}
switch (cookie->evtype) {
case XI_RawKeyPress:
case XI_RawButtonPress:
event.yOrIsPress = 1;
event.keyCodeOrButtonOrX = rawEv->detail;
event.time = rawEv->time - offset;
break;
case XI_RawKeyRelease:
case XI_RawButtonRelease:
event.yOrIsPress = 0;
event.keyCodeOrButtonOrX = rawEv->detail;
event.time = rawEv->time - offset;
break;
case XI_Motion:
event.keyCodeOrButtonOrX = devEv->root_x;
event.yOrIsPress = devEv->root_y;
event.time = devEv->time - offset;
break;
}
fwrite(&event, sizeof(struct Event), 1, file);
offset = event.time + offset;
}
XFreeEventData(dpy, cookie);
}
}
void
usage(void)
{
puts("Usage: quickMacro MODE FILE [LOOP]\n"
"\n"
" -p, --play play macro file\n"
" -r, --record record macro file\n"
" -l, --loop loop macro until QUITKEY is pressed\n"
" -h, --help display this help and exit\n"
" -v, --version output version information and exit\n");
}
void
version(void)
{
puts("quickMacro (v"VERSION")\n"
"\n"
"written and gpl'd by Drew Marino (OneTrueC)\n");
}