source: branches/upstream/xdotool/current/xdotool.c @ 1085

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

[svn-upgrade] Integrating new upstream version, xdotool (20080603)

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