selftests/hid: sync the python tests to hid-tools 0.8

Instead of backporting one by one each commits, let's pull them in bulk
and refer to the hid-tools project for a detailed history.

The short summary is:
- make use of dataclass when possible, to avoid tuples
- wacom: remove unused uhdev parameter
- various small fixes not worth mentioning

Reviewed-by: Peter Hutterer <peter.hutterer@who-t.net>
Link: https://patch.msgid.link/20250709-wip-fix-ci-v1-2-b7df4c271cf8@kernel.org
Signed-off-by: Benjamin Tissoires <bentiss@kernel.org>
This commit is contained in:
Benjamin Tissoires
2025-07-09 11:08:50 +02:00
parent c85a8cb9b8
commit 642f9b2d60
9 changed files with 69 additions and 43 deletions

View File

@@ -18,6 +18,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import dataclasses
import fcntl
import functools
import libevdev
@@ -104,6 +105,12 @@ class PowerSupply(object):
return self._type.str_value
@dataclasses.dataclass
class HidReadiness:
is_ready: bool = False
count: int = 0
class HIDIsReady(object):
"""
Companion class that binds to a kernel mechanism
@@ -115,18 +122,18 @@ class HIDIsReady(object):
def __init__(self: "HIDIsReady", uhid: UHIDDevice) -> None:
self.uhid = uhid
def is_ready(self: "HIDIsReady") -> bool:
def is_ready(self: "HIDIsReady") -> HidReadiness:
"""
Overwrite in subclasses: should return True or False whether
the attached uhid device is ready or not.
"""
return False
return HidReadiness()
class UdevHIDIsReady(HIDIsReady):
_pyudev_context: ClassVar[Optional[pyudev.Context]] = None
_pyudev_monitor: ClassVar[Optional[pyudev.Monitor]] = None
_uhid_devices: ClassVar[Dict[int, Tuple[bool, int]]] = {}
_uhid_devices: ClassVar[Dict[int, HidReadiness]] = {}
def __init__(self: "UdevHIDIsReady", uhid: UHIDDevice) -> None:
super().__init__(uhid)
@@ -157,18 +164,19 @@ class UdevHIDIsReady(HIDIsReady):
id = int(event.sys_path.strip().split(".")[-1], 16)
device_ready, count = cls._uhid_devices.get(id, (False, 0))
readiness = cls._uhid_devices.setdefault(id, HidReadiness())
ready = event.action == "bind"
if not device_ready and ready:
count += 1
cls._uhid_devices[id] = (ready, count)
if not readiness.is_ready and ready:
readiness.count += 1
def is_ready(self: "UdevHIDIsReady") -> Tuple[bool, int]:
readiness.is_ready = ready
def is_ready(self: "UdevHIDIsReady") -> HidReadiness:
try:
return self._uhid_devices[self.uhid.hid_id]
except KeyError:
return (False, 0)
return HidReadiness()
class EvdevMatch(object):
@@ -322,11 +330,11 @@ class BaseDevice(UHIDDevice):
@property
def kernel_is_ready(self: "BaseDevice") -> bool:
return self._kernel_is_ready.is_ready()[0] and self.started
return self._kernel_is_ready.is_ready().is_ready and self.started
@property
def kernel_ready_count(self: "BaseDevice") -> int:
return self._kernel_is_ready.is_ready()[1]
return self._kernel_is_ready.is_ready().count
@property
def input_nodes(self: "BaseDevice") -> List[EvdevDevice]: