source: trunk/xdotool/xdotool.c @ 1088

Last change on this file since 1088 was 1088, checked in by dkg, 5 years ago

xdotool: re-instating failure when used with missing commands.

File size: 13.7 KB
Line 
1/* xdotool
2 *
3 * command line interface to the xdo library
4 *
5 * $Id: xdotool.c 1891 2008-06-03 09:00:20Z jordansissel $
6 *
7 * getwindowfocus contributed by Lee Pumphret
8 * keyup/down contributed by Lee Pumphret
9 *
10 * XXX: Need to use 'Window' instead of 'int' where appropriate.
11 */
12
13#include <stdio.h>
14#include <stdlib.h>
15#include <getopt.h>
16#include <string.h>
17#include <strings.h>
18
19
20#include "xdo.h"
21
22int cmd_click(int argc, char **args);
23int cmd_getwindowfocus(int argc, char **args);
24int cmd_help(int argc, char **args);
25int cmd_key(int argc, char **args);
26int cmd_mousedown(int argc, char **args);
27int cmd_mousemove(int argc, char **args);
28int cmd_mousemove_relative(int argc, char **args);
29int cmd_mouseup(int argc, char **args);
30int cmd_search(int argc, char **args);
31int cmd_type(int argc, char **args);
32int cmd_windowactivate(int argc, char **args);
33int cmd_windowfocus(int argc, char **args);
34int cmd_windowmap(int argc, char **args);
35int cmd_windowmove(int argc, char **args);
36int cmd_windowraise(int argc, char **args);
37int cmd_windowsize(int argc, char **args);
38int cmd_windowunmap(int argc, char **args);
39
40/* pager-like commands */
41int cmd_set_num_desktops(int argc, char **args);
42int cmd_get_num_desktops(int argc, char **args);
43int cmd_set_desktop(int argc, char **args);
44int cmd_get_desktop(int argc, char **args);
45int cmd_set_desktop_for_window(int argc, char **args);
46int cmd_get_desktop_for_window(int argc, char **args);
47
48xdo_t *xdo;
49void window_print(Window wid);
50
51struct dispatch {
52  const char *name;
53  int (*func)(int argc, char **args);
54} dispatch[] = {
55  /* Query functions */
56  { "getwindowfocus", cmd_getwindowfocus, },
57  { "search", cmd_search, },
58
59  /* Help me! */
60  { "help", cmd_help, },
61  { "-h", cmd_help, },
62
63  /* Action functions */
64  { "click", cmd_click, },
65  { "key", cmd_key, },
66  { "keydown", cmd_key, },
67  { "keyup", cmd_key, },
68  { "mousedown", cmd_mousedown, },
69  { "mousemove", cmd_mousemove, },
70  { "mousemove_relative", cmd_mousemove_relative, },
71  { "mouseup", cmd_mouseup, },
72  { "type", cmd_type, },
73  { "windowactivate", cmd_windowactivate, },
74  { "windowfocus", cmd_windowfocus, },
75  { "windowmap", cmd_windowmap, },
76  { "windowmove", cmd_windowmove, },
77  { "windowraise", cmd_windowraise, },
78  { "windowsize", cmd_windowsize, },
79  { "windowunmap", cmd_windowunmap, },
80
81  { "set_num_desktops", cmd_set_num_desktops, },
82  { "get_num_desktops", cmd_get_num_desktops, },
83  { "set_desktop", cmd_set_desktop, },
84  { "get_desktop", cmd_get_desktop, },
85  { "set_desktop_for_window", cmd_set_desktop_for_window, },
86  { "get_desktop_for_window", cmd_get_desktop_for_window, },
87  { NULL, NULL, },
88};
89
90int main(int argc, char **argv) {
91  char *cmd;
92  int ret = 0;
93  int i;
94  int triggered = 0;
95
96  if (argc < 2) {
97    fprintf(stderr, "usage: %s <cmd> <args>\n", argv[0]);
98    cmd_help(0, NULL);
99    exit(1);
100  }
101
102  cmd = *++argv; /* argv[1] */
103  argc -= 1; /* ignore arg0 (program name) */
104
105  xdo = xdo_new(getenv("DISPLAY"));
106  if (xdo == NULL) {
107    fprintf(stderr, "Failed creating new xdo instance\n");
108    return 1;
109  }
110
111  for (i = 0; dispatch[i].name != NULL; i++) {
112    if (!strcasecmp(dispatch[i].name, cmd)) {
113      ret = dispatch[i].func(argc, argv);
114      triggered = 1;
115      break;
116    }
117  }
118
119  xdo_free(xdo);
120  if (!triggered) { 
121    fprintf(stderr, "No such command '%s'\n", cmd); 
122    return 1; 
123  } 
124  return ret;
125}
126
127void window_print(Window wid) {
128  /* Window is XID is 'unsigned long' or CARD32 */
129  printf("%ld\n", wid);
130}
131
132int cmd_help(int argc, char **args) {
133  int i;
134  printf("Available commands:\n");
135  for (i = 0; dispatch[i].name != NULL; i++)
136    printf("  %s\n", dispatch[i].name);
137
138  return 0;
139}
140
141int cmd_mousemove(int argc, char **args) {
142  int ret = 0;
143  int x, y;
144  char *cmd = *args; argc--; args++;
145
146  if (argc != 2) {
147    fprintf(stderr, "usage: %s <xcoord> <ycoord>\n", cmd);
148    fprintf(stderr, "You specified the wrong number of args.\n");
149    return 1;
150  }
151
152  x = atoi(args[0]);
153  y = atoi(args[1]);
154
155  ret = xdo_mousemove(xdo, x, y);
156
157  if (ret)
158    fprintf(stderr, "xdo_mousemove reported an error\n");
159
160  return ret;
161}
162
163int cmd_mousemove_relative(int argc, char **args) {
164  int x, y;
165  int ret = 0;
166  char *cmd = *args; argc--; args++;
167
168  if (argc != 2) {
169    fprintf(stderr, "usage: %s <xcoord> <ycoord>\n", cmd);
170    fprintf(stderr, "You specified the wrong number of args.\n");
171    return 1;
172  }
173
174  x = atoi(args[0]);
175  y = atoi(args[1]);
176
177  ret = xdo_mousemove_relative(xdo, x, y);
178
179  if (ret)
180    fprintf(stderr, "xdo_mousemove_relative reported an error\n");
181
182  return ret;
183}
184
185int cmd_mousedown(int argc, char **args) {
186  int ret = 0;
187  int button;
188  char *cmd = *args; argc--; args++;
189
190  if (argc != 1) {
191    fprintf(stderr, "usage: %s <button>\n", cmd);
192    fprintf(stderr, "You specified the wrong number of args.\n");
193    return 1;
194  }
195
196  button = atoi(args[0]);
197
198  ret = xdo_mousedown(xdo, button);
199
200  if (ret)
201    fprintf(stderr, "xdo_mousedown reported an error\n");
202
203  return ret;
204}
205
206int cmd_mouseup(int argc, char **args) {
207  int ret = 0;
208  int button;
209  char *cmd = *args; argc--; args++;
210
211  if (argc != 1) {
212    fprintf(stderr, "usage: %s <button>\n", cmd);
213    fprintf(stderr, "You specified the wrong number of args.\n");
214    return 1;
215  }
216
217  button = atoi(args[0]);
218
219  ret = xdo_mouseup(xdo, button);
220  if (ret)
221    fprintf(stderr, "xdo_mouseup reported an error\n");
222 
223  return ret;
224}
225
226int cmd_click(int argc, char **args) {
227  int button;
228  char *cmd = *args; argc--; args++;
229
230  if (argc != 1) {
231    fprintf(stderr, "usage: %s <button>\n", cmd);
232    fprintf(stderr, "You specified the wrong number of args.\n");
233    return 1;
234  }
235
236  button = atoi(args[0]);
237  return xdo_click(xdo, button);
238}
239
240int cmd_type(int argc, char **args) {
241  int ret = 0;
242  int i;
243  char *cmd = *args; argc--; args++;
244
245  if (argc == 0) {
246    fprintf(stderr, "usage: %s <things to type>\n", cmd);
247    fprintf(stderr, "You specified the wrong number of args.\n");
248    return 1;
249  }
250
251  for (i = 0; i < argc; i++) {
252    int tmp = xdo_type(xdo, args[i]);
253
254    if (tmp) {
255      fprintf(stderr, "xdo_type reported an error\n");
256    }
257
258    ret += tmp;
259  }
260
261  return ret > 0;
262}
263
264int cmd_key(int argc, char **args) {
265  int ret = 0;
266  int i;
267  char *cmd = *args; argc--; args++;
268
269  if (argc == 0) {
270    fprintf(stderr, "usage: %s <keyseq1> [keyseq2 ... keyseqN]\n", cmd);
271    fprintf(stderr, "You specified the wrong number of args.\n");
272    return 1;
273  }
274
275  int (*func)(xdo_t *, char *) = NULL;
276
277  if (!strcmp(cmd, "key")) {
278    func = xdo_keysequence;
279  } else if (!strcmp(cmd, "keyup")) {
280    func = xdo_keysequence_up;
281  } else if (!strcmp(cmd, "keydown")) {
282    func = xdo_keysequence_down;
283  } else {
284    fprintf(stderr, "Unknown command '%s'\n", cmd);
285    return 1;
286  }
287
288  for (i = 0; i < argc; i++) {
289    int tmp = func(xdo, args[i]);
290    if (tmp != 0)
291      fprintf(stderr, "xdo_keysequence reported an error for string '%s'\n", args[i]);
292    ret += tmp;
293  }
294
295  return ret;
296}
297
298int cmd_windowmove(int argc, char **args) {
299  int ret = 0;
300  int x, y;
301  Window wid;
302  char *cmd = *args; argc--; args++;
303
304  if (argc != 3) {
305    printf("usage: %s wid x y\n", cmd);
306    return 1;
307  }
308
309  wid = (Window)strtol(args[0], NULL, 0);
310  x = (int)strtol(args[1], NULL, 0);
311  y = (int)strtol(args[2], NULL, 0);
312
313  ret = xdo_window_move(xdo, wid, x, y);
314  if (ret)
315    fprintf(stderr, "xdo_window_move reported an error\n");
316 
317  return ret;
318}
319
320int cmd_windowactivate(int argc, char **args) {
321  int ret = 0;
322  Window wid;
323  char *cmd = *args; argc--; args++;
324
325  if (argc != 1) {
326    printf("usage: %s wid\n", cmd);
327    return 1;
328  }
329
330  wid = (Window)strtol(args[0], NULL, 0);
331  ret = xdo_window_activate(xdo, wid);
332
333  if (ret)
334    fprintf(stderr, "xdo_window_activate reported an error\n");
335
336  return ret;
337}
338
339int cmd_windowfocus(int argc, char **args) {
340  int ret = 0;
341  Window wid;
342  char *cmd = *args; argc--; args++;
343
344  if (argc != 1) {
345    printf("usage: %s wid\n", cmd);
346    return 1;
347  }
348
349  wid = (Window)strtol(args[0], NULL, 0);
350  ret = xdo_window_focus(xdo, wid);
351  if (ret)
352    fprintf(stderr, "xdo_window_focus reported an error\n");
353 
354  return ret;
355}
356
357int cmd_windowraise(int argc, char **args) {
358  int ret = 0;
359  Window wid;
360  char *cmd = *args; argc--; args++;
361
362  if (argc != 1) {
363    printf("usage: %s wid\n", cmd);
364    return 1;
365   }
366
367  wid = (Window)strtol(args[0], NULL, 0);
368  ret = xdo_window_raise(xdo, wid);
369  if (ret)
370    fprintf(stderr, "xdo_window_raise reported an error\n");
371 
372  return ret;
373}
374
375int cmd_windowsize(int argc, char **args) {
376  int ret = 0;
377  int width, height;
378  Window wid;
379  int c;
380
381  int use_hints = 0;
382  struct option longopts[] = {
383    { "usehints", 0, &use_hints, 1 },
384    { 0, 0, 0, 0 },
385  };
386
387  int size_flags = 0;
388  char *cmd = *args;
389
390  while (1) {
391    int option_index;
392
393    c = getopt_long_only(argc, args, "", longopts, &option_index);
394
395    if (c == -1)
396      break;
397  }
398
399  if (use_hints)
400    size_flags |= SIZE_USEHINTS;
401
402  args += optind;
403  argc -= optind;
404
405  if (argc != 3) {
406    printf("usage: %s wid width height\n", cmd);
407    return 1;
408  }
409
410  wid = (Window)strtol(args[0], NULL, 0);
411  width = (int)strtol(args[1], NULL, 0);
412  height = (int)strtol(args[2], NULL, 0);
413
414  ret = xdo_window_setsize(xdo, wid, width, height, size_flags);
415  if (ret)
416    fprintf(stderr, "xdo_window_setsize reported an error\n");
417  return ret;
418}
419
420int cmd_search(int argc, char **args) {
421  Window *list;
422  int nwindows;
423  int i;
424  int c;
425
426  int only_visible = 0;
427  int search_title = 0;
428  int search_name = 0;
429  int search_class = 0;
430  struct option longopts[] = {
431    { "onlyvisible", 0, &only_visible, 1 },
432    { "title", 0, &search_title, 1 },
433    { "name", 0, &search_name, 1 },
434    { "class", 0, &search_class, 1 },
435    { 0, 0, 0, 0 },
436  };
437
438  int search_flags = 0;
439
440  char *cmd = *args;
441
442  while (1) {
443    int option_index;
444
445    c = getopt_long_only(argc, args, "", longopts, &option_index);
446
447    if (c == -1)
448      break;
449  }
450
451  if (only_visible)
452    search_flags |= SEARCH_VISIBLEONLY;
453  if (search_title)
454    search_flags |= SEARCH_TITLE;
455  if (search_name)
456    search_flags |= SEARCH_NAME;
457  if (search_class)
458    search_flags |= SEARCH_CLASS;
459
460  args += optind;
461  argc -= optind;
462
463  if (argc != 1) {
464    printf(
465      "Usage: xdotool %s "
466      "[--onlyvisible] [--title --class --name] regexp_pattern\n"
467      " --onlyvisible   matches only windows currently visible\n"
468      " --title         check regexp_pattern agains the window title\n"
469      " --class         check regexp_pattern agains the window class\n"
470      " --name          check regexp_pattern agains the window name\n"
471      "\n"
472      "* If none of --title, --class, and --name are specified,\n"
473      "the defaults are to match any of them.\n", 
474      cmd);
475    return 1;
476  }
477
478  xdo_window_list_by_regex(xdo, *args, search_flags, &list, &nwindows);
479  for (i = 0; i < nwindows; i++)
480    window_print(list[i]);
481
482  /* Free list as it's malloc'd by xdo_window_list_by_regex */
483  free(list);
484
485  /* error if number of windows found is zero (behave like grep) */
486  return !nwindows;
487}
488
489/* Added 2007-07-28 - Lee Pumphret */
490int cmd_getwindowfocus(int argc, char **args) {
491  int ret = 0;
492  Window wid = 0;
493  char *cmd = *args; argc--; args++;
494
495  if (argc != 0) {
496    printf("usage: %s\n", cmd);
497    return 1;
498  }
499
500  ret = xdo_window_get_focus(xdo, &wid);
501
502  if (ret) {
503    fprintf(stderr, "xdo_window_focus reported an error\n");
504  } else { 
505    window_print(wid);
506  }
507
508  return ret;
509}
510
511int cmd_windowmap(int argc, char **args) {
512  int ret = 0;
513  Window wid;
514  char *cmd = *args; argc--; args++;
515
516  if (argc != 1) {
517    printf("usage: %s wid\n", cmd);
518    return 1;
519  }
520
521  wid = (Window)strtol(args[0], NULL, 0);
522  ret = xdo_window_map(xdo, wid);
523  if (ret)
524    fprintf(stderr, "xdo_window_map reported an error\n");
525 
526  return ret;
527}
528
529int cmd_windowunmap(int argc, char **args) {
530  int ret = 0;
531  Window wid;
532  char *cmd = *args; argc--; args++;
533
534  if (argc != 1) {
535    printf("usage: %s wid\n", cmd);
536    return 1;
537  }
538
539  wid = (Window)strtol(args[0], NULL, 0);
540  ret = xdo_window_unmap(xdo, wid);
541  if (ret)
542    fprintf(stderr, "xdo_window_unmap reported an error\n");
543 
544  return ret;
545}
546
547int cmd_set_num_desktops(int argc, char **args) {
548  char *cmd = *args; argc--; args++;
549  long ndesktops;
550
551  if (argc != 1) {
552    printf("usage: %s num_desktops\n", cmd);
553    return 1;
554  }
555
556  ndesktops = strtol(args[0], NULL, 0);
557
558  return xdo_set_number_of_desktops(xdo, ndesktops);
559}
560
561int cmd_get_num_desktops(int argc, char **args) {
562  int ret = 0;
563  char *cmd = *args; argc--; args++;
564  long ndesktops = 0;
565
566  if (argc != 0) {
567    printf("usage: %s\n", cmd);
568    return 1;
569  }
570
571  ret = xdo_get_number_of_desktops(xdo, &ndesktops);
572
573  printf("%ld\n", ndesktops);
574  return ret;
575}
576
577int cmd_set_desktop(int argc, char **args) {
578  char *cmd = *args; argc--; args++;
579  long desktop;
580
581  if (argc != 1) {
582    printf("usage: %s desktop\n", cmd);
583    return 1;
584  }
585
586  desktop = strtol(args[0], NULL, 0);
587
588  return xdo_set_current_desktop(xdo, desktop);
589}
590
591int cmd_get_desktop(int argc, char **args) {
592  int ret = 0;
593  char *cmd = *args; argc--; args++;
594  long desktop = 0;
595
596  if (argc != 0) {
597    printf("usage: %s\n", cmd);
598    return 1;
599  }
600
601  ret = xdo_get_current_desktop(xdo, &desktop);
602  printf("%ld\n", desktop);
603  return ret;
604}
605
606int cmd_set_desktop_for_window(int argc, char **args) {
607  char *cmd = *args; argc--; args++;
608  long desktop = 0;
609  Window window = 0;
610
611  if (argc != 2) {
612    printf("usage: %s <window> <desktop>\n", cmd);
613    return 1;
614  }
615
616  window = (Window)strtol(args[0], NULL, 0);
617  desktop = strtol(args[1], NULL, 0);
618
619  return xdo_set_desktop_for_window(xdo, window, desktop);
620}
621
622int cmd_get_desktop_for_window(int argc, char **args) {
623  int ret = 0;
624  char *cmd = *args; argc--; args++;
625  long desktop = 0;
626  Window window = 0;
627
628  if (argc != 1) {
629    printf("usage: %s <window>\n", cmd);
630    return 1;
631  }
632
633  window = (Window)strtol(args[0], NULL, 0);
634
635  ret = xdo_get_desktop_for_window(xdo, window, &desktop);
636  printf("%ld\n", desktop);
637  return ret;
638}
Note: See TracBrowser for help on using the repository browser.