summaryrefslogtreecommitdiff
path: root/test/test_wacom.py
blob: 19999ac0df7ab30009ab118ac61c8b843f76604f (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
# Copyright 2022 Red Hat, Inc
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.


from typing import Dict
from . import Device, Monitor, Ev, Sev, Proximity, PenId

import pytest
import logging
import gi
from gi.repository import GLib

gi.require_version("wacom", "1.0")
from gi.repository import wacom

logger = logging.getLogger(__name__)


@pytest.fixture
def mainloop():
    """Default mainloop fixture, exiting after 500ms"""
    mainloop = GLib.MainLoop()
    GLib.timeout_add(500, mainloop.quit)
    return mainloop


@pytest.fixture
def opts() -> Dict[str, str]:
    """Default driver options (for debugging)"""
    return {
        "CommonDBG": "12",
        "DebugLevel": "12",
    }


def test_proximity(mainloop, opts):
    """
    Simple test to verify proximity in/out events are sent
    """
    dev = Device.from_name("PTH660", "Pen")
    monitor = Monitor.new_from_device(dev, opts)

    prox_in = [
        Sev("ABS_X", 50),
        Sev("ABS_Y", 50),
        Sev("BTN_TOOL_PEN", 1),
        Sev("SYN_REPORT", 0),
    ]
    prox_out = [
        Sev("ABS_X", 50),
        Sev("ABS_Y", 50),
        Sev("BTN_TOOL_PEN", 0),
        Sev("SYN_REPORT", 0),
    ]
    monitor.write_events(prox_in)
    monitor.write_events(prox_out)
    mainloop.run()

    assert isinstance(monitor.events[0], Proximity)
    assert monitor.events[0].is_prox_in

    assert isinstance(monitor.events[-1], Proximity)
    assert not monitor.events[-1].is_prox_in


@pytest.mark.parametrize("rotate", ["NONE", "CW", "CCW", "HALF"])
def test_relative_motion(mainloop, opts, rotate):
    """
    Check relative motion works in the various rotations
    """
    opts["Mode"] = "Relative"
    opts["Rotate"] = rotate
    dev = Device.from_name("PTH660", "Pen")
    monitor = Monitor.new_from_device(dev, opts)

    prox_in = [
        Sev("ABS_X", 50),
        Sev("ABS_Y", 50),
        Sev("BTN_TOOL_PEN", 1),
        Sev("SYN_REPORT", 0),
    ]

    # Physical pen motion is center towards bottom right
    motions = []
    for i in range(20):
        motions.extend(
            [
                Sev("ABS_X", 50 + i),
                Sev("ABS_Y", 50 + i),
                Sev("SYN_REPORT", 0),
            ]
        )

    prox_out = [
        Sev("ABS_X", 50),
        Sev("ABS_Y", 50),
        Sev("BTN_TOOL_PEN", 0),
        Sev("SYN_REPORT", 0),
    ]
    monitor.write_events(prox_in)
    monitor.write_events(motions)
    monitor.write_events(prox_out)
    mainloop.run()

    # Now collect the events
    xs = [e.axes.x for e in monitor.events]
    ys = [e.axes.y for e in monitor.events]

    print(f"Collected events: {list(zip(xs, ys))}")

    # We're in relative mode, so we expect the first and last event (the
    # proximity ones) to be 0/0
    assert xs[0] == 0
    assert ys[0] == 0
    assert xs[-1] == 0
    assert ys[-1] == 0

    # There's some motion adjustment, so we skip the first five events, the
    # rest should be exactly the same
    motions = list(zip(xs[5:-1], ys[5:-1]))

    print(f"Motion events to analyze: {motions}")

    # WARNING: the CW and CCW rotation seems swapped but that's what the
    # driver does atm
    if rotate == "NONE":
        assert all([x > 0 and y > 0 for x, y in motions])
    elif rotate == "HALF":
        assert all([x < 0 and y < 0 for x, y in motions])
    elif rotate == "CCW":
        assert all([x < 0 and y > 0 for x, y in motions])
    elif rotate == "CW":
        assert all([x > 0 and y < 0 for x, y in motions])
    else:
        pytest.fail("Invalid rotation mode")

    assert all([m == motions[0] for m in motions])


@pytest.mark.parametrize("axis", ["x", "y", "pressure", "tilt_x", "tilt_y", "wheel"])
@pytest.mark.parametrize("stylus_type", [PenId.ARTPEN, PenId.CINTIQ_13_PEN])
def test_axis_updates(mainloop, opts, axis, stylus_type):
    """
    Check that the various axes come through correctly
    """
    dev = Device.from_name("PTH660", "Pen")
    monitor = Monitor.new_from_device(dev, opts)

    # This is merely our local mapping into the axes array in the loop,
    # nothing to do with the driver
    map = {
        "x": 0,
        "y": 1,
        "pressure": 2,
        "tilt_x": 3,
        "tilt_y": 4,
        "wheel": 5,
    }

    # Send a bunch of events with only one axis changing, the rest remains at
    # the device's logical center
    for i in range(0, 30, 2):
        axes = [0] * len(map)
        axes[map[axis]] = i

        def axval(axis: str) -> int:
            return axes[map[axis]]

        ev = [
            Ev("ABS_MISC", stylus_type),
            Ev("MSC_SERIAL", 0x123456),
            Sev("ABS_X", 50 + axval("x")),
            Sev("ABS_Y", 50 + axval("y")),
            # ABS_Z sets ds->abswheel in the driver which is used for artpen
            # physical rotation and airbrush wheel - both share the
            # same valuator. This is *not* rotation, that axis is for the
            # cursor rotation only.
            # Here we send this event even for the Cintiq Pen to ensure it
            # gets correctly ignored by the driver
            Sev("ABS_Z", 50 + axval("wheel")),
            Sev("ABS_PRESSURE", 50 + axval("pressure")),
            Sev("ABS_DISTANCE", 0),  # Distance isn't exported
            Sev("ABS_TILT_X", 50 + axval("tilt_x")),
            Sev("ABS_TILT_Y", 50 + axval("tilt_y")),
            Sev("BTN_TOOL_PEN", 1),
            Sev("SYN_REPORT", 0),
        ]

        monitor.write_events(ev)

    mainloop.run()
    logger.debug(f"We have {len(monitor.events)} events")

    # Force a prox-out so we don't get stuck buttons. We don't call
    # mainloop.run() because we don't want to collect the prox out.
    ev = [
        Sev("BTN_TOOL_PEN", 0),
        Sev("SYN_REPORT", 0),
    ]
    monitor.write_events(ev)

    events = iter(monitor.events)
    # Ignore the proximity event since all axes change there by necessity
    _ = next(events)

    # bitflags to indicate which axis is actually present in the event
    mask_map = {
        "x": wacom.EventAxis.AXIS_X,
        "y": wacom.EventAxis.AXIS_Y,
        "pressure": wacom.EventAxis.AXIS_PRESSURE,
        "tilt_x": wacom.EventAxis.AXIS_TILT_X,
        "tilt_y": wacom.EventAxis.AXIS_TILT_X,
        "wheel": wacom.EventAxis.AXIS_WHEEL,
    }

    first = {name: getattr(next(events).axes, name) for name in map}

    for e in events:
        current = {name: getattr(e.axes, name) for name in map}
        for name in map:
            # Artpen has rotation via our 'wheel' axis but the cintiq pen does
            # not send that axis even when the kernel sends events for it
            pen_has_axis = name != "wheel" or stylus_type == PenId.ARTPEN
            event_has_axis = e.axes.mask & mask_map[name] != 0

            assert event_has_axis == pen_has_axis, f"for axis {name}"
            if event_has_axis and name == axis:
                assert first[name] < current[name], f"for axis {name}"
            else:
                assert first[name] == current[name], f"for axis {name}"


@pytest.mark.parametrize(
    "stylus_type", [PenId.ARTPEN, PenId.CINTIQ_13_PEN, PenId.AIRBRUSH]
)
def test_axis_updates_wheel(mainloop, opts, stylus_type):
    """
    Same as the above test but tests the wheel axis only, since that one is
    multiplexed through a single valuator.
    """
    dev = Device.from_name("PTH660", "Pen")
    monitor = Monitor.new_from_device(dev, opts)

    # Send a bunch of events with only one the wheel axis changing, the rest
    # remains at the device's logical center
    for i in range(0, 30, 2):
        extra_events = []

        # FIXME: there's a bug in the driver - because *both* ABS_Z (artpen)
        # and ABS_WHEEL (airbrush) unconditionally update ds->abswheel
        # regardless of the tool currently in prox, we must not send the
        # respective "other" axis for the current tool. Exception is the
        # default pen which is supposed to ignore both ABS_Z and ABS_WHEEL.

        # ABS_WHEEL sets ds->abswheel in the driver which is used for airbrush
        # physical wheel - both artpen and airbrush share the same valuator.
        # This is *not* the wheel for touchring. That axis is for the pad.
        if stylus_type != PenId.ARTPEN:
            extra_events.append(Sev("ABS_WHEEL", 50 + i))

        # ABS_Z sets ds->abswheel in the driver which is used for artpen
        # physical rotation - artpen shares the same valuator with airbrush.
        # This is *not* rotation. That axis is for the cursor rotation only.
        if stylus_type != PenId.AIRBRUSH:
            extra_events.append(Sev("ABS_Z", 50 + i))

        ev = [
            Ev("ABS_MISC", stylus_type),
            Ev("MSC_SERIAL", 0x123456),
            Sev("ABS_X", 50),
            Sev("ABS_Y", 50),
            # expands to ABS_Z, ABS_WHEEL or both
            *extra_events,
            Sev("ABS_PRESSURE", 50),
            Sev("ABS_DISTANCE", 0),  # Distance isn't exported
            Sev("ABS_TILT_X", 50),
            Sev("ABS_TILT_Y", 50),
            Sev("BTN_TOOL_PEN", 1),
            Sev("SYN_REPORT", 0),
        ]

        monitor.write_events(ev)

    mainloop.run()
    logger.debug(f"We have {len(monitor.events)} events")

    # Force a prox-out so we don't get stuck buttons. We don't call
    # mainloop.run() because we don't want to collect the prox out.
    ev = [
        Sev("BTN_TOOL_PEN", 0),
        Sev("SYN_REPORT", 0),
    ]
    monitor.write_events(ev)

    events = iter(monitor.events)
    # Ignore the proximity event since all axes change there by necessity
    _ = next(events)

    first = next(events).axes
    first_wheel = first.wheel

    for e in events:
        current = e.axes
        current_wheel = current.wheel

        # The default pen doesn't have the axis, artpen and airbrush have it
        # and we sent events for it, so expect it to be present
        pen_has_axis = stylus_type in [PenId.ARTPEN, PenId.AIRBRUSH]
        event_has_axis = e.axes.mask & wacom.EventAxis.AXIS_WHEEL != 0
        assert event_has_axis == pen_has_axis
        if pen_has_axis:
            assert first_wheel < current_wheel
        else:
            assert first_wheel == current_wheel


def test_scroll(mainloop, opts):
    """
    Check panscrolling works correctly
    """
    dev = Device.from_name("PTH660", "Pen")
    opts["PanScrollThreshold"] = "150"

    prox_in = [
        Sev("ABS_X", 50),
        Sev("ABS_Y", 50),
        Sev("BTN_TOOL_PEN", 1),
        Sev("SYN_REPORT", 0),
    ]

    prox_out = [
        Sev("BTN_TOOL_PEN", 0),
        Sev("SYN_REPORT", 0),
    ]

    press_button2 = [
        Sev("BTN_STYLUS", 1),
        Sev("SYN_REPORT", 0),
    ]

    touchdown_pen = [
        Sev("BTN_TOUCH", 1),
        Sev("ABS_PRESSURE", 20),
        Sev("SYN_REPORT", 0),
    ]

    move_pen_x = [Sev("ABS_X", 75), Sev("SYN_REPORT", 0)]

    up_pen = [Sev("BTN_TOUCH", 0), Sev("ABS_PRESSURE", 0), Sev("SYN_REPORT", 0)]

    depress_button2 = [Sev("BTN_STYLUS", 0), Sev("SYN_REPORT", 0)]

    monitor = Monitor.new_from_device(dev, opts)
    monitor.wacom_device.set_runtime_option("PanButton", "2")

    monitor.write_events(prox_in)
    monitor.write_events(press_button2)
    monitor.write_events(touchdown_pen)  # Pen touchdown
    monitor.write_events(move_pen_x)  # Move pen 25% towards positive x
    monitor.write_events(up_pen)  # Pen up
    monitor.write_events(depress_button2)  # Depress button2
    monitor.write_events(prox_out)

    mainloop.run()
    have_we_scrolled = False
    for event in monitor.events:
        if event.axes.scroll_x != 0:
            assert event.axes.scroll_x == -1223320
            have_we_scrolled = True
    assert have_we_scrolled


# vim: set expandtab tabstop=4 shiftwidth=4: