summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMaxim Devaev <[email protected]>2023-06-07 05:12:22 +0300
committerMaxim Devaev <[email protected]>2023-06-07 05:12:22 +0300
commit9f98a2f701703a3f9f591868547c834463d3d6b1 (patch)
treef2fe4f4541c78edaa6ec791a304d73f0595d4751
parent9c694da00c7d93cbff3aa910ffdeeb8dded691c0 (diff)
binary keyboard protocol
-rw-r--r--kvmd/apps/kvmd/api/hid.py19
-rw-r--r--kvmd/clients/kvmd.py4
-rw-r--r--web/share/js/kvm/keyboard.js2
-rw-r--r--web/share/js/kvm/mouse.js33
-rw-r--r--web/share/js/kvm/recorder.js2
-rw-r--r--web/share/js/kvm/session.js43
6 files changed, 68 insertions, 35 deletions
diff --git a/kvmd/apps/kvmd/api/hid.py b/kvmd/apps/kvmd/api/hid.py
index 8143deff..a2ea5d4c 100644
--- a/kvmd/apps/kvmd/api/hid.py
+++ b/kvmd/apps/kvmd/api/hid.py
@@ -154,6 +154,25 @@ class HidApi:
# =====
+ @exposed_ws(1)
+ async def __ws_bin_key_handler(self, _: WsSession, data: bytes) -> None:
+ try:
+ key = valid_hid_key(data[1:].decode("ascii"))
+ state = valid_bool(data[0])
+ except Exception:
+ return
+ if key not in self.__ignore_keys:
+ self.__hid.send_key_events([(key, state)])
+
+ @exposed_ws(2)
+ async def __ws_bin_mouse_button_handler(self, _: WsSession, data: bytes) -> None:
+ try:
+ button = valid_hid_mouse_button(data[1:].decode("ascii"))
+ state = valid_bool(data[0])
+ except Exception:
+ return
+ self.__hid.send_mouse_button_event(button, state)
+
@exposed_ws(3)
async def __ws_bin_mouse_move_handler(self, _: WsSession, data: bytes) -> None:
try:
diff --git a/kvmd/clients/kvmd.py b/kvmd/clients/kvmd.py
index ec86378d..569fb9d0 100644
--- a/kvmd/clients/kvmd.py
+++ b/kvmd/clients/kvmd.py
@@ -188,10 +188,10 @@ class KvmdClientWs:
self.__communicated = False
async def send_key_event(self, key: str, state: bool) -> None:
- await self.__writer_queue.put(("key", {"key": key, "state": state}))
+ await self.__writer_queue.put(bytes([1, state]) + key.encode("ascii"))
async def send_mouse_button_event(self, button: str, state: bool) -> None:
- await self.__writer_queue.put(("mouse_button", {"button": button, "state": state}))
+ await self.__writer_queue.put(bytes([2, state]) + button.encode("ascii"))
async def send_mouse_move_event(self, to_x: int, to_y: int) -> None:
await self.__writer_queue.put(struct.pack(">bhh", 3, to_x, to_y))
diff --git a/web/share/js/kvm/keyboard.js b/web/share/js/kvm/keyboard.js
index 7cbdc1e8..b398e769 100644
--- a/web/share/js/kvm/keyboard.js
+++ b/web/share/js/kvm/keyboard.js
@@ -134,7 +134,7 @@ export function Keyboard(__recordWsEvent) {
"event": {"key": code, "state": state},
};
if (__ws && !$("hid-mute-switch").checked) {
- __ws.send(JSON.stringify(event));
+ __ws.sendHidEvent(event);
}
__recordWsEvent(event);
};
diff --git a/web/share/js/kvm/mouse.js b/web/share/js/kvm/mouse.js
index fb510d27..f2d6afde 100644
--- a/web/share/js/kvm/mouse.js
+++ b/web/share/js/kvm/mouse.js
@@ -345,38 +345,11 @@ export function Mouse(__getGeometry, __recordWsEvent) {
};
var __sendEvent = function(event_type, event) {
- let wrapped_event = {"event_type": event_type, "event": event};
+ event = {"event_type": event_type, "event": event};
if (__ws && !$("hid-mute-switch").checked) {
- if (event_type == "mouse_move") {
- let data = new Uint8Array([
- 3,
- (event.to.x >> 8) & 0xFF, event.to.x & 0xFF,
- (event.to.y >> 8) & 0xFF, event.to.y & 0xFF,
- ]);
- __ws.send(data);
-
- } else if (event_type == "mouse_relative" || event_type == "mouse_wheel") {
- let data;
- if (Array.isArray(event.delta)) {
- data = new Int8Array(2 + event.delta.length * 2);
- let index = 0;
- for (let delta of event.delta) {
- data[index + 2] = delta["x"];
- data[index + 3] = delta["y"];
- index += 2;
- }
- } else {
- data = new Int8Array([0, 0, event.delta.x, event.delta.y]);
- }
- data[0] = (event_type == "mouse_relative" ? 4 : 5);
- data[1] = (event.squash ? 1 : 0);
- __ws.send(data);
-
- } else {
- __ws.send(JSON.stringify(wrapped_event));
- }
+ __ws.sendHidEvent(event);
}
- __recordWsEvent(wrapped_event);
+ __recordWsEvent(event);
};
__init__();
diff --git a/web/share/js/kvm/recorder.js b/web/share/js/kvm/recorder.js
index 8f6683f7..88e5bbe5 100644
--- a/web/share/js/kvm/recorder.js
+++ b/web/share/js/kvm/recorder.js
@@ -290,7 +290,7 @@ export function Recorder() {
return;
} else if (["key", "mouse_button", "mouse_move", "mouse_wheel", "mouse_relative"].includes(event.event_type)) {
- __ws.send(JSON.stringify(event));
+ __ws.sendHidEvent(event);
}
index += 1;
diff --git a/web/share/js/kvm/session.js b/web/share/js/kvm/session.js
index 25159a4f..277e786d 100644
--- a/web/share/js/kvm/session.js
+++ b/web/share/js/kvm/session.js
@@ -278,6 +278,7 @@ export function Session() {
if (http.readyState === 4) {
if (http.status === 200) {
__ws = new WebSocket(`${tools.is_https ? "wss" : "ws"}://${location.host}/api/ws`);
+ __ws.sendHidEvent = (event) => __sendHidEvent(__ws, event.event_type, event.event);
__ws.onopen = __wsOpenHandler;
__ws.onmessage = __wsMessageHandler;
__ws.onerror = __wsErrorHandler;
@@ -294,6 +295,46 @@ export function Session() {
});
};
+ var __ascii_encoder = new TextEncoder("ascii");
+
+ var __sendHidEvent = function(ws, event_type, event) {
+ if (event_type == "key") {
+ let data = __ascii_encoder.encode("\x01\x00" + event.key);
+ data[1] = (event.state ? 1 : 0);
+ ws.send(data);
+
+ } else if (event_type == "mouse_button") {
+ let data = __ascii_encoder.encode("\x02\x00" + event.button);
+ data[1] = (event.state ? 1 : 0);
+ ws.send(data);
+
+ } else if (event_type == "mouse_move") {
+ let data = new Uint8Array([
+ 3,
+ (event.to.x >> 8) & 0xFF, event.to.x & 0xFF,
+ (event.to.y >> 8) & 0xFF, event.to.y & 0xFF,
+ ]);
+ ws.send(data);
+
+ } else if (event_type == "mouse_relative" || event_type == "mouse_wheel") {
+ let data;
+ if (Array.isArray(event.delta)) {
+ data = new Int8Array(2 + event.delta.length * 2);
+ let index = 0;
+ for (let delta of event.delta) {
+ data[index + 2] = delta["x"];
+ data[index + 3] = delta["y"];
+ index += 2;
+ }
+ } else {
+ data = new Int8Array([0, 0, event.delta.x, event.delta.y]);
+ }
+ data[0] = (event_type == "mouse_relative" ? 4 : 5);
+ data[1] = (event.squash ? 1 : 0);
+ ws.send(data);
+ }
+ };
+
var __wsOpenHandler = function(event) {
tools.debug("Session: socket opened:", event);
$("link-led").className = "led-green";
@@ -365,7 +406,7 @@ export function Session() {
if (__missed_heartbeats >= 15) {
throw new Error("Too many missed heartbeats");
}
- __ws.send(JSON.stringify({"event_type": "ping", "event": {}}));
+ __ws.send("{\"event_type\": \"ping\", \"event\": {}}");
} catch (err) {
tools.error("Session: ping error:", err.message);
if (__ws) {