Skip to content

Commit 83912f8

Browse files
author
Benjamin Tissoires
committed
selftests/hid: tablets: define the elements of PenState
This introduces a little bit more readability by not using the raw values but a dedicated Enum Acked-by: Jiri Kosina <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Benjamin Tissoires <[email protected]>
1 parent e08e493 commit 83912f8

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

tools/testing/selftests/hid/tests/test_tablet.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,40 +13,52 @@
1313
import libevdev
1414
import logging
1515
import pytest
16-
from typing import Dict, Tuple
16+
from typing import Dict, Optional, Tuple
1717

1818
logger = logging.getLogger("hidtools.test.tablet")
1919

2020

21+
class BtnTouch(Enum):
22+
"""Represents whether the BTN_TOUCH event is set to True or False"""
23+
24+
DOWN = True
25+
UP = False
26+
27+
28+
class ToolType(Enum):
29+
PEN = libevdev.EV_KEY.BTN_TOOL_PEN
30+
RUBBER = libevdev.EV_KEY.BTN_TOOL_RUBBER
31+
32+
2133
class PenState(Enum):
2234
"""Pen states according to Microsoft reference:
2335
https://docs.microsoft.com/en-us/windows-hardware/design/component-guidelines/windows-pen-states
2436
"""
2537

26-
PEN_IS_OUT_OF_RANGE = (False, None)
27-
PEN_IS_IN_RANGE = (False, libevdev.EV_KEY.BTN_TOOL_PEN)
28-
PEN_IS_IN_CONTACT = (True, libevdev.EV_KEY.BTN_TOOL_PEN)
29-
PEN_IS_IN_RANGE_WITH_ERASING_INTENT = (False, libevdev.EV_KEY.BTN_TOOL_RUBBER)
30-
PEN_IS_ERASING = (True, libevdev.EV_KEY.BTN_TOOL_RUBBER)
38+
PEN_IS_OUT_OF_RANGE = BtnTouch.UP, None
39+
PEN_IS_IN_RANGE = BtnTouch.UP, ToolType.PEN
40+
PEN_IS_IN_CONTACT = BtnTouch.DOWN, ToolType.PEN
41+
PEN_IS_IN_RANGE_WITH_ERASING_INTENT = BtnTouch.UP, ToolType.RUBBER
42+
PEN_IS_ERASING = BtnTouch.DOWN, ToolType.RUBBER
3143

32-
def __init__(self, touch, tool):
44+
def __init__(self, touch: BtnTouch, tool: Optional[ToolType]):
3345
self.touch = touch
3446
self.tool = tool
3547

3648
@classmethod
3749
def from_evdev(cls, evdev) -> "PenState":
38-
touch = bool(evdev.value[libevdev.EV_KEY.BTN_TOUCH])
50+
touch = BtnTouch(evdev.value[libevdev.EV_KEY.BTN_TOUCH])
3951
tool = None
4052
if (
4153
evdev.value[libevdev.EV_KEY.BTN_TOOL_RUBBER]
4254
and not evdev.value[libevdev.EV_KEY.BTN_TOOL_PEN]
4355
):
44-
tool = libevdev.EV_KEY.BTN_TOOL_RUBBER
56+
tool = ToolType(libevdev.EV_KEY.BTN_TOOL_RUBBER)
4557
elif (
4658
evdev.value[libevdev.EV_KEY.BTN_TOOL_PEN]
4759
and not evdev.value[libevdev.EV_KEY.BTN_TOOL_RUBBER]
4860
):
49-
tool = libevdev.EV_KEY.BTN_TOOL_PEN
61+
tool = ToolType(libevdev.EV_KEY.BTN_TOOL_PEN)
5062
elif (
5163
evdev.value[libevdev.EV_KEY.BTN_TOOL_PEN]
5264
or evdev.value[libevdev.EV_KEY.BTN_TOOL_RUBBER]
@@ -68,7 +80,7 @@ def apply(self, events) -> "PenState":
6880
if touch_found:
6981
raise ValueError(f"duplicated BTN_TOUCH in {events}")
7082
touch_found = True
71-
touch = bool(ev.value)
83+
touch = BtnTouch(ev.value)
7284
elif ev in (
7385
libevdev.InputEvent(libevdev.EV_KEY.BTN_TOOL_PEN),
7486
libevdev.InputEvent(libevdev.EV_KEY.BTN_TOOL_RUBBER),
@@ -77,7 +89,7 @@ def apply(self, events) -> "PenState":
7789
raise ValueError(f"duplicated BTN_TOOL_* in {events}")
7890
tool_found = True
7991
if ev.value:
80-
tool = ev.code
92+
tool = ToolType(ev.code)
8193
else:
8294
tool = None
8395

0 commit comments

Comments
 (0)