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 *  Get Device control attributes for an extension device.
50 *
51 */
52
53#ifdef HAVE_DIX_CONFIG_H
54#include <dix-config.h>
55#endif
56
57#include "inputstr.h"	/* DeviceIntPtr      */
58#include <X11/extensions/XI.h>
59#include <X11/extensions/XIproto.h>
60#include "exglobals.h"
61
62#include "getdctl.h"
63
64/***********************************************************************
65 *
66 * This procedure gets the control attributes for an extension device,
67 * for clients on machines with a different byte ordering than the server.
68 *
69 */
70
71int
72SProcXGetDeviceControl(ClientPtr client)
73{
74    char n;
75
76    REQUEST(xGetDeviceControlReq);
77    swaps(&stuff->length, n);
78    REQUEST_SIZE_MATCH(xGetDeviceControlReq);
79    swaps(&stuff->control, n);
80    return (ProcXGetDeviceControl(client));
81}
82
83/***********************************************************************
84 *
85 * This procedure copies DeviceResolution data, swapping if necessary.
86 *
87 */
88
89static void
90CopySwapDeviceResolution(ClientPtr client, ValuatorClassPtr v, char *buf,
91			 int length)
92{
93    char n;
94    AxisInfoPtr a;
95    xDeviceResolutionState *r;
96    int i, *iptr;
97
98    r = (xDeviceResolutionState *) buf;
99    r->control = DEVICE_RESOLUTION;
100    r->length = length;
101    r->num_valuators = v->numAxes;
102    buf += sizeof(xDeviceResolutionState);
103    iptr = (int *)buf;
104    for (i = 0, a = v->axes; i < v->numAxes; i++, a++)
105	*iptr++ = a->resolution;
106    for (i = 0, a = v->axes; i < v->numAxes; i++, a++)
107	*iptr++ = a->min_resolution;
108    for (i = 0, a = v->axes; i < v->numAxes; i++, a++)
109	*iptr++ = a->max_resolution;
110    if (client->swapped) {
111	swaps(&r->control, n);
112	swaps(&r->length, n);
113	swapl(&r->num_valuators, n);
114	iptr = (int *)buf;
115	for (i = 0; i < (3 * v->numAxes); i++, iptr++) {
116	    swapl(iptr, n);
117	}
118    }
119}
120
121static void CopySwapDeviceAbsCalib (ClientPtr client, AbsoluteClassPtr dts,
122                                char *buf)
123{
124    char n;
125    xDeviceAbsCalibState *calib = (xDeviceAbsCalibState *) buf;
126
127    calib->control = DEVICE_ABS_CALIB;
128    calib->length = sizeof(xDeviceAbsCalibState);
129    calib->min_x = dts->min_x;
130    calib->max_x = dts->max_x;
131    calib->min_y = dts->min_y;
132    calib->max_y = dts->max_y;
133    calib->flip_x = dts->flip_x;
134    calib->flip_y = dts->flip_y;
135    calib->rotation = dts->rotation;
136    calib->button_threshold = dts->button_threshold;
137
138    if (client->swapped) {
139        swaps(&calib->control, n);
140        swaps(&calib->length, n);
141        swapl(&calib->min_x, n);
142        swapl(&calib->max_x, n);
143        swapl(&calib->min_y, n);
144        swapl(&calib->max_y, n);
145        swapl(&calib->flip_x, n);
146        swapl(&calib->flip_y, n);
147        swapl(&calib->rotation, n);
148        swapl(&calib->button_threshold, n);
149    }
150}
151
152static void CopySwapDeviceAbsArea (ClientPtr client, AbsoluteClassPtr dts,
153                                char *buf)
154{
155    char n;
156    xDeviceAbsAreaState *area = (xDeviceAbsAreaState *) buf;
157
158    area->control = DEVICE_ABS_AREA;
159    area->length = sizeof(xDeviceAbsAreaState);
160    area->offset_x = dts->offset_x;
161    area->offset_y = dts->offset_y;
162    area->width = dts->width;
163    area->height = dts->height;
164    area->screen = dts->screen;
165    area->following = dts->following;
166
167    if (client->swapped) {
168        swaps(&area->control, n);
169        swaps(&area->length, n);
170        swapl(&area->offset_x, n);
171        swapl(&area->offset_y, n);
172        swapl(&area->width, n);
173        swapl(&area->height, n);
174        swapl(&area->screen, n);
175        swapl(&area->following, n);
176    }
177}
178
179static void CopySwapDeviceCore (ClientPtr client, DeviceIntPtr dev, char *buf)
180{
181    char n;
182    xDeviceCoreState *c = (xDeviceCoreState *) buf;
183
184    c->control = DEVICE_CORE;
185    c->length = sizeof(xDeviceCoreState);
186    c->status = dev->coreEvents;
187    c->iscore = (dev == inputInfo.keyboard || dev == inputInfo.pointer);
188
189    if (client->swapped) {
190        swaps(&c->control, n);
191        swaps(&c->length, n);
192        swaps(&c->status, n);
193    }
194}
195
196static void CopySwapDeviceEnable (ClientPtr client, DeviceIntPtr dev, char *buf)
197{
198    char n;
199    xDeviceEnableState *e = (xDeviceEnableState *) buf;
200
201    e->control = DEVICE_ENABLE;
202    e->length = sizeof(xDeviceEnableState);
203    e->enable = dev->enabled;
204
205    if (client->swapped) {
206        swaps(&e->control, n);
207        swaps(&e->length, n);
208        swaps(&e->enable, n);
209    }
210}
211
212/***********************************************************************
213 *
214 * This procedure writes the reply for the xGetDeviceControl function,
215 * if the client and server have a different byte ordering.
216 *
217 */
218
219void
220SRepXGetDeviceControl(ClientPtr client, int size, xGetDeviceControlReply * rep)
221{
222    char n;
223
224    swaps(&rep->sequenceNumber, n);
225    swapl(&rep->length, n);
226    WriteToClient(client, size, (char *)rep);
227}
228
229/***********************************************************************
230 *
231 * Get the state of the specified device control.
232 *
233 */
234
235int
236ProcXGetDeviceControl(ClientPtr client)
237{
238    int rc, total_length = 0;
239    char *buf, *savbuf;
240    DeviceIntPtr dev;
241    xGetDeviceControlReply rep;
242
243    REQUEST(xGetDeviceControlReq);
244    REQUEST_SIZE_MATCH(xGetDeviceControlReq);
245
246    rc = dixLookupDevice(&dev, stuff->deviceid, client, DixGetAttrAccess);
247    if (rc != Success)
248	return rc;
249
250    rep.repType = X_Reply;
251    rep.RepType = X_GetDeviceControl;
252    rep.length = 0;
253    rep.sequenceNumber = client->sequence;
254
255    switch (stuff->control) {
256    case DEVICE_RESOLUTION:
257	if (!dev->valuator)
258	    return BadMatch;
259	total_length = sizeof(xDeviceResolutionState) +
260	    (3 * sizeof(int) * dev->valuator->numAxes);
261	break;
262    case DEVICE_ABS_CALIB:
263        if (!dev->absolute)
264	    return BadMatch;
265
266        total_length = sizeof(xDeviceAbsCalibState);
267        break;
268    case DEVICE_ABS_AREA:
269        if (!dev->absolute)
270	    return BadMatch;
271
272        total_length = sizeof(xDeviceAbsAreaState);
273        break;
274    case DEVICE_CORE:
275        total_length = sizeof(xDeviceCoreState);
276        break;
277    case DEVICE_ENABLE:
278        total_length = sizeof(xDeviceEnableState);
279        break;
280    default:
281	return BadValue;
282    }
283
284    buf = (char *)malloc(total_length);
285    if (!buf)
286	return BadAlloc;
287    savbuf = buf;
288
289    switch (stuff->control) {
290    case DEVICE_RESOLUTION:
291	CopySwapDeviceResolution(client, dev->valuator, buf, total_length);
292	break;
293    case DEVICE_ABS_CALIB:
294        CopySwapDeviceAbsCalib(client, dev->absolute, buf);
295        break;
296    case DEVICE_ABS_AREA:
297        CopySwapDeviceAbsArea(client, dev->absolute, buf);
298        break;
299    case DEVICE_CORE:
300        CopySwapDeviceCore(client, dev, buf);
301        break;
302    case DEVICE_ENABLE:
303        CopySwapDeviceEnable(client, dev, buf);
304        break;
305    default:
306	break;
307    }
308
309    rep.length = bytes_to_int32(total_length);
310    WriteReplyToClient(client, sizeof(xGetDeviceControlReply), &rep);
311    WriteToClient(client, total_length, savbuf);
312    free(savbuf);
313    return Success;
314}
315