1 #include <X11/Xlibint.h>
4 #include <X11/cursorfont.h>
5 #include <X11/keysymdef.h>
6 #include <X11/keysym.h>
7 #include <X11/extensions/XTest.h>
22 display = XOpenDisplay(getenv(
"DISPLAY"));
26 cerr <<
"Failed to open display.\n";
36 XCloseDisplay(display);
44 Window X11Controller::getWindowByNameRecursive(Window root,
const char * search_name,
int level)
46 Window root_window, parent_window, result, *child_windows;
47 unsigned int num_children = 0;
50 int ret = XQueryTree(display,
52 &root_window, &parent_window,
53 &child_windows, &num_children);
56 for(
unsigned int i=0; i<num_children; i++)
59 ret = XFetchName(display,child_windows[i],&name);
63 string str2 = search_name;
66 result = child_windows[i];
73 result = getWindowByNameRecursive(child_windows[i],
74 search_name, level + 1);
82 if(child_windows != NULL ) XFree(child_windows);
88 Window ret = getWindowByNameRecursive(XDefaultRootWindow(display), search_name, 0);
94 Window root_return, child_return;
95 int win_x_return, win_y_return;
96 unsigned int mask_return;
97 bool ret = XQueryPointer(display, XDefaultRootWindow(display),
98 &root_return, &child_return,
100 &win_x_return, &win_y_return,
107 XWindowAttributes win_attributes;
109 if(XGetWindowAttributes(display, window, &win_attributes))
111 return XTranslateCoordinates (display, window, win_attributes.root,
112 -win_attributes.border_width,
113 -win_attributes.border_width,
124 XWindowAttributes win_attributes;
125 if(XGetWindowAttributes(display, window, &win_attributes))
127 *width = win_attributes.width - win_attributes.border_width;
128 *height = win_attributes.height - win_attributes.border_width;
139 int cursor_x, cursor_y, window_x, window_y, diffx, diffy;
142 diffx = window_x - cursor_x;
143 diffy = window_y - cursor_y;
144 XWarpPointer(display, None, None, 0, 0, 0, 0, diffx, diffy);
145 bool ret = XWarpPointer(display, None, None, 0, 0, 0, 0, diffx, diffy);
153 int cursor_x, cursor_y, diffx, diffy;
155 diffx = cursor_x + x;
156 diffy = cursor_y + y;
158 bool ret = XWarpPointer(display, None, window, 0, 0, 0, 0, x, y);
165 bool ret = XWarpPointer(display, None, None, 0, 0, 0, 0, x, y);
175 bool ret = XTestFakeButtonEvent(display, button,
true, 10);
182 bool ret = XTestFakeButtonEvent(display, button,
false, 10);
221 KeyCode X11Controller::getKeyCodeFromString(
const char * key)
223 KeySym ks = XStringToKeysym(key);
224 KeyCode kc = XKeysymToKeycode(display, ks);
230 KeyCode kc = getKeyCodeFromString(key);
235 bool ret = XTestFakeKeyEvent(display, kc,
true, 10);
243 KeyCode kc = getKeyCodeFromString(key);
248 bool ret = XTestFakeKeyEvent(display, kc,
false, 10);
266 for(
unsigned i = 0; i < strlen(str); i++)
272 strcpy(send,
"space");
278 strcpy(send,
"Return");
296 XCloseDisplay(display);
304 int X11Controller::msleep(
unsigned long milisec)
306 struct timespec req={0};
307 time_t sec=(int)(milisec/1000);
308 milisec=milisec-(sec*1000);
310 req.tv_nsec=milisec*1000000L;
311 while(nanosleep(&req,&req)==-1)
316 int X11Controller::randrange(
int low,
int high)
318 return (rand() % (high - low)) + low;
321 void X11Controller::pause()