pikvm/pikvm#1537: /hid/inactivity api

This commit is contained in:
Maxim Devaev 2025-06-10 02:49:50 +03:00
parent f25e5ef2b4
commit 735c2e6395
2 changed files with 16 additions and 3 deletions

View File

@ -102,6 +102,11 @@ class HidApi:
await self.__hid.reset() await self.__hid.reset()
return make_json_response() return make_json_response()
@exposed_http("GET", "/hid/inactivity")
async def __inactivity_handler(self, _: Request) -> Response:
secs = self.__hid.get_inactivity_seconds()
return make_json_response({"inactivity": secs})
# ===== # =====
async def get_keymaps(self) -> dict: # Ugly hack to generate hid_keymaps_state (see server.py) async def get_keymaps(self) -> dict: # Ugly hack to generate hid_keymaps_state (see server.py)

View File

@ -72,7 +72,7 @@ class BaseHid(BasePlugin): # pylint: disable=too-many-instance-attributes
self.__j_active = jiggler_active self.__j_active = jiggler_active
self.__j_interval = jiggler_interval self.__j_interval = jiggler_interval
self.__j_absolute = True self.__j_absolute = True
self.__j_activity_ts = 0 self.__j_activity_ts = self.__get_monotonic_seconds()
self.__j_last_x = 0 self.__j_last_x = 0
self.__j_last_y = 0 self.__j_last_y = 0
@ -143,6 +143,11 @@ class BaseHid(BasePlugin): # pylint: disable=too-many-instance-attributes
# ===== # =====
def get_inactivity_seconds(self) -> int:
return (self.__get_monotonic_seconds() - self.__j_activity_ts)
# =====
async def send_key_events( async def send_key_events(
self, self,
keys: Iterable[tuple[int, bool]], keys: Iterable[tuple[int, bool]],
@ -249,7 +254,10 @@ class BaseHid(BasePlugin): # pylint: disable=too-many-instance-attributes
handler(*xy) handler(*xy)
def __bump_activity(self) -> None: def __bump_activity(self) -> None:
self.__j_activity_ts = int(time.monotonic()) self.__j_activity_ts = self.__get_monotonic_seconds()
def __get_monotonic_seconds(self) -> int:
return int(time.monotonic())
def _set_jiggler_absolute(self, absolute: bool) -> None: def _set_jiggler_absolute(self, absolute: bool) -> None:
self.__j_absolute = absolute self.__j_absolute = absolute
@ -271,7 +279,7 @@ class BaseHid(BasePlugin): # pylint: disable=too-many-instance-attributes
async def systask(self) -> None: async def systask(self) -> None:
while True: while True:
if self.__j_active and (self.__j_activity_ts + self.__j_interval < int(time.monotonic())): if self.__j_active and (self.__j_activity_ts + self.__j_interval < self.__get_monotonic_seconds()):
if self.__j_absolute: if self.__j_absolute:
(x, y) = (self.__j_last_x, self.__j_last_y) (x, y) = (self.__j_last_x, self.__j_last_y)
for move in [100, -100, 100, -100, 0]: for move in [100, -100, 100, -100, 0]: