linux-input.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Benjamin Tissoires <bentiss@kernel.org>
To: Jiri Kosina <jikos@kernel.org>,
	 Benjamin Tissoires <benjamin.tissoires@redhat.com>,
	 Shuah Khan <shuah@kernel.org>,
	Peter Hutterer <peter.hutterer@who-t.net>
Cc: linux-input@vger.kernel.org, linux-kselftest@vger.kernel.org,
	 linux-kernel@vger.kernel.org,
	Benjamin Tissoires <bentiss@kernel.org>
Subject: [PATCH v2 09/15] selftests/hid: tablets: define the elements of PenState
Date: Wed, 06 Dec 2023 11:46:00 +0100	[thread overview]
Message-ID: <20231206-wip-selftests-v2-9-c0350c2f5986@kernel.org> (raw)
In-Reply-To: <20231206-wip-selftests-v2-0-c0350c2f5986@kernel.org>

This introduces a little bit more readability by not using the raw values
but a dedicated Enum

Signed-off-by: Benjamin Tissoires <bentiss@kernel.org>

---

new in v2
---
 tools/testing/selftests/hid/tests/test_tablet.py | 36 ++++++++++++++++--------
 1 file changed, 24 insertions(+), 12 deletions(-)

diff --git a/tools/testing/selftests/hid/tests/test_tablet.py b/tools/testing/selftests/hid/tests/test_tablet.py
index 0ddf82695ed9..cec65294c9ec 100644
--- a/tools/testing/selftests/hid/tests/test_tablet.py
+++ b/tools/testing/selftests/hid/tests/test_tablet.py
@@ -13,40 +13,52 @@ from hidtools.util import BusType
 import libevdev
 import logging
 import pytest
-from typing import Dict, Tuple
+from typing import Dict, Optional, Tuple
 
 logger = logging.getLogger("hidtools.test.tablet")
 
 
+class BtnTouch(Enum):
+    """Represents whether the BTN_TOUCH event is set to True or False"""
+
+    DOWN = True
+    UP = False
+
+
+class ToolType(Enum):
+    PEN = libevdev.EV_KEY.BTN_TOOL_PEN
+    RUBBER = libevdev.EV_KEY.BTN_TOOL_RUBBER
+
+
 class PenState(Enum):
     """Pen states according to Microsoft reference:
     https://docs.microsoft.com/en-us/windows-hardware/design/component-guidelines/windows-pen-states
     """
 
-    PEN_IS_OUT_OF_RANGE = (False, None)
-    PEN_IS_IN_RANGE = (False, libevdev.EV_KEY.BTN_TOOL_PEN)
-    PEN_IS_IN_CONTACT = (True, libevdev.EV_KEY.BTN_TOOL_PEN)
-    PEN_IS_IN_RANGE_WITH_ERASING_INTENT = (False, libevdev.EV_KEY.BTN_TOOL_RUBBER)
-    PEN_IS_ERASING = (True, libevdev.EV_KEY.BTN_TOOL_RUBBER)
+    PEN_IS_OUT_OF_RANGE = BtnTouch.UP, None
+    PEN_IS_IN_RANGE = BtnTouch.UP, ToolType.PEN
+    PEN_IS_IN_CONTACT = BtnTouch.DOWN, ToolType.PEN
+    PEN_IS_IN_RANGE_WITH_ERASING_INTENT = BtnTouch.UP, ToolType.RUBBER
+    PEN_IS_ERASING = BtnTouch.DOWN, ToolType.RUBBER
 
-    def __init__(self, touch, tool):
+    def __init__(self, touch: BtnTouch, tool: Optional[ToolType]):
         self.touch = touch
         self.tool = tool
 
     @classmethod
     def from_evdev(cls, evdev) -> "PenState":
-        touch = bool(evdev.value[libevdev.EV_KEY.BTN_TOUCH])
+        touch = BtnTouch(evdev.value[libevdev.EV_KEY.BTN_TOUCH])
         tool = None
         if (
             evdev.value[libevdev.EV_KEY.BTN_TOOL_RUBBER]
             and not evdev.value[libevdev.EV_KEY.BTN_TOOL_PEN]
         ):
-            tool = libevdev.EV_KEY.BTN_TOOL_RUBBER
+            tool = ToolType(libevdev.EV_KEY.BTN_TOOL_RUBBER)
         elif (
             evdev.value[libevdev.EV_KEY.BTN_TOOL_PEN]
             and not evdev.value[libevdev.EV_KEY.BTN_TOOL_RUBBER]
         ):
-            tool = libevdev.EV_KEY.BTN_TOOL_PEN
+            tool = ToolType(libevdev.EV_KEY.BTN_TOOL_PEN)
         elif (
             evdev.value[libevdev.EV_KEY.BTN_TOOL_PEN]
             or evdev.value[libevdev.EV_KEY.BTN_TOOL_RUBBER]
@@ -68,7 +80,7 @@ class PenState(Enum):
                 if touch_found:
                     raise ValueError(f"duplicated BTN_TOUCH in {events}")
                 touch_found = True
-                touch = bool(ev.value)
+                touch = BtnTouch(ev.value)
             elif ev in (
                 libevdev.InputEvent(libevdev.EV_KEY.BTN_TOOL_PEN),
                 libevdev.InputEvent(libevdev.EV_KEY.BTN_TOOL_RUBBER),
@@ -77,7 +89,7 @@ class PenState(Enum):
                     raise ValueError(f"duplicated BTN_TOOL_* in {events}")
                 tool_found = True
                 if ev.value:
-                    tool = ev.code
+                    tool = ToolType(ev.code)
                 else:
                     tool = None
 

-- 
2.41.0


  parent reply	other threads:[~2023-12-06 10:46 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-06 10:45 [PATCH v2 00/15] selftests/hid: tablets fixes Benjamin Tissoires
2023-12-06 10:45 ` [PATCH v2 01/15] selftests/hid: vmtest.sh: update vm2c and container Benjamin Tissoires
2023-12-06 10:45 ` [PATCH v2 02/15] selftests/hid: vmtest.sh: allow finer control on the build steps Benjamin Tissoires
2023-12-06 10:45 ` [PATCH v2 03/15] selftests/hid: base: allow for multiple skip_if_uhdev Benjamin Tissoires
2023-12-06 10:45 ` [PATCH v2 04/15] selftests/hid: tablets: remove unused class Benjamin Tissoires
2023-12-06 10:45 ` [PATCH v2 05/15] selftests/hid: tablets: move the transitions to PenState Benjamin Tissoires
2023-12-06 10:45 ` [PATCH v2 06/15] selftests/hid: tablets: move move_to function to PenDigitizer Benjamin Tissoires
2023-12-06 10:45 ` [PATCH v2 07/15] selftests/hid: tablets: do not set invert when the eraser is used Benjamin Tissoires
2023-12-06 10:45 ` [PATCH v2 08/15] selftests/hid: tablets: set initial data for tilt/twist Benjamin Tissoires
2023-12-06 10:46 ` Benjamin Tissoires [this message]
2023-12-06 10:46 ` [PATCH v2 10/15] selftests/hid: tablets: add variants of states with buttons Benjamin Tissoires
2023-12-06 10:46 ` [PATCH v2 11/15] selftests/hid: tablets: convert the primary button tests Benjamin Tissoires
2023-12-06 10:46 ` [PATCH v2 12/15] selftests/hid: tablets: add a secondary barrel switch test Benjamin Tissoires
2023-12-06 10:46 ` [PATCH v2 13/15] selftests/hid: tablets: be stricter for some transitions Benjamin Tissoires
2023-12-06 10:46 ` [PATCH v2 14/15] selftests/hid: fix mypy complains Benjamin Tissoires
2023-12-06 10:46 ` [PATCH v2 15/15] selftests/hid: fix ruff linter complains Benjamin Tissoires
2023-12-06 23:32 ` [PATCH v2 00/15] selftests/hid: tablets fixes Jiri Kosina
2023-12-07  8:53 ` Benjamin Tissoires
2023-12-07  8:56   ` Benjamin Tissoires

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231206-wip-selftests-v2-9-c0350c2f5986@kernel.org \
    --to=bentiss@kernel.org \
    --cc=benjamin.tissoires@redhat.com \
    --cc=jikos@kernel.org \
    --cc=linux-input@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=peter.hutterer@who-t.net \
    --cc=shuah@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).