1 /************************************************************
3 Copyright 1989, 1998 The Open Group
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
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
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.
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.
25 Copyright 1989 by Hewlett-Packard Company, Palo Alto, California.
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.
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
45 ********************************************************/
47 /***********************************************************************
49 * XListInputDevices - Request the server to return a list of
50 * available input devices.
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>
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).
69 static int pad_to_xid(int base_size)
71 int padsize = sizeof(XID);
73 return ((base_size + padsize - 1)/padsize) * padsize;
77 SizeClassInfo(xAnyClassPtr *any, int num_classes)
81 for (j = 0; j < num_classes; j++) {
82 switch ((*any)->class) {
84 size += pad_to_xid(sizeof(XKeyInfo));
87 size += pad_to_xid(sizeof(XButtonInfo));
93 v = (xValuatorInfoPtr) *any;
94 size += pad_to_xid(sizeof(XValuatorInfo) +
95 (v->num_axes * sizeof(XAxisInfo)));
101 *any = (xAnyClassPtr) ((char *)(*any) + (*any)->length);
108 ParseClassInfo(xAnyClassPtr *any, XAnyClassPtr *Any, int num_classes)
112 for (j = 0; j < num_classes; j++) {
113 switch ((*any)->class) {
116 XKeyInfoPtr K = (XKeyInfoPtr) *Any;
117 xKeyInfoPtr k = (xKeyInfoPtr) *any;
120 K->length = pad_to_xid(sizeof(XKeyInfo));
121 K->min_keycode = k->min_keycode;
122 K->max_keycode = k->max_keycode;
123 K->num_keys = k->num_keys;
128 XButtonInfoPtr B = (XButtonInfoPtr) *Any;
129 xButtonInfoPtr b = (xButtonInfoPtr) *any;
131 B->class = ButtonClass;
132 B->length = pad_to_xid(sizeof(XButtonInfo));
133 B->num_buttons = b->num_buttons;
139 XValuatorInfoPtr V = (XValuatorInfoPtr) *Any;
140 xValuatorInfoPtr v = (xValuatorInfoPtr) *any;
144 V->class = ValuatorClass;
145 V->length = pad_to_xid(sizeof(XValuatorInfo) +
146 (v->num_axes * sizeof(XAxisInfo)));
147 V->num_axes = v->num_axes;
148 V->motion_buffer = v->motion_buffer_size;
150 A = (XAxisInfoPtr) ((char *)V + sizeof(XValuatorInfo));
152 a = (xAxisInfoPtr) ((char *)(*any) + sizeof(xValuatorInfo));
153 for (k = 0; k < (int)v->num_axes; k++, a++, A++) {
154 A->min_value = a->min_value;
155 A->max_value = a->max_value;
156 A->resolution = a->resolution;
163 *any = (xAnyClassPtr) ((char *)(*any) + (*any)->length);
164 *Any = (XAnyClassPtr) ((char *)(*Any) + (*Any)->length);
170 register Display *dpy,
174 xListInputDevicesReq *req;
175 xListInputDevicesReply rep;
176 xDeviceInfo *list, *slist = NULL;
177 XDeviceInfo *sclist = NULL;
178 XDeviceInfo *clist = NULL;
179 xAnyClassPtr any, sav_any;
181 unsigned char *nptr, *Nptr;
184 XExtDisplayInfo *info = XInput_find_display(dpy);
187 if (_XiCheckExtInit(dpy, XInput_Initial_Release, info) == -1)
188 return ((XDeviceInfo *) NULL);
190 GetReq(ListInputDevices, req);
191 req->reqType = info->codes->major_opcode;
192 req->ReqType = X_ListInputDevices;
194 if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
197 return (XDeviceInfo *) NULL;
200 if ((*ndevices = rep.ndevices)) { /* at least 1 input device */
201 size = *ndevices * sizeof(XDeviceInfo);
202 if (rep.length < (INT_MAX >> 2)) {
203 rlen = rep.length << 2; /* multiply length by 4 */
204 slist = list = Xmalloc(rlen);
207 _XEatDataWords(dpy, rep.length);
210 return (XDeviceInfo *) NULL;
212 _XRead(dpy, (char *)list, rlen);
214 any = (xAnyClassPtr) ((char *)list + (*ndevices * sizeof(xDeviceInfo)));
216 for (i = 0; i < *ndevices; i++, list++) {
217 size += SizeClassInfo(&any, (int)list->num_classes);
220 Nptr = ((unsigned char *)list) + rlen + 1;
221 for (i = 0, nptr = (unsigned char *)any; i < *ndevices; i++) {
228 clist = (XDeviceInfoPtr) Xmalloc(size);
230 XFree((char *)slist);
233 return (XDeviceInfo *) NULL;
236 Any = (XAnyClassPtr) ((char *)clist +
237 (*ndevices * sizeof(XDeviceInfo)));
240 for (i = 0; i < *ndevices; i++, list++, clist++) {
241 clist->type = list->type;
242 clist->id = list->id;
243 clist->use = list->use;
244 clist->num_classes = list->num_classes;
245 clist->inputclassinfo = Any;
247 ParseClassInfo(&any, &Any, (int)list->num_classes);
251 nptr = (unsigned char *)any;
252 Nptr = (unsigned char *)Any;
253 for (i = 0; i < *ndevices; i++, clist++) {
254 clist->name = (char *)Nptr;
255 memcpy(Nptr, nptr + 1, *nptr);
263 XFree((char *)slist);
269 /***********************************************************************
271 * Free the list of input devices.
276 XFreeDeviceList(XDeviceInfo *list)