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