#!/bin/env python3 # SPDX-License-Identifier: GPL-2.0 # -*- coding: utf-8 -*- # # Copyright (c) 2017 Benjamin Tissoires <benjamin.tissoires@gmail.com> # Copyright (c) 2017 Red Hat, Inc. # Copyright (c) 2020 Wacom Technology Corp. # # Authors: # Jason Gerecke <jason.gerecke@wacom.com>
"""
Tests for the Wacom driver generic codepath.
This module tests the function of the Wacom driver's generic codepath.
The generic codepath is used by devices which are not explicitly listed in the driver's device table. It uses the device's HID descriptor to
decode reports sent by the device. """
from .descriptors_wacom import (
wacom_pth660_v145,
wacom_pth660_v150,
wacom_pth860_v145,
wacom_pth860_v150,
wacom_pth460_v105,
)
import attr from collections import namedtuple from enum import Enum from hidtools.hut import HUT from hidtools.hid import HidUnit from . import base from . import test_multitouch import libevdev import pytest
class ProximityState(Enum): """
Enumeration of allowed proximity states. """
# Tool is not able to be sensed by the device
OUT = 0
# Tool is close enough to be sensed, but some data may be invalid # or inaccurate
IN_PROXIMITY = 1
# Tool is close enough to be sensed with high accuracy. All data # valid.
IN_RANGE = 2
def fill(self, reportdata): """Fill a report with approrpiate HID properties/values."""
reportdata.inrange = self in [ProximityState.IN_RANGE]
reportdata.wacomsense = self in [
ProximityState.IN_PROXIMITY,
ProximityState.IN_RANGE,
]
class ReportData: """
Placeholder for HID report values. """
pass
@attr.s class Buttons: """
Stylus button state.
Describes the state of each of the buttons / "side switches" that
may be present on a stylus. Buttons set to 'None' indicate the
state is"unchanged" since the previous event. """
@staticmethod def clear(): """Button object with all states cleared.""" return Buttons(False, False, False)
def fill(self, reportdata): """Fill a report with approrpiate HID properties/values."""
reportdata.barrelswitch = int(self.primary or 0)
reportdata.secondarybarrelswitch = int(self.secondary or 0)
reportdata.b3 = int(self.tertiary or 0)
@attr.s class ToolID: """
Stylus tool identifiers.
Contains values used to identify a specific stylus, e.g. its serial
number and tool-type identifier. Values of ``0`` may sometimes be
used for the out-of-range condition. """
serial = attr.ib()
tooltype = attr.ib()
@staticmethod def clear(): """ToolID object with all fields cleared.""" return ToolID(0, 0)
def contains(self, field): """
Check if the physical size of the provided field isin range.
Compare the physical size described by the provided HID field
against the range of sizes described by this object. This is
an exclusive range comparison (e.g. 0 cm isnot within the
range 0 cm - 5 cm) and exact unit comparison (e.g. 1 inch is not within the range 0 cm - 5 cm). """
phys_size = (field.physical_max - field.physical_min) * 10 ** (field.unit_exp) return (
field.unit == self.unit.value and phys_size > self.min_size and phys_size < self.max_size
)
class BaseTablet(base.UHIDTestDevice): """
Skeleton object for all kinds of tablet devices. """
def match_evdev_rule(self, application, evdev): """
Filter out evdev nodes based on the requested application.
The Wacom driver may create several device nodes for each USB
interface device. It is crucial that we run tests with the
expected device node or things will obviously go off the rails.
Use the Wacom driver's usual naming conventions to apply a
sensible default filter. """ if application in ["Pen", "Pad"]: return evdev.name.endswith(application) else: returnTrue
def create_report(
self, x, y, pressure, buttons=None, toolid=None, proximity=None, reportID=None
): """ Return an input report for this device.
:param x: absolute x
:param y: absolute y
:param pressure: pressure
:param buttons: stylus button state. Use ``None`` for unchanged.
:param toolid: tool identifiers. Use ``None`` for unchanged.
:param proximity: a ProximityState indicating the sensor's ability
to detect and report attributes of this tool. Use ``None`` for unchanged.
:param reportID: the numeric report ID for this report, if needed """ if buttons isnotNone:
self.buttons = buttons
buttons = self.buttons
if toolid isnotNone:
self.toolid = toolid
toolid = self.toolid
if proximity isnotNone:
self.proximity = proximity
proximity = self.proximity
reportID = reportID or self.default_reportID
report = ReportData()
report.x = x
report.y = y
report.tippressure = pressure
report.tipswitch = pressure > 0
buttons.fill(report)
proximity.fill(report)
toolid.fill(report)
def create_report_heartbeat(self, reportID): """ Return a heartbeat input report for this device.
Heartbeat reports generally contain battery status information,
among other things. """
report = ReportData()
report.wacombatterycharging = 1 return super().create_report(report, reportID=reportID)
def event(self, x, y, pressure, buttons=None, toolid=None, proximity=None): """
Send an input event on the default report ID.
:param x: absolute x
:param y: absolute y
:param buttons: stylus button state. Use ``None`` for unchanged.
:param toolid: tool identifiers. Use ``None`` for unchanged.
:param proximity: a ProximityState indicating the sensor's ability
to detect and report attributes of this tool. Use ``None`` for unchanged. """
r = self.create_report(x, y, pressure, buttons, toolid, proximity)
self.call_input_event(r) return [r]
def event_heartbeat(self, reportID): """
Send a heartbeat event on the requested report ID. """
r = self.create_report_heartbeat(reportID)
self.call_input_event(r) return [r]
def event_pad(self, reportID, ring=None, ek0=None): """
Send a pad event on the requested report ID. """
r = self.create_report_pad(reportID, ring, ek0)
self.call_input_event(r) return [r]
class OpaqueTablet(BaseTablet): """
Bare-bones opaque tablet with a minimum of features.
A tablet stripped down to its absolute core. It is capable of
reporting X/Y position andif the pen isin contact. No pressure,
no barrel switches, no eraser. Notably it *does* report an "In
Range" flag, but this is only because the Wacom driver expects
one to function properly. The device uses only standard HID usages, not any of Wacom's vendor-defined pages. """
class OpaqueCTLTablet(BaseTablet): """
Opaque tablet similar to something in the CTL product line.
A pen-only tablet with most basic features you would expect from
an actual device. Position, eraser, pressure, barrel buttons.
Uses the Wacom vendor-defined usage page. """
class PTHX60_Pen(BaseTablet): """
Pen interface of a PTH-660 / PTH-860 / PTH-460 tablet.
This generation of devices are nearly identical to each other, though
the PTH-460 uses a slightly different descriptor construction (splits
the pad among several physical collections) """
class BaseTest: class TestTablet(base.BaseTestCase.TestUhid):
kernel_modules = [KERNEL_MODULE]
def sync_and_assert_events(
self, report, expected_events, auto_syn=True, strict=False
): """ Assert we see the expected events in response to a report. """
uhdev = self.uhdev
syn_event = self.syn_event if auto_syn:
expected_events.append(syn_event)
actual_events = uhdev.next_sync_events()
self.debug_reports(report, uhdev, actual_events) if strict:
self.assertInputEvents(expected_events, actual_events) else:
self.assertInputEventsIn(expected_events, actual_events)
def get_usages(self, uhdev): def get_report_usages(report):
application = report.application for field in report.fields: if field.usages isnotNone: for usage in field.usages: yield (field, usage, application) else: yield (field, field.usage, application)
desc = uhdev.parsed_rdesc
reports = [
*desc.input_reports.values(),
*desc.feature_reports.values(),
*desc.output_reports.values(),
] for report in reports: for usage in get_report_usages(report): yield usage
def assertName(self, uhdev, type): """ Assert that the name isas we expect.
The Wacom driver applies a number of decorations to the name
provided by the hardware. We cannot rely on the definition of
this assertion from the base class to work properly. """
evdev = uhdev.get_evdev()
expected_name = uhdev.name + type if"wacom"notin expected_name.lower():
expected_name = "Wacom " + expected_name assert evdev.name == expected_name
def test_descriptor_physicals(self): """
Verify that all HID usages which should have a physical range
actually do, and those which shouldn't don't. Also verify that
the associated unit is correct and within a sensible range. """
def empty_pad_sync(self, num, denom, reverse): """
Test that multiple pad collections do not trigger empty syncs. """
def offset_rotation(value): """
Offset touchring rotation values by the same factor as the
Linux kernel. Tablets historically don't use the same origin as HID, and it sometimes changes from tablet to tablet... """
evdev = self.uhdev.get_evdev()
info = evdev.absinfo[libevdev.EV_ABS.ABS_WHEEL]
delta = info.maximum - info.minimum + 1 if reverse:
value = info.maximum - value
value += num * delta // denom if value > info.maximum:
value -= delta elif value < info.minimum:
value += delta return value
def make_contact(self, contact_id=0, t=0): """
Make a single touch contact that can move over time.
Creates a touch object that has a well-known position in space that
does not overlap with other contacts. The value of `t` may be
incremented over time to move the point along a linear path. """
x = 50 + 10 * contact_id + t * 11
y = 100 + 100 * contact_id + t * 11 return test_multitouch.Touch(contact_id, x, y)
def make_contacts(self, n, t=0): """
Make multiple touch contacts that can move over time.
Returns a list of `n` touch objects that are positioned at well-known
locations. The value of `t` may be incremented over time to move the
points along a linear path. """ return [self.make_contact(id, t) for id in range(0, n)]
def assert_contact(self, evdev, contact_ids, t=0): """ Assert properties of a contact generated by make_contact. """
contact_id = contact_ids.contact_id
tracking_id = contact_ids.tracking_id
slot_num = contact_ids.slot_num
x = 50 + 10 * contact_id + t * 11
y = 100 + 100 * contact_id + t * 11
# If the data isn't supposed to be stored in any slots, there is # nothing we can check for in the evdev stream. if slot_num isNone: assert tracking_id == -1 return
assert evdev.slots[slot_num][libevdev.EV_ABS.ABS_MT_TRACKING_ID] == tracking_id if tracking_id != -1: assert evdev.slots[slot_num][libevdev.EV_ABS.ABS_MT_POSITION_X] == x assert evdev.slots[slot_num][libevdev.EV_ABS.ABS_MT_POSITION_Y] == y
def assert_contacts(self, evdev, data, t=0): """ Assert properties of a list of contacts generated by make_contacts. """ for contact_ids in data:
self.assert_contact(evdev, contact_ids, t)
def test_contact_id_0(self): """
Bring a finger in contact with the tablet, then hold it down and remove it.
Ensure that even with contact ID = 0 which is usually given as an invalid
touch event by most tablets with the exception of a few, that given the
confidence bit is set to 1 it should process it as a valid touch to cover
the few tablets using contact ID = 0 as a valid touch value. """
uhdev = self.uhdev
evdev = uhdev.get_evdev()
def confidence_change_assert_playback(self, uhdev, evdev, timeline): """ Assert proper behavior of contacts that move and change tipswitch /
confidence status over time.
Given a `timeline` list of touch states to iterate over, verify
that the contacts move and are reported as up/down as expected
by the state of the tipswitch and confidence bits. """
t = 0
for state in timeline:
touches = self.make_contacts(len(state), t)
for item in zip(touches, state):
item[0].tipswitch = item[1][1]
item[0].confidence = item[1][2]
r = uhdev.event(touches)
events = uhdev.next_sync_events()
self.debug_reports(r, uhdev, events)
ids = [x[0] for x in state]
self.assert_contacts(evdev, ids, t)
t += 1
def test_confidence_loss_a(self): """
Transition a confident contact to a non-confident contact by
first clearing the tipswitch.
Ensure that the driver reports the transitioned contact as
being removed and that other contacts continue to report
normally. This mode of confidence loss is used by the
DTH-2452. """
uhdev = self.uhdev
evdev = uhdev.get_evdev()
self.confidence_change_assert_playback(
uhdev,
evdev,
[ # t=0: Contact 0 == Down + confident; Contact 1 == Down + confident # Both fingers confidently in contact
[
(
self.ContactIds(contact_id=0, tracking_id=0, slot_num=0), True, True,
),
(
self.ContactIds(contact_id=1, tracking_id=1, slot_num=1), True, True,
),
], # t=1: Contact 0 == !Down + confident; Contact 1 == Down + confident # First finger looses confidence and clears only the tipswitch flag
[
(
self.ContactIds(contact_id=0, tracking_id=-1, slot_num=0), False, True,
),
(
self.ContactIds(contact_id=1, tracking_id=1, slot_num=1), True, True,
),
], # t=2: Contact 0 == !Down + !confident; Contact 1 == Down + confident # First finger has lost confidence and has both flags cleared
[
(
self.ContactIds(contact_id=0, tracking_id=-1, slot_num=0), False, False,
),
(
self.ContactIds(contact_id=1, tracking_id=1, slot_num=1), True, True,
),
], # t=3: Contact 0 == !Down + !confident; Contact 1 == Down + confident # First finger has lost confidence and has both flags cleared
[
(
self.ContactIds(contact_id=0, tracking_id=-1, slot_num=0), False, False,
),
(
self.ContactIds(contact_id=1, tracking_id=1, slot_num=1), True, True,
),
],
],
)
def test_confidence_loss_b(self): """
Transition a confident contact to a non-confident contact by
cleraing both tipswitch and confidence bits simultaneously.
Ensure that the driver reports the transitioned contact as
being removed and that other contacts continue to report
normally. This mode of confidence loss is used by some
AES devices. """
uhdev = self.uhdev
evdev = uhdev.get_evdev()
self.confidence_change_assert_playback(
uhdev,
evdev,
[ # t=0: Contact 0 == Down + confident; Contact 1 == Down + confident # Both fingers confidently in contact
[
(
self.ContactIds(contact_id=0, tracking_id=0, slot_num=0), True, True,
),
(
self.ContactIds(contact_id=1, tracking_id=1, slot_num=1), True, True,
),
], # t=1: Contact 0 == !Down + !confident; Contact 1 == Down + confident # First finger looses confidence and has both flags cleared simultaneously
[
(
self.ContactIds(contact_id=0, tracking_id=-1, slot_num=0), False, False,
),
(
self.ContactIds(contact_id=1, tracking_id=1, slot_num=1), True, True,
),
], # t=2: Contact 0 == !Down + !confident; Contact 1 == Down + confident # First finger has lost confidence and has both flags cleared
[
(
self.ContactIds(contact_id=0, tracking_id=-1, slot_num=0), False, False,
),
(
self.ContactIds(contact_id=1, tracking_id=1, slot_num=1), True, True,
),
], # t=3: Contact 0 == !Down + !confident; Contact 1 == Down + confident # First finger has lost confidence and has both flags cleared
[
(
self.ContactIds(contact_id=0, tracking_id=-1, slot_num=0), False, False,
),
(
self.ContactIds(contact_id=1, tracking_id=1, slot_num=1), True, True,
),
],
],
)
def test_confidence_loss_c(self): """
Transition a confident contact to a non-confident contact by
clearing only the confidence bit.
Ensure that the driver reports the transitioned contact as
being removed and that other contacts continue to report
normally. """
uhdev = self.uhdev
evdev = uhdev.get_evdev()
self.confidence_change_assert_playback(
uhdev,
evdev,
[ # t=0: Contact 0 == Down + confident; Contact 1 == Down + confident # Both fingers confidently in contact
[
(
self.ContactIds(contact_id=0, tracking_id=0, slot_num=0), True, True,
),
(
self.ContactIds(contact_id=1, tracking_id=1, slot_num=1), True, True,
),
], # t=1: Contact 0 == Down + !confident; Contact 1 == Down + confident # First finger looses confidence and clears only the confidence flag
[
(
self.ContactIds(contact_id=0, tracking_id=-1, slot_num=0), True, False,
),
(
self.ContactIds(contact_id=1, tracking_id=1, slot_num=1), True, True,
),
], # t=2: Contact 0 == !Down + !confident; Contact 1 == Down + confident # First finger has lost confidence and has both flags cleared
[
(
self.ContactIds(contact_id=0, tracking_id=-1, slot_num=0), False, False,
),
(
self.ContactIds(contact_id=1, tracking_id=1, slot_num=1), True, True,
),
], # t=3: Contact 0 == !Down + !confident; Contact 1 == Down + confident # First finger has lost confidence and has both flags cleared
[
(
self.ContactIds(contact_id=0, tracking_id=-1, slot_num=0), False, False,
),
(
self.ContactIds(contact_id=1, tracking_id=1, slot_num=1), True, True,
),
],
],
)
def test_confidence_gain_a(self): """
Transition a contact that was always non-confident to confident.
Ensure that the confident contact is reported normally. """
uhdev = self.uhdev
evdev = uhdev.get_evdev()
self.confidence_change_assert_playback(
uhdev,
evdev,
[ # t=0: Contact 0 == Down + !confident; Contact 1 == Down + confident # Only second finger is confidently in contact
[
(
self.ContactIds(contact_id=0, tracking_id=-1, slot_num=None), True, False,
),
(
self.ContactIds(contact_id=1, tracking_id=0, slot_num=0), True, True,
),
], # t=1: Contact 0 == Down + !confident; Contact 1 == Down + confident # First finger gains confidence
[
(
self.ContactIds(contact_id=0, tracking_id=-1, slot_num=None), True, False,
),
(
self.ContactIds(contact_id=1, tracking_id=0, slot_num=0), True, True,
),
], # t=2: Contact 0 == Down + confident; Contact 1 == Down + confident # First finger remains confident
[
(
self.ContactIds(contact_id=0, tracking_id=1, slot_num=1), True, True,
),
(
self.ContactIds(contact_id=1, tracking_id=0, slot_num=0), True, True,
),
], # t=3: Contact 0 == Down + confident; Contact 1 == Down + confident # First finger remains confident
[
(
self.ContactIds(contact_id=0, tracking_id=1, slot_num=1), True, True,
),
(
self.ContactIds(contact_id=1, tracking_id=0, slot_num=0), True, True,
),
],
],
)
def test_confidence_gain_b(self): """
Transition a contact from non-confident to confident.
Ensure that the confident contact is reported normally. """
uhdev = self.uhdev
evdev = uhdev.get_evdev()
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.