~ubuntu-branches/ubuntu/lucid/libxtst/lucid-updates

« back to all changes in this revision

Viewing changes to src/XTest.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Stone
  • Date: 2005-07-23 00:49:26 UTC
  • Revision ID: james.westby@ubuntu.com-20050723004926-1ca6yr1i6pvn1pbp
Tags: upstream-1.13.0
ImportĀ upstreamĀ versionĀ 1.13.0

Show diffs side-by-side

added added

removed removed

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