grabdev.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /************************************************************
  2. Copyright 1989, 1998 The Open Group
  3. Permission to use, copy, modify, distribute, and sell this software and its
  4. documentation for any purpose is hereby granted without fee, provided that
  5. the above copyright notice appear in all copies and that both that
  6. copyright notice and this permission notice appear in supporting
  7. documentation.
  8. The above copyright notice and this permission notice shall be included in
  9. all copies or substantial portions of the Software.
  10. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  11. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  12. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  13. OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  14. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  15. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. Except as contained in this notice, the name of The Open Group shall not be
  17. used in advertising or otherwise to promote the sale, use or other dealings
  18. in this Software without prior written authorization from The Open Group.
  19. Copyright 1989 by Hewlett-Packard Company, Palo Alto, California.
  20. All Rights Reserved
  21. Permission to use, copy, modify, and distribute this software and its
  22. documentation for any purpose and without fee is hereby granted,
  23. provided that the above copyright notice appear in all copies and that
  24. both that copyright notice and this permission notice appear in
  25. supporting documentation, and that the name of Hewlett-Packard not be
  26. used in advertising or publicity pertaining to distribution of the
  27. software without specific, written prior permission.
  28. HEWLETT-PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  29. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  30. HEWLETT-PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  31. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  32. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  33. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  34. SOFTWARE.
  35. ********************************************************/
  36. /***********************************************************************
  37. *
  38. * Extension function to grab an extension device.
  39. *
  40. */
  41. #ifdef HAVE_DIX_CONFIG_H
  42. #include <dix-config.h>
  43. #endif
  44. #include "inputstr.h" /* DeviceIntPtr */
  45. #include "windowstr.h" /* window structure */
  46. #include <X11/extensions/XI.h>
  47. #include <X11/extensions/XIproto.h>
  48. #include "exglobals.h"
  49. #include "dixevents.h" /* GrabDevice */
  50. #include "grabdev.h"
  51. extern XExtEventInfo EventInfo[];
  52. extern int ExtEventIndex;
  53. /***********************************************************************
  54. *
  55. * Swap the request if the requestor has a different byte order than us.
  56. *
  57. */
  58. int
  59. SProcXGrabDevice(ClientPtr client)
  60. {
  61. REQUEST(xGrabDeviceReq);
  62. swaps(&stuff->length);
  63. REQUEST_AT_LEAST_SIZE(xGrabDeviceReq);
  64. swapl(&stuff->grabWindow);
  65. swapl(&stuff->time);
  66. swaps(&stuff->event_count);
  67. if (stuff->length !=
  68. bytes_to_int32(sizeof(xGrabDeviceReq)) + stuff->event_count)
  69. return BadLength;
  70. SwapLongs((CARD32 *) (&stuff[1]), stuff->event_count);
  71. return (ProcXGrabDevice(client));
  72. }
  73. /***********************************************************************
  74. *
  75. * Grab an extension device.
  76. *
  77. */
  78. int
  79. ProcXGrabDevice(ClientPtr client)
  80. {
  81. int rc;
  82. xGrabDeviceReply rep;
  83. DeviceIntPtr dev;
  84. GrabMask mask;
  85. struct tmask tmp[EMASKSIZE];
  86. REQUEST(xGrabDeviceReq);
  87. REQUEST_AT_LEAST_SIZE(xGrabDeviceReq);
  88. if (stuff->length !=
  89. bytes_to_int32(sizeof(xGrabDeviceReq)) + stuff->event_count)
  90. return BadLength;
  91. rep = (xGrabDeviceReply) {
  92. .repType = X_Reply,
  93. .RepType = X_GrabDevice,
  94. .sequenceNumber = client->sequence,
  95. .length = 0,
  96. };
  97. rc = dixLookupDevice(&dev, stuff->deviceid, client, DixGrabAccess);
  98. if (rc != Success)
  99. return rc;
  100. if ((rc = CreateMaskFromList(client, (XEventClass *) &stuff[1],
  101. stuff->event_count, tmp, dev,
  102. X_GrabDevice)) != Success)
  103. return rc;
  104. mask.xi = tmp[stuff->deviceid].mask;
  105. rc = GrabDevice(client, dev, stuff->other_devices_mode,
  106. stuff->this_device_mode, stuff->grabWindow,
  107. stuff->ownerEvents, stuff->time,
  108. &mask, XI, None, None, &rep.status);
  109. if (rc != Success)
  110. return rc;
  111. WriteReplyToClient(client, sizeof(xGrabDeviceReply), &rep);
  112. return Success;
  113. }
  114. /***********************************************************************
  115. *
  116. * This procedure creates an event mask from a list of XEventClasses.
  117. *
  118. * Procedure is as follows:
  119. * An XEventClass is (deviceid << 8 | eventtype). For each entry in the list,
  120. * get the device. Then run through all available event indices (those are
  121. * set when XI starts up) and binary OR's the device's mask to whatever the
  122. * event mask for the given event type was.
  123. * If an error occurs, it is sent to the client. Errors are generated if
  124. * - if the device given in the event classs is invalid
  125. * - if the device in the class list is not the device given as parameter (no
  126. * error if parameter is NULL)
  127. *
  128. * mask has to be size EMASKSIZE and pre-allocated.
  129. *
  130. * @param client The client to send the error to (if one occurs)
  131. * @param list List of event classes as sent from the client.
  132. * @param count Number of elements in list.
  133. * @param mask Preallocated mask (size EMASKSIZE).
  134. * @param dev The device we're creating masks for.
  135. * @param req The request we're processing. Used to fill in error fields.
  136. */
  137. int
  138. CreateMaskFromList(ClientPtr client, XEventClass * list, int count,
  139. struct tmask *mask, DeviceIntPtr dev, int req)
  140. {
  141. int rc, i, j;
  142. int device;
  143. DeviceIntPtr tdev;
  144. memset(mask, 0, EMASKSIZE * sizeof(struct tmask));
  145. for (i = 0; i < count; i++, list++) {
  146. device = *list >> 8;
  147. if (device > 255)
  148. return BadClass;
  149. rc = dixLookupDevice(&tdev, device, client, DixUseAccess);
  150. if (rc != BadDevice && rc != Success)
  151. return rc;
  152. if (rc == BadDevice || (dev != NULL && tdev != dev))
  153. return BadClass;
  154. for (j = 0; j < ExtEventIndex; j++)
  155. if (EventInfo[j].type == (*list & 0xff)) {
  156. mask[device].mask |= EventInfo[j].mask;
  157. mask[device].dev = (void *) tdev;
  158. break;
  159. }
  160. }
  161. return Success;
  162. }
  163. /***********************************************************************
  164. *
  165. * This procedure writes the reply for the XGrabDevice function,
  166. * if the client and server have a different byte ordering.
  167. *
  168. */
  169. void
  170. SRepXGrabDevice(ClientPtr client, int size, xGrabDeviceReply * rep)
  171. {
  172. swaps(&rep->sequenceNumber);
  173. swapl(&rep->length);
  174. WriteToClient(client, size, rep);
  175. }