summaryrefslogtreecommitdiff
path: root/lib/libXi/src/XListDev.c
blob: 5d9e2b3064fd72f6b773e9901ad17caa270f72c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/************************************************************

Copyright 1989, 1998  The Open Group

Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of The Open Group shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from The Open Group.

Copyright 1989 by Hewlett-Packard Company, Palo Alto, California.

			All Rights Reserved

Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation, and that the name of Hewlett-Packard not be
used in advertising or publicity pertaining to distribution of the
software without specific, written prior permission.

HEWLETT-PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
HEWLETT-PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
SOFTWARE.

********************************************************/

/***********************************************************************
 *
 * XListInputDevices - Request the server to return a list of 
 *			 available input devices.
 *
 */

#include <X11/extensions/XI.h>
#include <X11/extensions/XIproto.h>
#include <X11/Xlibint.h>
#include <X11/extensions/XInput.h>
#include <X11/extensions/extutil.h>
#include "XIint.h"

/* Calculate length field to a multiples of sizeof(XID). XIDs are typedefs
 * to ulong and thus may be 8 bytes on some platforms. This can trigger a
 * SIGBUS if a class ends up not being 8-aligned (e.g. after XAxisInfo).
 */
static int pad_to_xid(int base_size)
{
    int padsize = sizeof(XID);

    return ((base_size + padsize - 1)/padsize) * padsize;
}

static int
SizeClassInfo(xAnyClassPtr *any, int num_classes)
{
    int size = 0;
    int j;
    for (j = 0; j < num_classes; j++) {
        switch ((*any)->class) {
            case KeyClass:
                size += pad_to_xid(sizeof(XKeyInfo));
                break;
            case ButtonClass:
                size += pad_to_xid(sizeof(XButtonInfo));
                break;
            case ValuatorClass:
                {
                    xValuatorInfoPtr v;

                    v = (xValuatorInfoPtr) *any;
                    size += pad_to_xid(sizeof(XValuatorInfo) +
                        (v->num_axes * sizeof(XAxisInfo)));
                    break;
                }
            default:
                break;
        }
        *any = (xAnyClassPtr) ((char *)(*any) + (*any)->length);
    }

    return size;
}

static void
ParseClassInfo(xAnyClassPtr *any, XAnyClassPtr *Any, int num_classes)
{
    int j, k;

    for (j = 0; j < num_classes; j++) {
        switch ((*any)->class) {
            case KeyClass:
                {
                    XKeyInfoPtr K = (XKeyInfoPtr) *Any;
                    xKeyInfoPtr k = (xKeyInfoPtr) *any;

                    K->class = KeyClass;
                    K->length = pad_to_xid(sizeof(XKeyInfo));
                    K->min_keycode = k->min_keycode;
                    K->max_keycode = k->max_keycode;
                    K->num_keys = k->num_keys;
                    break;
                }
            case ButtonClass:
                {
                    XButtonInfoPtr B = (XButtonInfoPtr) *Any;
                    xButtonInfoPtr b = (xButtonInfoPtr) *any;

                    B->class = ButtonClass;
                    B->length = pad_to_xid(sizeof(XButtonInfo));
                    B->num_buttons = b->num_buttons;
                    break;
                }
            case ValuatorClass:
                {
                    XValuatorInfoPtr V = (XValuatorInfoPtr) *Any;
                    xValuatorInfoPtr v = (xValuatorInfoPtr) *any;
                    XAxisInfoPtr A;
                    xAxisInfoPtr a;

                    V->class = ValuatorClass;
                    V->length = pad_to_xid(sizeof(XValuatorInfo) +
                        (v->num_axes * sizeof(XAxisInfo)));
                    V->num_axes = v->num_axes;
                    V->motion_buffer = v->motion_buffer_size;
                    V->mode = v->mode;
                    A = (XAxisInfoPtr) ((char *)V + sizeof(XValuatorInfo));
                    V->axes = A;
                    a = (xAxisInfoPtr) ((char *)(*any) + sizeof(xValuatorInfo));
                    for (k = 0; k < (int)v->num_axes; k++, a++, A++) {
                        A->min_value = a->min_value;
                        A->max_value = a->max_value;
                        A->resolution = a->resolution;
                    }
                    break;
                }
            default:
                break;
        }
        *any = (xAnyClassPtr) ((char *)(*any) + (*any)->length);
        *Any = (XAnyClassPtr) ((char *)(*Any) + (*Any)->length);
    }
}

XDeviceInfo *
XListInputDevices(
    register Display	*dpy,
    int			*ndevices)
{
    int size;
    xListInputDevicesReq *req;
    xListInputDevicesReply rep;
    xDeviceInfo *list, *slist = NULL;
    XDeviceInfo *sclist = NULL;
    XDeviceInfo *clist = NULL;
    xAnyClassPtr any, sav_any;
    XAnyClassPtr Any;
    char *nptr, *Nptr;
    int i;
    long rlen;
    XExtDisplayInfo *info = XInput_find_display(dpy);

    LockDisplay(dpy);
    if (_XiCheckExtInit(dpy, XInput_Initial_Release, info) == -1)
	return ((XDeviceInfo *) NULL);

    GetReq(ListInputDevices, req);
    req->reqType = info->codes->major_opcode;
    req->ReqType = X_ListInputDevices;

    if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
	UnlockDisplay(dpy);
	SyncHandle();
	return (XDeviceInfo *) NULL;
    }

    if ((*ndevices = rep.ndevices)) {	/* at least 1 input device */
	size = *ndevices * sizeof(XDeviceInfo);
	rlen = rep.length << 2;	/* multiply length by 4    */
	list = (xDeviceInfo *) Xmalloc(rlen);
	slist = list;
	if (!slist) {
	    _XEatData(dpy, (unsigned long)rlen);
	    UnlockDisplay(dpy);
	    SyncHandle();
	    return (XDeviceInfo *) NULL;
	}
	_XRead(dpy, (char *)list, rlen);

	any = (xAnyClassPtr) ((char *)list + (*ndevices * sizeof(xDeviceInfo)));
	sav_any = any;
	for (i = 0; i < *ndevices; i++, list++) {
            size += SizeClassInfo(&any, (int)list->num_classes);
	}

	for (i = 0, nptr = (char *)any; i < *ndevices; i++) {
	    size += *nptr + 1;
	    nptr += (*nptr + 1);
	}

	clist = (XDeviceInfoPtr) Xmalloc(size);
	if (!clist) {
	    XFree((char *)slist);
	    UnlockDisplay(dpy);
	    SyncHandle();
	    return (XDeviceInfo *) NULL;
	}
	sclist = clist;
	Any = (XAnyClassPtr) ((char *)clist +
			      (*ndevices * sizeof(XDeviceInfo)));
	list = slist;
	any = sav_any;
	for (i = 0; i < *ndevices; i++, list++, clist++) {
	    clist->type = list->type;
	    clist->id = list->id;
	    clist->use = list->use;
	    clist->num_classes = list->num_classes;
	    clist->inputclassinfo = Any;

            ParseClassInfo(&any, &Any, (int)list->num_classes);
	}

	clist = sclist;
	nptr = (char *)any;
	Nptr = (char *)Any;
	for (i = 0; i < *ndevices; i++, clist++) {
	    clist->name = (char *)Nptr;
	    memcpy(Nptr, nptr + 1, *nptr);
	    Nptr += (*nptr);
	    *Nptr++ = '\0';
	    nptr += (*nptr + 1);
	}
    }

    XFree((char *)slist);
    UnlockDisplay(dpy);
    SyncHandle();
    return (sclist);
}

/***********************************************************************
 *
 * Free the list of input devices.
 *
 */

void
XFreeDeviceList(XDeviceInfo *list)
{
    if (list != NULL) {
	XFree((char *)list);
    }
}