xigrabdev.c revision f7df2e56
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 */
25
26/***********************************************************************
27 *
28 * Request to grab or ungrab input device.
29 *
30 */
31
32#ifdef HAVE_DIX_CONFIG_H
33#include <dix-config.h>
34#endif
35
36#include "inputstr.h"           /* DeviceIntPtr      */
37#include "windowstr.h"          /* window structure  */
38#include <X11/extensions/XI2.h>
39#include <X11/extensions/XI2proto.h>
40
41#include "exglobals.h"          /* BadDevice */
42#include "exevents.h"
43#include "xigrabdev.h"
44#include "inpututils.h"
45
46int
47SProcXIGrabDevice(ClientPtr client)
48{
49    REQUEST(xXIGrabDeviceReq);
50    /*
51     * Check here for at least the length of the struct we swap, then
52     * let ProcXIGrabDevice check the full size after we swap mask_len.
53     */
54    REQUEST_AT_LEAST_SIZE(xXIGrabDeviceReq);
55
56    swaps(&stuff->length);
57    swaps(&stuff->deviceid);
58    swapl(&stuff->grab_window);
59    swapl(&stuff->cursor);
60    swapl(&stuff->time);
61    swaps(&stuff->mask_len);
62
63    return ProcXIGrabDevice(client);
64}
65
66int
67ProcXIGrabDevice(ClientPtr client)
68{
69    DeviceIntPtr dev;
70    xXIGrabDeviceReply rep;
71    int ret = Success;
72    uint8_t status;
73    GrabMask mask = { 0 };
74    int mask_len;
75    unsigned int keyboard_mode;
76    unsigned int pointer_mode;
77
78    REQUEST(xXIGrabDeviceReq);
79    REQUEST_FIXED_SIZE(xXIGrabDeviceReq, ((size_t) stuff->mask_len) * 4);
80
81    ret = dixLookupDevice(&dev, stuff->deviceid, client, DixGrabAccess);
82    if (ret != Success)
83        return ret;
84
85    if (!IsMaster(dev))
86        stuff->paired_device_mode = GrabModeAsync;
87
88    if (IsKeyboardDevice(dev)) {
89        keyboard_mode = stuff->grab_mode;
90        pointer_mode = stuff->paired_device_mode;
91    }
92    else {
93        keyboard_mode = stuff->paired_device_mode;
94        pointer_mode = stuff->grab_mode;
95    }
96
97    if (XICheckInvalidMaskBits(client, (unsigned char *) &stuff[1],
98                               stuff->mask_len * 4) != Success)
99        return BadValue;
100
101    mask.xi2mask = xi2mask_new();
102    if (!mask.xi2mask)
103        return BadAlloc;
104
105    mask_len = min(xi2mask_mask_size(mask.xi2mask), stuff->mask_len * 4);
106    /* FIXME: I think the old code was broken here */
107    xi2mask_set_one_mask(mask.xi2mask, dev->id, (unsigned char *) &stuff[1],
108                         mask_len);
109
110    ret = GrabDevice(client, dev, pointer_mode,
111                     keyboard_mode,
112                     stuff->grab_window,
113                     stuff->owner_events,
114                     stuff->time,
115                     &mask, XI2, stuff->cursor, None /* confineTo */ ,
116                     &status);
117
118    xi2mask_free(&mask.xi2mask);
119
120    if (ret != Success)
121        return ret;
122
123    rep = (xXIGrabDeviceReply) {
124        .repType = X_Reply,
125        .RepType = X_XIGrabDevice,
126        .sequenceNumber = client->sequence,
127        .length = 0,
128        .status = status
129    };
130
131    WriteReplyToClient(client, sizeof(rep), &rep);
132    return ret;
133}
134
135int
136SProcXIUngrabDevice(ClientPtr client)
137{
138    REQUEST(xXIUngrabDeviceReq);
139    REQUEST_SIZE_MATCH(xXIUngrabDeviceReq);
140
141    swaps(&stuff->length);
142    swaps(&stuff->deviceid);
143    swapl(&stuff->time);
144
145    return ProcXIUngrabDevice(client);
146}
147
148int
149ProcXIUngrabDevice(ClientPtr client)
150{
151    DeviceIntPtr dev;
152    GrabPtr grab;
153    int ret = Success;
154    TimeStamp time;
155
156    REQUEST(xXIUngrabDeviceReq);
157    REQUEST_SIZE_MATCH(xXIUngrabDeviceReq);
158
159    ret = dixLookupDevice(&dev, stuff->deviceid, client, DixGetAttrAccess);
160    if (ret != Success)
161        return ret;
162
163    grab = dev->deviceGrab.grab;
164
165    time = ClientTimeToServerTime(stuff->time);
166    if ((CompareTimeStamps(time, currentTime) != LATER) &&
167        (CompareTimeStamps(time, dev->deviceGrab.grabTime) != EARLIER) &&
168        (grab) && SameClient(grab, client) && grab->grabtype == XI2)
169        (*dev->deviceGrab.DeactivateGrab) (dev);
170
171    return Success;
172}
173
174void
175SRepXIGrabDevice(ClientPtr client, int size, xXIGrabDeviceReply * rep)
176{
177    swaps(&rep->sequenceNumber);
178    swapl(&rep->length);
179    WriteToClient(client, size, rep);
180}
181