queryst.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. Copyright 1998, 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
  9. in all copies or substantial portions of the Software.
  10. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  11. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  12. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  13. IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
  14. OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  15. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  16. OTHER DEALINGS IN THE SOFTWARE.
  17. Except as contained in this notice, the name of The Open Group shall
  18. not be used in advertising or otherwise to promote the sale, use or
  19. other dealings in this Software without prior written authorization
  20. from The Open Group.
  21. */
  22. /***********************************************************************
  23. *
  24. * Request to query the state of an extension input device.
  25. *
  26. */
  27. #ifdef HAVE_DIX_CONFIG_H
  28. #include <dix-config.h>
  29. #endif
  30. #include "inputstr.h" /* DeviceIntPtr */
  31. #include "windowstr.h" /* window structure */
  32. #include <X11/extensions/XI.h>
  33. #include <X11/extensions/XIproto.h>
  34. #include "exevents.h"
  35. #include "exglobals.h"
  36. #include "xkbsrv.h"
  37. #include "xkbstr.h"
  38. #include "queryst.h"
  39. /***********************************************************************
  40. *
  41. * This procedure allows a client to query the state of a device.
  42. *
  43. */
  44. int
  45. SProcXQueryDeviceState(ClientPtr client)
  46. {
  47. REQUEST(xQueryDeviceStateReq);
  48. swaps(&stuff->length);
  49. return (ProcXQueryDeviceState(client));
  50. }
  51. /***********************************************************************
  52. *
  53. * This procedure allows frozen events to be routed.
  54. *
  55. */
  56. int
  57. ProcXQueryDeviceState(ClientPtr client)
  58. {
  59. int rc, i;
  60. int num_classes = 0;
  61. int total_length = 0;
  62. char *buf, *savbuf;
  63. KeyClassPtr k;
  64. xKeyState *tk;
  65. ButtonClassPtr b;
  66. xButtonState *tb;
  67. ValuatorClassPtr v;
  68. xValuatorState *tv;
  69. xQueryDeviceStateReply rep;
  70. DeviceIntPtr dev;
  71. double *values;
  72. REQUEST(xQueryDeviceStateReq);
  73. REQUEST_SIZE_MATCH(xQueryDeviceStateReq);
  74. rc = dixLookupDevice(&dev, stuff->deviceid, client, DixReadAccess);
  75. if (rc != Success && rc != BadAccess)
  76. return rc;
  77. v = dev->valuator;
  78. if (v != NULL && v->motionHintWindow != NULL)
  79. MaybeStopDeviceHint(dev, client);
  80. k = dev->key;
  81. if (k != NULL) {
  82. total_length += sizeof(xKeyState);
  83. num_classes++;
  84. }
  85. b = dev->button;
  86. if (b != NULL) {
  87. total_length += sizeof(xButtonState);
  88. num_classes++;
  89. }
  90. if (v != NULL) {
  91. total_length += (sizeof(xValuatorState) + (v->numAxes * sizeof(int)));
  92. num_classes++;
  93. }
  94. buf = (char *) calloc(total_length, 1);
  95. if (!buf)
  96. return BadAlloc;
  97. savbuf = buf;
  98. if (k != NULL) {
  99. tk = (xKeyState *) buf;
  100. tk->class = KeyClass;
  101. tk->length = sizeof(xKeyState);
  102. tk->num_keys = k->xkbInfo->desc->max_key_code -
  103. k->xkbInfo->desc->min_key_code + 1;
  104. if (rc != BadAccess)
  105. for (i = 0; i < 32; i++)
  106. tk->keys[i] = k->down[i];
  107. buf += sizeof(xKeyState);
  108. }
  109. if (b != NULL) {
  110. tb = (xButtonState *) buf;
  111. tb->class = ButtonClass;
  112. tb->length = sizeof(xButtonState);
  113. tb->num_buttons = b->numButtons;
  114. if (rc != BadAccess)
  115. memcpy(tb->buttons, b->down, sizeof(b->down));
  116. buf += sizeof(xButtonState);
  117. }
  118. if (v != NULL) {
  119. tv = (xValuatorState *) buf;
  120. tv->class = ValuatorClass;
  121. tv->length = sizeof(xValuatorState) + v->numAxes * 4;
  122. tv->num_valuators = v->numAxes;
  123. tv->mode = valuator_get_mode(dev, 0);
  124. tv->mode |= (dev->proximity &&
  125. !dev->proximity->in_proximity) ? OutOfProximity : 0;
  126. buf += sizeof(xValuatorState);
  127. for (i = 0, values = v->axisVal; i < v->numAxes; i++) {
  128. if (rc != BadAccess)
  129. *((int *) buf) = *values;
  130. values++;
  131. if (client->swapped) {
  132. swapl((int *) buf);
  133. }
  134. buf += sizeof(int);
  135. }
  136. }
  137. rep = (xQueryDeviceStateReply) {
  138. .repType = X_Reply,
  139. .RepType = X_QueryDeviceState,
  140. .sequenceNumber = client->sequence,
  141. .length = bytes_to_int32(total_length),
  142. .num_classes = num_classes
  143. };
  144. WriteReplyToClient(client, sizeof(xQueryDeviceStateReply), &rep);
  145. if (total_length > 0)
  146. WriteToClient(client, total_length, savbuf);
  147. free(savbuf);
  148. return Success;
  149. }
  150. /***********************************************************************
  151. *
  152. * This procedure writes the reply for the XQueryDeviceState function,
  153. * if the client and server have a different byte ordering.
  154. *
  155. */
  156. void
  157. SRepXQueryDeviceState(ClientPtr client, int size, xQueryDeviceStateReply * rep)
  158. {
  159. swaps(&rep->sequenceNumber);
  160. swapl(&rep->length);
  161. WriteToClient(client, size, rep);
  162. }