Files
DroidDeck/tools/linuxfs/overlay/usr/local/bin/droiddeck-steam-compat
T
MaxsTechReview 69c88aa344 feat: fetch Proton, FEX and the runtimes automatically, and make Linux games run under FEX (#1045)
* fix: x86 Linux games that also ship for Windows run under Proton again

Since #749 the live compatibility helper treated every installed game with
both Windows and x86 Linux builds as a user's native choice, because the
client reports an empty tool for unmapped games. Those games were left on
the Steam Linux Runtime, which cannot start them here (bwrap fails, or FEX
looks for a RootFS that is not shipped), so Valheim, Moonlighter, Half-Life 2,
TF2 and others exited within seconds.

- An unmapped game the helper never managed now gets the default Proton.
  Clearing a mapping the helper set is still kept as a native choice.
- Native choices are stored under "native_choices". The old "native" list
  also held games nobody picked, so it is dropped.

* feat: Linux builds run under FEX by default, with the runtimes fetched automatically

- Steam fetches Proton, FEX, Steam Linux Runtime 3.0 and 4.0 on the first start and keeps them updated instead of leaving them pending.
- Games that ship a Linux build go to FEX once it can run them; Windows-only games and installed Windows builds keep the Proton default.
- FEX runs games on a combined x86 runtime with the newer glibc and libstdc++ the GL and Vulkan thunks need.
- Vulkan display extensions the driver cannot handle are hidden in front of the thunk, without an audit module.
- Mono's incremental garbage collector is turned off under FEX, which stops Unity games crashing.
- x86 Steamworks finds the x86 steamclient.
- 32-bit games read the virtual controller.

* fix: controllers work in Linux Unity games such as Valheim

Joystick ioctls on an event node now fail with EINVAL like the kernel's evdev, so the SDL2 that Unity embeds reads the pad's real ID instead of ignoring it as 0000:0000.

* test: skip the switch script test without node, and the ring tests without gcc

* fix: games with a Linux build keep the Proton default

The Windows build often runs better, so titles that ship both builds stay on the default Proton again. Users can still pick FEX for one, and that choice is kept. Linux-only titles still go to FEX.

* fix: unmapped dual-platform games get Proton even when config.vdf cannot be read
2026-10-18 08:59:54 -04:00

719 lines
29 KiB
Python
Executable File

#!/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 new Error("GetCompatTools " + appid + " failed (" + r.eresult + " " + r.error + ")");
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) out[app] = (await self.get(Number(app))).selected;
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"
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 || overview?.vr_supported || overview?.BSupportsVR?.();
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)
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):
self.steam_root = steam_root
self.port = port
self.steamos = steamos
self.page = None
self.known = set()
self.installed = None
self.fex_asked = set()
self.tools_asked = {}
self.tools_checked = None
self.vr_launch = None
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")
page.call("Runtime.addBinding", {"name": BINDING})
page.call("Runtime.addBinding", {"name": VR_BINDING})
page.evaluate(VR_JS)
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 ())
apps = sorted(set(installed) | set(picks) | set(prior_auto) | native_choices, key=COMPAT["app_order"])
overrides = COMPAT["requested_games"](
COMPAT["game_requests"](), catalog, [t["name"] for t in live])
apps = sorted(set(apps) | set(overrides), key=COMPAT["app_order"])
current = dict(page.compat("many(%s)" % json.dumps(apps)) or {})
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):
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:
current[app] = ""
elif not current.get(app):
current.pop(app, None)
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(app for app, tool in auto.items() if tool is COMPAT["NATIVE_CHOICE"]),
tools=catalog, live=True)
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()
return events
def serve(self, watcher):
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 [])
ready = [self.page.ws] if self.page.ws.pending() else select.select(sources, [], [], 30 if watcher.fileno() >= 0 else 2)[0]
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 main():
steam_root = sys.argv[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
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:
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__":
signal.signal(signal.SIGTERM, lambda *_: sys.exit(0))
try:
sys.exit(main())
finally:
detach()