| 1 |
/* |
| 2 |
Copyright 1990, 1991 by UniSoft Group Limited |
| 3 |
*/ |
| 4 |
|
| 5 |
/* |
| 6 |
|
| 7 |
Copyright 1992, 1993, 1998 The Open Group |
| 8 |
|
| 9 |
Permission to use, copy, modify, distribute, and sell this software and its |
| 10 |
documentation for any purpose is hereby granted without fee, provided that |
| 11 |
the above copyright notice appear in all copies and that both that |
| 12 |
copyright notice and this permission notice appear in supporting |
| 13 |
documentation. |
| 14 |
|
| 15 |
The above copyright notice and this permission notice shall be included |
| 16 |
in all copies or substantial portions of the Software. |
| 17 |
|
| 18 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 19 |
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 20 |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 21 |
IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 22 |
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 23 |
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 24 |
OTHER DEALINGS IN THE SOFTWARE. |
| 25 |
|
| 26 |
Except as contained in this notice, the name of The Open Group shall |
| 27 |
not be used in advertising or otherwise to promote the sale, use or |
| 28 |
other dealings in this Software without prior written authorization |
| 29 |
from The Open Group. |
| 30 |
|
| 31 |
*/ |
| 32 |
|
| 33 |
#include <X11/Xlibint.h> |
| 34 |
#include <X11/extensions/XTest.h> |
| 35 |
#include <X11/extensions/xtestproto.h> |
| 36 |
#include <X11/extensions/Xext.h> |
| 37 |
#include <X11/extensions/extutil.h> |
| 38 |
#include <X11/extensions/XInput.h> |
| 39 |
#include <X11/extensions/XIproto.h> |
| 40 |
|
| 41 |
static XExtensionInfo _xtest_info_data; |
| 42 |
static XExtensionInfo *xtest_info = &_xtest_info_data; |
| 43 |
static const char *xtest_extension_name = XTestExtensionName; |
| 44 |
|
| 45 |
#define XTestCheckExtension(dpy,i,val) \ |
| 46 |
XextCheckExtension (dpy, i, xtest_extension_name, val) |
| 47 |
|
| 48 |
#define XTestICheckExtension(dpy,i,val) \ |
| 49 |
XextCheckExtension (dpy, i, xtest_extension_name, val); \ |
| 50 |
if (!i->data) return val |
| 51 |
|
| 52 |
/***************************************************************************** |
| 53 |
* * |
| 54 |
* private utility routines * |
| 55 |
* * |
| 56 |
*****************************************************************************/ |
| 57 |
|
| 58 |
static int close_display(Display *dpy, XExtCodes *codes); |
| 59 |
static /* const */ XExtensionHooks xtest_extension_hooks = { |
| 60 |
NULL, /* create_gc */ |
| 61 |
NULL, /* copy_gc */ |
| 62 |
NULL, /* flush_gc */ |
| 63 |
NULL, /* free_gc */ |
| 64 |
NULL, /* create_font */ |
| 65 |
NULL, /* free_font */ |
| 66 |
close_display, /* close_display */ |
| 67 |
NULL, /* wire_to_event */ |
| 68 |
NULL, /* event_to_wire */ |
| 69 |
NULL, /* error */ |
| 70 |
NULL /* error_string */ |
| 71 |
}; |
| 72 |
|
| 73 |
static XPointer |
| 74 |
get_xinput_base(Display *dpy) |
| 75 |
{ |
| 76 |
int major_opcode, first_event, first_error; |
| 77 |
first_event = 0; |
| 78 |
|
| 79 |
XQueryExtension(dpy, INAME, &major_opcode, &first_event, &first_error); |
| 80 |
return (XPointer)(long)first_event; |
| 81 |
} |
| 82 |
|
| 83 |
static XEXT_GENERATE_FIND_DISPLAY (find_display, xtest_info, |
| 84 |
xtest_extension_name, |
| 85 |
&xtest_extension_hooks, XTestNumberEvents, |
| 86 |
get_xinput_base(dpy)) |
| 87 |
|
| 88 |
static XEXT_GENERATE_CLOSE_DISPLAY (close_display, xtest_info) |
| 89 |
|
| 90 |
/***************************************************************************** |
| 91 |
* * |
| 92 |
* public routines * |
| 93 |
* * |
| 94 |
*****************************************************************************/ |
| 95 |
|
| 96 |
Bool |
| 97 |
XTestQueryExtension (Display *dpy, |
| 98 |
int *event_base_return, int *error_base_return, |
| 99 |
int *major_return, int *minor_return) |
| 100 |
{ |
| 101 |
XExtDisplayInfo *info = find_display (dpy); |
| 102 |
xXTestGetVersionReply rep; |
| 103 |
|
| 104 |
if (XextHasExtension(info)) { |
| 105 |
xXTestGetVersionReq *req; |
| 106 |
|
| 107 |
LockDisplay(dpy); |
| 108 |
GetReq(XTestGetVersion, req); |
| 109 |
req->reqType = info->codes->major_opcode; |
| 110 |
req->xtReqType = X_XTestGetVersion; |
| 111 |
req->majorVersion = XTestMajorVersion; |
| 112 |
req->minorVersion = XTestMinorVersion; |
| 113 |
if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) { |
| 114 |
UnlockDisplay(dpy); |
| 115 |
SyncHandle(); |
| 116 |
return False; |
| 117 |
} |
| 118 |
UnlockDisplay(dpy); |
| 119 |
SyncHandle(); |
| 120 |
*event_base_return = info->codes->first_event; |
| 121 |
*error_base_return = info->codes->first_error; |
| 122 |
*major_return = rep.majorVersion; |
| 123 |
*minor_return = rep.minorVersion; |
| 124 |
return True; |
| 125 |
} else { |
| 126 |
return False; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
Bool |
| 131 |
XTestCompareCursorWithWindow(Display *dpy, Window window, Cursor cursor) |
| 132 |
{ |
| 133 |
XExtDisplayInfo *info = find_display (dpy); |
| 134 |
register xXTestCompareCursorReq *req; |
| 135 |
xXTestCompareCursorReply rep; |
| 136 |
|
| 137 |
XTestCheckExtension (dpy, info, 0); |
| 138 |
|
| 139 |
LockDisplay(dpy); |
| 140 |
GetReq(XTestCompareCursor, req); |
| 141 |
req->reqType = info->codes->major_opcode; |
| 142 |
req->xtReqType = X_XTestCompareCursor; |
| 143 |
req->window = window; |
| 144 |
req->cursor = cursor; |
| 145 |
if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) { |
| 146 |
UnlockDisplay(dpy); |
| 147 |
SyncHandle(); |
| 148 |
return False; |
| 149 |
} |
| 150 |
UnlockDisplay(dpy); |
| 151 |
SyncHandle(); |
| 152 |
return rep.same; |
| 153 |
} |
| 154 |
|
| 155 |
Bool |
| 156 |
XTestCompareCurrentCursorWithWindow(Display *dpy, Window window) |
| 157 |
{ |
| 158 |
return XTestCompareCursorWithWindow(dpy, window, XTestCurrentCursor); |
| 159 |
} |
| 160 |
|
| 161 |
int |
| 162 |
XTestFakeKeyEvent(Display *dpy, unsigned int keycode, |
| 163 |
Bool is_press, unsigned long delay) |
| 164 |
{ |
| 165 |
XExtDisplayInfo *info = find_display (dpy); |
| 166 |
register xXTestFakeInputReq *req; |
| 167 |
|
| 168 |
XTestCheckExtension (dpy, info, 0); |
| 169 |
|
| 170 |
LockDisplay(dpy); |
| 171 |
GetReq(XTestFakeInput, req); |
| 172 |
req->reqType = info->codes->major_opcode; |
| 173 |
req->xtReqType = X_XTestFakeInput; |
| 174 |
req->type = is_press ? KeyPress : KeyRelease; |
| 175 |
req->detail = keycode; |
| 176 |
req->time = delay; |
| 177 |
UnlockDisplay(dpy); |
| 178 |
SyncHandle(); |
| 179 |
return 1; |
| 180 |
} |
| 181 |
|
| 182 |
int |
| 183 |
XTestFakeButtonEvent(Display *dpy, unsigned int button, |
| 184 |
Bool is_press, unsigned long delay) |
| 185 |
{ |
| 186 |
XExtDisplayInfo *info = find_display (dpy); |
| 187 |
register xXTestFakeInputReq *req; |
| 188 |
|
| 189 |
XTestCheckExtension (dpy, info, 0); |
| 190 |
|
| 191 |
LockDisplay(dpy); |
| 192 |
GetReq(XTestFakeInput, req); |
| 193 |
req->reqType = info->codes->major_opcode; |
| 194 |
req->xtReqType = X_XTestFakeInput; |
| 195 |
req->type = is_press ? ButtonPress : ButtonRelease; |
| 196 |
req->detail = button; |
| 197 |
req->time = delay; |
| 198 |
UnlockDisplay(dpy); |
| 199 |
SyncHandle(); |
| 200 |
return 1; |
| 201 |
} |
| 202 |
|
| 203 |
int |
| 204 |
XTestFakeMotionEvent(Display *dpy, int screen, int x, int y, unsigned long delay) |
| 205 |
{ |
| 206 |
XExtDisplayInfo *info = find_display (dpy); |
| 207 |
register xXTestFakeInputReq *req; |
| 208 |
|
| 209 |
XTestCheckExtension (dpy, info, 0); |
| 210 |
|
| 211 |
LockDisplay(dpy); |
| 212 |
GetReq(XTestFakeInput, req); |
| 213 |
req->reqType = info->codes->major_opcode; |
| 214 |
req->xtReqType = X_XTestFakeInput; |
| 215 |
req->type = MotionNotify; |
| 216 |
req->detail = False; |
| 217 |
if (screen == -1) |
| 218 |
req->root = None; |
| 219 |
else |
| 220 |
req->root = RootWindow(dpy, screen); |
| 221 |
req->rootX = x; |
| 222 |
req->rootY = y; |
| 223 |
req->time = delay; |
| 224 |
UnlockDisplay(dpy); |
| 225 |
SyncHandle(); |
| 226 |
return 1; |
| 227 |
} |
| 228 |
|
| 229 |
int |
| 230 |
XTestFakeRelativeMotionEvent(Display *dpy, int dx, int dy, unsigned long delay) |
| 231 |
{ |
| 232 |
XExtDisplayInfo *info = find_display (dpy); |
| 233 |
register xXTestFakeInputReq *req; |
| 234 |
|
| 235 |
XTestCheckExtension (dpy, info, 0); |
| 236 |
|
| 237 |
LockDisplay(dpy); |
| 238 |
GetReq(XTestFakeInput, req); |
| 239 |
req->reqType = info->codes->major_opcode; |
| 240 |
req->xtReqType = X_XTestFakeInput; |
| 241 |
req->type = MotionNotify; |
| 242 |
req->detail = True; |
| 243 |
req->root = None; |
| 244 |
req->rootX = dx; |
| 245 |
req->rootY = dy; |
| 246 |
req->time = delay; |
| 247 |
UnlockDisplay(dpy); |
| 248 |
SyncHandle(); |
| 249 |
return 1; |
| 250 |
} |
| 251 |
|
| 252 |
static void |
| 253 |
send_axes( |
| 254 |
Display *dpy, |
| 255 |
XExtDisplayInfo *info, |
| 256 |
xXTestFakeInputReq *req, |
| 257 |
XDevice *dev, |
| 258 |
int first_axis, |
| 259 |
int *axes, |
| 260 |
int n_axes) |
| 261 |
{ |
| 262 |
deviceValuator ev; |
| 263 |
|
| 264 |
req->deviceid |= MORE_EVENTS; |
| 265 |
req->length += ((n_axes + 5) / 6) * (SIZEOF(xEvent) >> 2); |
| 266 |
ev.type = XI_DeviceValuator + (long)info->data; |
| 267 |
ev.deviceid = dev->device_id; |
| 268 |
ev.first_valuator = first_axis; |
| 269 |
while (n_axes > 0) { |
| 270 |
int n = n_axes > 6 ? 6 : n_axes; |
| 271 |
ev.num_valuators = n; |
| 272 |
switch (n) { |
| 273 |
case 6: |
| 274 |
ev.valuator5 = *(axes+5); |
| 275 |
/* fallthrough */ |
| 276 |
case 5: |
| 277 |
ev.valuator4 = *(axes+4); |
| 278 |
/* fallthrough */ |
| 279 |
case 4: |
| 280 |
ev.valuator3 = *(axes+3); |
| 281 |
/* fallthrough */ |
| 282 |
case 3: |
| 283 |
ev.valuator2 = *(axes+2); |
| 284 |
/* fallthrough */ |
| 285 |
case 2: |
| 286 |
ev.valuator1 = *(axes+1); |
| 287 |
/* fallthrough */ |
| 288 |
case 1: |
| 289 |
ev.valuator0 = *axes; |
| 290 |
} |
| 291 |
Data(dpy, (char *)&ev, SIZEOF(xEvent)); |
| 292 |
axes += n; |
| 293 |
n_axes -= n; |
| 294 |
ev.first_valuator += n; |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
int |
| 299 |
XTestFakeDeviceKeyEvent(Display *dpy, XDevice *dev, |
| 300 |
unsigned int keycode, Bool is_press, |
| 301 |
int *axes, int n_axes, unsigned long delay) |
| 302 |
{ |
| 303 |
XExtDisplayInfo *info = find_display (dpy); |
| 304 |
register xXTestFakeInputReq *req; |
| 305 |
|
| 306 |
XTestICheckExtension (dpy, info, 0); |
| 307 |
|
| 308 |
LockDisplay(dpy); |
| 309 |
GetReq(XTestFakeInput, req); |
| 310 |
req->reqType = info->codes->major_opcode; |
| 311 |
req->xtReqType = X_XTestFakeInput; |
| 312 |
req->type = is_press ? XI_DeviceKeyPress : XI_DeviceKeyRelease; |
| 313 |
req->type += (int)(long)info->data; |
| 314 |
req->detail = keycode; |
| 315 |
req->time = delay; |
| 316 |
req->deviceid = dev->device_id; |
| 317 |
if (n_axes) |
| 318 |
send_axes(dpy, info, req, dev, 0, axes, n_axes); |
| 319 |
UnlockDisplay(dpy); |
| 320 |
SyncHandle(); |
| 321 |
return 1; |
| 322 |
} |
| 323 |
|
| 324 |
int |
| 325 |
XTestFakeDeviceButtonEvent(Display *dpy, XDevice *dev, |
| 326 |
unsigned int button, Bool is_press, |
| 327 |
int *axes, int n_axes, unsigned long delay) |
| 328 |
{ |
| 329 |
XExtDisplayInfo *info = find_display (dpy); |
| 330 |
register xXTestFakeInputReq *req; |
| 331 |
|
| 332 |
XTestICheckExtension (dpy, info, 0); |
| 333 |
|
| 334 |
LockDisplay(dpy); |
| 335 |
GetReq(XTestFakeInput, req); |
| 336 |
req->reqType = info->codes->major_opcode; |
| 337 |
req->xtReqType = X_XTestFakeInput; |
| 338 |
req->type = is_press ? XI_DeviceButtonPress : XI_DeviceButtonRelease; |
| 339 |
req->type += (int)(long)info->data; |
| 340 |
req->detail = button; |
| 341 |
req->time = delay; |
| 342 |
req->deviceid = dev->device_id; |
| 343 |
if (n_axes) |
| 344 |
send_axes(dpy, info, req, dev, 0, axes, n_axes); |
| 345 |
UnlockDisplay(dpy); |
| 346 |
SyncHandle(); |
| 347 |
return 1; |
| 348 |
} |
| 349 |
|
| 350 |
int |
| 351 |
XTestFakeProximityEvent(Display *dpy, XDevice *dev, Bool in_prox, |
| 352 |
int *axes, int n_axes, unsigned long delay) |
| 353 |
{ |
| 354 |
XExtDisplayInfo *info = find_display (dpy); |
| 355 |
register xXTestFakeInputReq *req; |
| 356 |
|
| 357 |
XTestICheckExtension (dpy, info, 0); |
| 358 |
|
| 359 |
LockDisplay(dpy); |
| 360 |
GetReq(XTestFakeInput, req); |
| 361 |
req->reqType = info->codes->major_opcode; |
| 362 |
req->xtReqType = X_XTestFakeInput; |
| 363 |
req->type = in_prox ? XI_ProximityIn : XI_ProximityOut; |
| 364 |
req->type += (int)(long)info->data; |
| 365 |
req->time = delay; |
| 366 |
req->deviceid = dev->device_id; |
| 367 |
if (n_axes) |
| 368 |
send_axes(dpy, info, req, dev, 0, axes, n_axes); |
| 369 |
UnlockDisplay(dpy); |
| 370 |
SyncHandle(); |
| 371 |
return 1; |
| 372 |
} |
| 373 |
|
| 374 |
int |
| 375 |
XTestFakeDeviceMotionEvent(Display *dpy, XDevice *dev, |
| 376 |
Bool is_relative, int first_axis, |
| 377 |
int *axes, int n_axes, unsigned long delay) |
| 378 |
{ |
| 379 |
XExtDisplayInfo *info = find_display (dpy); |
| 380 |
register xXTestFakeInputReq *req; |
| 381 |
|
| 382 |
XTestICheckExtension (dpy, info, 0); |
| 383 |
|
| 384 |
LockDisplay(dpy); |
| 385 |
GetReq(XTestFakeInput, req); |
| 386 |
req->reqType = info->codes->major_opcode; |
| 387 |
req->xtReqType = X_XTestFakeInput; |
| 388 |
req->type = XI_DeviceMotionNotify + (int)(long)info->data; |
| 389 |
req->detail = is_relative; |
| 390 |
req->time = delay; |
| 391 |
req->deviceid = dev->device_id; |
| 392 |
send_axes(dpy, info, req, dev, first_axis, axes, n_axes); |
| 393 |
UnlockDisplay(dpy); |
| 394 |
SyncHandle(); |
| 395 |
return 1; |
| 396 |
} |
| 397 |
|
| 398 |
int |
| 399 |
XTestGrabControl(Display *dpy, Bool impervious) |
| 400 |
{ |
| 401 |
XExtDisplayInfo *info = find_display (dpy); |
| 402 |
register xXTestGrabControlReq *req; |
| 403 |
|
| 404 |
XTestCheckExtension (dpy, info, 0); |
| 405 |
|
| 406 |
LockDisplay(dpy); |
| 407 |
GetReq(XTestGrabControl, req); |
| 408 |
req->reqType = info->codes->major_opcode; |
| 409 |
req->xtReqType = X_XTestGrabControl; |
| 410 |
req->impervious = impervious; |
| 411 |
UnlockDisplay(dpy); |
| 412 |
SyncHandle(); |
| 413 |
return 1; |
| 414 |
} |
| 415 |
|
| 416 |
void |
| 417 |
XTestSetGContextOfGC(GC gc, GContext gid) |
| 418 |
{ |
| 419 |
gc->gid = gid; |
| 420 |
} |
| 421 |
|
| 422 |
void |
| 423 |
XTestSetVisualIDOfVisual(Visual *visual, VisualID visualid) |
| 424 |
{ |
| 425 |
visual->visualid = visualid; |
| 426 |
} |
| 427 |
|
| 428 |
static xReq _dummy_request = { |
| 429 |
0, 0, 0 |
| 430 |
}; |
| 431 |
|
| 432 |
Status |
| 433 |
XTestDiscard(Display *dpy) |
| 434 |
{ |
| 435 |
Bool something; |
| 436 |
|
| 437 |
LockDisplay(dpy); |
| 438 |
if ((something = (dpy->bufptr != dpy->buffer))) { |
| 439 |
for (char *ptr = dpy->buffer; |
| 440 |
ptr < dpy->bufptr; |
| 441 |
ptr += (((xReq *)ptr)->length << 2)) |
| 442 |
dpy->request--; |
| 443 |
dpy->bufptr = dpy->buffer; |
| 444 |
dpy->last_req = (char *)&_dummy_request; |
| 445 |
} |
| 446 |
UnlockDisplay(dpy); |
| 447 |
SyncHandle(); |
| 448 |
return something; |
| 449 |
} |