xiselectev.c 12 KB

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