16747b715Smrg/**
26747b715Smrg * Copyright © 2009 Red Hat, Inc.
36747b715Smrg *
46747b715Smrg *  Permission is hereby granted, free of charge, to any person obtaining a
56747b715Smrg *  copy of this software and associated documentation files (the "Software"),
66747b715Smrg *  to deal in the Software without restriction, including without limitation
76747b715Smrg *  the rights to use, copy, modify, merge, publish, distribute, sublicense,
86747b715Smrg *  and/or sell copies of the Software, and to permit persons to whom the
96747b715Smrg *  Software is furnished to do so, subject to the following conditions:
106747b715Smrg *
116747b715Smrg *  The above copyright notice and this permission notice (including the next
126747b715Smrg *  paragraph) shall be included in all copies or substantial portions of the
136747b715Smrg *  Software.
146747b715Smrg * *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
156747b715Smrg *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
166747b715Smrg *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
176747b715Smrg *  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
186747b715Smrg *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
196747b715Smrg *  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
206747b715Smrg *  DEALINGS IN THE SOFTWARE.
216747b715Smrg */
226747b715Smrg
23ed6184dfSmrg/* Test relies on assert() */
24ed6184dfSmrg#undef NDEBUG
25ed6184dfSmrg
266747b715Smrg#ifdef HAVE_DIX_CONFIG_H
276747b715Smrg#include <dix-config.h>
286747b715Smrg#endif
296747b715Smrg
306747b715Smrg#include <stdint.h>
316747b715Smrg
326747b715Smrg#include "inputstr.h"
336747b715Smrg#include "eventstr.h"
346747b715Smrg#include "eventconvert.h"
356747b715Smrg#include "exevents.h"
3635c4bbdfSmrg#include "inpututils.h"
376747b715Smrg#include <X11/extensions/XI2proto.h>
386747b715Smrg
391b5d61b8Smrg#include "protocol-common.h"
401b5d61b8Smrg
4135c4bbdfSmrgstatic void
4235c4bbdfSmrgtest_values_XIRawEvent(RawDeviceEvent *in, xXIRawEvent * out, BOOL swap)
436747b715Smrg{
446747b715Smrg    int i;
456747b715Smrg    unsigned char *ptr;
466747b715Smrg    FP3232 *value, *raw_value;
476747b715Smrg    int nvals = 0;
486747b715Smrg    int bits_set;
496747b715Smrg    int len;
5035c4bbdfSmrg    uint32_t flagmask = 0;
516747b715Smrg
5235c4bbdfSmrg    if (swap) {
5335c4bbdfSmrg        swaps(&out->sequenceNumber);
5435c4bbdfSmrg        swapl(&out->length);
5535c4bbdfSmrg        swaps(&out->evtype);
5635c4bbdfSmrg        swaps(&out->deviceid);
5735c4bbdfSmrg        swapl(&out->time);
5835c4bbdfSmrg        swapl(&out->detail);
5935c4bbdfSmrg        swaps(&out->valuators_len);
6035c4bbdfSmrg        swapl(&out->flags);
616747b715Smrg    }
626747b715Smrg
6335c4bbdfSmrg    assert(out->type == GenericEvent);
6435c4bbdfSmrg    assert(out->extension == 0);        /* IReqCode defaults to 0 */
6535c4bbdfSmrg    assert(out->evtype == GetXI2Type(in->type));
6635c4bbdfSmrg    assert(out->time == in->time);
6735c4bbdfSmrg    assert(out->detail == in->detail.button);
6835c4bbdfSmrg    assert(out->deviceid == in->deviceid);
6935c4bbdfSmrg    assert(out->valuators_len >=
7035c4bbdfSmrg           bytes_to_int32(bits_to_bytes(sizeof(in->valuators.mask))));
716747b715Smrg
7235c4bbdfSmrg    switch (in->type) {
7335c4bbdfSmrg    case ET_RawMotion:
7435c4bbdfSmrg    case ET_RawButtonPress:
7535c4bbdfSmrg    case ET_RawButtonRelease:
7635c4bbdfSmrg        flagmask = XIPointerEmulated;
7735c4bbdfSmrg        break;
7835c4bbdfSmrg    default:
7935c4bbdfSmrg        flagmask = 0;
8035c4bbdfSmrg    }
8135c4bbdfSmrg    assert((out->flags & ~flagmask) == 0);
826747b715Smrg
8335c4bbdfSmrg    ptr = (unsigned char *) &out[1];
846747b715Smrg    bits_set = 0;
856747b715Smrg
8635c4bbdfSmrg    for (i = 0; out->valuators_len && i < sizeof(in->valuators.mask) * 8; i++) {
8735c4bbdfSmrg        if (i >= MAX_VALUATORS)
8835c4bbdfSmrg            assert(!XIMaskIsSet(in->valuators.mask, i));
8935c4bbdfSmrg        assert(XIMaskIsSet(in->valuators.mask, i) == XIMaskIsSet(ptr, i));
906747b715Smrg        if (XIMaskIsSet(in->valuators.mask, i))
916747b715Smrg            bits_set++;
926747b715Smrg    }
936747b715Smrg
946747b715Smrg    /* length is len of valuator mask (in 4-byte units) + the number of bits
956747b715Smrg     * set. Each bit set represents 2 8-byte values, hence the
966747b715Smrg     * 'bits_set * 4' */
976747b715Smrg    len = out->valuators_len + bits_set * 4;
9835c4bbdfSmrg    assert(out->length == len);
996747b715Smrg
1006747b715Smrg    nvals = 0;
1016747b715Smrg
10235c4bbdfSmrg    for (i = 0; out->valuators_len && i < MAX_VALUATORS; i++) {
10335c4bbdfSmrg        assert(XIMaskIsSet(in->valuators.mask, i) == XIMaskIsSet(ptr, i));
10435c4bbdfSmrg        if (XIMaskIsSet(in->valuators.mask, i)) {
1056747b715Smrg            FP3232 vi, vo;
10635c4bbdfSmrg
10735c4bbdfSmrg            value =
10835c4bbdfSmrg                (FP3232 *) (((unsigned char *) &out[1]) +
10935c4bbdfSmrg                            out->valuators_len * 4);
1106747b715Smrg            value += nvals;
1116747b715Smrg
11235c4bbdfSmrg            vi = double_to_fp3232(in->valuators.data[i]);
1136747b715Smrg
1146747b715Smrg            vo.integral = value->integral;
1156747b715Smrg            vo.frac = value->frac;
11635c4bbdfSmrg            if (swap) {
11735c4bbdfSmrg                swapl(&vo.integral);
11835c4bbdfSmrg                swapl(&vo.frac);
1196747b715Smrg            }
1206747b715Smrg
12135c4bbdfSmrg            assert(vi.integral == vo.integral);
12235c4bbdfSmrg            assert(vi.frac == vo.frac);
1236747b715Smrg
1246747b715Smrg            raw_value = value + bits_set;
1256747b715Smrg
12635c4bbdfSmrg            vi = double_to_fp3232(in->valuators.data_raw[i]);
1276747b715Smrg
1286747b715Smrg            vo.integral = raw_value->integral;
1296747b715Smrg            vo.frac = raw_value->frac;
13035c4bbdfSmrg            if (swap) {
13135c4bbdfSmrg                swapl(&vo.integral);
13235c4bbdfSmrg                swapl(&vo.frac);
1336747b715Smrg            }
1346747b715Smrg
13535c4bbdfSmrg            assert(vi.integral == vo.integral);
13635c4bbdfSmrg            assert(vi.frac == vo.frac);
1376747b715Smrg
1386747b715Smrg            nvals++;
1396747b715Smrg        }
1406747b715Smrg    }
1416747b715Smrg}
1426747b715Smrg
14335c4bbdfSmrgstatic void
14435c4bbdfSmrgtest_XIRawEvent(RawDeviceEvent *in)
1456747b715Smrg{
1466747b715Smrg    xXIRawEvent *out, *swapped;
1476747b715Smrg    int rc;
1486747b715Smrg
14935c4bbdfSmrg    rc = EventToXI2((InternalEvent *) in, (xEvent **) &out);
15035c4bbdfSmrg    assert(rc == Success);
1516747b715Smrg
1526747b715Smrg    test_values_XIRawEvent(in, out, FALSE);
1536747b715Smrg
1546747b715Smrg    swapped = calloc(1, sizeof(xEvent) + out->length * 4);
15535c4bbdfSmrg    XI2EventSwap((xGenericEvent *) out, (xGenericEvent *) swapped);
1566747b715Smrg    test_values_XIRawEvent(in, swapped, TRUE);
1576747b715Smrg
1586747b715Smrg    free(out);
1596747b715Smrg    free(swapped);
1606747b715Smrg}
1616747b715Smrg
16235c4bbdfSmrgstatic void
16335c4bbdfSmrgtest_convert_XIFocusEvent(void)
1646747b715Smrg{
1656747b715Smrg    xEvent *out;
1666747b715Smrg    DeviceEvent in;
1676747b715Smrg    int rc;
1686747b715Smrg
1696747b715Smrg    in.header = ET_Internal;
1706747b715Smrg    in.type = ET_Enter;
17135c4bbdfSmrg    rc = EventToXI2((InternalEvent *) &in, &out);
17235c4bbdfSmrg    assert(rc == Success);
17335c4bbdfSmrg    assert(out == NULL);
1746747b715Smrg
1756747b715Smrg    in.header = ET_Internal;
1766747b715Smrg    in.type = ET_FocusIn;
17735c4bbdfSmrg    rc = EventToXI2((InternalEvent *) &in, &out);
17835c4bbdfSmrg    assert(rc == Success);
17935c4bbdfSmrg    assert(out == NULL);
1806747b715Smrg
1816747b715Smrg    in.header = ET_Internal;
1826747b715Smrg    in.type = ET_FocusOut;
18335c4bbdfSmrg    rc = EventToXI2((InternalEvent *) &in, &out);
18435c4bbdfSmrg    assert(rc == BadImplementation);
1856747b715Smrg
1866747b715Smrg    in.header = ET_Internal;
1876747b715Smrg    in.type = ET_Leave;
18835c4bbdfSmrg    rc = EventToXI2((InternalEvent *) &in, &out);
18935c4bbdfSmrg    assert(rc == BadImplementation);
1906747b715Smrg}
1916747b715Smrg
19235c4bbdfSmrgstatic void
19335c4bbdfSmrgtest_convert_XIRawEvent(void)
1946747b715Smrg{
1956747b715Smrg    RawDeviceEvent in;
1966747b715Smrg    int i;
1976747b715Smrg
1986747b715Smrg    memset(&in, 0, sizeof(in));
1996747b715Smrg
2006747b715Smrg    in.header = ET_Internal;
2016747b715Smrg    in.type = ET_RawMotion;
2026747b715Smrg    test_XIRawEvent(&in);
2036747b715Smrg
2046747b715Smrg    in.header = ET_Internal;
2056747b715Smrg    in.type = ET_RawKeyPress;
2066747b715Smrg    test_XIRawEvent(&in);
2076747b715Smrg
2086747b715Smrg    in.header = ET_Internal;
2096747b715Smrg    in.type = ET_RawKeyRelease;
2106747b715Smrg    test_XIRawEvent(&in);
2116747b715Smrg
2126747b715Smrg    in.header = ET_Internal;
2136747b715Smrg    in.type = ET_RawButtonPress;
2146747b715Smrg    test_XIRawEvent(&in);
2156747b715Smrg
2166747b715Smrg    in.header = ET_Internal;
2176747b715Smrg    in.type = ET_RawButtonRelease;
2186747b715Smrg    test_XIRawEvent(&in);
2196747b715Smrg
2206747b715Smrg    in.detail.button = 1L;
2216747b715Smrg    test_XIRawEvent(&in);
2226747b715Smrg    in.detail.button = 1L << 8;
2236747b715Smrg    test_XIRawEvent(&in);
2246747b715Smrg    in.detail.button = 1L << 16;
2256747b715Smrg    test_XIRawEvent(&in);
2266747b715Smrg    in.detail.button = 1L << 24;
2276747b715Smrg    test_XIRawEvent(&in);
2286747b715Smrg    in.detail.button = ~0L;
2296747b715Smrg    test_XIRawEvent(&in);
2306747b715Smrg
2316747b715Smrg    in.detail.button = 0;
2326747b715Smrg
2336747b715Smrg    in.time = 1L;
2346747b715Smrg    test_XIRawEvent(&in);
2356747b715Smrg    in.time = 1L << 8;
2366747b715Smrg    test_XIRawEvent(&in);
2376747b715Smrg    in.time = 1L << 16;
2386747b715Smrg    test_XIRawEvent(&in);
2396747b715Smrg    in.time = 1L << 24;
2406747b715Smrg    test_XIRawEvent(&in);
2416747b715Smrg    in.time = ~0L;
2426747b715Smrg    test_XIRawEvent(&in);
2436747b715Smrg
2446747b715Smrg    in.deviceid = 1;
2456747b715Smrg    test_XIRawEvent(&in);
2466747b715Smrg    in.deviceid = 1 << 8;
2476747b715Smrg    test_XIRawEvent(&in);
2486747b715Smrg    in.deviceid = ~0 & 0xFF;
2496747b715Smrg    test_XIRawEvent(&in);
2506747b715Smrg
25135c4bbdfSmrg    for (i = 0; i < MAX_VALUATORS; i++) {
2526747b715Smrg        XISetMask(in.valuators.mask, i);
2536747b715Smrg        test_XIRawEvent(&in);
2546747b715Smrg        XIClearMask(in.valuators.mask, i);
2556747b715Smrg    }
2566747b715Smrg
25735c4bbdfSmrg    for (i = 0; i < MAX_VALUATORS; i++) {
2586747b715Smrg        XISetMask(in.valuators.mask, i);
2596747b715Smrg
26035c4bbdfSmrg        in.valuators.data[i] = i + (i * 0.0010);
26135c4bbdfSmrg        in.valuators.data_raw[i] = (i + 10) + (i * 0.0030);
2626747b715Smrg        test_XIRawEvent(&in);
2636747b715Smrg        XIClearMask(in.valuators.mask, i);
2646747b715Smrg    }
2656747b715Smrg
26635c4bbdfSmrg    for (i = 0; i < MAX_VALUATORS; i++) {
2676747b715Smrg        XISetMask(in.valuators.mask, i);
2686747b715Smrg        test_XIRawEvent(&in);
2696747b715Smrg    }
2706747b715Smrg}
2716747b715Smrg
27235c4bbdfSmrgstatic void
27335c4bbdfSmrgtest_values_XIDeviceEvent(DeviceEvent *in, xXIDeviceEvent * out, BOOL swap)
2746747b715Smrg{
2756747b715Smrg    int buttons, valuators;
2766747b715Smrg    int i;
2776747b715Smrg    unsigned char *ptr;
2789ace9065Smrg    uint32_t flagmask = 0;
2796747b715Smrg    FP3232 *values;
2806747b715Smrg
2816747b715Smrg    if (swap) {
28235c4bbdfSmrg        swaps(&out->sequenceNumber);
28335c4bbdfSmrg        swapl(&out->length);
28435c4bbdfSmrg        swaps(&out->evtype);
28535c4bbdfSmrg        swaps(&out->deviceid);
28635c4bbdfSmrg        swaps(&out->sourceid);
28735c4bbdfSmrg        swapl(&out->time);
28835c4bbdfSmrg        swapl(&out->detail);
28935c4bbdfSmrg        swapl(&out->root);
29035c4bbdfSmrg        swapl(&out->event);
29135c4bbdfSmrg        swapl(&out->child);
29235c4bbdfSmrg        swapl(&out->root_x);
29335c4bbdfSmrg        swapl(&out->root_y);
29435c4bbdfSmrg        swapl(&out->event_x);
29535c4bbdfSmrg        swapl(&out->event_y);
29635c4bbdfSmrg        swaps(&out->buttons_len);
29735c4bbdfSmrg        swaps(&out->valuators_len);
29835c4bbdfSmrg        swapl(&out->mods.base_mods);
29935c4bbdfSmrg        swapl(&out->mods.latched_mods);
30035c4bbdfSmrg        swapl(&out->mods.locked_mods);
30135c4bbdfSmrg        swapl(&out->mods.effective_mods);
30235c4bbdfSmrg        swapl(&out->flags);
3036747b715Smrg    }
3046747b715Smrg
30535c4bbdfSmrg    assert(out->extension == 0);        /* IReqCode defaults to 0 */
30635c4bbdfSmrg    assert(out->evtype == GetXI2Type(in->type));
30735c4bbdfSmrg    assert(out->time == in->time);
30835c4bbdfSmrg    assert(out->detail == in->detail.button);
30935c4bbdfSmrg    assert(out->length >= 12);
3106747b715Smrg
31135c4bbdfSmrg    assert(out->deviceid == in->deviceid);
31235c4bbdfSmrg    assert(out->sourceid == in->sourceid);
3136747b715Smrg
3149ace9065Smrg    switch (in->type) {
31535c4bbdfSmrg    case ET_ButtonPress:
31635c4bbdfSmrg    case ET_Motion:
31735c4bbdfSmrg    case ET_ButtonRelease:
31835c4bbdfSmrg        flagmask = XIPointerEmulated;
31935c4bbdfSmrg        break;
32035c4bbdfSmrg    case ET_KeyPress:
32135c4bbdfSmrg        flagmask = XIKeyRepeat;
32235c4bbdfSmrg        break;
32335c4bbdfSmrg    default:
32435c4bbdfSmrg        flagmask = 0;
32535c4bbdfSmrg        break;
3269ace9065Smrg    }
32735c4bbdfSmrg    assert((out->flags & ~flagmask) == 0);
3286747b715Smrg
32935c4bbdfSmrg    assert(out->root == in->root);
33035c4bbdfSmrg    assert(out->event == None); /* set in FixUpEventFromWindow */
33135c4bbdfSmrg    assert(out->child == None); /* set in FixUpEventFromWindow */
3326747b715Smrg
33335c4bbdfSmrg    assert(out->mods.base_mods == in->mods.base);
33435c4bbdfSmrg    assert(out->mods.latched_mods == in->mods.latched);
33535c4bbdfSmrg    assert(out->mods.locked_mods == in->mods.locked);
33635c4bbdfSmrg    assert(out->mods.effective_mods == in->mods.effective);
3376747b715Smrg
33835c4bbdfSmrg    assert(out->group.base_group == in->group.base);
33935c4bbdfSmrg    assert(out->group.latched_group == in->group.latched);
34035c4bbdfSmrg    assert(out->group.locked_group == in->group.locked);
34135c4bbdfSmrg    assert(out->group.effective_group == in->group.effective);
3426747b715Smrg
34335c4bbdfSmrg    assert(out->event_x == 0);  /* set in FixUpEventFromWindow */
34435c4bbdfSmrg    assert(out->event_y == 0);  /* set in FixUpEventFromWindow */
3456747b715Smrg
34635c4bbdfSmrg    assert(out->root_x == double_to_fp1616(in->root_x + in->root_x_frac));
34735c4bbdfSmrg    assert(out->root_y == double_to_fp1616(in->root_y + in->root_y_frac));
3486747b715Smrg
3496747b715Smrg    buttons = 0;
35035c4bbdfSmrg    for (i = 0; i < bits_to_bytes(sizeof(in->buttons)); i++) {
35135c4bbdfSmrg        if (XIMaskIsSet(in->buttons, i)) {
35235c4bbdfSmrg            assert(out->buttons_len >= bytes_to_int32(bits_to_bytes(i)));
3536747b715Smrg            buttons++;
3546747b715Smrg        }
3556747b715Smrg    }
3566747b715Smrg
35735c4bbdfSmrg    ptr = (unsigned char *) &out[1];
3586747b715Smrg    for (i = 0; i < sizeof(in->buttons) * 8; i++)
35935c4bbdfSmrg        assert(XIMaskIsSet(in->buttons, i) == XIMaskIsSet(ptr, i));
3606747b715Smrg
3616747b715Smrg    valuators = 0;
36235c4bbdfSmrg    for (i = 0; i < MAX_VALUATORS; i++)
3636747b715Smrg        if (XIMaskIsSet(in->valuators.mask, i))
3646747b715Smrg            valuators++;
3656747b715Smrg
36635c4bbdfSmrg    assert(out->valuators_len >= bytes_to_int32(bits_to_bytes(valuators)));
3676747b715Smrg
3686747b715Smrg    ptr += out->buttons_len * 4;
36935c4bbdfSmrg    values = (FP3232 *) (ptr + out->valuators_len * 4);
3706747b715Smrg    for (i = 0; i < sizeof(in->valuators.mask) * 8 ||
37135c4bbdfSmrg         i < (out->valuators_len * 4) * 8; i++) {
372ed6184dfSmrg        if (i >= MAX_VALUATORS) {
373ed6184dfSmrg            assert(!XIMaskIsSet(in->valuators.mask, i));
374ed6184dfSmrg            assert(!XIMaskIsSet(ptr, i));
375ed6184dfSmrg        }
37635c4bbdfSmrg        else if (i > sizeof(in->valuators.mask) * 8)
37735c4bbdfSmrg            assert(!XIMaskIsSet(ptr, i));
3786747b715Smrg        else if (i > out->valuators_len * 4 * 8)
37935c4bbdfSmrg            assert(!XIMaskIsSet(in->valuators.mask, i));
3806747b715Smrg        else {
38135c4bbdfSmrg            assert(XIMaskIsSet(in->valuators.mask, i) == XIMaskIsSet(ptr, i));
3826747b715Smrg
38335c4bbdfSmrg            if (XIMaskIsSet(ptr, i)) {
3846747b715Smrg                FP3232 vi, vo;
3856747b715Smrg
38635c4bbdfSmrg                vi = double_to_fp3232(in->valuators.data[i]);
3876747b715Smrg                vo = *values;
3886747b715Smrg
38935c4bbdfSmrg                if (swap) {
39035c4bbdfSmrg                    swapl(&vo.integral);
39135c4bbdfSmrg                    swapl(&vo.frac);
3926747b715Smrg                }
3936747b715Smrg
39435c4bbdfSmrg                assert(vi.integral == vo.integral);
39535c4bbdfSmrg                assert(vi.frac == vo.frac);
3966747b715Smrg                values++;
3976747b715Smrg            }
3986747b715Smrg        }
3996747b715Smrg    }
4006747b715Smrg}
4016747b715Smrg
40235c4bbdfSmrgstatic void
40335c4bbdfSmrgtest_XIDeviceEvent(DeviceEvent *in)
4046747b715Smrg{
4056747b715Smrg    xXIDeviceEvent *out, *swapped;
4066747b715Smrg    int rc;
4076747b715Smrg
40835c4bbdfSmrg    rc = EventToXI2((InternalEvent *) in, (xEvent **) &out);
40935c4bbdfSmrg    assert(rc == Success);
4106747b715Smrg
4116747b715Smrg    test_values_XIDeviceEvent(in, out, FALSE);
4126747b715Smrg
4136747b715Smrg    swapped = calloc(1, sizeof(xEvent) + out->length * 4);
41435c4bbdfSmrg    XI2EventSwap((xGenericEvent *) out, (xGenericEvent *) swapped);
4156747b715Smrg    test_values_XIDeviceEvent(in, swapped, TRUE);
4166747b715Smrg
4176747b715Smrg    free(out);
4186747b715Smrg    free(swapped);
4196747b715Smrg}
4206747b715Smrg
42135c4bbdfSmrgstatic void
42235c4bbdfSmrgtest_convert_XIDeviceEvent(void)
4236747b715Smrg{
4246747b715Smrg    DeviceEvent in;
4256747b715Smrg    int i;
4266747b715Smrg
4276747b715Smrg    memset(&in, 0, sizeof(in));
4286747b715Smrg
4296747b715Smrg    in.header = ET_Internal;
4306747b715Smrg    in.type = ET_Motion;
4316747b715Smrg    in.length = sizeof(DeviceEvent);
43235c4bbdfSmrg    in.time = 0;
43335c4bbdfSmrg    in.deviceid = 1;
43435c4bbdfSmrg    in.sourceid = 2;
43535c4bbdfSmrg    in.root = 3;
43635c4bbdfSmrg    in.root_x = 4;
43735c4bbdfSmrg    in.root_x_frac = 5;
43835c4bbdfSmrg    in.root_y = 6;
43935c4bbdfSmrg    in.root_y_frac = 7;
44035c4bbdfSmrg    in.detail.button = 8;
44135c4bbdfSmrg    in.mods.base = 9;
44235c4bbdfSmrg    in.mods.latched = 10;
44335c4bbdfSmrg    in.mods.locked = 11;
44435c4bbdfSmrg    in.mods.effective = 11;
44535c4bbdfSmrg    in.group.base = 12;
44635c4bbdfSmrg    in.group.latched = 13;
44735c4bbdfSmrg    in.group.locked = 14;
44835c4bbdfSmrg    in.group.effective = 15;
44935c4bbdfSmrg
45035c4bbdfSmrg    test_XIDeviceEvent(&in);
45135c4bbdfSmrg
4526747b715Smrg    /* 32 bit */
4536747b715Smrg    in.detail.button = 1L;
4546747b715Smrg    test_XIDeviceEvent(&in);
4556747b715Smrg    in.detail.button = 1L << 8;
4566747b715Smrg    test_XIDeviceEvent(&in);
4576747b715Smrg    in.detail.button = 1L << 16;
4586747b715Smrg    test_XIDeviceEvent(&in);
4596747b715Smrg    in.detail.button = 1L << 24;
4606747b715Smrg    test_XIDeviceEvent(&in);
4616747b715Smrg    in.detail.button = ~0L;
4626747b715Smrg    test_XIDeviceEvent(&in);
4636747b715Smrg
4646747b715Smrg    /* 32 bit */
4656747b715Smrg    in.time = 1L;
4666747b715Smrg    test_XIDeviceEvent(&in);
4676747b715Smrg    in.time = 1L << 8;
4686747b715Smrg    test_XIDeviceEvent(&in);
4696747b715Smrg    in.time = 1L << 16;
4706747b715Smrg    test_XIDeviceEvent(&in);
4716747b715Smrg    in.time = 1L << 24;
4726747b715Smrg    test_XIDeviceEvent(&in);
4736747b715Smrg    in.time = ~0L;
4746747b715Smrg    test_XIDeviceEvent(&in);
4756747b715Smrg
4766747b715Smrg    /* 16 bit */
4776747b715Smrg    in.deviceid = 1;
4786747b715Smrg    test_XIDeviceEvent(&in);
4796747b715Smrg    in.deviceid = 1 << 8;
4806747b715Smrg    test_XIDeviceEvent(&in);
4816747b715Smrg    in.deviceid = ~0 & 0xFF;
4826747b715Smrg    test_XIDeviceEvent(&in);
4836747b715Smrg
4846747b715Smrg    /* 16 bit */
4856747b715Smrg    in.sourceid = 1;
4866747b715Smrg    test_XIDeviceEvent(&in);
4876747b715Smrg    in.deviceid = 1 << 8;
4886747b715Smrg    test_XIDeviceEvent(&in);
4896747b715Smrg    in.deviceid = ~0 & 0xFF;
4906747b715Smrg    test_XIDeviceEvent(&in);
4916747b715Smrg
4926747b715Smrg    /* 32 bit */
4936747b715Smrg    in.root = 1L;
4946747b715Smrg    test_XIDeviceEvent(&in);
4956747b715Smrg    in.root = 1L << 8;
4966747b715Smrg    test_XIDeviceEvent(&in);
4976747b715Smrg    in.root = 1L << 16;
4986747b715Smrg    test_XIDeviceEvent(&in);
4996747b715Smrg    in.root = 1L << 24;
5006747b715Smrg    test_XIDeviceEvent(&in);
5016747b715Smrg    in.root = ~0L;
5026747b715Smrg    test_XIDeviceEvent(&in);
5036747b715Smrg
5046747b715Smrg    /* 16 bit */
5056747b715Smrg    in.root_x = 1;
5066747b715Smrg    test_XIDeviceEvent(&in);
5076747b715Smrg    in.root_x = 1 << 8;
5086747b715Smrg    test_XIDeviceEvent(&in);
5096747b715Smrg    in.root_x = ~0 & 0xFF;
5106747b715Smrg    test_XIDeviceEvent(&in);
5116747b715Smrg
5126747b715Smrg    in.root_x_frac = 1;
5136747b715Smrg    test_XIDeviceEvent(&in);
5146747b715Smrg    in.root_x_frac = 1 << 8;
5156747b715Smrg    test_XIDeviceEvent(&in);
5166747b715Smrg    in.root_x_frac = ~0 & 0xFF;
5176747b715Smrg    test_XIDeviceEvent(&in);
5186747b715Smrg
5196747b715Smrg    in.root_y = 1;
5206747b715Smrg    test_XIDeviceEvent(&in);
5216747b715Smrg    in.root_y = 1 << 8;
5226747b715Smrg    test_XIDeviceEvent(&in);
5236747b715Smrg    in.root_y = ~0 & 0xFF;
5246747b715Smrg    test_XIDeviceEvent(&in);
5256747b715Smrg
5266747b715Smrg    in.root_y_frac = 1;
5276747b715Smrg    test_XIDeviceEvent(&in);
5286747b715Smrg    in.root_y_frac = 1 << 8;
5296747b715Smrg    test_XIDeviceEvent(&in);
5306747b715Smrg    in.root_y_frac = ~0 & 0xFF;
5316747b715Smrg    test_XIDeviceEvent(&in);
5326747b715Smrg
5336747b715Smrg    /* 32 bit */
5346747b715Smrg    in.mods.base = 1L;
5356747b715Smrg    test_XIDeviceEvent(&in);
5366747b715Smrg    in.mods.base = 1L << 8;
5376747b715Smrg    test_XIDeviceEvent(&in);
5386747b715Smrg    in.mods.base = 1L << 16;
5396747b715Smrg    test_XIDeviceEvent(&in);
5406747b715Smrg    in.mods.base = 1L << 24;
5416747b715Smrg    test_XIDeviceEvent(&in);
5426747b715Smrg    in.mods.base = ~0L;
5436747b715Smrg    test_XIDeviceEvent(&in);
5446747b715Smrg
5456747b715Smrg    in.mods.latched = 1L;
5466747b715Smrg    test_XIDeviceEvent(&in);
5476747b715Smrg    in.mods.latched = 1L << 8;
5486747b715Smrg    test_XIDeviceEvent(&in);
5496747b715Smrg    in.mods.latched = 1L << 16;
5506747b715Smrg    test_XIDeviceEvent(&in);
5516747b715Smrg    in.mods.latched = 1L << 24;
5526747b715Smrg    test_XIDeviceEvent(&in);
5536747b715Smrg    in.mods.latched = ~0L;
5546747b715Smrg    test_XIDeviceEvent(&in);
5556747b715Smrg
5566747b715Smrg    in.mods.locked = 1L;
5576747b715Smrg    test_XIDeviceEvent(&in);
5586747b715Smrg    in.mods.locked = 1L << 8;
5596747b715Smrg    test_XIDeviceEvent(&in);
5606747b715Smrg    in.mods.locked = 1L << 16;
5616747b715Smrg    test_XIDeviceEvent(&in);
5626747b715Smrg    in.mods.locked = 1L << 24;
5636747b715Smrg    test_XIDeviceEvent(&in);
5646747b715Smrg    in.mods.locked = ~0L;
5656747b715Smrg    test_XIDeviceEvent(&in);
5666747b715Smrg
5676747b715Smrg    in.mods.effective = 1L;
5686747b715Smrg    test_XIDeviceEvent(&in);
5696747b715Smrg    in.mods.effective = 1L << 8;
5706747b715Smrg    test_XIDeviceEvent(&in);
5716747b715Smrg    in.mods.effective = 1L << 16;
5726747b715Smrg    test_XIDeviceEvent(&in);
5736747b715Smrg    in.mods.effective = 1L << 24;
5746747b715Smrg    test_XIDeviceEvent(&in);
5756747b715Smrg    in.mods.effective = ~0L;
5766747b715Smrg    test_XIDeviceEvent(&in);
5776747b715Smrg
5786747b715Smrg    /* 8 bit */
5796747b715Smrg    in.group.base = 1;
5806747b715Smrg    test_XIDeviceEvent(&in);
5816747b715Smrg    in.group.base = ~0 & 0xFF;
5826747b715Smrg    test_XIDeviceEvent(&in);
5836747b715Smrg
5846747b715Smrg    in.group.latched = 1;
5856747b715Smrg    test_XIDeviceEvent(&in);
5866747b715Smrg    in.group.latched = ~0 & 0xFF;
5876747b715Smrg    test_XIDeviceEvent(&in);
5886747b715Smrg
5896747b715Smrg    in.group.locked = 1;
5906747b715Smrg    test_XIDeviceEvent(&in);
5916747b715Smrg    in.group.locked = ~0 & 0xFF;
5926747b715Smrg    test_XIDeviceEvent(&in);
5936747b715Smrg
5946747b715Smrg    in.mods.effective = 1;
5956747b715Smrg    test_XIDeviceEvent(&in);
5966747b715Smrg    in.mods.effective = ~0 & 0xFF;
5976747b715Smrg    test_XIDeviceEvent(&in);
5986747b715Smrg
59935c4bbdfSmrg    for (i = 0; i < sizeof(in.buttons) * 8; i++) {
6006747b715Smrg        XISetMask(in.buttons, i);
6016747b715Smrg        test_XIDeviceEvent(&in);
6026747b715Smrg        XIClearMask(in.buttons, i);
6036747b715Smrg    }
6046747b715Smrg
60535c4bbdfSmrg    for (i = 0; i < sizeof(in.buttons) * 8; i++) {
6066747b715Smrg        XISetMask(in.buttons, i);
6076747b715Smrg        test_XIDeviceEvent(&in);
6086747b715Smrg    }
6096747b715Smrg
61035c4bbdfSmrg    for (i = 0; i < MAX_VALUATORS; i++) {
6116747b715Smrg        XISetMask(in.valuators.mask, i);
6126747b715Smrg        test_XIDeviceEvent(&in);
6136747b715Smrg        XIClearMask(in.valuators.mask, i);
6146747b715Smrg    }
6156747b715Smrg
61635c4bbdfSmrg    for (i = 0; i < MAX_VALUATORS; i++) {
6176747b715Smrg        XISetMask(in.valuators.mask, i);
6186747b715Smrg
61935c4bbdfSmrg        in.valuators.data[i] = i + (i * 0.0020);
6206747b715Smrg        test_XIDeviceEvent(&in);
6216747b715Smrg        XIClearMask(in.valuators.mask, i);
6226747b715Smrg    }
6236747b715Smrg
62435c4bbdfSmrg    for (i = 0; i < MAX_VALUATORS; i++) {
6256747b715Smrg        XISetMask(in.valuators.mask, i);
6266747b715Smrg        test_XIDeviceEvent(&in);
6276747b715Smrg    }
6286747b715Smrg}
6296747b715Smrg
63035c4bbdfSmrgstatic void
63135c4bbdfSmrgtest_values_XIDeviceChangedEvent(DeviceChangedEvent *in,
63235c4bbdfSmrg                                 xXIDeviceChangedEvent * out, BOOL swap)
6336747b715Smrg{
6346747b715Smrg    int i, j;
6356747b715Smrg    unsigned char *ptr;
6366747b715Smrg
63735c4bbdfSmrg    if (swap) {
63835c4bbdfSmrg        swaps(&out->sequenceNumber);
63935c4bbdfSmrg        swapl(&out->length);
64035c4bbdfSmrg        swaps(&out->evtype);
64135c4bbdfSmrg        swaps(&out->deviceid);
64235c4bbdfSmrg        swaps(&out->sourceid);
64335c4bbdfSmrg        swapl(&out->time);
64435c4bbdfSmrg        swaps(&out->num_classes);
6456747b715Smrg    }
6466747b715Smrg
64735c4bbdfSmrg    assert(out->type == GenericEvent);
64835c4bbdfSmrg    assert(out->extension == 0);        /* IReqCode defaults to 0 */
64935c4bbdfSmrg    assert(out->evtype == GetXI2Type(in->type));
65035c4bbdfSmrg    assert(out->time == in->time);
65135c4bbdfSmrg    assert(out->deviceid == in->deviceid);
65235c4bbdfSmrg    assert(out->sourceid == in->sourceid);
65335c4bbdfSmrg
65435c4bbdfSmrg    ptr = (unsigned char *) &out[1];
65535c4bbdfSmrg    for (i = 0; i < out->num_classes; i++) {
65635c4bbdfSmrg        xXIAnyInfo *any = (xXIAnyInfo *) ptr;
65735c4bbdfSmrg
65835c4bbdfSmrg        if (swap) {
65935c4bbdfSmrg            swaps(&any->length);
66035c4bbdfSmrg            swaps(&any->type);
66135c4bbdfSmrg            swaps(&any->sourceid);
66235c4bbdfSmrg        }
66335c4bbdfSmrg
66435c4bbdfSmrg        switch (any->type) {
66535c4bbdfSmrg        case XIButtonClass:
66635c4bbdfSmrg        {
66735c4bbdfSmrg            xXIButtonInfo *b = (xXIButtonInfo *) any;
66835c4bbdfSmrg            Atom *names;
6696747b715Smrg
67035c4bbdfSmrg            if (swap) {
67135c4bbdfSmrg                swaps(&b->num_buttons);
67235c4bbdfSmrg            }
6736747b715Smrg
67435c4bbdfSmrg            assert(b->length ==
67535c4bbdfSmrg                   bytes_to_int32(sizeof(xXIButtonInfo)) +
67635c4bbdfSmrg                   bytes_to_int32(bits_to_bytes(b->num_buttons)) +
67735c4bbdfSmrg                   b->num_buttons);
67835c4bbdfSmrg            assert(b->num_buttons == in->buttons.num_buttons);
67935c4bbdfSmrg
68035c4bbdfSmrg            names = (Atom *) ((char *) &b[1] +
68135c4bbdfSmrg                              pad_to_int32(bits_to_bytes(b->num_buttons)));
68235c4bbdfSmrg            for (j = 0; j < b->num_buttons; j++) {
68335c4bbdfSmrg                if (swap) {
68435c4bbdfSmrg                    swapl(&names[j]);
68535c4bbdfSmrg                }
68635c4bbdfSmrg                assert(names[j] == in->buttons.names[j]);
68735c4bbdfSmrg            }
68835c4bbdfSmrg        }
68935c4bbdfSmrg            break;
69035c4bbdfSmrg        case XIKeyClass:
6916747b715Smrg        {
69235c4bbdfSmrg            xXIKeyInfo *k = (xXIKeyInfo *) any;
69335c4bbdfSmrg            uint32_t *kc;
69435c4bbdfSmrg
69535c4bbdfSmrg            if (swap) {
69635c4bbdfSmrg                swaps(&k->num_keycodes);
69735c4bbdfSmrg            }
69835c4bbdfSmrg
69935c4bbdfSmrg            assert(k->length ==
70035c4bbdfSmrg                   bytes_to_int32(sizeof(xXIKeyInfo)) + k->num_keycodes);
70135c4bbdfSmrg            assert(k->num_keycodes == in->keys.max_keycode -
70235c4bbdfSmrg                   in->keys.min_keycode + 1);
70335c4bbdfSmrg
70435c4bbdfSmrg            kc = (uint32_t *) &k[1];
70535c4bbdfSmrg            for (j = 0; j < k->num_keycodes; j++) {
70635c4bbdfSmrg                if (swap) {
70735c4bbdfSmrg                    swapl(&kc[j]);
70835c4bbdfSmrg                }
70935c4bbdfSmrg                assert(kc[j] >= in->keys.min_keycode);
71035c4bbdfSmrg                assert(kc[j] <= in->keys.max_keycode);
71135c4bbdfSmrg            }
7126747b715Smrg        }
71335c4bbdfSmrg            break;
71435c4bbdfSmrg        case XIValuatorClass:
71535c4bbdfSmrg        {
71635c4bbdfSmrg            xXIValuatorInfo *v = (xXIValuatorInfo *) any;
7176747b715Smrg
71835c4bbdfSmrg            assert(v->length == bytes_to_int32(sizeof(xXIValuatorInfo)));
71935c4bbdfSmrg
72035c4bbdfSmrg        }
72135c4bbdfSmrg            break;
72235c4bbdfSmrg        case XIScrollClass:
7236747b715Smrg        {
72435c4bbdfSmrg            xXIScrollInfo *s = (xXIScrollInfo *) any;
7256747b715Smrg
72635c4bbdfSmrg            assert(s->length == bytes_to_int32(sizeof(xXIScrollInfo)));
72735c4bbdfSmrg
72835c4bbdfSmrg            assert(s->sourceid == in->sourceid);
72935c4bbdfSmrg            assert(s->number < in->num_valuators);
73035c4bbdfSmrg            switch (s->type) {
73135c4bbdfSmrg            case XIScrollTypeVertical:
73235c4bbdfSmrg                assert(in->valuators[s->number].scroll.type ==
73335c4bbdfSmrg                       SCROLL_TYPE_VERTICAL);
7346747b715Smrg                break;
73535c4bbdfSmrg            case XIScrollTypeHorizontal:
73635c4bbdfSmrg                assert(in->valuators[s->number].scroll.type ==
73735c4bbdfSmrg                       SCROLL_TYPE_HORIZONTAL);
7386747b715Smrg                break;
73935c4bbdfSmrg            }
74035c4bbdfSmrg            if (s->flags & XIScrollFlagPreferred)
74135c4bbdfSmrg                assert(in->valuators[s->number].scroll.
74235c4bbdfSmrg                       flags & SCROLL_FLAG_PREFERRED);
74335c4bbdfSmrg        }
74435c4bbdfSmrg        default:
74535c4bbdfSmrg            printf("Invalid class type.\n\n");
74635c4bbdfSmrg            assert(1);
74735c4bbdfSmrg            break;
7486747b715Smrg        }
7496747b715Smrg
7506747b715Smrg        ptr += any->length * 4;
7516747b715Smrg    }
7526747b715Smrg
7536747b715Smrg}
7546747b715Smrg
75535c4bbdfSmrgstatic void
75635c4bbdfSmrgtest_XIDeviceChangedEvent(DeviceChangedEvent *in)
7576747b715Smrg{
7586747b715Smrg    xXIDeviceChangedEvent *out, *swapped;
7596747b715Smrg    int rc;
7606747b715Smrg
76135c4bbdfSmrg    rc = EventToXI2((InternalEvent *) in, (xEvent **) &out);
76235c4bbdfSmrg    assert(rc == Success);
7636747b715Smrg
7646747b715Smrg    test_values_XIDeviceChangedEvent(in, out, FALSE);
7656747b715Smrg
7666747b715Smrg    swapped = calloc(1, sizeof(xEvent) + out->length * 4);
76735c4bbdfSmrg    XI2EventSwap((xGenericEvent *) out, (xGenericEvent *) swapped);
7686747b715Smrg    test_values_XIDeviceChangedEvent(in, swapped, TRUE);
7696747b715Smrg
7706747b715Smrg    free(out);
7716747b715Smrg    free(swapped);
7726747b715Smrg}
7736747b715Smrg
77435c4bbdfSmrgstatic void
77535c4bbdfSmrgtest_convert_XIDeviceChangedEvent(void)
7766747b715Smrg{
7776747b715Smrg    DeviceChangedEvent in;
7786747b715Smrg    int i;
7796747b715Smrg
7806747b715Smrg    memset(&in, 0, sizeof(in));
7816747b715Smrg    in.header = ET_Internal;
7826747b715Smrg    in.type = ET_DeviceChanged;
7836747b715Smrg    in.length = sizeof(DeviceChangedEvent);
78435c4bbdfSmrg    in.time = 0;
78535c4bbdfSmrg    in.deviceid = 1;
78635c4bbdfSmrg    in.sourceid = 2;
78735c4bbdfSmrg    in.masterid = 3;
78835c4bbdfSmrg    in.num_valuators = 4;
78935c4bbdfSmrg    in.flags =
79035c4bbdfSmrg        DEVCHANGE_SLAVE_SWITCH | DEVCHANGE_POINTER_EVENT |
79135c4bbdfSmrg        DEVCHANGE_KEYBOARD_EVENT;
7926747b715Smrg
7936747b715Smrg    for (i = 0; i < MAX_BUTTONS; i++)
7946747b715Smrg        in.buttons.names[i] = i + 10;
7956747b715Smrg
7966747b715Smrg    in.keys.min_keycode = 8;
7976747b715Smrg    in.keys.max_keycode = 255;
7986747b715Smrg
7996747b715Smrg    test_XIDeviceChangedEvent(&in);
8006747b715Smrg
8016747b715Smrg    in.time = 1L;
8026747b715Smrg    test_XIDeviceChangedEvent(&in);
8036747b715Smrg    in.time = 1L << 8;
8046747b715Smrg    test_XIDeviceChangedEvent(&in);
8056747b715Smrg    in.time = 1L << 16;
8066747b715Smrg    test_XIDeviceChangedEvent(&in);
8076747b715Smrg    in.time = 1L << 24;
8086747b715Smrg    test_XIDeviceChangedEvent(&in);
8096747b715Smrg    in.time = ~0L;
8106747b715Smrg    test_XIDeviceChangedEvent(&in);
8116747b715Smrg
8126747b715Smrg    in.deviceid = 1L;
8136747b715Smrg    test_XIDeviceChangedEvent(&in);
8146747b715Smrg    in.deviceid = 1L << 8;
8156747b715Smrg    test_XIDeviceChangedEvent(&in);
8166747b715Smrg    in.deviceid = ~0 & 0xFFFF;
8176747b715Smrg    test_XIDeviceChangedEvent(&in);
8186747b715Smrg
8196747b715Smrg    in.sourceid = 1L;
8206747b715Smrg    test_XIDeviceChangedEvent(&in);
8216747b715Smrg    in.sourceid = 1L << 8;
8226747b715Smrg    test_XIDeviceChangedEvent(&in);
8236747b715Smrg    in.sourceid = ~0 & 0xFFFF;
8246747b715Smrg    test_XIDeviceChangedEvent(&in);
8256747b715Smrg
8266747b715Smrg    in.masterid = 1L;
8276747b715Smrg    test_XIDeviceChangedEvent(&in);
8286747b715Smrg    in.masterid = 1L << 8;
8296747b715Smrg    test_XIDeviceChangedEvent(&in);
8306747b715Smrg    in.masterid = ~0 & 0xFFFF;
8316747b715Smrg    test_XIDeviceChangedEvent(&in);
8326747b715Smrg
8336747b715Smrg    in.buttons.num_buttons = 0;
8346747b715Smrg    test_XIDeviceChangedEvent(&in);
8356747b715Smrg
8366747b715Smrg    in.buttons.num_buttons = 1;
8376747b715Smrg    test_XIDeviceChangedEvent(&in);
8386747b715Smrg
8396747b715Smrg    in.buttons.num_buttons = MAX_BUTTONS;
8406747b715Smrg    test_XIDeviceChangedEvent(&in);
8416747b715Smrg
8426747b715Smrg    in.keys.min_keycode = 0;
8436747b715Smrg    in.keys.max_keycode = 0;
8446747b715Smrg    test_XIDeviceChangedEvent(&in);
8456747b715Smrg
8466747b715Smrg    in.keys.max_keycode = 1 << 8;
8476747b715Smrg    test_XIDeviceChangedEvent(&in);
8486747b715Smrg
84935c4bbdfSmrg    in.keys.max_keycode = 0xFFFC;       /* highest range, above that the length
85035c4bbdfSmrg                                           field gives up */
8516747b715Smrg    test_XIDeviceChangedEvent(&in);
8526747b715Smrg
8536747b715Smrg    in.keys.min_keycode = 1 << 8;
8546747b715Smrg    in.keys.max_keycode = 1 << 8;
8556747b715Smrg    test_XIDeviceChangedEvent(&in);
8566747b715Smrg
8576747b715Smrg    in.keys.min_keycode = 1 << 8;
8586747b715Smrg    in.keys.max_keycode = 0;
8596747b715Smrg    test_XIDeviceChangedEvent(&in);
8606747b715Smrg
8616747b715Smrg    in.num_valuators = 0;
8626747b715Smrg    test_XIDeviceChangedEvent(&in);
8636747b715Smrg
8646747b715Smrg    in.num_valuators = 1;
8656747b715Smrg    test_XIDeviceChangedEvent(&in);
8666747b715Smrg
8676747b715Smrg    in.num_valuators = MAX_VALUATORS;
8686747b715Smrg    test_XIDeviceChangedEvent(&in);
8696747b715Smrg
87035c4bbdfSmrg    for (i = 0; i < MAX_VALUATORS; i++) {
8716747b715Smrg        in.valuators[i].min = 0;
8726747b715Smrg        in.valuators[i].max = 0;
8736747b715Smrg        test_XIDeviceChangedEvent(&in);
8746747b715Smrg
8756747b715Smrg        in.valuators[i].max = 1 << 8;
8766747b715Smrg        test_XIDeviceChangedEvent(&in);
8776747b715Smrg        in.valuators[i].max = 1 << 16;
8786747b715Smrg        test_XIDeviceChangedEvent(&in);
8796747b715Smrg        in.valuators[i].max = 1 << 24;
8806747b715Smrg        test_XIDeviceChangedEvent(&in);
8816747b715Smrg        in.valuators[i].max = abs(~0);
8826747b715Smrg        test_XIDeviceChangedEvent(&in);
8836747b715Smrg
8846747b715Smrg        in.valuators[i].resolution = 1 << 8;
8856747b715Smrg        test_XIDeviceChangedEvent(&in);
8866747b715Smrg        in.valuators[i].resolution = 1 << 16;
8876747b715Smrg        test_XIDeviceChangedEvent(&in);
8886747b715Smrg        in.valuators[i].resolution = 1 << 24;
8896747b715Smrg        test_XIDeviceChangedEvent(&in);
8906747b715Smrg        in.valuators[i].resolution = abs(~0);
8916747b715Smrg        test_XIDeviceChangedEvent(&in);
8926747b715Smrg
8936747b715Smrg        in.valuators[i].name = i;
8946747b715Smrg        test_XIDeviceChangedEvent(&in);
8956747b715Smrg
8966747b715Smrg        in.valuators[i].mode = Relative;
8976747b715Smrg        test_XIDeviceChangedEvent(&in);
8986747b715Smrg
8996747b715Smrg        in.valuators[i].mode = Absolute;
9006747b715Smrg        test_XIDeviceChangedEvent(&in);
9016747b715Smrg    }
9026747b715Smrg}
9036747b715Smrg
90435c4bbdfSmrgstatic void
90535c4bbdfSmrgtest_values_XITouchOwnershipEvent(TouchOwnershipEvent *in,
90635c4bbdfSmrg                                  xXITouchOwnershipEvent * out, BOOL swap)
90735c4bbdfSmrg{
90835c4bbdfSmrg    if (swap) {
90935c4bbdfSmrg        swaps(&out->sequenceNumber);
91035c4bbdfSmrg        swapl(&out->length);
91135c4bbdfSmrg        swaps(&out->evtype);
91235c4bbdfSmrg        swaps(&out->deviceid);
91335c4bbdfSmrg        swaps(&out->sourceid);
91435c4bbdfSmrg        swapl(&out->time);
91535c4bbdfSmrg        swapl(&out->touchid);
91635c4bbdfSmrg        swapl(&out->root);
91735c4bbdfSmrg        swapl(&out->event);
91835c4bbdfSmrg        swapl(&out->child);
91935c4bbdfSmrg        swapl(&out->time);
92035c4bbdfSmrg    }
92135c4bbdfSmrg
92235c4bbdfSmrg    assert(out->type == GenericEvent);
92335c4bbdfSmrg    assert(out->extension == 0);        /* IReqCode defaults to 0 */
92435c4bbdfSmrg    assert(out->evtype == GetXI2Type(in->type));
92535c4bbdfSmrg    assert(out->time == in->time);
92635c4bbdfSmrg    assert(out->deviceid == in->deviceid);
92735c4bbdfSmrg    assert(out->sourceid == in->sourceid);
92835c4bbdfSmrg    assert(out->touchid == in->touchid);
92935c4bbdfSmrg    assert(out->flags == in->reason);
93035c4bbdfSmrg}
93135c4bbdfSmrg
93235c4bbdfSmrgstatic void
93335c4bbdfSmrgtest_XITouchOwnershipEvent(TouchOwnershipEvent *in)
93435c4bbdfSmrg{
93535c4bbdfSmrg    xXITouchOwnershipEvent *out, *swapped;
93635c4bbdfSmrg    int rc;
93735c4bbdfSmrg
93835c4bbdfSmrg    rc = EventToXI2((InternalEvent *) in, (xEvent **) &out);
93935c4bbdfSmrg    assert(rc == Success);
94035c4bbdfSmrg
94135c4bbdfSmrg    test_values_XITouchOwnershipEvent(in, out, FALSE);
94235c4bbdfSmrg
94335c4bbdfSmrg    swapped = calloc(1, sizeof(xEvent) + out->length * 4);
94435c4bbdfSmrg    XI2EventSwap((xGenericEvent *) out, (xGenericEvent *) swapped);
94535c4bbdfSmrg    test_values_XITouchOwnershipEvent(in, swapped, TRUE);
94635c4bbdfSmrg    free(out);
94735c4bbdfSmrg    free(swapped);
94835c4bbdfSmrg}
94935c4bbdfSmrg
95035c4bbdfSmrgstatic void
95135c4bbdfSmrgtest_convert_XITouchOwnershipEvent(void)
95235c4bbdfSmrg{
95335c4bbdfSmrg    TouchOwnershipEvent in;
95435c4bbdfSmrg    long i;
95535c4bbdfSmrg
95635c4bbdfSmrg    memset(&in, 0, sizeof(in));
95735c4bbdfSmrg    in.header = ET_Internal;
95835c4bbdfSmrg    in.type = ET_TouchOwnership;
95935c4bbdfSmrg    in.length = sizeof(in);
96035c4bbdfSmrg    in.time = 0;
96135c4bbdfSmrg    in.deviceid = 1;
96235c4bbdfSmrg    in.sourceid = 2;
96335c4bbdfSmrg    in.touchid = 0;
96435c4bbdfSmrg    in.reason = 0;
96535c4bbdfSmrg    in.resource = 0;
96635c4bbdfSmrg    in.flags = 0;
96735c4bbdfSmrg
96835c4bbdfSmrg    test_XITouchOwnershipEvent(&in);
96935c4bbdfSmrg
97035c4bbdfSmrg    in.flags = XIAcceptTouch;
97135c4bbdfSmrg    test_XITouchOwnershipEvent(&in);
97235c4bbdfSmrg
97335c4bbdfSmrg    in.flags = XIRejectTouch;
97435c4bbdfSmrg    test_XITouchOwnershipEvent(&in);
97535c4bbdfSmrg
97635c4bbdfSmrg    for (i = 1; i <= 0xFFFF; i <<= 1) {
97735c4bbdfSmrg        in.deviceid = i;
97835c4bbdfSmrg        test_XITouchOwnershipEvent(&in);
97935c4bbdfSmrg    }
98035c4bbdfSmrg
98135c4bbdfSmrg    for (i = 1; i <= 0xFFFF; i <<= 1) {
98235c4bbdfSmrg        in.sourceid = i;
98335c4bbdfSmrg        test_XITouchOwnershipEvent(&in);
98435c4bbdfSmrg    }
98535c4bbdfSmrg
98635c4bbdfSmrg    for (i = 1;; i <<= 1) {
98735c4bbdfSmrg        in.touchid = i;
98835c4bbdfSmrg        test_XITouchOwnershipEvent(&in);
98935c4bbdfSmrg        if (i == ((long) 1 << 31))
99035c4bbdfSmrg            break;
99135c4bbdfSmrg    }
99235c4bbdfSmrg}
99335c4bbdfSmrg
99435c4bbdfSmrgstatic void
99535c4bbdfSmrgtest_XIBarrierEvent(BarrierEvent *in)
9966747b715Smrg{
99735c4bbdfSmrg    xXIBarrierEvent *out, *swapped;
99835c4bbdfSmrg    int count;
99935c4bbdfSmrg    int rc;
100035c4bbdfSmrg    int eventlen;
100135c4bbdfSmrg    FP3232 value;
100235c4bbdfSmrg
100335c4bbdfSmrg    rc = EventToXI((InternalEvent*)in, (xEvent**)&out, &count);
100435c4bbdfSmrg    assert(rc == BadMatch);
100535c4bbdfSmrg
100635c4bbdfSmrg    rc = EventToCore((InternalEvent*)in, (xEvent**)&out, &count);
100735c4bbdfSmrg    assert(rc == BadMatch);
100835c4bbdfSmrg
100935c4bbdfSmrg    rc = EventToXI2((InternalEvent*)in, (xEvent**)&out);
10106747b715Smrg
101135c4bbdfSmrg    assert(out->type == GenericEvent);
101235c4bbdfSmrg    assert(out->extension == 0); /* IReqCode defaults to 0 */
101335c4bbdfSmrg    assert(out->evtype == GetXI2Type(in->type));
101435c4bbdfSmrg    assert(out->time == in->time);
101535c4bbdfSmrg    assert(out->deviceid == in->deviceid);
101635c4bbdfSmrg    assert(out->sourceid == in->sourceid);
101735c4bbdfSmrg    assert(out->barrier == in->barrierid);
101835c4bbdfSmrg    assert(out->flags == in->flags);
101935c4bbdfSmrg    assert(out->event == in->window);
102035c4bbdfSmrg    assert(out->root == in->root);
102135c4bbdfSmrg    assert(out->dtime == in->dt);
102235c4bbdfSmrg    assert(out->eventid == in->event_id);
102335c4bbdfSmrg    assert(out->root_x == double_to_fp1616(in->root_x));
102435c4bbdfSmrg    assert(out->root_y == double_to_fp1616(in->root_y));
102535c4bbdfSmrg
102635c4bbdfSmrg    value = double_to_fp3232(in->dx);
102735c4bbdfSmrg    assert(out->dx.integral == value.integral);
102835c4bbdfSmrg    assert(out->dx.frac == value.frac);
102935c4bbdfSmrg    value = double_to_fp3232(in->dy);
103035c4bbdfSmrg    assert(out->dy.integral == value.integral);
103135c4bbdfSmrg    assert(out->dy.frac == value.frac);
103235c4bbdfSmrg
103335c4bbdfSmrg    eventlen = sizeof(xEvent) + out->length * 4;
103435c4bbdfSmrg    swapped = calloc(1, eventlen);
103535c4bbdfSmrg    XI2EventSwap((xGenericEvent *) out, (xGenericEvent *) swapped);
103635c4bbdfSmrg
103735c4bbdfSmrg    swaps(&swapped->sequenceNumber);
103835c4bbdfSmrg    swapl(&swapped->length);
103935c4bbdfSmrg    swaps(&swapped->evtype);
104035c4bbdfSmrg    swaps(&swapped->deviceid);
104135c4bbdfSmrg    swapl(&swapped->time);
104235c4bbdfSmrg    swapl(&swapped->eventid);
104335c4bbdfSmrg    swapl(&swapped->root);
104435c4bbdfSmrg    swapl(&swapped->event);
104535c4bbdfSmrg    swapl(&swapped->barrier);
104635c4bbdfSmrg    swapl(&swapped->dtime);
104735c4bbdfSmrg    swaps(&swapped->sourceid);
104835c4bbdfSmrg    swapl(&swapped->root_x);
104935c4bbdfSmrg    swapl(&swapped->root_y);
105035c4bbdfSmrg    swapl(&swapped->dx.integral);
105135c4bbdfSmrg    swapl(&swapped->dx.frac);
105235c4bbdfSmrg    swapl(&swapped->dy.integral);
105335c4bbdfSmrg    swapl(&swapped->dy.frac);
105435c4bbdfSmrg
105535c4bbdfSmrg    assert(memcmp(swapped, out, eventlen) == 0);
10566747b715Smrg
105735c4bbdfSmrg    free(swapped);
105835c4bbdfSmrg    free(out);
105935c4bbdfSmrg}
106035c4bbdfSmrg
106135c4bbdfSmrgstatic void
106235c4bbdfSmrgtest_convert_XIBarrierEvent(void)
106335c4bbdfSmrg{
106435c4bbdfSmrg    BarrierEvent in;
106535c4bbdfSmrg
106635c4bbdfSmrg    memset(&in, 0, sizeof(in));
106735c4bbdfSmrg    in.header = ET_Internal;
106835c4bbdfSmrg    in.type = ET_BarrierHit;
106935c4bbdfSmrg    in.length = sizeof(in);
107035c4bbdfSmrg    in.time = 0;
107135c4bbdfSmrg    in.deviceid = 1;
107235c4bbdfSmrg    in.sourceid = 2;
107335c4bbdfSmrg
107435c4bbdfSmrg    test_XIBarrierEvent(&in);
107535c4bbdfSmrg
107635c4bbdfSmrg    in.deviceid = 1;
107735c4bbdfSmrg    while(in.deviceid & 0xFFFF) {
107835c4bbdfSmrg        test_XIBarrierEvent(&in);
107935c4bbdfSmrg        in.deviceid <<= 1;
108035c4bbdfSmrg    }
108135c4bbdfSmrg    in.deviceid = 0;
108235c4bbdfSmrg
108335c4bbdfSmrg    in.sourceid = 1;
108435c4bbdfSmrg    while(in.sourceid & 0xFFFF) {
108535c4bbdfSmrg        test_XIBarrierEvent(&in);
108635c4bbdfSmrg        in.sourceid <<= 1;
108735c4bbdfSmrg    }
108835c4bbdfSmrg    in.sourceid = 0;
108935c4bbdfSmrg
109035c4bbdfSmrg    in.flags = 1;
109135c4bbdfSmrg    while(in.flags) {
109235c4bbdfSmrg        test_XIBarrierEvent(&in);
109335c4bbdfSmrg        in.flags <<= 1;
109435c4bbdfSmrg    }
109535c4bbdfSmrg
109635c4bbdfSmrg    in.barrierid = 1;
109735c4bbdfSmrg    while(in.barrierid) {
109835c4bbdfSmrg        test_XIBarrierEvent(&in);
109935c4bbdfSmrg        in.barrierid <<= 1;
110035c4bbdfSmrg    }
110135c4bbdfSmrg
110235c4bbdfSmrg    in.dt = 1;
110335c4bbdfSmrg    while(in.dt) {
110435c4bbdfSmrg        test_XIBarrierEvent(&in);
110535c4bbdfSmrg        in.dt <<= 1;
110635c4bbdfSmrg    }
110735c4bbdfSmrg
110835c4bbdfSmrg    in.event_id = 1;
110935c4bbdfSmrg    while(in.event_id) {
111035c4bbdfSmrg        test_XIBarrierEvent(&in);
111135c4bbdfSmrg        in.event_id <<= 1;
111235c4bbdfSmrg    }
111335c4bbdfSmrg
111435c4bbdfSmrg    in.window = 1;
111535c4bbdfSmrg    while(in.window) {
111635c4bbdfSmrg        test_XIBarrierEvent(&in);
111735c4bbdfSmrg        in.window <<= 1;
111835c4bbdfSmrg    }
111935c4bbdfSmrg
112035c4bbdfSmrg    in.root = 1;
112135c4bbdfSmrg    while(in.root) {
112235c4bbdfSmrg        test_XIBarrierEvent(&in);
112335c4bbdfSmrg        in.root <<= 1;
112435c4bbdfSmrg    }
112535c4bbdfSmrg
112635c4bbdfSmrg    /* pseudo-random 16 bit numbers */
112735c4bbdfSmrg    in.root_x = 1;
112835c4bbdfSmrg    test_XIBarrierEvent(&in);
112935c4bbdfSmrg    in.root_x = 1.3;
113035c4bbdfSmrg    test_XIBarrierEvent(&in);
113135c4bbdfSmrg    in.root_x = 264.908;
113235c4bbdfSmrg    test_XIBarrierEvent(&in);
113335c4bbdfSmrg    in.root_x = 35638.292;
113435c4bbdfSmrg    test_XIBarrierEvent(&in);
113535c4bbdfSmrg
113635c4bbdfSmrg    in.root_x = -1;
113735c4bbdfSmrg    test_XIBarrierEvent(&in);
113835c4bbdfSmrg    in.root_x = -1.3;
113935c4bbdfSmrg    test_XIBarrierEvent(&in);
114035c4bbdfSmrg    in.root_x = -264.908;
114135c4bbdfSmrg    test_XIBarrierEvent(&in);
114235c4bbdfSmrg    in.root_x = -35638.292;
114335c4bbdfSmrg    test_XIBarrierEvent(&in);
114435c4bbdfSmrg
114535c4bbdfSmrg    in.root_y = 1;
114635c4bbdfSmrg    test_XIBarrierEvent(&in);
114735c4bbdfSmrg    in.root_y = 1.3;
114835c4bbdfSmrg    test_XIBarrierEvent(&in);
114935c4bbdfSmrg    in.root_y = 264.908;
115035c4bbdfSmrg    test_XIBarrierEvent(&in);
115135c4bbdfSmrg    in.root_y = 35638.292;
115235c4bbdfSmrg    test_XIBarrierEvent(&in);
115335c4bbdfSmrg
115435c4bbdfSmrg    in.root_y = -1;
115535c4bbdfSmrg    test_XIBarrierEvent(&in);
115635c4bbdfSmrg    in.root_y = -1.3;
115735c4bbdfSmrg    test_XIBarrierEvent(&in);
115835c4bbdfSmrg    in.root_y = -264.908;
115935c4bbdfSmrg    test_XIBarrierEvent(&in);
116035c4bbdfSmrg    in.root_y = -35638.292;
116135c4bbdfSmrg    test_XIBarrierEvent(&in);
116235c4bbdfSmrg
116335c4bbdfSmrg    /* equally pseudo-random 32 bit numbers */
116435c4bbdfSmrg    in.dx = 1;
116535c4bbdfSmrg    test_XIBarrierEvent(&in);
116635c4bbdfSmrg    in.dx = 1.3;
116735c4bbdfSmrg    test_XIBarrierEvent(&in);
116835c4bbdfSmrg    in.dx = 264.908;
116935c4bbdfSmrg    test_XIBarrierEvent(&in);
117035c4bbdfSmrg    in.dx = 35638.292;
117135c4bbdfSmrg    test_XIBarrierEvent(&in);
117235c4bbdfSmrg    in.dx = 2947813871.2342;
117335c4bbdfSmrg    test_XIBarrierEvent(&in);
117435c4bbdfSmrg
117535c4bbdfSmrg    in.dx = -1;
117635c4bbdfSmrg    test_XIBarrierEvent(&in);
117735c4bbdfSmrg    in.dx = -1.3;
117835c4bbdfSmrg    test_XIBarrierEvent(&in);
117935c4bbdfSmrg    in.dx = -264.908;
118035c4bbdfSmrg    test_XIBarrierEvent(&in);
118135c4bbdfSmrg    in.dx = -35638.292;
118235c4bbdfSmrg    test_XIBarrierEvent(&in);
118335c4bbdfSmrg    in.dx = -2947813871.2342;
118435c4bbdfSmrg    test_XIBarrierEvent(&in);
118535c4bbdfSmrg
118635c4bbdfSmrg    in.dy = 1;
118735c4bbdfSmrg    test_XIBarrierEvent(&in);
118835c4bbdfSmrg    in.dy = 1.3;
118935c4bbdfSmrg    test_XIBarrierEvent(&in);
119035c4bbdfSmrg    in.dy = 264.908;
119135c4bbdfSmrg    test_XIBarrierEvent(&in);
119235c4bbdfSmrg    in.dy = 35638.292;
119335c4bbdfSmrg    test_XIBarrierEvent(&in);
119435c4bbdfSmrg    in.dy = 2947813871.2342;
119535c4bbdfSmrg    test_XIBarrierEvent(&in);
119635c4bbdfSmrg
119735c4bbdfSmrg    in.dy = -1;
119835c4bbdfSmrg    test_XIBarrierEvent(&in);
119935c4bbdfSmrg    in.dy = -1.3;
120035c4bbdfSmrg    test_XIBarrierEvent(&in);
120135c4bbdfSmrg    in.dy = -264.908;
120235c4bbdfSmrg    test_XIBarrierEvent(&in);
120335c4bbdfSmrg    in.dy = -35638.292;
120435c4bbdfSmrg    test_XIBarrierEvent(&in);
120535c4bbdfSmrg    in.dy = -2947813871.2342;
120635c4bbdfSmrg    test_XIBarrierEvent(&in);
120735c4bbdfSmrg}
120835c4bbdfSmrg
120935c4bbdfSmrgint
12101b5d61b8Smrgprotocol_eventconvert_test(void)
121135c4bbdfSmrg{
121235c4bbdfSmrg    test_convert_XIRawEvent();
121335c4bbdfSmrg    test_convert_XIFocusEvent();
121435c4bbdfSmrg    test_convert_XIDeviceEvent();
121535c4bbdfSmrg    test_convert_XIDeviceChangedEvent();
121635c4bbdfSmrg    test_convert_XITouchOwnershipEvent();
121735c4bbdfSmrg    test_convert_XIBarrierEvent();
121835c4bbdfSmrg
121935c4bbdfSmrg    return 0;
12206747b715Smrg}
1221