#!/usr/bin/python3 import base64 import ctypes import json import os import runpy import select import signal import socket import struct import subprocess import sys import time HERE = os.path.dirname(os.path.realpath(__file__)) COMPAT = runpy.run_path(os.path.join(HERE, "steam-compatibility")) BINDING = "droiddeckCompatChanged" INSTALL_JS = r""" (() => { if (window.__droiddeckCompat) return "present"; const enc = new TextEncoder(), dec = new TextDecoder(); const varint = (value, out) => { let n = BigInt(value); do { let b = Number(n & 0x7fn); n >>= 7n; if (n) b |= 0x80; out.push(b); } while (n); }; const key = (field, wire, out) => varint(field * 8 + wire, out); const str = (field, s, out) => { const b = enc.encode(s); key(field, 2, out); varint(b.length, out); for (const x of b) out.push(x); }; const uint = (field, v, out) => { key(field, 0, out); varint(v, out); }; const fixed64 = (field, v, out) => { key(field, 1, out); let n = BigInt(v); for (let i = 0; i < 8; i++) { out.push(Number(n & 0xffn)); n >>= 8n; } }; const parse = (bytes) => { const fields = {}; let i = 0; const read = () => { let r = 0n, s = 0n, b; do { b = bytes[i++]; r |= BigInt(b & 0x7f) << s; s += 7n; } while (b & 0x80); return r; }; while (i < bytes.length) { const k = Number(read()), field = k >> 3, wire = k & 7; let v; if (wire === 0) v = read(); else if (wire === 1) { v = 0n; for (let j = 0; j < 8; j++) v |= BigInt(bytes[i + j]) << BigInt(8 * j); i += 8; } else if (wire === 2) { const n = Number(read()); v = bytes.subarray(i, i + n); i += n; } else if (wire === 5) { v = bytes.subarray(i, i + 4); i += 4; } else throw new Error("unexpected wire type " + wire); (fields[field] = fields[field] || []).push(v); } return fields; }; const text = (f, n) => f[n] ? dec.decode(f[n][0]) : ""; const self = { seq: 0, pending: new Map(), ws: null, opening: null, changes: 0 }; const frame = (method, body, auth) => { const h = []; fixed64(10, ++self.seq, h); str(12, method, h); if (auth) str(40, auth, h); const out = new Uint8Array(8 + h.length + body.length); const view = new DataView(out.buffer); view.setUint32(0, (0x80000000 | 146) >>> 0, true); view.setUint32(4, h.length, true); out.set(h, 8); out.set(body, 8 + h.length); return [self.seq, out]; }; const onMessage = (event) => { const bytes = new Uint8Array(event.data); const length = new DataView(event.data).getUint32(4, true); const header = parse(bytes.subarray(8, 8 + length)); const target = header[11] ? Number(header[11][0] & 0xffffffffn) : -1; if (self.pending.has(target)) { const done = self.pending.get(target); self.pending.delete(target); const result = header[13] ? Number(header[13][0]) : 2; done(result === 1 ? { ok: true, body: parse(bytes.subarray(8 + length)) } : { ok: false, eresult: result, error: text(header, 14) }); return; } if (text(header, 12).startsWith("CompatManager.NotifyStateChanged")) { self.changes++; if (typeof window.__BINDING__ === "function") window.__BINDING__(String(self.changes)); } }; const send = (method, body, auth) => new Promise((resolve, reject) => { const [id, out] = frame(method, Uint8Array.from(body), auth); const timer = setTimeout(() => { self.pending.delete(id); reject(new Error(method + " timed out")); }, 10000); self.pending.set(id, (r) => { clearTimeout(timer); resolve(r); }); self.ws.send(out); }); const open = async () => { const info = await SteamClient.WebUITransport.GetTransportInfo(); const ws = new WebSocket("ws://127.0.0.1:" + info.portClientdll + "/transportsocket/"); ws.binaryType = "arraybuffer"; await new Promise((resolve, reject) => { ws.onopen = resolve; ws.onerror = () => reject(new Error("the client's transport refused the connection")); }); ws.onmessage = onMessage; ws.onclose = () => { if (self.ws === ws) self.ws = null; for (const done of self.pending.values()) done({ ok: false, eresult: 0, error: "connection closed" }); self.pending.clear(); }; self.ws = ws; const body = []; str(1, info.authKeyClientdll, body); const r = await send("TransportAuth.Authenticate#1", body, info.authKeyClientdll); if (!r.ok) { ws.close(); throw new Error("the client's transport refused authentication (" + r.eresult + ")"); } }; const ready = async () => { if (self.ws && self.ws.readyState === 1) return; if (!self.opening) self.opening = open().finally(() => { self.opening = null; }); await self.opening; }; self.get = async (appid) => { await ready(); const body = []; uint(1, appid, body); const r = await send("CompatManager.GetCompatTools#1", body); if (!r.ok) throw Object.assign(new Error("GetCompatTools " + appid + " failed (" + r.eresult + " " + r.error + ")"), { eresult: r.eresult }); return { selected: text(r.body, 3), fallback: text(r.body, 4), tools: (r.body[1] || []).map((t) => text(parse(t), 1)) }; }; self.set = async (appid, name) => { await ready(); const body = []; uint(1, appid, body); str(2, name, body); const r = await send("CompatManager.SpecifyCompatTool#1", body); if (!r.ok) throw new Error("SpecifyCompatTool " + appid + " " + name + " failed (" + r.eresult + " " + r.error + ")"); return true; }; self.many = async (apps) => { const out = {}; for (const app of apps) { try { out[app] = (await self.get(Number(app))).selected; } catch (e) { if (e.eresult !== 128) throw e; } } return out; }; window.__droiddeckCompat = self; return "installed"; })() """.replace("__BINDING__", BINDING) TOOLS_JS = r""" (() => { if (window.__droiddeckTools) return "present"; const installs = SteamClient.Installs, downloads = SteamClient.Downloads; if (typeof installs?.OpenInstallWizard !== "function" || typeof installs?.RegisterForShowInstallWizard !== "function" || typeof installs?.ContinueInstall !== "function" || typeof downloads?.QueueAppUpdate !== "function") throw new Error("the client's install calls are not ready"); const self = { asked: new Map() }; installs.RegisterForShowInstallWizard((state) => { const app = Number(state?.currentAppID); if (state?.eInstallState === 7 && Date.now() - (self.asked.get(app) || 0) < 120000) { self.asked.delete(app); installs.ContinueInstall(); } }); self.install = (app) => { self.asked.set(app, Date.now()); installs.OpenInstallWizard([app], "0"); return true; }; self.update = (app) => { downloads.QueueAppUpdate(app, "0"); return true; }; window.__droiddeckTools = self; return "installed"; })() """ TOOL_CHECK_SECONDS = 60 TOOL_RETRY_SECONDS = 300 SWITCH_BINDING = "droiddeckSwitchToDesktop" SWITCH_JS = r""" (() => { if (window.__droiddeckSwitch) return "present"; const name = new TextEncoder().encode("SteamOSManager.SwitchToDesktop#"); const send = WebSocket.prototype.send; const asks = (data) => { const bytes = data instanceof ArrayBuffer ? new Uint8Array(data) : ArrayBuffer.isView(data) ? new Uint8Array(data.buffer, data.byteOffset, data.byteLength) : null; if (!bytes) return false; const end = Math.min(bytes.length, 256) - name.length; for (let i = 8; i <= end; i++) { let j = 0; while (j < name.length && bytes[i + j] === name[j]) j++; if (j === name.length) return true; } return false; }; WebSocket.prototype.send = function (data) { if (typeof window.__BINDING__ === "function" && asks(data)) window.__BINDING__(""); return send.call(this, data); }; window.__droiddeckSwitch = true; return "installed"; })() """.replace("__BINDING__", SWITCH_BINDING) SESSION_SELECT = "/usr/bin/steamos-session-select" # Steam opens its Steam Controller welcome tour at every start while the client setting # controller_show_ibex_tour (CMsgClientSettings field 140014) is on, and only a finished tour turns it # off; closing it leaves it on. The app's pad is an emulated controller, so the tour never applies. # A pending firmware update still opens Steam's updater, which checks the update priority instead. TOUR_JS = r""" (() => { let key = 140014 * 8; const bytes = []; while (key > 127) { bytes.push((key & 127) | 128); key = Math.floor(key / 128); } bytes.push(key, 0); SteamClient.Settings.SetSetting(btoa(String.fromCharCode(...bytes))); return "off"; })() """ FILE_BINDING = "droiddeckOpenFileDialog" FILE_JS = r""" (() => { const cancelled = () => ({ result: 2, message: "" }); if (window.__droiddeckFiles) { window.__droiddeckFiles.cancelAll(); return "present"; } const self = { next: 0, pending: new Map() }; const open = function (options) { return new Promise((resolve, reject) => { if (typeof window.__BINDING__ !== "function") { reject(cancelled()); return; } const id = ++self.next; self.pending.set(id, { resolve, reject }); try { window.__BINDING__(JSON.stringify({ id, options: options || {} })); } catch (e) { self.pending.delete(id); reject(cancelled()); } }); }; self.done = (id, path) => { const item = self.pending.get(id); if (!item) return false; self.pending.delete(id); if (typeof path === "string" && path) item.resolve(path); else item.reject(cancelled()); return true; }; self.cancelAll = () => { const items = Array.from(self.pending.values()); self.pending.clear(); for (const item of items) item.reject(cancelled()); }; const patch = (w) => { const system = w?.SteamClient?.System; if (system && system.OpenFileDialog !== open) system.OpenFileDialog = open; }; patch(window); if (SteamClient.System.OpenFileDialog !== open) throw new Error("Steam OpenFileDialog could not be redirected"); const popups = window.g_PopupManager; if (popups) { for (const popup of popups.GetPopups?.() || []) patch(popup.window); popups.AddPopupCreatedCallback?.((popup) => patch(popup.window)); } window.__droiddeckFiles = self; return "installed"; })() """.replace("__BINDING__", FILE_BINDING) VR_BINDING = "droiddeckLaunchSteamVr" VR_HELPER = "/usr/local/bin/droiddeck-steamvr" VR_JS = r""" (() => { if (window.__droiddeckSteamVr) return "present"; const apps = SteamClient.Apps; if (!apps || typeof apps.RunGame !== "function") throw new Error("Steam RunGame is not ready"); const run = apps.RunGame; const self = { running: false, pending: new Map(), lastError: null }; const flush = () => { if (!self.running) return; const queued = Array.from(self.pending.values()); self.pending.clear(); for (const item of queued) { clearTimeout(item.timer); run.call(item.receiver, item.id, ...item.args); } }; if (typeof SteamClient.OpenVR?.RegisterForVRModeChange === "function") { self.subscription = SteamClient.OpenVR.RegisterForVRModeChange(running => { self.running = !!running; flush(); }); } const wrapped = function (id, ...args) { const gameid = String(id); if (typeof window.__BINDING__ !== "function") return run.call(this, id, ...args); if (gameid === "250820") { window.__BINDING__(gameid); return; } const overview = window.appStore?.GetAppOverviewByGameID?.(gameid); const vr = overview?.vr_only === true; if (vr && self.subscription && /^[1-9][0-9]{0,19}$/.test(gameid)) { if (self.pending.has(gameid)) return; self.lastError = null; const item = { receiver: this, id, args, timer: null }; item.timer = setTimeout(() => { self.pending.delete(gameid); self.lastError = "SteamVR did not become ready. Start SteamVR from DroidDeck and try the game again."; console.error("DroidDeck: " + self.lastError); }, 600000); self.pending.set(gameid, item); try { window.__BINDING__(gameid); } catch (e) { clearTimeout(item.timer); self.pending.delete(gameid); throw e; } flush(); return; } return run.call(this, id, ...args); }; apps.RunGame = wrapped; if (apps.RunGame !== wrapped) throw new Error("Steam RunGame could not be redirected"); if (typeof SteamClient.OpenVR?.StartVR === "function") { const start = SteamClient.OpenVR.StartVR; SteamClient.OpenVR.StartVR = function (...args) { if (typeof window.__BINDING__ === "function") return window.__BINDING__("250820"); return start.apply(this, args); }; } window.__droiddeckSteamVr = self; return "installed"; })() """.replace("__BINDING__", VR_BINDING) def launch_steamvr(): """Keep downloads/startup off the Steam UI bridge's event loop.""" say("SteamVR library launch: using the DroidDeck ARM64 helper") state = os.path.join(os.environ.get("XDG_RUNTIME_DIR", "/tmp"), "droiddeck-steamvr") try: os.makedirs(state, exist_ok=True) with open(os.path.join(state, "library-launch.log"), "a") as log: return subprocess.Popen([VR_HELPER, "launch"], stdin=subprocess.DEVNULL, stdout=log, stderr=subprocess.STDOUT, start_new_session=True) except OSError as e: say("SteamVR library launch failed: %s" % e) def choose_file(options): import gi gi.require_version("Gdk", "3.0") gi.require_version("Gtk", "3.0") from gi.repository import Gdk, Gtk directory = bool(options.get("bChooseDirectory")) save = bool(options.get("bSaveMode")) and not directory action = (Gtk.FileChooserAction.SELECT_FOLDER if directory else Gtk.FileChooserAction.SAVE if save else Gtk.FileChooserAction.OPEN) dialog = Gtk.FileChooserDialog(title=str(options.get("strTitle") or ""), action=action) dialog.add_buttons("gtk-cancel", Gtk.ResponseType.CANCEL, "gtk-save" if save else "gtk-open", Gtk.ResponseType.ACCEPT) dialog.set_default_response(Gtk.ResponseType.ACCEPT) dialog.set_local_only(True) dialog.set_do_overwrite_confirmation(True) for folder in ("/root/Storage", "/mnt/droiddeck-sd"): if os.path.isdir(folder): try: dialog.add_shortcut_folder(folder) except Exception: pass if not directory: for entry in options.get("rgFilters") or []: patterns = [str(p) for p in entry.get("rFilePatterns") or [] if p] if not patterns: continue chosen = Gtk.FileFilter() chosen.set_name(str(entry.get("strFileTypeName") or ", ".join(patterns))) for pattern in patterns: pattern = "*" if pattern == "*.*" else pattern chosen.add_pattern(pattern) if pattern.lower() != pattern.upper(): chosen.add_pattern(pattern.upper()) dialog.add_filter(chosen) if entry.get("bUseAsDefault"): dialog.set_filter(chosen) initial = str(options.get("strInitialFile") or "").strip('"') if initial: if os.path.isdir(initial): dialog.set_current_folder(initial) elif save: if os.path.isdir(os.path.dirname(initial)): dialog.set_current_folder(os.path.dirname(initial)) dialog.set_current_name(os.path.basename(initial)) elif os.path.exists(initial): dialog.set_filename(initial) elif os.path.isdir(os.path.dirname(initial)): dialog.set_current_folder(os.path.dirname(initial)) screen = Gdk.Screen.get_default() if screen is not None: dialog.set_default_size(screen.get_width(), screen.get_height()) chosen_path = dialog.get_filename() if dialog.run() == Gtk.ResponseType.ACCEPT else None dialog.destroy() return chosen_path class Closed(Exception): pass def say(message): print("droiddeck-steam-compat: %s" % message, flush=True) def called(event, name): return event.get("method") == "Runtime.bindingCalled" and event.get("params", {}).get("name") == name def switch_to_desktop(): say("Switch to Desktop: asking for the desktop") try: subprocess.call([SESSION_SELECT, "plasma"]) except OSError as e: say("Switch to Desktop failed: %s" % e) def connect(port, timeout=5): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.settimeout(timeout) try: sock.bind(("127.0.0.1", 0)) sock.connect(("127.0.0.1", port)) except OSError: sock.close() raise return sock def http_get(port, path): sock = connect(port) try: sock.sendall(("GET %s HTTP/1.1\r\nHost: 127.0.0.1:%d\r\nConnection: close\r\n\r\n" % (path, port)).encode()) data = b"" length = None while length is None or len(data) < length: chunk = sock.recv(65536) if not chunk: break data += chunk if length is None and b"\r\n\r\n" in data: head, _, rest = data.partition(b"\r\n\r\n") for line in head.split(b"\r\n")[1:]: name, _, value = line.partition(b":") if name.strip().lower() == b"content-length": length = int(value.strip()) if length is None: length = float("inf") data = rest if not head.startswith(b"HTTP/1.1 200"): raise OSError("devtools answered %r" % head[:40]) finally: sock.close() return json.loads(data.decode("utf-8", "replace")) class WebSocket: def __init__(self, port, path): self.sock = connect(port) key = base64.b64encode(os.urandom(16)).decode() self.sock.sendall(("GET %s HTTP/1.1\r\nHost: 127.0.0.1:%d\r\nUpgrade: websocket\r\nConnection: Upgrade\r\n" "Sec-WebSocket-Key: %s\r\nSec-WebSocket-Version: 13\r\n\r\n" % (path, port, key)).encode()) self.buffer = b"" while b"\r\n\r\n" not in self.buffer: self.fill() head, _, self.buffer = self.buffer.partition(b"\r\n\r\n") if not head.startswith(b"HTTP/1.1 101"): raise OSError("devtools refused the websocket: %r" % head[:60]) self.sock.settimeout(None) def fileno(self): return self.sock.fileno() def fill(self): chunk = self.sock.recv(65536) if not chunk: raise Closed("devtools closed the connection") self.buffer += chunk def take(self, n): while len(self.buffer) < n: self.fill() data, self.buffer = self.buffer[:n], self.buffer[n:] return data def send(self, text, opcode=1): payload = text.encode() if isinstance(text, str) else text mask = os.urandom(4) n = len(payload) if n < 126: head = struct.pack("!BB", 0x80 | opcode, 0x80 | n) elif n < 65536: head = struct.pack("!BBH", 0x80 | opcode, 0xFE, n) else: head = struct.pack("!BBQ", 0x80 | opcode, 0xFF, n) self.sock.sendall(head + mask + bytes(b ^ mask[i % 4] for i, b in enumerate(payload))) def pending(self): return len(self.buffer) > 0 def recv(self): message = b"" while True: first, second = self.take(2) n = second & 0x7F if n == 126: n = struct.unpack("!H", self.take(2))[0] elif n == 127: n = struct.unpack("!Q", self.take(8))[0] mask = self.take(4) if second & 0x80 else None payload = self.take(n) if mask: payload = bytes(b ^ mask[i % 4] for i, b in enumerate(payload)) opcode = first & 0x0F if opcode == 8: raise Closed("devtools closed the websocket") if opcode == 9: self.send(payload, 10) continue if opcode == 10: continue message += payload if first & 0x80: return message.decode("utf-8", "replace") def close(self): try: self.sock.close() except OSError: pass class Page: def __init__(self, port, path): self.ws = WebSocket(port, path) self.next_id = 0 self.events = [] def call(self, method, params=None, timeout=30): self.next_id += 1 mine = self.next_id self.ws.send(json.dumps({"id": mine, "method": method, "params": params or {}})) deadline = time.monotonic() + timeout while True: if not self.ws.pending(): left = deadline - time.monotonic() if left <= 0 or not select.select([self.ws], [], [], left)[0]: raise TimeoutError("%s timed out" % method) message = json.loads(self.ws.recv()) if message.get("id") == mine: if "error" in message: raise RuntimeError("%s: %s" % (method, message["error"].get("message"))) return message.get("result", {}) if "method" in message: self.events.append(message) def evaluate(self, expression, timeout=30): result = self.call("Runtime.evaluate", {"expression": expression, "awaitPromise": True, "returnByValue": True}, timeout) if "exceptionDetails" in result: details = result["exceptionDetails"] raise RuntimeError((details.get("exception") or {}).get("description") or details.get("text") or "script failed") return result.get("result", {}).get("value") def tools(self, call): return self.evaluate("%s;\nwindow.__droiddeckTools.%s" % (TOOLS_JS, call)) def compat(self, call): return self.evaluate("%s;\nwindow.__droiddeckCompat.%s" % (INSTALL_JS, call)) def drain(self): while True: if not self.ws.pending() and not select.select([self.ws], [], [], 0)[0]: break message = json.loads(self.ws.recv()) if "method" in message: self.events.append(message) events, self.events = self.events, [] return events def shared_context(port): for target in http_get(port, "/json/list"): if target.get("title") == "SharedJSContext" and target.get("webSocketDebuggerUrl"): path = target["webSocketDebuggerUrl"].split("/", 3)[3] return "/" + path return None class Watcher: MASK = 0x00000008 | 0x00000080 | 0x00000100 | 0x00000200 def __init__(self, files): self.files = files self.fd = -1 try: libc = ctypes.CDLL(None, use_errno=True) fd = libc.inotify_init1(os.O_NONBLOCK | os.O_CLOEXEC) dirs = sorted({os.path.dirname(f) for f in files if os.path.isdir(os.path.dirname(f))}) if fd >= 0 and dirs and all(libc.inotify_add_watch(fd, d.encode(), self.MASK) >= 0 for d in dirs): self.fd = fd elif fd >= 0: os.close(fd) except (OSError, AttributeError): self.fd = -1 self.stamp = self.snapshot() def snapshot(self): stamp = [] for f in self.files: for p in (f, os.path.dirname(f)): try: stamp.append(os.stat(p).st_mtime_ns) except OSError: stamp.append(0) return stamp def fileno(self): return self.fd def changed(self): if self.fd < 0: stamp = self.snapshot() moved, self.stamp = stamp != self.stamp, stamp return moved names = set() try: while True: data = os.read(self.fd, 65536) if not data: break at = 0 while at + 16 <= len(data): _, _, _, size = struct.unpack_from("iIII", data, at) names.add(data[at + 16:at + 16 + size].split(b"\0", 1)[0].decode("utf-8", "replace")) at += 16 + size except BlockingIOError: pass wanted = {os.path.basename(f) for f in self.files} return any(n in wanted or n.startswith("appmanifest_") for n in names) class Helper: def __init__(self, steam_root, port, steamos=False, files_only=False): self.steam_root = steam_root self.port = port self.steamos = steamos self.files_only = files_only self.page = None self.known = set() self.installed = None self.fex_asked = set() self.tools_asked = {} self.tools_checked = None self.vr_launch = None self.chooser = None def open_chooser(self, payload): try: request = json.loads(payload) ident = int(request["id"]) options = request.get("options", {}) if not isinstance(options, dict): raise ValueError("options are not an object") except (ValueError, KeyError, TypeError) as e: say("ignored a file dialog request (%s)" % e) return if self.chooser is not None: self.finish_chooser(ident, None) return command = [sys.executable, os.path.realpath(__file__), "--choose-file"] # Gamescope shows the dialog over the client by the AppId on a reaper above it; the x86-64 client has no # native reaper, so the arm64 client's is borrowed when it is installed. data = os.environ.get("XDG_DATA_HOME") or os.path.join(os.path.expanduser("~"), ".local", "share") for root in (self.steam_root, os.path.join(data, "Steam")): reaper = os.path.join(root, "steamrtarm64", "reaper") if os.access(reaper, os.X_OK): command = [reaper, "SteamLaunch", "AppId=769", "--"] + command break try: proc = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, start_new_session=True) proc.stdin.write(json.dumps(options).encode("utf-8")) proc.stdin.close() except OSError as e: say("could not open a file dialog (%s)" % e) self.finish_chooser(ident, None) return say("file dialog open: %s" % (options.get("strTitle") or "untitled")) self.chooser = (ident, proc) def chooser_done(self): ident, proc = self.chooser self.chooser = None out = proc.stdout.read().decode("utf-8", "replace") proc.stdout.close() proc.wait() if proc.returncode == 2: say("file dialog needs GTK 3 and PyGObject, which come with the desktop") try: path = json.loads(out) if proc.returncode == 0 else None except ValueError: path = None self.finish_chooser(ident, path if isinstance(path, str) and path else None) def finish_chooser(self, ident, path): say("file dialog %s" % ("chose " + path if path else "cancelled")) self.page.evaluate("window.__droiddeckFiles?.done(%d, %s)" % (ident, json.dumps(path))) def close_chooser(self): if self.chooser is not None: _, proc = self.chooser self.chooser = None try: os.killpg(proc.pid, signal.SIGTERM) except OSError: pass proc.stdout.close() proc.wait() def attach(self): path = shared_context(self.port) if path is None: raise LookupError("no SharedJSContext yet") page = Page(self.port, path) try: if page.evaluate("typeof SteamClient === 'object' && typeof SteamClient.WebUITransport === 'object'") is not True: raise LookupError("SteamClient is not ready") if self.files_only: page.call("Runtime.addBinding", {"name": FILE_BINDING}) page.evaluate(FILE_JS) self.page = page return True page.call("Runtime.addBinding", {"name": BINDING}) page.call("Runtime.addBinding", {"name": VR_BINDING}) page.evaluate(VR_JS) page.call("Runtime.addBinding", {"name": FILE_BINDING}) page.evaluate(FILE_JS) try: page.evaluate(TOUR_JS) except RuntimeError: # The optional tour setting must not block the compat bridge. pass if not self.steamos: page.call("Runtime.addBinding", {"name": SWITCH_BINDING}) page.evaluate(SWITCH_JS) page.compat("get(0)") except Exception: page.ws.close() raise self.page = page return True def reconcile(self): page = self.page with COMPAT["locked"](): state_file = COMPAT["state_path"]("state.json") state = COMPAT["read_json"](state_file) request = COMPAT["read_json"](COMPAT["state_path"]("request.json")) catalog = COMPAT["tool_catalog"](self.steam_root) top = page.compat("get(0)") self.known = set(top["tools"]) live = [t for t in catalog if t["name"] in self.known] wanted = COMPAT["requested_tool"](request, catalog) newer = isinstance(request.get("seq"), int) and request["seq"] > int(state.get("applied", 0) or 0) waiting = newer and wanted is not None and wanted not in self.known default, state = COMPAT["choose_default"](top["selected"], state, request, live, apply_request=not waiting) installed = COMPAT["installed_apps"](self.steam_root) self.installed = installed picks = COMPAT["valve_picks"](self.steam_root) prior_auto = state.get("auto", {}) if not isinstance(prior_auto, dict): prior_auto = {} # "native" from earlier builds also lists dual-platform games nobody chose to run natively. prior_native = state.get("native_choices", ()) native_choices = set(prior_native if isinstance(prior_native, (list, tuple, set)) else ()) picked = COMPAT["picked_apps"](state) runnable = {t["name"].lower() for t in live} apps = sorted(set(installed) | set(picks) | set(prior_auto) | native_choices, key=COMPAT["app_order"]) asked = [app for app, request in COMPAT["game_requests"]().items() if COMPAT["valid_game_request"](app, request)] apps = sorted(set(apps) | set(asked), key=COMPAT["app_order"]) current = dict(page.compat("many(%s)" % json.dumps(apps)) or {}) refused = set(apps) - set(current) refused_native = native_choices & refused if refused: apps = [app for app in apps if app not in refused] installed = [app for app in installed if app not in refused] picks = [app for app in picks if app not in refused] prior_auto = {app: tool for app, tool in prior_auto.items() if app not in refused} if isinstance(state.get("auto"), dict): state["auto"] = prior_auto native_choices -= refused current["0"] = top["selected"] native = COMPAT["native_targets"](self.steam_root, apps) raw_mapping = COMPAT["read_config_mapping"]( os.path.join(self.steam_root, "config", "config.vdf")) if raw_mapping is None: for app in native_choices: current[app] = "" for app in set(native) - native_choices: if not current.get(app) and app not in picked: current.pop(app, None) else: for app in set(native) | native_choices: if app in raw_mapping: # The client shows an explicit native choice as the tool it inherits, and writes config.vdf late. if raw_mapping[app] == "" or current.get(app) not in self.known: current[app] = raw_mapping[app] elif app in prior_auto or app in native_choices or app in picked and ( not current.get(app) or COMPAT["unrunnable"](current[app], runnable)): current[app] = "" elif not current.get(app): current.pop(app, None) requests = COMPAT["settled_game_requests"](state, current, catalog) overrides = COMPAT["requested_games"](requests or {}, catalog, [t["name"] for t in live]) overrides = {app: tool for app, tool in overrides.items() if app not in refused} changes, auto = COMPAT["plan_mapping"]( current, installed, [t["name"] for t in live], default, COMPAT["followers"](state, current), native, picks, overrides, prior_auto.keys(), native_choices) for app, tool in sorted(changes.items(), key=lambda item: COMPAT["app_order"](item[0])): page.compat("set(%d, %s)" % (int(app), json.dumps(tool if tool is not None else ""))) say("%s -> %s" % ("default" if app == "0" else "app %s" % app, tool if tool is not None else "Steam native default")) state.update( auto={app: tool for app, tool in auto.items() if tool is not COMPAT["NATIVE_CHOICE"]}, native_choices=sorted(refused_native | {app for app, tool in auto.items() if tool is COMPAT["NATIVE_CHOICE"]}), picked=COMPAT["picked_after"](current, changes), tools=catalog, live=True, games_applied=COMPAT["applied_game_requests"](requests, overrides, state)) state.pop("native", None) if waiting: state["pending"] = wanted say("%s is new to the running client; it applies when Steam next starts" % wanted) else: state.pop("pending", None) COMPAT["write_json"](state_file, state) def ensure_tools(self): """Proton, FEX and the runtimes FEX needs are fetched as soon as the client is up, never left pending.""" now = time.monotonic() if self.tools_checked is not None and now - self.tools_checked < TOOL_CHECK_SECONDS: return self.tools_checked = now if COMPAT["game_running"](): return for app, action in COMPAT["tool_downloads"](self.steam_root): if now - self.tools_asked.get(app, -TOOL_RETRY_SECONDS) < TOOL_RETRY_SECONDS: continue self.tools_asked[app] = now try: self.page.tools("%s(%d)" % (action, int(app))) say("asked the client to %s %s" % (action, app)) except Exception as e: say("the client could not %s %s (%s)" % (action, app, e)) if action == "install": for asked in COMPAT["request_fex"](self.steam_root, self.fex_asked): say("asked the client to install %s through steam://install" % asked) def request_waiting(self): request = COMPAT["read_json"](COMPAT["state_path"]("request.json")) state = COMPAT["read_json"](COMPAT["state_path"]("state.json")) return isinstance(request.get("seq"), int) and request["seq"] > int(state.get("applied", 0) or 0) def drain(self): """The page's events; a switch to the desktop is acted on as it comes.""" events = self.page.drain() if any(called(e, SWITCH_BINDING) for e in events): switch_to_desktop() vr_games = {e.get("params", {}).get("payload") for e in events if called(e, VR_BINDING) and isinstance(e.get("params", {}).get("payload"), str)} vr_games = {game for game in vr_games if isinstance(game, str) and game.isascii() and game.isdecimal() and not game.startswith("0") and len(game) <= 20 and int(game) <= 0xffffffffffffffff} if vr_games and (self.vr_launch is None or self.vr_launch.poll() is not None): say("preparing SteamVR for library app(s): " + ", ".join(sorted(vr_games))) self.vr_launch = launch_steamvr() for e in events: if called(e, FILE_BINDING) and isinstance(e.get("params", {}).get("payload"), str): self.open_chooser(e["params"]["payload"]) return events def serve_files(self): say("attached to the client on port %d for its file dialogs" % self.port) while True: sources = [self.page.ws] + ([self.chooser[1].stdout] if self.chooser is not None else []) queued = self.page.ws.pending() or bool(self.page.events) ready = [self.page.ws] if queued else select.select(sources, [], [], 30)[0] if self.chooser is not None and self.chooser[1].stdout in ready: self.chooser_done() if self.page.ws in ready or self.page.events: for e in self.page.drain(): if called(e, FILE_BINDING) and isinstance(e.get("params", {}).get("payload"), str): self.open_chooser(e["params"]["payload"]) def serve(self, watcher): if self.files_only: return self.serve_files() with open(COMPAT["state_path"]("live.pid"), "w") as f: f.write("%d\n" % os.getpid()) say("attached to the client on port %d" % self.port) self.reconcile() self.ensure_tools() while True: sources = [self.page.ws] + ([watcher] if watcher.fileno() >= 0 else []) if self.chooser is not None: sources.append(self.chooser[1].stdout) ready = [self.page.ws] if self.page.ws.pending() else select.select(sources, [], [], 30 if watcher.fileno() >= 0 else 2)[0] if self.chooser is not None and self.chooser[1].stdout in ready: self.chooser_done() due = False if self.page.ws in ready: due = any(called(e, BINDING) for e in self.drain()) if (watcher in ready or watcher.fileno() < 0) and watcher.changed(): due = True if due: time.sleep(0.3) if watcher.fileno() >= 0 and select.select([watcher], [], [], 0)[0]: watcher.changed() self.drain() self.reconcile() self.ensure_tools() def detach(): try: os.unlink(COMPAT["state_path"]("live.pid")) except OSError: pass with COMPAT["locked"](): state_file = COMPAT["state_path"]("state.json") state = COMPAT["read_json"](state_file) if state.get("live"): state["live"] = False COMPAT["write_json"](state_file, state) def chooser_main(): try: options = json.load(sys.stdin) except ValueError: return 1 try: path = choose_file(options if isinstance(options, dict) else {}) except (ImportError, ValueError): return 2 if not path: return 1 print(json.dumps(path)) return 0 def main(): files_only = sys.argv[1:2] == ["--files-only"] steam_root = sys.argv[2 if files_only else 1] try: port = int(os.environ.get("BL_CDP_PORT", "")) except ValueError: say("no devtools port for this session; the client's mapping is kept through config.vdf") return 0 if files_only: helper = Helper(steam_root, port, files_only=True) failures = 0 while True: try: helper.attach() failures = 0 helper.serve(None) except Exception as e: helper.close_chooser() if helper.page is not None: say("lost the client (%s); reattaching" % e) helper.page.ws.close() helper.page = None failures += 1 time.sleep(min(2 * max(failures, 1), 10)) os.makedirs(COMPAT["COMPAT_DIR"], exist_ok=True) watcher = Watcher([COMPAT["state_path"]("request.json"), COMPAT["state_path"]("games.json")] + [os.path.join(lib, "steamapps", "libraryfolders.vdf") for lib in (steam_root, COMPAT["LIBRARY"] + "-sd")]) steamos = os.environ.get("BL_STEAMDECK") == "1" and os.environ.get("BL_STEAM_UI") != "desktop" helper = Helper(steam_root, port, steamos) failures = 0 last = None while True: try: helper.attach() failures = 0 last = None helper.serve(watcher) except Exception as e: helper.close_chooser() if helper.page is not None: say("lost the client (%s); reattaching" % e) helper.page.ws.close() helper.page = None detach() elif str(e) != last: say("waiting for the client (%s)" % e) last = str(e) failures += 1 time.sleep(min(2 * max(failures, 1), 10)) if __name__ == "__main__": if sys.argv[1:2] == ["--choose-file"]: sys.exit(chooser_main()) signal.signal(signal.SIGTERM, lambda *_: sys.exit(0)) try: sys.exit(main()) finally: if sys.argv[1:2] != ["--files-only"]: detach()