option to disable jiggler

This commit is contained in:
Maxim Devaev
2023-10-26 08:28:18 +03:00
parent aec6727020
commit 7bb5531100
8 changed files with 58 additions and 21 deletions

View File

@@ -25,6 +25,11 @@ import time
from typing import Iterable from typing import Iterable
from typing import AsyncGenerator from typing import AsyncGenerator
from typing import Any
from ...yamlconf import Option
from ...validators.basic import valid_bool
from .. import BasePlugin from .. import BasePlugin
from .. import get_plugin_class from .. import get_plugin_class
@@ -32,11 +37,22 @@ from .. import get_plugin_class
# ===== # =====
class BaseHid(BasePlugin): class BaseHid(BasePlugin):
def __init__(self) -> None: def __init__(self, jiggler_enabled: bool) -> None:
self.__jiggler_enabled = False self.__jiggler_enabled = jiggler_enabled
self.__jiggler_active = False
self.__jiggler_absolute = True self.__jiggler_absolute = True
self.__activity_ts = 0 self.__activity_ts = 0
@classmethod
def _get_jiggler_options(cls) -> dict[str, Any]:
return {
"jiggler": {
"enabled": Option(True, type=valid_bool, unpack_as="jiggler_enabled"),
},
}
# =====
def sysprep(self) -> None: def sysprep(self) -> None:
raise NotImplementedError raise NotImplementedError
@@ -92,7 +108,7 @@ class BaseHid(BasePlugin):
async def systask(self) -> None: async def systask(self) -> None:
factor = 1 factor = 1
while True: while True:
if self.__jiggler_enabled and (self.__activity_ts + 60 < int(time.monotonic())): if self.__jiggler_active and (self.__activity_ts + 60 < int(time.monotonic())):
if self.__jiggler_absolute: if self.__jiggler_absolute:
self.send_mouse_move_event(100 * factor, 100 * factor) self.send_mouse_move_event(100 * factor, 100 * factor)
else: else:
@@ -106,11 +122,17 @@ class BaseHid(BasePlugin):
def _set_jiggler_absolute(self, absolute: bool) -> None: def _set_jiggler_absolute(self, absolute: bool) -> None:
self.__jiggler_absolute = absolute self.__jiggler_absolute = absolute
def _set_jiggler_enabled(self, enabled: bool) -> None: def _set_jiggler_active(self, active: bool) -> None:
self.__jiggler_enabled = enabled if self.__jiggler_enabled:
self.__jiggler_active = active
def _get_jiggler_state(self) -> dict: def _get_jiggler_state(self) -> dict:
return {"enabled": self.__jiggler_enabled} return {
"jiggler": {
"enabled": self.__jiggler_enabled,
"active": self.__jiggler_active,
},
}
# ===== # =====

View File

@@ -114,10 +114,11 @@ class BaseMcuHid(BaseHid, multiprocessing.Process): # pylint: disable=too-many-
retries_delay: float, retries_delay: float,
errors_threshold: int, errors_threshold: int,
noop: bool, noop: bool,
jiggler: dict[str, Any],
**gpio_kwargs: Any, **gpio_kwargs: Any,
) -> None: ) -> None:
BaseHid.__init__(self) BaseHid.__init__(self, **jiggler)
multiprocessing.Process.__init__(self, daemon=True) multiprocessing.Process.__init__(self, daemon=True)
self.__read_retries = read_retries self.__read_retries = read_retries
@@ -161,6 +162,8 @@ class BaseMcuHid(BaseHid, multiprocessing.Process): # pylint: disable=too-many-
"retries_delay": Option(0.5, type=valid_float_f01), "retries_delay": Option(0.5, type=valid_float_f01),
"errors_threshold": Option(5, type=valid_int_f0), "errors_threshold": Option(5, type=valid_int_f0),
"noop": Option(False, type=valid_bool), "noop": Option(False, type=valid_bool),
**cls._get_jiggler_options(),
} }
def sysprep(self) -> None: def sysprep(self) -> None:
@@ -226,7 +229,7 @@ class BaseMcuHid(BaseHid, multiprocessing.Process): # pylint: disable=too-many-
"absolute": absolute, "absolute": absolute,
"outputs": mouse_outputs, "outputs": mouse_outputs,
}, },
"jiggler": self._get_jiggler_state(), **self._get_jiggler_state(),
} }
async def poll_state(self) -> AsyncGenerator[dict, None]: async def poll_state(self) -> AsyncGenerator[dict, None]:
@@ -287,7 +290,7 @@ class BaseMcuHid(BaseHid, multiprocessing.Process): # pylint: disable=too-many-
for (index, event) in enumerate(events, 1): for (index, event) in enumerate(events, 1):
self.__queue_event(event, clear=(index == len(events))) self.__queue_event(event, clear=(index == len(events)))
if jiggler is not None: if jiggler is not None:
self._set_jiggler_enabled(jiggler) self._set_jiggler_active(jiggler)
self.__notifier.notify() self.__notifier.notify()
def set_connected(self, connected: bool) -> None: def set_connected(self, connected: bool) -> None:

View File

@@ -25,6 +25,7 @@ import time
from typing import Iterable from typing import Iterable
from typing import AsyncGenerator from typing import AsyncGenerator
from typing import Any
from ....logging import get_logger from ....logging import get_logger
@@ -61,6 +62,7 @@ class Plugin(BaseHid): # pylint: disable=too-many-instance-attributes
def __init__( # pylint: disable=too-many-arguments,too-many-locals def __init__( # pylint: disable=too-many-arguments,too-many-locals
self, self,
manufacturer: str, manufacturer: str,
product: str, product: str,
description: str, description: str,
@@ -76,9 +78,11 @@ class Plugin(BaseHid): # pylint: disable=too-many-instance-attributes
max_clients: int, max_clients: int,
socket_timeout: float, socket_timeout: float,
select_timeout: float, select_timeout: float,
jiggler: dict[str, Any],
) -> None: ) -> None:
super().__init__() super().__init__(**jiggler)
self._set_jiggler_absolute(False) self._set_jiggler_absolute(False)
self.__proc: (multiprocessing.Process | None) = None self.__proc: (multiprocessing.Process | None) = None
@@ -121,6 +125,8 @@ class Plugin(BaseHid): # pylint: disable=too-many-instance-attributes
"max_clients": Option(1, type=valid_int_f1), "max_clients": Option(1, type=valid_int_f1),
"socket_timeout": Option(5.0, type=valid_float_f01), "socket_timeout": Option(5.0, type=valid_float_f01),
"select_timeout": Option(1.0, type=valid_float_f01), "select_timeout": Option(1.0, type=valid_float_f01),
**cls._get_jiggler_options(),
} }
def sysprep(self) -> None: def sysprep(self) -> None:
@@ -149,7 +155,7 @@ class Plugin(BaseHid): # pylint: disable=too-many-instance-attributes
"absolute": False, "absolute": False,
"outputs": outputs, "outputs": outputs,
}, },
"jiggler": self._get_jiggler_state(), **self._get_jiggler_state(),
} }
async def poll_state(self) -> AsyncGenerator[dict, None]: async def poll_state(self) -> AsyncGenerator[dict, None]:
@@ -207,7 +213,7 @@ class Plugin(BaseHid): # pylint: disable=too-many-instance-attributes
_ = keyboard_output _ = keyboard_output
_ = mouse_output _ = mouse_output
if jiggler is not None: if jiggler is not None:
self._set_jiggler_enabled(jiggler) self._set_jiggler_active(jiggler)
self.__notifier.notify() self.__notifier.notify()
# ===== # =====

View File

@@ -26,6 +26,7 @@ import time
from typing import Iterable from typing import Iterable
from typing import AsyncGenerator from typing import AsyncGenerator
from typing import Any
from ....logging import get_logger from ....logging import get_logger
@@ -56,9 +57,10 @@ class Plugin(BaseHid, multiprocessing.Process): # pylint: disable=too-many-inst
device_path: str, device_path: str,
speed: int, speed: int,
read_timeout: float, read_timeout: float,
jiggler: dict[str, Any],
) -> None: ) -> None:
BaseHid.__init__(self) BaseHid.__init__(self, **jiggler)
multiprocessing.Process.__init__(self, daemon=True) multiprocessing.Process.__init__(self, daemon=True)
self.__device_path = device_path self.__device_path = device_path
@@ -86,6 +88,7 @@ class Plugin(BaseHid, multiprocessing.Process): # pylint: disable=too-many-inst
"device": Option("/dev/kvmd-hid", type=valid_abs_path, unpack_as="device_path"), "device": Option("/dev/kvmd-hid", type=valid_abs_path, unpack_as="device_path"),
"speed": Option(9600, type=valid_tty_speed), "speed": Option(9600, type=valid_tty_speed),
"read_timeout": Option(0.3, type=valid_float_f01), "read_timeout": Option(0.3, type=valid_float_f01),
**cls._get_jiggler_options(),
} }
def sysprep(self) -> None: def sysprep(self) -> None:
@@ -113,7 +116,7 @@ class Plugin(BaseHid, multiprocessing.Process): # pylint: disable=too-many-inst
"active": ("usb" if absolute else "usb_rel"), "active": ("usb" if absolute else "usb_rel"),
}, },
}, },
"jiggler": self._get_jiggler_state(), **self._get_jiggler_state(),
} }
async def poll_state(self) -> AsyncGenerator[dict, None]: async def poll_state(self) -> AsyncGenerator[dict, None]:
@@ -174,7 +177,7 @@ class Plugin(BaseHid, multiprocessing.Process): # pylint: disable=too-many-inst
self._set_jiggler_absolute(absolute) self._set_jiggler_absolute(absolute)
self.__notifier.notify() self.__notifier.notify()
if jiggler is not None: if jiggler is not None:
self._set_jiggler_enabled(jiggler) self._set_jiggler_active(jiggler)
self.__notifier.notify() self.__notifier.notify()
def set_connected(self, connected: bool) -> None: def set_connected(self, connected: bool) -> None:

View File

@@ -49,11 +49,12 @@ class Plugin(BaseHid): # pylint: disable=too-many-instance-attributes
keyboard: dict[str, Any], keyboard: dict[str, Any],
mouse: dict[str, Any], mouse: dict[str, Any],
mouse_alt: dict[str, Any], mouse_alt: dict[str, Any],
jiggler: dict[str, Any],
noop: bool, noop: bool,
udc: str, # XXX: Not from options, see /kvmd/apps/kvmd/__init__.py for details udc: str, # XXX: Not from options, see /kvmd/apps/kvmd/__init__.py for details
) -> None: ) -> None:
super().__init__() super().__init__(**jiggler)
self.__udc = udc self.__udc = udc
@@ -112,6 +113,7 @@ class Plugin(BaseHid): # pylint: disable=too-many-instance-attributes
"horizontal_wheel": Option(True, type=valid_bool), "horizontal_wheel": Option(True, type=valid_bool),
}, },
"noop": Option(False, type=valid_bool), "noop": Option(False, type=valid_bool),
**cls._get_jiggler_options(),
} }
def sysprep(self) -> None: def sysprep(self) -> None:
@@ -145,7 +147,7 @@ class Plugin(BaseHid): # pylint: disable=too-many-instance-attributes
}, },
**mouse_state, **mouse_state,
}, },
"jiggler": self._get_jiggler_state(), **self._get_jiggler_state(),
} }
async def poll_state(self) -> AsyncGenerator[dict, None]: async def poll_state(self) -> AsyncGenerator[dict, None]:
@@ -210,7 +212,7 @@ class Plugin(BaseHid): # pylint: disable=too-many-instance-attributes
self._set_jiggler_absolute(self.__mouse_current.is_absolute()) self._set_jiggler_absolute(self.__mouse_current.is_absolute())
self.__notifier.notify() self.__notifier.notify()
if jiggler is not None: if jiggler is not None:
self._set_jiggler_enabled(jiggler) self._set_jiggler_active(jiggler)
self.__notifier.notify() self.__notifier.notify()
def clear_events(self) -> None: def clear_events(self) -> None:

View File

@@ -350,7 +350,7 @@
</div> </div>
</td> </td>
</tr> </tr>
<tr> <tr class="feature-disabled" id="hid-jiggler">
<td>Mouse jiggler:</td> <td>Mouse jiggler:</td>
<td align="right"> <td align="right">
<div class="switch-box"> <div class="switch-box">

View File

@@ -103,7 +103,7 @@ li(id="system-dropdown" class="right")
table(class="kv") table(class="kv")
tr(id="hid-connect" class="feature-disabled") tr(id="hid-connect" class="feature-disabled")
+menu_switch_notable("hid-connect-switch", "Connect HID to Server", true, true) +menu_switch_notable("hid-connect-switch", "Connect HID to Server", true, true)
tr tr(id="hid-jiggler" class="feature-disabled")
+menu_switch_notable("hid-jiggler-switch", "Mouse jiggler", false, false) +menu_switch_notable("hid-jiggler-switch", "Mouse jiggler", false, false)
tr tr
+menu_switch_notable("hid-mute-switch", "Mute HID input events", true, false) +menu_switch_notable("hid-mute-switch", "Mute HID input events", true, false)

View File

@@ -133,7 +133,8 @@ export function Hid(__getGeometry, __recorder) {
let has_relative_squash = false; let has_relative_squash = false;
if (state) { if (state) {
$("hid-jiggler-switch").checked = !!state.jiggler.enabled; tools.feature.setEnabled($("hid-jiggler"), state.jiggler.enabled);
$("hid-jiggler-switch").checked = state.jiggler.active;
} }
if (state && state.online) { if (state && state.online) {
let keyboard_outputs = state.keyboard.outputs.available; let keyboard_outputs = state.keyboard.outputs.available;