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