1 /*
2 * Copyright © 2009 Red Hat, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Author: Peter Hutterer
24 */
26 /***********************************************************************
27 *
28 * Request to grab or ungrab input device.
29 *
30 */
32 #ifdef HAVE_DIX_CONFIG_H
33 #include <dix-config.h>
34 #endif
36 #include "inputstr.h" /* DeviceIntPtr */
37 #include "windowstr.h" /* window structure */
38 #include <X11/extensions/XI2.h>
39 #include <X11/extensions/XI2proto.h>
41 #include "exglobals.h" /* BadDevice */
42 #include "exevents.h"
43 #include "xigrabdev.h"
44 #include "inpututils.h"
46 int
47 SProcXIGrabDevice(ClientPtr client)
48 {
49 char n;
51 REQUEST(xXIGrabDeviceReq);
53 swaps(&stuff->length, n);
54 swaps(&stuff->deviceid, n);
55 swapl(&stuff->grab_window, n);
56 swapl(&stuff->cursor, n);
57 swapl(&stuff->time, n);
58 swaps(&stuff->mask_len, n);
60 return ProcXIGrabDevice(client);
61 }
63 int
64 ProcXIGrabDevice(ClientPtr client)
65 {
66 DeviceIntPtr dev;
67 xXIGrabDeviceReply rep;
68 int ret = Success;
69 uint8_t status;
70 GrabMask mask = { 0 };
71 int mask_len;
73 REQUEST(xXIGrabDeviceReq);
74 REQUEST_AT_LEAST_SIZE(xXIGrabDeviceReq);
76 ret = dixLookupDevice(&dev, stuff->deviceid, client, DixGrabAccess);
77 if (ret != Success)
78 return ret;
80 if (!IsMaster(dev))
81 stuff->paired_device_mode = GrabModeAsync;
83 if (XICheckInvalidMaskBits(client, (unsigned char*)&stuff[1],
84 stuff->mask_len * 4) != Success)
85 return BadValue;
87 mask.xi2mask = xi2mask_new();
88 if (!mask.xi2mask)
89 return BadAlloc;
91 mask_len = min(xi2mask_mask_size(mask.xi2mask), stuff->mask_len * 4);
92 /* FIXME: I think the old code was broken here */
93 xi2mask_set_one_mask(mask.xi2mask, dev->id, (unsigned char*)&stuff[1], mask_len);
95 ret = GrabDevice(client, dev, stuff->grab_mode,
96 stuff->paired_device_mode,
97 stuff->grab_window,
98 stuff->owner_events,
99 stuff->time,
100 &mask,
101 XI2,
102 stuff->cursor,
103 None /* confineTo */,
104 &status);
106 xi2mask_free(&mask.xi2mask);
108 if (ret != Success)
109 return ret;
111 rep.repType = X_Reply;
112 rep.RepType = X_XIGrabDevice;
113 rep.length = 0;
114 rep.sequenceNumber = client->sequence;
115 rep.status = status;
118 WriteReplyToClient(client, sizeof(rep), &rep);
119 return ret;
120 }
122 int
123 SProcXIUngrabDevice(ClientPtr client)
124 {
125 char n;
127 REQUEST(xXIUngrabDeviceReq);
129 swaps(&stuff->length, n);
130 swaps(&stuff->deviceid, n);
131 swapl(&stuff->time, n);
133 return ProcXIUngrabDevice(client);
134 }
136 int
137 ProcXIUngrabDevice(ClientPtr client)
138 {
139 DeviceIntPtr dev;
140 GrabPtr grab;
141 int ret = Success;
142 TimeStamp time;
144 REQUEST(xXIUngrabDeviceReq);
146 ret = dixLookupDevice(&dev, stuff->deviceid, client, DixGetAttrAccess);
147 if (ret != Success)
148 return ret;
150 grab = dev->deviceGrab.grab;
152 time = ClientTimeToServerTime(stuff->time);
153 if ((CompareTimeStamps(time, currentTime) != LATER) &&
154 (CompareTimeStamps(time, dev->deviceGrab.grabTime) != EARLIER) &&
155 (grab) && SameClient(grab, client) && grab->grabtype == XI2)
156 (*dev->deviceGrab.DeactivateGrab) (dev);
158 return Success;
159 }
161 void SRepXIGrabDevice(ClientPtr client, int size, xXIGrabDeviceReply * rep)
162 {
163 char n;
165 swaps(&rep->sequenceNumber, n);
166 swapl(&rep->length, n);
167 WriteToClient(client, size, (char *)rep);
168 }