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