summaryrefslogtreecommitdiff
path: root/kvmd/plugins/hid/otg/mouse.py
diff options
context:
space:
mode:
Diffstat (limited to 'kvmd/plugins/hid/otg/mouse.py')
-rw-r--r--kvmd/plugins/hid/otg/mouse.py96
1 files changed, 27 insertions, 69 deletions
diff --git a/kvmd/plugins/hid/otg/mouse.py b/kvmd/plugins/hid/otg/mouse.py
index 079a7dc3..3653cda4 100644
--- a/kvmd/plugins/hid/otg/mouse.py
+++ b/kvmd/plugins/hid/otg/mouse.py
@@ -21,48 +21,21 @@
import struct
-import dataclasses
from typing import Optional
from typing import Any
from ....logging import get_logger
-from .device import BaseEvent
from .device import BaseDeviceProcess
-
-# =====
-class _ClearEvent(BaseEvent):
- pass
-
-
-class _ResetEvent(BaseEvent):
- pass
-
-
[email protected](frozen=True)
-class _ButtonEvent(BaseEvent):
- code: int
- state: bool
-
-
[email protected](frozen=True)
-class _MoveEvent(BaseEvent):
- to_x: int
- to_y: int
-
-
[email protected](frozen=True)
-class _RelativeEvent(BaseEvent):
- delta_x: int
- delta_y: int
-
-
[email protected](frozen=True)
-class _WheelEvent(BaseEvent):
- delta_x: int
- delta_y: int
+from .events import BaseEvent
+from .events import ClearEvent
+from .events import ResetEvent
+from .events import MouseButtonEvent
+from .events import MouseMoveEvent
+from .events import MouseRelativeEvent
+from .events import MouseWheelEvent
# =====
@@ -93,55 +66,40 @@ class MouseProcess(BaseDeviceProcess):
def send_clear_event(self) -> None:
self._clear_queue()
- self._queue_event(_ClearEvent())
+ self._queue_event(ClearEvent())
def send_reset_event(self) -> None:
self._clear_queue()
- self._queue_event(_ResetEvent())
+ self._queue_event(ResetEvent())
def send_button_event(self, button: str, state: bool) -> None:
- code: int = {
- "left": 0x1,
- "right": 0x2,
- "middle": 0x4,
- "up": 0x8, # Back
- "down": 0x10, # Forward
- }[button]
- self._queue_event(_ButtonEvent(code, state))
+ self._queue_event(MouseButtonEvent(button, state))
def send_move_event(self, to_x: int, to_y: int) -> None:
if self.__absolute:
- assert -32768 <= to_x <= 32767
- assert -32768 <= to_y <= 32767
- to_x = (to_x + 32768) // 2
- to_y = (to_y + 32768) // 2
- self._queue_event(_MoveEvent(to_x, to_y))
+ self._queue_event(MouseMoveEvent(to_x, to_y))
def send_relative_event(self, delta_x: int, delta_y: int) -> None:
if not self.__absolute:
- assert -127 <= delta_x <= 127
- assert -127 <= delta_y <= 127
- self._queue_event(_RelativeEvent(delta_x, delta_y))
+ self._queue_event(MouseRelativeEvent(delta_x, delta_y))
def send_wheel_event(self, delta_x: int, delta_y: int) -> None:
- assert -127 <= delta_x <= 127
- assert -127 <= delta_y <= 127
- self._queue_event(_WheelEvent(delta_x, delta_y))
+ self._queue_event(MouseWheelEvent(delta_x, delta_y))
# =====
def _process_event(self, event: BaseEvent) -> bool:
- if isinstance(event, _ClearEvent):
+ if isinstance(event, ClearEvent):
return self.__process_clear_event()
- elif isinstance(event, _ResetEvent):
+ elif isinstance(event, ResetEvent):
return self.__process_clear_event(reopen=True)
- elif isinstance(event, _ButtonEvent):
+ elif isinstance(event, MouseButtonEvent):
return self.__process_button_event(event)
- elif isinstance(event, _MoveEvent):
+ elif isinstance(event, MouseMoveEvent):
return self.__process_move_event(event)
- elif isinstance(event, _RelativeEvent):
+ elif isinstance(event, MouseRelativeEvent):
return self.__process_relative_event(event)
- elif isinstance(event, _WheelEvent):
+ elif isinstance(event, MouseWheelEvent):
return self.__process_wheel_event(event)
raise RuntimeError(f"Not implemented event: {event}")
@@ -149,7 +107,7 @@ class MouseProcess(BaseDeviceProcess):
self.__clear_state()
return self.__send_current_state(reopen=reopen)
- def __process_button_event(self, event: _ButtonEvent) -> bool:
+ def __process_button_event(self, event: MouseButtonEvent) -> bool:
if event.code & self.__pressed_buttons:
# Ранее нажатую кнопку отжимаем
self.__pressed_buttons &= ~event.code
@@ -161,23 +119,23 @@ class MouseProcess(BaseDeviceProcess):
return self.__send_current_state()
return True
- def __process_move_event(self, event: _MoveEvent) -> bool:
- self.__x = event.to_x
- self.__y = event.to_y
+ def __process_move_event(self, event: MouseMoveEvent) -> bool:
+ self.__x = event.to_fixed_x
+ self.__y = event.to_fixed_y
return self.__send_current_state()
- def __process_relative_event(self, event: _RelativeEvent) -> bool:
+ def __process_relative_event(self, event: MouseRelativeEvent) -> bool:
return self.__send_current_state(relative_event=event)
- def __process_wheel_event(self, event: _WheelEvent) -> bool:
+ def __process_wheel_event(self, event: MouseWheelEvent) -> bool:
return self.__send_current_state(wheel_event=event)
# =====
def __send_current_state(
self,
- relative_event: Optional[_RelativeEvent]=None,
- wheel_event: Optional[_WheelEvent]=None,
+ relative_event: Optional[MouseRelativeEvent]=None,
+ wheel_event: Optional[MouseWheelEvent]=None,
reopen: bool=False,
) -> bool: