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 _X_COLD
72SProcXGetDeviceControl(ClientPtr client)
73{
74    REQUEST(xGetDeviceControlReq);
75    swaps(&stuff->length);
76    REQUEST_SIZE_MATCH(xGetDeviceControlReq);
77    swaps(&stuff->control);
78    return (ProcXGetDeviceControl(client));
79}
80
81/***********************************************************************
82 *
83 * This procedure copies DeviceResolution data, swapping if necessary.
84 *
85 */
86
87static void
88CopySwapDeviceResolution(ClientPtr client, ValuatorClassPtr v, char *buf,
89                         int length)
90{
91    AxisInfoPtr a;
92    xDeviceResolutionState *r;
93    int i, *iptr;
94
95    r = (xDeviceResolutionState *) buf;
96    r->control = DEVICE_RESOLUTION;
97    r->length = length;
98    r->num_valuators = v->numAxes;
99    buf += sizeof(xDeviceResolutionState);
100    iptr = (int *) buf;
101    for (i = 0, a = v->axes; i < v->numAxes; i++, a++)
102        *iptr++ = a->resolution;
103    for (i = 0, a = v->axes; i < v->numAxes; i++, a++)
104        *iptr++ = a->min_resolution;
105    for (i = 0, a = v->axes; i < v->numAxes; i++, a++)
106        *iptr++ = a->max_resolution;
107    if (client->swapped) {
108        swaps(&r->control);
109        swaps(&r->length);
110        swapl(&r->num_valuators);
111        iptr = (int *) buf;
112        for (i = 0; i < (3 * v->numAxes); i++, iptr++) {
113            swapl(iptr);
114        }
115    }
116}
117
118static void
119CopySwapDeviceCore(ClientPtr client, DeviceIntPtr dev, char *buf)
120{
121    xDeviceCoreState *c = (xDeviceCoreState *) buf;
122
123    c->control = DEVICE_CORE;
124    c->length = sizeof(xDeviceCoreState);
125    c->status = dev->coreEvents;
126    c->iscore = (dev == inputInfo.keyboard || dev == inputInfo.pointer);
127
128    if (client->swapped) {
129        swaps(&c->control);
130        swaps(&c->length);
131    }
132}
133
134static void
135CopySwapDeviceEnable(ClientPtr client, DeviceIntPtr dev, char *buf)
136{
137    xDeviceEnableState *e = (xDeviceEnableState *) buf;
138
139    e->control = DEVICE_ENABLE;
140    e->length = sizeof(xDeviceEnableState);
141    e->enable = dev->enabled;
142
143    if (client->swapped) {
144        swaps(&e->control);
145        swaps(&e->length);
146    }
147}
148
149/***********************************************************************
150 *
151 * This procedure writes the reply for the xGetDeviceControl function,
152 * if the client and server have a different byte ordering.
153 *
154 */
155
156void _X_COLD
157SRepXGetDeviceControl(ClientPtr client, int size, xGetDeviceControlReply * rep)
158{
159    swaps(&rep->sequenceNumber);
160    swapl(&rep->length);
161    WriteToClient(client, size, rep);
162}
163
164/***********************************************************************
165 *
166 * Get the state of the specified device control.
167 *
168 */
169
170int
171ProcXGetDeviceControl(ClientPtr client)
172{
173    int rc, total_length = 0;
174    char *buf, *savbuf;
175    DeviceIntPtr dev;
176    xGetDeviceControlReply rep;
177
178    REQUEST(xGetDeviceControlReq);
179    REQUEST_SIZE_MATCH(xGetDeviceControlReq);
180
181    rc = dixLookupDevice(&dev, stuff->deviceid, client, DixGetAttrAccess);
182    if (rc != Success)
183        return rc;
184
185    rep = (xGetDeviceControlReply) {
186        .repType = X_Reply,
187        .RepType = X_GetDeviceControl,
188        .sequenceNumber = client->sequence,
189        .length = 0
190    };
191
192    switch (stuff->control) {
193    case DEVICE_RESOLUTION:
194        if (!dev->valuator)
195            return BadMatch;
196        total_length = sizeof(xDeviceResolutionState) +
197            (3 * sizeof(int) * dev->valuator->numAxes);
198        break;
199    case DEVICE_ABS_CALIB:
200    case DEVICE_ABS_AREA:
201        return BadMatch;
202    case DEVICE_CORE:
203        total_length = sizeof(xDeviceCoreState);
204        break;
205    case DEVICE_ENABLE:
206        total_length = sizeof(xDeviceEnableState);
207        break;
208    default:
209        return BadValue;
210    }
211
212    buf = (char *) malloc(total_length);
213    if (!buf)
214        return BadAlloc;
215    savbuf = buf;
216
217    switch (stuff->control) {
218    case DEVICE_RESOLUTION:
219        CopySwapDeviceResolution(client, dev->valuator, buf, total_length);
220        break;
221    case DEVICE_CORE:
222        CopySwapDeviceCore(client, dev, buf);
223        break;
224    case DEVICE_ENABLE:
225        CopySwapDeviceEnable(client, dev, buf);
226        break;
227    default:
228        break;
229    }
230
231    rep.length = bytes_to_int32(total_length);
232    WriteReplyToClient(client, sizeof(xGetDeviceControlReply), &rep);
233    WriteToClient(client, total_length, savbuf);
234    free(savbuf);
235    return Success;
236}
237