From b0be6a88c8fecdf15176f642c0799bff99930e0d Mon Sep 17 00:00:00 2001 From: marha Date: Fri, 21 Jan 2011 08:10:52 +0000 Subject: xserver mesa xkbcomp libxcb git update 21 jan 2011 --- xorg-server/test/input.c | 2172 ++++++++++++------------ xorg-server/test/xi2/protocol-eventconvert.c | 1824 ++++++++++---------- xorg-server/test/xi2/protocol-xiselectevents.c | 18 +- 3 files changed, 2009 insertions(+), 2005 deletions(-) (limited to 'xorg-server/test') diff --git a/xorg-server/test/input.c b/xorg-server/test/input.c index 877e9776b..879e14f2f 100644 --- a/xorg-server/test/input.c +++ b/xorg-server/test/input.c @@ -1,1089 +1,1083 @@ -/** - * Copyright © 2009 Red Hat, Inc. - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice (including the next - * paragraph) shall be included in all copies or substantial portions of the - * Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ - -#ifdef HAVE_DIX_CONFIG_H -#include -#endif - -#include -#include -#include "misc.h" -#include "resource.h" -#include -#include -#include -#include "windowstr.h" -#include "inputstr.h" -#include "eventconvert.h" -#include "exevents.h" -#include "dixgrabs.h" -#include "eventstr.h" -#include "inpututils.h" -#include - -/** - * Init a device with axes. - * Verify values set on the device. - * - * Result: All axes set to default values (usually 0). - */ -static void dix_init_valuators(void) -{ - DeviceIntRec dev; - ValuatorClassPtr val; - const int num_axes = 2; - int i; - Atom atoms[MAX_VALUATORS] = { 0 }; - - - memset(&dev, 0, sizeof(DeviceIntRec)); - dev.type = MASTER_POINTER; /* claim it's a master to stop ptracccel */ - - g_assert(InitValuatorClassDeviceStruct(NULL, 0, atoms, 0, 0) == FALSE); - g_assert(InitValuatorClassDeviceStruct(&dev, num_axes, atoms, 0, Absolute)); - - val = dev.valuator; - g_assert(val); - g_assert(val->numAxes == num_axes); - g_assert(val->numMotionEvents == 0); - g_assert(val->axisVal); - - for (i = 0; i < num_axes; i++) - { - g_assert(val->axisVal[i] == 0); - g_assert(val->axes->min_value == NO_AXIS_LIMITS); - g_assert(val->axes->max_value == NO_AXIS_LIMITS); - g_assert(val->axes->mode == Absolute); - } - - g_assert(dev.last.numValuators == num_axes); -} - -/* just check the known success cases, and that error cases set the client's - * error value correctly. */ -static void dix_check_grab_values(void) -{ - ClientRec client; - GrabParameters param; - int rc; - - memset(&client, 0, sizeof(client)); - - param.grabtype = GRABTYPE_CORE; - param.this_device_mode = GrabModeSync; - param.other_devices_mode = GrabModeSync; - param.modifiers = AnyModifier; - param.ownerEvents = FALSE; - - rc = CheckGrabValues(&client, ¶m); - g_assert(rc == Success); - - param.this_device_mode = GrabModeAsync; - rc = CheckGrabValues(&client, ¶m); - g_assert(rc == Success); - - param.this_device_mode = GrabModeAsync + 1; - rc = CheckGrabValues(&client, ¶m); - g_assert(rc == BadValue); - g_assert(client.errorValue == param.this_device_mode); - g_assert(client.errorValue == GrabModeAsync + 1); - - param.this_device_mode = GrabModeSync; - param.other_devices_mode = GrabModeAsync; - rc = CheckGrabValues(&client, ¶m); - g_assert(rc == Success); - - param.other_devices_mode = GrabModeAsync + 1; - rc = CheckGrabValues(&client, ¶m); - g_assert(rc == BadValue); - g_assert(client.errorValue == param.other_devices_mode); - g_assert(client.errorValue == GrabModeAsync + 1); - - param.other_devices_mode = GrabModeSync; - - param.modifiers = 1 << 13; - rc = CheckGrabValues(&client, ¶m); - g_assert(rc == BadValue); - g_assert(client.errorValue == param.modifiers); - g_assert(client.errorValue == (1 << 13)); - - - param.modifiers = AnyModifier; - param.ownerEvents = TRUE; - rc = CheckGrabValues(&client, ¶m); - g_assert(rc == Success); - - param.ownerEvents = 3; - rc = CheckGrabValues(&client, ¶m); - g_assert(rc == BadValue); - g_assert(client.errorValue == param.ownerEvents); - g_assert(client.errorValue == 3); -} - - -/** - * Convert various internal events to the matching core event and verify the - * parameters. - */ -static void dix_event_to_core(int type) -{ - DeviceEvent ev; - xEvent core; - int time; - int x, y; - int rc; - int state; - int detail; - const int ROOT_WINDOW_ID = 0x100; - - /* EventToCore memsets the event to 0 */ -#define test_event() \ - g_assert(rc == Success); \ - g_assert(core.u.u.type == type); \ - g_assert(core.u.u.detail == detail); \ - g_assert(core.u.keyButtonPointer.time == time); \ - g_assert(core.u.keyButtonPointer.rootX == x); \ - g_assert(core.u.keyButtonPointer.rootY == y); \ - g_assert(core.u.keyButtonPointer.state == state); \ - g_assert(core.u.keyButtonPointer.eventX == 0); \ - g_assert(core.u.keyButtonPointer.eventY == 0); \ - g_assert(core.u.keyButtonPointer.root == ROOT_WINDOW_ID); \ - g_assert(core.u.keyButtonPointer.event == 0); \ - g_assert(core.u.keyButtonPointer.child == 0); \ - g_assert(core.u.keyButtonPointer.sameScreen == FALSE); - - x = 0; - y = 0; - time = 12345; - state = 0; - detail = 0; - - ev.header = 0xFF; - ev.length = sizeof(DeviceEvent); - ev.time = time; - ev.root_y = x; - ev.root_x = y; - SetBit(ev.valuators.mask, 0); - SetBit(ev.valuators.mask, 1); - ev.root = ROOT_WINDOW_ID; - ev.corestate = state; - ev.detail.key = detail; - - ev.type = type; - ev.detail.key = 0; - rc = EventToCore((InternalEvent*)&ev, &core); - test_event(); - - x = 1; - y = 2; - ev.root_x = x; - ev.root_y = y; - rc = EventToCore((InternalEvent*)&ev, &core); - test_event(); - - x = 0x7FFF; - y = 0x7FFF; - ev.root_x = x; - ev.root_y = y; - rc = EventToCore((InternalEvent*)&ev, &core); - test_event(); - - x = 0x8000; /* too high */ - y = 0x8000; /* too high */ - ev.root_x = x; - ev.root_y = y; - rc = EventToCore((InternalEvent*)&ev, &core); - g_assert(core.u.keyButtonPointer.rootX != x); - g_assert(core.u.keyButtonPointer.rootY != y); - - x = 0x7FFF; - y = 0x7FFF; - ev.root_x = x; - ev.root_y = y; - time = 0; - ev.time = time; - rc = EventToCore((InternalEvent*)&ev, &core); - test_event(); - - detail = 1; - ev.detail.key = detail; - rc = EventToCore((InternalEvent*)&ev, &core); - test_event(); - - detail = 0xFF; /* highest value */ - ev.detail.key = detail; - rc = EventToCore((InternalEvent*)&ev, &core); - test_event(); - - detail = 0xFFF; /* too big */ - ev.detail.key = detail; - rc = EventToCore((InternalEvent*)&ev, &core); - g_assert(rc == BadMatch); - - detail = 0xFF; /* too big */ - ev.detail.key = detail; - state = 0xFFFF; /* highest value */ - ev.corestate = state; - rc = EventToCore((InternalEvent*)&ev, &core); - test_event(); - - state = 0x10000; /* too big */ - ev.corestate = state; - rc = EventToCore((InternalEvent*)&ev, &core); - g_assert(core.u.keyButtonPointer.state != state); - g_assert(core.u.keyButtonPointer.state == (state & 0xFFFF)); - -#undef test_event -} - -static void dix_event_to_core_conversion(void) -{ - DeviceEvent ev; - xEvent core; - int rc; - - ev.header = 0xFF; - ev.length = sizeof(DeviceEvent); - - ev.type = 0; - rc = EventToCore((InternalEvent*)&ev, &core); - g_assert(rc == BadImplementation); - - ev.type = 1; - rc = EventToCore((InternalEvent*)&ev, &core); - g_assert(rc == BadImplementation); - - ev.type = ET_ProximityOut + 1; - rc = EventToCore((InternalEvent*)&ev, &core); - g_assert(rc == BadImplementation); - - ev.type = ET_ProximityIn; - rc = EventToCore((InternalEvent*)&ev, &core); - g_assert(rc == BadMatch); - - ev.type = ET_ProximityOut; - rc = EventToCore((InternalEvent*)&ev, &core); - g_assert(rc == BadMatch); - - dix_event_to_core(ET_KeyPress); - dix_event_to_core(ET_KeyRelease); - dix_event_to_core(ET_ButtonPress); - dix_event_to_core(ET_ButtonRelease); - dix_event_to_core(ET_Motion); -} - -static void xi2_struct_sizes(void) -{ -#define compare(req) \ - g_assert(sizeof(req) == sz_##req); - - compare(xXIQueryVersionReq); - compare(xXIWarpPointerReq); - compare(xXIChangeCursorReq); - compare(xXIChangeHierarchyReq); - compare(xXISetClientPointerReq); - compare(xXIGetClientPointerReq); - compare(xXISelectEventsReq); - compare(xXIQueryVersionReq); - compare(xXIQueryDeviceReq); - compare(xXISetFocusReq); - compare(xXIGetFocusReq); - compare(xXIGrabDeviceReq); - compare(xXIUngrabDeviceReq); - compare(xXIAllowEventsReq); - compare(xXIPassiveGrabDeviceReq); - compare(xXIPassiveUngrabDeviceReq); - compare(xXIListPropertiesReq); - compare(xXIChangePropertyReq); - compare(xXIDeletePropertyReq); - compare(xXIGetPropertyReq); - compare(xXIGetSelectedEventsReq); -#undef compare -} - - -static void dix_grab_matching(void) -{ - DeviceIntRec xi_all_devices, xi_all_master_devices, dev1, dev2; - GrabRec a, b; - BOOL rc; - - memset(&a, 0, sizeof(a)); - memset(&b, 0, sizeof(b)); - - /* different grabtypes must fail */ - a.grabtype = GRABTYPE_CORE; - b.grabtype = GRABTYPE_XI2; - rc = GrabMatchesSecond(&a, &b, FALSE); - g_assert(rc == FALSE); - rc = GrabMatchesSecond(&b, &a, FALSE); - g_assert(rc == FALSE); - - a.grabtype = GRABTYPE_XI; - b.grabtype = GRABTYPE_XI2; - rc = GrabMatchesSecond(&a, &b, FALSE); - g_assert(rc == FALSE); - rc = GrabMatchesSecond(&b, &a, FALSE); - g_assert(rc == FALSE); - - a.grabtype = GRABTYPE_XI; - b.grabtype = GRABTYPE_CORE; - rc = GrabMatchesSecond(&a, &b, FALSE); - g_assert(rc == FALSE); - rc = GrabMatchesSecond(&b, &a, FALSE); - g_assert(rc == FALSE); - - /* XI2 grabs for different devices must fail, regardless of ignoreDevice - * XI2 grabs for master devices must fail against a slave */ - memset(&xi_all_devices, 0, sizeof(DeviceIntRec)); - memset(&xi_all_master_devices, 0, sizeof(DeviceIntRec)); - memset(&dev1, 0, sizeof(DeviceIntRec)); - memset(&dev2, 0, sizeof(DeviceIntRec)); - - xi_all_devices.id = XIAllDevices; - xi_all_master_devices.id = XIAllMasterDevices; - dev1.id = 10; - dev1.type = SLAVE; - dev2.id = 11; - dev2.type = SLAVE; - - inputInfo.all_devices = &xi_all_devices; - inputInfo.all_master_devices = &xi_all_master_devices; - a.grabtype = GRABTYPE_XI2; - b.grabtype = GRABTYPE_XI2; - a.device = &dev1; - b.device = &dev2; - - rc = GrabMatchesSecond(&a, &b, FALSE); - g_assert(rc == FALSE); - - a.device = &dev2; - b.device = &dev1; - rc = GrabMatchesSecond(&a, &b, FALSE); - g_assert(rc == FALSE); - rc = GrabMatchesSecond(&a, &b, TRUE); - g_assert(rc == FALSE); - - a.device = inputInfo.all_master_devices; - b.device = &dev1; - rc = GrabMatchesSecond(&a, &b, FALSE); - g_assert(rc == FALSE); - rc = GrabMatchesSecond(&a, &b, TRUE); - g_assert(rc == FALSE); - - a.device = &dev1; - b.device = inputInfo.all_master_devices; - rc = GrabMatchesSecond(&a, &b, FALSE); - g_assert(rc == FALSE); - rc = GrabMatchesSecond(&a, &b, TRUE); - g_assert(rc == FALSE); - - /* ignoreDevice FALSE must fail for different devices for CORE and XI */ - a.grabtype = GRABTYPE_XI; - b.grabtype = GRABTYPE_XI; - a.device = &dev1; - b.device = &dev2; - a.modifierDevice = &dev1; - b.modifierDevice = &dev1; - rc = GrabMatchesSecond(&a, &b, FALSE); - g_assert(rc == FALSE); - - a.grabtype = GRABTYPE_CORE; - b.grabtype = GRABTYPE_CORE; - a.device = &dev1; - b.device = &dev2; - a.modifierDevice = &dev1; - b.modifierDevice = &dev1; - rc = GrabMatchesSecond(&a, &b, FALSE); - g_assert(rc == FALSE); - - /* ignoreDevice FALSE must fail for different modifier devices for CORE - * and XI */ - a.grabtype = GRABTYPE_XI; - b.grabtype = GRABTYPE_XI; - a.device = &dev1; - b.device = &dev1; - a.modifierDevice = &dev1; - b.modifierDevice = &dev2; - rc = GrabMatchesSecond(&a, &b, FALSE); - g_assert(rc == FALSE); - - a.grabtype = GRABTYPE_CORE; - b.grabtype = GRABTYPE_CORE; - a.device = &dev1; - b.device = &dev1; - a.modifierDevice = &dev1; - b.modifierDevice = &dev2; - rc = GrabMatchesSecond(&a, &b, FALSE); - g_assert(rc == FALSE); - - /* different event type must fail */ - a.grabtype = GRABTYPE_XI2; - b.grabtype = GRABTYPE_XI2; - a.device = &dev1; - b.device = &dev1; - a.modifierDevice = &dev1; - b.modifierDevice = &dev1; - a.type = XI_KeyPress; - b.type = XI_KeyRelease; - rc = GrabMatchesSecond(&a, &b, FALSE); - g_assert(rc == FALSE); - rc = GrabMatchesSecond(&a, &b, TRUE); - g_assert(rc == FALSE); - - a.grabtype = GRABTYPE_CORE; - b.grabtype = GRABTYPE_CORE; - a.device = &dev1; - b.device = &dev1; - a.modifierDevice = &dev1; - b.modifierDevice = &dev1; - a.type = XI_KeyPress; - b.type = XI_KeyRelease; - rc = GrabMatchesSecond(&a, &b, FALSE); - g_assert(rc == FALSE); - rc = GrabMatchesSecond(&a, &b, TRUE); - g_assert(rc == FALSE); - - a.grabtype = GRABTYPE_XI; - b.grabtype = GRABTYPE_XI; - a.device = &dev1; - b.device = &dev1; - a.modifierDevice = &dev1; - b.modifierDevice = &dev1; - a.type = XI_KeyPress; - b.type = XI_KeyRelease; - rc = GrabMatchesSecond(&a, &b, FALSE); - g_assert(rc == FALSE); - rc = GrabMatchesSecond(&a, &b, TRUE); - g_assert(rc == FALSE); - - /* different modifiers must fail */ - a.grabtype = GRABTYPE_XI2; - b.grabtype = GRABTYPE_XI2; - a.device = &dev1; - b.device = &dev1; - a.modifierDevice = &dev1; - b.modifierDevice = &dev1; - a.type = XI_KeyPress; - b.type = XI_KeyPress; - a.modifiersDetail.exact = 1; - b.modifiersDetail.exact = 2; - rc = GrabMatchesSecond(&a, &b, FALSE); - g_assert(rc == FALSE); - rc = GrabMatchesSecond(&b, &a, FALSE); - g_assert(rc == FALSE); - - a.grabtype = GRABTYPE_CORE; - b.grabtype = GRABTYPE_CORE; - rc = GrabMatchesSecond(&a, &b, FALSE); - g_assert(rc == FALSE); - rc = GrabMatchesSecond(&b, &a, FALSE); - g_assert(rc == FALSE); - - a.grabtype = GRABTYPE_XI; - b.grabtype = GRABTYPE_XI; - rc = GrabMatchesSecond(&a, &b, FALSE); - g_assert(rc == FALSE); - rc = GrabMatchesSecond(&b, &a, FALSE); - g_assert(rc == FALSE); - - /* AnyModifier must fail for XI2 */ - a.grabtype = GRABTYPE_XI2; - b.grabtype = GRABTYPE_XI2; - a.modifiersDetail.exact = AnyModifier; - b.modifiersDetail.exact = 1; - rc = GrabMatchesSecond(&a, &b, FALSE); - g_assert(rc == FALSE); - rc = GrabMatchesSecond(&b, &a, FALSE); - g_assert(rc == FALSE); - - /* XIAnyModifier must fail for CORE and XI */ - a.grabtype = GRABTYPE_XI; - b.grabtype = GRABTYPE_XI; - a.modifiersDetail.exact = XIAnyModifier; - b.modifiersDetail.exact = 1; - rc = GrabMatchesSecond(&a, &b, FALSE); - g_assert(rc == FALSE); - rc = GrabMatchesSecond(&b, &a, FALSE); - g_assert(rc == FALSE); - - a.grabtype = GRABTYPE_CORE; - b.grabtype = GRABTYPE_CORE; - a.modifiersDetail.exact = XIAnyModifier; - b.modifiersDetail.exact = 1; - rc = GrabMatchesSecond(&a, &b, FALSE); - g_assert(rc == FALSE); - rc = GrabMatchesSecond(&b, &a, FALSE); - g_assert(rc == FALSE); - - /* different detail must fail */ - a.grabtype = GRABTYPE_XI2; - b.grabtype = GRABTYPE_XI2; - a.detail.exact = 1; - b.detail.exact = 2; - a.modifiersDetail.exact = 1; - b.modifiersDetail.exact = 1; - rc = GrabMatchesSecond(&a, &b, FALSE); - g_assert(rc == FALSE); - rc = GrabMatchesSecond(&b, &a, FALSE); - g_assert(rc == FALSE); - - a.grabtype = GRABTYPE_XI; - b.grabtype = GRABTYPE_XI; - rc = GrabMatchesSecond(&a, &b, FALSE); - g_assert(rc == FALSE); - rc = GrabMatchesSecond(&b, &a, FALSE); - g_assert(rc == FALSE); - - a.grabtype = GRABTYPE_CORE; - b.grabtype = GRABTYPE_CORE; - rc = GrabMatchesSecond(&a, &b, FALSE); - g_assert(rc == FALSE); - rc = GrabMatchesSecond(&b, &a, FALSE); - g_assert(rc == FALSE); - - /* detail of AnyModifier must fail */ - a.grabtype = GRABTYPE_XI2; - b.grabtype = GRABTYPE_XI2; - a.detail.exact = AnyModifier; - b.detail.exact = 1; - a.modifiersDetail.exact = 1; - b.modifiersDetail.exact = 1; - rc = GrabMatchesSecond(&a, &b, FALSE); - g_assert(rc == FALSE); - rc = GrabMatchesSecond(&b, &a, FALSE); - g_assert(rc == FALSE); - - a.grabtype = GRABTYPE_CORE; - b.grabtype = GRABTYPE_CORE; - rc = GrabMatchesSecond(&a, &b, FALSE); - g_assert(rc == FALSE); - rc = GrabMatchesSecond(&b, &a, FALSE); - g_assert(rc == FALSE); - - a.grabtype = GRABTYPE_XI; - b.grabtype = GRABTYPE_XI; - rc = GrabMatchesSecond(&a, &b, FALSE); - g_assert(rc == FALSE); - rc = GrabMatchesSecond(&b, &a, FALSE); - g_assert(rc == FALSE); - - /* detail of XIAnyModifier must fail */ - a.grabtype = GRABTYPE_XI2; - b.grabtype = GRABTYPE_XI2; - a.detail.exact = XIAnyModifier; - b.detail.exact = 1; - a.modifiersDetail.exact = 1; - b.modifiersDetail.exact = 1; - rc = GrabMatchesSecond(&a, &b, FALSE); - g_assert(rc == FALSE); - rc = GrabMatchesSecond(&b, &a, FALSE); - g_assert(rc == FALSE); - - a.grabtype = GRABTYPE_CORE; - b.grabtype = GRABTYPE_CORE; - rc = GrabMatchesSecond(&a, &b, FALSE); - g_assert(rc == FALSE); - rc = GrabMatchesSecond(&b, &a, FALSE); - g_assert(rc == FALSE); - - a.grabtype = GRABTYPE_XI; - b.grabtype = GRABTYPE_XI; - rc = GrabMatchesSecond(&a, &b, FALSE); - g_assert(rc == FALSE); - rc = GrabMatchesSecond(&b, &a, FALSE); - g_assert(rc == FALSE); - - /* XIAnyModifier or AnyModifer must succeed */ - a.grabtype = GRABTYPE_XI2; - b.grabtype = GRABTYPE_XI2; - a.detail.exact = 1; - b.detail.exact = 1; - a.modifiersDetail.exact = XIAnyModifier; - b.modifiersDetail.exact = 1; - rc = GrabMatchesSecond(&a, &b, FALSE); - g_assert(rc == TRUE); - rc = GrabMatchesSecond(&b, &a, FALSE); - g_assert(rc == TRUE); - - a.grabtype = GRABTYPE_CORE; - b.grabtype = GRABTYPE_CORE; - a.detail.exact = 1; - b.detail.exact = 1; - a.modifiersDetail.exact = AnyModifier; - b.modifiersDetail.exact = 1; - rc = GrabMatchesSecond(&a, &b, FALSE); - g_assert(rc == TRUE); - rc = GrabMatchesSecond(&b, &a, FALSE); - g_assert(rc == TRUE); - - a.grabtype = GRABTYPE_XI; - b.grabtype = GRABTYPE_XI; - a.detail.exact = 1; - b.detail.exact = 1; - a.modifiersDetail.exact = AnyModifier; - b.modifiersDetail.exact = 1; - rc = GrabMatchesSecond(&a, &b, FALSE); - g_assert(rc == TRUE); - rc = GrabMatchesSecond(&b, &a, FALSE); - g_assert(rc == TRUE); - - /* AnyKey or XIAnyKeycode must succeed */ - a.grabtype = GRABTYPE_XI2; - b.grabtype = GRABTYPE_XI2; - a.detail.exact = XIAnyKeycode; - b.detail.exact = 1; - a.modifiersDetail.exact = 1; - b.modifiersDetail.exact = 1; - rc = GrabMatchesSecond(&a, &b, FALSE); - g_assert(rc == TRUE); - rc = GrabMatchesSecond(&b, &a, FALSE); - g_assert(rc == TRUE); - - a.grabtype = GRABTYPE_CORE; - b.grabtype = GRABTYPE_CORE; - a.detail.exact = AnyKey; - b.detail.exact = 1; - a.modifiersDetail.exact = 1; - b.modifiersDetail.exact = 1; - rc = GrabMatchesSecond(&a, &b, FALSE); - g_assert(rc == TRUE); - rc = GrabMatchesSecond(&b, &a, FALSE); - g_assert(rc == TRUE); - - a.grabtype = GRABTYPE_XI; - b.grabtype = GRABTYPE_XI; - a.detail.exact = AnyKey; - b.detail.exact = 1; - a.modifiersDetail.exact = 1; - b.modifiersDetail.exact = 1; - rc = GrabMatchesSecond(&a, &b, FALSE); - g_assert(rc == TRUE); - rc = GrabMatchesSecond(&b, &a, FALSE); - g_assert(rc == TRUE); -} - -static void test_bits_to_byte(int i) -{ - int expected_bytes; - expected_bytes = (i + 7)/8; - - g_assert(bits_to_bytes(i) >= i/8); - g_assert((bits_to_bytes(i) * 8) - i <= 7); - g_assert(expected_bytes == bits_to_bytes(i)); -} - -static void test_bytes_to_int32(int i) -{ - int expected_4byte; - expected_4byte = (i + 3)/4; - - g_assert(bytes_to_int32(i) <= i); - g_assert((bytes_to_int32(i) * 4) - i <= 3); - g_assert(expected_4byte == bytes_to_int32(i)); -} - -static void test_pad_to_int32(int i) -{ - int expected_bytes; - expected_bytes = ((i + 3)/4) * 4; - - g_assert(pad_to_int32(i) >= i); - g_assert(pad_to_int32(i) - i <= 3); - g_assert(expected_bytes == pad_to_int32(i)); -} -static void include_byte_padding_macros(void) -{ - g_test_message("Testing bits_to_bytes()"); - - /* the macros don't provide overflow protection */ - test_bits_to_byte(0); - test_bits_to_byte(1); - test_bits_to_byte(2); - test_bits_to_byte(7); - test_bits_to_byte(8); - test_bits_to_byte(0xFF); - test_bits_to_byte(0x100); - test_bits_to_byte(INT_MAX - 9); - test_bits_to_byte(INT_MAX - 8); - - g_test_message("Testing bytes_to_int32()"); - - test_bytes_to_int32(0); - test_bytes_to_int32(1); - test_bytes_to_int32(2); - test_bytes_to_int32(7); - test_bytes_to_int32(8); - test_bytes_to_int32(0xFF); - test_bytes_to_int32(0x100); - test_bytes_to_int32(0xFFFF); - test_bytes_to_int32(0x10000); - test_bytes_to_int32(0xFFFFFF); - test_bytes_to_int32(0x1000000); - test_bytes_to_int32(INT_MAX - 4); - test_bytes_to_int32(INT_MAX - 3); - - g_test_message("Testing pad_to_int32"); - - test_pad_to_int32(0); - test_pad_to_int32(0); - test_pad_to_int32(1); - test_pad_to_int32(2); - test_pad_to_int32(7); - test_pad_to_int32(8); - test_pad_to_int32(0xFF); - test_pad_to_int32(0x100); - test_pad_to_int32(0xFFFF); - test_pad_to_int32(0x10000); - test_pad_to_int32(0xFFFFFF); - test_pad_to_int32(0x1000000); - test_pad_to_int32(INT_MAX - 4); - test_pad_to_int32(INT_MAX - 3); -} - -static void xi_unregister_handlers(void) -{ - DeviceIntRec dev; - int handler; - - memset(&dev, 0, sizeof(dev)); - - handler = XIRegisterPropertyHandler(&dev, NULL, NULL, NULL); - g_assert(handler == 1); - handler = XIRegisterPropertyHandler(&dev, NULL, NULL, NULL); - g_assert(handler == 2); - handler = XIRegisterPropertyHandler(&dev, NULL, NULL, NULL); - g_assert(handler == 3); - - g_test_message("Unlinking from front."); - - XIUnregisterPropertyHandler(&dev, 4); /* NOOP */ - g_assert(dev.properties.handlers->id == 3); - XIUnregisterPropertyHandler(&dev, 3); - g_assert(dev.properties.handlers->id == 2); - XIUnregisterPropertyHandler(&dev, 2); - g_assert(dev.properties.handlers->id == 1); - XIUnregisterPropertyHandler(&dev, 1); - g_assert(dev.properties.handlers == NULL); - - handler = XIRegisterPropertyHandler(&dev, NULL, NULL, NULL); - g_assert(handler == 4); - handler = XIRegisterPropertyHandler(&dev, NULL, NULL, NULL); - g_assert(handler == 5); - handler = XIRegisterPropertyHandler(&dev, NULL, NULL, NULL); - g_assert(handler == 6); - XIUnregisterPropertyHandler(&dev, 3); /* NOOP */ - g_assert(dev.properties.handlers->next->next->next == NULL); - XIUnregisterPropertyHandler(&dev, 4); - g_assert(dev.properties.handlers->next->next == NULL); - XIUnregisterPropertyHandler(&dev, 5); - g_assert(dev.properties.handlers->next == NULL); - XIUnregisterPropertyHandler(&dev, 6); - g_assert(dev.properties.handlers == NULL); - - handler = XIRegisterPropertyHandler(&dev, NULL, NULL, NULL); - g_assert(handler == 7); - handler = XIRegisterPropertyHandler(&dev, NULL, NULL, NULL); - g_assert(handler == 8); - handler = XIRegisterPropertyHandler(&dev, NULL, NULL, NULL); - g_assert(handler == 9); - - XIDeleteAllDeviceProperties(&dev); - g_assert(dev.properties.handlers == NULL); - XIUnregisterPropertyHandler(&dev, 7); /* NOOP */ - -} - -static void cmp_attr_fields(InputAttributes *attr1, - InputAttributes *attr2) -{ - char **tags1, **tags2; - - g_assert(attr1 && attr2); - g_assert(attr1 != attr2); - g_assert(attr1->flags == attr2->flags); - - if (attr1->product != NULL) - { - g_assert(attr1->product != attr2->product); - g_assert(strcmp(attr1->product, attr2->product) == 0); - } else - g_assert(attr2->product == NULL); - - if (attr1->vendor != NULL) - { - g_assert(attr1->vendor != attr2->vendor); - g_assert(strcmp(attr1->vendor, attr2->vendor) == 0); - } else - g_assert(attr2->vendor == NULL); - - if (attr1->device != NULL) - { - g_assert(attr1->device != attr2->device); - g_assert(strcmp(attr1->device, attr2->device) == 0); - } else - g_assert(attr2->device == NULL); - - if (attr1->pnp_id != NULL) - { - g_assert(attr1->pnp_id != attr2->pnp_id); - g_assert(strcmp(attr1->pnp_id, attr2->pnp_id) == 0); - } else - g_assert(attr2->pnp_id == NULL); - - if (attr1->usb_id != NULL) - { - g_assert(attr1->usb_id != attr2->usb_id); - g_assert(strcmp(attr1->usb_id, attr2->usb_id) == 0); - } else - g_assert(attr2->usb_id == NULL); - - tags1 = attr1->tags; - tags2 = attr2->tags; - - /* if we don't have any tags, skip the tag checking bits */ - if (!tags1) - { - g_assert(!tags2); - return; - } - - /* Don't lug around empty arrays */ - g_assert(*tags1); - g_assert(*tags2); - - /* check for identical content, but duplicated */ - while (*tags1) - { - g_assert(*tags1 != *tags2); - g_assert(strcmp(*tags1, *tags2) == 0); - tags1++; - tags2++; - } - - /* ensure tags1 and tags2 have the same no of elements */ - g_assert(!*tags2); - - /* check for not sharing memory */ - tags1 = attr1->tags; - while (*tags1) - { - tags2 = attr2->tags; - while (*tags2) - g_assert(*tags1 != *tags2++); - - tags1++; - } -} - -static void dix_input_attributes(void) -{ - InputAttributes orig = {0}; - InputAttributes *new; - char *tags[4] = {"tag1", "tag2", "tag2", NULL}; - - new = DuplicateInputAttributes(NULL); - g_assert(!new); - - new = DuplicateInputAttributes(&orig); - g_assert(memcmp(&orig, new, sizeof(InputAttributes)) == 0); - - orig.product = "product name"; - new = DuplicateInputAttributes(&orig); - cmp_attr_fields(&orig, new); - FreeInputAttributes(new); - - orig.vendor = "vendor name"; - new = DuplicateInputAttributes(&orig); - cmp_attr_fields(&orig, new); - FreeInputAttributes(new); - - orig.device = "device path"; - new = DuplicateInputAttributes(&orig); - cmp_attr_fields(&orig, new); - FreeInputAttributes(new); - - orig.pnp_id = "PnPID"; - new = DuplicateInputAttributes(&orig); - cmp_attr_fields(&orig, new); - FreeInputAttributes(new); - - orig.usb_id = "USBID"; - new = DuplicateInputAttributes(&orig); - cmp_attr_fields(&orig, new); - FreeInputAttributes(new); - - orig.flags = 0xF0; - new = DuplicateInputAttributes(&orig); - cmp_attr_fields(&orig, new); - FreeInputAttributes(new); - - orig.tags = tags; - new = DuplicateInputAttributes(&orig); - cmp_attr_fields(&orig, new); - FreeInputAttributes(new); -} - -static void dix_input_valuator_masks(void) -{ - ValuatorMask *mask = NULL, *copy; - int nvaluators = MAX_VALUATORS; - int valuators[nvaluators]; - int i; - int first_val, num_vals; - - for (i = 0; i < nvaluators; i++) - valuators[i] = i; - - mask = valuator_mask_new(nvaluators); - g_assert(mask != NULL); - g_assert(valuator_mask_size(mask) == 0); - g_assert(valuator_mask_num_valuators(mask) == 0); - - for (i = 0; i < nvaluators; i++) - { - g_assert(!valuator_mask_isset(mask, i)); - valuator_mask_set(mask, i, valuators[i]); - g_assert(valuator_mask_isset(mask, i)); - g_assert(valuator_mask_get(mask, i) == valuators[i]); - g_assert(valuator_mask_size(mask) == i + 1); - g_assert(valuator_mask_num_valuators(mask) == i + 1); - } - - for (i = 0; i < nvaluators; i++) - { - g_assert(valuator_mask_isset(mask, i)); - valuator_mask_unset(mask, i); - /* we're removing valuators from the front, so size should stay the - * same until the last bit is removed */ - if (i < nvaluators - 1) - g_assert(valuator_mask_size(mask) == nvaluators); - g_assert(!valuator_mask_isset(mask, i)); - } - - g_assert(valuator_mask_size(mask) == 0); - valuator_mask_zero(mask); - g_assert(valuator_mask_size(mask) == 0); - g_assert(valuator_mask_num_valuators(mask) == 0); - for (i = 0; i < nvaluators; i++) - g_assert(!valuator_mask_isset(mask, i)); - - first_val = 5; - num_vals = 6; - - valuator_mask_set_range(mask, first_val, num_vals, valuators); - g_assert(valuator_mask_size(mask) == first_val + num_vals); - g_assert(valuator_mask_num_valuators(mask) == num_vals); - for (i = 0; i < nvaluators; i++) - { - if (i < first_val || i >= first_val + num_vals) - g_assert(!valuator_mask_isset(mask, i)); - else - { - g_assert(valuator_mask_isset(mask, i)); - g_assert(valuator_mask_get(mask, i) == valuators[i - first_val]); - } - } - - copy = valuator_mask_new(nvaluators); - valuator_mask_copy(copy, mask); - g_assert(mask != copy); - g_assert(valuator_mask_size(mask) == valuator_mask_size(copy)); - g_assert(valuator_mask_num_valuators(mask) == valuator_mask_num_valuators(copy)); - - for (i = 0; i < nvaluators; i++) - { - g_assert(valuator_mask_isset(mask, i) == valuator_mask_isset(copy, i)); - g_assert(valuator_mask_get(mask, i) == valuator_mask_get(copy, i)); - } - - free(mask); -} - -static void dix_valuator_mode(void) -{ - DeviceIntRec dev; - const int num_axes = MAX_VALUATORS; - int i; - Atom atoms[MAX_VALUATORS] = { 0 }; - - memset(&dev, 0, sizeof(DeviceIntRec)); - dev.type = MASTER_POINTER; /* claim it's a master to stop ptracccel */ - - g_assert(InitValuatorClassDeviceStruct(NULL, 0, atoms, 0, 0) == FALSE); - g_assert(InitValuatorClassDeviceStruct(&dev, num_axes, atoms, 0, Absolute)); - - for (i = 0; i < num_axes; i++) - { - g_assert(valuator_get_mode(&dev, i) == Absolute); - valuator_set_mode(&dev, i, Relative); - g_assert(dev.valuator->axes[i].mode == Relative); - g_assert(valuator_get_mode(&dev, i) == Relative); - } - - valuator_set_mode(&dev, VALUATOR_MODE_ALL_AXES, Absolute); - for (i = 0; i < num_axes; i++) - g_assert(valuator_get_mode(&dev, i) == Absolute); - - valuator_set_mode(&dev, VALUATOR_MODE_ALL_AXES, Relative); - for (i = 0; i < num_axes; i++) - g_assert(valuator_get_mode(&dev, i) == Relative); -} - -static void include_bit_test_macros(void) -{ - uint8_t mask[9] = { 0 }; - int i; - - for (i = 0; i < sizeof(mask)/sizeof(mask[0]); i++) - { - g_assert(BitIsOn(mask, i) == 0); - SetBit(mask, i); - g_assert(BitIsOn(mask, i) == 1); - g_assert(!!(mask[i/8] & (1 << (i % 8)))); - g_assert(CountBits(mask, sizeof(mask)) == 1); - ClearBit(mask, i); - g_assert(BitIsOn(mask, i) == 0); - } -} - -int main(int argc, char** argv) -{ - g_test_init(&argc, &argv,NULL); - g_test_bug_base("https://bugzilla.freedesktop.org/show_bug.cgi?id="); - - g_test_add_func("/dix/input/valuator-masks", dix_input_valuator_masks); - g_test_add_func("/dix/input/attributes", dix_input_attributes); - g_test_add_func("/dix/input/init-valuators", dix_init_valuators); - g_test_add_func("/dix/input/event-core-conversion", dix_event_to_core_conversion); - g_test_add_func("/dix/input/check-grab-values", dix_check_grab_values); - g_test_add_func("/dix/input/xi2-struct-sizes", xi2_struct_sizes); - g_test_add_func("/dix/input/grab_matching", dix_grab_matching); - g_test_add_func("/dix/input/valuator_mode", dix_valuator_mode); - g_test_add_func("/include/byte_padding_macros", include_byte_padding_macros); - g_test_add_func("/include/bit_test_macros", include_bit_test_macros); - g_test_add_func("/Xi/xiproperty/register-unregister", xi_unregister_handlers); - - - return g_test_run(); -} +/** + * Copyright © 2009 Red Hat, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#ifdef HAVE_DIX_CONFIG_H +#include +#endif + +#include +#include +#include "misc.h" +#include "resource.h" +#include +#include +#include +#include "windowstr.h" +#include "inputstr.h" +#include "eventconvert.h" +#include "exevents.h" +#include "dixgrabs.h" +#include "eventstr.h" +#include "inpututils.h" +#include + +/** + * Init a device with axes. + * Verify values set on the device. + * + * Result: All axes set to default values (usually 0). + */ +static void dix_init_valuators(void) +{ + DeviceIntRec dev; + ValuatorClassPtr val; + const int num_axes = 2; + int i; + Atom atoms[MAX_VALUATORS] = { 0 }; + + + memset(&dev, 0, sizeof(DeviceIntRec)); + dev.type = MASTER_POINTER; /* claim it's a master to stop ptracccel */ + + g_assert(InitValuatorClassDeviceStruct(NULL, 0, atoms, 0, 0) == FALSE); + g_assert(InitValuatorClassDeviceStruct(&dev, num_axes, atoms, 0, Absolute)); + + val = dev.valuator; + g_assert(val); + g_assert(val->numAxes == num_axes); + g_assert(val->numMotionEvents == 0); + g_assert(val->axisVal); + + for (i = 0; i < num_axes; i++) + { + g_assert(val->axisVal[i] == 0); + g_assert(val->axes->min_value == NO_AXIS_LIMITS); + g_assert(val->axes->max_value == NO_AXIS_LIMITS); + g_assert(val->axes->mode == Absolute); + } + + g_assert(dev.last.numValuators == num_axes); +} + +/* just check the known success cases, and that error cases set the client's + * error value correctly. */ +static void dix_check_grab_values(void) +{ + ClientRec client; + GrabParameters param; + int rc; + + memset(&client, 0, sizeof(client)); + + param.grabtype = GRABTYPE_CORE; + param.this_device_mode = GrabModeSync; + param.other_devices_mode = GrabModeSync; + param.modifiers = AnyModifier; + param.ownerEvents = FALSE; + + rc = CheckGrabValues(&client, ¶m); + g_assert(rc == Success); + + param.this_device_mode = GrabModeAsync; + rc = CheckGrabValues(&client, ¶m); + g_assert(rc == Success); + + param.this_device_mode = GrabModeAsync + 1; + rc = CheckGrabValues(&client, ¶m); + g_assert(rc == BadValue); + g_assert(client.errorValue == param.this_device_mode); + g_assert(client.errorValue == GrabModeAsync + 1); + + param.this_device_mode = GrabModeSync; + param.other_devices_mode = GrabModeAsync; + rc = CheckGrabValues(&client, ¶m); + g_assert(rc == Success); + + param.other_devices_mode = GrabModeAsync + 1; + rc = CheckGrabValues(&client, ¶m); + g_assert(rc == BadValue); + g_assert(client.errorValue == param.other_devices_mode); + g_assert(client.errorValue == GrabModeAsync + 1); + + param.other_devices_mode = GrabModeSync; + + param.modifiers = 1 << 13; + rc = CheckGrabValues(&client, ¶m); + g_assert(rc == BadValue); + g_assert(client.errorValue == param.modifiers); + g_assert(client.errorValue == (1 << 13)); + + + param.modifiers = AnyModifier; + param.ownerEvents = TRUE; + rc = CheckGrabValues(&client, ¶m); + g_assert(rc == Success); + + param.ownerEvents = 3; + rc = CheckGrabValues(&client, ¶m); + g_assert(rc == BadValue); + g_assert(client.errorValue == param.ownerEvents); + g_assert(client.errorValue == 3); +} + + +/** + * Convert various internal events to the matching core event and verify the + * parameters. + */ +static void dix_event_to_core(int type) +{ + DeviceEvent ev; + xEvent core; + int time; + int x, y; + int rc; + int state; + int detail; + const int ROOT_WINDOW_ID = 0x100; + + /* EventToCore memsets the event to 0 */ +#define test_event() \ + g_assert(rc == Success); \ + g_assert(core.u.u.type == type); \ + g_assert(core.u.u.detail == detail); \ + g_assert(core.u.keyButtonPointer.time == time); \ + g_assert(core.u.keyButtonPointer.rootX == x); \ + g_assert(core.u.keyButtonPointer.rootY == y); \ + g_assert(core.u.keyButtonPointer.state == state); \ + g_assert(core.u.keyButtonPointer.eventX == 0); \ + g_assert(core.u.keyButtonPointer.eventY == 0); \ + g_assert(core.u.keyButtonPointer.root == ROOT_WINDOW_ID); \ + g_assert(core.u.keyButtonPointer.event == 0); \ + g_assert(core.u.keyButtonPointer.child == 0); \ + g_assert(core.u.keyButtonPointer.sameScreen == FALSE); + + x = 0; + y = 0; + time = 12345; + state = 0; + detail = 0; + + ev.header = 0xFF; + ev.length = sizeof(DeviceEvent); + ev.time = time; + ev.root_y = x; + ev.root_x = y; + SetBit(ev.valuators.mask, 0); + SetBit(ev.valuators.mask, 1); + ev.root = ROOT_WINDOW_ID; + ev.corestate = state; + ev.detail.key = detail; + + ev.type = type; + ev.detail.key = 0; + rc = EventToCore((InternalEvent*)&ev, &core); + test_event(); + + x = 1; + y = 2; + ev.root_x = x; + ev.root_y = y; + rc = EventToCore((InternalEvent*)&ev, &core); + test_event(); + + x = 0x7FFF; + y = 0x7FFF; + ev.root_x = x; + ev.root_y = y; + rc = EventToCore((InternalEvent*)&ev, &core); + test_event(); + + x = 0x8000; /* too high */ + y = 0x8000; /* too high */ + ev.root_x = x; + ev.root_y = y; + rc = EventToCore((InternalEvent*)&ev, &core); + g_assert(core.u.keyButtonPointer.rootX != x); + g_assert(core.u.keyButtonPointer.rootY != y); + + x = 0x7FFF; + y = 0x7FFF; + ev.root_x = x; + ev.root_y = y; + time = 0; + ev.time = time; + rc = EventToCore((InternalEvent*)&ev, &core); + test_event(); + + detail = 1; + ev.detail.key = detail; + rc = EventToCore((InternalEvent*)&ev, &core); + test_event(); + + detail = 0xFF; /* highest value */ + ev.detail.key = detail; + rc = EventToCore((InternalEvent*)&ev, &core); + test_event(); + + detail = 0xFFF; /* too big */ + ev.detail.key = detail; + rc = EventToCore((InternalEvent*)&ev, &core); + g_assert(rc == BadMatch); + + detail = 0xFF; /* too big */ + ev.detail.key = detail; + state = 0xFFFF; /* highest value */ + ev.corestate = state; + rc = EventToCore((InternalEvent*)&ev, &core); + test_event(); + + state = 0x10000; /* too big */ + ev.corestate = state; + rc = EventToCore((InternalEvent*)&ev, &core); + g_assert(core.u.keyButtonPointer.state != state); + g_assert(core.u.keyButtonPointer.state == (state & 0xFFFF)); + +#undef test_event +} + +static void dix_event_to_core_fail(int evtype, int expected_rc) +{ + DeviceEvent ev; + xEvent core; + int rc; + + ev.header = 0xFF; + ev.length = sizeof(DeviceEvent); + + ev.type = evtype; + rc = EventToCore((InternalEvent*)&ev, &core); + g_assert(rc == expected_rc); +} + +static void dix_event_to_core_conversion(void) +{ + dix_event_to_core_fail(0, BadImplementation); + dix_event_to_core_fail(1, BadImplementation); + dix_event_to_core_fail(ET_ProximityOut + 1, BadImplementation); + dix_event_to_core_fail(ET_ProximityIn, BadMatch); + dix_event_to_core_fail(ET_ProximityOut, BadMatch); + + dix_event_to_core(ET_KeyPress); + dix_event_to_core(ET_KeyRelease); + dix_event_to_core(ET_ButtonPress); + dix_event_to_core(ET_ButtonRelease); + dix_event_to_core(ET_Motion); +} + +static void xi2_struct_sizes(void) +{ +#define compare(req) \ + g_assert(sizeof(req) == sz_##req); + + compare(xXIQueryVersionReq); + compare(xXIWarpPointerReq); + compare(xXIChangeCursorReq); + compare(xXIChangeHierarchyReq); + compare(xXISetClientPointerReq); + compare(xXIGetClientPointerReq); + compare(xXISelectEventsReq); + compare(xXIQueryVersionReq); + compare(xXIQueryDeviceReq); + compare(xXISetFocusReq); + compare(xXIGetFocusReq); + compare(xXIGrabDeviceReq); + compare(xXIUngrabDeviceReq); + compare(xXIAllowEventsReq); + compare(xXIPassiveGrabDeviceReq); + compare(xXIPassiveUngrabDeviceReq); + compare(xXIListPropertiesReq); + compare(xXIChangePropertyReq); + compare(xXIDeletePropertyReq); + compare(xXIGetPropertyReq); + compare(xXIGetSelectedEventsReq); +#undef compare +} + + +static void dix_grab_matching(void) +{ + DeviceIntRec xi_all_devices, xi_all_master_devices, dev1, dev2; + GrabRec a, b; + BOOL rc; + + memset(&a, 0, sizeof(a)); + memset(&b, 0, sizeof(b)); + + /* different grabtypes must fail */ + a.grabtype = GRABTYPE_CORE; + b.grabtype = GRABTYPE_XI2; + rc = GrabMatchesSecond(&a, &b, FALSE); + g_assert(rc == FALSE); + rc = GrabMatchesSecond(&b, &a, FALSE); + g_assert(rc == FALSE); + + a.grabtype = GRABTYPE_XI; + b.grabtype = GRABTYPE_XI2; + rc = GrabMatchesSecond(&a, &b, FALSE); + g_assert(rc == FALSE); + rc = GrabMatchesSecond(&b, &a, FALSE); + g_assert(rc == FALSE); + + a.grabtype = GRABTYPE_XI; + b.grabtype = GRABTYPE_CORE; + rc = GrabMatchesSecond(&a, &b, FALSE); + g_assert(rc == FALSE); + rc = GrabMatchesSecond(&b, &a, FALSE); + g_assert(rc == FALSE); + + /* XI2 grabs for different devices must fail, regardless of ignoreDevice + * XI2 grabs for master devices must fail against a slave */ + memset(&xi_all_devices, 0, sizeof(DeviceIntRec)); + memset(&xi_all_master_devices, 0, sizeof(DeviceIntRec)); + memset(&dev1, 0, sizeof(DeviceIntRec)); + memset(&dev2, 0, sizeof(DeviceIntRec)); + + xi_all_devices.id = XIAllDevices; + xi_all_master_devices.id = XIAllMasterDevices; + dev1.id = 10; + dev1.type = SLAVE; + dev2.id = 11; + dev2.type = SLAVE; + + inputInfo.all_devices = &xi_all_devices; + inputInfo.all_master_devices = &xi_all_master_devices; + a.grabtype = GRABTYPE_XI2; + b.grabtype = GRABTYPE_XI2; + a.device = &dev1; + b.device = &dev2; + + rc = GrabMatchesSecond(&a, &b, FALSE); + g_assert(rc == FALSE); + + a.device = &dev2; + b.device = &dev1; + rc = GrabMatchesSecond(&a, &b, FALSE); + g_assert(rc == FALSE); + rc = GrabMatchesSecond(&a, &b, TRUE); + g_assert(rc == FALSE); + + a.device = inputInfo.all_master_devices; + b.device = &dev1; + rc = GrabMatchesSecond(&a, &b, FALSE); + g_assert(rc == FALSE); + rc = GrabMatchesSecond(&a, &b, TRUE); + g_assert(rc == FALSE); + + a.device = &dev1; + b.device = inputInfo.all_master_devices; + rc = GrabMatchesSecond(&a, &b, FALSE); + g_assert(rc == FALSE); + rc = GrabMatchesSecond(&a, &b, TRUE); + g_assert(rc == FALSE); + + /* ignoreDevice FALSE must fail for different devices for CORE and XI */ + a.grabtype = GRABTYPE_XI; + b.grabtype = GRABTYPE_XI; + a.device = &dev1; + b.device = &dev2; + a.modifierDevice = &dev1; + b.modifierDevice = &dev1; + rc = GrabMatchesSecond(&a, &b, FALSE); + g_assert(rc == FALSE); + + a.grabtype = GRABTYPE_CORE; + b.grabtype = GRABTYPE_CORE; + a.device = &dev1; + b.device = &dev2; + a.modifierDevice = &dev1; + b.modifierDevice = &dev1; + rc = GrabMatchesSecond(&a, &b, FALSE); + g_assert(rc == FALSE); + + /* ignoreDevice FALSE must fail for different modifier devices for CORE + * and XI */ + a.grabtype = GRABTYPE_XI; + b.grabtype = GRABTYPE_XI; + a.device = &dev1; + b.device = &dev1; + a.modifierDevice = &dev1; + b.modifierDevice = &dev2; + rc = GrabMatchesSecond(&a, &b, FALSE); + g_assert(rc == FALSE); + + a.grabtype = GRABTYPE_CORE; + b.grabtype = GRABTYPE_CORE; + a.device = &dev1; + b.device = &dev1; + a.modifierDevice = &dev1; + b.modifierDevice = &dev2; + rc = GrabMatchesSecond(&a, &b, FALSE); + g_assert(rc == FALSE); + + /* different event type must fail */ + a.grabtype = GRABTYPE_XI2; + b.grabtype = GRABTYPE_XI2; + a.device = &dev1; + b.device = &dev1; + a.modifierDevice = &dev1; + b.modifierDevice = &dev1; + a.type = XI_KeyPress; + b.type = XI_KeyRelease; + rc = GrabMatchesSecond(&a, &b, FALSE); + g_assert(rc == FALSE); + rc = GrabMatchesSecond(&a, &b, TRUE); + g_assert(rc == FALSE); + + a.grabtype = GRABTYPE_CORE; + b.grabtype = GRABTYPE_CORE; + a.device = &dev1; + b.device = &dev1; + a.modifierDevice = &dev1; + b.modifierDevice = &dev1; + a.type = XI_KeyPress; + b.type = XI_KeyRelease; + rc = GrabMatchesSecond(&a, &b, FALSE); + g_assert(rc == FALSE); + rc = GrabMatchesSecond(&a, &b, TRUE); + g_assert(rc == FALSE); + + a.grabtype = GRABTYPE_XI; + b.grabtype = GRABTYPE_XI; + a.device = &dev1; + b.device = &dev1; + a.modifierDevice = &dev1; + b.modifierDevice = &dev1; + a.type = XI_KeyPress; + b.type = XI_KeyRelease; + rc = GrabMatchesSecond(&a, &b, FALSE); + g_assert(rc == FALSE); + rc = GrabMatchesSecond(&a, &b, TRUE); + g_assert(rc == FALSE); + + /* different modifiers must fail */ + a.grabtype = GRABTYPE_XI2; + b.grabtype = GRABTYPE_XI2; + a.device = &dev1; + b.device = &dev1; + a.modifierDevice = &dev1; + b.modifierDevice = &dev1; + a.type = XI_KeyPress; + b.type = XI_KeyPress; + a.modifiersDetail.exact = 1; + b.modifiersDetail.exact = 2; + rc = GrabMatchesSecond(&a, &b, FALSE); + g_assert(rc == FALSE); + rc = GrabMatchesSecond(&b, &a, FALSE); + g_assert(rc == FALSE); + + a.grabtype = GRABTYPE_CORE; + b.grabtype = GRABTYPE_CORE; + rc = GrabMatchesSecond(&a, &b, FALSE); + g_assert(rc == FALSE); + rc = GrabMatchesSecond(&b, &a, FALSE); + g_assert(rc == FALSE); + + a.grabtype = GRABTYPE_XI; + b.grabtype = GRABTYPE_XI; + rc = GrabMatchesSecond(&a, &b, FALSE); + g_assert(rc == FALSE); + rc = GrabMatchesSecond(&b, &a, FALSE); + g_assert(rc == FALSE); + + /* AnyModifier must fail for XI2 */ + a.grabtype = GRABTYPE_XI2; + b.grabtype = GRABTYPE_XI2; + a.modifiersDetail.exact = AnyModifier; + b.modifiersDetail.exact = 1; + rc = GrabMatchesSecond(&a, &b, FALSE); + g_assert(rc == FALSE); + rc = GrabMatchesSecond(&b, &a, FALSE); + g_assert(rc == FALSE); + + /* XIAnyModifier must fail for CORE and XI */ + a.grabtype = GRABTYPE_XI; + b.grabtype = GRABTYPE_XI; + a.modifiersDetail.exact = XIAnyModifier; + b.modifiersDetail.exact = 1; + rc = GrabMatchesSecond(&a, &b, FALSE); + g_assert(rc == FALSE); + rc = GrabMatchesSecond(&b, &a, FALSE); + g_assert(rc == FALSE); + + a.grabtype = GRABTYPE_CORE; + b.grabtype = GRABTYPE_CORE; + a.modifiersDetail.exact = XIAnyModifier; + b.modifiersDetail.exact = 1; + rc = GrabMatchesSecond(&a, &b, FALSE); + g_assert(rc == FALSE); + rc = GrabMatchesSecond(&b, &a, FALSE); + g_assert(rc == FALSE); + + /* different detail must fail */ + a.grabtype = GRABTYPE_XI2; + b.grabtype = GRABTYPE_XI2; + a.detail.exact = 1; + b.detail.exact = 2; + a.modifiersDetail.exact = 1; + b.modifiersDetail.exact = 1; + rc = GrabMatchesSecond(&a, &b, FALSE); + g_assert(rc == FALSE); + rc = GrabMatchesSecond(&b, &a, FALSE); + g_assert(rc == FALSE); + + a.grabtype = GRABTYPE_XI; + b.grabtype = GRABTYPE_XI; + rc = GrabMatchesSecond(&a, &b, FALSE); + g_assert(rc == FALSE); + rc = GrabMatchesSecond(&b, &a, FALSE); + g_assert(rc == FALSE); + + a.grabtype = GRABTYPE_CORE; + b.grabtype = GRABTYPE_CORE; + rc = GrabMatchesSecond(&a, &b, FALSE); + g_assert(rc == FALSE); + rc = GrabMatchesSecond(&b, &a, FALSE); + g_assert(rc == FALSE); + + /* detail of AnyModifier must fail */ + a.grabtype = GRABTYPE_XI2; + b.grabtype = GRABTYPE_XI2; + a.detail.exact = AnyModifier; + b.detail.exact = 1; + a.modifiersDetail.exact = 1; + b.modifiersDetail.exact = 1; + rc = GrabMatchesSecond(&a, &b, FALSE); + g_assert(rc == FALSE); + rc = GrabMatchesSecond(&b, &a, FALSE); + g_assert(rc == FALSE); + + a.grabtype = GRABTYPE_CORE; + b.grabtype = GRABTYPE_CORE; + rc = GrabMatchesSecond(&a, &b, FALSE); + g_assert(rc == FALSE); + rc = GrabMatchesSecond(&b, &a, FALSE); + g_assert(rc == FALSE); + + a.grabtype = GRABTYPE_XI; + b.grabtype = GRABTYPE_XI; + rc = GrabMatchesSecond(&a, &b, FALSE); + g_assert(rc == FALSE); + rc = GrabMatchesSecond(&b, &a, FALSE); + g_assert(rc == FALSE); + + /* detail of XIAnyModifier must fail */ + a.grabtype = GRABTYPE_XI2; + b.grabtype = GRABTYPE_XI2; + a.detail.exact = XIAnyModifier; + b.detail.exact = 1; + a.modifiersDetail.exact = 1; + b.modifiersDetail.exact = 1; + rc = GrabMatchesSecond(&a, &b, FALSE); + g_assert(rc == FALSE); + rc = GrabMatchesSecond(&b, &a, FALSE); + g_assert(rc == FALSE); + + a.grabtype = GRABTYPE_CORE; + b.grabtype = GRABTYPE_CORE; + rc = GrabMatchesSecond(&a, &b, FALSE); + g_assert(rc == FALSE); + rc = GrabMatchesSecond(&b, &a, FALSE); + g_assert(rc == FALSE); + + a.grabtype = GRABTYPE_XI; + b.grabtype = GRABTYPE_XI; + rc = GrabMatchesSecond(&a, &b, FALSE); + g_assert(rc == FALSE); + rc = GrabMatchesSecond(&b, &a, FALSE); + g_assert(rc == FALSE); + + /* XIAnyModifier or AnyModifer must succeed */ + a.grabtype = GRABTYPE_XI2; + b.grabtype = GRABTYPE_XI2; + a.detail.exact = 1; + b.detail.exact = 1; + a.modifiersDetail.exact = XIAnyModifier; + b.modifiersDetail.exact = 1; + rc = GrabMatchesSecond(&a, &b, FALSE); + g_assert(rc == TRUE); + rc = GrabMatchesSecond(&b, &a, FALSE); + g_assert(rc == TRUE); + + a.grabtype = GRABTYPE_CORE; + b.grabtype = GRABTYPE_CORE; + a.detail.exact = 1; + b.detail.exact = 1; + a.modifiersDetail.exact = AnyModifier; + b.modifiersDetail.exact = 1; + rc = GrabMatchesSecond(&a, &b, FALSE); + g_assert(rc == TRUE); + rc = GrabMatchesSecond(&b, &a, FALSE); + g_assert(rc == TRUE); + + a.grabtype = GRABTYPE_XI; + b.grabtype = GRABTYPE_XI; + a.detail.exact = 1; + b.detail.exact = 1; + a.modifiersDetail.exact = AnyModifier; + b.modifiersDetail.exact = 1; + rc = GrabMatchesSecond(&a, &b, FALSE); + g_assert(rc == TRUE); + rc = GrabMatchesSecond(&b, &a, FALSE); + g_assert(rc == TRUE); + + /* AnyKey or XIAnyKeycode must succeed */ + a.grabtype = GRABTYPE_XI2; + b.grabtype = GRABTYPE_XI2; + a.detail.exact = XIAnyKeycode; + b.detail.exact = 1; + a.modifiersDetail.exact = 1; + b.modifiersDetail.exact = 1; + rc = GrabMatchesSecond(&a, &b, FALSE); + g_assert(rc == TRUE); + rc = GrabMatchesSecond(&b, &a, FALSE); + g_assert(rc == TRUE); + + a.grabtype = GRABTYPE_CORE; + b.grabtype = GRABTYPE_CORE; + a.detail.exact = AnyKey; + b.detail.exact = 1; + a.modifiersDetail.exact = 1; + b.modifiersDetail.exact = 1; + rc = GrabMatchesSecond(&a, &b, FALSE); + g_assert(rc == TRUE); + rc = GrabMatchesSecond(&b, &a, FALSE); + g_assert(rc == TRUE); + + a.grabtype = GRABTYPE_XI; + b.grabtype = GRABTYPE_XI; + a.detail.exact = AnyKey; + b.detail.exact = 1; + a.modifiersDetail.exact = 1; + b.modifiersDetail.exact = 1; + rc = GrabMatchesSecond(&a, &b, FALSE); + g_assert(rc == TRUE); + rc = GrabMatchesSecond(&b, &a, FALSE); + g_assert(rc == TRUE); +} + +static void test_bits_to_byte(int i) +{ + int expected_bytes; + expected_bytes = (i + 7)/8; + + g_assert(bits_to_bytes(i) >= i/8); + g_assert((bits_to_bytes(i) * 8) - i <= 7); + g_assert(expected_bytes == bits_to_bytes(i)); +} + +static void test_bytes_to_int32(int i) +{ + int expected_4byte; + expected_4byte = (i + 3)/4; + + g_assert(bytes_to_int32(i) <= i); + g_assert((bytes_to_int32(i) * 4) - i <= 3); + g_assert(expected_4byte == bytes_to_int32(i)); +} + +static void test_pad_to_int32(int i) +{ + int expected_bytes; + expected_bytes = ((i + 3)/4) * 4; + + g_assert(pad_to_int32(i) >= i); + g_assert(pad_to_int32(i) - i <= 3); + g_assert(expected_bytes == pad_to_int32(i)); +} +static void include_byte_padding_macros(void) +{ + g_test_message("Testing bits_to_bytes()"); + + /* the macros don't provide overflow protection */ + test_bits_to_byte(0); + test_bits_to_byte(1); + test_bits_to_byte(2); + test_bits_to_byte(7); + test_bits_to_byte(8); + test_bits_to_byte(0xFF); + test_bits_to_byte(0x100); + test_bits_to_byte(INT_MAX - 9); + test_bits_to_byte(INT_MAX - 8); + + g_test_message("Testing bytes_to_int32()"); + + test_bytes_to_int32(0); + test_bytes_to_int32(1); + test_bytes_to_int32(2); + test_bytes_to_int32(7); + test_bytes_to_int32(8); + test_bytes_to_int32(0xFF); + test_bytes_to_int32(0x100); + test_bytes_to_int32(0xFFFF); + test_bytes_to_int32(0x10000); + test_bytes_to_int32(0xFFFFFF); + test_bytes_to_int32(0x1000000); + test_bytes_to_int32(INT_MAX - 4); + test_bytes_to_int32(INT_MAX - 3); + + g_test_message("Testing pad_to_int32"); + + test_pad_to_int32(0); + test_pad_to_int32(0); + test_pad_to_int32(1); + test_pad_to_int32(2); + test_pad_to_int32(7); + test_pad_to_int32(8); + test_pad_to_int32(0xFF); + test_pad_to_int32(0x100); + test_pad_to_int32(0xFFFF); + test_pad_to_int32(0x10000); + test_pad_to_int32(0xFFFFFF); + test_pad_to_int32(0x1000000); + test_pad_to_int32(INT_MAX - 4); + test_pad_to_int32(INT_MAX - 3); +} + +static void xi_unregister_handlers(void) +{ + DeviceIntRec dev; + int handler; + + memset(&dev, 0, sizeof(dev)); + + handler = XIRegisterPropertyHandler(&dev, NULL, NULL, NULL); + g_assert(handler == 1); + handler = XIRegisterPropertyHandler(&dev, NULL, NULL, NULL); + g_assert(handler == 2); + handler = XIRegisterPropertyHandler(&dev, NULL, NULL, NULL); + g_assert(handler == 3); + + g_test_message("Unlinking from front."); + + XIUnregisterPropertyHandler(&dev, 4); /* NOOP */ + g_assert(dev.properties.handlers->id == 3); + XIUnregisterPropertyHandler(&dev, 3); + g_assert(dev.properties.handlers->id == 2); + XIUnregisterPropertyHandler(&dev, 2); + g_assert(dev.properties.handlers->id == 1); + XIUnregisterPropertyHandler(&dev, 1); + g_assert(dev.properties.handlers == NULL); + + handler = XIRegisterPropertyHandler(&dev, NULL, NULL, NULL); + g_assert(handler == 4); + handler = XIRegisterPropertyHandler(&dev, NULL, NULL, NULL); + g_assert(handler == 5); + handler = XIRegisterPropertyHandler(&dev, NULL, NULL, NULL); + g_assert(handler == 6); + XIUnregisterPropertyHandler(&dev, 3); /* NOOP */ + g_assert(dev.properties.handlers->next->next->next == NULL); + XIUnregisterPropertyHandler(&dev, 4); + g_assert(dev.properties.handlers->next->next == NULL); + XIUnregisterPropertyHandler(&dev, 5); + g_assert(dev.properties.handlers->next == NULL); + XIUnregisterPropertyHandler(&dev, 6); + g_assert(dev.properties.handlers == NULL); + + handler = XIRegisterPropertyHandler(&dev, NULL, NULL, NULL); + g_assert(handler == 7); + handler = XIRegisterPropertyHandler(&dev, NULL, NULL, NULL); + g_assert(handler == 8); + handler = XIRegisterPropertyHandler(&dev, NULL, NULL, NULL); + g_assert(handler == 9); + + XIDeleteAllDeviceProperties(&dev); + g_assert(dev.properties.handlers == NULL); + XIUnregisterPropertyHandler(&dev, 7); /* NOOP */ + +} + +static void cmp_attr_fields(InputAttributes *attr1, + InputAttributes *attr2) +{ + char **tags1, **tags2; + + g_assert(attr1 && attr2); + g_assert(attr1 != attr2); + g_assert(attr1->flags == attr2->flags); + + if (attr1->product != NULL) + { + g_assert(attr1->product != attr2->product); + g_assert(strcmp(attr1->product, attr2->product) == 0); + } else + g_assert(attr2->product == NULL); + + if (attr1->vendor != NULL) + { + g_assert(attr1->vendor != attr2->vendor); + g_assert(strcmp(attr1->vendor, attr2->vendor) == 0); + } else + g_assert(attr2->vendor == NULL); + + if (attr1->device != NULL) + { + g_assert(attr1->device != attr2->device); + g_assert(strcmp(attr1->device, attr2->device) == 0); + } else + g_assert(attr2->device == NULL); + + if (attr1->pnp_id != NULL) + { + g_assert(attr1->pnp_id != attr2->pnp_id); + g_assert(strcmp(attr1->pnp_id, attr2->pnp_id) == 0); + } else + g_assert(attr2->pnp_id == NULL); + + if (attr1->usb_id != NULL) + { + g_assert(attr1->usb_id != attr2->usb_id); + g_assert(strcmp(attr1->usb_id, attr2->usb_id) == 0); + } else + g_assert(attr2->usb_id == NULL); + + tags1 = attr1->tags; + tags2 = attr2->tags; + + /* if we don't have any tags, skip the tag checking bits */ + if (!tags1) + { + g_assert(!tags2); + return; + } + + /* Don't lug around empty arrays */ + g_assert(*tags1); + g_assert(*tags2); + + /* check for identical content, but duplicated */ + while (*tags1) + { + g_assert(*tags1 != *tags2); + g_assert(strcmp(*tags1, *tags2) == 0); + tags1++; + tags2++; + } + + /* ensure tags1 and tags2 have the same no of elements */ + g_assert(!*tags2); + + /* check for not sharing memory */ + tags1 = attr1->tags; + while (*tags1) + { + tags2 = attr2->tags; + while (*tags2) + g_assert(*tags1 != *tags2++); + + tags1++; + } +} + +static void dix_input_attributes(void) +{ + InputAttributes orig = {0}; + InputAttributes *new; + char *tags[4] = {"tag1", "tag2", "tag2", NULL}; + + new = DuplicateInputAttributes(NULL); + g_assert(!new); + + new = DuplicateInputAttributes(&orig); + g_assert(memcmp(&orig, new, sizeof(InputAttributes)) == 0); + + orig.product = "product name"; + new = DuplicateInputAttributes(&orig); + cmp_attr_fields(&orig, new); + FreeInputAttributes(new); + + orig.vendor = "vendor name"; + new = DuplicateInputAttributes(&orig); + cmp_attr_fields(&orig, new); + FreeInputAttributes(new); + + orig.device = "device path"; + new = DuplicateInputAttributes(&orig); + cmp_attr_fields(&orig, new); + FreeInputAttributes(new); + + orig.pnp_id = "PnPID"; + new = DuplicateInputAttributes(&orig); + cmp_attr_fields(&orig, new); + FreeInputAttributes(new); + + orig.usb_id = "USBID"; + new = DuplicateInputAttributes(&orig); + cmp_attr_fields(&orig, new); + FreeInputAttributes(new); + + orig.flags = 0xF0; + new = DuplicateInputAttributes(&orig); + cmp_attr_fields(&orig, new); + FreeInputAttributes(new); + + orig.tags = tags; + new = DuplicateInputAttributes(&orig); + cmp_attr_fields(&orig, new); + FreeInputAttributes(new); +} + +static void dix_input_valuator_masks(void) +{ + ValuatorMask *mask = NULL, *copy; + int nvaluators = MAX_VALUATORS; + int valuators[nvaluators]; + int i; + int first_val, num_vals; + + for (i = 0; i < nvaluators; i++) + valuators[i] = i; + + mask = valuator_mask_new(nvaluators); + g_assert(mask != NULL); + g_assert(valuator_mask_size(mask) == 0); + g_assert(valuator_mask_num_valuators(mask) == 0); + + for (i = 0; i < nvaluators; i++) + { + g_assert(!valuator_mask_isset(mask, i)); + valuator_mask_set(mask, i, valuators[i]); + g_assert(valuator_mask_isset(mask, i)); + g_assert(valuator_mask_get(mask, i) == valuators[i]); + g_assert(valuator_mask_size(mask) == i + 1); + g_assert(valuator_mask_num_valuators(mask) == i + 1); + } + + for (i = 0; i < nvaluators; i++) + { + g_assert(valuator_mask_isset(mask, i)); + valuator_mask_unset(mask, i); + /* we're removing valuators from the front, so size should stay the + * same until the last bit is removed */ + if (i < nvaluators - 1) + g_assert(valuator_mask_size(mask) == nvaluators); + g_assert(!valuator_mask_isset(mask, i)); + } + + g_assert(valuator_mask_size(mask) == 0); + valuator_mask_zero(mask); + g_assert(valuator_mask_size(mask) == 0); + g_assert(valuator_mask_num_valuators(mask) == 0); + for (i = 0; i < nvaluators; i++) + g_assert(!valuator_mask_isset(mask, i)); + + first_val = 5; + num_vals = 6; + + valuator_mask_set_range(mask, first_val, num_vals, valuators); + g_assert(valuator_mask_size(mask) == first_val + num_vals); + g_assert(valuator_mask_num_valuators(mask) == num_vals); + for (i = 0; i < nvaluators; i++) + { + if (i < first_val || i >= first_val + num_vals) + g_assert(!valuator_mask_isset(mask, i)); + else + { + g_assert(valuator_mask_isset(mask, i)); + g_assert(valuator_mask_get(mask, i) == valuators[i - first_val]); + } + } + + copy = valuator_mask_new(nvaluators); + valuator_mask_copy(copy, mask); + g_assert(mask != copy); + g_assert(valuator_mask_size(mask) == valuator_mask_size(copy)); + g_assert(valuator_mask_num_valuators(mask) == valuator_mask_num_valuators(copy)); + + for (i = 0; i < nvaluators; i++) + { + g_assert(valuator_mask_isset(mask, i) == valuator_mask_isset(copy, i)); + g_assert(valuator_mask_get(mask, i) == valuator_mask_get(copy, i)); + } + + valuator_mask_free(&mask); + g_assert(mask == NULL); +} + +static void dix_valuator_mode(void) +{ + DeviceIntRec dev; + const int num_axes = MAX_VALUATORS; + int i; + Atom atoms[MAX_VALUATORS] = { 0 }; + + memset(&dev, 0, sizeof(DeviceIntRec)); + dev.type = MASTER_POINTER; /* claim it's a master to stop ptracccel */ + + g_assert(InitValuatorClassDeviceStruct(NULL, 0, atoms, 0, 0) == FALSE); + g_assert(InitValuatorClassDeviceStruct(&dev, num_axes, atoms, 0, Absolute)); + + for (i = 0; i < num_axes; i++) + { + g_assert(valuator_get_mode(&dev, i) == Absolute); + valuator_set_mode(&dev, i, Relative); + g_assert(dev.valuator->axes[i].mode == Relative); + g_assert(valuator_get_mode(&dev, i) == Relative); + } + + valuator_set_mode(&dev, VALUATOR_MODE_ALL_AXES, Absolute); + for (i = 0; i < num_axes; i++) + g_assert(valuator_get_mode(&dev, i) == Absolute); + + valuator_set_mode(&dev, VALUATOR_MODE_ALL_AXES, Relative); + for (i = 0; i < num_axes; i++) + g_assert(valuator_get_mode(&dev, i) == Relative); +} + +static void include_bit_test_macros(void) +{ + uint8_t mask[9] = { 0 }; + int i; + + for (i = 0; i < sizeof(mask)/sizeof(mask[0]); i++) + { + g_assert(BitIsOn(mask, i) == 0); + SetBit(mask, i); + g_assert(BitIsOn(mask, i) == 1); + g_assert(!!(mask[i/8] & (1 << (i % 8)))); + g_assert(CountBits(mask, sizeof(mask)) == 1); + ClearBit(mask, i); + g_assert(BitIsOn(mask, i) == 0); + } +} + +int main(int argc, char** argv) +{ + g_test_init(&argc, &argv,NULL); + g_test_bug_base("https://bugzilla.freedesktop.org/show_bug.cgi?id="); + + g_test_add_func("/dix/input/valuator-masks", dix_input_valuator_masks); + g_test_add_func("/dix/input/attributes", dix_input_attributes); + g_test_add_func("/dix/input/init-valuators", dix_init_valuators); + g_test_add_func("/dix/input/event-core-conversion", dix_event_to_core_conversion); + g_test_add_func("/dix/input/check-grab-values", dix_check_grab_values); + g_test_add_func("/dix/input/xi2-struct-sizes", xi2_struct_sizes); + g_test_add_func("/dix/input/grab_matching", dix_grab_matching); + g_test_add_func("/dix/input/valuator_mode", dix_valuator_mode); + g_test_add_func("/include/byte_padding_macros", include_byte_padding_macros); + g_test_add_func("/include/bit_test_macros", include_bit_test_macros); + g_test_add_func("/Xi/xiproperty/register-unregister", xi_unregister_handlers); + + + return g_test_run(); +} diff --git a/xorg-server/test/xi2/protocol-eventconvert.c b/xorg-server/test/xi2/protocol-eventconvert.c index 8184886c0..0478c33fe 100644 --- a/xorg-server/test/xi2/protocol-eventconvert.c +++ b/xorg-server/test/xi2/protocol-eventconvert.c @@ -1,907 +1,917 @@ -/** - * Copyright © 2009 Red Hat, Inc. - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice (including the next - * paragraph) shall be included in all copies or substantial portions of the - * Software. - * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ - -#ifdef HAVE_DIX_CONFIG_H -#include -#endif - -#include -#include - -#include "inputstr.h" -#include "eventstr.h" -#include "eventconvert.h" -#include "exevents.h" -#include - - -static void test_values_XIRawEvent(RawDeviceEvent *in, xXIRawEvent *out, - BOOL swap) -{ - int i; - unsigned char *ptr; - FP3232 *value, *raw_value; - int nvals = 0; - int bits_set; - int len; - - if (swap) - { - char n; - - swaps(&out->sequenceNumber, n); - swapl(&out->length, n); - swaps(&out->evtype, n); - swaps(&out->deviceid, n); - swapl(&out->time, n); - swapl(&out->detail, n); - swaps(&out->valuators_len, n); - } - - - g_assert(out->type == GenericEvent); - g_assert(out->extension == 0); /* IReqCode defaults to 0 */ - g_assert(out->evtype == GetXI2Type((InternalEvent*)in)); - g_assert(out->time == in->time); - g_assert(out->detail == in->detail.button); - g_assert(out->deviceid == in->deviceid); - g_assert(out->valuators_len >= bytes_to_int32(bits_to_bytes(sizeof(in->valuators.mask)))); - g_assert(out->flags == 0); /* FIXME: we don't set the flags yet */ - - ptr = (unsigned char*)&out[1]; - bits_set = 0; - - for (i = 0; out->valuators_len && i < sizeof(in->valuators.mask) * 8; i++) - { - g_assert (XIMaskIsSet(in->valuators.mask, i) == XIMaskIsSet(ptr, i)); - if (XIMaskIsSet(in->valuators.mask, i)) - bits_set++; - } - - /* length is len of valuator mask (in 4-byte units) + the number of bits - * set. Each bit set represents 2 8-byte values, hence the - * 'bits_set * 4' */ - len = out->valuators_len + bits_set * 4; - g_assert(out->length == len); - - nvals = 0; - - for (i = 0; out->valuators_len && i < MAX_VALUATORS; i++) - { - g_assert (XIMaskIsSet(in->valuators.mask, i) == XIMaskIsSet(ptr, i)); - if (XIMaskIsSet(in->valuators.mask, i)) - { - FP3232 vi, vo; - value = (FP3232*)(((unsigned char*)&out[1]) + out->valuators_len * 4); - value += nvals; - - vi.integral = in->valuators.data[i]; - vi.frac = in->valuators.data_frac[i]; - - vo.integral = value->integral; - vo.frac = value->frac; - if (swap) - { - char n; - swapl(&vo.integral, n); - swapl(&vo.frac, n); - } - - g_assert(vi.integral == vo.integral); - g_assert(vi.frac == vo.frac); - - raw_value = value + bits_set; - - vi.integral = in->valuators.data_raw[i]; - vi.frac = in->valuators.data_raw_frac[i]; - - vo.integral = raw_value->integral; - vo.frac = raw_value->frac; - if (swap) - { - char n; - swapl(&vo.integral, n); - swapl(&vo.frac, n); - } - - g_assert(vi.integral == vo.integral); - g_assert(vi.frac == vo.frac); - - nvals++; - } - } -} - -static void test_XIRawEvent(RawDeviceEvent *in) -{ - xXIRawEvent *out, *swapped; - int rc; - - rc = EventToXI2((InternalEvent*)in, (xEvent**)&out); - g_assert(rc == Success); - - test_values_XIRawEvent(in, out, FALSE); - - swapped = calloc(1, sizeof(xEvent) + out->length * 4); - XI2EventSwap((xGenericEvent*)out, (xGenericEvent*)swapped); - test_values_XIRawEvent(in, swapped, TRUE); - - free(out); - free(swapped); -} - -static void test_convert_XIFocusEvent(void) -{ - xEvent *out; - DeviceEvent in; - int rc; - - in.header = ET_Internal; - in.type = ET_Enter; - rc = EventToXI2((InternalEvent*)&in, &out); - g_assert(rc == Success); - g_assert(out == NULL); - - in.header = ET_Internal; - in.type = ET_FocusIn; - rc = EventToXI2((InternalEvent*)&in, &out); - g_assert(rc == Success); - g_assert(out == NULL); - - in.header = ET_Internal; - in.type = ET_FocusOut; - rc = EventToXI2((InternalEvent*)&in, &out); - g_assert(rc == BadImplementation); - - in.header = ET_Internal; - in.type = ET_Leave; - rc = EventToXI2((InternalEvent*)&in, &out); - g_assert(rc == BadImplementation); -} - - -static void test_convert_XIRawEvent(void) -{ - RawDeviceEvent in; - int i; - - memset(&in, 0, sizeof(in)); - - g_test_message("Testing all event types"); - in.header = ET_Internal; - in.type = ET_RawMotion; - test_XIRawEvent(&in); - - in.header = ET_Internal; - in.type = ET_RawKeyPress; - test_XIRawEvent(&in); - - in.header = ET_Internal; - in.type = ET_RawKeyRelease; - test_XIRawEvent(&in); - - in.header = ET_Internal; - in.type = ET_RawButtonPress; - test_XIRawEvent(&in); - - in.header = ET_Internal; - in.type = ET_RawButtonRelease; - test_XIRawEvent(&in); - - g_test_message("Testing details and other fields"); - in.detail.button = 1L; - test_XIRawEvent(&in); - in.detail.button = 1L << 8; - test_XIRawEvent(&in); - in.detail.button = 1L << 16; - test_XIRawEvent(&in); - in.detail.button = 1L << 24; - test_XIRawEvent(&in); - in.detail.button = ~0L; - test_XIRawEvent(&in); - - in.detail.button = 0; - - in.time = 1L; - test_XIRawEvent(&in); - in.time = 1L << 8; - test_XIRawEvent(&in); - in.time = 1L << 16; - test_XIRawEvent(&in); - in.time = 1L << 24; - test_XIRawEvent(&in); - in.time = ~0L; - test_XIRawEvent(&in); - - in.deviceid = 1; - test_XIRawEvent(&in); - in.deviceid = 1 << 8; - test_XIRawEvent(&in); - in.deviceid = ~0 & 0xFF; - test_XIRawEvent(&in); - - g_test_message("Testing valuator masks"); - for (i = 0; i < sizeof(in.valuators.mask) * 8; i++) - { - XISetMask(in.valuators.mask, i); - test_XIRawEvent(&in); - XIClearMask(in.valuators.mask, i); - } - - for (i = 0; i < MAX_VALUATORS; i++) - { - XISetMask(in.valuators.mask, i); - - in.valuators.data[i] = i; - in.valuators.data_raw[i] = i + 10; - in.valuators.data_frac[i] = i + 20; - in.valuators.data_raw_frac[i] = i + 30; - test_XIRawEvent(&in); - XIClearMask(in.valuators.mask, i); - } - - for (i = 0; i < sizeof(in.valuators.mask) * 8; i++) - { - XISetMask(in.valuators.mask, i); - test_XIRawEvent(&in); - } -} - -static void test_values_XIDeviceEvent(DeviceEvent *in, xXIDeviceEvent *out, - BOOL swap) -{ - int buttons, valuators; - int i; - unsigned char *ptr; - FP3232 *values; - - if (swap) { - char n; - - swaps(&out->sequenceNumber, n); - swapl(&out->length, n); - swaps(&out->evtype, n); - swaps(&out->deviceid, n); - swaps(&out->sourceid, n); - swapl(&out->time, n); - swapl(&out->detail, n); - swapl(&out->root, n); - swapl(&out->event, n); - swapl(&out->child, n); - swapl(&out->root_x, n); - swapl(&out->root_y, n); - swapl(&out->event_x, n); - swapl(&out->event_y, n); - swaps(&out->buttons_len, n); - swaps(&out->valuators_len, n); - swapl(&out->mods.base_mods, n); - swapl(&out->mods.latched_mods, n); - swapl(&out->mods.locked_mods, n); - swapl(&out->mods.effective_mods, n); - } - - g_assert(out->extension == 0); /* IReqCode defaults to 0 */ - g_assert(out->evtype == GetXI2Type((InternalEvent*)in)); - g_assert(out->time == in->time); - g_assert(out->detail == in->detail.button); - g_assert(out->length >= 12); - - g_assert(out->deviceid == in->deviceid); - g_assert(out->sourceid == in->sourceid); - - g_assert(out->flags == 0); /* FIXME: we don't set the flags yet */ - - g_assert(out->root == in->root); - g_assert(out->event == None); /* set in FixUpEventFromWindow */ - g_assert(out->child == None); /* set in FixUpEventFromWindow */ - - g_assert(out->mods.base_mods == in->mods.base); - g_assert(out->mods.latched_mods == in->mods.latched); - g_assert(out->mods.locked_mods == in->mods.locked); - g_assert(out->mods.effective_mods == in->mods.effective); - - g_assert(out->group.base_group == in->group.base); - g_assert(out->group.latched_group == in->group.latched); - g_assert(out->group.locked_group == in->group.locked); - g_assert(out->group.effective_group == in->group.effective); - - g_assert(out->event_x == 0); /* set in FixUpEventFromWindow */ - g_assert(out->event_y == 0); /* set in FixUpEventFromWindow */ - - g_assert(out->root_x == FP1616(in->root_x, in->root_x_frac)); - g_assert(out->root_y == FP1616(in->root_y, in->root_y_frac)); - - buttons = 0; - for (i = 0; i < bits_to_bytes(sizeof(in->buttons)); i++) - { - if (XIMaskIsSet(in->buttons, i)) - { - g_assert(out->buttons_len >= bytes_to_int32(bits_to_bytes(i))); - buttons++; - } - } - - ptr = (unsigned char*)&out[1]; - for (i = 0; i < sizeof(in->buttons) * 8; i++) - g_assert(XIMaskIsSet(in->buttons, i) == XIMaskIsSet(ptr, i)); - - - valuators = 0; - for (i = 0; i < sizeof(in->valuators.mask) * 8; i++) - if (XIMaskIsSet(in->valuators.mask, i)) - valuators++; - - g_assert(out->valuators_len >= bytes_to_int32(bits_to_bytes(valuators))); - - ptr += out->buttons_len * 4; - values = (FP3232*)(ptr + out->valuators_len * 4); - for (i = 0; i < sizeof(in->valuators.mask) * 8 || - i < (out->valuators_len * 4) * 8; i++) - { - if (i > sizeof(in->valuators.mask) * 8) - g_assert(!XIMaskIsSet(ptr, i)); - else if (i > out->valuators_len * 4 * 8) - g_assert(!XIMaskIsSet(in->valuators.mask, i)); - else { - g_assert(XIMaskIsSet(in->valuators.mask, i) == - XIMaskIsSet(ptr, i)); - - if (XIMaskIsSet(ptr, i)) - { - FP3232 vi, vo; - - vi.integral = in->valuators.data[i]; - vi.frac = in->valuators.data_frac[i]; - - vo = *values; - - if (swap) - { - char n; - swapl(&vo.integral, n); - swapl(&vo.frac, n); - } - - - g_assert(vi.integral == vo.integral); - g_assert(vi.frac == vo.frac); - values++; - } - } - } -} - -static void test_XIDeviceEvent(DeviceEvent *in) -{ - xXIDeviceEvent *out, *swapped; - int rc; - - rc = EventToXI2((InternalEvent*)in, (xEvent**)&out); - g_assert(rc == Success); - - test_values_XIDeviceEvent(in, out, FALSE); - - swapped = calloc(1, sizeof(xEvent) + out->length * 4); - XI2EventSwap((xGenericEvent*)out, (xGenericEvent*)swapped); - test_values_XIDeviceEvent(in, swapped, TRUE); - - free(out); - free(swapped); -} - -static void test_convert_XIDeviceEvent(void) -{ - DeviceEvent in; - int i; - - memset(&in, 0, sizeof(in)); - - g_test_message("Testing simple field values"); - in.header = ET_Internal; - in.type = ET_Motion; - in.length = sizeof(DeviceEvent); - in.time = 0; - in.deviceid = 1; - in.sourceid = 2; - in.root = 3; - in.root_x = 4; - in.root_x_frac = 5; - in.root_y = 6; - in.root_y_frac = 7; - in.detail.button = 8; - in.mods.base = 9; - in.mods.latched = 10; - in.mods.locked = 11; - in.mods.effective = 11; - in.group.base = 12; - in.group.latched = 13; - in.group.locked = 14; - in.group.effective = 15; - - test_XIDeviceEvent(&in); - - g_test_message("Testing field ranges"); - /* 32 bit */ - in.detail.button = 1L; - test_XIDeviceEvent(&in); - in.detail.button = 1L << 8; - test_XIDeviceEvent(&in); - in.detail.button = 1L << 16; - test_XIDeviceEvent(&in); - in.detail.button = 1L << 24; - test_XIDeviceEvent(&in); - in.detail.button = ~0L; - test_XIDeviceEvent(&in); - - /* 32 bit */ - in.time = 1L; - test_XIDeviceEvent(&in); - in.time = 1L << 8; - test_XIDeviceEvent(&in); - in.time = 1L << 16; - test_XIDeviceEvent(&in); - in.time = 1L << 24; - test_XIDeviceEvent(&in); - in.time = ~0L; - test_XIDeviceEvent(&in); - - /* 16 bit */ - in.deviceid = 1; - test_XIDeviceEvent(&in); - in.deviceid = 1 << 8; - test_XIDeviceEvent(&in); - in.deviceid = ~0 & 0xFF; - test_XIDeviceEvent(&in); - - /* 16 bit */ - in.sourceid = 1; - test_XIDeviceEvent(&in); - in.deviceid = 1 << 8; - test_XIDeviceEvent(&in); - in.deviceid = ~0 & 0xFF; - test_XIDeviceEvent(&in); - - /* 32 bit */ - in.root = 1L; - test_XIDeviceEvent(&in); - in.root = 1L << 8; - test_XIDeviceEvent(&in); - in.root = 1L << 16; - test_XIDeviceEvent(&in); - in.root = 1L << 24; - test_XIDeviceEvent(&in); - in.root = ~0L; - test_XIDeviceEvent(&in); - - /* 16 bit */ - in.root_x = 1; - test_XIDeviceEvent(&in); - in.root_x = 1 << 8; - test_XIDeviceEvent(&in); - in.root_x = ~0 & 0xFF; - test_XIDeviceEvent(&in); - - in.root_x_frac = 1; - test_XIDeviceEvent(&in); - in.root_x_frac = 1 << 8; - test_XIDeviceEvent(&in); - in.root_x_frac = ~0 & 0xFF; - test_XIDeviceEvent(&in); - - in.root_y = 1; - test_XIDeviceEvent(&in); - in.root_y = 1 << 8; - test_XIDeviceEvent(&in); - in.root_y = ~0 & 0xFF; - test_XIDeviceEvent(&in); - - in.root_y_frac = 1; - test_XIDeviceEvent(&in); - in.root_y_frac = 1 << 8; - test_XIDeviceEvent(&in); - in.root_y_frac = ~0 & 0xFF; - test_XIDeviceEvent(&in); - - /* 32 bit */ - in.mods.base = 1L; - test_XIDeviceEvent(&in); - in.mods.base = 1L << 8; - test_XIDeviceEvent(&in); - in.mods.base = 1L << 16; - test_XIDeviceEvent(&in); - in.mods.base = 1L << 24; - test_XIDeviceEvent(&in); - in.mods.base = ~0L; - test_XIDeviceEvent(&in); - - in.mods.latched = 1L; - test_XIDeviceEvent(&in); - in.mods.latched = 1L << 8; - test_XIDeviceEvent(&in); - in.mods.latched = 1L << 16; - test_XIDeviceEvent(&in); - in.mods.latched = 1L << 24; - test_XIDeviceEvent(&in); - in.mods.latched = ~0L; - test_XIDeviceEvent(&in); - - in.mods.locked = 1L; - test_XIDeviceEvent(&in); - in.mods.locked = 1L << 8; - test_XIDeviceEvent(&in); - in.mods.locked = 1L << 16; - test_XIDeviceEvent(&in); - in.mods.locked = 1L << 24; - test_XIDeviceEvent(&in); - in.mods.locked = ~0L; - test_XIDeviceEvent(&in); - - in.mods.effective = 1L; - test_XIDeviceEvent(&in); - in.mods.effective = 1L << 8; - test_XIDeviceEvent(&in); - in.mods.effective = 1L << 16; - test_XIDeviceEvent(&in); - in.mods.effective = 1L << 24; - test_XIDeviceEvent(&in); - in.mods.effective = ~0L; - test_XIDeviceEvent(&in); - - /* 8 bit */ - in.group.base = 1; - test_XIDeviceEvent(&in); - in.group.base = ~0 & 0xFF; - test_XIDeviceEvent(&in); - - in.group.latched = 1; - test_XIDeviceEvent(&in); - in.group.latched = ~0 & 0xFF; - test_XIDeviceEvent(&in); - - in.group.locked = 1; - test_XIDeviceEvent(&in); - in.group.locked = ~0 & 0xFF; - test_XIDeviceEvent(&in); - - in.mods.effective = 1; - test_XIDeviceEvent(&in); - in.mods.effective = ~0 & 0xFF; - test_XIDeviceEvent(&in); - - g_test_message("Testing button masks"); - for (i = 0; i < sizeof(in.buttons) * 8; i++) - { - XISetMask(in.buttons, i); - test_XIDeviceEvent(&in); - XIClearMask(in.buttons, i); - } - - for (i = 0; i < sizeof(in.buttons) * 8; i++) - { - XISetMask(in.buttons, i); - test_XIDeviceEvent(&in); - } - - g_test_message("Testing valuator masks"); - for (i = 0; i < sizeof(in.valuators.mask) * 8; i++) - { - XISetMask(in.valuators.mask, i); - test_XIDeviceEvent(&in); - XIClearMask(in.valuators.mask, i); - } - - for (i = 0; i < sizeof(in.valuators.mask) * 8; i++) - { - XISetMask(in.valuators.mask, i); - - in.valuators.data[i] = i; - in.valuators.data_frac[i] = i + 20; - test_XIDeviceEvent(&in); - XIClearMask(in.valuators.mask, i); - } - - for (i = 0; i < sizeof(in.valuators.mask) * 8; i++) - { - XISetMask(in.valuators.mask, i); - test_XIDeviceEvent(&in); - } -} - -static void test_values_XIDeviceChangedEvent(DeviceChangedEvent *in, - xXIDeviceChangedEvent *out, - BOOL swap) -{ - int i, j; - unsigned char *ptr; - - if (swap) - { - char n; - - swaps(&out->sequenceNumber, n); - swapl(&out->length, n); - swaps(&out->evtype, n); - swaps(&out->deviceid, n); - swaps(&out->sourceid, n); - swapl(&out->time, n); - swaps(&out->num_classes, n); - } - - g_assert(out->type == GenericEvent); - g_assert(out->extension == 0); /* IReqCode defaults to 0 */ - g_assert(out->evtype == GetXI2Type((InternalEvent*)in)); - g_assert(out->time == in->time); - g_assert(out->deviceid == in->deviceid); - g_assert(out->sourceid == in->sourceid); - - ptr = (unsigned char*)&out[1]; - for (i = 0; i < out->num_classes; i++) - { - xXIAnyInfo* any = (xXIAnyInfo*)ptr; - - if (swap) - { - char n; - swaps(&any->length, n); - swaps(&any->type, n); - swaps(&any->sourceid, n); - } - - switch(any->type) - { - case XIButtonClass: - { - xXIButtonInfo *b = (xXIButtonInfo*)any; - Atom *names; - - if (swap) - { - char n; - swaps(&b->num_buttons, n); - } - - g_assert(b->length == - bytes_to_int32(sizeof(xXIButtonInfo)) + - bytes_to_int32(bits_to_bytes(b->num_buttons)) + - b->num_buttons); - g_assert(b->num_buttons == in->buttons.num_buttons); - - names = (Atom*)((char*)&b[1] + - pad_to_int32(bits_to_bytes(b->num_buttons))); - for (j = 0; j < b->num_buttons; j++) - { - if (swap) - { - char n; - swapl(&names[j], n); - } - g_assert(names[j] == in->buttons.names[j]); - } - } - break; - case XIKeyClass: - { - xXIKeyInfo *k = (xXIKeyInfo*)any; - uint32_t *kc; - - if (swap) - { - char n; - swaps(&k->num_keycodes, n); - } - - g_assert(k->length == - bytes_to_int32(sizeof(xXIKeyInfo)) + - k->num_keycodes); - g_assert(k->num_keycodes == in->keys.max_keycode - - in->keys.min_keycode + 1); - - kc = (uint32_t*)&k[1]; - for (j = 0; j < k->num_keycodes; j++) - { - if (swap) - { - char n; - swapl(&kc[j], n); - } - g_assert(kc[j] >= in->keys.min_keycode); - g_assert(kc[j] <= in->keys.max_keycode); - } - } - break; - case XIValuatorClass: - { - xXIValuatorInfo *v = (xXIValuatorInfo*)any; - g_assert(v->length == - bytes_to_int32(sizeof(xXIValuatorInfo))); - - } - break; - default: - g_error("Invalid class type.\n"); - break; - } - - ptr += any->length * 4; - } - -} - -static void test_XIDeviceChangedEvent(DeviceChangedEvent *in) -{ - xXIDeviceChangedEvent *out, *swapped; - int rc; - - rc = EventToXI2((InternalEvent*)in, (xEvent**)&out); - g_assert(rc == Success); - - test_values_XIDeviceChangedEvent(in, out, FALSE); - - swapped = calloc(1, sizeof(xEvent) + out->length * 4); - XI2EventSwap((xGenericEvent*)out, (xGenericEvent*)swapped); - test_values_XIDeviceChangedEvent(in, swapped, TRUE); - - free(out); - free(swapped); -} - -static void test_convert_XIDeviceChangedEvent(void) -{ - DeviceChangedEvent in; - int i; - - g_test_message("Testing simple field values"); - memset(&in, 0, sizeof(in)); - in.header = ET_Internal; - in.type = ET_DeviceChanged; - in.length = sizeof(DeviceChangedEvent); - in.time = 0; - in.deviceid = 1; - in.sourceid = 2; - in.masterid = 3; - in.num_valuators = 4; - in.flags = DEVCHANGE_SLAVE_SWITCH | DEVCHANGE_POINTER_EVENT | DEVCHANGE_KEYBOARD_EVENT; - - for (i = 0; i < MAX_BUTTONS; i++) - in.buttons.names[i] = i + 10; - - in.keys.min_keycode = 8; - in.keys.max_keycode = 255; - - test_XIDeviceChangedEvent(&in); - - in.time = 1L; - test_XIDeviceChangedEvent(&in); - in.time = 1L << 8; - test_XIDeviceChangedEvent(&in); - in.time = 1L << 16; - test_XIDeviceChangedEvent(&in); - in.time = 1L << 24; - test_XIDeviceChangedEvent(&in); - in.time = ~0L; - test_XIDeviceChangedEvent(&in); - - in.deviceid = 1L; - test_XIDeviceChangedEvent(&in); - in.deviceid = 1L << 8; - test_XIDeviceChangedEvent(&in); - in.deviceid = ~0 & 0xFFFF; - test_XIDeviceChangedEvent(&in); - - in.sourceid = 1L; - test_XIDeviceChangedEvent(&in); - in.sourceid = 1L << 8; - test_XIDeviceChangedEvent(&in); - in.sourceid = ~0 & 0xFFFF; - test_XIDeviceChangedEvent(&in); - - in.masterid = 1L; - test_XIDeviceChangedEvent(&in); - in.masterid = 1L << 8; - test_XIDeviceChangedEvent(&in); - in.masterid = ~0 & 0xFFFF; - test_XIDeviceChangedEvent(&in); - - in.buttons.num_buttons = 0; - test_XIDeviceChangedEvent(&in); - - in.buttons.num_buttons = 1; - test_XIDeviceChangedEvent(&in); - - in.buttons.num_buttons = MAX_BUTTONS; - test_XIDeviceChangedEvent(&in); - - in.keys.min_keycode = 0; - in.keys.max_keycode = 0; - test_XIDeviceChangedEvent(&in); - - in.keys.max_keycode = 1 << 8; - test_XIDeviceChangedEvent(&in); - - in.keys.max_keycode = 0xFFFC; /* highest range, above that the length - field gives up */ - test_XIDeviceChangedEvent(&in); - - in.keys.min_keycode = 1 << 8; - in.keys.max_keycode = 1 << 8; - test_XIDeviceChangedEvent(&in); - - in.keys.min_keycode = 1 << 8; - in.keys.max_keycode = 0; - test_XIDeviceChangedEvent(&in); - - in.num_valuators = 0; - test_XIDeviceChangedEvent(&in); - - in.num_valuators = 1; - test_XIDeviceChangedEvent(&in); - - in.num_valuators = MAX_VALUATORS; - test_XIDeviceChangedEvent(&in); - - for (i = 0; i < MAX_VALUATORS; i++) - { - in.valuators[i].min = 0; - in.valuators[i].max = 0; - test_XIDeviceChangedEvent(&in); - - in.valuators[i].max = 1 << 8; - test_XIDeviceChangedEvent(&in); - in.valuators[i].max = 1 << 16; - test_XIDeviceChangedEvent(&in); - in.valuators[i].max = 1 << 24; - test_XIDeviceChangedEvent(&in); - in.valuators[i].max = abs(~0); - test_XIDeviceChangedEvent(&in); - - in.valuators[i].resolution = 1 << 8; - test_XIDeviceChangedEvent(&in); - in.valuators[i].resolution = 1 << 16; - test_XIDeviceChangedEvent(&in); - in.valuators[i].resolution = 1 << 24; - test_XIDeviceChangedEvent(&in); - in.valuators[i].resolution = abs(~0); - test_XIDeviceChangedEvent(&in); - - in.valuators[i].name = i; - test_XIDeviceChangedEvent(&in); - - in.valuators[i].mode = Relative; - test_XIDeviceChangedEvent(&in); - - in.valuators[i].mode = Absolute; - test_XIDeviceChangedEvent(&in); - } -} - -int main(int argc, char** argv) -{ - g_test_init(&argc, &argv,NULL); - g_test_bug_base("https://bugzilla.freedesktop.org/show_bug.cgi?id="); - - g_test_add_func("/xi2/eventconvert/XIRawEvent", test_convert_XIRawEvent); - g_test_add_func("/xi2/eventconvert/XIFocusEvent", test_convert_XIFocusEvent); - g_test_add_func("/xi2/eventconvert/XIDeviceEvent", test_convert_XIDeviceEvent); - g_test_add_func("/xi2/eventconvert/XIDeviceChangedEvent", test_convert_XIDeviceChangedEvent); - - return g_test_run(); -} +/** + * Copyright © 2009 Red Hat, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#ifdef HAVE_DIX_CONFIG_H +#include +#endif + +#include +#include + +#include "inputstr.h" +#include "eventstr.h" +#include "eventconvert.h" +#include "exevents.h" +#include + + +static void test_values_XIRawEvent(RawDeviceEvent *in, xXIRawEvent *out, + BOOL swap) +{ + int i; + unsigned char *ptr; + FP3232 *value, *raw_value; + int nvals = 0; + int bits_set; + int len; + + if (swap) + { + char n; + + swaps(&out->sequenceNumber, n); + swapl(&out->length, n); + swaps(&out->evtype, n); + swaps(&out->deviceid, n); + swapl(&out->time, n); + swapl(&out->detail, n); + swaps(&out->valuators_len, n); + } + + + g_assert(out->type == GenericEvent); + g_assert(out->extension == 0); /* IReqCode defaults to 0 */ + g_assert(out->evtype == GetXI2Type((InternalEvent*)in)); + g_assert(out->time == in->time); + g_assert(out->detail == in->detail.button); + g_assert(out->deviceid == in->deviceid); + g_assert(out->valuators_len >= bytes_to_int32(bits_to_bytes(sizeof(in->valuators.mask)))); + g_assert(out->flags == 0); /* FIXME: we don't set the flags yet */ + + ptr = (unsigned char*)&out[1]; + bits_set = 0; + + for (i = 0; out->valuators_len && i < sizeof(in->valuators.mask) * 8; i++) + { + g_assert (XIMaskIsSet(in->valuators.mask, i) == XIMaskIsSet(ptr, i)); + if (XIMaskIsSet(in->valuators.mask, i)) + bits_set++; + } + + /* length is len of valuator mask (in 4-byte units) + the number of bits + * set. Each bit set represents 2 8-byte values, hence the + * 'bits_set * 4' */ + len = out->valuators_len + bits_set * 4; + g_assert(out->length == len); + + nvals = 0; + + for (i = 0; out->valuators_len && i < MAX_VALUATORS; i++) + { + g_assert (XIMaskIsSet(in->valuators.mask, i) == XIMaskIsSet(ptr, i)); + if (XIMaskIsSet(in->valuators.mask, i)) + { + FP3232 vi, vo; + value = (FP3232*)(((unsigned char*)&out[1]) + out->valuators_len * 4); + value += nvals; + + vi.integral = in->valuators.data[i]; + vi.frac = in->valuators.data_frac[i]; + + vo.integral = value->integral; + vo.frac = value->frac; + if (swap) + { + char n; + swapl(&vo.integral, n); + swapl(&vo.frac, n); + } + + g_assert(vi.integral == vo.integral); + g_assert(vi.frac == vo.frac); + + raw_value = value + bits_set; + + vi.integral = in->valuators.data_raw[i]; + vi.frac = in->valuators.data_raw_frac[i]; + + vo.integral = raw_value->integral; + vo.frac = raw_value->frac; + if (swap) + { + char n; + swapl(&vo.integral, n); + swapl(&vo.frac, n); + } + + g_assert(vi.integral == vo.integral); + g_assert(vi.frac == vo.frac); + + nvals++; + } + } +} + +static void test_XIRawEvent(RawDeviceEvent *in) +{ + xXIRawEvent *out, *swapped; + int rc; + + rc = EventToXI2((InternalEvent*)in, (xEvent**)&out); + g_assert(rc == Success); + + test_values_XIRawEvent(in, out, FALSE); + + swapped = calloc(1, sizeof(xEvent) + out->length * 4); + XI2EventSwap((xGenericEvent*)out, (xGenericEvent*)swapped); + test_values_XIRawEvent(in, swapped, TRUE); + + free(out); + free(swapped); +} + +static void test_convert_XIFocusEvent(void) +{ + xEvent *out; + DeviceEvent in; + int rc; + + in.header = ET_Internal; + in.type = ET_Enter; + rc = EventToXI2((InternalEvent*)&in, &out); + g_assert(rc == Success); + g_assert(out == NULL); + + in.header = ET_Internal; + in.type = ET_FocusIn; + rc = EventToXI2((InternalEvent*)&in, &out); + g_assert(rc == Success); + g_assert(out == NULL); + + in.header = ET_Internal; + in.type = ET_FocusOut; + rc = EventToXI2((InternalEvent*)&in, &out); + g_assert(rc == BadImplementation); + + in.header = ET_Internal; + in.type = ET_Leave; + rc = EventToXI2((InternalEvent*)&in, &out); + g_assert(rc == BadImplementation); +} + + +static void test_convert_XIRawEvent(void) +{ + RawDeviceEvent in; + int i; + + memset(&in, 0, sizeof(in)); + + g_test_message("Testing all event types"); + in.header = ET_Internal; + in.type = ET_RawMotion; + test_XIRawEvent(&in); + + in.header = ET_Internal; + in.type = ET_RawKeyPress; + test_XIRawEvent(&in); + + in.header = ET_Internal; + in.type = ET_RawKeyRelease; + test_XIRawEvent(&in); + + in.header = ET_Internal; + in.type = ET_RawButtonPress; + test_XIRawEvent(&in); + + in.header = ET_Internal; + in.type = ET_RawButtonRelease; + test_XIRawEvent(&in); + + g_test_message("Testing details and other fields"); + in.detail.button = 1L; + test_XIRawEvent(&in); + in.detail.button = 1L << 8; + test_XIRawEvent(&in); + in.detail.button = 1L << 16; + test_XIRawEvent(&in); + in.detail.button = 1L << 24; + test_XIRawEvent(&in); + in.detail.button = ~0L; + test_XIRawEvent(&in); + + in.detail.button = 0; + + in.time = 1L; + test_XIRawEvent(&in); + in.time = 1L << 8; + test_XIRawEvent(&in); + in.time = 1L << 16; + test_XIRawEvent(&in); + in.time = 1L << 24; + test_XIRawEvent(&in); + in.time = ~0L; + test_XIRawEvent(&in); + + in.deviceid = 1; + test_XIRawEvent(&in); + in.deviceid = 1 << 8; + test_XIRawEvent(&in); + in.deviceid = ~0 & 0xFF; + test_XIRawEvent(&in); + + g_test_message("Testing valuator masks"); + for (i = 0; i < sizeof(in.valuators.mask) * 8; i++) + { + XISetMask(in.valuators.mask, i); + test_XIRawEvent(&in); + XIClearMask(in.valuators.mask, i); + } + + for (i = 0; i < MAX_VALUATORS; i++) + { + XISetMask(in.valuators.mask, i); + + in.valuators.data[i] = i; + in.valuators.data_raw[i] = i + 10; + in.valuators.data_frac[i] = i + 20; + in.valuators.data_raw_frac[i] = i + 30; + test_XIRawEvent(&in); + XIClearMask(in.valuators.mask, i); + } + + for (i = 0; i < sizeof(in.valuators.mask) * 8; i++) + { + XISetMask(in.valuators.mask, i); + test_XIRawEvent(&in); + } +} + +static void test_values_XIDeviceEvent(DeviceEvent *in, xXIDeviceEvent *out, + BOOL swap) +{ + int buttons, valuators; + int i; + unsigned char *ptr; + uint32_t flagmask = 0; + FP3232 *values; + + if (swap) { + char n; + + swaps(&out->sequenceNumber, n); + swapl(&out->length, n); + swaps(&out->evtype, n); + swaps(&out->deviceid, n); + swaps(&out->sourceid, n); + swapl(&out->time, n); + swapl(&out->detail, n); + swapl(&out->root, n); + swapl(&out->event, n); + swapl(&out->child, n); + swapl(&out->root_x, n); + swapl(&out->root_y, n); + swapl(&out->event_x, n); + swapl(&out->event_y, n); + swaps(&out->buttons_len, n); + swaps(&out->valuators_len, n); + swapl(&out->mods.base_mods, n); + swapl(&out->mods.latched_mods, n); + swapl(&out->mods.locked_mods, n); + swapl(&out->mods.effective_mods, n); + swapl(&out->flags, n); + } + + g_assert(out->extension == 0); /* IReqCode defaults to 0 */ + g_assert(out->evtype == GetXI2Type((InternalEvent*)in)); + g_assert(out->time == in->time); + g_assert(out->detail == in->detail.button); + g_assert(out->length >= 12); + + g_assert(out->deviceid == in->deviceid); + g_assert(out->sourceid == in->sourceid); + + switch (in->type) { + case ET_KeyPress: + flagmask = XIKeyRepeat; + break; + default: + flagmask = 0; + break; + } + g_assert((out->flags & ~flagmask) == 0); + + g_assert(out->root == in->root); + g_assert(out->event == None); /* set in FixUpEventFromWindow */ + g_assert(out->child == None); /* set in FixUpEventFromWindow */ + + g_assert(out->mods.base_mods == in->mods.base); + g_assert(out->mods.latched_mods == in->mods.latched); + g_assert(out->mods.locked_mods == in->mods.locked); + g_assert(out->mods.effective_mods == in->mods.effective); + + g_assert(out->group.base_group == in->group.base); + g_assert(out->group.latched_group == in->group.latched); + g_assert(out->group.locked_group == in->group.locked); + g_assert(out->group.effective_group == in->group.effective); + + g_assert(out->event_x == 0); /* set in FixUpEventFromWindow */ + g_assert(out->event_y == 0); /* set in FixUpEventFromWindow */ + + g_assert(out->root_x == FP1616(in->root_x, in->root_x_frac)); + g_assert(out->root_y == FP1616(in->root_y, in->root_y_frac)); + + buttons = 0; + for (i = 0; i < bits_to_bytes(sizeof(in->buttons)); i++) + { + if (XIMaskIsSet(in->buttons, i)) + { + g_assert(out->buttons_len >= bytes_to_int32(bits_to_bytes(i))); + buttons++; + } + } + + ptr = (unsigned char*)&out[1]; + for (i = 0; i < sizeof(in->buttons) * 8; i++) + g_assert(XIMaskIsSet(in->buttons, i) == XIMaskIsSet(ptr, i)); + + + valuators = 0; + for (i = 0; i < sizeof(in->valuators.mask) * 8; i++) + if (XIMaskIsSet(in->valuators.mask, i)) + valuators++; + + g_assert(out->valuators_len >= bytes_to_int32(bits_to_bytes(valuators))); + + ptr += out->buttons_len * 4; + values = (FP3232*)(ptr + out->valuators_len * 4); + for (i = 0; i < sizeof(in->valuators.mask) * 8 || + i < (out->valuators_len * 4) * 8; i++) + { + if (i > sizeof(in->valuators.mask) * 8) + g_assert(!XIMaskIsSet(ptr, i)); + else if (i > out->valuators_len * 4 * 8) + g_assert(!XIMaskIsSet(in->valuators.mask, i)); + else { + g_assert(XIMaskIsSet(in->valuators.mask, i) == + XIMaskIsSet(ptr, i)); + + if (XIMaskIsSet(ptr, i)) + { + FP3232 vi, vo; + + vi.integral = in->valuators.data[i]; + vi.frac = in->valuators.data_frac[i]; + + vo = *values; + + if (swap) + { + char n; + swapl(&vo.integral, n); + swapl(&vo.frac, n); + } + + + g_assert(vi.integral == vo.integral); + g_assert(vi.frac == vo.frac); + values++; + } + } + } +} + +static void test_XIDeviceEvent(DeviceEvent *in) +{ + xXIDeviceEvent *out, *swapped; + int rc; + + rc = EventToXI2((InternalEvent*)in, (xEvent**)&out); + g_assert(rc == Success); + + test_values_XIDeviceEvent(in, out, FALSE); + + swapped = calloc(1, sizeof(xEvent) + out->length * 4); + XI2EventSwap((xGenericEvent*)out, (xGenericEvent*)swapped); + test_values_XIDeviceEvent(in, swapped, TRUE); + + free(out); + free(swapped); +} + +static void test_convert_XIDeviceEvent(void) +{ + DeviceEvent in; + int i; + + memset(&in, 0, sizeof(in)); + + g_test_message("Testing simple field values"); + in.header = ET_Internal; + in.type = ET_Motion; + in.length = sizeof(DeviceEvent); + in.time = 0; + in.deviceid = 1; + in.sourceid = 2; + in.root = 3; + in.root_x = 4; + in.root_x_frac = 5; + in.root_y = 6; + in.root_y_frac = 7; + in.detail.button = 8; + in.mods.base = 9; + in.mods.latched = 10; + in.mods.locked = 11; + in.mods.effective = 11; + in.group.base = 12; + in.group.latched = 13; + in.group.locked = 14; + in.group.effective = 15; + + test_XIDeviceEvent(&in); + + g_test_message("Testing field ranges"); + /* 32 bit */ + in.detail.button = 1L; + test_XIDeviceEvent(&in); + in.detail.button = 1L << 8; + test_XIDeviceEvent(&in); + in.detail.button = 1L << 16; + test_XIDeviceEvent(&in); + in.detail.button = 1L << 24; + test_XIDeviceEvent(&in); + in.detail.button = ~0L; + test_XIDeviceEvent(&in); + + /* 32 bit */ + in.time = 1L; + test_XIDeviceEvent(&in); + in.time = 1L << 8; + test_XIDeviceEvent(&in); + in.time = 1L << 16; + test_XIDeviceEvent(&in); + in.time = 1L << 24; + test_XIDeviceEvent(&in); + in.time = ~0L; + test_XIDeviceEvent(&in); + + /* 16 bit */ + in.deviceid = 1; + test_XIDeviceEvent(&in); + in.deviceid = 1 << 8; + test_XIDeviceEvent(&in); + in.deviceid = ~0 & 0xFF; + test_XIDeviceEvent(&in); + + /* 16 bit */ + in.sourceid = 1; + test_XIDeviceEvent(&in); + in.deviceid = 1 << 8; + test_XIDeviceEvent(&in); + in.deviceid = ~0 & 0xFF; + test_XIDeviceEvent(&in); + + /* 32 bit */ + in.root = 1L; + test_XIDeviceEvent(&in); + in.root = 1L << 8; + test_XIDeviceEvent(&in); + in.root = 1L << 16; + test_XIDeviceEvent(&in); + in.root = 1L << 24; + test_XIDeviceEvent(&in); + in.root = ~0L; + test_XIDeviceEvent(&in); + + /* 16 bit */ + in.root_x = 1; + test_XIDeviceEvent(&in); + in.root_x = 1 << 8; + test_XIDeviceEvent(&in); + in.root_x = ~0 & 0xFF; + test_XIDeviceEvent(&in); + + in.root_x_frac = 1; + test_XIDeviceEvent(&in); + in.root_x_frac = 1 << 8; + test_XIDeviceEvent(&in); + in.root_x_frac = ~0 & 0xFF; + test_XIDeviceEvent(&in); + + in.root_y = 1; + test_XIDeviceEvent(&in); + in.root_y = 1 << 8; + test_XIDeviceEvent(&in); + in.root_y = ~0 & 0xFF; + test_XIDeviceEvent(&in); + + in.root_y_frac = 1; + test_XIDeviceEvent(&in); + in.root_y_frac = 1 << 8; + test_XIDeviceEvent(&in); + in.root_y_frac = ~0 & 0xFF; + test_XIDeviceEvent(&in); + + /* 32 bit */ + in.mods.base = 1L; + test_XIDeviceEvent(&in); + in.mods.base = 1L << 8; + test_XIDeviceEvent(&in); + in.mods.base = 1L << 16; + test_XIDeviceEvent(&in); + in.mods.base = 1L << 24; + test_XIDeviceEvent(&in); + in.mods.base = ~0L; + test_XIDeviceEvent(&in); + + in.mods.latched = 1L; + test_XIDeviceEvent(&in); + in.mods.latched = 1L << 8; + test_XIDeviceEvent(&in); + in.mods.latched = 1L << 16; + test_XIDeviceEvent(&in); + in.mods.latched = 1L << 24; + test_XIDeviceEvent(&in); + in.mods.latched = ~0L; + test_XIDeviceEvent(&in); + + in.mods.locked = 1L; + test_XIDeviceEvent(&in); + in.mods.locked = 1L << 8; + test_XIDeviceEvent(&in); + in.mods.locked = 1L << 16; + test_XIDeviceEvent(&in); + in.mods.locked = 1L << 24; + test_XIDeviceEvent(&in); + in.mods.locked = ~0L; + test_XIDeviceEvent(&in); + + in.mods.effective = 1L; + test_XIDeviceEvent(&in); + in.mods.effective = 1L << 8; + test_XIDeviceEvent(&in); + in.mods.effective = 1L << 16; + test_XIDeviceEvent(&in); + in.mods.effective = 1L << 24; + test_XIDeviceEvent(&in); + in.mods.effective = ~0L; + test_XIDeviceEvent(&in); + + /* 8 bit */ + in.group.base = 1; + test_XIDeviceEvent(&in); + in.group.base = ~0 & 0xFF; + test_XIDeviceEvent(&in); + + in.group.latched = 1; + test_XIDeviceEvent(&in); + in.group.latched = ~0 & 0xFF; + test_XIDeviceEvent(&in); + + in.group.locked = 1; + test_XIDeviceEvent(&in); + in.group.locked = ~0 & 0xFF; + test_XIDeviceEvent(&in); + + in.mods.effective = 1; + test_XIDeviceEvent(&in); + in.mods.effective = ~0 & 0xFF; + test_XIDeviceEvent(&in); + + g_test_message("Testing button masks"); + for (i = 0; i < sizeof(in.buttons) * 8; i++) + { + XISetMask(in.buttons, i); + test_XIDeviceEvent(&in); + XIClearMask(in.buttons, i); + } + + for (i = 0; i < sizeof(in.buttons) * 8; i++) + { + XISetMask(in.buttons, i); + test_XIDeviceEvent(&in); + } + + g_test_message("Testing valuator masks"); + for (i = 0; i < sizeof(in.valuators.mask) * 8; i++) + { + XISetMask(in.valuators.mask, i); + test_XIDeviceEvent(&in); + XIClearMask(in.valuators.mask, i); + } + + for (i = 0; i < sizeof(in.valuators.mask) * 8; i++) + { + XISetMask(in.valuators.mask, i); + + in.valuators.data[i] = i; + in.valuators.data_frac[i] = i + 20; + test_XIDeviceEvent(&in); + XIClearMask(in.valuators.mask, i); + } + + for (i = 0; i < sizeof(in.valuators.mask) * 8; i++) + { + XISetMask(in.valuators.mask, i); + test_XIDeviceEvent(&in); + } +} + +static void test_values_XIDeviceChangedEvent(DeviceChangedEvent *in, + xXIDeviceChangedEvent *out, + BOOL swap) +{ + int i, j; + unsigned char *ptr; + + if (swap) + { + char n; + + swaps(&out->sequenceNumber, n); + swapl(&out->length, n); + swaps(&out->evtype, n); + swaps(&out->deviceid, n); + swaps(&out->sourceid, n); + swapl(&out->time, n); + swaps(&out->num_classes, n); + } + + g_assert(out->type == GenericEvent); + g_assert(out->extension == 0); /* IReqCode defaults to 0 */ + g_assert(out->evtype == GetXI2Type((InternalEvent*)in)); + g_assert(out->time == in->time); + g_assert(out->deviceid == in->deviceid); + g_assert(out->sourceid == in->sourceid); + + ptr = (unsigned char*)&out[1]; + for (i = 0; i < out->num_classes; i++) + { + xXIAnyInfo* any = (xXIAnyInfo*)ptr; + + if (swap) + { + char n; + swaps(&any->length, n); + swaps(&any->type, n); + swaps(&any->sourceid, n); + } + + switch(any->type) + { + case XIButtonClass: + { + xXIButtonInfo *b = (xXIButtonInfo*)any; + Atom *names; + + if (swap) + { + char n; + swaps(&b->num_buttons, n); + } + + g_assert(b->length == + bytes_to_int32(sizeof(xXIButtonInfo)) + + bytes_to_int32(bits_to_bytes(b->num_buttons)) + + b->num_buttons); + g_assert(b->num_buttons == in->buttons.num_buttons); + + names = (Atom*)((char*)&b[1] + + pad_to_int32(bits_to_bytes(b->num_buttons))); + for (j = 0; j < b->num_buttons; j++) + { + if (swap) + { + char n; + swapl(&names[j], n); + } + g_assert(names[j] == in->buttons.names[j]); + } + } + break; + case XIKeyClass: + { + xXIKeyInfo *k = (xXIKeyInfo*)any; + uint32_t *kc; + + if (swap) + { + char n; + swaps(&k->num_keycodes, n); + } + + g_assert(k->length == + bytes_to_int32(sizeof(xXIKeyInfo)) + + k->num_keycodes); + g_assert(k->num_keycodes == in->keys.max_keycode - + in->keys.min_keycode + 1); + + kc = (uint32_t*)&k[1]; + for (j = 0; j < k->num_keycodes; j++) + { + if (swap) + { + char n; + swapl(&kc[j], n); + } + g_assert(kc[j] >= in->keys.min_keycode); + g_assert(kc[j] <= in->keys.max_keycode); + } + } + break; + case XIValuatorClass: + { + xXIValuatorInfo *v = (xXIValuatorInfo*)any; + g_assert(v->length == + bytes_to_int32(sizeof(xXIValuatorInfo))); + + } + break; + default: + g_error("Invalid class type.\n"); + break; + } + + ptr += any->length * 4; + } + +} + +static void test_XIDeviceChangedEvent(DeviceChangedEvent *in) +{ + xXIDeviceChangedEvent *out, *swapped; + int rc; + + rc = EventToXI2((InternalEvent*)in, (xEvent**)&out); + g_assert(rc == Success); + + test_values_XIDeviceChangedEvent(in, out, FALSE); + + swapped = calloc(1, sizeof(xEvent) + out->length * 4); + XI2EventSwap((xGenericEvent*)out, (xGenericEvent*)swapped); + test_values_XIDeviceChangedEvent(in, swapped, TRUE); + + free(out); + free(swapped); +} + +static void test_convert_XIDeviceChangedEvent(void) +{ + DeviceChangedEvent in; + int i; + + g_test_message("Testing simple field values"); + memset(&in, 0, sizeof(in)); + in.header = ET_Internal; + in.type = ET_DeviceChanged; + in.length = sizeof(DeviceChangedEvent); + in.time = 0; + in.deviceid = 1; + in.sourceid = 2; + in.masterid = 3; + in.num_valuators = 4; + in.flags = DEVCHANGE_SLAVE_SWITCH | DEVCHANGE_POINTER_EVENT | DEVCHANGE_KEYBOARD_EVENT; + + for (i = 0; i < MAX_BUTTONS; i++) + in.buttons.names[i] = i + 10; + + in.keys.min_keycode = 8; + in.keys.max_keycode = 255; + + test_XIDeviceChangedEvent(&in); + + in.time = 1L; + test_XIDeviceChangedEvent(&in); + in.time = 1L << 8; + test_XIDeviceChangedEvent(&in); + in.time = 1L << 16; + test_XIDeviceChangedEvent(&in); + in.time = 1L << 24; + test_XIDeviceChangedEvent(&in); + in.time = ~0L; + test_XIDeviceChangedEvent(&in); + + in.deviceid = 1L; + test_XIDeviceChangedEvent(&in); + in.deviceid = 1L << 8; + test_XIDeviceChangedEvent(&in); + in.deviceid = ~0 & 0xFFFF; + test_XIDeviceChangedEvent(&in); + + in.sourceid = 1L; + test_XIDeviceChangedEvent(&in); + in.sourceid = 1L << 8; + test_XIDeviceChangedEvent(&in); + in.sourceid = ~0 & 0xFFFF; + test_XIDeviceChangedEvent(&in); + + in.masterid = 1L; + test_XIDeviceChangedEvent(&in); + in.masterid = 1L << 8; + test_XIDeviceChangedEvent(&in); + in.masterid = ~0 & 0xFFFF; + test_XIDeviceChangedEvent(&in); + + in.buttons.num_buttons = 0; + test_XIDeviceChangedEvent(&in); + + in.buttons.num_buttons = 1; + test_XIDeviceChangedEvent(&in); + + in.buttons.num_buttons = MAX_BUTTONS; + test_XIDeviceChangedEvent(&in); + + in.keys.min_keycode = 0; + in.keys.max_keycode = 0; + test_XIDeviceChangedEvent(&in); + + in.keys.max_keycode = 1 << 8; + test_XIDeviceChangedEvent(&in); + + in.keys.max_keycode = 0xFFFC; /* highest range, above that the length + field gives up */ + test_XIDeviceChangedEvent(&in); + + in.keys.min_keycode = 1 << 8; + in.keys.max_keycode = 1 << 8; + test_XIDeviceChangedEvent(&in); + + in.keys.min_keycode = 1 << 8; + in.keys.max_keycode = 0; + test_XIDeviceChangedEvent(&in); + + in.num_valuators = 0; + test_XIDeviceChangedEvent(&in); + + in.num_valuators = 1; + test_XIDeviceChangedEvent(&in); + + in.num_valuators = MAX_VALUATORS; + test_XIDeviceChangedEvent(&in); + + for (i = 0; i < MAX_VALUATORS; i++) + { + in.valuators[i].min = 0; + in.valuators[i].max = 0; + test_XIDeviceChangedEvent(&in); + + in.valuators[i].max = 1 << 8; + test_XIDeviceChangedEvent(&in); + in.valuators[i].max = 1 << 16; + test_XIDeviceChangedEvent(&in); + in.valuators[i].max = 1 << 24; + test_XIDeviceChangedEvent(&in); + in.valuators[i].max = abs(~0); + test_XIDeviceChangedEvent(&in); + + in.valuators[i].resolution = 1 << 8; + test_XIDeviceChangedEvent(&in); + in.valuators[i].resolution = 1 << 16; + test_XIDeviceChangedEvent(&in); + in.valuators[i].resolution = 1 << 24; + test_XIDeviceChangedEvent(&in); + in.valuators[i].resolution = abs(~0); + test_XIDeviceChangedEvent(&in); + + in.valuators[i].name = i; + test_XIDeviceChangedEvent(&in); + + in.valuators[i].mode = Relative; + test_XIDeviceChangedEvent(&in); + + in.valuators[i].mode = Absolute; + test_XIDeviceChangedEvent(&in); + } +} + +int main(int argc, char** argv) +{ + g_test_init(&argc, &argv,NULL); + g_test_bug_base("https://bugzilla.freedesktop.org/show_bug.cgi?id="); + + g_test_add_func("/xi2/eventconvert/XIRawEvent", test_convert_XIRawEvent); + g_test_add_func("/xi2/eventconvert/XIFocusEvent", test_convert_XIFocusEvent); + g_test_add_func("/xi2/eventconvert/XIDeviceEvent", test_convert_XIDeviceEvent); + g_test_add_func("/xi2/eventconvert/XIDeviceChangedEvent", test_convert_XIDeviceChangedEvent); + + return g_test_run(); +} diff --git a/xorg-server/test/xi2/protocol-xiselectevents.c b/xorg-server/test/xi2/protocol-xiselectevents.c index fe1c26df8..f951a14fe 100644 --- a/xorg-server/test/xi2/protocol-xiselectevents.c +++ b/xorg-server/test/xi2/protocol-xiselectevents.c @@ -131,7 +131,7 @@ static void request_XISelectEvents_masks(xXISelectEventsReq *req) { int i, j; xXIEventMask *mask; - int nmasks = (XI_LASTEVENT + 7)/8; + int nmasks = (XI2LASTEVENT + 7)/8; unsigned char *bits; mask = (xXIEventMask*)&req[1]; @@ -150,14 +150,14 @@ static void request_XISelectEvents_masks(xXISelectEventsReq *req) request_XISelectEvent(req, Success); /* Test 1: - * mask may be larger than needed for XI_LASTEVENT. + * mask may be larger than needed for XI2LASTEVENT. * Test setting each valid mask bit, while leaving unneeded bits 0. * -> Success */ bits = (unsigned char*)&mask[1]; mask->mask_len = (nmasks + 3)/4 * 10; memset(bits, 0, mask->mask_len * 4); - for (j = 0; j <= XI_LASTEVENT; j++) + for (j = 0; j <= XI2LASTEVENT; j++) { SetBit(bits, j); request_XISelectEvent(req, Success); @@ -165,7 +165,7 @@ static void request_XISelectEvents_masks(xXISelectEventsReq *req) } /* Test 2: - * mask may be larger than needed for XI_LASTEVENT. + * mask may be larger than needed for XI2LASTEVENT. * Test setting all valid mask bits, while leaving unneeded bits 0. * -> Success */ @@ -173,21 +173,21 @@ static void request_XISelectEvents_masks(xXISelectEventsReq *req) mask->mask_len = (nmasks + 3)/4 * 10; memset(bits, 0, mask->mask_len * 4); - for (j = 0; j <= XI_LASTEVENT; j++) + for (j = 0; j <= XI2LASTEVENT; j++) { SetBit(bits, j); request_XISelectEvent(req, Success); } /* Test 3: - * mask is larger than needed for XI_LASTEVENT. If any unneeded bit + * mask is larger than needed for XI2LASTEVENT. If any unneeded bit * is set -> BadValue */ bits = (unsigned char*)&mask[1]; mask->mask_len = (nmasks + 3)/4 * 10; memset(bits, 0, mask->mask_len * 4); - for (j = XI_LASTEVENT + 1; j < mask->mask_len * 4; j++) + for (j = XI2LASTEVENT + 1; j < mask->mask_len * 4; j++) { SetBit(bits, j); request_XISelectEvent(req, BadValue); @@ -200,7 +200,7 @@ static void request_XISelectEvents_masks(xXISelectEventsReq *req) bits = (unsigned char*)&mask[1]; mask->mask_len = (nmasks + 3)/4; memset(bits, 0, mask->mask_len * 4); - for (j = 0; j <= XI_LASTEVENT; j++) + for (j = 0; j <= XI2LASTEVENT; j++) { SetBit(bits, j); request_XISelectEvent(req, Success); @@ -228,7 +228,7 @@ static void request_XISelectEvents_masks(xXISelectEventsReq *req) bits = (unsigned char*)&mask[1]; mask->mask_len = (nmasks + 3)/4; memset(bits, 0, mask->mask_len * 4); - for (j = 0; j <= XI_LASTEVENT; j++) + for (j = 0; j <= XI2LASTEVENT; j++) SetBit(bits, j); ClearBit(bits, XI_HierarchyChanged); for (j = 1; j < 6; j++) -- cgit v1.2.3