16747b715Smrg/*
26747b715Smrg * Copyright © 2009 Red Hat, Inc.
36747b715Smrg *
46747b715Smrg * Permission is hereby granted, free of charge, to any person obtaining a
56747b715Smrg * copy of this software and associated documentation files (the "Software"),
66747b715Smrg * to deal in the Software without restriction, including without limitation
76747b715Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
86747b715Smrg * and/or sell copies of the Software, and to permit persons to whom the
96747b715Smrg * Software is furnished to do so, subject to the following conditions:
106747b715Smrg *
116747b715Smrg * The above copyright notice and this permission notice (including the next
126747b715Smrg * paragraph) shall be included in all copies or substantial portions of the
136747b715Smrg * Software.
146747b715Smrg *
156747b715Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
166747b715Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
176747b715Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
186747b715Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
196747b715Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
206747b715Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
216747b715Smrg * DEALINGS IN THE SOFTWARE.
226747b715Smrg *
236747b715Smrg * Author: Peter Hutterer
246747b715Smrg */
256747b715Smrg
266747b715Smrg/***********************************************************************
276747b715Smrg *
286747b715Smrg * Request to grab or ungrab input device.
296747b715Smrg *
306747b715Smrg */
316747b715Smrg
326747b715Smrg#ifdef HAVE_DIX_CONFIG_H
336747b715Smrg#include <dix-config.h>
346747b715Smrg#endif
356747b715Smrg
36f7df2e56Smrg#include "inputstr.h"           /* DeviceIntPtr      */
37f7df2e56Smrg#include "windowstr.h"          /* window structure  */
386747b715Smrg#include <X11/extensions/XI2.h>
396747b715Smrg#include <X11/extensions/XI2proto.h>
406747b715Smrg
41f7df2e56Smrg#include "exglobals.h"          /* BadDevice */
426747b715Smrg#include "exevents.h"
436747b715Smrg#include "xigrabdev.h"
44f7df2e56Smrg#include "inpututils.h"
456747b715Smrg
467e31ba66Smrgint _X_COLD
476747b715SmrgSProcXIGrabDevice(ClientPtr client)
486747b715Smrg{
496747b715Smrg    REQUEST(xXIGrabDeviceReq);
500b0d8713Smrg    /*
510b0d8713Smrg     * Check here for at least the length of the struct we swap, then
520b0d8713Smrg     * let ProcXIGrabDevice check the full size after we swap mask_len.
530b0d8713Smrg     */
540b0d8713Smrg    REQUEST_AT_LEAST_SIZE(xXIGrabDeviceReq);
556747b715Smrg
56f7df2e56Smrg    swaps(&stuff->length);
57f7df2e56Smrg    swaps(&stuff->deviceid);
58f7df2e56Smrg    swapl(&stuff->grab_window);
59f7df2e56Smrg    swapl(&stuff->cursor);
60f7df2e56Smrg    swapl(&stuff->time);
61f7df2e56Smrg    swaps(&stuff->mask_len);
626747b715Smrg
636747b715Smrg    return ProcXIGrabDevice(client);
646747b715Smrg}
656747b715Smrg
666747b715Smrgint
676747b715SmrgProcXIGrabDevice(ClientPtr client)
686747b715Smrg{
696747b715Smrg    DeviceIntPtr dev;
706747b715Smrg    xXIGrabDeviceReply rep;
716747b715Smrg    int ret = Success;
726747b715Smrg    uint8_t status;
73f7df2e56Smrg    GrabMask mask = { 0 };
746747b715Smrg    int mask_len;
75f7df2e56Smrg    unsigned int keyboard_mode;
76f7df2e56Smrg    unsigned int pointer_mode;
776747b715Smrg
786747b715Smrg    REQUEST(xXIGrabDeviceReq);
790b0d8713Smrg    REQUEST_FIXED_SIZE(xXIGrabDeviceReq, ((size_t) stuff->mask_len) * 4);
806747b715Smrg
816747b715Smrg    ret = dixLookupDevice(&dev, stuff->deviceid, client, DixGrabAccess);
826747b715Smrg    if (ret != Success)
83f7df2e56Smrg        return ret;
846747b715Smrg
852f76b07dSmrg    if (!dev->enabled)
862f76b07dSmrg        return AlreadyGrabbed;
872f76b07dSmrg
886747b715Smrg    if (!IsMaster(dev))
896747b715Smrg        stuff->paired_device_mode = GrabModeAsync;
906747b715Smrg
91f7df2e56Smrg    if (IsKeyboardDevice(dev)) {
92f7df2e56Smrg        keyboard_mode = stuff->grab_mode;
93f7df2e56Smrg        pointer_mode = stuff->paired_device_mode;
94f7df2e56Smrg    }
95f7df2e56Smrg    else {
96f7df2e56Smrg        keyboard_mode = stuff->paired_device_mode;
97f7df2e56Smrg        pointer_mode = stuff->grab_mode;
98f7df2e56Smrg    }
99f7df2e56Smrg
100f7df2e56Smrg    if (XICheckInvalidMaskBits(client, (unsigned char *) &stuff[1],
1016747b715Smrg                               stuff->mask_len * 4) != Success)
1026747b715Smrg        return BadValue;
1036747b715Smrg
104f7df2e56Smrg    mask.xi2mask = xi2mask_new();
105f7df2e56Smrg    if (!mask.xi2mask)
106f7df2e56Smrg        return BadAlloc;
1076747b715Smrg
108f7df2e56Smrg    mask_len = min(xi2mask_mask_size(mask.xi2mask), stuff->mask_len * 4);
109f7df2e56Smrg    /* FIXME: I think the old code was broken here */
110f7df2e56Smrg    xi2mask_set_one_mask(mask.xi2mask, dev->id, (unsigned char *) &stuff[1],
111f7df2e56Smrg                         mask_len);
112f7df2e56Smrg
113f7df2e56Smrg    ret = GrabDevice(client, dev, pointer_mode,
114f7df2e56Smrg                     keyboard_mode,
1156747b715Smrg                     stuff->grab_window,
1166747b715Smrg                     stuff->owner_events,
1176747b715Smrg                     stuff->time,
118f7df2e56Smrg                     &mask, XI2, stuff->cursor, None /* confineTo */ ,
1196747b715Smrg                     &status);
1206747b715Smrg
121f7df2e56Smrg    xi2mask_free(&mask.xi2mask);
122f7df2e56Smrg
1236747b715Smrg    if (ret != Success)
1246747b715Smrg        return ret;
1256747b715Smrg
126f7df2e56Smrg    rep = (xXIGrabDeviceReply) {
127f7df2e56Smrg        .repType = X_Reply,
128f7df2e56Smrg        .RepType = X_XIGrabDevice,
129f7df2e56Smrg        .sequenceNumber = client->sequence,
130f7df2e56Smrg        .length = 0,
131f7df2e56Smrg        .status = status
132f7df2e56Smrg    };
1336747b715Smrg
1346747b715Smrg    WriteReplyToClient(client, sizeof(rep), &rep);
1356747b715Smrg    return ret;
1366747b715Smrg}
1376747b715Smrg
1387e31ba66Smrgint _X_COLD
1396747b715SmrgSProcXIUngrabDevice(ClientPtr client)
1406747b715Smrg{
1416747b715Smrg    REQUEST(xXIUngrabDeviceReq);
142f7df2e56Smrg    REQUEST_SIZE_MATCH(xXIUngrabDeviceReq);
1436747b715Smrg
144f7df2e56Smrg    swaps(&stuff->length);
145f7df2e56Smrg    swaps(&stuff->deviceid);
146f7df2e56Smrg    swapl(&stuff->time);
1476747b715Smrg
1486747b715Smrg    return ProcXIUngrabDevice(client);
1496747b715Smrg}
1506747b715Smrg
1516747b715Smrgint
1526747b715SmrgProcXIUngrabDevice(ClientPtr client)
1536747b715Smrg{
1546747b715Smrg    DeviceIntPtr dev;
1556747b715Smrg    GrabPtr grab;
1566747b715Smrg    int ret = Success;
1576747b715Smrg    TimeStamp time;
1586747b715Smrg
1596747b715Smrg    REQUEST(xXIUngrabDeviceReq);
1600b0d8713Smrg    REQUEST_SIZE_MATCH(xXIUngrabDeviceReq);
1616747b715Smrg
1626747b715Smrg    ret = dixLookupDevice(&dev, stuff->deviceid, client, DixGetAttrAccess);
1636747b715Smrg    if (ret != Success)
164f7df2e56Smrg        return ret;
1656747b715Smrg
1666747b715Smrg    grab = dev->deviceGrab.grab;
1676747b715Smrg
1686747b715Smrg    time = ClientTimeToServerTime(stuff->time);
1696747b715Smrg    if ((CompareTimeStamps(time, currentTime) != LATER) &&
170f7df2e56Smrg        (CompareTimeStamps(time, dev->deviceGrab.grabTime) != EARLIER) &&
171f7df2e56Smrg        (grab) && SameClient(grab, client) && grab->grabtype == XI2)
172f7df2e56Smrg        (*dev->deviceGrab.DeactivateGrab) (dev);
1736747b715Smrg
1746747b715Smrg    return Success;
1756747b715Smrg}
1766747b715Smrg
1777e31ba66Smrgvoid _X_COLD
178f7df2e56SmrgSRepXIGrabDevice(ClientPtr client, int size, xXIGrabDeviceReply * rep)
1796747b715Smrg{
180f7df2e56Smrg    swaps(&rep->sequenceNumber);
181f7df2e56Smrg    swapl(&rep->length);
182f7df2e56Smrg    WriteToClient(client, size, rep);
1836747b715Smrg}
184