1 /**
2  * Copyright © 2009 Red Hat, Inc.
3  *
4  *  Permission is hereby granted, free of charge, to any person obtaining a
5  *  copy of this software and associated documentation files (the "Software"),
6  *  to deal in the Software without restriction, including without limitation
7  *  the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  *  and/or sell copies of the Software, and to permit persons to whom the
9  *  Software is furnished to do so, subject to the following conditions:
10  *
11  *  The above copyright notice and this permission notice (including the next
12  *  paragraph) shall be included in all copies or substantial portions of the
13  *  Software.
14  *
15  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  *  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  *  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  *  DEALINGS IN THE SOFTWARE.
22  */
23 
24 #ifdef HAVE_DIX_CONFIG_H
25 #include <dix-config.h>
26 #endif
27 
28 #include <stdint.h>
29 #include <X11/X.h>
30 #include "misc.h"
31 #include "resource.h"
32 #include <X11/Xproto.h>
33 #include <X11/extensions/XI2proto.h>
34 #include <X11/Xatom.h>
35 #include "windowstr.h"
36 #include "inputstr.h"
37 #include "eventconvert.h"
38 #include "exevents.h"
39 #include "exglobals.h"
40 #include "dixgrabs.h"
41 #include "eventstr.h"
42 #include "inpututils.h"
43 #include "mi.h"
44 #include "assert.h"
45 
46 #include "tests-common.h"
47 
48 /**
49  * Init a device with axes.
50  * Verify values set on the device.
51  *
52  * Result: All axes set to default values (usually 0).
53  */
54 static void
dix_init_valuators(void)55 dix_init_valuators(void)
56 {
57     DeviceIntRec dev;
58     ValuatorClassPtr val;
59     AxisInfoPtr axis;
60     const int num_axes = 2;
61     int i;
62     Atom atoms[MAX_VALUATORS] = { 0 };
63 
64     memset(&dev, 0, sizeof(DeviceIntRec));
65     dev.type = MASTER_POINTER;  /* claim it's a master to stop ptracccel */
66 
67     assert(InitValuatorClassDeviceStruct(NULL, 0, atoms, 0, 0) == FALSE);
68     assert(InitValuatorClassDeviceStruct(&dev, num_axes, atoms, 0, Absolute));
69 
70     val = dev.valuator;
71     assert(val);
72     assert(val->numAxes == num_axes);
73     assert(val->numMotionEvents == 0);
74     assert(val->axisVal);
75 
76     for (i = 0; i < num_axes; i++) {
77         assert(val->axisVal[i] == 0);
78         assert(val->axes->min_value == NO_AXIS_LIMITS);
79         assert(val->axes->max_value == NO_AXIS_LIMITS);
80         assert(val->axes->mode == Absolute);
81     }
82 
83     assert(dev.last.numValuators == num_axes);
84 
85     /* invalid increment */
86     assert(SetScrollValuator
87            (&dev, 0, SCROLL_TYPE_VERTICAL, 0.0, SCROLL_FLAG_NONE) == FALSE);
88     /* invalid type */
89     assert(SetScrollValuator
90            (&dev, 0, SCROLL_TYPE_VERTICAL - 1, 1.0, SCROLL_FLAG_NONE) == FALSE);
91     assert(SetScrollValuator
92            (&dev, 0, SCROLL_TYPE_HORIZONTAL + 1, 1.0,
93             SCROLL_FLAG_NONE) == FALSE);
94     /* invalid axisnum */
95     assert(SetScrollValuator
96            (&dev, 2, SCROLL_TYPE_HORIZONTAL, 1.0, SCROLL_FLAG_NONE) == FALSE);
97 
98     /* valid */
99     assert(SetScrollValuator
100            (&dev, 0, SCROLL_TYPE_VERTICAL, 3.0, SCROLL_FLAG_NONE) == TRUE);
101     axis = &dev.valuator->axes[0];
102     assert(axis->scroll.increment == 3.0);
103     assert(axis->scroll.type == SCROLL_TYPE_VERTICAL);
104     assert(axis->scroll.flags == 0);
105 
106     /* valid */
107     assert(SetScrollValuator
108            (&dev, 1, SCROLL_TYPE_HORIZONTAL, 2.0, SCROLL_FLAG_NONE) == TRUE);
109     axis = &dev.valuator->axes[1];
110     assert(axis->scroll.increment == 2.0);
111     assert(axis->scroll.type == SCROLL_TYPE_HORIZONTAL);
112     assert(axis->scroll.flags == 0);
113 
114     /* can add another non-preffered axis */
115     assert(SetScrollValuator
116            (&dev, 1, SCROLL_TYPE_VERTICAL, 5.0, SCROLL_FLAG_NONE) == TRUE);
117     assert(SetScrollValuator
118            (&dev, 0, SCROLL_TYPE_HORIZONTAL, 5.0, SCROLL_FLAG_NONE) == TRUE);
119 
120     /* can overwrite with Preferred */
121     assert(SetScrollValuator
122            (&dev, 1, SCROLL_TYPE_VERTICAL, 5.5, SCROLL_FLAG_PREFERRED) == TRUE);
123     axis = &dev.valuator->axes[1];
124     assert(axis->scroll.increment == 5.5);
125     assert(axis->scroll.type == SCROLL_TYPE_VERTICAL);
126     assert(axis->scroll.flags == SCROLL_FLAG_PREFERRED);
127 
128     assert(SetScrollValuator
129            (&dev, 0, SCROLL_TYPE_HORIZONTAL, 8.8,
130             SCROLL_FLAG_PREFERRED) == TRUE);
131     axis = &dev.valuator->axes[0];
132     assert(axis->scroll.increment == 8.8);
133     assert(axis->scroll.type == SCROLL_TYPE_HORIZONTAL);
134     assert(axis->scroll.flags == SCROLL_FLAG_PREFERRED);
135 
136     /* can overwrite as none */
137     assert(SetScrollValuator(&dev, 0, SCROLL_TYPE_NONE, 5.0,
138                              SCROLL_FLAG_NONE) == TRUE);
139     axis = &dev.valuator->axes[0];
140     assert(axis->scroll.type == SCROLL_TYPE_NONE);
141 
142     /* can overwrite axis with new settings */
143     assert(SetScrollValuator
144            (&dev, 0, SCROLL_TYPE_VERTICAL, 5.0, SCROLL_FLAG_NONE) == TRUE);
145     axis = &dev.valuator->axes[0];
146     assert(axis->scroll.type == SCROLL_TYPE_VERTICAL);
147     assert(axis->scroll.increment == 5.0);
148     assert(axis->scroll.flags == SCROLL_FLAG_NONE);
149     assert(SetScrollValuator
150            (&dev, 0, SCROLL_TYPE_VERTICAL, 3.0, SCROLL_FLAG_NONE) == TRUE);
151     assert(axis->scroll.type == SCROLL_TYPE_VERTICAL);
152     assert(axis->scroll.increment == 3.0);
153     assert(axis->scroll.flags == SCROLL_FLAG_NONE);
154 }
155 
156 /* just check the known success cases, and that error cases set the client's
157  * error value correctly. */
158 static void
dix_check_grab_values(void)159 dix_check_grab_values(void)
160 {
161     ClientRec client;
162     GrabParameters param;
163     int rc;
164 
165     memset(&client, 0, sizeof(client));
166 
167     param.grabtype = CORE;
168     param.this_device_mode = GrabModeSync;
169     param.other_devices_mode = GrabModeSync;
170     param.modifiers = AnyModifier;
171     param.ownerEvents = FALSE;
172 
173     rc = CheckGrabValues(&client, &param);
174     assert(rc == Success);
175 
176     param.this_device_mode = GrabModeAsync;
177     rc = CheckGrabValues(&client, &param);
178     assert(rc == Success);
179 
180     param.this_device_mode = XIGrabModeTouch;
181     rc = CheckGrabValues(&client, &param);
182     assert(rc == Success);
183 
184     param.this_device_mode = XIGrabModeTouch + 1;
185     rc = CheckGrabValues(&client, &param);
186     assert(rc == BadValue);
187     assert(client.errorValue == param.this_device_mode);
188     assert(client.errorValue == XIGrabModeTouch + 1);
189 
190     param.this_device_mode = GrabModeSync;
191     param.other_devices_mode = GrabModeAsync;
192     rc = CheckGrabValues(&client, &param);
193 
194     param.this_device_mode = GrabModeSync;
195     param.other_devices_mode = XIGrabModeTouch;
196     rc = CheckGrabValues(&client, &param);
197     assert(rc == Success);
198     assert(rc == Success);
199 
200     param.other_devices_mode = XIGrabModeTouch + 1;
201     rc = CheckGrabValues(&client, &param);
202     assert(rc == BadValue);
203     assert(client.errorValue == param.other_devices_mode);
204     assert(client.errorValue == XIGrabModeTouch + 1);
205 
206     param.other_devices_mode = GrabModeSync;
207 
208     param.modifiers = 1 << 13;
209     rc = CheckGrabValues(&client, &param);
210     assert(rc == BadValue);
211     assert(client.errorValue == param.modifiers);
212     assert(client.errorValue == (1 << 13));
213 
214     param.modifiers = AnyModifier;
215     param.ownerEvents = TRUE;
216     rc = CheckGrabValues(&client, &param);
217     assert(rc == Success);
218 
219     param.ownerEvents = 3;
220     rc = CheckGrabValues(&client, &param);
221     assert(rc == BadValue);
222     assert(client.errorValue == param.ownerEvents);
223     assert(client.errorValue == 3);
224 }
225 
226 /**
227  * Convert various internal events to the matching core event and verify the
228  * parameters.
229  */
230 static void
dix_event_to_core(int type)231 dix_event_to_core(int type)
232 {
233     DeviceEvent ev = {};
234     xEvent *core;
235     int time;
236     int x, y;
237     int rc;
238     int state;
239     int detail;
240     int count;
241     const int ROOT_WINDOW_ID = 0x100;
242 
243     /* EventToCore memsets the event to 0 */
244 #define test_event() \
245     assert(rc == Success); \
246     assert(core); \
247     assert(count == 1); \
248     assert(core->u.u.type == type); \
249     assert(core->u.u.detail == detail); \
250     assert(core->u.keyButtonPointer.time == time); \
251     assert(core->u.keyButtonPointer.rootX == x); \
252     assert(core->u.keyButtonPointer.rootY == y); \
253     assert(core->u.keyButtonPointer.state == state); \
254     assert(core->u.keyButtonPointer.eventX == 0); \
255     assert(core->u.keyButtonPointer.eventY == 0); \
256     assert(core->u.keyButtonPointer.root == ROOT_WINDOW_ID); \
257     assert(core->u.keyButtonPointer.event == 0); \
258     assert(core->u.keyButtonPointer.child == 0); \
259     assert(core->u.keyButtonPointer.sameScreen == FALSE);
260 
261     x = 0;
262     y = 0;
263     time = 12345;
264     state = 0;
265     detail = 0;
266 
267     ev.header = 0xFF;
268     ev.length = sizeof(DeviceEvent);
269     ev.time = time;
270     ev.root_y = x;
271     ev.root_x = y;
272     SetBit(ev.valuators.mask, 0);
273     SetBit(ev.valuators.mask, 1);
274     ev.root = ROOT_WINDOW_ID;
275     ev.corestate = state;
276     ev.detail.key = detail;
277 
278     ev.type = type;
279     ev.detail.key = 0;
280     rc = EventToCore((InternalEvent *) &ev, &core, &count);
281     test_event();
282 
283     x = 1;
284     y = 2;
285     ev.root_x = x;
286     ev.root_y = y;
287     rc = EventToCore((InternalEvent *) &ev, &core, &count);
288     test_event();
289 
290     x = 0x7FFF;
291     y = 0x7FFF;
292     ev.root_x = x;
293     ev.root_y = y;
294     rc = EventToCore((InternalEvent *) &ev, &core, &count);
295     test_event();
296 
297     x = 0x8000;                 /* too high */
298     y = 0x8000;                 /* too high */
299     ev.root_x = x;
300     ev.root_y = y;
301     rc = EventToCore((InternalEvent *) &ev, &core, &count);
302     assert(rc == Success);
303     assert(core);
304     assert(count == 1);
305     assert(core->u.keyButtonPointer.rootX != x);
306     assert(core->u.keyButtonPointer.rootY != y);
307 
308     x = 0x7FFF;
309     y = 0x7FFF;
310     ev.root_x = x;
311     ev.root_y = y;
312     time = 0;
313     ev.time = time;
314     rc = EventToCore((InternalEvent *) &ev, &core, &count);
315     test_event();
316 
317     detail = 1;
318     ev.detail.key = detail;
319     rc = EventToCore((InternalEvent *) &ev, &core, &count);
320     test_event();
321 
322     detail = 0xFF;              /* highest value */
323     ev.detail.key = detail;
324     rc = EventToCore((InternalEvent *) &ev, &core, &count);
325     test_event();
326 
327     detail = 0xFFF;             /* too big */
328     ev.detail.key = detail;
329     rc = EventToCore((InternalEvent *) &ev, &core, &count);
330     assert(rc == BadMatch);
331 
332     detail = 0xFF;              /* too big */
333     ev.detail.key = detail;
334     state = 0xFFFF;             /* highest value */
335     ev.corestate = state;
336     rc = EventToCore((InternalEvent *) &ev, &core, &count);
337     test_event();
338 
339     state = 0x10000;            /* too big */
340     ev.corestate = state;
341     rc = EventToCore((InternalEvent *) &ev, &core, &count);
342     assert(rc == Success);
343     assert(core);
344     assert(count == 1);
345     assert(core->u.keyButtonPointer.state != state);
346     assert(core->u.keyButtonPointer.state == (state & 0xFFFF));
347 
348 #undef test_event
349 }
350 
351 static void
dix_event_to_core_fail(int evtype,int expected_rc)352 dix_event_to_core_fail(int evtype, int expected_rc)
353 {
354     DeviceEvent ev;
355     xEvent *core;
356     int rc;
357     int count;
358 
359     ev.header = 0xFF;
360     ev.length = sizeof(DeviceEvent);
361 
362     ev.type = evtype;
363     rc = EventToCore((InternalEvent *) &ev, &core, &count);
364     assert(rc == expected_rc);
365 }
366 
367 static void
dix_event_to_core_conversion(void)368 dix_event_to_core_conversion(void)
369 {
370     dix_event_to_core_fail(0, BadImplementation);
371     dix_event_to_core_fail(1, BadImplementation);
372     dix_event_to_core_fail(ET_ProximityOut + 1, BadImplementation);
373     dix_event_to_core_fail(ET_ProximityIn, BadMatch);
374     dix_event_to_core_fail(ET_ProximityOut, BadMatch);
375 
376     dix_event_to_core(ET_KeyPress);
377     dix_event_to_core(ET_KeyRelease);
378     dix_event_to_core(ET_ButtonPress);
379     dix_event_to_core(ET_ButtonRelease);
380     dix_event_to_core(ET_Motion);
381 }
382 
383 static void
_dix_test_xi_convert(DeviceEvent * ev,int expected_rc,int expected_count)384 _dix_test_xi_convert(DeviceEvent *ev, int expected_rc, int expected_count)
385 {
386     xEvent *xi;
387     int count = 0;
388     int rc;
389 
390     rc = EventToXI((InternalEvent *) ev, &xi, &count);
391     assert(rc == expected_rc);
392     assert(count >= expected_count);
393     if (count > 0) {
394         deviceKeyButtonPointer *kbp = (deviceKeyButtonPointer *) xi;
395 
396         assert(kbp->type == IEventBase + ev->type);
397         assert(kbp->detail == ev->detail.key);
398         assert(kbp->time == ev->time);
399         assert((kbp->deviceid & ~MORE_EVENTS) == ev->deviceid);
400         assert(kbp->root_x == ev->root_x);
401         assert(kbp->root_y == ev->root_y);
402         assert(kbp->state == ev->corestate);
403         assert(kbp->event_x == 0);
404         assert(kbp->event_y == 0);
405         assert(kbp->root == ev->root);
406         assert(kbp->event == 0);
407         assert(kbp->child == 0);
408         assert(kbp->same_screen == FALSE);
409 
410         while (--count > 0) {
411             deviceValuator *v = (deviceValuator *) &xi[count];
412 
413             assert(v->type == DeviceValuator);
414             assert(v->num_valuators <= 6);
415         }
416 
417         free(xi);
418     }
419 }
420 
421 /**
422  * This tests for internal event → XI1 event conversion
423  * - all conversions should generate the right XI event type
424  * - right number of events generated
425  * - extra events are valuators
426  */
427 static void
dix_event_to_xi1_conversion(void)428 dix_event_to_xi1_conversion(void)
429 {
430     DeviceEvent ev = { 0 };
431     int time;
432     int x, y;
433     int state;
434     int detail;
435     const int ROOT_WINDOW_ID = 0x100;
436     int deviceid;
437 
438     IEventBase = 80;
439     DeviceValuator = IEventBase - 1;
440     DeviceKeyPress = IEventBase + ET_KeyPress;
441     DeviceKeyRelease = IEventBase + ET_KeyRelease;
442     DeviceButtonPress = IEventBase + ET_ButtonPress;
443     DeviceButtonRelease = IEventBase + ET_ButtonRelease;
444     DeviceMotionNotify = IEventBase + ET_Motion;
445     DeviceFocusIn = IEventBase + ET_FocusIn;
446     DeviceFocusOut = IEventBase + ET_FocusOut;
447     ProximityIn = IEventBase + ET_ProximityIn;
448     ProximityOut = IEventBase + ET_ProximityOut;
449 
450     /* EventToXI callocs */
451     x = 0;
452     y = 0;
453     time = 12345;
454     state = 0;
455     detail = 0;
456     deviceid = 4;
457 
458     ev.header = 0xFF;
459 
460     ev.header = 0xFF;
461     ev.length = sizeof(DeviceEvent);
462     ev.time = time;
463     ev.root_y = x;
464     ev.root_x = y;
465     SetBit(ev.valuators.mask, 0);
466     SetBit(ev.valuators.mask, 1);
467     ev.root = ROOT_WINDOW_ID;
468     ev.corestate = state;
469     ev.detail.key = detail;
470     ev.deviceid = deviceid;
471 
472     /* test all types for bad match */
473     ev.type = ET_KeyPress;
474     _dix_test_xi_convert(&ev, Success, 1);
475     ev.type = ET_KeyRelease;
476     _dix_test_xi_convert(&ev, Success, 1);
477     ev.type = ET_ButtonPress;
478     _dix_test_xi_convert(&ev, Success, 1);
479     ev.type = ET_ButtonRelease;
480     _dix_test_xi_convert(&ev, Success, 1);
481     ev.type = ET_Motion;
482     _dix_test_xi_convert(&ev, Success, 1);
483     ev.type = ET_ProximityIn;
484     _dix_test_xi_convert(&ev, Success, 1);
485     ev.type = ET_ProximityOut;
486     _dix_test_xi_convert(&ev, Success, 1);
487 
488     /* No axes */
489     ClearBit(ev.valuators.mask, 0);
490     ClearBit(ev.valuators.mask, 1);
491     ev.type = ET_KeyPress;
492     _dix_test_xi_convert(&ev, Success, 1);
493     ev.type = ET_KeyRelease;
494     _dix_test_xi_convert(&ev, Success, 1);
495     ev.type = ET_ButtonPress;
496     _dix_test_xi_convert(&ev, Success, 1);
497     ev.type = ET_ButtonRelease;
498     _dix_test_xi_convert(&ev, Success, 1);
499     ev.type = ET_Motion;
500     _dix_test_xi_convert(&ev, BadMatch, 0);
501     ev.type = ET_ProximityIn;
502     _dix_test_xi_convert(&ev, BadMatch, 0);
503     ev.type = ET_ProximityOut;
504     _dix_test_xi_convert(&ev, BadMatch, 0);
505 
506     /* more than 6 axes → 2 valuator events */
507     SetBit(ev.valuators.mask, 0);
508     SetBit(ev.valuators.mask, 1);
509     SetBit(ev.valuators.mask, 2);
510     SetBit(ev.valuators.mask, 3);
511     SetBit(ev.valuators.mask, 4);
512     SetBit(ev.valuators.mask, 5);
513     SetBit(ev.valuators.mask, 6);
514     ev.type = ET_KeyPress;
515     _dix_test_xi_convert(&ev, Success, 2);
516     ev.type = ET_KeyRelease;
517     _dix_test_xi_convert(&ev, Success, 2);
518     ev.type = ET_ButtonPress;
519     _dix_test_xi_convert(&ev, Success, 2);
520     ev.type = ET_ButtonRelease;
521     _dix_test_xi_convert(&ev, Success, 2);
522     ev.type = ET_Motion;
523     _dix_test_xi_convert(&ev, Success, 2);
524     ev.type = ET_ProximityIn;
525     _dix_test_xi_convert(&ev, Success, 2);
526     ev.type = ET_ProximityOut;
527     _dix_test_xi_convert(&ev, Success, 2);
528 
529     /* keycode too high */
530     ev.type = ET_KeyPress;
531     ev.detail.key = 256;
532     _dix_test_xi_convert(&ev, Success, 0);
533 
534     /* deviceid too high */
535     ev.type = ET_KeyPress;
536     ev.detail.key = 18;
537     ev.deviceid = 128;
538     _dix_test_xi_convert(&ev, Success, 0);
539 }
540 
541 static void
xi2_struct_sizes(void)542 xi2_struct_sizes(void)
543 {
544 #define compare(req) \
545     assert(sizeof(req) == sz_##req);
546 
547     compare(xXIQueryVersionReq);
548     compare(xXIWarpPointerReq);
549     compare(xXIChangeCursorReq);
550     compare(xXIChangeHierarchyReq);
551     compare(xXISetClientPointerReq);
552     compare(xXIGetClientPointerReq);
553     compare(xXISelectEventsReq);
554     compare(xXIQueryVersionReq);
555     compare(xXIQueryDeviceReq);
556     compare(xXISetFocusReq);
557     compare(xXIGetFocusReq);
558     compare(xXIGrabDeviceReq);
559     compare(xXIUngrabDeviceReq);
560     compare(xXIAllowEventsReq);
561     compare(xXIPassiveGrabDeviceReq);
562     compare(xXIPassiveUngrabDeviceReq);
563     compare(xXIListPropertiesReq);
564     compare(xXIChangePropertyReq);
565     compare(xXIDeletePropertyReq);
566     compare(xXIGetPropertyReq);
567     compare(xXIGetSelectedEventsReq);
568 #undef compare
569 }
570 
571 static void
dix_grab_matching(void)572 dix_grab_matching(void)
573 {
574     DeviceIntRec xi_all_devices, xi_all_master_devices, dev1, dev2;
575     GrabRec a, b;
576     BOOL rc;
577 
578     memset(&a, 0, sizeof(a));
579     memset(&b, 0, sizeof(b));
580 
581     /* different grabtypes must fail */
582     a.grabtype = CORE;
583     b.grabtype = XI2;
584     rc = GrabMatchesSecond(&a, &b, FALSE);
585     assert(rc == FALSE);
586     rc = GrabMatchesSecond(&b, &a, FALSE);
587     assert(rc == FALSE);
588 
589     a.grabtype = XI;
590     b.grabtype = XI2;
591     rc = GrabMatchesSecond(&a, &b, FALSE);
592     assert(rc == FALSE);
593     rc = GrabMatchesSecond(&b, &a, FALSE);
594     assert(rc == FALSE);
595 
596     a.grabtype = XI;
597     b.grabtype = CORE;
598     rc = GrabMatchesSecond(&a, &b, FALSE);
599     assert(rc == FALSE);
600     rc = GrabMatchesSecond(&b, &a, FALSE);
601     assert(rc == FALSE);
602 
603     /* XI2 grabs for different devices must fail, regardless of ignoreDevice
604      * XI2 grabs for master devices must fail against a slave */
605     memset(&xi_all_devices, 0, sizeof(DeviceIntRec));
606     memset(&xi_all_master_devices, 0, sizeof(DeviceIntRec));
607     memset(&dev1, 0, sizeof(DeviceIntRec));
608     memset(&dev2, 0, sizeof(DeviceIntRec));
609 
610     xi_all_devices.id = XIAllDevices;
611     xi_all_master_devices.id = XIAllMasterDevices;
612     dev1.id = 10;
613     dev1.type = SLAVE;
614     dev2.id = 11;
615     dev2.type = SLAVE;
616 
617     inputInfo.all_devices = &xi_all_devices;
618     inputInfo.all_master_devices = &xi_all_master_devices;
619     a.grabtype = XI2;
620     b.grabtype = XI2;
621     a.device = &dev1;
622     b.device = &dev2;
623 
624     rc = GrabMatchesSecond(&a, &b, FALSE);
625     assert(rc == FALSE);
626 
627     a.device = &dev2;
628     b.device = &dev1;
629     rc = GrabMatchesSecond(&a, &b, FALSE);
630     assert(rc == FALSE);
631     rc = GrabMatchesSecond(&a, &b, TRUE);
632     assert(rc == FALSE);
633 
634     a.device = inputInfo.all_master_devices;
635     b.device = &dev1;
636     rc = GrabMatchesSecond(&a, &b, FALSE);
637     assert(rc == FALSE);
638     rc = GrabMatchesSecond(&a, &b, TRUE);
639     assert(rc == FALSE);
640 
641     a.device = &dev1;
642     b.device = inputInfo.all_master_devices;
643     rc = GrabMatchesSecond(&a, &b, FALSE);
644     assert(rc == FALSE);
645     rc = GrabMatchesSecond(&a, &b, TRUE);
646     assert(rc == FALSE);
647 
648     /* ignoreDevice FALSE must fail for different devices for CORE and XI */
649     a.grabtype = XI;
650     b.grabtype = XI;
651     a.device = &dev1;
652     b.device = &dev2;
653     a.modifierDevice = &dev1;
654     b.modifierDevice = &dev1;
655     rc = GrabMatchesSecond(&a, &b, FALSE);
656     assert(rc == FALSE);
657 
658     a.grabtype = CORE;
659     b.grabtype = CORE;
660     a.device = &dev1;
661     b.device = &dev2;
662     a.modifierDevice = &dev1;
663     b.modifierDevice = &dev1;
664     rc = GrabMatchesSecond(&a, &b, FALSE);
665     assert(rc == FALSE);
666 
667     /* ignoreDevice FALSE must fail for different modifier devices for CORE
668      * and XI */
669     a.grabtype = XI;
670     b.grabtype = XI;
671     a.device = &dev1;
672     b.device = &dev1;
673     a.modifierDevice = &dev1;
674     b.modifierDevice = &dev2;
675     rc = GrabMatchesSecond(&a, &b, FALSE);
676     assert(rc == FALSE);
677 
678     a.grabtype = CORE;
679     b.grabtype = CORE;
680     a.device = &dev1;
681     b.device = &dev1;
682     a.modifierDevice = &dev1;
683     b.modifierDevice = &dev2;
684     rc = GrabMatchesSecond(&a, &b, FALSE);
685     assert(rc == FALSE);
686 
687     /* different event type must fail */
688     a.grabtype = XI2;
689     b.grabtype = XI2;
690     a.device = &dev1;
691     b.device = &dev1;
692     a.modifierDevice = &dev1;
693     b.modifierDevice = &dev1;
694     a.type = XI_KeyPress;
695     b.type = XI_KeyRelease;
696     rc = GrabMatchesSecond(&a, &b, FALSE);
697     assert(rc == FALSE);
698     rc = GrabMatchesSecond(&a, &b, TRUE);
699     assert(rc == FALSE);
700 
701     a.grabtype = CORE;
702     b.grabtype = CORE;
703     a.device = &dev1;
704     b.device = &dev1;
705     a.modifierDevice = &dev1;
706     b.modifierDevice = &dev1;
707     a.type = XI_KeyPress;
708     b.type = XI_KeyRelease;
709     rc = GrabMatchesSecond(&a, &b, FALSE);
710     assert(rc == FALSE);
711     rc = GrabMatchesSecond(&a, &b, TRUE);
712     assert(rc == FALSE);
713 
714     a.grabtype = XI;
715     b.grabtype = XI;
716     a.device = &dev1;
717     b.device = &dev1;
718     a.modifierDevice = &dev1;
719     b.modifierDevice = &dev1;
720     a.type = XI_KeyPress;
721     b.type = XI_KeyRelease;
722     rc = GrabMatchesSecond(&a, &b, FALSE);
723     assert(rc == FALSE);
724     rc = GrabMatchesSecond(&a, &b, TRUE);
725     assert(rc == FALSE);
726 
727     /* different modifiers must fail */
728     a.grabtype = XI2;
729     b.grabtype = XI2;
730     a.device = &dev1;
731     b.device = &dev1;
732     a.modifierDevice = &dev1;
733     b.modifierDevice = &dev1;
734     a.type = XI_KeyPress;
735     b.type = XI_KeyPress;
736     a.modifiersDetail.exact = 1;
737     b.modifiersDetail.exact = 2;
738     rc = GrabMatchesSecond(&a, &b, FALSE);
739     assert(rc == FALSE);
740     rc = GrabMatchesSecond(&b, &a, FALSE);
741     assert(rc == FALSE);
742 
743     a.grabtype = CORE;
744     b.grabtype = CORE;
745     rc = GrabMatchesSecond(&a, &b, FALSE);
746     assert(rc == FALSE);
747     rc = GrabMatchesSecond(&b, &a, FALSE);
748     assert(rc == FALSE);
749 
750     a.grabtype = XI;
751     b.grabtype = XI;
752     rc = GrabMatchesSecond(&a, &b, FALSE);
753     assert(rc == FALSE);
754     rc = GrabMatchesSecond(&b, &a, FALSE);
755     assert(rc == FALSE);
756 
757     /* AnyModifier must fail for XI2 */
758     a.grabtype = XI2;
759     b.grabtype = XI2;
760     a.modifiersDetail.exact = AnyModifier;
761     b.modifiersDetail.exact = 1;
762     rc = GrabMatchesSecond(&a, &b, FALSE);
763     assert(rc == FALSE);
764     rc = GrabMatchesSecond(&b, &a, FALSE);
765     assert(rc == FALSE);
766 
767     /* XIAnyModifier must fail for CORE and XI */
768     a.grabtype = XI;
769     b.grabtype = XI;
770     a.modifiersDetail.exact = XIAnyModifier;
771     b.modifiersDetail.exact = 1;
772     rc = GrabMatchesSecond(&a, &b, FALSE);
773     assert(rc == FALSE);
774     rc = GrabMatchesSecond(&b, &a, FALSE);
775     assert(rc == FALSE);
776 
777     a.grabtype = CORE;
778     b.grabtype = CORE;
779     a.modifiersDetail.exact = XIAnyModifier;
780     b.modifiersDetail.exact = 1;
781     rc = GrabMatchesSecond(&a, &b, FALSE);
782     assert(rc == FALSE);
783     rc = GrabMatchesSecond(&b, &a, FALSE);
784     assert(rc == FALSE);
785 
786     /* different detail must fail */
787     a.grabtype = XI2;
788     b.grabtype = XI2;
789     a.detail.exact = 1;
790     b.detail.exact = 2;
791     a.modifiersDetail.exact = 1;
792     b.modifiersDetail.exact = 1;
793     rc = GrabMatchesSecond(&a, &b, FALSE);
794     assert(rc == FALSE);
795     rc = GrabMatchesSecond(&b, &a, FALSE);
796     assert(rc == FALSE);
797 
798     a.grabtype = XI;
799     b.grabtype = XI;
800     rc = GrabMatchesSecond(&a, &b, FALSE);
801     assert(rc == FALSE);
802     rc = GrabMatchesSecond(&b, &a, FALSE);
803     assert(rc == FALSE);
804 
805     a.grabtype = CORE;
806     b.grabtype = CORE;
807     rc = GrabMatchesSecond(&a, &b, FALSE);
808     assert(rc == FALSE);
809     rc = GrabMatchesSecond(&b, &a, FALSE);
810     assert(rc == FALSE);
811 
812     /* detail of AnyModifier must fail */
813     a.grabtype = XI2;
814     b.grabtype = XI2;
815     a.detail.exact = AnyModifier;
816     b.detail.exact = 1;
817     a.modifiersDetail.exact = 1;
818     b.modifiersDetail.exact = 1;
819     rc = GrabMatchesSecond(&a, &b, FALSE);
820     assert(rc == FALSE);
821     rc = GrabMatchesSecond(&b, &a, FALSE);
822     assert(rc == FALSE);
823 
824     a.grabtype = CORE;
825     b.grabtype = CORE;
826     rc = GrabMatchesSecond(&a, &b, FALSE);
827     assert(rc == FALSE);
828     rc = GrabMatchesSecond(&b, &a, FALSE);
829     assert(rc == FALSE);
830 
831     a.grabtype = XI;
832     b.grabtype = XI;
833     rc = GrabMatchesSecond(&a, &b, FALSE);
834     assert(rc == FALSE);
835     rc = GrabMatchesSecond(&b, &a, FALSE);
836     assert(rc == FALSE);
837 
838     /* detail of XIAnyModifier must fail */
839     a.grabtype = XI2;
840     b.grabtype = XI2;
841     a.detail.exact = XIAnyModifier;
842     b.detail.exact = 1;
843     a.modifiersDetail.exact = 1;
844     b.modifiersDetail.exact = 1;
845     rc = GrabMatchesSecond(&a, &b, FALSE);
846     assert(rc == FALSE);
847     rc = GrabMatchesSecond(&b, &a, FALSE);
848     assert(rc == FALSE);
849 
850     a.grabtype = CORE;
851     b.grabtype = CORE;
852     rc = GrabMatchesSecond(&a, &b, FALSE);
853     assert(rc == FALSE);
854     rc = GrabMatchesSecond(&b, &a, FALSE);
855     assert(rc == FALSE);
856 
857     a.grabtype = XI;
858     b.grabtype = XI;
859     rc = GrabMatchesSecond(&a, &b, FALSE);
860     assert(rc == FALSE);
861     rc = GrabMatchesSecond(&b, &a, FALSE);
862     assert(rc == FALSE);
863 
864     /* XIAnyModifier or AnyModifer must succeed */
865     a.grabtype = XI2;
866     b.grabtype = XI2;
867     a.detail.exact = 1;
868     b.detail.exact = 1;
869     a.modifiersDetail.exact = XIAnyModifier;
870     b.modifiersDetail.exact = 1;
871     rc = GrabMatchesSecond(&a, &b, FALSE);
872     assert(rc == TRUE);
873     rc = GrabMatchesSecond(&b, &a, FALSE);
874     assert(rc == TRUE);
875 
876     a.grabtype = CORE;
877     b.grabtype = CORE;
878     a.detail.exact = 1;
879     b.detail.exact = 1;
880     a.modifiersDetail.exact = AnyModifier;
881     b.modifiersDetail.exact = 1;
882     rc = GrabMatchesSecond(&a, &b, FALSE);
883     assert(rc == TRUE);
884     rc = GrabMatchesSecond(&b, &a, FALSE);
885     assert(rc == TRUE);
886 
887     a.grabtype = XI;
888     b.grabtype = XI;
889     a.detail.exact = 1;
890     b.detail.exact = 1;
891     a.modifiersDetail.exact = AnyModifier;
892     b.modifiersDetail.exact = 1;
893     rc = GrabMatchesSecond(&a, &b, FALSE);
894     assert(rc == TRUE);
895     rc = GrabMatchesSecond(&b, &a, FALSE);
896     assert(rc == TRUE);
897 
898     /* AnyKey or XIAnyKeycode must succeed */
899     a.grabtype = XI2;
900     b.grabtype = XI2;
901     a.detail.exact = XIAnyKeycode;
902     b.detail.exact = 1;
903     a.modifiersDetail.exact = 1;
904     b.modifiersDetail.exact = 1;
905     rc = GrabMatchesSecond(&a, &b, FALSE);
906     assert(rc == TRUE);
907     rc = GrabMatchesSecond(&b, &a, FALSE);
908     assert(rc == TRUE);
909 
910     a.grabtype = CORE;
911     b.grabtype = CORE;
912     a.detail.exact = AnyKey;
913     b.detail.exact = 1;
914     a.modifiersDetail.exact = 1;
915     b.modifiersDetail.exact = 1;
916     rc = GrabMatchesSecond(&a, &b, FALSE);
917     assert(rc == TRUE);
918     rc = GrabMatchesSecond(&b, &a, FALSE);
919     assert(rc == TRUE);
920 
921     a.grabtype = XI;
922     b.grabtype = XI;
923     a.detail.exact = AnyKey;
924     b.detail.exact = 1;
925     a.modifiersDetail.exact = 1;
926     b.modifiersDetail.exact = 1;
927     rc = GrabMatchesSecond(&a, &b, FALSE);
928     assert(rc == TRUE);
929     rc = GrabMatchesSecond(&b, &a, FALSE);
930     assert(rc == TRUE);
931 }
932 
933 static void
test_bits_to_byte(int i)934 test_bits_to_byte(int i)
935 {
936     int expected_bytes;
937 
938     expected_bytes = (i + 7) / 8;
939 
940     assert(bits_to_bytes(i) >= i / 8);
941     assert((bits_to_bytes(i) * 8) - i <= 7);
942     assert(expected_bytes == bits_to_bytes(i));
943 }
944 
945 static void
test_bytes_to_int32(int i)946 test_bytes_to_int32(int i)
947 {
948     int expected_4byte;
949 
950     expected_4byte = (i + 3) / 4;
951 
952     assert(bytes_to_int32(i) <= i);
953     assert((bytes_to_int32(i) * 4) - i <= 3);
954     assert(expected_4byte == bytes_to_int32(i));
955 }
956 
957 static void
test_pad_to_int32(int i)958 test_pad_to_int32(int i)
959 {
960     int expected_bytes;
961 
962     expected_bytes = ((i + 3) / 4) * 4;
963 
964     assert(pad_to_int32(i) >= i);
965     assert(pad_to_int32(i) - i <= 3);
966     assert(expected_bytes == pad_to_int32(i));
967 }
968 
969 static void
test_padding_for_int32(int i)970 test_padding_for_int32(int i)
971 {
972     static const int padlength[4] = { 0, 3, 2, 1 };
973     int expected_bytes = (((i + 3) / 4) * 4) - i;
974 
975     assert(padding_for_int32(i) >= 0);
976     assert(padding_for_int32(i) <= 3);
977     assert(padding_for_int32(i) == expected_bytes);
978     assert(padding_for_int32(i) == padlength[i & 3]);
979     assert((padding_for_int32(i) + i) == pad_to_int32(i));
980 }
981 
982 static void
include_byte_padding_macros(void)983 include_byte_padding_macros(void)
984 {
985     printf("Testing bits_to_bytes()\n");
986 
987     /* the macros don't provide overflow protection */
988     test_bits_to_byte(0);
989     test_bits_to_byte(1);
990     test_bits_to_byte(2);
991     test_bits_to_byte(7);
992     test_bits_to_byte(8);
993     test_bits_to_byte(0xFF);
994     test_bits_to_byte(0x100);
995     test_bits_to_byte(INT_MAX - 9);
996     test_bits_to_byte(INT_MAX - 8);
997 
998     printf("Testing bytes_to_int32()\n");
999 
1000     test_bytes_to_int32(0);
1001     test_bytes_to_int32(1);
1002     test_bytes_to_int32(2);
1003     test_bytes_to_int32(7);
1004     test_bytes_to_int32(8);
1005     test_bytes_to_int32(0xFF);
1006     test_bytes_to_int32(0x100);
1007     test_bytes_to_int32(0xFFFF);
1008     test_bytes_to_int32(0x10000);
1009     test_bytes_to_int32(0xFFFFFF);
1010     test_bytes_to_int32(0x1000000);
1011     test_bytes_to_int32(INT_MAX - 4);
1012     test_bytes_to_int32(INT_MAX - 3);
1013 
1014     printf("Testing pad_to_int32()\n");
1015 
1016     test_pad_to_int32(0);
1017     test_pad_to_int32(1);
1018     test_pad_to_int32(2);
1019     test_pad_to_int32(3);
1020     test_pad_to_int32(7);
1021     test_pad_to_int32(8);
1022     test_pad_to_int32(0xFF);
1023     test_pad_to_int32(0x100);
1024     test_pad_to_int32(0xFFFF);
1025     test_pad_to_int32(0x10000);
1026     test_pad_to_int32(0xFFFFFF);
1027     test_pad_to_int32(0x1000000);
1028     test_pad_to_int32(INT_MAX - 4);
1029     test_pad_to_int32(INT_MAX - 3);
1030 
1031     printf("Testing padding_for_int32()\n");
1032 
1033     test_padding_for_int32(0);
1034     test_padding_for_int32(1);
1035     test_padding_for_int32(2);
1036     test_padding_for_int32(3);
1037     test_padding_for_int32(7);
1038     test_padding_for_int32(8);
1039     test_padding_for_int32(0xFF);
1040     test_padding_for_int32(0x100);
1041     test_padding_for_int32(0xFFFF);
1042     test_padding_for_int32(0x10000);
1043     test_padding_for_int32(0xFFFFFF);
1044     test_padding_for_int32(0x1000000);
1045     test_padding_for_int32(INT_MAX - 4);
1046     test_padding_for_int32(INT_MAX - 3);
1047 }
1048 
1049 static void
xi_unregister_handlers(void)1050 xi_unregister_handlers(void)
1051 {
1052     DeviceIntRec dev;
1053     int handler;
1054 
1055     memset(&dev, 0, sizeof(dev));
1056 
1057     handler = XIRegisterPropertyHandler(&dev, NULL, NULL, NULL);
1058     assert(handler == 1);
1059     handler = XIRegisterPropertyHandler(&dev, NULL, NULL, NULL);
1060     assert(handler == 2);
1061     handler = XIRegisterPropertyHandler(&dev, NULL, NULL, NULL);
1062     assert(handler == 3);
1063 
1064     printf("Unlinking from front.\n");
1065 
1066     XIUnregisterPropertyHandler(&dev, 4);       /* NOOP */
1067     assert(dev.properties.handlers->id == 3);
1068     XIUnregisterPropertyHandler(&dev, 3);
1069     assert(dev.properties.handlers->id == 2);
1070     XIUnregisterPropertyHandler(&dev, 2);
1071     assert(dev.properties.handlers->id == 1);
1072     XIUnregisterPropertyHandler(&dev, 1);
1073     assert(dev.properties.handlers == NULL);
1074 
1075     handler = XIRegisterPropertyHandler(&dev, NULL, NULL, NULL);
1076     assert(handler == 4);
1077     handler = XIRegisterPropertyHandler(&dev, NULL, NULL, NULL);
1078     assert(handler == 5);
1079     handler = XIRegisterPropertyHandler(&dev, NULL, NULL, NULL);
1080     assert(handler == 6);
1081     XIUnregisterPropertyHandler(&dev, 3);       /* NOOP */
1082     assert(dev.properties.handlers->next->next->next == NULL);
1083     XIUnregisterPropertyHandler(&dev, 4);
1084     assert(dev.properties.handlers->next->next == NULL);
1085     XIUnregisterPropertyHandler(&dev, 5);
1086     assert(dev.properties.handlers->next == NULL);
1087     XIUnregisterPropertyHandler(&dev, 6);
1088     assert(dev.properties.handlers == NULL);
1089 
1090     handler = XIRegisterPropertyHandler(&dev, NULL, NULL, NULL);
1091     assert(handler == 7);
1092     handler = XIRegisterPropertyHandler(&dev, NULL, NULL, NULL);
1093     assert(handler == 8);
1094     handler = XIRegisterPropertyHandler(&dev, NULL, NULL, NULL);
1095     assert(handler == 9);
1096 
1097     XIDeleteAllDeviceProperties(&dev);
1098     assert(dev.properties.handlers == NULL);
1099     XIUnregisterPropertyHandler(&dev, 7);       /* NOOP */
1100 
1101 }
1102 
1103 static void
cmp_attr_fields(InputAttributes * attr1,InputAttributes * attr2)1104 cmp_attr_fields(InputAttributes * attr1, InputAttributes * attr2)
1105 {
1106     char **tags1, **tags2;
1107 
1108     assert(attr1 && attr2);
1109     assert(attr1 != attr2);
1110     assert(attr1->flags == attr2->flags);
1111 
1112     if (attr1->product != NULL) {
1113         assert(attr1->product != attr2->product);
1114         assert(strcmp(attr1->product, attr2->product) == 0);
1115     }
1116     else
1117         assert(attr2->product == NULL);
1118 
1119     if (attr1->vendor != NULL) {
1120         assert(attr1->vendor != attr2->vendor);
1121         assert(strcmp(attr1->vendor, attr2->vendor) == 0);
1122     }
1123     else
1124         assert(attr2->vendor == NULL);
1125 
1126     if (attr1->device != NULL) {
1127         assert(attr1->device != attr2->device);
1128         assert(strcmp(attr1->device, attr2->device) == 0);
1129     }
1130     else
1131         assert(attr2->device == NULL);
1132 
1133     if (attr1->pnp_id != NULL) {
1134         assert(attr1->pnp_id != attr2->pnp_id);
1135         assert(strcmp(attr1->pnp_id, attr2->pnp_id) == 0);
1136     }
1137     else
1138         assert(attr2->pnp_id == NULL);
1139 
1140     if (attr1->usb_id != NULL) {
1141         assert(attr1->usb_id != attr2->usb_id);
1142         assert(strcmp(attr1->usb_id, attr2->usb_id) == 0);
1143     }
1144     else
1145         assert(attr2->usb_id == NULL);
1146 
1147     tags1 = attr1->tags;
1148     tags2 = attr2->tags;
1149 
1150     /* if we don't have any tags, skip the tag checking bits */
1151     if (!tags1) {
1152         assert(!tags2);
1153         return;
1154     }
1155 
1156     /* Don't lug around empty arrays */
1157     assert(*tags1);
1158     assert(*tags2);
1159 
1160     /* check for identical content, but duplicated */
1161     while (*tags1) {
1162         assert(*tags1 != *tags2);
1163         assert(strcmp(*tags1, *tags2) == 0);
1164         tags1++;
1165         tags2++;
1166     }
1167 
1168     /* ensure tags1 and tags2 have the same no of elements */
1169     assert(!*tags2);
1170 
1171     /* check for not sharing memory */
1172     tags1 = attr1->tags;
1173     while (*tags1) {
1174         tags2 = attr2->tags;
1175         while (*tags2)
1176             assert(*tags1 != *tags2++);
1177 
1178         tags1++;
1179     }
1180 }
1181 
1182 static void
dix_input_attributes(void)1183 dix_input_attributes(void)
1184 {
1185     InputAttributes *orig;
1186     InputAttributes *new;
1187 
1188     new = DuplicateInputAttributes(NULL);
1189     assert(!new);
1190 
1191     orig = calloc(1, sizeof(InputAttributes));
1192     assert(orig);
1193 
1194     new = DuplicateInputAttributes(orig);
1195     assert(memcmp(orig, new, sizeof(InputAttributes)) == 0);
1196 
1197     orig->product = xnfstrdup("product name");
1198     new = DuplicateInputAttributes(orig);
1199     cmp_attr_fields(orig, new);
1200     FreeInputAttributes(new);
1201 
1202     orig->vendor = xnfstrdup("vendor name");
1203     new = DuplicateInputAttributes(orig);
1204     cmp_attr_fields(orig, new);
1205     FreeInputAttributes(new);
1206 
1207     orig->device = xnfstrdup("device path");
1208     new = DuplicateInputAttributes(orig);
1209     cmp_attr_fields(orig, new);
1210     FreeInputAttributes(new);
1211 
1212     orig->pnp_id = xnfstrdup("PnPID");
1213     new = DuplicateInputAttributes(orig);
1214     cmp_attr_fields(orig, new);
1215     FreeInputAttributes(new);
1216 
1217     orig->usb_id = xnfstrdup("USBID");
1218     new = DuplicateInputAttributes(orig);
1219     cmp_attr_fields(orig, new);
1220     FreeInputAttributes(new);
1221 
1222     orig->flags = 0xF0;
1223     new = DuplicateInputAttributes(orig);
1224     cmp_attr_fields(orig, new);
1225     FreeInputAttributes(new);
1226 
1227     orig->tags = xstrtokenize("tag1 tag2 tag3", " ");
1228     new = DuplicateInputAttributes(orig);
1229     cmp_attr_fields(orig, new);
1230     FreeInputAttributes(new);
1231 
1232     FreeInputAttributes(orig);
1233 }
1234 
1235 static void
dix_input_valuator_masks(void)1236 dix_input_valuator_masks(void)
1237 {
1238     ValuatorMask *mask = NULL, *copy;
1239     int nvaluators = MAX_VALUATORS;
1240     double valuators[nvaluators];
1241     int val_ranged[nvaluators];
1242     int i;
1243     int first_val, num_vals;
1244 
1245     for (i = 0; i < nvaluators; i++) {
1246         valuators[i] = i + 0.5;
1247         val_ranged[i] = i;
1248     }
1249 
1250     mask = valuator_mask_new(nvaluators);
1251     assert(mask != NULL);
1252     assert(valuator_mask_size(mask) == 0);
1253     assert(valuator_mask_num_valuators(mask) == 0);
1254 
1255     for (i = 0; i < nvaluators; i++) {
1256         assert(!valuator_mask_isset(mask, i));
1257         valuator_mask_set_double(mask, i, valuators[i]);
1258         assert(valuator_mask_isset(mask, i));
1259         assert(valuator_mask_get(mask, i) == trunc(valuators[i]));
1260         assert(valuator_mask_get_double(mask, i) == valuators[i]);
1261         assert(valuator_mask_size(mask) == i + 1);
1262         assert(valuator_mask_num_valuators(mask) == i + 1);
1263     }
1264 
1265     for (i = 0; i < nvaluators; i++) {
1266         assert(valuator_mask_isset(mask, i));
1267         valuator_mask_unset(mask, i);
1268         /* we're removing valuators from the front, so size should stay the
1269          * same until the last bit is removed */
1270         if (i < nvaluators - 1)
1271             assert(valuator_mask_size(mask) == nvaluators);
1272         assert(!valuator_mask_isset(mask, i));
1273     }
1274 
1275     assert(valuator_mask_size(mask) == 0);
1276     valuator_mask_zero(mask);
1277     assert(valuator_mask_size(mask) == 0);
1278     assert(valuator_mask_num_valuators(mask) == 0);
1279     for (i = 0; i < nvaluators; i++)
1280         assert(!valuator_mask_isset(mask, i));
1281 
1282     first_val = 5;
1283     num_vals = 6;
1284 
1285     valuator_mask_set_range(mask, first_val, num_vals, val_ranged);
1286     assert(valuator_mask_size(mask) == first_val + num_vals);
1287     assert(valuator_mask_num_valuators(mask) == num_vals);
1288     for (i = 0; i < nvaluators; i++) {
1289         double val;
1290 
1291         if (i < first_val || i >= first_val + num_vals) {
1292             assert(!valuator_mask_isset(mask, i));
1293             assert(!valuator_mask_fetch_double(mask, i, &val));
1294         }
1295         else {
1296             assert(valuator_mask_isset(mask, i));
1297             assert(valuator_mask_get(mask, i) == val_ranged[i - first_val]);
1298             assert(valuator_mask_get_double(mask, i) ==
1299                    val_ranged[i - first_val]);
1300             assert(valuator_mask_fetch_double(mask, i, &val));
1301             assert(val_ranged[i - first_val] == val);
1302         }
1303     }
1304 
1305     copy = valuator_mask_new(nvaluators);
1306     valuator_mask_copy(copy, mask);
1307     assert(mask != copy);
1308     assert(valuator_mask_size(mask) == valuator_mask_size(copy));
1309     assert(valuator_mask_num_valuators(mask) ==
1310            valuator_mask_num_valuators(copy));
1311 
1312     for (i = 0; i < nvaluators; i++) {
1313         double a, b;
1314 
1315         assert(valuator_mask_isset(mask, i) == valuator_mask_isset(copy, i));
1316 
1317         if (!valuator_mask_isset(mask, i))
1318             continue;
1319 
1320         assert(valuator_mask_get(mask, i) == valuator_mask_get(copy, i));
1321         assert(valuator_mask_get_double(mask, i) ==
1322                valuator_mask_get_double(copy, i));
1323         assert(valuator_mask_fetch_double(mask, i, &a));
1324         assert(valuator_mask_fetch_double(copy, i, &b));
1325         assert(a == b);
1326     }
1327 
1328     valuator_mask_free(&mask);
1329     assert(mask == NULL);
1330 }
1331 
1332 static void
dix_valuator_mode(void)1333 dix_valuator_mode(void)
1334 {
1335     DeviceIntRec dev;
1336     const int num_axes = MAX_VALUATORS;
1337     int i;
1338     Atom atoms[MAX_VALUATORS] = { 0 };
1339 
1340     memset(&dev, 0, sizeof(DeviceIntRec));
1341     dev.type = MASTER_POINTER;  /* claim it's a master to stop ptracccel */
1342 
1343     assert(InitValuatorClassDeviceStruct(NULL, 0, atoms, 0, 0) == FALSE);
1344     assert(InitValuatorClassDeviceStruct(&dev, num_axes, atoms, 0, Absolute));
1345 
1346     for (i = 0; i < num_axes; i++) {
1347         assert(valuator_get_mode(&dev, i) == Absolute);
1348         valuator_set_mode(&dev, i, Relative);
1349         assert(dev.valuator->axes[i].mode == Relative);
1350         assert(valuator_get_mode(&dev, i) == Relative);
1351     }
1352 
1353     valuator_set_mode(&dev, VALUATOR_MODE_ALL_AXES, Absolute);
1354     for (i = 0; i < num_axes; i++)
1355         assert(valuator_get_mode(&dev, i) == Absolute);
1356 
1357     valuator_set_mode(&dev, VALUATOR_MODE_ALL_AXES, Relative);
1358     for (i = 0; i < num_axes; i++)
1359         assert(valuator_get_mode(&dev, i) == Relative);
1360 }
1361 
1362 static void
dix_input_valuator_masks_unaccel(void)1363 dix_input_valuator_masks_unaccel(void)
1364 {
1365     ValuatorMask *mask = NULL;
1366     double x, ux;
1367 
1368     /* set mask normally */
1369     mask = valuator_mask_new(MAX_VALUATORS);
1370     assert(!valuator_mask_has_unaccelerated(mask));
1371     valuator_mask_set_double(mask, 0, 1.0);
1372     assert(!valuator_mask_has_unaccelerated(mask));
1373     valuator_mask_unset(mask, 0);
1374     assert(!valuator_mask_has_unaccelerated(mask));
1375 
1376     /* all unset, now set accel mask */
1377     valuator_mask_set_unaccelerated(mask, 0, 1.0, 2.0);
1378     assert(valuator_mask_has_unaccelerated(mask));
1379     assert(valuator_mask_isset(mask, 0));
1380     assert(!valuator_mask_isset(mask, 1));
1381     assert(valuator_mask_get_accelerated(mask, 0) ==  1.0);
1382     assert(valuator_mask_get_unaccelerated(mask, 0) ==  2.0);
1383     assert(valuator_mask_fetch_unaccelerated(mask, 0, &x, &ux));
1384     assert(x == 1.0);
1385     assert(ux == 2.0);
1386     x = 0xff;
1387     ux = 0xfe;
1388     assert(!valuator_mask_fetch_unaccelerated(mask, 1, &x, &ux));
1389     assert(x == 0xff);
1390     assert(ux == 0xfe);
1391 
1392     /* all unset, now set normally again */
1393     valuator_mask_unset(mask, 0);
1394     assert(!valuator_mask_has_unaccelerated(mask));
1395     assert(!valuator_mask_isset(mask, 0));
1396     valuator_mask_set_double(mask, 0, 1.0);
1397     assert(!valuator_mask_has_unaccelerated(mask));
1398     valuator_mask_unset(mask, 0);
1399     assert(!valuator_mask_has_unaccelerated(mask));
1400 
1401     valuator_mask_zero(mask);
1402     assert(!valuator_mask_has_unaccelerated(mask));
1403 
1404     valuator_mask_set_unaccelerated(mask, 0, 1.0, 2.0);
1405     valuator_mask_set_unaccelerated(mask, 1, 3.0, 4.5);
1406     assert(valuator_mask_isset(mask, 0));
1407     assert(valuator_mask_isset(mask, 1));
1408     assert(!valuator_mask_isset(mask, 2));
1409     assert(valuator_mask_has_unaccelerated(mask));
1410     assert(valuator_mask_get_accelerated(mask, 0) == 1.0);
1411     assert(valuator_mask_get_accelerated(mask, 1) == 3.0);
1412     assert(valuator_mask_get_unaccelerated(mask, 0) == 2.0);
1413     assert(valuator_mask_get_unaccelerated(mask, 1) == 4.5);
1414     assert(valuator_mask_fetch_unaccelerated(mask, 0, &x, &ux));
1415     assert(x == 1.0);
1416     assert(ux == 2.0);
1417     assert(valuator_mask_fetch_unaccelerated(mask, 1, &x, &ux));
1418     assert(x == 3.0);
1419     assert(ux == 4.5);
1420 
1421     valuator_mask_free(&mask);
1422 }
1423 
1424 static void
include_bit_test_macros(void)1425 include_bit_test_macros(void)
1426 {
1427     uint8_t mask[9] = { 0 };
1428     int i;
1429 
1430     for (i = 0; i < ARRAY_SIZE(mask); i++) {
1431         assert(BitIsOn(mask, i) == 0);
1432         SetBit(mask, i);
1433         assert(BitIsOn(mask, i) == 1);
1434         assert(! !(mask[i / 8] & (1 << (i % 8))));
1435         assert(CountBits(mask, sizeof(mask)) == 1);
1436         ClearBit(mask, i);
1437         assert(BitIsOn(mask, i) == 0);
1438     }
1439 }
1440 
1441 /**
1442  * Ensure that val->axisVal and val->axes are aligned on doubles.
1443  */
1444 static void
dix_valuator_alloc(void)1445 dix_valuator_alloc(void)
1446 {
1447     ValuatorClassPtr v = NULL;
1448     int num_axes = 0;
1449 
1450     while (num_axes < 5) {
1451         v = AllocValuatorClass(v, num_axes);
1452 
1453         assert(v);
1454         assert(v->numAxes == num_axes);
1455 #if !defined(__i386__) && !defined(__m68k__) && !defined(__sh__)
1456         /* must be double-aligned on 64 bit */
1457         assert(offsetof(struct _ValuatorClassRec, axisVal) % sizeof(double) == 0);
1458         assert(offsetof(struct _ValuatorClassRec, axes) % sizeof(double) == 0);
1459 #endif
1460         num_axes++;
1461     }
1462 
1463     free(v);
1464 }
1465 
1466 static void
dix_get_master(void)1467 dix_get_master(void)
1468 {
1469     DeviceIntRec vcp, vck;
1470     DeviceIntRec ptr, kbd;
1471     DeviceIntRec floating;
1472     SpriteInfoRec vcp_sprite, vck_sprite;
1473     SpriteInfoRec ptr_sprite, kbd_sprite;
1474     SpriteInfoRec floating_sprite;
1475 
1476     memset(&vcp, 0, sizeof(vcp));
1477     memset(&vck, 0, sizeof(vck));
1478     memset(&ptr, 0, sizeof(ptr));
1479     memset(&kbd, 0, sizeof(kbd));
1480     memset(&floating, 0, sizeof(floating));
1481 
1482     memset(&vcp_sprite, 0, sizeof(vcp_sprite));
1483     memset(&vck_sprite, 0, sizeof(vck_sprite));
1484     memset(&ptr_sprite, 0, sizeof(ptr_sprite));
1485     memset(&kbd_sprite, 0, sizeof(kbd_sprite));
1486     memset(&floating_sprite, 0, sizeof(floating_sprite));
1487 
1488     vcp.type = MASTER_POINTER;
1489     vck.type = MASTER_KEYBOARD;
1490     ptr.type = SLAVE;
1491     kbd.type = SLAVE;
1492     floating.type = SLAVE;
1493 
1494     vcp.spriteInfo = &vcp_sprite;
1495     vck.spriteInfo = &vck_sprite;
1496     ptr.spriteInfo = &ptr_sprite;
1497     kbd.spriteInfo = &kbd_sprite;
1498     floating.spriteInfo = &floating_sprite;
1499 
1500     vcp_sprite.paired = &vck;
1501     vck_sprite.paired = &vcp;
1502     ptr_sprite.paired = &vcp;
1503     kbd_sprite.paired = &vck;
1504     floating_sprite.paired = &floating;
1505 
1506     vcp_sprite.spriteOwner = TRUE;
1507     floating_sprite.spriteOwner = TRUE;
1508 
1509     ptr.master = &vcp;
1510     kbd.master = &vck;
1511 
1512     assert(GetPairedDevice(&vcp) == &vck);
1513     assert(GetPairedDevice(&vck) == &vcp);
1514     assert(GetMaster(&ptr, MASTER_POINTER) == &vcp);
1515     assert(GetMaster(&ptr, MASTER_KEYBOARD) == &vck);
1516     assert(GetMaster(&kbd, MASTER_POINTER) == &vcp);
1517     assert(GetMaster(&kbd, MASTER_KEYBOARD) == &vck);
1518     assert(GetMaster(&ptr, MASTER_ATTACHED) == &vcp);
1519     assert(GetMaster(&kbd, MASTER_ATTACHED) == &vck);
1520 
1521     assert(GetPairedDevice(&floating) == &floating);
1522     assert(GetMaster(&floating, MASTER_POINTER) == NULL);
1523     assert(GetMaster(&floating, MASTER_KEYBOARD) == NULL);
1524     assert(GetMaster(&floating, MASTER_ATTACHED) == NULL);
1525 
1526     assert(GetMaster(&vcp, POINTER_OR_FLOAT) == &vcp);
1527     assert(GetMaster(&vck, POINTER_OR_FLOAT) == &vcp);
1528     assert(GetMaster(&ptr, POINTER_OR_FLOAT) == &vcp);
1529     assert(GetMaster(&kbd, POINTER_OR_FLOAT) == &vcp);
1530 
1531     assert(GetMaster(&vcp, KEYBOARD_OR_FLOAT) == &vck);
1532     assert(GetMaster(&vck, KEYBOARD_OR_FLOAT) == &vck);
1533     assert(GetMaster(&ptr, KEYBOARD_OR_FLOAT) == &vck);
1534     assert(GetMaster(&kbd, KEYBOARD_OR_FLOAT) == &vck);
1535 
1536     assert(GetMaster(&floating, KEYBOARD_OR_FLOAT) == &floating);
1537     assert(GetMaster(&floating, POINTER_OR_FLOAT) == &floating);
1538 }
1539 
1540 static void
input_option_test(void)1541 input_option_test(void)
1542 {
1543     InputOption *list = NULL;
1544     InputOption *opt;
1545     const char *val;
1546 
1547     printf("Testing input_option list interface\n");
1548 
1549     list = input_option_new(list, "key", "value");
1550     assert(list);
1551     opt = input_option_find(list, "key");
1552     val = input_option_get_value(opt);
1553     assert(strcmp(val, "value") == 0);
1554 
1555     list = input_option_new(list, "2", "v2");
1556     opt = input_option_find(list, "key");
1557     val = input_option_get_value(opt);
1558     assert(strcmp(val, "value") == 0);
1559 
1560     opt = input_option_find(list, "2");
1561     val = input_option_get_value(opt);
1562     assert(strcmp(val, "v2") == 0);
1563 
1564     list = input_option_new(list, "3", "v3");
1565 
1566     /* search, delete */
1567     opt = input_option_find(list, "key");
1568     val = input_option_get_value(opt);
1569     assert(strcmp(val, "value") == 0);
1570     list = input_option_free_element(list, "key");
1571     opt = input_option_find(list, "key");
1572     assert(opt == NULL);
1573 
1574     opt = input_option_find(list, "2");
1575     val = input_option_get_value(opt);
1576     assert(strcmp(val, "v2") == 0);
1577     list = input_option_free_element(list, "2");
1578     opt = input_option_find(list, "2");
1579     assert(opt == NULL);
1580 
1581     opt = input_option_find(list, "3");
1582     val = input_option_get_value(opt);
1583     assert(strcmp(val, "v3") == 0);
1584     list = input_option_free_element(list, "3");
1585     opt = input_option_find(list, "3");
1586     assert(opt == NULL);
1587 
1588     /* list deletion */
1589     list = input_option_new(list, "1", "v3");
1590     list = input_option_new(list, "2", "v3");
1591     list = input_option_new(list, "3", "v3");
1592     input_option_free_list(&list);
1593 
1594     assert(list == NULL);
1595 
1596     list = input_option_new(list, "1", "v1");
1597     list = input_option_new(list, "2", "v2");
1598     list = input_option_new(list, "3", "v3");
1599 
1600     /* value replacement */
1601     opt = input_option_find(list, "2");
1602     val = input_option_get_value(opt);
1603     assert(strcmp(val, "v2") == 0);
1604     input_option_set_value(opt, "foo");
1605     val = input_option_get_value(opt);
1606     assert(strcmp(val, "foo") == 0);
1607     opt = input_option_find(list, "2");
1608     val = input_option_get_value(opt);
1609     assert(strcmp(val, "foo") == 0);
1610 
1611     /* key replacement */
1612     input_option_set_key(opt, "bar");
1613     val = input_option_get_key(opt);
1614     assert(strcmp(val, "bar") == 0);
1615     opt = input_option_find(list, "bar");
1616     val = input_option_get_value(opt);
1617     assert(strcmp(val, "foo") == 0);
1618 
1619     /* value replacement in input_option_new */
1620     list = input_option_new(list, "bar", "foobar");
1621     opt = input_option_find(list, "bar");
1622     val = input_option_get_value(opt);
1623     assert(strcmp(val, "foobar") == 0);
1624 
1625     input_option_free_list(&list);
1626     assert(list == NULL);
1627 }
1628 
1629 static void
_test_double_fp16_values(double orig_d)1630 _test_double_fp16_values(double orig_d)
1631 {
1632     FP1616 first_fp16, final_fp16;
1633     double final_d;
1634 
1635     if (orig_d > 0x7FFF) {
1636         printf("Test out of range\n");
1637         assert(0);
1638     }
1639 
1640     first_fp16 = double_to_fp1616(orig_d);
1641     final_d = fp1616_to_double(first_fp16);
1642     final_fp16 = double_to_fp1616(final_d);
1643 
1644     /* {
1645      *    char first_fp16_s[64];
1646      *    char final_fp16_s[64];
1647      *    snprintf(first_fp16_s, sizeof(first_fp16_s), "%d + %u * 2^-16", (first_fp16 & 0xffff0000) >> 16, first_fp16 & 0xffff);
1648      *    snprintf(final_fp16_s, sizeof(final_fp16_s), "%d + %u * 2^-16", (final_fp16 & 0xffff0000) >> 16, final_fp16 & 0xffff);
1649      *
1650      *    printf("FP16: original double: %f first fp16: %s, re-encoded double: %f, final fp16: %s\n", orig_d, first_fp16_s, final_d, final_fp16_s);
1651      * }
1652      */
1653 
1654     /* since we lose precision, we only do rough range testing */
1655     assert(final_d > orig_d - 0.1);
1656     assert(final_d < orig_d + 0.1);
1657 
1658     assert(memcmp(&first_fp16, &final_fp16, sizeof(FP1616)) == 0);
1659 
1660     if (orig_d > 0)
1661         _test_double_fp16_values(-orig_d);
1662 }
1663 
1664 static void
_test_double_fp32_values(double orig_d)1665 _test_double_fp32_values(double orig_d)
1666 {
1667     FP3232 first_fp32, final_fp32;
1668     double final_d;
1669 
1670     if (orig_d > 0x7FFFFFFF) {
1671         printf("Test out of range\n");
1672         assert(0);
1673     }
1674 
1675     first_fp32 = double_to_fp3232(orig_d);
1676     final_d = fp3232_to_double(first_fp32);
1677     final_fp32 = double_to_fp3232(final_d);
1678 
1679     /* {
1680      *     char first_fp32_s[64];
1681      *     char final_fp32_s[64];
1682      *     snprintf(first_fp32_s, sizeof(first_fp32_s), "%d + %u * 2^-32", first_fp32.integral, first_fp32.frac);
1683      *     snprintf(final_fp32_s, sizeof(final_fp32_s), "%d + %u * 2^-32", first_fp32.integral, final_fp32.frac);
1684      *
1685      *     printf("FP32: original double: %f first fp32: %s, re-encoded double: %f, final fp32: %s\n", orig_d, first_fp32_s, final_d, final_fp32_s);
1686      * }
1687      */
1688 
1689     /* since we lose precision, we only do rough range testing */
1690     assert(final_d > orig_d - 0.1);
1691     assert(final_d < orig_d + 0.1);
1692 
1693     assert(memcmp(&first_fp32, &final_fp32, sizeof(FP3232)) == 0);
1694 
1695     if (orig_d > 0)
1696         _test_double_fp32_values(-orig_d);
1697 }
1698 
1699 static void
dix_double_fp_conversion(void)1700 dix_double_fp_conversion(void)
1701 {
1702     uint32_t i;
1703 
1704     printf("Testing double to FP1616/FP3232 conversions\n");
1705 
1706     _test_double_fp16_values(0);
1707     for (i = 1; i < 0x7FFF; i <<= 1) {
1708         double val;
1709 
1710         val = i;
1711         _test_double_fp16_values(val);
1712         _test_double_fp32_values(val);
1713 
1714         /* and some pseudo-random floating points */
1715         val = i - 0.00382;
1716         _test_double_fp16_values(val);
1717         _test_double_fp32_values(val);
1718 
1719         val = i + 0.00382;
1720         _test_double_fp16_values(val);
1721         _test_double_fp32_values(val);
1722 
1723         val = i + 0.05234;
1724         _test_double_fp16_values(val);
1725         _test_double_fp32_values(val);
1726 
1727         val = i + 0.12342;
1728         _test_double_fp16_values(val);
1729         _test_double_fp32_values(val);
1730 
1731         val = i + 0.27583;
1732         _test_double_fp16_values(val);
1733         _test_double_fp32_values(val);
1734 
1735         val = i + 0.50535;
1736         _test_double_fp16_values(val);
1737         _test_double_fp32_values(val);
1738 
1739         val = i + 0.72342;
1740         _test_double_fp16_values(val);
1741         _test_double_fp32_values(val);
1742 
1743         val = i + 0.80408;
1744         _test_double_fp16_values(val);
1745         _test_double_fp32_values(val);
1746     }
1747 
1748     for (i = 0x7FFFF; i < 0x7FFFFFFF; i <<= 1) {
1749         _test_double_fp32_values(i);
1750         /* and a few more random floating points, obtained
1751          * by faceplanting into the numpad repeatedly */
1752         _test_double_fp32_values(i + 0.010177);
1753         _test_double_fp32_values(i + 0.213841);
1754         _test_double_fp32_values(i + 0.348720);
1755         _test_double_fp32_values(i + 0.472020);
1756         _test_double_fp32_values(i + 0.572020);
1757         _test_double_fp32_values(i + 0.892929);
1758     }
1759 }
1760 
1761 /* The mieq test verifies that events added to the queue come out in the same
1762  * order that they went in.
1763  */
1764 static uint32_t mieq_test_event_last_processed;
1765 
1766 static void
mieq_test_event_handler(int screenNum,InternalEvent * ie,DeviceIntPtr dev)1767 mieq_test_event_handler(int screenNum, InternalEvent *ie, DeviceIntPtr dev)
1768 {
1769     RawDeviceEvent *e = (RawDeviceEvent *) ie;
1770 
1771     assert(e->type == ET_RawMotion);
1772     assert(e->flags > mieq_test_event_last_processed);
1773     mieq_test_event_last_processed = e->flags;
1774 }
1775 
1776 static void
_mieq_test_generate_events(uint32_t start,uint32_t count)1777 _mieq_test_generate_events(uint32_t start, uint32_t count)
1778 {
1779     static DeviceIntRec dev;
1780     static SpriteInfoRec spriteInfo;
1781     static SpriteRec sprite;
1782 
1783     memset(&dev, 0, sizeof(dev));
1784     memset(&spriteInfo, 0, sizeof(spriteInfo));
1785     memset(&sprite, 0, sizeof(sprite));
1786     dev.spriteInfo = &spriteInfo;
1787     spriteInfo.sprite = &sprite;
1788 
1789     dev.enabled = 1;
1790 
1791     count += start;
1792     while (start < count) {
1793         RawDeviceEvent e = { 0 };
1794         e.header = ET_Internal;
1795         e.type = ET_RawMotion;
1796         e.length = sizeof(e);
1797         e.time = GetTimeInMillis();
1798         e.flags = start;
1799 
1800         mieqEnqueue(&dev, (InternalEvent *) &e);
1801 
1802         start++;
1803     }
1804 }
1805 
1806 #define mieq_test_generate_events(c) { _mieq_test_generate_events(next, c); next += c; }
1807 
1808 static void
mieq_test(void)1809 mieq_test(void)
1810 {
1811     uint32_t next = 1;
1812 
1813     mieq_test_event_last_processed = 0;
1814     mieqInit();
1815     mieqSetHandler(ET_RawMotion, mieq_test_event_handler);
1816 
1817     /* Enough to fit the buffer but trigger a grow */
1818     mieq_test_generate_events(180);
1819 
1820     /* We should resize to 512 now */
1821     mieqProcessInputEvents();
1822 
1823     /* Some should now get dropped */
1824     mieq_test_generate_events(500);
1825 
1826     /* Tell us how many got dropped, 1024 now */
1827     mieqProcessInputEvents();
1828 
1829     /* Now make it 2048 */
1830     mieq_test_generate_events(900);
1831     mieqProcessInputEvents();
1832 
1833     /* Now make it 4096 (max) */
1834     mieq_test_generate_events(1950);
1835     mieqProcessInputEvents();
1836 
1837     /* Now overflow one last time with the maximal queue and reach the verbosity limit */
1838     mieq_test_generate_events(10000);
1839     mieqProcessInputEvents();
1840 
1841     mieqFini();
1842 }
1843 
1844 /* Simple check that we're replaying events in-order */
1845 static void
process_input_proc(InternalEvent * ev,DeviceIntPtr device)1846 process_input_proc(InternalEvent *ev, DeviceIntPtr device)
1847 {
1848     static int last_evtype = -1;
1849 
1850     if (ev->any.header == 0xac)
1851         last_evtype = -1;
1852 
1853     assert(ev->any.type == ++last_evtype);
1854 }
1855 
1856 static void
dix_enqueue_events(void)1857 dix_enqueue_events(void)
1858 {
1859 #define NEVENTS 5
1860     DeviceIntRec dev;
1861     InternalEvent ev[NEVENTS];
1862     SpriteInfoRec spriteInfo;
1863     SpriteRec sprite;
1864     QdEventPtr qe;
1865     int i;
1866 
1867     memset(&dev, 0, sizeof(dev));
1868     dev.public.processInputProc = process_input_proc;
1869 
1870     memset(&spriteInfo, 0, sizeof(spriteInfo));
1871     memset(&sprite, 0, sizeof(sprite));
1872     dev.spriteInfo = &spriteInfo;
1873     spriteInfo.sprite = &sprite;
1874 
1875     InitEvents();
1876     assert(xorg_list_is_empty(&syncEvents.pending));
1877 
1878     /* this way PlayReleasedEvents really runs through all events in the
1879      * queue */
1880     inputInfo.devices = &dev;
1881 
1882     /* to reset process_input_proc */
1883     ev[0].any.header = 0xac;
1884 
1885     for (i = 0; i < NEVENTS; i++) {
1886         ev[i].any.length = sizeof(*ev);
1887         ev[i].any.type = i;
1888         EnqueueEvent(&ev[i], &dev);
1889         assert(!xorg_list_is_empty(&syncEvents.pending));
1890         qe = xorg_list_last_entry(&syncEvents.pending, QdEventRec, next);
1891         assert(memcmp(qe->event, &ev[i], ev[i].any.length) == 0);
1892         qe = xorg_list_first_entry(&syncEvents.pending, QdEventRec, next);
1893         assert(memcmp(qe->event, &ev[0], ev[i].any.length) == 0);
1894     }
1895 
1896     /* calls process_input_proc */
1897     dev.deviceGrab.sync.frozen = 1;
1898     PlayReleasedEvents();
1899     assert(!xorg_list_is_empty(&syncEvents.pending));
1900 
1901     dev.deviceGrab.sync.frozen = 0;
1902     PlayReleasedEvents();
1903     assert(xorg_list_is_empty(&syncEvents.pending));
1904 
1905     inputInfo.devices = NULL;
1906 }
1907 
1908 int
input_test(void)1909 input_test(void)
1910 {
1911     dix_enqueue_events();
1912     dix_double_fp_conversion();
1913     dix_input_valuator_masks();
1914     dix_input_valuator_masks_unaccel();
1915     dix_input_attributes();
1916     dix_init_valuators();
1917     dix_event_to_core_conversion();
1918     dix_event_to_xi1_conversion();
1919     dix_check_grab_values();
1920     xi2_struct_sizes();
1921     dix_grab_matching();
1922     dix_valuator_mode();
1923     include_byte_padding_macros();
1924     include_bit_test_macros();
1925     xi_unregister_handlers();
1926     dix_valuator_alloc();
1927     dix_get_master();
1928     input_option_test();
1929     mieq_test();
1930 
1931     return 0;
1932 }
1933