mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-22 06:15:58 +00:00
fix(steam): Browse buttons open a file chooser on the ARM64 client
Steam's ARM64 client has no desktop-portal file chooser, so SteamClient.System.OpenFileDialog failed at once and every Browse button (Add a Non-Steam Game, shortcut target, library folder) did nothing. The Steam UI bridge now answers it with a GTK file chooser run under Steam's reaper, so gamescope shows it over Steam.
This commit is contained in:
1 parent
77e23cb397
commit
5a56d9f785
3 files changed
+306
No files matched your search
@@ -196,6 +196,43 @@ TOUR_JS = r"""
|
||||
return "off";
|
||||
})()
|
||||
"""
|
||||
FILE_BINDING = "droiddeckOpenFileDialog"
|
||||
FILE_JS = r"""
|
||||
(() => {
|
||||
if (window.__droiddeckFiles) return "present";
|
||||
const self = { next: 0, pending: new Map() };
|
||||
const cancelled = () => ({ result: 2, message: "" });
|
||||
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;
|
||||
};
|
||||
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"""
|
||||
@@ -274,6 +311,62 @@ def launch_steamvr():
|
||||
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
|
||||
|
||||
@@ -530,6 +623,59 @@ class Helper:
|
||||
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"]
|
||||
reaper = os.path.join(self.steam_root, "steamrtarm64", "reaper")
|
||||
if os.access(reaper, os.X_OK):
|
||||
command = [reaper, "SteamLaunch", "AppId=769", "--"] + command
|
||||
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
|
||||
path = proc.stdout.read().decode("utf-8", "replace").strip()
|
||||
proc.stdout.close()
|
||||
proc.wait()
|
||||
self.finish_chooser(ident, path if proc.returncode == 0 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)
|
||||
@@ -542,6 +688,8 @@ class Helper:
|
||||
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:
|
||||
@@ -680,6 +828,9 @@ class Helper:
|
||||
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(self, watcher):
|
||||
@@ -690,7 +841,11 @@ class Helper:
|
||||
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())
|
||||
@@ -718,6 +873,18 @@ def detach():
|
||||
COMPAT["write_json"](state_file, state)
|
||||
|
||||
|
||||
def chooser_main():
|
||||
try:
|
||||
options = json.load(sys.stdin)
|
||||
except ValueError:
|
||||
return 1
|
||||
path = choose_file(options if isinstance(options, dict) else {})
|
||||
if not path:
|
||||
return 1
|
||||
print(path)
|
||||
return 0
|
||||
|
||||
|
||||
def main():
|
||||
steam_root = sys.argv[1]
|
||||
try:
|
||||
@@ -739,6 +906,7 @@ def main():
|
||||
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()
|
||||
@@ -752,6 +920,8 @@ def main():
|
||||
|
||||
|
||||
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())
|
||||
|
||||
Reference in new issue
Block a user