XListDev.c revision c43cc173
1c43cc173Smrg/* $Xorg: XListDev.c,v 1.5 2001/02/09 02:03:51 xorgcvs Exp $ */ 2c43cc173Smrg 3c43cc173Smrg/************************************************************ 4c43cc173Smrg 5c43cc173SmrgCopyright 1989, 1998 The Open Group 6c43cc173Smrg 7c43cc173SmrgPermission to use, copy, modify, distribute, and sell this software and its 8c43cc173Smrgdocumentation for any purpose is hereby granted without fee, provided that 9c43cc173Smrgthe above copyright notice appear in all copies and that both that 10c43cc173Smrgcopyright notice and this permission notice appear in supporting 11c43cc173Smrgdocumentation. 12c43cc173Smrg 13c43cc173SmrgThe above copyright notice and this permission notice shall be included in 14c43cc173Smrgall copies or substantial portions of the Software. 15c43cc173Smrg 16c43cc173SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17c43cc173SmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18c43cc173SmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19c43cc173SmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 20c43cc173SmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21c43cc173SmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22c43cc173Smrg 23c43cc173SmrgExcept as contained in this notice, the name of The Open Group shall not be 24c43cc173Smrgused in advertising or otherwise to promote the sale, use or other dealings 25c43cc173Smrgin this Software without prior written authorization from The Open Group. 26c43cc173Smrg 27c43cc173SmrgCopyright 1989 by Hewlett-Packard Company, Palo Alto, California. 28c43cc173Smrg 29c43cc173Smrg All Rights Reserved 30c43cc173Smrg 31c43cc173SmrgPermission to use, copy, modify, and distribute this software and its 32c43cc173Smrgdocumentation for any purpose and without fee is hereby granted, 33c43cc173Smrgprovided that the above copyright notice appear in all copies and that 34c43cc173Smrgboth that copyright notice and this permission notice appear in 35c43cc173Smrgsupporting documentation, and that the name of Hewlett-Packard not be 36c43cc173Smrgused in advertising or publicity pertaining to distribution of the 37c43cc173Smrgsoftware without specific, written prior permission. 38c43cc173Smrg 39c43cc173SmrgHEWLETT-PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING 40c43cc173SmrgALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL 41c43cc173SmrgHEWLETT-PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR 42c43cc173SmrgANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, 43c43cc173SmrgWHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 44c43cc173SmrgARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 45c43cc173SmrgSOFTWARE. 46c43cc173Smrg 47c43cc173Smrg********************************************************/ 48c43cc173Smrg/* $XFree86: xc/lib/Xi/XListDev.c,v 3.4 2001/12/14 19:55:19 dawes Exp $ */ 49c43cc173Smrg 50c43cc173Smrg/*********************************************************************** 51c43cc173Smrg * 52c43cc173Smrg * XListInputDevices - Request the server to return a list of 53c43cc173Smrg * available input devices. 54c43cc173Smrg * 55c43cc173Smrg */ 56c43cc173Smrg 57c43cc173Smrg#define NEED_REPLIES 58c43cc173Smrg#define NEED_EVENTS 59c43cc173Smrg#include <X11/extensions/XI.h> 60c43cc173Smrg#include <X11/extensions/XIproto.h> 61c43cc173Smrg#include <X11/Xlibint.h> 62c43cc173Smrg#include <X11/extensions/XInput.h> 63c43cc173Smrg#include <X11/extensions/extutil.h> 64c43cc173Smrg#include "XIint.h" 65c43cc173Smrg 66c43cc173SmrgXDeviceInfo * 67c43cc173SmrgXListInputDevices(dpy, ndevices) 68c43cc173Smrg register Display *dpy; 69c43cc173Smrg int *ndevices; 70c43cc173Smrg{ 71c43cc173Smrg int size; 72c43cc173Smrg xListInputDevicesReq *req; 73c43cc173Smrg xListInputDevicesReply rep; 74c43cc173Smrg xDeviceInfo *list, *slist = NULL; 75c43cc173Smrg XDeviceInfo *sclist = NULL; 76c43cc173Smrg XDeviceInfo *clist = NULL; 77c43cc173Smrg xAnyClassPtr any, sav_any; 78c43cc173Smrg XAnyClassPtr Any; 79c43cc173Smrg char *nptr, *Nptr; 80c43cc173Smrg register int i, j, k; 81c43cc173Smrg register long rlen; 82c43cc173Smrg XExtDisplayInfo *info = XInput_find_display(dpy); 83c43cc173Smrg 84c43cc173Smrg LockDisplay(dpy); 85c43cc173Smrg if (_XiCheckExtInit(dpy, XInput_Initial_Release, info) == -1) 86c43cc173Smrg return ((XDeviceInfo *) NULL); 87c43cc173Smrg 88c43cc173Smrg GetReq(ListInputDevices, req); 89c43cc173Smrg req->reqType = info->codes->major_opcode; 90c43cc173Smrg req->ReqType = X_ListInputDevices; 91c43cc173Smrg 92c43cc173Smrg if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) { 93c43cc173Smrg UnlockDisplay(dpy); 94c43cc173Smrg SyncHandle(); 95c43cc173Smrg return (XDeviceInfo *) NULL; 96c43cc173Smrg } 97c43cc173Smrg 98c43cc173Smrg if ((*ndevices = rep.ndevices)) { /* at least 1 input device */ 99c43cc173Smrg size = *ndevices * sizeof(XDeviceInfo); 100c43cc173Smrg rlen = rep.length << 2; /* multiply length by 4 */ 101c43cc173Smrg list = (xDeviceInfo *) Xmalloc(rlen); 102c43cc173Smrg slist = list; 103c43cc173Smrg if (!slist) { 104c43cc173Smrg _XEatData(dpy, (unsigned long)rlen); 105c43cc173Smrg UnlockDisplay(dpy); 106c43cc173Smrg SyncHandle(); 107c43cc173Smrg return (XDeviceInfo *) NULL; 108c43cc173Smrg } 109c43cc173Smrg _XRead(dpy, (char *)list, rlen); 110c43cc173Smrg 111c43cc173Smrg any = (xAnyClassPtr) ((char *)list + (*ndevices * sizeof(xDeviceInfo))); 112c43cc173Smrg sav_any = any; 113c43cc173Smrg for (i = 0; i < *ndevices; i++, list++) { 114c43cc173Smrg for (j = 0; j < (int)list->num_classes; j++) { 115c43cc173Smrg switch (any->class) { 116c43cc173Smrg case KeyClass: 117c43cc173Smrg size += sizeof(XKeyInfo); 118c43cc173Smrg break; 119c43cc173Smrg case ButtonClass: 120c43cc173Smrg size += sizeof(XButtonInfo); 121c43cc173Smrg break; 122c43cc173Smrg case ValuatorClass: 123c43cc173Smrg { 124c43cc173Smrg xValuatorInfoPtr v; 125c43cc173Smrg 126c43cc173Smrg v = (xValuatorInfoPtr) any; 127c43cc173Smrg size += sizeof(XValuatorInfo) + 128c43cc173Smrg (v->num_axes * sizeof(XAxisInfo)); 129c43cc173Smrg break; 130c43cc173Smrg } 131c43cc173Smrg default: 132c43cc173Smrg break; 133c43cc173Smrg } 134c43cc173Smrg any = (xAnyClassPtr) ((char *)any + any->length); 135c43cc173Smrg } 136c43cc173Smrg } 137c43cc173Smrg 138c43cc173Smrg for (i = 0, nptr = (char *)any; i < *ndevices; i++) { 139c43cc173Smrg size += *nptr + 1; 140c43cc173Smrg nptr += (*nptr + 1); 141c43cc173Smrg } 142c43cc173Smrg 143c43cc173Smrg clist = (XDeviceInfoPtr) Xmalloc(size); 144c43cc173Smrg if (!clist) { 145c43cc173Smrg XFree((char *)slist); 146c43cc173Smrg UnlockDisplay(dpy); 147c43cc173Smrg SyncHandle(); 148c43cc173Smrg return (XDeviceInfo *) NULL; 149c43cc173Smrg } 150c43cc173Smrg sclist = clist; 151c43cc173Smrg Any = (XAnyClassPtr) ((char *)clist + 152c43cc173Smrg (*ndevices * sizeof(XDeviceInfo))); 153c43cc173Smrg list = slist; 154c43cc173Smrg any = sav_any; 155c43cc173Smrg for (i = 0; i < *ndevices; i++, list++, clist++) { 156c43cc173Smrg clist->type = list->type; 157c43cc173Smrg clist->id = list->id; 158c43cc173Smrg clist->use = list->use; 159c43cc173Smrg clist->num_classes = list->num_classes; 160c43cc173Smrg clist->inputclassinfo = Any; 161c43cc173Smrg for (j = 0; j < (int)list->num_classes; j++) { 162c43cc173Smrg switch (any->class) { 163c43cc173Smrg case KeyClass: 164c43cc173Smrg { 165c43cc173Smrg XKeyInfoPtr K = (XKeyInfoPtr) Any; 166c43cc173Smrg xKeyInfoPtr k = (xKeyInfoPtr) any; 167c43cc173Smrg 168c43cc173Smrg K->class = KeyClass; 169c43cc173Smrg K->length = sizeof(XKeyInfo); 170c43cc173Smrg K->min_keycode = k->min_keycode; 171c43cc173Smrg K->max_keycode = k->max_keycode; 172c43cc173Smrg K->num_keys = k->num_keys; 173c43cc173Smrg break; 174c43cc173Smrg } 175c43cc173Smrg case ButtonClass: 176c43cc173Smrg { 177c43cc173Smrg XButtonInfoPtr B = (XButtonInfoPtr) Any; 178c43cc173Smrg xButtonInfoPtr b = (xButtonInfoPtr) any; 179c43cc173Smrg 180c43cc173Smrg B->class = ButtonClass; 181c43cc173Smrg B->length = sizeof(XButtonInfo); 182c43cc173Smrg B->num_buttons = b->num_buttons; 183c43cc173Smrg break; 184c43cc173Smrg } 185c43cc173Smrg case ValuatorClass: 186c43cc173Smrg { 187c43cc173Smrg XValuatorInfoPtr V = (XValuatorInfoPtr) Any; 188c43cc173Smrg xValuatorInfoPtr v = (xValuatorInfoPtr) any; 189c43cc173Smrg XAxisInfoPtr A; 190c43cc173Smrg xAxisInfoPtr a; 191c43cc173Smrg 192c43cc173Smrg V->class = ValuatorClass; 193c43cc173Smrg V->length = sizeof(XValuatorInfo) + 194c43cc173Smrg (v->num_axes * sizeof(XAxisInfo)); 195c43cc173Smrg V->num_axes = v->num_axes; 196c43cc173Smrg V->motion_buffer = v->motion_buffer_size; 197c43cc173Smrg V->mode = v->mode; 198c43cc173Smrg A = (XAxisInfoPtr) ((char *)V + sizeof(XValuatorInfo)); 199c43cc173Smrg V->axes = A; 200c43cc173Smrg a = (xAxisInfoPtr) ((char *)any + sizeof(xValuatorInfo)); 201c43cc173Smrg for (k = 0; k < (int)v->num_axes; k++, a++, A++) { 202c43cc173Smrg A->min_value = a->min_value; 203c43cc173Smrg A->max_value = a->max_value; 204c43cc173Smrg A->resolution = a->resolution; 205c43cc173Smrg } 206c43cc173Smrg break; 207c43cc173Smrg } 208c43cc173Smrg default: 209c43cc173Smrg break; 210c43cc173Smrg } 211c43cc173Smrg any = (xAnyClassPtr) ((char *)any + any->length); 212c43cc173Smrg Any = (XAnyClassPtr) ((char *)Any + Any->length); 213c43cc173Smrg } 214c43cc173Smrg } 215c43cc173Smrg 216c43cc173Smrg clist = sclist; 217c43cc173Smrg nptr = (char *)any; 218c43cc173Smrg Nptr = (char *)Any; 219c43cc173Smrg for (i = 0; i < *ndevices; i++, clist++) { 220c43cc173Smrg clist->name = (char *)Nptr; 221c43cc173Smrg memcpy(Nptr, nptr + 1, *nptr); 222c43cc173Smrg Nptr += (*nptr); 223c43cc173Smrg *Nptr++ = '\0'; 224c43cc173Smrg nptr += (*nptr + 1); 225c43cc173Smrg } 226c43cc173Smrg } 227c43cc173Smrg 228c43cc173Smrg XFree((char *)slist); 229c43cc173Smrg UnlockDisplay(dpy); 230c43cc173Smrg SyncHandle(); 231c43cc173Smrg return (sclist); 232c43cc173Smrg} 233c43cc173Smrg 234c43cc173Smrg/*********************************************************************** 235c43cc173Smrg * 236c43cc173Smrg * Free the list of input devices. 237c43cc173Smrg * 238c43cc173Smrg */ 239c43cc173Smrg 240c43cc173Smrgvoid 241c43cc173SmrgXFreeDeviceList(list) 242c43cc173Smrg XDeviceInfo *list; 243c43cc173Smrg{ 244c43cc173Smrg if (list != NULL) { 245c43cc173Smrg XFree((char *)list); 246c43cc173Smrg } 247c43cc173Smrg} 248