diff --git a/.github/workflows/build-aaudio-sink.yml b/.github/workflows/build-aaudio-sink.yml index b4140934ac..63133e545f 100644 --- a/.github/workflows/build-aaudio-sink.yml +++ b/.github/workflows/build-aaudio-sink.yml @@ -6,7 +6,6 @@ on: push: paths: - 'tools/aaudio-sink/**' - - 'tools/directaudio-relay/**' - '.github/workflows/build-aaudio-sink.yml' workflow_dispatch: @@ -26,7 +25,6 @@ jobs: set -euo pipefail export NDK="${ANDROID_NDK_LATEST_HOME:-${ANDROID_NDK_HOME:-$ANDROID_NDK_ROOT}}" tools/aaudio-sink/build.sh "$PWD/pa13" "$PWD/out" - tools/directaudio-relay/build.sh "$PWD/out" - uses: actions/upload-artifact@v4 with: name: audio-sink-modules diff --git a/.github/workflows/build-desktop-kde.yml b/.github/workflows/build-desktop-kde.yml new file mode 100644 index 0000000000..9390e1af85 --- /dev/null +++ b/.github/workflows/build-desktop-kde.yml @@ -0,0 +1,101 @@ +name: Build desktop package (KDE Plasma) + +# The desktop the app installs into its Linux runtime on first use: KDE Plasma as SteamOS's desktop +# mode has it, the closure of tools/desktop-kde/seeds.txt over Arch Linux ARM minus what the runtime +# already carries, plus tools/desktop-kde/overlay (tools/desktop-kde/build.sh), every package +# signature-checked. Published as a release in Droid-Deck/DroidDeck-Components with DroidDeck's +# other components; the apk pins it in tools/desktop-kde/release.env (tag, sha256, size). A +# published tag is never rebuilt: the packages are Arch Linux ARM's current ones, so a rebuild +# would not match the pinned sha256. A changed package gets a new tag (desktop-kde-r2, ...) and a +# release.env update. +on: + push: + paths: + - 'tools/desktop-kde/**' + - '.github/workflows/build-desktop-kde.yml' + workflow_dispatch: + inputs: + tag: + description: 'Release tag to publish as (e.g. desktop-kde-r1); empty = artifact only' + required: false + default: '' + +permissions: + contents: write + +jobs: + kwin: + runs-on: ubuntu-24.04-arm + timeout-minutes: 90 + steps: + - uses: actions/checkout@v4 + - name: Build KWin with nested desktop scaling + run: | + sudo apt-get update -qq && sudo apt-get install -y -qq zstd xz-utils gpgv patch binutils >/dev/null + bash tools/desktop-kde/build-kwin.sh out/kwin + - uses: actions/upload-artifact@v4 + with: + name: desktop-kde-kwin + retention-days: 7 + path: out/kwin/ + build: + needs: kwin + runs-on: ubuntu-24.04 + timeout-minutes: 90 + steps: + - uses: actions/checkout@v4 + - uses: actions/download-artifact@v4 + with: + name: desktop-kde-kwin + path: out/kwin + - uses: actions/create-github-app-token@v2 + id: components-token + if: github.event_name == 'workflow_dispatch' && inputs.tag != '' + with: + app-id: ${{ vars.DROIDDECK_CI_APP_ID }} + private-key: ${{ secrets.DROIDDECK_CI_APP_KEY }} + owner: Droid-Deck + repositories: DroidDeck-Components + - name: Refuse a published tag + if: github.event_name == 'workflow_dispatch' && inputs.tag != '' + env: + GH_TOKEN: ${{ steps.components-token.outputs.token }} + run: | + if gh release view '${{ inputs.tag }}' -R Droid-Deck/DroidDeck-Components >/dev/null 2>&1; then + echo "::error::${{ inputs.tag }} is already published, and published tags are never replaced: use a new tag" + exit 1 + fi + - name: Build + run: | + set -euo pipefail + sudo apt-get update -qq && sudo apt-get install -y -qq zstd xz-utils gpgv >/dev/null + KDE_KWIN_OVERRIDE="$PWD/out/kwin" tools/desktop-kde/build.sh out + cp out/kwin/desktop-kde.kwin.txt out/ + echo "size $(stat -c %s out/desktop-kde.tar.zst) sha256 $(cat out/desktop-kde.sha256)" + # The package itself too, briefly: enough to try a build on a device before publishing it. + - uses: actions/upload-artifact@v4 + with: + name: desktop-kde + retention-days: 7 + compression-level: 0 + path: | + out/desktop-kde.tar.zst + out/desktop-kde.packages.txt + out/desktop-kde.kwin.txt + out/desktop-kde.sha256 + - name: Publish + if: github.event_name == 'workflow_dispatch' && inputs.tag != '' + env: + GH_TOKEN: ${{ steps.components-token.outputs.token }} + GH_REPO: Droid-Deck/DroidDeck-Components + run: | + set -euo pipefail + TAG='${{ inputs.tag }}' + if gh release view "$TAG" >/dev/null 2>&1; then + echo "::error::$TAG is already published in DroidDeck-Components, and published tags are never replaced: use a new tag" + exit 1 + fi + gh release create "$TAG" --latest=false --prerelease=false --title "$TAG" \ + --notes "KDE Plasma desktop package for DroidDeck's Linux runtime ($GITHUB_REPOSITORY tools/desktop-kde, $GITHUB_SHA). size $(stat -c %s out/desktop-kde.tar.zst), sha256 $(cat out/desktop-kde.sha256). desktop-kde.packages.txt lists the signature-checked Arch Linux ARM packages it was built from." \ + out/desktop-kde.tar.zst out/desktop-kde.sha256 out/desktop-kde.packages.txt out/desktop-kde.kwin.txt + gh release edit "$TAG" --latest=false --prerelease=false diff --git a/.github/workflows/build-wlroots.yml b/.github/workflows/build-wlroots.yml deleted file mode 100644 index 92780a2a49..0000000000 --- a/.github/workflows/build-wlroots.yml +++ /dev/null @@ -1,49 +0,0 @@ -name: Build wlroots (patched) - -# The desktop's wlroots (Arch Linux ARM's wlroots0.20, what the desktop package's labwc links) -# rebuilt with tools/wlroots/patches: a Vulkan DMA-BUF allocator for the Adreno node, which is not -# a DRM device, so labwc's vulkan and gles2 renderers can start. Built in an Arch Linux ARM -# container on an arm64 runner and published as a release asset the apk build fetches -# (tools/wlroots/release.env names the tag and sha). The hosted desktop package is never changed. -on: - push: - paths: - - 'tools/wlroots/**' - - '.github/workflows/build-wlroots.yml' - workflow_dispatch: - inputs: - tag: - description: 'Release tag to publish as (e.g. wlroots-0.20.2-p1); empty = artifact only' - required: false - default: '' - -permissions: - contents: write - -jobs: - build: - runs-on: ubuntu-24.04-arm - timeout-minutes: 90 - steps: - - uses: actions/checkout@v4 - - name: Build in Arch Linux ARM - run: | - set -euo pipefail - docker run --rm -v "$PWD:/work" -w /work menci/archlinuxarm:base-devel bash tools/wlroots/build-in-arch.sh - sudo chown -R "$(id -u):$(id -g)" . - - uses: actions/upload-artifact@v4 - with: - name: wlroots-patched - path: | - wlroots.tzst - wlroots.tzst.sha256 - - name: Publish - if: github.event_name == 'workflow_dispatch' && inputs.tag != '' - env: - GH_TOKEN: ${{ github.token }} - run: | - set -euo pipefail - gh release create "${{ inputs.tag }}" --latest=false --prerelease=false --title "${{ inputs.tag }}" --notes "wlroots 0.20.2 with tools/wlroots/patches (Vulkan DMA-BUF allocator for non-DRM GPU nodes); staged by the apk at /usr/local/lib/droiddeck-wlroots for the desktop's GPU renderers. sha256 $(cut -d' ' -f1 wlroots.tzst.sha256)" wlroots.tzst wlroots.tzst.sha256 || gh release upload "${{ inputs.tag }}" wlroots.tzst wlroots.tzst.sha256 --clobber - # A component the apk fetches by tag, never something users install: it must not - # become the repo's Latest release or appear as a pre-release, also when re-uploaded. - gh release edit "${{ inputs.tag }}" --latest=false --prerelease=false diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 8d179a4993..7b8d6be6b9 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -173,7 +173,7 @@ jobs: uses: actions/cache@v4 with: path: app/src/main/assets/linuxfs - key: preloads-${{ env.PRELOAD_CACHE_VERSION }}-${{ hashFiles('app/src/main/cpp/fakeinput_steam.cpp', 'tools/linuxfs/preload/*.c', 'tools/linuxfs/build-x86-preloads.sh', 'tools/linuxfs/fex/*', 'tools/linuxfs/clipboard/*.c', 'tools/linuxfs/ssbs/*.c', 'tools/proot/fastpath/*.c', 'tools/linuxfs/overlay/usr/local/bin/*', 'tools/linuxfs/overlay/usr/bin/**', 'tools/linuxfs/desktop/*', 'tools/gamescope/release.env', 'tools/wlroots/release.env', 'tools/linuxfs/uruntime.env', 'tools/linuxfs/licenses/*', 'tools/mangoapp/*', 'tools/msitools/**') }} + key: preloads-${{ env.PRELOAD_CACHE_VERSION }}-${{ hashFiles('app/src/main/cpp/fakeinput_steam.cpp', 'tools/linuxfs/preload/*.c', 'tools/linuxfs/build-x86-preloads.sh', 'tools/linuxfs/fex/*', 'tools/linuxfs/clipboard/*.c', 'tools/linuxfs/ssbs/*.c', 'tools/proot/fastpath/*.c', 'tools/linuxfs/overlay/usr/local/bin/*', 'tools/linuxfs/overlay/usr/bin/**', 'tools/linuxfs/desktop/*', 'tools/gamescope/release.env', 'tools/wlroots/release.env', 'tools/linuxfs/uruntime.env', 'tools/linuxfs/licenses/*', 'tools/mangoapp/*', 'tools/msitools/**', 'tools/linuxfs/debian-image.env') }} - name: Build libfakeinput.so and libblsession.so (glibc aarch64) if: steps.preloads.outputs.cache-hit != 'true' @@ -195,6 +195,9 @@ jobs: 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" # Force-SSBS adapter, preloaded into a game's Wine only when the game asks for it. aarch64-linux-gnu-gcc -shared -fPIC -O2 -Wall -pthread \ -o "$D/libssbs.so" tools/linuxfs/ssbs/ssbs_adapter.c -ldl @@ -216,14 +219,6 @@ jobs: test -f "$D/usr/local/bin/gamescope" rm gamescope.tzst fi - if [ -f tools/wlroots/release.env ]; then - . tools/wlroots/release.env - gh release download "${WLROOTS_TAG}" -R "${GITHUB_REPOSITORY}" -p wlroots.tzst -O wlroots.tzst - echo "${WLROOTS_SHA256} wlroots.tzst" | sha256sum -c - - tar --use-compress-program=unzstd -xf wlroots.tzst -C "$D" - test -f "$D/usr/local/lib/droiddeck-wlroots/libwlroots-0.20.so" - rm wlroots.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 - @@ -262,10 +257,13 @@ jobs: done install -m644 msi-pkgs/usr/bin/msiinfo "$T/msiinfo" install -m644 msi-pkgs/usr/bin/cabextract "$T/cabextract" + # 7-Zip (the full build with its format plugin) opens installer .exe wrappers. + 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; do + 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 @@ -273,17 +271,42 @@ jobs: 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/autostart "$D/etc/xdg/labwc/autostart" - install -Dm644 tools/linuxfs/desktop/rc.xml "$D/etc/xdg/labwc/rc.xml" - install -Dm644 tools/linuxfs/desktop/panel.conf "$D/etc/xdg/lxqt/panel.conf" + 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" + # The Debian 11 image the preloads are compiled in comes from a release on this repo + # (published once by ci-debian-image.yml, pinned in tools/linuxfs/debian-image.env): the + # public registry mirrors rate-limit the shared runner addresses and failed builds on a + # busy evening. Only if that download fails is the image pulled, with retries. + - name: Fetch the Debian image the preloads build in + if: steps.preloads.outputs.cache-hit != 'true' + env: + GH_TOKEN: ${{ github.token }} + run: | + 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" + - name: Build the x86 preloads FEX guests load (glibc 2.31) if: steps.preloads.outputs.cache-hit != 'true' run: | set -euo pipefail - docker run --rm -v "$PWD:/src" -w /src debian:bullseye bash -c ' + docker run --rm -v "$PWD:/src" -w /src public.ecr.aws/docker/library/debian:bullseye bash -c ' set -euo pipefail printf "deb http://archive.debian.org/debian bullseye main\ndeb http://archive.debian.org/debian-security bullseye-security main\n" > /etc/apt/sources.list apt-get -o Acquire::Check-Valid-Until=false update -qq @@ -331,6 +354,11 @@ jobs: set -euo pipefail curl -fsSL -o pa.tar.gz https://github.com/pulseaudio/pulseaudio/archive/refs/tags/v13.0.tar.gz mkdir -p pa13 && tar -xzf pa.tar.gz -C pa13 --strip-components=1 && rm pa.tar.gz + # DirectAudio - the game driver (one set per Wine audio interface), the Android-side helper and + # the Steam client's sinks - comes from a pinned release of The412Banner/directaudio, checksummed, + # exactly like gamescope; nothing compiled for audio is kept in this repo. + - name: Fetch DirectAudio (tools/directaudio/release.env) + run: tools/directaudio/fetch.sh "$PWD" "$PWD/sink-out" - name: Build the AAudio sink into the audio bundle run: | set -euo pipefail @@ -340,17 +368,12 @@ jobs: 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-sink.so pa-bundle/modules/arm64/module-directaudio-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 .) - mv pulseaudio.tzst app/src/main/assets/pulseaudio.tzst - tar --use-compress-program=unzstd -tf app/src/main/assets/pulseaudio.tzst | grep -c '\.so$' + 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 - - name: Build the DirectAudio relay - run: | - set -euo pipefail - export NDK="${ANDROID_NDK_LATEST_HOME:-${ANDROID_NDK_HOME:-$ANDROID_NDK_ROOT}}" - tools/directaudio-relay/build.sh app/src/main/jniLibs/arm64-v8a - - name: Cache proot id: proot uses: actions/cache@v4 @@ -369,42 +392,40 @@ jobs: - name: Check the preloads run: | set -euo pipefail - D=app/src/main/assets/linuxfs - NEED=$(readelf -d "$D/libfakeinput.so" | sed -n 's/.*NEEDED.*\[\(.*\)\]/\1/p') - for bad in libstdc++.so.6 libgcc_s.so.1; do - if printf '%s\n' "$NEED" | grep -qx "$bad"; then - echo "::error::libfakeinput.so links $bad - the C++ runtime must stay static"; exit 1 - fi - done - syms() { readelf -Ws "$1" | awk '$4 == "FUNC" && $5 == "GLOBAL" {sub(/@.*/, "", $8); print $8}'; } - FAKE=$(syms "$D/libfakeinput.so") - for sym in open openat ioctl read close poll ppoll select stat fstat access scandir; do - grep -qx "$sym" <<<"$FAKE" || { echo "::error::libfakeinput.so does not export $sym"; exit 1; } - done - # The static C++ runtime stays private: every session process preloads this library, - # and an exported personality routine takes over other programs' unwinding (shadPS4 - # aborted in pthread_exit). - if grep -qxE '__gxx_personality_v0|__cxa_throw|_Unwind_Resume' <<<"$FAKE"; then - echo "::error::libfakeinput.so exports the C++ runtime"; exit 1 - fi - SESSION=$(syms "$D/libblsession.so") - for sym in socket bind getsockname setsockopt statfs statvfs syscall shm_open shm_unlink mmap mmap64 munmap drmPrimeFDToHandle drmCloseBufferHandle; do - grep -qx "$sym" <<<"$SESSION" || { echo "::error::libblsession.so does not export $sym"; exit 1; } - done - # Zink's GEM_CLOSE is caught at ioctl(); an exported drmIoctl made NMS replay old frames. - if grep -qx drmIoctl <<<"$SESSION"; then - echo "::error::libblsession.so exports drmIoctl"; exit 1 - fi - SSBS=$(syms "$D/libssbs.so") - for sym in sigaction signal bsd_signal sysv_signal; do - grep -qx "$sym" <<<"$SSBS" || { echo "::error::libssbs.so does not export $sym"; exit 1; } - done - test -f "$D/usr/local/bin/droiddeck-session" - test -f "$D/usr/local/bin/droiddeck-proton-extra" - test -f "$D/usr/local/bin/droiddeck-msi-install" - for f in msiinfo cabextract libmsi-1.0.so.0 libgsf-1.so.114 libgcab-1.0.so.0; do - test -f "$D/usr/local/lib/droiddeck-msitools/$f" || { echo "::error::droiddeck-msitools/$f missing"; exit 1; } - done + bash tools/linuxfs/check-preloads.sh app/src/main/assets/linuxfs + + # The store download engines (app/src/main/rust/stores) are Rust, built by Gradle's + # buildRustStores task through cargo-ndk. rustup and cargo are on the runner image; the Android + # target and cargo-ndk are added here. Cache = cargo registry + cargo-ndk + the crate's target + # tree, keyed on the rustc version and Cargo.lock, so a source-only change rebuilds this crate + # on top of the cached dependency objects, and a lockfile change still starts from the + # previous objects (restore-keys). + - name: Rust toolchain for the store engines (aarch64-linux-android) + id: rust + run: | + set -euo pipefail + rustup target add aarch64-linux-android + echo "rustc=$(rustc -V | awk '{print $2}')" >> "$GITHUB_OUTPUT" + rustc -V; cargo -V + + - name: Cache cargo registry and the store engines' target + uses: actions/cache@v4 + with: + path: | + ~/.cargo/registry/index + ~/.cargo/registry/cache + ~/.cargo/git/db + ~/.cargo/bin/cargo-ndk + app/src/main/rust/stores/target + key: stores-${{ steps.rust.outputs.rustc }}-${{ hashFiles('app/src/main/rust/stores/Cargo.lock') }} + restore-keys: | + stores-${{ steps.rust.outputs.rustc }}- + + - name: cargo-ndk + run: | + set -euo pipefail + command -v cargo-ndk >/dev/null || cargo install cargo-ndk --locked + cargo ndk --version - name: Cache the CMake build tree uses: actions/cache@v4 @@ -420,10 +441,46 @@ jobs: id: build env: DROIDDECK_BUILD_LABEL: ${{ steps.build-label.outputs.label }} + SGDB_API_KEY: ${{ secrets.SGDB_API_KEY }} run: | set -o pipefail + python3 tools/release/runtime_inputs.py record ./gradlew assembleRelease --console=plain -PndkVersion="$NDK_VERSION" 2>&1 | tee /tmp/build.log python3 tools/release/check_session_assets.py app/build/outputs/apk/release/app-release.apk + python3 tools/release/runtime_inputs.py apk app/build/outputs/apk/release/app-release.apk + + - name: Check the store engines' library + # The exports Kotlin binds must be in the apk's libdroiddeckstores.so, or the stores fail on + # the device with an UnsatisfiedLinkError instead of here. The lockfile check is the + # reproducibility guard: cargo must not have had to re-resolve anything. + run: | + set -euo pipefail + NDK="${ANDROID_NDK_LATEST_HOME:-${ANDROID_NDK_HOME:-$ANDROID_NDK_ROOT}}" + python3 tools/release/check_android_libraries.py app/build/outputs/apk/release/app-release.apk \ + --readelf "$NDK/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-readelf" + git diff --stat -- app/src/main/rust/stores/Cargo.lock + git diff --exit-code -- app/src/main/rust/stores/Cargo.lock \ + || { echo "::error::Cargo.lock changed during the build; commit the regenerated lockfile (uploaded as stores-cargo-lock)"; exit 1; } + + - name: Store engines host unit tests + # The crate's #[test] functions are pure host-side logic (manifest parsing, chunk plans, + # assembly from a cache, hashes), so a plain `cargo test` on the runner's own architecture + # runs them; the Android build above compiles none of them. Informational on this first + # run - the point is to learn whether they pass - and bounded so a hang can never hold + # the apk. Make it blocking once it is known green. + timeout-minutes: 10 + continue-on-error: true + run: | + cd app/src/main/rust/stores + cargo test --locked 2>&1 | tail -60 + + - name: Upload the regenerated Cargo.lock + if: failure() + uses: actions/upload-artifact@v4 + with: + name: stores-cargo-lock + path: app/src/main/rust/stores/Cargo.lock + if-no-files-found: ignore - name: Steam import and game environment tests run: python3 -m unittest discover -s tools/tests -p 'test_*.py' diff --git a/.github/workflows/ci-debian-image.yml b/.github/workflows/ci-debian-image.yml new file mode 100644 index 0000000000..1635ed4074 --- /dev/null +++ b/.github/workflows/ci-debian-image.yml @@ -0,0 +1,55 @@ +name: Publish the CI Debian image + +# Pulls the Debian 11 image the x86 preloads build in, once, and publishes it as a release asset +# on this repo so build.yml never depends on a registry mirror's rate limit. Run it again (with a +# new tag) only to refresh the image; tools/linuxfs/debian-image.env names the one builds use. + +on: + workflow_dispatch: + inputs: + tag: + description: 'Release tag to publish to (a new -rN tag for a refresh; builds pin the one in tools/linuxfs/debian-image.env)' + required: true + default: 'ci-debian-bullseye-r2' + replace: + description: 'Overwrite the asset if the release already has one (changes its checksum: re-pin it in debian-image.env)' + type: boolean + default: false + +permissions: + contents: write + +jobs: + publish: + runs-on: ubuntu-24.04 + timeout-minutes: 30 + steps: + - name: Pull, save and publish + env: + GH_TOKEN: ${{ github.token }} + TAG: ${{ inputs.tag }} + REPLACE: ${{ inputs.replace }} + run: | + set -euo pipefail + image=public.ecr.aws/docker/library/debian:bullseye + # A published image is pinned by checksum in the build; never replace one by accident. + if [ "$REPLACE" != true ] && gh release view "$TAG" -R "$GITHUB_REPOSITORY" --json assets -q '.assets[].name' 2>/dev/null | grep -qx debian-bullseye.tar.zst; then + echo "::error::$TAG already carries debian-bullseye.tar.zst; publish to a new tag, or set replace and re-pin tools/linuxfs/debian-image.env" + exit 1 + fi + for attempt in 1 2 3 4 5 6; do + docker pull "$image" && break + [ "$attempt" -lt 6 ] || { echo "::error::could not pull $image"; exit 1; } + echo "pull attempt $attempt failed (registry busy?); retrying in $((attempt * 30)) s" + sleep $((attempt * 30)) + done + docker image inspect "$image" --format 'image id {{.Id}} created {{.Created}}' + docker save "$image" | zstd -19 -T0 -o debian-bullseye.tar.zst + sha256sum debian-bullseye.tar.zst | tee SHA256SUMS + ls -l debian-bullseye.tar.zst + if ! gh release view "$TAG" -R "$GITHUB_REPOSITORY" >/dev/null 2>&1; then + gh release create "$TAG" -R "$GITHUB_REPOSITORY" --title "CI Debian 11 image ($TAG)" --latest=false \ + --notes "The debian:bullseye image build.yml compiles the x86 preloads in (glibc 2.31), saved with docker save and compressed with zstd. Pulled from public.ecr.aws/docker/library/debian:bullseye on $(date -u +%Y-%m-%d); image id $(docker image inspect "$image" --format '{{.Id}}'). Pinned in tools/linuxfs/debian-image.env." + fi + gh release upload "$TAG" -R "$GITHUB_REPOSITORY" debian-bullseye.tar.zst SHA256SUMS --clobber + { echo "Published \`$TAG\`:"; echo; echo '```'; cat SHA256SUMS; echo '```'; } >> "$GITHUB_STEP_SUMMARY" diff --git a/.github/workflows/pr-release-cleanup.yml b/.github/workflows/pr-release-cleanup.yml index 0cd4867e69..af4581f45b 100644 --- a/.github/workflows/pr-release-cleanup.yml +++ b/.github/workflows/pr-release-cleanup.yml @@ -13,13 +13,28 @@ permissions: contents: read pull-requests: read +concurrency: + group: pr-release-${{ github.event.pull_request.number }} + cancel-in-progress: false + queue: max + jobs: cleanup: runs-on: ubuntu-24.04 timeout-minutes: 5 steps: + - name: Check the current PR state + id: state + env: + GH_TOKEN: ${{ github.token }} + PR_NUMBER: ${{ github.event.pull_request.number }} + run: | + set -euo pipefail + state=$(gh api "repos/$GITHUB_REPOSITORY/pulls/$PR_NUMBER" --jq .state) + echo "closed=$([ "$state" = closed ] && echo true || echo false)" >> "$GITHUB_OUTPUT" # A token of the org's "DroidDeck CI Publisher" app, as build.yml's announce job uses. - uses: actions/create-github-app-token@v2 + if: ${{ steps.state.outputs.closed == 'true' }} id: ci-token with: app-id: ${{ vars.DROIDDECK_CI_APP_ID }} @@ -27,6 +42,7 @@ jobs: owner: Droid-Deck repositories: DroidDeck-CI - name: Delete the test build + if: ${{ steps.state.outputs.closed == 'true' }} env: GH_TOKEN: ${{ steps.ci-token.outputs.token }} PR_NUMBER: ${{ github.event.pull_request.number }} @@ -42,6 +58,19 @@ jobs: done [ "$found" = 1 ] || echo "No test build for PR #$PR_NUMBER" + - uses: actions/checkout@v4 + if: ${{ steps.state.outputs.closed == 'true' }} + with: + ref: main + persist-credentials: false + - name: Retire the Discord PR announcement + if: ${{ steps.state.outputs.closed == 'true' }} + env: + WEBHOOK: ${{ secrets.DISCORD_PR_BUILDS_WEBHOOK }} + GH_TOKEN: ${{ github.token }} + PR_NUMBER: ${{ github.event.pull_request.number }} + run: python3 tools/release/pr_discord.py retire + catalog: needs: cleanup if: ${{ always() }} diff --git a/.github/workflows/pr-release-publish.yml b/.github/workflows/pr-release-publish.yml index 6679d94824..6c5070b270 100644 --- a/.github/workflows/pr-release-publish.yml +++ b/.github/workflows/pr-release-publish.yml @@ -26,6 +26,7 @@ permissions: concurrency: group: pr-release-${{ github.event_name == 'pull_request_target' && github.event.pull_request.number || github.event_name == 'workflow_run' && github.event.workflow_run.pull_requests[0].number || inputs.pr_number }} cancel-in-progress: false + queue: max jobs: authorize: @@ -178,6 +179,10 @@ jobs: issues: write pull-requests: write steps: + - uses: actions/checkout@v4 + with: + ref: main + persist-credentials: false - uses: actions/download-artifact@v4 with: pattern: droiddeck-pr-signed-* @@ -307,39 +312,27 @@ jobs: $NEXT EOF ) - id=$(gh api "repos/$GITHUB_REPOSITORY/issues/$PR_NUMBER/comments" --paginate \ - --jq ".[] | select(.body | startswith(\"$mark\")) | .id" | head -1 || true) + comment=$(gh api "repos/$GITHUB_REPOSITORY/issues/$PR_NUMBER/comments" --paginate --slurp | \ + jq "[.[][] | select(.user.login == \"github-actions[bot]\") | select(.body | startswith(\"$mark\"))] | first // {}") + id=$(jq -r '.id // empty' <<<"$comment") if [ -n "$id" ]; then + state=$(jq -r '.body | split("\n")[] | select(startswith(""))' <<<"$comment") + if [ -n "$state" ]; then body=$(printf '%s\n%s' "$body" "$state"); fi gh api -X PATCH "repos/$GITHUB_REPOSITORY/issues/comments/$id" -f body="$body" >/dev/null else gh api -X POST "repos/$GITHUB_REPOSITORY/issues/$PR_NUMBER/comments" -f body="$body" >/dev/null fi - # Asked for (the label, a manual run): always announced. Signed on its own after a push: - # only a PR's first test build is, and later ones update its release quietly. - - name: Post to Discord - if: ${{ always() && steps.publish.outcome == 'success' && (needs.authorize.outputs.automatic != 'true' || steps.publish.outputs.first == 'true') }} + - name: Update the Discord PR announcement + if: ${{ always() && steps.publish.outcome == 'success' }} env: - WEBHOOK: ${{ secrets.DISCORD_BUILDS_WEBHOOK }} + WEBHOOK: ${{ secrets.DISCORD_PR_BUILDS_WEBHOOK }} GH_TOKEN: ${{ github.token }} PR_NUMBER: ${{ needs.authorize.outputs.pr_number }} HEAD_SHA: ${{ needs.authorize.outputs.head_sha }} RELEASE: ${{ steps.publish.outputs.release }} APK: ${{ steps.publish.outputs.apk }} - run: | - set -euo pipefail - if [ -z "$WEBHOOK" ]; then - echo "::notice::No DISCORD_BUILDS_WEBHOOK secret; nothing posted" - exit 0 - fi - short=${HEAD_SHA:0:7} - title=$(gh api "repos/$GITHUB_REPOSITORY/pulls/$PR_NUMBER" --jq .title) - what="[#$PR_NUMBER]($GITHUB_SERVER_URL/$GITHUB_REPOSITORY/pull/$PR_NUMBER): $title" - get=$(printf 'Download: [standard APK](<%s>) · [all variants](<%s>)' "$APK" "$RELEASE") - content=$(printf '**New signed PR test build** \`pr-%s\` · \`%s\`\n%s\n_Not merged — for testing._\n%s' "$PR_NUMBER" "$short" "$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" + run: python3 tools/release/pr_discord.py publish catalog: needs: publish diff --git a/.github/workflows/update-catalog.yml b/.github/workflows/update-catalog.yml index 25b40a3995..d3a4075b5d 100644 --- a/.github/workflows/update-catalog.yml +++ b/.github/workflows/update-catalog.yml @@ -13,6 +13,7 @@ permissions: concurrency: group: update-app-catalog cancel-in-progress: false + queue: max jobs: catalog: diff --git a/.gitignore b/.gitignore index 7601e9a431..a9e068c0d1 100644 --- a/.gitignore +++ b/.gitignore @@ -13,8 +13,12 @@ captures/ app/src/main/assets/linuxfs/ app/src/main/jniLibs/arm64-v8a/libproot.so app/src/main/jniLibs/arm64-v8a/.proot-inputs +app/src/main/jniLibs/arm64-v8a/.proot-outputs app/src/main/jniLibs/arm64-v8a/libproot-loader.so app/src/main/jniLibs/arm64-v8a/libdirectaudiorelay.so +# The store download engines (app/src/main/rust/stores) are cross-compiled by Gradle into +# app/build/rustJniLibs; cargo's own build tree stays next to the crate. Neither is source. +app/src/main/rust/stores/target/ app/src/main/assets/droiddeck-esync/ # Kotlin compiler session files (build-tool state, never source). @@ -35,3 +39,4 @@ __pycache__/ # Local signing with the release key (tools/build_local.sh); holds a password. .signing.env +app/src/main/assets/directaudio/ diff --git a/README.md b/README.md index e9b495d1a0..ceaef7ff73 100644 --- a/README.md +++ b/README.md @@ -15,21 +15,25 @@ DroidDeck brings the SteamOS experience to Android: Valve's Steam client in Big ## Requirements and install -Use Android 9 or newer on a supported Adreno device (730 or newer, or 8xx). Adreno 6xx is experimental: DirectX 11 uses DXVK 2 and may run, and DirectX 12 games can still crash. Mali, Xclipse, PowerVR, and Adreno 710 are unsupported. No root is required. Allow about 3 GB for the runtime and 1.1 GB more for the desktop and emulators. Install the APK from [Releases](https://github.com/Droid-Deck/DroidDeck/releases), install the Linux runtime, then press **Play** and sign in. Steam downloads on first launch. Install **Desktop & apps** to use the desktop and emulators. The **Store** installs Linux apps and games from Flathub (ARM64 builds) with Flatpak; with additional options to install Appimages and set up scripts. +Use Android 9 or newer on a supported Adreno device (730 or newer, or 8xx). Adreno 6xx is experimental: DirectX 11 uses DXVK 2 and may run, and DirectX 12 games can still crash. Mali, Xclipse, PowerVR, and Adreno 710 are unsupported. No root is required. Allow about 3 GB for the runtime and 1.1 GB more for the desktop and emulators. Install the APK from [Releases](https://github.com/Droid-Deck/DroidDeck/releases), install the Linux runtime, then press **Play** and sign in. Steam downloads on first launch. Install **Desktop & apps** to use the desktop and emulators. The **Store** installs Linux apps and games from Flathub (ARM64 builds) with Flatpak; with additional options to install Appimages and set up scripts. **Stores** (Setup › Stores › "Show Stores in the rail") signs in to GOG, Epic Games and Amazon Games, browses their catalogs and libraries, downloads games into the Games storage and adds each one to the Steam client as a non-Steam game, so it launches through Proton like everything else; the cog on its chip row sets which tab a store opens on and the download speed tier. Before Steam launches, you must turn off **Restrict child processes** in Developer options. If this option is not available in developer settings (Android 12 and 13 devices), first launch of Steam will present a "Fix it for me" button, which will help automate the setup process. ## Community -Join the [DroidDeck Discord](https://discord.gg/JRGAvawjsm) for help, Preview builds, and device reports. Bug reports go in its **#bug-reports** forum; attach the session folder from `Download/DroidDeck/` so the logs come with it. +Join the [DroidDeck Discord](https://discord.gg/JRGAvawjsm) for help, Preview builds, and device reports. Bug reports go in its **#bug-reports** forum; attach the zip from **Share logs** (Setup, or the session drawer) so the logs come with it. ## Build -Run `tools/build_local.sh` with Docker, Java 17, the Android SDK/NDK, and `zstd` installed. It builds the ARM64 audio sinks from PulseAudio 13.0 and packages them into the APK at `app/build/outputs/apk/release/app-release.apk`. Set `DROIDDECK_PA13_SOURCE_DIR` to an existing PulseAudio 13.0 source directory to skip downloading it. To install the APK on an attached device, run `tools/deploy_local.sh`. +Run `tools/build_local.sh` with Docker, Java 17, the Android SDK/NDK, Rust through rustup, GitHub CLI, Python 3, and `zstd` installed. It prepares the same native libraries, pinned components and audio sinks as CI, then checks the APK's assets, JNI exports and dependencies. The APK is at `app/build/outputs/apk/release/app-release.apk`. Set `DROIDDECK_PA13_SOURCE_DIR` to an existing PulseAudio 13.0 source directory to skip downloading it. Set `DROIDDECK_SIGNING_ENV` to a signing environment file to install over a release build; relative keystore paths resolve beside that file. To install on an attached device, run `tools/deploy_local.sh`. + +After preparation, ordinary `./gradlew :app:assembleRelease` builds can reuse the verified inputs. If native sources, component pins or staged files change, packaging stops with instructions to rerun the helper. Generated audio stays under `app/build/`; the tracked base bundle is never modified. `./gradlew -PskipRust=true :app:testDebugUnitTest` needs no native preparation. For an APK without store engines, use `DROIDDECK_SKIP_RUST=1 tools/build_local.sh`; it omits cached engine libraries too. + +Run the helper and preload regression suite with `bash tools/test_local.sh`. On macOS it runs in Linux through Docker, matching the helpers' runtime and CI instead of compiling Linux preloads against macOS headers. ## Limits -Compatibility and performance vary by device; hardware validation is limited. Desktop compositing uses software rendering. Firefox sandboxing is reduced under proot. See the session logs in `Download/DroidDeck/` when diagnosing problems. +Compatibility and performance vary by device; hardware validation is limited. Desktop compositing uses software rendering. Firefox sandboxing is reduced under proot. Logs are kept app-private (`files/logs/`, the last 30 sessions); **Share logs** packs the newest session, with the stores and tool logs, into one scrubbed zip. ## Credits and licence diff --git a/app/build.gradle b/app/build.gradle index 7dafc08bc8..5f0052c30f 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -59,6 +59,19 @@ def ciBuild = providers.environmentVariable('GITHUB_ACTIONS').getOrNull() == 'tr def releaseSigner = rootProject.file('keystore/release-signer.sha256').text.trim() // When the source was committed, for "built 3 hours ago": fixed per commit, so it costs no rebuilds. def commitTime = (gitValue(['show', '-s', '--format=%ct', 'HEAD']) ?: '0').with { it.isLong() ? it : '0' } +// The desktop package this apk installs (tools/desktop-kde/release.env): published once per tag in +// DroidDeck-Components, pinned here with its checksum like gamescope and the esync packs, so the +// desktop scripts in the apk always get the package they were made for. +def desktopKde = rootProject.file('tools/desktop-kde/release.env').readLines() + .findAll { it.contains('=') && !it.startsWith('#') } + .collectEntries { def kv = it.split('=', 2); [(kv[0].trim()): kv[1].trim()] } +def desktopRemovalSources = new groovy.json.JsonSlurper().parse(file('src/main/assets/desktop-removal/sources.json')) +if (desktopRemovalSources.archives[desktopKde.DESKTOP_KDE_TAG] != desktopKde.DESKTOP_KDE_SHA256) { + throw new GradleException('Desktop removal inventories do not match the pinned archive') +} +if (!file('src/main/assets/desktop-removal/versions.txt').readLines().contains(desktopKde.DESKTOP_KDE_TAG)) { + throw new GradleException('Regenerate desktop removal inventories for ' + desktopKde.DESKTOP_KDE_TAG) +} // tools/release/variants.txt as "package=suffix;...", so the updater picks the apk of the package it runs as. def variantSuffixes = rootProject.file('tools/release/variants.txt').readLines() .findAll { it.trim() && !it.startsWith('#') } @@ -79,8 +92,8 @@ android { // file out of its own data directory, and this app's entire job is to exec a rootfs: // proot, gamescope, the Steam client and everything it launches all live under files/. targetSdkVersion 28 - versionCode 11 - versionName "0.3.1" + versionCode 12 + versionName "0.3.2" buildConfigField "String", "BUILD_LABEL", "\"${escapedBuildLabel}\"" buildConfigField "String", "SOURCE_COMMIT", "\"${sourceCommit}\"" buildConfigField "int", "PR_NUMBER", "${prNumber}" @@ -88,6 +101,11 @@ android { buildConfigField "String", "RELEASE_SIGNER", "\"${releaseSigner}\"" buildConfigField "long", "COMMIT_TIME", "${commitTime}L * 1000L" buildConfigField "String", "VARIANT_SUFFIXES", "\"${variantSuffixes}\"" + buildConfigField "String", "SGDB_API_KEY", "\"${System.getenv("SGDB_API_KEY") ?: project.findProperty("sgdbApiKey") ?: ""}\"" + buildConfigField "String", "DESKTOP_KDE_TAG", "\"${desktopKde.DESKTOP_KDE_TAG}\"" + buildConfigField "String", "DESKTOP_KDE_URL", "\"https://github.com/${desktopKde.DESKTOP_KDE_REPO}/releases/download/${desktopKde.DESKTOP_KDE_TAG}/desktop-kde.tar.zst\"" + buildConfigField "String", "DESKTOP_KDE_SHA256", "\"${desktopKde.DESKTOP_KDE_SHA256}\"" + buildConfigField "long", "DESKTOP_KDE_SIZE", "${desktopKde.DESKTOP_KDE_SIZE}L" ndk { abiFilters 'arm64-v8a' @@ -188,6 +206,76 @@ def stageSessionScripts = tasks.register('stageSessionScripts', Copy) { } tasks.named('preBuild').configure { dependsOn(stageSessionScripts) } +// Prepared audio lives under build/, so native builds never rewrite the tracked base bundle. +// Copying into one asset root also prevents an old base bundle winning a duplicate asset merge. +def runtimeAssetsDir = layout.buildDirectory.dir('runtimeAssets') +def stageRuntimeAssets = tasks.register('stageRuntimeAssets', Sync) { + dependsOn(stageSessionScripts) + from(layout.projectDirectory.dir('src/main/assets')) { exclude 'pulseaudio.tzst' } + from(layout.buildDirectory.dir('prepared-assets')) + into(runtimeAssetsDir) +} +android.sourceSets.main.assets.setSrcDirs([runtimeAssetsDir]) +tasks.named('preBuild').configure { dependsOn(stageRuntimeAssets) } + +// Unit tests and previews remain usable without Docker. Packaging an APK requires the complete +// runtime payload prepared by the local helper or CI, with matching sources and output hashes. +def verifyRuntimeInputs = tasks.register('verifyRuntimeInputs', Exec) { + // The session scripts are re-copied into the asset tree by stageSessionScripts; with parallel + // task execution this check once hashed a file mid-copy and failed a main build. Hash only + // once the staging has finished. + mustRunAfter(stageSessionScripts, stageRuntimeAssets) + workingDir rootProject.projectDir + commandLine 'python3', 'tools/release/runtime_inputs.py', 'verify' +} +tasks.configureEach { task -> + if (task.name in ['packageDebug', 'packageRelease']) task.dependsOn(verifyRuntimeInputs) +} + +// The GOG / Epic / Amazon download engines are Rust (app/src/main/rust/stores, ported from +// Bannerlator's bl-steam-client crate). cargo-ndk cross-compiles them for arm64-v8a into the build +// directory, which is one more jniLibs source below: nothing compiled is committed, the same rule +// the preloads, proot and the DirectAudio relay follow. cargo-ndk exports the NDK clang/llvm-ar as +// the C compiler for the one C-compiling crate (ring, under rustls) - the step Bannerlator's CI +// does by hand - and reads the NDK from ANDROID_NDK_HOME, which is set to the NDK this build pins. +// -PskipRust=true builds without the library, for a checkout with no Rust toolchain: the stores +// then report themselves unavailable instead of the build failing. +def rustCrateDir = layout.projectDirectory.dir('src/main/rust/stores') +def rustJniLibsDir = layout.buildDirectory.dir('rustJniLibs') +def skipRust = (project.findProperty('skipRust') ?: 'false').toString().toBoolean() +def buildRustStores = tasks.register('buildRustStores', Exec) { + description = 'Cross-compiles the store download engines (libdroiddeckstores.so) with cargo-ndk.' + onlyIf { !skipRust } + inputs.dir(rustCrateDir.dir('src')) + inputs.files(rustCrateDir.file('Cargo.toml'), rustCrateDir.file('Cargo.lock')) + inputs.property('ndkVersion', android.ndkVersion) + if (!skipRust) inputs.property('rustToolchain', providers.exec { + environment 'PATH', "${System.getProperty('user.home')}/.cargo/bin${File.pathSeparator}${System.getenv('PATH')}" + commandLine 'rustup', 'run', 'stable', 'rustc', '-Vv' + }.standardOutput.asText) + outputs.dir(rustJniLibsDir) + workingDir rustCrateDir + // The configuration cache serialises the task, so everything it needs is resolved here, not + // in doFirst. ~/.cargo/bin is where rustup puts cargo and cargo-ndk; an IDE-launched Gradle + // does not always have it on PATH. + def cargoBin = "${System.getProperty('user.home')}/.cargo/bin" + environment 'PATH', "${cargoBin}${File.pathSeparator}${System.getenv('PATH')}" + environment 'ANDROID_NDK_HOME', android.ndkDirectory.absolutePath + environment 'CARGO_TERM_COLOR', 'never' + if (!skipRust) environment 'RUSTC', providers.exec { + commandLine 'rustup', 'which', '--toolchain', 'stable', 'rustc' + }.standardOutput.asText.get().trim() + // --platform 26 = minSdkVersion: the engines link against the API 26 libc, like the C++ code. + // Homebrew's cargo/rustc can precede rustup on macOS and use a different sysroot, despite + // rustup reporting the Android target installed. Run both through the selected toolchain. + commandLine 'rustup', 'run', 'stable', 'cargo', 'ndk', '--target', 'arm64-v8a', '--platform', '26', + '--output-dir', rustJniLibsDir.get().asFile.absolutePath, + 'build', '--release', '--locked' +} +// Skipping Rust must omit its library even if another build left a cached .so behind. +if (!skipRust) android.sourceSets.main.jniLibs.srcDir(rustJniLibsDir) +tasks.named('preBuild').configure { dependsOn(buildRustStores) } + dependencies { // The runtime tarball is a zstd-compressed tar. implementation 'com.github.luben:zstd-jni:1.5.2-3@aar' diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 7fa9d195aa..e0f1038414 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -8,6 +8,8 @@ + + @@ -127,6 +129,13 @@ android:screenOrientation="sensorLandscape" android:theme="@style/AppTheme" /> + + + + + /directaudio/lib/wine/aarch64-unix/winedirectaudio.so -/directaudio/lib/wine/aarch64-windows/winedirectaudio.drv -/directaudio/lib/wine/i386-windows/winedirectaudio.drv -``` - -and set, for the game process: - -``` -WINEDLLPATH=/directaudio/lib/wine -``` - -Wine searches `WINEDLLPATH` entries with the per-arch subdirectories appended, -so both the PE shell and the unixlib are found there. In Bannerlator's runtime -the natural place to export it is the `bannerlator-proton` compatibility-tool -wrapper, which every Proton launch goes through. - -## Selecting the driver - -`mmdevapi` only tries the drivers named in the registry (or its built-in -`pulse,alsa,oss,coreaudio` list, which does not know about this one). Set, in the -game's prefix: - -``` -wine reg add "HKCU\Software\Wine\Drivers" /v Audio /d directaudio /f -``` - -Under Proton the prefix is `compatdata//pfx`; run the command with that -prefix as `WINEPREFIX` using Proton's own `bin-arm64/wine`, or write the key from -the wrapper before it hands off to Proton. - -## Running the helper - -``` -directaudio-relay --socket [--log] -``` - -- Run it **under the app's uid**, the way the app already runs its PulseAudio - daemon. Android checks `RECORD_AUDIO` against the calling uid, so a helper - started any other way can play but cannot record. -- Start it **before** the game: the driver asks "is a helper there?" once, when - Wine picks an audio driver, and falls back to unavailable if not. -- The socket path must be reachable from inside the Linux rootfs. Bannerlator - binds the app's files directory and its runtime directory into the session at - their own paths, so any socket under either works unchanged on both sides. -- The helper serves any number of game processes; each gets its own streams. - It exits on `SIGTERM`; a game exiting tears down that game's streams only. - -## Telling the driver where the helper is - -``` -BANNER_AUDIO_DIRECT_RELAY= -``` - -Unset, the driver looks for `$XDG_RUNTIME_DIR/directaudio-relay`. - -## Everything else is unchanged - -All `BANNER_AUDIO_DIRECT_*` knobs mean what they mean in the in-process build: -`_PERF`, `_ADAPTIVE`, `_DECAY`, `_MS`, `_MAXMS`, `_BF`, `_MBF`, `_EXCLUSIVE`, -`_WATCHDOG`, `_STALL_MS`, `_DECAY_*`, `_LOG`, `_PERIOD_MS`, `_MINPERIOD_MS`, and -the live-config mailbox `_RUNTIME` (the helper watches the file; it must be a -path the helper can read on the Android side). **`BANNER_AUDIO_DIRECT_MIC=1`** -exposes the capture endpoint exactly as before: the helper opens the input -stream (`VOICE_COMMUNICATION` preset) when the game connects and makes it hot on -the game's first capture start. - -One thing is different: there is one extra hand-off, the ring. The helper's -callback pulls a burst from it, so the driver keeps two bursts queued (about 8 ms -on a 192-frame device) on top of the AAudio buffer the presets control, and the -helper raises that if a callback ever finds the ring short. `get_latency` reports -the sum, so a game that sizes its own buffers from it still sees the truth. - -## Proving it is running - -- Helper side: `logcat -s DA-Relay:I` shows `hello from `, then `open: buffer - ... ring target ...`, then `capture open` / `capture start` for the mic. -- Game side: the session log carries `DirectAudio: relay: connected - ...`. -- The game process's `/proc//maps` shows `winedirectaudio.so` and **no** - `libaaudio.so` (it is in the helper's maps instead). The AudioFlinger track is - owned by the helper's pid. diff --git a/app/src/main/assets/directaudio/linux-wine11/aarch64-unix/winedirectaudio.so b/app/src/main/assets/directaudio/linux-wine11/aarch64-unix/winedirectaudio.so deleted file mode 100644 index 7d3083052a..0000000000 Binary files a/app/src/main/assets/directaudio/linux-wine11/aarch64-unix/winedirectaudio.so and /dev/null differ diff --git a/app/src/main/assets/directaudio/linux-wine11/aarch64-windows/winedirectaudio.drv b/app/src/main/assets/directaudio/linux-wine11/aarch64-windows/winedirectaudio.drv deleted file mode 100644 index d078149dc9..0000000000 Binary files a/app/src/main/assets/directaudio/linux-wine11/aarch64-windows/winedirectaudio.drv and /dev/null differ diff --git a/app/src/main/assets/directaudio/linux-wine11/i386-windows/winedirectaudio.drv b/app/src/main/assets/directaudio/linux-wine11/i386-windows/winedirectaudio.drv deleted file mode 100644 index 93efd051f7..0000000000 Binary files a/app/src/main/assets/directaudio/linux-wine11/i386-windows/winedirectaudio.drv and /dev/null differ diff --git a/app/src/main/assets/directaudio/linux-wine11/version.txt b/app/src/main/assets/directaudio/linux-wine11/version.txt deleted file mode 100644 index a32f892380..0000000000 --- a/app/src/main/assets/directaudio/linux-wine11/version.txt +++ /dev/null @@ -1,4 +0,0 @@ -directaudio linux-wine11 relay build -source 3548a4a730e8d01cb0cb0268d51a2fb8f23dcd33 -wine ValveSoftware/wine@proton_11.0 -unixlib.h sha256 fdbed263338d0719c4d12e02b045c23721de33219ec8878e4c2d7ebc9efcbe09 diff --git a/app/src/main/cpp/fakeinput_steam.cpp b/app/src/main/cpp/fakeinput_steam.cpp index d1c69d2a4d..c4a1da0ac8 100644 --- a/app/src/main/cpp/fakeinput_steam.cpp +++ b/app/src/main/cpp/fakeinput_steam.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -301,23 +302,43 @@ namespace Logger { int log_enabled; // FAKE_EVDEV_LOG_FILE: the session's pad.log, shared by every process the library is loaded in // (the client, each game), so a pad report has the guest side in one file of the session folder. -// Opened with the raw syscall: open() is this library's own hook. -static int log_fd = -1; +// Opened with the raw syscalls (open() and close() are this library's own hooks), and only once +// there is a line to write: most processes never log one, and each open is a stop in proot on +// shared storage. +static std::atomic log_fd{-1}; +static std::atomic log_unusable{false}; +static char log_path[PATH_MAX]; static char log_name[17]; void init() { log_enabled = getenv("FAKE_EVDEV_LOG") && atoi(getenv("FAKE_EVDEV_LOG")); const char *path = getenv("FAKE_EVDEV_LOG_FILE"); - if (log_enabled && path && *path) - log_fd = static_cast(syscall(SYS_openat, AT_FDCWD, path, O_WRONLY | O_APPEND | O_CREAT | O_CLOEXEC, 0644)); + if (log_enabled && path && *path && strlen(path) < sizeof(log_path)) strcpy(log_path, path); if (prctl(PR_GET_NAME, log_name) != 0) strcpy(log_name, "?"); } +static int log_file() { + int fd = log_fd.load(std::memory_order_acquire); + if (fd >= 0 || !log_path[0] || log_unusable.load(std::memory_order_relaxed)) return fd; + fd = static_cast(syscall(SYS_openat, AT_FDCWD, log_path, O_WRONLY | O_APPEND | O_CREAT | O_CLOEXEC, 0644)); + if (fd < 0) { + log_unusable.store(true, std::memory_order_relaxed); + return -1; + } + int expected = -1; + if (!log_fd.compare_exchange_strong(expected, fd, std::memory_order_acq_rel)) { + syscall(SYS_close, fd); + fd = expected; + } + return fd; +} + void log(const char *message, ...) { if (!log_enabled) return; - if (log_fd < 0) { + int fd = log_file(); + if (fd < 0) { va_list args; va_start(args, message); vfprintf(stderr, message, args); @@ -343,7 +364,7 @@ void log(const char *message, ...) { n = sizeof(line) - 1; line[n - 1] = '\n'; } - ssize_t ignored = write(log_fd, line, n); + ssize_t ignored = write(fd, line, n); (void)ignored; } } // namespace Logger @@ -1216,6 +1237,7 @@ struct DeckHidraw { FakeInputRingHeader *ring = nullptr; size_t mapping_size = 0; uint8_t pending_feature = 0; + bool touch = false; // Steam's touch controller (below), not the Deck }; // What the Deck has and an Xbox pad does not, after the ring's events: motion (PadMotion) and the @@ -1315,13 +1337,15 @@ __attribute__((visibility("hidden"))) static bool is_deck_hidraw_path(const char return pathname && fake_deck_enabled() && !strcmp(pathname, DECK_HIDRAW_PATH); } -// The app's own pads are the Deck now; their evdev nodes are withdrawn from everyone. +// The app's first pad is the Deck now: its evdev node is withdrawn from everyone. Further players' +// pads are left to the client alone, which hands them to games through Steam Input. __attribute__((visibility("hidden"))) static bool is_withdrawn_pad_path(const char *pathname) { if (!fake_deck_enabled() || !pathname || strncmp(pathname, "/dev/input/", 11)) return false; const char *event = strrchr(pathname, '/') + 1; if (strncmp(event, "event", 5) && strncmp(event, "js", 2)) return false; int number = get_event_number(event); - return number >= 0 && number < UINPUT_EVENT_BASE; + if (number < 0 || number >= UINPUT_EVENT_BASE) return false; + return number == 0 || !process_is_steam_client(); } static inline void put16(uint8_t *at, int value) { @@ -1483,6 +1507,321 @@ __attribute__((visibility("hidden"))) static int open_deck_hidraw(int flags) { return pair[0]; } + +// ---- Steam's touch controller ---- +// +// docs/development/steam-touch-controller.md. Steam builds its touch controller (Steam Input's +// Mobile Touch type, the one Steam Link's on-screen controls are) for any HID device with the ids +// 0000:11fb. With FAKE_TOUCHCTL_RING set, the client is offered one as /dev/hidraw17 beside the +// Deck, its sysfs written by the app (SteamDeckPad.kt). The app draws the controls; this side only +// carries the device's traffic through the file FAKE_TOUCHCTL_RING names, which both map: +// +// - the app writes the 40-byte input report (under a seqlock), whether the device is plugged in, +// and the battery level; +// - this side writes what the client tells the device: the app and action set it is on (output +// report 4), the action set layers added and removed (5, 6), rumble (1, also passed to the app's +// vibration like any pad's) and the setting report (3), and how many readers have it open. +// +// Plugged in and out by the app (while its controls are up and no physical pad is in use), as +// Steam Link withdraws its touch controls: an unplugged device ends its open stream, refuses new +// opens, and a udev event (libblsession's stand-in monitor, udevmon.c) tells the client's HID +// discovery either way. Only the client is offered it; a game reads Steam Input's virtual pad. +static constexpr const char *TOUCH_HIDRAW_PATH = "/dev/hidraw17"; +static constexpr unsigned int TOUCH_HIDRAW_MINOR = 17; +static constexpr int TOUCH_REPORT_BYTES = 40; +static constexpr int TOUCH_REPORT_INTERVAL_US = 8000; +static constexpr const char *TOUCH_NAME = "Mobile Touch Control"; +static constexpr const char *TOUCH_SERIAL = "DROIDDECK0001"; +static constexpr const char *TOUCH_DEVPATH = "/devices/droiddeck/usb2/2-1:1.0/0003:0000:11FB.0002/hidraw/hidraw17"; +// Generic Desktop / Game Pad: for a vendor id that is not Valve's the client opens only a device +// with a gamepad usage. One 40-byte input report, one 64-byte output and one 64-byte feature +// report, no report ids. Must match SteamDeckPad.kt. +static const uint8_t kTouchReportDescriptor[] = { + 0x05, 0x01, 0x09, 0x05, 0xa1, 0x01, + 0x09, 0x02, 0x15, 0x00, 0x26, 0xff, 0x00, 0x75, 0x08, 0x95, 0x28, 0x81, 0x02, + 0x09, 0x03, 0x15, 0x00, 0x26, 0xff, 0x00, 0x75, 0x08, 0x95, 0x40, 0x91, 0x02, + 0x09, 0x04, 0x15, 0x00, 0x26, 0xff, 0x00, 0x75, 0x08, 0x95, 0x40, 0xb1, 0x02, 0xc0}; + +// The shared file. Must match SteamTouchDevice.kt. +struct TouchRingFile { + uint32_t magic; // TCH1 + uint32_t version; + uint64_t report_seq; // odd while the app writes report + uint8_t report[TOUCH_REPORT_BYTES]; + uint32_t plugged; + uint32_t battery; // percent + // Written here. + uint64_t state_seq; // odd while being written + uint32_t opened; + uint32_t appid; + uint32_t action_set; + uint32_t layer_count; + uint32_t layers[8]; + uint32_t rumble_seq; + uint16_t rumble_low; + uint16_t rumble_high; + uint32_t rumble_ms; + uint32_t setting30; + uint32_t action_seq; +}; +static constexpr uint32_t TOUCH_RING_MAGIC = 0x31484354; +static constexpr size_t TOUCH_RING_SIZE = 256; +static_assert(offsetof(TouchRingFile, report) == 16, "TouchRingFile layout"); +static_assert(offsetof(TouchRingFile, plugged) == 56, "TouchRingFile layout"); +static_assert(offsetof(TouchRingFile, state_seq) == 64, "TouchRingFile layout"); +static_assert(offsetof(TouchRingFile, layers) == 88, "TouchRingFile layout"); +static_assert(offsetof(TouchRingFile, rumble_seq) == 120, "TouchRingFile layout"); +static_assert(offsetof(TouchRingFile, action_seq) == 136, "TouchRingFile layout"); +static_assert(sizeof(TouchRingFile) <= TOUCH_RING_SIZE, "TouchRingFile layout"); + +__attribute__((visibility("hidden"))) static TouchRingFile *touch_ring() { + static TouchRingFile *ring = nullptr; + static std::once_flag once; + std::call_once(once, [] { + const char *path = getenv("FAKE_TOUCHCTL_RING"); + if (!path || !*path || !fake_deck_enabled()) return; + static auto my_open = reinterpret_cast(dlsym(RTLD_NEXT, "open")); + int fd = my_open(path, O_RDWR | O_CLOEXEC); + if (fd < 0) { + Logger::log("touch: %s not openable (%s)\n", path, strerror(errno)); + return; + } + void *mapping = mmap(nullptr, TOUCH_RING_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + syscall(SYS_close, fd); + if (mapping == MAP_FAILED || static_cast(mapping)->magic != TOUCH_RING_MAGIC) { + Logger::log("touch: %s %s\n", path, mapping == MAP_FAILED ? "could not be mapped" : "has no valid header"); + if (mapping != MAP_FAILED) munmap(mapping, TOUCH_RING_SIZE); + return; + } + ring = static_cast(mapping); + }); + return ring; +} + +__attribute__((visibility("hidden"))) static bool touch_plugged() { + TouchRingFile *ring = touch_ring(); + return ring && __atomic_load_n(&ring->plugged, __ATOMIC_ACQUIRE) != 0; +} + +__attribute__((visibility("hidden"))) static bool is_touch_hidraw_path(const char *pathname) { + return pathname && !strcmp(pathname, TOUCH_HIDRAW_PATH) && process_is_steam_client() && touch_ring(); +} + +// The state the client gave the device, written under the ring's seqlock. +template +__attribute__((visibility("hidden"))) static void touch_state(F &&write) { + TouchRingFile *ring = touch_ring(); + if (!ring) return; + static std::mutex mutex; + std::lock_guard guard(mutex); + __atomic_fetch_add(&ring->state_seq, 1, __ATOMIC_ACQ_REL); + write(*ring); + __atomic_fetch_add(&ring->state_seq, 1, __ATOMIC_ACQ_REL); +} + +static inline uint32_t get32(const uint8_t *at) { + return static_cast(at[0]) | static_cast(at[1]) << 8 | static_cast(at[2]) << 16 | + static_cast(at[3]) << 24; +} + +// An output report from the client (Steam Link's CVirtualController::HandleFeatureReports): the +// type, then its fields. +__attribute__((visibility("hidden"))) static void touch_output(const uint8_t *data, size_t size) { + if (size < 9) return; + static std::atomic seen[8]; + uint8_t type = data[0]; + unsigned n = type < 8 ? ++seen[type] : 0; + if (n && (n & (n - 1)) == 0) + Logger::log("touch: output report %u (x%u): app %u, %u\n", type, n, get32(data + 1), get32(data + 5)); + switch (type) { + case 1: { // rumble: low, high, duration + uint16_t low = data[1] | data[2] << 8, high = data[3] | data[4] << 8; + uint32_t ms = get32(data + 5); + touch_state([&](TouchRingFile &r) { + r.rumble_low = low; + r.rumble_high = high; + r.rumble_ms = ms; + r.rumble_seq++; + }); + send_vibration(low, high, static_cast(std::min(ms, 0xffff)), 0); + break; + } + case 3: // a setting: 0x30 and a value + if (data[1] == 0x30) touch_state([&](TouchRingFile &r) { r.setting30 = data[2] | data[3] << 8; }); + break; + case 4: // the app and its action set + touch_state([&](TouchRingFile &r) { + r.appid = get32(data + 1); + r.action_set = get32(data + 5); + r.layer_count = 0; + r.action_seq++; + }); + break; + case 5: // a layer added to the app's action set + case 6: // and removed + touch_state([&](TouchRingFile &r) { + uint32_t layer = get32(data + 5); + uint32_t count = std::min(r.layer_count, 8); + uint32_t kept = 0; + for (uint32_t i = 0; i < count; i++) + if (r.layers[i] != layer) r.layers[kept++] = r.layers[i]; + if (type == 5 && kept < 8) r.layers[kept++] = layer; + r.layer_count = kept; + r.action_seq++; + }); + break; + } +} + +__attribute__((visibility("hidden"))) static bool read_touch_report(TouchRingFile *ring, uint8_t *out) { + for (int attempt = 0; attempt < 8; attempt++) { + uint64_t seq = __atomic_load_n(&ring->report_seq, __ATOMIC_ACQUIRE); + if (seq & 1) continue; + memcpy(out, ring->report, TOUCH_REPORT_BYTES); + __atomic_thread_fence(__ATOMIC_ACQUIRE); + if (seq == __atomic_load_n(&ring->report_seq, __ATOMIC_RELAXED)) return true; + } + return false; +} + +__attribute__((visibility("hidden"))) static void *touch_report_thread(void *arg) { + auto *holder = static_cast *>(arg); + std::shared_ptr self = *holder; + delete holder; + TouchRingFile *ring = touch_ring(); + uint8_t report[TOUCH_REPORT_BYTES] = {}; + while (touch_plugged()) { + read_touch_report(ring, report); + if (send(self->peer, report, sizeof(report), MSG_DONTWAIT | MSG_NOSIGNAL) < 0 && + errno != EAGAIN && errno != EWOULDBLOCK) + break; + struct timespec interval = {0, TOUCH_REPORT_INTERVAL_US * 1000L}; + nanosleep(&interval, nullptr); + } + // Ending the stream is the unplug: the client's read fails and it lets the controller go. + syscall(SYS_close, self->peer); + touch_state([](TouchRingFile &r) { if (r.opened) r.opened--; }); + return nullptr; +} + +__attribute__((visibility("hidden"))) static int open_touch_hidraw(int flags) { + if (!touch_plugged()) { + errno = ENOENT; + return -1; + } + int pair[2]; + if (socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, pair) < 0) return -1; + int buffer = TOUCH_REPORT_BYTES * 4; + setsockopt(pair[1], SOL_SOCKET, SO_SNDBUF, &buffer, sizeof(buffer)); + if (!(flags & O_CLOEXEC)) fcntl(pair[0], F_SETFD, 0); + if (flags & O_NONBLOCK) fcntl(pair[0], F_SETFL, fcntl(pair[0], F_GETFL) | O_NONBLOCK); + auto touch = std::make_shared(); + touch->peer = pair[1]; + touch->touch = true; + { + std::lock_guard guard(controller_mutex()); + deck_map()[pair[0]] = touch; + } + touch_state([](TouchRingFile &r) { r.opened++; }); + pthread_t thread; + auto *arg = new std::shared_ptr(touch); + if (pthread_create(&thread, nullptr, touch_report_thread, arg) != 0) { + delete arg; + std::lock_guard guard(controller_mutex()); + deck_map().erase(pair[0]); + syscall(SYS_close, pair[0]); + syscall(SYS_close, pair[1]); + touch_state([](TouchRingFile &r) { if (r.opened) r.opened--; }); + errno = ENOMEM; + return -1; + } + pthread_detach(thread); + Logger::log("touch: %s opened as fd %d\n", TOUCH_HIDRAW_PATH, pair[0]); + return pair[0]; +} + +static int copy_ioctl_string(ioctl_request_t op, void *argp, const char *value); + +__attribute__((visibility("hidden"))) static int ioctl_touch(DeckHidraw &touch, ioctl_request_t op, void *argp) { + if (_IOC_TYPE(op) != 'H') { + errno = ENOTTY; + return -1; + } + size_t size = _IOC_SIZE(op); + switch (_IOC_NR(op)) { + case 0x01: // HIDIOCGRDESCSIZE + *static_cast(argp) = sizeof(kTouchReportDescriptor); + return 0; + case 0x02: { // HIDIOCGRDESC + auto *descriptor = static_cast(argp); + uint32_t want; + memcpy(&want, descriptor, sizeof(want)); + memcpy(descriptor + 4, kTouchReportDescriptor, std::min(want, sizeof(kTouchReportDescriptor))); + return 0; + } + case 0x03: { // HIDIOCGRAWINFO + struct { + uint32_t bustype; + int16_t vendor; + int16_t product; + } info = {BUS_USB, 0x0000, static_cast(0x11fb)}; + memcpy(argp, &info, sizeof(info)); + return 0; + } + case 0x04: return copy_ioctl_string(op, argp, TOUCH_NAME); // HIDIOCGRAWNAME + case 0x05: return copy_ioctl_string(op, argp, "usb-droiddeck-2/input0"); // HIDIOCGRAWPHYS + case 0x08: return copy_ioctl_string(op, argp, TOUCH_SERIAL); // HIDIOCGRAWUNIQ + case 0x06: // HIDIOCSFEATURE: Steam Link's controller ignores these + touch.pending_feature = size > 0 ? static_cast(argp)[0] : 0; + return static_cast(size); + case 0x07: { // HIDIOCGFEATURE: report 2 is the battery + auto *buf = static_cast(argp); + uint8_t id = size ? buf[0] : 0; + memset(buf, 0, size); + if (size) buf[0] = id; + if (size > 2) { + TouchRingFile *ring = touch_ring(); + buf[1] = 2; + buf[2] = static_cast(ring && ring->battery ? std::min(ring->battery, 100) : 100); + } + return static_cast(std::min(size, 18)); + } + default: + errno = EINVAL; + return -1; + } +} + +// Plugging in and out: the app flips `plugged`; this tells the client's HID discovery, which +// otherwise never looks again (the sandbox gives it no kernel uevents). +__attribute__((visibility("hidden"))) static void touch_uevent(const char *action) { + using inject_fn = int (*)(const char *, const char *, const char *, const char *, unsigned, unsigned); + static auto inject = reinterpret_cast(dlsym(RTLD_DEFAULT, "bl_udevmon_inject")); + int sent = inject ? inject(action, TOUCH_DEVPATH, "hidraw", TOUCH_HIDRAW_PATH, DECK_HIDRAW_MAJOR, TOUCH_HIDRAW_MINOR) : -1; + Logger::log("touch: %s (udev event to %d monitor%s)\n", action, sent, sent == 1 ? "" : "s"); +} + +__attribute__((visibility("hidden"))) static void *touch_plug_thread(void *) { + bool was = touch_plugged(); + Logger::log("touch: %s at start\n", was ? "plugged in" : "unplugged"); + for (;;) { + struct timespec interval = {0, 50 * 1000 * 1000}; + nanosleep(&interval, nullptr); + bool now = touch_plugged(); + if (now == was) continue; + was = now; + touch_uevent(now ? "add" : "remove"); + } + return nullptr; +} + +__attribute__((constructor)) static void start_touch_controller() { + config(); + if (!process_is_steam_client() || !touch_ring()) return; + pthread_t thread; + if (pthread_create(&thread, nullptr, touch_plug_thread, nullptr) == 0) pthread_detach(thread); +} + // libudev (systemd's sd-device) accepts a device only where its directory is on sysfs, which it // checks with fstatfs on the directory it resolved. The Deck's sysfs is the app's own files bound // into /sys, so those paths are said to be sysfs; nothing else is touched. @@ -1823,6 +2162,7 @@ EXPORT int open(const char *pathname, int flags, ...) { if (fake_uinput_enabled() && is_uinput_path(pathname)) return open_uinput(flags); if (is_deck_hidraw_path(pathname)) return open_deck_hidraw(flags); + if (is_touch_hidraw_path(pathname)) return open_touch_hidraw(flags); if (is_withdrawn_pad_path(pathname)) { errno = ENOENT; return -1; @@ -1897,6 +2237,7 @@ EXPORT int openat(int dirfd, const char *pathname, int flags, ...) { if (fake_uinput_enabled() && is_uinput_path(pathname)) return open_uinput(flags); if (is_deck_hidraw_path(pathname)) return open_deck_hidraw(flags); + if (is_touch_hidraw_path(pathname)) return open_touch_hidraw(flags); if (is_withdrawn_pad_path(pathname)) { errno = ENOENT; return -1; @@ -1957,6 +2298,12 @@ static int fake_stat_path(const char *pathname, S *statbuf, Real real) { errno = ENOENT; return -1; } + if (is_touch_hidraw_path(pathname)) { + memset(statbuf, 0, sizeof(*statbuf)); + statbuf->st_mode = S_IFCHR | 0666; + statbuf->st_rdev = makedev(DECK_HIDRAW_MAJOR, TOUCH_HIDRAW_MINOR); + return 0; + } if (is_deck_hidraw_path(pathname)) { memset(statbuf, 0, sizeof(*statbuf)); statbuf->st_mode = S_IFCHR | 0666; @@ -2008,7 +2355,7 @@ static int fake_stat_fd(int fd, S *buf, Real real) { std::lock_guard guard(controller_mutex()); if (ret == 0 && deck_map().count(fd)) { buf->st_mode = (buf->st_mode & ~S_IFMT) | S_IFCHR; - buf->st_rdev = makedev(DECK_HIDRAW_MAJOR, DECK_HIDRAW_MINOR); + buf->st_rdev = makedev(DECK_HIDRAW_MAJOR, deck_map()[fd]->touch ? TOUCH_HIDRAW_MINOR : DECK_HIDRAW_MINOR); return ret; } auto controller = controller_map().find(fd); @@ -2054,6 +2401,7 @@ EXPORT int access(const char *pathname, int mode) { static auto my_access = reinterpret_cast(dlsym(RTLD_NEXT, "access")); if (fake_uinput_enabled() && is_uinput_path(pathname)) return 0; if (is_deck_hidraw_path(pathname) && process_is_steam_client()) return 0; + if (is_touch_hidraw_path(pathname)) return 0; if (is_withdrawn_pad_path(pathname) || is_deck_hidraw_path(pathname)) { errno = ENOENT; return -1; @@ -2090,6 +2438,7 @@ EXPORT int faccessat(int dirfd, const char *pathname, int mode, int flags) { static auto my_faccessat = reinterpret_cast(dlsym(RTLD_NEXT, "faccessat")); if (fake_uinput_enabled() && is_uinput_path(pathname)) return 0; if (is_deck_hidraw_path(pathname) && process_is_steam_client()) return 0; + if (is_touch_hidraw_path(pathname)) return 0; if (is_withdrawn_pad_path(pathname) || is_deck_hidraw_path(pathname)) { errno = ENOENT; return -1; @@ -2211,7 +2560,8 @@ EXPORT int ioctl(int fd, ioctl_request_t op, ...) { auto maker = uinput_map().find(fd); if (maker != uinput_map().end()) return ioctl_uinput(*maker->second, op, argp); auto deck = deck_map().find(fd); - if (deck != deck_map().end()) return ioctl_deck(*deck->second, op, argp); + if (deck != deck_map().end()) + return deck->second->touch ? ioctl_touch(*deck->second, op, argp) : ioctl_deck(*deck->second, op, argp); auto controller = controller_map().find(fd); if (controller == controller_map().end()) { guard.unlock(); @@ -2537,7 +2887,10 @@ EXPORT ssize_t write(int fd, const void *buf, size_t count) { std::unique_lock guard(controller_mutex()); auto made = uinput_map().find(fd); if (made != uinput_map().end()) return write_uinput(*made->second, buf, count); - if (deck_map().count(fd)) return static_cast(count); + if (deck_map().count(fd)) { + if (deck_map()[fd]->touch) touch_output(static_cast(buf), count); + return static_cast(count); + } auto controller = controller_map().find(fd); if (controller != controller_map().end()) { if (fake_fd_is_stale(fd)) { @@ -2562,7 +2915,11 @@ EXPORT ssize_t writev(int fd, const struct iovec *iov, int iovcnt) { std::unique_lock guard(controller_mutex()); if (deck_map().count(fd)) { ssize_t total = 0; - for (int i = 0; i < iovcnt; i++) total += static_cast(iov[i].iov_len); + bool touch = deck_map()[fd]->touch; + for (int i = 0; i < iovcnt; i++) { + if (touch) touch_output(static_cast(iov[i].iov_base), iov[i].iov_len); + total += static_cast(iov[i].iov_len); + } return total; } auto made = uinput_map().find(fd); diff --git a/app/src/main/cpp/waylandcomp/CMakeLists.txt b/app/src/main/cpp/waylandcomp/CMakeLists.txt index ddf19d05f7..c2f9cb6543 100644 --- a/app/src/main/cpp/waylandcomp/CMakeLists.txt +++ b/app/src/main/cpp/waylandcomp/CMakeLists.txt @@ -17,6 +17,7 @@ add_library(droiddeckwayland SHARED src/wl_clipboard.c src/wl_text_input.c src/wl_toplevel_icon.c + src/wl_single_pixel.c src/wl_color_mgmt.c src/hdr_compose.c src/blend_pass.c @@ -31,6 +32,7 @@ add_library(droiddeckwayland SHARED generated/wlr-data-control-unstable-v1-protocol.c generated/text-input-unstable-v3-protocol.c generated/xdg-toplevel-icon-v1-protocol.c + generated/single-pixel-buffer-v1-protocol.c generated/banner-ahb-v1-protocol.c src/framegen_bridge.c src/framegen_jni.c diff --git a/app/src/main/cpp/waylandcomp/generated/single-pixel-buffer-v1-protocol.c b/app/src/main/cpp/waylandcomp/generated/single-pixel-buffer-v1-protocol.c new file mode 100644 index 0000000000..01434221c6 --- /dev/null +++ b/app/src/main/cpp/waylandcomp/generated/single-pixel-buffer-v1-protocol.c @@ -0,0 +1,61 @@ +/* Generated by wayland-scanner 1.26.0 */ + +/* + * Copyright © 2022 Simon Ser + * + * 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 (including the next + * paragraph) 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. + */ + +#include +#include +#include +#include "wayland-util.h" + +#ifndef __has_attribute +# define __has_attribute(x) 0 /* Compatibility with non-clang compilers. */ +#endif + +#if (__has_attribute(visibility) || defined(__GNUC__) && __GNUC__ >= 4) +#define WL_PRIVATE __attribute__ ((visibility("hidden"))) +#else +#define WL_PRIVATE +#endif + +extern const struct wl_interface wl_buffer_interface; + +static const struct wl_interface *single_pixel_buffer_v1_types[] = { + &wl_buffer_interface, + NULL, + NULL, + NULL, + NULL, +}; + +static const struct wl_message wp_single_pixel_buffer_manager_v1_requests[] = { + { "destroy", "", single_pixel_buffer_v1_types + 0 }, + { "create_u32_rgba_buffer", "nuuuu", single_pixel_buffer_v1_types + 0 }, +}; + +WL_PRIVATE const struct wl_interface wp_single_pixel_buffer_manager_v1_interface = { + "wp_single_pixel_buffer_manager_v1", 1, + 2, wp_single_pixel_buffer_manager_v1_requests, + 0, NULL, +}; + diff --git a/app/src/main/cpp/waylandcomp/generated/single-pixel-buffer-v1-server-protocol.h b/app/src/main/cpp/waylandcomp/generated/single-pixel-buffer-v1-server-protocol.h new file mode 100644 index 0000000000..a030d47757 --- /dev/null +++ b/app/src/main/cpp/waylandcomp/generated/single-pixel-buffer-v1-server-protocol.h @@ -0,0 +1,142 @@ +/* Generated by wayland-scanner 1.26.0 */ + +#ifndef SINGLE_PIXEL_BUFFER_V1_SERVER_PROTOCOL_H +#define SINGLE_PIXEL_BUFFER_V1_SERVER_PROTOCOL_H + +#include +#include +#include "wayland-server.h" + +#ifdef __cplusplus +extern "C" { +#endif + +struct wl_client; +struct wl_resource; + +/** + * @page page_single_pixel_buffer_v1 The single_pixel_buffer_v1 protocol + * single pixel buffer factory + * + * @section page_desc_single_pixel_buffer_v1 Description + * + * This protocol extension allows clients to create single-pixel buffers. + * + * Compositors supporting this protocol extension should also support the + * viewporter protocol extension. Clients may use viewporter to scale a + * single-pixel buffer to a desired size. + * + * Warning! The protocol described in this file is currently in the testing + * phase. Backward compatible changes may be added together with the + * corresponding interface version bump. Backward incompatible changes can + * only be done by creating a new major version of the extension. + * + * @section page_ifaces_single_pixel_buffer_v1 Interfaces + * - @subpage page_iface_wp_single_pixel_buffer_manager_v1 - global factory for single-pixel buffers + * @section page_copyright_single_pixel_buffer_v1 Copyright + *
+ *
+ * Copyright © 2022 Simon Ser
+ *
+ * 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 (including the next
+ * paragraph) 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.
+ * 
+ */ +struct wl_buffer; +struct wp_single_pixel_buffer_manager_v1; + +#ifndef WP_SINGLE_PIXEL_BUFFER_MANAGER_V1_INTERFACE +#define WP_SINGLE_PIXEL_BUFFER_MANAGER_V1_INTERFACE +/** + * @page page_iface_wp_single_pixel_buffer_manager_v1 wp_single_pixel_buffer_manager_v1 + * @section page_iface_wp_single_pixel_buffer_manager_v1_desc Description + * + * The wp_single_pixel_buffer_manager_v1 interface is a factory for + * single-pixel buffers. + * @section page_iface_wp_single_pixel_buffer_manager_v1_api API + * See @ref iface_wp_single_pixel_buffer_manager_v1. + */ +/** + * @defgroup iface_wp_single_pixel_buffer_manager_v1 The wp_single_pixel_buffer_manager_v1 interface + * + * The wp_single_pixel_buffer_manager_v1 interface is a factory for + * single-pixel buffers. + */ +extern const struct wl_interface wp_single_pixel_buffer_manager_v1_interface; +#endif + +/** + * @ingroup iface_wp_single_pixel_buffer_manager_v1 + * @struct wp_single_pixel_buffer_manager_v1_interface + */ +struct wp_single_pixel_buffer_manager_v1_interface { + /** + * destroy the manager + * + * Destroy the wp_single_pixel_buffer_manager_v1 object. + * + * The child objects created via this interface are unaffected. + */ + void (*destroy)(struct wl_client *client, + struct wl_resource *resource); + /** + * create a 1×1 buffer from 32-bit RGBA values + * + * Create a single-pixel buffer from four 32-bit RGBA values. + * + * Unless specified in another protocol extension, the RGBA values + * use pre-multiplied alpha. + * + * The width and height of the buffer are 1. + * + * The r, g, b and a arguments valid range is from UINT32_MIN (0) + * to UINT32_MAX (0xffffffff). + * + * These arguments should be interpreted as a percentage, i.e. - + * UINT32_MIN = 0% of the given color component - UINT32_MAX = 100% + * of the given color component + * @param r value of the buffer's red channel + * @param g value of the buffer's green channel + * @param b value of the buffer's blue channel + * @param a value of the buffer's alpha channel + */ + void (*create_u32_rgba_buffer)(struct wl_client *client, + struct wl_resource *resource, + uint32_t id, + uint32_t r, + uint32_t g, + uint32_t b, + uint32_t a); +}; + + +/** + * @ingroup iface_wp_single_pixel_buffer_manager_v1 + */ +#define WP_SINGLE_PIXEL_BUFFER_MANAGER_V1_DESTROY_SINCE_VERSION 1 +/** + * @ingroup iface_wp_single_pixel_buffer_manager_v1 + */ +#define WP_SINGLE_PIXEL_BUFFER_MANAGER_V1_CREATE_U32_RGBA_BUFFER_SINCE_VERSION 1 + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/app/src/main/cpp/waylandcomp/protocols/single-pixel-buffer-v1.xml b/app/src/main/cpp/waylandcomp/protocols/single-pixel-buffer-v1.xml new file mode 100644 index 0000000000..bcbd0382eb --- /dev/null +++ b/app/src/main/cpp/waylandcomp/protocols/single-pixel-buffer-v1.xml @@ -0,0 +1,76 @@ + + + + Copyright © 2022 Simon Ser + + 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 (including the next + paragraph) 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. + + + + This protocol extension allows clients to create single-pixel buffers. + + Compositors supporting this protocol extension should also support the + viewporter protocol extension. Clients may use viewporter to scale a + single-pixel buffer to a desired size. + + Warning! The protocol described in this file is currently in the testing + phase. Backward compatible changes may be added together with the + corresponding interface version bump. Backward incompatible changes can + only be done by creating a new major version of the extension. + + + + + The wp_single_pixel_buffer_manager_v1 interface is a factory for + single-pixel buffers. + + + + + Destroy the wp_single_pixel_buffer_manager_v1 object. + + The child objects created via this interface are unaffected. + + + + + + Create a single-pixel buffer from four 32-bit RGBA values. + + Unless specified in another protocol extension, the RGBA values use + pre-multiplied alpha. + + The width and height of the buffer are 1. + + The r, g, b and a arguments valid range is from UINT32_MIN (0) + to UINT32_MAX (0xffffffff). + + These arguments should be interpreted as a percentage, i.e. + - UINT32_MIN = 0% of the given color component + - UINT32_MAX = 100% of the given color component + + + + + + + + + diff --git a/app/src/main/cpp/waylandcomp/src/compositor.c b/app/src/main/cpp/waylandcomp/src/compositor.c index fbbd09886e..805eff9fa1 100644 --- a/app/src/main/cpp/waylandcomp/src/compositor.c +++ b/app/src/main/cpp/waylandcomp/src/compositor.c @@ -745,6 +745,21 @@ static void take_shm(struct surface *s, struct wl_shm_buffer *shm, struct wl_res if (ci && ci->asked_feedback) ci->shm_frames++; /* since it asked for GPU buffers */ } +/* A single-pixel buffer (wl_single_pixel.c) as a 1x1 image, which the surface's viewport stretches. */ +static void take_pixel(struct surface *s, const uint8_t bgra[4], struct wl_resource *buffer) { + if (s->shm_img && (vkp_image_width(s->shm_img) != 1 || vkp_image_height(s->shm_img) != 1)) { + vkp_image_destroy(s->shm_img); + s->shm_img = NULL; + } + if (!s->shm_img) s->shm_img = vkp_image_create_shm(1, 1); + if (s->shm_img) vkp_image_upload_shm(s->shm_img, bgra, 4); + wl_buffer_send_release(buffer); + s->buf_w = 1; + s->buf_h = 1; + s->buf_alpha = bgra[3] != 0xff; + s->has_content = s->shm_img != NULL; +} + /* The window the app's performance HUD follows: the latest one at least as big as the last to * start presenting GPU frames (take_dmabuf) (X11 binds the HUD to the _MESA_DRV window and counts X presents instead). JNI upcalls. */ static struct surface *g_hud_surface; @@ -1084,6 +1099,7 @@ static void surface_commit(struct wl_client *c, struct wl_resource *r) { struct wl_resource *buffer = s->pending_buffer; struct dmabuf_buffer *db = get_dmabuf(buffer); struct wl_shm_buffer *shm = buffer && !db ? wl_shm_buffer_get(buffer) : NULL; + uint8_t pixel[4]; /* The previous content is replaced before reaching the screen. */ feedback_discard_all(&s->feedback); @@ -1113,6 +1129,9 @@ static void surface_commit(struct wl_client *c, struct wl_resource *r) { } else if (shm) { drop_dmabuf(s, 1); take_shm(s, shm, buffer); + } else if (single_pixel_buffer_get(buffer, pixel)) { + drop_dmabuf(s, 1); + take_pixel(s, pixel, buffer); } else { drop_dmabuf(s, 1); s->has_content = 0; @@ -1661,15 +1680,24 @@ static void bind_desktop(struct wl_client *c, void *data, uint32_t ver, uint32_t static void output_release(struct wl_client *c, struct wl_resource *r) { wl_resource_destroy(r); } static const struct wl_output_interface output_impl = { .release = output_release }; -static void bind_output(struct wl_client *c, void *data, uint32_t ver, uint32_t id) { - struct wl_resource *r = wl_resource_create(c, &wl_output_interface, ver, id); - if (!r) { wl_client_post_no_memory(c); return; } - wl_resource_set_implementation(r, &output_impl, NULL, NULL); - wl_output_send_geometry(r, 0, 0, 340, 190, WL_OUTPUT_SUBPIXEL_UNKNOWN, - "DroidDeck", "Wayland", WL_OUTPUT_TRANSFORM_NORMAL); +/* Every bound wl_output, so a resized output (a foldable opening) can send its new mode. */ +static struct wl_list g_outputs; +static void output_resource_destroy(struct wl_resource *r) { wl_list_remove(wl_resource_get_link(r)); } + +static void output_send_mode(struct wl_resource *r) { wl_output_send_mode(r, WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED, g_output_w > 0 ? g_output_w : 1920, g_output_h > 0 ? g_output_h : 1080, g_output_refresh_mhz > 0 ? g_output_refresh_mhz : 60000); +} + +static void bind_output(struct wl_client *c, void *data, uint32_t ver, uint32_t id) { + struct wl_resource *r = wl_resource_create(c, &wl_output_interface, ver, id); + if (!r) { wl_client_post_no_memory(c); return; } + wl_resource_set_implementation(r, &output_impl, NULL, output_resource_destroy); + wl_list_insert(&g_outputs, wl_resource_get_link(r)); + wl_output_send_geometry(r, 0, 0, 340, 190, WL_OUTPUT_SUBPIXEL_UNKNOWN, + "DroidDeck", "Wayland", WL_OUTPUT_TRANSFORM_NORMAL); + output_send_mode(r); if (ver >= WL_OUTPUT_NAME_SINCE_VERSION) { wl_output_send_name(r, "DroidDeck-1"); wl_output_send_description(r, "DroidDeck display"); @@ -3200,6 +3228,23 @@ static void key_event(uint32_t evdev, int pressed) { wl_display_flush_clients(g_display); } +/* The output changed size under a running session (a foldable opened or closed, with the + * session following the screen). Every client that has had its first configure is told the new + * size: gamescope's Wayland backend and the desktop's KWin both resize their output to it, and + * the next buffers they commit fill the new panel instead of being letterboxed onto it. */ +static void output_resized(void) { + struct surface *s; + struct wl_resource *r; + wl_resource_for_each(r, &g_outputs) { + output_send_mode(r); + if (wl_resource_get_version(r) >= 2) wl_output_send_done(r); + } + wl_list_for_each(s, &g_surfaces, link) + if (s->xdg_toplevel && s->toplevel_committed) send_toplevel_configure(s); + droiddeck_log("display", "output resized to %dx%d", g_output_w, g_output_h); + wl_display_flush_clients(g_display); +} + /* wl event-loop callback: drain queued input events written by the Android UI thread. */ static int on_input_readable(int fd, uint32_t mask, void *data) { struct input_msg m; @@ -3212,6 +3257,7 @@ static int on_input_readable(int fd, uint32_t mask, void *data) { case 5: on_vsync(((int64_t)m.p1 << 32) | (uint32_t)m.p2); break; case 6: pointer_delta(m.p1 / 256.0, m.p2 / 256.0); break; /* relative motion, 1/256 px */ case 7: deliver_touch(&m); break; + case 8: output_resized(); break; default: deliver_pointer(&m); break; } } @@ -3242,6 +3288,14 @@ void droiddeck_wayland_send_key(int evdev, int state) { (void)n; } +/* Called from JNI after g_output_w/h changed: the compositor thread re-sends the size. */ +void droiddeck_wayland_output_resized(void) { + if (g_input_pipe[1] < 0) return; + struct input_msg m = { 8, 0, 0, 0, 0 }; + ssize_t n = write(g_input_pipe[1], &m, sizeof(m)); + (void)n; +} + /* Called from JNI on every screen refresh (Choreographer). */ void droiddeck_wayland_vsync(int64_t frame_time_ns) { if (g_input_pipe[1] < 0) return; @@ -3439,6 +3493,7 @@ static struct wl_listener g_client_created = { .notify = on_client_created }; int droiddeck_wayland_run(void) { wl_list_init(&g_surfaces); wl_list_init(&g_toplevels); + wl_list_init(&g_outputs); wl_list_init(&g_constraints); wl_list_init(&g_relative_pointers); open_session_log(); diff --git a/app/src/main/cpp/waylandcomp/src/droiddeck_ext.c b/app/src/main/cpp/waylandcomp/src/droiddeck_ext.c index 74ddc6adaf..913f713d38 100644 --- a/app/src/main/cpp/waylandcomp/src/droiddeck_ext.c +++ b/app/src/main/cpp/waylandcomp/src/droiddeck_ext.c @@ -122,4 +122,5 @@ void droiddeck_ext_init(struct wl_display *display) { clipboard_init(display); text_input_init(display); toplevel_icon_init(display); + single_pixel_init(display); } diff --git a/app/src/main/cpp/waylandcomp/src/droiddeck_ext.h b/app/src/main/cpp/waylandcomp/src/droiddeck_ext.h index e21014fbf5..0dc8ae41aa 100644 --- a/app/src/main/cpp/waylandcomp/src/droiddeck_ext.h +++ b/app/src/main/cpp/waylandcomp/src/droiddeck_ext.h @@ -48,3 +48,6 @@ void text_input_host_commit(const char *utf8, size_t len); void text_input_host_preedit(const char *utf8, size_t len, int cursor_begin, int cursor_end); void text_input_host_delete(int before, int after); void toplevel_icon_init(struct wl_display *display); +void single_pixel_init(struct wl_display *display); +/* A wp_single_pixel_buffer_manager_v1 buffer: 1 and its pixel (B, G, R, A, premultiplied), else 0. */ +int single_pixel_buffer_get(struct wl_resource *buffer, uint8_t bgra[4]); diff --git a/app/src/main/cpp/waylandcomp/src/vk_present.c b/app/src/main/cpp/waylandcomp/src/vk_present.c index 3af9a8c254..4551c4806f 100644 --- a/app/src/main/cpp/waylandcomp/src/vk_present.c +++ b/app/src/main/cpp/waylandcomp/src/vk_present.c @@ -160,6 +160,11 @@ static const char *vk_result_name(VkResult r); static pthread_mutex_t g_req_lock = PTHREAD_MUTEX_INITIALIZER; static ANativeWindow *g_req_window; static int g_req_pending; +/* A detach was asked for since the compositor thread last looked. A rebind (null, then the same + * window) to rebuild the swapchain at a new size can land in full before the thread gets to it, + * and would otherwise read as the same window handed over again - nothing rebuilt, and the old + * size's buffers scaled onto the rotated or refolded panel. */ +static int g_req_detached; /* Scale mode + alignment (app values, see vk_present.h); written from any thread, read per frame. */ static volatile int g_mode = VKP_MODE_STRETCH, g_align = VKP_ALIGN_CENTER; @@ -252,6 +257,7 @@ void vk_present_set_window(ANativeWindow *window) { if (g_req_pending && g_req_window && g_req_window != window) superseded = g_req_window; g_req_window = window; g_req_pending = 1; + if (!window) g_req_detached = 1; pthread_mutex_unlock(&g_req_lock); if (superseded) ANativeWindow_release(superseded); } @@ -261,13 +267,20 @@ int vkp_apply_window_request(void) { pthread_mutex_lock(&g_req_lock); if (!g_req_pending) { pthread_mutex_unlock(&g_req_lock); return 0; } w = g_req_window; + const int detached = g_req_detached; g_req_window = NULL; g_req_pending = 0; + g_req_detached = 0; pthread_mutex_unlock(&g_req_lock); if (w == g_window) { /* The same window handed over again (ANativeWindow_fromSurface adds a reference each time). */ if (w) ANativeWindow_release(w); - return 0; + if (!w || !detached) return 0; + /* ... after a detach: a rebind for a resized surface. Rebuilt at its size on the next frame. */ + sc_layer_window_gone(); + destroy_swapchain(); + droiddeck_log("gpu", "screen surface rebound: rebuilding the swapchain at its new size"); + return 1; } sc_layer_window_gone(); /* the layer (if any) belongs to the old window */ destroy_swapchain(); /* recreated against the new window on the next frame */ @@ -1516,6 +1529,7 @@ static int render_impl(int scene_w, int scene_h, const struct vkp_draw *draws, i const struct vkp_hdr_frame *hf, int *how) { if (how) *how = 0; vkp_apply_window_request(); + check_surface_changed(); /* a rotation or a fold the rebind did not cover: rebuild at the new size */ if (g_dev_state == -2) return -1; if (dev_init() != 0 || !g_window || scene_w <= 0 || scene_h <= 0) return -1; @@ -2099,6 +2113,7 @@ static int pass_begin_impl(int scene_w, int scene_h, const struct vkp_draw *draw const struct vkp_hdr_frame *hf, int *rw, int *rh) { if (g_pass.active) vkp_pass_abort(); if (g_dev_state == -2 || dev_init() != 0 || !g_window || scene_w <= 0 || scene_h <= 0) return -1; + check_surface_changed(); /* as in render_impl: the effects pass maps onto the output's size */ if (!g_swapchain && swap_init() != 0) return -1; /* the mapping is in output pixels */ update_map(scene_w, scene_h); if (!g_map.valid) return -1; diff --git a/app/src/main/cpp/waylandcomp/src/waylandcomp_jni.c b/app/src/main/cpp/waylandcomp/src/waylandcomp_jni.c index 4d11a54192..8ec4b96b64 100644 --- a/app/src/main/cpp/waylandcomp/src/waylandcomp_jni.c +++ b/app/src/main/cpp/waylandcomp/src/waylandcomp_jni.c @@ -30,6 +30,7 @@ extern int droiddeck_cursor_serial(void); extern void droiddeck_wayland_send_key(int evdev, int state); extern void droiddeck_wayland_send_scene_input(int type, int a, int b); extern void droiddeck_wayland_vsync(int64_t frame_time_ns); +extern void droiddeck_wayland_output_resized(void); extern volatile int g_fps_limit; extern volatile int g_hide_shell; extern volatile int g_zero_copy; @@ -401,6 +402,17 @@ Java_com_droiddeck_launcher_wayland_WaylandCompositor_nativeSetOutputSize(JNIEnv g_output_h = h > 0 ? h : 0; } +/* The output's size changed while the compositor runs (a foldable opening or closing with the + * session following the screen): store it and have the compositor thread tell the clients. */ +JNIEXPORT void JNICALL +Java_com_droiddeck_launcher_wayland_WaylandCompositor_nativeResizeOutput(JNIEnv *env, jclass clazz, jint w, jint h) { + (void)env; (void)clazz; + if (w <= 0 || h <= 0) return; + g_output_w = w; + g_output_h = h; + droiddeck_wayland_output_resized(); +} + /* Fullscreen mode + screen alignment (Container.FULLSCREEN_* / ALIGN_* values): how the scene is * fitted onto the output. Any thread, any time; the next frame uses it. */ JNIEXPORT void JNICALL diff --git a/app/src/main/cpp/waylandcomp/src/wl_single_pixel.c b/app/src/main/cpp/waylandcomp/src/wl_single_pixel.c new file mode 100644 index 0000000000..9dfe78233c --- /dev/null +++ b/app/src/main/cpp/waylandcomp/src/wl_single_pixel.c @@ -0,0 +1,51 @@ +/* + * wp_single_pixel_buffer_manager_v1 (version 1): a wl_buffer of one pixel of one colour, which a + * client stretches over a surface with wp_viewporter. KWin's Wayland backend - the desktop's KWin + * nested in this compositor - refuses to start without it. The surface takes the pixel as a 1x1 + * image, the way it takes a wl_shm buffer (compositor.c, take_pixel). + */ +#include +#include "droiddeck_ext.h" +#include "single-pixel-buffer-v1-server-protocol.h" + +struct single_pixel { + uint8_t bgra[4]; /* premultiplied, as wl_shm's ARGB8888 lies in memory */ +}; + +static void buffer_destroy(struct wl_client *c, struct wl_resource *r) { wl_resource_destroy(r); } +static const struct wl_buffer_interface buffer_impl = { .destroy = buffer_destroy }; + +static void buffer_free(struct wl_resource *r) { free(wl_resource_get_user_data(r)); } + +int single_pixel_buffer_get(struct wl_resource *buffer, uint8_t bgra[4]) { + if (!buffer || !wl_resource_instance_of(buffer, &wl_buffer_interface, &buffer_impl)) return 0; + struct single_pixel *p = wl_resource_get_user_data(buffer); + for (int i = 0; i < 4; i++) bgra[i] = p->bgra[i]; + return 1; +} + +static void mgr_destroy(struct wl_client *c, struct wl_resource *r) { wl_resource_destroy(r); } +static void mgr_create_buffer(struct wl_client *c, struct wl_resource *r, uint32_t id, + uint32_t red, uint32_t green, uint32_t blue, uint32_t alpha) { + struct single_pixel *p = calloc(1, sizeof(*p)); + struct wl_resource *buffer = p ? wl_resource_create(c, &wl_buffer_interface, 1, id) : NULL; + if (!buffer) { free(p); wl_client_post_no_memory(c); return; } + /* The protocol's values span the whole u32 range; the top byte is the 8-bit channel. */ + p->bgra[0] = blue >> 24; + p->bgra[1] = green >> 24; + p->bgra[2] = red >> 24; + p->bgra[3] = alpha >> 24; + wl_resource_set_implementation(buffer, &buffer_impl, p, buffer_free); +} +static const struct wp_single_pixel_buffer_manager_v1_interface mgr_impl = { + .destroy = mgr_destroy, .create_u32_rgba_buffer = mgr_create_buffer, +}; +static void bind_manager(struct wl_client *c, void *data, uint32_t ver, uint32_t id) { + struct wl_resource *r = wl_resource_create(c, &wp_single_pixel_buffer_manager_v1_interface, ver, id); + if (!r) { wl_client_post_no_memory(c); return; } + wl_resource_set_implementation(r, &mgr_impl, NULL, NULL); +} + +void single_pixel_init(struct wl_display *display) { + wl_global_create(display, &wp_single_pixel_buffer_manager_v1_interface, 1, NULL, bind_manager); +} diff --git a/app/src/main/java/com/droiddeck/launcher/App.kt b/app/src/main/java/com/droiddeck/launcher/App.kt index 4271956465..d1287beddb 100644 --- a/app/src/main/java/com/droiddeck/launcher/App.kt +++ b/app/src/main/java/com/droiddeck/launcher/App.kt @@ -29,5 +29,10 @@ class App : Application() { GpuClockPin.clearLeftover(this) BwrapSpawner.start(this) com.droiddeck.launcher.frontend.GameFileSync.start(this) + // The game stores the Stores section can talk to; the native engine loads only when one needs it. + com.droiddeck.launcher.stores.StoresState.init(this) + com.droiddeck.launcher.stores.StoresState.register(com.droiddeck.launcher.stores.gog.GogBackend) + com.droiddeck.launcher.stores.StoresState.register(com.droiddeck.launcher.stores.epic.EpicBackend) + com.droiddeck.launcher.stores.StoresState.register(com.droiddeck.launcher.stores.amazon.AmazonBackend) } } diff --git a/app/src/main/java/com/droiddeck/launcher/MainActivity.kt b/app/src/main/java/com/droiddeck/launcher/MainActivity.kt index 90a3a5d5f3..9d613e521e 100644 --- a/app/src/main/java/com/droiddeck/launcher/MainActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/MainActivity.kt @@ -35,6 +35,7 @@ import com.droiddeck.launcher.runtime.LinuxRuntimeInstaller import com.droiddeck.launcher.runtime.DeckyManager import com.droiddeck.launcher.session.SessionService import com.droiddeck.launcher.ui.PackageRow +import com.droiddeck.launcher.ui.exposeTestTags import com.droiddeck.launcher.session.OfflineMode import com.droiddeck.launcher.session.ProtonExtras import com.droiddeck.launcher.session.ComponentsManager @@ -138,6 +139,7 @@ class MainActivity : ComponentActivity() { private var pkgStage by mutableStateOf(null) private var pkgPercent by mutableIntStateOf(-1) private var desktopInstalled by mutableStateOf(false) + private var desktopPresent by mutableStateOf(false) private var offlineAccount by mutableStateOf(null) private var offline by mutableStateOf(false) private var showPerformance by mutableStateOf(false) @@ -159,11 +161,16 @@ class MainActivity : ComponentActivity() { private var phantomProcessStatus by mutableStateOf(PhantomProcessStatus.NOT_APPLICABLE) private var showPhantomGate by mutableStateOf(false) private var directAudio by mutableStateOf(false) - private var clientDirectAudio by mutableStateOf(true) private var stretch16x9 by mutableStateOf(false) private var launcherFullscreen by mutableStateOf(true) private var animationsEnabled by mutableStateOf(true) private var storeEnabled by mutableStateOf(false) + private var gameStoresEnabled by mutableStateOf(false) + private var gameStoresSpeedTier by mutableStateOf("fast") + private var storesOpenTab by mutableStateOf(SessionPrefs.STORES_OPEN_LIBRARY) + /** A page a notification asked for ("stores:downloads", "app:"), until the front end has gone there. */ + private var navRequest by mutableStateOf(null) + private var storesShowMature by mutableStateOf(false) private var mic by mutableStateOf(false) private var wifiDiscovery by mutableStateOf(false) private var wifiDiscoveryPermission by mutableStateOf(false) @@ -295,29 +302,34 @@ class MainActivity : ComponentActivity() { GameSaves.savesDir().mkdirs() pickSaveDir.launch(InAppFilePicker.buildDirIntent(this, getString(R.string.main_saves_pick_folder, name, layout.label(this)), GameSaves.savesDir().path)) } - private val pickAddedGamesDir = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { r -> - if (r.resultCode == RESULT_OK) InAppFilePicker.pickedPath(r.data)?.let { path -> - SessionPrefs.setAddedGamesDirs(this, addedGamesDirs + path) - addedGamesDirs = SessionPrefs.addedGamesDirs(this) - addedGamesArt = SessionPrefs.addedGamesArt(this) - refreshAddedGames() - refresh() - } + /** The Games tab's +: the picked .exe becomes a game (or selects / switches the one it belongs to). */ + private fun addGameExe(path: String) { + val exe = java.io.File(path) + Thread({ + val result = try { com.droiddeck.launcher.frontend.AddedGames.addExe(this, exe) } catch (e: Exception) { + Log.w(TAG, "add game: ${e.message}"); com.droiddeck.launcher.frontend.AddedGames.AddResult.NotFound + } + val game = when (result) { + is com.droiddeck.launcher.frontend.AddedGames.AddResult.Added -> result.game + is com.droiddeck.launcher.frontend.AddedGames.AddResult.Existing -> result.game + is com.droiddeck.launcher.frontend.AddedGames.AddResult.Switched -> result.game + else -> null + } + ui.post { + if (game == null) { + if (result is com.droiddeck.launcher.frontend.AddedGames.AddResult.Unreachable) + android.widget.Toast.makeText(this, R.string.games_add_unreachable, android.widget.Toast.LENGTH_LONG).show() + return@post + } + updateAddedGames(listOf(game.folder.path), then = { fetchAddedGameArt() }) + navRequest = "app:${game.steamAppId ?: game.appId.toInt()}" + } + }, "add-game-exe").start() } - private var pendingAddedGame: String? = null - private val pickAddedGameExe = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { r -> - val folder = pendingAddedGame ?: return@registerForActivityResult - pendingAddedGame = null - if (r.resultCode == RESULT_OK) InAppFilePicker.pickedPath(r.data)?.let { path -> - SessionPrefs.setAddedGameExe(this, folder, path) - refreshAddedGames() - refresh() - } - } - private var addedGamesDirs by mutableStateOf>(emptyList()) private var addedGamesArt by mutableStateOf(true) @Volatile private var artFetchRunning = false - private var addedGames by mutableStateOf>(emptyList()) + /** The user entered a SteamGridDB API key of their own. */ + private var sgdbUserKey by mutableStateOf(false) private val pickRomsDir = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { r -> if (r.resultCode == RESULT_OK) InAppFilePicker.pickedPath(r.data)?.let { path -> SessionPrefs.setRomsDir(this, path) @@ -343,7 +355,6 @@ class MainActivity : ComponentActivity() { private var steamDownloadsInBackground by mutableStateOf(false) private var oscMode by mutableStateOf(SessionPrefs.OSC_AUTO) private var backActionsInverted by mutableStateOf(false) - private var renderer by mutableStateOf("vulkan") private var gameStorage by mutableStateOf("") private var storageDiagnostics by mutableStateOf(false) private var storageOptions by mutableStateOf>(emptyList()) @@ -355,6 +366,7 @@ class MainActivity : ComponentActivity() { private var emulatorList by mutableStateOf>(emptyList()) private var runningLabel by mutableStateOf(null) private var logsEnabled by mutableStateOf(true) + private var agentCommands by mutableStateOf(false) private var showRoms by mutableStateOf(false) private var homeAppSelected by mutableStateOf(false) private var homeScreenEnabled by mutableStateOf(false) @@ -436,6 +448,7 @@ class MainActivity : ComponentActivity() { } private fun readGameIntent(request: Intent) { + request.getStringExtra(com.droiddeck.launcher.stores.download.StoreDownloadService.EXTRA_NAV)?.let { navRequest = it; request.removeExtra(com.droiddeck.launcher.stores.download.StoreDownloadService.EXTRA_NAV) } shortcutPicker = request.action == Intent.ACTION_CREATE_SHORTCUT if (shortcutPicker) { // The shortcut picker must only offer games from the scan started for this request. @@ -522,11 +535,15 @@ class MainActivity : ComponentActivity() { launcherFullscreen = SessionPrefs.launcherFullscreen(this) animationsEnabled = SessionPrefs.animationsEnabled(this) storeEnabled = SessionPrefs.storeEnabled(this) + gameStoresEnabled = SessionPrefs.gameStoresEnabled(this) + gameStoresSpeedTier = SessionPrefs.gameStoresSpeedTier(this) + storesOpenTab = SessionPrefs.storesOpenTab(this) + storesShowMature = SessionPrefs.storesShowMature(this) applyLauncherFullscreen() updates.start() setContent { DroidDeckTheme(theme, appScale = appScale) { - com.droiddeck.launcher.ui.FocusGlideHost(androidx.compose.ui.Modifier.fillMaxSize()) { + com.droiddeck.launcher.ui.FocusGlideHost(androidx.compose.ui.Modifier.fillMaxSize().exposeTestTags()) { val sm = settingsMode val page: (@Composable () -> Unit)? = when { sm != null -> { { ModeSettingsHost(sm) } } @@ -544,9 +561,9 @@ class MainActivity : ComponentActivity() { shortcutLibraryScanning = shortcutLibraryScanning, gameSyncFolder = gameSyncFolder, busy = busy, stage = stage, percent = percent, - desktopInstalled = desktopInstalled, + desktopInstalled = desktopInstalled, desktopPresent = desktopPresent, offlineAccount = offlineAccount, offline = offline, - frameGenLabel = frameGenLabel, romsDir = romsDir, logsEnabled = logsEnabled, + frameGenLabel = frameGenLabel, romsDir = romsDir, logsEnabled = logsEnabled, agentCommands = agentCommands, steamGames = steamGames, emulators = emulatorList, running = runningLabel, frameGen = FrameGen.mode(this), lossless = lossless, @@ -575,20 +592,42 @@ class MainActivity : ComponentActivity() { launcherFullscreen = launcherFullscreen, animationsEnabled = animationsEnabled, storeEnabled = storeEnabled, + gameStoresEnabled = gameStoresEnabled, + gameStoresSpeedTier = gameStoresSpeedTier, + storesOpenTab = storesOpenTab, + navigate = navRequest, + addedGamesArt = addedGamesArt, + sgdbUserKey = sgdbUserKey, + storesShowMature = storesShowMature, + storeDownloadsActive = com.droiddeck.launcher.stores.StoresState.activeDownloads, ), FrontEndActions( onPlay = { startSteamSession() }, - // Steam's desktop client as a window on the desktop: under gamescope the - // client puts itself into Big Picture whatever it is started with. + // The client's desktop UI, with gamescope's Deck integration disabled. onPlayDesktopUi = { startSession(Intent(this, SessionActivity::class.java) - .putExtra(SessionService.EXTRA_MODE, SessionService.MODE_DESKTOP) + .putExtra(SessionService.EXTRA_MODE, SessionService.MODE_STEAM) .putExtra(SessionService.EXTRA_STEAM_UI, "desktop"), steamSession = true) }, - onSteamGame = { g -> if (shortcutPicker) chooseGameShortcut(g) else launchGame(g) }, + // A store game may need something done first (an Epic game its sign-in code). + onSteamGame = { g -> if (shortcutPicker) chooseGameShortcut(g) else com.droiddeck.launcher.stores.StoreLaunch.prepare(this, g) { launchGame(g) } }, onGameShortcut = { g -> com.droiddeck.launcher.frontend.GameShortcuts.pin(this, g) }, onExportGameFile = { g -> pickGameExport(g) }, onSyncGameFiles = { pickGameExport(null) }, + onAddGameExe = { path -> addGameExe(path) }, + onAddedGamesChanged = { folders -> + // New games want art; an edit does not. + val known = steamGames.mapNotNull { it.gameFiles?.path }.toSet() + updateAddedGames(folders, then = { if (folders.any { it !in known }) fetchAddedGameArt() }) + }, + onAddedGameRemoved = { folder, appId, name -> removeAddedGame(folder, appId, name) }, + onAddedGamesArt = { on -> SessionPrefs.setAddedGamesArt(this, on); addedGamesArt = on; if (on) refreshAddedGames() }, + onSgdbKey = { key -> + Thread({ + com.droiddeck.launcher.frontend.SteamGridDb.setUserKey(this, key) + ui.post { sgdbUserKey = key.isNotBlank(); refreshAddedGames() } + }, "sgdb-key").start() + }, onStopGameFileSync = { Thread({ GameFileSync.disable(this) @@ -631,6 +670,7 @@ class MainActivity : ComponentActivity() { // The activity re-attaches to the session that is running; nothing restarts. onResume = { startActivity(Intent(this, SessionActivity::class.java)) }, onSteamSettings = { openModeSettings(SessionService.MODE_STEAM) }, + onRemoveDesktop = { removeDesktop() }, onDesktopSettings = { openModeSettings(SessionService.MODE_DESKTOP) }, onInstallPackage = { id -> installPackage(id) }, onRemovePackage = { id -> removePackage(id) }, @@ -658,6 +698,10 @@ class MainActivity : ComponentActivity() { SessionPrefs.setLogsEnabled(this, !SessionPrefs.logsEnabled(this)) logsEnabled = SessionPrefs.logsEnabled(this) }, + onAgentCommands = { + com.droiddeck.launcher.agent.AgentAccess.setCommandsEnabled(this, !com.droiddeck.launcher.agent.AgentAccess.commandsEnabled(this)) + agentCommands = com.droiddeck.launcher.agent.AgentAccess.commandsEnabled(this) + }, onShareLogs = { SessionLogShare.prepare(this, { SessionLogShare.latest(this) }) { zip -> if (zip == null) android.widget.Toast.makeText(this, R.string.main_no_logs_yet, android.widget.Toast.LENGTH_LONG).show() @@ -697,6 +741,12 @@ class MainActivity : ComponentActivity() { applyLauncherFullscreen() }, onStoreEnabled = { on -> SessionPrefs.setStoreEnabled(this, on); storeEnabled = on }, + onGameStoresEnabled = { on -> SessionPrefs.setGameStoresEnabled(this, on); gameStoresEnabled = on; if (on) com.droiddeck.launcher.stores.StoresState.refresh(this) }, + onGameStoresSpeedTier = { tier -> SessionPrefs.setGameStoresSpeedTier(this, tier); gameStoresSpeedTier = tier }, + onStoresOpenTab = { tab -> SessionPrefs.setStoresOpenTab(this, tab); storesOpenTab = tab }, + onNavigated = { navRequest = null }, + onStoresShowMature = { show -> SessionPrefs.setStoresShowMature(this, show); storesShowMature = show }, + onLibraryChanged = { refreshAddedGames(); refresh() }, onAnimationsEnabled = { on -> SessionPrefs.setAnimationsEnabled(this, on) animationsEnabled = on @@ -838,8 +888,12 @@ class MainActivity : ComponentActivity() { SessionPrefs.setMicAsked(this) } if (wanted.isNotEmpty()) requestPermissions(wanted.toTypedArray(), 1) + // A store sign-in an earlier build left in plain text is sealed with the Keystore key. + Thread({ com.droiddeck.launcher.stores.StoreAccounts.encryptAll(this) }, "store-credentials").start() // A session folder left without its ending - the process was killed - gets it now. if (!SessionState.running) Thread({ + com.droiddeck.launcher.session.LogMigration.run(this) + SessionLogShare.clear(this) SessionArtifacts.finishAbandoned(this) SessionArtifacts.scrubOlder(this) SessionArtifacts.prune(this) @@ -884,6 +938,7 @@ class MainActivity : ComponentActivity() { /** On the Components page the pad's LB / RB step through GPU drivers, FEX, DXVK and VKD3D-Proton, wrapping around. */ override fun dispatchKeyEvent(event: KeyEvent): Boolean { + if (event.keyCode == KeyEvent.KEYCODE_BUTTON_A) com.droiddeck.launcher.ui.HeldKeys.confirm = event.action == KeyEvent.ACTION_DOWN if (showComponents && (event.keyCode == KeyEvent.KEYCODE_BUTTON_L1 || event.keyCode == KeyEvent.KEYCODE_BUTTON_R1)) { if (event.action == KeyEvent.ACTION_DOWN && event.repeatCount == 0) { val all = listOf(com.droiddeck.launcher.ui.GPU_TAB) + ComponentsManager.COMPONENTS @@ -892,12 +947,24 @@ class MainActivity : ComponentActivity() { } return true } - return super.dispatchKeyEvent(event) + // Compose throws when its focus tree has lost the focused node; one key is dropped + // instead of the app. + return try { + super.dispatchKeyEvent(event) + } catch (e: IllegalStateException) { + if (e.message?.contains("active focus target") != true) throw e + android.util.Log.w("MainActivity", "key ${event.keyCode} dropped: ${e.message}") + true + } } override fun onResume() { super.onResume() refreshWifiDiscovery() + // Back from a store's sign-in page: the Stores chips show the account at once. A store + // install finishing while this screen is up rebuilds the Games list through the listener. + if (gameStoresEnabled) com.droiddeck.launcher.stores.StoresState.refresh(this) + com.droiddeck.launcher.stores.StoresState.libraryListener = { refreshAddedGames(); refresh() } com.droiddeck.launcher.ui.Motion.refresh(this) // Back from a session stopped behind a flood: open on its blue, before the first frame. com.droiddeck.launcher.ui.QuitFlood.take()?.let { c -> @@ -955,6 +1022,8 @@ class MainActivity : ComponentActivity() { unregisterReceiver(wifiLocationReceiver) unregisterReceiver(mediaReceiver) ui.removeCallbacks(mediaRefresh) + // An install finishing while this screen is away is picked up by onResume's refresh. + com.droiddeck.launcher.stores.StoresState.libraryListener = null displayManager.unregisterDisplayListener(secondScreenDisplayListener) // The session covers the page by now; coming back finds it as it was. flood = null @@ -1131,6 +1200,7 @@ class MainActivity : ComponentActivity() { private fun refreshPackages() { packageRows = catalog?.map { PackageRow(it.id, it.kind, it.notes) } desktopInstalled = DesktopCatalog.desktopInstalled(this) + desktopPresent = DesktopCatalog.desktopPresent(this) } private fun launchProgram(path: String) { @@ -1187,10 +1257,8 @@ class MainActivity : ComponentActivity() { oscMode = if (mode == SessionService.MODE_STEAM) oscMode else null, backActionsInverted = backActionsInverted, directAudio = if (mode == SessionService.MODE_STEAM) directAudio else null, - clientDirectAudio = clientDirectAudio, stretch16x9 = if (mode == SessionService.MODE_STEAM) stretch16x9 else null, mic = if (mode == SessionService.MODE_STEAM) mic else null, - renderer = if (mode == SessionService.MODE_DESKTOP) renderer else null, gameStorage = if (mode == SessionService.MODE_STEAM) gameStorage else null, storageOptions = storageOptions, storageDiagnostics = mode == SessionService.MODE_STEAM && storageDiagnostics, @@ -1208,9 +1276,6 @@ class MainActivity : ComponentActivity() { wifiDiscoveryLocation = wifiDiscoveryLocation, wifiDiscoveryAsked = wifiDiscoveryAsked, wifiDiscoveryBlocked = wifiDiscoveryBlocked, - addedGamesDirs = if (mode == SessionService.MODE_STEAM) addedGamesDirs else null, - addedGames = if (mode == SessionService.MODE_STEAM) addedGames else emptyList(), - addedGamesArt = addedGamesArt, deckyInstalled = if (mode == SessionService.MODE_STEAM) decky.deckyInstalled else null, deckyLatestRelease = if (mode == SessionService.MODE_STEAM) decky.deckyReleases.firstOrNull() else null, deckyChecking = decky.deckyChecking, deckyStage = decky.deckyStage, deckyPercent = decky.deckyPercent, @@ -1252,7 +1317,6 @@ class MainActivity : ComponentActivity() { backActionsInverted = inverted }, onDirectAudio = { on -> SessionPrefs.setDirectAudio(this, on); directAudio = on }, - onClientDirectAudio = { on -> SessionPrefs.setClientDirectAudio(this, on); clientDirectAudio = on }, onStretch16x9 = { on -> SessionPrefs.setStretch16x9(this, on); stretch16x9 = on }, onMic = { on -> SessionPrefs.setMicEnabled(this, on) @@ -1263,7 +1327,6 @@ class MainActivity : ComponentActivity() { requestPermissions(arrayOf(Manifest.permission.RECORD_AUDIO), 2) } }, - onRenderer = { r -> SessionPrefs.setDesktopRenderer(this, r); renderer = r }, onGameStorage = { path, label -> setGameStorage(path, label) }, onPickGameStorageFolder = { pickGameStorage.launch(InAppFilePicker.buildDirIntent(this, getString(R.string.main_pick_game_storage), gameStorage.ifEmpty { null })) @@ -1293,14 +1356,6 @@ class MainActivity : ComponentActivity() { SessionPrefs.setRunSteamAtStartup(this, on) runSteamAtStartup = on }, - onPickAddedGamesDir = { pickAddedGamesDir.launch(InAppFilePicker.buildDirIntent(this, getString(R.string.main_pick_added_games), addedGamesDirs.lastOrNull())) }, - onAddedGamesArt = { on -> SessionPrefs.setAddedGamesArt(this, on); addedGamesArt = on; if (on) refreshAddedGames() }, - onForgetAddedGamesDir = { dir -> SessionPrefs.setAddedGamesDirs(this, addedGamesDirs - dir); addedGamesDirs = SessionPrefs.addedGamesDirs(this); refreshAddedGames(); refresh() }, - onAddedGameExe = { folder, path -> SessionPrefs.setAddedGameExe(this, folder, path); refreshAddedGames(); refresh() }, - onPickAddedGameExe = { folder -> - pendingAddedGame = folder - pickAddedGameExe.launch(InAppFilePicker.buildIntent(this, listOf("exe"), getString(R.string.main_pick_game_exe), folder)) - }, onDeckyInstall = { release -> decky.installDecky(release) }, onDeckyCheck = { decky.refreshDecky() }, onDeckyEnabled = { enabled -> DeckyManager.setSupervisorEnabled(this, enabled); decky.deckySupervisor = enabled }, @@ -1355,18 +1410,42 @@ class MainActivity : ComponentActivity() { ) } - /** The added games as the settings page lists them; a scan of the folder, on this thread (one level, small). */ - /** The added games for the session settings: a folder walk, so off the main thread. */ - private fun refreshAddedGames() { + /** + * The Games tab after these added games were added or edited: each read alone and put in + * place of its old entry (or added), no library-wide scan. [then] runs after, on the main thread. + */ + private fun updateAddedGames(folders: List, then: () -> Unit = {}) { + if (folders.isEmpty()) return Thread({ - val games = scanAddedGames() - ui.post { addedGames = games; fetchAddedGameArt() } - }, "added-games").start() + val fresh = folders.mapNotNull { f -> + com.droiddeck.launcher.frontend.AddedGames.single(this, f)?.let { com.droiddeck.launcher.frontend.Library.addedGame(this, it) } + } + ui.post { + val paths = folders.toSet() + steamGames = (steamGames.filterNot { it.library == Library.ADDED && it.gameFiles?.path in paths } + fresh).distinctBy { it.gameId } + then() + } + }, "added-games-update").start() } - /** Walks the added-games folders, which can sit on slow shared storage or an SD card. */ - private fun scanAddedGames() = com.droiddeck.launcher.frontend.AddedGames.scan(this).map { g -> - com.droiddeck.launcher.ui.AddedGameRow(g.folder.path, g.folderName(), g.exe.path, g.exe.name, (g.candidates + g.exe).map { c -> c.path to c.name }.distinctBy { it.first }) + /** An added game removed: off the Games tab now; the rest (cache, Steam, listing) off the main thread. */ + private fun removeAddedGame(folder: String, appId: Long?, name: String) { + steamGames = steamGames.filterNot { it.library == Library.ADDED && it.gameFiles?.path == folder } + Thread({ com.droiddeck.launcher.frontend.AddedGames.remove(this, java.io.File(folder), appId, name) }, "added-game-remove").start() + } + + /** After the added games changed: art that is missing is fetched (off the main thread). */ + private fun refreshAddedGames() { + sgdbKeyKnown() + fetchAddedGameArt() + } + + /** Whether the user has a SteamGridDB key of their own, for Setup; read off the main thread. */ + private fun sgdbKeyKnown() { + Thread({ + val set = com.droiddeck.launcher.frontend.SteamGridDb.userKey(this).isNotEmpty() + ui.post { sgdbUserKey = set } + }, "sgdb-key-state").start() } private fun fetchAddedGameArt() { @@ -1405,7 +1484,6 @@ class MainActivity : ComponentActivity() { mangoapp = SessionPrefs.mangoapp(this) steamController = SessionPrefs.steamController(this) runSteamAtStartup = SessionPrefs.runSteamAtStartup(this) - addedGamesDirs = SessionPrefs.addedGamesDirs(this) hdrOn = SessionPrefs.hdr(this, mode) fpsLimit = SessionPrefs.fpsLimit(this, mode) upscaler = SessionPrefs.upscaler(this) @@ -1417,11 +1495,9 @@ class MainActivity : ComponentActivity() { oscMode = SessionPrefs.oscMode(this) backActionsInverted = SessionPrefs.backActionsInverted(this) directAudio = SessionPrefs.directAudio(this) - clientDirectAudio = SessionPrefs.clientDirectAudio(this) stretch16x9 = SessionPrefs.stretch16x9(this) mic = SessionPrefs.micEnabled(this) refreshWifiDiscovery() - renderer = SessionPrefs.desktopRenderer(this) gameStorage = SessionPrefs.gameStorage(this) storageDiagnostics = SessionPrefs.storageDiagnosticsEnabled(this) settingsMode = mode @@ -1429,12 +1505,10 @@ class MainActivity : ComponentActivity() { // added-games folders, the storage volumes) lands while it animates in. Thread({ drivers.refreshDrivers() - val games = scanAddedGames() val storage = GameStorage.options(this) val deckyInstalled = if (mode == SessionService.MODE_STEAM) DeckyManager.installed(this) else null val deckySupervisor = mode == SessionService.MODE_STEAM && DeckyManager.supervisorEnabled(this) ui.post { - addedGames = games storageOptions = storage if (mode == SessionService.MODE_STEAM) { decky.deckyInstalled = deckyInstalled @@ -1483,7 +1557,9 @@ class MainActivity : ComponentActivity() { private fun refresh() { if (!busy && LinuxRuntimeInstaller.isBusy()) followRuntimeOperation { LinuxRuntimeInstaller.attach(it) } + addedGamesArt = SessionPrefs.addedGamesArt(this) desktopInstalled = DesktopCatalog.desktopInstalled(this) + desktopPresent = DesktopCatalog.desktopPresent(this) offlineAccount = OfflineMode.account(this) offline = OfflineMode.enabled(this) installed = LinuxRuntimeInstaller.installedVersion(this) @@ -1492,6 +1568,7 @@ class MainActivity : ComponentActivity() { frameGenLabel = FrameGen.label(this) romsDir = SessionPrefs.romsDir(this).takeIf { it.isNotEmpty() } logsEnabled = SessionPrefs.logsEnabled(this) + agentCommands = com.droiddeck.launcher.agent.AgentAccess.commandsEnabled(this) runningLabel = if (SessionState.running) when (SessionState.mode) { SessionService.MODE_DESKTOP -> getString(R.string.rail_desktop) SessionService.MODE_RUN -> Library.nameForProgram(SessionState.program) @@ -1595,6 +1672,15 @@ class MainActivity : ComponentActivity() { protons.protonBusyId != null || ProtonExtras.installInProgress || pkgStage != null || components.compBusy != null || saveBusy != null || com.droiddeck.launcher.store.UserAppsState.working != null + private fun removeDesktop() { + if (runtimeChangesBlocked()) return + val operation = LinuxRuntimeInstaller.beginMaintenance(this, getString(R.string.desktop_removing)) ?: return + com.droiddeck.launcher.runtime.RuntimeInstallService.keepRemovalAlive(this) + followRuntimeOperation { listener -> + operation.run(listener) { progress -> DesktopCatalog.removeDesktop(this, progress) } + } + } + private fun removeRuntime() { if (runtimeChangesBlocked()) return val removal = LinuxRuntimeInstaller.beginUninstall(this) ?: return @@ -1617,7 +1703,7 @@ class MainActivity : ComponentActivity() { private fun followRuntimeOperation(run: (LinuxRuntimeInstaller.ProgressListener) -> Boolean?) { busy = true failed = false - stage = getString(if (LinuxRuntimeInstaller.isRemoving()) R.string.main_removing_runtime else R.string.store_starting) + stage = getString(if (LinuxRuntimeInstaller.isMaintaining()) R.string.desktop_removing else if (LinuxRuntimeInstaller.isRemoving()) R.string.main_removing_runtime else R.string.store_starting) percent = -1 Thread({ val ok = run(LinuxRuntimeInstaller.ProgressListener { s, p -> diff --git a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt index 364bef141b..0ee7ff9017 100644 --- a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt @@ -45,6 +45,7 @@ import com.droiddeck.launcher.gpu.ScreenEffects import com.droiddeck.launcher.gpu.Lossless import com.droiddeck.launcher.gpu.TurnipDriver import com.droiddeck.launcher.input.EvdevKeys +import com.droiddeck.launcher.input.InputSpace import com.droiddeck.launcher.input.KeyboardHost import com.droiddeck.launcher.input.ControllerPrefs import com.droiddeck.launcher.input.SessionClipboard @@ -144,6 +145,15 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { if (resumed && !pipUi && SessionState.deckPad) padMotion?.start() else padMotion?.stop() } private var onScreenControls: OnScreenControls? = null + /** Steam's touch controls (SteamTouchControls), made once the session offers the device. */ + private var steamTouchControls: com.droiddeck.launcher.input.SteamTouchControls? = null + private var steamTouchAvailable by mutableStateOf(false) + private val steamTouchRetry = Runnable { updateOnScreenControls() } + /** While Steam's touch controls are up: how the game's touch config says touches outside them + * act (Steam Link's input and mouse modes), in place of the drawer's touch mode. */ + private var steamTouchOptions: com.droiddeck.launcher.input.SteamTouchConfig.Options? = null + /** The touch mode touches are routed by: the drawer's, or the game's while Steam's controls are up. */ + private var routedTouchMode = SessionPrefs.TOUCH_AUTO private var keyboard: KeyboardHost? = null private var controllerSettings by mutableStateOf(null) private val sessionClipboard by lazy { @@ -294,6 +304,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { } override fun onInputDeviceRemoved(deviceId: Int) { Log.i(TAG, "input device $deviceId disconnected") + padBridge?.onDeviceRemoved(deviceId) updateOnScreenControls() updatePointerCapture() } @@ -304,7 +315,8 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - if (com.droiddeck.launcher.runtime.LinuxRuntimeInstaller.isRemoving()) { + if (com.droiddeck.launcher.runtime.LinuxRuntimeInstaller.isRemoving() || + com.droiddeck.launcher.runtime.LinuxRuntimeInstaller.isMaintaining()) { runtimeRemovalBlocked = true android.widget.Toast.makeText(this, R.string.session_wait_removal, android.widget.Toast.LENGTH_SHORT).show() finish() @@ -335,6 +347,8 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { ) } Log.i(TAG, "perf: " + PerfMode.apply(this, SessionState.fpsLimit)) + if (!SessionState.running) SessionState.followScreen = followChoice() + applyFollowOrientation(resources.configuration) goFullscreen() // The device's volume keys change the stream the session plays on (the relay and // PulseAudio are media playback); they are never forwarded to the guest. @@ -362,6 +376,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { val bridge = PadBridge(File(LinuxRuntime.sessionRoot(this), "dev/input")) padBridge = bridge + SessionState.padBridge = bridge padMotion = com.droiddeck.launcher.input.PadMotion(this) { @Suppress("DEPRECATION") (if (Build.VERSION.SDK_INT >= 30) display else windowManager.defaultDisplay)?.rotation ?: android.view.Surface.ROTATION_0 @@ -558,7 +573,17 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { SessionPrefs.setBackActionsInverted(this@SessionActivity, inverted) backActionsInverted = inverted }, - onOsc = { v -> SessionPrefs.setOscMode(this@SessionActivity, v); readPrefs(); updateOnScreenControls() }, + onOsc = { v -> + SessionPrefs.setOscMode(this@SessionActivity, v) + readPrefs() + updateOnScreenControls() + if (v == SessionPrefs.OSC_STEAM_TOUCH && com.droiddeck.launcher.input.SteamTouchDevice.current == null) + Toast.makeText(this@SessionActivity, R.string.osc_style_next_session, Toast.LENGTH_LONG).show() + }, + onEditSteamTouch = if (steamTouchAvailable && oscMode == SessionPrefs.OSC_STEAM_TOUCH && onScreenButtonsVisible) ({ + drawerOpen = false + steamTouchControls?.startEditing() + }) else null, onSuspendPolicy = { policy -> SessionPrefs.setSuspendPolicy(this@SessionActivity, SessionState.mode, policy) suspendPolicy = policy @@ -741,9 +766,9 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { } private fun loadingTitle(): String = when (loadingMode()) { - SessionService.MODE_DESKTOP -> - if (!SessionState.running && intent.getStringExtra(SessionService.EXTRA_STEAM_UI) != null) getString(R.string.session_starting_steam_desktop) - else getString(R.string.session_starting_desktop) + // Also when the desktop was asked for with Steam (Big Picture's Switch to Desktop): the client + // does not open on it. + SessionService.MODE_DESKTOP -> getString(R.string.session_starting_desktop) SessionService.MODE_RUN -> { val program = if (SessionState.running) SessionState.program else intent.getStringExtra(SessionService.EXTRA_PROGRAM) com.droiddeck.launcher.frontend.Library.nameForProgram(program)?.let { getString(R.string.session_starting_named, it) } @@ -793,6 +818,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { if (nextTouchMode == SessionPrefs.TOUCH_OFF) cursorVisible = false } touchMode = nextTouchMode + routedTouchMode = effectiveTouchMode() frameGen = FrameGen.mode(this) fexPreset = SessionPrefs.fexPreset(this) suspendPolicy = SessionPrefs.suspendPolicy(this, SessionState.mode) @@ -925,8 +951,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { /** Null when the desktop package is in, else the loading screen's closing line. */ private fun installDesktop(): String? { uiHandler.post { loading.percent = -1; loading.say(getString(DESKTOP_LINES.downloading), DESKTOP_LINES.topic, readable = true) } - val entry = com.droiddeck.launcher.runtime.DesktopCatalog.fetch()?.firstOrNull { it.id == "desktop" } - ?: return getString(R.string.session_desktop_catalog_unreachable) + val entry = com.droiddeck.launcher.runtime.DesktopCatalog.desktopEntry(this) val problem = com.droiddeck.launcher.runtime.DesktopCatalog.install(this, entry, progressFor(DESKTOP_LINES, entry.size / 1_000_000)) return problem?.let { getString(R.string.session_desktop_install_failed, it) } @@ -960,7 +985,13 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { // sized once when the session starts and cannot change. A foldable recreates the Surface // on the other panel, and recomputing the size there told the compositor a 21:9 buffer // was 16:9 - the picture came back squashed sideways. While a session runs, keep its size. - val size = if (SessionState.running) SessionState.outputSize else outputSize() + // Follow screen is the exception: there the compositor tells gamescope the new size, so the + // session takes the shape of the Surface it now has. + val previous = SessionState.outputSize + val live = if (SessionState.followScreen && !isInPictureInPictureMode) + com.droiddeck.launcher.session.SessionDisplay.followSize(holder.surfaceFrame.width(), holder.surfaceFrame.height()) + else null + val size = live ?: if (SessionState.running) previous else outputSize() SessionState.outputSize = size if (!SessionState.running) SessionState.refreshHz = refreshHz() // How far the panel enlarges the session, for texture sharpness "Auto" (TextureFiltering): @@ -1019,6 +1050,10 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { driverId?.let { turnip.driverPath(it) }, driverId?.let { turnip.libraryName(it) }, applicationInfo.nativeLibraryDir, size.first, size.second, refreshHz(), SessionState.fpsLimit, ) + if (SessionState.running && size != previous) { + Log.i(TAG, "follow screen: ${previous.first}x${previous.second} -> ${size.first}x${size.second} on a new surface") + WaylandCompositor.nativeResizeOutput(size.first, size.second) + } if (!SessionState.running) SessionEvents.record("compositor.started") // The service owns everything below the compositor. It is started whenever no session is // running - NOT only when the compositor was just started: the compositor lives for the @@ -1047,9 +1082,51 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { if (resized) { Log.i(TAG, "surface resized to ${width}x$height - rebinding the compositor") CompositorHost.resize(holder.surface) { applyFrameGen() } + followWindow(width, height) } } + /** + * Follow screen: the session's display takes the window's new size (a Fold opening onto its + * inner panel, or closing onto the cover), so the picture fills the panel instead of being + * letterboxed onto it. Not in picture-in-picture: that window is a preview of the session, + * and resizing the guest to it would leave Steam laid out for a thumbnail. + */ + private fun followWindow(width: Int, height: Int) { + if (!SessionState.followScreen || isInPictureInPictureMode || !CompositorHost.isStarted) return + val size = com.droiddeck.launcher.session.SessionDisplay.followSize(width, height) ?: return + if (size == SessionState.outputSize) return + Log.i(TAG, "follow screen: ${SessionState.outputSize.first}x${SessionState.outputSize.second} -> ${size.first}x${size.second}") + SessionState.outputSize = size + val panel = panelBounds() + SessionState.upscaleRatio = maxOf(panel.width(), panel.height()).toFloat() / maxOf(size.first, size.second).coerceAtLeast(1) + WaylandCompositor.nativeResizeOutput(size.first, size.second) + } + + /** Whether this session's resolution is Follow screen; decided once, when it starts. */ + private fun followChoice(): Boolean { + val mode = SessionPrefs.prefMode(intent.getStringExtra(SessionService.EXTRA_MODE) ?: SessionService.MODE_STEAM) + return SessionPrefs.resolutionChoice(this, mode, com.droiddeck.launcher.session.SessionDisplay.panelSize(this)) == + com.droiddeck.launcher.session.SessionDisplay.FOLLOW_SCREEN + } + + /** + * Follow screen on a large panel (a Fold's inner screen) may turn with the device: a window + * locked to landscape on a squarer panel is letterboxed by the system itself, which no output + * size can undo. A phone-sized panel (the cover screen) stays landscape as before. + */ + private fun applyFollowOrientation(config: android.content.res.Configuration) { + if (!SessionState.followScreen) return + val wanted = if (config.smallestScreenWidthDp >= 600) android.content.pm.ActivityInfo.SCREEN_ORIENTATION_USER + else android.content.pm.ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE + if (requestedOrientation != wanted) requestedOrientation = wanted + } + + override fun onConfigurationChanged(newConfig: android.content.res.Configuration) { + super.onConfigurationChanged(newConfig) + applyFollowOrientation(newConfig) + } + override fun surfaceDestroyed(holder: SurfaceHolder) { surfaceW = 0 surfaceH = 0 @@ -1275,7 +1352,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { return super.dispatchKeyEvent(event) } if (pipUi) return true - val fromController = event.device != null && PadBridge.isFromController(event.device) + val fromController = PadBridge.isControllerKey(event) if (fromController && event.action == KeyEvent.ACTION_DOWN) { if (drawerOpen && !drawerControllerActive) sessionOverlay.requestFocus() drawerControllerActive = true @@ -1327,11 +1404,11 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { } return true } - if (pcKeyboardOpen && event.device != null && PadBridge.isFromController(event.device)) return super.dispatchKeyEvent(event) + if (pcKeyboardOpen && fromController) return super.dispatchKeyEvent(event) if (event.keyCode != KeyEvent.KEYCODE_BACK && padBridge?.onKeyEvent(event) == true) return true // A hardware keyboard, forwarded to the compositor's wl_keyboard. Back is left to the // activity, which opens the drawer. - val fromPad = event.device != null && PadBridge.isFromController(event.device) + val fromPad = fromController if (CompositorHost.isStarted && event.keyCode != KeyEvent.KEYCODE_BACK && !fromPad) { val down = event.action == KeyEvent.ACTION_DOWN if (down || event.action == KeyEvent.ACTION_UP) { @@ -1485,18 +1562,11 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { } private fun movePointer(x: Float, y: Float) { - val width = surfaceView.width.takeIf { it > 0 } ?: return - val height = surfaceView.height.takeIf { it > 0 } ?: return - val out = SessionState.outputSize - val scale = minOf(width / out.first.toFloat(), height / out.second.toFloat()) - val drawnW = out.first * scale - val drawnH = out.second * scale - val left = (width - drawnW) / 2f - val top = (height - drawnH) / 2f - val px = ((x - left) / drawnW * 1920f).toInt().coerceIn(0, 1919) - val py = ((y - top) / drawnH * 1080f).toInt().coerceIn(0, 1079) - WaylandCompositor.nativeSendPointer(1, px, py) - showCursor(x.coerceIn(left, left + drawnW), y.coerceIn(top, top + drawnH)) + val rect = drawnRect() ?: return + val cx = x.coerceIn(rect.left, rect.right) + val cy = y.coerceIn(rect.top, rect.bottom) + WaylandCompositor.nativeSendPointer(1, InputSpace.x(cx, surfaceView.width), InputSpace.y(cy, surfaceView.height)) + showCursor(cx, cy) } /** @@ -1643,10 +1713,11 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { if (width <= 0f || height <= 0f) return val nx = (x / width).coerceIn(0f, 1f) val ny = (y / height).coerceIn(0f, 1f) - WaylandCompositor.nativeSendPointer(1, (nx * 1919f).toInt(), (ny * 1079f).toInt()) - drawnRect()?.let { rect -> - showCursor(rect.left + nx * rect.width(), rect.top + ny * rect.height()) - } + val rect = drawnRect() ?: return + val px = rect.left + nx * rect.width() + val py = rect.top + ny * rect.height() + WaylandCompositor.nativeSendPointer(1, InputSpace.x(px, surfaceView.width), InputSpace.y(py, surfaceView.height)) + showCursor(px, py) } private fun closeSecondScreen(reset: Boolean) { @@ -1658,8 +1729,32 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { } } + /** The touch mode in force: the game's own while Steam's touch controls are shown. */ + private fun effectiveTouchMode(): String { + val options = steamTouchOptions + if (options == null || steamTouchControls?.visibility != View.VISIBLE) return SessionPrefs.touchMode(this) + return when { + options.inputMode == com.droiddeck.launcher.input.SteamTouchConfig.INPUT_CONTROLLER -> SessionPrefs.TOUCH_OFF + options.mouseMode == com.droiddeck.launcher.input.SteamTouchConfig.MOUSE_RELATIVE -> SessionPrefs.TOUCH_PAD + else -> SessionPrefs.TOUCH_DIRECT + } + } + + private fun applyTouchRouting() { + val next = effectiveTouchMode() + if (::touchpad.isInitialized) { + val speed = steamTouchOptions?.trackpadSensitivity?.takeIf { steamTouchControls?.visibility == View.VISIBLE } ?: 1f + touchpad.sensitivity = 1.15f * speed + } + if (next == routedTouchMode) return + if (::touchpad.isInitialized) touchpad.cancel() + if (CompositorHost.isStarted) WaylandCompositor.nativeSendTouch(3, -1, 0, 0) + if (next == SessionPrefs.TOUCH_OFF) cursorVisible = false + routedTouchMode = next + } + /** Touchpad on the desktop, direct in Steam, unless the drawer says otherwise. */ - private fun usingTouchpad(): Boolean = when (SessionPrefs.touchMode(this)) { + private fun usingTouchpad(): Boolean = when (routedTouchMode) { SessionPrefs.TOUCH_PAD -> true SessionPrefs.TOUCH_DIRECT -> false else -> SessionState.mode != SessionService.MODE_STEAM || SessionState.steamUi == "desktop" @@ -1681,7 +1776,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { override fun onTouchEvent(event: MotionEvent): Boolean { if (event.isFromSource(android.view.InputDevice.SOURCE_MOUSE)) return onMouse(event) // Child views get the event first, so menus, keyboards and the on-screen pad still work. - if (touchMode == SessionPrefs.TOUCH_OFF) return true + if (routedTouchMode == SessionPrefs.TOUCH_OFF) return true if (usingTouchpad()) { val rect = drawnRect() ?: return false if (touchpad.bounds != rect) { @@ -1693,9 +1788,10 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { } val rect = drawnRect() ?: return false fun sendTouch(action: Int, index: Int) { - val x = ((event.getX(index) - rect.left) / rect.width()).coerceIn(0f, 1f) - val y = ((event.getY(index) - rect.top) / rect.height()).coerceIn(0f, 1f) - WaylandCompositor.nativeSendTouch(action, event.getPointerId(index), (x * 1919f).toInt(), (y * 1079f).toInt()) + val x = event.getX(index).coerceIn(rect.left, rect.right) + val y = event.getY(index).coerceIn(rect.top, rect.bottom) + WaylandCompositor.nativeSendTouch(action, event.getPointerId(index), + InputSpace.x(x, surfaceView.width), InputSpace.y(y, surfaceView.height)) } when (event.actionMasked) { // A first finger starts a new gesture, and Android has ended every earlier one: whatever @@ -1799,6 +1895,41 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { what, d.name, d.vendorId, d.productId, d.id, d.sources)) } + /** Steam's touch controls, once the session has made the device (SessionService). */ + private fun steamTouch(): com.droiddeck.launcher.input.SteamTouchControls? { + val device = com.droiddeck.launcher.input.SteamTouchDevice.current ?: return null + steamTouchControls?.let { view -> + if (view.device === device) return view + // A new session made a new device: the view of the old one goes. + view.releaseAll() + (view.parent as? android.view.ViewGroup)?.removeView(view) + steamTouchControls = null + } + val root = onScreenControls?.parent as? android.view.ViewGroup ?: return null + val view = com.droiddeck.launcher.input.SteamTouchControls(this, device, + onMenu = { drawerOpen = true }, onKeyboard = ::togglePcKeyboard) + view.visibility = View.GONE + view.onOptions = { options -> + steamTouchOptions = options + applyTouchRouting() + } + // Paste: the clipboard is shared with the session's X servers; Ctrl+V types it. + view.onPaste = { + sendGuestKey(29, true); sendGuestKey(47, true) // KEY_LEFTCTRL, KEY_V + sendGuestKey(47, false); sendGuestKey(29, false) + } + // The client reads a controller's config when it connects: after a save, plug the device + // out and back in so it takes the new one. + view.onSaved = { + device.plugged = false + uiHandler.postDelayed({ updateOnScreenControls() }, 700) + } + root.addView(view, root.indexOfChild(onScreenControls) + 1) + steamTouchControls = view + steamTouchAvailable = true + return view + } + private fun updateOnScreenControls() { val forced = File(Environment.getExternalStorageDirectory(), "Download/droiddeck-osc") .takeIf { it.isFile } @@ -1809,20 +1940,49 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { onScreenButtonsVisible = false controls.releaseAll() controls.visibility = View.GONE + steamTouchControls?.let { it.releaseAll(); it.visibility = View.GONE } + com.droiddeck.launcher.input.SteamTouchDevice.current?.plugged = false return } val buttonsOnly = forced == SessionPrefs.OSC_STEAM_QAM + // Steam's touch controller stands in for the app's pad, and is plugged into the client only + // while it is shown - as Steam Link withdraws its controls when a physical pad is in use. + val wantsSteam = forced == SessionPrefs.OSC_STEAM_TOUCH && SessionState.mode == SessionService.MODE_STEAM + val steam = if (wantsSteam) steamTouch() else null + // The service makes the device while the session starts, after this activity: the app's + // own pad until then, and another look in a moment. + if (wantsSteam && steam == null && !isFinishing && !isDestroyed) { + uiHandler.removeCallbacks(steamTouchRetry) + uiHandler.postDelayed(steamTouchRetry, 1000) + } + if (steam == null) { + steamTouchControls?.let { it.releaseAll(); it.visibility = View.GONE } + com.droiddeck.launcher.input.SteamTouchDevice.current?.plugged = false + } controls.setButtonsOnly(buttonsOnly) val show = when (forced) { SessionPrefs.OSC_ALWAYS -> true SessionPrefs.OSC_STEAM_QAM -> true SessionPrefs.OSC_NEVER -> false + // Shown throughout a Steam session: a handheld's built-in pad would keep Auto off for good. + // Outside Steam it is Auto. + SessionPrefs.OSC_STEAM_TOUCH -> SessionState.mode == SessionService.MODE_STEAM || + (!PadBridge.anyControllerConnected() && SessionState.mode != SessionService.MODE_DESKTOP) // Auto: the touch pad when there is no controller - except on the desktop, where the // screen is a touchpad for the pointer and a pad over it would be in the way. A game // started from the rail, or Steam, gets it; the drawer turns it on anywhere. else -> !PadBridge.anyControllerConnected() && SessionState.mode != SessionService.MODE_DESKTOP } onScreenButtonsVisible = show + if (steam != null) { + if (controls.visibility == View.VISIBLE) { controls.releaseAll(); controls.visibility = View.GONE } + if (!show) steam.releaseAll() + steam.visibility = if (show) View.VISIBLE else View.GONE + com.droiddeck.launcher.input.SteamTouchDevice.current?.plugged = show + applyTouchRouting() + Log.i(TAG, "on-screen controls: Steam's touch controller " + if (show) "shown" else "hidden") + return + } if (show == (controls.visibility == View.VISIBLE)) return if (!show) controls.releaseAll() controls.visibility = if (show) View.VISIBLE else View.GONE @@ -1894,6 +2054,8 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { override fun onPictureInPictureModeChanged(inPip: Boolean, newConfig: android.content.res.Configuration) { super.onPictureInPictureModeChanged(inPip, newConfig) if (::pip.isInitialized) pip.changed(inPip) + // The panel may have changed while the session was a thumbnail (folded or unfolded in PiP). + if (!inPip && surfaceW > 0) followWindow(surfaceW, surfaceH) } override fun startActivityForResult(intent: Intent, requestCode: Int, options: Bundle?) { @@ -2021,10 +2183,13 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { or View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION) + // ALWAYS from Android 11: SHORT_EDGES left a black strip along a camera on the long edge, + // which is where a Fold's inner-screen camera sits with the device held in landscape. if (Build.VERSION.SDK_INT >= 28) { window.attributes = window.attributes.apply { - layoutInDisplayCutoutMode = - WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES + layoutInDisplayCutoutMode = if (Build.VERSION.SDK_INT >= 30) + WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS + else WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES } } } @@ -2041,6 +2206,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { } override fun onDestroy() { + uiHandler.removeCallbacks(steamTouchRetry) if (runtimeRemovalBlocked) { super.onDestroy(); return } // Deliberately does NOT end the session: this activity can be destroyed while the user is // in another app, and the whole point of the service is that Steam survives that. @@ -2052,6 +2218,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { closeSecondScreen(reset = false) if (::hud.isInitialized) hud.stop() padBridge?.stop() + if (SessionState.padBridge === padBridge) SessionState.padBridge = null padMotion?.stop() if (SessionState.deckPadListener === deckPadListener) SessionState.deckPadListener = null if (SessionState.endListener === endListener) SessionState.endListener = null diff --git a/app/src/main/java/com/droiddeck/launcher/agent/AgentAccess.kt b/app/src/main/java/com/droiddeck/launcher/agent/AgentAccess.kt new file mode 100644 index 0000000000..e43ba8669c --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/agent/AgentAccess.kt @@ -0,0 +1,48 @@ +package com.droiddeck.launcher.agent + +import android.content.Context + +/** + * Who may run code through the agent bridge. Reading state, starting and stopping sessions, + * launching and quitting games and sending input only need the shell (the provider's DUMP + * permission). Running commands in the guest, evaluating JavaScript in the Steam client, session + * environment, binary overrides and preference writes reach inside the app's sandbox - the guest + * holds the Steam login - which the shell of a stock device otherwise cannot. They require + * Setup > Session > Debugging tools to be on, in every build. The bridge itself cannot turn it on. + */ +object AgentAccess { + const val MAX_REQUEST_CHARS = 256 * 1024 + private const val PREFS = "agent" + private const val KEY_COMMANDS = "commands" + + /** The preferences file the bridge must never write: set-pref refuses it. */ + const val PROTECTED_PREFS = PREFS + + fun commandsAllowed(context: Context): Boolean = + prefs(context).getBoolean(KEY_COMMANDS, false) + + /** The Setup toggle's own state. */ + fun commandsEnabled(context: Context): Boolean = prefs(context).getBoolean(KEY_COMMANDS, false) + + fun setCommandsEnabled(context: Context, enabled: Boolean) { + prefs(context).edit().putBoolean(KEY_COMMANDS, enabled).apply() + if (!enabled) runCatching { AgentEnv.revoke(context) } + .onFailure { android.util.Log.w("AgentAccess", "Could not finish clearing debugging experiments") } + } + + fun requireCommands(context: Context) { + if (!commandsAllowed(context)) throw AgentException( + "AGENT_COMMANDS_DISABLED", + "Debugging tools are off: turn on Setup > Session > Debugging tools on the device", + ) + } + + fun requireStart(context: Context, request: org.json.JSONObject) { + if (request.optString("mode") == "run") requireCommands(context) + } + + private fun prefs(context: Context) = context.getSharedPreferences(PREFS, Context.MODE_PRIVATE) +} + +/** A refusal with a stable code, answered as {"ok": false, "error": {code, message}}. */ +class AgentException(val code: String, message: String) : RuntimeException(message) diff --git a/app/src/main/java/com/droiddeck/launcher/agent/AgentBridgeProvider.kt b/app/src/main/java/com/droiddeck/launcher/agent/AgentBridgeProvider.kt index 4e8b066eb8..2b8ba775b0 100644 --- a/app/src/main/java/com/droiddeck/launcher/agent/AgentBridgeProvider.kt +++ b/app/src/main/java/com/droiddeck/launcher/agent/AgentBridgeProvider.kt @@ -1,5 +1,6 @@ package com.droiddeck.launcher.agent +import com.droiddeck.launcher.session.SessionPaths import android.Manifest import android.content.ContentProvider import android.content.ContentValues @@ -10,13 +11,14 @@ import android.net.Uri import android.os.Binder import android.os.Bundle import android.os.Process +import android.util.Base64 import com.droiddeck.launcher.BuildConfig +import com.droiddeck.launcher.frontend.GameLaunchLink import com.droiddeck.launcher.runtime.LinuxRuntime import com.droiddeck.launcher.runtime.LinuxRuntimeInstaller import com.droiddeck.launcher.session.SessionArtifacts import com.droiddeck.launcher.session.SessionEvents import com.droiddeck.launcher.session.SessionPhase -import com.droiddeck.launcher.session.SessionPaths import com.droiddeck.launcher.session.SessionService import com.droiddeck.launcher.session.SessionState import org.json.JSONArray @@ -25,7 +27,14 @@ import java.io.File import java.util.concurrent.CountDownLatch import java.util.concurrent.TimeUnit -/** A shell-only control surface, installed only by debug builds. */ +/** + * The shell's control surface (docs/agent-control.md), in every build: the provider and its start + * Activity need android.permission.DUMP, held by the shell and privileged or explicitly granted callers. Commands that + * reach into the app's sandbox are further gated by [AgentAccess]. + * + * A method's request is JSON, base64-encoded in the "request" extra; every answer is JSON in the + * "json" key of the returned Bundle, {"ok": true, ...} or {"ok": false, "error": {code, message}}. + */ class AgentBridgeProvider : ContentProvider() { override fun onCreate(): Boolean { val appContext = context?.applicationContext ?: return false @@ -48,22 +57,58 @@ class AgentBridgeProvider : ContentProvider() { } val response = try { - when (method) { - "state" -> state(context).put("ok", true) - "start" -> error("USE_DROIDDECKCTL", "Start sessions with tools/droiddeckctl so Android launches a visible Activity") - "stop" -> stop(context) - "resume" -> resume(context) - else -> error("UNKNOWN_COMMAND", "Unknown agent command '$method'") + val request = decode(extras?.getString(EXTRA_REQUEST)) + // Binder identity is the shell's; everything below acts as the app. + val token = Binder.clearCallingIdentity() + try { + dispatch(context, method, request) + } finally { + Binder.restoreCallingIdentity(token) } + } catch (e: AgentException) { + error(e.code, e.message ?: e.code) } catch (e: Exception) { error("COMMAND_FAILED", e.message ?: e.javaClass.simpleName) } - return Bundle().apply { putString(RESULT_JSON, response.toString()) } + val json = response.toString().let { + if (it.length <= 384 * 1024) it else error("RESPONSE_TOO_LARGE", "Reduce the command output or select fewer fields").toString() + } + return Bundle().apply { putString(RESULT_JSON, json) } + } + + private fun dispatch(context: Context, method: String, request: JSONObject): JSONObject = when (method) { + "state" -> state(context).put("ok", true) + "start" -> error("USE_DROIDDECKCTL", "Start sessions with tools/droiddeckctl so Android launches a visible Activity") + "stop" -> stop(context) + "resume" -> resume(context) + "launch" -> launch(context, request) + "focus" -> guest(context, JSONObject().put("kind", "focus"), 15_000) + "quit" -> guest(context, JSONObject().put("kind", "quit").put("appId", request.opt("appId")) + .put("timeout", request.optDouble("timeout", 15.0)), timeoutMs(request, 15.0, 30_000)) + "input" -> AgentInput.handle(request) + "ui" -> AgentUi.handle(request) + "guest" -> { + AgentAccess.requireCommands(context) + guest(context, JSONObject(request.toString()).put("kind", "exec"), timeoutMs(request, 30.0, 10_000)) + } + "cdp" -> { + AgentAccess.requireCommands(context) + guest(context, JSONObject(request.toString()).put("kind", "cdp"), timeoutMs(request, 15.0, 5_000)) + } + "env" -> env(context, request) + "override" -> { + AgentAccess.requireCommands(context) + AgentEnv.override(context, request.optString("name")).put("ok", true).put("command", "override") + } + "prefs" -> prefs(context, request) + "access" -> JSONObject().put("ok", true).put("commands", AgentAccess.commandsAllowed(context)) + .put("debugBuild", BuildConfig.DEBUG).put("inbox", AgentEnv.inbox(context).absolutePath) + else -> error("UNKNOWN_COMMAND", "Unknown agent command '$method'") } private fun state(context: Context): JSONObject { val runtimeVersion = LinuxRuntimeInstaller.installedVersion(context) - val dir = SessionState.logDirectory ?: SessionState.logFile?.parentFile ?: latestSessionDirectory() + val dir = SessionState.logDirectory ?: SessionState.logFile?.parentFile ?: SessionPaths.sessionFolders(context).lastOrNull() val session = JSONObject() .put("id", SessionState.sessionId ?: dir?.name ?: JSONObject.NULL) .put("phase", SessionState.phase.name) @@ -91,22 +136,34 @@ class AgentBridgeProvider : ContentProvider() { .put("status", SessionState.failureStatus ?: JSONObject.NULL) session.put("failure", failure) + // gamescope's focus as last published by the guest agent, without the window lists + // (the focus command returns those, fresh). + val focus = AgentGuest.focus(context)?.let { snap -> + JSONObject() + .put("focusedApp", snap.opt("focusedApp") ?: JSONObject.NULL) + .put("focusedWindow", snap.opt("focusedWindow") ?: JSONObject.NULL) + .put("focusableApps", snap.optJSONArray("focusableApps") ?: JSONArray()) + .put("baselayerAppIds", snap.optJSONArray("baselayerAppIds") ?: JSONArray()) + .put("seq", snap.optInt("seq")) + .put("t", snap.optLong("t")) + } + session.put("focus", focus ?: JSONObject.NULL) + + val hello = AgentGuest.hello(context) return JSONObject() - .put("schema", 1) + .put("schema", SCHEMA) .put("build", BuildConfig.BUILD_LABEL) .put("appVersion", context.packageManager.getPackageInfo(context.packageName, 0).versionName) .put("runtime", JSONObject() .put("installed", LinuxRuntime.isInstalled(context)) .put("version", runtimeVersion ?: JSONObject.NULL)) + .put("agent", JSONObject() + .put("guest", hello != null) + .put("guestFocus", hello?.optBoolean("focus") ?: false) + .put("commands", AgentAccess.commandsAllowed(context))) .put("session", session) } - private fun latestSessionDirectory(): File? { - val parent = LinuxRuntime.debugLogDir() - return parent.listFiles { file -> file.isDirectory && file.name.startsWith("session-") } - ?.maxWithOrNull(compareBy { it.lastModified() }.thenBy { it.name }) - } - private fun stop(context: Context): JSONObject { if (SessionState.running) { SessionService.stop(context) @@ -131,6 +188,67 @@ class AgentBridgeProvider : ContentProvider() { return JSONObject().put("ok", true).put("command", "resume") } + private fun launch(context: Context, request: JSONObject): JSONObject { + val appId = request.optString("appId") + if (!GameLaunchLink.validId(appId)) throw AgentException("INVALID_APP_ID", "appId must be a decimal Steam app id") + if (!SessionState.running || SessionState.mode != SessionService.MODE_STEAM) { + throw AgentException("NO_STEAM_SESSION", "Games launch inside a running Steam session") + } + if (!SessionService.launchGame(context, appId)) throw AgentException("LAUNCH_REJECTED", "The session did not accept the launch") + SessionEvents.record("agent.launch_requested", mapOf("appId" to appId)) + return JSONObject().put("ok", true).put("command", "launch").put("appId", appId) + } + + private fun env(context: Context, request: JSONObject): JSONObject { + AgentAccess.requireCommands(context) + val result = when (val op = request.optString("op", "list")) { + "list" -> AgentEnv.list(context) + "set" -> { + val lines = request.optJSONArray("lines") ?: JSONArray() + AgentEnv.set(context, List(lines.length()) { lines.optString(it) }, request.optBoolean("persistent")) + } + "clear" -> AgentEnv.clear(context, request.optString("scope", "next")) + else -> throw AgentException("INVALID_REQUEST", "env op '$op' is not list, set or clear") + } + return result.put("ok", true).put("command", "env") + } + + private fun prefs(context: Context, request: JSONObject): JSONObject { + AgentAccess.requireCommands(context) + val file = request.optString("file") + val result = when (val op = request.optString("op", "get")) { + "get" -> AgentPrefs.get(context, file) + "set" -> AgentPrefs.set(context, file, request.optString("key"), request.optString("type", "string"), + if (request.isNull("value")) null else request.optString("value")) + else -> throw AgentException("INVALID_REQUEST", "prefs op '$op' is not get or set") + } + return result.put("ok", true).put("command", "prefs") + } + + /** A guest agent request; its own answer already carries ok or error. */ + private fun guest(context: Context, request: JSONObject, timeoutMs: Long): JSONObject = + AgentGuest.call(context, request, timeoutMs) + + /** The guest's own timeout plus slack for the round trip. */ + private fun timeoutMs(request: JSONObject, defaultSeconds: Double, slackMs: Long): Long { + val seconds = if (request.has("timeout")) request.opt("timeout")?.toString()?.toDoubleOrNull() + ?: throw AgentException("INVALID_REQUEST", "timeout must be a number") else defaultSeconds + if (!seconds.isFinite() || seconds <= 0 || seconds > 600) { + throw AgentException("INVALID_REQUEST", "timeout must be a positive number no greater than 600 seconds") + } + return (seconds * 1000).toLong() + slackMs + } + + private fun decode(encoded: String?): JSONObject { + if (encoded.isNullOrBlank()) return JSONObject() + if (encoded.length > AgentAccess.MAX_REQUEST_CHARS) throw AgentException("INVALID_REQUEST", "The encoded request exceeds 256 KiB") + return try { + JSONObject(String(Base64.decode(encoded, Base64.DEFAULT), Charsets.UTF_8)) + } catch (e: Exception) { + throw AgentException("INVALID_REQUEST", "The request extra is not base64 JSON") + } + } + private fun error(code: String, message: String) = JSONObject() .put("ok", false) .put("error", JSONObject().put("code", code).put("message", message)) @@ -143,6 +261,9 @@ class AgentBridgeProvider : ContentProvider() { companion object { const val RESULT_JSON = "json" + const val EXTRA_REQUEST = "request" + /** 2 added agent and session.focus; everything in 1 is unchanged. */ + const val SCHEMA = 2 private val recoveryComplete = CountDownLatch(1) fun awaitRecovery() { diff --git a/app/src/main/java/com/droiddeck/launcher/agent/AgentEnv.kt b/app/src/main/java/com/droiddeck/launcher/agent/AgentEnv.kt new file mode 100644 index 0000000000..83ea0ccd11 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/agent/AgentEnv.kt @@ -0,0 +1,222 @@ +package com.droiddeck.launcher.agent + +import android.content.Context +import android.util.Log +import com.droiddeck.launcher.runtime.LinuxRuntime +import org.json.JSONArray +import org.json.JSONObject +import java.io.File +import java.nio.file.Files +import java.nio.file.LinkOption.NOFOLLOW_LINKS +import java.nio.file.StandardOpenOption.READ + +/** + * Experiment switches an agent sets without touching Download/droiddeck-env or rebuilding the apk: + * + * - environment lines (KEY=VALUE) for the session, applied after the user's own droiddeck-env, so + * they win. "next" lines are taken by the next session that starts and dropped when it ends; + * "persistent" ones stay until cleared. + * - binary overrides: a file pushed to the app's external files (agent-inbox/) is installed + * as /opt/droiddeck-agent/bin/ in the rootfs and put first on the next session's PATH - + * a gamescope build under test, say. Removed when that session ends. + * + * Kept in the app's own files (agent-env.json), never in a place the shell can write. + */ +object AgentEnv { + private const val TAG = "AgentEnv" + private const val FILE = "agent-env.json" + private const val GUEST_BIN = "/opt/droiddeck-agent/bin" + private const val BASE_PATH = "/usr/local/bin:/usr/bin:/bin" + private val NAME = Regex("[A-Za-z0-9._-]{1,64}") + private val KEY = Regex("[A-Za-z_][A-Za-z0-9_]*") + private val lock = Any() + + fun inbox(context: Context): File = File(context.getExternalFilesDir(null), "agent-inbox") + + fun list(context: Context): JSONObject = synchronized(lock) { + AgentAccess.requireCommands(context) + load(context) + } + + /** Add [lines] for the next session ([persistent] = every session until cleared). */ + fun set(context: Context, lines: List, persistent: Boolean): JSONObject = synchronized(lock) { + AgentAccess.requireCommands(context) + if (lines.any { line -> KEY.matchEntire(line.substringBefore('=', "")) == null || !line.contains('=') || line.any { it == '\n' || it == '\r' || it == '\u0000' } }) { + throw AgentException("INVALID_ENV", "Environment entries must be single KEY=VALUE lines without NUL bytes") + } + val state = load(context) + val key = if (persistent) "persistent" else "pending" + val merged = merge(strings(state.optJSONArray(key)), lines) + state.put(key, JSONArray(merged)) + save(context, state) + state + } + + /** Drop lines: [scope] is "next", "persistent" or "all". */ + fun clear(context: Context, scope: String): JSONObject = synchronized(lock) { + AgentAccess.requireCommands(context) + val state = load(context) + when (scope) { + "next" -> state.put("pending", JSONArray()) + "persistent" -> state.put("persistent", JSONArray()) + "all" -> { state.put("pending", JSONArray()); state.put("persistent", JSONArray()); state.put("pendingOverrides", JSONArray()) } + else -> throw AgentException("INVALID_SCOPE", "Scope must be next, persistent or all") + } + save(context, state) + state + } + + /** Install agent-inbox/[name] as an override for the next session. */ + fun override(context: Context, name: String): JSONObject = synchronized(lock) { + AgentAccess.requireCommands(context) + if (NAME.matchEntire(name) == null || name in setOf(".", "..")) throw AgentException("INVALID_NAME", "Override names are letters, digits, '.', '_' and '-'") + val source = File(inbox(context), name) + if (Files.isSymbolicLink(source.toPath())) throw AgentException("INVALID_NAME", "The override must be a regular file") + if (!source.isFile) throw AgentException("NO_SUCH_FILE", "Push the binary to ${source.path} first") + val target = File(bin(context), name) + target.parentFile?.mkdirs() + val staged = File.createTempFile("override-", ".tmp", target.parentFile) + try { + Files.newInputStream(source.toPath(), READ, NOFOLLOW_LINKS).use { input -> staged.outputStream().use { input.copyTo(it) } } + } catch (e: Exception) { + staged.delete() + throw AgentException("OVERRIDE_FAILED", "Could not stage the override") + } + if (!staged.setExecutable(true, false) || !staged.renameTo(target)) { + staged.delete() + throw AgentException("OVERRIDE_FAILED", "Could not install $name in the rootfs") + } + source.delete() + val state = load(context) + state.put("pendingOverrides", JSONArray(merge(strings(state.optJSONArray("pendingOverrides")), listOf(name)))) + save(context, state) + state.put("installed", "$GUEST_BIN/$name") + } + + /** + * At session start: the lines this session gets, after the user's droiddeck-env. Pending lines + * and overrides become this session's own, to be dropped by [endSession]. + */ + fun beginSession(context: Context): List = synchronized(lock) { + if (!AgentAccess.commandsAllowed(context)) { + revoke(context) + return@synchronized emptyList() + } + val state = load(context) + val active = strings(state.optJSONArray("pending")) + val overrides = strings(state.optJSONArray("pendingOverrides")) + state.put("active", JSONArray(active)).put("pending", JSONArray()) + state.put("activeOverrides", JSONArray(overrides)).put("pendingOverrides", JSONArray()) + save(context, state) + // Anything else there was left by a session that never reached endSession (the app died). + bin(context).listFiles() + ?.filter { it.name !in overrides }?.forEach { it.delete() } + val lines = strings(state.optJSONArray("persistent")) + active + val withPath = if (overrides.isEmpty()) lines else lines + "PATH=$GUEST_BIN:${pathOf(lines)}" + if (withPath.isNotEmpty()) Log.i(TAG, "agent environment keys for this session: ${withPath.map { it.substringBefore('=') }}") + withPath + } + + fun revoke(context: Context) = synchronized(lock) { + val state = load(context) + for (key in listOf("pending", "active", "persistent", "pendingOverrides", "activeOverrides")) state.put(key, JSONArray()) + save(context, state) + bin(context).listFiles()?.forEach { it.delete() } + } + + /** At session end: this session's lines and override binaries go. */ + fun endSession(context: Context) = synchronized(lock) { + val state = load(context) + val overrides = strings(state.optJSONArray("activeOverrides")) + val pending = strings(state.optJSONArray("pendingOverrides")) + val bin = bin(context) + overrides.filter { it !in pending }.forEach { File(bin, it).delete() } + state.put("active", JSONArray()).put("activeOverrides", JSONArray()) + save(context, state) + } + + /** A PATH= line among [lines] (the user's or an agent's) is extended rather than replaced. */ + private fun pathOf(lines: List) = + lines.lastOrNull { it.startsWith("PATH=") }?.substringAfter('=')?.takeIf { it.isNotBlank() } ?: BASE_PATH + + private fun bin(context: Context): File { + val root = LinuxRuntime.rootDir(context).canonicalFile.toPath() + val directory = root.resolve(GUEST_BIN.removePrefix("/")) + var path = directory + while (path != root) { + if (Files.isSymbolicLink(path)) throw AgentException("OVERRIDE_FAILED", "The override directory must not contain symbolic links") + path = path.parent + } + return directory.toFile() + } + + private fun merge(existing: List, added: List): List { + val keys = added.map { it.substringBefore('=') }.toSet() + return existing.filter { it.substringBefore('=') !in keys } + added + } + + private fun strings(array: JSONArray?): List = + if (array == null) emptyList() else List(array.length()) { array.optString(it) }.filter { it.isNotEmpty() } + + private fun load(context: Context): JSONObject { + val file = File(context.filesDir, FILE) + val state = try { if (file.isFile) JSONObject(file.readText()) else JSONObject() } catch (e: Exception) { JSONObject() } + for (key in listOf("pending", "active", "persistent", "pendingOverrides", "activeOverrides")) { + if (!state.has(key)) state.put(key, JSONArray()) + } + return state + } + + private fun save(context: Context, state: JSONObject) { + val file = File(context.filesDir, FILE) + val staged = File(context.filesDir, "$FILE.tmp") + staged.writeText(state.toString()) + if (!staged.renameTo(file)) { + staged.delete() + throw AgentException("ENV_WRITE_FAILED", "Could not save the debugging environment") + } + } +} + +/** Typed writes to the app's SharedPreferences, for driving a settings matrix without the UI. */ +object AgentPrefs { + fun set(context: Context, file: String, key: String, type: String, value: String?): JSONObject { + AgentAccess.requireCommands(context) + validateFile(file) + if (file == AgentAccess.PROTECTED_PREFS) throw AgentException("PROTECTED_PREFS", "The agent preferences are set on the device only") + if (file.isBlank() || key.isBlank()) throw AgentException("INVALID_PREF", "file and key are required") + val prefs = context.getSharedPreferences(file, Context.MODE_PRIVATE) + val previous = prefs.all[key] + val editor = prefs.edit() + try { + when (type) { + "remove" -> editor.remove(key) + "bool" -> editor.putBoolean(key, value?.toBooleanStrictOrNull() ?: throw NumberFormatException("not true/false")) + "int" -> editor.putInt(key, value!!.toInt()) + "long" -> editor.putLong(key, value!!.toLong()) + "float" -> editor.putFloat(key, value!!.toFloat()) + "string" -> editor.putString(key, value ?: "") + else -> throw AgentException("INVALID_PREF", "type must be bool, int, long, float, string or remove") + } + } catch (e: RuntimeException) { + if (e is AgentException) throw e + throw AgentException("INVALID_PREF", "The value is not a $type") + } + if (!editor.commit()) throw AgentException("PREF_WRITE_FAILED", "Could not write $file/$key") + return JSONObject().put("file", file).put("key", key).put("type", type) + .put("previous", previous ?: JSONObject.NULL).put("value", prefs.all[key] ?: JSONObject.NULL) + } + + fun get(context: Context, file: String): JSONObject { + AgentAccess.requireCommands(context) + validateFile(file) + val all = context.getSharedPreferences(file, Context.MODE_PRIVATE).all + return JSONObject().put("file", file).put("values", JSONObject(all.mapValues { it.value ?: JSONObject.NULL })) + } + + private fun validateFile(file: String) { + if (!Regex("[A-Za-z0-9_-][A-Za-z0-9._-]{0,127}").matches(file)) { + throw AgentException("INVALID_PREF", "Preference files must be simple names") + } + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/agent/AgentGuest.kt b/app/src/main/java/com/droiddeck/launcher/agent/AgentGuest.kt new file mode 100644 index 0000000000..fbf3f84e9e --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/agent/AgentGuest.kt @@ -0,0 +1,128 @@ +package com.droiddeck.launcher.agent + +import android.content.Context +import android.os.FileObserver +import android.util.Log +import com.droiddeck.launcher.runtime.LinuxRuntime +import com.droiddeck.launcher.session.SessionEvents +import com.droiddeck.launcher.session.SessionState +import org.json.JSONObject +import java.io.File +import java.util.UUID + +/** + * The app's side of the guest agent (tools/linuxfs/overlay/usr/local/bin/droiddeck-agent): requests + * go in as files under the session root's agent directory and answers come back the same way, so + * whatever runs does so inside the live session with its own environment. + */ +object AgentGuest { + private const val TAG = "AgentGuest" + private const val POLL_MS = 50L + private val lock = Any() + private var observer: FileObserver? = null + private var lastFocusKey: String? = null + + fun dir(context: Context) = File(LinuxRuntime.sessionRoot(context), "agent") + + /** + * Before each session: nothing of the last one's daemon, answers or focus may be read as + * current. Then watch for the new daemon's hello and focus snapshots, which go into the + * session's events.jsonl. + */ + fun reset(context: Context) { + synchronized(lock) { + observer?.stopWatching() + observer = null + lastFocusKey = null + val dir = dir(context) + dir.deleteRecursively() + dir.mkdirs() + @Suppress("DEPRECATION") + observer = object : FileObserver(dir.path, MOVED_TO or CLOSE_WRITE) { + override fun onEvent(event: Int, path: String?) { + when (path) { + "hello.json" -> readJson(File(dir, path))?.let { + SessionEvents.record("agent.ready", mapOf("pid" to it.optInt("pid"), "focus" to it.optBoolean("focus"))) + } + "focus.json" -> readJson(File(dir, path))?.let(::recordFocus) + } + } + }.also { it.startWatching() } + } + } + + /** At the end of a session: no more events for a folder that is being closed. */ + fun stop() { + synchronized(lock) { + observer?.stopWatching() + observer = null + } + } + + private fun recordFocus(snapshot: JSONObject) { + val fields = linkedMapOf( + "focusedApp" to snapshot.opt("focusedApp"), + "focusableApps" to snapshot.optJSONArray("focusableApps")?.toString(), + "baselayerAppIds" to snapshot.optJSONArray("baselayerAppIds")?.toString(), + ) + val key = fields.values.joinToString("|") + synchronized(lock) { + if (key == lastFocusKey) return + lastFocusKey = key + } + SessionEvents.record("focus.changed", fields + ("seq" to snapshot.optInt("seq"))) + } + + /** The daemon's hello, when it is running in the current session. */ + fun hello(context: Context): JSONObject? { + if (!SessionState.running) return null + val hello = readJson(File(dir(context), "hello.json")) ?: return null + val pid = hello.optInt("pid", -1) + return hello.takeIf { pid > 1 && File("/proc/$pid").exists() } + } + + /** The newest focus snapshot the daemon published, when a session is running. */ + fun focus(context: Context): JSONObject? = + if (SessionState.running) readJson(File(dir(context), "focus.json")) else null + + /** Send one request and wait for its answer. Throws [AgentException] with a stable code. */ + fun call(context: Context, request: JSONObject, timeoutMs: Long): JSONObject { + if (request.optString("kind") in setOf("exec", "cdp")) AgentAccess.requireCommands(context) + if (!SessionState.running) throw AgentException("NO_ACTIVE_SESSION", "There is no running session") + hello(context) ?: throw AgentException("AGENT_UNAVAILABLE", "The guest agent is not running in this session (yet)") + val dir = dir(context) + val requests = File(dir, "req").apply { mkdirs() } + val answers = File(dir, "resp").apply { mkdirs() } + val id = UUID.randomUUID().toString() + val staged = File(requests, "$id.json.tmp") + val published = File(requests, "$id.json") + val answer = File(answers, "$id.json") + staged.writeText(request.toString()) + if (!staged.renameTo(published)) { + staged.delete() + throw AgentException("AGENT_UNAVAILABLE", "Could not hand the request to the guest agent") + } + val deadline = System.currentTimeMillis() + timeoutMs + while (System.currentTimeMillis() < deadline) { + if (answer.isFile) { + if (answer.length() > 1024 * 1024) { + answer.delete() + throw AgentException("RESPONSE_TOO_LARGE", "The guest response exceeds 1 MiB") + } + val result = readJson(answer) + answer.delete() + return result ?: throw AgentException("INVALID_AGENT_RESPONSE", "The guest agent's answer was not JSON") + } + Thread.sleep(POLL_MS) + } + published.delete() + Log.w(TAG, "no answer to ${request.optString("kind")} in ${timeoutMs}ms") + throw AgentException("TIMEOUT", "The guest agent did not answer within ${timeoutMs / 1000}s") + } + + private fun readJson(file: File): JSONObject? = try { + if (file.isFile && file.length() <= 1024 * 1024) JSONObject(file.readText()) else null + } catch (e: Exception) { + null + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/agent/AgentInput.kt b/app/src/main/java/com/droiddeck/launcher/agent/AgentInput.kt new file mode 100644 index 0000000000..e2c8674b4c --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/agent/AgentInput.kt @@ -0,0 +1,221 @@ +package com.droiddeck.launcher.agent + +import com.droiddeck.launcher.input.PadState +import com.droiddeck.launcher.session.SessionState +import com.droiddeck.launcher.wayland.WaylandCompositor +import org.json.JSONObject + +/** + * Input into the running session the way a player gives it: keys and touches through the + * compositor (what the session screen sends), and the virtual pad through the same PadBridge the + * on-screen controls drive, so games that ignore touch still see a controller. + */ +object AgentInput { + /** Out of the way of real fingers, which Android numbers from 0. */ + private const val TOUCH_ID = 9 + private const val TOUCH_SCALE_X = 1919f + private const val TOUCH_SCALE_Y = 1079f + + fun handle(request: JSONObject): JSONObject { + if (!SessionState.running || !SessionState.firstFrameSeen) { + throw AgentException("SESSION_NOT_READY", "Input needs a running session that has drawn its first frame") + } + return when (val type = request.optString("type")) { + "key" -> key(request) + "text" -> text(request) + "tap" -> tap(request) + "swipe" -> swipe(request) + "pad" -> pad(request) + "stick" -> stick(request) + "trigger" -> trigger(request) + else -> throw AgentException("INVALID_INPUT", "Unknown input type '$type'") + }.put("ok", true).put("command", "input").put("type", request.optString("type")) + } + + private fun key(request: JSONObject): JSONObject { + val name = request.optString("key") + val code = KEYS[name.uppercase()] ?: name.toIntOrNull() + ?: throw AgentException("INVALID_INPUT", "Unknown key '$name' (a name such as ENTER or an evdev code)") + val modifiers = request.optJSONArray("modifiers")?.let { array -> + List(array.length()) { i -> + KEYS[array.optString(i).uppercase()] ?: throw AgentException("INVALID_INPUT", "Unknown modifier '${array.optString(i)}'") + } + }.orEmpty() + when (val action = request.optString("action", "tap")) { + "down" -> WaylandCompositor.nativeSendKey(code, 1) + "up" -> WaylandCompositor.nativeSendKey(code, 0) + "tap" -> { + modifiers.forEach { WaylandCompositor.nativeSendKey(it, 1) } + WaylandCompositor.nativeSendKey(code, 1) + hold(request, 40) + WaylandCompositor.nativeSendKey(code, 0) + modifiers.asReversed().forEach { WaylandCompositor.nativeSendKey(it, 0) } + } + else -> throw AgentException("INVALID_INPUT", "Key action '$action' is not tap, down or up") + } + return JSONObject().put("code", code) + } + + /** Typed as key presses (US layout), which reaches X programs that have no text-input support. */ + private fun text(request: JSONObject): JSONObject { + val text = request.optString("text") + if (request.optBoolean("commit")) { + WaylandCompositor.textInputCommit(text) + return JSONObject().put("length", text.length).put("via", "text-input") + } + val missing = text.filter { TYPED[it] == null } + if (missing.isNotEmpty()) throw AgentException("INVALID_INPUT", "Cannot type '${missing.toSet().joinToString("")}' as keys; use commit") + val delay = request.optLong("delayMs", 15) + for (ch in text) { + val (code, shift) = TYPED.getValue(ch) + if (shift) WaylandCompositor.nativeSendKey(KEYS.getValue("LEFTSHIFT"), 1) + WaylandCompositor.nativeSendKey(code, 1) + WaylandCompositor.nativeSendKey(code, 0) + if (shift) WaylandCompositor.nativeSendKey(KEYS.getValue("LEFTSHIFT"), 0) + Thread.sleep(delay) + } + return JSONObject().put("length", text.length).put("via", "keys") + } + + private fun tap(request: JSONObject): JSONObject { + val (x, y) = point(request, "x", "y") + WaylandCompositor.nativeSendTouch(0, TOUCH_ID, x, y) + hold(request, 60) + WaylandCompositor.nativeSendTouch(2, TOUCH_ID, x, y) + return JSONObject().put("touch", org.json.JSONArray().put(x).put(y)) + } + + private fun swipe(request: JSONObject): JSONObject { + val (x1, y1) = point(request, "x", "y") + val (x2, y2) = point(request, "x2", "y2") + val duration = request.optLong("durationMs", 300).coerceIn(16, 10_000) + val steps = (duration / 16).toInt().coerceAtLeast(2) + WaylandCompositor.nativeSendTouch(0, TOUCH_ID, x1, y1) + for (i in 1..steps) { + Thread.sleep(duration / steps) + WaylandCompositor.nativeSendTouch(1, TOUCH_ID, x1 + (x2 - x1) * i / steps, y1 + (y2 - y1) * i / steps) + } + WaylandCompositor.nativeSendTouch(2, TOUCH_ID, x2, y2) + return JSONObject().put("from", org.json.JSONArray().put(x1).put(y1)).put("to", org.json.JSONArray().put(x2).put(y2)) + } + + /** + * A point in the compositor's touch space (0..1919 x 0..1079 whatever the session's size), from + * the session's output pixels (space "output", the default: what the guest draws at) or 0..1 + * fractions (space "fraction"). + */ + private fun point(request: JSONObject, xKey: String, yKey: String): Pair { + if (!request.has(xKey) || !request.has(yKey)) throw AgentException("INVALID_INPUT", "$xKey and $yKey are required") + val x = request.getDouble(xKey) + val y = request.getDouble(yKey) + val (fx, fy) = when (val space = request.optString("space", "output")) { + "fraction" -> x to y + "output" -> { + val (w, h) = SessionState.outputSize + x / (w - 1).coerceAtLeast(1) to y / (h - 1).coerceAtLeast(1) + } + else -> throw AgentException("INVALID_INPUT", "space '$space' is not output or fraction") + } + return (fx.coerceIn(0.0, 1.0) * TOUCH_SCALE_X).toInt() to (fy.coerceIn(0.0, 1.0) * TOUCH_SCALE_Y).toInt() + } + + private fun pad(request: JSONObject): JSONObject { + val bridge = SessionState.padBridge ?: throw AgentException("NO_PAD", "The session has no virtual pad") + val name = request.optString("button").lowercase() + if (name == "qam") { + bridge.triggerQam() + return JSONObject().put("button", name) + } + val button = BUTTONS[name] + if (button == null && name !in DPAD) throw AgentException("INVALID_INPUT", "Unknown pad button '$name'") + val apply = { s: PadState, down: Boolean -> + when (name) { + "up" -> s.up = down + "down" -> s.down = down + "left" -> s.left = down + "right" -> s.right = down + else -> s.press(button!!, down) + } + } + when (val action = request.optString("action", "tap")) { + "down" -> bridge.applyTouch { apply(it, true) } + "up" -> bridge.applyTouch { apply(it, false) } + "tap" -> { + bridge.applyTouch { apply(it, true) } + hold(request, 120) + bridge.applyTouch { apply(it, false) } + } + else -> throw AgentException("INVALID_INPUT", "Pad action '$action' is not tap, down or up") + } + return JSONObject().put("button", name) + } + + private fun stick(request: JSONObject): JSONObject { + val bridge = SessionState.padBridge ?: throw AgentException("NO_PAD", "The session has no virtual pad") + val right = request.optString("stick", "left") == "right" + val x = request.optDouble("x", 0.0).toFloat().coerceIn(-1f, 1f) + val y = request.optDouble("y", 0.0).toFloat().coerceIn(-1f, 1f) + bridge.applyTouch { if (right) { it.rightX = x; it.rightY = y } else { it.leftX = x; it.leftY = y } } + hold(request, 300) + bridge.applyTouch { if (right) { it.rightX = 0f; it.rightY = 0f } else { it.leftX = 0f; it.leftY = 0f } } + return JSONObject().put("stick", if (right) "right" else "left") + } + + private fun trigger(request: JSONObject): JSONObject { + val bridge = SessionState.padBridge ?: throw AgentException("NO_PAD", "The session has no virtual pad") + val right = request.optString("trigger", "right") == "right" + val value = request.optDouble("value", 1.0).toFloat().coerceIn(0f, 1f) + bridge.applyTouch { if (right) it.rightTrigger = value else it.leftTrigger = value } + hold(request, 200) + bridge.applyTouch { if (right) it.rightTrigger = 0f else it.leftTrigger = 0f } + return JSONObject().put("trigger", if (right) "right" else "left") + } + + private fun hold(request: JSONObject, defaultMs: Long) = + Thread.sleep(request.optLong("holdMs", defaultMs).coerceIn(0, 10_000)) + + private val DPAD = setOf("up", "down", "left", "right") + + private val BUTTONS = mapOf( + "a" to PadState.A, "b" to PadState.B, "x" to PadState.X, "y" to PadState.Y, + "lb" to PadState.LB, "rb" to PadState.RB, "select" to PadState.SELECT, "back" to PadState.SELECT, + "start" to PadState.START, "menu" to PadState.START, "l3" to PadState.L3, "r3" to PadState.R3, + "guide" to PadState.GUIDE, "steam" to PadState.GUIDE, + ) + + /** Linux evdev codes (input-event-codes.h) by name. */ + private val KEYS: Map = buildMap { + "1234567890".forEachIndexed { i, c -> put(c.toString(), 2 + i) } + "QWERTYUIOP".forEachIndexed { i, c -> put(c.toString(), 16 + i) } + "ASDFGHJKL".forEachIndexed { i, c -> put(c.toString(), 30 + i) } + "ZXCVBNM".forEachIndexed { i, c -> put(c.toString(), 44 + i) } + (1..10).forEach { put("F$it", 58 + it) } + put("F11", 87); put("F12", 88) + putAll(mapOf( + "ESC" to 1, "MINUS" to 12, "EQUAL" to 13, "BACKSPACE" to 14, "TAB" to 15, "LEFTBRACE" to 26, + "RIGHTBRACE" to 27, "ENTER" to 28, "LEFTCTRL" to 29, "SEMICOLON" to 39, "APOSTROPHE" to 40, + "GRAVE" to 41, "LEFTSHIFT" to 42, "BACKSLASH" to 43, "COMMA" to 51, "DOT" to 52, "SLASH" to 53, + "RIGHTSHIFT" to 54, "LEFTALT" to 56, "SPACE" to 57, "CAPSLOCK" to 58, "RIGHTCTRL" to 97, + "RIGHTALT" to 100, "HOME" to 102, "UP" to 103, "PAGEUP" to 104, "LEFT" to 105, "RIGHT" to 106, + "END" to 107, "DOWN" to 108, "PAGEDOWN" to 109, "INSERT" to 110, "DELETE" to 111, "LEFTMETA" to 125, + "CTRL" to 29, "SHIFT" to 42, "ALT" to 56, "META" to 125, "SUPER" to 125, "RETURN" to 28, "ESCAPE" to 1, + )) + } + + /** US-layout characters as (evdev code, shift). */ + private val TYPED: Map> = buildMap { + for (c in 'a'..'z') put(c, KEYS.getValue(c.uppercase()) to false) + for (c in 'A'..'Z') put(c, KEYS.getValue(c.toString()) to true) + for (c in '0'..'9') put(c, KEYS.getValue(c.toString()) to false) + "!@#$%^&*()".forEachIndexed { i, c -> put(c, KEYS.getValue(((i + 1) % 10).toString()) to true) } + putAll(mapOf( + ' ' to (57 to false), '\n' to (28 to false), '\t' to (15 to false), + '-' to (12 to false), '_' to (12 to true), '=' to (13 to false), '+' to (13 to true), + '[' to (26 to false), '{' to (26 to true), ']' to (27 to false), '}' to (27 to true), + ';' to (39 to false), ':' to (39 to true), '\'' to (40 to false), '"' to (40 to true), + '`' to (41 to false), '~' to (41 to true), '\\' to (43 to false), '|' to (43 to true), + ',' to (51 to false), '<' to (51 to true), '.' to (52 to false), '>' to (52 to true), + '/' to (53 to false), '?' to (53 to true), + )) + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/agent/AgentStartActivity.kt b/app/src/main/java/com/droiddeck/launcher/agent/AgentStartActivity.kt index 9e35ca46c9..99a6f784c6 100644 --- a/app/src/main/java/com/droiddeck/launcher/agent/AgentStartActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/agent/AgentStartActivity.kt @@ -38,6 +38,10 @@ class AgentStartActivity : Activity() { Log.e(TAG, "missing agent start request") return null } + if (encoded.length > AgentAccess.MAX_REQUEST_CHARS) { + Log.w(TAG, "agent start request is too large") + return null + } return try { JSONObject(String(Base64.decode(encoded, Base64.DEFAULT), Charsets.UTF_8)) } catch (e: Exception) { @@ -47,6 +51,13 @@ class AgentStartActivity : Activity() { } private fun startSession(request: JSONObject) { + try { + AgentAccess.requireStart(this, request) + } catch (e: AgentException) { + Log.w(TAG, e.code) + finish() + return + } val requestedMode = request.optString("mode") val sessionMode = when (requestedMode) { "steam" -> SessionService.MODE_STEAM diff --git a/app/src/main/java/com/droiddeck/launcher/agent/AgentUi.kt b/app/src/main/java/com/droiddeck/launcher/agent/AgentUi.kt new file mode 100644 index 0000000000..6153da0f30 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/agent/AgentUi.kt @@ -0,0 +1,157 @@ +package com.droiddeck.launcher.agent + +import android.os.Build +import android.os.Handler +import android.os.Looper +import android.view.View +import android.view.ViewGroup +import android.view.inspector.WindowInspector +import androidx.compose.ui.platform.ViewRootForTest +import androidx.compose.ui.semantics.SemanticsActions +import androidx.compose.ui.semantics.SemanticsConfiguration +import androidx.compose.ui.semantics.SemanticsNode +import androidx.compose.ui.semantics.SemanticsProperties +import androidx.compose.ui.semantics.getOrNull +import androidx.compose.ui.text.AnnotatedString +import org.json.JSONArray +import org.json.JSONObject +import java.util.concurrent.CountDownLatch +import java.util.concurrent.TimeUnit +import java.util.concurrent.atomic.AtomicReference + +/** + * The app's own UI as an agent sees it: every Compose node in every window of the process (popups + * and menus included), read from Compose's semantics rather than through UI Automator, which on a + * dual-screen device dumps whichever window has focus - often not this app's. Nodes carry their + * test tag (ui/TestTags.kt), text, content description, role, state and on-screen bounds; click + * and set-text act on a node by tag or text, so a test needs no coordinates. + */ +object AgentUi { + private const val TIMEOUT_MS = 5_000L + /** Setup's Debugging tools toggle: the bridge never acts on its own gate (AgentAccess). */ + private const val GATE_TAG = "setting-agent-commands" + + fun handle(request: JSONObject): JSONObject = when (val op = request.optString("op", "dump")) { + "dump" -> onMain { dump(request.optBoolean("all")) } + "click" -> onMain { act(request) { node -> perform(node, SemanticsActions.OnClick) } } + "set-text" -> onMain { + val text = request.optString("text") + act(request) { node -> + node.config.getOrNull(SemanticsActions.SetText)?.action?.invoke(AnnotatedString(text)) + ?: throw AgentException("NOT_EDITABLE", "That node takes no text") + } + } + else -> throw AgentException("INVALID_REQUEST", "ui op '$op' is not dump, click or set-text") + }.put("ok", true).put("command", "ui") + + private fun roots(): List> { + if (Build.VERSION.SDK_INT < 29) throw AgentException("UNSUPPORTED", "The UI dump needs Android 10 or later") + val found = mutableListOf>() + fun walk(view: View) { + if (view is ViewRootForTest) found += view to view + if (view is ViewGroup) for (i in 0 until view.childCount) walk(view.getChildAt(i)) + } + WindowInspector.getGlobalWindowViews().filter { it.isAttachedToWindow && it.isShown }.forEach(::walk) + return found + } + + private fun dump(all: Boolean): JSONObject { + val windows = JSONArray() + for ((view, root) in roots()) { + val nodes = JSONArray() + val location = IntArray(2).also { view.getLocationOnScreen(it) } + fun visit(node: SemanticsNode) { + val json = describe(node, location) + if (all || json.has("tag") || json.has("text") || json.has("description") || json.optBoolean("clickable")) nodes.put(json) + node.children.forEach(::visit) + } + visit(root.semanticsOwner.rootSemanticsNode) + windows.put(JSONObject() + .put("display", view.display?.displayId ?: JSONObject.NULL) + .put("window", view.rootView.javaClass.simpleName) + .put("nodes", nodes)) + } + return JSONObject().put("windows", windows) + } + + private fun describe(node: SemanticsNode, origin: IntArray): JSONObject { + val config = node.config + val json = JSONObject().put("id", node.id) + config.getOrNull(SemanticsProperties.TestTag)?.let { json.put("tag", it) } + val text = textFields(config) + text.keys().forEach { json.put(it, text.get(it)) } + config.getOrNull(SemanticsProperties.Role)?.let { json.put("role", it.toString()) } + config.getOrNull(SemanticsProperties.ToggleableState)?.let { json.put("toggle", it.name) } + config.getOrNull(SemanticsProperties.Selected)?.let { json.put("selected", it) } + config.getOrNull(SemanticsProperties.Focused)?.let { json.put("focused", it) } + if (config.contains(SemanticsProperties.Disabled)) json.put("disabled", true) + if (config.contains(SemanticsActions.OnClick)) json.put("clickable", true) + if (config.contains(SemanticsActions.SetText)) json.put("editable", true) + val b = node.boundsInWindow + json.put("bounds", JSONArray() + .put(origin[0] + b.left.toInt()).put(origin[1] + b.top.toInt()) + .put(origin[0] + b.right.toInt()).put(origin[1] + b.bottom.toInt())) + return json + } + + internal fun textFields(config: SemanticsConfiguration): JSONObject { + val password = config.contains(SemanticsProperties.Password) + val json = JSONObject() + for ((key, value) in listOf( + "text" to config.getOrNull(SemanticsProperties.Text)?.joinToString(" ") { it.text }, + "editableText" to config.getOrNull(SemanticsProperties.EditableText)?.text, + "description" to config.getOrNull(SemanticsProperties.ContentDescription)?.joinToString(" "), + "state" to config.getOrNull(SemanticsProperties.StateDescription), + )) value?.let { json.put(key, if (password) "" else it) } + return json + } + + /** + * The node a request names by "tag" (exact) or "match" (text or description containing it, + * ignoring case); "index" picks among several, in tree order. + */ + private fun act(request: JSONObject, action: (SemanticsNode) -> Unit): JSONObject { + val tag = request.optString("tag").takeIf { it.isNotEmpty() } + val text = if (tag == null) request.optString("match").takeIf { it.isNotEmpty() } else null + if (tag == null && text == null) throw AgentException("INVALID_REQUEST", "Name the node by tag or match") + val matches = mutableListOf>() + for ((view, root) in roots()) { + val origin = IntArray(2).also { view.getLocationOnScreen(it) } + fun visit(node: SemanticsNode) { + val config = node.config + val hit = if (tag != null) config.getOrNull(SemanticsProperties.TestTag) == tag + else (config.getOrNull(SemanticsProperties.Text)?.joinToString(" ") { it.text } + ?: config.getOrNull(SemanticsProperties.ContentDescription)?.joinToString(" ") ?: "") + .contains(text!!, ignoreCase = true) + if (hit) matches += node to origin + node.children.forEach(::visit) + } + visit(root.semanticsOwner.rootSemanticsNode) + } + val index = request.optInt("index", 0) + val (node, origin) = matches.getOrNull(index) + ?: throw AgentException("NO_SUCH_NODE", "No node with ${if (tag != null) "tag '$tag'" else "text '$text'"} (${matches.size} found)") + if (generateSequence(node) { it.parent }.any { it.config.getOrNull(SemanticsProperties.TestTag) == GATE_TAG }) { + throw AgentException("PROTECTED_CONTROL", "Debugging tools are switched on the device only") + } + action(node) + return JSONObject().put("matches", matches.size).put("node", describe(node, origin)) + } + + private fun perform(node: SemanticsNode, key: androidx.compose.ui.semantics.SemanticsPropertyKey Boolean>>) { + val action = node.config.getOrNull(key)?.action ?: throw AgentException("NOT_CLICKABLE", "That node has no click action") + action() + } + + private fun onMain(block: () -> T): T { + if (Looper.myLooper() == Looper.getMainLooper()) return block() + val result = AtomicReference>() + val done = CountDownLatch(1) + Handler(Looper.getMainLooper()).post { + result.set(runCatching(block)) + done.countDown() + } + if (!done.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)) throw AgentException("TIMEOUT", "The UI thread did not answer") + return result.get().getOrThrow() + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/audio/PulseAudioComponent.java b/app/src/main/java/com/droiddeck/launcher/audio/PulseAudioComponent.java index 94bed409f5..9af7ab959f 100644 --- a/app/src/main/java/com/droiddeck/launcher/audio/PulseAudioComponent.java +++ b/app/src/main/java/com/droiddeck/launcher/audio/PulseAudioComponent.java @@ -28,7 +28,9 @@ public class PulseAudioComponent extends SessionPart { /** Where the guest reaches the daemon; the session exports PULSE_SERVER=unix:. */ public static final String SOCKET_NAME = "PS0"; /** Identifies the bundled pulseaudio.tzst; a change here re-unpacks it over what a device has. */ - private static final String BUNDLE_STAMP = "2026-09-23-pa13-suspend-r5"; + private static final String BUNDLE_STAMP = "2026-10-08-pa13-directaudio-native"; + + private final File workingDir; /** Where the daemon's own output is kept for this session, or null for logcat only. */ @@ -40,8 +42,6 @@ public class PulseAudioComponent extends SessionPart { * turns that one stream into a source the client can see, named DirectAudioMic. */ private final String micFifoPath; - /** The DirectAudio relay's socket when the client's output should go through it, else null. */ - private String relaySocketPath; private volatile int pid = -1; public PulseAudioComponent(Context context) { @@ -54,15 +54,6 @@ public class PulseAudioComponent extends SessionPart { this.micFifoPath = micFifoPath; } - /** - * Route the daemon's output through the DirectAudio relay at this socket instead of an AAudio - * stream of its own. The relay owns the stream outside proot, with its adaptive buffer; the - * daemon only fills a shared ring. Set before {@link #start()}; the relay may start later. - */ - public void setRelaySocket(String path) { - this.relaySocketPath = path; - } - /** CRC-32 of the bundled pulseaudio.tzst, or "?" if it cannot be read. About 600 KB; cheap. */ private String bundleChecksum() { java.util.zip.CRC32 crc = new java.util.zip.CRC32(); @@ -106,6 +97,7 @@ public class PulseAudioComponent extends SessionPart { String have = FileUtils.readString(stamp); String want = BUNDLE_STAMP + "-" + bundleChecksum(); if (!new File(modulesDir, "arm64/module-aaudio-sink.so").isFile() + || !new File(modulesDir, "arm64/module-directaudio-native-sink.so").isFile() || !new File(workingDir, "pactl").isFile() || have == null || !want.equals(have.trim())) { Log.i(TAG, "unpacking pulseaudio.tzst (" + want + "; had " + have + ")"); @@ -139,13 +131,15 @@ public class PulseAudioComponent extends SessionPart { // the sink to 0% and the session plays silence. // The sink's name is what the client's Audio settings show as the output device, so it // says which road the sound takes. - if (relaySocketPath != null && !relaySocketPath.isEmpty()) { - config.add("load-module module-directaudio-sink sink_name=DirectAudio socket=\"" + relaySocketPath + "\" performance_mode=1 adaptive=1 volume=1.0"); - config.add("set-default-sink DirectAudio"); - } else { - config.add("load-module module-aaudio-sink sink_name=AAudioSink performance_mode=1 adaptive=1 volume=1.0"); - config.add("set-default-sink AAudioSink"); - } + // The client's sound: DirectAudio's own engine, in this process, one step from Android. + // decay=0: shrinking the device buffer after a quiet spell probes below what works and that + // probe is a click; the client plays menus. Between .nofail and .fail a module that cannot + // open its stream does not take the daemon down; ensureClientSink() then loads the plain + // AAudio sink in its place. + config.add(".nofail"); + config.add("load-module module-directaudio-native-sink sink_name=DirectAudio performance_mode=1 adaptive=1 decay=0 limiter=1 watchdog=1 volume=1.0"); + config.add("set-default-sink DirectAudio"); + config.add(".fail"); if (micFifoPath != null && !micFifoPath.isEmpty()) { // The format is the helper's, fixed at s16le/48000/mono: it resamples when the device // grants another input rate, so the daemon is never told a rate the bytes are not. @@ -177,6 +171,74 @@ public class PulseAudioComponent extends SessionPart { Log.i(TAG, line); if (out != null) synchronized (out) { out.println(line); out.flush(); } }); + Thread guard = new Thread(() -> ensureClientSink(out), "pulse-sink-guard"); + guard.setDaemon(true); + guard.start(); + } + + /** + * The fallback: once the daemon answers, if the DirectAudio sink is not + * there (the module failed to load or to open its stream), load the plain AAudio sink so the + * client is never left without an output. Says so in the audio log either way. + */ + private void ensureClientSink(java.io.PrintWriter out) { + long deadline = System.currentTimeMillis() + 15000L; + String sinks = null; + while (System.currentTimeMillis() < deadline && pid > 1) { + if (socket().exists() && (sinks = pactl("list", "short", "sinks")) != null) break; + try { Thread.sleep(250L); } catch (InterruptedException e) { Thread.currentThread().interrupt(); return; } + } + String line; + if (sinks == null) { + line = "== client sink: the daemon did not answer in 15 s; no fallback attempted"; + } else if (sinks.contains("\tDirectAudio\t") || sinks.startsWith("DirectAudio\t") || sinks.contains("\tDirectAudio\n") || sinks.matches("(?s).*\\bDirectAudio\\b.*")) { + line = "== client sink: DirectAudio (module-directaudio-native-sink)"; + } else { + boolean loaded = pactl("load-module", "module-aaudio-sink", "sink_name=AAudioSink", "performance_mode=1", "adaptive=1", "volume=1.0") != null + && pactl("set-default-sink", "AAudioSink") != null; + line = loaded ? "== client sink: DirectAudio did not come up; FALLBACK to the plain AAudio sink (AAudioSink)" + : "== client sink: DirectAudio did not come up and the AAudio fallback failed too - no client sound"; + Log.w(TAG, line); + } + Log.i(TAG, line); + if (out != null) synchronized (out) { out.println(line); out.flush(); } + } + + /** Runs pactl against this daemon; its stdout, or null when it failed or timed out. */ + private String pactl(String... args) { + File pactl = new File(workingDir, "pactl"); + if (!pactl.isFile() || pid <= 1) return null; + java.lang.Process process = null; + try { + File modules = new File(workingDir, "modules/arm64"); + ArrayList cmd = new ArrayList<>(); + cmd.add(pactl.getAbsolutePath()); + java.util.Collections.addAll(cmd, args); + ProcessBuilder builder = new ProcessBuilder(cmd); + builder.directory(workingDir); + builder.redirectErrorStream(true); + builder.environment().put("LD_LIBRARY_PATH", "/system/lib64:" + modules + ":" + workingDir); + builder.environment().put("HOME", workingDir.getAbsolutePath()); + builder.environment().put("TMPDIR", workingDir.getAbsolutePath()); + builder.environment().put("PULSE_SERVER", "unix:" + socket().getAbsolutePath()); + process = builder.start(); + String output; + try (InputStream in = process.getInputStream()) { + output = new String(in.readAllBytes(), java.nio.charset.StandardCharsets.UTF_8); + } + if (!process.waitFor(5, TimeUnit.SECONDS)) { + process.destroyForcibly(); + return null; + } + return process.exitValue() == 0 ? output : null; + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + return null; + } catch (Exception e) { + return null; + } finally { + if (process != null && process.isAlive()) process.destroyForcibly(); + } } @Override diff --git a/app/src/main/java/com/droiddeck/launcher/core/DeviceReport.kt b/app/src/main/java/com/droiddeck/launcher/core/DeviceReport.kt index d5f9e97961..d23021295d 100644 --- a/app/src/main/java/com/droiddeck/launcher/core/DeviceReport.kt +++ b/app/src/main/java/com/droiddeck/launcher/core/DeviceReport.kt @@ -53,7 +53,7 @@ object DeviceReport { b.append("DroidDeck session report\n") b.append("========================\n") k("Written", SimpleDateFormat("yyyy-MM-dd HH:mm:ss zzz", Locale.US).format(Date())) - k("Session mode", when (mode) { SessionService.MODE_DESKTOP -> "desktop (labwc/LXQt)"; SessionService.MODE_RUN -> "a program under gamescope"; else -> "Steam client (gamescope)" }) + k("Session mode", when (mode) { SessionService.MODE_DESKTOP -> "desktop (KDE Plasma)"; SessionService.MODE_RUN -> "a program under gamescope"; else -> "Steam client (gamescope)" }) h("App") runCatching { @@ -167,7 +167,6 @@ object DeviceReport { k("Guest host name", SessionPrefs.guestHostname(context)) k("DirectAudio for games", SessionPrefs.directAudio(context)) k("Stretch 16:9 to panel", SessionPrefs.stretch16x9(context)) - k("Client audio", if (SessionPrefs.clientDirectAudio(context)) "DirectAudio" else "classic") k("Microphone", SessionPrefs.micEnabled(context)) k("On-screen controls", SessionPrefs.oscMode(context)) k("Touch mode", SessionPrefs.touchMode(context)) @@ -181,10 +180,16 @@ object DeviceReport { h("Android process limits") k("Phantom proc monitor", PhantomProcessLimit.reportValue(PhantomProcessLimit.read(context))) - h("Device switch files in Download") - for (name in listOf("droiddeck-env", "droiddeck-tu-debug", "droiddeck-driver", + h("Device switch files") + SessionService.envSwitchFile(context)?.takeIf { it.isFile }?.let { f -> + k("droiddeck-env", FileUtils.readString(f)?.trim()?.replace('\n', ' ')?.ifEmpty { "(present, empty)" } ?: "(present)") + } + if (File(Environment.getExternalStorageDirectory(), "Download/droiddeck-env").isFile) { + k("Download/droiddeck-env", "(ignored - it moved to Android/data/${context.packageName}/files)") + } + for (name in listOf("droiddeck-tu-debug", "droiddeck-driver", "droiddeck-osc", "droiddeck-no-pad", - "droiddeck-no-hud", "droiddeck-wlr-renderer")) { + "droiddeck-no-hud")) { val f = File(Environment.getExternalStorageDirectory(), "Download/$name") if (f.isFile) k(name, FileUtils.readString(f)?.trim()?.replace('\n', ' ')?.ifEmpty { "(present, empty)" } ?: "(present)") } diff --git a/app/src/main/java/com/droiddeck/launcher/core/DeviceSupport.kt b/app/src/main/java/com/droiddeck/launcher/core/DeviceSupport.kt index 4170d69f17..1af4095be1 100644 --- a/app/src/main/java/com/droiddeck/launcher/core/DeviceSupport.kt +++ b/app/src/main/java/com/droiddeck/launcher/core/DeviceSupport.kt @@ -23,4 +23,8 @@ object DeviceSupport { val soc = if (Build.VERSION.SDK_INT >= 31) Build.SOC_MODEL.takeIf { it.isNotBlank() && it != Build.UNKNOWN } else null return soc?.let { "$it (${Build.HARDWARE})" } ?: Build.HARDWARE.ifBlank { context.getString(R.string.gpu_this_gpu) } } + + /** False on the Odin 3 (CQ8725S): a low-latency Wi-Fi lock makes its WLAN firmware hang, and + * that firmware reboots the whole device instead of restarting Wi-Fi. */ + fun lowLatencyWifiSafe(): Boolean = Build.VERSION.SDK_INT < 31 || Build.SOC_MODEL != "CQ8725S" } diff --git a/app/src/main/java/com/droiddeck/launcher/core/HostProcess.kt b/app/src/main/java/com/droiddeck/launcher/core/HostProcess.kt index 79334af3be..550d55a529 100644 --- a/app/src/main/java/com/droiddeck/launcher/core/HostProcess.kt +++ b/app/src/main/java/com/droiddeck/launcher/core/HostProcess.kt @@ -40,7 +40,7 @@ object HostProcess { if (onLine == null) builder.redirectOutput(File("/dev/null")) val process = builder.start() val pid = pidOf(process) - Log.i(TAG, "started pid $pid: $command") + Log.i(TAG, "started pid $pid: ${argv.first()} (${argv.size - 1} arguments)") if (onLine != null) { Thread({ try { @@ -59,7 +59,7 @@ object HostProcess { } pid } catch (e: Exception) { - Log.e(TAG, "could not start: $command", e) + Log.e(TAG, "could not start: ${argv.first()}", e) -1 } } diff --git a/app/src/main/java/com/droiddeck/launcher/core/LogRedactor.kt b/app/src/main/java/com/droiddeck/launcher/core/LogRedactor.kt index f9c03fefb3..ad48f26641 100644 --- a/app/src/main/java/com/droiddeck/launcher/core/LogRedactor.kt +++ b/app/src/main/java/com/droiddeck/launcher/core/LogRedactor.kt @@ -60,15 +60,39 @@ object LogRedactor { private val IPV4 = Regex("(?]+\"|'[^'\\r\\n<>]+'|[^\\s\"'<>&;,\\[\\]{}]+)" + ) /** * The account a Steam UI login line names ("Login: OnLoginStateChange 2 1 0 0"): a * user who signs in with a plain account name, not an email, had it in every log. The single * space then a non-space keeps "OnLoginStateChange 0 1 0 0" (no account yet) as it is. */ - private val LOGIN_STATE = Regex("(OnLoginStateChange )(\\S+)") + private val LOGIN_STATE = Regex("(?i)(OnLoginStateChange:? )([^\\s<]\\S*)") private val LOGIN_USERS = Regex("(OnLoginUsersChanged )(\\S.*)$") + /** + * An account name as a field: `AccountName`, `account_name`, `username` (`"AccountName" + * "someone"` in a VDF, `account_name=...`, `"username": "..."`) and `login` - the last only + * as `login=`, a quoted key or a VDF pair, so "Login: OnLoginStateChange ..." keeps its label. + * A quoted value is blanked whole; one already blanked is left as it is. + */ + private const val ACCOUNT_VALUE = "(\"[^\"<\\r\\n]+\"|'[^'<\\r\\n]+'|[^\\s\"'<>&;,}]+)" + private val ACCOUNT_KV = Regex( + "(?i)(? = emptyList() @@ -196,18 +220,61 @@ object LogRedactor { * only when that matches, so a folder scrubbed under older rules is scrubbed again on the way * out. */ - const val RULES_VERSION = 1 + const val RULES_VERSION = 3 + + /** + * A private IPv4 address (10/8, 172.16/12, 192.168/16, link-local 169.254/16): the home + * network's layout, e.g. its router as the resolver. Blanked in a shared zip only; public + * server addresses stay. Not part of a longer dotted number (a version like 10.0.19041.1). + */ + private val LAN_IPV4 = Regex( + "(? out.write(redact(line)); out.write("\n") } } + /** + * The last pass for a file going into a shared zip: [redact], then the rules the stores' log + * uses too ([SecretScrub]) - URLs without query, fragment and user:password and with any + * token-bearing path segment blanked, token values and Authorization / Cookie headers blanked + * - and private IPv4 addresses as ``. A line already clean comes out unchanged. + */ + fun redactForShare(line: String): String { + if (line.isEmpty()) return line + return try { + val out = SecretScrub.scrub(redact(line), SecretScrub.Urls.KEEP_PATH, "") + LAN_IPV4.replace(out, "") + } catch (t: Throwable) { + "" + } + } + + /** [src]'s lines through [redactForShare], to [out]: the pass every text file in a shared zip gets. */ + fun scrubForShare(src: java.io.File, out: java.io.Writer) { + src.forEachLine { line -> out.write(redactForShare(line)); out.write("\n") } + } + + /** ``, inside [value]'s quotes when it had them. */ + private fun quoted(value: String): String { + val q = value.first().takeIf { it == '"' || it == '\'' }?.toString().orEmpty() + return "$q$q" + } + /** [line] with every credential shape replaced. Null- and exception-safe by construction. */ fun redact(line: String): String { if (line.isEmpty()) return line return try { var out = line + out = MAC_FIELD.replace(out) { "${it.groupValues[1]}" } + out = MAC.replace(out, "") + out = DEVICE_IDENTIFIER.replace(out) { + val value = it.groupValues[2] + val quote = value.first().takeIf { char -> char == '\"' || char == '\'' }?.toString().orEmpty() + "${it.groupValues[1]}${quote}${quote}" + } out = GUID.replace(out, "") out = JWT_LABELLED.replace(out) { "${it.groupValues[1]}" } out = JWT_BASE64.replace(out, "") @@ -224,6 +291,8 @@ object LogRedactor { for (r in own) out = r.replace(out, "") out = LOGIN_STATE.replace(out) { "${it.groupValues[1]}" } out = LOGIN_USERS.replace(out) { "${it.groupValues[1]}" } + out = ACCOUNT_KV.replace(out) { "${it.groupValues[1]}${it.groupValues[2]}${quoted(it.groupValues[3])}" } + out = LOGIN_KV.replace(out) { "${it.groupValues[1]}${it.groupValues[2]}${quoted(it.groupValues[3])}" } for (r in accounts) out = r.replace(out, "") out = EMAIL.replace(out, "") out = RESIDUAL.replace(out) { "${it.groupValues[1]}=" } diff --git a/app/src/main/java/com/droiddeck/launcher/core/SecretScrub.kt b/app/src/main/java/com/droiddeck/launcher/core/SecretScrub.kt new file mode 100644 index 0000000000..c91ca60ef9 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/core/SecretScrub.kt @@ -0,0 +1,72 @@ +package com.droiddeck.launcher.core + +/** + * The one set of rules that keeps URLs and tokens out of anything the app writes or shares: the + * stores' log ([com.droiddeck.launcher.stores.StoreLog.redactLine]) and the last pass over every + * text file in a shared log zip ([LogRedactor.redactForShare]). + * + * - Every URL loses its query, fragment and `user:password@`. [Urls.SHORT] then keeps only the host + * and a plain first path segment (the stores' lines, where a GOG secure link carries its token in + * the path); [Urls.KEEP_PATH] keeps the path but blanks any segment that carries a token. + * - Outside URLs, the values of token-like keys (`token`, `__token__`, `f_token`, `hdnts`, access / + * refresh / id tokens; exchange and authorization codes of four characters or more, and a bare + * `code=` where it is one: in a query string or on a line with OAuth keys such as `client_id`, + * `redirect_uri` or `state`, never an 8-hex exception code like Proton's `code=c0000005`) and of + * Authorization / Cookie headers are blanked with [mark]. + * + * A line already scrubbed comes out unchanged. + */ +object SecretScrub { + enum class Urls { SHORT, KEEP_PATH } + + private val URL = Regex("""[A-Za-z][A-Za-z0-9+.\-]*://[^\s"'<>]+""") + private val SAFE_SEGMENT = Regex("""[A-Za-z0-9._\-]{1,40}""") + private val TOKEN_SEGMENT = Regex("""(?i)(token|hdnts|hmac|signature|sig|exp)=""") + private val TOKEN = Regex("""(?i)(?}]+""") + /** Codes that are only ever OAuth grants. */ + private val GRANT_CODE = Regex("""(?i)(?}]{4,}""") + /** A bare `code=`: a grant only right after `?` / `&` (a query string) or beside OAuth keys ([OAUTH_CONTEXT]). */ + private val CODE = Regex("""(?i)(?}]{4,})""") + private val OAUTH_CONTEXT = Regex("""(?i)(? if (urls == Urls.SHORT) short(m.value) else keepPath(m.value, mark) } + out = TOKEN.replace(out) { m -> m.groupValues[1] + m.groupValues[2] + mark } + out = GRANT_CODE.replace(out) { m -> m.groupValues[1] + m.groupValues[2] + mark } + val oauth = OAUTH_CONTEXT.containsMatchIn(text) + val src = out + out = CODE.replace(src) { m -> + val inQuery = m.range.first > 0 && src[m.range.first - 1] in "?&" + val grant = !STATUS_CODE.matches(m.groupValues[3]) && (oauth || inQuery) + if (grant) m.groupValues[1] + m.groupValues[2] + mark else m.value + } + out = HEADER.replace(out) { m -> m.groupValues[1] + m.groupValues[2] + mark } + return out + } + + private fun split(url: String): Triple { + val schemeEnd = url.indexOf("://") + val rest = url.substring(schemeEnd + 3).substringBefore('#').substringBefore('?') + val slash = rest.indexOf('/') + val authority = (if (slash >= 0) rest.substring(0, slash) else rest).substringAfterLast('@') + return Triple(url.substring(0, schemeEnd), authority, if (slash >= 0) rest.substring(slash + 1) else null) + } + + private fun short(url: String): String { + val (scheme, authority, path) = split(url) + if (path == null) return "$scheme://$authority" + val first = path.substringBefore('/') + if (first.isEmpty()) return "$scheme://$authority/" + val shown = if (SAFE_SEGMENT.matches(first)) first else "…" + return "$scheme://$authority/$shown" + if (path.length > first.length && shown != "…") "/…" else "" + } + + private fun keepPath(url: String, mark: String): String { + val (scheme, authority, path) = split(url) + val clean = path?.split('/')?.joinToString("/") { seg -> if (TOKEN_SEGMENT.containsMatchIn(seg)) mark else seg } + return "$scheme://$authority" + (clean?.let { "/$it" } ?: "") + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/files/FileManagerScreen.kt b/app/src/main/java/com/droiddeck/launcher/files/FileManagerScreen.kt index 393d953394..81eb00965a 100644 --- a/app/src/main/java/com/droiddeck/launcher/files/FileManagerScreen.kt +++ b/app/src/main/java/com/droiddeck/launcher/files/FileManagerScreen.kt @@ -1031,7 +1031,8 @@ fun FileManagerScreen( // The ROMs folder chosen on the main screen: what the session shows as /root/ROMs. (if (inspecting) "" else SessionPrefs.romsDir(context)).takeIf { it.isNotEmpty() }?.let(::File)?.takeIf { it.isDirectory } ?.let { add(locItem(stringResource(R.string.fm_rail_roms), Icons.Filled.SportsEsports, it)) } - File("/storage/emulated/0/Download/DroidDeck").takeIf { !inspecting && it.isDirectory }?.let { add(locItem(stringResource(R.string.fm_rail_session_logs), Icons.Filled.Description, it)) } + // The app's own logs folder: private, which this file manager (the app itself) can read. + com.droiddeck.launcher.runtime.LinuxRuntime.logDir(context).takeIf { !inspecting && it.isDirectory }?.let { add(locItem(stringResource(R.string.fm_rail_session_logs), Icons.Filled.Description, it)) } File("/storage/emulated/0/Pictures").takeIf { !inspecting && it.isDirectory }?.let { add(locItem(stringResource(R.string.fm_rail_pictures), Icons.Filled.Image, it)) } } val favItems = remember(favTick) { if (inspecting) sample?.entries.orEmpty().filter { it.isDirectory } else FavoritesStore.list(context).map(::File).filter { it.exists() } } diff --git a/app/src/main/java/com/droiddeck/launcher/frontend/AddedExes.kt b/app/src/main/java/com/droiddeck/launcher/frontend/AddedExes.kt new file mode 100644 index 0000000000..62afb0b8af --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/frontend/AddedExes.kt @@ -0,0 +1,48 @@ +package com.droiddeck.launcher.frontend + +import android.content.Context +import org.json.JSONArray +import org.json.JSONObject +import java.io.File + +/** + * Games added from the Games tab's + rather than a Games folder: one picked .exe, or each game + * "Add all games in this folder" found. Kept by the game's folder in `files/added-exes.json`, + * which [AddedGames.scan] merges in. The exe chosen later, the shortcut appid, the edits and the + * removal live where every added game keeps them ([com.droiddeck.launcher.session.SessionPrefs]); + * this holds which folders are games and the exe they came with. + */ +object AddedExes { + /** [picked]: the user chose [exe]; false = [GameExePicker]'s best guess, which a scan ranks again. */ + class Entry(val folder: String, val exe: String, val picked: Boolean = true) + + private fun file(context: Context) = File(context.filesDir, "added-exes.json") + + @Synchronized + fun list(context: Context): List = try { + val array = JSONArray(file(context).takeIf { it.isFile }?.readText() ?: "[]") + (0 until array.length()).mapNotNull { i -> + val o = array.optJSONObject(i) ?: return@mapNotNull null + val folder = o.optString("folder"); val exe = o.optString("exe") + if (folder.isEmpty() || exe.isEmpty()) null else Entry(folder, exe, o.optBoolean("picked", true)) + } + } catch (e: Exception) { + emptyList() + } + + /** Adds [entry], replacing one for the same folder. */ + @Synchronized + fun put(context: Context, entry: Entry) = write(context, list(context).filter { it.folder != entry.folder } + entry) + + @Synchronized + fun drop(context: Context, folder: String) = write(context, list(context).filter { it.folder != folder }) + + private fun write(context: Context, entries: List) { + val array = JSONArray() + entries.forEach { array.put(JSONObject().put("folder", it.folder).put("exe", it.exe).put("picked", it.picked)) } + val f = file(context) + val tmp = File(f.path + ".tmp") + tmp.writeText(array.toString()) + if (!tmp.renameTo(f)) { f.delete(); tmp.renameTo(f) } + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/frontend/AddedGameArt.kt b/app/src/main/java/com/droiddeck/launcher/frontend/AddedGameArt.kt index ecc92a1db6..a29e43679b 100644 --- a/app/src/main/java/com/droiddeck/launcher/frontend/AddedGameArt.kt +++ b/app/src/main/java/com/droiddeck/launcher/frontend/AddedGameArt.kt @@ -12,8 +12,9 @@ import java.net.URL import java.net.URLEncoder /** - * Artwork for an added game: what the user put in the game's folder first, else what Steam's - * store has for the same title, fetched once into the app's cache. Four pieces, named the way + * Artwork for an added game: what the user put in the game's folder first, else what Steam has + * for it - by the Steam appid its files name, else by a store search on its title - else what + * SteamGridDB has ([SteamGridDb]), fetched once into the app's cache. Four pieces, named the way * the client names them in its grid folder: the portrait capsule, the wide header, the hero * background and the logo; plus an icon pulled out of the game's own .exe. The session hands * the paths to the runtime's shortcuts writer, which copies them into the client's grid. @@ -28,13 +29,40 @@ object AddedGameArt { val any get() = portrait != null || header != null || hero != null || logo != null } + /** + * The pieces a user can choose, as the client takes them: [assetType] is its custom-artwork + * number (null: the icon, set by path); [cacheName] the automatic file in the game's cache. + */ + enum class Slot(val key: String, val cacheName: String, val assetType: Int?) { + COVER("p", "p.jpg", 0), BACKGROUND("hero", "hero.jpg", 1), LOGO("logo", "logo.png", 2), ICON("icon", "icon.png", null), + } + + /** Where a piece came from; [AUTO] = nothing found yet. */ + const val FOLDER = "folder" + const val STEAM = "steam" + const val SGDB = "sgdb" + const val FILE = "file" + const val EXE = "exe" + const val AUTO = "" + + /** A slot as it stands: the file shown and where it came from (one of [FOLDER].. [AUTO]); [chosen] = the user picked it. */ + class SlotState(val slot: Slot, val file: File?, val source: String, val chosen: Boolean) + + /** One image the chooser offers: its group ([FOLDER], [STEAM], [SGDB]), a thumbnail and the full image (URL or path). */ + class Option(val group: String, val thumb: String, val full: String) + + private val CHOSEN = Regex("""chosen-(\w+)\.(\w+)""") + + private fun chosen(cache: File, slot: Slot): File? = + cache.listFiles { f -> CHOSEN.matchEntire(f.name)?.groupValues?.get(1) == slot.key }?.firstOrNull() + private val IMAGE = listOf("png", "jpg", "jpeg") private val PORTRAIT = listOf("cover", "poster", "grid", "boxart", "capsule", "folder", "library_600x900", "portrait") private val HEADER = listOf("header", "banner", "library_header", "wide") private val HERO = listOf("hero", "background", "library_hero") private val LOGO = listOf("logo") - private fun cacheDir(context: Context, game: AddedGames.Game) = File(context.filesDir, "added-art/${game.appId}") + internal fun cacheDir(context: Context, game: AddedGames.Game) = File(context.filesDir, "added-art/${game.appId}") /** An image in the folder (or its `art` subfolder) under one of the given names, any image extension. */ private fun find(folder: File, names: List): File? { @@ -58,20 +86,112 @@ object AddedGameArt { ) } - /** The folder's art, else the cached Steam art, without touching the network. */ + /** The user's choice for each piece, else the folder's art, else the cached art; no network. */ fun resolve(context: Context, game: AddedGames.Game): Art { val own = local(game) val cache = cacheDir(context, game) fun cached(name: String) = File(cache, name).takeIf { it.isFile } return Art( - portrait = own.portrait ?: cached("p.jpg"), - header = own.header ?: cached("header.jpg"), - hero = own.hero ?: cached("hero.jpg"), - logo = own.logo ?: cached("logo.png"), - icon = own.icon ?: exeIcon(context, game), + portrait = chosen(cache, Slot.COVER) ?: own.portrait ?: cached("p.jpg") ?: exeArt(context, game, cover = true), + header = own.header ?: cached("header.jpg") ?: exeArt(context, game, cover = false), + hero = chosen(cache, Slot.BACKGROUND) ?: own.hero ?: cached("hero.jpg"), + logo = chosen(cache, Slot.LOGO) ?: own.logo ?: cached("logo.png"), + icon = chosen(cache, Slot.ICON) ?: own.icon ?: cached("icon-sgdb.png") ?: exeIcon(context, game), ) } + /** Each choosable piece with its file and where it came from, for the game's editor; no network. */ + fun describe(context: Context, game: AddedGames.Game): List { + val own = local(game) + val cache = cacheDir(context, game) + fun cached(name: String): Pair? = File(cache, name).takeIf { it.isFile }?.let { f -> + f to (File(cache, "$name.src").takeIf { it.isFile }?.readText()?.trim()?.takeIf { it.isNotEmpty() } ?: STEAM) + } + return Slot.values().map { slot -> + chosen(cache, slot)?.let { return@map SlotState(slot, it, chosenSource(context, game, slot).first.ifEmpty { FILE }, true) } + val auto: Pair? = when (slot) { + Slot.COVER -> own.portrait?.let { it to FOLDER } ?: cached("p.jpg") ?: exeArt(context, game, cover = true)?.let { it to EXE } + Slot.BACKGROUND -> own.hero?.let { it to FOLDER } ?: cached("hero.jpg") + Slot.LOGO -> own.logo?.let { it to FOLDER } ?: cached("logo.png") + Slot.ICON -> own.icon?.let { it to FOLDER } ?: File(cache, "icon-sgdb.png").takeIf { it.isFile }?.let { it to SGDB } + ?: exeIcon(context, game)?.let { it to EXE } + } + SlotState(slot, auto?.first, auto?.second ?: AUTO, false) + } + } + + private val CHOOSABLE = setOf("png", "jpg", "jpeg", "webp") + + /** Images in the game's folder (and its art folders) that could stand for a piece. */ + internal fun folderImages(folder: File): List = + listOf(folder, File(folder, "art"), File(folder, ".art")).flatMap { dir -> + dir.listFiles { f -> f.isFile && f.extension.lowercase() in CHOOSABLE }?.sortedBy { it.name.lowercase() }.orEmpty() + }.take(12) + + /** + * What the chooser offers for [slot]: the game folder's images, Steam's official piece (when the + * game has a Steam appid and Steam has the piece), and SteamGridDB's best few (with a key). + * Blocking: it asks the network. + */ + fun options(context: Context, game: AddedGames.Game, slot: Slot): List