summaryrefslogtreecommitdiff
path: root/kvmd
diff options
context:
space:
mode:
authorDevaev Maxim <[email protected]>2019-10-01 17:32:54 +0300
committerDevaev Maxim <[email protected]>2019-10-01 17:32:54 +0300
commit56ae6a5d90fda2031bb225624bb98b1eea2c8aeb (patch)
tree1657f2760204d8fee16f5f2d7eb8bb9bbf71604e /kvmd
parent05e9dca03c948cc641057a94d150323cb0bea150 (diff)
refactoring
Diffstat (limited to 'kvmd')
-rw-r--r--kvmd/plugins/hid/otg/keyboard.py79
-rw-r--r--kvmd/plugins/hid/otg/mouse.py36
2 files changed, 64 insertions, 51 deletions
diff --git a/kvmd/plugins/hid/otg/keyboard.py b/kvmd/plugins/hid/otg/keyboard.py
index cd8a1f5f..fab7b992 100644
--- a/kvmd/plugins/hid/otg/keyboard.py
+++ b/kvmd/plugins/hid/otg/keyboard.py
@@ -45,10 +45,22 @@ class _ResetEvent(BaseEvent):
@dataclasses.dataclass(frozen=True)
+class _ModifierEvent(BaseEvent):
+ modifier: keymap.OtgKey
+ state: bool
+
+ def __post_init__(self) -> None:
+ assert self.modifier.is_modifier
+
+
[email protected](frozen=True)
class _KeyEvent(BaseEvent):
key: keymap.OtgKey
state: bool
+ def __post_init__(self) -> None:
+ assert not self.key.is_modifier
+
# =====
class KeyboardProcess(BaseDeviceProcess):
@@ -74,8 +86,11 @@ class KeyboardProcess(BaseDeviceProcess):
self._queue_event(_ResetEvent())
def send_key_event(self, key: str, state: bool) -> None:
- assert key in keymap.KEYMAP
- self._queue_event(_KeyEvent(key=keymap.KEYMAP[key].otg, state=state))
+ otg_key = keymap.KEYMAP[key].otg
+ if otg_key.is_modifier:
+ self._queue_event(_ModifierEvent(otg_key, state))
+ else:
+ self._queue_event(_KeyEvent(otg_key, state))
# =====
@@ -84,6 +99,8 @@ class KeyboardProcess(BaseDeviceProcess):
self.__process_clear_event()
elif isinstance(event, _ResetEvent):
self.__process_clear_event(reopen=True)
+ elif isinstance(event, _ModifierEvent):
+ self.__process_modifier_event(event)
elif isinstance(event, _KeyEvent):
self.__process_key_event(event)
@@ -94,41 +111,41 @@ class KeyboardProcess(BaseDeviceProcess):
self._close_device()
self.__send_current_state()
+ def __process_modifier_event(self, event: _ModifierEvent) -> None:
+ if event.modifier in self.__pressed_modifiers:
+ # Ранее нажатый модификатор отжимаем
+ self.__pressed_modifiers.remove(event.modifier)
+ if not self.__send_current_state():
+ return
+ if event.state:
+ # Нажимаем если нужно
+ self.__pressed_modifiers.add(event.modifier)
+ self.__send_current_state()
+
def __process_key_event(self, event: _KeyEvent) -> None:
- if event.key.is_modifier:
- if event.key in self.__pressed_modifiers:
- # Ранее нажатый модификатор отжимаем
- self.__pressed_modifiers.remove(event.key)
- if not self.__send_current_state():
- return
- if event.state:
- # Нажимаем если нужно
- self.__pressed_modifiers.add(event.key)
- self.__send_current_state()
-
- else: # regular key
- if event.key in self.__pressed_keys:
- # Ранее нажатую клавишу отжимаем
- self.__pressed_keys[self.__pressed_keys.index(event.key)] = None
- if not self.__send_current_state():
- return
- elif event.state and None not in self.__pressed_keys:
- # Если нужно нажать что-то новое, но свободных слотов нет - отжимаем всё
- self.__clear_keys()
- if not self.__send_current_state():
- return
- if event.state:
- # Нажимаем если нужно
- self.__pressed_keys[self.__pressed_keys.index(None)] = event.key
- self.__send_current_state()
+ if event.key in self.__pressed_keys:
+ # Ранее нажатую клавишу отжимаем
+ self.__pressed_keys[self.__pressed_keys.index(event.key)] = None
+ if not self.__send_current_state():
+ return
+ elif event.state and None not in self.__pressed_keys:
+ # Если нужно нажать что-то новое, но свободных слотов нет - отжимаем всё
+ self.__clear_keys()
+ if not self.__send_current_state():
+ return
+ if event.state:
+ # Нажимаем если нужно
+ self.__pressed_keys[self.__pressed_keys.index(None)] = event.key
+ self.__send_current_state()
+
+ # =====
def __send_current_state(self) -> bool:
ok = False
if self._ensure_device():
modifiers = 0
- for key in self.__pressed_modifiers:
- assert key.is_modifier
- modifiers |= key.code
+ for modifier in self.__pressed_modifiers:
+ modifiers |= modifier.code
assert len(self.__pressed_keys) == 6
keys = [
diff --git a/kvmd/plugins/hid/otg/mouse.py b/kvmd/plugins/hid/otg/mouse.py
index 7c201ab6..4c9facaf 100644
--- a/kvmd/plugins/hid/otg/mouse.py
+++ b/kvmd/plugins/hid/otg/mouse.py
@@ -32,14 +32,6 @@ from .device import BaseDeviceProcess
# =====
-_BUTTONS = {
- "left": 0x1,
- "right": 0x2,
- "middle": 0x4,
-}
-
-
-# =====
class _ClearEvent(BaseEvent):
pass
@@ -59,20 +51,12 @@ class _MoveEvent(BaseEvent):
to_x: int
to_y: int
- def __post_init__(self) -> None:
- assert -32768 <= self.to_x <= 32767
- assert -32768 <= self.to_y <= 32767
-
@dataclasses.dataclass(frozen=True)
class _WheelEvent(BaseEvent):
delta_x: int
delta_y: int
- def __post_init__(self) -> None:
- assert -127 <= self.delta_x <= 127
- assert -127 <= self.delta_y <= 127
-
# =====
class MouseProcess(BaseDeviceProcess):
@@ -99,13 +83,23 @@ class MouseProcess(BaseDeviceProcess):
self._queue_event(_ResetEvent())
def send_button_event(self, button: str, state: bool) -> None:
- assert button in _BUTTONS
- self._queue_event(_ButtonEvent(code=_BUTTONS[button], state=state))
+ code: int = {
+ "left": 0x1,
+ "right": 0x2,
+ "middle": 0x4,
+ }[button]
+ self._queue_event(_ButtonEvent(code, state))
def send_move_event(self, to_x: int, to_y: int) -> None:
+ 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))
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))
# =====
@@ -147,6 +141,8 @@ class MouseProcess(BaseDeviceProcess):
def __process_wheel_event(self, event: _WheelEvent) -> None:
self.__send_current_state(event.delta_x, event.delta_y)
+ # =====
+
def __send_current_state(self, delta_x: int, delta_y: int) -> bool:
ok = False
if self._ensure_device():
@@ -167,6 +163,6 @@ class MouseProcess(BaseDeviceProcess):
self.__y = 0
def __make_report(self, buttons: int, to_x: int, to_y: int, delta_x: int, delta_y: int) -> bytes:
- to_x = (to_x + 32768) // 2
- to_y = (to_y + 32768) // 2
+ # XXX: Delta Y before X: it's ok.
+ # See /kvmd/apps/otg/hid/keyboard.py for details
return struct.pack("<BHHbb", buttons, to_x, to_y, delta_y, delta_x)