XQueryDv.c revision 37eb1ca1
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 = NULL;
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        goto out;
86
87    rlen = rep.length << 2;
88    if (rlen > 0) {
89	data = Xmalloc(rlen);
90	if (!data) {
91	    _XEatData(dpy, (unsigned long)rlen);
92	    goto out;
93	}
94	_XRead(dpy, data, rlen);
95
96	for (i = 0, any = (XInputClass *) data; i < (int)rep.num_classes; i++) {
97	    switch (any->class) {
98	    case KeyClass:
99		size += sizeof(XKeyState);
100		break;
101	    case ButtonClass:
102		size += sizeof(XButtonState);
103		break;
104	    case ValuatorClass:
105	    {
106		xValuatorState *v = (xValuatorState *) any;
107		size += (sizeof(XValuatorState) +
108			 (v->num_valuators * sizeof(int)));
109	    }
110		break;
111	    }
112	    any = (XInputClass *) ((char *)any + any->length);
113	}
114	state = (XDeviceState *) Xmalloc(size + sizeof(XDeviceState));
115	if (!state)
116            goto out;
117
118	state->device_id = dev->device_id;
119	state->num_classes = rep.num_classes;
120	state->data = (XInputClass *) (state + 1);
121
122	Any = state->data;
123	for (i = 0, any = (XInputClass *) data; i < (int)rep.num_classes; i++) {
124	    switch (any->class) {
125	    case KeyClass:
126	    {
127		xKeyState *k = (xKeyState *) any;
128		XKeyState *K = (XKeyState *) Any;
129
130		K->class = k->class;
131		K->length = sizeof(XKeyState);
132		K->num_keys = k->num_keys;
133		memcpy((char *)&K->keys[0], (char *)&k->keys[0], 32);
134		Any = (XInputClass *) (K + 1);
135	    }
136		break;
137	    case ButtonClass:
138	    {
139		xButtonState *b = (xButtonState *) any;
140		XButtonState *B = (XButtonState *) Any;
141
142		B->class = b->class;
143		B->length = sizeof(XButtonState);
144		B->num_buttons = b->num_buttons;
145		memcpy((char *)&B->buttons[0], (char *)&b->buttons[0], 32);
146		Any = (XInputClass *) (B + 1);
147	    }
148		break;
149	    case ValuatorClass:
150	    {
151		xValuatorState *v = (xValuatorState *) any;
152		XValuatorState *V = (XValuatorState *) Any;
153		CARD32 *valuators = (CARD32 *) (v + 1);
154
155		V->class = v->class;
156		V->length = sizeof(XValuatorState) +
157			    v->num_valuators * sizeof(int);
158		V->num_valuators = v->num_valuators;
159		V->mode = v->mode;
160		Any = (XInputClass *) (V + 1);
161		V->valuators = (int *)Any;
162		for (j = 0; j < (int)V->num_valuators; j++)
163		    *(V->valuators + j) = *valuators++;
164		Any = (XInputClass *) ((char *)Any +
165				       V->num_valuators * sizeof(int));
166	    }
167		break;
168	    }
169	    any = (XInputClass *) ((char *)any + any->length);
170	}
171    }
172out:
173    Xfree(data);
174
175    UnlockDisplay(dpy);
176    SyncHandle();
177    return (state);
178}
179
180void
181XFreeDeviceState(XDeviceState *list)
182{
183    XFree((char *)list);
184}
185