| 1 | /* |
| 2 | * Copyright 2008 Red Hat, Inc. |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice (including the next |
| 12 | * paragraph) shall be included in all copies or substantial portions of the |
| 13 | * Software. |
| 14 | * |
| 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: Peter Hutterer |
| 24 | */ |
| 25 | |
| 26 | #ifdef HAVE_DIX_CONFIG_H |
| 27 | #include <dix-config.h> |
| 28 | #endif |
| 29 | |
| 30 | #include "dixstruct.h" |
| 31 | #include "windowstr.h" |
| 32 | #include "exglobals.h" |
| 33 | #include "exevents.h" |
| 34 | #include <X11/extensions/XI2proto.h> |
| 35 | #include "inpututils.h" |
| 36 | |
| 37 | #include "xiselectev.h" |
| 38 | |
| 39 | /** |
| 40 | * Ruleset: |
| 41 | * - if A has XIAllDevices, B may select on device X |
| 42 | * - If A has XIAllDevices, B may select on XIAllMasterDevices |
| 43 | * - If A has XIAllMasterDevices, B may select on device X |
| 44 | * - If A has XIAllMasterDevices, B may select on XIAllDevices |
| 45 | * - if A has device X, B may select on XIAllDevices/XIAllMasterDevices |
| 46 | */ |
| 47 | static int check_for_touch_selection_conflicts(ClientPtr B, WindowPtr win, int deviceid) |
| 48 | { |
| 49 | OtherInputMasks *inputMasks = wOtherInputMasks(win); |
| 50 | InputClients *A = NULL; |
| 51 | |
| 52 | if (inputMasks) |
| 53 | A = inputMasks->inputClients; |
| 54 | for (; A; A = A->next) { |
| 55 | DeviceIntPtr tmp; |
| 56 | |
| 57 | if (CLIENT_ID(A->resource) == B->index) |
| 58 | continue; |
| 59 | |
| 60 | if (deviceid == XIAllDevices) |
| 61 | tmp = inputInfo.all_devices; |
| 62 | else if (deviceid == XIAllMasterDevices) |
| 63 | tmp = inputInfo.all_master_devices; |
| 64 | else |
| 65 | dixLookupDevice(&tmp, deviceid, serverClient, DixReadAccess); |
| 66 | if (!tmp) |
| 67 | return BadImplementation; /* this shouldn't happen */ |
| 68 | |
| 69 | /* A has XIAllDevices */ |
| 70 | if (xi2mask_isset_for_device(A->xi2mask, inputInfo.all_devices, XI_TouchBegin)) { |
| 71 | if (deviceid == XIAllDevices) |
| 72 | return BadAccess; |
| 73 | } |
| 74 | |
| 75 | /* A has XIAllMasterDevices */ |
| 76 | if (xi2mask_isset_for_device(A->xi2mask, inputInfo.all_master_devices, XI_TouchBegin)) { |
| 77 | if (deviceid == XIAllMasterDevices) |
| 78 | return BadAccess; |
| 79 | } |
| 80 | |
| 81 | /* A has this device */ |
| 82 | if (xi2mask_isset_for_device(A->xi2mask, tmp, XI_TouchBegin)) |
| 83 | return BadAccess; |
| 84 | } |
| 85 | |
| 86 | return Success; |
| 87 | } |
| 88 | |
| 89 | |
| 90 | /** |
| 91 | * Check the given mask (in len bytes) for invalid mask bits. |
| 92 | * Invalid mask bits are any bits above XI2LastEvent. |
| 93 | * |
| 94 | * @return BadValue if at least one invalid bit is set or Success otherwise. |
| 95 | */ |
| 96 | int |
| 97 | XICheckInvalidMaskBits(ClientPtr client, unsigned char *mask, int len) |
| 98 | { |
| 99 | if (len >= XIMaskLen(XI2LASTEVENT)) { |
| 100 | int i; |
| 101 | |
| 102 | for (i = XI2LASTEVENT + 1; i < len * 8; i++) { |
| 103 | if (BitIsOn(mask, i)) { |
| 104 | client->errorValue = i; |
| 105 | return BadValue; |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | return Success; |
| 111 | } |
| 112 | |
| 113 | int |
| 114 | SProcXISelectEvents(ClientPtr client) |
| 115 | { |
| 116 | int i; |
| 117 | xXIEventMask *evmask; |
| 118 | |
| 119 | REQUEST(xXISelectEventsReq); |
| 120 | swaps(&stuff->length); |
| 121 | REQUEST_AT_LEAST_SIZE(xXISelectEventsReq); |
| 122 | swapl(&stuff->win); |
| 123 | swaps(&stuff->num_masks); |
| 124 | |
| 125 | evmask = (xXIEventMask *) &stuff[1]; |
| 126 | for (i = 0; i < stuff->num_masks; i++) { |
| 127 | swaps(&evmask->deviceid); |
| 128 | swaps(&evmask->mask_len); |
| 129 | evmask = |
| 130 | (xXIEventMask *) (((char *) &evmask[1]) + evmask->mask_len * 4); |
| 131 | } |
| 132 | |
| 133 | return (ProcXISelectEvents(client)); |
| 134 | } |
| 135 | |
| 136 | int |
| 137 | ProcXISelectEvents(ClientPtr client) |
| 138 | { |
| 139 | int rc, num_masks; |
| 140 | WindowPtr win; |
| 141 | DeviceIntPtr dev; |
| 142 | DeviceIntRec dummy; |
| 143 | xXIEventMask *evmask; |
| 144 | int *types = NULL; |
| 145 | int len; |
| 146 | |
| 147 | REQUEST(xXISelectEventsReq); |
| 148 | REQUEST_AT_LEAST_SIZE(xXISelectEventsReq); |
| 149 | |
| 150 | if (stuff->num_masks == 0) |
| 151 | return BadValue; |
| 152 | |
| 153 | rc = dixLookupWindow(&win, stuff->win, client, DixReceiveAccess); |
| 154 | if (rc != Success) |
| 155 | return rc; |
| 156 | |
| 157 | len = sz_xXISelectEventsReq; |
| 158 | |
| 159 | /* check request validity */ |
| 160 | evmask = (xXIEventMask *) &stuff[1]; |
| 161 | num_masks = stuff->num_masks; |
| 162 | while (num_masks--) { |
| 163 | len += sizeof(xXIEventMask) + evmask->mask_len * 4; |
| 164 | |
| 165 | if (bytes_to_int32(len) > stuff->length) |
| 166 | return BadLength; |
| 167 | |
| 168 | if (evmask->deviceid != XIAllDevices && |
| 169 | evmask->deviceid != XIAllMasterDevices) |
| 170 | rc = dixLookupDevice(&dev, evmask->deviceid, client, DixUseAccess); |
| 171 | else { |
| 172 | /* XXX: XACE here? */ |
| 173 | } |
| 174 | if (rc != Success) |
| 175 | return rc; |
| 176 | |
| 177 | /* hierarchy event mask is not allowed on devices */ |
| 178 | if (evmask->deviceid != XIAllDevices && evmask->mask_len >= 1) { |
| 179 | unsigned char *bits = (unsigned char *) &evmask[1]; |
| 180 | |
| 181 | if (BitIsOn(bits, XI_HierarchyChanged)) { |
| 182 | client->errorValue = XI_HierarchyChanged; |
| 183 | return BadValue; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | /* Raw events may only be selected on root windows */ |
| 188 | if (win->parent && evmask->mask_len >= 1) { |
| 189 | unsigned char *bits = (unsigned char *) &evmask[1]; |
| 190 | |
| 191 | if (BitIsOn(bits, XI_RawKeyPress) || |
| 192 | BitIsOn(bits, XI_RawKeyRelease) || |
| 193 | BitIsOn(bits, XI_RawButtonPress) || |
| 194 | BitIsOn(bits, XI_RawButtonRelease) || |
| 195 | BitIsOn(bits, XI_RawMotion) || |
| 196 | BitIsOn(bits, XI_RawTouchBegin) || |
| 197 | BitIsOn(bits, XI_RawTouchUpdate) || |
| 198 | BitIsOn(bits, XI_RawTouchEnd)) { |
| 199 | client->errorValue = XI_RawKeyPress; |
| 200 | return BadValue; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | if (evmask->mask_len >= 1) { |
| 205 | unsigned char *bits = (unsigned char *) &evmask[1]; |
| 206 | |
| 207 | /* All three touch events must be selected at once */ |
| 208 | if ((BitIsOn(bits, XI_TouchBegin) || |
| 209 | BitIsOn(bits, XI_TouchUpdate) || |
| 210 | BitIsOn(bits, XI_TouchOwnership) || |
| 211 | BitIsOn(bits, XI_TouchEnd)) && |
| 212 | (!BitIsOn(bits, XI_TouchBegin) || |
| 213 | !BitIsOn(bits, XI_TouchUpdate) || |
| 214 | !BitIsOn(bits, XI_TouchEnd))) { |
| 215 | client->errorValue = XI_TouchBegin; |
| 216 | return BadValue; |
| 217 | } |
| 218 | |
| 219 | /* Only one client per window may select for touch events on the |
| 220 | * same devices, including master devices. |
| 221 | * XXX: This breaks if a device goes from floating to attached. */ |
| 222 | if (BitIsOn(bits, XI_TouchBegin)) { |
| 223 | rc = check_for_touch_selection_conflicts(client, |
| 224 | win, |
| 225 | evmask->deviceid); |
| 226 | if (rc != Success) |
| 227 | return rc; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | if (XICheckInvalidMaskBits(client, (unsigned char *) &evmask[1], |
| 232 | evmask->mask_len * 4) != Success) |
| 233 | return BadValue; |
| 234 | |
| 235 | evmask = |
| 236 | (xXIEventMask *) (((unsigned char *) evmask) + |
| 237 | evmask->mask_len * 4); |
| 238 | evmask++; |
| 239 | } |
| 240 | |
| 241 | if (bytes_to_int32(len) != stuff->length) |
| 242 | return BadLength; |
| 243 | |
| 244 | /* Set masks on window */ |
| 245 | evmask = (xXIEventMask *) &stuff[1]; |
| 246 | num_masks = stuff->num_masks; |
| 247 | while (num_masks--) { |
| 248 | if (evmask->deviceid == XIAllDevices || |
| 249 | evmask->deviceid == XIAllMasterDevices) { |
| 250 | dummy.id = evmask->deviceid; |
| 251 | dev = &dummy; |
| 252 | } |
| 253 | else |
| 254 | dixLookupDevice(&dev, evmask->deviceid, client, DixUseAccess); |
| 255 | if (XISetEventMask(dev, win, client, evmask->mask_len * 4, |
| 256 | (unsigned char *) &evmask[1]) != Success) |
| 257 | return BadAlloc; |
| 258 | evmask = |
| 259 | (xXIEventMask *) (((unsigned char *) evmask) + |
| 260 | evmask->mask_len * 4); |
| 261 | evmask++; |
| 262 | } |
| 263 | |
| 264 | RecalculateDeliverableEvents(win); |
| 265 | |
| 266 | free(types); |
| 267 | return Success; |
| 268 | } |
| 269 | |
| 270 | int |
| 271 | SProcXIGetSelectedEvents(ClientPtr client) |
| 272 | { |
| 273 | REQUEST(xXIGetSelectedEventsReq); |
| 274 | swaps(&stuff->length); |
| 275 | REQUEST_SIZE_MATCH(xXIGetSelectedEventsReq); |
| 276 | swapl(&stuff->win); |
| 277 | |
| 278 | return (ProcXIGetSelectedEvents(client)); |
| 279 | } |
| 280 | |
| 281 | int |
| 282 | ProcXIGetSelectedEvents(ClientPtr client) |
| 283 | { |
| 284 | int rc, i; |
| 285 | WindowPtr win; |
| 286 | char *buffer = NULL; |
| 287 | xXIGetSelectedEventsReply reply; |
| 288 | OtherInputMasks *masks; |
| 289 | InputClientsPtr others = NULL; |
| 290 | xXIEventMask *evmask = NULL; |
| 291 | DeviceIntPtr dev; |
| 292 | |
| 293 | REQUEST(xXIGetSelectedEventsReq); |
| 294 | REQUEST_SIZE_MATCH(xXIGetSelectedEventsReq); |
| 295 | |
| 296 | rc = dixLookupWindow(&win, stuff->win, client, DixGetAttrAccess); |
| 297 | if (rc != Success) |
| 298 | return rc; |
| 299 | |
| 300 | reply = (xXIGetSelectedEventsReply) { |
| 301 | .repType = X_Reply, |
| 302 | .RepType = X_XIGetSelectedEvents, |
| 303 | .sequenceNumber = client->sequence, |
| 304 | .length = 0, |
| 305 | .num_masks = 0 |
| 306 | }; |
| 307 | |
| 308 | masks = wOtherInputMasks(win); |
| 309 | if (masks) { |
| 310 | for (others = wOtherInputMasks(win)->inputClients; others; |
| 311 | others = others->next) { |
| 312 | if (SameClient(others, client)) { |
| 313 | break; |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | if (!others) { |
| 319 | WriteReplyToClient(client, sizeof(xXIGetSelectedEventsReply), &reply); |
| 320 | return Success; |
| 321 | } |
| 322 | |
| 323 | buffer = |
| 324 | calloc(MAXDEVICES, sizeof(xXIEventMask) + pad_to_int32(XI2MASKSIZE)); |
| 325 | if (!buffer) |
| 326 | return BadAlloc; |
| 327 | |
| 328 | evmask = (xXIEventMask *) buffer; |
| 329 | for (i = 0; i < MAXDEVICES; i++) { |
| 330 | int j; |
| 331 | const unsigned char *devmask = xi2mask_get_one_mask(others->xi2mask, i); |
| 332 | |
| 333 | if (i > 2) { |
| 334 | rc = dixLookupDevice(&dev, i, client, DixGetAttrAccess); |
| 335 | if (rc != Success) |
| 336 | continue; |
| 337 | } |
| 338 | |
| 339 | for (j = xi2mask_mask_size(others->xi2mask) - 1; j >= 0; j--) { |
| 340 | if (devmask[j] != 0) { |
| 341 | int mask_len = (j + 4) / 4; /* j is an index, hence + 4, not + 3 */ |
| 342 | |
| 343 | evmask->deviceid = i; |
| 344 | evmask->mask_len = mask_len; |
| 345 | reply.num_masks++; |
| 346 | reply.length += sizeof(xXIEventMask) / 4 + evmask->mask_len; |
| 347 | |
| 348 | if (client->swapped) { |
| 349 | swaps(&evmask->deviceid); |
| 350 | swaps(&evmask->mask_len); |
| 351 | } |
| 352 | |
| 353 | memcpy(&evmask[1], devmask, j + 1); |
| 354 | evmask = (xXIEventMask *) ((char *) evmask + |
| 355 | sizeof(xXIEventMask) + mask_len * 4); |
| 356 | break; |
| 357 | } |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | WriteReplyToClient(client, sizeof(xXIGetSelectedEventsReply), &reply); |
| 362 | |
| 363 | if (reply.num_masks) |
| 364 | WriteToClient(client, reply.length * 4, buffer); |
| 365 | |
| 366 | free(buffer); |
| 367 | return Success; |
| 368 | } |
| 369 | |
| 370 | void |
| 371 | SRepXIGetSelectedEvents(ClientPtr client, |
| 372 | int len, xXIGetSelectedEventsReply * rep) |
| 373 | { |
| 374 | swaps(&rep->sequenceNumber); |
| 375 | swapl(&rep->length); |
| 376 | swaps(&rep->num_masks); |
| 377 | WriteToClient(client, len, rep); |
| 378 | } |