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