Files
DroidDeck/tools/linuxfs/overlay/usr/local/bin/steam-compatibility
T
MaxsTechReview 49a5f4aa94 ESync, FSync, NTSync + OpenNow + FEX for Desktop Apps (#197)
* Make the Compatible Proton ARM64 the default and label compatible builds

The CompatToolMapping "0" default always points at DroidDeck's Proton
ARM64 tool, and per-game picks of adopted GE/CachyOS builds are kept,
including when the shortcuts writer remaps.

The tool is named after the Proton build it runs, Proton Experimental
ARM64 (Compatible), or Proton 11.0 ARM64 (Compatible) once that newer build
is installed. Adopted builds carry the same Compatible mark, read from a
string resource the app stages into the runtime, and the Proton page
explains which builds have the prefix fixes.

The registrar is renamed steam-compatibility and the tool
droiddeck-proton-arm64. Existing installs migrate on the next session: the
old tool directory is removed, every mapping that named it moves to the new
tool, and the retired bannerlator-steam-compat script is removed from the
runtime. CI and local packaging include the renamed script.

* Drop the duplicate R import left by merging main

* Add userspace ntsync for Proton on Android

Android kernels have no /dev/ntsync and its app policy refuses futex_waitv,
so every Proton 11 build here fell back to wineserver for every wait.
libblsession now serves /dev/ntsync itself: wineserver opens a memfd region,
each sync object is a timerfd that carries its slot, and the kernel ntsync
ioctls are answered in shared memory with futexes, following the kernel
driver's wake order. Valve's Proton Experimental ARM64, GE-Proton and
proton-cachyos use it unchanged.

Locks record their owner so a killed process cannot wedge the others,
signals are held off inside each critical section, dead processes and
exiting threads are swept, and object lifetimes follow close, dup2, dup3,
close_range and received descriptors.

Fast sync is on by default under Performance > Session fixes, and
PROTON_NO_NTSYNC=1 turns it off per game.

* Add droiddeck-esync as the default sync for Proton on Android

Without a kernel ntsync driver, Proton 11 builds here fell back to
wineserver for every wait. droiddeck-esync brings eventfd and shared-memory
sync to the exact Proton build a game runs on: a pack holds that build's
ntdll.so and wineserver with the patch series in tools/droiddeck-esync
applied, and the guest runs Proton from a shadow tree that links the stock
tool and swaps in those two files. A wineserver from a pack still prefers
/dev/ntsync when a kernel driver or droiddeck-ntsync is there.

Packs are built in Valve's SDK image from each release's own wine source
(Proton Experimental ARM64 tags, GE-Proton and proton-cachyos releases); the
control build must reproduce the release binaries byte for byte, and a
signed index lists the packs. The APK bundles the packs pinned in
release.env, the app fetches newer ones, and Steam's compatibility tool
picks the pack for each Proton build, so new Proton releases get packs
without an app update.

The ntdll side reads, writes and polls its eventfds with raw system calls,
so the guest's LD_PRELOAD wrappers stay out of the sync path. The server
abandons a mutex once a thread killed by TerminateThread has really exited
and when its process dies, and a mutex's owner and recursion count change
in one atomic step, so a killed owner cannot touch the next owner's count.

droiddeck-esync is on by default and PROTON_NO_ESYNC=1 turns it off per
game; droiddeck-ntsync, the userspace ntsync, is now an opt-in toggle.

* Watch Valve's Proton releases and build droiddeck-esync packs for them

A scheduled workflow reads the Proton ARM64 builds Steam ships (anonymous
steamcmd app info) and the upstream source tags, compares them with the
previous watch state and starts the pack build for any build that has no
pack yet. The pack build applies the patch series with --fuzz=0 and runs a
smoke test of each new pack against the esstress suite before it is
published.

* Add droiddeck-fsync, keep Steam and the app on one Proton, fix DirectAudio

droiddeck-fsync: libblsession answers futex_waitv(2), which the app
sandbox refuses, in userspace, so Proton's own fsync runs on the stock
Proton files with no pack. Waiters register the words they wait on in a
table beside the fsync shared memory and sleep on a futex of their own;
FUTEX_WAKE on the fsync memory wakes the registered waiters. Slots carry
one owner word that the sweeper and the owner claim with a CAS, and the
mapping table is rebuilt under a lock held across the real munmap, so a
reused address or a recycled slot cannot lose a waiter. It stands in for
a Proton build without a pack (droiddeck-ntsync where fsync is off), takes
over for games with esync off, and the "droiddeck-fsync first" toggle
(BL_FSYNC_FIRST, also per game) puts it ahead of the packs. The games
Proton keeps off fsync and esync never get it. A launch into a prefix
whose wineserver is still running keeps that server's sync choice.

One Proton for Steam and the app: the Components page's Proton is the
default Steam uses, applied live through the client while Steam runs
(droiddeck-steam-compat) and at the next start otherwise; a choice made in
Steam's menu comes back to the app. Requests are ordered by a sequence that
never goes backwards, and the page no longer jumps back to the default
after a swap or restore.

DirectAudio: the wrapper wrote the driver key with its backslashes
stripped, so no game ever used DirectAudio. Wine 11 also loads a builtin
only from the prefix, so the driver is now linked into system32 and
syswow64 before "directaudio,pulse" is selected; mmdevapi falls back to
winepulse whenever DirectAudio cannot start.

* Pick esync, ntsync or fsync from the Steam settings' Games group

A three-way tab row above the FEX preset sets the sync Proton games use. It
writes the existing switches together (droiddeck-ntsync, droiddeck-fsync
first, droiddeck-esync), so the Performance toggles and the tabs always agree,
starts on esync and keeps the choice until it is changed again.

* Make droiddeck-fsync the default sync for Proton games

A fresh install, or one that never picked a sync, starts on fsync; a choice made
in the Steam settings' Games group still sticks. Games Proton keeps off fsync
still fall back to droiddeck-esync.

* Add wineserver to the Wine sync tabs and order them ntsync, fsync, esync, wineserver

wineserver turns every in-process sync off so games wait through wineserver;
picking any other tab turns sync back on. The tabs shrink to fit a narrow pane.

* Shorten the Wine sync description

* Answer openat2 and execveat for FEX in the session preload

FEX opens every file of its x86 rootfs with openat2(RESOLVE_IN_ROOT) and
only falls back to the host path on EXDEV. Under proot on Android openat2
answers ENOSYS, so each lookup of an x86 library silently went to the host
and missed. The exported syscall() now turns openat2 into openat once the
real call reports ENOSYS: RESOLVE_IN_ROOT paths are taken relative to the
root fd (FEX has already resolved the links inside the rootfs), and an
absolute path under RESOLVE_BENEATH fails with EXDEV as the kernel would.

execveat with an absolute path, or relative to AT_FDCWD, and without
AT_EMPTY_PATH or AT_SYMLINK_NOFOLLOW, is plain execve; it is sent as such
because proot refuses execveat with a directory fd, which FEX uses when it
re-executes itself for a child x86 program.

* Run x86 Linux games and programs through FEX

Steam: Linux-only titles that have no ARM64 build (from the client's
appinfo cache: type game, demo, application or beta) are mapped to a new
compatibility tool, droiddeck-fex ("Linux x86 (FEX)", linux to linux,
no required runtime). Valve's own "fex" tool needs pressure-vessel, which
cannot run under proot, so a mapping to it or to a Steam Linux Runtime is
replaced; any other pick the user made stays. Titles that also ship for
Windows stay on Proton. When such a title is installed and FEX or the
sniper runtime is missing, the client is asked to install them.

droiddeck-fex runs the program under the FEX that Steam installs (app
3127680), with Steam Linux Runtime 3.0 (sniper, app 1628350) as the x86
rootfs:
- the sniper platform is linked in place from its usr-mtree (relative
  links only, files/usr -> .), once per runtime version;
- the x86_64 and i386 builds of libblsession and libfakeinput go into the
  runtime's lib/<triplet>/droiddeck and its etc/ld.so.preload, mirroring
  the session's own list, so x86 programs get the pads and the session
  fixes (FEX passes their system calls straight to the kernel, so the
  ARM64 preloads cannot reach them);
- Config.json sets RootFS and the GL and Vulkan thunks, so x86 games draw
  with the device's Turnip and Mesa;
- the FEX preset and the game's own environment from the Steam settings
  apply, and ARM64 entries are dropped from LD_PRELOAD/LD_LIBRARY_PATH.
It also links ~/.steam/sdk64 and sdk32 to the client's x86 steamclient
when the client has one and nothing is there yet.

Desktop: x86_64 and i386 AppImages can be imported now; they unpack
through FEX (or unsquashfs) and record their architecture. Scripts,
AppImages and programs started in a run session use FEX automatically
when they are x86, and each added app has a "Use FEX" Auto/On/Off choice
in its edit dialog. The launchers' "still running" checks also look at
FEX processes, whose exe is FEX rather than the program. A GitHub release
without an ARM64 AppImage falls back to its x86_64 one. Decky's x86
loader starts through droiddeck-fex instead of the fex-mesa rootfs path,
which no runtime ships.

The x86 preloads are built against glibc 2.31 (Debian 11) so they load
in sniper: tools/linuxfs/build-x86-preloads.sh, run in CI and by
build_local.sh. fakeinput's stat hooks fall back to __xstat on a glibc
without a stat export, and mmap64 takes off64_t for the i386 build.

* Keep a FEX unixlib from outliving its DLL

A FEX package or original bundle without the aarch64-unix helpers left
the previous version's libarm64ecfex.so / libwow64fex.so in the Proton,
so the new DLLs loaded a unixlib built for another FEX. A swap now removes
the helpers the package does not carry; FEX then runs DLL-only, which it
already falls back to when Wine cannot load the unixlib.

* Fix the Wine sync choices an audit found

- wineserver tab: it passed Proton's environment through, so a build with
  esync built in (GE, cachyos) still ran esync and a kernel ntsync would
  still be used. It now sets PROTON_NO_ESYNC, PROTON_NO_FSYNC and
  PROTON_NO_NTSYNC (unless the game's own environment sets them).
- ntsync tab: a Proton whose wineserver has no /dev/ntsync client got
  wineserver-only sync although fsync would work. The hash cache records
  whether the wineserver has one; without it the launch goes to
  droiddeck-fsync, and no stand-in or pack turns droiddeck-ntsync on.
- A build with esync built in returned before the per-game fallbacks, so
  a game that turns esync off ran with no in-process sync. It now gets
  droiddeck-fsync, or droiddeck-ntsync where fsync is off too.
- When a pack's proton cannot start and the stock one is used, or the
  selection fails, the prefix's record now says stock, so a second
  launch into the same running prefix no longer picks the pack's ntdll.
- droiddeck-fsync: its mutex is held across fork and re-made in the
  child, a closed or replaced shm fd is forgotten (close, dup2, dup3,
  close_range, closefrom), and a waiter woken while it was leaving on a
  timeout or signal reports the wake instead of losing it.
- BL_SYNC_MIN_NOFILE applies per game to every verb, debug logs carry
  prefixes.json, the device report names the chosen sync instead of the
  three raw switches, and PATCHES.md and the proot notes describe the
  selector, the fsync default and droiddeck-fsync's futex_waitv.

* Name the runtime's scripts and files droiddeck, migrating old installs

The guest scripts are droiddeck-* now (droiddeck-session and the other 19),
as are the files the app and scripts generate (etc/droiddeck, the network
state files, the components dir, the Steam install stamp, the redist marker,
the extra-Proton wrapper and backups) and the compositor's own names
(libdroiddeckwayland, droiddeck_* sources, DROIDDECK_* switches, the
wl_output and seat names).

Installed runtimes carry on: LegacyNames moves etc/bannerlator and
share/bannerlator over and leaves relative links at the old paths before
every proot command, turns any old script into a link to its new name,
moves the generated files and points menu entries at the new scripts.
The session scripts and steam-compatibility move their markers on first
use, the components dir's desired.tsv is rewritten with it, and the
runtime counts as installed under either session script.

Kept: the banner_ahb_v1/banner_desktop_v1 wire names and BANNER_AUDIO_DIRECT
(read by prebuilt Turnip and Wine builds), Banners-Turnip and other
third-party names, BL_* switches, libblsession, and credits to Bannerlator.

* Unpack x86 AppImages without FEX, then ask Steam for FEX

Importing an x86 AppImage failed when FEX was not installed yet ("needs FEX
to unpack and no unsquashfs is installed"). AppImages now unpack with
uruntime (VHSgunzo, MIT, v0.8.1 aarch64, pinned by sha256 and shipped
unmodified with its licence), which reads SquashFS and DwarFS images of any
architecture natively. FEX's own --appimage-extract and unsquashfs stay as
fallbacks, and an ARM64 image whose own runtime cannot unpack it goes the
same way.

An x86 import with FEX missing records that FEX is wanted, so the next
Steam session requests FEX and the Steam Linux Runtime, and the program's
page says FEX is not set up yet and how to get it.

* Run Vulkan Qt Quick AppImages on Wayland

OpenNOW (Qt 6.8, Qt Quick on Vulkan) closed within two seconds in a run
mode. The session sends toolkits to gamescope's Xwayland, and Qt's xcb
backend only presents with Vulkan when Mesa's X11 WSI does, which needs
DRI3; Xwayland on KGSL has none, so Qt found no queue that can present and
aborted ("Failed to create RHI").

An AppImage that bundles Qt Quick, the Vulkan loader and Qt's Wayland
plugin now runs on gamescope's Wayland socket (the desktop's, outside
gamescope), where Turnip presents as it does for games, with Qt's xcb
backend as the fallback. SDL_VIDEODRIVER and XDG_SESSION_TYPE follow, so
helpers that pick their window system from them (OpenNOW's streamer) agree
with Qt. Everything else stays on X11; a qt file in the app's directory
(wayland or xcb) overrides the choice.

* Keep gamescope's WSI layer away from Qt apps on its Wayland socket

OpenNOW still closed at once: its FFmpeg (the Raspberry Pi fork, libavutil
61) creates its Vulkan queues internally synchronized whenever the driver
offers VK_KHR_internally_synchronized_queues, which DD-Turnip does. The
gamescope WSI layer registers every queue of a new device through
vkGetDeviceQueue, which only returns queues created without flags, so it
got VK_NULL_HANDLE and vkroots aborted on assert(obj) before Qt opened a
window.

Programs that draw on gamescope's Wayland socket do not need the layer,
which only carries X11 windows past Xwayland, so it is turned off for
them. OpenNOW bundles no Vulkan loader (it reaches Vulkan through Qt and
dlopen), so a binary that uses QVulkanInstance counts as a Vulkan app too,
and a Qt Quick app left on X11 says why in the session log.

* Keep the end of the session log, and say when FEX is missing

The session sends its output through head -c into session.log. head
writes through stdio, so it held up to 4 KB that proot's --kill-on-exit
threw away when the guest ended: every log stopped mid-line at a 4096-byte
boundary, losing exactly the lines that say why a program quit (an x86
AppImage's "x86 Linux programs need FEX" among them). dd writes each read
as it comes and keeps the same cap.

A run mode always ends with status 0 (gamescope does not pass its child's
on), so a program that could not start just closed. When the log says FEX
is missing, the ended screen now says so and how to get it.

The app reads FEX's readiness from status.json, which only a launch or an
import wrote, so it still said "not set up" after Steam installed FEX.
droiddeck-fex status rewrites it, and a Steam session ends with it while
FEX is wanted. FEX is also looked for at the tool's root and under files/,
and a tool folder without it lists what it holds.

* Let FEX and its runtime install runnable on an SD card library

A Steam library on shared storage loses the executable bits, so native
tools are staged in private directories bound over it; only Proton and
SteamLinuxRuntime folders were. FEX (3127680) installed there could never
run, droiddeck-fex saw no FEX and kept asking Steam for it. FEX and the
sniper runtime (1628350) now get private directories from their first
download, whatever their install folder is called.

* Keep fsync off in the pack smoke test

A runner whose kernel allows futex_waitv started stock Proton with fsync,
so the server-side baseline never said what the test waits for and every
pack was waved through as untestable; the esync run would have measured
fsync too.

* Judge a pack against stock's own esstress failures

Stock Proton fails esstress's dup wait all under server-side sync as well
(a Wine bug, seen on the phone too), and any stock failure made the test
call the runner unusable. A pack now fails only when it crashes, hangs or
fails a check stock passes.

* Ask Steam for FEX and its runtime one at a time

Both steam://install requests were started together, and the running client
took only the first: FEX installed, the sniper runtime FEX needs never did.
Each request now waits for the client to take it before the next.

* Bundle the first published droiddeck-esync packs in the apk

* Log how an x86 program under FEX ends

FEX keeps its own errors quiet unless asked, so a program it could not run
left nothing in the session log. Its log now goes to stderr, and the AppImage
launcher records the program's exit status or the signal that ended it.

* Show the log a failed AppImage program wrote

Programs like Eden stop themselves on a failed check and say why only in
their own log under the home folder. When the program ends badly, the end of
any log it wrote during the run is copied into the session log.

* Report where an x86 program under FEX hit an illegal instruction

A tiny x86_64 preload, built without libc, catches SIGILL and writes the
address, the instruction bytes and the mapping they belong to before the
program dies as it would have. droiddeck-fex preloads it into x86_64 programs.

* Load the fault reporter from the x86 runtime

Programs under FEX look libraries up in the x86 runtime, so the reporter is
copied there with the other preloads and named by its path inside it.

* Hand FEX its x86 runtime and thunks through the environment

Valve's FEX runs portable and reads its config beside itself, so the
Config.json droiddeck-fex writes was not seen: programs ran without the
runtime. The runtime, thunk libraries and thunk list now also go in FEX's
own environment variables, and each launch says what it was given.

* Use the sync pack built for the installed Proton release

Some Proton releases ship byte-identical ntdll.so and wineserver (Valve
11.0-2b/2c, CachyOS 0702/0703), so a pack built for a newer release
matched an older install exactly and the older install never fetched its
own pack. A pack from a newer release no longer counts as an exact match;
among matches the one built for the installed version wins, then rev.

The app ranks the pack a tool already uses the same way and only fetches
a pack that ranks higher, reads the release epoch from tools.tsv and
refetches when tools.tsv changes. A prefix whose server is still running
re-checks its recorded pack on waitforexitandrun and starts fresh when a
better one now applies.

* Keep published sync packs and revocations consistent

The publish step never replaces a pack that has an entry beside it; a
pack left without its entry by an interrupted run is replaced so the
build stops repeating. Discovery counts revocations the signed index does
not show yet, including superseded packs only the flavor releases list,
so the index is signed again. The release watch always builds every
flavor.

* Fetch the droiddeck-esync packs from DroidDeck-Components

The packs, the signed index and the apk bundle now live in the releases
of Droid-Deck/DroidDeck-Components, together with the pipeline that
builds them, so this repository keeps only the app and the guest code
that consumes them. The app's index and asset URLs point there,
release.env names the repository its bundle comes from, and the apk
builds drop bundled packs that the published index revokes.

* Stop the apk build when the published sync index cannot be read

* Ask Steam for one FEX dependency at a time

The client shows a confirmation dialog for each steam://install, and a
second request replaces the first dialog, so FEX was never installed
when the runtime was requested right after it. The next dependency is
now requested once the previous one has an app manifest.

* Use generic names in the AppImage test fixtures

* Give x86-64 apps under FEX the Vulkan thunk

Apps that bring their own glibc and Vulkan loader skipped FEX's thunk
overlay and loaded their bundled desktop Mesa drivers, which crash on the
phone's GPU during Vulkan setup. droiddeck-fex now adds an x86-64 audit
module when a program's bundled glibc and libstdc++ meet the thunk's
version needs. The module sends libvulkan.so.1 lookups to a nodelete stub
that needs FEX's thunk path. That keeps the thunk mapped when an app
unloads Vulkan and loads it again, since FEX keeps its first trampoline
links and the old addresses get reused.

The fault reporter now restores the default action before re-raising,
because FEX ignores SA_RESETHAND and the handler looped.
2026-10-04 12:43:57 -04:00

1113 lines
45 KiB
Python
Executable File

#!/usr/bin/python3
# Makes the ARM64 Proton the client already downloaded usable here, and sets it as the default tool
# for Windows titles (CompatToolMapping "0" in config.vdf) when the user has not chosen one. Run
# before the client starts; it rewrites config.vdf on exit.
#
# steam-compatibility <steam root>
#
# Valve ships a native aarch64 Proton (arm64ec Wine with FEX as the x86 layer) as an ordinary depot,
# which is exactly what this runtime wants. Its toolmanifest declares require_tool_appid 4185400 -
# Steam Linux Runtime 4 for arm64 - so Steam stacks pressure-vessel underneath it, and
# pressure-vessel needs unprivileged user namespaces, which an Android app does not get. Steam then
# drops the launch on the floor: no window, no log, straight back to the library.
#
# So the depot is re-registered here as a compatibility tool of our own: every entry symlinked so
# nothing is copied and Steam still updates the real depot, but with a toolmanifest that leaves out
# require_tool_appid. Proton takes its base directory from dirname(sys.argv[0]) without resolving
# it, so it reads files/ through those symlinks and runs directly on this rootfs.
import json
import os
import re
import runpy
import shutil
import sys
TOOL = "droiddeck-proton-arm64"
TOOL_11 = "droiddeck-proton-11-arm64"
LEGACY_TOOL = "bannerlator-proton-arm64"
LABEL_FILE = "/etc/steam-compatibility-label"
# The ARM64 Proton depot the client downloads for itself, as a compatibility-tool appid.
# The session asks the client to install it on the command line the first time (steam://install),
# so a new client has it before a Windows title is first launched. It is deliberately NOT named as
# this tool's dependency: a dependency is composed with its own dependencies, and this depot
# requires 4185400, the arm64 Steam Linux Runtime, whose container needs user namespaces Android
# does not give - the launch would be wrapped in it and die before this tool ever ran.
PROTON_ARM64_APPID = "4427310"
LAUNCHER = "droiddeck-proton"
LEGACY_LAUNCHER = "bannerlator-proton"
# The extra Steam library the app mounts the installed games into (see droiddeck-steam-library).
# installed_apps() scans it too; it lives only in the sibling script, so define it here as well.
LIBRARY = "/mnt/droiddeck" # the card library is LIBRARY + "-sd"; internal storage is steam_root itself
# The client lists its own "Proton Experimental (ARM64)" and "Proton 11.0 (ARM64)" in the same
# Compatibility dropdown, and those are the two that cannot start a game here. Ours has to be
# unmistakable at a glance, or a user picks a neighbour and gets a launch that fails silently.
# Newest first: whichever the client has downloaded is used.
SOURCES = ("Proton Experimental (ARM64)", "Proton 11.0 (ARM64)")
OWN_TOOLS = ((TOOL, SOURCES), (TOOL_11, ("Proton 11.0 (ARM64)",)))
FEX_TOOL = "droiddeck-fex"
FEX_LAUNCHER = "droiddeck-fex-tool"
FEX_FILES = ("toolmanifest.vdf", "compatibilitytool.vdf", FEX_LAUNCHER)
FEX_APPID = "3127680"
SNIPER_APPID = "1628350"
FEX_DEPENDENCIES = (FEX_APPID, SNIPER_APPID)
REQUEST_HANDOFF_SECONDS = 30
PLAYABLE_TYPES = frozenset(("game", "demo", "application", "beta"))
COMPAT_DIR = os.path.join(os.environ.get("HOME", "/root"), ".local", "share", "droiddeck-compat")
# Written by us; everything else in the depot is symlinked through.
OWN_FILES = ("toolmanifest.vdf", "compatibilitytool.vdf", LAUNCHER)
# The client assigns a Proton to a Windows title by itself, as a per-app CompatToolMapping entry at
# priority 250 - which outranks the "0" default, so setting that default alone changes nothing. Every
# Proton it can pick is one this runtime cannot start: the ARM64 ones want pressure-vessel (see
# above) and the x86_64 ones want pressure-vessel and FEX on top. Ours is the only one that runs
# here, so every Proton mapping is repointed at it. Tools that are not Proton are left alone.
COMPAT_TOOL_VDF = """"compatibilitytools"
{
"compat_tools"
{
"%s"
{
"install_path" "."
"display_name" "%s"
"from_oslist" "windows"
"to_oslist" "linux"
}
}
}
"""
TOOL_MANIFEST_VDF = """"manifest"
{
"version" "2"
"commandline" "/%s %%verb%%"
"use_sessions" "1"
"compatmanager_layer_name" "proton"
}
""" % LAUNCHER
# Steam runs its own graphics probe through EVERY registered tool at once, ~40 s after logging in,
# all with STEAM_COMPAT_DATA_PATH=.../compatdata/0, and those concurrent Protons can write their
# builtin DLLs through each other's prefix links into each other's trees (seen: GE's FEX DLLs became
# Proton Experimental's). Moving those runs to a prefix per tool was tried and crashed the Steam
# client right after the probe started (segfault, rc 139, first ever in 25 sessions), so the client
# relies on compatdata/0 itself. The launch-time check below is the protection instead.
# The Components page's choices, enforced in the one place every game launch passes: the app keeps
# desired.tsv (tool dir, Proton build, component, file, sha256, source copy) and an unpacked copy of
# each package in use. A file that no longer matches - whatever changed it - is put back before
# Proton starts. Only for the Proton build the choice was made for: after a Steam update the build
# differs and the Proton's own new files are left alone until the user chooses again.
BL_COMPONENTS_SETUP = """
bl_components() {
base=$1
cm_dir="${HOME:-/root}/.local/share/droiddeck-components"
cm_list="$cm_dir/desired.tsv"
# A real game launch only: the verb the client starts a game with and the game's own prefix. Every
# other run - above all the client's own probes right after login, which all share compatdata/0 -
# is left exactly as it was: with this check running through them, the client segfaulted (rc 139)
# within seconds of starting its probes, four sessions out of four. The prefix decides rather than
# the app id because a non-Steam shortcut launches with app id 0 but still gets a prefix of its own.
[ "${verb:-}" = waitforexitandrun ] || return 0
case "${STEAM_COMPAT_DATA_PATH:-}" in ''|*/compatdata/0|*/compatdata/0/|*/compatdata/0-*) return 0 ;; esac
[ -f "$cm_list" ] || return 0
cm_app=${STEAM_COMPAT_DATA_PATH%/}
cm_app=${cm_app##*/}
cm_tool=${base##*/}
cm_build=$(awk 'NR == 1 { print $2 }' "$base/version" 2>/dev/null)
cm_fixed=0
cm_checked=0
while IFS=$'\\t' read -r t want comp rel sum src; do
[ "$t" = "$cm_tool" ] && [ "$want" = "$cm_build" ] || continue
cm_checked=$((cm_checked + 1))
f="$base/$rel"
if [ -f "$f" ] && [ "$(sha256sum "$f" | cut -d' ' -f1)" = "$sum" ]; then continue; fi
if [ ! -f "$src" ]; then
echo "droiddeck-proton: components: $comp source missing ($src), left as it is" >&2
continue
fi
mkdir -p "$(dirname "$f")"
cp -f "$src" "$f.bl-new" && mv -f "$f.bl-new" "$f" || continue
case "$rel" in *.so) chmod 755 "$f" ;; *) chmod 644 "$f" ;; esac
cm_fixed=$((cm_fixed + 1))
done < "$cm_list"
[ "$cm_checked" -gt 0 ] && echo "droiddeck-proton: components: checked $cm_checked file(s) in $cm_tool for app $cm_app" >&2
if [ "$cm_fixed" -gt 0 ]; then
echo "droiddeck-proton: components: re-applied $cm_fixed of $cm_checked file(s) in $cm_tool before this launch" >&2
printf '%s\\t%s\\t%s\\n' "$(date +%s)" "$cm_tool" "$cm_fixed" >> "$cm_dir/reapplied.log"
fi
}
"""
# Resolves the depot at launch time, so the tool can be registered before the depot exists and
# survives the client updating or replacing it. Proton takes its base directory from
# dirname(sys.argv[0]) without resolving it, so it is run from the depot itself.
# Cores for a game the client launches, kept separate from the client's own. One mask suits the
# client's menus and another suits a game, and they are wanted at the same time - so the game's is
# applied here, in the launch path every game goes through, rather than to the session as a whole.
BL_GAME_CPUS_SETUP = """
bl_game_cpus() {
[ -n "${BL_GAME_CPUS:-}" ] || return 0
command -v taskset >/dev/null 2>&1 || return 0
# exec'd through taskset rather than set afterwards: a mask applied to a pid races the process
# into spawning its own threads, and this way every thread inherits it from the first instruction.
BL_TASKSET="taskset -c $BL_GAME_CPUS"
echo "droiddeck-proton: game cores $BL_GAME_CPUS" >&2
}
"""
# Quake III engine games (id Tech 3) set their own resolution and switch the display to it. Under
# gamescope with the ARM64 Proton that switch is refused ("calling CDS: failed, bad mode") and the
# game never gets OpenGL; left at its own size in a window, it draws small in a corner. So these
# titles are told to stay windowed at the session's own size - unless the user's launch options
# already say something about the video mode, which are left as they are.
# The session puts the client's own libraries (steamrtarm64, its panorama/) ahead of everything for
# the client, and a game inherits that: Wine then resolved gnutls, SDL, FFmpeg and the rest against
# the client's private copies instead of the rootfs it was built for. Inside Steam's runtime
# container that path never reaches Proton either. (WinNative's winnative-proton-launch.)
BL_LIBPATH_SETUP = """
bl_libpath() {
# A Proton built to prefer PipeWire (CachyOS's among the adopted ones) hung its audio thread
# here, where only PulseAudio answers. (WinNative, winnative-proton-launch.)
export PROTON_USE_PIPEWIRE=${PROTON_USE_PIPEWIRE:-0}
[ -n "${LD_LIBRARY_PATH:-}" ] || return 0
local kept= dir old_ifs=$IFS
IFS=:
for dir in $LD_LIBRARY_PATH; do
case $dir in
"" | */steamrtarm64 | */steamrtarm64/*) ;;
*) kept=${kept:+$kept:}$dir ;;
esac
done
IFS=$old_ifs
if [ -n "$kept" ]; then export LD_LIBRARY_PATH="$kept"; else unset LD_LIBRARY_PATH; fi
}
"""
BL_IDTECH3_SETUP = """
bl_idtech3() {
BL_EXTRA_ARGS=()
case "${SteamAppId:-${STEAM_COMPAT_APP_ID:-}}" in
# Quake III Arena, Quake III: Team Arena, Return to Castle Wolfenstein, Jedi Academy.
2200|2350|9010|6020) ;;
*) return 0 ;;
esac
case " $* " in
*r_fullscreen*|*r_mode*|*r_customwidth*|*r_customheight*)
echo "droiddeck-proton: Quake-engine title; its launch options set the video mode, left as they are" >&2
return 0 ;;
esac
w=${BL_WIDTH:-1280}
h=${BL_HEIGHT:-720}
BL_EXTRA_ARGS=(+set r_fullscreen 0 +set r_mode -1 +set r_customwidth "$w" +set r_customheight "$h")
echo "droiddeck-proton: Quake-engine title: windowed at ${w}x${h}" >&2
}
"""
BL_DIRECTAUDIO_SETUP = """
bl_directaudio() {
base=$1
[ -n "${BL_DIRECTAUDIO:-}" ] || return 0
[ -d "$BL_DIRECTAUDIO/lib/wine" ] || return 0
version=$("$base/files/bin-arm64/wine" --version 2>/dev/null)
major=${version#wine-}
major=${major%%.*}
if [ "$major" != "11" ]; then
echo "droiddeck-proton: DirectAudio left off - it is built for Wine 11 and this Proton is '${version:-unknown}'. Pairing them would be silent rather than broken, so it is not worth guessing." >&2
return 0
fi
# Wine appends the per-architecture subdirectory to each entry, so the directory holding
# aarch64-unix/ and aarch64-windows/ is what goes here.
WINEDLLPATH="$BL_DIRECTAUDIO/lib/wine${WINEDLLPATH:+:$WINEDLLPATH}"
export WINEDLLPATH
# mmdevapi only tries the drivers named in the registry; its built-in list is pulse, alsa, oss and
# coreaudio and knows nothing about this one. The key is appended to the prefix's user.reg rather
# than written with `wine reg`, which would start a wineserver here just to set one value.
pfx="${STEAM_COMPAT_DATA_PATH:-}/pfx"
reg="$pfx/user.reg"
if [ -f "$reg" ]; then
for pair in system32:aarch64-windows syswow64:i386-windows; do
dir="$pfx/drive_c/windows/${pair%%:*}"
src="$BL_DIRECTAUDIO/lib/wine/${pair#*:}/winedirectaudio.drv"
if [ -d "$dir" ] && [ -f "$src" ] && [ "$(readlink "$dir/winedirectaudio.drv" 2>/dev/null)" != "$src" ]; then
ln -sfn "$src" "$dir/winedirectaudio.drv.bl-new" && mv -f "$dir/winedirectaudio.drv.bl-new" "$dir/winedirectaudio.drv"
fi
done
key='[Software\\\\Wine\\\\Drivers]'
if [ "$(readlink "$pfx/drive_c/windows/system32/winedirectaudio.drv" 2>/dev/null)" != "$BL_DIRECTAUDIO/lib/wine/aarch64-windows/winedirectaudio.drv" ]; then
echo "droiddeck-proton: DirectAudio could not be placed in the prefix; Proton's own audio stays" >&2
elif ! BL_KEY="$key" awk 'index($0, "[") == 1 { inside = index($0, ENVIRON["BL_KEY"] " ") == 1 } inside && index($0, "\\"Audio\\"=") == 1 { last = $0 } END { exit last != "\\"Audio\\"=\\"directaudio,pulse\\"" }' "$reg" 2>/dev/null; then
printf '\\n%s %s\\n"Audio"="directaudio,pulse"\\n' "$key" "$(date +%s)" >> "$reg"
echo "droiddeck-proton: DirectAudio selected in the prefix" >&2
fi
else
# A prefix that does not exist yet is created by Proton moments from now, after this runs, so
# the key lands on the NEXT launch of that game. Said out loud because silent audio on a first
# launch would otherwise look like a fault.
echo "droiddeck-proton: no prefix yet - DirectAudio takes effect the next time this game starts" >&2
fi
echo "droiddeck-proton: DirectAudio ready (Wine $major)" >&2
}
"""
# Resolves the depot at launch time, so the tool can be registered before the depot exists and
# survives the client updating or replacing it. Proton takes its base directory from
# dirname(sys.argv[0]) without resolving it, so it is run from the depot itself.
LAUNCHER_SH = """#!/bin/bash
# DroidDeck's Proton ARM64: runs the ARM64 Proton the Steam client downloaded, without the
# Steam Linux Runtime container its own manifest asks for (no user namespaces on Android).
verb=$1; shift
steam_root=${STEAM_COMPAT_CLIENT_INSTALL_PATH:-${XDG_DATA_HOME:-$HOME/.local/share}/Steam}
# Steam's overlay is preloaded into every game Valve launches, and used to be dropped here: it was
# landing in front of open() and read() on /dev/input, letting Wine open the session's pad and
# answering every read of it with nothing - a controller that never reports, and Wine's HID thread
# spinning a core on it.
#
# Removing it was the wrong cure for an ordering problem. glibc processes LD_PRELOAD BEFORE
# /etc/ld.so.preload, and the interposer lives in the latter, so the overlay was simply ahead of it.
# Naming the interposer first in LD_PRELOAD puts it back in front and the overlay loads as Valve
# intends. That matters beyond the overlay itself: every deviation from the launch Valve ships is a
# difference this runtime would otherwise have to justify.
for fake in /usr/local/lib/libfakeinput.so /usr/local/lib/libblsession.so; do
if [ -f "$fake" ]; then
case ":${LD_PRELOAD:-}:" in
*":$fake:"*) ;;
*) LD_PRELOAD="$fake${LD_PRELOAD:+:$LD_PRELOAD}"; export LD_PRELOAD ;;
esac
fi
done
# The depot this tool is named after, newest across the libraries that hold it; the others in the
# list stand in only when it is missing.
depot=
for name in %s; do
best=0
for library in "$steam_root" /mnt/droiddeck-sd; do
candidate="$library/steamapps/common/$name"
[ -d "$candidate/files/bin-arm64" ] && [ -x "$candidate/proton" ] || continue
build=0
read -r build _ 2>/dev/null < "$candidate/version"
case $build in "" | *[!0-9]*) build=0 ;; esac
if [ -z "$depot" ] || [ "$build" -gt "$best" ]; then
depot=$candidate
best=$build
fi
done
[ -n "$depot" ] && break
done
if [ -n "$depot" ]; then
bl_components "$depot"
bl_directaudio "$depot"
bl_game_cpus
bl_idtech3 "$@"
bl_libpath
exec ${BL_TASKSET:-} /usr/local/bin/droiddeck-game-env "$depot/proton" "$verb" "$@" "${BL_EXTRA_ARGS[@]}"
fi
echo "droiddeck-proton: no ARM64 Proton depot yet - the client downloads one as this tool's dependency; retry the launch once it has" >&2
exit 1
"""
LAUNCHER_TEMPLATE = LAUNCHER_SH
def launcher_sh(sources=SOURCES):
# Spliced in after the formatting, never through it: the function body contains ${major%%.*}
# and a % format would eat that into a single %, leaving a shell expansion that silently does
# something else.
return (LAUNCHER_TEMPLATE % " ".join('"%s"' % n for n in sources)).replace(
"verb=$1; shift", "verb=$1; shift\n" + BL_DIRECTAUDIO_SETUP + BL_GAME_CPUS_SETUP + BL_IDTECH3_SETUP
+ BL_COMPONENTS_SETUP + BL_LIBPATH_SETUP, 1)
LAUNCHER_SH = launcher_sh()
# A third-party Proton dropped into compatibilitytools.d - GE-Proton, proton-cachyos, anything of
# that shape - is adopted rather than used as shipped, for the same two reasons Valve's ARM64 depot
# is: its manifest declares require_tool_appid, and the client's overlay library has to come out of
# LD_PRELOAD. Its own entry point is left untouched and called through this wrapper.
EXTRA_WRAPPER = "droiddeck-proton-wrap"
LEGACY_EXTRA_WRAPPER = "bannerlator-proton-wrap"
ORIGINAL_SUFFIX = ".droiddeck-orig"
LEGACY_ORIGINAL_SUFFIX = ".bannerlator-orig"
EXTRA_MANIFEST_VDF = """"manifest"
{
"version" "2"
"commandline" "/%s %%verb%%"
"use_sessions" "1"
"compatmanager_layer_name" "proton"
}
""" % EXTRA_WRAPPER
# %s is the tool's own entry point, read out of the manifest it shipped with.
EXTRA_WRAPPER_SH = """#!/bin/bash
# DroidDeck: runs a Proton installed into compatibilitytools.d without the Steam Linux Runtime
# container its own manifest asks for - pressure-vessel needs unprivileged user namespaces, which
# an Android app does not get, and the launch dies before Proton is reached.
verb=$1; shift
here=$(dirname "$0")
# See droiddeck-proton: the interposer goes first in LD_PRELOAD rather than the overlay being
# removed, because glibc reads LD_PRELOAD before /etc/ld.so.preload and the overlay was simply
# ahead of it on /dev/input.
for fake in /usr/local/lib/libfakeinput.so /usr/local/lib/libblsession.so; do
if [ -f "$fake" ]; then
case ":${LD_PRELOAD:-}:" in
*":$fake:"*) ;;
*) LD_PRELOAD="$fake${LD_PRELOAD:+:$LD_PRELOAD}"; export LD_PRELOAD ;;
esac
fi
done
bl_components "$here"
bl_directaudio "$here"
bl_game_cpus
bl_idtech3 "$@"
bl_libpath
# Proton takes its base directory from dirname(sys.argv[0]) without resolving it, so it has to be
# started from its own tree.
exec ${BL_TASKSET:-} /usr/local/bin/droiddeck-game-env "$here/%s" "$verb" "$@" "${BL_EXTRA_ARGS[@]}"
"""
EXTRA_COMPAT_TOOL_VDF = """"compatibilitytools"
{
"compat_tools"
{
"%s"
{
"install_path" "."
"display_name" "%s"
"from_oslist" "windows"
"to_oslist" "linux"
}
}
}
"""
FEX_COMPAT_TOOL_VDF = """"compatibilitytools"
{
"compat_tools"
{
"%s"
{
"install_path" "."
"display_name" "%s"
"from_oslist" "linux"
"to_oslist" "linux"
}
}
}
"""
FEX_MANIFEST_VDF = """"manifest"
{
"version" "2"
"commandline" "/%s %%verb%%"
"use_tool_subprocess_reaper" "1"
}
""" % FEX_LAUNCHER
FEX_LAUNCHER_SH = """#!/bin/bash
verb=$1; shift
case $verb in
getcompatpath | getnativepath) printf '%s\\n' "$1"; exit 0 ;;
esac
exec /usr/local/bin/droiddeck-fex steam "$verb" "$@"
"""
TOKEN = re.compile(r'\s*(?:"((?:[^"\\]|\\.)*)"|([{}])|//[^\n]*)')
def tokenize(text):
tokens = []
pos = 0
while pos < len(text):
m = TOKEN.match(text, pos)
if not m or m.end() == pos:
break
pos = m.end()
if m.group(2):
tokens.append(m.group(2))
elif m.group(1) is not None:
tokens.append('"' + m.group(1) + '"')
return tokens
def find_block(tokens, path):
"""Index of the '{' opening the block at path, or None."""
depth, want, i = 0, 0, 0
while i < len(tokens):
t = tokens[i]
if t == "{":
depth += 1
elif t == "}":
depth -= 1
if depth < want:
return None
elif depth == want and i + 1 < len(tokens) and tokens[i + 1] == "{":
if t.lower() == '"%s"' % path[want].lower():
if want == len(path) - 1:
return i + 1
want += 1
i += 1
return None
def block_end(tokens, start):
depth = 0
for i in range(start, len(tokens)):
if tokens[i] == "{":
depth += 1
elif tokens[i] == "}":
depth -= 1
if depth == 0:
return i
return len(tokens)
def serialize(tokens):
out, depth, i = [], 0, 0
while i < len(tokens):
t = tokens[i]
if t == "}":
depth -= 1
out.append("\t" * depth + "}")
i += 1
elif i + 1 < len(tokens) and tokens[i + 1] == "{":
out.append("\t" * depth + t)
out.append("\t" * depth + "{")
depth += 1
i += 2
else:
out.append("\t" * depth + t + "\t\t" + tokens[i + 1])
i += 2
return "\n".join(out) + "\n"
def write_changed(path, text, mode=None):
"""Writes `text` to `path` only when it differs, through a rename so a reader never sees half a
file. The session runs this script every 15 s for as long as the client does; rewriting files
that have not changed cost a python start's worth of writes each time, and config.vdf is the
client's own, which it may be writing at that moment. Returns whether anything was written."""
try:
with open(path, encoding="utf-8", errors="replace") as f:
same = f.read() == text
except OSError:
same = False
if not same:
tmp = path + ".droiddeck"
with open(tmp, "w", encoding="utf-8") as f:
f.write(text)
if mode is not None:
os.chmod(tmp, mode)
os.replace(tmp, path)
elif mode is not None and (os.stat(path).st_mode & 0o7777) != mode:
os.chmod(path, mode)
return not same
def display_name(base):
try:
with open(LABEL_FILE, encoding="utf-8") as f:
label = f.read().strip().replace('"', "'").replace("\\", "")
except OSError:
label = ""
return "%s (%s)" % (base, label) if label else base
def first_key(path, section):
"""The single key naming a block inside `section`, or None. Used to keep a third-party tool's
own internal name, so a mapping the user already made against it keeps working."""
try:
with open(path, encoding="utf-8", errors="replace") as f:
tokens = tokenize(f.read())
except OSError:
return None
block = find_block(tokens, section)
if block is None:
return None
for i in range(block + 1, block_end(tokens, block)):
if tokens[i] == "}":
break
if i + 1 < len(tokens) and tokens[i + 1] == "{":
return tokens[i].strip('"')
return None
def manifest_entry_point(path):
"""The script a toolmanifest's commandline runs, relative to the tool directory."""
try:
with open(path, encoding="utf-8", errors="replace") as f:
tokens = tokenize(f.read())
except OSError:
return None
for i in range(len(tokens) - 1):
if tokens[i].lower() == '"commandline"':
command = tokens[i + 1].strip('"').split()
if command:
return command[0].lstrip("/")
return None
def adopt_extras(tools):
"""Makes every other Proton in compatibilitytools.d launchable here, and returns the tool names
to leave alone in CompatToolMapping - the whole point of installing one is to pick it per game,
so register_default must not repoint it back at ours."""
adopted = {}
try:
entries = sorted(os.listdir(tools))
except OSError:
return adopted
for entry in entries:
if entry in dict(OWN_TOOLS) or entry == FEX_TOOL or entry.startswith("."):
continue
tool_dir = os.path.join(tools, entry)
manifest = os.path.join(tool_dir, "toolmanifest.vdf")
if not os.path.isdir(tool_dir) or not os.path.isfile(manifest):
continue
# The shipped manifest is kept, once, and stays the source of the entry point: re-running
# must not read back our own rewritten commandline and wrap the wrapper.
original = manifest + ORIGINAL_SUFFIX
legacy = manifest + LEGACY_ORIGINAL_SUFFIX
if not os.path.isfile(original) and os.path.isfile(legacy):
try:
os.replace(legacy, original)
except OSError:
continue
if not os.path.isfile(original):
try:
shutil.copy2(manifest, original)
except OSError:
continue
entry_point = manifest_entry_point(original)
if not entry_point or entry_point in (EXTRA_WRAPPER, LEGACY_EXTRA_WRAPPER):
entry_point = "proton"
if not os.path.exists(os.path.join(tool_dir, entry_point)):
print("steam-compatibility: %s has no %s; left alone" % (entry, entry_point))
continue
compat_vdf = os.path.join(tool_dir, "compatibilitytool.vdf")
name = first_key(compat_vdf, ["compatibilitytools", "compat_tools"]) or entry
wrapper = os.path.join(tool_dir, EXTRA_WRAPPER)
try:
# Formatted first, spliced second: the function carries a printf %s and a
# ${major%%.*} of its own, and putting it through the format would eat both.
body = (EXTRA_WRAPPER_SH % entry_point).replace(
'here=$(dirname "$0")',
'here=$(dirname "$0")\n' + BL_DIRECTAUDIO_SETUP + BL_GAME_CPUS_SETUP + BL_IDTECH3_SETUP
+ BL_COMPONENTS_SETUP + BL_LIBPATH_SETUP, 1)
write_changed(wrapper, body, 0o755)
write_changed(manifest, EXTRA_MANIFEST_VDF)
write_changed(compat_vdf, EXTRA_COMPAT_TOOL_VDF % (name, display_name(entry)))
legacy_wrapper = os.path.join(tool_dir, LEGACY_EXTRA_WRAPPER)
if os.path.isfile(legacy_wrapper):
os.unlink(legacy_wrapper)
except OSError as e:
print("steam-compatibility: could not adopt %s (%s)" % (entry, e))
continue
adopted[name] = entry
return adopted
# Steam's own tools and runtimes live in steamapps beside the games; they are not titles and must
# not be mapped to a compatibility tool.
NOT_GAMES = frozenset((
"228980", # Steamworks Common Redistributables
"1070560", # Steam Linux Runtime 1.0 (scout)
"1391110", # Steam Linux Runtime 2.0 (soldier)
"1628350", # Steam Linux Runtime 3.0 (sniper)
"1493710", # Proton Experimental
"3127680", # FEX
"4183110", # Steam Linux Runtime 4.0
"4185400", # Steam Linux Runtime 4.0 - Arm64
"4427310", # Proton Experimental (ARM64)
"4628740", # Proton 11.0 (ARM64)
))
def installed_apps(steam_root):
"""App ids of the titles the client has installed, tools and runtimes excluded."""
apps = set()
for library in (steam_root, LIBRARY + "-sd"):
steamapps = os.path.join(library, "steamapps")
try:
names = os.listdir(steamapps)
except OSError:
continue
for name in names:
m = re.fullmatch(r"appmanifest_(\d+)\.acf", name)
if m and m.group(1) not in NOT_GAMES:
apps.add(m.group(1))
return sorted(apps, key=int)
APPINFO_CACHE = {}
def appinfo_platforms(path, wanted):
import struct
try:
stamp = os.stat(path).st_mtime_ns
except OSError:
return {}
cached = APPINFO_CACHE.get(path)
if cached and cached[0] == stamp and wanted <= cached[1].keys() | cached[2]:
return {app: cached[1][app] for app in wanted if app in cached[1]}
try:
with open(path, "rb") as f:
data = f.read()
except OSError:
return {}
if len(data) < 8:
return {}
magic = struct.unpack_from("<I", data, 0)[0]
if magic not in (0x07564427, 0x07564428, 0x07564429):
return {}
offset = 8
strings = None
if magic == 0x07564429:
table = struct.unpack_from("<q", data, offset)[0]
offset += 8
count = struct.unpack_from("<I", data, table)[0]
position = table + 4
strings = []
for _ in range(count):
end = data.index(b"\0", position)
strings.append(data[position:end].decode("utf-8", "replace"))
position = end + 1
header = 4 + 4 + 8 + 20 + 4 + (20 if magic >= 0x07564428 else 0)
def section(position):
result = {}
while True:
kind = data[position]
position += 1
if kind == 8:
return result, position
if strings is not None:
key = strings[struct.unpack_from("<I", data, position)[0]]
position += 4
else:
end = data.index(b"\0", position)
key = data[position:end].decode("utf-8", "replace")
position = end + 1
if kind == 0:
result[key], position = section(position)
elif kind == 1:
end = data.index(b"\0", position)
result[key] = data[position:end].decode("utf-8", "replace")
position = end + 1
elif kind == 2:
result[key] = struct.unpack_from("<i", data, position)[0]
position += 4
elif kind == 7:
result[key] = struct.unpack_from("<Q", data, position)[0]
position += 8
else:
raise ValueError("unknown appinfo field type %d" % kind)
found = {}
try:
while offset + 8 <= len(data):
app, size = struct.unpack_from("<II", data, offset)
if app == 0:
break
start = offset + 8
offset = start + size
if str(app) not in wanted:
continue
info = section(start + header)[0].get("appinfo", {})
common = info.get("common", {}) if isinstance(info, dict) else {}
oslist = {part.strip().lower() for part in str(common.get("oslist", "")).split(",") if part.strip()}
found[str(app)] = (oslist, str(common.get("osarch", "")).lower(), str(common.get("type", "")).lower())
except (ValueError, IndexError, struct.error):
return {}
APPINFO_CACHE[path] = (stamp, found, set(wanted))
return {app: found[app] for app in wanted if app in found}
def native_targets(steam_root, apps):
wanted = {str(app) for app in apps if str(app) not in NOT_GAMES}
if not wanted:
return {}
targets = {}
for app, (oslist, osarch, kind) in appinfo_platforms(os.path.join(steam_root, "appcache", "appinfo.vdf"), wanted).items():
if kind not in PLAYABLE_TYPES or "windows" in oslist or "linux" not in oslist:
continue
if "arm64" in osarch or "aarch64" in osarch:
continue
targets[app] = FEX_TOOL
return targets
def linux_runtime_tool(name):
low = name.lower()
return low == "fex" or low.startswith("steamlinuxruntime")
def fex_missing(steam_root):
missing = []
for app in FEX_DEPENDENCIES:
if not any(os.path.isfile(os.path.join(library, "steamapps", "appmanifest_%s.acf" % app))
for library in (steam_root, LIBRARY + "-sd")):
missing.append(app)
return missing
def fex_wanted_marker():
return os.path.join(os.environ.get("HOME", "/root"), ".config", "droiddeck", "fex-wanted")
def request_fex(steam_root, asked):
import subprocess
client = os.path.join(steam_root, "steamrtarm64", "steam")
missing = fex_missing(steam_root)
if not os.access(client, os.X_OK) or any(app in asked for app in missing):
return []
for app in missing:
asked.add(app)
try:
process = subprocess.Popen([client, "steam://install/%s" % app], stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL, start_new_session=True)
except OSError:
continue
try:
process.wait(timeout=REQUEST_HANDOFF_SECONDS)
except subprocess.TimeoutExpired:
pass
return [app]
return []
def read_json(path):
try:
with open(path, encoding="utf-8") as f:
value = json.load(f)
except (OSError, ValueError):
return {}
return value if isinstance(value, dict) else {}
def write_json(path, value):
os.makedirs(os.path.dirname(path), exist_ok=True)
return write_changed(path, json.dumps(value, indent=1, sort_keys=True) + "\n")
class locked:
def __init__(self, directory=None):
self.path = os.path.join(directory or COMPAT_DIR, "lock")
def __enter__(self):
import fcntl
os.makedirs(os.path.dirname(self.path), exist_ok=True)
self.fd = os.open(self.path, os.O_RDWR | os.O_CREAT, 0o644)
fcntl.flock(self.fd, fcntl.LOCK_EX)
return self
def __exit__(self, *exc):
os.close(self.fd)
def state_path(name, directory=None):
return os.path.join(directory or COMPAT_DIR, name)
def unrunnable(name, runnable):
low = name.lower()
return low not in runnable and (low.startswith("proton") or low == LEGACY_TOOL or low.startswith("droiddeck-proton"))
def app_order(app):
return (len(app), app)
def plan_mapping(current, installed, runnable, default, auto, native=None):
runnable = {n.lower() for n in runnable}
installed = set(installed)
native = native or {}
changes, kept = {}, {}
if current.get("0", "") != default:
changes["0"] = default
for app in sorted((set(current) | installed | set(auto)) - {"0"}, key=app_order):
name = current.get(app, "")
mine = auto.get(app)
target = native.get(app, default)
stale = unrunnable(name, runnable) or (app in native and linux_runtime_tool(name))
if name and not stale and (mine is None or name != mine):
continue
if not name and app not in installed:
continue
if name != target:
changes[app] = target
kept[app] = target
return changes, kept
def followers(state, current):
if "auto" in state:
return state["auto"]
return {app: name for app, name in current.items() if app != "0" and name == TOOL}
def tool_entry(name, display, path, valve):
return {"name": name, "display": display, "dir": os.path.basename(path) if path else "", "valve": valve}
def tool_catalog(steam_root, extras=None):
tools = os.path.join(steam_root, "compatibilitytools.d")
catalog = []
for name, sources in OWN_TOOLS:
depot = find_source(steam_root, sources)
if depot is None and name != TOOL:
continue
catalog.append(tool_entry(name, display_name(
os.path.basename(depot).replace(" (ARM64)", " ARM64") if depot else "Proton ARM64"), depot, True))
if extras is None:
extras = {}
try:
entries = sorted(os.listdir(tools))
except OSError:
entries = []
for entry in entries:
tool_dir = os.path.join(tools, entry)
if entry in dict(OWN_TOOLS) or not any(os.path.isfile(os.path.join(tool_dir, w)) for w in (EXTRA_WRAPPER, LEGACY_EXTRA_WRAPPER)):
continue
extras[first_key(os.path.join(tool_dir, "compatibilitytool.vdf"), ["compatibilitytools", "compat_tools"]) or entry] = entry
for name, entry in sorted(extras.items()):
catalog.append(tool_entry(name, display_name(entry), os.path.join(tools, entry), False))
return catalog
def requested_tool(request, catalog):
name = request.get("name")
if isinstance(name, str) and any(t["name"] == name for t in catalog):
return name
for tool in catalog:
if tool["dir"] and tool["dir"] == request.get("dir") and tool["valve"] == bool(request.get("valve")):
return tool["name"]
return None
def now_ms():
import time
return int(time.time() * 1000)
def choose_default(steam_default, state, request, catalog, adopt_steam=True, apply_request=True, now=None):
state = dict(state)
names = [t["name"] for t in catalog]
seq = request.get("seq")
seq = seq if isinstance(seq, int) else 0
if apply_request and seq > int(state.get("applied", 0) or 0):
wanted = requested_tool(request, catalog)
state["applied"] = seq
if wanted:
if wanted != state.get("default"):
state.update(default=wanted, seq=now or now_ms(), source="app")
else:
print("steam-compatibility: the app asked for %s, which is not installed; kept %s" % (
request.get("name") or request.get("dir"), state.get("default") or TOOL))
elif adopt_steam and steam_default in names and steam_default != state.get("default"):
state.update(default=steam_default, seq=now or now_ms(), source="steam")
if state.get("default") not in names:
state["default"] = TOOL
return state["default"], state
def mapping_block(tokens):
steam = find_block(tokens, ["InstallConfigStore", "Software", "Valve", "Steam"])
if steam is None:
return None
mapping = find_block(tokens[steam + 1:], ["CompatToolMapping"])
if mapping is None:
end = block_end(tokens, steam)
tokens[end:end] = ['"CompatToolMapping"', "{", "}"]
return end + 1
return mapping + steam + 1
def read_mapping(tokens, mapping):
current = {}
end = block_end(tokens, mapping)
i = mapping + 1
while i < end:
if i + 1 < end and tokens[i + 1] == "{":
close = block_end(tokens, i + 1)
name = ""
for j in range(i + 2, close - 1):
if tokens[j] == '"name"':
name = tokens[j + 1].strip('"')
break
current[tokens[i].strip('"')] = name
i = close + 1
else:
i += 2
return current
def set_mapping(tokens, mapping, app, tool):
block = find_block(tokens[mapping + 1:], [app])
if block is None:
tokens[mapping + 1:mapping + 1] = [
'"%s"' % app, "{", '"name"', '"%s"' % tool, '"config"', '""', '"priority"', '"75"' if app == "0" else '"250"', "}"]
return
block += mapping + 1
end = block_end(tokens, block)
for i in range(block + 1, end):
if tokens[i] == '"name"':
tokens[i + 1] = '"%s"' % tool
return
tokens[block + 1:block + 1] = ['"name"', '"%s"' % tool]
def steam_running():
try:
pids = [p for p in os.listdir("/proc") if p.isdigit()]
except OSError:
return False
for pid in pids:
try:
with open("/proc/%s/cmdline" % pid, "rb") as f:
argv0 = f.read().split(b"\0", 1)[0]
except OSError:
continue
if argv0.endswith(b"steamrtarm64/steam"):
return True
return False
def live_helper_running(directory=None):
try:
with open(state_path("live.pid", directory)) as f:
pid = int(f.read().split()[0])
os.kill(pid, 0)
return True
except (OSError, ValueError, IndexError):
return False
def register_default(config_path, apps=(), protect=(), default=None, catalog=None, directory=None, settle=False):
"""Writes the mapping: the "0" default and every title that follows it (see plan_mapping).
With settle, the default itself is decided first (see choose_default): before the client
starts, config.vdf holds what the client last kept, menu changes included."""
with locked(directory):
state = read_json(state_path("state.json", directory))
with open(config_path, encoding="utf-8", errors="replace") as f:
original = f.read()
tokens = tokenize(original)
mapping = mapping_block(tokens)
if mapping is None:
return state.get("default") or TOOL
current = read_mapping(tokens, mapping)
if catalog is None:
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]
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
steam_root = os.path.dirname(os.path.dirname(os.path.abspath(config_path)))
changes, auto = plan_mapping(current, apps, names, default, followers(state, current), native_targets(steam_root, apps))
for app, tool in changes.items():
set_mapping(tokens, mapping, app, tool)
text = serialize(tokens)
if text != serialize(tokenize(original)):
write_changed(config_path, text)
state["auto"] = auto
state["tools"] = catalog
state.setdefault("default", default)
write_json(state_path("state.json", directory), state)
return default
def depot_build(depot):
"""The build number a depot's version file starts with; 0 when there is none."""
try:
with open(os.path.join(depot, "version"), encoding="utf-8", errors="replace") as f:
first = f.read().split(None, 1)
except OSError:
return 0
return int(first[0]) if first and first[0].isdigit() else 0
def find_source(steam_root, sources=SOURCES):
"""The first of `sources` the client has downloaded, newest across the libraries holding it."""
for name in sources:
found = [os.path.join(root, "steamapps", "common", name) for root in (steam_root, "/mnt/droiddeck-sd")
if os.path.isdir(os.path.join(root, "steamapps", "common", name, "files", "bin-arm64"))]
if found:
return max(found, key=depot_build)
return None
def build_tool(tool_dir, depot, sources=SOURCES, name=TOOL):
"""Our own manifests and the launcher, named after `depot`. The depot is run at launch time."""
if os.path.islink(tool_dir):
os.unlink(tool_dir)
elif os.path.isdir(tool_dir) and set(os.listdir(tool_dir)) - set(OWN_FILES):
# Earlier versions mirrored the depot in here as symlinks; start clean. A directory holding
# only our own files is kept, and each file rewritten only when it changed.
shutil.rmtree(tool_dir)
os.makedirs(tool_dir, exist_ok=True)
write_changed(os.path.join(tool_dir, "toolmanifest.vdf"), TOOL_MANIFEST_VDF)
write_changed(os.path.join(tool_dir, "compatibilitytool.vdf"), COMPAT_TOOL_VDF % (name, display_name(
os.path.basename(depot).replace(" (ARM64)", " ARM64") if depot else "Proton ARM64")))
write_changed(os.path.join(tool_dir, LAUNCHER), launcher_sh(sources), 0o755)
def build_fex_tool(tool_dir):
if os.path.islink(tool_dir):
os.unlink(tool_dir)
elif os.path.isdir(tool_dir) and set(os.listdir(tool_dir)) - set(FEX_FILES):
shutil.rmtree(tool_dir)
os.makedirs(tool_dir, exist_ok=True)
write_changed(os.path.join(tool_dir, "toolmanifest.vdf"), FEX_MANIFEST_VDF)
write_changed(os.path.join(tool_dir, "compatibilitytool.vdf"), FEX_COMPAT_TOOL_VDF % (FEX_TOOL, display_name("Linux x86 (FEX)")))
write_changed(os.path.join(tool_dir, FEX_LAUNCHER), FEX_LAUNCHER_SH, 0o755)
def remove_tool(tool_dir):
if os.path.isdir(tool_dir) and not os.path.islink(tool_dir) and not set(os.listdir(tool_dir)) - set(OWN_FILES) - {LEGACY_LAUNCHER}:
shutil.rmtree(tool_dir)
def main():
steam_root = sys.argv[1]
tools = os.path.join(steam_root, "compatibilitytools.d")
os.makedirs(tools, exist_ok=True)
legacy = os.path.join(tools, LEGACY_TOOL)
if os.path.islink(legacy):
os.unlink(legacy)
elif os.path.isdir(legacy):
shutil.rmtree(legacy)
for name, sources in OWN_TOOLS:
depot = find_source(steam_root, sources)
if depot is None and name != TOOL:
remove_tool(os.path.join(tools, name))
continue
build_tool(os.path.join(tools, name), depot, sources, name)
print("steam-compatibility: registered %s (%s)" % (
name, "depot %s present" % os.path.basename(depot) if depot else
"no ARM64 Proton depot yet; the session asks the client to fetch it"))
build_fex_tool(os.path.join(tools, FEX_TOOL))
print("steam-compatibility: registered %s" % FEX_TOOL)
extras = adopt_extras(tools)
for name, directory in sorted(extras.items()):
print("steam-compatibility: adopted %s as %s" % (directory, name))
catalog = tool_catalog(steam_root, extras)
config = os.path.join(steam_root, "config", "config.vdf")
if live_helper_running():
with locked():
state = read_json(state_path("state.json"))
state["tools"] = catalog
write_json(state_path("state.json"), state)
print("steam-compatibility: the live helper keeps the client's mapping; tools listed")
elif os.path.isfile(config):
apps = installed_apps(steam_root)
default = register_default(config, apps, protect=extras, catalog=catalog, settle=not steam_running())
print("steam-compatibility: default and %d installed title(s) set to %s" % (len(apps), default))
sync = os.path.join(os.path.dirname(os.path.realpath(__file__)), "droiddeck-esync")
if os.path.isfile(sync):
try:
summary = runpy.run_path(sync)["reconcile"](steam_root, os.environ)
except Exception as e:
summary = "droiddeck-esync: reconcile failed (%s)" % e
if summary:
print("steam-compatibility: %s" % summary)
if __name__ == "__main__":
main()