XListDev.c revision f1c62215
1c43cc173Smrg/************************************************************ 2c43cc173Smrg 3c43cc173SmrgCopyright 1989, 1998 The Open Group 4c43cc173Smrg 5c43cc173SmrgPermission to use, copy, modify, distribute, and sell this software and its 6c43cc173Smrgdocumentation for any purpose is hereby granted without fee, provided that 7c43cc173Smrgthe above copyright notice appear in all copies and that both that 8c43cc173Smrgcopyright notice and this permission notice appear in supporting 9c43cc173Smrgdocumentation. 10c43cc173Smrg 11c43cc173SmrgThe above copyright notice and this permission notice shall be included in 12c43cc173Smrgall copies or substantial portions of the Software. 13c43cc173Smrg 14c43cc173SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15c43cc173SmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16c43cc173SmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17c43cc173SmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 18c43cc173SmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19c43cc173SmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20c43cc173Smrg 21c43cc173SmrgExcept as contained in this notice, the name of The Open Group shall not be 22c43cc173Smrgused in advertising or otherwise to promote the sale, use or other dealings 23c43cc173Smrgin this Software without prior written authorization from The Open Group. 24c43cc173Smrg 25c43cc173SmrgCopyright 1989 by Hewlett-Packard Company, Palo Alto, California. 26c43cc173Smrg 27c43cc173Smrg All Rights Reserved 28c43cc173Smrg 29c43cc173SmrgPermission to use, copy, modify, and distribute this software and its 30c43cc173Smrgdocumentation for any purpose and without fee is hereby granted, 31c43cc173Smrgprovided that the above copyright notice appear in all copies and that 32c43cc173Smrgboth that copyright notice and this permission notice appear in 33c43cc173Smrgsupporting documentation, and that the name of Hewlett-Packard not be 34c43cc173Smrgused in advertising or publicity pertaining to distribution of the 35c43cc173Smrgsoftware without specific, written prior permission. 36c43cc173Smrg 37c43cc173SmrgHEWLETT-PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING 38c43cc173SmrgALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL 39c43cc173SmrgHEWLETT-PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR 40c43cc173SmrgANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, 41c43cc173SmrgWHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 42c43cc173SmrgARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 43c43cc173SmrgSOFTWARE. 44c43cc173Smrg 45c43cc173Smrg********************************************************/ 46c43cc173Smrg 47c43cc173Smrg/*********************************************************************** 48c43cc173Smrg * 49c43cc173Smrg * XListInputDevices - Request the server to return a list of 50c43cc173Smrg * available input devices. 51c43cc173Smrg * 52c43cc173Smrg */ 53f1ee322dSmrg#ifdef HAVE_CONFIG_H 54f1ee322dSmrg#include <config.h> 55f1ee322dSmrg#endif 56c43cc173Smrg 57c43cc173Smrg#include <X11/extensions/XI.h> 58c43cc173Smrg#include <X11/extensions/XIproto.h> 59c43cc173Smrg#include <X11/Xlibint.h> 60c43cc173Smrg#include <X11/extensions/XInput.h> 61c43cc173Smrg#include <X11/extensions/extutil.h> 62c43cc173Smrg#include "XIint.h" 63190694daSmrg#include <limits.h> 64c43cc173Smrg 65f1ee322dSmrg/* Calculate length field to a multiples of sizeof(XID). XIDs are typedefs 66f1ee322dSmrg * to ulong and thus may be 8 bytes on some platforms. This can trigger a 67f1ee322dSmrg * SIGBUS if a class ends up not being 8-aligned (e.g. after XAxisInfo). 68f1ee322dSmrg */ 69f1ee322dSmrgstatic int pad_to_xid(int base_size) 70f1ee322dSmrg{ 71f1ee322dSmrg int padsize = sizeof(XID); 72f1ee322dSmrg 73f1ee322dSmrg return ((base_size + padsize - 1)/padsize) * padsize; 74f1ee322dSmrg} 75f1ee322dSmrg 76190694daSmrgstatic size_t 77f1c62215SmrgSizeClassInfo(xAnyClassPtr *any, size_t len, int num_classes) 78c27c18e8Smrg{ 79c27c18e8Smrg int size = 0; 80c27c18e8Smrg int j; 81c27c18e8Smrg for (j = 0; j < num_classes; j++) { 82c27c18e8Smrg switch ((*any)->class) { 83c27c18e8Smrg case KeyClass: 84f1ee322dSmrg size += pad_to_xid(sizeof(XKeyInfo)); 85c27c18e8Smrg break; 86c27c18e8Smrg case ButtonClass: 87f1ee322dSmrg size += pad_to_xid(sizeof(XButtonInfo)); 88c27c18e8Smrg break; 89c27c18e8Smrg case ValuatorClass: 90c27c18e8Smrg { 91c27c18e8Smrg xValuatorInfoPtr v; 92c27c18e8Smrg 93f1c62215Smrg if (len < sizeof(v)) 94f1c62215Smrg return 0; 95c27c18e8Smrg v = (xValuatorInfoPtr) *any; 96f1ee322dSmrg size += pad_to_xid(sizeof(XValuatorInfo) + 97f1ee322dSmrg (v->num_axes * sizeof(XAxisInfo))); 98c27c18e8Smrg break; 99c27c18e8Smrg } 100c27c18e8Smrg default: 101c27c18e8Smrg break; 102c27c18e8Smrg } 103f1c62215Smrg if ((*any)->length > len) 104f1c62215Smrg return 0; 105c27c18e8Smrg *any = (xAnyClassPtr) ((char *)(*any) + (*any)->length); 106c27c18e8Smrg } 107c27c18e8Smrg 108c27c18e8Smrg return size; 109c27c18e8Smrg} 110c27c18e8Smrg 111c27c18e8Smrgstatic void 112c27c18e8SmrgParseClassInfo(xAnyClassPtr *any, XAnyClassPtr *Any, int num_classes) 113c27c18e8Smrg{ 114f1ee322dSmrg int j; 115c27c18e8Smrg 116c27c18e8Smrg for (j = 0; j < num_classes; j++) { 117c27c18e8Smrg switch ((*any)->class) { 118c27c18e8Smrg case KeyClass: 119c27c18e8Smrg { 120c27c18e8Smrg XKeyInfoPtr K = (XKeyInfoPtr) *Any; 121c27c18e8Smrg xKeyInfoPtr k = (xKeyInfoPtr) *any; 122c27c18e8Smrg 123c27c18e8Smrg K->class = KeyClass; 124f1ee322dSmrg K->length = pad_to_xid(sizeof(XKeyInfo)); 125c27c18e8Smrg K->min_keycode = k->min_keycode; 126c27c18e8Smrg K->max_keycode = k->max_keycode; 127c27c18e8Smrg K->num_keys = k->num_keys; 128c27c18e8Smrg break; 129c27c18e8Smrg } 130c27c18e8Smrg case ButtonClass: 131c27c18e8Smrg { 132c27c18e8Smrg XButtonInfoPtr B = (XButtonInfoPtr) *Any; 133c27c18e8Smrg xButtonInfoPtr b = (xButtonInfoPtr) *any; 134c27c18e8Smrg 135c27c18e8Smrg B->class = ButtonClass; 136f1ee322dSmrg B->length = pad_to_xid(sizeof(XButtonInfo)); 137c27c18e8Smrg B->num_buttons = b->num_buttons; 138c27c18e8Smrg break; 139c27c18e8Smrg } 140c27c18e8Smrg case ValuatorClass: 141c27c18e8Smrg { 142f1ee322dSmrg int k; 143c27c18e8Smrg XValuatorInfoPtr V = (XValuatorInfoPtr) *Any; 144c27c18e8Smrg xValuatorInfoPtr v = (xValuatorInfoPtr) *any; 145c27c18e8Smrg XAxisInfoPtr A; 146c27c18e8Smrg xAxisInfoPtr a; 147c27c18e8Smrg 148c27c18e8Smrg V->class = ValuatorClass; 149f1ee322dSmrg V->length = pad_to_xid(sizeof(XValuatorInfo) + 150f1ee322dSmrg (v->num_axes * sizeof(XAxisInfo))); 151c27c18e8Smrg V->num_axes = v->num_axes; 152c27c18e8Smrg V->motion_buffer = v->motion_buffer_size; 153c27c18e8Smrg V->mode = v->mode; 154c27c18e8Smrg A = (XAxisInfoPtr) ((char *)V + sizeof(XValuatorInfo)); 155c27c18e8Smrg V->axes = A; 156c27c18e8Smrg a = (xAxisInfoPtr) ((char *)(*any) + sizeof(xValuatorInfo)); 157c27c18e8Smrg for (k = 0; k < (int)v->num_axes; k++, a++, A++) { 158c27c18e8Smrg A->min_value = a->min_value; 159c27c18e8Smrg A->max_value = a->max_value; 160c27c18e8Smrg A->resolution = a->resolution; 161c27c18e8Smrg } 162c27c18e8Smrg break; 163c27c18e8Smrg } 164c27c18e8Smrg default: 165c27c18e8Smrg break; 166c27c18e8Smrg } 167c27c18e8Smrg *any = (xAnyClassPtr) ((char *)(*any) + (*any)->length); 168c27c18e8Smrg *Any = (XAnyClassPtr) ((char *)(*Any) + (*Any)->length); 169c27c18e8Smrg } 170c27c18e8Smrg} 171c27c18e8Smrg 172c43cc173SmrgXDeviceInfo * 173c27c18e8SmrgXListInputDevices( 174c27c18e8Smrg register Display *dpy, 175c27c18e8Smrg int *ndevices) 176c43cc173Smrg{ 177f1c62215Smrg size_t s, size; 178c43cc173Smrg xListInputDevicesReq *req; 179c43cc173Smrg xListInputDevicesReply rep; 180c43cc173Smrg xDeviceInfo *list, *slist = NULL; 181c43cc173Smrg XDeviceInfo *sclist = NULL; 182c43cc173Smrg XDeviceInfo *clist = NULL; 183c43cc173Smrg xAnyClassPtr any, sav_any; 184c43cc173Smrg XAnyClassPtr Any; 185f1c62215Smrg char *end = NULL; 186190694daSmrg unsigned char *nptr, *Nptr; 187c27c18e8Smrg int i; 188190694daSmrg unsigned long rlen; 189c43cc173Smrg XExtDisplayInfo *info = XInput_find_display(dpy); 190c43cc173Smrg 191c43cc173Smrg LockDisplay(dpy); 192c43cc173Smrg if (_XiCheckExtInit(dpy, XInput_Initial_Release, info) == -1) 193c43cc173Smrg return ((XDeviceInfo *) NULL); 194c43cc173Smrg 195c43cc173Smrg GetReq(ListInputDevices, req); 196c43cc173Smrg req->reqType = info->codes->major_opcode; 197c43cc173Smrg req->ReqType = X_ListInputDevices; 198c43cc173Smrg 199c43cc173Smrg if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) { 200c43cc173Smrg UnlockDisplay(dpy); 201c43cc173Smrg SyncHandle(); 202c43cc173Smrg return (XDeviceInfo *) NULL; 203c43cc173Smrg } 204c43cc173Smrg 205c43cc173Smrg if ((*ndevices = rep.ndevices)) { /* at least 1 input device */ 206c43cc173Smrg size = *ndevices * sizeof(XDeviceInfo); 207190694daSmrg if (rep.length < (INT_MAX >> 2)) { 208190694daSmrg rlen = rep.length << 2; /* multiply length by 4 */ 209190694daSmrg slist = list = Xmalloc(rlen); 210190694daSmrg } 211c43cc173Smrg if (!slist) { 21210baa824Smrg _XEatDataWords(dpy, rep.length); 213c43cc173Smrg UnlockDisplay(dpy); 214c43cc173Smrg SyncHandle(); 215c43cc173Smrg return (XDeviceInfo *) NULL; 216c43cc173Smrg } 217c43cc173Smrg _XRead(dpy, (char *)list, rlen); 218c43cc173Smrg 219c43cc173Smrg any = (xAnyClassPtr) ((char *)list + (*ndevices * sizeof(xDeviceInfo))); 220c43cc173Smrg sav_any = any; 221f1c62215Smrg end = (char *)list + rlen; 222c43cc173Smrg for (i = 0; i < *ndevices; i++, list++) { 223f1c62215Smrg s = SizeClassInfo(&any, end - (char *)any, (int)list->num_classes); 224f1c62215Smrg if (!s) 225f1c62215Smrg goto out; 226f1c62215Smrg size += s; 227c43cc173Smrg } 228c43cc173Smrg 229f1c62215Smrg Nptr = ((unsigned char *)list) + rlen; 230190694daSmrg for (i = 0, nptr = (unsigned char *)any; i < *ndevices; i++) { 231f1c62215Smrg if (nptr >= Nptr) 232f1c62215Smrg goto out; 233c43cc173Smrg size += *nptr + 1; 234c43cc173Smrg nptr += (*nptr + 1); 235c43cc173Smrg } 236c43cc173Smrg 237c43cc173Smrg clist = (XDeviceInfoPtr) Xmalloc(size); 238c43cc173Smrg if (!clist) { 239c43cc173Smrg XFree((char *)slist); 240c43cc173Smrg UnlockDisplay(dpy); 241c43cc173Smrg SyncHandle(); 242c43cc173Smrg return (XDeviceInfo *) NULL; 243c43cc173Smrg } 244c43cc173Smrg sclist = clist; 245c43cc173Smrg Any = (XAnyClassPtr) ((char *)clist + 246c43cc173Smrg (*ndevices * sizeof(XDeviceInfo))); 247c43cc173Smrg list = slist; 248c43cc173Smrg any = sav_any; 249c43cc173Smrg for (i = 0; i < *ndevices; i++, list++, clist++) { 250c43cc173Smrg clist->type = list->type; 251c43cc173Smrg clist->id = list->id; 252c43cc173Smrg clist->use = list->use; 253c43cc173Smrg clist->num_classes = list->num_classes; 254c43cc173Smrg clist->inputclassinfo = Any; 255c27c18e8Smrg 256c27c18e8Smrg ParseClassInfo(&any, &Any, (int)list->num_classes); 257c43cc173Smrg } 258c43cc173Smrg 259c43cc173Smrg clist = sclist; 260190694daSmrg nptr = (unsigned char *)any; 261190694daSmrg Nptr = (unsigned char *)Any; 262c43cc173Smrg for (i = 0; i < *ndevices; i++, clist++) { 263c43cc173Smrg clist->name = (char *)Nptr; 264c43cc173Smrg memcpy(Nptr, nptr + 1, *nptr); 265c43cc173Smrg Nptr += (*nptr); 266c43cc173Smrg *Nptr++ = '\0'; 267c43cc173Smrg nptr += (*nptr + 1); 268c43cc173Smrg } 269c43cc173Smrg } 270c43cc173Smrg 271190694daSmrg out: 272c43cc173Smrg XFree((char *)slist); 273c43cc173Smrg UnlockDisplay(dpy); 274c43cc173Smrg SyncHandle(); 275c43cc173Smrg return (sclist); 276c43cc173Smrg} 277c43cc173Smrg 278c43cc173Smrg/*********************************************************************** 279c43cc173Smrg * 280c43cc173Smrg * Free the list of input devices. 281c43cc173Smrg * 282c43cc173Smrg */ 283c43cc173Smrg 284c43cc173Smrgvoid 285c27c18e8SmrgXFreeDeviceList(XDeviceInfo *list) 286c43cc173Smrg{ 287c43cc173Smrg if (list != NULL) { 288c43cc173Smrg XFree((char *)list); 289c43cc173Smrg } 290c43cc173Smrg} 291