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