diff options
author | Maxim Devaev <[email protected]> | 2025-01-10 22:56:28 +0200 |
---|---|---|
committer | Maxim Devaev <[email protected]> | 2025-01-10 22:56:28 +0200 |
commit | 72c9ae3aa00b8817fab2b8243203ca6e1c25e146 (patch) | |
tree | aac3f3233212bb029a1e8f117501dc72dc26bce5 | |
parent | 05bced1461d6044e95f8bffd93f574bdd7c52735 (diff) |
improved jiggler logic
-rw-r--r-- | kvmd/mouse.py | 4 | ||||
-rw-r--r-- | kvmd/plugins/hid/__init__.py | 47 |
2 files changed, 30 insertions, 21 deletions
diff --git a/kvmd/mouse.py b/kvmd/mouse.py index 31894297..3fb76800 100644 --- a/kvmd/mouse.py +++ b/kvmd/mouse.py @@ -32,3 +32,7 @@ class MouseRange: @classmethod def remap(cls, value: int, out_min: int, out_max: int) -> int: return tools.remap(value, cls.MIN, cls.MAX, out_min, out_max) + + @classmethod + def normalize(cls, value: int) -> int: + return min(max(cls.MIN, value), cls.MAX) diff --git a/kvmd/plugins/hid/__init__.py b/kvmd/plugins/hid/__init__.py index a0de32c7..a385023a 100644 --- a/kvmd/plugins/hid/__init__.py +++ b/kvmd/plugins/hid/__init__.py @@ -65,11 +65,13 @@ class BaseHid(BasePlugin): # pylint: disable=too-many-instance-attributes self.__mouse_x_range = (mouse_x_min, mouse_x_max) self.__mouse_y_range = (mouse_y_min, mouse_y_max) - self.__jiggler_enabled = jiggler_enabled - self.__jiggler_active = jiggler_active - self.__jiggler_interval = jiggler_interval - self.__jiggler_absolute = True - self.__activity_ts = 0 + self.__j_enabled = jiggler_enabled + self.__j_active = jiggler_active + self.__j_interval = jiggler_interval + self.__j_absolute = True + self.__j_activity_ts = 0 + self.__j_last_x = 0 + self.__j_last_y = 0 @classmethod def _get_base_options(cls) -> dict[str, Any]: @@ -174,6 +176,8 @@ class BaseHid(BasePlugin): # pylint: disable=too-many-instance-attributes # ===== def send_mouse_move_event(self, to_x: int, to_y: int) -> None: + self.__j_last_x = to_x + self.__j_last_y = to_y if self.__mouse_x_range != MouseRange.RANGE: to_x = MouseRange.remap(to_x, *self.__mouse_x_range) if self.__mouse_y_range != MouseRange.RANGE: @@ -242,37 +246,38 @@ class BaseHid(BasePlugin): # pylint: disable=too-many-instance-attributes handler(*xy) def __bump_activity(self) -> None: - self.__activity_ts = int(time.monotonic()) + self.__j_activity_ts = int(time.monotonic()) def _set_jiggler_absolute(self, absolute: bool) -> None: - self.__jiggler_absolute = absolute + self.__j_absolute = absolute def _set_jiggler_active(self, active: bool) -> None: - if self.__jiggler_enabled: - self.__jiggler_active = active + if self.__j_enabled: + self.__j_active = active def _get_jiggler_state(self) -> dict: return { "jiggler": { - "enabled": self.__jiggler_enabled, - "active": self.__jiggler_active, - "interval": self.__jiggler_interval, + "enabled": self.__j_enabled, + "active": self.__j_active, + "interval": self.__j_interval, }, } # ===== async def systask(self) -> None: - factor = 1 while True: - if self.__jiggler_active and (self.__activity_ts + self.__jiggler_interval < int(time.monotonic())): - for _ in range(5): - if self.__jiggler_absolute: - self.send_mouse_move_event(100 * factor, 100 * factor) - else: - self.send_mouse_relative_event(10 * factor, 10 * factor) - factor *= -1 - await asyncio.sleep(0.1) + if self.__j_active and (self.__j_activity_ts + self.__j_interval < int(time.monotonic())): + if self.__j_absolute: + (x, y) = (self.__j_last_x, self.__j_last_y) + for move in [100, -100, 100, -100, 0]: + self.send_mouse_move_event(MouseRange.normalize(x + move), MouseRange.normalize(y + move)) + await asyncio.sleep(0.1) + else: + for move in [10, -10, 10, -10]: + self.send_mouse_relative_event(move, move) + await asyncio.sleep(0.1) await asyncio.sleep(1) |