mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-19 04:46:08 +00:00
fix: the on-screen pad steps aside while Steam's or DroidDeck's keyboard is open (#1217)
This commit is contained in:
1 parent
b8b3ecf915
commit
862afdf0a9
6 files changed
+465
-9
No files matched your search
@@ -23,6 +23,7 @@ import json
|
||||
import math
|
||||
import re
|
||||
import os
|
||||
import select
|
||||
import selectors
|
||||
import signal
|
||||
import socket
|
||||
@@ -532,6 +533,111 @@ def cdp_eval(agent_dir, expression, target=None, await_promise=True, timeout=15)
|
||||
ws.close()
|
||||
|
||||
|
||||
KEYBOARD_BINDING = "droiddeckSteamKeyboard"
|
||||
KEYBOARD_CHECK_SECONDS = 30
|
||||
KEYBOARD_JS = r"""
|
||||
(() => {
|
||||
const self = window.__droiddeckKeyboard || (window.__droiddeckKeyboard = { watched: new WeakSet(), last: null });
|
||||
self.check = () => {
|
||||
let showing = false;
|
||||
for (const win of Array.from(globalThis.SteamUIStore?.WindowStore?.SteamUIWindows || [])) {
|
||||
const value = win?.VirtualKeyboardManager?.IsShowingVirtualKeyboard;
|
||||
if (!value) continue;
|
||||
if (!self.watched.has(value) && typeof value.Subscribe === "function") {
|
||||
self.watched.add(value);
|
||||
value.Subscribe(() => setTimeout(() => self.check()));
|
||||
}
|
||||
if (value.m_currentValue === true) showing = true;
|
||||
}
|
||||
if (showing !== self.last) {
|
||||
self.last = showing;
|
||||
if (typeof window.__BINDING__ === "function") window.__BINDING__(String(showing));
|
||||
}
|
||||
return showing;
|
||||
};
|
||||
self.timer = self.timer || setInterval(() => self.check(), 500);
|
||||
return self.check();
|
||||
})()
|
||||
""".replace("__BINDING__", KEYBOARD_BINDING)
|
||||
|
||||
|
||||
class KeyboardWatcher(threading.Thread):
|
||||
def __init__(self, agent_dir):
|
||||
super().__init__(daemon=True, name="keyboard")
|
||||
self.agent_dir = agent_dir
|
||||
self.path = agent_dir / "keyboard.json"
|
||||
self.showing = None
|
||||
self.seq = 0
|
||||
self.next_id = 0
|
||||
self.attached = False
|
||||
|
||||
def report(self, showing):
|
||||
if showing == self.showing:
|
||||
return
|
||||
self.showing = showing
|
||||
self.seq += 1
|
||||
write_json(self.path, {"steamKeyboard": showing, "seq": self.seq})
|
||||
|
||||
def run(self):
|
||||
failures, last = 0, None
|
||||
while True:
|
||||
try:
|
||||
self.watch()
|
||||
except Exception as e:
|
||||
if self.attached:
|
||||
log("Steam keyboard: lost the client (%s); reattaching" % e)
|
||||
failures = 0
|
||||
elif str(e) != last:
|
||||
log("Steam keyboard: waiting for the client (%s)" % e)
|
||||
self.attached = False
|
||||
last = str(e)
|
||||
self.report(False)
|
||||
failures += 1
|
||||
time.sleep(min(2 * failures, 10))
|
||||
|
||||
def watch(self):
|
||||
targets = [t for t in cdp_targets(self.agent_dir)
|
||||
if t.get("title") == "SharedJSContext" and t.get("webSocketDebuggerUrl")]
|
||||
if not targets:
|
||||
raise AgentError("NO_TARGET", "No SharedJSContext yet")
|
||||
ws = WebSocket(targets[0]["webSocketDebuggerUrl"], 5)
|
||||
try:
|
||||
self.call(ws, "Runtime.addBinding", {"name": KEYBOARD_BINDING})
|
||||
self.report(self.install(ws))
|
||||
self.attached = True
|
||||
while True:
|
||||
if not ws.buffer and not select.select([ws.s], [], [], KEYBOARD_CHECK_SECONDS)[0]:
|
||||
self.report(self.install(ws))
|
||||
continue
|
||||
self.event(json.loads(ws.recv()))
|
||||
finally:
|
||||
ws.close()
|
||||
|
||||
def install(self, ws):
|
||||
result = self.call(ws, "Runtime.evaluate", {"expression": KEYBOARD_JS, "returnByValue": True})
|
||||
if result.get("exceptionDetails"):
|
||||
raise AgentError("JS_EXCEPTION", json.dumps(result["exceptionDetails"])[:500])
|
||||
return result.get("result", {}).get("value") is True
|
||||
|
||||
def call(self, ws, method, params):
|
||||
self.next_id += 1
|
||||
ws.send(json.dumps({"id": self.next_id, "method": method, "params": params}))
|
||||
deadline = time.monotonic() + 10
|
||||
while time.monotonic() < deadline:
|
||||
message = json.loads(ws.recv())
|
||||
if message.get("id") == self.next_id:
|
||||
if "error" in message:
|
||||
raise AgentError("CDP_ERROR", json.dumps(message["error"]))
|
||||
return message.get("result", {})
|
||||
self.event(message)
|
||||
raise AgentError("TIMEOUT", "No answer to %s from DevTools" % method)
|
||||
|
||||
def event(self, message):
|
||||
params = message.get("params") or {}
|
||||
if message.get("method") == "Runtime.bindingCalled" and params.get("name") == KEYBOARD_BINDING:
|
||||
self.report(params.get("payload") == "true")
|
||||
|
||||
|
||||
def steam_idle_inhibit(agent_dir, inhibit):
|
||||
expression = """(async want => {
|
||||
const toggle = () => new Promise(done => {
|
||||
@@ -918,6 +1024,8 @@ def serve(agent_dir, focus=True, poll=0.1):
|
||||
can_focus = focus and bool(os.environ.get("DISPLAY"))
|
||||
if can_focus:
|
||||
FocusWatcher(agent_dir / "focus.json").start()
|
||||
if focus:
|
||||
KeyboardWatcher(agent_dir).start()
|
||||
write_json(agent_dir / "hello.json", {"pid": os.getpid(), "version": VERSION, "started": int(time.time() * 1000),
|
||||
"displays": displays(), "focus": can_focus})
|
||||
log("serving %s (focus %s)" % (agent_dir, "on" if can_focus else "off"))
|
||||
|
||||
Reference in new issue
Block a user