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 * Extension function to grab 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 "windowstr.h"	/* window structure  */
59#include <X11/extensions/XI.h>
60#include <X11/extensions/XIproto.h>
61#include "exglobals.h"
62#include "dixevents.h"	/* GrabDevice */
63
64#include "grabdev.h"
65
66extern XExtEventInfo EventInfo[];
67extern int ExtEventIndex;
68
69/***********************************************************************
70 *
71 * Swap the request if the requestor has a different byte order than us.
72 *
73 */
74
75int
76SProcXGrabDevice(ClientPtr client)
77{
78    char n;
79
80    REQUEST(xGrabDeviceReq);
81    swaps(&stuff->length, n);
82    REQUEST_AT_LEAST_SIZE(xGrabDeviceReq);
83    swapl(&stuff->grabWindow, n);
84    swapl(&stuff->time, n);
85    swaps(&stuff->event_count, n);
86
87    if (stuff->length != bytes_to_int32(sizeof(xGrabDeviceReq)) + stuff->event_count)
88       return BadLength;
89
90    SwapLongs((CARD32 *) (&stuff[1]), stuff->event_count);
91
92    return (ProcXGrabDevice(client));
93}
94
95/***********************************************************************
96 *
97 * Grab an extension device.
98 *
99 */
100
101int
102ProcXGrabDevice(ClientPtr client)
103{
104    int rc;
105    xGrabDeviceReply rep;
106    DeviceIntPtr dev;
107    GrabMask mask;
108    struct tmask tmp[EMASKSIZE];
109
110    REQUEST(xGrabDeviceReq);
111    REQUEST_AT_LEAST_SIZE(xGrabDeviceReq);
112
113    if (stuff->length != bytes_to_int32(sizeof(xGrabDeviceReq)) + stuff->event_count)
114	return BadLength;
115
116    rep.repType = X_Reply;
117    rep.RepType = X_GrabDevice;
118    rep.sequenceNumber = client->sequence;
119    rep.length = 0;
120
121    rc = dixLookupDevice(&dev, stuff->deviceid, client, DixGrabAccess);
122    if (rc != Success)
123	return rc;
124
125    if ((rc = CreateMaskFromList(client, (XEventClass *) & stuff[1],
126				 stuff->event_count, tmp, dev,
127				 X_GrabDevice)) != Success)
128	return rc;
129
130    mask.xi = tmp[stuff->deviceid].mask;
131
132    rc = GrabDevice(client, dev, stuff->other_devices_mode,
133                    stuff->this_device_mode, stuff->grabWindow,
134		    stuff->ownerEvents, stuff->time,
135		    &mask, GRABTYPE_XI, None, None,
136		    &rep.status);
137
138    if (rc != Success)
139	return rc;
140
141    WriteReplyToClient(client, sizeof(xGrabDeviceReply), &rep);
142    return Success;
143}
144
145/***********************************************************************
146 *
147 * This procedure creates an event mask from a list of XEventClasses.
148 *
149 * Procedure is as follows:
150 * An XEventClass is (deviceid << 8 | eventtype). For each entry in the list,
151 * get the device. Then run through all available event indices (those are
152 * set when XI starts up) and binary OR's the device's mask to whatever the
153 * event mask for the given event type was.
154 * If an error occurs, it is sent to the client. Errors are generated if
155 *  - if the device given in the event classs is invalid
156 *  - if the device in the class list is not the device given as parameter (no
157 *  error if parameter is NULL)
158 *
159 * mask has to be size EMASKSIZE and pre-allocated.
160 *
161 * @param client The client to send the error to (if one occurs)
162 * @param list List of event classes as sent from the client.
163 * @param count Number of elements in list.
164 * @param mask Preallocated mask (size EMASKSIZE).
165 * @param dev The device we're creating masks for.
166 * @param req The request we're processing. Used to fill in error fields.
167 */
168
169int
170CreateMaskFromList(ClientPtr client, XEventClass * list, int count,
171		   struct tmask *mask, DeviceIntPtr dev, int req)
172{
173    int rc, i, j;
174    int device;
175    DeviceIntPtr tdev;
176
177    for (i = 0; i < EMASKSIZE; i++) {
178	mask[i].mask = 0;
179	mask[i].dev = NULL;
180    }
181
182    for (i = 0; i < count; i++, list++) {
183	device = *list >> 8;
184	if (device > 255)
185	    return BadClass;
186
187	rc = dixLookupDevice(&tdev, device, client, DixUseAccess);
188	if (rc != BadDevice && rc != Success)
189	    return rc;
190	if (rc == BadDevice || (dev != NULL && tdev != dev))
191	    return BadClass;
192
193	for (j = 0; j < ExtEventIndex; j++)
194	    if (EventInfo[j].type == (*list & 0xff)) {
195		mask[device].mask |= EventInfo[j].mask;
196		mask[device].dev = (Pointer) tdev;
197		break;
198	    }
199    }
200    return Success;
201}
202
203/***********************************************************************
204 *
205 * This procedure writes the reply for the XGrabDevice function,
206 * if the client and server have a different byte ordering.
207 *
208 */
209
210void
211SRepXGrabDevice(ClientPtr client, int size, xGrabDeviceReply * rep)
212{
213    char n;
214
215    swaps(&rep->sequenceNumber, n);
216    swapl(&rep->length, n);
217    WriteToClient(client, size, (char *)rep);
218}
219