summaryrefslogtreecommitdiff
path: root/tests/input/wacom.cpp
blob: e338ef06476a40043bb2ea2f7a9e2dc0ca99efc0 (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
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
#if HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdexcept>
#include <map>
#include <unistd.h>

#include <xorg/gtest/xorg-gtest.h>
#include <linux/input.h>

#include <X11/Xlib.h>
#include <X11/Xatom.h>
#define XK_LATIN1
#include <X11/keysymdef.h>
#include <X11/XF86keysym.h>
#include <X11/Xutil.h>
#include <X11/extensions/XInput.h>
#include <X11/extensions/XInput2.h>

#include "wacom_devs.h"

#include <stdexcept>
#include <fstream>
#include <xorg/gtest/xorg-gtest.h>

#include <X11/extensions/XInput.h>
#include <X11/extensions/XInput2.h>

#include <xorg/wacom-properties.h>
#include <unistd.h>

#include "xit-server-input-test.h"
#include "device-interface.h"
#include "helpers.h"

/**
 * Wacom driver test class. This class takes a struct Tablet that defines
 * which device should be initialised.
 */
class WacomDriverTest : public XITServerInputTest,
                        public DeviceInterface,
                        public ::testing::WithParamInterface<Tablet> {
protected:

    /**
     * Write a configuration file for a hotplug-enabled server using the
     * dummy video driver.
     */
    virtual void SetUpConfigAndLog() {

        config.AddDefaultScreenWithDriver();
        config.SetAutoAddDevices(true);
        config.WriteConfig();
    }

    /**
     * Register for XI2 Hierarchy events on the root window
     */
    void SetUpXIEventMask () {
        XIEventMask evmask;
        unsigned char mask[2] = { 0, 0 };

        XISetMask(mask, XI_HierarchyChanged);

        evmask.deviceid = XIAllDevices;
        evmask.mask_len = sizeof(mask);
        evmask.mask = mask;

        EXPECT_EQ(Success, XISelectEvents(Display(), DefaultRootWindow(Display()), &evmask, 1));
        XSync(Display(), False);
    }

    /**
     * Create an evemu device based on GetParam()'s tablet.
     */
    void VerifyDevice (const Tablet& tablet)
    {
        char tool_name[255];

        if (tablet.stylus) {
            snprintf (tool_name, sizeof (tool_name), "%s %s", tablet.name, tablet.stylus);
            if (!FindInputDeviceByName(Display(), tool_name)) {
                ASSERT_TRUE(xorg::testing::XServer::WaitForDevice(Display(), tool_name, 5000))
                    << "Tool '" << tool_name << "' not found" << std::endl;
            }
        }

        if (tablet.eraser) {
            snprintf (tool_name, sizeof (tool_name), "%s %s", tablet.name, tablet.eraser);
            if (!FindInputDeviceByName(Display(), tool_name)) {
                ASSERT_TRUE(xorg::testing::XServer::WaitForDevice(Display(), tool_name, 5000))
                    << "Tool '" << tool_name << "' not found" << std::endl;
            }
        }

        if (tablet.cursor) {
            snprintf (tool_name, sizeof (tool_name), "%s %s", tablet.name, tablet.cursor);
            if (!FindInputDeviceByName(Display(), tool_name)) {
                ASSERT_TRUE(xorg::testing::XServer::WaitForDevice(Display(), tool_name, 5000))
                    << "Tool '" << tool_name << "' not found" << std::endl;
            }
        }

        if (tablet.touch) {
            snprintf (tool_name, sizeof (tool_name), "%s %s", tablet.name, tablet.touch);
            if (!FindInputDeviceByName(Display(), tool_name)) {
                ASSERT_TRUE(xorg::testing::XServer::WaitForDevice(Display(), tool_name, 5000))
                    << "Tool '" << tool_name << "' not found" << std::endl;
            }
        }

        if (tablet.pad) {
            snprintf (tool_name, sizeof (tool_name), "%s %s", tablet.name, tablet.pad);
            if (!FindInputDeviceByName(Display(), tool_name)) {
                ASSERT_TRUE(xorg::testing::XServer::WaitForDevice(Display(), tool_name, 5000))
                    << "Tool '" << tool_name << "' not found" << std::endl;
            }
        }
    }

    virtual void SetUp()
    {
        Tablet tablet = GetParam();
        SetDevice("tablets/" + std::string (tablet.descfile));

        XITServerInputTest::SetUp();

        SetUpXIEventMask ();
        VerifyDevice(tablet);
    }
};

/* Return True if the given device has the property, or False otherwise */
bool test_property(Display *dpy, int deviceid, const char *prop_name)
{
    Status status;
    Atom prop;
    Atom realtype;
    int realformat;
    unsigned long nitems, bytes_after;
    unsigned char *data;

    prop = XInternAtom (dpy, prop_name, False);
    status = XIGetProperty(dpy, deviceid, prop , 0, 1000, False,
                           AnyPropertyType, &realtype, &realformat,
                           &nitems, &bytes_after, &data);
    if (data != NULL)
        XFree (data);

    if (status == Success)
        return True;

    return False;
}

TEST_P(WacomDriverTest, DeviceNames)
{
    XORG_TESTCASE("Test names of the tools and type properties match the tools");

    Tablet tablet = GetParam();

    int major = 2;
    int minor = 0;
    ASSERT_EQ(Success, XIQueryVersion(Display(), &major, &minor));

    bool prop_found;

    /* Wait for devices to settle */

    int deviceid;
    char tool_name[255];

    if (tablet.stylus) {
        snprintf (tool_name, sizeof (tool_name), "%s %s", tablet.name, tablet.stylus);
        ASSERT_EQ(FindInputDeviceByName(Display(), tool_name, &deviceid), 1) <<  "Tool not found " << tool_name <<std::endl;

        prop_found = test_property (Display(), deviceid, WACOM_PROP_PRESSURECURVE);
        EXPECT_EQ(prop_found, True) << "Property " << WACOM_PROP_PRESSURECURVE << " not found on " << tool_name << std::endl;

        prop_found = test_property (Display(), deviceid, WACOM_PROP_ROTATION);
        EXPECT_EQ(prop_found, True) << "Property " << WACOM_PROP_ROTATION << " not found on " << tool_name << std::endl;

        prop_found = test_property (Display(), deviceid, WACOM_PROP_TOOL_TYPE);
        EXPECT_EQ(prop_found, True) << "Property " << WACOM_PROP_TOOL_TYPE << " not found on " << tool_name << std::endl;
    }

    if (tablet.eraser) {
        snprintf (tool_name, sizeof (tool_name), "%s %s", tablet.name, tablet.eraser);
        ASSERT_EQ(FindInputDeviceByName(Display(), tool_name, &deviceid), 1) <<  "Tool not found " << tool_name <<std::endl;

        prop_found = test_property (Display(), deviceid, WACOM_PROP_PRESSURECURVE);
        EXPECT_EQ(prop_found, True) << "Property " << WACOM_PROP_PRESSURECURVE << " not found on " << tool_name << std::endl;

        prop_found = test_property (Display(), deviceid, WACOM_PROP_ROTATION);
        EXPECT_EQ(prop_found, True) << "Property " << WACOM_PROP_ROTATION << " not found on " << tool_name << std::endl;
    }

    if (tablet.cursor) {
        snprintf (tool_name, sizeof (tool_name), "%s %s", tablet.name, tablet.cursor);
        ASSERT_EQ(FindInputDeviceByName(Display(), tool_name, &deviceid), 1) <<  "Tool not found " << tool_name <<std::endl;

        prop_found = test_property (Display(), deviceid,  WACOM_PROP_WHEELBUTTONS);
        EXPECT_EQ(prop_found, True) << "Property " << WACOM_PROP_WHEELBUTTONS << " not found on " << tool_name << std::endl;
    }

    if (tablet.pad) {
        snprintf (tool_name, sizeof (tool_name), "%s %s", tablet.name, tablet.pad);
        ASSERT_EQ(FindInputDeviceByName(Display(), tool_name, &deviceid), 1) <<  "Tool not found " << tool_name <<std::endl;

        prop_found = test_property (Display(), deviceid,  WACOM_PROP_STRIPBUTTONS);
        EXPECT_EQ(prop_found, True) << "Property " << WACOM_PROP_STRIPBUTTONS << " not found on " << tool_name << std::endl;
    }

    if (tablet.touch) {
        snprintf (tool_name, sizeof (tool_name), "%s %s", tablet.name, tablet.touch);
        ASSERT_EQ(FindInputDeviceByName(Display(), tool_name, &deviceid), 1) <<  "Tool not found " << tool_name <<std::endl;

        prop_found = test_property (Display(), deviceid,  WACOM_PROP_TOUCH);
        EXPECT_EQ(prop_found, True) << "Property " << WACOM_PROP_TOUCH << " not found on " << tool_name << std::endl;

        prop_found = test_property (Display(), deviceid,  WACOM_PROP_ENABLE_GESTURE);
        EXPECT_EQ(prop_found, True) << "Property " << WACOM_PROP_ENABLE_GESTURE << " not found on " << tool_name << std::endl;

        prop_found = test_property (Display(), deviceid,  WACOM_PROP_GESTURE_PARAMETERS);
        EXPECT_EQ(prop_found, True) << "Property " << WACOM_PROP_GESTURE_PARAMETERS << " not found on " << tool_name << std::endl;
    }
}

void check_for_type (Display *dpy, XIDeviceInfo *list, int ndevices, const char *type)
{
    XIDeviceInfo *info;
    int loop;
    bool found = false;

    info = list;
    for (loop = 0; loop < ndevices; loop++, info++) {
        if (test_property (dpy, info->deviceid, type)) {
                found = True;
                break;
        }
    }
    ASSERT_TRUE(found) << type << " not found" << std::endl;
}

TEST_P(WacomDriverTest, DeviceType)
{
    XORG_TESTCASE("Test type of the tools for the device");

    Tablet tablet = GetParam();
    int major = 2;
    int minor = 0;
    ASSERT_EQ(Success, XIQueryVersion(Display(), &major, &minor));

    XIDeviceInfo *list;
    int ndevices;
    list = XIQueryDevice(Display(), XIAllDevices, &ndevices);

    if (tablet.stylus)
        check_for_type (Display(), list, ndevices, WACOM_PROP_XI_TYPE_STYLUS);

    if (tablet.eraser)
        check_for_type (Display(), list, ndevices, WACOM_PROP_XI_TYPE_ERASER);

    if (tablet.cursor)
        check_for_type (Display(), list, ndevices, WACOM_PROP_XI_TYPE_CURSOR);

    if (tablet.pad)
        check_for_type (Display(), list, ndevices, WACOM_PROP_XI_TYPE_PAD);

    if (tablet.touch)
        check_for_type (Display(), list, ndevices, WACOM_PROP_XI_TYPE_TOUCH);

    XIFreeDeviceInfo (list);
}

bool set_rotate(Display *dpy, int deviceid, const char *rotate)
{
    Status status;
    int rotation = 0;
    Atom prop, type;
    int format;
    unsigned char* data;
    unsigned long nitems, bytes_after;

    if (strcasecmp(rotate, "cw") == 0)
        rotation = 1;
    else if (strcasecmp(rotate, "ccw") == 0)
        rotation = 2;
    else if (strcasecmp(rotate, "half") == 0)
        rotation = 3;

    prop = XInternAtom(dpy, WACOM_PROP_ROTATION, True);
    if (!prop)
        return False;

    status = XIGetProperty(dpy, deviceid, prop, 0, 1000, False,
                           AnyPropertyType, &type, &format,
                           &nitems, &bytes_after, &data);

    if (status != Success || nitems == 0 || format != 8)
        return False;

    *data = rotation;

    XIChangeProperty(dpy, deviceid, prop, type, format,
                          PropModeReplace, data, nitems);
    XFlush(dpy);

    return True;
}

int stylus_move_right (Display *dpy, xorg::testing::evemu::Device *dev)
{
    int root_x1, root_x2;
    int loop, step;
    XEvent ev;

    XSync(dpy, False);
    while(XPending(dpy))
        XNextEvent(dpy, &ev);

    // Might be considered as uninitialized otherwise
    root_x1 = 0;

    // Move to device coord (1000,1000)
    dev->PlayOne(EV_ABS, ABS_X, 1000, True);
    dev->PlayOne(EV_ABS, ABS_Y, 1000, True);
    dev->PlayOne(EV_ABS, ABS_DISTANCE, 0, True);
    dev->PlayOne(EV_KEY, BTN_TOOL_PEN, 1, True);

    XSync (dpy, False);
    EXPECT_NE(XPending(dpy), 0) << "No event received??" << std::endl;

    // Capture motion events and save screen coordinates
    XSync (dpy, False);
    while(XCheckMaskEvent (dpy, PointerMotionMask, &ev))
        root_x1 = ev.xmotion.x_root;

    root_x2 = root_x1;

    step = 10;
    for (loop = 1000; loop < 3000; loop += step) {
        dev->PlayOne(EV_ABS, ABS_X, loop, True);
        dev->PlayOne(EV_ABS, ABS_DISTANCE, step, True);
    }
    dev->PlayOne(EV_KEY, BTN_TOOL_PEN, 0, True);

    XSync (dpy, False);
    EXPECT_NE(XPending(dpy), 0) << "Still no event received??" << std::endl;

    XSync (dpy, False);
    while(XCheckMaskEvent (dpy, PointerMotionMask|PointerMotionHintMask, &ev))
        root_x2 = ev.xmotion.x_root;

    XSync (dpy, False);
    while(XPending(dpy))
        XNextEvent(dpy, &ev);

    return (root_x2 - root_x1);
}

// FIXME
// This test would fail in some random case, as it the X events were
// not generated...
TEST_P(WacomDriverTest, Rotation)
{
    XORG_TESTCASE("Test rotation is applied on a device");

    Tablet tablet = GetParam();

    int major = 2;
    int minor = 0;
    ASSERT_EQ(Success, XIQueryVersion(Display(), &major, &minor));

    bool status;

    XSelectInput(Display(), DefaultRootWindow(Display()), ButtonPressMask |
                                                          ButtonReleaseMask |
                                                          PointerMotionMask |
                                                          ButtonMotionMask);

    /* the server takes a while to start up but the devices may not respond
       to events yet. Add a noop call that just delays everything long
       enough for this test to work */
    XInternAtom(Display(), "foo", True);
    XFlush(Display());

    char tool_name[255];
    int displace;
    int deviceid;

    if (tablet.stylus) {
        snprintf (tool_name, sizeof (tool_name), "%s %s", tablet.name, tablet.stylus);
        ASSERT_EQ(FindInputDeviceByName(Display(), tool_name, &deviceid), 1) <<  "Tool not found " << tool_name <<std::endl;

        // Try with no rotation
        status = set_rotate (Display(), deviceid, "none");
        EXPECT_TRUE(status) << "Failed to rotate " << tool_name << std::endl;

        displace = stylus_move_right (Display(), dev.get());
        ASSERT_NE (displace, 0) << "Did not get any event from " << tool_name << std::endl;
        EXPECT_TRUE(displace > 0) << "Pointer moved to the wrong direction with rotate none for " << tool_name << std::endl;

        // Set opposite rotation
        status = set_rotate (Display(), deviceid, "half");
        EXPECT_TRUE(status) << "Failed to rotate " << tool_name << std::endl;

        displace = stylus_move_right (Display(), dev.get());
        ASSERT_NE (displace, 0) << "Did not get any event from " << tool_name << std::endl;
        EXPECT_TRUE(displace < 0) << "Pointer moved to the wrong direction with rotate half for " << tool_name << std::endl;
    }
}

INSTANTIATE_TEST_CASE_P(, WacomDriverTest,
        ::testing::ValuesIn(tablets));

TEST(WacomDriver, PrivToolDoubleFree)
{
    XORG_TESTCASE("Two devices with the same device node cause a double-free\n"
                 "of priv->tool in wcmPreInitParseOptions.\n"
                 "https://bugs.freedesktop.org/show_bug.cgi?id=55200");

    XOrgConfig config;
    XITServer server;

    std::auto_ptr<xorg::testing::evemu::Device> stylus =
        std::auto_ptr<xorg::testing::evemu::Device>(
                new xorg::testing::evemu::Device(RECORDINGS_DIR "tablets/Wacom-Intuos5-touch-M-Pen.desc")
                );

    config.AddInputSection("wacom", "I5 stylus",
                           "Option \"Device\" \"" + stylus->GetDeviceNode() + "\"\n"
                           "Option \"USB\" \"on\"\n"
                           "Option \"Type\" \"stylus\"\n"
                           );

    config.AddInputSection("wacom", "I5 stylus 2",
                           "Option \"Device\" \"" + stylus->GetDeviceNode() + "\"\n"
                           "Option \"USB\" \"on\"\n"
                           "Option \"Type\" \"stylus\"\n"
                           );
    config.AddDefaultScreenWithDriver();

    EXPECT_NO_THROW(server.Start(config));
}

class WacomLensCursorTest : public WacomDriverTest
{
};

TEST_P(WacomLensCursorTest, CursorMove)
{
    XSelectInput(Display(), DefaultRootWindow(Display()), ButtonPressMask |
                                                          ButtonReleaseMask |
                                                          PointerMotionMask |
                                                          ButtonMotionMask);
    XSync(Display(), False);

    dev->Play(RECORDINGS_DIR "tablets/Wacom-Intuos4-8x13.lens-cursor-move.events");

    XSync(Display(), False);

    ASSERT_TRUE(XPending(Display()));

    XEvent ev;
    XNextEvent(Display(), &ev);
    ASSERT_EQ(ev.xany.type, MotionNotify);
    XNextEvent(Display(), &ev);
    ASSERT_EQ(ev.xany.type, MotionNotify);
    XNextEvent(Display(), &ev);
    ASSERT_EQ(ev.xany.type, MotionNotify);
}

INSTANTIATE_TEST_CASE_P(, WacomLensCursorTest, ::testing::ValuesIn(lens_cursor_tablets));

class WacomToolProximityTest : public XITServerInputTest,
                               public DeviceInterface,
                               public ::testing::WithParamInterface<int> {
public:
    virtual void SetUpConfigAndLog() {
        std::string tool_type;
        switch (GetParam()) {
            case BTN_TOOL_PEN:
            case BTN_TOOL_BRUSH:
            case BTN_TOOL_AIRBRUSH:
            case BTN_TOOL_PENCIL:
                tool_type = "stylus";
                break;
            case BTN_TOOL_RUBBER:
                tool_type = "eraser";
                break;
            case BTN_TOOL_MOUSE:
            case BTN_TOOL_LENS:
                tool_type = "cursor";
                break;
            default:
                FAIL() << "Invalid tool type";
        }

        config.AddDefaultScreenWithDriver();
        config.SetAutoAddDevices(false);
        config.AddInputSection("wacom", "--cursor--",
                               "Option \"Type\" \"" + tool_type + "\"\n"
                               "Option \"GrabDevice\" \"off\"\n"
                               "Option \"DebugLevel\" \"12\"\n"
                               "Option \"CommonDBG\" \"12\"\n"
                               "Option \"CursorProx\" \"63\"\n"
                               "Option \"Device\" \"" + dev->GetDeviceNode() + "\"");
        config.WriteConfig();
    }

    virtual void SetUp() {
        SetDevice("tablets/Wacom-Intuos4-8x13.desc");

        int tool_type = GetParam();

        /* bring device into prox */
        dev->PlayOne(EV_ABS, ABS_X, 6784, false);
        dev->PlayOne(EV_ABS, ABS_Y, 2946, false);
        dev->PlayOne(EV_ABS, ABS_DISTANCE, 63, false);
        dev->PlayOne(EV_KEY, tool_type, 1, false);
        dev->PlayOne(EV_MSC, MSC_SERIAL, -494927850, true);

        dev->PlayOne(EV_ABS, ABS_X, 6740, false);
        dev->PlayOne(EV_ABS, ABS_DISTANCE, 45, false);
        dev->PlayOne(EV_MSC, MSC_SERIAL, -494927850, true);

        XITServerInputTest::SetUp();
    }
};


TEST_P(WacomToolProximityTest, ToolMovesOnProximity)
{
    XORG_TESTCASE("Create device with the given tool.\n"
                  "Get tool into proximity.\n"
                  "Start server.\n"
                  "Check that events from the tool move the cursor\n"
                  "https://bugs.freedesktop.org/show_bug.cgi?id=56536");

    XSelectInput(Display(), DefaultRootWindow(Display()), ButtonPressMask |
                                                          ButtonReleaseMask |
                                                          PointerMotionMask |
                                                          ButtonMotionMask);
    dev->PlayOne(EV_ABS, ABS_X, 6800, false);
    dev->PlayOne(EV_ABS, ABS_Y, 2900, false);
    dev->PlayOne(EV_ABS, ABS_DISTANCE, 40, false);
    dev->PlayOne(EV_MSC, MSC_SERIAL, -494927850, true);

    dev->PlayOne(EV_ABS, ABS_X, 6800, false);
    dev->PlayOne(EV_ABS, ABS_Y, 2900, false);
    dev->PlayOne(EV_ABS, ABS_DISTANCE, 40, false);
    dev->PlayOne(EV_MSC, MSC_SERIAL, -494927850, true);


    ASSERT_TRUE(xorg::testing::XServer::WaitForEventOfType(Display(), MotionNotify));
}

INSTANTIATE_TEST_CASE_P(, WacomToolProximityTest, ::testing::Values(BTN_TOOL_PEN,
                                                                    BTN_TOOL_AIRBRUSH,
                                                                    BTN_TOOL_PENCIL,
                                                                    BTN_TOOL_BRUSH,
                                                                    BTN_TOOL_RUBBER,
                                                                    BTN_TOOL_LENS,
                                                                    BTN_TOOL_MOUSE));

int main(int argc, char **argv) {
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}