protocol-eventconvert.c revision 6747b715
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#include <glib.h>
29
30#include "inputstr.h"
31#include "eventstr.h"
32#include "eventconvert.h"
33#include "exevents.h"
34#include <X11/extensions/XI2proto.h>
35
36
37static void test_values_XIRawEvent(RawDeviceEvent *in, xXIRawEvent *out,
38                                   BOOL swap)
39{
40    int i;
41    unsigned char *ptr;
42    FP3232 *value, *raw_value;
43    int nvals = 0;
44    int bits_set;
45    int len;
46
47    if (swap)
48    {
49        char n;
50
51        swaps(&out->sequenceNumber, n);
52        swapl(&out->length, n);
53        swaps(&out->evtype, n);
54        swaps(&out->deviceid, n);
55        swapl(&out->time, n);
56        swapl(&out->detail, n);
57        swaps(&out->valuators_len, n);
58    }
59
60
61    g_assert(out->type == GenericEvent);
62    g_assert(out->extension == 0); /* IReqCode defaults to 0 */
63    g_assert(out->evtype == GetXI2Type((InternalEvent*)in));
64    g_assert(out->time == in->time);
65    g_assert(out->detail == in->detail.button);
66    g_assert(out->deviceid == in->deviceid);
67    g_assert(out->valuators_len >= bytes_to_int32(bits_to_bytes(sizeof(in->valuators.mask))));
68    g_assert(out->flags == 0); /* FIXME: we don't set the flags yet */
69
70    ptr = (unsigned char*)&out[1];
71    bits_set = 0;
72
73    for (i = 0; out->valuators_len && i < sizeof(in->valuators.mask) * 8; i++)
74    {
75        g_assert (XIMaskIsSet(in->valuators.mask, i) == XIMaskIsSet(ptr, i));
76        if (XIMaskIsSet(in->valuators.mask, i))
77            bits_set++;
78    }
79
80    /* length is len of valuator mask (in 4-byte units) + the number of bits
81     * set. Each bit set represents 2 8-byte values, hence the
82     * 'bits_set * 4' */
83    len = out->valuators_len + bits_set * 4;
84    g_assert(out->length == len);
85
86    nvals = 0;
87
88    for (i = 0; out->valuators_len && i < MAX_VALUATORS; i++)
89    {
90        g_assert (XIMaskIsSet(in->valuators.mask, i) == XIMaskIsSet(ptr, i));
91        if (XIMaskIsSet(in->valuators.mask, i))
92        {
93            FP3232 vi, vo;
94            value = (FP3232*)(((unsigned char*)&out[1]) + out->valuators_len * 4);
95            value += nvals;
96
97            vi.integral = in->valuators.data[i];
98            vi.frac = in->valuators.data_frac[i];
99
100            vo.integral = value->integral;
101            vo.frac = value->frac;
102            if (swap)
103            {
104                char n;
105                swapl(&vo.integral, n);
106                swapl(&vo.frac, n);
107            }
108
109            g_assert(vi.integral == vo.integral);
110            g_assert(vi.frac == vo.frac);
111
112            raw_value = value + bits_set;
113
114            vi.integral = in->valuators.data_raw[i];
115            vi.frac = in->valuators.data_raw_frac[i];
116
117            vo.integral = raw_value->integral;
118            vo.frac = raw_value->frac;
119            if (swap)
120            {
121                char n;
122                swapl(&vo.integral, n);
123                swapl(&vo.frac, n);
124            }
125
126            g_assert(vi.integral == vo.integral);
127            g_assert(vi.frac == vo.frac);
128
129            nvals++;
130        }
131    }
132}
133
134static void test_XIRawEvent(RawDeviceEvent *in)
135{
136    xXIRawEvent *out, *swapped;
137    int rc;
138
139    rc = EventToXI2((InternalEvent*)in, (xEvent**)&out);
140    g_assert(rc == Success);
141
142    test_values_XIRawEvent(in, out, FALSE);
143
144    swapped = calloc(1, sizeof(xEvent) + out->length * 4);
145    XI2EventSwap((xGenericEvent*)out, (xGenericEvent*)swapped);
146    test_values_XIRawEvent(in, swapped, TRUE);
147
148    free(out);
149    free(swapped);
150}
151
152static void test_convert_XIFocusEvent(void)
153{
154    xEvent *out;
155    DeviceEvent in;
156    int rc;
157
158    in.header = ET_Internal;
159    in.type = ET_Enter;
160    rc = EventToXI2((InternalEvent*)&in, &out);
161    g_assert(rc == Success);
162    g_assert(out == NULL);
163
164    in.header = ET_Internal;
165    in.type = ET_FocusIn;
166    rc = EventToXI2((InternalEvent*)&in, &out);
167    g_assert(rc == Success);
168    g_assert(out == NULL);
169
170    in.header = ET_Internal;
171    in.type = ET_FocusOut;
172    rc = EventToXI2((InternalEvent*)&in, &out);
173    g_assert(rc == BadImplementation);
174
175    in.header = ET_Internal;
176    in.type = ET_Leave;
177    rc = EventToXI2((InternalEvent*)&in, &out);
178    g_assert(rc == BadImplementation);
179}
180
181
182static void test_convert_XIRawEvent(void)
183{
184    RawDeviceEvent in;
185    int i;
186
187    memset(&in, 0, sizeof(in));
188
189    g_test_message("Testing all event types");
190    in.header = ET_Internal;
191    in.type = ET_RawMotion;
192    test_XIRawEvent(&in);
193
194    in.header = ET_Internal;
195    in.type = ET_RawKeyPress;
196    test_XIRawEvent(&in);
197
198    in.header = ET_Internal;
199    in.type = ET_RawKeyRelease;
200    test_XIRawEvent(&in);
201
202    in.header = ET_Internal;
203    in.type = ET_RawButtonPress;
204    test_XIRawEvent(&in);
205
206    in.header = ET_Internal;
207    in.type = ET_RawButtonRelease;
208    test_XIRawEvent(&in);
209
210    g_test_message("Testing details and other fields");
211    in.detail.button = 1L;
212    test_XIRawEvent(&in);
213    in.detail.button = 1L << 8;
214    test_XIRawEvent(&in);
215    in.detail.button = 1L << 16;
216    test_XIRawEvent(&in);
217    in.detail.button = 1L << 24;
218    test_XIRawEvent(&in);
219    in.detail.button = ~0L;
220    test_XIRawEvent(&in);
221
222    in.detail.button = 0;
223
224    in.time = 1L;
225    test_XIRawEvent(&in);
226    in.time = 1L << 8;
227    test_XIRawEvent(&in);
228    in.time = 1L << 16;
229    test_XIRawEvent(&in);
230    in.time = 1L << 24;
231    test_XIRawEvent(&in);
232    in.time = ~0L;
233    test_XIRawEvent(&in);
234
235    in.deviceid = 1;
236    test_XIRawEvent(&in);
237    in.deviceid = 1 << 8;
238    test_XIRawEvent(&in);
239    in.deviceid = ~0 & 0xFF;
240    test_XIRawEvent(&in);
241
242    g_test_message("Testing valuator masks");
243    for (i = 0; i < sizeof(in.valuators.mask) * 8; i++)
244    {
245        XISetMask(in.valuators.mask, i);
246        test_XIRawEvent(&in);
247        XIClearMask(in.valuators.mask, i);
248    }
249
250    for (i = 0; i < MAX_VALUATORS; i++)
251    {
252        XISetMask(in.valuators.mask, i);
253
254        in.valuators.data[i] = i;
255        in.valuators.data_raw[i] = i + 10;
256        in.valuators.data_frac[i] = i + 20;
257        in.valuators.data_raw_frac[i] = i + 30;
258        test_XIRawEvent(&in);
259        XIClearMask(in.valuators.mask, i);
260    }
261
262    for (i = 0; i < sizeof(in.valuators.mask) * 8; i++)
263    {
264        XISetMask(in.valuators.mask, i);
265        test_XIRawEvent(&in);
266    }
267}
268
269static void test_values_XIDeviceEvent(DeviceEvent *in, xXIDeviceEvent *out,
270                                      BOOL swap)
271{
272    int buttons, valuators;
273    int i;
274    unsigned char *ptr;
275    FP3232 *values;
276
277    if (swap) {
278        char n;
279
280        swaps(&out->sequenceNumber, n);
281        swapl(&out->length, n);
282        swaps(&out->evtype, n);
283        swaps(&out->deviceid, n);
284        swaps(&out->sourceid, n);
285        swapl(&out->time, n);
286        swapl(&out->detail, n);
287        swapl(&out->root, n);
288        swapl(&out->event, n);
289        swapl(&out->child, n);
290        swapl(&out->root_x, n);
291        swapl(&out->root_y, n);
292        swapl(&out->event_x, n);
293        swapl(&out->event_y, n);
294        swaps(&out->buttons_len, n);
295        swaps(&out->valuators_len, n);
296        swapl(&out->mods.base_mods, n);
297        swapl(&out->mods.latched_mods, n);
298        swapl(&out->mods.locked_mods, n);
299        swapl(&out->mods.effective_mods, n);
300    }
301
302    g_assert(out->extension == 0); /* IReqCode defaults to 0 */
303    g_assert(out->evtype == GetXI2Type((InternalEvent*)in));
304    g_assert(out->time == in->time);
305    g_assert(out->detail == in->detail.button);
306    g_assert(out->length >= 12);
307
308    g_assert(out->deviceid == in->deviceid);
309    g_assert(out->sourceid == in->sourceid);
310
311    g_assert(out->flags == 0); /* FIXME: we don't set the flags yet */
312
313    g_assert(out->root == in->root);
314    g_assert(out->event == None); /* set in FixUpEventFromWindow */
315    g_assert(out->child == None); /* set in FixUpEventFromWindow */
316
317    g_assert(out->mods.base_mods == in->mods.base);
318    g_assert(out->mods.latched_mods == in->mods.latched);
319    g_assert(out->mods.locked_mods == in->mods.locked);
320    g_assert(out->mods.effective_mods == in->mods.effective);
321
322    g_assert(out->group.base_group == in->group.base);
323    g_assert(out->group.latched_group == in->group.latched);
324    g_assert(out->group.locked_group == in->group.locked);
325    g_assert(out->group.effective_group == in->group.effective);
326
327    g_assert(out->event_x == 0); /* set in FixUpEventFromWindow */
328    g_assert(out->event_y == 0); /* set in FixUpEventFromWindow */
329
330    g_assert(out->root_x == FP1616(in->root_x, in->root_x_frac));
331    g_assert(out->root_y == FP1616(in->root_y, in->root_y_frac));
332
333    buttons = 0;
334    for (i = 0; i < bits_to_bytes(sizeof(in->buttons)); i++)
335    {
336        if (XIMaskIsSet(in->buttons, i))
337        {
338            g_assert(out->buttons_len >= bytes_to_int32(bits_to_bytes(i)));
339            buttons++;
340        }
341    }
342
343    ptr = (unsigned char*)&out[1];
344    for (i = 0; i < sizeof(in->buttons) * 8; i++)
345        g_assert(XIMaskIsSet(in->buttons, i) == XIMaskIsSet(ptr, i));
346
347
348    valuators = 0;
349    for (i = 0; i < sizeof(in->valuators.mask) * 8; i++)
350        if (XIMaskIsSet(in->valuators.mask, i))
351            valuators++;
352
353    g_assert(out->valuators_len >= bytes_to_int32(bits_to_bytes(valuators)));
354
355    ptr += out->buttons_len * 4;
356    values = (FP3232*)(ptr + out->valuators_len * 4);
357    for (i = 0; i < sizeof(in->valuators.mask) * 8 ||
358                i < (out->valuators_len * 4) * 8; i++)
359    {
360        if (i > sizeof(in->valuators.mask) * 8)
361            g_assert(!XIMaskIsSet(ptr, i));
362        else if (i > out->valuators_len * 4 * 8)
363            g_assert(!XIMaskIsSet(in->valuators.mask, i));
364        else {
365            g_assert(XIMaskIsSet(in->valuators.mask, i) ==
366                     XIMaskIsSet(ptr, i));
367
368            if (XIMaskIsSet(ptr, i))
369            {
370                FP3232 vi, vo;
371
372                vi.integral = in->valuators.data[i];
373                vi.frac = in->valuators.data_frac[i];
374
375                vo = *values;
376
377                if (swap)
378                {
379                    char n;
380                    swapl(&vo.integral, n);
381                    swapl(&vo.frac, n);
382                }
383
384
385                g_assert(vi.integral == vo.integral);
386                g_assert(vi.frac == vo.frac);
387                values++;
388            }
389        }
390    }
391}
392
393static void test_XIDeviceEvent(DeviceEvent *in)
394{
395    xXIDeviceEvent *out, *swapped;
396    int rc;
397
398    rc = EventToXI2((InternalEvent*)in, (xEvent**)&out);
399    g_assert(rc == Success);
400
401    test_values_XIDeviceEvent(in, out, FALSE);
402
403    swapped = calloc(1, sizeof(xEvent) + out->length * 4);
404    XI2EventSwap((xGenericEvent*)out, (xGenericEvent*)swapped);
405    test_values_XIDeviceEvent(in, swapped, TRUE);
406
407    free(out);
408    free(swapped);
409}
410
411static void test_convert_XIDeviceEvent(void)
412{
413    DeviceEvent in;
414    int i;
415
416    memset(&in, 0, sizeof(in));
417
418    g_test_message("Testing simple field values");
419    in.header = ET_Internal;
420    in.type = ET_Motion;
421    in.length = sizeof(DeviceEvent);
422    in.time             = 0;
423    in.deviceid         = 1;
424    in.sourceid         = 2;
425    in.root             = 3;
426    in.root_x           = 4;
427    in.root_x_frac      = 5;
428    in.root_y           = 6;
429    in.root_y_frac      = 7;
430    in.detail.button    = 8;
431    in.mods.base        = 9;
432    in.mods.latched     = 10;
433    in.mods.locked      = 11;
434    in.mods.effective   = 11;
435    in.group.base       = 12;
436    in.group.latched    = 13;
437    in.group.locked     = 14;
438    in.group.effective  = 15;
439
440    test_XIDeviceEvent(&in);
441
442    g_test_message("Testing field ranges");
443    /* 32 bit */
444    in.detail.button = 1L;
445    test_XIDeviceEvent(&in);
446    in.detail.button = 1L << 8;
447    test_XIDeviceEvent(&in);
448    in.detail.button = 1L << 16;
449    test_XIDeviceEvent(&in);
450    in.detail.button = 1L << 24;
451    test_XIDeviceEvent(&in);
452    in.detail.button = ~0L;
453    test_XIDeviceEvent(&in);
454
455    /* 32 bit */
456    in.time = 1L;
457    test_XIDeviceEvent(&in);
458    in.time = 1L << 8;
459    test_XIDeviceEvent(&in);
460    in.time = 1L << 16;
461    test_XIDeviceEvent(&in);
462    in.time = 1L << 24;
463    test_XIDeviceEvent(&in);
464    in.time = ~0L;
465    test_XIDeviceEvent(&in);
466
467    /* 16 bit */
468    in.deviceid = 1;
469    test_XIDeviceEvent(&in);
470    in.deviceid = 1 << 8;
471    test_XIDeviceEvent(&in);
472    in.deviceid = ~0 & 0xFF;
473    test_XIDeviceEvent(&in);
474
475    /* 16 bit */
476    in.sourceid = 1;
477    test_XIDeviceEvent(&in);
478    in.deviceid = 1 << 8;
479    test_XIDeviceEvent(&in);
480    in.deviceid = ~0 & 0xFF;
481    test_XIDeviceEvent(&in);
482
483    /* 32 bit */
484    in.root = 1L;
485    test_XIDeviceEvent(&in);
486    in.root = 1L << 8;
487    test_XIDeviceEvent(&in);
488    in.root = 1L << 16;
489    test_XIDeviceEvent(&in);
490    in.root = 1L << 24;
491    test_XIDeviceEvent(&in);
492    in.root = ~0L;
493    test_XIDeviceEvent(&in);
494
495    /* 16 bit */
496    in.root_x = 1;
497    test_XIDeviceEvent(&in);
498    in.root_x = 1 << 8;
499    test_XIDeviceEvent(&in);
500    in.root_x = ~0 & 0xFF;
501    test_XIDeviceEvent(&in);
502
503    in.root_x_frac = 1;
504    test_XIDeviceEvent(&in);
505    in.root_x_frac = 1 << 8;
506    test_XIDeviceEvent(&in);
507    in.root_x_frac = ~0 & 0xFF;
508    test_XIDeviceEvent(&in);
509
510    in.root_y = 1;
511    test_XIDeviceEvent(&in);
512    in.root_y = 1 << 8;
513    test_XIDeviceEvent(&in);
514    in.root_y = ~0 & 0xFF;
515    test_XIDeviceEvent(&in);
516
517    in.root_y_frac = 1;
518    test_XIDeviceEvent(&in);
519    in.root_y_frac = 1 << 8;
520    test_XIDeviceEvent(&in);
521    in.root_y_frac = ~0 & 0xFF;
522    test_XIDeviceEvent(&in);
523
524    /* 32 bit */
525    in.mods.base = 1L;
526    test_XIDeviceEvent(&in);
527    in.mods.base = 1L << 8;
528    test_XIDeviceEvent(&in);
529    in.mods.base = 1L << 16;
530    test_XIDeviceEvent(&in);
531    in.mods.base = 1L << 24;
532    test_XIDeviceEvent(&in);
533    in.mods.base = ~0L;
534    test_XIDeviceEvent(&in);
535
536    in.mods.latched = 1L;
537    test_XIDeviceEvent(&in);
538    in.mods.latched = 1L << 8;
539    test_XIDeviceEvent(&in);
540    in.mods.latched = 1L << 16;
541    test_XIDeviceEvent(&in);
542    in.mods.latched = 1L << 24;
543    test_XIDeviceEvent(&in);
544    in.mods.latched = ~0L;
545    test_XIDeviceEvent(&in);
546
547    in.mods.locked = 1L;
548    test_XIDeviceEvent(&in);
549    in.mods.locked = 1L << 8;
550    test_XIDeviceEvent(&in);
551    in.mods.locked = 1L << 16;
552    test_XIDeviceEvent(&in);
553    in.mods.locked = 1L << 24;
554    test_XIDeviceEvent(&in);
555    in.mods.locked = ~0L;
556    test_XIDeviceEvent(&in);
557
558    in.mods.effective = 1L;
559    test_XIDeviceEvent(&in);
560    in.mods.effective = 1L << 8;
561    test_XIDeviceEvent(&in);
562    in.mods.effective = 1L << 16;
563    test_XIDeviceEvent(&in);
564    in.mods.effective = 1L << 24;
565    test_XIDeviceEvent(&in);
566    in.mods.effective = ~0L;
567    test_XIDeviceEvent(&in);
568
569    /* 8 bit */
570    in.group.base = 1;
571    test_XIDeviceEvent(&in);
572    in.group.base = ~0 & 0xFF;
573    test_XIDeviceEvent(&in);
574
575    in.group.latched = 1;
576    test_XIDeviceEvent(&in);
577    in.group.latched = ~0 & 0xFF;
578    test_XIDeviceEvent(&in);
579
580    in.group.locked = 1;
581    test_XIDeviceEvent(&in);
582    in.group.locked = ~0 & 0xFF;
583    test_XIDeviceEvent(&in);
584
585    in.mods.effective = 1;
586    test_XIDeviceEvent(&in);
587    in.mods.effective = ~0 & 0xFF;
588    test_XIDeviceEvent(&in);
589
590    g_test_message("Testing button masks");
591    for (i = 0; i < sizeof(in.buttons) * 8; i++)
592    {
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    {
600        XISetMask(in.buttons, i);
601        test_XIDeviceEvent(&in);
602    }
603
604    g_test_message("Testing valuator masks");
605    for (i = 0; i < sizeof(in.valuators.mask) * 8; i++)
606    {
607        XISetMask(in.valuators.mask, i);
608        test_XIDeviceEvent(&in);
609        XIClearMask(in.valuators.mask, i);
610    }
611
612    for (i = 0; i < sizeof(in.valuators.mask) * 8; i++)
613    {
614        XISetMask(in.valuators.mask, i);
615
616        in.valuators.data[i] = i;
617        in.valuators.data_frac[i] = i + 20;
618        test_XIDeviceEvent(&in);
619        XIClearMask(in.valuators.mask, i);
620    }
621
622    for (i = 0; i < sizeof(in.valuators.mask) * 8; i++)
623    {
624        XISetMask(in.valuators.mask, i);
625        test_XIDeviceEvent(&in);
626    }
627}
628
629static void test_values_XIDeviceChangedEvent(DeviceChangedEvent *in,
630                                             xXIDeviceChangedEvent *out,
631                                             BOOL swap)
632{
633    int i, j;
634    unsigned char *ptr;
635
636    if (swap)
637    {
638        char n;
639
640        swaps(&out->sequenceNumber, n);
641        swapl(&out->length, n);
642        swaps(&out->evtype, n);
643        swaps(&out->deviceid, n);
644        swaps(&out->sourceid, n);
645        swapl(&out->time, n);
646        swaps(&out->num_classes, n);
647    }
648
649    g_assert(out->type == GenericEvent);
650    g_assert(out->extension == 0); /* IReqCode defaults to 0 */
651    g_assert(out->evtype == GetXI2Type((InternalEvent*)in));
652    g_assert(out->time == in->time);
653    g_assert(out->deviceid == in->deviceid);
654    g_assert(out->sourceid == in->sourceid);
655
656    ptr = (unsigned char*)&out[1];
657    for (i = 0; i < out->num_classes; i++)
658    {
659        xXIAnyInfo* any = (xXIAnyInfo*)ptr;
660
661        if (swap)
662        {
663            char n;
664            swaps(&any->length, n);
665            swaps(&any->type, n);
666            swaps(&any->sourceid, n);
667        }
668
669        switch(any->type)
670        {
671            case XIButtonClass:
672                {
673                    xXIButtonInfo *b = (xXIButtonInfo*)any;
674                    Atom *names;
675
676                    if (swap)
677                    {
678                        char n;
679                        swaps(&b->num_buttons, n);
680                    }
681
682                    g_assert(b->length ==
683                            bytes_to_int32(sizeof(xXIButtonInfo)) +
684                            bytes_to_int32(bits_to_bytes(b->num_buttons)) +
685                            b->num_buttons);
686                    g_assert(b->num_buttons == in->buttons.num_buttons);
687
688                    names = (Atom*)((char*)&b[1] +
689                            pad_to_int32(bits_to_bytes(b->num_buttons)));
690                    for (j = 0; j < b->num_buttons; j++)
691                    {
692                        if (swap)
693                        {
694                            char n;
695                            swapl(&names[j], n);
696                        }
697                        g_assert(names[j] == in->buttons.names[j]);
698                    }
699                }
700                break;
701            case XIKeyClass:
702                {
703                    xXIKeyInfo *k = (xXIKeyInfo*)any;
704                    uint32_t *kc;
705
706                    if (swap)
707                    {
708                        char n;
709                        swaps(&k->num_keycodes, n);
710                    }
711
712                    g_assert(k->length ==
713                            bytes_to_int32(sizeof(xXIKeyInfo)) +
714                            k->num_keycodes);
715                    g_assert(k->num_keycodes == in->keys.max_keycode -
716                            in->keys.min_keycode + 1);
717
718                    kc = (uint32_t*)&k[1];
719                    for (j = 0; j < k->num_keycodes; j++)
720                    {
721                        if (swap)
722                        {
723                            char n;
724                            swapl(&kc[j], n);
725                        }
726                        g_assert(kc[j] >= in->keys.min_keycode);
727                        g_assert(kc[j] <= in->keys.max_keycode);
728                    }
729                }
730                break;
731            case XIValuatorClass:
732                {
733                    xXIValuatorInfo *v = (xXIValuatorInfo*)any;
734                    g_assert(v->length ==
735                             bytes_to_int32(sizeof(xXIValuatorInfo)));
736
737                }
738                break;
739            default:
740                g_error("Invalid class type.\n");
741                break;
742        }
743
744        ptr += any->length * 4;
745    }
746
747}
748
749static void test_XIDeviceChangedEvent(DeviceChangedEvent *in)
750{
751    xXIDeviceChangedEvent *out, *swapped;
752    int rc;
753
754    rc = EventToXI2((InternalEvent*)in, (xEvent**)&out);
755    g_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 test_convert_XIDeviceChangedEvent(void)
768{
769    DeviceChangedEvent in;
770    int i;
771
772    g_test_message("Testing simple field values");
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 = DEVCHANGE_SLAVE_SWITCH | DEVCHANGE_POINTER_EVENT | DEVCHANGE_KEYBOARD_EVENT;
783
784    for (i = 0; i < MAX_BUTTONS; i++)
785        in.buttons.names[i] = i + 10;
786
787    in.keys.min_keycode = 8;
788    in.keys.max_keycode = 255;
789
790    test_XIDeviceChangedEvent(&in);
791
792    in.time = 1L;
793    test_XIDeviceChangedEvent(&in);
794    in.time = 1L << 8;
795    test_XIDeviceChangedEvent(&in);
796    in.time = 1L << 16;
797    test_XIDeviceChangedEvent(&in);
798    in.time = 1L << 24;
799    test_XIDeviceChangedEvent(&in);
800    in.time = ~0L;
801    test_XIDeviceChangedEvent(&in);
802
803    in.deviceid = 1L;
804    test_XIDeviceChangedEvent(&in);
805    in.deviceid = 1L << 8;
806    test_XIDeviceChangedEvent(&in);
807    in.deviceid = ~0 & 0xFFFF;
808    test_XIDeviceChangedEvent(&in);
809
810    in.sourceid = 1L;
811    test_XIDeviceChangedEvent(&in);
812    in.sourceid = 1L << 8;
813    test_XIDeviceChangedEvent(&in);
814    in.sourceid = ~0 & 0xFFFF;
815    test_XIDeviceChangedEvent(&in);
816
817    in.masterid = 1L;
818    test_XIDeviceChangedEvent(&in);
819    in.masterid = 1L << 8;
820    test_XIDeviceChangedEvent(&in);
821    in.masterid = ~0 & 0xFFFF;
822    test_XIDeviceChangedEvent(&in);
823
824    in.buttons.num_buttons = 0;
825    test_XIDeviceChangedEvent(&in);
826
827    in.buttons.num_buttons = 1;
828    test_XIDeviceChangedEvent(&in);
829
830    in.buttons.num_buttons = MAX_BUTTONS;
831    test_XIDeviceChangedEvent(&in);
832
833    in.keys.min_keycode = 0;
834    in.keys.max_keycode = 0;
835    test_XIDeviceChangedEvent(&in);
836
837    in.keys.max_keycode = 1 << 8;
838    test_XIDeviceChangedEvent(&in);
839
840    in.keys.max_keycode = 0xFFFC; /* highest range, above that the length
841                                     field gives up */
842    test_XIDeviceChangedEvent(&in);
843
844    in.keys.min_keycode = 1 << 8;
845    in.keys.max_keycode = 1 << 8;
846    test_XIDeviceChangedEvent(&in);
847
848    in.keys.min_keycode = 1 << 8;
849    in.keys.max_keycode = 0;
850    test_XIDeviceChangedEvent(&in);
851
852    in.num_valuators = 0;
853    test_XIDeviceChangedEvent(&in);
854
855    in.num_valuators = 1;
856    test_XIDeviceChangedEvent(&in);
857
858    in.num_valuators = MAX_VALUATORS;
859    test_XIDeviceChangedEvent(&in);
860
861    for (i = 0; i < MAX_VALUATORS; i++)
862    {
863        in.valuators[i].min = 0;
864        in.valuators[i].max = 0;
865        test_XIDeviceChangedEvent(&in);
866
867        in.valuators[i].max = 1 << 8;
868        test_XIDeviceChangedEvent(&in);
869        in.valuators[i].max = 1 << 16;
870        test_XIDeviceChangedEvent(&in);
871        in.valuators[i].max = 1 << 24;
872        test_XIDeviceChangedEvent(&in);
873        in.valuators[i].max = abs(~0);
874        test_XIDeviceChangedEvent(&in);
875
876        in.valuators[i].resolution = 1 << 8;
877        test_XIDeviceChangedEvent(&in);
878        in.valuators[i].resolution = 1 << 16;
879        test_XIDeviceChangedEvent(&in);
880        in.valuators[i].resolution = 1 << 24;
881        test_XIDeviceChangedEvent(&in);
882        in.valuators[i].resolution = abs(~0);
883        test_XIDeviceChangedEvent(&in);
884
885        in.valuators[i].name = i;
886        test_XIDeviceChangedEvent(&in);
887
888        in.valuators[i].mode = Relative;
889        test_XIDeviceChangedEvent(&in);
890
891        in.valuators[i].mode = Absolute;
892        test_XIDeviceChangedEvent(&in);
893    }
894}
895
896int main(int argc, char** argv)
897{
898    g_test_init(&argc, &argv,NULL);
899    g_test_bug_base("https://bugzilla.freedesktop.org/show_bug.cgi?id=");
900
901    g_test_add_func("/xi2/eventconvert/XIRawEvent", test_convert_XIRawEvent);
902    g_test_add_func("/xi2/eventconvert/XIFocusEvent", test_convert_XIFocusEvent);
903    g_test_add_func("/xi2/eventconvert/XIDeviceEvent", test_convert_XIDeviceEvent);
904    g_test_add_func("/xi2/eventconvert/XIDeviceChangedEvent", test_convert_XIDeviceChangedEvent);
905
906    return g_test_run();
907}
908