summaryrefslogtreecommitdiff
path: root/Xi/xiselectev.c
blob: 1b6c47a87047801a5fc6430668579d6ab6aafec2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
 * Copyright 2008 Red Hat, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 * Author: Peter Hutterer
 */

#ifdef HAVE_DIX_CONFIG_H
#include <dix-config.h>
#endif


#include "dixstruct.h"
#include "windowstr.h"
#include "exglobals.h"
#include "exevents.h"
#include <X11/extensions/XI2proto.h>
#include "inpututils.h"

#include "xiselectev.h"

/**
 * Check the given mask (in len bytes) for invalid mask bits.
 * Invalid mask bits are any bits above XI2LastEvent.
 *
 * @return BadValue if at least one invalid bit is set or Success otherwise.
 */
int XICheckInvalidMaskBits(ClientPtr client, unsigned char *mask, int len)
{
    if (len >= XIMaskLen(XI2LASTEVENT))
    {
        int i;
        for (i = XI2LASTEVENT + 1; i < len * 8; i++)
        {
            if (BitIsOn(mask, i))
            {
                client->errorValue = i;
                return BadValue;
            }
        }
    }

    return Success;
}

int
SProcXISelectEvents(ClientPtr client)
{
    int i;
    xXIEventMask* evmask;

    REQUEST(xXISelectEventsReq);
    swaps(&stuff->length);
    REQUEST_AT_LEAST_SIZE(xXISelectEventsReq);
    swapl(&stuff->win);
    swaps(&stuff->num_masks);

    evmask = (xXIEventMask*)&stuff[1];
    for (i = 0; i < stuff->num_masks; i++)
    {
        swaps(&evmask->deviceid);
        swaps(&evmask->mask_len);
        evmask = (xXIEventMask*)(((char*)&evmask[1]) + evmask->mask_len * 4);
    }

    return (ProcXISelectEvents(client));
}

int
ProcXISelectEvents(ClientPtr client)
{
    int rc, num_masks;
    WindowPtr win;
    DeviceIntPtr dev;
    DeviceIntRec dummy;
    xXIEventMask *evmask;
    int *types = NULL;
    int len;

    REQUEST(xXISelectEventsReq);
    REQUEST_AT_LEAST_SIZE(xXISelectEventsReq);

    if (stuff->num_masks == 0)
        return BadValue;

    rc = dixLookupWindow(&win, stuff->win, client, DixReceiveAccess);
    if (rc != Success)
        return rc;

    len = sz_xXISelectEventsReq;

    /* check request validity */
    evmask = (xXIEventMask*)&stuff[1];
    num_masks = stuff->num_masks;
    while(num_masks--)
    {
        len += sizeof(xXIEventMask) + evmask->mask_len * 4;

        if (bytes_to_int32(len) > stuff->length)
            return BadLength;

        if (evmask->deviceid != XIAllDevices &&
            evmask->deviceid != XIAllMasterDevices)
            rc = dixLookupDevice(&dev, evmask->deviceid, client, DixUseAccess);
        else {
            /* XXX: XACE here? */
        }
        if (rc != Success)
            return rc;

        /* hierarchy event mask is not allowed on devices */
        if (evmask->deviceid != XIAllDevices && evmask->mask_len >= 1)
        {
            unsigned char *bits = (unsigned char*)&evmask[1];
            if (BitIsOn(bits, XI_HierarchyChanged))
            {
                client->errorValue = XI_HierarchyChanged;
                return BadValue;
            }
        }

        /* Raw events may only be selected on root windows */
        if (win->parent && evmask->mask_len >= 1)
        {
            unsigned char *bits = (unsigned char*)&evmask[1];
            if (BitIsOn(bits, XI_RawKeyPress) ||
                BitIsOn(bits, XI_RawKeyRelease) ||
                BitIsOn(bits, XI_RawButtonPress) ||
                BitIsOn(bits, XI_RawButtonRelease) ||
                BitIsOn(bits, XI_RawMotion) ||
                BitIsOn(bits, XI_RawTouchBegin) ||
                BitIsOn(bits, XI_RawTouchUpdate) ||
                BitIsOn(bits, XI_RawTouchEnd))
            {
                client->errorValue = XI_RawKeyPress;
                return BadValue;
            }
        }

        if (evmask->mask_len >= 1)
        {
            unsigned char *bits = (unsigned char*)&evmask[1];

            /* All three touch events must be selected at once */
            if ((BitIsOn(bits, XI_TouchBegin) ||
                 BitIsOn(bits, XI_TouchUpdate) ||
                 BitIsOn(bits, XI_TouchOwnership) ||
                 BitIsOn(bits, XI_TouchEnd)) &&
                (!BitIsOn(bits, XI_TouchBegin) ||
                 !BitIsOn(bits, XI_TouchUpdate) ||
                 !BitIsOn(bits, XI_TouchEnd)))
            {
                client->errorValue = XI_TouchBegin;
                return BadValue;
            }

            /* Only one client per window may select for touch events on the
             * same devices, including master devices.
             * XXX: This breaks if a device goes from floating to attached. */
            if (BitIsOn(bits, XI_TouchBegin))
            {
                OtherInputMasks *inputMasks = wOtherInputMasks(win);
                InputClients *iclient = NULL;
                if (inputMasks)
                    iclient = inputMasks->inputClients;
                for (; iclient; iclient = iclient->next)
                {
                    DeviceIntPtr dummy;

                    if (CLIENT_ID(iclient->resource) == client->index)
                        continue;

                    dixLookupDevice(&dummy, evmask->deviceid, serverClient, DixReadAccess);
                    if (!dummy)
                        return BadImplementation; /* this shouldn't happen */

                    if (xi2mask_isset(iclient->xi2mask, dummy, XI_TouchBegin))
                        return BadAccess;
                }
            }
        }

        if (XICheckInvalidMaskBits(client, (unsigned char*)&evmask[1],
                                   evmask->mask_len * 4) != Success)
            return BadValue;

        evmask = (xXIEventMask*)(((unsigned char*)evmask) + evmask->mask_len * 4);
        evmask++;
    }

    if (bytes_to_int32(len) != stuff->length)
        return BadLength;

    /* Set masks on window */
    evmask = (xXIEventMask*)&stuff[1];
    num_masks = stuff->num_masks;
    while(num_masks--)
    {
        if (evmask->deviceid == XIAllDevices ||
            evmask->deviceid == XIAllMasterDevices)
        {
            dummy.id = evmask->deviceid;
            dev = &dummy;
        } else
            dixLookupDevice(&dev, evmask->deviceid, client, DixUseAccess);
        if (XISetEventMask(dev, win, client, evmask->mask_len * 4,
                           (unsigned char*)&evmask[1]) != Success)
            return BadAlloc;
        evmask = (xXIEventMask*)(((unsigned char*)evmask) + evmask->mask_len * 4);
        evmask++;
    }

    RecalculateDeliverableEvents(win);

    free(types);
    return Success;
}


int
SProcXIGetSelectedEvents(ClientPtr client)
{
    REQUEST(xXIGetSelectedEventsReq);
    swaps(&stuff->length);
    REQUEST_SIZE_MATCH(xXIGetSelectedEventsReq);
    swapl(&stuff->win);

    return (ProcXIGetSelectedEvents(client));
}

int
ProcXIGetSelectedEvents(ClientPtr client)
{
    int rc, i;
    WindowPtr win;
    char *buffer = NULL;
    xXIGetSelectedEventsReply reply;
    OtherInputMasks *masks;
    InputClientsPtr others = NULL;
    xXIEventMask *evmask = NULL;
    DeviceIntPtr dev;

    REQUEST(xXIGetSelectedEventsReq);
    REQUEST_SIZE_MATCH(xXIGetSelectedEventsReq);

    rc = dixLookupWindow(&win, stuff->win, client, DixGetAttrAccess);
    if (rc != Success)
        return rc;

    reply.repType = X_Reply;
    reply.RepType = X_XIGetSelectedEvents;
    reply.length = 0;
    reply.sequenceNumber = client->sequence;
    reply.num_masks = 0;

    masks = wOtherInputMasks(win);
    if (masks)
    {
	for (others = wOtherInputMasks(win)->inputClients; others;
	     others = others->next) {
	    if (SameClient(others, client)) {
                break;
            }
        }
    }

    if (!others)
    {
        WriteReplyToClient(client, sizeof(xXIGetSelectedEventsReply), &reply);
        return Success;
    }

    buffer = calloc(MAXDEVICES, sizeof(xXIEventMask) + pad_to_int32(XI2MASKSIZE));
    if (!buffer)
        return BadAlloc;

    evmask = (xXIEventMask*)buffer;
    for (i = 0; i < MAXDEVICES; i++)
    {
        int j;
        const unsigned char *devmask = xi2mask_get_one_mask(others->xi2mask, i);

        if (i > 2)
        {
            rc = dixLookupDevice(&dev, i, client, DixGetAttrAccess);
            if (rc != Success)
                continue;
        }


        for (j = xi2mask_mask_size(others->xi2mask) - 1; j >= 0; j--)
        {
            if (devmask[j] != 0)
            {
                int mask_len = (j + 4)/4; /* j is an index, hence + 4, not + 3 */
                evmask->deviceid = i;
                evmask->mask_len = mask_len;
                reply.num_masks++;
                reply.length += sizeof(xXIEventMask)/4 + evmask->mask_len;

                if (client->swapped)
                {
                    swaps(&evmask->deviceid);
                    swaps(&evmask->mask_len);
                }

                memcpy(&evmask[1], devmask, j + 1);
                evmask = (xXIEventMask*)((char*)evmask +
                           sizeof(xXIEventMask) + mask_len * 4);
                break;
            }
        }
    }

    WriteReplyToClient(client, sizeof(xXIGetSelectedEventsReply), &reply);

    if (reply.num_masks)
        WriteToClient(client, reply.length * 4, buffer);

    free(buffer);
    return Success;
}

void SRepXIGetSelectedEvents(ClientPtr client,
                            int len, xXIGetSelectedEventsReply *rep)
{
    swaps(&rep->sequenceNumber);
    swapl(&rep->length);
    swaps(&rep->num_masks);
    WriteToClient(client, len, (char *)rep);
}