root/trunk/xdotool/xdotool.c

Revision 1109, 14.6 kB (checked in by dkg, 6 months ago)

xdotool: upgrading to 20080720 from upstream.

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