1706f2543Smrg/** 2706f2543Smrg * Copyright © 2009 Red Hat, Inc. 3706f2543Smrg * 4706f2543Smrg * Permission is hereby granted, free of charge, to any person obtaining a 5706f2543Smrg * copy of this software and associated documentation files (the "Software"), 6706f2543Smrg * to deal in the Software without restriction, including without limitation 7706f2543Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8706f2543Smrg * and/or sell copies of the Software, and to permit persons to whom the 9706f2543Smrg * Software is furnished to do so, subject to the following conditions: 10706f2543Smrg * 11706f2543Smrg * The above copyright notice and this permission notice (including the next 12706f2543Smrg * paragraph) shall be included in all copies or substantial portions of the 13706f2543Smrg * Software. 14706f2543Smrg * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15706f2543Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16706f2543Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17706f2543Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18706f2543Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19706f2543Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20706f2543Smrg * DEALINGS IN THE SOFTWARE. 21706f2543Smrg */ 22706f2543Smrg 23706f2543Smrg#ifdef HAVE_DIX_CONFIG_H 24706f2543Smrg#include <dix-config.h> 25706f2543Smrg#endif 26706f2543Smrg 27706f2543Smrg#include <stdint.h> 28706f2543Smrg#include <glib.h> 29706f2543Smrg 30706f2543Smrg#include "inputstr.h" 31706f2543Smrg#include "eventstr.h" 32706f2543Smrg#include "eventconvert.h" 33706f2543Smrg#include "exevents.h" 34706f2543Smrg#include <X11/extensions/XI2proto.h> 35706f2543Smrg 36706f2543Smrg 37706f2543Smrgstatic void test_values_XIRawEvent(RawDeviceEvent *in, xXIRawEvent *out, 38706f2543Smrg BOOL swap) 39706f2543Smrg{ 40706f2543Smrg int i; 41706f2543Smrg unsigned char *ptr; 42706f2543Smrg FP3232 *value, *raw_value; 43706f2543Smrg int nvals = 0; 44706f2543Smrg int bits_set; 45706f2543Smrg int len; 46706f2543Smrg 47706f2543Smrg if (swap) 48706f2543Smrg { 49706f2543Smrg char n; 50706f2543Smrg 51706f2543Smrg swaps(&out->sequenceNumber, n); 52706f2543Smrg swapl(&out->length, n); 53706f2543Smrg swaps(&out->evtype, n); 54706f2543Smrg swaps(&out->deviceid, n); 55706f2543Smrg swapl(&out->time, n); 56706f2543Smrg swapl(&out->detail, n); 57706f2543Smrg swaps(&out->valuators_len, n); 58706f2543Smrg } 59706f2543Smrg 60706f2543Smrg 61706f2543Smrg g_assert(out->type == GenericEvent); 62706f2543Smrg g_assert(out->extension == 0); /* IReqCode defaults to 0 */ 63706f2543Smrg g_assert(out->evtype == GetXI2Type((InternalEvent*)in)); 64706f2543Smrg g_assert(out->time == in->time); 65706f2543Smrg g_assert(out->detail == in->detail.button); 66706f2543Smrg g_assert(out->deviceid == in->deviceid); 67706f2543Smrg g_assert(out->valuators_len >= bytes_to_int32(bits_to_bytes(sizeof(in->valuators.mask)))); 68706f2543Smrg g_assert(out->flags == 0); /* FIXME: we don't set the flags yet */ 69706f2543Smrg 70706f2543Smrg ptr = (unsigned char*)&out[1]; 71706f2543Smrg bits_set = 0; 72706f2543Smrg 73706f2543Smrg for (i = 0; out->valuators_len && i < sizeof(in->valuators.mask) * 8; i++) 74706f2543Smrg { 75706f2543Smrg g_assert (XIMaskIsSet(in->valuators.mask, i) == XIMaskIsSet(ptr, i)); 76706f2543Smrg if (XIMaskIsSet(in->valuators.mask, i)) 77706f2543Smrg bits_set++; 78706f2543Smrg } 79706f2543Smrg 80706f2543Smrg /* length is len of valuator mask (in 4-byte units) + the number of bits 81706f2543Smrg * set. Each bit set represents 2 8-byte values, hence the 82706f2543Smrg * 'bits_set * 4' */ 83706f2543Smrg len = out->valuators_len + bits_set * 4; 84706f2543Smrg g_assert(out->length == len); 85706f2543Smrg 86706f2543Smrg nvals = 0; 87706f2543Smrg 88706f2543Smrg for (i = 0; out->valuators_len && i < MAX_VALUATORS; i++) 89706f2543Smrg { 90706f2543Smrg g_assert (XIMaskIsSet(in->valuators.mask, i) == XIMaskIsSet(ptr, i)); 91706f2543Smrg if (XIMaskIsSet(in->valuators.mask, i)) 92706f2543Smrg { 93706f2543Smrg FP3232 vi, vo; 94706f2543Smrg value = (FP3232*)(((unsigned char*)&out[1]) + out->valuators_len * 4); 95706f2543Smrg value += nvals; 96706f2543Smrg 97706f2543Smrg vi.integral = in->valuators.data[i]; 98706f2543Smrg vi.frac = in->valuators.data_frac[i]; 99706f2543Smrg 100706f2543Smrg vo.integral = value->integral; 101706f2543Smrg vo.frac = value->frac; 102706f2543Smrg if (swap) 103706f2543Smrg { 104706f2543Smrg char n; 105706f2543Smrg swapl(&vo.integral, n); 106706f2543Smrg swapl(&vo.frac, n); 107706f2543Smrg } 108706f2543Smrg 109706f2543Smrg g_assert(vi.integral == vo.integral); 110706f2543Smrg g_assert(vi.frac == vo.frac); 111706f2543Smrg 112706f2543Smrg raw_value = value + bits_set; 113706f2543Smrg 114706f2543Smrg vi.integral = in->valuators.data_raw[i]; 115706f2543Smrg vi.frac = in->valuators.data_raw_frac[i]; 116706f2543Smrg 117706f2543Smrg vo.integral = raw_value->integral; 118706f2543Smrg vo.frac = raw_value->frac; 119706f2543Smrg if (swap) 120706f2543Smrg { 121706f2543Smrg char n; 122706f2543Smrg swapl(&vo.integral, n); 123706f2543Smrg swapl(&vo.frac, n); 124706f2543Smrg } 125706f2543Smrg 126706f2543Smrg g_assert(vi.integral == vo.integral); 127706f2543Smrg g_assert(vi.frac == vo.frac); 128706f2543Smrg 129706f2543Smrg nvals++; 130706f2543Smrg } 131706f2543Smrg } 132706f2543Smrg} 133706f2543Smrg 134706f2543Smrgstatic void test_XIRawEvent(RawDeviceEvent *in) 135706f2543Smrg{ 136706f2543Smrg xXIRawEvent *out, *swapped; 137706f2543Smrg int rc; 138706f2543Smrg 139706f2543Smrg rc = EventToXI2((InternalEvent*)in, (xEvent**)&out); 140706f2543Smrg g_assert(rc == Success); 141706f2543Smrg 142706f2543Smrg test_values_XIRawEvent(in, out, FALSE); 143706f2543Smrg 144706f2543Smrg swapped = calloc(1, sizeof(xEvent) + out->length * 4); 145706f2543Smrg XI2EventSwap((xGenericEvent*)out, (xGenericEvent*)swapped); 146706f2543Smrg test_values_XIRawEvent(in, swapped, TRUE); 147706f2543Smrg 148706f2543Smrg free(out); 149706f2543Smrg free(swapped); 150706f2543Smrg} 151706f2543Smrg 152706f2543Smrgstatic void test_convert_XIFocusEvent(void) 153706f2543Smrg{ 154706f2543Smrg xEvent *out; 155706f2543Smrg DeviceEvent in; 156706f2543Smrg int rc; 157706f2543Smrg 158706f2543Smrg in.header = ET_Internal; 159706f2543Smrg in.type = ET_Enter; 160706f2543Smrg rc = EventToXI2((InternalEvent*)&in, &out); 161706f2543Smrg g_assert(rc == Success); 162706f2543Smrg g_assert(out == NULL); 163706f2543Smrg 164706f2543Smrg in.header = ET_Internal; 165706f2543Smrg in.type = ET_FocusIn; 166706f2543Smrg rc = EventToXI2((InternalEvent*)&in, &out); 167706f2543Smrg g_assert(rc == Success); 168706f2543Smrg g_assert(out == NULL); 169706f2543Smrg 170706f2543Smrg in.header = ET_Internal; 171706f2543Smrg in.type = ET_FocusOut; 172706f2543Smrg rc = EventToXI2((InternalEvent*)&in, &out); 173706f2543Smrg g_assert(rc == BadImplementation); 174706f2543Smrg 175706f2543Smrg in.header = ET_Internal; 176706f2543Smrg in.type = ET_Leave; 177706f2543Smrg rc = EventToXI2((InternalEvent*)&in, &out); 178706f2543Smrg g_assert(rc == BadImplementation); 179706f2543Smrg} 180706f2543Smrg 181706f2543Smrg 182706f2543Smrgstatic void test_convert_XIRawEvent(void) 183706f2543Smrg{ 184706f2543Smrg RawDeviceEvent in; 185706f2543Smrg int i; 186706f2543Smrg 187706f2543Smrg memset(&in, 0, sizeof(in)); 188706f2543Smrg 189706f2543Smrg g_test_message("Testing all event types"); 190706f2543Smrg in.header = ET_Internal; 191706f2543Smrg in.type = ET_RawMotion; 192706f2543Smrg test_XIRawEvent(&in); 193706f2543Smrg 194706f2543Smrg in.header = ET_Internal; 195706f2543Smrg in.type = ET_RawKeyPress; 196706f2543Smrg test_XIRawEvent(&in); 197706f2543Smrg 198706f2543Smrg in.header = ET_Internal; 199706f2543Smrg in.type = ET_RawKeyRelease; 200706f2543Smrg test_XIRawEvent(&in); 201706f2543Smrg 202706f2543Smrg in.header = ET_Internal; 203706f2543Smrg in.type = ET_RawButtonPress; 204706f2543Smrg test_XIRawEvent(&in); 205706f2543Smrg 206706f2543Smrg in.header = ET_Internal; 207706f2543Smrg in.type = ET_RawButtonRelease; 208706f2543Smrg test_XIRawEvent(&in); 209706f2543Smrg 210706f2543Smrg g_test_message("Testing details and other fields"); 211706f2543Smrg in.detail.button = 1L; 212706f2543Smrg test_XIRawEvent(&in); 213706f2543Smrg in.detail.button = 1L << 8; 214706f2543Smrg test_XIRawEvent(&in); 215706f2543Smrg in.detail.button = 1L << 16; 216706f2543Smrg test_XIRawEvent(&in); 217706f2543Smrg in.detail.button = 1L << 24; 218706f2543Smrg test_XIRawEvent(&in); 219706f2543Smrg in.detail.button = ~0L; 220706f2543Smrg test_XIRawEvent(&in); 221706f2543Smrg 222706f2543Smrg in.detail.button = 0; 223706f2543Smrg 224706f2543Smrg in.time = 1L; 225706f2543Smrg test_XIRawEvent(&in); 226706f2543Smrg in.time = 1L << 8; 227706f2543Smrg test_XIRawEvent(&in); 228706f2543Smrg in.time = 1L << 16; 229706f2543Smrg test_XIRawEvent(&in); 230706f2543Smrg in.time = 1L << 24; 231706f2543Smrg test_XIRawEvent(&in); 232706f2543Smrg in.time = ~0L; 233706f2543Smrg test_XIRawEvent(&in); 234706f2543Smrg 235706f2543Smrg in.deviceid = 1; 236706f2543Smrg test_XIRawEvent(&in); 237706f2543Smrg in.deviceid = 1 << 8; 238706f2543Smrg test_XIRawEvent(&in); 239706f2543Smrg in.deviceid = ~0 & 0xFF; 240706f2543Smrg test_XIRawEvent(&in); 241706f2543Smrg 242706f2543Smrg g_test_message("Testing valuator masks"); 243706f2543Smrg for (i = 0; i < sizeof(in.valuators.mask) * 8; i++) 244706f2543Smrg { 245706f2543Smrg XISetMask(in.valuators.mask, i); 246706f2543Smrg test_XIRawEvent(&in); 247706f2543Smrg XIClearMask(in.valuators.mask, i); 248706f2543Smrg } 249706f2543Smrg 250706f2543Smrg for (i = 0; i < MAX_VALUATORS; i++) 251706f2543Smrg { 252706f2543Smrg XISetMask(in.valuators.mask, i); 253706f2543Smrg 254706f2543Smrg in.valuators.data[i] = i; 255706f2543Smrg in.valuators.data_raw[i] = i + 10; 256706f2543Smrg in.valuators.data_frac[i] = i + 20; 257706f2543Smrg in.valuators.data_raw_frac[i] = i + 30; 258706f2543Smrg test_XIRawEvent(&in); 259706f2543Smrg XIClearMask(in.valuators.mask, i); 260706f2543Smrg } 261706f2543Smrg 262706f2543Smrg for (i = 0; i < sizeof(in.valuators.mask) * 8; i++) 263706f2543Smrg { 264706f2543Smrg XISetMask(in.valuators.mask, i); 265706f2543Smrg test_XIRawEvent(&in); 266706f2543Smrg } 267706f2543Smrg} 268706f2543Smrg 269706f2543Smrgstatic void test_values_XIDeviceEvent(DeviceEvent *in, xXIDeviceEvent *out, 270706f2543Smrg BOOL swap) 271706f2543Smrg{ 272706f2543Smrg int buttons, valuators; 273706f2543Smrg int i; 274706f2543Smrg unsigned char *ptr; 275706f2543Smrg uint32_t flagmask = 0; 276706f2543Smrg FP3232 *values; 277706f2543Smrg 278706f2543Smrg if (swap) { 279706f2543Smrg char n; 280706f2543Smrg 281706f2543Smrg swaps(&out->sequenceNumber, n); 282706f2543Smrg swapl(&out->length, n); 283706f2543Smrg swaps(&out->evtype, n); 284706f2543Smrg swaps(&out->deviceid, n); 285706f2543Smrg swaps(&out->sourceid, n); 286706f2543Smrg swapl(&out->time, n); 287706f2543Smrg swapl(&out->detail, n); 288706f2543Smrg swapl(&out->root, n); 289706f2543Smrg swapl(&out->event, n); 290706f2543Smrg swapl(&out->child, n); 291706f2543Smrg swapl(&out->root_x, n); 292706f2543Smrg swapl(&out->root_y, n); 293706f2543Smrg swapl(&out->event_x, n); 294706f2543Smrg swapl(&out->event_y, n); 295706f2543Smrg swaps(&out->buttons_len, n); 296706f2543Smrg swaps(&out->valuators_len, n); 297706f2543Smrg swapl(&out->mods.base_mods, n); 298706f2543Smrg swapl(&out->mods.latched_mods, n); 299706f2543Smrg swapl(&out->mods.locked_mods, n); 300706f2543Smrg swapl(&out->mods.effective_mods, n); 301706f2543Smrg swapl(&out->flags, n); 302706f2543Smrg } 303706f2543Smrg 304706f2543Smrg g_assert(out->extension == 0); /* IReqCode defaults to 0 */ 305706f2543Smrg g_assert(out->evtype == GetXI2Type((InternalEvent*)in)); 306706f2543Smrg g_assert(out->time == in->time); 307706f2543Smrg g_assert(out->detail == in->detail.button); 308706f2543Smrg g_assert(out->length >= 12); 309706f2543Smrg 310706f2543Smrg g_assert(out->deviceid == in->deviceid); 311706f2543Smrg g_assert(out->sourceid == in->sourceid); 312706f2543Smrg 313706f2543Smrg switch (in->type) { 314706f2543Smrg case ET_KeyPress: 315706f2543Smrg flagmask = XIKeyRepeat; 316706f2543Smrg break; 317706f2543Smrg default: 318706f2543Smrg flagmask = 0; 319706f2543Smrg break; 320706f2543Smrg } 321706f2543Smrg g_assert((out->flags & ~flagmask) == 0); 322706f2543Smrg 323706f2543Smrg g_assert(out->root == in->root); 324706f2543Smrg g_assert(out->event == None); /* set in FixUpEventFromWindow */ 325706f2543Smrg g_assert(out->child == None); /* set in FixUpEventFromWindow */ 326706f2543Smrg 327706f2543Smrg g_assert(out->mods.base_mods == in->mods.base); 328706f2543Smrg g_assert(out->mods.latched_mods == in->mods.latched); 329706f2543Smrg g_assert(out->mods.locked_mods == in->mods.locked); 330706f2543Smrg g_assert(out->mods.effective_mods == in->mods.effective); 331706f2543Smrg 332706f2543Smrg g_assert(out->group.base_group == in->group.base); 333706f2543Smrg g_assert(out->group.latched_group == in->group.latched); 334706f2543Smrg g_assert(out->group.locked_group == in->group.locked); 335706f2543Smrg g_assert(out->group.effective_group == in->group.effective); 336706f2543Smrg 337706f2543Smrg g_assert(out->event_x == 0); /* set in FixUpEventFromWindow */ 338706f2543Smrg g_assert(out->event_y == 0); /* set in FixUpEventFromWindow */ 339706f2543Smrg 340706f2543Smrg g_assert(out->root_x == FP1616(in->root_x, in->root_x_frac)); 341706f2543Smrg g_assert(out->root_y == FP1616(in->root_y, in->root_y_frac)); 342706f2543Smrg 343706f2543Smrg buttons = 0; 344706f2543Smrg for (i = 0; i < bits_to_bytes(sizeof(in->buttons)); i++) 345706f2543Smrg { 346706f2543Smrg if (XIMaskIsSet(in->buttons, i)) 347706f2543Smrg { 348706f2543Smrg g_assert(out->buttons_len >= bytes_to_int32(bits_to_bytes(i))); 349706f2543Smrg buttons++; 350706f2543Smrg } 351706f2543Smrg } 352706f2543Smrg 353706f2543Smrg ptr = (unsigned char*)&out[1]; 354706f2543Smrg for (i = 0; i < sizeof(in->buttons) * 8; i++) 355706f2543Smrg g_assert(XIMaskIsSet(in->buttons, i) == XIMaskIsSet(ptr, i)); 356706f2543Smrg 357706f2543Smrg 358706f2543Smrg valuators = 0; 359706f2543Smrg for (i = 0; i < sizeof(in->valuators.mask) * 8; i++) 360706f2543Smrg if (XIMaskIsSet(in->valuators.mask, i)) 361706f2543Smrg valuators++; 362706f2543Smrg 363706f2543Smrg g_assert(out->valuators_len >= bytes_to_int32(bits_to_bytes(valuators))); 364706f2543Smrg 365706f2543Smrg ptr += out->buttons_len * 4; 366706f2543Smrg values = (FP3232*)(ptr + out->valuators_len * 4); 367706f2543Smrg for (i = 0; i < sizeof(in->valuators.mask) * 8 || 368706f2543Smrg i < (out->valuators_len * 4) * 8; i++) 369706f2543Smrg { 370706f2543Smrg if (i > sizeof(in->valuators.mask) * 8) 371706f2543Smrg g_assert(!XIMaskIsSet(ptr, i)); 372706f2543Smrg else if (i > out->valuators_len * 4 * 8) 373706f2543Smrg g_assert(!XIMaskIsSet(in->valuators.mask, i)); 374706f2543Smrg else { 375706f2543Smrg g_assert(XIMaskIsSet(in->valuators.mask, i) == 376706f2543Smrg XIMaskIsSet(ptr, i)); 377706f2543Smrg 378706f2543Smrg if (XIMaskIsSet(ptr, i)) 379706f2543Smrg { 380706f2543Smrg FP3232 vi, vo; 381706f2543Smrg 382706f2543Smrg vi.integral = in->valuators.data[i]; 383706f2543Smrg vi.frac = in->valuators.data_frac[i]; 384706f2543Smrg 385706f2543Smrg vo = *values; 386706f2543Smrg 387706f2543Smrg if (swap) 388706f2543Smrg { 389706f2543Smrg char n; 390706f2543Smrg swapl(&vo.integral, n); 391706f2543Smrg swapl(&vo.frac, n); 392706f2543Smrg } 393706f2543Smrg 394706f2543Smrg 395706f2543Smrg g_assert(vi.integral == vo.integral); 396706f2543Smrg g_assert(vi.frac == vo.frac); 397706f2543Smrg values++; 398706f2543Smrg } 399706f2543Smrg } 400706f2543Smrg } 401706f2543Smrg} 402706f2543Smrg 403706f2543Smrgstatic void test_XIDeviceEvent(DeviceEvent *in) 404706f2543Smrg{ 405706f2543Smrg xXIDeviceEvent *out, *swapped; 406706f2543Smrg int rc; 407706f2543Smrg 408706f2543Smrg rc = EventToXI2((InternalEvent*)in, (xEvent**)&out); 409706f2543Smrg g_assert(rc == Success); 410706f2543Smrg 411706f2543Smrg test_values_XIDeviceEvent(in, out, FALSE); 412706f2543Smrg 413706f2543Smrg swapped = calloc(1, sizeof(xEvent) + out->length * 4); 414706f2543Smrg XI2EventSwap((xGenericEvent*)out, (xGenericEvent*)swapped); 415706f2543Smrg test_values_XIDeviceEvent(in, swapped, TRUE); 416706f2543Smrg 417706f2543Smrg free(out); 418706f2543Smrg free(swapped); 419706f2543Smrg} 420706f2543Smrg 421706f2543Smrgstatic void test_convert_XIDeviceEvent(void) 422706f2543Smrg{ 423706f2543Smrg DeviceEvent in; 424706f2543Smrg int i; 425706f2543Smrg 426706f2543Smrg memset(&in, 0, sizeof(in)); 427706f2543Smrg 428706f2543Smrg g_test_message("Testing simple field values"); 429706f2543Smrg in.header = ET_Internal; 430706f2543Smrg in.type = ET_Motion; 431706f2543Smrg in.length = sizeof(DeviceEvent); 432706f2543Smrg in.time = 0; 433706f2543Smrg in.deviceid = 1; 434706f2543Smrg in.sourceid = 2; 435706f2543Smrg in.root = 3; 436706f2543Smrg in.root_x = 4; 437706f2543Smrg in.root_x_frac = 5; 438706f2543Smrg in.root_y = 6; 439706f2543Smrg in.root_y_frac = 7; 440706f2543Smrg in.detail.button = 8; 441706f2543Smrg in.mods.base = 9; 442706f2543Smrg in.mods.latched = 10; 443706f2543Smrg in.mods.locked = 11; 444706f2543Smrg in.mods.effective = 11; 445706f2543Smrg in.group.base = 12; 446706f2543Smrg in.group.latched = 13; 447706f2543Smrg in.group.locked = 14; 448706f2543Smrg in.group.effective = 15; 449706f2543Smrg 450706f2543Smrg test_XIDeviceEvent(&in); 451706f2543Smrg 452706f2543Smrg g_test_message("Testing field ranges"); 453706f2543Smrg /* 32 bit */ 454706f2543Smrg in.detail.button = 1L; 455706f2543Smrg test_XIDeviceEvent(&in); 456706f2543Smrg in.detail.button = 1L << 8; 457706f2543Smrg test_XIDeviceEvent(&in); 458706f2543Smrg in.detail.button = 1L << 16; 459706f2543Smrg test_XIDeviceEvent(&in); 460706f2543Smrg in.detail.button = 1L << 24; 461706f2543Smrg test_XIDeviceEvent(&in); 462706f2543Smrg in.detail.button = ~0L; 463706f2543Smrg test_XIDeviceEvent(&in); 464706f2543Smrg 465706f2543Smrg /* 32 bit */ 466706f2543Smrg in.time = 1L; 467706f2543Smrg test_XIDeviceEvent(&in); 468706f2543Smrg in.time = 1L << 8; 469706f2543Smrg test_XIDeviceEvent(&in); 470706f2543Smrg in.time = 1L << 16; 471706f2543Smrg test_XIDeviceEvent(&in); 472706f2543Smrg in.time = 1L << 24; 473706f2543Smrg test_XIDeviceEvent(&in); 474706f2543Smrg in.time = ~0L; 475706f2543Smrg test_XIDeviceEvent(&in); 476706f2543Smrg 477706f2543Smrg /* 16 bit */ 478706f2543Smrg in.deviceid = 1; 479706f2543Smrg test_XIDeviceEvent(&in); 480706f2543Smrg in.deviceid = 1 << 8; 481706f2543Smrg test_XIDeviceEvent(&in); 482706f2543Smrg in.deviceid = ~0 & 0xFF; 483706f2543Smrg test_XIDeviceEvent(&in); 484706f2543Smrg 485706f2543Smrg /* 16 bit */ 486706f2543Smrg in.sourceid = 1; 487706f2543Smrg test_XIDeviceEvent(&in); 488706f2543Smrg in.deviceid = 1 << 8; 489706f2543Smrg test_XIDeviceEvent(&in); 490706f2543Smrg in.deviceid = ~0 & 0xFF; 491706f2543Smrg test_XIDeviceEvent(&in); 492706f2543Smrg 493706f2543Smrg /* 32 bit */ 494706f2543Smrg in.root = 1L; 495706f2543Smrg test_XIDeviceEvent(&in); 496706f2543Smrg in.root = 1L << 8; 497706f2543Smrg test_XIDeviceEvent(&in); 498706f2543Smrg in.root = 1L << 16; 499706f2543Smrg test_XIDeviceEvent(&in); 500706f2543Smrg in.root = 1L << 24; 501706f2543Smrg test_XIDeviceEvent(&in); 502706f2543Smrg in.root = ~0L; 503706f2543Smrg test_XIDeviceEvent(&in); 504706f2543Smrg 505706f2543Smrg /* 16 bit */ 506706f2543Smrg in.root_x = 1; 507706f2543Smrg test_XIDeviceEvent(&in); 508706f2543Smrg in.root_x = 1 << 8; 509706f2543Smrg test_XIDeviceEvent(&in); 510706f2543Smrg in.root_x = ~0 & 0xFF; 511706f2543Smrg test_XIDeviceEvent(&in); 512706f2543Smrg 513706f2543Smrg in.root_x_frac = 1; 514706f2543Smrg test_XIDeviceEvent(&in); 515706f2543Smrg in.root_x_frac = 1 << 8; 516706f2543Smrg test_XIDeviceEvent(&in); 517706f2543Smrg in.root_x_frac = ~0 & 0xFF; 518706f2543Smrg test_XIDeviceEvent(&in); 519706f2543Smrg 520706f2543Smrg in.root_y = 1; 521706f2543Smrg test_XIDeviceEvent(&in); 522706f2543Smrg in.root_y = 1 << 8; 523706f2543Smrg test_XIDeviceEvent(&in); 524706f2543Smrg in.root_y = ~0 & 0xFF; 525706f2543Smrg test_XIDeviceEvent(&in); 526706f2543Smrg 527706f2543Smrg in.root_y_frac = 1; 528706f2543Smrg test_XIDeviceEvent(&in); 529706f2543Smrg in.root_y_frac = 1 << 8; 530706f2543Smrg test_XIDeviceEvent(&in); 531706f2543Smrg in.root_y_frac = ~0 & 0xFF; 532706f2543Smrg test_XIDeviceEvent(&in); 533706f2543Smrg 534706f2543Smrg /* 32 bit */ 535706f2543Smrg in.mods.base = 1L; 536706f2543Smrg test_XIDeviceEvent(&in); 537706f2543Smrg in.mods.base = 1L << 8; 538706f2543Smrg test_XIDeviceEvent(&in); 539706f2543Smrg in.mods.base = 1L << 16; 540706f2543Smrg test_XIDeviceEvent(&in); 541706f2543Smrg in.mods.base = 1L << 24; 542706f2543Smrg test_XIDeviceEvent(&in); 543706f2543Smrg in.mods.base = ~0L; 544706f2543Smrg test_XIDeviceEvent(&in); 545706f2543Smrg 546706f2543Smrg in.mods.latched = 1L; 547706f2543Smrg test_XIDeviceEvent(&in); 548706f2543Smrg in.mods.latched = 1L << 8; 549706f2543Smrg test_XIDeviceEvent(&in); 550706f2543Smrg in.mods.latched = 1L << 16; 551706f2543Smrg test_XIDeviceEvent(&in); 552706f2543Smrg in.mods.latched = 1L << 24; 553706f2543Smrg test_XIDeviceEvent(&in); 554706f2543Smrg in.mods.latched = ~0L; 555706f2543Smrg test_XIDeviceEvent(&in); 556706f2543Smrg 557706f2543Smrg in.mods.locked = 1L; 558706f2543Smrg test_XIDeviceEvent(&in); 559706f2543Smrg in.mods.locked = 1L << 8; 560706f2543Smrg test_XIDeviceEvent(&in); 561706f2543Smrg in.mods.locked = 1L << 16; 562706f2543Smrg test_XIDeviceEvent(&in); 563706f2543Smrg in.mods.locked = 1L << 24; 564706f2543Smrg test_XIDeviceEvent(&in); 565706f2543Smrg in.mods.locked = ~0L; 566706f2543Smrg test_XIDeviceEvent(&in); 567706f2543Smrg 568706f2543Smrg in.mods.effective = 1L; 569706f2543Smrg test_XIDeviceEvent(&in); 570706f2543Smrg in.mods.effective = 1L << 8; 571706f2543Smrg test_XIDeviceEvent(&in); 572706f2543Smrg in.mods.effective = 1L << 16; 573706f2543Smrg test_XIDeviceEvent(&in); 574706f2543Smrg in.mods.effective = 1L << 24; 575706f2543Smrg test_XIDeviceEvent(&in); 576706f2543Smrg in.mods.effective = ~0L; 577706f2543Smrg test_XIDeviceEvent(&in); 578706f2543Smrg 579706f2543Smrg /* 8 bit */ 580706f2543Smrg in.group.base = 1; 581706f2543Smrg test_XIDeviceEvent(&in); 582706f2543Smrg in.group.base = ~0 & 0xFF; 583706f2543Smrg test_XIDeviceEvent(&in); 584706f2543Smrg 585706f2543Smrg in.group.latched = 1; 586706f2543Smrg test_XIDeviceEvent(&in); 587706f2543Smrg in.group.latched = ~0 & 0xFF; 588706f2543Smrg test_XIDeviceEvent(&in); 589706f2543Smrg 590706f2543Smrg in.group.locked = 1; 591706f2543Smrg test_XIDeviceEvent(&in); 592706f2543Smrg in.group.locked = ~0 & 0xFF; 593706f2543Smrg test_XIDeviceEvent(&in); 594706f2543Smrg 595706f2543Smrg in.mods.effective = 1; 596706f2543Smrg test_XIDeviceEvent(&in); 597706f2543Smrg in.mods.effective = ~0 & 0xFF; 598706f2543Smrg test_XIDeviceEvent(&in); 599706f2543Smrg 600706f2543Smrg g_test_message("Testing button masks"); 601706f2543Smrg for (i = 0; i < sizeof(in.buttons) * 8; i++) 602706f2543Smrg { 603706f2543Smrg XISetMask(in.buttons, i); 604706f2543Smrg test_XIDeviceEvent(&in); 605706f2543Smrg XIClearMask(in.buttons, i); 606706f2543Smrg } 607706f2543Smrg 608706f2543Smrg for (i = 0; i < sizeof(in.buttons) * 8; i++) 609706f2543Smrg { 610706f2543Smrg XISetMask(in.buttons, i); 611706f2543Smrg test_XIDeviceEvent(&in); 612706f2543Smrg } 613706f2543Smrg 614706f2543Smrg g_test_message("Testing valuator masks"); 615706f2543Smrg for (i = 0; i < sizeof(in.valuators.mask) * 8; i++) 616706f2543Smrg { 617706f2543Smrg XISetMask(in.valuators.mask, i); 618706f2543Smrg test_XIDeviceEvent(&in); 619706f2543Smrg XIClearMask(in.valuators.mask, i); 620706f2543Smrg } 621706f2543Smrg 622706f2543Smrg for (i = 0; i < sizeof(in.valuators.mask) * 8; i++) 623706f2543Smrg { 624706f2543Smrg XISetMask(in.valuators.mask, i); 625706f2543Smrg 626706f2543Smrg in.valuators.data[i] = i; 627706f2543Smrg in.valuators.data_frac[i] = i + 20; 628706f2543Smrg test_XIDeviceEvent(&in); 629706f2543Smrg XIClearMask(in.valuators.mask, i); 630706f2543Smrg } 631706f2543Smrg 632706f2543Smrg for (i = 0; i < sizeof(in.valuators.mask) * 8; i++) 633706f2543Smrg { 634706f2543Smrg XISetMask(in.valuators.mask, i); 635706f2543Smrg test_XIDeviceEvent(&in); 636706f2543Smrg } 637706f2543Smrg} 638706f2543Smrg 639706f2543Smrgstatic void test_values_XIDeviceChangedEvent(DeviceChangedEvent *in, 640706f2543Smrg xXIDeviceChangedEvent *out, 641706f2543Smrg BOOL swap) 642706f2543Smrg{ 643706f2543Smrg int i, j; 644706f2543Smrg unsigned char *ptr; 645706f2543Smrg 646706f2543Smrg if (swap) 647706f2543Smrg { 648706f2543Smrg char n; 649706f2543Smrg 650706f2543Smrg swaps(&out->sequenceNumber, n); 651706f2543Smrg swapl(&out->length, n); 652706f2543Smrg swaps(&out->evtype, n); 653706f2543Smrg swaps(&out->deviceid, n); 654706f2543Smrg swaps(&out->sourceid, n); 655706f2543Smrg swapl(&out->time, n); 656706f2543Smrg swaps(&out->num_classes, n); 657706f2543Smrg } 658706f2543Smrg 659706f2543Smrg g_assert(out->type == GenericEvent); 660706f2543Smrg g_assert(out->extension == 0); /* IReqCode defaults to 0 */ 661706f2543Smrg g_assert(out->evtype == GetXI2Type((InternalEvent*)in)); 662706f2543Smrg g_assert(out->time == in->time); 663706f2543Smrg g_assert(out->deviceid == in->deviceid); 664706f2543Smrg g_assert(out->sourceid == in->sourceid); 665706f2543Smrg 666706f2543Smrg ptr = (unsigned char*)&out[1]; 667706f2543Smrg for (i = 0; i < out->num_classes; i++) 668706f2543Smrg { 669706f2543Smrg xXIAnyInfo* any = (xXIAnyInfo*)ptr; 670706f2543Smrg 671706f2543Smrg if (swap) 672706f2543Smrg { 673706f2543Smrg char n; 674706f2543Smrg swaps(&any->length, n); 675706f2543Smrg swaps(&any->type, n); 676706f2543Smrg swaps(&any->sourceid, n); 677706f2543Smrg } 678706f2543Smrg 679706f2543Smrg switch(any->type) 680706f2543Smrg { 681706f2543Smrg case XIButtonClass: 682706f2543Smrg { 683706f2543Smrg xXIButtonInfo *b = (xXIButtonInfo*)any; 684706f2543Smrg Atom *names; 685706f2543Smrg 686706f2543Smrg if (swap) 687706f2543Smrg { 688706f2543Smrg char n; 689706f2543Smrg swaps(&b->num_buttons, n); 690706f2543Smrg } 691706f2543Smrg 692706f2543Smrg g_assert(b->length == 693706f2543Smrg bytes_to_int32(sizeof(xXIButtonInfo)) + 694706f2543Smrg bytes_to_int32(bits_to_bytes(b->num_buttons)) + 695706f2543Smrg b->num_buttons); 696706f2543Smrg g_assert(b->num_buttons == in->buttons.num_buttons); 697706f2543Smrg 698706f2543Smrg names = (Atom*)((char*)&b[1] + 699706f2543Smrg pad_to_int32(bits_to_bytes(b->num_buttons))); 700706f2543Smrg for (j = 0; j < b->num_buttons; j++) 701706f2543Smrg { 702706f2543Smrg if (swap) 703706f2543Smrg { 704706f2543Smrg char n; 705706f2543Smrg swapl(&names[j], n); 706706f2543Smrg } 707706f2543Smrg g_assert(names[j] == in->buttons.names[j]); 708706f2543Smrg } 709706f2543Smrg } 710706f2543Smrg break; 711706f2543Smrg case XIKeyClass: 712706f2543Smrg { 713706f2543Smrg xXIKeyInfo *k = (xXIKeyInfo*)any; 714706f2543Smrg uint32_t *kc; 715706f2543Smrg 716706f2543Smrg if (swap) 717706f2543Smrg { 718706f2543Smrg char n; 719706f2543Smrg swaps(&k->num_keycodes, n); 720706f2543Smrg } 721706f2543Smrg 722706f2543Smrg g_assert(k->length == 723706f2543Smrg bytes_to_int32(sizeof(xXIKeyInfo)) + 724706f2543Smrg k->num_keycodes); 725706f2543Smrg g_assert(k->num_keycodes == in->keys.max_keycode - 726706f2543Smrg in->keys.min_keycode + 1); 727706f2543Smrg 728706f2543Smrg kc = (uint32_t*)&k[1]; 729706f2543Smrg for (j = 0; j < k->num_keycodes; j++) 730706f2543Smrg { 731706f2543Smrg if (swap) 732706f2543Smrg { 733706f2543Smrg char n; 734706f2543Smrg swapl(&kc[j], n); 735706f2543Smrg } 736706f2543Smrg g_assert(kc[j] >= in->keys.min_keycode); 737706f2543Smrg g_assert(kc[j] <= in->keys.max_keycode); 738706f2543Smrg } 739706f2543Smrg } 740706f2543Smrg break; 741706f2543Smrg case XIValuatorClass: 742706f2543Smrg { 743706f2543Smrg xXIValuatorInfo *v = (xXIValuatorInfo*)any; 744706f2543Smrg g_assert(v->length == 745706f2543Smrg bytes_to_int32(sizeof(xXIValuatorInfo))); 746706f2543Smrg 747706f2543Smrg } 748706f2543Smrg break; 749706f2543Smrg default: 750706f2543Smrg g_error("Invalid class type.\n"); 751706f2543Smrg break; 752706f2543Smrg } 753706f2543Smrg 754706f2543Smrg ptr += any->length * 4; 755706f2543Smrg } 756706f2543Smrg 757706f2543Smrg} 758706f2543Smrg 759706f2543Smrgstatic void test_XIDeviceChangedEvent(DeviceChangedEvent *in) 760706f2543Smrg{ 761706f2543Smrg xXIDeviceChangedEvent *out, *swapped; 762706f2543Smrg int rc; 763706f2543Smrg 764706f2543Smrg rc = EventToXI2((InternalEvent*)in, (xEvent**)&out); 765706f2543Smrg g_assert(rc == Success); 766706f2543Smrg 767706f2543Smrg test_values_XIDeviceChangedEvent(in, out, FALSE); 768706f2543Smrg 769706f2543Smrg swapped = calloc(1, sizeof(xEvent) + out->length * 4); 770706f2543Smrg XI2EventSwap((xGenericEvent*)out, (xGenericEvent*)swapped); 771706f2543Smrg test_values_XIDeviceChangedEvent(in, swapped, TRUE); 772706f2543Smrg 773706f2543Smrg free(out); 774706f2543Smrg free(swapped); 775706f2543Smrg} 776706f2543Smrg 777706f2543Smrgstatic void test_convert_XIDeviceChangedEvent(void) 778706f2543Smrg{ 779706f2543Smrg DeviceChangedEvent in; 780706f2543Smrg int i; 781706f2543Smrg 782706f2543Smrg g_test_message("Testing simple field values"); 783706f2543Smrg memset(&in, 0, sizeof(in)); 784706f2543Smrg in.header = ET_Internal; 785706f2543Smrg in.type = ET_DeviceChanged; 786706f2543Smrg in.length = sizeof(DeviceChangedEvent); 787706f2543Smrg in.time = 0; 788706f2543Smrg in.deviceid = 1; 789706f2543Smrg in.sourceid = 2; 790706f2543Smrg in.masterid = 3; 791706f2543Smrg in.num_valuators = 4; 792706f2543Smrg in.flags = DEVCHANGE_SLAVE_SWITCH | DEVCHANGE_POINTER_EVENT | DEVCHANGE_KEYBOARD_EVENT; 793706f2543Smrg 794706f2543Smrg for (i = 0; i < MAX_BUTTONS; i++) 795706f2543Smrg in.buttons.names[i] = i + 10; 796706f2543Smrg 797706f2543Smrg in.keys.min_keycode = 8; 798706f2543Smrg in.keys.max_keycode = 255; 799706f2543Smrg 800706f2543Smrg test_XIDeviceChangedEvent(&in); 801706f2543Smrg 802706f2543Smrg in.time = 1L; 803706f2543Smrg test_XIDeviceChangedEvent(&in); 804706f2543Smrg in.time = 1L << 8; 805706f2543Smrg test_XIDeviceChangedEvent(&in); 806706f2543Smrg in.time = 1L << 16; 807706f2543Smrg test_XIDeviceChangedEvent(&in); 808706f2543Smrg in.time = 1L << 24; 809706f2543Smrg test_XIDeviceChangedEvent(&in); 810706f2543Smrg in.time = ~0L; 811706f2543Smrg test_XIDeviceChangedEvent(&in); 812706f2543Smrg 813706f2543Smrg in.deviceid = 1L; 814706f2543Smrg test_XIDeviceChangedEvent(&in); 815706f2543Smrg in.deviceid = 1L << 8; 816706f2543Smrg test_XIDeviceChangedEvent(&in); 817706f2543Smrg in.deviceid = ~0 & 0xFFFF; 818706f2543Smrg test_XIDeviceChangedEvent(&in); 819706f2543Smrg 820706f2543Smrg in.sourceid = 1L; 821706f2543Smrg test_XIDeviceChangedEvent(&in); 822706f2543Smrg in.sourceid = 1L << 8; 823706f2543Smrg test_XIDeviceChangedEvent(&in); 824706f2543Smrg in.sourceid = ~0 & 0xFFFF; 825706f2543Smrg test_XIDeviceChangedEvent(&in); 826706f2543Smrg 827706f2543Smrg in.masterid = 1L; 828706f2543Smrg test_XIDeviceChangedEvent(&in); 829706f2543Smrg in.masterid = 1L << 8; 830706f2543Smrg test_XIDeviceChangedEvent(&in); 831706f2543Smrg in.masterid = ~0 & 0xFFFF; 832706f2543Smrg test_XIDeviceChangedEvent(&in); 833706f2543Smrg 834706f2543Smrg in.buttons.num_buttons = 0; 835706f2543Smrg test_XIDeviceChangedEvent(&in); 836706f2543Smrg 837706f2543Smrg in.buttons.num_buttons = 1; 838706f2543Smrg test_XIDeviceChangedEvent(&in); 839706f2543Smrg 840706f2543Smrg in.buttons.num_buttons = MAX_BUTTONS; 841706f2543Smrg test_XIDeviceChangedEvent(&in); 842706f2543Smrg 843706f2543Smrg in.keys.min_keycode = 0; 844706f2543Smrg in.keys.max_keycode = 0; 845706f2543Smrg test_XIDeviceChangedEvent(&in); 846706f2543Smrg 847706f2543Smrg in.keys.max_keycode = 1 << 8; 848706f2543Smrg test_XIDeviceChangedEvent(&in); 849706f2543Smrg 850706f2543Smrg in.keys.max_keycode = 0xFFFC; /* highest range, above that the length 851706f2543Smrg field gives up */ 852706f2543Smrg test_XIDeviceChangedEvent(&in); 853706f2543Smrg 854706f2543Smrg in.keys.min_keycode = 1 << 8; 855706f2543Smrg in.keys.max_keycode = 1 << 8; 856706f2543Smrg test_XIDeviceChangedEvent(&in); 857706f2543Smrg 858706f2543Smrg in.keys.min_keycode = 1 << 8; 859706f2543Smrg in.keys.max_keycode = 0; 860706f2543Smrg test_XIDeviceChangedEvent(&in); 861706f2543Smrg 862706f2543Smrg in.num_valuators = 0; 863706f2543Smrg test_XIDeviceChangedEvent(&in); 864706f2543Smrg 865706f2543Smrg in.num_valuators = 1; 866706f2543Smrg test_XIDeviceChangedEvent(&in); 867706f2543Smrg 868706f2543Smrg in.num_valuators = MAX_VALUATORS; 869706f2543Smrg test_XIDeviceChangedEvent(&in); 870706f2543Smrg 871706f2543Smrg for (i = 0; i < MAX_VALUATORS; i++) 872706f2543Smrg { 873706f2543Smrg in.valuators[i].min = 0; 874706f2543Smrg in.valuators[i].max = 0; 875706f2543Smrg test_XIDeviceChangedEvent(&in); 876706f2543Smrg 877706f2543Smrg in.valuators[i].max = 1 << 8; 878706f2543Smrg test_XIDeviceChangedEvent(&in); 879706f2543Smrg in.valuators[i].max = 1 << 16; 880706f2543Smrg test_XIDeviceChangedEvent(&in); 881706f2543Smrg in.valuators[i].max = 1 << 24; 882706f2543Smrg test_XIDeviceChangedEvent(&in); 883706f2543Smrg in.valuators[i].max = abs(~0); 884706f2543Smrg test_XIDeviceChangedEvent(&in); 885706f2543Smrg 886706f2543Smrg in.valuators[i].resolution = 1 << 8; 887706f2543Smrg test_XIDeviceChangedEvent(&in); 888706f2543Smrg in.valuators[i].resolution = 1 << 16; 889706f2543Smrg test_XIDeviceChangedEvent(&in); 890706f2543Smrg in.valuators[i].resolution = 1 << 24; 891706f2543Smrg test_XIDeviceChangedEvent(&in); 892706f2543Smrg in.valuators[i].resolution = abs(~0); 893706f2543Smrg test_XIDeviceChangedEvent(&in); 894706f2543Smrg 895706f2543Smrg in.valuators[i].name = i; 896706f2543Smrg test_XIDeviceChangedEvent(&in); 897706f2543Smrg 898706f2543Smrg in.valuators[i].mode = Relative; 899706f2543Smrg test_XIDeviceChangedEvent(&in); 900706f2543Smrg 901706f2543Smrg in.valuators[i].mode = Absolute; 902706f2543Smrg test_XIDeviceChangedEvent(&in); 903706f2543Smrg } 904706f2543Smrg} 905706f2543Smrg 906706f2543Smrgint main(int argc, char** argv) 907706f2543Smrg{ 908706f2543Smrg g_test_init(&argc, &argv,NULL); 909706f2543Smrg g_test_bug_base("https://bugzilla.freedesktop.org/show_bug.cgi?id="); 910706f2543Smrg 911706f2543Smrg g_test_add_func("/xi2/eventconvert/XIRawEvent", test_convert_XIRawEvent); 912706f2543Smrg g_test_add_func("/xi2/eventconvert/XIFocusEvent", test_convert_XIFocusEvent); 913706f2543Smrg g_test_add_func("/xi2/eventconvert/XIDeviceEvent", test_convert_XIDeviceEvent); 914706f2543Smrg g_test_add_func("/xi2/eventconvert/XIDeviceChangedEvent", test_convert_XIDeviceChangedEvent); 915706f2543Smrg 916706f2543Smrg return g_test_run(); 917706f2543Smrg} 918