diff options
author | Devaev Maxim <[email protected]> | 2020-02-08 03:34:43 +0300 |
---|---|---|
committer | Devaev Maxim <[email protected]> | 2020-02-08 03:34:43 +0300 |
commit | 635cfc7d9e3b57eee608c34f789ed6a7bfec8962 (patch) | |
tree | facabdb816f6d302188d859e49c8a825241aabd0 | |
parent | 301e58148e5b2140e2a9e96dbaa04b27d6bd8b6e (diff) |
fixed hid ensuring
-rw-r--r-- | kvmd/plugins/hid/otg/device.py | 37 | ||||
-rw-r--r-- | kvmd/plugins/hid/otg/keyboard.py | 44 | ||||
-rw-r--r-- | kvmd/plugins/hid/otg/mouse.py | 34 |
3 files changed, 55 insertions, 60 deletions
diff --git a/kvmd/plugins/hid/otg/device.py b/kvmd/plugins/hid/otg/device.py index fbd91f59..7da6dd18 100644 --- a/kvmd/plugins/hid/otg/device.py +++ b/kvmd/plugins/hid/otg/device.py @@ -75,25 +75,19 @@ class BaseDeviceProcess(multiprocessing.Process): # pylint: disable=too-many-in while not self.__stop_event.is_set(): try: while not self.__stop_event.is_set(): - passed = 0 try: - event: BaseEvent = self.__events_queue.get(timeout=0.05) + event: BaseEvent = self.__events_queue.get(timeout=1) except queue.Empty: - if passed >= 20: # 20 * 0.05 = 1 sec - self._ensure_device() # Check device - passed = 0 - else: - passed += 1 + self.__ensure_device() # Check device else: self._process_event(event) - passed = 0 except Exception: logger.exception("Unexpected HID-%s error", self.__name) - self._close_device() + self.__close_device() finally: time.sleep(1) - self._close_device() + self.__close_device() def is_online(self) -> bool: return bool(self.__online_shared.value and self.is_alive()) @@ -111,7 +105,20 @@ class BaseDeviceProcess(multiprocessing.Process): # pylint: disable=too-many-in def _queue_event(self, event: BaseEvent) -> None: self.__events_queue.put(event) - def _write_report(self, report: bytes) -> bool: + def _ensure_write(self, report: bytes, reopen: bool=False, close: bool=False) -> bool: + if reopen: + self.__close_device() + try: + if self.__ensure_device(): + return self.__write_report(report) + return False + finally: + if close: + self.__close_device() + + # ===== + + def __write_report(self, report: bytes) -> bool: if self.__noop: return True @@ -141,10 +148,10 @@ class BaseDeviceProcess(multiprocessing.Process): # pylint: disable=too-many-in logger.debug("HID-%s write retries left: %d", self.__name, retries) time.sleep(self.__write_retries_delay) - self._close_device() + self.__close_device() return False - def _ensure_device(self) -> bool: + def __ensure_device(self) -> bool: if self.__noop: return True @@ -168,12 +175,12 @@ class BaseDeviceProcess(multiprocessing.Process): # pylint: disable=too-many-in logger.debug("HID-%s is busy/unplugged (select)", self.__name) except Exception as err: logger.error("Can't select() HID-%s: %s: %s", self.__name, type(err).__name__, err) - self._close_device() + self.__close_device() self.__online_shared.value = 0 return False - def _close_device(self) -> None: + def __close_device(self) -> None: if self.__fd >= 0: try: os.close(self.__fd) diff --git a/kvmd/plugins/hid/otg/keyboard.py b/kvmd/plugins/hid/otg/keyboard.py index fab7b992..6cf8f72d 100644 --- a/kvmd/plugins/hid/otg/keyboard.py +++ b/kvmd/plugins/hid/otg/keyboard.py @@ -73,11 +73,7 @@ class KeyboardProcess(BaseDeviceProcess): def cleanup(self) -> None: self._stop() get_logger().info("Clearing HID-keyboard events ...") - if self._ensure_device(): - try: - self._write_report(b"\x00" * 8) # Release all keys and modifiers - finally: - self._close_device() + self._ensure_write(b"\x00" * 8, close=True) # Release all keys and modifiers def send_clear_event(self) -> None: self._queue_event(_ClearEvent()) @@ -107,9 +103,7 @@ class KeyboardProcess(BaseDeviceProcess): def __process_clear_event(self, reopen: bool=False) -> None: self.__clear_modifiers() self.__clear_keys() - if reopen: - self._close_device() - self.__send_current_state() + self.__send_current_state(reopen=reopen) def __process_modifier_event(self, event: _ModifierEvent) -> None: if event.modifier in self.__pressed_modifiers: @@ -140,28 +134,28 @@ class KeyboardProcess(BaseDeviceProcess): # ===== - def __send_current_state(self) -> bool: - ok = False - if self._ensure_device(): - modifiers = 0 - for modifier in self.__pressed_modifiers: - modifiers |= modifier.code - - assert len(self.__pressed_keys) == 6 - keys = [ - (0 if key is None else key.code) - for key in self.__pressed_keys - ] - - ok = self._write_report(bytes([modifiers, 0] + keys)) - - if not ok: + def __send_current_state(self, reopen: bool=False) -> bool: + if not self._ensure_write(self.__make_report(), reopen=reopen): self.__clear_modifiers() self.__clear_keys() - return ok + return False + return True def __clear_modifiers(self) -> None: self.__pressed_modifiers.clear() def __clear_keys(self) -> None: self.__pressed_keys = [None] * 6 + + def __make_report(self) -> bytes: + modifiers = 0 + for modifier in self.__pressed_modifiers: + modifiers |= modifier.code + + assert len(self.__pressed_keys) == 6 + keys = [ + (0 if key is None else key.code) + for key in self.__pressed_keys + ] + + return bytes([modifiers, 0] + keys) diff --git a/kvmd/plugins/hid/otg/mouse.py b/kvmd/plugins/hid/otg/mouse.py index 4c9facaf..172ed666 100644 --- a/kvmd/plugins/hid/otg/mouse.py +++ b/kvmd/plugins/hid/otg/mouse.py @@ -70,11 +70,8 @@ class MouseProcess(BaseDeviceProcess): def cleanup(self) -> None: self._stop() get_logger().info("Clearing HID-mouse events ...") - if self._ensure_device(): - try: - self._write_report(self.__make_report(0, self.__x, self.__y, 0, 0)) # Release all buttons - finally: - self._close_device() + report = self.__make_report(0, self.__x, self.__y, 0, 0) + self._ensure_write(report, close=True) # Release all buttons def send_clear_event(self) -> None: self._queue_event(_ClearEvent()) @@ -118,9 +115,7 @@ class MouseProcess(BaseDeviceProcess): def __process_clear_event(self, reopen: bool=False) -> None: self.__clear_state() - if reopen: - self._close_device() - self.__send_current_state(0, 0) + self.__send_current_state(0, 0, reopen=reopen) def __process_button_event(self, event: _ButtonEvent) -> None: if event.code & self.__pressed_buttons: @@ -143,19 +138,18 @@ class MouseProcess(BaseDeviceProcess): # ===== - def __send_current_state(self, delta_x: int, delta_y: int) -> bool: - ok = False - if self._ensure_device(): - ok = self._write_report(self.__make_report( - buttons=self.__pressed_buttons, - to_x=self.__x, - to_y=self.__y, - delta_x=delta_x, - delta_y=delta_y, - )) - if not ok: + def __send_current_state(self, delta_x: int, delta_y: int, reopen: bool=False) -> bool: + report = self.__make_report( + buttons=self.__pressed_buttons, + to_x=self.__x, + to_y=self.__y, + delta_x=delta_x, + delta_y=delta_y, + ) + if not self._ensure_write(report, reopen=reopen): self.__clear_state() - return ok + return False + return True def __clear_state(self) -> None: self.__pressed_buttons = 0 |