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
76d678aadfSmrgstatic int
77d678aadfSmrgSizeClassInfo(xAnyClassPtr *any, size_t len, int num_classes, size_t *size)
78c27c18e8Smrg{
79c27c18e8Smrg    int j;
80d678aadfSmrg    size_t sz = 0;
81d678aadfSmrg
82c27c18e8Smrg    for (j = 0; j < num_classes; j++) {
83c27c18e8Smrg        switch ((*any)->class) {
84c27c18e8Smrg            case KeyClass:
85d678aadfSmrg                sz += pad_to_xid(sizeof(XKeyInfo));
86c27c18e8Smrg                break;
87c27c18e8Smrg            case ButtonClass:
88d678aadfSmrg                sz += pad_to_xid(sizeof(XButtonInfo));
89c27c18e8Smrg                break;
90c27c18e8Smrg            case ValuatorClass:
91c27c18e8Smrg                {
92c27c18e8Smrg                    xValuatorInfoPtr v;
93c27c18e8Smrg
94f1c62215Smrg                    if (len < sizeof(v))
95d678aadfSmrg                        return 1;
96c27c18e8Smrg                    v = (xValuatorInfoPtr) *any;
97d678aadfSmrg                    sz += pad_to_xid(sizeof(XValuatorInfo) +
98f1ee322dSmrg                        (v->num_axes * sizeof(XAxisInfo)));
99c27c18e8Smrg                    break;
100c27c18e8Smrg                }
101c27c18e8Smrg            default:
102c27c18e8Smrg                break;
103c27c18e8Smrg        }
104f1c62215Smrg        if ((*any)->length > len)
105d678aadfSmrg            return 1;
106c27c18e8Smrg        *any = (xAnyClassPtr) ((char *)(*any) + (*any)->length);
107c27c18e8Smrg    }
108c27c18e8Smrg
109d678aadfSmrg    *size = sz;
110d678aadfSmrg
111d678aadfSmrg    return 0;
112c27c18e8Smrg}
113c27c18e8Smrg
114c27c18e8Smrgstatic void
115c27c18e8SmrgParseClassInfo(xAnyClassPtr *any, XAnyClassPtr *Any, int num_classes)
116c27c18e8Smrg{
117f1ee322dSmrg    int j;
118c27c18e8Smrg
119c27c18e8Smrg    for (j = 0; j < num_classes; j++) {
120c27c18e8Smrg        switch ((*any)->class) {
121c27c18e8Smrg            case KeyClass:
122c27c18e8Smrg                {
123c27c18e8Smrg                    XKeyInfoPtr K = (XKeyInfoPtr) *Any;
124c27c18e8Smrg                    xKeyInfoPtr k = (xKeyInfoPtr) *any;
125c27c18e8Smrg
126c27c18e8Smrg                    K->class = KeyClass;
127f1ee322dSmrg                    K->length = pad_to_xid(sizeof(XKeyInfo));
128c27c18e8Smrg                    K->min_keycode = k->min_keycode;
129c27c18e8Smrg                    K->max_keycode = k->max_keycode;
130c27c18e8Smrg                    K->num_keys = k->num_keys;
131c27c18e8Smrg                    break;
132c27c18e8Smrg                }
133c27c18e8Smrg            case ButtonClass:
134c27c18e8Smrg                {
135c27c18e8Smrg                    XButtonInfoPtr B = (XButtonInfoPtr) *Any;
136c27c18e8Smrg                    xButtonInfoPtr b = (xButtonInfoPtr) *any;
137c27c18e8Smrg
138c27c18e8Smrg                    B->class = ButtonClass;
139f1ee322dSmrg                    B->length = pad_to_xid(sizeof(XButtonInfo));
140c27c18e8Smrg                    B->num_buttons = b->num_buttons;
141c27c18e8Smrg                    break;
142c27c18e8Smrg                }
143c27c18e8Smrg            case ValuatorClass:
144c27c18e8Smrg                {
145f1ee322dSmrg                    int k;
146c27c18e8Smrg                    XValuatorInfoPtr V = (XValuatorInfoPtr) *Any;
147c27c18e8Smrg                    xValuatorInfoPtr v = (xValuatorInfoPtr) *any;
148c27c18e8Smrg                    XAxisInfoPtr A;
149c27c18e8Smrg                    xAxisInfoPtr a;
150c27c18e8Smrg
151c27c18e8Smrg                    V->class = ValuatorClass;
152f1ee322dSmrg                    V->length = pad_to_xid(sizeof(XValuatorInfo) +
153f1ee322dSmrg                        (v->num_axes * sizeof(XAxisInfo)));
154c27c18e8Smrg                    V->num_axes = v->num_axes;
155c27c18e8Smrg                    V->motion_buffer = v->motion_buffer_size;
156c27c18e8Smrg                    V->mode = v->mode;
157c27c18e8Smrg                    A = (XAxisInfoPtr) ((char *)V + sizeof(XValuatorInfo));
158c27c18e8Smrg                    V->axes = A;
159c27c18e8Smrg                    a = (xAxisInfoPtr) ((char *)(*any) + sizeof(xValuatorInfo));
160c27c18e8Smrg                    for (k = 0; k < (int)v->num_axes; k++, a++, A++) {
161c27c18e8Smrg                        A->min_value = a->min_value;
162c27c18e8Smrg                        A->max_value = a->max_value;
163c27c18e8Smrg                        A->resolution = a->resolution;
164c27c18e8Smrg                    }
165c27c18e8Smrg                    break;
166c27c18e8Smrg                }
167c27c18e8Smrg            default:
168c27c18e8Smrg                break;
169c27c18e8Smrg        }
170c27c18e8Smrg        *any = (xAnyClassPtr) ((char *)(*any) + (*any)->length);
171c27c18e8Smrg        *Any = (XAnyClassPtr) ((char *)(*Any) + (*Any)->length);
172c27c18e8Smrg    }
173c27c18e8Smrg}
174c27c18e8Smrg
175c43cc173SmrgXDeviceInfo *
176c27c18e8SmrgXListInputDevices(
177c27c18e8Smrg    register Display	*dpy,
178d678aadfSmrg    int			*ndevices_return)
179c43cc173Smrg{
180f1c62215Smrg    size_t s, size;
181c43cc173Smrg    xListInputDevicesReq *req;
182c43cc173Smrg    xListInputDevicesReply rep;
183c43cc173Smrg    xDeviceInfo *list, *slist = NULL;
184c43cc173Smrg    XDeviceInfo *sclist = NULL;
185c43cc173Smrg    XDeviceInfo *clist = NULL;
186c43cc173Smrg    xAnyClassPtr any, sav_any;
187c43cc173Smrg    XAnyClassPtr Any;
188f1c62215Smrg    char *end = NULL;
189190694daSmrg    unsigned char *nptr, *Nptr;
190c27c18e8Smrg    int i;
191190694daSmrg    unsigned long rlen;
192c43cc173Smrg    XExtDisplayInfo *info = XInput_find_display(dpy);
193d678aadfSmrg    int ndevices;
194c43cc173Smrg
195c43cc173Smrg    LockDisplay(dpy);
196c43cc173Smrg    if (_XiCheckExtInit(dpy, XInput_Initial_Release, info) == -1)
197c43cc173Smrg	return ((XDeviceInfo *) NULL);
198c43cc173Smrg
199c43cc173Smrg    GetReq(ListInputDevices, req);
200c43cc173Smrg    req->reqType = info->codes->major_opcode;
201c43cc173Smrg    req->ReqType = X_ListInputDevices;
202c43cc173Smrg
203c43cc173Smrg    if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
204c43cc173Smrg	UnlockDisplay(dpy);
205c43cc173Smrg	SyncHandle();
206c43cc173Smrg	return (XDeviceInfo *) NULL;
207c43cc173Smrg    }
208c43cc173Smrg
209d678aadfSmrg    if ((ndevices = rep.ndevices)) {	/* at least 1 input device */
210d678aadfSmrg	size = ndevices * sizeof(XDeviceInfo);
211190694daSmrg	if (rep.length < (INT_MAX >> 2)) {
212190694daSmrg	    rlen = rep.length << 2;	/* multiply length by 4    */
213190694daSmrg	    slist = list = Xmalloc(rlen);
214190694daSmrg	}
215c43cc173Smrg	if (!slist) {
21610baa824Smrg	    _XEatDataWords(dpy, rep.length);
217c43cc173Smrg	    UnlockDisplay(dpy);
218c43cc173Smrg	    SyncHandle();
219c43cc173Smrg	    return (XDeviceInfo *) NULL;
220c43cc173Smrg	}
221c43cc173Smrg	_XRead(dpy, (char *)list, rlen);
222c43cc173Smrg
223d678aadfSmrg	any = (xAnyClassPtr) ((char *)list + (ndevices * sizeof(xDeviceInfo)));
224c43cc173Smrg	sav_any = any;
225f1c62215Smrg	end = (char *)list + rlen;
226d678aadfSmrg	for (i = 0; i < ndevices; i++, list++) {
227d678aadfSmrg            if(SizeClassInfo(&any, end - (char *)any, (int)list->num_classes, &s))
228f1c62215Smrg                goto out;
229f1c62215Smrg            size += s;
230c43cc173Smrg	}
231c43cc173Smrg
232f1c62215Smrg	Nptr = ((unsigned char *)list) + rlen;
233d678aadfSmrg	for (i = 0, nptr = (unsigned char *)any; i < ndevices; i++) {
234f1c62215Smrg	    if (nptr >= Nptr)
235f1c62215Smrg		goto out;
236c43cc173Smrg	    size += *nptr + 1;
237c43cc173Smrg	    nptr += (*nptr + 1);
238c43cc173Smrg	}
239c43cc173Smrg
240c43cc173Smrg	clist = (XDeviceInfoPtr) Xmalloc(size);
241c43cc173Smrg	if (!clist) {
242c43cc173Smrg	    XFree((char *)slist);
243c43cc173Smrg	    UnlockDisplay(dpy);
244c43cc173Smrg	    SyncHandle();
245c43cc173Smrg	    return (XDeviceInfo *) NULL;
246c43cc173Smrg	}
247c43cc173Smrg	sclist = clist;
248c43cc173Smrg	Any = (XAnyClassPtr) ((char *)clist +
249d678aadfSmrg			      (ndevices * sizeof(XDeviceInfo)));
250c43cc173Smrg	list = slist;
251c43cc173Smrg	any = sav_any;
252d678aadfSmrg	for (i = 0; i < ndevices; i++, list++, clist++) {
253c43cc173Smrg	    clist->type = list->type;
254c43cc173Smrg	    clist->id = list->id;
255c43cc173Smrg	    clist->use = list->use;
256c43cc173Smrg	    clist->num_classes = list->num_classes;
257c43cc173Smrg	    clist->inputclassinfo = Any;
258c27c18e8Smrg
259c27c18e8Smrg            ParseClassInfo(&any, &Any, (int)list->num_classes);
260c43cc173Smrg	}
261c43cc173Smrg
262c43cc173Smrg	clist = sclist;
263190694daSmrg	nptr = (unsigned char *)any;
264190694daSmrg	Nptr = (unsigned char *)Any;
265d678aadfSmrg	for (i = 0; i < ndevices; i++, clist++) {
266c43cc173Smrg	    clist->name = (char *)Nptr;
267c43cc173Smrg	    memcpy(Nptr, nptr + 1, *nptr);
268c43cc173Smrg	    Nptr += (*nptr);
269c43cc173Smrg	    *Nptr++ = '\0';
270c43cc173Smrg	    nptr += (*nptr + 1);
271c43cc173Smrg	}
272c43cc173Smrg    }
273c43cc173Smrg
274d678aadfSmrg    *ndevices_return = ndevices;
275d678aadfSmrg
276190694daSmrg  out:
277c43cc173Smrg    XFree((char *)slist);
278c43cc173Smrg    UnlockDisplay(dpy);
279c43cc173Smrg    SyncHandle();
280c43cc173Smrg    return (sclist);
281c43cc173Smrg}
282c43cc173Smrg
283c43cc173Smrg/***********************************************************************
284c43cc173Smrg *
285c43cc173Smrg * Free the list of input devices.
286c43cc173Smrg *
287c43cc173Smrg */
288c43cc173Smrg
289c43cc173Smrgvoid
290c27c18e8SmrgXFreeDeviceList(XDeviceInfo *list)
291c43cc173Smrg{
292c43cc173Smrg    if (list != NULL) {
293c43cc173Smrg	XFree((char *)list);
294c43cc173Smrg    }
295c43cc173Smrg}
296