xwayland-input.c 87.3 KB
Newer Older
Kristian Høgsberg's avatar
Kristian Høgsberg committed
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
/*
 * Copyright © 2014 Intel Corporation
 * Copyright © 2008 Kristian Høgsberg
 *
 * Permission to use, copy, modify, distribute, and sell this software
 * and its documentation for any purpose is hereby granted without
 * fee, provided that the above copyright notice appear in all copies
 * and that both that copyright notice and this permission notice
 * appear in supporting documentation, and that the name of the
 * copyright holders not be used in advertising or publicity
 * pertaining to distribution of the software without specific,
 * written prior permission.  The copyright holders make no
 * representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied
 * warranty.
 *
 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 * SOFTWARE.
 */

27
#include <xwayland-config.h>
Kristian Høgsberg's avatar
Kristian Høgsberg committed
28
29
30

#include <linux/input.h>
#include <sys/mman.h>
31
32
33

#include <inputstr.h>
#include <exevents.h>
Kristian Høgsberg's avatar
Kristian Høgsberg committed
34
35
36
#include <xkbsrv.h>
#include <xserver-properties.h>
#include <inpututils.h>
37
#include <mi.h>
38
39
#include <mipointer.h>
#include <mipointrst.h>
40
#include <misc.h>
41

42
#include "xwayland-cursor.h"
43
#include "xwayland-input.h"
44
#include "xwayland-window.h"
45
#include "xwayland-screen.h"
46
47
48

#include "pointer-constraints-unstable-v1-client-protocol.h"
#include "relative-pointer-unstable-v1-client-protocol.h"
49
#include "tablet-unstable-v2-client-protocol.h"
50
#include "xwayland-keyboard-grab-unstable-v1-client-protocol.h"
51

52
53
54
55
56
57
struct axis_discrete_pending {
    struct xorg_list l;
    uint32_t axis;
    int32_t discrete;
};

58
59
60
61
62
struct sync_pending {
    struct xorg_list l;
    DeviceIntPtr pending_dev;
};

63
64
static DevPrivateKeyRec xwl_tablet_private_key;

65
66
67
68
69
70
71
72
73
74
75
76
static void
xwl_pointer_warp_emulator_handle_motion(struct xwl_pointer_warp_emulator *warp_emulator,
                                        double dx,
                                        double dy,
                                        double dx_unaccel,
                                        double dy_unaccel);
static void
xwl_pointer_warp_emulator_maybe_lock(struct xwl_pointer_warp_emulator *warp_emulator,
                                     struct xwl_window *xwl_window,
                                     SpritePtr sprite,
                                     int x, int y);

77
78
79
static Bool
xwl_seat_maybe_lock_on_hidden_cursor(struct xwl_seat *xwl_seat);

80
81
82
static void
xwl_seat_destroy_confined_pointer(struct xwl_seat *xwl_seat);

83
84
85
86
87
88
static void
init_tablet_manager_seat(struct xwl_screen *xwl_screen,
                         struct xwl_seat *xwl_seat);
static void
release_tablet_manager_seat(struct xwl_seat *xwl_seat);

Kristian Høgsberg's avatar
Kristian Høgsberg committed
89
90
91
92
93
94
static void
xwl_pointer_control(DeviceIntPtr device, PtrCtrl *ctrl)
{
    /* Nothing to do, dix handles all settings */
}

95
96
static Bool
init_pointer_buttons(DeviceIntPtr device)
Kristian Høgsberg's avatar
Kristian Høgsberg committed
97
98
99
100
101
{
#define NBUTTONS 10
    BYTE map[NBUTTONS + 1];
    int i = 0;
    Atom btn_labels[NBUTTONS] = { 0 };
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124

    for (i = 1; i <= NBUTTONS; i++)
        map[i] = i;

    btn_labels[0] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_LEFT);
    btn_labels[1] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_MIDDLE);
    btn_labels[2] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_RIGHT);
    btn_labels[3] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_WHEEL_UP);
    btn_labels[4] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_WHEEL_DOWN);
    btn_labels[5] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_HWHEEL_LEFT);
    btn_labels[6] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_HWHEEL_RIGHT);
    /* don't know about the rest */

    if (!InitButtonClassDeviceStruct(device, NBUTTONS, btn_labels, map))
        return FALSE;

    return TRUE;
}

static int
xwl_pointer_proc(DeviceIntPtr device, int what)
{
#define NAXES 4
Kristian Høgsberg's avatar
Kristian Høgsberg committed
125
126
127
128
129
130
    Atom axes_labels[NAXES] = { 0 };

    switch (what) {
    case DEVICE_INIT:
        device->public.on = FALSE;

131
132
        if (!init_pointer_buttons(device))
            return BadValue;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
133
134
135

        axes_labels[0] = XIGetKnownProperty(AXIS_LABEL_PROP_ABS_X);
        axes_labels[1] = XIGetKnownProperty(AXIS_LABEL_PROP_ABS_Y);
136
137
        axes_labels[2] = XIGetKnownProperty(AXIS_LABEL_PROP_REL_HWHEEL);
        axes_labels[3] = XIGetKnownProperty(AXIS_LABEL_PROP_REL_WHEEL);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
138

139
        if (!InitValuatorClassDeviceStruct(device, NAXES, axes_labels,
Kristian Høgsberg's avatar
Kristian Høgsberg committed
140
141
142
143
144
145
146
147
                                           GetMotionHistorySize(), Absolute))
            return BadValue;

        /* Valuators */
        InitValuatorAxisStruct(device, 0, axes_labels[0],
                               0, 0xFFFF, 10000, 0, 10000, Absolute);
        InitValuatorAxisStruct(device, 1, axes_labels[1],
                               0, 0xFFFF, 10000, 0, 10000, Absolute);
148
149
150
151
152
153
154
        InitValuatorAxisStruct(device, 2, axes_labels[2],
                               NO_AXIS_LIMITS, NO_AXIS_LIMITS, 0, 0, 0, Relative);
        InitValuatorAxisStruct(device, 3, axes_labels[3],
                               NO_AXIS_LIMITS, NO_AXIS_LIMITS, 0, 0, 0, Relative);

        SetScrollValuator(device, 2, SCROLL_TYPE_HORIZONTAL, 1.0, SCROLL_FLAG_NONE);
        SetScrollValuator(device, 3, SCROLL_TYPE_VERTICAL, 1.0, SCROLL_FLAG_PREFERRED);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176

        if (!InitPtrFeedbackClassDeviceStruct(device, xwl_pointer_control))
            return BadValue;

        return Success;

    case DEVICE_ON:
        device->public.on = TRUE;
        return Success;

    case DEVICE_OFF:
    case DEVICE_CLOSE:
        device->public.on = FALSE;
        return Success;
    }

    return BadMatch;

#undef NBUTTONS
#undef NAXES
}

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
static int
xwl_pointer_proc_relative(DeviceIntPtr device, int what)
{
#define NAXES 2
    Atom axes_labels[NAXES] = { 0 };

    switch (what) {
    case DEVICE_INIT:
        device->public.on = FALSE;

        axes_labels[0] = XIGetKnownProperty(AXIS_LABEL_PROP_REL_X);
        axes_labels[1] = XIGetKnownProperty(AXIS_LABEL_PROP_REL_Y);

        /*
         * We'll never send buttons, but XGetPointerMapping might in certain
         * situations make the client think we have no buttons.
         */
        if (!init_pointer_buttons(device))
            return BadValue;

        if (!InitValuatorClassDeviceStruct(device, NAXES, axes_labels,
                                           GetMotionHistorySize(), Relative))
            return BadValue;

        /* Valuators */
        InitValuatorAxisStruct(device, 0, axes_labels[0],
                               NO_AXIS_LIMITS, NO_AXIS_LIMITS, 1, 0, 1, Relative);
        InitValuatorAxisStruct(device, 1, axes_labels[1],
                               NO_AXIS_LIMITS, NO_AXIS_LIMITS, 1, 0, 1, Relative);

        if (!InitPtrFeedbackClassDeviceStruct(device, xwl_pointer_control))
            return BadValue;

        return Success;

    case DEVICE_ON:
        device->public.on = TRUE;
        return Success;

    case DEVICE_OFF:
    case DEVICE_CLOSE:
        device->public.on = FALSE;
        return Success;
    }

    return BadMatch;

#undef NAXES
}

Kristian Høgsberg's avatar
Kristian Høgsberg committed
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
static void
xwl_keyboard_control(DeviceIntPtr device, KeybdCtrl *ctrl)
{
}

static int
xwl_keyboard_proc(DeviceIntPtr device, int what)
{
    struct xwl_seat *xwl_seat = device->public.devicePrivate;
    int len;

    switch (what) {
    case DEVICE_INIT:
        device->public.on = FALSE;
        if (xwl_seat->keymap)
            len = strnlen(xwl_seat->keymap, xwl_seat->keymap_size);
        else
            len = 0;
        if (!InitKeyboardDeviceStructFromString(device, xwl_seat->keymap,
                                                len,
                                                NULL, xwl_keyboard_control))
            return BadValue;

        return Success;
    case DEVICE_ON:
        device->public.on = TRUE;
        return Success;

    case DEVICE_OFF:
    case DEVICE_CLOSE:
        device->public.on = FALSE;
        return Success;
    }

    return BadMatch;
}

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
static int
xwl_touch_proc(DeviceIntPtr device, int what)
{
#define NTOUCHPOINTS 20
#define NBUTTONS 1
#define NAXES 2
    Atom btn_labels[NBUTTONS] = { 0 };
    Atom axes_labels[NAXES] = { 0 };
    BYTE map[NBUTTONS + 1] = { 0 };

    switch (what) {
    case DEVICE_INIT:
        device->public.on = FALSE;

        axes_labels[0] = XIGetKnownProperty(AXIS_LABEL_PROP_ABS_MT_POSITION_X);
        axes_labels[1] = XIGetKnownProperty(AXIS_LABEL_PROP_ABS_MT_POSITION_Y);

        if (!InitValuatorClassDeviceStruct(device, NAXES, axes_labels,
                                           GetMotionHistorySize(), Absolute))
            return BadValue;

        if (!InitButtonClassDeviceStruct(device, NBUTTONS, btn_labels, map))
            return BadValue;

        if (!InitTouchClassDeviceStruct(device, NTOUCHPOINTS,
                                        XIDirectTouch, NAXES))
            return BadValue;

        /* Valuators */
        InitValuatorAxisStruct(device, 0, axes_labels[0],
294
                               0, 0xFFFF, 10000, 0, 10000, Absolute);
295
        InitValuatorAxisStruct(device, 1, axes_labels[1],
296
                               0, 0xFFFF, 10000, 0, 10000, Absolute);
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
        return Success;

    case DEVICE_ON:
        device->public.on = TRUE;
        return Success;

    case DEVICE_OFF:
    case DEVICE_CLOSE:
        device->public.on = FALSE;
        return Success;
    }

    return BadMatch;
#undef NAXES
#undef NBUTTONS
#undef NTOUCHPOINTS
}

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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
static int
xwl_tablet_proc(DeviceIntPtr device, int what)
{
#define NBUTTONS 9
#define NAXES 6
    Atom btn_labels[NBUTTONS] = { 0 };
    Atom axes_labels[NAXES] = { 0 };
    BYTE map[NBUTTONS + 1] = { 0 };
    int i;

    switch (what) {
    case DEVICE_INIT:
        device->public.on = FALSE;

        for (i = 1; i <= NBUTTONS; i++)
            map[i] = i;

        axes_labels[0] = XIGetKnownProperty(AXIS_LABEL_PROP_ABS_X);
        axes_labels[1] = XIGetKnownProperty(AXIS_LABEL_PROP_ABS_Y);
        axes_labels[2] = XIGetKnownProperty(AXIS_LABEL_PROP_ABS_PRESSURE);
        axes_labels[3] = XIGetKnownProperty(AXIS_LABEL_PROP_ABS_TILT_X);
        axes_labels[4] = XIGetKnownProperty(AXIS_LABEL_PROP_ABS_TILT_Y);
        axes_labels[5] = XIGetKnownProperty(AXIS_LABEL_PROP_ABS_WHEEL);

        if (!InitValuatorClassDeviceStruct(device, NAXES, axes_labels,
                                           GetMotionHistorySize(), Absolute))
            return BadValue;

        /* Valuators - match the xf86-input-wacom ranges */
        InitValuatorAxisStruct(device, 0, axes_labels[0],
                               0, 262143, 10000, 0, 10000, Absolute);
        InitValuatorAxisStruct(device, 1, axes_labels[1],
                               0, 262143, 10000, 0, 10000, Absolute);
        /* pressure */
        InitValuatorAxisStruct(device, 2, axes_labels[2],
                               0, 65535, 1, 0, 1, Absolute);
        /* tilt x */
        InitValuatorAxisStruct(device, 3, axes_labels[3],
                               -64, 63, 57, 0, 57, Absolute);
        /* tilt y */
        InitValuatorAxisStruct(device, 4, axes_labels[4],
                               -64, 63, 57, 0, 57, Absolute);
        /* abs wheel (airbrush) or rotation (artpen) */
        InitValuatorAxisStruct(device, 5, axes_labels[5],
                               -900, 899, 1, 0, 1, Absolute);

        if (!InitPtrFeedbackClassDeviceStruct(device, xwl_pointer_control))
            return BadValue;

        if (!InitButtonClassDeviceStruct(device, NBUTTONS, btn_labels, map))
            return BadValue;

        return Success;

    case DEVICE_ON:
        device->public.on = TRUE;
        return Success;

    case DEVICE_OFF:
    case DEVICE_CLOSE:
        device->public.on = FALSE;
        return Success;
    }

    return BadMatch;
#undef NAXES
#undef NBUTTONS
}

Kristian Høgsberg's avatar
Kristian Høgsberg committed
384
385
386
387
388
389
390
static void
pointer_handle_enter(void *data, struct wl_pointer *pointer,
                     uint32_t serial, struct wl_surface *surface,
                     wl_fixed_t sx_w, wl_fixed_t sy_w)
{
    struct xwl_seat *xwl_seat = data;
    DeviceIntPtr dev = xwl_seat->pointer;
391
    DeviceIntPtr master;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
392
393
394
    int i;
    int sx = wl_fixed_to_int(sx_w);
    int sy = wl_fixed_to_int(sy_w);
395
    int dx, dy;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
396
397
398
    ScreenPtr pScreen = xwl_seat->xwl_screen->screen;
    ValuatorMask mask;

399
400
401
402
403
404
405
406
407
    /* There's a race here where if we create and then immediately
     * destroy a surface, we might end up in a state where the Wayland
     * compositor sends us an event for a surface that doesn't exist.
     *
     * Don't process enter events in this case.
     */
    if (surface == NULL)
        return;

Kristian Høgsberg's avatar
Kristian Høgsberg committed
408
409
410
411
    xwl_seat->xwl_screen->serial = serial;
    xwl_seat->pointer_enter_serial = serial;

    xwl_seat->focus_window = wl_surface_get_user_data(surface);
412
413
    dx = xwl_seat->focus_window->window->drawable.x;
    dy = xwl_seat->focus_window->window->drawable.y;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
414

415
416
417
    /* We just entered a new xwindow, forget about the old last xwindow */
    xwl_seat->last_xwindow = NullWindow;

418
    master = GetMaster(dev, POINTER_OR_FLOAT);
419
    (*pScreen->SetCursorPosition) (dev, pScreen, dx + sx, dy + sy, TRUE);
420

421
    miPointerInvalidateSprite(master);
422
423

    CheckMotion(NULL, master);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
424
425
426
427
428
429
430
431
432
433
434
435
436
437

    /* Ideally, X clients shouldn't see these button releases.  When
     * the pointer leaves a window with buttons down, it means that
     * the wayland compositor has grabbed the pointer.  The button
     * release event is consumed by whatever grab in the compositor
     * and won't be sent to clients (the X server is a client).
     * However, we need to reset X's idea of which buttons are up and
     * down, and they're all up (by definition) when the pointer
     * enters a window.  We should figure out a way to swallow these
     * events, perhaps using an X grab whenever the pointer is not in
     * any X window, but for now just send the events. */
    valuator_mask_zero(&mask);
    for (i = 0; i < dev->button->numButtons; i++)
        if (BitIsOn(dev->button->down, i))
438
            QueuePointerEvents(dev, ButtonRelease, i, 0, &mask);
439

Alan Coopersmith's avatar
Alan Coopersmith committed
440
    /* The last cursor frame we committed before the pointer left one
441
442
443
     * of our surfaces might not have been shown. In that case we'll
     * have a cursor surface frame callback pending which we need to
     * clear so that we can continue submitting new cursor frames. */
444
445
446
    if (xwl_seat->cursor.frame_cb) {
        wl_callback_destroy(xwl_seat->cursor.frame_cb);
        xwl_seat->cursor.frame_cb = NULL;
447
448
        xwl_seat_set_cursor(xwl_seat);
    }
449
450
451
452
453
454

    if (xwl_seat->pointer_warp_emulator) {
        xwl_pointer_warp_emulator_maybe_lock(xwl_seat->pointer_warp_emulator,
                                             xwl_seat->focus_window,
                                             NULL, 0, 0);
    }
455
456
457
    else {
        xwl_seat_maybe_lock_on_hidden_cursor(xwl_seat);
    }
Kristian Høgsberg's avatar
Kristian Høgsberg committed
458
459
460
461
462
463
464
465
466
467
468
}

static void
pointer_handle_leave(void *data, struct wl_pointer *pointer,
                     uint32_t serial, struct wl_surface *surface)
{
    struct xwl_seat *xwl_seat = data;
    DeviceIntPtr dev = xwl_seat->pointer;

    xwl_seat->xwl_screen->serial = serial;

469
470
471
472
473
474
475
476
    /* The pointer has left a known xwindow, save it for a possible match
     * in sprite_check_lost_focus()
     */
    if (xwl_seat->focus_window) {
        xwl_seat->last_xwindow = xwl_seat->focus_window->window;
        xwl_seat->focus_window = NULL;
        CheckMotion(NULL, GetMaster(dev, POINTER_OR_FLOAT));
    }
Kristian Høgsberg's avatar
Kristian Høgsberg committed
477
478
}

479
static void
480
dispatch_pointer_motion_event(struct xwl_seat *xwl_seat)
481
482
483
{
    ValuatorMask mask;

484
    if (xwl_seat->pointer_warp_emulator &&
485
        xwl_seat->pending_pointer_event.has_relative) {
486
487
488
489
490
491
492
493
494
495
496
497
498
499
        double dx;
        double dy;
        double dx_unaccel;
        double dy_unaccel;

        dx = xwl_seat->pending_pointer_event.dx;
        dy = xwl_seat->pending_pointer_event.dy;
        dx_unaccel = xwl_seat->pending_pointer_event.dx_unaccel;
        dy_unaccel = xwl_seat->pending_pointer_event.dy_unaccel;
        xwl_pointer_warp_emulator_handle_motion(xwl_seat->pointer_warp_emulator,
                                                dx, dy,
                                                dx_unaccel, dy_unaccel);
    } else if (xwl_seat->pending_pointer_event.has_absolute ||
               xwl_seat->pending_pointer_event.has_relative) {
500
501
502
503
504
505
506
507
508
        int x;
        int y;

        if (xwl_seat->pending_pointer_event.has_absolute) {
            int sx = wl_fixed_to_int(xwl_seat->pending_pointer_event.x);
            int sy = wl_fixed_to_int(xwl_seat->pending_pointer_event.y);
            int dx = xwl_seat->focus_window->window->drawable.x;
            int dy = xwl_seat->focus_window->window->drawable.y;

509
510
511
512
513
            if (xwl_window_has_viewport_enabled(xwl_seat->focus_window)) {
                sx *= xwl_seat->focus_window->scale_x;
                sy *= xwl_seat->focus_window->scale_y;
            }

514
515
516
517
518
            x = dx + sx;
            y = dy + sy;
        } else {
            miPointerGetPosition(xwl_seat->pointer, &x, &y);
        }
519
520

        valuator_mask_zero(&mask);
521
522
523
524
525
526
527
528
529
530
531
532
        if (xwl_seat->pending_pointer_event.has_relative) {
            double dx_unaccel;
            double dy_unaccel;

            dx_unaccel = xwl_seat->pending_pointer_event.dx_unaccel;
            dy_unaccel = xwl_seat->pending_pointer_event.dy_unaccel;
            valuator_mask_set_absolute_unaccelerated(&mask, 0, x, dx_unaccel);
            valuator_mask_set_absolute_unaccelerated(&mask, 1, y, dy_unaccel);
        } else {
            valuator_mask_set(&mask, 0, x);
            valuator_mask_set(&mask, 1, y);
        }
533
534
535
536
537
538

        QueuePointerEvents(xwl_seat->pointer, MotionNotify, 0,
                           POINTER_ABSOLUTE | POINTER_SCREEN, &mask);
    }

    xwl_seat->pending_pointer_event.has_absolute = FALSE;
539
    xwl_seat->pending_pointer_event.has_relative = FALSE;
540
541
}

Kristian Høgsberg's avatar
Kristian Høgsberg committed
542
543
544
545
546
547
548
549
550
static void
pointer_handle_motion(void *data, struct wl_pointer *pointer,
                      uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
{
    struct xwl_seat *xwl_seat = data;

    if (!xwl_seat->focus_window)
        return;

551
552
553
    xwl_seat->pending_pointer_event.has_absolute = TRUE;
    xwl_seat->pending_pointer_event.x = sx_w;
    xwl_seat->pending_pointer_event.y = sy_w;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
554

555
    if (wl_proxy_get_version((struct wl_proxy *) xwl_seat->wl_pointer) < 5)
556
        dispatch_pointer_motion_event(xwl_seat);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
557
558
559
560
561
562
563
564
565
566
567
568
569
}

static void
pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
                      uint32_t time, uint32_t button, uint32_t state)
{
    struct xwl_seat *xwl_seat = data;
    int index;
    ValuatorMask mask;

    xwl_seat->xwl_screen->serial = serial;

    switch (button) {
570
571
572
    case BTN_LEFT:
        index = 1;
        break;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
573
574
575
576
577
578
579
    case BTN_MIDDLE:
        index = 2;
        break;
    case BTN_RIGHT:
        index = 3;
        break;
    default:
580
581
582
        /* Skip indexes 4-7: they are used for vertical and horizontal scroll.
           The rest of the buttons go in order: BTN_SIDE becomes 8, etc. */
        index = 8 + button - BTN_SIDE;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
583
584
585
586
587
588
589
590
591
592
593
594
595
        break;
    }

    valuator_mask_zero(&mask);
    QueuePointerEvents(xwl_seat->pointer,
                       state ? ButtonPress : ButtonRelease, index, 0, &mask);
}

static void
pointer_handle_axis(void *data, struct wl_pointer *pointer,
                    uint32_t time, uint32_t axis, wl_fixed_t value)
{
    struct xwl_seat *xwl_seat = data;
596
    int index;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
597
598
    const int divisor = 10;
    ValuatorMask mask;
599
600
    struct axis_discrete_pending *pending = NULL;
    struct axis_discrete_pending *iter;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
601
602
603

    switch (axis) {
    case WL_POINTER_AXIS_VERTICAL_SCROLL:
604
        index = 3;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
605
606
        break;
    case WL_POINTER_AXIS_HORIZONTAL_SCROLL:
607
        index = 2;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
608
609
610
611
612
        break;
    default:
        return;
    }

613
614
615
616
617
618
619
    xorg_list_for_each_entry(iter, &xwl_seat->axis_discrete_pending, l) {
        if (iter->axis == axis) {
            pending = iter;
            break;
        }
    }

Kristian Høgsberg's avatar
Kristian Høgsberg committed
620
    valuator_mask_zero(&mask);
621
622
623
624
625
626
627
628

    if (pending) {
        valuator_mask_set(&mask, index, pending->discrete);
        xorg_list_del(&pending->l);
        free(pending);
    } else {
        valuator_mask_set_double(&mask, index, wl_fixed_to_double(value) / divisor);
    }
629
    QueuePointerEvents(xwl_seat->pointer, MotionNotify, 0, POINTER_RELATIVE, &mask);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
630
631
}

632
633
634
635
636
static void
pointer_handle_frame(void *data, struct wl_pointer *wl_pointer)
{
    struct xwl_seat *xwl_seat = data;

637
638
639
    if (!xwl_seat->focus_window)
        return;

640
    dispatch_pointer_motion_event(xwl_seat);
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
}

static void
pointer_handle_axis_source(void *data, struct wl_pointer *wl_pointer, uint32_t axis_source)
{
}

static void
pointer_handle_axis_stop(void *data, struct wl_pointer *wl_pointer,
                         uint32_t time, uint32_t axis)
{
}

static void
pointer_handle_axis_discrete(void *data, struct wl_pointer *wl_pointer,
                             uint32_t axis, int32_t discrete)
{
658
659
660
661
662
663
664
665
666
667
    struct xwl_seat *xwl_seat = data;

    struct axis_discrete_pending *pending = malloc(sizeof *pending);
    if (!pending)
        return;

    pending->axis = axis;
    pending->discrete = discrete;

    xorg_list_add(&pending->l, &xwl_seat->axis_discrete_pending);
668
669
}

Kristian Høgsberg's avatar
Kristian Høgsberg committed
670
671
672
673
674
675
static const struct wl_pointer_listener pointer_listener = {
    pointer_handle_enter,
    pointer_handle_leave,
    pointer_handle_motion,
    pointer_handle_button,
    pointer_handle_axis,
676
677
678
679
    pointer_handle_frame,
    pointer_handle_axis_source,
    pointer_handle_axis_stop,
    pointer_handle_axis_discrete,
Kristian Høgsberg's avatar
Kristian Høgsberg committed
680
681
};

682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
static void
relative_pointer_handle_relative_motion(void *data,
                                        struct zwp_relative_pointer_v1 *zwp_relative_pointer_v1,
                                        uint32_t utime_hi,
                                        uint32_t utime_lo,
                                        wl_fixed_t dxf,
                                        wl_fixed_t dyf,
                                        wl_fixed_t dx_unaccelf,
                                        wl_fixed_t dy_unaccelf)
{
    struct xwl_seat *xwl_seat = data;

    xwl_seat->pending_pointer_event.has_relative = TRUE;
    xwl_seat->pending_pointer_event.dx = wl_fixed_to_double(dxf);
    xwl_seat->pending_pointer_event.dy = wl_fixed_to_double(dyf);
    xwl_seat->pending_pointer_event.dx_unaccel = wl_fixed_to_double(dx_unaccelf);
    xwl_seat->pending_pointer_event.dy_unaccel = wl_fixed_to_double(dy_unaccelf);

700
701
702
    if (!xwl_seat->focus_window)
        return;

703
704
705
706
707
708
709
710
    if (wl_proxy_get_version((struct wl_proxy *) xwl_seat->wl_pointer) < 5)
        dispatch_pointer_motion_event(xwl_seat);
}

static const struct zwp_relative_pointer_v1_listener relative_pointer_listener = {
    relative_pointer_handle_relative_motion,
};

Kristian Høgsberg's avatar
Kristian Høgsberg committed
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
static void
keyboard_handle_key(void *data, struct wl_keyboard *keyboard, uint32_t serial,
                    uint32_t time, uint32_t key, uint32_t state)
{
    struct xwl_seat *xwl_seat = data;
    uint32_t *k, *end;

    xwl_seat->xwl_screen->serial = serial;

    end = (uint32_t *) ((char *) xwl_seat->keys.data + xwl_seat->keys.size);
    for (k = xwl_seat->keys.data; k < end; k++) {
        if (*k == key)
            *k = *--end;
    }
    xwl_seat->keys.size = (char *) end - (char *) xwl_seat->keys.data;
    if (state) {
        k = wl_array_add(&xwl_seat->keys, sizeof *k);
        *k = key;
    }

    QueueKeyboardEvents(xwl_seat->keyboard,
732
                        state ? KeyPress : KeyRelease, key + 8);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
}

static void
keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
                       uint32_t format, int fd, uint32_t size)
{
    struct xwl_seat *xwl_seat = data;
    DeviceIntPtr master;
    XkbDescPtr xkb;
    XkbChangesRec changes = { 0 };

    if (xwl_seat->keymap)
        munmap(xwl_seat->keymap, xwl_seat->keymap_size);

    xwl_seat->keymap_size = size;
    xwl_seat->keymap = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
    if (xwl_seat->keymap == MAP_FAILED) {
        xwl_seat->keymap_size = 0;
        xwl_seat->keymap = NULL;
        goto out;
    }

    xkb = XkbCompileKeymapFromString(xwl_seat->keyboard, xwl_seat->keymap,
                                     strnlen(xwl_seat->keymap,
                                             xwl_seat->keymap_size));
    if (!xkb)
        goto out;

    XkbUpdateDescActions(xkb, xkb->min_key_code, XkbNumKeys(xkb), &changes);

    if (xwl_seat->keyboard->key)
        /* Keep the current controls */
        XkbCopyControls(xkb, xwl_seat->keyboard->key->xkbInfo->desc);

    XkbDeviceApplyKeymap(xwl_seat->keyboard, xkb);

    master = GetMaster(xwl_seat->keyboard, MASTER_KEYBOARD);
770
    if (master)
Kristian Høgsberg's avatar
Kristian Høgsberg committed
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
        XkbDeviceApplyKeymap(master, xkb);

    XkbFreeKeyboard(xkb, XkbAllComponentsMask, TRUE);

 out:
    close(fd);
}

static void
keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
                      uint32_t serial,
                      struct wl_surface *surface, struct wl_array *keys)
{
    struct xwl_seat *xwl_seat = data;
    uint32_t *k;

    xwl_seat->xwl_screen->serial = serial;
    xwl_seat->keyboard_focus = surface;

    wl_array_copy(&xwl_seat->keys, keys);
    wl_array_for_each(k, &xwl_seat->keys)
792
        QueueKeyboardEvents(xwl_seat->keyboard, EnterNotify, *k + 8);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
793
794
795
796
797
798
799
800
801
802
803
804
}

static void
keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
                      uint32_t serial, struct wl_surface *surface)
{
    struct xwl_seat *xwl_seat = data;
    uint32_t *k;

    xwl_seat->xwl_screen->serial = serial;

    wl_array_for_each(k, &xwl_seat->keys)
805
        QueueKeyboardEvents(xwl_seat->keyboard, LeaveNotify, *k + 8);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821

    xwl_seat->keyboard_focus = NULL;
}

static void
keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
                          uint32_t serial, uint32_t mods_depressed,
                          uint32_t mods_latched, uint32_t mods_locked,
                          uint32_t group)
{
    struct xwl_seat *xwl_seat = data;
    DeviceIntPtr dev;
    XkbStateRec old_state, *new_state;
    xkbStateNotify sn;
    CARD16 changed;

822
823
    mieqProcessInputEvents();

Kristian Høgsberg's avatar
Kristian Høgsberg committed
824
825
826
827
828
829
830
831
832
    for (dev = inputInfo.devices; dev; dev = dev->next) {
        if (dev != xwl_seat->keyboard &&
            dev != GetMaster(xwl_seat->keyboard, MASTER_KEYBOARD))
            continue;

        old_state = dev->key->xkbInfo->state;
        new_state = &dev->key->xkbInfo->state;

        new_state->locked_group = group & XkbAllGroupsMask;
833
834
835
836
        new_state->base_mods = mods_depressed & XkbAllModifiersMask;
        new_state->locked_mods = mods_locked & XkbAllModifiersMask;
        XkbLatchModifiers(dev, XkbAllModifiersMask,
                          mods_latched & XkbAllModifiersMask);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852

        XkbComputeDerivedState(dev->key->xkbInfo);

        changed = XkbStateChangedFlags(&old_state, new_state);
        if (!changed)
            continue;

        sn.keycode = 0;
        sn.eventType = 0;
        sn.requestMajor = XkbReqCode;
        sn.requestMinor = X_kbLatchLockState;   /* close enough */
        sn.changed = changed;
        XkbSendStateNotify(dev, &sn);
    }
}

853
854
855
856
857
858
static void
remove_sync_pending(DeviceIntPtr dev)
{
    struct xwl_seat *xwl_seat = dev->public.devicePrivate;
    struct sync_pending *p, *npd;

859
860
861
    if (!xwl_seat)
        return;

862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
    xorg_list_for_each_entry_safe(p, npd, &xwl_seat->sync_pending, l) {
        if (p->pending_dev == dev) {
            xorg_list_del(&xwl_seat->sync_pending);
            free (p);
            return;
        }
    }
}

static void
sync_callback(void *data, struct wl_callback *callback, uint32_t serial)
{
    DeviceIntPtr dev = (DeviceIntPtr) data;

    remove_sync_pending(dev);
    wl_callback_destroy(callback);
}

static const struct wl_callback_listener sync_listener = {
   sync_callback
};

static Bool
keyboard_check_repeat (DeviceIntPtr dev, XkbSrvInfoPtr xkbi, unsigned key)
{
    struct xwl_seat *xwl_seat = dev->public.devicePrivate;
888
    struct xwl_screen *xwl_screen;
889
890
891
    struct wl_callback *callback;
    struct sync_pending *p;

892
893
894
    if (!xwl_seat)
        return FALSE;

895
    /* Make sure we didn't miss a possible reply from the compositor */
896
    xwl_screen = xwl_seat->xwl_screen;
897
898
    xwl_sync_events (xwl_screen);

899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
    xorg_list_for_each_entry(p, &xwl_seat->sync_pending, l) {
        if (p->pending_dev == dev) {
            ErrorF("Key repeat discarded, Wayland compositor doesn't "
                   "seem to be processing events fast enough!\n");

            return FALSE;
        }
    }

    p = xnfalloc(sizeof(struct sync_pending));
    p->pending_dev = dev;
    callback = wl_display_sync (xwl_screen->display);
    xorg_list_add(&p->l, &xwl_seat->sync_pending);

    wl_callback_add_listener(callback, &sync_listener, dev);

    return TRUE;
}

918
919
920
921
922
923
924
925
926
static void
keyboard_handle_repeat_info (void *data, struct wl_keyboard *keyboard,
                             int32_t rate, int32_t delay)
{
    struct xwl_seat *xwl_seat = data;
    DeviceIntPtr dev;
    XkbControlsPtr ctrl;

    if (rate < 0 || delay < 0) {
927
928
        ErrorF("Wrong rate/delay: %d, %d\n", rate, delay);
        return;
929
930
931
932
933
934
935
    }

    for (dev = inputInfo.devices; dev; dev = dev->next) {
        if (dev != xwl_seat->keyboard &&
            dev != GetMaster(xwl_seat->keyboard, MASTER_KEYBOARD))
            continue;

936
        if (rate != 0) {
937
938
939
940
941
            ctrl = dev->key->xkbInfo->desc->ctrls;
            ctrl->repeat_delay = delay;
            /* rate is number of keys per second */
            ctrl->repeat_interval = 1000 / rate;

942
943
944
            XkbSetRepeatKeys(dev, -1, AutoRepeatModeOn);
        } else
            XkbSetRepeatKeys(dev, -1, AutoRepeatModeOff);
945
946
947
    }
}

Kristian Høgsberg's avatar
Kristian Høgsberg committed
948
949
950
951
952
953
static const struct wl_keyboard_listener keyboard_listener = {
    keyboard_handle_keymap,
    keyboard_handle_enter,
    keyboard_handle_leave,
    keyboard_handle_key,
    keyboard_handle_modifiers,
954
    keyboard_handle_repeat_info,
Kristian Høgsberg's avatar
Kristian Høgsberg committed
955
956
};

957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
static struct xwl_touch *
xwl_seat_lookup_touch(struct xwl_seat *xwl_seat, int32_t id)
{
    struct xwl_touch *xwl_touch, *next_xwl_touch;

    xorg_list_for_each_entry_safe(xwl_touch, next_xwl_touch,
                                  &xwl_seat->touches, link_touch) {
        if (xwl_touch->id == id)
            return xwl_touch;
    }

    return NULL;
}

static void
xwl_touch_send_event(struct xwl_touch *xwl_touch,
                     struct xwl_seat *xwl_seat, int type)
{
975
    double dx, dy, x, y;
976
977
978
979
980
    ValuatorMask mask;

    dx = xwl_touch->window->window->drawable.x;
    dy = xwl_touch->window->window->drawable.y;

981
982
983
    x = (dx + xwl_touch->x) * 0xFFFF / xwl_seat->xwl_screen->width;
    y = (dy + xwl_touch->y) * 0xFFFF / xwl_seat->xwl_screen->height;

984
    valuator_mask_zero(&mask);
985
986
    valuator_mask_set_double(&mask, 0, x);
    valuator_mask_set_double(&mask, 1, y);
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
    QueueTouchEvents(xwl_seat->touch, type, xwl_touch->id, 0, &mask);
}

static void
touch_handle_down(void *data, struct wl_touch *wl_touch,
                  uint32_t serial, uint32_t time,
                  struct wl_surface *surface,
                  int32_t id, wl_fixed_t sx_w, wl_fixed_t sy_w)
{
    struct xwl_seat *xwl_seat = data;
    struct xwl_touch *xwl_touch;

    if (surface == NULL)
        return;