XQueryDv.c revision c27c18e8
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
53#include <X11/extensions/XI.h>
54#include <X11/extensions/XIproto.h>
55#include <X11/Xlibint.h>
56#include <X11/extensions/XInput.h>
57#include <X11/extensions/extutil.h>
58#include "XIint.h"
59
60XDeviceState *
61XQueryDeviceState(
62    register Display	*dpy,
63    XDevice		*dev)
64{
65    int i, j;
66    int rlen;
67    int size = 0;
68    xQueryDeviceStateReq *req;
69    xQueryDeviceStateReply rep;
70    XDeviceState *state = NULL;
71    XInputClass *any, *Any;
72    char *data;
73    XExtDisplayInfo *info = XInput_find_display(dpy);
74
75    LockDisplay(dpy);
76    if (_XiCheckExtInit(dpy, XInput_Initial_Release, info) == -1)
77	return ((XDeviceState *) NoSuchExtension);
78
79    GetReq(QueryDeviceState, req);
80    req->reqType = info->codes->major_opcode;
81    req->ReqType = X_QueryDeviceState;
82    req->deviceid = dev->device_id;
83
84    if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
85	UnlockDisplay(dpy);
86	SyncHandle();
87	return (XDeviceState *) NULL;
88    }
89
90    rlen = rep.length << 2;
91    if (rlen > 0) {
92	data = Xmalloc(rlen);
93	if (!data) {
94	    _XEatData(dpy, (unsigned long)rlen);
95	    UnlockDisplay(dpy);
96	    SyncHandle();
97	    return ((XDeviceState *) NULL);
98	}
99	_XRead(dpy, data, rlen);
100
101	for (i = 0, any = (XInputClass *) data; i < (int)rep.num_classes; i++) {
102	    switch (any->class) {
103	    case KeyClass:
104		size += sizeof(XKeyState);
105		break;
106	    case ButtonClass:
107		size += sizeof(XButtonState);
108		break;
109	    case ValuatorClass:
110	    {
111		xValuatorState *v = (xValuatorState *) any;
112		size += (sizeof(XValuatorState) +
113			 (v->num_valuators * sizeof(int)));
114	    }
115		break;
116	    }
117	    any = (XInputClass *) ((char *)any + any->length);
118	}
119	state = (XDeviceState *) Xmalloc(size + sizeof(XDeviceState));
120	if (!state) {
121	    UnlockDisplay(dpy);
122	    SyncHandle();
123	    return ((XDeviceState *) NULL);
124	}
125	state->device_id = dev->device_id;
126	state->num_classes = rep.num_classes;
127	state->data = (XInputClass *) (state + 1);
128
129	Any = state->data;
130	for (i = 0, any = (XInputClass *) data; i < (int)rep.num_classes; i++) {
131	    switch (any->class) {
132	    case KeyClass:
133	    {
134		xKeyState *k = (xKeyState *) any;
135		XKeyState *K = (XKeyState *) Any;
136
137		K->class = k->class;
138		K->length = sizeof(XKeyState);
139		K->num_keys = k->num_keys;
140		memcpy((char *)&K->keys[0], (char *)&k->keys[0], 32);
141		Any = (XInputClass *) (K + 1);
142	    }
143		break;
144	    case ButtonClass:
145	    {
146		xButtonState *b = (xButtonState *) any;
147		XButtonState *B = (XButtonState *) Any;
148
149		B->class = b->class;
150		B->length = sizeof(XButtonState);
151		B->num_buttons = b->num_buttons;
152		memcpy((char *)&B->buttons[0], (char *)&b->buttons[0], 32);
153		Any = (XInputClass *) (B + 1);
154	    }
155		break;
156	    case ValuatorClass:
157	    {
158		xValuatorState *v = (xValuatorState *) any;
159		XValuatorState *V = (XValuatorState *) Any;
160		CARD32 *valuators = (CARD32 *) (v + 1);
161
162		V->class = v->class;
163		V->length = sizeof(XValuatorState) +
164			    v->num_valuators * sizeof(int);
165		V->num_valuators = v->num_valuators;
166		V->mode = v->mode;
167		Any = (XInputClass *) (V + 1);
168		V->valuators = (int *)Any;
169		for (j = 0; j < (int)V->num_valuators; j++)
170		    *(V->valuators + j) = *valuators++;
171		Any = (XInputClass *) ((char *)Any +
172				       V->num_valuators * sizeof(int));
173	    }
174		break;
175	    }
176	    any = (XInputClass *) ((char *)any + any->length);
177	}
178	Xfree(data);
179    }
180
181    UnlockDisplay(dpy);
182    SyncHandle();
183    return (state);
184}
185
186void
187XFreeDeviceState(XDeviceState *list)
188{
189    XFree((char *)list);
190}
191