Desktop: run programs, scripts and AppImages; Switch to Desktop without Deck mode (#549)

* Run programs, scripts and AppImages from the desktop

Windows programs open with the Proton Steam has chosen, and a Steam game's own
launch program is handed to Steam. x86-64 Linux programs and game scripts run
under FEX, and an exec the kernel refuses goes to droiddeck-open through
libblsession's binfmt.c. Programs on noexec shared storage pass KIO's checks; a
folder of Linux programs there, and sharun AppImages, are added by the app once
and run from the copy. Executable scripts run without KIO's prompt, mpv plays
on the software desktop, FEXServer's socket moves under XDG_RUNTIME_DIR, and
Proton runtimes no longer show up as games.

* Switch to Desktop from Big Picture without Deck mode

Big Picture's Switch to Desktop asks the client's SteamOS manager, which the
client only has when started as SteamOS (Deck mode). Without it the request
failed and the screen stayed on "Switching to Desktop..." for good. The live
compat helper now sees the request on its way to the client and hands it to
steamos-session-select, which opens the KDE desktop as Deck mode already did.

* Run x86 programs from shared storage where they lie

FEX asks for the guest's PROT_EXEC when it maps a program or library, which
the kernel refuses on noexec shared storage; FEX never runs those pages
itself, so libblsession maps them without it in FEX's processes. An x86
game, its libraries and its start script now run in place instead of being
copied in; only native code on noexec storage is still copied.

A Steam library's games are never copied, and a Linux game's launch program
opened from the desktop goes to Steam like a Windows one.
This commit is contained in:
MaxsTechReview authored and GitHub committed 2026-10-10 19:12:20 -04:00
1 parent eadebc46d5
commit d9cd0d2cc8
25 files changed
+2135 -28

No files matched your search

@@ -12,12 +12,14 @@ import sys
HOME = os.environ.get("HOME", "/root")
STEAM_ROOT = sys.argv[1] if len(sys.argv) > 1 else os.path.join(HOME, ".local/share/Steam")
LIBRARIES = ((STEAM_ROOT, "Steam Games"), ("/mnt/droiddeck-sd", "SD Games"))
# The client's own tools and runtimes live in steamapps beside the games; they are not titles.
NOT_GAMES = {228980, 1493710, 3127680, 4183110, 4427310, 4185400}
# The client's own tools and runtimes live in steamapps beside the games; they are not titles. A
# depot with a toolmanifest.vdf is one too (a runtime or a Proton), listed here or not.
NOT_GAMES = {228980, 1070560, 1391110, 1493710, 1628350, 3127680, 4183110, 4185400, 4427310, 4628740}
APPS_DIR = os.path.join(HOME, ".local/share/applications")
PREFIX = "droiddeck-game-"
NAME = re.compile(r'^\s*"name"\s*"([^"]*)"', re.M)
STATE = re.compile(r'^\s*"StateFlags"\s*"(\d+)"', re.M)
INSTALLDIR = re.compile(r'^\s*"installdir"\s*"([^"]*)"', re.M)
def games(library):
@@ -42,6 +44,9 @@ def games(library):
# StateFlags 4 = fully installed; anything else is downloading, updating or broken.
if not name or not state or (int(state.group(1)) & 4) == 0:
continue
installdir = INSTALLDIR.search(text)
if installdir and os.path.isfile(os.path.join(steamapps, "common", installdir.group(1), "toolmanifest.vdf")):
continue
out.append((appid, name.group(1)))
return out
@@ -406,12 +406,22 @@ def split_paths(value):
return [part for part in re.split(r"[:\s]+", value or "") if part]
def data_dir(env):
# FEXServer's socket lives here. Under ~/.local its host path passes sun_path's 108 bytes, and
# proot then binds it to a temporary name only the server sees: no client ever reaches it.
runtime = env.get("XDG_RUNTIME_DIR")
if runtime and os.path.isdir(runtime) and os.access(runtime, os.W_OK):
return os.path.join(runtime, "fex")
return DATA_DIR
def guest_environment(fex, env):
result = dict(env)
for directory in (CONFIG_DIR, DATA_DIR, CACHE_DIR):
data = data_dir(env)
for directory in (CONFIG_DIR, data, CACHE_DIR):
os.makedirs(directory, exist_ok=True)
result["FEX_APP_CONFIG_LOCATION"] = CONFIG_DIR + "/"
result["FEX_APP_DATA_LOCATION"] = DATA_DIR + "/"
result["FEX_APP_DATA_LOCATION"] = data + "/"
result["FEX_APP_CACHE_LOCATION"] = CACHE_DIR + "/"
result.setdefault("FEX_SILENTLOG", "0")
result.setdefault("FEX_OUTPUTLOG", "stderr")
@@ -0,0 +1,434 @@
#!/usr/bin/python3
"""Runs a program from the desktop: a Windows program through the Proton Steam has chosen, a Linux
program (x86 ones under FEX), an AppImage, or a script (under FEX beside x86 programs, as a game's
start script is). Shared storage is noexec: FEX runs x86 programs there where they lie (libblsession's
noexec.c), while a native one is copied - alone into the cache, or with its folder by the app once (as
Add an app does) when that holds native programs or libraries. A Steam library's games are Steam's.
droiddeck-open [--window | --terminal] FILE [ARGS...] the file manager's Run entries
droiddeck-open --exec FILE [ARGS...] an exec the kernel refused (libblsession)
droiddeck-open --install the Run entries and default handlers
The desktop composites in software, so a program started from it goes full screen on the GPU
(droiddeck-gpu), as the menu's games do: --window keeps it in a desktop window instead, --terminal
runs it in Konsole. A plain script stays on the desktop, and an exec whose output is read (a terminal,
a pipe, a file) stays where it was started.
"""
import fcntl
import hashlib
import os
import runpy
import shutil
import stat
import subprocess
import sys
import time
HOME = os.environ.get("HOME") or "/root"
BIN = "/usr/local/bin"
FEX = os.path.join(BIN, "droiddeck-fex")
GPU = os.path.join(BIN, "droiddeck-gpu")
PROTON_RUN = os.path.join(BIN, "droiddeck-proton-run")
APPIMAGE_RUN = os.path.join(BIN, "droiddeck-appimage-run")
SCRIPT_RUN = os.path.join(BIN, "droiddeck-script-run")
STEAM_LAUNCH = os.path.join(BIN, "droiddeck-steam-launch")
CACHE = os.path.join(HOME, ".cache", "droiddeck")
# Where the app adds each kind, and what an added one holds.
IMPORTS = {"appimage": ("/opt/appimages/user", "app"), "folder": ("/opt/droiddeck-apps", "entry")}
# Android's own folders on shared storage: never copied in whole.
SHARED_FOLDERS = ("alarms", "android", "audiobooks", "dcim", "documents", "download", "movies", "music",
"notifications", "pictures", "podcasts", "recordings", "ringtones")
SCAN_LIMIT = 4000
APPS_DIR = "/usr/local/share/applications"
WINDOWS = (".exe", ".bat", ".cmd", ".com", ".msi")
NOT_PROGRAMS = (".dll", ".sys", ".drv", ".cpl", ".ocx", ".efi", ".scr", ".mui")
SCRIPTS = (".sh", ".bash")
MACHINES = {3: "i386", 62: "x86_64", 183: "arm64"}
EMULATED = (3, 62)
QUICK_FAILURE = 20
IMPORT_WAIT = 1800
WINDOWS_TYPES = ("application/x-ms-dos-executable", "application/x-msdownload",
"application/vnd.microsoft.portable-executable", "application/x-msi", "application/x-bat")
SCRIPT_TYPES = ("application/x-shellscript", "text/x-shellscript")
LINUX_TYPES = ("application/x-executable", "application/x-pie-executable", "application/vnd.appimage")
ENTRIES = (
("droiddeck-open.desktop", "Run", "", WINDOWS_TYPES + SCRIPT_TYPES + LINUX_TYPES),
("droiddeck-open-window.desktop", "Run in a Desktop Window", "--window ", WINDOWS_TYPES + SCRIPT_TYPES + LINUX_TYPES),
("droiddeck-open-terminal.desktop", "Run in Konsole", "--terminal ", SCRIPT_TYPES + LINUX_TYPES),
)
def say(message):
print("droiddeck-open: %s" % message, file=sys.stderr, flush=True)
def notify(title, body, icon="dialog-error"):
if not os.environ.get("DBUS_SESSION_BUS_ADDRESS") or not shutil.which("notify-send"):
return
try:
subprocess.run(["notify-send", "-a", "DroidDeck", "-i", icon, title, body],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=5)
except (OSError, subprocess.SubprocessError):
pass
def head(path, size=64):
try:
with open(path, "rb") as f:
return f.read(size)
except OSError:
return b""
def machine(data):
if len(data) < 20 or data[:4] != b"\x7fELF":
return None
return int.from_bytes(data[18:20], "big" if data[5] == 2 else "little")
def kind(path):
data = head(path)
ext = os.path.splitext(path)[1].lower()
if data[:4] == b"\x7fELF":
return "appimage" if data[8:11] == b"AI\x02" or ext == ".appimage" else "elf"
if ext in NOT_PROGRAMS:
return None
if ext in WINDOWS or data[:2] == b"MZ":
return "windows"
if data[:2] == b"#!" or ext in SCRIPTS:
return "script"
return None
def noexec(path):
try:
return bool(os.statvfs(path).f_flag & os.ST_NOEXEC)
except OSError:
return False
def interpreter(path):
line = head(path, 256).split(b"\n", 1)[0].rstrip(b"\r")
if line.startswith(b"#!"):
words = line[2:].decode("utf-8", "replace").split()
if words:
return words
return ["/bin/bash"]
def for_x86(script):
return subprocess.call([FEX, "needs", os.path.dirname(script)], stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL) == 0
def runnable(path):
"""The program itself, or a copy in the cache of a native one on noexec storage."""
if not noexec(path) or machine(head(path)) in EMULATED:
return path
st = os.stat(path)
key = hashlib.sha1(os.path.realpath(path).encode("utf-8")).hexdigest()[:16]
copy = os.path.join(CACHE, "exec", key, os.path.basename(path))
try:
same = os.stat(copy)
if same.st_size == st.st_size and same.st_mtime_ns == st.st_mtime_ns:
return copy
except OSError:
pass
os.makedirs(os.path.dirname(copy), exist_ok=True)
shutil.copyfile(path, copy + ".tmp")
os.utime(copy + ".tmp", ns=(st.st_atime_ns, st.st_mtime_ns))
os.chmod(copy + ".tmp", 0o755)
os.replace(copy + ".tmp", copy)
return copy
def appimage(path):
"""The image unpacked once into the cache, laid out as droiddeck-appimage-run expects."""
st = os.stat(path)
real = os.path.realpath(path)
key = hashlib.sha1(("%s\0%d\0%d" % (real, st.st_size, st.st_mtime_ns)).encode("utf-8")).hexdigest()[:16]
images = os.path.join(CACHE, "appimages")
return once(key, lambda: unpacked(path, real, key, images, os.path.join(images, key)))
def once(key, make):
"""[make]() for [key] one start at a time: a second start waits for the first, then finds it done."""
locks = os.path.join(CACHE, "locks")
os.makedirs(locks, exist_ok=True)
with open(os.path.join(locks, key), "w") as lock:
fcntl.flock(lock, fcntl.LOCK_EX)
return make()
def unpacked(path, real, key, images, folder):
if os.path.exists(os.path.join(folder, "app", "AppRun")):
return folder
earlier = import_of(key, "appimage")
if earlier:
return earlier
name = os.path.splitext(os.path.basename(path))[0]
notify("Preparing %s" % name, "Unpacking it for its first start.", "system-run")
work = folder + ".tmp"
shutil.rmtree(work, ignore_errors=True)
os.makedirs(work)
status = subprocess.call([FEX, "extract", real], cwd=work, stdout=subprocess.DEVNULL)
unpacked = os.path.realpath(os.path.join(work, "squashfs-root"))
if status != 0 or not os.path.isdir(unpacked):
shutil.rmtree(work, ignore_errors=True)
return imported("appimage", real, key, name)
os.replace(unpacked, os.path.join(work, "app"))
if os.path.islink(os.path.join(work, "squashfs-root")):
os.unlink(os.path.join(work, "squashfs-root"))
for file, value in (("name", name), ("arch", MACHINES.get(machine(head(path)), "")), ("source", real)):
with open(os.path.join(work, file), "w", encoding="utf-8") as f:
f.write(value + "\n")
for other in os.listdir(images):
old = os.path.join(images, other)
if other != key + ".tmp" and (head(os.path.join(old, "source"), 4096).decode("utf-8", "replace").strip() == real):
shutil.rmtree(old, ignore_errors=True)
os.replace(work, folder)
return folder
def import_of(key, what):
root, made = IMPORTS[what]
try:
entries = sorted(os.listdir(root))
except OSError:
return None
for entry in entries:
folder = os.path.join(root, entry)
if head(os.path.join(folder, "desktop-import"), 64).split(b"\n", 1)[0] == key.encode() \
and os.path.exists(os.path.join(folder, made)):
return folder
return None
def imported(what, real, key, name):
"""[real] added by the app instead, as Add an app adds it (to the menu too): an AppImage whose hard
links (sharun's) only the app's own proot can make, or a folder of native programs off noexec storage."""
launch = os.environ.get("BL_LAUNCH_DIR")
if not launch or not os.path.isdir(launch):
raise RuntimeError("%s cannot run from here; add it in DroidDeck with Add an app." % name)
result = os.path.join(launch, "desktop-import-%s.result" % key)
request = os.path.join(launch, "desktop-import-%s.request" % key)
with open(request + ".tmp", "w", encoding="utf-8") as f:
f.write("id=%s\nkind=%s\npath=%s\n" % (key, what, real))
os.replace(request + ".tmp", request)
if what == "appimage":
notify("Adding %s to DroidDeck" % name, "It is unpacked once, then it starts, and it stays in the menu.", "system-run")
else:
notify("Adding %s to DroidDeck" % name, "Shared storage cannot run its programs, so the folder is copied in "
"once; then it starts, and it stays in the menu.", "system-run")
deadline = time.monotonic() + IMPORT_WAIT
while time.monotonic() < deadline:
try:
with open(result, encoding="utf-8") as f:
answer = f.read().strip()
except OSError:
time.sleep(1)
continue
os.unlink(result)
verdict, _, detail = answer.partition(" ")
if verdict == "ok" and os.path.exists(os.path.join(detail, IMPORTS[what][1])):
return detail
raise RuntimeError(detail or "DroidDeck could not add %s." % name)
raise RuntimeError("DroidDeck did not finish adding %s." % name)
def game_folder(path):
"""The folder of [path] when it runs only from a copy: on noexec storage, holding native programs or
libraries (besides [path]) that it starts or loads. Not the storage itself, one of Android's folders
or a Steam library."""
real = os.path.realpath(path)
folder = os.path.dirname(real)
if in_steam_library(real) or not noexec(folder):
return None
top = storage_root(folder)
parts = os.path.relpath(folder, top).split(os.sep)
if folder == top or (len(parts) == 1 and parts[0].lower() in SHARED_FOLDERS):
return None
seen = 0
for root, dirs, files in os.walk(folder):
if root[len(folder):].count(os.sep) >= 3:
dirs[:] = []
for name in files:
child = os.path.join(root, name)
if child == real:
continue
seen += 1
if seen > SCAN_LIMIT:
return None
if machine(head(child)) not in (None,) + EMULATED:
return folder
return None
def in_steam_library(path):
"""Whether [path] is in a Steam library: its games are Steam's to start, never copied in."""
return "steamapps" in os.path.realpath(path).lower().split(os.sep)
def storage_root(folder):
"""The top of the storage [folder] is on: the last folder above it on the same file system."""
top = folder
try:
dev = os.stat(folder).st_dev
while os.path.dirname(top) != top and os.stat(os.path.dirname(top)).st_dev == dev:
top = os.path.dirname(top)
except OSError:
pass
return top
def copied_in(path):
"""The app's copy of [path]'s folder, added (once) for it."""
real = os.path.realpath(path)
key = hashlib.sha1(("folder\0%s" % real).encode("utf-8")).hexdigest()[:16]
name = os.path.basename(os.path.dirname(real))
return once(key, lambda: import_of(key, "folder") or imported("folder", real, key, name))
def steam_title(path):
"""The Steam game [path] is the program of: Steam starts it, with the client most games need."""
try:
run = runpy.run_path(PROTON_RUN)
root = os.environ.get("STEAM_COMPAT_CLIENT_INSTALL_PATH") or run["STEAM_ROOT"]
target = run["locate"](path, root)
return target.appid if run["steam_launches"](target, path, root) else None
except (OSError, ValueError, KeyError, IndexError):
return None
def command(path, args, what, window=False, appid=None):
"""(command, working directory) that runs [path] in place."""
folder = os.path.dirname(path)
if what == "steam":
return [STEAM_LAUNCH, "desktop", appid], folder
if what == "windows":
return [PROTON_RUN] + (["--window"] if window else []) + [path] + args, folder
if what == "x86 script":
return [FEX, "run", "--mode", "on", "--for", folder, "--"] + interpreter(path) + [path] + args, folder
if what == "script":
return interpreter(path) + [path] + args, folder
if what == "appimage":
return [APPIMAGE_RUN, appimage(path)] + args, folder
if what == "folder":
return [SCRIPT_RUN, copied_in(path)] + args, folder
return [FEX, "run", "--mode", "auto", "--", runnable(path)] + args, folder
def on_desktop(env):
return env.get("BL_DESKTOP") == "1" and not env.get("GAMESCOPE_WAYLAND_DISPLAY") and os.access(GPU, os.X_OK)
def unattended(env):
"""Nobody reads the output: it goes to /dev/null or the desktop's log, as from Dolphin or the menu."""
try:
out = os.fstat(1)
except OSError:
return True
if stat.S_ISCHR(out.st_mode):
return not os.isatty(1)
folder = env.get("BL_DEBUG_DIR")
try:
log = os.stat(os.path.join(folder, "desktop.log")) if folder else None
except OSError:
log = None
return log is not None and (log.st_dev, log.st_ino) == (out.st_dev, out.st_ino)
def placed(cmd, what, mode, env):
"""[cmd] where it shows: full screen on the GPU from the desktop, else where it was started."""
if mode == "terminal" and what not in ("windows", "steam") and shutil.which("konsole"):
return ["konsole", "--hold", "-e"] + cmd
if mode == "window" or what in ("script", "steam") or not on_desktop(env):
return cmd
if mode == "exec" and what != "windows" and not unattended(env):
return cmd
return [GPU] + cmd
def install(directory=APPS_DIR):
os.makedirs(directory, exist_ok=True)
defaults = []
for file, name, option, types in ENTRIES:
body = ("[Desktop Entry]\nType=Application\nName=%s\nIcon=system-run\nExec=%s %s%%f\nTerminal=false\n"
"NoDisplay=true\nMimeType=%s;\n" % (name, os.path.join(BIN, "droiddeck-open"), option, ";".join(types)))
write(os.path.join(directory, file), body)
for mime in WINDOWS_TYPES + SCRIPT_TYPES + LINUX_TYPES:
defaults.append("%s=%s" % (mime, ENTRIES[0][0]))
added = ["%s=%s" % (mime, ";".join(f for f, _, _, types in ENTRIES if mime in types) + ";")
for mime in WINDOWS_TYPES + SCRIPT_TYPES + LINUX_TYPES]
write(os.path.join(directory, "mimeapps.list"),
"[Default Applications]\n%s\n\n[Added Associations]\n%s\n" % ("\n".join(defaults), "\n".join(added)))
return 0
def write(path, text):
try:
with open(path, encoding="utf-8") as f:
if f.read() == text:
return
except OSError:
pass
with open(path + ".tmp", "w", encoding="utf-8") as f:
f.write(text)
os.replace(path + ".tmp", path)
def main(argv):
mode = "run"
if argv and argv[0] == "--install":
return install()
if argv and argv[0] in ("--window", "--terminal", "--exec"):
mode = argv.pop(0)[2:]
if argv and argv[0] == "--":
argv.pop(0)
if not argv:
say("usage: droiddeck-open [--window | --terminal | --exec] FILE [ARGS...] | --install")
return 2
path = os.path.abspath(argv[0])
what = kind(path)
if what is None:
say("%s is not a program this desktop can run" % path)
if mode != "exec":
notify("Cannot run %s" % os.path.basename(path), "It is not a Windows or Linux program or a script.")
return 126
linux_game = what in ("script", "elf") and mode == "run" and in_steam_library(path)
appid = steam_title(path) if (what == "windows" and mode != "window") or linux_game else None
if appid:
what = "steam"
elif what == "script" and for_x86(path):
what = "x86 script"
elif what in ("script", "elf") and game_folder(path):
what = "folder"
try:
cmd, folder = command(path, argv[1:], what, mode == "window", appid)
except (OSError, RuntimeError) as error:
say(str(error))
notify("Cannot run %s" % os.path.basename(path), str(error))
return 126
cmd = placed(cmd, what, mode, os.environ)
if mode != "exec" or not sys.stderr.isatty():
say("%s (%s): %s" % (path, what, " ".join(cmd[:4])))
try:
os.chdir(folder)
except OSError:
pass
if mode == "exec":
os.execv(cmd[0], cmd)
started = time.monotonic()
try:
status = subprocess.call(cmd)
except OSError as error:
status = 127
say("%s: %s" % (cmd[0], error))
if status != 0 and time.monotonic() - started < QUICK_FAILURE:
notify("%s did not start" % os.path.basename(path),
"It exited with status %d; the session's desktop.log says why." % status)
return status
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))
@@ -0,0 +1,331 @@
#!/usr/bin/python3
"""Runs a Windows program with the Proton the Steam client has chosen for it.
droiddeck-proton-run [--window] PROGRAM [ARGS...]
The program belongs to an installed Steam game when it sits in one's folder, to a prefix when it
sits in one's drive_c, or to a non-Steam shortcut that names it; any other program gets a prefix of
its own, under the id the client gives a new shortcut. That title's CompatToolMapping entry in
config.vdf picks the tool, else the client's default for all other titles ("0"). The tool is
started as the client starts it - its toolmanifest's command line, the waitforexitandrun verb and
the STEAM_COMPAT_* environment - so its wrapper applies the game's settings, components and audio.
--window keeps the program in the desktop's own window, drawn with WineD3D.
"""
import json
import os
import re
import runpy
import struct
import sys
import zlib
HOME = os.environ.get("HOME") or "/root"
STEAM_ROOT = os.path.join(HOME, ".local", "share", "Steam")
BIN_DIR = os.path.dirname(os.path.abspath(__file__))
STATE = os.path.join(HOME, ".local", "share", "droiddeck-compat", "state.json")
EXTRA_LIBRARIES = ("/mnt/droiddeck", "/mnt/droiddeck-sd")
SYSTEM_TOOLS = ("/usr/share/steam/compatibilitytools.d", "/usr/local/share/steam/compatibilitytools.d")
VERB = "waitforexitandrun"
SHORTCUT_BIT = 0x80000000
STEAM_APP_LIMIT = 2 ** 31
_compat = None
def say(message):
print("droiddeck-proton-run: %s" % message, file=sys.stderr, flush=True)
def compat():
global _compat
if _compat is None:
_compat = runpy.run_path(os.path.join(BIN_DIR, "steam-compatibility"))
return _compat
def read_text(path):
try:
with open(path, encoding="utf-8", errors="replace") as f:
return f.read()
except OSError:
return None
def libraries(root):
found, seen = [], set()
for library in compat()["libraries"](root) + list(EXTRA_LIBRARIES):
try:
key = os.stat(os.path.join(library, "steamapps"))
except OSError:
continue
if (key.st_dev, key.st_ino) not in seen:
seen.add((key.st_dev, key.st_ino))
found.append(library)
return found
def mapping(root):
text = read_text(os.path.join(root, "config", "config.vdf"))
if not text:
return {}
c = compat()
tokens = c["tokenize"](text)
steam = c["find_block"](tokens, ["InstallConfigStore", "Software", "Valve", "Steam"])
if steam is None:
return {}
block = c["find_block"](tokens[steam + 1:], ["CompatToolMapping"])
return {} if block is None else c["read_mapping"](tokens, block + steam + 1)
def tools(root):
"""Windows compatibility tools this runtime can start, by the name config.vdf uses."""
found = {}
for base in [os.path.join(root, "compatibilitytools.d")] + list(SYSTEM_TOOLS):
try:
entries = sorted(os.listdir(base))
except OSError:
continue
for entry in entries:
folder = os.path.join(base, entry)
vdf = os.path.join(folder, "compatibilitytool.vdf")
if not re.search(r'"from_oslist"\s+"[^"]*windows', read_text(vdf) or "", re.I):
continue
command = compat()["manifest_entry_point"](os.path.join(folder, "toolmanifest.vdf"))
if not command or not os.access(os.path.join(folder, command), os.X_OK):
continue
name = compat()["first_key"](vdf, ["compatibilitytools", "compat_tools"]) or entry
found.setdefault(name, (folder, command))
return found
def state_default():
try:
with open(STATE, encoding="utf-8") as f:
value = json.load(f).get("default")
return value if isinstance(value, str) else None
except (OSError, ValueError, AttributeError):
return None
def choose_tool(appid, current, available):
"""(tool name, why) from the client's mapping, else the session's own default."""
for key, why in ((appid, "Steam's choice for this title"), ("0", "Steam's default")):
name = current.get(key)
if name in available:
return name, why
c = compat()
for name in (state_default(), c["TOOL"], c["TOOL_11"]):
if name in available:
return name, "the default ARM64 Proton"
return (sorted(available)[0], "the only one installed") if available else (None, None)
class Target:
def __init__(self, appid, compatdata, install, library, kind, start=None):
self.appid = str(appid)
self.compatdata = compatdata
self.install = install
self.library = library
self.kind = kind
self.start = start or install
def ancestors(path):
"""(folder, the entry below it on the way to path) from the program's folder up to /."""
child, folder = path, os.path.dirname(path)
while True:
yield folder, os.path.basename(child)
if folder == os.path.dirname(folder):
return
child, folder = folder, os.path.dirname(folder)
def identity(path):
try:
st = os.stat(path)
return st.st_dev, st.st_ino
except OSError:
return None
def installed_app(steamapps, installdir):
for name in sorted(os.listdir(steamapps)):
m = re.fullmatch(r"appmanifest_(\d+)\.acf", name)
if not m:
continue
found = re.search(r'"installdir"\s+"([^"]+)"', read_text(os.path.join(steamapps, name)) or "")
if found and found.group(1).lower() == installdir.lower():
return m.group(1)
return None
def steam_game(program, libs):
commons = {}
for library in libs:
key = identity(os.path.join(library, "steamapps", "common"))
if key:
commons.setdefault(key, library)
for folder, below in ancestors(program):
library = commons.get(identity(folder))
if library is None:
continue
appid = installed_app(os.path.join(library, "steamapps"), below)
if appid is None or appid in compat()["NOT_GAMES"]:
return None
return Target(appid, os.path.join(library, "steamapps", "compatdata", appid),
os.path.join(folder, below), library, "steam")
return None
def prefix_of(program):
for folder, below in ancestors(program):
parent = os.path.dirname(folder)
if below == "pfx" and os.path.basename(parent) == "compatdata" and os.path.basename(folder).isdigit():
library = os.path.dirname(os.path.dirname(parent))
return Target(os.path.basename(folder), folder, os.path.dirname(program), library, "prefix")
return None
def unquoted(value):
return value.strip().strip('"') if isinstance(value, str) else ""
def same_file(a, b):
try:
return bool(b) and os.path.samefile(a, b)
except OSError:
return False
def shortcut_entries(root):
"""(appid, exe, start dir) of the client's non-Steam shortcuts and the app's added games."""
entries = []
try:
tool = runpy.run_path(os.path.join(BIN_DIR, "droiddeck-steam-shortcuts"))
account = tool["account_dir"](root)
if account:
with open(os.path.join(account, "config", "shortcuts.vdf"), "rb") as f:
shortcuts = tool["parse"](f.read()).get("shortcuts", {})
for value in shortcuts.values():
if not isinstance(value, dict):
continue
appid = value.get("appid", value.get("AppId"))
exe = value.get("Exe", value.get("exe"))
if isinstance(appid, int) and exe:
entries.append((appid & 0xFFFFFFFF, unquoted(exe), unquoted(value.get("StartDir", ""))))
except (OSError, ValueError, IndexError, KeyError, struct.error):
pass
try:
with open(os.environ.get("BL_ADDED_GAMES", ""), encoding="utf-8") as f:
for game in json.load(f):
if isinstance(game, dict) and game.get("exe") and str(game.get("appid", "")).isdigit():
entries.append((int(game["appid"]) & 0xFFFFFFFF, game["exe"], game.get("dir") or ""))
except (OSError, ValueError, TypeError):
pass
return entries
def shortcut(program, root):
for appid, exe, start in shortcut_entries(root):
if same_file(program, exe):
return Target(appid, os.path.join(root, "steamapps", "compatdata", str(appid)),
os.path.dirname(program), root, "shortcut", start if os.path.isdir(start) else None)
return None
def own_id(program):
"""The id the client gives a new shortcut: the CRC of its quoted target and its name."""
name = os.path.splitext(os.path.basename(program))[0]
return zlib.crc32(('"%s"%s' % (program, name)).encode("utf-8")) | SHORTCUT_BIT
def locate(program, root):
libs = libraries(root)
found = steam_game(program, libs) or prefix_of(program) or shortcut(program, root)
if found:
return found
appid = own_id(program)
return Target(appid, os.path.join(root, "steamapps", "compatdata", str(appid)),
os.path.dirname(program), root, "own")
def steam_launches(target, program, root):
"""Whether a launch option of the Steam game starts [program]: then Steam should start it."""
if target.kind != "steam":
return False
entry = compat()["appinfo_entries"](os.path.join(root, "appcache", "appinfo.vdf")).get(target.appid, {})
return os.path.relpath(program, target.install).replace(os.sep, "/").lower() in entry.get("launch", ())
def environment(env, target, tool_folder, root, window=False):
env = dict(env)
steamapps = [os.path.join(lib, "steamapps") for lib in libraries(root)]
env.update({
"STEAM_COMPAT_DATA_PATH": target.compatdata,
"STEAM_COMPAT_CLIENT_INSTALL_PATH": root,
"STEAM_COMPAT_INSTALL_PATH": target.install,
"STEAM_COMPAT_LIBRARY_PATHS": ":".join(steamapps),
"STEAM_COMPAT_TOOL_PATHS": tool_folder,
"STEAM_COMPAT_SHADER_PATH": os.path.join(target.library, "steamapps", "shadercache", target.appid),
})
number = int(target.appid)
if number < STEAM_APP_LIMIT:
env.update(SteamAppId=target.appid, SteamGameId=target.appid, SteamOverlayGameId=target.appid,
STEAM_COMPAT_APP_ID=target.appid)
else:
game = str(number << 32 | 0x02000000)
env.update(SteamAppId="0", STEAM_COMPAT_APP_ID="0", SteamGameId=game, SteamOverlayGameId=game)
if window:
env.setdefault("PROTON_USE_WINED3D", "1")
return env
def windows_path(path):
return "Z:" + path.replace("/", "\\")
def arguments(program, args):
if program.lower().endswith(".msi"):
return ["msiexec", "/i", windows_path(program)] + args
return [program] + args
def main(argv):
window = False
while argv and argv[0].startswith("--"):
option = argv.pop(0)
if option == "--window":
window = True
elif option == "--":
break
else:
say("unknown option %s" % option)
return 2
if not argv:
say("usage: droiddeck-proton-run [--window] PROGRAM [ARGS...]")
return 2
program = os.path.abspath(argv[0])
if not os.path.isfile(program):
say("%s does not exist" % program)
return 2
root = os.environ.get("STEAM_COMPAT_CLIENT_INSTALL_PATH") or STEAM_ROOT
target = locate(program, root)
available = tools(root)
name, why = choose_tool(target.appid, mapping(root), available)
if name is None:
say("no Proton is installed yet; open Steam once and it is downloaded for you")
return 69
folder, command = available[name]
os.makedirs(target.compatdata, exist_ok=True)
env = environment(os.environ, target, folder, root, window)
say("%s with %s (%s), %s prefix %s" % (program, name, why, target.kind, target.compatdata))
try:
os.chdir(target.start)
except OSError:
os.chdir(os.path.dirname(program))
entry = os.path.join(folder, command)
os.execve(entry, [entry, VERB] + arguments(program, argv[1:]), env)
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))
@@ -12,14 +12,15 @@ first=
IFS= read -r first < "$entry"
first=${first%$'\r'}
interp=(/bin/bash)
case $first in '#!'*) read -r -a interp <<< "${first#'#!'}" ;; esac
# A program as the entry (the desktop's copy of a game's folder) runs itself.
case $first in '#!'*) read -r -a interp <<< "${first#'#!'}" ;; $'\x7f'ELF*) interp=() ;; esac
fex=$(cat "$dir/fex" 2>/dev/null)
case $fex in on|off) ;; *) fex=auto ;; esac
run=("${interp[@]}" "./$entry")
if [ -x /usr/local/bin/droiddeck-fex ]; then
run=(/usr/local/bin/droiddeck-fex run --mode "$fex" --for "$folder" -- "${interp[@]}" "./$entry")
fi
echo "== script $entry in $folder (${interp[*]}, FEX $fex)"
echo "== script $entry in $folder (${interp[*]:-a program}, FEX $fex)"
case ${DBUS_SESSION_BUS_ADDRESS:-} in
unix:*) "${run[@]}" "$@" ;;
*) unset DBUS_SESSION_BUS_ADDRESS
@@ -8,6 +8,7 @@ import select
import signal
import socket
import struct
import subprocess
import sys
import time
@@ -125,6 +126,37 @@ INSTALL_JS = r"""
return "installed";
})()
""".replace("__BINDING__", BINDING)
# Big Picture's "Switch to Desktop" asks the client's SteamOS manager, which the client has only when it
# runs as SteamOS (-steamos3, Deck mode); otherwise the request fails and the screen stays on "Switching
# to Desktop..." for good. The request is caught on its way to the client and answered as SteamOS's
# own path does, with steamos-session-select.
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"
class Closed(Exception):
@@ -135,6 +167,18 @@ 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)
@@ -357,9 +401,10 @@ class Watcher:
class Helper:
def __init__(self, steam_root, port):
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
@@ -374,6 +419,9 @@ class Helper:
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})
if not self.steamos:
page.call("Runtime.addBinding", {"name": SWITCH_BINDING})
page.evaluate(SWITCH_JS)
page.compat("get(0)")
except Exception:
page.ws.close()
@@ -422,6 +470,13 @@ class Helper:
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()
return events
def serve(self, watcher):
with open(COMPAT["state_path"]("live.pid"), "w") as f:
f.write("%d\n" % os.getpid())
@@ -432,15 +487,14 @@ class Helper:
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(e.get("method") == "Runtime.bindingCalled" and e.get("params", {}).get("name") == BINDING
for e in self.page.drain())
due = any(called(e, BINDING) for e in self.drain())
if (watcher in ready or watcher.fileno() < 0) and watcher.changed():
due = self.installed != COMPAT["installed_apps"](self.steam_root) or self.request_waiting()
if due:
time.sleep(0.3)
if watcher.fileno() >= 0 and select.select([watcher], [], [], 0)[0]:
watcher.changed()
self.page.drain()
self.drain()
self.reconcile()
@@ -466,7 +520,9 @@ def main():
return 0
os.makedirs(COMPAT["COMPAT_DIR"], exist_ok=True)
watcher = Watcher([COMPAT["state_path"]("request.json")] + [os.path.join(lib, "steamapps", "libraryfolders.vdf") for lib in (steam_root, COMPAT["LIBRARY"] + "-sd")])
helper = Helper(steam_root, port)
# The client has its own SteamOS manager when started as SteamOS (droiddeck-session's Deck mode).
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:
@@ -754,6 +754,21 @@ APPINFO_CACHE = {}
STEAM_PLAY_MANIFESTS = 891390
def launch_programs(config):
"""The Windows and Linux programs an app's launch options start, relative to its folder (lowercase, "/")."""
launch = config.get("launch") if isinstance(config, dict) else None
found = []
for option in launch.values() if isinstance(launch, dict) else ():
if not isinstance(option, dict) or not option.get("executable"):
continue
settings = option.get("config") if isinstance(option.get("config"), dict) else {}
oslist = str(settings.get("oslist", "")).lower()
if oslist and "windows" not in oslist and "linux" not in oslist:
continue
found.append(os.path.normpath(str(option["executable"]).replace("\\", "/")).lstrip("/").lower())
return found
def appinfo_entries(path):
import struct
try:
@@ -844,6 +859,7 @@ def appinfo_entries(path):
"osarch": str(common.get("osarch", "")).lower(),
"type": str(common.get("type", "")).lower(),
"recommended": str(recommended),
"launch": launch_programs(info.get("config")),
}
if app == STEAM_PLAY_MANIFESTS:
extended = info.get("extended", {})