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