evdev.c 90.9 KB
Newer Older
1
/*
Adam Jackson's avatar
Adam Jackson committed
2
 * Copyright © 2004-2008 Red Hat, Inc.
3
4
5
6
7
8
9
10
11
12
13
14
 *
 * 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 Red Hat
 * not be used in advertising or publicity pertaining to distribution
 * of the software without specific, written prior permission.  Red
 * Hat makes no representations about the suitability of this software
 * for any purpose.  It is provided "as is" without express or implied
 * warranty.
 *
Adam Jackson's avatar
Adam Jackson committed
15
 * THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
Adam Jackson's avatar
Adam Jackson committed
17
 * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18
19
20
21
22
 * 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.
 *
Adam Jackson's avatar
Adam Jackson committed
23
24
25
 * Authors:
 *	Kristian Høgsberg (krh@redhat.com)
 *	Adam Jackson (ajax@redhat.com)
26
 *	Peter Hutterer (peter.hutterer@redhat.com)
27
 *	Oliver McFadden (oliver.mcfadden@nokia.com)
28
 *	Thomas H.P. Andersen (phomes@gmail.com)
29
30
 */

31
32
33
34
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

35
#include "evdev.h"
36
#include "axis_labels.h"
37

38
#include <X11/keysym.h>
39
#include <X11/extensions/XI.h>
40

41
#include <linux/version.h>
42
#include <sys/stat.h>
43
#include <libudev.h>
44
#include <unistd.h>
45
46
#include <errno.h>
#include <fcntl.h>
47
48
49
50

#include <xf86.h>
#include <xf86Xinput.h>
#include <exevents.h>
Julien Cristau's avatar
Julien Cristau committed
51
#include <xorgVersion.h>
52
#include <xkbsrv.h>
53

54
55
#include <X11/Xatom.h>
#include <evdev-properties.h>
Peter Hutterer's avatar
Peter Hutterer committed
56
#include <xserver-properties.h>
57
#include <mtdev-plumbing.h>
58

59
60
61
62
#ifndef XI_PROP_PRODUCT_ID
#define XI_PROP_PRODUCT_ID "Device Product ID"
#endif

63
64
65
66
#ifndef XI_PROP_VIRTUAL_DEVICE
#define XI_PROP_VIRTUAL_DEVICE "Virtual Device"
#endif

67
68
69
/* removed from server, purge when dropping support for server 1.10 */
#define XI86_SEND_DRAG_EVENTS   0x08

70
71
72
73
74
75
76
77
78
79
#define ArrayLength(a) (sizeof(a) / (sizeof((a)[0])))

#define MIN_KEYCODE 8

#define CAPSFLAG	1
#define NUMFLAG		2
#define SCROLLFLAG	4
#define MODEFLAG	8
#define COMPOSEFLAG	16

80
81
82
83
84
85
86
87
#ifndef ABS_MT_SLOT
#define ABS_MT_SLOT 0x2f
#endif

#ifndef ABS_MT_TRACKING_ID
#define ABS_MT_TRACKING_ID 0x39
#endif

88
89
90
91
#ifndef XI86_SERVER_FD
#define XI86_SERVER_FD 0x20
#endif

92
93
94
95
96
97
98
99
100
101
102
103
/* Any of those triggers a proximity event */
static int proximity_bits[] = {
        BTN_TOOL_PEN,
        BTN_TOOL_RUBBER,
        BTN_TOOL_BRUSH,
        BTN_TOOL_PENCIL,
        BTN_TOOL_AIRBRUSH,
        BTN_TOOL_FINGER,
        BTN_TOOL_MOUSE,
        BTN_TOOL_LENS,
};

104
static int EvdevOn(DeviceIntPtr);
105
static int EvdevCache(InputInfoPtr pInfo);
106
static void EvdevKbdCtrl(DeviceIntPtr device, KeybdCtrl *ctrl);
107
static int EvdevSwitchMode(ClientPtr client, DeviceIntPtr device, int mode);
108
static BOOL EvdevGrabDevice(InputInfoPtr pInfo, int grab, int ungrab);
109
static void EvdevSetCalibration(InputInfoPtr pInfo, int num_calibration, int calibration[4]);
110
static int EvdevOpenDevice(InputInfoPtr pInfo);
111
static void EvdevCloseDevice(InputInfoPtr pInfo);
112

113
static void EvdevInitAxesLabels(EvdevPtr pEvdev, int mode, int natoms, Atom *atoms);
114
static void EvdevInitOneAxisLabel(EvdevPtr pEvdev, int mapped_axis,
115
                                  const char **labels, int label_idx, Atom *atoms);
116
static void EvdevInitButtonLabels(EvdevPtr pEvdev, int natoms, Atom *atoms);
117
118
static void EvdevInitProperty(DeviceIntPtr dev);
static int EvdevSetProperty(DeviceIntPtr dev, Atom atom,
119
                            XIPropertyValuePtr val, BOOL checkonly);
120
static Atom prop_product_id;
121
122
123
124
125
static Atom prop_invert;
static Atom prop_calibration;
static Atom prop_swap;
static Atom prop_axis_label;
static Atom prop_btn_label;
126
static Atom prop_device;
127
static Atom prop_virtual;
128
static Atom prop_scroll_dist;
129

130
131
132
133
static int EvdevSwitchMode(ClientPtr client, DeviceIntPtr device, int mode)
{
    InputInfoPtr pInfo;
    EvdevPtr pEvdev;
134
    int val;
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149

    pInfo = device->public.devicePrivate;
    pEvdev = pInfo->private;

    if (pEvdev->flags & EVDEV_RELATIVE_EVENTS)
    {
        if (mode == Relative)
            return Success;
        else
            return XI_BadMode;
    }

    switch (mode) {
        case Absolute:
            pEvdev->flags &= ~EVDEV_RELATIVE_MODE;
150
151
152
153
154
            if (valuator_mask_fetch(pEvdev->old_vals, 0, &val))
                valuator_mask_set(pEvdev->abs_vals, 0, val);
            if (valuator_mask_fetch(pEvdev->old_vals, 1, &val))
                valuator_mask_set(pEvdev->abs_vals, 1, val);
            valuator_mask_zero(pEvdev->old_vals);
155
156
157
158
            break;

        case Relative:
            pEvdev->flags |= EVDEV_RELATIVE_MODE;
159
160
161
162
163
164
            if (valuator_mask_fetch(pEvdev->abs_vals, 0, &val))
                valuator_mask_set(pEvdev->old_vals, 0, val);
            if (valuator_mask_fetch(pEvdev->abs_vals, 1, &val))
                valuator_mask_set(pEvdev->old_vals, 1, val);
            valuator_mask_unset(pEvdev->abs_vals, 0);
            valuator_mask_unset(pEvdev->abs_vals, 1);
165
166
167
168
169
170
171
172
173
            break;

        default:
            return XI_BadMode;
    }

    return Success;
}

174
175
static inline int EvdevBitIsSet(const unsigned long *array, int bit)
{
176
    return !!(array[bit / LONG_BITS] & (1LL << (bit % LONG_BITS)));
177
178
179
180
181
182
183
}

static inline void EvdevSetBit(unsigned long *array, int bit)
{
    array[bit / LONG_BITS] |= (1LL << (bit % LONG_BITS));
}

184
185
186
187
188
189
190
static int
EvdevGetMajorMinor(InputInfoPtr pInfo)
{
    struct stat st;

    if (fstat(pInfo->fd, &st) == -1)
    {
191
192
        xf86IDrvMsg(pInfo, X_ERROR, "stat failed (%s). cannot check for duplicates.\n",
                    strerror(errno));
193
194
195
196
197
198
199
200
201
202
203
204
205
206
        return 0;
    }

    return st.st_rdev;
}

/**
 * Return TRUE if one of the devices we know about has the same min/maj
 * number.
 */
static BOOL
EvdevIsDuplicate(InputInfoPtr pInfo)
{
    EvdevPtr pEvdev = pInfo->private;
207
    InputInfoPtr d;
208

209
    nt_list_for_each_entry(d, xf86FirstLocalDevice(), next)
210
    {
211
        EvdevPtr e;
212

213
214
        if (strcmp(d->drv->driverName, "evdev") != 0)
            continue;
215

216
217
218
219
220
        e = (EvdevPtr)d->private;
        if (e != pEvdev &&
            e->min_maj &&
            e->min_maj == pEvdev->min_maj)
            return TRUE;
221
    }
222
223

    return FALSE;
224
225
}

226
227
228
229
230
231
232
233
234
235
236
237
238
static BOOL
EvdevDeviceIsVirtual(const char* devicenode)
{
    struct udev *udev = NULL;
    struct udev_device *device = NULL;
    struct stat st;
    int rc = FALSE;
    const char *devpath;

    udev = udev_new();
    if (!udev)
        goto out;

239
240
241
    if (stat(devicenode, &st) == -1)
        goto out;

242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
    device = udev_device_new_from_devnum(udev, 'c', st.st_rdev);

    if (!device)
        goto out;


    devpath = udev_device_get_devpath(device);
    if (!devpath)
        goto out;

    if (strstr(devpath, "LNXSYSTM"))
        rc = TRUE;

out:
    udev_device_unref(device);
    udev_unref(udev);
    return rc;
}

261

262
263
264
265
266
267
268
static EventQueuePtr
EvdevNextInQueue(InputInfoPtr pInfo)
{
    EvdevPtr pEvdev = pInfo->private;

    if (pEvdev->num_queue >= EVDEV_MAXQUEUE)
    {
269
        LogMessageVerbSigSafe(X_WARNING, 0, "dropping event due to full queue!\n");
270
271
272
273
274
275
276
        return NULL;
    }

    pEvdev->num_queue++;
    return &pEvdev->queue[pEvdev->num_queue - 1];
}

277
void
278
EvdevQueueKbdEvent(InputInfoPtr pInfo, struct input_event *ev, int value)
279
{
280
    int code = ev->code + MIN_KEYCODE;
281
    EventQueuePtr pQueue;
282

283
    /* Filter all repeated events from device.
284
       We'll do softrepeat in the server, but only since 1.6 */
285
286
    if (value == 2)
        return;
287

288
    if ((pQueue = EvdevNextInQueue(pInfo)))
289
    {
290
        pQueue->type = EV_QUEUE_KEY;
291
        pQueue->detail.key = code;
292
        pQueue->val = value;
293
294
295
    }
}

296
void
297
EvdevQueueButtonEvent(InputInfoPtr pInfo, int button, int value)
298
299
300
{
    EventQueuePtr pQueue;

301
    if ((pQueue = EvdevNextInQueue(pInfo)))
302
    {
303
        pQueue->type = EV_QUEUE_BTN;
304
        pQueue->detail.key = button;
305
        pQueue->val = value;
306
    }
Peter Hutterer's avatar
Peter Hutterer committed
307
}
308

Peter Hutterer's avatar
Peter Hutterer committed
309
310
311
312
313
314
315
void
EvdevQueueProximityEvent(InputInfoPtr pInfo, int value)
{
    EventQueuePtr pQueue;
    if ((pQueue = EvdevNextInQueue(pInfo)))
    {
        pQueue->type = EV_QUEUE_PROXIMITY;
316
        pQueue->detail.key = 0;
Peter Hutterer's avatar
Peter Hutterer committed
317
318
        pQueue->val = value;
    }
319
320
}

321
322
323
324
325
326
327
328
329
330
331
332
333
334
void
EvdevQueueTouchEvent(InputInfoPtr pInfo, unsigned int touch, ValuatorMask *mask,
                     uint16_t evtype)
{
    EventQueuePtr pQueue;
    if ((pQueue = EvdevNextInQueue(pInfo)))
    {
        pQueue->type = EV_QUEUE_TOUCH;
        pQueue->detail.touch = touch;
        valuator_mask_copy(pQueue->touchMask, mask);
        pQueue->val = evtype;
    }
}

335
336
337
338
339
/**
 * Post button event right here, right now.
 * Interface for MB emulation since these need to post immediately.
 */
void
340
EvdevPostButtonEvent(InputInfoPtr pInfo, int button, enum ButtonAction act)
341
{
342
343
    xf86PostButtonEvent(pInfo->dev, Relative, button,
                        (act == BUTTON_PRESS) ? 1 : 0, 0, 0);
344
345
}

346
void
347
EvdevQueueButtonClicks(InputInfoPtr pInfo, int button, int count)
348
349
350
351
{
    int i;

    for (i = 0; i < count; i++) {
352
353
        EvdevQueueButtonEvent(pInfo, button, 1);
        EvdevQueueButtonEvent(pInfo, button, 0);
354
    }
355
356
}

357
358
359
360
361
362
363
364
365
366
367
368
static void
EvdevSwapAbsValuators(EvdevPtr pEvdev, ValuatorMask *mask)
{
    int i;
    int swapped_isset[2] = {0, 0};
    int swapped_values[2];

    if (!pEvdev->swap_axes)
        return;

    for(i = 0; i <= 1; i++) {
        if (valuator_mask_isset(mask, i)) {
Peter Hutterer's avatar
Peter Hutterer committed
369
370
371
372
373
            const struct input_absinfo *abs1 =
                libevdev_get_abs_info(pEvdev->dev, i);
            const struct input_absinfo *abs2 =
                libevdev_get_abs_info(pEvdev->dev, 1 - i);

374
375
376
            swapped_isset[1 - i] = 1;
            swapped_values[1 - i] =
                xf86ScaleAxis(valuator_mask_get(mask, i),
Peter Hutterer's avatar
Peter Hutterer committed
377
378
                              abs2->maximum, abs2->minimum,
                              abs1->maximum, abs1->minimum);
379
380
381
382
383
384
385
386
387
388
389
        }
    }

    for (i = 0; i <= 1; i++) {
        if (swapped_isset[i])
            valuator_mask_set(mask, i, swapped_values[i]);
        else
            valuator_mask_unset(mask, i);
    }
}

390
391
392
393
394
395
static void
EvdevApplyCalibration(EvdevPtr pEvdev, ValuatorMask *mask)
{
    int i;

    for (i = 0; i <= 1; i++) {
Peter Hutterer's avatar
Peter Hutterer committed
396
        const struct input_absinfo *abs;
397
398
399
400
401
402
403
404
        int val;
        int calib_min;
        int calib_max;

        if (!valuator_mask_isset(mask, i))
            continue;

        val = valuator_mask_get(mask, i);
Peter Hutterer's avatar
Peter Hutterer committed
405
        abs = libevdev_get_abs_info(pEvdev->dev, i);
406
407
408
409
410
411
412
413
414
415

        if (i == 0) {
            calib_min = pEvdev->calibration.min_x;
            calib_max = pEvdev->calibration.max_x;
        } else {
            calib_min = pEvdev->calibration.min_y;
            calib_max = pEvdev->calibration.max_y;
        }

        if (pEvdev->flags & EVDEV_CALIBRATED)
Peter Hutterer's avatar
Peter Hutterer committed
416
417
            val = xf86ScaleAxis(val, abs->maximum, abs->minimum,
                                calib_max, calib_min);
418
419

        if ((i == 0 && pEvdev->invert_x) || (i == 1 && pEvdev->invert_y))
Peter Hutterer's avatar
Peter Hutterer committed
420
            val = (abs->maximum - val + abs->minimum);
421
422
423
424
425

        valuator_mask_set(mask, i, val);
    }
}

426
/**
427
 * Take the valuators and process them accordingly.
428
 */
429
static void
430
EvdevProcessValuators(InputInfoPtr pInfo)
431
432
{
    EvdevPtr pEvdev = pInfo->private;
433
434
435
436
437
438
439
440
    int val;

    if (pEvdev->abs_vals) {
            if (valuator_mask_fetch(pEvdev->abs_vals, 0, &val))
                    valuator_mask_set(pEvdev->old_vals, 0, val);
            if (valuator_mask_fetch(pEvdev->abs_vals, 1, &val))
                    valuator_mask_set(pEvdev->old_vals, 1, val);
    }
Éric Brunet's avatar
Éric Brunet committed
441
442

    /* Apply transformations on relative coordinates */
443
    if (pEvdev->rel_queued) {
444
        double deltaX = 0, deltaY = 0;
445

Éric Brunet's avatar
Éric Brunet committed
446
        if (valuator_mask_isset(pEvdev->rel_vals, REL_X))
447
            deltaX = valuator_mask_get_double(pEvdev->rel_vals, REL_X);
Éric Brunet's avatar
Éric Brunet committed
448
        if (valuator_mask_isset(pEvdev->rel_vals, REL_Y))
449
            deltaY = valuator_mask_get_double(pEvdev->rel_vals, REL_Y);
450
451

        if (pEvdev->swap_axes) {
452
            double tmp = deltaX;
Éric Brunet's avatar
Éric Brunet committed
453
454
            deltaX = deltaY;
            deltaY = tmp;
455
        }
Éric Brunet's avatar
Éric Brunet committed
456

457
458
459
460
461
        if (pEvdev->resolution > 0) {
            deltaX *= DEFAULT_MOUSE_DPI / pEvdev->resolution;
            deltaY *= DEFAULT_MOUSE_DPI / pEvdev->resolution;
        }

462
        if (pEvdev->invert_x)
Éric Brunet's avatar
Éric Brunet committed
463
            deltaX *= -1;
464
        if (pEvdev->invert_y)
Éric Brunet's avatar
Éric Brunet committed
465
            deltaY *= -1;
466

Éric Brunet's avatar
Éric Brunet committed
467
        if (deltaX)
468
            valuator_mask_set_double(pEvdev->rel_vals, REL_X, deltaX);
Éric Brunet's avatar
Éric Brunet committed
469
470
        else
            valuator_mask_unset(pEvdev->rel_vals, REL_X);
Peter Hutterer's avatar
Peter Hutterer committed
471

Éric Brunet's avatar
Éric Brunet committed
472
        if (deltaY)
473
            valuator_mask_set_double(pEvdev->rel_vals, REL_Y, deltaY);
Éric Brunet's avatar
Éric Brunet committed
474
475
476
477
        else
            valuator_mask_unset(pEvdev->rel_vals, REL_Y);

        Evdev3BEmuProcessRelMotion(pInfo, deltaX, deltaY);
Peter Hutterer's avatar
Peter Hutterer committed
478

479
480
    }
    /*
481
     * Some devices only generate valid abs coords when BTN_TOOL_PEN is
482
     * pressed.  On wacom tablets, this means that the pen is in
483
     * proximity of the tablet.  After the pen is removed, BTN_TOOL_PEN is
484
     * released, and a (0, 0) absolute event is generated.  Checking
485
     * pEvdev->in_proximity here lets us ignore that event.  pEvdev is
Peter Hutterer's avatar
Peter Hutterer committed
486
     * initialized to 1 so devices that don't use this scheme still
487
488
     * just works.
     */
489
    else if (pEvdev->abs_queued && pEvdev->in_proximity) {
490
491
492
        EvdevSwapAbsValuators(pEvdev, pEvdev->abs_vals);
        EvdevApplyCalibration(pEvdev, pEvdev->abs_vals);
        Evdev3BEmuProcessAbsMotion(pInfo, pEvdev->abs_vals);
493
494
495
    }
}

Peter Hutterer's avatar
Peter Hutterer committed
496
497
498
499
500
static void
EvdevProcessProximityEvent(InputInfoPtr pInfo, struct input_event *ev)
{
    EvdevPtr pEvdev = pInfo->private;

501
502
503
    if (!pEvdev->use_proximity)
        return;

504
    pEvdev->prox_queued = 1;
Peter Hutterer's avatar
Peter Hutterer committed
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529

    EvdevQueueProximityEvent(pInfo, ev->value);
}

/**
 * Proximity handling is rather weird because of tablet-specific issues.
 * Some tablets, notably Wacoms, send a 0/0 coordinate in the same EV_SYN as
 * the out-of-proximity notify. We need to ignore those, hence we only
 * actually post valuator events when we're in proximity.
 *
 * Other tablets send the x/y coordinates, then EV_SYN, then the proximity
 * event. For those, we need to remember x/y to post it when the proximity
 * comes.
 *
 * If we're not in proximity and we get valuator events, remember that, they
 * won't be posted though. If we move into proximity without valuators, use
 * the last ones we got and let the rest of the code post them.
 */
static int
EvdevProcessProximityState(InputInfoPtr pInfo)
{
    EvdevPtr pEvdev = pInfo->private;
    int prox_state = 0;
    int i;

530
531
532
533
    /* Does this device have any proximity axes? */
    if (!pEvdev->prox)
        return 0;

Peter Hutterer's avatar
Peter Hutterer committed
534
    /* no proximity change in the queue */
535
    if (!pEvdev->prox_queued)
Peter Hutterer's avatar
Peter Hutterer committed
536
    {
537
        if (pEvdev->abs_queued && !pEvdev->in_proximity)
538
539
            for (i = 0; i < valuator_mask_size(pEvdev->abs_vals); i++)
                if (valuator_mask_isset(pEvdev->abs_vals, i))
540
                    valuator_mask_set(pEvdev->prox, i,
541
                                      valuator_mask_get(pEvdev->abs_vals, i));
Peter Hutterer's avatar
Peter Hutterer committed
542
543
544
545
546
547
548
549
550
551
552
553
        return 0;
    }

    for (i = 0; i < pEvdev->num_queue; i++)
    {
        if (pEvdev->queue[i].type == EV_QUEUE_PROXIMITY)
        {
            prox_state = pEvdev->queue[i].val;
            break;
        }
    }

554
555
556
557
558
559
560
561
562
563
    /* Wacom's last frame resets all values to 0, including x/y.
       Skip over this. */
    if (prox_state == 0) {
        int v;
        if (valuator_mask_fetch(pEvdev->abs_vals, 0, &v) && v == 0)
            valuator_mask_unset(pEvdev->abs_vals, 0);
        if (valuator_mask_fetch(pEvdev->abs_vals, 1, &v) && v == 0)
            valuator_mask_unset(pEvdev->abs_vals, 1);
    }

564
565
    if ((prox_state && !pEvdev->in_proximity) ||
        (!prox_state && pEvdev->in_proximity))
Peter Hutterer's avatar
Peter Hutterer committed
566
567
568
    {
        /* We're about to go into/out of proximity but have no abs events
         * within the EV_SYN. Use the last coordinates we have. */
569
        for (i = 0; i < valuator_mask_size(pEvdev->prox); i++)
570
            if (!valuator_mask_isset(pEvdev->abs_vals, i) &&
571
                valuator_mask_isset(pEvdev->prox, i))
572
                valuator_mask_set(pEvdev->abs_vals, i,
573
574
575
                                  valuator_mask_get(pEvdev->prox, i));
        valuator_mask_zero(pEvdev->prox);

576
        pEvdev->abs_queued = valuator_mask_size(pEvdev->abs_vals);
Peter Hutterer's avatar
Peter Hutterer committed
577
578
    }

579
    pEvdev->in_proximity = prox_state;
Peter Hutterer's avatar
Peter Hutterer committed
580
581
582
    return 1;
}

583
584
585
586
587
/**
 * Take a button input event and process it accordingly.
 */
static void
EvdevProcessButtonEvent(InputInfoPtr pInfo, struct input_event *ev)
588
{
589
    unsigned int button;
590
    int value;
591
    EvdevPtr pEvdev = pInfo->private;
592

593
594
    button = EvdevUtilButtonEventToButtonNumber(pEvdev, ev->code);

595
596
597
    /* Get the signed value, earlier kernels had this as unsigned */
    value = ev->value;

598
599
600
    /* Handle drag lock */
    if (EvdevDragLockFilterEvent(pInfo, button, value))
        return;
601

602
603
    if (EvdevWheelEmuFilterButton(pInfo, button, value))
        return;
604

605
606
607
608
    if (EvdevMBEmuFilterEvent(pInfo, button, value))
        return;

    if (button)
609
        EvdevQueueButtonEvent(pInfo, button, value);
610
    else
611
        EvdevQueueKbdEvent(pInfo, ev, value);
612
613
614
615
616
617
618
619
}

/**
 * Take the relative motion input event and process it accordingly.
 */
static void
EvdevProcessRelativeMotionEvent(InputInfoPtr pInfo, struct input_event *ev)
{
620
    int value;
621
    EvdevPtr pEvdev = pInfo->private;
622
    int map;
623
624
625
626
627
628

    /* Get the signed value, earlier kernels had this as unsigned */
    value = ev->value;

    switch (ev->code) {
        default:
629
            /* Ignore EV_REL events if we never set up for them. */
630
631
632
            if (!(pEvdev->flags & EVDEV_RELATIVE_EVENTS) &&
                    ev->code != REL_WHEEL && ev->code != REL_DIAL &&
                    ev->code != REL_HWHEEL)
633
634
635
636
637
638
                return;

            /* Handle mouse wheel emulation */
            if (EvdevWheelEmuFilterMotion(pInfo, ev))
                return;

639
            pEvdev->rel_queued = 1;
640
            map = pEvdev->rel_axis_map[ev->code];
Éric Brunet's avatar
Éric Brunet committed
641
642
643
644

            if (valuator_mask_isset(pEvdev->rel_vals, map))
                value += valuator_mask_get(pEvdev->rel_vals, map);

645
            valuator_mask_set(pEvdev->rel_vals, map, value);
646
647
648
649
            break;
    }
}

650
651
652
653
static void
EvdevProcessTouch(InputInfoPtr pInfo)
{
    EvdevPtr pEvdev = pInfo->private;
654
    int type;
Peter Hutterer's avatar
Peter Hutterer committed
655
    int slot = pEvdev->cur_slot;
656

Peter Hutterer's avatar
Peter Hutterer committed
657
    if (slot < 0 || !pEvdev->mt_mask)
658
659
        return;

Peter Hutterer's avatar
Peter Hutterer committed
660
    if (!pEvdev->slots[slot].dirty)
661
662
        return;

Peter Hutterer's avatar
Peter Hutterer committed
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
    switch(pEvdev->slots[slot].state)
    {
        case SLOTSTATE_EMPTY:
            return;
        case SLOTSTATE_CLOSE:
            type = XI_TouchEnd;
            pEvdev->slots[slot].state = SLOTSTATE_EMPTY;
            break;
        case SLOTSTATE_OPEN:
            type = XI_TouchBegin;
            pEvdev->slots[slot].state = SLOTSTATE_UPDATE;
            break;
        case SLOTSTATE_UPDATE:
        default:
            type = XI_TouchUpdate;
            break;
    }
680

681
682
683
    EvdevSwapAbsValuators(pEvdev, pEvdev->mt_mask);
    EvdevApplyCalibration(pEvdev, pEvdev->mt_mask);

684
685
    EvdevQueueTouchEvent(pInfo, pEvdev->cur_slot, pEvdev->mt_mask, type);

Peter Hutterer's avatar
Peter Hutterer committed
686
    pEvdev->slots[slot].dirty = 0;
687
688
689
690

    valuator_mask_zero(pEvdev->mt_mask);
}

691
692
693
static int
num_slots(EvdevPtr pEvdev)
{
Peter Hutterer's avatar
Peter Hutterer committed
694
695
696
697
698
699
    int value;

    if (pEvdev->mtdev)
        value = pEvdev->mtdev->caps.slot.maximum + 1;
    else
        value = libevdev_get_num_slots(pEvdev->dev);
700
701
702
703
704
705
706
707

    /* If we don't know how many slots there are, assume at least 10 */
    return value > 1 ? value : 10;
}

static int
last_mt_vals_slot(EvdevPtr pEvdev)
{
708
    int value = pEvdev->cur_slot;
709
710
711
712

    return value < num_slots(pEvdev) ? value : -1;
}

713
714
715
716
717
718
static void
EvdevProcessTouchEvent(InputInfoPtr pInfo, struct input_event *ev)
{
    EvdevPtr pEvdev = pInfo->private;
    int map;

Peter Hutterer's avatar
Peter Hutterer committed
719
720
    if (!pEvdev->mtdev &&
        !libevdev_has_event_code(pEvdev->dev, EV_ABS, ABS_MT_SLOT))
721
722
        return;

723
724
725
    if (pEvdev->fake_mt)
        return;

726
727
    if (ev->code == ABS_MT_SLOT) {
        EvdevProcessTouch(pInfo);
728
729
730
731
732
733
734
735
        if (ev->value >= num_slots(pEvdev) ) {
            LogMessageVerbSigSafe(X_WARNING, 0,
                                  "%s: Slot index %d out of bounds (max %d), touch events may be incorrect.\n",
                                  pInfo->name,
                                  ev->value,
                                  num_slots(pEvdev) - 1);
            return;
        }
736
        pEvdev->cur_slot = ev->value;
737
738
    } else
    {
739
        int slot_index = last_mt_vals_slot(pEvdev);
Peter Hutterer's avatar
Peter Hutterer committed
740
741
742
743
744
745
746
        if (slot_index < 0) {
                    LogMessageVerbSigSafe(X_WARNING, 0,
                                          "%s: Invalid slot index %d, touch events may be incorrect.\n",
                                          pInfo->name,
                                          slot_index);
                    return;
        }
747

Peter Hutterer's avatar
Peter Hutterer committed
748
        pEvdev->slots[slot_index].dirty = 1;
749
        if (ev->code == ABS_MT_TRACKING_ID) {
750
            if (ev->value >= 0) {
Peter Hutterer's avatar
Peter Hutterer committed
751
                pEvdev->slots[slot_index].state = SLOTSTATE_OPEN;
752

Peter Hutterer's avatar
Peter Hutterer committed
753
754
755
756
                valuator_mask_copy(pEvdev->mt_mask,
                                   pEvdev->last_mt_vals[slot_index]);
            } else if (pEvdev->slots[slot_index].state != SLOTSTATE_EMPTY)
                pEvdev->slots[slot_index].state = SLOTSTATE_CLOSE;
757
        } else {
758
            map = pEvdev->abs_axis_map[ev->code];
759
            valuator_mask_set(pEvdev->mt_mask, map, ev->value);
Peter Hutterer's avatar
Peter Hutterer committed
760
761
            valuator_mask_set(pEvdev->last_mt_vals[slot_index], map,
                              ev->value);
762
        }
763
764
765
    }
}

766
767
768
769
770
771
/**
 * Take the absolute motion input event and process it accordingly.
 */
static void
EvdevProcessAbsoluteMotionEvent(InputInfoPtr pInfo, struct input_event *ev)
{
772
    int value;
773
    EvdevPtr pEvdev = pInfo->private;
774
    int map;
775
776
777
778
779
780
781
782
783
784
785

    /* Get the signed value, earlier kernels had this as unsigned */
    value = ev->value;

    /* Ignore EV_ABS events if we never set up for them. */
    if (!(pEvdev->flags & EVDEV_ABSOLUTE_EVENTS))
        return;

    if (ev->code > ABS_MAX)
        return;

786
787
788
789
790
791
    /* Always store the current abs valuator, we need it to update old_vals
     * which is required by wheel emulation */
    map = pEvdev->abs_axis_map[ev->code];
    if (map < 2)
            valuator_mask_set(pEvdev->abs_vals, map, value);

792
793
794
    if (EvdevWheelEmuFilterMotion(pInfo, ev))
        return;

795
    if (ev->code >= ABS_MT_SLOT) {
796
        EvdevProcessTouchEvent(pInfo, ev);
797
    } else if (!pEvdev->mt_mask) {
798
        map = pEvdev->abs_axis_map[ev->code];
799
800
801
802
803
804
805
806
807

        /* check if the event must be translated into relative */
        if (map < 2 && (pEvdev->flags & EVDEV_RELATIVE_MODE)) {
            int oldval;
            if (valuator_mask_fetch(pEvdev->old_vals, map, &oldval)) {
                valuator_mask_set(pEvdev->rel_vals, map, value - oldval);
                pEvdev->rel_queued = 1;
            }
        } else {
Peter Hutterer's avatar
Peter Hutterer committed
808
            valuator_mask_set(pEvdev->abs_vals, map, value);
809
810
            pEvdev->abs_queued = 1;
        }
811
    }
812
813
814
815
816
817
818
819
}

/**
 * Take the key press/release input event and process it accordingly.
 */
static void
EvdevProcessKeyEvent(InputInfoPtr pInfo, struct input_event *ev)
{
820
    int value, i;
821
    EvdevPtr pEvdev = pInfo->private;
822

823
824
825
826
827
828
829
830
    /* Get the signed value, earlier kernels had this as unsigned */
    value = ev->value;

    /* don't repeat mouse buttons */
    if (ev->code >= BTN_MOUSE && ev->code < KEY_OK)
        if (value == 2)
            return;

831
832
833
834
    for (i = 0; i < ArrayLength(proximity_bits); i++)
    {
        if (ev->code == proximity_bits[i])
        {
Peter Hutterer's avatar
Peter Hutterer committed
835
            EvdevProcessProximityEvent(pInfo, ev);
836
837
838
            return;
        }
    }
839

840
    switch (ev->code) {
841
        case BTN_TOUCH:
842
843
844
845
            /* For devices that have but don't use proximity, use
             * BTN_TOUCH as the proximity notifier */
            if (!pEvdev->use_proximity)
                pEvdev->in_proximity = value ? ev->code : 0;
846
847
848
849
850
851
852
853
854
            /* When !pEvdev->use_proximity, we don't report
             * proximity events to the X server. However, we
             * still want to keep track if one is in proximity or
             * not. This is especially important for touchpad
             * who report proximity information to the computer
             * (but it is not sent to X) and who might send unreliable
             * position information when not in_proximity.
             */

855
856
            if (!(pEvdev->flags & (EVDEV_TOUCHSCREEN | EVDEV_TABLET)) ||
                pEvdev->mt_mask)
857
                break;
858
859
860
861
862
863
864
            /* Treat BTN_TOUCH from devices that only have BTN_TOUCH as
             * BTN_LEFT. */
            ev->code = BTN_LEFT;
            /* Intentional fallthrough! */

        default:
            EvdevProcessButtonEvent(pInfo, ev);
865
            break;
866
867
    }
}
868

869
870
871
/**
 * Post the relative motion events.
 */
872
void
873
EvdevPostRelativeMotionEvents(InputInfoPtr pInfo)
874
875
876
{
    EvdevPtr pEvdev = pInfo->private;

877
    if (pEvdev->rel_queued && pEvdev->in_proximity) {
878
        xf86PostMotionEventM(pInfo->dev, Relative, pEvdev->rel_vals);
879
880
881
882
883
884
    }
}

/**
 * Post the absolute motion events.
 */
885
void
886
EvdevPostAbsoluteMotionEvents(InputInfoPtr pInfo)
887
888
889
890
{
    EvdevPtr pEvdev = pInfo->private;

    /*
891
     * Some devices only generate valid abs coords when BTN_TOOL_PEN is
892
     * pressed.  On wacom tablets, this means that the pen is in
893
     * proximity of the tablet.  After the pen is removed, BTN_TOOL_PEN is
894
     * released, and a (0, 0) absolute event is generated.  Checking
895
896
897
     * pEvdev->in_proximity here lets us ignore that event.
     * pEvdev->in_proximity is initialized to 1 so devices that don't use
     * this scheme still just work.
898
     */
899
    if (pEvdev->abs_queued && pEvdev->in_proximity) {
900
        xf86PostMotionEventM(pInfo->dev, Absolute, pEvdev->abs_vals);
901
902
903
    }
}

Peter Hutterer's avatar
Peter Hutterer committed
904
static void
905
EvdevPostProximityEvents(InputInfoPtr pInfo, int which)
Peter Hutterer's avatar
Peter Hutterer committed
906
907
908
909
{
    int i;
    EvdevPtr pEvdev = pInfo->private;

910
    for (i = 0; pEvdev->prox_queued && i < pEvdev->num_queue; i++) {
Peter Hutterer's avatar
Peter Hutterer committed
911
912
913
        switch (pEvdev->queue[i].type) {
            case EV_QUEUE_KEY:
            case EV_QUEUE_BTN:
914
            case EV_QUEUE_TOUCH:
Peter Hutterer's avatar
Peter Hutterer committed
915
916
917
                break;
            case EV_QUEUE_PROXIMITY:
                if (pEvdev->queue[i].val == which)
918
                    xf86PostProximityEventM(pInfo->dev, which, pEvdev->old_vals);
Peter Hutterer's avatar
Peter Hutterer committed
919
920
921
922
923
                break;
        }
    }
}

924
925
926
/**
 * Post the queued key/button events.
 */
927
static void EvdevPostQueuedEvents(InputInfoPtr pInfo)
928
929
930
931
932
933
934
{
    int i;
    EvdevPtr pEvdev = pInfo->private;

    for (i = 0; i < pEvdev->num_queue; i++) {
        switch (pEvdev->queue[i].type) {
        case EV_QUEUE_KEY:
935
            xf86PostKeyboardEvent(pInfo->dev, pEvdev->queue[i].detail.key,
936
937
938
                                  pEvdev->queue[i].val);
            break;
        case EV_QUEUE_BTN:
Peter Hutterer's avatar
Peter Hutterer committed
939
            if (Evdev3BEmuFilterEvent(pInfo,
940
                                      pEvdev->queue[i].detail.key,
Peter Hutterer's avatar
Peter Hutterer committed
941
942
943
                                      pEvdev->queue[i].val))
                break;

944
            if (pEvdev->abs_queued && pEvdev->in_proximity) {
945
946
                xf86PostButtonEvent(pInfo->dev, Absolute, pEvdev->queue[i].detail.key,
                                     pEvdev->queue[i].val, 0, 0);
947
948

            } else
949
                xf86PostButtonEvent(pInfo->dev, Relative, pEvdev->queue[i].detail.key,
950
                                    pEvdev->queue[i].val, 0, 0);
951
            break;
Peter Hutterer's avatar
Peter Hutterer committed
952
953
        case EV_QUEUE_PROXIMITY:
            break;
954
955
956
957
958
        case EV_QUEUE_TOUCH:
            xf86PostTouchEvent(pInfo->dev, pEvdev->queue[i].detail.touch,
                               pEvdev->queue[i].val, 0,
                               pEvdev->queue[i].touchMask);
            break;
959
960
961
        }
    }
}
962

963
964
965
966
967
968
969
/**
 * Take the synchronization input event and process it accordingly; the motion
 * notify events are sent first, then any button/key press/release events.
 */
static void
EvdevProcessSyncEvent(InputInfoPtr pInfo, struct input_event *ev)
{
970
    int i;
971
    EvdevPtr pEvdev = pInfo->private;
972

Peter Hutterer's avatar
Peter Hutterer committed
973
974
    EvdevProcessProximityState(pInfo);

975
    EvdevProcessValuators(pInfo);
976
    EvdevProcessTouch(pInfo);
977

978
979
980
981
982
    EvdevPostProximityEvents(pInfo, TRUE);
    EvdevPostRelativeMotionEvents(pInfo);
    EvdevPostAbsoluteMotionEvents(pInfo);
    EvdevPostQueuedEvents(pInfo);
    EvdevPostProximityEvents(pInfo, FALSE);
983

984
985
986
987
988
989
990
991
992
    for (i = 0; i < ArrayLength(pEvdev->queue); i++)
    {
        EventQueuePtr queue = &pEvdev->queue[i];
        queue->detail.key = 0;
        queue->type = 0;
        queue->val = 0;
        /* don't reset the touchMask */
    }

993
994
995
996
    if (pEvdev->rel_vals)
        valuator_mask_zero(pEvdev->rel_vals);
    if (pEvdev->abs_vals)
        valuator_mask_zero(pEvdev->abs_vals);
997
    pEvdev->num_queue = 0;
998
999
1000
    pEvdev->abs_queued = 0;
    pEvdev->rel_queued = 0;
    pEvdev->prox_queued = 0;