XListDev.c revision f1ee322d
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"
63c43cc173Smrg
64f1ee322dSmrg/* Calculate length field to a multiples of sizeof(XID). XIDs are typedefs
65f1ee322dSmrg * to ulong and thus may be 8 bytes on some platforms. This can trigger a
66f1ee322dSmrg * SIGBUS if a class ends up not being 8-aligned (e.g. after XAxisInfo).
67f1ee322dSmrg */
68f1ee322dSmrgstatic int pad_to_xid(int base_size)
69f1ee322dSmrg{
70f1ee322dSmrg    int padsize = sizeof(XID);
71f1ee322dSmrg
72f1ee322dSmrg    return ((base_size + padsize - 1)/padsize) * padsize;
73f1ee322dSmrg}
74f1ee322dSmrg
75c27c18e8Smrgstatic int
76c27c18e8SmrgSizeClassInfo(xAnyClassPtr *any, int num_classes)
77c27c18e8Smrg{
78c27c18e8Smrg    int size = 0;
79c27c18e8Smrg    int j;
80c27c18e8Smrg    for (j = 0; j < num_classes; j++) {
81c27c18e8Smrg        switch ((*any)->class) {
82c27c18e8Smrg            case KeyClass:
83f1ee322dSmrg                size += pad_to_xid(sizeof(XKeyInfo));
84c27c18e8Smrg                break;
85c27c18e8Smrg            case ButtonClass:
86f1ee322dSmrg                size += pad_to_xid(sizeof(XButtonInfo));
87c27c18e8Smrg                break;
88c27c18e8Smrg            case ValuatorClass:
89c27c18e8Smrg                {
90c27c18e8Smrg                    xValuatorInfoPtr v;
91c27c18e8Smrg
92c27c18e8Smrg                    v = (xValuatorInfoPtr) *any;
93f1ee322dSmrg                    size += pad_to_xid(sizeof(XValuatorInfo) +
94f1ee322dSmrg                        (v->num_axes * sizeof(XAxisInfo)));
95c27c18e8Smrg                    break;
96c27c18e8Smrg                }
97c27c18e8Smrg            default:
98c27c18e8Smrg                break;
99c27c18e8Smrg        }
100c27c18e8Smrg        *any = (xAnyClassPtr) ((char *)(*any) + (*any)->length);
101c27c18e8Smrg    }
102c27c18e8Smrg
103c27c18e8Smrg    return size;
104c27c18e8Smrg}
105c27c18e8Smrg
106c27c18e8Smrgstatic void
107c27c18e8SmrgParseClassInfo(xAnyClassPtr *any, XAnyClassPtr *Any, int num_classes)
108c27c18e8Smrg{
109f1ee322dSmrg    int j;
110c27c18e8Smrg
111c27c18e8Smrg    for (j = 0; j < num_classes; j++) {
112c27c18e8Smrg        switch ((*any)->class) {
113c27c18e8Smrg            case KeyClass:
114c27c18e8Smrg                {
115c27c18e8Smrg                    XKeyInfoPtr K = (XKeyInfoPtr) *Any;
116c27c18e8Smrg                    xKeyInfoPtr k = (xKeyInfoPtr) *any;
117c27c18e8Smrg
118c27c18e8Smrg                    K->class = KeyClass;
119f1ee322dSmrg                    K->length = pad_to_xid(sizeof(XKeyInfo));
120c27c18e8Smrg                    K->min_keycode = k->min_keycode;
121c27c18e8Smrg                    K->max_keycode = k->max_keycode;
122c27c18e8Smrg                    K->num_keys = k->num_keys;
123c27c18e8Smrg                    break;
124c27c18e8Smrg                }
125c27c18e8Smrg            case ButtonClass:
126c27c18e8Smrg                {
127c27c18e8Smrg                    XButtonInfoPtr B = (XButtonInfoPtr) *Any;
128c27c18e8Smrg                    xButtonInfoPtr b = (xButtonInfoPtr) *any;
129c27c18e8Smrg
130c27c18e8Smrg                    B->class = ButtonClass;
131f1ee322dSmrg                    B->length = pad_to_xid(sizeof(XButtonInfo));
132c27c18e8Smrg                    B->num_buttons = b->num_buttons;
133c27c18e8Smrg                    break;
134c27c18e8Smrg                }
135c27c18e8Smrg            case ValuatorClass:
136c27c18e8Smrg                {
137f1ee322dSmrg                    int k;
138c27c18e8Smrg                    XValuatorInfoPtr V = (XValuatorInfoPtr) *Any;
139c27c18e8Smrg                    xValuatorInfoPtr v = (xValuatorInfoPtr) *any;
140c27c18e8Smrg                    XAxisInfoPtr A;
141c27c18e8Smrg                    xAxisInfoPtr a;
142c27c18e8Smrg
143c27c18e8Smrg                    V->class = ValuatorClass;
144f1ee322dSmrg                    V->length = pad_to_xid(sizeof(XValuatorInfo) +
145f1ee322dSmrg                        (v->num_axes * sizeof(XAxisInfo)));
146c27c18e8Smrg                    V->num_axes = v->num_axes;
147c27c18e8Smrg                    V->motion_buffer = v->motion_buffer_size;
148c27c18e8Smrg                    V->mode = v->mode;
149c27c18e8Smrg                    A = (XAxisInfoPtr) ((char *)V + sizeof(XValuatorInfo));
150c27c18e8Smrg                    V->axes = A;
151c27c18e8Smrg                    a = (xAxisInfoPtr) ((char *)(*any) + sizeof(xValuatorInfo));
152c27c18e8Smrg                    for (k = 0; k < (int)v->num_axes; k++, a++, A++) {
153c27c18e8Smrg                        A->min_value = a->min_value;
154c27c18e8Smrg                        A->max_value = a->max_value;
155c27c18e8Smrg                        A->resolution = a->resolution;
156c27c18e8Smrg                    }
157c27c18e8Smrg                    break;
158c27c18e8Smrg                }
159c27c18e8Smrg            default:
160c27c18e8Smrg                break;
161c27c18e8Smrg        }
162c27c18e8Smrg        *any = (xAnyClassPtr) ((char *)(*any) + (*any)->length);
163c27c18e8Smrg        *Any = (XAnyClassPtr) ((char *)(*Any) + (*Any)->length);
164c27c18e8Smrg    }
165c27c18e8Smrg}
166c27c18e8Smrg
167c43cc173SmrgXDeviceInfo *
168c27c18e8SmrgXListInputDevices(
169c27c18e8Smrg    register Display	*dpy,
170c27c18e8Smrg    int			*ndevices)
171c43cc173Smrg{
172c43cc173Smrg    int size;
173c43cc173Smrg    xListInputDevicesReq *req;
174c43cc173Smrg    xListInputDevicesReply rep;
175c43cc173Smrg    xDeviceInfo *list, *slist = NULL;
176c43cc173Smrg    XDeviceInfo *sclist = NULL;
177c43cc173Smrg    XDeviceInfo *clist = NULL;
178c43cc173Smrg    xAnyClassPtr any, sav_any;
179c43cc173Smrg    XAnyClassPtr Any;
180c43cc173Smrg    char *nptr, *Nptr;
181c27c18e8Smrg    int i;
182c27c18e8Smrg    long rlen;
183c43cc173Smrg    XExtDisplayInfo *info = XInput_find_display(dpy);
184c43cc173Smrg
185c43cc173Smrg    LockDisplay(dpy);
186c43cc173Smrg    if (_XiCheckExtInit(dpy, XInput_Initial_Release, info) == -1)
187c43cc173Smrg	return ((XDeviceInfo *) NULL);
188c43cc173Smrg
189c43cc173Smrg    GetReq(ListInputDevices, req);
190c43cc173Smrg    req->reqType = info->codes->major_opcode;
191c43cc173Smrg    req->ReqType = X_ListInputDevices;
192c43cc173Smrg
193c43cc173Smrg    if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
194c43cc173Smrg	UnlockDisplay(dpy);
195c43cc173Smrg	SyncHandle();
196c43cc173Smrg	return (XDeviceInfo *) NULL;
197c43cc173Smrg    }
198c43cc173Smrg
199c43cc173Smrg    if ((*ndevices = rep.ndevices)) {	/* at least 1 input device */
200c43cc173Smrg	size = *ndevices * sizeof(XDeviceInfo);
201c43cc173Smrg	rlen = rep.length << 2;	/* multiply length by 4    */
202c43cc173Smrg	list = (xDeviceInfo *) Xmalloc(rlen);
203c43cc173Smrg	slist = list;
204c43cc173Smrg	if (!slist) {
205c43cc173Smrg	    _XEatData(dpy, (unsigned long)rlen);
206c43cc173Smrg	    UnlockDisplay(dpy);
207c43cc173Smrg	    SyncHandle();
208c43cc173Smrg	    return (XDeviceInfo *) NULL;
209c43cc173Smrg	}
210c43cc173Smrg	_XRead(dpy, (char *)list, rlen);
211c43cc173Smrg
212c43cc173Smrg	any = (xAnyClassPtr) ((char *)list + (*ndevices * sizeof(xDeviceInfo)));
213c43cc173Smrg	sav_any = any;
214c43cc173Smrg	for (i = 0; i < *ndevices; i++, list++) {
215c27c18e8Smrg            size += SizeClassInfo(&any, (int)list->num_classes);
216c43cc173Smrg	}
217c43cc173Smrg
218c43cc173Smrg	for (i = 0, nptr = (char *)any; i < *ndevices; i++) {
219c43cc173Smrg	    size += *nptr + 1;
220c43cc173Smrg	    nptr += (*nptr + 1);
221c43cc173Smrg	}
222c43cc173Smrg
223c43cc173Smrg	clist = (XDeviceInfoPtr) Xmalloc(size);
224c43cc173Smrg	if (!clist) {
225c43cc173Smrg	    XFree((char *)slist);
226c43cc173Smrg	    UnlockDisplay(dpy);
227c43cc173Smrg	    SyncHandle();
228c43cc173Smrg	    return (XDeviceInfo *) NULL;
229c43cc173Smrg	}
230c43cc173Smrg	sclist = clist;
231c43cc173Smrg	Any = (XAnyClassPtr) ((char *)clist +
232c43cc173Smrg			      (*ndevices * sizeof(XDeviceInfo)));
233c43cc173Smrg	list = slist;
234c43cc173Smrg	any = sav_any;
235c43cc173Smrg	for (i = 0; i < *ndevices; i++, list++, clist++) {
236c43cc173Smrg	    clist->type = list->type;
237c43cc173Smrg	    clist->id = list->id;
238c43cc173Smrg	    clist->use = list->use;
239c43cc173Smrg	    clist->num_classes = list->num_classes;
240c43cc173Smrg	    clist->inputclassinfo = Any;
241c27c18e8Smrg
242c27c18e8Smrg            ParseClassInfo(&any, &Any, (int)list->num_classes);
243c43cc173Smrg	}
244c43cc173Smrg
245c43cc173Smrg	clist = sclist;
246c43cc173Smrg	nptr = (char *)any;
247c43cc173Smrg	Nptr = (char *)Any;
248c43cc173Smrg	for (i = 0; i < *ndevices; i++, clist++) {
249c43cc173Smrg	    clist->name = (char *)Nptr;
250c43cc173Smrg	    memcpy(Nptr, nptr + 1, *nptr);
251c43cc173Smrg	    Nptr += (*nptr);
252c43cc173Smrg	    *Nptr++ = '\0';
253c43cc173Smrg	    nptr += (*nptr + 1);
254c43cc173Smrg	}
255c43cc173Smrg    }
256c43cc173Smrg
257c43cc173Smrg    XFree((char *)slist);
258c43cc173Smrg    UnlockDisplay(dpy);
259c43cc173Smrg    SyncHandle();
260c43cc173Smrg    return (sclist);
261c43cc173Smrg}
262c43cc173Smrg
263c43cc173Smrg/***********************************************************************
264c43cc173Smrg *
265c43cc173Smrg * Free the list of input devices.
266c43cc173Smrg *
267c43cc173Smrg */
268c43cc173Smrg
269c43cc173Smrgvoid
270c27c18e8SmrgXFreeDeviceList(XDeviceInfo *list)
271c43cc173Smrg{
272c43cc173Smrg    if (list != NULL) {
273c43cc173Smrg	XFree((char *)list);
274c43cc173Smrg    }
275c43cc173Smrg}
276