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