1/* 2 * Copyright © 2008 Daniel Stone 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice (including the next 12 * paragraph) shall be included in all copies or substantial portions of the 13 * Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 21 * DEALINGS IN THE SOFTWARE. 22 * 23 * Author: Daniel Stone <daniel@fooishbar.org> 24 */ 25 26#ifdef HAVE_DIX_CONFIG_H 27#include "dix-config.h" 28#endif 29 30#include "exevents.h" 31#include "exglobals.h" 32#include "misc.h" 33#include "input.h" 34#include "inputstr.h" 35#include "xace.h" 36#include "xkbsrv.h" 37#include "xkbstr.h" 38#include "inpututils.h" 39#include "eventstr.h" 40#include "scrnintstr.h" 41#include "optionstr.h" 42 43/* Check if a button map change is okay with the device. 44 * Returns -1 for BadValue, as it collides with MappingBusy. */ 45static int 46check_butmap_change(DeviceIntPtr dev, CARD8 *map, int len, CARD32 *errval_out, 47 ClientPtr client) 48{ 49 int i, ret; 50 51 if (!dev || !dev->button) { 52 client->errorValue = (dev) ? dev->id : 0; 53 return BadDevice; 54 } 55 56 ret = XaceHook(XACE_DEVICE_ACCESS, client, dev, DixManageAccess); 57 if (ret != Success) { 58 client->errorValue = dev->id; 59 return ret; 60 } 61 62 for (i = 0; i < len; i++) { 63 if (dev->button->map[i + 1] != map[i] && 64 button_is_down(dev, i + 1, BUTTON_PROCESSED)) 65 return MappingBusy; 66 } 67 68 return Success; 69} 70 71static void 72do_butmap_change(DeviceIntPtr dev, CARD8 *map, int len, ClientPtr client) 73{ 74 int i; 75 xEvent core_mn = { .u.u.type = MappingNotify }; 76 deviceMappingNotify xi_mn; 77 78 /* The map in ButtonClassRec refers to button numbers, whereas the 79 * protocol is zero-indexed. Sigh. */ 80 memcpy(&(dev->button->map[1]), map, len); 81 82 core_mn.u.mappingNotify.request = MappingPointer; 83 84 /* 0 is the server client. */ 85 for (i = 1; i < currentMaxClients; i++) { 86 /* Don't send irrelevant events to naïve clients. */ 87 if (!clients[i] || clients[i]->clientState != ClientStateRunning) 88 continue; 89 90 if (!XIShouldNotify(clients[i], dev)) 91 continue; 92 93 WriteEventsToClient(clients[i], 1, &core_mn); 94 } 95 96 xi_mn = (deviceMappingNotify) { 97 .type = DeviceMappingNotify, 98 .request = MappingPointer, 99 .deviceid = dev->id, 100 .time = GetTimeInMillis() 101 }; 102 103 SendEventToAllWindows(dev, DeviceMappingNotifyMask, (xEvent *) &xi_mn, 1); 104} 105 106/* 107 * Does what it says on the box, both for core and Xi. 108 * 109 * Faithfully reports any errors encountered while trying to apply the map 110 * to the requested device, faithfully ignores any errors encountered while 111 * trying to apply the map to its master/slaves. 112 */ 113int 114ApplyPointerMapping(DeviceIntPtr dev, CARD8 *map, int len, ClientPtr client) 115{ 116 int ret; 117 118 /* If we can't perform the change on the requested device, bail out. */ 119 ret = check_butmap_change(dev, map, len, &client->errorValue, client); 120 if (ret != Success) 121 return ret; 122 do_butmap_change(dev, map, len, client); 123 124 return Success; 125} 126 127/* Check if a modifier map change is okay with the device. Negative return 128 * values mean BadValue, positive values mean Mapping{Busy,Failed}, 0 is 129 * Success / MappingSuccess. 130 */ 131static int 132check_modmap_change(ClientPtr client, DeviceIntPtr dev, KeyCode *modmap) 133{ 134 int ret, i; 135 XkbDescPtr xkb; 136 137 ret = XaceHook(XACE_DEVICE_ACCESS, client, dev, DixManageAccess); 138 if (ret != Success) 139 return ret; 140 141 if (!dev->key) 142 return BadMatch; 143 xkb = dev->key->xkbInfo->desc; 144 145 for (i = 0; i < MAP_LENGTH; i++) { 146 if (!modmap[i]) 147 continue; 148 149 /* Check that all the new modifiers fall within the advertised 150 * keycode range. */ 151 if (i < xkb->min_key_code || i > xkb->max_key_code) { 152 client->errorValue = i; 153 return -1; 154 } 155 156 /* None of the new modifiers may be down while we change the 157 * map. */ 158 if (key_is_down(dev, i, KEY_POSTED | KEY_PROCESSED)) { 159 client->errorValue = i; 160 return MappingBusy; 161 } 162 } 163 164 /* None of the old modifiers may be down while we change the map, 165 * either. */ 166 for (i = xkb->min_key_code; i < xkb->max_key_code; i++) { 167 if (!xkb->map->modmap[i]) 168 continue; 169 if (key_is_down(dev, i, KEY_POSTED | KEY_PROCESSED)) { 170 client->errorValue = i; 171 return MappingBusy; 172 } 173 } 174 175 return Success; 176} 177 178static int 179check_modmap_change_slave(ClientPtr client, DeviceIntPtr master, 180 DeviceIntPtr slave, CARD8 *modmap) 181{ 182 XkbDescPtr master_xkb, slave_xkb; 183 int i, j; 184 185 if (!slave->key || !master->key) 186 return 0; 187 188 master_xkb = master->key->xkbInfo->desc; 189 slave_xkb = slave->key->xkbInfo->desc; 190 191 /* Ignore devices with a clearly different keymap. */ 192 if (slave_xkb->min_key_code != master_xkb->min_key_code || 193 slave_xkb->max_key_code != master_xkb->max_key_code) 194 return 0; 195 196 for (i = 0; i < MAP_LENGTH; i++) { 197 if (!modmap[i]) 198 continue; 199 200 /* If we have different symbols for any modifier on an 201 * extended keyboard, ignore the whole remap request. */ 202 for (j = 0; 203 j < XkbKeyNumSyms(slave_xkb, i) && 204 j < XkbKeyNumSyms(master_xkb, i); j++) 205 if (XkbKeySymsPtr(slave_xkb, i)[j] != 206 XkbKeySymsPtr(master_xkb, i)[j]) 207 return 0; 208 } 209 210 if (check_modmap_change(client, slave, modmap) != Success) 211 return 0; 212 213 return 1; 214} 215 216/* Actually change the modifier map, and send notifications. Cannot fail. */ 217static void 218do_modmap_change(ClientPtr client, DeviceIntPtr dev, CARD8 *modmap) 219{ 220 XkbApplyMappingChange(dev, NULL, 0, 0, modmap, serverClient); 221} 222 223/* Rebuild modmap (key -> mod) from map (mod -> key). */ 224static int 225build_modmap_from_modkeymap(CARD8 *modmap, KeyCode *modkeymap, 226 int max_keys_per_mod) 227{ 228 int i, len = max_keys_per_mod * 8; 229 230 memset(modmap, 0, MAP_LENGTH); 231 232 for (i = 0; i < len; i++) { 233 if (!modkeymap[i]) 234 continue; 235 236#if MAP_LENGTH < 256 237 if (modkeymap[i] >= MAP_LENGTH) 238 return BadValue; 239#endif 240 241 if (modmap[modkeymap[i]]) 242 return BadValue; 243 244 modmap[modkeymap[i]] = 1 << (i / max_keys_per_mod); 245 } 246 247 return Success; 248} 249 250int 251change_modmap(ClientPtr client, DeviceIntPtr dev, KeyCode *modkeymap, 252 int max_keys_per_mod) 253{ 254 int ret; 255 CARD8 modmap[MAP_LENGTH]; 256 DeviceIntPtr tmp; 257 258 ret = build_modmap_from_modkeymap(modmap, modkeymap, max_keys_per_mod); 259 if (ret != Success) 260 return ret; 261 262 /* If we can't perform the change on the requested device, bail out. */ 263 ret = check_modmap_change(client, dev, modmap); 264 if (ret != Success) 265 return ret; 266 do_modmap_change(client, dev, modmap); 267 268 /* Change any attached masters/slaves. */ 269 if (IsMaster(dev)) { 270 for (tmp = inputInfo.devices; tmp; tmp = tmp->next) { 271 if (!IsMaster(tmp) && GetMaster(tmp, MASTER_KEYBOARD) == dev) 272 if (check_modmap_change_slave(client, dev, tmp, modmap)) 273 do_modmap_change(client, tmp, modmap); 274 } 275 } 276 else if (!IsFloating(dev) && 277 GetMaster(dev, MASTER_KEYBOARD)->lastSlave == dev) { 278 /* If this fails, expect the results to be weird. */ 279 if (check_modmap_change(client, dev->master, modmap) == Success) 280 do_modmap_change(client, dev->master, modmap); 281 } 282 283 return Success; 284} 285 286int 287generate_modkeymap(ClientPtr client, DeviceIntPtr dev, 288 KeyCode **modkeymap_out, int *max_keys_per_mod_out) 289{ 290 CARD8 keys_per_mod[8]; 291 int max_keys_per_mod; 292 KeyCode *modkeymap = NULL; 293 int i, j, ret; 294 295 ret = XaceHook(XACE_DEVICE_ACCESS, client, dev, DixGetAttrAccess); 296 if (ret != Success) 297 return ret; 298 299 if (!dev->key) 300 return BadMatch; 301 302 /* Count the number of keys per modifier to determine how wide we 303 * should make the map. */ 304 max_keys_per_mod = 0; 305 for (i = 0; i < 8; i++) 306 keys_per_mod[i] = 0; 307 for (i = 8; i < MAP_LENGTH; i++) { 308 for (j = 0; j < 8; j++) { 309 if (dev->key->xkbInfo->desc->map->modmap[i] & (1 << j)) { 310 if (++keys_per_mod[j] > max_keys_per_mod) 311 max_keys_per_mod = keys_per_mod[j]; 312 } 313 } 314 } 315 316 if (max_keys_per_mod != 0) { 317 modkeymap = calloc(max_keys_per_mod * 8, sizeof(KeyCode)); 318 if (!modkeymap) 319 return BadAlloc; 320 321 for (i = 0; i < 8; i++) 322 keys_per_mod[i] = 0; 323 324 for (i = 8; i < MAP_LENGTH; i++) { 325 for (j = 0; j < 8; j++) { 326 if (dev->key->xkbInfo->desc->map->modmap[i] & (1 << j)) { 327 modkeymap[(j * max_keys_per_mod) + keys_per_mod[j]] = i; 328 keys_per_mod[j]++; 329 } 330 } 331 } 332 } 333 334 *max_keys_per_mod_out = max_keys_per_mod; 335 *modkeymap_out = modkeymap; 336 337 return Success; 338} 339 340/** 341 * Duplicate the InputAttributes in the most obvious way. 342 * No special memory handling is used to give drivers the maximum 343 * flexibility with the data. Drivers should be able to call realloc on the 344 * product string if needed and perform similar operations. 345 */ 346InputAttributes * 347DuplicateInputAttributes(InputAttributes * attrs) 348{ 349 InputAttributes *new_attr; 350 int ntags = 0; 351 char **tags, **new_tags; 352 353 if (!attrs) 354 return NULL; 355 356 if (!(new_attr = calloc(1, sizeof(InputAttributes)))) 357 goto unwind; 358 359 if (attrs->product && !(new_attr->product = strdup(attrs->product))) 360 goto unwind; 361 if (attrs->vendor && !(new_attr->vendor = strdup(attrs->vendor))) 362 goto unwind; 363 if (attrs->device && !(new_attr->device = strdup(attrs->device))) 364 goto unwind; 365 if (attrs->pnp_id && !(new_attr->pnp_id = strdup(attrs->pnp_id))) 366 goto unwind; 367 if (attrs->usb_id && !(new_attr->usb_id = strdup(attrs->usb_id))) 368 goto unwind; 369 370 new_attr->flags = attrs->flags; 371 372 if ((tags = attrs->tags)) { 373 while (*tags++) 374 ntags++; 375 376 new_attr->tags = calloc(ntags + 1, sizeof(char *)); 377 if (!new_attr->tags) 378 goto unwind; 379 380 tags = attrs->tags; 381 new_tags = new_attr->tags; 382 383 while (*tags) { 384 *new_tags = strdup(*tags); 385 if (!*new_tags) 386 goto unwind; 387 388 tags++; 389 new_tags++; 390 } 391 } 392 393 return new_attr; 394 395 unwind: 396 FreeInputAttributes(new_attr); 397 return NULL; 398} 399 400void 401FreeInputAttributes(InputAttributes * attrs) 402{ 403 char **tags; 404 405 if (!attrs) 406 return; 407 408 free(attrs->product); 409 free(attrs->vendor); 410 free(attrs->device); 411 free(attrs->pnp_id); 412 free(attrs->usb_id); 413 414 if ((tags = attrs->tags)) 415 while (*tags) 416 free(*tags++); 417 418 free(attrs->tags); 419 free(attrs); 420} 421 422/** 423 * Alloc a valuator mask large enough for num_valuators. 424 */ 425ValuatorMask * 426valuator_mask_new(int num_valuators) 427{ 428 /* alloc a fixed size mask for now and ignore num_valuators. in the 429 * flying-car future, when we can dynamically alloc the masks and are 430 * not constrained by signals, we can start using num_valuators */ 431 ValuatorMask *mask = calloc(1, sizeof(ValuatorMask)); 432 433 if (mask == NULL) 434 return NULL; 435 436 mask->last_bit = -1; 437 return mask; 438} 439 440void 441valuator_mask_free(ValuatorMask **mask) 442{ 443 free(*mask); 444 *mask = NULL; 445} 446 447/** 448 * Sets a range of valuators between first_valuator and num_valuators with 449 * the data in the valuators array. All other values are set to 0. 450 */ 451void 452valuator_mask_set_range(ValuatorMask *mask, int first_valuator, 453 int num_valuators, const int *valuators) 454{ 455 int i; 456 457 valuator_mask_zero(mask); 458 459 for (i = first_valuator; 460 i < min(first_valuator + num_valuators, MAX_VALUATORS); i++) 461 valuator_mask_set(mask, i, valuators[i - first_valuator]); 462} 463 464/** 465 * Reset mask to zero. 466 */ 467void 468valuator_mask_zero(ValuatorMask *mask) 469{ 470 memset(mask, 0, sizeof(*mask)); 471 mask->last_bit = -1; 472} 473 474/** 475 * Returns the current size of the mask (i.e. the highest number of 476 * valuators currently set + 1). 477 */ 478int 479valuator_mask_size(const ValuatorMask *mask) 480{ 481 return mask->last_bit + 1; 482} 483 484/** 485 * Returns the number of valuators set in the given mask. 486 */ 487int 488valuator_mask_num_valuators(const ValuatorMask *mask) 489{ 490 return CountBits(mask->mask, min(mask->last_bit + 1, MAX_VALUATORS)); 491} 492 493/** 494 * Return true if the valuator is set in the mask, or false otherwise. 495 */ 496int 497valuator_mask_isset(const ValuatorMask *mask, int valuator) 498{ 499 return mask->last_bit >= valuator && BitIsOn(mask->mask, valuator); 500} 501 502static inline void 503_valuator_mask_set_double(ValuatorMask *mask, int valuator, double data) 504{ 505 mask->last_bit = max(valuator, mask->last_bit); 506 SetBit(mask->mask, valuator); 507 mask->valuators[valuator] = data; 508} 509 510/** 511 * Set the valuator to the given floating-point data. 512 */ 513void 514valuator_mask_set_double(ValuatorMask *mask, int valuator, double data) 515{ 516 BUG_WARN_MSG(mask->has_unaccelerated, 517 "Do not mix valuator types, zero mask first\n"); 518 _valuator_mask_set_double(mask, valuator, data); 519} 520 521/** 522 * Set the valuator to the given integer data. 523 */ 524void 525valuator_mask_set(ValuatorMask *mask, int valuator, int data) 526{ 527 valuator_mask_set_double(mask, valuator, data); 528} 529 530/** 531 * Return the requested valuator value as a double. If the mask bit is not 532 * set for the given valuator, the returned value is undefined. 533 */ 534double 535valuator_mask_get_double(const ValuatorMask *mask, int valuator) 536{ 537 return mask->valuators[valuator]; 538} 539 540/** 541 * Return the requested valuator value as an integer, rounding towards zero. 542 * If the mask bit is not set for the given valuator, the returned value is 543 * undefined. 544 */ 545int 546valuator_mask_get(const ValuatorMask *mask, int valuator) 547{ 548 return trunc(valuator_mask_get_double(mask, valuator)); 549} 550 551/** 552 * Set value to the requested valuator. If the mask bit is set for this 553 * valuator, value contains the requested valuator value and TRUE is 554 * returned. 555 * If the mask bit is not set for this valuator, value is unchanged and 556 * FALSE is returned. 557 */ 558Bool 559valuator_mask_fetch_double(const ValuatorMask *mask, int valuator, 560 double *value) 561{ 562 if (valuator_mask_isset(mask, valuator)) { 563 *value = valuator_mask_get_double(mask, valuator); 564 return TRUE; 565 } 566 else 567 return FALSE; 568} 569 570/** 571 * Set value to the requested valuator. If the mask bit is set for this 572 * valuator, value contains the requested valuator value and TRUE is 573 * returned. 574 * If the mask bit is not set for this valuator, value is unchanged and 575 * FALSE is returned. 576 */ 577Bool 578valuator_mask_fetch(const ValuatorMask *mask, int valuator, int *value) 579{ 580 if (valuator_mask_isset(mask, valuator)) { 581 *value = valuator_mask_get(mask, valuator); 582 return TRUE; 583 } 584 else 585 return FALSE; 586} 587 588/** 589 * Remove the valuator from the mask. 590 */ 591void 592valuator_mask_unset(ValuatorMask *mask, int valuator) 593{ 594 if (mask->last_bit >= valuator) { 595 int i, lastbit = -1; 596 597 ClearBit(mask->mask, valuator); 598 mask->valuators[valuator] = 0.0; 599 mask->unaccelerated[valuator] = 0.0; 600 601 for (i = 0; i <= mask->last_bit; i++) 602 if (valuator_mask_isset(mask, i)) 603 lastbit = max(lastbit, i); 604 mask->last_bit = lastbit; 605 606 if (mask->last_bit == -1) 607 mask->has_unaccelerated = FALSE; 608 } 609} 610 611void 612valuator_mask_copy(ValuatorMask *dest, const ValuatorMask *src) 613{ 614 if (src) 615 memcpy(dest, src, sizeof(*dest)); 616 else 617 valuator_mask_zero(dest); 618} 619 620Bool 621valuator_mask_has_unaccelerated(const ValuatorMask *mask) 622{ 623 return mask->has_unaccelerated; 624} 625 626void 627valuator_mask_drop_unaccelerated(ValuatorMask *mask) 628{ 629 memset(mask->unaccelerated, 0, sizeof(mask->unaccelerated)); 630 mask->has_unaccelerated = FALSE; 631} 632 633void 634valuator_mask_set_absolute_unaccelerated(ValuatorMask *mask, 635 int valuator, 636 int absolute, 637 double unaccel) 638{ 639 BUG_WARN_MSG(mask->last_bit != -1 && !mask->has_unaccelerated, 640 "Do not mix valuator types, zero mask first\n"); 641 _valuator_mask_set_double(mask, valuator, absolute); 642 mask->has_unaccelerated = TRUE; 643 mask->unaccelerated[valuator] = unaccel; 644} 645 646/** 647 * Set both accelerated and unaccelerated value for this mask. 648 */ 649void 650valuator_mask_set_unaccelerated(ValuatorMask *mask, 651 int valuator, 652 double accel, 653 double unaccel) 654{ 655 BUG_WARN_MSG(mask->last_bit != -1 && !mask->has_unaccelerated, 656 "Do not mix valuator types, zero mask first\n"); 657 _valuator_mask_set_double(mask, valuator, accel); 658 mask->has_unaccelerated = TRUE; 659 mask->unaccelerated[valuator] = unaccel; 660} 661 662double 663valuator_mask_get_accelerated(const ValuatorMask *mask, 664 int valuator) 665{ 666 return valuator_mask_get_double(mask, valuator); 667} 668 669double 670valuator_mask_get_unaccelerated(const ValuatorMask *mask, 671 int valuator) 672{ 673 return mask->unaccelerated[valuator]; 674} 675 676Bool 677valuator_mask_fetch_unaccelerated(const ValuatorMask *mask, 678 int valuator, 679 double *accel, 680 double *unaccel) 681{ 682 if (valuator_mask_isset(mask, valuator)) { 683 if (accel) 684 *accel = valuator_mask_get_accelerated(mask, valuator); 685 if (unaccel) 686 *unaccel = valuator_mask_get_unaccelerated(mask, valuator); 687 return TRUE; 688 } 689 else 690 return FALSE; 691} 692 693int 694CountBits(const uint8_t * mask, int len) 695{ 696 int i; 697 int ret = 0; 698 699 for (i = 0; i < len; i++) 700 if (BitIsOn(mask, i)) 701 ret++; 702 703 return ret; 704} 705 706/** 707 * Verifies sanity of the event. If the event is not an internal event, 708 * memdumps the first 32 bytes of event to the log, a backtrace, then kill 709 * the server. 710 */ 711void 712verify_internal_event(const InternalEvent *ev) 713{ 714 if (ev && ev->any.header != ET_Internal) { 715 int i; 716 const unsigned char *data = (const unsigned char *) ev; 717 718 ErrorF("dix: invalid event type %d\n", ev->any.header); 719 720 for (i = 0; i < sizeof(xEvent); i++, data++) { 721 ErrorF("%02hhx ", *data); 722 723 if ((i % 8) == 7) 724 ErrorF("\n"); 725 } 726 727 xorg_backtrace(); 728 FatalError("Wrong event type %d. Aborting server\n", ev->any.header); 729 } 730} 731 732/** 733 * Initializes the given event to zero (or default values), for the given 734 * device. 735 */ 736void 737init_device_event(DeviceEvent *event, DeviceIntPtr dev, Time ms, 738 enum DeviceEventSource source_type) 739{ 740 memset(event, 0, sizeof(DeviceEvent)); 741 event->header = ET_Internal; 742 event->length = sizeof(DeviceEvent); 743 event->time = ms; 744 event->deviceid = dev->id; 745 event->sourceid = dev->id; 746 event->source_type = source_type; 747} 748 749/** 750 * Initializes the given gesture event to zero (or default values), 751 * for the given device. 752 */ 753void 754init_gesture_event(GestureEvent *event, DeviceIntPtr dev, Time ms) 755{ 756 memset(event, 0, sizeof(GestureEvent)); 757 event->header = ET_Internal; 758 event->length = sizeof(GestureEvent); 759 event->time = ms; 760 event->deviceid = dev->id; 761 event->sourceid = dev->id; 762} 763 764int 765event_get_corestate(DeviceIntPtr mouse, DeviceIntPtr kbd) 766{ 767 int corestate; 768 769 /* core state needs to be assembled BEFORE the device is updated. */ 770 corestate = (kbd && 771 kbd->key) ? XkbStateFieldFromRec(&kbd->key->xkbInfo-> 772 state) : 0; 773 corestate |= (mouse && mouse->button) ? (mouse->button->state) : 0; 774 corestate |= (mouse && mouse->touch) ? (mouse->touch->state) : 0; 775 776 return corestate; 777} 778 779void 780event_set_state(DeviceIntPtr mouse, DeviceIntPtr kbd, DeviceEvent *event) 781{ 782 int i; 783 784 for (i = 0; mouse && mouse->button && i < mouse->button->numButtons; i++) 785 if (BitIsOn(mouse->button->down, i)) 786 SetBit(event->buttons, mouse->button->map[i]); 787 788 if (mouse && mouse->touch && mouse->touch->buttonsDown > 0) 789 SetBit(event->buttons, mouse->button->map[1]); 790 791 if (kbd && kbd->key) { 792 XkbStatePtr state; 793 794 /* we need the state before the event happens */ 795 if (event->type == ET_KeyPress || event->type == ET_KeyRelease) 796 state = &kbd->key->xkbInfo->prev_state; 797 else 798 state = &kbd->key->xkbInfo->state; 799 800 event->mods.base = state->base_mods; 801 event->mods.latched = state->latched_mods; 802 event->mods.locked = state->locked_mods; 803 event->mods.effective = state->mods; 804 805 event->group.base = state->base_group; 806 event->group.latched = state->latched_group; 807 event->group.locked = state->locked_group; 808 event->group.effective = state->group; 809 } 810} 811 812void 813event_set_state_gesture(DeviceIntPtr kbd, GestureEvent *event) 814{ 815 if (kbd && kbd->key) { 816 XkbStatePtr state= &kbd->key->xkbInfo->state; 817 818 event->mods.base = state->base_mods; 819 event->mods.latched = state->latched_mods; 820 event->mods.locked = state->locked_mods; 821 event->mods.effective = state->mods; 822 823 event->group.base = state->base_group; 824 event->group.latched = state->latched_group; 825 event->group.locked = state->locked_group; 826 event->group.effective = state->group; 827 } 828} 829 830/** 831 * Return the event filter mask for the given device and the given core or 832 * XI1 protocol type. 833 */ 834Mask 835event_get_filter_from_type(DeviceIntPtr dev, int evtype) 836{ 837 return event_filters[dev ? dev->id : 0][evtype]; 838} 839 840/** 841 * Return the event filter mask for the given device and the given core or 842 * XI2 protocol type. 843 */ 844Mask 845event_get_filter_from_xi2type(int evtype) 846{ 847 return (1 << (evtype % 8)); 848} 849 850Bool 851point_on_screen(ScreenPtr pScreen, int x, int y) 852{ 853 return x >= pScreen->x && x < pScreen->x + pScreen->width && 854 y >= pScreen->y && y < pScreen->y + pScreen->height; 855} 856 857/** 858 * Update desktop dimensions on the screenInfo struct. 859 */ 860void 861update_desktop_dimensions(void) 862{ 863 int i; 864 int x1 = INT_MAX, y1 = INT_MAX; /* top-left */ 865 int x2 = INT_MIN, y2 = INT_MIN; /* bottom-right */ 866 867 for (i = 0; i < screenInfo.numScreens; i++) { 868 ScreenPtr screen = screenInfo.screens[i]; 869 870 x1 = min(x1, screen->x); 871 y1 = min(y1, screen->y); 872 x2 = max(x2, screen->x + screen->width); 873 y2 = max(y2, screen->y + screen->height); 874 } 875 876 screenInfo.x = x1; 877 screenInfo.y = y1; 878 screenInfo.width = x2 - x1; 879 screenInfo.height = y2 - y1; 880} 881 882/* 883 * Delete the element with the key from the list, freeing all memory 884 * associated with the element.. 885 */ 886static void 887input_option_free(InputOption *o) 888{ 889 free(o->opt_name); 890 free(o->opt_val); 891 free(o->opt_comment); 892 free(o); 893} 894 895/* 896 * Create a new InputOption with the key/value pair provided. 897 * If a list is provided, the new options is added to the list and the list 898 * is returned. 899 * 900 * If a new option is added to a list that already contains that option, the 901 * previous option is overwritten. 902 * 903 * @param list The list to add to. 904 * @param key Option key, will be copied. 905 * @param value Option value, will be copied. 906 * 907 * @return If list is not NULL, the list with the new option added. If list 908 * is NULL, a new option list with one element. On failure, NULL is 909 * returned. 910 */ 911InputOption * 912input_option_new(InputOption *list, const char *key, const char *value) 913{ 914 InputOption *opt = NULL; 915 916 if (!key) 917 return NULL; 918 919 if (list) { 920 nt_list_for_each_entry(opt, list, list.next) { 921 if (strcmp(input_option_get_key(opt), key) == 0) { 922 input_option_set_value(opt, value); 923 return list; 924 } 925 } 926 } 927 928 opt = calloc(1, sizeof(InputOption)); 929 if (!opt) 930 return NULL; 931 932 nt_list_init(opt, list.next); 933 input_option_set_key(opt, key); 934 input_option_set_value(opt, value); 935 936 if (list) { 937 nt_list_append(opt, list, InputOption, list.next); 938 939 return list; 940 } 941 else 942 return opt; 943} 944 945InputOption * 946input_option_free_element(InputOption *list, const char *key) 947{ 948 InputOption *element; 949 950 nt_list_for_each_entry(element, list, list.next) { 951 if (strcmp(input_option_get_key(element), key) == 0) { 952 nt_list_del(element, list, InputOption, list.next); 953 954 input_option_free(element); 955 break; 956 } 957 } 958 return list; 959} 960 961/** 962 * Free the list pointed at by opt. 963 */ 964void 965input_option_free_list(InputOption **opt) 966{ 967 InputOption *element, *tmp; 968 969 nt_list_for_each_entry_safe(element, tmp, *opt, list.next) { 970 nt_list_del(element, *opt, InputOption, list.next); 971 972 input_option_free(element); 973 } 974 *opt = NULL; 975} 976 977/** 978 * Find the InputOption with the given option name. 979 * 980 * @return The InputOption or NULL if not present. 981 */ 982InputOption * 983input_option_find(InputOption *list, const char *key) 984{ 985 InputOption *element; 986 987 nt_list_for_each_entry(element, list, list.next) { 988 if (strcmp(input_option_get_key(element), key) == 0) 989 return element; 990 } 991 992 return NULL; 993} 994 995const char * 996input_option_get_key(const InputOption *opt) 997{ 998 return opt->opt_name; 999} 1000 1001const char * 1002input_option_get_value(const InputOption *opt) 1003{ 1004 return opt->opt_val; 1005} 1006 1007void 1008input_option_set_key(InputOption *opt, const char *key) 1009{ 1010 free(opt->opt_name); 1011 if (key) 1012 opt->opt_name = strdup(key); 1013} 1014 1015void 1016input_option_set_value(InputOption *opt, const char *value) 1017{ 1018 free(opt->opt_val); 1019 if (value) 1020 opt->opt_val = strdup(value); 1021} 1022 1023/* FP1616/FP3232 conversion functions. 1024 * Fixed point types are encoded as signed integral and unsigned frac. So any 1025 * negative number -n.m is encoded as floor(n) + (1 - 0.m). 1026 */ 1027double 1028fp1616_to_double(FP1616 in) 1029{ 1030 return pixman_fixed_to_double(in); 1031} 1032 1033double 1034fp3232_to_double(FP3232 in) 1035{ 1036 double ret; 1037 1038 ret = (double) in.integral; 1039 ret += (double) in.frac * (1.0 / (1ULL << 32)); /* Optimized: ldexp((double)in.frac, -32); */ 1040 return ret; 1041} 1042 1043FP1616 1044double_to_fp1616(double in) 1045{ 1046 return pixman_double_to_fixed(in); 1047} 1048 1049FP3232 1050double_to_fp3232(double in) 1051{ 1052 FP3232 ret; 1053 int32_t integral; 1054 double tmp; 1055 uint32_t frac_d; 1056 1057 tmp = floor(in); 1058 integral = (int32_t) tmp; 1059 1060 tmp = (in - integral) * (1ULL << 32); /* Optimized: ldexp(in - integral, 32) */ 1061 frac_d = (uint32_t) tmp; 1062 1063 ret.integral = integral; 1064 ret.frac = frac_d; 1065 return ret; 1066} 1067 1068/** 1069 * DO NOT USE THIS FUNCTION. It only exists for the test cases. Use 1070 * xi2mask_new() instead to get the standard sized masks. 1071 * 1072 * @param nmasks The number of masks (== number of devices) 1073 * @param size The size of the masks in bytes 1074 * @return The new mask or NULL on allocation error. 1075 */ 1076XI2Mask * 1077xi2mask_new_with_size(size_t nmasks, size_t size) 1078{ 1079 int i; 1080 int alloc_size; 1081 unsigned char *cursor; 1082 XI2Mask *mask; 1083 1084 alloc_size = sizeof(struct _XI2Mask) 1085 + nmasks * sizeof(unsigned char *) 1086 + nmasks * size; 1087 1088 mask = calloc(1, alloc_size); 1089 1090 if (!mask) 1091 return NULL; 1092 1093 mask->nmasks = nmasks; 1094 mask->mask_size = size; 1095 1096 mask->masks = (unsigned char **)(mask + 1); 1097 cursor = (unsigned char *)(mask + 1) + nmasks * sizeof(unsigned char *); 1098 1099 for (i = 0; i < nmasks; i++) { 1100 mask->masks[i] = cursor; 1101 cursor += size; 1102 } 1103 return mask; 1104} 1105 1106/** 1107 * Create a new XI2 mask of the standard size, i.e. for all devices + fake 1108 * devices and for the highest supported XI2 event type. 1109 * 1110 * @return The new mask or NULL on allocation error. 1111 */ 1112XI2Mask * 1113xi2mask_new(void) 1114{ 1115 return xi2mask_new_with_size(EMASKSIZE, XI2MASKSIZE); 1116} 1117 1118/** 1119 * Frees memory associated with mask and resets mask to NULL. 1120 */ 1121void 1122xi2mask_free(XI2Mask **mask) 1123{ 1124 if (!(*mask)) 1125 return; 1126 1127 free((*mask)); 1128 *mask = NULL; 1129} 1130 1131/** 1132 * Test if the bit for event type is set for this device only. 1133 * 1134 * @return TRUE if the bit is set, FALSE otherwise 1135 */ 1136Bool 1137xi2mask_isset_for_device(XI2Mask *mask, const DeviceIntPtr dev, int event_type) 1138{ 1139 BUG_WARN(dev->id < 0); 1140 BUG_WARN(dev->id >= mask->nmasks); 1141 BUG_WARN(bits_to_bytes(event_type + 1) > mask->mask_size); 1142 1143 return BitIsOn(mask->masks[dev->id], event_type); 1144} 1145 1146/** 1147 * Test if the bit for event type is set for this device, or the 1148 * XIAllDevices/XIAllMasterDevices (if applicable) is set. 1149 * 1150 * @return TRUE if the bit is set, FALSE otherwise 1151 */ 1152Bool 1153xi2mask_isset(XI2Mask *mask, const DeviceIntPtr dev, int event_type) 1154{ 1155 int set = 0; 1156 1157 if (xi2mask_isset_for_device(mask, inputInfo.all_devices, event_type)) 1158 set = 1; 1159 else if (xi2mask_isset_for_device(mask, dev, event_type)) 1160 set = 1; 1161 else if (IsMaster(dev) && xi2mask_isset_for_device(mask, inputInfo.all_master_devices, event_type)) 1162 set = 1; 1163 1164 return set; 1165} 1166 1167/** 1168 * Set the mask bit for this event type for this device. 1169 */ 1170void 1171xi2mask_set(XI2Mask *mask, int deviceid, int event_type) 1172{ 1173 BUG_WARN(deviceid < 0); 1174 BUG_WARN(deviceid >= mask->nmasks); 1175 BUG_WARN(bits_to_bytes(event_type + 1) > mask->mask_size); 1176 1177 SetBit(mask->masks[deviceid], event_type); 1178} 1179 1180/** 1181 * Zero out the xi2mask, for the deviceid given. If the deviceid is < 0, all 1182 * masks are zeroed. 1183 */ 1184void 1185xi2mask_zero(XI2Mask *mask, int deviceid) 1186{ 1187 int i; 1188 1189 BUG_WARN(deviceid > 0 && deviceid >= mask->nmasks); 1190 1191 if (deviceid >= 0) 1192 memset(mask->masks[deviceid], 0, mask->mask_size); 1193 else 1194 for (i = 0; i < mask->nmasks; i++) 1195 memset(mask->masks[i], 0, mask->mask_size); 1196} 1197 1198/** 1199 * Merge source into dest, i.e. dest |= source. 1200 * If the masks are of different size, only the overlapping section is merged. 1201 */ 1202void 1203xi2mask_merge(XI2Mask *dest, const XI2Mask *source) 1204{ 1205 int i, j; 1206 1207 for (i = 0; i < min(dest->nmasks, source->nmasks); i++) 1208 for (j = 0; j < min(dest->mask_size, source->mask_size); j++) 1209 dest->masks[i][j] |= source->masks[i][j]; 1210} 1211 1212/** 1213 * @return The number of masks in mask 1214 */ 1215size_t 1216xi2mask_num_masks(const XI2Mask *mask) 1217{ 1218 return mask->nmasks; 1219} 1220 1221/** 1222 * @return The size of each mask in bytes 1223 */ 1224size_t 1225xi2mask_mask_size(const XI2Mask *mask) 1226{ 1227 return mask->mask_size; 1228} 1229 1230/** 1231 * Set the mask for the given deviceid to the source mask. 1232 * If the mask given is larger than the target memory, only the overlapping 1233 * parts are copied. 1234 */ 1235void 1236xi2mask_set_one_mask(XI2Mask *xi2mask, int deviceid, const unsigned char *mask, 1237 size_t mask_size) 1238{ 1239 BUG_WARN(deviceid < 0); 1240 BUG_WARN(deviceid >= xi2mask->nmasks); 1241 1242 memcpy(xi2mask->masks[deviceid], mask, min(xi2mask->mask_size, mask_size)); 1243} 1244 1245/** 1246 * Get a reference to the XI2mask for this particular device. 1247 */ 1248const unsigned char * 1249xi2mask_get_one_mask(const XI2Mask *mask, int deviceid) 1250{ 1251 BUG_WARN(deviceid < 0); 1252 BUG_WARN(deviceid >= mask->nmasks); 1253 1254 return mask->masks[deviceid]; 1255} 1256 1257/** 1258 * Copies a sprite data from src to dst sprites. 1259 * 1260 * Returns FALSE on error. 1261 */ 1262Bool 1263CopySprite(SpritePtr src, SpritePtr dst) 1264{ 1265 WindowPtr *trace; 1266 if (src->spriteTraceGood > dst->spriteTraceSize) { 1267 trace = reallocarray(dst->spriteTrace, 1268 src->spriteTraceSize, sizeof(*trace)); 1269 if (!trace) { 1270 dst->spriteTraceGood = 0; 1271 return FALSE; 1272 } 1273 dst->spriteTrace = trace; 1274 dst->spriteTraceSize = src->spriteTraceGood; 1275 } 1276 memcpy(dst->spriteTrace, src->spriteTrace, 1277 src->spriteTraceGood * sizeof(*trace)); 1278 dst->spriteTraceGood = src->spriteTraceGood; 1279 return TRUE; 1280} 1281