summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMaxim Devaev <[email protected]>2025-01-10 23:04:12 +0200
committerMaxim Devaev <[email protected]>2025-01-10 23:04:12 +0200
commite7c06643b4a43c1d35bd2c3aaf4326547084b091 (patch)
tree7abb1ac834d3fa1685bf14af31a596397911b0c5
parent72c9ae3aa00b8817fab2b8243203ca6e1c25e146 (diff)
refactoring
-rw-r--r--kvmd/mouse.py10
-rw-r--r--kvmd/plugins/hid/_mcu/proto.py9
-rw-r--r--kvmd/plugins/hid/ch9329/mouse.py5
-rw-r--r--kvmd/plugins/hid/otg/events.py9
-rw-r--r--kvmd/validators/hid.py5
5 files changed, 26 insertions, 12 deletions
diff --git a/kvmd/mouse.py b/kvmd/mouse.py
index 3fb76800..399c6a33 100644
--- a/kvmd/mouse.py
+++ b/kvmd/mouse.py
@@ -36,3 +36,13 @@ class MouseRange:
@classmethod
def normalize(cls, value: int) -> int:
return min(max(cls.MIN, value), cls.MAX)
+
+
+class MouseDelta:
+ MIN = -127
+ MAX = 127
+ RANGE = (MIN, MAX)
+
+ @classmethod
+ def normalize(cls, value: int) -> int:
+ return min(max(cls.MIN, value), cls.MAX)
diff --git a/kvmd/plugins/hid/_mcu/proto.py b/kvmd/plugins/hid/_mcu/proto.py
index deaf5c15..315c1f89 100644
--- a/kvmd/plugins/hid/_mcu/proto.py
+++ b/kvmd/plugins/hid/_mcu/proto.py
@@ -26,6 +26,7 @@ import struct
from ....keyboard.mappings import KEYMAP
from ....mouse import MouseRange
+from ....mouse import MouseDelta
from .... import tools
from .... import bitbang
@@ -162,8 +163,8 @@ class MouseRelativeEvent(BaseEvent):
delta_y: int
def __post_init__(self) -> None:
- assert -127 <= self.delta_x <= 127
- assert -127 <= self.delta_y <= 127
+ assert MouseDelta.MIN <= self.delta_x <= MouseDelta.MAX
+ assert MouseDelta.MIN <= self.delta_y <= MouseDelta.MAX
def make_request(self) -> bytes:
return _make_request(struct.pack(">Bbbxx", 0x15, self.delta_x, self.delta_y))
@@ -175,8 +176,8 @@ class MouseWheelEvent(BaseEvent):
delta_y: int
def __post_init__(self) -> None:
- assert -127 <= self.delta_x <= 127
- assert -127 <= self.delta_y <= 127
+ assert MouseDelta.MIN <= self.delta_x <= MouseDelta.MAX
+ assert MouseDelta.MIN <= self.delta_y <= MouseDelta.MAX
def make_request(self) -> bytes:
# Горизонтальная прокрутка пока не поддерживается
diff --git a/kvmd/plugins/hid/ch9329/mouse.py b/kvmd/plugins/hid/ch9329/mouse.py
index 0a0cfbcc..d61bab4e 100644
--- a/kvmd/plugins/hid/ch9329/mouse.py
+++ b/kvmd/plugins/hid/ch9329/mouse.py
@@ -23,6 +23,7 @@
import math
from ....mouse import MouseRange
+from ....mouse import MouseDelta
# =====
@@ -79,7 +80,7 @@ class Mouse: # pylint: disable=too-many-instance-attributes
def process_wheel(self, delta_x: int, delta_y: int) -> bytes:
_ = delta_x
- assert -127 <= delta_y <= 127
+ assert MouseDelta.MIN <= delta_y <= MouseDelta.MAX
self.__wheel_y = (1 if delta_y > 0 else 255)
if not self.__absolute:
return self.__make_relative_cmd()
@@ -110,6 +111,6 @@ class Mouse: # pylint: disable=too-many-instance-attributes
])
def __fix_relative(self, value: int) -> int:
- assert -127 <= value <= 127
+ assert MouseDelta.MIN <= value <= MouseDelta.MAX
value = math.ceil(value / 3)
return (value if value >= 0 else (255 + value))
diff --git a/kvmd/plugins/hid/otg/events.py b/kvmd/plugins/hid/otg/events.py
index ae2ba67d..44f5e373 100644
--- a/kvmd/plugins/hid/otg/events.py
+++ b/kvmd/plugins/hid/otg/events.py
@@ -27,6 +27,7 @@ from ....keyboard.mappings import UsbKey
from ....keyboard.mappings import KEYMAP
from ....mouse import MouseRange
+from ....mouse import MouseDelta
# =====
@@ -144,8 +145,8 @@ class MouseRelativeEvent(BaseEvent):
delta_y: int
def __post_init__(self) -> None:
- assert -127 <= self.delta_x <= 127
- assert -127 <= self.delta_y <= 127
+ assert MouseDelta.MIN <= self.delta_x <= MouseDelta.MAX
+ assert MouseDelta.MIN <= self.delta_y <= MouseDelta.MAX
@dataclasses.dataclass(frozen=True)
@@ -154,8 +155,8 @@ class MouseWheelEvent(BaseEvent):
delta_y: int
def __post_init__(self) -> None:
- assert -127 <= self.delta_x <= 127
- assert -127 <= self.delta_y <= 127
+ assert MouseDelta.MIN <= self.delta_x <= MouseDelta.MAX
+ assert MouseDelta.MIN <= self.delta_y <= MouseDelta.MAX
def make_mouse_report(
diff --git a/kvmd/validators/hid.py b/kvmd/validators/hid.py
index 376a7f62..0f74a6eb 100644
--- a/kvmd/validators/hid.py
+++ b/kvmd/validators/hid.py
@@ -25,6 +25,7 @@ from typing import Any
from ..keyboard.mappings import KEYMAP
from ..mouse import MouseRange
+from ..mouse import MouseDelta
from . import check_string_in_list
@@ -46,7 +47,7 @@ def valid_hid_key(arg: Any) -> str:
def valid_hid_mouse_move(arg: Any) -> int:
arg = valid_number(arg, name="Mouse move")
- return min(max(MouseRange.MIN, arg), MouseRange.MAX)
+ return MouseRange.normalize(arg)
def valid_hid_mouse_button(arg: Any) -> str:
@@ -55,4 +56,4 @@ def valid_hid_mouse_button(arg: Any) -> str:
def valid_hid_mouse_delta(arg: Any) -> int:
arg = valid_number(arg, name="Mouse delta")
- return min(max(-127, arg), 127)
+ return MouseDelta.normalize(arg)