From edb967c633487c90846a6df69adb3842a91fd95c Mon Sep 17 00:00:00 2001 From: Devaev Maxim Date: Thu, 24 Dec 2020 15:49:23 +0300 Subject: hid set_connected api --- kvmd/apps/kvmd/api/hid.py | 5 +++++ kvmd/plugins/hid/__init__.py | 3 +++ kvmd/plugins/hid/_mcu/__init__.py | 19 ++++++++++++------- kvmd/plugins/hid/_mcu/proto.py | 9 +++++++++ kvmd/plugins/hid/bt/__init__.py | 1 + kvmd/plugins/hid/otg/__init__.py | 1 + 6 files changed, 31 insertions(+), 7 deletions(-) diff --git a/kvmd/apps/kvmd/api/hid.py b/kvmd/apps/kvmd/api/hid.py index a988ab34..9c39ab20 100644 --- a/kvmd/apps/kvmd/api/hid.py +++ b/kvmd/apps/kvmd/api/hid.py @@ -79,6 +79,11 @@ class HidApi: self.__hid.set_mouse_output(valid_hid_mouse_output(request.query.get("output"))) return make_json_response() + @exposed_http("POST", "/hid/set_connected") + async def __set_connected_handler(self, request: Request) -> Response: + self.__hid.set_connected(valid_bool(request.query.get("connected"))) + return make_json_response() + @exposed_http("POST", "/hid/reset") async def __reset_handler(self, _: Request) -> Response: await self.__hid.reset() diff --git a/kvmd/plugins/hid/__init__.py b/kvmd/plugins/hid/__init__.py index 8baceb28..0ba82e1b 100644 --- a/kvmd/plugins/hid/__init__.py +++ b/kvmd/plugins/hid/__init__.py @@ -73,6 +73,9 @@ class BaseHid(BasePlugin): def set_mouse_output(self, output: str) -> None: _ = output + def set_connected(self, connected: bool) -> None: + _ = connected + def clear_events(self) -> None: raise NotImplementedError diff --git a/kvmd/plugins/hid/_mcu/__init__.py b/kvmd/plugins/hid/_mcu/__init__.py index 424f0913..242de465 100644 --- a/kvmd/plugins/hid/_mcu/__init__.py +++ b/kvmd/plugins/hid/_mcu/__init__.py @@ -58,6 +58,7 @@ from .proto import RESPONSE_LEGACY_OK from .proto import BaseEvent from .proto import SetKeyboardOutputEvent from .proto import SetMouseOutputEvent +from .proto import SetConnectedEvent from .proto import ClearEvent from .proto import KeyEvent from .proto import MouseButtonEvent @@ -166,27 +167,27 @@ class BaseMcuHid(BaseHid, multiprocessing.Process): # pylint: disable=too-many- state = await self.__state_flags.get() online = bool(state["online"]) pong = (state["status"] >> 16) & 0xFF - outputs = (state["status"] >> 8) & 0xFF - features = state["status"] & 0xFF + outputs1 = (state["status"] >> 8) & 0xFF + outputs2 = state["status"] & 0xFF absolute = True - active_mouse = get_active_mouse(outputs) + active_mouse = get_active_mouse(outputs1) if online and active_mouse in ["usb_rel", "ps2"]: absolute = False keyboard_outputs: Dict = {"available": [], "active": ""} mouse_outputs: Dict = {"available": [], "active": ""} - if outputs & 0b10000000: # Dynamic - if features & 0b00000001: # USB + if outputs1 & 0b10000000: # Dynamic + if outputs2 & 0b00000001: # USB keyboard_outputs["available"].extend(["usb"]) mouse_outputs["available"].extend(["usb", "usb_rel"]) - if features & 0b00000010: # PS/2 + if outputs2 & 0b00000010: # PS/2 keyboard_outputs["available"].extend(["ps2"]) mouse_outputs["available"].extend(["ps2"]) - active_keyboard = get_active_keyboard(outputs) + active_keyboard = get_active_keyboard(outputs1) if active_keyboard in keyboard_outputs["available"]: keyboard_outputs["active"] = active_keyboard @@ -196,6 +197,7 @@ class BaseMcuHid(BaseHid, multiprocessing.Process): # pylint: disable=too-many- return { "online": online, "busy": bool(state["busy"]), + "connected": (bool(outputs2 & 0b01000000) if outputs2 & 0b10000000 else None), "keyboard": { "online": (online and not (pong & 0b00001000)), "leds": { @@ -256,6 +258,9 @@ class BaseMcuHid(BaseHid, multiprocessing.Process): # pylint: disable=too-many- def set_mouse_output(self, output: str) -> None: self.__queue_event(SetMouseOutputEvent(output), clear=True) + def set_connected(self, connected: bool) -> None: + self.__queue_event(SetConnectedEvent(connected), clear=True) + def clear_events(self) -> None: self.__queue_event(ClearEvent(), clear=True) diff --git a/kvmd/plugins/hid/_mcu/proto.py b/kvmd/plugins/hid/_mcu/proto.py index 7cfc3fd7..a0f83528 100644 --- a/kvmd/plugins/hid/_mcu/proto.py +++ b/kvmd/plugins/hid/_mcu/proto.py @@ -82,6 +82,15 @@ class SetMouseOutputEvent(BaseEvent): return _make_request(struct.pack(">BBxxx", 0x04, _MOUSE_NAMES_TO_CODES.get(self.mouse, 0))) +# ===== +@dataclasses.dataclass(frozen=True) +class SetConnectedEvent(BaseEvent): + connected: bool + + def make_request(self) -> bytes: + return _make_request(struct.pack(">BBxxx", 0x05, int(self.connected))) + + # ===== class ClearEvent(BaseEvent): def make_request(self) -> bytes: diff --git a/kvmd/plugins/hid/bt/__init__.py b/kvmd/plugins/hid/bt/__init__.py index 72830d4f..83e7bf80 100644 --- a/kvmd/plugins/hid/bt/__init__.py +++ b/kvmd/plugins/hid/bt/__init__.py @@ -134,6 +134,7 @@ class Plugin(BaseHid): # pylint: disable=too-many-instance-attributes return { "online": True, "busy": False, + "connected": None, "keyboard": { "online": state["online"], "leds": { diff --git a/kvmd/plugins/hid/otg/__init__.py b/kvmd/plugins/hid/otg/__init__.py index 3a3fc791..22a6b223 100644 --- a/kvmd/plugins/hid/otg/__init__.py +++ b/kvmd/plugins/hid/otg/__init__.py @@ -93,6 +93,7 @@ class Plugin(BaseHid): return { "online": True, "busy": False, + "connected": None, "keyboard": { "online": keyboard_state["online"], "leds": { -- cgit v1.2.3