Skip to content

Commit

Permalink
pikvm/pikvm#858, pikvm/pikvm#1249: Added slow typing mode for /api/hi…
Browse files Browse the repository at this point in the history
…d/print
  • Loading branch information
mdevaev committed Dec 11, 2024
1 parent adbd4f2 commit e014cbc
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 14 deletions.
5 changes: 3 additions & 2 deletions kvmd/apps/kvmd/api/hid.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ async def __print_handler(self, req: Request) -> Response:
if limit > 0:
text = text[:limit]
symmap = self.__ensure_symmap(req.query.get("keymap", self.__default_keymap_name))
self.__hid.send_key_events(text_to_web_keys(text, symmap), no_ignore_keys=True)
slow = valid_bool(req.query.get("slow", False))
await self.__hid.send_key_events(text_to_web_keys(text, symmap), no_ignore_keys=True, slow=slow)
return make_json_response()

def __ensure_symmap(self, keymap_name: str) -> dict[int, dict[int, str]]:
Expand Down Expand Up @@ -250,7 +251,7 @@ async def __events_send_key_handler(self, req: Request) -> Response:
state = valid_bool(req.query["state"])
self.__hid.send_key_event(key, state)
else:
self.__hid.send_key_events([(key, True), (key, False)])
await self.__hid.send_key_events([(key, True), (key, False)], slow=True)
return make_json_response()

@exposed_http("POST", "/hid/events/send_mouse_button")
Expand Down
2 changes: 1 addition & 1 deletion kvmd/apps/kvmd/snapshoter.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ async def __wakeup(self) -> None:

if self.__wakeup_key:
logger.info("Waking up using key %r ...", self.__wakeup_key)
self.__hid.send_key_events(
await self.__hid.send_key_events(
keys=[(self.__wakeup_key, True), (self.__wakeup_key, False)],
no_ignore_keys=True,
)
Expand Down
10 changes: 9 additions & 1 deletion kvmd/plugins/hid/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,17 @@ def set_connected(self, connected: bool) -> None:

# =====

def send_key_events(self, keys: Iterable[tuple[str, bool]], no_ignore_keys: bool=False) -> None:
async def send_key_events(
self,
keys: Iterable[tuple[str, bool]],
no_ignore_keys: bool=False,
slow: bool=False,
) -> None:

for (key, state) in keys:
if no_ignore_keys or key not in self.__ignore_keys:
if slow:
await asyncio.sleep(0.02)
self.send_key_event(key, state)

def send_key_event(self, key: str, state: bool) -> None:
Expand Down
15 changes: 12 additions & 3 deletions web/kvm/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -728,11 +728,11 @@
</table>
<table class="kv">
<tr>
<td>Ask paste confirmation:</td>
<td>Slow typing:</td>
<td align="right">
<div class="switch-box">
<input checked type="checkbox" id="hid-pak-ask-switch">
<label for="hid-pak-ask-switch"><span class="switch-inner"></span><span class="switch"></span></label>
<input type="checkbox" id="hid-pak-slow-switch">
<label for="hid-pak-slow-switch"><span class="switch-inner"></span><span class="switch"></span></label>
</div>
</td>
</tr>
Expand All @@ -745,6 +745,15 @@
</div>
</td>
</tr>
<tr>
<td>Ask paste confirmation:</td>
<td align="right">
<div class="switch-box">
<input checked type="checkbox" id="hid-pak-ask-switch">
<label for="hid-pak-ask-switch"><span class="switch-inner"></span><span class="switch"></span></label>
</div>
</td>
</tr>
</table>
<div class="feature-disabled" id="stream-ocr">
<hr><br>
Expand Down
4 changes: 3 additions & 1 deletion web/kvm/navbar-text.pug
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ li(id="text-dropdown" class="right")
select(id="hid-pak-keymap-selector")
table(class="kv")
tr
+menu_switch_notable("hid-pak-ask-switch", "Ask paste confirmation", true, true)
+menu_switch_notable("hid-pak-slow-switch", "Slow typing", true, false)
tr
+menu_switch_notable("hid-pak-secure-switch", "Hide input text", true, false)
tr
+menu_switch_notable("hid-pak-ask-switch", "Ask paste confirmation", true, true)
div(id="stream-ocr" class="feature-disabled")
hr
br
Expand Down
6 changes: 4 additions & 2 deletions web/share/js/kvm/paste.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export function Paste(__recorder) {

var __init__ = function() {
tools.storage.bindSimpleSwitch($("hid-pak-ask-switch"), "hid.pak.ask", true);
tools.storage.bindSimpleSwitch($("hid-pak-slow-switch"), "hid.pak.slow", false);
tools.storage.bindSimpleSwitch($("hid-pak-secure-switch"), "hid.pak.secure", false, function(value) {
$("hid-pak-text").style.setProperty("-webkit-text-security", (value ? "disc" : "none"));
});
Expand Down Expand Up @@ -67,10 +68,11 @@ export function Paste(__recorder) {
tools.el.setEnabled($("hid-pak-keymap-selector"), false);

let keymap = $("hid-pak-keymap-selector").value;
let slow = $("hid-pak-slow-switch").checked;

tools.debug(`HID: paste-as-keys ${keymap}: ${text}`);

tools.httpPost("/api/hid/print", {"limit": 0, "keymap": keymap}, function(http) {
tools.httpPost("/api/hid/print", {"limit": 0, "keymap": keymap, "slow": slow}, function(http) {
tools.el.setEnabled($("hid-pak-text"), true);
tools.el.setEnabled($("hid-pak-button"), true);
tools.el.setEnabled($("hid-pak-keymap-selector"), true);
Expand All @@ -80,7 +82,7 @@ export function Paste(__recorder) {
} else if (http.status !== 200) {
wm.error("HID paste error", http.responseText);
} else if (http.status === 200) {
__recorder.recordPrintEvent(text, keymap);
__recorder.recordPrintEvent(text, keymap, slow);
}
}, text, "text/plain");
};
Expand Down
14 changes: 10 additions & 4 deletions web/share/js/kvm/recorder.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ export function Recorder() {
__recordEvent(event);
};

self.recordPrintEvent = function(text, keymap) {
__recordEvent({"event_type": "print", "event": {"text": text, "keymap": keymap}});
self.recordPrintEvent = function(text, keymap, slow) {
__recordEvent({"event_type": "print", "event": {"text": text, "keymap": keymap, "slow": slow}});
};

self.recordAtxButtonEvent = function(button) {
Expand Down Expand Up @@ -159,9 +159,12 @@ export function Recorder() {

} else if (event.event_type === "print") {
__checkType(event.event.text, "string", "Non-string print text");
if (event.event.keymap) {
if (event.event.keymap !== undefined) {
__checkType(event.event.keymap, "string", "Non-string keymap");
}
if (event.event.slow !== undefined) {
__checkType(event.event.slow, "boolean", "Non-bool slow");
}

} else if (event.event_type === "key") {
__checkType(event.event.key, "string", "Non-string key code");
Expand Down Expand Up @@ -284,9 +287,12 @@ export function Recorder() {

} else if (event.event_type === "print") {
let params = {"limit": 0};
if (event.event.keymap) {
if (event.event.keymap !== undefined) {
params["keymap"] = event.event.keymap;
}
if (event.event.slow !== undefined) {
params["slow"] = event.event.slow;
}
tools.httpPost("/api/hid/print", params, function(http) {
if (http.status === 413) {
wm.error("Too many text for paste!");
Expand Down

0 comments on commit e014cbc

Please sign in to comment.