XIQueryDevice.c revision d678aadf
1c27c18e8Smrg/* 2c27c18e8Smrg * Copyright © 2009 Red Hat, Inc. 3c27c18e8Smrg * 4c27c18e8Smrg * Permission is hereby granted, free of charge, to any person obtaining a 5c27c18e8Smrg * copy of this software and associated documentation files (the "Software"), 6c27c18e8Smrg * to deal in the Software without restriction, including without limitation 7c27c18e8Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8c27c18e8Smrg * and/or sell copies of the Software, and to permit persons to whom the 9c27c18e8Smrg * Software is furnished to do so, subject to the following conditions: 10c27c18e8Smrg * 11c27c18e8Smrg * The above copyright notice and this permission notice (including the next 12c27c18e8Smrg * paragraph) shall be included in all copies or substantial portions of the 13c27c18e8Smrg * Software. 14c27c18e8Smrg * 15c27c18e8Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16c27c18e8Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17c27c18e8Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18c27c18e8Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19c27c18e8Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20c27c18e8Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 21c27c18e8Smrg * DEALINGS IN THE SOFTWARE. 22c27c18e8Smrg * 23c27c18e8Smrg */ 24c27c18e8Smrg 25f1ee322dSmrg#if HAVE_CONFIG_H 26f1ee322dSmrg#include <config.h> 27f1ee322dSmrg#endif 28f1ee322dSmrg 290eb1301bSmrg#include <limits.h> 30c27c18e8Smrg#include <stdint.h> 31c27c18e8Smrg#include <X11/Xlibint.h> 32c27c18e8Smrg#include <X11/extensions/XI2proto.h> 33c27c18e8Smrg#include <X11/extensions/XInput2.h> 34c27c18e8Smrg#include <X11/extensions/extutil.h> 35c27c18e8Smrg#include "XIint.h" 36c27c18e8Smrg 37f1ee322dSmrgextern int copy_classes(XIDeviceInfo* to, xXIAnyInfo* from, int *nclasses); 38c27c18e8Smrgextern int size_classes(xXIAnyInfo* from, int nclasses); 39c27c18e8Smrg 40c27c18e8SmrgXIDeviceInfo* 41c27c18e8SmrgXIQueryDevice(Display *dpy, int deviceid, int *ndevices_return) 42c27c18e8Smrg{ 43c27c18e8Smrg XIDeviceInfo *info = NULL; 44c27c18e8Smrg xXIQueryDeviceReq *req; 45c27c18e8Smrg xXIQueryDeviceReply reply; 46c27c18e8Smrg char *ptr; 470eb1301bSmrg char *end; 48c27c18e8Smrg int i; 49d678aadfSmrg char *buf = NULL; 50c27c18e8Smrg 51c27c18e8Smrg XExtDisplayInfo *extinfo = XInput_find_display(dpy); 52c27c18e8Smrg 53c27c18e8Smrg LockDisplay(dpy); 54b789ec8aSmrg if (_XiCheckExtInit(dpy, XInput_2_0, extinfo) == -1) 5544584a44Smrg goto error_unlocked; 56c27c18e8Smrg 57c27c18e8Smrg GetReq(XIQueryDevice, req); 58c27c18e8Smrg req->reqType = extinfo->codes->major_opcode; 59c27c18e8Smrg req->ReqType = X_XIQueryDevice; 60c27c18e8Smrg req->deviceid = deviceid; 61c27c18e8Smrg 62c27c18e8Smrg if (!_XReply(dpy, (xReply*) &reply, 0, xFalse)) 63c27c18e8Smrg goto error; 64c27c18e8Smrg 650eb1301bSmrg if (reply.length < INT_MAX / 4) 660eb1301bSmrg { 670eb1301bSmrg *ndevices_return = reply.num_devices; 680eb1301bSmrg info = Xmalloc((reply.num_devices + 1) * sizeof(XIDeviceInfo)); 69d678aadfSmrg buf = Xmalloc(reply.length * 4); 700eb1301bSmrg } 710eb1301bSmrg else 720eb1301bSmrg { 730eb1301bSmrg *ndevices_return = 0; 740eb1301bSmrg info = NULL; 75d678aadfSmrg buf = NULL; 760eb1301bSmrg } 770eb1301bSmrg 78d678aadfSmrg if (!info || !buf) 79c27c18e8Smrg goto error; 80c27c18e8Smrg 81c27c18e8Smrg _XRead(dpy, buf, reply.length * 4); 82c27c18e8Smrg ptr = buf; 830eb1301bSmrg end = buf + reply.length * 4; 84c27c18e8Smrg 85c27c18e8Smrg /* info is a null-terminated array */ 86c27c18e8Smrg info[reply.num_devices].name = NULL; 87c27c18e8Smrg 88c27c18e8Smrg for (i = 0; i < reply.num_devices; i++) 89c27c18e8Smrg { 90f1ee322dSmrg int nclasses; 91f1ee322dSmrg size_t sz; 92c27c18e8Smrg XIDeviceInfo *lib = &info[i]; 93c27c18e8Smrg xXIDeviceInfo *wire = (xXIDeviceInfo*)ptr; 94c27c18e8Smrg 950eb1301bSmrg if (ptr + sizeof(xXIDeviceInfo) > end) 960eb1301bSmrg goto error_loop; 970eb1301bSmrg 98c27c18e8Smrg lib->deviceid = wire->deviceid; 99c27c18e8Smrg lib->use = wire->use; 100c27c18e8Smrg lib->attachment = wire->attachment; 101c27c18e8Smrg lib->enabled = wire->enabled; 102f1ee322dSmrg nclasses = wire->num_classes; 103c27c18e8Smrg 104c27c18e8Smrg ptr += sizeof(xXIDeviceInfo); 105c27c18e8Smrg 1060eb1301bSmrg if (ptr + wire->name_len > end) 1070eb1301bSmrg goto error_loop; 1080eb1301bSmrg 109c27c18e8Smrg lib->name = Xcalloc(wire->name_len + 1, 1); 1100eb1301bSmrg if (lib->name == NULL) 1110eb1301bSmrg goto error_loop; 112c27c18e8Smrg strncpy(lib->name, ptr, wire->name_len); 1130eb1301bSmrg lib->name[wire->name_len] = '\0'; 114c27c18e8Smrg ptr += ((wire->name_len + 3)/4) * 4; 115c27c18e8Smrg 116f1ee322dSmrg sz = size_classes((xXIAnyInfo*)ptr, nclasses); 117f1ee322dSmrg lib->classes = Xmalloc(sz); 1180eb1301bSmrg if (lib->classes == NULL) 1190eb1301bSmrg { 1200eb1301bSmrg Xfree(lib->name); 1210eb1301bSmrg goto error_loop; 1220eb1301bSmrg } 123f1ee322dSmrg ptr += copy_classes(lib, (xXIAnyInfo*)ptr, &nclasses); 124f1ee322dSmrg /* We skip over unused classes */ 125f1ee322dSmrg lib->num_classes = nclasses; 126c27c18e8Smrg } 127c27c18e8Smrg 128c27c18e8Smrg Xfree(buf); 129c27c18e8Smrg UnlockDisplay(dpy); 130c27c18e8Smrg SyncHandle(); 131c27c18e8Smrg return info; 132c27c18e8Smrg 1330eb1301bSmrgerror_loop: 1340eb1301bSmrg while (--i >= 0) 1350eb1301bSmrg { 1360eb1301bSmrg Xfree(info[i].name); 1370eb1301bSmrg Xfree(info[i].classes); 1380eb1301bSmrg } 139c27c18e8Smrgerror: 140d678aadfSmrg Xfree(info); 141d678aadfSmrg Xfree(buf); 142c27c18e8Smrg UnlockDisplay(dpy); 14344584a44Smrgerror_unlocked: 144c27c18e8Smrg SyncHandle(); 145c27c18e8Smrg *ndevices_return = -1; 146c27c18e8Smrg return NULL; 147c27c18e8Smrg} 148c27c18e8Smrg 149c27c18e8Smrgvoid 150c27c18e8SmrgXIFreeDeviceInfo(XIDeviceInfo* info) 151c27c18e8Smrg{ 152c27c18e8Smrg XIDeviceInfo *ptr = info; 153c27c18e8Smrg while(ptr->name) 154c27c18e8Smrg { 155c27c18e8Smrg Xfree(ptr->classes); 156c27c18e8Smrg Xfree(ptr->name); 157c27c18e8Smrg ptr++; 158c27c18e8Smrg } 159c27c18e8Smrg Xfree(info); 160c27c18e8Smrg} 161