xigrabdev.c revision 6747b715
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
45int
46SProcXIGrabDevice(ClientPtr client)
47{
48    char n;
49
50    REQUEST(xXIGrabDeviceReq);
51
52    swaps(&stuff->length, n);
53    swaps(&stuff->deviceid, n);
54    swapl(&stuff->grab_window, n);
55    swapl(&stuff->cursor, n);
56    swapl(&stuff->time, n);
57    swaps(&stuff->mask_len, n);
58
59    return ProcXIGrabDevice(client);
60}
61
62int
63ProcXIGrabDevice(ClientPtr client)
64{
65    DeviceIntPtr dev;
66    xXIGrabDeviceReply rep;
67    int ret = Success;
68    uint8_t status;
69    GrabMask mask;
70    int mask_len;
71
72    REQUEST(xXIGrabDeviceReq);
73    REQUEST_AT_LEAST_SIZE(xXIGrabDeviceReq);
74
75    ret = dixLookupDevice(&dev, stuff->deviceid, client, DixGrabAccess);
76    if (ret != Success)
77	return ret;
78
79    if (!IsMaster(dev))
80        stuff->paired_device_mode = GrabModeAsync;
81
82    if (XICheckInvalidMaskBits((unsigned char*)&stuff[1],
83                               stuff->mask_len * 4) != Success)
84        return BadValue;
85
86    mask_len = min(sizeof(mask.xi2mask[stuff->deviceid]), stuff->mask_len * 4);
87    memset(mask.xi2mask, 0, sizeof(mask.xi2mask));
88    memcpy(mask.xi2mask, (char*)&stuff[1], mask_len);
89
90    ret = GrabDevice(client, dev, stuff->grab_mode,
91                     stuff->paired_device_mode,
92                     stuff->grab_window,
93                     stuff->owner_events,
94                     stuff->time,
95                     &mask,
96                     GRABTYPE_XI2,
97                     stuff->cursor,
98                     None /* confineTo */,
99                     &status);
100
101    if (ret != Success)
102        return ret;
103
104    rep.repType = X_Reply;
105    rep.RepType = X_XIGrabDevice;
106    rep.length = 0;
107    rep.sequenceNumber = client->sequence;
108    rep.status = status;
109
110
111    WriteReplyToClient(client, sizeof(rep), &rep);
112    return ret;
113}
114
115int
116SProcXIUngrabDevice(ClientPtr client)
117{
118    char n;
119
120    REQUEST(xXIUngrabDeviceReq);
121
122    swaps(&stuff->length, n);
123    swaps(&stuff->deviceid, n);
124    swapl(&stuff->time, n);
125
126    return ProcXIUngrabDevice(client);
127}
128
129int
130ProcXIUngrabDevice(ClientPtr client)
131{
132    DeviceIntPtr dev;
133    GrabPtr grab;
134    int ret = Success;
135    TimeStamp time;
136
137    REQUEST(xXIUngrabDeviceReq);
138
139    ret = dixLookupDevice(&dev, stuff->deviceid, client, DixGetAttrAccess);
140    if (ret != Success)
141	return ret;
142
143    grab = dev->deviceGrab.grab;
144
145    time = ClientTimeToServerTime(stuff->time);
146    if ((CompareTimeStamps(time, currentTime) != LATER) &&
147	(CompareTimeStamps(time, dev->deviceGrab.grabTime) != EARLIER) &&
148	(grab) && SameClient(grab, client) && grab->grabtype == GRABTYPE_XI2)
149	(*dev->deviceGrab.DeactivateGrab) (dev);
150
151    return Success;
152}
153
154void SRepXIGrabDevice(ClientPtr client, int size, xXIGrabDeviceReply * rep)
155{
156    char n;
157
158    swaps(&rep->sequenceNumber, n);
159    swapl(&rep->length, n);
160    WriteToClient(client, size, (char *)rep);
161}
162