XQueryDv.c revision f1c62215
1/************************************************************
2
3Copyright 1989, 1998  The Open Group
4
5Permission to use, copy, modify, distribute, and sell this software and its
6documentation for any purpose is hereby granted without fee, provided that
7the above copyright notice appear in all copies and that both that
8copyright notice and this permission notice appear in supporting
9documentation.
10
11The above copyright notice and this permission notice shall be included in
12all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
17OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
21Except as contained in this notice, the name of The Open Group shall not be
22used in advertising or otherwise to promote the sale, use or other dealings
23in this Software without prior written authorization from The Open Group.
24
25Copyright 1989 by Hewlett-Packard Company, Palo Alto, California.
26
27			All Rights Reserved
28
29Permission to use, copy, modify, and distribute this software and its
30documentation for any purpose and without fee is hereby granted,
31provided that the above copyright notice appear in all copies and that
32both that copyright notice and this permission notice appear in
33supporting documentation, and that the name of Hewlett-Packard not be
34used in advertising or publicity pertaining to distribution of the
35software without specific, written prior permission.
36
37HEWLETT-PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
38ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
39HEWLETT-PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
40ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
41WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
42ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
43SOFTWARE.
44
45********************************************************/
46
47/***********************************************************************
48 *
49 * XQueryDeviceState - Query the state of an extension input device.
50 *
51 */
52#ifdef HAVE_CONFIG_H
53#include <config.h>
54#endif
55
56#include <X11/extensions/XI.h>
57#include <X11/extensions/XIproto.h>
58#include <X11/Xlibint.h>
59#include <X11/extensions/XInput.h>
60#include <X11/extensions/extutil.h>
61#include "XIint.h"
62#include <limits.h>
63
64XDeviceState *
65XQueryDeviceState(
66    register Display	*dpy,
67    XDevice		*dev)
68{
69    int i, j;
70    unsigned long rlen;
71    size_t size = 0;
72    xQueryDeviceStateReq *req;
73    xQueryDeviceStateReply rep;
74    XDeviceState *state = NULL;
75    XInputClass *any, *Any;
76    char *data = NULL, *end = NULL;
77    XExtDisplayInfo *info = XInput_find_display(dpy);
78
79    LockDisplay(dpy);
80    if (_XiCheckExtInit(dpy, XInput_Initial_Release, info) == -1)
81        return NULL;
82
83    GetReq(QueryDeviceState, req);
84    req->reqType = info->codes->major_opcode;
85    req->ReqType = X_QueryDeviceState;
86    req->deviceid = dev->device_id;
87
88    if (!_XReply(dpy, (xReply *) & rep, 0, xFalse))
89        goto out;
90
91    if (rep.length > 0) {
92	if (rep.length < (INT_MAX >> 2)) {
93	    rlen = (unsigned long) rep.length << 2;
94	    data = Xmalloc(rlen);
95	    end = data + rlen;
96	}
97	if (!data) {
98	    _XEatDataWords(dpy, rep.length);
99	    goto out;
100	}
101	_XRead(dpy, data, rlen);
102
103	for (i = 0, any = (XInputClass *) data; i < (int)rep.num_classes; i++) {
104	    if ((char *)any + sizeof(XInputClass) > end ||
105		any->length == 0 || any->length > rlen)
106		goto out;
107	    rlen -= any->length;
108
109	    switch (any->class) {
110	    case KeyClass:
111		size += sizeof(XKeyState);
112		break;
113	    case ButtonClass:
114		size += sizeof(XButtonState);
115		break;
116	    case ValuatorClass:
117	    {
118		xValuatorState *v = (xValuatorState *) any;
119		if ((char *)any + sizeof(xValuatorState) > end)
120		    goto out;
121		size += (sizeof(XValuatorState) +
122			 (v->num_valuators * sizeof(int)));
123	    }
124		break;
125	    }
126	    any = (XInputClass *) ((char *)any + any->length);
127	}
128	state = (XDeviceState *) Xmalloc(size + sizeof(XDeviceState));
129	if (!state)
130            goto out;
131
132	state->device_id = dev->device_id;
133	state->num_classes = rep.num_classes;
134	state->data = (XInputClass *) (state + 1);
135
136	Any = state->data;
137	for (i = 0, any = (XInputClass *) data; i < (int)rep.num_classes; i++) {
138	    switch (any->class) {
139	    case KeyClass:
140	    {
141		xKeyState *k = (xKeyState *) any;
142		XKeyState *K = (XKeyState *) Any;
143
144		K->class = k->class;
145		K->length = sizeof(XKeyState);
146		K->num_keys = k->num_keys;
147		memcpy((char *)&K->keys[0], (char *)&k->keys[0], 32);
148		Any = (XInputClass *) (K + 1);
149	    }
150		break;
151	    case ButtonClass:
152	    {
153		xButtonState *b = (xButtonState *) any;
154		XButtonState *B = (XButtonState *) Any;
155
156		B->class = b->class;
157		B->length = sizeof(XButtonState);
158		B->num_buttons = b->num_buttons;
159		memcpy((char *)&B->buttons[0], (char *)&b->buttons[0], 32);
160		Any = (XInputClass *) (B + 1);
161	    }
162		break;
163	    case ValuatorClass:
164	    {
165		xValuatorState *v = (xValuatorState *) any;
166		XValuatorState *V = (XValuatorState *) Any;
167		CARD32 *valuators = (CARD32 *) (v + 1);
168
169		V->class = v->class;
170		V->length = sizeof(XValuatorState) +
171			    v->num_valuators * sizeof(int);
172		V->num_valuators = v->num_valuators;
173		V->mode = v->mode;
174		Any = (XInputClass *) (V + 1);
175		V->valuators = (int *)Any;
176		for (j = 0; j < (int)V->num_valuators; j++)
177		    *(V->valuators + j) = *valuators++;
178		Any = (XInputClass *) ((char *)Any +
179				       V->num_valuators * sizeof(int));
180	    }
181		break;
182	    }
183	    any = (XInputClass *) ((char *)any + any->length);
184	}
185    }
186out:
187    Xfree(data);
188
189    UnlockDisplay(dpy);
190    SyncHandle();
191    return (state);
192}
193
194void
195XFreeDeviceState(XDeviceState *list)
196{
197    XFree((char *)list);
198}
199