Merge upstream main and close diagnostic publication races

This commit is contained in:
wallentx committed 2026-10-25 18:15:44 -05:00
commit f4adbeb602
473 files changed
+36063 -3507

No files matched your search

@@ -0,0 +1,7 @@
#!/bin/sh
# Runs DroidDeck's bundled innoextract with its own boost libraries. The guest's
# env -i startup has no LD_LIBRARY_PATH, and the rootfs must not grow a second
# copy of boost for the installer fallback.
LD_LIBRARY_PATH=/usr/local/lib/droiddeck-inno
export LD_LIBRARY_PATH
exec /usr/local/lib/droiddeck-inno/innoextract "$@"
@@ -0,0 +1,23 @@
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,18 @@
Copyright (C) 2011-2020 Daniel Scharrer <daniel@constexpr.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the author(s) be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
@@ -259,24 +259,91 @@ def focus_snapshot():
STEAM_APP_CLASS = re.compile(r"steam_app_(\d+)$")
def tag_game_windows(snap):
if os.environ.get("BL_STEAM_CLIENT_ARCH") != "x86_64":
return False
def process_env(pid, proc=Path("/proc")):
try:
return dict(entry.split(b"=", 1) for entry in (proc / str(pid) / "environ").read_bytes().split(b"\0") if b"=" in entry)
except OSError:
return {}
def steam_app_id(env):
try:
app = int(env.get(b"SteamAppId", b"0"))
except ValueError:
return None
return app if 0 < app < 2**32 else None
def under_reaper(pid, proc=Path("/proc")):
for _ in range(64):
try:
if (proc / str(pid) / "comm").read_text().strip() == "reaper":
return True
stat = (proc / str(pid) / "stat").read_text()
pid = int(stat[stat.rindex(")") + 2:].split()[1])
except (OSError, ValueError, IndexError):
return False
if pid <= 1:
return False
return False
def sandboxed_apps(proc=Path("/proc")):
apps = {}
try:
pids = [entry for entry in os.listdir(proc) if entry.isdigit()]
except OSError:
return apps
for pid in pids:
env = process_env(pid, proc)
flatpak, app = env.get(b"FLATPAK_ID"), steam_app_id(env)
if flatpak and app:
name = flatpak.decode(errors="replace").lower()
for key in {name, name.rsplit(".", 1)[-1]}:
apps[key] = app if apps.get(key, app) == app else None
return apps
def window_steam_app(win, flatpaks, proc=Path("/proc")):
match = STEAM_APP_CLASS.match(win.get("class") or "")
if match:
return match.group(1) if os.environ.get("BL_STEAM_CLIENT_ARCH") == "x86_64" else None
pid = win.get("pid")
if pid:
if under_reaper(pid, proc):
return None
app = steam_app_id(process_env(pid, proc))
if app:
return str(app)
app = flatpaks().get((win.get("class") or "").lower())
return str(app) if app else None
def tag_game_windows(snap, proc=Path("/proc")):
tagged = False
found = []
def flatpaks():
if not found:
found.append(sandboxed_apps(proc))
return found[0]
for display, wins in snap.get("displays", {}).items():
if display == os.environ.get("DISPLAY"):
continue
for win in wins:
match = STEAM_APP_CLASS.match(win.get("class") or "")
if not match or win.get("appId") or win.get("map") != "viewable" or win.get("overrideRedirect"):
if win.get("appId") or win.get("map") != "viewable" or win.get("overrideRedirect") or win.get("inputOnly"):
continue
app = window_steam_app(win, flatpaks, proc)
if not app:
continue
try:
done = subprocess.run(["xprop", "-display", display, "-id", str(win["window"]), "-f", "STEAM_GAME", "32c",
"-set", "STEAM_GAME", match.group(1)],
"-set", "STEAM_GAME", app],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=5).returncode == 0
except (OSError, subprocess.TimeoutExpired):
done = False
log("game window %s on %s tagged STEAM_GAME=%s: %s" % (win["window"], display, match.group(1), "ok" if done else "failed"))
log("game window %s on %s tagged STEAM_GAME=%s: %s" % (win["window"], display, app, "ok" if done else "failed"))
tagged = tagged or done
return tagged
+965
View File
@@ -0,0 +1,965 @@
#!/usr/bin/python3
import fcntl
import http.client
import json
import math
import os
import re
import shutil
import signal
import subprocess
import sys
import threading
import time
import urllib.request
HOME = os.environ.get("HOME") or "/root"
BIN = "/usr/local/bin"
PROTON_RUN = os.path.join(BIN, "droiddeck-proton-run")
APP_ID = "2596113170"
INSTALLER_URL = "https://downloader.battle.net/download/getInstallerForGame?os=win&gameProgram=BATTLENET_APP&version=Live"
CLIENT_DIR = ("Program Files (x86)", "Battle.net")
LAUNCHER = "Battle.net Launcher.exe"
CLIENT = "Battle.net.exe"
CONFIG = ("users", "steamuser", "AppData", "Roaming", "Battle.net", "Battle.net.config")
DEFAULT_CONFIG = {"Client": {"HardwareAcceleration": "false", "Streaming": {"StreamingEnabled": "false"}}}
LAUNCH_BEHAVIOR = ("GameLaunchWindowBehavior", "2")
CLIENT_32_BIT = ("s1",)
CACHE = os.path.join(HOME, ".cache", "droiddeck", "battlenet")
MENU = "/usr/local/share/applications/droiddeck-battlenet.desktop"
WALLPAPER_DIR = ("users", "steamuser", "AppData", "Local", "DroidDeck")
DESKTOP_KEY = "[Control Panel\\\\Desktop]"
COLORS_KEY = "[Control Panel\\\\Colors]"
WALLPAPER_EDGE = (4, 6, 11)
WALLPAPER_MAX = (1280, 720)
FEX_REQUEST = "battlenet-fex.request"
FEX_RESULT = "battlenet-fex.result"
FEX_WAIT = 120
START_GRACE = 90
POLL = 2
STOP_WAIT = 5
CLOSE_GRACE = 15
HANDOFF_WAIT = 120
GAME_SETTLE = 20
AGENT_GONE = 30
AGENT = "agent.exe"
CODE = re.compile(r"[A-Za-z0-9_]{1,16}")
GAME_UID = re.compile(r"[a-z0-9_]{1,32}")
HANDOVER_WAIT = 1800
CLIENT_READY = 5
READY_SETTLE = 5
CLIENT_WAIT = 300
GAME_GRACE = 15
LAUNCH_WAIT = 15
LAUNCH_ASKS = 3
CLIENT_PATH = re.compile(r"[a-z]:\\(program files \(x86\)|programdata)\\battle\.net\\")
IDLE = {"agent.exe", "conhost.exe", "explorer.exe", "lsass.exe", "plugplay.exe", "rpcss.exe", "services.exe",
"start.exe", "steam.exe", "svchost.exe", "tabtip.exe", "winedevice.exe", "wineserver"}
NOT_GAME = IDLE | {"rundll32.exe", "wineboot.exe", "winemenubuilder.exe"}
MIN_INSTALLER = 1 << 20
SETUP = "Battle.net-Setup.exe"
REQUEST = "battlenet-request"
REQUEST_FILE = re.compile(re.escape(REQUEST) + r"\.\d+")
LIBRARY = None
WOW_UID = re.compile(r"--uid=wow[a-z0-9_]*", re.I)
GAMEPAD_SETTING = re.compile(r"(?im)^\s*SET\s+GamePadEnable\s")
GX_API_SETTING = re.compile(r"(?im)^[ \t]*SET[ \t]+gxApi[ \t].*$")
GX_API_MARK = ".droiddeck-d3d11"
STEAM_CLIENT = {"steam", "steamwebhelper"}
LOG = os.path.join(CACHE, "battlenet.log")
LOG_LIMIT = 512 << 10
def say(message):
line = "droiddeck-battlenet: %s" % message
print(line, file=sys.stderr, flush=True)
try:
os.makedirs(CACHE, exist_ok=True)
if os.path.exists(LOG) and os.path.getsize(LOG) > LOG_LIMIT:
os.replace(LOG, LOG + ".1")
with open(LOG, "a", encoding="utf-8") as f:
f.write("%s [%d] %s\n" % (time.strftime("%Y-%m-%d %H:%M:%S"), os.getpid(), message))
except OSError:
pass
def notify(title, body, icon="dialog-information"):
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 steam_root():
return os.environ.get("STEAM_COMPAT_CLIENT_INSTALL_PATH") or os.path.join(HOME, ".local", "share", "Steam")
def prefix(root):
return os.path.join(root, "steamapps", "compatdata", APP_ID)
def drive_c(root):
return os.path.join(prefix(root), "pfx", "drive_c")
def client_dir(root):
return os.path.join(drive_c(root), *CLIENT_DIR)
def launcher(root):
return os.path.join(client_dir(root), LAUNCHER)
def installed(root):
return os.path.isfile(launcher(root))
def install_path(library):
return "Z:" + library.rstrip("/")
def write_config(root, library=None):
path = os.path.join(drive_c(root), *CONFIG)
key, value = LAUNCH_BEHAVIOR
try:
with open(path, encoding="utf-8-sig") as f:
config = json.load(f)
except FileNotFoundError:
config = None
except (OSError, ValueError):
return None
first = config is None
if first:
config = json.loads(json.dumps(DEFAULT_CONFIG))
elif not (isinstance(config, dict) and isinstance(config.setdefault("Client", {}), dict)):
return None
client = config["Client"]
changes = []
if client.get(key) != value:
client[key] = value
changes.append("launch")
if client.get("HardwareAcceleration") != "false":
client["HardwareAcceleration"] = "false"
changes.append("acceleration")
if library and isinstance(client.setdefault("Install", {}), dict) \
and str(client["Install"].get("DefaultInstallPath", "")).rstrip("/\\") != install_path(library):
client["Install"]["DefaultInstallPath"] = install_path(library)
changes.append("library")
games = config.setdefault("Games", {})
for uid in CLIENT_32_BIT if isinstance(games, dict) else ():
game = games.setdefault(uid, {})
if isinstance(game, dict) and game.get("Run64BitSet") != "true":
game.update(Run64Bit="false", Run64BitSet="true")
changes.append("32-bit")
if not changes:
return None
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path + ".tmp", "w", encoding="utf-8") as f:
json.dump(config, f, indent=4)
os.replace(path + ".tmp", path)
return "first" if first else " ".join(changes)
def wallpaper_bmp(width, height):
scale = min(width / 1920.0, height / 1080.0)
ox, oy = (width - 1920 * scale) / 2, (height - 1080 * scale) / 2
stops = ((0.0, (20, 33, 58)), (0.5, (10, 17, 32)), (1.0, WALLPAPER_EDGE))
glow = (26, 159, 255)
logo_px = scale * 1.6
def background(u, v):
t = min(math.hypot(u - 960, v - 540) / 1150, 1.0)
(t0, c0), (t1, c1) = (stops[0], stops[1]) if t <= 0.5 else (stops[1], stops[2])
f = (t - t0) / (t1 - t0)
color = [c0[i] + (c1[i] - c0[i]) * f for i in range(3)]
d = math.hypot(u - 947.21, v - 540) / 420
if d < 1:
a = 0.22 * (1 - d)
color = [color[i] * (1 - a) + glow[i] * a for i in range(3)]
return color
def box(x, y, x0, y0, x1, y1):
dx, dy = max(x0 - x, x - x1), max(y0 - y, y - y1)
return math.hypot(max(dx, 0), max(dy, 0)) + min(max(dx, dy), 0)
def white(x, y):
half = max(math.hypot(x - 63.97, y - 111.86) - 111.86, 63.97 - x)
shape = min(box(x, y, -31.98, 0, 63.97, 223.72), half)
hole = min(math.hypot(x - 63.98, y - 111.86) - 71.81,
box(x, y, 56.055, -1, 71.885, 45), box(x, y, 56.055, 178.72, 71.885, 225))
return max(shape, -hole)
rows = []
pad = b"\0" * ((4 - width * 3 % 4) % 4)
for y in range(height - 1, -1, -1):
v = (y + 0.5 - oy) / scale
ly = (v - 361.02) / 1.6
row = bytearray()
for x in range(width):
u = (x + 0.5 - ox) / scale
color = background(u, v)
lx = (u - 844.84) / 1.6
if -40 < lx < 185 and -8 < ly < 232:
for sd, ink in ((white(lx, ly), (255, 255, 255)), (math.hypot(lx - 63.98, ly - 111.86) - 55.98, glow)):
cover = min(max(0.5 - sd * logo_px, 0.0), 1.0)
if cover:
color = [color[i] * (1 - cover) + ink[i] * cover for i in range(3)]
row += bytes((int(color[2] + 0.5), int(color[1] + 0.5), int(color[0] + 0.5)))
rows.append(bytes(row) + pad)
pixels = b"".join(rows)
header = b"BM" + (54 + len(pixels)).to_bytes(4, "little") + b"\0\0\0\0" + (54).to_bytes(4, "little")
info = (40).to_bytes(4, "little") + width.to_bytes(4, "little") + height.to_bytes(4, "little") + \
(1).to_bytes(2, "little") + (24).to_bytes(2, "little") + b"\0" * 4 + len(pixels).to_bytes(4, "little") + \
(2835).to_bytes(4, "little") * 2 + b"\0" * 8
return header + info + pixels
def reg_value(reg, key, name, value):
line = '"%s"="%s"' % (name, value.replace("\\", "\\\\").replace('"', '\\"'))
try:
with open(reg, encoding="utf-8", errors="replace") as f:
rows = f.read().splitlines()
except OSError:
return False
inside, last = False, None
for row in rows:
if row.startswith("["):
inside = row.startswith(key + " ") or row == key
elif inside and row.startswith('"%s"=' % name):
last = row
if last != line:
with open(reg, "a", encoding="utf-8") as out:
out.write("\n%s %d\n%s\n" % (key, int(time.time()), line))
return True
def desktop_size():
width, height = os.environ.get("BL_WIDTH", ""), os.environ.get("BL_HEIGHT", "")
if width.isdigit() and height.isdigit() and 0 < int(width) <= 7680 and 0 < int(height) <= 4320:
return int(width), int(height)
return 1280, 720
def wallpaper(root):
width, height = desktop_size()
shrink = min(1.0, WALLPAPER_MAX[0] / width, WALLPAPER_MAX[1] / height)
width, height = max(1, round(width * shrink)), max(1, round(height * shrink))
name = "wallpaper-%dx%d.bmp" % (width, height)
folder = os.path.join(drive_c(root), *WALLPAPER_DIR)
path = os.path.join(folder, name)
reg = os.path.join(prefix(root), "pfx", "user.reg")
if not os.path.isfile(reg):
return False
if not os.path.isfile(path):
os.makedirs(folder, exist_ok=True)
with open(path + ".tmp", "wb") as f:
f.write(wallpaper_bmp(width, height))
os.replace(path + ".tmp", path)
say("drew DroidDeck's wallpaper for Battle.net's %dx%d desktop" % (width, height))
windows = "C:\\" + "\\".join(WALLPAPER_DIR + (name,))
reg_value(reg, DESKTOP_KEY, "Wallpaper", windows)
reg_value(reg, DESKTOP_KEY, "TileWallpaper", "0")
reg_value(reg, DESKTOP_KEY, "WallpaperStyle", "2" if shrink < 1 else "0")
reg_value(reg, COLORS_KEY, "Background", "%d %d %d" % WALLPAPER_EDGE)
return True
def windows_path(root, path):
found = re.match(r"(?i)^([cz]):[\\/](.*)$", path.strip())
if not found:
return None
parts = [part for part in re.split(r"[\\/]+", found.group(2)) if part]
if not parts or ".." in parts:
return None
return os.path.join(drive_c(root) if found.group(1).lower() == "c" else "/", *parts)
def wow_folders(root):
try:
with open(os.path.join(prefix(root), "pfx", "system.reg"), encoding="utf-8", errors="replace") as f:
rows = f.read().splitlines()
except OSError:
return []
folders, location, wow = [], None, False
for row in rows + ["["]:
if row.startswith("["):
if wow and location:
folders.append(location)
location, wow = None, False
elif row.startswith('"InstallLocation"='):
location = windows_path(root, row.split("=", 1)[1].strip().strip('"').replace("\\\\", "\\"))
elif row.startswith('"UninstallString"=') and WOW_UID.search(row):
wow = True
return [folder for folder in dict.fromkeys(folders) if os.path.isdir(folder)]
def wow_settings(root):
changed = []
for folder in wow_folders(root):
try:
flavors = sorted(name for name in os.listdir(folder) if len(name) > 2 and name.startswith("_") and name.endswith("_"))
except OSError:
continue
for name in flavors:
if not os.path.isdir(os.path.join(folder, name)):
continue
wtf = os.path.join(folder, name, "WTF")
path = os.path.join(wtf, "Config.wtf")
mark = os.path.join(wtf, GX_API_MARK)
try:
with open(path, encoding="utf-8", errors="replace") as f:
text = f.read()
except FileNotFoundError:
text = ""
except OSError:
continue
updated, notes = text, []
if not GAMEPAD_SETTING.search(updated):
updated += ("\n" if updated and not updated.endswith("\n") else "") + 'SET GamePadEnable "1"\n'
notes.append("controller support on")
first = not os.path.exists(mark)
if first:
before = updated
updated, found = GX_API_SETTING.subn('SET gxApi "D3D11"', updated)
if not found:
updated += ("\n" if updated and not updated.endswith("\n") else "") + 'SET gxApi "D3D11"\n'
if updated != before:
notes.append("DirectX 11")
if updated == text and not first:
continue
try:
os.makedirs(wtf, exist_ok=True)
if updated != text:
with open(path + ".tmp", "w", encoding="utf-8") as f:
f.write(updated)
os.replace(path + ".tmp", path)
if first:
with open(mark, "w", encoding="utf-8"):
pass
except OSError as error:
say("World of Warcraft's settings not written in %s: %s" % (path, error))
continue
if notes:
changed.append(path)
say("World of Warcraft's settings in %s: %s" % (path, ", ".join(notes)))
return changed
def steam_running(proc="/proc"):
try:
pids = [p for p in os.listdir(proc) if p.isdigit()]
except OSError:
return False
for pid in pids:
try:
with open(os.path.join(proc, pid, "cmdline"), "rb") as f:
argv = [os.path.basename(a.decode("utf-8", "replace")) for a in f.read().split(b"\0")[:2] if a]
except OSError:
continue
if argv[:1] and argv[0] in ("FEX", "FEXInterpreter"):
argv = argv[1:]
if argv[:1] and argv[0] in STEAM_CLIENT:
return True
return False
def proton_env():
env = dict(os.environ)
if not steam_running():
env.setdefault("PROTON_SPOOF_STEAMINPUT_VIDPID", "1")
return env
def configure(root, library=None):
if library:
try:
os.makedirs(library, exist_ok=True)
except OSError as error:
say("%s not made: %s" % (library, error))
try:
change = write_config(root, library)
except OSError as error:
say("Battle.net's config not written: %s" % error)
return
try:
wallpaper(root)
except OSError as error:
say("Battle.net's desktop wallpaper not set: %s" % error)
wow_settings(root)
if change == "first":
say("wrote Battle.net's first config: hardware acceleration off, streaming off, closing itself as it launches a game, "
"StarCraft on its 32-bit client")
elif change:
if "launch" in change:
say("Battle.net's config: closing itself as it launches a game")
if "library" in change:
say("Battle.net's config: games install to %s" % library)
if "acceleration" in change:
say("Battle.net's config: browser hardware acceleration off")
if "32-bit" in change:
say("Battle.net's config: StarCraft starts its 32-bit client")
def write_menu(path=MENU):
body = ("[Desktop Entry]\nType=Application\nName=Battle.net\nComment=Blizzard's games and launcher\n"
"Exec=%s\nIcon=applications-games\nTerminal=false\nCategories=Network;\n"
% os.path.join(BIN, "droiddeck-battlenet"))
try:
with open(path, encoding="utf-8") as f:
if f.read() == body:
return
except OSError:
pass
try:
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path + ".tmp", "w", encoding="utf-8") as f:
f.write(body)
os.replace(path + ".tmp", path)
except OSError as error:
say("menu entry not written: %s" % error)
def ask_fex(launch_dir, wait=FEX_WAIT):
if not launch_dir or not os.path.isdir(launch_dir):
say("not inside a DroidDeck session; FEX stays as Components has it")
return None
result = os.path.join(launch_dir, FEX_RESULT)
request = os.path.join(launch_dir, FEX_REQUEST)
try:
os.unlink(result)
except OSError:
pass
with open(request + ".tmp", "w", encoding="utf-8") as f:
f.write("appid=%s\n" % APP_ID)
os.replace(request + ".tmp", request)
deadline = time.monotonic() + wait
while time.monotonic() < deadline:
try:
with open(result, encoding="utf-8") as f:
answer = f.read().strip()
except OSError:
time.sleep(0.5)
continue
os.unlink(result)
return answer
return None
def wine_processes(pfx, proc="/proc"):
want = os.path.realpath(pfx).rstrip("/")
found = []
try:
pids = [p for p in os.listdir(proc) if p.isdigit()]
except OSError:
return found
for pid in pids:
try:
with open(os.path.join(proc, pid, "environ"), "rb") as f:
env = f.read().split(b"\0")
with open(os.path.join(proc, pid, "cmdline"), "rb") as f:
argv = [a.decode("utf-8", "replace") for a in f.read().split(b"\0") if a]
except OSError:
continue
for item in env:
if item.startswith(b"WINEPREFIX="):
value = item[len(b"WINEPREFIX="):].decode("utf-8", "replace")
if os.path.realpath(value).rstrip("/") == want:
found.append((int(pid), argv))
break
return found
def program(argv):
return re.split(r"[\\/]", argv[0])[-1].lower() if argv else ""
def is_client(argv):
return program(argv) == CLIENT.lower()
def idle(processes):
return all(program(argv) in IDLE for _, argv in processes)
def running(pfx):
return any(is_client(argv) for _, argv in wine_processes(pfx))
def is_battlenet(argv):
return bool(argv) and bool(CLIENT_PATH.match(argv[0].replace("/", "\\").lower()))
def is_game(argv):
if not argv or program(argv) in NOT_GAME or not re.match(r"[A-Za-z]:[\\/]", argv[0]):
return False
return not is_battlenet(argv)
def launched(argv):
return is_game(argv) and "-uid" in (arg.lower() for arg in argv[1:])
def shares_sign_in(argv):
return "-sso" in (arg.lower() for arg in argv[1:])
def agent_up(processes):
return any(is_client(argv) for _, argv in processes) and any(program(argv) == AGENT for _, argv in processes)
class Tidy:
def __init__(self, settle=None, gone=None):
self.settle = GAME_SETTLE if settle is None else settle
self.gone = AGENT_GONE if gone is None else gone
self.game_since = None
self.closed_for_game = False
self.agent_seen = False
self.agent_lost = None
def __call__(self, processes, now):
own = [(pid, argv) for pid, argv in processes if is_battlenet(argv)]
games = [argv for _, argv in processes if launched(argv)]
if games:
self.game_since = self.game_since if self.game_since is not None else now
if own and not self.closed_for_game and now - self.game_since >= self.settle:
self.closed_for_game, self.agent_seen, self.agent_lost = True, False, None
if any(is_client(argv) for _, argv in own) and any(shares_sign_in(argv) for argv in games):
say("Battle.net stayed open beside a game signing in with its token: left open")
return False
say("the game is up: closing what is left of Battle.net (%d process(es)) to leave it their memory" % len(own))
stop(own)
return True
return False
self.game_since, self.closed_for_game = None, False
if not any(is_client(argv) for _, argv in processes):
self.agent_seen, self.agent_lost = False, None
elif agent_up(processes):
self.agent_seen, self.agent_lost = True, None
elif self.agent_seen:
self.agent_lost = self.agent_lost if self.agent_lost is not None else now
if now - self.agent_lost >= self.gone:
say("Battle.net has outlived its Agent: closing it (%d process(es))" % len(own))
stop(own)
notify("Battle.net was closed", "It had lost Blizzard's update service. Open it again to reconnect.",
"dialog-warning")
self.agent_seen, self.agent_lost = False, None
return True
return False
def proton_options():
if os.environ.get("BL_DESKTOP") == "1":
return ["--window"]
width, height = os.environ.get("BL_WIDTH", ""), os.environ.get("BL_HEIGHT", "")
options = ["--desktop", "%sx%s" % (width, height)] if width.isdigit() and height.isdigit() else []
game = os.environ.get("SteamGameId", "")
return options + (["--game-id", game] if game.isdigit() and game != "0" else [])
def stop(processes, wait=STOP_WAIT):
pids = [pid for pid, _ in processes]
for sig in (signal.SIGTERM, signal.SIGKILL):
for pid in pids:
try:
os.kill(pid, sig)
except OSError:
pass
deadline = time.monotonic() + wait
while time.monotonic() < deadline:
pids = [pid for pid in pids if os.path.exists("/proc/%d" % pid)]
if not pids:
return
time.sleep(0.2)
def queued_requests(launch_dir):
try:
names = os.listdir(launch_dir)
except OSError:
return []
return sorted(name for name in names if REQUEST_FILE.fullmatch(name))
def serve_requests(pfx, processes=None, launch_dir=None):
launch_dir = launch_dir or os.environ.get("BL_LAUNCH_DIR")
names = queued_requests(launch_dir) if launch_dir else []
if not names or not agent_up(wine_processes(pfx) if processes is None else processes):
return False
client = os.path.join(pfx, "drive_c", *CLIENT_DIR, CLIENT)
served = False
for name in names:
path = os.path.join(launch_dir, name)
try:
with open(path, encoding="utf-8") as f:
args = [line for line in f.read().splitlines() if line]
os.unlink(path)
except OSError:
continue
if len(args) == 2 and args[0] == "install-game" and GAME_UID.fullmatch(args[1]):
spawn(client, "run", ["--install", "--game=%s" % args[1]])
elif len(args) == 2 and args[0] == "launch" and CODE.fullmatch(args[1]):
spawn(client, "run", ["--exec=launch %s" % args[1]])
else:
say("not a request: %s" % " ".join(args))
continue
say("the app asked for %s" % " ".join(args))
served = True
return served
def wait_while_open(pfx, grace, poll=None):
deadline = time.monotonic() + grace
seen = False
while True:
serve_requests(pfx)
if running(pfx):
seen = True
elif seen or time.monotonic() >= deadline:
return seen
time.sleep(POLL if poll is None else poll)
def proton(exe, args=(), verb=None, watch=None, poll=None, close_grace=None, tidy=None):
cmd = [PROTON_RUN] + proton_options() + ["--prefix", APP_ID] + (["--verb", verb] if verb else []) + [exe] + list(args)
say(" ".join(cmd))
try:
process = subprocess.Popen(cmd, env=proton_env())
except OSError as error:
say("%s: %s" % (PROTON_RUN, error))
return 127, False
seen = False
closed_at = None
grace = CLOSE_GRACE if close_grace is None else close_grace
tidy = Tidy() if tidy is None else tidy
while True:
try:
status = process.wait(timeout=POLL if poll is None else poll)
return status, seen
except subprocess.TimeoutExpired:
pass
if watch is None:
continue
processes = wine_processes(watch)
serve_requests(watch, processes)
now = time.monotonic()
if tidy(processes, now):
seen = True
processes = wine_processes(watch)
if any(is_client(argv) for _, argv in processes):
seen, closed_at = True, None
elif seen and idle(processes):
closed_at = closed_at if closed_at is not None else now
if now - closed_at >= grace:
say("Battle.net closed; stopping %d process(es) it left in its prefix" % len(processes))
stop(processes)
closed_at = None
else:
closed_at = None
def setup_ready(path):
try:
with open(path, "rb") as f:
return f.read(2) == b"MZ" and os.path.getsize(path) >= MIN_INSTALLER
except OSError:
return False
def download(url=INSTALLER_URL, folder=CACHE):
os.makedirs(folder, exist_ok=True)
dest = os.path.join(folder, SETUP)
if setup_ready(dest):
return dest
part = dest + ".part"
try:
try:
request = urllib.request.Request(url, headers={"User-Agent": "DroidDeck"})
with urllib.request.urlopen(request, timeout=60) as response, open(part, "wb") as f:
shutil.copyfileobj(response, f)
length = response.headers.get("Content-Length", "")
if length.isdigit() and f.tell() != int(length):
raise OSError("the download ended early")
except (OSError, http.client.HTTPException) as error:
if not shutil.which("curl"):
raise
say("download through Python failed (%s); trying curl" % error)
if subprocess.call(["curl", "-fsSL", "--max-time", "300", "-o", part, url]) != 0:
raise OSError("curl could not download it")
if not setup_ready(part):
raise OSError("not a Windows program")
os.replace(part, dest)
except (OSError, http.client.HTTPException) as error:
say("installer download failed: %s" % error)
try:
os.unlink(part)
except OSError:
pass
return dest if os.path.isfile(dest) else None
def install(root, pfx, verb=None):
setup = download()
if setup is None:
notify("Battle.net could not be downloaded", "Check the connection and try again.", "dialog-error")
return False, False
configure(root, LIBRARY)
notify("Installing Battle.net", "Blizzard's installer opens in a window. It can take a few minutes.")
_, seen = proton(setup, verb=verb, watch=pfx)
return installed(root), seen
def acquire(folder=CACHE):
os.makedirs(folder, exist_ok=True)
handle = open(os.path.join(folder, "battlenet.lock"), "w")
try:
fcntl.flock(handle, fcntl.LOCK_EX | fcntl.LOCK_NB)
except OSError:
handle.close()
return None
return handle
def status(root):
pfx = os.path.join(prefix(root), "pfx")
return {"appid": int(APP_ID), "prefix": prefix(root), "installed": installed(root),
"launcher": launcher(root), "running": running(pfx)}
def spawn(program, verb, args=()):
cmd = [PROTON_RUN] + proton_options() + ["--prefix", APP_ID, "--verb", verb, program] + list(args)
say(" ".join(cmd))
try:
os.makedirs(CACHE, exist_ok=True)
with open(os.path.join(CACHE, "spawned.log"), "ab") as log:
child = subprocess.Popen(cmd, stdin=subprocess.DEVNULL, stdout=log, stderr=log, start_new_session=True, env=proton_env())
threading.Thread(target=child.wait, daemon=True).start()
except OSError as error:
say("%s: %s" % (PROTON_RUN, error))
return 127
return 0
def show(root, wait=None, poll=None, args=()):
pfx = os.path.join(prefix(root), "pfx")
client = os.path.join(client_dir(root), CLIENT)
deadline = time.monotonic() + (HANDOFF_WAIT if wait is None else wait)
while True:
processes = wine_processes(pfx)
if any(is_client(argv) for _, argv in processes) and os.path.isfile(client) \
and (agent_up(processes) or time.monotonic() >= deadline):
spawn(client, "run", args)
return None
lock = acquire()
if lock is not None:
say("the other start has ended; opening Battle.net")
return lock
if time.monotonic() >= deadline:
say("the other start neither opened Battle.net nor ended")
notify("Battle.net is busy", "Another start is still running; try again in a moment.", "dialog-warning")
return None
time.sleep(POLL if poll is None else poll)
def stop_leftovers(pfx):
processes = wine_processes(pfx)
if not processes:
return True
if not idle(processes):
say("the prefix is in use (%s); Battle.net starts beside it" % ", ".join(sorted({program(a) for _, a in processes} - IDLE)))
return False
say("stopping %d process(es) a closed Battle.net left in its prefix" % len(processes))
stop(processes)
return True
def open_client(root, reinstall=False):
pfx = os.path.join(prefix(root), "pfx")
write_menu()
answer = ask_fex(os.environ.get("BL_LAUNCH_DIR"))
say("FEX: %s" % (answer or "no answer from the app; Components' choice stays"))
if running(pfx):
say("Battle.net is already running in its prefix; waiting for it to close")
wait_while_open(pfx, grace=0)
return 0
verb = None if stop_leftovers(pfx) else "run"
if reinstall or not installed(root):
done, seen = install(root, pfx, verb)
if not done:
notify("Battle.net did not install", "The session's desktop.log says why.", "dialog-error")
return 1
if wait_while_open(pfx, grace=0 if seen else START_GRACE) or seen:
return 0
verb = None if stop_leftovers(pfx) else "run"
configure(root, LIBRARY)
status_code, seen = proton(launcher(root), verb=verb, watch=pfx)
say("Proton returned %d; Battle.net %s" % (status_code, "ran" if seen else "has not been seen yet"))
if not (wait_while_open(pfx, grace=0 if seen else START_GRACE) or seen):
say("Battle.net did not start (the launcher exited with %d)" % status_code)
notify("Battle.net did not start", "The session's desktop.log says why.", "dialog-error")
return status_code or 1
say("Battle.net closed")
return 0
def start(exe, verb=None):
cmd = [PROTON_RUN] + proton_options() + ["--prefix", APP_ID] + (["--verb", verb] if verb else []) + [exe]
say(" ".join(cmd))
try:
return subprocess.Popen(cmd, env=proton_env())
except OSError as error:
say("%s: %s" % (PROTON_RUN, error))
return None
def wait_ready(pfx, wait=None, poll=None, settle=None):
deadline = time.monotonic() + (CLIENT_WAIT if wait is None else wait)
while True:
count = sum(1 for _, argv in wine_processes(pfx) if is_client(argv))
if count >= CLIENT_READY:
time.sleep(READY_SETTLE if settle is None else settle)
return True
if time.monotonic() >= deadline:
return count > 0
time.sleep(POLL if poll is None else poll)
def follow(pfx, poll=None, grace=None, tidy=None, ask=None):
seen, gone_at = False, None
asked, asks = None, 1
grace = GAME_GRACE if grace is None else grace
tidy = Tidy() if tidy is None else tidy
while True:
processes = wine_processes(pfx)
serve_requests(pfx, processes)
now = time.monotonic()
if tidy(processes, now):
processes = wine_processes(pfx)
games = sorted({program(argv) for _, argv in processes if is_game(argv)})
if games:
if not seen:
say("game running: %s" % ", ".join(games))
seen, gone_at = True, None
elif seen or not any(is_client(argv) for _, argv in processes):
gone_at = gone_at if gone_at is not None else now
if now - gone_at >= grace:
say("the game has exited" if seen else "Battle.net closed before a game started")
return seen
else:
gone_at = None
asked = now if asked is None else asked
if ask is not None and asks < LAUNCH_ASKS and now - asked >= LAUNCH_WAIT:
say("Battle.net has not started the game yet; asking again")
ask()
asked, asks = now, asks + 1
time.sleep(POLL if poll is None else poll)
def launch(root, code):
pfx = os.path.join(prefix(root), "pfx")
client = os.path.join(client_dir(root), CLIENT)
if not installed(root) or not os.path.isfile(client):
say("Battle.net is not installed; %s is not launched" % code)
notify("Battle.net is not installed", "Install it from its tile on DroidDeck's Desktop tab.", "dialog-error")
return 1
lock = acquire()
try:
own = None
if lock is not None and not running(pfx):
answer = ask_fex(os.environ.get("BL_LAUNCH_DIR"))
say("FEX: %s" % (answer or "no answer from the app; Components' choice stays"))
verb = None if stop_leftovers(pfx) else "run"
configure(root, LIBRARY)
own = start(launcher(root), verb)
if own is None:
return 127
if not wait_ready(pfx):
say("Battle.net did not open; %s is not launched" % code)
notify("Battle.net did not start", "The session's desktop.log says why.", "dialog-error")
return 1
if code.lower().startswith("wow"):
wow_settings(root)
say("launching %s" % code)
spawn(client, "run", ["--exec=launch %s" % code])
played = follow(pfx, ask=lambda: spawn(client, "run", ["--exec=launch %s" % code]))
if own is not None:
leftovers = wine_processes(pfx)
say("stopping what the Battle.net opened for %s left: %d process(es)" % (code, len(leftovers)))
stop(leftovers)
try:
own.wait(timeout=STOP_WAIT * 4)
except subprocess.TimeoutExpired:
say("its Proton is still ending")
return 0 if played else 1
finally:
if lock is not None:
lock.close()
def hand_over(root, args, wait=None):
if not wait_ready(os.path.join(prefix(root), "pfx"), wait=HANDOVER_WAIT if wait is None else wait):
say("Battle.net did not come up; %s not asked for" % " ".join(args))
return False
spawn(os.path.join(client_dir(root), CLIENT), "run", args)
return True
def install_game(root, uid):
args = ["--install", "--game=%s" % uid]
if installed(root) and agent_up(wine_processes(os.path.join(prefix(root), "pfx"))):
say("asking the open Battle.net to install %s" % uid)
spawn(os.path.join(client_dir(root), CLIENT), "run", args)
return 0
lock = acquire()
if lock is None:
say("another start holds the lock")
lock = show(root, args=args)
if lock is None:
return 0
threading.Thread(target=hand_over, args=(root, args), daemon=True).start()
with lock:
return open_client(root)
def main(argv):
global LIBRARY
argv = list(argv)
if len(argv) >= 2 and argv[0] == "--library":
if not argv[1].startswith("/") or "\n" in argv[1]:
say("%s is not a folder" % argv[1])
return 2
LIBRARY = os.path.normpath(argv[1])
argv = argv[2:]
action = argv[0] if argv else "open"
launching = action == "launch" and len(argv) == 2 and CODE.fullmatch(argv[1])
getting = action == "install-game" and len(argv) == 2 and GAME_UID.fullmatch(argv[1])
if not (launching or getting) and (action not in ("open", "install", "status") or len(argv) > 1):
say("usage: droiddeck-battlenet [--library DIR] [open | install | status | launch CODE | install-game ID]")
return 2
root = steam_root()
if action == "status":
print(json.dumps(status(root)))
return 0
os.environ["BL_BATTLENET"] = "1"
if launching:
say("launch %s" % argv[1])
return launch(root, argv[1])
if getting:
say("install-game %s" % argv[1])
return install_game(root, argv[1])
say("%s (%s)" % (action, "from the menu" if not sys.stderr.isatty() else "from a terminal"))
lock = acquire()
if lock is None:
say("another start holds the lock")
lock = show(root)
if lock is None:
return 0
with lock:
return open_client(root, reinstall=action == "install")
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))
@@ -8,7 +8,7 @@ X64 = os.environ.get("BL_STEAM_CLIENT_ARCH") == "x86_64"
CLIENT_HOME = (os.environ.get("BL_X64_HOME") or os.path.join(HOME, ".droiddeck-x64")) if X64 else HOME
STEAM_ROOT = sys.argv[1] if len(sys.argv) > 1 else os.path.join(CLIENT_HOME, ".local/share/Steam")
LIBRARIES = ((STEAM_ROOT, "Steam Games"), ("/mnt/droiddeck-sd/x86-64" if X64 else "/mnt/droiddeck-sd", "SD Games"))
NOT_GAMES = {228980, 1070560, 1391110, 1493710, 1628350, 3127680, 4183110, 4185400, 4427310, 4628740}
NOT_GAMES = {228980, 1070560, 1161040, 1391110, 1493710, 1628350, 1826330, 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)
@@ -589,7 +589,8 @@ def guest_environment(fex, env):
result["FEX_THUNKHOSTLIBS"] = fex["host_thunks"]
result["FEX_THUNKGUESTLIBS"] = fex["guest_thunks"]
result["FEX_THUNKCONFIG"] = os.path.join(CONFIG_DIR, "ThunksConfig.json")
preload = [part for part in split_paths(result.get("LD_PRELOAD")) if os.path.exists(part) and not is_arm(part)]
preload = [part for part in split_paths(result.get("LD_PRELOAD"))
if "/" not in part or (os.path.exists(part) and not is_arm(part))]
library_path = [part for part in (result.get("LD_LIBRARY_PATH") or "").split(":")
if part and "arm64" not in part and "aarch64" not in part]
for name, parts in (("LD_PRELOAD", preload), ("LD_LIBRARY_PATH", library_path)):
@@ -671,15 +672,26 @@ def launch(command, env, mode, decide=None):
return os.execve(fex["bin"], [fex["bin"], program] + command[1:], guest)
def game_environment(env):
def game_environment(env, command):
try:
game_env = runpy.run_path(os.path.join(BIN_DIR, "droiddeck-game-env"))
env = game_env["game_gpu"](env)
app = game_env["game_id"](env) or env.get("SteamAppId") or env.get("SteamGameId")
if not app:
return env
with open(GAME_ENVIRONMENT, encoding="utf-8") as source:
return game_env["apply_config"](env, json.load(source), str(app))
fixes = {}
game = game_env["Game"](resolve(command[0], env), command[1:])
for check in game_env["FIXES"]:
found = check(game)
if found and found[0]:
fixes.update(found[0])
say(found[2])
try:
with open(GAME_ENVIRONMENT, encoding="utf-8") as source:
config = json.load(source)
except FileNotFoundError:
config = {"version": 1}
return game_env["apply_config"](env, config, str(app), fixes)
except FileNotFoundError:
return env
except Exception as error:
@@ -810,7 +822,7 @@ def main(argv):
return 0
if not command:
return 2
return launch(command, game_environment(dict(os.environ)), os.environ.get("DROIDDECK_FEX", "auto"))
return launch(command, game_environment(dict(os.environ), command), os.environ.get("DROIDDECK_FEX", "auto"))
if action == "run":
try:
mode, decide, command = parse_run(args)
@@ -3,6 +3,7 @@ set -u
export LANG=C.UTF-8
step() { echo "== STEP $*"; }
fail() { echo "== FAIL $*"; exit 1; }
[ "$(id -u)" = 0 ] || fail "needs root: install Flatpak from the launcher's Store, which runs this as root"
PACKAGES="flatpak ostree appstream libxmlb libmalcontent libfyaml accountsservice composefs xdg-dbus-proxy"
while read -r pkg probe; do
@@ -48,10 +49,13 @@ if [ ! -f /etc/pacman.d/gnupg/pubring.gpg ] && [ ! -f /etc/pacman.d/gnupg/pubrin
fi
step "Reading the package lists"
pacman --config "$CONF" --dbpath "$WORK/db" --noconfirm -Sy >/dev/null 2>&1 \
|| pacman --config "$CONF" --dbpath "$WORK/db" --noconfirm -Sy 2>&1 | tail -3 \
|| fail "could not reach the Arch Linux ARM mirrors"
[ -f "$WORK/db/sync/extra.db" ] || fail "could not reach the Arch Linux ARM mirrors"
sync_log=$(pacman --config "$CONF" --dbpath "$WORK/db" --noconfirm -Sy 2>&1) \
|| sync_log=$(pacman --config "$CONF" --dbpath "$WORK/db" --noconfirm -Sy 2>&1)
if [ ! -f "$WORK/db/sync/extra.db" ]; then
printf '%s\n' "$sync_log" | tail -3
reason=$(printf '%s\n' "$sync_log" | grep -m1 -i '^error' | cut -c1-160)
fail "could not reach the Arch Linux ARM mirrors${reason:+ ($reason)}"
fi
step "Downloading Flatpak"
out=$(pacman --config "$CONF" --dbpath "$WORK/db" --cachedir "$WORK/pkg" --noconfirm -Sw -dd $PACKAGES 2>&1)
@@ -6,6 +6,7 @@ import re
import runpy
import subprocess
import sys
import tempfile
from pathlib import Path
GAME_TU_DEBUG = "DROIDDECK_GAME_TU_DEBUG"
@@ -87,6 +88,12 @@ def dotnet_memory_ordering(game):
return DOTNET_FEX, [], ".NET runtime: FEX with full TSO and without multiblock"
@fix
def unity_memory_ordering(game):
if {"unityplayer.so", "gameassembly.so", "unityplayer.dll"} & game.files:
return {"FEX_TSOENABLED": "1"}, [], "Unity: FEX with TSO"
@fix
def godot_vulkan(game):
if is_godot(game) and not game.has_arg("--rendering-driver"):
@@ -114,6 +121,35 @@ def arkham_city_x87(game):
return {"FEX_X87REDUCEDPRECISION": "0"}, [], "Batman: Arkham City: FEX with full x87 precision"
@fix
def protonfixes_off(game):
if game.stem in ("blackops3", "darksoulsremastered"):
return {"PROTONFIXES_DISABLE": "1"}, [], "umu-protonfixes off (its ALSA audio and VC++ installer steps fail here)"
@fix
def caravan_sandwitch_feature_level(game):
if "caravansandwitch.exe" in game.files or game.stem == "caravansandwitch-win64-shipping":
return {"VKD3D_FEATURE_LEVEL": "12_1"}, [], "Caravan SandWitch: D3D12 feature level 12_1 (12_2 hands it mesh shader pipelines vkd3d-proton cannot build)"
@fix
def robocop_rogue_city_feature_level(game):
"""vkd3d-proton turns mesh shaders off on Adreno, but 12_2 still advertises them, and UE5 then builds mesh
pipelines with no pipeline layout that crash Turnip on launch."""
if "robocop.exe" in game.files or game.stem == "robocop-win64-shipping":
return {"VKD3D_FEATURE_LEVEL": "12_1"}, [], "RoboCop: Rogue City: D3D12 feature level 12_1"
@fix
def cyberpunk_feature_level(game):
if game.stem == "cyberpunk2077" or (
game.stem == "redprelauncher"
and os.path.isfile(os.path.join(os.path.dirname(game.exe), "bin", "x64", "Cyberpunk2077.exe"))
):
return {"VKD3D_FEATURE_LEVEL": "12_1"}, [], "Cyberpunk 2077: D3D12 feature level 12_1"
def skip_red_launcher(command):
"""Cyberpunk 2077's prelauncher opens REDlauncher, a Qt WebEngine window that hands over to the
game half a minute later and then stays up beside it, well over a core busy for as long as the
@@ -127,6 +163,61 @@ def skip_red_launcher(command):
return command
UBISOFT_LAUNCHER = "drive_c/Program Files (x86)/Ubisoft/Ubisoft Game Launcher"
UBISOFT_SETTINGS = "drive_c/users/steamuser/AppData/Local/Ubisoft Game Launcher/settings.yaml"
def overlay_off(text):
lines = text.splitlines()
try:
start = next(i for i, line in enumerate(lines) if re.fullmatch(r"overlay:\s*(#.*)?", line))
except StopIteration:
if lines and not lines[-1].strip():
lines.pop()
return "\n".join(lines + ["overlay:", " enabled: false", " warning_enabled: false"]) + "\n"
end = start + 1
for index in range(start + 1, len(lines)):
if lines[index].strip() and not lines[index].startswith(" "):
break
if lines[index].strip():
end = index + 1
block = lines[start + 1:end]
for key in ("enabled", "warning_enabled"):
entry = " %s: false" % key
found = [i for i, line in enumerate(block) if line.split(":", 1)[0] == " " + key]
if found:
block[found[0]] = entry
else:
block.append(entry)
return "\n".join(lines[:start + 1] + block + lines[end:]) + "\n"
def ubisoft_overlay_off(env):
prefix = Path(env.get("STEAM_COMPAT_DATA_PATH", "/nonexistent")) / "pfx"
if not (prefix / UBISOFT_LAUNCHER).is_dir():
return
settings = prefix / UBISOFT_SETTINGS
try:
text = settings.read_text(encoding="utf-8") if settings.exists() else ""
updated = overlay_off(text)
if updated == text:
return
settings.parent.mkdir(parents=True, exist_ok=True)
handle, staged = tempfile.mkstemp(prefix=".settings-", dir=settings.parent)
try:
with os.fdopen(handle, "w", encoding="utf-8") as target:
target.write(updated)
if settings.exists():
os.chmod(staged, settings.stat().st_mode & 0o7777)
os.replace(staged, settings)
except BaseException:
os.unlink(staged)
raise
print("droiddeck-game-env: Ubisoft Connect overlay turned off", file=sys.stderr)
except (OSError, UnicodeDecodeError) as error:
print("droiddeck-game-env: Ubisoft Connect overlay left as is (%s)" % error, file=sys.stderr)
def offline_without_anticheat(command, env):
"""Start the configured game executable when the player opts into launching without EAC."""
if (env.get("DROIDDECK_ANTICHEAT_OFFLINE") != "1" or len(command) <= 2
@@ -196,6 +287,58 @@ def game_gpu(env):
return result
def vram_cap(env):
"""Puts the session's VRAM cap ahead of the game's own DXVK settings, so a game profile can still raise it."""
value = env.get("BL_VRAM_MB", "")
if not re.fullmatch(r"[1-9][0-9]{0,6}", value):
return env
result = env.copy()
cap = "dxgi.maxDeviceMemory = %d" % int(value)
own = result.get("DXVK_CONFIG", "").strip().strip(";").strip()
result["DXVK_CONFIG"] = cap + "; " + own if own else cap
return result
def parse_cores(text):
"""CPU numbers from a list such as "0-3,6"; None when the text is not one."""
cores = set()
for part in text.replace(" ", "").split(","):
match = re.fullmatch(r"(\d{1,3})(?:-(\d{1,3}))?", part)
if not match:
return None
first, last = int(match.group(1)), int(match.group(2) or match.group(1))
if last < first:
return None
cores.update(range(first, last + 1))
return cores or None
def online_cores():
try:
with open("/sys/devices/system/cpu/online") as f:
cores = parse_cores(f.read().strip())
except OSError:
cores = None
return cores or os.sched_getaffinity(0) | set(range(os.cpu_count() or 0))
def game_cores(env):
"""Pins the game to the cores its profile names, in place of the session's game cores."""
text = env.get("DROIDDECK_GAME_CORES", "").strip()
if not text:
return
cores = parse_cores(text)
chosen = cores & online_cores() if cores else None
if not chosen:
print("droiddeck-game-env: ignoring game cores %r" % text, file=sys.stderr)
return
try:
os.sched_setaffinity(0, chosen)
print("droiddeck-game-env: game cores %s" % ",".join(map(str, sorted(chosen))), file=sys.stderr)
except OSError as error:
print("droiddeck-game-env: could not use game cores %r (%s)" % (text, error), file=sys.stderr)
def apply_config(env, config, app_id, fixes=None):
if config.get("version") != 1:
raise ValueError("unsupported environment configuration")
@@ -497,6 +640,7 @@ def main():
fixes = {}
if waiting:
command = skip_red_launcher(command)
ubisoft_overlay_off(env)
fixes, extra, notes = engine_fixes(command[2], command[3:]) if len(command) > 2 else ({}, [], [])
for note in notes:
print("droiddeck-game-env: " + note, file=sys.stderr)
@@ -528,7 +672,9 @@ def main():
command = offline_without_anticheat(command, env)
if mic_enabled == "0":
env["BANNER_AUDIO_DIRECT_MIC"] = "0"
env = vram_cap(env)
env = force_ssbs(command, env)
game_cores(env)
try:
xbox = runpy.run_path(str(Path(__file__).with_name("droiddeck-xbox")))
except Exception as error:
@@ -1,7 +1,7 @@
#!/usr/bin/python3
"""Runs a Windows program with the Proton the Steam client has chosen for it.
droiddeck-proton-run [--window] PROGRAM [ARGS...]
droiddeck-proton-run [--window] [--prefix ID] [--verb run] [--game-id ID] [--desktop WxH] PROGRAM [ARGS...]
droiddeck-proton-run --app ID [--dir FOLDER] [--options TEXT] [PROGRAM [ARGS...]]
The program belongs to an installed Steam game when it sits in one's folder, to a prefix when it
@@ -10,10 +10,15 @@ its own, under the id the client gives a new shortcut. That title's CompatToolMa
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.
--window keeps the program in the desktop's own window, drawn with WineD3D on the desktop's software
GL (llvmpipe): KWin there composites in software, and the session's zink finds no device.
--prefix ID puts the program in compatdata/ID whatever it is. --verb run starts it beside what already
runs in the prefix, which waitforexitandrun would wait for; the wrapper's launch-time steps are skipped.
--game-id ID keeps the id of the launch it runs for: gamescope shows the windows that carry the id Steam launched.
--app starts one game without the Steam client (its Start without Steam): a Steam game's default
Windows launch option from appinfo.vdf when no program is given, else the program in that
shortcut's own prefix, started in --dir with the shortcut's launch options (--options).
shortcut's own prefix, started in --dir with the shortcut's launch options (--options). Without
--options it takes the launch options set in the client's Properties for that game or shortcut.
"""
import json
import os
@@ -30,7 +35,10 @@ 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")
DESKTOP_GL = {"LIBGL_ALWAYS_SOFTWARE": "1", "GALLIUM_DRIVER": "llvmpipe"}
NOT_DESKTOP_GL = ("MESA_LOADER_DRIVER_OVERRIDE", "LIBGL_KOPPER_DRI2")
VERB = "waitforexitandrun"
VERBS = (VERB, "run")
SHORTCUT_BIT = 0x80000000
STEAM_APP_LIMIT = 2 ** 31
@@ -185,9 +193,9 @@ def steam_game(program, libs):
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():
if below == "pfx" and os.path.basename(parent) == "compatdata" and re.fullmatch(r"-?[0-9]+", os.path.basename(folder)):
library = os.path.dirname(os.path.dirname(parent))
return Target(os.path.basename(folder), folder, os.path.dirname(program), library, "prefix")
return Target(str(int(os.path.basename(folder)) % 2 ** 32), folder, os.path.dirname(program), library, "prefix")
return None
@@ -230,10 +238,68 @@ def shortcut_entries(root):
return entries
def prefix_folder(root, appid):
"""The prefix folder the client made for [appid], under either spelling of the id."""
folder = os.path.join(root, "steamapps", "compatdata")
try:
names = sorted(os.listdir(folder))
except OSError:
names = []
candidates = []
for name in names:
if not re.fullmatch(r"-?[0-9]+", name):
continue
try:
matches = int(name) % 2 ** 32 == appid % 2 ** 32
except ValueError:
continue
match = os.path.join(folder, name)
if matches and os.path.isdir(match):
candidates.append((os.path.isdir(os.path.join(match, "pfx")),
name == str(appid % 2 ** 32), name, match))
if candidates:
return min(candidates, key=lambda item: (not item[0], not item[1], item[2]))[3]
return os.path.join(folder, str(appid % 2 ** 32))
def vdf_string(token):
return re.sub(r'\\(.)', r'\1', token[1:-1]) if token.startswith('"') else ""
def steam_options(app, root):
"""The launch options the client keeps for a game (localconfig.vdf) or a shortcut (shortcuts.vdf)."""
try:
tool = runpy.run_path(os.path.join(BIN_DIR, "droiddeck-steam-shortcuts"))
account = tool["account_dir"](root)
if not account:
return ""
if int(app) >= STEAM_APP_LIMIT:
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 isinstance(value, dict) and (value.get("appid", value.get("AppId")) or 0) & 0xFFFFFFFF == int(app):
options = value.get("LaunchOptions", value.get("launchoptions", ""))
return options if isinstance(options, str) else ""
return ""
tokens = compat()["tokenize"](read_text(os.path.join(account, "config", "localconfig.vdf")) or "")
start = compat()["find_block"](tokens, ["UserLocalConfigStore", "Software", "Valve", "Steam", "apps", app])
if start is None:
return ""
end, depth = compat()["block_end"](tokens, start), 0
for i in range(start + 1, end):
if tokens[i] in ("{", "}"):
depth += 1 if tokens[i] == "{" else -1
elif depth == 0 and tokens[i].lower() == '"launchoptions"' and i + 1 < end:
return vdf_string(tokens[i + 1])
except (OSError, ValueError, IndexError, KeyError, TypeError, AttributeError, struct.error):
pass
return ""
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)),
return Target(appid, prefix_folder(root, appid),
os.path.dirname(program), root, "shortcut", start if os.path.isdir(start) else None)
return None
@@ -250,10 +316,19 @@ def locate(program, root):
if found:
return found
appid = own_id(program)
return Target(appid, os.path.join(root, "steamapps", "compatdata", str(appid)),
return Target(appid, prefix_folder(root, appid),
os.path.dirname(program), root, "own")
def fixed(program, root, appid):
return Target(appid, os.path.join(root, "steamapps", "compatdata", str(appid)),
os.path.dirname(program), root, "fixed")
def prefix_id(value):
return value if value.isdigit() and not value.startswith("0") and int(value) <= 0xFFFFFFFF else None
def split_words(text):
try:
return shlex.split(text or "")
@@ -325,7 +400,8 @@ def steam_launch(appid, root):
def shortcut_target(appid, program, start, root):
return Target(appid, os.path.join(root, "steamapps", "compatdata", str(appid)), os.path.dirname(program), root,
appid = int(appid) % 2 ** 32
return Target(str(appid), prefix_folder(root, appid), os.path.dirname(program), root,
"shortcut", start if start and os.path.isdir(start) else None)
@@ -337,7 +413,7 @@ def steam_launches(target, program, root):
return os.path.relpath(program, target.install).replace(os.sep, "/").lower() in entry.get("launch", ())
def environment(env, target, tool_folder, root, window=False):
def environment(env, target, tool_folder, root, window=False, game_id=None):
env = dict(env)
steamapps = [os.path.join(lib, "steamapps") for lib in libraries(root)]
env.update({
@@ -355,28 +431,52 @@ def environment(env, target, tool_folder, root, window=False):
else:
game = str(number << 32 | 0x02000000)
env.update(SteamAppId="0", STEAM_COMPAT_APP_ID="0", SteamGameId=game, SteamOverlayGameId=game)
if game_id:
env.update(SteamGameId=game_id, SteamOverlayGameId=game_id)
if window:
env.setdefault("PROTON_USE_WINED3D", "1")
env.update(DESKTOP_GL)
for name in NOT_DESKTOP_GL:
env.pop(name, None)
return env
def windows_path(path):
def windows_path(path, compatdata=None):
if compatdata:
drive = os.path.join(compatdata, "pfx", "drive_c")
inside = os.path.relpath(path, drive)
if inside != "." and not inside.startswith(".."):
return "C:\\" + inside.replace("/", "\\")
return "Z:" + path.replace("/", "\\")
def arguments(program, args):
def arguments(program, args, desktop=None, compatdata=None):
if program.lower().endswith(".msi"):
return ["msiexec", "/i", windows_path(program)] + args
if desktop:
return ["explorer.exe", "/desktop=droiddeck,%s" % desktop, windows_path(program, compatdata)] + args
return [program] + args
def main(argv):
window = False
app, start, options = None, None, ""
prefix = None
verb = VERB
game_id = None
desktop = None
app, start, options = None, None, None
while argv and argv[0].startswith("--"):
option = argv.pop(0)
if option == "--window":
window = True
elif option == "--prefix" and argv and prefix_id(argv[0]):
prefix = argv.pop(0)
elif option == "--verb" and argv and argv[0] in VERBS:
verb = argv.pop(0)
elif option == "--game-id" and argv and argv[0].isdigit() and argv[0] != "0":
game_id = argv.pop(0)
elif option == "--desktop" and argv and re.fullmatch(r"[1-9][0-9]{2,4}x[1-9][0-9]{2,4}", argv[0]):
desktop = argv.pop(0)
elif option in ("--app", "--dir", "--options") and argv:
value = argv.pop(0)
if option == "--app":
@@ -394,9 +494,11 @@ def main(argv):
say("%s is not an app id" % app)
return 2
if not argv and app is None:
say("usage: droiddeck-proton-run [--window] PROGRAM [ARGS...]")
say("usage: droiddeck-proton-run [--window] [--prefix ID] [--verb run] [--game-id ID] [--desktop WxH] PROGRAM [ARGS...]")
return 2
root = os.environ.get("STEAM_COMPAT_CLIENT_INSTALL_PATH") or STEAM_ROOT
if options is None:
options = steam_options(app, root) if app is not None else ""
extra, args = split_options(options)
if not argv:
if int(app) >= STEAM_APP_LIMIT:
@@ -416,6 +518,8 @@ def main(argv):
args = args + argv[1:]
if app is not None and int(app) >= STEAM_APP_LIMIT:
target = shortcut_target(app, program, start, root)
elif prefix:
target = fixed(program, root, prefix)
else:
target = locate(program, root)
available = tools(root)
@@ -425,7 +529,7 @@ def main(argv):
return 69
folder, command = available[name]
os.makedirs(target.compatdata, exist_ok=True)
env = environment(os.environ, target, folder, root, window)
env = environment(os.environ, target, folder, root, window, game_id)
env.update(extra)
say("%s with %s (%s), %s prefix %s%s" % (program, name, why, target.kind, target.compatdata,
", without the Steam client" if app is not None else ""))
@@ -434,7 +538,7 @@ def main(argv):
except OSError:
os.chdir(os.path.dirname(program))
entry = os.path.join(folder, command)
os.execve(entry, [entry, VERB] + arguments(program, args), env)
os.execve(entry, [entry, verb] + arguments(program, args, desktop, target.compatdata), env)
if __name__ == "__main__":
@@ -24,7 +24,7 @@ read_scripts() { xargs -0 -r awk -v want="${1:-redists}" '
proc[depth] = 1
program = tolower(part[4])
sub(/.*[\\\/]/, "", program)
if (program ~ /vulkan/) vulkan[depth] = 1
if (program ~ /vulkan|uplay|ubisoft/) vulkan[depth] = 1
next
}
if (n == 3) pending = part[2]
@@ -243,7 +243,7 @@ for system in "${regs[@]}"; do
had=()
for line in "${kept[@]}"; do had[$line]=1; done
printf '%s\n' "${other[@]}" "${kept[@]}" >"$seeded"
echo "== took Vulkan runtime markers out of $reg"
echo "== took Vulkan runtime and Ubisoft Connect markers out of $reg"
fi
if [ ${#had[@]} -gt 0 ] && { [ -z "$id" ] || [ "$id" != "$prev" ]; }; then
declare -A held=()
@@ -86,12 +86,6 @@ run_gamescope() (
collect_steam_logs() {
[ -n "${BL_DEBUG_DIR:-}" ] || return 0
local steam_root=${XDG_DATA_HOME:-$HOME/.local/share}/Steam
[ "${steam_x64:-0}" = 1 ] && steam_root=${BL_X64_HOME:-$HOME/.droiddeck-x64}/.local/share/Steam
mkdir -p "$BL_DEBUG_DIR/steam" 2>/dev/null || return 0
[ -d "$steam_root/logs" ] && find "$steam_root/logs" -maxdepth 1 -type f -size -8192k \
-exec cp {} "$BL_DEBUG_DIR/steam/" \; 2>/dev/null
[ "${steam_x64:-0}" = 0 ] && [ -f "$HOME/.steam/steam/logs/steamwebhelper.log" ] && cp "$HOME/.steam/steam/logs/steamwebhelper.log" "$BL_DEBUG_DIR/steam/" 2>/dev/null
local sync_store=$HOME/.local/share/droiddeck-esync sync_file
if [ -d "$sync_store" ] && mkdir -p "$BL_DEBUG_DIR/droiddeck-esync" 2>/dev/null; then
for sync_file in launches.log tools.tsv wanted.tsv prefixes.json; do
@@ -102,7 +96,6 @@ collect_steam_logs() {
if [ -d "$fex_state" ] && mkdir -p "$BL_DEBUG_DIR/droiddeck-fex" 2>/dev/null; then
cp "$fex_state/status.json" "$fex_state/config/Config.json" "$BL_DEBUG_DIR/droiddeck-fex/" 2>/dev/null
fi
echo "== steam logs copied to $BL_DEBUG_DIR/steam"
}
trap 'collect_steam_logs 2>/dev/null || true' TERM INT HUP
if [ -n "${BL_VK_DRIVER:-}" ]; then
@@ -218,20 +211,31 @@ if [ -z "${BL_INSIDE:-}" ]; then
width=${BL_WIDTH:-1280}
height=${BL_HEIGHT:-720}
nested=$width
nested_h=$height
if [ "${BL_FOLLOW_OUTPUT:-0}" = 1 ]; then
echo "== gamescope: the output follows the screen (${width}x${height} to start)"
elif [ "$mode" = steam ] && [ "${BL_GAMESCOPE_STRETCH_16X9:-0}" = 1 ]; then
wide=$(( (height * 16 + 4) / 9 / 2 * 2 ))
tall=$(( (width * 9 + 8) / 16 / 2 * 2 ))
if [ "$wide" -gt "$width" ]; then
nested=$wide
echo "== gamescope: games at ${nested}x${height} stretched to the ${width}x${height} output"
elif [ "$tall" -gt "$height" ]; then
nested_h=$tall
fi
if [ "$nested" != "$width" ] || [ "$nested_h" != "$height" ]; then
echo "== gamescope: games at ${nested}x${nested_h} stretched to the ${width}x${height} output"
fi
fi
if [ "$mode" = steam ]; then
# Off unless Performance asks for it. Compositing the cursor here adds a second
# plane on the phone's compositor; a controller-driven cursor is the reason to turn it on.
if [ "$mode" = steam ] && [ "${BL_CURSOR_COMPOSITE:-0}" = 1 ]; then
export gamescope_cursor_composite=2
fi
args=(--backend wayland --expose-wayland -f -W "$width" -H "$height" -w "$nested" -h "$height")
[ "$nested" != "$width" ] && args+=(-S stretch) && export BL_STRETCHED=1
args=(--backend wayland --expose-wayland -f -W "$width" -H "$height" -w "$nested" -h "$nested_h")
if [ "$nested" != "$width" ] || [ "$nested_h" != "$height" ]; then
args+=(-S stretch)
export BL_STRETCHED=1
fi
if [ "${BL_FPS:-0}" -gt 0 ]; then
args+=(-r "$BL_FPS")
elif [ "${BL_REFRESH:-0}" -gt 0 ]; then
@@ -401,8 +405,14 @@ case $mode in
steam_root=$XDG_DATA_HOME/Steam
if [ -f "${BL_X64_HOME:-$HOME/.droiddeck-x64}/.local/share/Steam/local.vdf" ]; then
droiddeck-steam-x64 --sync-login || true
droiddeck-steam-x64 --share || true
fi
steam_client=("$steam_root/steamrtarm64/steam")
_bl_srt_shim=$HOME/.droiddeck-steamrt-shim
if droiddeck-steamrt-shim "$steam_root/steamrtarm64" "$_bl_srt_shim"; then
echo "== steam: Valve containerized the arm64 Steam client (it carries a Steam Runtime container); starting it through $_bl_srt_shim"
steam_client=(env STEAM_RUNTIME_STEAMRT="$_bl_srt_shim" PATH="$_bl_srt_shim/bin:$PATH" "${steam_client[@]}")
fi
fi
_bl_proton_root=$steam_root
if [ "$steam_x64" = 1 ]; then
@@ -445,48 +455,135 @@ case $mode in
fi
if [ "${BL_ZRAM:-0}" = 1 ]; then
step "zRAM compression: on"
[ "${BL_ZRAM_CLIENT:-1}" = 1 ] || step "zRAM: Steam is not compressed while games run (turned off in Performance)"
_bl_ram_cap=0
case ${BL_RAM_CAP_MB:-} in
''|0) ;;
*[!0-9]*|0*|???????*) step "RAM cap: ignored invalid value '$BL_RAM_CAP_MB'" ;;
*) _bl_ram_cap=$((BL_RAM_CAP_MB * 1024)); step "RAM cap: ${BL_RAM_CAP_MB} MB" ;;
esac
(
bl_zram_scan() {
_bl_game=0 _bl_pids=()
_bl_game=0 _bl_pids=() _bl_others=()
for _d in /proc/[0-9]*; do
[ "${_d#/proc/}" != "$BASHPID" ] || continue
# Under FEX the program is FEX's first argument.
{ IFS= read -r -d '' _c && case ${_c##*/} in FEX|FEXInterpreter) IFS= read -r -d '' _c ;; esac; } \
2>/dev/null < "$_d/cmdline" || continue
case ${_c##*/} in
reaper) _bl_game=1 ;;
steam|steamwebhelper) _bl_pids+=("${_d#/proc/}") ;;
*) _bl_others+=("${_d#/proc/}") ;;
esac
done
}
bl_zram_usage() {
local _p _k _v _u _rss=0 _swap=0
for _p in "${_bl_pids[@]}"; do
for _p in "$@"; do
while read -r _k _v _u; do
case $_k in VmRSS:) _rss=$((_rss + _v)) ;; VmSwap:) _swap=$((_swap + _v)) ;; esac
done 2>/dev/null < "/proc/$_p/status"
done
echo "${#_bl_pids[@]} processes, resident $((_rss / 1024)) MB, in zram $((_swap / 1024)) MB"
_bl_rss=$_rss
echo "$# processes, resident $((_rss / 1024)) MB, in zram $((_swap / 1024)) MB"
}
# Only processes that loaded libblsession are the session's own and answer SIGURG.
bl_session_others() {
local _p
_bl_session=()
for _p in "${_bl_others[@]}"; do
grep -q -m1 '/libblsession\.so$' "/proc/$_p/maps" 2>/dev/null && _bl_session+=("$_p")
done
}
bl_zram_pick() {
if [ "$1" = session ]; then
bl_session_others
_pids=("${_bl_session[@]}")
else
_pids=("${_bl_pids[@]}")
fi
}
# Same protocol as SessionSuspendController: an empty ack when the page-out starts, a byte when it is done.
# A process whose SIGURG handler was replaced never acks, so it is not waited for after the first 2 s.
bl_zram_wait() {
local _p _left _start=$SECONDS
while [ $((SECONDS - _start)) -lt 12 ]; do
_left=0
for _p in "$@"; do
[ -s "/tmp/.bl-zram.ack.$_p" ] && continue
[ -d "/proc/$_p" ] || continue
[ $((SECONDS - _start)) -ge 2 ] && [ ! -e "/tmp/.bl-zram.ack.$_p" ] && continue
_left=1
done
[ "$_left" = 1 ] || break
nap 0.2
done
for _p in "$@"; do rm -f "/tmp/.bl-zram.ack.$_p"; done
}
bl_zram_send() {
step "zRAM: $2 ($(bl_zram_usage))"
printf '%s' "$1" > /tmp/.bl-zram
[ "${#_bl_pids[@]}" = 0 ] || kill -URG "${_bl_pids[@]}" 2>/dev/null
nap 5
local _mode=$1 _what=$2 _which=$3 _pids _p
bl_zram_pick "$_which"
step "zRAM: $_what ($(bl_zram_usage "${_pids[@]}"))"
for _p in "${_pids[@]}"; do rm -f "/tmp/.bl-zram.ack.$_p"; done
printf '%s' "$_mode" > /tmp/.bl-zram
[ "${#_pids[@]}" = 0 ] || kill -URG "${_pids[@]}" 2>/dev/null
if [ "$_mode" = 1 ]; then bl_zram_wait "${_pids[@]}"; else nap 5; fi
bl_zram_scan
step "zRAM: done ($(bl_zram_usage))"
bl_zram_pick "$_which"
step "zRAM: done ($(bl_zram_usage "${_pids[@]}"))"
}
_bl_since=-1 _bl_packed=-1
bl_over_cap() {
[ "$_bl_ram_cap" -gt 0 ] || return 1
_bl_measured=$SECONDS
bl_session_others
bl_zram_usage "${_bl_pids[@]}" "${_bl_session[@]}" > /dev/null
[ "$_bl_rss" -gt "$_bl_ram_cap" ]
}
bl_cap_due() {
[ "$_bl_ram_cap" -gt 0 ] && { [ "$_bl_measured" -lt 0 ] || [ $((SECONDS - _bl_measured)) -ge 30 ]; }
}
_bl_since=-1 _bl_packed=-1 _bl_measured=-1 _bl_unpack=0 _bl_capped=-1 _bl_backoff=30
while kill -0 $$ 2>/dev/null; do
bl_zram_scan
if bl_cap_due; then
if bl_over_cap; then
# A cap below the game's working set cannot be met; paging out again every 30 s only churns.
if [ "$_bl_capped" -lt 0 ] || [ $((SECONDS - _bl_capped)) -ge "$_bl_backoff" ]; then
bl_zram_usage "${_bl_pids[@]}" > /dev/null
if [ "$_bl_rss" -ge $((512 * 1024)) ]; then
bl_zram_send 1 "over the ${BL_RAM_CAP_MB} MB RAM cap, compressing the client" client
[ "$_bl_game" = 1 ] && _bl_packed=$SECONDS
else
step "RAM cap: over, client already small ($((_bl_rss / 1024)) MB resident), skipping it"
fi
bl_over_cap && bl_zram_send 1 "still over the RAM cap, compressing the rest of the session" session
if bl_over_cap; then
_bl_backoff=$((_bl_backoff * 2 > 600 ? 600 : _bl_backoff * 2))
step "RAM cap: still over after paging out, next try in ${_bl_backoff} s"
else
_bl_backoff=30
fi
_bl_capped=$SECONDS
fi
else
_bl_backoff=30
if [ "$_bl_unpack" = 1 ] && [ "$_bl_game" = 0 ]; then
bl_zram_send 0 "back under the RAM cap, reading the client back" client
_bl_unpack=0
fi
fi
fi
if [ "$_bl_game" = 1 ]; then
[ "$_bl_since" -ge 0 ] || _bl_since=$SECONDS
if [ $((SECONDS - _bl_since)) -ge 20 ] && [ "${#_bl_pids[@]}" -gt 0 ] &&
if [ "${BL_ZRAM_CLIENT:-1}" = 1 ] && [ $((SECONDS - _bl_since)) -ge 20 ] && [ "${#_bl_pids[@]}" -gt 0 ] &&
{ [ "$_bl_packed" -lt 0 ] || [ $((SECONDS - _bl_packed)) -ge 300 ]; }; then
bl_zram_send 1 "compressing the client"
bl_zram_send 1 "compressing the client" client
_bl_packed=$SECONDS
fi
elif [ "$_bl_since" -ge 0 ]; then
[ "$_bl_packed" -lt 0 ] || bl_zram_send 0 "game closed, reading the client back"
if [ "$_bl_packed" -ge 0 ]; then
if bl_over_cap; then _bl_unpack=1; else bl_zram_send 0 "game closed, reading the client back" client; _bl_unpack=0; fi
fi
_bl_since=-1 _bl_packed=-1
fi
nap 5
@@ -526,6 +623,11 @@ case $mode in
fi
droiddeck-seed-redists "$steam_root" || true
fi
if [ -n "${BL_SHADER_PRECACHE:-}" ]; then
droiddeck-steam-shaders "$BL_SHADER_PRECACHE" "$XDG_DATA_HOME/Steam" \
"${BL_X64_HOME:-$HOME/.droiddeck-x64}/.local/share/Steam" &&
echo "$BL_SHADER_PRECACHE" > "$HOME/.bl-shader-precache-applied" || true
fi
if [ "$steam_x64" = 0 ] && [ ! -f "$steam_root/steamapps/appmanifest_4427310.acf" ]; then
step "asking the client to fetch the ARM64 Proton"
set -- "$@" steam://install/4427310
@@ -746,10 +848,13 @@ BUSCONF
fi
# The UI bridge launches SteamVR, which needs the guest agent's library environment.
_bl_compat_env=(env -u LD_LIBRARY_PATH -u STEAM_RUNTIME)
_bl_compat_env=(env -u LD_LIBRARY_PATH -u STEAM_RUNTIME -u STEAM_STEAMRT_RECURSION_GUARD)
[ "${LD_LIBRARY_PATH+x}" ] && _bl_compat_env+=("LD_LIBRARY_PATH=$LD_LIBRARY_PATH")
[ "${STEAM_RUNTIME+x}" ] && _bl_compat_env+=("STEAM_RUNTIME=$STEAM_RUNTIME")
[ "${STEAM_STEAMRT_RECURSION_GUARD+x}" ] && _bl_compat_env+=("STEAM_STEAMRT_RECURSION_GUARD=$STEAM_STEAMRT_RECURSION_GUARD")
export STEAM_RUNTIME=1
# A containerized client re-executes itself through pressure-vessel-wrap (bwrap) unless told it is already inside.
export STEAM_STEAMRT_RECURSION_GUARD=1
export LD_LIBRARY_PATH=$steam_root/steamrtarm64:$steam_root/steamrtarm64/panorama${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
export SDL_VIDEO_X11_DGAMOUSE=0
decky_loader=$HOME/homebrew/services/PluginLoader
@@ -799,7 +904,11 @@ BUSCONF
echo "== steam: devtools port $BL_CDP_PORT, guest processes only"
fi
[ -n "${BL_LAUNCH_DIR:-}" ] && mkdir -p "$BL_LAUNCH_DIR/agent" && echo "$BL_CDP_PORT" > "$BL_LAUNCH_DIR/agent/cdp-port"
[ "$steam_x64" = 1 ] || "${_bl_compat_env[@]}" droiddeck-steam-compat "$steam_root" 2>&1 &
if [ "$steam_x64" = 1 ]; then
{ droiddeck-steam-compat --files-only "$steam_root" 2>&1 & }
else
{ "${_bl_compat_env[@]}" droiddeck-steam-compat "$steam_root" 2>&1 & }
fi
_bl_ui=(-gamepadui)
[ "${BL_STEAM_UI:-}" = desktop ] && _bl_ui=()
_bl_cef=(-cef-force-gpu -cef-use-gl=angle -cef-use-angle=vulkan)
@@ -829,6 +938,9 @@ BUSCONF
unset _stale _f
fi
steam_channel=steamdeck_publicbeta
if [ "$steam_x64" = 0 ]; then
steam_channel=$(droiddeck-steam-install --channel 2>/dev/null || echo steamdeck_publicbeta)
fi
_bl_lang=()
[ -n "${BL_STEAM_LANGUAGE:-}" ] && _bl_lang=(-language "$BL_STEAM_LANGUAGE")
steam_beta=$steam_root/package/beta
@@ -196,6 +196,51 @@ TOUR_JS = r"""
return "off";
})()
"""
FILE_BINDING = "droiddeckOpenFileDialog"
FILE_JS = r"""
(() => {
const cancelled = () => ({ result: 2, message: "" });
if (window.__droiddeckFiles) {
window.__droiddeckFiles.cancelAll();
return "present";
}
const self = { next: 0, pending: new Map() };
const open = function (options) {
return new Promise((resolve, reject) => {
if (typeof window.__BINDING__ !== "function") { reject(cancelled()); return; }
const id = ++self.next;
self.pending.set(id, { resolve, reject });
try { window.__BINDING__(JSON.stringify({ id, options: options || {} })); }
catch (e) { self.pending.delete(id); reject(cancelled()); }
});
};
self.done = (id, path) => {
const item = self.pending.get(id);
if (!item) return false;
self.pending.delete(id);
if (typeof path === "string" && path) item.resolve(path); else item.reject(cancelled());
return true;
};
self.cancelAll = () => {
const items = Array.from(self.pending.values());
self.pending.clear();
for (const item of items) item.reject(cancelled());
};
const patch = (w) => {
const system = w?.SteamClient?.System;
if (system && system.OpenFileDialog !== open) system.OpenFileDialog = open;
};
patch(window);
if (SteamClient.System.OpenFileDialog !== open) throw new Error("Steam OpenFileDialog could not be redirected");
const popups = window.g_PopupManager;
if (popups) {
for (const popup of popups.GetPopups?.() || []) patch(popup.window);
popups.AddPopupCreatedCallback?.((popup) => patch(popup.window));
}
window.__droiddeckFiles = self;
return "installed";
})()
""".replace("__BINDING__", FILE_BINDING)
VR_BINDING = "droiddeckLaunchSteamVr"
VR_HELPER = "/usr/local/bin/droiddeck-steamvr"
VR_JS = r"""
@@ -274,6 +319,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
@@ -519,10 +620,11 @@ class Watcher:
class Helper:
def __init__(self, steam_root, port, steamos=False):
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
@@ -530,6 +632,70 @@ 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"]
# Gamescope shows the dialog over the client by the AppId on a reaper above it; the x86-64 client has no
# native reaper, so the arm64 client's is borrowed when it is installed.
data = os.environ.get("XDG_DATA_HOME") or os.path.join(os.path.expanduser("~"), ".local", "share")
for root in (self.steam_root, os.path.join(data, "Steam")):
reaper = os.path.join(root, "steamrtarm64", "reaper")
if os.access(reaper, os.X_OK):
command = [reaper, "SteamLaunch", "AppId=769", "--"] + command
break
try:
proc = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL, start_new_session=True)
proc.stdin.write(json.dumps(options).encode("utf-8"))
proc.stdin.close()
except OSError as e:
say("could not open a file dialog (%s)" % e)
self.finish_chooser(ident, None)
return
say("file dialog open: %s" % (options.get("strTitle") or "untitled"))
self.chooser = (ident, proc)
def chooser_done(self):
ident, proc = self.chooser
self.chooser = None
out = proc.stdout.read().decode("utf-8", "replace")
proc.stdout.close()
proc.wait()
if proc.returncode == 2:
say("file dialog needs GTK 3 and PyGObject, which come with the desktop")
try:
path = json.loads(out) if proc.returncode == 0 else None
except ValueError:
path = None
self.finish_chooser(ident, path if isinstance(path, str) and path else None)
def finish_chooser(self, ident, path):
say("file dialog %s" % ("chose " + path if path else "cancelled"))
self.page.evaluate("window.__droiddeckFiles?.done(%d, %s)" % (ident, json.dumps(path)))
def close_chooser(self):
if self.chooser is not None:
_, proc = self.chooser
self.chooser = None
try:
os.killpg(proc.pid, signal.SIGTERM)
except OSError:
pass
proc.stdout.close()
proc.wait()
def attach(self):
path = shared_context(self.port)
@@ -539,9 +705,16 @@ class Helper:
try:
if page.evaluate("typeof SteamClient === 'object' && typeof SteamClient.WebUITransport === 'object'") is not True:
raise LookupError("SteamClient is not ready")
if self.files_only:
page.call("Runtime.addBinding", {"name": FILE_BINDING})
page.evaluate(FILE_JS)
self.page = page
return True
page.call("Runtime.addBinding", {"name": BINDING})
page.call("Runtime.addBinding", {"name": VR_BINDING})
page.evaluate(VR_JS)
page.call("Runtime.addBinding", {"name": FILE_BINDING})
page.evaluate(FILE_JS)
try:
page.evaluate(TOUR_JS)
except RuntimeError:
@@ -583,9 +756,8 @@ class Helper:
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"])
overrides = COMPAT["requested_games"](
COMPAT["game_requests"](), catalog, [t["name"] for t in live])
apps = sorted(set(apps) | set(overrides), 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
@@ -597,7 +769,6 @@ class Helper:
if isinstance(state.get("auto"), dict):
state["auto"] = prior_auto
native_choices -= refused
overrides = {app: tool for app, tool in overrides.items() if app not in refused}
current["0"] = top["selected"]
native = COMPAT["native_targets"](self.steam_root, apps)
raw_mapping = COMPAT["read_config_mapping"](
@@ -619,6 +790,9 @@ class Helper:
current[app] = ""
elif not current.get(app):
current.pop(app, None)
requests = COMPAT["settled_game_requests"](state, current, catalog)
overrides = COMPAT["requested_games"](requests or {}, catalog, [t["name"] for t in live])
overrides = {app: tool for app, tool in overrides.items() if app not in refused}
changes, auto = COMPAT["plan_mapping"](
current, installed, [t["name"] for t in live], default,
COMPAT["followers"](state, current), native, picks, overrides,
@@ -630,7 +804,8 @@ class Helper:
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)
picked=COMPAT["picked_after"](current, changes), tools=catalog, live=True,
games_applied=COMPAT["applied_game_requests"](requests, overrides, state))
state.pop("native", None)
if waiting:
state["pending"] = wanted
@@ -678,9 +853,27 @@ 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_files(self):
say("attached to the client on port %d for its file dialogs" % self.port)
while True:
sources = [self.page.ws] + ([self.chooser[1].stdout] if self.chooser is not None else [])
queued = self.page.ws.pending() or bool(self.page.events)
ready = [self.page.ws] if queued else select.select(sources, [], [], 30)[0]
if self.chooser is not None and self.chooser[1].stdout in ready:
self.chooser_done()
if self.page.ws in ready or self.page.events:
for e in self.page.drain():
if called(e, FILE_BINDING) and isinstance(e.get("params", {}).get("payload"), str):
self.open_chooser(e["params"]["payload"])
def serve(self, watcher):
if self.files_only:
return self.serve_files()
with open(COMPAT["state_path"]("live.pid"), "w") as f:
f.write("%d\n" % os.getpid())
say("attached to the client on port %d" % self.port)
@@ -688,7 +881,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())
@@ -716,13 +913,45 @@ def detach():
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():
steam_root = sys.argv[1]
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")])
@@ -737,6 +966,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()
@@ -750,8 +980,11 @@ 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())
finally:
detach()
if sys.argv[1:2] != ["--files-only"]:
detach()
@@ -1,25 +1,57 @@
#!/bin/bash
set -eu
cdn_list="https://client-update.steamstatic.com https://client-update.akamai.steamstatic.com https://client-update.fastly.steamstatic.com"
steam_channel=steamdeck_publicbeta
manifest=steam_client_${steam_channel}_linuxarm64
beta_channel=steamdeck_publicbeta
stable_channel=steamdeck_stable
steam_root=${XDG_DATA_HOME:-$HOME/.local/share}/Steam
channel_state=$steam_root/package/droiddeck-channel
pinned=1
wanted=${BL_STEAM_CHANNEL:-$(cat "$HOME/.config/droiddeck/steam-channel" 2>/dev/null || true)}
case $wanted in
stable|"$stable_channel") steam_channel=$stable_channel ;;
beta|"$beta_channel") steam_channel=$beta_channel ;;
*)
pinned=
steam_channel=$beta_channel
[ "$(cat "$channel_state" 2>/dev/null)" = "$stable_channel" ] && steam_channel=$stable_channel
;;
esac
if [ "${1:-}" = --channel ]; then
echo "$steam_channel"
exit 0
fi
manifest=steam_client_${steam_channel}_linuxarm64
stamp=$steam_root/package/droiddeck-installed
legacy_stamp=$steam_root/package/bannerlator-installed
unconfirmed=$steam_root/package/droiddeck-unconfirmed
repair_request=$HOME/.bl-steam-repair
parts=$steam_root/package/droiddeck-download
if [ -f "$legacy_stamp" ] && [ ! -f "$stamp" ]; then
mv -f "$legacy_stamp" "$stamp" || true
fi
repair=""
fallback=""
starts=0
[ -f "$unconfirmed" ] && starts=$(grep -cx start "$unconfirmed" || true)
if [ -e "$repair_request" ]; then
repair="requested"
elif [ -f "$unconfirmed" ] && ! grep -qx repaired "$unconfirmed" \
&& [ "$(grep -cx start "$unconfirmed")" -ge 2 ]; then
repair="the client was started $(grep -cx start "$unconfirmed") times without showing anything"
elif [ "$starts" -ge 2 ] && ! grep -qx repaired "$unconfirmed"; then
repair="the client was started $starts times without showing anything"
elif [ "$starts" -ge 2 ] && [ -z "$pinned" ] && [ "$steam_channel" = "$beta_channel" ] && [ "${1:-}" != --sync ]; then
repair="the repaired $beta_channel client was started $starts more times without showing anything"
fallback=1
fi
if [ -n "$fallback" ]; then
echo "== STEP $(date -u +%H:%M:%S) falling back to Valve's $stable_channel Steam client: $repair"
steam_channel=$stable_channel
manifest=steam_client_${steam_channel}_linuxarm64
fi
if [ -n "$repair" ]; then
echo "== STEP $(date -u +%H:%M:%S) repairing the Steam client ($repair)"
if [ ! -f "$parts/.repair" ]; then
rm -rf "$parts"
mkdir -p "$parts" && : > "$parts/.repair"
fi
rm -f "$stamp" "$legacy_stamp" "$steam_root/.crash" "$steam_root/steam.pid" \
"$steam_root"/package/steam_client_*.manifest "$steam_root"/package/steam_client_*.installed
for part in "$steam_root"/package/*.zip.*; do
@@ -42,20 +74,99 @@ fi
work=$(mktemp -d)
trap 'rm -rf "$work"' EXIT
cdn=""
for candidate in $cdn_list; do
if curl -fsSL --connect-timeout 10 --max-time 30 --retry 2 --retry-connrefused --retry-delay 3 \
"$candidate/$manifest" -o "$work/manifest"; then
cdn=$candidate
break
nap() {
[ -n "${nap_fd:-}" ] || exec {nap_fd}<> <(:)
read -r -t "$1" -u "$nap_fd" _ 2>/dev/null || :
}
size_of() {
local n=0
[ -f "$1" ] && n=$(wc -c < "$1")
echo $((n))
}
limits=(--connect-timeout 30 --speed-limit 4096 --speed-time 60)
read -r -a delays <<< "${BL_STEAM_RETRY_DELAYS:-5 10 20 30 30}"
step() { echo "== STEP $(date -u +%H:%M:%S) $*"; }
progress() {
local mb=$(( (done_bytes + $(size_of "$1")) / 1000000 ))
[ -z "${3:-}" ] && [ "$mb" = "${shown_mb:-}" ] && [ "$2" = "${shown_part:-}" ] && return 0
shown_mb=$mb shown_part=$2
step "downloading Steam: $2 ($i/$total), $mb of $((total_bytes / 1000000)) MB${3:+ - $3}"
}
fetch() {
local url=$1 dest=$2 size=$3 sum=$4 part=$5 failures=0 bad=0 finished=0 before rc fd wait
while :; do
if [ -f "$dest" ] && { [ "$finished" = 1 ] || { [ "$size" -gt 0 ] && [ "$(size_of "$dest")" -ge "$size" ]; }; }; then
echo "$sum $dest" | sha256sum -c --quiet 2>/dev/null && return 0
rm -f "$dest"
finished=0 bad=$((bad + 1))
[ "$bad" -lt 3 ] || { echo "droiddeck-steam-install: $part does not match its checksum" >&2; return 1; }
fi
before=$(size_of "$dest")
exec {fd}< <(code=$(curl -fsSL "${limits[@]}" -w '%{http_code}' -C - -o "$dest" "$url") && echo "0 $code" || echo "$? $code")
until read -r -t 5 -u "$fd" rc http; do
if [ $? -le 128 ]; then rc=1; break; fi
progress "$dest" "$part"
done
exec {fd}<&-
if [ "$rc" = 0 ]; then finished=1; continue; fi
case "$rc:${http:-}" in
22:416 | 33:*) rm -f "$dest" ;;
22:4??)
echo "droiddeck-steam-install: $part: HTTP $http from ${url%/*}" >&2
return 1
;;
esac
if [ "$(size_of "$dest")" -gt "$before" ]; then failures=0; else failures=$((failures + 1)); fi
if [ "$failures" -gt "${#delays[@]}" ]; then
echo "droiddeck-steam-install: could not download $part (curl exit $rc); what arrived is kept for the next try" >&2
return 1
fi
wait=${delays[$((failures > 0 ? failures - 1 : 0))]}
progress "$dest" "$part" "interrupted (curl exit $rc), trying again in ${wait} s"
nap "$wait"
done
}
fetch_manifest() {
cdn=""
for candidate in $cdn_list; do
if curl -fsSL --connect-timeout 10 --max-time 30 --retry 2 --retry-connrefused --retry-delay 3 \
"$candidate/$manifest" -o "$work/manifest"; then
cdn=$candidate
break
fi
done
[ -n "$cdn" ] || { echo "droiddeck-steam-install: no Steam client CDN answered for $manifest" >&2; exit 1; }
cdn_order=("$cdn")
for candidate in $cdn_list; do
[ "$candidate" = "$cdn" ] || cdn_order+=("$candidate")
done
version=$(awk -F'"' '$2 == "version" { print $4; exit }' "$work/manifest")
awk -F'"' '
/^\t"/ && NF >= 3 && $4 == "" { block = $2 }
$2 == "file" { file[block] = $4 }
$2 == "sha2" { sha[block] = $4 }
$2 == "size" { size[block] = $4 }
END { for (b in file) if (b ~ /_all$/ || b ~ /_linuxarm64_linuxarm64$/) print b, file[b], sha[b], size[b] + 0 }
' "$work/manifest" | sort > "$work/parts"
[ -s "$work/parts" ] && return 0
echo "droiddeck-steam-install: no client components in $manifest; its blocks are:" \
"$(awk -F'"' '/^\t"/ && NF >= 3 && $4 == "" { printf "%s ", $2 }' "$work/manifest")" >&2
return 1
}
if ! fetch_manifest; then
if [ -n "$sync" ] || [ -n "$pinned" ] || [ "$steam_channel" = "$stable_channel" ]; then
exit 1
fi
done
[ -n "$cdn" ] || { echo "droiddeck-steam-install: no Steam client CDN answered for $manifest" >&2; exit 1; }
cdn_order=("$cdn")
for candidate in $cdn_list; do
[ "$candidate" = "$cdn" ] || cdn_order+=("$candidate")
done
version=$(awk -F'"' '$2 == "version" { print $4; exit }' "$work/manifest")
echo "== STEP $(date -u +%H:%M:%S) falling back to Valve's $stable_channel Steam client: $manifest has no arm64 client"
steam_channel=$stable_channel
manifest=steam_client_${steam_channel}_linuxarm64
fallback=1
fetch_manifest || exit 1
fi
if [ -n "$sync" ]; then
installed=$(awk -F'"' '$2 == "version" { print $4; exit }' "$steam_root/package/$manifest.manifest" 2>/dev/null || true)
if [ -x "$steam_root/steamrtarm64/steam" ] && [ "$installed" = "$version" ]; then
@@ -64,31 +175,36 @@ if [ -n "$sync" ]; then
fi
echo "== STEP $(date -u +%H:%M:%S) updating the arm64 Steam client to $version (was ${installed:-none}) for the x86-64 client"
fi
awk -F'"' '
/^\t"/ && NF >= 3 && $4 == "" { block = $2 }
$2 == "file" { file[block] = $4 }
$2 == "sha2" { sha[block] = $4 }
END { for (b in file) if (b ~ /_all$/ || b ~ /_linuxarm64_linuxarm64$/) print b, file[b], sha[b] }
' "$work/manifest" | sort > "$work/parts"
[ -s "$work/parts" ] || { echo "droiddeck-steam-install: no client components in $manifest" >&2; exit 1; }
total=$(grep -c . "$work/parts" 2>/dev/null || echo 0)
total_bytes=$(awk '{ n += $4 } END { print n + 0 }' "$work/parts")
mkdir -p "$steam_root/package"
while read -r block file sum; do
mkdir -p "$parts"
for left in "$parts"/*; do
[ -e "$left" ] || continue
name=${left##*/}
name=${name%.unpacked}
grep -qF " $name " "$work/parts" || rm -rf "$left"
done
done_bytes=0
while read -r block file sum size; do
i=$((${i:-0} + 1))
echo "== STEP $(date -u +%H:%M:%S) downloading Steam: $block ($i/$total)"
if [ -f "$parts/$file.unpacked" ]; then
done_bytes=$((done_bytes + size))
continue
fi
progress "$parts/$file" "$block"
fetched=""
for host in "${cdn_order[@]}"; do
if curl -fsSL --connect-timeout 10 --speed-limit 1024 --speed-time 60 --max-time 1800 \
"$host/$file" -o "$work/$file" && echo "$sum $work/$file" | sha256sum -c --quiet; then
if fetch "$host/$file" "$parts/$file" "$size" "$sum" "$block"; then
fetched=$host
break
fi
rm -f "$work/$file"
done
[ -n "$fetched" ] || { echo "droiddeck-steam-install: $file failed on every CDN" >&2; exit 1; }
unzip -qo "$work/$file" -d "$steam_root"
rm -f "$work/$file"
unzip -qo "$parts/$file" -d "$steam_root"
: > "$parts/$file.unpacked"
rm -f "$parts/$file"
done_bytes=$((done_bytes + size))
done < "$work/parts"
find "$steam_root" -depth -name '*\\*' | while read -r odd; do
fixed=${odd//\\//}
@@ -117,6 +233,11 @@ ln -sfn "$steam_root/linuxarm64" "$HOME/.steam/sdkarm64"
ln -sfn "$steam_root/steamrtarm64" "$HOME/.steam/binarm64"
ln -sfn "$steam_root/linux32" "$HOME/.steam/sdk32"
echo "$version" > "$stamp"
rm -rf "$parts"
if [ -n "$fallback" ]; then
echo "$stable_channel" > "$channel_state"
echo "== STEP $(date -u +%H:%M:%S) the Steam client now follows $stable_channel (beta in ~/.config/droiddeck/steam-channel returns to $beta_channel)"
fi
if [ -n "$repair" ]; then
echo repaired > "$unconfirmed"
rm -f "$repair_request"
@@ -12,6 +12,10 @@ case $target in
esac
if [ "$ui" = desktop ] && [ "${BL_DESKTOP:-}" = 1 ]; then
case ",${MESA_VK_WSI_DEBUG:-}," in
*,sw,*) ;;
*) export MESA_VK_WSI_DEBUG=${MESA_VK_WSI_DEBUG:+$MESA_VK_WSI_DEBUG,}sw ;;
esac
if [ "${BL_STEAM_CLIENT_ARCH:-}" = x86_64 ]; then
client_home=${BL_X64_HOME:-${HOME:-/root}/.droiddeck-x64}
client=(/usr/local/bin/droiddeck-steam-x64 --exec)
@@ -0,0 +1,61 @@
#!/usr/bin/python3
"""Sets the Steam client's shader pre-caching (Settings > Downloads), before the client starts.
droiddeck-steam-shaders <0|1> <steam root>...
The client stores the toggle as InstallConfigStore/Software/Valve/Steam/ShaderCacheManager/
DisableShaderCache in config/config.vdf: "1" when off, "0" when on. It keeps the setting in memory
and writes config.vdf back on exit, so this runs only while the client is stopped. The launcher
passes a value only after its own toggle changes; otherwise the client's toggle stays the user's."""
from pathlib import Path
import runpy
import sys
VDF = runpy.run_path(str(Path(__file__).with_name("droiddeck-steam-ui-scale")))
PATH = ("Software", "Valve", "Steam", "ShaderCacheManager", "DisableShaderCache")
def set_text(text, enabled):
value = "0" if enabled else "1"
updated, changed = VDF["seed_value"](text, PATH, value)
if changed:
return updated, True
block = VDF["parse"](text).get("InstallConfigStore")
for section in PATH[:-1]:
block = block.get(section)
current = block.get(PATH[-1])
if isinstance(current, VDF["Block"]):
raise ValueError("invalid DisableShaderCache setting")
if current == value:
return text, False
at = block.offset(PATH[-1])
end = VDF["TOKEN"].match(text, at).end()
return text[:at] + f'"{value}"' + text[end:], True
def apply(steam_root, enabled):
config = Path(steam_root) / "config/config.vdf"
text = config.read_text() if config.exists() else '"InstallConfigStore"\n{\n}\n'
updated, changed = set_text(text, enabled)
if changed:
VDF["atomic_write"](config, updated)
return changed
if __name__ == "__main__":
if len(sys.argv) < 3 or sys.argv[1] not in ("0", "1"):
print("usage: droiddeck-steam-shaders <0|1> <steam root>...", file=sys.stderr)
sys.exit(2)
enabled = sys.argv[1] == "1"
status = 0
for root in sys.argv[2:]:
if not Path(root).is_dir():
continue
try:
if apply(root, enabled):
print(f"== steam: shader pre-caching {'on' if enabled else 'off'} in {root}")
except (OSError, ValueError, RecursionError) as error:
print(f"== steam: left shader pre-caching unchanged in {root}: {error}", file=sys.stderr)
status = 1
sys.exit(status)
+273
View File
@@ -0,0 +1,273 @@
#!/usr/bin/python3
import json
import os
import re
import runpy
import sys
NOT_GAMES = frozenset((
"228980", "1070560", "1391110", "1628350", "1493710", "3127680",
"4183110", "4185400", "4427310", "4628740",
))
TOOL_DIR = re.compile(r"(steam ?linux ?runtime.*|proton.*|steamworks shared|fex|steamvr)", re.I)
AUTOMATIC = ("", "droiddeck-direct", "droiddeck-fex", "droiddeck-proton-arm64", "droiddeck-proton-11-arm64")
LINUX_TOOLS = ("droiddeck-direct", "droiddeck-fex")
STATE = ".droiddeck-shared.json"
LABELS = {"arm64": "ARM64 client games", "x86-64": "x86-64 client games"}
def say(message):
print("droiddeck-steam-share: " + message)
def read(path):
try:
with open(path, encoding="utf-8", errors="surrogateescape") as f:
return f.read()
except OSError:
return None
def write(path, text):
staged = path + ".droiddeck-new"
with open(staged, "w", encoding="utf-8", errors="surrogateescape") as f:
f.write(text)
os.replace(staged, path)
def field(text, key):
m = re.search(r'"%s"\s+"([^"]*)"' % re.escape(key), text or "", re.I)
return m.group(1) if m else None
def number(text, key):
value = field(text, key)
return int(value) if value and value.isdigit() else 0
def same(a, b):
return os.path.realpath(a) == os.path.realpath(b)
def libraries(root, skip):
found = [root] + [p.replace("\\\\", "\\") for p in
re.findall(r'"path"\s+"((?:[^"\\]|\\.)*)"', read(os.path.join(root, "steamapps", "libraryfolders.vdf")) or "")]
return [p for p in dict.fromkeys(found) if not any(same(p, s) for s in skip)]
def installs(libs):
games = {}
for library in libs:
steamapps = os.path.join(library, "steamapps")
try:
names = sorted(os.listdir(steamapps))
except OSError:
continue
for name in names:
m = re.fullmatch(r"appmanifest_(\d+)\.acf", name)
if not m or m.group(1) in NOT_GAMES or m.group(1) in games:
continue
text = read(os.path.join(steamapps, name))
folder = field(text, "installdir")
if (not folder or folder in (".", "..") or "/" in folder or TOOL_DIR.fullmatch(folder)
or not number(text, "StateFlags") & 4
or not os.path.isdir(os.path.join(steamapps, "common", folder))):
continue
games[m.group(1)] = (steamapps, text, folder)
return games
def link(path, target):
if os.path.islink(path):
if os.readlink(path) == target:
return True
os.unlink(path)
elif os.path.lexists(path):
return False
os.symlink(target, path)
return True
def unlink(path):
if os.path.islink(path):
os.unlink(path)
def emptied(src, folder):
path = os.path.join(src, "common", folder)
return not os.path.isdir(path) or not os.listdir(path)
def sync(view, source, own):
steamapps = os.path.join(view, "steamapps")
for sub in ("common", "compatdata"):
os.makedirs(os.path.join(steamapps, sub), exist_ok=True)
state = {}
try:
state = json.loads(read(os.path.join(view, STATE)) or "{}")
except ValueError:
pass
managed = {app: tuple(v) for app, v in state.get("shared", {}).items() if isinstance(v, list) and len(v) == 2}
hidden = state.get("hidden", {})
shared, back = {}, []
for app, (src, folder) in managed.items():
if app in source and app not in own:
continue
manifest = os.path.join(steamapps, "appmanifest_%s.acf" % app)
if app not in own and not os.path.isfile(manifest) and emptied(src, folder) and os.path.isfile(os.path.join(src, "appmanifest_%s.acf" % app)):
os.unlink(os.path.join(src, "appmanifest_%s.acf" % app))
unlink(os.path.join(steamapps, "common", folder))
unlink(os.path.join(steamapps, "compatdata", app))
if os.path.isfile(manifest):
os.unlink(manifest)
for app, (src, text, folder) in sorted(source.items(), key=lambda item: int(item[0])):
if app in own:
continue
stamp = number(text, "LastUpdated")
if hidden.get(app) == stamp:
continue
hidden.pop(app, None)
manifest = os.path.join(steamapps, "appmanifest_%s.acf" % app)
mine = read(manifest)
if app in managed and mine is None:
unlink(os.path.join(steamapps, "common", folder))
unlink(os.path.join(steamapps, "compatdata", app))
if emptied(src, folder):
os.unlink(os.path.join(src, "appmanifest_%s.acf" % app))
else:
hidden[app] = stamp
continue
if not link(os.path.join(steamapps, "common", folder), os.path.join(src, "common", folder)):
continue
if os.path.isdir(os.path.join(src, "compatdata", app)):
link(os.path.join(steamapps, "compatdata", app), os.path.join(src, "compatdata", app))
if mine is not None and number(mine, "LastUpdated") > stamp:
write(os.path.join(src, "appmanifest_%s.acf" % app), mine)
back.append(app)
elif mine != text:
write(manifest, text)
shared[app] = [src, folder]
state = {"shared": shared, "hidden": hidden}
write(os.path.join(view, STATE), json.dumps(state, indent=1, sort_keys=True) + "\n")
return shared, back
def tools(root):
names = set()
folder = os.path.join(root, "compatibilitytools.d")
try:
entries = os.listdir(folder)
except OSError:
return names
for entry in entries:
text = read(os.path.join(folder, entry, "compatibilitytool.vdf")) or ""
m = re.search(r'"compat_tools"\s*\{\s*"([^"]+)"', text)
if m:
names.add(m.group(1))
return names
def mapping_block(text):
m = re.search(r'"CompatToolMapping"\s*\{', text)
if not m:
return None
depth, k = 1, m.end()
while depth and k < len(text):
depth += {"{": 1, "}": -1}.get(text[k], 0)
k += 1
return m.end(), k - 1
def entries(text):
span = mapping_block(text)
found = {}
if not span:
return found
inner = text[span[0]:span[1]]
for m in re.finditer(r'"(\d+)"\s*\{([^{}]*)\}', inner):
name = re.search(r'"name"\s*"([^"]*)"', m.group(2))
found.setdefault(m.group(1), name.group(1) if name else "")
return found
def set_tools(config, wanted):
text = read(config)
if text is None or not wanted:
return []
span = mapping_block(text)
if not span:
return []
inner = text[span[0]:span[1]]
changed = []
for app, tool in sorted(wanted.items(), key=lambda item: int(item[0])):
m = re.search(r'"%s"\s*\{([^{}]*)\}' % app, inner)
if m:
name = re.search(r'"name"\s*"([^"]*)"', m.group(1))
name = name.group(1) if name else ""
if name == tool or not (canonical(name) in map(canonical, AUTOMATIC) or name.startswith("proton_")):
continue
if name and (name in LINUX_TOOLS) == (tool in LINUX_TOOLS):
continue
inner = inner[:m.start()] + inner[m.end():]
inner = ('\n\t\t\t\t\t"%s"\n\t\t\t\t\t{\n\t\t\t\t\t\t"name"\t\t"%s"\n\t\t\t\t\t\t"config"\t\t""\n'
'\t\t\t\t\t\t"priority"\t\t"250"\n\t\t\t\t\t}' % (app, tool)) + inner
changed.append(app)
if changed:
write(config, text[:span[0]] + inner + text[span[1]:])
return changed
def canonical(name):
return re.sub(r"arm64|aarch64", "a64", name)
def translate(name, here, available):
if name == "droiddeck-fex" and here == "x86-64":
return "droiddeck-direct"
if name == "droiddeck-direct" and here == "arm64":
return "droiddeck-fex"
return {canonical(a): a for a in available}.get(canonical(name))
def library_helper():
sibling = os.path.join(os.path.dirname(os.path.realpath(__file__)), "droiddeck-steam-library")
return runpy.run_path(sibling if os.path.isfile(sibling) else "/usr/local/bin/droiddeck-steam-library")
def main(argv):
if len(argv) != 5 or argv[0] not in LABELS:
say("usage: droiddeck-steam-share arm64|x86-64 ARM_STEAM X64_STEAM X64_CARD SHARED_DIR")
return 2
here, arm, x64, x64_card, shared_dir = argv
views = {name: os.path.join(shared_dir, name) for name in LABELS}
roots = {"arm64": arm, "x86-64": x64}
other = "x86-64" if here == "arm64" else "arm64"
skip = list(views.values()) + [x64, x64_card]
arm_libs = libraries(arm, skip)
x64_libs = [x64] + [p for p in libraries(x64, list(views.values()) + [arm]) if not same(p, x64)]
games = {"arm64": installs(arm_libs), "x86-64": installs(x64_libs)}
view = views[other]
shared, back = sync(view, games[other], games[here])
helper = library_helper()
if not any(re.fullmatch(r"appmanifest_\d+\.acf", n) for n in os.listdir(os.path.join(view, "steamapps"))):
helper["forget"](roots[here], view)
return 0
helper["register"](roots[here], view, LABELS[other])
helper["relabel"](roots[here], view, LABELS[other])
picks = entries(read(os.path.join(roots[other], "config", "config.vdf")) or "")
available = tools(roots[here]) | {"droiddeck-direct" if here == "x86-64" else "droiddeck-fex"}
wanted = {}
for app in shared:
tool = translate(picks.get(app) or picks.get("0", ""), here, available)
if tool:
wanted[app] = tool
changed = set_tools(os.path.join(roots[here], "config", "config.vdf"), wanted)
say("%d game(s) from the %s client%s%s" % (
len(shared), other,
"; tools set for %s" % " ".join(changed) if changed else "",
"; manifests sent back for %s" % " ".join(back) if back else ""))
return 0
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))
@@ -18,13 +18,17 @@ TAG = "droiddeck-app"
def account_dir(steam_root):
"""The most recent account's userdata folder, else the only one that is not 0."""
users = os.path.join(steam_root, "config", "loginusers.vdf")
recent = None
recent, best = None, None
try:
with open(users, encoding="utf-8", errors="replace") as f:
text = f.read()
for block in re.finditer(r'"(\d{5,})"\s*\{([^}]*)\}', text):
if re.search(r'"MostRecent"\s*"1"', block.group(2)):
recent = int(block.group(1))
stamp = re.search(r'"timestamp"\s*"(\d+)"', block.group(2))
rank = (bool(re.search(r'"MostRecent"\s*"1"', block.group(2))),
bool(re.search(r'"AutoLogin"\s*"1"', block.group(2))),
int(stamp.group(1)) if stamp else 0)
if any(rank) and (best is None or rank >= best):
recent, best = int(block.group(1)), rank
except OSError:
pass
userdata = os.path.join(steam_root, "userdata")
@@ -26,9 +26,10 @@ TOKEN = re.compile(r'\s+|//[^\n]*|"(?:\\.|[^"\\])*"|[{}]')
class Block:
def __init__(self, pairs, end):
def __init__(self, pairs, end, offsets=()):
self.pairs = pairs
self.end = end
self.offsets = offsets
def get(self, name):
matches = [value for key, value in self.pairs if key.lower() == name.lower()]
@@ -36,6 +37,9 @@ class Block:
raise ValueError("duplicate UI setting")
return matches[0] if matches else None
def offset(self, name):
return next(at for (key, _), at in zip(self.pairs, self.offsets) if key.lower() == name.lower())
def parse(text):
tokens = []
@@ -51,17 +55,17 @@ def parse(text):
raise ValueError("invalid config.vdf")
def block(index, nested=False):
pairs = []
pairs, offsets = [], []
while index < len(tokens):
token, offset = tokens[index]
if token == '}':
if not nested:
raise ValueError("unexpected closing brace")
return Block(pairs, offset), index + 1
return Block(pairs, offset, offsets), index + 1
if not token.startswith('"') or index + 1 == len(tokens):
raise ValueError("missing config value")
key = token[1:-1]
value = tokens[index + 1][0]
value, at = tokens[index + 1]
index += 2
if value == '{':
value, index = block(index, True)
@@ -70,9 +74,10 @@ def parse(text):
else:
raise ValueError("invalid config value")
pairs.append((key, value))
offsets.append(at)
if nested:
raise ValueError("unclosed config block")
return Block(pairs, len(text)), index
return Block(pairs, len(text), offsets), index
return block(0)[0]
@@ -87,7 +87,7 @@ x64_map_direct() {
[ -f "$STEAM_X64/config/config.vdf" ] || return 0
if [ "$mode" = write ] && pgrep -f 'steamrt64/steam( |$)' >/dev/null 2>&1; then return 0; fi
python3 - "$mode" "$STEAM_X64/config/config.vdf" "$STEAM_X64/appcache/appinfo.vdf" "$X64_HOME/direct-auto.json" \
"$X64_HOME/direct-sniper" "$STEAM_X64/steamapps:/mnt/droiddeck-sd/x86-64/steamapps" "$(x64_default_tool)" \
"$X64_HOME/direct-sniper" "$STEAM_X64/steamapps:/mnt/droiddeck-sd/x86-64/steamapps:$X64_HOME/shared/arm64/steamapps" "$(x64_default_tool)" \
"$REAL_HOME/.bl-compat-pending" ${BL_X64_DIRECT_APPS:-550} \
<<'PY' || { [ "$mode" = live ] || step "could not map titles to DroidDeck direct"; }
# Steam does not swap an installed build when the tool changes: a title installed for Windows stays on Proton.
@@ -520,39 +520,8 @@ ln -sfn "$STEAM_X64/ubuntu12_32" "$X64_HOME/.steam/bin32"
ln -sfn "$STEAM_X64/ubuntu12_64" "$X64_HOME/.steam/bin64"
ln -sfn "$STEAM_X64/linux32" "$X64_HOME/.steam/sdk32"
ln -sfn "$STEAM_X64/linux64" "$X64_HOME/.steam/sdk64"
# The client's bwrap probe (steam-runtime-check-requirements) needs user namespaces, which no Android app has.
srt_real=$STEAM_X64/steamrt64/pv-runtime/steam-runtime-steamrt
srt_shim=$X64_HOME/steamrt-shim
if [ -d "$srt_real" ]; then
rm -rf "$srt_shim"
mkdir -p "$srt_shim/bin"
for e in "$srt_real"/* "$srt_real"/.[!.]*; do
[ -e "$e" ] || continue
[ "$(basename "$e")" = bin ] || ln -s "$e" "$srt_shim/$(basename "$e")"
done
for e in "$srt_real"/bin/*; do ln -s "$e" "$srt_shim/bin/$(basename "$e")"; done
rm -f "$srt_shim/bin/steam-runtime-check-requirements"
printf '#!/bin/sh
exit 0
' > "$srt_shim/bin/steam-runtime-check-requirements"
chmod 755 "$srt_shim/bin/steam-runtime-check-requirements"
rm -f "$srt_shim/bin/steam-runtime-launch-client"
cat > "$srt_shim/bin/steam-runtime-launch-client" <<'LAUNCH'
#!/bin/sh
while [ $# -gt 0 ]; do
case $1 in
--) shift; break ;;
--directory=*) cd "${1#--directory=}" || exit 127 ;;
--env=*) export "${1#--env=}" ;;
--unset-env=*) unset "${1#--unset-env=}" ;;
esac
shift
done
[ $# -gt 0 ] || exit 0
exec "$@"
LAUNCH
chmod 755 "$srt_shim/bin/steam-runtime-launch-client"
fi
# Valve's Steam Runtime container needs user namespaces and a session-bus launcher; steamrt-shim stands in for both.
droiddeck-steamrt-shim "$STEAM_X64/steamrt64" "$X64_HOME/steamrt-shim" || rm -rf "$X64_HOME/steamrt-shim"
# Valve's platform libraries ship without soname links (pressure-vessel makes them); ldconfig adds them here.
srt_libs=$X64_HOME/steamrt-libs
rm -rf "$srt_libs"
@@ -641,6 +610,7 @@ RUN
printf '"InstallConfigStore"\n{\n\t"Software"\n\t{\n\t\t"Valve"\n\t\t{\n\t\t\t"Steam"\n\t\t\t{\n\t\t\t\t"CompatToolMapping"\n\t\t\t\t{\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n' \
> "$STEAM_X64/config/config.vdf"
fi
x64_share x86-64
x64_map_direct
# Each wrapper execs droiddeck-x64-arm-proton through an arm64 binary, which FEX hands straight to the CPU.
arm_tools=$ARM_STEAM/compatibilitytools.d
@@ -690,9 +660,9 @@ VDF
# Each client keeps its own libraries; written only while no x86 client runs, as it rewrites these on exit.
if ! pgrep -f 'steamrt64/steam( |$)' >/dev/null 2>&1; then
listing_ok=0
if python3 - "$STEAM_X64" "${BL_X64_STEAM_LIBRARIES:-}" "$STEAM_X64/config/libraryfolders.vdf" "$STEAM_X64/steamapps/libraryfolders.vdf" <<'PY'; then
if BL_X64_SHARED=$X64_HOME/shared/arm64 python3 - "$STEAM_X64" "${BL_X64_STEAM_LIBRARIES:-}" "$STEAM_X64/config/libraryfolders.vdf" "$STEAM_X64/steamapps/libraryfolders.vdf" <<'PY'; then
import json, os, re, sys
keep = {os.path.realpath(sys.argv[1])}
keep = {os.path.realpath(sys.argv[1]), os.path.realpath(os.environ["BL_X64_SHARED"])}
# Without the session's list nothing is known about the card: leave the folders as they are.
if not sys.argv[2]:
sys.exit(3)
@@ -942,6 +912,14 @@ x64_exec() {
exec "${BL_FEX:-/usr/local/bin/FEX}" "$STEAM_X64/steamrt64/steam" "$@"
}
x64_share() {
pgrep -f 'steamrt64/steam( |$)' >/dev/null 2>&1 && return 0
pgrep -f 'steamrtarm64/steam( |$)' >/dev/null 2>&1 && return 0
[ -d "$STEAM_X64/steamapps" ] && [ -d "$ARM_STEAM/steamapps" ] || return 0
droiddeck-steam-share "$1" "$ARM_STEAM" "$STEAM_X64" /mnt/droiddeck-sd/x86-64 "$X64_HOME/shared" ||
step "could not share the games between the clients"
}
# Steam renews its token every few days and refuses old copies: copy the newest every start, never while a client runs.
x64_sync_login() {
pgrep -f 'steamrt64/steam( |$)' >/dev/null 2>&1 && return 0
@@ -1058,6 +1036,7 @@ PY
case "${1:-}" in
--sync-login) x64_sync_login; exit 0 ;;
--share) x64_share arm64; exit 0 ;;
--prepare) x64_prepare; exit 0 ;;
--exec) shift; x64_exec "$@" ;;
--gpu) x64_gpu; exit $? ;;
+58
View File
@@ -0,0 +1,58 @@
#!/bin/bash
# droiddeck-steamrt-shim <client runtime dir> <shim dir>
# Builds <shim dir> from the Steam Runtime container the client carries and exits 0, or removes it and exits 1
# when the client carries none.
set -eu
client=$1
shim=$2
srt_real=$client/pv-runtime/steam-runtime-steamrt
if [ ! -d "$srt_real" ]; then
probe=$(find "$client" -maxdepth 4 -name steam-runtime-check-requirements -path '*/bin/*' -print -quit 2>/dev/null || true)
srt_real=${probe:+${probe%/bin/*}}
fi
if [ -z "$srt_real" ] || [ ! -d "$srt_real" ]; then
rm -rf "$shim"
exit 1
fi
rm -rf "$shim"
trap 'rm -rf "$shim"' ERR
mkdir -p "$shim/bin"
for e in "$srt_real"/* "$srt_real"/.[!.]*; do
[ -e "$e" ] || continue
[ "$(basename "$e")" = bin ] || ln -s "$e" "$shim/$(basename "$e")"
done
for e in "$srt_real"/bin/*; do
[ -e "$e" ] || continue
ln -s "$e" "$shim/bin/$(basename "$e")"
done
# The client's bwrap probe (steam-runtime-check-requirements) needs user namespaces, which no Android app has.
rm -f "$shim/bin/steam-runtime-check-requirements"
printf '#!/bin/sh
exit 0
' > "$shim/bin/steam-runtime-check-requirements"
chmod 755 "$shim/bin/steam-runtime-check-requirements"
# Valve's launcher asks a service on the session bus; proot is already the container, so the command runs here.
rm -f "$shim/bin/steam-runtime-launch-client"
cat > "$shim/bin/steam-runtime-launch-client" <<'LAUNCH'
#!/bin/sh
while [ $# -gt 0 ]; do
case $1 in
--) shift; break ;;
--directory=*) cd "${1#--directory=}" || exit 127 ;;
--env=*) export "${1#--env=}" ;;
--unset-env=*) unset "${1#--unset-env=}" ;;
--directory|--env|--unset-env)
[ $# -ge 2 ] || exit 127
case $1 in
--directory) cd "$2" || exit 127 ;;
--env) export "$2" ;;
--unset-env) unset "$2" ;;
esac
shift ;;
esac
shift
done
[ $# -gt 0 ] || exit 0
exec "$@"
LAUNCH
chmod 755 "$shim/bin/steam-runtime-launch-client"
@@ -25,6 +25,8 @@ Android: an EOS sign-in step that falls back to a browser (a consent Epic asks f
is finished there. An EOS overlay pointer (HKCU\Software\Epic Games\EOS "OverlayPath") an earlier
build wrote is removed - with it the game exited while loading.
Battle.net.exe in Battle.net's prefix is handed to droiddeck-battlenet, which runs the whole launch.
droiddeck-store-launch <the arguments Steam gave the compat tool>
"""
@@ -291,6 +293,27 @@ def create_prefix(compat_data, proton, timeout=120):
CLOUD_TIMEOUT = 15.0
CLOUD_STORES = ("gog", "epic")
STORE_GAME = 10
RAN = 11
RAN_FAILED = 12
BATTLENET = "/usr/local/bin/droiddeck-battlenet"
BATTLENET_EXE = re.compile(r"(?i)/compatdata/2596113170/pfx/drive_c/Program Files \(x86\)/Battle\.net/Battle\.net\.exe$")
BATTLENET_LAUNCH = re.compile(r'(?i)^--exec=\s*"?launch\s+([A-Za-z0-9_]{1,16})"?$')
def battlenet(argv, env=os.environ):
if env.get("BL_BATTLENET"):
return None
exe, code = False, None
for raw in argv:
arg = raw.strip().strip('"').strip("'")
if BATTLENET_EXE.search(arg.replace("\\", "/")):
exe = True
found = BATTLENET_LAUNCH.match(arg)
if found:
code = found.group(1)
if not exe:
return None
return [BATTLENET, "launch", code] if code else [BATTLENET, "open"]
def cloud_on(sidecar):
@@ -511,6 +534,10 @@ def main(argv):
pass
rest = rest[2:]
return exited(rest, code)
command = battlenet(argv)
if command is not None:
log("battlenet " + " ".join(command[1:]))
return RAN if subprocess.call(command) == 0 else RAN_FAILED
found = find_store_game(argv)
if found is None:
return 0
@@ -15,7 +15,18 @@ import threading
BIN = Path(__file__).resolve().parent
RUNTIME = Path('/usr/local/lib/droiddeck-xbox')
DLLS = ('xgameruntime.dll', 'winhttp.dll', 'windows.web.dll', 'twinapi.appcore.dll', 'libnghttp2.dll')
DLLS = ('xgameruntime.dll', 'winhttp.dll', 'windows.web.dll', 'twinapi.appcore.dll', 'wintypes.dll', 'libnghttp2.dll')
CLASSES = {
'twinapi.appcore.dll': ('Windows.ApplicationModel.DataTransfer.DataTransferManager',
'Windows.Security.ExchangeActiveSyncProvisioning.EasClientDeviceInformation',
'Windows.System.Profile.AnalyticsInfo', 'Windows.System.UserProfile.AdvertisingManager',
'Windows.UI.ViewManagement.ApplicationView'),
'windows.web.dll': ('Windows.Data.Json.JsonArray', 'Windows.Data.Json.JsonObject', 'Windows.Data.Json.JsonValue'),
'wintypes.dll': ('Windows.Foundation.PropertyValue', 'Windows.Foundation.Collections.PropertySet',
'Windows.Foundation.Metadata.ApiInformation', 'Windows.Storage.Streams.Buffer',
'Windows.Storage.Streams.DataWriter'),
}
CLASS_KEY = 'Software\\Microsoft\\WindowsRuntime\\ActivatableClassId\\'
def bridge(upstream):
@@ -102,6 +113,30 @@ def bridge(upstream):
return server.getsockname()[1], key.decode('ascii'), close
def register_classes(proton, prefix, env):
try:
registry = (prefix / 'system.reg').read_text(encoding='utf-8', errors='replace')
except OSError:
return
missing = [(name, dll) for dll, names in CLASSES.items() for name in names
if '[%s]' % (CLASS_KEY + name).replace('\\', '\\\\') not in registry]
if not missing:
return
text = 'Windows Registry Editor Version 5.00\r\n\r\n' + ''.join(
'[HKEY_LOCAL_MACHINE\\%s%s]\r\n"DllPath"="C:\\\\windows\\\\system32\\\\%s"\r\n\r\n' % (CLASS_KEY, name, dll)
for name, dll in missing)
with tempfile.TemporaryDirectory(prefix='droiddeck-xbox-') as temporary:
source = Path(temporary) / 'classes.reg'
source.write_bytes(b'\xff\xfe' + text.encode('utf-16-le'))
try:
failed = subprocess.run([proton, 'run', 'reg.exe', 'import', 'Z:' + str(source).replace('/', '\\')],
stdout=sys.stderr, env=env, timeout=90).returncode != 0
except subprocess.TimeoutExpired:
failed = True
if failed:
print('droiddeck-xbox: Windows Runtime class registration failed; launching without it', file=sys.stderr)
def wine(argv, source_env=None):
source_env = os.environ if source_env is None else source_env
with open(source_env['DROIDDECK_XBOX_COMMAND_FILE'], encoding='utf-8') as file:
@@ -109,7 +144,8 @@ def wine(argv, source_env=None):
if len(command) < 3 or command[1] != 'waitforexitandrun' or len(argv) != 1:
raise ValueError('Invalid Xbox Proton invocation')
env = dict(source_env)
env.pop('DROIDDECK_XBOX_CREDENTIAL_SOCKET', None)
for name in ('DROIDDECK_XBOX_CREDENTIAL_SOCKET', 'DROIDDECK_XBOX_CREDENTIAL_PORT', 'DROIDDECK_XBOX_CREDENTIAL_KEY'):
env.pop(name, None)
env.pop('DROIDDECK_XBOX_COMMAND_FILE', None)
prefix = Path(source_env['STEAM_COMPAT_DATA_PATH']) / 'pfx'
prefix.parent.mkdir(parents=True, exist_ok=True)
@@ -124,6 +160,7 @@ def wine(argv, source_env=None):
if target.is_symlink():
target.unlink()
shutil.copyfile(RUNTIME / 'windows' / name, target)
register_classes(command[0], prefix, env)
overrides = ';'.join(name[:-4] + '=n' for name in DLLS if name != 'libnghttp2.dll')
env['WINEDLLOVERRIDES'] = overrides + ';' + env.get('WINEDLLOVERRIDES', '')
return subprocess.call([*command[:2], argv[0], *command[3:]], env=env)
@@ -171,7 +208,8 @@ def launch(command, env):
owned_env = dict(env, DROIDDECK_XBOX_ACTIVE='1', DROIDDECK_XBOX_COMMAND_FILE=str(context),
XODUS_SOCKET=reply['socket'], XODUS_BRIDGE_PORT=str(port), XODUS_BRIDGE_KEY=key,
XODUS_LOG='off', RUST_LOG='off', XODUS_SAVE_SYNC='auto' if sidecar.get('cloud', True) else 'off')
owned_env.pop('DROIDDECK_XBOX_CREDENTIAL_SOCKET', None)
for name in ('DROIDDECK_XBOX_CREDENTIAL_SOCKET', 'DROIDDECK_XBOX_CREDENTIAL_PORT', 'DROIDDECK_XBOX_CREDENTIAL_KEY'):
owned_env.pop(name, None)
if reply.get('playfab'):
owned_env['XODUS_XSTS_PLAYFAB'] = reply['playfab']
content = folder / '.xbox-content'
@@ -69,7 +69,10 @@ bl_store_launch() {
fi
[ -x /usr/local/bin/droiddeck-store-launch ] || return 0
/usr/local/bin/droiddeck-store-launch "$@"
[ $? -eq 10 ] && BL_STORE_EXIT=1
rc=$?
[ $rc -eq 10 ] && BL_STORE_EXIT=1
[ $rc -eq 11 ] && exit 0
[ $rc -eq 12 ] && exit 1
return 0
}
@@ -1113,10 +1116,89 @@ def requested_tool(request, catalog):
def game_requests(directory=None):
value = read_json(state_path("games.json", directory))
if value.get("version") != 1 or not isinstance(value.get("games"), dict):
return {}
return value["games"]
return read_game_requests(directory)[0]
def read_game_requests(directory=None):
try:
with open(state_path("games.json", directory), encoding="utf-8") as f:
text = f.read()
value = json.loads(text)
except (OSError, ValueError):
return {}, None
if not isinstance(value, dict) or value.get("version") != 1 or not isinstance(value.get("games"), dict):
return {}, text
return value["games"], text
def write_game_requests(requests, original, directory=None):
path = state_path("games.json", directory)
try:
with open(path, encoding="utf-8") as f:
if f.read() != original:
return False
except OSError:
if original is not None:
return False
return write_json(path, {"version": 1, "games": requests})
def valid_game_request(app, request):
return (isinstance(app, str) and re.fullmatch(r"[1-9][0-9]*", app) is not None and int(app) <= 0xFFFFFFFF and
(request is None or isinstance(request, dict) and isinstance(request.get("dir"), str) and
request["dir"] != "" and isinstance(request.get("valve"), bool)))
def settle_game_requests(requests, state, current, catalog, missing_is_cleared=False):
recorded = state.get("games_applied")
if isinstance(recorded, dict):
applied = dict(recorded)
else:
applied = dict(requests) if "tools" in state else {}
by_name = {t["name"]: t for t in catalog}
out = dict(requests)
for app, request in requests.items():
if not valid_game_request(app, request) or app not in applied or applied[app] != request:
continue
if request is None:
del out[app]
continue
wanted = requested_tool(request, catalog)
if wanted is None:
continue
if app in current:
picked = current[app]
elif missing_is_cleared:
picked = ""
else:
continue
if picked == wanted:
continue
tool = by_name.get(picked)
if tool and tool["dir"]:
out[app] = {"valve": tool["valve"], "dir": tool["dir"]}
else:
del out[app]
return out, out != requests
def settled_game_requests(state, current, catalog, missing_is_cleared=False, directory=None):
for _ in range(2):
requests, text = read_game_requests(directory)
settled, moved = settle_game_requests(requests, state, current, catalog, missing_is_cleared)
if not moved or write_game_requests(settled, text, directory):
return settled
return None
def applied_game_requests(requests, overrides, state):
recorded = state.get("games_applied")
before = recorded if isinstance(recorded, dict) else {}
if requests is None:
return dict(before)
return {app: request if app in overrides and overrides[app] is not DEFERRED_GAME_TOOL else before[app]
for app, request in requests.items()
if app in overrides and overrides[app] is not DEFERRED_GAME_TOOL or app in before}
def requested_games(requests, catalog, runnable=None):
@@ -1140,6 +1222,32 @@ def requested_games(requests, catalog, runnable=None):
return resolved
OWN_PROTON = "DroidDeck-Proton"
ARCH_WORDS = re.compile(r"(?i)aarch64|arm64|x86_64|x86-64|amd64")
def own_builds(tools):
return [t for t in tools if isinstance(t, dict) and not t.get("valve") and
isinstance(t.get("dir"), str) and t["dir"].startswith(OWN_PROTON) and t.get("name")]
def build_key(name):
return [int(n) for n in re.findall(r"\d+", ARCH_WORDS.sub(" ", name))]
def own_proton_moves(state, catalog):
names = {t["name"] for t in catalog}
before = state.get("tools") if isinstance(state.get("tools"), list) else ()
seen = state.get("own_builds") if isinstance(state.get("own_builds"), list) else []
known = {n for n in seen if isinstance(n, str)} | {t["name"] for t in own_builds(before)} | {t["name"] for t in own_builds(catalog)}
state["own_builds"] = sorted(known)
builds = own_builds(catalog)
if not builds:
return None, set()
newest = max(builds, key=lambda t: (build_key(t["dir"]), t["dir"]))["name"]
return newest, known - names
def now_ms():
import time
return int(time.time() * 1000)
@@ -1300,13 +1408,29 @@ def register_default(config_path, apps=(), protect=(), default=None, catalog=Non
catalog = tool_catalog(os.path.dirname(os.path.dirname(os.path.abspath(config_path))),
dict(protect) if isinstance(protect, dict) else {name: name for name in protect})
names = [t["name"] for t in catalog]
newest, gone = own_proton_moves(state, catalog)
previous, applied, chosen = state.get("default"), state.get("applied"), state.get("seq")
if default is None:
if settle:
default, state = choose_default(current.get("0", ""), state, read_json(state_path("request.json", directory)), catalog)
else:
default = state.get("default") if state.get("default") in names else TOOL
if settle and newest and default == TOOL and state.get("applied") == applied and state.get("seq") == chosen and (
previous in gone or not state.get("own_proton")):
default = newest
state.update(default=newest, seq=now_ms(), source="update")
if settle and newest:
state["own_proton"] = True
if newest:
for app, name in list(current.items()):
if app != "0" and name in gone:
set_mapping(tokens, mapping, app, newest)
current[app] = newest
if isinstance(state.get("auto"), dict):
state["auto"] = {app: newest if name in gone else name for app, name in state["auto"].items()}
steam_root = os.path.dirname(os.path.dirname(os.path.abspath(config_path)))
overrides = requested_games(game_requests(directory), catalog)
requests = settled_game_requests(state, current, catalog, not steam_running(), directory)
overrides = requested_games(requests or {}, catalog)
managed = state.get("auto", {}).keys() if isinstance(state.get("auto"), dict) else ()
native_choices = state.get("native_choices", ())
native = native_targets(steam_root, set(apps) | set(picks) | set(managed) | set(native_choices))
@@ -1325,6 +1449,7 @@ def register_default(config_path, apps=(), protect=(), default=None, catalog=Non
state["auto"] = {app: tool for app, tool in auto.items() if tool is not NATIVE_CHOICE}
state["native_choices"] = sorted(app for app, tool in auto.items() if tool is NATIVE_CHOICE)
state["picked"] = picked_after(current, changes)
state["games_applied"] = applied_game_requests(requests, overrides, state)
state.pop("native", None)
state["tools"] = catalog
state.setdefault("default", default)
+279 -74
View File
@@ -17,6 +17,7 @@
#include <sys/syscall.h>
#include <sys/timerfd.h>
#include <time.h>
#include <ucontext.h>
#include <unistd.h>
struct ns_sem_args {
@@ -68,12 +69,13 @@ struct ns_wait_args {
#define NS_PROCS 256u
#define NS_FDS 65536
#define NS_DEVS 8
#define NS_VERSION 1u
#define NS_VERSION 2u
#define NS_HEAD_MAGIC 0x31434e5953544e44ull
#define NS_DEV_MAGIC 0x4e54445600000000ull
#define NS_OBJ_MAGIC 0x27ull
#define NS_WAITERS 0x80000000u
#define NS_SWEEP_NS 2000000000ull
#define NS_SPIN_NS 4000ull
#define NS_DEAD 0xffffffffu
#ifndef CLOSE_RANGE_CLOEXEC
@@ -144,6 +146,7 @@ struct ns_q {
uint32_t waiting;
uint32_t inuse;
uint32_t next_free;
uint32_t sleeping;
struct ns_ent ent[NS_ENTS];
};
@@ -168,6 +171,7 @@ struct ns_tls {
struct ns_dev *d;
uint32_t q;
int waiting;
int sleep;
};
static struct ns_dev ns_devs[NS_DEVS];
@@ -255,19 +259,148 @@ static void ns_unlock(uint32_t *w) {
}
}
static void ns_block(sigset_t *old) {
sigset_t s;
sigfillset(&s);
sigdelset(&s, SIGSEGV);
sigdelset(&s, SIGBUS);
sigdelset(&s, SIGILL);
sigdelset(&s, SIGFPE);
sigdelset(&s, SIGTRAP);
pthread_sigmask(SIG_BLOCK, &s, old);
#define NS_DEFER 4
#define NS_LEVELS 8
struct ns_cs {
int depth;
int count;
int level;
unsigned held_levels;
int sigs[NS_DEFER];
siginfo_t info[NS_DEFER];
sigset_t held[NS_LEVELS];
};
static __thread struct ns_cs ns_cs;
static uintptr_t ns_handlers[_NSIG];
static uint8_t ns_siginfo[_NSIG];
int bl_from_ntdll(const void *caller) __attribute__((visibility("hidden")));
static int ns_deferrable(int sig) {
return sig > 0 && sig < _NSIG && sig != SIGKILL && sig != SIGSTOP && sig != SIGSEGV && sig != SIGBUS && sig != SIGILL
&& sig != SIGFPE && sig != SIGTRAP && sig != SIGSYS;
}
static void ns_restore(const sigset_t *old) {
pthread_sigmask(SIG_SETMASK, old, NULL);
static int ns_defer(int sig, const siginfo_t *si) {
struct ns_cs *cs = &ns_cs;
if (sig < SIGRTMIN)
for (int i = 0; i < NS_DEFER; i++)
if (__atomic_load_n(&cs->sigs[i], __ATOMIC_RELAXED) == sig) return 1;
int i = __atomic_fetch_add(&cs->count, 1, __ATOMIC_RELAXED);
if (i >= NS_DEFER) {
__atomic_fetch_sub(&cs->count, 1, __ATOMIC_RELAXED);
return 0;
}
if (si) memcpy(&cs->info[i], si, sizeof(*si));
else memset(&cs->info[i], 0, sizeof(*si));
__atomic_signal_fence(__ATOMIC_SEQ_CST);
__atomic_store_n(&cs->sigs[i], sig, __ATOMIC_RELAXED);
return 1;
}
static int ns_park(int sig, const siginfo_t *si, ucontext_t *uc) {
int level = ns_cs.level;
if (level < 0 || level >= NS_LEVELS) return 0;
sigset_t one;
sigemptyset(&one);
sigaddset(&one, sig);
int saved = errno;
pthread_sigmask(SIG_BLOCK, &one, NULL);
pid_t pid = getpid(), tid = (pid_t)ns_self();
int ok = (si && syscall(SYS_rt_tgsigqueueinfo, pid, tid, sig, si) == 0) || syscall(SYS_tgkill, pid, tid, sig) == 0;
if (!ok) pthread_sigmask(SIG_UNBLOCK, &one, NULL);
errno = saved;
if (!ok) return 0;
sigaddset(&uc->uc_sigmask, sig);
unsigned long *words = (unsigned long *)&ns_cs.held[level];
unsigned bits = 8 * sizeof(unsigned long);
__atomic_fetch_or(&words[(unsigned)(sig - 1) / bits], 1ul << ((unsigned)(sig - 1) % bits), __ATOMIC_RELAXED);
__atomic_fetch_or(&ns_cs.held_levels, 1u << level, __ATOMIC_RELAXED);
return 1;
}
static void ns_trampoline(int sig, siginfo_t *si, void *uc) {
if (__atomic_load_n(&ns_cs.depth, __ATOMIC_RELAXED) > 0 && (ns_defer(sig, si) || (uc && ns_park(sig, si, uc)))) return;
uintptr_t h = __atomic_load_n(&ns_handlers[sig], __ATOMIC_ACQUIRE);
if (!h) return;
ns_cs.level++;
__atomic_signal_fence(__ATOMIC_SEQ_CST);
if (__atomic_load_n(&ns_siginfo[sig], __ATOMIC_RELAXED)) ((void (*)(int, siginfo_t *, void *))h)(sig, si, uc);
else ((void (*)(int))h)(sig);
__atomic_signal_fence(__ATOMIC_SEQ_CST);
ns_cs.level--;
}
static void ns_flush(void) {
struct ns_cs *cs = &ns_cs;
sigset_t all, mask;
sigfillset(&all);
int saved = errno;
pthread_sigmask(SIG_BLOCK, &all, &mask);
int n = cs->count < NS_DEFER ? cs->count : NS_DEFER;
pid_t pid = getpid(), tid = (pid_t)ns_self();
for (int i = 0; i < n; i++)
if (cs->sigs[i] > 0 && syscall(SYS_rt_tgsigqueueinfo, pid, tid, cs->sigs[i], &cs->info[i]) != 0) syscall(SYS_tgkill, pid, tid, cs->sigs[i]);
memset(cs->sigs, 0, sizeof(cs->sigs));
cs->count = 0;
int level = cs->level;
if (level >= 0 && level < NS_LEVELS && (cs->held_levels & (1u << level))) {
for (int sig = 1; sig < _NSIG; sig++)
if (sigismember(&cs->held[level], sig) == 1) sigdelset(&mask, sig);
sigemptyset(&cs->held[level]);
cs->held_levels &= ~(1u << level);
}
pthread_sigmask(SIG_SETMASK, &mask, NULL);
errno = saved;
}
static void ns_block(void) {
ns_cs.depth++;
__atomic_signal_fence(__ATOMIC_SEQ_CST);
}
static void ns_restore(void) {
__atomic_signal_fence(__ATOMIC_SEQ_CST);
if (--ns_cs.depth == 0) {
__atomic_signal_fence(__ATOMIC_SEQ_CST);
if (ns_cs.count || (ns_cs.held_levels && ns_cs.level >= 0 && ns_cs.level < NS_LEVELS && (ns_cs.held_levels & (1u << ns_cs.level))))
ns_flush();
}
}
int sigaction(int sig, const struct sigaction *act, struct sigaction *old) {
static int (*real)(int, const struct sigaction *, struct sigaction *);
if (!real) real = (int (*)(int, const struct sigaction *, struct sigaction *))dlsym(RTLD_NEXT, "sigaction");
if (!real) {
errno = ENOSYS;
return -1;
}
if (!ns_deferrable(sig) || !ns_on()) return real(sig, act, old);
uintptr_t prev = __atomic_load_n(&ns_handlers[sig], __ATOMIC_ACQUIRE);
uint8_t prev_info = __atomic_load_n(&ns_siginfo[sig], __ATOMIC_RELAXED);
struct sigaction mine;
const struct sigaction *use = act;
if (act && act->sa_handler != SIG_DFL && act->sa_handler != SIG_IGN && !(act->sa_flags & SA_RESETHAND) &&
bl_from_ntdll(__builtin_return_address(0))) {
mine = *act;
mine.sa_sigaction = ns_trampoline;
mine.sa_flags |= SA_SIGINFO;
__atomic_store_n(&ns_siginfo[sig], (act->sa_flags & SA_SIGINFO) ? 1 : 0, __ATOMIC_RELAXED);
__atomic_store_n(&ns_handlers[sig], (uintptr_t)act->sa_sigaction, __ATOMIC_RELEASE);
use = &mine;
}
int r = real(sig, use, old);
if (r != 0 && use == &mine) {
__atomic_store_n(&ns_siginfo[sig], prev_info, __ATOMIC_RELAXED);
__atomic_store_n(&ns_handlers[sig], prev, __ATOMIC_RELEASE);
}
if (r == 0 && old && old->sa_sigaction == ns_trampoline) {
old->sa_sigaction = (void (*)(int, siginfo_t *, void *))prev;
if (!prev_info) old->sa_flags &= ~SA_SIGINFO;
}
return r;
}
static inline struct ns_obj *ns_obj(struct ns_head *h, uint32_t s) {
@@ -378,10 +511,11 @@ static int ns_claim(struct ns_q *q, int32_t index) {
if (dead) return 0;
}
int32_t want = -1;
return __atomic_compare_exchange_n(&q->signaled, &want, index, 0, __ATOMIC_ACQ_REL, __ATOMIC_RELAXED);
return __atomic_compare_exchange_n(&q->signaled, &want, index, 0, __ATOMIC_SEQ_CST, __ATOMIC_RELAXED);
}
static void ns_wake(struct ns_q *q) {
if (!__atomic_load_n(&q->sleeping, __ATOMIC_SEQ_CST)) return;
int saved = errno;
ns_futex(&q->signaled, FUTEX_WAKE, 1, NULL, 0);
errno = saved;
@@ -618,14 +752,13 @@ static void ns_dev_put_locked(struct ns_dev *d) {
static void ns_dev_put(struct ns_dev *d) {
if (__atomic_sub_fetch(&d->refs, 1, __ATOMIC_ACQ_REL)) return;
sigset_t old;
int saved = errno;
ns_block(&old);
ns_block();
pthread_mutex_lock(&ns_mx);
uint32_t zero = 0;
if (d->h && __atomic_compare_exchange_n(&d->refs, &zero, NS_DEAD, 0, __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE)) ns_release(d);
pthread_mutex_unlock(&ns_mx);
ns_restore(&old);
ns_restore();
errno = saved;
}
@@ -682,20 +815,18 @@ static void ns_forget_locked(int fd) {
}
static void ns_forget(int fd) {
sigset_t old;
int saved = errno;
ns_block(&old);
ns_block();
pthread_mutex_lock(&ns_mx);
ns_forget_locked(fd);
pthread_mutex_unlock(&ns_mx);
ns_restore(&old);
ns_restore();
errno = saved;
}
static uint64_t ns_adopt(struct ns_dev *d, uint32_t tag, int fd, uint32_t gen, uint32_t s) {
uint64_t e = 0;
sigset_t old;
ns_block(&old);
ns_block();
pthread_mutex_lock(&ns_mx);
if (!d->h || d->tag != tag || !ns_table() || ns_attach(d) < 0 || s >= NS_SLOTS) goto out;
if ((e = __atomic_load_n(&ns_fdt[fd], __ATOMIC_ACQUIRE))) goto out;
@@ -710,7 +841,7 @@ static uint64_t ns_adopt(struct ns_dev *d, uint32_t tag, int fd, uint32_t gen, u
ns_unlock(&o->lock);
out:
pthread_mutex_unlock(&ns_mx);
ns_restore(&old);
ns_restore();
return e;
}
@@ -719,8 +850,7 @@ static uint64_t ns_note_device(int fd) {
uint64_t e = 0;
if (syscall(SYS_fstat, fd, &st) != 0 || !S_ISREG(st.st_mode)) return 0;
if (((uint64_t)st.st_size & ~0xffffull) != NS_DEV_MAGIC) return 0;
sigset_t old;
ns_block(&old);
ns_block();
pthread_mutex_lock(&ns_mx);
struct ns_dev *d = ns_table() ? ns_map(fd, (uint32_t)st.st_size & 0xffff, (uint64_t)st.st_ino) : NULL;
if (d) {
@@ -732,7 +862,7 @@ static uint64_t ns_note_device(int fd) {
}
}
pthread_mutex_unlock(&ns_mx);
ns_restore(&old);
ns_restore();
return e;
}
@@ -773,15 +903,14 @@ static int ns_ready(struct ns_dev *d) {
static void ns_thread_gone(void *p) {
struct ns_tls *t = p;
if (!t || !t->d || !t->q) return;
sigset_t old;
if (!t || !t->d) return;
struct ns_dev *d = t->d;
ns_block(&old);
ns_free_q(d->h, t->q - 1);
ns_block();
if (t->q) ns_free_q(d->h, t->q - 1);
t->q = 0;
t->waiting = 0;
t->d = NULL;
ns_restore(&old);
ns_restore();
ns_dev_put(d);
}
@@ -789,24 +918,28 @@ static void ns_key_init(void) {
pthread_key_create(&ns_key, ns_thread_gone);
}
static struct ns_q *ns_thread_q(struct ns_dev *d, uint32_t *qi) {
struct ns_head *h = d->h;
if (ns_tq.d && ns_tq.q && ns_tq.waiting) {
ns_drop_q(ns_tq.d->h, ns_tq.q - 1);
ns_tq.waiting = 0;
}
if (ns_tq.d == d && ns_tq.q) {
*qi = ns_tq.q - 1;
return ns_q(h, *qi);
}
if (ns_tq.d && ns_tq.q) {
struct ns_dev *old = ns_tq.d;
ns_free_q(old->h, ns_tq.q - 1);
static int ns_hold(struct ns_dev *d) {
if (ns_tq.d == d) return 1;
if (!ns_dev_get(d)) return 0;
if (ns_tq.sleep) return 2;
struct ns_dev *old = ns_tq.d;
if (old) {
ns_block();
if (ns_tq.q) ns_free_q(old->h, ns_tq.q - 1);
ns_tq.q = 0;
ns_tq.waiting = 0;
ns_tq.d = NULL;
ns_restore();
ns_dev_put(old);
}
pthread_once(&ns_once, ns_key_init);
ns_tq.d = d;
pthread_setspecific(ns_key, &ns_tq);
return 1;
}
static struct ns_q *ns_new_q(struct ns_dev *d, uint32_t *qi) {
struct ns_head *h = d->h;
uint32_t i;
if (!ns_pop(h, &h->q_free, 0, &i) && !ns_grow(&h->q_hwm, NS_QS, &i)) {
ns_sweep_dead(h);
@@ -815,22 +948,35 @@ static struct ns_q *ns_thread_q(struct ns_dev *d, uint32_t *qi) {
struct ns_q *q = ns_q(h, i);
q->proc = (uint32_t)d->proc;
q->waiting = 0;
q->sleeping = 0;
q->inuse = 1;
__atomic_add_fetch(&d->refs, 1, __ATOMIC_ACQ_REL);
ns_tq.d = d;
ns_tq.q = i + 1;
ns_tq.waiting = 0;
pthread_setspecific(ns_key, &ns_tq);
*qi = i;
return q;
}
static struct ns_q *ns_thread_q(struct ns_dev *d, uint32_t *qi) {
struct ns_head *h = d->h;
if (ns_tq.d != d) return NULL;
if (ns_tq.q && ns_tq.waiting) {
ns_drop_q(h, ns_tq.q - 1);
ns_tq.waiting = 0;
}
if (ns_tq.q) {
*qi = ns_tq.q - 1;
return ns_q(h, *qi);
}
struct ns_q *q = ns_new_q(d, qi);
if (!q) return NULL;
ns_tq.q = *qi + 1;
ns_tq.waiting = 0;
return q;
}
static int ns_create(struct ns_dev *d, int type, uint32_t a, uint32_t b) {
struct ns_head *h = d->h;
sigset_t old;
int fd = -1, err = 0;
uint32_t s = 0;
ns_block(&old);
ns_block();
pthread_mutex_lock(&ns_mx);
if (!ns_table() || ns_attach(d) < 0) {
err = ENOMEM;
@@ -879,7 +1025,7 @@ static int ns_create(struct ns_dev *d, int type, uint32_t a, uint32_t b) {
ns_unlock(&o->lock);
out:
pthread_mutex_unlock(&ns_mx);
ns_restore(&old);
ns_restore();
if (fd < 0) errno = err;
return fd;
}
@@ -890,6 +1036,20 @@ static int ns_expired(const struct ns_wait_args *args) {
return (uint64_t)now.tv_sec * 1000000000ull + (uint64_t)now.tv_nsec >= args->timeout;
}
static uint64_t ns_now(void) {
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
return (uint64_t)now.tv_sec * 1000000000ull + (uint64_t)now.tv_nsec;
}
static void ns_spin(struct ns_q *q) {
uint64_t end = ns_now() + NS_SPIN_NS;
for (unsigned i = 1; __atomic_load_n(&q->signaled, __ATOMIC_ACQUIRE) == -1; i++) {
ns_relax();
if (!(i & 63) && ns_now() >= end) return;
}
}
static int ns_sleep(struct ns_q *q, const struct ns_wait_args *args) {
struct timespec ts, *tsp = NULL;
int op = FUTEX_WAIT_BITSET | ((args->flags & NS_WAIT_REALTIME) ? FUTEX_CLOCK_REALTIME : 0);
@@ -898,14 +1058,45 @@ static int ns_sleep(struct ns_q *q, const struct ns_wait_args *args) {
ts.tv_nsec = (long)(args->timeout % 1000000000ull);
tsp = &ts;
}
while (__atomic_load_n(&q->signaled, __ATOMIC_ACQUIRE) == -1) {
if (tsp && ns_expired(args)) return ETIMEDOUT;
if (ns_futex(&q->signaled, op, (uint32_t)-1, tsp, FUTEX_BITSET_MATCH_ANY) == -1) {
if (errno == ETIMEDOUT) return ETIMEDOUT;
if (errno == EINTR) return EINTR;
}
ns_spin(q);
int ret = 0;
__atomic_store_n(&q->sleeping, 1, __ATOMIC_SEQ_CST);
while (!ret && __atomic_load_n(&q->signaled, __ATOMIC_SEQ_CST) == -1) {
if (tsp && ns_expired(args)) ret = ETIMEDOUT;
else if (ns_futex(&q->signaled, op, (uint32_t)-1, tsp, FUTEX_BITSET_MATCH_ANY) == -1 && (errno == ETIMEDOUT || errno == EINTR))
ret = errno;
}
return 0;
__atomic_store_n(&q->sleeping, 0, __ATOMIC_RELAXED);
return ret;
}
static int ns_try_any(struct ns_head *h, const uint32_t *slots, const uint32_t *gens, uint32_t total, uint32_t owner, int *dead) {
for (uint32_t i = 0; i < total; i++) {
struct ns_obj *o = ns_obj(h, slots[i]);
ns_lock(&o->lock);
int busy = o->type == NS_FREE || o->gen != gens[i] || o->any_head || o->all_hint;
if (!busy && ns_signaled(o, owner)) {
switch (o->type) {
case NS_SEM:
o->a--;
break;
case NS_MUTEX:
*dead = (int)o->ownerdead;
o->ownerdead = 0;
o->a++;
o->b = owner;
break;
case NS_EVENT:
if (!o->a) o->b = 0;
break;
}
ns_unlock(&o->lock);
return (int)i;
}
ns_unlock(&o->lock);
if (busy) return -1;
}
return -2;
}
static int ns_wait(struct ns_dev *d, struct ns_wait_args *ua, int all) {
@@ -927,12 +1118,24 @@ static int ns_wait(struct ns_dev *d, struct ns_wait_args *ua, int all) {
for (uint32_t j = 0; j < i; j++)
if (slots[j] == slots[i]) return EINVAL;
}
sigset_t old;
ns_block(&old);
int nested = ns_tq.sleep > 0;
ns_block();
if (!all && (nested || !ns_tq.waiting)) {
int dead = 0, got = ns_try_any(h, slots, gens, total, args.owner, &dead);
if (got >= 0) {
ns_restore();
ua->index = (uint32_t)got;
return dead ? EOWNERDEAD : 0;
}
if (got == -2 && args.timeout != UINT64_MAX && ns_expired(&args)) {
ns_restore();
return ETIMEDOUT;
}
}
uint32_t qi;
struct ns_q *q = ns_ready(d) ? ns_thread_q(d, &qi) : NULL;
struct ns_q *q = !ns_ready(d) ? NULL : nested ? ns_new_q(d, &qi) : ns_thread_q(d, &qi);
if (!q) {
ns_restore(&old);
ns_restore();
return ENOMEM;
}
ns_maybe_sweep(h);
@@ -952,7 +1155,7 @@ static int ns_wait(struct ns_dev *d, struct ns_wait_args *ua, int all) {
}
q->total = total;
q->waiting = 1;
ns_tq.waiting = 1;
if (!nested) ns_tq.waiting = 1;
int bad = 0;
if (!all) {
for (uint32_t i = 0; i < total && !bad; i++) {
@@ -1004,16 +1207,18 @@ static int ns_wait(struct ns_dev *d, struct ns_wait_args *ua, int all) {
}
int ret = 0;
if (!bad && __atomic_load_n(&q->signaled, __ATOMIC_ACQUIRE) == -1) {
sigset_t held;
ns_restore(&old);
ns_tq.sleep++;
ns_restore();
ret = ns_sleep(q, &args);
ns_block(&held);
ns_block();
ns_tq.sleep--;
}
ns_drop_q(h, qi);
ns_tq.waiting = 0;
if (!nested) ns_tq.waiting = 0;
int32_t got = __atomic_load_n(&q->signaled, __ATOMIC_ACQUIRE);
int dead = (int)q->ownerdead;
ns_restore(&old);
if (nested) ns_free_q(h, qi);
ns_restore();
if (bad) return EINVAL;
if (got != -1) {
ua->index = (uint32_t)got;
@@ -1061,12 +1266,11 @@ static int ns_obj_ioctl(struct ns_dev *d, uint64_t e, unsigned long req, void *a
return ENOTTY;
}
if (type != (uint32_t)want) return EINVAL;
sigset_t old;
ns_block(&old);
ns_block();
int all = ns_lock_obj(h, o);
if (o->type != (uint32_t)want || o->gen != gen) {
ns_unlock_obj(h, o, all);
ns_restore(&old);
ns_restore();
return EINVAL;
}
switch (req) {
@@ -1133,7 +1337,7 @@ static int ns_obj_ioctl(struct ns_dev *d, uint64_t e, unsigned long req, void *a
}
}
ns_unlock_obj(h, o, all);
ns_restore(&old);
ns_restore();
if (ret) return ret;
if (req == NS_SEM_RELEASE || req == NS_EVENT_SET || req == NS_EVENT_RESET || req == NS_EVENT_PULSE)
memcpy(arg, &prev, sizeof(prev));
@@ -1176,9 +1380,10 @@ int bl_ntsync_ioctl(int fd, unsigned long req, void *arg, int *rc) {
uint64_t e = ns_lookup(fd);
if (!e) return 0;
struct ns_dev *d = &ns_devs[(e >> 56) & 7];
if (!ns_dev_get(d)) return 0;
int held = ns_hold(d);
if (!held) return 0;
int out = 0, err = (e & NS_E_DEVICE) ? ns_dev_ioctl(d, req, arg, &out) : ns_obj_ioctl(d, e, req, arg);
ns_dev_put(d);
if (held == 2) ns_dev_put(d);
if (err) {
errno = err;
*rc = -1;
+9 -2
View File
@@ -51,9 +51,16 @@ static void rt_send(void *dpy, uint32_t action) {
}
}
static void *rt_xtst(const char *name) {
void *fn = dlsym(RTLD_NEXT, name);
if (fn) return fn;
void *lib = dlopen("libXtst.so.6", RTLD_LAZY | RTLD_NOLOAD);
return lib ? dlsym(lib, name) : NULL;
}
int XTestFakeMotionEvent(void *dpy, int screen, int x, int y, unsigned long delay) {
static int (*real)(void *, int, int, int, unsigned long);
if (!real) real = (int (*)(void *, int, int, int, unsigned long))dlsym(RTLD_NEXT, "XTestFakeMotionEvent");
if (!real) real = (int (*)(void *, int, int, int, unsigned long))rt_xtst("XTestFakeMotionEvent");
if (rt_enabled()) {
pthread_mutex_lock(&rt_lock);
rt_x = x;
@@ -69,7 +76,7 @@ int XTestFakeMotionEvent(void *dpy, int screen, int x, int y, unsigned long dela
int XTestFakeButtonEvent(void *dpy, unsigned int button, int press, unsigned long delay) {
static int (*real)(void *, unsigned int, int, unsigned long);
if (!real) real = (int (*)(void *, unsigned int, int, unsigned long))dlsym(RTLD_NEXT, "XTestFakeButtonEvent");
if (!real) real = (int (*)(void *, unsigned int, int, unsigned long))rt_xtst("XTestFakeButtonEvent");
if (button == 1) {
pthread_mutex_lock(&rt_lock);
int handled = 0;