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