6b9123847f70ccbc1d5c000e12f54de6d03fa011
[platform/upstream/libXi.git] / src / XListDev.c
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
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).
67  */
68 static int pad_to_xid(int base_size)
69 {
70     int padsize = sizeof(XID);
71
72     return ((base_size + padsize - 1)/padsize) * padsize;
73 }
74
75 static int
76 SizeClassInfo(xAnyClassPtr *any, int num_classes)
77 {
78     int size = 0;
79     int j;
80     for (j = 0; j < num_classes; j++) {
81         switch ((*any)->class) {
82             case KeyClass:
83                 size += pad_to_xid(sizeof(XKeyInfo));
84                 break;
85             case ButtonClass:
86                 size += pad_to_xid(sizeof(XButtonInfo));
87                 break;
88             case ValuatorClass:
89                 {
90                     xValuatorInfoPtr v;
91
92                     v = (xValuatorInfoPtr) *any;
93                     size += pad_to_xid(sizeof(XValuatorInfo) +
94                         (v->num_axes * sizeof(XAxisInfo)));
95                     break;
96                 }
97             default:
98                 break;
99         }
100         *any = (xAnyClassPtr) ((char *)(*any) + (*any)->length);
101     }
102
103     return size;
104 }
105
106 static void
107 ParseClassInfo(xAnyClassPtr *any, XAnyClassPtr *Any, int num_classes)
108 {
109     int j, k;
110
111     for (j = 0; j < num_classes; j++) {
112         switch ((*any)->class) {
113             case KeyClass:
114                 {
115                     XKeyInfoPtr K = (XKeyInfoPtr) *Any;
116                     xKeyInfoPtr k = (xKeyInfoPtr) *any;
117
118                     K->class = KeyClass;
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;
123                     break;
124                 }
125             case ButtonClass:
126                 {
127                     XButtonInfoPtr B = (XButtonInfoPtr) *Any;
128                     xButtonInfoPtr b = (xButtonInfoPtr) *any;
129
130                     B->class = ButtonClass;
131                     B->length = pad_to_xid(sizeof(XButtonInfo));
132                     B->num_buttons = b->num_buttons;
133                     break;
134                 }
135             case ValuatorClass:
136                 {
137                     XValuatorInfoPtr V = (XValuatorInfoPtr) *Any;
138                     xValuatorInfoPtr v = (xValuatorInfoPtr) *any;
139                     XAxisInfoPtr A;
140                     xAxisInfoPtr a;
141
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;
147                     V->mode = v->mode;
148                     A = (XAxisInfoPtr) ((char *)V + sizeof(XValuatorInfo));
149                     V->axes = A;
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;
155                     }
156                     break;
157                 }
158             default:
159                 break;
160         }
161         *any = (xAnyClassPtr) ((char *)(*any) + (*any)->length);
162         *Any = (XAnyClassPtr) ((char *)(*Any) + (*Any)->length);
163     }
164 }
165
166 XDeviceInfo *
167 XListInputDevices(
168     register Display    *dpy,
169     int                 *ndevices)
170 {
171     int size;
172     xListInputDevicesReq *req;
173     xListInputDevicesReply rep;
174     xDeviceInfo *list, *slist = NULL;
175     XDeviceInfo *sclist = NULL;
176     XDeviceInfo *clist = NULL;
177     xAnyClassPtr any, sav_any;
178     XAnyClassPtr Any;
179     char *nptr, *Nptr;
180     int i;
181     long rlen;
182     XExtDisplayInfo *info = XInput_find_display(dpy);
183
184     LockDisplay(dpy);
185     if (_XiCheckExtInit(dpy, XInput_Initial_Release, info) == -1)
186         return ((XDeviceInfo *) NULL);
187
188     GetReq(ListInputDevices, req);
189     req->reqType = info->codes->major_opcode;
190     req->ReqType = X_ListInputDevices;
191
192     if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
193         UnlockDisplay(dpy);
194         SyncHandle();
195         return (XDeviceInfo *) NULL;
196     }
197
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);
202         slist = list;
203         if (!slist) {
204             _XEatData(dpy, (unsigned long)rlen);
205             UnlockDisplay(dpy);
206             SyncHandle();
207             return (XDeviceInfo *) NULL;
208         }
209         _XRead(dpy, (char *)list, rlen);
210
211         any = (xAnyClassPtr) ((char *)list + (*ndevices * sizeof(xDeviceInfo)));
212         sav_any = any;
213         for (i = 0; i < *ndevices; i++, list++) {
214             size += SizeClassInfo(&any, (int)list->num_classes);
215         }
216
217         for (i = 0, nptr = (char *)any; i < *ndevices; i++) {
218             size += *nptr + 1;
219             nptr += (*nptr + 1);
220         }
221
222         clist = (XDeviceInfoPtr) Xmalloc(size);
223         if (!clist) {
224             XFree((char *)slist);
225             UnlockDisplay(dpy);
226             SyncHandle();
227             return (XDeviceInfo *) NULL;
228         }
229         sclist = clist;
230         Any = (XAnyClassPtr) ((char *)clist +
231                               (*ndevices * sizeof(XDeviceInfo)));
232         list = slist;
233         any = sav_any;
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;
240
241             ParseClassInfo(&any, &Any, (int)list->num_classes);
242         }
243
244         clist = sclist;
245         nptr = (char *)any;
246         Nptr = (char *)Any;
247         for (i = 0; i < *ndevices; i++, clist++) {
248             clist->name = (char *)Nptr;
249             memcpy(Nptr, nptr + 1, *nptr);
250             Nptr += (*nptr);
251             *Nptr++ = '\0';
252             nptr += (*nptr + 1);
253         }
254     }
255
256     XFree((char *)slist);
257     UnlockDisplay(dpy);
258     SyncHandle();
259     return (sclist);
260 }
261
262 /***********************************************************************
263  *
264  * Free the list of input devices.
265  *
266  */
267
268 void
269 XFreeDeviceList(XDeviceInfo *list)
270 {
271     if (list != NULL) {
272         XFree((char *)list);
273     }
274 }