Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mdevaev committed Jan 10, 2025
1 parent 72c9ae3 commit e7c0664
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 12 deletions.
10 changes: 10 additions & 0 deletions kvmd/mouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,13 @@ def remap(cls, value: int, out_min: int, out_max: int) -> int:
@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)
9 changes: 5 additions & 4 deletions kvmd/plugins/hid/_mcu/proto.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from ....keyboard.mappings import KEYMAP

from ....mouse import MouseRange
from ....mouse import MouseDelta

from .... import tools
from .... import bitbang
Expand Down Expand Up @@ -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))
Expand All @@ -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:
# Горизонтальная прокрутка пока не поддерживается
Expand Down
5 changes: 3 additions & 2 deletions kvmd/plugins/hid/ch9329/mouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import math

from ....mouse import MouseRange
from ....mouse import MouseDelta


# =====
Expand Down Expand Up @@ -79,7 +80,7 @@ def __fix_absolute(self, value: int) -> tuple[int, int]:

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()
Expand Down Expand Up @@ -110,6 +111,6 @@ def __make_relative_cmd(self) -> bytes:
])

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))
9 changes: 5 additions & 4 deletions kvmd/plugins/hid/otg/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from ....keyboard.mappings import KEYMAP

from ....mouse import MouseRange
from ....mouse import MouseDelta


# =====
Expand Down Expand Up @@ -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)
Expand All @@ -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(
Expand Down
5 changes: 3 additions & 2 deletions kvmd/validators/hid.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from ..keyboard.mappings import KEYMAP

from ..mouse import MouseRange
from ..mouse import MouseDelta

from . import check_string_in_list

Expand All @@ -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:
Expand All @@ -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)

0 comments on commit e7c0664

Please sign in to comment.