listdev.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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 list the available input devices.
  39. *
  40. */
  41. #ifdef HAVE_DIX_CONFIG_H
  42. #include <dix-config.h>
  43. #endif
  44. #include <X11/X.h> /* for inputstr.h */
  45. #include <X11/Xproto.h> /* Request macro */
  46. #include "inputstr.h" /* DeviceIntPtr */
  47. #include <X11/extensions/XI.h>
  48. #include <X11/extensions/XIproto.h>
  49. #include "XIstubs.h"
  50. #include "extnsionst.h"
  51. #include "exevents.h"
  52. #include "xace.h"
  53. #include "xkbsrv.h"
  54. #include "xkbstr.h"
  55. #include "listdev.h"
  56. /***********************************************************************
  57. *
  58. * This procedure lists the input devices available to the server.
  59. *
  60. */
  61. int
  62. SProcXListInputDevices(ClientPtr client)
  63. {
  64. REQUEST(xListInputDevicesReq);
  65. swaps(&stuff->length);
  66. return (ProcXListInputDevices(client));
  67. }
  68. /***********************************************************************
  69. *
  70. * This procedure calculates the size of the information to be returned
  71. * for an input device.
  72. *
  73. */
  74. static void
  75. SizeDeviceInfo(DeviceIntPtr d, int *namesize, int *size)
  76. {
  77. int chunks;
  78. *namesize += 1;
  79. if (d->name)
  80. *namesize += strlen(d->name);
  81. if (d->key != NULL)
  82. *size += sizeof(xKeyInfo);
  83. if (d->button != NULL)
  84. *size += sizeof(xButtonInfo);
  85. if (d->valuator != NULL) {
  86. chunks = ((int) d->valuator->numAxes + 19) / VPC;
  87. *size += (chunks * sizeof(xValuatorInfo) +
  88. d->valuator->numAxes * sizeof(xAxisInfo));
  89. }
  90. }
  91. /***********************************************************************
  92. *
  93. * This procedure copies data to the DeviceInfo struct, swapping if necessary.
  94. *
  95. * We need the extra byte in the allocated buffer, because the trailing null
  96. * hammers one extra byte, which is overwritten by the next name except for
  97. * the last name copied.
  98. *
  99. */
  100. static void
  101. CopyDeviceName(char **namebuf, const char *name)
  102. {
  103. char *nameptr = *namebuf;
  104. if (name) {
  105. *nameptr++ = strlen(name);
  106. strcpy(nameptr, name);
  107. *namebuf += (strlen(name) + 1);
  108. }
  109. else {
  110. *nameptr++ = 0;
  111. *namebuf += 1;
  112. }
  113. }
  114. /***********************************************************************
  115. *
  116. * This procedure copies ButtonClass information, swapping if necessary.
  117. *
  118. */
  119. static void
  120. CopySwapButtonClass(ClientPtr client, ButtonClassPtr b, char **buf)
  121. {
  122. xButtonInfoPtr b2;
  123. b2 = (xButtonInfoPtr) * buf;
  124. b2->class = ButtonClass;
  125. b2->length = sizeof(xButtonInfo);
  126. b2->num_buttons = b->numButtons;
  127. if (client && client->swapped) {
  128. swaps(&b2->num_buttons);
  129. }
  130. *buf += sizeof(xButtonInfo);
  131. }
  132. /***********************************************************************
  133. *
  134. * This procedure copies data to the DeviceInfo struct, swapping if necessary.
  135. *
  136. */
  137. static void
  138. CopySwapDevice(ClientPtr client, DeviceIntPtr d, int num_classes, char **buf)
  139. {
  140. xDeviceInfoPtr dev;
  141. dev = (xDeviceInfoPtr) * buf;
  142. dev->id = d->id;
  143. dev->type = d->xinput_type;
  144. dev->num_classes = num_classes;
  145. if (IsMaster(d) && IsKeyboardDevice(d))
  146. dev->use = IsXKeyboard;
  147. else if (IsMaster(d) && IsPointerDevice(d))
  148. dev->use = IsXPointer;
  149. else if (d->valuator && d->button)
  150. dev->use = IsXExtensionPointer;
  151. else if (d->key && d->kbdfeed)
  152. dev->use = IsXExtensionKeyboard;
  153. else
  154. dev->use = IsXExtensionDevice;
  155. if (client->swapped) {
  156. swapl(&dev->type);
  157. }
  158. *buf += sizeof(xDeviceInfo);
  159. }
  160. /***********************************************************************
  161. *
  162. * This procedure copies KeyClass information, swapping if necessary.
  163. *
  164. */
  165. static void
  166. CopySwapKeyClass(ClientPtr client, KeyClassPtr k, char **buf)
  167. {
  168. xKeyInfoPtr k2;
  169. k2 = (xKeyInfoPtr) * buf;
  170. k2->class = KeyClass;
  171. k2->length = sizeof(xKeyInfo);
  172. k2->min_keycode = k->xkbInfo->desc->min_key_code;
  173. k2->max_keycode = k->xkbInfo->desc->max_key_code;
  174. k2->num_keys = k2->max_keycode - k2->min_keycode + 1;
  175. if (client && client->swapped) {
  176. swaps(&k2->num_keys);
  177. }
  178. *buf += sizeof(xKeyInfo);
  179. }
  180. /***********************************************************************
  181. *
  182. * This procedure copies ValuatorClass information, swapping if necessary.
  183. *
  184. * Devices may have up to 255 valuators. The length of a ValuatorClass is
  185. * defined to be sizeof(ValuatorClassInfo) + num_axes * sizeof (xAxisInfo).
  186. * The maximum length is therefore (8 + 255 * 12) = 3068. However, the
  187. * length field is one byte. If a device has more than 20 valuators, we
  188. * must therefore return multiple valuator classes to the client.
  189. *
  190. */
  191. static int
  192. CopySwapValuatorClass(ClientPtr client, DeviceIntPtr dev, char **buf)
  193. {
  194. int i, j, axes, t_axes;
  195. ValuatorClassPtr v = dev->valuator;
  196. xValuatorInfoPtr v2;
  197. AxisInfo *a;
  198. xAxisInfoPtr a2;
  199. for (i = 0, axes = v->numAxes; i < ((v->numAxes + 19) / VPC);
  200. i++, axes -= VPC) {
  201. t_axes = axes < VPC ? axes : VPC;
  202. if (t_axes < 0)
  203. t_axes = v->numAxes % VPC;
  204. v2 = (xValuatorInfoPtr) * buf;
  205. v2->class = ValuatorClass;
  206. v2->length = sizeof(xValuatorInfo) + t_axes * sizeof(xAxisInfo);
  207. v2->num_axes = t_axes;
  208. v2->mode = valuator_get_mode(dev, 0);
  209. v2->motion_buffer_size = v->numMotionEvents;
  210. if (client && client->swapped) {
  211. swapl(&v2->motion_buffer_size);
  212. }
  213. *buf += sizeof(xValuatorInfo);
  214. a = v->axes + (VPC * i);
  215. a2 = (xAxisInfoPtr) * buf;
  216. for (j = 0; j < t_axes; j++) {
  217. a2->min_value = a->min_value;
  218. a2->max_value = a->max_value;
  219. a2->resolution = a->resolution;
  220. if (client && client->swapped) {
  221. swapl(&a2->min_value);
  222. swapl(&a2->max_value);
  223. swapl(&a2->resolution);
  224. }
  225. a2++;
  226. a++;
  227. *buf += sizeof(xAxisInfo);
  228. }
  229. }
  230. return i;
  231. }
  232. static void
  233. CopySwapClasses(ClientPtr client, DeviceIntPtr dev, CARD8 *num_classes,
  234. char **classbuf)
  235. {
  236. if (dev->key != NULL) {
  237. CopySwapKeyClass(client, dev->key, classbuf);
  238. (*num_classes)++;
  239. }
  240. if (dev->button != NULL) {
  241. CopySwapButtonClass(client, dev->button, classbuf);
  242. (*num_classes)++;
  243. }
  244. if (dev->valuator != NULL) {
  245. (*num_classes) += CopySwapValuatorClass(client, dev, classbuf);
  246. }
  247. }
  248. /***********************************************************************
  249. *
  250. * This procedure lists information to be returned for an input device.
  251. *
  252. */
  253. static void
  254. ListDeviceInfo(ClientPtr client, DeviceIntPtr d, xDeviceInfoPtr dev,
  255. char **devbuf, char **classbuf, char **namebuf)
  256. {
  257. CopyDeviceName(namebuf, d->name);
  258. CopySwapDevice(client, d, 0, devbuf);
  259. CopySwapClasses(client, d, &dev->num_classes, classbuf);
  260. }
  261. /***********************************************************************
  262. *
  263. * This procedure checks if a device should be left off the list.
  264. *
  265. */
  266. static Bool
  267. ShouldSkipDevice(ClientPtr client, DeviceIntPtr d)
  268. {
  269. /* don't send master devices other than VCP/VCK */
  270. if (!IsMaster(d) || d == inputInfo.pointer ||d == inputInfo.keyboard) {
  271. int rc = XaceHook(XACE_DEVICE_ACCESS, client, d, DixGetAttrAccess);
  272. if (rc == Success)
  273. return FALSE;
  274. }
  275. return TRUE;
  276. }
  277. /***********************************************************************
  278. *
  279. * This procedure lists the input devices available to the server.
  280. *
  281. * If this request is called by a client that has not issued a
  282. * GetExtensionVersion request with major/minor version set, we don't send the
  283. * complete device list. Instead, we only send the VCP, the VCK and floating
  284. * SDs. This resembles the setup found on XI 1.x machines.
  285. */
  286. int
  287. ProcXListInputDevices(ClientPtr client)
  288. {
  289. xListInputDevicesReply rep;
  290. int numdevs = 0;
  291. int namesize = 1; /* need 1 extra byte for strcpy */
  292. int i = 0, size = 0;
  293. int total_length;
  294. char *devbuf, *classbuf, *namebuf, *savbuf;
  295. Bool *skip;
  296. xDeviceInfo *dev;
  297. DeviceIntPtr d;
  298. REQUEST_SIZE_MATCH(xListInputDevicesReq);
  299. rep = (xListInputDevicesReply) {
  300. .repType = X_Reply,
  301. .RepType = X_ListInputDevices,
  302. .sequenceNumber = client->sequence,
  303. .length = 0
  304. };
  305. /* allocate space for saving skip value */
  306. skip = calloc(sizeof(Bool), inputInfo.numDevices);
  307. if (!skip)
  308. return BadAlloc;
  309. /* figure out which devices to skip */
  310. numdevs = 0;
  311. for (d = inputInfo.devices; d; d = d->next, i++) {
  312. skip[i] = ShouldSkipDevice(client, d);
  313. if (skip[i])
  314. continue;
  315. SizeDeviceInfo(d, &namesize, &size);
  316. numdevs++;
  317. }
  318. for (d = inputInfo.off_devices; d; d = d->next, i++) {
  319. skip[i] = ShouldSkipDevice(client, d);
  320. if (skip[i])
  321. continue;
  322. SizeDeviceInfo(d, &namesize, &size);
  323. numdevs++;
  324. }
  325. /* allocate space for reply */
  326. total_length = numdevs * sizeof(xDeviceInfo) + size + namesize;
  327. devbuf = (char *) calloc(1, total_length);
  328. classbuf = devbuf + (numdevs * sizeof(xDeviceInfo));
  329. namebuf = classbuf + size;
  330. savbuf = devbuf;
  331. /* fill in and send reply */
  332. i = 0;
  333. dev = (xDeviceInfoPtr) devbuf;
  334. for (d = inputInfo.devices; d; d = d->next, i++) {
  335. if (skip[i])
  336. continue;
  337. ListDeviceInfo(client, d, dev++, &devbuf, &classbuf, &namebuf);
  338. }
  339. for (d = inputInfo.off_devices; d; d = d->next, i++) {
  340. if (skip[i])
  341. continue;
  342. ListDeviceInfo(client, d, dev++, &devbuf, &classbuf, &namebuf);
  343. }
  344. rep.ndevices = numdevs;
  345. rep.length = bytes_to_int32(total_length);
  346. WriteReplyToClient(client, sizeof(xListInputDevicesReply), &rep);
  347. WriteToClient(client, total_length, savbuf);
  348. free(savbuf);
  349. free(skip);
  350. return Success;
  351. }
  352. /***********************************************************************
  353. *
  354. * This procedure writes the reply for the XListInputDevices function,
  355. * if the client and server have a different byte ordering.
  356. *
  357. */
  358. void
  359. SRepXListInputDevices(ClientPtr client, int size, xListInputDevicesReply * rep)
  360. {
  361. swaps(&rep->sequenceNumber);
  362. swapl(&rep->length);
  363. WriteToClient(client, size, rep);
  364. }