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>
64 /* Calculate length field to a multiples of sizeof(XID). XIDs are typedefs
65 * to ulong and thus may be 8 bytes on some platforms. This can trigger a
66 * SIGBUS if a class ends up not being 8-aligned (e.g. after XAxisInfo).
68 static int pad_to_xid(int base_size)
70 int padsize = sizeof(XID);
72 return ((base_size + padsize - 1)/padsize) * padsize;
76 SizeClassInfo(xAnyClassPtr *any, int num_classes)
80 for (j = 0; j < num_classes; j++) {
81 switch ((*any)->class) {
83 size += pad_to_xid(sizeof(XKeyInfo));
86 size += pad_to_xid(sizeof(XButtonInfo));
92 v = (xValuatorInfoPtr) *any;
93 size += pad_to_xid(sizeof(XValuatorInfo) +
94 (v->num_axes * sizeof(XAxisInfo)));
100 *any = (xAnyClassPtr) ((char *)(*any) + (*any)->length);
107 ParseClassInfo(xAnyClassPtr *any, XAnyClassPtr *Any, int num_classes)
111 for (j = 0; j < num_classes; j++) {
112 switch ((*any)->class) {
115 XKeyInfoPtr K = (XKeyInfoPtr) *Any;
116 xKeyInfoPtr k = (xKeyInfoPtr) *any;
119 K->length = pad_to_xid(sizeof(XKeyInfo));
120 K->min_keycode = k->min_keycode;
121 K->max_keycode = k->max_keycode;
122 K->num_keys = k->num_keys;
127 XButtonInfoPtr B = (XButtonInfoPtr) *Any;
128 xButtonInfoPtr b = (xButtonInfoPtr) *any;
130 B->class = ButtonClass;
131 B->length = pad_to_xid(sizeof(XButtonInfo));
132 B->num_buttons = b->num_buttons;
137 XValuatorInfoPtr V = (XValuatorInfoPtr) *Any;
138 xValuatorInfoPtr v = (xValuatorInfoPtr) *any;
142 V->class = ValuatorClass;
143 V->length = pad_to_xid(sizeof(XValuatorInfo) +
144 (v->num_axes * sizeof(XAxisInfo)));
145 V->num_axes = v->num_axes;
146 V->motion_buffer = v->motion_buffer_size;
148 A = (XAxisInfoPtr) ((char *)V + sizeof(XValuatorInfo));
150 a = (xAxisInfoPtr) ((char *)(*any) + sizeof(xValuatorInfo));
151 for (k = 0; k < (int)v->num_axes; k++, a++, A++) {
152 A->min_value = a->min_value;
153 A->max_value = a->max_value;
154 A->resolution = a->resolution;
161 *any = (xAnyClassPtr) ((char *)(*any) + (*any)->length);
162 *Any = (XAnyClassPtr) ((char *)(*Any) + (*Any)->length);
168 register Display *dpy,
172 xListInputDevicesReq *req;
173 xListInputDevicesReply rep;
174 xDeviceInfo *list, *slist = NULL;
175 XDeviceInfo *sclist = NULL;
176 XDeviceInfo *clist = NULL;
177 xAnyClassPtr any, sav_any;
182 XExtDisplayInfo *info = XInput_find_display(dpy);
185 if (_XiCheckExtInit(dpy, XInput_Initial_Release, info) == -1)
186 return ((XDeviceInfo *) NULL);
188 GetReq(ListInputDevices, req);
189 req->reqType = info->codes->major_opcode;
190 req->ReqType = X_ListInputDevices;
192 if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
195 return (XDeviceInfo *) NULL;
198 if ((*ndevices = rep.ndevices)) { /* at least 1 input device */
199 size = *ndevices * sizeof(XDeviceInfo);
200 rlen = rep.length << 2; /* multiply length by 4 */
201 list = (xDeviceInfo *) Xmalloc(rlen);
204 _XEatData(dpy, (unsigned long)rlen);
207 return (XDeviceInfo *) NULL;
209 _XRead(dpy, (char *)list, rlen);
211 any = (xAnyClassPtr) ((char *)list + (*ndevices * sizeof(xDeviceInfo)));
213 for (i = 0; i < *ndevices; i++, list++) {
214 size += SizeClassInfo(&any, (int)list->num_classes);
217 for (i = 0, nptr = (char *)any; i < *ndevices; i++) {
222 clist = (XDeviceInfoPtr) Xmalloc(size);
224 XFree((char *)slist);
227 return (XDeviceInfo *) NULL;
230 Any = (XAnyClassPtr) ((char *)clist +
231 (*ndevices * sizeof(XDeviceInfo)));
234 for (i = 0; i < *ndevices; i++, list++, clist++) {
235 clist->type = list->type;
236 clist->id = list->id;
237 clist->use = list->use;
238 clist->num_classes = list->num_classes;
239 clist->inputclassinfo = Any;
241 ParseClassInfo(&any, &Any, (int)list->num_classes);
247 for (i = 0; i < *ndevices; i++, clist++) {
248 clist->name = (char *)Nptr;
249 memcpy(Nptr, nptr + 1, *nptr);
256 XFree((char *)slist);
262 /***********************************************************************
264 * Free the list of input devices.
269 XFreeDeviceList(XDeviceInfo *list)