PageRenderTime 52ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/test/xi2/protocol-xigetselectedevents.c

#
C | 234 lines | 147 code | 42 blank | 45 comment | 17 complexity | 46ca326d21954d63b5a5d47b083e4fce MD5 | raw file
Possible License(s): MIT
  1. /**
  2. * Copyright Š 2009 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. #ifdef HAVE_DIX_CONFIG_H
  24. #include <dix-config.h>
  25. #endif
  26. /*
  27. * Protocol testing for XIGetSelectedEvents request.
  28. *
  29. * Tests include:
  30. * BadWindow on wrong window.
  31. * Zero-length masks if no masks are set.
  32. * Valid masks for valid devices.
  33. * Masks set on non-existent devices are not returned.
  34. *
  35. * Note that this test is not connected to the XISelectEvents request.
  36. */
  37. #include <stdint.h>
  38. #include <X11/X.h>
  39. #include <X11/Xproto.h>
  40. #include <X11/extensions/XI2proto.h>
  41. #include "inputstr.h"
  42. #include "windowstr.h"
  43. #include "extinit.h" /* for XInputExtensionInit */
  44. #include "scrnintstr.h"
  45. #include "xiselectev.h"
  46. #include "exevents.h"
  47. #include "protocol-common.h"
  48. static void reply_XIGetSelectedEvents(ClientPtr client, int len, char *data, void *userdata);
  49. static void reply_XIGetSelectedEvents_data(ClientPtr client, int len, char *data, void *userdata);
  50. struct {
  51. int num_masks_expected;
  52. unsigned char mask[MAXDEVICES][XI2LASTEVENT]; /* intentionally bigger */
  53. int mask_len;
  54. } test_data;
  55. /* dixLookupWindow requires a lot of setup not necessary for this test.
  56. * Simple wrapper that returns either one of the fake root window or the
  57. * fake client window. If the requested ID is neither of those wanted,
  58. * return whatever the real dixLookupWindow does.
  59. */
  60. int __wrap_dixLookupWindow(WindowPtr *win, XID id, ClientPtr client, Mask access)
  61. {
  62. if (id == root.drawable.id)
  63. {
  64. *win = &root;
  65. return Success;
  66. } else if (id == window.drawable.id)
  67. {
  68. *win = &window;
  69. return Success;
  70. }
  71. return __real_dixLookupWindow(win, id, client, access);
  72. }
  73. /* AddResource is called from XISetSEventMask, we don't need this */
  74. Bool __wrap_AddResource(XID id, RESTYPE type, pointer value)
  75. {
  76. return TRUE;
  77. }
  78. static void reply_XIGetSelectedEvents(ClientPtr client, int len, char *data, void *userdata)
  79. {
  80. xXIGetSelectedEventsReply *rep = (xXIGetSelectedEventsReply*)data;
  81. if (client->swapped)
  82. {
  83. swapl(&rep->length);
  84. swaps(&rep->sequenceNumber);
  85. swaps(&rep->num_masks);
  86. }
  87. reply_check_defaults(rep, len, XIGetSelectedEvents);
  88. assert(rep->num_masks == test_data.num_masks_expected);
  89. reply_handler = reply_XIGetSelectedEvents_data;
  90. }
  91. static void reply_XIGetSelectedEvents_data(ClientPtr client, int len, char *data, void *userdata)
  92. {
  93. int i;
  94. xXIEventMask *mask;
  95. unsigned char *bitmask;
  96. mask = (xXIEventMask*)data;
  97. for (i = 0; i < test_data.num_masks_expected; i++)
  98. {
  99. if (client->swapped)
  100. {
  101. swaps(&mask->deviceid);
  102. swaps(&mask->mask_len);
  103. }
  104. assert(mask->deviceid < 6);
  105. assert(mask->mask_len <= (((XI2LASTEVENT + 8)/8) + 3)/4) ;
  106. bitmask = (unsigned char*)&mask[1];
  107. assert(memcmp(bitmask,
  108. test_data.mask[mask->deviceid],
  109. mask->mask_len * 4) == 0);
  110. mask = (xXIEventMask*)((char*)mask + mask->mask_len * 4 + sizeof(xXIEventMask));
  111. }
  112. }
  113. static void request_XIGetSelectedEvents(xXIGetSelectedEventsReq* req, int error)
  114. {
  115. int rc;
  116. ClientRec client;
  117. client = init_client(req->length, req);
  118. reply_handler = reply_XIGetSelectedEvents;
  119. rc = ProcXIGetSelectedEvents(&client);
  120. assert(rc == error);
  121. reply_handler = reply_XIGetSelectedEvents;
  122. client.swapped = TRUE;
  123. swapl(&req->win);
  124. swaps(&req->length);
  125. rc = SProcXIGetSelectedEvents(&client);
  126. assert(rc == error);
  127. }
  128. static void test_XIGetSelectedEvents(void)
  129. {
  130. int i, j;
  131. xXIGetSelectedEventsReq request;
  132. ClientRec client = init_client(0, NULL);
  133. unsigned char *mask;
  134. DeviceIntRec dev;
  135. request_init(&request, XIGetSelectedEvents);
  136. printf("Testing for BadWindow on invalid window.\n");
  137. request.win = None;
  138. request_XIGetSelectedEvents(&request, BadWindow);
  139. printf("Testing for zero-length (unset) masks.\n");
  140. /* No masks set yet */
  141. test_data.num_masks_expected = 0;
  142. request.win = ROOT_WINDOW_ID;
  143. request_XIGetSelectedEvents(&request, Success);
  144. request.win = CLIENT_WINDOW_ID;
  145. request_XIGetSelectedEvents(&request, Success);
  146. memset(test_data.mask, 0,
  147. sizeof(test_data.mask));
  148. printf("Testing for valid masks\n");
  149. memset(&dev, 0, sizeof(dev)); /* dev->id is enough for XISetEventMask */
  150. request.win = ROOT_WINDOW_ID;
  151. /* devices 6 - MAXDEVICES don't exist, they mustn't be included in the
  152. * reply even if a mask is set */
  153. for (j = 0; j < MAXDEVICES; j++)
  154. {
  155. test_data.num_masks_expected = min(j + 1, devices.num_devices + 2);
  156. dev.id = j;
  157. mask = test_data.mask[j];
  158. /* bits one-by-one */
  159. for (i = 0; i < XI2LASTEVENT; i++)
  160. {
  161. SetBit(mask, i);
  162. XISetEventMask(&dev, &root, &client, (i + 8)/8, mask);
  163. request_XIGetSelectedEvents(&request, Success);
  164. ClearBit(mask, i);
  165. }
  166. /* all valid mask bits */
  167. for (i = 0; i < XI2LASTEVENT; i++)
  168. {
  169. SetBit(mask, i);
  170. XISetEventMask(&dev, &root, &client, (i + 8)/8, mask);
  171. request_XIGetSelectedEvents(&request, Success);
  172. }
  173. }
  174. printf("Testing removing all masks\n");
  175. /* Unset all masks one-by-one */
  176. for (j = MAXDEVICES - 1; j >= 0; j--)
  177. {
  178. if (j < devices.num_devices + 2)
  179. test_data.num_masks_expected--;
  180. mask = test_data.mask[j];
  181. memset(mask, 0, XI2LASTEVENT);
  182. dev.id = j;
  183. XISetEventMask(&dev, &root, &client, 0, NULL);
  184. request_XIGetSelectedEvents(&request, Success);
  185. }
  186. }
  187. int main(int argc, char** argv)
  188. {
  189. init_simple();
  190. test_XIGetSelectedEvents();
  191. return 0;
  192. }