forked from jordansissel/xdotool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xdo.c
2062 lines (1742 loc) · 64.7 KB
/
xdo.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
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* xdo library
* - getwindowfocus contributed by Lee Pumphret
* - keysequence_{up,down} contributed by Magnus Boman
*
* See the following url for an explanation of how keymaps work in X11
* http://www.in-ulm.de/~mascheck/X11/xmodmap.html
*/
#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE 500
#endif /* _XOPEN_SOURCE */
#include <sys/select.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <regex.h>
#include <ctype.h>
#include <locale.h>
#include <stdarg.h>
#include <X11/Xlib.h>
#include <X11/XKBlib.h>
#include <X11/Xatom.h>
#include <X11/Xresource.h>
#include <X11/Xutil.h>
#include <X11/extensions/XTest.h>
#include <X11/extensions/Xinerama.h>
#include <X11/extensions/XInput2.h>
#include <X11/keysym.h>
#include <X11/cursorfont.h>
#include <xkbcommon/xkbcommon.h>
#include "xdo.h"
#include "xdo_util.h"
#include "xdo_version.h"
#define DEFAULT_DELAY 12
/**
* The number of tries to check for a wait condition before aborting.
* TODO(sissel): Make this tunable at runtime?
*/
#define MAX_TRIES 500
static void _xdo_populate_charcode_map(xdo_t *xdo);
static int _xdo_has_xtest(const xdo_t *xdo);
static KeySym _xdo_keysym_from_char(const xdo_t *xdo, wchar_t key);
static void _xdo_charcodemap_from_char(const xdo_t *xdo, charcodemap_t *key);
static void _xdo_charcodemap_from_keysym(const xdo_t *xdo, charcodemap_t *key, KeySym keysym);
//static int _xdo_get_shiftcode_if_needed(const xdo_t *xdo, char key);
static int _xdo_send_keysequence_window_to_keycode_list(const xdo_t *xdo, const char *keyseq,
charcodemap_t **keys, int *nkeys);
static int _xdo_send_keysequence_window_do(const xdo_t *xdo, Window window, const char *keyseq,
int pressed, int *modifier, useconds_t delay);
static int _xdo_ewmh_is_supported(const xdo_t *xdo, const char *feature);
static void _xdo_init_xkeyevent(const xdo_t *xdo, XKeyEvent *xk);
static void _xdo_send_key(const xdo_t *xdo, Window window, charcodemap_t *key,
int modstate, int is_press, useconds_t delay);
static void _xdo_send_modifier(const xdo_t *xdo, int modmask, int is_press);
static int _xdo_query_keycode_to_modifier(XModifierKeymap *modmap, KeyCode keycode);
static int _xdo_mousebutton(const xdo_t *xdo, Window window, int button, int is_press);
static int _is_success(const char *funcname, int code, const xdo_t *xdo);
static void _xdo_debug(const xdo_t *xdo, const char *format, ...);
static void _xdo_eprintf(const xdo_t *xdo, int hushable, const char *format, ...);
static int appears_to_be_wayland(Display *xdpy);
/* context-free functions */
static wchar_t _keysym_to_char(KeySym keysym);
/* Default to -1, initialize it when we need it */
static Atom atom_NET_WM_PID = -1;
static Atom atom_NET_WM_NAME = -1;
static Atom atom_WM_NAME = -1;
static Atom atom_STRING = -1;
static Atom atom_UTF8_STRING = -1;
xdo_t* xdo_new(const char *display_name) {
Display *xdpy;
if (display_name == NULL) {
display_name = XDisplayName(display_name);
}
#define DISPLAY_HINT "Is there an Xorg or other X server running? You can try setting 'export DISPLAY=:0' and trying again."
if (display_name == NULL) {
fprintf(stderr, "Error: No DISPLAY environment variable is set. " DISPLAY_HINT "\n");
return NULL;
}
if (*display_name == '\0') {
fprintf(stderr, "Error: DISPLAY environment variable is empty. " DISPLAY_HINT "\n");
return NULL;
}
if ((xdpy = XOpenDisplay(display_name)) == NULL) {
return NULL;
}
return xdo_new_with_opened_display(xdpy, display_name, 1);
}
xdo_t* xdo_new_with_opened_display(Display *xdpy, const char *display,
int close_display_when_freed) {
xdo_t *xdo = NULL;
if (xdpy == NULL) {
/* Can't use _xdo_eprintf yet ... */
fprintf(stderr, "xdo_new: xdisplay I was given is a null pointer\n");
return NULL;
}
// This library and xdotool do not work correctly on Wayland/XWayland.
// Try to detect XWayland and warn the user about problems.
// TODO(sissel): This was disabled due to issue #346
// -- xdotool works on XWayland for some operations, so it isn't helpful to refuse all usage on XWayland.
//if (appears_to_be_wayland(xdpy)) {
//fprintf(stderr, "The X server at %s appears to be XWayland. Unfortunately, XWayland does not correctly support the features used by libxdo and xdotool.\n", display);
//return NULL;
//}
/* XXX: Check for NULL here */
xdo = malloc(sizeof(xdo_t));
memset(xdo, 0, sizeof(xdo_t));
xdo->xdpy = xdpy;
xdo->close_display_when_freed = close_display_when_freed;
if (display == NULL) {
display = "unknown";
}
if (getenv("XDO_QUIET")) {
xdo->quiet = True;
}
if (_xdo_has_xtest(xdo)) {
xdo_enable_feature(xdo, XDO_FEATURE_XTEST);
_xdo_debug(xdo, "XTEST enabled.");
} else {
_xdo_eprintf(xdo, False, "Warning: XTEST extension unavailable on '%s'. Some"
" functionality may be disabled; See 'man xdotool' for more"
" info.", xdo->display_name);
xdo_disable_feature(xdo, XDO_FEATURE_XTEST);
}
_xdo_populate_charcode_map(xdo);
return xdo;
}
void xdo_free(xdo_t *xdo) {
if (xdo == NULL)
return;
free(xdo->display_name);
free(xdo->charcodes);
if (xdo->xdpy && xdo->close_display_when_freed)
XCloseDisplay(xdo->xdpy);
free(xdo);
}
const char *xdo_version(void) {
return XDO_VERSION;
}
int xdo_wait_for_window_map_state(const xdo_t *xdo, Window wid, int map_state) {
int tries = MAX_TRIES;
XWindowAttributes attr;
attr.map_state = IsUnmapped;
while (tries > 0 && attr.map_state != map_state) {
XGetWindowAttributes(xdo->xdpy, wid, &attr);
usleep(30000); /* TODO(sissel): Use exponential backoff up to 1 second */
tries--;
}
return 0;
}
int xdo_map_window(const xdo_t *xdo, Window wid) {
int ret = 0;
ret = XMapWindow(xdo->xdpy, wid);
XFlush(xdo->xdpy);
return _is_success("XMapWindow", ret == 0, xdo);
}
int xdo_unmap_window(const xdo_t *xdo, Window wid) {
int ret = 0;
ret = XUnmapWindow(xdo->xdpy, wid);
XFlush(xdo->xdpy);
return _is_success("XUnmapWindow", ret == 0, xdo);
}
int xdo_reparent_window(const xdo_t *xdo, Window wid_source, Window wid_target) {
int ret = 0;
ret = XReparentWindow(xdo->xdpy, wid_source, wid_target, 0, 0);
XFlush(xdo->xdpy);
return _is_success("XReparentWindow", ret == 0, xdo);
}
int xdo_get_window_location(const xdo_t *xdo, Window wid,
int *x_ret, int *y_ret, Screen **screen_ret) {
int ret;
XWindowAttributes attr;
ret = XGetWindowAttributes(xdo->xdpy, wid, &attr);
if (ret != 0) {
int x, y;
Window unused_child;
/* The coordinates in attr are relative to the parent window. If
* the parent window is the root window, then the coordinates are
* correct. If the parent window isn't the root window --- which
* is likely --- then we translate them. */
Window parent;
Window root;
Window* children;
unsigned int nchildren;
XQueryTree(xdo->xdpy, wid, &root, &parent, &children, &nchildren);
if (children != NULL) {
XFree(children);
}
if (parent == attr.root) {
x = attr.x;
y = attr.y;
} else {
XTranslateCoordinates(xdo->xdpy, wid, attr.root,
0, 0, &x, &y, &unused_child);
}
if (x_ret != NULL) {
*x_ret = x;
}
if (y_ret != NULL) {
*y_ret = y;
}
if (screen_ret != NULL) {
*screen_ret = attr.screen;
}
}
return _is_success("XGetWindowAttributes", ret == 0, xdo);
}
int xdo_get_window_size(const xdo_t *xdo, Window wid, unsigned int *width_ret,
unsigned int *height_ret) {
int ret;
XWindowAttributes attr;
ret = XGetWindowAttributes(xdo->xdpy, wid, &attr);
if (ret != 0) {
if (width_ret != NULL) {
*width_ret = attr.width;
}
if (height_ret != NULL) {
*height_ret = attr.height;
}
}
return _is_success("XGetWindowAttributes", ret == 0, xdo);
}
int xdo_move_window(const xdo_t *xdo, Window wid, int x, int y) {
XWindowChanges wc;
int ret = 0;
wc.x = x;
wc.y = y;
ret = XConfigureWindow(xdo->xdpy, wid, CWX | CWY, &wc);
return _is_success("XConfigureWindow", ret == 0, xdo);
}
int xdo_translate_window_with_sizehint(const xdo_t *xdo, Window window,
unsigned int width, unsigned int height,
unsigned int *width_ret, unsigned int *height_ret) {
XSizeHints hints;
long supplied_return;
XGetWMNormalHints(xdo->xdpy, window, &hints, &supplied_return);
if (supplied_return & PResizeInc) {
width *= hints.width_inc;
height *= hints.height_inc;
} else {
fprintf(stderr, "No size hints found for window %ld\n", window);
*width_ret = width;
*height_ret = width;
}
if (supplied_return & PBaseSize) {
width += hints.base_width;
height += hints.base_height;
}
if (width_ret != NULL) {
*width_ret = width;
}
if (height_ret != NULL) {
*height_ret = height;
}
return XDO_SUCCESS;
}
int xdo_set_window_size(const xdo_t *xdo, Window window, int width, int height, int flags) {
XWindowChanges wc;
int ret = 0;
int cw_flags = 0;
if (flags & SIZE_USEHINTS) {
flags |= SIZE_USEHINTS_X | SIZE_USEHINTS_Y;
}
wc.width = width;
wc.height = height;
if (flags & SIZE_USEHINTS_X) {
xdo_translate_window_with_sizehint(xdo, window, width, height, (unsigned int*)&wc.width,
NULL);
}
if (flags & SIZE_USEHINTS_Y) {
xdo_translate_window_with_sizehint(xdo, window, width, height, NULL,
(unsigned int*)&wc.height);
}
if (width > 0) {
cw_flags |= CWWidth;
}
if (height > 0) {
cw_flags |= CWHeight;
}
ret = XConfigureWindow(xdo->xdpy, window, cw_flags, &wc);
XFlush(xdo->xdpy);
return _is_success("XConfigureWindow", ret == 0, xdo);
}
int xdo_set_window_override_redirect(const xdo_t *xdo, Window wid,
int override_redirect) {
int ret;
XSetWindowAttributes wattr;
long mask = CWOverrideRedirect;
wattr.override_redirect = override_redirect;
ret = XChangeWindowAttributes(xdo->xdpy, wid, mask, &wattr);
return _is_success("XChangeWindowAttributes", ret == 0, xdo);
}
int xdo_set_window_class (const xdo_t *xdo, Window wid, const char *name,
const char *_class) {
int ret = 0;
XClassHint *hint = XAllocClassHint();
XGetClassHint(xdo->xdpy, wid, hint);
if (name != NULL)
hint->res_name = (char*)name;
if(_class != NULL)
hint->res_class = (char*)_class;
ret = XSetClassHint(xdo->xdpy, wid, hint);
XFree(hint);
return _is_success("XSetClassHint", ret == 0, xdo);
}
int xdo_set_window_urgency (const xdo_t *xdo, Window wid, int urgency) {
int ret = 0;
XWMHints *hint = XGetWMHints(xdo->xdpy, wid);
if (hint == NULL)
hint = XAllocWMHints();
if (urgency)
hint->flags = hint->flags | XUrgencyHint;
else
hint->flags = hint->flags & ~XUrgencyHint;
ret = XSetWMHints(xdo->xdpy, wid, hint);
XFree(hint);
return _is_success("XSetWMHint", ret == 0, xdo);
}
int xdo_set_window_property(const xdo_t *xdo, Window wid, const char *property, const char *value) {
char netwm_property[256] = "_NET_";
int ret = 0;
strcat(netwm_property, property);
// Change the property
ret = XChangeProperty(xdo->xdpy, wid,
XInternAtom(xdo->xdpy, property, False),
XInternAtom(xdo->xdpy, "STRING", False), 8,
PropModeReplace, (unsigned char*)value, strlen(value));
if (ret == 0) {
return _is_success("XChangeProperty", ret == 0, xdo);
}
// Change _NET_<property> just in case for simpler NETWM compliance?
ret = XChangeProperty(xdo->xdpy, wid,
XInternAtom(xdo->xdpy, netwm_property, False),
XInternAtom(xdo->xdpy, "STRING", False), 8,
PropModeReplace, (unsigned char*)value, strlen(value));
return _is_success("XChangeProperty", ret == 0, xdo);
}
int xdo_focus_window(const xdo_t *xdo, Window wid) {
int ret = 0;
ret = XSetInputFocus(xdo->xdpy, wid, RevertToParent, CurrentTime);
XFlush(xdo->xdpy);
return _is_success("XSetInputFocus", ret == 0, xdo);
}
int xdo_wait_for_window_size(const xdo_t *xdo, Window window,
unsigned int width, unsigned int height,
int flags, int to_or_from) {
unsigned int cur_width, cur_height;
/*unsigned int alt_width, alt_height;*/
//printf("Want: %udx%ud\n", width, height);
if (flags & SIZE_USEHINTS) {
xdo_translate_window_with_sizehint(xdo, window, width, height,
&width, &height);
} else {
unsigned int hint_width, hint_height;
/* TODO(sissel): fix compiler warning here, but it will require
* an ABI breakage by changing types... */
xdo_translate_window_with_sizehint(xdo, window, 1, 1,
&hint_width, &hint_height);
//printf("Hint: %dx%d\n", hint_width, hint_height);
/* Find the nearest multiple (rounded down) of the hint height. */
/*alt_width = (width - (width % hint_width));*/
/*alt_height = (height - (height % hint_height));*/
//printf("Alt: %udx%ud\n", alt_width, alt_height);
}
int tries = MAX_TRIES;
xdo_get_window_size(xdo, window, &cur_width,
&cur_height);
//printf("Want: %udx%ud\n", width, height);
//printf("Alt: %udx%ud\n", alt_width, alt_height);
while (tries > 0 && (to_or_from == SIZE_TO
? (cur_width != width && cur_height != height)
: (cur_width == width && cur_height == height))) {
xdo_get_window_size(xdo, window, (unsigned int *)&cur_width,
(unsigned int *)&cur_height);
usleep(30000);
tries--;
}
return 0;
}
int xdo_wait_for_window_active(const xdo_t *xdo, Window window, int active) {
Window activewin = 0;
int ret = 0;
int tries = MAX_TRIES;
/* If active is true, wait until activewin is our window
* otherwise, wait until activewin is not our window */
while (tries > 0 &&
(active ? activewin != window : activewin == window)) {
ret = xdo_get_active_window(xdo, &activewin);
if (ret == XDO_ERROR) {
return ret;
}
usleep(30000);
tries--;
}
return 0;
}
int xdo_activate_window(const xdo_t *xdo, Window wid) {
int ret = 0;
long desktop = 0;
XEvent xev;
XWindowAttributes wattr;
if (_xdo_ewmh_is_supported(xdo, "_NET_ACTIVE_WINDOW") == False) {
fprintf(stderr,
"Your windowmanager claims not to support _NET_ACTIVE_WINDOW, "
"so the attempt to activate the window was aborted.\n");
return XDO_ERROR;
}
/* If this window is on another desktop, let's go to that desktop first */
if (_xdo_ewmh_is_supported(xdo, "_NET_WM_DESKTOP") == True
&& _xdo_ewmh_is_supported(xdo, "_NET_CURRENT_DESKTOP") == True) {
xdo_get_desktop_for_window(xdo, wid, &desktop);
xdo_set_current_desktop(xdo, desktop);
}
memset(&xev, 0, sizeof(xev));
xev.type = ClientMessage;
xev.xclient.display = xdo->xdpy;
xev.xclient.window = wid;
xev.xclient.message_type = XInternAtom(xdo->xdpy, "_NET_ACTIVE_WINDOW", False);
xev.xclient.format = 32;
xev.xclient.data.l[0] = 2L; /* 2 == Message from a window pager */
xev.xclient.data.l[1] = CurrentTime;
XGetWindowAttributes(xdo->xdpy, wid, &wattr);
ret = XSendEvent(xdo->xdpy, wattr.screen->root, False,
SubstructureNotifyMask | SubstructureRedirectMask,
&xev);
/* XXX: XSendEvent returns 0 on conversion failure, nonzero otherwise.
* Manpage says it will only generate BadWindow or BadValue errors */
return _is_success("XSendEvent[EWMH:_NET_ACTIVE_WINDOW]", ret == 0, xdo);
}
int xdo_set_number_of_desktops(const xdo_t *xdo, long ndesktops) {
/* XXX: This should support passing a screen number */
XEvent xev;
Window root;
int ret = 0;
if (_xdo_ewmh_is_supported(xdo, "_NET_NUMBER_OF_DESKTOPS") == False) {
fprintf(stderr,
"Your windowmanager claims not to support _NET_NUMBER_OF_DESKTOPS, "
"so the attempt to change the number of desktops was aborted.\n");
return XDO_ERROR;
}
root = RootWindow(xdo->xdpy, 0);
memset(&xev, 0, sizeof(xev));
xev.type = ClientMessage;
xev.xclient.display = xdo->xdpy;
xev.xclient.window = root;
xev.xclient.message_type = XInternAtom(xdo->xdpy, "_NET_NUMBER_OF_DESKTOPS",
False);
xev.xclient.format = 32;
xev.xclient.data.l[0] = ndesktops;
ret = XSendEvent(xdo->xdpy, root, False,
SubstructureNotifyMask | SubstructureRedirectMask,
&xev);
return _is_success("XSendEvent[EWMH:_NET_NUMBER_OF_DESKTOPS]", ret == 0, xdo);
}
int xdo_get_number_of_desktops(const xdo_t *xdo, long *ndesktops) {
Atom type;
int size;
long nitems;
unsigned char *data;
Window root;
Atom request;
if (_xdo_ewmh_is_supported(xdo, "_NET_NUMBER_OF_DESKTOPS") == False) {
fprintf(stderr,
"Your windowmanager claims not to support _NET_NUMBER_OF_DESKTOPS, "
"so the attempt to query the number of desktops was aborted.\n");
return XDO_ERROR;
}
request = XInternAtom(xdo->xdpy, "_NET_NUMBER_OF_DESKTOPS", False);
root = XDefaultRootWindow(xdo->xdpy);
data = xdo_get_window_property_by_atom(xdo, root, request, &nitems, &type, &size);
if (nitems > 0) {
*ndesktops = *((long*)data);
} else {
*ndesktops = 0;
}
free(data);
return _is_success("XGetWindowProperty[_NET_NUMBER_OF_DESKTOPS]",
*ndesktops == 0, xdo);
}
int xdo_set_current_desktop(const xdo_t *xdo, long desktop) {
/* XXX: This should support passing a screen number */
XEvent xev;
Window root;
int ret = 0;
root = RootWindow(xdo->xdpy, 0);
if (_xdo_ewmh_is_supported(xdo, "_NET_CURRENT_DESKTOP") == False) {
fprintf(stderr,
"Your windowmanager claims not to support _NET_CURRENT_DESKTOP, "
"so the attempt to change desktops was aborted.\n");
return XDO_ERROR;
}
memset(&xev, 0, sizeof(xev));
xev.type = ClientMessage;
xev.xclient.display = xdo->xdpy;
xev.xclient.window = root;
xev.xclient.message_type = XInternAtom(xdo->xdpy, "_NET_CURRENT_DESKTOP",
False);
xev.xclient.format = 32;
xev.xclient.data.l[0] = desktop;
xev.xclient.data.l[1] = CurrentTime;
ret = XSendEvent(xdo->xdpy, root, False,
SubstructureNotifyMask | SubstructureRedirectMask,
&xev);
return _is_success("XSendEvent[EWMH:_NET_CURRENT_DESKTOP]", ret == 0, xdo);
}
int xdo_get_current_desktop(const xdo_t *xdo, long *desktop) {
Atom type;
int size;
long nitems;
unsigned char *data;
Window root;
Atom request;
if (_xdo_ewmh_is_supported(xdo, "_NET_CURRENT_DESKTOP") == False) {
fprintf(stderr,
"Your windowmanager claims not to support _NET_CURRENT_DESKTOP, "
"so the query for the current desktop was aborted.\n");
return XDO_ERROR;
}
request = XInternAtom(xdo->xdpy, "_NET_CURRENT_DESKTOP", False);
root = XDefaultRootWindow(xdo->xdpy);
data = xdo_get_window_property_by_atom(xdo, root, request, &nitems, &type, &size);
if (nitems > 0) {
*desktop = *((long*)data);
} else {
*desktop = -1;
}
free(data);
return _is_success("XGetWindowProperty[_NET_CURRENT_DESKTOP]",
*desktop == -1, xdo);
}
int xdo_set_desktop_for_window(const xdo_t *xdo, Window wid, long desktop) {
XEvent xev;
int ret = 0;
XWindowAttributes wattr;
XGetWindowAttributes(xdo->xdpy, wid, &wattr);
if (_xdo_ewmh_is_supported(xdo, "_NET_WM_DESKTOP") == False) {
fprintf(stderr,
"Your windowmanager claims not to support _NET_WM_DESKTOP, "
"so the attempt to change a window's desktop location was "
"aborted.\n");
return XDO_ERROR;
}
memset(&xev, 0, sizeof(xev));
xev.type = ClientMessage;
xev.xclient.display = xdo->xdpy;
xev.xclient.window = wid;
xev.xclient.message_type = XInternAtom(xdo->xdpy, "_NET_WM_DESKTOP",
False);
xev.xclient.format = 32;
xev.xclient.data.l[0] = desktop;
xev.xclient.data.l[1] = 2; /* indicate we are messaging from a pager */
ret = XSendEvent(xdo->xdpy, wattr.screen->root, False,
SubstructureNotifyMask | SubstructureRedirectMask,
&xev);
return _is_success("XSendEvent[EWMH:_NET_WM_DESKTOP]", ret == 0, xdo);
}
int xdo_get_desktop_for_window(const xdo_t *xdo, Window wid, long *desktop) {
Atom type;
int size;
long nitems;
unsigned char *data;
Atom request;
if (_xdo_ewmh_is_supported(xdo, "_NET_WM_DESKTOP") == False) {
fprintf(stderr,
"Your windowmanager claims not to support _NET_WM_DESKTOP, "
"so the attempt to query a window's desktop location was "
"aborted.\n");
return XDO_ERROR;
}
request = XInternAtom(xdo->xdpy, "_NET_WM_DESKTOP", False);
data = xdo_get_window_property_by_atom(xdo, wid, request, &nitems, &type, &size);
if (nitems > 0) {
*desktop = *((long*)data);
} else {
*desktop = -1;
}
free(data);
return _is_success("XGetWindowProperty[_NET_WM_DESKTOP]",
*desktop == -1, xdo);
}
int xdo_get_active_window(const xdo_t *xdo, Window *window_ret) {
Atom type;
int size;
long nitems;
unsigned char *data;
Atom request;
Window root;
if (_xdo_ewmh_is_supported(xdo, "_NET_ACTIVE_WINDOW") == False) {
fprintf(stderr,
"Your windowmanager claims not to support _NET_ACTIVE_WINDOW, "
"so the attempt to query the active window aborted.\n");
return XDO_ERROR;
}
request = XInternAtom(xdo->xdpy, "_NET_ACTIVE_WINDOW", False);
root = XDefaultRootWindow(xdo->xdpy);
data = xdo_get_window_property_by_atom(xdo, root, request, &nitems, &type, &size);
if (nitems > 0) {
*window_ret = *((Window*)data);
} else {
*window_ret = 0;
}
free(data);
return _is_success("XGetWindowProperty[_NET_ACTIVE_WINDOW]",
*window_ret == 0, xdo);
}
int xdo_select_window_with_click(const xdo_t *xdo, Window *window_ret) {
int screen_num;
Screen *screen;
xdo_get_mouse_location(xdo, NULL, NULL, &screen_num);
screen = ScreenOfDisplay(xdo->xdpy, screen_num);
/* Grab sync mode so we can ensure nothing changes while we figure
* out what the client window is.
* Also, everyone else who does 'select window' does it this way.
*/
Cursor cursor = XCreateFontCursor(xdo->xdpy, XC_target);
int grab_ret = 0;
grab_ret = XGrabPointer(xdo->xdpy, screen->root, False, ButtonReleaseMask,
GrabModeSync, GrabModeAsync, screen->root, cursor, CurrentTime);
if (grab_ret == AlreadyGrabbed) {
fprintf(stderr, "Attempt to grab the mouse failed. Something already has"
" the mouse grabbed. This can happen if you are dragging something"
" or if there is a popup currently shown\n");
return XDO_ERROR;
}
XEvent e;
XAllowEvents(xdo->xdpy, SyncPointer, CurrentTime);
XWindowEvent(xdo->xdpy, screen->root, ButtonReleaseMask, &e);
XUngrabPointer(xdo->xdpy, CurrentTime);
XFreeCursor(xdo->xdpy, cursor);
if (e.xbutton.button != 1) {
fprintf(stderr, "window selection aborted with button %d\n", e.xbutton.button);
return XDO_ERROR;
}
/* If there is no subwindow, then we clicked on the root window */
if (e.xbutton.subwindow == 0) {
*window_ret = e.xbutton.root;
} else {
/* Random testing showed that 'root' always is the same as 'window'
* while 'subwindow' is the actual window we clicked on. Confusing... */
*window_ret = e.xbutton.subwindow;
_xdo_debug(xdo, "Click on window %lu foo", *window_ret);
xdo_find_window_client(xdo, *window_ret, window_ret, XDO_FIND_CHILDREN);
}
return XDO_SUCCESS;
}
/* XRaiseWindow is ignored in ion3 and Gnome2. Is it even useful? */
int xdo_raise_window(const xdo_t *xdo, Window wid) {
int ret = 0;
ret = XRaiseWindow(xdo->xdpy, wid);
XFlush(xdo->xdpy);
return _is_success("XRaiseWindow", ret == 0, xdo);
}
int xdo_move_mouse(const xdo_t *xdo, int x, int y, int screen) {
int ret = 0;
/* There is a bug (feature?) in XTestFakeMotionEvent that causes
* the screen number in the request to be ignored. The internets
* seem to recommend XWarpPointer instead, ie;
* https://bugzilla.redhat.com/show_bug.cgi?id=518803
*/
Window screen_root = RootWindow(xdo->xdpy, screen);
ret = XWarpPointer(xdo->xdpy, None, screen_root, 0, 0, 0, 0, x, y);
XFlush(xdo->xdpy);
return _is_success("XWarpPointer", ret == 0, xdo);
}
int xdo_move_mouse_relative_to_window(const xdo_t *xdo, Window window, int x, int y) {
XWindowAttributes attr;
Window unused_child;
int root_x, root_y;
XGetWindowAttributes(xdo->xdpy, window, &attr);
XTranslateCoordinates(xdo->xdpy, window, attr.root,
x, y, &root_x, &root_y, &unused_child);
return xdo_move_mouse(xdo, root_x, root_y, XScreenNumberOfScreen(attr.screen));
}
int xdo_move_mouse_relative(const xdo_t *xdo, int x, int y) {
int ret = 0;
ret = XTestFakeRelativeMotionEvent(xdo->xdpy, x, y, CurrentTime);
XFlush(xdo->xdpy);
return _is_success("XTestFakeRelativeMotionEvent", ret == 0, xdo);
}
int _xdo_mousebutton(const xdo_t *xdo, Window window, int button, int is_press) {
int ret = 0;
if (window == CURRENTWINDOW) {
ret = XTestFakeButtonEvent(xdo->xdpy, button, is_press, CurrentTime);
XFlush(xdo->xdpy);
return _is_success("XTestFakeButtonEvent(down)", ret == 0, xdo);
} else {
/* Send to specific window */
int screen = 0;
XButtonEvent xbpe;
charcodemap_t *active_mod;
int active_mod_n;
xdo_get_mouse_location(xdo, &xbpe.x_root, &xbpe.y_root, &screen);
xdo_get_active_modifiers(xdo, &active_mod, &active_mod_n);
xbpe.window = window;
xbpe.button = button;
xbpe.display = xdo->xdpy;
xbpe.root = RootWindow(xdo->xdpy, screen);
xbpe.same_screen = True; /* Should we detect if window is on the same
screen as cursor? */
xbpe.state = xdo_get_input_state(xdo);
xbpe.subwindow = None;
xbpe.time = CurrentTime;
xbpe.type = (is_press ? ButtonPress : ButtonRelease);
/* Get the coordinates of the cursor relative to xbpe.window and also find what
* subwindow it might be on */
XTranslateCoordinates(xdo->xdpy, xbpe.root, xbpe.window,
xbpe.x_root, xbpe.y_root, &xbpe.x, &xbpe.y, &xbpe.subwindow);
/* Normal behavior of 'mouse up' is that the modifier mask includes
* 'ButtonNMotionMask' where N is the button being released. This works the
* same way with keys, too. */
if (!is_press) { /* is mouse up */
switch(button) {
case 1: xbpe.state |= Button1MotionMask; break;
case 2: xbpe.state |= Button2MotionMask; break;
case 3: xbpe.state |= Button3MotionMask; break;
case 4: xbpe.state |= Button4MotionMask; break;
case 5: xbpe.state |= Button5MotionMask; break;
}
}
ret = XSendEvent(xdo->xdpy, window, True, ButtonPressMask, (XEvent *)&xbpe);
XFlush(xdo->xdpy);
free(active_mod);
return _is_success("XSendEvent(mousedown)", ret == 0, xdo);
}
}
int xdo_mouse_up(const xdo_t *xdo, Window window, int button) {
return _xdo_mousebutton(xdo, window, button, False);
}
int xdo_mouse_down(const xdo_t *xdo, Window window, int button) {
return _xdo_mousebutton(xdo, window, button, True);
}
int xdo_get_mouse_location(const xdo_t *xdo, int *x_ret, int *y_ret,
int *screen_num_ret) {
return xdo_get_mouse_location2(xdo, x_ret, y_ret, screen_num_ret, NULL);
}
int xdo_get_window_at_mouse(const xdo_t *xdo, Window *window_ret) {
return xdo_get_mouse_location2(xdo, NULL, NULL, NULL, window_ret);
}
int xdo_get_mouse_location2(const xdo_t *xdo, int *x_ret, int *y_ret,
int *screen_num_ret, Window *window_ret) {
int ret = False;
int x = 0, y = 0, screen_num = 0;
int i = 0;
Window window = 0;
Window root = 0;
int dummy_int = 0;
unsigned int dummy_uint = 0;
int screencount = ScreenCount(xdo->xdpy);
for (i = 0; i < screencount; i++) {
Screen *screen = ScreenOfDisplay(xdo->xdpy, i);
ret = XQueryPointer(xdo->xdpy, RootWindowOfScreen(screen),
&root, &window,
&x, &y, &dummy_int, &dummy_int, &dummy_uint);
if (ret == True) {
screen_num = i;
break;
}
}
if (window_ret != NULL) {
/* Find the client window if we are not root. */
if (window != root && window != 0) {
int findret;
Window client = 0;
/* Search up the stack for a client window for this window */
findret = xdo_find_window_client(xdo, window, &client, XDO_FIND_PARENTS);
if (findret == XDO_ERROR) {
/* If no client found, search down the stack */
findret = xdo_find_window_client(xdo, window, &client, XDO_FIND_CHILDREN);
}
//fprintf(stderr, "%ld, %ld, %ld, %d\n", window, root, client, findret);
if (findret == XDO_SUCCESS) {
window = client;
}
} else {
window = root;
}
}
//printf("mouseloc root: %ld\n", root);
//printf("mouseloc window: %ld\n", window);
if (ret == True) {
if (x_ret != NULL) *x_ret = x;
if (y_ret != NULL) *y_ret = y;
if (screen_num_ret != NULL) *screen_num_ret = screen_num;
if (window_ret != NULL) *window_ret = window;
}
return _is_success("XQueryPointer", ret == False, xdo);
}
int xdo_click_window(const xdo_t *xdo, Window window, int button) {
int ret = 0;
ret = xdo_mouse_down(xdo, window, button);
if (ret != XDO_SUCCESS) {
fprintf(stderr, "xdo_mouse_down failed, aborting click.\n");
return ret;
}
usleep(DEFAULT_DELAY);
ret = xdo_mouse_up(xdo, window, button);
return ret;
}
int xdo_click_window_multiple(const xdo_t *xdo, Window window, int button,
int repeat, useconds_t delay) {
int ret = 0;
while (repeat > 0) {
ret = xdo_click_window(xdo, window, button);
if (ret != XDO_SUCCESS) {
fprintf(stderr, "click failed with %d repeats remaining\n", repeat);
return ret;
}
repeat--;
/* Sleeping even after the last click is important, so that a call to xdo_set_active_modifiers()
* right after won't think that the button is still pressed. */
usleep(delay);
} /* while (repeat > 0) */
return ret;
} /* int xdo_click_window_multiple */
/* XXX: Return proper code if errors found */
int xdo_enter_text_window(const xdo_t *xdo, Window window, const char *string, useconds_t delay) {
/* Since we're doing down/up, the delay should be based on the number
* of keys pressed (including shift). Since up/down is two calls,
* divide by two. */
delay /= 2;
/* XXX: Add error handling */
//int nkeys = strlen(string);
//charcodemap_t *keys = calloc(nkeys, sizeof(charcodemap_t));
charcodemap_t key;
//int modifier = 0;
setlocale(LC_CTYPE,"");
mbstate_t ps = { 0 };
ssize_t len;
while ( (len = mbsrtowcs(&key.key, &string, 1, &ps)) ) {
if (len == -1) {
fprintf(stderr, "Invalid multi-byte sequence encountered\n");
return XDO_ERROR;
}
_xdo_charcodemap_from_char(xdo, &key);
if (key.code == 0 && key.symbol == NoSymbol) {
fprintf(stderr, "I don't know which key produces '%lc', skipping.\n",
key.key);