Debian Codegraph @

GUITest.xs

raw
1 /* X11::GUITest ($Id: GUITest.xs 253 2026-08-19 22:37:51Z ctrondlp $)
2 *
3 * Copyright (c) 2003-2026 Dennis K. Paulsen, All Rights Reserved.
4 * Email: [email protected]
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of
9 * the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses>.
18 *
19 */
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 #include "EXTERN.h"
24 #include "perl.h"
25 #include "XSUB.h"
26 #ifdef __cplusplus
27 }
28 #endif
29
30 /* Added for pre-v5.6.x Perl */
31 #ifndef newSVuv
32 #define newSVuv newSViv
33 #endif
34
35 #include <stdlib.h>
36 #include <string.h>
37 #include <strings.h> /* strcasecmp() is POSIX, declared here rather than in string.h */
38 #include <unistd.h>
39 #include <X11/Xlib.h>
40 #include <X11/Xutil.h>
41 #include <X11/Xatom.h>
42 #include <X11/Intrinsic.h>
43 #include <X11/StringDefs.h>
44 #include <X11/keysym.h>
45 #include <X11/extensions/XTest.h>
46 #include "Common.h"
47 #include "GUITest.h"
48 #include "KeyUtil.h"
49
50 /* File Level Variables */
51 static Display *TheXDisplay = NULL;
52 static int TheScreen = 0;
53 static WindowTable ChildWindows = {0};
54 static ULONG EventSendDelay = DEF_EVENT_SEND_DELAY;
55 static ULONG KeySendDelay = DEF_KEY_SEND_DELAY;
56 static int (*OldErrorHandler)(Display *, XErrorEvent *) = NULL;
57
58 /* Non Exported Utility Functions: */
59
60 /* Function: IgnoreBadWindow
61 * Description: User defined error handler callback for X event errors.
62 */
63 static int IgnoreBadWindow(Display *display, XErrorEvent *error)
64 {
65 /* Ignore bad window errors here, handle elsewhere */
66 if (error->error_code != BadWindow) {
67 assert(NULL != OldErrorHandler);
68 (*OldErrorHandler)(display, error);
69 }
70 /* Note: Return is ignored */
71 return(0);
72 }
73
74 /* Function: SetupXDisplay
75 * Description: Sets up our connection to the X server's display
76 */
77 static void SetupXDisplay(void)
78 {
79 int eventnum = 0, errornum = 0,
80 majornum = 0, minornum = 0;
81
82 /* Get Display Pointer */
83 TheXDisplay = XOpenDisplay(NULL);
84 if (TheXDisplay == NULL) {
85 croak("X11::GUITest - This program is designed to run in X Windows!\n");
86 }
87
88 /* Ensure the XTest extension is available */
89 if (!XTestQueryExtension(TheXDisplay, &eventnum, &errornum,
90 &majornum, &minornum)) {
91 croak("X11::GUITest - XServer %s doesn't support the XTest extensions!\n",
92 DisplayString(TheXDisplay));
93 }
94
95 TheScreen = DefaultScreen(TheXDisplay);
96
97 /* Discard current events in queue. */
98 XSync(TheXDisplay, True);
99 }
100
101 /* Function: CloseXDisplay
102 * Description: Closes our connection to the X server's display
103 */
104 static void CloseXDisplay(void)
105 {
106 if (TheXDisplay) {
107 XSync(TheXDisplay, False);
108 XCloseDisplay(TheXDisplay);
109 TheXDisplay = NULL;
110 }
111 }
112
113 /* Function: IsNumber
114 * Description: Determines if the specified string represents a
115 * number.
116 */
117 static BOOL IsNumber(const char *str)
118 {
119 size_t x = 0, len = 0;
120
121 assert(str != NULL);
122
123 len = strlen(str);
124 for (x = 0; x < len; x++) {
125 if (!isdigit(str[x])) {
126 return(FALSE);
127 }
128 }
129 return(TRUE);
130 }
131
132 /* Function: GetRegKeySym
133 * Description: Given a regular key name as a single character(i.e., a), this
134 * function obtains the appropriate keysym by calling GetKeySym().
135 * Note: Returns TRUE (non-zero) on success, FALSE (zero) on failure. Also,
136 * on success, sym gets set to the appropriate keysym. On failure, sym
137 * will get set to NoSymbol.
138 */
139 static BOOL GetRegKeySym(const char name, KeySym *sym)
140 {
141 #define MAX_REG_KEY 2
142 static char key[MAX_REG_KEY] = "";
143
144 key[0] = name;
145 key[1] = NUL;
146
147 return( GetKeySym(key, sym) );
148 }
149
150 /* Function: GetKeycodeFromKeysym
151 * Description: Wrapper around XKeysymToKeycode. Supports compile-time
152 * workarounds, etc.
153 */
154 static KeyCode GetKeycodeFromKeysym(Display *display, KeySym keysym)
155 {
156 KeyCode kc = XKeysymToKeycode(display, keysym);
157 #ifdef X11_GUITEST_ALT_L_FALLBACK_META_L
158 /* Xvfb lacks XK_Alt_L; fall back to XK_Meta_L */
159 if (kc == 0 && keysym == XK_Alt_L) {
160 kc = XKeysymToKeycode(display, XK_Meta_L);
161 }
162 #endif
163 return(kc);
164 }
165
166 /* Function: PressKeyImp
167 * Description: Presses the key for the specified keysym. Lower-level
168 * implementation.
169 * Note: Returns TRUE (non-zero) on success, FALSE (zero) on failure.
170 */
171 static BOOL PressKeyImp(KeySym sym)
172 {
173 KeyCode kc = 0;
174 BOOL retval = 0;
175
176 kc = GetKeycodeFromKeysym(TheXDisplay, sym);
177 if (kc == 0) {
178 return(FALSE);
179 }
180
181 retval = (BOOL)XTestFakeKeyEvent(TheXDisplay, kc, True, EventSendDelay);
182
183 XFlush(TheXDisplay);
184 return(retval);
185 }
186
187 /* Function: ReleaseKeyImp
188 * Description: Releases the key for the specified keysym. Lower-level
189 * implementation.
190 * Note: Returns TRUE (non-zero) on success, FALSE (zero) on failure.
191 */
192 static BOOL ReleaseKeyImp(KeySym sym)
193 {
194 KeyCode kc = 0;
195 BOOL retval = 0;
196
197 kc = GetKeycodeFromKeysym(TheXDisplay, sym);
198 if (kc == 0) {
199 return(FALSE);
200 }
201
202 retval = (BOOL)XTestFakeKeyEvent(TheXDisplay, kc, False, EventSendDelay);
203
204 XFlush(TheXDisplay);
205 return(retval);
206 }
207
208 /* Function: PressReleaseKeyImp
209 * Description: Presses and releases the key for the specified keysym.
210 * Also implements key send delay. Lower-level implementation.
211 * Note: Returns TRUE (non-zero) on success, FALSE (zero) on failure.
212 */
213 static BOOL PressReleaseKeyImp(KeySym sym)
214 {
215 if (!PressKeyImp(sym)) {
216 return(FALSE);
217 }
218 if (!ReleaseKeyImp(sym)) {
219 return(FALSE);
220 }
221 /* Possibly wait between(after) keystrokes */
222 if (KeySendDelay > 0) {
223 /* usleep(500 * 1000) = ~500ms */
224 usleep(KeySendDelay * 1000);
225 }
226 return(TRUE);
227 }
228
229 /* Function: IsShiftNeeded
230 * Description: Determines if the specified keysym needs the shift
231 * modifier.
232 * Note: Returns TRUE (non-zero) on success, FALSE (zero) on failure.
233 */
234 static BOOL IsShiftNeeded(KeySym sym)
235 {
236 KeySym ksl = 0, ksu = 0, *kss = NULL;
237 KeyCode kc = 0;
238 int syms = 0;
239 BOOL needed = FALSE;
240
241 kc = GetKeycodeFromKeysym(TheXDisplay, sym);
242 if (!kc) {
243 return(FALSE);
244 }
245
246 /* kc(grave) = kss(grave, asciitilde, ) */
247 kss = XGetKeyboardMapping(TheXDisplay, kc, 1, &syms);
248
249 XConvertCase(sym, &ksl, &ksu);
250
251 if (sym == kss[0] && (sym == ksl && sym == ksu)) {
252 /* Not subject to case conversion */
253 needed = FALSE;
254 } else if (sym == ksl && sym != ksu) {
255 /* Shift not needed */
256 needed = FALSE;
257 } else {
258 /* Shift needed */
259 needed = TRUE;
260 }
261
262 XFree(kss);
263 return(needed);
264 }
265
266 /* Function: ProcessBraceSet
267 * Description: Takes a brace set such as: {Tab}, {Tab 3},
268 * {Tab Tab a b c}, {PAUSE 500}, {PAUSE 500 Tab}, etc.
269 * and breaks it into components, then proceeds to press
270 * the appropriate keys or perform the special functionality
271 * requested (i.e., PAUSE). Numeric elements are used in the
272 * special functionality or simply to ensure the previous key
273 * element gets pressed the specified number of times.
274 * Note: Returns TRUE (non-zero) on success, FALSE (zero) on failure.
275 */
276 static BOOL ProcessBraceSet(const char *braceset, size_t *len)
277 {
278 enum {NONE, PAUSE, KEY}; /* Various Functionalities */
279 int cmd = NONE, count = 0, i = 0;
280 BOOL needshift = FALSE;
281 char *buffer = NULL, *endbrc = NULL, *token = NULL;
282 KeySym sym = 0;
283
284 assert(braceset != NULL);
285 assert(len != NULL);
286
287 /* Fail if there isn't a valid brace set */
288 if (*braceset != '{' || !strchr(braceset, '}')) {
289 return(FALSE);
290 }
291
292 /* Create backup buffer because we are using strtok */
293 buffer = (char *)safemalloc(strlen(braceset));
294 if (buffer == NULL) {
295 return(FALSE);
296 }
297 /* Ignore beginning { char */
298 strcpy(buffer, &braceset[1]);
299
300 /* Get brace end in buffer */
301 endbrc = strchr(buffer, '}');
302 if (endbrc == NULL) {
303 safefree(buffer);
304 return(FALSE);
305 }
306 /* If we have a quoted }, move over one character */
307 if (endbrc[1] == '}') {
308 endbrc++;
309 }
310 /* Terminate brace set */
311 *endbrc = NUL;
312
313 /* Store brace set length for calling function. Include
314 * 2 for {} we ignored */
315 *len = strlen(buffer) + 2;
316
317 /* Work on the space delimited items in the buffer. */
318 if ( !(token = strtok(buffer, " ")) ) {
319 safefree(buffer);
320 return(FALSE);
321 }
322
323 do { /* } while ( (token = strtok(NULL, " ")) ); */
324 count = 0;
325 if (IsNumber(token)) {
326 /* Yes, a number, so convert it for key depresses or for command specific use */
327 if ( (count = atoi(token)) <= 0 ) {
328 safefree(buffer);
329 return(FALSE);
330 }
331 } else {
332 cmd = NONE;
333 /* Special functionality? */
334 if (strcasecmp(token, "PAUSE") == 0) {
335 /* Yes, PAUSE, so continue on to get the duration count */
336 cmd = PAUSE;
337 continue;
338 } else {
339 /* No, just a key, so get symbol */
340 cmd = KEY;
341 if (!GetKeySym(token, &sym)) {
342 safefree(buffer);
343 return(FALSE);
344 }
345 needshift = IsShiftNeeded(sym);
346 if (needshift) {
347 PressKeyImp(XK_Shift_L);
348 }
349 /* Press key */
350 if (!PressReleaseKeyImp(sym)) {
351 if (needshift) {
352 ReleaseKeyImp(XK_Shift_L);
353 }
354 safefree(buffer);
355 return(FALSE);
356 }
357 if (needshift) {
358 ReleaseKeyImp(XK_Shift_L);
359 }
360 }
361 }
362 /* Handle commands that can use a specified count */
363 if (count > 0) {
364 switch (cmd) {
365 case PAUSE:
366 /* usleep(500 * 1000) = 500ms */
367 usleep(count * 1000);
368 break;
369 case KEY:
370 /* Repeat the last key if needed. Start at iteration 2
371 * because we have already depressed key once up above */
372 /* Use shift if needed */
373 if (needshift) {
374 PressKeyImp(XK_Shift_L);
375 }
376 for (i = 2; i <= count; i++) {
377 /* Use sym that was already stored from above */
378 if (!PressReleaseKeyImp(sym)) {
379 if (needshift) {
380 ReleaseKeyImp(XK_Shift_L);
381 }
382 safefree(buffer);
383 return(FALSE);
384 }
385 }
386 if (needshift) {
387 ReleaseKeyImp(XK_Shift_L);
388 }
389 break;
390 default:
391 /* Fail, we have a count, but an unknown command! */
392 safefree(buffer);
393 return(FALSE);
394 }; /* switch (cmd) { */
395 } /* if (count > 0) { */
396 } while ( (token = strtok(NULL, " ")) );
397
398 safefree(buffer);
399 return(TRUE);
400 }
401
402 /* Function: SendKeysImp
403 * Description: Underlying implementation of the SendKeys routine. Read
404 * the SendKeys documentation below for some specifics.
405 * Note: Returns TRUE (non-zero) on success, FALSE (zero) on failure.
406 * Also, if you add special character handling below, also ensure
407 * the QuoteStringForSendKeys function is accurate in GUITest.pm.
408 */
409 static BOOL SendKeysImp(const char *keys)
410 {
411 KeySym sym = 0;
412 size_t keyslen = 0, bracelen = 0;
413 size_t x = 0;
414 BOOL retval = FALSE, shift = FALSE, ctrl = FALSE, altgr = FALSE,
415 alt = FALSE, meta = FALSE, modlock = FALSE, needshift = FALSE;
416
417 assert(keys != NULL);
418
419 keyslen = strlen(keys);
420 for (x = 0; x < keyslen; x++) {
421 switch (keys[x]) {
422 /* Brace Set? of quoted/special characters (i.e. {{}, {TAB}, {F1 F2}, {PAUSE 200}) */
423 case '{':
424 if (!ProcessBraceSet(&keys[x], &bracelen)) {
425 return(FALSE);
426 }
427 /* Skip past the brace set, Note: - 1 because we are at { already */
428 x += (bracelen - 1);
429 continue;
430 /* Modifiers? */
431 case '~': retval = PressReleaseKeyImp(XK_Return); break;
432 case '+': /* Shift */
433 retval = PressKeyImp(XK_Shift_L);
434 shift = TRUE;
435 break;
436 case '^': /* Control */
437 retval = PressKeyImp(XK_Control_L);
438 ctrl = TRUE;
439 break;
440 case '%': /* Alt */
441 retval = PressKeyImp(XK_Alt_L);
442 alt = TRUE;
443 break;
444 case '#': /* Meta */
445 retval = PressKeyImp(XK_Meta_L);
446 meta = TRUE;
447 break;
448 case '&': /* AltGr */
449 retval = PressKeyImp(XK_ISO_Level3_Shift);
450 altgr = TRUE;
451 break;
452 case '(': modlock = TRUE; break;
453 case ')': modlock = FALSE; break;
454 /* Regular Key? (a, b, c, 1, 2, 3, _, *, %), etc. */
455 default:
456 if (!GetRegKeySym(keys[x], &sym)) {
457 return(FALSE);
458 }
459 /* Use shift if needed */
460 needshift = IsShiftNeeded(sym);
461 if (!shift && needshift) {
462 PressKeyImp(XK_Shift_L);
463 }
464 retval = PressReleaseKeyImp(sym);
465 /* Release shift if needed */
466 if (!shift && needshift) {
467 ReleaseKeyImp(XK_Shift_L);
468 }
469 break;
470 }; /* switch (keys[x]) { */
471 /* If modlock coming up next, go on to process it */
472 if (keys[x + 1] == '(') {
473 continue;
474 }
475 /* Ensure modifiers are clear when needed */
476 if (!modlock) {
477 if (shift) {
478 ReleaseKeyImp(XK_Shift_L);
479 shift = FALSE;
480 }
481 if (ctrl) {
482 ReleaseKeyImp(XK_Control_L);
483 ctrl = FALSE;
484 }
485 if (alt) {
486 ReleaseKeyImp(XK_Alt_L);
487 alt = FALSE;
488 }
489 if (meta) {
490 ReleaseKeyImp(XK_Meta_L);
491 meta = FALSE;
492 }
493 if (altgr) {
494 ReleaseKeyImp(XK_ISO_Level3_Shift);
495 altgr = FALSE;
496 }
497 }
498 if (!retval) {
499 return(FALSE);
500 }
501 } /* for (x = 0; x < keyslen; x++) { */
502
503 return(TRUE);
504 }
505
506 /* Function: IsWindowImp
507 * Description: Underlying implementation of the IsWindow routine. Read
508 * the IsWindow documentation below for some specifics.
509 * Note: Returns non-zero for true, zero for false.
510 */
511 static BOOL IsWindowImp(Window win)
512 {
513 XWindowAttributes wattrs = {0};
514 BOOL retval;
515
516 OldErrorHandler = XSetErrorHandler(IgnoreBadWindow);
517 retval = (BOOL)(XGetWindowAttributes(TheXDisplay, win, &wattrs) != 0);
518 XSetErrorHandler(OldErrorHandler);
519 return(retval);
520 }
521
522 /* Function: AddChildWindow
523 * Description: Adds the specified window Id to the internally managed
524 * table of available window Ids. Also handles the memory
525 * allocation for this table.
526 * Note: Returns TRUE (non-zero) on success, FALSE (zero) on failure.
527 */
528 static BOOL AddChildWindow(Window win)
529 {
530 enum {INIT = 1, GROW = 2}; /* Memory Allocation */
531
532 if (!win) {
533 return(FALSE);
534 }
535
536 if (ChildWindows.Ids == NULL) {
537 /* Initialize */
538 ChildWindows.Ids = (Window *)safemalloc(INIT * sizeof(Window));
539 if (ChildWindows.Ids == NULL) {
540 return(FALSE);
541 }
542 ChildWindows.Max = INIT;
543 ChildWindows.NVals = 0;
544 } else if (ChildWindows.NVals >= ChildWindows.Max) {
545 /* Grow */
546 /* Note: Did not use [insert fancy algorythm name here] algorythm here on purpose */
547 Window *TempIds = NULL;
548 TempIds = (Window *)saferealloc(ChildWindows.Ids,
549 (GROW * ChildWindows.Max) * sizeof(Window));
550 if (TempIds == NULL) {
551 return(FALSE);
552 }
553 ChildWindows.Max *= GROW;
554 ChildWindows.Ids = TempIds;
555 }
556 /* Place the new window Id in */
557 ChildWindows.Ids[ChildWindows.NVals] = win;
558 ChildWindows.NVals++;
559
560 return(TRUE);
561 }
562
563 /* Function: ClearChildWindows
564 * Description: Clears the table of window Ids. Memory allocated
565 * in AddChildWindow is not freed here, because
566 * we'll probably want to take advantage of it again.
567 * Note: No return value.
568 */
569 static void ClearChildWindows(void)
570 {
571 if (ChildWindows.Ids) {
572 memset(ChildWindows.Ids, 0, ChildWindows.Max * sizeof(Window));
573 }
574 ChildWindows.NVals = 0;
575 }
576
577 /* Function: FreeChildWindows
578 * Description: Deallocates the memory of the window Id table
579 * that was allocated through AddChildWindow. This
580 * should be called on exit.
581 * Note: No return value.
582 */
583 static void FreeChildWindows(void)
584 {
585 if (ChildWindows.Ids) {
586 safefree(ChildWindows.Ids);
587 ChildWindows.Ids = NULL;
588 }
589 ChildWindows.NVals = 0;
590 ChildWindows.Max = 0;
591 }
592
593 /* Function: EnumChildWindowsAux
594 * Description: Obtains the list of window Ids
595 * Note: Returns value indicating success of obtaining
596 * windows.
597 */
598 static BOOL EnumChildWindowsAux(Window win)
599 {
600 Window root = 0, parent = 0, *children = NULL;
601 UINT childcount = 0;
602 UINT i = 0;
603
604 /* get list of child windows */
605 if (XQueryTree(TheXDisplay, win, &root, &parent, &children,
606 &childcount)) {
607 for (i = 0; i < childcount; i++) {
608 /* Add Child */
609 AddChildWindow(children[i]);
610 /* Look for its descendents */
611 if (!EnumChildWindowsAux(children[i])) {
612 XFree(children);
613 return FALSE;
614 }
615 }
616 if (children) {
617 XFree(children);
618 }
619 return TRUE;
620 } else {
621 return FALSE;
622 }
623 }
624
625 /* Function: EnumChildWindows
626 * Description: Calls utility function to obtain list of window
627 * Ids. Helps handle window transitions.
628 * Note: Returns nothing.
629 */
630 static void EnumChildWindows(Window win)
631 {
632 BOOL success = 0;
633
634 for (;;) {
635 if (!IsWindowImp(win)) {
636 return;
637 }
638
639 OldErrorHandler = XSetErrorHandler(IgnoreBadWindow);
640 success = EnumChildWindowsAux(win);
641 XSetErrorHandler(OldErrorHandler);
642 if (success) {
643 return;
644 }
645 /* Failure: try again, in 1/2 second. */
646 ClearChildWindows();
647 usleep(500000); /* 500000 = 1/2 second */
648 }
649 }
650
651
652 MODULE = X11::GUITest PACKAGE = X11::GUITest
653 PROTOTYPES: DISABLE
654
655 void
656 InitGUITest()
657 PPCODE:
658 /* Things to do on initialization */
659 SetupXDisplay();
660 XTestGrabControl(TheXDisplay, True);
661 XSRETURN(0);
662
663 void
664 DeInitGUITest()
665 PPCODE:
666 /* Things to do on deinitialization */
667 CloseXDisplay();
668 FreeChildWindows();
669 XSRETURN(0);
670
671
672 int
673 DefaultScreen()
674 CODE:
675 RETVAL = TheScreen;
676 OUTPUT:
677 RETVAL
678
679
680 int
681 ScreenCount()
682 CODE:
683 RETVAL = ScreenCount(TheXDisplay);
684 OUTPUT:
685 RETVAL
686
687
688 ULONG
689 SetEventSendDelay(delay)
690 ULONG delay
691 CODE:
692 /* Returning old delay amount */
693 RETVAL = EventSendDelay;
694 EventSendDelay = delay;
695 OUTPUT:
696 RETVAL
697
698
699 ULONG
700 GetEventSendDelay()
701 CODE:
702 RETVAL = EventSendDelay;
703 OUTPUT:
704 RETVAL
705
706
707 ULONG
708 SetKeySendDelay(delay)
709 ULONG delay
710 CODE:
711 /* Returning old delay amount */
712 RETVAL = KeySendDelay;
713 KeySendDelay = delay;
714 OUTPUT:
715 RETVAL
716
717
718 ULONG
719 GetKeySendDelay()
720 CODE:
721 RETVAL = KeySendDelay;
722 OUTPUT:
723 RETVAL
724
725
726 SV *
727 GetWindowName(win)
728 Window win
729 PREINIT:
730 char *name = NULL;
731 XTextProperty wm_name = {0};
732 Atom wm_name_prop = None;
733 CODE:
734 RETVAL = &PL_sv_undef;
735 if (IsWindowImp(win)) {
736 if (XFetchName(TheXDisplay, win, &name)) {
737 RETVAL = newSVpv(name, strlen(name));
738 XFree(name);
739 } else {
740 wm_name_prop = XInternAtom(TheXDisplay, "_NET_WM_NAME", False);
741 if (wm_name_prop != None) {
742 if (XGetTextProperty(TheXDisplay, win, &wm_name, wm_name_prop) && wm_name.value != NULL) {
743 RETVAL = newSVpv((char *)wm_name.value, strlen((char *)wm_name.value));
744 XFree(wm_name.value);
745 }
746 }
747 }
748 }
749 OUTPUT:
750 RETVAL
751
752
753 BOOL
754 SetWindowName(win, name)
755 Window win
756 char *name
757 PREINIT:
758 XTextProperty textprop = {0};
759 size_t namelen = 0;
760 Atom utf8_string = 0;
761 Atom net_wm_name = 0;
762 Atom net_wm_icon_name = 0;
763 CODE:
764 RETVAL = FALSE;
765 if (IsWindowImp(win)) {
766 if (XStringListToTextProperty(&name, 1, &textprop)) {
767 XSetWMName(TheXDisplay, win, &textprop);
768 XSetWMIconName(TheXDisplay, win, &textprop);
769 XFree(textprop.value);
770 RETVAL = TRUE;
771 }
772 /* These UTF8 window name properties can be the properties
773 * that get displayed, so we set them too. */
774 utf8_string = XInternAtom(TheXDisplay, "UTF8_STRING", True);
775 if (utf8_string != None) {
776 net_wm_name = XInternAtom(TheXDisplay, "_NET_WM_NAME", True);
777 net_wm_icon_name = XInternAtom(TheXDisplay, "_NET_WM_ICON_NAME", True);
778 if (net_wm_name != None && net_wm_icon_name != None) {
779 namelen = strlen(name);
780 XChangeProperty(TheXDisplay, win, net_wm_name, utf8_string, 8,
781 PropModeReplace, (unsigned char *)name, namelen);
782 XChangeProperty(TheXDisplay, win, net_wm_icon_name, utf8_string, 8,
783 PropModeReplace, (unsigned char *)name, namelen);
784 }
785 }
786 }
787 OUTPUT:
788 RETVAL
789
790
791 Window
792 GetRootWindow(scr_num = NO_INIT)
793 int scr_num
794 CODE:
795 if (0 == items)
796 scr_num = TheScreen;
797 if (scr_num >= 0 && scr_num < ScreenCount(TheXDisplay))
798 RETVAL = RootWindow(TheXDisplay, scr_num);
799 else
800 RETVAL = None;
801 OUTPUT:
802 RETVAL
803
804
805 void
806 GetChildWindows(win)
807 Window win
808 PREINIT:
809 UINT i = 0;
810 PPCODE:
811 EnumChildWindows(win);
812 EXTEND(SP, (int)ChildWindows.NVals);
813 for (i = 0; i < ChildWindows.NVals; i++) {
814 PUSHs(sv_2mortal(newSVuv((UV)ChildWindows.Ids[i])));
815 }
816 ClearChildWindows();
817 XSRETURN(i);
818
819
820 BOOL
821 MoveMouseAbs(x, y, scr_num = NO_INIT)
822 int x
823 int y
824 int scr_num
825 CODE:
826 if (items < 3)
827 scr_num = TheScreen;
828 if (scr_num >= 0 && scr_num < ScreenCount(TheXDisplay)) {
829 #ifndef X11_GUITEST_USING_XINERAMA
830 RETVAL = (BOOL)XTestFakeMotionEvent(TheXDisplay, scr_num, x, y,
831 EventSendDelay);
832 XFlush(TheXDisplay);
833 #else
834 ULONG tmp;
835
836 /* I decided not to set our error handler, since the
837 window must exist. */
838 XWarpPointer(TheXDisplay, None,
839 RootWindow(TheXDisplay, scr_num),
840 0, 0, 0, 0,
841 x, y);
842 XSync(TheXDisplay, False);
843 tmp = EventSendDelay / (ULONG) 1000;
844 while ((ULONG) 0 != tmp)
845 tmp = (ULONG) sleep((int) tmp);
846 tmp = EventSendDelay % (ULONG) 1000;
847 usleep(1000 * tmp);
848 /* XWarpPointer's return status isn't a reliable success
849 * indicator (it's a one-way request, not a round-trip), so
850 * report success here once the request has been issued. */
851 RETVAL = (BOOL) 1;
852 #endif
853 } else {
854 RETVAL = (BOOL) 0;
855 }
856 OUTPUT:
857 RETVAL
858
859
860 void
861 GetMousePos()
862 PREINIT:
863 Window root = 0, child = 0;
864 int root_x = 0, root_y = 0;
865 int win_x = 0, win_y = 0, scr_num = 0;
866 UINT mask = 0;
867 PPCODE:
868 /* We do not bother to set our error handler because the
869 window given has to exist. */
870 XQueryPointer(TheXDisplay, RootWindow(TheXDisplay, TheScreen),
871 &root, &child, &root_x, &root_y, &win_x, &win_y, &mask);
872 EXTEND(SP, 3);
873 PUSHs( sv_2mortal(newSViv((IV)root_x)) );
874 PUSHs( sv_2mortal(newSViv((IV)root_y)) );
875 for (scr_num = ScreenCount(TheXDisplay) - 1; scr_num >= 0 ; --scr_num)
876 {
877 if (root == RootWindow(TheXDisplay, scr_num)) {
878 break;
879 }
880 assert(0 != scr_num);
881 /* There is something really wrong with the Xlib data
882 if this "assert" fails. */
883 }
884 PUSHs( sv_2mortal(newSViv((IV)scr_num)) );
885 XSRETURN(3);
886
887
888 BOOL
889 PressMouseButton(button)
890 int button
891 CODE:
892 RETVAL = (BOOL)XTestFakeButtonEvent(TheXDisplay, button, True, EventSendDelay);
893 XFlush(TheXDisplay);
894 OUTPUT:
895 RETVAL
896
897
898 BOOL
899 ReleaseMouseButton(button)
900 int button
901 CODE:
902 RETVAL = (BOOL)XTestFakeButtonEvent(TheXDisplay, button, False, EventSendDelay);
903 XFlush(TheXDisplay);
904 OUTPUT:
905 RETVAL
906
907
908 BOOL
909 SendKeys(keys)
910 char *keys
911 CODE:
912 RETVAL = SendKeysImp(keys);
913 OUTPUT:
914 RETVAL
915
916
917 BOOL
918 PressKey(key)
919 char *key
920 PREINIT:
921 KeySym sym = 0;
922 CODE:
923 RETVAL = GetKeySym(key, &sym);
924 if (RETVAL) {
925 RETVAL = PressKeyImp(sym);
926 }
927 OUTPUT:
928 RETVAL
929
930
931 BOOL
932 ReleaseKey(key)
933 char *key
934 PREINIT:
935 KeySym sym = 0;
936 CODE:
937 RETVAL = GetKeySym(key, &sym);
938 if (RETVAL) {
939 RETVAL = ReleaseKeyImp(sym);
940 }
941 OUTPUT:
942 RETVAL
943
944
945 BOOL
946 PressReleaseKey(key)
947 char *key
948 PREINIT:
949 KeySym sym = 0;
950 CODE:
951 RETVAL = GetKeySym(key, &sym);
952 if (RETVAL) {
953 RETVAL = PressReleaseKeyImp(sym);
954 }
955 OUTPUT:
956 RETVAL
957
958
959 BOOL
960 IsKeyPressed(key)
961 char *key
962 PREINIT:
963 int pos = 0;
964 KeySym sym = 0;
965 KeyCode kc = 0, skc = 0;
966 BOOL keyon = FALSE, shifton = FALSE;
967 char keys_return[KEYMAP_VECTOR_SIZE] = "";
968 CODE:
969 if (key && GetKeySym(key, &sym)) {
970 kc = GetKeycodeFromKeysym(TheXDisplay, sym);
971 skc = GetKeycodeFromKeysym(TheXDisplay, XK_Shift_L);
972 XQueryKeymap(TheXDisplay, keys_return);
973 for (pos = 0; pos < (KEYMAP_VECTOR_SIZE * KEYMAP_BIT_COUNT); pos++) {
974 /* For the derived keycode, are we at the correct bit position for it? */
975 if (kc == pos) {
976 /* Check the bit at this position to determine the state of the key */
977 if ( keys_return[pos / KEYMAP_BIT_COUNT] & (1 << (pos % KEYMAP_BIT_COUNT)) ) {
978 /* Bit On, so key is pressed */
979 keyon = TRUE;
980 }
981 }
982 /* For the shift keycode, ... */
983 if (skc == pos) {
984 /* Check the bit at this position to determine the state of the shift key */
985 if ( keys_return[pos / KEYMAP_BIT_COUNT] & (1 << (pos % KEYMAP_BIT_COUNT)) ) {
986 /* Bit On, so shift is pressed */
987 shifton = TRUE;
988 }
989 }
990 } /* for (pos = 0; pos < (KEYMAP_VECTOR_SIZE * KEYMAP_BIT_COUNT); pos++) { */
991 } /* if (key && GetKeySym(key, &sym)) { */
992
993 /* Determine result */
994 if (keyon) {
995 /* Key is on, so use its keysym to determine if shift modifier needs to be verified also */
996 if (IsShiftNeeded(sym)) {
997 RETVAL = (shifton);
998 } else {
999 RETVAL = (!shifton);
1000 }
1001 } else {
1002 /* Key not on, so it is not pressed */
1003 RETVAL = FALSE;
1004 }
1005 OUTPUT:
1006 RETVAL
1007
1008
1009 BOOL
1010 IsMouseButtonPressed(button)
1011 int button
1012 PREINIT:
1013 Window root = 0, child = 0;
1014 int root_x = 0, root_y = 0;
1015 int win_x = 0, win_y = 0;
1016 UINT mask = 0;
1017 CODE:
1018 XQueryPointer(TheXDisplay, RootWindow(TheXDisplay, TheScreen),
1019 &root, &child, &root_x, &root_y,
1020 &win_x, &win_y, &mask);
1021 switch (button) {
1022 case Button1:
1023 RETVAL = (mask & Button1Mask);
1024 break;
1025 case Button2:
1026 RETVAL = (mask & Button2Mask);
1027 break;
1028 case Button3:
1029 RETVAL = (mask & Button3Mask);
1030 break;
1031 case Button4:
1032 RETVAL = (mask & Button4Mask);
1033 break;
1034 case Button5:
1035 RETVAL = (mask & Button5Mask);
1036 break;
1037 default:
1038 RETVAL = FALSE;
1039 break;
1040 };
1041 OUTPUT:
1042 RETVAL
1043
1044 BOOL
1045 IsWindowCursor(win, cursor)
1046 Window win
1047 Cursor cursor
1048 CODE:
1049 OldErrorHandler = XSetErrorHandler(IgnoreBadWindow);
1050 RETVAL = XTestCompareCursorWithWindow(TheXDisplay, win, cursor);
1051 XSetErrorHandler(OldErrorHandler);
1052 OUTPUT:
1053 RETVAL
1054
1055 BOOL
1056 IsWindow(win)
1057 Window win
1058 CODE:
1059 RETVAL = IsWindowImp(win);
1060 OUTPUT:
1061 RETVAL
1062
1063
1064 BOOL
1065 IsWindowViewable(win)
1066 Window win
1067 PREINIT:
1068 XWindowAttributes wattrs = {0};
1069 CODE:
1070 OldErrorHandler = XSetErrorHandler(IgnoreBadWindow);
1071 if (!XGetWindowAttributes(TheXDisplay, win, &wattrs)) {
1072 RETVAL = FALSE;
1073 } else {
1074 RETVAL = (wattrs.map_state == IsViewable);
1075 }
1076 XSetErrorHandler(OldErrorHandler);
1077 OUTPUT:
1078 RETVAL
1079
1080
1081 BOOL
1082 MoveWindow(win, x, y)
1083 Window win
1084 int x
1085 int y
1086 CODE:
1087 OldErrorHandler = XSetErrorHandler(IgnoreBadWindow);
1088 RETVAL = XMoveWindow(TheXDisplay, win, x, y);
1089 XSync(TheXDisplay, False);
1090 XSetErrorHandler(OldErrorHandler);
1091 OUTPUT:
1092 RETVAL
1093
1094
1095 BOOL
1096 ResizeWindow(win, w, h)
1097 Window win
1098 int w
1099 int h
1100 CODE:
1101 OldErrorHandler = XSetErrorHandler(IgnoreBadWindow);
1102 RETVAL = XResizeWindow(TheXDisplay, win, w, h);
1103 XSync(TheXDisplay, False);
1104 XSetErrorHandler(OldErrorHandler);
1105 OUTPUT:
1106 RETVAL
1107
1108
1109 BOOL
1110 IconifyWindow(win)
1111 Window win
1112 PREINIT:
1113 XWindowAttributes wattrs = {0};
1114 int scr_num;
1115 CODE:
1116 OldErrorHandler = XSetErrorHandler(IgnoreBadWindow);
1117 if (XGetWindowAttributes(TheXDisplay, win, &wattrs)) {
1118 for (scr_num = ScreenCount(TheXDisplay) - 1;
1119 scr_num >= 0 ; --scr_num)
1120 {
1121 if ( wattrs.screen
1122 == ScreenOfDisplay(TheXDisplay, scr_num))
1123 {
1124 break;
1125 }
1126 /* There is something really wrong with the Xlib data
1127 if this "assert" fails. */
1128 assert(0 != scr_num);
1129 }
1130 RETVAL = XIconifyWindow(TheXDisplay, win, scr_num);
1131 XSync(TheXDisplay, False);
1132 } else {
1133 RETVAL = (BOOL) 0;
1134 }
1135 XSetErrorHandler(OldErrorHandler);
1136 OUTPUT:
1137 RETVAL
1138
1139
1140 BOOL
1141 UnIconifyWindow(win)
1142 Window win
1143 CODE:
1144 OldErrorHandler = XSetErrorHandler(IgnoreBadWindow);
1145 RETVAL = XMapWindow(TheXDisplay, win);
1146 XSync(TheXDisplay, False);
1147 XSetErrorHandler(OldErrorHandler);
1148 OUTPUT:
1149 RETVAL
1150
1151
1152 BOOL
1153 RaiseWindow(win)
1154 Window win
1155 CODE:
1156 OldErrorHandler = XSetErrorHandler(IgnoreBadWindow);
1157 RETVAL = XRaiseWindow(TheXDisplay, win);
1158 XSync(TheXDisplay, False);
1159 XSetErrorHandler(OldErrorHandler);
1160 OUTPUT:
1161 RETVAL
1162
1163
1164 BOOL
1165 LowerWindow(win)
1166 Window win
1167 CODE:
1168 OldErrorHandler = XSetErrorHandler(IgnoreBadWindow);
1169 RETVAL = XLowerWindow(TheXDisplay, win);
1170 XSync(TheXDisplay, False);
1171 XSetErrorHandler(OldErrorHandler);
1172 OUTPUT:
1173 RETVAL
1174
1175
1176 Window
1177 GetInputFocus()
1178 PREINIT:
1179 Window focus = 0;
1180 int revert = 0;
1181 CODE:
1182 XGetInputFocus(TheXDisplay, &focus, &revert);
1183 RETVAL = focus;
1184 OUTPUT:
1185 RETVAL
1186
1187
1188 BOOL
1189 SetInputFocus(win)
1190 Window win
1191 PREINIT:
1192 Window focus = 0;
1193 int revert = 0;
1194 CODE:
1195 OldErrorHandler = XSetErrorHandler(IgnoreBadWindow);
1196 /* Note: Per function man page, there is no effect if the time parameter
1197 * of this call isn't accurate. Will use CurrentTime. Also, it
1198 * appears that we can't trust its return value. */
1199 XSetInputFocus(TheXDisplay, win, RevertToParent, CurrentTime);
1200 XSync(TheXDisplay, False);
1201 XSetErrorHandler(OldErrorHandler);
1202 /* Verify that the window now has focus. Used to determine return value */
1203 XGetInputFocus(TheXDisplay, &focus, &revert);
1204 RETVAL = (focus == win);
1205 OUTPUT:
1206 RETVAL
1207
1208
1209 void
1210 GetWindowPos(win)
1211 Window win
1212 PREINIT:
1213 XWindowAttributes wattrs = {0};
1214 Window child = 0;
1215 int num_ret = 0, x = 0, y = 0, scr_num;
1216 PPCODE:
1217 OldErrorHandler = XSetErrorHandler(IgnoreBadWindow);
1218 if (XGetWindowAttributes(TheXDisplay, win, &wattrs)) {
1219 XTranslateCoordinates(TheXDisplay, win, wattrs.root,
1220 0 - wattrs.border_width, 0 - wattrs.border_width,
1221 &x, &y, &child);
1222 EXTEND(SP, 6);
1223 PUSHs( sv_2mortal(newSViv((IV)x)) );
1224 PUSHs( sv_2mortal(newSViv((IV)y)) );
1225 PUSHs( sv_2mortal(newSViv((IV)wattrs.width)) );
1226 PUSHs( sv_2mortal(newSViv((IV)wattrs.height)) );
1227 PUSHs( sv_2mortal(newSViv((IV)wattrs.border_width)) );
1228 for (scr_num = ScreenCount(TheXDisplay) - 1;
1229 scr_num >= 0 ; --scr_num)
1230 {
1231 if ( wattrs.screen
1232 == ScreenOfDisplay(TheXDisplay, scr_num))
1233 {
1234 break;
1235 }
1236 /* There is something really wrong with the Xlib data
1237 if this "assert" fails. */
1238 assert(0 != scr_num);
1239 }
1240 PUSHs( sv_2mortal(newSViv((IV)scr_num)) );
1241 num_ret = 6;
1242 }
1243 XSetErrorHandler(OldErrorHandler);
1244 XSRETURN(num_ret);
1245
1246 Window
1247 GetParentWindow(win)
1248 Window win
1249 PREINIT:
1250 Window parent = 0, *children = NULL, root = 0;
1251 UINT childcount = 0;
1252 CODE:
1253 RETVAL = 0;
1254 if (XQueryTree(TheXDisplay, win, &root, &parent, &children, &childcount)) {
1255 XFree(children);
1256 RETVAL = parent;
1257 }
1258 OUTPUT:
1259 RETVAL
1260
1261
1262 void
1263 GetScreenRes(scr_num = NO_INIT)
1264 int scr_num
1265 PREINIT:
1266 int x = 0, y = 0, num_ret = 0;
1267 PPCODE:
1268 if (0 == items)
1269 scr_num = TheScreen;
1270 if (scr_num >= 0 && scr_num < ScreenCount(TheXDisplay)) {
1271 x = DisplayWidth(TheXDisplay, scr_num);
1272 y = DisplayHeight(TheXDisplay, scr_num);
1273 EXTEND(SP, 2);
1274 PUSHs( sv_2mortal(newSViv((IV)x)) );
1275 PUSHs( sv_2mortal(newSViv((IV)y)) );
1276 num_ret = 2;
1277 }
1278 XSRETURN(num_ret);
1279
1280 int
1281 GetScreenDepth(scr_num = NO_INIT)
1282 int scr_num
1283 CODE:
1284 if (0 == items)
1285 scr_num = TheScreen;
1286 if (scr_num >= 0 && scr_num < ScreenCount(TheXDisplay)) {
1287 RETVAL = DefaultDepth(TheXDisplay, scr_num);
1288 } else {
1289 RETVAL = -1;
1290 }
1291 OUTPUT:
1292 RETVAL
1293
1294 unsigned long
1295 GetWindowPid(win)
1296 Window win
1297 PREINIT:
1298 Atom wm_pid_prop = None;
1299 Atom actual_type = None;
1300 int actual_format = 0;
1301 int status = 0;
1302 unsigned long nitems = 0;
1303 unsigned long bytes_after = 0;
1304 unsigned long *prop = NULL;
1305 CODE:
1306 RETVAL = 0;
1307 wm_pid_prop = XInternAtom(TheXDisplay, "_NET_WM_PID", False);
1308
1309 if (wm_pid_prop != None) {
1310 status = XGetWindowProperty(TheXDisplay, win, wm_pid_prop, 0,
1311 1, False, XA_CARDINAL, &actual_type, &actual_format,
1312 &nitems, &bytes_after, (unsigned char **)&prop);
1313 if (status == Success && actual_type != None) {
1314 RETVAL = *prop;
1315 XFree(prop);
1316 }
1317 }
1318 OUTPUT:
1319 RETVAL