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