source: trunk/xdotool/xdotool.c @ 1092

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

xdotool: merging changes for 20080606

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