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