Merge branch 'main' into stretch-to-panel

This commit is contained in:
Magarto authored and GitHub committed 2026-09-29 17:49:28 +02:00
commit 9d36f1a73b
47 files changed
+2671 -271

No files matched your search

@@ -510,20 +510,19 @@ BUSCONF
# desktop's launchers ask for; gamescope shows one window at a time and the game takes over.
_bl_ui=(-gamepadui)
[ "${BL_STEAM_UI:-}" = desktop ] && _bl_ui=()
# Deck mode (BL_STEAMDECK=1, the Performance page): -steamdeck is the flag that puts the Quick
# Access Menu, the performance overlay and the Deck settings pages on screen. Only -steamdeck,
# never -steamos3: that one makes the client manage itself as SteamOS does - it calls
# steamos-select-branch and opts the install into the steamdeck_stable client branch, writes
# that to package/beta, and from then on the branch on disk loses to the -clientbeta below on
# every launch; the updater finds "installed version 0", marks an update pending and exits 42
# to apply it, which never happens, so the client restarts for ever - under a running game,
# taking Steam Input with it each time. (Bannerlator 38996ea1, on device.) The SteamOS helpers
# the client calls in this mode are staged as no-op stubs under /usr/bin.
# Deck mode (BL_STEAMDECK=1, the Steam page's Client settings): -steamdeck is the flag that puts
# the Quick Access Menu and the Deck settings pages on screen; the QAM's performance overlay
# controls stay greyed out without -steamos3, as SteamOS and Armada launch it. -steamos3 makes
# the client manage itself as SteamOS does - it calls steamos-select-branch and writes
# steamdeck_stable to package/beta, and a branch on disk that loses to the -clientbeta below
# is an update that never installs: exit 42, a restart, for ever (Bannerlator 38996ea1). The
# loop below puts package/beta back before every start. The SteamOS helpers the client calls
# in this mode are staged as no-op stubs under /usr/bin.
if [ "${BL_STEAMDECK:-0}" = 1 ] && [ "${BL_STEAM_UI:-}" != desktop ]; then
_bl_ui+=(-steamdeck)
_bl_ui+=(-steamdeck -steamos3)
# The QAM's Scaling Filter lists NVIDIA Image Scaling only when told gamescope has it (it does).
export STEAM_GAMESCOPE_NIS_SUPPORTED=1
echo "== steam: Deck mode on (-steamdeck)"
echo "== steam: Deck mode on (-steamdeck -steamos3)"
fi
# What a killed session leaves behind. Android ends sessions without warning (backgrounding, the
# low-memory killer), and the client then finds .crash - opening its update UI on the spinner -
@@ -548,10 +547,31 @@ BUSCONF
# again on the branch it just wrote.
steam_channel=${BL_STEAM_CHANNEL:-publicbeta}
steam_beta=$steam_root/package/beta
if [ -n "${BL_ADDED_GAMES:-}" ] && [ -f "$BL_ADDED_GAMES" ]; then
bannerlator-steam-games watch "$steam_root" "$BL_ADDED_GAMES" &
games_watcher=$!
fi
echo "== steam args: ${_bl_ui[*]} -clientbeta $steam_channel -no-cef-sandbox -cef-force-gpu -cef-ozone-platform=x11 -cef-use-gl=angle -cef-use-angle=vulkan $*"
# An update that cannot install asks for a restart (rc=42) every time; the count is capped so a
# session can never again loop in silence, which reads as a menu opening and closing by itself.
steam_updates=0
rm -f "$HOME/.bl-stop-requested"
if [ -n "${BL_LAUNCH_DIR:-}" ] && [ -d "$BL_LAUNCH_DIR" ]; then
rm -f "$BL_LAUNCH_DIR/steam-stop"
(
while :; do
if [ -e "$BL_LAUNCH_DIR/steam-stop" ]; then
rm -f "$BL_LAUNCH_DIR/steam-stop"
: > "$HOME/.bl-stop-requested"
echo "== steam: the app asked for a clean exit"
"$steam_root/steamrtarm64/steam" -shutdown >/dev/null 2>&1 || true
exit 0
fi
sleep 0.5
done
) &
stop_watcher=$!
fi
while :; do
rm -f "$HOME/.bl-restart-once"
if [ -f "$steam_beta" ] && [ "$(cat "$steam_beta" 2>/dev/null)" != "$steam_channel" ]; then
@@ -565,6 +585,13 @@ BUSCONF
rc=$?
echo "== steam exited rc=$rc"
collect_steam_logs
if [ -n "${BL_ADDED_GAMES:-}" ] && [ -f "$BL_ADDED_GAMES" ]; then
bannerlator-steam-shortcuts "$steam_root" "$BL_ADDED_GAMES" || true
fi
if [ -f "$HOME/.bl-stop-requested" ]; then
rm -f "$HOME/.bl-stop-requested" "$HOME/.bl-restart-once"
break
fi
# The session asked for this exit (the ARM64 Proton landed): start again, without the URL
# arguments, which were acted on. Not counted against the update cap.
if [ -f "$HOME/.bl-restart-once" ]; then
@@ -581,6 +608,14 @@ BUSCONF
# The URL arguments were acted on the first time; a restart must not repeat them.
set --
done
if [ -n "${stop_watcher:-}" ]; then
kill "$stop_watcher" 2>/dev/null || true
wait "$stop_watcher" 2>/dev/null || true
fi
if [ -n "${games_watcher:-}" ]; then
kill "$games_watcher" 2>/dev/null || true
wait "$games_watcher" 2>/dev/null || true
fi
if [ -n "${BL_DESKTOP:-}" ]; then
# Started from the desktop (bannerlator-steam-launch): the desktop's own programs share this
# script's parent, so reap_orphans - right under gamescope, where everything is the client's -
+254
View File
@@ -0,0 +1,254 @@
#!/usr/bin/env python3
"""Resolve added games against the signed-in Steam client's licenses."""
import ctypes as C
import json
import os
from pathlib import Path
import re
import subprocess
import sys
import time
import unicodedata
import urllib.parse
import urllib.request
STATE = ".droiddeck-games.json"
BASE_ID = 76561197960265728
def read_json(path, default):
try:
return json.loads(Path(path).read_text())
except (OSError, ValueError):
return default
def save_json(path, value):
path = Path(path)
path.parent.mkdir(parents=True, exist_ok=True)
temp = path.with_suffix(".tmp")
temp.write_text(json.dumps(value))
os.replace(temp, path)
def title_key(name):
return "".join(c for c in unicodedata.normalize("NFKC", name.replace("™", "").replace("®", "")).casefold() if c.isalnum())
def edition_key(name):
name = re.sub(r"(?i)\s+(?:windows edition|intergrade)\s*$", "", name)
return title_key(name)
def source_stamp(game):
try:
exe = Path(game["exe"])
stat = exe.stat()
hints = []
for parent in sorted({Path(game["folder"]), exe.parent}):
hint = parent / "steam_appid.txt"
hints.append(hint.read_text().strip() if hint.is_file() else "")
return [game["name"], str(exe), stat.st_size, stat.st_mtime_ns, hints]
except (OSError, ValueError):
return None
def identify(game):
folder = Path(game["folder"])
steamapps = folder.parent.parent
if folder.parent.name == "common" and steamapps.name == "steamapps":
matches = []
for manifest in steamapps.glob("appmanifest_*.acf"):
try:
text = manifest.read_text()
install = re.search(r'"installdir"\s*"([^"\\]*)"', text, re.I)
if install and install[1] == folder.name:
matches.append(int(manifest.stem.removeprefix("appmanifest_")))
except (OSError, ValueError):
pass
if matches:
return matches[0] if len(matches) == 1 else None
ids = set()
for parent in {folder, Path(game["exe"]).parent}:
try:
appid = int((parent / "steam_appid.txt").read_text().strip())
if 0 < appid < 0x80000000:
ids.add(appid)
except (OSError, ValueError):
pass
if ids:
return next(iter(ids)) if len(ids) == 1 else None
url = "https://store.steampowered.com/api/storesearch/?l=english&cc=US&term=" + urllib.parse.quote(game["name"])
try:
with urllib.request.urlopen(url, timeout=5) as response:
items = json.loads(response.read(1024 * 1024)).get("items", [])
for key in (title_key, edition_key):
ids = {int(item["id"]) for item in items
if key(item.get("name", "")) == key(game["name"]) and key(game["name"])}
if ids:
return next(iter(ids)) if len(ids) == 1 else None
return None
except (OSError, ValueError, KeyError):
return None
def method(obj, slot, result, *args):
if not obj:
raise RuntimeError("Steam interface unavailable")
table = C.cast(obj, C.POINTER(C.POINTER(C.c_void_p))).contents
return C.CFUNCTYPE(result, C.c_void_p, *args)(table[slot])
def probe(steam_root, ids):
# Slots belong to Valve's SteamClient020, SteamUser021 and STEAMAPPS_INTERFACE_VERSION008.
library = C.CDLL(str(Path(steam_root) / "steamrtarm64/steamclient.so"))
library.CreateInterface.argtypes = [C.c_char_p, C.c_void_p]
library.CreateInterface.restype = C.c_void_p
client = library.CreateInterface(b"SteamClient020", None)
pipe = method(client, 0, C.c_int)(client)
if not pipe:
raise RuntimeError("Steam pipe unavailable")
user = 0
try:
user = method(client, 2, C.c_int, C.c_int)(client, pipe)
if not user:
raise RuntimeError("No signed-in Steam user")
get_interface = lambda slot, version: method(client, slot, C.c_void_p, C.c_int, C.c_int, C.c_char_p)(client, user, pipe, version)
account = get_interface(5, b"SteamUser021")
if not method(account, 1, C.c_bool)(account):
raise RuntimeError("Steam is offline")
steamid = method(account, 2, C.c_uint64)(account)
apps = get_interface(15, b"STEAMAPPS_INTERFACE_VERSION008")
subscribed = method(apps, 6, C.c_bool, C.c_uint32)
return {"account": str(steamid - BASE_ID), "checked": time.time(),
"owned": {str(appid): bool(subscribed(apps, appid)) for appid in ids}}
finally:
if user:
method(client, 4, None, C.c_int, C.c_int)(client, pipe, user)
method(client, 1, C.c_bool, C.c_int)(client, pipe)
def watch(steam_root, listing):
games = read_json(listing, [])
if os.environ.get("BL_STEAM_OFFLINE") == "1" or not games:
return
candidates, sources = {}, {}
next_lookup = 0
last_status = None
while True:
now = time.monotonic()
for game in games:
folder = game["folder"]
stamp = source_stamp(game)
if now >= next_lookup or stamp != sources.get(folder):
sources[folder] = stamp
appid = identify(game)
if appid:
candidates[folder] = appid
else:
candidates.pop(folder, None)
if now >= next_lookup:
next_lookup = now + 300
status = "waiting for a matching Steam title"
if candidates:
try:
result = subprocess.run([sys.executable, __file__, "probe", steam_root,
*map(str, sorted(set(candidates.values())))],
capture_output=True, text=True, timeout=15)
if result.returncode != 0:
status = "ownership probe failed: " + (result.stderr.strip()[-300:] or str(result.returncode))
else:
line = next(line for line in result.stdout.splitlines() if line.startswith("DROIDDECK_OWNERSHIP="))
snapshot = json.loads(line.removeprefix("DROIDDECK_OWNERSHIP="))
if not snapshot["account"].isdigit() or int(snapshot["account"]) <= 0:
raise ValueError("invalid Steam account")
snapshot["candidates"] = candidates
snapshot["sources"] = sources
save_json(Path(steam_root) / "userdata" / snapshot["account"] / "config" / STATE, snapshot)
status = "matched %d/%d games; %d owned" % (len(candidates), len(games), sum(snapshot["owned"].values()))
except (OSError, ValueError, KeyError, StopIteration, subprocess.TimeoutExpired) as error:
status = "ownership check unavailable: " + str(error)
if status != last_status:
print("bannerlator-steam-games: " + status, flush=True)
last_status = status
time.sleep(30)
def seed_install(steam_root, game, appid):
steamapps = Path(steam_root) / "steamapps"
common = steamapps / "common"
name = f"DroidDeck-{appid}"
target = common / name
source = Path(game["folder"])
title = game["name"].replace("\\", "\\\\").replace('"', '\\"').replace("\n", " ").replace("\r", " ")
content = ('"AppState"\n{\n' + f' "appid" "{appid}"\n "Universe" "1"\n "name" "{title}"\n'
+ f' "StateFlags" "4"\n "installdir" "{name}"\n' + '}\n')
legacy = content.replace(' "Universe" "1"\n', '')
manifest = steamapps / f"appmanifest_{appid}.acf"
for root in [Path(steam_root), Path("/mnt/bannerlator-sd")]:
existing = root / "steamapps" / manifest.name
if existing.is_file():
if (existing == manifest and source.is_dir() and target.is_symlink()
and os.readlink(target) == str(source)
and existing.read_text() in (legacy, legacy.replace('"StateFlags" "4"', '"StateFlags" "2"'))):
temp = manifest.with_suffix(".droiddeck")
temp.write_text(content)
os.replace(temp, manifest)
return True
if not source.is_dir():
return False
common.mkdir(parents=True, exist_ok=True)
if os.path.lexists(target):
if not target.is_symlink() or os.readlink(target) != str(source):
return False
else:
target.symlink_to(source, target_is_directory=True)
try:
with manifest.open("x") as output:
output.write(content)
except FileExistsError:
pass
return True
def route(steam_root, acct, games):
snapshot = read_json(Path(acct) / "config" / STATE, {})
if not isinstance(snapshot, dict):
snapshot = {}
owned = snapshot.get("owned", {}) if snapshot.get("account") == Path(acct).name else {}
candidates = snapshot.get("candidates", {})
sources = snapshot.get("sources", {})
if not all(isinstance(value, dict) for value in (owned, candidates, sources)):
owned, candidates, sources = {}, {}, {}
shortcuts, routes = [], {}
for game in games:
appid = candidates.get(game.get("folder"))
stamp = source_stamp(game)
confirmed = (isinstance(appid, int) and 0 < appid < 0x80000000
and owned.get(str(appid)) is True and stamp is not None
and sources.get(game.get("folder")) == stamp)
if confirmed:
try:
if seed_install(steam_root, game, appid):
routes[str(game["appid"])] = appid
continue
except OSError as error:
print(f"bannerlator-steam-games: import unavailable ({error})")
shortcuts.append(game)
save_json(Path(acct) / "config/.droiddeck-routes.json", routes)
return shortcuts, routes
if __name__ == "__main__":
if len(sys.argv) < 4:
sys.exit(2)
try:
if sys.argv[1] == "probe":
print("DROIDDECK_OWNERSHIP=" + json.dumps(probe(sys.argv[2], [int(value) for value in sys.argv[3:]])), flush=True)
elif sys.argv[1] == "watch":
watch(sys.argv[2], sys.argv[3])
else:
sys.exit(2)
except (OSError, RuntimeError, ValueError) as error:
print(str(error), file=sys.stderr)
sys.exit(1)
@@ -1,15 +1,7 @@
#!/usr/bin/env python3
"""Puts the app's added games (Windows games in the user's own folders) into the Steam client's
library as non-Steam shortcuts, each mapped to the ARM64 Proton, before the client starts.
"""Import added games while Steam is closed, preserving user-created shortcuts.
bannerlator-steam-shortcuts <steam_root> <added-games.json>
The JSON is a list the app writes each session: {"name", "exe", "dir", "appid"} with guest paths.
The client keeps non-Steam games in userdata/<account>/config/shortcuts.vdf, a binary VDF it
reads at start-up and rewrites at exit, so this runs while it is down. Entries the app wrote
carry the tag "droiddeck-app" and are replaced wholesale; anything the user added inside the
client is kept as it is. The appid is the one the client itself derives (crc32 of exe+name with
the top bit set), so the launch URL the app uses matches what the client shows.
Usage: bannerlator-steam-shortcuts <steam_root> <added-games.json>
"""
import importlib.machinery
import importlib.util
@@ -207,8 +199,15 @@ def main():
try:
with open(path, "rb") as f:
existing = parse(f.read()).get("shortcuts", {})
except (ValueError, IndexError) as e:
print("bannerlator-steam-shortcuts: could not read the existing file (%s); starting over" % e)
except (ValueError, IndexError, struct.error) as e:
print("bannerlator-steam-shortcuts: could not read the existing file (%s); keeping it unchanged" % e)
return 1
helper = os.path.join(os.path.dirname(os.path.abspath(__file__)), "bannerlator-steam-games")
loader = importlib.machinery.SourceFileLoader("steam_games", helper)
spec = importlib.util.spec_from_loader(loader.name, loader)
routing = importlib.util.module_from_spec(spec)
loader.exec_module(routing)
games, routes = routing.route(steam_root, acct, games)
kept = [v for v in existing.values() if isinstance(v, dict) and TAG not in (v.get("tags") or {}).values()]
ours = [shortcut(g) for g in games if g.get("exe") and g.get("name")]
entries = {str(i): v for i, v in enumerate(kept + ours)}
@@ -226,8 +225,8 @@ def main():
compat = importlib.util.module_from_spec(spec)
spec.loader.exec_module(compat)
cfg = os.path.join(steam_root, "config", "config.vdf")
if os.path.isfile(cfg) and ours:
compat.register_default(cfg, apps=[str(int(g["appid"]) & 0xFFFFFFFF) for g in games if g.get("exe")])
if os.path.isfile(cfg) and (ours or routes):
compat.register_default(cfg, apps=[str(int(g["appid"]) & 0xFFFFFFFF) for g in games if g.get("exe")] + [str(appid) for appid in set(routes.values())])
print("bannerlator-steam-shortcuts: mapped them to %s" % compat.TOOL)
except Exception as e: # noqa: BLE001 - the games still show; the client falls back to the default tool
print("bannerlator-steam-shortcuts: compat mapping skipped (%s)" % e)
+4 -1
View File
@@ -131,6 +131,9 @@ static struct pad *pad_for_syslink(const char *path) {
static void *next_symbol(const char *name) { return dlsym(RTLD_NEXT, name); }
char *bl_cached_realpath(const char *path, char *resolved, char *(*real)(const char *, char *))
__attribute__((visibility("hidden")));
/* Wine reaches libudev from its HID bus driver, which is the only caller these pads are for. */
static int caller_is_winebus(void *caller) {
Dl_info info;
@@ -152,7 +155,7 @@ char *realpath(const char *path, char *resolved) {
}
if (real_realpath == NULL) real_realpath = next_symbol("realpath");
if (real_realpath == NULL) return NULL;
return real_realpath(path, resolved);
return bl_cached_realpath(path, resolved, real_realpath);
}
void *udev_device_new_from_syspath(void *udev, const char *syspath) {
+189
View File
@@ -0,0 +1,189 @@
/*
* Path lookups the Steam client repeats, answered from a short-lived cache.
*
* While it registers its compatibility tools after logon, the client resolves the same few
* directories - its local tools under compatibilitytools.d and its own platform directory - with
* realpath() and access() thousands of times a second. Each realpath() is one readlinkat() per
* path component, and under proot every one of those is a trapped syscall with a round trip
* through the tracer. Measured on an AYN Thor, that loop was 23 of the client's 31 seconds of
* startup, with proot busy for 16 of them.
*
* Only the client itself uses the cache, never a game or a helper. Only successful answers are
* kept, and only for a moment: a path that stops existing is noticed within PATH_CACHE_TTL_NS, and
* one that starts existing is never hidden, since failures always go to the filesystem.
* BL_NO_PATH_CACHE=1 turns it off.
*/
#define _GNU_SOURCE
#include <dlfcn.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#define PATH_CACHE_SLOTS 256
#define PATH_CACHE_KEY_MAX 512
#define PATH_CACHE_TTL_NS 2000000000LL
enum { KIND_REALPATH, KIND_ACCESS };
struct entry {
long long stamp;
unsigned hash;
int kind;
int mode;
int flags;
char key[PATH_CACHE_KEY_MAX];
char *value;
};
static struct entry table[PATH_CACHE_SLOTS];
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
static long long now_ns(void) {
struct timespec t;
clock_gettime(CLOCK_MONOTONIC_COARSE, &t);
return (long long)t.tv_sec * 1000000000LL + t.tv_nsec;
}
static int enabled(void) {
static int cached = -1;
const char *off;
if (cached >= 0) return cached;
off = getenv("BL_NO_PATH_CACHE");
cached = strcmp(program_invocation_short_name, "steam") == 0 &&
strstr(program_invocation_name, "steamrtarm64") != NULL &&
!(off != NULL && off[0] == '1');
return cached;
}
static unsigned hash_of(const char *s, int kind, int mode, int flags) {
unsigned h = 2166136261u ^ (unsigned)(kind * 31 + mode * 7 + flags);
while (*s) h = (h ^ (unsigned char)*s++) * 16777619u;
return h;
}
static int cacheable(const char *path) {
return path != NULL && path[0] == '/' && strlen(path) < PATH_CACHE_KEY_MAX && enabled();
}
static struct entry *find(const char *path, int kind, int mode, int flags, unsigned h) {
struct entry *e = &table[h % PATH_CACHE_SLOTS];
if (e->stamp == 0 || e->hash != h || e->kind != kind || e->mode != mode || e->flags != flags) return NULL;
if (now_ns() - e->stamp > PATH_CACHE_TTL_NS || strcmp(e->key, path) != 0) return NULL;
return e;
}
static void store(const char *path, int kind, int mode, int flags, unsigned h, const char *value) {
struct entry *e = &table[h % PATH_CACHE_SLOTS];
char *copy = value != NULL ? strdup(value) : NULL;
if (value != NULL && copy == NULL) return;
free(e->value);
e->value = copy;
e->hash = h;
e->kind = kind;
e->mode = mode;
e->flags = flags;
memcpy(e->key, path, strlen(path) + 1);
e->stamp = now_ns();
}
__attribute__((visibility("hidden")))
char *bl_cached_realpath(const char *path, char *resolved, char *(*real)(const char *, char *)) {
char out[PATH_MAX];
unsigned h;
struct entry *e;
if (!cacheable(path)) return real(path, resolved);
h = hash_of(path, KIND_REALPATH, 0, 0);
pthread_mutex_lock(&lock);
e = find(path, KIND_REALPATH, 0, 0, h);
if (e != NULL && e->value != NULL) {
size_t n = strlen(e->value) + 1;
char *result = resolved != NULL ? resolved : malloc(n);
if (result != NULL) memcpy(result, e->value, n);
pthread_mutex_unlock(&lock);
return result;
}
pthread_mutex_unlock(&lock);
if (real(path, out) == NULL) return NULL;
pthread_mutex_lock(&lock);
store(path, KIND_REALPATH, 0, 0, h, out);
pthread_mutex_unlock(&lock);
if (resolved != NULL) {
memcpy(resolved, out, strlen(out) + 1);
return resolved;
}
return strdup(out);
}
static int cached_access(const char *path, int mode, int flags, int (*check)(const char *, int, int)) {
unsigned h;
int hit;
int result;
if (!cacheable(path)) return check(path, mode, flags);
h = hash_of(path, KIND_ACCESS, mode, flags);
pthread_mutex_lock(&lock);
hit = find(path, KIND_ACCESS, mode, flags, h) != NULL;
pthread_mutex_unlock(&lock);
if (hit) return 0;
result = check(path, mode, flags);
if (result == 0) {
pthread_mutex_lock(&lock);
store(path, KIND_ACCESS, mode, flags, h, NULL);
pthread_mutex_unlock(&lock);
}
return result;
}
static int (*next_faccessat(void))(int, const char *, int, int) {
static int (*real)(int, const char *, int, int);
if (real == NULL) real = (int (*)(int, const char *, int, int))dlsym(RTLD_NEXT, "faccessat");
return real;
}
static int check_cwd(const char *path, int mode, int flags) {
int (*real)(int, const char *, int, int) = next_faccessat();
if (real == NULL) {
errno = ENOSYS;
return -1;
}
return real(AT_FDCWD, path, mode, flags);
}
int access(const char *path, int mode) {
return cached_access(path, mode, 0, check_cwd);
}
int euidaccess(const char *path, int mode) {
return cached_access(path, mode, AT_EACCESS, check_cwd);
}
int eaccess(const char *path, int mode) {
return cached_access(path, mode, AT_EACCESS, check_cwd);
}
int faccessat(int dirfd, const char *path, int mode, int flags) {
int (*real)(int, const char *, int, int);
if (dirfd == AT_FDCWD) {
return cached_access(path, mode, flags, check_cwd);
}
real = next_faccessat();
if (real == NULL) {
errno = ENOSYS;
return -1;
}
return real(dirfd, path, mode, flags);
}
+124 -1
View File
@@ -7,7 +7,8 @@
* segment is a plain file that shmat maps. Objects are found by the inode of their file, which is
* what the id is. semop applies its operations one by one (no all-or-nothing across a set) and
* SEM_UNDO is accepted but not performed - enough for the client, which uses single-counter sets.
* Message queues stay unsupported. */
* A message queue is a table of slots in its file: gamescope hands mangoapp (Deck mode's performance
* overlay) each frame's timings through one. */
#define _GNU_SOURCE
#include <dirent.h>
#include <errno.h>
@@ -15,6 +16,7 @@
#include <limits.h>
#include <linux/futex.h>
#include <pthread.h>
#include <signal.h>
#include <stdarg.h>
#include <stdatomic.h>
#include <stdint.h>
@@ -23,6 +25,7 @@
#include <string.h>
#include <sys/ipc.h>
#include <sys/mman.h>
#include <sys/msg.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <sys/stat.h>
@@ -396,6 +399,126 @@ int shmctl(int shmid, int cmd, struct shmid_ds *buf) {
}
}
/* ------------------------------------------------------------------ message queues */
/* Only what gamescope and mangoapp use: msgtyp 0 or > 0, IPC_NOWAIT and IPC_RMID. */
#define MSG_MAGIC 0x574e4d51u /* "WNMQ" */
#define MSG_SLOTS 32
struct msg_queue {
uint32_t magic;
_Atomic int32_t lock; /* 0 free, else the holder's pid */
_Atomic int32_t changed; /* bumped on every send and receive; waiters sleep on it */
uint64_t next_seq;
struct { uint64_t seq; long mtype; size_t len; char data[2048]; } slot[MSG_SLOTS]; /* seq 0: free */
};
static struct msg_queue *msg_map(int id) {
int fd = open_by_id("msg", id);
if (fd < 0) return NULL;
struct msg_queue *q = mmap(NULL, sizeof(*q), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
close(fd);
if (q == MAP_FAILED) return NULL;
if (q->magic == MSG_MAGIC) return q;
munmap(q, sizeof(*q));
errno = EINVAL;
return NULL;
}
/* A process killed while holding the lock (Android ends sessions without warning) loses it. */
static void msg_lock(struct msg_queue *q) {
for (int32_t owner = 0; !atomic_compare_exchange_strong(&q->lock, &owner, getpid()); owner = 0) {
if (kill(owner, 0) != 0 && errno == ESRCH) atomic_compare_exchange_strong(&q->lock, &owner, 0);
else futex(&q->lock, FUTEX_WAIT, owner, &(struct timespec){0, 20000000});
}
}
static void msg_unlock(struct msg_queue *q, int changed) {
atomic_store(&q->lock, 0);
futex(&q->lock, FUTEX_WAKE, 1, NULL);
if (changed) {
atomic_fetch_add(&q->changed, 1);
futex(&q->changed, FUTEX_WAKE, INT_MAX, NULL);
}
}
int msgget(key_t key, int msgflg) {
int created;
int fd = open_object("msg", key, msgflg, &created);
if (fd < 0) return -1;
uint32_t magic = MSG_MAGIC;
if (created && (ftruncate(fd, sizeof(struct msg_queue)) != 0 || pwrite(fd, &magic, sizeof(magic), 0) != sizeof(magic))) {
close(fd);
return -1;
}
int id = object_id(fd);
close(fd);
return id;
}
int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg) {
struct msg_queue *q = msg_map(msqid);
if (!q) return -1;
if (msgsz > sizeof(q->slot[0].data) || *(const long *)msgp < 1) { munmap(q, sizeof(*q)); errno = EINVAL; return -1; }
for (;;) {
msg_lock(q);
int32_t seen = atomic_load(&q->changed);
for (int i = 0; i < MSG_SLOTS; i++) {
if (q->slot[i].seq) continue;
q->slot[i].mtype = *(const long *)msgp;
q->slot[i].len = msgsz;
memcpy(q->slot[i].data, (const long *)msgp + 1, msgsz);
q->slot[i].seq = ++q->next_seq;
msg_unlock(q, 1);
munmap(q, sizeof(*q));
return 0;
}
msg_unlock(q, 0);
if ((msgflg & IPC_NOWAIT) || (futex(&q->changed, FUTEX_WAIT, seen, NULL) != 0 && errno == EINTR)) {
munmap(q, sizeof(*q));
if (msgflg & IPC_NOWAIT) errno = EAGAIN;
return -1;
}
}
}
ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg) {
if (msgtyp < 0) { errno = EINVAL; return -1; }
struct msg_queue *q = msg_map(msqid);
if (!q) return -1;
for (;;) {
msg_lock(q);
int32_t seen = atomic_load(&q->changed);
int best = -1;
for (int i = 0; i < MSG_SLOTS; i++)
if (q->slot[i].seq && (!msgtyp || q->slot[i].mtype == msgtyp) && (best < 0 || q->slot[i].seq < q->slot[best].seq))
best = i;
if (best >= 0) {
size_t n = q->slot[best].len < msgsz ? q->slot[best].len : msgsz;
*(long *)msgp = q->slot[best].mtype;
memcpy((long *)msgp + 1, q->slot[best].data, n);
q->slot[best].seq = 0;
msg_unlock(q, 1);
munmap(q, sizeof(*q));
return (ssize_t)n;
}
msg_unlock(q, 0);
if ((msgflg & IPC_NOWAIT) || (futex(&q->changed, FUTEX_WAIT, seen, NULL) != 0 && errno == EINTR)) {
munmap(q, sizeof(*q));
if (msgflg & IPC_NOWAIT) errno = ENOMSG;
return -1;
}
}
}
int msgctl(int msqid, int cmd, struct msqid_ds *buf) {
if (cmd != IPC_RMID) { errno = EINVAL; return -1; }
pthread_mutex_lock(&g_lock);
int rc = unlink_by_id("msg", msqid);
pthread_mutex_unlock(&g_lock);
return rc;
}
/*
* fork() carries over only the calling thread, so a lock another thread was holding at that
* instant stays held in the child by a thread that is not there to release it. Taking it before
+101
View File
@@ -0,0 +1,101 @@
/*
* A trace instance for mangoapp where the kernel's tracefs is closed to the app.
*
* Valve's mangoapp (Deck mode's performance overlay) reads the GPU memory total on a Qualcomm
* display driver (msm_dpu / msm_drm) from the gpu_mem_total event, through a tracefs instance of
* its own - and aborts when tracefs_instance_create() fails, taking the whole overlay with it.
* Android mounts tracefs for root and the readtracefs group only, so it always fails here.
*
* The real calls run first. Only when they fail is the instance placed in an empty directory of
* the session's own, and reading its events answers with one gpu_mem_total record carrying the
* total KGSL - the GPU driver of every Adreno under Android - reports to the app.
*/
#define _GNU_SOURCE
#include <dlfcn.h>
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#define STANDIN_DIR "/tmp/droiddeck-tracefs"
#define KGSL_TOTAL "/sys/class/kgsl/kgsl/page_alloc"
struct tracefs_instance;
struct tep_handle;
struct tep_event;
/* The head of libtraceevent 1.8's struct tep_record (the version tools/mangoapp/packages.txt pins). */
struct tep_record {
unsigned long long ts, offset;
long long missed_events;
int record_size, size;
void *data;
int cpu, ref_count, locked;
void *priv;
};
/* The event as Android kernels declare it (trace/events/gpu_mem.h). */
static const char gpu_mem_total_format[] =
"name: gpu_mem_total\n"
"ID: 1\n"
"format:\n"
"\tfield:unsigned short common_type;\toffset:0;\tsize:2;\tsigned:0;\n"
"\tfield:unsigned char common_flags;\toffset:2;\tsize:1;\tsigned:0;\n"
"\tfield:unsigned char common_preempt_count;\toffset:3;\tsize:1;\tsigned:0;\n"
"\tfield:int common_pid;\toffset:4;\tsize:4;\tsigned:1;\n"
"\n"
"\tfield:uint32_t gpu_id;\toffset:8;\tsize:4;\tsigned:0;\n"
"\tfield:uint32_t pid;\toffset:12;\tsize:4;\tsigned:0;\n"
"\tfield:uint64_t size;\toffset:16;\tsize:8;\tsigned:0;\n"
"\n"
"print fmt: \"gpu_id=%u pid=%u size=%llu\", REC->gpu_id, REC->pid, REC->size\n";
static struct tracefs_instance *standin;
struct tracefs_instance *tracefs_instance_create(const char *name) {
struct tracefs_instance *(*real)(const char *) = dlsym(RTLD_NEXT, "tracefs_instance_create");
struct tracefs_instance *instance = real ? real(name) : NULL;
if (instance || !name) return instance;
struct tracefs_instance *(*alloc)(const char *, const char *) = dlsym(RTLD_NEXT, "tracefs_instance_alloc");
if (!alloc) return NULL;
char path[PATH_MAX];
mkdir(STANDIN_DIR, 0700);
mkdir(STANDIN_DIR "/instances", 0700);
snprintf(path, sizeof(path), STANDIN_DIR "/instances/%s", name);
mkdir(path, 0700);
return standin = alloc(STANDIN_DIR, name);
}
struct tep_handle *tracefs_local_events(const char *tracing_dir) {
if (!tracing_dir || strncmp(tracing_dir, STANDIN_DIR "/", sizeof(STANDIN_DIR))) {
struct tep_handle *(*real)(const char *) = dlsym(RTLD_NEXT, "tracefs_local_events");
return real ? real(tracing_dir) : NULL;
}
struct tep_handle *(*tep_alloc)(void) = dlsym(RTLD_DEFAULT, "tep_alloc");
int (*parse)(struct tep_handle *, const char *, unsigned long, const char *) = dlsym(RTLD_DEFAULT, "tep_parse_event");
struct tep_handle *tep = tep_alloc ? tep_alloc() : NULL;
if (tep && parse) parse(tep, gpu_mem_total_format, sizeof(gpu_mem_total_format) - 1, "gpu_mem");
return tep;
}
int tracefs_iterate_raw_events(struct tep_handle *tep, struct tracefs_instance *instance, void *cpus, int cpu_size,
int (*callback)(struct tep_event *, struct tep_record *, int, void *), void *context) {
if (!standin || instance != standin) {
int (*real)(struct tep_handle *, struct tracefs_instance *, void *, int,
int (*)(struct tep_event *, struct tep_record *, int, void *), void *) =
dlsym(RTLD_NEXT, "tracefs_iterate_raw_events");
return real ? real(tep, instance, cpus, cpu_size, callback, context) : -1;
}
struct tep_event *(*find)(struct tep_handle *, int) = dlsym(RTLD_DEFAULT, "tep_find_event");
struct tep_event *event = find && tep ? find(tep, 1) : NULL;
unsigned long long total;
FILE *f = fopen(KGSL_TOTAL, "r");
int have = f && fscanf(f, "%llu", &total) == 1;
if (f) fclose(f);
if (!event || !have || !callback) return -1;
struct { uint16_t type; uint8_t flags, preempt; int32_t common_pid; uint32_t gpu_id, pid; uint64_t size; } data = {
.type = 1, .size = total};
struct tep_record record = {.record_size = sizeof(data), .size = sizeof(data), .data = &data};
return callback(event, &record, 0, context);
}
+4
View File
@@ -1,4 +1,8 @@
#!/bin/sh
# gamescope --mangoapp runs `mangoapp` from PATH; this puts Valve's build beside the libraries the
# runtime lacks (fmt 10, spdlog 1.13, glfw, libtracefs, libtraceevent), staged by the app.
# mangoapp reads its window through GLFW's X11 API. GLFW 3.4 takes XDG_SESSION_TYPE=wayland with a
# WAYLAND_DISPLAY as Wayland-only, and unset, libwayland falls back to wayland-0 - DroidDeck's own
# compositor, which shares XDG_RUNTIME_DIR - so both fail; x11 makes GLFW use gamescope's X server.
export XDG_SESSION_TYPE=x11
LD_LIBRARY_PATH=/usr/local/lib/mangoapp${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH} exec /usr/local/lib/mangoapp/mangoapp "$@"
+231
View File
@@ -0,0 +1,231 @@
import importlib.machinery
import importlib.util
import json
from pathlib import Path
import subprocess
import tempfile
import unittest
from unittest.mock import patch
BIN = Path(__file__).resolve().parents[1] / 'linuxfs/overlay/usr/local/bin'
def load(name):
loader = importlib.machinery.SourceFileLoader(name, str(BIN / name))
spec = importlib.util.spec_from_loader(name, loader)
module = importlib.util.module_from_spec(spec)
loader.exec_module(module)
return module
imports = load('bannerlator-steam-games')
shortcuts = load('bannerlator-steam-shortcuts')
class ImportsTest(unittest.TestCase):
def setUp(self):
self.temp = tempfile.TemporaryDirectory()
self.addCleanup(self.temp.cleanup)
self.root = Path(self.temp.name)
self.steam = self.root / 'Steam'
self.acct = self.steam / 'userdata/123'
self.folder = self.root / 'Games/Example'
self.folder.mkdir(parents=True)
self.exe = self.folder / 'Example.exe'
self.exe.write_bytes(b'game')
self.game = dict(name='Example', exe=str(self.exe), folder=str(self.folder),
dir=str(self.folder), appid=0x87654321)
def snapshot(self, owned=True, account='123'):
imports.save_json(self.acct / 'config' / imports.STATE,
dict(account=account, owned={'42': owned},
candidates={str(self.folder): 42},
sources={str(self.folder): imports.source_stamp(self.game)}))
def test_owned_uses_real_id_and_registers_installed_files(self):
self.snapshot()
games, routes = imports.route(self.steam, self.acct, [self.game])
self.assertEqual([], games)
self.assertEqual({str(self.game['appid']): 42}, routes)
manifest = self.steam / 'steamapps/appmanifest_42.acf'
self.assertIn('"StateFlags" "4"', manifest.read_text())
self.assertIn('"Universe" "1"', manifest.read_text())
self.assertEqual(self.folder, (self.steam / 'steamapps/common/DroidDeck-42').resolve())
self.assertEqual(b'game', self.exe.read_bytes())
def test_migrates_only_unchanged_legacy_import_manifests(self):
self.snapshot()
imports.route(self.steam, self.acct, [self.game])
path = self.steam / 'steamapps/appmanifest_42.acf'
current = path.read_text()
legacy = current.replace(' "Universe" "1"\n', '').replace('"StateFlags" "4"', '"StateFlags" "2"')
path.write_text(legacy)
imports.route(self.steam, self.acct, [self.game])
self.assertEqual(current, path.read_text())
changed = legacy.replace('"StateFlags" "2"', '"StateFlags" "6"')
path.write_text(changed)
imports.route(self.steam, self.acct, [self.game])
self.assertEqual(changed, path.read_text())
def test_unresolved_titles_are_retried_after_network_recovers(self):
listing = self.root / 'games.json'
listing.write_text(json.dumps([self.game]))
from unittest.mock import Mock
result = Mock(returncode=0, stdout='DROIDDECK_OWNERSHIP=' + json.dumps(dict(account='123', owned={'42': True})))
with patch.object(imports, 'identify', side_effect=[None, 42]) as identify, \
patch.object(imports.time, 'monotonic', side_effect=[0, 301]), \
patch.object(imports.time, 'sleep', side_effect=[None, RuntimeError('stop')]), \
patch.object(imports.subprocess, 'run', return_value=result), \
patch('builtins.print'):
with self.assertRaisesRegex(RuntimeError, 'stop'):
imports.watch(self.steam, listing)
self.assertEqual(2, identify.call_count)
snapshot = imports.read_json(self.acct / 'config' / imports.STATE, {})
self.assertTrue(snapshot['owned']['42'])
def test_unowned_unknown_and_different_account_stay_shortcuts(self):
for owned, account in [(False, '123'), (True, '456')]:
self.snapshot(owned, account)
self.assertEqual(([self.game], {}), imports.route(self.steam, self.acct, [self.game]))
(self.acct / 'config' / imports.STATE).unlink()
self.assertEqual(([self.game], {}), imports.route(self.steam, self.acct, [self.game]))
self.assertFalse((self.steam / 'steamapps').exists())
def test_existing_manifest_is_never_overwritten(self):
manifest = self.steam / 'steamapps/appmanifest_42.acf'
manifest.parent.mkdir(parents=True)
manifest.write_text('Steam owns this file')
self.snapshot()
imports.route(self.steam, self.acct, [self.game])
self.assertEqual('Steam owns this file', manifest.read_text())
def test_import_collision_falls_back_without_touching_files(self):
occupied = self.steam / 'steamapps/common/DroidDeck-42'
occupied.mkdir(parents=True)
self.snapshot()
self.assertEqual(([self.game], {}), imports.route(self.steam, self.acct, [self.game]))
self.assertTrue(occupied.is_dir())
self.assertFalse(occupied.is_symlink())
def test_removed_folder_does_not_import(self):
self.snapshot()
self.exe.unlink()
self.folder.rmdir()
self.assertEqual(([self.game], {}), imports.route(self.steam, self.acct, [self.game]))
def test_replaced_game_does_not_inherit_cached_ownership(self):
self.snapshot()
self.exe.write_bytes(b'a different game')
self.assertEqual(([self.game], {}), imports.route(self.steam, self.acct, [self.game]))
def test_duplicate_owned_folders_share_one_steam_entry(self):
self.snapshot()
games, routes = imports.route(self.steam, self.acct, [self.game, self.game])
self.assertEqual([], games)
self.assertEqual(1, len(routes))
self.assertEqual(1, len(list((self.steam / 'steamapps').glob('*.acf'))))
def test_explicit_appid_does_not_need_network(self):
(self.folder / 'steam_appid.txt').write_text('42\n')
with patch.object(imports.urllib.request, 'urlopen', side_effect=AssertionError):
self.assertEqual(42, imports.identify(self.game))
def test_manifest_identity_uses_install_directory(self):
folder = self.root / 'second/steamapps/common/Custom Name'
folder.mkdir(parents=True)
(folder.parent.parent / 'appmanifest_42.acf').write_text('"AppState" { "installdir" "Custom Name" }')
game = dict(self.game, folder=str(folder), exe=str(folder / 'game.exe'))
with patch.object(imports.urllib.request, 'urlopen', side_effect=AssertionError):
self.assertEqual(42, imports.identify(game))
def test_store_requires_unique_exact_title(self):
cases = [([{'id': 42, 'name': 'Example Deluxe'}], None),
([{'id': 42, 'name': 'Example'}, {'id': 43, 'name': 'Example'}], None),
([{'id': 42, 'name': 'EXAMPLE'}], 42)]
for items, expected in cases:
with patch.object(imports.urllib.request, 'urlopen') as request:
request.return_value.__enter__.return_value.read.return_value = json.dumps({'items': items}).encode()
self.assertEqual(expected, imports.identify(self.game))
def test_store_country_and_edition_suffixes(self):
for folder, store in [('FINAL FANTASY VII REMAKE', 'FINAL FANTASY VII REMAKE INTERGRADE'),
('FINAL FANTASY XV', 'FINAL FANTASY XV WINDOWS EDITION')]:
with patch.object(imports.urllib.request, 'urlopen') as request:
request.return_value.__enter__.return_value.read.return_value = json.dumps({'items': [{'id': 42, 'name': store}]}).encode()
self.assertEqual(42, imports.identify(dict(self.game, name=folder)))
self.assertIn('cc=US&', request.call_args.args[0])
def test_edition_matching_rejects_ambiguous_titles_and_soundtracks(self):
with patch.object(imports.urllib.request, 'urlopen') as request:
request.return_value.__enter__.return_value.read.return_value = json.dumps({'items': [
{'id': 42, 'name': 'Example WINDOWS EDITION'},
{'id': 43, 'name': 'Example INTERGRADE'},
{'id': 44, 'name': 'Example Soundtrack'},
]}).encode()
self.assertIsNone(imports.identify(self.game))
def test_corrupt_shortcuts_are_preserved(self):
path = self.acct / 'config/shortcuts.vdf'
path.parent.mkdir(parents=True)
original = b'corrupt'
path.write_bytes(original)
listing = self.root / 'games.json'
listing.write_text(json.dumps([self.game]))
run = subprocess.run(['python3', str(BIN / 'bannerlator-steam-shortcuts'), str(self.steam), str(listing)], capture_output=True)
self.assertEqual(1, run.returncode)
self.assertEqual(original, path.read_bytes())
def test_user_shortcuts_survive_owned_promotion(self):
self.snapshot()
path = self.acct / 'config/shortcuts.vdf'
manual = dict(shortcuts.shortcut(self.game), tags={'0': 'Personal'}, AppName='Manual')
path.write_bytes(shortcuts.write({'shortcuts': {'0': manual, '1': shortcuts.shortcut(self.game)}}))
listing = self.root / 'games.json'
listing.write_text(json.dumps([self.game]))
subprocess.run(['python3', str(BIN / 'bannerlator-steam-shortcuts'), str(self.steam), str(listing)], check=True, capture_output=True)
result = shortcuts.parse(path.read_bytes())['shortcuts']
self.assertEqual([manual], list(result.values()))
self.assertEqual({str(self.game['appid']): 42}, json.loads((path.parent / '.droiddeck-routes.json').read_text()))
class SteamProbeTest(unittest.TestCase):
def test_probe_reads_account_and_licenses_and_releases_handles(self):
import ctypes as C
from unittest.mock import Mock
objects, callbacks, calls = [], [], []
def interface(slots):
table = (C.c_void_p * 24)()
for slot, result, args, callback in slots:
fn = C.CFUNCTYPE(result, C.c_void_p, *args)(callback)
callbacks.append(fn)
table[slot] = C.cast(fn, C.c_void_p).value
obj = C.pointer(C.cast(table, C.POINTER(C.c_void_p)))
objects.extend([table, obj])
return C.cast(obj, C.c_void_p).value
user = interface([
(1, C.c_bool, [], lambda _: True),
(2, C.c_uint64, [], lambda _: imports.BASE_ID + 123),
])
apps = interface([(6, C.c_bool, [C.c_uint32], lambda _, appid: appid == 42)])
client = interface([
(0, C.c_int, [], lambda _: 7),
(1, C.c_bool, [C.c_int], lambda _, pipe: calls.append(('pipe', pipe)) or True),
(2, C.c_int, [C.c_int], lambda _, pipe: 9),
(4, None, [C.c_int, C.c_int], lambda _, pipe, handle: calls.append(('user', pipe, handle))),
(5, C.c_void_p, [C.c_int, C.c_int, C.c_char_p], lambda _, handle, pipe, version: user if version == b'SteamUser021' else 0),
(15, C.c_void_p, [C.c_int, C.c_int, C.c_char_p], lambda _, handle, pipe, version: apps if version == b'STEAMAPPS_INTERFACE_VERSION008' else 0),
])
library = Mock()
library.CreateInterface.return_value = client
with patch.object(imports.C, 'CDLL', return_value=library):
result = imports.probe('/Steam', [42, 43])
self.assertEqual('123', result['account'])
self.assertEqual({'42': True, '43': False}, result['owned'])
self.assertEqual([('user', 7, 9), ('pipe', 7)], calls)
library.CreateInterface.assert_called_once_with(b'SteamClient020', None)
if __name__ == '__main__':
unittest.main()