Debian Codegraph @

test/xi2/protocol-eventconvert.c

raw
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 * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 */
22
23 /* Test relies on assert() */
24 #undef NDEBUG
25
26 #ifdef HAVE_DIX_CONFIG_H
27 #include <dix-config.h>
28 #endif
29
30 #include <stdint.h>
31
32 #include "dix/eventconvert.h"
33
34 #include "inputstr.h"
35 #include "eventstr.h"
36 #include "exevents.h"
37 #include "inpututils.h"
38 #include <X11/extensions/XI2proto.h>
39
40 #include "protocol-common.h"
41
42 static void
43 test_values_XIRawEvent(RawDeviceEvent *in, xXIRawEvent * out, BOOL swap)
44 {
45 int i;
46 unsigned char *ptr;
47 FP3232 *value, *raw_value;
48 int nvals = 0;
49 int bits_set;
50 int len;
51 uint32_t flagmask = 0;
52
53 if (swap) {
54 swaps(&out->sequenceNumber);
55 swapl(&out->length);
56 swaps(&out->evtype);
57 swaps(&out->deviceid);
58 swapl(&out->time);
59 swapl(&out->detail);
60 swaps(&out->valuators_len);
61 swapl(&out->flags);
62 }
63
64 assert(out->type == GenericEvent);
65 assert(out->extension == 0); /* IReqCode defaults to 0 */
66 assert(out->evtype == GetXI2Type(in->type));
67 assert(out->time == in->time);
68 assert(out->detail == in->detail.button);
69 assert(out->deviceid == in->deviceid);
70 assert(out->valuators_len >=
71 bytes_to_int32(bits_to_bytes(sizeof(in->valuators.mask))));
72
73 switch (in->type) {
74 case ET_RawMotion:
75 case ET_RawButtonPress:
76 case ET_RawButtonRelease:
77 flagmask = XIPointerEmulated;
78 break;
79 default:
80 flagmask = 0;
81 }
82 assert((out->flags & ~flagmask) == 0);
83
84 ptr = (unsigned char *) &out[1];
85 bits_set = 0;
86
87 for (i = 0; out->valuators_len && i < sizeof(in->valuators.mask) * 8; i++) {
88 if (i >= MAX_VALUATORS)
89 assert(!XIMaskIsSet(in->valuators.mask, i));
90 assert(XIMaskIsSet(in->valuators.mask, i) == XIMaskIsSet(ptr, i));
91 if (XIMaskIsSet(in->valuators.mask, i))
92 bits_set++;
93 }
94
95 /* length is len of valuator mask (in 4-byte units) + the number of bits
96 * set. Each bit set represents 2 8-byte values, hence the
97 * 'bits_set * 4' */
98 len = out->valuators_len + bits_set * 4;
99 assert(out->length == len);
100
101 nvals = 0;
102
103 for (i = 0; out->valuators_len && i < MAX_VALUATORS; i++) {
104 assert(XIMaskIsSet(in->valuators.mask, i) == XIMaskIsSet(ptr, i));
105 if (XIMaskIsSet(in->valuators.mask, i)) {
106 FP3232 vi, vo;
107
108 value =
109 (FP3232 *) (((unsigned char *) &out[1]) +
110 out->valuators_len * 4);
111 value += nvals;
112
113 vi = double_to_fp3232(in->valuators.data[i]);
114
115 vo.integral = value->integral;
116 vo.frac = value->frac;
117 if (swap) {
118 swapl(&vo.integral);
119 swapl(&vo.frac);
120 }
121
122 assert(vi.integral == vo.integral);
123 assert(vi.frac == vo.frac);
124
125 raw_value = value + bits_set;
126
127 vi = double_to_fp3232(in->valuators.data_raw[i]);
128
129 vo.integral = raw_value->integral;
130 vo.frac = raw_value->frac;
131 if (swap) {
132 swapl(&vo.integral);
133 swapl(&vo.frac);
134 }
135
136 assert(vi.integral == vo.integral);
137 assert(vi.frac == vo.frac);
138
139 nvals++;
140 }
141 }
142 }
143
144 static void
145 test_XIRawEvent(RawDeviceEvent *in)
146 {
147 xXIRawEvent *out, *swapped;
148 int rc;
149
150 rc = EventToXI2((InternalEvent *) in, (xEvent **) &out);
151 assert(rc == Success);
152
153 test_values_XIRawEvent(in, out, FALSE);
154
155 swapped = calloc(1, sizeof(xEvent) + out->length * 4);
156 XI2EventSwap((xGenericEvent *) out, (xGenericEvent *) swapped);
157 test_values_XIRawEvent(in, swapped, TRUE);
158
159 free(out);
160 free(swapped);
161 }
162
163 static void
164 test_convert_XIFocusEvent(void)
165 {
166 xEvent *out;
167 DeviceEvent in;
168 int rc;
169
170 in.header = ET_Internal;
171 in.type = ET_Enter;
172 rc = EventToXI2((InternalEvent *) &in, &out);
173 assert(rc == Success);
174 assert(out == NULL);
175
176 in.header = ET_Internal;
177 in.type = ET_FocusIn;
178 rc = EventToXI2((InternalEvent *) &in, &out);
179 assert(rc == Success);
180 assert(out == NULL);
181
182 in.header = ET_Internal;
183 in.type = ET_FocusOut;
184 rc = EventToXI2((InternalEvent *) &in, &out);
185 assert(rc == BadImplementation);
186
187 in.header = ET_Internal;
188 in.type = ET_Leave;
189 rc = EventToXI2((InternalEvent *) &in, &out);
190 assert(rc == BadImplementation);
191 }
192
193 static void
194 test_convert_XIRawEvent(void)
195 {
196 RawDeviceEvent in;
197 int i;
198
199 memset(&in, 0, sizeof(in));
200
201 in.header = ET_Internal;
202 in.type = ET_RawMotion;
203 test_XIRawEvent(&in);
204
205 in.header = ET_Internal;
206 in.type = ET_RawKeyPress;
207 test_XIRawEvent(&in);
208
209 in.header = ET_Internal;
210 in.type = ET_RawKeyRelease;
211 test_XIRawEvent(&in);
212
213 in.header = ET_Internal;
214 in.type = ET_RawButtonPress;
215 test_XIRawEvent(&in);
216
217 in.header = ET_Internal;
218 in.type = ET_RawButtonRelease;
219 test_XIRawEvent(&in);
220
221 in.detail.button = 1L;
222 test_XIRawEvent(&in);
223 in.detail.button = 1L << 8;
224 test_XIRawEvent(&in);
225 in.detail.button = 1L << 16;
226 test_XIRawEvent(&in);
227 in.detail.button = 1L << 24;
228 test_XIRawEvent(&in);
229 in.detail.button = ~0L;
230 test_XIRawEvent(&in);
231
232 in.detail.button = 0;
233
234 in.time = 1L;
235 test_XIRawEvent(&in);
236 in.time = 1L << 8;
237 test_XIRawEvent(&in);
238 in.time = 1L << 16;
239 test_XIRawEvent(&in);
240 in.time = 1L << 24;
241 test_XIRawEvent(&in);
242 in.time = ~0L;
243 test_XIRawEvent(&in);
244
245 in.deviceid = 1;
246 test_XIRawEvent(&in);
247 in.deviceid = 1 << 8;
248 test_XIRawEvent(&in);
249 in.deviceid = ~0 & 0xFF;
250 test_XIRawEvent(&in);
251
252 for (i = 0; i < MAX_VALUATORS; i++) {
253 XISetMask(in.valuators.mask, i);
254 test_XIRawEvent(&in);
255 XIClearMask(in.valuators.mask, i);
256 }
257
258 for (i = 0; i < MAX_VALUATORS; i++) {
259 XISetMask(in.valuators.mask, i);
260
261 in.valuators.data[i] = i + (i * 0.0010);
262 in.valuators.data_raw[i] = (i + 10) + (i * 0.0030);
263 test_XIRawEvent(&in);
264 XIClearMask(in.valuators.mask, i);
265 }
266
267 for (i = 0; i < MAX_VALUATORS; i++) {
268 XISetMask(in.valuators.mask, i);
269 test_XIRawEvent(&in);
270 }
271 }
272
273 static void
274 test_values_XIDeviceEvent(DeviceEvent *in, xXIDeviceEvent * out, BOOL swap)
275 {
276 int valuators;
277 int i;
278 unsigned char *ptr;
279 uint32_t flagmask = 0;
280 FP3232 *values;
281
282 if (swap) {
283 swaps(&out->sequenceNumber);
284 swapl(&out->length);
285 swaps(&out->evtype);
286 swaps(&out->deviceid);
287 swaps(&out->sourceid);
288 swapl(&out->time);
289 swapl(&out->detail);
290 swapl(&out->root);
291 swapl(&out->event);
292 swapl(&out->child);
293 swapl(&out->root_x);
294 swapl(&out->root_y);
295 swapl(&out->event_x);
296 swapl(&out->event_y);
297 swaps(&out->buttons_len);
298 swaps(&out->valuators_len);
299 swapl(&out->mods.base_mods);
300 swapl(&out->mods.latched_mods);
301 swapl(&out->mods.locked_mods);
302 swapl(&out->mods.effective_mods);
303 swapl(&out->flags);
304 }
305
306 assert(out->extension == 0); /* IReqCode defaults to 0 */
307 assert(out->evtype == GetXI2Type(in->type));
308 assert(out->time == in->time);
309 assert(out->detail == in->detail.button);
310 assert(out->length >= 12);
311
312 assert(out->deviceid == in->deviceid);
313 assert(out->sourceid == in->sourceid);
314
315 switch (in->type) {
316 case ET_ButtonPress:
317 case ET_Motion:
318 case ET_ButtonRelease:
319 flagmask = XIPointerEmulated;
320 break;
321 case ET_KeyPress:
322 flagmask = XIKeyRepeat;
323 break;
324 default:
325 flagmask = 0;
326 break;
327 }
328 assert((out->flags & ~flagmask) == 0);
329
330 assert(out->root == in->root);
331 assert(out->event == None); /* set in FixUpEventFromWindow */
332 assert(out->child == None); /* set in FixUpEventFromWindow */
333
334 assert(out->mods.base_mods == in->mods.base);
335 assert(out->mods.latched_mods == in->mods.latched);
336 assert(out->mods.locked_mods == in->mods.locked);
337 assert(out->mods.effective_mods == in->mods.effective);
338
339 assert(out->group.base_group == in->group.base);
340 assert(out->group.latched_group == in->group.latched);
341 assert(out->group.locked_group == in->group.locked);
342 assert(out->group.effective_group == in->group.effective);
343
344 assert(out->event_x == 0); /* set in FixUpEventFromWindow */
345 assert(out->event_y == 0); /* set in FixUpEventFromWindow */
346
347 assert(out->root_x == double_to_fp1616(in->root_x + in->root_x_frac));
348 assert(out->root_y == double_to_fp1616(in->root_y + in->root_y_frac));
349
350 for (i = 0; i < bits_to_bytes(sizeof(in->buttons)); i++) {
351 if (XIMaskIsSet(in->buttons, i)) {
352 assert(out->buttons_len >= bytes_to_int32(bits_to_bytes(i)));
353 }
354 }
355
356 ptr = (unsigned char *) &out[1];
357 for (i = 0; i < sizeof(in->buttons) * 8; i++)
358 assert(XIMaskIsSet(in->buttons, i) == XIMaskIsSet(ptr, i));
359
360 valuators = 0;
361 for (i = 0; i < MAX_VALUATORS; i++)
362 if (XIMaskIsSet(in->valuators.mask, i))
363 valuators++;
364
365 assert(out->valuators_len >= bytes_to_int32(bits_to_bytes(valuators)));
366
367 ptr += out->buttons_len * 4;
368 values = (FP3232 *) (ptr + out->valuators_len * 4);
369 for (i = 0; i < sizeof(in->valuators.mask) * 8 ||
370 i < (out->valuators_len * 4) * 8; i++) {
371 if (i >= MAX_VALUATORS) {
372 assert(!XIMaskIsSet(in->valuators.mask, i));
373 assert(!XIMaskIsSet(ptr, i));
374 }
375 else if (i > sizeof(in->valuators.mask) * 8)
376 assert(!XIMaskIsSet(ptr, i));
377 else if (i > out->valuators_len * 4 * 8)
378 assert(!XIMaskIsSet(in->valuators.mask, i));
379 else {
380 assert(XIMaskIsSet(in->valuators.mask, i) == XIMaskIsSet(ptr, i));
381
382 if (XIMaskIsSet(ptr, i)) {
383 FP3232 vi, vo;
384
385 vi = double_to_fp3232(in->valuators.data[i]);
386 vo = *values;
387
388 if (swap) {
389 swapl(&vo.integral);
390 swapl(&vo.frac);
391 }
392
393 assert(vi.integral == vo.integral);
394 assert(vi.frac == vo.frac);
395 values++;
396 }
397 }
398 }
399 }
400
401 static void
402 test_XIDeviceEvent(DeviceEvent *in)
403 {
404 xXIDeviceEvent *out, *swapped;
405 int rc;
406
407 rc = EventToXI2((InternalEvent *) in, (xEvent **) &out);
408 assert(rc == Success);
409
410 test_values_XIDeviceEvent(in, out, FALSE);
411
412 swapped = calloc(1, sizeof(xEvent) + out->length * 4);
413 XI2EventSwap((xGenericEvent *) out, (xGenericEvent *) swapped);
414 test_values_XIDeviceEvent(in, swapped, TRUE);
415
416 free(out);
417 free(swapped);
418 }
419
420 static void
421 test_convert_XIDeviceEvent(void)
422 {
423 DeviceEvent in;
424 int i;
425
426 memset(&in, 0, sizeof(in));
427
428 in.header = ET_Internal;
429 in.type = ET_Motion;
430 in.length = sizeof(DeviceEvent);
431 in.time = 0;
432 in.deviceid = 1;
433 in.sourceid = 2;
434 in.root = 3;
435 in.root_x = 4;
436 in.root_x_frac = 5;
437 in.root_y = 6;
438 in.root_y_frac = 7;
439 in.detail.button = 8;
440 in.mods.base = 9;
441 in.mods.latched = 10;
442 in.mods.locked = 11;
443 in.mods.effective = 11;
444 in.group.base = 12;
445 in.group.latched = 13;
446 in.group.locked = 14;
447 in.group.effective = 15;
448
449 test_XIDeviceEvent(&in);
450
451 /* 32 bit */
452 in.detail.button = 1L;
453 test_XIDeviceEvent(&in);
454 in.detail.button = 1L << 8;
455 test_XIDeviceEvent(&in);
456 in.detail.button = 1L << 16;
457 test_XIDeviceEvent(&in);
458 in.detail.button = 1L << 24;
459 test_XIDeviceEvent(&in);
460 in.detail.button = ~0L;
461 test_XIDeviceEvent(&in);
462
463 /* 32 bit */
464 in.time = 1L;
465 test_XIDeviceEvent(&in);
466 in.time = 1L << 8;
467 test_XIDeviceEvent(&in);
468 in.time = 1L << 16;
469 test_XIDeviceEvent(&in);
470 in.time = 1L << 24;
471 test_XIDeviceEvent(&in);
472 in.time = ~0L;
473 test_XIDeviceEvent(&in);
474
475 /* 16 bit */
476 in.deviceid = 1;
477 test_XIDeviceEvent(&in);
478 in.deviceid = 1 << 8;
479 test_XIDeviceEvent(&in);
480 in.deviceid = ~0 & 0xFF;
481 test_XIDeviceEvent(&in);
482
483 /* 16 bit */
484 in.sourceid = 1;
485 test_XIDeviceEvent(&in);
486 in.deviceid = 1 << 8;
487 test_XIDeviceEvent(&in);
488 in.deviceid = ~0 & 0xFF;
489 test_XIDeviceEvent(&in);
490
491 /* 32 bit */
492 in.root = 1L;
493 test_XIDeviceEvent(&in);
494 in.root = 1L << 8;
495 test_XIDeviceEvent(&in);
496 in.root = 1L << 16;
497 test_XIDeviceEvent(&in);
498 in.root = 1L << 24;
499 test_XIDeviceEvent(&in);
500 in.root = ~0L;
501 test_XIDeviceEvent(&in);
502
503 /* 16 bit */
504 in.root_x = 1;
505 test_XIDeviceEvent(&in);
506 in.root_x = 1 << 8;
507 test_XIDeviceEvent(&in);
508 in.root_x = ~0 & 0xFF;
509 test_XIDeviceEvent(&in);
510
511 in.root_x_frac = 1;
512 test_XIDeviceEvent(&in);
513 in.root_x_frac = 1 << 8;
514 test_XIDeviceEvent(&in);
515 in.root_x_frac = ~0 & 0xFF;
516 test_XIDeviceEvent(&in);
517
518 in.root_y = 1;
519 test_XIDeviceEvent(&in);
520 in.root_y = 1 << 8;
521 test_XIDeviceEvent(&in);
522 in.root_y = ~0 & 0xFF;
523 test_XIDeviceEvent(&in);
524
525 in.root_y_frac = 1;
526 test_XIDeviceEvent(&in);
527 in.root_y_frac = 1 << 8;
528 test_XIDeviceEvent(&in);
529 in.root_y_frac = ~0 & 0xFF;
530 test_XIDeviceEvent(&in);
531
532 /* 32 bit */
533 in.mods.base = 1L;
534 test_XIDeviceEvent(&in);
535 in.mods.base = 1L << 8;
536 test_XIDeviceEvent(&in);
537 in.mods.base = 1L << 16;
538 test_XIDeviceEvent(&in);
539 in.mods.base = 1L << 24;
540 test_XIDeviceEvent(&in);
541 in.mods.base = ~0L;
542 test_XIDeviceEvent(&in);
543
544 in.mods.latched = 1L;
545 test_XIDeviceEvent(&in);
546 in.mods.latched = 1L << 8;
547 test_XIDeviceEvent(&in);
548 in.mods.latched = 1L << 16;
549 test_XIDeviceEvent(&in);
550 in.mods.latched = 1L << 24;
551 test_XIDeviceEvent(&in);
552 in.mods.latched = ~0L;
553 test_XIDeviceEvent(&in);
554
555 in.mods.locked = 1L;
556 test_XIDeviceEvent(&in);
557 in.mods.locked = 1L << 8;
558 test_XIDeviceEvent(&in);
559 in.mods.locked = 1L << 16;
560 test_XIDeviceEvent(&in);
561 in.mods.locked = 1L << 24;
562 test_XIDeviceEvent(&in);
563 in.mods.locked = ~0L;
564 test_XIDeviceEvent(&in);
565
566 in.mods.effective = 1L;
567 test_XIDeviceEvent(&in);
568 in.mods.effective = 1L << 8;
569 test_XIDeviceEvent(&in);
570 in.mods.effective = 1L << 16;
571 test_XIDeviceEvent(&in);
572 in.mods.effective = 1L << 24;
573 test_XIDeviceEvent(&in);
574 in.mods.effective = ~0L;
575 test_XIDeviceEvent(&in);
576
577 /* 8 bit */
578 in.group.base = 1;
579 test_XIDeviceEvent(&in);
580 in.group.base = ~0 & 0xFF;
581 test_XIDeviceEvent(&in);
582
583 in.group.latched = 1;
584 test_XIDeviceEvent(&in);
585 in.group.latched = ~0 & 0xFF;
586 test_XIDeviceEvent(&in);
587
588 in.group.locked = 1;
589 test_XIDeviceEvent(&in);
590 in.group.locked = ~0 & 0xFF;
591 test_XIDeviceEvent(&in);
592
593 in.mods.effective = 1;
594 test_XIDeviceEvent(&in);
595 in.mods.effective = ~0 & 0xFF;
596 test_XIDeviceEvent(&in);
597
598 for (i = 0; i < sizeof(in.buttons) * 8; i++) {
599 XISetMask(in.buttons, i);
600 test_XIDeviceEvent(&in);
601 XIClearMask(in.buttons, i);
602 }
603
604 for (i = 0; i < sizeof(in.buttons) * 8; i++) {
605 XISetMask(in.buttons, i);
606 test_XIDeviceEvent(&in);
607 }
608
609 for (i = 0; i < MAX_VALUATORS; i++) {
610 XISetMask(in.valuators.mask, i);
611 test_XIDeviceEvent(&in);
612 XIClearMask(in.valuators.mask, i);
613 }
614
615 for (i = 0; i < MAX_VALUATORS; i++) {
616 XISetMask(in.valuators.mask, i);
617
618 in.valuators.data[i] = i + (i * 0.0020);
619 test_XIDeviceEvent(&in);
620 XIClearMask(in.valuators.mask, i);
621 }
622
623 for (i = 0; i < MAX_VALUATORS; i++) {
624 XISetMask(in.valuators.mask, i);
625 test_XIDeviceEvent(&in);
626 }
627 }
628
629 static void
630 test_values_XIDeviceChangedEvent(DeviceChangedEvent *in,
631 xXIDeviceChangedEvent * out, BOOL swap)
632 {
633 int i, j;
634 unsigned char *ptr;
635
636 if (swap) {
637 swaps(&out->sequenceNumber);
638 swapl(&out->length);
639 swaps(&out->evtype);
640 swaps(&out->deviceid);
641 swaps(&out->sourceid);
642 swapl(&out->time);
643 swaps(&out->num_classes);
644 }
645
646 assert(out->type == GenericEvent);
647 assert(out->extension == 0); /* IReqCode defaults to 0 */
648 assert(out->evtype == GetXI2Type(in->type));
649 assert(out->time == in->time);
650 assert(out->deviceid == in->deviceid);
651 assert(out->sourceid == in->sourceid);
652
653 ptr = (unsigned char *) &out[1];
654 for (i = 0; i < out->num_classes; i++) {
655 xXIAnyInfo *any = (xXIAnyInfo *) ptr;
656
657 if (swap) {
658 swaps(&any->length);
659 swaps(&any->type);
660 swaps(&any->sourceid);
661 }
662
663 switch (any->type) {
664 case XIButtonClass:
665 {
666 xXIButtonInfo *b = (xXIButtonInfo *) any;
667 Atom *names;
668
669 if (swap) {
670 swaps(&b->num_buttons);
671 }
672
673 assert(b->length ==
674 bytes_to_int32(sizeof(xXIButtonInfo)) +
675 bytes_to_int32(bits_to_bytes(b->num_buttons)) +
676 b->num_buttons);
677 assert(b->num_buttons == in->buttons.num_buttons);
678
679 names = (Atom *) ((char *) &b[1] +
680 pad_to_int32(bits_to_bytes(b->num_buttons)));
681 for (j = 0; j < b->num_buttons; j++) {
682 if (swap) {
683 swapl(&names[j]);
684 }
685 assert(names[j] == in->buttons.names[j]);
686 }
687 }
688 break;
689 case XIKeyClass:
690 {
691 xXIKeyInfo *k = (xXIKeyInfo *) any;
692 uint32_t *kc;
693
694 if (swap) {
695 swaps(&k->num_keycodes);
696 }
697
698 assert(k->length ==
699 bytes_to_int32(sizeof(xXIKeyInfo)) + k->num_keycodes);
700 assert(k->num_keycodes == in->keys.max_keycode -
701 in->keys.min_keycode + 1);
702
703 kc = (uint32_t *) &k[1];
704 for (j = 0; j < k->num_keycodes; j++) {
705 if (swap) {
706 swapl(&kc[j]);
707 }
708 assert(kc[j] >= in->keys.min_keycode);
709 assert(kc[j] <= in->keys.max_keycode);
710 }
711 }
712 break;
713 case XIValuatorClass:
714 {
715 xXIValuatorInfo *v = (xXIValuatorInfo *) any;
716
717 assert(v->length == bytes_to_int32(sizeof(xXIValuatorInfo)));
718
719 }
720 break;
721 case XIScrollClass:
722 {
723 xXIScrollInfo *s = (xXIScrollInfo *) any;
724
725 assert(s->length == bytes_to_int32(sizeof(xXIScrollInfo)));
726
727 assert(s->sourceid == in->sourceid);
728 assert(s->number < in->num_valuators);
729 switch (s->type) {
730 case XIScrollTypeVertical:
731 assert(in->valuators[s->number].scroll.type ==
732 SCROLL_TYPE_VERTICAL);
733 break;
734 case XIScrollTypeHorizontal:
735 assert(in->valuators[s->number].scroll.type ==
736 SCROLL_TYPE_HORIZONTAL);
737 break;
738 }
739 if (s->flags & XIScrollFlagPreferred)
740 assert(in->valuators[s->number].scroll.
741 flags & SCROLL_FLAG_PREFERRED);
742 }
743 default:
744 dbg("Invalid class type.\n\n");
745 assert(1);
746 break;
747 }
748
749 ptr += any->length * 4;
750 }
751
752 }
753
754 static void
755 test_XIDeviceChangedEvent(DeviceChangedEvent *in)
756 {
757 xXIDeviceChangedEvent *out, *swapped;
758 int rc;
759
760 rc = EventToXI2((InternalEvent *) in, (xEvent **) &out);
761 assert(rc == Success);
762
763 test_values_XIDeviceChangedEvent(in, out, FALSE);
764
765 swapped = calloc(1, sizeof(xEvent) + out->length * 4);
766 XI2EventSwap((xGenericEvent *) out, (xGenericEvent *) swapped);
767 test_values_XIDeviceChangedEvent(in, swapped, TRUE);
768
769 free(out);
770 free(swapped);
771 }
772
773 static void
774 test_convert_XIDeviceChangedEvent(void)
775 {
776 DeviceChangedEvent in;
777 int i;
778
779 memset(&in, 0, sizeof(in));
780 in.header = ET_Internal;
781 in.type = ET_DeviceChanged;
782 in.length = sizeof(DeviceChangedEvent);
783 in.time = 0;
784 in.deviceid = 1;
785 in.sourceid = 2;
786 in.masterid = 3;
787 in.num_valuators = 4;
788 in.flags =
789 DEVCHANGE_SLAVE_SWITCH | DEVCHANGE_POINTER_EVENT |
790 DEVCHANGE_KEYBOARD_EVENT;
791
792 for (i = 0; i < MAX_BUTTONS; i++)
793 in.buttons.names[i] = i + 10;
794
795 in.keys.min_keycode = 8;
796 in.keys.max_keycode = 255;
797
798 test_XIDeviceChangedEvent(&in);
799
800 in.time = 1L;
801 test_XIDeviceChangedEvent(&in);
802 in.time = 1L << 8;
803 test_XIDeviceChangedEvent(&in);
804 in.time = 1L << 16;
805 test_XIDeviceChangedEvent(&in);
806 in.time = 1L << 24;
807 test_XIDeviceChangedEvent(&in);
808 in.time = ~0L;
809 test_XIDeviceChangedEvent(&in);
810
811 in.deviceid = 1L;
812 test_XIDeviceChangedEvent(&in);
813 in.deviceid = 1L << 8;
814 test_XIDeviceChangedEvent(&in);
815 in.deviceid = ~0 & 0xFFFF;
816 test_XIDeviceChangedEvent(&in);
817
818 in.sourceid = 1L;
819 test_XIDeviceChangedEvent(&in);
820 in.sourceid = 1L << 8;
821 test_XIDeviceChangedEvent(&in);
822 in.sourceid = ~0 & 0xFFFF;
823 test_XIDeviceChangedEvent(&in);
824
825 in.masterid = 1L;
826 test_XIDeviceChangedEvent(&in);
827 in.masterid = 1L << 8;
828 test_XIDeviceChangedEvent(&in);
829 in.masterid = ~0 & 0xFFFF;
830 test_XIDeviceChangedEvent(&in);
831
832 in.buttons.num_buttons = 0;
833 test_XIDeviceChangedEvent(&in);
834
835 in.buttons.num_buttons = 1;
836 test_XIDeviceChangedEvent(&in);
837
838 in.buttons.num_buttons = MAX_BUTTONS;
839 test_XIDeviceChangedEvent(&in);
840
841 in.keys.min_keycode = 0;
842 in.keys.max_keycode = 0;
843 test_XIDeviceChangedEvent(&in);
844
845 in.keys.max_keycode = 1 << 8;
846 test_XIDeviceChangedEvent(&in);
847
848 in.keys.max_keycode = 0xFFFC; /* highest range, above that the length
849 field gives up */
850 test_XIDeviceChangedEvent(&in);
851
852 in.keys.min_keycode = 1 << 8;
853 in.keys.max_keycode = 1 << 8;
854 test_XIDeviceChangedEvent(&in);
855
856 in.keys.min_keycode = 1 << 8;
857 in.keys.max_keycode = 0;
858 test_XIDeviceChangedEvent(&in);
859
860 in.num_valuators = 0;
861 test_XIDeviceChangedEvent(&in);
862
863 in.num_valuators = 1;
864 test_XIDeviceChangedEvent(&in);
865
866 in.num_valuators = MAX_VALUATORS;
867 test_XIDeviceChangedEvent(&in);
868
869 for (i = 0; i < MAX_VALUATORS; i++) {
870 in.valuators[i].min = 0;
871 in.valuators[i].max = 0;
872 test_XIDeviceChangedEvent(&in);
873
874 in.valuators[i].max = 1 << 8;
875 test_XIDeviceChangedEvent(&in);
876 in.valuators[i].max = 1 << 16;
877 test_XIDeviceChangedEvent(&in);
878 in.valuators[i].max = 1 << 24;
879 test_XIDeviceChangedEvent(&in);
880 in.valuators[i].max = abs(~0);
881 test_XIDeviceChangedEvent(&in);
882
883 in.valuators[i].resolution = 1 << 8;
884 test_XIDeviceChangedEvent(&in);
885 in.valuators[i].resolution = 1 << 16;
886 test_XIDeviceChangedEvent(&in);
887 in.valuators[i].resolution = 1 << 24;
888 test_XIDeviceChangedEvent(&in);
889 in.valuators[i].resolution = abs(~0);
890 test_XIDeviceChangedEvent(&in);
891
892 in.valuators[i].name = i;
893 test_XIDeviceChangedEvent(&in);
894
895 in.valuators[i].mode = Relative;
896 test_XIDeviceChangedEvent(&in);
897
898 in.valuators[i].mode = Absolute;
899 test_XIDeviceChangedEvent(&in);
900 }
901 }
902
903 static void
904 test_values_XITouchOwnershipEvent(TouchOwnershipEvent *in,
905 xXITouchOwnershipEvent * out, BOOL swap)
906 {
907 if (swap) {
908 swaps(&out->sequenceNumber);
909 swapl(&out->length);
910 swaps(&out->evtype);
911 swaps(&out->deviceid);
912 swaps(&out->sourceid);
913 swapl(&out->time);
914 swapl(&out->touchid);
915 swapl(&out->root);
916 swapl(&out->event);
917 swapl(&out->child);
918 swapl(&out->time);
919 }
920
921 assert(out->type == GenericEvent);
922 assert(out->extension == 0); /* IReqCode defaults to 0 */
923 assert(out->evtype == GetXI2Type(in->type));
924 assert(out->time == in->time);
925 assert(out->deviceid == in->deviceid);
926 assert(out->sourceid == in->sourceid);
927 assert(out->touchid == in->touchid);
928 assert(out->flags == in->reason);
929 }
930
931 static void
932 test_XITouchOwnershipEvent(TouchOwnershipEvent *in)
933 {
934 xXITouchOwnershipEvent *out, *swapped;
935 int rc;
936
937 rc = EventToXI2((InternalEvent *) in, (xEvent **) &out);
938 assert(rc == Success);
939
940 test_values_XITouchOwnershipEvent(in, out, FALSE);
941
942 swapped = calloc(1, sizeof(xEvent) + out->length * 4);
943 XI2EventSwap((xGenericEvent *) out, (xGenericEvent *) swapped);
944 test_values_XITouchOwnershipEvent(in, swapped, TRUE);
945 free(out);
946 free(swapped);
947 }
948
949 static void
950 test_convert_XITouchOwnershipEvent(void)
951 {
952 TouchOwnershipEvent in;
953 long i;
954
955 memset(&in, 0, sizeof(in));
956 in.header = ET_Internal;
957 in.type = ET_TouchOwnership;
958 in.length = sizeof(in);
959 in.time = 0;
960 in.deviceid = 1;
961 in.sourceid = 2;
962 in.touchid = 0;
963 in.reason = 0;
964 in.resource = 0;
965 in.flags = 0;
966
967 test_XITouchOwnershipEvent(&in);
968
969 in.flags = XIAcceptTouch;
970 test_XITouchOwnershipEvent(&in);
971
972 in.flags = XIRejectTouch;
973 test_XITouchOwnershipEvent(&in);
974
975 for (i = 1; i <= 0xFFFF; i <<= 1) {
976 in.deviceid = i;
977 test_XITouchOwnershipEvent(&in);
978 }
979
980 for (i = 1; i <= 0xFFFF; i <<= 1) {
981 in.sourceid = i;
982 test_XITouchOwnershipEvent(&in);
983 }
984
985 for (i = 1;; i <<= 1) {
986 in.touchid = i;
987 test_XITouchOwnershipEvent(&in);
988 if (i == ((long) 1 << 31))
989 break;
990 }
991 }
992
993 static void
994 test_XIBarrierEvent(BarrierEvent *in)
995 {
996 xXIBarrierEvent *out, *swapped;
997 int count;
998 int rc;
999 int eventlen;
1000 FP3232 value;
1001
1002 rc = EventToXI((InternalEvent*)in, (xEvent**)&out, &count);
1003 assert(rc == BadMatch);
1004
1005 rc = EventToCore((InternalEvent*)in, (xEvent**)&out, &count);
1006 assert(rc == BadMatch);
1007
1008 rc = EventToXI2((InternalEvent*)in, (xEvent**)&out);
1009
1010 assert(out->type == GenericEvent);
1011 assert(out->extension == 0); /* IReqCode defaults to 0 */
1012 assert(out->evtype == GetXI2Type(in->type));
1013 assert(out->time == in->time);
1014 assert(out->deviceid == in->deviceid);
1015 assert(out->sourceid == in->sourceid);
1016 assert(out->barrier == in->barrierid);
1017 assert(out->flags == in->flags);
1018 assert(out->event == in->window);
1019 assert(out->root == in->root);
1020 assert(out->dtime == in->dt);
1021 assert(out->eventid == in->event_id);
1022 assert(out->root_x == double_to_fp1616(in->root_x));
1023 assert(out->root_y == double_to_fp1616(in->root_y));
1024
1025 value = double_to_fp3232(in->dx);
1026 assert(out->dx.integral == value.integral);
1027 assert(out->dx.frac == value.frac);
1028 value = double_to_fp3232(in->dy);
1029 assert(out->dy.integral == value.integral);
1030 assert(out->dy.frac == value.frac);
1031
1032 eventlen = sizeof(xEvent) + out->length * 4;
1033 swapped = calloc(1, eventlen);
1034 XI2EventSwap((xGenericEvent *) out, (xGenericEvent *) swapped);
1035
1036 swaps(&swapped->sequenceNumber);
1037 swapl(&swapped->length);
1038 swaps(&swapped->evtype);
1039 swaps(&swapped->deviceid);
1040 swapl(&swapped->time);
1041 swapl(&swapped->eventid);
1042 swapl(&swapped->root);
1043 swapl(&swapped->event);
1044 swapl(&swapped->barrier);
1045 swapl(&swapped->dtime);
1046 swaps(&swapped->sourceid);
1047 swapl(&swapped->root_x);
1048 swapl(&swapped->root_y);
1049 swapl(&swapped->dx.integral);
1050 swapl(&swapped->dx.frac);
1051 swapl(&swapped->dy.integral);
1052 swapl(&swapped->dy.frac);
1053
1054 assert(memcmp(swapped, out, eventlen) == 0);
1055
1056 free(swapped);
1057 free(out);
1058 }
1059
1060 static void
1061 test_convert_XIBarrierEvent(void)
1062 {
1063 BarrierEvent in;
1064
1065 memset(&in, 0, sizeof(in));
1066 in.header = ET_Internal;
1067 in.type = ET_BarrierHit;
1068 in.length = sizeof(in);
1069 in.time = 0;
1070 in.deviceid = 1;
1071 in.sourceid = 2;
1072
1073 test_XIBarrierEvent(&in);
1074
1075 in.deviceid = 1;
1076 while(in.deviceid & 0xFFFF) {
1077 test_XIBarrierEvent(&in);
1078 in.deviceid <<= 1;
1079 }
1080 in.deviceid = 0;
1081
1082 in.sourceid = 1;
1083 while(in.sourceid & 0xFFFF) {
1084 test_XIBarrierEvent(&in);
1085 in.sourceid <<= 1;
1086 }
1087 in.sourceid = 0;
1088
1089 in.flags = 1;
1090 while(in.flags) {
1091 test_XIBarrierEvent(&in);
1092 in.flags <<= 1;
1093 }
1094
1095 in.barrierid = 1;
1096 while(in.barrierid) {
1097 test_XIBarrierEvent(&in);
1098 in.barrierid <<= 1;
1099 }
1100
1101 in.dt = 1;
1102 while(in.dt) {
1103 test_XIBarrierEvent(&in);
1104 in.dt <<= 1;
1105 }
1106
1107 in.event_id = 1;
1108 while(in.event_id) {
1109 test_XIBarrierEvent(&in);
1110 in.event_id <<= 1;
1111 }
1112
1113 in.window = 1;
1114 while(in.window) {
1115 test_XIBarrierEvent(&in);
1116 in.window <<= 1;
1117 }
1118
1119 in.root = 1;
1120 while(in.root) {
1121 test_XIBarrierEvent(&in);
1122 in.root <<= 1;
1123 }
1124
1125 /* pseudo-random 16 bit numbers */
1126 in.root_x = 1;
1127 test_XIBarrierEvent(&in);
1128 in.root_x = 1.3;
1129 test_XIBarrierEvent(&in);
1130 in.root_x = 264.908;
1131 test_XIBarrierEvent(&in);
1132 in.root_x = 35638.292;
1133 test_XIBarrierEvent(&in);
1134
1135 in.root_x = -1;
1136 test_XIBarrierEvent(&in);
1137 in.root_x = -1.3;
1138 test_XIBarrierEvent(&in);
1139 in.root_x = -264.908;
1140 test_XIBarrierEvent(&in);
1141 in.root_x = -35638.292;
1142 test_XIBarrierEvent(&in);
1143
1144 in.root_y = 1;
1145 test_XIBarrierEvent(&in);
1146 in.root_y = 1.3;
1147 test_XIBarrierEvent(&in);
1148 in.root_y = 264.908;
1149 test_XIBarrierEvent(&in);
1150 in.root_y = 35638.292;
1151 test_XIBarrierEvent(&in);
1152
1153 in.root_y = -1;
1154 test_XIBarrierEvent(&in);
1155 in.root_y = -1.3;
1156 test_XIBarrierEvent(&in);
1157 in.root_y = -264.908;
1158 test_XIBarrierEvent(&in);
1159 in.root_y = -35638.292;
1160 test_XIBarrierEvent(&in);
1161
1162 /* equally pseudo-random 32 bit numbers */
1163 in.dx = 1;
1164 test_XIBarrierEvent(&in);
1165 in.dx = 1.3;
1166 test_XIBarrierEvent(&in);
1167 in.dx = 264.908;
1168 test_XIBarrierEvent(&in);
1169 in.dx = 35638.292;
1170 test_XIBarrierEvent(&in);
1171 in.dx = 2947813871.2342;
1172 test_XIBarrierEvent(&in);
1173
1174 in.dx = -1;
1175 test_XIBarrierEvent(&in);
1176 in.dx = -1.3;
1177 test_XIBarrierEvent(&in);
1178 in.dx = -264.908;
1179 test_XIBarrierEvent(&in);
1180 in.dx = -35638.292;
1181 test_XIBarrierEvent(&in);
1182 in.dx = -2947813871.2342;
1183 test_XIBarrierEvent(&in);
1184
1185 in.dy = 1;
1186 test_XIBarrierEvent(&in);
1187 in.dy = 1.3;
1188 test_XIBarrierEvent(&in);
1189 in.dy = 264.908;
1190 test_XIBarrierEvent(&in);
1191 in.dy = 35638.292;
1192 test_XIBarrierEvent(&in);
1193 in.dy = 2947813871.2342;
1194 test_XIBarrierEvent(&in);
1195
1196 in.dy = -1;
1197 test_XIBarrierEvent(&in);
1198 in.dy = -1.3;
1199 test_XIBarrierEvent(&in);
1200 in.dy = -264.908;
1201 test_XIBarrierEvent(&in);
1202 in.dy = -35638.292;
1203 test_XIBarrierEvent(&in);
1204 in.dy = -2947813871.2342;
1205 test_XIBarrierEvent(&in);
1206 }
1207
1208 const testfunc_t*
1209 protocol_eventconvert_test(void)
1210 {
1211 static const testfunc_t testfuncs[] = {
1212 test_convert_XIRawEvent,
1213 test_convert_XIFocusEvent,
1214 test_convert_XIDeviceEvent,
1215 test_convert_XIDeviceChangedEvent,
1216 test_convert_XITouchOwnershipEvent,
1217 test_convert_XIBarrierEvent,
1218 NULL,
1219 };
1220 return testfuncs;
1221 }