From 6b148b377c01ec6a6d67fb6c8bc9476ae22121ee Mon Sep 17 00:00:00 2001 From: Devaev Maxim Date: Mon, 9 Jul 2018 05:32:16 +0000 Subject: some keyboard code --- kvmd/kvmd/ps2.py | 8 ++++++-- kvmd/kvmd/server.py | 14 ++++++++++++++ kvmd/web/index.html | 31 +++++++++++++++---------------- 3 files changed, 35 insertions(+), 18 deletions(-) diff --git a/kvmd/kvmd/ps2.py b/kvmd/kvmd/ps2.py index 7f6bc85d..fe20e2c6 100644 --- a/kvmd/kvmd/ps2.py +++ b/kvmd/kvmd/ps2.py @@ -29,8 +29,12 @@ class Ps2Keyboard(multiprocessing.Process): self.__event.set() self.join() - def send_byte(self, code: int) -> None: - self.__queue.put(code) + def send_event(self, code: str, state: bool) -> None: + if state: + get_logger().info("Key pressed: %s", code) + else: + get_logger().info("Key released: %s", code) + # TODO: self.__queue.put(code) def cleanup(self) -> None: if self.is_alive(): diff --git a/kvmd/kvmd/server.py b/kvmd/kvmd/server.py index a49762d1..71a3a02c 100644 --- a/kvmd/kvmd/server.py +++ b/kvmd/kvmd/server.py @@ -124,11 +124,25 @@ class Server: # pylint: disable=too-many-instance-attributes aiohttp.web.run_app(app, host=host, port=port, print=self.__run_app_print) async def __ws_handler(self, request: aiohttp.web.Request) -> aiohttp.web.WebSocketResponse: + logger = get_logger(0) ws = aiohttp.web.WebSocketResponse(heartbeat=self.__heartbeat) await ws.prepare(request) await self.__register_socket(ws) async for msg in ws: if msg.type == aiohttp.web.WSMsgType.TEXT: + try: + event = json.loads(msg.data) + except Exception: + logger.exception("Can't parse JSON event from websocket") + else: + if event.get("event_type") == "key_event": + key_code = str(event.get("key_code", ""))[:64].strip() + key_state = event.get("key_state") + if key_code and key_state in [True, False]: + self.__keyboard.send_event(key_code, key_state) + continue + else: + logger.error("Invalid websocket event: %r", event) await ws.send_str(json.dumps({"msg_type": "echo", "msg": msg.data})) else: break diff --git a/kvmd/web/index.html b/kvmd/web/index.html index 87281e88..cb61185a 100644 --- a/kvmd/web/index.html +++ b/kvmd/web/index.html @@ -29,33 +29,32 @@ body { -- cgit v1.2.3