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 * XGetDeviceControl - get the Device control state of an extension device.
50 *
51 */
52
53#ifdef HAVE_CONFIG_H
54#include <config.h>
55#endif
56
57#include <X11/extensions/XI.h>
58#include <X11/extensions/XIproto.h>
59#include <X11/Xlibint.h>
60#include <X11/Xlib.h>
61#include <X11/extensions/XInput.h>
62#include <X11/extensions/extutil.h>
63#include "XIint.h"
64#include <limits.h>
65
66XDeviceControl *
67XGetDeviceControl(
68    register Display	*dpy,
69    XDevice		*dev,
70    int			 control)
71{
72    XDeviceControl *Device = NULL;
73    XDeviceControl *Sav = NULL;
74    xDeviceState *d = NULL;
75    xDeviceState *sav = NULL;
76    xGetDeviceControlReq *req;
77    xGetDeviceControlReply rep;
78    XExtDisplayInfo *info = XInput_find_display(dpy);
79
80    LockDisplay(dpy);
81    if (_XiCheckExtInit(dpy, XInput_Add_XChangeDeviceControl, info) == -1)
82        return NULL;
83
84    GetReq(GetDeviceControl, req);
85    req->reqType = info->codes->major_opcode;
86    req->ReqType = X_GetDeviceControl;
87    req->deviceid = dev->device_id;
88    req->control = control;
89
90    if (!_XReply(dpy, (xReply *) & rep, 0, xFalse))
91	goto out;
92
93    if (rep.length > 0) {
94	unsigned long nbytes;
95	size_t size = 0;
96	if (rep.length < (INT_MAX >> 2) &&
97	    (rep.length << 2) >= sizeof(xDeviceState)) {
98	    nbytes = (unsigned long) rep.length << 2;
99	    d = Xmalloc(nbytes);
100	}
101	if (!d) {
102	    _XEatDataWords(dpy, rep.length);
103	    goto out;
104	}
105	sav = d;
106	_XRead(dpy, (char *)d, nbytes);
107
108        /* In theory, we should just be able to use d->length to get the size.
109         * Turns out that a number of X servers (up to and including server
110         * 1.4) sent the wrong length value down the wire. So to not break
111         * apps that run against older servers, we have to calculate the size
112         * manually.
113         */
114	switch (d->control) {
115	case DEVICE_RESOLUTION:
116	{
117	    xDeviceResolutionState *r;
118	    size_t val_size;
119
120	    r = (xDeviceResolutionState *) d;
121	    if (sizeof(xDeviceResolutionState) > nbytes ||
122		r->num_valuators >= (INT_MAX / (3 * sizeof(int))))
123		goto out;
124	    val_size = 3 * sizeof(int) * r->num_valuators;
125	    if ((sizeof(xDeviceResolutionState) + val_size) > nbytes)
126		goto out;
127	    size = sizeof(XDeviceResolutionState) + val_size;
128	    break;
129	}
130        case DEVICE_ABS_CALIB:
131        {
132            if (sizeof(xDeviceAbsCalibState) > nbytes)
133                goto out;
134            size = sizeof(XDeviceAbsCalibState);
135            break;
136        }
137        case DEVICE_ABS_AREA:
138        {
139            if (sizeof(xDeviceAbsAreaState) > nbytes)
140                goto out;
141            size = sizeof(XDeviceAbsAreaState);
142            break;
143        }
144        case DEVICE_CORE:
145        {
146            if (sizeof(xDeviceCoreState) > nbytes)
147                goto out;
148            size = sizeof(XDeviceCoreState);
149            break;
150        }
151	default:
152	    if (d->length > nbytes)
153		goto out;
154	    size = d->length;
155	    break;
156	}
157
158	Device = Xmalloc(size);
159	if (!Device)
160	    goto out;
161
162	Sav = Device;
163
164	d = sav;
165	switch (control) {
166	case DEVICE_RESOLUTION:
167	{
168	    int *iptr, *iptr2;
169	    xDeviceResolutionState *r;
170	    XDeviceResolutionState *R;
171	    unsigned int i;
172
173	    r = (xDeviceResolutionState *) d;
174	    R = (XDeviceResolutionState *) Device;
175
176	    R->control = DEVICE_RESOLUTION;
177	    R->length = sizeof(XDeviceResolutionState);
178	    R->num_valuators = r->num_valuators;
179	    iptr = (int *)(R + 1);
180	    iptr2 = (int *)(r + 1);
181	    R->resolutions = iptr;
182	    R->min_resolutions = iptr + R->num_valuators;
183	    R->max_resolutions = iptr + (2 * R->num_valuators);
184	    for (i = 0; i < (3 * R->num_valuators); i++)
185		*iptr++ = *iptr2++;
186	    break;
187	}
188        case DEVICE_ABS_CALIB:
189        {
190            xDeviceAbsCalibState *c = (xDeviceAbsCalibState *) d;
191            XDeviceAbsCalibState *C = (XDeviceAbsCalibState *) Device;
192
193            C->control = DEVICE_ABS_CALIB;
194            C->length = sizeof(XDeviceAbsCalibState);
195            C->min_x = c->min_x;
196            C->max_x = c->max_x;
197            C->min_y = c->min_y;
198            C->max_y = c->max_y;
199            C->flip_x = c->flip_x;
200            C->flip_y = c->flip_y;
201            C->rotation = c->rotation;
202            C->button_threshold = c->button_threshold;
203
204            break;
205        }
206        case DEVICE_ABS_AREA:
207        {
208            xDeviceAbsAreaState *a = (xDeviceAbsAreaState *) d;
209            XDeviceAbsAreaState *A = (XDeviceAbsAreaState *) Device;
210
211            A->control = DEVICE_ABS_AREA;
212            A->length = sizeof(XDeviceAbsAreaState);
213            A->offset_x = a->offset_x;
214            A->offset_y = a->offset_y;
215            A->width = a->width;
216            A->height = a->height;
217            A->screen = a->screen;
218            A->following = a->following;
219
220            break;
221        }
222        case DEVICE_CORE:
223        {
224            xDeviceCoreState *c = (xDeviceCoreState *) d;
225            XDeviceCoreState *C = (XDeviceCoreState *) Device;
226
227            C->control = DEVICE_CORE;
228            C->length = sizeof(XDeviceCoreState);
229            C->status = c->status;
230            C->iscore = c->iscore;
231
232            break;
233        }
234        case DEVICE_ENABLE:
235        {
236            xDeviceEnableState *e = (xDeviceEnableState *) d;
237            XDeviceEnableState *E = (XDeviceEnableState *) Device;
238
239            E->control = DEVICE_ENABLE;
240            E->length = sizeof(E);
241            E->enable = e->enable;
242
243            break;
244        }
245	default:
246	    break;
247	}
248    }
249out:
250    XFree(sav);
251
252    UnlockDisplay(dpy);
253    SyncHandle();
254    return (Sav);
255}
256
257void
258XFreeDeviceControl(XDeviceControl *control)
259{
260    XFree(control);
261}
262