merge: with main branch

This commit is contained in:
0x1Nawaf committed 2026-10-25 10:38:42 +00:00
commit aa1a8da39a
285 files changed
+18085 -1245

No files matched your search

+40
View File
@@ -0,0 +1,40 @@
#!/usr/bin/env bash
set -euo pipefail
missing=()
for package in "$@"; do
if [ "$(dpkg-query -W -f='${Status}' "$package" 2>/dev/null || true)" != 'install ok installed' ]; then
missing+=("$package")
fi
done
if [ "${#missing[@]}" = 0 ]; then
echo "Ubuntu build dependencies are already installed."
exit 0
fi
options=(
-o Dir::Etc::sourcelist=sources.list.d/ubuntu.sources
-o Dir::Etc::sourceparts=-
-o Acquire::Retries=1
-o Acquire::http::Timeout=15
-o Acquire::https::Timeout=15
-o DPkg::Lock::Timeout=30
)
install_packages() {
echo "Updating Ubuntu package indexes."
sudo timeout --kill-after=10s 60s apt-get "${options[@]}" -o APT::Update::Error-Mode=any update -q || return $?
echo "Installing: ${missing[*]}"
sudo timeout --kill-after=10s 120s env DEBIAN_FRONTEND=noninteractive NEEDRESTART_MODE=a apt-get "${options[@]}" install -y -q --no-install-recommends "${missing[@]}"
}
if install_packages; then
exit 0
fi
echo "::warning::Runner mirror failed or timed out; retrying with the official Ubuntu mirror."
sources=$(mktemp --suffix=.sources)
trap 'rm -f "$sources"' EXIT
mirror=https://archive.ubuntu.com/ubuntu
case "$(dpkg --print-architecture)" in
arm64) mirror=https://ports.ubuntu.com/ubuntu-ports ;;
esac
sed -E "s@^URIs:.*@URIs: $mirror@" /etc/apt/sources.list.d/ubuntu.sources > "$sources"
chmod 644 "$sources"
options[1]="Dir::Etc::sourcelist=$sources"
install_packages
+16
View File
@@ -0,0 +1,16 @@
#!/usr/bin/env bash
set -euo pipefail
repo=$(cd "$(dirname "$0")/../.." && pwd)
source "$repo/tools/ci/toolchains.env"
ndk="$ANDROID_HOME/ndk/$CI_ANDROID_NDK_VERSION"
if [ ! -f "$ndk/source.properties" ]; then
sdkmanager --sdk_root="$ANDROID_HOME" --install "ndk;$CI_ANDROID_NDK_VERSION"
fi
actual=$(sed -n 's/^Pkg.Revision *= *//p' "$ndk/source.properties" | tr -d '\r')
test "$actual" = "$CI_ANDROID_NDK_VERSION"
{
echo "NDK_VERSION=$actual"
echo "ANDROID_NDK_HOME=$ndk"
echo "ANDROID_NDK_ROOT=$ndk"
echo "ANDROID_NDK_LATEST_HOME=$ndk"
} >> "$GITHUB_ENV"
+1
View File
@@ -0,0 +1 @@
CI_ANDROID_NDK_VERSION=30.0.16248370
+65
View File
@@ -0,0 +1,65 @@
# Runtime component preparation
`recipes.json` owns component source inputs, builders, outputs, package requirements
and pinned downloads. `prepare.py` runs those recipes, calculates cache identities,
installs the union of missing tools and verifies the prepared artifact handoff.
Builders live in `recipes/`; the workflow describes scheduling and gates.
```mermaid
flowchart LR
Source[Pin source tree] --> Components[Prepare verified components]
Source --> Xbox[Xbox ARM64 backend]
Source --> Tests[Python and native tests]
Components --> APK[Compile APK and Android tests]
Xbox --> APK
APK --> Gate[Compile and test APK gate]
Tests --> Gate
Gate --> Publish[Main only: signing and publishing]
```
All workers verify the same source tree. The component artifact records that tree,
recipe identities and every output hash; the APK worker verifies the inventory
before building. This handoff verifies consistency within the same CI run. It is
not independent provenance for arbitrary artifacts. Source policy checks, tests,
APK content/ABI checks and signing/publication gates remain in place.
`tools/ci/toolchains.env` pins the Android NDK shared by component, APK and native
test workers. Linux builds use Ubuntu 24.04. Recipe and input changes invalidate
the relevant component cache; ordinary caches remain performance aids.
## Verified preload snapshots
`preloads.lock.json` pins a small immutable preload archive. It records each
binary's hash, its producing main CI run, the original APK artifact identity and
digest, source/header hashes, the ARM64 build recipe and x86 compiler-image inputs.
It covers core ARM64 preloads and the x86 preload libraries. VR, Android native
libraries, Comet and Xbox use their registered recipes and existing release pins
or component caches.
Every build compares current source inputs, including transitive quoted headers,
and build recipes with this manifest. Changed groups rebuild from source. A
missing release also selects a rebuild. Corrupt archives, unexpected files and
binary hash mismatches fail the build. Existing preload checks, runtime packaging
checks and all test jobs still run.
The trust anchor is the manifest in the pinned main base. A new or changed PR
manifest is independently checked against a successful `push` build of main:
GitHub must confirm the run/source/workflow and original APK artifact digest, and
every claimed binary must match that APK. The claimed source inputs and recipe
must match that run's Git source. A PR cannot establish trust by changing both a
binary and its claimed checksum. This is approved-build reuse, not a claim of
bit-for-bit reproducible compilation or a signed SLSA attestation.
To refresh after source or compiler changes:
1. Let a main build compile and test the new inputs.
2. Run `python tools/components/preloads.py snapshot --run RUN_ID`.
3. Publish `app/build/component-snapshot/preloads.tar.gz` to
DroidDeck-Components as `preloads-<full archive SHA-256>`, without replacing an
existing release. Include the manifest and producing run in the release notes.
4. Review and commit the updated manifest. CI independently checks its origin.
The workflow dispatch `rebuild_preloads` option forces source compilation even
when an approved snapshot matches. Use it when refreshing a compiler or system
header revision. Runner image updates do not silently replace an approved binary;
the producing run remains the record of its compiler environment.
+23
View File
@@ -0,0 +1,23 @@
#!/usr/bin/env bash
set -euo pipefail
D=${1:?output directory}
mkdir -p "$D"
aarch64-linux-gnu-g++ -shared -fPIC -O2 -Wall -Wno-attributes -Wno-nonnull-compare \
-pthread -std=c++17 -static-libstdc++ -static-libgcc -Wl,--exclude-libs,ALL \
-o "$D/libfakeinput.so" app/src/main/cpp/fakeinput_steam.cpp -ldl
aarch64-linux-gnu-strip --strip-unneeded "$D/libfakeinput.so"
aarch64-linux-gnu-gcc -shared -fPIC -O2 -Wall -pthread \
-o "$D/libblsession.so" tools/linuxfs/preload/*.c -ldl
aarch64-linux-gnu-strip --strip-unneeded "$D/libblsession.so"
aarch64-linux-gnu-gcc -shared -fPIC -O2 -Wall -pthread \
-o "$D/libblfastpath.so" tools/proot/fastpath/fastpath.c -ldl
aarch64-linux-gnu-strip --strip-unneeded "$D/libblfastpath.so"
aarch64-linux-gnu-gcc -shared -fPIC -O2 -Wall -nostdlib -ffreestanding -fno-stack-protector \
-fno-tree-loop-distribute-patterns -Wl,-z,defs -o "$D/libblaudit.so" tools/proot/fastpath/audit.c
aarch64-linux-gnu-strip --strip-unneeded "$D/libblaudit.so"
aarch64-linux-gnu-gcc -shared -fPIC -O2 -Wall -pthread \
-o "$D/libssbs.so" tools/linuxfs/ssbs/ssbs_adapter.c -ldl
aarch64-linux-gnu-strip --strip-unneeded "$D/libssbs.so"
mkdir -p "$D/usr/local/bin"
aarch64-linux-gnu-gcc -O2 -Wall -Wextra -o "$D/usr/local/bin/droiddeck-clipboard" tools/linuxfs/clipboard/clipboard.c -ldl
aarch64-linux-gnu-strip --strip-unneeded "$D/usr/local/bin/droiddeck-clipboard"
+126
View File
@@ -0,0 +1,126 @@
{
"archive_sha256": "cb6487a94f25a3068fd07958174eb5b5e14f709655ff4ef0a8cdbd99138513b2",
"arm64_recipe": "1c92a1b0669277de3e78e9f292634affb5b5a19d5ea49d3ad8610c1dc638c2fb",
"groups": {
"arm64": {
"@runner": "84f825635599f81ed33f62c5fc4c73f66044d1468b060d65f83855eb9c8bfe77",
"app/src/main/cpp/adrenotools/src/hook/kgsl.h": "396c56761034a4ed783543ee3312c6ec64d63b2a1cf7eecf1939c23251f25adf",
"app/src/main/cpp/fakeinput_steam.cpp": "3d11e859ca58a42a9da92a3fc589d404df790e151f7e18ea6114645750e70ef5",
"app/src/main/cpp/venc/venc_proto.h": "2242ddbc8981d14a33bac70264bb3e7f701fd1c3ebb988e17bbb2b19b6b808f7",
"tools/linuxfs/clipboard/clipboard.c": "96291c2bbc040c83caabf059ab3e95a9ed90d6a768c759d7cb4c0b64706bddfa",
"tools/linuxfs/preload/binfmt.c": "0933cc49cb53f0aa1206e2eae516d38dc3337e74174be07e10619c42aa56792b",
"tools/linuxfs/preload/dircache.c": "8e7f19ea4a1198783d53a3f357326c6177da157b0cc7e0d22e1670963e1c26e7",
"tools/linuxfs/preload/drm.c": "e0eabab0d8ed9e841918e827bf87168efa7f2dfb8404169e9bed3ab5eda2836c",
"tools/linuxfs/preload/fsync.c": "795c114c2a05d3f250d07145d04c5658091699da3b96290da36348ccadfb0c09",
"tools/linuxfs/preload/inputudev.c": "2648248d3d235a582eb8459709e0a85de537c03ac10c5040fbfb50d9846c68eb",
"tools/linuxfs/preload/kgslid.c": "88a7875fd5289930204d155d92ac9d15492cd06db16ed87aa9c95af37f3e7089",
"tools/linuxfs/preload/kgslpoll.c": "f0d853d919a52487c04cbda5fb3fa22269da54b7f4ee2f7dd3b5bd9909418670",
"tools/linuxfs/preload/links.c": "b7bbcf9112393b152f358fe6f8dc9a1955f0c8532e1eddf016a971415cd13c64",
"tools/linuxfs/preload/lock.c": "f71b3fd40e6a2b74908d3cdc601e8748699ae9c7a89b542d2445b999015ffd66",
"tools/linuxfs/preload/net.c": "335853c8ea0e9a8ebbc5a5500dc4bded9c34c4310a6ab8aa46b07d5ce1c2b55e",
"tools/linuxfs/preload/netif.c": "cc26d50b8d4639d8dd4697811bbf85c9d9f31761f9504a4af42473de3cb22b3c",
"tools/linuxfs/preload/noexec.c": "1855aeaeda3d506329bd9af1107580c74d1f56787c411b2e01c0a8a9d4059c7b",
"tools/linuxfs/preload/ntsync.c": "0614c48af0c062bfcb4c30141efe1b792b340767a67d2fe92ee9877d2ba16e0d",
"tools/linuxfs/preload/opens.c": "92ae4aea16aa457a898dedf4a70eb332d80d3b1e77f3a60bace7715a4317399e",
"tools/linuxfs/preload/pathcache.c": "eb0d4b5238fe6c8891e805d90bd837e466754100c978fad2d0ff717116b9b8a3",
"tools/linuxfs/preload/perms.c": "f600619c4ca130f69001ed7ad7bd74a3dc215b14c61479c45bb770f916d244b9",
"tools/linuxfs/preload/pulseguard.c": "57674ea40283e53a27bc78362de1ba41ee972d172ea38b1f818b1d6eed0abe65",
"tools/linuxfs/preload/quest_kgsl.c": "59d9b8416be6a1c680f1d1e4c2427d2cbb7b415344261adb357d9d211ec95cfe",
"tools/linuxfs/preload/robust.c": "9eb6c0ed50e0f48a8cfb86bd92587f471354bdf2531b9177f4b3ab83bdfed39a",
"tools/linuxfs/preload/rptouch.c": "0e3f8bf2f9a41324361f38fa4da8d2d47593c9555f0cabf8228767397846c7c3",
"tools/linuxfs/preload/space.c": "118389bcc89dfa2b4230b80915e21d2d4331c4cff1f5a58265f85e6ecb5c29d2",
"tools/linuxfs/preload/storage.c": "08a8b46c3588f3160bb8608489768010ec36d5ccdb2b25a431ec9d6fcd413a6e",
"tools/linuxfs/preload/sysv.c": "49774ee5e2e39b84dcfadf63abdc741b3df236cb11783744a1280adee36965e8",
"tools/linuxfs/preload/tracefs.c": "be3f8a2f032b56e6e08a931e77a9386aea4bb93421a482271e06386ec0c469b6",
"tools/linuxfs/preload/tracer.c": "f34e5377c891b0a8d00489245d66f96bc352df4cd90f47a951dcadae980af4c1",
"tools/linuxfs/preload/udevmon.c": "2045dfb883cfb6e2016c359a3dc3af17f3b8f383f6dd0d5dfee407ef0d344852",
"tools/linuxfs/preload/usr2.c": "6ae704380b45a9b3a74c4fe2b3ce4482470dae59ced26cf0aee64f4b4dcc11f9",
"tools/linuxfs/preload/venc.c": "24c3c46fe49d28adfc8c977e52c9cc407f534b26f56d94b3425b8bb01803dba8",
"tools/linuxfs/preload/vmwrite.c": "1a3ea2ec4354e0716c17f7db537b19ab82bcd524f2aece2fa5dec89b076a2188",
"tools/linuxfs/preload/webhelper.c": "a8213a7a96b407459c83fb93365cfe58fd05964e7bb262b8061c1c0792a6aac8",
"tools/linuxfs/preload/writewatch.c": "61e395d1caa184d329dead0e36c4e92cb1069b1154ac36581f37406c1c242240",
"tools/linuxfs/preload/xmode.c": "9a09995bf52d40b07f16828bcc545b59b8fb1ba60d9cb09f14f7f31dba1e65a6",
"tools/linuxfs/preload/zram.c": "12bcf07459728d4a7ede6fa0fea8deb330f7c5d0f0dd61e610e81b9efecd4262",
"tools/linuxfs/ssbs/ssbs_adapter.c": "9e4e0fe637f7b8f91f12d8151f02e8d31bcb4294be2b2c62d3cd3ce76f200306",
"tools/proot/fastpath/audit.c": "bfe347a2ddaceef40a78d7d590e4e679724ee4c82f3f1f1e3c7017a18ad536d4",
"tools/proot/fastpath/fastpath.c": "4cd07b25ba14f6faa6bac5091ae323ded6b646dbb34b9e5a92ea6385e4d37cb5",
"tools/quest/kgsl_compat.c": "111afffda7a6df2469610f6c342ae086b1a97a559c586efc4a28015b86efd6bd",
"tools/quest/kgsl_compat.h": "2e9e0eb647530a35277954b7e61a22ee958067fe46056a608fb32f40f273f1bf"
},
"x86": {
"@runner": "84f825635599f81ed33f62c5fc4c73f66044d1468b060d65f83855eb9c8bfe77",
"app/src/main/cpp/adrenotools/src/hook/kgsl.h": "396c56761034a4ed783543ee3312c6ec64d63b2a1cf7eecf1939c23251f25adf",
"app/src/main/cpp/fakeinput_steam.cpp": "3d11e859ca58a42a9da92a3fc589d404df790e151f7e18ea6114645750e70ef5",
"app/src/main/cpp/venc/venc_proto.h": "2242ddbc8981d14a33bac70264bb3e7f701fd1c3ebb988e17bbb2b19b6b808f7",
"tools/linuxfs/build-x86-preloads.sh": "d0820252809881db89db5a17427e74b9aa4964aae70623cb7f4b474e90c7a23b",
"tools/linuxfs/debian-image.env": "1c79dea4314c784042a5c59208765c8401771098f293185d61a157d89edf6e52",
"tools/linuxfs/fex/faultreport.c": "573bf780f32f7256dc2fbf40fe850d67e67c354509fdedcac3ff7f1a261daac3",
"tools/linuxfs/fex/vulkanpin.c": "f2693ac2be8d15f7881115e61b1c6b3fe4aeec8f603584c6fce5b0f046563d51",
"tools/linuxfs/fex/webkitinput.c": "693ffd6c8c8c97e87156444ad2255c84dbf22cd0053d135fb8d9be094b5534d1",
"tools/linuxfs/preload/binfmt.c": "0933cc49cb53f0aa1206e2eae516d38dc3337e74174be07e10619c42aa56792b",
"tools/linuxfs/preload/dircache.c": "8e7f19ea4a1198783d53a3f357326c6177da157b0cc7e0d22e1670963e1c26e7",
"tools/linuxfs/preload/drm.c": "e0eabab0d8ed9e841918e827bf87168efa7f2dfb8404169e9bed3ab5eda2836c",
"tools/linuxfs/preload/fsync.c": "795c114c2a05d3f250d07145d04c5658091699da3b96290da36348ccadfb0c09",
"tools/linuxfs/preload/inputudev.c": "2648248d3d235a582eb8459709e0a85de537c03ac10c5040fbfb50d9846c68eb",
"tools/linuxfs/preload/kgslid.c": "88a7875fd5289930204d155d92ac9d15492cd06db16ed87aa9c95af37f3e7089",
"tools/linuxfs/preload/kgslpoll.c": "f0d853d919a52487c04cbda5fb3fa22269da54b7f4ee2f7dd3b5bd9909418670",
"tools/linuxfs/preload/links.c": "b7bbcf9112393b152f358fe6f8dc9a1955f0c8532e1eddf016a971415cd13c64",
"tools/linuxfs/preload/lock.c": "f71b3fd40e6a2b74908d3cdc601e8748699ae9c7a89b542d2445b999015ffd66",
"tools/linuxfs/preload/net.c": "335853c8ea0e9a8ebbc5a5500dc4bded9c34c4310a6ab8aa46b07d5ce1c2b55e",
"tools/linuxfs/preload/netif.c": "cc26d50b8d4639d8dd4697811bbf85c9d9f31761f9504a4af42473de3cb22b3c",
"tools/linuxfs/preload/noexec.c": "1855aeaeda3d506329bd9af1107580c74d1f56787c411b2e01c0a8a9d4059c7b",
"tools/linuxfs/preload/ntsync.c": "0614c48af0c062bfcb4c30141efe1b792b340767a67d2fe92ee9877d2ba16e0d",
"tools/linuxfs/preload/opens.c": "92ae4aea16aa457a898dedf4a70eb332d80d3b1e77f3a60bace7715a4317399e",
"tools/linuxfs/preload/pathcache.c": "eb0d4b5238fe6c8891e805d90bd837e466754100c978fad2d0ff717116b9b8a3",
"tools/linuxfs/preload/perms.c": "f600619c4ca130f69001ed7ad7bd74a3dc215b14c61479c45bb770f916d244b9",
"tools/linuxfs/preload/pulseguard.c": "57674ea40283e53a27bc78362de1ba41ee972d172ea38b1f818b1d6eed0abe65",
"tools/linuxfs/preload/quest_kgsl.c": "59d9b8416be6a1c680f1d1e4c2427d2cbb7b415344261adb357d9d211ec95cfe",
"tools/linuxfs/preload/robust.c": "9eb6c0ed50e0f48a8cfb86bd92587f471354bdf2531b9177f4b3ab83bdfed39a",
"tools/linuxfs/preload/rptouch.c": "0e3f8bf2f9a41324361f38fa4da8d2d47593c9555f0cabf8228767397846c7c3",
"tools/linuxfs/preload/space.c": "118389bcc89dfa2b4230b80915e21d2d4331c4cff1f5a58265f85e6ecb5c29d2",
"tools/linuxfs/preload/storage.c": "08a8b46c3588f3160bb8608489768010ec36d5ccdb2b25a431ec9d6fcd413a6e",
"tools/linuxfs/preload/sysv.c": "49774ee5e2e39b84dcfadf63abdc741b3df236cb11783744a1280adee36965e8",
"tools/linuxfs/preload/tracefs.c": "be3f8a2f032b56e6e08a931e77a9386aea4bb93421a482271e06386ec0c469b6",
"tools/linuxfs/preload/tracer.c": "f34e5377c891b0a8d00489245d66f96bc352df4cd90f47a951dcadae980af4c1",
"tools/linuxfs/preload/udevmon.c": "2045dfb883cfb6e2016c359a3dc3af17f3b8f383f6dd0d5dfee407ef0d344852",
"tools/linuxfs/preload/usr2.c": "6ae704380b45a9b3a74c4fe2b3ce4482470dae59ced26cf0aee64f4b4dcc11f9",
"tools/linuxfs/preload/venc.c": "24c3c46fe49d28adfc8c977e52c9cc407f534b26f56d94b3425b8bb01803dba8",
"tools/linuxfs/preload/vmwrite.c": "1a3ea2ec4354e0716c17f7db537b19ab82bcd524f2aece2fa5dec89b076a2188",
"tools/linuxfs/preload/webhelper.c": "a8213a7a96b407459c83fb93365cfe58fd05964e7bb262b8061c1c0792a6aac8",
"tools/linuxfs/preload/writewatch.c": "61e395d1caa184d329dead0e36c4e92cb1069b1154ac36581f37406c1c242240",
"tools/linuxfs/preload/xmode.c": "9a09995bf52d40b07f16828bcc545b59b8fb1ba60d9cb09f14f7f31dba1e65a6",
"tools/linuxfs/preload/zram.c": "12bcf07459728d4a7ede6fa0fea8deb330f7c5d0f0dd61e610e81b9efecd4262",
"tools/quest/kgsl_compat.c": "111afffda7a6df2469610f6c342ae086b1a97a559c586efc4a28015b86efd6bd",
"tools/quest/kgsl_compat.h": "2e9e0eb647530a35277954b7e61a22ee958067fe46056a608fb32f40f273f1bf"
}
},
"origin": {
"artifact": 12520502054,
"artifact_sha256": "b7e800a1884a516ec52b050b7f904f9881427bca4579f660b909fd18f15e49ff",
"commit": "b76f13f7acc0af0cc00369dde61fb88e40529e6d",
"run": 39992155294,
"url": "https://github.com/Droid-Deck/DroidDeck/actions/runs/39992155294"
},
"outputs": {
"i386/libblsession.so": "5343cdb704217ddc81ee593e67d5987363dc7b5ac78bd4bd36000219e5063268",
"i386/libfakeinput.so": "188d88dfb13b6e2f2df948e7b8b4d9ec604770f55d1ea5b6b9e21ad56a74ce5e",
"i386/libfaultreport.so": "95f9ef60f04a13ee60cfbd4d2d2efb60a7063b722e646c78c9dc27162292877b",
"libblaudit.so": "6c6abe89baf5d7309babee0c3372a4b00c2b689e2a63de38c5e9a660626509b5",
"libblfastpath.so": "0e1f151b5c1c5ea9dd380ad58d09f8fe7ba3ae037d0991cb3eef6899e9d5ec83",
"libblsession.so": "29d4b5ea46dba941107e02c0ee544244b137a339cada43bff65c350f2ef2755a",
"libfakeinput.so": "5f6dc3aea05de2dc1fbe0bbfac2ce4d11e3d47f761c6b06c8badb15fe1904392",
"libssbs.so": "c288ed0da26c2481aff270d1cdafe1205061c4a300c2c46ae1b53c6e0c94f44b",
"usr/local/bin/droiddeck-clipboard": "ec50e8f67fdf76372927189b0fdb670f9b70fd3484f2fe3e833bc8f9fdc44205",
"x86_64/libblsession.so": "61d19f06aebab6a789f8c99f687af9903376bfc853e46b70c48a2ce5e497accc",
"x86_64/libfakeinput.so": "8663dd2086bf8ae53069a480dd9ae4619109d32f0947a29e2ce38f07a21ae2e6",
"x86_64/libfaultreport.so": "bb0dc9165d0d764473e552508650edd586df16d620fc5b16b8b1c5740a28ce22",
"x86_64/libvulkan-thunk.so": "74f01b652d7e8179561a096a552328b68da640cc4bad3981edd38e6afbf90243",
"x86_64/libwebkitinput.so": "9b07380a7b017397b9c56bb7bb1161e046b0c6a647fc413888904606b0def6d0"
},
"toolchain": {
"arm64": "Ubuntu 24.04 GCC cross compiler; exact build commands pinned by arm64_recipe",
"x86": "Debian bullseye compiler image and recipe pinned by source inputs"
},
"version": 1,
"x86_recipe": "36d06d49f7a4bff5ef675c1e03ce5a5b61498df4c36b9d15a938b41564f755cc"
}
+290
View File
@@ -0,0 +1,290 @@
"""Reuse reviewed preload snapshots; changed snapshots must prove their main-build origin."""
import argparse
import gzip
import http.client
import hashlib
import io
import json
import os
import posixpath
import re
from pathlib import Path
import subprocess
import tarfile
import textwrap
import urllib.error
import urllib.request
from zipfile import ZipFile
ROOT = Path(__file__).resolve().parents[2]
REPO = 'Droid-Deck/DroidDeck'
LOCK = 'tools/components/preloads.lock.json'
DEFINITIONS = json.loads((ROOT / 'tools/components/recipes.json').read_text())
INPUTS = {group: DEFINITIONS[name]['source_inputs'] for group, name in
[('arm64', 'arm64-preloads'), ('x86', 'x86-preloads')]}
OUTPUTS = {group: DEFINITIONS[name]['snapshot_outputs'] for group, name in
[('arm64', 'arm64-preloads'), ('x86', 'x86-preloads')]}
COMMON = [name for name in INPUTS['arm64'] if name in INPUTS['x86']]
FILES = set(sum(OUTPUTS.values(), []))
def sha(data):
return hashlib.sha256(data).hexdigest()
def run(*args, root=ROOT):
return subprocess.check_output(args, cwd=root)
def api(path):
return json.loads(run('gh', 'api', 'repos/' + REPO + '/' + path))
def source_files(group, root=ROOT, commit=None, patterns=None):
patterns = patterns if patterns is not None else INPUTS[group]
if commit:
paths = run('git', 'ls-tree', '-r', '--name-only', commit, '--', *patterns, root=root).decode().splitlines()
read = lambda path: run('git', 'show', f'{commit}:{path}', root=root)
else:
paths = run('git', 'ls-files', '--cached', '--others', '--exclude-standard', '--', *patterns, root=root).decode().splitlines()
read = lambda path: (root / path).read_bytes()
paths = [p for p in paths if not p.endswith('.md') and '__pycache__' not in p and not p.endswith('.pyc')]
if not paths:
raise ValueError('No preload inputs found')
result = {}
pending = list(paths)
while pending:
path = pending.pop()
if path in result:
continue
if path.startswith('../') or path.startswith('/'):
raise ValueError('Preload include escapes repository')
content = read(path).replace(b'\r\n', b'\n')
result[path] = sha(content)
for include in re.findall(rb'^\s*#\s*include\s*"([^"\n]+)"', content, re.M):
pending.append(posixpath.normpath(posixpath.join(posixpath.dirname(path), include.decode())))
workflow = (run('git', 'show', f'{commit}:.github/workflows/build.yml', root=root).decode() if commit
else (root / '.github/workflows/build.yml').read_text())
job = 'components' if re.search(r'(?m)^ components:', workflow) else 'build'
runner = re.search(r'(?ms)^ ' + job + r':\n.*?^ runs-on: ([^\n]+)', workflow)
if runner is None:
raise ValueError('APK build runner policy is missing')
result['@runner'] = sha(runner[1].strip().encode())
return dict(sorted(result.items()))
def recipe(root=ROOT, commit=None):
def read(path):
return (run('git', 'show', f'{commit}:{path}', root=root).decode() if commit
else (root / path).read_text())
script = 'tools/components/build-arm64-preloads.sh'
if commit and run('git', 'ls-tree', commit, script, root=root) == b'':
workflow = read('.github/workflows/build.yml')
text = workflow[workflow.index(' aarch64-linux-gnu-g++ -shared'):]
text = textwrap.dedent(text[:text.index(' VK_INCLUDE=')])
else:
text = read(script)
preamble = '#!/usr/bin/env bash\nset -euo pipefail\nD=${1:?output directory}\nmkdir -p "$D"\n'
if not text.startswith(preamble):
return sha(text.encode())
text = text[len(preamble):]
return sha(text.replace('\r\n', '\n').encode())
def x86_recipe(root=ROOT, commit=None):
path = 'tools/components/recipes/x86-preloads.sh'
if commit and not run('git', 'ls-tree', commit, path, root=root):
workflow = run('git', 'show', f'{commit}:.github/workflows/build.yml', root=root).decode()
section = workflow[workflow.index(' - name: Build the x86 preloads'):]
text = section.split(' run: |\n', 1)[1].split('\n - name:', 1)[0]
text = textwrap.dedent(text).rstrip() + '\n'
else:
text = (run('git', 'show', f'{commit}:{path}', root=root).decode() if commit
else (root / path).read_text())
text = text.removeprefix('#!/usr/bin/env bash\n')
return sha(text.replace('\r\n', '\n').encode())
def artifact_bytes(origin, cache):
build = api(f"actions/runs/{origin['run']}")
if (build['event'] != 'push' or build['head_branch'] != 'main' or build['conclusion'] != 'success'
or build['head_sha'] != origin['commit'] or build['path'] != '.github/workflows/build.yml'
or build['repository']['full_name'] != REPO):
raise ValueError('Snapshot is not from a successful main APK build')
artifact = api(f"actions/artifacts/{origin['artifact']}")
if (artifact['name'] != 'droiddeck-apk' or artifact['workflow_run']['id'] != origin['run']
or artifact['digest'] != 'sha256:' + origin['artifact_sha256']):
raise ValueError('Original APK artifact identity or digest mismatch')
cache.mkdir(parents=True, exist_ok=True)
path = cache / (origin['artifact_sha256'] + '.zip')
if not path.exists():
path.write_bytes(run('gh', 'api', f"repos/{REPO}/actions/artifacts/{origin['artifact']}/zip"))
content = path.read_bytes()
if sha(content) != origin['artifact_sha256']:
raise ValueError('Original APK archive checksum mismatch')
with ZipFile(io.BytesIO(content)) as archive:
names = [name for name in archive.namelist() if name.endswith('.apk')]
if len(names) != 1:
raise ValueError('Expected one original APK')
with ZipFile(io.BytesIO(archive.read(names[0]))) as apk:
return {name: apk.read('assets/linuxfs/' + name) for name in FILES}
def validate_lock(data):
if data.get('version') != 1 or set(data['groups']) != set(INPUTS):
raise ValueError('Unsupported preload snapshot')
if set(data['outputs']) != FILES:
raise ValueError('Unexpected preload output set')
for digest in [data['archive_sha256'], *data['outputs'].values()]:
if len(digest) != 64 or any(c not in '0123456789abcdef' for c in digest):
raise ValueError('Invalid preload checksum')
def producer_sources(group, commit, root=ROOT):
config = 'tools/components/recipes.json'
if run('git', 'ls-tree', commit, config, root=root):
definitions = json.loads(run('git', 'show', f'{commit}:{config}', root=root))
name = 'arm64-preloads' if group == 'arm64' else 'x86-preloads'
patterns = definitions[name]['source_inputs']
else:
# Older approved builds kept ARM64 commands inline, before the Vulkan/VR build.
if group == 'arm64':
text = run('git', 'show', f'{commit}:.github/workflows/build.yml', root=root).decode()
text = text[text.index(' aarch64-linux-gnu-g++ -shared'):]
text = text[:text.index(' VK_INCLUDE=')]
else:
text = run('git', 'show', f'{commit}:tools/linuxfs/build-x86-preloads.sh', root=root).decode()
patterns = re.findall(r'\b((?:tools|app)/[\w./*+-]+\.(?:cpp|c|h))(?=\s|$)', text)
if group == 'x86':
patterns += ['tools/linuxfs/fex', 'tools/linuxfs/build-x86-preloads.sh', 'tools/linuxfs/debian-image.env']
return source_files(group, root, commit, patterns)
def prove_origin(data, cache, root=ROOT):
content = artifact_bytes(data['origin'], cache)
if {name: sha(value) for name, value in content.items()} != data['outputs']:
raise ValueError('Snapshot binaries differ from the original main APK')
commit = data['origin']['commit']
run('git', 'fetch', '--no-tags', 'origin', commit, root=root)
for group in INPUTS:
if source_files(group, root, commit) != data['groups'][group]:
raise ValueError('Snapshot inputs differ from the producing source')
for name, checksum in producer_sources(group, commit, root).items():
if data['groups'][group].get(name) != checksum:
raise ValueError('Snapshot omits or changes a producing build dependency')
if recipe(root, commit) != data['arm64_recipe']:
raise ValueError('Snapshot recipe differs from the producing source')
if x86_recipe(root, commit) != data['x86_recipe']:
raise ValueError('Snapshot x86 recipe differs from the producing source')
def unpack(content, data):
if sha(content) != data['archive_sha256']:
raise ValueError('Preload archive checksum mismatch')
result = {}
total = 0
with tarfile.open(fileobj=io.BytesIO(content), mode='r:gz') as archive:
for entry in archive:
if entry.name not in FILES or entry.name in result or not entry.isfile() or entry.size > 32 * 1024 * 1024:
raise ValueError('Unsafe or unexpected preload archive entry')
total += entry.size
if total > 64 * 1024 * 1024:
raise ValueError('Preload archive exceeds expanded size limit')
value = archive.extractfile(entry).read()
if sha(value) != data['outputs'][entry.name]:
raise ValueError('Preload binary checksum mismatch')
result[entry.name] = value
if set(result) != FILES:
raise ValueError('Incomplete preload archive')
return result
def restore(root=ROOT):
data = json.loads((root / LOCK).read_text())
validate_lock(data)
matching = {group: source_files(group, root) == data['groups'][group] for group in INPUTS}
matching['arm64'] &= recipe(root) == data['arm64_recipe']
matching['x86'] &= x86_recipe(root) == data.get('x86_recipe')
if os.environ.get('FORCE_PRELOAD_REBUILD') == 'true':
matching = dict.fromkeys(INPUTS, False)
if any(matching.values()):
base = os.environ.get('COMPONENT_TRUSTED_BASE')
approved = None
if base:
result = subprocess.run(['git', 'show', f'{base}:{LOCK}'], cwd=root, capture_output=True)
if result.returncode == 0:
approved = json.loads(result.stdout)
if approved != data:
try:
prove_origin(data, root / 'app/build/component-downloads', root)
except (subprocess.CalledProcessError, OSError, http.client.HTTPException):
print('Snapshot origin is unavailable; rebuilding without trusting the snapshot.')
matching = dict.fromkeys(INPUTS, False)
if any(matching.values()):
url = (f"https://github.com/Droid-Deck/DroidDeck-Components/releases/download/"
f"preloads-{data['archive_sha256']}/preloads.tar.gz")
try:
with urllib.request.urlopen(url, timeout=60) as response:
content = response.read(64 * 1024 * 1024)
except (OSError, http.client.HTTPException) as error:
if isinstance(error, urllib.error.HTTPError) and error.code not in (404, 429) and error.code < 500:
raise
print('Preload snapshot is unavailable; rebuilding from source.')
matching = dict.fromkeys(INPUTS, False)
else:
binaries = unpack(content, data)
for group, matches in matching.items():
if matches:
for name in OUTPUTS[group]:
path = root / 'app/src/main/assets/linuxfs' / name
if not path.resolve().is_relative_to(root.resolve()) or path.is_symlink():
raise ValueError('Preload output escapes repository or is a symlink')
path.parent.mkdir(parents=True, exist_ok=True)
path.write_bytes(binaries[name])
path.chmod(0o755 if name.endswith('droiddeck-clipboard') else 0o644)
for group, matches in matching.items():
print(f"{group}: {'verified snapshot reused' if matches else 'inputs changed or snapshot unavailable; rebuild required'}")
if os.environ.get('GITHUB_OUTPUT'):
with open(os.environ['GITHUB_OUTPUT'], 'a') as output:
output.write(f'{group}={str(matches).lower()}\n')
return matching
def snapshot(run_id, destination, root=ROOT):
build = api(f'actions/runs/{run_id}')
candidates = api(f'actions/runs/{run_id}/artifacts?per_page=100')['artifacts']
artifact = next(a for a in candidates if a['name'] == 'droiddeck-apk')
origin = dict(run=run_id, commit=build['head_sha'], artifact=artifact['id'],
artifact_sha256=artifact['digest'].removeprefix('sha256:'),
url=build['html_url'])
content = artifact_bytes(origin, root / 'app/build/component-downloads')
run('git', 'fetch', '--no-tags', 'origin', origin['commit'], root=root)
data = dict(version=1, origin=origin, groups={g: source_files(g, root, origin['commit']) for g in INPUTS},
arm64_recipe=recipe(root, origin['commit']), x86_recipe=x86_recipe(root, origin['commit']),
outputs={name: sha(value) for name, value in content.items()},
toolchain=dict(arm64='Ubuntu 24.04 GCC cross compiler; exact build commands pinned by arm64_recipe',
x86='Debian bullseye compiler image and recipe pinned by source inputs'))
stream = io.BytesIO()
with gzip.GzipFile(fileobj=stream, mode='wb', mtime=0) as compressed:
with tarfile.open(fileobj=compressed, mode='w') as archive:
for name, value in sorted(content.items()):
entry = tarfile.TarInfo(name)
entry.size, entry.mode = len(value), 0o644
archive.addfile(entry, io.BytesIO(value))
destination.mkdir(parents=True, exist_ok=True)
(destination / 'preloads.tar.gz').write_bytes(stream.getvalue())
data['archive_sha256'] = sha(stream.getvalue())
(root / LOCK).write_text(json.dumps(data, indent=2, sort_keys=True) + '\n')
print('Snapshot SHA-256: ' + data['archive_sha256'])
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('operation', choices=['restore', 'snapshot'])
parser.add_argument('--run', type=int)
parser.add_argument('--out', type=Path, default=ROOT / 'app/build/component-snapshot')
args = parser.parse_args()
if args.operation == 'restore':
restore()
else:
snapshot(args.run, args.out)
+167
View File
@@ -0,0 +1,167 @@
"""One entry point for runtime recipes, cache identities and verified job handoff."""
import argparse
import hashlib
import json
import os
from pathlib import Path, PurePosixPath
import shutil
import subprocess
import tarfile
ROOT = Path(__file__).resolve().parents[2]
REGISTRY = 'tools/components/recipes.json'
TREES = ['app/src/main/assets/linuxfs', 'app/src/main/assets/directaudio',
'app/src/main/assets/droiddeck-esync', 'app/build/prepared-assets',
'app/build/component-records']
LIBRARIES = ['app/src/main/jniLibs/arm64-v8a/' + name for name in
('libproot.so', 'libproot-loader.so', 'libdirectaudiorelay.so')]
MANIFEST = 'app/build/prepared-components.json'
def digest(path):
with path.open('rb') as stream:
return hashlib.file_digest(stream, 'sha256').hexdigest()
def registry(root=ROOT):
return json.loads((root / REGISTRY).read_text())
def identity(name, root=ROOT):
spec = registry(root)[name]
paths = subprocess.check_output(['git', 'ls-files', '--cached', '--others', '--exclude-standard',
'--', *spec['inputs'], *spec.get('source_inputs', [])], cwd=root, text=True).splitlines()
values = {}
for name_ in sorted(set(paths)):
if '__pycache__' in name_ or name_.endswith(('.pyc', '.md')):
continue
content = (root / name_).read_bytes()
if b'\0' not in content:
content = content.replace(b'\r\n', b'\n')
values[name_] = hashlib.sha256(content).hexdigest()
policy = dict(spec=spec, sources=values, runner=os.environ.get('RUNNER_OS', 'Linux'),
architecture=os.environ.get('RUNNER_ARCH', 'X64'))
entrypoint = root / 'tools/components/prepare.py'
if entrypoint.is_file():
policy['entrypoint'] = hashlib.sha256(entrypoint.read_bytes().replace(b'\r\n', b'\n')).hexdigest()
installer = root / 'tools/ci/install-ubuntu-packages.sh'
if spec.get('packages') and installer.is_file():
policy['installer'] = hashlib.sha256(installer.read_bytes().replace(b'\r\n', b'\n')).hexdigest()
if spec.get('ndk'):
policy['ndk'] = os.environ.get('NDK_VERSION', '')
policy['ndk_policy'] = hashlib.sha256((root / 'tools/ci/toolchains.env').read_bytes().replace(b'\r\n', b'\n')).hexdigest()
return hashlib.sha256(json.dumps(policy, sort_keys=True).encode()).hexdigest()
def prepare(name, root=ROOT):
spec = registry(root)[name]
prepare_tools([name], root)
if 'command' in spec:
command = spec['command']
else:
script = (root / spec['script']).resolve()
if not script.is_relative_to((root / 'tools/components').resolve()):
raise ValueError('Recipe script escapes components directory')
command = ['bash', str(script)]
environment = dict(os.environ)
if spec.get('source'):
environment.update(COMPONENT_SOURCE_URL=spec['source']['url'],
COMPONENT_SOURCE_SHA256=spec['source']['sha256'])
subprocess.run(command, cwd=root, env=environment, check=True)
missing = [name_ for name_ in spec['outputs'] if not (root / name_).is_file()]
if missing:
raise ValueError(f'{name} did not prepare required outputs: {missing}')
record = dict(component=name, input_sha256=identity(name, root),
outputs={p: digest(root / p) for p in spec['outputs']})
destination = root / 'app/build/component-records' / (name + '.json')
destination.parent.mkdir(parents=True, exist_ok=True)
destination.write_text(json.dumps(record, sort_keys=True, indent=2) + '\n')
def prepare_tools(names, root=ROOT):
packages = sorted({package for name in names for package in registry(root)[name].get('packages', [])})
if packages:
subprocess.run(['bash', 'tools/ci/install-ubuntu-packages.sh', *packages], cwd=root, check=True)
def files(root=ROOT):
result = []
for name in TREES:
folder = root / name
if not folder.is_dir() or folder.is_symlink():
raise ValueError('Missing prepared component tree: ' + name)
for path in folder.rglob('*'):
if path.is_symlink():
raise ValueError('Symlink in prepared components: ' + str(path))
if path.is_file():
result.append(path.relative_to(root).as_posix())
for name in LIBRARIES:
if not (root / name).is_file() or (root / name).is_symlink():
raise ValueError('Missing prepared component library: ' + name)
result.append(name)
return sorted(result)
def pack(destination, root=ROOT):
names = files(root)
tree = subprocess.check_output(['git', 'rev-parse', 'HEAD^{tree}'], cwd=root, text=True).strip()
data = dict(version=1, source_tree=tree, run=os.environ.get('GITHUB_RUN_ID', ''),
recipes={name: identity(name, root) for name in registry(root)},
files={name: digest(root / name) for name in names})
(root / MANIFEST).write_text(json.dumps(data, sort_keys=True, indent=2) + '\n')
destination.parent.mkdir(parents=True, exist_ok=True)
with tarfile.open(destination, mode='w:gz', compresslevel=1) as archive:
for name in names + [MANIFEST]:
archive.add(root / name, arcname=name, recursive=False)
def unpack(archive_path, source_tree, root=ROOT):
total = 0
seen = set()
with tarfile.open(archive_path, mode='r:gz') as archive:
for entry in archive:
allowed = entry.name in LIBRARIES + [MANIFEST] or any(entry.name.startswith(p + '/') for p in TREES)
path = root / entry.name
total += entry.size
if (not allowed or '\\' in entry.name or '..' in PurePosixPath(entry.name).parts
or PurePosixPath(entry.name).as_posix() != entry.name
or not entry.isfile() or entry.name in seen or len(seen) >= 100000
or entry.size > 512 * 1024 * 1024
or total > 2 * 1024**3 or not path.resolve().is_relative_to(root.resolve())
or path.is_symlink()):
raise ValueError('Unsafe prepared component archive')
seen.add(entry.name)
path.parent.mkdir(parents=True, exist_ok=True)
with path.open('wb') as output:
shutil.copyfileobj(archive.extractfile(entry), output)
path.chmod(entry.mode & 0o777)
data = json.loads((root / MANIFEST).read_text())
actual = {name: digest(root / name) for name in files(root)}
recipes = {name: identity(name, root) for name in registry(root)}
if (data.get('version') != 1 or data['source_tree'] != source_tree or data['files'] != actual
or data['recipes'] != recipes):
raise ValueError('Prepared components do not match the pinned source tree and file hashes')
print('Prepared component source identity and all output hashes verified.')
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('recipe')
parser.add_argument('--archive', type=Path, default=ROOT / 'app/build/prepared-components.tar.gz')
parser.add_argument('--tree')
args = parser.parse_args()
if args.recipe == 'plan':
for name in registry():
value = identity(name)
print(f'{name}: {value}')
if os.environ.get('GITHUB_OUTPUT'):
with open(os.environ['GITHUB_OUTPUT'], 'a') as stream:
stream.write(f'{name}={value}\n')
elif args.recipe == 'tools':
prepare_tools(os.environ.get('COMPONENTS_TO_BUILD', '').split())
elif args.recipe == 'pack':
pack(args.archive)
elif args.recipe == 'unpack':
unpack(args.archive, args.tree)
else:
prepare(args.recipe)
+225
View File
@@ -0,0 +1,225 @@
{
"runtime": {
"script": "tools/components/recipes/runtime.sh",
"inputs": [
"tools/components/recipes/runtime.sh",
"tools/quest",
"tools/linuxfs/overlay",
"tools/linuxfs/desktop",
"tools/linuxfs/licenses",
"tools/linuxfs/uruntime.env",
"tools/gamescope/release.env",
"tools/wlroots/release.env",
"tools/mangoapp",
"tools/msitools",
"tools/pipewire",
"tools/vr",
"app/src/main/cpp/waylandcomp/generated",
"app/src/main/cpp/waylandcomp/src/headpose_shm.h",
"app/src/main/cpp/waylandcomp/src/spatialpose_shm.h",
"app/src/main/cpp/venc/venc_proto.h"
],
"outputs": [
"app/src/main/assets/linuxfs/usr/local/bin/gamescope",
"app/src/main/assets/linuxfs/usr/local/lib/droiddeck-vr/droiddeck/bin/linuxarm64/driver_droiddeck.so"
],
"ndk": true,
"packages": [
"gcc-aarch64-linux-gnu",
"g++-aarch64-linux-gnu",
"libvulkan1",
"mesa-vulkan-drivers"
]
},
"esync": {
"script": "tools/components/recipes/esync.sh",
"inputs": [
"tools/components/recipes/esync.sh",
"tools/droiddeck-esync/release.env"
],
"outputs": [
"app/src/main/assets/droiddeck-esync/index.json",
"app/src/main/assets/droiddeck-esync/index.json.sig"
]
},
"debian-image": {
"script": "tools/components/recipes/debian-image.sh",
"inputs": [
"tools/components/recipes/debian-image.sh"
],
"outputs": []
},
"x86-preloads": {
"script": "tools/components/recipes/x86-preloads.sh",
"inputs": [
"tools/components/recipes/x86-preloads.sh",
"tools/linuxfs/build-x86-preloads.sh",
"tools/linuxfs/preload",
"tools/linuxfs/fex"
],
"outputs": [
"app/src/main/assets/linuxfs/x86_64/libfakeinput.so",
"app/src/main/assets/linuxfs/x86_64/libblsession.so",
"app/src/main/assets/linuxfs/x86_64/libfaultreport.so",
"app/src/main/assets/linuxfs/i386/libfakeinput.so",
"app/src/main/assets/linuxfs/i386/libblsession.so",
"app/src/main/assets/linuxfs/i386/libfaultreport.so",
"app/src/main/assets/linuxfs/x86_64/libwebkitinput.so",
"app/src/main/assets/linuxfs/x86_64/libvulkan-thunk.so"
],
"source_inputs": [
"app/src/main/cpp/fakeinput_steam.cpp",
"app/src/main/cpp/adrenotools/src/hook/kgsl.h",
"app/src/main/cpp/venc/venc_proto.h",
"tools/linuxfs/preload",
"tools/quest/kgsl_compat.c",
"tools/quest/kgsl_compat.h",
"tools/linuxfs/fex",
"tools/linuxfs/build-x86-preloads.sh",
"tools/linuxfs/debian-image.env"
],
"snapshot_outputs": [
"x86_64/libfakeinput.so",
"x86_64/libblsession.so",
"x86_64/libfaultreport.so",
"i386/libfakeinput.so",
"i386/libblsession.so",
"i386/libfaultreport.so",
"x86_64/libwebkitinput.so",
"x86_64/libvulkan-thunk.so"
]
},
"pulseaudio-source": {
"script": "tools/components/recipes/pulseaudio-source.sh",
"inputs": [
"tools/components/recipes/pulseaudio-source.sh"
],
"outputs": [
"pa13/src/pulsecore/module.h"
],
"source": {
"url": "https://github.com/pulseaudio/pulseaudio/archive/refs/tags/v13.0.tar.gz",
"sha256": "bd7980bc9bd44bd3b3363545dabd19476ebf4d6bb6d2527e7d179256394a6758"
}
},
"directaudio": {
"script": "tools/components/recipes/directaudio.sh",
"inputs": [
"tools/components/recipes/directaudio.sh",
"tools/directaudio"
],
"outputs": [
"app/src/main/jniLibs/arm64-v8a/libdirectaudiorelay.so"
]
},
"audio": {
"script": "tools/components/recipes/audio.sh",
"inputs": [
"tools/aaudio-sink",
"tools/directaudio",
"tools/components/recipes/audio.sh",
"app/src/main/assets/pulseaudio.tzst"
],
"outputs": [
"app/build/prepared-assets/pulseaudio.tzst"
],
"ndk": true
},
"proot": {
"script": "tools/components/recipes/proot.sh",
"inputs": [
"tools/proot",
"tools/components/recipes/proot.sh"
],
"outputs": [
"app/src/main/jniLibs/arm64-v8a/libproot.so",
"app/src/main/jniLibs/arm64-v8a/libproot-loader.so"
],
"ndk": true
},
"comet": {
"script": "tools/components/recipes/comet.sh",
"inputs": [
"tools/comet",
"tools/components/recipes/comet.sh"
],
"outputs": [
"app/src/main/assets/linuxfs/usr/local/lib/droiddeck-comet/comet",
"app/src/main/assets/linuxfs/usr/local/lib/droiddeck-comet/version"
],
"packages": [
"gcc-aarch64-linux-gnu"
]
},
"arm64-preloads": {
"script": "tools/components/recipes/arm64-preloads.sh",
"inputs": [
"tools/components/build-arm64-preloads.sh",
"tools/linuxfs/preload",
"app/src/main/cpp/fakeinput_steam.cpp",
"tools/components/recipes/arm64-preloads.sh"
],
"outputs": [
"app/src/main/assets/linuxfs/libfakeinput.so",
"app/src/main/assets/linuxfs/libblsession.so",
"app/src/main/assets/linuxfs/libblfastpath.so",
"app/src/main/assets/linuxfs/libblaudit.so",
"app/src/main/assets/linuxfs/libssbs.so",
"app/src/main/assets/linuxfs/usr/local/bin/droiddeck-clipboard"
],
"packages": [
"gcc-aarch64-linux-gnu",
"g++-aarch64-linux-gnu"
],
"source_inputs": [
"app/src/main/cpp/fakeinput_steam.cpp",
"app/src/main/cpp/adrenotools/src/hook/kgsl.h",
"app/src/main/cpp/venc/venc_proto.h",
"tools/linuxfs/preload",
"tools/quest/kgsl_compat.c",
"tools/quest/kgsl_compat.h",
"tools/proot/fastpath",
"tools/linuxfs/ssbs",
"tools/linuxfs/clipboard"
],
"snapshot_outputs": [
"libfakeinput.so",
"libblsession.so",
"libblfastpath.so",
"libblaudit.so",
"libssbs.so",
"usr/local/bin/droiddeck-clipboard"
]
},
"preload-snapshot": {
"command": [
"python3",
"tools/components/preloads.py",
"restore"
],
"inputs": [
"tools/components/preloads.py",
"tools/components/preloads.lock.json"
],
"outputs": []
},
"xbox": {
"script": "tools/components/recipes/xbox.sh",
"inputs": [
"tools/xbox",
"tools/components/recipes/xbox.sh"
],
"outputs": [
"app/src/main/assets/linuxfs/usr/local/lib/droiddeck-xbox/xodus-cli",
"app/src/main/assets/linuxfs/usr/local/lib/droiddeck-xbox/xodus-service"
],
"packages": [
"build-essential",
"pkg-config",
"libssl-dev",
"protobuf-compiler",
"cmake",
"xz-utils"
]
}
}
@@ -0,0 +1,3 @@
#!/usr/bin/env bash
set -euo pipefail
bash tools/components/build-arm64-preloads.sh app/src/main/assets/linuxfs
+14
View File
@@ -0,0 +1,14 @@
#!/usr/bin/env bash
set -euo pipefail
export NDK="${ANDROID_NDK_LATEST_HOME:-${ANDROID_NDK_HOME:-$ANDROID_NDK_ROOT}}"
tools/aaudio-sink/build.sh "$PWD/pa13" "$PWD/sink-out"
rm -rf pa-bundle && mkdir -p pa-bundle
tar --use-compress-program=unzstd -xf app/src/main/assets/pulseaudio.tzst -C pa-bundle
test ! -f pa-bundle/modules/arm64/module-aaudio-sink.so
install -m755 sink-out/module-aaudio-sink.so pa-bundle/modules/arm64/module-aaudio-sink.so
install -m755 sink-out/module-directaudio-native-sink.so pa-bundle/modules/arm64/module-directaudio-native-sink.so
(cd pa-bundle && tar --use-compress-program='zstd -19' -cf ../pulseaudio.tzst .)
mkdir -p app/build/prepared-assets
mv pulseaudio.tzst app/build/prepared-assets/pulseaudio.tzst
tar --use-compress-program=unzstd -tf app/build/prepared-assets/pulseaudio.tzst | grep -c '\.so$'
rm -rf pa-bundle sink-out
+5
View File
@@ -0,0 +1,5 @@
#!/usr/bin/env bash
set -euo pipefail
rustup toolchain install 1.90.0 --profile minimal
rustup target add --toolchain 1.90.0 aarch64-unknown-linux-gnu
RUSTUP_TOOLCHAIN=1.90.0 bash tools/comet/build.sh
+18
View File
@@ -0,0 +1,18 @@
#!/usr/bin/env bash
set -euo pipefail
. tools/linuxfs/debian-image.env
image=public.ecr.aws/docker/library/debian:bullseye
if gh release download "$DEBIAN_IMAGE_TAG" -R "$GITHUB_REPOSITORY" -p "$DEBIAN_IMAGE_ASSET" -O /tmp/debian-image.tar.zst \
&& { [ -z "$DEBIAN_IMAGE_SHA256" ] || echo "$DEBIAN_IMAGE_SHA256 /tmp/debian-image.tar.zst" | sha256sum -c -; }; then
zstd -dc /tmp/debian-image.tar.zst | docker load
rm -f /tmp/debian-image.tar.zst
else
echo "::warning::the hosted Debian image could not be fetched; pulling from the registry instead"
for attempt in 1 2 3 4 5; do
docker pull "$image" && break
[ "$attempt" -lt 5 ] || { echo "::error::could not pull $image after $attempt attempts"; exit 1; }
echo "pull attempt $attempt failed (registry busy?); retrying in $((attempt * 30)) s"
sleep $((attempt * 30))
done
fi
docker image ls "$image"
+3
View File
@@ -0,0 +1,3 @@
#!/usr/bin/env bash
set -euo pipefail
tools/directaudio/fetch.sh "$PWD" "$PWD/sink-out"
+23
View File
@@ -0,0 +1,23 @@
#!/usr/bin/env bash
set -euo pipefail
if [ -f tools/droiddeck-esync/release.env ]; then
. tools/droiddeck-esync/release.env
release_url="https://github.com/${SYNC_BUNDLE_REPO}/releases/download/${SYNC_BUNDLE_TAG}"
curl -fsSL --retry 3 -o sync-bundle.tzst "$release_url/$SYNC_BUNDLE_ASSET"
echo "$SYNC_BUNDLE_SHA256 sync-bundle.tzst" | sha256sum -c -
mkdir -p app/src/main/assets/droiddeck-esync
tar --use-compress-program=unzstd -xf sync-bundle.tzst -C app/src/main/assets/droiddeck-esync
rm sync-bundle.tzst
curl -fsSL --retry 3 -o sync-index.json "$release_url/index.json"
revoked=$(python3 -c 'import json, sys; print(" ".join(p["id"] for p in json.load(open(sys.argv[1]))["packs"] if p.get("revoked") is True))' sync-index.json)
for id in $revoked; do
if [ -e "app/src/main/assets/droiddeck-esync/packs/$id.tzst" ]; then
echo "::warning::$SYNC_BUNDLE_ASSET carries revoked pack $id; it is left out of the APK (point tools/droiddeck-esync/release.env at a newer bundle)"
rm -f "app/src/main/assets/droiddeck-esync/packs/$id.tzst"
fi
done
rm sync-index.json
test -f app/src/main/assets/droiddeck-esync/index.json
test -f app/src/main/assets/droiddeck-esync/index.json.sig
ls -lR app/src/main/assets/droiddeck-esync
fi
+4
View File
@@ -0,0 +1,4 @@
#!/usr/bin/env bash
set -euo pipefail
export NDK="${ANDROID_NDK_LATEST_HOME:-${ANDROID_NDK_HOME:-$ANDROID_NDK_ROOT}}"
tools/proot/build.sh app/src/main/jniLibs/arm64-v8a
@@ -0,0 +1,5 @@
#!/usr/bin/env bash
set -euo pipefail
curl -fsSL --retry 3 -o pa.tar.gz "$COMPONENT_SOURCE_URL"
echo "$COMPONENT_SOURCE_SHA256 pa.tar.gz" | sha256sum -c -
mkdir -p pa13 && tar -xzf pa.tar.gz -C pa13 --strip-components=1 && rm pa.tar.gz
+83
View File
@@ -0,0 +1,83 @@
#!/usr/bin/env bash
set -euo pipefail
D=app/src/main/assets/linuxfs
mkdir -p "$D"
VK_INCLUDE=$(ls -d "${ANDROID_NDK_LATEST_HOME:-${ANDROID_NDK_HOME:-$ANDROID_NDK_ROOT}}"/toolchains/llvm/prebuilt/*/sysroot/usr/include | head -1) \
tools/vr/build.sh --native "$D/usr/local/lib/droiddeck-vr"
for script in tools/linuxfs/overlay/usr/local/bin/droiddeck-* tools/linuxfs/overlay/usr/local/bin/steam-compatibility; do
install -Dm644 "$script" "$D/usr/local/bin/$(basename "$script")"
done
for f in tools/linuxfs/overlay/usr/bin/* tools/linuxfs/overlay/usr/bin/steamos-polkit-helpers/*; do
[ -f "$f" ] && install -Dm644 "$f" "$D/${f#tools/linuxfs/overlay/}"
done
if [ -f tools/gamescope/release.env ]; then
. tools/gamescope/release.env
curl -fsSL --retry 3 -o gamescope.tzst "https://github.com/${GAMESCOPE_REPO:-${GITHUB_REPOSITORY}}/releases/download/${GAMESCOPE_TAG}/gamescope.tzst"
echo "${GAMESCOPE_SHA256} gamescope.tzst" | sha256sum -c -
tar --use-compress-program=unzstd -xf gamescope.tzst -C "$D"
test -f "$D/usr/local/bin/gamescope"
rm gamescope.tzst
fi
. tools/linuxfs/uruntime.env
curl -fsSL --retry 3 -o uruntime "https://github.com/VHSgunzo/uruntime/releases/download/${URUNTIME_VERSION}/${URUNTIME_ASSET}"
echo "${URUNTIME_SHA256} uruntime" | sha256sum -c -
install -Dm644 uruntime "$D/usr/local/lib/droiddeck/uruntime"
install -Dm644 tools/linuxfs/licenses/uruntime-LICENSE "$D/usr/local/share/licenses/uruntime/LICENSE"
rm uruntime
M="$D/usr/local/lib/mangoapp"
mkdir -p "$M" mango-pkgs
grep -v '^#' tools/mangoapp/packages.txt | while read -r sum url; do
[ -n "$url" ] || continue
curl -fsSL --retry 3 -o mango-pkgs/pkg.tar.zst "$url"
echo "$sum mango-pkgs/pkg.tar.zst" | sha256sum -c -
tar --use-compress-program=unzstd -xf mango-pkgs/pkg.tar.zst -C mango-pkgs
rm mango-pkgs/pkg.tar.zst
done
install -m644 mango-pkgs/usr/bin/mangoapp "$M/mangoapp"
for so in libfmt.so.10 libspdlog.so.1.13 libglfw.so.3 libtraceevent.so.1; do
cp -L "mango-pkgs/usr/lib/$so" "$M/$so"
done
aarch64-linux-gnu-gcc -shared -fPIC -O2 -Wall -Wl,-soname,libtracefs.so.1 \
-o "$M/libtracefs.so.1" tools/mangoapp/libtracefs-shim.c
aarch64-linux-gnu-strip --strip-unneeded "$M/libtracefs.so.1"
install -Dm644 tools/mangoapp/mangoapp "$D/usr/local/bin/mangoapp"
rm -rf mango-pkgs
ls -l "$M"
mkdir -p pw-pkgs
grep -v '^#' tools/pipewire/packages.txt | while read -r sum url; do
[ -n "$url" ] || continue
curl -fsSL --retry 3 -o pw-pkgs/pkg.tar.zst "$url"
echo "$sum pw-pkgs/pkg.tar.zst" | sha256sum -c -
tar --use-compress-program=unzstd -xf pw-pkgs/pkg.tar.zst -C pw-pkgs
rm pw-pkgs/pkg.tar.zst
done
tools/pipewire/assemble.sh pw-pkgs "$D/usr/local/lib/droiddeck-pipewire.tzst"
rm -rf pw-pkgs
ls -l "$D/usr/local/lib/droiddeck-pipewire.tzst"
T="$D/usr/local/lib/droiddeck-msitools"
mkdir -p "$T" msi-pkgs
grep -v '^#' tools/msitools/packages.txt | while read -r sum url; do
[ -n "$url" ] || continue
curl -fsSL --retry 3 -o msi-pkgs/pkg.tar.xz "$url"
echo "$sum msi-pkgs/pkg.tar.xz" | sha256sum -c -
tar -xJf msi-pkgs/pkg.tar.xz -C msi-pkgs
rm msi-pkgs/pkg.tar.xz
done
install -m644 msi-pkgs/usr/bin/msiinfo "$T/msiinfo"
install -m644 msi-pkgs/usr/bin/cabextract "$T/cabextract"
install -m644 msi-pkgs/usr/lib/7zip/7z "$T/7z"
install -m644 msi-pkgs/usr/lib/7zip/7z.so "$T/7z.so"
for so in libmsi-1.0.so.0 libgsf-1.so.114 libgcab-1.0.so.0; do
cp -L "msi-pkgs/usr/lib/$so" "$T/$so"
done
for f in NOTICE GPL-2 GPL-3 LGPL-2.1 7zip-License 7zip-unRarLicense; do
install -Dm644 "tools/msitools/licenses/$f" "$D/usr/local/share/licenses/droiddeck-msitools/$f"
done
rm -rf msi-pkgs
ls -l "$T"
install -Dm644 tools/linuxfs/desktop/droiddeck-desktop "$D/usr/local/bin/droiddeck-desktop"
install -Dm644 tools/linuxfs/desktop/droiddeck-gpu "$D/usr/local/bin/droiddeck-gpu"
install -Dm644 tools/linuxfs/desktop/droiddeck-desktop-gpu "$D/usr/local/bin/droiddeck-desktop-gpu"
install -Dm644 tools/linuxfs/desktop/kwin_wayland_wrapper "$D/usr/local/bin/kwin_wayland_wrapper"
install -Dm644 tools/linuxfs/desktop/firefox-droiddeck.js "$D/usr/lib/firefox/defaults/pref/droiddeck.js"
ls -lR "$D"
+17
View File
@@ -0,0 +1,17 @@
#!/usr/bin/env bash
set -euo pipefail
docker run --rm -v "$PWD:/src" -w /src public.ecr.aws/docker/library/debian:bullseye bash -c '
set -euo pipefail
install_compilers() {
apt-get -o Acquire::Check-Valid-Until=false update -qq &&
DEBIAN_FRONTEND=noninteractive apt-get install -y -qq --no-install-recommends gcc g++ gcc-multilib g++-multilib binutils >/dev/null
}
printf "deb http://deb.debian.org/debian bullseye main\ndeb http://security.debian.org/debian-security bullseye-security main\n" > /etc/apt/sources.list
if ! install_compilers; then
printf "deb http://archive.debian.org/debian bullseye main\ndeb http://archive.debian.org/debian-security bullseye-security main\n" > /etc/apt/sources.list
install_compilers
fi
tools/linuxfs/build-x86-preloads.sh app/src/main/assets/linuxfs
'
sudo chown -R "$(id -u):$(id -g)" app/src/main/assets/linuxfs
ls -l app/src/main/assets/linuxfs/x86_64 app/src/main/assets/linuxfs/i386
+4
View File
@@ -0,0 +1,4 @@
#!/usr/bin/env bash
set -euo pipefail
rustup toolchain install 1.99.0 --profile minimal
bash tools/xbox/build.sh
@@ -259,24 +259,91 @@ def focus_snapshot():
STEAM_APP_CLASS = re.compile(r"steam_app_(\d+)$")
def tag_game_windows(snap):
if os.environ.get("BL_STEAM_CLIENT_ARCH") != "x86_64":
return False
def process_env(pid, proc=Path("/proc")):
try:
return dict(entry.split(b"=", 1) for entry in (proc / str(pid) / "environ").read_bytes().split(b"\0") if b"=" in entry)
except OSError:
return {}
def steam_app_id(env):
try:
app = int(env.get(b"SteamAppId", b"0"))
except ValueError:
return None
return app if 0 < app < 2**32 else None
def under_reaper(pid, proc=Path("/proc")):
for _ in range(64):
try:
if (proc / str(pid) / "comm").read_text().strip() == "reaper":
return True
stat = (proc / str(pid) / "stat").read_text()
pid = int(stat[stat.rindex(")") + 2:].split()[1])
except (OSError, ValueError, IndexError):
return False
if pid <= 1:
return False
return False
def sandboxed_apps(proc=Path("/proc")):
apps = {}
try:
pids = [entry for entry in os.listdir(proc) if entry.isdigit()]
except OSError:
return apps
for pid in pids:
env = process_env(pid, proc)
flatpak, app = env.get(b"FLATPAK_ID"), steam_app_id(env)
if flatpak and app:
name = flatpak.decode(errors="replace").lower()
for key in {name, name.rsplit(".", 1)[-1]}:
apps[key] = app if apps.get(key, app) == app else None
return apps
def window_steam_app(win, flatpaks, proc=Path("/proc")):
match = STEAM_APP_CLASS.match(win.get("class") or "")
if match:
return match.group(1) if os.environ.get("BL_STEAM_CLIENT_ARCH") == "x86_64" else None
pid = win.get("pid")
if pid:
if under_reaper(pid, proc):
return None
app = steam_app_id(process_env(pid, proc))
if app:
return str(app)
app = flatpaks().get((win.get("class") or "").lower())
return str(app) if app else None
def tag_game_windows(snap, proc=Path("/proc")):
tagged = False
found = []
def flatpaks():
if not found:
found.append(sandboxed_apps(proc))
return found[0]
for display, wins in snap.get("displays", {}).items():
if display == os.environ.get("DISPLAY"):
continue
for win in wins:
match = STEAM_APP_CLASS.match(win.get("class") or "")
if not match or win.get("appId") or win.get("map") != "viewable" or win.get("overrideRedirect"):
if win.get("appId") or win.get("map") != "viewable" or win.get("overrideRedirect") or win.get("inputOnly"):
continue
app = window_steam_app(win, flatpaks, proc)
if not app:
continue
try:
done = subprocess.run(["xprop", "-display", display, "-id", str(win["window"]), "-f", "STEAM_GAME", "32c",
"-set", "STEAM_GAME", match.group(1)],
"-set", "STEAM_GAME", app],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=5).returncode == 0
except (OSError, subprocess.TimeoutExpired):
done = False
log("game window %s on %s tagged STEAM_GAME=%s: %s" % (win["window"], display, match.group(1), "ok" if done else "failed"))
log("game window %s on %s tagged STEAM_GAME=%s: %s" % (win["window"], display, app, "ok" if done else "failed"))
tagged = tagged or done
return tagged
@@ -8,7 +8,7 @@ X64 = os.environ.get("BL_STEAM_CLIENT_ARCH") == "x86_64"
CLIENT_HOME = (os.environ.get("BL_X64_HOME") or os.path.join(HOME, ".droiddeck-x64")) if X64 else HOME
STEAM_ROOT = sys.argv[1] if len(sys.argv) > 1 else os.path.join(CLIENT_HOME, ".local/share/Steam")
LIBRARIES = ((STEAM_ROOT, "Steam Games"), ("/mnt/droiddeck-sd/x86-64" if X64 else "/mnt/droiddeck-sd", "SD Games"))
NOT_GAMES = {228980, 1070560, 1391110, 1493710, 1628350, 3127680, 4183110, 4185400, 4427310, 4628740}
NOT_GAMES = {228980, 1070560, 1161040, 1391110, 1493710, 1628350, 1826330, 3127680, 4183110, 4185400, 4427310, 4628740}
APPS_DIR = os.path.join(HOME, ".local/share/applications")
PREFIX = "droiddeck-game-"
NAME = re.compile(r'^\s*"name"\s*"([^"]*)"', re.M)
@@ -589,7 +589,8 @@ def guest_environment(fex, env):
result["FEX_THUNKHOSTLIBS"] = fex["host_thunks"]
result["FEX_THUNKGUESTLIBS"] = fex["guest_thunks"]
result["FEX_THUNKCONFIG"] = os.path.join(CONFIG_DIR, "ThunksConfig.json")
preload = [part for part in split_paths(result.get("LD_PRELOAD")) if os.path.exists(part) and not is_arm(part)]
preload = [part for part in split_paths(result.get("LD_PRELOAD"))
if "/" not in part or (os.path.exists(part) and not is_arm(part))]
library_path = [part for part in (result.get("LD_LIBRARY_PATH") or "").split(":")
if part and "arm64" not in part and "aarch64" not in part]
for name, parts in (("LD_PRELOAD", preload), ("LD_LIBRARY_PATH", library_path)):
@@ -671,15 +672,26 @@ def launch(command, env, mode, decide=None):
return os.execve(fex["bin"], [fex["bin"], program] + command[1:], guest)
def game_environment(env):
def game_environment(env, command):
try:
game_env = runpy.run_path(os.path.join(BIN_DIR, "droiddeck-game-env"))
env = game_env["game_gpu"](env)
app = game_env["game_id"](env) or env.get("SteamAppId") or env.get("SteamGameId")
if not app:
return env
with open(GAME_ENVIRONMENT, encoding="utf-8") as source:
return game_env["apply_config"](env, json.load(source), str(app))
fixes = {}
game = game_env["Game"](resolve(command[0], env), command[1:])
for check in game_env["FIXES"]:
found = check(game)
if found and found[0]:
fixes.update(found[0])
say(found[2])
try:
with open(GAME_ENVIRONMENT, encoding="utf-8") as source:
config = json.load(source)
except FileNotFoundError:
config = {"version": 1}
return game_env["apply_config"](env, config, str(app), fixes)
except FileNotFoundError:
return env
except Exception as error:
@@ -810,7 +822,7 @@ def main(argv):
return 0
if not command:
return 2
return launch(command, game_environment(dict(os.environ)), os.environ.get("DROIDDECK_FEX", "auto"))
return launch(command, game_environment(dict(os.environ), command), os.environ.get("DROIDDECK_FEX", "auto"))
if action == "run":
try:
mode, decide, command = parse_run(args)
@@ -6,6 +6,7 @@ import re
import runpy
import subprocess
import sys
import tempfile
from pathlib import Path
GAME_TU_DEBUG = "DROIDDECK_GAME_TU_DEBUG"
@@ -87,6 +88,12 @@ def dotnet_memory_ordering(game):
return DOTNET_FEX, [], ".NET runtime: FEX with full TSO and without multiblock"
@fix
def unity_memory_ordering(game):
if {"unityplayer.so", "gameassembly.so", "unityplayer.dll"} & game.files:
return {"FEX_TSOENABLED": "1"}, [], "Unity: FEX with TSO"
@fix
def godot_vulkan(game):
if is_godot(game) and not game.has_arg("--rendering-driver"):
@@ -114,6 +121,21 @@ def arkham_city_x87(game):
return {"FEX_X87REDUCEDPRECISION": "0"}, [], "Batman: Arkham City: FEX with full x87 precision"
@fix
def caravan_sandwitch_feature_level(game):
if "caravansandwitch.exe" in game.files or game.stem == "caravansandwitch-win64-shipping":
return {"VKD3D_FEATURE_LEVEL": "12_1"}, [], "Caravan SandWitch: D3D12 feature level 12_1 (12_2 hands it mesh shader pipelines vkd3d-proton cannot build)"
@fix
def cyberpunk_feature_level(game):
if game.stem == "cyberpunk2077" or (
game.stem == "redprelauncher"
and os.path.isfile(os.path.join(os.path.dirname(game.exe), "bin", "x64", "Cyberpunk2077.exe"))
):
return {"VKD3D_FEATURE_LEVEL": "12_1"}, [], "Cyberpunk 2077: D3D12 feature level 12_1"
def skip_red_launcher(command):
"""Cyberpunk 2077's prelauncher opens REDlauncher, a Qt WebEngine window that hands over to the
game half a minute later and then stays up beside it, well over a core busy for as long as the
@@ -127,6 +149,61 @@ def skip_red_launcher(command):
return command
UBISOFT_LAUNCHER = "drive_c/Program Files (x86)/Ubisoft/Ubisoft Game Launcher"
UBISOFT_SETTINGS = "drive_c/users/steamuser/AppData/Local/Ubisoft Game Launcher/settings.yaml"
def overlay_off(text):
lines = text.splitlines()
try:
start = next(i for i, line in enumerate(lines) if re.fullmatch(r"overlay:\s*(#.*)?", line))
except StopIteration:
if lines and not lines[-1].strip():
lines.pop()
return "\n".join(lines + ["overlay:", " enabled: false", " warning_enabled: false"]) + "\n"
end = start + 1
for index in range(start + 1, len(lines)):
if lines[index].strip() and not lines[index].startswith(" "):
break
if lines[index].strip():
end = index + 1
block = lines[start + 1:end]
for key in ("enabled", "warning_enabled"):
entry = " %s: false" % key
found = [i for i, line in enumerate(block) if line.split(":", 1)[0] == " " + key]
if found:
block[found[0]] = entry
else:
block.append(entry)
return "\n".join(lines[:start + 1] + block + lines[end:]) + "\n"
def ubisoft_overlay_off(env):
prefix = Path(env.get("STEAM_COMPAT_DATA_PATH", "/nonexistent")) / "pfx"
if not (prefix / UBISOFT_LAUNCHER).is_dir():
return
settings = prefix / UBISOFT_SETTINGS
try:
text = settings.read_text(encoding="utf-8") if settings.exists() else ""
updated = overlay_off(text)
if updated == text:
return
settings.parent.mkdir(parents=True, exist_ok=True)
handle, staged = tempfile.mkstemp(prefix=".settings-", dir=settings.parent)
try:
with os.fdopen(handle, "w", encoding="utf-8") as target:
target.write(updated)
if settings.exists():
os.chmod(staged, settings.stat().st_mode & 0o7777)
os.replace(staged, settings)
except BaseException:
os.unlink(staged)
raise
print("droiddeck-game-env: Ubisoft Connect overlay turned off", file=sys.stderr)
except (OSError, UnicodeDecodeError) as error:
print("droiddeck-game-env: Ubisoft Connect overlay left as is (%s)" % error, file=sys.stderr)
def offline_without_anticheat(command, env):
"""Start the configured game executable when the player opts into launching without EAC."""
if (env.get("DROIDDECK_ANTICHEAT_OFFLINE") != "1" or len(command) <= 2
@@ -443,6 +520,7 @@ def main():
fixes = {}
if waiting:
command = skip_red_launcher(command)
ubisoft_overlay_off(env)
fixes, extra, notes = engine_fixes(command[2], command[3:]) if len(command) > 2 else ({}, [], [])
for note in notes:
print("droiddeck-game-env: " + note, file=sys.stderr)
@@ -24,7 +24,7 @@ read_scripts() { xargs -0 -r awk -v want="${1:-redists}" '
proc[depth] = 1
program = tolower(part[4])
sub(/.*[\\\/]/, "", program)
if (program ~ /vulkan/) vulkan[depth] = 1
if (program ~ /vulkan|uplay|ubisoft/) vulkan[depth] = 1
next
}
if (n == 3) pending = part[2]
@@ -243,7 +243,7 @@ for system in "${regs[@]}"; do
had=()
for line in "${kept[@]}"; do had[$line]=1; done
printf '%s\n' "${other[@]}" "${kept[@]}" >"$seeded"
echo "== took Vulkan runtime markers out of $reg"
echo "== took Vulkan runtime and Ubisoft Connect markers out of $reg"
fi
if [ ${#had[@]} -gt 0 ] && { [ -z "$id" ] || [ "$id" != "$prev" ]; }; then
declare -A held=()
@@ -86,12 +86,6 @@ run_gamescope() (
collect_steam_logs() {
[ -n "${BL_DEBUG_DIR:-}" ] || return 0
local steam_root=${XDG_DATA_HOME:-$HOME/.local/share}/Steam
[ "${steam_x64:-0}" = 1 ] && steam_root=${BL_X64_HOME:-$HOME/.droiddeck-x64}/.local/share/Steam
mkdir -p "$BL_DEBUG_DIR/steam" 2>/dev/null || return 0
[ -d "$steam_root/logs" ] && find "$steam_root/logs" -maxdepth 1 -type f -size -8192k \
-exec cp {} "$BL_DEBUG_DIR/steam/" \; 2>/dev/null
[ "${steam_x64:-0}" = 0 ] && [ -f "$HOME/.steam/steam/logs/steamwebhelper.log" ] && cp "$HOME/.steam/steam/logs/steamwebhelper.log" "$BL_DEBUG_DIR/steam/" 2>/dev/null
local sync_store=$HOME/.local/share/droiddeck-esync sync_file
if [ -d "$sync_store" ] && mkdir -p "$BL_DEBUG_DIR/droiddeck-esync" 2>/dev/null; then
for sync_file in launches.log tools.tsv wanted.tsv prefixes.json; do
@@ -102,7 +96,6 @@ collect_steam_logs() {
if [ -d "$fex_state" ] && mkdir -p "$BL_DEBUG_DIR/droiddeck-fex" 2>/dev/null; then
cp "$fex_state/status.json" "$fex_state/config/Config.json" "$BL_DEBUG_DIR/droiddeck-fex/" 2>/dev/null
fi
echo "== steam logs copied to $BL_DEBUG_DIR/steam"
}
trap 'collect_steam_logs 2>/dev/null || true' TERM INT HUP
if [ -n "${BL_VK_DRIVER:-}" ]; then
@@ -227,7 +220,9 @@ if [ -z "${BL_INSIDE:-}" ]; then
echo "== gamescope: games at ${nested}x${height} stretched to the ${width}x${height} output"
fi
fi
if [ "$mode" = steam ]; then
# Off unless Performance asks for it. Compositing the cursor here adds a second
# plane on the phone's compositor; a controller-driven cursor is the reason to turn it on.
if [ "$mode" = steam ] && [ "${BL_CURSOR_COMPOSITE:-0}" = 1 ]; then
export gamescope_cursor_composite=2
fi
args=(--backend wayland --expose-wayland -f -W "$width" -H "$height" -w "$nested" -h "$height")
@@ -401,8 +396,14 @@ case $mode in
steam_root=$XDG_DATA_HOME/Steam
if [ -f "${BL_X64_HOME:-$HOME/.droiddeck-x64}/.local/share/Steam/local.vdf" ]; then
droiddeck-steam-x64 --sync-login || true
droiddeck-steam-x64 --share || true
fi
steam_client=("$steam_root/steamrtarm64/steam")
_bl_srt_shim=$HOME/.droiddeck-steamrt-shim
if droiddeck-steamrt-shim "$steam_root/steamrtarm64" "$_bl_srt_shim"; then
echo "== steam: Valve containerized the arm64 Steam client (it carries a Steam Runtime container); starting it through $_bl_srt_shim"
steam_client=(env STEAM_RUNTIME_STEAMRT="$_bl_srt_shim" PATH="$_bl_srt_shim/bin:$PATH" "${steam_client[@]}")
fi
fi
_bl_proton_root=$steam_root
if [ "$steam_x64" = 1 ]; then
@@ -561,6 +562,11 @@ case $mode in
fi
droiddeck-seed-redists "$steam_root" || true
fi
if [ -n "${BL_SHADER_PRECACHE:-}" ]; then
droiddeck-steam-shaders "$BL_SHADER_PRECACHE" "$XDG_DATA_HOME/Steam" \
"${BL_X64_HOME:-$HOME/.droiddeck-x64}/.local/share/Steam" &&
echo "$BL_SHADER_PRECACHE" > "$HOME/.bl-shader-precache-applied" || true
fi
if [ "$steam_x64" = 0 ] && [ ! -f "$steam_root/steamapps/appmanifest_4427310.acf" ]; then
step "asking the client to fetch the ARM64 Proton"
set -- "$@" steam://install/4427310
@@ -781,10 +787,13 @@ BUSCONF
fi
# The UI bridge launches SteamVR, which needs the guest agent's library environment.
_bl_compat_env=(env -u LD_LIBRARY_PATH -u STEAM_RUNTIME)
_bl_compat_env=(env -u LD_LIBRARY_PATH -u STEAM_RUNTIME -u STEAM_STEAMRT_RECURSION_GUARD)
[ "${LD_LIBRARY_PATH+x}" ] && _bl_compat_env+=("LD_LIBRARY_PATH=$LD_LIBRARY_PATH")
[ "${STEAM_RUNTIME+x}" ] && _bl_compat_env+=("STEAM_RUNTIME=$STEAM_RUNTIME")
[ "${STEAM_STEAMRT_RECURSION_GUARD+x}" ] && _bl_compat_env+=("STEAM_STEAMRT_RECURSION_GUARD=$STEAM_STEAMRT_RECURSION_GUARD")
export STEAM_RUNTIME=1
# A containerized client re-executes itself through pressure-vessel-wrap (bwrap) unless told it is already inside.
export STEAM_STEAMRT_RECURSION_GUARD=1
export LD_LIBRARY_PATH=$steam_root/steamrtarm64:$steam_root/steamrtarm64/panorama${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
export SDL_VIDEO_X11_DGAMOUSE=0
decky_loader=$HOME/homebrew/services/PluginLoader
@@ -834,7 +843,7 @@ BUSCONF
echo "== steam: devtools port $BL_CDP_PORT, guest processes only"
fi
[ -n "${BL_LAUNCH_DIR:-}" ] && mkdir -p "$BL_LAUNCH_DIR/agent" && echo "$BL_CDP_PORT" > "$BL_LAUNCH_DIR/agent/cdp-port"
[ "$steam_x64" = 1 ] || "${_bl_compat_env[@]}" droiddeck-steam-compat "$steam_root" 2>&1 &
[ "$steam_x64" = 1 ] || { "${_bl_compat_env[@]}" droiddeck-steam-compat "$steam_root" 2>&1 & }
_bl_ui=(-gamepadui)
[ "${BL_STEAM_UI:-}" = desktop ] && _bl_ui=()
_bl_cef=(-cef-force-gpu -cef-use-gl=angle -cef-use-angle=vulkan)
@@ -864,6 +873,9 @@ BUSCONF
unset _stale _f
fi
steam_channel=steamdeck_publicbeta
if [ "$steam_x64" = 0 ]; then
steam_channel=$(droiddeck-steam-install --channel 2>/dev/null || echo steamdeck_publicbeta)
fi
_bl_lang=()
[ -n "${BL_STEAM_LANGUAGE:-}" ] && _bl_lang=(-language "$BL_STEAM_LANGUAGE")
steam_beta=$steam_root/package/beta
@@ -196,6 +196,51 @@ TOUR_JS = r"""
return "off";
})()
"""
FILE_BINDING = "droiddeckOpenFileDialog"
FILE_JS = r"""
(() => {
const cancelled = () => ({ result: 2, message: "" });
if (window.__droiddeckFiles) {
window.__droiddeckFiles.cancelAll();
return "present";
}
const self = { next: 0, pending: new Map() };
const open = function (options) {
return new Promise((resolve, reject) => {
if (typeof window.__BINDING__ !== "function") { reject(cancelled()); return; }
const id = ++self.next;
self.pending.set(id, { resolve, reject });
try { window.__BINDING__(JSON.stringify({ id, options: options || {} })); }
catch (e) { self.pending.delete(id); reject(cancelled()); }
});
};
self.done = (id, path) => {
const item = self.pending.get(id);
if (!item) return false;
self.pending.delete(id);
if (typeof path === "string" && path) item.resolve(path); else item.reject(cancelled());
return true;
};
self.cancelAll = () => {
const items = Array.from(self.pending.values());
self.pending.clear();
for (const item of items) item.reject(cancelled());
};
const patch = (w) => {
const system = w?.SteamClient?.System;
if (system && system.OpenFileDialog !== open) system.OpenFileDialog = open;
};
patch(window);
if (SteamClient.System.OpenFileDialog !== open) throw new Error("Steam OpenFileDialog could not be redirected");
const popups = window.g_PopupManager;
if (popups) {
for (const popup of popups.GetPopups?.() || []) patch(popup.window);
popups.AddPopupCreatedCallback?.((popup) => patch(popup.window));
}
window.__droiddeckFiles = self;
return "installed";
})()
""".replace("__BINDING__", FILE_BINDING)
VR_BINDING = "droiddeckLaunchSteamVr"
VR_HELPER = "/usr/local/bin/droiddeck-steamvr"
VR_JS = r"""
@@ -274,6 +319,62 @@ def launch_steamvr():
say("SteamVR library launch failed: %s" % e)
def choose_file(options):
import gi
gi.require_version("Gdk", "3.0")
gi.require_version("Gtk", "3.0")
from gi.repository import Gdk, Gtk
directory = bool(options.get("bChooseDirectory"))
save = bool(options.get("bSaveMode")) and not directory
action = (Gtk.FileChooserAction.SELECT_FOLDER if directory else
Gtk.FileChooserAction.SAVE if save else Gtk.FileChooserAction.OPEN)
dialog = Gtk.FileChooserDialog(title=str(options.get("strTitle") or ""), action=action)
dialog.add_buttons("gtk-cancel", Gtk.ResponseType.CANCEL,
"gtk-save" if save else "gtk-open", Gtk.ResponseType.ACCEPT)
dialog.set_default_response(Gtk.ResponseType.ACCEPT)
dialog.set_local_only(True)
dialog.set_do_overwrite_confirmation(True)
for folder in ("/root/Storage", "/mnt/droiddeck-sd"):
if os.path.isdir(folder):
try:
dialog.add_shortcut_folder(folder)
except Exception:
pass
if not directory:
for entry in options.get("rgFilters") or []:
patterns = [str(p) for p in entry.get("rFilePatterns") or [] if p]
if not patterns:
continue
chosen = Gtk.FileFilter()
chosen.set_name(str(entry.get("strFileTypeName") or ", ".join(patterns)))
for pattern in patterns:
pattern = "*" if pattern == "*.*" else pattern
chosen.add_pattern(pattern)
if pattern.lower() != pattern.upper():
chosen.add_pattern(pattern.upper())
dialog.add_filter(chosen)
if entry.get("bUseAsDefault"):
dialog.set_filter(chosen)
initial = str(options.get("strInitialFile") or "").strip('"')
if initial:
if os.path.isdir(initial):
dialog.set_current_folder(initial)
elif save:
if os.path.isdir(os.path.dirname(initial)):
dialog.set_current_folder(os.path.dirname(initial))
dialog.set_current_name(os.path.basename(initial))
elif os.path.exists(initial):
dialog.set_filename(initial)
elif os.path.isdir(os.path.dirname(initial)):
dialog.set_current_folder(os.path.dirname(initial))
screen = Gdk.Screen.get_default()
if screen is not None:
dialog.set_default_size(screen.get_width(), screen.get_height())
chosen_path = dialog.get_filename() if dialog.run() == Gtk.ResponseType.ACCEPT else None
dialog.destroy()
return chosen_path
class Closed(Exception):
pass
@@ -530,6 +631,65 @@ class Helper:
self.tools_asked = {}
self.tools_checked = None
self.vr_launch = None
self.chooser = None
def open_chooser(self, payload):
try:
request = json.loads(payload)
ident = int(request["id"])
options = request.get("options", {})
if not isinstance(options, dict):
raise ValueError("options are not an object")
except (ValueError, KeyError, TypeError) as e:
say("ignored a file dialog request (%s)" % e)
return
if self.chooser is not None:
self.finish_chooser(ident, None)
return
command = [sys.executable, os.path.realpath(__file__), "--choose-file"]
reaper = os.path.join(self.steam_root, "steamrtarm64", "reaper")
if os.access(reaper, os.X_OK):
command = [reaper, "SteamLaunch", "AppId=769", "--"] + command
try:
proc = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL, start_new_session=True)
proc.stdin.write(json.dumps(options).encode("utf-8"))
proc.stdin.close()
except OSError as e:
say("could not open a file dialog (%s)" % e)
self.finish_chooser(ident, None)
return
say("file dialog open: %s" % (options.get("strTitle") or "untitled"))
self.chooser = (ident, proc)
def chooser_done(self):
ident, proc = self.chooser
self.chooser = None
out = proc.stdout.read().decode("utf-8", "replace")
proc.stdout.close()
proc.wait()
if proc.returncode == 2:
say("file dialog needs GTK 3 and PyGObject, which come with the desktop")
try:
path = json.loads(out) if proc.returncode == 0 else None
except ValueError:
path = None
self.finish_chooser(ident, path if isinstance(path, str) and path else None)
def finish_chooser(self, ident, path):
say("file dialog %s" % ("chose " + path if path else "cancelled"))
self.page.evaluate("window.__droiddeckFiles?.done(%d, %s)" % (ident, json.dumps(path)))
def close_chooser(self):
if self.chooser is not None:
_, proc = self.chooser
self.chooser = None
try:
os.killpg(proc.pid, signal.SIGTERM)
except OSError:
pass
proc.stdout.close()
proc.wait()
def attach(self):
path = shared_context(self.port)
@@ -542,6 +702,8 @@ class Helper:
page.call("Runtime.addBinding", {"name": BINDING})
page.call("Runtime.addBinding", {"name": VR_BINDING})
page.evaluate(VR_JS)
page.call("Runtime.addBinding", {"name": FILE_BINDING})
page.evaluate(FILE_JS)
try:
page.evaluate(TOUR_JS)
except RuntimeError:
@@ -583,9 +745,8 @@ class Helper:
picked = COMPAT["picked_apps"](state)
runnable = {t["name"].lower() for t in live}
apps = sorted(set(installed) | set(picks) | set(prior_auto) | native_choices, key=COMPAT["app_order"])
overrides = COMPAT["requested_games"](
COMPAT["game_requests"](), catalog, [t["name"] for t in live])
apps = sorted(set(apps) | set(overrides), key=COMPAT["app_order"])
asked = [app for app, request in COMPAT["game_requests"]().items() if COMPAT["valid_game_request"](app, request)]
apps = sorted(set(apps) | set(asked), key=COMPAT["app_order"])
current = dict(page.compat("many(%s)" % json.dumps(apps)) or {})
refused = set(apps) - set(current)
refused_native = native_choices & refused
@@ -597,7 +758,6 @@ class Helper:
if isinstance(state.get("auto"), dict):
state["auto"] = prior_auto
native_choices -= refused
overrides = {app: tool for app, tool in overrides.items() if app not in refused}
current["0"] = top["selected"]
native = COMPAT["native_targets"](self.steam_root, apps)
raw_mapping = COMPAT["read_config_mapping"](
@@ -619,6 +779,9 @@ class Helper:
current[app] = ""
elif not current.get(app):
current.pop(app, None)
requests = COMPAT["settled_game_requests"](state, current, catalog)
overrides = COMPAT["requested_games"](requests or {}, catalog, [t["name"] for t in live])
overrides = {app: tool for app, tool in overrides.items() if app not in refused}
changes, auto = COMPAT["plan_mapping"](
current, installed, [t["name"] for t in live], default,
COMPAT["followers"](state, current), native, picks, overrides,
@@ -630,7 +793,8 @@ class Helper:
state.update(
auto={app: tool for app, tool in auto.items() if tool is not COMPAT["NATIVE_CHOICE"]},
native_choices=sorted(refused_native | {app for app, tool in auto.items() if tool is COMPAT["NATIVE_CHOICE"]}),
picked=COMPAT["picked_after"](current, changes), tools=catalog, live=True)
picked=COMPAT["picked_after"](current, changes), tools=catalog, live=True,
games_applied=COMPAT["applied_game_requests"](requests, overrides, state))
state.pop("native", None)
if waiting:
state["pending"] = wanted
@@ -678,6 +842,9 @@ class Helper:
if vr_games and (self.vr_launch is None or self.vr_launch.poll() is not None):
say("preparing SteamVR for library app(s): " + ", ".join(sorted(vr_games)))
self.vr_launch = launch_steamvr()
for e in events:
if called(e, FILE_BINDING) and isinstance(e.get("params", {}).get("payload"), str):
self.open_chooser(e["params"]["payload"])
return events
def serve(self, watcher):
@@ -688,7 +855,11 @@ class Helper:
self.ensure_tools()
while True:
sources = [self.page.ws] + ([watcher] if watcher.fileno() >= 0 else [])
if self.chooser is not None:
sources.append(self.chooser[1].stdout)
ready = [self.page.ws] if self.page.ws.pending() else select.select(sources, [], [], 30 if watcher.fileno() >= 0 else 2)[0]
if self.chooser is not None and self.chooser[1].stdout in ready:
self.chooser_done()
due = False
if self.page.ws in ready:
due = any(called(e, BINDING) for e in self.drain())
@@ -716,6 +887,21 @@ def detach():
COMPAT["write_json"](state_file, state)
def chooser_main():
try:
options = json.load(sys.stdin)
except ValueError:
return 1
try:
path = choose_file(options if isinstance(options, dict) else {})
except (ImportError, ValueError):
return 2
if not path:
return 1
print(json.dumps(path))
return 0
def main():
steam_root = sys.argv[1]
try:
@@ -737,6 +923,7 @@ def main():
last = None
helper.serve(watcher)
except Exception as e:
helper.close_chooser()
if helper.page is not None:
say("lost the client (%s); reattaching" % e)
helper.page.ws.close()
@@ -750,6 +937,8 @@ def main():
if __name__ == "__main__":
if sys.argv[1:2] == ["--choose-file"]:
sys.exit(chooser_main())
signal.signal(signal.SIGTERM, lambda *_: sys.exit(0))
try:
sys.exit(main())
@@ -1,25 +1,57 @@
#!/bin/bash
set -eu
cdn_list="https://client-update.steamstatic.com https://client-update.akamai.steamstatic.com https://client-update.fastly.steamstatic.com"
steam_channel=steamdeck_publicbeta
manifest=steam_client_${steam_channel}_linuxarm64
beta_channel=steamdeck_publicbeta
stable_channel=steamdeck_stable
steam_root=${XDG_DATA_HOME:-$HOME/.local/share}/Steam
channel_state=$steam_root/package/droiddeck-channel
pinned=1
wanted=${BL_STEAM_CHANNEL:-$(cat "$HOME/.config/droiddeck/steam-channel" 2>/dev/null || true)}
case $wanted in
stable|"$stable_channel") steam_channel=$stable_channel ;;
beta|"$beta_channel") steam_channel=$beta_channel ;;
*)
pinned=
steam_channel=$beta_channel
[ "$(cat "$channel_state" 2>/dev/null)" = "$stable_channel" ] && steam_channel=$stable_channel
;;
esac
if [ "${1:-}" = --channel ]; then
echo "$steam_channel"
exit 0
fi
manifest=steam_client_${steam_channel}_linuxarm64
stamp=$steam_root/package/droiddeck-installed
legacy_stamp=$steam_root/package/bannerlator-installed
unconfirmed=$steam_root/package/droiddeck-unconfirmed
repair_request=$HOME/.bl-steam-repair
parts=$steam_root/package/droiddeck-download
if [ -f "$legacy_stamp" ] && [ ! -f "$stamp" ]; then
mv -f "$legacy_stamp" "$stamp" || true
fi
repair=""
fallback=""
starts=0
[ -f "$unconfirmed" ] && starts=$(grep -cx start "$unconfirmed" || true)
if [ -e "$repair_request" ]; then
repair="requested"
elif [ -f "$unconfirmed" ] && ! grep -qx repaired "$unconfirmed" \
&& [ "$(grep -cx start "$unconfirmed")" -ge 2 ]; then
repair="the client was started $(grep -cx start "$unconfirmed") times without showing anything"
elif [ "$starts" -ge 2 ] && ! grep -qx repaired "$unconfirmed"; then
repair="the client was started $starts times without showing anything"
elif [ "$starts" -ge 2 ] && [ -z "$pinned" ] && [ "$steam_channel" = "$beta_channel" ] && [ "${1:-}" != --sync ]; then
repair="the repaired $beta_channel client was started $starts more times without showing anything"
fallback=1
fi
if [ -n "$fallback" ]; then
echo "== STEP $(date -u +%H:%M:%S) falling back to Valve's $stable_channel Steam client: $repair"
steam_channel=$stable_channel
manifest=steam_client_${steam_channel}_linuxarm64
fi
if [ -n "$repair" ]; then
echo "== STEP $(date -u +%H:%M:%S) repairing the Steam client ($repair)"
if [ ! -f "$parts/.repair" ]; then
rm -rf "$parts"
mkdir -p "$parts" && : > "$parts/.repair"
fi
rm -f "$stamp" "$legacy_stamp" "$steam_root/.crash" "$steam_root/steam.pid" \
"$steam_root"/package/steam_client_*.manifest "$steam_root"/package/steam_client_*.installed
for part in "$steam_root"/package/*.zip.*; do
@@ -42,20 +74,99 @@ fi
work=$(mktemp -d)
trap 'rm -rf "$work"' EXIT
cdn=""
for candidate in $cdn_list; do
if curl -fsSL --connect-timeout 10 --max-time 30 --retry 2 --retry-connrefused --retry-delay 3 \
"$candidate/$manifest" -o "$work/manifest"; then
cdn=$candidate
break
nap() {
[ -n "${nap_fd:-}" ] || exec {nap_fd}<> <(:)
read -r -t "$1" -u "$nap_fd" _ 2>/dev/null || :
}
size_of() {
local n=0
[ -f "$1" ] && n=$(wc -c < "$1")
echo $((n))
}
limits=(--connect-timeout 30 --speed-limit 4096 --speed-time 60)
read -r -a delays <<< "${BL_STEAM_RETRY_DELAYS:-5 10 20 30 30}"
step() { echo "== STEP $(date -u +%H:%M:%S) $*"; }
progress() {
local mb=$(( (done_bytes + $(size_of "$1")) / 1000000 ))
[ -z "${3:-}" ] && [ "$mb" = "${shown_mb:-}" ] && [ "$2" = "${shown_part:-}" ] && return 0
shown_mb=$mb shown_part=$2
step "downloading Steam: $2 ($i/$total), $mb of $((total_bytes / 1000000)) MB${3:+ - $3}"
}
fetch() {
local url=$1 dest=$2 size=$3 sum=$4 part=$5 failures=0 bad=0 finished=0 before rc fd wait
while :; do
if [ -f "$dest" ] && { [ "$finished" = 1 ] || { [ "$size" -gt 0 ] && [ "$(size_of "$dest")" -ge "$size" ]; }; }; then
echo "$sum $dest" | sha256sum -c --quiet 2>/dev/null && return 0
rm -f "$dest"
finished=0 bad=$((bad + 1))
[ "$bad" -lt 3 ] || { echo "droiddeck-steam-install: $part does not match its checksum" >&2; return 1; }
fi
before=$(size_of "$dest")
exec {fd}< <(code=$(curl -fsSL "${limits[@]}" -w '%{http_code}' -C - -o "$dest" "$url") && echo "0 $code" || echo "$? $code")
until read -r -t 5 -u "$fd" rc http; do
if [ $? -le 128 ]; then rc=1; break; fi
progress "$dest" "$part"
done
exec {fd}<&-
if [ "$rc" = 0 ]; then finished=1; continue; fi
case "$rc:${http:-}" in
22:416 | 33:*) rm -f "$dest" ;;
22:4??)
echo "droiddeck-steam-install: $part: HTTP $http from ${url%/*}" >&2
return 1
;;
esac
if [ "$(size_of "$dest")" -gt "$before" ]; then failures=0; else failures=$((failures + 1)); fi
if [ "$failures" -gt "${#delays[@]}" ]; then
echo "droiddeck-steam-install: could not download $part (curl exit $rc); what arrived is kept for the next try" >&2
return 1
fi
wait=${delays[$((failures > 0 ? failures - 1 : 0))]}
progress "$dest" "$part" "interrupted (curl exit $rc), trying again in ${wait} s"
nap "$wait"
done
}
fetch_manifest() {
cdn=""
for candidate in $cdn_list; do
if curl -fsSL --connect-timeout 10 --max-time 30 --retry 2 --retry-connrefused --retry-delay 3 \
"$candidate/$manifest" -o "$work/manifest"; then
cdn=$candidate
break
fi
done
[ -n "$cdn" ] || { echo "droiddeck-steam-install: no Steam client CDN answered for $manifest" >&2; exit 1; }
cdn_order=("$cdn")
for candidate in $cdn_list; do
[ "$candidate" = "$cdn" ] || cdn_order+=("$candidate")
done
version=$(awk -F'"' '$2 == "version" { print $4; exit }' "$work/manifest")
awk -F'"' '
/^\t"/ && NF >= 3 && $4 == "" { block = $2 }
$2 == "file" { file[block] = $4 }
$2 == "sha2" { sha[block] = $4 }
$2 == "size" { size[block] = $4 }
END { for (b in file) if (b ~ /_all$/ || b ~ /_linuxarm64_linuxarm64$/) print b, file[b], sha[b], size[b] + 0 }
' "$work/manifest" | sort > "$work/parts"
[ -s "$work/parts" ] && return 0
echo "droiddeck-steam-install: no client components in $manifest; its blocks are:" \
"$(awk -F'"' '/^\t"/ && NF >= 3 && $4 == "" { printf "%s ", $2 }' "$work/manifest")" >&2
return 1
}
if ! fetch_manifest; then
if [ -n "$sync" ] || [ -n "$pinned" ] || [ "$steam_channel" = "$stable_channel" ]; then
exit 1
fi
done
[ -n "$cdn" ] || { echo "droiddeck-steam-install: no Steam client CDN answered for $manifest" >&2; exit 1; }
cdn_order=("$cdn")
for candidate in $cdn_list; do
[ "$candidate" = "$cdn" ] || cdn_order+=("$candidate")
done
version=$(awk -F'"' '$2 == "version" { print $4; exit }' "$work/manifest")
echo "== STEP $(date -u +%H:%M:%S) falling back to Valve's $stable_channel Steam client: $manifest has no arm64 client"
steam_channel=$stable_channel
manifest=steam_client_${steam_channel}_linuxarm64
fallback=1
fetch_manifest || exit 1
fi
if [ -n "$sync" ]; then
installed=$(awk -F'"' '$2 == "version" { print $4; exit }' "$steam_root/package/$manifest.manifest" 2>/dev/null || true)
if [ -x "$steam_root/steamrtarm64/steam" ] && [ "$installed" = "$version" ]; then
@@ -64,31 +175,36 @@ if [ -n "$sync" ]; then
fi
echo "== STEP $(date -u +%H:%M:%S) updating the arm64 Steam client to $version (was ${installed:-none}) for the x86-64 client"
fi
awk -F'"' '
/^\t"/ && NF >= 3 && $4 == "" { block = $2 }
$2 == "file" { file[block] = $4 }
$2 == "sha2" { sha[block] = $4 }
END { for (b in file) if (b ~ /_all$/ || b ~ /_linuxarm64_linuxarm64$/) print b, file[b], sha[b] }
' "$work/manifest" | sort > "$work/parts"
[ -s "$work/parts" ] || { echo "droiddeck-steam-install: no client components in $manifest" >&2; exit 1; }
total=$(grep -c . "$work/parts" 2>/dev/null || echo 0)
total_bytes=$(awk '{ n += $4 } END { print n + 0 }' "$work/parts")
mkdir -p "$steam_root/package"
while read -r block file sum; do
mkdir -p "$parts"
for left in "$parts"/*; do
[ -e "$left" ] || continue
name=${left##*/}
name=${name%.unpacked}
grep -qF " $name " "$work/parts" || rm -rf "$left"
done
done_bytes=0
while read -r block file sum size; do
i=$((${i:-0} + 1))
echo "== STEP $(date -u +%H:%M:%S) downloading Steam: $block ($i/$total)"
if [ -f "$parts/$file.unpacked" ]; then
done_bytes=$((done_bytes + size))
continue
fi
progress "$parts/$file" "$block"
fetched=""
for host in "${cdn_order[@]}"; do
if curl -fsSL --connect-timeout 10 --speed-limit 1024 --speed-time 60 --max-time 1800 \
"$host/$file" -o "$work/$file" && echo "$sum $work/$file" | sha256sum -c --quiet; then
if fetch "$host/$file" "$parts/$file" "$size" "$sum" "$block"; then
fetched=$host
break
fi
rm -f "$work/$file"
done
[ -n "$fetched" ] || { echo "droiddeck-steam-install: $file failed on every CDN" >&2; exit 1; }
unzip -qo "$work/$file" -d "$steam_root"
rm -f "$work/$file"
unzip -qo "$parts/$file" -d "$steam_root"
: > "$parts/$file.unpacked"
rm -f "$parts/$file"
done_bytes=$((done_bytes + size))
done < "$work/parts"
find "$steam_root" -depth -name '*\\*' | while read -r odd; do
fixed=${odd//\\//}
@@ -117,6 +233,11 @@ ln -sfn "$steam_root/linuxarm64" "$HOME/.steam/sdkarm64"
ln -sfn "$steam_root/steamrtarm64" "$HOME/.steam/binarm64"
ln -sfn "$steam_root/linux32" "$HOME/.steam/sdk32"
echo "$version" > "$stamp"
rm -rf "$parts"
if [ -n "$fallback" ]; then
echo "$stable_channel" > "$channel_state"
echo "== STEP $(date -u +%H:%M:%S) the Steam client now follows $stable_channel (beta in ~/.config/droiddeck/steam-channel returns to $beta_channel)"
fi
if [ -n "$repair" ]; then
echo repaired > "$unconfirmed"
rm -f "$repair_request"
@@ -12,6 +12,10 @@ case $target in
esac
if [ "$ui" = desktop ] && [ "${BL_DESKTOP:-}" = 1 ]; then
case ",${MESA_VK_WSI_DEBUG:-}," in
*,sw,*) ;;
*) export MESA_VK_WSI_DEBUG=${MESA_VK_WSI_DEBUG:+$MESA_VK_WSI_DEBUG,}sw ;;
esac
if [ "${BL_STEAM_CLIENT_ARCH:-}" = x86_64 ]; then
client_home=${BL_X64_HOME:-${HOME:-/root}/.droiddeck-x64}
client=(/usr/local/bin/droiddeck-steam-x64 --exec)
@@ -0,0 +1,61 @@
#!/usr/bin/python3
"""Sets the Steam client's shader pre-caching (Settings > Downloads), before the client starts.
droiddeck-steam-shaders <0|1> <steam root>...
The client stores the toggle as InstallConfigStore/Software/Valve/Steam/ShaderCacheManager/
DisableShaderCache in config/config.vdf: "1" when off, "0" when on. It keeps the setting in memory
and writes config.vdf back on exit, so this runs only while the client is stopped. The launcher
passes a value only after its own toggle changes; otherwise the client's toggle stays the user's."""
from pathlib import Path
import runpy
import sys
VDF = runpy.run_path(str(Path(__file__).with_name("droiddeck-steam-ui-scale")))
PATH = ("Software", "Valve", "Steam", "ShaderCacheManager", "DisableShaderCache")
def set_text(text, enabled):
value = "0" if enabled else "1"
updated, changed = VDF["seed_value"](text, PATH, value)
if changed:
return updated, True
block = VDF["parse"](text).get("InstallConfigStore")
for section in PATH[:-1]:
block = block.get(section)
current = block.get(PATH[-1])
if isinstance(current, VDF["Block"]):
raise ValueError("invalid DisableShaderCache setting")
if current == value:
return text, False
at = block.offset(PATH[-1])
end = VDF["TOKEN"].match(text, at).end()
return text[:at] + f'"{value}"' + text[end:], True
def apply(steam_root, enabled):
config = Path(steam_root) / "config/config.vdf"
text = config.read_text() if config.exists() else '"InstallConfigStore"\n{\n}\n'
updated, changed = set_text(text, enabled)
if changed:
VDF["atomic_write"](config, updated)
return changed
if __name__ == "__main__":
if len(sys.argv) < 3 or sys.argv[1] not in ("0", "1"):
print("usage: droiddeck-steam-shaders <0|1> <steam root>...", file=sys.stderr)
sys.exit(2)
enabled = sys.argv[1] == "1"
status = 0
for root in sys.argv[2:]:
if not Path(root).is_dir():
continue
try:
if apply(root, enabled):
print(f"== steam: shader pre-caching {'on' if enabled else 'off'} in {root}")
except (OSError, ValueError, RecursionError) as error:
print(f"== steam: left shader pre-caching unchanged in {root}: {error}", file=sys.stderr)
status = 1
sys.exit(status)
+273
View File
@@ -0,0 +1,273 @@
#!/usr/bin/python3
import json
import os
import re
import runpy
import sys
NOT_GAMES = frozenset((
"228980", "1070560", "1391110", "1628350", "1493710", "3127680",
"4183110", "4185400", "4427310", "4628740",
))
TOOL_DIR = re.compile(r"(steam ?linux ?runtime.*|proton.*|steamworks shared|fex|steamvr)", re.I)
AUTOMATIC = ("", "droiddeck-direct", "droiddeck-fex", "droiddeck-proton-arm64", "droiddeck-proton-11-arm64")
LINUX_TOOLS = ("droiddeck-direct", "droiddeck-fex")
STATE = ".droiddeck-shared.json"
LABELS = {"arm64": "ARM64 client games", "x86-64": "x86-64 client games"}
def say(message):
print("droiddeck-steam-share: " + message)
def read(path):
try:
with open(path, encoding="utf-8", errors="surrogateescape") as f:
return f.read()
except OSError:
return None
def write(path, text):
staged = path + ".droiddeck-new"
with open(staged, "w", encoding="utf-8", errors="surrogateescape") as f:
f.write(text)
os.replace(staged, path)
def field(text, key):
m = re.search(r'"%s"\s+"([^"]*)"' % re.escape(key), text or "", re.I)
return m.group(1) if m else None
def number(text, key):
value = field(text, key)
return int(value) if value and value.isdigit() else 0
def same(a, b):
return os.path.realpath(a) == os.path.realpath(b)
def libraries(root, skip):
found = [root] + [p.replace("\\\\", "\\") for p in
re.findall(r'"path"\s+"((?:[^"\\]|\\.)*)"', read(os.path.join(root, "steamapps", "libraryfolders.vdf")) or "")]
return [p for p in dict.fromkeys(found) if not any(same(p, s) for s in skip)]
def installs(libs):
games = {}
for library in libs:
steamapps = os.path.join(library, "steamapps")
try:
names = sorted(os.listdir(steamapps))
except OSError:
continue
for name in names:
m = re.fullmatch(r"appmanifest_(\d+)\.acf", name)
if not m or m.group(1) in NOT_GAMES or m.group(1) in games:
continue
text = read(os.path.join(steamapps, name))
folder = field(text, "installdir")
if (not folder or folder in (".", "..") or "/" in folder or TOOL_DIR.fullmatch(folder)
or not number(text, "StateFlags") & 4
or not os.path.isdir(os.path.join(steamapps, "common", folder))):
continue
games[m.group(1)] = (steamapps, text, folder)
return games
def link(path, target):
if os.path.islink(path):
if os.readlink(path) == target:
return True
os.unlink(path)
elif os.path.lexists(path):
return False
os.symlink(target, path)
return True
def unlink(path):
if os.path.islink(path):
os.unlink(path)
def emptied(src, folder):
path = os.path.join(src, "common", folder)
return not os.path.isdir(path) or not os.listdir(path)
def sync(view, source, own):
steamapps = os.path.join(view, "steamapps")
for sub in ("common", "compatdata"):
os.makedirs(os.path.join(steamapps, sub), exist_ok=True)
state = {}
try:
state = json.loads(read(os.path.join(view, STATE)) or "{}")
except ValueError:
pass
managed = {app: tuple(v) for app, v in state.get("shared", {}).items() if isinstance(v, list) and len(v) == 2}
hidden = state.get("hidden", {})
shared, back = {}, []
for app, (src, folder) in managed.items():
if app in source and app not in own:
continue
manifest = os.path.join(steamapps, "appmanifest_%s.acf" % app)
if app not in own and not os.path.isfile(manifest) and emptied(src, folder) and os.path.isfile(os.path.join(src, "appmanifest_%s.acf" % app)):
os.unlink(os.path.join(src, "appmanifest_%s.acf" % app))
unlink(os.path.join(steamapps, "common", folder))
unlink(os.path.join(steamapps, "compatdata", app))
if os.path.isfile(manifest):
os.unlink(manifest)
for app, (src, text, folder) in sorted(source.items(), key=lambda item: int(item[0])):
if app in own:
continue
stamp = number(text, "LastUpdated")
if hidden.get(app) == stamp:
continue
hidden.pop(app, None)
manifest = os.path.join(steamapps, "appmanifest_%s.acf" % app)
mine = read(manifest)
if app in managed and mine is None:
unlink(os.path.join(steamapps, "common", folder))
unlink(os.path.join(steamapps, "compatdata", app))
if emptied(src, folder):
os.unlink(os.path.join(src, "appmanifest_%s.acf" % app))
else:
hidden[app] = stamp
continue
if not link(os.path.join(steamapps, "common", folder), os.path.join(src, "common", folder)):
continue
if os.path.isdir(os.path.join(src, "compatdata", app)):
link(os.path.join(steamapps, "compatdata", app), os.path.join(src, "compatdata", app))
if mine is not None and number(mine, "LastUpdated") > stamp:
write(os.path.join(src, "appmanifest_%s.acf" % app), mine)
back.append(app)
elif mine != text:
write(manifest, text)
shared[app] = [src, folder]
state = {"shared": shared, "hidden": hidden}
write(os.path.join(view, STATE), json.dumps(state, indent=1, sort_keys=True) + "\n")
return shared, back
def tools(root):
names = set()
folder = os.path.join(root, "compatibilitytools.d")
try:
entries = os.listdir(folder)
except OSError:
return names
for entry in entries:
text = read(os.path.join(folder, entry, "compatibilitytool.vdf")) or ""
m = re.search(r'"compat_tools"\s*\{\s*"([^"]+)"', text)
if m:
names.add(m.group(1))
return names
def mapping_block(text):
m = re.search(r'"CompatToolMapping"\s*\{', text)
if not m:
return None
depth, k = 1, m.end()
while depth and k < len(text):
depth += {"{": 1, "}": -1}.get(text[k], 0)
k += 1
return m.end(), k - 1
def entries(text):
span = mapping_block(text)
found = {}
if not span:
return found
inner = text[span[0]:span[1]]
for m in re.finditer(r'"(\d+)"\s*\{([^{}]*)\}', inner):
name = re.search(r'"name"\s*"([^"]*)"', m.group(2))
found.setdefault(m.group(1), name.group(1) if name else "")
return found
def set_tools(config, wanted):
text = read(config)
if text is None or not wanted:
return []
span = mapping_block(text)
if not span:
return []
inner = text[span[0]:span[1]]
changed = []
for app, tool in sorted(wanted.items(), key=lambda item: int(item[0])):
m = re.search(r'"%s"\s*\{([^{}]*)\}' % app, inner)
if m:
name = re.search(r'"name"\s*"([^"]*)"', m.group(1))
name = name.group(1) if name else ""
if name == tool or not (canonical(name) in map(canonical, AUTOMATIC) or name.startswith("proton_")):
continue
if name and (name in LINUX_TOOLS) == (tool in LINUX_TOOLS):
continue
inner = inner[:m.start()] + inner[m.end():]
inner = ('\n\t\t\t\t\t"%s"\n\t\t\t\t\t{\n\t\t\t\t\t\t"name"\t\t"%s"\n\t\t\t\t\t\t"config"\t\t""\n'
'\t\t\t\t\t\t"priority"\t\t"250"\n\t\t\t\t\t}' % (app, tool)) + inner
changed.append(app)
if changed:
write(config, text[:span[0]] + inner + text[span[1]:])
return changed
def canonical(name):
return re.sub(r"arm64|aarch64", "a64", name)
def translate(name, here, available):
if name == "droiddeck-fex" and here == "x86-64":
return "droiddeck-direct"
if name == "droiddeck-direct" and here == "arm64":
return "droiddeck-fex"
return {canonical(a): a for a in available}.get(canonical(name))
def library_helper():
sibling = os.path.join(os.path.dirname(os.path.realpath(__file__)), "droiddeck-steam-library")
return runpy.run_path(sibling if os.path.isfile(sibling) else "/usr/local/bin/droiddeck-steam-library")
def main(argv):
if len(argv) != 5 or argv[0] not in LABELS:
say("usage: droiddeck-steam-share arm64|x86-64 ARM_STEAM X64_STEAM X64_CARD SHARED_DIR")
return 2
here, arm, x64, x64_card, shared_dir = argv
views = {name: os.path.join(shared_dir, name) for name in LABELS}
roots = {"arm64": arm, "x86-64": x64}
other = "x86-64" if here == "arm64" else "arm64"
skip = list(views.values()) + [x64, x64_card]
arm_libs = libraries(arm, skip)
x64_libs = [x64] + [p for p in libraries(x64, list(views.values()) + [arm]) if not same(p, x64)]
games = {"arm64": installs(arm_libs), "x86-64": installs(x64_libs)}
view = views[other]
shared, back = sync(view, games[other], games[here])
helper = library_helper()
if not any(re.fullmatch(r"appmanifest_\d+\.acf", n) for n in os.listdir(os.path.join(view, "steamapps"))):
helper["forget"](roots[here], view)
return 0
helper["register"](roots[here], view, LABELS[other])
helper["relabel"](roots[here], view, LABELS[other])
picks = entries(read(os.path.join(roots[other], "config", "config.vdf")) or "")
available = tools(roots[here]) | {"droiddeck-direct" if here == "x86-64" else "droiddeck-fex"}
wanted = {}
for app in shared:
tool = translate(picks.get(app) or picks.get("0", ""), here, available)
if tool:
wanted[app] = tool
changed = set_tools(os.path.join(roots[here], "config", "config.vdf"), wanted)
say("%d game(s) from the %s client%s%s" % (
len(shared), other,
"; tools set for %s" % " ".join(changed) if changed else "",
"; manifests sent back for %s" % " ".join(back) if back else ""))
return 0
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))
@@ -18,13 +18,17 @@ TAG = "droiddeck-app"
def account_dir(steam_root):
"""The most recent account's userdata folder, else the only one that is not 0."""
users = os.path.join(steam_root, "config", "loginusers.vdf")
recent = None
recent, best = None, None
try:
with open(users, encoding="utf-8", errors="replace") as f:
text = f.read()
for block in re.finditer(r'"(\d{5,})"\s*\{([^}]*)\}', text):
if re.search(r'"MostRecent"\s*"1"', block.group(2)):
recent = int(block.group(1))
stamp = re.search(r'"timestamp"\s*"(\d+)"', block.group(2))
rank = (bool(re.search(r'"MostRecent"\s*"1"', block.group(2))),
bool(re.search(r'"AutoLogin"\s*"1"', block.group(2))),
int(stamp.group(1)) if stamp else 0)
if any(rank) and (best is None or rank >= best):
recent, best = int(block.group(1)), rank
except OSError:
pass
userdata = os.path.join(steam_root, "userdata")
@@ -26,9 +26,10 @@ TOKEN = re.compile(r'\s+|//[^\n]*|"(?:\\.|[^"\\])*"|[{}]')
class Block:
def __init__(self, pairs, end):
def __init__(self, pairs, end, offsets=()):
self.pairs = pairs
self.end = end
self.offsets = offsets
def get(self, name):
matches = [value for key, value in self.pairs if key.lower() == name.lower()]
@@ -36,6 +37,9 @@ class Block:
raise ValueError("duplicate UI setting")
return matches[0] if matches else None
def offset(self, name):
return next(at for (key, _), at in zip(self.pairs, self.offsets) if key.lower() == name.lower())
def parse(text):
tokens = []
@@ -51,17 +55,17 @@ def parse(text):
raise ValueError("invalid config.vdf")
def block(index, nested=False):
pairs = []
pairs, offsets = [], []
while index < len(tokens):
token, offset = tokens[index]
if token == '}':
if not nested:
raise ValueError("unexpected closing brace")
return Block(pairs, offset), index + 1
return Block(pairs, offset, offsets), index + 1
if not token.startswith('"') or index + 1 == len(tokens):
raise ValueError("missing config value")
key = token[1:-1]
value = tokens[index + 1][0]
value, at = tokens[index + 1]
index += 2
if value == '{':
value, index = block(index, True)
@@ -70,9 +74,10 @@ def parse(text):
else:
raise ValueError("invalid config value")
pairs.append((key, value))
offsets.append(at)
if nested:
raise ValueError("unclosed config block")
return Block(pairs, len(text)), index
return Block(pairs, len(text), offsets), index
return block(0)[0]
@@ -87,7 +87,7 @@ x64_map_direct() {
[ -f "$STEAM_X64/config/config.vdf" ] || return 0
if [ "$mode" = write ] && pgrep -f 'steamrt64/steam( |$)' >/dev/null 2>&1; then return 0; fi
python3 - "$mode" "$STEAM_X64/config/config.vdf" "$STEAM_X64/appcache/appinfo.vdf" "$X64_HOME/direct-auto.json" \
"$X64_HOME/direct-sniper" "$STEAM_X64/steamapps:/mnt/droiddeck-sd/x86-64/steamapps" "$(x64_default_tool)" \
"$X64_HOME/direct-sniper" "$STEAM_X64/steamapps:/mnt/droiddeck-sd/x86-64/steamapps:$X64_HOME/shared/arm64/steamapps" "$(x64_default_tool)" \
"$REAL_HOME/.bl-compat-pending" ${BL_X64_DIRECT_APPS:-550} \
<<'PY' || { [ "$mode" = live ] || step "could not map titles to DroidDeck direct"; }
# Steam does not swap an installed build when the tool changes: a title installed for Windows stays on Proton.
@@ -520,39 +520,8 @@ ln -sfn "$STEAM_X64/ubuntu12_32" "$X64_HOME/.steam/bin32"
ln -sfn "$STEAM_X64/ubuntu12_64" "$X64_HOME/.steam/bin64"
ln -sfn "$STEAM_X64/linux32" "$X64_HOME/.steam/sdk32"
ln -sfn "$STEAM_X64/linux64" "$X64_HOME/.steam/sdk64"
# The client's bwrap probe (steam-runtime-check-requirements) needs user namespaces, which no Android app has.
srt_real=$STEAM_X64/steamrt64/pv-runtime/steam-runtime-steamrt
srt_shim=$X64_HOME/steamrt-shim
if [ -d "$srt_real" ]; then
rm -rf "$srt_shim"
mkdir -p "$srt_shim/bin"
for e in "$srt_real"/* "$srt_real"/.[!.]*; do
[ -e "$e" ] || continue
[ "$(basename "$e")" = bin ] || ln -s "$e" "$srt_shim/$(basename "$e")"
done
for e in "$srt_real"/bin/*; do ln -s "$e" "$srt_shim/bin/$(basename "$e")"; done
rm -f "$srt_shim/bin/steam-runtime-check-requirements"
printf '#!/bin/sh
exit 0
' > "$srt_shim/bin/steam-runtime-check-requirements"
chmod 755 "$srt_shim/bin/steam-runtime-check-requirements"
rm -f "$srt_shim/bin/steam-runtime-launch-client"
cat > "$srt_shim/bin/steam-runtime-launch-client" <<'LAUNCH'
#!/bin/sh
while [ $# -gt 0 ]; do
case $1 in
--) shift; break ;;
--directory=*) cd "${1#--directory=}" || exit 127 ;;
--env=*) export "${1#--env=}" ;;
--unset-env=*) unset "${1#--unset-env=}" ;;
esac
shift
done
[ $# -gt 0 ] || exit 0
exec "$@"
LAUNCH
chmod 755 "$srt_shim/bin/steam-runtime-launch-client"
fi
# Valve's Steam Runtime container needs user namespaces and a session-bus launcher; steamrt-shim stands in for both.
droiddeck-steamrt-shim "$STEAM_X64/steamrt64" "$X64_HOME/steamrt-shim" || rm -rf "$X64_HOME/steamrt-shim"
# Valve's platform libraries ship without soname links (pressure-vessel makes them); ldconfig adds them here.
srt_libs=$X64_HOME/steamrt-libs
rm -rf "$srt_libs"
@@ -641,6 +610,7 @@ RUN
printf '"InstallConfigStore"\n{\n\t"Software"\n\t{\n\t\t"Valve"\n\t\t{\n\t\t\t"Steam"\n\t\t\t{\n\t\t\t\t"CompatToolMapping"\n\t\t\t\t{\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n' \
> "$STEAM_X64/config/config.vdf"
fi
x64_share x86-64
x64_map_direct
# Each wrapper execs droiddeck-x64-arm-proton through an arm64 binary, which FEX hands straight to the CPU.
arm_tools=$ARM_STEAM/compatibilitytools.d
@@ -690,9 +660,9 @@ VDF
# Each client keeps its own libraries; written only while no x86 client runs, as it rewrites these on exit.
if ! pgrep -f 'steamrt64/steam( |$)' >/dev/null 2>&1; then
listing_ok=0
if python3 - "$STEAM_X64" "${BL_X64_STEAM_LIBRARIES:-}" "$STEAM_X64/config/libraryfolders.vdf" "$STEAM_X64/steamapps/libraryfolders.vdf" <<'PY'; then
if BL_X64_SHARED=$X64_HOME/shared/arm64 python3 - "$STEAM_X64" "${BL_X64_STEAM_LIBRARIES:-}" "$STEAM_X64/config/libraryfolders.vdf" "$STEAM_X64/steamapps/libraryfolders.vdf" <<'PY'; then
import json, os, re, sys
keep = {os.path.realpath(sys.argv[1])}
keep = {os.path.realpath(sys.argv[1]), os.path.realpath(os.environ["BL_X64_SHARED"])}
# Without the session's list nothing is known about the card: leave the folders as they are.
if not sys.argv[2]:
sys.exit(3)
@@ -942,6 +912,14 @@ x64_exec() {
exec "${BL_FEX:-/usr/local/bin/FEX}" "$STEAM_X64/steamrt64/steam" "$@"
}
x64_share() {
pgrep -f 'steamrt64/steam( |$)' >/dev/null 2>&1 && return 0
pgrep -f 'steamrtarm64/steam( |$)' >/dev/null 2>&1 && return 0
[ -d "$STEAM_X64/steamapps" ] && [ -d "$ARM_STEAM/steamapps" ] || return 0
droiddeck-steam-share "$1" "$ARM_STEAM" "$STEAM_X64" /mnt/droiddeck-sd/x86-64 "$X64_HOME/shared" ||
step "could not share the games between the clients"
}
# Steam renews its token every few days and refuses old copies: copy the newest every start, never while a client runs.
x64_sync_login() {
pgrep -f 'steamrt64/steam( |$)' >/dev/null 2>&1 && return 0
@@ -1058,6 +1036,7 @@ PY
case "${1:-}" in
--sync-login) x64_sync_login; exit 0 ;;
--share) x64_share arm64; exit 0 ;;
--prepare) x64_prepare; exit 0 ;;
--exec) shift; x64_exec "$@" ;;
--gpu) x64_gpu; exit $? ;;
+58
View File
@@ -0,0 +1,58 @@
#!/bin/bash
# droiddeck-steamrt-shim <client runtime dir> <shim dir>
# Builds <shim dir> from the Steam Runtime container the client carries and exits 0, or removes it and exits 1
# when the client carries none.
set -eu
client=$1
shim=$2
srt_real=$client/pv-runtime/steam-runtime-steamrt
if [ ! -d "$srt_real" ]; then
probe=$(find "$client" -maxdepth 4 -name steam-runtime-check-requirements -path '*/bin/*' -print -quit 2>/dev/null || true)
srt_real=${probe:+${probe%/bin/*}}
fi
if [ -z "$srt_real" ] || [ ! -d "$srt_real" ]; then
rm -rf "$shim"
exit 1
fi
rm -rf "$shim"
trap 'rm -rf "$shim"' ERR
mkdir -p "$shim/bin"
for e in "$srt_real"/* "$srt_real"/.[!.]*; do
[ -e "$e" ] || continue
[ "$(basename "$e")" = bin ] || ln -s "$e" "$shim/$(basename "$e")"
done
for e in "$srt_real"/bin/*; do
[ -e "$e" ] || continue
ln -s "$e" "$shim/bin/$(basename "$e")"
done
# The client's bwrap probe (steam-runtime-check-requirements) needs user namespaces, which no Android app has.
rm -f "$shim/bin/steam-runtime-check-requirements"
printf '#!/bin/sh
exit 0
' > "$shim/bin/steam-runtime-check-requirements"
chmod 755 "$shim/bin/steam-runtime-check-requirements"
# Valve's launcher asks a service on the session bus; proot is already the container, so the command runs here.
rm -f "$shim/bin/steam-runtime-launch-client"
cat > "$shim/bin/steam-runtime-launch-client" <<'LAUNCH'
#!/bin/sh
while [ $# -gt 0 ]; do
case $1 in
--) shift; break ;;
--directory=*) cd "${1#--directory=}" || exit 127 ;;
--env=*) export "${1#--env=}" ;;
--unset-env=*) unset "${1#--unset-env=}" ;;
--directory|--env|--unset-env)
[ $# -ge 2 ] || exit 127
case $1 in
--directory) cd "$2" || exit 127 ;;
--env) export "$2" ;;
--unset-env) unset "$2" ;;
esac
shift ;;
esac
shift
done
[ $# -gt 0 ] || exit 0
exec "$@"
LAUNCH
chmod 755 "$shim/bin/steam-runtime-launch-client"
@@ -15,7 +15,18 @@ import threading
BIN = Path(__file__).resolve().parent
RUNTIME = Path('/usr/local/lib/droiddeck-xbox')
DLLS = ('xgameruntime.dll', 'winhttp.dll', 'windows.web.dll', 'twinapi.appcore.dll', 'libnghttp2.dll')
DLLS = ('xgameruntime.dll', 'winhttp.dll', 'windows.web.dll', 'twinapi.appcore.dll', 'wintypes.dll', 'libnghttp2.dll')
CLASSES = {
'twinapi.appcore.dll': ('Windows.ApplicationModel.DataTransfer.DataTransferManager',
'Windows.Security.ExchangeActiveSyncProvisioning.EasClientDeviceInformation',
'Windows.System.Profile.AnalyticsInfo', 'Windows.System.UserProfile.AdvertisingManager',
'Windows.UI.ViewManagement.ApplicationView'),
'windows.web.dll': ('Windows.Data.Json.JsonArray', 'Windows.Data.Json.JsonObject', 'Windows.Data.Json.JsonValue'),
'wintypes.dll': ('Windows.Foundation.PropertyValue', 'Windows.Foundation.Collections.PropertySet',
'Windows.Foundation.Metadata.ApiInformation', 'Windows.Storage.Streams.Buffer',
'Windows.Storage.Streams.DataWriter'),
}
CLASS_KEY = 'Software\\Microsoft\\WindowsRuntime\\ActivatableClassId\\'
def bridge(upstream):
@@ -102,6 +113,30 @@ def bridge(upstream):
return server.getsockname()[1], key.decode('ascii'), close
def register_classes(proton, prefix, env):
try:
registry = (prefix / 'system.reg').read_text(encoding='utf-8', errors='replace')
except OSError:
return
missing = [(name, dll) for dll, names in CLASSES.items() for name in names
if '[%s]' % (CLASS_KEY + name).replace('\\', '\\\\') not in registry]
if not missing:
return
text = 'Windows Registry Editor Version 5.00\r\n\r\n' + ''.join(
'[HKEY_LOCAL_MACHINE\\%s%s]\r\n"DllPath"="C:\\\\windows\\\\system32\\\\%s"\r\n\r\n' % (CLASS_KEY, name, dll)
for name, dll in missing)
with tempfile.TemporaryDirectory(prefix='droiddeck-xbox-') as temporary:
source = Path(temporary) / 'classes.reg'
source.write_bytes(b'\xff\xfe' + text.encode('utf-16-le'))
try:
failed = subprocess.run([proton, 'run', 'reg.exe', 'import', 'Z:' + str(source).replace('/', '\\')],
stdout=sys.stderr, env=env, timeout=90).returncode != 0
except subprocess.TimeoutExpired:
failed = True
if failed:
print('droiddeck-xbox: Windows Runtime class registration failed; launching without it', file=sys.stderr)
def wine(argv, source_env=None):
source_env = os.environ if source_env is None else source_env
with open(source_env['DROIDDECK_XBOX_COMMAND_FILE'], encoding='utf-8') as file:
@@ -109,7 +144,8 @@ def wine(argv, source_env=None):
if len(command) < 3 or command[1] != 'waitforexitandrun' or len(argv) != 1:
raise ValueError('Invalid Xbox Proton invocation')
env = dict(source_env)
env.pop('DROIDDECK_XBOX_CREDENTIAL_SOCKET', None)
for name in ('DROIDDECK_XBOX_CREDENTIAL_SOCKET', 'DROIDDECK_XBOX_CREDENTIAL_PORT', 'DROIDDECK_XBOX_CREDENTIAL_KEY'):
env.pop(name, None)
env.pop('DROIDDECK_XBOX_COMMAND_FILE', None)
prefix = Path(source_env['STEAM_COMPAT_DATA_PATH']) / 'pfx'
prefix.parent.mkdir(parents=True, exist_ok=True)
@@ -124,6 +160,7 @@ def wine(argv, source_env=None):
if target.is_symlink():
target.unlink()
shutil.copyfile(RUNTIME / 'windows' / name, target)
register_classes(command[0], prefix, env)
overrides = ';'.join(name[:-4] + '=n' for name in DLLS if name != 'libnghttp2.dll')
env['WINEDLLOVERRIDES'] = overrides + ';' + env.get('WINEDLLOVERRIDES', '')
return subprocess.call([*command[:2], argv[0], *command[3:]], env=env)
@@ -171,7 +208,8 @@ def launch(command, env):
owned_env = dict(env, DROIDDECK_XBOX_ACTIVE='1', DROIDDECK_XBOX_COMMAND_FILE=str(context),
XODUS_SOCKET=reply['socket'], XODUS_BRIDGE_PORT=str(port), XODUS_BRIDGE_KEY=key,
XODUS_LOG='off', RUST_LOG='off', XODUS_SAVE_SYNC='auto' if sidecar.get('cloud', True) else 'off')
owned_env.pop('DROIDDECK_XBOX_CREDENTIAL_SOCKET', None)
for name in ('DROIDDECK_XBOX_CREDENTIAL_SOCKET', 'DROIDDECK_XBOX_CREDENTIAL_PORT', 'DROIDDECK_XBOX_CREDENTIAL_KEY'):
owned_env.pop(name, None)
if reply.get('playfab'):
owned_env['XODUS_XSTS_PLAYFAB'] = reply['playfab']
content = folder / '.xbox-content'
@@ -1113,10 +1113,89 @@ def requested_tool(request, catalog):
def game_requests(directory=None):
value = read_json(state_path("games.json", directory))
if value.get("version") != 1 or not isinstance(value.get("games"), dict):
return {}
return value["games"]
return read_game_requests(directory)[0]
def read_game_requests(directory=None):
try:
with open(state_path("games.json", directory), encoding="utf-8") as f:
text = f.read()
value = json.loads(text)
except (OSError, ValueError):
return {}, None
if not isinstance(value, dict) or value.get("version") != 1 or not isinstance(value.get("games"), dict):
return {}, text
return value["games"], text
def write_game_requests(requests, original, directory=None):
path = state_path("games.json", directory)
try:
with open(path, encoding="utf-8") as f:
if f.read() != original:
return False
except OSError:
if original is not None:
return False
return write_json(path, {"version": 1, "games": requests})
def valid_game_request(app, request):
return (isinstance(app, str) and re.fullmatch(r"[1-9][0-9]*", app) is not None and int(app) <= 0xFFFFFFFF and
(request is None or isinstance(request, dict) and isinstance(request.get("dir"), str) and
request["dir"] != "" and isinstance(request.get("valve"), bool)))
def settle_game_requests(requests, state, current, catalog, missing_is_cleared=False):
recorded = state.get("games_applied")
if isinstance(recorded, dict):
applied = dict(recorded)
else:
applied = dict(requests) if "tools" in state else {}
by_name = {t["name"]: t for t in catalog}
out = dict(requests)
for app, request in requests.items():
if not valid_game_request(app, request) or app not in applied or applied[app] != request:
continue
if request is None:
del out[app]
continue
wanted = requested_tool(request, catalog)
if wanted is None:
continue
if app in current:
picked = current[app]
elif missing_is_cleared:
picked = ""
else:
continue
if picked == wanted:
continue
tool = by_name.get(picked)
if tool and tool["dir"]:
out[app] = {"valve": tool["valve"], "dir": tool["dir"]}
else:
del out[app]
return out, out != requests
def settled_game_requests(state, current, catalog, missing_is_cleared=False, directory=None):
for _ in range(2):
requests, text = read_game_requests(directory)
settled, moved = settle_game_requests(requests, state, current, catalog, missing_is_cleared)
if not moved or write_game_requests(settled, text, directory):
return settled
return None
def applied_game_requests(requests, overrides, state):
recorded = state.get("games_applied")
before = recorded if isinstance(recorded, dict) else {}
if requests is None:
return dict(before)
return {app: request if app in overrides and overrides[app] is not DEFERRED_GAME_TOOL else before[app]
for app, request in requests.items()
if app in overrides and overrides[app] is not DEFERRED_GAME_TOOL or app in before}
def requested_games(requests, catalog, runnable=None):
@@ -1306,7 +1385,8 @@ def register_default(config_path, apps=(), protect=(), default=None, catalog=Non
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)))
overrides = requested_games(game_requests(directory), catalog)
requests = settled_game_requests(state, current, catalog, not steam_running(), directory)
overrides = requested_games(requests or {}, catalog)
managed = state.get("auto", {}).keys() if isinstance(state.get("auto"), dict) else ()
native_choices = state.get("native_choices", ())
native = native_targets(steam_root, set(apps) | set(picks) | set(managed) | set(native_choices))
@@ -1325,6 +1405,7 @@ def register_default(config_path, apps=(), protect=(), default=None, catalog=Non
state["auto"] = {app: tool for app, tool in auto.items() if tool is not NATIVE_CHOICE}
state["native_choices"] = sorted(app for app, tool in auto.items() if tool is NATIVE_CHOICE)
state["picked"] = picked_after(current, changes)
state["games_applied"] = applied_game_requests(requests, overrides, state)
state.pop("native", None)
state["tools"] = catalog
state.setdefault("default", default)
+279 -74
View File
@@ -17,6 +17,7 @@
#include <sys/syscall.h>
#include <sys/timerfd.h>
#include <time.h>
#include <ucontext.h>
#include <unistd.h>
struct ns_sem_args {
@@ -68,12 +69,13 @@ struct ns_wait_args {
#define NS_PROCS 256u
#define NS_FDS 65536
#define NS_DEVS 8
#define NS_VERSION 1u
#define NS_VERSION 2u
#define NS_HEAD_MAGIC 0x31434e5953544e44ull
#define NS_DEV_MAGIC 0x4e54445600000000ull
#define NS_OBJ_MAGIC 0x27ull
#define NS_WAITERS 0x80000000u
#define NS_SWEEP_NS 2000000000ull
#define NS_SPIN_NS 4000ull
#define NS_DEAD 0xffffffffu
#ifndef CLOSE_RANGE_CLOEXEC
@@ -144,6 +146,7 @@ struct ns_q {
uint32_t waiting;
uint32_t inuse;
uint32_t next_free;
uint32_t sleeping;
struct ns_ent ent[NS_ENTS];
};
@@ -168,6 +171,7 @@ struct ns_tls {
struct ns_dev *d;
uint32_t q;
int waiting;
int sleep;
};
static struct ns_dev ns_devs[NS_DEVS];
@@ -255,19 +259,148 @@ static void ns_unlock(uint32_t *w) {
}
}
static void ns_block(sigset_t *old) {
sigset_t s;
sigfillset(&s);
sigdelset(&s, SIGSEGV);
sigdelset(&s, SIGBUS);
sigdelset(&s, SIGILL);
sigdelset(&s, SIGFPE);
sigdelset(&s, SIGTRAP);
pthread_sigmask(SIG_BLOCK, &s, old);
#define NS_DEFER 4
#define NS_LEVELS 8
struct ns_cs {
int depth;
int count;
int level;
unsigned held_levels;
int sigs[NS_DEFER];
siginfo_t info[NS_DEFER];
sigset_t held[NS_LEVELS];
};
static __thread struct ns_cs ns_cs;
static uintptr_t ns_handlers[_NSIG];
static uint8_t ns_siginfo[_NSIG];
int bl_from_ntdll(const void *caller) __attribute__((visibility("hidden")));
static int ns_deferrable(int sig) {
return sig > 0 && sig < _NSIG && sig != SIGKILL && sig != SIGSTOP && sig != SIGSEGV && sig != SIGBUS && sig != SIGILL
&& sig != SIGFPE && sig != SIGTRAP && sig != SIGSYS;
}
static void ns_restore(const sigset_t *old) {
pthread_sigmask(SIG_SETMASK, old, NULL);
static int ns_defer(int sig, const siginfo_t *si) {
struct ns_cs *cs = &ns_cs;
if (sig < SIGRTMIN)
for (int i = 0; i < NS_DEFER; i++)
if (__atomic_load_n(&cs->sigs[i], __ATOMIC_RELAXED) == sig) return 1;
int i = __atomic_fetch_add(&cs->count, 1, __ATOMIC_RELAXED);
if (i >= NS_DEFER) {
__atomic_fetch_sub(&cs->count, 1, __ATOMIC_RELAXED);
return 0;
}
if (si) memcpy(&cs->info[i], si, sizeof(*si));
else memset(&cs->info[i], 0, sizeof(*si));
__atomic_signal_fence(__ATOMIC_SEQ_CST);
__atomic_store_n(&cs->sigs[i], sig, __ATOMIC_RELAXED);
return 1;
}
static int ns_park(int sig, const siginfo_t *si, ucontext_t *uc) {
int level = ns_cs.level;
if (level < 0 || level >= NS_LEVELS) return 0;
sigset_t one;
sigemptyset(&one);
sigaddset(&one, sig);
int saved = errno;
pthread_sigmask(SIG_BLOCK, &one, NULL);
pid_t pid = getpid(), tid = (pid_t)ns_self();
int ok = (si && syscall(SYS_rt_tgsigqueueinfo, pid, tid, sig, si) == 0) || syscall(SYS_tgkill, pid, tid, sig) == 0;
if (!ok) pthread_sigmask(SIG_UNBLOCK, &one, NULL);
errno = saved;
if (!ok) return 0;
sigaddset(&uc->uc_sigmask, sig);
unsigned long *words = (unsigned long *)&ns_cs.held[level];
unsigned bits = 8 * sizeof(unsigned long);
__atomic_fetch_or(&words[(unsigned)(sig - 1) / bits], 1ul << ((unsigned)(sig - 1) % bits), __ATOMIC_RELAXED);
__atomic_fetch_or(&ns_cs.held_levels, 1u << level, __ATOMIC_RELAXED);
return 1;
}
static void ns_trampoline(int sig, siginfo_t *si, void *uc) {
if (__atomic_load_n(&ns_cs.depth, __ATOMIC_RELAXED) > 0 && (ns_defer(sig, si) || (uc && ns_park(sig, si, uc)))) return;
uintptr_t h = __atomic_load_n(&ns_handlers[sig], __ATOMIC_ACQUIRE);
if (!h) return;
ns_cs.level++;
__atomic_signal_fence(__ATOMIC_SEQ_CST);
if (__atomic_load_n(&ns_siginfo[sig], __ATOMIC_RELAXED)) ((void (*)(int, siginfo_t *, void *))h)(sig, si, uc);
else ((void (*)(int))h)(sig);
__atomic_signal_fence(__ATOMIC_SEQ_CST);
ns_cs.level--;
}
static void ns_flush(void) {
struct ns_cs *cs = &ns_cs;
sigset_t all, mask;
sigfillset(&all);
int saved = errno;
pthread_sigmask(SIG_BLOCK, &all, &mask);
int n = cs->count < NS_DEFER ? cs->count : NS_DEFER;
pid_t pid = getpid(), tid = (pid_t)ns_self();
for (int i = 0; i < n; i++)
if (cs->sigs[i] > 0 && syscall(SYS_rt_tgsigqueueinfo, pid, tid, cs->sigs[i], &cs->info[i]) != 0) syscall(SYS_tgkill, pid, tid, cs->sigs[i]);
memset(cs->sigs, 0, sizeof(cs->sigs));
cs->count = 0;
int level = cs->level;
if (level >= 0 && level < NS_LEVELS && (cs->held_levels & (1u << level))) {
for (int sig = 1; sig < _NSIG; sig++)
if (sigismember(&cs->held[level], sig) == 1) sigdelset(&mask, sig);
sigemptyset(&cs->held[level]);
cs->held_levels &= ~(1u << level);
}
pthread_sigmask(SIG_SETMASK, &mask, NULL);
errno = saved;
}
static void ns_block(void) {
ns_cs.depth++;
__atomic_signal_fence(__ATOMIC_SEQ_CST);
}
static void ns_restore(void) {
__atomic_signal_fence(__ATOMIC_SEQ_CST);
if (--ns_cs.depth == 0) {
__atomic_signal_fence(__ATOMIC_SEQ_CST);
if (ns_cs.count || (ns_cs.held_levels && ns_cs.level >= 0 && ns_cs.level < NS_LEVELS && (ns_cs.held_levels & (1u << ns_cs.level))))
ns_flush();
}
}
int sigaction(int sig, const struct sigaction *act, struct sigaction *old) {
static int (*real)(int, const struct sigaction *, struct sigaction *);
if (!real) real = (int (*)(int, const struct sigaction *, struct sigaction *))dlsym(RTLD_NEXT, "sigaction");
if (!real) {
errno = ENOSYS;
return -1;
}
if (!ns_deferrable(sig) || !ns_on()) return real(sig, act, old);
uintptr_t prev = __atomic_load_n(&ns_handlers[sig], __ATOMIC_ACQUIRE);
uint8_t prev_info = __atomic_load_n(&ns_siginfo[sig], __ATOMIC_RELAXED);
struct sigaction mine;
const struct sigaction *use = act;
if (act && act->sa_handler != SIG_DFL && act->sa_handler != SIG_IGN && !(act->sa_flags & SA_RESETHAND) &&
bl_from_ntdll(__builtin_return_address(0))) {
mine = *act;
mine.sa_sigaction = ns_trampoline;
mine.sa_flags |= SA_SIGINFO;
__atomic_store_n(&ns_siginfo[sig], (act->sa_flags & SA_SIGINFO) ? 1 : 0, __ATOMIC_RELAXED);
__atomic_store_n(&ns_handlers[sig], (uintptr_t)act->sa_sigaction, __ATOMIC_RELEASE);
use = &mine;
}
int r = real(sig, use, old);
if (r != 0 && use == &mine) {
__atomic_store_n(&ns_siginfo[sig], prev_info, __ATOMIC_RELAXED);
__atomic_store_n(&ns_handlers[sig], prev, __ATOMIC_RELEASE);
}
if (r == 0 && old && old->sa_sigaction == ns_trampoline) {
old->sa_sigaction = (void (*)(int, siginfo_t *, void *))prev;
if (!prev_info) old->sa_flags &= ~SA_SIGINFO;
}
return r;
}
static inline struct ns_obj *ns_obj(struct ns_head *h, uint32_t s) {
@@ -378,10 +511,11 @@ static int ns_claim(struct ns_q *q, int32_t index) {
if (dead) return 0;
}
int32_t want = -1;
return __atomic_compare_exchange_n(&q->signaled, &want, index, 0, __ATOMIC_ACQ_REL, __ATOMIC_RELAXED);
return __atomic_compare_exchange_n(&q->signaled, &want, index, 0, __ATOMIC_SEQ_CST, __ATOMIC_RELAXED);
}
static void ns_wake(struct ns_q *q) {
if (!__atomic_load_n(&q->sleeping, __ATOMIC_SEQ_CST)) return;
int saved = errno;
ns_futex(&q->signaled, FUTEX_WAKE, 1, NULL, 0);
errno = saved;
@@ -618,14 +752,13 @@ static void ns_dev_put_locked(struct ns_dev *d) {
static void ns_dev_put(struct ns_dev *d) {
if (__atomic_sub_fetch(&d->refs, 1, __ATOMIC_ACQ_REL)) return;
sigset_t old;
int saved = errno;
ns_block(&old);
ns_block();
pthread_mutex_lock(&ns_mx);
uint32_t zero = 0;
if (d->h && __atomic_compare_exchange_n(&d->refs, &zero, NS_DEAD, 0, __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE)) ns_release(d);
pthread_mutex_unlock(&ns_mx);
ns_restore(&old);
ns_restore();
errno = saved;
}
@@ -682,20 +815,18 @@ static void ns_forget_locked(int fd) {
}
static void ns_forget(int fd) {
sigset_t old;
int saved = errno;
ns_block(&old);
ns_block();
pthread_mutex_lock(&ns_mx);
ns_forget_locked(fd);
pthread_mutex_unlock(&ns_mx);
ns_restore(&old);
ns_restore();
errno = saved;
}
static uint64_t ns_adopt(struct ns_dev *d, uint32_t tag, int fd, uint32_t gen, uint32_t s) {
uint64_t e = 0;
sigset_t old;
ns_block(&old);
ns_block();
pthread_mutex_lock(&ns_mx);
if (!d->h || d->tag != tag || !ns_table() || ns_attach(d) < 0 || s >= NS_SLOTS) goto out;
if ((e = __atomic_load_n(&ns_fdt[fd], __ATOMIC_ACQUIRE))) goto out;
@@ -710,7 +841,7 @@ static uint64_t ns_adopt(struct ns_dev *d, uint32_t tag, int fd, uint32_t gen, u
ns_unlock(&o->lock);
out:
pthread_mutex_unlock(&ns_mx);
ns_restore(&old);
ns_restore();
return e;
}
@@ -719,8 +850,7 @@ static uint64_t ns_note_device(int fd) {
uint64_t e = 0;
if (syscall(SYS_fstat, fd, &st) != 0 || !S_ISREG(st.st_mode)) return 0;
if (((uint64_t)st.st_size & ~0xffffull) != NS_DEV_MAGIC) return 0;
sigset_t old;
ns_block(&old);
ns_block();
pthread_mutex_lock(&ns_mx);
struct ns_dev *d = ns_table() ? ns_map(fd, (uint32_t)st.st_size & 0xffff, (uint64_t)st.st_ino) : NULL;
if (d) {
@@ -732,7 +862,7 @@ static uint64_t ns_note_device(int fd) {
}
}
pthread_mutex_unlock(&ns_mx);
ns_restore(&old);
ns_restore();
return e;
}
@@ -773,15 +903,14 @@ static int ns_ready(struct ns_dev *d) {
static void ns_thread_gone(void *p) {
struct ns_tls *t = p;
if (!t || !t->d || !t->q) return;
sigset_t old;
if (!t || !t->d) return;
struct ns_dev *d = t->d;
ns_block(&old);
ns_free_q(d->h, t->q - 1);
ns_block();
if (t->q) ns_free_q(d->h, t->q - 1);
t->q = 0;
t->waiting = 0;
t->d = NULL;
ns_restore(&old);
ns_restore();
ns_dev_put(d);
}
@@ -789,24 +918,28 @@ static void ns_key_init(void) {
pthread_key_create(&ns_key, ns_thread_gone);
}
static struct ns_q *ns_thread_q(struct ns_dev *d, uint32_t *qi) {
struct ns_head *h = d->h;
if (ns_tq.d && ns_tq.q && ns_tq.waiting) {
ns_drop_q(ns_tq.d->h, ns_tq.q - 1);
ns_tq.waiting = 0;
}
if (ns_tq.d == d && ns_tq.q) {
*qi = ns_tq.q - 1;
return ns_q(h, *qi);
}
if (ns_tq.d && ns_tq.q) {
struct ns_dev *old = ns_tq.d;
ns_free_q(old->h, ns_tq.q - 1);
static int ns_hold(struct ns_dev *d) {
if (ns_tq.d == d) return 1;
if (!ns_dev_get(d)) return 0;
if (ns_tq.sleep) return 2;
struct ns_dev *old = ns_tq.d;
if (old) {
ns_block();
if (ns_tq.q) ns_free_q(old->h, ns_tq.q - 1);
ns_tq.q = 0;
ns_tq.waiting = 0;
ns_tq.d = NULL;
ns_restore();
ns_dev_put(old);
}
pthread_once(&ns_once, ns_key_init);
ns_tq.d = d;
pthread_setspecific(ns_key, &ns_tq);
return 1;
}
static struct ns_q *ns_new_q(struct ns_dev *d, uint32_t *qi) {
struct ns_head *h = d->h;
uint32_t i;
if (!ns_pop(h, &h->q_free, 0, &i) && !ns_grow(&h->q_hwm, NS_QS, &i)) {
ns_sweep_dead(h);
@@ -815,22 +948,35 @@ static struct ns_q *ns_thread_q(struct ns_dev *d, uint32_t *qi) {
struct ns_q *q = ns_q(h, i);
q->proc = (uint32_t)d->proc;
q->waiting = 0;
q->sleeping = 0;
q->inuse = 1;
__atomic_add_fetch(&d->refs, 1, __ATOMIC_ACQ_REL);
ns_tq.d = d;
ns_tq.q = i + 1;
ns_tq.waiting = 0;
pthread_setspecific(ns_key, &ns_tq);
*qi = i;
return q;
}
static struct ns_q *ns_thread_q(struct ns_dev *d, uint32_t *qi) {
struct ns_head *h = d->h;
if (ns_tq.d != d) return NULL;
if (ns_tq.q && ns_tq.waiting) {
ns_drop_q(h, ns_tq.q - 1);
ns_tq.waiting = 0;
}
if (ns_tq.q) {
*qi = ns_tq.q - 1;
return ns_q(h, *qi);
}
struct ns_q *q = ns_new_q(d, qi);
if (!q) return NULL;
ns_tq.q = *qi + 1;
ns_tq.waiting = 0;
return q;
}
static int ns_create(struct ns_dev *d, int type, uint32_t a, uint32_t b) {
struct ns_head *h = d->h;
sigset_t old;
int fd = -1, err = 0;
uint32_t s = 0;
ns_block(&old);
ns_block();
pthread_mutex_lock(&ns_mx);
if (!ns_table() || ns_attach(d) < 0) {
err = ENOMEM;
@@ -879,7 +1025,7 @@ static int ns_create(struct ns_dev *d, int type, uint32_t a, uint32_t b) {
ns_unlock(&o->lock);
out:
pthread_mutex_unlock(&ns_mx);
ns_restore(&old);
ns_restore();
if (fd < 0) errno = err;
return fd;
}
@@ -890,6 +1036,20 @@ static int ns_expired(const struct ns_wait_args *args) {
return (uint64_t)now.tv_sec * 1000000000ull + (uint64_t)now.tv_nsec >= args->timeout;
}
static uint64_t ns_now(void) {
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
return (uint64_t)now.tv_sec * 1000000000ull + (uint64_t)now.tv_nsec;
}
static void ns_spin(struct ns_q *q) {
uint64_t end = ns_now() + NS_SPIN_NS;
for (unsigned i = 1; __atomic_load_n(&q->signaled, __ATOMIC_ACQUIRE) == -1; i++) {
ns_relax();
if (!(i & 63) && ns_now() >= end) return;
}
}
static int ns_sleep(struct ns_q *q, const struct ns_wait_args *args) {
struct timespec ts, *tsp = NULL;
int op = FUTEX_WAIT_BITSET | ((args->flags & NS_WAIT_REALTIME) ? FUTEX_CLOCK_REALTIME : 0);
@@ -898,14 +1058,45 @@ static int ns_sleep(struct ns_q *q, const struct ns_wait_args *args) {
ts.tv_nsec = (long)(args->timeout % 1000000000ull);
tsp = &ts;
}
while (__atomic_load_n(&q->signaled, __ATOMIC_ACQUIRE) == -1) {
if (tsp && ns_expired(args)) return ETIMEDOUT;
if (ns_futex(&q->signaled, op, (uint32_t)-1, tsp, FUTEX_BITSET_MATCH_ANY) == -1) {
if (errno == ETIMEDOUT) return ETIMEDOUT;
if (errno == EINTR) return EINTR;
}
ns_spin(q);
int ret = 0;
__atomic_store_n(&q->sleeping, 1, __ATOMIC_SEQ_CST);
while (!ret && __atomic_load_n(&q->signaled, __ATOMIC_SEQ_CST) == -1) {
if (tsp && ns_expired(args)) ret = ETIMEDOUT;
else if (ns_futex(&q->signaled, op, (uint32_t)-1, tsp, FUTEX_BITSET_MATCH_ANY) == -1 && (errno == ETIMEDOUT || errno == EINTR))
ret = errno;
}
return 0;
__atomic_store_n(&q->sleeping, 0, __ATOMIC_RELAXED);
return ret;
}
static int ns_try_any(struct ns_head *h, const uint32_t *slots, const uint32_t *gens, uint32_t total, uint32_t owner, int *dead) {
for (uint32_t i = 0; i < total; i++) {
struct ns_obj *o = ns_obj(h, slots[i]);
ns_lock(&o->lock);
int busy = o->type == NS_FREE || o->gen != gens[i] || o->any_head || o->all_hint;
if (!busy && ns_signaled(o, owner)) {
switch (o->type) {
case NS_SEM:
o->a--;
break;
case NS_MUTEX:
*dead = (int)o->ownerdead;
o->ownerdead = 0;
o->a++;
o->b = owner;
break;
case NS_EVENT:
if (!o->a) o->b = 0;
break;
}
ns_unlock(&o->lock);
return (int)i;
}
ns_unlock(&o->lock);
if (busy) return -1;
}
return -2;
}
static int ns_wait(struct ns_dev *d, struct ns_wait_args *ua, int all) {
@@ -927,12 +1118,24 @@ static int ns_wait(struct ns_dev *d, struct ns_wait_args *ua, int all) {
for (uint32_t j = 0; j < i; j++)
if (slots[j] == slots[i]) return EINVAL;
}
sigset_t old;
ns_block(&old);
int nested = ns_tq.sleep > 0;
ns_block();
if (!all && (nested || !ns_tq.waiting)) {
int dead = 0, got = ns_try_any(h, slots, gens, total, args.owner, &dead);
if (got >= 0) {
ns_restore();
ua->index = (uint32_t)got;
return dead ? EOWNERDEAD : 0;
}
if (got == -2 && args.timeout != UINT64_MAX && ns_expired(&args)) {
ns_restore();
return ETIMEDOUT;
}
}
uint32_t qi;
struct ns_q *q = ns_ready(d) ? ns_thread_q(d, &qi) : NULL;
struct ns_q *q = !ns_ready(d) ? NULL : nested ? ns_new_q(d, &qi) : ns_thread_q(d, &qi);
if (!q) {
ns_restore(&old);
ns_restore();
return ENOMEM;
}
ns_maybe_sweep(h);
@@ -952,7 +1155,7 @@ static int ns_wait(struct ns_dev *d, struct ns_wait_args *ua, int all) {
}
q->total = total;
q->waiting = 1;
ns_tq.waiting = 1;
if (!nested) ns_tq.waiting = 1;
int bad = 0;
if (!all) {
for (uint32_t i = 0; i < total && !bad; i++) {
@@ -1004,16 +1207,18 @@ static int ns_wait(struct ns_dev *d, struct ns_wait_args *ua, int all) {
}
int ret = 0;
if (!bad && __atomic_load_n(&q->signaled, __ATOMIC_ACQUIRE) == -1) {
sigset_t held;
ns_restore(&old);
ns_tq.sleep++;
ns_restore();
ret = ns_sleep(q, &args);
ns_block(&held);
ns_block();
ns_tq.sleep--;
}
ns_drop_q(h, qi);
ns_tq.waiting = 0;
if (!nested) ns_tq.waiting = 0;
int32_t got = __atomic_load_n(&q->signaled, __ATOMIC_ACQUIRE);
int dead = (int)q->ownerdead;
ns_restore(&old);
if (nested) ns_free_q(h, qi);
ns_restore();
if (bad) return EINVAL;
if (got != -1) {
ua->index = (uint32_t)got;
@@ -1061,12 +1266,11 @@ static int ns_obj_ioctl(struct ns_dev *d, uint64_t e, unsigned long req, void *a
return ENOTTY;
}
if (type != (uint32_t)want) return EINVAL;
sigset_t old;
ns_block(&old);
ns_block();
int all = ns_lock_obj(h, o);
if (o->type != (uint32_t)want || o->gen != gen) {
ns_unlock_obj(h, o, all);
ns_restore(&old);
ns_restore();
return EINVAL;
}
switch (req) {
@@ -1133,7 +1337,7 @@ static int ns_obj_ioctl(struct ns_dev *d, uint64_t e, unsigned long req, void *a
}
}
ns_unlock_obj(h, o, all);
ns_restore(&old);
ns_restore();
if (ret) return ret;
if (req == NS_SEM_RELEASE || req == NS_EVENT_SET || req == NS_EVENT_RESET || req == NS_EVENT_PULSE)
memcpy(arg, &prev, sizeof(prev));
@@ -1176,9 +1380,10 @@ int bl_ntsync_ioctl(int fd, unsigned long req, void *arg, int *rc) {
uint64_t e = ns_lookup(fd);
if (!e) return 0;
struct ns_dev *d = &ns_devs[(e >> 56) & 7];
if (!ns_dev_get(d)) return 0;
int held = ns_hold(d);
if (!held) return 0;
int out = 0, err = (e & NS_E_DEVICE) ? ns_dev_ioctl(d, req, arg, &out) : ns_obj_ioctl(d, e, req, arg);
ns_dev_put(d);
if (held == 2) ns_dev_put(d);
if (err) {
errno = err;
*rc = -1;
+9 -2
View File
@@ -51,9 +51,16 @@ static void rt_send(void *dpy, uint32_t action) {
}
}
static void *rt_xtst(const char *name) {
void *fn = dlsym(RTLD_NEXT, name);
if (fn) return fn;
void *lib = dlopen("libXtst.so.6", RTLD_LAZY | RTLD_NOLOAD);
return lib ? dlsym(lib, name) : NULL;
}
int XTestFakeMotionEvent(void *dpy, int screen, int x, int y, unsigned long delay) {
static int (*real)(void *, int, int, int, unsigned long);
if (!real) real = (int (*)(void *, int, int, int, unsigned long))dlsym(RTLD_NEXT, "XTestFakeMotionEvent");
if (!real) real = (int (*)(void *, int, int, int, unsigned long))rt_xtst("XTestFakeMotionEvent");
if (rt_enabled()) {
pthread_mutex_lock(&rt_lock);
rt_x = x;
@@ -69,7 +76,7 @@ int XTestFakeMotionEvent(void *dpy, int screen, int x, int y, unsigned long dela
int XTestFakeButtonEvent(void *dpy, unsigned int button, int press, unsigned long delay) {
static int (*real)(void *, unsigned int, int, unsigned long);
if (!real) real = (int (*)(void *, unsigned int, int, unsigned long))dlsym(RTLD_NEXT, "XTestFakeButtonEvent");
if (!real) real = (int (*)(void *, unsigned int, int, unsigned long))rt_xtst("XTestFakeButtonEvent");
if (button == 1) {
pthread_mutex_lock(&rt_lock);
int handled = 0;
+1 -8
View File
@@ -25,14 +25,7 @@ trap 'rm -rf "$work"' EXIT
sha256() { if command -v sha256sum >/dev/null; then sha256sum "$1" | cut -d' ' -f1; else shasum -a 256 "$1" | cut -d' ' -f1; fi; }
fetch() {
local url=$1 expected=$2 dest=$3
curl -fsSL --retry 4 -o "$dest" "$url"
local actual
actual=$(sha256 "$dest")
if [ "$actual" != "$expected" ]; then
echo "checksum mismatch for $url: $actual" >&2
exit 1
fi
bash "$here/fetch-verified.sh" "$@"
}
pack() {
+32
View File
@@ -0,0 +1,32 @@
#!/usr/bin/env bash
set -euo pipefail
url=${1:?URL required}
expected=${2:?SHA-256 required}
destination=${3:?destination required}
[[ "$expected" =~ ^[a-f0-9]{64}$ ]] || { echo 'Invalid download SHA-256' >&2; exit 1; }
cache=${DROIDDECK_PACK_CACHE:-"$HOME/.cache/droiddeck-pack-inputs"}
mkdir -p "$cache" "$(dirname "$destination")"
kept="$cache/$expected"
verify() {
if command -v sha256sum >/dev/null; then
echo "$expected $1" | sha256sum -c - >/dev/null 2>&1
else
echo "$expected $1" | shasum -a 256 -c - >/dev/null 2>&1
fi
}
if [ -f "$kept" ] && verify "$kept"; then
echo "Verified pack input cache hit: $expected"
else
part=$(mktemp "$cache/$expected.part.XXXXXX")
trap 'rm -f "$part"' EXIT
curl -fsSL --retry 4 --retry-all-errors --connect-timeout 15 --max-time 180 --retry-max-time 240 \
-o "$part" "$url"
verify "$part" || { echo "Checksum mismatch for $url" >&2; exit 1; }
if [ -d "$kept" ]; then
rm -rf -- "$kept"
fi
mv -f "$part" "$kept"
echo "Downloaded and verified pack input: $expected"
fi
cp "$kept" "$destination"
verify "$destination" || { echo 'Copied pack input checksum mismatch' >&2; exit 1; }
+20
View File
@@ -0,0 +1,20 @@
#!/usr/bin/env bash
set -euo pipefail
if [ -z "$WEBHOOK" ]; then
echo "::notice::No DISCORD_BUILDS_WEBHOOK secret; nothing posted"
exit 0
fi
commit=$(gh api "repos/$GITHUB_REPOSITORY/commits/$GITHUB_SHA")
subject=$(jq -r '.commit.message | split("\n")[0]' <<<"$commit")
title=$(jq -r '.commit.message | split("\n") | map(select(length > 0)) | .[1] // ""' <<<"$commit")
pr=$(sed -n 's/^Merge pull request #\([0-9]*\).*/\1/p' <<<"$subject")
if [ -n "$pr" ]; then
what="[#$pr]($GITHUB_SERVER_URL/$GITHUB_REPOSITORY/pull/$pr): ${title:-$subject}"
else
what="[\`${GITHUB_SHA::7}\`]($GITHUB_SERVER_URL/$GITHUB_REPOSITORY/commit/$GITHUB_SHA): $subject"
fi
get=$(printf 'Download: [standard APK](<%s>) · [all variants](<%s>)' "$APK" "$RELEASE")
content=$(printf '**New signed Preview build** `main-%s`\n%s\nIn the app: **Updates → Preview**\n%s' "${GITHUB_SHA::7}" "$what" "$get")
jq -n --arg content "$content" '{content: $content, allowed_mentions: {parse: []}}' |
curl -fsS -H 'Content-Type: application/json' -d @- "$WEBHOOK" -o /dev/null
echo "Posted: $content"
+22
View File
@@ -0,0 +1,22 @@
#!/usr/bin/env bash
set -euo pipefail
pr=$(gh api "repos/$GITHUB_REPOSITORY/commits/$GITHUB_SHA/pulls" \
--jq '[.[] | select(.merged_at != null and .base.ref == "main")] | first // empty')
if [ -n "$pr" ]; then
number=$(jq -r .number <<<"$pr")
title=$(jq -r .title <<<"$pr")
merged=$(jq -r '.merged_at[:10]' <<<"$pr")
summary=$(jq -r '.body // ""' <<<"$pr" | tr -d '\r' | { grep -v '^🤖' || true; } |
awk '!p && /^#/ { next } NF { p = 1; print; next } p { exit }' | head -n 6 | cut -c1-400)
{
echo "**Merged to main** - [PR #$number]($GITHUB_SERVER_URL/$GITHUB_REPOSITORY/pull/$number): $title (merged $merged)"
if [ -n "$summary" ]; then
echo
echo "$summary"
fi
} > notes-head.md
else
subject=$(gh api "repos/$GITHUB_REPOSITORY/commits/$GITHUB_SHA" --jq '.commit.message | split("\n")[0]')
echo "**Pushed to main** without a PR - $subject" > notes-head.md
fi
cat notes-head.md
+39
View File
@@ -0,0 +1,39 @@
#!/usr/bin/env bash
set -euo pipefail
tag=main-${GITHUB_SHA::7}
cat signed/SHA256SUMS-*.txt > signed/SHA256SUMS.txt
rm signed/SHA256SUMS-*.txt
notes="$(cat notes-head.md)
Signed build of $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/commit/$GITHUB_SHA - in main, not yet in a DroidDeck release; for testing.
Commit: $GITHUB_SHA
VersionCode: $(sed -n 's/^ *versionCode \([0-9][0-9]*\)$/\1/p' app/build.gradle)
Each apk installs over a DroidDeck release and keeps its data. \`standard\` is the normal app; the others are the same app under another package name, installed beside it.
SHA-256:
\`\`\`
$(cat signed/SHA256SUMS.txt)
\`\`\`"
gh release delete "$tag" -R "$CI_REPO" --yes --cleanup-tag 2>/dev/null || true
gh release create "$tag" -R "$CI_REPO" --target main --prerelease \
--title "main ${GITHUB_SHA::7}" --notes "$notes" signed/*
gh release list -R "$CI_REPO" -L 200 --json tagName,publishedAt \
--jq '[.[] | select(.tagName | startswith("main-"))] | sort_by(.publishedAt) | reverse | .['"$KEEP"':][] | .tagName' |
while IFS= read -r old; do
if [ "$old" = "$tag" ]; then
continue
fi
if ! gh release delete "$old" -R "$CI_REPO" --yes --cleanup-tag 2>/dev/null; then
if gh release view "$old" -R "$CI_REPO" >/dev/null 2>&1; then
echo "::error::Could not delete old release $old"
exit 1
fi
fi
done
published=$(gh release view "$tag" -R "$CI_REPO" --json tagName,assets)
jq -e --arg tag "$tag" '.tagName == $tag and (.assets | length) > 0' <<< "$published" >/dev/null
url="$GITHUB_SERVER_URL/$CI_REPO/releases/tag/$tag"
apk=$(cd signed && ls DroidDeck-*.apk | awk 'length(a) == 0 || length($0) < length(a) { a = $0 } END { print a }')
echo "release=$url" >> "$GITHUB_OUTPUT"
echo "apk=$GITHUB_SERVER_URL/$CI_REPO/releases/download/$tag/$apk" >> "$GITHUB_OUTPUT"
echo "Published $url"
+77
View File
@@ -0,0 +1,77 @@
#!/usr/bin/env bash
set -uo pipefail
MARK="<!-- droiddeck-pr-build -->"
{
echo "$MARK"
echo "## Build of this pull request"
echo
[ -f /tmp/pr-merge.md ] && cat /tmp/pr-merge.md
echo
case "$BUILD_OUTCOME:$UNIT_TEST_OUTCOME:$APK_OUTCOME" in
success:success:success)
echo "### Build passed"
echo
echo "The \`droiddeck-apk\` artifact is [on this run]($RUN_URL), signed with the public test key."
echo
if [ "$SAME_REPO" = true ]; then
echo "After the parallel regression tests pass, a release-signed tester build that installs over Stable or Preview will be published and will appear in DroidDeck's **Test builds** list. Label the PR \`no-test-build\` to skip that."
else
echo "**Maintainer:** add the \`publish-test\` label to publish a release-signed tester build that installs over Stable or Preview and appears in DroidDeck's **Test builds** list. The label is removed automatically after publishing."
fi
echo
echo "Fallback: [open **PR Release Publish**]($GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/workflows/pr-release-publish.yml) on \`main\` and enter PR \`$PR\`."
;;
success:success:*)
echo "### APK publication incomplete"
echo
echo "Compilation and unit tests passed, but APK publication did not complete. See [the run]($RUN_URL)."
;;
success:failure:*)
echo "### ERROR: Unit tests failed"
echo
echo "The APK compiled, but unit tests failed. No installable artifact was published."
echo
if [ -n "$TEST_DIAGNOSTICS_URL" ]; then
echo "The [unit-test-results]($TEST_DIAGNOSTICS_URL) artifact contains the test log and any generated reports."
else
echo "The test diagnostics upload did not complete. See [the test log]($RUN_URL)."
fi
;;
success:*)
echo "### Build incomplete"
echo
echo "The APK compiled, but unit tests did not complete successfully. See [the run]($RUN_URL)."
;;
failure:*)
echo "### ERROR: Build failed"
echo
echo "The compiler's errors ([full log]($RUN_URL)):"
echo
echo '```'
{ grep -E '^e: |^w: .*error|error:|CMake Error|fatal error|ninja: error|FAILED|What went wrong|Execution failed for task|Unresolved reference|Type mismatch|Unresolved' /tmp/build.log 2>/dev/null \
| sed -e 's#file:///home/runner/work/[^/]*/[^/]*/##' -e 's#/home/runner/work/[^/]*/[^/]*/##' \
| grep -v '^w: ' | awk '!seen[$0]++' | head -80; } || true
echo '```'
echo
echo "<details><summary>What went wrong (Gradle)</summary>"
echo
echo '```'
awk '/^\* What went wrong/{p=1} /^\* Try:/{p=0} p{print}' /tmp/build.log 2>/dev/null | head -60
echo '```'
echo "</details>"
;;
skipped:*|cancelled:*)
echo "### Not built"
echo
echo "The build did not run (see above, or [the run]($RUN_URL))."
;;
esac
echo
} > /tmp/pr-report.md
cat /tmp/pr-report.md >> "$GITHUB_STEP_SUMMARY"
ID=$(gh api "repos/${GITHUB_REPOSITORY}/issues/${PR}/comments" --paginate --jq ".[] | select(.user.login == \"github-actions[bot]\" and (.body | startswith(\"$MARK\"))) | .id" | head -1 || true)
if [ -n "$ID" ]; then
gh api -X PATCH "repos/${GITHUB_REPOSITORY}/issues/comments/${ID}" -F body=@/tmp/pr-report.md >/dev/null
else
gh api -X POST "repos/${GITHUB_REPOSITORY}/issues/${PR}/comments" -F body=@/tmp/pr-report.md >/dev/null
fi
+2 -2
View File
@@ -21,9 +21,9 @@ PACKED_FILES = {'lib/arm64-v8a/libproot.so': ('proot', 'libproot.so'),
PACKED_LIBRARIES = ['lib/arm64-v8a/libopencv_java4.so', 'lib/arm64-v8a/libmediapipe_tasks_jni.so']
PACK_NAMES = ['proot', 'directaudio', 'tracking', 'xbox', 'comet']
SOURCE_PATHS = ['app/build.gradle', 'app/src/main/cpp/fakeinput_steam.cpp', 'app/src/main/assets/pulseaudio.tzst',
'tools/linuxfs', 'tools/proot', 'tools/aaudio-sink', 'tools/directaudio',
'tools/linuxfs', 'tools/proot', 'tools/aaudio-sink', 'tools/directaudio', 'tools/packs',
'tools/gamescope/release.env', 'tools/droiddeck-esync/release.env',
'tools/mangoapp', 'tools/msitools', 'tools/pipewire', 'tools/comet', 'tools/xbox', 'tools/build_local.sh',
'tools/mangoapp', 'tools/msitools', 'tools/pipewire', 'tools/comet', 'tools/xbox', 'tools/components', 'tools/ci', 'tools/build_local.sh',
'tools/release/runtime_inputs.py', 'app/src/main/assets/desktop-removal',
'tools/quest', 'tools/vr/src', 'tools/vr/driver_droiddeck', 'tools/vr/layer', 'tools/vr/build.sh',
'app/src/main/cpp/waylandcomp/src/headpose_shm.h',
+95
View File
@@ -0,0 +1,95 @@
import itertools
import json
import os
from pathlib import Path
import shlex
import shutil
import subprocess
import tempfile
import unittest
import yaml
ROOT = Path(__file__).resolve().parents[2]
class CiChecksTest(unittest.TestCase):
def test_pr_cache_writes_and_source_timestamp_policy(self):
jobs = yaml.safe_load((ROOT / '.github/workflows/build.yml').read_text())['jobs']
cache = next(step for step in jobs['build']['steps']
if step.get('uses') == 'gradle/actions/setup-gradle@v4')['with']
self.assertEqual(cache['cache-read-only'],
"${{ github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name != github.repository }}")
gradle = (ROOT / 'app/build.gradle').read_text()
self.assertIn("gitValue(['show', '-s', '--format=%ct', '--end-of-options', sourceCommit])", gradle)
def test_report_comment_lookup_ignores_human_marker(self):
if not shutil.which('jq'):
self.skipTest('jq required to execute comment selection')
script = (ROOT / 'tools/release/report-build.sh').read_text()
line = next(line for line in script.splitlines() if line.startswith('ID='))
arguments = shlex.split(line[len('ID=$('):].split(' | head', 1)[0])
query = arguments[arguments.index('--jq') + 1].replace('$MARK', '<!-- build -->')
comments = [dict(id=1, user=dict(login='someone'), body='<!-- build --> forged'),
dict(id=2, user=dict(login='github-actions[bot]'), body='<!-- build --> result')]
result = subprocess.check_output(['jq', '-r', query], input=json.dumps(comments), text=True)
self.assertEqual(result.strip(), '2')
def test_build_and_tests_use_identical_generated_metadata(self):
jobs = yaml.safe_load((ROOT / '.github/workflows/build.yml').read_text())['jobs']
steps = {step.get('id'): step for step in jobs['build']['steps']}
self.assertEqual(steps['build']['env'], steps['unit-tests']['env'])
self.assertIn('DROIDDECK_BUILD_LABEL', steps['unit-tests']['env'])
self.assertIn('SGDB_API_KEY', steps['unit-tests']['env'])
gradle = (ROOT / 'app/build.gradle').read_text()
self.assertIn("outputs.doNotCacheIf('CI executes the test suite on every run') { true }", gradle)
self.assertIn('outputs.upToDateWhen { false }', gradle)
def test_independent_jobs_wait_only_for_the_pinned_source(self):
jobs = yaml.safe_load((ROOT / '.github/workflows/build.yml').read_text())['jobs']
for name in ('xbox', 'components', 'python-tests'):
self.assertEqual(jobs[name]['needs'], 'source')
self.assertEqual(set(jobs['build']['needs']), {'source', 'components', 'xbox'})
def test_dependency_update_errors_block_installation(self):
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
sudo = root / 'sudo'
sudo.write_text('#!/bin/bash\nprintf "%s\\n" "$*" >> "$APT_LOG"\nexit "${APT_RESULT:-0}"\n')
sudo.chmod(0o755)
query = root / 'dpkg-query'
query.write_text('#!/bin/sh\nexit 1\n')
query.chmod(0o755)
log = root / 'apt.log'
env = dict(os.environ, PATH=str(root) + os.pathsep + os.environ['PATH'], APT_LOG=str(log))
installer = ROOT / 'tools/ci/install-ubuntu-packages.sh'
result = subprocess.run(['bash', str(installer), 'qemu-user'], env=dict(env, APT_RESULT='17'))
self.assertEqual(result.returncode, 17)
self.assertEqual(len(log.read_text().splitlines()), 2)
log.unlink()
subprocess.run(['bash', str(installer), 'qemu-user'], env=env, check=True)
update, install = log.read_text().splitlines()
for command in (update, install):
self.assertIn('Dir::Etc::sourceparts=-', command)
self.assertIn('Acquire::https::Timeout=15', command)
self.assertIn('Acquire::Retries=1', command)
self.assertIn('APT::Update::Error-Mode=any', update)
self.assertIn('install -y -q --no-install-recommends qemu-user', install)
def test_required_status_rejects_failed_cancelled_or_skipped_test_jobs(self):
jobs = yaml.safe_load((ROOT / '.github/workflows/build.yml').read_text())['jobs']
gate = jobs['checks']
self.assertEqual(gate['name'], 'Compile and test APK')
self.assertEqual(set(gate['needs']), {'build', 'python-tests'})
self.assertIn('always()', gate['if'])
for build, python in itertools.product(('success', 'failure', 'cancelled', 'skipped'), repeat=2):
with self.subTest(build=build, python=python):
result = subprocess.run(
['bash', '-c', gate['steps'][0]['run']],
env=dict(os.environ, BUILD_RESULT=build, PYTHON_RESULT=python),
capture_output=True)
self.assertEqual(result.returncode == 0, build == python == 'success')
for publisher in ('sign', 'packs'):
self.assertIn('checks', jobs[publisher]['needs'])
+27
View File
@@ -0,0 +1,27 @@
import os
from pathlib import Path
import subprocess
import tempfile
import unittest
ROOT = Path(__file__).resolve().parents[2]
class CiNdkTest(unittest.TestCase):
def test_installed_ndk_must_match_the_declared_version(self):
version = (ROOT / 'tools/ci/toolchains.env').read_text().strip().split('=', 1)[1]
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
ndk = root / 'ndk' / version
ndk.mkdir(parents=True)
properties = ndk / 'source.properties'
properties.write_text('Pkg.Revision = ' + version + '\n')
output = root / 'environment'
env = dict(os.environ, ANDROID_HOME=str(root), GITHUB_ENV=str(output))
script = ROOT / 'tools/ci/resolve-ndk.sh'
subprocess.run(['bash', str(script)], env=env, check=True)
self.assertIn('NDK_VERSION=' + version, output.read_text())
self.assertIn('ANDROID_NDK_HOME=' + str(ndk), output.read_text())
properties.write_text('Pkg.Revision = unexpected\n')
self.assertNotEqual(subprocess.run(['bash', str(script)], env=env).returncode, 0)
+106
View File
@@ -0,0 +1,106 @@
import importlib.util
import io
import json
import os
from pathlib import Path
import subprocess
import tarfile
import tempfile
import unittest
from unittest.mock import patch
SPEC = importlib.util.spec_from_file_location('component_prepare', Path(__file__).resolve().parents[1] / 'components/prepare.py')
components = importlib.util.module_from_spec(SPEC)
SPEC.loader.exec_module(components)
class ComponentPrepareTest(unittest.TestCase):
def setUp(self):
directory = tempfile.TemporaryDirectory()
self.addCleanup(directory.cleanup)
self.root = Path(directory.name)
self.fixture(self.root)
def fixture(self, root):
for name in components.TREES:
(root / name).mkdir(parents=True)
(root / 'tools/components/recipes').mkdir(parents=True)
(root / 'input.h').write_text('#define VALUE 1\n')
(root / 'tools/components/recipes/fake.sh').write_text('printf binary > app/src/main/assets/linuxfs/demo.so\n')
spec = dict(script='tools/components/recipes/fake.sh', inputs=['input.h', 'tools/components/recipes/fake.sh'],
outputs=['app/src/main/assets/linuxfs/demo.so'])
(root / components.REGISTRY).write_text(json.dumps(dict(fake=spec)))
for name in components.LIBRARIES:
(root / name).parent.mkdir(parents=True, exist_ok=True)
(root / name).write_bytes(b'library')
subprocess.run(['git', 'init', '-q', str(root)], check=True)
subprocess.run(['git', '-C', str(root), 'config', 'core.autocrlf', 'false'], check=True)
subprocess.run(['git', '-C', str(root), 'add', '.'], check=True)
subprocess.run(['git', '-C', str(root), '-c', 'user.name=Test Developer', '-c', 'user.email=test@example.test',
'commit', '-qm', 'baseline'], check=True)
def test_recipe_failure_or_missing_output_does_not_succeed(self):
script = self.root / 'tools/components/recipes/fake.sh'
script.write_text('exit 7\n')
with self.assertRaises(subprocess.CalledProcessError):
components.prepare('fake', self.root)
script.write_text('exit 0\n')
with self.assertRaisesRegex(ValueError, 'required outputs'):
components.prepare('fake', self.root)
def test_identity_covers_source_and_recipe_changes(self):
original = components.identity('fake', self.root)
(self.root / 'input.h').write_text('#define VALUE 2\n')
changed = components.identity('fake', self.root)
self.assertNotEqual(original, changed)
script = self.root / 'tools/components/recipes/fake.sh'
script.write_text(script.read_text() + '\nprintf changed\n')
self.assertNotEqual(changed, components.identity('fake', self.root))
def test_tool_requirements_are_installed_in_one_union(self):
definitions = dict(one=dict(packages=['gcc', 'cmake']), two=dict(packages=['gcc', 'qemu-user']))
with patch.object(components, 'registry', return_value=definitions), patch.object(components.subprocess, 'run') as run:
components.prepare_tools(['one', 'two'], self.root)
run.assert_called_once_with(['bash', 'tools/ci/install-ubuntu-packages.sh', 'cmake', 'gcc', 'qemu-user'],
cwd=self.root, check=True)
def test_prepared_handoff_verifies_every_file_and_source_tree(self):
components.prepare('fake', self.root)
archive = self.root / 'prepared.tar.gz'
components.pack(archive, self.root)
tree = subprocess.check_output(['git', 'rev-parse', 'HEAD^{tree}'], cwd=self.root, text=True).strip()
target = self.root / 'target'
self.fixture(target)
components.unpack(archive, tree, target)
self.assertEqual((target / 'app/src/main/assets/linuxfs/demo.so').read_bytes(), b'binary')
with self.assertRaisesRegex(ValueError, 'pinned source'):
components.unpack(archive, 'wrong-tree', target)
def test_tampered_component_bytes_fail_handoff_verification(self):
components.prepare('fake', self.root)
archive = self.root / 'prepared.tar.gz'
components.pack(archive, self.root)
corrupt = self.root / 'corrupt.tar.gz'
with tarfile.open(archive) as original, tarfile.open(corrupt, 'w:gz') as output:
for entry in original:
content = original.extractfile(entry).read()
if entry.name.endswith('demo.so'):
content = b'tampered'
entry.size = len(content)
output.addfile(entry, io.BytesIO(content))
target = self.root / 'target'
self.fixture(target)
tree = subprocess.check_output(['git', 'rev-parse', 'HEAD^{tree}'], cwd=self.root, text=True).strip()
with self.assertRaisesRegex(ValueError, 'file hashes'):
components.unpack(corrupt, tree, target)
def test_archive_cannot_overwrite_source_through_traversal(self):
archive = self.root / 'bad.tar.gz'
with tarfile.open(archive, mode='w:gz') as tar:
name = 'app/src/main/assets/linuxfs/../../../../../input.h'
entry = tarfile.TarInfo(name)
entry.size = 3
tar.addfile(entry, io.BytesIO(b'bad'))
with self.assertRaisesRegex(ValueError, 'Unsafe'):
components.unpack(archive, 'tree', self.root)
self.assertEqual((self.root / 'input.h').read_text(), '#define VALUE 1\n')
+57
View File
@@ -196,6 +196,63 @@ class AgentTest(unittest.TestCase):
answer = self.ask({"kind": "vr", "action": "status"})
self.assertEqual("UNAVAILABLE", answer["error"]["code"])
def fake_proc(self, pid, parent, comm, env):
root = self.dir / "proc" / str(pid)
root.mkdir(parents=True)
(root / "comm").write_text(comm + "\n")
(root / "stat").write_text("%d (%s) S %d 0 0" % (pid, comm, parent))
(root / "environ").write_bytes(b"\0".join(b"%s=%s" % (k.encode(), v.encode()) for k, v in env.items()) + b"\0")
def tag(self, wins, arch="arm64"):
snap = {"displays": {":0": [], ":1": wins}}
with patch.dict(os.environ, {"DISPLAY": ":0", "BL_STEAM_CLIENT_ARCH": arch}), \
patch.object(agent.subprocess, "run") as run, patch.object(agent, "log"):
run.return_value.returncode = 0
tagged = agent.tag_game_windows(snap, proc=self.dir / "proc")
return tagged, [call.args[0][-1] for call in run.call_args_list]
def test_a_sandboxed_flatpak_window_is_tagged_with_its_shortcut(self):
self.fake_proc(40, 1, "gjs", {"FLATPAK_ID": "com.github.tchx84.Flatseal", "SteamAppId": "2166984148"})
win = {"window": 7, "map": "viewable", "class": "com.github.tchx84.Flatseal", "pid": None}
self.assertEqual(self.tag([win]), (True, ["2166984148"]))
def test_a_flatpak_window_named_after_the_app_is_matched(self):
self.fake_proc(40, 1, "vacuumtube", {"FLATPAK_ID": "rocks.shy.VacuumTube", "SteamAppId": "3000000001"})
win = {"window": 7, "map": "viewable", "class": "VacuumTube", "pid": None}
self.assertEqual(self.tag([win]), (True, ["3000000001"]))
def test_a_window_whose_process_has_no_app_id_falls_back_to_its_flatpak(self):
self.fake_proc(40, 1, "bwrap", {"FLATPAK_ID": "com.github.tchx84.Flatseal", "SteamAppId": "2166984148"})
self.fake_proc(41, 1, "gjs", {})
win = {"window": 7, "map": "viewable", "class": "com.github.tchx84.Flatseal", "pid": 41}
self.assertEqual(self.tag([win]), (True, ["2166984148"]))
def test_a_flatpak_shared_by_two_shortcuts_is_not_guessed(self):
self.fake_proc(40, 1, "gjs", {"FLATPAK_ID": "com.github.tchx84.Flatseal", "SteamAppId": "2166984148"})
self.fake_proc(42, 1, "gjs", {"FLATPAK_ID": "com.github.tchx84.Flatseal", "SteamAppId": "3000000003"})
win = {"window": 7, "map": "viewable", "class": "com.github.tchx84.Flatseal", "pid": None}
self.assertEqual(self.tag([win]), (False, []))
def test_a_window_outside_the_reaper_takes_its_process_app_id(self):
self.fake_proc(30, 1, "proot", {})
self.fake_proc(31, 30, "electron", {"SteamAppId": "3000000002"})
win = {"window": 8, "map": "viewable", "class": "electron", "pid": 31}
self.assertEqual(self.tag([win]), (True, ["3000000002"]))
def test_windows_gamescope_already_resolves_are_left_alone(self):
self.fake_proc(20, 1, "reaper", {})
self.fake_proc(21, 20, "game", {"SteamAppId": "413150"})
wins = [{"window": 9, "map": "viewable", "class": "game", "pid": 21},
{"window": 10, "map": "viewable", "class": "tagged", "pid": None, "appId": 5},
{"window": 11, "map": "viewable", "class": "other", "pid": None},
{"window": 12, "map": "unmapped", "class": "game", "pid": 21},
{"window": 13, "map": "viewable", "class": "steam_app_550", "pid": None}]
self.assertEqual(self.tag(wins), (False, []))
def test_the_x86_client_still_tags_its_game_classes(self):
win = {"window": 13, "map": "viewable", "class": "steam_app_550", "pid": None}
self.assertEqual(self.tag([win], arch="x86_64"), (True, ["550"]))
def test_exec_output_and_status(self):
answer = self.ask({"kind": "exec", "argv": ["sh", "-c", "echo out; echo err >&2; exit 3"], "env": {"X": "1"}})
self.assertTrue(answer["ok"])
+29 -2
View File
@@ -549,11 +549,12 @@ class LaunchTest(FexTestCase):
def test_the_guest_environment(self):
arm = self.write(self.tmp / "arm.so", elf(183))
x86 = self.write(self.tmp / "x86.so", elf(62))
env = {"LD_PRELOAD": "%s:%s %s" % (arm, x86, self.tmp / "gone.so"),
env = {"LD_PRELOAD": "%s:%s %s libMelonLoader.Bootstrap.so" % (arm, x86, self.tmp / "gone.so"),
"LD_LIBRARY_PATH": "/steam/steamrtarm64:/usr/lib/aarch64-linux-gnu:/game/lib:/steam/linuxarm64",
"FEX_ROOTFS": "/usr/share/guestos/fex-mesa", "FEX_TSOENABLED": "1"}
guest = FEX["guest_environment"]({"portable": True}, env)
self.assertEqual((guest["LD_PRELOAD"], guest["LD_LIBRARY_PATH"], guest["FEX_PORTABLE"]), (str(x86), "/game/lib", "1"))
self.assertEqual((guest["LD_PRELOAD"], guest["LD_LIBRARY_PATH"], guest["FEX_PORTABLE"]),
("%s:libMelonLoader.Bootstrap.so" % x86, "/game/lib", "1"))
self.assertNotIn("FEX_ROOTFS", guest)
self.assertEqual(guest["FEX_TSOENABLED"], "1")
self.assertTrue(guest["FEX_APP_CONFIG_LOCATION"].endswith("/config/"))
@@ -690,6 +691,32 @@ class LaunchTest(FexTestCase):
command, env, mode = G["launch"].call_args[0]
self.assertEqual((command, mode), (["/game/run.sh", "-a"], "auto"))
def test_steam_games_get_their_settings_and_linux_unity_keeps_tso(self):
config = self.home / ".config/droiddeck/game-environment.json"
unity = self.write(self.tmp / "Megabonk/Megabonk.x86_64", elf(62), 0o755)
self.write(self.tmp / "Megabonk/UnityPlayer.so", elf(62))
self.write(self.tmp / "Megabonk/GameAssembly.so", elf(62))
other = self.write(self.tmp / "Other/game.x86_64", elf(62), 0o755)
self.write(config, json.dumps({"version": 1, "shared": {"FEX_TSOENABLED": "0", "FEX_MULTIBLOCK": "1"},
"games": {"20": {"FEX_TSOENABLED": "0"}}}))
def started(program, app):
with mock.patch.dict(G, {"launch": mock.Mock(return_value=0), "GAME_ENVIRONMENT": str(config)}), \
mock.patch.dict(os.environ, {"SteamAppId": app}, clear=True):
_, said = self.quiet(FEX["main"], ["steam", "waitforexitandrun", str(program), "-arg"])
command, env, _ = G["launch"].call_args[0]
self.assertEqual(command, [str(program), "-arg"])
return env.get("FEX_TSOENABLED"), env.get("FEX_MULTIBLOCK"), said
tso, multiblock, said = started(unity, "10")
self.assertEqual((tso, multiblock), ("1", "1"))
self.assertIn("Unity: FEX with TSO", said)
self.assertEqual(started(other, "10")[:2], ("0", "1"))
self.assertEqual(started(unity, "20")[:2], ("0", "1"))
config.unlink()
self.assertEqual(started(unity, "10")[:2], ("1", None))
self.assertEqual(started(other, "10")[:2], (None, None))
class AppInfoTest(unittest.TestCase):
COMPAT = runpy.run_path(str(BIN / "steam-compatibility"))
+7 -3
View File
@@ -1160,8 +1160,12 @@ class BundleTest(unittest.TestCase):
except ImportError:
self.skipTest("PyYAML is not installed")
apk = yaml.safe_load((self.ROOT / ".github/workflows/build.yml").read_text())
steps = {step.get("name"): step for step in apk["jobs"]["build"]["steps"]}
bundle = steps["Bundle the droiddeck-esync packs"]
steps = {step.get("name"): step for step in apk["jobs"]["components"]["steps"]}
import json
recipes = json.loads((self.ROOT / "tools/components/recipes.json").read_text())
bundle = dict(steps["Bundle the droiddeck-esync packs"])
self.assertIn("prepare.py esync", bundle["run"])
bundle["run"] = (self.ROOT / recipes["esync"]["script"]).read_text()
self.assertNotIn("if", bundle)
self.assertIn("tools/droiddeck-esync/release.env", bundle["run"])
self.assertIn('release_url="https://github.com/${SYNC_BUNDLE_REPO}/releases/download/${SYNC_BUNDLE_TAG}"', bundle["run"])
@@ -1169,7 +1173,7 @@ class BundleTest(unittest.TestCase):
self.assertIn('curl -fsSL --retry 3 -o sync-index.json "$release_url/index.json"', bundle["run"])
self.assertIn("sha256sum -c -", bundle["run"])
self.assertNotIn("revoked.txt", bundle["run"])
stage = next(step for step in apk["jobs"]["build"]["steps"] if "overlay/usr/local/bin/droiddeck-*" in step.get("run", ""))
stage = {"run": (self.ROOT / recipes["runtime"]["script"]).read_text()}
self.assertIn("tools/linuxfs/overlay/usr/local/bin/droiddeck-* ", stage["run"])
local = (self.ROOT / "tools/build_local.sh").read_text()
self.assertIn("tools/linuxfs/overlay/usr/local/bin/droiddeck-* ", local)
+99
View File
@@ -357,6 +357,59 @@ class GameEnvironmentTest(unittest.TestCase):
(asylum / name).touch()
self.assertEqual(MODULE["engine_fixes"](str(asylum / "BmLauncher.exe"), []), ({}, [], []))
def test_caravan_sandwitch_and_cyberpunk_run_at_feature_level_12_1(self):
with tempfile.TemporaryDirectory() as tmp:
caravan = Path(tmp) / "Caravan SandWitch"
shipping = caravan / "CaravanSandWitch/Binaries/Win64"
shipping.mkdir(parents=True)
for name in ("CaravanSandWitch.exe", ".droiddeck-launch.bat"):
(caravan / name).touch()
(shipping / "CaravanSandWitch-Win64-Shipping.exe").touch()
cyberpunk = Path(tmp) / "Cyberpunk 2077"
(cyberpunk / "bin/x64").mkdir(parents=True)
for name in ("REDprelauncher.exe", "bin/x64/Cyberpunk2077.exe"):
(cyberpunk / name).touch()
games = [caravan / "CaravanSandWitch.exe", caravan / ".droiddeck-launch.bat",
shipping / "CaravanSandWitch-Win64-Shipping.exe",
cyberpunk / "REDprelauncher.exe", cyberpunk / "bin/x64/Cyberpunk2077.exe"]
for exe in games:
env, extra, notes = MODULE["engine_fixes"](str(exe), [])
self.assertEqual((env, extra), ({"VKD3D_FEATURE_LEVEL": "12_1"}, []), exe)
self.assertTrue(notes)
config = {"version": 1, "shared": {"VKD3D_FEATURE_LEVEL": "12_2"}}
self.assertEqual(MODULE["apply_config"]({}, config, "1582650", env)["VKD3D_FEATURE_LEVEL"], "12_1")
config["games"] = {"1582650": {"VKD3D_FEATURE_LEVEL": "12_2"}}
self.assertEqual(MODULE["apply_config"]({}, config, "1582650", env)["VKD3D_FEATURE_LEVEL"], "12_2")
other = Path(tmp) / "Other/Binaries/Win64"
other.mkdir(parents=True)
(other / "Other-Win64-Shipping.exe").touch()
self.assertEqual(MODULE["engine_fixes"](str(other / "Other-Win64-Shipping.exe"), []), ({}, [], []))
witcher = Path(tmp) / "The Witcher 3"
witcher.mkdir()
(witcher / "REDprelauncher.exe").touch()
self.assertNotIn("VKD3D_FEATURE_LEVEL", MODULE["engine_fixes"](str(witcher / "REDprelauncher.exe"), [])[0])
def test_unity_players_keep_tso_over_the_preset(self):
with tempfile.TemporaryDirectory() as tmp:
def game(*names):
for name in names:
(Path(tmp) / name).parent.mkdir(parents=True, exist_ok=True)
(Path(tmp) / name).touch()
return str(Path(tmp) / names[0])
il2cpp = game("Megabonk/Megabonk.x86_64", "Megabonk/UnityPlayer.so", "Megabonk/GameAssembly.so")
mono = game("Mono/Game.x86_64", "Mono/UnityPlayer.so")
windows = game("Crab Game/Crab Game.exe", "Crab Game/UnityPlayer.dll", "Crab Game/GameAssembly.dll")
windows_mono = game("RoR2/Risk of Rain 2.exe", "RoR2/UnityPlayer.dll", "RoR2/MonoBleedingEdge/mono-2.0-bdwgc.dll")
for exe in (il2cpp, mono, windows, windows_mono):
env, extra, notes = MODULE["engine_fixes"](exe, [])
self.assertEqual((env, extra, notes), ({"FEX_TSOENABLED": "1"}, [], ["Unity: FEX with TSO"]))
performance = {"version": 1, "shared": {"FEX_TSOENABLED": "0", "FEX_VECTORTSOENABLED": "0"}}
result = MODULE["apply_config"]({}, performance, "3405340", env)
self.assertEqual((result["FEX_TSOENABLED"], result["FEX_VECTORTSOENABLED"]), ("1", "0"))
performance["games"] = {"3405340": {"FEX_TSOENABLED": "0"}}
self.assertEqual(MODULE["apply_config"]({}, performance, "3405340", env)["FEX_TSOENABLED"], "0")
def test_a_new_fix_only_needs_registering(self):
with tempfile.TemporaryDirectory() as tmp:
(Path(tmp) / "Game.exe").touch()
@@ -1163,6 +1216,52 @@ class DirectAudioPrefixTest(unittest.TestCase):
self.assertIsNone(self.link("system32"))
class UbisoftOverlayTest(unittest.TestCase):
SETTINGS = (
"language:\n code: en-US\n"
"overlay:\n enabled: true\n forceunhookgame: false\n fps_enabled: false\n warning_enabled: true\n"
"patchTrigger:\n source: USER_RESTART\n"
)
def test_the_overlay_and_its_prompt_are_turned_off_and_the_rest_is_kept(self):
got = MODULE["overlay_off"](self.SETTINGS)
self.assertEqual(got, self.SETTINGS.replace("enabled: true", "enabled: false"))
self.assertEqual(MODULE["overlay_off"](got), got)
def test_missing_keys_or_section_are_added(self):
self.assertEqual(MODULE["overlay_off"]("overlay:\n fps_enabled: true\nmisc:\n a: 1\n"),
"overlay:\n fps_enabled: true\n enabled: false\n warning_enabled: false\nmisc:\n a: 1\n")
self.assertEqual(MODULE["overlay_off"](""), "overlay:\n enabled: false\n warning_enabled: false\n")
def test_blank_lines_inside_the_overlay_section_do_not_end_it(self):
text = "overlay:\n enabled: true\n\n warning_enabled: true\n\nmisc:\n a: 1\n"
self.assertEqual(MODULE["overlay_off"](text), text.replace("true", "false"))
self.assertEqual(MODULE["overlay_off"]("overlay:\n fps_enabled: true\n\nmisc:\n a: 1\n"),
"overlay:\n fps_enabled: true\n enabled: false\n warning_enabled: false\n\nmisc:\n a: 1\n")
def test_an_annotated_overlay_header_is_the_same_section(self):
self.assertEqual(MODULE["overlay_off"]("overlay: # in-game\n enabled: true\n"),
"overlay: # in-game\n enabled: false\n warning_enabled: false\n")
def test_only_prefixes_with_ubisoft_connect_are_touched(self):
with tempfile.TemporaryDirectory() as tmp:
prefix = Path(tmp) / "447040/pfx"
env = {"STEAM_COMPAT_DATA_PATH": str(prefix.parent)}
settings = prefix / MODULE["UBISOFT_SETTINGS"]
prefix.mkdir(parents=True)
MODULE["ubisoft_overlay_off"](env)
self.assertFalse(settings.exists())
(prefix / MODULE["UBISOFT_LAUNCHER"]).mkdir(parents=True)
MODULE["ubisoft_overlay_off"](env)
self.assertEqual(settings.read_text(), "overlay:\n enabled: false\n warning_enabled: false\n")
settings.write_text(self.SETTINGS)
settings.chmod(0o644)
MODULE["ubisoft_overlay_off"](env)
self.assertEqual(settings.read_text(), self.SETTINGS.replace("enabled: true", "enabled: false"))
self.assertEqual(settings.stat().st_mode & 0o777, 0o644)
self.assertEqual(sorted(p.name for p in settings.parent.iterdir()), ["settings.yaml"])
if __name__ == "__main__":
unittest.main()
+188
View File
@@ -0,0 +1,188 @@
#include <assert.h>
#include <errno.h>
#include <pthread.h>
#include <stdarg.h>
#include <stdatomic.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>
#include "../../app/src/main/cpp/adrenotools/src/hook/kgsl.h"
#include "../../app/src/main/cpp/waylandcomp/src/gpu_clock_pin.h"
enum { NONE, OPEN, DEVICE, CONTEXT, CONSTRAINT, MEMORY, MAP, SYNC, SUBMIT, THREAD, WAIT };
static atomic_int failure, descriptors, contexts, memories, mappings, submissions, waits;
static atomic_bool timeout_wait;
static bool legacy_core;
static uint32_t commands[1024];
int __android_log_print(int priority, const char *tag, const char *format, ...) {
(void)priority; (void)tag; (void)format;
return 0;
}
int __wrap_open(const char *path, int flags, ...) {
(void)flags;
assert(!strcmp(path, "/dev/kgsl-3d0"));
if (atomic_load(&failure) == OPEN) { errno = EACCES; return -1; }
assert(atomic_fetch_add(&descriptors, 1) == 0);
return 91;
}
int __wrap_close(int fd) {
assert(fd == 91);
assert(atomic_fetch_sub(&descriptors, 1) == 1);
errno = EBADF;
return 0;
}
void *__wrap_mmap(void *address, size_t size, int protection, int flags, int fd, off_t offset) {
(void)address; (void)protection; (void)flags;
assert(fd == 91 && size == sizeof(commands) && offset == (23 << 12));
if (atomic_load(&failure) == MAP) { errno = EACCES; return MAP_FAILED; }
assert(atomic_fetch_add(&mappings, 1) == 0);
return commands;
}
int __wrap_munmap(void *address, size_t size) {
assert(address == commands && size == sizeof(commands));
assert(atomic_fetch_sub(&mappings, 1) == 1);
return 0;
}
#ifdef __ANDROID__
int __wrap_ioctl(int fd, int request, ...) {
#else
int __wrap_ioctl(int fd, unsigned long request, ...) {
#endif
assert(fd == 91);
va_list args;
va_start(args, request);
void *arg = va_arg(args, void *);
va_end(args);
int step = NONE;
switch ((unsigned int)request) {
case IOCTL_KGSL_DEVICE_GETPROPERTY: step = DEVICE; break;
case IOCTL_KGSL_DRAWCTXT_CREATE: step = CONTEXT; break;
case IOCTL_KGSL_SETPROPERTY: step = CONSTRAINT; break;
case IOCTL_KGSL_GPUMEM_ALLOC_ID: step = MEMORY; break;
case IOCTL_KGSL_GPUMEM_SYNC_CACHE: step = SYNC; break;
case IOCTL_KGSL_GPU_COMMAND: step = SUBMIT; break;
case IOCTL_KGSL_DEVICE_WAITTIMESTAMP_CTXTID: step = WAIT; break;
}
if (step && atomic_load(&failure) == step) { errno = EACCES; return -1; }
switch ((unsigned int)request) {
case IOCTL_KGSL_DEVICE_GETPROPERTY: {
struct kgsl_device_getproperty *property = arg;
assert(property->type == KGSL_PROP_DEVICE_INFO);
((struct kgsl_devinfo *)property->value)->chip_id = legacy_core ? 0x04030000 : 0x07030000;
break;
}
case IOCTL_KGSL_DRAWCTXT_CREATE: {
struct kgsl_drawctxt_create *context = arg;
assert(context->flags & KGSL_CONTEXT_PWR_CONSTRAINT);
context->drawctxt_id = 17;
atomic_fetch_add(&contexts, 1);
break;
}
case IOCTL_KGSL_SETPROPERTY: {
struct kgsl_device_getproperty *property = arg;
assert(property->type == KGSL_PROP_PWR_CONSTRAINT);
struct kgsl_device_constraint *constraint = property->value;
assert(constraint->context_id == 17 && constraint->type == KGSL_CONSTRAINT_PWRLEVEL);
assert(((struct kgsl_device_constraint_pwrlevel *)constraint->data)->level == KGSL_CONSTRAINT_PWR_MAX);
break;
}
case IOCTL_KGSL_GPUMEM_ALLOC_ID: {
struct kgsl_gpumem_alloc_id *memory = arg;
memory->id = 23; memory->gpuaddr = 0x100000; memory->mmapsize = sizeof(commands);
atomic_fetch_add(&memories, 1);
break;
}
case IOCTL_KGSL_GPUMEM_SYNC_CACHE:
assert(((struct kgsl_gpumem_sync_cache *)arg)->op == KGSL_GPUMEM_CACHE_CLEAN);
break;
case IOCTL_KGSL_GPU_COMMAND: {
struct kgsl_gpu_command *command = arg;
struct kgsl_command_object *object = (void *)(uintptr_t)command->cmdlist;
assert(command->context_id == 17 && command->numcmds == 1);
assert(command->flags & KGSL_CMDBATCH_PWR_CONSTRAINT);
assert(object->id == 23 && object->gpuaddr == 0x100000 && object->size == 8);
assert(commands[0] == (legacy_core ? 0xc0001000 : 0x70100001) && commands[1] == 0);
command->timestamp = atomic_fetch_add(&submissions, 1) + 1;
break;
}
case IOCTL_KGSL_DEVICE_WAITTIMESTAMP_CTXTID:
atomic_fetch_add(&waits, 1);
if (atomic_load(&timeout_wait)) { errno = ETIMEDOUT; return -1; }
break;
case IOCTL_KGSL_DRAWCTXT_DESTROY:
assert(atomic_fetch_sub(&contexts, 1) == 1);
break;
case IOCTL_KGSL_GPUMEM_FREE_ID:
assert(atomic_fetch_sub(&memories, 1) == 1);
break;
default: assert(!"unexpected ioctl");
}
return 0;
}
int __real_pthread_create(pthread_t *, const pthread_attr_t *, void *(*)(void *), void *);
int __wrap_pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start)(void *), void *arg) {
if (atomic_load(&failure) == THREAD) return EAGAIN;
return __real_pthread_create(thread, attr, start, arg);
}
static void assert_released(void) {
assert(!atomic_load(&descriptors) && !atomic_load(&contexts));
assert(!atomic_load(&memories) && !atomic_load(&mappings));
}
int main(void) {
assert(gpu_clock_pin_set(false));
assert_released();
legacy_core = true;
assert(gpu_clock_pin_set(true));
assert(gpu_clock_pin_set(false));
assert_released();
legacy_core = false;
for (int step = OPEN; step <= THREAD; ++step) {
atomic_store(&failure, step);
assert(!gpu_clock_pin_set(true));
assert(errno == (step == THREAD ? EAGAIN : EACCES));
assert_released();
}
atomic_store(&failure, NONE);
atomic_store(&submissions, 0);
assert(gpu_clock_pin_set(true));
assert(gpu_clock_pin_set(true));
for (int i = 0; i < 100 && atomic_load(&submissions) < 2; ++i) usleep(10000);
assert(atomic_load(&submissions) > 1);
assert(gpu_clock_pin_set(false));
assert(gpu_clock_pin_set(false));
assert_released();
atomic_store(&timeout_wait, true);
atomic_store(&submissions, 0);
atomic_store(&waits, 0);
assert(gpu_clock_pin_set(true));
for (int i = 0; i < 100 && atomic_load(&waits) < 2; ++i) usleep(10000);
assert(atomic_load(&waits) >= 2);
assert(atomic_load(&submissions) == 1);
assert(gpu_clock_pin_set(false));
assert_released();
atomic_store(&timeout_wait, false);
atomic_store(&failure, WAIT);
assert(gpu_clock_pin_set(true));
for (int i = 0; i < 100 && atomic_load(&descriptors); ++i) usleep(10000);
assert_released();
atomic_store(&failure, NONE);
assert(gpu_clock_pin_set(true));
assert(gpu_clock_pin_set(false));
assert_released();
puts("gpu clock pin: failure cleanup, renewal, bounded queue and restart passed");
return 0;
}
+30
View File
@@ -0,0 +1,30 @@
import pathlib
import shutil
import subprocess
import sys
import tempfile
import unittest
ROOT = pathlib.Path(__file__).resolve().parents[2]
class GpuClockPinTest(unittest.TestCase):
@unittest.skipUnless(sys.platform.startswith('linux') and shutil.which('cc'), 'requires a Linux C compiler')
def test_constraint_lifecycle(self):
with tempfile.TemporaryDirectory() as tmp:
directory = pathlib.Path(tmp)
(directory / 'android').mkdir()
(directory / 'android/log.h').write_text(
'#define ANDROID_LOG_WARN 5\n'
'int __android_log_print(int, const char *, const char *, ...);\n')
binary = directory / 'gpu-clock-pin-test'
subprocess.run([
'cc', '-std=c11', '-D_DEFAULT_SOURCE', '-D__user=', '-Wall', '-Wextra', '-Werror',
'-I', str(directory), '-pthread',
str(ROOT / 'tools/tests/test_gpu_clock_pin.c'),
str(ROOT / 'app/src/main/cpp/waylandcomp/src/gpu_clock_pin.c'),
'-Wl,--wrap=open,--wrap=close,--wrap=mmap,--wrap=munmap,--wrap=ioctl,--wrap=pthread_create',
'-o', str(binary),
], check=True)
subprocess.run([str(binary)], check=True, timeout=10)
+580
View File
@@ -0,0 +1,580 @@
import os
from pathlib import Path
import shutil
import subprocess
import tempfile
import unittest
PRELOAD = Path(__file__).resolve().parents[1] / "linuxfs/preload"
NTDLL = r"""
#define _GNU_SOURCE
#include <signal.h>
#include <string.h>
int install_handler(int sig, void (*fn)(int, siginfo_t *, void *), void **seen) {
struct sigaction sa, old;
memset(&sa, 0, sizeof(sa));
sa.sa_sigaction = fn;
sa.sa_flags = SA_SIGINFO | SA_RESTART;
sigemptyset(&sa.sa_mask);
if (sigaction(sig, &sa, NULL) != 0) return -1;
if (sigaction(sig, NULL, &old) != 0) return -1;
*seen = (void *)old.sa_sigaction;
return 0;
}
int install_oneshot(int sig, void (*fn)(int, siginfo_t *, void *)) {
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_sigaction = fn;
sa.sa_flags = SA_SIGINFO | SA_RESTART | SA_RESETHAND;
sigemptyset(&sa.sa_mask);
return sigaction(sig, &sa, NULL);
}
"""
PROGRAM = r"""
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <linux/ioctl.h>
#include <pthread.h>
#include <semaphore.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
struct sem_args { uint32_t count, max; };
struct mutex_args { uint32_t owner, count; };
struct event_args { uint32_t manual, signaled; };
struct wait_args { uint64_t timeout, objs; uint32_t count, index, flags, owner, alert, pad; };
#define NT_CREATE_SEM _IOW('N', 0x80, struct sem_args)
#define NT_SEM_RELEASE _IOWR('N', 0x81, uint32_t)
#define NT_WAIT_ANY _IOWR('N', 0x82, struct wait_args)
#define NT_WAIT_ALL _IOWR('N', 0x83, struct wait_args)
#define NT_CREATE_MUTEX _IOW('N', 0x84, struct mutex_args)
#define NT_MUTEX_UNLOCK _IOWR('N', 0x85, struct mutex_args)
#define NT_MUTEX_KILL _IOW('N', 0x86, uint32_t)
#define NT_CREATE_EVENT _IOW('N', 0x87, struct event_args)
#define NT_EVENT_SET _IOR('N', 0x88, uint32_t)
#define NT_EVENT_RESET _IOR('N', 0x89, uint32_t)
#define NT_EVENT_PULSE _IOR('N', 0x8a, uint32_t)
#define NT_SEM_READ _IOR('N', 0x8b, struct sem_args)
#define NT_MUTEX_READ _IOR('N', 0x8c, struct mutex_args)
#define NT_EVENT_READ _IOR('N', 0x8d, struct event_args)
int install_handler(int sig, void (*fn)(int, siginfo_t *, void *), void **seen);
int install_oneshot(int sig, void (*fn)(int, siginfo_t *, void *));
static int dev, fails;
#define CHECK(name, cond) do { if (!(cond)) { printf("FAIL %s line %d errno %d\n", name, __LINE__, errno); fails++; } } while (0)
static uint64_t mono_ns(int64_t delta_ms) {
struct timespec t;
clock_gettime(CLOCK_MONOTONIC, &t);
return (uint64_t)t.tv_sec * 1000000000ull + (uint64_t)t.tv_nsec + (uint64_t)(delta_ms * 1000000);
}
static int wait_on(unsigned long req, const int *objs, uint32_t count, uint32_t owner, int alert, uint64_t timeout, uint32_t *index) {
struct wait_args w = {timeout, (uintptr_t)objs, count, 0xdead, 0, owner, (uint32_t)alert, 0};
int r = ioctl(dev, req, &w);
if (index) *index = w.index;
return r < 0 ? errno : 0;
}
static int event(int manual, int signaled) {
struct event_args a = {(uint32_t)manual, (uint32_t)signaled};
return ioctl(dev, NT_CREATE_EVENT, &a);
}
static int sem(uint32_t count, uint32_t max) {
struct sem_args a = {count, max};
return ioctl(dev, NT_CREATE_SEM, &a);
}
static int mutex(uint32_t owner, uint32_t count) {
struct mutex_args a = {owner, count};
return ioctl(dev, NT_CREATE_MUTEX, &a);
}
static uint32_t prev_of(int fd, unsigned long req, uint32_t in) {
uint32_t v = in;
if (ioctl(fd, req, &v) < 0) return 0xffffffff;
return v;
}
static void semaphores(void) {
struct sem_args bad = {3, 2}, r;
CHECK("sem_create_invalid", ioctl(dev, NT_CREATE_SEM, &bad) < 0 && errno == EINVAL);
int s = sem(2, 2);
CHECK("sem_create", s >= 0);
uint32_t idx;
CHECK("sem_wait1", wait_on(NT_WAIT_ANY, &s, 1, 1, 0, 0, &idx) == 0 && idx == 0);
CHECK("sem_wait2", wait_on(NT_WAIT_ANY, &s, 1, 1, 0, 0, &idx) == 0);
CHECK("sem_empty", wait_on(NT_WAIT_ANY, &s, 1, 1, 0, 0, NULL) == ETIMEDOUT);
CHECK("sem_release_prev", prev_of(s, NT_SEM_RELEASE, 1) == 0);
CHECK("sem_overflow", prev_of(s, NT_SEM_RELEASE, 2) == 0xffffffff && errno == EOVERFLOW);
CHECK("sem_read", ioctl(s, NT_SEM_READ, &r) == 0 && r.count == 1 && r.max == 2);
close(s);
}
static void mutexes(void) {
struct mutex_args bad = {0, 1}, r, u;
CHECK("mutex_create_invalid", ioctl(dev, NT_CREATE_MUTEX, &bad) < 0 && errno == EINVAL);
int m = mutex(0, 0);
uint32_t idx;
CHECK("mutex_owner_zero", wait_on(NT_WAIT_ANY, &m, 1, 0, 0, 0, NULL) == EINVAL);
CHECK("mutex_take", wait_on(NT_WAIT_ANY, &m, 1, 7, 0, 0, &idx) == 0 && idx == 0);
CHECK("mutex_recurse", wait_on(NT_WAIT_ANY, &m, 1, 7, 0, 0, NULL) == 0);
CHECK("mutex_other", wait_on(NT_WAIT_ANY, &m, 1, 8, 0, 0, NULL) == ETIMEDOUT);
CHECK("mutex_read", ioctl(m, NT_MUTEX_READ, &r) == 0 && r.owner == 7 && r.count == 2);
u = (struct mutex_args){8, 0};
CHECK("mutex_unlock_eperm", ioctl(m, NT_MUTEX_UNLOCK, &u) < 0 && errno == EPERM);
u = (struct mutex_args){0, 0};
CHECK("mutex_unlock_einval", ioctl(m, NT_MUTEX_UNLOCK, &u) < 0 && errno == EINVAL);
u = (struct mutex_args){7, 0};
CHECK("mutex_unlock_prev", ioctl(m, NT_MUTEX_UNLOCK, &u) == 0 && u.count == 2);
uint32_t who = 8;
CHECK("mutex_kill_eperm", ioctl(m, NT_MUTEX_KILL, &who) < 0 && errno == EPERM);
who = 7;
CHECK("mutex_kill", ioctl(m, NT_MUTEX_KILL, &who) == 0);
CHECK("mutex_read_dead", ioctl(m, NT_MUTEX_READ, &r) < 0 && errno == EOWNERDEAD && r.owner == 0 && r.count == 0);
CHECK("mutex_abandoned", wait_on(NT_WAIT_ANY, &m, 1, 9, 0, 0, &idx) == EOWNERDEAD && idx == 0);
CHECK("mutex_after_abandon", ioctl(m, NT_MUTEX_READ, &r) == 0 && r.owner == 9 && r.count == 1);
close(m);
}
static void events(void) {
int a = event(0, 1), m = event(1, 0);
struct event_args r;
CHECK("auto_take", wait_on(NT_WAIT_ANY, &a, 1, 1, 0, 0, NULL) == 0);
CHECK("auto_consumed", wait_on(NT_WAIT_ANY, &a, 1, 1, 0, 0, NULL) == ETIMEDOUT);
CHECK("set_prev0", prev_of(m, NT_EVENT_SET, 0) == 0);
CHECK("set_prev1", prev_of(m, NT_EVENT_SET, 0) == 1);
CHECK("manual_kept", wait_on(NT_WAIT_ANY, &m, 1, 1, 0, 0, NULL) == 0 && wait_on(NT_WAIT_ANY, &m, 1, 1, 0, 0, NULL) == 0);
CHECK("reset_prev", prev_of(m, NT_EVENT_RESET, 0) == 1);
CHECK("pulse_prev", prev_of(m, NT_EVENT_PULSE, 0) == 0);
CHECK("pulse_leaves_unsignaled", ioctl(m, NT_EVENT_READ, &r) == 0 && r.manual == 1 && r.signaled == 0);
close(a);
close(m);
}
static void any_order(void) {
int o[4] = {event(1, 0), sem(1, 5), event(1, 1), sem(0, 5)};
int alert = event(0, 1);
uint32_t idx;
CHECK("any_lowest", wait_on(NT_WAIT_ANY, o, 4, 1, alert, 0, &idx) == 0 && idx == 1);
CHECK("any_next", wait_on(NT_WAIT_ANY, o, 4, 1, alert, 0, &idx) == 0 && idx == 2);
prev_of(o[2], NT_EVENT_RESET, 0);
CHECK("any_alert", wait_on(NT_WAIT_ANY, o, 4, 1, alert, 0, &idx) == 0 && idx == 4);
CHECK("any_alert_consumed", wait_on(NT_WAIT_ANY, o, 4, 1, alert, 0, NULL) == ETIMEDOUT);
int dup[3] = {o[3], o[0], o[3]};
prev_of(o[3], NT_SEM_RELEASE, 1);
CHECK("any_duplicate_lowest", wait_on(NT_WAIT_ANY, dup, 3, 1, 0, 0, &idx) == 0 && idx == 0);
int many[65];
for (int i = 0; i < 65; i++) many[i] = o[0];
CHECK("any_too_many", wait_on(NT_WAIT_ANY, many, 65, 1, 0, 0, NULL) == EINVAL);
int badfd = 0;
CHECK("any_invalid_fd", wait_on(NT_WAIT_ANY, &badfd, 1, 1, 0, 0, NULL) == EINVAL);
CHECK("any_timeout", wait_on(NT_WAIT_ANY, o, 1, 1, 0, mono_ns(30), NULL) == ETIMEDOUT);
for (int i = 0; i < 4; i++) close(o[i]);
close(alert);
}
static void all_semantics(void) {
int s = sem(1, 5), e = event(0, 0), m = mutex(0, 0);
int o[3] = {s, e, m};
struct sem_args sr;
uint32_t idx;
CHECK("all_not_ready", wait_on(NT_WAIT_ALL, o, 3, 4, 0, 0, NULL) == ETIMEDOUT);
CHECK("all_untouched", ioctl(s, NT_SEM_READ, &sr) == 0 && sr.count == 1);
prev_of(e, NT_EVENT_SET, 0);
CHECK("all_ready", wait_on(NT_WAIT_ALL, o, 3, 4, 0, 0, &idx) == 0 && idx == 0);
CHECK("all_took_sem", ioctl(s, NT_SEM_READ, &sr) == 0 && sr.count == 0);
int dup[2] = {s, s};
CHECK("all_duplicate", wait_on(NT_WAIT_ALL, dup, 2, 4, 0, 0, NULL) == EINVAL);
int alert = event(1, 1);
CHECK("all_alert", wait_on(NT_WAIT_ALL, o, 3, 4, alert, 0, &idx) == 0 && idx == 3);
close(s);
close(e);
close(m);
close(alert);
}
static int wake_obj;
static uint32_t wake_index;
static int wake_ret;
static void *waiter(void *p) {
int all = (int)(intptr_t)p;
int objs[2] = {wake_obj, wake_obj + 0};
wake_ret = wait_on(all ? NT_WAIT_ALL : NT_WAIT_ANY, objs, 1, 3, 0, UINT64_MAX, &wake_index);
return NULL;
}
static void cross_thread(void) {
wake_obj = event(0, 0);
pthread_t t;
pthread_create(&t, NULL, waiter, NULL);
usleep(30000);
prev_of(wake_obj, NT_EVENT_SET, 0);
pthread_join(t, NULL);
CHECK("thread_wake", wake_ret == 0 && wake_index == 0);
struct event_args r;
CHECK("thread_auto_reset", ioctl(wake_obj, NT_EVENT_READ, &r) == 0 && r.signaled == 0);
close(wake_obj);
int s = sem(0, 10);
wake_obj = s;
pthread_t ts[3];
for (int i = 0; i < 3; i++) pthread_create(&ts[i], NULL, waiter, NULL);
usleep(30000);
CHECK("sem_release_three", prev_of(s, NT_SEM_RELEASE, 3) == 0);
for (int i = 0; i < 3; i++) pthread_join(ts[i], NULL);
struct sem_args sr;
CHECK("sem_all_taken", ioctl(s, NT_SEM_READ, &sr) == 0 && sr.count == 0);
close(s);
}
static void cross_process(void) {
int sv[2];
CHECK("socketpair", socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == 0);
int inherited = event(0, 0);
pid_t pid = fork();
if (pid == 0) {
uint32_t idx;
int r1 = wait_on(NT_WAIT_ANY, &inherited, 1, 5, 0, mono_ns(5000), &idx);
char c;
struct iovec io = {&c, 1};
char cbuf[CMSG_SPACE(sizeof(int))];
struct msghdr msg = {0};
msg.msg_iov = &io;
msg.msg_iovlen = 1;
msg.msg_control = cbuf;
msg.msg_controllen = sizeof(cbuf);
int got = -1;
if (recvmsg(sv[1], &msg, 0) == 1) memcpy(&got, CMSG_DATA(CMSG_FIRSTHDR(&msg)), sizeof(int));
int r2 = wait_on(NT_WAIT_ANY, &got, 1, 5, 0, mono_ns(5000), NULL);
_exit(r1 == 0 && r2 == 0 ? 0 : 1);
}
usleep(30000);
prev_of(inherited, NT_EVENT_SET, 0);
int sent = sem(0, 1);
char c = 'x';
struct iovec io = {&c, 1};
char cbuf[CMSG_SPACE(sizeof(int))];
struct msghdr msg = {0};
msg.msg_iov = &io;
msg.msg_iovlen = 1;
msg.msg_control = cbuf;
msg.msg_controllen = sizeof(cbuf);
struct cmsghdr *cm = CMSG_FIRSTHDR(&msg);
cm->cmsg_level = SOL_SOCKET;
cm->cmsg_type = SCM_RIGHTS;
cm->cmsg_len = CMSG_LEN(sizeof(int));
memcpy(CMSG_DATA(cm), &sent, sizeof(int));
sendmsg(sv[0], &msg, 0);
usleep(30000);
prev_of(sent, NT_SEM_RELEASE, 1);
int status = 0;
waitpid(pid, &status, 0);
CHECK("process_wake", WIFEXITED(status) && WEXITSTATUS(status) == 0);
close(inherited);
close(sent);
close(sv[0]);
close(sv[1]);
}
static int ping_obj, pong_obj, stress_sem;
static void *stress_ponger(void *p) {
long n = (long)p;
uint32_t v;
for (long i = 0; i < n; i++) {
if (wait_on(NT_WAIT_ANY, &ping_obj, 1, 21, 0, UINT64_MAX, NULL) != 0) return (void *)1;
ioctl(pong_obj, NT_EVENT_SET, &v);
}
return NULL;
}
static void *stress_hammer(void *p) {
long n = (long)p;
for (long i = 0; i < n; i++) {
if (wait_on(NT_WAIT_ANY, &stress_sem, 1, 22, 0, UINT64_MAX, NULL) != 0) return (void *)1;
uint32_t c = 1;
ioctl(stress_sem, NT_SEM_RELEASE, &c);
}
return NULL;
}
static void stress(void) {
long rounds = 20000;
ping_obj = event(0, 0);
pong_obj = event(0, 0);
pthread_t t;
void *bad = NULL;
pthread_create(&t, NULL, stress_ponger, (void *)rounds);
uint32_t v;
int ok = 1;
for (long i = 0; i < rounds && ok; i++) {
ioctl(ping_obj, NT_EVENT_SET, &v);
ok = wait_on(NT_WAIT_ANY, &pong_obj, 1, 20, 0, UINT64_MAX, NULL) == 0;
}
pthread_join(t, &bad);
CHECK("stress_pingpong", ok && bad == NULL);
stress_sem = sem(1, 1);
pthread_t th[4];
for (int i = 0; i < 4; i++) pthread_create(&th[i], NULL, stress_hammer, (void *)5000L);
int hammer_ok = 1;
for (int i = 0; i < 4; i++) {
pthread_join(th[i], &bad);
hammer_ok &= bad == NULL;
}
struct sem_args sr;
CHECK("stress_contended", hammer_ok && ioctl(stress_sem, NT_SEM_READ, &sr) == 0 && sr.count == 1);
close(ping_obj);
close(pong_obj);
close(stress_sem);
}
static int busy_obj;
static volatile int handled, stop_worker;
static sem_t parked, unpark;
static void reentrant(int sig, siginfo_t *si, void *uc) {
(void)sig; (void)si; (void)uc;
uint32_t v = 0;
ioctl(busy_obj, NT_EVENT_SET, &v);
handled++;
}
static void suspending(int sig, siginfo_t *si, void *uc) {
(void)sig; (void)si; (void)uc;
sem_post(&parked);
while (sem_wait(&unpark) != 0) {}
handled++;
}
static void *worker(void *p) {
(void)p;
uint32_t v;
while (!stop_worker) {
ioctl(busy_obj, NT_EVENT_SET, &v);
wait_on(NT_WAIT_ANY, &busy_obj, 1, 11, 0, 0, NULL);
}
return NULL;
}
static void signals(void) {
void *seen = NULL;
busy_obj = event(0, 0);
CHECK("install_reentrant", install_handler(SIGUSR1, reentrant, &seen) == 0);
CHECK("query_original", seen == (void *)reentrant);
pthread_t w;
stop_worker = 0;
handled = 0;
pthread_create(&w, NULL, worker, NULL);
for (int i = 0; i < 2000; i++) {
pthread_kill(w, SIGUSR1);
if (i % 64 == 0) usleep(100);
}
usleep(20000);
stop_worker = 1;
pthread_join(w, NULL);
CHECK("reentrant_ran", handled > 0);
sem_init(&parked, 0, 0);
sem_init(&unpark, 0, 0);
CHECK("install_suspending", install_handler(SIGUSR1, suspending, &seen) == 0);
stop_worker = 0;
handled = 0;
pthread_create(&w, NULL, worker, NULL);
uint32_t v;
for (int i = 0; i < 300; i++) {
pthread_kill(w, SIGUSR1);
while (sem_wait(&parked) != 0) {}
ioctl(busy_obj, NT_EVENT_SET, &v);
wait_on(NT_WAIT_ANY, &busy_obj, 1, 12, 0, 0, NULL);
sem_post(&unpark);
}
stop_worker = 1;
pthread_join(w, NULL);
CHECK("suspend_ran", handled == 300);
close(busy_obj);
}
static volatile int burst_count;
static void bursting(int sig, siginfo_t *si, void *uc) {
(void)sig; (void)si; (void)uc;
uint32_t v = 0;
ioctl(busy_obj, NT_EVENT_SET, &v);
__atomic_add_fetch(&burst_count, 1, __ATOMIC_RELAXED);
}
static void bursts(void) {
void *seen = NULL;
busy_obj = event(0, 0);
for (int s = 0; s < 8; s++) CHECK("install_burst", install_handler(SIGRTMIN + s, bursting, &seen) == 0);
pthread_t w;
stop_worker = 0;
burst_count = 0;
pthread_create(&w, NULL, worker, NULL);
union sigval val = {0};
int sent = 0;
for (int i = 0; i < 400; i++) {
for (int s = 0; s < 8; s++)
if (pthread_sigqueue(w, SIGRTMIN + s, val) == 0) sent++;
usleep(200);
}
for (int i = 0; i < 2000 && __atomic_load_n(&burst_count, __ATOMIC_RELAXED) < sent; i++) usleep(1000);
stop_worker = 1;
pthread_join(w, NULL);
CHECK("burst_all_handled", burst_count == sent && sent > 0);
close(busy_obj);
}
static volatile int oneshot_count;
static void oneshot(int sig, siginfo_t *si, void *uc) {
(void)sig; (void)si; (void)uc;
__atomic_add_fetch(&oneshot_count, 1, __ATOMIC_RELAXED);
}
static void oneshots(void) {
busy_obj = event(0, 0);
pthread_t w;
stop_worker = 0;
oneshot_count = 0;
pthread_create(&w, NULL, worker, NULL);
int fired = 0;
for (int i = 0; i < 300; i++) {
if (install_oneshot(SIGWINCH, oneshot) != 0) break;
int before = __atomic_load_n(&oneshot_count, __ATOMIC_RELAXED);
pthread_kill(w, SIGWINCH);
fired++;
for (int j = 0; j < 2000 && __atomic_load_n(&oneshot_count, __ATOMIC_RELAXED) == before; j++) usleep(50);
}
stop_worker = 1;
pthread_join(w, NULL);
CHECK("oneshot_handlers_run", oneshot_count == fired && fired == 300);
close(busy_obj);
}
static int outer_obj, inner_obj, other_dev, other_obj;
static volatile int nested_ok, nested_done, outer_done;
static void nesting(int sig, siginfo_t *si, void *uc) {
(void)sig; (void)si; (void)uc;
uint32_t idx = 99;
int ok = wait_on(NT_WAIT_ANY, &inner_obj, 1, 21, 0, mono_ns(50), &idx) == 0 && idx == 0;
struct wait_args w = {mono_ns(2), (uintptr_t)&other_obj, 1, 0xdead, 0, 22, 0, 0};
ok = ok && ioctl(other_dev, NT_WAIT_ANY, &w) < 0 && errno == ETIMEDOUT;
nested_ok += ok;
__atomic_store_n(&nested_done, 1, __ATOMIC_RELEASE);
}
static void *outer_waiter(void *p) {
struct wait_args w = {mono_ns(3000), (uintptr_t)&outer_obj, 1, 99, 0, 23, 0, 0};
int r;
do { r = ioctl(dev, NT_WAIT_ANY, &w); } while (r < 0 && errno == EINTR);
*(int *)p = r == 0 && w.index == 0;
__atomic_store_n(&outer_done, 1, __ATOMIC_RELEASE);
return NULL;
}
static void nested_waits(void) {
void *seen = NULL;
other_dev = open("/dev/ntsync", O_RDWR | O_CLOEXEC);
CHECK("second_device", other_dev >= 0);
struct event_args ea = {0, 0};
other_obj = ioctl(other_dev, NT_CREATE_EVENT, &ea);
inner_obj = event(1, 1);
CHECK("install_nesting", install_handler(SIGUSR2, nesting, &seen) == 0);
int good = 0, early = 0;
nested_ok = 0;
for (int i = 0; i < 40; i++) {
outer_obj = event(0, 0);
int result = 0;
pthread_t t;
nested_done = outer_done = 0;
pthread_create(&t, NULL, outer_waiter, &result);
usleep(5000);
pthread_kill(t, SIGUSR2);
while (!__atomic_load_n(&nested_done, __ATOMIC_ACQUIRE)) usleep(200);
usleep(5000);
early += __atomic_load_n(&outer_done, __ATOMIC_ACQUIRE);
uint32_t v = 0;
ioctl(outer_obj, NT_EVENT_SET, &v);
pthread_join(t, NULL);
good += result;
close(outer_obj);
}
CHECK("outer_wait_not_woken_by_nested_wait", early == 0);
CHECK("outer_wait_survives_nested_waits", good == 40);
CHECK("nested_waits_complete", nested_ok == 40);
close(inner_obj);
close(other_obj);
close(other_dev);
}
int main(void) {
alarm(60);
dev = open("/dev/ntsync", O_RDWR | O_CLOEXEC);
if (dev < 0) {
printf("no device %d\n", errno);
return 1;
}
semaphores();
mutexes();
events();
any_order();
all_semantics();
cross_thread();
cross_process();
stress();
signals();
bursts();
oneshots();
nested_waits();
printf("done fails=%d\n", fails);
return fails != 0;
}
"""
class NtsyncPreloadTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
if not shutil.which("cc"):
raise unittest.SkipTest("no compiler")
cls.tmp = tempfile.TemporaryDirectory()
tmp = Path(cls.tmp.name)
(tmp / "ntdll.c").write_text(NTDLL)
(tmp / "prog.c").write_text(PROGRAM)
sources = sorted(str(p) for p in PRELOAD.glob("*.c"))
subprocess.run(["cc", "-shared", "-fPIC", "-O2", "-Wall", "-pthread", "-o", str(tmp / "libblsession.so"), *sources, "-ldl"],
check=True)
subprocess.run(["cc", "-shared", "-fPIC", "-O2", "-o", str(tmp / "ntdll.so"), str(tmp / "ntdll.c")], check=True)
subprocess.run(["cc", "-O1", "-pthread", "-o", str(tmp / "prog"), str(tmp / "prog.c"), str(tmp / "ntdll.so"),
"-Wl,-rpath," + str(tmp)], check=True)
cls.program = tmp / "prog"
cls.preload = tmp / "libblsession.so"
@classmethod
def tearDownClass(cls):
cls.tmp.cleanup()
def run_program(self):
env = dict(os.environ, LD_PRELOAD=str(self.preload), BL_SYNC="1")
return subprocess.run([str(self.program)], env=env, capture_output=True, text=True, timeout=90)
def test_emulation_matches_the_ntsync_driver(self):
out = self.run_program()
self.assertEqual(out.returncode, 0, out.stdout + out.stderr)
self.assertIn("done fails=0", out.stdout)
def test_repeated_runs_stay_consistent(self):
for _ in range(3):
out = self.run_program()
self.assertEqual(out.returncode, 0, out.stdout + out.stderr)
if __name__ == "__main__":
unittest.main()
+2
View File
@@ -66,6 +66,8 @@ static void send_toplevel_configure(struct surface *s) { (void)s; resizes++; }
#define pointer_button(...) ((void)0)
#define scroll_event(...) ((void)0)
#define axis_event(...) ((void)0)
#define stream_take(...) ((void)0)
#define stream_forget_all(...) ((void)0)
#define pointer_delta(...) ((void)0)
#define deliver_touch(m) ((void)0)
#define deliver_pointer(m) (pointers++)
+125
View File
@@ -0,0 +1,125 @@
import hashlib
import http.server
import json
import os
from pathlib import Path
import shutil
import socket
import subprocess
import tempfile
import threading
import unittest
ROOT = Path(__file__).resolve().parents[2]
FETCH = ROOT / 'tools/packs/fetch-verified.sh'
class PackDownloadsTest(unittest.TestCase):
def setUp(self):
directory = tempfile.TemporaryDirectory()
self.addCleanup(directory.cleanup)
self.root = Path(directory.name)
self.cache = self.root / 'cache'
self.cache.mkdir()
self.destination = self.root / 'download'
self.remote = self.root / 'remote'
self.remote.write_bytes(b'verified input')
self.checksum = hashlib.sha256(self.remote.read_bytes()).hexdigest()
self.log = self.root / 'curl.log'
tool = self.root / 'bin/curl'
tool.parent.mkdir()
tool.write_text('#!/usr/bin/env python3\nimport json,os,shutil,sys\n'
'args=sys.argv[1:]\n'
'with open(os.environ["CURL_LOG"],"a") as f: f.write(json.dumps(args)+"\\n")\n'
'shutil.copyfile(os.environ["REMOTE"],args[args.index("-o")+1])\n')
tool.chmod(0o755)
self.env = dict(os.environ, DROIDDECK_PACK_CACHE=str(self.cache), CURL_LOG=str(self.log),
REMOTE=str(self.remote), PATH=str(tool.parent) + os.pathsep + os.environ['PATH'])
def fetch(self, checksum=None, url='https://example.test/pinned', env=None):
return subprocess.run(['bash', str(FETCH), url, checksum or self.checksum, str(self.destination)],
env=env or self.env, capture_output=True, text=True)
def test_cold_download_then_warm_reuse_and_corruption_repair(self):
self.assertEqual(self.fetch().returncode, 0)
self.assertEqual(self.fetch().returncode, 0)
self.assertEqual(len(self.log.read_text().splitlines()), 1)
self.assertEqual(self.destination.read_bytes(), b'verified input')
(self.cache / self.checksum).write_bytes(b'corrupt')
self.assertEqual(self.fetch().returncode, 0)
self.assertEqual(len(self.log.read_text().splitlines()), 2)
self.assertEqual((self.cache / self.checksum).read_bytes(), b'verified input')
def test_directory_cache_entry_is_replaced_only_after_verification(self):
entry = self.cache / self.checksum
entry.mkdir()
sentinel = entry / 'old-entry'
sentinel.write_bytes(b'preserve until verified')
self.remote.write_bytes(b'tampered download')
self.assertNotEqual(self.fetch().returncode, 0)
self.assertEqual(sentinel.read_bytes(), b'preserve until verified')
self.assertFalse(self.destination.exists())
self.remote.write_bytes(b'verified input')
result = self.fetch()
self.assertEqual(result.returncode, 0, result.stderr)
self.assertTrue(entry.is_file())
self.assertEqual(entry.read_bytes(), b'verified input')
self.assertEqual(self.destination.read_bytes(), b'verified input')
self.assertEqual(list(self.cache.iterdir()), [entry])
self.assertEqual(self.fetch().returncode, 0)
self.assertEqual(len(self.log.read_text().splitlines()), 2)
def test_changed_pin_cannot_reuse_old_bytes(self):
self.assertEqual(self.fetch().returncode, 0)
self.remote.write_bytes(b'new approved input')
updated = hashlib.sha256(self.remote.read_bytes()).hexdigest()
self.assertEqual(self.fetch(updated).returncode, 0)
self.assertEqual(len(self.log.read_text().splitlines()), 2)
self.assertEqual(self.destination.read_bytes(), b'new approved input')
def test_bad_download_is_not_cached_or_written(self):
self.remote.write_bytes(b'tampered download')
self.assertNotEqual(self.fetch().returncode, 0)
self.assertFalse((self.cache / self.checksum).exists())
self.assertFalse(self.destination.exists())
self.assertEqual(list(self.cache.iterdir()), [])
arguments = json.loads(self.log.read_text())
self.assertIn('--retry-all-errors', arguments)
self.assertIn('--max-time', arguments)
self.assertIn('--retry-max-time', arguments)
def test_connection_reset_retries_then_verifies_real_download(self):
if not shutil.which('curl'):
self.skipTest('curl required for transport test')
content = self.remote.read_bytes()
attempts = []
class Handler(http.server.BaseHTTPRequestHandler):
def do_GET(self):
attempts.append(True)
if len(attempts) == 1:
self.connection.shutdown(socket.SHUT_RDWR)
self.connection.close()
return
self.send_response(200)
self.send_header('Content-Length', str(len(content)))
self.end_headers()
self.wfile.write(content)
def log_message(self, *args):
pass
server = http.server.ThreadingHTTPServer(('127.0.0.1', 0), Handler)
thread = threading.Thread(target=server.serve_forever, daemon=True)
thread.start()
try:
env = dict(self.env, PATH=os.environ['PATH'])
result = self.fetch(url=f'http://127.0.0.1:{server.server_port}/input', env=env)
self.assertEqual(result.returncode, 0, result.stderr)
self.assertGreaterEqual(len(attempts), 2)
self.assertEqual(self.destination.read_bytes(), content)
finally:
server.shutdown()
server.server_close()
thread.join()
+187
View File
@@ -0,0 +1,187 @@
import copy
import importlib.util
import io
import json
import os
from pathlib import Path
import subprocess
import tarfile
import tempfile
import unittest
from unittest.mock import patch
SPEC = importlib.util.spec_from_file_location('preload_snapshots', Path(__file__).resolve().parents[1] / 'components/preloads.py')
snapshots = importlib.util.module_from_spec(SPEC)
SPEC.loader.exec_module(snapshots)
def archive(files):
stream = io.BytesIO()
with tarfile.open(fileobj=stream, mode='w:gz') as tar:
for name, content in files.items():
entry = tarfile.TarInfo(name)
entry.size = len(content)
tar.addfile(entry, io.BytesIO(content))
return stream.getvalue()
class PreloadSnapshotTest(unittest.TestCase):
def setUp(self):
directory = tempfile.TemporaryDirectory()
self.addCleanup(directory.cleanup)
self.root = Path(directory.name)
# Real repository inputs exercise the transitive include walker and build recipe.
for group in snapshots.INPUTS:
for name in snapshots.source_files(group):
if name.startswith('@'):
continue
target = self.root / name
target.parent.mkdir(parents=True, exist_ok=True)
target.write_bytes((snapshots.ROOT / name).read_bytes())
recipe = 'tools/components/build-arm64-preloads.sh'
(self.root / recipe).parent.mkdir(parents=True, exist_ok=True)
(self.root / recipe).write_text((snapshots.ROOT / recipe).read_text())
x86 = 'tools/components/recipes/x86-preloads.sh'
(self.root / x86).parent.mkdir(parents=True, exist_ok=True)
(self.root / x86).write_text((snapshots.ROOT / x86).read_text())
(self.root / '.github/workflows').mkdir(parents=True)
(self.root / '.github/workflows/build.yml').write_text((snapshots.ROOT / '.github/workflows/build.yml').read_text())
self.git('init', '-q')
self.git('config', 'core.autocrlf', 'false')
self.git('config', 'user.name', 'Test Developer')
self.git('config', 'user.email', 'test@example.test')
self.git('add', '.')
self.binaries = {name: ('verified-' + name).encode() for name in snapshots.FILES}
self.content = archive(self.binaries)
self.data = dict(version=1, origin=dict(run=123, commit='abc', artifact=456, artifact_sha256='0' * 64),
groups={g: snapshots.source_files(g, self.root) for g in snapshots.INPUTS},
arm64_recipe=snapshots.recipe(self.root), x86_recipe=snapshots.x86_recipe(self.root),
archive_sha256=snapshots.sha(self.content),
outputs={name: snapshots.sha(value) for name, value in self.binaries.items()})
(self.root / snapshots.LOCK).write_text(json.dumps(self.data))
self.git('add', '.')
self.git('commit', '-qm', 'Approved baseline')
self.base = self.git('rev-parse', 'HEAD').strip()
def git(self, *args):
return subprocess.check_output(['git', *args], cwd=self.root, text=True)
def restore(self, base=None):
with patch.dict(os.environ, COMPONENT_TRUSTED_BASE=base or self.base, FORCE_PRELOAD_REBUILD='false'), \
patch.object(snapshots.urllib.request, 'urlopen', return_value=io.BytesIO(self.content)) as fetch, \
patch.object(snapshots, 'prove_origin') as prove:
result = snapshots.restore(self.root)
return result, fetch, prove
def test_reviewed_unchanged_binaries_reused_and_written(self):
result, fetch, prove = self.restore()
self.assertEqual(result, dict(arm64=True, x86=True))
prove.assert_not_called()
fetch.assert_called_once()
for name, value in self.binaries.items():
self.assertEqual((self.root / 'app/src/main/assets/linuxfs' / name).read_bytes(), value)
def test_changed_common_source_requires_both_rebuilds(self):
path = self.root / snapshots.COMMON[0]
path.write_text(path.read_text() + '\n// Changed source\n')
result, fetch, prove = self.restore()
self.assertEqual(result, dict(arm64=False, x86=False))
fetch.assert_not_called()
prove.assert_not_called()
def test_new_transitive_header_is_an_input(self):
source = self.root / snapshots.COMMON[0]
source.write_text(source.read_text() + '\n#include "new-header.h"\n')
header = source.with_name('new-header.h')
header.write_text('#define VALUE 1\n')
before = snapshots.source_files('arm64', self.root)
header.write_text('#define VALUE 2\n')
self.assertNotEqual(before, snapshots.source_files('arm64', self.root))
def test_changed_recipe_rebuilds_arm64_only(self):
script = self.root / 'tools/components/build-arm64-preloads.sh'
script.write_text(script.read_text().replace('-O2', '-O3'))
result, _, _ = self.restore()
self.assertEqual(result, dict(arm64=False, x86=True))
def test_shell_newline_changes_invalidate_the_recipe(self):
script = self.root / 'tools/components/build-arm64-preloads.sh'
original = snapshots.recipe(self.root)
text = script.read_text()
script.write_text(text.replace('-ldl\n', '-ldl ', 1))
self.assertNotEqual(original, snapshots.recipe(self.root))
def test_x86_container_recipe_changes_rebuild_x86_only(self):
script = self.root / 'tools/components/recipes/x86-preloads.sh'
script.write_text(script.read_text().replace('debian:bullseye', 'debian:bookworm'))
result, _, _ = self.restore()
self.assertEqual(result, dict(arm64=True, x86=False))
def test_new_manifest_requires_main_build_proof(self):
result, _, prove = self.restore('HEAD~1')
self.assertTrue(all(result.values()))
prove.assert_called_once()
def test_forged_binary_hash_cannot_prove_main_origin(self):
data = copy.deepcopy(self.data)
data['outputs']['libblsession.so'] = 'f' * 64
with patch.object(snapshots, 'artifact_bytes', return_value=self.binaries):
with self.assertRaisesRegex(ValueError, 'original main APK'):
snapshots.prove_origin(data, self.root / 'cache', self.root)
def test_modified_input_policy_cannot_omit_producer_dependencies(self):
data = copy.deepcopy(self.data)
data['origin']['commit'] = self.base
omitted = snapshots.COMMON[0]
del data['groups']['arm64'][omitted]
with patch.object(snapshots, 'artifact_bytes', return_value=self.binaries), \
patch.object(snapshots, 'run', return_value=b''), \
patch.object(snapshots, 'source_files', side_effect=lambda group, *args: data['groups'][group]), \
patch.object(snapshots, 'producer_sources', return_value={omitted: self.data['groups']['arm64'][omitted]}):
with self.assertRaisesRegex(ValueError, 'producing build dependency'):
snapshots.prove_origin(data, self.root / 'cache', self.root)
def test_non_main_or_unsuccessful_run_cannot_prove_origin(self):
build = dict(event='pull_request', head_branch='main', conclusion='success', head_sha='abc',
path='.github/workflows/build.yml', repository=dict(full_name=snapshots.REPO))
for field, value in [('event', 'pull_request'), ('head_branch', 'feature'), ('conclusion', 'failure')]:
candidate = dict(build, event='push')
candidate[field] = value
with self.subTest(field=field), patch.object(snapshots, 'api', return_value=candidate):
with self.assertRaisesRegex(ValueError, 'successful main'):
snapshots.artifact_bytes(self.data['origin'], self.root / 'cache')
def test_corrupt_archive_is_rejected(self):
with self.assertRaisesRegex(ValueError, 'checksum'):
snapshots.unpack(self.content + b'tampered', self.data)
def test_missing_duplicate_or_traversal_entries_are_rejected(self):
for files in ({}, {'../escaped': b'bad'}, {'libblsession.so': b'wrong'}):
content = archive(files)
data = dict(self.data, archive_sha256=snapshots.sha(content))
with self.subTest(names=list(files)), self.assertRaises(ValueError):
snapshots.unpack(content, data)
def test_missing_release_rebuilds_without_accepting_unverified_bytes(self):
error = snapshots.urllib.error.HTTPError('https://example.test', 404, 'missing', {}, None)
with patch.dict(os.environ, COMPONENT_TRUSTED_BASE=self.base), \
patch.object(snapshots.urllib.request, 'urlopen', side_effect=error):
self.assertEqual(snapshots.restore(self.root), dict(arm64=False, x86=False))
def test_transient_download_failure_rebuilds_but_integrity_errors_fail(self):
errors = [TimeoutError('network'), snapshots.http.client.IncompleteRead(b'partial'),
snapshots.urllib.error.HTTPError('https://example.test', 503, 'unavailable', {}, None)]
for error in errors:
with self.subTest(error=type(error).__name__), patch.dict(os.environ, COMPONENT_TRUSTED_BASE=self.base), \
patch.object(snapshots.urllib.request, 'urlopen', side_effect=error):
self.assertEqual(snapshots.restore(self.root), dict(arm64=False, x86=False))
with patch.dict(os.environ, COMPONENT_TRUSTED_BASE=self.base), \
patch.object(snapshots.urllib.request, 'urlopen', return_value=io.BytesIO(b'corrupt')):
with self.assertRaisesRegex(ValueError, 'checksum'):
snapshots.restore(self.root)
def test_manual_rebuild_does_not_download_snapshot(self):
with patch.dict(os.environ, FORCE_PRELOAD_REBUILD='true'), \
patch.object(snapshots.urllib.request, 'urlopen') as fetch:
self.assertEqual(snapshots.restore(self.root), dict(arm64=False, x86=False))
fetch.assert_not_called()
+48
View File
@@ -45,6 +45,26 @@ VULKAN_SCRIPT = r'''"InstallScript"
'''
UBISOFT_SCRIPT = r'''"installscript"
{
"Run Process"
{
"Ubisoft Connect PC Client"
{
"process 1" "%INSTALLDIR%\\UbisoftConnectInstaller.exe"
"command 1" "/S"
}
"easyanticheat"
{
"HasRunKey" "HKEY_CURRENT_USER\\SOFTWARE\\Ubisoft\\WatchDogs2EAC"
"process 1" "%INSTALLDIR%\\EasyAntiCheat\\InstallWD2EAC.cmd"
"command 1" ""
}
}
}
'''
class SeedRedistsTest(unittest.TestCase):
def setUp(self):
self.tmp = Path(tempfile.mkdtemp())
@@ -353,5 +373,33 @@ class SeedRedistsTest(unittest.TestCase):
self.assertEqual(self.reg.read_text().count('"Vulkan111080"'), 2)
def ubisoft(self):
script = self.tmp / "legacycompat/evaluatorscript_447040.vdf"
script.parent.mkdir(parents=True)
script.write_text('"evaluatorscript"\n{\n\t"appid"\t\t"447040"\n\t"1"\n'
+ "\n".join("\t" + line for line in UBISOFT_SCRIPT.splitlines()) + "\n}\n")
env = dict(os.environ, DROIDDECK_LIBRARIES="", DROIDDECK_SEED_SCRIPTS=str(script))
return subprocess.run(["bash", str(SCRIPT), str(self.steam)], env=env, capture_output=True, text=True, check=True)
def test_the_ubisoft_connect_installer_is_left_to_run(self):
self.ubisoft()
self.assertIn('"easyanticheat"=dword:00000001', self.user.read_text())
self.assertNotIn("Ubisoft Connect PC Client", self.reg.read_text())
self.assertNotIn("Ubisoft Connect PC Client", (self.reg.parent / ".droiddeck-redists").read_text())
def test_a_ubisoft_connect_marker_an_earlier_seeding_wrote_comes_out(self):
marker = "HKEY_LOCAL_MACHINE\\\\Software\\\\Valve\\\\Steam\\\\Apps\\\\447040\tUbisoft Connect PC Client"
self.reg.write_text(
"WINE REGISTRY Version 2\n"
"\n[Software\\\\Valve\\\\Steam\\\\Apps\\\\447040] 1791729070\n"
'"Ubisoft Connect PC Client"=dword:00000001\n'
"\n[Software\\\\Wow6432Node\\\\Valve\\\\Steam\\\\Apps\\\\447040] 1791729070\n"
'"Ubisoft Connect PC Client"=dword:00000001\n')
(self.reg.parent / ".droiddeck-redists").write_text(marker + "\n")
result = self.ubisoft()
self.assertNotIn("Ubisoft Connect PC Client", self.reg.read_text())
self.assertNotIn("Ubisoft Connect PC Client", (self.reg.parent / ".droiddeck-redists").read_text())
self.assertIn("Ubisoft Connect markers out", result.stdout)
if __name__ == "__main__":
unittest.main()
+15 -7
View File
@@ -1,4 +1,4 @@
"""The XTest cursor path is enabled only for Steam sessions."""
"""Gamescope composites the cursor only when a Steam session asks for it."""
import os
from pathlib import Path
import re
@@ -8,29 +8,37 @@ import unittest
SESSION = Path(__file__).resolve().parents[1] / 'linuxfs/overlay/usr/local/bin/droiddeck-session'
SOURCE = SESSION.read_text()
MATCH = re.search(
r'^ if \[ "\$mode" = steam \]; then\n.*?^ fi\n', SOURCE, re.M | re.S)
r'^ if \[ "\$mode" = steam \] && \[ "\$\{BL_CURSOR_COMPOSITE:-0\}" = 1 \]; then\n.*?^ fi\n',
SOURCE, re.M | re.S)
assert MATCH is not None
CURSOR_SETUP = MATCH.group(0)
class SessionCursorRoutingTest(unittest.TestCase):
def run_setup(self, mode, initial=None):
def run_setup(self, mode, initial=None, composite=None):
env = dict(os.environ, mode=mode)
if initial is not None:
env['gamescope_cursor_composite'] = initial
else:
env.pop('gamescope_cursor_composite', None)
if composite is not None:
env['BL_CURSOR_COMPOSITE'] = composite
else:
env.pop('BL_CURSOR_COMPOSITE', None)
result = subprocess.run(
['bash', '-c', CURSOR_SETUP + 'printf "%s\n" "${gamescope_cursor_composite-}"'],
env=env, text=True, capture_output=True, check=True)
return result.stdout.strip()
def test_steam_enables_manual_composition_for_the_nested_cursor(self):
self.assertEqual('2', self.run_setup('steam'))
def test_steam_leaves_the_cursor_with_the_host_unless_asked(self):
self.assertEqual('', self.run_setup('steam'))
def test_steam_composites_the_cursor_when_the_setting_is_on(self):
self.assertEqual('2', self.run_setup('steam', composite='1'))
def test_other_sessions_keep_their_existing_cursor_composition(self):
self.assertEqual('', self.run_setup('desktop'))
self.assertEqual('1', self.run_setup('run', '1'))
self.assertEqual('', self.run_setup('desktop', composite='1'))
self.assertEqual('1', self.run_setup('run', '1', composite='1'))
if __name__ == '__main__':
@@ -45,6 +45,30 @@ class SteamCompatEnvironmentTest(unittest.TestCase):
client_path += ':' + inherited['LD_LIBRARY_PATH']
self.assertEqual(result.stdout.splitlines(), ['1', client_path])
def test_session_exit_stops_the_library_helper(self):
source = (BIN / 'droiddeck-session').read_text()
launch = next(line for line in source.splitlines()
if 'droiddeck-steam-compat "$steam_root"' in line)
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
pid = root / 'pid'
helper = root / 'droiddeck-steam-compat'
helper.write_text('#!/bin/sh\necho $$ > "$PID_FILE"\nexec sleep 30\n')
helper.chmod(0o755)
env = dict(os.environ, PATH=str(root) + os.pathsep + os.environ['PATH'],
PID_FILE=str(pid), steam_root='/steam', steam_x64='0')
subprocess.run(['bash', '-c', '_bl_compat_env=(env)\n' + launch + '\n'
'until [ -s "$PID_FILE" ]; do sleep 0.05; done\n'
'kill $(jobs -p)\nwait'],
env=env, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=10)
helper_pid = int(pid.read_text())
try:
os.kill(helper_pid, 0)
except ProcessLookupError:
return
os.kill(helper_pid, 9)
self.fail('droiddeck-steam-compat outlived the session')
if __name__ == '__main__':
unittest.main()
+108
View File
@@ -641,6 +641,114 @@ class ReconcileTest(unittest.TestCase):
self.assertEqual(page.mapping["10"], "GE-Proton9-1")
self.assertEqual(state["auto"], {"11": TOOL})
def games(self):
return json.loads((self.state / "games.json").read_text())["games"]
def write_games(self, games):
(self.state / "games.json").write_text(json.dumps({"version": 1, "games": games}))
def test_a_steam_pick_after_the_app_choice_is_kept_live(self):
page = FakePage({"0": TOOL, "10": TOOL, "11": TOOL}, [TOOL, TOOL_11, COMPAT["FEX_TOOL"]])
self.write_games({"10": {"valve": True, "dir": "Proton 11.0 (ARM64)"}})
state = self.reconcile(page)
self.assertEqual(page.sets, [("10", TOOL_11)])
self.assertEqual(state["games_applied"], {"10": {"valve": True, "dir": "Proton 11.0 (ARM64)"}})
page.sets.clear()
page.mapping["10"] = COMPAT["FEX_TOOL"]
state = self.reconcile(page)
self.assertEqual(page.sets, [])
self.assertEqual(page.mapping["10"], COMPAT["FEX_TOOL"])
self.assertEqual(self.games(), {})
self.assertEqual(state["games_applied"], {})
self.write_games({"10": {"valve": True, "dir": "Proton 11.0 (ARM64)"}})
self.reconcile(page)
self.assertEqual(page.sets, [("10", TOOL_11)])
def test_a_steam_pick_of_our_proton_becomes_the_app_choice_live(self):
page = FakePage({"0": TOOL, "10": TOOL, "11": TOOL}, [TOOL, TOOL_11])
self.write_games({"10": {"valve": True, "dir": "Proton 11.0 (ARM64)"}})
self.reconcile(page)
page.sets.clear()
page.mapping["10"] = TOOL
state = self.reconcile(page)
self.assertEqual(page.sets, [])
self.assertEqual(self.games(), {"10": {"valve": True, "dir": "Proton Experimental (ARM64)"}})
self.assertEqual(state["games_applied"], self.games())
def test_use_the_default_applies_once_then_steam_picks_stand_live(self):
page = FakePage({"0": TOOL, "10": TOOL_11, "11": TOOL}, [TOOL, TOOL_11, COMPAT["FEX_TOOL"]])
self.write_games({"10": None})
self.reconcile(page)
self.assertEqual(page.sets, [("10", TOOL)])
page.sets.clear()
self.reconcile(page)
self.assertEqual(self.games(), {})
page.mapping["10"] = COMPAT["FEX_TOOL"]
self.reconcile(page)
self.assertEqual(page.sets, [])
self.assertEqual(page.mapping["10"], COMPAT["FEX_TOOL"])
def test_choices_an_earlier_build_applied_give_way_to_steam_picks_live(self):
(self.state / "state.json").write_text(json.dumps({"default": TOOL, "auto": {"11": TOOL}, "tools": []}))
self.write_games({"10": None, "11": {"valve": True, "dir": "Proton 11.0 (ARM64)"}})
page = FakePage({"0": TOOL, "10": COMPAT["FEX_TOOL"], "11": "steamlinuxruntime_sniper"},
[TOOL, TOOL_11, COMPAT["FEX_TOOL"]])
self.reconcile(page)
self.assertEqual(page.sets, [])
self.assertEqual(self.games(), {})
def test_an_app_choice_written_during_a_sync_is_not_overwritten(self):
self.write_games({"10": {"valve": True, "dir": "Proton 11.0 (ARM64)"}})
requests, text = COMPAT["read_game_requests"]()
self.write_games({"10": {"valve": True, "dir": "Proton Experimental (ARM64)"}})
self.assertFalse(COMPAT["write_game_requests"]({}, text))
self.assertEqual(self.games(), {"10": {"valve": True, "dir": "Proton Experimental (ARM64)"}})
def test_an_app_the_client_briefly_refuses_keeps_its_applied_choice_live(self):
page = FakePage({"0": TOOL, "10": TOOL, "11": TOOL}, [TOOL, TOOL_11, COMPAT["FEX_TOOL"]])
self.write_games({"10": {"valve": True, "dir": "Proton 11.0 (ARM64)"}})
self.reconcile(page)
page.refused = {"10"}
state = self.reconcile(page)
self.assertEqual(state["games_applied"], {"10": {"valve": True, "dir": "Proton 11.0 (ARM64)"}})
page.refused = set()
page.sets.clear()
page.mapping["10"] = COMPAT["FEX_TOOL"]
self.reconcile(page)
self.assertEqual(page.sets, [])
self.assertEqual(page.mapping["10"], COMPAT["FEX_TOOL"])
def test_an_app_rewriting_its_choices_during_both_tries_applies_none_of_them_live(self):
page = FakePage({"0": TOOL, "10": TOOL, "11": TOOL}, [TOOL, TOOL_11, COMPAT["FEX_TOOL"]])
self.write_games({"10": {"valve": True, "dir": "Proton 11.0 (ARM64)"}})
self.reconcile(page)
page.sets.clear()
page.mapping["10"] = COMPAT["FEX_TOOL"]
self.write_games({"10": {"valve": True, "dir": "Proton 11.0 (ARM64)"}, "11": None})
with mock.patch.dict(COMPAT["settled_game_requests"].__globals__, write_game_requests=lambda *_: False):
state = self.reconcile(page)
self.assertEqual(page.sets, [])
self.assertEqual(state["games_applied"], {"10": {"valve": True, "dir": "Proton 11.0 (ARM64)"}})
def test_a_steam_pick_is_kept_at_the_next_start(self):
config = self.steam / "config/config.vdf"
config.parent.mkdir(parents=True)
vdf = '"InstallConfigStore" { "Software" { "Valve" { "Steam" { "CompatToolMapping" { %s } } } } }'
entry = '"%s" { "name" "%s" "config" "" "priority" "250" }'
catalog = COMPAT["tool_catalog"](str(self.steam))
self.write_games({"10": {"valve": True, "dir": "Proton 11.0 (ARM64)"}})
config.write_text(vdf % (entry % ("0", TOOL) + entry % ("10", TOOL)))
with mock.patch.dict(COMPAT["register_default"].__globals__, steam_running=lambda: False):
COMPAT["register_default"](str(config), ["10"], catalog=catalog, directory=str(self.state), settle=True)
self.assertIn(entry % ("10", TOOL_11), " ".join(config.read_text().split()))
config.write_text(vdf % (entry % ("0", TOOL) + entry % ("10", COMPAT["FEX_TOOL"])))
COMPAT["register_default"](str(config), ["10"], catalog=catalog, directory=str(self.state), settle=True)
self.assertIn(entry % ("10", COMPAT["FEX_TOOL"]), " ".join(config.read_text().split()))
self.assertEqual(self.games(), {})
def test_file_mode_defers_to_the_live_helper(self):
(self.state / "live.pid").write_text("%d\n" % os.getpid())
self.assertTrue(COMPAT["live_helper_running"]())
+9
View File
@@ -42,6 +42,15 @@ class DesktopSteamLaunchTest(unittest.TestCase):
self.assertEqual(result.stdout.splitlines(), ['1', 'desktop', ':7', 'steam', 'steam://open/library'])
self.assertFalse((self.session / 'steam-launch').exists())
def test_desktop_games_present_without_dri3(self):
(self.root / 'supervisor').write_text('#!/bin/bash\nprintf "%s\\n" "$MESA_VK_WSI_DEBUG"\n')
self.env.pop('MESA_VK_WSI_DEBUG', None)
self.assertEqual(self.launch('desktop', '945360').stdout.splitlines(), ['sw'])
self.env['MESA_VK_WSI_DEBUG'] = 'noshm'
self.assertEqual(self.launch('desktop', '945360').stdout.splitlines(), ['noshm,sw'])
self.env['MESA_VK_WSI_DEBUG'] = 'sw,noshm'
self.assertEqual(self.launch('desktop', '945360').stdout.splitlines(), ['sw,noshm'])
def test_desktop_game_url_goes_to_client(self):
result = self.launch('desktop', '501590')
self.assertEqual(result.returncode, 0, result.stderr)
+142
View File
@@ -0,0 +1,142 @@
import io
import json
import os
import runpy
import subprocess
import sys
import tempfile
import unittest
from pathlib import Path
from unittest import mock
BIN = Path(__file__).resolve().parents[1] / "linuxfs/overlay/usr/local/bin"
LIVE = runpy.run_path(str(BIN / "droiddeck-steam-compat"))
HARNESS = r"""
const assert = require('assert');
globalThis.window = globalThis;
const native = () => Promise.reject({ result: 2, message: '' });
const popupCallbacks = [];
const popup = { window: { SteamClient: { System: { OpenFileDialog: native } } } };
const later = { window: { SteamClient: { System: { OpenFileDialog: native } } } };
globalThis.SteamClient = { System: { OpenFileDialog: native } };
window.g_PopupManager = { GetPopups: () => [popup], AddPopupCreatedCallback: cb => popupCallbacks.push(cb) };
const asked = [];
window.droiddeckOpenFileDialog = payload => asked.push(JSON.parse(payload));
const script = require('fs').readFileSync(process.argv[2], 'utf8');
(async () => {
assert.strictEqual(eval(script), 'installed');
const orphan = SteamClient.System.OpenFileDialog({ strTitle: 'Before reconnect' });
assert.strictEqual(eval(script), 'present');
await assert.rejects(orphan, e => e.result === 2);
assert.strictEqual(window.__droiddeckFiles.done(1, '/late'), false);
asked.length = 0;
assert.notStrictEqual(SteamClient.System.OpenFileDialog, native);
assert.strictEqual(popup.window.SteamClient.System.OpenFileDialog, SteamClient.System.OpenFileDialog);
popupCallbacks.forEach(cb => cb(later));
assert.strictEqual(later.window.SteamClient.System.OpenFileDialog, SteamClient.System.OpenFileDialog);
const options = { strTitle: 'Pick', bChooseDirectory: true };
const chosen = SteamClient.System.OpenFileDialog(options);
const cancelled = later.window.SteamClient.System.OpenFileDialog({ strTitle: 'Other' });
assert.deepStrictEqual(asked, [{ id: 2, options }, { id: 3, options: { strTitle: 'Other' } }]);
assert.strictEqual(window.__droiddeckFiles.done(2, '/root/Storage/Games'), true);
assert.strictEqual(window.__droiddeckFiles.done(2, '/again'), false);
assert.strictEqual(await chosen, '/root/Storage/Games');
window.__droiddeckFiles.done(3, null);
await assert.rejects(cancelled, e => e.result === 2);
delete window.droiddeckOpenFileDialog;
await assert.rejects(SteamClient.System.OpenFileDialog({}), e => e.result === 2);
})().catch(e => { console.error(e); process.exit(1); });
"""
def bound(payload):
return {"method": "Runtime.bindingCalled", "params": {"name": LIVE["FILE_BINDING"], "payload": payload}}
class FileDialogScriptTest(unittest.TestCase):
def test_every_window_asks_the_bridge_and_gets_the_path_or_a_cancel(self):
with tempfile.TemporaryDirectory() as tmp:
script = Path(tmp) / "files.js"
script.write_text(LIVE["FILE_JS"])
harness = Path(tmp) / "harness.js"
harness.write_text(HARNESS)
result = subprocess.run(["node", str(harness), str(script)], capture_output=True, text=True, timeout=20)
self.assertEqual(result.returncode, 0, result.stderr)
class FileDialogBridgeTest(unittest.TestCase):
def setUp(self):
self.globals = LIVE["Helper"].attach.__globals__
self.tmp = tempfile.TemporaryDirectory()
self.addCleanup(self.tmp.cleanup)
self.root = Path(self.tmp.name)
self.helper = LIVE["Helper"](str(self.root), 1)
self.helper.page = mock.Mock()
def answer(self):
return self.helper.page.evaluate.call_args.args[0]
def test_the_chooser_runs_under_a_steam_reaper_with_the_options_on_stdin(self):
reaper = self.root / "steamrtarm64/reaper"
reaper.parent.mkdir()
reaper.write_text("#!/bin/sh\n")
reaper.chmod(0o755)
proc = mock.Mock(stdin=io.BytesIO())
proc.stdin.close = lambda: None
with mock.patch.object(self.globals["subprocess"], "Popen", return_value=proc) as popen:
self.helper.page.drain.return_value = [bound(json.dumps({"id": 3, "options": {"strTitle": "Pick"}}))]
self.helper.drain()
command = popen.call_args.args[0]
self.assertEqual(command[:4], [str(reaper), "SteamLaunch", "AppId=769", "--"])
self.assertEqual(command[-1], "--choose-file")
self.assertEqual(json.loads(proc.stdin.getvalue()), {"strTitle": "Pick"})
self.assertEqual(self.helper.chooser, (3, proc))
def test_a_second_request_while_one_is_open_is_cancelled(self):
first = mock.Mock()
self.helper.chooser = (1, first)
self.helper.open_chooser(json.dumps({"id": 2, "options": {}}))
self.assertEqual(self.answer(), "window.__droiddeckFiles?.done(2, null)")
self.assertEqual(self.helper.chooser, (1, first))
def test_the_chosen_path_or_a_cancel_goes_back_to_the_page(self):
for code, out, expected in ((0, b'"/root/Storage/My Game/game.sh"\n', '"/root/Storage/My Game/game.sh"'),
(0, b'" spaced name "\n', '" spaced name "'),
(1, b"", "null"), (2, b"", "null"), (0, b"\n", "null"), (0, b"not json\n", "null")):
proc = mock.Mock(stdout=io.BytesIO(out), returncode=code)
self.helper.chooser = (7, proc)
self.helper.chooser_done()
self.assertEqual(self.answer(), "window.__droiddeckFiles?.done(7, %s)" % expected)
self.assertIsNone(self.helper.chooser)
def test_bad_requests_are_ignored(self):
with mock.patch.object(self.globals["subprocess"], "Popen") as popen:
for payload in ("not json", json.dumps({"options": {}}), json.dumps({"id": 1, "options": []})):
self.helper.open_chooser(payload)
popen.assert_not_called()
self.helper.page.evaluate.assert_not_called()
def test_choose_file_mode_prints_the_path_and_fails_on_cancel(self):
for chooser, code, printed in ((mock.Mock(return_value="/root/Storage/a.exe "), 0, '"/root/Storage/a.exe "\n'),
(mock.Mock(return_value=None), 1, ""),
(mock.Mock(side_effect=ValueError("Namespace Gtk not available for version 3.0")), 2, ""),
(mock.Mock(side_effect=ImportError("No module named 'gi'")), 2, "")):
with mock.patch.dict(LIVE["chooser_main"].__globals__, {"choose_file": chooser}), \
mock.patch.object(sys, "stdin", io.StringIO('{"strTitle": "Pick"}')), \
mock.patch.object(sys, "stdout", io.StringIO()) as out:
self.assertEqual(LIVE["chooser_main"](), code)
self.assertEqual(out.getvalue(), printed)
def test_attach_installs_the_file_dialog_bridge(self):
page = mock.Mock()
page.evaluate.return_value = True
with mock.patch.dict(self.globals, {"shared_context": lambda port: "/devtools/page/1",
"Page": mock.Mock(return_value=page)}):
self.helper.attach()
page.call.assert_any_call("Runtime.addBinding", {"name": LIVE["FILE_BINDING"]})
page.evaluate.assert_any_call(LIVE["FILE_JS"])
if __name__ == "__main__":
unittest.main()
+262 -23
View File
@@ -26,25 +26,48 @@ class SteamInstallTest(unittest.TestCase):
self.bin = self.root / 'bin'
self.bin.mkdir()
curl = self.bin / 'curl'
curl.write_text('''#!/bin/bash
set -eu
url=""
out=""
while [ $# -gt 0 ]; do
case $1 in
-o) out=$2; shift 2 ;;
--connect-timeout|--max-time|--speed-limit|--speed-time|--retry|--retry-delay) shift 2 ;;
-*) shift ;;
*) url=$1; shift ;;
esac
done
printf '%s\\n' "$url" >> "$TEST_REQUESTS"
if [ -n "${TEST_FAIL_HOST:-}" ]; then
case $url in *"$TEST_FAIL_HOST"*) exit 22 ;; esac
fi
cp "$TEST_CDN/${url##*/}" "$out"
curl.write_text('''#!/usr/bin/env python3
import os, sys
args = sys.argv[1:]
url = next(a for a in args if a.startswith('https://'))
out = args[args.index('-o') + 1]
name = url.rsplit('/', 1)[1]
offset = os.path.getsize(out) if '-C' in args and os.path.exists(out) else 0
with open(os.environ['TEST_REQUESTS'], 'a') as log:
log.write(url + '\\n')
with open(os.environ['TEST_REQUESTS'] + '.offsets', 'a') as log:
log.write(f'{name} {offset}\\n')
def done(status, code='200'):
if '-w' in args:
sys.stdout.write(code)
sys.exit(status)
host_fail = os.environ.get('TEST_FAIL_HOST')
if host_fail and host_fail in url:
done(22, '000')
action = []
plan = os.environ.get('TEST_PLAN')
if plan and os.path.exists(plan):
lines = open(plan).read().splitlines()
for n, line in enumerate(lines):
if line.split()[0] == name:
action = line.split()[1:]
del lines[n]
open(plan, 'w').write(''.join(l + '\\n' for l in lines))
break
if action[:1] == ['fail']:
done(int(action[1]), '000')
if action[:1] == ['http']:
done(22, action[1])
if action[:1] == ['norange'] and offset:
done(33)
data = open(os.path.join(os.environ['TEST_CDN'], name), 'rb').read()
end = int(action[1]) if action[:1] == ['cut'] else len(data)
with open(out, 'ab' if offset else 'wb') as target:
target.write(data[offset:end])
done(28 if end < len(data) else 0)
''')
curl.chmod(0o755)
self.plan = self.root / 'plan'
if shutil.which('sha256sum') is None:
sha = self.bin / 'sha256sum'
sha.write_text('#!/bin/bash\nexec shasum -a 256 "$@"\n')
@@ -55,8 +78,9 @@ cp "$TEST_CDN/${url##*/}" "$out"
sha1.chmod(0o755)
self.env = dict(os.environ, HOME=str(self.home), XDG_DATA_HOME=str(self.data),
PATH=str(self.bin) + os.pathsep + os.environ['PATH'],
TEST_CDN=str(self.cdn), TEST_REQUESTS=str(self.requests))
for channel in ('steamdeck_publicbeta',):
TEST_CDN=str(self.cdn), TEST_REQUESTS=str(self.requests),
TEST_PLAN=str(self.plan), BL_STEAM_RETRY_DELAYS='0 0 0')
for channel in ('steamdeck_publicbeta', 'steamdeck_stable'):
archive = self.cdn / (channel + '.zip')
with zipfile.ZipFile(archive, 'w') as package:
package.writestr('steamrtarm64/steam', channel)
@@ -68,12 +92,17 @@ cp "$TEST_CDN/${url##*/}" "$out"
f'\t\t"file" "{archive.name}"\n\t\t"sha2" "{digest}"\n'
'\t}\n}\n')
def install(self, fail_host=None):
def install(self, fail_host=None, *args, **extra):
env = self.env if fail_host is None else dict(self.env, TEST_FAIL_HOST=fail_host)
return subprocess.run(['bash', str(SCRIPT)], env=env, text=True, capture_output=True)
return subprocess.run(['bash', str(SCRIPT), *args], env=dict(env, **extra), text=True, capture_output=True)
def assert_installed(self, channel='steamdeck_publicbeta', fail_host=None, expected=None):
result = self.install(fail_host)
def channel(self, **extra):
result = self.install(None, '--channel', **extra)
self.assertEqual(0, result.returncode, result.stderr)
return result.stdout.strip()
def assert_installed(self, channel='steamdeck_publicbeta', fail_host=None, expected=None, **extra):
result = self.install(fail_host, **extra)
self.assertEqual(0, result.returncode, result.stderr)
self.assertEqual(channel + '\n', (self.steam / 'package/beta').read_text())
self.assertEqual('123456\n', (self.steam / 'package/droiddeck-installed').read_text())
@@ -87,6 +116,7 @@ cp "$TEST_CDN/${url##*/}" "$out"
'https://client-update.steamstatic.com/' + channel + '.zip',
]
self.assertEqual(expected, self.requests.read_text().splitlines())
return result
def test_downloads_the_deck_beta_client(self):
self.assert_installed()
@@ -155,8 +185,80 @@ cp "$TEST_CDN/${url##*/}" "$out"
self.requests.unlink()
with (package / 'droiddeck-unconfirmed').open('a') as marker:
marker.write('start\nstart\nstart\n')
self.assertEqual(0, self.install(BL_STEAM_CHANNEL='beta').returncode)
self.assertFalse(self.requests.exists())
def test_the_default_channel_is_the_deck_beta(self):
self.assertEqual('steamdeck_publicbeta', self.channel())
def stable_requests(self, beta_manifest=True):
stable = [
'https://client-update.steamstatic.com/steam_client_steamdeck_stable_linuxarm64',
'https://client-update.steamstatic.com/steamdeck_stable.zip',
]
if beta_manifest:
stable.insert(0, 'https://client-update.steamstatic.com/steam_client_steamdeck_publicbeta_linuxarm64')
return stable
def test_a_repaired_beta_client_that_still_shows_nothing_falls_back_to_stable(self):
package = self.break_client(2)
self.assertEqual(0, self.install().returncode)
self.requests.unlink()
with (package / 'droiddeck-unconfirmed').open('a') as marker:
marker.write('start\nstart\n')
result = self.assert_installed('steamdeck_stable', expected=self.stable_requests(False))
self.assertIn("falling back to Valve's steamdeck_stable Steam client", result.stdout)
self.assertEqual('steamdeck_stable\n', (package / 'droiddeck-channel').read_text())
self.assertEqual('repaired\n', (package / 'droiddeck-unconfirmed').read_text())
self.assertEqual('steamdeck_stable', self.channel())
def test_the_stable_fallback_is_not_repeated(self):
self.test_a_repaired_beta_client_that_still_shows_nothing_falls_back_to_stable()
self.requests.unlink()
with (self.steam / 'package/droiddeck-unconfirmed').open('a') as marker:
marker.write('start\nstart\nstart\n')
self.assertEqual(0, self.install().returncode)
self.assertFalse(self.requests.exists())
self.assertEqual('steamdeck_stable\n', (self.steam / 'package/beta').read_text())
def test_a_pinned_beta_channel_never_falls_back(self):
package = self.break_client(2)
self.assertEqual(0, self.install().returncode)
with (package / 'droiddeck-unconfirmed').open('a') as marker:
marker.write('start\nstart\n')
self.assertEqual('steamdeck_publicbeta', self.channel(BL_STEAM_CHANNEL='beta'))
self.assertEqual(0, self.install(BL_STEAM_CHANNEL='beta').returncode)
self.assertFalse((package / 'droiddeck-channel').exists())
def test_the_channel_setting_selects_stable(self):
setting = self.home / '.config/droiddeck/steam-channel'
setting.parent.mkdir(parents=True)
setting.write_text('stable\n')
self.assertEqual('steamdeck_stable', self.channel())
self.assert_installed('steamdeck_stable', expected=self.stable_requests(False))
self.assertFalse((self.steam / 'package/droiddeck-channel').exists())
def empty_beta_manifest(self):
(self.cdn / 'steam_client_steamdeck_publicbeta_linuxarm64').write_text(
'"linuxarm64"\n{\n\t"version" "123457"\n'
'\t"client_steamrtarm64_linuxarm64"\n\t{\n'
'\t\t"file" "client.zip"\n\t\t"sha2" "00"\n'
'\t}\n}\n')
def test_a_beta_manifest_without_an_arm64_client_falls_back_to_stable(self):
self.empty_beta_manifest()
result = self.assert_installed('steamdeck_stable', expected=self.stable_requests())
self.assertIn('no client components in steam_client_steamdeck_publicbeta_linuxarm64', result.stderr)
self.assertIn('client_steamrtarm64_linuxarm64', result.stderr)
self.assertEqual('steamdeck_stable\n', (self.steam / 'package/droiddeck-channel').read_text())
self.assertEqual('steamdeck_stable', self.channel())
def test_a_pinned_beta_manifest_without_an_arm64_client_fails_and_names_its_blocks(self):
self.empty_beta_manifest()
result = self.install(BL_STEAM_CHANNEL='beta')
self.assertNotEqual(0, result.returncode)
self.assertIn('client_steamrtarm64_linuxarm64', result.stderr)
self.assertFalse((self.steam / 'package/droiddeck-installed').exists())
def test_requested_repair_runs_once_and_is_retried_until_it_completes(self):
package = self.break_client(0)
@@ -177,6 +279,143 @@ cp "$TEST_CDN/${url##*/}" "$out"
self.assertEqual(0, self.install().returncode)
self.assertFalse(self.requests.exists())
def publish(self, **parts):
"""The CDN's client as these parts (block name -> byte count of incompressible content),
listed with sizes as Valve's manifest lists them."""
files = {}
blocks = ''
for block, length in parts.items():
archive = self.cdn / f'{block}.zip.{hashlib.sha1(block.encode()).hexdigest()}'
with zipfile.ZipFile(archive, 'w', zipfile.ZIP_STORED) as package:
package.writestr(f'parts/{block}', os.urandom(length))
if block.endswith('_linuxarm64_linuxarm64'):
package.writestr('steamrtarm64/steam', 'steamdeck_publicbeta')
data = archive.read_bytes()
files[block] = archive.name
blocks += (f'\t"{block}"\n\t{{\n\t\t"file"\t\t"{archive.name}"\n\t\t"size"\t\t"{len(data)}"\n'
f'\t\t"sha2"\t\t"{hashlib.sha256(data).hexdigest()}"\n\t}}\n')
(self.cdn / 'steam_client_steamdeck_publicbeta_linuxarm64').write_text(
'"linuxarm64"\n{\n\t"version"\t\t"123456"\n' + blocks + '}\n')
return files
def two_parts(self):
return self.publish(a_all=1_200_000, client_linuxarm64_linuxarm64=1_300_000)
def plan_for(self, name, *actions):
with self.plan.open('a') as plan:
plan.writelines(f'{name} {action}\n' for action in actions)
def offsets(self, name):
log = Path(str(self.requests) + '.offsets')
return [int(line.split()[1]) for line in log.read_text().splitlines() if line.split()[0] == name]
def requested(self):
return [line.rsplit('/', 1)[1] for line in self.requests.read_text().splitlines()]
@property
def download(self):
return self.steam / 'package/droiddeck-download'
def test_progress_is_counted_in_megabytes_across_parts(self):
self.two_parts()
result = self.install()
self.assertEqual(0, result.returncode, result.stderr)
self.assertIn('downloading Steam: a_all (1/2), 0 of 2 MB\n', result.stdout)
self.assertIn('downloading Steam: client_linuxarm64_linuxarm64 (2/2), 1 of 2 MB\n', result.stdout)
self.assertTrue((self.steam / 'parts/a_all').is_file())
self.assertFalse(self.download.exists())
def test_a_dropped_connection_resumes_from_what_arrived(self):
files = self.two_parts()
client = files['client_linuxarm64_linuxarm64']
self.plan_for(client, 'cut 500000')
result = self.install()
self.assertEqual(0, result.returncode, result.stderr)
self.assertEqual([0, 500000], self.offsets(client))
self.assertIn('interrupted (curl exit 28), trying again in 0 s', result.stdout)
self.assertEqual('123456\n', (self.steam / 'package/droiddeck-installed').read_text())
def test_a_failed_session_keeps_what_it_finished_and_fetched(self):
files = self.two_parts()
client = files['client_linuxarm64_linuxarm64']
self.plan_for(client, 'cut 400000', *['fail 6'] * 12)
result = self.install()
self.assertNotEqual(0, result.returncode)
self.assertIn('could not download client_linuxarm64_linuxarm64 (curl exit 6)', result.stderr)
self.assertFalse((self.steam / 'package/droiddeck-installed').exists())
self.assertTrue((self.download / (files['a_all'] + '.unpacked')).is_file())
self.assertFalse((self.download / files['a_all']).exists())
self.assertEqual(400000, (self.download / client).stat().st_size)
self.requests.unlink()
Path(str(self.requests) + '.offsets').unlink()
self.assertEqual(0, self.install().returncode)
self.assertEqual(['steam_client_steamdeck_publicbeta_linuxarm64', client], self.requested())
self.assertEqual([400000], self.offsets(client))
self.assertFalse(self.download.exists())
def test_a_server_that_will_not_resume_starts_the_part_over(self):
files = self.two_parts()
client = files['client_linuxarm64_linuxarm64']
self.plan_for(client, 'cut 400000', 'norange')
result = self.install()
self.assertEqual(0, result.returncode, result.stderr)
self.assertEqual([0, 400000, 0], self.offsets(client))
def test_a_missing_part_hands_over_to_the_next_cdn_at_once(self):
files = self.two_parts()
client = files['client_linuxarm64_linuxarm64']
self.plan_for(client, 'http 404')
result = self.install()
self.assertEqual(0, result.returncode, result.stderr)
self.assertEqual(['https://client-update.steamstatic.com/' + client,
'https://client-update.akamai.steamstatic.com/' + client],
[line for line in self.requests.read_text().splitlines() if line.endswith(client)])
self.assertIn('HTTP 404 from https://client-update.steamstatic.com', result.stderr)
self.assertNotIn('trying again', result.stdout)
def test_a_server_error_is_retried_on_the_same_cdn(self):
files = self.two_parts()
client = files['client_linuxarm64_linuxarm64']
self.plan_for(client, 'http 503')
result = self.install()
self.assertEqual(0, result.returncode, result.stderr)
self.assertEqual(['https://client-update.steamstatic.com/' + client] * 2,
[line for line in self.requests.read_text().splitlines() if line.endswith(client)])
self.assertIn('interrupted (curl exit 22), trying again in 0 s', result.stdout)
def test_bytes_that_do_not_check_out_are_fetched_again(self):
files = self.two_parts()
client = files['client_linuxarm64_linuxarm64']
self.download.mkdir(parents=True)
(self.download / client).write_bytes(b'x' * (self.cdn / client).stat().st_size)
result = self.install()
self.assertEqual(0, result.returncode, result.stderr)
self.assertEqual([0], self.offsets(client))
def test_parts_left_for_another_client_version_are_dropped(self):
files = self.two_parts()
self.download.mkdir(parents=True)
(self.download / 'a_all.zip.0123').write_bytes(b'older')
(self.download / 'b_all.zip.4567.unpacked').write_bytes(b'')
self.plan_for(files['client_linuxarm64_linuxarm64'], *['fail 7'] * 12)
self.assertNotEqual(0, self.install().returncode)
self.assertEqual([files['a_all'] + '.unpacked'], sorted(p.name for p in self.download.iterdir()))
def test_a_repair_unpacks_every_part_again_once(self):
files = self.two_parts()
self.assertEqual(0, self.install().returncode)
(self.home / '.bl-steam-repair').write_text('1\n')
self.requests.unlink()
self.plan_for(files['client_linuxarm64_linuxarm64'], *['fail 7'] * 12)
self.assertNotEqual(0, self.install().returncode)
self.assertIn(files['a_all'], self.requested())
self.requests.unlink()
self.assertEqual(0, self.install().returncode)
self.assertEqual(['steam_client_steamdeck_publicbeta_linuxarm64', files['client_linuxarm64_linuxarm64']],
self.requested())
self.assertFalse((self.home / '.bl-steam-repair').exists())
self.assertFalse(self.download.exists())
if __name__ == '__main__':
unittest.main()
+2
View File
@@ -153,6 +153,8 @@ class DesktopGamesTest(unittest.TestCase):
steam = root / "Steam"
manifest(steam / "steamapps", 40, name="Game")
manifest(steam / "steamapps", 1628350, name="Steam Linux Runtime 3.0 (sniper)", installdir="SteamLinuxRuntime_sniper")
manifest(steam / "steamapps", 1826330, name="Proton EasyAntiCheat Runtime", installdir="Proton EasyAntiCheat Runtime")
manifest(steam / "steamapps", 1161040, name="Proton BattlEye Runtime", installdir="Proton BattlEye Runtime")
manifest(steam / "steamapps", 5000, name="Some Proton", installdir="Some Proton")
(steam / "steamapps/common/Some Proton").mkdir(parents=True)
(steam / "steamapps/common/Some Proton/toolmanifest.vdf").write_text('"manifest"\n{\n}\n')
+88
View File
@@ -0,0 +1,88 @@
from pathlib import Path
import runpy
import tempfile
import unittest
BIN = Path(__file__).resolve().parents[1] / 'linuxfs/overlay/usr/local/bin'
MODULE = runpy.run_path(str(BIN / 'droiddeck-steam-shaders'))
apply = MODULE['apply']
set_text = MODULE['set_text']
parse = runpy.run_path(str(BIN / 'droiddeck-steam-ui-scale'))['parse']
THOR = '''"InstallConfigStore"
{
\t"Software"
\t{
\t\t"Valve"
\t\t{
\t\t\t"Steam"
\t\t\t{
\t\t\t\t"ShaderCacheManager"
\t\t\t\t{
\t\t\t\t\t"HasCurrentBucket"\t\t"1"
\t\t\t\t\t"ProcessingQueue"\t\t"207140;848350;"
\t\t\t\t\t"App"
\t\t\t\t\t{
\t\t\t\t\t}
\t\t\t\t}
\t\t\t}
\t\t}
\t}
}
'''
def disabled(text):
steam = parse(text).get('InstallConfigStore').get('Software').get('Valve').get('Steam')
return steam.get('ShaderCacheManager').get('DisableShaderCache')
class SteamShadersTest(unittest.TestCase):
def setUp(self):
self.temp = tempfile.TemporaryDirectory()
self.addCleanup(self.temp.cleanup)
self.root = Path(self.temp.name)
self.config = self.root / 'config/config.vdf'
self.config.parent.mkdir()
def test_off_adds_the_key_the_client_writes(self):
text, changed = set_text(THOR, False)
self.assertTrue(changed)
self.assertEqual('1', disabled(text))
self.assertIn('\t\t\t\t\t"DisableShaderCache"\t\t"1"\n\t\t\t\t}', text)
self.assertIn('"ProcessingQueue"\t\t"207140;848350;"', text)
def test_existing_value_is_replaced_in_place(self):
off, _ = set_text(THOR, False)
on, changed = set_text(off, True)
self.assertTrue(changed)
self.assertEqual('0', disabled(on))
self.assertEqual(off.replace('"DisableShaderCache"\t\t"1"', '"DisableShaderCache"\t\t"0"'), on)
def test_matching_value_leaves_the_file_alone(self):
off, _ = set_text(THOR, False)
self.assertEqual((off, False), set_text(off, False))
def test_missing_sections_are_created(self):
text, changed = set_text('"InstallConfigStore"\n{\n}\n', False)
self.assertTrue(changed)
self.assertEqual('1', disabled(text))
def test_apply_writes_config_and_creates_a_fresh_one(self):
self.config.write_text(THOR)
self.assertTrue(apply(self.root, False))
self.assertEqual('1', disabled(self.config.read_text()))
self.assertFalse(apply(self.root, False))
fresh = Path(self.temp.name) / 'fresh'
fresh.mkdir()
self.assertTrue(apply(fresh, True))
self.assertEqual('0', disabled((fresh / 'config/config.vdf').read_text()))
def test_invalid_config_is_rejected(self):
with self.assertRaises(ValueError):
set_text('"InstallConfigStore" {', False)
if __name__ == '__main__':
unittest.main()
+167
View File
@@ -0,0 +1,167 @@
from pathlib import Path
import contextlib
import io
import json
import os
import runpy
import tempfile
import unittest
SCRIPT = Path(__file__).resolve().parents[1] / 'linuxfs/overlay/usr/local/bin/droiddeck-steam-share'
SHARE = runpy.run_path(str(SCRIPT))
CONFIG = ('"InstallConfigStore"\n{\n\t"Software"\n\t{\n\t\t"Valve"\n\t\t{\n\t\t\t"Steam"\n\t\t\t{\n'
'\t\t\t\t"CompatToolMapping"\n\t\t\t\t{\n%s\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n')
def mapping(**tools):
return ''.join('\t\t\t\t\t"%s"\n\t\t\t\t\t{\n\t\t\t\t\t\t"name"\t\t"%s"\n\t\t\t\t\t\t"config"\t\t""\n'
'\t\t\t\t\t\t"priority"\t\t"250"\n\t\t\t\t\t}\n' % (app.lstrip('_'), name)
for app, name in tools.items())
def manifest(app, folder, updated=100, flags=4):
return ('"AppState"\n{\n\t"appid"\t\t"%s"\n\t"name"\t\t"%s"\n\t"StateFlags"\t\t"%d"\n'
'\t"installdir"\t\t"%s"\n\t"LastUpdated"\t\t"%d"\n}\n' % (app, folder, flags, folder, updated))
class SteamShareTest(unittest.TestCase):
def setUp(self):
self.temp = tempfile.TemporaryDirectory()
self.addCleanup(self.temp.cleanup)
home = Path(self.temp.name)
self.arm = home / 'arm/Steam'
self.x64 = home / 'x64/Steam'
self.card = home / 'card'
self.shared = home / 'x64/shared'
for root, tool in ((self.arm, 'droiddeck-proton-arm64'), (self.x64, 'droiddeck-proton-a64')):
(root / 'steamapps/common').mkdir(parents=True)
(root / 'config').mkdir()
(root / 'compatibilitytools.d/proton').mkdir(parents=True)
(root / 'compatibilitytools.d/proton/compatibilitytool.vdf').write_text(
'"compatibilitytools"\n{\n\t"compat_tools"\n\t{\n\t\t"%s"\n\t\t{\n\t\t}\n\t}\n}\n' % tool)
(self.arm / 'config/config.vdf').write_text(CONFIG % mapping(_0='droiddeck-proton-arm64', _413150='droiddeck-fex'))
(self.x64 / 'config/config.vdf').write_text(CONFIG % '')
self.install(self.arm, '413150', 'Stardew Valley')
self.install(self.arm, '13210', 'Unreal Tournament 3')
self.install(self.arm, '1493710', 'Proton - Experimental')
def install(self, root, app, folder, updated=100):
(root / 'steamapps/common' / folder).mkdir(parents=True, exist_ok=True)
(root / 'steamapps/common' / folder / 'game.bin').write_text('x')
(root / ('steamapps/appmanifest_%s.acf' % app)).write_text(manifest(app, folder, updated))
def run_share(self, here):
with contextlib.redirect_stdout(io.StringIO()):
return SHARE['main']([here, str(self.arm), str(self.x64), str(self.card), str(self.shared)])
def view(self, *parts):
return self.shared.joinpath('arm64', 'steamapps', *parts)
def test_x64_client_sees_arm64_games_in_place(self):
self.assertEqual(self.run_share('x86-64'), 0)
self.assertEqual(os.readlink(self.view('common/Stardew Valley')), str(self.arm / 'steamapps/common/Stardew Valley'))
self.assertTrue(self.view('appmanifest_413150.acf').is_file())
self.assertTrue(self.view('appmanifest_13210.acf').is_file())
self.assertFalse(self.view('appmanifest_1493710.acf').exists())
folders = (self.x64 / 'steamapps/libraryfolders.vdf').read_text()
self.assertIn(str(self.shared / 'arm64'), folders)
self.assertIn('ARM64 client games', folders)
def test_tools_follow_the_other_client(self):
self.run_share('x86-64')
tools = SHARE['entries']((self.x64 / 'config/config.vdf').read_text())
self.assertEqual(tools['413150'], 'droiddeck-direct')
self.assertEqual(tools['13210'], 'droiddeck-proton-a64')
def test_a_proton_pick_stays_when_the_other_client_uses_another_proton(self):
(self.x64 / 'config/config.vdf').write_text(CONFIG % mapping(_13210='droiddeck-proton-11-a64'))
self.run_share('x86-64')
self.assertEqual(SHARE['entries']((self.x64 / 'config/config.vdf').read_text())['13210'], 'droiddeck-proton-11-a64')
def test_an_automatic_proton_gives_way_to_the_installed_linux_build(self):
self.install(self.x64, '550', 'Left 4 Dead 2')
(self.x64 / 'config/config.vdf').write_text(CONFIG % mapping(_550='droiddeck-direct'))
(self.arm / 'config/config.vdf').write_text(CONFIG % mapping(_0='droiddeck-proton-arm64', _550='droiddeck-proton-arm64'))
self.run_share('arm64')
self.assertEqual(SHARE['entries']((self.arm / 'config/config.vdf').read_text())['550'], 'droiddeck-fex')
def test_an_x64_proton_pick_reaches_the_arm64_client(self):
self.install(self.x64, '550', 'Left 4 Dead 2')
(self.x64 / 'config/config.vdf').write_text(CONFIG % mapping(_550='droiddeck-proton-a64'))
self.run_share('arm64')
self.assertEqual(SHARE['entries']((self.arm / 'config/config.vdf').read_text())['550'], 'droiddeck-proton-arm64')
def test_a_tool_the_user_chose_is_kept(self):
(self.x64 / 'config/config.vdf').write_text(CONFIG % mapping(_13210='my-proton'))
self.run_share('x86-64')
self.assertEqual(SHARE['entries']((self.x64 / 'config/config.vdf').read_text())['13210'], 'my-proton')
def test_own_install_wins(self):
self.install(self.x64, '413150', 'Stardew Valley')
self.run_share('x86-64')
self.assertFalse(self.view('appmanifest_413150.acf').exists())
self.assertFalse(os.path.lexists(self.view('common/Stardew Valley')))
def test_newer_update_goes_back_to_the_source(self):
self.run_share('x86-64')
self.view('appmanifest_413150.acf').write_text(manifest('413150', 'Stardew Valley', 200))
self.run_share('x86-64')
self.assertIn('"LastUpdated"\t\t"200"', (self.arm / 'steamapps/appmanifest_413150.acf').read_text())
def test_uninstall_from_the_view_hides_until_reinstalled(self):
self.run_share('x86-64')
self.view('appmanifest_13210.acf').unlink()
self.run_share('x86-64')
self.assertFalse(os.path.lexists(self.view('common/Unreal Tournament 3')))
self.assertTrue((self.arm / 'steamapps/common/Unreal Tournament 3/game.bin').is_file())
self.run_share('x86-64')
self.assertFalse(self.view('appmanifest_13210.acf').exists())
self.install(self.arm, '13210', 'Unreal Tournament 3', 300)
self.run_share('x86-64')
self.assertTrue(self.view('appmanifest_13210.acf').is_file())
def test_files_deleted_through_the_view_drop_the_source_manifest(self):
self.run_share('x86-64')
(self.arm / 'steamapps/common/Unreal Tournament 3/game.bin').unlink()
self.view('appmanifest_13210.acf').unlink()
self.run_share('x86-64')
self.assertFalse((self.arm / 'steamapps/appmanifest_13210.acf').exists())
def test_removed_source_leaves_the_view(self):
self.run_share('x86-64')
(self.arm / 'steamapps/appmanifest_413150.acf').unlink()
self.run_share('x86-64')
self.assertFalse(self.view('appmanifest_413150.acf').exists())
self.assertFalse(os.path.lexists(self.view('common/Stardew Valley')))
state = json.loads((self.shared / 'arm64/.droiddeck-shared.json').read_text())
self.assertNotIn('413150', state['shared'])
def test_arm64_client_sees_x64_games(self):
self.install(self.x64, '550', 'Left 4 Dead 2')
(self.x64 / 'config/config.vdf').write_text(CONFIG % mapping(_550='droiddeck-direct'))
self.run_share('arm64')
self.assertTrue(self.shared.joinpath('x86-64/steamapps/appmanifest_550.acf').is_file())
self.assertEqual(SHARE['entries']((self.arm / 'config/config.vdf').read_text())['550'], 'droiddeck-fex')
self.assertIn('x86-64 client games', (self.arm / 'steamapps/libraryfolders.vdf').read_text())
def test_an_empty_view_is_not_offered(self):
self.run_share('x86-64')
for app in ('413150', '13210', '1493710'):
(self.arm / ('steamapps/appmanifest_%s.acf' % app)).unlink()
self.run_share('x86-64')
self.assertNotIn(str(self.shared / 'arm64'), (self.x64 / 'steamapps/libraryfolders.vdf').read_text())
def test_unfinished_installs_are_not_shared(self):
(self.arm / 'steamapps/appmanifest_413150.acf').write_text(manifest('413150', 'Stardew Valley', flags=1026))
self.run_share('x86-64')
self.assertFalse(self.view('appmanifest_413150.acf').exists())
def test_bad_arguments(self):
with contextlib.redirect_stdout(io.StringIO()):
self.assertEqual(SHARE['main'](['mips']), 2)
if __name__ == '__main__':
unittest.main()
+24
View File
@@ -203,6 +203,30 @@ class ShortcutsTest(unittest.TestCase):
self.assertEqual('-vulkan', steam['LaunchOptions'])
self.assertEqual('Mine', steam['AppName'])
def users(self, *blocks):
body = ''.join('\t"%d"\n\t{\n%s\t}\n' % (76561197960265728 + acct, ''.join('\t\t"%s"\t\t"%s"\n' % kv for kv in keys.items()))
for acct, keys in blocks)
(self.steam / 'config').mkdir(exist_ok=True)
(self.steam / 'config/loginusers.vdf').write_text('"users"\n{\n%s}\n' % body)
(self.steam / 'userdata/456/config').mkdir(parents=True, exist_ok=True)
def test_the_account_marked_most_recent_is_used(self):
self.users((123, dict(MostRecent='1', AutoLogin='0', timestamp='100')), (456, dict(MostRecent='0', AutoLogin='1', timestamp='200')))
self.assertEqual(str(self.steam / 'userdata/123'), shortcuts.account_dir(str(self.steam)))
def test_without_most_recent_the_account_that_signs_in_is_used(self):
self.users((123, dict(AutoLogin='1', timestamp='100')), (456, dict(AutoLogin='0', timestamp='200')))
self.assertEqual(str(self.steam / 'userdata/123'), shortcuts.account_dir(str(self.steam)))
self.assertIn(APPID, self.run_writer(self.game()))
def test_without_either_mark_the_account_used_last_is_used(self):
self.users((456, dict(AutoLogin='0', timestamp='100')), (123, dict(AutoLogin='0', timestamp='200')))
self.assertEqual(str(self.steam / 'userdata/123'), shortcuts.account_dir(str(self.steam)))
def test_two_accounts_without_any_mark_write_nothing(self):
self.users((123, dict(AccountName='a')), (456, dict(AccountName='b')))
self.assertIsNone(shortcuts.account_dir(str(self.steam)))
def test_an_older_record_without_launch_options_still_takes_the_apps(self):
self.run_writer(self.game())
record = self.record()
+123
View File
@@ -0,0 +1,123 @@
import os
from pathlib import Path
import re
import subprocess
import tempfile
import unittest
BIN = Path(__file__).resolve().parents[1] / 'linuxfs/overlay/usr/local/bin'
SHIM = BIN / 'droiddeck-steamrt-shim'
SESSION = (BIN / 'droiddeck-session').read_text()
X64 = (BIN / 'droiddeck-steam-x64').read_text()
class SteamrtShimTest(unittest.TestCase):
def setUp(self):
self.temp = tempfile.TemporaryDirectory()
self.addCleanup(self.temp.cleanup)
self.root = Path(self.temp.name)
self.client = self.root / 'steamrtarm64'
self.client.mkdir()
self.shim = self.root / 'shim'
def runtime(self, path):
runtime = self.client / path
(runtime / 'bin').mkdir(parents=True)
(runtime / 'bin/steam-runtime-check-requirements').write_text('#!/bin/sh\nexit 1\n')
(runtime / 'bin/steam-runtime-system-info').write_text('#!/bin/sh\necho info\n')
(runtime / 'files').mkdir()
(runtime / 'files/marker').write_text('runtime files')
return runtime
def build(self):
return subprocess.run(['bash', str(SHIM), str(self.client), str(self.shim)],
capture_output=True, text=True, timeout=30)
def sh(self, *args):
return subprocess.run(['sh', *map(str, args)], capture_output=True, text=True, timeout=30)
def assert_shim(self):
result = self.build()
self.assertEqual(0, result.returncode, result.stderr)
self.assertEqual(0, self.sh(self.shim / 'bin/steam-runtime-check-requirements').returncode)
self.assertEqual('info\n', self.sh(self.shim / 'bin/steam-runtime-system-info').stdout)
self.assertEqual('runtime files', (self.shim / 'files/marker').read_text())
launched = self.sh(self.shim / 'bin/steam-runtime-launch-client', '--env=BL_SHIM_TEST=yes',
f'--directory={self.root}', '--', 'sh', '-c', 'echo "$BL_SHIM_TEST $(basename "$PWD")"')
self.assertEqual(f'yes {self.root.name}\n', launched.stdout)
launched = self.sh(self.shim / 'bin/steam-runtime-launch-client', '--env', 'BL_SHIM_TEST=split',
'--directory', self.root, '--', 'sh', '-c', 'echo "$BL_SHIM_TEST $(basename "$PWD")"')
self.assertEqual(f'split {self.root.name}\n', launched.stdout)
missing = self.sh(self.shim / 'bin/steam-runtime-launch-client', '--directory', self.root / 'missing',
'--', 'true')
self.assertEqual(127, missing.returncode)
def test_a_client_without_a_container_gets_no_shim(self):
self.shim.mkdir()
self.assertEqual(1, self.build().returncode)
self.assertFalse(self.shim.exists())
def test_the_pressure_vessel_runtime_is_shimmed(self):
self.runtime('pv-runtime/steam-runtime-steamrt')
self.assert_shim()
def test_a_requirements_probe_elsewhere_is_shimmed(self):
self.runtime('steam-runtime')
self.assert_shim()
def test_the_arm64_session_starts_a_containerized_client_through_the_shim(self):
block = re.search(r'\n( +steam_client=\("\$steam_root/steamrtarm64/steam"\)\n.*?\n fi\n)', SESSION, re.S).group(1)
stub = self.root / 'droiddeck-steamrt-shim'
for status, shimmed in ((1, False), (0, True)):
stub.write_text(f'#!/bin/bash\nexit {status}\n')
stub.chmod(0o755)
script = 'if :; then\n' + block + 'printf "%s\\n" "${steam_client[@]}"\n'
env = dict(os.environ, HOME=str(self.root), steam_root='/steam',
PATH=str(self.root) + os.pathsep + os.environ['PATH'])
result = subprocess.run(['bash', '-c', script], env=env, capture_output=True, text=True, timeout=30)
self.assertEqual(0, result.returncode, result.stderr)
lines = result.stdout.splitlines()
self.assertEqual('/steam/steamrtarm64/steam', lines[-1])
self.assertEqual(shimmed, 'Valve containerized the arm64 Steam client' in result.stdout)
self.assertEqual(shimmed, any(line.startswith('STEAM_RUNTIME_STEAMRT=') and
line.endswith('/.droiddeck-steamrt-shim') for line in lines))
def test_both_clients_are_told_they_already_run_inside_the_container(self):
guard = SESSION.index('export STEAM_STEAMRT_RECURSION_GUARD=1')
self.assertLess(guard, SESSION.index('"${steam_client[@]}" "${_bl_ui[@]}"'))
self.assertIn('-u STEAM_STEAMRT_RECURSION_GUARD', SESSION[:guard])
self.assertIn('export STEAM_STEAMRT_RECURSION_GUARD=1', X64)
self.assertIn('droiddeck-steamrt-shim "$STEAM_X64/steamrt64" "$X64_HOME/steamrt-shim" || rm -rf "$X64_HOME/steamrt-shim"', X64)
class SessionChannelTest(unittest.TestCase):
BLOCK = re.search(r'\n( +steam_channel=steamdeck_publicbeta\n.*?\n +fi\n)', SESSION, re.S).group(1)
def channel(self, x64, installer):
with tempfile.TemporaryDirectory() as temp:
stub = Path(temp) / 'droiddeck-steam-install'
stub.write_text('#!/bin/bash\n' + installer + '\n')
stub.chmod(0o755)
env = dict(os.environ, steam_x64=x64, PATH=temp + os.pathsep + os.environ['PATH'])
result = subprocess.run(['bash', '-c', self.BLOCK + 'echo "$steam_channel"\n'], env=env,
capture_output=True, text=True, timeout=30)
self.assertEqual(0, result.returncode, result.stderr)
return result.stdout.strip()
def test_the_arm64_client_follows_the_installed_channel(self):
self.assertEqual('steamdeck_stable', self.channel('0', '[ "$1" = --channel ] && echo steamdeck_stable'))
def test_an_installer_without_an_answer_keeps_the_deck_beta(self):
self.assertEqual('steamdeck_publicbeta', self.channel('0', 'exit 1'))
def test_the_x86_client_keeps_its_own_channel(self):
self.assertEqual('steamdeck_publicbeta', self.channel('1', 'echo steamdeck_stable'))
def test_the_client_branch_on_disk_and_clientbeta_use_the_same_channel(self):
self.assertIn('-clientbeta "$steam_channel"', SESSION)
self.assertIn('echo "$steam_channel" > "$steam_beta"', SESSION)
if __name__ == '__main__':
unittest.main()
+30
View File
@@ -223,5 +223,35 @@ class VencEmulatorTest(unittest.TestCase):
self.assertEqual(server.touches, [(0, 65535, 32767), (1, 32767, 0), (2, 32767, 0)])
def test_xtest_calls_from_a_locally_loaded_library_still_reach_libxtst(self):
with tempfile.TemporaryDirectory() as tmp:
tmp = Path(tmp)
(tmp / 'xtst.c').write_text(
'int XTestFakeButtonEvent(void *d, unsigned b, int p, unsigned long t) { return 7; }\n'
'int XTestFakeMotionEvent(void *d, int s, int x, int y, unsigned long t) { return 7; }\n')
(tmp / 'ui.c').write_text(
'int XTestFakeButtonEvent(void *, unsigned, int, unsigned long);\n'
'int XTestFakeMotionEvent(void *, int, int, int, unsigned long);\n'
'int ui_click(void) { return XTestFakeButtonEvent(0, 1, 1, 0) + XTestFakeButtonEvent(0, 3, 0, 0); }\n'
'int ui_move(void) { return XTestFakeMotionEvent(0, 0, 10, 20, 0); }\n')
(tmp / 'main.c').write_text(
'#include <dlfcn.h>\n#include <stdio.h>\n'
'int main(int argc, char **argv) {\n'
' void *ui = dlopen(argv[1], RTLD_NOW | RTLD_LOCAL);\n'
' if (!ui) return 2;\n'
' int (*click)(void) = dlsym(ui, "ui_click"), (*move)(void) = dlsym(ui, "ui_move");\n'
' printf("%d %d\\n", click(), move());\n'
' return click() == 14 && move() == 7 ? 0 : 1;\n'
'}\n')
run = lambda *a: subprocess.run(['cc', *a], check=True, cwd=tmp)
run('-shared', '-fPIC', '-Wl,-soname,libXtst.so.6', '-o', 'libXtst.so.6', 'xtst.c')
run('-shared', '-fPIC', '-o', 'libui.so', 'ui.c', '-L.', '-l:libXtst.so.6', "-Wl,-rpath,$ORIGIN")
run('-shared', '-fPIC', '-pthread', '-o', 'libhook.so', str(VENC), str(TOUCH), '-ldl')
run('-o', 'main', 'main.c', '-ldl')
done = subprocess.run([str(tmp / 'main'), str(tmp / 'libui.so')],
env=dict(os.environ, LD_PRELOAD=str(tmp / 'libhook.so')),
capture_output=True, text=True, timeout=20)
self.assertEqual(done.returncode, 0, done.stdout + done.stderr)
if __name__ == '__main__':
unittest.main()
+19 -3
View File
@@ -31,14 +31,14 @@ class XboxBuildSourceTest(unittest.TestCase):
git('switch', '-q', 'main')
(root / 'backend').write_text('merged backend')
git('commit', '-qam', 'backend changed after branch')
for name in ('xbox', 'apk'):
for name in ('source', 'xbox', 'components', 'apk', 'tests', 'dispatch'):
git('clone', '-q', str(root), str(root / name))
git('checkout', '-q', head, cwd=root / name)
output = root / 'output'
env['GITHUB_OUTPUT'] = str(output)
subprocess.run(['bash', '-c', step('xbox', 'Pin and merge the APK source')], cwd=root / 'xbox', env=env, check=True, capture_output=True)
subprocess.run(['bash', '-c', step('source', 'Pin and merge the APK source')], cwd=root / 'source', env=env, check=True, capture_output=True)
base = output.read_text().strip().split('=', 1)[1]
tree = git('rev-parse', 'HEAD^{tree}', cwd=root / 'xbox')
tree = git('rev-parse', 'HEAD^{tree}', cwd=root / 'source')
(root / 'backend').write_text('newer main, after backend build')
git('commit', '-qam', 'main advanced again')
env.update(BUILD_BASE_SHA=base, XBOX_SOURCE_TREE=tree)
@@ -46,3 +46,19 @@ class XboxBuildSourceTest(unittest.TestCase):
subprocess.run(['bash', '-c', step('build', 'Verify backend and APK source match')], cwd=root / 'apk', env=env, check=True, capture_output=True)
self.assertEqual((root / 'apk/backend').read_text(), 'merged backend')
self.assertEqual(git('rev-parse', 'HEAD^{tree}', cwd=root / 'apk'), tree)
subprocess.run(['bash', '-c', step('python-tests', 'Merge the pinned APK base')], cwd=root / 'tests', env=env, check=True, capture_output=True)
subprocess.run(['bash', '-c', step('python-tests', 'Verify backend and test source match')], cwd=root / 'tests', env=env, check=True, capture_output=True)
self.assertEqual(git('rev-parse', 'HEAD^{tree}', cwd=root / 'tests'), tree)
env['SOURCE_TREE'] = tree
for job in ('xbox', 'components'):
subprocess.run(['bash', '-c', step(job, 'Merge the pinned APK base')], cwd=root / job, env=env, check=True, capture_output=True)
subprocess.run(['bash', '-c', step(job, 'Verify pinned source tree')], cwd=root / job, env=env, check=True, capture_output=True)
self.assertEqual(git('rev-parse', 'HEAD^{tree}', cwd=root / job), tree)
trusted = root / 'trusted-output'
env['GITHUB_OUTPUT'] = str(trusted)
subprocess.run(['bash', '-c', step('source', 'Resolve trusted manifest base')], cwd=root / 'dispatch', env=env, check=True, capture_output=True)
self.assertEqual(trusted.read_text().strip(), 'base-sha=' + git('rev-parse', 'main'))
self.assertNotEqual(git('rev-parse', 'HEAD', cwd=root / 'dispatch'), git('rev-parse', 'main'))
+46 -4
View File
@@ -1,8 +1,10 @@
import io
import json
import os
from pathlib import Path
import runpy
import socket
import subprocess
import tempfile
import threading
import unittest
@@ -54,11 +56,13 @@ class XboxLaunchTest(unittest.TestCase):
patch.dict(xbox['launch'].__globals__, wine=unittest.mock.Mock(return_value=7)):
launch = xbox['launch']
self.assertEqual(launch(['/proton', 'waitforexitandrun', '/encrypted/Game.exe'],
{'BL_LAUNCH_DIR': '/launch', 'DROIDDECK_XBOX_CREDENTIAL_SOCKET': 'stale'}), 7)
{'BL_LAUNCH_DIR': '/launch', 'DROIDDECK_XBOX_CREDENTIAL_SOCKET': 'stale',
'DROIDDECK_XBOX_CREDENTIAL_PORT': '4100', 'DROIDDECK_XBOX_CREDENTIAL_KEY': 'stale-key'}), 7)
wine = launch.__globals__['wine']
self.assertEqual(wine.call_args.args[0], ['/private/Game.exe'])
env = wine.call_args.args[1]
self.assertNotIn('DROIDDECK_XBOX_CREDENTIAL_SOCKET', env)
for name in ('DROIDDECK_XBOX_CREDENTIAL_SOCKET', 'DROIDDECK_XBOX_CREDENTIAL_PORT', 'DROIDDECK_XBOX_CREDENTIAL_KEY'):
self.assertNotIn(name, env)
self.assertEqual(env['XODUS_XSTS_PLAYFAB'], 'game-token')
close.assert_called_once()
self.assertEqual(store['ask'].call_args.args[1], {'op': 'xbox-released', 'lease': 'lease-1'})
@@ -115,7 +119,8 @@ class XboxLaunchTest(unittest.TestCase):
command = root / 'command.json'
command.write_text(json.dumps(['/selected/proton', 'waitforexitandrun', '/encrypted/Game.exe', '--custom', 'two words']))
env = {'STEAM_COMPAT_DATA_PATH': str(prefix.parent), 'DROIDDECK_XBOX_COMMAND_FILE': str(command),
'DROIDDECK_XBOX_CREDENTIAL_SOCKET': 'private-account-channel', 'XODUS_BRIDGE_KEY': 'runtime-key'}
'DROIDDECK_XBOX_CREDENTIAL_SOCKET': 'private-account-channel', 'DROIDDECK_XBOX_CREDENTIAL_PORT': '4100',
'DROIDDECK_XBOX_CREDENTIAL_KEY': 'private-key', 'XODUS_BRIDGE_KEY': 'runtime-key'}
with patch.dict(os.environ, env), patch.dict(xbox['wine'].__globals__, RUNTIME=runtime.parent), patch('subprocess.run') as initialize, patch('subprocess.call', return_value=7) as call:
initialize.return_value.returncode = 0
self.assertEqual(xbox['wine'](['/private-view/Game.exe']), 7)
@@ -126,6 +131,43 @@ class XboxLaunchTest(unittest.TestCase):
initialize.assert_called_once()
for child in (initialize, call):
child_env = child.call_args.kwargs['env']
self.assertNotIn('DROIDDECK_XBOX_CREDENTIAL_SOCKET', child_env)
for name in ('DROIDDECK_XBOX_CREDENTIAL_SOCKET', 'DROIDDECK_XBOX_CREDENTIAL_PORT', 'DROIDDECK_XBOX_CREDENTIAL_KEY'):
self.assertNotIn(name, child_env)
self.assertNotIn('DROIDDECK_XBOX_COMMAND_FILE', child_env)
self.assertEqual(child_env['XODUS_BRIDGE_KEY'], 'runtime-key')
def test_missing_runtime_classes_are_registered_once(self):
with tempfile.TemporaryDirectory() as root:
prefix = Path(root)
key = xbox['CLASS_KEY'].replace('\\', '\\\\')
present = [name for names in xbox['CLASSES'].values() for name in names
if name != 'Windows.ApplicationModel.DataTransfer.DataTransferManager']
(prefix / 'system.reg').write_text(''.join('[%s%s] 1\n"DllPath"="x"\n\n' % (key, name) for name in present))
imported = []
def run(argv, **kwargs):
imported.append(Path(argv[4][2:].replace('\\', '/')).read_bytes()[2:].decode('utf-16-le'))
return subprocess.CompletedProcess(argv, 0)
with patch('subprocess.run', side_effect=run) as child:
xbox['register_classes']('/selected/proton', prefix, {'A': '1'})
self.assertEqual(child.call_args.args[0][:4], ['/selected/proton', 'run', 'reg.exe', 'import'])
self.assertEqual(child.call_args.kwargs['env'], {'A': '1'})
self.assertEqual(imported, ['Windows Registry Editor Version 5.00\r\n\r\n[HKEY_LOCAL_MACHINE\\Software\\Microsoft\\WindowsRuntime'
'\\ActivatableClassId\\Windows.ApplicationModel.DataTransfer.DataTransferManager]\r\n'
'"DllPath"="C:\\\\windows\\\\system32\\\\twinapi.appcore.dll"\r\n\r\n'])
with (prefix / 'system.reg').open('a') as file:
file.write('[%sWindows.ApplicationModel.DataTransfer.DataTransferManager] 1\n' % key)
with patch('subprocess.run') as child:
xbox['register_classes']('/selected/proton', prefix, {})
(prefix / 'system.reg').unlink()
xbox['register_classes']('/selected/proton', prefix, {})
child.assert_not_called()
def test_failed_class_registration_is_reported_and_launch_continues(self):
with tempfile.TemporaryDirectory() as root:
prefix = Path(root)
(prefix / 'system.reg').write_text('')
for outcome in ({'return_value': subprocess.CompletedProcess([], 1)},
{'side_effect': subprocess.TimeoutExpired('reg.exe', 90)}):
with patch('subprocess.run', **outcome), patch('sys.stderr', new_callable=io.StringIO) as err:
self.assertIsNone(xbox['register_classes']('/selected/proton', prefix, {}))
self.assertIn('registration failed', err.getvalue())
+16
View File
@@ -7,6 +7,12 @@ checksums. `runtime.lock.json` pins the Nexus Windows runtime release and each
extracted PE DLL. `prepare.py` verifies both source revisions and all Nexus patch
checksums before applying the local transport, launch and library patches.
`droiddeck-cloud-token.patch` adds `xodus-cli cloud-token`: the stored account's
Xbox Cloud Gaming sign-in (XSTS for `http://gssv.xboxlive.com/`, then the streaming
service's `/v2/login/user`) and the login proof token a session's `/connect` step
takes. It prints one JSON object and never opens a login or provisions a device.
Ported from Bannerlator.
`droiddeck-library-subscriptions.patch` queries fresh subscription membership
alongside product entitlements using the same Store session, account and market.
Android can validate a Game Pass child's parent plan when that plan is absent
@@ -30,6 +36,16 @@ patched backend source archive and the checksum-verified runtime source archive
in the build cache and publishes both in CI's `xbox-corresponding-source`
artifact; distribute corresponding sources with published binaries.
`droiddeck-credential-fallback.patch` names the reason when the backend cannot reach the
app's credential socket (`NEXUS_CREDENTIAL_ERROR_V1 socket-refused`, `-denied`, ...) and then
uses a keyed loopback port the app also listens on; some devices refuse the abstract socket
from inside PRoot. The key is passed in the backend's environment only.
`droiddeck-credential-diagnostics.patch` names the step behind a `credential-io` failure: the
session lock (`lock-dir-owner`, `lock-file-mode`, `lock-flock-<reason>`, ...), a socket or loopback
exchange that broke after connecting (`socket-exchange-<reason>`), a malformed host reply, or a
missing socket name, port or key. Errno values without a name are spelled with letters, a = 0 ... j = 9.
`droiddeck-credentials.patch` replaces Linux Secret Service with a bounded,
Android abstract socket restricted to the backend process started by the host. The Android host encrypts every persistent
write using its existing Keystore cipher, verifies backend read-backs, and
+21 -8
View File
@@ -1,14 +1,7 @@
#!/usr/bin/env bash
set -euo pipefail
repo=$(cd "$(dirname "$0")/../.." && pwd)
cache=${DROIDDECK_XBOX_CACHE:-"${HOME}/.cache/droiddeck-xbox-build"}
mkdir -p "$cache"
docker run --rm --platform linux/arm64 -v "$repo:/src" -v "$cache:/work" -w /src \
-e CARGO_HOME=/work/cargo -e RUSTUP_HOME=/work/rustup -e XBOX_BUILD_DIR=/work/build ubuntu:24.04 bash -lc '
set -euo pipefail
apt-get update -qq
DEBIAN_FRONTEND=noninteractive apt-get install -y -qq --no-install-recommends \
build-essential pkg-config libssl-dev protobuf-compiler cmake curl git ca-certificates xz-utils python3 >/dev/null
build='
export PATH=/work/cargo/bin:$PATH
if ! command -v rustup >/dev/null; then
version=$(python3 -c '\''import json;print(json.load(open("tools/xbox/backend.lock.json"))["rust"])'\'')
@@ -16,3 +9,23 @@ docker run --rm --platform linux/arm64 -v "$repo:/src" -v "$cache:/work" -w /src
fi
bash tools/xbox/build.sh
'
if [[ $(uname -m) = x86_64 ]]; then
cache=${DROIDDECK_XBOX_CACHE:-"${HOME}/.cache/droiddeck-xbox-cross"}
platform=linux/amd64
image=droiddeck-xbox-cross:$(shasum -a 256 "$repo/tools/xbox/cross.Dockerfile" | cut -c1-12)
docker image inspect "$image" >/dev/null 2>&1 \
|| docker build --platform "$platform" -t "$image" -f "$repo/tools/xbox/cross.Dockerfile" "$repo/tools/xbox"
else
cache=${DROIDDECK_XBOX_CACHE:-"${HOME}/.cache/droiddeck-xbox-build"}
platform=linux/arm64
image=ubuntu:24.04
build='
apt-get update -qq
DEBIAN_FRONTEND=noninteractive apt-get install -y -qq --no-install-recommends \
build-essential pkg-config libssl-dev protobuf-compiler cmake curl git ca-certificates xz-utils python3 >/dev/null
'"$build"
fi
mkdir -p "$cache"
docker run --rm --platform "$platform" -v "$repo:/src" -v "$cache:/work" -w /src \
-e CARGO_HOME=/work/cargo -e RUSTUP_HOME=/work/rustup -e XBOX_BUILD_DIR=/work/build "$image" \
bash -lc "set -euo pipefail$build"
+1 -1
View File
@@ -13,7 +13,7 @@ export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=${CARGO_TARGET_AARCH64_UNKN
if [[ $(uname -m) = aarch64 ]]; then export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=cc; fi
cd "$source_dir"
cargo build --locked --release --no-default-features --target aarch64-unknown-linux-gnu -p xodus-cli -p xodus-service
if [[ $(uname -m) = aarch64 ]]; then
if [[ $(uname -m) = aarch64 || -n ${CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_RUNNER:-} ]]; then
cargo test --locked --release --no-default-features --target aarch64-unknown-linux-gnu -p xodus-cli
cargo test --locked --release --no-default-features --target aarch64-unknown-linux-gnu -p xodus credential_
cargo test --locked --release --no-default-features --target aarch64-unknown-linux-gnu -p msixvc streaming_
+28
View File
@@ -0,0 +1,28 @@
FROM ubuntu:24.04
ENV DEBIAN_FRONTEND=noninteractive
RUN dpkg --add-architecture arm64 \
&& sed -i '/^Types:/a Architectures: amd64' /etc/apt/sources.list.d/ubuntu.sources \
&& printf '%s\n' \
'Types: deb' \
'URIs: http://ports.ubuntu.com/ubuntu-ports/' \
'Suites: noble noble-updates noble-security' \
'Components: main universe restricted multiverse' \
'Architectures: arm64' \
'Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg' \
> /etc/apt/sources.list.d/ubuntu-ports.sources \
&& apt-get update -qq \
&& apt-get install -y -qq --no-install-recommends \
build-essential pkg-config protobuf-compiler cmake curl git ca-certificates xz-utils python3 \
gcc-aarch64-linux-gnu g++-aarch64-linux-gnu qemu-user \
libssl-dev:arm64 libstdc++6:arm64 \
&& rm -rf /var/lib/apt/lists/*
ENV CC_aarch64_unknown_linux_gnu=aarch64-linux-gnu-gcc \
CXX_aarch64_unknown_linux_gnu=aarch64-linux-gnu-g++ \
AR_aarch64_unknown_linux_gnu=aarch64-linux-gnu-ar \
PKG_CONFIG_ALLOW_CROSS=1 \
PKG_CONFIG_LIBDIR_aarch64_unknown_linux_gnu=/usr/lib/aarch64-linux-gnu/pkgconfig:/usr/share/pkgconfig \
CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=aarch64-linux-gnu-gcc \
CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_RUNNER=qemu-aarch64
+171
View File
@@ -0,0 +1,171 @@
diff --git a/crates/xodus-cli/src/commands.rs b/crates/xodus-cli/src/commands.rs
index 5a66715..5b310e4 100644
--- a/crates/xodus-cli/src/commands.rs
+++ b/crates/xodus-cli/src/commands.rs
@@ -6,6 +6,7 @@ pub mod license;
pub mod login;
pub mod logout;
pub mod profile;
+pub mod cloud_token;
pub mod game_activity;
pub mod achievements;
pub mod subscription;
diff --git a/crates/xodus-cli/src/commands/cloud_token.rs b/crates/xodus-cli/src/commands/cloud_token.rs
new file mode 100644
index 0000000..fbd0ed0
--- /dev/null
+++ b/crates/xodus-cli/src/commands/cloud_token.rs
@@ -0,0 +1,124 @@
+//! Xbox Cloud Gaming streaming token for the stored account (DroidDeck). Same bearer
+//! RPS -> Xbox user -> XSTS exchange as the profile read, for the cloud gaming relying party,
+//! then the streaming service's user login (as xbox.com and Greenlight's xal-node do).
+//! Never provisions a device or opens login.
+use std::{process::ExitCode, time::Duration};
+use serde_json::{Value, json};
+use xodus::{models::{live::ExchangeUserTokenOutcome, secrets::Token, soap}, tokens::TokenManager};
+
+const GSSV_RELYING_PARTY: &str = "http://gssv.xboxlive.com/";
+const OFFERINGS: [&str; 2] = ["xgpuweb", "xgpuwebf2p"];
+
+/// The stored account's compact Microsoft account ticket for one service target and policy.
+async fn ticket(tokens: &TokenManager, client: &reqwest::Client, target: &str, policy: soap::PolicyReference) -> Result<(String, String), &'static str> {
+ let (account, device, user) = super::session::prepare(tokens, client).await.map_err(|stage| {
+ super::session::diagnostic(stage); stage
+ })?;
+ let outcome = super::session::exchange(|| async { xodus::api::live::exchange_user_token(client, user.clone(), account.clone(), device.clone(),
+ None, Some("Silent".into()), "{d6d5a677-0872-4ab0-9442-bb792fce85c5}".into(),
+ &[(target.into(), Some(policy.clone()))])
+ .await.map_err(|error| super::session::classify(&error)) }).await?;
+ let response = match outcome {
+ ExchangeUserTokenOutcome::Issued(soap::BodyContent::RequestSecurityTokenResponseCollection(mut collection)) if !collection.security_tokens.is_empty() => collection.security_tokens.remove(0),
+ ExchangeUserTokenOutcome::Issued(soap::BodyContent::RequestSecurityTokenResponse(response)) => *response,
+ _ => return Err("msa-response"),
+ };
+ // Token::from panics on token types it does not know; only take the compact forms here. The cloud
+ // transfer purpose answers with a login proof token, which is what /connect takes (Greenlight
+ // sends the MSAL response's "lpt").
+ let kind = response.token_type.clone();
+ if kind == "urn:passport:loginprooftoken" {
+ let shape = match (&response.requested_security_token.binary_security_token, &response.requested_security_token.encrypted_data) {
+ (Some(token), _) if !token.value.is_empty() => "binary",
+ (_, Some(_)) => "encrypted",
+ _ => "empty",
+ };
+ eprintln!("NEXUS_SESSION_DIAGNOSTIC_V1 login-proof-{shape}");
+ return match response.requested_security_token.binary_security_token {
+ Some(token) if !token.value.is_empty() => Ok((account, token.value)),
+ _ => Err("msa-ticket"),
+ };
+ }
+ let compact = matches!(kind.as_str(), "urn:passport:compact" | "urn:passport:delegationcompact")
+ && response.requested_security_token.binary_security_token.is_some();
+ if !compact {
+ let slug: String = kind.chars().map(|c| if c.is_ascii_alphabetic() { c.to_ascii_lowercase() } else { '-' }).take(40).collect();
+ eprintln!("NEXUS_SESSION_DIAGNOSTIC_V1 ticket-type-{slug}");
+ return Err("msa-ticket");
+ }
+ let Token::Compact(ticket) = Token::from(response) else { return Err("msa-ticket"); };
+ Ok((account, ticket))
+}
+
+async fn xsts(tokens: &TokenManager, client: &reqwest::Client) -> Result<(String, String), &'static str> {
+ let (account, ticket) = ticket(tokens, client, "user.auth.xboxlive.com", soap::PolicyReference::mbi_ssl()).await?;
+ let user = xodus::api::xbox::authenticate_xbox_user(client, ticket).await.map_err(|_| "xbox-user")?;
+ let xsts = xodus::api::xbox::request_xsts_token(client, user.token, GSSV_RELYING_PARTY).await.map_err(|_| "xsts")?;
+ Ok((account, xsts.token))
+}
+
+/// The Microsoft account token a cloud session's /connect step takes (xbox.com asks MSAL for
+/// "service::http://Passport.NET/purpose::PURPOSE_XBOX_CLOUD_CONSOLE_TRANSFER_TOKEN").
+async fn transfer_token(tokens: &TokenManager, client: &reqwest::Client) -> Result<String, &'static str> {
+ let policy = soap::PolicyReference { uri: "PURPOSE_XBOX_CLOUD_CONSOLE_TRANSFER_TOKEN".into(), val: String::default() };
+ Ok(ticket(tokens, client, "http://Passport.NET/purpose", policy).await?.1)
+}
+
+fn regions(value: &Value) -> Vec<Value> {
+ value["offeringSettings"]["regions"].as_array().map(|list| list.iter().filter_map(|region| {
+ let name = region["name"].as_str()?;
+ let base = reqwest::Url::parse(region["baseUri"].as_str()?).ok()?;
+ let host = base.host_str()?;
+ if base.scheme() != "https" || !host.ends_with(".xboxlive.com") || !base.username().is_empty() { return None; }
+ Some(json!({ "name": name, "baseUri": base.as_str().trim_end_matches('/'), "isDefault": region["isDefault"].as_bool().unwrap_or(false) }))
+ }).take(32).collect()).unwrap_or_default()
+}
+
+async fn login(tokens: &TokenManager) -> Result<Value, String> {
+ let client = reqwest::Client::builder().redirect(reqwest::redirect::Policy::none())
+ .timeout(super::session::REQUEST_TIMEOUT).user_agent("xodus-nexus-cloud/1").build().map_err(|_| "http-client")?;
+ let (account, xsts) = xsts(tokens, &client).await?;
+ let mut failures = Vec::new();
+ for offering in OFFERINGS {
+ let response = client.post(format!("https://{offering}.gssv-play-prod.xboxlive.com/v2/login/user"))
+ .header("x-gssv-client", "XboxComBrowser").header("Cache-Control", "no-store, must-revalidate, no-cache")
+ .json(&json!({ "token": xsts, "offeringId": offering })).send().await.map_err(|_| "gssv-request")?;
+ let status = response.status().as_u16();
+ let body = super::profile::bounded(response, 256 * 1024).await;
+ let Ok(body) = body else { failures.push(format!("{offering}:{status}")); continue; };
+ let value: Value = serde_json::from_slice(&body).map_err(|_| "gssv-json")?;
+ let token = value["gsToken"].as_str().filter(|token| !token.is_empty() && token.len() <= 16384).ok_or("gs-token")?;
+ // Diagnostic lines are letters and dashes only (the app's allow-list).
+ eprintln!("NEXUS_SESSION_DIAGNOSTIC_V1 gssv-login-ok-{}", if offering == "xgpuweb" { "gamepass" } else { "free" });
+ let transfer = transfer_token(tokens, &client).await;
+ eprintln!("NEXUS_SESSION_DIAGNOSTIC_V1 transfer-token-{}", if transfer.is_ok() { "ok" } else { "missing" });
+ if tokens.get_user().map_err(|_| "account-changed")?.username != account { return Err("account-changed".into()); }
+ return Ok(json!({ "schema": 1, "account": account, "offering": offering, "gsToken": token, "userToken": transfer.ok(),
+ "durationInSeconds": value["durationInSeconds"].as_u64().unwrap_or(0), "market": value["market"].as_str().unwrap_or_default(),
+ "regions": regions(&value) }));
+ }
+ Err(format!("gssv-login {}", failures.join(" ")))
+}
+
+pub async fn run(tokens: &TokenManager) -> ExitCode {
+ match tokio::time::timeout(Duration::from_secs(45), login(tokens)).await {
+ Ok(Ok(value)) => { println!("{value}"); ExitCode::SUCCESS }
+ Ok(Err(stage)) => { eprintln!("Xbox Cloud Gaming sign-in failed ({stage})."); ExitCode::FAILURE }
+ Err(_) => { eprintln!("Xbox Cloud Gaming sign-in timed out."); ExitCode::FAILURE }
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ #[test]
+ fn only_https_xboxlive_regions_are_kept() {
+ let value = json!({"offeringSettings":{"regions":[
+ {"name":"WestEurope","baseUri":"https://weu.core.gssv-play-prod.xboxlive.com/","isDefault":true},
+ {"name":"Evil","baseUri":"https://evil.test/"},
+ {"name":"Plain","baseUri":"http://eus.core.gssv-play-prod.xboxlive.com"}]}});
+ let list = regions(&value);
+ assert_eq!(list.len(), 1);
+ assert_eq!(list[0]["baseUri"], "https://weu.core.gssv-play-prod.xboxlive.com");
+ }
+}
diff --git a/crates/xodus-cli/src/main.rs b/crates/xodus-cli/src/main.rs
index 1ae4167..4b0ac26 100644
--- a/crates/xodus-cli/src/main.rs
+++ b/crates/xodus-cli/src/main.rs
@@ -68,6 +68,8 @@ enum SubCommand {
#[arg(long, default_value_t = false)]
json: bool,
},
+ #[command(about = "Get an Xbox Cloud Gaming streaming token for the stored account")]
+ CloudToken,
#[command(about = "Read Xbox gamertag and gamerpic for the stored account")]
Profile {
#[arg(long, default_value_t = false)]
@@ -239,6 +241,7 @@ async fn main() -> ExitCode {
args.command,
SubCommand::Account { .. }
| SubCommand::Profile { .. }
+ | SubCommand::CloudToken
| SubCommand::GameActivity { .. }
| SubCommand::Achievements { .. }
| SubCommand::Library { .. }
@@ -304,6 +307,7 @@ async fn main() -> ExitCode {
}
},
SubCommand::Profile { json } => commands::profile::run(&tokens, json).await,
+ SubCommand::CloudToken => commands::cloud_token::run(&tokens).await,
SubCommand::GameActivity { title_id, json } => commands::game_activity::run(&tokens, title_id, json).await,
SubCommand::Achievements { title_id, json } => commands::achievements::run(&tokens, title_id, json).await,
SubCommand::Login => commands::login::run(&client, &tokens).await,
@@ -0,0 +1,237 @@
diff --git a/crates/xodus/src/tokens/backend/keychain.rs b/crates/xodus/src/tokens/backend/keychain.rs
index 19480da..dc8f136 100644
--- a/crates/xodus/src/tokens/backend/keychain.rs
+++ b/crates/xodus/src/tokens/backend/keychain.rs
@@ -13,29 +13,24 @@ pub struct KeychainBackend;
// keyed loopback port the app also listens on is used for the rest of the process.
static LOOPBACK: AtomicBool = AtomicBool::new(false);
static REPORTED: AtomicBool = AtomicBool::new(false);
+// The app seals credentials with the Android Keystore, which took over 10 s on some phones.
+const REPLY_TIMEOUT: Duration = Duration::from_secs(30);
-/// Letters and dashes only: the app passes `NEXUS_CREDENTIAL_ERROR_V1 [a-z-]{1,48}` lines to its log.
-fn reason(error: &std::io::Error) -> &'static str {
- match error.raw_os_error() {
- Some(111) => "refused",
- Some(13) => "denied",
- Some(1) => "not-permitted",
- Some(2) => "missing",
- Some(110) => "timed-out",
- Some(38) => "unsupported",
- Some(97) => "family-unsupported",
- Some(22) => "invalid",
- Some(104) => "reset",
- Some(32) => "broken-pipe",
- _ => match error.kind() {
- std::io::ErrorKind::TimedOut | std::io::ErrorKind::WouldBlock => "timed-out",
- std::io::ErrorKind::UnexpectedEof => "closed",
- _ => "other",
- },
- }
+use crate::tokens::store::io_reason as reason;
+
+fn exchange<S: Read + Write>(stream: S, prefix: Option<&str>, value: &Value) -> Result<Value, TokenStoreError> {
+ let route = if prefix.is_some() { "loopback" } else { "socket" };
+ transfer(stream, prefix, value).inspect_err(|error| {
+ // Kind Other is the host's own refusal, already reported.
+ if let TokenStoreError::Io(error) = error
+ && error.kind() != std::io::ErrorKind::Other
+ {
+ eprintln!("NEXUS_CREDENTIAL_ERROR_V1 {route}-exchange-{}", reason(error));
+ }
+ })
}
-fn exchange<S: Read + Write>(mut stream: S, prefix: Option<&str>, value: &Value) -> Result<Value, TokenStoreError> {
+fn transfer<S: Read + Write>(mut stream: S, prefix: Option<&str>, value: &Value) -> Result<Value, TokenStoreError> {
if let Some(key) = prefix {
stream.write_all(key.as_bytes())?;
stream.write_all(b"\n")?;
@@ -45,6 +40,7 @@ fn exchange<S: Read + Write>(mut stream: S, prefix: Option<&str>, value: &Value)
let mut bytes = Vec::new();
std::io::BufReader::new(stream).take(2_097_153).read_until(b'\n', &mut bytes)?;
if bytes.len() > 2_097_152 || bytes.last() != Some(&b'\n') {
+ eprintln!("NEXUS_CREDENTIAL_ERROR_V1 host-response-invalid");
return Err(std::io::Error::other("Invalid Android credential response").into());
}
let response: Value = serde_json::from_slice(&bytes)?;
@@ -56,28 +52,36 @@ fn exchange<S: Read + Write>(mut stream: S, prefix: Option<&str>, value: &Value)
}
fn loopback(value: &Value) -> Result<Value, TokenStoreError> {
- let port: u16 = std::env::var("DROIDDECK_XBOX_CREDENTIAL_PORT").ok().and_then(|v| v.parse().ok())
- .ok_or_else(|| std::io::Error::other("Android credential port is unavailable"))?;
- let key = std::env::var("DROIDDECK_XBOX_CREDENTIAL_KEY")
- .map_err(|_| std::io::Error::other("Android credential key is unavailable"))?;
+ let port: u16 = std::env::var("DROIDDECK_XBOX_CREDENTIAL_PORT").ok().and_then(|v| v.parse().ok()).ok_or_else(|| {
+ eprintln!("NEXUS_CREDENTIAL_ERROR_V1 loopback-port-missing");
+ std::io::Error::other("Android credential port is unavailable")
+ })?;
+ let key = std::env::var("DROIDDECK_XBOX_CREDENTIAL_KEY").map_err(|_| {
+ eprintln!("NEXUS_CREDENTIAL_ERROR_V1 loopback-key-missing");
+ std::io::Error::other("Android credential key is unavailable")
+ })?;
let address = SocketAddrV4::new(Ipv4Addr::LOCALHOST, port).into();
let stream = TcpStream::connect_timeout(&address, Duration::from_secs(5)).inspect_err(|error| {
eprintln!("NEXUS_CREDENTIAL_ERROR_V1 loopback-{}", reason(error));
})?;
- stream.set_read_timeout(Some(Duration::from_secs(10)))?;
- stream.set_write_timeout(Some(Duration::from_secs(10)))?;
+ stream.set_read_timeout(Some(REPLY_TIMEOUT))?;
+ stream.set_write_timeout(Some(REPLY_TIMEOUT))?;
exchange(stream, Some(&key), value)
}
fn request(value: Value) -> Result<Value, TokenStoreError> {
if LOOPBACK.load(Ordering::Relaxed) { return loopback(&value); }
- let name = std::env::var("DROIDDECK_XBOX_CREDENTIAL_SOCKET")
- .map_err(|_| std::io::Error::other("Android credential host is unavailable"))?;
- let address = SocketAddr::from_abstract_name(name)?;
+ let name = std::env::var("DROIDDECK_XBOX_CREDENTIAL_SOCKET").map_err(|_| {
+ eprintln!("NEXUS_CREDENTIAL_ERROR_V1 socket-name-missing");
+ std::io::Error::other("Android credential host is unavailable")
+ })?;
+ let address = SocketAddr::from_abstract_name(name).inspect_err(|_| {
+ eprintln!("NEXUS_CREDENTIAL_ERROR_V1 socket-name-invalid");
+ })?;
match UnixStream::connect_addr(&address) {
Ok(stream) => {
- stream.set_read_timeout(Some(Duration::from_secs(10)))?;
- stream.set_write_timeout(Some(Duration::from_secs(10)))?;
+ stream.set_read_timeout(Some(REPLY_TIMEOUT))?;
+ stream.set_write_timeout(Some(REPLY_TIMEOUT))?;
exchange(stream, None, &value)
}
Err(error) => {
diff --git a/crates/xodus/src/tokens/session.rs b/crates/xodus/src/tokens/session.rs
index 38c4b2b..254c14f 100644
--- a/crates/xodus/src/tokens/session.rs
+++ b/crates/xodus/src/tokens/session.rs
@@ -44,14 +44,30 @@ async fn renewal_lock_at(
wait: WaitDuration,
) -> std::io::Result<std::fs::File> {
use std::os::unix::fs::{DirBuilderExt, MetadataExt, OpenOptionsExt};
+ fn report(stage: &'static str) -> impl Fn(&std::io::Error) {
+ move |error| eprintln!("NEXUS_CREDENTIAL_ERROR_V1 lock-{stage}-{}", super::store::io_reason(error))
+ }
+ fn unsafe_lock(stage: &str, check: &str) -> std::io::Error {
+ eprintln!("NEXUS_CREDENTIAL_ERROR_V1 lock-{stage}-{check}");
+ std::io::Error::other(if stage == "dir" { "Unsafe session lock directory" } else { "Unsafe session lock" })
+ }
match std::fs::DirBuilder::new().mode(0o700).create(&root) {
Ok(()) => {}
Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => {}
- Err(error) => return Err(error),
+ Err(error) => {
+ report("dir-create")(&error);
+ return Err(error);
+ }
+ }
+ let dir = std::fs::symlink_metadata(&root).inspect_err(report("dir-stat"))?;
+ if !dir.is_dir() {
+ return Err(unsafe_lock("dir", "type"));
+ }
+ if dir.uid() != uid {
+ return Err(unsafe_lock("dir", "owner"));
}
- let dir = std::fs::symlink_metadata(&root)?;
- if !dir.is_dir() || dir.uid() != uid || dir.mode() & 0o077 != 0 {
- return Err(std::io::Error::other("Unsafe session lock directory"));
+ if dir.mode() & 0o077 != 0 {
+ return Err(unsafe_lock("dir", "mode"));
}
let file = std::fs::OpenOptions::new()
.read(true)
@@ -60,10 +76,20 @@ async fn renewal_lock_at(
.truncate(false)
.mode(0o600)
.custom_flags(libc::O_NOFOLLOW | libc::O_CLOEXEC)
- .open(root.join("device.lock"))?;
- let meta = file.metadata()?;
- if !meta.is_file() || meta.uid() != uid || meta.nlink() != 1 || meta.mode() & 0o077 != 0 {
- return Err(std::io::Error::other("Unsafe session lock"));
+ .open(root.join("device.lock"))
+ .inspect_err(report("file-open"))?;
+ let meta = file.metadata().inspect_err(report("file-stat"))?;
+ if !meta.is_file() {
+ return Err(unsafe_lock("file", "type"));
+ }
+ if meta.uid() != uid {
+ return Err(unsafe_lock("file", "owner"));
+ }
+ if meta.nlink() != 1 {
+ return Err(unsafe_lock("file", "links"));
+ }
+ if meta.mode() & 0o077 != 0 {
+ return Err(unsafe_lock("file", "mode"));
}
let deadline = tokio::time::Instant::now() + wait;
loop {
@@ -75,7 +101,10 @@ async fn renewal_lock_at(
{
tokio::time::sleep(WaitDuration::from_millis(100)).await
}
- Err(error) => return Err(error),
+ Err(error) => {
+ report("flock")(&error);
+ return Err(error);
+ }
}
}
}
diff --git a/crates/xodus/src/tokens/store.rs b/crates/xodus/src/tokens/store.rs
index 78b73d0..15cbff0 100644
--- a/crates/xodus/src/tokens/store.rs
+++ b/crates/xodus/src/tokens/store.rs
@@ -50,3 +50,51 @@ impl TokenStoreError {
eprintln!("{self}");
}
}
+
+/// Letters and dashes only: DroidDeck passes `NEXUS_CREDENTIAL_ERROR_V1 [a-z-]{1,48}` lines to its
+/// log. Unlisted errno values are spelled with letters, a = 0 ... j = 9 (`os-dh` = errno 37).
+pub(crate) fn io_reason(error: &std::io::Error) -> String {
+ let known = match error.raw_os_error() {
+ Some(111) => "refused",
+ Some(13) => "denied",
+ Some(1) => "not-permitted",
+ Some(2) => "missing",
+ Some(110) => "timed-out",
+ Some(38) => "unsupported",
+ Some(97) => "family-unsupported",
+ Some(22) => "invalid",
+ Some(104) => "reset",
+ Some(32) => "broken-pipe",
+ Some(4) => "interrupted",
+ Some(11) => "would-block",
+ Some(17) => "exists",
+ Some(20) => "not-directory",
+ Some(21) => "is-directory",
+ Some(28) => "no-space",
+ Some(30) => "read-only",
+ Some(37) => "no-locks",
+ Some(40) => "symlink-loop",
+ Some(95) => "not-supported",
+ Some(code) => {
+ let letters: String = code.unsigned_abs().to_string().bytes().map(|b| (b - b'0' + b'a') as char).collect();
+ return format!("os-{letters}");
+ }
+ None => match error.kind() {
+ std::io::ErrorKind::TimedOut | std::io::ErrorKind::WouldBlock => "timed-out",
+ std::io::ErrorKind::UnexpectedEof => "closed",
+ _ => "other",
+ },
+ };
+ known.to_string()
+}
+
+#[cfg(test)]
+mod io_reason_tests {
+ #[test]
+ fn io_reason_is_letters_and_dashes() {
+ for (code, expected) in [(37, "no-locks"), (13, "denied"), (99, "os-jj"), (125, "os-bcf")] {
+ assert_eq!(super::io_reason(&std::io::Error::from_raw_os_error(code)), expected);
+ }
+ assert_eq!(super::io_reason(&std::io::Error::other("x")), "other");
+ }
+}
@@ -0,0 +1,113 @@
diff --git a/crates/xodus/src/tokens/backend/keychain.rs b/crates/xodus/src/tokens/backend/keychain.rs
index 1903638..19480da 100644
--- a/crates/xodus/src/tokens/backend/keychain.rs
+++ b/crates/xodus/src/tokens/backend/keychain.rs
@@ -1,20 +1,46 @@
use crate::tokens::store::{TokenBackend, TokenStoreError};
use serde_json::{Value, json};
use std::io::{BufRead, Read, Write};
+use std::net::{Ipv4Addr, SocketAddrV4, TcpStream};
use std::os::linux::net::SocketAddrExt;
use std::os::unix::net::{SocketAddr, UnixStream};
+use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Duration;
pub struct KeychainBackend;
-fn request(value: Value) -> Result<Value, TokenStoreError> {
- let name = std::env::var("DROIDDECK_XBOX_CREDENTIAL_SOCKET")
- .map_err(|_| std::io::Error::other("Android credential host is unavailable"))?;
- let address = SocketAddr::from_abstract_name(name)?;
- let mut stream = UnixStream::connect_addr(&address)?;
- stream.set_read_timeout(Some(Duration::from_secs(10)))?;
- stream.set_write_timeout(Some(Duration::from_secs(10)))?;
- serde_json::to_writer(&mut stream, &value)?;
+// Some Android devices refuse the abstract socket from inside PRoot; after one failure the
+// keyed loopback port the app also listens on is used for the rest of the process.
+static LOOPBACK: AtomicBool = AtomicBool::new(false);
+static REPORTED: AtomicBool = AtomicBool::new(false);
+
+/// Letters and dashes only: the app passes `NEXUS_CREDENTIAL_ERROR_V1 [a-z-]{1,48}` lines to its log.
+fn reason(error: &std::io::Error) -> &'static str {
+ match error.raw_os_error() {
+ Some(111) => "refused",
+ Some(13) => "denied",
+ Some(1) => "not-permitted",
+ Some(2) => "missing",
+ Some(110) => "timed-out",
+ Some(38) => "unsupported",
+ Some(97) => "family-unsupported",
+ Some(22) => "invalid",
+ Some(104) => "reset",
+ Some(32) => "broken-pipe",
+ _ => match error.kind() {
+ std::io::ErrorKind::TimedOut | std::io::ErrorKind::WouldBlock => "timed-out",
+ std::io::ErrorKind::UnexpectedEof => "closed",
+ _ => "other",
+ },
+ }
+}
+
+fn exchange<S: Read + Write>(mut stream: S, prefix: Option<&str>, value: &Value) -> Result<Value, TokenStoreError> {
+ if let Some(key) = prefix {
+ stream.write_all(key.as_bytes())?;
+ stream.write_all(b"\n")?;
+ }
+ serde_json::to_writer(&mut stream, value)?;
stream.write_all(b"\n")?;
let mut bytes = Vec::new();
std::io::BufReader::new(stream).take(2_097_153).read_until(b'\n', &mut bytes)?;
@@ -22,10 +48,52 @@ fn request(value: Value) -> Result<Value, TokenStoreError> {
return Err(std::io::Error::other("Invalid Android credential response").into());
}
let response: Value = serde_json::from_slice(&bytes)?;
- if response["ok"] != true { return Err(std::io::Error::other("Android credential storage unavailable").into()); }
+ if response["ok"] != true {
+ eprintln!("NEXUS_CREDENTIAL_ERROR_V1 host-refused");
+ return Err(std::io::Error::other("Android credential storage unavailable").into());
+ }
Ok(response)
}
+fn loopback(value: &Value) -> Result<Value, TokenStoreError> {
+ let port: u16 = std::env::var("DROIDDECK_XBOX_CREDENTIAL_PORT").ok().and_then(|v| v.parse().ok())
+ .ok_or_else(|| std::io::Error::other("Android credential port is unavailable"))?;
+ let key = std::env::var("DROIDDECK_XBOX_CREDENTIAL_KEY")
+ .map_err(|_| std::io::Error::other("Android credential key is unavailable"))?;
+ let address = SocketAddrV4::new(Ipv4Addr::LOCALHOST, port).into();
+ let stream = TcpStream::connect_timeout(&address, Duration::from_secs(5)).inspect_err(|error| {
+ eprintln!("NEXUS_CREDENTIAL_ERROR_V1 loopback-{}", reason(error));
+ })?;
+ stream.set_read_timeout(Some(Duration::from_secs(10)))?;
+ stream.set_write_timeout(Some(Duration::from_secs(10)))?;
+ exchange(stream, Some(&key), value)
+}
+
+fn request(value: Value) -> Result<Value, TokenStoreError> {
+ if LOOPBACK.load(Ordering::Relaxed) { return loopback(&value); }
+ let name = std::env::var("DROIDDECK_XBOX_CREDENTIAL_SOCKET")
+ .map_err(|_| std::io::Error::other("Android credential host is unavailable"))?;
+ let address = SocketAddr::from_abstract_name(name)?;
+ match UnixStream::connect_addr(&address) {
+ Ok(stream) => {
+ stream.set_read_timeout(Some(Duration::from_secs(10)))?;
+ stream.set_write_timeout(Some(Duration::from_secs(10)))?;
+ exchange(stream, None, &value)
+ }
+ Err(error) => {
+ if !REPORTED.swap(true, Ordering::Relaxed) {
+ eprintln!("NEXUS_CREDENTIAL_ERROR_V1 socket-{}", reason(&error));
+ }
+ let result = loopback(&value);
+ if result.is_ok() {
+ LOOPBACK.store(true, Ordering::Relaxed);
+ eprintln!("NEXUS_CREDENTIAL_ERROR_V1 loopback-in-use");
+ }
+ result
+ }
+ }
+}
+
impl TokenBackend for KeychainBackend {
fn get(&self, key: &str) -> Result<Option<Vec<u8>>, TokenStoreError> {
let response = request(json!({"op":"get", "key":key}))?;
+2 -1
View File
@@ -9,7 +9,8 @@ import sys
recipe = Path(__file__).resolve().parent
cache = Path(sys.argv[1]).resolve()
lock = json.loads((recipe / 'backend.lock.json').read_text())
local_patches = ['droiddeck-credentials.patch', 'droiddeck-launch-view.patch', 'droiddeck-library-subscriptions.patch']
local_patches = ['droiddeck-credentials.patch', 'droiddeck-launch-view.patch', 'droiddeck-library-subscriptions.patch',
'droiddeck-credential-fallback.patch', 'droiddeck-credential-diagnostics.patch', 'droiddeck-cloud-token.patch']
identity = hashlib.sha256((recipe / 'backend.lock.json').read_bytes() + b''.join((recipe / name).read_bytes() for name in local_patches)).hexdigest()
source = cache / ('source-' + identity[:16])
if not (source / '.prepared').is_file():
+2 -1
View File
@@ -12,6 +12,7 @@
"windows.web.dll": "1a510c73ca31475b163692337d3cf37fef3381078df9729662095fc92b2e4e83",
"libnghttp2.dll": "2b171eba9296b207fe6016560e5381bc6e7405f3ad5ee5cd4add316358204ec0",
"xgameruntime.dll": "060120e0ec6bccfbab516128315a0658b11d3e316f62a917c653daf1a1dd3e05",
"winhttp.dll": "81e1da7a731d83fbd24dbe2fa2ecb8f155bd477a9f9b6f4cc3932063cc326c27"
"winhttp.dll": "81e1da7a731d83fbd24dbe2fa2ecb8f155bd477a9f9b6f4cc3932063cc326c27",
"wintypes.dll": "7d06083e3d8b3a7e11555a91e7918f11c7d444a74755d41c8d1990db3697c895"
}
}
+20
View File
@@ -0,0 +1,20 @@
# Xbox Cloud Gaming player
`greenlight/lib` is the WebRTC client of **Greenlight** by **UnknownSKL (Jim Kroon)**
(https://github.com/unknownskl/greenlight, `packages/player/src/client/lib`, commit ef852c3, MIT —
see `greenlight/LICENSE`). DroidDeck changes: `player.ts` renders video with the `<video>`
component instead of WebGPU, `render/video.ts` sets a transparent poster, and `input/gamepad.ts` treats missing buttons/axes as unpressed (Android reports no guide button); `uuid` is replaced by `uuid.ts` (crypto.randomUUID).
`sdp-tweaks.ts` (H.264 profile preference, bitrate cap), the CAS / unsharp-mask sharpening in
`renderer.ts` and the "stop quality drops" metadata change follow **Better xCloud** by **redphx**
(https://github.com/redphx/better-xcloud, MIT). The colour, deband, FXAA, Toon, CRT and NTSC shaders are
ported from Bannerlator's screen effects (The412Banner). `streamstats.ts` is DroidDeck's.
`entry.ts` is DroidDeck's: it drives a cloud session through the app (`DDCloud` bridge; the
streaming and Microsoft account tokens never reach the page) and attaches the first controller.
Build: `bash tools/xcloud-player/build.sh` (esbuild) writes
`app/src/main/assets/xcloud/player.js`; the built file is committed.
The page, its sources and the Android side (`stores/xbox/XboxCloud*.kt`) are ported from
Bannerlator's Xbox Cloud Gaming player (https://github.com/The412Banner/Bannerlator).
+9
View File
@@ -0,0 +1,9 @@
#!/usr/bin/env bash
# Bundles the cloud player page script with esbuild 0.24.0 (ESBUILD=/path/to/esbuild, default: esbuild on PATH).
set -euo pipefail
here=$(cd "$(dirname "$0")" && pwd)
repo=$(cd "$here/../.." && pwd)
"${ESBUILD:-esbuild}" "$here/entry.ts" --bundle --format=iife --target=chrome110 --minify \
--alias:uuid="$here/uuid.ts" --legal-comments=inline \
--banner:js="/* DroidDeck Xbox Cloud Gaming player. Includes Greenlight's player (c) UnknownSKL (Jim Kroon), MIT. */" \
--outfile="$repo/app/src/main/assets/xcloud/player.js"
+212
View File
@@ -0,0 +1,212 @@
// DroidDeck cloud player page: runs one Xbox Cloud Gaming session. Every request to Microsoft goes
// through the app (DDCloud), which holds the tokens; this page only does WebRTC, video, sound and input.
import xCloudPlayer from './greenlight/lib/player'
import Gamepad from './greenlight/lib/input/gamepad'
import { EffectRenderer, Effects, needsRenderer } from './renderer'
import { StreamStats } from './streamstats'
import { capVideoBitrate, preferH264Profile } from './sdp-tweaks'
interface Reply { status: number, body: string }
declare const DDCloud: { call(id: number, op: string, arg: string): void, status(text: string): void, failed(stage: string, detail: string): void, playing(): void, log(text: string): void, settings(): string }
/** The player settings the app hands over (XboxCloudSettings.toJson); stream-start ones are applied by the app. */
interface Settings extends Effects {
bitrateMbps: number // 0 = Microsoft's own limit
codec: string // 'default' | 'high' | 'normal' | 'low'
preventDrops: boolean
statsLevel: number // 0 off, 1 fps, 2 basic, 3 detailed, 4 full
statsLayout: string // 'horizontal' | 'vertical'
statsCorner: string // 'right' | 'left'
server: string // the region's host, for the full overlay
volume: number // 100..300 %
rumble: number // 0..100 %
}
let settings: Settings = JSON.parse(DDCloud.settings())
// Codec profile and bitrate cap go into our offer (Better xCloud's approach).
const nativeSetLocalDescription = RTCPeerConnection.prototype.setLocalDescription
RTCPeerConnection.prototype.setLocalDescription = function (this: RTCPeerConnection, description?: RTCLocalSessionDescriptionInit) {
if (description?.sdp) {
if (settings.codec === 'high' || settings.codec === 'normal' || settings.codec === 'low') description.sdp = preferH264Profile(description.sdp, settings.codec)
if (settings.bitrateMbps > 0) description.sdp = capVideoBitrate(description.sdp, settings.bitrateMbps * 1000)
}
return nativeSetLocalDescription.call(this, description as RTCLocalSessionDescriptionInit)
} as typeof RTCPeerConnection.prototype.setLocalDescription
;(window as any).__ddRumble = settings.rumble / 100
// Page errors go to the app's Xbox log (no tokens ever reach this page).
window.addEventListener('error', event => DDCloud.log(`error: ${event.message} @${event.lineno}`))
window.addEventListener('unhandledrejection', event => DDCloud.log(`rejection: ${String(event.reason).slice(0, 200)}`))
window.addEventListener('gamepadconnected', event => DDCloud.log(`controller: ${event.gamepad.id.slice(0, 60)} mapping=${event.gamepad.mapping || 'none'} buttons=${event.gamepad.buttons.length} axes=${event.gamepad.axes.length}`))
const pending = new Map<number, (reply: Reply) => void>()
let nextId = 1
;(window as any).__ddResolve = (id: number, status: number, body: string) => {
const done = pending.get(id)
pending.delete(id)
if (done) done({ status, body })
}
function call(op: string, arg: unknown = ''): Promise<Reply> {
return new Promise(resolve => {
const id = nextId++
pending.set(id, resolve)
DDCloud.call(id, op, typeof arg === 'string' ? arg : JSON.stringify(arg))
})
}
const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms))
const ok = (reply: Reply) => reply.status >= 200 && reply.status < 300
function show(text: string) {
const element = document.getElementById('status')
if (element) element.textContent = text
DDCloud.status(text)
}
class Failure extends Error {
constructor(public stage: string, public detail: string) { super(stage) }
}
function expect(stage: string, reply: Reply): Reply {
if (!ok(reply)) throw new Failure(stage, `${reply.status} ${reply.body.slice(0, 300)}`)
return reply
}
function waitForCandidates(pc: RTCPeerConnection, timeout: number): Promise<void> {
return new Promise(resolve => {
if (pc.iceGatheringState === 'complete') return resolve()
const timer = setTimeout(resolve, timeout)
pc.addEventListener('icegatheringstatechange', () => {
if (pc.iceGatheringState === 'complete') { clearTimeout(timer); resolve() }
})
})
}
async function run() {
show('Starting the game on Xbox Cloud Gaming…')
expect('start', await call('start'))
let connected = false
let waited = 0
for (;;) {
const reply = expect('state', await call('state'))
const state = JSON.parse(reply.body || '{}')
if (state.state === 'Provisioned') break
if (state.state === 'Failed') throw new Failure('provision', JSON.stringify(state.errorDetails ?? {}).slice(0, 300))
if (state.state === 'ReadyToConnect' && !connected) {
connected = true
show('Signing in to the cloud console…')
expect('connect', await call('connect'))
} else if (state.state === 'WaitingForResources') {
show(`In the queue for a cloud console… ${Math.round(waited / 1000)} s`)
} else {
show(`Getting a cloud console ready… ${Math.round(waited / 1000)} s`)
}
await sleep(1000)
waited += 1000
}
show('Connecting…')
const player = new xCloudPlayer('player')
player.init()
const pc = player._peerConnection
// "Stop quality drops": report a short decode time so the server does not lower the resolution
// for a slow decoder (Better xCloud's patchStreamMetadata).
const input = player._channels.input as any
const queueMetadata = input.queueMetadataFrame.bind(input)
input.queueMetadataFrame = (frame: any) => {
if (settings.preventDrops) {
const render = frame.frameRenderedTimeMs - frame.frameDecodedTimeMs
frame.frameDecodedTimeMs = frame.frameSubmittedTimeMs + 10
frame.frameRenderedTimeMs = frame.frameDecodedTimeMs + render
}
queueMetadata(frame)
}
// Volume above 100 %: the stream's sound goes through a gain node instead of the <audio> element.
let gain: GainNode | undefined
let audioStream: MediaStream | undefined
const applyVolume = () => {
if (audioStream === undefined || (gain === undefined && settings.volume === 100)) return
if (gain === undefined) {
const context = new AudioContext()
gain = context.createGain()
context.createMediaStreamSource(audioStream).connect(gain).connect(context.destination)
const element = document.querySelector('#player audio') as HTMLAudioElement | null
if (element) element.muted = true
}
gain.gain.value = settings.volume / 100
}
pc.addEventListener('track', event => {
if (event.track.kind === 'audio') { audioStream = event.streams[0]; applyVolume() }
})
const stats = new StreamStats(pc)
let renderer: EffectRenderer | undefined
const applyEffects = () => {
const video = player.getVideoElement() as HTMLVideoElement | undefined
if (video === undefined) return
if (!needsRenderer(settings)) { renderer?.pause(); return }
if (renderer === undefined) {
try { renderer = new EffectRenderer(video, settings) }
catch (error) { DDCloud.log(`effects unavailable: ${String(error).slice(0, 200)}`) }
} else renderer.resume(settings)
}
// Live changes from the in-player menu.
;(window as any).__ddApply = (json: string) => {
settings = JSON.parse(json)
;(window as any).__ddRumble = settings.rumble / 100
stats.show(settings.statsLevel, settings.statsLayout === 'vertical', settings.server, settings.statsCorner === 'left')
applyVolume()
applyEffects()
}
pc.addEventListener('connectionstatechange', () => {
if (pc.connectionState === 'connected') {
show(''); DDCloud.playing()
stats.show(settings.statsLevel, settings.statsLayout === 'vertical', settings.server, settings.statsCorner === 'left')
// The video element exists once its track arrived; give it a moment for its first frame.
setTimeout(applyEffects, 500)
}
else if (pc.connectionState === 'failed') DDCloud.failed('webrtc', 'connection failed')
else if (pc.connectionState === 'disconnected') show('Connection lost…')
})
const offer = await player.createOffer()
const sdp = expect('sdp', await call('sdp', offer.sdp ?? ''))
const answer = JSON.parse(JSON.parse(sdp.body).exchangeResponse)
if (answer.status && answer.status !== 'success') throw new Failure('sdp-answer', JSON.stringify(answer).slice(0, 300))
await player.setRemoteSDP(answer.sdp)
await waitForCandidates(pc, 3000)
const local = (await player.getICECandidates()).map(c => JSON.stringify({
candidate: c.candidate, sdpMid: c.sdpMid, sdpMLineIndex: c.sdpMLineIndex, usernameFragment: c.usernameFragment,
}))
const ice = expect('ice', await call('ice', local))
player.setRemoteIceCandidates(JSON.parse(JSON.parse(ice.body).exchangeResponse))
// The control channel's authorization (sent on the message handshake) announces controller 0 and
// then removes it, clearing any handler attached before; attach the controller after that.
const handshake = Date.now()
while (!player._channels.message._handshakeCompleted && Date.now() - handshake < 20_000) await sleep(100)
DDCloud.log(`handshake ${player._channels.message._handshakeCompleted ? 'done' : 'missing'} after ${Date.now() - handshake} ms`)
await sleep(300)
const pad = new Gamepad(0, { enable_keyboard: false })
pad.attach(player)
DDCloud.log(`controllers at attach: ${Array.from(navigator.getGamepads()).filter(Boolean).length}`)
// The app's "Xbox button" (in the leave dialog) presses Nexus for a moment.
;(window as any).__ddNexus = () => { pad.sendButtonState('Nexus', 1); setTimeout(() => pad.sendButtonState('Nexus', 0), 200) }
// The app's menu button / a controller's Guide button: Nexus follows the physical button.
;(window as any).__ddButton = (name: string, value: number) => pad.sendButtonState(name, value)
// The in-player menu pauses controller input to the game while it is open.
;(window as any).__ddInput = (enabled: boolean) => { if (enabled) pad.onWindowFocus(); else pad.onWindowBlur() }
setInterval(() => { call('keepalive') }, 30_000)
;(window as any).__ddStop = () => { try { renderer?.destroy(); stats.destroy(); pad.detach(); player.destroy() } catch (e) { /* closing anyway */ } }
}
run().catch((error: unknown) => {
if (error instanceof Failure) DDCloud.failed(error.stage, error.detail)
else DDCloud.failed('page', String(error).slice(0, 300))
})
+21
View File
@@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2019 UnknownSKL (Jim Kroon)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
@@ -0,0 +1,69 @@
import xCloudPlayer from '../player'
export default class Channel {
private _player:xCloudPlayer
private _dataChannel:RTCDataChannel
constructor(player:xCloudPlayer) {
this._player = player
// console.log('creating data channel: ', this.constructor.name, this, this.getChannelConfig())
this._dataChannel = this._player._peerConnection.createDataChannel(this.getChannelName(), this.getChannelConfig())
this._dataChannel.onopen = this.onOpen.bind(this)
this._dataChannel.onmessage = this.onMessage.bind(this)
this._dataChannel.onclosing = this.onClosing.bind(this)
this._dataChannel.onclose = this.onClose.bind(this)
this._dataChannel.onerror = this.onError.bind(this)
}
// Channel config functions
getChannelName() {
return 'channel'
}
getChannelConfig() {
return { }
}
// Default channel functions
onOpen(event:Event) {
console.log(this.getChannelName(), 'Opening channel:', event)
}
onMessage(event:MessageEvent) {
console.log(this.getChannelName(), 'Message channel:', event)
}
onClosing(event:Event) {
console.log(this.getChannelName(), 'Closing channel:', event)
}
onClose(event:Event) {
console.log(this.getChannelName(), 'Closed channel:', event)
}
onError(event:Event) {
console.log(this.getChannelName(), 'Error channel:', event)
}
send(data:any) {
if(this._dataChannel.readyState !== 'open'){
console.log('['+this.getChannelName()+'] Tried to send data while channel is not open. readyState is:', this._dataChannel.readyState)
return
}
if(typeof data === 'string'){
data = (new TextEncoder).encode(data)
}
return this._dataChannel.send(data)
}
getPlayer(){
return this._player
}
// Channel destroy function
destroy() {}
}
@@ -0,0 +1,84 @@
import Channel from './channel'
export default class ChatChannel extends Channel {
_micPermissions:'granted'|'denied'|'prompt' = 'prompt'
_micStream:MediaStream | undefined
constructor(player:any){
super(player)
navigator.permissions.query({ name: 'microphone' }).then((permissionStatus) => {
this._micPermissions = permissionStatus.state
permissionStatus.onchange = () => {
this._micPermissions = permissionStatus.state
}
})
}
getChannelName() {
return 'chat'
}
getChannelConfig() {
return {
ordered: true,
protocol: 'chatV1',
}
}
// startMicrophone() {
// if(this._micPermissions === 'denied'){
// alert('Microphone permissions are denied. Please enable them in your browser settings.')
// return
// }
// if(this._micStream !== undefined){
// console.log('Microphone already started. You need to stop it first by calling stopMicrophone()')
// return
// }
// navigator.mediaDevices.getUserMedia({ audio: {
// channelCount: 1,
// sampleRate: 24e3,
// } }).then((stream) => {
// if(this._micStream !== undefined){
// return
// }
// this._micStream = stream
// // Add to SDP
// this.getPlayer()._peerConnection.addTrack(stream.getAudioTracks()[0], stream)
// this.getPlayer().createOffer().then((offer) => {
// if(this.getPlayer()._sdpHandler){
// this.getPlayer()._sdpHandler(offer)
// } else {
// console.log('No SDP handler set. Set an SDP Handler via player.setChatSdpHandler()')
// }
// })
// }).catch((err) => {
// alert(`Error connecting to microphone: ${err}`)
// })
// }
// stopMicrophone() {
// if(this._micStream === undefined) {return}
// this._micStream.getTracks().forEach((track) => {
// track.stop()
// })
// this.getPlayer()._peerConnection.getSenders().forEach((sender) => {
// if(sender.track && sender.track.kind === 'audio'){
// this.getPlayer()._peerConnection.removeTrack(sender)
// }
// })
// this._micStream = undefined
// }
destroy() {
// console.log('DebugChannel destroy() called')
}
}
@@ -0,0 +1,108 @@
import Gamepad from '../input/gamepad'
import MouseKeyboard from '../input/mousekeyboard'
import Touch from '../input/touch'
import Channel from './channel'
interface GamepadHandlers {
0: undefined|Gamepad|MouseKeyboard|Touch;
1: undefined|Gamepad|MouseKeyboard|Touch;
2: undefined|Gamepad|MouseKeyboard|Touch;
3: undefined|Gamepad|MouseKeyboard|Touch;
}
export default class ControlChannel extends Channel {
_keyframeInterval
_gamepadHandlers:GamepadHandlers = {
0: undefined,
1: undefined,
2: undefined,
3: undefined,
}
getChannelName() {
return 'control'
}
getChannelConfig() {
return {
protocol: 'controlV1',
ordered: true,
}
}
destroy() {
// console.log('DebugChannel destroy() called')
clearInterval(this._keyframeInterval)
this._gamepadHandlers[0]?.detach()
this._gamepadHandlers[1]?.detach()
this._gamepadHandlers[2]?.detach()
this._gamepadHandlers[3]?.detach()
}
sendAuthorization(){
const authRequest = JSON.stringify({
'message':'authorizationRequest',
'accessKey':'4BDB3609-C1F1-4195-9B37-FEFF45DA8B8E',
})
this.send(authRequest)
this.sendGamepadState(0, true)
this.sendGamepadState(0, false)
// if(this.getPlayer()._config.keyframe_interval > 0) {
// this._keyframeInterval = setInterval(() => {
// this.requestKeyframeRequest(true)
// }, this.getPlayer()._config.keyframe_interval*1000)
// }
}
sendGamepadState(gamepadIndex, wasAdded = true, handler:undefined|Gamepad|MouseKeyboard|Touch = undefined) {
if(wasAdded === true){
if(handler !== undefined){
if(this._gamepadHandlers[gamepadIndex] !== undefined){
this._gamepadHandlers[gamepadIndex]?.detach()
}
this._gamepadHandlers[gamepadIndex] = handler
}
} else {
this._gamepadHandlers[gamepadIndex] = undefined
}
const gamepadRequest = JSON.stringify({
'message': 'gamepadChanged',
'gamepadIndex': gamepadIndex,
'wasAdded': wasAdded,
})
this.send(gamepadRequest)
}
requestKeyframeRequest(ifrRequested = false) {
// console.log('xCloudPlayer Channel/Control.ts - ['+this._channelName+'] User requested Video KeyFrame')
const keyframeRequest = JSON.stringify({
message: 'videoKeyframeRequested',
ifrRequested: ifrRequested,
})
this.send(keyframeRequest)
}
getGamepadHandlers(){
return this._gamepadHandlers
}
getGamepadHandler(index):Gamepad|undefined{
for(const gamepad in this.getPlayer()._channels.control.getGamepadHandlers()){
if(
this.getPlayer()._channels.control.getGamepadHandlers()[gamepad] instanceof Gamepad &&
(this.getPlayer()._channels.control.getGamepadHandlers()[gamepad] as Gamepad).getPhysicalGamepadId() === index
) {
return this.getPlayer()._channels.control.getGamepadHandlers()[gamepad]
}
}
return undefined
}
}
@@ -0,0 +1,15 @@
import Channel from './channel'
export default class DebugChannel extends Channel {
getChannelName() {
return 'debug'
}
getChannelConfig() {
return { }
}
destroy() {
// console.log('DebugChannel destroy() called')
}
}
@@ -0,0 +1,142 @@
import Channel from './channel'
import InputPacket, { MetadataFrame, ReportTypes, InputFrame as GamepadFrame, MouseFrame, KeyboardFrame, PointerFrame } from './input/packet'
import InputQueue from './input/queue'
import Gamepad from '../input/gamepad'
export interface VibrationFrame {
gamepadIndex: number;
leftMotorPercent: number;
rightMotorPercent: number;
leftTriggerMotorPercent: number;
rightTriggerMotorPercent: number;
durationMs: number;
delayMs: number;
repeat: number;
}
export default class InputChannel extends Channel {
private _serverVideoWidth = 0
private _serverVideoHeight = 0
private _inputQueue = new InputQueue(this.getPlayer())
getChannelName() {
return 'input'
}
getChannelConfig() {
return {
ordered: true,
protocol: '1.0',
}
}
getServerVideoWidth(){
return this._serverVideoWidth
}
getServerVideoHeight(){
return this._serverVideoHeight
}
start(){
const Packet = new InputPacket(0)
Packet.setMetadata(navigator.maxTouchPoints > 1 ? navigator.maxTouchPoints : 1)
this.send(Packet.toBuffer())
console.log('[InputChannel] Sent metadata:', Packet, Packet.toBuffer())
setTimeout(this.gamepadStateLoop.bind(this), 16)
}
onMessage(event: MessageEvent<any>) {
const dataView = new DataView(event.data)
const reportType = dataView.getUint8(0)
console.log('[InputChannel] received:', dataView)
if(reportType === ReportTypes.Vibration){
console.log('Received a vibration report')
dataView.getUint8(2) // rumbleType: 0 = FourMotorRumble
const gamepadIndex = dataView.getUint8(3)
const gamepad = this.getPlayer()._channels.control.getGamepadHandler(gamepadIndex)
if(gamepad !== undefined){
const leftMotorPercent = dataView.getUint8(4) / 100
const rightMotorPercent = dataView.getUint8(5) / 100
const leftTriggerMotorPercent = dataView.getUint8(6) / 100
const rightTriggerMotorPercent = dataView.getUint8(7) / 100
const durationMs = dataView.getUint16(8, true)
const delayMs = dataView.getUint16(10, true)
const repeat = dataView.getUint8(12)
const rumble:VibrationFrame = {
gamepadIndex,
leftMotorPercent,
rightMotorPercent,
leftTriggerMotorPercent,
rightTriggerMotorPercent,
durationMs,
delayMs,
repeat,
}
gamepad.handleVibration(rumble)
} else {
console.log('[InputChannel] Received a vibration report but no gamepad handler is available')
}
} else if(reportType === ReportTypes.ServerMetadata){
console.log('[InputChannel] Received server video dimensions:', dataView.getUint32(2, true), dataView.getUint32(6, true))
this._serverVideoHeight = dataView.getUint32(2, true)
this._serverVideoWidth = dataView.getUint32(6, true)
}
}
gamepadStateLoop(){
const gamepadHandlers = this.getPlayer()._channels.control.getGamepadHandlers()
// console.log('[InputChannel] gamepadStateLoop() called', gamepadHandlers)
const gamepadFrames:Array<GamepadFrame> = []
for(const gamepad in gamepadHandlers){
if(gamepadHandlers[gamepad] instanceof Gamepad){
const frame = (gamepadHandlers[gamepad] as Gamepad).getGamepadState()
if(frame !== undefined){
gamepadFrames.push(frame)
}
}
}
if(gamepadFrames.length > 0){
this.queueGamepadFrames(gamepadFrames)
}
setTimeout(this.gamepadStateLoop.bind(this), 16)
}
queueMetadataFrame(data:MetadataFrame){
return this._inputQueue.queueMetadataFrame(data)
}
queueGamepadFrames(frames:Array<GamepadFrame>, forceSend = false){
this._inputQueue.queueGamepadFrames(frames, forceSend)
}
queueMouseFrame(data:MouseFrame){
this._inputQueue.queueMouseFrame(data)
}
queueKeyboardFrame(data:KeyboardFrame){
this._inputQueue.queueKeyboardFrame(data)
}
queuePointerFrame(data:PointerFrame){
this._inputQueue.queuePointerFrame(data)
}
destroy() {
// console.log('DebugChannel destroy() called')
}
}
@@ -0,0 +1,404 @@
// import { InputFrame, PointerFrame, MouseFrame, KeyboardFrame } from '../Input'
export interface InputFrame {
GamepadIndex: number;
Nexus: number;
Menu: number;
View: number;
A: number;
B: number;
X: number;
Y: number;
DPadUp: number;
DPadDown: number;
DPadLeft: number;
DPadRight: number;
LeftShoulder: number;
RightShoulder: number;
LeftThumb: number;
RightThumb: number;
LeftThumbXAxis: number;
LeftThumbYAxis: number;
RightThumbXAxis: number;
RightThumbYAxis: number;
LeftTrigger: number;
RightTrigger: number;
}
export interface PointerFrame {
events: Array<any>;
}
export interface MouseFrame {
X: number;
Y: number;
WheelX: number;
WheelY: number;
Buttons: number;
Relative: number;
}
export interface KeyboardFrame {
pressed: boolean;
keyCode: number;
key: string;
}
export enum ReportTypes {
None = 0,
Metadata = 1,
Gamepad = 2,
Pointer = 4,
ClientMetadata = 8,
ServerMetadata = 16,
Mouse = 32,
Keyboard = 64,
Vibration = 128,
Sensor = 256,
UnreliableInputReport = 512,
UnreliableInputAck = 1024,
}
export interface MetadataFrame {
serverDataKey: number;
firstFramePacketArrivalTimeMs: number;
frameSubmittedTimeMs: number;
frameDecodedTimeMs: number;
frameRenderedTimeMs: number;
}
export interface StreamConfig {
width: number;
height: number;
}
export default class InputPacket {
_reportType = ReportTypes.None
_totalSize = -1
_sequence = -1
_metadataFrames:Array<MetadataFrame> = []
_gamepadFrames:Array<InputFrame> = []
_pointerFrames:Array<PointerFrame> = []
_mouseFrames:Array<MouseFrame> = []
_keyboardFrames:Array<KeyboardFrame> = []
_maxTouchpoints = 0
_serverVideoWidth = 0
_serverVideoHeight = 0
constructor(sequence:number){
this._sequence = sequence
}
setStreamConfig(options:StreamConfig){
this._serverVideoWidth = options.width
this._serverVideoHeight = options.height
}
setMetadata(maxTouchpoints = 1){
this._reportType = ReportTypes.ClientMetadata
this._totalSize = 15
this._maxTouchpoints = maxTouchpoints
}
setData(metadataQueue:Array<any>, gamepadQueue:Array<InputFrame>, pointerQueue:Array<PointerFrame>, mouseQueue:Array<MouseFrame>, keyboardQueue:Array<KeyboardFrame>){
let size = 14
if(metadataQueue.length > 0){
this._reportType |= ReportTypes.Metadata
size = size+this._calculateMetadataSize(metadataQueue)
this._metadataFrames = metadataQueue
}
if(gamepadQueue.length > 0){
this._reportType |= ReportTypes.Gamepad
size = size+this._calculateGamepadSize(gamepadQueue)
this._gamepadFrames = gamepadQueue
}
if(pointerQueue.length > 0){
this._reportType |= ReportTypes.Pointer
size = size+this._calculatePointerSize(pointerQueue)
this._pointerFrames = pointerQueue
}
if(mouseQueue.length > 0){
this._reportType |= ReportTypes.Mouse
size = size+this._calculateMouseSize(mouseQueue)
this._mouseFrames = mouseQueue
}
if(keyboardQueue.length > 0){
this._reportType |= ReportTypes.Keyboard
size = size+this._calculateKeyboardSize(keyboardQueue)
this._keyboardFrames = keyboardQueue
}
this._totalSize = size
}
_calculateMetadataSize(frames:Array<MetadataFrame>){
return 1 + ((7 * 4) * frames.length)
}
_calculateGamepadSize(frames:Array<InputFrame>){
return 1 + (23 * frames.length)
}
_calculatePointerSize(frames:Array<PointerFrame>){
let pointerSize = 1
for(const frame in frames){
pointerSize = pointerSize + 1 + (frames[frame].events.length * 20)
}
return pointerSize
}
_calculateMouseSize(frames:Array<MouseFrame>){
return 1 + (18 * frames.length)
}
_calculateKeyboardSize(frames:Array<KeyboardFrame>){
return 1 + (3 * frames.length)
}
_writeMetadataData(packet:DataView, offset:number, frames:Array<MetadataFrame>){
packet.setUint8(offset, frames.length)
offset++
if(frames.length >= 30){
console.warn('metadataQueue is bigger then 30. This might impact reliability!')
}
for(let i = 0; i < frames.length; i++) {
const frame = frames[i]
const firstFramePacketArrivalTimeMs = frame.firstFramePacketArrivalTimeMs
const frameSubmittedTimeMs = frame.frameSubmittedTimeMs
const frameDecodedTimeMs = frame.frameDecodedTimeMs
const frameRenderedTimeMs = frame.frameRenderedTimeMs
const framePacketTime = performance.now()
const frameDateNow = performance.now()
packet.setUint32(offset, frame.serverDataKey, true)
packet.setUint32(offset+4, firstFramePacketArrivalTimeMs, true)
packet.setUint32(offset+8, frameSubmittedTimeMs, true)
packet.setUint32(offset+12, frameDecodedTimeMs, true)
packet.setUint32(offset+16, frameRenderedTimeMs, true)
packet.setUint32(offset+20, framePacketTime, true)
packet.setUint32(offset+24, frameDateNow, true)
offset += 28
}
return offset
}
_writeGamepadData(packet:DataView, offset:number, frames:Array<InputFrame>){
packet.setUint8(offset, frames.length)
offset++
if(frames.length >= 30){
console.warn('gamepadQueue is bigger then 30. This might impact reliability!')
}
for(let i = 0; i < frames.length; i++) {
const input:InputFrame = frames[i]
packet.setUint8(offset, input.GamepadIndex)
offset++
let buttonMask = 0
if(input.Nexus > 0){ buttonMask |= 2 }
if(input.Menu > 0){ buttonMask |= 4 }
if(input.View > 0){ buttonMask |= 8 }
if(input.A > 0){ buttonMask |= 16 }
if(input.B > 0){ buttonMask |= 32 }
if(input.X > 0){ buttonMask |= 64 }
if(input.Y > 0){ buttonMask |= 128 }
if(input.DPadUp > 0){ buttonMask |= 256 }
if(input.DPadDown > 0){ buttonMask |= 512 }
if(input.DPadLeft > 0){ buttonMask |= 1024 }
if(input.DPadRight > 0){ buttonMask |= 2048 }
if(input.LeftShoulder > 0){ buttonMask |= 4096 }
if(input.RightShoulder > 0){ buttonMask |= 8192 }
if(input.LeftThumb > 0){ buttonMask |= 16384 }
if(input.RightThumb > 0){ buttonMask |= 32768 }
packet.setUint16(offset, buttonMask, true)
packet.setInt16(offset+2, this._normalizeAxisValue(input.LeftThumbXAxis), true) // LeftThumbXAxis
packet.setInt16(offset+4, this._normalizeAxisValue(-input.LeftThumbYAxis), true) // LeftThumbYAxis
packet.setInt16(offset+6, this._normalizeAxisValue(input.RightThumbXAxis), true) // RightThumbXAxis
packet.setInt16(offset+8, this._normalizeAxisValue(-input.RightThumbYAxis), true) // RightThumbYAxis
packet.setUint16(offset+10, this._normalizeTriggerValue(input.LeftTrigger), true) // LeftTrigger
packet.setUint16(offset+12, this._normalizeTriggerValue(input.RightTrigger), true) // RightTrigger
packet.setUint32(offset+14, 1, true) // PhysicalPhysicality
packet.setUint32(offset+18, 1, false) // VirtualPhysicality
offset += 22
}
return offset
}
_writePointerData(packet:DataView, offset:number, frames:Array<PointerFrame>){
packet.setUint8(offset, 1)
// packet.setUint8(offset, frames.length)
offset++
if(frames.length >= 2){
console.warn('pointerQueue is bigger then 1. Only one event will be sent.')
}
// for (; frames.length > 0;) {
// this._inputFps.count()
const frame = frames[0]
if(frame !== undefined){
packet.setUint8(offset, frame.events.length)
offset++
const screenWidth = this._serverVideoWidth
const screenHeight = this._serverVideoHeight
for(let i = 0; i < frame.events.length; i++){
const event = frame.events[i]
const rect = event.target.getBoundingClientRect()
let e = 0.06575749909301447 * (screenHeight / 1)
let n = 0.06575749909301447 * (screenWidth / 1)
// @TODO: Check if this is correct
e =1
n =1
if(event.type === 'pointerup'){
e = 0
n = 0
}
packet.setUint16(offset, e, true)
packet.setUint16(offset+2, n, true)
packet.setUint8(offset+4, 255*event.pressure)
packet.setUint16(offset+5, event.twist, true)
packet.setUint32(offset+7, 0, true)
let o = (event.x - rect.left) * (screenWidth / rect.width)
, l = (event.y - rect.top) * (screenHeight / rect.height)
if(event.type === 'pointerup'){
// Reset x and y to 0 on pointerup
o = 0
l = 0
}
packet.setUint32(offset+11, o, true)
packet.setUint32(offset+15, l, true)
packet.setUint8(offset+19, (event.type === 'pointerdown') ? 1 :
(event.type === 'pointerup') ? 2 : (event.type === 'pointermove') ? 3 : 0)
offset = offset+20
}
}
return offset
}
_writeMouseData(packet:DataView, offset:number, frames:Array<MouseFrame>){
packet.setUint8(offset, frames.length)
offset++
if(frames.length >= 30){
console.warn('mouseQueue is bigger then 30. This might impact reliability!')
}
for(let i = 0; i < frames.length; i++) {
const input:MouseFrame = frames[i]
packet.setUint32(offset, input.X, true)
packet.setUint32(offset+4, input.Y, true)
packet.setUint32(offset+8, input.WheelX, true)
packet.setUint32(offset+12, input.WheelY, true)
packet.setUint8(offset+16, input.Buttons)
packet.setUint8(offset+17, input.Relative)
offset += 18
}
return offset
}
_writeKeyboardData(packet:DataView, offset:number, frames:Array<KeyboardFrame>){
packet.setUint8(offset, frames.length)
offset++
if(frames.length >= 30){
console.warn('keyboardQueue is bigger then 30. This might impact reliability!')
}
for(let i = 0; i < frames.length; i++) {
const input:KeyboardFrame = frames[i]
packet.setUint8(offset, 2) // 1 = Known, 2 = VKey, 3 = AppCommand, 0 = Unknown
packet.setUint8(offset+1, input.pressed ? 1 : 0)
packet.setUint8(offset+2, input.keyCode)
// packet.setUint8(offset+3, 1)
// packet.setUint8(offset+4, input.keyCode)
offset += 3
}
return offset
}
toBuffer(){
const metadataAlloc = new Uint8Array(this._totalSize)
const packet = new DataView(metadataAlloc.buffer)
packet.setUint16(0, this._reportType, true)
packet.setUint32(2, this._sequence, true)
packet.setFloat64(6, performance.now(), true)
let offset = 14
if(this._metadataFrames.length > 0) {offset = this._writeMetadataData(packet, offset, this._metadataFrames)}
if(this._gamepadFrames.length > 0) {offset = this._writeGamepadData(packet, offset, this._gamepadFrames)}
if(this._pointerFrames.length > 0) {offset = this._writePointerData(packet, offset, this._pointerFrames)}
if(this._mouseFrames.length > 0) {offset = this._writeMouseData(packet, offset, this._mouseFrames)}
if(this._keyboardFrames.length > 0) {offset = this._writeKeyboardData(packet, offset, this._keyboardFrames)}
if(this._reportType === ReportTypes.ClientMetadata){
packet.setUint8(offset, this._maxTouchpoints)
offset++
}
if(offset !== offset){
throw new Error('Packet length mismatch. Something is wrong!')
}
return packet
}
_normalizeTriggerValue(value: number): number {
if (value <= 0 || Number.isNaN(value)) {
return 0
}
if (value >= 1) {
return 65535
}
// Fast float -> int conversion in range [0, 65535]
return (value * 65535) | 0
}
_normalizeAxisValue(value: number) {
const max = 32767
const min = -32767
const scaled = Math.round(value * max)
return scaled > max ? max : scaled < min ? min : scaled
}
}
@@ -0,0 +1,171 @@
import xCloudPlayer from '../../player'
import InputPacket, { InputFrame as GamepadFrame, KeyboardFrame, MetadataFrame, MouseFrame, PointerFrame } from './packet'
export default class InputQueue {
private _player:xCloudPlayer
private _inputSequenceNum = 0
private _metadataQueue:Array<MetadataFrame> = []
private _gamepadQueue:Array<GamepadFrame> = []
private _pointerQueue:Array<PointerFrame> = []
private _mouseQueue:Array<MouseFrame> = []
private _keyboardQueue:Array<KeyboardFrame> = []
// Axis change detection and heartbeat tracking
private _gamepadHeartbeatTimers = new Map<number, number>()
private _lastGamepadState = new Map<number, GamepadFrame>()
private _axisChangeThreshold = 0.08
constructor(player:xCloudPlayer) {
this._player = player
}
private _hasQueuedData() {
return this._metadataQueue.length > 0 ||
this._gamepadQueue.length > 0 ||
this._pointerQueue.length > 0 ||
this._mouseQueue.length > 0 ||
this._keyboardQueue.length > 0
}
private _upsertGamepadFrame(data:GamepadFrame) {
for(let i = 0; i < this._gamepadQueue.length; i++){
if(this._gamepadQueue[i].GamepadIndex === data.GamepadIndex){
this._gamepadQueue[i] = data
return
}
}
this._gamepadQueue.push(data)
}
private _hasSignificantAxisChange(current:GamepadFrame, last:GamepadFrame | undefined):boolean {
if(!last) return true // First frame always send
const stickNames = ['LeftThumbXAxis', 'LeftThumbYAxis', 'RightThumbXAxis', 'RightThumbYAxis'] as const
const triggerNames = ['LeftTrigger', 'RightTrigger'] as const
// For analog sticks: only skip if BOTH change is small AND value is near center
for(const stick of stickNames){
const delta = Math.abs(current[stick] - last[stick])
const value = Math.abs(current[stick])
if(delta > this._axisChangeThreshold || value > 0.15){
return true
}
}
// For triggers: always send if they changed (higher sensitivity needed)
for(const trigger of triggerNames){
if(Math.abs(current[trigger] - last[trigger]) > 0.01){
return true
}
}
return false
}
private _isHeartbeatDue(gamepadIndex:number):boolean {
const lastHeartbeat = this._gamepadHeartbeatTimers.get(gamepadIndex) ?? 0
return performance.now() - lastHeartbeat > 33 // Send at least every 33ms
}
queueMetadataFrame(data:MetadataFrame) {
this._metadataQueue.push(data)
this.checkQueueAndSend()
}
queueGamepadFrame(data:GamepadFrame) {
const lastFrame = this._lastGamepadState.get(data.GamepadIndex)
const hasChange = this._hasSignificantAxisChange(data, lastFrame)
const isDue = this._isHeartbeatDue(data.GamepadIndex)
if(hasChange || isDue){
this._upsertGamepadFrame(data)
this._lastGamepadState.set(data.GamepadIndex, data)
this._gamepadHeartbeatTimers.set(data.GamepadIndex, performance.now())
this.checkQueueAndSend()
}
}
queueGamepadFrames(frames:Array<GamepadFrame>, forceSend = false) {
for(const frame of frames){
this._upsertGamepadFrame(frame)
}
this.checkQueueAndSend(forceSend)
}
queueMouseFrame(data:MouseFrame) {
this._mouseQueue.push(data)
this.checkQueueAndSend()
}
queueKeyboardFrame(data:KeyboardFrame) {
this._keyboardQueue.push(data)
this.checkQueueAndSend()
}
queuePointerFrame(data:PointerFrame) {
if(this._pointerQueue.length > 0){
this._pointerQueue[0].events = this._pointerQueue[0].events.concat(data.events)
} else {
this._pointerQueue.push(data)
}
this.checkQueueAndSend()
}
checkQueueAndSend(forceSend = false) {
if(forceSend === true){
if(this._hasQueuedData()){
this.sendQueue()
}
return
}
// Prioritize low-latency gamepad delivery.
if(this._gamepadQueue.length > 0){
this.sendQueue()
return
}
// Non-metadata input should also not wait.
if(this._mouseQueue.length > 0 || this._keyboardQueue.length > 0 || this._pointerQueue.length > 0){
this.sendQueue()
return
}
// Metadata-only can batch slightly.
if(this._metadataQueue.length > 5){
this.sendQueue()
}
}
sendQueue() {
// console.log('Sending queues:', this._metadataQueue.length, this._gamepadQueue.length, this._pointerQueue.length, this._mouseQueue.length, this._keyboardQueue.length)
const packet = new InputPacket(this.getSequenceNum())
if(this._pointerQueue.length > 0){
packet.setStreamConfig({
width: this._player._channels.input.getServerVideoWidth(),
height: this._player._channels.input.getServerVideoHeight(),
})
}
packet.setData(this._metadataQueue, this._gamepadQueue, this._pointerQueue, this._mouseQueue, this._keyboardQueue)
this._player._channels.input.send(packet.toBuffer())
this.clearQueues()
}
clearQueues() {
this._metadataQueue = []
this._gamepadQueue = []
this._pointerQueue = []
this._mouseQueue = []
this._keyboardQueue = []
}
getSequenceNum() {
this._inputSequenceNum++
return this._inputSequenceNum
}
}
@@ -0,0 +1,120 @@
import { v4 as uuidv4 } from 'uuid'
import Channel from './channel'
import MessageHandler from './message/handler'
export default class MessageChannel extends Channel {
_handshakeCompleted = false
getChannelName() {
return 'message'
}
getChannelConfig() {
return {
protocol: 'messageV1',
ordered: true,
}
}
onOpen() {
console.log('[MessageChannel] Sending handshake...')
const handshake = JSON.stringify({
'type':'Handshake',
'version':'messageV1',
'id':'be0bfc6d-1e83-4c8a-90ed-fa8601c5a179',
'cv':'0',
})
this.send(handshake)
}
onMessage(event: MessageEvent<any>) {
const data = JSON.parse(event.data)
// console.log('[MessageChannel] received:', data)
switch(data.type){
case 'HandshakeAck':
console.log('[MessageChannel] Handshake completed')
this._handshakeCompleted = true
// Start control & input channels
this.getPlayer()._channels.control.sendAuthorization()
this.getPlayer()._channels.input.start()
this.sendConfig()
break
case 'TransactionStart':
case 'Message':
new MessageHandler(this, data)
break
default:
console.log('[MessageChannel] Unhandled message type:', data.type, data)
}
}
sendConfig(){
const systemUis = []
const systemVersion = [0, 2, 0]
const uiConfig = JSON.stringify(this.generateMessage('/streaming/systemUi/configuration', {
'version': systemVersion,
'systemUis': systemUis, // Xbox Windows app has [33], xCloud has [10,19,31,27,32,-41]
// 8 = unknown
// 10 = ShowVirtualKeyboard
// 13 = unknown
// 15 = unknown
// 19 = ShowMessageDialog
// 31 = ShowApplication
// 27 = ShowPurchase
// 32 = ShowTimerExtensions
// 33 = Xbox windows app, disables the nexus menu on xCloud (Alt nexus menu?)
// -44 = unknown
// 40 = unknown
// 41 = unknown
// -43 = unknown
// Possible options: Keyboard, PurchaseModal
}))
this.send(uiConfig)
const clientConfig = JSON.stringify(this.generateMessage('/streaming/properties/clientappinstallidchanged', { 'clientAppInstallId': 'c97d7ee0-73b2-4239-bf1d-9d805a338429' }))
this.send(clientConfig)
const orientationConfig = JSON.stringify(this.generateMessage('/streaming/characteristics/orientationchanged', { 'orientation': 0 }))
this.send(orientationConfig)
const touchConfig = JSON.stringify(this.generateMessage('/streaming/characteristics/touchinputenabledchanged', { 'touchInputEnabled': false }))
this.send(touchConfig)
const deviceConfig = JSON.stringify(this.generateMessage('/streaming/characteristics/clientdevicecapabilities', {}))
this.send(deviceConfig)
const dimensionsConfig = JSON.stringify(this.generateMessage('/streaming/characteristics/dimensionschanged', {
'horizontal': 1920,
'vertical': 1080,
'preferredWidth': 1920,
'preferredHeight': 1080,
'safeAreaLeft': 0,
'safeAreaTop': 0,
'safeAreaRight': 1920,
'safeAreaBottom': 1080,
'supportsCustomResolution':true,
}))
this.send(dimensionsConfig)
}
generateMessage(path, data) {
return {
'type': 'Message',
'content': JSON.stringify(data),
'id': uuidv4(),
'target': path,
'cv': '',
}
}
destroy() {
// console.log('DebugChannel destroy() called')
}
}
@@ -0,0 +1,73 @@
import Channel from '../../lib/channel'
export default class MessageHandler {
private _channel:Channel
private _data:object
constructor(channel:Channel, data:object) {
this._channel = channel
this._data = data
this.handle(this._data)
}
handle(data) {
console.log('[MessageHandler] Handling:', data)
switch(data.target){
case '/streaming/sessionLifetimeManagement/serverInitiatedDisconnect':
this.completeTransaction(data.id, '')
this._channel.getPlayer().destroy()
break
case '/streaming/properties/titleinfo':
console.log('Title update:', data)
break
case '/streaming/touchcontrols/showlayoutv2':
// console.log('Showing touch layout:', data)
break
case '/streaming/systemUi/messages/ShowMessageDialog':
this.handleMessage(data)
break
default:
console.log('[MessageChannel] Unhandled transaction:', data)
}
}
completeTransaction(id, data) {
const transaction = JSON.stringify({
'type': 'TransactionComplete',
'content': JSON.stringify(data),
// 'content':'{\'Result\':0}',
'id': id,
'cv': '',
})
this._channel.send(transaction)
}
cancelTransaction(id, data) {
const transaction = JSON.stringify({
'type': 'ReceiverCancel',
'content': JSON.stringify(data),
'id': id,
'cv': '',
})
this._channel.send(transaction)
}
handleMessage(data) {
const jsonData = JSON.parse(data.content)
console.log(jsonData)
if(confirm(jsonData.TitleText+'\n\n'+jsonData.ContentText)){
this.completeTransaction(data.id, { Result: 0 })
} else {
this.completeTransaction(data.id, { Result: 1 })
// this.cancelTransaction(data.id, '')
}
}
}
+56
View File
@@ -0,0 +1,56 @@
import xCloudPlayer from './player'
import Teredo from './teredo'
export default class Ice {
private _peerConnection:RTCPeerConnection
private _iceCandidates: RTCIceCandidate[] = []
constructor(player:xCloudPlayer) {
this._peerConnection = player._peerConnection
this._peerConnection.addEventListener('icecandidate', event => {
if(event.candidate) {
this._iceCandidates.push(event.candidate)
// @TODO: Implement Teredo port selection for remote streaming
}
})
}
getCandidates(){
return this._iceCandidates
}
setRemoteCandidates(candidates:RTCIceCandidate[]){
if(candidates.length === 0){
console.warn('Error: No candidates received!')
}
for(const candidate in candidates){
if(candidates[candidate].candidate === 'a=end-of-candidates') {continue}
const candidateAddress = candidates[candidate].candidate.split(' ')
if(candidateAddress.length > 4 && candidateAddress[4].substr(0, 4) === '2001'){
const teredo = new Teredo(candidateAddress[4])
this._peerConnection.addIceCandidate({
candidate: 'a=candidate:10 1 UDP 1 '+teredo.getIpv4Address()+' 9002 typ host ',
sdpMLineIndex: candidates[candidate].sdpMLineIndex,
sdpMid: candidates[candidate].sdpMid,
})
this._peerConnection.addIceCandidate({
candidate: 'a=candidate:11 1 UDP 1 '+teredo.getIpv4Address()+' '+teredo.getIpv4Port()+' typ host ',
sdpMLineIndex: candidates[candidate].sdpMLineIndex,
sdpMid: candidates[candidate].sdpMid,
})
}
this._peerConnection.addIceCandidate({
candidate: candidates[candidate].candidate,
sdpMid: candidates[candidate].sdpMid,
sdpMLineIndex: candidates[candidate].sdpMLineIndex,
})
}
}
}
@@ -0,0 +1,441 @@
import xCloudPlayer from '../player'
import { VibrationFrame } from '../channel/input'
export interface GamepadOptions {
enable_keyboard?: boolean;
enable_gamepad?: boolean;
enable_vibration?: boolean;
keyboard_mapping?: {
[key:string]: string;
};
gamepad_mapping?: {
[key:string]: string;
};
gamepad_axes_mapping?: {
[key:string]: string;
};
gamepad_deadzone?: number;
gamepad_force_capture?: boolean;
}
export interface GamepadOptionsDefaults extends Required<GamepadOptions> {}
export default class Gamepad {
private _player: xCloudPlayer | undefined
private _index: number
private _physicalGamepadId = -1
private _rumbleInterval
private _isFocused = true
private _gamepadOverride = {
buttons: {},
}
private _options:GamepadOptionsDefaults = {
enable_keyboard: false,
enable_gamepad: true,
enable_vibration: true,
keyboard_mapping: {
'A': 'Enter',
'B': 'Backspace',
'X': 'x',
'Y': 'y',
'DPadUp': 'ArrowUp',
'DPadDown': 'ArrowDown',
'DPadLeft': 'ArrowLeft',
'DPadRight': 'ArrowRight',
'LeftShoulder': '[',
'RightShoulder': ']',
'LeftThumb': 'l',
'RightThumb': 'r',
'LeftTrigger': '-',
'RightTrigger': '=',
'Menu': 'm',
'View': 'v',
'Nexus': 'n',
},
gamepad_mapping: {
'A': '0',
'B': '1',
'X': '2',
'Y': '3',
'DPadUp': '12',
'DPadDown': '13',
'DPadLeft': '14',
'DPadRight': '15',
'LeftShoulder': '4',
'RightShoulder': '5',
'LeftThumb': '10',
'RightThumb': '11',
'LeftTrigger': '6',
'RightTrigger': '7',
'Menu': '9',
'View': '8',
'Nexus': '16',
},
gamepad_axes_mapping: {
'LeftThumbXAxis': '0',
'LeftThumbYAxis': '1',
'RightThumbXAxis': '2',
'RightThumbYAxis': '3',
},
gamepad_deadzone: 0.08,
gamepad_force_capture: false,
}
private _listener = {
keyDown: this.onKeyDown.bind(this),
keyUp: this.onKeyUp.bind(this),
gamepadConnected: this.onGamepadConnected.bind(this),
gamepadDisconnected: this.onGamepadDisconnected.bind(this),
windowFocus: this.onWindowFocus.bind(this),
windowBlur: this.onWindowBlur.bind(this),
}
constructor(index:number, options:GamepadOptions = {}){
this._index = index
this._options = {
...this._options,
...options,
}
}
attach(xCloudPlayer:xCloudPlayer){
this._player = xCloudPlayer
this._player._channels.control.sendGamepadState(this._index, true, this)
if(this._options.enable_keyboard === true){
window.addEventListener('keydown', this._listener.keyDown)
window.addEventListener('keyup', this._listener.keyUp)
}
if(this._options.enable_gamepad === true){
window.addEventListener('gamepadconnected', this._listener.gamepadConnected)
window.addEventListener('gamepaddisconnected', this._listener.gamepadDisconnected)
}
if(this._options.enable_gamepad === true && this._options.gamepad_force_capture === false){
window.addEventListener('blur', this._listener.windowBlur)
window.addEventListener('focus', this._listener.windowFocus)
}
this.detectActiveGamepad()
}
detach(){
if(this._player === undefined){
console.log('[Gamepad] Player is not attached. this._player is:', this._player)
return
}
this._player._channels.control.sendGamepadState(this._index, false)
this._physicalGamepadId = -1
if(this._rumbleInterval !== undefined){
clearInterval(this._rumbleInterval)
}
if(this._options.enable_keyboard === true){
window.removeEventListener('keydown', this._listener.keyDown)
window.removeEventListener('keyup', this._listener.keyUp)
}
if(this._options.enable_gamepad === true){
window.removeEventListener('gamepadconnected', this._listener.gamepadConnected)
window.removeEventListener('gamepaddisconnected', this._listener.gamepadDisconnected)
}
if(this._options.enable_gamepad === true && this._options.gamepad_force_capture === false){
window.removeEventListener('blur', this._listener.windowBlur)
window.removeEventListener('focus', this._listener.windowFocus)
}
}
sendButtonState(button, value){
if(this._player === undefined){
console.log('[Gamepad] Player is not attached. this._player is:', this._player)
return
}
if(! (button in this.getDefaultFamepadFrame())){
console.log('[Gamepad] Invalid button:', button)
return
}
const buttonPreset = this.getDefaultFamepadFrame()
buttonPreset.GamepadIndex = this._index
this._gamepadOverride.buttons[button] = value
}
onKeyDown(event:KeyboardEvent){
if(this._isFocused === false){
return
}
for(const button in this._options.keyboard_mapping){
if(this._options.keyboard_mapping[button] === event.key){
this._gamepadOverride.buttons[button] = 1
event.preventDefault()
}
}
}
onKeyUp(event:KeyboardEvent){
if(this._isFocused === false){
return
}
for(const button in this._options.keyboard_mapping){
if(this._options.keyboard_mapping[button] === event.key){
this._gamepadOverride.buttons[button] = 0
event.preventDefault()
}
}
}
onWindowFocus(){
this._isFocused = true
}
onWindowBlur(){
// this._isFocused = false
}
getDefaultFamepadFrame(){
return {
GamepadIndex: 0,
Nexus: 0,
Menu: 0,
View: 0,
A: 0,
B: 0,
X: 0,
Y: 0,
DPadUp: 0,
DPadDown: 0,
DPadLeft: 0,
DPadRight: 0,
LeftShoulder: 0,
RightShoulder: 0,
LeftThumb: 0,
RightThumb: 0,
LeftThumbXAxis: 0,
LeftThumbYAxis: 0,
RightThumbXAxis: 0,
RightThumbYAxis: 0,
LeftTrigger: 0,
RightTrigger: 0,
}
}
detectActiveGamepad(){
if(this._physicalGamepadId < 0){
for(const gamepadBrowser in navigator.getGamepads()){
const gamepadIndex = navigator.getGamepads()[gamepadBrowser]?.index
if(gamepadIndex === undefined){
continue
}
let gamepadFoundInHandler = false
for(const gamepad in this._player?._channels.control.getGamepadHandlers()){
if(
this._player?._channels.control.getGamepadHandlers()[gamepad] instanceof Gamepad &&
(this._player?._channels.control.getGamepadHandlers()[gamepad] as Gamepad)._physicalGamepadId === navigator.getGamepads()[gamepadBrowser]?.index
) {
console.log('[Gamepad] Active gamepad already detected:', gamepadIndex, (this._player?._channels.control.getGamepadHandlers()[gamepad] as Gamepad)._physicalGamepadId)
gamepadFoundInHandler = true
continue
}
}
if(gamepadFoundInHandler === false){
this._physicalGamepadId = gamepadIndex
console.log('[Gamepad] Active gamepad detected:', this._physicalGamepadId)
}
}
} else {
console.log('[Gamepad] Physical gamepad already detected:', this._physicalGamepadId)
}
}
onGamepadConnected(event:GamepadEvent){
if(this._physicalGamepadId < 0){
for(const gamepad in this._player?._channels.control.getGamepadHandlers()){
if(
this._player?._channels.control.getGamepadHandlers()[gamepad] instanceof Gamepad &&
(this._player?._channels.control.getGamepadHandlers()[gamepad] as Gamepad)._physicalGamepadId === event.gamepad.index
) {
return
}
}
this._physicalGamepadId = event.gamepad.index
}
}
onGamepadDisconnected(event:GamepadEvent){
if(event.gamepad.index === this._physicalGamepadId){
this._physicalGamepadId = -1
}
}
hasOverrideButtons(){
return Object.keys(this._gamepadOverride.buttons).length > 0
}
getGamepadState(){
const gamepad = this.getGamepad(this._physicalGamepadId)
if((this._physicalGamepadId < 0 || gamepad === undefined || this._isFocused === false) && (this.hasOverrideButtons() === false)){
return undefined
}
const frame = this.getDefaultFamepadFrame()
frame.GamepadIndex = this._index
// Set buttons
if(gamepad !== undefined){
for(const button in this._options.gamepad_mapping){
// DroidDeck: Android often reports no guide button (index 16); a missing button is "not pressed".
frame[button] = gamepad.buttons[this._options.gamepad_mapping[button]]?.value ?? 0
}
// Set axis
for(const axis in this._options.gamepad_axes_mapping){
frame[axis] = this.normaliseAxis(gamepad.axes[this._options.gamepad_axes_mapping[axis]] ?? 0)
}
}
// Check for button overrides from the keyboard
// for(const button in this._gamepadOverride.buttons){
// frame[button] = this._gamepadOverride.buttons[button]
// // Clean up existing keys if they are 0
// if(this._gamepadOverride.buttons[button] === 0){
// // Remove key from override
// const newOverrideButtons = {}
// for(const oButton in this._gamepadOverride.buttons){
// if(this._gamepadOverride.buttons[oButton] !== 0){
// newOverrideButtons[oButton] = this._gamepadOverride.buttons[oButton]
// }
// }
// this._gamepadOverride.buttons = newOverrideButtons
// }
// }
// Check for button overrides from the keyboard
for(const button in this._gamepadOverride.buttons){
frame[button] = this._gamepadOverride.buttons[button]
}
// Clean up released buttons in a separate pass
const buttonsToRemove = []
for(const button in this._gamepadOverride.buttons){
if(this._gamepadOverride.buttons[button] === 0){
buttonsToRemove.push(button)
}
}
// Delete released buttons instead of recreating the object
for(const button of buttonsToRemove){
delete this._gamepadOverride.buttons[button]
}
// Start + Select Nexus menu workaround
if(frame.View > 0 && frame.Menu > 0){
frame.View = 0
frame.Menu = 0
frame.Nexus = 1
}
return frame
}
getGamepad(index = -1){
const gamepad = navigator.getGamepads()[index]
if(gamepad !== null) {
return gamepad
} else {
return undefined
}
}
getPhysicalGamepadId(){
return this._physicalGamepadId
}
handleVibration(report:VibrationFrame){
if(this._physicalGamepadId < 0){
console.log('[Gamepad] Received a vibration report but no physical gamepad is connected')
return
}
const gamepad = this.getGamepad(this._physicalGamepadId)
if(gamepad === undefined){
console.log('[Gamepad] Received a vibration report but no physical gamepad is connected')
return
}
if(this._options.enable_vibration === false){
return
}
const rumbleData = {
startDelay: 0,
duration: report.durationMs,
// DroidDeck: the player's rumble strength setting (0..1).
weakMagnitude: report.rightMotorPercent * ((window as any).__ddRumble ?? 1),
strongMagnitude: report.leftMotorPercent * ((window as any).__ddRumble ?? 1),
leftTrigger: report.leftTriggerMotorPercent,
rightTrigger: report.rightTriggerMotorPercent,
}
// @ts-expect-error - dual-rumble is not in the types
if(gamepad.vibrationActuator?.type === 'dual-rumble') {
const intensityRumble = report.rightMotorPercent < .6 ? (.6 - report.rightMotorPercent) / 2 : 0
const intensityRumbleTriggers = (report.leftTriggerMotorPercent + report.rightTriggerMotorPercent) / 4
const endIntensity = Math.min(intensityRumble, intensityRumbleTriggers)
rumbleData.weakMagnitude = Math.min(1, (report.rightMotorPercent + endIntensity) * ((window as any).__ddRumble ?? 1))
// Set triggers to 0 as dual-rumble does not support Triggers
// @TODO: Check if we can improve rumble: https://github.com/MicrosoftEdge/MSEdgeExplainers/blob/main/GamepadHapticsActuatorTriggerRumble/explainer.md
rumbleData.leftTrigger = 0
rumbleData.rightTrigger = 0
gamepad.vibrationActuator?.playEffect('dual-rumble', rumbleData)
clearInterval(this._rumbleInterval)
if(report.repeat > 0) {
let repeatCount = report.repeat
this._rumbleInterval = setInterval(() => {
if(repeatCount <= 0){
clearInterval(this._rumbleInterval)
}
if(gamepad.vibrationActuator !== undefined) {
gamepad.vibrationActuator?.playEffect('dual-rumble', rumbleData)
}
repeatCount--
}, report.delayMs + report.durationMs)
}
} else {
console.log('[Gamepad] Unknown vibration type:', (gamepad.vibrationActuator as any).type)
}
}
normaliseAxis(value:number) {
if(Math.abs(value) < this._options.gamepad_deadzone) {
return 0
}
value = value - Math.sign(value) * this._options.gamepad_deadzone
value /= (1.0 - this._options.gamepad_deadzone)
return value
}
}
@@ -0,0 +1,143 @@
import xCloudPlayer from '../player'
export default class MouseKeyboard {
private _player: xCloudPlayer | undefined
private _index: number
private _listener = {
keyDown: this.onKeyDown.bind(this),
keyUp: this.onKeyUp.bind(this),
pointerMove: this.onPointerMove.bind(this),
pointerClick: this.onPointerClick.bind(this),
pointerScroll: this.onPointerScroll.bind(this),
lockMouseKeyboard: this.lockMouseKeyboard.bind(this),
}
constructor(index:number){
this._index = index
}
attach(xCloudPlayer:xCloudPlayer){
this._player = xCloudPlayer
this._player._channels.control.sendGamepadState(this._index, true, this)
// Mod the video UI to capture MKB input
if(this._player.getVideoElement() === undefined){
console.log('[MKB] VideoComponent is not attached. this._player._videoComponent is:', this._player.getVideoElement())
return
} else {
this._player.getVideoElement()?.addEventListener('pointermove', this._listener.pointerMove)
this._player.getVideoElement()?.addEventListener('pointerdown', this._listener.pointerClick)
this._player.getVideoElement()?.addEventListener('pointerup', this._listener.pointerClick)
this._player.getVideoElement()?.addEventListener('wheel', this._listener.pointerScroll)
this._player.getVideoElement()?.addEventListener('click', this._listener.lockMouseKeyboard)
window.addEventListener('keydown', this._listener.keyDown)
window.addEventListener('keyup', this._listener.keyUp)
this._player.getVideoElement()?.addEventListener('contextmenu', event => {
event.preventDefault()
})
}
}
detach(){
if(this._player === undefined){
console.log('[MKB] Player is not attached. this._player is:', this._player)
return
}
this._player._channels.control.sendGamepadState(this._index, false)
window.removeEventListener('keydown', this._listener.keyDown)
window.removeEventListener('keyup', this._listener.keyUp)
this._player.getVideoElement()?.removeEventListener('pointermove', this._listener.pointerMove)
this._player.getVideoElement()?.removeEventListener('pointerdown', this._listener.pointerClick)
this._player.getVideoElement()?.removeEventListener('pointerup', this._listener.pointerClick)
this._player.getVideoElement()?.removeEventListener('wheel', this._listener.pointerScroll)
this._player.getVideoElement()?.removeEventListener('click', this._listener.lockMouseKeyboard)
}
onPointerMove(event:PointerEvent){
this._player?._channels.input.queueMouseFrame({
X: event.movementX*2,
Y: event.movementY*2,
WheelX: 0,
WheelY: 0,
Buttons: event.buttons,
Relative: 0, // 0 = Relative, 1 = Absolute
})
}
onPointerClick(event:PointerEvent){
// console.log('Pointer clicked:', event)
this._player?._channels.input.queueMouseFrame({
X: event.movementX*2,
Y: event.movementY*2,
WheelX: 0,
WheelY: 0,
Buttons: event.buttons,
Relative: 0, // 0 = Relative, 1 = Absolute
})
}
onPointerScroll(event:WheelEvent){
console.log('[MKB] Pointer scrolled:', event)
}
onKeyDown(event:KeyboardEvent){
this._player?._channels.input.queueKeyboardFrame({
key: event.key,
keyCode: event.keyCode,
pressed: true,
})
event.preventDefault()
}
onKeyUp(event:KeyboardEvent){
this._player?._channels.input.queueKeyboardFrame({
key: event.key,
keyCode: event.keyCode,
pressed: false,
})
}
lockMouseKeyboard(){
if(this._player === undefined || this._player.getVideoElement() === undefined){
console.log('[MKB] Player or VideoComponent is not attached. Failed to lock Mouse & Keyboard because there is nothing to lock for.')
return
}
// @disable ts-expect-error requestPointerLock() is not defined to consume arguments
const promise = this._player?.getVideoElement()?.requestPointerLock({
unadjustedMovement: true,
}) as Promise<undefined> | undefined
if(promise === undefined){
console.log('[MKB] Failed to lock Mouse & Keyboard because the requestPointerLock() is undefined')
return
}
if ('keyboard' in navigator && 'lock' in (navigator.keyboard as any)) {
document.body.requestFullscreen().then(() => {
(navigator as any).keyboard.lock()
})
} else {
console.log('[MKB] Keyboard lock is not supported. Error:', 'NotSupportedError')
return
}
promise.then(() => {
console.log('[MKB] pointer is locked')
}).catch((error) => {
if (error.name === 'NotSupportedError') {
console.log('[MKB] Mouse pointer lock is not supported. Error:', 'NotSupportedError')
}
})
}
}
@@ -0,0 +1,70 @@
import xCloudPlayer from '../player'
export default class Touch {
private _player: xCloudPlayer | undefined
private _index: number
private _pointerEvents = {}
private _lastPointerId = -1
private _mouseDown = false
private _listener = {
onPointer: this.onPointer.bind(this),
}
constructor(index:number){
this._index = index
}
attach(xCloudPlayer:xCloudPlayer){
this._player = xCloudPlayer
this._player._channels.control.sendGamepadState(this._index, true, this)
// Mod the video UI to capture MKB input
if(this._player.getVideoElement() === undefined){
console.log('VideoComponent is not attached. this._player._videoComponent is:', this._player.getVideoElement())
return
} else {
this._player.getVideoElement()?.addEventListener('pointermove', this._listener.onPointer, { passive: false })
this._player.getVideoElement()?.addEventListener('pointerdown', this._listener.onPointer, { passive: false })
this._player.getVideoElement()?.addEventListener('pointerup', this._listener.onPointer, { passive: false })
}
}
detach(){
if(this._player === undefined){
console.log('Player is not attached. this._player is:', this._player)
return
}
this._player._channels.control.sendGamepadState(this._index, false)
this._player.getVideoElement()?.removeEventListener('pointermove', this._listener.onPointer)
this._player.getVideoElement()?.removeEventListener('pointerdown', this._listener.onPointer)
this._player.getVideoElement()?.removeEventListener('pointerup', this._listener.onPointer)
}
onPointer(event:PointerEvent) {
this._lastPointerId = event.pointerId
if(event.pointerType === 'mouse' && event.type === 'pointerdown'){
this._mouseDown = true
}
if(event.pointerType === 'mouse' && event.type === 'pointerup'){
this._mouseDown = false
}
if(event.pointerType === 'mouse' && event.type === 'pointermove' && this._mouseDown === false){
// ignore events if the pointer type is mouse and is not pressed.
} else {
this._player?._channels.input.queuePointerFrame({
events: [event],
})
}
event.preventDefault()
}
}
Loaded 100 of 285 files, more files were not shown because too many files have changed in this diff. Show more