forked from jordansissel/xdotool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xdotool.c
642 lines (549 loc) · 17.6 KB
/
xdotool.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
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
/* xdotool
*
* command line interface to the xdo library
*
* getwindowfocus contributed by Lee Pumphret
* keyup/down contributed by Lee Pumphret
*
* vim:expandtab shiftwidth=2 softtabstop=2
*/
#define _GNU_SOURCE 1
#ifndef __USE_BSD
#define __USE_BSD /* for strdup on linux/glibc */
#endif /* __USE_BSD */
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <string.h>
#include <strings.h>
#include <errno.h>
#include <ctype.h>
#include <stdarg.h>
#include "xdo.h"
#include "xdotool.h"
static int script_main(int argc, char **argv);
static int args_main(int argc, char **argv);
int context_execute(context_t *context);
void consume_args(context_t *context, int argc);
void window_save(context_t *context, Window window);
void window_list(context_t *context, const char *window_arg,
Window **windowlist_ret, int *nwindows_ret,
const int add_to_list);
int window_get_arg(context_t *context, int min_arg, int window_arg_pos,
const char **window_arg);
int window_is_valid(context_t *context, const char *window_arg);
int is_command(char* cmd);
void xdotool_debug(context_t *context, const char *format, ...);
void xdotool_output(context_t *context, const char *format, ...);
void consume_args(context_t *context, int argc) {
if (argc > context->argc) {
fprintf(stderr,
"Can't consume %d args; are only %d available. This is a bug.\n",
argc, context->argc);
context->argv += context->argc;
context->argc = 0;
return;
}
context->argv += argc;
context->argc -= argc;
} /* void consume_args(context_t *, int) */
void window_save(context_t *context, Window window) {
if (context->windows != NULL) {
free(context->windows);
}
context->windows = calloc(1, sizeof(Window));
context->nwindows = 1;
context->windows[0] = window;
} /* void window_save(context_t *, Window) */
int window_is_valid(context_t *context, const char *window_arg) {
if (window_arg == NULL) {
return True;
}
if (window_arg[0] != '%') {
return True;
}
/* Selected a window with %N or %@, but are there windows on the stack? */
if (context->nwindows == 0) {
fprintf(stderr, "There are no windows in the stack\n");
return False;
}
if (window_arg[1] == '\0') {
fprintf(stderr, "Invalid window stack selection '%s'\n", window_arg);
return False;
}
if (window_arg[1] == '@') {
return True;
}
int window_index = atoi(window_arg + 1);
if (abs(window_index - 1) >= context->nwindows || (window_index == 0)) {
fprintf(stderr, "Invalid window stack selection '%s' (out of range)\n", window_arg);
return False;
}
return True;
} /* int window_is_valid(context_t *, const char *) */
int window_get_arg(context_t *context, int min_arg, int window_arg_pos,
const char **window_arg) {
if (context->argc < min_arg) {
fprintf(stderr, "Too few arguments (got %d, minimum is %d)\n",
context->argc, min_arg);
return False;
} else if (context->argc == min_arg) {
//fprintf(stderr, "Using default arg\n");
/* nothing, keep default */
} else if (context->argc > min_arg) {
if (is_command(context->argv[min_arg])) {
//fprintf(stderr, "arg is command, using default\n");
/* keep default */
} else {
/* got enough args, let's use the window you asked for */
//fprintf(stderr, "got enough args\n");
*window_arg = context->argv[window_arg_pos];
consume_args(context, 1);
}
}
if (!window_is_valid(context, *window_arg)) {
fprintf(stderr, "Invalid window '%s'\n", *window_arg);
return False;
}
return True;
} /* int window_get_arg(context_t *, int, int, char **, int *) */
void window_list(context_t *context, const char *window_arg,
Window **windowlist_ret, int *nwindows_ret,
const int add_to_list) {
/* If window_arg is NULL and we have windows in the list, use the list.
* If window_arg is "%@" and we have windows in the list, use the list.
* If window_arg is "%N" and we have windows in the list, use Nth window.
* 'N' above must be a positive number.
* Otherwise, assume it's a window id.
*
* TODO(sissel): Not implemented yet:
* If window_arg is "%r" it means the root window of the current screen.
* If window_arg is "%q" it means we will wait for you to select a window
* by clicking on it. (May not be necessary since we have 'selectwindow')
* If window_arg is "%c" it means the currently-active window.
*/
*nwindows_ret = 0;
*windowlist_ret = NULL;
if (window_arg != NULL && window_arg[0] == '%') {
if (context->nwindows == 0) {
fprintf(stderr, "There are no windows on the stack, Can't continue.\n");
return;
}
if (strlen(window_arg) < 2) {
fprintf(stderr, "Invalid window selection '%s'\n", window_arg);
return;
}
/* options.
* %N selects the Nth window. %1, %2, %-1 (last), %-2, etc.
* %@ selects all
*/
if (window_arg[1] == '@') {
*windowlist_ret = context->windows;
*nwindows_ret = context->nwindows;
} else if (window_arg[1] == 'q') {
/* TODO(sissel): Wait for you to click on the window. */
} else if (window_arg[1] == 'r') {
/* TODO(sissel): Get the root window of the current screen */
} else if (window_arg[1] == 'c') {
/* TODO(sissel): Get the current window */
} else {
/* Otherwise assume %N */
int window_index = atoi(window_arg + 1);
if (window_index < 0) {
/* negative offset */
window_index = context->nwindows + window_index;
}
if (window_index > context->nwindows || window_index <= 0) {
fprintf(stderr, "%d is out of range (only %d windows in list)\n",
window_index, context->nwindows);
return;
}
/* Subtract 1 since %1 is the first window in the list */
context->window_placeholder[0] = context->windows[window_index - 1];
*windowlist_ret = context->window_placeholder;
*nwindows_ret = 1;
}
} else {
/* Otherwise, window_arg is either invalid or null. Default to CURRENTWINDOW
*/
/* We can't return a pointer to a piece of the stack in this function,
* so we'll store the window in the context_t and return a pointer
* to that.
*/
Window window = CURRENTWINDOW;
if (window_arg != NULL) {
window = (Window)strtol(window_arg, NULL, 0);
}
context->window_placeholder[0] = window;
*nwindows_ret = 1;
*windowlist_ret = context->window_placeholder;
}
if (add_to_list) {
/* save the window to the windowlist */
}
}
struct dispatch {
const char *name;
int (*func)(context_t *context);
} dispatch[] = {
/* Query functions */
{ "getactivewindow", cmd_getactivewindow, },
{ "getwindowfocus", cmd_getwindowfocus, },
{ "getwindowname", cmd_getwindowname, },
{ "getwindowpid", cmd_getwindowpid, },
{ "getwindowgeometry", cmd_getwindowgeometry, },
{ "getdisplaygeometry", cmd_get_display_geometry, },
{ "search", cmd_search, },
{ "selectwindow", cmd_window_select, },
/* Help me! */
{ "help", cmd_help, },
{ "version", cmd_version, },
/* Action functions */
{ "behave", cmd_behave, },
{ "behave_screen_edge", cmd_behave_screen_edge, },
{ "click", cmd_click, },
{ "getmouselocation", cmd_getmouselocation, },
{ "key", cmd_key, },
{ "keydown", cmd_key, },
{ "keyup", cmd_key, },
{ "mousedown", cmd_mousedown, },
{ "mousemove", cmd_mousemove, },
{ "mousemove_relative", cmd_mousemove_relative, },
{ "mouseup", cmd_mouseup, },
{ "set_window", cmd_set_window, },
{ "type", cmd_type, },
{ "windowactivate", cmd_windowactivate, },
{ "windowfocus", cmd_windowfocus, },
{ "windowkill", cmd_windowkill, },
{ "windowclose", cmd_windowclose, },
{ "windowmap", cmd_windowmap, },
{ "windowminimize", cmd_windowminimize, },
{ "windowmove", cmd_windowmove, },
{ "windowraise", cmd_windowraise, },
{ "windowreparent", cmd_windowreparent, },
{ "windowsize", cmd_windowsize, },
{ "windowstate", cmd_windowstate, },
{ "windowunmap", cmd_windowunmap, },
{ "set_num_desktops", cmd_set_num_desktops, },
{ "get_num_desktops", cmd_get_num_desktops, },
{ "set_desktop", cmd_set_desktop, },
{ "get_desktop", cmd_get_desktop, },
{ "set_desktop_for_window", cmd_set_desktop_for_window, },
{ "get_desktop_for_window", cmd_get_desktop_for_window, },
{ "get_desktop_viewport", cmd_get_desktop_viewport, },
{ "set_desktop_viewport", cmd_set_desktop_viewport, },
{ "exec", cmd_exec, },
{ "sleep", cmd_sleep, },
{ NULL, NULL, },
};
int is_command(char* cmd) {
int i;
for (i = 0; dispatch[i].name != NULL; i++) {
if (!strcasecmp(dispatch[i].name, cmd)) {
return 1;
}
}
return 0;
}
int main(int argc, char **argv) {
return xdotool_main(argc, argv);
}
int xdotool_main(int argc, char **argv) {
/* If argv[1] is a file or "-", read commands from file or stdin,
* else use commands from argv.
*/
struct stat data;
int stat_ret;
if (argc >= 2) {
/* See if the first argument is an existing file */
stat_ret = stat(argv[1], &data);
int i = 0;
int argv1_is_command= 0;
for (i = 0; dispatch[i].name != NULL; i++) {
if (!strcasecmp(dispatch[i].name, argv[1])) {
argv1_is_command = 1;
break;
}
}
if (!argv1_is_command && (strcmp(argv[1], "-") == 0 || stat_ret == 0)) {
return script_main(argc, argv);
}
}
return args_main(argc, argv);
}
int script_main(int argc, char **argv) {
/* Tokenize the input file while expanding positional parameters and
* environment variables. Pass the resulting argument list to
* args_main().
*/
FILE *input = NULL;
const char *path = argv[1];
char buffer[4096];
char **script_argv = (char **) calloc(1, sizeof(char *));
int script_argc = 0;
int script_argc_max = 0;
/* determine whether reading from a file or from stdin */
if (!strcmp(path, "-")) {
input = fdopen(0, "r");
} else {
input = fopen(path, "r");
if (input == NULL) {
fprintf(stderr, "Failure opening '%s': %s\n", path, strerror(errno));
return EXIT_FAILURE;
}
}
context_t context;
context.xdo = xdo_new(NULL);
context.prog = *argv;
context.windows = NULL;
context.nwindows = 0;
context.have_last_mouse = False;
context.debug = (getenv("DEBUG") != NULL);
if (context.xdo == NULL) {
fprintf(stderr, "Failed creating new xdo instance\n");
return 1;
}
context.xdo->debug = context.debug;
/* read input... */
int pos;
char *token;
int result;
while (fgets(buffer, 4096, input) != NULL) {
char *line = buffer;
token = NULL;
/* Ignore leading whitespace */
line += strspn(line, " \t");
/* blanklines or line comment are ignored, too */
if (line[0] == '\n' || line[0] == '#') {
continue;
}
/* replace newline with null */
if (line[strlen(line)-1] == '\n')
line[strlen(line)-1] = '\0';
/* tokenize line into script_argv... */
while (strlen(line)) {
token = NULL;
/* modify line to contain the current token. Tokens are
* separated by whitespace, or quoted with single/double quotes.
*/
if (line[0] == '"') {
line++;
line[strcspn(line, "\"")] = '\0';
}
else if (line[0] == '\'') {
line++;
line[strcspn(line, "\'")] = '\0';
}
else {
line[strcspn(line, " \t")] = '\0';
}
/* if a token begins with "$", append the corresponding
* positional parameter or environment variable to
* script_argv...
*/
if (line[0] == '$') {
/* ignore dollar sign */
line++;
if (isdigit(line[0])) {
/* get the position of this parameter in argv */
pos = atoi(line) + 1; /* $1 is actually index 2 in the argv array */
/* bail if no argument was given for this parameter */
if (pos >= argc) {
fprintf (stderr, "%s: error: `%s' needs at least %d %s; only %d given\n",
argv[0], argv[1], pos - 1, pos == 2 ? "argument" : "arguments",
argc - 2);
return EXIT_FAILURE;
}
/* use command line argument */
token = argv[pos];
}
else {
/* use environment variable */
token = getenv(line);
if (token == NULL) {
/* since it's not clear what we should do if this env var is not
* present, let's abort */
fprintf(stderr, "%s: error: environment variable $%s is not set.\n",
argv[0], line);
return EXIT_FAILURE;
}
}
}
else {
/* use the verbatim token */
token = line;
}
/* append token */
if (token != NULL) {
if(script_argc + 1 > script_argc_max){
script_argv = realloc(script_argv, (script_argc + 1) * sizeof(char *));
script_argc_max++;
}
if (script_argv == NULL) {
fprintf(stderr, "%s: error: failed to allocate memory while parsing `%s'.\n",
argv[0], argv[1]);
exit(EXIT_FAILURE);
}
script_argv[script_argc] = (char *) calloc(strlen(token) + 1, sizeof(char));
//printf("arg %d: %s\n", script_argc, token);
strncpy(script_argv[script_argc], token, strlen(token)+1);
script_argc++;
}
/* advance line to the next token */
line += strlen(line) + 1;
line += strspn(line, " \t");
} /* while line being tokenized */
/*
* Add NULL at the end and reallocate memory if necessary.
*/
if(script_argc_max <= script_argc){
script_argv = realloc(script_argv, (script_argc+1) * sizeof(char *));
/* TODO(sissel): STOPPED HERE */
script_argc_max++;
}
*(script_argv + script_argc) = NULL;
if(script_argc > 0){
context.argc = script_argc;
context.argv = script_argv;
result = context_execute(&context);
/*
* Free the allocated memory for tokens.
*/
for(int j = 0; j < script_argc; j++){
if(*(script_argv + j) != NULL){
free(*(script_argv + j));
}
}
script_argc = 0;
*script_argv = NULL;
}
}
fclose(input);
xdo_free(context.xdo);
if (context.windows != NULL) {
free(context.windows);
}
for(int i=0; i<script_argc+1; ++i) {
free(script_argv[i]);
}
free(script_argv);
return result;
}
int args_main(int argc, char **argv) {
int ret = 0;
int opt;
int option_index;
const char *usage = "Usage: %s <cmd> <args>\n";
static struct option long_options[] = {
{ "help", no_argument, NULL, 'h' },
{ "version", no_argument, NULL, 'v' },
{ 0, 0, 0, 0 }
};
if (argc < 2) {
fprintf(stderr, usage, argv[0]);
cmd_help(NULL);
exit(1);
}
//for(i = 0; i<argc; i++) {
//fprintf(stderr, "argv[%d] = \"%s\"\n", i, argv[i]);
//}
while ((opt = getopt_long_only(argc, argv, "++hv", long_options, &option_index)) != -1) {
switch (opt) {
case 'h':
cmd_help(NULL);
exit(EXIT_SUCCESS);
case 'v':
cmd_version(NULL);
exit(EXIT_SUCCESS);
default:
fprintf(stderr, usage, argv[0]);
exit(EXIT_FAILURE);
}
}
context_t context;
context.xdo = xdo_new(NULL);
context.prog = *argv;
argv++; argc--;
context.argc = argc;
context.argv = argv;
context.windows = NULL;
context.nwindows = 0;
context.have_last_mouse = False;
context.debug = (getenv("DEBUG") != NULL);
if (context.xdo == NULL) {
fprintf(stderr, "Failed creating new xdo instance\n");
return 1;
}
context.xdo->debug = context.debug;
ret = context_execute(&context);
xdo_free(context.xdo);
if (context.windows != NULL) {
free(context.windows);
}
return ret;
} /* int args_main(int, char **) */
int context_execute(context_t *context) {
int cmd_found = 0;
int i = 0;
char *cmd = NULL;
int ret = XDO_SUCCESS;
/* Loop until all argv is consumed. */
while (context->argc > 0 && ret == XDO_SUCCESS) {
cmd = context->argv[0];
cmd_found = 0;
for (i = 0; dispatch[i].name != NULL && !cmd_found; i++) {
if (!strcasecmp(dispatch[i].name, cmd)) {
cmd_found = 1;
optind = 0;
if (context->debug) {
fprintf(stderr, "command: %s\n", cmd);
}
ret = dispatch[i].func(context);
}
}
if (!cmd_found) {
fprintf(stderr, "%s: Unknown command: %s\n", context->prog, cmd);
fprintf(stderr, "Run '%s help' if you want a command list\n", context->prog);
ret = 1;
}
} /* while ... */
return ret;
} /* int args_main(int, char **) */
int cmd_help(context_t *context) {
int i;
printf("Available commands:\n");
for (i = 0; dispatch[i].name != NULL; i++)
printf(" %s\n", dispatch[i].name);
/* "help" can be invoked on errors, like when xdotool is given no arguments,
* so let's make sure we only consume if we have a context */
if (context != NULL) {
consume_args(context, 1);
}
return 0;
}
int cmd_version(context_t *context) {
xdotool_output(context, "xdotool version %s", xdo_version());
if (context != NULL) {
consume_args(context, 1);
}
return 0;
}
void xdotool_debug(context_t *context, const char *format, ...) {
va_list args;
va_start(args, format);
if (context->debug) {
vfprintf(stderr, format, args);
fprintf(stderr, "\n");
}
} /* xdotool_debug */
void xdotool_output(context_t *context, const char *format, ...) {
context = context; /* Do something with context to avoid warnings */
va_list args;
va_start(args, format);
vfprintf(stdout, format, args);
fprintf(stdout, "\n");
fflush(stdout);
} /* xdotool_output */