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 _X_COLD 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 (!dev->enabled) 86 return AlreadyGrabbed; 87 88 if (!IsMaster(dev)) 89 stuff->paired_device_mode = GrabModeAsync; 90 91 if (IsKeyboardDevice(dev)) { 92 keyboard_mode = stuff->grab_mode; 93 pointer_mode = stuff->paired_device_mode; 94 } 95 else { 96 keyboard_mode = stuff->paired_device_mode; 97 pointer_mode = stuff->grab_mode; 98 } 99 100 if (XICheckInvalidMaskBits(client, (unsigned char *) &stuff[1], 101 stuff->mask_len * 4) != Success) 102 return BadValue; 103 104 mask.xi2mask = xi2mask_new(); 105 if (!mask.xi2mask) 106 return BadAlloc; 107 108 mask_len = min(xi2mask_mask_size(mask.xi2mask), stuff->mask_len * 4); 109 /* FIXME: I think the old code was broken here */ 110 xi2mask_set_one_mask(mask.xi2mask, dev->id, (unsigned char *) &stuff[1], 111 mask_len); 112 113 ret = GrabDevice(client, dev, pointer_mode, 114 keyboard_mode, 115 stuff->grab_window, 116 stuff->owner_events, 117 stuff->time, 118 &mask, XI2, stuff->cursor, None /* confineTo */ , 119 &status); 120 121 xi2mask_free(&mask.xi2mask); 122 123 if (ret != Success) 124 return ret; 125 126 rep = (xXIGrabDeviceReply) { 127 .repType = X_Reply, 128 .RepType = X_XIGrabDevice, 129 .sequenceNumber = client->sequence, 130 .length = 0, 131 .status = status 132 }; 133 134 WriteReplyToClient(client, sizeof(rep), &rep); 135 return ret; 136} 137 138int _X_COLD 139SProcXIUngrabDevice(ClientPtr client) 140{ 141 REQUEST(xXIUngrabDeviceReq); 142 REQUEST_SIZE_MATCH(xXIUngrabDeviceReq); 143 144 swaps(&stuff->length); 145 swaps(&stuff->deviceid); 146 swapl(&stuff->time); 147 148 return ProcXIUngrabDevice(client); 149} 150 151int 152ProcXIUngrabDevice(ClientPtr client) 153{ 154 DeviceIntPtr dev; 155 GrabPtr grab; 156 int ret = Success; 157 TimeStamp time; 158 159 REQUEST(xXIUngrabDeviceReq); 160 REQUEST_SIZE_MATCH(xXIUngrabDeviceReq); 161 162 ret = dixLookupDevice(&dev, stuff->deviceid, client, DixGetAttrAccess); 163 if (ret != Success) 164 return ret; 165 166 grab = dev->deviceGrab.grab; 167 168 time = ClientTimeToServerTime(stuff->time); 169 if ((CompareTimeStamps(time, currentTime) != LATER) && 170 (CompareTimeStamps(time, dev->deviceGrab.grabTime) != EARLIER) && 171 (grab) && SameClient(grab, client) && grab->grabtype == XI2) 172 (*dev->deviceGrab.DeactivateGrab) (dev); 173 174 return Success; 175} 176 177void _X_COLD 178SRepXIGrabDevice(ClientPtr client, int size, xXIGrabDeviceReply * rep) 179{ 180 swaps(&rep->sequenceNumber); 181 swapl(&rep->length); 182 WriteToClient(client, size, rep); 183} 184