mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-22 22:36:00 +00:00
fix(proton): a Valve Proton picked for a game in Steam stays on the ARM64 build made from it Steam lists Valve's own Proton Experimental and Proton 11.0 next to the DroidDeck tools. They are not runnable here, so a per-game pick of one was replaced with the default. That used to be Proton Experimental ARM64, so the pick looked kept; since an installed DroidDeck-Proton became the default, the game is moved to DroidDeck-Proton instead. When Steam records one of those tools for a title the sync already knows (a title it mapped or one with a recorded pick), the title now gets the DroidDeck tool built from the same Valve depot, stops following the default and the choice is mirrored into the app's per-game setting. The same applies to Steam's default. Titles nobody picked anything for still follow the default, and Valve tools with no ARM64 build still fall back to it.
1339 lines
56 KiB
Python
Executable File
1339 lines
56 KiB
Python
Executable File
#!/usr/bin/python3
|
|
import base64
|
|
import ctypes
|
|
import json
|
|
import math
|
|
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"
|
|
BRIGHTNESS_BINDING = "droiddeckBrightnessChanged"
|
|
CONTROLS_DIR = "/etc/droiddeck-device-controls"
|
|
LIBRARY_ART_JS = r"""
|
|
(() => {
|
|
if (window.__droiddeckLibraryArt) return "present";
|
|
const store = typeof appStore === "object" ? appStore : null;
|
|
if (typeof store?.BHasCachedLibraryAsset !== "function") return "unavailable";
|
|
// The client reports which cached assets exist only after a logon that reaches Steam; before that every app reads as
|
|
// uncached and the UI asks the CDN, which is blank offline. The native check still runs, so misses are still reported.
|
|
const native = store.BHasCachedLibraryAsset.bind(store);
|
|
store.BHasCachedLibraryAsset = (appid, version, type) => native(appid, version, type) || !version;
|
|
window.__droiddeckLibraryArt = true;
|
|
return "installed";
|
|
})()
|
|
"""
|
|
BRIGHTNESS_JS = r"""
|
|
(() => {
|
|
if (window.__droiddeckBrightness) return "present";
|
|
const display = SteamClient.System?.Display;
|
|
if (!display?.RegisterForBrightnessChanges || !display?.SetBrightness) return "unavailable";
|
|
const self = { expected: null, updating: true, revision: 0, localAt: -Infinity };
|
|
// A held stick steps Steam faster than Android's readback returns; those stale readbacks would snap the slider back.
|
|
const HOLD_MS = 750;
|
|
// Returns "held" while a readback is being ignored, so the caller retries it after the hold.
|
|
self.set = value => {
|
|
if (typeof value !== "number" || !Number.isFinite(value) || value < 0 || value > 1) return true;
|
|
if (performance.now() - self.localAt < HOLD_MS) return "held";
|
|
if (!self.updating && self.expected !== null && Math.abs(value - self.expected) < 0.0001) return true;
|
|
self.expected = value;
|
|
self.updating = true;
|
|
const revision = ++self.revision;
|
|
setTimeout(() => { if (self.revision === revision) self.updating = false; }, 500);
|
|
display.SetBrightness(value);
|
|
return true;
|
|
};
|
|
self.subscription = display.RegisterForBrightnessChanges(state => {
|
|
const value = state?.flBrightness;
|
|
if (typeof value !== "number" || !Number.isFinite(value) || value < 0 || value > 1 || self.expected === null) return;
|
|
if (Math.abs(value - self.expected) < 0.0001) { self.updating = false; return; }
|
|
if (self.updating) { self.updating = false; return; }
|
|
self.expected = value;
|
|
self.localAt = performance.now();
|
|
if (typeof window.__BINDING__ === "function") window.__BINDING__(String(value));
|
|
});
|
|
window.__droiddeckBrightness = self;
|
|
return "installed";
|
|
})()
|
|
""".replace("__BINDING__", BRIGHTNESS_BINDING)
|
|
|
|
|
|
def brightness_request(payload, directory=CONTROLS_DIR):
|
|
try:
|
|
value = float(payload)
|
|
if not math.isfinite(value) or not 0 <= value <= 1:
|
|
return
|
|
path = os.path.join(directory, "brightness.request")
|
|
with open(path + ".tmp", "w") as target:
|
|
target.write(str(value) + "\n")
|
|
os.replace(path + ".tmp", path)
|
|
except (ValueError, TypeError, OSError):
|
|
return
|
|
|
|
|
|
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"
|
|
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";
|
|
})()
|
|
"""
|
|
FRAME_LIMIT_JS = r"""
|
|
(() => {
|
|
if (window.__droiddeckFrameLimit) return "present";
|
|
const settings = window.settingsStore;
|
|
let service = null;
|
|
try {
|
|
// Steam's webpack does not expose its module cache; the gamescope service client is found by
|
|
// the calls its module makes and loaded by id.
|
|
let require;
|
|
window.webpackChunksteamui.push([[Symbol("droiddeck-frame-limit")], {}, r => { require = r; }]);
|
|
for (const id of Object.keys(require.m || {})) {
|
|
const source = String(require.m[id]);
|
|
if (!source.includes("SetBlurParams") || !source.includes("RegisterForNotifyStateChanged")) continue;
|
|
let exports;
|
|
try { exports = require(id); } catch (e) { continue; }
|
|
if (!exports || typeof exports !== "object") continue;
|
|
service = Object.values(exports).find(value => value && typeof value.GetState === "function" &&
|
|
typeof value.RegisterForNotifyStateChanged === "function" && typeof value.SetBlurParams === "function");
|
|
if (service) break;
|
|
}
|
|
} catch (e) {
|
|
service = null;
|
|
}
|
|
if (!service || !settings?.clientSettings || typeof SteamClient.Settings?.SetSetting !== "function") return "unavailable";
|
|
const self = { timer: null };
|
|
const write = value => {
|
|
const bytes = [];
|
|
for (let n of [21002 * 8, value]) {
|
|
while (n > 127) { bytes.push((n & 127) | 128); n = Math.floor(n / 128); }
|
|
bytes.push(n);
|
|
}
|
|
return SteamClient.Settings.SetSetting(btoa(String.fromCharCode(...bytes)));
|
|
};
|
|
self.check = async () => {
|
|
let rates;
|
|
try {
|
|
const reply = await service.GetState({});
|
|
if (!reply.BSuccess()) return;
|
|
rates = reply.Body().toObject().state?.active_display_info?.supported_frame_rates;
|
|
} catch (e) {
|
|
return;
|
|
}
|
|
if (!Array.isArray(rates) || rates.length === 0) return;
|
|
const current = settings.clientSettings;
|
|
if (!current.gamescope_enable_app_target_framerate || rates.includes(current.gamescope_app_target_framerate)) return;
|
|
try {
|
|
await write(Math.max(...rates));
|
|
} catch (e) {
|
|
}
|
|
};
|
|
self.schedule = () => {
|
|
clearTimeout(self.timer);
|
|
self.timer = setTimeout(self.check, 1500);
|
|
};
|
|
self.subscription = service.RegisterForNotifyStateChanged(self.schedule);
|
|
self.interval = setInterval(self.schedule, 5000);
|
|
self.schedule();
|
|
window.__droiddeckFrameLimit = self;
|
|
return "installed";
|
|
})()
|
|
"""
|
|
WINDOW_BINDING = "droiddeckWindowAction"
|
|
WINDOW_TARGET = "createflags=18"
|
|
WINDOW_MOVES = {"nw": 0, "n": 1, "ne": 2, "e": 3, "se": 4, "s": 5, "sw": 6, "w": 7, "move": 8}
|
|
WINDOW_JS = r"""
|
|
(() => {
|
|
if (window.__droiddeckWindow) return "present";
|
|
const EDGE = 5, CORNER = 10, THRESHOLD = 3;
|
|
const CURSORS = { n: "ns-resize", s: "ns-resize", e: "ew-resize", w: "ew-resize",
|
|
ne: "nesw-resize", sw: "nesw-resize", nw: "nwse-resize", se: "nwse-resize" };
|
|
const send = action => window.droiddeckWindowAction(action);
|
|
const filled = () => innerWidth >= screen.availWidth && innerHeight >= screen.availHeight - 60;
|
|
const side = e => {
|
|
if (filled()) return "";
|
|
const right = innerWidth - e.clientX, bottom = innerHeight - e.clientY;
|
|
const near = (a, b) => a < EDGE || (a < CORNER && b < CORNER);
|
|
const across = Math.min(e.clientX, right), along = Math.min(e.clientY, bottom);
|
|
const v = near(e.clientY, across) ? "n" : near(bottom, across) ? "s" : "";
|
|
const h = near(e.clientX, along) ? "w" : near(right, along) ? "e" : "";
|
|
return v + h;
|
|
};
|
|
const dragging = (x, y) => {
|
|
for (const node of document.elementsFromPoint(x, y)) {
|
|
const region = getComputedStyle(node).webkitAppRegion;
|
|
if (region === "drag") return true;
|
|
if (region === "no-drag") return false;
|
|
}
|
|
return [...document.querySelectorAll("*")].some(node => {
|
|
if (getComputedStyle(node).webkitAppRegion !== "drag") return false;
|
|
const box = node.getBoundingClientRect();
|
|
return x >= box.left && x < box.right && y >= box.top && y < box.bottom;
|
|
});
|
|
};
|
|
const style = document.createElement("style");
|
|
document.documentElement.append(style);
|
|
let press = null, shown = "";
|
|
addEventListener("pointermove", e => {
|
|
if (press) {
|
|
if (Math.hypot(e.clientX - press.x, e.clientY - press.y) >= THRESHOLD) {
|
|
const hit = dragging(press.x, press.y);
|
|
press = null;
|
|
if (hit) send("move");
|
|
}
|
|
return;
|
|
}
|
|
const cursor = e.buttons ? shown : side(e);
|
|
if (cursor !== shown) {
|
|
shown = cursor;
|
|
style.textContent = cursor ? `* { cursor: ${CURSORS[cursor]} !important; }` : "";
|
|
}
|
|
}, true);
|
|
addEventListener("pointerdown", e => {
|
|
if (e.button !== 0) return;
|
|
const edge = side(e);
|
|
if (edge) {
|
|
e.stopPropagation();
|
|
e.preventDefault();
|
|
send(edge);
|
|
} else {
|
|
press = { x: e.clientX, y: e.clientY };
|
|
}
|
|
}, true);
|
|
addEventListener("pointerup", () => { press = null; }, true);
|
|
addEventListener("dblclick", e => { if (dragging(e.clientX, e.clientY) && !side(e)) send("maximize"); }, true);
|
|
SteamClient.Window.ToggleMaximize = () => send("maximize");
|
|
window.__droiddeckWindow = true;
|
|
return "installed";
|
|
})()
|
|
"""
|
|
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():
|
|
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 XMessage(ctypes.Structure):
|
|
_fields_ = [("type", ctypes.c_int), ("serial", ctypes.c_ulong), ("send_event", ctypes.c_int),
|
|
("display", ctypes.c_void_p), ("window", ctypes.c_ulong), ("message_type", ctypes.c_ulong),
|
|
("format", ctypes.c_int), ("data", ctypes.c_long * 5), ("pad", ctypes.c_long * 20)]
|
|
|
|
|
|
X_ERRORS = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_void_p, ctypes.c_void_p)(lambda display, error: 0)
|
|
|
|
|
|
def window_action(action):
|
|
if action != "maximize" and action not in WINDOW_MOVES:
|
|
return False
|
|
x11 = ctypes.CDLL("libX11.so.6")
|
|
x11.XOpenDisplay.restype = ctypes.c_void_p
|
|
x11.XOpenDisplay.argtypes = [ctypes.c_char_p]
|
|
x11.XDefaultRootWindow.restype = ctypes.c_ulong
|
|
x11.XDefaultRootWindow.argtypes = [ctypes.c_void_p]
|
|
x11.XInternAtom.restype = ctypes.c_ulong
|
|
x11.XInternAtom.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.c_int]
|
|
x11.XGetWindowProperty.argtypes = [ctypes.c_void_p, ctypes.c_ulong, ctypes.c_ulong, ctypes.c_long, ctypes.c_long,
|
|
ctypes.c_int, ctypes.c_ulong] + [ctypes.c_void_p] * 5
|
|
x11.XQueryPointer.argtypes = [ctypes.c_void_p, ctypes.c_ulong] + [ctypes.c_void_p] * 7
|
|
x11.XSendEvent.argtypes = [ctypes.c_void_p, ctypes.c_ulong, ctypes.c_int, ctypes.c_long, ctypes.c_void_p]
|
|
x11.XFree.argtypes = [ctypes.c_void_p]
|
|
x11.XFlush.argtypes = [ctypes.c_void_p]
|
|
x11.XCloseDisplay.argtypes = [ctypes.c_void_p]
|
|
x11.XSetErrorHandler(X_ERRORS)
|
|
display = x11.XOpenDisplay(None)
|
|
if not display:
|
|
return False
|
|
try:
|
|
root = x11.XDefaultRootWindow(display)
|
|
atom = lambda name: x11.XInternAtom(display, name, 0)
|
|
kind, width, count, after = ctypes.c_ulong(), ctypes.c_int(), ctypes.c_ulong(), ctypes.c_ulong()
|
|
data = ctypes.c_void_p()
|
|
if x11.XGetWindowProperty(display, root, atom(b"_NET_ACTIVE_WINDOW"), 0, 1, 0, 33, ctypes.byref(kind),
|
|
ctypes.byref(width), ctypes.byref(count), ctypes.byref(after),
|
|
ctypes.byref(data)) != 0 or not data.value:
|
|
return False
|
|
window = ctypes.cast(data, ctypes.POINTER(ctypes.c_ulong))[0] if count.value else 0
|
|
x11.XFree(data)
|
|
if not window:
|
|
return False
|
|
if action == "maximize":
|
|
kind_name = b"_NET_WM_STATE"
|
|
values = [2, atom(b"_NET_WM_STATE_MAXIMIZED_VERT"), atom(b"_NET_WM_STATE_MAXIMIZED_HORZ"), 1, 0]
|
|
else:
|
|
ignored = ctypes.c_ulong()
|
|
x, y, inner, mask = ctypes.c_int(), ctypes.c_int(), ctypes.c_int(), ctypes.c_uint()
|
|
if not x11.XQueryPointer(display, root, ctypes.byref(ignored), ctypes.byref(ignored), ctypes.byref(x),
|
|
ctypes.byref(y), ctypes.byref(inner), ctypes.byref(inner), ctypes.byref(mask)):
|
|
return False
|
|
if not mask.value & 0x100:
|
|
return False
|
|
kind_name = b"_NET_WM_MOVERESIZE"
|
|
values = [x.value, y.value, WINDOW_MOVES[action], 1, 1]
|
|
message = XMessage(type=33, window=window, message_type=atom(kind_name), format=32)
|
|
message.data[:] = values
|
|
sent = x11.XSendEvent(display, root, 0, (1 << 19) | (1 << 20), ctypes.byref(message))
|
|
x11.XFlush(display)
|
|
return bool(sent)
|
|
finally:
|
|
x11.XCloseDisplay(display)
|
|
|
|
|
|
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
|
|
self.controls_state = None
|
|
self.controls_installed = False
|
|
self.frame_limit_state = None
|
|
self.frame_limit_retry = 0
|
|
self.windows = {}
|
|
self.windows_retry = 0
|
|
|
|
def sync_controls(self):
|
|
try:
|
|
with open(os.path.join(CONTROLS_DIR, "state.json")) as source:
|
|
state = json.load(source)
|
|
value = state.get("brightness")
|
|
if isinstance(value, bool) or not isinstance(value, (int, float)) or not math.isfinite(value) or not 0 <= value <= 1:
|
|
return
|
|
if not self.controls_installed:
|
|
self.page.call("Runtime.addBinding", {"name": BRIGHTNESS_BINDING})
|
|
self.controls_installed = self.page.evaluate(BRIGHTNESS_JS) in ("installed", "present")
|
|
if not self.controls_installed:
|
|
return
|
|
if state != self.controls_state:
|
|
applied = self.page.evaluate("window.__droiddeckBrightness ? window.__droiddeckBrightness.set(%s) : false" % json.dumps(value))
|
|
if applied is True:
|
|
self.controls_state = state
|
|
elif applied != "held":
|
|
self.controls_installed = False
|
|
self.controls_state = None
|
|
except (OSError, ValueError, TypeError):
|
|
pass
|
|
|
|
def sync_windows(self):
|
|
if os.environ.get("XDG_CURRENT_DESKTOP") != "KDE":
|
|
return
|
|
if time.monotonic() >= self.windows_retry:
|
|
self.windows_retry = time.monotonic() + 2
|
|
try:
|
|
targets = http_get(self.port, "/json/list")
|
|
except (OSError, ValueError):
|
|
targets = []
|
|
seen = set()
|
|
for target in targets:
|
|
url = target.get("webSocketDebuggerUrl")
|
|
if WINDOW_TARGET not in target.get("url", "").split("&")[0] or not url:
|
|
continue
|
|
seen.add(target.get("id"))
|
|
try:
|
|
page = self.windows.get(target.get("id"))
|
|
if page is None:
|
|
page = Page(self.port, "/" + url.split("/", 3)[3])
|
|
page.call("Runtime.addBinding", {"name": WINDOW_BINDING}, timeout=5)
|
|
self.windows[target.get("id")] = page
|
|
page.evaluate(WINDOW_JS, timeout=5)
|
|
except (Closed, OSError, RuntimeError, ValueError) as e:
|
|
say("window controls: %s" % e)
|
|
self.windows.pop(target.get("id"), None)
|
|
for ident in set(self.windows) - seen:
|
|
del self.windows[ident]
|
|
for ident, page in list(self.windows.items()):
|
|
try:
|
|
for event in page.drain():
|
|
if called(event, WINDOW_BINDING):
|
|
window_action(event.get("params", {}).get("payload"))
|
|
except (Closed, OSError, ValueError):
|
|
del self.windows[ident]
|
|
|
|
def sync_frame_limit(self):
|
|
if self.frame_limit_state in ("installed", "present") or time.monotonic() < self.frame_limit_retry:
|
|
return
|
|
try:
|
|
state = self.page.evaluate(FRAME_LIMIT_JS)
|
|
except RuntimeError as e:
|
|
state = "failed (%s)" % e
|
|
if state != self.frame_limit_state:
|
|
say("frame limit sync: %s" % state)
|
|
self.frame_limit_state = state
|
|
self.frame_limit_retry = time.monotonic() + 10
|
|
|
|
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"]
|
|
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)
|
|
page.evaluate(LIBRARY_ART_JS)
|
|
self.page = page
|
|
self.controls_state = None
|
|
self.controls_installed = False
|
|
self.sync_controls()
|
|
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)
|
|
page.evaluate(LIBRARY_ART_JS)
|
|
try:
|
|
page.evaluate(TOUR_JS)
|
|
except RuntimeError:
|
|
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
|
|
self.controls_state = None
|
|
self.controls_installed = False
|
|
self.frame_limit_state = None
|
|
self.frame_limit_retry = 0
|
|
self.sync_controls()
|
|
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
|
|
listed = [t["name"] for t in live]
|
|
default, state = COMPAT["choose_default"](COMPAT["built_from"](top["selected"], catalog, listed) or top["selected"],
|
|
state, request, catalog, apply_request=not waiting, listed=listed)
|
|
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 = {}
|
|
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:
|
|
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)
|
|
moved = COMPAT["rebuilt_picks"](current, catalog, set(prior_auto) | picked, listed)
|
|
if moved:
|
|
following = COMPAT["followers"](state, current)
|
|
state["auto"] = {app: name for app, name in following.items() if app not in moved} if isinstance(following, dict) else {}
|
|
prior_auto = {app: name for app, name in prior_auto.items() if app not in moved}
|
|
current.update(moved)
|
|
requests = COMPAT["settled_game_requests"](state, current, catalog)
|
|
overrides = COMPAT["requested_games"](requests or {}, catalog, listed)
|
|
overrides = {app: tool for app, tool in overrides.items() if app not in refused}
|
|
changes, auto = COMPAT["plan_mapping"](
|
|
current, installed, listed, default,
|
|
COMPAT["followers"](state, current), native, picks, overrides,
|
|
prior_auto.keys(), native_choices)
|
|
changes = dict(moved, **changes)
|
|
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, written=default,
|
|
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):
|
|
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):
|
|
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, BRIGHTNESS_BINDING):
|
|
brightness_request(e.get("params", {}).get("payload"))
|
|
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:
|
|
self.sync_controls()
|
|
self.sync_windows()
|
|
sources = [self.page.ws] + ([self.chooser[1].stdout] if self.chooser is not None else [])
|
|
sources += [page.ws for page in self.windows.values()]
|
|
queued = self.page.ws.pending() or bool(self.page.events)
|
|
ready = [self.page.ws] if queued else select.select(sources, [], [], 0.25)[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:
|
|
self.drain()
|
|
|
|
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:
|
|
self.sync_controls()
|
|
self.sync_frame_limit()
|
|
self.sync_windows()
|
|
sources = [self.page.ws] + ([watcher] if watcher.fileno() >= 0 else [])
|
|
sources += [page.ws for page in self.windows.values()]
|
|
if self.chooser is not None:
|
|
sources.append(self.chooser[1].stdout)
|
|
queued = self.page.ws.pending() or bool(self.page.events)
|
|
ready = [self.page.ws] if queued else select.select(sources, [], [], 0.25)[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()
|