diff --git a/.coderabbit.yaml b/.coderabbit.yaml index ef66d13d74..c83ae0e95d 100644 --- a/.coderabbit.yaml +++ b/.coderabbit.yaml @@ -18,7 +18,7 @@ reviews: enabled: true auto_incremental_review: true auto_pause_after_reviewed_commits: 0 - drafts: false + drafts: true pre_merge_checks: docstrings: mode: "off" diff --git a/.gitattributes b/.gitattributes index 940dbb5f13..019aa1e8f9 100644 --- a/.gitattributes +++ b/.gitattributes @@ -10,3 +10,5 @@ tools/mangoapp/mangoapp text eol=lf app/src/main/assets/desktop-removal/*.json text eol=lf app/src/main/assets/desktop-removal/*.txt text eol=lf *.pdf binary + +app/src/main/resources/ea/graphql/*.gql -text diff --git a/.github/trusted-test-builds.txt b/.github/trusted-test-builds.txt index 8f7b0148c5..fa4cff1104 100644 --- a/.github/trusted-test-builds.txt +++ b/.github/trusted-test-builds.txt @@ -1,5 +1,7 @@ ASEbrahim FX192 K11MCH1 +KeegantheVeganat0r kingokksa ovurrsl +KeegantheVeganat0r diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index fcb30d8ac1..555d8b673c 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -136,11 +136,18 @@ jobs: if-no-files-found: error retention-days: 14 - components: - name: Prepare verified components + components-runner: needs: source if: ${{ github.event_name != 'pull_request' || !github.event.pull_request.draft }} - runs-on: ubuntu-24.04 + uses: ./.github/workflows/pick-runner.yml + secrets: + RUNNER_STATUS_TOKEN: ${{ secrets.RUNNER_STATUS_TOKEN }} + + components: + name: Prepare verified components + needs: [source, components-runner] + if: ${{ github.event_name != 'pull_request' || !github.event.pull_request.draft }} + runs-on: ${{ fromJSON(needs.components-runner.outputs.labels) }} timeout-minutes: 40 steps: - uses: actions/checkout@v4 @@ -188,6 +195,7 @@ jobs: !app/src/main/assets/linuxfs/i386/libfaultreport.so !app/src/main/assets/linuxfs/libblaudit.so !app/src/main/assets/linuxfs/libblfastpath.so + !app/src/main/assets/linuxfs/libddnatv-hook.so !app/src/main/assets/linuxfs/libblsession.so !app/src/main/assets/linuxfs/libfakeinput.so !app/src/main/assets/linuxfs/libssbs.so @@ -306,6 +314,11 @@ jobs: - name: Build the DiRT 3 / DiRT Showdown ws2_32 stand-in run: python3 tools/components/prepare.py dirt-ws2 + - name: Cache graphics driver inputs + uses: actions/cache@v4 + with: + path: app/build/quest-inputs + key: quest-inputs-${{ hashFiles('tools/quest/build-driver.py') }} - name: Prepare graphics driver variants run: python3 tools/quest/build-driver.py "$ANDROID_HOME/ndk/$NDK_VERSION" - name: Record and pack prepared components @@ -318,11 +331,18 @@ jobs: compression-level: 0 retention-days: 14 + build-runner: + needs: [components] + if: ${{ github.event_name != 'pull_request' || !github.event.pull_request.draft }} + uses: ./.github/workflows/pick-runner.yml + secrets: + RUNNER_STATUS_TOKEN: ${{ secrets.RUNNER_STATUS_TOKEN }} + build: - needs: [source, components, xbox] + needs: [source, components, xbox, build-runner] name: Compile APK and Android tests if: ${{ github.event_name != 'pull_request' || !github.event.pull_request.draft }} - runs-on: ubuntu-24.04 + runs-on: ${{ fromJSON(needs.build-runner.outputs.labels) }} timeout-minutes: 45 outputs: variants: ${{ steps.variants.outputs.list }} @@ -536,6 +556,12 @@ jobs: SOURCE_TREE: ${{ needs.source.outputs.tree-sha }} run: python3 tools/components/prepare.py unpack --tree "$SOURCE_TREE" + - name: Ubisoft launcher service (x86_64-pc-windows-gnu) + run: | + set -euo pipefail + command -v x86_64-w64-mingw32-gcc >/dev/null || { sudo apt-get update -qq && sudo apt-get install -y -qq gcc-mingw-w64-x86-64; } + bash tools/ubisoft-launcher/build.sh + - uses: actions/download-artifact@v4 with: name: xbox-arm64 @@ -544,9 +570,11 @@ jobs: - name: Cache checksum-pinned pack inputs uses: actions/cache@v4 with: - path: ~/.cache/droiddeck-pack-inputs - key: pack-inputs-v1-${{ hashFiles('tools/packs/build.sh', 'tools/packs/fetch-verified.sh', 'tools/packs/tracking/release.env') }} - restore-keys: pack-inputs-v1- + path: | + ~/.cache/droiddeck-pack-inputs + ~/.cache/droiddeck-build + key: pack-inputs-v2-${{ hashFiles('tools/packs/build.sh', 'tools/packs/fetch-verified.sh', 'tools/packs/tracking/release.env', 'tools/powervr/release.json') }} + restore-keys: pack-inputs-v2- - name: Build the add-on packs run: | python3 tools/powervr/fetch.py @@ -770,6 +798,9 @@ jobs: set -o pipefail cd app/src/main/rust/stores cargo test --locked 2>&1 | tail -60 + - name: Ubisoft launcher service host unit tests + timeout-minutes: 10 + run: cargo test --locked --manifest-path tools/ubisoft-launcher/Cargo.toml python-tests: name: Python and native regression tests @@ -862,6 +893,17 @@ jobs: variants: ${{ needs.build.outputs.variants }} secrets: inherit + scan: + needs: sign + if: github.event_name != 'pull_request' && github.ref == 'refs/heads/main' + permissions: + contents: read + uses: ./.github/workflows/scan.yml + with: + artifact: droiddeck-signed + variant: standard + secrets: inherit + packs: needs: [build, checks] if: github.event_name != 'pull_request' && github.ref == 'refs/heads/main' diff --git a/.github/workflows/pick-runner.yml b/.github/workflows/pick-runner.yml new file mode 100644 index 0000000000..cb2a0b0ffd --- /dev/null +++ b/.github/workflows/pick-runner.yml @@ -0,0 +1,60 @@ +name: Pick runner + +on: + workflow_call: + outputs: + labels: + value: ${{ jobs.pick.outputs.labels }} + secrets: + RUNNER_STATUS_TOKEN: + required: false + +permissions: {} + +jobs: + pick: + name: Pick runner + runs-on: ubuntu-24.04 + timeout-minutes: 2 + outputs: + labels: ${{ steps.runner.outputs.labels || steps.fork.outputs.labels || '"ubuntu-24.04"' }} + steps: + - name: Pick the compile runner + id: runner + if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository + continue-on-error: true + env: + GH_TOKEN: ${{ secrets.RUNNER_STATUS_TOKEN }} + run: | + set -euo pipefail + labels='"ubuntu-24.04"' + if [ -n "$GH_TOKEN" ]; then + idle=$(gh api "orgs/${GITHUB_REPOSITORY_OWNER}/actions/runners" --jq '[.runners[] | select(.status == "online" and .busy == false and any(.labels[]; .name == "droiddeck-ws"))] | length' 2>/dev/null || echo 0) + if [ "${idle:-0}" -gt 0 ]; then + labels='["self-hosted","droiddeck-ws"]' + fi + fi + echo "labels=$labels" >> "$GITHUB_OUTPUT" + echo "Compile runner: \`$labels\`" >> "$GITHUB_STEP_SUMMARY" + - name: Pick the fork runner + id: fork + if: github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name != github.repository + continue-on-error: true + env: + GH_TOKEN: ${{ github.token }} + run: | + set -euo pipefail + labels='"ubuntu-24.04"' + gist=https://api.github.com/gists/0c7ed7ad6d6493de84f5123689c64ae6 + body=$(curl -fsS --max-time 15 -H "Authorization: Bearer $GH_TOKEN" "$gist" 2>/dev/null || curl -fsS --max-time 15 "$gist" 2>/dev/null || echo '{}') + IDLE_AT=$(jq -r 'select(.owner.login == "maxjivi05") | .files["idle-at"].content // "0"' <<< "$body" 2>/dev/null || echo 0) + echo "Heartbeat: ${IDLE_AT:-none}" + age=-1 + if [[ "${IDLE_AT:-0}" =~ ^[0-9]+$ ]] && [ "${IDLE_AT:-0}" -gt 0 ]; then + age=$(( $(date +%s) - IDLE_AT )) + fi + if [ "$age" -ge 0 ] && [ "$age" -lt 180 ]; then + labels='"droiddeck-fork"' + fi + echo "labels=$labels" >> "$GITHUB_OUTPUT" + echo "Compile runner: \`$labels\`" >> "$GITHUB_STEP_SUMMARY" diff --git a/.github/workflows/publish-packs.yml b/.github/workflows/publish-packs.yml index ca61616d18..53e26f4ed8 100644 --- a/.github/workflows/publish-packs.yml +++ b/.github/workflows/publish-packs.yml @@ -55,7 +55,7 @@ jobs: jq -r --arg n "$2" '.assets // [] | .[] | select(.name == $n) | .digest // empty' } commit=$(gh api "repos/$GITHUB_REPOSITORY/actions/runs/$BUILD_RUN" --jq .head_sha 2>/dev/null || echo "$GITHUB_SHA") - for name in proot directaudio tracking xbox comet butler powervr; do + for name in proot directaudio tracking xbox comet butler powervr ubisoft; do file="packs/$name.tar.zst" [ -f "$file" ] || { echo "::error::$name.tar.zst is missing from Build APK run $BUILD_RUN"; exit 1; } sha=$(sha256sum "$file" | cut -d' ' -f1) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index a151592681..eabc77a358 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -63,6 +63,15 @@ jobs: artifact: droiddeck-release secrets: inherit + scan: + needs: sign + permissions: + contents: read + uses: ./.github/workflows/scan.yml + with: + artifact: droiddeck-release + secrets: inherit + packs: needs: find permissions: diff --git a/.github/workflows/scan.yml b/.github/workflows/scan.yml new file mode 100644 index 0000000000..9790f40c7b --- /dev/null +++ b/.github/workflows/scan.yml @@ -0,0 +1,49 @@ +name: Scan APKs + +on: + workflow_call: + inputs: + artifact: + description: Prefix of the signed artifacts to scan, as passed to sign.yml + type: string + required: true + variant: + description: Variant to scan, as listed in tools/release/variants.txt; every variant when omitted + type: string + default: '*' + fail_on_detection: + type: boolean + default: false + +permissions: + contents: read + +jobs: + virustotal: + name: VirusTotal + runs-on: ubuntu-24.04 + timeout-minutes: 90 + steps: + - uses: actions/checkout@v4 + with: + persist-credentials: false + sparse-checkout: tools/release + + - uses: actions/download-artifact@v4 + with: + pattern: ${{ inputs.artifact }}-${{ inputs.variant }} + merge-multiple: true + path: scan + + - name: Scan + continue-on-error: ${{ !inputs.fail_on_detection }} + env: + VT_API_KEY: ${{ secrets.VIRUSTOTAL_API_KEY }} + FAIL_ON_DETECTION: ${{ inputs.fail_on_detection && '1' || '0' }} + run: | + set -euo pipefail + if [ -z "$VT_API_KEY" ]; then + echo "::warning title=VirusTotal::no VIRUSTOTAL_API_KEY secret; skipping the scan" + exit 0 + fi + bash tools/release/virustotal-scan.sh scan/*.apk diff --git a/README.md b/README.md index e9f172bfa1..73c18762b1 100644 --- a/README.md +++ b/README.md @@ -7,10 +7,10 @@ DroidDeck brings the SteamOS experience to Android: Steam Gaming Mode, full Linux desktop, integration with GOG, Epic Games, Xbox, and other storefronts, Steam VR, and more! -

100% open source. No ads. No telemetry. No source withheld.

+

100% open source. No ads. No telemetry. No proprietary components. No source withheld.

- Download DroidDeck 0.6.9 + Download DroidDeck 0.7.1 Join the DroidDeck Discord

@@ -36,9 +36,9 @@ DroidDeck brings the SteamOS experience to Android: Steam Gaming Mode, full Linu ## Install -1. Android 9 or newer on Adreno 730 or newer, or any 8xx. Adreno 6xx is experimental. Mali, Xclipse, PowerVR and Adreno 710 are unsupported. No root needed. +1. Android 9 or newer on Adreno 730 or newer, or any 8xx. Adreno 6xx, Mali, Xclipse and PowerVR are experimental. Adreno 710 is currently unsupported. No root needed. 2. About 3 GB free for the runtime, plus 1.1 GB for the desktop and emulators. -3. Install the [APK](https://github.com/Droid-Deck/DroidDeck/releases/download/0.6.9/DroidDeck-0.6.9.apk), install the Linux runtime, press **Play** and sign in to Steam. +3. Install the [APK](https://github.com/Droid-Deck/DroidDeck/releases/download/0.7.1/DroidDeck-0.7.1.apk), install the Linux runtime, press **Play** and sign in to Steam. 4. On Android 14 and newer, turn on **Disable child process restrictions** in Developer options. On Android 12 and 13, Steam's first launch shows a **Fix it for me** button that walks you through it. Smart glasses setup is in the [support matrix](docs/smart-glasses-support.md). Release notes are in [docs/releases](docs/releases). @@ -79,7 +79,8 @@ DroidDeck is [GPL-3.0](LICENSE). Steam and Proton belong to Valve Corporation. V - droiddeck-ntsync follows the [Linux](https://github.com/torvalds/linux) ntsync semantics in `Documentation/userspace-api/ntsync.rst` (GPL-2.0). - The proot fast path orders bindings like [PRoot](https://github.com/proot-me/proot)'s `path/binding.c` (GPL-2.0-or-later). - DiRT 3 and DiRT Showdown keep their telemetry socket the way RoccoC's DiRTTelemetryErrorFix (MIT) does, based on the fix by Carsten Gottschling. -- Games use [Mesa](https://gitlab.freedesktop.org/mesa/mesa) Turnip's per render pass memory choice. The Steam client keeps `TU_DEBUG=sysmem`. +- Zink on PowerVR keeps colour textures in colour with Icenowy Zheng's upstream [Mesa](https://gitlab.freedesktop.org/mesa/mesa) fix d55418dae7 (MIT), carried as a runtime patch. +- Games and AppImage, Flatpak and Snap programs such as emulators use [Mesa](https://gitlab.freedesktop.org/mesa/mesa) Turnip's per render pass memory choice. The Steam client and Chromium-based apps keep `TU_DEBUG=sysmem`. - Black Ops III and Dark Souls Remastered start without [umu-protonfixes](https://github.com/Open-Wine-Components/umu-protonfixes) (BSD-2-Clause), whose `311210.py` and `570940.py` fail under ARM64 Proton. - [ReSkate](https://github.com/Dingo-Shenanigans/ReSkate) (GPL-3.0) by Dingo Shenanigans uses the launch option from its README and D3D12 feature level 12_1. - The arm64 client's sign in was checked by reading Steam's saved token with the `ConnectCache` format from [deadlock-api-ingest](https://github.com/deadlock-api/deadlock-api-ingest). @@ -92,8 +93,10 @@ DroidDeck is [GPL-3.0](LICENSE). Steam and Proton belong to Valve Corporation. V - Epic Games and GOG build on [Phobos](https://github.com/phobos665)'s adaptation of [Legendary](https://github.com/legendary-gl/legendary) and [gogdl](https://github.com/Heroic-Games-Launcher/heroic-gogdl). GOG install script registry entries are read the way [Heroic Games Launcher](https://github.com/Heroic-Games-Launcher/HeroicGamesLauncher) (GPL-3.0) reads them. - Epic's official Linux launcher preview runs through FEX. That setup is outside Epic's listed Linux support. - Battle.net is by [@kayakyakr](https://github.com/kayakyakr). It runs Blizzard's own client in Proton (downloaded from Blizzard, not included) and shows key art from the [Battle.net Shop](https://shop.battle.net). Without Steam, its games see the pad as an Xbox 360 controller via `PROTON_SPOOF_STEAMINPUT_VIDPID` in [Proton](https://github.com/ValveSoftware/wine)'s `dlls/winebus.sys`. +- The EA store follows the EA app's protocols as documented by [ArmchairDevelopers](https://github.com/ArmchairDevelopers)' [Maxima](https://github.com/ArmchairDevelopers/Maxima) (GPL-3.0): sign-in, the GraphQL queries (included verbatim, since EA matches them by hash), build archives, online activation licences and the LSX game endpoint. The code is DroidDeck's own. - Battle.net installs drive Blizzard's Agent the way [@barncastle](https://github.com/barncastle)'s [Battle.Net-Installer](https://github.com/barncastle/Battle.Net-Installer) does, with requests from [wowdev.wiki](https://wowdev.wiki/Agent). Read without taking code: [@mubaraknumann](https://github.com/mubaraknumann)'s [unifideck](https://github.com/mubaraknumann/unifideck), [@djmittens](https://github.com/djmittens)'s [neo-mittens](https://github.com/djmittens/neo-mittens) `bnetswitch`, [@BCD1210](https://github.com/BCD1210)'s [soju](https://github.com/BCD1210/soju) and [Lutris](https://github.com/lutris/lutris)'s `product.db` reader. - World of Warcraft addon imports replace an addon's folder from a temporary copy, as [WowUp](https://github.com/WowUp/WowUp) does, and read flavour folders the way [WoWDeck](https://github.com/sigboe/wowdeck) does. No code from either is used. +- The World of Warcraft addon Download window searches [WoWInterface](https://www.wowinterface.com) through its MMOUI API. [CurseForge](https://www.curseforge.com/wow) opens in the user's browser, with no API and no key. - Xbox games load [@domsvoboda](https://github.com/domsvoboda)'s [xodus-nexus](https://github.com/domsvoboda/xodus-nexus) modules (LGPL-2.1-or-later), including `wintypes.dll`. Minecraft's GameInput setup was checked against [@Wyze3306](https://github.com/Wyze3306)'s [BedrockOnLinux](https://github.com/Wyze3306/BedrockOnLinux) (MIT). - The Xbox Cloud Gaming player uses the WebRTC client of [@unknownskl](https://github.com/unknownskl)'s [Greenlight](https://github.com/unknownskl/greenlight) (MIT), and its offer tweaks and sharpening follow [@redphx](https://github.com/redphx)'s [Better xCloud](https://github.com/redphx/better-xcloud) (MIT). See `tools/xcloud-player/NOTICE.md`. - The itch.io butler pack ships butler's `7z.so`, itch.io's build of [7-Zip](https://www.7-zip.org) by Igor Pavlov (LGPL-2.1, unRAR restriction on RAR code), unmodified. Other archives use Apache Commons Compress and the runtime's bsdtar (libarchive). diff --git a/app/build.gradle b/app/build.gradle index 743ad2a847..28bf184e32 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -93,7 +93,7 @@ def trackingPack = rootProject.file('tools/packs/tracking/release.env').readLine def prootPackPin = rootProject.file('tools/packs/proot/release.env').readLines() .findAll { it.contains('=') && !it.startsWith('#') } .collectEntries { def kv = it.split('=', 2); [(kv[0].trim()): kv[1].trim()] } -def addonPacks = ['proot', 'directaudio', 'tracking', 'xbox', 'comet', 'butler', 'powervr'].collectEntries { name -> +def addonPacks = ['proot', 'directaudio', 'tracking', 'xbox', 'comet', 'butler', 'powervr', 'ubisoft'].collectEntries { name -> def pack = file("build/packs/${name}.tar.zst") if (!pack.isFile()) { if (ciBuild) throw new GradleException("Add-on pack ${pack} is missing: run tools/packs/build.sh app/build/packs") @@ -131,8 +131,8 @@ android { applicationId "com.droiddeck.launcher" minSdkVersion 26 targetSdkVersion 28 - versionCode 17 - versionName "0.6.9" + versionCode 19 + versionName "0.7.1" buildConfigField "String", "BUILD_LABEL", "\"${escapedBuildLabel}\"" buildConfigField "String", "SOURCE_COMMIT", "\"${sourceCommit}\"" buildConfigField "int", "PR_NUMBER", "${prNumber}" @@ -205,7 +205,7 @@ android { } androidResources { - ignoreAssetsPattern '!.svn:!.git:!.ds_store:!*.scc:.*:_*:!CVS:!thumbs.db:!picasa.ini:!*~:droiddeck-xbox:droiddeck-comet:directaudio:hybris' + ignoreAssetsPattern '!.svn:!.git:!.ds_store:!*.scc:.*:_*:!CVS:!thumbs.db:!picasa.ini:!*~:droiddeck-xbox:droiddeck-comet:droiddeck-ubisoft:directaudio:hybris' } packagingOptions { diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index a187dc15b0..514b4d9146 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -91,14 +91,6 @@ - - - - - @@ -195,7 +187,6 @@ - - @@ -360,6 +345,14 @@ + + + + + #include #include +#include #include #include #include @@ -208,23 +209,33 @@ struct FakeConfig { bool uinput_enabled; }; +// The first allocation here can start the host program's own allocator, which may call a hooked +// function (mimalloc probes NUMA nodes with access()) and land back in config(). A guarded static +// would throw a recursive-init error there, so the fields are filled without allocating, and a +// reentry on the initializing thread gets them while other threads wait for the fork handlers. static FakeConfig &config() { - static FakeConfig *cfg = [] { - auto *c = new FakeConfig(); - const char *dir = getenv("FAKE_EVDEV_DIR"); - c->hook_dir = dir ? dir : "/data/data/com.termux/files/home/fake-input"; - c->udev_data_dir = getenv("FAKE_UDEV_DATA_DIR"); - c->vibration_enabled = - getenv("FAKE_EVDEV_VIBRATION") && atoi(getenv("FAKE_EVDEV_VIBRATION")); - const char *identity = getenv("FAKE_EVDEV_IDENTITY"); - c->xbox360_identity = identity && !strcmp(identity, "xbox360"); - c->uinput_enabled = getenv("FAKE_EVDEV_UINPUT") && atoi(getenv("FAKE_EVDEV_UINPUT")); - pthread_atfork(controller_fork_prepare, controller_fork_parent, - controller_fork_child); - Logger::init(); - return c; - }(); - return *cfg; + static FakeConfig cfg; + static std::atomic state{0}; + static __thread bool initializing __attribute__((tls_model("initial-exec"))); + if (state.load(std::memory_order_acquire) == 2 || initializing) return cfg; + int expected = 0; + if (!state.compare_exchange_strong(expected, 1, std::memory_order_acq_rel)) { + while (state.load(std::memory_order_acquire) != 2) sched_yield(); + return cfg; + } + initializing = true; + const char *dir = getenv("FAKE_EVDEV_DIR"); + cfg.hook_dir = dir ? dir : "/data/data/com.termux/files/home/fake-input"; + cfg.udev_data_dir = getenv("FAKE_UDEV_DATA_DIR"); + cfg.vibration_enabled = getenv("FAKE_EVDEV_VIBRATION") && atoi(getenv("FAKE_EVDEV_VIBRATION")); + const char *identity = getenv("FAKE_EVDEV_IDENTITY"); + cfg.xbox360_identity = identity && !strcmp(identity, "xbox360"); + cfg.uinput_enabled = getenv("FAKE_EVDEV_UINPUT") && atoi(getenv("FAKE_EVDEV_UINPUT")); + pthread_atfork(controller_fork_prepare, controller_fork_parent, controller_fork_child); + Logger::init(); + state.store(2, std::memory_order_release); + initializing = false; + return cfg; } static std::recursive_mutex &controller_mutex() { @@ -2311,7 +2322,15 @@ __attribute__((visibility("hidden"))) static bool is_deck_sysfs_path(const char if (!path) return false; if (!strncmp(path, "/sys/devices/droiddeck", 22) || !strncmp(path, "/sys/class/hidraw", 17)) return true; const char *listing = deck_listing_dir(); - return listing && !strncmp(path, listing, strlen(listing)); + if (!listing) return false; + if (!strncmp(path, listing, strlen(listing))) return true; + // Without proot, a descriptor's /proc/self/fd link names the stand-in where it really is (the + // listing's parent holds devices/ and class-hidraw/), not /sys + const char *slash = strrchr(listing, '/'); + if (!slash || slash == listing) return false; + size_t root = static_cast(slash - listing); + return !strncmp(path, listing, root) && path[root] == '/' && + (!strncmp(path + root, "/devices", 8) || !strncmp(path + root, "/class-hidraw", 13)); } __attribute__((visibility("hidden"))) static bool is_deck_sysfs_fd(int fd) { @@ -2953,15 +2972,19 @@ EXPORT DIR *opendir(const char *name) { return dir; } +static bool open_has_mode(int flags) { + return (flags & O_CREAT) != 0 || (flags & O_TMPFILE) == O_TMPFILE; +} + EXPORT int open(const char *pathname, int flags, ...) { va_list va; - mode_t mode; + mode_t mode = 0; int fd; bool hasMode; va_start(va, flags); - hasMode = flags & O_CREAT; + hasMode = open_has_mode(flags); if (hasMode) { mode = va_arg(va, mode_t); @@ -3033,13 +3056,13 @@ EXPORT int open(const char *pathname, int flags, ...) { EXPORT int openat(int dirfd, const char *pathname, int flags, ...) { va_list va; - mode_t mode; + mode_t mode = 0; int fd; bool hasMode; va_start(va, flags); - hasMode = flags & O_CREAT; + hasMode = open_has_mode(flags); if (hasMode) { mode = va_arg(va, mode_t); @@ -4251,7 +4274,7 @@ EXPORT int select(int nfds, fd_set *readfds, fd_set *writefds, #ifdef __GLIBC__ static mode_t open_mode(int flags, va_list va) { - return (flags & (O_CREAT | O_TMPFILE)) ? va_arg(va, mode_t) : 0; + return open_has_mode(flags) ? va_arg(va, mode_t) : 0; } EXPORT int open64(const char *pathname, int flags, ...) { diff --git a/app/src/main/cpp/waylandcomp/src/vk_present.c b/app/src/main/cpp/waylandcomp/src/vk_present.c index 8d0267cc13..44fea0c768 100644 --- a/app/src/main/cpp/waylandcomp/src/vk_present.c +++ b/app/src/main/cpp/waylandcomp/src/vk_present.c @@ -213,7 +213,7 @@ static struct { } g_map; static pthread_mutex_t g_game_picture_lock = PTHREAD_MUTEX_INITIALIZER; static float g_game_picture_rect[4]; -static int g_game_picture_top; +static int g_game_picture_placed; static int64_t g_swap_retry_at_ns; static int g_swap_fail_logged; @@ -362,7 +362,7 @@ int vkp_completion_unknown(void) { return g_dev_state == -3; } void vk_present_set_scale_mode(int mode, int alignment) { if (mode < VKP_MODE_OFF || mode > VKP_MODE_INTEGER) mode = VKP_MODE_FIT; - if (alignment < VKP_ALIGN_CENTER || alignment > VKP_ALIGN_PICTURE_BOTTOM) alignment = VKP_ALIGN_CENTER; + if (alignment < VKP_ALIGN_CENTER || alignment > VKP_ALIGN_PICTURE_RIGHT) alignment = VKP_ALIGN_CENTER; g_mode = mode; g_align = alignment; } @@ -383,6 +383,8 @@ static const char *align_name(int a) { case VKP_ALIGN_BOTTOM: return "bottom"; case VKP_ALIGN_PICTURE_TOP: return "picture top"; case VKP_ALIGN_PICTURE_BOTTOM: return "picture bottom"; + case VKP_ALIGN_PICTURE_LEFT: return "picture left"; + case VKP_ALIGN_PICTURE_RIGHT: return "picture right"; default: return "center"; } } @@ -420,9 +422,11 @@ static void update_map(int scene_w, int scene_h) { } if (align == VKP_ALIGN_PICTURE_TOP) g_map.off_y = 0; else if (align == VKP_ALIGN_PICTURE_BOTTOM) g_map.off_y = H - scene_h * g_map.ky; + else if (align == VKP_ALIGN_PICTURE_LEFT) g_map.off_x = 0; + else if (align == VKP_ALIGN_PICTURE_RIGHT) g_map.off_x = W - scene_w * g_map.kx; g_map.valid = 1; pthread_mutex_lock(&g_game_picture_lock); - g_game_picture_top = align == VKP_ALIGN_PICTURE_TOP || align == VKP_ALIGN_PICTURE_BOTTOM; + g_game_picture_placed = align == VKP_ALIGN_PICTURE_TOP || align == VKP_ALIGN_PICTURE_BOTTOM || align == VKP_ALIGN_PICTURE_LEFT || align == VKP_ALIGN_PICTURE_RIGHT; g_game_picture_rect[0] = g_map.off_x; g_game_picture_rect[1] = g_map.off_y; g_game_picture_rect[2] = g_map.off_x + scene_w * g_map.kx; @@ -436,7 +440,7 @@ static void update_map(int scene_w, int scene_h) { int vkp_game_picture_rect(float rect[4]) { pthread_mutex_lock(&g_game_picture_lock); - int active = g_game_picture_top; + int active = g_game_picture_placed; if (active) memcpy(rect, g_game_picture_rect, sizeof(g_game_picture_rect)); pthread_mutex_unlock(&g_game_picture_lock); return active; diff --git a/app/src/main/cpp/waylandcomp/src/vk_present.h b/app/src/main/cpp/waylandcomp/src/vk_present.h index 3a76b4c0b6..2a363979e6 100644 --- a/app/src/main/cpp/waylandcomp/src/vk_present.h +++ b/app/src/main/cpp/waylandcomp/src/vk_present.h @@ -15,7 +15,9 @@ int vkp_quest_layer_presentation(void); enum vkp_scale_mode { VKP_MODE_OFF = 0, VKP_MODE_FIT = 1, VKP_MODE_STRETCH = 2, VKP_MODE_FILL = 3, VKP_MODE_INTEGER = 4 }; -enum vkp_align { VKP_ALIGN_CENTER = 0, VKP_ALIGN_TOP = 1, VKP_ALIGN_BOTTOM = 2, VKP_ALIGN_PICTURE_TOP = 3, VKP_ALIGN_PICTURE_BOTTOM = 4 }; +enum vkp_align { VKP_ALIGN_CENTER = 0, VKP_ALIGN_TOP = 1, VKP_ALIGN_BOTTOM = 2, + VKP_ALIGN_PICTURE_TOP = 3, VKP_ALIGN_PICTURE_BOTTOM = 4, + VKP_ALIGN_PICTURE_LEFT = 5, VKP_ALIGN_PICTURE_RIGHT = 6 }; void vk_present_set_scale_mode(int mode, int alignment); int vkp_game_picture_rect(float rect[4]); int vkp_output_to_scene(double ox, double oy, double *sx, double *sy); diff --git a/app/src/main/java/com/droiddeck/launcher/App.kt b/app/src/main/java/com/droiddeck/launcher/App.kt index 5ef39f6b6e..b4c1621a47 100644 --- a/app/src/main/java/com/droiddeck/launcher/App.kt +++ b/app/src/main/java/com/droiddeck/launcher/App.kt @@ -7,8 +7,11 @@ import com.droiddeck.launcher.session.EnhancedSessionLogShare import com.droiddeck.launcher.session.GpuClockPin class App : Application() { - override fun attachBaseContext(base: android.content.Context) = + override fun attachBaseContext(base: android.content.Context) { super.attachBaseContext(com.droiddeck.launcher.core.AppLanguage.wrap(base)) + val process = if (android.os.Build.VERSION.SDK_INT >= 28) Application.getProcessName() else packageName + com.droiddeck.launcher.lab.LabOverlay.load(this, process == packageName) + } override fun onConfigurationChanged(newConfig: android.content.res.Configuration) { super.onConfigurationChanged(newConfig) @@ -39,6 +42,7 @@ class App : Application() { if (process == "$packageName:vr_recovery" || process == "$packageName:rgb_diagnostic" || process == "$packageName:shortcuts_sync" || process == "$packageName:bottom_desktop") return CrashHandler.install(this) + com.droiddeck.launcher.lab.LabNotice.started(this) com.droiddeck.launcher.core.SteamIntegration.syncShortcutsListing(this) com.droiddeck.launcher.session.QuestExperimental.initialize(this) com.droiddeck.launcher.session.SessionPrefs.applyNtsyncDefault(this) @@ -61,6 +65,9 @@ class App : Application() { com.droiddeck.launcher.stores.StoresState.register(com.droiddeck.launcher.stores.xbox.XboxBackend) com.droiddeck.launcher.sync.Syncthing.init(this) com.droiddeck.launcher.stores.StoresState.register(com.droiddeck.launcher.stores.battlenet.BattleNetBackend) + com.droiddeck.launcher.stores.StoresState.register(com.droiddeck.launcher.stores.ubisoft.UbisoftBackend) + com.droiddeck.launcher.stores.ea.EaBackend.app = this + com.droiddeck.launcher.stores.StoresState.register(com.droiddeck.launcher.stores.ea.EaBackend) } private fun prefetchAddonPacks() { @@ -74,6 +81,8 @@ class App : Application() { if (com.droiddeck.launcher.stores.gog.GogComet.enabled(app)) com.droiddeck.launcher.core.AddonPack.COMET.ensure(app) if (com.droiddeck.launcher.stores.StoreAccounts.signedInAs(app, com.droiddeck.launcher.stores.Store.XBOX) != null) com.droiddeck.launcher.core.AddonPack.XBOX.ensure(app) + if (com.droiddeck.launcher.stores.StoreAccounts.signedInAs(app, com.droiddeck.launcher.stores.Store.UBISOFT) != null) + com.droiddeck.launcher.session.SessionFiles.ensureUbisoftService(app) com.droiddeck.launcher.handtracking.HandTracking.load(app) }.onFailure { android.util.Log.w("App", "add-on pack prefetch failed", it) } }, "addon-packs").start() diff --git a/app/src/main/java/com/droiddeck/launcher/ComponentsMenu.kt b/app/src/main/java/com/droiddeck/launcher/ComponentsMenu.kt index db1d3eda5d..e5e74f791f 100644 --- a/app/src/main/java/com/droiddeck/launcher/ComponentsMenu.kt +++ b/app/src/main/java/com/droiddeck/launcher/ComponentsMenu.kt @@ -39,7 +39,7 @@ internal class ComponentsMenu(private val activity: android.app.Activity, privat runCatching { ComponentUpdater.settle(activity) } val modes = ComponentsManager.COMPONENTS.associateWith { SessionPrefs.componentMode(activity, it) } val auto = ComponentsManager.COMPONENTS.associateWith { runCatching { ComponentUpdater.status(activity, it) }.getOrDefault(emptyList()) } - val held = if (runCatching { ComponentsManager.hardwareDxvk(activity) }.getOrDefault(false)) setOf("dxvk") else emptySet() + val held = if (runCatching { ComponentsManager.autoDxvkHeld(activity) }.getOrDefault(false)) setOf("dxvk") else emptySet() ui.post { compModes = modes compAuto = auto diff --git a/app/src/main/java/com/droiddeck/launcher/DriverMenus.kt b/app/src/main/java/com/droiddeck/launcher/DriverMenus.kt index 35a043c1ff..be0af56a06 100644 --- a/app/src/main/java/com/droiddeck/launcher/DriverMenus.kt +++ b/app/src/main/java/com/droiddeck/launcher/DriverMenus.kt @@ -79,10 +79,12 @@ internal class DriverMenus(private val activity: Activity, private val ui: Handl mode = uiMode, display = displaySlot(), runtime = runtimeSlot(), canRestart = !sessionRunning, checkedAt = TurnipReleases.cached(activity)?.checkedAt ?: 0L, - tpUbwcMode = tpUbwcMode.takeIf { gpu.model == 740 }, - tpUbwcRestartRequired = com.droiddeck.launcher.gpu.TpUbwcHint.restartRequired(activity), ) + fun tpUbwcModeForDevice(): String? = tpUbwcMode.takeIf { gpu.model == 740 } + + fun tpUbwcRestartRequired(): Boolean = com.droiddeck.launcher.gpu.TpUbwcHint.restartRequired(activity) + fun chooseTpUbwcMode(mode: String) { if (gpu.model != 740) return SessionPrefs.setTpUbwcMode(activity, mode) @@ -127,8 +129,14 @@ internal class DriverMenus(private val activity: Activity, private val ui: Handl ) } + var restartBlocked: () -> Boolean = { SessionState.running } + + fun loadTpUbwcMode() { + tpUbwcMode = SessionPrefs.tpUbwcMode(activity) + } + fun restartApp() { - if (SessionState.running) return + if (restartBlocked()) return val launch = activity.packageManager.getLaunchIntentForPackage(activity.packageName) ?: return activity.startActivity(android.content.Intent.makeRestartActivityTask(launch.component) .putExtra(com.droiddeck.launcher.stores.download.StoreDownloadService.EXTRA_NAV, "components")) @@ -595,11 +603,19 @@ internal class DriverMenus(private val activity: Activity, private val ui: Handl } fun selectDriver(id: String, linux: Boolean) { + if (!linux && id == TurnipDriver.AUTO) { + DriverBundle.selectDisplay(activity, id) + pairModeOpen = false + lastAutoCheck = 0L + setMode(auto = true) + refreshDrivers() + return + } val bundle = DriverBundle.containing(activity, id, linux) when { bundle != null -> useBundle(bundle) linux -> SessionPrefs.setLinuxDriver(activity, id) - else -> SessionPrefs.setAndroidDriver(activity, id) + else -> DriverBundle.selectDisplay(activity, id) } pairModeOpen = false setMode(auto = false, mix = true) diff --git a/app/src/main/java/com/droiddeck/launcher/FrontEndActivity.kt b/app/src/main/java/com/droiddeck/launcher/FrontEndActivity.kt index dd9c787f78..f429efcbf7 100644 --- a/app/src/main/java/com/droiddeck/launcher/FrontEndActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/FrontEndActivity.kt @@ -23,6 +23,7 @@ import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.activity.result.contract.ActivityResultContracts import androidx.compose.runtime.Composable +import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableIntStateOf import androidx.compose.runtime.mutableStateOf @@ -63,9 +64,7 @@ import com.droiddeck.launcher.ui.ModeSettings import com.droiddeck.launcher.ui.ModeSettingsActions import com.droiddeck.launcher.ui.ConfirmDialog import com.droiddeck.launcher.ui.ControllerActions -import com.droiddeck.launcher.ui.ControllerMappingPage import com.droiddeck.launcher.input.ControllerPrefs -import com.droiddeck.launcher.input.ControllerEditorActivity import com.droiddeck.launcher.ui.FrontEndScreen import com.droiddeck.launcher.ui.FrontEndState import com.droiddeck.launcher.ui.FrontEndActions @@ -186,6 +185,7 @@ class FrontEndActivity : ComponentActivity() { private var gpuClockPin by mutableStateOf(false) private var prootNoSeccomp by mutableStateOf(false) private var prootFastPath by mutableStateOf(true) + private var nativeUserland by mutableStateOf(false) private var cursorComposite by mutableStateOf(false) private var guestHostname by mutableStateOf(SessionPrefs.DEFAULT_GUEST_HOSTNAME) private var phantomWarning by mutableStateOf(null) @@ -202,6 +202,7 @@ class FrontEndActivity : ComponentActivity() { private var downloadPrefix by mutableStateOf("") private var downloadProxy by mutableStateOf("") private var animationsEnabled by mutableStateOf(true) + private var lowStorageWarning by mutableStateOf(true) private var launcherIcon by mutableStateOf(LauncherIcon.DARK) private var gameStoresEnabled by mutableStateOf(true) private var gameStoresSpeedTier by mutableStateOf("fast") @@ -625,6 +626,7 @@ class FrontEndActivity : ComponentActivity() { private var dualScreenMode by mutableStateOf(com.droiddeck.launcher.input.DualScreen.MODE_AUTO) private var dualScreenSessions by mutableStateOf(com.droiddeck.launcher.input.DualScreen.MODE_AUTO) private var dualScreenControls by mutableStateOf(true) + private var androidTextFieldKeyboard by mutableStateOf(false) private var launcherStarted = false private lateinit var displayManager: DisplayManager private val secondScreenDisplayListener = object : DisplayManager.DisplayListener { @@ -873,6 +875,7 @@ class FrontEndActivity : ComponentActivity() { android.widget.Toast.makeText(this, R.string.game_link_failed, android.widget.Toast.LENGTH_LONG).show() return false } + Library.recordPlayed(this, game) startActivity(Intent(this, SessionActivity::class.java).setAction(SessionService.ACTION_RESUME) .putExtra(SessionActivity.EXTRA_STEAMVR, steamVr) .returnTo(returnHome) @@ -888,7 +891,15 @@ class FrontEndActivity : ComponentActivity() { putExtra(SessionService.EXTRA_STEAM_URL, com.droiddeck.launcher.frontend.GameLaunchLink.steamUrl(game.gameIdString)) } - }, steamSession = true) + } + .playing(game), steamSession = true) + } + + private fun Intent.playing(game: Library.SteamGame): Intent = + apply { if (game.library == Library.ADDED) putExtra(SessionActivity.EXTRA_PLAYED_GAME, game.gameIdString) } + + private fun recordPlayed(intent: Intent) { + intent.getStringExtra(SessionActivity.EXTRA_PLAYED_GAME)?.let { Library.recordPlayed(this, it) } } private fun Intent.returnTo(returnHome: Boolean): Intent { @@ -915,14 +926,17 @@ class FrontEndActivity : ComponentActivity() { .putExtra(SessionService.EXTRA_PROGRAM, command.program) .putExtra(SessionService.EXTRA_PROGRAM_ARGS, command.args.toTypedArray()) .putExtra(SessionService.EXTRA_GAME_ID, com.droiddeck.launcher.session.NoSteamLaunch.scope(game)) + .playing(game) when { !SessionState.running -> startSessionOrWait(next) SessionState.stopRequested -> android.widget.Toast.makeText(this, R.string.game_link_session_busy, android.widget.Toast.LENGTH_LONG).show() - command.program == com.droiddeck.launcher.session.NoSteamLaunch.BATTLENET && handToBattleNet(command.args, returnHome) { pendingSwitch = next } -> {} - SessionState.mode == SessionService.MODE_RUN && SessionState.program == command.program && SessionState.programArgs == command.args -> + command.program == com.droiddeck.launcher.session.NoSteamLaunch.BATTLENET && handToBattleNet(command.args, next, returnHome) -> {} + SessionState.mode == SessionService.MODE_RUN && SessionState.program == command.program && SessionState.programArgs == command.args -> { + recordPlayed(next) startActivity(Intent(this, SessionActivity::class.java).setAction(SessionService.ACTION_RESUME) .returnTo(returnHome) .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP)) + } else -> pendingSwitch = next } } @@ -1084,6 +1098,8 @@ class FrontEndActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) + drivers.restartBlocked = ::runtimeChangesBlocked + drivers.loadTpUbwcMode() pendingSessionLogExport = savedInstanceState?.getString(KEY_PENDING_SESSION_LOG_EXPORT)?.let(::File) homeChoiceOpen = savedInstanceState?.getBoolean(STATE_HOME_CHOICE_OPEN) == true gameSpaceOffered = savedInstanceState?.getBoolean(STATE_GAME_SPACE_OFFERED) == true @@ -1110,6 +1126,7 @@ class FrontEndActivity : ComponentActivity() { downloadProxy = DownloadSource.proxyHostPort(this) DownloadSource.probe() animationsEnabled = SessionPrefs.animationsEnabled(this) + lowStorageWarning = SessionPrefs.lowStorageWarning(this) LauncherIcon.repair(this, intent?.component) launcherIcon = LauncherIcon.current(this) gameStoresEnabled = SessionPrefs.gameStoresEnabled(this) @@ -1120,6 +1137,10 @@ class FrontEndActivity : ComponentActivity() { applyLauncherFullscreen() updates.start() setContent { + val prootPack by com.droiddeck.launcher.core.AddonPack.PROOT.state.collectAsState() + androidx.compose.runtime.LaunchedEffect(prootPack) { + if (prootPack == com.droiddeck.launcher.core.AddonPack.State.Ready && installed == null) refresh() + } DroidDeckTheme(theme, appScale = appScale) { com.droiddeck.launcher.ui.FocusGlideHost(androidx.compose.ui.Modifier.fillMaxSize().exposeTestTags()) { if (decky.deckyPendingZip != null) com.droiddeck.launcher.ui.DeckyZipWarningDialog( @@ -1156,6 +1177,7 @@ class FrontEndActivity : ComponentActivity() { vacDiagnostics = vacDiagnostics, agentCommands = agentCommands, graphicsLogs = graphicsLogs, steamGpuDrawing = steamGpuDrawing, bridgeFellBack = bridgeFellBack, bridgeFellBackMemory = bridgeFellBackMemory, clearCachedApps = clearCachedApps, adbPaired = adbPaired, memoryHud = memoryHud, backgroundAppLimit = backgroundAppLimit, + tpUbwcMode = drivers.tpUbwcModeForDevice(), tpUbwcRestartRequired = drivers.tpUbwcRestartRequired(), canRestartApp = !runtimeChangesBlocked(), steamGames = steamGames, steamGamesX64 = steamGamesX64, emulators = emulatorList, running = runningLabel, frameGen = FrameGen.mode(this), lossless = lossless, @@ -1175,6 +1197,7 @@ class FrontEndActivity : ComponentActivity() { dualScreenMode = dualScreenMode, dualScreenSessions = dualScreenSessions, dualScreenControls = dualScreenControls, + androidTextFieldKeyboard = androidTextFieldKeyboard, packages = packageRows, packageCatalogLoading = catalogLoading, packageBusyId = pkgId, @@ -1197,6 +1220,7 @@ class FrontEndActivity : ComponentActivity() { downloadPrefix = downloadPrefix, downloadProxy = downloadProxy, animationsEnabled = animationsEnabled, + lowStorageWarning = lowStorageWarning, launcherIcon = launcherIcon, gameStoresEnabled = gameStoresEnabled, gameStoresSpeedTier = gameStoresSpeedTier, @@ -1400,6 +1424,8 @@ class FrontEndActivity : ComponentActivity() { }, onClearCachedApps = { on -> SessionPrefs.setClearCachedApps(this, on); clearCachedApps = on }, onMemoryHud = { value -> SessionPrefs.setMemoryHud(this, value); memoryHud = value }, + onTpUbwcMode = { drivers.chooseTpUbwcMode(it) }, + onRestartApp = { drivers.restartApp() }, onBackgroundAppLimit = { limit -> SessionPrefs.setBackgroundAppLimit(this, limit); backgroundAppLimit = limit if (limit == com.droiddeck.launcher.core.BackgroundAppLimit.OFF) com.droiddeck.launcher.core.BackgroundAppLimit.restoreAsync(this) @@ -1503,6 +1529,14 @@ class FrontEndActivity : ComponentActivity() { SessionPrefs.setDualScreenSessionControls(this, on) dualScreenControls = on }, + onAndroidTextFieldKeyboard = { on -> + SessionPrefs.setAndroidTextFieldKeyboard(this, on) + androidTextFieldKeyboard = on + }, + onLowStorageWarning = { on -> + SessionPrefs.setLowStorageWarning(this, on) + lowStorageWarning = on + }, onAnimationsEnabled = { on -> SessionPrefs.setAnimationsEnabled(this, on) animationsEnabled = on @@ -1602,23 +1636,7 @@ class FrontEndActivity : ComponentActivity() { }, updates = updates.actions(), controller = ControllerActions( - onOsc = { o -> SessionPrefs.setOscMode(this, o); oscMode = o }, - onTint = { t -> ControllerPrefs.setTint(this, t); refreshController() }, - onOpacity = { o -> ControllerPrefs.setOpacity(this, o); refreshController() }, - onSize = { v -> ControllerPrefs.setSize(this, v); refreshController() }, - onStickClick = { on -> ControllerPrefs.setStickClick(this, on); refreshController() }, - onAdaptiveSticks = { on -> ControllerPrefs.setAdaptiveSticks(this, on); refreshController() }, - onRumble = { on -> ControllerPrefs.setRumble(this, on); refreshController() }, - onDeadZone = { v -> ControllerPrefs.setDeadZone(this, v); refreshController() }, - onSteamButton = { on -> ControllerPrefs.setSteamButton(this, on); refreshController() }, - onQamButton = { on -> ControllerPrefs.setQamButton(this, on); refreshController() }, - onKeyboardButton = { on -> ControllerPrefs.setKeyboardButton(this, on); refreshController() }, - onSelectSteam = { on -> ControllerPrefs.setSelectSteam(this, on); refreshController() }, - onJoyConVr = { on -> ControllerPrefs.setJoyConVr(this, on); refreshController() }, - onEditLayout = { startActivity(Intent(this, ControllerEditorActivity::class.java)) }, - onResetLayout = { ControllerPrefs.resetAllLayouts(this); refreshController() }, onMapping = { showPerformance = false; showComponents = false; showMapping = true }, - onResetAll = { ControllerPrefs.resetAll(this); refreshController() }, ), ), page = page, @@ -1668,6 +1686,8 @@ class FrontEndActivity : ComponentActivity() { lowSpace?.let { free -> com.droiddeck.launcher.ui.LowSpaceDialog( free, + warn = lowStorageWarning, + onWarnChange = { on -> SessionPrefs.setLowStorageWarning(this, on); lowStorageWarning = on }, onFreeUp = { lowSpace = null; com.droiddeck.launcher.storage.StorageState.requestOpen() }, onPlay = { lowSpace = null; com.droiddeck.launcher.storage.StorageState.lowSpaceDismissed = true; startSteamSession() }, onDismiss = { lowSpace = null }, @@ -1763,8 +1783,9 @@ class FrontEndActivity : ComponentActivity() { private fun playSteam() { val storage = com.droiddeck.launcher.storage.StorageState - if (!SessionState.running && !storage.lowSpaceDismissed && storage.lowOnSpace(this)) { - lowSpace = storage.freeBytes(this) + val free = storage.freeBytes(this) + if (storage.shouldWarnLowSpace(SessionPrefs.lowStorageWarning(this), storage.lowSpaceDismissed, SessionState.running, free)) { + lowSpace = free storage.refresh(this) } else startSteamSession() } @@ -2044,6 +2065,7 @@ class FrontEndActivity : ComponentActivity() { dualScreenDevice = com.droiddeck.launcher.input.DualScreen.anyDisplay(displayManager, ownDisplayId) != null dualScreenSessions = SessionPrefs.dualScreenSessionMode(this) dualScreenControls = SessionPrefs.dualScreenSessionControls(this) + androidTextFieldKeyboard = SessionPrefs.androidTextFieldKeyboard(this) val target = com.droiddeck.launcher.input.DualScreen.display(this, displayManager, ownDisplayId) dualDisplayId = target?.displayId ?: -1 syncDualPanel() @@ -2136,13 +2158,7 @@ class FrontEndActivity : ComponentActivity() { @Composable private fun MappingHost() { - val settings = controllerSettings ?: return - ControllerMappingPage( - mapping = settings.mapping, - onPick = { id, target -> ControllerPrefs.setTarget(this, id, target); refreshController() }, - onReset = { ControllerPrefs.resetMapping(this); refreshController() }, - onBack = { showMapping = false }, - ) + com.droiddeck.launcher.ui.InputSettingsPage(onBack = { showMapping = false }) } private fun openComponents(focusContent: Boolean = true, tab: String? = null, gameKey: String? = null) { @@ -2226,7 +2242,6 @@ class FrontEndActivity : ComponentActivity() { onRestoreBundled = { TurnipDriver(this).restoreBundled(); drivers.refreshDrivers() }, onMode = { m -> drivers.chooseMode(m) }, onRestart = { drivers.restartApp() }, - onTpUbwcMode = { drivers.chooseTpUbwcMode(it) }, ), protons = com.droiddeck.launcher.ui.ProtonsState( rows = protons.protonRows, busyId = protons.protonBusyId, stage = protons.protonStage, percent = protons.protonPercent, @@ -2279,14 +2294,14 @@ class FrontEndActivity : ComponentActivity() { when { !SessionState.running -> startSessionOrWait(next) SessionState.stopRequested -> android.widget.Toast.makeText(this, R.string.game_link_session_busy, android.widget.Toast.LENGTH_LONG).show() - handToBattleNet(full) { pendingSwitch = next } -> {} + handToBattleNet(full, next) -> {} else -> pendingSwitch = next } } }, "battlenet-open").start() } - private fun handToBattleNet(args: List, returnHome: Boolean = false, otherwise: () -> Unit): Boolean { + private fun handToBattleNet(args: List, next: Intent, returnHome: Boolean = false): Boolean { if (SessionState.mode != SessionService.MODE_RUN || SessionState.program != com.droiddeck.launcher.session.NoSteamLaunch.BATTLENET) return false val library = args.takeIf { it.firstOrNull() == "--library" }?.getOrNull(1) val sessionLibrary = SessionState.programArgs.takeIf { it.firstOrNull() == "--library" }?.getOrNull(1) @@ -2297,10 +2312,13 @@ class FrontEndActivity : ComponentActivity() { val handed = request.isEmpty() || com.droiddeck.launcher.stores.battlenet.BattleNetClient.request(LinuxRuntime.sessionRoot(app), request) ui.post { if (isDestroyed) return@post - if (!handed) otherwise() - else startActivity(Intent(this, SessionActivity::class.java).setAction(SessionService.ACTION_RESUME) - .returnTo(returnHome) - .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP)) + if (!handed) pendingSwitch = next + else { + recordPlayed(next) + startActivity(Intent(this, SessionActivity::class.java).setAction(SessionService.ACTION_RESUME) + .returnTo(returnHome) + .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP)) + } } }, "battlenet-request").start() return true @@ -2438,6 +2456,7 @@ class FrontEndActivity : ComponentActivity() { SessionPrefs.setUnfoldedControlsWithController(this, on); unfoldedControlsWithController = on }, onOsc = { o -> SessionPrefs.setOscMode(this, o); oscMode = o }, + onInputSettings = { showPerformance = false; showComponents = false; showMapping = true }, onBackHold = { on -> SessionPrefs.setBackHold(this, on); backHold = on }, onBackActionsInverted = { inverted -> SessionPrefs.setBackActionsInverted(this, inverted) @@ -2539,7 +2558,7 @@ class FrontEndActivity : ComponentActivity() { deviceRamMb = (android.app.ActivityManager.MemoryInfo() .also((getSystemService(ACTIVITY_SERVICE) as android.app.ActivityManager)::getMemoryInfo).totalMem / (1024 * 1024)).toInt(), ramCapAvailable = com.droiddeck.launcher.core.ZramSupport.supported(), - prootNoSeccomp = prootNoSeccomp, prootFastPath = prootFastPath, cursorComposite = cursorComposite, + prootNoSeccomp = prootNoSeccomp, prootFastPath = prootFastPath, nativeUserland = nativeUserland, cursorComposite = cursorComposite, guestHostname = guestHostname, phantomWarning = phantomWarning, onClientOverride = { on -> SessionPrefs.setClientCpusOverride(this, on); clientOverride = on }, onTuSysmem = { on -> SessionPrefs.setTuSysmem(this, on); tuSysmem = on }, @@ -2557,6 +2576,7 @@ class FrontEndActivity : ComponentActivity() { onVramCapMb = { mb -> SessionPrefs.setVramCapMb(this, mb); vramCapMb = mb }, onProotNoSeccomp = { on -> SessionPrefs.setProotNoSeccomp(this, on); prootNoSeccomp = on }, onProotFastPath = { on -> SessionPrefs.setProotFastPath(this, on); prootFastPath = on }, + onNativeUserland = { on -> SessionPrefs.setNativeUserland(this, on); nativeUserland = on }, onCursorComposite = { on -> SessionPrefs.setCursorComposite(this, on); cursorComposite = on }, onGuestHostname = { name -> SessionPrefs.setGuestHostname(this, name) }, onClientCore = { core, on -> @@ -2779,6 +2799,7 @@ class FrontEndActivity : ComponentActivity() { gpuClockPin = SessionPrefs.gpuClockPin(this) prootNoSeccomp = SessionPrefs.prootNoSeccomp(this) prootFastPath = SessionPrefs.prootFastPath(this) + nativeUserland = SessionPrefs.nativeUserland(this) cursorComposite = SessionPrefs.cursorComposite(this) guestHostname = SessionPrefs.guestHostname(this) refreshPhantomStatus() diff --git a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt index 99fe2528ff..5f39d03c6c 100644 --- a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt @@ -55,6 +55,7 @@ import com.droiddeck.launcher.stores.StoreGameSidecar import com.droiddeck.launcher.stores.StoreLaunchRequests import com.droiddeck.launcher.stores.epic.EpicClient import com.droiddeck.launcher.ui.SessionAchievements +import com.droiddeck.launcher.ui.screenIsOff import com.droiddeck.launcher.gpu.Cardboard import com.droiddeck.launcher.gpu.FrameGen import com.droiddeck.launcher.gpu.HeadTracker @@ -76,12 +77,23 @@ import com.droiddeck.launcher.input.KeyboardHost import com.droiddeck.launcher.input.ControllerPrefs import com.droiddeck.launcher.input.SessionClipboard import com.droiddeck.launcher.input.OnScreenControls +import com.droiddeck.launcher.input.InputRouter +import com.droiddeck.launcher.input.InputOutput +import com.droiddeck.launcher.input.InputCommand +import com.droiddeck.launcher.input.quickButton +import com.droiddeck.launcher.input.InputProfiles +import com.droiddeck.launcher.input.InputProfile +import com.droiddeck.launcher.input.InputAction +import com.droiddeck.launcher.input.InputRole +import com.droiddeck.launcher.input.InputControl +import com.droiddeck.launcher.input.InputMotion +import com.droiddeck.launcher.input.HeldInput +import com.droiddeck.launcher.input.DeckControls import com.droiddeck.launcher.input.PadBridge import com.droiddeck.launcher.input.PhysicalShortcuts import com.droiddeck.launcher.input.PhysicalShortcutRouter import com.droiddeck.launcher.input.PadMouse import com.droiddeck.launcher.input.PadState -import com.droiddeck.launcher.input.ScreenKeys import com.droiddeck.launcher.input.SteamControllerBle import com.droiddeck.launcher.input.DirectTouchHold import com.droiddeck.launcher.input.GestureTouchStreamGate @@ -109,6 +121,7 @@ import com.droiddeck.launcher.session.SessionArtifacts import com.droiddeck.launcher.session.SessionLogShare import com.droiddeck.launcher.session.SessionPhase import com.droiddeck.launcher.session.SessionPaths +import com.droiddeck.launcher.session.SessionLoadingDecision import com.droiddeck.launcher.wayland.HdrSupport import com.droiddeck.launcher.session.SessionService import com.droiddeck.launcher.session.SessionState @@ -149,6 +162,8 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { private var pipAutoEnter by mutableStateOf(false) private var unfoldedControls by mutableStateOf(false) private var unfoldedControlsWithController by mutableStateOf(false) + private var adaptiveSticks by mutableStateOf(null) + private var quickButtons by mutableStateOf>(emptyMap()) private var picturePosition by mutableStateOf("center") private fun applyGameAlignment() { @@ -158,15 +173,55 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { val alignment = if (!active) ALIGN_CENTER else when (picturePosition) { "top" -> ALIGN_PICTURE_TOP "bottom" -> ALIGN_PICTURE_BOTTOM + "left" -> ALIGN_PICTURE_LEFT + "right" -> ALIGN_PICTURE_RIGHT else -> ALIGN_CENTER } WaylandCompositor.nativeSetScaleMode(SCALE_FIT, alignment) } - private var unfoldedPanel: com.droiddeck.launcher.ui.DeckControlsPanel? = null + private var unfoldedPanel by mutableStateOf(null) + private var unfoldedColumn: android.widget.LinearLayout? = null + private var unfoldedDeckSurface: com.droiddeck.launcher.ui.InputSurfaceView? = null + private var unfoldedDeckPanel by mutableStateOf(null) + private var deckPanelLayouts: Map = emptyMap() private lateinit var sessionRoot: FrameLayout + private var inputRouter: InputRouter? = null + private var physicalInputBindings: com.droiddeck.launcher.input.PhysicalInputBindings? = null + private var inputSettingsOpen by mutableStateOf(false) + @Volatile private var inputUsesLocalActions = false + private var inputUsesPointer = false + private var inputProfile = InputProfile.defaults(InputProfile.GAME) + private val inputMotion = InputMotion { inputRouter } + private val heldKeys = HeldInput { code, down -> WaylandCompositor.nativeSendKey(code, if (down) 1 else 0) } + private val heldButtons = HeldInput(::sendSceneButton) + private val inputOutput = object : InputOutput { + override fun key(code: Int, down: Boolean) { heldKeys.set(4, code, down) } + override fun button(code: Int, down: Boolean) { heldButtons.set(4, code, down) } + override fun pointer(dx: Float, dy: Float) { + if (Looper.myLooper() == Looper.getMainLooper()) moveInputPointer(dx, dy) + else uiHandler.post { + if (resumed && !SessionState.suspended && !pipUi && !drawerOpen && !inputSettingsOpen) + moveInputPointer(dx, dy) + } + } + override fun scroll(steps: Int) { sendSceneWheel(steps) } + override fun scrollHorizontal(steps: Int) { sendSceneWheel(steps, horizontal = true) } + override fun requestMotion() { inputMotion.request() } + override fun command(command: InputCommand) = command(command, InputRole.PHYSICAL) + override fun command(command: InputCommand, role: InputRole) { + uiHandler.post { + if (!resumed || isDestroyed || pipUi || SessionState.suspended) return@post + when (command) { + InputCommand.MENU -> { releasePads(); drawerOpen = true } + InputCommand.KEYBOARD -> if (role == InputRole.PRIMARY || secondScreenPresentation?.showKeyboard() != true) togglePcKeyboard() + InputCommand.STEAM -> tapSteamButton() + InputCommand.QUICK_ACCESS -> tapQam() + } + } + } + } private val guestKeysDown = mutableSetOf() private val guestButtonsDown = mutableMapOf() - private val trackpadButtonsDown = mutableSetOf() private lateinit var surfaceView: SurfaceView private lateinit var sessionOverlay: ComposeView private lateinit var loading: LoadingState @@ -177,8 +232,8 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { private var joyConVr: com.droiddeck.launcher.input.JoyConVr? = null private var handCamera: com.droiddeck.launcher.handtracking.HandCamera? = null private var padMotion: com.droiddeck.launcher.input.PadMotion? = null - private var resumed = false - private val deckPadListener: () -> Unit = { uiHandler.post { updatePadMotion(); openDeckRing("deck pad changed"); applyUnfoldedControls() } } + private var resumed by mutableStateOf(false) + private val deckPadListener: () -> Unit = { uiHandler.post { updatePadMotion(); openDeckRing("deck pad changed"); applyUnfoldedControls(); leaveDeckPanel() } } private fun openDeckRing(why: String) { val deck = SessionState.deckPad @@ -196,8 +251,12 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { } private var steamController: SteamControllerBle? = null private var onScreenControls: OnScreenControls? = null + private var bottomInputControls: OnScreenControls? = null + private var primaryInputLayout: com.droiddeck.launcher.input.SavedInputLayout? = null + private var secondaryInputLayout by mutableStateOf(null) private var steamTouchControls: com.droiddeck.launcher.input.SteamTouchControls? = null private var steamTouchAvailable by mutableStateOf(false) + private var steamTouchInUse by mutableStateOf(false) private val steamTouchRetry = Runnable { updateOnScreenControls() } private var steamTouchOptions: com.droiddeck.launcher.input.SteamTouchConfig.Options? = null private var routedTouchMode = SessionPrefs.TOUCH_AUTO @@ -230,19 +289,11 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { private var directHoldBehavior by mutableStateOf(com.droiddeck.launcher.input.TouchGestureConfig.MOUSE_BEHAVIOR_CLICK) private var relativeMouseMode by mutableIntStateOf(0) @Volatile private var appliedRelativeMouseMode: Int? = null - private var screenKeyPickerOpen by mutableStateOf(false) - private var screenKeyBind: List? = null - private var screenKeyDir: Int? = null - private var screenKeyTextFocus = false - private val appTextFocus get() = screenKeyTextFocus || wowChat.request != null + private val agentReadyListener: () -> Unit = { relativeMouseRetries = 0; requestRelativeMouse() } + private val appTextFocus get() = wowChat.request != null private val wowChat by lazy { com.droiddeck.launcher.ui.WowChat(this, ::sendWowChat, ::cancelWowChat, ::readGameField) } private var wowChatBottom: SecondScreenPresentation? = null private var wowChatOnTop by mutableStateOf(false) - private var screenKeyEdit by mutableStateOf(null) - private var screenLayoutsOpen by mutableStateOf(false) - private var screenKeysShown by mutableStateOf(true) - private var screenLayoutNames by mutableStateOf(listOf()) - private var screenKeys: ScreenKeys? = null private var recognizedGesture by mutableStateOf(null) private var gesturePointerX = 0f private var gesturePointerY = 0f @@ -339,9 +390,10 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { holds = { backHold }, ) private val mouseBackInput = com.droiddeck.launcher.input.MouseBackInput( - sendRight = { down -> sendSceneButton(PointerGestures.BTN_RIGHT, down) }, - mouseAttached = ::mouseAttached, + sendRight = { down -> heldButtons.set(32, PointerGestures.BTN_RIGHT, down) }, + mouseAttached = ::externalMouseAttached, ) + private var lastExternalMouse: InputDevice? = null private var drawerDirectionKey = KeyEvent.KEYCODE_UNKNOWN private var drawerDirectionDownTime = 0L private var drawerDirectionDeviceId = -1 @@ -521,6 +573,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { private var sessionActivityDisplayId = Display.DEFAULT_DISPLAY private lateinit var displayManager: DisplayManager private var secondScreenPresentation: SecondScreenPresentation? = null + private val inputWindowDisplays = mutableListOf() private val bottomScreenListener: () -> Unit = { if (secondScreenPresentation?.display?.displayId?.let(com.droiddeck.launcher.input.DualScreen::taken) == true) closeSecondScreen(reset = false) updateBottomGame() @@ -588,6 +641,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { padBridge?.syncConnectedControllers() refreshControls() updatePointerCapture() + requestRelativeMouse() updateHandCamera() refreshSecondScreenDisplays() } @@ -598,16 +652,19 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { backQamInput.onDeviceRemoved(deviceId) mouseBackInput.onDeviceRemoved(deviceId) joyConVr?.refresh() + physicalInputBindings?.disconnected(deviceId) padBridge?.onDeviceRemoved(deviceId) if (::padMouse.isInitialized) padMouse.reset() refreshControls() updatePointerCapture() + requestRelativeMouse() updateHandCamera() refreshSecondScreenDisplays() } override fun onInputDeviceChanged(deviceId: Int) { joyConVr?.refresh() refreshControls() + requestRelativeMouse() refreshSecondScreenDisplays() } } @@ -616,6 +673,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) + AgentGuest.readyListener = agentReadyListener if (com.droiddeck.launcher.runtime.LinuxRuntimeInstaller.isRemoving() || com.droiddeck.launcher.runtime.LinuxRuntimeInstaller.isMaintaining()) { runtimeRemovalBlocked = true @@ -635,6 +693,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { SessionEvents.begin(this, mode, SessionPaths.label(mode, intent.getStringExtra(SessionService.EXTRA_PROGRAM))) SessionState.secondScreenAutoOpened = false SessionState.secondScreenProvisional = false + SessionState.secondScreenAutomatic = false SessionState.gameOnBottom = false gameOnBottom = false } @@ -692,9 +751,10 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { val bridge = PadBridge(File(LinuxRuntime.sessionRoot(this), "dev/input")) bridge.setStickDeadZone(ControllerPrefs.deadZone(this)) - bridge.setSelectSteam(ControllerPrefs.read(this).selectSteam) bridge.setBuiltinPad(SessionState.mode != SessionService.MODE_STEAM || SessionPrefs.builtinPad(this)) padBridge = bridge + inputRouter = InputRouter(inputOutput).also { bridge.setInputRouter(it); DeckControls.setRouter(it); physicalInputBindings = com.droiddeck.launcher.input.PhysicalInputBindings(it) } + refreshInputProfile() bridge.setOnChordInput(backQamInput::onChordInput) SessionState.padBridge = bridge if (Build.VERSION.SDK_INT >= 31) { @@ -708,20 +768,16 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { joyConVr = vr } SteamControllerBle.listener = { uiHandler.post { updatePadMotion(); refreshControls() } } - steamController = SteamControllerBle(this).also { it.start() } + steamController = SteamControllerBle(this, inputRouter).also { it.start() } 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 } SessionState.deckPadListener = deckPadListener openDeckRing("activity created") - bridge.setOnPlayerInput { - lastPadInputMs = android.os.SystemClock.uptimeMillis() - uiHandler.removeCallbacks(cursorHide) - mouseOwnsCursor = false - cursorVisible = false - } - onScreenControls = OnScreenControls(this, bridge, onKeyboard = ::togglePcKeyboard).also { controls -> + bridge.setOnPlayerInput(::onPadInput) + onScreenControls = OnScreenControls(this, bridge).also { controls -> + controls.updateLayout(primaryInputLayout) controls.onTouchMiss = miss@{ event -> if (!gestureTouchStreamGate.accepts(GestureTouchStreamGate.Source.ON_SCREEN_CONTROLS, event)) return@miss event.offsetLocation(controls.left - surfaceView.left.toFloat(), controls.top - surfaceView.top.toFloat()) @@ -729,20 +785,6 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { } root.addView(controls) } - screenKeysShown = com.droiddeck.launcher.input.ControllerPrefs.screenKeysShown(this) - screenKeys = ScreenKeys(this) { code, down -> if (CompositorHost.isStarted) sendGuestKey(code, down) }.also { keys -> - keys.onTouchMiss = miss@{ event -> - if (!gestureTouchStreamGate.accepts(GestureTouchStreamGate.Source.SCREEN_KEYS, event)) return@miss - event.offsetLocation(keys.left - surfaceView.left.toFloat(), keys.top - surfaceView.top.toFloat()) - onTouchEvent(event) - } - keys.onAdd = { screenKeyPickerOpen = true } - keys.onSelect = { codes -> screenKeyEdit = screenKeys?.stateOf(codes) } - keys.onEditingChanged = { touchEditorChanged(); if (!it) screenKeyEdit = null } - keys.steamStyle = { steamTouchControls?.style } - keys.visibility = if (screenKeysShown) View.VISIBLE else View.GONE - root.addView(keys) - } keyboard = KeyboardHost(this).also { host -> host.onImeChanged = { updateDesktopKeyboardButton() } root.addView(host) @@ -882,6 +924,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { if (pipUi) return when { ::loading.isInitialized && loading.ended -> finish() + inputSettingsOpen -> inputSettingsOpen = false drawerOpen -> drawerOpen = false achievementsOpen -> closeAchievements() pcKeyboardOpen -> pcKeyboardOpen = false @@ -906,7 +949,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { androidx.compose.runtime.DisposableEffect(Unit) { onDispose { com.droiddeck.launcher.glasses.GlassesMenuMirror.setSessionMenuVisible(this@SessionActivity, false) } } - androidx.compose.runtime.LaunchedEffect(drawerOpen, pcKeyboardOpen, achievementsOpen, pipUi, bottomOutputActive) { updatePointerCapture() } + androidx.compose.runtime.LaunchedEffect(drawerOpen, pcKeyboardOpen, achievementsOpen, inputSettingsOpen, pipUi, bottomOutputActive) { updatePointerCapture() } androidx.compose.runtime.LaunchedEffect(bottomOutputActive) { hud.setHidden(pipUi || bottomOutputActive) } androidx.compose.runtime.LaunchedEffect(pipUi) { hud.setHidden(pipUi || bottomOutputActive) @@ -947,7 +990,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { } } else { com.droiddeck.launcher.ui.FocusGlideHost(androidx.compose.ui.Modifier.fillMaxSize()) { - CursorOverlay({ cursorPos }, cursorVisible && !uiPointer && !bottomOutputActive, + CursorOverlay({ cursorPos }, cursorVisible && (!uiPointer || onlyPcKeyboardOpen()) && !bottomOutputActive, resources.displayMetrics.density, cursorImage, cursorHotX, cursorHotY, cursorImageScale) if (memoryHud.text.isNotEmpty()) HudText(memoryHud.text, hudColor, hudOpacity) @@ -1011,15 +1054,28 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { runCatching { com.droiddeck.launcher.session.TelemetryOutput.applyAutomatic(this@SessionActivity, nextGame) } } } - LaunchedEffect(drawerOpen, pcKeyboardOpen, achievementsOpen, gestureSettingsOpen, pipUi, SessionState.suspended) { - if (drawerOpen || pcKeyboardOpen || achievementsOpen || gestureSettingsOpen || pipUi || SessionState.suspended) { + LaunchedEffect(drawerOpen, pcKeyboardOpen, achievementsOpen, gestureSettingsOpen, inputSettingsOpen, pipUi, SessionState.suspended, resumed, appTextFocus) { + updateOnScreenControls() + padBridge?.setInputEnabled(resumed && !drawerOpen && !pcKeyboardOpen && !achievementsOpen && !gestureSettingsOpen && !inputSettingsOpen && !pipUi && !appTextFocus && !SessionState.suspended) + if (drawerOpen || pcKeyboardOpen || achievementsOpen || gestureSettingsOpen || inputSettingsOpen || pipUi || appTextFocus || SessionState.suspended) { backQamInput.cancel() com.droiddeck.launcher.input.HomeButtonShortcuts.cancel(this@SessionActivity) cancelTouchGestures() - screenKeys?.stopEditing() } } com.droiddeck.launcher.ui.GestureFeedback(recognizedGesture) + if (inputSettingsOpen) com.droiddeck.launcher.ui.InputSettingsDialog( + onBack = { inputSettingsOpen = false; secondScreenPresentation?.refreshControls() }, + onChanged = { + controllerSettings = ControllerPrefs.read(this@SessionActivity) + oscMode = SessionPrefs.oscMode(this@SessionActivity) + padBridge?.setStickDeadZone(ControllerPrefs.deadZone(this@SessionActivity)) + joyConVr?.refresh() + onScreenControls?.reload() + refreshInputProfile() + refreshControls() + }, + ) LaunchedEffect(Unit) { val root = LinuxRuntime.rootDir(this@SessionActivity) while (true) { @@ -1052,84 +1108,6 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { onDismiss = { gestureSettingsOpen = false }, onSaved = { gestureSettingsOpen = false; refreshGestureConfig() }, ) - if (screenKeyPickerOpen) { - val dir = screenKeyDir - com.droiddeck.launcher.ui.ScreenKeyPicker( - initial = if (dir != null) { - listOfNotNull((screenKeyEdit?.codes?.getOrNull(dir) ?: 0).takeIf { it != 0 }) - } else { - screenKeyBind ?: emptyList() - }, - onDismiss = { screenKeyPickerOpen = false; screenKeyBind = null; screenKeyDir = null }, - onAdd = { codes -> - val bind = screenKeyBind - if (dir != null && bind != null) { - screenKeys?.setDirection(bind, dir, codes.lastOrNull() ?: 0)?.let { screenKeyEdit = it } - } else if (bind != null) { - screenKeys?.setCodes(bind, codes)?.let { screenKeyEdit = it } - } else { - screenKeys?.add(codes) - screenKeyEdit = screenKeys?.stateOf(codes) - } - screenKeyPickerOpen = false - screenKeyBind = null - screenKeyDir = null - }, - ) - } - screenKeyEdit?.let { key -> - com.droiddeck.launcher.ui.ScreenKeyEditor( - codes = key.codes, keyY = key.y, - size = key.size, opacity = key.opacity, shape = key.shape, toggle = key.toggle, - labels = key.labels, - text = key.text, hasImage = key.image.isNotEmpty(), - onResize = { screenKeys?.resize(key.codes, it); screenKeyEdit = screenKeys?.stateOf(key.codes) }, - onOpacity = { screenKeys?.setOpacity(key.codes, it); screenKeyEdit = screenKeys?.stateOf(key.codes) }, - onShape = { screenKeys?.setShape(key.codes, it); screenKeyEdit = screenKeys?.stateOf(key.codes) }, - onToggle = { on -> screenKeys?.setToggle(key.codes, on); screenKeyEdit = screenKeys?.stateOf(key.codes) }, - onLabels = { on -> screenKeys?.setLabels(key.codes, on); screenKeyEdit = screenKeys?.stateOf(key.codes) }, - onDirection = { index -> screenKeyBind = key.codes; screenKeyDir = index; screenKeyPickerOpen = true }, - onText = { screenKeys?.setText(key.codes, it); screenKeyEdit = screenKeys?.stateOf(key.codes) }, - onImage = { screenKeys?.setImage(key.codes, it); screenKeyEdit = screenKeys?.stateOf(key.codes) }, - onClearImage = { screenKeys?.setImage(key.codes, ""); screenKeyEdit = screenKeys?.stateOf(key.codes) }, - onBind = { screenKeyBind = key.codes; screenKeyDir = null; screenKeyPickerOpen = true }, - onTextFocus = { screenKeyTextFocus = true; keyboard?.hide() }, - onTextBlur = { screenKeyTextFocus = false }, - onRemove = { screenKeys?.remove(key.codes); screenKeyEdit = null }, - onDismiss = { screenKeyEdit = null; screenKeyTextFocus = false; screenKeys?.deselect() }, - ) - } - if (screenLayoutsOpen) com.droiddeck.launcher.ui.ScreenLayoutPanel( - layouts = screenLayoutNames, - onSave = { name -> - screenKeys?.let { keys -> - com.droiddeck.launcher.input.ControllerPrefs.saveScreenLayout(this@SessionActivity, name, keys.export()) - } - screenLayoutNames = layoutNames() - }, - onLoad = { name -> - com.droiddeck.launcher.input.ControllerPrefs.screenLayouts(this@SessionActivity)[name]?.let { screenKeys?.load(it) } - screenLayoutsOpen = false - }, - onDelete = { name -> - com.droiddeck.launcher.input.ControllerPrefs.deleteScreenLayout(this@SessionActivity, name) - screenLayoutNames = layoutNames() - }, - onImport = { text -> - val width = (screenKeys?.width ?: 0).takeIf { it > 0 } ?: resources.displayMetrics.widthPixels - val keys = com.droiddeck.launcher.input.importScreenLayout(text, width, resources.displayMetrics.density) - if (keys != null) { - screenKeys?.load(com.droiddeck.launcher.input.ControllerPrefs.encodeScreenKeys(keys)) - screenLayoutsOpen = false - } - }, - exportText = { - val width = (screenKeys?.width ?: 0).takeIf { it > 0 } ?: resources.displayMetrics.widthPixels - val keys = com.droiddeck.launcher.input.ControllerPrefs.decodeScreenKeys(screenKeys?.export().orEmpty()) - com.droiddeck.launcher.input.exportScreenLayout(keys, width, resources.displayMetrics.density) - }, - onDismiss = { screenLayoutsOpen = false }, - ) androidx.compose.runtime.LaunchedEffect(drawerOpen) { if (drawerOpen) { syncLossless() @@ -1170,7 +1148,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { if (pcKeyboardOpen || drawerOpen || achievementsOpen) padMouse.reset() } if (pcKeyboardOpen) com.droiddeck.launcher.ui.PcKeyboard( - sendKey = { code, down -> if (CompositorHost.isStarted) sendGuestKey(code, down) }, + sendKey = { code, down -> if (CompositorHost.isStarted) sendGuestKey(code, down, 32) }, onAndroidKeyboard = { pcKeyboardOpen = false; keyboard?.toggle() }, onClose = { pcKeyboardOpen = false }, ) @@ -1228,6 +1206,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { SessionDrawer(drawerOpen || (docked && drawerPinned), drawerPage, drawerControllerActive, onPageChange = { drawerPage = it }, docked = docked, backRequest = drawerBackRequests, onLayerChange = { drawerLayerOpen = it }, a = DrawerActions( steam = SessionState.mode == SessionService.MODE_STEAM, + desktop = SessionState.mode == SessionService.MODE_DESKTOP, systemPowerVr = com.droiddeck.launcher.gpu.SystemVulkanDriver.usesDefault( SessionPrefs.linuxDriver(this@SessionActivity)), direct = noSteamSession(), @@ -1256,7 +1235,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { textureLodBias = if (drawerGameProfileId == null) textureLodBias else drawerTextureLodBias, frameGen = frameGen, lossless = lossless, - oscMode = oscMode, onScreenButtonsVisible = onScreenButtonsVisible, suspendPolicy = suspendPolicy, + oscMode = oscMode, suspendPolicy = suspendPolicy, steamDownloadsInBackground = steamDownloadsInBackground, touchMode = touchMode, micAvailable = SessionState.micAvailable && !SessionState.suspended, micEnabled = SessionState.micEnabled, @@ -1329,9 +1308,6 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { onWowChat = wowChat::choose, controller = controllerSettings, onRumble = { on -> updateControllerPrefs { ControllerPrefs.setRumble(this@SessionActivity, on) } }, - onSteamButton = { on -> updateControllerPrefs { ControllerPrefs.setSteamButton(this@SessionActivity, on) } }, - onQamButton = { on -> updateControllerPrefs { ControllerPrefs.setQamButton(this@SessionActivity, on) } }, - onKeyboardButton = { on -> updateControllerPrefs { ControllerPrefs.setKeyboardButton(this@SessionActivity, on) } }, onSteamMenu = if (SessionState.mode == SessionService.MODE_STEAM) ({ sendSteamGuide() }) else null, onQam = if (SessionState.mode == SessionService.MODE_STEAM) ({ triggerSteamQam() }) else null, onAchievements = achievementsInFront?.let { { drawerOpen = false; achievementsOpen = true } }, @@ -1399,6 +1375,8 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { SessionService.suspendPolicyChanged(this@SessionActivity) }, onTouch = { v -> SessionPrefs.setTouchMode(this@SessionActivity, v); readPrefs() }, + onInputSettings = { releasePads(); drawerOpen = false; inputSettingsOpen = true }, + onInputSelection = { releasePads(); refreshInputProfile() }, onGestureSettings = { drawerOpen = false; gestureSettingsOpen = true }, touchActive = drawerTouchMode(), directHoldBehavior = directHoldBehavior, @@ -1414,24 +1392,12 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { relativeMouseMode = mode setRelativeMouse(mode) }, - onScreenKeys = { if (screenKeysShown) { drawerOpen = false; screenKeys?.startEditing() } }, - screenKeysShown = screenKeysShown, - onScreenKeysShown = { shown -> - screenKeysShown = shown - com.droiddeck.launcher.input.ControllerPrefs.setScreenKeysShown(this@SessionActivity, shown) - screenKeys?.stopEditing() - screenKeys?.visibility = if (shown) View.VISIBLE else View.GONE - }, - onScreenLayouts = { - drawerOpen = false - screenLayoutNames = layoutNames() - screenLayoutsOpen = true - }, onFexPreset = { v -> SessionPrefs.setFexPreset(this@SessionActivity, v); readPrefs() }, onSecondScreenMode = { mode -> if (docked) drawerOpen = false typingReturnMode = null - if (mode != SecondScreenMode.NONE) SessionPrefs.setSecondScreenPanel(this@SessionActivity, mode.id) + automaticPanel = false + if (mode != SecondScreenMode.NONE) SessionPrefs.setSecondScreenPanel(this@SessionActivity, desktopSession, mode.id) selectSecondScreenMode(mode) }, onSecondScreenDisplay = ::selectSecondScreenDisplay, @@ -1460,6 +1426,21 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { }, unfoldedControls = unfoldedControls, onUnfoldedControls = ::chooseUnfoldedControls, + adaptiveSticks = adaptiveSticks.takeIf { + unfoldedPanel != null || oscMode != SessionPrefs.OSC_NEVER && oscMode != SessionPrefs.OSC_STEAM_QAM && !steamTouchInUse + }, + onAdaptiveSticks = { on -> + primaryInputLayout?.let { com.droiddeck.launcher.input.InputLibraries.setAdaptiveSticks(this@SessionActivity, it.id, on) } + refreshInputProfile() + }, + quickButtons = quickButtons.takeIf { + unfoldedPanel != null || oscMode != SessionPrefs.OSC_NEVER && !steamTouchInUse + }.orEmpty(), + onQuickButton = { button, on -> + primaryInputLayout?.let { com.droiddeck.launcher.input.InputLibraries.setQuickButton(this@SessionActivity, it.id, button, on) } + refreshInputProfile() + updateOnScreenControls() + }, unfoldedControlsWithController = unfoldedControlsWithController, onUnfoldedControlsWithController = ::chooseUnfoldedControlsWithController, onBackground = { drawerOpen = false; pip.background(); moveTaskToBack(true) }, @@ -1519,10 +1500,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { private fun sendSteamGuide() { drawerOpen = false - padBridge?.applyTouch { state -> state.press(com.droiddeck.launcher.input.PadState.GUIDE, true) } - uiHandler.postDelayed({ - padBridge?.applyTouch { state -> state.press(com.droiddeck.launcher.input.PadState.GUIDE, false) } - }, 90) + padBridge?.triggerSteamMenu() } @androidx.compose.runtime.Composable @@ -1564,12 +1542,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { } private fun switchVrSticks() { - val chord = { down: Boolean -> padBridge?.applyTouch { state -> - state.press(com.droiddeck.launcher.input.PadState.L3, down) - state.press(com.droiddeck.launcher.input.PadState.R3, down) - } } - chord(true) - uiHandler.postDelayed({ chord(false) }, 120) + padBridge?.triggerVrStickSwitch() } private fun triggerSteamQam() { @@ -1602,6 +1575,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { drawerOpen = false keyboard?.hide() onScreenControls?.releaseAll() + bottomInputControls?.releaseAll() pcKeyboardOpen = !pcKeyboardOpen } @@ -1986,7 +1960,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { if (CompositorHost.isStarted) { WaylandCompositor.nativeSendTouch(3, -1, 0, 0) listOf(PointerGestures.BTN_LEFT, PointerGestures.BTN_RIGHT, PointerGestures.BTN_MIDDLE).forEach { - WaylandCompositor.nativeSendSceneInput(3, it, 0) + heldButtons.set(1, it, false) } } if (nextTouchMode == SessionPrefs.TOUCH_OFF || !usingTouchpad()) cursorVisible = false @@ -1995,6 +1969,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { val nextRoutedTouchMode = effectiveTouchMode() if (routedTouchMode != nextRoutedTouchMode) gestureTouchStreamGate.reset() routedTouchMode = nextRoutedTouchMode + requestRelativeMouse() frameGen = FrameGen.mode(this) fexPreset = SessionPrefs.fexPreset(this) suspendPolicy = SessionPrefs.suspendPolicy(this, SessionState.mode) @@ -2265,6 +2240,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { intent.getStringExtra(SessionService.EXTRA_GAME_ID), sessionActivityDisplayId, ) + intent.getStringExtra(EXTRA_PLAYED_GAME)?.let { com.droiddeck.launcher.frontend.Library.recordPlayed(this, it) } } applyFrameGen() applyGameAlignment() @@ -2355,9 +2331,11 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { if (::touchscreenGestures.isInitialized) touchscreenGestures.cancel() if (::touchpad.isInitialized) touchpad.cancel() if (::directTouchHold.isInitialized) directTouchHold.cancel() + if (::twoFingerGestures.isInitialized) twoFingerGestures.cancel() if (::scrollTouch.isInitialized) scrollTouch.cancel() scrollTouchGesture = false gesturePointerReady = false + if (sessionTouchDown) dismissTouch = true } private fun panelBounds(): android.graphics.Rect = if (Build.VERSION.SDK_INT >= 30) { @@ -2461,6 +2439,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { private fun watchSession() { val handler = Handler(Looper.getMainLooper()) var ticks = 0 + val loadingStartedAt = SystemClock.elapsedRealtime() val poll = object : Runnable { override fun run() { if (!watching) return @@ -2473,6 +2452,12 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { finish() return } + if (loading.visible && !loading.ended && !SessionState.running && !installingRuntime && + SessionLoadingDecision.shouldReturnToLauncher( + loadingStartedAt, SystemClock.elapsedRealtime(), SessionState.running, SessionState.phase)) { + finish() + return + } if (loading.visible && !loading.ended) { if (!installingRuntime) loading.update(this@SessionActivity, SessionState.logFile) if (ticks++ % 2 == 0) loading.tick() @@ -2497,8 +2482,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { val hint = sessionExitHint(status) runOnUiThread { if (isFinishing || isDestroyed) return@runOnUiThread - if (status == 0) SessionState.relaunch?.let { next -> - SessionState.relaunch = null + if (status == 0) SessionState.takeRelaunch()?.let { next -> next.putExtra(EXTRA_RETURN_HOME, intent.getBooleanExtra(EXTRA_RETURN_HOME, false)) next.putExtra(EXTRA_LINK_TASK, intent.getIntExtra(EXTRA_LINK_TASK, -1)) setIntent(next) @@ -2532,6 +2516,8 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { } private fun releasePads() { + inputMotion.stop() + physicalInputBindings?.release() com.droiddeck.launcher.input.HomeButtonShortcuts.cancel(this) backQamInput.cancel() padBridge?.releaseAll() @@ -2657,7 +2643,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { SessionState.mode == SessionService.MODE_STEAM && SessionState.running && SessionState.firstFrameSeen && !SessionState.suspended && !ended && !pipUi && !drawerOpen && !pcKeyboardOpen && !achievementsOpen && - !gestureSettingsOpen && !appTextFocus && window.decorView.hasWindowFocus() + !gestureSettingsOpen && !inputSettingsOpen && !appTextFocus && window.decorView.hasWindowFocus() if (steamVolume) { val audio = getSystemService(android.media.AudioManager::class.java) if (com.droiddeck.launcher.input.SteamVolumeKeys.dispatch(event, !audio.isVolumeFixed) { direction -> @@ -2667,14 +2653,55 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { return super.dispatchKeyEvent(event) } } + if (appTextFocus) return super.dispatchKeyEvent(event) + if (!pipUi) { + if (physicalInputBindings?.onKey(event, resumed && !drawerOpen && !pcKeyboardOpen && !achievementsOpen && + !inputSettingsOpen && !gestureSettingsOpen && !SessionState.suspended && !loading.visible) == true) return true + if (physicalShortcuts.onKey( + event, + enabled = resumed && SessionState.running && !SessionState.suspended && + !(drawerOpen && event.keyCode == KeyEvent.KEYCODE_BUTTON_A) && + !(drawerOpen && PhysicalShortcuts.isDrawerNavigationKey(event)) && + (event.deviceId to event.keyCode) !in resumeKeysDown && + !(::loading.isInitialized && (loading.visible || loading.ended)) && + !pcKeyboardOpen && !achievementsOpen && !gestureSettingsOpen && !inputSettingsOpen, + steam = SessionState.mode == SessionService.MODE_STEAM, + ) { action -> + pendingBackAction?.let(uiHandler::removeCallbacks) + pendingBackAction = null + drawerControllerActive = true + Log.i(TAG, "physical shortcut ${action.name} device=${event.deviceId} key=${event.keyCode} scan=${event.scanCode}") + when (action) { + PhysicalShortcuts.Action.STEAM -> sendSteamGuide() + PhysicalShortcuts.Action.QAM -> triggerSteamQam() + PhysicalShortcuts.Action.DROIDDECK -> drawerOpen = !drawerOpen + } + }) return true + } if (event.keyCode == KeyEvent.KEYCODE_BACK && event.action == KeyEvent.ACTION_DOWN && event.repeatCount == 0) { Log.i(TAG, String.format(java.util.Locale.ROOT, "back key device=%d (%s) source=0x%x flags=0x%x", event.deviceId, event.device?.name, event.source, event.flags)) } - val mouseToGuest = !pipUi && !appTextFocus && !drawerOpen && !pcKeyboardOpen && !achievementsOpen && !gestureSettingsOpen && + val mouseToGuest = !pipUi && !appTextFocus && !drawerOpen && !pcKeyboardOpen && !achievementsOpen && !gestureSettingsOpen && !inputSettingsOpen && !SessionState.suspended && !(::loading.isInitialized && (loading.visible || loading.ended)) - if (mouseBackInput.onKeyEvent(event, mouseToGuest)) return true + if (event.keyCode == KeyEvent.KEYCODE_BACK && event.action == KeyEvent.ACTION_DOWN && event.repeatCount == 0 && + com.droiddeck.launcher.input.MouseBackInput.isInjectedBack(event)) lastExternalMouse = null + val mouseBackHandled = mouseBackInput.onKeyEvent(event, mouseToGuest) + if (event.keyCode == KeyEvent.KEYCODE_BACK && event.action == KeyEvent.ACTION_DOWN && event.repeatCount == 0 && + com.droiddeck.launcher.input.MouseBackInput.isInjectedBack(event) && mouseToGuest) { + val device = lastExternalMouse + if (mouseBackHandled && device != null) { + Log.i(TAG, String.format(java.util.Locale.ROOT, + "injected Back right-click mouse=\"%s\" (%04x:%04x, id %d, sources 0x%x, external=%s, virtual=%s)", + device.name, device.vendorId, device.productId, device.id, device.sources, + Build.VERSION.SDK_INT < Build.VERSION_CODES.Q || device.isExternal, device.isVirtual)) + } else if (!mouseBackHandled) { + Log.i(TAG, "injected Back no external mouse found") + } + } + if (mouseBackHandled) return true val backToQam = !pipUi && !appTextFocus && !drawerOpen && !pcKeyboardOpen && !achievementsOpen && + !inputSettingsOpen && !gestureSettingsOpen && SessionState.mode == SessionService.MODE_STEAM && SessionState.running && SessionState.firstFrameSeen && !SessionState.suspended && padBridge?.canHoldQam() == true if (backQamInput.onKeyEvent(event, backToQam)) { @@ -2683,30 +2710,13 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { return true } if (pipUi) return true - if (appTextFocus) return super.dispatchKeyEvent(event) - if (physicalShortcuts.onKey( - event, - enabled = SessionState.running && !SessionState.suspended && - !(drawerOpen && event.keyCode == KeyEvent.KEYCODE_BUTTON_A) && - !(drawerOpen && PhysicalShortcuts.isDrawerNavigationKey(event)) && - (event.deviceId to event.keyCode) !in resumeKeysDown && - !(::loading.isInitialized && (loading.visible || loading.ended)) && - !pcKeyboardOpen && !achievementsOpen && !gestureSettingsOpen, - steam = SessionState.mode == SessionService.MODE_STEAM, - ) { action -> - pendingBackAction?.let(uiHandler::removeCallbacks) - pendingBackAction = null - drawerControllerActive = true - Log.i(TAG, "physical shortcut ${action.name} device=${event.deviceId} key=${event.keyCode} scan=${event.scanCode}") - when (action) { - PhysicalShortcuts.Action.STEAM -> sendSteamGuide() - PhysicalShortcuts.Action.QAM -> triggerSteamQam() - PhysicalShortcuts.Action.DROIDDECK -> drawerOpen = !drawerOpen - } - }) return true val pad = com.droiddeck.launcher.input.JoyCon.normalize(event) if (pad !== event) return dispatchKeyEvent(pad) val fromController = PadBridge.isControllerKeyEvent(event) + if (fromController && (drawerOpen || pcKeyboardOpen || achievementsOpen || inputSettingsOpen || gestureSettingsOpen || SessionState.suspended)) { + padBridge?.setInputEnabled(false) + padBridge?.onKeyEvent(event) + } if (fromController && event.action == KeyEvent.ACTION_DOWN) { if (drawerOpen && !drawerControllerActive) focusDrawer() drawerControllerActive = true @@ -2772,7 +2782,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { } return true } - if ((pcKeyboardOpen || achievementsOpen) && fromController) return super.dispatchKeyEvent(event) + if ((pcKeyboardOpen || achievementsOpen || inputSettingsOpen) && fromController) return super.dispatchKeyEvent(event) if (!fromController && event.keyCode != KeyEvent.KEYCODE_BACK && sendToTopDesktop(event)) return true if (joyConVr?.onKeyEvent(event) == true) return true if (fromController && event.keyCode != KeyEvent.KEYCODE_BACK && padMouseMode() && padMouse.onKey(event)) return true @@ -2782,7 +2792,8 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { if (CompositorHost.isStarted && keyboardKey && !fromController) { val down = event.action == KeyEvent.ACTION_DOWN if (down && event.repeatCount > 0) return true - if (com.droiddeck.launcher.input.GuestKeys.send(event, ::sendGuestKey)) return true + val owner = if (event.deviceId <= 0) 16 else 8 + if (com.droiddeck.launcher.input.GuestKeys.send(event) { code, pressed -> sendGuestKey(code, pressed, owner) }) return true } return super.dispatchKeyEvent(event) } @@ -2808,6 +2819,10 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { override fun dispatchGenericMotionEvent(event: MotionEvent): Boolean { if (pipUi) return true notePress(event) + if (PadBridge.isControllerMotionEvent(event) && (drawerOpen || pcKeyboardOpen || achievementsOpen || inputSettingsOpen || gestureSettingsOpen || SessionState.suspended)) { + padBridge?.setInputEnabled(false) + padBridge?.onMotionEvent(event) + } if (drawerOpen && PadBridge.isControllerMotionEvent(event)) { if (!drawerControllerActive) focusDrawer() drawerControllerActive = true @@ -2819,7 +2834,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { return true } if (drawerDirectionKey != KeyEvent.KEYCODE_UNKNOWN) releaseDrawerDirection() - if ((pcKeyboardOpen || achievementsOpen) && PadBridge.isControllerMotionEvent(event)) return super.dispatchGenericMotionEvent(event) + if ((pcKeyboardOpen || achievementsOpen || inputSettingsOpen) && PadBridge.isControllerMotionEvent(event)) return super.dispatchGenericMotionEvent(event) if (joyConVr?.onMotionEvent(event) == true) return true if (PadBridge.isControllerMotionEvent(event) && padMouseMode() && padMouse.onMotion(event)) return true if (padBridge?.onMotionEvent(event) == true) return true @@ -2849,8 +2864,9 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { } return onScreenControls?.let { takes(it, it::takesTouchAt) } == true || steamTouchControls?.let { takes(it, it::takesTouchAt) } == true || - screenKeys?.let { takes(it, it::takesTouchAt) } == true || - unfoldedPanel?.let { takes(it, it::takesTouchAt) } == true + unfoldedPanel?.let { takes(it, it::takesTouchAt) } == true || + unfoldedDeckSurface?.let { takes(it, it::takesTouchAt) } == true || + unfoldedColumn?.getChildAt(0)?.let { takes(it) { _, _ -> true } } == true } override fun dispatchTouchEvent(event: MotionEvent): Boolean { @@ -2977,8 +2993,6 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { if (secondScreenMode == SecondScreenMode.NONE) closeSecondScreen(reset = false) else updateKeyboardRoute() } - private fun layoutNames(): List = - com.droiddeck.launcher.input.ControllerPrefs.screenLayouts(this).keys.sorted() private val relativeMouseRequests = java.util.concurrent.Executors.newSingleThreadExecutor { runnable -> Thread(runnable, "relative-mouse").apply { isDaemon = true } @@ -2998,7 +3012,15 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { } private fun applyRelativeMouse() { - val mode = desiredRelativeMouseMode + val wanted = desiredRelativeMouseMode + val touchpadRoute = com.droiddeck.launcher.input.TouchRouter.decide(com.droiddeck.launcher.input.TouchRouter.Snapshot( + uiBlocking = false, + mode = routedTouchMode, + scrollLayer = false, + steamSession = SessionState.mode == SessionService.MODE_STEAM, + steamDesktopUi = SessionState.steamUi == "desktop", + )) == com.droiddeck.launcher.input.TouchRouter.Route.TOUCHPAD + val mode = com.droiddeck.launcher.input.TouchGestureConfig.gamescopeRelativeMouse(wanted, mouseAttached(), touchpadRoute) if (appliedRelativeMouseMode == mode) return val request = org.json.JSONObject() .put("kind", "exec") @@ -3014,7 +3036,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { Log.w(TAG, "gamescope did not take relative mouse $mode: ${result.exceptionOrNull() ?: answer}") if (relativeMouseRetries++ < RELATIVE_MOUSE_MAX_RETRIES) uiHandler.postDelayed({ requestRelativeMouse() }, 2_000) } - if (desiredRelativeMouseMode != mode) requestRelativeMouse() + if (desiredRelativeMouseMode != wanted) requestRelativeMouse() } private fun refreshGestureConfig() { @@ -3048,7 +3070,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { } else moveTo(gesturePointerX + dx, gesturePointerY + dy) } override fun button(code: Int, down: Boolean) { - sendSceneButton(code, down) + heldButtons.set(16, code, down) } override fun prepareRelativePan() { if (pointerLocked) return @@ -3057,7 +3079,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { } override fun key(code: Int, down: Boolean) { if (down) gestureKeysDown.add(code) else gestureKeysDown.remove(code) - if (code !in guestKeysDown) WaylandCompositor.nativeSendKey(code, if (down) 1 else 0) + heldKeys.set(2, code, down) } override fun wheel(steps: Int) { sendSceneWheel(steps) } override fun showKeyboard() { uiHandler.post { touchscreenGestures.cancel(); togglePcKeyboard() } } @@ -3074,7 +3096,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { } override fun onButton(button: Int, pressed: Boolean, x: Float, y: Float) { if (!pointerLocked) movePointer(x, y) - sendSceneButton(button, pressed) + heldButtons.set(1, button, pressed) } override fun onWheel(steps: Int) { sendSceneWheel(steps) } override fun onScroll(dx: Float, dy: Float) = fingerScroll(dx, dy) @@ -3084,9 +3106,9 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { return true } override fun onZoom(steps: Int) { - WaylandCompositor.nativeSendKey(KEY_LEFTCTRL, 1) + heldKeys.set(128, KEY_LEFTCTRL, true) sendSceneWheel(steps) - WaylandCompositor.nativeSendKey(KEY_LEFTCTRL, 0) + heldKeys.set(128, KEY_LEFTCTRL, false) } override fun onLongPress() { @Suppress("DEPRECATION") @@ -3103,7 +3125,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { cursorVisible = false } override fun onButton(button: Int, pressed: Boolean) { - WaylandCompositor.nativeSendSceneInput(3, button, if (pressed) 1 else 0) + heldButtons.set(1, button, pressed) } override fun onScroll(dx: Float, dy: Float) { val rect = pointerSurfaceRect() ?: return @@ -3143,6 +3165,11 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { private fun padMouseMode(): Boolean { val now = android.os.SystemClock.uptimeMillis() + if (inputUsesLocalActions) { + if (padMouseOn) padMouse.reset() + padMouseOn = false + return false + } if (now - padMouseCheckedAt < PAD_MOUSE_CHECK_MS) return padMouseOn padMouseCheckedAt = now val on = !pointerLocked && EpicClient.launcherFocused(this) && !EpicClient.playing(this) @@ -3198,6 +3225,35 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { showCursor(cx, cy, fromMouse) } + private fun moveInputPointer(dx: Float, dy: Float) { + if (pointerLocked) { + sendSceneDelta(dx, dy) + cursorVisible = false + return + } + val rect = if (glassesOutputActive) pointerSurfaceRect() else drawnRect() + val pointerRect = rect ?: return + val position = cursorPos + val x = if (position.x >= 0f) position.x.coerceIn(pointerRect.left, pointerRect.right) else pointerRect.centerX() + val y = if (position.y >= 0f) position.y.coerceIn(pointerRect.top, pointerRect.bottom) else pointerRect.centerY() + val out = SessionState.outputSize + val nextX = (x + dx * pointerRect.width() / out.first) + .coerceIn(pointerRect.left, pointerRect.right) + val nextY = (y + dy * pointerRect.height() / out.second) + .coerceIn(pointerRect.top, pointerRect.bottom) + movePointer(nextX, nextY) + // Make frame-callback updates visible to drawing before this frame's traversal. + androidx.compose.runtime.snapshots.Snapshot.sendApplyNotifications() + } + + private fun onPadInput() { + if (inputUsesPointer && !pointerLocked) return + lastPadInputMs = android.os.SystemClock.uptimeMillis() + uiHandler.removeCallbacks(cursorHide) + mouseOwnsCursor = false + cursorVisible = false + } + private fun moveCapturedMouse(dx: Float, dy: Float) { val rect = drawnRect() ?: return val scale = sceneScale(rect) @@ -3217,8 +3273,8 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { cursorResyncIdle = 0 if (fromMouse || !touchDrivesPointer()) uiHandler.postDelayed(cursorResync, 60) if (clientHidesCursor) { cursorVisible = false; return } - if (!fromMouse && directTouchCursorHidden()) { cursorVisible = false; return } - if (android.os.SystemClock.uptimeMillis() - lastPadInputMs < CURSOR_PAD_HOLD_MS) return + if (!inputUsesPointer && !fromMouse && directTouchCursorHidden()) { cursorVisible = false; return } + if (!inputUsesPointer && android.os.SystemClock.uptimeMillis() - lastPadInputMs < CURSOR_PAD_HOLD_MS) return cursorVisible = true uiHandler.removeCallbacks(cursorHide) if (!fromMouse && SessionState.mode == SessionService.MODE_STEAM && SessionState.steamUi != "desktop") uiHandler.postDelayed(cursorHide, 2500) @@ -3260,6 +3316,11 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { return true } + private var dockedToDisplay = false + private var automaticPanel: Boolean + get() = SessionState.secondScreenAutomatic + set(value) { SessionState.secondScreenAutomatic = value } + private val shownOnDisplayId: Int get() = if (Build.VERSION.SDK_INT >= 30) display?.displayId ?: windowManager.defaultDisplay.displayId else windowManager.defaultDisplay.displayId @@ -3271,6 +3332,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { .filter { glassesScreen == null || it.id != glasses.displayId } val oldIds = secondScreenDisplays.map { it.id }.toSet() secondScreenDisplays = candidates + reconcileDock(primaryId) bottomSwapAvailable = bottomGame != null || bottomGameDisplay() != null val newIds = candidates.map { it.id }.toSet() if (secondScreenMode != SecondScreenMode.NONE && oldIds.isNotEmpty() && selectedSecondScreenDisplay !in newIds) { @@ -3287,6 +3349,17 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { updateKeyboardRoute() } + private fun reconcileDock(primaryId: Int) { + val nowDocked = com.droiddeck.launcher.input.DualScreen.dockedToDisplay(displayManager, primaryId) + val automaticOnBuiltIn = automaticPanel && secondScreenMode != SecondScreenMode.NONE && + displayManager.getDisplay(selectedSecondScreenDisplay)?.let(com.droiddeck.launcher.input.DualScreen::builtIn) == true + if (com.droiddeck.launcher.input.DualScreen.leavesBottomOnDock(dockedToDisplay, nowDocked, SessionPrefs.dualScreenSessionMode(this), automaticOnBuiltIn)) { + closeSecondScreen(reset = true) + SessionState.secondScreenAutoOpened = false + } + dockedToDisplay = nowDocked + } + private fun openDualScreenControls() { if (SessionState.secondScreenAutoOpened || secondScreenMode != SecondScreenMode.NONE || pipUi) return if (!SessionPrefs.dualScreenSessionControls(this)) return @@ -3294,17 +3367,25 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { if (secondScreenDisplays.none { it.id == target.displayId }) return SessionState.secondScreenAutoOpened = true selectedSecondScreenDisplay = target.displayId - selectSecondScreenMode(SecondScreenMode.opening(SessionPrefs.secondScreenPanel(this), SessionState.deckPad, bottomPages().isNotEmpty(), - com.droiddeck.launcher.session.BottomDesktop.available())) + selectSecondScreenMode(openingPanel()) + automaticPanel = secondScreenMode != SecondScreenMode.NONE dualControlsProvisional = ::loading.isInitialized && loading.visible } + private val desktopSession get() = loadingMode() == SessionService.MODE_DESKTOP + + private fun leaveDeckPanel() { + if (secondScreenMode == SecondScreenMode.DECK_CONTROLS && !SessionState.deckPad) selectSecondScreenMode(secondScreenMode) + } + + private fun openingPanel() = SecondScreenMode.opening(SessionPrefs.secondScreenPanel(this, desktopSession), SessionState.deckPad, + bottomPages().isNotEmpty(), com.droiddeck.launcher.session.BottomDesktop.available(), desktopSession) + private fun settleDualScreenControls() { if (!dualControlsProvisional || pipUi || !started || (::loading.isInitialized && loading.visible)) return dualControlsProvisional = false if (secondScreenMode == SecondScreenMode.NONE) return - val wanted = SecondScreenMode.opening(SessionPrefs.secondScreenPanel(this), SessionState.deckPad, bottomPages().isNotEmpty(), - com.droiddeck.launcher.session.BottomDesktop.available()) + val wanted = openingPanel() if (wanted != secondScreenMode) selectSecondScreenMode(wanted) else secondScreenPresentation?.retryTerminal() } @@ -3326,7 +3407,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { return } if (secondScreenDisplays.isEmpty()) { - secondScreenMode = SecondScreenMode.NONE + panelMode(SecondScreenMode.NONE) return } if (selectedSecondScreenDisplay !in secondScreenDisplays.map { it.id }) { @@ -3335,8 +3416,9 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { showSecondScreen(mode) } - private fun showSecondScreen(mode: SecondScreenMode) { + private fun showSecondScreen(requested: SecondScreenMode) { if (pipUi || !started) return + val mode = if (requested == SecondScreenMode.DECK_CONTROLS && !SessionState.deckPad) openingPanel() else requested val target = displayManager.getDisplay(selectedSecondScreenDisplay) if (target == null || !target.isValid || (target.flags and Display.FLAG_PRESENTATION) == 0) { closeSecondScreen(reset = false) @@ -3344,12 +3426,15 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { } if (com.droiddeck.launcher.input.DualScreen.taken(target.displayId) || bottomGame?.display?.displayId == target.displayId) { if (secondScreenPresentation != null) closeSecondScreen(reset = false) - secondScreenMode = mode - SessionState.secondScreenMode = mode + panelMode(mode) return } var presentation = secondScreenPresentation if (presentation?.display?.displayId != target.displayId) { + if (target.displayId in inputWindowDisplays) { + panelMode(mode) + return + } dropDockedDrawer() presentation?.closeControls() presentation = secondScreenPresentation(target) @@ -3358,8 +3443,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { try { if (!presentation.isShowing) presentation.show() presentation.showMode(mode) - secondScreenMode = mode - SessionState.secondScreenMode = mode + panelMode(mode) SessionState.secondScreenDisplay = selectedSecondScreenDisplay updateKeyboardRoute() } catch (e: Exception) { @@ -3370,9 +3454,6 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { private fun secondScreenPresentation(target: Display) = SecondScreenPresentation( this, target, - sendPointer = ::moveSecondScreenPointer, - sendButton = ::sendTrackpadButton, - sendWheel = { steps -> WaylandCompositor.nativeSendSceneInput(4, steps, 0) }, onSteamMenu = ::tapSteamButton, onQam = ::tapQam, onClose = ::closeBottomPanel, @@ -3390,6 +3471,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { DroidDeckTheme { SessionMenu(docked = true) } }, onSwap = if (bottomSwapAvailable && com.droiddeck.launcher.input.DualScreen.builtIn(target)) ({ putGameOnBottom(true) }) else null, + onDeckPanel = ::selectDeckPanel, dashboard = if (bottomPages().isEmpty()) null else ({ DroidDeckTheme { BottomDashboard( @@ -3407,50 +3489,9 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { private fun bottomPages(): List = sessionBottomPages(BottomPageActions( takeScreenshot = ::captureScreenshot, fps = { if (::hud.isInitialized) hud.liveFps else null }, - openSettings = ::openSettingsBelow, runningGame = { gestureGame }, )) - private fun openSettingsBelow(intent: android.content.Intent, done: () -> Boolean) { - val presentation = secondScreenPresentation - if (presentation == null) { - if (!bottomOutputActive) return - listOf(intent, android.content.Intent(android.provider.Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS)).any { - runCatching { startActivity(android.content.Intent(it)) } - .onFailure { e -> Log.w(TAG, "could not open ${it.action}", e) } - .isSuccess - } - return - } - val options = android.app.ActivityOptions.makeBasic().setLaunchDisplayId(presentation.display.displayId) - val opened = listOf(intent, android.content.Intent(android.provider.Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS)).any { - runCatching { startActivity(android.content.Intent(it).addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK), options.toBundle()) } - .onFailure { e -> Log.w(TAG, "could not open ${it.action} on display ${presentation.display.displayId}", e) } - .isSuccess - } - if (!opened) return - presentation.hide() - var checks = 0 - fun showAgain() { - try { - presentation.show() - } catch (e: Exception) { - Log.w(TAG, "could not show the dashboard again", e) - if (secondScreenPresentation === presentation) closeSecondScreen(reset = true) - } - } - uiHandler.postDelayed(object : Runnable { - override fun run() { - if (secondScreenPresentation !== presentation || presentation.isShowing) return - if (done()) { - showAgain() - return - } - if (++checks < SETTINGS_WATCH_CHECKS) uiHandler.postDelayed(this, SETTINGS_WATCH_MS) else showAgain() - } - }, SETTINGS_WATCH_MS) - } - private fun captureScreenshot(onDone: (Boolean) -> Unit) { val source = if (glassesOutputActive) glassesScreen?.surfaceView else if (bottomOutputActive) bottomGame?.surfaceView else if (::surfaceView.isInitialized) surfaceView else null @@ -3486,18 +3527,63 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { } private fun tapSteamButton() { - if (SessionState.mode == SessionService.MODE_STEAM) { - padBridge?.applyTouch { st -> st.press(com.droiddeck.launcher.input.PadState.GUIDE, true) } - Handler(Looper.getMainLooper()).postDelayed({ - padBridge?.applyTouch { st -> st.press(com.droiddeck.launcher.input.PadState.GUIDE, false) } - }, 90) - } + if (SessionState.mode == SessionService.MODE_STEAM) padBridge?.triggerSteamMenu() + } + + fun inputEditorDisplay(role: InputRole): Display? { + val main = window.decorView.display + if (role == InputRole.PRIMARY) return bottomGame?.display ?: main + if (bottomGame != null) return main + return com.droiddeck.launcher.input.SecondScreenDisplays.editorCandidates(selectedSecondScreenDisplay, + secondScreenDisplays.map { it.id }, com.droiddeck.launcher.input.DualScreen::taken).firstNotNullOfOrNull(displayManager::getDisplay) + } + + fun inputWindowShown(displayId: Int) { inputWindowDisplays += displayId } + + fun inputWindowClosed(displayId: Int) { + inputWindowDisplays -= displayId + if (started && displayId !in inputWindowDisplays && secondScreenMode != SecondScreenMode.NONE && secondScreenPresentation == null) showSecondScreen(secondScreenMode) + } + + private fun refreshInputProfile() { + val library = com.droiddeck.launcher.input.InputLibraries.read(this) + val desktop = loadingMode() == SessionService.MODE_DESKTOP + inputProfile = library.compileActive(desktop, secondScreenMode.panel(this)) + primaryInputLayout = library.activeLayout(desktop, InputRole.PRIMARY) + adaptiveSticks = primaryInputLayout?.adaptiveSticks + quickButtons = primaryInputLayout?.takeIf { it.editable }?.let { l -> l.quickButtons.associateWith { it !in l.style.hidden } }.orEmpty() + secondaryInputLayout = library.panelLayout(desktop, secondScreenMode.panel(this)) + deckPanelLayouts = com.droiddeck.launcher.input.DeckPanel.entries.associateWith { library.panelLayout(desktop, it.bindings) } + inputUsesLocalActions = InputControl.padControls.any { inputProfile.action(InputRole.PHYSICAL, it) !is InputAction.Native } + inputUsesPointer = inputProfile.bindings.values.any { bindings -> + bindings.values.any { it is InputAction.Pointer || it is InputAction.MouseButton || it is InputAction.Scroll } + } || inputProfile.elements.values.any { bindings -> + bindings.values.any { it is InputAction.Pointer || it is InputAction.MouseButton || it is InputAction.Scroll } + } || inputProfile.clicks.values.any { bindings -> bindings.values.any { it is InputAction.MouseButton } } + || inputProfile.keys.any { it.action is InputAction.MouseButton } + physicalInputBindings?.configure(inputProfile.keys) + padBridge?.setInputProfile(inputProfile) + DeckControls.refresh() + onScreenControls?.updateLayout(primaryInputLayout) + bottomInputControls?.updateLayout(primaryInputLayout) + unfoldedPanel?.updateLayout(primaryInputLayout) + if (unfoldedPanel != null) applyUnfoldedControls() + showUnfoldedDeckPanel() + updateOnScreenPad() + secondScreenPresentation?.refreshControls() } private fun tapQam() { if (SessionState.mode == SessionService.MODE_STEAM) padBridge?.triggerQam() } + private fun selectDeckPanel(panel: com.droiddeck.launcher.input.DeckPanel) { + if (SessionPrefs.deckPanel(this) == panel) return + releasePads() + SessionPrefs.setDeckPanel(this, panel) + refreshInputProfile() + } + private fun chooseUnfoldedControls(on: Boolean) { SessionPrefs.setUnfoldedControls(this, on) unfoldedControls = on @@ -3513,7 +3599,6 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { private fun refreshControls() { applyUnfoldedControls() updateOnScreenControls() - screenKeys?.refreshAppearance() } private fun controlsBelowWanted(): Boolean { @@ -3527,7 +3612,10 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { val width = sessionRoot.width val height = sessionRoot.height val wanted = controlsBelowWanted() - val gameHeight = if (wanted) com.droiddeck.launcher.session.SessionDisplay.unfoldedGameHeight(width, height) + val tabRow = unfoldedColumn?.getChildAt(0)?.height?.let { + it - (com.droiddeck.launcher.ui.BAR_HEIGHT.value * resources.displayMetrics.density + 0.5f).toInt() + } ?: 0 + val gameHeight = if (wanted) com.droiddeck.launcher.session.SessionDisplay.unfoldedGameHeight(width, height, tabRow) else FrameLayout.LayoutParams.MATCH_PARENT val params = surfaceView.layoutParams as FrameLayout.LayoutParams if (params.height != gameHeight) { @@ -3535,36 +3623,65 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { Log.i(TAG, if (wanted) "unfolded controls: game ${width}x$gameHeight, controls ${width}x${height - gameHeight}" else "unfolded controls: game full screen") } + val portrait = !com.droiddeck.launcher.session.SessionDisplay.foldable(this) + val dock = if (SessionState.deckPad) com.droiddeck.launcher.input.OnScreenLayout.Dock.deck(portrait, deckPanelLayouts[com.droiddeck.launcher.input.DeckPanel.GAMEPAD]) + else com.droiddeck.launcher.input.OnScreenLayout.Dock(portrait, false) var panel = unfoldedPanel - if (panel != null && panel.deckControls != SessionState.deckPad) { - sessionRoot.removeView(panel) + if (panel != null && (!wanted || panel.dock != dock)) { + sessionRoot.removeView(unfoldedColumn) + unfoldedColumn = null + unfoldedDeckSurface = null + unfoldedDeckPanel = null unfoldedPanel = null panel = null + if (!wanted) updateOnScreenControls() } - if (!wanted) { - if (panel != null) { sessionRoot.removeView(panel); unfoldedPanel = null; updateOnScreenControls() } - return - } + if (!wanted) return if (panel == null) { - panel = com.droiddeck.launcher.ui.DeckControlsPanel(this, onClose = { chooseUnfoldedControls(false) }, - onSteamMenu = ::tapSteamButton, onQam = ::tapQam, pad = padBridge, - portraitDock = !com.droiddeck.launcher.session.SessionDisplay.foldable(this), onMenu = { drawerOpen = true }) - sessionRoot.addView(panel, sessionRoot.indexOfChild(surfaceView) + 1) + panel = OnScreenControls(this, padBridge, dock).also { it.updateLayout(primaryInputLayout) } + val column = android.widget.LinearLayout(this).apply { + orientation = android.widget.LinearLayout.VERTICAL + addView(unfoldedBar().apply { + addOnLayoutChangeListener { _, _, top, _, bottom, _, oldTop, _, oldBottom -> + if (bottom - top != oldBottom - oldTop) post { applyUnfoldedControls() } + } + }, android.widget.LinearLayout.LayoutParams(android.widget.LinearLayout.LayoutParams.MATCH_PARENT, + android.widget.LinearLayout.LayoutParams.WRAP_CONTENT)) + addView(panel, android.widget.LinearLayout.LayoutParams(android.widget.LinearLayout.LayoutParams.MATCH_PARENT, 0, 1f)) + } + sessionRoot.addView(column, sessionRoot.indexOfChild(surfaceView) + 1) + unfoldedColumn = column unfoldedPanel = panel + showUnfoldedDeckPanel() updateOnScreenControls() } - panel.layoutParams = FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, height - gameHeight, Gravity.BOTTOM) + unfoldedColumn?.layoutParams = FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, height - gameHeight, Gravity.BOTTOM) } - private fun moveSecondScreenPointer(x: Float, y: Float, width: Float, height: Float) { - if (width <= 0f || height <= 0f) return - val nx = (x / width).coerceIn(0f, 1f) - val ny = (y / height).coerceIn(0f, 1f) - val rect = pointerSurfaceRect() ?: return - val px = rect.left + nx * rect.width() - val py = rect.top + ny * rect.height() - WaylandCompositor.nativeSendPointer(1, InputSpace.x(px, outputView().width), InputSpace.y(py, outputView().height)) - showCursor(px, py) + private fun unfoldedBar(): View = com.droiddeck.launcher.ui.bottomScreenBarView(this) { + val deck = unfoldedDeckPanel + com.droiddeck.launcher.ui.sessionBarModel(getString(if (deck != null) R.string.second_mode_deck_controls else R.string.deck_tab_gamepad), + { chooseUnfoldedControls(false) }, { drawerOpen = true }, ::tapSteamButton, ::tapQam, + tabs = deck?.let { com.droiddeck.launcher.ui.deckPanelTabs(this, it, ::selectDeckPanel) }) + } + + private fun showUnfoldedDeckPanel() { + val column = unfoldedColumn + val panel = unfoldedPanel + val selected = SessionPrefs.deckPanel(this).takeIf { column != null && panel?.dock?.grips == true } + unfoldedDeckPanel = selected + val layout = selected?.takeIf { it != com.droiddeck.launcher.input.DeckPanel.GAMEPAD }?.let { deckPanelLayouts[it] } + if (layout != null && unfoldedDeckSurface?.tag == layout) return + unfoldedDeckSurface?.let { it.releaseInputs(); column?.removeView(it) } + unfoldedDeckSurface = null + if (column == null || panel == null) return + if (layout == null) { panel.visibility = View.VISIBLE; return } + panel.releaseAll() + panel.visibility = View.GONE + unfoldedDeckSurface = com.droiddeck.launcher.ui.InputSurfaceView(this, padBridge, layout.elements, InputRole.PRIMARY, layout.style, haptics = true).also { + it.tag = layout + column.addView(it, android.widget.LinearLayout.LayoutParams(android.widget.LinearLayout.LayoutParams.MATCH_PARENT, 0, 1f)) + } } private fun bottomKeyboardPresentation(): SecondScreenPresentation? { @@ -3645,13 +3762,13 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { } private fun selectGameField(vararg then: Int) { - val held = KEY_LEFTCTRL in guestKeysDown || KEY_LEFTCTRL in gestureKeysDown - if (!held) WaylandCompositor.nativeSendKey(KEY_LEFTCTRL, 1) - for (key in intArrayOf(KEY_A) + then) { - WaylandCompositor.nativeSendKey(key, 1) - WaylandCompositor.nativeSendKey(key, 0) - } - if (!held) WaylandCompositor.nativeSendKey(KEY_LEFTCTRL, 0) + heldKeys.set(256, KEY_LEFTCTRL, true) + try { + for (key in intArrayOf(KEY_A) + then) { + heldKeys.set(256, key, true) + heldKeys.set(256, key, false) + } + } finally { heldKeys.release(256) } } private fun readGameField(done: (String?) -> Unit) { @@ -3677,7 +3794,8 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { private fun updateKeyboardRoute() { AgentGuest.routeKeyboard(this, SessionState.mode == SessionService.MODE_STEAM && - (secondScreenPresentation?.showsKeyboard == true || dockDrawer())) + (secondScreenPresentation?.showsKeyboard == true || dockDrawer()) && + SessionPrefs.androidTextFieldKeyboard(this)) } private fun dropDockedDrawer() { @@ -3697,11 +3815,18 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { else com.droiddeck.launcher.ui.SessionTyping.close() updateKeyboardRoute() if (reset) { - secondScreenMode = SecondScreenMode.NONE - SessionState.secondScreenMode = SecondScreenMode.NONE + panelMode(SecondScreenMode.NONE) + automaticPanel = false } } + private fun panelMode(mode: SecondScreenMode) { + val rebind = secondScreenMode.panel(this) != mode.panel(this) + secondScreenMode = mode + SessionState.secondScreenMode = mode + if (rebind) refreshInputProfile() + } + private fun effectiveTouchMode(): String { val options = steamTouchOptions if (options == null || steamTouchControls?.visibility != View.VISIBLE) return SessionPrefs.touchMode(this) @@ -3732,6 +3857,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { if (next == SessionPrefs.TOUCH_OFF) cursorVisible = false gestureTouchStreamGate.reset() routedTouchMode = next + requestRelativeMouse() if (directTouchCursorHidden()) cursorVisible = false } @@ -3756,40 +3882,44 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { private fun sceneScale(rect: android.graphics.RectF): Float = SessionState.outputSize.first / rect.width() - private fun openEvdevMouse() { + // Clearing button owners emits releases, so backend transitions share their lock with all mouse writes. + private fun openEvdevMouse(): Unit = synchronized(heldButtons) { if (evdevMouseActive || !evdevMouseOn) return val writer = mouseWriter ?: com.droiddeck.launcher.input.FakeInputWriter( File(LinuxRuntime.sessionRoot(this), "dev/input").absolutePath, com.droiddeck.launcher.input.FakeInputWriter.MOUSE_SLOT, ).also { mouseWriter = it } if (!writer.open()) return + heldButtons.clear() evdevMouseActive = true lastMouseX = Float.NaN lastMouseY = Float.NaN Log.i(TAG, "raw mouse channel active (pointer locked)") } - private fun closeEvdevMouse() { + private fun closeEvdevMouse(): Unit = synchronized(heldButtons) { if (!evdevMouseActive) return + heldButtons.clear() mouseWriter?.softRelease() evdevMouseActive = false Log.i(TAG, "raw mouse channel idle") } - private fun releaseEvdevMouse() { + private fun releaseEvdevMouse(): Unit = synchronized(heldButtons) { + if (evdevMouseActive) heldButtons.clear() evdevMouseActive = false mouseWriter?.destroy() mouseWriter = null Log.i(TAG, "raw mouse channel removed") } - private fun sendSceneDelta(dx: Float, dy: Float) { + private fun sendSceneDelta(dx: Float, dy: Float): Unit = synchronized(heldButtons) { val writer = if (evdevMouseActive) mouseWriter else null if (writer != null) writer.writeMouseDelta(dx, dy) else WaylandCompositor.sendPointerDelta(dx, dy) } - private fun sendSceneButton(code: Int, down: Boolean) { + private fun sendSceneButton(code: Int, down: Boolean): Unit = synchronized(heldButtons) { val raw = if (down) evdevMouseActive.also { guestButtonsDown[code] = it } else guestButtonsDown.remove(code) ?: evdevMouseActive val writer = if (raw) mouseWriter else null @@ -3797,14 +3927,10 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { else WaylandCompositor.nativeSendSceneInput(3, code, if (down) 1 else 0) } - private fun sendTrackpadButton(button: Int, pressed: Boolean) { - if (pressed) trackpadButtonsDown.add(button) else trackpadButtonsDown.remove(button) - WaylandCompositor.nativeSendSceneInput(3, button, if (pressed) 1 else 0) - } - - private fun sendSceneWheel(steps: Int) { + private fun sendSceneWheel(steps: Int, horizontal: Boolean = false): Unit = synchronized(heldButtons) { val writer = if (evdevMouseActive) mouseWriter else null - if (writer != null) writer.writeMouseWheel(-steps, 0, 120) + if (writer != null) writer.writeMouseWheel(if (horizontal) 0 else -steps, if (horizontal) steps else 0, 120) + else if (horizontal) WaylandCompositor.nativeSendSceneInput(11, 0, steps * 120) else WaylandCompositor.nativeSendSceneInput(4, steps, 0) } @@ -3824,8 +3950,29 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { val width = outputView().width.takeIf { it > 0 } ?: return null val height = outputView().height.takeIf { it > 0 } ?: return null topPictureMapped = CompositorHost.isStarted && WaylandCompositor.nativeGamePictureRect(gamePictureBounds) - if (topPictureMapped) return android.graphics.RectF( - gamePictureBounds[0], gamePictureBounds[1], gamePictureBounds[2], gamePictureBounds[3]) + if (topPictureMapped) { + val rect = android.graphics.RectF( + gamePictureBounds[0], gamePictureBounds[1], + gamePictureBounds[2], gamePictureBounds[3] + ) + + if (glassesOutputActive) { + val mode = glassesScreen?.display?.mode ?: return null + val outputWidth = mode.physicalWidth.takeIf { it > 0 } ?: return null + val outputHeight = mode.physicalHeight.takeIf { it > 0 } ?: return null + val scaleX = width.toFloat() / outputWidth + val scaleY = height.toFloat() / outputHeight + + return android.graphics.RectF( + rect.left * scaleX, + rect.top * scaleY, + rect.right * scaleX, + rect.bottom * scaleY + ) + } + + return rect + } val out = SessionState.outputSize val scale = minOf(width / out.first.toFloat(), height / out.second.toFloat()) val drawnW = out.first * scale @@ -3852,9 +3999,17 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { private var lockedSwipeX = 0f private var lockedSwipeY = 0f private var dismissTouch = false + private var sessionTouchDown = false override fun onTouchEvent(event: MotionEvent): Boolean { if (event.isFromSource(android.view.InputDevice.SOURCE_MOUSE)) return onMouse(event) + when (event.actionMasked) { + MotionEvent.ACTION_DOWN -> { + sessionTouchDown = true + dismissTouch = false + } + MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> sessionTouchDown = false + } if (drawerOpen && drawerDocked && event.actionMasked == MotionEvent.ACTION_DOWN) { drawerLayerOpen = false drawerOpenState = false @@ -3866,7 +4021,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { if (event.actionMasked == MotionEvent.ACTION_UP || event.actionMasked == MotionEvent.ACTION_CANCEL) dismissTouch = false return true } - val uiBlocking = drawerOpen || pcKeyboardOpen || achievementsOpen || gestureSettingsOpen || pipUi || SessionState.suspended + val uiBlocking = drawerOpen || achievementsOpen || gestureSettingsOpen || inputSettingsOpen || pipUi || SessionState.suspended if (event.actionMasked == MotionEvent.ACTION_DOWN && !uiBlocking && routedTouchMode != SessionPrefs.TOUCH_GESTURES) { scrollTouchGesture = scrollTouchMode() scrollRestX = 0f @@ -3894,7 +4049,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { com.droiddeck.launcher.input.TouchRouter.Route.SCROLL -> scrollTouch.onTouch(event) else -> { if (com.droiddeck.launcher.input.TouchRouter.steamKeyboardZone(snapshot)) { - val wasActive = threeFingerTap.active + val wasActive = threeFingerTap.active && event.actionMasked != MotionEvent.ACTION_DOWN if (threeFingerTap.onTouch(event)) padBridge?.triggerSteamKeyboard() if (threeFingerTap.active && !wasActive) { if (route == com.droiddeck.launcher.input.TouchRouter.Route.TOUCHPAD) touchpad.cancel() @@ -3970,9 +4125,10 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { return true } if (event.actionMasked == MotionEvent.ACTION_DOWN) lockedSwipeUsed = false + if (lockedSwipeUsed && (event.actionMasked != MotionEvent.ACTION_MOVE || !programLocksPointer())) return true if (programLocksPointer() && event.actionMasked == MotionEvent.ACTION_MOVE) { + if (lockedSwipeUsed) directTouchHold.cancelPendingHold() else directTouchHold.cancelTouch() lockedSwipeUsed = true - directTouchHold.cancelPendingHold() val scale = sceneScale(rect) sendSceneDelta((event.x - swipeX) * scale, (event.y - swipeY) * scale) return true @@ -3981,7 +4137,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { if (!pointerLocked) showCursor(event.x.coerceIn(rect.left, rect.right), event.y.coerceIn(rect.top, rect.bottom)) if (WaylandCompositor.nativeFocusTakesTouch()) { listOf(PointerGestures.BTN_LEFT, PointerGestures.BTN_RIGHT, PointerGestures.BTN_MIDDLE).forEach { - WaylandCompositor.nativeSendSceneInput(3, it, 0) + heldButtons.set(1, it, false) } } } @@ -3995,7 +4151,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { if (evdevMouseActive) sendMouseDelta(event.x, event.y) else movePointer(event.x, event.y, fromMouse = true) MotionEvent.ACTION_BUTTON_PRESS, MotionEvent.ACTION_BUTTON_RELEASE -> { if (evdevMouseActive) sendMouseDelta(event.x, event.y) else movePointer(event.x, event.y, fromMouse = true) - guestMouseButton(event.actionButton)?.let { sendSceneButton(it, event.actionMasked == MotionEvent.ACTION_BUTTON_PRESS) } + guestMouseButton(event.actionButton)?.let { heldButtons.set(8, it, event.actionMasked == MotionEvent.ACTION_BUTTON_PRESS) } } MotionEvent.ACTION_SCROLL -> { val v = event.getAxisValue(MotionEvent.AXIS_VSCROLL) @@ -4023,6 +4179,12 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { isHardwareMouse(d) && d?.supportsSource(InputDevice.SOURCE_TOUCHPAD) != true } + private fun externalMouseAttached(): Boolean { + lastExternalMouse = InputDevice.getDeviceIds().map { InputDevice.getDevice(it) } + .firstOrNull { device -> device != null && com.droiddeck.launcher.input.MouseBackInput.isExternalMouse(device) } + return lastExternalMouse != null + } + private fun relativeMouseAttached(): Boolean = !isInMultiWindowMode && !glassesOutputActive && InputDevice.getDeviceIds().any { id -> val d = InputDevice.getDevice(id) isHardwareMouse(d) && d?.supportsSource(InputDevice.SOURCE_TOUCHPAD) != true @@ -4031,7 +4193,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { private fun updatePointerCapture() { if (!::surfaceView.isInitialized) return val view = window.decorView - val want = (pointerLocked || relativeMouseAttached()) && resumed && hasWindowFocus() && !drawerOpen && !pcKeyboardOpen && !achievementsOpen && !pipUi && !bottomOutputActive && !touchEditorOpen && mouseAttached() + val want = (pointerLocked || relativeMouseAttached()) && resumed && hasWindowFocus() && !drawerOpen && !pcKeyboardOpen && !achievementsOpen && !inputSettingsOpen && !pipUi && !bottomOutputActive && !touchEditorOpen && mouseAttached() releaseDesktopMouseGrab() if (want == view.hasPointerCapture()) return if (want) view.requestPointerCapture() else view.releasePointerCapture() @@ -4084,13 +4246,16 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { private var platformPointerShown: Boolean? = null private fun touchEditorChanged() { - touchEditorOpen = screenKeys?.isEditing == true || steamTouchControls?.isEditing == true + touchEditorOpen = steamTouchControls?.isEditing == true updatePointerCapture() applyPlatformPointer(appUiOwnsPointer()) } + private fun onlyPcKeyboardOpen(): Boolean = + pcKeyboardOpen && !drawerOpen && !achievementsOpen && !inputSettingsOpen && !pipUi && !glassesOutputActive && !touchEditorOpen && !wowChatOnTop + private fun appUiOwnsPointer(): Boolean = - drawerOpen || pcKeyboardOpen || achievementsOpen || pipUi || glassesOutputActive || bottomOutputActive || touchEditorOpen || wowChatOnTop + drawerOpen || pcKeyboardOpen || achievementsOpen || inputSettingsOpen || pipUi || glassesOutputActive || bottomOutputActive || touchEditorOpen || wowChatOnTop private fun applyPlatformPointer(shown: Boolean) { if (!::sessionRoot.isInitialized || !::surfaceView.isInitialized) return @@ -4120,7 +4285,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { } } MotionEvent.ACTION_BUTTON_PRESS, MotionEvent.ACTION_BUTTON_RELEASE -> - guestMouseButton(event.actionButton)?.let { sendSceneButton(it, event.actionMasked == MotionEvent.ACTION_BUTTON_PRESS) } + guestMouseButton(event.actionButton)?.let { heldButtons.set(8, it, event.actionMasked == MotionEvent.ACTION_BUTTON_PRESS) } MotionEvent.ACTION_SCROLL -> { val v = event.getAxisValue(MotionEvent.AXIS_VSCROLL) if (v != 0f) sendSceneWheel(-Math.round(v)) @@ -4164,7 +4329,6 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { device.plugged = false uiHandler.postDelayed({ updateOnScreenControls() }, 700) } - view.onStyle = { screenKeys?.refreshAppearance() } view.onTouchMiss = miss@{ event -> if (!gestureTouchStreamGate.accepts(GestureTouchStreamGate.Source.ON_SCREEN_CONTROLS, event)) return@miss event.offsetLocation(view.left - surfaceView.left.toFloat(), view.top - surfaceView.top.toFloat()) @@ -4180,33 +4344,41 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { updateOnScreenPad() val c = onScreenControls glassesSlot = when { - c == null || c.visibility != View.VISIBLE -> 0 + c == null || c.visibility != View.VISIBLE || c.isPinnedOnly -> 0 c.isButtonsOnly -> -1 - controllerSettings?.keyboardButton != false -> 2 + primaryInputLayout?.let { l -> l.elements.any { it.quickButton == InputCommand.KEYBOARD && !l.style.hides(it) } } + ?: (controllerSettings?.keyboardButton != false) -> 2 else -> 1 } } private fun updateOnScreenPad() { + fun hideBottomControls() { bottomInputControls?.takeIf { it.visibility == View.VISIBLE }?.let { it.releaseAll(); it.visibility = View.GONE } } val forced = File(Environment.getExternalStorageDirectory(), "Download/droiddeck-osc") .takeIf { it.isFile } ?.let { FileUtils.readString(it)?.trim()?.lowercase() } ?: SessionPrefs.oscMode(this) val controls = onScreenControls ?: return - if (pipUi || unfoldedPanel != null || pcKeyboardOpen || + fun showPinned() { + controls.setPinnedOnly(true) + val pinned = controls.hasPinned && !pipUi && !inputSettingsOpen + if (!pinned) controls.releaseAll() + controls.visibility = if (pinned) View.VISIBLE else View.GONE + } + if (pipUi || unfoldedPanel != null || pcKeyboardOpen || inputSettingsOpen || (SessionState.mode == SessionService.MODE_STEAM && !SessionPrefs.builtinPad(this))) { onScreenButtonsVisible = false - controls.releaseAll() - controls.visibility = View.GONE + hideBottomControls() + showPinned() steamTouchControls?.let { it.cancelEditing(); it.releaseAll(); it.visibility = View.GONE } com.droiddeck.launcher.input.SteamTouchDevice.current?.plugged = false - screenKeys?.followSteam = false applyTouchRouting() return } val buttonsOnly = forced == SessionPrefs.OSC_STEAM_QAM val wantsSteam = forced == SessionPrefs.OSC_STEAM_TOUCH && SessionState.mode == SessionService.MODE_STEAM val steam = if (wantsSteam) steamTouch() else null + steamTouchInUse = steam != null if (wantsSteam && steam == null && !isFinishing && !isDestroyed) { uiHandler.removeCallbacks(steamTouchRetry) uiHandler.postDelayed(steamTouchRetry, 1000) @@ -4229,8 +4401,8 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { onScreenButtonsVisible = show val drawn = show && !keyboardCoversPad() if (steam != null) { - screenKeys?.followSteam = drawn - if (controls.visibility == View.VISIBLE) { controls.releaseAll(); controls.visibility = View.GONE } + hideBottomControls() + showPinned() if (!drawn) { steam.cancelEditing(); steam.releaseAll() } steam.visibility = if (drawn) View.VISIBLE else View.GONE com.droiddeck.launcher.input.SteamTouchDevice.current?.plugged = show @@ -4238,10 +4410,28 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { Log.i(TAG, "on-screen controls: Steam's touch controller " + if (drawn) "shown" else "hidden") return } - screenKeys?.followSteam = false - if (drawn == (controls.visibility == View.VISIBLE)) return - if (!drawn) controls.releaseAll() - controls.visibility = if (drawn) View.VISIBLE else View.GONE + val bottomRoot = bottomGame?.surfaceView?.parent as? android.view.ViewGroup + if (bottomRoot != null && (drawn || controls.hasPinned)) { + controls.releaseAll(); controls.visibility = View.GONE + var bottom = bottomInputControls + if (bottom?.parent !== bottomRoot) { + (bottom?.parent as? android.view.ViewGroup)?.removeView(bottom) + bottom = OnScreenControls(bottomRoot.context, padBridge).also { view -> + view.onTouchMiss = { event -> onBottomGameTouch(event) } + bottomRoot.addView(view, android.view.ViewGroup.LayoutParams(-1, -1)) + } + bottomInputControls = bottom + } + bottom.updateLayout(primaryInputLayout) + bottom.setButtonsOnly(buttonsOnly) + bottom.setPinnedOnly(!drawn) + bottom.visibility = View.VISIBLE + return + } + hideBottomControls() + val wasDrawn = controls.visibility == View.VISIBLE && !controls.isPinnedOnly + if (!drawn) showPinned() else { controls.setPinnedOnly(false); controls.visibility = View.VISIBLE } + if (drawn == wasDrawn) return Log.i(TAG, "on-screen controls " + when { !drawn -> "hidden" buttonsOnly -> "Steam + QAM" @@ -4307,17 +4497,17 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { SessionState.mode == SessionService.MODE_STEAM && AgentGuest.steamKeyboard private fun releaseHeldGuestInput() { - guestButtonsDown.keys.toList().forEach { sendSceneButton(it, false) } - trackpadButtonsDown.toList().forEach { WaylandCompositor.nativeSendSceneInput(3, it, 0) } - trackpadButtonsDown.clear() + mouseBackInput.cancel() + heldButtons.release(1 or 2 or 8 or 16) guestKeysDown.toList().forEach { sendGuestKey(it, false) } + heldKeys.release(8 or 16 or 32) } - private fun sendGuestKey(key: Int, down: Boolean) { + private fun sendGuestKey(key: Int, down: Boolean, owner: Int = 1) { if (down && pipUi) return val code = if (key == KEY_RIGHTCTRL && SessionState.mode == SessionService.MODE_DESKTOP) KEY_LEFTCTRL else key - if (down) guestKeysDown.add(code) else guestKeysDown.remove(code) - if (down || code !in gestureKeysDown) WaylandCompositor.nativeSendKey(code, if (down) 1 else 0) + if (owner == 1) { if (down) guestKeysDown.add(code) else guestKeysDown.remove(code) } + heldKeys.set(owner, code, down) } private fun capturePausedFrame(manual: Boolean = false) { @@ -4393,13 +4583,15 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { pendingBackAction = null releaseDrawerDirection() onScreenControls?.releaseAll() - screenKeys?.releaseAll() + bottomInputControls?.releaseAll() releasePads() touchpad.cancel() scrollTouch.cancel() directTouchHold.cancel() twoFingerGestures.cancel() - releaseHeldGuestInput() + guestKeysDown.toList().forEach { sendGuestKey(it, false) } + heldKeys.clear() + heldButtons.clear() keyboard?.hide() sessionClipboard.stop() padMotion?.stop() @@ -4410,7 +4602,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { if (CompositorHost.isStarted) { WaylandCompositor.nativeSendTouch(3, -1, 0, 0) listOf(PointerGestures.BTN_LEFT, PointerGestures.BTN_RIGHT, PointerGestures.BTN_MIDDLE).forEach { - WaylandCompositor.nativeSendSceneInput(3, it, 0) + heldButtons.set(1, it, false) } WaylandCompositor.nativeSetScaleMode(SCALE_FIT, ALIGN_CENTER) } @@ -4459,6 +4651,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { for (name in arrayOf("peak_refresh_rate", "min_refresh_rate")) { contentResolver.registerContentObserver(android.provider.Settings.System.getUriFor(name), false, refreshSettingObserver) } + reconcileDock(shownOnDisplayId) settleDualScreenControls() refreshSecondScreenDisplays() if (secondScreenMode != SecondScreenMode.NONE && secondScreenPresentation == null) showSecondScreen(secondScreenMode) @@ -4479,7 +4672,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { displayManager.unregisterDisplayListener(displayListener) com.droiddeck.launcher.input.DualScreen.unwatchTaken(bottomScreenListener) contentResolver.unregisterContentObserver(refreshSettingObserver) - closeSecondScreen(reset = false) + if (!screenIsOff()) closeSecondScreen(reset = false) startedScreens = (startedScreens - 1).coerceAtLeast(0) updateGlassesScreen() updateBottomGame() @@ -4516,7 +4709,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { override fun onResume() { super.onResume() com.droiddeck.launcher.input.SystemKeyCapture.attach(this, { hasWindowFocus() && !appUiOwnsPointer() && CompositorHost.isStarted }) { event -> - com.droiddeck.launcher.input.GuestKeys.send(event, ::sendGuestKey) + com.droiddeck.launcher.input.GuestKeys.send(event) { code, down -> sendGuestKey(code, down, 8) } } if (refreshActivityDisplayId() && started) { SessionService.setActivityDisplay(this, sessionActivityDisplayId) @@ -4544,7 +4737,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { com.droiddeck.launcher.input.HomeButtonShortcuts.attach(this, enabled = { resumed && window.decorView.hasWindowFocus() && !ended && !drawerOpen && !pcKeyboardOpen && - !achievementsOpen && !gestureSettingsOpen && !pipUi && SessionState.running && !SessionState.suspended && + !achievementsOpen && !gestureSettingsOpen && !inputSettingsOpen && !pipUi && SessionState.running && !SessionState.suspended && SessionState.mode == SessionService.MODE_STEAM && padBridge != null }, setPressed = { padBridge?.setHomeGuidePressed(it) }, @@ -4555,6 +4748,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { updatePadMotion() ControllerPrefs.prefs(this).registerOnSharedPreferenceChangeListener(shortcutPrefListener) readPrefs() + refreshInputProfile() refreshControls() if (::pip.isInitialized) pip.resumed() if (CompositorHost.isStarted) applyFrameGen() @@ -4569,6 +4763,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { backQamInput.cancel() mouseBackInput.cancel() capturePausedFrame() + padBridge?.setInputEnabled(false) sessionClipboard.stop() if (::touchscreenGestures.isInitialized) touchscreenGestures.cancel() if (::touchpad.isInitialized) touchpad.cancel() @@ -4576,13 +4771,23 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { if (::twoFingerGestures.isInitialized) twoFingerGestures.cancel() uiHandler.removeCallbacks(clearRecognizedGesture) uiHandler.removeCallbacks(cursorResync) - gestureSettingsOpen = false - drawerOpen = false - pcKeyboardOpen = false - achievementsOpen = false + val screenOff = screenIsOff() + if (!screenOff) { + gestureSettingsOpen = false + inputSettingsOpen = false + } + releasePads() + heldKeys.clear() + heldButtons.clear() + if (!screenOff) { + drawerOpen = false + pcKeyboardOpen = false + achievementsOpen = false + } (getSystemService(INPUT_SERVICE) as? InputManager)?.unregisterInputDeviceListener(deviceListener) releaseDrawerDirection() onScreenControls?.releaseAll() + bottomInputControls?.releaseAll() if (::scrollTouch.isInitialized) scrollTouch.cancel() if (::padMouse.isInitialized) padMouse.reset() resumed = false @@ -4806,6 +5011,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { if (holder !== outputHolder()) return glassesOutputActive = glassesScreen?.holder === holder bottomOutputActive = bottomGame?.holder === holder + updateOnScreenPad() updatePointerCapture() if (bottomOutputActive) bottomRecoveries = 0 if (CompositorHost.isStarted) WaylandCompositor.nativeSetCursorComposite(glassesOutputActive || bottomOutputActive) @@ -4910,6 +5116,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { if (bottomGame !== screen) return bottomGame = null bottomOutputActive = false + updateOnScreenPad() surfaceW = 0 surfaceH = 0 CompositorHost.detach(screen.holder.surface) @@ -4937,13 +5144,16 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { val swapBack = { putGameOnBottom(false) } val steam = SessionState.mode == SessionService.MODE_STEAM val openMenu = { drawerOpen = true } - fun bar(title: String?, extras: List = emptyList()) = com.droiddeck.launcher.ui.BottomBarModel( - title, openMenu, openMenu, if (steam) ({ tapSteamButton() }) else null, if (steam) ({ tapQam() }) else null, - extras + com.droiddeck.launcher.ui.BarExtra(getString(R.string.swap_screens), icon = R.drawable.ic_bottom_swap, onClick = swapBack), - ) + fun bar(title: String?, extras: List = emptyList(), tabs: com.droiddeck.launcher.ui.BarTabs? = null) = + com.droiddeck.launcher.ui.BottomBarModel( + title, openMenu, openMenu, if (steam) ({ tapSteamButton() }) else null, if (steam) ({ tapQam() }) else null, + extras + com.droiddeck.launcher.ui.BarExtra(getString(R.string.swap_screens), icon = R.drawable.ic_bottom_swap, onClick = swapBack), + tabs = tabs, + ) val keyboardExtra = com.droiddeck.launcher.ui.BarExtra(getString(R.string.second_show_keyboard), icon = R.drawable.ic_bottom_keyboard) { keyboard?.toggle() } + val profileLayout = secondaryInputLayout androidx.compose.foundation.layout.Box( Modifier.fillMaxSize().background(androidx.compose.ui.graphics.Color.Black) .pointerInput(Unit) { awaitPointerEventScope { while (true) awaitPointerEvent() } }, @@ -4957,25 +5167,23 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { }, modifier = Modifier.fillMaxSize(), ) - secondScreenMode == SecondScreenMode.DECK_CONTROLS && SessionState.deckPad -> - androidx.compose.ui.viewinterop.AndroidView( - factory = { context -> - com.droiddeck.launcher.ui.DeckControlsPanel(context, onClose = openMenu, onSteamMenu = ::tapSteamButton, - onQam = ::tapQam, pad = padBridge, onMenu = openMenu, onSwap = swapBack) - }, - modifier = Modifier.fillMaxSize(), - ) - secondScreenMode == SecondScreenMode.KEYBOARD_TRACKPAD -> androidx.compose.foundation.layout.Column(Modifier.fillMaxSize()) { - com.droiddeck.launcher.ui.BottomScreenBar(bar(getString(SecondScreenMode.KEYBOARD_TRACKPAD.label), listOf(keyboardExtra))) - androidx.compose.ui.viewinterop.AndroidView( - factory = { context -> - com.droiddeck.launcher.ui.SessionTrackpadView(context, ::moveSecondScreenPointer, - ::sendTrackpadButton, - { steps -> WaylandCompositor.nativeSendSceneInput(4, steps, 0) }) - }, - modifier = Modifier.fillMaxWidth().weight(1f), - ) - } + secondScreenMode.controls && (secondScreenMode != SecondScreenMode.DECK_CONTROLS || SessionState.deckPad) -> + androidx.compose.foundation.layout.Column(Modifier.fillMaxSize()) { + when (secondScreenMode) { + SecondScreenMode.KEYBOARD_TRACKPAD -> + com.droiddeck.launcher.ui.BottomScreenBar(bar(getString(SecondScreenMode.KEYBOARD_TRACKPAD.label), listOf(keyboardExtra))) + SecondScreenMode.DECK_CONTROLS -> com.droiddeck.launcher.ui.BottomScreenBar(bar(getString(SecondScreenMode.DECK_CONTROLS.label), + tabs = com.droiddeck.launcher.ui.deckPanelTabs(this@SessionActivity, SessionPrefs.deckPanel(this@SessionActivity), ::selectDeckPanel))) + else -> com.droiddeck.launcher.ui.BottomScreenBar(bar(getString(secondScreenMode.label))) + } + androidx.compose.runtime.key(profileLayout) { + androidx.compose.ui.viewinterop.AndroidView( + factory = { context -> com.droiddeck.launcher.ui.InputSurfaceView(context, padBridge, profileLayout?.elements.orEmpty(), + surfaceStyle = profileLayout?.style ?: com.droiddeck.launcher.input.InputSurfaceStyle()) }, + modifier = Modifier.fillMaxWidth().weight(1f), + ) + } + } secondScreenMode == SecondScreenMode.TERMINAL -> androidx.compose.foundation.layout.Column(Modifier.fillMaxSize()) { var terminal by androidx.compose.runtime.remember { androidx.compose.runtime.mutableStateOf(null) } com.droiddeck.launcher.ui.BottomScreenBar(bar(getString(SecondScreenMode.TERMINAL.label), listOf( @@ -5183,6 +5391,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { } override fun onDestroy() { + if (AgentGuest.readyListener === agentReadyListener) AgentGuest.readyListener = null com.droiddeck.launcher.input.HomeButtonShortcuts.detach(this) relativeMouseRequests.shutdownNow() backQamInput.cancel() @@ -5199,6 +5408,8 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { closeSecondScreen(reset = false) glassesScope.cancel() glassesScreen?.let { screen -> glassesScreen = null; runCatching { screen.dismiss() } } + bottomInputControls?.let { (it.parent as? android.view.ViewGroup)?.removeView(it) } + bottomInputControls = null bottomGame?.let { screen -> bottomGame = null; runCatching { screen.dismiss() } } headTracker?.stop() if (::hud.isInitialized) hud.stop() @@ -5207,7 +5418,14 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { joyConVr = null handCamera?.stop() handCamera = null + inputMotion.stop() + padBridge?.setInputEnabled(false) + physicalInputBindings?.close() padBridge?.stop() + DeckControls.clearRouter(inputRouter) + inputRouter = null + heldKeys.clear() + heldButtons.clear() steamController?.stop() if (SessionState.padBridge === padBridge) SessionState.padBridge = null padMotion?.stop() @@ -5278,13 +5496,12 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { const val EXTRA_RETURN_HOME = "returnHome" const val EXTRA_LINK_TASK = "linkTask" const val EXTRA_STEAMVR = "steamVr" + const val EXTRA_PLAYED_GAME = "com.droiddeck.launcher.PLAYED_GAME" const val EXTRA_REPAIR_RUNTIME = "repairRuntime" private const val LEAVE_TIMEOUT_MS = 1_500L private const val TEXT_FIELD_TAP_MS = 1_000L private const val PAUSED_FRAME_MAX_EDGE = 1920f private const val PAUSED_FRAME_REDRAW_MS = 150L - private const val SETTINGS_WATCH_MS = 1_000L - private const val SETTINGS_WATCH_CHECKS = 600 private const val BOTTOM_RECOVERY_MS = 1_000L private const val BOTTOM_RECOVERY_TRIES = 3 private const val TAG = "SessionActivity" @@ -5326,6 +5543,8 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { private const val ALIGN_CENTER = 0 private const val ALIGN_PICTURE_TOP = 3 private const val ALIGN_PICTURE_BOTTOM = 4 + private const val ALIGN_PICTURE_LEFT = 5 + private const val ALIGN_PICTURE_RIGHT = 6 private val RUNTIME_LINES = PackageLines( R.string.session_runtime_downloading, R.string.session_runtime_downloading_mb, R.string.session_runtime_checking, R.string.session_runtime_unpacking, 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 c1955caa05..93922c8050 100644 --- a/app/src/main/java/com/droiddeck/launcher/agent/AgentBridgeProvider.kt +++ b/app/src/main/java/com/droiddeck/launcher/agent/AgentBridgeProvider.kt @@ -85,6 +85,10 @@ class AgentBridgeProvider : ContentProvider() { AgentAccess.requireCommands(context) guest(context, JSONObject(request.toString()).put("kind", "exec"), timeoutMs(request, 30.0, 10_000)) } + "native" -> { + AgentAccess.requireCommands(context) + AgentNative.run(context, request, timeoutMs(request, 30.0, 0)) + } "cdp" -> { AgentAccess.requireCommands(context) guest(context, JSONObject(request.toString()).put("kind", "cdp"), timeoutMs(request, 15.0, 5_000)) @@ -284,6 +288,8 @@ class AgentBridgeProvider : ContentProvider() { return JSONObject() .put("schema", SCHEMA) .put("build", BuildConfig.BUILD_LABEL) + .put("apkCommit", com.droiddeck.launcher.lab.LabOverlay.apkCommit) + .put("lab", com.droiddeck.launcher.lab.LabOverlay.active?.label ?: JSONObject.NULL) .put("appVersion", context.packageManager.getPackageInfo(context.packageName, 0).versionName) .put("runtime", JSONObject() .put("installed", LinuxRuntime.isInstalled(context)) diff --git a/app/src/main/java/com/droiddeck/launcher/agent/AgentCallReceiver.kt b/app/src/main/java/com/droiddeck/launcher/agent/AgentCallReceiver.kt new file mode 100644 index 0000000000..6ef8b2d4ba --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/agent/AgentCallReceiver.kt @@ -0,0 +1,41 @@ +package com.droiddeck.launcher.agent + +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent +import android.os.Bundle +import java.io.File + +/** + * The agent bridge for devices whose shell has no `content` command: the same calls as + * [AgentBridgeProvider], answered in a file under the app's external files folder. + */ +class AgentCallReceiver : BroadcastReceiver() { + + override fun onReceive(context: Context, intent: Intent) { + val method = intent.getStringExtra("method") ?: return + val id = intent.getStringExtra("id")?.takeIf { it.matches(Regex("[A-Za-z0-9_-]{1,64}")) } ?: return + val arg = intent.getStringExtra("arg") + val request = intent.getStringExtra(AgentBridgeProvider.EXTRA_REQUEST) + val pending = goAsync() + Thread({ + val json = try { + val extras = Bundle().apply { putString(AgentBridgeProvider.EXTRA_REQUEST, request) } + context.contentResolver.call("${context.packageName}.agent", method, arg, extras) + ?.getString(AgentBridgeProvider.RESULT_JSON) ?: """{"ok":false,"error":{"code":"NO_RESPONSE","message":"no response"}}""" + } catch (e: Exception) { + org.json.JSONObject().put("ok", false) + .put("error", org.json.JSONObject().put("code", "COMMAND_FAILED").put("message", e.message ?: e.javaClass.simpleName)).toString() + } + try { + val dir = File(context.getExternalFilesDir(null), "agent-calls").apply { mkdirs() } + File(dir, "$id.json.part").writeText(json) + File(dir, "$id.json.part").renameTo(File(dir, "$id.json")) + } catch (e: Exception) { + android.util.Log.w("AgentCallReceiver", "could not write the answer to $method: ${e.message}") + } finally { + pending.finish() + } + }, "agent-call-$id").start() + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/agent/AgentGuest.kt b/app/src/main/java/com/droiddeck/launcher/agent/AgentGuest.kt index eaa2bbc0f7..e04dda2580 100644 --- a/app/src/main/java/com/droiddeck/launcher/agent/AgentGuest.kt +++ b/app/src/main/java/com/droiddeck/launcher/agent/AgentGuest.kt @@ -33,6 +33,7 @@ object AgentGuest { var keyboardDismissals by mutableIntStateOf(0) private set @Volatile private var keyboardRoute = false + @Volatile var readyListener: (() -> Unit)? = null @Volatile private var routeDir: File? = null private const val TAG = "AgentGuest" private const val POLL_MS = 50L @@ -59,6 +60,7 @@ object AgentGuest { when (path) { "hello.json" -> readJson(File(dir, path))?.let { SessionEvents.record("agent.ready", mapOf("pid" to it.optInt("pid"), "focus" to it.optBoolean("focus"))) + readyListener?.invoke() } "focus.json" -> readJson(File(dir, path))?.let(::recordFocus) "keyboard.json" -> readJson(File(dir, path))?.let { recordKeyboard(this, it) } diff --git a/app/src/main/java/com/droiddeck/launcher/agent/AgentNative.kt b/app/src/main/java/com/droiddeck/launcher/agent/AgentNative.kt new file mode 100644 index 0000000000..613cbe2c39 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/agent/AgentNative.kt @@ -0,0 +1,65 @@ +package com.droiddeck.launcher.agent + +import android.content.Context +import com.droiddeck.launcher.runtime.LinuxRuntime +import com.droiddeck.launcher.runtime.NativeGuest +import org.json.JSONArray +import org.json.JSONObject +import java.util.concurrent.TimeUnit + +/** Runs a guest program without proot (`droiddeckctl native`), for testing the native userland. */ +object AgentNative { + private const val OUTPUT_CAP = 256 * 1024 + + fun run(context: Context, request: JSONObject, timeoutMs: Long): JSONObject { + val root = LinuxRuntime.rootDir(context) + com.droiddeck.launcher.session.SessionFiles.stage(context, root) + // with the setting off, staging leaves the rootfs as proot wants it + if (!com.droiddeck.launcher.session.SessionPrefs.nativeUserland(context)) NativeGuest.prepare(context, root) + if (!NativeGuest.available(root)) throw AgentException("NATIVE_UNAVAILABLE", "The rootfs has no native hook or glibc loader yet; start a session once") + val argvJson = request.optJSONArray("argv") ?: throw AgentException("INVALID_REQUEST", "argv is required") + val argv = (0 until argvJson.length()).map { argvJson.getString(it) } + if (argv.isEmpty()) throw AgentException("INVALID_REQUEST", "argv is empty") + val env = linkedMapOf() + request.optJSONObject("env")?.let { obj -> obj.keys().forEach { env[it] = obj.getString(it) } } + val preload = request.optJSONArray("preload")?.let { a -> (0 until a.length()).map { a.getString(it) } } ?: emptyList() + val builder = NativeGuest.builder(root, request.optString("cwd", "/root"), argv, env, preload) + val started = System.nanoTime() + val process = builder.start() + val out = StringBuilder() + val err = StringBuilder() + val readers = listOf( + Thread { drain(process.inputStream, out) }, + Thread { drain(process.errorStream, err) }, + ).onEach { it.isDaemon = true; it.start() } + // written beside the wait: a program that never reads more than a pipe holds would block it + val input = request.optString("stdin").toByteArray() + Thread { + try { + process.outputStream.use { stdin -> if (input.isNotEmpty()) stdin.write(input) } + } catch (_: java.io.IOException) { + } + }.apply { isDaemon = true; start() } + val finished = process.waitFor(timeoutMs, TimeUnit.MILLISECONDS) + if (!finished) process.destroyForcibly().waitFor() + readers.forEach { it.join(2000) } + val code = if (finished) process.exitValue() else -1 + return JSONObject().put("ok", true).put("command", "native") + .put("exitCode", code).put("timedOut", !finished) + .put("ms", (System.nanoTime() - started) / 1_000_000) + .put("stdout", JSONObject().put("text", out.toString())) + .put("stderr", JSONObject().put("text", err.toString())) + .put("argv", JSONArray(argv)) + } + + private fun drain(input: java.io.InputStream, into: StringBuilder) { + val buffer = ByteArray(8192) + val text = java.io.ByteArrayOutputStream() + while (true) { + val n = try { input.read(buffer) } catch (e: java.io.IOException) { -1 } + if (n < 0) break + if (text.size() < OUTPUT_CAP) text.write(buffer, 0, minOf(n, OUTPUT_CAP - text.size())) + } + into.append(text.toString(Charsets.UTF_8.name())) + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/core/AddonPack.kt b/app/src/main/java/com/droiddeck/launcher/core/AddonPack.kt index 4f88d90e34..d3d5cf2c74 100644 --- a/app/src/main/java/com/droiddeck/launcher/core/AddonPack.kt +++ b/app/src/main/java/com/droiddeck/launcher/core/AddonPack.kt @@ -152,5 +152,7 @@ internal class AddonPack( @JvmField val POWERVR = AddonPack("powervr", BuildConfig.PACK_POWERVR_SHA256, BuildConfig.PACK_POWERVR_SIZE, listOf("runtime.tzst", "version.txt")) + val UBISOFT = AddonPack("ubisoft", BuildConfig.PACK_UBISOFT_SHA256, BuildConfig.PACK_UBISOFT_SIZE, + listOf("droiddeck-upc.exe")) } } 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 cb0b9487b8..63037a24ac 100644 --- a/app/src/main/java/com/droiddeck/launcher/core/DeviceReport.kt +++ b/app/src/main/java/com/droiddeck/launcher/core/DeviceReport.kt @@ -278,6 +278,7 @@ object DeviceReport { k("RAM cap", SessionPrefs.ramCapMb(context).let { if (it > 0) "$it MB" else "off" }) k("VRAM cap", SessionPrefs.vramCapMb(context).let { if (it > 0) "$it MB" else "driver default" }) k("proot without seccomp", SessionPrefs.prootNoSeccomp(context)) + k("native userland (no proot)", SessionPrefs.nativeUserland(context)) k("proot fast path", SessionPrefs.prootFastPath(context)) k("proot binary", LinuxRuntime.prootSource(context) + " " + LinuxRuntime.prootBinary(context).path) k("proot fast path (eligible)", com.droiddeck.launcher.runtime.ProotFastPath.enabled(context)) diff --git a/app/src/main/java/com/droiddeck/launcher/core/Downloader.java b/app/src/main/java/com/droiddeck/launcher/core/Downloader.java index 19cec5acbe..9bd86c5719 100644 --- a/app/src/main/java/com/droiddeck/launcher/core/Downloader.java +++ b/app/src/main/java/com/droiddeck/launcher/core/Downloader.java @@ -58,7 +58,7 @@ public final class Downloader { private static void closeAsync(InputStream active) { CANCEL_EXECUTOR.execute(() -> { - try { active.close(); } catch (IOException ignored) { } + try { active.close(); } catch (IOException | IllegalStateException ignored) { } }); } diff --git a/app/src/main/java/com/droiddeck/launcher/core/Hashes.kt b/app/src/main/java/com/droiddeck/launcher/core/Hashes.kt index 0e4dce5001..9f133dcf40 100644 --- a/app/src/main/java/com/droiddeck/launcher/core/Hashes.kt +++ b/app/src/main/java/com/droiddeck/launcher/core/Hashes.kt @@ -12,6 +12,9 @@ object Hashes { @JvmStatic fun sha512(file: File): String = hex(file, "SHA-512") + @JvmStatic + fun md5(file: File): String = hex(file, "MD5") + @JvmStatic fun isGithubSha256(digest: String?): Boolean = digest != null && digest.matches(GITHUB_SHA256) diff --git a/app/src/main/java/com/droiddeck/launcher/files/StorageRoots.kt b/app/src/main/java/com/droiddeck/launcher/files/StorageRoots.kt index b19a2e9874..825c2e24c6 100644 --- a/app/src/main/java/com/droiddeck/launcher/files/StorageRoots.kt +++ b/app/src/main/java/com/droiddeck/launcher/files/StorageRoots.kt @@ -90,7 +90,8 @@ object StorageRoots { .filter { present(it.state) } .forEachIndexed { index, volume -> val uuid = volume.uuid?.takeIf { it.isNotBlank() } - val dirName = volume.directory?.name?.takeIf { it.isNotBlank() } + val directory = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) volume.directory else null + val dirName = directory?.name?.takeIf { it.isNotBlank() } val descSlug = volume.getDescription(context)?.trim() ?.takeIf { it.isNotBlank() && !it.equals("android", ignoreCase = true) } ?.replace(Regex("[^A-Za-z0-9._-]+"), "-") @@ -98,9 +99,7 @@ object StorageRoots { entry.removable = !volume.isPrimary && volume.isRemovable if (entry.label == null) entry.label = labelFor(context, volume, uuid ?: dirName ?: descSlug) - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { - volume.directory?.let { entry.candidates += it } - } + directory?.let { entry.candidates += it } val name = uuid ?: dirName if (name != null) { entry.candidates += File("/storage/$name") diff --git a/app/src/main/java/com/droiddeck/launcher/frontend/AddedGames.kt b/app/src/main/java/com/droiddeck/launcher/frontend/AddedGames.kt index 7057cadd39..c7d774bf2e 100644 --- a/app/src/main/java/com/droiddeck/launcher/frontend/AddedGames.kt +++ b/app/src/main/java/com/droiddeck/launcher/frontend/AddedGames.kt @@ -417,7 +417,7 @@ object AddedGames { private fun scanGame(context: Context, folder: File, out: MutableList, single: AddedExes.Entry? = null, reuse: Boolean = false) { run { - val sidecar = StoreGameSidecar.read(folder) + val sidecar = StoreGameSidecar.read(folder)?.let { com.droiddeck.launcher.stores.StoreLaunch.adopt(folder, it) } sidecar?.takeIf { it.id.isNotEmpty() }?.let { SessionPrefs.followStoreGame(context, "${it.store.id}:${it.id}", folder.path) { old -> !File(old).exists() } } val ranking = rank(folder, reuse) val candidates = ranking.exes @@ -457,7 +457,11 @@ object AddedGames { steamId?.toLong() ?: ((appId shl 32) or 0x02000000L), candidates, steamId, source = sidecar?.store?.id ?: Library.ADDED, storeId = sidecar?.id, exeUncertain = sidecar == null && chosen == null && adopted == null && ranking.uncertain, - launchOptions = if (sidecar == null) SessionPrefs.addedGameLaunch(context, folder.path) else "", + launchOptions = when { + sidecar == null -> SessionPrefs.addedGameLaunch(context, folder.path) + sidecar.store == Store.UBISOFT -> com.droiddeck.launcher.stores.ubisoft.UbisoftConnect.launchOptions(context) + else -> "" + }, single = single != null)) } } diff --git a/app/src/main/java/com/droiddeck/launcher/frontend/Library.kt b/app/src/main/java/com/droiddeck/launcher/frontend/Library.kt index ef7ec06a10..b303ecc71e 100644 --- a/app/src/main/java/com/droiddeck/launcher/frontend/Library.kt +++ b/app/src/main/java/com/droiddeck/launcher/frontend/Library.kt @@ -18,9 +18,10 @@ object Library { val source: String = SOURCE_STEAM, val storeId: String? = null, val logo: File? = null, + val installedAt: Long = 0L, ) { fun withArt(art: File?, hero: File?, icon: File?, logo: File? = this.logo) = - SteamGame(appId, name, art, library, gameId, hero, lastPlayed, gameFiles, protonPrefix, icon, source, storeId, logo) + SteamGame(appId, name, art, library, gameId, hero, lastPlayed, gameFiles, protonPrefix, icon, source, storeId, logo, installedAt) val gameIdString: String get() = java.lang.Long.toUnsignedString(gameId) val launchKey: String get() = if (storeId != null) "$source:$storeId" else gameIdString @@ -31,6 +32,7 @@ object Library { } } const val ADDED = "added" + private const val PLAYED = "added_games_played" const val EPIC_LAUNCHER = "epic-launcher" const val EPIC_LAUNCHER_NAME = "Epic Games Launcher" const val BATTLENET_LAUNCHER = "battlenet-launcher" @@ -221,6 +223,18 @@ object Library { return (steam + own.map { g -> addedGame(context, g, roots, played[g.appId] ?: 0L) }).distinctBy { it.gameId } } + fun recordPlayed(context: Context, game: SteamGame) { + if (game.library == ADDED) recordPlayed(context, game.gameIdString) + } + + fun recordPlayed(context: Context, gameId: String) { + context.getSharedPreferences(PLAYED, Context.MODE_PRIVATE).edit() + .putLong(gameId, System.currentTimeMillis() / 1000).apply() + } + + internal fun recordedPlay(context: Context, gameId: Long): Long = + context.getSharedPreferences(PLAYED, Context.MODE_PRIVATE).getLong(java.lang.Long.toUnsignedString(gameId), 0L) + internal fun shortcutsLastPlayed(root: File): Map = runCatching { val config = AddedGames.accountConfig(root) ?: return emptyMap() val bytes = File(config, "shortcuts.vdf").takeIf { it.isFile }?.readBytes() ?: return emptyMap() @@ -249,12 +263,13 @@ object Library { AddedGameArt.resolve(context, g).let { art -> SteamGame( g.steamAppId ?: g.appId.toInt(), g.name, art.portrait ?: art.header, ADDED, g.gameId, - hero = art.hero ?: art.header, lastPlayed = lastPlayed, gameFiles = g.folder, + hero = art.hero ?: art.header, lastPlayed = maxOf(lastPlayed, recordedPlay(context, g.gameId)), gameFiles = g.folder, protonPrefix = if (g.source == com.droiddeck.launcher.stores.Store.BATTLENET.id) File(com.droiddeck.launcher.runtime.BattleNet.prefix(context), "pfx").takeIf { it.isDirectory } else protonPrefix(context, g.steamAppId?.toLong() ?: g.appId, libraries = roots), icon = art.icon?.takeIf { it.extension.lowercase() != "ico" }, source = g.source, storeId = g.storeId, logo = art.logo, + installedAt = g.folder.lastModified(), ) } diff --git a/app/src/main/java/com/droiddeck/launcher/frontend/LibraryCache.kt b/app/src/main/java/com/droiddeck/launcher/frontend/LibraryCache.kt index 6fa35c753a..81434ce2ca 100644 --- a/app/src/main/java/com/droiddeck/launcher/frontend/LibraryCache.kt +++ b/app/src/main/java/com/droiddeck/launcher/frontend/LibraryCache.kt @@ -27,6 +27,7 @@ object LibraryCache { source = o.optString("source").ifEmpty { if (o.getString("library") == Library.ADDED) Library.ADDED else Library.SOURCE_STEAM }, storeId = o.optString("storeId").ifEmpty { null }, logo = o.file("logo"), + installedAt = o.optLong("installedAt"), ) } to x64 }.getOrElse { e -> Log.w(TAG, "unreadable, ignored: ${e.message}"); Pair(emptyList(), false) } @@ -44,7 +45,7 @@ object LibraryCache { JSONObject().put("appId", g.appId).put("name", g.name).put("library", g.library).put("gameId", g.gameId) .put("lastPlayed", g.lastPlayed).putFile("art", g.art).putFile("hero", g.hero) .putFile("gameFiles", g.gameFiles).putFile("protonPrefix", g.protonPrefix).putFile("icon", g.icon).putFile("logo", g.logo) - .put("source", g.source).apply { g.storeId?.let { put("storeId", it) } }, + .put("source", g.source).put("installedAt", g.installedAt).apply { g.storeId?.let { put("storeId", it) } }, ) val f = file(context) val tmp = File(f.path + ".tmp") diff --git a/app/src/main/java/com/droiddeck/launcher/frontend/WinCompSources.kt b/app/src/main/java/com/droiddeck/launcher/frontend/WinCompSources.kt index 22044c76d4..e9910a67df 100644 --- a/app/src/main/java/com/droiddeck/launcher/frontend/WinCompSources.kt +++ b/app/src/main/java/com/droiddeck/launcher/frontend/WinCompSources.kt @@ -9,7 +9,7 @@ import java.io.File import java.util.Locale object WinCompSources { - enum class Origin { STEAM, GOG, EPIC, AMAZON, STEAM_NAME, FOLDER, XBOX } + enum class Origin { STEAM, GOG, EPIC, AMAZON, STEAM_NAME, FOLDER, XBOX, UBISOFT } data class Finding(val component: String?, val original: String, val origin: Origin, val auto: Boolean, val args: List = emptyList()) @@ -83,6 +83,13 @@ object WinCompSources { } } + /** The prerequisite installers a Ubisoft manifest lists, with `$INSTALL_DIR` dropped. */ + internal fun ubisoftInstallRuns(setup: String?): List { + val runs = setup?.let { runCatching { org.json.JSONObject(it).optJSONArray("installRuns") }.getOrNull() } ?: return emptyList() + return (0 until runs.length()).mapNotNull { runs.optJSONObject(it)?.optString("exe") } + .map { it.replace('\\', '/').removePrefix("\$INSTALL_DIR").trimStart('/') }.filter { it.isNotEmpty() } + } + fun forGame(context: Context, game: Library.SteamGame): List { val folder = game.gameFiles val out = ArrayList() @@ -112,6 +119,12 @@ object WinCompSources { if (parts.isEmpty()) out += Finding(null, original, Origin.AMAZON, false) parts.forEach { out += Finding(it, original, Origin.AMAZON, true) } } + store == Store.UBISOFT -> ubisoftInstallRuns(sidecar?.extra?.get(com.droiddeck.launcher.stores.ubisoft.UbisoftLaunch.SETUP)).forEach { exe -> + val parts = installer(exe) + val original = exe.substringAfterLast('/') + if (parts.isEmpty()) out += Finding(null, original, Origin.UBISOFT, false) + parts.forEach { out += Finding(it, original, Origin.UBISOFT, true) } + } else -> {} } if (game.library == Library.ADDED && folder != null) SteamMatch.get(context, folder.path)?.let { match -> diff --git a/app/src/main/java/com/droiddeck/launcher/gpu/AutoPicks.kt b/app/src/main/java/com/droiddeck/launcher/gpu/AutoPicks.kt index 6216ccf7d3..f48e788bba 100644 --- a/app/src/main/java/com/droiddeck/launcher/gpu/AutoPicks.kt +++ b/app/src/main/java/com/droiddeck/launcher/gpu/AutoPicks.kt @@ -27,12 +27,11 @@ object AutoPicks { } val BUILT_IN = listOf( - Rule(setOf(Family.A8XX), models = setOf(840), prefer = listOf(NEWEST_DD_TURNIP, DriverPairs.WN_BALANCED)), - Rule(setOf(Family.A8XX), prefer = listOf(DriverPairs.WN_BALANCED)), + Rule(setOf(Family.A8XX), prefer = listOf(NEWEST_DD_TURNIP, DriverPairs.WN_BALANCED)), Rule(setOf(Family.A7XX_LOW), prefer = listOf(NEWEST_DD_TURNIP, DriverPairs.BANNER_710)), - Rule(setOf(Family.A7XX), oneUi8Gen2 = true, prefer = listOf(DriverPairs.BANNER_ONEUI)), + Rule(setOf(Family.A7XX), oneUi8Gen2 = true, prefer = listOf(NEWEST_DD_TURNIP, DriverPairs.BANNER_ONEUI)), Rule(setOf(Family.A7XX), prefer = listOf(NEWEST_DD_TURNIP, DriverPairs.BANNER)), - Rule(setOf(Family.A6XX), prefer = listOf(DriverPairs.BANNER)), + Rule(setOf(Family.A6XX), prefer = listOf(NEWEST_DD_TURNIP, DriverPairs.BANNER)), Rule(setOf(Family.ADRENO_UNKNOWN), minSdk = 36, prefer = listOf(DriverPairs.WN_BALANCED)), Rule(setOf(Family.ADRENO_UNKNOWN), prefer = listOf(DriverPairs.BANNER)), ) diff --git a/app/src/main/java/com/droiddeck/launcher/gpu/DriverBundle.kt b/app/src/main/java/com/droiddeck/launcher/gpu/DriverBundle.kt index 594dbebb27..319a123811 100644 --- a/app/src/main/java/com/droiddeck/launcher/gpu/DriverBundle.kt +++ b/app/src/main/java/com/droiddeck/launcher/gpu/DriverBundle.kt @@ -159,6 +159,12 @@ object DriverBundle { SessionPrefs.setLinuxDriver(context, bundle.linuxId) } + fun selectDisplay(context: Context, id: String) { + val left = active(context) + SessionPrefs.setAndroidDriver(context, id) + if (left != null && left.androidId != id) SessionPrefs.setLinuxDriver(context, "") + } + fun remove(context: Context, bundle: Bundle) { TurnipDriver(context).remove(bundle.androidId) LinuxVulkanDriverManager(context).removeDriver(bundle.linuxId) diff --git a/app/src/main/java/com/droiddeck/launcher/gpu/SystemVulkanDriver.java b/app/src/main/java/com/droiddeck/launcher/gpu/SystemVulkanDriver.java index 25a6e363c6..1106ac9602 100644 --- a/app/src/main/java/com/droiddeck/launcher/gpu/SystemVulkanDriver.java +++ b/app/src/main/java/com/droiddeck/launcher/gpu/SystemVulkanDriver.java @@ -191,6 +191,7 @@ public final class SystemVulkanDriver { /** Collect abandoned stages, and obsolete versions once older guests can no longer load them. */ public static synchronized void pruneUnused(Context context, boolean guestsStopped) throws IOException { + if (!AddonPack.POWERVR.installed(context)) return; pruneUnused(runtimeDirectory(context), guestsStopped); } diff --git a/app/src/main/java/com/droiddeck/launcher/gpu/TurnipReleases.kt b/app/src/main/java/com/droiddeck/launcher/gpu/TurnipReleases.kt index 1936779cbd..020285d073 100644 --- a/app/src/main/java/com/droiddeck/launcher/gpu/TurnipReleases.kt +++ b/app/src/main/java/com/droiddeck/launcher/gpu/TurnipReleases.kt @@ -97,6 +97,7 @@ object TurnipReleases { // Venus): its driver pairs fall past the first 15, so it is read in one full page. val perPage = if (src.repo == NON_ADRENO_REPO) 100 else 15 val releases = JSONArray(get(context, "https://api.github.com/repos/${src.repo}/releases?per_page=$perPage")) + val found = JSONArray() val seen = HashSet() var newest: String? = null for (i in 0 until releases.length()) { @@ -114,12 +115,13 @@ object TurnipReleases { val label = kind.label val sha = Hashes.githubSha256(a.optString("digest")) ?: continue if (!seen.add(if (kind.bundle) kind.pair else "$linux|$label")) continue - assets.put(JSONObject().put("source", src.label).put("tag", tag).put("name", name) + found.put(JSONObject().put("source", src.label).put("tag", tag).put("name", name) .put("url", a.getString("browser_download_url")).put("size", a.optLong("size")) .put("linux", linux).put("label", label).put("sha256", sha).put("pair", kind.pair) .put("bundle", kind.bundle).put("id", a.optLong("id")).put("updated_at", a.optString("updated_at"))) } } + for (i in 0 until found.length()) assets.put(found.get(i)) if (newest != null) latest.put(JSONObject().put("source", src.label).put("tag", newest)) } catch (e: Exception) { failed.add(src.label) @@ -127,7 +129,8 @@ object TurnipReleases { } } if (failed.size == sources.size) throw lastError ?: IOException("no source answered") - runCatching { get(context, AutoPicks.URL, AutoPicks.URL) } + carryOver(failed, prefs(context).getString(KEY_RELEASE, null)?.let { runCatching { JSONObject(it) }.getOrNull() }, assets, latest) + runCatching { fetch(context, AutoPicks.URL, AutoPicks.URL) } .onFailure { Log.i(TAG, "auto picks: ${it.message}") } .getOrNull()?.takeIf { AutoPicks.parse(it) != null } ?.let { prefs(context).edit().putString(KEY_PICKS, it).apply() } @@ -137,12 +140,22 @@ object TurnipReleases { return parse(stored) } + internal var fetch: (Context, String, String) -> String = { context, source, url -> get(context, source, url) } + + internal fun carryOver(failed: List, previous: JSONObject?, assets: JSONArray, latest: JSONArray) { + if (previous == null || failed.isEmpty()) return + for ((key, into) in listOf("assets" to assets, "latest" to latest)) { + val old = previous.optJSONArray(key) ?: continue + for (i in 0 until old.length()) old.optJSONObject(i)?.takeIf { it.optString("source") in failed }?.let(into::put) + } + } + private fun get(context: Context, url: String): String { val sources = DownloadSource.candidates(url) var last: IOException? = null for ((index, source) in sources.withIndex()) { try { - val body = get(context, source, url) + val body = fetch(context, source, url) DownloadSource.succeeded(source) return body } catch (e: IOException) { diff --git a/app/src/main/java/com/droiddeck/launcher/hud/DroidDeckHudView.kt b/app/src/main/java/com/droiddeck/launcher/hud/DroidDeckHudView.kt index b5dbb54ef3..a353a6d201 100644 --- a/app/src/main/java/com/droiddeck/launcher/hud/DroidDeckHudView.kt +++ b/app/src/main/java/com/droiddeck/launcher/hud/DroidDeckHudView.kt @@ -534,9 +534,6 @@ class DroidDeckHudView(context: Context) : View(context) { private val inkRect = android.graphics.Rect() private fun rebuild() { - val renderingLayer = if (size == DroidDeckHudSize.STRIP && dock == DroidDeckHudDock.NONE) - LAYER_TYPE_SOFTWARE else LAYER_TYPE_NONE - if (layerType != renderingLayer) setLayerType(renderingLayer, null) glyphs.clear(); tileRects.clear(); graphRect = null headerMark = null; headerWordmark = null; chipRect = null; dPanel = null; dDividerTop = -1f; tabRect = null; stripRect = null val s = snap @@ -1353,10 +1350,15 @@ class DroidDeckHudView(context: Context) : View(context) { canvas.drawText(g.text, g.x, g.baseline, paint) if (g.glow) paint.clearShadowLayer() } - headerMark?.let { DroidDeckMark.draw(canvas, it.left, it.top, it.height(), blend(colValue), accent) } + headerMark?.let { + if (stripRect != null && layerType != LAYER_TYPE_SOFTWARE) drawCachedMark(canvas, it) + else DroidDeckMark.draw(canvas, it.left, it.top, it.height(), blend(colValue), accent) + } headerWordmark?.let { WordmarkArt.draw(canvas, it.left, it.top, it.height(), blend(colValue), accent) } - graphRect?.let { drawGraph(canvas, it) } + graphRect?.let { + if (stripRect != null && layerType != LAYER_TYPE_SOFTWARE) drawCachedGraph(canvas, it) else drawGraph(canvas, it) + } lockController.drawBadge(canvas) } @@ -1381,6 +1383,41 @@ class DroidDeckHudView(context: Context) : View(context) { canvas.drawPath(graphPath, graphPaint) } + private var cachedGraphBitmap: Bitmap? = null + private var cachedGraphCanvas: Canvas? = null + private val cachedGraphRect = RectF() + private var cachedMarkBitmap: Bitmap? = null + private var cachedMarkKey = "" + + private fun drawCachedGraph(canvas: Canvas, rect: RectF) { + val w = rect.width().roundToInt().coerceAtLeast(1) + val h = rect.height().roundToInt().coerceAtLeast(1) + var bitmap = cachedGraphBitmap + if (bitmap == null || bitmap.width != w || bitmap.height != h) { + bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888) + cachedGraphBitmap = bitmap + cachedGraphCanvas = Canvas(bitmap) + cachedGraphRect.set(0f, 0f, w.toFloat(), h.toFloat()) + } + bitmap.eraseColor(Color.TRANSPARENT) + drawGraph(cachedGraphCanvas!!, cachedGraphRect) + canvas.drawBitmap(bitmap, rect.left, rect.top, null) + } + + private fun drawCachedMark(canvas: Canvas, rect: RectF) { + val w = rect.width().roundToInt().coerceAtLeast(1) + val h = rect.height().roundToInt().coerceAtLeast(1) + val key = "$w,$h,${blend(colValue)},$accent" + var bitmap = cachedMarkBitmap + if (bitmap == null || key != cachedMarkKey) { + bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888) + DroidDeckMark.draw(Canvas(bitmap), 0f, 0f, h.toFloat(), blend(colValue), accent) + cachedMarkBitmap = bitmap + cachedMarkKey = key + } + canvas.drawBitmap(bitmap, rect.left, rect.top, null) + } + private fun drawStrip(canvas: Canvas, rect: RectF) { val p = parent as? View val atBottom = p != null && p.height > 0 && y + height / 2f > p.height / 2f diff --git a/app/src/main/java/com/droiddeck/launcher/hud/GameFrames.kt b/app/src/main/java/com/droiddeck/launcher/hud/GameFrames.kt index 98dc35074f..2aba209ded 100644 --- a/app/src/main/java/com/droiddeck/launcher/hud/GameFrames.kt +++ b/app/src/main/java/com/droiddeck/launcher/hud/GameFrames.kt @@ -6,6 +6,7 @@ import kotlin.math.roundToInt internal class FrameStats { private val graph = FloatArray(GRAPH_FRAMES) private var graphHead = 0 + private var graphFrames = 0L private var lastPresentNs = 0L private var lastFpsUpdateNs = 0L private var framesSinceUpdate = 0 @@ -38,6 +39,7 @@ internal class FrameStats { if (lastPresentNs != 0L) { graph[graphHead] = frametimeNs / 1e6f graphHead = (graphHead + 1) % GRAPH_FRAMES + graphFrames++ } val elapsed = timeNs - lastFpsUpdateNs if (elapsed >= FPS_PERIOD_NS) { @@ -68,6 +70,7 @@ internal class FrameStats { fun reset() { graph.fill(0f) graphHead = 0 + graphFrames = 0L lastPresentNs = 0L lastFpsUpdateNs = 0L framesSinceUpdate = 0 @@ -86,8 +89,9 @@ internal class FrameStats { fun frametimeMs(): Float = 1000f / maxOf(fps, 1f) @Synchronized - fun graph(out: FloatArray) { + fun graph(out: FloatArray): Long { for (i in 0 until GRAPH_FRAMES) out[i] = graph[(graphHead + i) % GRAPH_FRAMES] + return graphFrames } @Synchronized diff --git a/app/src/main/java/com/droiddeck/launcher/input/BottomScreenGuests.kt b/app/src/main/java/com/droiddeck/launcher/input/BottomScreenGuests.kt index ae44dc0e6c..574536e18a 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/BottomScreenGuests.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/BottomScreenGuests.kt @@ -9,6 +9,7 @@ import android.hardware.display.DisplayManager import android.os.Build import android.os.Handler import android.os.HandlerThread +import android.os.SystemClock import android.util.Log import android.view.Display import android.view.accessibility.AccessibilityEvent @@ -33,6 +34,8 @@ object BottomScreenGuests { else -> null } + internal fun current(arrivedAt: Long, ownShownAt: Long?): Boolean = ownShownAt == null || arrivedAt >= ownShownAt + internal fun ignorable(type: Int): Boolean = type == AccessibilityWindowInfo.TYPE_INPUT_METHOD || type == AccessibilityWindowInfo.TYPE_ACCESSIBILITY_OVERLAY || type == AccessibilityWindowInfo.TYPE_SPLIT_SCREEN_DIVIDER || @@ -43,7 +46,7 @@ object BottomScreenGuests { } class BottomScreenWatch(private val service: AccessibilityService) { - private class Arrival(val pkg: String) + private class Arrival(val pkg: String, val at: Long) private val thread = HandlerThread("bottom-screen").apply { start() } private val handler = Handler(thread.looper) @@ -52,6 +55,8 @@ class BottomScreenWatch(private val service: AccessibilityService) { private var homes: Set = emptySet() private var keyboards: Set = emptySet() private var holders: Map = emptyMap() + private var ownWindows: Map> = emptyMap() + private val ownShownAt = mutableMapOf() private val check = Runnable { evaluate() } fun start() { @@ -72,7 +77,7 @@ class BottomScreenWatch(private val service: AccessibilityService) { AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED -> { val pkg = event.packageName?.toString() ?: return if (event.displayId == Display.DEFAULT_DISPLAY || pkg == own || pkg == SYSTEM_UI) return - arrivals[event.displayId] = Arrival(pkg) + arrivals[event.displayId] = Arrival(pkg, event.eventTime) } AccessibilityEvent.TYPE_WINDOWS_CHANGED -> Unit else -> return @@ -97,19 +102,25 @@ class BottomScreenWatch(private val service: AccessibilityService) { val all = runCatching { service.windowsOnAllDisplays }.getOrNull() ?: return val displays = service.getSystemService(DisplayManager::class.java) ?: return val taken = mutableMapOf() + val shown = mutableMapOf>() + val now = SystemClock.uptimeMillis() for (i in 0 until all.size()) { val id = all.keyAt(i) if (id == Display.DEFAULT_DISPLAY) continue val display = displays.getDisplay(id)?.takeIf(DualScreen::builtIn) ?: continue val windows = all.valueAt(i) - val topPkg = windows.asSequence() .filter { !BottomScreenGuests.ignorable(it.type) && cover(it, display) >= BottomScreenGuests.MIN_COVER } .sortedByDescending { it.layer } - .firstNotNullOfOrNull { packageOf(it)?.takeUnless { pkg -> pkg == own } } - val arrival = seen[id] - if (topPkg != null && arrival != null) arrivals.remove(id, arrival) + .map { it to packageOf(it) } + val ours = windows.filter { it.second == own }.map { it.first.id }.toSet() + if (!ownWindows[id].orEmpty().containsAll(ours)) ownShownAt[id] = now + shown[id] = ours + val topPkg = windows.firstNotNullOfOrNull { it.second?.takeUnless { pkg -> pkg == own } } + val arrival = seen[id]?.takeIf { BottomScreenGuests.current(it.at, ownShownAt[id]) } + seen[id]?.takeIf { topPkg != null || arrival == null }?.let { arrivals.remove(id, it) } BottomScreenGuests.holder(topPkg, arrival?.pkg?.takeUnless { it in keyboards }, homes)?.let { taken[id] = it } } + ownWindows = shown if (current !== this) return if (taken != holders) Log.i(TAG, "bottom screen holders: $taken") holders = taken diff --git a/app/src/main/java/com/droiddeck/launcher/input/ControllerEditorActivity.kt b/app/src/main/java/com/droiddeck/launcher/input/ControllerEditorActivity.kt deleted file mode 100644 index cb544d5a4d..0000000000 --- a/app/src/main/java/com/droiddeck/launcher/input/ControllerEditorActivity.kt +++ /dev/null @@ -1,136 +0,0 @@ -package com.droiddeck.launcher.input - -import com.droiddeck.launcher.ui.focusGlide -import android.os.Build -import android.os.Bundle -import android.view.Gravity -import android.view.View -import android.view.WindowManager -import android.widget.FrameLayout -import androidx.activity.ComponentActivity -import androidx.compose.foundation.background -import androidx.compose.foundation.clickable -import androidx.compose.foundation.layout.Arrangement -import androidx.compose.foundation.layout.Column -import androidx.compose.foundation.layout.Row -import androidx.compose.foundation.layout.padding -import androidx.compose.foundation.selection.toggleable -import androidx.compose.foundation.shape.RoundedCornerShape -import androidx.compose.material3.MaterialTheme -import androidx.compose.material3.Text -import androidx.compose.ui.Alignment -import androidx.compose.runtime.Composable -import androidx.compose.runtime.getValue -import androidx.compose.runtime.mutableStateOf -import androidx.compose.runtime.remember -import androidx.compose.runtime.setValue -import androidx.compose.ui.Modifier -import androidx.compose.ui.draw.clip -import androidx.compose.ui.graphics.Color -import androidx.compose.ui.semantics.Role -import androidx.compose.ui.text.font.FontWeight -import androidx.compose.ui.platform.ComposeView -import androidx.compose.ui.unit.dp -import androidx.compose.ui.unit.sp -import androidx.compose.ui.res.stringResource -import com.droiddeck.launcher.R -import com.droiddeck.launcher.ui.DroidDeckTheme -import com.droiddeck.launcher.ui.onSignal -import com.droiddeck.launcher.ui.LocalPalette - -class ControllerEditorActivity : ComponentActivity() { - override fun attachBaseContext(newBase: android.content.Context) { - super.attachBaseContext(newBase) - com.droiddeck.launcher.core.AppLanguage.applyTo(this, newBase) - } - - private lateinit var controls: OnScreenControls - - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - requestedOrientation = com.droiddeck.launcher.session.SessionPrefs.orientation(this) - immersive() - val root = FrameLayout(this) - root.setBackgroundColor(0xFF0B0F14.toInt()) - controls = OnScreenControls(this, null, editing = true) - root.addView(controls, FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT)) - val bar = ComposeView(this).apply { - setContent { - DroidDeckTheme { - var snap by remember { mutableStateOf(ControllerPrefs.read(context).snap) } - Column( - horizontalAlignment = Alignment.CenterHorizontally, - verticalArrangement = Arrangement.spacedBy(5.dp), - modifier = Modifier - .background(MaterialTheme.colorScheme.surface.copy(alpha = 0.9f), RoundedCornerShape(20.dp)) - .padding(start = 5.dp, end = 5.dp, top = 8.dp, bottom = 5.dp), - ) { - Text(stringResource(R.string.ctrl_editor_drag), fontSize = 12.sp, color = MaterialTheme.colorScheme.onSurfaceVariant) - Row(horizontalArrangement = Arrangement.spacedBy(6.dp)) { - EditorButton(stringResource(R.string.ctrl_editor_snap), false, selected = snap) { - snap = !snap - ControllerPrefs.setSnap(context, snap) - controls.setSnap(snap) - } - EditorButton(stringResource(R.string.ctrl_reset), false) { controls.resetLayout() } - EditorButton(stringResource(R.string.common_cancel), false) { finish() } - EditorButton(stringResource(R.string.ctrl_editor_save), true) { - controls.saveLayout() - finish() - } - } - } - } - } - } - root.addView(bar, FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.CENTER)) - setContentView(root) - } - - @Composable - private fun EditorButton(label: String, primary: Boolean, selected: Boolean? = null, onClick: () -> Unit) { - val pal = LocalPalette.current - Text( - label, fontSize = 12.sp, fontWeight = FontWeight.SemiBold, - color = when { - primary -> pal.onSignal - selected == true -> pal.signal - else -> MaterialTheme.colorScheme.onBackground - }, - modifier = Modifier.clip(RoundedCornerShape(50)) - .background( - when { - primary -> pal.signal - selected == true -> pal.signal.copy(alpha = 0.18f) - else -> Color.White.copy(alpha = 0.06f) - }, - ) - .focusGlide(RoundedCornerShape(50)) - .then( - if (selected == null) Modifier.clickable(onClick = onClick) - else Modifier.toggleable(value = selected, role = Role.Switch, onValueChange = { onClick() }), - ) - .padding(horizontal = 12.dp, vertical = 6.dp), - ) - } - - override fun onWindowFocusChanged(hasFocus: Boolean) { - super.onWindowFocusChanged(hasFocus) - if (hasFocus) immersive() - } - - private fun immersive() { - @Suppress("DEPRECATION") - window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY - or View.SYSTEM_UI_FLAG_FULLSCREEN - or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION - or View.SYSTEM_UI_FLAG_LAYOUT_STABLE - or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN - or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION) - if (Build.VERSION.SDK_INT >= 28) { - window.attributes = window.attributes.apply { - layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES - } - } - } -} diff --git a/app/src/main/java/com/droiddeck/launcher/input/ControllerPrefs.kt b/app/src/main/java/com/droiddeck/launcher/input/ControllerPrefs.kt index bb1705ba68..5231a753ac 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/ControllerPrefs.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/ControllerPrefs.kt @@ -73,7 +73,7 @@ object ControllerPrefs { keyboardButton = p.getBoolean("keyboardButton", true), selectSteam = p.getBoolean("selectSteam", false), joyConVr = joyConVr(context), - snap = p.getBoolean("snap", true), + snap = snap(context), customLayout = p.all.keys.any { it.startsWith("layout.") }, mapping = mappable.associate { (id, _) -> id to target(context, id) }, deadZone = deadZone(context), @@ -87,11 +87,6 @@ object ControllerPrefs { prefs(context).edit().putString("map.$id", target).apply() } - fun resetMapping(context: Context) { - val p = prefs(context) - p.edit().apply { p.all.keys.filter { it.startsWith("map.") }.forEach { remove(it) } }.apply() - } - fun setTint(context: Context, tint: Int) { prefs(context).edit().putInt("tint", tint).apply() } @@ -129,10 +124,6 @@ object ControllerPrefs { prefs(context).edit().putBoolean("steamButton", on).apply() } - fun setQamButton(context: Context, on: Boolean) { - prefs(context).edit().putBoolean("qamButton", on).apply() - } - fun setKeyboardButton(context: Context, on: Boolean) { prefs(context).edit().putBoolean("keyboardButton", on).apply() } @@ -147,6 +138,8 @@ object ControllerPrefs { prefs(context).edit().putBoolean("joyConVr", on).apply() } + fun snap(context: Context): Boolean = prefs(context).getBoolean("snap", true) + fun setSnap(context: Context, on: Boolean) { prefs(context).edit().putBoolean("snap", on).apply() } @@ -169,62 +162,7 @@ object ControllerPrefs { prefs(context).edit().remove("layout.${width}x$height").apply() } - fun screenKeysShown(context: Context): Boolean = prefs(context).getBoolean("screenKeysShown", true) - - fun setScreenKeysShown(context: Context, shown: Boolean) { - prefs(context).edit().putBoolean("screenKeysShown", shown).apply() - } - - fun screenKeys(context: Context): List = decodeScreenKeys(prefs(context).getString("screenKeys", null).orEmpty()) - - fun setScreenKeys(context: Context, keys: List) { - prefs(context).edit().putString("screenKeys", encodeScreenKeys(keys)).apply() - } - - fun encodeScreenKeys(keys: List): String = keys.joinToString(";") { - "${it.codes.joinToString("+")}:${it.x},${it.y},${it.size}:${escape(it.glyph)}:${escape(it.text)}:${escape(it.image)}:${it.opacity}:${it.shape}:${if (it.toggle) 1 else 0}:${if (it.labels) 1 else 0}" - } - - fun decodeScreenKeys(raw: String): List = raw.split(';').mapNotNull { entry -> - val parts = entry.split(':') - if (parts.size < 2) return@mapNotNull null - val keys = parts[0].split('+').mapNotNull { it.toIntOrNull() }.takeIf { it.isNotEmpty() } ?: return@mapNotNull null - val nums = parts[1].split(',').mapNotNull { it.toFloatOrNull() }.takeIf { it.size == 3 } ?: return@mapNotNull null - ScreenKey( - keys, nums[0], nums[1], nums[2].toInt(), - parts.getOrNull(2)?.let(::unescape).orEmpty(), - parts.getOrNull(3)?.let(::unescape).orEmpty(), - parts.getOrNull(4)?.let(::unescape).orEmpty(), - parts.getOrNull(5)?.toFloatOrNull()?.coerceIn(0.2f, 1f) ?: 1f, - parts.getOrNull(6)?.toIntOrNull()?.coerceIn(SCREEN_KEY_PILL, SCREEN_KEY_STICK) ?: SCREEN_KEY_PILL, - parts.getOrNull(7) == "1", - parts.getOrNull(8) != "0", - ) - } - - fun screenLayouts(context: Context): Map = - prefs(context).all.mapNotNull { (key, value) -> - if (!key.startsWith("screenLayout.") || value !is String) null else key.removePrefix("screenLayout.") to value - }.toMap() - - fun saveScreenLayout(context: Context, name: String, raw: String) { - prefs(context).edit().putString("screenLayout.$name", raw).apply() - } - - fun deleteScreenLayout(context: Context, name: String) { - prefs(context).edit().remove("screenLayout.$name").apply() - } - - private fun escape(value: String) = value.replace("%", "%25").replace(";", "%3B").replace(":", "%3A").replace(",", "%2C") - - private fun unescape(value: String) = value.replace("%2C", ",").replace("%3A", ":").replace("%3B", ";").replace("%25", "%") - fun resetAll(context: Context) { prefs(context).edit().clear().putBoolean("rumble", true).apply() } - - fun resetAllLayouts(context: Context) { - val p = prefs(context) - p.edit().apply { p.all.keys.filter { it.startsWith("layout.") }.forEach { remove(it) } }.apply() - } } diff --git a/app/src/main/java/com/droiddeck/launcher/input/DeckControls.kt b/app/src/main/java/com/droiddeck/launcher/input/DeckControls.kt index a8c499793b..41b7240362 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/DeckControls.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/DeckControls.kt @@ -5,53 +5,165 @@ object DeckControls { const val R4 = 2 const val L5 = 4 const val R5 = 8 - private const val LEFT_TOUCH = 16 - private const val RIGHT_TOUCH = 32 - private const val LEFT_CLICK = 64 - private const val RIGHT_CLICK = 128 - - private const val SLOT = 0 - - private var controls = 0 + private val controls = arrayOf(InputControl.L4, InputControl.R4, InputControl.L5, InputControl.R5, + InputControl.LEFT_PAD, InputControl.RIGHT_PAD, InputControl.LEFT_CLICK, InputControl.RIGHT_CLICK) + private val sources = ArrayList() + private val physical = Source(InputRole.PHYSICAL) + private var router: InputRouter? = null private val pads = ShortArray(4) private val pressure = ShortArray(2) + private val touchOwners = arrayOfNulls(2) @Synchronized - fun setGrip(bit: Int, down: Boolean) { - set(bit, down) + fun setRouter(next: InputRouter?) { + if (router === next) return + for (source in sources) source.closeRoutes() + router = next + for (source in sources) source.configure() publish() } @Synchronized - fun setPad(right: Boolean, touching: Boolean, x: Float = 0f, y: Float = 0f) { - val at = if (right) 2 else 0 - set(if (right) RIGHT_TOUCH else LEFT_TOUCH, touching) - pads[at] = if (touching) axis(x) else 0 - pads[at + 1] = if (touching) axis(y) else 0 - publish() + fun clearRouter(owner: InputRouter?) { + if (owner != null && router === owner) setRouter(null) } @Synchronized - fun setClick(right: Boolean, down: Boolean) { - set(if (right) RIGHT_CLICK else LEFT_CLICK, down) - pressure[if (right) 1 else 0] = if (down) Short.MAX_VALUE else 0 - publish() - } + @JvmOverloads + fun source(role: InputRole, routed: Boolean = false) = Source(role, routed) + + @Synchronized + fun setGrip(bit: Int, down: Boolean) = physical.setGrip(bit, down) + + @Synchronized + fun setPad(right: Boolean, touching: Boolean, x: Float = 0f, y: Float = 0f) = physical.setPad(right, touching, x, y) + + @Synchronized + fun setClick(right: Boolean, down: Boolean) = physical.setClick(right, down) @Synchronized fun releaseAll() { - if (controls == 0 && pads.all { it.toInt() == 0 } && pressure.all { it.toInt() == 0 }) return - controls = 0 - pads.fill(0) - pressure.fill(0) + for (source in sources) source.clear() publish() } - private fun set(bit: Int, on: Boolean) { - controls = if (on) controls or bit else controls and bit.inv() + @Synchronized + fun suspend() { + for (source in sources) source.suspend() + publish() } - private fun axis(value: Float): Short = (value.coerceIn(-1f, 1f) * Short.MAX_VALUE).toInt().toShort() + @Synchronized + fun refresh() { + for (source in sources) source.route() + publish() + } - private fun publish() = FakeInputWriter.writeDeckControls(SLOT, controls, pads, pressure) + class Source internal constructor(private val role: InputRole, private val routed: Boolean = false) { + private val x = FloatArray(8) + private val y = FloatArray(8) + private val touched = BooleanArray(8) + private var routes: Array? = null + internal var mask = 0 + internal val axes = ShortArray(4) + + internal fun configure() { + routes = router?.takeUnless { routed }?.let { r -> Array(8) { i -> + r.channel(role, controls[i]).also { it.awaitNeutral(x[i], y[i], touched[i]) } + } } + mask = 0 + axes.fill(0) + } + + private fun attach() { + if (this !in sources) { sources.add(this); configure() } + } + + fun setGrip(bit: Int, down: Boolean) = synchronized(DeckControls) { + val at = when (bit) { L4 -> 0; R4 -> 1; L5 -> 2; R5 -> 3; else -> return@synchronized } + update(at, if (down) 1f else 0f) + } + + fun setPad(right: Boolean, touching: Boolean, x: Float = 0f, y: Float = 0f) = synchronized(DeckControls) { + update(if (right) 5 else 4, if (touching) x else 0f, if (touching) y else 0f, touching) + } + + fun setClick(right: Boolean, down: Boolean) = synchronized(DeckControls) { + update(if (right) 7 else 6, if (down) 1f else 0f) + } + + private fun update(at: Int, value: Float, second: Float = 0f, contact: Boolean = false) { + val timing = InputTiming.begin() + attach() + x[at] = value; y[at] = second; touched[at] = contact + route() + publish() + InputTiming.end(timing) + } + + internal fun native(control: InputControl?, value: Float, second: Float, contact: Boolean) = synchronized(DeckControls) { + require(routed) + attach() + x.fill(0f); y.fill(0f); touched.fill(false) + val at = controls.indexOf(control) + if (at >= 0) { x[at] = value; y[at] = second; touched[at] = contact } + route(); publish() + } + + internal fun route() { + mask = 0 + axes.fill(0) + for (i in controls.indices) { + val target = if (routes == null) controls[i] else routes!![i].update(x[i], y[i], touched[i]) ?: continue + val at = controls.indexOf(target) + if (at < 0) continue + if (at == 4 || at == 5) { + if (touched[i]) { + mask = mask or (1 shl at) + axes[(at - 4) * 2] = axis(x[i]); axes[(at - 4) * 2 + 1] = axis(y[i]) + } + } else if (x[i] > 0.5f) mask = mask or (1 shl at) + } + } + + internal fun clear() { + x.fill(0f); y.fill(0f); touched.fill(false); mask = 0; axes.fill(0) + routes?.forEach { it.release() } + } + + internal fun suspend() { + mask = 0; axes.fill(0) + routes?.forEach { it.suspend() } + } + + fun release() = synchronized(DeckControls) { clear(); publish() } + + internal fun closeRoutes() { routes?.forEach { it.close() }; routes = null } + + fun close() = synchronized(DeckControls) { + clear(); closeRoutes(); sources.remove(this); publish() + } + } + + private fun axis(value: Float) = (value.coerceIn(-1f, 1f) * Short.MAX_VALUE).toInt().toShort() + + private fun publish() { + var mask = 0 + pads.fill(0) + for (source in sources) { + mask = mask or source.mask + } + for (side in 0..1) { + val contact = 16 shl side + val owner = touchOwners[side]?.takeIf { it.mask and contact != 0 } + ?: sources.firstOrNull { it.mask and contact != 0 } + touchOwners[side] = owner + if (owner == null) continue + pads[side * 2] = owner.axes[side * 2] + pads[side * 2 + 1] = owner.axes[side * 2 + 1] + } + pressure[0] = if (mask and 64 != 0) Short.MAX_VALUE else 0 + pressure[1] = if (mask and 128 != 0) Short.MAX_VALUE else 0 + FakeInputWriter.writeDeckControls(0, mask, pads, pressure) + } } diff --git a/app/src/main/java/com/droiddeck/launcher/input/DirectTouchHold.kt b/app/src/main/java/com/droiddeck/launcher/input/DirectTouchHold.kt index ee8d68ef05..7a64caaac6 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/DirectTouchHold.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/DirectTouchHold.kt @@ -70,6 +70,14 @@ class DirectTouchHold( handler.removeCallbacks(hold) } + fun cancelTouch() { + handler.removeCallbacks(hold) + handler.removeCallbacks(lift) + liftPending = false + if (forwarded) touch.cancel() + forwarded = false + } + fun onTouch(event: MotionEvent): Boolean { if (event.actionMasked == MotionEvent.ACTION_DOWN) { if (liftPending) { diff --git a/app/src/main/java/com/droiddeck/launcher/input/DualScreen.kt b/app/src/main/java/com/droiddeck/launcher/input/DualScreen.kt index cfd37c95c5..848fc65701 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/DualScreen.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/DualScreen.kt @@ -16,6 +16,7 @@ object DualScreen { const val MODE_OFF = "off" val modes = listOf(MODE_AUTO, MODE_ON, MODE_OFF) private const val TYPE_INTERNAL = 1 + private const val TYPE_EXTERNAL = 2 private val knownModels = setOf("retroid pocket duo", "ayn thor") private val touchedDisplays = mutableSetOf() private val takenListeners = java.util.concurrent.CopyOnWriteArraySet<() -> Unit>() @@ -85,9 +86,22 @@ object DualScreen { when (mode) { MODE_OFF -> null MODE_ON -> internalDisplay(displayManager, primaryDisplayId) ?: anyDisplay(displayManager, primaryDisplayId) - else -> builtInDisplay(displayManager, primaryDisplayId) + else -> if (dockedToDisplay(displayManager, primaryDisplayId)) null else builtInDisplay(displayManager, primaryDisplayId) }?.takeUnless { taken(it.displayId) } + fun leavesBottomOnDock(wasDocked: Boolean, docked: Boolean, mode: String, automaticOnBuiltIn: Boolean): Boolean = + docked && !wasDocked && mode == MODE_AUTO && automaticOnBuiltIn + + fun dockedToDisplay(displayManager: DisplayManager, primaryDisplayId: Int): Boolean { + val glasses = com.droiddeck.launcher.glasses.Glasses.state.value + val external = displayManager.getDisplays().filter { + it.isValid && it.displayId != primaryDisplayId && it.state != Display.STATE_OFF && displayType(it) == TYPE_EXTERNAL + } + val glassesId = glasses.displayId.takeIf { it != Display.INVALID_DISPLAY } + ?: com.droiddeck.launcher.glasses.Glasses.selectGlassesDisplay(external, glasses.present)?.displayId + return external.any { it.displayId != glassesId && com.droiddeck.launcher.glasses.Glasses.selectGlassesDisplay(listOf(it), false) == null } + } + fun anyDisplay(displayManager: DisplayManager, primaryDisplayId: Int): Display? = displayManager.getDisplays(DisplayManager.DISPLAY_CATEGORY_PRESENTATION).firstOrNull { it.isValid && it.displayId != primaryDisplayId && (it.flags and Display.FLAG_PRESENTATION) != 0 diff --git a/app/src/main/java/com/droiddeck/launcher/ui/GamepadGeometry.kt b/app/src/main/java/com/droiddeck/launcher/input/GamepadGeometry.kt similarity index 99% rename from app/src/main/java/com/droiddeck/launcher/ui/GamepadGeometry.kt rename to app/src/main/java/com/droiddeck/launcher/input/GamepadGeometry.kt index 2b369c20aa..988ab5f764 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/GamepadGeometry.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/GamepadGeometry.kt @@ -1,4 +1,4 @@ -package com.droiddeck.launcher.ui +package com.droiddeck.launcher.input import kotlin.math.min diff --git a/app/src/main/java/com/droiddeck/launcher/input/GestureTouchStreamGate.kt b/app/src/main/java/com/droiddeck/launcher/input/GestureTouchStreamGate.kt index 4d69f68c5b..9ba24e3381 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/GestureTouchStreamGate.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/GestureTouchStreamGate.kt @@ -3,7 +3,7 @@ package com.droiddeck.launcher.input import android.view.MotionEvent internal class GestureTouchStreamGate { - enum class Source { SURFACE, SCREEN_KEYS, ON_SCREEN_CONTROLS } + enum class Source { SURFACE, ON_SCREEN_CONTROLS } private var owner: Source? = null diff --git a/app/src/main/java/com/droiddeck/launcher/input/HeldInput.kt b/app/src/main/java/com/droiddeck/launcher/input/HeldInput.kt new file mode 100644 index 0000000000..1fdb6121fc --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/input/HeldInput.kt @@ -0,0 +1,27 @@ +package com.droiddeck.launcher.input + +class HeldInput(private val emit: (Int, Boolean) -> Unit) { + private val owners = IntArray(768) + + @Synchronized + fun set(source: Int, code: Int, down: Boolean) { + if (code !in owners.indices) return + val before = owners[code] + val after = if (down) before or source else before and source.inv() + owners[code] = after + if ((before == 0) != (after == 0)) emit(code, after != 0) + } + + @Synchronized + fun release(source: Int) { + for (code in owners.indices) if (owners[code] and source != 0) set(source, code, false) + } + + @Synchronized + fun clear() { + for (code in owners.indices) if (owners[code] != 0) { + owners[code] = 0 + emit(code, false) + } + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/input/ScreenKeyIcons.kt b/app/src/main/java/com/droiddeck/launcher/input/InputButtonIcons.kt similarity index 77% rename from app/src/main/java/com/droiddeck/launcher/input/ScreenKeyIcons.kt rename to app/src/main/java/com/droiddeck/launcher/input/InputButtonIcons.kt index b1ea677b99..8f87c05cbf 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/ScreenKeyIcons.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/InputButtonIcons.kt @@ -1,16 +1,49 @@ package com.droiddeck.launcher.input +import android.content.Context import android.graphics.Canvas import android.graphics.Paint import android.graphics.Path +import android.graphics.drawable.Drawable +import com.droiddeck.launcher.R -object ScreenKeyIcons { - fun has(code: Int): Boolean = code in SET +object InputButtonIcons { + // Persisted symbol IDs must not depend on Android resource IDs or overlap Linux key codes. + const val MENU = 1001 + const val KEYBOARD = 1002 + const val STEAM = 1003 + const val QUICK_ACCESS = 1004 - private val SET = setOf(28, 14, 15, 57, 42, 54, 29, 97, 125, 103, 108, 105, 106) + fun has(code: Int): Boolean = code in codes - fun draw(canvas: Canvas, code: Int, cx: Float, cy: Float, box: Float, paint: Paint): Boolean { - if (!has(code)) return false + private val keyCodes = listOf(28, 14, 15, 57, 42, 54, 29, 97, 125, 103, 108, 105, 106) + val codes = listOf(MENU, KEYBOARD, STEAM, QUICK_ACCESS) + keyCodes + + fun label(context: Context, code: Int): String = when (code) { + MENU -> context.getString(R.string.touch_droiddeck_menu) + KEYBOARD -> context.getString(R.string.drawer_keyboard) + STEAM -> context.getString(R.string.drawer_steam_menu) + QUICK_ACCESS -> context.getString(R.string.drawer_open_qam) + else -> pcKeyLabel(context, code) + } + + fun drawable(context: Context, code: Int): Drawable? = when (code) { + MENU -> R.drawable.ic_stat_session + KEYBOARD -> R.drawable.ic_bottom_keyboard + STEAM -> R.drawable.ic_input_steam + QUICK_ACCESS -> R.drawable.ic_input_quick_access + else -> null + }?.let { context.getDrawable(it)?.mutate() } + + fun draw(canvas: Canvas, code: Int, cx: Float, cy: Float, box: Float, paint: Paint, drawable: Drawable? = null): Boolean { + if (drawable != null) { + drawable.setTint(paint.color) + val half = box / 2f + drawable.setBounds((cx - half).toInt(), (cy - half).toInt(), (cx + half).toInt(), (cy + half).toInt()) + drawable.draw(canvas) + return true + } + if (code !in keyCodes) return false val h = box / 2f val stroke = box * 0.085f val path = Path() diff --git a/app/src/main/java/com/droiddeck/launcher/input/InputButtonImages.kt b/app/src/main/java/com/droiddeck/launcher/input/InputButtonImages.kt new file mode 100644 index 0000000000..3ca2053faa --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/input/InputButtonImages.kt @@ -0,0 +1,43 @@ +package com.droiddeck.launcher.input + +import android.content.Context +import android.graphics.Bitmap +import android.graphics.BitmapFactory +import android.net.Uri +import java.io.File +import java.util.UUID + +internal object InputButtonImages { + const val DIR = "screenkeys" + private const val MAX_SIZE_DP = 140 + + fun file(context: Context, name: String): File? = + name.takeIf { it.matches(Regex("[A-Za-z0-9_.-]{1,128}")) && it != "." && it != ".." }?.let { File(File(context.filesDir, DIR), it) } + + fun import(context: Context, uri: Uri): String? { + val dir = File(context.filesDir, DIR).apply { mkdirs() } + val name = "key_" + UUID.randomUUID() + ".png" + val longest = (MAX_SIZE_DP * context.resources.displayMetrics.density).toInt() + return runCatching { + val bounds = BitmapFactory.Options().apply { inJustDecodeBounds = true } + context.contentResolver.openInputStream(uri)?.use { BitmapFactory.decodeStream(it, null, bounds) } + val source = maxOf(bounds.outWidth, bounds.outHeight) + if (source <= 0) return@runCatching null + var sample = 1 + while (source / (sample * 2) >= longest) sample *= 2 + val bitmap = context.contentResolver.openInputStream(uri)?.use { + BitmapFactory.decodeStream(it, null, BitmapFactory.Options().apply { inSampleSize = sample }) + } ?: return@runCatching null + val scale = longest.toFloat() / maxOf(bitmap.width, bitmap.height) + val scaled = if (scale < 1f) Bitmap.createScaledBitmap(bitmap, (bitmap.width * scale).toInt().coerceAtLeast(1), + (bitmap.height * scale).toInt().coerceAtLeast(1), true) else bitmap + try { + val saved = File(dir, name).outputStream().use { scaled.compress(Bitmap.CompressFormat.PNG, 100, it) } + if (saved) name else null + } finally { + if (scaled !== bitmap) bitmap.recycle() + scaled.recycle() + } + }.getOrNull().also { if (it == null) File(dir, name).delete() } + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/input/InputLayoutFile.kt b/app/src/main/java/com/droiddeck/launcher/input/InputLayoutFile.kt new file mode 100644 index 0000000000..b47f9bf92b --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/input/InputLayoutFile.kt @@ -0,0 +1,123 @@ +package com.droiddeck.launcher.input + +import android.content.Context +import org.json.JSONArray +import org.json.JSONObject +import kotlin.math.roundToInt + +internal object InputLayoutFile { + private val NAMES: Map = mapOf( + 103 to "KEY_UP", 106 to "KEY_RIGHT", 108 to "KEY_DOWN", 105 to "KEY_LEFT", + 28 to "KEY_ENTER", 1 to "KEY_ESC", 14 to "KEY_BKSP", 111 to "KEY_DEL", 15 to "KEY_TAB", + 57 to "KEY_SPACE", 29 to "KEY_CTRL_L", 97 to "KEY_CTRL_R", 110 to "KEY_INSERT", + 42 to "KEY_SHIFT_L", 54 to "KEY_SHIFT_R", 56 to "KEY_ALT_L", 100 to "KEY_ALT_R", + 102 to "KEY_HOME", 107 to "KEY_END", 99 to "KEY_PRTSCN", 104 to "KEY_PG_UP", 109 to "KEY_PG_DOWN", + 58 to "KEY_CAPS_LOCK", 69 to "KEY_NUM_LOCK", 41 to "KEY_GRAVE", 12 to "KEY_MINUS", + 11 to "KEY_0", 2 to "KEY_1", 3 to "KEY_2", 4 to "KEY_3", 5 to "KEY_4", 6 to "KEY_5", + 7 to "KEY_6", 8 to "KEY_7", 9 to "KEY_8", 10 to "KEY_9", + 30 to "KEY_A", 48 to "KEY_B", 46 to "KEY_C", 32 to "KEY_D", 18 to "KEY_E", 33 to "KEY_F", + 34 to "KEY_G", 35 to "KEY_H", 23 to "KEY_I", 36 to "KEY_J", 37 to "KEY_K", 38 to "KEY_L", + 50 to "KEY_M", 49 to "KEY_N", 24 to "KEY_O", 25 to "KEY_P", 16 to "KEY_Q", 19 to "KEY_R", + 31 to "KEY_S", 20 to "KEY_T", 22 to "KEY_U", 47 to "KEY_V", 17 to "KEY_W", 45 to "KEY_X", + 21 to "KEY_Y", 44 to "KEY_Z", + 26 to "KEY_BRACKET_LEFT", 27 to "KEY_BRACKET_RIGHT", 43 to "KEY_BACKSLASH", 53 to "KEY_SLASH", + 39 to "KEY_SEMICOLON", 51 to "KEY_COMMA", 52 to "KEY_PERIOD", 40 to "KEY_APOSTROPHE", + 59 to "KEY_F1", 60 to "KEY_F2", 61 to "KEY_F3", 62 to "KEY_F4", 63 to "KEY_F5", 64 to "KEY_F6", + 65 to "KEY_F7", 66 to "KEY_F8", 67 to "KEY_F9", 68 to "KEY_F10", 87 to "KEY_F11", 88 to "KEY_F12", + 79 to "KEY_KP_1", 80 to "KEY_KP_2", 81 to "KEY_KP_3", 75 to "KEY_KP_4", 76 to "KEY_KP_5", + 77 to "KEY_KP_6", 71 to "KEY_KP_7", 72 to "KEY_KP_8", 73 to "KEY_KP_9", 82 to "KEY_KP_0", + ) + private val CODES: Map = NAMES.entries.associate { (code, name) -> name to code } + + fun fileName(layout: SavedInputLayout) = layout.name.replace(Regex("[^A-Za-z0-9 _.-]"), "_").trim().ifEmpty { "droiddeck-layout" } + ".icp" + + fun export(layout: SavedInputLayout, bindings: SavedInputBindings?, width: Int, height: Int): String? { + val snap = width.coerceAtLeast(1) / 100f + val elements = JSONArray() + for (e in layout.elements) { + val action = bindings?.action(e) ?: InputAction.Disabled + val directional = e.kind == InputElementKind.DPAD || e.kind == InputElementKind.STICK + val codes = when { + directional && action is InputAction.Directions -> listOf(action.up, action.right, action.down, action.left) + !directional && action is InputAction.Keys -> action.codes + else -> continue + } + val binds = JSONArray() + for (code in codes) { + val name = NAMES[code] + if (name != null) binds.put(name) else if (directional) binds.put("") + } + if ((0 until binds.length()).all { binds.getString(it).isEmpty() }) continue + val shape = when { + directional -> "ROUND_RECT" + e.buttonShape == InputButtonShape.CIRCLE -> "CIRCLE" + e.buttonShape == InputButtonShape.RECTANGLE -> "RECT" + else -> "ROUND_RECT" + } + val sizePx = e.height * height + val scale = when { + directional -> sizePx * 2.3f / (14f * snap) + shape == "CIRCLE" -> sizePx / (6f * snap) + else -> sizePx / (4f * snap) + } + elements.put(JSONObject() + .put("type", when (e.kind) { InputElementKind.DPAD -> "D_PAD"; InputElementKind.STICK -> "STICK"; else -> "BUTTON" }) + .put("shape", shape).put("customColor", 0).put("bindings", binds).put("scale", scale.toDouble()) + .put("x", (e.x + e.width / 2).toDouble()).put("y", (e.y + e.height / 2).toDouble()) + .put("toggleSwitch", e.toggle).put("swipeable", false) + .put("text", if (e.icon != 0 || e.image.isNotEmpty()) "" else e.name).put("iconId", 0) + .apply { if (e.opacity < 1f) put("opacity", e.opacity.toDouble()) }) + } + if (elements.length() == 0) return null + return JSONObject().put("id", 0).put("name", layout.name).put("cursorSpeed", 1.5).put("elements", elements).toString() + } + + fun parse(raw: String, width: Int, density: Float): List? { + val root = runCatching { JSONObject(raw) }.getOrNull() ?: return null + val elements = root.optJSONArray("elements") ?: return null + val snap = width.coerceAtLeast(1) / 100f + val keys = mutableListOf() + for (i in 0 until elements.length()) { + val element = elements.optJSONObject(i) ?: continue + val type = element.optString("type", "BUTTON") + if (type != "BUTTON" && type != "D_PAD" && type != "STICK") continue + val binds = element.optJSONArray("bindings") ?: continue + val raw = (0 until binds.length()).map { CODES[binds.optString(it)] ?: 0 } + val directional = type != "BUTTON" + val codes = when { + !directional -> raw.filter { it != 0 }.distinct().take(8) + type == "STICK" && raw.size <= 4 -> listOf(raw.getOrElse(0) { 0 }, 0, raw.getOrElse(1) { 0 }, 0, raw.getOrElse(2) { 0 }, 0, raw.getOrElse(3) { 0 }, 0) + else -> raw.take(if (type == "STICK") 8 else 4) + } + if (codes.none { it != 0 }) continue + val shape = when { + type == "D_PAD" -> 5 + type == "STICK" -> 6 + else -> when (element.optString("shape")) { "CIRCLE" -> 2; "RECT" -> 3; "SQUARE" -> 1; else -> 0 } + } + val scale = element.optDouble("scale", 1.0).toFloat().takeIf { it.isFinite() && it > 0f } ?: 1f + val sizePx = when { + directional -> 14f * snap * scale / 2.3f + shape == 2 -> 6f * snap * scale + else -> 4f * snap * scale + } + fun coordinate(name: String) = element.optDouble(name, .5).toFloat().takeIf { it.isFinite() }?.coerceIn(0f, 1f) ?: .5f + keys += LegacyScreenKey(codes, coordinate("x"), coordinate("y"), (sizePx / density).roundToInt().coerceIn(24, 140).toFloat(), + text = element.optString("text").trim().take(24), + opacity = element.optDouble("opacity", 1.0).toFloat().takeIf { it.isFinite() }?.coerceIn(.2f, 1f) ?: 1f, + shape = shape, toggle = element.optBoolean("toggleSwitch")) + if (keys.size == 64) break + } + return keys + } + + fun import(context: Context, raw: String, fallbackName: String, screen: InputLayoutScreen, style: InputSurfaceStyle, + width: Int, height: Int, density: Float, id: String = newInputId()): Pair? { + val keys = parse(raw, width, density)?.takeIf { it.isNotEmpty() } ?: return null + val converted = runCatching { LegacyScreenKeyMigration.convert(context, keys, width, height, density) }.getOrNull() ?: return null + val name = runCatching { JSONObject(raw).optString("name") }.getOrNull().orEmpty().trim().take(80).ifEmpty { fallbackName.trim().take(80) } + val layout = SavedInputLayout(id, name, converted.map { it.first }, style = style, screen = screen, + aspect = (width.coerceAtLeast(1).toFloat() / height.coerceAtLeast(1)).coerceIn(.05f, 20f)) + return layout to SavedInputBindings(newInputId(), name, false, id, elements = converted.associate { it.first.id to it.second }) + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/input/InputLibraries.kt b/app/src/main/java/com/droiddeck/launcher/input/InputLibraries.kt new file mode 100644 index 0000000000..bcc641ebaa --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/input/InputLibraries.kt @@ -0,0 +1,395 @@ +package com.droiddeck.launcher.input + +import android.content.Context +import android.hardware.display.DisplayManager +import android.util.DisplayMetrics +import android.view.Display +import com.droiddeck.launcher.session.SessionPrefs +import org.json.JSONArray +import org.json.JSONObject +import java.util.UUID + +object InputLibraries { + private const val VERSION = 14 + private const val KEY = "library" + private const val RECOVERY = "library-recovery." + const val OVERLAY = "builtin.overlay" + const val DECK = "builtin.deck" + const val TRACKPAD = "builtin.trackpad" + + private fun prefs(context: Context) = context.getSharedPreferences("input-profiles", Context.MODE_PRIVATE) + + @Synchronized + fun read(context: Context): InputLibrary { + val p = prefs(context) + val raw = p.getString(KEY, null) + val decoded = raw?.let(::decode) + if (raw != null && decoded == null) return scopeLayouts(upgradeStockLayouts(upgradeOverlay(context, migrate(context)))) + var library = decoded ?: scopeLayouts(upgradeStockLayouts(upgradeOverlay(context, migrate(context)))) + val version = raw?.let { JSONObject(it).getInt("version") } ?: 0 + if (version < 5) library = scopeLayouts(upgradeStockLayouts(upgradeOverlay(context, library))) + if (version < 11) library = neutralStockLayouts(library) + if (version < 10) library = restyleGamepads(context, library) + if (version < 12) library = barStockLayouts(library) + if (version < 13) library = LegacyScreenKeyMigration.pin(library) + if (version < 14) library = deckGamepadSurfaces(context, library) + if (version < 6 || !p.contains(LegacyScreenKeyMigration.BACKUP)) { + val legacy = LegacyScreenKeyMigration.snapshot(context) + if (legacy != null) { + val migrated = runCatching { LegacyScreenKeyMigration.migrate(context, library, legacy) }.getOrElse { + android.util.Log.e("InputLibraries", "Could not migrate screen keys; keeping the original settings", it) + return library + } + val merged = mergeScreenKeys(migrated, legacy) + val upgraded = merged ?: migrated + val encoded = encode(upgraded) + require(encoded.length <= 2_000_000) + val edit = p.edit().putString(KEY, encoded).putString(LegacyScreenKeyMigration.BACKUP, legacy) + if (merged != null) edit.putBoolean(LegacyScreenKeyMigration.MERGED, true) + if (!edit.commit()) return library + LegacyScreenKeyMigration.clean(context) + return upgraded + } + } + val backup = p.getString(LegacyScreenKeyMigration.BACKUP, null) + if (backup != null && !p.contains(LegacyScreenKeyMigration.MERGED)) { + val merged = mergeScreenKeys(library, backup) + if (merged != null) { + val encoded = encode(merged) + if (encoded.length <= 2_000_000 && p.edit().putString(KEY, encoded).putBoolean(LegacyScreenKeyMigration.MERGED, true).commit()) library = merged + } + } + if (raw == null || version < VERSION) write(context, library) + if (p.contains(LegacyScreenKeyMigration.BACKUP)) LegacyScreenKeyMigration.clean(context) + return library + } + + private fun mergeScreenKeys(library: InputLibrary, legacy: String): InputLibrary? = runCatching { LegacyScreenKeyMigration.merge(library, legacy) } + .onFailure { android.util.Log.e("InputLibraries", "Could not show the imported screen keys with the gamepad", it) }.getOrNull() + + fun hasPendingScreenKeyMigration(context: Context) = !prefs(context).contains(LegacyScreenKeyMigration.BACKUP) && LegacyScreenKeyMigration.snapshot(context) != null + + fun hasLibraryRecovery(context: Context) = prefs(context).all.keys.any { it.startsWith(RECOVERY) } + + @Synchronized + fun update(context: Context, change: (InputLibrary) -> InputLibrary): InputLibrary { + val p = prefs(context) + val stored = p.getString(KEY, null) + val invalid = stored != null && decode(stored) == null + require(!invalid || runCatching { JSONObject(stored!!).getInt("version") }.getOrNull()?.let { it <= VERSION } != false) { + "The saved input library cannot be edited by this version." + } + val updated = change(read(context)).validate() + if (invalid) { + val encoded = encode(updated) + require(encoded.length <= 2_000_000) { "The input library is too large to save." } + check(p.edit().putString(RECOVERY + UUID.nameUUIDFromBytes(stored!!.toByteArray(Charsets.UTF_8)), stored) + .putString(KEY, encoded).commit()) { "Could not save the input library and its recovery copy." } + } else write(context, updated) + return updated + } + + private fun write(context: Context, library: InputLibrary) { + val encoded = encode(library.validate()) + require(encoded.length <= 2_000_000) { "The input library is too large to save." } + prefs(context).edit().putString(KEY, encoded).apply() + } + + private fun migrate(context: Context): InputLibrary { + val profiles = mutableListOf() + val stock = InputStockLayouts.layouts() + val layouts = (listOf(SavedInputLayout(OVERLAY, "Built-in gamepad", template = InputLayoutTemplate.OVERLAY)) + stock).toMutableList() + val bindings = InputStockLayouts.presets(stock).toMutableList() + for (id in listOf(InputProfile.GAME, InputProfile.DESKTOP)) { + val old = InputProfiles.readLegacy(context, id) + val name = if (id == InputProfile.GAME) "Game" else "Desktop" + val legacySurface = InputSurfaceLayout.hasLegacy(context, id) || + old.bindings[InputRole.SECONDARY] != InputProfile.defaults(id).bindings[InputRole.SECONDARY] + val layout = if (legacySurface) SavedInputLayout("$id.layout", "$name touch", InputSurfaceLayout.readLegacy(context, id)).also { layouts += it } + else stock.first { it.id == if (id == InputProfile.DESKTOP) TRACKPAD else DECK } + val parts = InputRole.entries.associateWith { role -> + if (role == InputRole.SECONDARY && !legacySurface) { + val preset = bindings.first { it.layoutId == layout.id } + return@associateWith InputPart(preset.id, layout.id) + } + val preset = SavedInputBindings("$id.${role.name.lowercase()}", "$name / ${role.name.lowercase()}", + role == InputRole.PHYSICAL, if (role == InputRole.SECONDARY) layout.id else null, + old.bindings[role].orEmpty(), keys = if (role == InputRole.PHYSICAL) old.keys else emptyList()) + bindings += preset + InputPart(preset.id, when (role) { InputRole.PHYSICAL -> null; InputRole.PRIMARY -> OVERLAY; InputRole.SECONDARY -> layout.id }) + } + profiles += SavedInputProfile(id, name, id, parts) + } + val selected = prefs(context).getString("selected", InputProfiles.AUTO) + ?.takeIf { it == InputProfiles.AUTO || profiles.any { p -> p.id == it } } ?: InputProfiles.AUTO + return InputLibrary(profiles, layouts, bindings, selected).validate() + } + + fun setAdaptiveSticks(context: Context, layoutId: String, on: Boolean): Boolean = runCatching { + update(context) { library -> + library.copy(layouts = library.layouts.map { layout -> + if (layout.id != layoutId || !layout.editable) layout + else layout.copy(elements = layout.elements.map { if (it.kind == InputElementKind.STICK) it.copy(floating = on, hideWhenIdle = on) else it }) + }) + } + }.onSuccess { ControllerPrefs.setAdaptiveSticks(context, on) }.isSuccess + + fun setQuickButton(context: Context, layoutId: String, button: InputCommand, shown: Boolean): Boolean = runCatching { + update(context) { library -> library.layouts.firstOrNull { it.id == layoutId && it.editable }?.let { library.withLayout(it.withQuickButton(button, shown)) } ?: library } + }.isSuccess + + fun defaultPositions(context: Context, library: InputLibrary, layout: SavedInputLayout): List? { + val origin = layout.origin ?: layout.id + InputStockLayouts.layouts().firstOrNull { it.id == origin }?.let { return it.elements } + if (origin == OVERLAY || origin.startsWith("migrated.layout.")) { + val (surfaceContext, metrics) = primaryDisplay(context) + val size = if (origin == OVERLAY) listOf(metrics.widthPixels, metrics.heightPixels) + else origin.removePrefix("migrated.layout.").split('x').mapNotNull { it.toIntOrNull() } + return size.takeIf { it.size == 2 && it.all { side -> side > 0 } }?.let { LegacyGamepadLayout(surfaceContext, it[0], it[1]).elements() } + } + return library.layouts.firstOrNull { it.id == origin && it.id != layout.id }?.elements + } + + @Suppress("DEPRECATION") + internal fun primaryDisplay(context: Context): Pair { + val display = context.getSystemService(DisplayManager::class.java)?.getDisplay(Display.DEFAULT_DISPLAY) + val surfaceContext = display?.let(context::createDisplayContext) ?: context + return surfaceContext to DisplayMetrics().also { if (display != null) display.getRealMetrics(it) else it.setTo(context.resources.displayMetrics) } + } + + private fun restyleGamepads(context: Context, library: InputLibrary): InputLibrary { + if (library.layouts.none { it.id == OVERLAY || it.id.startsWith("migrated.layout.") }) return library + val (surfaceContext, metrics) = primaryDisplay(context) + return library.copy(layouts = library.layouts.map { layout -> + val size = when { + layout.id == OVERLAY -> listOf(metrics.widthPixels, metrics.heightPixels) + layout.id.startsWith("migrated.layout.") -> layout.id.removePrefix("migrated.layout.").split('x').mapNotNull { it.toIntOrNull() } + else -> emptyList() + } + if (size.size != 2 || size.any { it <= 0 }) layout + else layout.copy(elements = LegacyGamepadLayout(surfaceContext, size[0], size[1]).restyle(layout.elements)) + }) + } + + private fun upgradeOverlay(context: Context, library: InputLibrary): InputLibrary { + if (library.layouts.none { it.template == InputLayoutTemplate.OVERLAY }) return library + val (surfaceContext, metrics) = primaryDisplay(context) + val settings = ControllerPrefs.read(context) + val elements = LegacyGamepadLayout(surfaceContext, metrics.widthPixels.coerceAtLeast(1), metrics.heightPixels.coerceAtLeast(1)).elements() + val style = InputSurfaceStyle(settings.tint, settings.opacity) + val replacements = library.layouts.filter { it.template == InputLayoutTemplate.OVERLAY }.associate { old -> + old.id to old.copy(name = "Gamepad", elements = elements, template = InputLayoutTemplate.CUSTOM, style = style, + aspect = (metrics.widthPixels.coerceAtLeast(1).toFloat() / metrics.heightPixels.coerceAtLeast(1)).coerceIn(.05f, 20f)) + } + var result = library.copy(layouts = library.layouts.map { replacements[it.id] ?: it }) + val migrated = mutableMapOf, String>() + for (profile in library.profiles) { + val parts = profile.parts.mapValues { (role, part) -> + val layout = replacements[part.layout] ?: return@mapValues part + val key = part.bindings to layout.id + val id = migrated.getOrPut(key) { + val old = library.preset(part.bindings) + val newId = migrationBindingId(result, old.id, layout.id) + val keyboard = elements.filter { it.id == "builtin:keyboard" }.associate { it.id to InputAction.Command(InputCommand.KEYBOARD) } + val clicks = if (settings.stickClick) old.clicks else old.clicks + elements.filter { it.hasClick }.associate { it.id to InputAction.Disabled } + result = result.withPreset(old.copy(id = newId, layoutId = layout.id, elements = old.elements + keyboard, clicks = clicks)) + newId + } + part.copy(bindings = id) + } + result = result.withProfile(profile.copy(parts = parts)) + } + val oldBindings = migrated.keys.map { it.first }.toSet() + result = result.copy(bindings = result.bindings.filter { it.id !in oldBindings || result.usersOfBindings(it.id).isNotEmpty() }) + // Older editor layouts were resolution-specific. Keep each as a selectable saved layout. + for (key in ControllerPrefs.prefs(context).all.keys.filter { it.startsWith("layout.") }.sorted()) { + val size = key.removePrefix("layout.").split('x').mapNotNull { it.toIntOrNull() } + if (size.size != 2 || size.any { it <= 0 } || result.layouts.size >= 128) continue + if (size[0] == metrics.widthPixels && size[1] == metrics.heightPixels) continue + val id = "migrated.$key" + if (result.layouts.none { it.id == id }) result = result.withLayout(SavedInputLayout(id, "Gamepad ${size[0]} × ${size[1]}", + LegacyGamepadLayout(surfaceContext, size[0], size[1]).elements(), style = style, aspect = (size[0].toFloat() / size[1]).coerceIn(.05f, 20f))) + } + return result.validate() + } + + private fun neutralStockLayouts(library: InputLibrary): InputLibrary = library.copy(layouts = library.layouts.map { layout -> + if ((layout.id == DECK || layout.id == TRACKPAD) && layout.style.tint == ControllerPrefs.STEAM_BLUE) layout.copy(style = layout.style.copy(tint = null)) + else layout + }) + + private fun upgradeStockLayouts(library: InputLibrary): InputLibrary { + val stock = InputStockLayouts.layouts() + val replacements = library.layouts.filter { it.template == InputLayoutTemplate.DECK || it.template == InputLayoutTemplate.TRACKPAD } + .associate { old -> old.id to stock.first { it.id == if (old.template == InputLayoutTemplate.DECK) DECK else TRACKPAD }.copy(id = old.id, name = old.name) } + var result = library.copy(layouts = library.layouts.map { replacements[it.id] ?: it }) + val migrated = mutableMapOf, String>() + for (profile in library.profiles) { + val parts = profile.parts.mapValues { (_, part) -> + val layout = replacements[part.layout] ?: return@mapValues part + val id = migrated.getOrPut(part.bindings to layout.id) { + val old = library.preset(part.bindings) + val id = migrationBindingId(result, old.id, layout.id) + val original = library.layouts.first { it.id == layout.id } + val stockLayout = layout.copy(id = if (original.template == InputLayoutTemplate.TRACKPAD) TRACKPAD else DECK) + result = result.withPreset(old.copy(id = id, layoutId = layout.id, + elements = InputStockLayouts.actions(stockLayout) + old.elements, clicks = InputStockLayouts.clicks(stockLayout) + old.clicks)) + id + } + part.copy(bindings = id) + } + result = result.withProfile(profile.copy(parts = parts)) + } + val oldBindings = migrated.keys.map { it.first }.toSet() + result = result.copy(bindings = result.bindings.filter { it.id !in oldBindings || result.usersOfBindings(it.id).isNotEmpty() }) + return addStockLayouts(result) + } + + private fun addStockLayouts(library: InputLibrary): InputLibrary { + val stock = InputStockLayouts.layouts() + var result = library + for (layout in stock) if (result.layouts.none { it.id == layout.id }) result = result.withLayout(layout) + for (preset in InputStockLayouts.presets(stock)) if (result.bindings.none { it.id == preset.id }) result = result.withPreset(preset) + return result.validate() + } + + private fun barStockLayouts(library: InputLibrary): InputLibrary { + var result = library + for (stock in InputStockLayouts.layouts().filter { it.id == DECK || it.id == TRACKPAD }) { + val saved = result.layouts.firstOrNull { it.id == stock.id } ?: continue + val (commands, rest) = saved.elements.partition { it.id.startsWith("command:") } + val untouched = commands.isNotEmpty() && commands.all { it.control == InputControl.A && it.kind == InputElementKind.BUTTON } && + rest.size == stock.elements.size && rest.all { e -> stock.elements.any { s -> s.id == e.id && s.copy(y = e.y, height = e.height) == e && + kotlin.math.abs(s.y + s.height - e.y - e.height) < 1e-3f } } + if (untouched) result = result.withLayout(saved.copy(elements = stock.elements)) + } + return addStockLayouts(result) + } + + private fun deckGamepadSurfaces(context: Context, library: InputLibrary): InputLibrary { + val leftPad = SessionPrefs.gamepadSurface(context, right = false) == SessionPrefs.SURFACE_TRACKPAD + val rightStick = SessionPrefs.gamepadSurface(context, right = true) == SessionPrefs.SURFACE_STICK + if (!leftPad && !rightStick) return library + val saved = library.layouts.firstOrNull { it.id == DeckPanel.GAMEPAD.layout } ?: return library + if (saved.elements != InputStockLayouts.gamepad(leftPad = false, rightStick = false)) return library + return library.withLayout(saved.copy(elements = InputStockLayouts.gamepad(leftPad, rightStick))).validate() + } + + private fun migrationBindingId(library: InputLibrary, binding: String, layout: String): String { + val seed = JSONArray(listOf(binding, layout)).toString().toByteArray(Charsets.UTF_8) + val base = "migrated.bindings." + UUID.nameUUIDFromBytes(seed) + var id = base + var suffix = 0 + while (library.bindings.any { it.id == id }) id = "$base.${++suffix}" + return id + } + + private fun scopeLayouts(library: InputLibrary): InputLibrary = library.copy(layouts = library.layouts.map { layout -> + val roles = library.profiles.flatMap { p -> p.parts.filterValues { it.layout == layout.id }.keys } + + library.selections.values.flatMap { s -> s.parts.filterValues { library.preset(it).layoutId == layout.id }.keys } + val screen = when { + InputRole.PRIMARY in roles && InputRole.SECONDARY in roles -> InputLayoutScreen.BOTH + InputRole.PRIMARY in roles -> InputLayoutScreen.MAIN + InputRole.SECONDARY in roles -> InputLayoutScreen.SECONDARY + layout.id == OVERLAY || layout.id.startsWith("migrated.layout.") -> InputLayoutScreen.MAIN + else -> InputLayoutScreen.SECONDARY + } + layout.copy(screen = screen) + }) + + internal fun encode(library: InputLibrary): String { + val layouts = JSONArray() + for (layout in library.layouts) { + val elements = JSONArray() + for (e in layout.elements) elements.put(JSONObject().put("id", e.id).put("kind", e.kind.name) + .put("control", e.control.id).put("name", e.name).put("x", e.x.toDouble()).put("y", e.y.toDouble()) + .put("width", e.width.toDouble()).put("height", e.height.toDouble()).put("floating", e.floating).put("hideWhenIdle", e.hideWhenIdle).put("mouseGestures", e.mouseGestures).put("toggle", e.toggle).put("icon", e.icon).put("image", e.image) + .put("elementOpacity", e.opacity.toDouble()).put("buttonShape", e.buttonShape.name).put("showLabel", e.showLabel).put("alwaysShown", e.alwaysShown)) + layouts.put(JSONObject().put("id", layout.id).put("name", layout.name).put("template", layout.template.name).put("elements", elements) + .put("tint", layout.style.tint ?: JSONObject.NULL).put("opacity", layout.style.opacity).put("screen", layout.screen.name).put("aspect", layout.aspect.toDouble()).put("scale", layout.scale.toDouble()) + .put("hidden", JSONArray(layout.style.hidden.map { it.name }.sorted())).put("origin", layout.origin ?: JSONObject.NULL)) + } + val bindings = JSONArray() + for (b in library.bindings) { + val controls = JSONObject(); b.controls.forEach { (c, a) -> controls.put(c.id, InputProfiles.encode(a)) } + val elements = JSONObject(); b.elements.forEach { (id, a) -> elements.put(id, InputProfiles.encode(a)) } + val clicks = JSONObject(); b.clicks.forEach { (id, a) -> clicks.put(id, InputProfiles.encode(a)) } + val keys = JSONArray() + b.keys.forEach { binding -> keys.put(JSONObject().put("device", binding.key.device).put("name", binding.key.name) + .put("keyCode", binding.key.keyCode).put("scanCode", binding.key.scanCode).put("action", InputProfiles.encode(binding.action))) } + bindings.put(JSONObject().put("id", b.id).put("name", b.name).put("physical", b.physical) + .put("layout", b.layoutId).put("controls", controls).put("elements", elements).put("keys", keys).put("clicks", clicks)) + } + val profiles = JSONArray() + for (p in library.profiles) { + val parts = JSONObject() + p.parts.forEach { (role, part) -> parts.put(role.name, JSONObject().put("bindings", part.bindings).put("layout", part.layout)) } + profiles.put(JSONObject().put("id", p.id).put("name", p.name).put("base", p.base).put("parts", parts)) + } + val selections = JSONObject() + for ((mode, selection) in library.selections) { + val parts = JSONObject() + selection.parts.forEach { (role, id) -> parts.put(role.name, id) } + selections.put(mode, JSONObject().put("profile", selection.profile).put("parts", parts).put("profileSelected", selection.profileSelected)) + } + return JSONObject().put("selections", selections).put("version", VERSION).put("selected", library.selected).put("steam", library.steam).put("desktop", library.desktop) + .put("profiles", profiles).put("layouts", layouts).put("bindings", bindings).toString() + } + + internal fun decode(raw: String): InputLibrary? = runCatching { + require(raw.length <= 2_000_000) + val doc = JSONObject(raw); require(doc.getInt("version") in 2..VERSION) + val layouts = doc.getJSONArray("layouts").objects(256).map { layout -> + val elements = layout.getJSONArray("elements").objects(64).map { e -> + InputSurfaceElement(requireNotNull(InputControl.fromId(e.getString("control"))), e.getDouble("x").toFloat(), + e.getDouble("y").toFloat(), e.getDouble("width").toFloat(), e.getDouble("height").toFloat(), + e.getString("id"), InputElementKind.valueOf(e.getString("kind")), e.getString("name"), + e.optBoolean("floating", true), e.optBoolean("hideWhenIdle", false), e.optBoolean("mouseGestures", false), e.optBoolean("toggle", false), e.optInt("icon", 0), e.optString("image", ""), + e.optDouble("elementOpacity", 1.0).toFloat(), InputButtonShape.valueOf(e.optString("buttonShape", InputButtonShape.ROUNDED.name)), e.optBoolean("showLabel", true), e.optBoolean("alwaysShown", false)) + } + SavedInputLayout(layout.getString("id"), layout.getString("name"), elements, InputLayoutTemplate.valueOf(layout.getString("template")), + InputSurfaceStyle(if (!layout.has("tint")) ControllerPrefs.STEAM_BLUE else if (layout.isNull("tint")) null else layout.getInt("tint"), layout.optInt("opacity", 100), + layout.optJSONArray("hidden")?.let { hidden -> (0 until hidden.length()).map { InputCommand.valueOf(hidden.getString(it)) }.toSet() }.orEmpty()), + InputLayoutScreen.valueOf(layout.optString("screen", InputLayoutScreen.BOTH.name)), layout.optDouble("aspect", 0.0).toFloat(), layout.optionalString("origin"), + layout.optDouble("scale", 1.0).toFloat()) + } + val bindings = doc.getJSONArray("bindings").objects(512).map { b -> + val controls = b.getJSONObject("controls").actions(28).mapKeys { requireNotNull(InputControl.fromId(it.key)) } + val keys = b.getJSONArray("keys").objects(64).map { k -> PhysicalInputBinding( + PhysicalInputKey(k.getString("device"), k.getString("name"), k.getInt("keyCode"), k.getInt("scanCode")), + requireNotNull(InputProfiles.decode(k.getJSONObject("action")))) } + SavedInputBindings(b.getString("id"), b.getString("name"), b.getBoolean("physical"), b.optionalString("layout"), + controls, b.getJSONObject("elements").actions(64), keys, b.optJSONObject("clicks")?.actions(64).orEmpty()) + } + val profiles = doc.getJSONArray("profiles").objects(64).map { p -> + val parts = p.getJSONObject("parts") + SavedInputProfile(p.getString("id"), p.getString("name"), p.getString("base"), InputRole.entries.associateWith { role -> + val part = parts.getJSONObject(role.name); InputPart(part.getString("bindings"), part.optionalString("layout")) + }) + } + val selections = doc.optJSONObject("selections")?.let { saved -> + require(saved.length() <= 2) + saved.keys().asSequence().associateWith { mode -> + val value = saved.getJSONObject(mode) + val parts = value.getJSONObject("parts") + require(parts.length() <= InputRole.entries.size) + val profile = value.getString("profile") + InputSelection(profile, parts.keys().asSequence().associate { role -> InputRole.valueOf(role) to parts.getString(role) }, + value.optBoolean("profileSelected", profile != InputProfiles.AUTO || parts.length() == 0)) + } + }.orEmpty() + InputLibrary(profiles, layouts, bindings, doc.getString("selected"), doc.getString("steam"), doc.getString("desktop"), selections).validate() + }.getOrNull() + + private fun JSONArray.objects(limit: Int): List { + require(length() <= limit) + return (0 until length()).map { getJSONObject(it) } + } + + private fun JSONObject.actions(limit: Int): Map { + require(length() <= limit) + return keys().asSequence().associateWith { requireNotNull(InputProfiles.decode(getJSONObject(it))) } + } + + private fun JSONObject.optionalString(key: String): String? = if (isNull(key)) null else getString(key) +} diff --git a/app/src/main/java/com/droiddeck/launcher/input/InputLibrary.kt b/app/src/main/java/com/droiddeck/launcher/input/InputLibrary.kt new file mode 100644 index 0000000000..cc6313dc30 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/input/InputLibrary.kt @@ -0,0 +1,304 @@ +package com.droiddeck.launcher.input + +import java.util.UUID + +enum class InputLayoutTemplate { CUSTOM, OVERLAY, DECK, TRACKPAD } + +enum class InputLayoutScreen { MAIN, SECONDARY, BOTH; + fun supports(role: InputRole) = role != InputRole.PHYSICAL && (this == BOTH || + if (role == InputRole.PRIMARY) this == MAIN else this == SECONDARY) +} + +data class InputSurfaceStyle(val tint: Int? = ControllerPrefs.STEAM_BLUE, val opacity: Int = 100, val hidden: Set = emptySet()) { + fun hides(element: InputSurfaceElement) = element.quickButton?.let { it in hidden } == true + fun shown(elements: List) = if (hidden.isEmpty()) elements else elements.filterNot(::hides) +} + +data class SavedInputLayout( + val id: String, val name: String, val elements: List = emptyList(), + val template: InputLayoutTemplate = InputLayoutTemplate.CUSTOM, + val style: InputSurfaceStyle = InputSurfaceStyle(), + val screen: InputLayoutScreen = InputLayoutScreen.BOTH, + val aspect: Float = 0f, + val origin: String? = null, + val scale: Float = 1f, +) { + val editable get() = template == InputLayoutTemplate.CUSTOM + val quickButtons get() = QUICK_BUTTONS.filter { button -> elements.any { it.quickButton == button } } + fun withQuickButton(button: InputCommand, shown: Boolean) = + copy(style = style.copy(hidden = if (shown) style.hidden - button else style.hidden + button)) + fun derived(id: String, name: String) = copy(id = id, name = name, origin = origin ?: this.id) + val adaptiveSticks get() = elements.filter { it.kind == InputElementKind.STICK }.takeIf { editable && it.isNotEmpty() }?.all { it.floating && it.hideWhenIdle } + fun supports(role: InputRole) = screen.supports(role) && when (template) { + InputLayoutTemplate.OVERLAY -> role == InputRole.PRIMARY + InputLayoutTemplate.DECK, InputLayoutTemplate.TRACKPAD -> role == InputRole.SECONDARY + InputLayoutTemplate.CUSTOM -> true + } +} + +data class SavedInputBindings( + val id: String, val name: String, val physical: Boolean, + val layoutId: String? = null, + val controls: Map = emptyMap(), + val elements: Map = emptyMap(), + val keys: List = emptyList(), + val clicks: Map = emptyMap(), +) { + fun compatible(layout: SavedInputLayout?) = if (physical) layout == null + else layout != null && if (layout.editable) layoutId == layout.id else layoutId == null + + fun action(element: InputSurfaceElement) = elements[element.id] + ?: if (element.id == "legacy:${element.control.id}") controls[element.control] ?: InputAction.Native() + else InputAction.Disabled + + fun clickAction(element: InputSurfaceElement) = clicks[element.id] + ?: defaultElementClick(element, action(element)) { controls[it] ?: InputAction.Native() } +} + +data class InputSelection( + val profile: String = InputProfiles.AUTO, val parts: Map = emptyMap(), val profileSelected: Boolean = false, +) + +data class InputPart(val bindings: String, val layout: String? = null) + +data class SavedInputProfile(val id: String, val name: String, val base: String, val parts: Map) { + fun binding(role: InputRole) = parts.getValue(role).bindings +} + +data class InputLibrary( + val profiles: List, val layouts: List, val bindings: List, + val selected: String = InputProfiles.AUTO, + val steam: String = InputProfile.GAME, val desktop: String = InputProfile.DESKTOP, + val selections: Map = emptyMap(), +) { + fun profile(id: String) = profiles.first { it.id == id } + fun preset(id: String) = bindings.first { it.id == id } + fun layout(profile: String, role: InputRole) = profiles.firstOrNull { it.id == profile }?.parts?.get(role)?.layout + ?.let { id -> layouts.firstOrNull { it.id == id } } + private fun mode(isDesktop: Boolean) = if (isDesktop) InputProfile.DESKTOP else InputProfile.GAME + fun selection(isDesktop: Boolean) = selections[mode(isDesktop)] ?: InputSelection(selected) + fun activeId(isDesktop: Boolean): String { + val id = selection(isDesktop).profile + return if (id == InputProfiles.AUTO) { if (isDesktop) desktop else steam } else id + } + fun selectProfile(isDesktop: Boolean, id: String) = copy(selections = selections + (mode(isDesktop) to InputSelection(id, profileSelected = true))) + fun selectPart(isDesktop: Boolean, role: InputRole, bindingsId: String): InputLibrary { + val previous = selection(isDesktop) + return copy(selections = selections + (mode(isDesktop) to previous.copy(parts = previous.parts + (role to bindingsId)))) + } + fun activePart(isDesktop: Boolean, role: InputRole): InputPart { + val binding = selection(isDesktop).parts[role]?.let(::preset) + return if (binding != null) InputPart(binding.id, binding.layoutId) else profile(activeId(isDesktop)).parts.getValue(role) + } + fun activeLayout(isDesktop: Boolean, role: InputRole) = activePart(isDesktop, role).layout?.let { id -> layouts.first { it.id == id } } + fun panelPart(isDesktop: Boolean, panel: String?): InputPart = + panel?.let { id -> bindings.firstOrNull { it.id == id } }?.let { InputPart(it.id, it.layoutId) } ?: activePart(isDesktop, InputRole.SECONDARY) + fun panelLayout(isDesktop: Boolean, panel: String?) = panelPart(isDesktop, panel).layout?.let { id -> layouts.firstOrNull { it.id == id } } + fun compileActive(isDesktop: Boolean, panel: String? = null): InputProfile = compileParts(activeId(isDesktop), InputRole.entries.associateWith { + preset(if (it == InputRole.SECONDARY) panelPart(isDesktop, panel).bindings else activePart(isDesktop, it).bindings) + }) + + fun saveSelection(isDesktop: Boolean, name: String, id: String = newInputId()): InputLibrary { + val current = profile(activeId(isDesktop)) + var result = withProfile(SavedInputProfile(id, inputName(name), current.base, InputRole.entries.associateWith { activePart(isDesktop, it) })) + for (role in InputRole.entries) result = result.usePart(id, role, id, shared = false) + return result.selectProfile(isDesktop, id) + } + + + fun compile(id: String): InputProfile { + val profile = profile(id) + val parts = InputRole.entries.associateWith { preset(profile.binding(it)) } + return compileParts(id, parts) + } + + private fun compileParts(id: String, parts: Map): InputProfile { + return InputProfile(id, parts.mapValues { it.value.controls }.filterValues { it.isNotEmpty() }, + parts.getValue(InputRole.PHYSICAL).keys, + parts.mapValues { it.value.elements }.filterValues { it.isNotEmpty() }, + parts.mapValues { it.value.clicks }.filterValues { it.isNotEmpty() }) + } + + fun usersOfBindings(id: String) = profiles.filter { p -> p.parts.values.any { it.bindings == id } } + fun selectedBindings(id: String) = selections.values.any { id in it.parts.values } + fun usersOfLayout(id: String) = profiles.filter { p -> p.parts.values.any { it.layout == id } } + fun withProfile(value: SavedInputProfile) = copy(profiles = profiles.filterNot { it.id == value.id } + value) + fun withPreset(value: SavedInputBindings) = copy(bindings = bindings.filterNot { it.id == value.id } + value) + fun withLayout(value: SavedInputLayout) = copy(layouts = layouts.filterNot { it.id == value.id } + value, + bindings = bindings.map { preset -> if (preset.layoutId != value.id) preset else preset.copy( + elements = preset.elements.filter { (id, action) -> value.elements.any { it.id == id && action.validFor(it) } }, + clicks = preset.clicks.filter { (id, action) -> value.elements.any { it.id == id && it.hasClick && action.validForClick() } }) }) + + fun duplicateProfile(id: String, name: String, newId: String = newInputId(), sharePhysical: Boolean = false): InputLibrary { + require(profiles.size < 64) + var result = withProfile(profile(id).copy(id = newId, name = inputName(name))) + for (role in InputRole.entries) result = result.usePart(newId, role, id, shared = sharePhysical && role == InputRole.PHYSICAL) + return result + } + + fun usePart(profileId: String, role: InputRole, sourceId: String, shared: Boolean): InputLibrary { + val target = profile(profileId) + val source = profile(sourceId).parts.getValue(role) + if (shared) return withProfile(target.copy(parts = target.parts + (role to source))) + val sourceLayout = layout(sourceId, role) + val name = (target.name + " / " + role.name.lowercase()).take(80) + val copiedLayout = sourceLayout?.takeIf { it.editable }?.derived(newInputId(), name) + val copiedBindings = preset(source.bindings).copy(id = newInputId(), name = name, + layoutId = copiedLayout?.id ?: preset(source.bindings).layoutId) + return (if (copiedLayout == null) this else withLayout(copiedLayout)).withPreset(copiedBindings) + .withProfile(target.copy(parts = target.parts + (role to InputPart(copiedBindings.id, copiedLayout?.id ?: source.layout)))) + } + + fun useLayout(profileId: String, role: InputRole, layoutId: String, shared: Boolean): InputLibrary { + val source = layouts.first { it.id == layoutId } + require(source.supports(role)) + val target = profile(profileId) + val layout = if (shared || !source.editable) source else source.derived(newInputId(), (target.name + " / " + source.name).take(80)) + val previous = preset(target.binding(role)) + val actions = if (previous.compatible(source)) previous else bindings.firstOrNull { it.compatible(source) } ?: SavedInputBindings(newInputId(), target.name, false, + controls = if (source.editable) emptyMap() else InputProfile.defaults(target.base).bindings[role].orEmpty(), + elements = if (source.editable) source.elements.associate { it.id to InputAction.Disabled } else emptyMap()) + val copied = actions.copy(id = newInputId(), name = (target.name + " / " + role.name.lowercase()).take(80), + layoutId = layout.id.takeIf { layout.editable }) + return withLayout(layout).withPreset(copied) + .withProfile(target.copy(parts = target.parts + (role to InputPart(copied.id, layout.id)))) + } + + fun copyBindings(id: String, name: String, profileId: String? = null, role: InputRole? = null, newId: String = newInputId()): InputLibrary { + require(bindings.size < 512) + var result = withPreset(preset(id).copy(id = newId, name = inputName(name))) + if (profileId != null && role != null) result = result.chooseBindings(profileId, role, newId) + return result + } + + fun chooseBindings(profileId: String, role: InputRole, id: String): InputLibrary { + val profile = profile(profileId) + require(preset(id).compatible(layout(profileId, role))) + return withProfile(profile.copy(parts = profile.parts + (role to profile.parts.getValue(role).copy(bindings = id)))) + } + + fun chooseLayout(profileId: String, role: InputRole, id: String): InputLibrary { + val layout = layouts.first { it.id == id } + require(layout.supports(role)) + val profile = profile(profileId) + val previous = preset(profile.binding(role)) + val compatible = bindings.firstOrNull { it.id == previous.id && it.compatible(layout) } + ?: bindings.firstOrNull { it.compatible(layout) } + ?: SavedInputBindings(newInputId(), layout.name, false, if (layout.editable) layout.id else null) + return withPreset(compatible).withProfile(profile.copy(parts = profile.parts + (role to InputPart(compatible.id, id)))) + } + + fun copyLayout(id: String, name: String, newId: String = newInputId()): InputLibrary { + val source = layouts.first { it.id == id } + require(source.editable) + return withLayout(source.derived(newId, inputName(name))) + } + + fun removeProfile(id: String): InputLibrary { + require(id != InputProfile.GAME && id != InputProfile.DESKTOP) + return copy(profiles = profiles.filterNot { it.id == id }, selected = selected.takeUnless { it == id } ?: InputProfiles.AUTO, + steam = steam.takeUnless { it == id } ?: InputProfile.GAME, desktop = desktop.takeUnless { it == id } ?: InputProfile.DESKTOP, + selections = selections.mapValues { (_, selection) -> if (selection.profile == id) selection.copy(profile = InputProfiles.AUTO) else selection }) + } + + fun removeLayout(id: String): InputLibrary { + require(usersOfLayout(id).isEmpty() && bindings.none { it.layoutId == id && selectedBindings(it.id) } && layouts.first { it.id == id }.editable) + return copy(layouts = layouts.filterNot { it.id == id }, bindings = bindings.filterNot { it.layoutId == id }) + } + + fun removeBindings(id: String): InputLibrary { + require(usersOfBindings(id).isEmpty() && !selectedBindings(id)) + return copy(bindings = bindings.filterNot { it.id == id }) + } + + fun resetProfile(id: String): InputLibrary { + val p = profile(id) + var result = this + val defaults = InputProfile.defaults(p.base) + val parts = p.parts.mapValues { (role, part) -> + val previous = preset(part.bindings) + val isPrivate = usersOfBindings(part.bindings).all { it.id == id } + val preset = SavedInputBindings(if (isPrivate) previous.id else newInputId(), + if (isPrivate) previous.name else (p.name + " / " + role.name.lowercase()).take(80), role == InputRole.PHYSICAL, + part.layout?.takeIf { layout(id, role)?.editable == true }, defaults.bindings[role].orEmpty(), + elements = (layout(id, role)?.let { InputStockLayouts.actions(it) } ?: emptyMap()) + layout(id, role)?.elements.orEmpty().filter { it.id == "builtin:keyboard" } + .associate { it.id to InputAction.Command(InputCommand.KEYBOARD) }) + result = result.withPreset(preset) + part.copy(bindings = preset.id) + } + return result.withProfile(p.copy(parts = parts)) + } + + fun validate(): InputLibrary { + require(profiles.size in 2..64 && layouts.size in 3..256 && bindings.size in 1..512) + fun unique(ids: List) { require(ids.all { it.isNotBlank() && it.length <= 128 } && ids.distinct().size == ids.size) } + unique(profiles.map { it.id }); unique(layouts.map { it.id }); unique(bindings.map { it.id }) + require(profiles.any { it.id == InputProfile.GAME } && profiles.any { it.id == InputProfile.DESKTOP }) + require(selected == InputProfiles.AUTO || profiles.any { it.id == selected }) + require(profiles.any { it.id == steam } && profiles.any { it.id == desktop }) + for (layout in layouts) { + require(layout.style.opacity in 1..100 && layout.style.hidden.all { it in QUICK_BUTTONS }) + require(layout.aspect == 0f || layout.aspect.isFinite() && layout.aspect in .05f..20f) + require(layout.scale.isFinite() && layout.scale in .5f..2f) + inputName(layout.name); require(layout.elements.size <= 64); unique(layout.elements.map { it.id }) + for (e in layout.elements) { + inputName(e.name) + require(e.icon == 0 || InputButtonIcons.has(e.icon)) + require(e.image.isEmpty() || e.image.matches(Regex("[A-Za-z0-9_.-]{1,128}")) && e.image != "." && e.image != "..") + require(e.opacity.isFinite() && e.opacity in 0.01f..1f) + require(!e.toggle || e.kind == InputElementKind.BUTTON || e.kind == InputElementKind.TRIGGER) + require(listOf(e.x, e.y, e.width, e.height).all { it.isFinite() }) + require(e == e.bounded()) + require(if (e.id.startsWith("legacy:")) e.id == "legacy:${e.control.id}" && e.kind == InputElementKind.from(e.control) + else e.control == e.kind.control) + } + } + for (preset in bindings) { + inputName(preset.name) + require(preset.controls.all { (c, a) -> a.validFor(c) }) + require(preset.elements.size <= 64 && preset.keys.size <= 64 && preset.clicks.size <= 64) + require(preset.keys.map { Triple(it.key.device, it.key.keyCode, it.key.scanCode) }.distinct().size == preset.keys.size) + require(preset.physical || preset.keys.isEmpty()) + val layout = preset.layoutId?.let { id -> layouts.first { it.id == id } } + require(!preset.physical || layout == null && preset.elements.isEmpty() && preset.clicks.isEmpty()) + require(preset.elements.all { (id, action) -> layout?.elements?.firstOrNull { it.id == id }?.let { action.validFor(it) } == true }) + require(preset.clicks.all { (id, action) -> layout?.elements?.any { it.id == id && it.hasClick } == true && action.validForClick() }) + require(preset.keys.all { it.key.device.isNotBlank() && it.key.keyCode >= 0 && it.key.scanCode >= 0 && + it.action.validFor(InputControl.A) && (it.action !is InputAction.Native || it.action.target == null) }) + } + require(selections.keys.all { it == InputProfile.GAME || it == InputProfile.DESKTOP }) + for (selection in selections.values) { + require(selection.profile == InputProfiles.AUTO || profiles.any { it.id == selection.profile }) + for ((role, id) in selection.parts) { + val binding = preset(id) + require(binding.physical == (role == InputRole.PHYSICAL)) + val layout = binding.layoutId?.let { lid -> layouts.first { it.id == lid } } + require(binding.compatible(layout) && (layout == null || layout.supports(role))) + } + } + for (p in profiles) { + inputName(p.name); require(p.base == InputProfile.GAME || p.base == InputProfile.DESKTOP) + require(p.parts.keys == InputRole.entries.toSet()) + for ((role, part) in p.parts) { + val layout = part.layout?.let { id -> layouts.first { it.id == id } } + require(if (role == InputRole.PHYSICAL) layout == null else layout?.supports(role) == true) + require(preset(part.bindings).compatible(layout)) + } + } + return this + } +} + +internal fun inputName(value: String): String = value.trim().also { require(it.isNotEmpty() && it.length <= 80) } +fun newInputId(): String = UUID.randomUUID().toString() + +internal fun InputAction.validFor(element: InputSurfaceElement): Boolean = when { + this !is InputAction.Native -> validFor(element.control) + element.id.startsWith("legacy:") -> validFor(element.control) + element.kind == InputElementKind.DPAD -> target == null + target == null -> false + element.kind == InputElementKind.TRACKPAD -> target.isTrackpad + element.kind == InputElementKind.STICK -> target == InputControl.LS || target == InputControl.RS + element.kind == InputElementKind.TRIGGER -> target == InputControl.LT || target == InputControl.RT + else -> target.dimensions == 1 && target != InputControl.LT && target != InputControl.RT +} diff --git a/app/src/main/java/com/droiddeck/launcher/input/InputMotion.kt b/app/src/main/java/com/droiddeck/launcher/input/InputMotion.kt new file mode 100644 index 0000000000..9d24ca8d54 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/input/InputMotion.kt @@ -0,0 +1,35 @@ +package com.droiddeck.launcher.input + +import android.os.Handler +import android.os.Looper +import android.view.Choreographer + +class InputMotion(private val router: () -> InputRouter?) : Choreographer.FrameCallback { + private val handler = Handler(Looper.getMainLooper()) + private var scheduled = false + private var lastFrame = 0L + private val start = Runnable { + if (!scheduled) { + scheduled = true + lastFrame = System.nanoTime() + Choreographer.getInstance().postFrameCallback(this) + } + } + + fun request() { + if (Looper.myLooper() == Looper.getMainLooper()) start.run() else handler.post(start) + } + + override fun doFrame(frameTimeNanos: Long) { + val dt = (frameTimeNanos - lastFrame).coerceAtLeast(0) / 1_000_000_000f + lastFrame = frameTimeNanos + if (router()?.tick(dt) == true) Choreographer.getInstance().postFrameCallback(this) + else scheduled = false + } + + fun stop() { + handler.removeCallbacks(start) + Choreographer.getInstance().removeFrameCallback(this) + scheduled = false + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/input/InputProfile.kt b/app/src/main/java/com/droiddeck/launcher/input/InputProfile.kt new file mode 100644 index 0000000000..2375cf34e8 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/input/InputProfile.kt @@ -0,0 +1,107 @@ +package com.droiddeck.launcher.input + +enum class InputRole { PHYSICAL, PRIMARY, SECONDARY } + +enum class InputControl(val id: String, val label: String, val dimensions: Int = 1) { + A("a", "A"), B("b", "B"), X("x", "X"), Y("y", "Y"), + LB("lb", "L1"), RB("rb", "R1"), SELECT("select", "View / Select"), START("start", "Menu / Start"), + L3("l3", "L3"), R3("r3", "R3"), GUIDE("guide", "Steam / Guide"), QAM("qam", "Quick access"), + UP("up", "D-pad up"), RIGHT("right", "D-pad right"), DOWN("down", "D-pad down"), LEFT("left", "D-pad left"), + LT("lt", "L2"), RT("rt", "R2"), LS("ls", "Left stick", 2), RS("rs", "Right stick", 2), + L4("l4", "L4"), R4("r4", "R4"), L5("l5", "L5"), R5("r5", "R5"), + LEFT_PAD("left-pad", "Left trackpad", 2), RIGHT_PAD("right-pad", "Right trackpad", 2), + LEFT_CLICK("left-click", "Left trackpad click"), RIGHT_CLICK("right-click", "Right trackpad click"); + + val isPad: Boolean get() = ordinal <= RS.ordinal + val isTrackpad: Boolean get() = this == LEFT_PAD || this == RIGHT_PAD + + companion object { + val padControls = entries.filter { it.isPad }.toTypedArray() + fun fromId(id: String) = entries.firstOrNull { it.id == id } + } +} + +enum class InputCommand { MENU, KEYBOARD, STEAM, QUICK_ACCESS } + +sealed interface InputAction { + data class Native(val target: InputControl? = null) : InputAction + data object Disabled : InputAction + data class Keys(val codes: List) : InputAction + data class MouseButton(val code: Int) : InputAction + data class Command(val command: InputCommand) : InputAction + data class Directions(val up: Int, val right: Int, val down: Int, val left: Int, val diagonals: List? = null) : InputAction + data class Pointer(val speed: Float = 900f) : InputAction + data class Scroll(val speed: Float = 10f, val horizontal: Boolean = false) : InputAction +} + +data class InputProfile( + val id: String, + val bindings: Map> = emptyMap(), + val keys: List = emptyList(), + val elements: Map> = emptyMap(), + val clicks: Map> = emptyMap(), +) { + fun action(role: InputRole, control: InputControl): InputAction = + bindings[role]?.get(control) ?: InputAction.Native() + + fun withBinding(role: InputRole, control: InputControl, action: InputAction) = + copy(bindings = bindings + (role to (bindings[role].orEmpty() + (control to action)))) + + fun elementAction(role: InputRole, id: String, control: InputControl): InputAction = + elements[role]?.get(id) ?: if (id == "legacy:${control.id}") action(role, control) else InputAction.Disabled + + fun elementClickAction(role: InputRole, element: InputSurfaceElement): InputAction = clicks[role]?.get(element.id) + ?: defaultElementClick(element, elementAction(role, element.id, element.control)) { action(role, it) } + + companion object { + const val GAME = "game" + const val DESKTOP = "desktop" + + fun defaults(id: String): InputProfile { + if (id != DESKTOP) return InputProfile(GAME) + val desktop = mapOf( + InputControl.LS to InputAction.Pointer(), InputControl.RS to InputAction.Scroll(), + InputControl.A to InputAction.MouseButton(272), InputControl.B to InputAction.MouseButton(273), + InputControl.X to InputAction.Keys(listOf(28)), InputControl.Y to InputAction.Keys(listOf(1)), + InputControl.UP to InputAction.Keys(listOf(103)), InputControl.RIGHT to InputAction.Keys(listOf(106)), + InputControl.DOWN to InputAction.Keys(listOf(108)), InputControl.LEFT to InputAction.Keys(listOf(105)), + InputControl.LT to InputAction.MouseButton(273), InputControl.RT to InputAction.MouseButton(272), + InputControl.LB to InputAction.Keys(listOf(29)), InputControl.RB to InputAction.Keys(listOf(56)), + InputControl.SELECT to InputAction.Command(InputCommand.KEYBOARD), + InputControl.START to InputAction.Command(InputCommand.MENU), + InputControl.LEFT_PAD to InputAction.Scroll(), InputControl.RIGHT_PAD to InputAction.Pointer(), + InputControl.LEFT_CLICK to InputAction.MouseButton(273), InputControl.RIGHT_CLICK to InputAction.MouseButton(272), + ) + return InputProfile(DESKTOP, InputRole.entries.associateWith { desktop }) + } + } +} + +internal fun defaultElementClick(element: InputSurfaceElement, action: InputAction, legacy: (InputControl) -> InputAction): InputAction { + fun click(control: InputControl) = when (control) { + InputControl.LS -> InputControl.L3; InputControl.RS -> InputControl.R3 + InputControl.LEFT_PAD -> InputControl.LEFT_CLICK; InputControl.RIGHT_PAD -> InputControl.RIGHT_CLICK + else -> null + } + if (!element.hasClick) return InputAction.Disabled + if (element.id == "legacy:${element.control.id}") { + val target = click(element.control) ?: return InputAction.Disabled + return legacy(target).let { if (it is InputAction.Native && it.target == null) InputAction.Native(target) else it } + } + return (action as? InputAction.Native)?.let { click(it.target ?: element.control) }?.let { InputAction.Native(it) } ?: InputAction.Disabled +} + +internal fun InputAction.validForClick(): Boolean = if (this is InputAction.Native) + target != null && target.dimensions == 1 && target != InputControl.LT && target != InputControl.RT +else validFor(InputControl.A) + +internal fun InputAction.validFor(control: InputControl): Boolean = when (this) { + is InputAction.Native -> target == null || (control.isPad == target.isPad && control.dimensions == target.dimensions && + (!control.isTrackpad && !target.isTrackpad || control.isTrackpad && target.isTrackpad)) + is InputAction.Keys -> codes.isNotEmpty() && codes.size <= 8 && codes.distinct().size == codes.size && codes.all { it in 1..255 } + is InputAction.MouseButton -> code in 272..276 + is InputAction.Directions -> control.dimensions == 2 && listOf(up, right, down, left).all { it in 0..255 } && (diagonals == null || diagonals.size == 4 && diagonals.all { it in 0..255 }) + is InputAction.Pointer -> control.dimensions == 2 && speed.isFinite() && speed in 100f..3000f + is InputAction.Scroll -> control.dimensions == 2 && speed.isFinite() && speed in 1f..60f + else -> true +} diff --git a/app/src/main/java/com/droiddeck/launcher/input/InputProfiles.kt b/app/src/main/java/com/droiddeck/launcher/input/InputProfiles.kt new file mode 100644 index 0000000000..16d41bfd0b --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/input/InputProfiles.kt @@ -0,0 +1,139 @@ +package com.droiddeck.launcher.input + +import android.content.Context +import org.json.JSONArray +import org.json.JSONObject + +object InputProfiles { + const val AUTO = "auto" + private const val VERSION = 1 + + private fun prefs(context: Context) = context.getSharedPreferences("input-profiles", Context.MODE_PRIVATE) + + fun selection(context: Context): String = InputLibraries.read(context).selected + + fun select(context: Context, id: String) { + InputLibraries.update(context) { library -> + require(id == AUTO || library.profiles.any { it.id == id }) + library.copy(selected = id, selections = emptyMap()) + } + } + + fun active(context: Context, desktop: Boolean): InputProfile { + val library = InputLibraries.read(context) + return library.compileActive(desktop) + } + + fun read(context: Context, id: String): InputProfile { + return InputLibraries.read(context).compile(id) + } + + internal fun readLegacy(context: Context, id: String): InputProfile { + migrate(context) + var result = InputProfile.defaults(id) + val stored = prefs(context).getString("profile.$id", null) ?: return result + val document = runCatching { JSONObject(stored) }.getOrNull() ?: return result + if (document.optInt("version") != VERSION) return result + val bindings = document.optJSONArray("bindings") ?: return result + for (i in 0 until minOf(bindings.length(), 256)) { + val entry = bindings.optJSONObject(i) ?: continue + val role = runCatching { InputRole.valueOf(entry.getString("role")) }.getOrNull() ?: continue + val control = InputControl.fromId(entry.optString("control")) ?: continue + val action = decode(entry.optJSONObject("action") ?: continue) ?: continue + if (action.validFor(control)) result = result.withBinding(role, control, action) + } + val keys = document.optJSONArray("keys") + if (keys != null) result = result.copy(keys = (0 until minOf(keys.length(), 64)).mapNotNull { i -> + runCatching { + val item = keys.getJSONObject(i) + val action = decode(item.getJSONObject("action")) ?: return@mapNotNull null + if (!action.validFor(InputControl.A) || action is InputAction.Native && action.target != null) return@mapNotNull null + val key = PhysicalInputKey(item.getString("device"), item.getString("name"), item.getInt("keyCode"), item.getInt("scanCode")) + if (key.device.isBlank() || key.keyCode < 0 || key.scanCode < 0) return@mapNotNull null + PhysicalInputBinding(key, action) + }.getOrNull() + }.distinctBy { Triple(it.key.device, it.key.keyCode, it.key.scanCode) }) + return result + } + + fun save(context: Context, profile: InputProfile) { + InputLibraries.update(context) { library -> + val definition = library.profile(profile.id) + var updated = library + for (role in InputRole.entries) { + val preset = updated.preset(definition.binding(role)) + updated = updated.withPreset(preset.copy(controls = profile.bindings[role].orEmpty(), + elements = profile.elements[role].orEmpty(), keys = if (role == InputRole.PHYSICAL) profile.keys else emptyList(), + clicks = profile.clicks[role].orEmpty())) + } + updated + } + } + + private fun saveLegacy(context: Context, profile: InputProfile) { + val bindings = JSONArray() + for ((role, controls) in profile.bindings) for ((control, action) in controls) { + require(action.validFor(control)) + bindings.put(JSONObject().put("role", role.name).put("control", control.id).put("action", encode(action))) + } + val keys = JSONArray() + for (binding in profile.keys) keys.put(JSONObject().put("device", binding.key.device).put("name", binding.key.name) + .put("keyCode", binding.key.keyCode).put("scanCode", binding.key.scanCode).put("action", encode(binding.action))) + prefs(context).edit().putString("profile.${profile.id}", JSONObject() + .put("version", VERSION).put("id", profile.id).put("bindings", bindings).put("keys", keys).toString()).apply() + } + + fun reset(context: Context, id: String) = InputLibraries.update(context) { it.resetProfile(id) }.let { } + + fun resetAll(context: Context) { + prefs(context).edit().clear().putBoolean("migrated", true).apply() + InputSurfaceLayout.resetAll(context) + } + + private fun migrate(context: Context) { + val p = prefs(context) + if (p.getBoolean("migrated", false)) return + var game = InputProfile.defaults(InputProfile.GAME) + val old = ControllerPrefs.read(context) + for ((id, target) in old.mapping) { + val source = InputControl.fromId(id) ?: continue + val action = if (target == ControllerPrefs.OFF) InputAction.Disabled + else InputAction.Native(InputControl.fromId(target) ?: continue) + if (target != id) game = game.withBinding(InputRole.PRIMARY, source, action) + } + if (old.selectSteam) game = game.withBinding(InputRole.PHYSICAL, InputControl.SELECT, InputAction.Native(InputControl.GUIDE)) + if (!p.contains("profile.${InputProfile.GAME}")) saveLegacy(context, game) + if (old.selectSteam && !p.contains("profile.${InputProfile.DESKTOP}")) { + saveLegacy(context, InputProfile.defaults(InputProfile.DESKTOP) + .withBinding(InputRole.PHYSICAL, InputControl.SELECT, InputAction.Native(InputControl.GUIDE))) + } + p.edit().putBoolean("migrated", true).apply() + } + + internal fun encode(action: InputAction): JSONObject = JSONObject().apply { + when (action) { + is InputAction.Native -> { put("type", "native"); action.target?.let { put("target", it.id) } } + InputAction.Disabled -> put("type", "disabled") + is InputAction.Keys -> { put("type", "keys"); put("codes", JSONArray(action.codes)) } + is InputAction.MouseButton -> { put("type", "button"); put("code", action.code) } + is InputAction.Command -> { put("type", "command"); put("command", action.command.name) } + is InputAction.Directions -> { put("type", "directions"); put("codes", JSONArray(listOf(action.up, action.right, action.down, action.left))); action.diagonals?.let { put("diagonals", JSONArray(it)) } } + is InputAction.Pointer -> { put("type", "pointer"); put("speed", action.speed.toDouble()) } + is InputAction.Scroll -> { put("type", "scroll"); put("speed", action.speed.toDouble()); put("horizontal", action.horizontal) } + } + } + + internal fun decode(json: JSONObject): InputAction? = runCatching { + when (json.getString("type")) { + "native" -> InputAction.Native(if (json.has("target")) InputControl.fromId(json.getString("target")) ?: return null else null) + "disabled" -> InputAction.Disabled + "keys" -> InputAction.Keys(json.getJSONArray("codes").let { a -> (0 until a.length()).map { a.getInt(it) } }) + "button" -> InputAction.MouseButton(json.getInt("code")) + "command" -> InputAction.Command(InputCommand.valueOf(json.getString("command"))) + "directions" -> json.getJSONArray("codes").let { InputAction.Directions(it.getInt(0), it.getInt(1), it.getInt(2), it.getInt(3), json.optJSONArray("diagonals")?.let { d -> (0 until d.length()).map { d.getInt(it) } }) } + "pointer" -> InputAction.Pointer(json.getDouble("speed").toFloat()) + "scroll" -> InputAction.Scroll(json.getDouble("speed").toFloat(), json.optBoolean("horizontal", false)) + else -> null + } + }.getOrNull() +} diff --git a/app/src/main/java/com/droiddeck/launcher/input/InputRouter.kt b/app/src/main/java/com/droiddeck/launcher/input/InputRouter.kt new file mode 100644 index 0000000000..6907af4d41 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/input/InputRouter.kt @@ -0,0 +1,361 @@ +package com.droiddeck.launcher.input + +import kotlin.math.abs + +interface InputOutput { + fun key(code: Int, down: Boolean) + fun button(code: Int, down: Boolean) + fun pointer(dx: Float, dy: Float) + fun scroll(steps: Int) + fun scrollHorizontal(steps: Int) {} + fun command(command: InputCommand) + fun command(command: InputCommand, role: InputRole) = command(command) + fun requestMotion() +} + +class InputRouter(private val output: InputOutput) { + private val keys = IntArray(256) + private val buttons = IntArray(5) + private val channels = ArrayList() + private val pads = ArrayList() + private var profile = InputProfile.defaults(InputProfile.GAME) + private var moving = false + private var wheel = 0f + private var horizontalWheel = 0f + private var enabled = true + private val snapshots = ArrayList<() -> Unit>() + + @Synchronized + fun beforeProfileChange(snapshot: () -> Unit) { snapshots.add(snapshot) } + + @Synchronized + fun removeSnapshot(snapshot: () -> Unit) { snapshots.remove(snapshot) } + + @Synchronized + fun setEnabled(on: Boolean): Boolean { + if (enabled == on) return false + if (!on) releaseAll() + enabled = on + for (pad in pads) pad.refreshIdentity() + return true + } + + @Synchronized + fun setProfile(next: InputProfile) { + if (profile == next) return + for (snapshot in snapshots) snapshot() + for (pad in pads) pad.snapshot() + profile = next + for (channel in channels) channel.configure(channel.actionFrom(next)) + for (pad in pads) pad.refreshIdentity() + wheel = 0f + horizontalWheel = 0f + moving = false + } + + @JvmOverloads + @Synchronized + fun pad(role: InputRole, retained: PadState? = null): PadSource = PadSource(role, retained).also { pads.add(it) } + + @Synchronized + fun channel(role: InputRole, control: InputControl, fixedAction: InputAction? = null, element: InputSurfaceElement? = null, elementClick: Boolean = false): Channel = Channel(role, control, fixedAction, element, elementClick).also { + channels.add(it) + it.configure(it.actionFrom(profile)) + } + + @Synchronized + fun releaseAll() { + for (snapshot in snapshots) snapshot() + for (pad in pads) pad.snapshot() + for (channel in channels) channel.suspend() + for (pad in pads) pad.refreshIdentity() + wheel = 0f + horizontalWheel = 0f + moving = false + } + + @Synchronized + fun tick(seconds: Float): Boolean { + if (!enabled) return false + var x = 0f + var y = 0f + var scroll = 0f + var horizontalScroll = 0f + for (channel in channels) { + if (channel.blocked || channel.control.isTrackpad) continue + val cx = if (abs(channel.x) > 0.12f) channel.x else 0f + val cy = if (abs(channel.y) > 0.12f) channel.y else 0f + when (val action = channel.action) { + is InputAction.Pointer -> { x += cx * action.speed; y += cy * action.speed } + is InputAction.Scroll -> if (action.horizontal) horizontalScroll += cx * action.speed else scroll += cy * action.speed + else -> Unit + } + } + val dt = seconds.coerceIn(0f, 0.05f) + if (x != 0f || y != 0f) output.pointer(x * dt, y * dt) + wheel += scroll * dt + horizontalWheel += horizontalScroll * dt + flushWheel() + moving = x != 0f || y != 0f || scroll != 0f || horizontalScroll != 0f + return moving + } + + private fun flushWheel() { + val steps = wheel.toInt() + if (steps != 0) { wheel -= steps; output.scroll(steps) } + val horizontalSteps = horizontalWheel.toInt() + if (horizontalSteps != 0) { horizontalWheel -= horizontalSteps; output.scrollHorizontal(horizontalSteps) } + } + + private fun key(code: Int, down: Boolean) { + val was = keys[code] + keys[code] = (was + if (down) 1 else -1).coerceAtLeast(0) + if ((was == 0) != (keys[code] == 0)) output.key(code, down) + } + + private fun button(code: Int, down: Boolean) { + val at = code - 272 + val was = buttons[at] + buttons[at] = (was + if (down) 1 else -1).coerceAtLeast(0) + if ((was == 0) != (buttons[at] == 0)) output.button(code, down) + } + + inner class Channel internal constructor(val role: InputRole, val control: InputControl, private val fixedAction: InputAction?, private val element: InputSurfaceElement?, private val elementClick: Boolean) { + var action: InputAction = InputAction.Native() + private set + internal var x = 0f + internal var y = 0f + internal var contact = false + internal var blocked = false + private var held = false + private val directions = BooleanArray(4) + private var radialKeys = emptyList() + + internal fun actionFrom(profile: InputProfile) = element?.let { if (elementClick) profile.elementClickAction(role, it) else profile.elementAction(role, it.id, control) } + ?: profile.action(role, control) + + internal fun configure(next: InputAction) { + suspend() + action = (fixedAction ?: next).takeIf { if (elementClick) it.validForClick() else if (element != null) it.validFor(element) else it.validFor(control) } + ?: if (element != null) InputAction.Disabled else InputAction.Native() + } + + internal fun observe(value: Float, second: Float = 0f, touching: Boolean = false) { + x = value; y = second; contact = touching + } + + internal fun awaitNeutral(value: Float, second: Float, touching: Boolean) = synchronized(this@InputRouter) { + observe(value, second, touching) + suspend() + } + + internal fun nativeIdentity(): Boolean = enabled && !blocked && (action as? InputAction.Native)?.target == null && action is InputAction.Native + + internal fun suspend() { + releaseOutputs() + blocked = contact || abs(x) > 0.12f || abs(y) > 0.12f + } + + fun release() = synchronized(this@InputRouter) { + releaseOutputs() + x = 0f; y = 0f; contact = false; blocked = false + } + + fun close() = synchronized(this@InputRouter) { + release() + channels.remove(this) + } + + fun acceptsPointerGesture() = synchronized(this@InputRouter) { enabled && !blocked && contact && action is InputAction.Pointer } + + fun pointerDelta(dx: Float, dy: Float) = synchronized(this@InputRouter) { + val pointer = action as? InputAction.Pointer + if (enabled && !blocked && contact && pointer != null && dx.isFinite() && dy.isFinite()) output.pointer(dx * pointer.speed, dy * pointer.speed) + } + + fun wheelSteps(steps: Int) = synchronized(this@InputRouter) { + val scroll = action as? InputAction.Scroll + if (enabled && !blocked && scroll != null && steps != 0) { + if (scroll.horizontal) output.scrollHorizontal(steps) else output.scroll(steps) + } + } + + fun update(value: Float, second: Float = 0f, touching: Boolean = false): InputControl? = synchronized(this@InputRouter) { + val oldX = x + val oldY = y + val oldContact = contact + x = value.coerceIn(-1f, 1f) + y = second.coerceIn(-1f, 1f) + contact = touching + if (!enabled) { + blocked = contact || abs(x) > 0.12f || abs(y) > 0.12f + return@synchronized null + } + if (blocked) { + if (!contact && abs(x) <= 0.12f && abs(y) <= 0.12f) blocked = false + return@synchronized null + } + when (val a = action) { + is InputAction.Native -> return@synchronized a.target ?: control + is InputAction.Disabled -> Unit + is InputAction.Pointer -> { + if (control.isTrackpad) { + if (contact && oldContact) output.pointer((x - oldX) * a.speed, -(y - oldY) * a.speed) + } else requestMotion() + } + is InputAction.Scroll -> { + if (control.isTrackpad) { + if (contact && oldContact) { + if (a.horizontal) horizontalWheel -= (x - oldX) * a.speed else wheel += (y - oldY) * a.speed + flushWheel() + } + } else requestMotion() + } + is InputAction.Directions -> { + val vertical = if (control.isTrackpad) -y else y + if (a.diagonals == null) { + direction(0, -vertical, a.up); direction(1, x, a.right) + direction(2, vertical, a.down); direction(3, -x, a.left) + } else { + val cardinal = listOf(a.up, a.right, a.down, a.left) + val sector = ((kotlin.math.round(Math.toDegrees(kotlin.math.atan2(vertical, x).toDouble()) / 45).toInt() + 2) % 8 + 8) % 8 + val next = if (kotlin.math.hypot(x, vertical) < .24f) emptyList() else if (sector % 2 == 0) listOf(cardinal[sector / 2]) + else a.diagonals[sector / 2].let { if (it != 0) listOf(it) else listOf(cardinal[sector / 2], cardinal[(sector / 2 + 1) % 4]) } + val codes = next.filter { it != 0 }.distinct() + radialKeys.filterNot { it in codes }.forEach { key(it, false) } + codes.filterNot { it in radialKeys }.forEach { key(it, true) } + radialKeys = codes + } + } + else -> { + val down = if (control.isTrackpad) contact else maxOf(abs(x), abs(y)) > if (held) 0.35f else 0.55f + if (held != down) { + held = down + when (a) { + is InputAction.Keys -> if (down) a.codes.forEach { key(it, true) } else a.codes.asReversed().forEach { key(it, false) } + is InputAction.MouseButton -> button(a.code, down) + is InputAction.Command -> if (!down) output.command(a.command, role) + else -> Unit + } + } + } + } + null + } + + private fun requestMotion() { + if (!moving && (abs(x) > 0.12f || abs(y) > 0.12f)) { moving = true; output.requestMotion() } + } + + private fun direction(at: Int, value: Float, code: Int) { + val down = value > if (directions[at]) 0.35f else 0.55f + if (directions[at] != down) { directions[at] = down; if (code != 0) key(code, down) } + } + + private fun releaseOutputs() { + when (val a = action) { + is InputAction.Keys -> if (held) a.codes.asReversed().forEach { key(it, false) } + is InputAction.MouseButton -> if (held) button(a.code, false) + is InputAction.Directions -> { + val codes = intArrayOf(a.up, a.right, a.down, a.left) + for (i in directions.indices) if (directions[i] && codes[i] != 0) key(codes[i], false) + } + else -> Unit + } + radialKeys.forEach { key(it, false) }; radialKeys = emptyList() + held = false + directions.fill(false) + } + } + + inner class PadSource internal constructor(role: InputRole, retained: PadState?) { + private val routes = Array(InputControl.padControls.size) { channel(role, InputControl.padControls[it]) } + private val result = PadState() + private var last: PadState? = null + private var identity = true + private var nativeCandidate = true + + init { + last = retained + if (retained != null) for (route in routes) { + route.awaitNeutral(retained.value(route.control), retained.secondValue(route.control), false) + } + refreshIdentity() + } + + internal fun refreshIdentity() { + nativeCandidate = routes.all { (it.action as? InputAction.Native)?.target == null && it.action is InputAction.Native } + identity = enabled && nativeCandidate && routes.none { it.blocked } + } + + internal fun snapshot() { + val raw = last ?: return + for (route in routes) { + route.x = raw.value(route.control) + route.y = raw.secondValue(route.control) + } + } + + fun route(raw: PadState): PadState = synchronized(this@InputRouter) { + last = raw + if (identity) return@synchronized raw + result.clear() + for (route in routes) { + val x = raw.value(route.control) + val y = raw.secondValue(route.control) + val target = route.update(x, y) ?: continue + result.add(target, x, y) + } + if (nativeCandidate && enabled && routes.none { it.blocked }) identity = true + result + } + + fun release() = synchronized(this@InputRouter) { + for (route in routes) route.release() + last = null + result.clear() + refreshIdentity() + } + + fun close() = synchronized(this@InputRouter) { + release() + for (route in routes) route.close() + pads.remove(this) + } + } +} + +private val padButtons = intArrayOf(PadState.A, PadState.B, PadState.X, PadState.Y, PadState.LB, PadState.RB, + PadState.SELECT, PadState.START, PadState.L3, PadState.R3, PadState.GUIDE, PadState.QAM) + +internal fun PadState.value(control: InputControl): Float = when (control) { + InputControl.UP -> if (up) 1f else 0f + InputControl.RIGHT -> if (right) 1f else 0f + InputControl.DOWN -> if (down) 1f else 0f + InputControl.LEFT -> if (left) 1f else 0f + InputControl.LT -> leftTrigger + InputControl.RT -> rightTrigger + InputControl.LS -> leftX + InputControl.RS -> rightX + else -> if (control.ordinal < padButtons.size && isDown(padButtons[control.ordinal])) 1f else 0f +} + +internal fun PadState.secondValue(control: InputControl): Float = when (control) { + InputControl.LS -> leftY + InputControl.RS -> rightY + else -> 0f +} + +internal fun PadState.add(control: InputControl, x: Float, y: Float = 0f) { + when (control) { + InputControl.UP -> up = up || x > 0.5f + InputControl.RIGHT -> right = right || x > 0.5f + InputControl.DOWN -> down = down || x > 0.5f + InputControl.LEFT -> left = left || x > 0.5f + InputControl.LT -> leftTrigger = maxOf(leftTrigger, x) + InputControl.RT -> rightTrigger = maxOf(rightTrigger, x) + InputControl.LS -> if (x * x + y * y > leftX * leftX + leftY * leftY) { leftX = x; leftY = y } + InputControl.RS -> if (x * x + y * y > rightX * rightX + rightY * rightY) { rightX = x; rightY = y } + else -> if (control.ordinal < padButtons.size && x > 0.5f) press(padButtons[control.ordinal], true) + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/input/InputStockLayouts.kt b/app/src/main/java/com/droiddeck/launcher/input/InputStockLayouts.kt new file mode 100644 index 0000000000..f3c451d6f7 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/input/InputStockLayouts.kt @@ -0,0 +1,80 @@ +package com.droiddeck.launcher.input + +import androidx.annotation.StringRes +import com.droiddeck.launcher.R + +enum class DeckPanel(val id: String, val layout: String, val bindings: String, @StringRes val label: Int) { + GAMEPAD("gamepad", "builtin.deck.gamepad", "preset.deck.gamepad", R.string.deck_tab_gamepad), + GRIPS("grips", "builtin.deck.grips", "preset.deck.grips", R.string.deck_tab_grips), + LEFT("left", "builtin.deck.left", "preset.deck.left", R.string.deck_tab_left), + RIGHT("right", "builtin.deck.right", "preset.deck.right", R.string.deck_tab_right), + BOTH("both", InputLibraries.DECK, InputStockLayouts.DECK, R.string.deck_tab_both); + + companion object { + fun from(id: String?) = entries.firstOrNull { it.id == id } ?: BOTH + } +} + +internal object InputStockLayouts { + private val NEUTRAL = InputSurfaceStyle(tint = null) + const val DECK = "preset.deck" + const val TRACKPAD = "preset.trackpad" + private fun grips() = listOf(InputControl.L4, InputControl.L5, InputControl.R5, InputControl.R4).mapIndexed { i, c -> + InputSurfaceElement(c, .02f + i * .245f, .78f, .225f, .20f) + } + private fun pads() = listOf(InputSurfaceElement(InputControl.LEFT_PAD, .02f, .02f, .47f, .74f), + InputSurfaceElement(InputControl.RIGHT_PAD, .51f, .02f, .47f, .74f)) + private fun gripGrid() = listOf(InputControl.L4 to (.02f to .02f), InputControl.L5 to (.02f to .51f), + InputControl.R4 to (.51f to .02f), InputControl.R5 to (.51f to .51f)).map { (c, at) -> + InputSurfaceElement(c, at.first, at.second, .47f, .47f) + } + private fun pad(right: Boolean) = listOf( + InputSurfaceElement(if (right) InputControl.RIGHT_PAD else InputControl.LEFT_PAD, .02f, .02f, .96f, .74f), + InputSurfaceElement(if (right) InputControl.RIGHT_CLICK else InputControl.LEFT_CLICK, .02f, .78f, .96f, .20f), + ) + private fun gamepad(): List { + fun round(c: InputControl, x: Float, y: Float, size: Float) = InputSurfaceElement(c, x, y, size, size, buttonShape = InputButtonShape.CIRCLE) + fun pill(c: InputControl, x: Float) = InputSurfaceElement(c, x, .02f, .2f, .12f, buttonShape = InputButtonShape.PILL) + return listOf(pill(InputControl.LT, .02f), pill(InputControl.LB, .24f), pill(InputControl.RB, .56f), pill(InputControl.RT, .78f), + InputSurfaceElement(InputControl.LS, .02f, .17f, .4f, .4f), + InputSurfaceElement(InputControl.RIGHT_PAD, .58f, .59f, .4f, .39f), + round(InputControl.UP, .14f, .6f, .12f), round(InputControl.LEFT, .03f, .73f, .12f), + round(InputControl.RIGHT, .25f, .73f, .12f), round(InputControl.DOWN, .14f, .86f, .12f), + round(InputControl.Y, .72f, .17f, .12f), round(InputControl.X, .61f, .3f, .12f), + round(InputControl.B, .83f, .3f, .12f), round(InputControl.A, .72f, .43f, .12f), + InputSurfaceElement(InputControl.SELECT, .42f, .3f, .07f, .1f, buttonShape = InputButtonShape.PILL), + InputSurfaceElement(InputControl.START, .51f, .3f, .07f, .1f, buttonShape = InputButtonShape.PILL)) + } + fun gamepad(leftPad: Boolean, rightStick: Boolean) = gamepad().map { + when { + leftPad && it.control == InputControl.LS -> InputSurfaceElement(InputControl.LEFT_PAD, it.x, it.y, it.width, it.height) + rightStick && it.control == InputControl.RIGHT_PAD -> InputSurfaceElement(InputControl.RS, it.x, it.y, it.width, it.height) + else -> it + } + } + + fun layouts() = listOf( + SavedInputLayout(InputLibraries.DECK, "Deck grips + trackpads", pads() + grips(), style = NEUTRAL, screen = InputLayoutScreen.SECONDARY), + SavedInputLayout(InputLibraries.TRACKPAD, "Trackpad + keyboard", listOf( + InputSurfaceElement(InputControl.RIGHT_PAD, .02f, .02f, .96f, .96f, mouseGestures = true)), style = NEUTRAL, screen = InputLayoutScreen.SECONDARY), + SavedInputLayout(DeckPanel.GAMEPAD.layout, "Deck gamepad", gamepad(), style = NEUTRAL, screen = InputLayoutScreen.SECONDARY), + SavedInputLayout(DeckPanel.GRIPS.layout, "Deck grips", gripGrid(), style = NEUTRAL, screen = InputLayoutScreen.SECONDARY), + SavedInputLayout(DeckPanel.LEFT.layout, "Deck left trackpad", pad(right = false), style = NEUTRAL, screen = InputLayoutScreen.SECONDARY), + SavedInputLayout(DeckPanel.RIGHT.layout, "Deck right trackpad", pad(right = true), style = NEUTRAL, screen = InputLayoutScreen.SECONDARY), + ) + + fun actions(layout: SavedInputLayout): Map = (if (layout.id == InputLibraries.TRACKPAD) mapOf( + "legacy:right-pad" to InputAction.Pointer(), "legacy:a" to InputAction.MouseButton(272), "legacy:b" to InputAction.MouseButton(273), + ) else emptyMap()).filterKeys { id -> layout.elements.any { it.id == id } } + + fun clicks(layout: SavedInputLayout): Map = if (layout.id == InputLibraries.TRACKPAD) + mapOf("legacy:right-pad" to InputAction.MouseButton(272)) else emptyMap() + + fun presets(layouts: List) = layouts.map { layout -> + val desktop = layout.id == InputLibraries.TRACKPAD + val id = if (desktop) TRACKPAD else DeckPanel.entries.first { it.layout == layout.id }.bindings + SavedInputBindings(id, layout.name, false, layout.id, + controls = InputProfile.defaults(if (desktop) InputProfile.DESKTOP else InputProfile.GAME).bindings[InputRole.SECONDARY].orEmpty(), + elements = actions(layout), clicks = clicks(layout)) + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/input/InputSurfaceLayout.kt b/app/src/main/java/com/droiddeck/launcher/input/InputSurfaceLayout.kt new file mode 100644 index 0000000000..e7e1c58a36 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/input/InputSurfaceLayout.kt @@ -0,0 +1,122 @@ +package com.droiddeck.launcher.input + +import android.content.Context +import org.json.JSONArray +import org.json.JSONObject + +enum class InputElementKind(val control: InputControl) { + BUTTON(InputControl.A), STICK(InputControl.LS), TRACKPAD(InputControl.LEFT_PAD), + DPAD(InputControl.LS), TRIGGER(InputControl.LT); + + companion object { + fun from(control: InputControl) = when { + control.isTrackpad -> TRACKPAD + control.dimensions == 2 -> STICK + control == InputControl.LT || control == InputControl.RT -> TRIGGER + else -> BUTTON + } + } +} + +enum class InputButtonShape { ROUNDED, PILL, CIRCLE, RECTANGLE } + +data class InputSurfaceElement( + val control: InputControl, val x: Float, val y: Float, val width: Float, val height: Float, + val id: String = "legacy:${control.id}", + val kind: InputElementKind = InputElementKind.from(control), + val name: String = control.label, + val floating: Boolean = true, + val hideWhenIdle: Boolean = false, + val mouseGestures: Boolean = false, + val toggle: Boolean = false, + val icon: Int = 0, + val image: String = "", + val opacity: Float = 1f, + val buttonShape: InputButtonShape = InputButtonShape.ROUNDED, + val showLabel: Boolean = true, + val alwaysShown: Boolean = false, +) { + val hasClick get() = kind == InputElementKind.STICK || kind == InputElementKind.TRACKPAD + + fun bounded() = copy(x = x.coerceIn(0f, 0.97f), y = y.coerceIn(0f, 0.97f), + width = width.coerceIn(0.03f, (1f - x.coerceIn(0f, 0.97f)).coerceAtLeast(0.03f)), height = height.coerceIn(0.03f, (1f - y.coerceIn(0f, 0.97f)).coerceAtLeast(0.03f))) +} + +internal val QUICK_BUTTONS = listOf(InputCommand.STEAM, InputCommand.QUICK_ACCESS, InputCommand.KEYBOARD) + +val InputSurfaceElement.quickButton: InputCommand? get() = when { + id == "legacy:${InputControl.GUIDE.id}" -> InputCommand.STEAM + id == "legacy:${InputControl.QAM.id}" -> InputCommand.QUICK_ACCESS + id == "builtin:keyboard" -> InputCommand.KEYBOARD + else -> null +} + +internal fun List.scaled(factor: Float): List = map { e -> + fun anchor(centre: Float) = when { centre < 1f / 3f -> 0f; centre > 2f / 3f -> 1f; else -> .5f } + val w = (e.width * factor).coerceIn(.03f, 1f) + val h = (e.height * factor).coerceIn(.03f, 1f) + val cx = e.x + e.width / 2 + val cy = e.y + e.height / 2 + val ax = anchor(cx) + val ay = anchor(cy) + e.copy(x = (ax + (cx - ax) * factor - w / 2).coerceIn(0f, 1f - w), y = (ay + (cy - ay) * factor - h / 2).coerceIn(0f, 1f - h), + width = w, height = h).bounded() +} + +internal fun List.withPositionsOf(defaults: List): List { + val byId = defaults.associateBy { it.id } + return map { e -> byId[e.id]?.let { d -> e.copy(x = d.x, y = d.y, width = d.width, height = d.height).bounded() } ?: e } +} + +object InputSurfaceLayout { + private fun prefs(context: Context) = context.getSharedPreferences("input-surfaces", Context.MODE_PRIVATE) + + fun resetAll(context: Context) { prefs(context).edit().clear().apply() } + + fun defaults() = listOf( + InputSurfaceElement(InputControl.LEFT_PAD, 0.02f, 0.02f, 0.46f, 0.65f), + InputSurfaceElement(InputControl.RIGHT_PAD, 0.52f, 0.02f, 0.46f, 0.65f), + InputSurfaceElement(InputControl.A, 0.02f, 0.72f, 0.20f, 0.25f), + InputSurfaceElement(InputControl.B, 0.26f, 0.72f, 0.20f, 0.25f), + InputSurfaceElement(InputControl.SELECT, 0.52f, 0.72f, 0.20f, 0.25f), + InputSurfaceElement(InputControl.START, 0.76f, 0.72f, 0.22f, 0.25f), + ) + + fun read(context: Context, profile: String, role: InputRole = InputRole.SECONDARY): List = + InputLibraries.read(context).layout(profile, role)?.elements.orEmpty() + + internal fun hasLegacy(context: Context, profile: String) = prefs(context).contains(profile) + + internal fun readLegacy(context: Context, profile: String): List { + val text = prefs(context).getString(profile, null) ?: return defaults() + return runCatching { + val doc = JSONObject(text) + require(doc.getInt("version") == 1) + val elements = doc.getJSONArray("elements") + (0 until minOf(elements.length(), 28)).mapNotNull { i -> + val item = elements.optJSONObject(i) ?: return@mapNotNull null + val control = InputControl.fromId(item.optString("control")) ?: return@mapNotNull null + val values = listOf("x", "y", "w", "h").map { item.optDouble(it, Double.NaN).toFloat() } + if (values.any { !it.isFinite() }) return@mapNotNull null + InputSurfaceElement(control, values[0], values[1], values[2], values[3]).bounded() + }.distinctBy { it.control } + }.getOrElse { defaults() } + } + + fun save(context: Context, profile: String, layout: List) { + InputLibraries.update(context) { library -> + val saved = library.layout(profile, InputRole.SECONDARY) ?: return@update library + library.withLayout(saved.copy(elements = layout.map { it.bounded() })) + } + } + + internal fun saveLegacy(context: Context, profile: String, layout: List) { + val array = JSONArray() + for (element in layout.distinctBy { it.control }.take(28)) { + val e = element.bounded() + array.put(JSONObject().put("control", e.control.id).put("x", e.x.toDouble()).put("y", e.y.toDouble()) + .put("w", e.width.toDouble()).put("h", e.height.toDouble())) + } + prefs(context).edit().putString(profile, JSONObject().put("version", 1).put("elements", array).toString()).apply() + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/input/InputSurfaceSnap.kt b/app/src/main/java/com/droiddeck/launcher/input/InputSurfaceSnap.kt new file mode 100644 index 0000000000..011a478888 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/input/InputSurfaceSnap.kt @@ -0,0 +1,24 @@ +package com.droiddeck.launcher.input + +import kotlin.math.abs + +internal data class SurfaceSnapResult(val element: InputSurfaceElement, val x: Float? = null, val y: Float? = null) + +internal fun snapSurfaceElement(element: InputSurfaceElement, others: List, thresholdX: Float, thresholdY: Float, + corner: Int = 0): SurfaceSnapResult { + fun axis(start: Float, size: Float, targets: List, threshold: Float, low: Boolean, high: Boolean): Triple { + val anchors = when { low -> listOf(0f); high -> listOf(size); else -> listOf(0f, size / 2f, size) } + val candidate = anchors.flatMap { anchor -> targets.map { target -> Triple(target - start - anchor, target, anchor) } } + .filter { abs(it.first) <= threshold }.minByOrNull { abs(it.first) } ?: return Triple(start, size, null) + val offset = candidate.first + val nextStart = if (high) start else start + offset + val nextSize = when { low -> size - offset; high -> size + offset; else -> size } + if (nextStart < 0f || nextSize < .03f || nextStart + nextSize > 1.00001f) return Triple(start, size, null) + return Triple(nextStart.coerceAtLeast(0f), nextSize, candidate.second) + } + val xTargets = listOf(0f, .5f, 1f) + others.flatMap { listOf(it.x, it.x + it.width / 2f, it.x + it.width) } + val yTargets = listOf(0f, .5f, 1f) + others.flatMap { listOf(it.y, it.y + it.height / 2f, it.y + it.height) } + val x = axis(element.x, element.width, xTargets, thresholdX, corner and 1 != 0, corner and 2 != 0) + val y = axis(element.y, element.height, yTargets, thresholdY, corner and 4 != 0, corner and 8 != 0) + return SurfaceSnapResult(element.copy(x = x.first, y = y.first, width = x.second, height = y.second).bounded(), x.third, y.third) +} diff --git a/app/src/main/java/com/droiddeck/launcher/input/InputSurfaceSource.kt b/app/src/main/java/com/droiddeck/launcher/input/InputSurfaceSource.kt new file mode 100644 index 0000000000..7012731ae0 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/input/InputSurfaceSource.kt @@ -0,0 +1,124 @@ +package com.droiddeck.launcher.input + +class InputSurfaceSource(private val pad: PadBridge, private val role: InputRole, val element: InputSurfaceElement) { + private val router = pad.inputRouter + private val route = router?.channel(role, element.control, element = element) + private val deck = DeckControls.source(role, routed = true) + private val clickDeck = DeckControls.source(role, routed = true) + private val clickOwner = Any() + private val clickRoute = if (element.hasClick) router?.channel(role, InputControl.A, element = element, elementClick = true) else null + private val gestureWheel = if (element.mouseGestures) router?.channel(role, InputControl.RIGHT_PAD, fixedAction = InputAction.Scroll()) else null + private val gestureRight = if (element.mouseGestures) router?.channel(role, InputControl.A, fixedAction = InputAction.MouseButton(273)) else null + private val gestureMiddle = if (element.mouseGestures) router?.channel(role, InputControl.A, fixedAction = InputAction.MouseButton(274)) else null + val mouseGestures get() = element.mouseGestures && route?.action is InputAction.Pointer + val directions get() = route?.action as? InputAction.Directions + + fun gestureContact(touching: Boolean) { if (!closed) route?.update(0f, 0f, touching) } + fun pointerDelta(dx: Float, dy: Float) { + if (closed) return + val timing = InputTiming.begin() + route?.pointerDelta(dx, dy) + InputTiming.end(timing) + } + fun gestureWheel(steps: Int) { if (!closed && route?.acceptsPointerGesture() == true) gestureWheel?.wheelSteps(steps) } + fun gestureButton(code: Int, down: Boolean) { + if (closed || down && route?.acceptsPointerGesture() != true) return + when (code) { + 272 -> click(down) + 273 -> gestureRight?.update(if (down) 1f else 0f) + 274 -> gestureMiddle?.update(if (down) 1f else 0f) + } + } + + private var nativePad = false + private var nativeDeck = false + private var clickPad = false + private var clickDeckActive = false + private var closed = false + private var fingerDown = false + private var latchBlocked = false + var latched = false + private set + var onStateChanged: () -> Unit = {} + private fun resetLatch() { latched = false; latchBlocked = fingerDown; onStateChanged() } + private val snapshot: () -> Unit = { releaseNative(); resetLatch(); if (element.toggle) route?.release() } + + init { router?.beforeProfileChange(snapshot) } + + fun update(value: Float, y: Float = 0f, touching: Boolean = false) { + if (closed) return + val timing = InputTiming.begin() + val x = if (element.toggle && route?.action !is InputAction.Command && route?.action !is InputAction.Disabled) { + val down = value > .5f + if (!down) latchBlocked = false + if (down && !fingerDown && !latchBlocked) latched = !latched + fingerDown = down + if (latched) 1f else 0f + } else value + val target = if (router == null) element.control else route?.update(x, y, touching) + val toPad = target?.isPad == true + val toDeck = target != null && !target.isPad + if (toPad || nativePad) writePad(this, target) { state -> + state.clear() + if (toPad) { + if (element.kind == InputElementKind.DPAD) { + state.up = y < -.5f; state.down = y > .5f; state.left = x < -.5f; state.right = x > .5f + } else state.add(target, x, y) + } + } + if (toDeck || nativeDeck) deck.native(if (toDeck) target else null, x, y, touching) + nativePad = toPad; nativeDeck = toDeck + if (element.toggle) onStateChanged() + InputTiming.end(timing) + } + + fun cancel() { + if (closed) return + route?.takeIf { it.action is InputAction.Command }?.release() + update(0f) + } + + fun click(down: Boolean) { + if (closed) return + val timing = InputTiming.begin() + val target = if (router == null) when (element.control) { InputControl.LS -> InputControl.L3; InputControl.RS -> InputControl.R3; else -> null } else clickRoute?.update(if (down) 1f else 0f) + val toPad = target?.isPad == true + val toDeck = target != null && !target.isPad + if (toPad || clickPad) writePad(clickOwner, target) { state -> + state.clear() + if (toPad && down) state.add(target, 1f) + } + if (toDeck || clickDeckActive) clickDeck.native(if (toDeck) target else null, if (down) 1f else 0f, 0f, false) + clickPad = toPad; clickDeckActive = toDeck + InputTiming.end(timing) + } + + private fun writePad(owner: Any, target: InputControl?, mutation: (PadState) -> Unit) { + if (router != null) pad.applyRoutedTouch(owner, role, mutation) + else pad.applyTouch(owner, role, when (target) { InputControl.LS -> PadBridge.LEFT_STICK; InputControl.RS -> PadBridge.RIGHT_STICK; else -> 0 }, mutation) + } + + private fun releaseNative() { + if (nativePad) pad.releaseTouch(this) + if (nativeDeck) deck.release() + if (clickPad) pad.releaseTouch(clickOwner) + if (clickDeckActive) clickDeck.release() + clickPad = false; clickDeckActive = false + nativePad = false; nativeDeck = false + } + + fun close() { + if (closed) return + closed = true + router?.removeSnapshot(snapshot) + releaseNative(); resetLatch() + route?.close(); clickRoute?.close(); gestureWheel?.close(); gestureRight?.close(); gestureMiddle?.close(); deck.close(); clickDeck.close() + } + + fun release() { + if (closed) return + releaseNative(); resetLatch() + route?.release(); clickRoute?.release(); gestureWheel?.release(); gestureRight?.release(); gestureMiddle?.release() + } + +} diff --git a/app/src/main/java/com/droiddeck/launcher/input/InputTiming.kt b/app/src/main/java/com/droiddeck/launcher/input/InputTiming.kt new file mode 100644 index 0000000000..0d2589c42f --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/input/InputTiming.kt @@ -0,0 +1,33 @@ +package com.droiddeck.launcher.input + +import java.util.Locale + +object InputTiming { + @JvmField @Volatile var enabled = false + private val samples = LongArray(4096) + private var count = 0L + private var next = 0 + + @JvmStatic fun begin(): Long = if (enabled) System.nanoTime() else 0L + + @JvmStatic fun end(start: Long) { + if (start == 0L) return + val elapsed = System.nanoTime() - start + synchronized(this) { + samples[next] = elapsed + next = (next + 1) % samples.size + count++ + } + } + + @Synchronized fun reset() { count = 0; next = 0; samples.fill(0) } + + @Synchronized fun report(): String { + val size = minOf(count, samples.size.toLong()).toInt() + if (size == 0) return "No input samples" + val values = samples.copyOf(size).sortedArray() + fun percentile(p: Double) = values[((size - 1) * p).toInt()] / 1000.0 + return String.format(Locale.ROOT, "Dispatch to publication (µs): p50 %.1f, p95 %.1f, p99 %.1f; %d samples, last %d retained", + percentile(.5), percentile(.95), percentile(.99), count, size) + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/input/LegacyGamepadLayout.kt b/app/src/main/java/com/droiddeck/launcher/input/LegacyGamepadLayout.kt new file mode 100644 index 0000000000..e281a2b741 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/input/LegacyGamepadLayout.kt @@ -0,0 +1,352 @@ +package com.droiddeck.launcher.input + +import android.content.Context +import android.graphics.Rect +import kotlin.math.* + +internal class LegacyGamepadLayout(private val context: Context, private val width: Int, private val height: Int, private val safe: Rect = Rect(), + private val scale: Float = 1f) { + private val resources get() = context.resources + private val settings = ControllerPrefs.read(context) + private var fit = 1f + private class Control(val id: String, val group: String, val stick: Int, val radiusDp: Float, val offsetXDp: Float, val offsetYDp: Float) { + var radius = 0f + var cx = 0f + var cy = 0f + val halfW get() = if (id in shoulderIds) radius * SHOULDER_WIDTH else radius + val halfH get() = if (id in shoulderIds) radius * SHOULDER_HEIGHT else radius + } + private val controls = listOf( + Control("up", "dpad", -1, 30f, 0f, -50f), + Control("right", "dpad", -1, 30f, 50f, 0f), + Control("down", "dpad", -1, 30f, 0f, 50f), + Control("left", "dpad", -1, 30f, -50f, 0f), + Control("ls", "ls", 0, 48f, 0f, 0f), + Control("rs", "rs", 1, 48f, 0f, 0f), + Control("a", "face", -1, 30f, 0f, 50f), + Control("b", "face", -1, 30f, 50f, 0f), + Control("x", "face", -1, 30f, -50f, 0f), + Control("y", "face", -1, 30f, 0f, -50f), + Control("lb", "lb", -1, 24f, 0f, 0f), + Control("rb", "rb", -1, 24f, 0f, 0f), + Control("lt", "lt", -1, 24f, 0f, 0f), + Control("rt", "rt", -1, 24f, 0f, 0f), + Control("select", "select", -1, 20f, 0f, 0f), + Control("start", "start", -1, 20f, 0f, 0f), + Control("guide", "guide", -1, 22f, 0f, 0f), + Control("qam", "qam", -1, 22f, 0f, 0f), + ) + + private val groups = controls.map { it.group }.distinct() + + private fun dp(value: Float) = value * resources.displayMetrics.density + + private fun scaled(value: Float) = dp(value) * settings.size / 100f * scale * fit + + private fun fitScale(): Float { + val metrics = resources.displayMetrics + val heightMm = (height - safe.top - safe.bottom) / pxPerMm(metrics.ydpi) + val faceMm = dp(60f) / pxPerMm(metrics.xdpi) + val floor = (MIN_FACE_MM / faceMm).coerceAtMost(1f) + return (heightMm / FULL_SIZE_HEIGHT_MM).coerceIn(floor, 1f) + } + + private fun pxPerMm(dpi: Float): Float { + val nominal = resources.displayMetrics.densityDpi.toFloat() + return (if (dpi > nominal * 0.6f && dpi < nominal * 1.6f) dpi else nominal) / 25.4f + } + + private fun placeAuto(w: Float, h: Float) { + val metrics = resources.displayMetrics + val mmX = pxPerMm(metrics.xdpi) + val mmY = pxPerMm(metrics.ydpi) + val usableW = w - safe.left - safe.right + val usableH = h - safe.top - safe.bottom + val inboard = (0.17f * usableW / mmX).coerceIn(24f, 40f) * mmX + val lift = (0.285f * usableH / mmY).coerceIn(22f, 45f) * mmY + val gap = dp(12f) + val floor = BOTTOM_MARGIN_MM * mmY + val drop = max(drop("dpad", "ls", gap), drop("rs", "face", gap)) + val restY = min(h - safe.bottom - lift, h - safe.bottom - floor - drop) + put("ls", safe.left + inboard, restY) + put("face", w - safe.right - inboard, restY) + besideBelow("dpad", "ls", 1f, gap, floor) + besideBelow("rs", "face", -1f, gap, floor) + val phone = usableH / mmY < PHONE_MAX_HEIGHT_MM + val shoulderY = if (phone) safe.top + floor + halfH("lb") + else min(centre("ls").second - extent("ls"), centre("face").second - extent("face")) - gap - halfH("lb") + shoulders("ls", "lb", "lt", 1f, gap, shoulderY) + shoulders("face", "rb", "rt", -1f, gap, shoulderY) + val bottom = h - safe.bottom - floor - extent("guide") + val spread = extent("guide") + gap / 2f + put("guide", w / 2f - spread, bottom) + put("qam", w / 2f + spread, bottom) + val inner = max(centre("dpad").first + extent("dpad"), w - centre("rs").first + extent("rs")) + gap + extent("select") + put("select", inner, restY) + put("start", w - inner, restY) + if (phone) { + val top = max(centre("lb").first + extent("lb"), w - centre("rb").first + extent("rb")) + gap * 2.5f + extent("select") + put("select", top, shoulderY) + put("start", w - top, shoulderY) + } + if (crowded("select", gap) || crowded("start", gap)) { + above("select", "dpad", gap) + val (sx, sy) = centre("select") + put("start", w - sx, sy) + if (crowded("start", gap)) above("start", "rs", gap) + } + if (crowded("guide", gap, gap, "qam") || crowded("qam", gap, gap, "guide")) { + val row = centre("select").second + put("guide", w / 2f - spread, row) + put("qam", w / 2f + spread, row) + if (crowded("guide", gap, gap, "qam") || crowded("qam", gap, gap, "guide")) { + val top = safe.top + dp(8f) + extent("guide") + put("guide", w / 2f - spread, top) + put("qam", w / 2f + spread, top) + } + } + } + + private fun drop(group: String, anchor: String, gap: Float): Float = + (extent(anchor) + extent(group) + gap) * sin(Math.toRadians(DROP_DEGREES)).toFloat() + extent(group) + + private fun besideBelow(group: String, anchor: String, inward: Float, gap: Float, margin: Float) { + val (ax, ay) = centre(anchor) + val reach = extent(anchor) + extent(group) + gap + val floor = height - safe.bottom - margin - extent(group) + val dy = min(reach * sin(Math.toRadians(DROP_DEGREES)).toFloat(), floor - ay) + val dx = sqrt(max(0f, reach * reach - dy * dy)) + put(group, ax + inward * dx, ay + dy) + } + + private fun shoulders(anchor: String, bumper: String, trigger: String, inward: Float, gap: Float, y: Float) { + val (ax, _) = centre(anchor) + val spread = extent(bumper) + gap / 2f + put(bumper, ax + inward * spread, y) + put(trigger, ax - inward * spread, y) + } + + private fun above(group: String, anchor: String, gap: Float) { + val (ax, ay) = centre(anchor) + put(group, ax, ay - extent(anchor) - gap - extent(group)) + } + + private fun crowded(group: String, gap: Float, space: Float = gap / 2f, except: String? = null): Boolean { + val (x, y) = centre(group) + return groups.any { other -> + if (other == group || other == except) return@any false + val (ox, oy) = centre(other) + val dx = x - ox + val dy = y - oy + sqrt(dx * dx + dy * dy) < extent(group) + extent(other) + space + } + } + + private fun members(group: String) = controls.filter { it.group == group } + + private fun centre(group: String): Pair { + val first = members(group).first() + return (first.cx - scaled(first.offsetXDp)) to (first.cy - scaled(first.offsetYDp)) + } + + private fun extent(group: String): Float = members(group).maxOf { control -> + sqrt(scaled(control.offsetXDp).let { it * it } + scaled(control.offsetYDp).let { it * it }) + control.halfW + } + + private fun halfH(group: String): Float = members(group).maxOf { abs(scaled(it.offsetYDp)) + it.halfH } + + private fun put(group: String, x: Float, y: Float) { + for (control in members(group)) { + control.cx = x + scaled(control.offsetXDp) + control.cy = y + scaled(control.offsetYDp) + } + } + + private fun clamp(group: String) { + val list = members(group) + val margin = dp(4f) + val minX = list.minOf { it.cx - it.halfW } - safe.left - margin + val maxX = list.maxOf { it.cx + it.halfW } - (width - safe.right - margin) + val minY = list.minOf { it.cy - it.halfH } - safe.top - margin + val maxY = list.maxOf { it.cy + it.halfH } - (height - safe.bottom - margin) + val dx = if (minX < 0) -minX else if (maxX > 0) -maxX else 0f + val dy = if (minY < 0) -minY else if (maxY > 0) -maxY else 0f + if (dx == 0f && dy == 0f) return + for (control in list) { control.cx += dx; control.cy += dy } + } + + + fun elements(): List { + fit = fitScale() + controls.forEach { it.radius = scaled(it.radiusDp) } + placeAuto(width.toFloat(), height.toFloat()) + for ((group, position) in ControllerPrefs.layout(context, width, height)) { + if (group in groups && position.first.isFinite() && position.second.isFinite()) put(group, position.first * width, position.second * height) + } + groups.forEach(::clamp) + val result = controls.filter { + settings.mapping[it.id] != ControllerPrefs.OFF && (it.id != "guide" || settings.steamButton) && (it.id != "qam" || settings.qamButton) + }.map { c -> + InputSurfaceElement(requireNotNull(InputControl.fromId(c.id)), (c.cx - c.halfW) / width, (c.cy - c.halfH) / height, + 2 * c.halfW / width, 2 * c.halfH / height, floating = settings.adaptiveSticks, hideWhenIdle = c.stick >= 0 && settings.adaptiveSticks, + buttonShape = if (c.id in shoulderIds) InputButtonShape.PILL else InputButtonShape.CIRCLE).let(::widenStick).bounded() + }.toMutableList() + if (settings.keyboardButton) result += keyboard() + return result + } + + fun regenerate(stored: List): List = withGeometry(stored, elements()) + + fun rescale(stored: List, from: LegacyGamepadLayout): List { + val before = from.elements().associateBy { it.id } + val after = elements().associateBy { it.id } + return stored.map { e -> + val b = before[e.id] ?: return@map e + val a = after[e.id] ?: return@map e + val w = (a.width * e.width / b.width).coerceIn(.03f, 1f) + val h = (a.height * e.height / b.height).coerceIn(.03f, 1f) + val cx = a.x + a.width / 2 + (e.x + e.width / 2 - b.x - b.width / 2) + val cy = a.y + a.height / 2 + (e.y + e.height / 2 - b.y - b.height / 2) + e.copy(x = (cx - w / 2).coerceIn(0f, 1f - w), y = (cy - h / 2).coerceIn(0f, 1f - h), width = w, height = h).bounded() + } + } + + fun buttonsOnly(stored: List): List { + val inset = dp(BUTTONS_ONLY_INSET_DP) + val bottom = height - safe.bottom - inset + val radius = dp(BUTTONS_ONLY_RADIUS_DP) + fun pick(control: InputControl, shown: Boolean) = stored.firstOrNull { it.id == "legacy:${control.id}" } + ?: stored.firstOrNull { it.control == control && it.kind == InputElementKind.BUTTON } + ?: InputSurfaceElement(control, 0f, 0f, 0f, 0f, buttonShape = InputButtonShape.CIRCLE).takeIf { shown } + return listOfNotNull( + pick(InputControl.GUIDE, settings.steamButton)?.let { circle(it, safe.left + inset, bottom, radius) }, + pick(InputControl.QAM, settings.qamButton)?.let { circle(it, width - safe.right - inset, bottom, radius) }, + stored.firstOrNull { it.id == KEYBOARD_ID }?.let { circle(it, width / 2f, bottom, dp(KEYBOARD_RADIUS_DP)) }, + ) + } + + fun dock(stored: List, portrait: Boolean, grips: Boolean, left: InputControl = InputControl.LS, + right: InputControl = InputControl.RS): List { + val boxes = GamepadGeometry.layout(width.toFloat(), height.toFloat(), resources.displayMetrics.density, portrait) + val placed = mutableListOf() + fun box(control: InputControl, slot: GamepadGeometry.Slot, shape: InputButtonShape) { + val b = boxes[slot] ?: return + placed += InputSurfaceElement(control, b.left / width, b.top / height, b.width / width, b.height / height, + floating = false, buttonShape = shape).bounded() + } + fun cross(controls: List, slot: GamepadGeometry.Slot) { + val b = boxes[slot] ?: return + val side = min(b.width, b.height) + val spread = side * CROSS_SPREAD + val radius = side * CROSS_RADIUS + controls.zip(listOf(0f to -1f, 1f to 0f, 0f to 1f, -1f to 0f)).forEach { (control, direction) -> + placed += circle(InputSurfaceElement(control, 0f, 0f, 0f, 0f, buttonShape = InputButtonShape.CIRCLE), + b.cx + direction.first * spread, b.cy + direction.second * spread, radius) + } + } + box(left, GamepadGeometry.Slot.LEFT_MAIN, InputButtonShape.CIRCLE) + cross(DPAD_ORDER, GamepadGeometry.Slot.LEFT_SECOND) + box(right, GamepadGeometry.Slot.RIGHT_MAIN, InputButtonShape.CIRCLE) + cross(FACE_ORDER, GamepadGeometry.Slot.RIGHT_SECOND) + box(InputControl.LT, GamepadGeometry.Slot.LEFT_OUTER_SHOULDER, InputButtonShape.PILL) + box(InputControl.LB, GamepadGeometry.Slot.LEFT_INNER_SHOULDER, InputButtonShape.PILL) + box(InputControl.RB, GamepadGeometry.Slot.RIGHT_INNER_SHOULDER, InputButtonShape.PILL) + box(InputControl.RT, GamepadGeometry.Slot.RIGHT_OUTER_SHOULDER, InputButtonShape.PILL) + box(InputControl.SELECT, GamepadGeometry.Slot.VIEW, InputButtonShape.PILL) + box(InputControl.START, GamepadGeometry.Slot.MENU, InputButtonShape.PILL) + if (grips) GRIPS.forEach { (control, slot) -> box(control, slot, InputButtonShape.ROUNDED) } + val surfaces = listOf(left, right).filter { it.isTrackpad } + return withGeometry(stored, placed) + placed.filter { p -> (GRIPS.any { it.first == p.control } || p.control in surfaces) && stored.none { it.id == p.id } } + } + + private fun circle(e: InputSurfaceElement, cx: Float, cy: Float, radius: Float) = + e.copy(x = (cx - radius) / width, y = (cy - radius) / height, width = 2 * radius / width, height = 2 * radius / height).bounded() + + private fun withGeometry(stored: List, placed: List): List { + val byId = placed.associateBy { it.id } + return stored.mapNotNull { e -> byId[e.id]?.let { e.copy(x = it.x, y = it.y, width = it.width, height = it.height) } } + } + + private fun keyboard(): InputSurfaceElement { + val radius = dp(KEYBOARD_RADIUS_DP) + val cx = safe.left + dp(KEYBOARD_X_DP) + val cy = height - safe.bottom - dp(KEYBOARD_Y_DP) + return InputSurfaceElement(InputControl.A, (cx - radius) / width, (cy - radius) / height, 2 * radius / width, 2 * radius / height, + KEYBOARD_ID, name = "Keyboard", icon = InputButtonIcons.KEYBOARD, buttonShape = InputButtonShape.CIRCLE).bounded() + } + + fun restyle(elements: List): List { + val dpad = elements.filter { it.id == "legacy:${it.control.id}" && it.control in DPAD && it.buttonShape == InputButtonShape.ROUNDED } + val centreX = dpad.map { (it.x + it.width / 2f) * width }.average().toFloat() + val centreY = dpad.map { (it.y + it.height / 2f) * height }.average().toFloat() + val resize = untouchedDpad(dpad, centreX, centreY) + return elements.map { e -> + when { + e.id == KEYBOARD_ID && e.icon == 0 && e.name == "Keyboard" && e.buttonShape == InputButtonShape.ROUNDED && + e.x == .02f && e.y == .9f && e.width == .12f && e.height == .08f -> keyboard() + e.id == "legacy:${e.control.id}" && e.buttonShape == InputButtonShape.ROUNDED -> { + val round = e.copy(buttonShape = if (e.control.id in shoulderIds) InputButtonShape.PILL else InputButtonShape.CIRCLE) + (if (resize && e.control in DPAD) resizeDpad(round, centreX, centreY) else widenStick(round)).bounded() + } + else -> e + } + } + } + + private fun untouchedDpad(dpad: List, centreX: Float, centreY: Float): Boolean { + if (dpad.size != DPAD.size) return false + val radius = dpad[0].width * width / 2f + val tolerance = 1.5f + val offset = radius * LEGACY_DPAD_OFFSET_DP / LEGACY_DPAD_RADIUS_DP + return dpad.all { e -> + val old = controls.first { it.id == e.control.id } + val dx = (e.x + e.width / 2f) * width - centreX + val dy = (e.y + e.height / 2f) * height - centreY + abs(e.width * width / 2f - radius) <= tolerance && abs(e.height * height / 2f - radius) <= tolerance && + abs(dx - offset * sign(old.offsetXDp)) <= tolerance && abs(dy - offset * sign(old.offsetYDp)) <= tolerance + } + } + + private fun resizeDpad(e: InputSurfaceElement, centreX: Float, centreY: Float): InputSurfaceElement { + val old = controls.first { it.id == e.control.id } + val radius = e.width * width / 2f * old.radiusDp / LEGACY_DPAD_RADIUS_DP + val cx = centreX + ((e.x + e.width / 2f) * width - centreX) * abs(old.offsetXDp + old.offsetYDp) / LEGACY_DPAD_OFFSET_DP + val cy = centreY + ((e.y + e.height / 2f) * height - centreY) * abs(old.offsetXDp + old.offsetYDp) / LEGACY_DPAD_OFFSET_DP + return e.copy(x = (cx - radius) / width, y = (cy - radius) / height, width = 2 * radius / width, height = 2 * radius / height) + } + + private fun widenStick(e: InputSurfaceElement): InputSurfaceElement { + val extra = e.width * (STICK_REACH - 1f) + return when (e.control) { + InputControl.LS -> (e.x - extra).coerceAtLeast(0f).let { x -> e.copy(x = x, width = e.x + e.width - x) } + InputControl.RS -> e.copy(width = (e.width + extra).coerceAtMost(1f - e.x)) + else -> e + } + } + private companion object { + const val MIN_FACE_MM = 9.5f + const val FULL_SIZE_HEIGHT_MM = 85f + const val BOTTOM_MARGIN_MM = 5f + const val DROP_DEGREES = 35.0 + const val PHONE_MAX_HEIGHT_MM = 90f + const val SHOULDER_WIDTH = 1.45f + const val SHOULDER_HEIGHT = 0.8f + val shoulderIds = setOf("lb", "rb", "lt", "rt") + const val STICK_REACH = 1.25f + const val LEGACY_DPAD_RADIUS_DP = 26f + const val LEGACY_DPAD_OFFSET_DP = 52f + val DPAD = setOf(InputControl.UP, InputControl.RIGHT, InputControl.DOWN, InputControl.LEFT) + const val KEYBOARD_ID = "builtin:keyboard" + const val KEYBOARD_RADIUS_DP = 18f + const val KEYBOARD_X_DP = 90f + const val KEYBOARD_Y_DP = 34f + const val BUTTONS_ONLY_RADIUS_DP = 30f + const val BUTTONS_ONLY_INSET_DP = 44f + const val CROSS_SPREAD = .30f + const val CROSS_RADIUS = .16f + val DPAD_ORDER = listOf(InputControl.UP, InputControl.RIGHT, InputControl.DOWN, InputControl.LEFT) + val FACE_ORDER = listOf(InputControl.Y, InputControl.B, InputControl.A, InputControl.X) + val GRIPS = listOf(InputControl.L4 to GamepadGeometry.Slot.L4, InputControl.L5 to GamepadGeometry.Slot.L5, + InputControl.R5 to GamepadGeometry.Slot.R5, InputControl.R4 to GamepadGeometry.Slot.R4) + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/input/LegacyScreenKeyMigration.kt b/app/src/main/java/com/droiddeck/launcher/input/LegacyScreenKeyMigration.kt new file mode 100644 index 0000000000..9921fdf960 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/input/LegacyScreenKeyMigration.kt @@ -0,0 +1,130 @@ +package com.droiddeck.launcher.input + +import android.content.Context +import android.hardware.display.DisplayManager +import android.util.DisplayMetrics +import android.view.Display +import org.json.JSONObject + +internal object LegacyScreenKeyMigration { + const val BACKUP = "legacy-screen-keys-backup" + const val MERGED = "legacy-screen-keys-merged" + private const val SHOWN = "migrated.screenkeys.0" + + fun snapshot(context: Context): String? { + val values = ControllerPrefs.prefs(context).all.filterKeys { it == "screenKeys" || it == "screenKeysShown" || it.startsWith("screenLayout.") } + return values.takeIf { it.isNotEmpty() }?.let { JSONObject(it).toString() } + } + + fun clean(context: Context) { + val prefs = ControllerPrefs.prefs(context) + prefs.edit().apply { prefs.all.keys.filter { it == "screenKeys" || it == "screenKeysShown" || it.startsWith("screenLayout.") }.forEach(::remove) }.apply() + } + + @Suppress("DEPRECATION") + fun migrate(context: Context, library: InputLibrary, raw: String): InputLibrary { + val old = JSONObject(raw) + val display = context.getSystemService(DisplayManager::class.java)?.getDisplay(Display.DEFAULT_DISPLAY) + val metrics = DisplayMetrics().also { if (display != null) display.getRealMetrics(it) else it.setTo(context.resources.displayMetrics) } + val settings = ControllerPrefs.read(context) + val style = InputSurfaceStyle(settings.tint, settings.opacity) + val sets = buildList { + old.optString("screenKeys").takeIf { it.isNotBlank() }?.let { add("Screen keys (imported)" to it) } + old.keys().asSequence().filter { it.startsWith("screenLayout.") }.sorted().forEach { key -> + add((key.removePrefix("screenLayout.").ifBlank { "Screen keys" } + " (imported)").take(80) to old.getString(key)) + } + } + var result = library + for ((index, set) in sets.withIndex()) { + val id = "migrated.screenkeys.$index" + if (result.profiles.any { it.id == id }) continue + val converted = convert(context, set.second, metrics.widthPixels, metrics.heightPixels, metrics.density) + if (converted.isEmpty()) continue + val layout = SavedInputLayout("$id.layout", set.first, converted.map { it.first.copy(alwaysShown = true) }, style = style, screen = InputLayoutScreen.MAIN, + aspect = (metrics.widthPixels.coerceAtLeast(1).toFloat() / metrics.heightPixels.coerceAtLeast(1)).coerceIn(.05f, 20f)) + val bindings = SavedInputBindings("$id.bindings", set.first, false, layout.id, elements = converted.associate { it.first.id to it.second }) + val base = library.profile(library.steam) + val profile = SavedInputProfile(id, set.first, base.base, base.parts + (InputRole.PRIMARY to InputPart(bindings.id, layout.id))) + result = result.withLayout(layout).withPreset(bindings).withProfile(profile) + } + return result.validate() + } + + fun merge(library: InputLibrary, raw: String): InputLibrary { + val old = JSONObject(raw) + if (!old.optBoolean("screenKeysShown", true) || old.optString("screenKeys").isBlank()) return library + val keys = library.layouts.firstOrNull { it.id == "$SHOWN.layout" } ?: return library + val actions = library.bindings.firstOrNull { it.id == "$SHOWN.bindings" }?.elements.orEmpty() + var result = library + for (part in listOf(false, true).map { library.activePart(it, InputRole.PRIMARY) }.distinct()) { + val layout = result.layouts.firstOrNull { it.id == part.layout } ?: continue + val preset = result.preset(part.bindings) + if (!layout.editable || preset.layoutId != layout.id || layout.elements.any { e -> keys.elements.any { it.id == e.id } } || + layout.elements.size + keys.elements.size > 64) continue + val added = keys.elements.mapNotNull { e -> actions[e.id]?.let { e.id to it } }.toMap() + result = result.withLayout(layout.copy(elements = layout.elements + keys.elements)) + .withPreset(preset.copy(elements = added + preset.elements)) + } + return result.validate() + } + + fun pin(library: InputLibrary): InputLibrary { + val keys = library.layouts.firstOrNull { it.id == "$SHOWN.layout" }?.elements.orEmpty() + return library.copy(layouts = library.layouts.map { layout -> + val imported = layout.id.startsWith("migrated.screenkeys.") + layout.copy(elements = layout.elements.map { e -> + val key = e.id.startsWith("imported:") && (imported || keys.any { it.id == e.id && it.kind == e.kind && it.name == e.name }) + if (key) e.copy(alwaysShown = true) else e + }) + }) + } + + internal fun convert(context: Context, raw: String, width: Int, height: Int, density: Float): List> = + convert(context, parse(raw), width, height, density) + + internal fun parse(raw: String): List = raw.split(';').filter { it.isNotBlank() }.map { entry -> + val parts = entry.split(':') + require(parts.size >= 2) { "Invalid legacy screen key" } + val codes = parts[0].split('+').map { it.toInt() } + val position = parts[1].split(',').map { it.toFloat() } + require(position.size == 3) + fun text(at: Int) = parts.getOrNull(at).orEmpty().replace("%2C", ",").replace("%3A", ":").replace("%3B", ";").replace("%25", "%") + LegacyScreenKey(codes, position[0], position[1], position[2], text(2), text(3), text(4), + parts.getOrNull(5)?.toFloatOrNull() ?: 1f, parts.getOrNull(6)?.toIntOrNull() ?: 0, parts.getOrNull(7) == "1", parts.getOrNull(8) != "0") + } + + internal fun convert(context: Context, keys: List, width: Int, height: Int, density: Float): List> = + keys.mapIndexed { index, key -> + val codes = key.codes + require(codes.all { it in 0..255 }) + require(listOf(key.x, key.y, key.size).all { it.isFinite() }) + val shape = key.shape + val kind = when (shape) { 5 -> InputElementKind.DPAD; 6 -> InputElementKind.STICK; 4 -> InputElementKind.TRIGGER; else -> InputElementKind.BUTTON } + val label = key.text.ifEmpty { key.glyph }.ifEmpty { codes.filter { it != 0 }.joinToString("+") { pcKeyLabel(context, it) } }.ifBlank { "Button" }.take(80) + val image = key.image + val icon = codes.singleOrNull()?.takeIf { InputButtonIcons.has(it) && key.text.isEmpty() && key.glyph.isEmpty() } ?: 0 + val heightPx = key.size.coerceIn(24f, 140f) * density + val ratio = when { shape == 2 -> 1f; shape == 4 -> 1.7f; shape == 5 || shape == 6 || image.isNotEmpty() || icon != 0 -> 1.15f; else -> (.55f * label.length + 1.05f).coerceIn(1.7f, 4.6f) } + val w = (heightPx * ratio / width.coerceAtLeast(1)).coerceIn(.03f, 1f) + val h = (heightPx / height.coerceAtLeast(1)).coerceIn(.03f, 1f) + val element = InputSurfaceElement(kind.control, (key.x - w / 2).coerceIn(0f, 1f - w), (key.y - h / 2).coerceIn(0f, 1f - h), w, h, + id = "imported:$index", kind = kind, name = label, floating = false, + toggle = key.toggle && kind != InputElementKind.DPAD && kind != InputElementKind.STICK, + icon = icon, image = image, opacity = key.opacity.coerceIn(.2f, 1f), + buttonShape = when (shape) { 0 -> InputButtonShape.PILL; 2 -> InputButtonShape.CIRCLE; 3 -> InputButtonShape.RECTANGLE; else -> InputButtonShape.ROUNDED }, + showLabel = key.labels).bounded() + val action = when (kind) { + InputElementKind.DPAD -> InputAction.Directions(codes.getOrElse(0) { 0 }, codes.getOrElse(1) { 0 }, codes.getOrElse(2) { 0 }, codes.getOrElse(3) { 0 }) + InputElementKind.STICK -> InputAction.Directions(codes.getOrElse(0) { 0 }, codes.getOrElse(2) { 0 }, codes.getOrElse(4) { 0 }, codes.getOrElse(6) { 0 }, listOf(1, 3, 5, 7).map { codes.getOrElse(it) { 0 } }) + else -> codes.filter { it != 0 }.distinct().let { if (it.isEmpty()) InputAction.Disabled else InputAction.Keys(it) } + } + require(action.validFor(element)) + element to action + } +} + +internal data class LegacyScreenKey( + val codes: List, val x: Float, val y: Float, val size: Float, + val glyph: String = "", val text: String = "", val image: String = "", + val opacity: Float = 1f, val shape: Int = 0, val toggle: Boolean = false, val labels: Boolean = true, +) diff --git a/app/src/main/java/com/droiddeck/launcher/input/MouseBackInput.kt b/app/src/main/java/com/droiddeck/launcher/input/MouseBackInput.kt index 261c88a74b..9f8240640f 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/MouseBackInput.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/MouseBackInput.kt @@ -1,5 +1,6 @@ package com.droiddeck.launcher.input +import android.os.Build import android.view.InputDevice import android.view.KeyCharacterMap import android.view.KeyEvent @@ -7,7 +8,7 @@ import android.view.KeyEvent /** * Some firmware turns a mouse's right button into Back. Most report it from the mouse itself; REDMAGIC's * cast mode injects it from no device and without the virtual-hard-key flag that the navigation bar and - * back gesture set, so that form only counts while a mouse is connected. + * back gesture set, so that form only counts while an external mouse is connected. */ class MouseBackInput(private val sendRight: (Boolean) -> Unit, private val mouseAttached: () -> Boolean) { private val held = mutableSetOf() @@ -45,6 +46,21 @@ class MouseBackInput(private val sendRight: (Boolean) -> Unit, private val mouse } companion object { + fun isExternalMouse(device: InputDevice?): Boolean { + if (device == null || device.isVirtual || PadBridge.isSystemVirtualInputName(device.name) || + declaresVirtual(device.name) || + PadBridge.isFromController(device) || + (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && !device.isExternal)) return false + return (device.supportsSource(InputDevice.SOURCE_MOUSE) || + device.supportsSource(InputDevice.SOURCE_MOUSE_RELATIVE)) && + !device.supportsSource(InputDevice.SOURCE_TOUCHPAD) && + !device.supportsSource(InputDevice.SOURCE_TOUCHSCREEN) + } + + private val VIRTUAL_NAME = Regex("\\bvirtual\\b", RegexOption.IGNORE_CASE) + + fun declaresVirtual(name: String?): Boolean = name != null && VIRTUAL_NAME.containsMatchIn(name) + fun isFromMouse(event: KeyEvent): Boolean = event.isFromSource(InputDevice.SOURCE_MOUSE) || event.isFromSource(InputDevice.SOURCE_MOUSE_RELATIVE) diff --git a/app/src/main/java/com/droiddeck/launcher/input/OnScreenControls.kt b/app/src/main/java/com/droiddeck/launcher/input/OnScreenControls.kt index dc26a75f55..4923221fa0 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/OnScreenControls.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/OnScreenControls.kt @@ -3,831 +3,181 @@ package com.droiddeck.launcher.input import android.annotation.SuppressLint import android.content.Context import android.graphics.Canvas -import android.graphics.Color import android.graphics.Paint -import android.graphics.Path import android.graphics.Rect -import android.graphics.RectF +import android.graphics.Typeface import android.os.Build +import android.view.Gravity import android.view.HapticFeedbackConstants import android.view.MotionEvent import android.view.View import android.view.WindowInsets -import com.droiddeck.launcher.session.SessionPrefs -import kotlin.math.abs -import kotlin.math.hypot -import kotlin.math.max -import kotlin.math.min -import kotlin.math.sin -import kotlin.math.sqrt +import android.widget.FrameLayout +import com.droiddeck.launcher.ui.InputSurfaceView -@SuppressLint("ViewConstructor") -class OnScreenControls( - context: Context, - private val pad: PadBridge?, - private val editing: Boolean = false, - private val onKeyboard: (() -> Unit)? = null, -) : View(context) { +class OnScreenControls(context: Context, pad: PadBridge?, val dock: OnScreenLayout.Dock? = null) : FrameLayout(context) { + private val surface = InputSurfaceView(context, pad, inputRole = InputRole.PRIMARY, haptics = dock != null) + private var layout: SavedInputLayout? = null + private val safe = Rect() + private var viewWidth = 0 + private var viewHeight = 0 + private val placements = object : LinkedHashMap>(4, .75f, true) { + override fun removeEldestEntry(eldest: MutableMap.MutableEntry>) = size > PLACEMENT_CACHE + } + private val quickToggle = QuickToggle(context) + var isButtonsOnly = false + private set + var isQuickHidden = false + private set + var isPinnedOnly = false + private set + val hasPinned get() = layout?.elements?.any { it.alwaysShown } == true + var onTouchMiss: ((MotionEvent) -> Unit)? + get() = surface.onTouchMiss + set(value) { surface.onTouchMiss = value } - private class Control( - val id: String, - val group: String, - val stick: Int, - val radiusDp: Float, - val offsetXDp: Float, - val offsetYDp: Float, - ) { - var target = id - var radius = 0f - var cx = 0f - var cy = 0f - var pressedBy = -1 - var kx = 0f - var ky = 0f - var ax = 0f - var ay = 0f - var dirty = false - var clicked = false - var lastUp = 0L - var downAt = 0L - var downX = 0f - var downY = 0f - var travel = 0f - val wide = id in shoulderIds - val halfW get() = if (wide) radius * SHOULDER_WIDTH else radius - val halfH get() = if (wide) radius * SHOULDER_HEIGHT else radius + init { + addView(surface, LayoutParams(-1, -1)) + val size = (QUICK_HIT_DP * 2 * resources.displayMetrics.density).toInt() + addView(quickToggle, LayoutParams(size, size, Gravity.START or Gravity.BOTTOM)) + } - fun contains(x: Float, y: Float, r: Float, padding: Float = 0f): Boolean { - val dx = x - cx - val dy = y - cy - if (wide) return abs(dx) <= halfW + r * 0.25f + padding && abs(dy) <= halfH + r * 0.25f + padding - val hit = r * 1.25f + padding - return dx * dx + dy * dy <= hit * hit + fun updateLayout(value: SavedInputLayout?) { + if (layout == value) return + releaseAll() + layout = value + render() + } + + fun setButtonsOnly(enabled: Boolean) { + if (isButtonsOnly == enabled) return + releaseAll() + isButtonsOnly = enabled + isQuickHidden = false + render() + } + + fun setQuickHidden(hidden: Boolean) { + if (isButtonsOnly || isQuickHidden == hidden) return + releaseAll() + isQuickHidden = hidden + render() + } + + fun setPinnedOnly(enabled: Boolean) { + if (isPinnedOnly == enabled) return + releaseAll() + isPinnedOnly = enabled + render() + } + + private data class Placement(val layout: SavedInputLayout?, val width: Int, val height: Int, val safe: Rect, val buttonsOnly: Boolean) + + private fun placed(): List { + val key = Placement(layout, viewWidth, viewHeight, Rect(safe), isButtonsOnly) + return placements.getOrPut(key) { + OnScreenLayout.elements(context, layout, viewWidth, viewHeight, safe, isButtonsOnly, dock) + } + } + + private fun render() { + val elements = placed() + surface.updateCustomLayout(when { + isPinnedOnly -> elements.filter { it.alwaysShown } + isQuickHidden -> elements.filter { it.id == "builtin:keyboard" || it.alwaysShown } + else -> elements + }) + val style = layout?.style ?: InputSurfaceStyle() + surface.updateStyle(style) + quickToggle.tint = style.tint + quickToggle.alpha = style.opacity / 100f + quickToggle.visibility = if (isButtonsOnly || isPinnedOnly || layout == null || dock != null) GONE else VISIBLE + quickToggle.invalidate() + } + + fun takesTouchAt(x: Float, y: Float) = surface.takesTouchAt(x, y) || + quickToggle.visibility == VISIBLE && x >= quickToggle.left && x < quickToggle.right && y >= quickToggle.top && y < quickToggle.bottom + fun releaseAll() = surface.releaseInputs() + fun reload() { releaseAll(); placements.clear(); render() } + + override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { + val w = MeasureSpec.getSize(widthMeasureSpec) + val h = MeasureSpec.getSize(heightMeasureSpec) + if (w != viewWidth || h != viewHeight) { + viewWidth = w + viewHeight = h + render() + } + super.onMeasure(widthMeasureSpec, heightMeasureSpec) + } + + override fun onDetachedFromWindow() { releaseAll(); super.onDetachedFromWindow() } + + override fun onApplyWindowInsets(insets: WindowInsets): WindowInsets { + val cutout = if (Build.VERSION.SDK_INT >= 28) insets.displayCutout else null + val offset = ((QUICK_CENTER_DP - QUICK_HIT_DP) * resources.displayMetrics.density).toInt() + val params = quickToggle.layoutParams as LayoutParams + val left = (cutout?.safeInsetLeft ?: 0) + offset + val bottom = (cutout?.safeInsetBottom ?: 0) + offset + if (params.leftMargin != left || params.bottomMargin != bottom) { + params.leftMargin = left + params.bottomMargin = bottom + quickToggle.layoutParams = params + } + val fresh = Rect(cutout?.safeInsetLeft ?: 0, if (dock == null) cutout?.safeInsetTop ?: 0 else 0, cutout?.safeInsetRight ?: 0, cutout?.safeInsetBottom ?: 0) + if (fresh != safe) { + safe.set(fresh) + render() + } + return super.onApplyWindowInsets(insets) + } + + private inner class QuickToggle(context: Context) : View(context) { + var tint: Int? = ControllerPrefs.STEAM_BLUE + private val density = resources.displayMetrics.density + private var armed = false + private val fill = Paint(Paint.ANTI_ALIAS_FLAG) + private val stroke = Paint(Paint.ANTI_ALIAS_FLAG).apply { style = Paint.Style.STROKE; strokeWidth = 1.5f * density } + private val text = Paint(Paint.ANTI_ALIAS_FLAG).apply { + textAlign = Paint.Align.CENTER + typeface = Typeface.create(Typeface.DEFAULT, Typeface.BOLD) + textSize = 22 * density } - fun drag(x: Float, y: Float): Boolean { - travel = max(travel, hypot(x - downX, y - downY)) - var dx = x - ax - var dy = y - ay - val d = sqrt(dx * dx + dy * dy) - val atLimit = d > radius - if (atLimit) { dx = dx / d * radius; dy = dy / d * radius } - if (!atLimit && abs(dx - kx) < MIN_STICK_MOVE_PX && abs(dy - ky) < MIN_STICK_MOVE_PX) return false - kx = dx; ky = dy; dirty = true + override fun onDraw(canvas: Canvas) { + val x = width / 2f + val y = height / 2f + val radius = QUICK_RADIUS_DP * density + val color = tint + fill.color = if (color != null) InputSurfaceView.shade(color, 190, 0.12f) else (InputSurfaceView.SURFACE and 0x00ffffff) or (190 shl 24) + canvas.drawCircle(x, y, radius, fill) + stroke.color = if (color != null) InputSurfaceView.light(color, 150, 0.25f) else InputSurfaceView.LINE + canvas.drawCircle(x, y, radius, stroke) + text.color = if (color != null) InputSurfaceView.light(color, 210, 0.75f) else InputSurfaceView.TEXT + canvas.drawText(if (isQuickHidden) "+" else "−", x, y + text.textSize * 0.35f, text) + } + + @SuppressLint("ClickableViewAccessibility") + override fun onTouchEvent(event: MotionEvent): Boolean { + when (event.actionMasked) { + MotionEvent.ACTION_DOWN -> armed = true + MotionEvent.ACTION_CANCEL -> armed = false + MotionEvent.ACTION_UP -> { + if (armed && event.x in 0f..width.toFloat() && event.y in 0f..height.toFloat()) { + performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY) + setQuickHidden(!isQuickHidden) + } + armed = false + } + } return true } } - private val stickRedraw = ThrottledRedraw(this, STICK_REDRAW_MS) - private val tapSlop = android.view.ViewConfiguration.get(context).scaledTouchSlop.toFloat() - - private fun stickTap(control: Control, event: MotionEvent): Boolean { - val i = event.actionIndex - val travel = max(control.travel, hypot(event.getX(i) - control.downX, event.getY(i) - control.downY)) - return stickTap(control.downAt, event.eventTime, travel, tapSlop, control.clicked, - event.actionMasked == MotionEvent.ACTION_CANCEL || (event.flags and MotionEvent.FLAG_CANCELED) != 0) - } - - private val controls = listOf( - Control("up", "dpad", -1, 26f, 0f, -52f), - Control("right", "dpad", -1, 26f, 52f, 0f), - Control("down", "dpad", -1, 26f, 0f, 52f), - Control("left", "dpad", -1, 26f, -52f, 0f), - Control("ls", "ls", 0, 48f, 0f, 0f), - Control("rs", "rs", 1, 48f, 0f, 0f), - Control("a", "face", -1, 30f, 0f, 50f), - Control("b", "face", -1, 30f, 50f, 0f), - Control("x", "face", -1, 30f, -50f, 0f), - Control("y", "face", -1, 30f, 0f, -50f), - Control("lb", "lb", -1, 24f, 0f, 0f), - Control("rb", "rb", -1, 24f, 0f, 0f), - Control("lt", "lt", -1, 24f, 0f, 0f), - Control("rt", "rt", -1, 24f, 0f, 0f), - Control("select", "select", -1, 20f, 0f, 0f), - Control("start", "start", -1, 20f, 0f, 0f), - Control("guide", "guide", -1, 22f, 0f, 0f), - Control("qam", "qam", -1, 22f, 0f, 0f), - ) - - private val groups = controls.map { it.group }.distinct() - - private var buttonsOnly = false - val isButtonsOnly: Boolean get() = buttonsOnly - private var quickHidden = false - private var quickPressedBy = -1 - private var keyboardPressedBy = -1 - private var forwardedPointerBits = 0 - var onTouchMiss: ((MotionEvent) -> Unit)? = null - private var settings = ControllerPrefs.read(context) - private var snap = settings.snap - private var safe = Rect() - private var selected: String? = null - private var snapX: Float? = null - private var snapY: Float? = null - private var grabX = 0f - private var grabY = 0f - private var ignoreSaved = false - private var fit = 1f - - private var idleFill = 0 - private var heldFill = 0 - private var idleStroke = 0 - private var heldStroke = 0 - private var idleText = 0 - private var stickFill = 0 - private var knobFill = 0 - - private val fill = Paint(Paint.ANTI_ALIAS_FLAG).apply { style = Paint.Style.FILL } - private val stroke = Paint(Paint.ANTI_ALIAS_FLAG).apply { style = Paint.Style.STROKE } - private val arrow = Path() - private val box = RectF() - private val text = Paint(Paint.ANTI_ALIAS_FLAG).apply { - textAlign = Paint.Align.CENTER - typeface = android.graphics.Typeface.DEFAULT_BOLD - } - - init { - applySettings() - } - - private fun dp(value: Float) = value * resources.displayMetrics.density - - private fun scaled(value: Float) = dp(value) * settings.size / 100f * fit - - private fun fitScale(): Float { - val metrics = resources.displayMetrics - val heightMm = (height - safe.top - safe.bottom) / pxPerMm(metrics.ydpi) - val faceMm = dp(60f) / pxPerMm(metrics.xdpi) - val floor = (MIN_FACE_MM / faceMm).coerceAtMost(1f) - return (heightMm / FULL_SIZE_HEIGHT_MM).coerceIn(floor, 1f) - } - - fun reload() { - releaseAll() - settings = ControllerPrefs.read(context) - setSnap(settings.snap) - applySettings() - relayout() - } - - private fun clearSnapGuides() { - if (snapX == null && snapY == null) return - snapX = null - snapY = null - invalidate() - } - - private fun applySettings() { - for (control in controls) control.target = settings.mapping[control.id] ?: control.id - val tint = settings.tint - val alpha = settings.opacity / 100f - fun shade(a: Int, f: Float) = Color.argb((a * alpha).toInt(), (Color.red(tint) * f).toInt(), (Color.green(tint) * f).toInt(), (Color.blue(tint) * f).toInt()) - fun light(a: Int, f: Float) = Color.argb( - (a * alpha).toInt(), - (Color.red(tint) + (255 - Color.red(tint)) * f).toInt(), - (Color.green(tint) + (255 - Color.green(tint)) * f).toInt(), - (Color.blue(tint) + (255 - Color.blue(tint)) * f).toInt(), - ) - idleFill = shade(80, 0.12f) - heldFill = shade(160, 1f) - idleStroke = light(150, 0.25f) - heldStroke = light(230, 0.7f) - idleText = light(210, 0.75f) - stickFill = shade(60, 0.12f) - knobFill = shade(120, 0.35f) - stroke.strokeWidth = dp(1.5f) - } - - fun setButtonsOnly(enabled: Boolean) { - if (buttonsOnly == enabled) return - releaseAll() - buttonsOnly = enabled - quickHidden = false - relayout() - } - - fun setQuickHidden(hidden: Boolean) { - if (buttonsOnly || editing || quickHidden == hidden) return - if (hidden) releaseAll() - quickHidden = hidden - invalidate() - } - - fun setSnap(on: Boolean) { - snap = on - if (!on) clearSnapGuides() - } - - override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) { - super.onSizeChanged(w, h, oldw, oldh) - relayout() - } - - override fun onApplyWindowInsets(insets: WindowInsets): WindowInsets { - val cutout = if (Build.VERSION.SDK_INT >= 28) insets.displayCutout else null - val fresh = Rect(cutout?.safeInsetLeft ?: 0, cutout?.safeInsetTop ?: 0, cutout?.safeInsetRight ?: 0, cutout?.safeInsetBottom ?: 0) - if (fresh != safe) { safe = fresh; relayout() } - return super.onApplyWindowInsets(insets) - } - - override fun onDetachedFromWindow() { - stickRedraw.cancel() - releaseAll() - super.onDetachedFromWindow() - } - - private fun relayout() { - if (width <= 0 || height <= 0) return - fit = if (buttonsOnly && !editing) 1f else fitScale() - for (control in controls) control.radius = if (buttonsOnly && !editing && (control.id == "guide" || control.id == "qam")) dp(30f) else scaled(control.radiusDp) - if (buttonsOnly && !editing) { - val inset = dp(44f) - put("guide", safe.left + inset, height - safe.bottom - inset) - put("qam", width - safe.right - inset, height - safe.bottom - inset) - } else { - placeAuto(width.toFloat(), height.toFloat()) - if (!ignoreSaved) for ((group, pos) in ControllerPrefs.layout(context, width, height)) { - if (group in groups) put(group, pos.first * width, pos.second * height) - } - } - groups.forEach { clamp(it) } - invalidate() - } - - private fun pxPerMm(dpi: Float): Float { - val nominal = resources.displayMetrics.densityDpi.toFloat() - return (if (dpi > nominal * 0.6f && dpi < nominal * 1.6f) dpi else nominal) / 25.4f - } - - private fun placeAuto(w: Float, h: Float) { - val metrics = resources.displayMetrics - val mmX = pxPerMm(metrics.xdpi) - val mmY = pxPerMm(metrics.ydpi) - val usableW = w - safe.left - safe.right - val usableH = h - safe.top - safe.bottom - val inboard = (0.17f * usableW / mmX).coerceIn(24f, 40f) * mmX - val lift = (0.285f * usableH / mmY).coerceIn(22f, 45f) * mmY - val gap = dp(12f) - val floor = BOTTOM_MARGIN_MM * mmY - val drop = max(drop("dpad", "ls", gap), drop("rs", "face", gap)) - val restY = min(h - safe.bottom - lift, h - safe.bottom - floor - drop) - put("ls", safe.left + inboard, restY) - put("face", w - safe.right - inboard, restY) - besideBelow("dpad", "ls", 1f, gap, floor) - besideBelow("rs", "face", -1f, gap, floor) - val phone = usableH / mmY < PHONE_MAX_HEIGHT_MM - val shoulderY = if (phone) safe.top + floor + halfH("lb") - else min(centre("ls").second - extent("ls"), centre("face").second - extent("face")) - gap - halfH("lb") - shoulders("ls", "lb", "lt", 1f, gap, shoulderY) - shoulders("face", "rb", "rt", -1f, gap, shoulderY) - val bottom = h - safe.bottom - floor - extent("guide") - val spread = extent("guide") + gap / 2f - put("guide", w / 2f - spread, bottom) - put("qam", w / 2f + spread, bottom) - val inner = max(centre("dpad").first + extent("dpad"), w - centre("rs").first + extent("rs")) + gap + extent("select") - put("select", inner, restY) - put("start", w - inner, restY) - if (phone) { - val top = max(centre("lb").first + extent("lb"), w - centre("rb").first + extent("rb")) + gap * 2.5f + extent("select") - put("select", top, shoulderY) - put("start", w - top, shoulderY) - } - if (crowded("select", gap) || crowded("start", gap)) { - above("select", "dpad", gap) - val (sx, sy) = centre("select") - put("start", w - sx, sy) - if (crowded("start", gap)) above("start", "rs", gap) - } - if (crowded("guide", gap, gap, "qam") || crowded("qam", gap, gap, "guide")) { - val row = centre("select").second - put("guide", w / 2f - spread, row) - put("qam", w / 2f + spread, row) - if (crowded("guide", gap, gap, "qam") || crowded("qam", gap, gap, "guide")) { - val top = safe.top + dp(8f) + extent("guide") - put("guide", w / 2f - spread, top) - put("qam", w / 2f + spread, top) - } - } - } - - private fun drop(group: String, anchor: String, gap: Float): Float = - (extent(anchor) + extent(group) + gap) * sin(Math.toRadians(DROP_DEGREES)).toFloat() + extent(group) - - private fun besideBelow(group: String, anchor: String, inward: Float, gap: Float, margin: Float) { - val (ax, ay) = centre(anchor) - val reach = extent(anchor) + extent(group) + gap - val floor = height - safe.bottom - margin - extent(group) - val dy = min(reach * sin(Math.toRadians(DROP_DEGREES)).toFloat(), floor - ay) - val dx = sqrt(max(0f, reach * reach - dy * dy)) - put(group, ax + inward * dx, ay + dy) - } - - private fun shoulders(anchor: String, bumper: String, trigger: String, inward: Float, gap: Float, y: Float) { - val (ax, _) = centre(anchor) - val spread = extent(bumper) + gap / 2f - put(bumper, ax + inward * spread, y) - put(trigger, ax - inward * spread, y) - } - - private fun above(group: String, anchor: String, gap: Float) { - val (ax, ay) = centre(anchor) - put(group, ax, ay - extent(anchor) - gap - extent(group)) - } - - private fun crowded(group: String, gap: Float, space: Float = gap / 2f, except: String? = null): Boolean { - val (x, y) = centre(group) - return groups.any { other -> - if (other == group || other == except) return@any false - val (ox, oy) = centre(other) - val dx = x - ox - val dy = y - oy - sqrt(dx * dx + dy * dy) < extent(group) + extent(other) + space - } - } - - private fun members(group: String) = controls.filter { it.group == group } - - private fun centre(group: String): Pair { - val first = members(group).first() - return (first.cx - scaled(first.offsetXDp)) to (first.cy - scaled(first.offsetYDp)) - } - - private fun extent(group: String): Float = members(group).maxOf { control -> - sqrt(scaled(control.offsetXDp).let { it * it } + scaled(control.offsetYDp).let { it * it }) + control.halfW - } - - private fun halfH(group: String): Float = members(group).maxOf { abs(scaled(it.offsetYDp)) + it.halfH } - - private fun put(group: String, x: Float, y: Float) { - for (control in members(group)) { - control.cx = x + scaled(control.offsetXDp) - control.cy = y + scaled(control.offsetYDp) - } - } - - private fun clamp(group: String) { - val list = members(group) - val margin = dp(4f) - val minX = list.minOf { it.cx - it.halfW } - safe.left - margin - val maxX = list.maxOf { it.cx + it.halfW } - (width - safe.right - margin) - val minY = list.minOf { it.cy - it.halfH } - safe.top - margin - val maxY = list.maxOf { it.cy + it.halfH } - (height - safe.bottom - margin) - val dx = if (minX < 0) -minX else if (maxX > 0) -maxX else 0f - val dy = if (minY < 0) -minY else if (maxY > 0) -maxY else 0f - if (dx == 0f && dy == 0f) return - for (control in list) { control.cx += dx; control.cy += dy } - } - - fun saveLayout() { - if (width <= 0 || height <= 0) return - if (ignoreSaved) { ControllerPrefs.resetLayout(context, width, height); return } - ControllerPrefs.setLayout(context, width, height, groups.associateWith { group -> - val (x, y) = centre(group) - x / width to y / height - }) - } - - fun resetLayout() { - ControllerPrefs.resetLayout(context, width, height) - ignoreSaved = true - selected = null - relayout() - } - - private fun label(control: Control): String = when (control.target) { - "ls" -> "L" - "rs" -> "R" - "select" -> "⧉" - "start" -> "☰" - "guide" -> if (buttonsOnly && !editing) "Steam" else "◉" - "qam" -> "⋯" - else -> control.target.uppercase() - } - - override fun onDraw(canvas: Canvas) { - if (!quickHidden) for (control in controls) { - if (!isVisible(control)) continue - if (control.stick >= 0 && settings.adaptiveSticks && !editing && control.pressedBy == -1) continue - val held = control.pressedBy != -1 - val radius = control.radius - if (control.stick >= 0) { - val bx = if (held) control.ax else control.cx - val by = if (held) control.ay else control.cy - fill.color = if (held) heldFill else stickFill - canvas.drawCircle(bx, by, radius, fill) - stroke.color = if (held) heldStroke else idleStroke - canvas.drawCircle(bx, by, radius, stroke) - val knob = radius * 0.46f - fill.color = if (held || control.clicked) heldFill else knobFill - canvas.drawCircle(bx + control.kx, by + control.ky, knob, fill) - stroke.color = if (held) heldStroke else idleStroke - canvas.drawCircle(bx + control.kx, by + control.ky, knob, stroke) - text.color = if (held) Color.WHITE else idleText - text.textSize = knob * 0.8f - canvas.drawText(label(control), bx + control.kx, by + control.ky + text.textSize * 0.35f, text) - } else if (control.wide) { - box.set(control.cx - control.halfW, control.cy - control.halfH, control.cx + control.halfW, control.cy + control.halfH) - val corner = control.halfH * 0.55f - fill.color = if (held) heldFill else idleFill - canvas.drawRoundRect(box, corner, corner, fill) - stroke.color = if (held) heldStroke else idleStroke - canvas.drawRoundRect(box, corner, corner, stroke) - text.color = if (held) Color.WHITE else idleText - text.textSize = control.halfH * 0.8f - canvas.drawText(label(control), control.cx, control.cy + text.textSize * 0.35f, text) - } else { - fill.color = if (held) heldFill else idleFill - canvas.drawCircle(control.cx, control.cy, radius, fill) - stroke.color = if (held) heldStroke else idleStroke - canvas.drawCircle(control.cx, control.cy, radius, stroke) - text.color = if (held) Color.WHITE else idleText - val direction = directions.indexOf(control.target) - if (direction >= 0) { - drawArrow(canvas, control.cx, control.cy, radius * 0.34f, direction) - continue - } - val name = label(control) - text.textSize = if (name.length > 2) dp(11f) else radius * if (name.length == 2) 0.62f else 0.85f - canvas.drawText(name, control.cx, control.cy + text.textSize * 0.35f, text) - } - } - if (editing) { - stroke.color = heldStroke - snapX?.let { canvas.drawLine(it, safe.top.toFloat(), it, (height - safe.bottom).toFloat(), stroke) } - snapY?.let { canvas.drawLine(safe.left.toFloat(), it, (width - safe.right).toFloat(), it, stroke) } - selected?.let { group -> - val (x, y) = centre(group) - val single = members(group).singleOrNull() - if (single?.wide == true) { - val pad = dp(6f) - box.set(x - single.halfW - pad, y - single.halfH - pad, x + single.halfW + pad, y + single.halfH + pad) - canvas.drawRoundRect(box, single.halfH, single.halfH, stroke) - } else canvas.drawCircle(x, y, extent(group) + dp(6f), stroke) - } - } else if (!buttonsOnly) { - val (x, y) = quickCenter() - val radius = dp(18f) - fill.color = Color.argb(190, Color.red(idleFill), Color.green(idleFill), Color.blue(idleFill)) - canvas.drawCircle(x, y, radius, fill) - stroke.color = idleStroke - canvas.drawCircle(x, y, radius, stroke) - text.color = idleText - text.textSize = dp(22f) - canvas.drawText(if (quickHidden) "+" else "−", x, y + text.textSize * 0.35f, text) - } - if (!editing && settings.keyboardButton && onKeyboard != null) { - val (x, y) = keyboardCenter() - fill.color = Color.argb(190, Color.red(idleFill), Color.green(idleFill), Color.blue(idleFill)) - canvas.drawCircle(x, y, dp(18f), fill) - stroke.color = if (keyboardPressedBy == -1) idleStroke else heldStroke - canvas.drawCircle(x, y, dp(18f), stroke) - box.set(x - dp(10f), y - dp(7f), x + dp(10f), y + dp(7f)) - canvas.drawRoundRect(box, dp(2f), dp(2f), stroke) - for (row in 0..1) for (col in 0..2) { - val kx = x + dp(-6f + col * 5f) - val ky = y + dp(-3f + row * 4f) - canvas.drawLine(kx, ky, kx + dp(2f), ky, stroke) - } - canvas.drawLine(x - dp(5f), y + dp(4f), x + dp(5f), y + dp(4f), stroke) - } - } - - private fun quickCenter() = (safe.left + dp(34f)) to (height - safe.bottom - dp(34f)) - - private fun keyboardCenter() = if (buttonsOnly) - width / 2f to (height - safe.bottom - dp(44f)) - else (safe.left + dp(90f)) to (height - safe.bottom - dp(34f)) - - private fun keyboardContains(x: Float, y: Float): Boolean { - if (!settings.keyboardButton || onKeyboard == null) return false - val (cx, cy) = keyboardCenter() - val dx = x - cx - val dy = y - cy - return dx * dx + dy * dy <= dp(26f) * dp(26f) - } - - private fun quickContains(x: Float, y: Float): Boolean { - val (cx, cy) = quickCenter() - val radius = dp(26f) - val dx = x - cx - val dy = y - cy - return dx * dx + dy * dy <= radius * radius - } - - private fun drawArrow(canvas: Canvas, x: Float, y: Float, size: Float, direction: Int) { - val (fx, fy) = when (direction) { - 0 -> 0f to -1f - 1 -> 1f to 0f - 2 -> 0f to 1f - else -> -1f to 0f - } - arrow.reset() - arrow.moveTo(x + fx * size, y + fy * size) - arrow.lineTo(x - fx * size * 0.7f - fy * size, y - fy * size * 0.7f + fx * size) - arrow.lineTo(x - fx * size * 0.7f + fy * size, y - fy * size * 0.7f - fx * size) - arrow.close() - fill.color = text.color - canvas.drawPath(arrow, fill) - } - - @SuppressLint("ClickableViewAccessibility") - override fun onTouchEvent(event: MotionEvent): Boolean { - if (editing) return onEditTouch(event) - if (event.getToolType(event.actionIndex) == MotionEvent.TOOL_TYPE_MOUSE) return false - when (event.actionMasked) { - MotionEvent.ACTION_DOWN, MotionEvent.ACTION_POINTER_DOWN -> { - val index = event.actionIndex - val x = event.getX(index) - val y = event.getY(index) - if (event.actionMasked == MotionEvent.ACTION_DOWN) forwardedPointerBits = 0 - if (event.actionMasked == MotionEvent.ACTION_POINTER_DOWN && forwardMiss(event, x, y)) return true - if (forwardedPointerBits != 0) forwardPointers(event, forwardedPointerBits) - if (event.actionMasked == MotionEvent.ACTION_DOWN && keyboardContains(x, y)) { - keyboardPressedBy = event.getPointerId(index) - invalidate() - return true - } - if (keyboardPressedBy != -1) return true - if (!buttonsOnly && event.actionMasked == MotionEvent.ACTION_DOWN && quickContains(x, y)) { - quickPressedBy = event.getPointerId(index) - return true - } - if (quickPressedBy != -1) return true - if (quickHidden) return false - val control = controlAt(x, y) ?: adaptiveStickAt(x, y) ?: return false - if (control.pressedBy != -1) return true - control.pressedBy = event.getPointerId(index) - if (control.stick >= 0) { - control.clicked = settings.stickClick && control.lastUp > 0L && event.eventTime - control.lastUp < DOUBLE_TAP_MS - control.downAt = event.eventTime - control.downX = event.getX(index) - control.downY = event.getY(index) - control.travel = 0f - val adaptive = settings.adaptiveSticks - control.ax = if (adaptive) event.getX(index) else control.cx - control.ay = if (adaptive) event.getY(index) else control.cy - control.drag(event.getX(index), event.getY(index)) - control.dirty = true - } - if (SessionPrefs.lowLatency(context)) requestUnbufferedDispatch(event) - apply() - return true - } - MotionEvent.ACTION_MOVE -> { - if (forwardedPointerBits != 0) forwardPointers(event, forwardedPointerBits) - if (keyboardPressedBy != -1) return true - if (quickPressedBy != -1) return true - var changed = false - var stickMoved = false - for (index in 0 until event.pointerCount) { - val pointer = event.getPointerId(index) - if (forwardedPointerBits and (1 shl pointer) != 0) continue - val x = event.getX(index) - val y = event.getY(index) - val stick = controls.firstOrNull { it.stick >= 0 && it.pressedBy == pointer } - if (stick != null) { stickMoved = stick.drag(x, y) || stickMoved; continue } - val over = controlAt(x, y)?.takeIf { it.stick < 0 } - for (control in controls) { - if (control.stick < 0 && control.pressedBy == pointer && control !== over) { - control.pressedBy = -1 - changed = true - } - } - if (over != null && over.pressedBy == -1) { - over.pressedBy = pointer - changed = true - } - } - if (changed) apply() else if (stickMoved) apply(stickOnly = true) - return true - } - MotionEvent.ACTION_UP, MotionEvent.ACTION_POINTER_UP, MotionEvent.ACTION_CANCEL -> { - val pointer = event.getPointerId(event.actionIndex) - if (forwardedPointerBits != 0) forwardPointers(event, forwardedPointerBits) - if (event.actionMasked == MotionEvent.ACTION_CANCEL) { - forwardedPointerBits = 0 - releaseAll() - return true - } - if (forwardedPointerBits and (1 shl pointer) != 0) { - forwardedPointerBits = forwardedPointerBits and (1 shl pointer).inv() - return true - } - if (keyboardPressedBy == pointer) { - val tapped = event.actionMasked == MotionEvent.ACTION_UP && keyboardContains(event.x, event.y) - keyboardPressedBy = -1 - invalidate() - if (tapped) { releaseAll(); onKeyboard?.invoke() } - return true - } - if (quickPressedBy == pointer) { - val toggled = event.actionMasked == MotionEvent.ACTION_UP && quickContains(event.x, event.y) - quickPressedBy = -1 - if (toggled) setQuickHidden(!quickHidden) - return true - } - if (quickHidden) return false - var changed = false - for (control in controls) { - if (control.pressedBy == pointer || event.actionMasked == MotionEvent.ACTION_CANCEL) { - if (control.pressedBy != -1) changed = true - if (control.stick >= 0 && control.pressedBy != -1) control.lastUp = if (stickTap(control, event)) event.eventTime else 0L - control.pressedBy = -1 - control.clicked = false - if (control.stick >= 0 && (control.kx != 0f || control.ky != 0f)) control.dirty = true - control.kx = 0f; control.ky = 0f - } - } - if (changed) apply() - return true - } - } - return false - } - - private fun forwardMiss(event: MotionEvent, x: Float, y: Float): Boolean { - if (onTouchMiss == null || keyboardPressedBy != -1 || quickPressedBy != -1) return false - if (!quickHidden && (controlAt(x, y) ?: adaptiveStickAt(x, y)) != null) return false - forwardedPointerBits = forwardedPointerBits or (1 shl event.getPointerId(event.actionIndex)) - forwardPointers(event, forwardedPointerBits) - return true - } - - private fun forwardPointers(event: MotionEvent, pointerIdBits: Int) { - val split = event.pointerSubset(pointerIdBits) ?: return - try { - onTouchMiss?.invoke(split) - } finally { - split.recycle() - } - } - - private fun onEditTouch(event: MotionEvent): Boolean { - when (event.actionMasked) { - MotionEvent.ACTION_DOWN -> { - clearSnapGuides() - val control = controlAt(event.x, event.y) - selected = control?.group - val group = selected ?: run { invalidate(); return false } - val (x, y) = centre(group) - grabX = x - event.x - grabY = y - event.y - invalidate() - } - MotionEvent.ACTION_MOVE -> { - val group = selected ?: return true - ignoreSaved = false - val rawX = event.x + grabX - val rawY = event.y + grabY - val others = groups.filter { it != group && members(it).any(::isVisible) }.map { centre(it) } - val nextX = if (snap) closestSnap(rawX, others.map { it.first } + width / 2f) else null - val nextY = if (snap) closestSnap(rawY, others.map { it.second }) else null - put(group, nextX ?: rawX, nextY ?: rawY) - clamp(group) - val (x, y) = centre(group) - val visibleX = nextX?.takeIf { abs(x - it) <= SNAP_TOLERANCE_PX } - val visibleY = nextY?.takeIf { abs(y - it) <= SNAP_TOLERANCE_PX } - if ((visibleX != null && visibleX != snapX) || (visibleY != null && visibleY != snapY)) performHapticFeedback(HapticFeedbackConstants.CLOCK_TICK) - snapX = visibleX - snapY = visibleY - invalidate() - } - MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> clearSnapGuides() - } - return true - } - - private fun closestSnap(position: Float, targets: List): Float? = - targets.minByOrNull { abs(position - it) }?.takeIf { abs(position - it) <= dp(SNAP_DP) } - - fun takesTouchAt(x: Float, y: Float): Boolean = - editing || keyboardContains(x, y) || (!buttonsOnly && quickContains(x, y)) || - (!quickHidden && (controlAt(x, y) ?: adaptiveStickAt(x, y)) != null) - - private fun controlAt(x: Float, y: Float): Control? = - controls.firstOrNull { - !quickHidden && isVisible(it) && (editing || !settings.adaptiveSticks || it.stick < 0) && it.contains(x, y, it.radius) - } - - private fun adaptiveStickAt(x: Float, y: Float): Control? { - if (!settings.adaptiveSticks || editing || buttonsOnly || quickHidden) return null - if (x < safe.left || x >= width - safe.right || y < safe.top || y >= height - safe.bottom) return null - if (controls.any { control -> - control.stick < 0 && isVisible(control) && control.contains(x, y, control.radius, 5f) - }) return null - return controls.filter { control -> - val dx = if (control.stick == 0) max(0f, x - control.cx) else x - control.cx - val dy = y - control.cy - val reach = control.radius * 1.4f * sqrt(1.5f) - control.stick >= 0 && isVisible(control) && control.pressedBy == -1 && - dx * dx + dy * dy <= reach * reach - }.minByOrNull { control -> - val dx = x - control.cx - val dy = y - control.cy - dx * dx + dy * dy - } - } - - private fun isVisible(control: Control): Boolean = when { - !editing && control.id == "guide" && !settings.steamButton -> false - !editing && control.id == "qam" && !settings.qamButton -> false - buttonsOnly && !editing -> control.id == "guide" || control.id == "qam" - else -> control.target != ControllerPrefs.OFF - } - - private fun apply(stickOnly: Boolean = false) { - val bridge = pad - if (bridge == null || editing) { invalidate(); return } - val qamHeld = controls.any { it.id == "qam" && it.pressedBy != -1 } - if (buttonsOnly) { - bridge.setSystemButtons(controls.any { it.id == "guide" && it.pressedBy != -1 }, qamHeld) - } else { - bridge.setSystemButtons(false, qamHeld) - val stickWrites = controls.filter { it.stick >= 0 && it.dirty }.fold(0) { mask, control -> - mask or if (control.stick == 0) PadBridge.LEFT_STICK else PadBridge.RIGHT_STICK - } - bridge.applyTouch(stickWrites) { state -> - val used = HashSet() - val held = HashSet() - for (control in controls) { - when { - control.stick >= 0 -> { - val click = if (control.stick == 0) "l3" else "r3" - if (settings.stickClick) used.add(click) - if (control.clicked) held.add(click) - if (!control.dirty) continue - if (control.stick == 0) { state.leftX = control.kx / control.radius; state.leftY = control.ky / control.radius } - else { state.rightX = control.kx / control.radius; state.rightY = control.ky / control.radius } - control.dirty = false - } - control.id == "qam" || control.target == ControllerPrefs.OFF -> {} - else -> { - used.add(control.target) - if (control.pressedBy != -1) held.add(control.target) - } - } - } - for (target in used) write(state, target, target in held) - } - } - if (stickOnly) stickRedraw.request() else invalidate() - } - - private fun write(state: PadState, target: String, down: Boolean) { - when (target) { - "a" -> state.press(PadState.A, down) - "b" -> state.press(PadState.B, down) - "x" -> state.press(PadState.X, down) - "y" -> state.press(PadState.Y, down) - "lb" -> state.press(PadState.LB, down) - "rb" -> state.press(PadState.RB, down) - "select" -> state.press(PadState.SELECT, down) - "start" -> state.press(PadState.START, down) - "l3" -> state.press(PadState.L3, down) - "r3" -> state.press(PadState.R3, down) - "guide" -> state.press(PadState.GUIDE, down) - "lt" -> state.leftTrigger = if (down) 1f else 0f - "rt" -> state.rightTrigger = if (down) 1f else 0f - "up" -> state.up = down - "right" -> state.right = down - "down" -> state.down = down - "left" -> state.left = down - } - } - - fun releaseAll() { - quickPressedBy = -1 - keyboardPressedBy = -1 - invalidate() - if (controls.none { it.pressedBy != -1 || it.clicked } && !buttonsOnly) return - controls.forEach { - it.pressedBy = -1 - it.clicked = false - it.lastUp = 0L - if (it.stick >= 0 && (it.kx != 0f || it.ky != 0f)) it.dirty = true - it.kx = 0f; it.ky = 0f - } - apply() - } - private companion object { - const val DOUBLE_TAP_MS = 300L - const val STICK_TAP_MS = 200L - - internal fun stickTap(downAt: Long, upAt: Long, travel: Float, slop: Float, clicked: Boolean, cancelled: Boolean): Boolean = - !clicked && !cancelled && upAt - downAt <= STICK_TAP_MS && travel <= slop - const val STICK_REDRAW_MS = 33L - const val MIN_STICK_MOVE_PX = 1f - const val MIN_FACE_MM = 9.5f - const val FULL_SIZE_HEIGHT_MM = 85f - const val BOTTOM_MARGIN_MM = 5f - const val DROP_DEGREES = 35.0 - const val PHONE_MAX_HEIGHT_MM = 90f - const val SHOULDER_WIDTH = 1.45f - const val SHOULDER_HEIGHT = 0.8f - const val SNAP_DP = 9f - const val SNAP_TOLERANCE_PX = 1f - val shoulderIds = setOf("lb", "rb", "lt", "rt") - val directions = listOf("up", "right", "down", "left") + const val QUICK_CENTER_DP = 34f + const val QUICK_HIT_DP = 26f + const val QUICK_RADIUS_DP = 18f + const val PLACEMENT_CACHE = 4 } } diff --git a/app/src/main/java/com/droiddeck/launcher/input/OnScreenLayout.kt b/app/src/main/java/com/droiddeck/launcher/input/OnScreenLayout.kt new file mode 100644 index 0000000000..3935c1a565 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/input/OnScreenLayout.kt @@ -0,0 +1,95 @@ +package com.droiddeck.launcher.input + +import android.content.Context +import android.graphics.Rect +import kotlin.math.abs +import kotlin.math.max +import kotlin.math.min + +object OnScreenLayout { + data class Dock(val portrait: Boolean, val grips: Boolean, val left: InputControl = InputControl.LS, val right: InputControl = InputControl.RS) { + companion object { + fun deck(portrait: Boolean, gamepad: SavedInputLayout?): Dock { + fun side(right: Boolean) = gamepad?.elements?.firstOrNull { + (it.kind == InputElementKind.STICK || it.kind == InputElementKind.TRACKPAD) && (it.x + it.width / 2f >= .5f) == right + }?.kind + val left = if (side(false) == InputElementKind.TRACKPAD) InputControl.LEFT_PAD else InputControl.LS + val right = if (side(true) == InputElementKind.STICK) InputControl.RS else InputControl.RIGHT_PAD + return Dock(portrait, true, left, right) + } + } + } + + private const val TOLERANCE = .01f + private const val KEYBOARD_ID = "builtin:keyboard" + + fun elements(context: Context, layout: SavedInputLayout?, width: Int, height: Int, safe: Rect, + buttonsOnly: Boolean = false, dock: Dock? = null): List { + val stored = layout?.elements ?: return emptyList() + if (width <= 0 || height <= 0) return stored + val generator = LegacyGamepadLayout(context, width, height, safe, layout.scale) + val (reference, generated) = reference(context, layout, width, height) + if (buttonsOnly && dock == null) return generator.buttonsOnly(stored) + fit(stored.filter { it.alwaysShown }, reference.first, reference.second, width, height, safe) + val gamepad = if (generated) stored.filter(::isGamepad) else emptyList() + val others = fit(stored.filterNot { generated && isGamepad(it) || dock != null && it.alwaysShown }, reference.first, reference.second, width, height, safe) + val placed = when { + gamepad.isEmpty() -> emptyList() + dock != null -> generator.dock(gamepad, dock.portrait, dock.grips, dock.left, dock.right) + else -> generator.regenerate(gamepad) + } + val byId = (placed + others).associateBy { it.id } + return stored.mapNotNull { byId[it.id] } + placed.filter { p -> stored.none { it.id == p.id } } + } + + fun fit(elements: List, referenceWidth: Float, referenceHeight: Float, width: Int, height: Int, safe: Rect): List { + val w = width.toFloat() + val h = height.toFloat() + val scale = min(w / referenceWidth, h / referenceHeight) + return elements.map { e -> + val ew = e.width * referenceWidth * scale + val eh = e.height * referenceHeight * scale + val left = anchor(e.x, e.width, referenceWidth, w, scale).coerceIn(safe.left.toFloat(), max(safe.left.toFloat(), w - safe.right - ew)) + val top = anchor(e.y, e.height, referenceHeight, h, scale).coerceIn(safe.top.toFloat(), max(safe.top.toFloat(), h - safe.bottom - eh)) + e.copy(x = left / w, y = top / h, width = ew / w, height = eh / h).bounded() + } + } + + private fun anchor(start: Float, size: Float, reference: Float, actual: Float, scale: Float): Float { + val centre = start + size / 2f + val scaled = size * reference * scale + return when { + centre < 1f / 3f -> start * reference * scale + centre > 2f / 3f -> actual - (1f - start - size) * reference * scale - scaled + else -> centre * actual - scaled / 2f + } + } + + private fun isGamepad(e: InputSurfaceElement) = e.id.startsWith("legacy:") || e.id == KEYBOARD_ID + + fun generatedSize(context: Context, layout: SavedInputLayout, width: Int, height: Int): Pair? = + reference(context, layout, width, height).takeIf { it.second }?.first?.let { it.first.toInt() to it.second.toInt() } + + private fun reference(context: Context, layout: SavedInputLayout, width: Int, height: Int): Pair, Boolean> { + val (display, metrics) = InputLibraries.primaryDisplay(context) + val long = max(metrics.widthPixels, metrics.heightPixels).coerceAtLeast(1) + val short = min(metrics.widthPixels, metrics.heightPixels).coerceAtLeast(1) + val gamepad = layout.elements.filter(::isGamepad) + if (gamepad.any { it.id.startsWith("legacy:") }) { + val named = layout.id.removePrefix("migrated.layout.").takeIf { it != layout.id }?.split('x')?.mapNotNull { it.toIntOrNull() } + ?.takeIf { it.size == 2 && it.all { size -> size > 0 } }?.let { it[0] to it[1] } + for ((w, h) in listOfNotNull(named, width to height, long to short, short to long).distinct()) { + if (matches(gamepad, LegacyGamepadLayout(display, w, h, scale = layout.scale).elements())) return (w.toFloat() to h.toFloat()) to true + } + } + val aspect = layout.aspect.takeIf { it > 0f } ?: (long.toFloat() / short) + return (aspect to 1f) to false + } + + private fun matches(stored: List, generated: List): Boolean { + val byId = generated.associateBy { it.id } + return stored.all { e -> + val g = byId[e.id] ?: return@all false + abs(e.x - g.x) <= TOLERANCE && abs(e.y - g.y) <= TOLERANCE && abs(e.width - g.width) <= TOLERANCE && abs(e.height - g.height) <= TOLERANCE + } + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/input/PadBridge.java b/app/src/main/java/com/droiddeck/launcher/input/PadBridge.java index 4bf45e9fee..c7dd27a271 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/PadBridge.java +++ b/app/src/main/java/com/droiddeck/launcher/input/PadBridge.java @@ -59,11 +59,25 @@ public final class PadBridge { private final int[] retainedSticks = new int[SLOTS]; private final PadState effectiveState = new PadState(); private final PadState homeGuideReport = new PadState(); + private final PadState combinedState = new PadState(); + private InputRouter inputRouter; + private final InputRouter.PadSource[] physicalRoutes = new InputRouter.PadSource[SLOTS]; + private final java.util.IdentityHashMap touchSources = new java.util.IdentityHashMap<>(); + private final Object defaultTouchSource = new Object(); + + private static final class TouchSource { + final PadState raw = new PadState(); + final PadState passthrough = new PadState(); + final InputRole role; + boolean routed; + InputRouter.PadSource route; + TouchSource(InputRole role) { this.role = role; } + } private final Handler mainHandler = new Handler(Looper.getMainLooper()); private boolean open; - private volatile boolean selectSteam; private volatile boolean builtinPad = true; private TouchRoute touchRoute; + private boolean touchMouseActive; private final PadState routedTouch = new PadState(); private boolean systemGuidePressed; private boolean systemQamPressed; @@ -72,6 +86,7 @@ public final class PadBridge { private int homeGuideSlot = -1; private final PadState lastBackQamReport = new PadState(); private final PadState lastWrittenReport = new PadState(); + private final PadState liveReport = new PadState(); private boolean backQamAxesBlocked; private final java.util.Set backQamWrites = new java.util.HashSet<>(); private long backQamDelayMs, backQamWriteAt; @@ -79,6 +94,11 @@ public final class PadBridge { private int chordButton = -1; private boolean chordButtonPressed; private boolean qamTapPressed; + private boolean guideTapPressed; + private int guideTapGeneration; + private boolean vrStickTapPressed; + private int vrStickTapGeneration; + private boolean routedQamPressed; private int qamChordGeneration; private volatile Runnable onPlayerInput; private Runnable onChordInput; @@ -119,8 +139,36 @@ public final class PadBridge { for (int slot = 1; slot < SLOTS; slot++) playerStates[slot] = new PadState(); } - public void setSelectSteam(boolean on) { - selectSteam = on; + public synchronized void setInputRouter(InputRouter router) { + if (inputRouter == router) return; + for (InputRouter.PadSource route : physicalRoutes) if (route != null) route.close(); + for (TouchSource source : touchSources.values()) if (source.route != null) source.route.close(); + inputRouter = router; + for (int slot = 0; slot < SLOTS; slot++) physicalRoutes[slot] = router == null ? null : router.pad(InputRole.PHYSICAL, stateFor(slot)); + for (TouchSource source : touchSources.values()) source.route = router == null || source.routed ? null : router.pad(source.role, source.raw); + clearBackQamWrites(); + if (open) publish(); + for (int slot = 1; slot < SLOTS; slot++) if (playerWriters[slot] != null) publish(slot); + } + + public synchronized boolean hasInputRouter() { return inputRouter != null; } + + public synchronized InputRouter getInputRouter() { return inputRouter; } + + public synchronized void setInputProfile(InputProfile profile) { + if (inputRouter == null) return; + inputRouter.setProfile(profile); + clearBackQamWrites(); + if (open) publish(); + for (int slot = 1; slot < SLOTS; slot++) if (playerWriters[slot] != null) publish(slot); + } + + public synchronized void setInputEnabled(boolean enabled) { + if (inputRouter == null || !inputRouter.setEnabled(enabled)) return; + if (!enabled) clearBackQamWrites(); + if (open) publish(); + for (int slot = 1; slot < SLOTS; slot++) if (playerWriters[slot] != null) publish(slot); + DeckControls.INSTANCE.refresh(); } public void setExcluded(java.util.function.IntPredicate devices) { @@ -147,10 +195,18 @@ public final class PadBridge { homeGuidePressed = false; clearBackQamWrites(); routedTouch.clear(); + for (TouchSource source : touchSources.values()) { + source.raw.clear(); + if (source.route != null) source.route.release(); + } systemGuidePressed = false; systemQamPressed = false; backQamPressed = false; qamTapPressed = false; + guideTapPressed = false; + guideTapGeneration++; + vrStickTapPressed = false; + vrStickTapGeneration++; qamChordActive = false; chordButtonPressed = false; qamChordGeneration++; @@ -168,6 +224,16 @@ public final class PadBridge { } public synchronized void stop() { + guideTapPressed = false; + guideTapGeneration++; + vrStickTapPressed = false; + vrStickTapGeneration++; + if (inputRouter != null) inputRouter.releaseAll(); + for (TouchSource source : touchSources.values()) if (source.route != null) source.route.close(); + touchSources.clear(); + for (InputRouter.PadSource route : physicalRoutes) if (route != null) route.close(); + Arrays.fill(physicalRoutes, null); + inputRouter = null; homeGuidePressed = false; homeGuideSlot = -1; clearBackQamWrites(); @@ -314,6 +380,7 @@ public final class PadBridge { if (slot < 0) return false; int side = JoyCon.side(event.getDevice()); if (side >= 0 && partners[slot] != NO_CONTROLLER && JoyCon.isRail(side, event.getScanCode())) return true; + long timing = InputTiming.begin(); PadState pad = stateFor(slot); boolean pressed = event.getAction() == KeyEvent.ACTION_DOWN; switch (event.getKeyCode()) { @@ -324,9 +391,6 @@ public final class PadBridge { case KeyEvent.KEYCODE_BUTTON_L1: pad.press(4, pressed); break; case KeyEvent.KEYCODE_BUTTON_R1: pad.press(5, pressed); break; case KeyEvent.KEYCODE_BUTTON_SELECT: - if (selectSteam) pad.press(PadState.GUIDE, pressed); - else pad.press(6, pressed); - break; case KeyEvent.KEYCODE_BACK: pad.press(6, pressed); break; case KeyEvent.KEYCODE_BUTTON_START: case KeyEvent.KEYCODE_MENU: pad.press(7, pressed); break; @@ -341,8 +405,14 @@ public final class PadBridge { case KeyEvent.KEYCODE_BUTTON_C: if (isPrimary(event)) DeckControls.INSTANCE.setGrip(isNovaController(event.getDevice()) ? DeckControls.L4 : DeckControls.R4, pressed); break; - case KeyEvent.KEYCODE_BUTTON_L2: pad.leftTrigger = pressed ? 1f : 0f; break; - case KeyEvent.KEYCODE_BUTTON_R2: pad.rightTrigger = pressed ? 1f : 0f; break; + case KeyEvent.KEYCODE_BUTTON_L2: + pad.leftTrigger = keyTrigger(pad.leftTrigger, pressed, event.getRepeatCount(), + analogTrigger(event.getDevice(), MotionEvent.AXIS_LTRIGGER, MotionEvent.AXIS_BRAKE)); + break; + case KeyEvent.KEYCODE_BUTTON_R2: + pad.rightTrigger = keyTrigger(pad.rightTrigger, pressed, event.getRepeatCount(), + analogTrigger(event.getDevice(), MotionEvent.AXIS_RTRIGGER, MotionEvent.AXIS_GAS)); + break; case KeyEvent.KEYCODE_DPAD_UP: pad.up = pressed; break; case KeyEvent.KEYCODE_DPAD_RIGHT: pad.right = pressed; break; case KeyEvent.KEYCODE_DPAD_DOWN: pad.down = pressed; break; @@ -355,6 +425,7 @@ public final class PadBridge { statButtons++; scheduleStats(); publish(slot); + InputTiming.end(timing); return true; } @@ -422,6 +493,7 @@ public final class PadBridge { if (event.getAction() != MotionEvent.ACTION_MOVE) return false; int slot = slotFor(event.getDevice()); if (slot < 0) return false; + long timing = InputTiming.begin(); PadState pad = stateFor(slot); noteDevice(event.getDevice(), slot); statAxes++; @@ -438,6 +510,7 @@ public final class PadBridge { if (chordStickInput) noteChordInput(slot); if (Math.max(Math.abs(x), Math.abs(y)) > 0.5f) notePlayerInput(); publish(slot); + InputTiming.end(timing); return true; } float lx = event.getAxisValue(MotionEvent.AXIS_X); @@ -476,9 +549,20 @@ public final class PadBridge { notePlayerInput(); } publish(slot); + InputTiming.end(timing); return true; } + static float keyTrigger(float current, boolean pressed, int repeatCount, boolean analog) { + if (analog || repeatCount > 0) return current; + return pressed ? 1f : 0f; + } + + static boolean analogTrigger(InputDevice device, int... axes) { + return wiredXboxLayout(device, InputDevice.SOURCE_JOYSTICK) + || hasAxis(device, InputDevice.SOURCE_JOYSTICK, axes); + } + static boolean hasAxis(InputDevice device, int source, int... axes) { if (device == null) return false; for (int axis : axes) if (device.getMotionRange(axis, source) != null) return true; @@ -493,41 +577,70 @@ public final class PadBridge { public synchronized void setTouchRoute(TouchRoute route) { touchRoute = route; + touchMouseActive = false; routedTouch.clear(); } public synchronized void applyTouch(java.util.function.Consumer mutation) { - applyTouch(0, mutation); + applyTouch(defaultTouchSource, InputRole.PRIMARY, 0, mutation); } public synchronized void applyTouch(int stickWrites, java.util.function.Consumer mutation) { + applyTouch(defaultTouchSource, InputRole.PRIMARY, stickWrites, mutation); + } + + public synchronized void applyTouch(Object owner, InputRole role, java.util.function.Consumer mutation) { + applyTouch(owner, role, 0, mutation); + } + + public synchronized void applyTouch(Object owner, InputRole role, int stickWrites, java.util.function.Consumer mutation) { if (!builtinPad) return; - if (touchRoute != null && touchRoute.active()) { - mutation.accept(routedTouch); - touchRoute.accept(routedTouch); - boolean changed = false; - for (int button : ROUTED_PASS_THROUGH) { - boolean down = routedTouch.isDown(button); - if (state.isDown(button) == down) continue; - state.press(button, down); - changed = true; + long timing = InputTiming.begin(); + TouchSource source = touchSources.get(owner); + if (source == null) { + source = new TouchSource(role); + if (inputRouter != null) source.route = inputRouter.pad(role); + touchSources.put(owner, source); + } + float lx = source.raw.leftX, ly = source.raw.leftY, rx = source.raw.rightX, ry = source.raw.rightY; + mutation.accept(source.raw); + if (inputRouter == null && (touchRoute == null || !touchRoute.active())) { + if ((stickWrites & LEFT_STICK) != 0 || source.raw.leftX != lx || source.raw.leftY != ly) { + retainedSticks[0] &= ~LEFT_STICK; + state.leftX = state.leftY = 0f; } - if (changed) { - activeControllerId = NO_CONTROLLER; - slotControllers.set(0, NO_CONTROLLER); - publish(); + if ((stickWrites & RIGHT_STICK) != 0 || source.raw.rightX != rx || source.raw.rightY != ry) { + retainedSticks[0] &= ~RIGHT_STICK; + state.rightX = state.rightY = 0f; } - return; } - routedTouch.clear(); - retainedSticks[0] &= ~stickWrites; - mutateState(mutation); activeControllerId = NO_CONTROLLER; slotControllers.set(0, NO_CONTROLLER); notePlayerInput(); statOnScreen++; scheduleStats(); publish(); + InputTiming.end(timing); + } + + public synchronized void releaseTouch(Object owner) { + TouchSource source = touchSources.remove(owner); + if (source == null) return; + if (source.route != null) source.route.close(); + publish(); + } + + public synchronized void applyRoutedTouch(Object owner, InputRole role, java.util.function.Consumer mutation) { + if (!builtinPad) return; + TouchSource source = touchSources.get(owner); + if (source == null) { + source = new TouchSource(role); + source.routed = true; + touchSources.put(owner, source); + } + mutation.accept(source.raw); + notePlayerInput(); + publish(); } public synchronized void applyExternal(java.util.function.Consumer mutation, boolean changed) { @@ -552,16 +665,30 @@ public final class PadBridge { } public synchronized void releaseAll() { + guideTapGeneration++; + vrStickTapPressed = false; + vrStickTapGeneration++; + routedQamPressed = false; + systemGuidePressed = false; + systemQamPressed = false; + guideTapPressed = false; + qamTapPressed = false; + qamChordActive = false; + chordButtonPressed = false; + qamChordGeneration++; + if (inputRouter != null) inputRouter.releaseAll(); + if (inputRouter == null) for (TouchSource source : touchSources.values()) source.raw.clear(); + routedTouch.clear(); + DeckControls.INSTANCE.suspend(); + if (inputRouter == null) clearPlayerState(0); homeGuidePressed = false; homeGuideSlot = -1; clearBackQamWrites(); - routedTouch.clear(); backQamPressed = false; - releaseBackPaddles(); - clearPlayerState(0); + if (inputRouter == null) releaseBackPaddles(); publish(); for (int slot = 1; slot < SLOTS; slot++) { - clearPlayerState(slot); + if (inputRouter == null) clearPlayerState(slot); if (playerWriters[slot] == null) continue; publish(slot); } @@ -602,7 +729,11 @@ public final class PadBridge { if (!homeGuidePressed) return; clearBackQamWrites(); homeGuidePressed = false; - clearPlayerState(0); + if (inputRouter == null) clearPlayerState(0); + else { + inputRouter.releaseAll(); + DeckControls.INSTANCE.suspend(); + } publish(); } @@ -641,7 +772,11 @@ public final class PadBridge { if (!backQamPressed && backQamWrites.isEmpty()) return; clearBackQamWrites(); backQamPressed = false; - clearPlayerState(0); + if (inputRouter == null) clearPlayerState(0); + else { + inputRouter.releaseAll(); + DeckControls.INSTANCE.suspend(); + } publish(); } @@ -677,6 +812,7 @@ public final class PadBridge { } private void writePad(PadState report) { + liveReport.copyFrom(report); if (backQamAxesBlocked && !hasQamAxes()) backQamAxesBlocked = false; long at = android.os.SystemClock.uptimeMillis(); if (backQamPressed || homeGuidePressed || !backQamWrites.isEmpty()) at += backQamDelayMs; @@ -684,11 +820,11 @@ public final class PadBridge { } private boolean hasQamAxes() { - boolean left = (retainedSticks[0] & 1) != 0 - ? hasChordStickInput(rawSticks[0][0], rawSticks[0][1]) : state.leftX != 0f || state.leftY != 0f; - boolean right = (retainedSticks[0] & 2) != 0 - ? hasChordStickInput(rawSticks[0][2], rawSticks[0][3]) : state.rightX != 0f || state.rightY != 0f; - return left || right || state.leftTrigger != 0f || state.rightTrigger != 0f; + boolean left = (retainedSticks[0] & 1) != 0 && liveReport.leftX == state.leftX && liveReport.leftY == state.leftY + ? hasChordStickInput(rawSticks[0][0], rawSticks[0][1]) : liveReport.leftX != 0f || liveReport.leftY != 0f; + boolean right = (retainedSticks[0] & 2) != 0 && liveReport.rightX == state.rightX && liveReport.rightY == state.rightY + ? hasChordStickInput(rawSticks[0][2], rawSticks[0][3]) : liveReport.rightX != 0f || liveReport.rightY != 0f; + return left || right || liveReport.leftTrigger != 0f || liveReport.rightTrigger != 0f; } private static void clearAxes(PadState pad) { @@ -709,9 +845,9 @@ public final class PadBridge { PadState live = new PadState(); live.copyFrom(report); if (queued) { - live.leftX = state.leftX; live.leftY = state.leftY; - live.rightX = state.rightX; live.rightY = state.rightY; - live.leftTrigger = state.leftTrigger; live.rightTrigger = state.rightTrigger; + live.leftX = liveReport.leftX; live.leftY = liveReport.leftY; + live.rightX = liveReport.rightX; live.rightY = liveReport.rightY; + live.leftTrigger = liveReport.leftTrigger; live.rightTrigger = liveReport.rightTrigger; } boolean held = (backQamPressed && report.isDown(PadState.QAM)) || (homeGuidePressed && report.isDown(PadState.GUIDE)); if ((queued && !held) || backQamAxesBlocked) clearAxes(live); @@ -732,6 +868,34 @@ public final class PadBridge { chordHandler.postDelayed(() -> releaseQamTap(generation), QAM_A_HOLD_MS); } + public synchronized void triggerSteamMenu() { + if (!builtinPad || guideTapPressed) return; + guideTapPressed = true; + int generation = ++guideTapGeneration; + publish(); + chordHandler.postDelayed(() -> { + synchronized (PadBridge.this) { + if (generation != guideTapGeneration) return; + guideTapPressed = false; + publish(); + } + }, 90); + } + + public synchronized void triggerVrStickSwitch() { + if (!builtinPad || vrStickTapPressed) return; + vrStickTapPressed = true; + int generation = ++vrStickTapGeneration; + publish(); + chordHandler.postDelayed(() -> { + synchronized (PadBridge.this) { + if (generation != vrStickTapGeneration) return; + vrStickTapPressed = false; + publish(); + } + }, 120); + } + private synchronized void releaseQamTap(int generation) { if (generation != qamChordGeneration || !qamTapPressed) return; qamTapPressed = false; @@ -856,6 +1020,7 @@ public final class PadBridge { } private void free(int slot) { + if (physicalRoutes[slot] != null) physicalRoutes[slot].release(); Log.i(TAG, "player " + (slot + 1) + " (id " + owners[slot] + ") left"); owners[slot] = NO_CONTROLLER; partners[slot] = NO_CONTROLLER; @@ -909,6 +1074,7 @@ public final class PadBridge { publish(); return; } + PadState output = physicalRoutes[slot] == null ? playerStates[slot] : physicalRoutes[slot].route(playerStates[slot]); FakeInputWriter player = playerWriters[slot]; if (player == null) { player = new FakeInputWriter(fakeInputDir.getAbsolutePath(), slot); @@ -918,43 +1084,75 @@ public final class PadBridge { } playerWriters[slot] = player; } - PadState report = playerStates[slot]; if (slot == homeGuideSlot) { - homeGuideReport.copyFrom(report); + homeGuideReport.copyFrom(output); homeGuideReport.press(PadState.GUIDE, true); - report = homeGuideReport; + output = homeGuideReport; } - player.writePad(report); + player.writePad(output); } private void publish() { if (!builtinPad && !open && owners[0] == NO_CONTROLLER && partners[0] == NO_CONTROLLER) return; + PadState output = physicalRoutes[0] == null ? state : physicalRoutes[0].route(state); + boolean mouseRoute = (!touchSources.isEmpty() || touchMouseActive) && touchRoute != null && touchRoute.active(); + touchMouseActive = mouseRoute && !touchSources.isEmpty(); + if (mouseRoute) routedTouch.clear(); + if (!touchSources.isEmpty()) { + combinedState.copyFrom(output); + for (TouchSource source : touchSources.values()) { + PadState touch = source.route == null ? source.raw : source.route.route(source.raw); + if (mouseRoute) { + routedTouch.merge(touch); + source.passthrough.clear(); + for (int button : ROUTED_PASS_THROUGH) source.passthrough.press(button, touch.isDown(button)); + combinedState.merge(source.passthrough); + } else combinedState.merge(touch); + } + output = combinedState; + } + if (mouseRoute) touchRoute.accept(routedTouch); + boolean guide = systemGuidePressed || qamChordActive || guideTapPressed || homeGuidePressed; + boolean routedQam = output.isDown(PadState.QAM); + boolean routedQamStarted = routedQam && !routedQamPressed; + routedQamPressed = routedQam; + if (routedQamStarted && !nativeQam() && !qamChordActive) { + startChord(PadState.A); + return; + } + boolean qam = nativeQam() && (systemQamPressed || qamTapPressed || backQamPressed); if (!open && !start()) { statDropped++; return; } - boolean guide = systemGuidePressed || qamChordActive || homeGuidePressed; - boolean qam = nativeQam() && (systemQamPressed || qamTapPressed || backQamPressed); - if (!guide && !qam) { - writePad(state); + if (!guide && !qam && !vrStickTapPressed && (!routedQam || nativeQam())) { + writePad(output); return; } - effectiveState.copyFrom(state); + effectiveState.copyFrom(output); + if (!nativeQam()) effectiveState.press(PadState.QAM, false); if (guide) effectiveState.press(PadState.GUIDE, true); if (qam) effectiveState.press(PadState.QAM, true); if (chordButtonPressed) effectiveState.press(chordButton, true); + if (vrStickTapPressed) { + effectiveState.press(PadState.L3, true); + effectiveState.press(PadState.R3, true); + } writePad(effectiveState); } public static boolean wiredXboxLayout(MotionEvent event) { - InputDevice device = event.getDevice(); + return wiredXboxLayout(event.getDevice(), event.getSource()); + } + + static boolean wiredXboxLayout(InputDevice device, int source) { return device != null && ((device.getVendorId() == 0x20d6 && device.getProductId() == 0x2063) || (device.getVendorId() == 0x0e6f && device.getProductId() == 0x02f0)) - && device.getMotionRange(MotionEvent.AXIS_RX, event.getSource()) != null - && device.getMotionRange(MotionEvent.AXIS_RY, event.getSource()) != null - && device.getMotionRange(MotionEvent.AXIS_Z, event.getSource()) != null - && device.getMotionRange(MotionEvent.AXIS_RZ, event.getSource()) != null; + && device.getMotionRange(MotionEvent.AXIS_RX, source) != null + && device.getMotionRange(MotionEvent.AXIS_RY, source) != null + && device.getMotionRange(MotionEvent.AXIS_Z, source) != null + && device.getMotionRange(MotionEvent.AXIS_RZ, source) != null; } public static float leftTrigger(MotionEvent event, boolean wired) { diff --git a/app/src/main/java/com/droiddeck/launcher/input/PadState.kt b/app/src/main/java/com/droiddeck/launcher/input/PadState.kt index d337a2a320..a1261dcd8b 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/PadState.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/PadState.kt @@ -36,6 +36,19 @@ class PadState { buttons = 0 } + fun merge(other: PadState) { + buttons = buttons or other.buttons + up = up || other.up; right = right || other.right; down = down || other.down; left = left || other.left + leftTrigger = maxOf(leftTrigger, other.leftTrigger) + rightTrigger = maxOf(rightTrigger, other.rightTrigger) + if (other.leftX * other.leftX + other.leftY * other.leftY > leftX * leftX + leftY * leftY) { + leftX = other.leftX; leftY = other.leftY + } + if (other.rightX * other.rightX + other.rightY * other.rightY > rightX * rightX + rightY * rightY) { + rightX = other.rightX; rightY = other.rightY + } + } + companion object { const val A = 0 const val B = 1 diff --git a/app/src/main/java/com/droiddeck/launcher/input/PhysicalInputBindings.kt b/app/src/main/java/com/droiddeck/launcher/input/PhysicalInputBindings.kt new file mode 100644 index 0000000000..fcb883a997 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/input/PhysicalInputBindings.kt @@ -0,0 +1,89 @@ +package com.droiddeck.launcher.input + +import android.view.InputDevice +import android.view.KeyEvent + +data class PhysicalInputKey(val device: String, val name: String, val keyCode: Int, val scanCode: Int) { + val label: String get() = "$name · ${KeyEvent.keyCodeToString(keyCode).removePrefix("KEYCODE_")} ($scanCode)" + + companion object { + fun identity(device: InputDevice) = "${device.vendorId}:${device.productId}:${device.descriptor.ifBlank { device.name }}" + + fun capture(event: KeyEvent): PhysicalInputKey? { + val device = event.device ?: return null + if (device.isVirtual || PadBridge.isSystemVirtualInputName(device.name)) return null + when (event.keyCode) { + KeyEvent.KEYCODE_VOLUME_UP, KeyEvent.KEYCODE_VOLUME_DOWN, KeyEvent.KEYCODE_VOLUME_MUTE, + KeyEvent.KEYCODE_HOME, KeyEvent.KEYCODE_POWER, KeyEvent.KEYCODE_SLEEP, + KeyEvent.KEYCODE_WAKEUP, KeyEvent.KEYCODE_APP_SWITCH -> return null + } + if (event.keyCode == KeyEvent.KEYCODE_UNKNOWN && event.scanCode <= 0) return null + return PhysicalInputKey(identity(device), device.name, event.keyCode, event.scanCode) + } + } +} + +data class PhysicalInputBinding(val key: PhysicalInputKey, val action: InputAction) + +class PhysicalInputBindings(private val router: InputRouter) { + private class Entry(val binding: PhysicalInputBinding, val channel: InputRouter.Channel) { + var heldDevice = -1 + var suppressed = false + } + private var entries = emptyList() + private val released = ArrayList() + private val devices = HashMap() + + fun configure(bindings: List) { + if (entries.map { it.binding } == bindings) return + for (entry in entries) { + entry.channel.close() + if (entry.heldDevice != -1) released.add(entry) + } + entries = bindings.map { Entry(it, router.channel(InputRole.PHYSICAL, InputControl.A, it.action)) } + } + + fun onKey(event: KeyEvent, enabled: Boolean): Boolean { + if (event.action != KeyEvent.ACTION_DOWN && event.action != KeyEvent.ACTION_UP) return false + for (i in released.indices.reversed()) { + val old = released[i] + if (old.heldDevice == event.deviceId && old.binding.key.keyCode == event.keyCode && old.binding.key.scanCode == event.scanCode) { + if (event.action == KeyEvent.ACTION_UP) released.removeAt(i) + return true + } + } + for (entry in entries) { + val key = entry.binding.key + if (key.keyCode != event.keyCode || key.scanCode != event.scanCode) continue + if (entry.heldDevice != -1 && entry.heldDevice == event.deviceId) { + if (event.action == KeyEvent.ACTION_UP) { + entry.channel.update(0f); entry.heldDevice = -1; entry.suppressed = false + } + return true + } + if (!enabled || event.action != KeyEvent.ACTION_DOWN || entry.binding.action is InputAction.Native) continue + val identity = devices[event.deviceId] ?: event.device?.let { PhysicalInputKey.identity(it) }?.also { devices[event.deviceId] = it } ?: continue + if (identity != key.device) continue + entry.heldDevice = event.deviceId + entry.suppressed = event.repeatCount != 0 + if (!entry.suppressed) entry.channel.update(1f) + return true + } + return false + } + + fun release() { + for (entry in entries) { + entry.channel.release() + if (entry.heldDevice != -1) entry.suppressed = true + } + } + + fun disconnected(id: Int) { + devices.remove(id) + released.removeAll { it.heldDevice == id } + for (entry in entries) if (entry.heldDevice == id) { entry.channel.release(); entry.heldDevice = -1 } + } + + fun close() { entries.forEach { it.channel.close() }; entries = emptyList(); released.clear(); devices.clear() } +} diff --git a/app/src/main/java/com/droiddeck/launcher/input/PhysicalShortcuts.kt b/app/src/main/java/com/droiddeck/launcher/input/PhysicalShortcuts.kt index a79fa8c706..11e76cb4ad 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/PhysicalShortcuts.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/PhysicalShortcuts.kt @@ -105,6 +105,10 @@ object PhysicalShortcuts { write(context, read(context).filterNot { it == binding }) } + fun restoreDefault(context: Context, action: Action) { + write(context, read(context).filterNot { it.action == action }) + } + private fun write(context: Context, bindings: List) { val array = JSONArray() bindings.forEach { diff --git a/app/src/main/java/com/droiddeck/launcher/input/ScreenKey.kt b/app/src/main/java/com/droiddeck/launcher/input/ScreenKey.kt deleted file mode 100644 index 4223754320..0000000000 --- a/app/src/main/java/com/droiddeck/launcher/input/ScreenKey.kt +++ /dev/null @@ -1,52 +0,0 @@ -package com.droiddeck.launcher.input - -import android.content.Context - -fun screenKeyLabel(context: Context, codes: List): String = - codes.filter { it != 0 }.joinToString("+") { short(context, it) } - -private fun short(context: Context, code: Int): String = when (code) { - 28 -> "⏎" - 14 -> "⌫" - 15 -> "Tab" - 57 -> "Space" - 58 -> "Caps" - 42, 54 -> "Shift" - 29, 97 -> "Ctrl" - 56 -> "Alt" - 100 -> "AltGr" - 125 -> "Super" - else -> pcKeyLabel(context, code) -} - -const val SCREEN_KEY_PILL = 0 -const val SCREEN_KEY_ROUND = 1 -const val SCREEN_KEY_CIRCLE = 2 -const val SCREEN_KEY_RECT = 3 -const val SCREEN_KEY_TRIGGER = 4 -const val SCREEN_KEY_DPAD = 5 -const val SCREEN_KEY_STICK = 6 - -val SCREEN_KEY_DPAD_DIRS = listOf("↑", "→", "↓", "←") -val SCREEN_KEY_STICK_DIRS = listOf("↑", "↗", "→", "↘", "↓", "↙", "←", "↖") - -fun screenKeyDirections(shape: Int): List = when (shape) { - SCREEN_KEY_DPAD -> SCREEN_KEY_DPAD_DIRS - SCREEN_KEY_STICK -> SCREEN_KEY_STICK_DIRS - else -> emptyList() -} - -data class ScreenKey( - val codes: List, - val x: Float, - val y: Float, - val size: Int, - val glyph: String = "", - val text: String = "", - val image: String = "", - val opacity: Float = 1f, - val shape: Int = SCREEN_KEY_PILL, - val toggle: Boolean = false, - val labels: Boolean = true, -) { -} diff --git a/app/src/main/java/com/droiddeck/launcher/input/ScreenKeys.kt b/app/src/main/java/com/droiddeck/launcher/input/ScreenKeys.kt deleted file mode 100644 index 2e27eecbe6..0000000000 --- a/app/src/main/java/com/droiddeck/launcher/input/ScreenKeys.kt +++ /dev/null @@ -1,1098 +0,0 @@ -package com.droiddeck.launcher.input - -import android.content.Context -import android.graphics.Bitmap -import android.graphics.BitmapFactory -import android.graphics.Canvas -import android.graphics.Color -import android.graphics.Paint -import android.graphics.PointF -import android.graphics.Rect -import android.graphics.RectF -import android.graphics.Typeface -import android.os.Build -import android.view.MotionEvent -import android.view.View -import android.view.WindowInsets -import java.io.File -import kotlin.math.abs -import kotlin.math.hypot -import kotlin.math.round - -class ScreenKeys(context: Context, private val sendKey: (Int, Boolean) -> Unit) : View(context) { - private class Entry(val codes: List) { - var glyph = "" - var text = "" - var image = "" - var bitmap: Bitmap? = null - var bitmapFor = "" - var x = 0.5f - var y = 0.5f - var size = DEFAULT_SIZE - var opacity = 1f - var shape = SCREEN_KEY_PILL - var toggle = false - var labels = true - var armed = -1 - var cx = 0f - var cy = 0f - var halfW = 0f - var halfH = 0f - } - - private val entries = mutableListOf() - private val down = HashMap() - private val latched = mutableListOf() - private val dpadDown = HashMap() - private val stickEntries = HashMap() - private val stickCodes = HashMap>() - private val held = HashMap() - private var forwardedPointerBits = 0 - private var addX = -1f - private var addY = -1f - private var pressX = 0f - private var pressY = 0f - private var guideX = -1f - private var guideY = -1f - private val addPress = Runnable { addX = pressX; addY = pressY; onAdd?.invoke() } - private var editing = false - val isEditing get() = editing - private var selected: Entry? = null - private var dragX = 0f - private var dragY = 0f - private var dragging = false - private var moved = false - private var toolbarDrag = false - private var safe = Rect() - private var tint = ControllerPrefs.STEAM_BLUE - - init { - ControllerPrefs.screenKeys(context).forEach { key -> entries += entryOf(key) } - } - - var followSteam = false - set(value) { - if (field == value) return - field = value - invalidate() - } - - var steamStyle: () -> Pair? = { null } - - var onAdd: (() -> Unit)? = null - var onSelect: ((List) -> Unit)? = null - var onEditingChanged: ((Boolean) -> Unit)? = null - var onTouchMiss: ((MotionEvent) -> Unit)? = null - - private val fillPaint = Paint(Paint.ANTI_ALIAS_FLAG) - private val strokePaint = Paint(Paint.ANTI_ALIAS_FLAG).apply { style = Paint.Style.STROKE } - private val ringPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply { style = Paint.Style.STROKE } - private val textPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply { - textAlign = Paint.Align.CENTER - typeface = Typeface.DEFAULT_BOLD - } - private val gridPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply { style = Paint.Style.STROKE } - private val rect = RectF() - private val bodyPathRect = RectF() - private val bodyPath = android.graphics.Path() - private val clipPath = android.graphics.Path() - private val imageRect = RectF() - private val bitmapRect = RectF() - - private val capsuleRadius = dp(24f) - private val capsuleGap = dp(62f) - private val toolbar = PointF() - private val addCenter = PointF() - private val doneCenter = PointF() - - fun startEditing() { - if (editing) return - editing = true - releaseAll() - selected = null - placeToolbar() - invalidate() - onEditingChanged?.invoke(true) - } - - fun stopEditing() { - if (!editing) return - releaseAll() - editing = false - selected = null - save() - invalidate() - onEditingChanged?.invoke(false) - } - - fun add(codes: List) { - if (codes.isEmpty()) return - entries.firstOrNull { it.codes == codes }?.let { - selected = it - invalidate() - return - } - val entry = Entry(codes) - if (addX >= 0f && width > 0) { - entry.x = (addX / width).coerceIn(0.05f, 0.95f) - entry.y = (addY / height).coerceIn(0.05f, 0.95f) - } else { - entry.x = (0.30f + 0.08f * (entries.size % 5)).coerceAtMost(0.70f) - entry.y = 0.62f - } - addX = -1f - addY = -1f - entries += entry - if (width > 0) measure(entry) - selected = entry - save() - invalidate() - } - - fun remove(codes: List) { - val entry = entries.firstOrNull { it.codes == codes } ?: return - entries.remove(entry) - if (selected === entry) selected = null - save() - invalidate() - } - - fun setCodes(from: List, to: List): ScreenKey? { - if (to.isEmpty()) return null - val index = entries.indexOfFirst { it.codes == from } - if (index < 0) return null - val old = entries[index] - if (to != from && entries.any { it !== old && it.codes == to }) return null - val fresh = Entry(to).also { - it.glyph = old.glyph - it.text = old.text - it.image = old.image - it.bitmap = old.bitmap - it.bitmapFor = old.bitmapFor - it.x = old.x - it.y = old.y - it.size = old.size - it.opacity = old.opacity - it.shape = old.shape - it.toggle = old.toggle - it.labels = old.labels - } - entries[index] = fresh - measure(fresh) - if (selected === old) selected = fresh - save() - invalidate() - return stateOf(fresh.codes) - } - - fun setText(codes: List, text: String) { - val entry = entries.firstOrNull { it.codes == codes } ?: return - entry.text = text - measure(entry) - save() - invalidate() - } - - fun setImage(codes: List, image: String) { - val entry = entries.firstOrNull { it.codes == codes } ?: return - val previous = entry.image - entry.image = image - entry.bitmap = null - entry.bitmapFor = "" - if (previous.isNotEmpty() && previous != image) File(File(context.filesDir, IMAGE_DIR), previous).delete() - measure(entry) - save() - invalidate() - } - - fun resize(codes: List, size: Int) { - val entry = entries.firstOrNull { it.codes == codes } ?: return - entry.size = size.coerceIn(MIN_SIZE, MAX_SIZE) - measure(entry) - save() - invalidate() - } - - fun setOpacity(codes: List, opacity: Float) { - val entry = entries.firstOrNull { it.codes == codes } ?: return - entry.opacity = opacity.coerceIn(0.2f, 1f) - save() - invalidate() - } - - fun setShape(codes: List, shape: Int) { - val entry = entries.firstOrNull { it.codes == codes } ?: return - entry.shape = shape.coerceIn(SCREEN_KEY_PILL, SCREEN_KEY_STICK) - measure(entry) - save() - invalidate() - } - - fun setToggle(codes: List, toggle: Boolean) { - val entry = entries.firstOrNull { it.codes == codes } ?: return - entry.toggle = toggle - if (!toggle && latched.remove(entry)) release(entry) - save() - invalidate() - } - - fun setLabels(codes: List, labels: Boolean) { - val entry = entries.firstOrNull { it.codes == codes } ?: return - entry.labels = labels - save() - invalidate() - } - - fun setDirection(codes: List, index: Int, code: Int): ScreenKey? { - val entry = entries.firstOrNull { it.codes == codes } ?: entries.firstOrNull { it.codes == trimmed(codes) } ?: return null - val next = entry.codes.toMutableList() - while (next.size <= index) next += 0 - next[index] = code - val nextCodes = trimmed(next) - if (entries.any { it !== entry && it.codes == nextCodes }) return null - val updated = Entry(nextCodes).also { - it.glyph = entry.glyph - it.text = entry.text - it.image = entry.image - it.bitmap = entry.bitmap - it.bitmapFor = entry.bitmapFor - it.x = entry.x - it.y = entry.y - it.size = entry.size - it.opacity = entry.opacity - it.shape = entry.shape - it.toggle = entry.toggle - it.labels = entry.labels - } - entries[entries.indexOf(entry)] = updated - measure(updated) - if (selected === entry) selected = updated - save() - invalidate() - return stateOf(updated.codes) - } - - private fun trimmed(codes: List): List = codes.dropLastWhile { it == 0 }.ifEmpty { listOf(0) } - - fun stateOf(codes: List): ScreenKey? = entries.firstOrNull { it.codes == codes }?.let { - ScreenKey(it.codes, it.x, it.y, it.size, it.glyph, it.text, it.image, it.opacity, it.shape, it.toggle, it.labels) - } - - fun deselect() { - if (selected == null) return - selected = null - invalidate() - } - - override fun onAttachedToWindow() { - super.onAttachedToWindow() - refreshAppearance() - } - - fun refreshAppearance() { - tint = ControllerPrefs.read(context).tint - invalidate() - } - - override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) { - super.onSizeChanged(w, h, oldw, oldh) - entries.forEach { measure(it) } - placeToolbar() - } - - override fun onApplyWindowInsets(insets: WindowInsets): WindowInsets { - val cutout = if (Build.VERSION.SDK_INT >= 28) insets.displayCutout else null - val fresh = Rect( - cutout?.safeInsetLeft ?: 0, cutout?.safeInsetTop ?: 0, - cutout?.safeInsetRight ?: 0, cutout?.safeInsetBottom ?: 0, - ) - if (fresh != safe) { - safe = fresh - placeToolbar() - invalidate() - } - return super.onApplyWindowInsets(insets) - } - - override fun onDetachedFromWindow() { - releaseAll() - super.onDetachedFromWindow() - } - - override fun onVisibilityChanged(changedView: View, visibility: Int) { - super.onVisibilityChanged(changedView, visibility) - if (visibility != VISIBLE) releaseAll() - } - - override fun setVisibility(visibility: Int) { - super.setVisibility(visibility) - if (visibility != VISIBLE) releaseAll() - } - - fun releaseAll() { - dpadDown.values.forEach { releaseKey(it) } - dpadDown.clear() - entries.forEach { it.armed = -1 } - stickCodes.values.flatten().forEach { if (it != 0) releaseKey(it) } - stickCodes.clear() - stickEntries.clear() - down.values.forEach { release(it) } - down.clear() - latched.toList().forEach { release(it) } - latched.clear() - invalidate() - } - - private fun snap(value: Float) = if (width > 0) round(value / (width / 100f)) * (width / 100f) else value - - private fun placeToolbar() { - if (width == 0 || height == 0) return - if (toolbar.x == 0f && toolbar.y == 0f) { - toolbar.set(width - safe.right - capsuleGap - capsuleRadius, height - safe.bottom - capsuleRadius - dp(20f)) - } - addCenter.set(toolbar.x - capsuleGap / 2f, toolbar.y) - doneCenter.set(toolbar.x + capsuleGap / 2f, toolbar.y) - } - - private fun entryOf(key: ScreenKey): Entry = Entry(key.codes).also { - it.glyph = key.glyph - it.text = key.text - it.image = key.image - it.x = key.x - it.y = key.y - it.size = key.size - it.opacity = key.opacity - it.shape = key.shape - it.toggle = key.toggle - it.labels = key.labels - } - - private fun label(entry: Entry) = when { - entry.text.isNotEmpty() -> entry.text - entry.glyph.isNotEmpty() -> entry.glyph - else -> screenKeyLabel(context, entry.codes) - } - - private fun bitmapFor(entry: Entry): Bitmap? { - if (entry.image.isEmpty()) return null - if (entry.bitmapFor == entry.image) return entry.bitmap - val file = File(File(context.filesDir, IMAGE_DIR), entry.image) - val bitmap = if (file.isFile) BitmapFactory.decodeFile(file.absolutePath) else null - entry.bitmap = bitmap - entry.bitmapFor = entry.image - return bitmap - } - - private fun measure(entry: Entry) { - val h = dp(entry.size.toFloat()) - entry.halfH = h / 2f - entry.halfW = when { - entry.shape == SCREEN_KEY_CIRCLE -> entry.halfH - entry.shape == SCREEN_KEY_TRIGGER -> entry.halfH * 1.7f - entry.shape == SCREEN_KEY_DPAD || entry.shape == SCREEN_KEY_STICK -> entry.halfH * 1.15f - entry.image.isNotEmpty() -> entry.halfH * 1.15f - hasIcon(entry) -> entry.halfH * 1.15f - else -> { - val chars = label(entry).length - entry.halfH * (0.55f * chars + 1.05f).coerceIn(1.7f, 4.6f) - } - } - entry.cx = entry.x * width - entry.cy = entry.y * height - } - - private fun hasIcon(entry: Entry): Boolean = - entry.text.isEmpty() && entry.glyph.isEmpty() && entry.codes.size == 1 && ScreenKeyIcons.has(entry.codes[0]) - - private fun unit() = minOf(width / 1280f, height / 720f) - - private fun accent(): Pair { - val style = if (followSteam) steamStyle() else null - return (style?.first ?: tint) to (style?.second ?: 1f) - } - - private fun tinted(color: Int, alpha: Float): Int { - val scale = accent().second - return Color.argb( - (alpha * scale * 255f).toInt().coerceIn(0, 255), - Color.red(color), Color.green(color), Color.blue(color), - ) - } - - private fun triggerPath(entry: Entry, path: android.graphics.Path, inset: Float = 0f) { - val lean = (entry.halfH - inset) * 0.6f - val left = entry.cx - entry.halfW + inset - val right = entry.cx + entry.halfW - inset - val top = entry.cy - entry.halfH + inset - val bottom = entry.cy + entry.halfH - inset - path.reset() - path.moveTo(left + lean, top) - path.lineTo(right, top) - path.lineTo(right - lean, bottom) - path.lineTo(left, bottom) - path.close() - } - - private fun body(canvas: Canvas, entry: Entry, radius: Float, paint: Paint, inset: Float = 0f) { - if (entry.shape != SCREEN_KEY_TRIGGER) { - canvas.drawRoundRect(rect, radius, radius, paint) - return - } - triggerPath(entry, bodyPath, inset) - canvas.drawPath(bodyPath, paint) - } - - private fun drawDpad(canvas: Canvas, entry: Entry) { - val color = accent().first - val alpha = entry.opacity - val r = minOf(entry.halfW, entry.halfH) - val t = r * 0.42f - val hairline = maxOf(dp(1.2f), entry.halfH * 0.032f) - val engaged = entry.armed >= 0 - crossPath(entry, bodyPath, r, t) - fillPaint.style = Paint.Style.FILL - fillPaint.color = tinted(color, if (entry.armed >= 0) 0.26f * alpha else 0.10f * alpha) - canvas.drawPath(bodyPath, fillPaint) - if (entry.armed >= 0) { - armRect(entry, bodyPathRect, entry.armed, r, t) - fillPaint.color = tinted(color, 0.55f * alpha) - canvas.drawRoundRect(bodyPathRect, t * 0.6f, t * 0.6f, fillPaint) - } - strokePaint.strokeJoin = Paint.Join.ROUND - strokePaint.strokeWidth = if (engaged) hairline * 1.6f else hairline - strokePaint.color = tinted(color, (if (editing) 0.90f else 0.80f) * alpha) - canvas.drawPath(bodyPath, strokePaint) - textPaint.setShadowLayer(0f, 0f, 0f, 0) - fillPaint.color = tinted(color, if (editing) 0.90f else 0.80f) - DPAD_DIRS.forEachIndexed { index, dir -> - val on = entry.armed == index - val code = entry.codes.getOrNull(index) ?: 0 - val name = if (code == 0) "" else screenKeyLabel(context, listOf(code)) - if (entry.labels && name.isNotEmpty()) { - textPaint.color = if (on) Color.WHITE else tinted(color, alpha) - textPaint.textSize = dp(48f) - val fit = 48f * (t * 1.9f) / textPaint.measureText(name).coerceAtLeast(1f) - textPaint.textSize = minOf(fit, t * 1.7f).coerceIn(dp(8f), dp(60f)) - val tx = entry.cx + dir.first * (r - t * 0.6f) - val ty = entry.cy + dir.second * (r - t * 0.6f) - canvas.drawText(name, tx, ty - (textPaint.ascent() + textPaint.descent()) / 2f, textPaint) - } else { - fillPaint.color = if (on) Color.WHITE else tinted(color, if (editing) 0.90f else 0.80f) - arrow(canvas, fillPaint, entry.cx, entry.cy, r - t * 0.55f, dir.first, dir.second, t * 0.8f) - } - } - if (editing && selected === entry) { - ringPaint.strokeWidth = hairline * 0.8f - ringPaint.color = tinted(color, 0.9f) - ringPaint.style = Paint.Style.STROKE - canvas.drawCircle(entry.cx, entry.cy, r + hairline * 3f, ringPaint) - } - } - - private fun crossPath(entry: Entry, path: android.graphics.Path, r: Float, t: Float) { - val cx = entry.cx - val cy = entry.cy - path.reset() - path.moveTo(cx - t, cy - r) - path.lineTo(cx + t, cy - r) - path.lineTo(cx + t, cy - t) - path.lineTo(cx + r, cy - t) - path.lineTo(cx + r, cy + t) - path.lineTo(cx + t, cy + t) - path.lineTo(cx + t, cy + r) - path.lineTo(cx - t, cy + r) - path.lineTo(cx - t, cy + t) - path.lineTo(cx - r, cy + t) - path.lineTo(cx - r, cy - t) - path.lineTo(cx - t, cy - t) - path.close() - } - - private fun armRect(entry: Entry, rect: RectF, index: Int, r: Float, t: Float) { - when (index) { - 0 -> rect.set(entry.cx - t, entry.cy - r, entry.cx + t, entry.cy - t) - 1 -> rect.set(entry.cx + t, entry.cy - t, entry.cx + r, entry.cy + t) - 2 -> rect.set(entry.cx - t, entry.cy + t, entry.cx + t, entry.cy + r) - else -> rect.set(entry.cx - r, entry.cy - t, entry.cx - t, entry.cy + t) - } - } - - private fun arrow(canvas: Canvas, paint: Paint, cx: Float, cy: Float, dist: Float, dx: Float, dy: Float, size: Float) { - val tipX = cx + dx * dist - val tipY = cy + dy * dist - val baseX = cx + dx * (dist - size) - val baseY = cy + dy * (dist - size) - val nx = -dy * size * 0.62f - val ny = dx * size * 0.62f - bodyPath.reset() - bodyPath.moveTo(tipX, tipY) - bodyPath.lineTo(baseX + nx, baseY + ny) - bodyPath.lineTo(baseX - nx, baseY - ny) - bodyPath.close() - canvas.drawPath(bodyPath, paint) - } - - private fun armOf(entry: Entry, x: Float, y: Float): Int { - val dx = x - entry.cx - val dy = y - entry.cy - return if (abs(dx) > abs(dy)) (if (dx > 0f) 1 else 3) else (if (dy > 0f) 2 else 0) - } - - private fun drawStick(canvas: Canvas, entry: Entry) { - val color = accent().first - val alpha = entry.opacity - val engaged = entry.armed >= 0 - val radius = minOf(entry.halfW, entry.halfH) - val hairline = maxOf(dp(1.2f), entry.halfH * 0.032f) - fillPaint.style = Paint.Style.FILL - fillPaint.color = tinted(color, 0.14f * alpha) - canvas.drawCircle(entry.cx, entry.cy, radius - hairline, fillPaint) - strokePaint.strokeWidth = if (engaged) hairline * 1.6f else hairline - strokePaint.color = tinted(color, (if (editing) 0.90f else 0.80f) * alpha) - canvas.drawCircle(entry.cx, entry.cy, radius - hairline / 2f, strokePaint) - val dir = if (engaged) STICK_DIRS[entry.armed.coerceIn(0, 7)] else 0f to 0f - val knob = radius * 0.36f - fillPaint.color = tinted(color, if (engaged) 0.55f * alpha else 0.24f * alpha) - canvas.drawCircle(entry.cx + dir.first * radius * 0.40f, entry.cy + dir.second * radius * 0.40f, knob, fillPaint) - strokePaint.strokeWidth = hairline - strokePaint.color = tinted(color, (if (editing) 0.90f else 0.80f) * alpha) - canvas.drawCircle(entry.cx + dir.first * radius * 0.40f, entry.cy + dir.second * radius * 0.40f, knob, strokePaint) - textPaint.setShadowLayer(0f, 0f, 0f, 0) - STICK_DIRS.forEachIndexed { index, unit -> - val on = entry.armed == index - val code = entry.codes.getOrNull(index) ?: 0 - val name = if (code == 0) "" else screenKeyLabel(context, listOf(code)) - if (entry.labels && name.isNotEmpty()) { - textPaint.color = if (on) Color.WHITE else tinted(color, 0.85f * alpha) - textPaint.textSize = dp(48f) - val fit = 48f * (radius * 0.34f) / textPaint.measureText(name).coerceAtLeast(1f) - textPaint.textSize = minOf(fit, radius * 0.22f).coerceAtLeast(dp(6f)) - val tx = entry.cx + unit.first * radius * 0.66f - val ty = entry.cy + unit.second * radius * 0.66f - canvas.drawText(name, tx, ty - (textPaint.ascent() + textPaint.descent()) / 2f, textPaint) - } else { - fillPaint.color = if (on) Color.WHITE else tinted(color, if (editing) 0.88f else 0.72f) - arrow(canvas, fillPaint, entry.cx, entry.cy, radius * 0.88f, unit.first, unit.second, radius * 0.16f) - } - } - if (editing && selected === entry) { - ringPaint.strokeWidth = hairline * 0.8f - ringPaint.color = tinted(color, 0.9f) - ringPaint.style = Paint.Style.STROKE - canvas.drawCircle(entry.cx, entry.cy, radius + hairline * 3f, ringPaint) - } - } - - private fun directionCodes(entry: Entry, index: Int): List { - if (index < 0) return emptyList() - val code = entry.codes.getOrNull(index) ?: 0 - if (code != 0) return listOf(code) - if (entry.shape != SCREEN_KEY_STICK || index % 2 == 0) return emptyList() - return listOfNotNull( - entry.codes.getOrNull((index + 7) % 8)?.takeIf { it != 0 }, - entry.codes.getOrNull((index + 1) % 8)?.takeIf { it != 0 }, - ) - } - - private fun stickSector(entry: Entry, x: Float, y: Float): Int { - val dx = x - entry.cx - val dy = y - entry.cy - if (hypot(dx, dy) < minOf(entry.halfW, entry.halfH) * 0.24f) return -1 - val degrees = Math.toDegrees(kotlin.math.atan2(dy, dx).toDouble()).toFloat() - val index = (round(degrees / 45f).toInt() + 2) % 8 - return (index + 8) % 8 - } - - private fun radiusOf(entry: Entry): Float = when (entry.shape) { - SCREEN_KEY_ROUND -> entry.halfH * 0.30f - SCREEN_KEY_RECT -> entry.halfH * 0.12f - SCREEN_KEY_TRIGGER -> entry.halfH * 0.45f - else -> entry.halfH - } - - fun takesTouchAt(x: Float, y: Float): Boolean = editing || entryAt(x, y) != null - - private fun entryAt(x: Float, y: Float): Entry? = entries.lastOrNull { entry -> - x >= entry.cx - entry.halfW && x <= entry.cx + entry.halfW && y >= entry.cy - entry.halfH && y <= entry.cy + entry.halfH - } - - private fun inCircle(center: PointF, x: Float, y: Float, radius: Float) = hypot(x - center.x, y - center.y) <= radius - - private fun inToolbar(x: Float, y: Float): Boolean = - editing && x >= toolbar.x - capsuleGap / 2f - capsuleRadius && x <= toolbar.x + capsuleGap / 2f + capsuleRadius && - y >= toolbar.y - capsuleRadius && y <= toolbar.y + capsuleRadius - - override fun onDraw(canvas: Canvas) { - if (editing) drawGrid(canvas) - entries.forEach { draw(canvas, it) } - if (editing) { - drawGuides(canvas) - drawToolbar(canvas) - } - } - - private fun drawGuides(canvas: Canvas) { - if (guideX < 0f && guideY < 0f) return - gridPaint.strokeWidth = dp(1f) - gridPaint.color = tinted(accent().first, 0.9f) - if (guideX >= 0f) canvas.drawLine(guideX, 0f, guideX, height.toFloat(), gridPaint) - if (guideY >= 0f) canvas.drawLine(0f, guideY, width.toFloat(), guideY, gridPaint) - } - - private fun drawGrid(canvas: Canvas) { - if (width == 0 || height == 0) return - val step = width / 100f - gridPaint.strokeWidth = dp(1f) - gridPaint.color = Color.argb(38, 255, 255, 255) - var x = step - while (x < width) { - canvas.drawLine(x, 0f, x, height.toFloat(), gridPaint) - x += step - } - var y = step - while (y < height) { - canvas.drawLine(0f, y, width.toFloat(), y, gridPaint) - y += step - } - gridPaint.color = Color.argb(96, 255, 255, 255) - gridPaint.strokeWidth = dp(1f) - canvas.drawLine(width / 2f, 0f, width / 2f, height.toFloat(), gridPaint) - canvas.drawLine(0f, height / 2f, width.toFloat(), height / 2f, gridPaint) - } - - private fun draw(canvas: Canvas, entry: Entry) { - if (entry.shape == SCREEN_KEY_DPAD) { - drawDpad(canvas, entry) - return - } - if (entry.shape == SCREEN_KEY_STICK) { - drawStick(canvas, entry) - return - } - val style = if (followSteam) steamStyle() else null - val engaged = down.values.any { it === entry } || latched.contains(entry) - val bitmap = bitmapFor(entry) - val icon = bitmap == null && hasIcon(entry) - val frameless = entry.text.isBlank() && (bitmap != null || icon) - val alpha = entry.opacity - val h2 = entry.halfH * 2f - val radius = radiusOf(entry) - val selectedNow = editing && selected === entry - val hairline = maxOf(dp(1.2f), h2 * 0.032f) - rect.set(entry.cx - entry.halfW, entry.cy - entry.halfH, entry.cx + entry.halfW, entry.cy + entry.halfH) - val inkColor: Int - if (style != null) { - val inkAlpha = (255f * style.second * alpha).toInt().coerceIn(30, 255) - inkColor = Color.argb(inkAlpha, Color.red(style.first), Color.green(style.first), Color.blue(style.first)) - fillPaint.style = Paint.Style.FILL - if (engaged) { - fillPaint.color = Color.argb((inkAlpha * 0.85f).toInt(), 230, 233, 236) - body(canvas, entry, radius, fillPaint) - } - if (frameless) { - if (engaged) { - fillPaint.color = Color.argb((inkAlpha * 0.45f).toInt(), 230, 233, 236) - body(canvas, entry, radius, fillPaint) - } - } else { - strokePaint.color = if (selectedNow) Color.rgb(26, 159, 255) else inkColor - strokePaint.strokeWidth = if (engaged) hairline * 1.6f else hairline - body(canvas, entry, radius, strokePaint) - } - } else { - fillPaint.style = Paint.Style.FILL - if (!frameless) { - fillPaint.color = tinted(BODY, 0.60f * alpha) - canvas.drawRoundRect(rect, radius, radius, fillPaint) - } - if (engaged) { - fillPaint.color = tinted(accent().first, if (frameless) 0.32f * alpha else 0.42f * alpha) - canvas.drawRoundRect(rect, radius, radius, fillPaint) - } - if (!frameless) { - strokePaint.strokeWidth = if (engaged) hairline * 1.6f else hairline - strokePaint.color = tinted( - accent().first, - (if (engaged) 0.96f else if (editing) 0.80f else 0.68f) * alpha, - ) - body(canvas, entry, radius, strokePaint) - } - inkColor = if (engaged) Color.WHITE else tinted(accent().first, alpha) - } - if (editing && selected === entry) { - ringPaint.strokeWidth = hairline * 0.8f - ringPaint.color = tinted(accent().first, 0.9f) - val gap = hairline * 2.4f - rect.set( - entry.cx - entry.halfW - gap, entry.cy - entry.halfH - gap, - entry.cx + entry.halfW + gap, entry.cy + entry.halfH + gap, - ) - if (entry.shape == SCREEN_KEY_TRIGGER) { - body(canvas, entry, radius + gap, ringPaint, gap) - } else { - canvas.drawRoundRect(rect, radius + gap, radius + gap, ringPaint) - } - rect.set(entry.cx - entry.halfW, entry.cy - entry.halfH, entry.cx + entry.halfW, entry.cy + entry.halfH) - } - - if (bitmap != null) { - imageRect.set(rect) - canvas.save() - clipPath.reset() - when (entry.shape) { - SCREEN_KEY_CIRCLE -> clipPath.addCircle( - entry.cx, entry.cy, minOf(entry.halfW, entry.halfH), android.graphics.Path.Direction.CW, - ) - SCREEN_KEY_TRIGGER -> triggerPath(entry, clipPath, 0f) - else -> clipPath.addRoundRect(rect, radius, radius, android.graphics.Path.Direction.CW) - } - canvas.clipPath(clipPath) - val scale = maxOf(imageRect.width() / bitmap.width, imageRect.height() / bitmap.height) - val w = bitmap.width * scale - val h = bitmap.height * scale - bitmapRect.set( - imageRect.centerX() - w / 2f, imageRect.centerY() - h / 2f, - imageRect.centerX() + w / 2f, imageRect.centerY() + h / 2f, - ) - canvas.drawBitmap(bitmap, null, bitmapRect, fillPaint) - canvas.restore() - } - - val name = label(entry) - textPaint.color = inkColor - if (bitmap != null && name.isEmpty()) return - var iconDrawn = false - if (icon) { - val box = minOf(entry.halfW, entry.halfH) * 1.35f - iconDrawn = ScreenKeyIcons.draw(canvas, entry.codes[0], entry.cx, entry.cy, box, textPaint) - textPaint.color = inkColor - } - if (iconDrawn || name.isEmpty()) return - val overImage = bitmap != null - if (overImage) textPaint.color = Color.WHITE - textPaint.setShadowLayer(if (overImage) dp(3f) else 0f, 0f, 0f, Color.argb(200, 0, 0, 0)) - textPaint.textSize = dp(48f) - val available = rect.width() - hairline * 4f - val fit = 48f * available / textPaint.measureText(name).coerceAtLeast(1f) - textPaint.textSize = minOf(fit, entry.halfH * 1.15f).coerceIn(dp(8f), dp(120f)) - canvas.drawText(name, entry.cx, entry.cy - (textPaint.ascent() + textPaint.descent()) / 2f, textPaint) - textPaint.clearShadowLayer() - } - - private fun drawToolbar(canvas: Canvas) { - val (color, _) = accent() - val capsule = RectF( - toolbar.x - capsuleGap / 2f - capsuleRadius, toolbar.y - capsuleRadius, - toolbar.x + capsuleGap / 2f + capsuleRadius, toolbar.y + capsuleRadius, - ) - fillPaint.style = Paint.Style.FILL - fillPaint.color = tinted(color, 0.30f) - canvas.drawRoundRect(capsule, capsuleRadius, capsuleRadius, fillPaint) - strokePaint.strokeWidth = dp(1.5f) - strokePaint.color = tinted(color, 0.85f) - canvas.drawRoundRect(capsule, capsuleRadius, capsuleRadius, strokePaint) - - textPaint.color = Color.WHITE - textPaint.setShadowLayer(0f, 0f, 0f, 0) - textPaint.textSize = capsuleRadius * 1.15f - val plus = "+" - canvas.drawText(plus, addCenter.x, addCenter.y - (textPaint.ascent() + textPaint.descent()) / 2f, textPaint) - val path = android.graphics.Path() - val check = capsuleRadius * 0.42f - path.moveTo(doneCenter.x - check, doneCenter.y) - path.lineTo(doneCenter.x - check * 0.25f, doneCenter.y + check * 0.7f) - path.lineTo(doneCenter.x + check, doneCenter.y - check * 0.7f) - ringPaint.strokeWidth = dp(3f) - ringPaint.strokeCap = Paint.Cap.ROUND - ringPaint.color = Color.WHITE - canvas.drawPath(path, ringPaint) - ringPaint.strokeCap = Paint.Cap.BUTT - } - - override fun onTouchEvent(event: MotionEvent): Boolean = if (editing) editTouch(event) else playTouch(event) - - private fun playTouch(event: MotionEvent): Boolean { - if (event.getToolType(event.actionIndex) == MotionEvent.TOOL_TYPE_MOUSE) return false - when (event.actionMasked) { - MotionEvent.ACTION_DOWN, MotionEvent.ACTION_POINTER_DOWN -> { - val index = event.actionIndex - val entry = entryAt(event.getX(index), event.getY(index)) - if (entry == null) { - if (event.actionMasked == MotionEvent.ACTION_DOWN) return false - forwardedPointerBits = forwardedPointerBits or (1 shl event.getPointerId(index)) - forwardPointers(event, forwardedPointerBits) - return true - } - if (forwardedPointerBits != 0) forwardPointers(event, forwardedPointerBits) - if (entry.shape == SCREEN_KEY_DPAD) { - val arm = armOf(entry, event.getX(index), event.getY(index)) - val code = entry.codes.getOrNull(arm) ?: 0 - if (code == 0) return true - val pid = event.getPointerId(index) - dpadDown.remove(pid)?.let { releaseKey(it) } - dpadDown[pid] = code - entry.armed = arm - pressKey(code) - invalidate() - return true - } - if (entry.shape == SCREEN_KEY_STICK) { - val sector = stickSector(entry, event.getX(index), event.getY(index)) - val pid = event.getPointerId(index) - stickCodes.remove(pid)?.forEach { if (it != 0) releaseKey(it) } - val codes = directionCodes(entry, sector) - stickEntries[pid] = entry - stickCodes[pid] = codes - entry.armed = if (codes.isEmpty()) -1 else sector - codes.forEach { pressKey(it) } - invalidate() - return true - } - if (entry.toggle) { - if (latched.remove(entry)) release(entry) else { - latched += entry - entry.codes.forEach { pressKey(it) } - } - invalidate() - return true - } - if (down.values.any { it === entry }) return true - down[event.getPointerId(index)] = entry - entry.codes.forEach { pressKey(it) } - invalidate() - return true - } - - MotionEvent.ACTION_MOVE -> { - if (forwardedPointerBits != 0) forwardPointers(event, forwardedPointerBits) - if (stickEntries.isEmpty()) return true - repeat(event.pointerCount) { index -> - val pid = event.getPointerId(index) - val entry = stickEntries[pid] ?: return@repeat - val sector = stickSector(entry, event.getX(index), event.getY(index)) - val codes = directionCodes(entry, sector) - val previous = stickCodes[pid].orEmpty() - if (codes != previous) { - previous.filterNot { it in codes }.forEach { releaseKey(it) } - codes.filterNot { it in previous }.forEach { pressKey(it) } - stickCodes[pid] = codes - entry.armed = if (codes.isEmpty()) -1 else sector - invalidate() - } - } - return true - } - - MotionEvent.ACTION_UP, MotionEvent.ACTION_POINTER_UP -> { - val pid = event.getPointerId(event.actionIndex) - if (forwardedPointerBits and (1 shl pid) != 0) { - forwardPointers(event, forwardedPointerBits) - forwardedPointerBits = forwardedPointerBits and (1 shl pid).inv() - if (stickCodes.containsKey(pid) || dpadDown.containsKey(pid)) return true - down.remove(pid)?.let(::release) - return true - } - if (forwardedPointerBits != 0) forwardPointers(event, forwardedPointerBits) - if (stickCodes.containsKey(pid)) { - stickCodes.remove(pid)?.forEach { if (it != 0) releaseKey(it) } - stickEntries.remove(pid)?.armed = -1 - invalidate() - return true - } - val armedCode = dpadDown.remove(pid) - if (armedCode != null) { - releaseKey(armedCode) - entries.firstOrNull { it.armed >= 0 && it.codes.contains(armedCode) }?.armed = -1 - invalidate() - return true - } - val entry = down.remove(pid) - if (entry != null) release(entry) - return true - } - - MotionEvent.ACTION_CANCEL -> { - if (forwardedPointerBits != 0) forwardPointers(event, forwardedPointerBits) - forwardedPointerBits = 0 - stickCodes.values.flatten().forEach { if (it != 0) releaseKey(it) } - stickCodes.clear() - stickEntries.clear() - entries.forEach { it.armed = -1 } - dpadDown.values.forEach { releaseKey(it) } - dpadDown.clear() - entries.forEach { it.armed = -1 } - down.values.forEach { release(it) } - down.clear() - return true - } - } - return true - } - - private fun forwardPointers(event: MotionEvent, pointerIdBits: Int) { - val split = event.pointerSubset(pointerIdBits) ?: return - try { - onTouchMiss?.invoke(split) - } finally { - split.recycle() - } - } - - private fun editTouch(event: MotionEvent): Boolean { - when (event.actionMasked) { - MotionEvent.ACTION_DOWN -> { - val x = event.x - val y = event.y - if (inCircle(addCenter, x, y, capsuleRadius)) { - onAdd?.invoke() - return true - } - if (inCircle(doneCenter, x, y, capsuleRadius)) { - stopEditing() - return true - } - if (inToolbar(x, y)) { - toolbarDrag = true - dragX = toolbar.x - x - dragY = toolbar.y - y - return true - } - val entry = entryAt(x, y) - if (entry == null) { - pressX = x - pressY = y - postDelayed(addPress, 520) - selected = null - invalidate() - return true - } - selected = entry - dragging = true - moved = false - dragX = entry.cx - x - dragY = entry.cy - y - invalidate() - return true - } - - MotionEvent.ACTION_MOVE -> { - if (toolbarDrag) { - val maxX = width - safe.right - capsuleGap / 2f - capsuleRadius - val maxY = height - safe.bottom - capsuleRadius - toolbar.x = (event.x + dragX).coerceIn(safe.left + capsuleGap / 2f + capsuleRadius, maxOf(safe.left + capsuleGap / 2f + capsuleRadius, maxX)) - toolbar.y = (event.y + dragY).coerceIn(safe.top + capsuleRadius, maxOf(safe.top + capsuleRadius, maxY)) - addCenter.set(toolbar.x - capsuleGap / 2f, toolbar.y) - doneCenter.set(toolbar.x + capsuleGap / 2f, toolbar.y) - invalidate() - return true - } - val entry = selected - if (entry == null) { - if (hypot(event.x - pressX, event.y - pressY) > dp(10f)) removeCallbacks(addPress) - return true - } - if (!dragging) return true - if (hypot(event.x - (entry.cx - dragX), event.y - (entry.cy - dragY)) > dp(10f)) removeCallbacks(addPress) - var nextX = snap((event.x + dragX).coerceIn(entry.halfW + safe.left, width - entry.halfW - safe.right)) - var nextY = snap((event.y + dragY).coerceIn(entry.halfH + safe.top, height - entry.halfH - safe.bottom)) - guideX = -1f - guideY = -1f - entries.forEach { other -> - if (other === entry) return@forEach - if (abs(nextX - other.cx) < dp(8f)) { nextX = other.cx; guideX = other.cx } - if (abs(nextY - other.cy) < dp(8f)) { nextY = other.cy; guideY = other.cy } - } - if (abs(nextX - width / 2f) < dp(8f)) { nextX = width / 2f; guideX = nextX } - if (abs(nextY - height / 2f) < dp(8f)) { nextY = height / 2f; guideY = nextY } - if (hypot(nextX - entry.cx, nextY - entry.cy) > dp(4f)) moved = true - entry.cx = nextX - entry.cy = nextY - entry.x = entry.cx / width - entry.y = entry.cy / height - invalidate() - return true - } - - MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> { - removeCallbacks(addPress) - guideX = -1f - guideY = -1f - if (toolbarDrag) { - toolbarDrag = false - return true - } - val entry = selected - if (entry != null && dragging) { - dragging = false - if (moved) { - save() - invalidate() - } else { - onSelect?.invoke(entry.codes) - } - } - return true - } - } - return true - } - - private fun pressKey(code: Int) { - val owners = held[code] ?: 0 - held[code] = owners + 1 - if (owners == 0) sendKey(code, true) - } - - private fun releaseKey(code: Int) { - val owners = held[code] ?: return - if (owners > 1) { - held[code] = owners - 1 - return - } - held.remove(code) - sendKey(code, false) - } - - private fun release(entry: Entry) { - entry.codes.asReversed().forEach { releaseKey(it) } - invalidate() - } - - fun export(): String = ControllerPrefs.encodeScreenKeys( - entries.map { ScreenKey(it.codes, it.x, it.y, it.size, it.glyph, it.text, it.image, it.opacity, it.shape, it.toggle, it.labels) }, - ) - - fun load(raw: String) { - releaseAll() - entries.clear() - ControllerPrefs.decodeScreenKeys(raw).forEach { entries += entryOf(it) } - entries.forEach { measure(it) } - selected = null - save() - invalidate() - } - - private fun save() { - ControllerPrefs.setScreenKeys( - context, - entries.map { ScreenKey(it.codes, it.x, it.y, it.size, it.glyph, it.text, it.image, it.opacity, it.shape, it.toggle, it.labels) }, - ) - } - - private fun dp(value: Float) = value * resources.displayMetrics.density - - companion object { - private val DPAD_DIRS = listOf( - 0f to -1f, 1f to 0f, 0f to 1f, -1f to 0f, - ) - private val STICK_DIRS = listOf( - 0f to -1f, 0.7071f to -0.7071f, 1f to 0f, 0.7071f to 0.7071f, - 0f to 1f, -0.7071f to 0.7071f, -1f to 0f, -0.7071f to -0.7071f, - ) - const val DEFAULT_SIZE = 44 - const val MIN_SIZE = 24 - const val MAX_SIZE = 140 - const val IMAGE_DIR = "screenkeys" - private const val BODY = 0xFF14181F.toInt() - } -} diff --git a/app/src/main/java/com/droiddeck/launcher/input/ScreenLayoutFile.kt b/app/src/main/java/com/droiddeck/launcher/input/ScreenLayoutFile.kt deleted file mode 100644 index fc25940719..0000000000 --- a/app/src/main/java/com/droiddeck/launcher/input/ScreenLayoutFile.kt +++ /dev/null @@ -1,140 +0,0 @@ -package com.droiddeck.launcher.input - -import org.json.JSONArray -import org.json.JSONObject -import kotlin.math.roundToInt - -private val ICP_NAMES: Map = mapOf( - 103 to "KEY_UP", 106 to "KEY_RIGHT", 108 to "KEY_DOWN", 105 to "KEY_LEFT", - 28 to "KEY_ENTER", 1 to "KEY_ESC", 14 to "KEY_BKSP", 111 to "KEY_DEL", 15 to "KEY_TAB", - 57 to "KEY_SPACE", 29 to "KEY_CTRL_L", 97 to "KEY_CTRL_R", 110 to "KEY_INSERT", - 42 to "KEY_SHIFT_L", 54 to "KEY_SHIFT_R", 56 to "KEY_ALT_L", 100 to "KEY_ALT_R", - 102 to "KEY_HOME", 107 to "KEY_END", 99 to "KEY_PRTSCN", 104 to "KEY_PG_UP", 109 to "KEY_PG_DOWN", - 58 to "KEY_CAPS_LOCK", 69 to "KEY_NUM_LOCK", 41 to "KEY_GRAVE", 12 to "KEY_MINUS", - 11 to "KEY_0", 2 to "KEY_1", 3 to "KEY_2", 4 to "KEY_3", 5 to "KEY_4", 6 to "KEY_5", - 7 to "KEY_6", 8 to "KEY_7", 9 to "KEY_8", 10 to "KEY_9", - 30 to "KEY_A", 48 to "KEY_B", 46 to "KEY_C", 32 to "KEY_D", 18 to "KEY_E", 33 to "KEY_F", - 34 to "KEY_G", 35 to "KEY_H", 23 to "KEY_I", 36 to "KEY_J", 37 to "KEY_K", 38 to "KEY_L", - 50 to "KEY_M", 49 to "KEY_N", 24 to "KEY_O", 25 to "KEY_P", 16 to "KEY_Q", 19 to "KEY_R", - 31 to "KEY_S", 20 to "KEY_T", 22 to "KEY_U", 47 to "KEY_V", 17 to "KEY_W", 45 to "KEY_X", - 21 to "KEY_Y", 44 to "KEY_Z", - 26 to "KEY_BRACKET_LEFT", 27 to "KEY_BRACKET_RIGHT", 43 to "KEY_BACKSLASH", 53 to "KEY_SLASH", - 39 to "KEY_SEMICOLON", 51 to "KEY_COMMA", 52 to "KEY_PERIOD", 40 to "KEY_APOSTROPHE", - 59 to "KEY_F1", 60 to "KEY_F2", 61 to "KEY_F3", 62 to "KEY_F4", 63 to "KEY_F5", 64 to "KEY_F6", - 65 to "KEY_F7", 66 to "KEY_F8", 67 to "KEY_F9", 68 to "KEY_F10", 87 to "KEY_F11", 88 to "KEY_F12", - 79 to "KEY_KP_1", 80 to "KEY_KP_2", 81 to "KEY_KP_3", 75 to "KEY_KP_4", 76 to "KEY_KP_5", - 77 to "KEY_KP_6", 71 to "KEY_KP_7", 72 to "KEY_KP_8", 73 to "KEY_KP_9", 82 to "KEY_KP_0", -) - -private val ICP_CODES: Map = ICP_NAMES.entries.associate { (code, name) -> name to code } - -private fun icpShape(shape: Int): String = when (shape) { - SCREEN_KEY_CIRCLE -> "CIRCLE" - SCREEN_KEY_RECT -> "RECT" - SCREEN_KEY_ROUND -> "ROUND_RECT" - SCREEN_KEY_DPAD -> "ROUND_RECT" - SCREEN_KEY_STICK -> "ROUND_RECT" - SCREEN_KEY_TRIGGER -> "ROUND_RECT" - else -> "ROUND_RECT" -} - -private fun screenKeyShape(shape: String): Int = when (shape) { - "CIRCLE" -> SCREEN_KEY_CIRCLE - "RECT" -> SCREEN_KEY_RECT - "SQUARE" -> SCREEN_KEY_ROUND - else -> SCREEN_KEY_PILL -} - -fun exportScreenLayout(keys: List, widthPx: Int, density: Float): String { - val snap = widthPx / 100f - val elements = JSONArray() - keys.forEach { key -> - val binds = JSONArray() - val directional = key.shape == SCREEN_KEY_DPAD || key.shape == SCREEN_KEY_STICK - key.codes.forEach { code -> - val name = ICP_NAMES[code] - if (name != null) binds.put(name) else if (directional) binds.put("") - } - if (binds.length() == 0) return@forEach - val shape = icpShape(key.shape) - val sizePx = key.size * density - val scale = when { - key.shape == SCREEN_KEY_DPAD -> sizePx * 2.3f / (14f * snap) - key.shape == SCREEN_KEY_STICK -> sizePx * 2.3f / (14f * snap) - shape == "CIRCLE" -> sizePx / (6f * snap) - else -> sizePx / (4f * snap) - } - elements.put( - JSONObject() - .put( - "type", - when (key.shape) { - SCREEN_KEY_DPAD -> "D_PAD" - SCREEN_KEY_STICK -> "STICK" - else -> "BUTTON" - }, - ) - .put("shape", shape) - .put("customColor", 0) - .put("bindings", binds) - .put("scale", scale) - .put("x", key.x) - .put("y", key.y) - .put("toggleSwitch", key.toggle) - .put("swipeable", false) - .put("text", key.text) - .put("iconId", 0) - .apply { if (key.opacity < 1f) put("opacity", key.opacity) }, - ) - } - return JSONObject() - .put("id", 0) - .put("name", "DroidDeck") - .put("cursorSpeed", 1.5f) - .put("elements", elements) - .toString() -} - -fun importScreenLayout(raw: String, widthPx: Int, density: Float): List? { - val root = runCatching { JSONObject(raw) }.getOrNull() ?: return null - val elements = root.optJSONArray("elements") ?: return null - val snap = widthPx / 100f - val keys = mutableListOf() - for (i in 0 until elements.length()) { - val element = elements.optJSONObject(i) ?: continue - val type = element.optString("type", "BUTTON") - if (type != "BUTTON" && type != "D_PAD" && type != "STICK") continue - val binds = element.optJSONArray("bindings") ?: continue - val raw = (0 until binds.length()).map { ICP_CODES[binds.optString(it)] ?: 0 } - val codes = if (type == "D_PAD" || type == "STICK") { - raw.dropLastWhile { it == 0 }.ifEmpty { listOf(0) } - } else { - raw.filter { it != 0 } - } - if (codes.isEmpty()) continue - val dpad = type == "D_PAD" - val stick = type == "STICK" - val shape = when { - dpad -> SCREEN_KEY_DPAD - stick -> SCREEN_KEY_STICK - else -> screenKeyShape(element.optString("shape")) - } - val scale = element.optDouble("scale", 1.0).toFloat().takeIf { it > 0f } ?: 1f - val sizePx = when { - dpad || stick -> 14f * snap * scale / 2.3f - shape == SCREEN_KEY_CIRCLE -> 6f * snap * scale - else -> 4f * snap * scale - } - keys += ScreenKey( - codes = codes, - x = element.optDouble("x", 0.5).toFloat().coerceIn(0f, 1f), - y = element.optDouble("y", 0.5).toFloat().coerceIn(0f, 1f), - size = (sizePx / density).roundToInt().coerceIn(ScreenKeys.MIN_SIZE, ScreenKeys.MAX_SIZE), - text = element.optString("text").take(24), - opacity = element.optDouble("opacity", 1.0).toFloat().coerceIn(0.2f, 1f), - shape = shape, - toggle = element.optBoolean("toggleSwitch"), - ) - } - return keys -} diff --git a/app/src/main/java/com/droiddeck/launcher/input/SecondScreenMode.kt b/app/src/main/java/com/droiddeck/launcher/input/SecondScreenMode.kt index 35400d9553..2d0006925f 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/SecondScreenMode.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/SecondScreenMode.kt @@ -1,26 +1,34 @@ package com.droiddeck.launcher.input +import android.content.Context import android.hardware.display.DisplayManager import android.view.Display import androidx.annotation.StringRes import com.droiddeck.launcher.R +import com.droiddeck.launcher.session.SessionPrefs -enum class SecondScreenMode(val id: String, @StringRes val label: Int) { +enum class SecondScreenMode(val id: String, @StringRes val label: Int, val bindings: String? = null) { NONE("none", R.string.second_mode_none), - KEYBOARD_TRACKPAD("keyboard-trackpad", R.string.second_mode_keyboard_trackpad), + KEYBOARD_TRACKPAD("keyboard-trackpad", R.string.second_mode_keyboard_trackpad, InputStockLayouts.TRACKPAD), TERMINAL("terminal", R.string.second_mode_terminal), - DECK_CONTROLS("deck-controls", R.string.second_mode_deck_controls), + DECK_CONTROLS("deck-controls", R.string.second_mode_deck_controls, InputStockLayouts.DECK), + CUSTOM_CONTROLS("custom-controls", R.string.input_profile_controls_mode), DASHBOARD("dashboard", R.string.second_mode_dashboard), DESKTOP("desktop", R.string.second_mode_desktop); + val controls: Boolean get() = this == CUSTOM_CONTROLS || bindings != null + + fun panel(context: Context): String? = if (this == DECK_CONTROLS) SessionPrefs.deckPanel(context).bindings else bindings + companion object { - fun opening(remembered: String?, deckPad: Boolean, dashboard: Boolean = false, desktop: Boolean = false): SecondScreenMode { + fun opening(remembered: String?, deckPad: Boolean, dashboard: Boolean = false, bottomDesktop: Boolean = false, desktopSession: Boolean = false): SecondScreenMode { entries.firstOrNull { it != NONE && it.id == remembered && (it != DECK_CONTROLS || deckPad) && (it != DASHBOARD || dashboard) && - (it != DESKTOP || desktop) + (it != DESKTOP || bottomDesktop) }?.let { return it } val picked = entries.any { it != NONE && it.id == remembered } return when { + desktopSession -> KEYBOARD_TRACKPAD deckPad -> DECK_CONTROLS dashboard && !picked -> DASHBOARD else -> KEYBOARD_TRACKPAD @@ -44,4 +52,7 @@ object SecondScreenDisplays { ) } .toList() + + fun editorCandidates(selected: Int, available: List, taken: (Int) -> Boolean): List = + (listOf(selected) + available).distinct().filterNot(taken) } diff --git a/app/src/main/java/com/droiddeck/launcher/input/SteamControllerBle.kt b/app/src/main/java/com/droiddeck/launcher/input/SteamControllerBle.kt index e77b322f2b..5b0b69c8f0 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/SteamControllerBle.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/SteamControllerBle.kt @@ -20,7 +20,11 @@ import java.util.ArrayDeque import java.util.UUID @SuppressLint("MissingPermission") -class SteamControllerBle(private val context: Context) { +class SteamControllerBle(private val context: Context, router: InputRouter? = null) { + private val inputLock = router ?: Any() + private var routedInput = router?.let(::TritonInputRouter) + @Volatile private var stopped = false + private var started = false private val thread = HandlerThread("steam-controller-ble").apply { start() } private val handler = Handler(thread.looper) private val outputReports = mutableMapOf() @@ -36,22 +40,54 @@ class SteamControllerBle(private val context: Context) { private var outputWritePending = false private var featureRead = 0L - fun start() = handler.post { - if (running) return@post - running = true - Log.i(TAG, "looking for a paired Steam Controller") - connect() + fun start(): Unit = synchronized(inputLock) { + if (stopped || started) return@synchronized + started = true + synchronized(slotLock) { + slotOwner = this + connected = false + FakeInputWriter.writeTritonState(SLOT, 0x45, ByteArray(STATE_BYTES)) + FakeInputWriter.setTritonConnected(SLOT, false) + } + handler.post { + if (stopped || slotOwner !== this || running) return@post + running = true + Log.i(TAG, "looking for a paired Steam Controller") + connect() + } } - fun stop() = handler.post { - running = false - handler.removeCallbacksAndMessages(null) - disconnect("session ended") - thread.quitSafely() + private inline fun withSlot(block: () -> Unit) = synchronized(inputLock) { + synchronized(slotLock) { + if (!stopped && slotOwner === this) block() + } + } + + fun stop() { + synchronized(inputLock) { + if (stopped) return + stopped = true + routedInput?.close() + routedInput = null + synchronized(slotLock) { + if (slotOwner === this) { + FakeInputWriter.writeTritonState(SLOT, if (reportId == 0x47) 0x47 else 0x45, ByteArray(STATE_BYTES)) + connected = false + FakeInputWriter.setTritonConnected(SLOT, false) + slotOwner = null + } + } + } + handler.post { + running = false + handler.removeCallbacksAndMessages(null) + disconnect("session ended") + thread.quitSafely() + } } private fun connect() { - if (!running || gatt != null) return + if (stopped || slotOwner !== this || !running || gatt != null) return if (SessionState.suspended) { handler.postDelayed(::connect, RETRY_MS) return @@ -84,11 +120,17 @@ class SteamControllerBle(private val context: Context) { featureRead = 0L queuedWrites.clear() outputReports.clear() - connected = false - FakeInputWriter.setTritonConnected(SLOT, false) + synchronized(inputLock) { + if (!stopped) { + release() + withSlot { + connected = false + FakeInputWriter.setTritonConnected(SLOT, false) + } + } + } g.disconnect() g.close() - release() Log.i(TAG, "disconnected: $why") listener?.invoke() } @@ -119,10 +161,12 @@ class SteamControllerBle(private val context: Context) { } override fun onDescriptorWrite(g: BluetoothGatt, descriptor: BluetoothGattDescriptor, status: Int) { - if (g !== gatt || descriptor.characteristic.uuid != input) return - Log.i(TAG, "subscribed to report 0x%02x (status %d)".format(reportId, status)) - connected = status == BluetoothGatt.GATT_SUCCESS - FakeInputWriter.setTritonConnected(SLOT, connected) + withSlot { + if (g !== gatt || descriptor.characteristic.uuid != input) return + Log.i(TAG, "subscribed to report 0x%02x (status %d)".format(reportId, status)) + connected = status == BluetoothGatt.GATT_SUCCESS + FakeInputWriter.setTritonConnected(SLOT, connected) + } listener?.invoke() if (connected) { refreshLizardMode() @@ -164,6 +208,7 @@ class SteamControllerBle(private val context: Context) { } private fun subscribe(g: BluetoothGatt) { + if (stopped || slotOwner !== this) return val service = g.getService(SERVICE) if (service == null) { Log.w(TAG, "no Valve service on \"${g.device.name}\" (attempt ${discoveries + 1}); services: " + @@ -207,25 +252,31 @@ class SteamControllerBle(private val context: Context) { .onFailure { Log.w(TAG, "could not refresh the service cache", it) } } - private fun refreshLizardMode() { - if (!connected) return + private fun refreshLizardMode() = withSlot { + if (!connected) return@withSlot val msg = ByteArray(FEATURE_BYTES) msg[0] = 0x87.toByte(); msg[1] = 3; msg[2] = 9 enqueueOutput(REPORT, msg) handler.postDelayed(::refreshLizardMode, LIZARD_REFRESH_MS) } - private fun onState(data: ByteArray) { - if (data.size < STATE_BYTES) return - FakeInputWriter.writeTritonState(SLOT, reportId, data) + private fun onState(data: ByteArray) = withSlot { + if (data.size < STATE_BYTES) return@withSlot + val timing = InputTiming.begin() + FakeInputWriter.writeTritonState(SLOT, reportId, routedInput?.route(data) ?: data) + InputTiming.end(timing) } - private fun release() { - FakeInputWriter.writeTritonState(SLOT, if (reportId == 0x47) 0x47 else 0x45, ByteArray(STATE_BYTES)) + private fun release() = synchronized(inputLock) { + if (stopped) return@synchronized + routedInput?.release() + withSlot { + FakeInputWriter.writeTritonState(SLOT, if (reportId == 0x47) 0x47 else 0x45, ByteArray(STATE_BYTES)) + } } - private fun pollOutput() { - if (!running || !connected || gatt == null) return + private fun pollOutput() = withSlot { + if (!running || !connected || gatt == null) return@withSlot FakeInputWriter.readTritonOutput(SLOT, outputSequence)?.let { output -> outputSequence = ByteBuffer.wrap(output, 0, 8).order(ByteOrder.LITTLE_ENDIAN).long sendOutputReport(output[8] != 0.toByte(), output.copyOfRange(9, output.size)) @@ -239,7 +290,7 @@ class SteamControllerBle(private val context: Context) { handler.postDelayed(::pollOutput, OUTPUT_POLL_MS) } - private fun onFeatureRead(value: ByteArray?) { + private fun onFeatureRead(value: ByteArray?) = withSlot { outputWritePending = false if (featureRead != 0L) { if (value == null) Log.w(TAG, "feature read failed") @@ -267,14 +318,14 @@ class SteamControllerBle(private val context: Context) { writeNextOutput() } - private fun writeNextOutput() { - if (outputWritePending) return - val g = gatt ?: return - val (uuid, value) = queuedWrites.pollFirst() ?: return + private fun writeNextOutput() = withSlot { + if (outputWritePending) return@withSlot + val g = gatt ?: return@withSlot + val (uuid, value) = queuedWrites.pollFirst() ?: return@withSlot val characteristic = g.getService(SERVICE)?.getCharacteristic(uuid) if (characteristic == null) { Log.w(TAG, "BLE output characteristic $uuid disappeared") - return + return@withSlot } if (value == null) { if (g.readCharacteristic(characteristic)) { @@ -283,7 +334,7 @@ class SteamControllerBle(private val context: Context) { queuedWrites.addFirst(uuid to null) handler.postDelayed(::writeNextOutput, OUTPUT_RETRY_MS) } - return + return@withSlot } @Suppress("DEPRECATION") characteristic.writeType = BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT @@ -299,6 +350,8 @@ class SteamControllerBle(private val context: Context) { } companion object { + private val slotLock = Any() + @Volatile private var slotOwner: SteamControllerBle? = null private const val TAG = "SteamControllerBle" private const val SLOT = 0 private const val RETRY_MS = 5_000L diff --git a/app/src/main/java/com/droiddeck/launcher/input/TouchGestureConfig.kt b/app/src/main/java/com/droiddeck/launcher/input/TouchGestureConfig.kt index d3be470568..035060585f 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/TouchGestureConfig.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/TouchGestureConfig.kt @@ -213,6 +213,9 @@ data class TouchGestureConfig( return if (value == MOUSE_BEHAVIOR_CLICK) MOUSE_BEHAVIOR_CLICK else MOUSE_BEHAVIOR_HOLD } + fun gamescopeRelativeMouse(mode: Int, mouseAttached: Boolean, touchpad: Boolean): Int = + if (mode == RELATIVE_MOUSE_AUTO && !mouseAttached && !touchpad) RELATIVE_MOUSE_OFF else mode + private fun normalizeRelativeMouseMode(mode: Int): Int { return if (mode in RELATIVE_MOUSE_AUTO..RELATIVE_MOUSE_OFF) mode else RELATIVE_MOUSE_AUTO } diff --git a/app/src/main/java/com/droiddeck/launcher/input/TritonInputRouter.kt b/app/src/main/java/com/droiddeck/launcher/input/TritonInputRouter.kt new file mode 100644 index 0000000000..c85b4ffa94 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/input/TritonInputRouter.kt @@ -0,0 +1,93 @@ +package com.droiddeck.launcher.input + +import kotlin.math.roundToInt + +class TritonInputRouter(private val router: InputRouter) { + private val channels = InputControl.entries.map { router.channel(InputRole.PHYSICAL, it) }.toTypedArray() + private var last: ByteArray? = null + private var buffer = ByteArray(45) + private val snapshot: () -> Unit = { last?.let { read(it, false) } } + + init { router.beforeProfileChange(snapshot) } + + fun route(data: ByteArray): ByteArray = synchronized(router) { + if (data.size < 45) return@synchronized data + last = data + if (channels.all { it.nativeIdentity() }) return@synchronized data + if (buffer.size != data.size) buffer = ByteArray(data.size) + data.copyInto(buffer) + putBits(buffer, bits(data) and CONTROL_BITS.inv()) + for (offset in 5..27 step 2) putShort(buffer, offset, 0) + read(data, true) + buffer + } + + private fun read(data: ByteArray, emit: Boolean) { + val bits = bits(data) + for (control in InputControl.entries) { + val channel = channels[control.ordinal] + val offset = offset(control) + val pair = control.dimensions == 2 + val x = if (offset != 0) axis(data, offset) else if (bits and bit(control) != 0) 1f else 0f + val y = if (pair) axis(data, offset + 2) * if (control.isTrackpad) 1f else -1f else 0f + val contact = control.isTrackpad && bits and bit(control) != 0 + if (!emit) { channel.observe(x, y, contact); continue } + val target = channel.update(x, y, contact) ?: continue + val targetOffset = offset(target) + if (targetOffset != 0) { + val value = shortValue(x) + if (target.dimensions == 2) { + val second = shortValue(y * if (target.isTrackpad) 1f else -1f) + val oldX = short(buffer, targetOffset).toLong() + val oldY = short(buffer, targetOffset + 2).toLong() + if (value.toLong() * value + second.toLong() * second >= oldX * oldX + oldY * oldY) { + putShort(buffer, targetOffset, value) + putShort(buffer, targetOffset + 2, second) + } + val touch = if (control.isTrackpad) contact else bits and bit(control) != 0 + if (touch) putBits(buffer, bits(buffer) or bit(target)) + } else { + if (value >= short(buffer, targetOffset)) putShort(buffer, targetOffset, value) + if (bits and bit(control) != 0) putBits(buffer, bits(buffer) or bit(target)) + } + } else if (x > 0.5f) putBits(buffer, bits(buffer) or bit(target)) + if (target == InputControl.LEFT_CLICK || target == InputControl.RIGHT_CLICK) { + val pressureOffset = if (target == InputControl.LEFT_CLICK) 21 else 27 + val from = if (control == InputControl.LEFT_CLICK) 21 else if (control == InputControl.RIGHT_CLICK) 27 else 0 + if (from != 0) putShort(buffer, pressureOffset, short(data, from)) + else if (x > 0.5f) putShort(buffer, pressureOffset, 32767) + } + } + } + + fun release() = synchronized(router) { channels.forEach { it.release() }; last = null } + fun close() = synchronized(router) { release(); channels.forEach { it.close() }; router.removeSnapshot(snapshot) } + + private fun bits(data: ByteArray): Int = (data[1].toInt() and 255) or ((data[2].toInt() and 255) shl 8) or + ((data[3].toInt() and 255) shl 16) or ((data[4].toInt() and 255) shl 24) + private fun putBits(data: ByteArray, value: Int) { for (i in 0..3) data[i + 1] = (value ushr (i * 8)).toByte() } + private fun short(data: ByteArray, offset: Int) = ((data[offset].toInt() and 255) or (data[offset + 1].toInt() shl 8)).toShort().toInt() + private fun axis(data: ByteArray, offset: Int): Float = short(data, offset).let { it / if (it < 0) 32768f else 32767f } + private fun shortValue(value: Float) = (value * if (value < 0f) 32768f else 32767f).roundToInt().coerceIn(-32768, 32767) + private fun putShort(data: ByteArray, offset: Int, value: Int) { data[offset] = value.toByte(); data[offset + 1] = (value shr 8).toByte() } + + companion object { + private const val CONTROL_BITS = 0x0fffffff + private fun bit(c: InputControl): Int = when (c) { + InputControl.A -> 0x1; InputControl.B -> 0x2; InputControl.X -> 0x4; InputControl.Y -> 0x8 + InputControl.QAM -> 0x10; InputControl.R3 -> 0x20; InputControl.START -> 0x40 + InputControl.R4 -> 0x80; InputControl.R5 -> 0x100; InputControl.RB -> 0x200 + InputControl.DOWN -> 0x400; InputControl.RIGHT -> 0x800; InputControl.LEFT -> 0x1000; InputControl.UP -> 0x2000 + InputControl.SELECT -> 0x4000; InputControl.L3 -> 0x8000; InputControl.GUIDE -> 0x10000 + InputControl.L4 -> 0x20000; InputControl.L5 -> 0x40000; InputControl.LB -> 0x80000 + InputControl.RS -> 0x100000; InputControl.RIGHT_PAD -> 0x200000; InputControl.RIGHT_CLICK -> 0x400000 + InputControl.RT -> 0x800000; InputControl.LS -> 0x1000000; InputControl.LEFT_PAD -> 0x2000000 + InputControl.LEFT_CLICK -> 0x4000000; InputControl.LT -> 0x8000000 + } + private fun offset(c: InputControl): Int = when (c) { + InputControl.LT -> 5; InputControl.RT -> 7; InputControl.LS -> 9; InputControl.RS -> 13 + InputControl.LEFT_PAD -> 17; InputControl.RIGHT_PAD -> 23 + else -> 0 + } + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/lab/LabNotice.kt b/app/src/main/java/com/droiddeck/launcher/lab/LabNotice.kt new file mode 100644 index 0000000000..85e1bab990 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/lab/LabNotice.kt @@ -0,0 +1,60 @@ +package com.droiddeck.launcher.lab + +import android.app.Notification +import android.app.NotificationChannel +import android.app.NotificationManager +import android.app.PendingIntent +import android.content.Context +import android.content.Intent +import com.droiddeck.launcher.FrontEndActivity +import com.droiddeck.launcher.R +import com.droiddeck.launcher.stores.download.StoreDownloadService + +/** + * Says what happened to a Lab build when the app starts, so a restart onto a new build does not + * look like a crash, and a build Android or the safety checks turned off is not lost silently. + */ +object LabNotice { + private const val CHANNEL_ID = "lab" + private const val NOTIFICATION_ID = 4120 + + /** Where a start that just moved a Lab build into use opens; the agent that built it sets it. */ + @Volatile var openOnInstall: String = "updates" + + fun started(context: Context) { + val app = context.applicationContext + val active = LabOverlay.active + val skipped = LabOverlay.skipped + val current = LabOverlay.current(app) + when { + LabOverlay.promoted && active != null -> + post(app, app.getString(R.string.lab_installed_title), app.getString(R.string.lab_installed_text, active.label), openOnInstall) + skipped == LabOverlay.SKIPPED_APP_IMAGE && current != null -> + post(app, app.getString(R.string.lab_paused_title), app.getString(R.string.lab_paused_text, current.label), "updates") + skipped == LabOverlay.Reason.OTHER_INSTALL.name || skipped == LabOverlay.Reason.DID_NOT_START.name -> { + val reason = if (skipped == LabOverlay.Reason.OTHER_INSTALL.name) R.string.lab_off_other_install else R.string.lab_off_did_not_start + post(app, app.getString(R.string.lab_off_title), app.getString(reason), "updates") + } + } + } + + private fun post(app: Context, title: String, text: String, nav: String) { + val manager = app.getSystemService(NotificationManager::class.java) ?: return + manager.createNotificationChannel(NotificationChannel(CHANNEL_ID, app.getString(R.string.upd_lab), NotificationManager.IMPORTANCE_DEFAULT)) + val open = PendingIntent.getActivity( + app, NOTIFICATION_ID, + Intent(app, FrontEndActivity::class.java).putExtra(StoreDownloadService.EXTRA_NAV, nav) + .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_REORDER_TO_FRONT), + PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT, + ) + val notification = Notification.Builder(app, CHANNEL_ID) + .setSmallIcon(R.drawable.ic_stat_session) + .setContentTitle(title) + .setContentText(text) + .setStyle(Notification.BigTextStyle().bigText(text)) + .setContentIntent(open) + .setAutoCancel(true) + .build() + runCatching { manager.notify(NOTIFICATION_ID, notification) } + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/lab/LabOverlay.kt b/app/src/main/java/com/droiddeck/launcher/lab/LabOverlay.kt new file mode 100644 index 0000000000..7fb7f7a26d --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/lab/LabOverlay.kt @@ -0,0 +1,276 @@ +package com.droiddeck.launcher.lab + +import android.app.Activity +import android.app.Application +import android.content.Context +import android.content.Intent +import android.content.res.Resources +import android.content.res.loader.ResourcesLoader +import android.content.res.loader.ResourcesProvider +import android.os.Build +import android.os.Bundle +import android.os.Handler +import android.os.Looper +import android.os.ParcelFileDescriptor +import android.util.Log +import androidx.annotation.RequiresApi +import androidx.annotation.VisibleForTesting +import com.droiddeck.launcher.BuildConfig +import org.json.JSONObject +import java.io.File + +/** + * A Lab build: app code and resources an on-device agent built from this install's own commit, + * loaded over the installed APK at startup so it can be tried without reinstalling or re-signing. + * + * `files/lab/next` holds a build waiting for the next start and `files/lab/active` the one in use, + * each with `overlay.apk` (the build's dex files and linked resources) and `overlay.json`. A build + * that is replaced, turned off or refused moves to `files/lab/builds`, so it can be used again. + */ +object LabOverlay { + data class Info(val label: String, val baseCommit: String, val baseApk: String, val built: Long = 0) + + enum class Reason { REPLACED, TURNED_OFF, OTHER_INSTALL, DID_NOT_START, UNREADABLE } + + /** A build kept in `files/lab/builds`; [usable] when it was made for this install. */ + data class Saved(val dir: File, val info: Info?, val reason: Reason?, val usable: Boolean) + + private const val TAG = "LabOverlay" + private const val MAX_FAILED_STARTS = 2 + private const val STABLE_AFTER_MS = 10_000L + private const val KEEP_BUILDS = 12 + const val SKIPPED_APP_IMAGE = "app image present" + + /** The installed APK's commit; this class always comes from the APK, never from a Lab build. */ + val apkCommit: String = BuildConfig.SOURCE_COMMIT + + @Volatile var active: Info? = null + private set + @Volatile var skipped: String? = null + private set + /** This start moved a newly staged build into use. */ + @Volatile var promoted = false + private set + + private var loader: Any? = null + + @VisibleForTesting + internal var injectDex: (ClassLoader, File) -> Unit = ::prependDex + @VisibleForTesting + internal var openResources: (File) -> Any? = { if (Build.VERSION.SDK_INT >= 30) createLoader(it) else null } + @VisibleForTesting + internal var appImage: (Context) -> Boolean = ::hasAppImage + + fun root(context: Context) = File(context.filesDir, "lab") + + /** Called from [Application.attachBaseContext], before any class the build may change is loaded. */ + fun load(app: Application, mainProcess: Boolean) { + active = null + skipped = null + promoted = false + try { + val root = root(app) + if (mainProcess) promoted = promote(root) + val dir = File(root, "active") + val apk = File(dir, "overlay.apk") + val meta = File(dir, "overlay.json") + if (!apk.isFile || !meta.isFile) return + val info = parse(meta.readText()) ?: return refuse(root, mainProcess, Reason.UNREADABLE) + if (!madeFor(app, info)) return refuse(root, mainProcess, Reason.OTHER_INSTALL) + if (Build.VERSION.SDK_INT < 30) return skip("needs Android 11") + // Classes in an app image are loaded before attachBaseContext and would ignore the overlay; + // dd-lab-build recompiles the app without one (cmd package compile -m verify). + if (appImage(app)) return skip(SKIPPED_APP_IMAGE) + if (mainProcess) { + val starts = failedStarts(root) + 1 + if (starts > MAX_FAILED_STARTS) return refuse(root, true, Reason.DID_NOT_START) + File(root, "starts").writeText(starts.toString()) + } + apk.setReadOnly() + loader = openResources(apk) + injectDex(app.classLoader, apk) + active = info + addLoader(app.resources) + app.registerActivityLifecycleCallbacks(Callbacks(root, mainProcess)) + } catch (e: Throwable) { + Log.w(TAG, "Lab build not loaded", e) + skipped = e.message ?: e.javaClass.simpleName + } + } + + /** The APK's path changes with every install and update of the app, so it pins a build to one install. */ + fun madeFor(context: Context, info: Info) = info.baseCommit == apkCommit && info.baseApk == context.applicationInfo.sourceDir + + /** The build in `active`, whether or not this start loaded it. */ + fun current(context: Context): Info? = read(File(root(context), "active")) + + /** A build staged for the next start. */ + fun pending(context: Context): Info? = read(File(root(context), "next")) + + /** Kept builds, newest first. */ + fun saved(context: Context): List = File(root(context), "builds").listFiles().orEmpty() + .filter { File(it, "overlay.apk").isFile } + .sortedByDescending { it.name.toLongOrNull() ?: 0L } + .map { dir -> + val info = read(dir) + val reason = File(dir, "reason").takeIf { it.isFile }?.readText()?.trim()?.let { r -> Reason.entries.firstOrNull { it.name == r } } + Saved(dir, info, reason, info != null && madeFor(context, info)) + } + + /** Leaves the Lab build and restarts the app on the installed APK. */ + fun turnOff(context: Context) { + val root = root(context) + archive(root, File(root, "active"), Reason.TURNED_OFF) + archive(root, File(root, "next"), Reason.TURNED_OFF) + File(root, "next").deleteRecursively() + File(root, "starts").delete() + restart(context) + } + + /** Stages [build] for the next start and keeps a build that was waiting; the caller restarts the app. */ + fun stage(context: Context, build: Saved) { + val root = root(context) + val next = File(root, "next") + val waiting = File(root, "next.replaced").apply { deleteRecursively() } + next.renameTo(waiting) + next.deleteRecursively() + File(build.dir, "reason").delete() + if (!build.dir.renameTo(next)) { + waiting.renameTo(next) + error("could not stage ${build.dir.name}") + } + archive(root, waiting, Reason.REPLACED) + waiting.deleteRecursively() + } + + fun delete(build: Saved) { + build.dir.deleteRecursively() + } + + /** + * Android's background optimization can give the APK an app image again, which keeps a Lab + * build from loading. Recompiling without one needs a shell, which the caller provides. + */ + fun needsRecompile(context: Context) = appImage(context) + + fun recompileCommand(context: Context) = "cmd package compile -m verify -f ${context.packageName}" + + fun restart(context: Context) { + val launch = context.packageManager.getLaunchIntentForPackage(context.packageName) + ?.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK) + if (launch != null) context.startActivity(launch) + Runtime.getRuntime().exit(0) + } + + @VisibleForTesting + internal fun parse(json: String): Info? = runCatching { + val o = JSONObject(json) + Info(o.getString("label"), o.getString("baseCommit"), o.getString("baseApk"), o.optLong("built")) + }.getOrNull() + + private fun read(dir: File): Info? = File(dir, "overlay.json").takeIf { it.isFile && File(dir, "overlay.apk").isFile }?.let { parse(it.readText()) } + + /** Moves `next` into use and keeps the build it replaces. Returns whether there was one to move. */ + @VisibleForTesting + internal fun promote(root: File): Boolean { + val next = File(root, "next") + if (!File(next, "overlay.apk").isFile || !File(next, "overlay.json").isFile) return false + val active = File(root, "active") + archive(root, active, Reason.REPLACED) + active.deleteRecursively() + if (!next.renameTo(active)) return false + File(root, "starts").delete() + return true + } + + @VisibleForTesting + internal fun archive(root: File, dir: File, reason: Reason) { + if (!File(dir, "overlay.apk").isFile) return + val builds = File(root, "builds").apply { mkdirs() } + var stamp = System.currentTimeMillis() + while (File(builds, stamp.toString()).exists()) stamp++ + val target = File(builds, stamp.toString()) + if (!dir.renameTo(target)) return + File(target, "reason").writeText(reason.name) + builds.listFiles().orEmpty().sortedByDescending { it.name.toLongOrNull() ?: 0L }.drop(KEEP_BUILDS).forEach { it.deleteRecursively() } + } + + @VisibleForTesting + internal fun failedStarts(root: File) = File(root, "starts").takeIf { it.isFile }?.readText()?.trim()?.toIntOrNull() ?: 0 + + private fun refuse(root: File, mainProcess: Boolean, reason: Reason) { + skipped = reason.name + Log.w(TAG, "Lab build turned off: $reason") + if (!mainProcess) return + archive(root, File(root, "active"), reason) + File(root, "starts").delete() + File(root, "last-off-reason").writeText(reason.name) + } + + private fun skip(reason: String) { + skipped = reason + Log.w(TAG, "Lab build not loaded: $reason") + } + + private fun hasAppImage(context: Context): Boolean { + val apk = File(context.applicationInfo.sourceDir) + val oat = File(apk.parentFile, "oat") + return oat.listFiles().orEmpty().any { File(it, "${apk.nameWithoutExtension}.art").isFile } + } + + @RequiresApi(30) + private fun createLoader(apk: File): ResourcesLoader = + ParcelFileDescriptor.open(apk, ParcelFileDescriptor.MODE_READ_ONLY).use { fd -> + ResourcesLoader().apply { addProvider(ResourcesProvider.loadFromApk(fd)) } + } + + /** Gives [resources] the Lab build's resources; contexts made from them inherit it. */ + fun addLoader(resources: Resources) { + if (Build.VERSION.SDK_INT < 30) return + val current = loader as? ResourcesLoader ?: return + resources.addLoaders(current) + } + + private fun prependDex(classLoader: ClassLoader, apk: File) { + val pathList = field(Class.forName("dalvik.system.BaseDexClassLoader"), "pathList").get(classLoader)!! + val elementsField = field(pathList.javaClass, "dexElements") + @Suppress("UNCHECKED_CAST") val installed = elementsField.get(pathList) as Array + val errors = ArrayList() + val made = try { + pathList.javaClass.getDeclaredMethod("makeDexElements", List::class.java, File::class.java, List::class.java, ClassLoader::class.java) + .apply { isAccessible = true }.invoke(null, listOf(apk), null, errors, classLoader) + } catch (_: NoSuchMethodException) { + pathList.javaClass.getDeclaredMethod( + "makeDexElements", List::class.java, File::class.java, List::class.java, ClassLoader::class.java, Boolean::class.javaPrimitiveType, + ).apply { isAccessible = true }.invoke(null, listOf(apk), null, errors, classLoader, false) + } + errors.firstOrNull()?.let { throw it } + @Suppress("UNCHECKED_CAST") val added = made as Array + if (added.isEmpty()) error("no dex in ${apk.name}") + val merged = java.lang.reflect.Array.newInstance(installed.javaClass.componentType!!, added.size + installed.size) + System.arraycopy(added, 0, merged, 0, added.size) + System.arraycopy(installed, 0, merged, added.size, installed.size) + elementsField.set(pathList, merged) + } + + private fun field(type: Class<*>, name: String) = type.getDeclaredField(name).apply { isAccessible = true } + + private class Callbacks(private val root: File, private val mainProcess: Boolean) : Application.ActivityLifecycleCallbacks { + private var settled = false + + override fun onActivityPreCreated(activity: Activity, savedInstanceState: Bundle?) = addLoader(activity.resources) + + override fun onActivityResumed(activity: Activity) { + if (settled || !mainProcess) return + settled = true + Handler(Looper.getMainLooper()).postDelayed({ File(root, "starts").delete() }, STABLE_AFTER_MS) + } + + override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) = Unit + override fun onActivityStarted(activity: Activity) = Unit + override fun onActivityPaused(activity: Activity) = Unit + override fun onActivityStopped(activity: Activity) = Unit + override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) = Unit + override fun onActivityDestroyed(activity: Activity) = Unit + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/BwrapSpawner.kt b/app/src/main/java/com/droiddeck/launcher/runtime/BwrapSpawner.kt index 2d1a9aa401..4a98fdedec 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/BwrapSpawner.kt +++ b/app/src/main/java/com/droiddeck/launcher/runtime/BwrapSpawner.kt @@ -182,6 +182,7 @@ object BwrapSpawner { val argv = request.getJSONArray("argv").let { a -> (0 until a.length()).map { a.getString(it) } } val env = LinkedHashMap() request.optJSONObject("env")?.let { e -> e.keys().forEach { k -> env[k] = e.getString(k) } } + withoutRootfsSettings(env, rootfs) val envTool = request.optString("envTool").takeIf { it.startsWith("/") } if (envTool == null && binds.any { it.guest == "/dev" }) gpu(rootfs, root, env)?.forEach { addBind(it) } val libc = listOf("/usr/lib/aarch64-linux-gnu/libc.so.6", "/lib/aarch64-linux-gnu/libc.so.6").map { hostPath(binds, root.path, it) } @@ -191,6 +192,24 @@ object BwrapSpawner { return Plan(root, binds, request.optString("cwd", "/").ifEmpty { "/" }, argv, env, envTool ?: "/usr/bin/env") } + private val ROOTFS_PIPEWIRE = listOf("PIPEWIRE_MODULE_DIR", "SPA_PLUGIN_DIR", "PIPEWIRE_CONFIG_DIR") + + /** + * The session's Flatpak passes settings for the session's own root on: the native hook's, + * DroidDeck's PipeWire build, which a runtime does not have (its apps found no PipeWire plugins), + * and the session's video driver for SDL. + */ + internal fun withoutRootfsSettings(env: MutableMap, rootfs: String) { + env.keys.removeAll { it.startsWith("DDNATV_") } + for (key in ROOTFS_PIPEWIRE) if (env[key]?.startsWith("/usr/local/lib/droiddeck-pipewire") == true) env.remove(key) + // the session's choice of X11 for SDL, in an app that Flatpak gave Wayland and no X display + if (env["SDL_VIDEODRIVER"] == "x11" && env["DISPLAY"].isNullOrEmpty() && !env["WAYLAND_DISPLAY"].isNullOrEmpty()) env.remove("SDL_VIDEODRIVER") + env["LD_PRELOAD"]?.let { preload -> + val kept = preload.split(':').filter { it.isNotEmpty() && it != rootfs && !it.startsWith("$rootfs/") } + if (kept.isEmpty()) env.remove("LD_PRELOAD") else env["LD_PRELOAD"] = kept.joinToString(":") + } + } + private const val GPU_DIR = "/run/droiddeck-gpu" private val LIB_DIRS = listOf("usr/lib/aarch64-linux-gnu", "usr/lib") @@ -347,22 +366,201 @@ object BwrapSpawner { try { Os.chmod(f.path, mode) } catch (e: Exception) { } } + private val SANDBOX_LOADERS = listOf("/usr/lib/aarch64-linux-gnu/ld-linux-aarch64.so.1", "/usr/lib/ld-linux-aarch64.so.1", + "/lib/ld-linux-aarch64.so.1", "/lib64/ld-linux-aarch64.so.1") + private val SANDBOX_LIBCS = listOf("/usr/lib/aarch64-linux-gnu/libc.so.6", "/usr/lib/libc.so.6", "/lib/aarch64-linux-gnu/libc.so.6") + private val SANDBOX_LIB_DIRS = listOf("/app/lib/aarch64-linux-gnu", "/app/lib", "/usr/lib/aarch64-linux-gnu", "/usr/lib") + private val seccompPatched = java.util.concurrent.ConcurrentHashMap.newKeySet() + + /** + * The sandbox started by the runtime's own glibc loader with the native hook, which maps the + * sandbox's mounts; null when the runtime cannot load the hook (older glibc), so proot runs it. + */ + private fun nativeLaunch(context: Context, plan: Plan): Pair, Map>? { + val rootfs = LinuxRuntime.rootDir(context) + val sandbox = plan.root.path + fun host(guest: String) = File(hostPath(plan.binds, sandbox, guest)) + if (!hasEnvTool(plan)) { + val program = plan.argv.firstOrNull()?.takeIf { it.startsWith("/") }?.let(::host)?.canonicalFile + ?.takeIf { it.isFile && isStaticElf(it) } ?: return null + if (!linkMounts(plan)) return null + return (listOf(program.path) + plan.argv.drop(1)) to plan.env + } + val loader = SANDBOX_LOADERS.map(::host).firstOrNull { it.isFile }?.canonicalFile ?: return null + val libc = SANDBOX_LIBCS.map(::host).firstOrNull { it.isFile }?.canonicalFile ?: return null + val hook = NativeGuest.hook(rootfs) + val need = glibcMinor(hook, GLIBC_NEED) { it.maxOrNull() } ?: return null + val have = glibcMinor(libc, GLIBC_RELEASE) { it.firstOrNull() } ?: return null + if (have < need) { + Log.i(TAG, "native userland: this runtime's glibc 2.$have cannot load the hook (2.$need); proot runs the sandbox") + return null + } + for (file in listOf(loader, libc)) { + val key = "${file.path}:${file.length()}:${file.lastModified()}" + if (key in seccompPatched) continue + // an unpatched loader or libc dies under Android's filter before the hook loads: proot runs it + val patched = runCatching { ElfSeccompPatch.patchFile(file) } + .onFailure { Log.e(TAG, "native userland: could not patch $file for Android's seccomp filter; proot runs the sandbox", it) } + if (patched.isFailure) return null + seccompPatched.add("${file.path}:${file.length()}:${file.lastModified()}") + } + val program = host(plan.envTool).canonicalFile.takeIf { it.isFile } ?: return null + if (!linkMounts(plan)) return null + writeRunpathConf(plan, ::host) + val launch = listOf(loader.path, "--argv0", program.name, program.path, "-i") + + plan.env.map { (k, v) -> "$k=$v" } + plan.argv + val env = linkedMapOf( + "DDNATV_ROOT" to sandbox, + "DDNATV_LINK_ROOT" to rootfs.path, + "DDNATV_BINDS" to plan.binds.joinToString("|") { "${it.host}:${it.guest}" }, + "DDNATV_HOSTNAME" to SessionPrefs.guestHostname(context), + "LD_LIBRARY_PATH" to SANDBOX_LIB_DIRS.map(::host).filter { it.isDirectory }.joinToString(":") { it.path }, + "LD_PRELOAD" to hook.path, + ).filterValues { it.isNotEmpty() } + return (NativeGuest.simulator(rootfs)?.let { it + launch } ?: launch) to env + } + + private val runpathDirs = java.util.concurrent.ConcurrentHashMap>() + + /** + * ld.so opens a library's absolute RUNPATH (libpulse's pulseaudio/ directory) without the hook, + * on Android's own filesystem, so those directories are listed for the hook in the sandbox's + * ld.so.conf too, as [NativeGuest.syncLibraryDirs] does for the rootfs. A checkout never changes. + */ + private fun writeRunpathConf(plan: Plan, host: (String) -> File) { + val confDir = inRoot(plan.root, "/run/flatpak/ld.so.conf.d")?.takeIf { it.isDirectory } ?: return + val dirs = sortedSetOf() + for (top in listOf("/usr/lib", "/app/lib")) { + val real = host(top).takeIf { it.isDirectory }?.canonicalFile ?: continue + dirs += runpathDirs.getOrPut(real.path) { + real.walkTopDown().maxDepth(4).onEnter { !Files.isSymbolicLink(it.toPath()) } + .filter { it.isFile && ".so" in it.name && !Files.isSymbolicLink(it.toPath()) } + .flatMap { runCatching { ElfRunpath.read(it) }.getOrDefault(emptyList()) } + .filter { it.startsWith("/usr/") || it.startsWith("/app/") || it.startsWith("/lib") } + .distinct().toList() + } + } + if (dirs.isEmpty()) return + runCatching { File(confDir, "runtime-999-droiddeck-runpath.conf").writeText(dirs.joinToString("\n", postfix = "\n")) } + } + + /** A runtime extension's apply_extra runs in a sandbox with nothing but the extension itself. */ + private fun hasEnvTool(plan: Plan): Boolean = + File(hostPath(plan.binds, plan.root.path, plan.envTool)).let { it.exists() || Files.isSymbolicLink(it.toPath()) } + + internal fun isStaticElf(file: File): Boolean = try { + java.io.RandomAccessFile(file, "r").use { f -> + val head = ByteArray(64).also { f.readFully(it) } + if (head[0] != 0x7f.toByte() || head[1] != 'E'.code.toByte() || head[4] != 2.toByte() || head[5] != 1.toByte()) return false + val le = java.nio.ByteBuffer.wrap(head).order(java.nio.ByteOrder.LITTLE_ENDIAN) + val phoff = le.getLong(32) + val phentsize = le.getShort(54).toInt() and 0xffff + val phnum = le.getShort(56).toInt() and 0xffff + val entry = ByteArray(4) + (0 until phnum).none { i -> + f.seek(phoff + i.toLong() * phentsize) + f.readFully(entry) + java.nio.ByteBuffer.wrap(entry).order(java.nio.ByteOrder.LITTLE_ENDIAN).int == 3 // PT_INTERP + } + } + } catch (_: java.io.IOException) { + false + } + + /** + * The kernel follows paths the hook does not see (ld.so, symbolic links), so each top-level + * mount of the sandbox is also a link to its source in the sandbox's own root, and the + * sandbox's links with absolute guest targets are made relative, which reaches them through those. + * False when a mount's place could not be made; proot then runs the sandbox, whose mounts hide + * whatever was left at their places. + */ + private fun linkMounts(plan: Plan): Boolean { + val root = plan.root.toPath() + plan.root.walkTopDown().onEnter { !Files.isSymbolicLink(it.toPath()) }.map { it.toPath() } + .filter { Files.isSymbolicLink(it) }.toList().forEach { link -> + runCatching { + val target = Files.readSymbolicLink(link).toString() + if (!target.startsWith("/")) return@runCatching + Files.delete(link) + Files.createSymbolicLink(link, link.parent.relativize(root.resolve(target.removePrefix("/")))) + } + } + // a mount inside another mount (a GL extension at /usr/lib/aarch64-linux-gnu/GL) needs its own + // place: the links of the mounts it is in become directories of links to their entries + val entryLinks = HashSet() + for (bind in plan.binds.sortedBy { it.guest.length }) { + if (bind.guest == "/") continue + val at = try { + mountPlace(plan.root, bind.guest, entryLinks)?.also { at -> + val path = at.toPath() + if (path in entryLinks) { + Files.delete(path) + entryLinks.remove(path) + } + } + } catch (e: java.io.IOException) { + Log.e(TAG, "native userland: could not make a place for ${bind.guest}; proot runs the sandbox", e) + return false + } ?: continue + val path = at.toPath() + if (at.exists() || Files.isSymbolicLink(path)) continue + at.parentFile?.mkdirs() + runCatching { Files.createSymbolicLink(path, java.nio.file.Paths.get(bind.host)) } + } + return true + } + + private fun mountPlace(root: File, guest: String, entryLinks: MutableSet): File? { + val parts = guest.split('/').filter { it.isNotEmpty() } + var f = root + for ((i, part) in parts.withIndex()) { + if (part == "..") return null + f = File(f, part) + val path = f.toPath() + if (i == parts.lastIndex || !Files.isSymbolicLink(path)) continue + val target = Files.readSymbolicLink(path) + // a link of the sandbox's own tree, made relative above, is not a mount + if (!target.isAbsolute) return null + Files.delete(path) + entryLinks.remove(path) + Files.createDirectory(path) + target.toFile().listFiles()?.forEach { entry -> + val link = path.resolve(entry.name) + Files.createSymbolicLink(link, entry.toPath()) + entryLinks.add(link) + } + } + return f + } + private fun run(context: Context, plan: Plan, client: LocalSocket, out: DataOutputStream, id: Int) { - val cmd = LinuxRuntime.prootPrefix(context, plan.root, plan.cwd) val verbose = plan.env["DROIDDECK_PROOT_VERBOSE"]?.toIntOrNull() - if (verbose != null) { cmd.add(1, "-v"); cmd.add(2, verbose.toString()) } - plan.binds.forEach { cmd.add("-b"); cmd.add(it.spec) } - cmd.add(plan.envTool) - cmd.add("-i") - plan.env.forEach { (k, v) -> cmd.add("$k=$v") } - cmd.addAll(plan.argv) - val builder = ProcessBuilder(cmd).redirectErrorStream(true).directory(plan.root) - builder.environment().apply { - clear() - put("PROOT_LOADER", LinuxRuntime.prootLoader(context).path) - put("PROOT_TMP_DIR", LinuxRuntime.prootTempDir(context).path) - if (SessionPrefs.prootNoSeccomp(context)) put("PROOT_NO_SECCOMP", "1") - LinuxRuntime.prootLibraryPath(context).takeIf { it.isNotEmpty() }?.let { put("LD_LIBRARY_PATH", it) } + val native = if (NativeGuest.forSession(context)) nativeLaunch(context, plan) else null + val builder = if (native != null) { + ProcessBuilder(native.first).redirectErrorStream(true) + .directory(File(hostPath(plan.binds, plan.root.path, plan.cwd)).takeIf { it.isDirectory } ?: plan.root) + .also { b -> b.environment().apply { clear(); putAll(native.second) } } + } else { + val cmd = LinuxRuntime.prootPrefix(context, plan.root, plan.cwd) + if (verbose != null) { cmd.add(1, "-v"); cmd.add(2, verbose.toString()) } + plan.binds.forEach { cmd.add("-b"); cmd.add(it.spec) } + val envTool = hasEnvTool(plan) + if (envTool) { + cmd.add(plan.envTool) + cmd.add("-i") + plan.env.forEach { (k, v) -> cmd.add("$k=$v") } + } + cmd.addAll(plan.argv) + ProcessBuilder(cmd).redirectErrorStream(true).directory(plan.root).also { b -> + b.environment().apply { + clear() + if (!envTool) putAll(plan.env) + put("PROOT_LOADER", LinuxRuntime.prootLoader(context).path) + put("PROOT_TMP_DIR", LinuxRuntime.prootTempDir(context).path) + if (SessionPrefs.prootNoSeccomp(context)) put("PROOT_NO_SECCOMP", "1") + LinuxRuntime.prootLibraryPath(context).takeIf { it.isNotEmpty() }?.let { put("LD_LIBRARY_PATH", it) } + } + } } val process = builder.start() val pid = HostProcess.pidOf(process) diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/DesktopCatalog.kt b/app/src/main/java/com/droiddeck/launcher/runtime/DesktopCatalog.kt index 2b976796fc..835c85894d 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/DesktopCatalog.kt +++ b/app/src/main/java/com/droiddeck/launcher/runtime/DesktopCatalog.kt @@ -15,6 +15,7 @@ object DesktopCatalog { const val CATALOG_URL = "https://raw.githubusercontent.com/Droid-Deck/DroidDeck-Components/main/desktop.json" const val STEAM_SEED_URL = "https://raw.githubusercontent.com/Droid-Deck/DroidDeck-Components/main/steam-seed.json" const val PROTON_SEED_ID = "proton-arm64" + const val APPIMAGE_EXEC = "/usr/local/bin/droiddeck-appimage-exec" class Entry( val id: String, val name: String, val tier: Int, val version: String, val kind: String, @@ -216,7 +217,7 @@ object DesktopCatalog { target.setExecutable(true, false) FileUtils.writeString(File(root, "usr/share/applications/droiddeck-${entry.id}.desktop"), "[Desktop Entry]\nType=Application\nName=${entry.name}\n" + - "Exec=env APPIMAGE_EXTRACT_AND_RUN=1 /opt/appimages/${entry.id}.AppImage\n" + + "Exec=$APPIMAGE_EXEC /opt/appimages/${entry.id}.AppImage\n" + "Icon=${entry.icon}\nTerminal=false\nCategories=${entry.category};\n") } else -> { diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/ElfRunpath.kt b/app/src/main/java/com/droiddeck/launcher/runtime/ElfRunpath.kt new file mode 100644 index 0000000000..dc772c49a1 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/runtime/ElfRunpath.kt @@ -0,0 +1,72 @@ +package com.droiddeck.launcher.runtime + +import java.io.File +import java.io.RandomAccessFile +import java.nio.ByteBuffer +import java.nio.ByteOrder + +/** The absolute DT_RUNPATH and DT_RPATH directories of a 64-bit little-endian ELF object. */ +object ElfRunpath { + private const val PT_LOAD = 1 + private const val PT_DYNAMIC = 2 + private const val DT_STRTAB = 5 + private const val DT_RPATH = 15 + private const val DT_RUNPATH = 29 + + fun read(file: File): List { + if (file.length() < 64) return emptyList() + RandomAccessFile(file, "r").use { raf -> + val channel = raf.channel + val header = ByteBuffer.allocate(64).order(ByteOrder.LITTLE_ENDIAN) + channel.read(header, 0) + if (header.getInt(0) != 0x464c457f || header.get(4).toInt() != 2 || header.get(5).toInt() != 1) return emptyList() + val phoff = header.getLong(32) + val phentsize = header.getShort(54).toInt() and 0xffff + val phnum = header.getShort(56).toInt() and 0xffff + if (phnum == 0 || phnum > 128 || phentsize < 56) return emptyList() + val table = ByteBuffer.allocate(phentsize * phnum).order(ByteOrder.LITTLE_ENDIAN) + channel.read(table, phoff) + class Load(val vaddr: Long, val offset: Long, val size: Long) + val loads = ArrayList() + var dynOffset = -1L + var dynSize = 0L + for (i in 0 until phnum) { + val at = i * phentsize + val type = table.getInt(at) + val offset = table.getLong(at + 8) + val vaddr = table.getLong(at + 16) + val filesz = table.getLong(at + 32) + if (type == PT_LOAD) loads.add(Load(vaddr, offset, filesz)) + if (type == PT_DYNAMIC) { dynOffset = offset; dynSize = filesz } + } + if (dynOffset < 0 || dynSize <= 0 || dynSize > 1 shl 20) return emptyList() + val dynamic = ByteBuffer.allocate(dynSize.toInt()).order(ByteOrder.LITTLE_ENDIAN) + channel.read(dynamic, dynOffset) + var strtab = -1L + val names = ArrayList() + var at = 0 + while (at + 16 <= dynamic.capacity()) { + val tag = dynamic.getLong(at) + val value = dynamic.getLong(at + 8) + if (tag == 0L) break + if (tag == DT_STRTAB.toLong()) strtab = value + if (tag == DT_RPATH.toLong() || tag == DT_RUNPATH.toLong()) names.add(value) + at += 16 + } + if (strtab < 0 || names.isEmpty()) return emptyList() + val strtabOffset = loads.firstOrNull { strtab >= it.vaddr && strtab < it.vaddr + it.size } + ?.let { strtab - it.vaddr + it.offset } ?: return emptyList() + val result = ArrayList() + for (name in names) { + val buffer = ByteBuffer.allocate(4096) + val n = channel.read(buffer, strtabOffset + name) + if (n <= 0) continue + val bytes = buffer.array() + var end = 0 + while (end < n && bytes[end].toInt() != 0) end++ + String(bytes, 0, end, Charsets.UTF_8).split(':').filter { it.startsWith("/") }.forEach { result.add(it.trimEnd('/')) } + } + return result + } + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/ElfSeccompPatch.kt b/app/src/main/java/com/droiddeck/launcher/runtime/ElfSeccompPatch.kt new file mode 100644 index 0000000000..817cbc9757 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/runtime/ElfSeccompPatch.kt @@ -0,0 +1,85 @@ +package com.droiddeck.launcher.runtime + +import java.io.File +import java.nio.ByteBuffer +import java.nio.ByteOrder + +/** + * Android's app seccomp filter kills a process with SIGSYS for rseq, set_robust_list and clone3. glibc's + * dynamic linker makes the first two before any preloaded library (so before the hook's SIGSYS + * handler) exists, so the call sites in the rootfs's ld.so and libc answer by themselves instead: the + * `svc` after `mov x8, #nr` becomes a no-op for set_robust_list (glibc ignores the result) and + * `mov x0, #-ENOSYS` for rseq and clone3 (glibc falls back from both). Under proot the same + * answers are what proot gives. + */ +object ElfSeccompPatch { + private const val SVC = 0xd4000001.toInt() + private const val NOP = 0xd503201f.toInt() + private const val MOV_X0_ENOSYS = 0x928004a0.toInt() + private const val MOV_X8_SET_ROBUST_LIST = 0xd2800c68.toInt() + private const val MOV_X8_CLONE3 = 0xd2803668.toInt() + private const val MOV_X8_RSEQ = 0xd28024a8.toInt() + private const val WINDOW = 16 + + /** Patches [data] in place and returns the number of call sites changed. */ + fun patch(data: ByteArray): Int { + if (data.size < 64 || data[0] != 0x7f.toByte() || data[1] != 'E'.code.toByte() || data[4].toInt() != 2 || data[5].toInt() != 1) return 0 + val buffer = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN) + if (buffer.getShort(18).toInt() != 183) return 0 + val phoff = buffer.getLong(32).toInt() + val phentsize = buffer.getShort(54).toInt() and 0xffff + val phnum = buffer.getShort(56).toInt() and 0xffff + var patched = 0 + for (i in 0 until phnum) { + val at = phoff + i * phentsize + if (at + 56 > data.size) break + if (buffer.getInt(at) != 1 || (buffer.getInt(at + 4) and 1) == 0) continue + val start = ((buffer.getLong(at + 8).toInt()) + 3) and 3.inv() + val end = minOf(data.size, buffer.getLong(at + 8).toInt() + buffer.getLong(at + 32).toInt()) and 3.inv() + var pos = start + while (pos + 4 <= end) { + val replacement = when (buffer.getInt(pos)) { + MOV_X8_SET_ROBUST_LIST -> NOP + MOV_X8_CLONE3, MOV_X8_RSEQ -> MOV_X0_ENOSYS + else -> null + } + if (replacement != null) { + var next = pos + 4 + val limit = minOf(end, pos + 4 + WINDOW * 4) + while (next < limit) { + val insn = buffer.getInt(next) + if (insn == SVC) { + buffer.putInt(next, replacement) + patched++ + break + } + // A site patched before, or x8 set again: an svc after it is another call's + // (a second run found clone3's thread exit there and turned it into -ENOSYS) + if (insn == MOV_X0_ENOSYS || (insn and 0x1f) == 8) break + next += 4 + } + } + pos += 4 + } + } + return patched + } + + /** Patches the file behind [path] (symlinks followed) if it still has unpatched call sites. */ + fun patchFile(path: File): Int { + val target = path.canonicalFile + if (!target.isFile) return 0 + val data = target.readBytes() + val count = patch(data) + if (count == 0) return 0 + val temp = File(target.parentFile, "." + target.name + ".seccomp") + temp.writeBytes(data) + temp.setExecutable(true, false) + temp.setReadable(true, false) + if (!temp.renameTo(target)) { + temp.delete() + throw java.io.IOException("could not replace ${target.path}") + } + return count + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/GuestCommand.kt b/app/src/main/java/com/droiddeck/launcher/runtime/GuestCommand.kt index 8e5769c312..0560752a31 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/GuestCommand.kt +++ b/app/src/main/java/com/droiddeck/launcher/runtime/GuestCommand.kt @@ -35,12 +35,10 @@ object GuestCommand { SessionFiles.stage(context, root) LinuxNetworkLinkComponent(context, root).publish() val runtimeDir = File(context.filesDir, ".flatpak-rt").apply { mkdirs() } - val cmd = LinuxRuntime.prootPrefix(context, root, "/root", fakeRoot) - if (linkDir != null) cmd.add(1, "--link2symlink") val binds = LinuxRuntime.binds(context, null, runtimeDir, Environment.getExternalStorageDirectory(), extraBinds) - binds.forEach { cmd.add("-b"); cmd.add(it) } - val fastPathKey = if (fastPath && !fakeRoot && linkDir == null && ProotFastPath.enabled(context)) ProotFastPath.key(root, binds) else null - cmd += listOf( + val native = NativeGuest.forSession(context) + val fastPathKey = if (!native && fastPath && !fakeRoot && linkDir == null && ProotFastPath.enabled(context)) ProotFastPath.key(root, binds) else null + val guest = mutableListOf( "/usr/bin/env", "-i", "HOME=/root", "USER=root", "LANG=C.UTF-8", "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games", "XDG_RUNTIME_DIR=${LinuxRuntime.GUEST_RUNTIME_DIR}", "XDG_DATA_HOME=/root/.local/share", "FLATPAK_BWRAP=${FlatpakManager.BWRAP}", @@ -48,22 +46,31 @@ object GuestCommand { "XDG_DATA_DIRS=/root/.local/share/flatpak/exports/share:/usr/local/share:/usr/share", ) DownloadSource.proxyHostPort(context).takeIf { it.isNotEmpty() }?.let { hostPort -> - cmd += listOf( + guest += listOf( "http_proxy=http://$hostPort", "https_proxy=http://$hostPort", "HTTP_PROXY=http://$hostPort", "HTTPS_PROXY=http://$hostPort", "no_proxy=localhost,127.0.0.1,::1", "NO_PROXY=localhost,127.0.0.1,::1", ) } - fastPathKey?.let { cmd += ProotFastPath.guestEnv(root, binds, it) } - cmd += argv - val builder = ProcessBuilder(cmd).directory(root).redirectErrorStream(true) - builder.environment().apply { - put("PROOT_LOADER", LinuxRuntime.prootLoader(context).path) - put("PROOT_TMP_DIR", LinuxRuntime.prootTempDir(context).path) - if (linkDir != null) put("PROOT_L2S_DIR", linkDir.path) - if (SessionPrefs.prootNoSeccomp(context)) put("PROOT_NO_SECCOMP", "1") - fastPathKey?.let { ProotFastPath.hostEnv(it).let { (k, v) -> put(k, v) } } - LinuxRuntime.prootLibraryPath(context).takeIf { it.isNotEmpty() }?.let { put("LD_LIBRARY_PATH", it) } + fastPathKey?.let { guest += ProotFastPath.guestEnv(root, binds, it) } + guest += argv + val builder = if (native) { + NativeGuest.processBuilder(context, root, binds, guest).redirectErrorStream(true) + } else { + val cmd = LinuxRuntime.prootPrefix(context, root, "/root", fakeRoot) + if (linkDir != null) cmd.add(1, "--link2symlink") + binds.forEach { cmd.add("-b"); cmd.add(it) } + cmd += guest + ProcessBuilder(cmd).directory(root).redirectErrorStream(true).also { b -> + b.environment().apply { + put("PROOT_LOADER", LinuxRuntime.prootLoader(context).path) + put("PROOT_TMP_DIR", LinuxRuntime.prootTempDir(context).path) + if (linkDir != null) put("PROOT_L2S_DIR", linkDir.path) + if (SessionPrefs.prootNoSeccomp(context)) put("PROOT_NO_SECCOMP", "1") + fastPathKey?.let { ProotFastPath.hostEnv(it).let { (k, v) -> put(k, v) } } + LinuxRuntime.prootLibraryPath(context).takeIf { it.isNotEmpty() }?.let { put("LD_LIBRARY_PATH", it) } + } + } } if (cancelled?.get() == true) return 130 val process = builder.start() diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/LegacyNames.kt b/app/src/main/java/com/droiddeck/launcher/runtime/LegacyNames.kt index eb01d3dc53..3febbfd1fa 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/LegacyNames.kt +++ b/app/src/main/java/com/droiddeck/launcher/runtime/LegacyNames.kt @@ -21,9 +21,11 @@ object LegacyNames { "etc/bannerlator-network.json" to "etc/droiddeck-network.json", "etc/bannerlator-wifi-scan-request" to "etc/droiddeck-wifi-scan-request", ) - private const val MENU_DIR = "usr/local/share/applications" - private const val OLD_BIN = "/usr/local/bin/bannerlator-" - private const val NEW_BIN = "/usr/local/bin/droiddeck-" + private val MENU_REWRITES = listOf( + Triple("usr/local/share/applications", "/usr/local/bin/bannerlator-", "/usr/local/bin/droiddeck-"), + Triple("usr/share/applications", "Exec=env APPIMAGE_EXTRACT_AND_RUN=1 /opt/appimages/", + "Exec=${DesktopCatalog.APPIMAGE_EXEC} /opt/appimages/"), + ) @JvmStatic @Synchronized @@ -95,14 +97,16 @@ object LegacyNames { } private fun rewriteMenuEntries(root: File) { - val entries = File(root, MENU_DIR).listFiles { f -> f.isFile && f.name.startsWith("droiddeck-") && f.name.endsWith(".desktop") } ?: return - for (entry in entries) { - val text = FileUtils.readString(entry) ?: continue - if (!text.contains(OLD_BIN)) continue - val staged = File(entry.parentFile, entry.name + ".staged") - if (!FileUtils.writeString(staged, text.replace(OLD_BIN, NEW_BIN)) || !staged.renameTo(entry)) { - staged.delete() - Log.w(TAG, "could not rewrite $entry") + for ((dir, old, new) in MENU_REWRITES) { + val entries = File(root, dir).listFiles { f -> f.isFile && f.name.startsWith("droiddeck-") && f.name.endsWith(".desktop") } ?: continue + for (entry in entries) { + val text = FileUtils.readString(entry) ?: continue + if (!text.contains(old)) continue + val staged = File(entry.parentFile, entry.name + ".staged") + if (!FileUtils.writeString(staged, text.replace(old, new)) || !staged.renameTo(entry)) { + staged.delete() + Log.w(TAG, "could not rewrite $entry") + } } } } diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/LinuxRuntime.java b/app/src/main/java/com/droiddeck/launcher/runtime/LinuxRuntime.java index 3e5b0a29cb..0253604021 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/LinuxRuntime.java +++ b/app/src/main/java/com/droiddeck/launcher/runtime/LinuxRuntime.java @@ -155,6 +155,11 @@ public final class LinuxRuntime { private static volatile List lastBinds; + /** The mounts of a session started without proot, which sandboxes and root helpers see too. */ + public static void rememberSessionBinds(List binds) { + lastBinds = binds; + } + public static List lastBinds(Context context) { List binds = lastBinds; if (binds != null) return binds; @@ -306,7 +311,17 @@ public final class LinuxRuntime { private static boolean unreadablePciDevice() { File[] devices = new File("/sys/bus/pci/devices").listFiles(); - if (devices == null) return false; + if (devices == null) { + // The directory is there but the app may not list it (Android 13 on the AYN Thor): libpci then + // aborts the whole program with "Cannot open /sys/bus/pci/devices", which Chromium's GPU + // initialisation runs into. Not there at all (no PCI bus) is fine as it is. + try { + android.system.Os.stat("/sys/bus/pci/devices"); + return true; + } catch (android.system.ErrnoException e) { + return e.errno == android.system.OsConstants.EACCES; + } + } for (File device : devices) { if (!new File(device, "vendor").canRead()) return true; } diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/LinuxRuntimeInstaller.java b/app/src/main/java/com/droiddeck/launcher/runtime/LinuxRuntimeInstaller.java index 21a40a92ae..cf613008bb 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/LinuxRuntimeInstaller.java +++ b/app/src/main/java/com/droiddeck/launcher/runtime/LinuxRuntimeInstaller.java @@ -82,8 +82,12 @@ public final class LinuxRuntimeInstaller { private LinuxRuntimeInstaller() {} public static String installedVersion(Context context) { + return LinuxRuntime.isInstalled(context) ? rootfsVersion(context) : null; + } + + private static String rootfsVersion(Context context) { File marker = new File(LinuxRuntime.rootDir(context), VERSION_FILE); - if (!marker.isFile() || !LinuxRuntime.isInstalled(context)) return null; + if (!marker.isFile()) return null; String v = FileUtils.readString(marker); return v == null ? null : v.trim(); } @@ -144,6 +148,10 @@ public final class LinuxRuntimeInstaller { } }; boolean desktop = DesktopCatalog.INSTANCE.desktopInstalled(app); + if (packsOnly(app, release, progress)) { + job.ok = true; + return true; + } job.ok = installOnce(app, release, progress); if (job.ok && desktop) keepDesktop(app, progress); return job.ok; @@ -283,6 +291,31 @@ public final class LinuxRuntimeInstaller { for (File file : files) file.delete(); } + private static kotlin.jvm.functions.Function1 packProgress(ProgressListener listener, String stage) { + return state -> { + if (listener != null && state instanceof AddonPack.State.Downloading) { + listener.onProgress(Step.DOWNLOADING, stage, Math.round(((AddonPack.State.Downloading) state).getProgress() * 100f)); + } + return kotlin.Unit.INSTANCE; + }; + } + + private static boolean packsOnly(Context context, Release release, ProgressListener listener) { + if (!LinuxRuntime.hasRootfs(context) || LinuxRuntime.isInstalled(context) || !release.version.equals(rootfsVersion(context))) return false; + File root = LinuxRuntime.rootDir(context); + try { + recoverInterruptedSwap(root, new File(root.getParentFile(), LinuxRuntime.DIR + ".new"), new File(root.getParentFile(), LinuxRuntime.DIR + ".old")); + } catch (IOException e) { + Log.w(TAG, "packs only: " + e); + return false; + } + String downloading = context.getString(R.string.rtinst_downloading); + if (listener != null) listener.onProgress(Step.DOWNLOADING, downloading, 0); + return AddonPack.PROOT.await(context, () -> true, packProgress(listener, downloading)) + && AddonPack.DIRECTAUDIO.await(context, () -> true, packProgress(listener, downloading)) + && LinuxRuntime.isInstalled(context); + } + private static boolean installOnce(Context context, Release release, ProgressListener listener) { File archive = new File(context.getCacheDir(), archiveName(release)); boolean keepPartial = false; @@ -294,10 +327,10 @@ public final class LinuxRuntimeInstaller { emptyTrash(LinuxRuntime.rootDir(context).getParentFile()); String downloading = context.getString(R.string.rtinst_downloading); if (listener != null) listener.onProgress(Step.DOWNLOADING, downloading, 0); - if (!LinuxRuntime.patchedProot(context) && !AddonPack.PROOT.await(context) + if (!LinuxRuntime.patchedProot(context) && !AddonPack.PROOT.await(context, () -> true, packProgress(listener, downloading)) && !SessionPrefs.allowStockProot(context)) throw new IOException("Cannot download proot"); - if (!AddonPack.DIRECTAUDIO.await(context)) throw new IOException("Cannot download DirectAudio"); + if (!AddonPack.DIRECTAUDIO.await(context, () -> true, packProgress(listener, downloading))) throw new IOException("Cannot download DirectAudio"); boolean ok = Downloader.downloadFile(release.url, archive, true, release.sha256, (fraction) -> { if (listener != null) { listener.onProgress(Step.DOWNLOADING, downloading, diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/NativeGuest.kt b/app/src/main/java/com/droiddeck/launcher/runtime/NativeGuest.kt new file mode 100644 index 0000000000..ae4106a081 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/runtime/NativeGuest.kt @@ -0,0 +1,269 @@ +package com.droiddeck.launcher.runtime + +import android.content.Context +import com.droiddeck.launcher.session.SessionPrefs +import java.io.File +import java.nio.file.Files + +/** + * Starts guest programs directly on the Android kernel, with no proot: the glibc loader of the + * rootfs runs the program and [HOOK] translates the guest's absolute paths, execs and the system + * calls Android's seccomp policy blocks, from inside the process. + */ +object NativeGuest { + private const val TAG = "NativeGuest" + const val HOOK = "usr/lib/libddnatv-hook.so" + private val LIB_DIRS = listOf("usr/lib", "usr/lib/aarch64-linux-gnu", "usr/local/lib", "usr/lib/systemd") + + /** Libraries every native program loads before the hook, as /etc/ld.so.preload does for proot. */ + fun preloadList(root: File): List { + val listed = File(root, "etc/droiddeck/native-preload").takeIf { it.isFile }?.readLines().orEmpty() + return listed.map { it.trim() }.filter { it.startsWith("/") && File(root, it.removePrefix("/")).isFile } + } + + fun enabled(context: Context): Boolean = + SessionPrefs.nativeUserland(context) && available(LinuxRuntime.rootDir(context)) + + /** The mode the running session started in; null between sessions. */ + @Volatile var sessionMode: Boolean? = null + + /** + * Whether a session's helpers (root commands, Flatpak sandboxes, installers) run natively: the + * session's own mode while one runs, as the setting can change under it and the rootfs is only + * prepared for native programs at a session's start. + */ + fun forSession(context: Context): Boolean = sessionMode ?: enabled(context) + + /** + * Development switch: etc/droiddeck/seccomp-sim names a helper (a guest path, then its arguments) + * that applies Android's app seccomp filter before the program starts, for devices that run apps + * without one (eng builds, or a permissive SELinux, where zygote skips it). Guest paths among the + * arguments are given as their places in the rootfs, as the helper runs before the hook. + */ + fun simulator(root: File): List? { + val words = File(root, "etc/droiddeck/seccomp-sim").takeIf { it.isFile }?.readText()?.trim() + ?.split(Regex("\\s+"))?.filter { it.isNotEmpty() } ?: return null + val helper = words.firstOrNull()?.takeIf { it.startsWith("/") }?.let { File(root, it.removePrefix("/")) } + ?.takeIf { it.isFile } ?: return null + return listOf(helper.path) + words.drop(1).map { if (it.startsWith("/")) File(root, it.removePrefix("/")).path else it } + } + + fun hook(root: File): File = File(root, HOOK) + + fun loader(root: File): File? = + listOf("usr/lib/ld-linux-aarch64.so.1", "lib/ld-linux-aarch64.so.1").map { File(root, it) }.firstOrNull { it.exists() } + + fun available(root: File): Boolean = hook(root).isFile && loader(root) != null + + fun libraryPath(root: File): String = LIB_DIRS.joinToString(":") { File(root, it).path } + + /** Environment every native process starts with; a guest `env -i` is re-seeded by the hook. */ + fun environment(root: File, preload: List = emptyList()): Map = linkedMapOf( + "DDNATV_ROOT" to root.path, + "LD_LIBRARY_PATH" to libraryPath(root), + "LD_PRELOAD" to (preload.map { File(root, it.removePrefix("/")).path } + hook(root).path).joinToString(":"), + ) + + /** + * The host command line for the guest program at [program] (an absolute guest path to a real + * file, not a symlink). + */ + fun command(root: File, program: String, args: List): List { + val loader = checkNotNull(loader(root)) { "no glibc loader in ${root.path}" } + val host = File(root, program.removePrefix("/")) + val launch = listOf(loader.path, "--argv0", host.name, host.path) + args + return simulator(root)?.let { it + launch } ?: launch + } + + /** The environment of the session process: the hook's root, the mounts proot used to make, the preload list. */ + fun sessionEnvironment(root: File, binds: List, hostname: String? = null): Map { + val preload = preloadList(root) + return linkedMapOf( + "DDNATV_ROOT" to root.path, + "DDNATV_HOSTNAME" to hostname.orEmpty(), + "DDNATV_BINDS" to binds.joinToString("|") { if (it.contains(':')) it else "$it:$it" }, + "DDNATV_PRELOAD" to preload.joinToString(":"), + "LD_LIBRARY_PATH" to libraryPath(root), + "LD_PRELOAD" to (preload.map { File(root, it.removePrefix("/")).path } + hook(root).path).joinToString(":"), + ).filterValues { it.isNotEmpty() } + } + + /** Where guest directory [cwd] is, through the session's mounts ([binds], "src:dst"); the rootfs if it is nowhere. */ + fun workingDirectory(root: File, binds: List, cwd: String): File = + File(BwrapSpawner.hostPath(binds.map { BwrapSpawner.Bind.parse(it) }, root.path, cwd)).takeIf { it.isDirectory } ?: root + + /** A process for [guest] (argv) started as proot would with [binds], in guest directory [cwd]. */ + fun processBuilder(context: Context, root: File, binds: List, guest: List, cwd: String = "/root"): ProcessBuilder = + ProcessBuilder(command(root, guest.first(), guest.drop(1))) + .directory(workingDirectory(root, binds, cwd)) + .also { builder -> + builder.environment().apply { + remove("LD_AUDIT") + putAll(sessionEnvironment(root, binds, SessionPrefs.guestHostname(context))) + } + } + + private const val RUNPATH_CONF = "etc/ld.so.conf.d/zz-droiddeck-native.conf" + private val SCAN_DIRS = listOf("usr/lib", "usr/local/lib", "opt") + + /** + * ld.so opens the absolute RUNPATH directories of a library itself, with system calls the hook + * cannot translate, so every one of them is also listed in ld.so.conf (which the hook maps). + * Rescanned when dpkg's state or a library directory changes. + */ + fun syncLibraryDirs(root: File) { + val conf = File(root, RUNPATH_CONF) + val signature = (listOf("var/lib/dpkg/status", "usr/lib", "usr/local/lib", "usr/lib/aarch64-linux-gnu") + .joinToString(",") { File(root, it).lastModified().toString() }) + "," + conf.parentFile.list()?.size + val stamp = File(root, "etc/droiddeck/native-runpath-signature") + if (conf.isFile && stamp.isFile && stamp.readText() == signature) return + val dirs = sortedSetOf() + for (name in SCAN_DIRS) File(root, name).walkTopDown().maxDepth(6) + .onEnter { !Files.isSymbolicLink(it.toPath()) } + .filter { it.isFile && !Files.isSymbolicLink(it.toPath()) && ".so" in it.name } + .forEach { file -> runCatching { ElfRunpath.read(file) }.getOrNull()?.let { dirs.addAll(it) } } + val kept = dirs.filter { it.startsWith("/usr") || it.startsWith("/lib") || it.startsWith("/opt") } + conf.parentFile?.mkdirs() + conf.writeText(kept.joinToString("\n", postfix = "\n")) + stamp.parentFile?.mkdirs() + stamp.writeText(signature) + } + + /** What a rootfs needs before anything runs in it natively; cheap once done. */ + fun prepare(context: Context, root: File) { + val relinked = relinkAbsoluteSymlinks(context, root) + if (relinked > 0) android.util.Log.i(TAG, "native userland: $relinked absolute symlinks made relative") + val patched = patchForSeccomp(root) + if (patched > 0) android.util.Log.i(TAG, "native userland: $patched system call sites in ld.so and libc answer Android's blocked calls") + syncLibraryDirs(root) + } + + /** See [ElfSeccompPatch]: the linker and libc must survive Android's seccomp filter before the hook loads. */ + fun patchForSeccomp(root: File): Int = + listOf("usr/lib/ld-linux-aarch64.so.1", "usr/lib/aarch64-linux-gnu/ld-linux-aarch64.so.1", + "usr/lib/aarch64-linux-gnu/libc.so.6", "usr/lib/libc.so.6") + .map { File(root, it) }.filter { it.exists() } + .distinctBy { it.canonicalPath }.sumOf { file -> + runCatching { ElfSeccompPatch.patchFile(file) }.getOrElse { + android.util.Log.e(TAG, "native userland: could not patch $file for Android's seccomp filter; programs will die with SIGSYS", it) + 0 + } + } + + private val HOST_LINK_TARGETS = listOf("/proc", "/dev", "/sys", "/sdcard", "/storage", "/data", "/system", "/vendor", "/apex", + "/system_ext", "/product", "/odm", "/linkerconfig", "/mnt") + + /** + * Symbolic links the kernel follows by itself (ld.so, Steam, anything that skips the hook) read + * their absolute target on Android's own filesystem. The image's links were made for proot, + * which reads the target inside the rootfs, so each is rewritten as a relative link, which + * resolves the same way under proot and without it. Rerun when the runtime changes. + */ + fun relinkAbsoluteSymlinks(context: Context, root: File): Int { + val stamp = File(root, "etc/droiddeck/native-symlinks") + val version = com.droiddeck.launcher.runtime.LinuxRuntimeInstaller.installedVersion(context).orEmpty() + "/2" + if (stamp.isFile && stamp.readText() == version) return 0 + val rootPath = root.toPath() + var rewritten = 0 + Files.walkFileTree(rootPath, object : java.nio.file.SimpleFileVisitor() { + override fun visitFile(file: java.nio.file.Path, attrs: java.nio.file.attribute.BasicFileAttributes): java.nio.file.FileVisitResult { + if (attrs.isSymbolicLink) { + try { + val target = Files.readSymbolicLink(file).toString() + if (target.startsWith("/") && HOST_LINK_TARGETS.none { target == it || target.startsWith("$it/") } + && !target.startsWith(root.path)) { + val relative = file.parent.relativize(rootPath.resolve(target.removePrefix("/"))) + val temp = file.resolveSibling(".relink-" + file.fileName) + Files.deleteIfExists(temp) + Files.createSymbolicLink(temp, relative) + Files.move(temp, file, java.nio.file.StandardCopyOption.REPLACE_EXISTING) + rewritten++ + } + } catch (_: java.io.IOException) { + } + } + return java.nio.file.FileVisitResult.CONTINUE + } + + override fun visitFileFailed(file: java.nio.file.Path, exc: java.io.IOException) = java.nio.file.FileVisitResult.CONTINUE + }) + stamp.parentFile?.mkdirs() + stamp.writeText(version) + return rewritten + } + + /** + * The guest path that host path [target] stands for in a session with [binds] ("src:dst"), or null + * when it is a guest path already: the rootfs, or the longest mount source holding it (the first + * such mount when several share a source). + */ + fun guestLinkTarget(target: String, root: String, binds: List): String? { + if (!target.startsWith("/")) return null + fun under(path: String, dir: String) = path == dir || path.startsWith("$dir/") + var best: Pair? = if (under(target, root)) root to "/" else null + for (spec in binds) { + val colon = spec.indexOf(':') + if (colon < 0) continue + val src = spec.substring(0, colon).trimEnd('/') + val dst = spec.substring(colon + 1).trimEnd('/').ifEmpty { "/" } + if (src.isEmpty() || src == dst || !under(target, src)) continue + if (best == null || src.length > best.first.length) best = src to dst + } + val (src, dst) = best ?: return null + val rest = target.removePrefix(src) + return if (rest.isEmpty()) dst else dst.trimEnd('/') + rest + } + + /** + * Wine prefixes' drive links (dosdevices) hold guest paths ("s:" -> /mnt/droiddeck-sd), which + * proot follows and the hook follows inside a mount. Earlier native sessions made them point at + * their real places, which proot cannot see: those are given their guest paths back. + */ + fun relinkPrefixDrives(root: File, binds: List): Int { + val compatdata = listOf(File(root, "root/.local/share/Steam/steamapps/compatdata")) + binds.mapNotNull { spec -> + val colon = spec.indexOf(':') + if (colon > 0 && spec.substring(colon + 1).trimEnd('/').endsWith("/steamapps/compatdata")) File(spec.substring(0, colon)) else null + } + var rewritten = 0 + for (prefix in compatdata.distinct().flatMap { it.listFiles()?.toList().orEmpty() }) { + val devices = File(prefix, "pfx/dosdevices").toPath() + val links = try { + Files.newDirectoryStream(devices).use { it.toList() } + } catch (e: java.nio.file.NoSuchFileException) { + continue + } catch (e: java.io.IOException) { + android.util.Log.w(TAG, "native userland: could not read the drive links of $prefix", e) + continue + } catch (e: java.nio.file.DirectoryIteratorException) { + android.util.Log.w(TAG, "native userland: could not read the drive links of $prefix", e) + continue + } + for (link in links) { + try { + if (!Files.isSymbolicLink(link)) continue + val guest = guestLinkTarget(Files.readSymbolicLink(link).toString(), root.path, binds) ?: continue + val temp = link.resolveSibling(".relink-" + link.fileName) + Files.deleteIfExists(temp) + Files.createSymbolicLink(temp, java.nio.file.Paths.get(guest)) + Files.move(temp, link, java.nio.file.StandardCopyOption.REPLACE_EXISTING) + rewritten++ + } catch (_: java.io.IOException) { + } + } + } + return rewritten + } + + fun builder(root: File, cwd: String, argv: List, env: Map, preload: List = emptyList()): ProcessBuilder { + val program = argv.first() + val cmd = command(root, program, argv.drop(1)) + val builder = ProcessBuilder(cmd).redirectErrorStream(false) + builder.directory(File(root, cwd.removePrefix("/")).takeIf { it.isDirectory } ?: root) + builder.environment().apply { + clear() + putAll(environment(root, preload)) + putAll(env) + } + return builder + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/ProotFastPath.kt b/app/src/main/java/com/droiddeck/launcher/runtime/ProotFastPath.kt index e9a9f4a72e..11265bd8ae 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/ProotFastPath.kt +++ b/app/src/main/java/com/droiddeck/launcher/runtime/ProotFastPath.kt @@ -12,15 +12,16 @@ object ProotFastPath { const val LIBRARY = "/usr/local/lib/libblfastpath.so" const val MAX_BINDS = 256 - fun allows(preference: Boolean, noSeccomp: Boolean, source: String): Boolean = - preference && !noSeccomp && source == LinuxRuntime.PROOT_PACK + fun allows(preference: Boolean, noSeccomp: Boolean, source: String, nativeUserland: Boolean = false): Boolean = + preference && !noSeccomp && !nativeUserland && source == LinuxRuntime.PROOT_PACK fun enabled(context: Context): Boolean { val preference = SessionPrefs.prootFastPath(context) val noSeccomp = SessionPrefs.prootNoSeccomp(context) + val nativeUserland = SessionPrefs.nativeUserland(context) val source = LinuxRuntime.prootSource(context) - val on = allows(preference, noSeccomp, source) - if (preference && !noSeccomp && !on) + val on = allows(preference, noSeccomp, source, nativeUserland) + if (preference && !noSeccomp && !nativeUserland && !on) Log.i(TAG, "proot: fast path off - binary is $source, not the patched proot") return on } diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/RootSpawner.kt b/app/src/main/java/com/droiddeck/launcher/runtime/RootSpawner.kt index 1138bab0de..a96ff46f38 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/RootSpawner.kt +++ b/app/src/main/java/com/droiddeck/launcher/runtime/RootSpawner.kt @@ -113,20 +113,26 @@ object RootSpawner { val argv = request.getJSONArray("argv").let { a -> (0 until a.length()).map { a.getString(it) } } if (argv.isEmpty()) throw IOException("no command") val cwd = request.optString("cwd", "/root").ifEmpty { "/root" } - val cmd = LinuxRuntime.prootPrefix(context, root, cwd, true) - cmd.add(1, "--link2symlink") - LinuxRuntime.lastBinds(context).filterNot { it.startsWith("/proc/self/fd/") }.forEach { cmd.add("-b"); cmd.add(it) } - cmd.add("/usr/bin/env") - cmd.add("-i") - request.optJSONObject("env")?.let { e -> e.keys().forEach { k -> cmd.add("$k=${e.getString(k)}") } } - cmd.addAll(argv) - val builder = ProcessBuilder(cmd).directory(root) - builder.environment().apply { - clear() - put("PROOT_LOADER", LinuxRuntime.prootLoader(context).path) - put("PROOT_TMP_DIR", LinuxRuntime.prootTempDir(context).path) - if (SessionPrefs.prootNoSeccomp(context)) put("PROOT_NO_SECCOMP", "1") - LinuxRuntime.prootLibraryPath(context).takeIf { it.isNotEmpty() }?.let { put("LD_LIBRARY_PATH", it) } + val binds = LinuxRuntime.lastBinds(context).filterNot { it.startsWith("/proc/self/fd/") } + val guest = mutableListOf("/usr/bin/env", "-i") + request.optJSONObject("env")?.let { e -> e.keys().forEach { k -> guest.add("$k=${e.getString(k)}") } } + guest.addAll(argv) + val builder = if (NativeGuest.forSession(context)) { + NativeGuest.processBuilder(context, root, binds, guest, cwd) + } else { + val cmd = LinuxRuntime.prootPrefix(context, root, cwd, true) + cmd.add(1, "--link2symlink") + binds.forEach { cmd.add("-b"); cmd.add(it) } + cmd.addAll(guest) + ProcessBuilder(cmd).directory(root).also { b -> + b.environment().apply { + clear() + put("PROOT_LOADER", LinuxRuntime.prootLoader(context).path) + put("PROOT_TMP_DIR", LinuxRuntime.prootTempDir(context).path) + if (SessionPrefs.prootNoSeccomp(context)) put("PROOT_NO_SECCOMP", "1") + LinuxRuntime.prootLibraryPath(context).takeIf { it.isNotEmpty() }?.let { put("LD_LIBRARY_PATH", it) } + } + } } val streams = listOf(stdio(fds.getOrNull(0), false), stdio(fds.getOrNull(1), true), stdio(fds.getOrNull(2), true)) try { diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/UserApps.kt b/app/src/main/java/com/droiddeck/launcher/runtime/UserApps.kt index f40443e028..cd16d03b42 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/UserApps.kt +++ b/app/src/main/java/com/droiddeck/launcher/runtime/UserApps.kt @@ -35,6 +35,7 @@ object UserApps { private const val GITHUB_REPO = "github-repo" private const val GITHUB_TAG = "github-tag" private const val GITHUB_PUBLISHED = "github-published" + private const val GITHUB_ASSET = "github-asset" enum class Kind { SCRIPT, APPIMAGE, FLATPAK } @@ -56,7 +57,7 @@ object UserApps { sealed class Source { class Script(val file: File) : Source() class AppImage(val file: File) : Source() - class GitHub(val repo: String) : Source() + class GitHub(val repo: String, val asset: String? = null) : Source() class Flatpak(val id: String) : Source() class FlatpakBundle(val file: File) : Source() } @@ -113,7 +114,7 @@ object UserApps { when (val s = request.source) { is Source.Script -> addScript(context, s.file, name, icon, request.meta, onProgress) is Source.AppImage -> AppImageManager.import(context, s.file, name, icon, request.meta) { onProgress(it, -1) } - is Source.GitHub -> addFromGitHub(context, s.repo, name, icon, onProgress) + is Source.GitHub -> addFromGitHub(context, s.repo, s.asset, name, icon, onProgress) is Source.Flatpak -> addFlatpak(context, s.id, name, icon, onProgress) is Source.FlatpakBundle -> addFlatpakBundle(context, s.file, name, icon, onProgress) } @@ -305,14 +306,26 @@ object UserApps { return if (repo.isEmpty() || repo == "." || repo == "..") null else "${m.groupValues[1]}/$repo" } - internal fun pickAsset(names: List): String? { + fun githubAsset(input: String): String? = + Regex("github\\.com/[\\w.-]+/[\\w.-]+/releases/(?:download/[^/]+|latest/download)/([^/?#]+\\.appimage)(?:[?#].*)?$", RegexOption.IGNORE_CASE) + .find(input.trim())?.groupValues?.get(1)?.let { runCatching { java.net.URLDecoder.decode(it, "UTF-8") }.getOrNull() } + + private fun assetVariant(name: String): Set = + name.lowercase().replace(Regex("x86[_-]64|amd64"), "x64").replace(Regex("x86[_-]32|i[36]86"), "x32") + .split(Regex("[^a-z0-9]+")).filter { it.isNotEmpty() && !Regex("[0-9a-f]{7,}|v?[0-9]+").matches(it) }.toSet() + + internal fun pickAsset(names: List, wanted: String? = null): String? { val images = names.filter { it.endsWith(".appimage", ignoreCase = true) } + if (wanted != null) { + images.firstOrNull { it == wanted }?.let { return it } + images.firstOrNull { assetVariant(it) == assetVariant(wanted) }?.let { return it } + } return images.firstOrNull { Regex("aarch64|arm64", RegexOption.IGNORE_CASE).containsMatchIn(it) } ?: images.firstOrNull { !Regex("x86_64|x86-64|amd64|x64|i[36]86|armhf|armv7", RegexOption.IGNORE_CASE).containsMatchIn(it) } ?: images.firstOrNull { Regex("x86_64|x86-64|amd64|x64", RegexOption.IGNORE_CASE).containsMatchIn(it) } } - private fun latestRelease(context: Context, repo: String): Pair { + private fun latestRelease(context: Context, repo: String, wanted: String? = null): Pair { val body = Downloader.downloadString("https://api.github.com/repos/$repo/releases?per_page=15") val releases = body?.let { runCatching { JSONArray(it) }.getOrNull() } ?: return null to context.getString(R.string.user_apps_github_unreachable, repo) @@ -321,7 +334,7 @@ object UserApps { if (r.optBoolean("draft")) return@mapNotNull null val assets = r.optJSONArray("assets") ?: return@mapNotNull null val byName = (0 until assets.length()).associate { j -> assets.getJSONObject(j).let { it.getString("name") to it } } - pickAsset(byName.keys.toList())?.let { asset -> + pickAsset(byName.keys.toList(), wanted)?.let { asset -> r.optBoolean("prerelease") to byName.getValue(asset).let { a -> Release(r.optString("tag_name", asset), r.optString("published_at"), asset, a.getString("browser_download_url"), Hashes.githubSha256(a.optString("digest"))) @@ -344,11 +357,11 @@ object UserApps { } private fun githubMeta(repo: String, release: Release) = - mapOf(GITHUB_REPO to repo, GITHUB_TAG to release.tag, GITHUB_PUBLISHED to release.published) + mapOf(GITHUB_REPO to repo, GITHUB_TAG to release.tag, GITHUB_PUBLISHED to release.published, GITHUB_ASSET to release.asset) - private fun addFromGitHub(context: Context, repo: String, name: String?, icon: File?, onProgress: (String, Int) -> Unit): String? { + private fun addFromGitHub(context: Context, repo: String, asset: String?, name: String?, icon: File?, onProgress: (String, Int) -> Unit): String? { onProgress(context.getString(R.string.user_apps_github_checking, repo), -1) - val (release, problem) = latestRelease(context, repo) + val (release, problem) = latestRelease(context, repo, asset) if (release == null) return problem return withDownload(context, release, onProgress) { file -> AppImageManager.import(context, file, name ?: repo.substringAfter('/'), icon, githubMeta(repo, release)) { onProgress(it, -1) } @@ -381,9 +394,10 @@ object UserApps { fun checkUpdate(context: Context, app: App): UpdateCheck { val repo = app.repo ?: return UpdateCheck.Failed(context.getString(R.string.user_apps_update_failed)) - val (release, problem) = latestRelease(context, repo) + val dir = AppImageManager.dir(context, app.key.substringAfter(':')) + val (release, problem) = latestRelease(context, repo, FileUtils.readString(File(dir, GITHUB_ASSET))?.trim()?.takeIf { it.isNotEmpty() }) if (release == null) return UpdateCheck.Failed(problem ?: context.getString(R.string.user_apps_update_failed)) - val published = FileUtils.readString(File(AppImageManager.dir(context, app.key.substringAfter(':')), GITHUB_PUBLISHED))?.trim().orEmpty() + val published = FileUtils.readString(File(dir, GITHUB_PUBLISHED))?.trim().orEmpty() val newer = if (published.isNotEmpty() && release.published.isNotEmpty()) release.published > published else release.tag != app.version return if (newer) UpdateCheck.Available(release) else UpdateCheck.Current(app.version ?: release.tag) } diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/WowAddonCatalog.kt b/app/src/main/java/com/droiddeck/launcher/runtime/WowAddonCatalog.kt new file mode 100644 index 0000000000..63c1436f14 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/runtime/WowAddonCatalog.kt @@ -0,0 +1,245 @@ +package com.droiddeck.launcher.runtime + +import android.content.Context +import android.util.Log +import com.droiddeck.launcher.R +import com.droiddeck.launcher.core.Downloader +import com.droiddeck.launcher.core.Hashes +import com.droiddeck.launcher.stores.StoreNet +import org.json.JSONArray +import java.io.File +import java.io.IOException +import java.net.URLEncoder + +object WowAddonCatalog { + private const val TAG = "WowAddonCatalog" + enum class Sort { POPULAR, UPDATED, NAME, DOWNLOADS } + + class Category(val id: String, val name: String) + + class Client(val folder: String, val family: String?) + + class Query(val text: String = "", val category: String? = null, val client: Client, val sort: Sort = Sort.POPULAR) + + class Addon( + val id: String, + val name: String, + val author: String, + val summary: String, + val downloads: Long, + val updated: Long, + val thumb: String?, + ) + + class Page(val items: List, val more: Boolean) + + internal class Entry(val addon: Addon, val category: String, val flavors: Set?, val monthly: Long = 0L) + + internal class Cat(val id: String, val name: String, val parents: List) + + private const val WOWI = "https://api.mmoui.com/v3/game/WOW/" + private const val CURSEFORGE = "https://www.curseforge.com/wow/search?class=addons" + private const val WOWI_FILES = "wowi-filelist.json" + internal const val FRESH_MS = 6L * 60 * 60 * 1000 + + internal const val RETAIL = "_retail_" + internal const val ERA = "_classic_era_" + internal const val CLASSIC = "_classic_" + internal const val ANNIVERSARY = "_anniversary_" + internal const val FOREVER = "_forever_" + + private val TEST_SUFFIX = Regex("_(beta|ptr|xptr)_$") + + internal fun flavorOf(version: String): String? { + val major = version.substringBefore('.').trim().toIntOrNull() ?: return null + return when { + major == 1 -> ERA + major == 2 -> ANNIVERSARY + major in 3..5 -> CLASSIC + major >= 10 -> RETAIL + else -> null + } + } + + internal fun baseFolder(folder: String): String = folder.replace(TEST_SUFFIX, "_") + + internal fun folderFamily(folder: String): String? = when (val base = baseFolder(folder)) { + FOREVER -> RETAIL + RETAIL, ERA, CLASSIC, ANNIVERSARY -> base + else -> null + } + + internal fun buildInfoVersion(buildInfo: String, product: String): String? { + val lines = buildInfo.lineSequence().map { it.trim() }.filter { it.isNotEmpty() }.toList() + if (lines.isEmpty()) return null + val header = lines.first().split('|').map { it.substringBefore('!').trim().lowercase() } + val productAt = header.indexOf("product") + val versionAt = header.indexOf("version") + if (productAt < 0 || versionAt < 0) return null + return lines.drop(1).map { it.split('|') } + .firstOrNull { it.getOrNull(productAt)?.trim().equals(product, ignoreCase = true) } + ?.getOrNull(versionAt)?.trim()?.ifEmpty { null } + } + + internal fun flavorProduct(flavorInfo: String): String? = + flavorInfo.lineSequence().map { it.trim() }.filter { it.isNotEmpty() && '!' !in it }.firstOrNull() + + internal fun clientVersion(flavorDir: File): String? { + val product = runCatching { File(flavorDir, ".flavor.info").readText() }.getOrNull()?.let(::flavorProduct) ?: return null + val info = runCatching { File(flavorDir.parentFile, ".build.info").readText() }.getOrNull() ?: return null + return buildInfoVersion(info, product) + } + + internal fun client(folder: String, version: String?): Client = + Client(folder, version?.let(::flavorOf) ?: folderFamily(folder)) + + fun client(target: WowAddons.Target): Client { + val dir = target.addOns.parentFile?.parentFile + return client(target.flavor, dir?.let(::clientVersion)) + } + + internal fun flavorMatches(flavors: Set?, family: String?): Boolean = + flavors == null || family == null || family in flavors + + internal fun parseWowiFiles(json: String): List { + val arr = JSONArray(json) + val out = ArrayList(arr.length()) + for (i in 0 until arr.length()) { + val o = arr.optJSONObject(i) ?: continue + val id = o.optString("UID") + if (id.isEmpty()) continue + val compat = o.optJSONArray("UICompatibility") + val flavors = if (compat == null || compat.length() == 0) null else { + val set = HashSet() + for (k in 0 until compat.length()) compat.optJSONObject(k)?.optString("version")?.let(::flavorOf)?.let(set::add) + set + } + val thumbs = o.optJSONArray("UIIMG_Thumbs") + val thumb = if (thumbs != null && thumbs.length() > 0) thumbs.optString(0).ifEmpty { null } else null + val addon = Addon( + id, o.optString("UIName"), o.optString("UIAuthorName"), o.optString("UIVersion"), + o.optString("UIDownloadTotal").toLongOrNull() ?: 0L, o.optLong("UIDate"), thumb, + ) + out.add(Entry(addon, o.optString("UICATID"), flavors, o.optString("UIDownloadMonthly").toLongOrNull() ?: 0L)) + } + return out + } + + internal fun parseWowiCategories(json: String): List { + val arr = JSONArray(json) + val out = ArrayList(arr.length()) + for (i in 0 until arr.length()) { + val o = arr.optJSONObject(i) ?: continue + val parents = o.optJSONArray("UICATParentIDs") + out.add(Cat(o.optString("UICATID"), o.optString("UICATTitle"), (0 until (parents?.length() ?: 0)).map { parents!!.optString(it) })) + } + return out + } + + internal fun filterWowi(entries: List, cats: List, query: Query): List { + val parents = cats.associate { it.id to it.parents } + val text = query.text.trim().lowercase() + val list = entries.filter { e -> + flavorMatches(e.flavors, query.client.family) && + (query.category == null || e.category == query.category || query.category in (parents[e.category] ?: emptyList())) && + (text.isEmpty() || e.addon.name.lowercase().contains(text) || e.addon.author.lowercase().contains(text)) + } + return when (query.sort) { + Sort.POPULAR -> list.sortedWith(compareByDescending { it.monthly }.thenByDescending { it.addon.downloads }) + Sort.DOWNLOADS -> list.sortedByDescending { it.addon.downloads } + Sort.UPDATED -> list.sortedByDescending { it.addon.updated } + Sort.NAME -> list.sortedBy { it.addon.name.lowercase() } + }.map { it.addon } + } + + internal fun pageOf(all: List, page: Int, pageSize: Int): Page { + val from = (page * pageSize).coerceAtMost(all.size) + val to = (from + pageSize).coerceAtMost(all.size) + return Page(all.subList(from, to), to < all.size) + } + + internal fun cached(file: File, now: Long, fetch: () -> String?): String? { + val kept = if (file.isFile) file.readText().takeIf(::isJsonArray) else null + if (kept != null && isFresh(file, now)) return kept + val body = fetch()?.takeIf(::isJsonArray) + if (body != null) { + val part = File(file.parentFile, "${file.name}.${java.util.UUID.randomUUID()}.part") + runCatching { + file.parentFile?.mkdirs() + part.writeText(body) + part.setLastModified(now) + if (!part.renameTo(file)) Log.w(TAG, "could not keep ${file.name}") + }.onFailure { Log.w(TAG, "could not keep ${file.name}", it) } + part.delete() + return body + } + return kept + } + + private fun isJsonArray(body: String): Boolean = + runCatching { org.json.JSONTokener(body).nextValue() }.getOrNull() is JSONArray + + private fun isFresh(file: File, now: Long) = file.isFile && now - file.lastModified() < FRESH_MS + + private fun cacheFile(context: Context, name: String) = File(File(context.cacheDir, "wowaddons"), name) + + private fun cachedGet(context: Context, name: String, url: String): String? = + cached(cacheFile(context, name), System.currentTimeMillis()) { StoreNet.get(url, timeoutMs = 60_000) } + + private fun unreachable(context: Context) = + IOException(context.getString(R.string.addons_unreachable, context.getString(R.string.addons_source_wowi))) + + fun search(context: Context, query: Query, page: Int, pageSize: Int = 40): Page = + pageOf(filterWowi(wowiEntries(context), wowiCategories(context), query), page, pageSize) + + private class Memo(val stamp: Long, val size: Long, val entries: List) + + @Volatile private var wowiMemo: Memo? = null + + private fun wowiEntries(context: Context): List { + val file = cacheFile(context, WOWI_FILES) + val memo = wowiMemo + if (memo != null && isFresh(file, System.currentTimeMillis()) && memo.stamp == file.lastModified() && memo.size == file.length()) return memo.entries + val body = cachedGet(context, WOWI_FILES, WOWI + "filelist.json") ?: throw unreachable(context) + return parseWowiFiles(body).also { wowiMemo = Memo(file.lastModified(), file.length(), it) } + } + + private fun wowiCategories(context: Context): List = + cachedGet(context, "wowi-categories.json", WOWI + "categorylist.json")?.let(::parseWowiCategories) ?: emptyList() + + fun categories(context: Context): List = wowiCategories(context).map { Category(it.id, it.name) } + + internal fun parseWowiDetails(json: String): Pair? { + val o = JSONArray(json).optJSONObject(0) ?: return null + val url = o.optString("UIDownload").ifEmpty { return null } + return url to o.optString("UIMD5").ifEmpty { null } + } + + internal fun isHttps(url: String) = url.startsWith("https://", ignoreCase = true) + + internal fun md5Matches(file: File, expected: String?): Boolean = + expected.isNullOrBlank() || Hashes.md5(file).equals(expected.trim(), ignoreCase = true) + + fun download(context: Context, addon: Addon, dest: File, progress: (Int) -> Unit): File { + val body = StoreNet.get(WOWI + "filedetails/${URLEncoder.encode(addon.id, "UTF-8")}.json", timeoutMs = 30_000) + ?: throw unreachable(context) + val (url, md5) = parseWowiDetails(body) ?: throw IOException(context.getString(R.string.addons_no_file)) + if (!isHttps(url)) throw IOException(context.getString(R.string.addons_no_file)) + dest.parentFile?.mkdirs() + dest.delete() + val ok = Downloader.downloadFile(url, dest, false) { f -> if (f >= 0) progress((f * 100).toInt().coerceIn(0, 100)) } + if (!ok || !dest.isFile) { + dest.delete() + throw unreachable(context) + } + if (!md5Matches(dest, md5)) { + dest.delete() + throw IOException(context.getString(R.string.selfinst_checksum_mismatch)) + } + progress(100) + return dest + } + + internal fun curseForgeUrl(text: String): String = + text.trim().let { if (it.isEmpty()) CURSEFORGE else CURSEFORGE + "&search=" + URLEncoder.encode(it, "UTF-8") } +} diff --git a/app/src/main/java/com/droiddeck/launcher/session/BottomDesktop.kt b/app/src/main/java/com/droiddeck/launcher/session/BottomDesktop.kt index 36b9c3dfdc..10d031715b 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/BottomDesktop.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/BottomDesktop.kt @@ -39,6 +39,7 @@ object BottomDesktop { val sessionDir: File, val generation: Int, val installed: Boolean, + val nativeUserland: Boolean, ) @Volatile private var launch: Launch? = null @@ -89,10 +90,11 @@ object BottomDesktop { root: File, sessionDir: File, generation: Int, + nativeUserland: Boolean, stillCurrent: () -> Boolean, ) { val prepared = Launch(ArrayList(guest), ArrayList(binds), sessionRoot, hostEnvironment.copyOf(), root, sessionDir, generation, - DesktopCatalog.desktopInstalled(root)) + DesktopCatalog.desktopInstalled(root), nativeUserland) synchronized(launchLock) { if (stillCurrent()) launch = prepared } } @@ -320,9 +322,17 @@ object BottomDesktop { add("BL_IME_DIR=" + imeDir.path) add(SCRIPT) } - val command = LinuxRuntime.commandWithBinds(context, current.root, binds, guest) + val command = if (current.nativeUserland) { + com.droiddeck.launcher.runtime.NativeGuest.command(current.root, guest.first(), guest.drop(1)) + } else { + LinuxRuntime.commandWithBinds(context, current.root, binds, guest) + } + val environment = if (current.nativeUserland) { + com.droiddeck.launcher.runtime.NativeGuest.sessionEnvironment(current.root, binds, SessionPrefs.guestHostname(context)) + .map { (k, v) -> "$k=$v" }.toTypedArray() + } else current.hostEnvironment val line = command.joinToString(" ") { it.replace("\\", "\\\\").replace(" ", "\\ ") } - val pid = HostProcess.start(line, current.hostEnvironment, current.root, { status -> + val pid = HostProcess.start(line, environment, current.root, { status -> main.post { if (generation != guestGeneration) return@post Log.i(TAG, "the desktop ended ($status)") diff --git a/app/src/main/java/com/droiddeck/launcher/session/BottomNotifications.kt b/app/src/main/java/com/droiddeck/launcher/session/BottomNotifications.kt deleted file mode 100644 index 45e0890c52..0000000000 --- a/app/src/main/java/com/droiddeck/launcher/session/BottomNotifications.kt +++ /dev/null @@ -1,60 +0,0 @@ -package com.droiddeck.launcher.session - -import android.app.Notification -import android.content.Context -import android.service.notification.NotificationListenerService -import android.service.notification.StatusBarNotification -import androidx.compose.runtime.mutableStateListOf -import androidx.core.app.NotificationManagerCompat - -data class BottomNotification(val key: String, val app: String, val title: String, val text: String, val postedAt: Long) - -object BottomNotifications { - val items = mutableStateListOf() - - fun granted(context: Context): Boolean = - context.packageName in NotificationManagerCompat.getEnabledListenerPackages(context) - - internal fun keep(own: String, pkg: String, ongoing: Boolean, groupSummary: Boolean, title: String, text: String): Boolean = - pkg != own && !ongoing && !groupSummary && (title.isNotBlank() || text.isNotBlank()) - - internal fun add(list: MutableList, item: BottomNotification) { - list.removeAll { it.key == item.key } - list.add(0, item) - while (list.size > MAX_ITEMS) list.removeAt(list.lastIndex) - } - - fun clear() = items.clear() - - internal fun post(context: Context, sbn: StatusBarNotification) { - val n = sbn.notification ?: return - val extras = n.extras - val title = extras?.getCharSequence(Notification.EXTRA_TITLE)?.toString().orEmpty() - val text = (extras?.getCharSequence(Notification.EXTRA_BIG_TEXT) ?: extras?.getCharSequence(Notification.EXTRA_TEXT))?.toString().orEmpty() - if (!keep(context.packageName, sbn.packageName, sbn.isOngoing, (n.flags and Notification.FLAG_GROUP_SUMMARY) != 0, title, text)) return - val app = runCatching { - val pm = context.packageManager - pm.getApplicationLabel(pm.getApplicationInfo(sbn.packageName, 0)).toString() - }.getOrDefault(sbn.packageName) - add(items, BottomNotification(sbn.key, app, title, text, sbn.postTime)) - } - - internal fun remove(key: String) { - items.removeAll { it.key == key } - } - - private const val MAX_ITEMS = 30 -} - -class BottomNotificationListener : NotificationListenerService() { - override fun onListenerConnected() { - BottomNotifications.clear() - runCatching { activeNotifications }.getOrNull()?.sortedBy { it.postTime }?.forEach { BottomNotifications.post(this, it) } - } - - override fun onNotificationPosted(sbn: StatusBarNotification) = BottomNotifications.post(this, sbn) - - override fun onNotificationRemoved(sbn: StatusBarNotification) = BottomNotifications.remove(sbn.key) - - override fun onListenerDisconnected() = BottomNotifications.clear() -} diff --git a/app/src/main/java/com/droiddeck/launcher/session/ComponentUpdater.kt b/app/src/main/java/com/droiddeck/launcher/session/ComponentUpdater.kt index 05f89fce4a..2f7b3f17c2 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/ComponentUpdater.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/ComponentUpdater.kt @@ -5,15 +5,17 @@ import android.util.AtomicFile import android.util.Log import com.droiddeck.launcher.R import com.droiddeck.launcher.frontend.AutoComponents +import com.droiddeck.launcher.gpu.GpuInfo +import java.io.File import org.json.JSONArray import org.json.JSONObject -import java.io.File object ComponentUpdater { private const val TAG = "ComponentUpdater" private const val CHECK_EVERY_MS = 3_600_000L private const val BOOTED_KEPT = 200 private const val HANDED_BACK = "handedBack" + private const val PINNED_NOTE = "pinnedNote" internal const val HAND_BACK_FILES = "handBackFiles" private const val IMPORTED = "imported" private val lock = Any() @@ -103,6 +105,13 @@ object ComponentUpdater { val protons = found ?: emptyList() if (found != null) handBack(context, state, found) judge(context, state, protons, notes) + ComponentsManager.pinnedAutoDxvk(context)?.let { pinned -> + runCatching { ComponentsManager.ensureForcedPackages(context) } + .onSuccess { if (it != null) Log.i(TAG, it) }.onFailure { Log.w(TAG, "pinned DXVK", it) } + val followers = protons.filter { ComponentUpdates.follows(it) && ComponentsManager.supports(it, "dxvk") }.map { it.id to it.version } + if (pinnedHeld(pinned, followers, ComponentsManager.activeChoices(context), pinnedBase())) state.remove(PINNED_NOTE) + else state.put(PINNED_NOTE, context.getString(R.string.comp_auto_install_failed, pinned.removeSuffix(".wcp"))) + } val due = force || System.currentTimeMillis() - state.optLong("checkedAt") >= CHECK_EVERY_MS val wanted = ComponentsManager.COMPONENTS.filter { managed(context, it) } if (wanted.isEmpty() || protons.isEmpty()) { @@ -385,7 +394,32 @@ object ComponentUpdater { } } + internal fun pinnedHeld(pinned: String, followers: List>, choices: Map>, base: String?): Boolean = + followers.all { (id, version) -> + val choice = choices[id]?.get("dxvk") + ownChoice(choice, version, pinned, base) || choice?.let { it.file == ComponentsManager.safeName(pinned) && it.protonVersion == version } == true + } + + internal fun ownChoice(choice: ComponentsManager.Choice?, version: String, pinned: String, base: String?): Boolean = + choice != null && !choice.auto && choice.protonVersion == version && choice.file != ComponentsManager.safeName(pinned) && + (base == null || choice.file != ComponentsManager.safeName(base)) + + private fun pinnedBase(): String? = ComponentsManager.forcedDxvk(GpuInfo.detect())?.file + fun status(context: Context, comp: String): List { + val pinned = if (comp == "dxvk") ComponentsManager.pinnedAutoDxvk(context) else null + if (pinned != null) { + val on = runCatching { ComponentsManager.protons(context) }.getOrDefault(emptyList()) + .filter { ComponentUpdates.follows(it) && ComponentsManager.supports(it, comp) } + if (on.isEmpty()) return emptyList() + val choices = ComponentsManager.activeChoices(context) + val base = pinnedBase() + val (own, auto) = on.partition { ownChoice(choices[it.id]?.get(comp), it.version, pinned, base) } + val held = pinnedHeld(pinned, auto.map { it.id to it.version }, choices, base) + val pinnedLine = if (auto.isEmpty()) emptyList() + else listOf(Status(comp, pinned, pinned, auto.map { it.name }, if (held) "" else load(context).optString(PINNED_NOTE))) + return pinnedLine + own.groupBy { choices.getValue(it.id).getValue(comp).file }.map { (file, ps) -> Status(comp, file, file, ps.map { it.name }, "") } + } val state = load(context) val lines = state.optJSONObject("lines") ?: JSONObject() val protons = runCatching { ComponentsManager.protons(context) }.getOrDefault(emptyList()) diff --git a/app/src/main/java/com/droiddeck/launcher/session/ComponentsManager.kt b/app/src/main/java/com/droiddeck/launcher/session/ComponentsManager.kt index 3c1c92c0d1..e9409b74cd 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/ComponentsManager.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/ComponentsManager.kt @@ -1065,14 +1065,24 @@ object ComponentsManager { val file: String, val release: String, val models: Set = emptySet(), + val autoFile: String? = null, + val autoRelease: String? = null, ) { fun matches(gpu: GpuInfo) = gpu.family in families && (models.isEmpty() || gpu.model in models) + + fun autoRule(): ForcedPackage? = + if (autoFile != null && autoRelease != null) copy(file = autoFile, release = autoRelease, autoFile = null, autoRelease = null) else null } internal val FORCED_PACKAGES = listOf( - ForcedPackage(setOf(GpuInfo.Family.A6XX), "dxvk", "dxvk-2.7.1-linux.wcp", "Dxvk-Linux"), + ForcedPackage(setOf(GpuInfo.Family.A6XX), "dxvk", "dxvk-2.7.1-linux.wcp", "Dxvk-Linux", + autoFile = "dxvk-gplasync-2.7.1-1-linux.wcp", autoRelease = "Dxvk-gplasync-Linux"), ) + internal fun autoDxvk(gpu: GpuInfo): ForcedPackage? = forcedDxvk(gpu)?.autoRule() + + internal fun forcedDxvk(gpu: GpuInfo): ForcedPackage? = FORCED_PACKAGES.firstOrNull { it.comp == "dxvk" && it.matches(gpu) } + private val ARM64EC_PROBE = mapOf("dxvk" to "d3d11.dll", "vkd3d" to "d3d12.dll") fun arm64ec(context: Context, p: Proton, comp: String): Boolean? = stockProbe(context, p, comp)?.let(ComponentUpdates::arm64ec) @@ -1124,6 +1134,12 @@ object ComponentsManager { (com.droiddeck.launcher.core.DeviceSupport.vkBridge(context) && !SessionPrefs.bridgeProtonDxvk(context) && bridgeDxvk(AppVulkan.detect(context)) != null) + fun autoDxvkHeld(context: Context): Boolean = + hardwareDxvk(context) && (runtimeManaged(context, "dxvk") || autoDxvk(GpuInfo.detect()) == null) + + fun pinnedAutoDxvk(context: Context): String? = + if (SessionPrefs.componentMode(context, "dxvk") == ComponentUpdates.AUTO) autoDxvk(GpuInfo.detect())?.file else null + data class Choice(val file: String, val auto: Boolean, val protonVersion: String) fun activeChoices(context: Context): Map> = synchronized(lock) { @@ -1168,6 +1184,14 @@ object ComponentsManager { internal const val DXVK_FOR_VULKAN_1_1 = "dxvk-1.10.3-linux.wcp" internal const val DXVK_FOR_VULKAN_1_3_NO_INT64 = "dxvk-2.6.2-linux.wcp" + private data class ForcedRule( + val pkg: ForcedPackage, + val auto: Boolean, + val covers: (Proton) -> Boolean, + val replaces: String? = null, + val label: String? = null, + ) + internal fun autoDxvkUndoWanted(bridgeSession: Boolean, keepProtonDxvk: Boolean, vulkan: AppVulkan?, bridgeRule: ForcedPackage?): Boolean = !bridgeSession || keepProtonDxvk || (vulkan != null && bridgeRule == null) @@ -1200,6 +1224,7 @@ object ComponentsManager { undoAuto = { id, comp -> undoAutoChoice(context, id, comp) }, keepProtonDxvk = SessionPrefs.bridgeProtonDxvk(context), autoPossible = !com.droiddeck.launcher.core.DeviceSupport.adreno(), + dxvkAuto = pinnedAutoDxvk(context) != null, ) } fun settleAutoDxvk(context: Context) { @@ -1239,10 +1264,16 @@ object ComponentsManager { undoAuto: (protonId: String, comp: String) -> String = { _, _ -> "" }, keepProtonDxvk: Boolean = false, autoPossible: Boolean = true, + dxvkAuto: Boolean = false, + follows: (Proton) -> Boolean = ComponentUpdates::follows, ): String? { val chooseDxvk = bridgeSession && !keepProtonDxvk val bridgeRule = if (chooseDxvk) bridgeDxvk(vulkan) else null - val rules = FORCED_PACKAGES.filter { it.matches(gpu) }.map { it to false } + listOfNotNull(bridgeRule?.let { it to true }) + val family = FORCED_PACKAGES.filter { it.matches(gpu) } + val pinned = if (dxvkAuto) family.mapNotNull { base -> base.autoRule()?.let { base to it } } else emptyList() + val rules = family.map { base -> ForcedRule(base, false, if (pinned.any { it.first == base }) { p -> !follows(p) } else { _ -> true }) } + + pinned.map { (base, rule) -> ForcedRule(rule, true, follows, replaces = base.file) } + + listOfNotNull(bridgeRule?.let { ForcedRule(it, true, { true }, label = "${vulkan?.label}: DXVK ${it.file}") }) if (rules.isEmpty() && !autoPossible) return null val installed = protons(context) val lines = mutableListOf() @@ -1256,12 +1287,13 @@ object ComponentsManager { if (autoChoiceStale(entry, null)) lines.add(each(p) { undoAuto(p.id, "dxvk") }) } } - for ((rule, auto) in rules) { + for ((rule, auto, covers, replaces, label) in rules) { val state = loadState(context) val targets = installed.filter { p -> val active = state.optJSONObject("active")?.optJSONObject(p.id)?.optJSONObject(rule.comp) - forcedPackageWanted(active?.optString("file"), active?.optString("protonVersion") == p.version) || - (auto && autoChoiceStale(active, rule.file)) + val current = active?.optString("protonVersion") == p.version + covers(p) && (forcedPackageWanted(active?.optString("file"), current) || + (auto && autoChoiceStale(active, rule.file)) || (replaces != null && current && active?.optString("file") == replaces)) } if (targets.isEmpty()) continue val missing = missingPackage(context, rule, lookup, fetch) @@ -1269,7 +1301,7 @@ object ComponentsManager { lines.add(missing) continue } - if (auto) lines.add("${vulkan?.label}: DXVK ${rule.file}") + if (label != null) lines.add(label) lines.add(targets.joinToString("; ") { p -> if (auto) each(p) { applyAuto(p.id, rule.file) } else apply(p.id, rule.file) }) } return lines.filter { it.isNotEmpty() }.joinToString("; ").ifEmpty { null } diff --git a/app/src/main/java/com/droiddeck/launcher/session/GameStorage.kt b/app/src/main/java/com/droiddeck/launcher/session/GameStorage.kt index 729533d5a2..5bf25103b0 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/GameStorage.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/GameStorage.kt @@ -57,8 +57,10 @@ object GameStorage { return pref.isNotEmpty() && pref != SessionPrefs.GAME_STORAGE_OFF && !attached(File(pref)) } - internal fun attached(path: File): Boolean = - attached(path, runCatching { Environment.getExternalStorageState(path) }.getOrNull()) + internal fun attached( + path: File, + stateOf: (File) -> String? = { runCatching { Environment.getExternalStorageState(it) }.getOrNull() }, + ): Boolean = attached(path, stateOf(File(UsbLink.storagePath(path.path)))) internal fun attached( path: File, @@ -66,8 +68,9 @@ object GameStorage { externalStoragePath: Boolean = isExternalStoragePath(path), ): Boolean = when { state == Environment.MEDIA_MOUNTED || state == Environment.MEDIA_MOUNTED_READ_ONLY -> true + path.isDirectory -> true externalStoragePath -> false - state == Environment.MEDIA_UNKNOWN || state == null -> path.isDirectory || path.parentFile?.isDirectory == true + state == Environment.MEDIA_UNKNOWN || state == null -> path.parentFile?.isDirectory == true else -> false } diff --git a/app/src/main/java/com/droiddeck/launcher/session/ProtonExtras.kt b/app/src/main/java/com/droiddeck/launcher/session/ProtonExtras.kt index 962291d2c7..94334f4dc9 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/ProtonExtras.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/ProtonExtras.kt @@ -48,11 +48,19 @@ object ProtonExtras { internal fun installedIn(dir: File, tool: Tool): List = dir.listFiles() - ?.filter { it.isDirectory && it.name.startsWith(tool.prefix) && File(it, "toolmanifest.vdf").isFile } + ?.filter { it.isDirectory && it.name.startsWith(tool.prefix) && usable(it) } ?.map { it.name } ?.sortedWith { a, b -> compareVersions(a, b) } ?: emptyList() + private fun usable(dir: File): Boolean { + val manifest = runCatching { File(dir, "toolmanifest.vdf").readText() }.getOrNull() ?: return false + val command = Regex("\\\"commandline\\\"\\s+\\\"/?([^\\\" ]+)").find(manifest)?.groupValues?.getOrNull(1) ?: return false + val compat = runCatching { File(dir, "compatibilitytool.vdf").readText() }.getOrNull() ?: return false + return Regex("\\\"from_oslist\\\"\\s+\\\"[^\\\"]*windows", RegexOption.IGNORE_CASE).containsMatchIn(compat) && + File(dir, command).isFile && File(dir, command).canExecute() + } + private val ARCH = Regex("(?i)aarch64|arm64|x86_64|x86-64|amd64") private val NUMBER = Regex("\\d+") @@ -275,14 +283,20 @@ object ProtonExtras { "LANG=C.UTF-8", "XDG_DATA_HOME=/root/.local/share", "/usr/local/bin/droiddeck-proton-extra", "/root/.local/share/Steam", archive.absolutePath, ) - val command = LinuxRuntime.command(context, null, runtimeDir, null, guest) - val process = ProcessBuilder(command) - .directory(root) - .redirectErrorStream(true) - val hostEnv = process.environment() - hostEnv["PROOT_LOADER"] = LinuxRuntime.prootLoader(context).path - hostEnv["PROOT_TMP_DIR"] = LinuxRuntime.prootTempDir(context).path - LinuxRuntime.prootLibraryPath(context).takeIf { it.isNotEmpty() }?.let { hostEnv["LD_LIBRARY_PATH"] = it } + val process = if (com.droiddeck.launcher.runtime.NativeGuest.enabled(context)) { + com.droiddeck.launcher.runtime.NativeGuest.processBuilder(context, root, + LinuxRuntime.binds(context, null, runtimeDir, null, null), guest).redirectErrorStream(true) + } else { + ProcessBuilder(LinuxRuntime.command(context, null, runtimeDir, null, guest)) + .directory(root) + .redirectErrorStream(true) + .also { builder -> + val hostEnv = builder.environment() + hostEnv["PROOT_LOADER"] = LinuxRuntime.prootLoader(context).path + hostEnv["PROOT_TMP_DIR"] = LinuxRuntime.prootTempDir(context).path + LinuxRuntime.prootLibraryPath(context).takeIf { it.isNotEmpty() }?.let { hostEnv["LD_LIBRARY_PATH"] = it } + } + } val child = process.start() var placed: String? = null child.inputStream.bufferedReader().useLines { lines -> diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionDisplay.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionDisplay.kt index 1cb526c90d..9b253d88a7 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionDisplay.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionDisplay.kt @@ -69,8 +69,8 @@ object SessionDisplay { const val UNFOLDED_MIN_DP = 600 - fun unfoldedGameHeight(width: Int, height: Int): Int = - minOf(width * 9 / 16, (height * UNFOLDED_MAX_GAME_SHARE).toInt()) and 1.inv() + fun unfoldedGameHeight(width: Int, height: Int, tabRow: Int = 0): Int = + (minOf(width * 9 / 16, (height * UNFOLDED_MAX_GAME_SHARE).toInt()) - tabRow.coerceAtLeast(0)).coerceAtLeast(0) and 1.inv() fun unfoldedControlsWanted( enabled: Boolean, diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionEvents.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionEvents.kt index 26b63fd5cf..6c0a076456 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionEvents.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionEvents.kt @@ -14,6 +14,7 @@ object SessionEvents { SessionPaths.take() val dir = SessionPaths.beginOrCurrent(context, label ?: SessionPaths.label(mode, null)) synchronized(SessionState) { + SessionState.clearRelaunch() SessionState.phase = SessionPhase.PREPARING SessionState.mode = mode SessionState.sessionId = dir.name diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionFiles.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionFiles.kt index cb9b5391fc..8c5e7431b1 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionFiles.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionFiles.kt @@ -33,6 +33,37 @@ object SessionFiles { "etc/xdg/autostart/droiddeck-clipboard.desktop" to "etc/xdg/autostart/droiddeck-clipboard.desktop", ) + private const val UBISOFT_SERVICE = "usr/local/lib/droiddeck-ubisoft/droiddeck-upc.exe" + + /** Fetches the Ubisoft pack and, once it lands, stages the service so a running session gets it without restarting. */ + fun ensureUbisoftService(context: Context) { + AddonPack.UBISOFT.ensure(context) { app -> + com.droiddeck.launcher.runtime.LinuxRuntime.rootDir(app).takeIf { it.isDirectory } + ?.let { stageUbisoftService(app, it, null) } + } + } + + @Synchronized + private fun stageUbisoftService(context: Context, root: File, ledger: StageLedger?) { + val pack = AddonPack.UBISOFT.takeIf { it.installed(context) }?.dir(context) ?: return + val target = File(root, UBISOFT_SERVICE) + if (ledger?.current(UBISOFT_SERVICE, target) == true) return + val staged = File(target.parentFile, target.name + ".staged") + var installed = false + try { + target.parentFile?.mkdirs() + File(pack, "droiddeck-upc.exe").inputStream().use { input -> + staged.outputStream().use { output -> FileUtils.copy(input, output) } + } + installed = staged.setExecutable(true, false) && staged.renameTo(target) + } catch (e: Exception) { + Log.w(TAG, "could not stage the Ubisoft launcher service", e) + } finally { + if (!installed) staged.delete() + } + if (installed) ledger?.staged(UBISOFT_SERVICE, target) else Log.e(TAG, "$UBISOFT_SERVICE NOT staged") + } + fun stage(context: Context, root: File, startsSession: Boolean = false) { if (DesktopCatalog.desktopInstalled(root) && !DesktopCatalog.desktopFromPack(root)) try { DesktopCatalog.cleanLegacy(context) } catch (e: Exception) { Log.w(TAG, "LXQt cleanup will retry next session", e) } @@ -40,8 +71,10 @@ object SessionFiles { WinComponents.publish(context) val files = arrayOf( "usr/local/bin/droiddeck-game-env" to "usr/local/bin/droiddeck-game-env", + "usr/local/bin/droiddeck-game-gpu" to "usr/local/bin/droiddeck-game-gpu", "usr/local/bin/droiddeck-diagnostic-inventory" to "usr/local/bin/droiddeck-diagnostic-inventory", "usr/local/bin/droiddeck-wincomponents" to "usr/local/bin/droiddeck-wincomponents", + "usr/local/bin/droiddeck-wine-pe" to "usr/local/bin/droiddeck-wine-pe", "usr/local/bin/droiddeck-msi-install" to "usr/local/bin/droiddeck-msi-install", "usr/local/bin/droiddeck-esync" to "usr/local/bin/droiddeck-esync", "usr/local/bin/droiddeck-steam-compat" to "usr/local/bin/droiddeck-steam-compat", @@ -50,6 +83,7 @@ object SessionFiles { "libblsession.so" to "usr/local/lib/libblsession.so", "libfakeinput.so" to "usr/local/lib/libfakeinput.so", "libblfastpath.so" to "usr/local/lib/libblfastpath.so", + "libddnatv-hook.so" to "usr/lib/libddnatv-hook.so", "libblaudit.so" to "usr/local/lib/libblaudit.so", "libssbs.so" to "usr/local/lib/libssbs.so", "usr/local/bin/droiddeck-session" to "usr/local/bin/droiddeck-session", @@ -112,6 +146,7 @@ object SessionFiles { "usr/local/bin/droiddeck-flatpak-setup" to "usr/local/bin/droiddeck-flatpak-setup", "usr/local/bin/droiddeck-flatpak-run" to "usr/local/bin/droiddeck-flatpak-run", "usr/local/bin/droiddeck-appimage-run" to "usr/local/bin/droiddeck-appimage-run", + "usr/local/bin/droiddeck-appimage-exec" to "usr/local/bin/droiddeck-appimage-exec", "usr/local/bin/droiddeck-script-run" to "usr/local/bin/droiddeck-script-run", "usr/local/bin/droiddeck-open" to "usr/local/bin/droiddeck-open", "usr/local/bin/droiddeck-proton-run" to "usr/local/bin/droiddeck-proton-run", @@ -248,18 +283,23 @@ object SessionFiles { } if (installed) ledger.staged(key, target) else Log.e(TAG, "DirectAudio $set/$relative NOT staged") } + stageUbisoftService(context, root, ledger) ledger.save() for (old in arrayOf("aarch64-unix", "aarch64-windows", "i386-windows")) File(root, "$DIRECTAUDIO_DIR/lib/wine/$old").deleteRecursively() val preload = StringBuilder("/usr/local/lib/libblsession.so\n") if (!File(Environment.getExternalStorageDirectory(), NO_PAD_SWITCH).exists()) { preload.append("/usr/local/lib/libfakeinput.so\n") } + val nativeUserland = SessionPrefs.nativeUserland(context) + if (nativeUserland) com.droiddeck.launcher.runtime.NativeGuest.prepare(context, root) if (ProotFastPath.enabled(context)) { val library = com.droiddeck.launcher.agent.AgentEnv.fastPathLibrary(context) preload.append(library + "\n") if (startsSession && library != ProotFastPath.LIBRARY) Log.i(TAG, "fast path preload: $library (debugging override)") } val etc = File(root, "etc").apply { mkdirs() } + File(etc, "droiddeck").mkdirs() + FileUtils.writeString(File(etc, "droiddeck/native-preload"), preload.toString()) val staged = File(etc, "ld.so.preload.staged") if (!FileUtils.writeString(staged, preload.toString()) || !staged.renameTo(File(etc, "ld.so.preload"))) { diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionLoadingDecision.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionLoadingDecision.kt new file mode 100644 index 0000000000..1c5d034dff --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionLoadingDecision.kt @@ -0,0 +1,8 @@ +package com.droiddeck.launcher.session + +internal object SessionLoadingDecision { + const val TIMEOUT_MS = 10_000L + + fun shouldReturnToLauncher(startedAt: Long, now: Long, running: Boolean, phase: SessionPhase): Boolean = + !running && phase in setOf(SessionPhase.IDLE, SessionPhase.FAILED) && now - startedAt >= TIMEOUT_MS +} diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt index 2abf094c0f..4b0a94f7b6 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt @@ -23,6 +23,7 @@ object SessionPrefs { private const val LEGACY_SUSPEND_DOWNLOADS = "downloads" private const val STEAM_DOWNLOADS_IN_BACKGROUND = "steamDownloadsInBackground" private const val DECKY_ZIP_WARNING_SEEN = "deckyZipWarningSeen" + private const val LOW_STORAGE_WARNING = "lowStorageWarning" private const val SLOW_USB_WARNING_OFF = "slowUsbWarningOff" private const val SLOW_USB_DISMISSED = "slowUsbDismissed" @@ -110,12 +111,16 @@ object SessionPrefs { prefs(context).edit().remove("dualScreenLauncher").putString("dualScreenMode", launcher).putString("dualScreenSessionMode", mode).apply() } - fun secondScreenPanel(context: Context): String? = prefs(context).getString("secondScreenPanel", null) - - fun setSecondScreenPanel(context: Context, mode: String) { - prefs(context).edit().putString("secondScreenPanel", mode).apply() + fun secondScreenPanel(context: Context, desktop: Boolean): String? = prefs(context).let { + it.getString(secondScreenPanelKey(desktop), null) ?: if (desktop) null else it.getString("secondScreenPanel", null)?.takeIf { id -> id != "custom-controls" } } + fun setSecondScreenPanel(context: Context, desktop: Boolean, mode: String) { + prefs(context).edit().putString(secondScreenPanelKey(desktop), mode).apply() + } + + private fun secondScreenPanelKey(desktop: Boolean) = if (desktop) "secondScreenPanel.desktop" else "secondScreenPanel.game" + /** The bottom-screen desktop takes touch as a trackpad unless the user switched it to direct touch. */ fun bottomDesktopDirectTouch(context: Context): Boolean = prefs(context).getBoolean("bottomDesktopDirectTouch", false) @@ -184,6 +189,12 @@ object SessionPrefs { prefs(context).edit().putBoolean("dualScreenSessionControls", on).apply() } + fun androidTextFieldKeyboard(context: Context): Boolean = prefs(context).getBoolean("androidTextFieldKeyboard", false) + + fun setAndroidTextFieldKeyboard(context: Context, on: Boolean) { + prefs(context).edit().putBoolean("androidTextFieldKeyboard", on).apply() + } + fun gameStoresEnabled(context: Context): Boolean = prefs(context).getBoolean("gameStoresEnabled", true) fun setGameStoresEnabled(context: Context, on: Boolean) { @@ -547,11 +558,11 @@ object SessionPrefs { fun picturePosition(context: Context): String = prefs(context).getString("picturePosition", "center") - ?.takeIf { it in listOf("top", "bottom", "center") } ?: "center" + ?.takeIf { it in listOf("top", "bottom", "left", "right", "center") } ?: "center" fun setPicturePosition(context: Context, position: String) { prefs(context).edit().putString("picturePosition", - position.takeIf { it in listOf("top", "bottom", "center") } ?: "center").apply() + position.takeIf { it in listOf("top", "bottom", "left", "right", "center") } ?: "center").apply() } fun returnAfterGame(context: Context): Boolean = prefs(context).getBoolean("returnAfterGame", true) @@ -741,6 +752,12 @@ object SessionPrefs { prefs(context).edit().putBoolean("prootNoSeccomp", on).apply() } + fun nativeUserland(context: Context): Boolean = prefs(context).getBoolean("nativeUserland", false) + + fun setNativeUserland(context: Context, on: Boolean) { + prefs(context).edit().putBoolean("nativeUserland", on).apply() + } + fun prootFastPath(context: Context): Boolean = prefs(context).getBoolean("prootFastPath", true) fun setProotFastPath(context: Context, on: Boolean) { @@ -1544,8 +1561,11 @@ object SessionPrefs { prefs(context).getString(if (right) "gamepadRight" else "gamepadLeft", null) ?: if (right) SURFACE_TRACKPAD else SURFACE_STICK - fun setGamepadSurface(context: Context, right: Boolean, surface: String) { - prefs(context).edit().putString(if (right) "gamepadRight" else "gamepadLeft", surface).apply() + fun deckPanel(context: Context): com.droiddeck.launcher.input.DeckPanel = + com.droiddeck.launcher.input.DeckPanel.from(prefs(context).getString("deckPanel", null)) + + fun setDeckPanel(context: Context, panel: com.droiddeck.launcher.input.DeckPanel) { + prefs(context).edit().putString("deckPanel", panel.id).apply() } fun suspendPolicy(context: Context, mode: String): String = @@ -1576,6 +1596,12 @@ object SessionPrefs { prefs(context).edit().putBoolean(DECKY_ZIP_WARNING_SEEN, true).apply() } + fun lowStorageWarning(context: Context): Boolean = prefs(context).getBoolean(LOW_STORAGE_WARNING, true) + + fun setLowStorageWarning(context: Context, on: Boolean) { + prefs(context).edit().putBoolean(LOW_STORAGE_WARNING, on).apply() + } + fun slowUsbWarningOff(context: Context): Boolean = prefs(context).getBoolean(SLOW_USB_WARNING_OFF, false) fun setSlowUsbWarningOff(context: Context) { diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt index cc67bc800d..4faa870866 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt @@ -665,6 +665,10 @@ class SessionService : Service() { FileUtils.clear(File(LinuxRuntime.liveDir(this), "shm")) val binds = sessionBinds(controllersOn, fakeInputDir, sessionDir, guest, sessionRoot, runtimeDir) + val nativeUserland = com.droiddeck.launcher.runtime.NativeGuest.enabled(this) + synchronized(stopLock) { + if (gen == sessionGen && SessionState.running) com.droiddeck.launcher.runtime.NativeGuest.sessionMode = nativeUserland + } val fastPathKey = if (ProotFastPath.enabled(this)) { val prootBinds = LinuxRuntime.binds( this, sessionRoot, runtimeDir, Environment.getExternalStorageDirectory(), binds, @@ -681,27 +685,42 @@ class SessionService : Service() { } } else null - val command = LinuxRuntime.command( - this, sessionRoot, runtimeDir, Environment.getExternalStorageDirectory(), binds, guest, - ) - - val hostEnv = HostEnvironment() - hostEnv["PROOT_LOADER"] = LinuxRuntime.prootLoader(this).path - hostEnv["PROOT_TMP_DIR"] = LinuxRuntime.prootTempDir(this).path - if (SessionPrefs.prootNoSeccomp(this)) { - hostEnv["PROOT_NO_SECCOMP"] = "1" - Log.i(TAG, "proot: seccomp acceleration off by request") + val sessionBindSpecs = LinuxRuntime.binds(this, sessionRoot, runtimeDir, Environment.getExternalStorageDirectory(), binds) + val command = if (nativeUserland) { + LinuxRuntime.rememberSessionBinds(sessionBindSpecs) + com.droiddeck.launcher.runtime.NativeGuest.command(root, guest.first(), guest.drop(1)) + } else { + LinuxRuntime.command(this, sessionRoot, runtimeDir, Environment.getExternalStorageDirectory(), binds, guest) } - fastPathKey?.let { ProotFastPath.hostEnv(it).let { (k, v) -> hostEnv[k] = v } } + + val prootEnv = HostEnvironment() + prootEnv["PROOT_LOADER"] = LinuxRuntime.prootLoader(this).path + prootEnv["PROOT_TMP_DIR"] = LinuxRuntime.prootTempDir(this).path + if (SessionPrefs.prootNoSeccomp(this)) { + prootEnv["PROOT_NO_SECCOMP"] = "1" + if (!nativeUserland) Log.i(TAG, "proot: seccomp acceleration off by request") + } + fastPathKey?.let { ProotFastPath.hostEnv(it).let { (k, v) -> prootEnv[k] = v } } for (key in listOf("PROOT_PROFILE", "PROOT_PROFILE_SLOW_US")) { - guest.lastOrNull { it.startsWith("$key=") }?.substringAfter('=')?.let { hostEnv[key] = it } + guest.lastOrNull { it.startsWith("$key=") }?.substringAfter('=')?.let { prootEnv[key] = it } } val prootLibs = LinuxRuntime.prootLibraryPath(this) - if (prootLibs.isNotEmpty()) hostEnv["LD_LIBRARY_PATH"] = prootLibs + if (prootLibs.isNotEmpty()) prootEnv["LD_LIBRARY_PATH"] = prootLibs - val terminalCommand = LinuxRuntime.command( - this, sessionRoot, runtimeDir, Environment.getExternalStorageDirectory(), binds, shellGuest, - ) + val drives = com.droiddeck.launcher.runtime.NativeGuest.relinkPrefixDrives(root, sessionBindSpecs) + if (drives > 0) Log.i(TAG, "$drives Wine drive links given their guest paths back") + val hostEnv = if (nativeUserland) HostEnvironment().also { env -> + Log.i(TAG, "native userland: no proot (${sessionBindSpecs.size} mounts)") + com.droiddeck.launcher.runtime.NativeGuest.sessionEnvironment(root, sessionBindSpecs, SessionPrefs.guestHostname(this)) + .forEach { (k, v) -> env[k] = v } + } else prootEnv + + // The terminal and the bottom desktop run the way the session does: without proot when it does + val terminalCommand = if (nativeUserland) { + com.droiddeck.launcher.runtime.NativeGuest.command(root, shellGuest.first(), shellGuest.drop(1)) + } else { + LinuxRuntime.command(this, sessionRoot, runtimeDir, Environment.getExternalStorageDirectory(), binds, shellGuest) + } val terminalHostEnvironment = LinkedHashMap(System.getenv()) hostEnv.asArray().forEach { entry -> val separator = entry.indexOf('=') @@ -711,7 +730,7 @@ class SessionService : Service() { SessionTerminal.prepare(terminalCommand, terminalHostEnvironment.map { (key, value) -> "$key=$value" }.toTypedArray(), root, gen) val (fastPathName, _) = ProotFastPath.hostEnv("") BottomDesktop.prepare(desktopGuest, binds, sessionRoot, - hostEnv.asArray().filterNot { it.startsWith("$fastPathName=") }.toTypedArray(), root, sessionDir, gen, + prootEnv.asArray().filterNot { it.startsWith("$fastPathName=") }.toTypedArray(), root, sessionDir, gen, nativeUserland, ) { gen == sessionGen && SessionState.running } } @@ -1697,8 +1716,8 @@ class SessionService : Service() { .putExtra(EXTRA_STEAM_UI, ui) .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) if (url.isNotEmpty()) next.putExtra(EXTRA_STEAM_URL, url) - SessionState.relaunch = next - android.os.Handler(android.os.Looper.getMainLooper()).post { stopSession(0) } + SessionState.queueRelaunch(next) + android.os.Handler(android.os.Looper.getMainLooper()).post { stopSession(0, relaunch = true) } } } watcher.startWatching() @@ -2013,7 +2032,7 @@ class SessionService : Service() { } } - private fun stopSession(status: Int, gen: Int? = null) { + private fun stopSession(status: Int, gen: Int? = null, relaunch: Boolean = false) { if (gen != null && gen != sessionGen) { Log.i(TAG, "ignoring a stop from session $gen; session $sessionGen is current") return @@ -2023,7 +2042,10 @@ class SessionService : Service() { if (gen != null && gen != sessionGen) return cancelQueuedSteamGame() gameExitWatchToken++ + // also when startup stopped before the guest ran + com.droiddeck.launcher.runtime.NativeGuest.sessionMode = null if (!SessionState.running) return + if (relaunch) SessionState.confirmRelaunchStop() SessionEvents.record("guest.exited", mapOf("status" to status)) SessionEvents.transition(SessionPhase.STOPPING, "session.stopping", mapOf("status" to status)) SessionState.running = false diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionState.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionState.kt index 2e66d2b80f..5f17212e52 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionState.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionState.kt @@ -73,7 +73,44 @@ object SessionState { @Volatile var steamClientArch: String? = null var steamVr by mutableStateOf(false) + @Volatile var relaunch: android.content.Intent? = null + @Volatile + private var relaunchSessionId: String? = null + @Volatile + private var relaunchStopRequested = false + + fun queueRelaunch(intent: android.content.Intent) { + synchronized(this) { + relaunch = intent + relaunchSessionId = sessionId + relaunchStopRequested = false + } + } + + fun confirmRelaunchStop() { + synchronized(this) { + relaunchStopRequested = relaunch != null && relaunchSessionId == sessionId + } + } + + fun takeRelaunch(): android.content.Intent? = synchronized(this) { + val next = relaunch + val valid = relaunchStopRequested && relaunchSessionId == sessionId + relaunch = null + relaunchSessionId = null + relaunchStopRequested = false + next.takeIf { valid } + } + + fun clearRelaunch() { + synchronized(this) { + relaunch = null + relaunchSessionId = null + relaunchStopRequested = false + } + } + @JvmStatic var hdr = false @Volatile private var deckPadValue = false @@ -93,6 +130,7 @@ object SessionState { @Volatile var secondScreenDisplay = -1 @Volatile var secondScreenAutoOpened = false @Volatile var secondScreenProvisional = false + @Volatile var secondScreenAutomatic = false @Volatile var gameOnBottom = false @Volatile var steamLibraryMounts: Map = emptyMap() diff --git a/app/src/main/java/com/droiddeck/launcher/session/SettingsReset.kt b/app/src/main/java/com/droiddeck/launcher/session/SettingsReset.kt index df74d80544..67f013b78f 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SettingsReset.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SettingsReset.kt @@ -5,14 +5,15 @@ import android.util.Log import com.droiddeck.launcher.core.DownloadSource import com.droiddeck.launcher.core.GameEnvironment import com.droiddeck.launcher.input.ControllerPrefs +import com.droiddeck.launcher.input.InputProfiles import com.droiddeck.launcher.runtime.LinuxRuntime object SettingsReset { private const val TAG = "SettingsReset" internal val SESSION_KEYS = setOf( - "orientation", "launcherFullscreen", "animations", "theme", "runSteamAtStartup", "startupSession", "addedGamesArt", - "dualScreenMode", "dualScreenSessionMode", "dualScreenLauncher", "dualScreenSessionControls", + "orientation", "launcherFullscreen", "animations", "lowStorageWarning", "theme", "runSteamAtStartup", "startupSession", "addedGamesArt", + "dualScreenMode", "dualScreenSessionMode", "dualScreenLauncher", "dualScreenSessionControls", "androidTextFieldKeyboard", "gameStoresEnabled", "gameStoresSpeedTier", "gameStoresParallel", "storesOpenTab", "storesShowMature", "storesInstallLanguage", "hud", "hudDeck", "hudFps", "hudBattery", "hudGraph", "hudColor", "hudOpacity", "__KEEP_droidDeckHud__", "__KEEP_droidDeckHudPos__", "backHold", "backActionsInverted", "backDoubleTapMs", "touch", "osc", "shape", "steamController", @@ -21,7 +22,7 @@ object SettingsReset { "gpuDriverMode", "androidDriver", "linuxDriver", "linuxDriver.steam", "linuxDriver.desktop", "tuSysmem", "glThread", "noGlError", "zinkLazy", "mangoapp", "gpuClockPin", "gamescopeRealtime", "ramCapMb", "vramCapMb", "zramClient", "directAudio", "mic", "wifiDiscovery", "clientCpusOverride", "clientCpus", "gameCpus", - "noXalia", "fastSync", "fsyncFirst", "syncFallback", "prootNoSeccomp", "prootFastPath", "cursorComposite", + "noXalia", "fastSync", "fsyncFirst", "syncFallback", "prootNoSeccomp", "prootFastPath", "nativeUserland", "cursorComposite", "guestHostname", "fexPreset", "forceSsbs", "logs", "gameDebugLogsEnabled", "enhancedDiagnosticsNext", "storageDiagnostics", "upscaler", "upscaleSharpness", "screenEffects", "textureAnisotropy", "textureLodBias", "steamDownloadsInBackground", "steam_offline", "bootMovie", "bootMovieSound", "bootMovieVolume", "bootMovieSeconds", @@ -43,6 +44,7 @@ object SettingsReset { val session = context.getSharedPreferences("session", Context.MODE_PRIVATE) session.edit().apply { session.all.keys.filter { isSessionSetting(it) }.forEach { remove(it) } }.commit() ControllerPrefs.resetAll(context) + InputProfiles.resetAll(context) context.getSharedPreferences("app-ui", Context.MODE_PRIVATE).edit().clear().commit() context.getSharedPreferences("frame_gen", Context.MODE_PRIVATE).edit().clear().commit() context.getSharedPreferences("steam_touch", Context.MODE_PRIVATE).edit() diff --git a/app/src/main/java/com/droiddeck/launcher/session/SteamLibraries.kt b/app/src/main/java/com/droiddeck/launcher/session/SteamLibraries.kt index 6f89079677..0846763599 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SteamLibraries.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SteamLibraries.kt @@ -96,7 +96,7 @@ object SteamLibraries { if (picked.none { overlaps(canonical(it.first), canonical(hit.first)) }) picked += hit } return picked.map { (dir, games) -> - val shown = File(storagePath(dir.path)) + val shown = File(UsbLink.storagePath(dir.path)) Found(dir.path, shown.path, defaultLabel(context, shown), games) } } @@ -155,7 +155,7 @@ object SteamLibraries { } fun defaultLabel(context: Context, picked: File): String { - val folder = File(storagePath(picked.path)) + val folder = File(UsbLink.storagePath(picked.path)) val volume = try { context.getSystemService(StorageManager::class.java)?.getStorageVolume(folder)?.getDescription(context) } catch (e: Exception) { @@ -171,9 +171,7 @@ object SteamLibraries { internal fun overlaps(a: String, b: String): Boolean = a == b || a.startsWith("$b/") || b.startsWith("$a/") - private fun storagePath(path: String): String = if (path.startsWith("/mnt/media_rw/")) "/storage/" + path.removePrefix("/mnt/media_rw/") else path - - private fun canonical(file: File): String = storagePath(runCatching { file.canonicalPath }.getOrDefault(file.absolutePath)) + private fun canonical(file: File): String = UsbLink.storagePath(runCatching { file.canonicalPath }.getOrDefault(file.absolutePath)) private const val SCAN_DEPTH = 2 private const val SCAN_LIMIT = 4000 diff --git a/app/src/main/java/com/droiddeck/launcher/storage/StorageState.kt b/app/src/main/java/com/droiddeck/launcher/storage/StorageState.kt index 503e769be7..331c5b402f 100644 --- a/app/src/main/java/com/droiddeck/launcher/storage/StorageState.kt +++ b/app/src/main/java/com/droiddeck/launcher/storage/StorageState.kt @@ -66,6 +66,9 @@ object StorageState { fun lowOnSpace(context: Context): Boolean = freeBytes(context) < LOW_SPACE_BYTES + fun shouldWarnLowSpace(warningEnabled: Boolean, dismissed: Boolean, sessionRunning: Boolean, freeBytes: Long): Boolean = + warningEnabled && !dismissed && !sessionRunning && freeBytes < LOW_SPACE_BYTES + fun bytes(report: StorageReport, item: CleanItem): Long = when (item) { CleanItem.OLD_TOOLS -> report.unusedTools.filter { it.reason != ToolReason.CUSTOM }.sumOf { it.bytes } CleanItem.LEFTOVERS -> report.leftovers.filter { it.safe }.sumOf { it.bytes } diff --git a/app/src/main/java/com/droiddeck/launcher/stores/CloudSaves.kt b/app/src/main/java/com/droiddeck/launcher/stores/CloudSaves.kt index 48244c4895..f68ab0ce62 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/CloudSaves.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/CloudSaves.kt @@ -87,7 +87,7 @@ object CloudSaves { val item = sidecar.extra["catalogItemId"].orEmpty() if (ns.isEmpty() || item.isEmpty()) "" else EpicApiClient.getCustomAttribute(token, ns, item, "CloudSaveFolder") } - Store.AMAZON, Store.ITCH, Store.GAMEJOLT, Store.XBOX, Store.BATTLENET -> "" + Store.AMAZON, Store.ITCH, Store.GAMEJOLT, Store.XBOX, Store.BATTLENET, Store.EA, Store.UBISOFT -> "" } ?: return null p.edit() .putString("cloud_template_$id", fetched.ifEmpty { TEMPLATE_NONE }) @@ -148,7 +148,7 @@ object CloudSaves { val dir = when (store) { Store.GOG -> CloudSavePaths.gog(template, pfx, folder) Store.EPIC -> CloudSavePaths.epic(template, pfx, folder, EpicCredentialStore.load(context)?.accountId.orEmpty(), id) - Store.AMAZON, Store.ITCH, Store.GAMEJOLT, Store.XBOX, Store.BATTLENET -> null + Store.AMAZON, Store.ITCH, Store.GAMEJOLT, Store.XBOX, Store.BATTLENET, Store.EA, Store.UBISOFT -> null } return dir to (if (dir == null) "unresolved" else "ok") } @@ -173,7 +173,7 @@ object CloudSaves { val account = EpicCredentialStore.load(context)?.accountId.orEmpty() if (token == null || account.isEmpty()) null else EpicCloud(account, id, token) } - Store.AMAZON, Store.ITCH, Store.GAMEJOLT, Store.XBOX, Store.BATTLENET -> null + Store.AMAZON, Store.ITCH, Store.GAMEJOLT, Store.XBOX, Store.BATTLENET, Store.EA, Store.UBISOFT -> null } private fun gogScopedToken(context: Context, id: String, clientId: String): String? { diff --git a/app/src/main/java/com/droiddeck/launcher/stores/LoginFlows.kt b/app/src/main/java/com/droiddeck/launcher/stores/LoginFlows.kt index 4e53c5ea08..980f496b26 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/LoginFlows.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/LoginFlows.kt @@ -13,11 +13,15 @@ internal object LoginFlows { Store.EPIC -> epicFlow() Store.AMAZON -> amazonFlow() Store.GAMEJOLT -> gameJoltFlow(browserUserAgent) + Store.UBISOFT -> ubisoftFlow(browserUserAgent) + Store.EA -> eaFlow(browserUserAgent) Store.ITCH, Store.XBOX, Store.BATTLENET -> null } @Volatile var epicFlow: () -> StoreLoginActivity.Flow? = { null } @Volatile var amazonFlow: () -> StoreLoginActivity.Flow? = { null } + @Volatile var ubisoftFlow: (String) -> StoreLoginActivity.Flow? = { null } + @Volatile var eaFlow: (String) -> StoreLoginActivity.Flow? = { null } @Volatile var gameJoltFlow: (String) -> StoreLoginActivity.Flow? = { null } private class GogFlow(private val state: String, override val userAgent: String) : StoreLoginActivity.Flow { diff --git a/app/src/main/java/com/droiddeck/launcher/stores/Store.kt b/app/src/main/java/com/droiddeck/launcher/stores/Store.kt index b7d0fbce1c..b8a6e8931e 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/Store.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/Store.kt @@ -7,7 +7,9 @@ enum class Store(val id: String, val label: String, val shortLabel: String) { XBOX("xbox", "Xbox", "Xbox"), ITCH("itch", "Itch.io", "Itch.io"), GAMEJOLT("gamejolt", "Game Jolt", "Game Jolt"), - BATTLENET("battlenet", "Battle.net", "Battle.net"); + BATTLENET("battlenet", "Battle.net", "Battle.net"), + UBISOFT("ubisoft", "Ubisoft Connect", "Ubisoft"), + EA("ea", "EA", "EA"); val folder: String get() = if (this == ITCH) "itch" else shortLabel diff --git a/app/src/main/java/com/droiddeck/launcher/stores/StoreCatalog.kt b/app/src/main/java/com/droiddeck/launcher/stores/StoreCatalog.kt index ad59e511a0..cb46d23601 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/StoreCatalog.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/StoreCatalog.kt @@ -65,6 +65,8 @@ private fun releaseDateValue(raw: String): Long = runCatching { java.time.LocalDate.parse(day).toEpochDay() }.getOrDefault(0L) } ?: 0L +internal fun entryTime(first: Long?, fallback: Long?): Long = first?.takeIf { it > 0 } ?: fallback?.takeIf { it > 0 } ?: 0L + fun sortGames( items: List, sort: String, diff --git a/app/src/main/java/com/droiddeck/launcher/stores/StoreExe.kt b/app/src/main/java/com/droiddeck/launcher/stores/StoreExe.kt index ad2d478c18..0e9792edc7 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/StoreExe.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/StoreExe.kt @@ -3,7 +3,8 @@ package com.droiddeck.launcher.stores import java.io.File object StoreExe { - private val SKIP = listOf("redist", "unins", "setup", "crash", "report", "helper", "dotnet", "vcredist", "directx", "eac", "easyanticheat", "battleye", "launcher", "updater", "config") + private val SKIP = listOf("redist", "unins", "setup", "crash", "report", "helper", "dotnet", "vcredist", "directx", "eac", "easyanticheat", "battleye", "launcher", "updater", "config", "installer", "autorun") + private val SKIP_DIRS = setOf("installer_resources", "redist", "_commonredist", "support") fun pick(folder: File, hint: String?, title: String): String { val hinted = hint?.takeIf { it.isNotBlank() }?.replace('\\', '/')?.trimStart('/')?.let { File(folder, it) } @@ -28,7 +29,7 @@ object StoreExe { if (SKIP.none { lower.contains(it) }) out.add(f) } } - for (f in files) if (f.isDirectory && !f.name.startsWith(".")) collect(f, out, depth + 1) + for (f in files) if (f.isDirectory && !f.name.startsWith(".") && f.name.lowercase() !in SKIP_DIRS) collect(f, out, depth + 1) } private fun depth(root: File, f: File): Int = relative(root, f).count { it == '/' } diff --git a/app/src/main/java/com/droiddeck/launcher/stores/StoreLaunch.kt b/app/src/main/java/com/droiddeck/launcher/stores/StoreLaunch.kt index 378d7fa74b..222b53702e 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/StoreLaunch.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/StoreLaunch.kt @@ -11,9 +11,14 @@ object StoreLaunch { const val LAUNCHER = ".droiddeck-launch.bat" const val EPIC_CODE = ".droiddeck-epic-code" const val EPIC_ARGS = ".droiddeck-epic-args" + const val UBISOFT_LAUNCH = ".droiddeck-ubisoft.json" + private const val UBISOFT_SERVICE = "Z:\\usr\\local\\lib\\droiddeck-ubisoft\\droiddeck-upc.exe" + const val EA_ENV = ".droiddeck-ea-env" + /** A sleeping stand-in that the launch hook installs; older EA SDKs require a running Origin process. */ + const val EA_ORIGIN = "C:\\Program Files (x86)\\Origin\\Origin.exe" fun writeLauncher(folder: File, sidecar: StoreGameSidecar): StoreGameSidecar { - val needs = sidecar.args.isNotEmpty() || sidecar.env.isNotEmpty() || sidecar.store == Store.EPIC + val needs = sidecar.args.isNotEmpty() || sidecar.env.isNotEmpty() || sidecar.store == Store.EPIC || sidecar.store == Store.EA || sidecar.store == Store.UBISOFT val file = File(folder, LAUNCHER) if (!needs) { file.delete(); return sidecar.copy(launcher = null) } file.writeText(launcherText(sidecar)) @@ -49,6 +54,21 @@ object StoreLaunch { lines += "del \"%~dp0$EPIC_ARGS\"" lines += "if exist \"%~dp0$EPIC_CODE\" del \"%~dp0$EPIC_CODE\"" lines += "set \"$CLIENT_VAR=\" & \"%~dp0$exe\" %$CLIENT_VAR%" + } else if (sidecar.store == Store.UBISOFT) { + lines += "if not exist \"%~dp0$UBISOFT_LAUNCH\" goto direct" + lines += "if not exist \"$UBISOFT_SERVICE\" goto direct" + lines += "\"$UBISOFT_SERVICE\" \"%~dp0$UBISOFT_LAUNCH\"" + lines += "goto :eof" + lines += ":direct" + lines += run + } else if (sidecar.store == Store.EA) { + lines += "if not exist \"%~dp0$EA_ENV\" goto run" + lines += "for /f \"usebackq tokens=1,* delims==\" %%a in (\"%~dp0$EA_ENV\") do set \"%%a=%%b\"" + lines += "del \"%~dp0$EA_ENV\"" + lines += ":run" + lines += "if exist \"$EA_ORIGIN\" start \"\" \"$EA_ORIGIN\"" + lines += run + lines += "taskkill /f /im Origin.exe >nul 2>&1" } else { lines += run } @@ -73,6 +93,17 @@ object StoreLaunch { return Library.addedGame(context, game) } + /** Gives a Ubisoft game installed before it needed one the launcher that starts the launcher service. */ + fun adopt(folder: File, sidecar: StoreGameSidecar): StoreGameSidecar { + if (sidecar.store != Store.UBISOFT || !sidecar.isInstalled) return sidecar + val wrongExe = !sidecar.exeFile(folder).isFile || sidecar.exe.contains("installer", ignoreCase = true) || sidecar.exe.contains("autorun", ignoreCase = true) + if (sidecar.launcher == LAUNCHER && !wrongExe) return sidecar + val exe = if (wrongExe) StoreExe.pick(folder, null, sidecar.title).ifEmpty { sidecar.exe } else sidecar.exe + if (exe == sidecar.exe && sidecar.launcher == LAUNCHER && + runCatching { File(folder, LAUNCHER).readText() }.getOrNull() == launcherText(sidecar)) return sidecar + return runCatching { writeLauncher(folder, sidecar.copy(exe = exe)).also { it.write(folder) } }.getOrDefault(sidecar) + } + fun refreshLauncher(folder: File, sidecar: StoreGameSidecar) { if (sidecar.launcher != LAUNCHER) return val file = File(folder, LAUNCHER) diff --git a/app/src/main/java/com/droiddeck/launcher/stores/StoreLaunchRequests.kt b/app/src/main/java/com/droiddeck/launcher/stores/StoreLaunchRequests.kt index b4b9f7669a..18f5a161e3 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/StoreLaunchRequests.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/StoreLaunchRequests.kt @@ -38,6 +38,7 @@ object StoreLaunchRequests { fun stop() { com.droiddeck.launcher.stores.xbox.XboxRuntime.stop() + com.droiddeck.launcher.stores.ea.EaLsx.stop() synchronized(lock) { observer?.stopWatching() observer = null @@ -58,6 +59,12 @@ object StoreLaunchRequests { "xbox-renew" -> JSONObject().put("ok", com.droiddeck.launcher.stores.xbox.XboxRuntime.renew(app, request.optString("id"), request.optString("lease"))) "xbox-prepare" -> runCatching { com.droiddeck.launcher.stores.xbox.XboxRuntime.prepare(app, request.getString("id")) } .getOrElse { JSONObject().put("ok", false).put("reason", "Xbox account service is unavailable. Sign in again and retry.") } + "ea-prepare" -> runCatching { lastLaunched = Store.EA to request.optString("id"); com.droiddeck.launcher.stores.ea.EaLaunch.prepare(app, request) } + .getOrElse { JSONObject().put("ok", false).put("reason", "error-" + it.javaClass.simpleName) } + "ea-exited" -> { + com.droiddeck.launcher.stores.ea.EaLsx.stop(request.optString("id")) + JSONObject().put("ok", true) + } "gog-achievements" -> runCatching { com.droiddeck.launcher.stores.gog.GogComet.prepare(app, request.optString("id")) } .getOrElse { JSONObject().put("ok", false).put("ready", false).put("reason", "service-unavailable") } "epic-code" -> { @@ -69,6 +76,15 @@ object StoreLaunchRequests { JSONObject().put("ok", true).put("code", code.written).put("reason", code.reason) } } + "ubisoft-launch" -> { + val id = request.optString("id") + if (id.isEmpty()) JSONObject().put("ok", false).put("reason", "no-id") + else { + lastLaunched = Store.UBISOFT to id + runCatching { com.droiddeck.launcher.stores.ubisoft.UbisoftLaunch.prepare(app, id) } + .getOrElse { JSONObject().put("ok", false).put("reason", it.javaClass.simpleName) } + } + } "cloud-down" -> { val store = Store.byId(request.optString("store")) val id = request.optString("id") diff --git a/app/src/main/java/com/droiddeck/launcher/stores/StoreLoginActivity.kt b/app/src/main/java/com/droiddeck/launcher/stores/StoreLoginActivity.kt index 228d97cd78..a07e0be855 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/StoreLoginActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/StoreLoginActivity.kt @@ -47,6 +47,9 @@ class StoreLoginActivity : ComponentActivity() { ?.takeIf { "Chrome/" in it } ?: "Mozilla/5.0 (Linux; Android ${Build.VERSION.RELEASE}; ${Build.MODEL}) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.6533.103 Mobile Safari/537.36" + private const val PAGE_TRIES = 20 + private const val PAGE_RETRY_MS = 500L + fun generateState(): String { val bytes = ByteArray(24) java.security.SecureRandom().nextBytes(bytes) @@ -60,6 +63,8 @@ class StoreLoginActivity : ComponentActivity() { fun isRedirect(url: String): Boolean fun finish(activity: StoreLoginActivity, view: WebView, url: String): String? val readsPage: Boolean get() = false + val pageScript: String get() = "(function(){ try { return document.body.innerText; } catch(e){ return ''; } })()" + fun pageReady(result: String): Boolean = true fun forgetSignIn(done: () -> Unit) = done() } @@ -323,6 +328,14 @@ class StoreLoginActivity : ComponentActivity() { } } + private fun readPage(view: WebView, f: Flow, left: Int) { + view.evaluateJavascript(f.pageScript) { json -> + val result = json ?: "" + if (!f.pageReady(result) && left > 0) view.postDelayed({ readPage(view, f, left - 1) }, PAGE_RETRY_MS) + else Thread({ settle(f.finish(this, view, result)) }, "store-login").start() + } + } + private fun complete(view: WebView, url: String) { if (!captured.compareAndSet(false, true)) return val f = flow ?: return @@ -332,11 +345,7 @@ class StoreLoginActivity : ComponentActivity() { titleText.text = getString(R.string.stores_login_finishing) if (!f.readsPage) view.stopLoading() } - if (f.readsPage) { - view.evaluateJavascript("(function(){ try { return document.body.innerText; } catch(e){ return ''; } })()") { json -> - Thread({ settle(f.finish(this, view, json ?: "")) }, "store-login").start() - } - } else Thread({ settle(f.finish(this, view, url)) }, "store-login").start() + if (f.readsPage) readPage(view, f, PAGE_TRIES) else Thread({ settle(f.finish(this, view, url)) }, "store-login").start() } private fun settle(problem: String?) { diff --git a/app/src/main/java/com/droiddeck/launcher/stores/StoreSizes.kt b/app/src/main/java/com/droiddeck/launcher/stores/StoreSizes.kt index 545c981e50..5e8ce7c6d7 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/StoreSizes.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/StoreSizes.kt @@ -55,7 +55,8 @@ object StoreSizes { Store.EPIC -> EpicPrefs.get(app).edit().putLong("size_${item.id}", it).apply() Store.AMAZON -> AmazonPrefs.get(app).edit().putLong("size_${item.id}", it).apply() Store.ITCH, Store.GAMEJOLT -> {} - Store.GOG, Store.XBOX, Store.BATTLENET -> {} + Store.EA -> com.droiddeck.launcher.stores.ea.EaPrefs.get(app).edit().putLong("size_${item.id}", it).apply() + Store.GOG, Store.XBOX, Store.BATTLENET, Store.UBISOFT -> {} } } }, pool).exceptionally { error -> @@ -111,7 +112,8 @@ object StoreSizes { val bytes = AmazonApiClient.getBytes(AmazonApiClient.appendPath(spec.downloadUrl, "manifest.proto"), token) ?: return 0L AmazonManifest.parse(bytes).totalInstallSize } - Store.GOG, Store.XBOX, Store.BATTLENET -> 0L + Store.EA -> com.droiddeck.launcher.stores.ea.EaSizes.size(context, item) + Store.GOG, Store.XBOX, Store.BATTLENET, Store.UBISOFT -> 0L Store.ITCH -> com.droiddeck.launcher.stores.itch.ItchLibrary.size(context, item) Store.GAMEJOLT -> com.droiddeck.launcher.stores.gamejolt.GameJoltBuilds.size(context, item) } } diff --git a/app/src/main/java/com/droiddeck/launcher/stores/battlenet/BattleNetStore.kt b/app/src/main/java/com/droiddeck/launcher/stores/battlenet/BattleNetStore.kt index 737ba3b4ff..403b867538 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/battlenet/BattleNetStore.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/battlenet/BattleNetStore.kt @@ -204,7 +204,8 @@ object BattleNetBackend : StoreBackend { fun installedGames(context: Context): List = installed(context).map { game -> val art = BattleNet.art(context, game.title) val cover = art.cover?.let { Uri.fromFile(it).toString() } - InstalledStoreGame(StoreGameSidecar(store, game.title.uid, game.name, "", cover = cover, hero = art.header?.let { Uri.fromFile(it).toString() } ?: cover), game.folder) + InstalledStoreGame(StoreGameSidecar(store, game.title.uid, game.name, "", installedAt = game.folder.lastModified(), + cover = cover, hero = art.header?.let { Uri.fromFile(it).toString() } ?: cover), game.folder) } private fun shelves(context: Context): StoreShelves { diff --git a/app/src/main/java/com/droiddeck/launcher/stores/ea/EaApi.kt b/app/src/main/java/com/droiddeck/launcher/stores/ea/EaApi.kt new file mode 100644 index 0000000000..185e23f424 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/ea/EaApi.kt @@ -0,0 +1,224 @@ +package com.droiddeck.launcher.stores.ea + +import android.net.Uri +import org.json.JSONArray +import org.json.JSONObject + +class EaApiException(val status: Int, message: String) : Exception(message) + +object EaApi { + private const val GRAPHQL = "https://service-aggregation-layer.juno.ea.com/graphql" + private const val LOCALE = "DEFAULT" + + class Player(val userId: String, val personaId: String, val displayName: String, val eaId: String) + + class OwnedProduct( + val offerId: String, + val productId: String, + val name: String, + val slug: String, + val baseSlug: String, + val gameType: String, + val downloadable: Boolean, + val trial: Boolean, + val ownership: List, + ) + + class Offer( + val offerId: String, + val contentId: String, + val displayName: String, + val displayType: String, + val downloadable: Boolean, + val executePathOverride: String, + val installCheckOverride: String, + val executeParameters: String, + val multiplayerId: String, + val masterTitleId: String, + val gameVersion: String, + val cloudSaves: String, + val locales: List, + ) + + class Images(val tall: String?, val wide: String?, val logo: String?) + + class Build(val buildId: String, val gameVersion: String, val liveDate: String) + + private val queries = HashMap() + + internal fun query(name: String): String = synchronized(queries) { + queries.getOrPut(name) { + val stream = EaApi::class.java.getResourceAsStream("/ea/graphql/$name.gql") ?: throw IllegalStateException("missing query $name") + stream.use { it.readBytes().toString(Charsets.UTF_8) } + } + } + + private fun persisted(name: String): String = + JSONObject().put("persistedQuery", JSONObject().put("version", 1).put("sha256Hash", EaCrypto.hex(EaCrypto.sha256(query(name).toByteArray())))).toString() + + internal fun call(token: String, name: String, variables: JSONObject): JSONObject { + val headers = mapOf("Authorization" to "Bearer $token", "Accept" to "application/json") + val get = Uri.parse(GRAPHQL).buildUpon() + .appendQueryParameter("extensions", persisted(name)) + .appendQueryParameter("operationName", name) + .appendQueryParameter("variables", variables.toString()) + .build().toString() + var reply = EaHttp.request(get, headers = headers) + var json = runCatching { JSONObject(reply.text) }.getOrNull() + if (!reply.ok || json == null || json.has("errors")) { + val body = JSONObject() + .put("extensions", JSONObject(persisted(name))) + .put("variables", variables) + .put("operationName", name) + .put("query", query(name)) + reply = EaHttp.request(GRAPHQL, "POST", headers + ("Content-Type" to "application/json"), body.toString().toByteArray()) + json = runCatching { JSONObject(reply.text) }.getOrNull() + } + if (reply.status == 401) throw EaApiException(401, "EA refused the session") + if (json == null) throw EaApiException(reply.status, "EA gave no answer to $name (${reply.status})") + json.optJSONArray("errors")?.optJSONObject(0)?.let { throw EaApiException(reply.status, "$name: ${it.optString("message")}") } + return json.optJSONObject("data") ?: throw EaApiException(reply.status, "$name returned no data") + } + + fun player(token: String): Player { + val me = call(token, "GetUserPlayer", JSONObject()).optJSONObject("me") ?: throw EaApiException(0, "no player") + val p = me.optJSONObject("player") + return Player( + userId = me.optString("pd"), + personaId = p?.optString("psd").orEmpty(), + displayName = p?.optString("displayName").orEmpty(), + eaId = p?.optString("uniqueName").orEmpty().ifEmpty { p?.optString("displayName").orEmpty() }, + ) + } + + internal fun parseOwned(page: JSONObject): Pair, String?> { + val owned = page.optJSONObject("me")?.optJSONObject("ownedGameProducts") ?: return emptyList() to null + val items = owned.optJSONArray("items") ?: JSONArray() + val list = (0 until items.length()).mapNotNull { items.optJSONObject(it) }.mapNotNull { o -> + val product = o.optJSONObject("product") ?: return@mapNotNull null + val base = product.optJSONObject("baseItem") + val user = product.optJSONObject("gameProductUser") + val methods = user?.optJSONArray("ownershipMethods") + OwnedProduct( + offerId = o.optString("originOfferId").ifEmpty { return@mapNotNull null }, + productId = product.optString("id"), + name = (base?.optString("title")?.takeIf { it.isNotBlank() } ?: product.optString("name")).replace("\n", " ").trim(), + slug = product.optString("gameSlug"), + baseSlug = base?.optString("baseGameSlug").orEmpty().takeIf { it != "null" }.orEmpty(), + gameType = base?.optString("gameType").orEmpty(), + downloadable = product.optBoolean("downloadable", true), + trial = product.optJSONObject("trialDetails")?.optString("trialType").orEmpty().let { it.isNotEmpty() && it != "null" }, + ownership = (0 until (methods?.length() ?: 0)).map { methods!!.optString(it) }, + ) + } + val next = owned.optString("next").takeIf { it.isNotBlank() && it != "null" } + return list to next + } + + fun owned(token: String): List { + val out = ArrayList() + var next: String? = "0" + var guard = 0 + while (next != null && guard++ < 50) { + val variables = JSONObject() + .put("isMac", false) + .put("limit", 100) + .put("next", next) + .put("type", JSONArray().put("DIGITAL_FULL_GAME").put("PACKAGED_FULL_GAME")) + .put("storefronts", JSONArray().put("EA")) + .put("platforms", JSONArray().put("PC")) + val (items, after) = parseOwned(call(token, "getPreloadedOwnedGames", variables)) + out += items + next = after?.takeIf { it != next && items.isNotEmpty() } + } + return out + } + + internal fun parseOffers(data: JSONObject): List { + val arr = data.optJSONArray("legacyOffers") ?: return emptyList() + return (0 until arr.length()).mapNotNull { arr.optJSONObject(it) }.mapNotNull { o -> + val downloads = o.optJSONArray("downloads") + val version = (0 until (downloads?.length() ?: 0)).mapNotNull { downloads!!.optJSONObject(it) } + .firstOrNull { it.optString("downloadType").equals("LIVE", true) }?.optString("gameVersion") + ?: downloads?.optJSONObject(0)?.optString("gameVersion") + val locales = o.optJSONArray("softwareLocales") + fun s(k: String) = o.optString(k).takeIf { it != "null" }.orEmpty() + Offer( + offerId = s("offerId").ifEmpty { return@mapNotNull null }, + contentId = s("contentId"), + displayName = s("displayName"), + displayType = s("displayType"), + downloadable = o.optBoolean("isDownloadable", true), + executePathOverride = s("executePathOverride"), + installCheckOverride = s("installCheckOverride"), + executeParameters = s("executeParameters"), + multiplayerId = s("multiplayerId"), + masterTitleId = s("primaryMasterTitleId"), + gameVersion = version.orEmpty().takeIf { it != "null" }.orEmpty(), + cloudSaves = s("cloudSaveConfigurationOverride"), + locales = (0 until (locales?.length() ?: 0)).map { locales!!.optString(it) }, + ) + } + } + + fun offers(token: String, ids: List): List = ids.distinct().chunked(50).flatMap { chunk -> + parseOffers(call(token, "getLegacyCatalogDefs", JSONObject().put("offerIds", JSONArray(chunk)))) + } + + private fun path(o: JSONObject?, vararg keys: String): String? { + var cur = o ?: return null + for (k in keys.dropLast(1)) cur = cur.optJSONObject(k) ?: return null + return cur.optString(keys.last()).takeIf { it.startsWith("http") } + } + + internal fun parseImages(data: JSONObject): Images { + val game = data.optJSONObject("game") + val key = game?.optJSONObject("keyArt") + val pack = game?.optJSONObject("packArt") + return Images( + tall = path(pack, "aspect9x16Image", "path") ?: path(key, "aspect9x16Image", "path"), + wide = path(key, "aspect16x9Image", "path") ?: path(key, "largestImage", "path") ?: path(pack, "aspect2x1Image", "path"), + logo = path(game, "primaryLogo", "largestImage", "path") ?: path(game, "primaryLogo", "path"), + ) + } + + fun images(token: String, slug: String): Images = + parseImages(call(token, "GameImages", JSONObject().put("gameSlug", slug).put("locale", LOCALE))) + + fun downloadUrl(token: String, offerId: String, buildId: String? = null): String { + val data = call(token, "downloadUrl", JSONObject().put("offerId", offerId).put("buildId", buildId.orEmpty())) + return data.optJSONObject("downloadUrl")?.optString("url").orEmpty().ifEmpty { throw EaApiException(0, "EA gave no download link") } + } + + fun builds(token: String, offerId: String): List { + val arr = call(token, "availableBuilds", JSONObject().put("offerId", offerId)).optJSONArray("availableBuilds") ?: return emptyList() + return (0 until arr.length()).mapNotNull { arr.optJSONObject(it) }.map { + Build(it.optString("buildId"), it.optString("gameVersion").takeIf { v -> v != "null" }.orEmpty(), it.optString("buildLiveDate")) + } + } + + fun legacyEntitlements(token: String): List { + val out = ArrayList() + var next: String? = "0" + var guard = 0 + while (next != null && guard++ < 50) { + val variables = JSONObject().put("limit", 100).put("next", next).put("storeFronts", JSONArray().put("EA")) + val page = call(token, "getLegacyEntitlements", variables).optJSONObject("me")?.optJSONObject("legacyEntitlements") ?: break + val items = page.optJSONArray("items") ?: JSONArray() + for (i in 0 until items.length()) items.optJSONObject(i)?.let(out::add) + next = page.optString("next").takeIf { it.isNotBlank() && it != "null" && it != next && items.length() > 0 } + } + return out + } + + fun entitlements(token: String, group: String): JSONArray { + val variables = JSONObject() + .put("productIds", JSONArray()) + .put("includeChildGroups", false) + .put("groupNames", JSONArray().put(group)) + .put("entitlementTag", "") + .put("pageSize", 100) + .put("pageNumber", 1) + return call(token, "sdkEntitlements", variables).optJSONObject("me")?.optJSONObject("sdkEntitlements")?.optJSONArray("entitlements") ?: JSONArray() + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/ea/EaAuth.kt b/app/src/main/java/com/droiddeck/launcher/stores/ea/EaAuth.kt new file mode 100644 index 0000000000..67fb8e655e --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/ea/EaAuth.kt @@ -0,0 +1,230 @@ +package com.droiddeck.launcher.stores.ea + +import android.content.Context +import android.net.Uri +import android.util.Log +import com.droiddeck.launcher.stores.Store +import com.droiddeck.launcher.stores.StoreAccounts +import org.json.JSONObject +import java.security.SecureRandom +import java.text.SimpleDateFormat +import java.util.Date +import java.util.Locale +import java.util.TimeZone + +object EaAuth { + private const val TAG = "EaAuth" + const val CLIENT_ID = "JUNO_PC_CLIENT" + private const val CLIENT_SECRET = "4mRLtYMb6vq9qglomWEaT4ChxsXWcyqbQpuBNfMPOYOiDmYYQmjuaBsF2Zp0RyVeWkfqhE9TuGgAw7te" + const val AUTH_URL = "https://accounts.ea.com/connect/auth" + private const val TOKEN_URL = "https://accounts.ea.com/connect/token" + const val REDIRECT = "qrc:///html/login_successful.html" + private const val REFRESH_MARGIN_MS = 5 * 60 * 1000L + private val refreshLock = Any() + private val random = SecureRandom() + + private val PC_SIGN_KEYS = mapOf( + "v1" to "ISa3dpGOc8wW7Adn4auACSQmaccrOyR2".toByteArray(), + "v2" to "nt5FfJbdPzNcl2pkC3zgjO43Knvscxft".toByteArray(), + ) + + val OPAQUE_SCOPES = listOf( + "basic.commerce.cartv2", "service.atom", "dp.client.default", "signin", "social_recommendation_user", + "basic.optin.write", "basic.commerce.cartv2.write", "basic.billing", "external.social_information_ups_admin", + ) + + class Account( + val accessToken: String, + val refreshToken: String, + val expiresAt: Long, + val userId: String, + val personaId: String, + val displayName: String, + val eaId: String, + ) + + class Pending(val url: String, val verifier: String) + + internal fun fromJson(o: JSONObject): Account? { + val access = o.optString("access_token").ifEmpty { return null } + return Account( + accessToken = access, + refreshToken = o.optString("refresh_token"), + expiresAt = o.optLong("expires_at"), + userId = o.optString("user_id"), + personaId = o.optString("persona_id"), + displayName = o.optString("display_name"), + eaId = o.optString("ea_id"), + ) + } + + fun load(context: Context): Account? = StoreAccounts.read(context.applicationContext, Store.EA)?.let(::fromJson) + + private fun deviceId(context: Context): String { + val prefs = EaPrefs.get(context) + prefs.getString("device", null)?.let { return it } + val made = EaCrypto.hex(ByteArray(16).also(random::nextBytes)) + prefs.edit().putString("device", made).apply() + return made + } + + internal fun pcSign(device: String, now: Date = Date(), version: String = if (random.nextBoolean()) "v1" else "v2"): String { + val stamp = SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS", Locale.US).apply { timeZone = TimeZone.getTimeZone("UTC") }.format(now) + val mid = EaCrypto.fnv1a(("DroidDeck" + device.uppercase() + "DroidDeck").toByteArray()).toString() + val json = "{\"av\":\"v1\",\"bsn\":\"None\",\"gid\":0,\"hsn\":\"None\",\"mid\":\"$mid\",\"msn\":\"${device.uppercase()}\",\"sv\":\"$version\",\"ts\":\"$stamp\"}" + val payload = EaCrypto.base64Url(json.toByteArray()) + val signature = EaCrypto.base64Url(EaCrypto.hmacSha256(PC_SIGN_KEYS.getValue(version), payload.toByteArray())) + return "$payload.$signature" + } + + internal fun authUrl( + device: String, + responseType: String, + challenge: String? = null, + accessToken: String? = null, + clientId: String = CLIENT_ID, + scopes: List = emptyList(), + tokenFormat: String? = null, + expiresIn: Int? = null, + ): String { + val b = Uri.parse(AUTH_URL).buildUpon() + .appendQueryParameter("client_id", clientId) + .appendQueryParameter("sbiod_enabled", "false") + .appendQueryParameter("response_type", responseType) + .appendQueryParameter("locale", "en_US") + .appendQueryParameter("pc_sign", pcSign(device)) + .appendQueryParameter("nonce", random.nextInt().toString()) + if (scopes.isNotEmpty()) b.appendQueryParameter("scope", scopes.joinToString(" ")) + if (challenge != null) { + b.appendQueryParameter("code_challenge_method", "S256") + b.appendQueryParameter("code_challenge", challenge) + } + if (accessToken != null) b.appendQueryParameter("access_token", accessToken) + if (tokenFormat != null) b.appendQueryParameter("token_format", tokenFormat) + if (expiresIn != null) b.appendQueryParameter("expires_in", expiresIn.toString()) + return b.build().toString() + } + + fun begin(context: Context): Pending { + val verifier = EaCrypto.base64Url(ByteArray(32).also(random::nextBytes)) + val challenge = EaCrypto.base64Url(EaCrypto.sha256(verifier.toByteArray())) + return Pending(authUrl(deviceId(context), "code", challenge = challenge), verifier) + } + + /** Pulls one parameter out of the redirect EA sends to its desktop client's local page. */ + internal fun redirectValue(location: String, name: String): String? { + val normal = location.replace("qrc:///html", "http://127.0.0.1").replace("qrc:/html", "http://127.0.0.1") + val uri = Uri.parse(normal) + uri.getQueryParameter(name)?.let { return it } + val fragment = uri.encodedFragment ?: return null + return Uri.parse("http://x/?$fragment").getQueryParameter(name) + } + + private fun tokenCall(values: List>): JSONObject { + val reply = EaHttp.request( + TOKEN_URL, "POST", + mapOf("Content-Type" to "application/x-www-form-urlencoded"), + EaHttp.form(values + listOf("client_id" to CLIENT_ID, "client_secret" to CLIENT_SECRET)).toByteArray(), + follow = false, + ) + val json = runCatching { JSONObject(reply.text) }.getOrNull() + if (!reply.ok || json == null || json.optString("access_token").isEmpty()) { + throw IllegalStateException("EA token request failed (${reply.status}): ${json?.optString("error_description")?.ifEmpty { json.optString("error") } ?: ""}") + } + return json + } + + fun exchange(context: Context, code: String, verifier: String) { + val token = tokenCall( + listOf( + "grant_type" to "authorization_code", "code" to code, "code_verifier" to verifier, + "redirect_uri" to REDIRECT, "token_format" to "JWS", + ), + ) + val access = token.getString("access_token") + val player = runCatching { EaApi.player(access) }.onFailure { Log.w(TAG, "player: ${it.message}") }.getOrNull() + val record = JSONObject() + .put("access_token", access) + .put("refresh_token", token.optString("refresh_token")) + .put("expires_at", System.currentTimeMillis() + token.optLong("expires_in", 3600) * 1000) + .put("user_id", player?.userId.orEmpty()) + .put("persona_id", player?.personaId.orEmpty()) + .put("display_name", player?.displayName.orEmpty()) + .put("ea_id", player?.eaId.orEmpty()) + StoreAccounts.write(context.applicationContext, Store.EA, record) + } + + private fun fresh(a: Account) = a.expiresAt - System.currentTimeMillis() > REFRESH_MARGIN_MS || a.refreshToken.isEmpty() + + private fun refreshLocked(context: Context, a: Account): String? { + if (a.refreshToken.isEmpty()) return null + val token = try { + tokenCall(listOf("grant_type" to "refresh_token", "refresh_token" to a.refreshToken)) + } catch (e: Exception) { + Log.w(TAG, "refresh: ${e.message}") + return null + } + val access = token.getString("access_token") + val player = if (load(context)?.userId.isNullOrEmpty()) runCatching { EaApi.player(access) }.getOrNull() else null + return access.takeIf { storeRefreshed(context, access, token.optLong("expires_in", 3600), token.optString("refresh_token"), player) } + } + + /** Writes a refreshed token into the saved sign-in, unless sign-out removed it meanwhile. */ + internal fun storeRefreshed(context: Context, access: String, expiresIn: Long, refresh: String, player: EaApi.Player? = null): Boolean = + synchronized(refreshLock) { + val app = context.applicationContext + val record = StoreAccounts.read(app, Store.EA) ?: return false + record.put("access_token", access).put("expires_at", System.currentTimeMillis() + expiresIn * 1000) + refresh.takeIf { it.isNotEmpty() }?.let { record.put("refresh_token", it) } + player?.let { record.put("user_id", it.userId).put("persona_id", it.personaId).put("display_name", it.displayName).put("ea_id", it.eaId) } + StoreAccounts.write(app, Store.EA, record) + true + } + + fun accessToken(context: Context): String? { + val current = load(context) ?: return null + if (fresh(current)) return current.accessToken + synchronized(refreshLock) { + val again = load(context) ?: return null + if (fresh(again)) return again.accessToken + return refreshLocked(context, again) ?: again.accessToken.takeIf { again.expiresAt > System.currentTimeMillis() } + } + } + + fun refreshNow(context: Context, stale: String?): String? = synchronized(refreshLock) { + val current = load(context) ?: return null + if (stale != null && current.accessToken != stale) return current.accessToken + refreshLocked(context, current) + } + + /** Asks EA to mint a code or token for another client from the signed-in session, as the EA app does for games. */ + fun mint( + context: Context, + clientId: String, + responseType: String, + scopes: List = emptyList(), + tokenFormat: String? = null, + expiresIn: Int? = null, + ): String? { + val access = accessToken(context) ?: return null + val url = authUrl(deviceId(context), responseType, accessToken = access, clientId = clientId, scopes = scopes, tokenFormat = tokenFormat, expiresIn = expiresIn) + val reply = runCatching { EaHttp.request(url, follow = false) }.getOrElse { + Log.w(TAG, "mint $clientId: ${it.javaClass.simpleName}") + return null + } + val location = reply.header("Location") ?: run { + Log.w(TAG, "mint $clientId: no redirect (${reply.status})") + return null + } + if (location.startsWith("https://signin.ea.com")) { + Log.w(TAG, "mint $clientId: the session needs a new sign-in") + return null + } + return redirectValue(location, if (responseType == "token") "access_token" else responseType) + } + + /** Waits for any refresh in progress, so it cannot write the sign-in back afterwards. */ + fun signOut(context: Context) { + synchronized(refreshLock) { StoreAccounts.clear(context.applicationContext, Store.EA) } + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/ea/EaBackend.kt b/app/src/main/java/com/droiddeck/launcher/stores/ea/EaBackend.kt new file mode 100644 index 0000000000..091243ae53 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/ea/EaBackend.kt @@ -0,0 +1,131 @@ +package com.droiddeck.launcher.stores.ea + +import android.content.Context +import android.content.Intent +import android.util.Log +import android.webkit.WebView +import androidx.compose.runtime.mutableStateMapOf +import com.droiddeck.launcher.R +import com.droiddeck.launcher.stores.CatalogItem +import com.droiddeck.launcher.stores.InstalledStoreGame +import com.droiddeck.launcher.stores.LoginFlows +import com.droiddeck.launcher.stores.Store +import com.droiddeck.launcher.stores.StoreBackend +import com.droiddeck.launcher.stores.StoreInstallRoot +import com.droiddeck.launcher.stores.StoreLoginActivity +import com.droiddeck.launcher.stores.StoresState +import com.droiddeck.launcher.stores.WebCookies +import com.droiddeck.launcher.stores.download.DownloadEntry +import com.droiddeck.launcher.stores.download.DownloadQueue + +object EaBackend : StoreBackend { + override val store: Store = Store.EA + + @Volatile internal var app: Context? = null + + init { + LoginFlows.eaFlow = { ua -> app?.let { EaLogin(EaAuth.begin(it), ua) } } + } + + override fun signIn(context: Context) { + app = context.applicationContext + context.startActivity( + Intent(context, StoreLoginActivity::class.java) + .putExtra(StoreLoginActivity.EXTRA_STORE, store.id) + .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK), + ) + } + + override fun signOut(context: Context) { + val app = context.applicationContext + EaAuth.signOut(app) + EaLibrary.reset(app) + EaUpdates.available.clear() + runCatching { WebCookies.expire(EaLogin.SIGN_IN_URLS, ".ea.com", listOf("/")) {} } + } + + override fun syncLibrary(context: Context, force: Boolean) { + val app = context.applicationContext + this.app = app + Thread({ EaLibrary.sync(app, force) }, "ea-sync").start() + } + + override fun loadShelves(context: Context, force: Boolean) {} + + override fun install(context: Context, item: CatalogItem, root: java.io.File) { + val app = context.applicationContext + if (!EaNative.ready()) { + StoresState.logLine(EaNative.ENGINE_MISSING) + StoresState.post { StoresState.problems[store] = EaNative.ENGINE_MISSING } + return + } + val folder = StoreInstallRoot.folderFor(app, store, item.id, item.title, root) + val entry = DownloadEntry(store, item.id, item.title, cover = item.tallImageUrl ?: item.imageUrl, bytesTotal = item.sizeBytes, location = StoreInstallRoot.labelFor(app, folder)) + DownloadQueue.enqueue(app, entry) { EaInstaller(app, item, folder) } + } + + override fun uninstall(context: Context, game: InstalledStoreGame) {} +} + +internal class EaLogin(private val pending: EaAuth.Pending, override val userAgent: String) : StoreLoginActivity.Flow { + override val startUrl: String get() = pending.url + + override fun isRedirect(url: String): Boolean = url.startsWith("qrc:", ignoreCase = true) + + override fun forgetSignIn(done: () -> Unit) { + runCatching { WebCookies.expire(SIGN_IN_URLS, ".ea.com", listOf("/")) { done() } }.onFailure { done() } + } + + override fun finish(activity: StoreLoginActivity, view: WebView, url: String): String? { + val code = EaAuth.redirectValue(url, "code") ?: return activity.getString(R.string.stores_login_error_verification) + return try { + EaAuth.exchange(activity, code, pending.verifier) + null + } catch (e: Exception) { + Log.w("EaLogin", "sign-in failed: ${e.message}") + activity.getString(R.string.stores_login_error_generic) + } + } + + companion object { + val SIGN_IN_URLS = listOf("https://accounts.ea.com", "https://signin.ea.com", "https://www.ea.com") + } +} + +object EaUpdates { + val available = mutableStateMapOf() + private val checked = java.util.concurrent.ConcurrentHashMap() + private const val RECHECK_MS = 30 * 60 * 1000L + + fun check(context: Context, game: InstalledStoreGame, force: Boolean = false) { + val app = context.applicationContext + val id = game.sidecar.id + val now = System.currentTimeMillis() + if (!force && now - (checked[id] ?: 0L) < RECHECK_MS) return + checked[id] = now + Thread({ + val token = EaAuth.accessToken(app) + val offer = token?.let { runCatching { EaApi.offers(it, listOf(id)) }.getOrNull() }?.firstOrNull() + if (offer == null) { + checked.remove(id) + return@Thread + } + val live = offer.gameVersion + val installed = game.sidecar.installVersion + StoresState.post { + if (live.isNotEmpty() && installed.isNotEmpty() && live != installed) available[game.key] = live else available.remove(game.key) + } + }, "ea-update-check").start() + } + + fun start(context: Context, game: InstalledStoreGame) { + val app = context.applicationContext + val root = game.folder.parentFile?.parentFile ?: return + val known = EaLibrary.game(app, game.sidecar.id) + val item = known?.let { EaLibrary.toCatalogItem(app, it) } ?: CatalogItem( + Store.EA, game.sidecar.id, game.sidecar.title, game.sidecar.hero, tallImageUrl = game.sidecar.cover, owned = true, extra = game.sidecar.extra, + ) + available.remove(game.key) + StoresState.install(app, item, root) + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/ea/EaCrypto.kt b/app/src/main/java/com/droiddeck/launcher/stores/ea/EaCrypto.kt new file mode 100644 index 0000000000..d36a541085 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/ea/EaCrypto.kt @@ -0,0 +1,83 @@ +package com.droiddeck.launcher.stores.ea + +import java.security.MessageDigest +import java.time.LocalDate +import java.time.ZoneOffset +import javax.crypto.Cipher +import javax.crypto.Mac +import javax.crypto.spec.IvParameterSpec +import javax.crypto.spec.SecretKeySpec + +internal object EaCrypto { + val LSX_DEFAULT_KEY = ByteArray(16) { it.toByte() } + + private val OOA_KEY = intArrayOf(65, 50, 114, 45, 208, 130, 239, 176, 220, 100, 87, 197, 118, 104, 202, 9) + .map { it.toByte() }.toByteArray() + + private const val PRIME_10K = 104729L + private const val PRIME_20K = 224737L + private const val PRIME_30K = 350377L + + fun ecbEncrypt(data: ByteArray, key: ByteArray): ByteArray = + Cipher.getInstance("AES/ECB/PKCS5Padding").run { init(Cipher.ENCRYPT_MODE, SecretKeySpec(key, "AES")); doFinal(data) } + + fun ecbDecrypt(data: ByteArray, key: ByteArray): ByteArray = + Cipher.getInstance("AES/ECB/PKCS5Padding").run { init(Cipher.DECRYPT_MODE, SecretKeySpec(key, "AES")); doFinal(data) } + + fun licenseEncrypt(data: ByteArray): ByteArray = + Cipher.getInstance("AES/CBC/PKCS5Padding").run { init(Cipher.ENCRYPT_MODE, SecretKeySpec(OOA_KEY, "AES"), IvParameterSpec(ByteArray(16))); doFinal(data) } + + fun licenseDecrypt(data: ByteArray): ByteArray = + Cipher.getInstance("AES/CBC/PKCS5Padding").run { init(Cipher.DECRYPT_MODE, SecretKeySpec(OOA_KEY, "AES"), IvParameterSpec(ByteArray(16))); doFinal(data) } + + fun hex(bytes: ByteArray): String = bytes.joinToString("") { "%02x".format(it.toInt() and 0xFF) } + + fun unhex(text: String): ByteArray { + val digits = text.lowercase().filter { it in '0'..'9' || it in 'a'..'f' } + if (digits.length % 2 != 0) return ByteArray(0) + return ByteArray(digits.length / 2) { i -> digits.substring(i * 2, i * 2 + 2).toInt(16).toByte() } + } + + /** The session key both sides derive after the LSX challenge, from a seed the game picks. */ + fun lsxKey(seed: Int): ByteArray { + if (seed == 0) return LSX_DEFAULT_KEY.copyOf() + var state = 7L + fun next(): Int { + state = (state * 214013L + 2531011L) and 0xFFFFFFFFL + return ((state ushr 16) and 0xFFFF).toInt() + } + val start = (next().toLong() + seed) and 0xFFFFFFFFL + state = start + return ByteArray(16) { next().toByte() } + } + + fun challengeAnswer(key: String): String = hex(ecbEncrypt(key.toByteArray(Charsets.US_ASCII), LSX_DEFAULT_KEY)) + + fun challengeSeed(version: String, answer: String): Int? = when (version) { + "2" -> 0 + "3" -> (answer[0].code shl 8) or answer[1].code + else -> null + } + + /** EA's launch handshake code, which changes every UTC day. */ + fun rtpCode(day: LocalDate = LocalDate.now(ZoneOffset.UTC)): Long { + val time = ((PRIME_10K * day.year) xor (day.monthValue * PRIME_20K) xor (day.dayOfMonth * PRIME_30K)) and 0xFFFFFFFFL + return (time xor ((time shl 16) and 0xFFFFFFFFL) xor (time ushr 16)) and 0xFFFFFFFFL + } + + fun sha256(data: ByteArray): ByteArray = MessageDigest.getInstance("SHA-256").digest(data) + + fun sha1(data: ByteArray): ByteArray = MessageDigest.getInstance("SHA-1").digest(data) + + fun hmacSha256(key: ByteArray, data: ByteArray): ByteArray = + Mac.getInstance("HmacSHA256").run { init(SecretKeySpec(key, "HmacSHA256")); doFinal(data) } + + fun fnv1a(data: ByteArray): ULong { + var hash = 0xcbf29ce484222325UL + for (b in data) hash = (hash xor (b.toUByte().toULong())) * 0x100000001b3UL + return hash + } + + fun base64Url(data: ByteArray): String = + java.util.Base64.getUrlEncoder().withoutPadding().encodeToString(data) +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/ea/EaHttp.kt b/app/src/main/java/com/droiddeck/launcher/stores/ea/EaHttp.kt new file mode 100644 index 0000000000..193c4d741c --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/ea/EaHttp.kt @@ -0,0 +1,47 @@ +package com.droiddeck.launcher.stores.ea + +import java.net.HttpURLConnection +import java.net.URL +import java.net.URLEncoder + +internal class EaReply(val status: Int, val body: ByteArray, val headers: Map>) { + val ok: Boolean get() = status in 200..299 + val text: String get() = body.toString(Charsets.UTF_8) + fun header(name: String): String? = headers.entries.firstOrNull { it.key.equals(name, ignoreCase = true) }?.value?.firstOrNull() +} + +internal object EaHttp { + const val USER_AGENT = "Mozilla/5.0 EA Desktop/13.735.2.6250" + private const val TIMEOUT = 30_000 + + fun form(values: List>): String = + values.joinToString("&") { (k, v) -> URLEncoder.encode(k, "UTF-8") + "=" + URLEncoder.encode(v, "UTF-8") } + + fun request( + url: String, + method: String = "GET", + headers: Map = emptyMap(), + body: ByteArray? = null, + follow: Boolean = true, + ): EaReply { + val c = URL(url).openConnection() as HttpURLConnection + try { + c.requestMethod = method + c.connectTimeout = TIMEOUT + c.readTimeout = TIMEOUT + c.instanceFollowRedirects = follow + c.setRequestProperty("User-Agent", USER_AGENT) + for ((k, v) in headers) c.setRequestProperty(k, v) + if (body != null) { + c.doOutput = true + c.outputStream.use { it.write(body) } + } + val status = c.responseCode + val stream = if (status >= 400) c.errorStream else c.inputStream + val bytes = stream?.use { it.readBytes() } ?: ByteArray(0) + return EaReply(status, bytes, c.headerFields.filterKeys { it != null }) + } finally { + c.disconnect() + } + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/ea/EaInstaller.kt b/app/src/main/java/com/droiddeck/launcher/stores/ea/EaInstaller.kt new file mode 100644 index 0000000000..22d973592d --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/ea/EaInstaller.kt @@ -0,0 +1,140 @@ +package com.droiddeck.launcher.stores.ea + +import android.content.Context +import com.droiddeck.launcher.R +import com.droiddeck.launcher.stores.CatalogItem +import com.droiddeck.launcher.stores.Store +import com.droiddeck.launcher.stores.StoreExe +import com.droiddeck.launcher.stores.StoreGameSidecar +import com.droiddeck.launcher.stores.StoreInstalls +import com.droiddeck.launcher.stores.StoreLanguage +import com.droiddeck.launcher.stores.amazon.AmazonNative +import com.droiddeck.launcher.stores.download.DownloadQueue +import com.droiddeck.launcher.stores.download.DownloadStage +import com.droiddeck.launcher.stores.download.StoreDownloadTier +import org.json.JSONArray +import org.json.JSONObject +import java.io.File +import java.util.concurrent.atomic.AtomicBoolean +import java.util.concurrent.atomic.AtomicLong + +internal class EaInstaller( + private val app: Context, + private val item: CatalogItem, + private val folder: File, +) : DownloadQueue.DownloadJob { + private val cancelled = AtomicBoolean(false) + + override fun run(handle: DownloadQueue.JobHandle): String? { + handle.stage(DownloadStage.MANIFEST, app.getString(R.string.stores_sync_sign_in)) + var token = EaAuth.accessToken(app) ?: throw IllegalStateException("Not signed in to EA") + val url = try { + EaApi.downloadUrl(token, item.id) + } catch (e: EaApiException) { + if (e.status != 401) throw IllegalStateException(e.message) + token = EaAuth.refreshNow(app, token) ?: throw IllegalStateException("The EA sign-in has expired") + EaApi.downloadUrl(token, item.id) + } + val game = EaLibrary.game(app, item.id) + val version = runCatching { EaApi.offers(token, listOf(item.id)).firstOrNull()?.gameVersion }.getOrNull().orEmpty() + .ifEmpty { game?.version.orEmpty() } + val cover = item.tallImageUrl ?: item.imageUrl + StoreInstalls.begin(folder, Store.EA, item.id, item.title, cover, item.imageUrl) + if (cancelled.get()) return null + + val tier = StoreDownloadTier.current(app) + handle.log("ea: \"${item.title}\" tier=${tier.id} window=${tier.networkWindow}") + val request = JSONObject() + .put("url", url) + .put("gameDir", folder.absolutePath) + .put("maxWorkers", tier.networkWindow.coerceIn(1, 64)) + .put("processWorkers", tier.processWorkers) + .put("caBundle", "") + val shown = AtomicLong(-1) + val downloading = AtomicBoolean(false) + val listener = object : AmazonNative.InstallListener { + override fun onStatus(text: String) { handle.stage(DownloadStage.MANIFEST, text) } + override fun onCheck(doneBytes: Long, totalBytes: Long, files: Int, filesTotal: Int) { + handle.stage(DownloadStage.MANIFEST, "Checking files…") + handle.stageProgress(doneBytes, totalBytes, files, filesTotal, bytes = true) + } + override fun onSizes(pendingBytes: Long, totalBytes: Long) { handle.diskSize(totalBytes) } + override fun onProgress(done: Long, total: Long, files: Int, filesTotal: Int) { + if (downloading.compareAndSet(false, true)) handle.stage(DownloadStage.DOWNLOAD) + while (true) { + val before = shown.get() + if (done < before) return + if (shown.compareAndSet(before, done)) break + } + handle.progress(done, total, "Downloading ($files/$filesTotal files)") + } + override fun onLog(line: String) { handle.log("ea: $line") } + override fun beforeReplace(): Boolean = try { + StoreInstalls.markUpdating(folder) + true + } catch (e: Exception) { + handle.log("ea: the install record could not be switched to updating (${e.javaClass.simpleName})") + false + } + override fun onComplete(success: Boolean, cancelled: Boolean, error: String, result: String) = Unit + } + val outcome = EaNative.install(request, listener, cancelled) + if (outcome.cancelled || cancelled.get() && !outcome.success) return null + if (!outcome.success) throw IllegalStateException(outcome.error.ifEmpty { "The EA install failed" }) + val result = outcome.result ?: JSONObject() + result.optLong("totalBytes").takeIf { it > 0 }?.let { EaPrefs.get(app).edit().putLong("size_${item.id}", it).apply() } + + handle.stage(DownloadStage.INSTALL, app.getString(R.string.stores_registering)) + val sidecar = sidecarFor(app, item, folder, game, version, cover) { handle.log("ea: $it") } + StoreInstalls.complete(app, folder, sidecar, onStep = { done, steps -> handle.stageProgress(done.toLong(), steps.toLong()) }) + return folder.path + } + + override fun cancel(deleteFiles: Boolean) { + cancelled.set(true) + if (deleteFiles) StoreInstalls.discard(app, folder, emptyList()) + } + + companion object { + fun locale(context: Context, available: List): String { + val wanted = StoreLanguage.localeFor(null, context) + val exact = "${wanted.language}_${wanted.country}" + return available.firstOrNull { it.equals(exact, true) } + ?: available.firstOrNull { it.substringBefore('_').equals(wanted.language, true) } + ?: available.firstOrNull { it.equals("en_US", true) } + ?: "en_US" + } + + internal fun sidecarFor(app: Context, item: CatalogItem, folder: File, game: EaGame?, version: String, cover: String?, log: (String) -> Unit): StoreGameSidecar { + val manifest = EaManifest.read(folder) + if (manifest == null) log("no installerdata.xml in ${folder.name}") + val launcher = manifest?.launcher() + val candidates = listOfNotNull(launcher?.path, game?.executePath?.takeIf { it.isNotEmpty() }) + var exe = candidates.firstNotNullOfOrNull { EaPaths.relativeOnDisk(folder, EaPaths.relative(it)) }.orEmpty() + if (exe.isEmpty()) exe = StoreExe.pick(folder, null, item.title) + if (exe.isEmpty()) { log("no exe found; pick one in Steam settings › Added games"); exe = "game.exe" } else log("exe=$exe") + val args = EaPaths.splitArgs(listOf(game?.parameters.orEmpty(), launcher?.parameters.orEmpty()).filter { it.isNotBlank() }.joinToString(" ")) + val keys = JSONArray() + listOfNotNull(game?.installCheck, game?.executePath, launcher?.path).mapNotNull { EaPaths.registered(it) } + .distinctBy { it.key.lowercase() + "|" + it.value.lowercase() } + .forEach { keys.put(JSONArray().put(it.key).put(it.value)) } + val extra = linkedMapOf( + "contentId" to (game?.contentId ?: item.extra["contentId"].orEmpty()), + "slug" to (game?.slug ?: item.extra["slug"].orEmpty()), + "multiplayerId" to game?.multiplayerId.orEmpty(), + "masterTitleId" to game?.masterTitleId.orEmpty(), + "locale" to locale(app, game?.locales.orEmpty()), + "registry" to keys.toString(), + "touchup" to manifest?.touchupPath.orEmpty().replace('\\', '/').trimStart('/'), + "touchupArgs" to manifest?.touchupParameters.orEmpty(), + "contentIds" to manifest?.contentIds.orEmpty().joinToString(","), + "displayName" to manifest?.title(locale(app, game?.locales.orEmpty())).orEmpty(), + ).filterValues { it.isNotEmpty() } + return StoreGameSidecar( + Store.EA, item.id, item.title, exe = exe, args = args, + installVersion = version.ifEmpty { manifest?.version.orEmpty() }, installedAt = System.currentTimeMillis(), + cover = cover, hero = item.imageUrl, extra = extra, cloud = false, logo = item.logoUrl, + ) + } + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/ea/EaLaunch.kt b/app/src/main/java/com/droiddeck/launcher/stores/ea/EaLaunch.kt new file mode 100644 index 0000000000..a9b623a2d6 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/ea/EaLaunch.kt @@ -0,0 +1,138 @@ +package com.droiddeck.launcher.stores.ea + +import android.content.Context +import android.util.Base64 +import android.util.Log +import com.droiddeck.launcher.stores.Store +import com.droiddeck.launcher.stores.StoreGameSidecar +import com.droiddeck.launcher.stores.StoreInstallRoot +import org.json.JSONArray +import org.json.JSONObject +import java.io.File +import java.util.UUID + +object EaLaunch { + private const val TAG = "EaLaunch" + const val ENV_FILE = ".droiddeck-ea-env" + + private fun installed(context: Context, id: String): Pair? = + StoreInstallRoot.gameFolders(context).firstNotNullOfOrNull { f -> + StoreGameSidecar.read(f)?.takeIf { it.store == Store.EA && it.id == id && it.isInstalled }?.let { f to it } + } + + internal fun environment( + offerId: String, + contentId: String, + locale: String, + generic: String, + access: String, + opaque: String, + account: EaAuth.Account, + session: String = UUID.randomUUID().toString(), + ): LinkedHashMap = linkedMapOf( + "EAAuthCode" to "unavailable", + "EAEgsProxyIpcPort" to "0", + "EAEntitlementSource" to "EA", + "EAExternalSource" to "EA", + "EAFreeTrialGame" to "false", + "EAGameLocale" to locale, + "EAGenericAuthToken" to generic, + "EALaunchCode" to "unavailable", + "EALaunchOwner" to "EA", + "EALaunchEAID" to account.eaId.ifEmpty { account.displayName }, + "EALaunchEnv" to "production", + "EALaunchOfflineMode" to "false", + "EALsxPort" to EaLsx.PORT.toString(), + "EARtPLaunchCode" to EaCrypto.rtpCode().toString(), + "EASecureLaunchTokenTemp" to account.userId, + "EASteamProxyIpcPort" to "0", + "OriginSessionKey" to session, + "ContentId" to contentId, + "EAOnErrorExitRetCode" to "1", + "EAConnectionId" to offerId, + "EALicenseToken" to offerId, + "EALaunchUserAuthToken" to opaque, + "EAAccessTokenJWS" to access, + ) + + /** Adds what installs made before the manifest's content ids and title were recorded are missing. */ + internal fun backfill(folder: File, sidecar: StoreGameSidecar): StoreGameSidecar { + if (sidecar.extra.containsKey("contentIds") || sidecar.extra.containsKey("displayName")) return sidecar + val manifest = EaManifest.read(folder) ?: return sidecar + val locale = sidecar.extra["locale"].orEmpty().ifEmpty { "en_US" } + val added = mapOf( + "contentIds" to manifest.contentIds.joinToString(","), + "displayName" to manifest.title(locale).orEmpty(), + ).filterValues { it.isNotEmpty() } + if (added.isEmpty()) return sidecar + val updated = sidecar.copy(extra = sidecar.extra + added) + runCatching { updated.write(folder) }.onFailure { Log.w(TAG, "sidecar: ${it.javaClass.simpleName}") } + return updated + } + + /** Answers the guest's request right before an EA game starts: session, licence and the LSX endpoint. */ + fun prepare(context: Context, request: JSONObject): JSONObject { + val app = context.applicationContext + val id = request.optString("id") + val reply = JSONObject().put("ok", false) + val (folder, recorded) = installed(app, id) ?: return reply.put("reason", "not-installed") + val sidecar = backfill(folder, recorded) + runCatching { com.droiddeck.launcher.stores.StoreLaunch.refreshLauncher(folder, sidecar) } + File(folder, ENV_FILE).delete() + val account = EaAuth.load(app) ?: return reply.put("reason", "signed-out") + val access = EaAuth.accessToken(app) ?: return reply.put("reason", "sign-in-expired") + val contentId = sidecar.extra["contentId"].orEmpty() + val locale = sidecar.extra["locale"].orEmpty().ifEmpty { "en_US" } + val core = EaOoa.coreDir(folder, sidecar.exe) + val hashVersion = core?.let(EaOoa::activationVersion) ?: 1 + val hardware = request.optJSONObject("hardware")?.let(EaOoa.Hardware::fromJson)?.takeIf { it.complete(hashVersion) } + val legacy = EaOoa.legacyCore(core) + + val lifetime = if (legacy) 14400 else 550 + val opaque = EaAuth.mint(app, EaAuth.CLIENT_ID, "token", EaAuth.OPAQUE_SCOPES, "OPAQUE", lifetime).orEmpty() + val known = (sidecar.extra["contentIds"].orEmpty().split(',') + sidecar.extra["multiplayerId"].orEmpty()).map { it.trim() }.filter { it.isNotEmpty() }.toSet() + val lsx = EaLsx.start(app, EaLsx.Game(id, contentId, sidecar.title, sidecar.installVersion, locale, hardware, hashVersion, opaque, + System.currentTimeMillis() + (lifetime - 30) * 1000L, known)) + val env = environment(id, contentId, locale, if (legacy && opaque.isNotEmpty()) opaque else access, access, opaque, EaAuth.load(app) ?: account) + val tmp = File(folder, "$ENV_FILE.tmp") + tmp.writeText(env.entries.joinToString("\r\n", postfix = "\r\n") { "${it.key}=${it.value}" }) + tmp.renameTo(File(folder, ENV_FILE)) + + val licenses = JSONArray() + val notes = ArrayList() + if (core != null) { + val ids = (listOf(contentId) + sidecar.extra["contentIds"].orEmpty().split(',')).map { it.trim() }.filter { it.isNotEmpty() }.distinct() + if (hardware == null) notes += "no-hardware" else { + val hash = EaOoa.machineHash(hardware, hashVersion) + for (content in ids) { + notes += try { + for ((path, data) in EaOoa.files(core, EaOoa.request(access, content, hash))) { + licenses.put(JSONObject().put("path", path).put("data", Base64.encodeToString(data, Base64.NO_WRAP))) + } + "$content:ok" + } catch (e: Exception) { + Log.w(TAG, "licence $content for $id: ${e.message}") + com.droiddeck.launcher.stores.StoresState.logLine("ea licence $content for $id v$hashVersion legacy=$legacy: ${e.message}") + "$content:refused" + } + } + } + } + if (core != null && notes.isNotEmpty() && notes.none { it.endsWith(":ok") }) runCatching { + val rows = EaApi.legacyEntitlements(access).filter { it.optString("productId") == id } + val types = rows.joinToString(",") { it.optString("entitlementType") + "/" + it.optString("entitlementTag") + "/" + it.optString("status") } + com.droiddeck.launcher.stores.StoresState.logLine("ea $id has no licence; entitlements: ${types.ifEmpty { "none" }}") + } + val licenseNote = if (core == null) "none-needed" else "v$hashVersion " + notes.joinToString(",") + Log.i(TAG, "prepared $id lsx=$lsx legacy=$legacy opaque=${opaque.isNotEmpty()} licence=$licenseNote") + return reply.put("ok", true).put("lsx", lsx).put("license", licenseNote).put("licenses", licenses) + } +} + +object EaSizes { + fun size(context: Context, item: com.droiddeck.launcher.stores.CatalogItem): Long { + val token = EaAuth.accessToken(context) ?: return 0L + val url = runCatching { EaApi.downloadUrl(token, item.id) }.getOrNull() ?: return 0L + return EaNative.call("zip.size", JSONObject().put("url", url))?.optLong("totalBytes") ?: 0L + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/ea/EaLibrary.kt b/app/src/main/java/com/droiddeck/launcher/stores/ea/EaLibrary.kt new file mode 100644 index 0000000000..2a90b2550c --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/ea/EaLibrary.kt @@ -0,0 +1,293 @@ +package com.droiddeck.launcher.stores.ea + +import android.content.Context +import android.content.SharedPreferences +import com.droiddeck.launcher.R +import com.droiddeck.launcher.stores.CatalogItem +import com.droiddeck.launcher.stores.Store +import com.droiddeck.launcher.stores.StoreNet +import com.droiddeck.launcher.stores.StoreShelves +import com.droiddeck.launcher.stores.StoresState +import com.droiddeck.launcher.stores.mergeItems +import org.json.JSONArray +import org.json.JSONObject +import java.util.concurrent.Executors +import java.util.concurrent.TimeUnit +import java.util.concurrent.atomic.AtomicBoolean + +object EaPrefs { + const val FILE = "stores.ea" + fun get(context: Context): SharedPreferences = context.applicationContext.getSharedPreferences(FILE, Context.MODE_PRIVATE) +} + +data class EaGame( + val offerId: String, + val contentId: String, + val title: String, + val slug: String, + val tall: String = "", + val wide: String = "", + val logo: String = "", + val version: String = "", + val executePath: String = "", + val installCheck: String = "", + val parameters: String = "", + val multiplayerId: String = "", + val masterTitleId: String = "", + val cloudSaves: String = "", + val locales: List = emptyList(), +) { + fun toJson(): JSONObject = JSONObject() + .put("offerId", offerId).put("contentId", contentId).put("title", title).put("slug", slug) + .put("tall", tall).put("wide", wide).put("logo", logo).put("version", version) + .put("executePath", executePath).put("installCheck", installCheck).put("parameters", parameters) + .put("multiplayerId", multiplayerId).put("masterTitleId", masterTitleId).put("cloudSaves", cloudSaves) + .put("locales", JSONArray(locales)) + + companion object { + fun fromJson(o: JSONObject): EaGame? { + val locales = o.optJSONArray("locales") + return EaGame( + offerId = o.optString("offerId").ifEmpty { return null }, + contentId = o.optString("contentId"), + title = o.optString("title"), + slug = o.optString("slug"), + tall = o.optString("tall"), + wide = o.optString("wide"), + logo = o.optString("logo"), + version = o.optString("version"), + executePath = o.optString("executePath"), + installCheck = o.optString("installCheck"), + parameters = o.optString("parameters"), + multiplayerId = o.optString("multiplayerId"), + masterTitleId = o.optString("masterTitleId"), + cloudSaves = o.optString("cloudSaves"), + locales = (0 until (locales?.length() ?: 0)).map { locales!!.optString(it) }, + ) + } + } +} + +object EaLibrary { + private const val KEY_CACHE = "library_cache" + private const val KEY_SYNCED_AT = "library_synced_at" + private const val THROTTLE_MS = 6 * 60 * 60 * 1000L + private const val ART_TTL_MS = 7 * 24 * 60 * 60 * 1000L + private const val ART_THREADS = 4 + const val STORE_URL = "https://www.ea.com/ea-app/library" + private val gate = SyncGate() + private val generation = java.util.concurrent.atomic.AtomicInteger() + + /** Drops the cache and makes any sync already running drop its results, in one step. */ + fun reset(context: Context) { + synchronized(cacheLock) { + generation.incrementAndGet() + EaPrefs.get(context).edit().remove(KEY_CACHE).remove(KEY_SYNCED_AT).commit() + } + } + + internal fun snapshot(app: Context): Pair> = synchronized(cacheLock) { generation.get() to cached(app) } + + internal fun storeCache(app: Context, games: List, gen: Int): Boolean = synchronized(cacheLock) { + gen == generation.get() && + EaPrefs.get(app).edit().putString(KEY_CACHE, JSONArray(games.map { it.toJson() }).toString()).putLong(KEY_SYNCED_AT, System.currentTimeMillis()).commit() + } + + private val cacheLock = Any() + private val store = Store.EA + + fun cached(context: Context): List { + val text = EaPrefs.get(context).getString(KEY_CACHE, null) ?: return emptyList() + val arr = runCatching { JSONArray(text) }.getOrNull() ?: return emptyList() + return (0 until arr.length()).mapNotNull { arr.optJSONObject(it)?.let(EaGame::fromJson) } + } + + fun game(context: Context, offerId: String): EaGame? = cached(context).firstOrNull { it.offerId == offerId } + + + fun toCatalogItem(context: Context, g: EaGame): CatalogItem { + val prefs = EaPrefs.get(context) + val tall = g.tall.ifEmpty { prefs.getString("poster_${g.offerId}", null).orEmpty() } + return CatalogItem( + store = store, + id = g.offerId, + title = g.title.ifBlank { g.slug }, + imageUrl = g.wide.ifEmpty { tall }.ifEmpty { null }, + tallImageUrl = tall.ifEmpty { g.wide }.ifEmpty { null }, + publisher = "Electronic Arts", + owned = true, + sizeBytes = prefs.getLong("size_${g.offerId}", 0L), + storeUrl = STORE_URL, + extra = mapOf("contentId" to g.contentId, "slug" to g.slug, "version" to g.version), + logoUrl = g.logo.ifEmpty { null }, + ) + } + + /** One entry per game: owned editions of the same base game collapse onto the base offer. */ + internal fun pick(owned: List, offers: Map): List> { + val usable = owned.filter { o -> + val offer = offers[o.offerId] ?: return@filter false + !o.trial && o.downloadable && offer.downloadable && + (offer.displayType.isEmpty() || offer.displayType.equals("FullGame", true)) && + (o.gameType.isEmpty() || o.gameType.equals("BASE_GAME", true)) + } + return usable.groupBy { it.baseSlug.ifEmpty { it.slug }.ifEmpty { it.offerId } }.values.map { group -> + val chosen = group.firstOrNull { it.baseSlug.isEmpty() } ?: group.first() + chosen to offers.getValue(chosen.offerId) + }.sortedBy { it.first.name.lowercase() } + } + + private fun art(app: Context, token: String, slugs: Collection, gen: Int): Map { + val prefs = EaPrefs.get(app) + val now = System.currentTimeMillis() + val out = java.util.concurrent.ConcurrentHashMap() + val pool = Executors.newFixedThreadPool(ART_THREADS) + try { + for (slug in slugs.filter { it.isNotEmpty() }) { + if (gen != generation.get()) break + val cached = prefs.getString("art_$slug", null)?.let { runCatching { JSONObject(it) }.getOrNull() } + if (cached != null && now - cached.optLong("at") < ART_TTL_MS) { + out[slug] = EaApi.Images(cached.optString("tall").ifEmpty { null }, cached.optString("wide").ifEmpty { null }, cached.optString("logo").ifEmpty { null }) + continue + } + pool.execute { + if (gen != generation.get()) return@execute + val images = runCatching { EaApi.images(token, slug) }.getOrNull() ?: return@execute + out[slug] = images + prefs.edit().putString( + "art_$slug", + JSONObject().put("at", now).put("tall", images.tall.orEmpty()).put("wide", images.wide.orEmpty()).put("logo", images.logo.orEmpty()).toString(), + ).apply() + } + } + } finally { + pool.shutdown() + pool.awaitTermination(2, TimeUnit.MINUTES) + } + return out + } + + private fun posters(app: Context, games: List, gen: Int) { + val prefs = EaPrefs.get(app) + val todo = games.filter { it.tall.isEmpty() && !prefs.contains("poster_${it.offerId}") }.take(40) + if (todo.isEmpty()) return + val pool = Executors.newFixedThreadPool(ART_THREADS) + try { + for (g in todo) pool.execute { + if (gen != generation.get()) return@execute + prefs.edit().putString("poster_${g.offerId}", runCatching { StoreNet.sgdbPoster(g.title) }.getOrDefault("")).apply() + } + } finally { + pool.shutdown() + pool.awaitTermination(5, TimeUnit.MINUTES) + } + } + + private fun publish(app: Context, games: List, gen: Int = generation.get()) { + if (gen != generation.get()) return + val items = games.map { toCatalogItem(app, it) } + StoresState.post { + if (gen != generation.get()) return@post + StoresState.library[store] = mergeItems(StoresState.library[store], items) + val installed = StoresState.installed.filter { it.sidecar.store == store }.map { it.sidecar.id }.toSet() + StoresState.shelves[store] = StoreShelves(trending = items.filterNot { it.id in installed }, art = StoresState.shelves[store]?.art.orEmpty()) + } + } + + private inline fun withToken(app: Context, token: String, call: (String) -> T): T = try { + call(token) + } catch (e: EaApiException) { + if (e.status != 401) throw e + call(EaAuth.refreshNow(app, token) ?: throw e) + } + + fun sync(context: Context, force: Boolean) { + if (!gate.enter()) return + var forced = force + do { + runCatching { syncOnce(context.applicationContext, forced) } + forced = true + } while (gate.leave()) + } + + private fun syncOnce(app: Context, force: Boolean) { + val (gen, cache) = snapshot(app) + val prefs = EaPrefs.get(app) + val quiet = cache.isNotEmpty() && !force + fun status(id: Int) { if (!quiet) StoresState.post { StoresState.status[store] = app.getString(id) } } + fun problem(text: String?) { + StoresState.post { + StoresState.status.remove(store) + if (text == null) StoresState.problems.remove(store) else StoresState.problems[store] = text + } + } + try { + if (cache.isNotEmpty()) publish(app, cache, gen) + if (!force && cache.isNotEmpty() && System.currentTimeMillis() - prefs.getLong(KEY_SYNCED_AT, 0L) in 0 until THROTTLE_MS) { + return problem(null) + } + status(R.string.stores_sync_sign_in) + val token = EaAuth.accessToken(app) ?: return problem(StoresState.notSignedInLine(store)) + status(R.string.stores_sync_game_list) + val owned = withToken(app, token) { EaApi.owned(it) } + val offers = withToken(app, token) { EaApi.offers(it, owned.map { o -> o.offerId }) }.associateBy { it.offerId } + val picked = pick(owned, offers) + StoresState.logLine("ea: ${owned.size} owned products, ${picked.size} games") + for ((o, offer) in picked) StoresState.logLine( + "ea: owned ${o.offerId} content=${offer.contentId} master=${offer.masterTitleId} mp=${offer.multiplayerId} via=${o.ownership.joinToString("+")} type=${o.gameType}") + if (picked.isEmpty()) return problem(if (cache.isEmpty()) app.getString(R.string.stores_sync_no_games, store.label) else null) + if (gen != generation.get()) return + status(R.string.stores_sync_cover_art) + val images = art(app, token, picked.map { it.first.slug }, gen) + val games = picked.map { (o, offer) -> + val img = images[o.slug] + EaGame( + offerId = o.offerId, contentId = offer.contentId, title = o.name.ifBlank { offer.displayName }, slug = o.slug, + tall = img?.tall.orEmpty(), wide = img?.wide.orEmpty(), logo = img?.logo.orEmpty(), version = offer.gameVersion, + executePath = offer.executePathOverride, installCheck = offer.installCheckOverride, parameters = offer.executeParameters, + multiplayerId = offer.multiplayerId, masterTitleId = offer.masterTitleId, cloudSaves = offer.cloudSaves, locales = offer.locales, + ) + } + if (!storeCache(app, games, gen)) return + posters(app, games, gen) + if (gen != generation.get()) return + publish(app, games, gen) + problem(null) + } catch (e: EaApiException) { + StoresState.logLine("ea: library sync failed: ${e.message}") + if (e.status == 401) StoresState.markSignInExpired(store) + problem(if (cache.isEmpty()) e.message else null) + } catch (e: Exception) { + StoresState.logLine("ea: library sync failed: ${e.javaClass.simpleName} ${e.message.orEmpty()}") + problem(if (cache.isEmpty()) e.message ?: app.getString(R.string.stores_sync_failed) else null) + } + } +} + +/** One sync at a time; a request made meanwhile makes the running one go round again rather than being lost. */ +internal class SyncGate { + private var busy = false + private var again = false + + /** True when the caller should run; false when a running sync will pick the request up. */ + @Synchronized + fun enter(): Boolean { + if (busy) { + again = true + return false + } + busy = true + return true + } + + /** True when another round was requested while running; the caller stays the runner. */ + @Synchronized + fun leave(): Boolean { + if (again) { + again = false + return true + } + busy = false + return false + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/ea/EaLsx.kt b/app/src/main/java/com/droiddeck/launcher/stores/ea/EaLsx.kt new file mode 100644 index 0000000000..f9e6b286a0 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/ea/EaLsx.kt @@ -0,0 +1,446 @@ +package com.droiddeck.launcher.stores.ea + +import android.content.Context +import android.util.Log +import org.xmlpull.v1.XmlPullParser +import org.xmlpull.v1.XmlPullParserFactory +import java.io.ByteArrayOutputStream +import java.io.OutputStream +import java.io.StringReader +import java.net.InetAddress +import java.net.ServerSocket +import java.net.Socket +import java.time.LocalDateTime +import java.time.ZoneOffset +import java.time.format.DateTimeFormatter + +/** + * The local SDK endpoint EA games talk to in place of the EA app (LSX: XML over TCP, AES after a + * challenge). It answers for the one game DroidDeck last prepared. + */ +object EaLsx { + private const val TAG = "EaLsx" + const val PORT = 3216 + private const val SENDER = "EALS" + private const val CHALLENGE_KEY = "cacf897a20b6d612ad0c05e011df52bb" + private const val CHALLENGE_VERSION = "10,5,30,15625" + private const val LANGUAGES = "ar_SA,de_DE,en_US,es_ES,es_MX,fr_FR,it_IT,ja_JP,ko_KR,pl_PL,pt_BR,ru_RU,zh_CN,zh_TW" + + private val SERVICES = listOf( + "EbisuSDK" to "SDK", "EbisuSDK" to "PROFILE", "XMPP" to "PRESENCE", "XMPP" to "FRIENDS", "Commerce" to "COMMERCE", + "EbisuSDK" to "RECENTPLAYER", "EbisuSDK" to "IGO", "EbisuSDK" to "MISC", "EALS" to "LOGIN", "EbisuSDK" to "UTILITY", + "XMPP" to "XMPP", "XMPP" to "CHAT", "EbisuSDK" to "IGO_EVENT", "EALS" to "EALS_EVENTS", "EbisuSDK" to "LOGIN_EVENT", + "XMPP" to "INVITE_EVENT", "EbisuSDK" to "PROFILE_EVENT", "XMPP" to "PRESENCE_EVENT", "XMPP" to "FRIENDS_EVENT", + "Commerce" to "COMMERCE_EVENT", "XMPP" to "CHAT_EVENT", "EbisuSDK" to "DOWNLOAD_EVENT", "EbisuSDK" to "PERMISSION", + "EbisuSDK" to "RESOURCES", "EbisuSDK" to "BLOCKED_USERS", "EbisuSDK" to "BLOCKED_USER_EVENT", "EbisuSDK" to "GET_USERID", + "EbisuSDK" to "ONLINE_STATUS_EVENT", "EbisuSDK" to "ACHIEVEMENT", "EbisuSDK" to "ACHIEVEMENT_EVENT", + "EbisuSDK" to "BROADCAST_EVENT", "PI" to "PROGRESSIVE_INSTALLATION", "PI" to "PROGRESSIVE_INSTALLATION_EVENT", + "EbisuSDK" to "CONTENT", + ) + + class Game( + val offerId: String, + val contentId: String, + val title: String, + val version: String, + val locale: String, + val hardware: EaOoa.Hardware?, + val hashVersion: Int, + val token: String = "", + val tokenExpiresAt: Long = 0L, + val contentIds: Set = emptySet(), + ) { + val validToken: String? get() = token.takeIf { it.isNotEmpty() && System.currentTimeMillis() < tokenExpiresAt } + } + + /** The parts of a request a reply depends on. */ + class Message(val recipient: String, val id: String, val name: String, val attrs: Map, val children: List>) + + interface Account { + val userId: String + val personaId: String + val persona: String + fun authCode(clientId: String, scope: String?): String? + fun license(contentId: String, requestTicket: String, ticketEngine: String): String? + fun entitlements(group: String): List> + fun token(): String? + } + + @Volatile var game: Game? = null + @Volatile private var account: Account? = null + /** The one session of the current launch that may act for the account. */ + /** Bumped whenever the launch changes; a connection accepted under an older one is not admitted. */ + @Volatile internal var generation = 0 + private val holder = java.util.concurrent.atomic.AtomicReference(null) + private var server: ServerSocket? = null + private val lock = Any() + private const val MAX_CLIENTS = 8 + private val WHITESPACE = Regex("[ \t]+") + + fun start(context: Context, game: Game): Boolean { + synchronized(lock) { + closeSessions() + this.game = game + account = AppAccount(context.applicationContext) + runCatching { server?.close() } + return try { + val socket = ServerSocket().apply { + reuseAddress = true + bind(java.net.InetSocketAddress(InetAddress.getByName("127.0.0.1"), PORT), 8) + } + server = socket + Thread({ accept(socket) }, "ea-lsx").apply { isDaemon = true }.start() + Log.i(TAG, "listening on $PORT") + true + } catch (e: Exception) { + Log.w(TAG, "could not listen on $PORT: ${e.javaClass.simpleName}") + false + } + } + } + + /** Ends the endpoint; with [offerId], only if it still serves that game, so a late exit cannot stop a newer launch. */ + fun stop(offerId: String? = null) { + synchronized(lock) { + if (offerId != null && game?.offerId != offerId) return + runCatching { server?.close() } + server = null + game = null + account = null + closeSessions() + } + } + + private fun closeSessions() { + generation++ + holder.set(null) + synchronized(sessions) { sessions.forEach { it.trusted = false } } + for (socket in open) runCatching { socket.close() } + open.clear() + } + + private val open = java.util.Collections.newSetFromMap(java.util.concurrent.ConcurrentHashMap()) + private const val HANDSHAKE_TIMEOUT_MS = 15_000 + private const val UNTRUSTED_IDLE_MS = 10 * 60 * 1000 + + private fun accept(socket: ServerSocket) { + while (!socket.isClosed) { + val client = runCatching { socket.accept() }.getOrNull() ?: continue + val (owner, unreadable) = ownerUid(client) + val verified = owner?.let { it == android.os.Process.myUid() } + if (verified == false) { + Log.w(TAG, "refused a connection from another app") + runCatching { client.close() } + continue + } + if (unreadable != null) note("peer cannot be identified: $unreadable") + val session = runCatching { Session(client, client.getOutputStream(), verified == true, unreadable != null) }.getOrNull() + val live = session != null && synchronized(lock) { server === socket && admit(session).also { if (it) open += client } } + if (!live) { + runCatching { client.close() } + continue + } + Thread({ try { serve(client, session!!) } finally { synchronized(sessions) { sessions -= session!! }; open -= client } }, "ea-lsx-client").apply { isDaemon = true }.start() + } + } + + /** + * The uid owning the connecting socket, or null where SELinux hides /proc/net. The guest runs + * under this app's uid, so any other uid is another app; an unknown owner never gets credentials. + */ + /** The connecting socket's uid, or why Android hid socket ownership from this app (some builds deny /proc/net). */ + private fun ownerUid(client: Socket): Pair { + val port = client.port + var denied: String? = null + var read = false + for (path in listOf("/proc/net/tcp6", "/proc/net/tcp")) { + val lines = runCatching { java.io.File(path).readLines() }.onFailure { denied = "$path ${it.javaClass.simpleName}" }.getOrNull() ?: continue + read = true + owner(lines, port)?.let { return it to null } + } + return null to if (read) null else denied ?: "no socket table" + } + + internal fun owner(lines: List, port: Int): Int? = lines.drop(1).firstNotNullOfOrNull { line -> + val f = line.trim().split(WHITESPACE) + val local = f.getOrNull(1)?.substringAfterLast(':')?.toIntOrNull(16) + val remote = f.getOrNull(2)?.substringAfterLast(':')?.toIntOrNull(16) + if (local == port && remote == PORT) f.getOrNull(7)?.toIntOrNull() else null + } + + /** + * Adds [session] unless the cap is reached by sessions that cannot be replaced; otherwise the + * oldest session that never claimed the prepared game (then the oldest untrusted one) makes room. + * Counting and eviction share one lock, so the live total never passes the cap. + */ + internal fun admit(session: Session): Boolean = synchronized(sessions) { + if (sessions.count { !it.evicted } >= MAX_CLIENTS) { + val candidates = sessions.filter { !it.trusted && !it.evicted } + val victim = candidates.firstOrNull { !it.claimed } ?: candidates.firstOrNull() ?: return false + victim.evicted = true + runCatching { victim.socket.close() } + } + sessions += session + true + } + + internal val sessions = LinkedHashSet() + + /** Trust and eviction exclude each other: an evicted session never becomes trusted, a trusted one is never evicted. */ + internal fun promote(session: Session, matches: Boolean) { + synchronized(sessions) { session.trusted = matches && !session.evicted && session.born == generation && holder.compareAndSet(null, session) } + } + + internal class Session(val socket: Socket, val output: OutputStream, val verified: Boolean, val unverifiable: Boolean = false) { + val born = generation + val connectedAt = System.currentTimeMillis() + @Volatile var evicted = false + @Volatile var claimed = false + var key: ByteArray? = null + @Volatile var trusted = false + var pending: ByteArray? = null + + /** A peer Android names as another app never gets account data; one Android cannot name is judged by its claim alone. */ + val mayTrust get() = (verified || unverifiable) && claimed + + @Synchronized + fun send(xml: String) { + val key = this.key + val text = if (key == null) xml else EaCrypto.hex(EaCrypto.ecbEncrypt(xml.toByteArray(), key)) + output.write(text.toByteArray()) + output.write(0) + output.flush() + } + } + + private fun note(line: String) = com.droiddeck.launcher.stores.StoresState.logLine("ea lsx: $line") + + private fun serve(client: Socket, session: Session) { + client.tcpNoDelay = true + client.soTimeout = HANDSHAKE_TIMEOUT_MS + val seen = LinkedHashSet() + var why = "closed by the game" + note("game connected") + try { + session.send("") + val input = client.getInputStream() + val pending = ByteArrayOutputStream() + val chunk = ByteArray(16 * 1024) + while (true) { + if (session.key == null) { + val left = session.connectedAt + HANDSHAKE_TIMEOUT_MS - System.currentTimeMillis() + if (left <= 0) throw java.net.SocketTimeoutException() + client.soTimeout = left.toInt() + } + val n = input.read(chunk) + if (n < 0) break + pending.write(chunk, 0, n) + for (frame in frames(pending, session.key)) { + for (one in Regex(".*?", RegexOption.DOT_MATCHES_ALL).findAll(frame)) { + val message = parse(one.value) ?: continue + if (seen.add(message.name)) note("request ${message.name}") + val reply = runCatching { answer(session, message) } + .onFailure { note("${message.name} failed: ${it.javaClass.simpleName}") }.getOrNull() + if (reply != null) session.send("$reply") + session.pending?.let { session.key = it; session.pending = null } + if (session.key != null) { + val idle = if (session.trusted) 0 else UNTRUSTED_IDLE_MS + if (client.soTimeout != idle) client.soTimeout = idle + } + } + } + if (pending.size() > 4 * 1024 * 1024) { why = "message too large"; break } + } + } catch (e: java.net.SocketTimeoutException) { + why = "no trusted handshake in time" + } catch (e: Exception) { + why = e.javaClass.simpleName + } finally { + holder.compareAndSet(session, null) + note("game disconnected ($why)") + runCatching { client.close() } + } + } + + /** + * Complete messages in what has arrived so far. Messages normally end in a NUL; one that does + * not counts once it holds a whole `` element, plain or decrypted. + */ + private fun frames(pending: ByteArrayOutputStream, key: ByteArray?): List { + val bytes = pending.toByteArray() + val out = ArrayList() + var start = 0 + for (i in bytes.indices) { + if (bytes[i].toInt() != 0) continue + if (i > start) decode(String(bytes, start, i - start, Charsets.UTF_8), key)?.let(out::add) + start = i + 1 + } + var rest = String(bytes, start, bytes.size - start, Charsets.UTF_8) + if (rest.isNotBlank()) decode(rest, key)?.takeIf { it.contains("") }?.let { out += it; rest = "" } + pending.reset() + pending.write(rest.toByteArray(Charsets.UTF_8)) + return out + } + + private fun decode(frame: String, key: ByteArray?): String? { + val text = frame.trim() + if (text.isEmpty()) return null + if (text.startsWith("<")) return text + val k = key ?: return null + return runCatching { String(EaCrypto.ecbDecrypt(EaCrypto.unhex(text), k)) }.getOrNull() + } + + internal fun parse(xml: String): Message? = runCatching { + val parser = XmlPullParserFactory.newInstance().newPullParser() + parser.setInput(StringReader(xml)) + var recipient = "" + var id = "" + var name = "" + val attrs = HashMap() + val children = ArrayList>() + var depth = 0 + var child = "" + while (parser.eventType != XmlPullParser.END_DOCUMENT) { + when (parser.eventType) { + XmlPullParser.START_TAG -> { + depth++ + when (depth) { + 2 -> if (parser.name == "Request") { + recipient = parser.getAttributeValue(null, "recipient").orEmpty() + id = parser.getAttributeValue(null, "id").orEmpty() + } else return@runCatching null + 3 -> { + name = parser.name + for (i in 0 until parser.attributeCount) attrs[parser.getAttributeName(i)] = parser.getAttributeValue(i) + } + 4 -> child = parser.name + } + } + XmlPullParser.TEXT -> if (depth == 4) children += child to parser.text.trim() + XmlPullParser.END_TAG -> depth-- + } + parser.next() + } + if (name.isEmpty()) null else Message(recipient, id, name, attrs, children) + }.getOrNull() + + private fun esc(s: String) = s.replace("&", "&").replace("<", "<").replace(">", ">").replace("\"", """) + + private fun tag(name: String, vararg attrs: Pair, body: String? = null): String { + val a = attrs.filter { it.second != null }.joinToString("") { " ${it.first}=\"${esc(it.second.toString())}\"" } + return if (body == null) "<$name$a/>" else "<$name$a>$body" + } + + private fun now() = LocalDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss")) + + private fun answer(session: Session, m: Message): String? { + val g = game + val who = account + val person = who?.takeIf { session.trusted } + val locale = g?.locale ?: "en_US" + return when (m.name) { + "ChallengeResponse" -> { + val answer = EaCrypto.challengeAnswer(m.attrs["key"].orEmpty()) + val version = m.attrs["version"].orEmpty() + val seed = EaCrypto.challengeSeed(version, answer) ?: 0 + val content = m.children.firstOrNull { it.first == "ContentId" }?.second.orEmpty() + session.claimed = g != null && content.isNotEmpty() && (content == g.contentId || content == g.offerId || content in g.contentIds) + val matches = session.mayTrust + promote(session, matches) + note("challenge version '$version' content '$content' ${when { session.trusted -> "matches the prepared game"; matches -> "matches, but another session holds this launch"; !session.verified && !session.unverifiable -> "comes from an unverified peer"; else -> "does not match the prepared game" }}") + session.pending = EaCrypto.lsxKey(seed) + Log.i(TAG, "game connected: ${m.children.firstOrNull { it.first == "Title" }?.second.orEmpty()}") + tag("ChallengeAccepted", "response" to answer) + } + "GetConfig" -> tag("GetConfigResponse", body = SERVICES.joinToString("") { tag("Service", "Name" to it.first, "Facility" to it.second) }) + "GetProfile" -> tag( + "GetProfileResponse", + "Persona" to person?.persona.orEmpty(), "SubscriberLevel" to 0, "CommerceCurrency" to "USD", "IsTrialSubscriber" to false, + "Country" to "US", "UserId" to person?.userId.orEmpty(), "GeoCountry" to "US", "AvatarId" to "", "IsSubscriber" to false, + "IsSteamSubscriber" to false, "PersonaId" to person?.personaId.orEmpty(), "IsUnderAge" to false, "UserIndex" to 0, + ) + "GetSetting" -> tag("GetSettingResponse", "Setting" to when (m.attrs["SettingId"]) { + "ENVIRONMENT" -> "production" + "LANGUAGE" -> locale + else -> "false" + }) + "GetGameInfo" -> tag("GetGameInfoResponse", "GameInfo" to when (m.attrs["GameInfoId"]) { + "FREETRIAL" -> "false" + "LANGUAGES" -> LANGUAGES + "INSTALLED_LANGUAGE" -> locale + "INSTALLED_VERSION", "AVAILABLE_VERSION" -> g?.version.orEmpty() + else -> "" + }) + "GetAllGameInfo" -> tag( + "GetAllGameInfoResponse", + "FullGamePurchased" to true, "FullGameReleased" to true, "InstalledVersion" to (g?.version ?: "0"), "MaxGroupSize" to 16, + "Languages" to LANGUAGES, "Expiration" to "0000-00-00T00:00:00", "UpToDate" to true, "HasExpiration" to false, + "EntitlementSource" to "EA", "AvailableVersion" to (g?.version ?: "0"), "DisplayName" to g?.title.orEmpty(), + "FreeTrial" to false, "InstalledLanguage" to locale, "FullGameReleaseDate" to "2000-01-01T00:00:00", "SystemTime" to now(), + ) + "GetInternetConnectedState" -> tag("InternetConnectedState", "connected" to 1) + "IsProgressiveInstallationAvailable" -> tag("IsProgressiveInstallationAvailableResponse", "ItemId" to m.attrs["ItemId"].orEmpty(), "Available" to false) + "AreChunksInstalled" -> tag( + "AreChunksInstalledResponse", "ItemId" to m.attrs["ItemId"].orEmpty(), "Installed" to true, + body = m.children.joinToString("") { tag(it.first, body = esc(it.second)) }, + ) + "GetAuthCode" -> tag("AuthCode", "value" to (who?.takeIf { session.trusted }?.authCode(m.attrs["ClientId"].orEmpty(), m.attrs["Scope"])?.takeIf { session.trusted } ?: "invalid")) + "GetAuthToken" -> tag("AuthToken", "value" to if (!session.trusted) "" else (g?.validToken ?: who?.token().orEmpty())) + "RequestLicense" -> { + val token = who?.takeIf { session.trusted }?.license(g?.contentId.orEmpty(), m.attrs["RequestTicket"].orEmpty(), m.attrs["TicketEngine"].orEmpty())?.takeIf { session.trusted }.orEmpty() + tag("RequestLicenseResponse", "License" to token) + } + "QueryEntitlements" -> { + val group = m.attrs["Group"].orEmpty() + val rows = who?.takeIf { session.trusted }?.entitlements(group).orEmpty() + tag("QueryEntitlementsResponse", body = rows.joinToString("") { e -> + tag( + "Entitlement", + "LastModifiedDate" to "0000-00-00T00:00:00", "EntitlementId" to e["id"], "UseCount" to (e["useCount"] ?: "0"), + "Version" to (e["version"] ?: "0"), "ItemId" to e["productId"], "ResourceId" to "", "GrantDate" to e["grantDate"], + "Group" to group, "EntitlementTag" to e["entitlementTag"], "Type" to e["entitlementType"], + "Expiration" to (e["terminationDate"]?.takeIf { it.isNotEmpty() && it != "null" } ?: "0000-00-00T00:00:00"), "Source" to "", + ) + }) + } + "GetPresence" -> tag("GetPresenceResponse", "UserId" to person?.userId.orEmpty(), "Presence" to "INGAME") + "QueryPresence" -> tag("QueryPresenceResponse", body = "") + "QueryFriends" -> tag("QueryFriendsResponse", body = "") + "GetBlockList" -> tag("GetBlockListResponse", "Return" to "Success", body = "") + "QueryOffers" -> tag("QueryOffersResponse", body = "") + "QueryImage" -> tag("QueryImageResponse", "Result" to 0, body = "") + "GetVoipStatus" -> tag("GetVoipStatusResponse", "Available" to false, "Active" to false) + "ShowIGOWindow", "SetDownloaderUtilization" -> null + else -> tag("ErrorSuccess", "Code" to 0, "Description" to "") + } + } + + private class AppAccount(private val app: Context) : Account { + private val saved get() = EaAuth.load(app) + override val userId: String get() = saved?.userId.orEmpty() + override val personaId: String get() = saved?.personaId.orEmpty() + override val persona: String get() = saved?.eaId?.ifEmpty { null } ?: saved?.displayName.orEmpty() + + override fun authCode(clientId: String, scope: String?): String? = + EaAuth.mint(app, clientId, "code", scopes = scope?.split(' ')?.filter { it.isNotBlank() }.orEmpty()) + + override fun license(contentId: String, requestTicket: String, ticketEngine: String): String? { + val g = game ?: return null + val hw = g.hardware ?: return null + val token = EaAuth.accessToken(app) ?: return null + return runCatching { + EaOoa.request(token, contentId, EaOoa.machineHash(hw, g.hashVersion), requestTicket.ifEmpty { null }, ticketEngine.ifEmpty { null }).gameToken + }.onFailure { Log.w(TAG, "licence: ${it.message}") }.getOrNull() + } + + override fun token(): String? = EaAuth.mint(app, EaAuth.CLIENT_ID, "token", EaAuth.OPAQUE_SCOPES, "OPAQUE", 14400) + + override fun entitlements(group: String): List> { + val token = EaAuth.accessToken(app) ?: return emptyList() + val arr = runCatching { EaApi.entitlements(token, group) }.getOrNull() ?: return emptyList() + return (0 until arr.length()).mapNotNull { arr.optJSONObject(it) }.map { o -> + o.keys().asSequence().associateWith { k -> o.optString(k) } + } + } + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/ea/EaManifest.kt b/app/src/main/java/com/droiddeck/launcher/stores/ea/EaManifest.kt new file mode 100644 index 0000000000..8c98311c0c --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/ea/EaManifest.kt @@ -0,0 +1,134 @@ +package com.droiddeck.launcher.stores.ea + +import org.xmlpull.v1.XmlPullParser +import org.xmlpull.v1.XmlPullParserFactory +import java.io.File +import java.io.StringReader + +/** What `__Installer/installerdata.xml` in an EA build says about running and finishing the install. */ +class EaManifest( + val version: String, + val launchers: List, + val touchupPath: String, + val touchupParameters: String, + val contentIds: List = emptyList(), + val titles: Map = emptyMap(), +) { + fun title(locale: String): String? = titles[locale] ?: titles["en_US"] ?: titles.values.firstOrNull() + + class Launcher(val path: String, val parameters: String, val trial: Boolean) + + fun launcher(): Launcher? = launchers.firstOrNull { !it.trial } ?: launchers.firstOrNull() + + companion object { + const val RELATIVE = "__Installer/installerdata.xml" + + fun decode(bytes: ByteArray): String { + val utf16 = bytes.size >= 2 && ((bytes[0] == 0xFF.toByte() && bytes[1] == 0xFE.toByte()) || (bytes[0] == 0xFE.toByte() && bytes[1] == 0xFF.toByte())) + val text = if (utf16) String(bytes, Charsets.UTF_16) else String(bytes, Charsets.UTF_8) + return text.trimStart('') + } + + fun read(folder: File): EaManifest? { + val file = File(folder, RELATIVE).takeIf { it.isFile } ?: EaPaths.find(folder, RELATIVE) ?: return null + return runCatching { parse(decode(file.readBytes())) }.getOrNull() + } + + fun parse(xml: String): EaManifest { + val parser = XmlPullParserFactory.newInstance().newPullParser() + parser.setInput(StringReader(xml.replace(Regex("^<\\?xml[^>]*\\?>"), ""))) + val stack = ArrayList() + var version = "" + val launchers = ArrayList() + var touchupPath = "" + var touchupParams = "" + var path = "" + var params = "" + var trial = false + val contentIds = ArrayList() + val titles = LinkedHashMap() + var locale = "" + while (parser.eventType != XmlPullParser.END_DOCUMENT) { + when (parser.eventType) { + XmlPullParser.START_TAG -> { + val name = parser.name + stack += name + if (name.equals("gameVersion", true)) { + version = parser.getAttributeValue(null, "version") ?: version + } + if (name.equals("launcher", true)) { path = ""; params = ""; trial = false } + if (name.equals("localeInfo", true)) locale = parser.getAttributeValue(null, "locale").orEmpty() + } + XmlPullParser.TEXT -> { + val text = parser.text.trim() + val tag = stack.lastOrNull().orEmpty() + val parent = stack.getOrNull(stack.size - 2).orEmpty() + if (text.isNotEmpty()) when { + parent.equals("launcher", true) && tag.equals("filePath", true) -> path = text + parent.equals("launcher", true) && tag.equals("parameters", true) -> params = text + parent.equals("launcher", true) && tag.equals("trial", true) -> trial = text.equals("true", true) || text == "1" + (parent.equals("touchup", true) || parent.equals("executable", true)) && tag.equals("filePath", true) -> touchupPath = text + (parent.equals("touchup", true) || parent.equals("executable", true)) && tag.equals("parameters", true) -> touchupParams = text + parent.equals("contentIDs", true) && tag.equals("contentID", true) -> contentIds += text + parent.equals("localeInfo", true) && tag.equals("title", true) && locale.isNotEmpty() -> titles[locale] = text + } + } + XmlPullParser.END_TAG -> { + if (parser.name.equals("launcher", true) && path.isNotEmpty()) launchers += Launcher(path, params, trial) + stack.removeLastOrNull() + } + } + parser.next() + } + return EaManifest(version, launchers, touchupPath, touchupParams, contentIds, titles) + } + } +} + +object EaPaths { + private val KEY = Regex("^\\[([^\\]]+)\\](.*)$") + + /** `[HKEY_LOCAL_MACHINE\SOFTWARE\Publisher\Game\Install Dir]bin\game.exe` splits into the registry key, value and relative path. */ + class Registered(val key: String, val value: String, val rest: String) + + fun registered(path: String): Registered? { + val m = KEY.find(path.trim()) ?: return null + val full = m.groupValues[1] + val cut = full.lastIndexOf('\\').takeIf { it > 0 } ?: return null + return Registered(full.substring(0, cut), full.substring(cut + 1), m.groupValues[2]) + } + + fun relative(path: String): String { + val rest = registered(path)?.rest ?: path.replace("{installLocation}", "", ignoreCase = true) + return rest.replace('\\', '/').trimStart('/').split('/').filter { it.isNotEmpty() && it != "." }.joinToString("/") + } + + /** The real file for a path the manifest may spell in a different case. */ + fun find(folder: File, relative: String): File? { + var cur = folder + for (part in relative.replace('\\', '/').split('/').filter { it.isNotEmpty() }) { + val names = cur.listFiles() ?: return null + cur = names.firstOrNull { it.name == part } ?: names.firstOrNull { it.name.equals(part, true) } ?: return null + } + return cur.takeIf { it.exists() && it != folder } + } + + fun relativeOnDisk(folder: File, relative: String): String? = + find(folder, relative)?.relativeTo(folder)?.path?.replace(File.separatorChar, '/') + + fun splitArgs(text: String): List { + val out = ArrayList() + val cur = StringBuilder() + var quoted = false + var any = false + for (c in text) { + when { + c == '"' -> { quoted = !quoted; any = true } + c.isWhitespace() && !quoted -> { if (any || cur.isNotEmpty()) out += cur.toString(); cur.clear(); any = false } + else -> cur.append(c) + } + } + if (any || cur.isNotEmpty()) out += cur.toString() + return out + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/ea/EaNative.kt b/app/src/main/java/com/droiddeck/launcher/stores/ea/EaNative.kt new file mode 100644 index 0000000000..b8a4b60052 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/ea/EaNative.kt @@ -0,0 +1,57 @@ +package com.droiddeck.launcher.stores.ea + +import android.util.Log +import com.droiddeck.launcher.stores.StoresNative +import com.droiddeck.launcher.stores.amazon.AmazonNative +import org.json.JSONObject +import java.util.concurrent.CountDownLatch +import java.util.concurrent.TimeUnit +import java.util.concurrent.atomic.AtomicBoolean + +object EaNative { + private const val TAG = "EaNative" + const val ENGINE_MISSING = "EA: the store engine is not available" + + fun ready(): Boolean = StoresNative.ensureLoaded() + + fun call(op: String, request: JSONObject): JSONObject? { + if (!ready()) return null + val text = try { nativeCall(op, request.toString()) } catch (t: Throwable) { + Log.w(TAG, "$op: ${t.javaClass.simpleName}") + null + } ?: return null + return runCatching { JSONObject(text) }.getOrNull() + } + + fun install(request: JSONObject, events: AmazonNative.InstallListener, cancelled: AtomicBoolean): AmazonNative.InstallOutcome { + if (!ready()) return AmazonNative.InstallOutcome(false, false, ENGINE_MISSING, null) + val done = CountDownLatch(1) + val holder = arrayOfNulls(1) + val bridge = object : AmazonNative.InstallListener by events { + override fun onComplete(success: Boolean, cancelled: Boolean, error: String, result: String) { + holder[0] = AmazonNative.InstallOutcome(success, cancelled, error, result.takeIf { it.isNotEmpty() }?.let { runCatching { JSONObject(it) }.getOrNull() }) + done.countDown() + } + } + val handle = try { nativeInstall(request.toString(), bridge) } catch (t: Throwable) { + return AmazonNative.InstallOutcome(false, false, "The EA install could not start: ${t.javaClass.simpleName}", null) + } + if (handle == 0L) return holder[0] ?: AmazonNative.InstallOutcome(false, false, "The EA install could not start", null) + try { + var forwarded = false + while (true) { + val finished = try { done.await(250, TimeUnit.MILLISECONDS) } catch (e: InterruptedException) { cancelled.set(true); false } + if (finished) break + if (!forwarded && cancelled.get()) { nativeCancel(handle); forwarded = true } + } + } finally { + nativeRelease(handle) + } + return holder[0] ?: AmazonNative.InstallOutcome(false, false, "The EA install ended without a result", null) + } + + @JvmStatic private external fun nativeCall(op: String, request: String): String? + @JvmStatic private external fun nativeInstall(request: String, listener: AmazonNative.InstallListener): Long + @JvmStatic private external fun nativeCancel(handle: Long) + @JvmStatic private external fun nativeRelease(handle: Long) +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/ea/EaOoa.kt b/app/src/main/java/com/droiddeck/launcher/stores/ea/EaOoa.kt new file mode 100644 index 0000000000..9590ce4a0b --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/ea/EaOoa.kt @@ -0,0 +1,196 @@ +package com.droiddeck.launcher.stores.ea + +import org.json.JSONObject +import org.xmlpull.v1.XmlPullParser +import org.xmlpull.v1.XmlPullParserFactory +import java.io.File +import java.io.StringReader +import java.net.URLEncoder +import java.security.SecureRandom + +/** Origin Online Activation: the per-machine licence an EA game's activation layer checks before it runs. */ +object EaOoa { + private const val LICENSES = "https://proxy.novafusion.ea.com/licenses" + const val LICENSE_DIR = "ProgramData/Electronic Arts/EA Services/License" + private const val SIGNATURE_SLOT = 65 + + /** + * What the game sees of the machine inside its Wine prefix: firmware and OS values from Wine's + * WMI, CPU values from CPUID executed as x86 code, since an emulated game sees the emulator's + * CPUID rather than the host CPU WMI describes. + */ + class Hardware( + val boardManufacturer: String, + val boardSerial: String, + val biosManufacturer: String, + val biosSerial: String, + val osInstallDate: String, + val osSerial: String, + val volumeSerial: String, + val gpuPnpId: String, + val cpuVendor: String, + val cpuEdx: String, + val cpuEax: String, + val cpuEcx: String, + val brand: String, + val hostname: String, + ) { + /** Whether the probe reported everything the hash of this activation version is built from. */ + fun complete(version: Int): Boolean { + val needed = arrayListOf(boardManufacturer, biosManufacturer) + when { + version <= 1 -> needed += listOf(hostname, osInstallDate, osSerial) + version == 2 -> needed += listOf(osInstallDate, osSerial, volumeSerial, cpuVendor, cpuEdx, cpuEcx) + version == 3 -> needed += listOf(volumeSerial, cpuVendor, cpuEdx, cpuEcx) + else -> needed += listOf(volumeSerial, cpuVendor, cpuEdx, cpuEax) + } + return needed.all { it.isNotEmpty() } + } + + companion object { + fun fromJson(o: JSONObject): Hardware { + fun s(k: String) = o.optString(k).trim() + return Hardware( + boardManufacturer = s("boardManufacturer"), + boardSerial = s("boardSerial").ifEmpty { "None" }, + biosManufacturer = s("biosManufacturer"), + biosSerial = s("biosSerial").ifEmpty { "None" }, + osInstallDate = s("osInstallDate"), + osSerial = s("osSerial"), + volumeSerial = s("volumeSerial").replace("-", "").lowercase().let { if (it.isEmpty()) it else it.padStart(8, '0') }, + gpuPnpId = s("gpuPnpId").ifEmpty { "None" }, + cpuVendor = s("cpuVendor"), + cpuEdx = s("cpuEdx").uppercase(), + cpuEax = s("cpuEax").uppercase(), + cpuEcx = s("cpuEcx").lowercase(), + brand = o.optString("cpuBrand"), + hostname = s("hostname"), + ) + } + } + } + + fun machineHash(hw: Hardware, version: Int): String { + val edxEax = hw.cpuEdx + hw.cpuEax + val edx = hw.cpuEdx.lowercase() + val shortGpu = hw.gpuPnpId.substringBeforeLast('\\', hw.gpuPnpId) + val parts = arrayListOf(hw.boardManufacturer, hw.boardSerial) + when { + version <= 1 -> parts += listOf(hw.hostname, hw.biosManufacturer, hw.biosSerial, hw.osInstallDate, hw.osSerial) + version == 2 -> parts += listOf(hw.biosManufacturer, hw.biosSerial, hw.osInstallDate, hw.osSerial, hw.volumeSerial, shortGpu, hw.cpuVendor, edx, hw.cpuEcx) + version == 3 -> parts += listOf(hw.biosManufacturer, hw.biosSerial, hw.volumeSerial, shortGpu, hw.cpuVendor, edx, hw.cpuEcx) + else -> parts += listOf(hw.biosManufacturer, hw.biosSerial, hw.volumeSerial, hw.gpuPnpId, hw.cpuVendor, edxEax) + } + val text = StringBuilder(parts.joinToString(";")).append(';') + if (version >= 2) text.append(hw.brand.take(47).padEnd(47, '\u0000')).append(';') + val digest = EaCrypto.sha1(text.toString().toByteArray(Charsets.ISO_8859_1)) + return if (version < 4) digest.joinToString("") { Integer.toHexString(it.toInt() and 0xFF) } else EaCrypto.hex(digest) + } + + /** Version of the game's activation layer, which decides how the machine hash is built. */ + fun activationVersion(core: File): Int { + val dll = listOf("activation.dll", "awc.dll").firstNotNullOfOrNull { EaPaths.find(core, it) } ?: return 1 + val (major, minor, patch) = productVersion(dll.readBytes()) ?: return 1 + return when { + major >= 5 -> 4 + major == 4 && (minor > 7 || (minor == 7 && patch >= 6)) -> 3 + major == 4 && minor == 7 -> 2 + major == 4 -> 1 + else -> 0 + } + } + + /** The activation layer's folder: `Core` beside the game's exe, where EA ships it, or at the game root. */ + fun coreDir(game: File, exe: String): File? { + val exeDir = exe.replace('\\', '/').substringBeforeLast('/', "") + val candidates = listOfNotNull(exeDir.takeIf { it.isNotEmpty() }?.let { "$it/Core" }, "Core") + return candidates.firstNotNullOfOrNull { EaPaths.find(game, it)?.takeIf { f -> f.isDirectory } } + } + + /** Games built on the 2012-era EA Core client, which want an opaque token rather than a JWS one. */ + fun legacyCore(core: File?): Boolean = core != null && EaPaths.find(core, "EACore.dll") != null + + private fun encodedSignature(core: File): Boolean = EaPaths.find(core, "activation.exe") != null + + internal fun productVersion(pe: ByteArray): Triple? { + val magic = byteArrayOf(0xBD.toByte(), 0x04, 0xEF.toByte(), 0xFE.toByte()) + var i = 0 + while (i + 24 <= pe.size) { + if (pe[i] == magic[0] && pe[i + 1] == magic[1] && pe[i + 2] == magic[2] && pe[i + 3] == magic[3]) { + fun u32(at: Int) = (pe[at].toLong() and 0xFF) or ((pe[at + 1].toLong() and 0xFF) shl 8) or ((pe[at + 2].toLong() and 0xFF) shl 16) or ((pe[at + 3].toLong() and 0xFF) shl 24) + val ms = u32(i + 16) + val ls = u32(i + 20) + return Triple((ms ushr 16).toInt(), (ms and 0xFFFF).toInt(), (ls ushr 16).toInt()) + } + i++ + } + return null + } + + class License(val fields: LinkedHashMap, val namespace: String, val signature: String) { + val gameToken: String? get() = fields["GameToken"]?.takeIf { it.isNotEmpty() } + val contentId: String get() = fields["ContentId"].orEmpty() + } + + internal fun parseLicense(xml: String, signature: String): License { + val parser = XmlPullParserFactory.newInstance().apply { isNamespaceAware = false }.newPullParser() + parser.setInput(StringReader(xml.replace(Regex("^<\\?xml[^>]*\\?>"), "").trim())) + val fields = LinkedHashMap() + var ns = "" + var depth = 0 + var tag = "" + while (parser.eventType != XmlPullParser.END_DOCUMENT) { + when (parser.eventType) { + XmlPullParser.START_TAG -> { + depth++ + if (depth == 1) ns = parser.getAttributeValue(null, "xmlns").orEmpty() else tag = parser.name + if (depth == 2) fields[tag] = "" + } + XmlPullParser.TEXT -> if (depth == 2) fields[tag] = fields[tag].orEmpty() + parser.text + XmlPullParser.END_TAG -> depth-- + } + parser.next() + } + return License(fields, ns, signature) + } + + private fun esc(s: String) = s.replace("&", "&").replace("<", "<").replace(">", ">").replace("\"", """) + + fun licenseXml(l: License): String { + val order = listOf("CipherKey", "MachineHash", "ContentId", "UserId", "GameToken", "GrantTime", "StartTime", "Nonce") + val body = StringBuilder("") + body.append("") + for (k in order) { + val v = l.fields[k] ?: continue + if (k == "GameToken" && v.isEmpty()) continue + body.append('<').append(k).append('>').append(esc(v)).append("') + } + return body.append("").toString() + } + + fun licenseBlob(l: License, encoded: Boolean): ByteArray { + val signature = if (encoded) l.signature.toByteArray() else java.util.Base64.getDecoder().decode(l.signature) + val head = ByteArray(SIGNATURE_SLOT) + System.arraycopy(signature, 0, head, 0, signature.size.coerceAtMost(SIGNATURE_SLOT)) + return head + EaCrypto.licenseEncrypt(licenseXml(l).toByteArray()) + } + + fun request(token: String, contentId: String, machineHash: String, requestToken: String? = null, requestType: String? = null): License { + val nonce = EaCrypto.hex(ByteArray(4).also(SecureRandom()::nextBytes)) + val params = mutableListOf("contentId" to contentId, "machineHash" to machineHash, "nonce" to nonce, "ea_eadmtoken" to token) + if (requestToken != null) params += "requestToken" to requestToken + if (requestType != null) params += "requestType" to requestType + val url = LICENSES + "?" + params.joinToString("&") { (k, v) -> k + "=" + URLEncoder.encode(v, "UTF-8") } + val reply = EaHttp.request(url, headers = mapOf("X-Requester-Id" to "Origin Online Activation", "User-Agent" to "EACTransaction")) + if (!reply.ok) throw IllegalStateException("EA refused the licence (${reply.status}): ${reply.text.replace(Regex("(eadmToken value=\")[^\"]*"), "$1\"").take(600)}") + val signature = reply.header("x-signature") ?: throw IllegalStateException("EA's licence had no signature") + return parseLicense(String(EaCrypto.licenseDecrypt(reply.body), Charsets.UTF_8), signature) + } + + /** Licence files for a game about to start, named as the activation layer looks for them. */ + fun files(core: File, license: License): List> { + val blob = licenseBlob(license, encodedSignature(core)) + val id = license.contentId + return listOf("$LICENSE_DIR/$id.dlf" to blob, "$LICENSE_DIR/${id}_cached.dlf" to blob) + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/gog/GogBackend.kt b/app/src/main/java/com/droiddeck/launcher/stores/gog/GogBackend.kt index fb6b0c8905..e36f0fc16d 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/gog/GogBackend.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/gog/GogBackend.kt @@ -64,6 +64,8 @@ object GogBackend : StoreBackend { }, "gog-sync").start() } + override fun search(context: Context, query: String): List = GogStoreCatalog.search(query) + override fun loadShelves(context: Context, force: Boolean) { val app = context.applicationContext GogStoreCatalog.cachedFeatured(app)?.let { StoresState.post { StoresState.shelves[store] = it } } diff --git a/app/src/main/java/com/droiddeck/launcher/stores/gog/GogComet.kt b/app/src/main/java/com/droiddeck/launcher/stores/gog/GogComet.kt index dec376c456..8ad240e651 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/gog/GogComet.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/gog/GogComet.kt @@ -139,15 +139,21 @@ object GogComet { !File(LinuxRuntime.rootDir(context), BIN.removePrefix("/")).isFile) return reply(false, "not-installed") val data = File(context.filesDir, "gog-achievements").apply { mkdirs() } val ready = CountDownLatch(1) - val command = LinuxRuntime.command(context, null, null, Environment.getExternalStorageDirectory(), - listOf("/usr/bin/env", "HOME=/root", "XDG_DATA_HOME=${data.path}/data", "XDG_CONFIG_HOME=${data.path}/config", "COMET_LOG=off", "COMET_ALLOWED_CLIENTS=${policy.path}", BIN, "--credentials-stdin")) - val pb = ProcessBuilder(command).redirectErrorStream(true) - pb.environment().apply { - remove("LD_PRELOAD"); remove("LD_AUDIT"); remove("LD_LIBRARY_PATH") - put("PROOT_LOADER", LinuxRuntime.prootLoader(context).path) - put("PROOT_TMP_DIR", LinuxRuntime.prootTempDir(context).path) - if (SessionPrefs.prootNoSeccomp(context)) put("PROOT_NO_SECCOMP", "1") - LinuxRuntime.prootLibraryPath(context).takeIf { it.isNotEmpty() }?.let { put("LD_LIBRARY_PATH", it) } + val guest = listOf("/usr/bin/env", "HOME=/root", "XDG_DATA_HOME=${data.path}/data", "XDG_CONFIG_HOME=${data.path}/config", "COMET_LOG=off", "COMET_ALLOWED_CLIENTS=${policy.path}", BIN, "--credentials-stdin") + val pb = if (com.droiddeck.launcher.runtime.NativeGuest.forSession(context)) { + val root = LinuxRuntime.rootDir(context) + com.droiddeck.launcher.runtime.NativeGuest.processBuilder(context, root, + LinuxRuntime.binds(context, null, null, Environment.getExternalStorageDirectory(), null), guest).redirectErrorStream(true) + } else { + ProcessBuilder(LinuxRuntime.command(context, null, null, Environment.getExternalStorageDirectory(), guest)).redirectErrorStream(true).also { + it.environment().apply { + remove("LD_PRELOAD"); remove("LD_AUDIT"); remove("LD_LIBRARY_PATH") + put("PROOT_LOADER", LinuxRuntime.prootLoader(context).path) + put("PROOT_TMP_DIR", LinuxRuntime.prootTempDir(context).path) + if (SessionPrefs.prootNoSeccomp(context)) put("PROOT_NO_SECCOMP", "1") + LinuxRuntime.prootLibraryPath(context).takeIf { it.isNotEmpty() }?.let { put("LD_LIBRARY_PATH", it) } + } + } } try { val child = pb.start() diff --git a/app/src/main/java/com/droiddeck/launcher/stores/gog/GogContent.kt b/app/src/main/java/com/droiddeck/launcher/stores/gog/GogContent.kt index 3d86fd5854..8ab48333e7 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/gog/GogContent.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/gog/GogContent.kt @@ -16,6 +16,7 @@ internal object GogContent { fun token(context: Context) = GogAuth.validToken(context) ?: error("Sign in to GOG and retry") + @JvmStatic fun builds(id: String, token: String): JSONObject { val result = json("$SYSTEM/products/$id/os/windows/builds?generation=2", token) if (result.optJSONArray("items")?.length() != 0) return result @@ -24,6 +25,36 @@ internal object GogContent { fun product(id: String, token: String): JSONObject = json("https://api.gog.com/products/$id?expand=downloads", token) + @JvmStatic + fun ownedProductIds(token: String): Set? = runCatching { + val body = StoreNet.get("https://embed.gog.com/user/data/games", bearer = token) ?: return@runCatching null + JSONObject(body).optJSONArray("owned")?.strings()?.toSet() ?: emptySet() + }.getOrNull() + + @JvmStatic + fun depotProducts(depot: JSONObject, baseProductId: String): List = + depot.optJSONArray("gameIDs")?.strings() ?: listOf(depot.optString("productId").ifEmpty { baseProductId }) + + @JvmStatic + fun clientCredentials(manifest: JSONObject): Pair? { + val clientId = manifest.optString("clientId") + val clientSecret = manifest.optString("clientSecret") + return clientId.takeIf { it.isNotBlank() }?.let { id -> clientSecret.takeIf { it.isNotBlank() }?.let { id to it } } + } + + @JvmStatic + fun tempExecutable(manifest: JSONObject): String? = manifest.optJSONArray("products")?.optJSONObject(0) + ?.optString("temp_executable")?.takeIf { it.isNotBlank() } + + @JvmStatic + fun installProducts(baseProductId: String, depots: JSONArray, installed: List, token: String, + ownership: (String) -> Set? = { ownedProductIds(it) }): Set { + val listed = depots.objects().flatMap { depotProducts(it, baseProductId) }.distinct() + val owned = if (listed.any { it != baseProductId } || installed.any { it != baseProductId }) ownership(token) else emptySet() + check(installed.all { it == baseProductId || owned?.contains(it) == true }) { "Could not confirm ownership of installed GOG DLC" } + return (listOf(baseProductId) + installed + listed.filter { it == baseProductId || owned?.contains(it) == true }).toSet() + } + fun downloadFilename(url: String): String = java.net.URLDecoder.decode(URL(url).path.substringAfterLast('/').replace("+", "%2B"), "UTF-8") fun resolveDownload(download: GogOfflineDownload, token: String): GogOfflineDownload = download.copy(parts = download.parts.map { part -> @@ -42,6 +73,7 @@ internal object GogContent { } finally { connection.disconnect() } }) + @JvmStatic fun json(url: String, token: String? = null): JSONObject { require(URL(url).protocol == "https") { "GOG metadata requires HTTPS" } val connection = URL(url).openConnection() as HttpURLConnection @@ -61,21 +93,21 @@ internal object GogContent { } fun snapshot(old: GogInstallRecord, build: GogBuild, token: String, - metadata: (String, String?) -> JSONObject = ::json): GogInstallRecord { + ownership: (String) -> Set? = { ownedProductIds(it) }, + metadata: (String, String?) -> JSONObject = ::json, + onManifest: (JSONObject) -> Unit = {}): GogInstallRecord { check(build.link.isNotBlank()) { "GOG build manifest is unavailable" } val manifest = metadata(build.link, token) + onManifest(manifest) val files = JSONArray() val depots = if (build.generation == 1) manifest.optJSONObject("product")?.optJSONArray("depots") ?: manifest.optJSONArray("depot") else manifest.optJSONArray("depots") check(depots != null) { "GOG build has no depot list" } val resolvedLanguage = StoreLanguage.pick(StoreLanguage.codesFromDepots(depots), old.language) val selectedBitness = if (depots.objects().any { it.optJSONArray("osBitness")?.strings()?.contains("64") == true }) "64" else "32" - val owned = if (old.products.any { it != old.id }) StoreNet.get("https://embed.gog.com/user/data/games", bearer = token)?.let { JSONObject(it).optJSONArray("owned")?.strings()?.toSet() } else emptySet() - val products = old.products.filter { it == old.id || owned?.contains(it) == true } - check(old.products.all { it == old.id || owned?.contains(it) == true }) { "Could not confirm ownership of installed GOG DLC" } + val products = installProducts(old.id, depots, old.products, token, ownership) for (depot in depots.objects()) { if (depot.has("redist")) continue - val ids = depot.optJSONArray("gameIDs")?.strings() ?: listOf(depot.optString("productId", old.id)) - val product = ids.firstOrNull { it in products } ?: continue + val product = depotProducts(depot, old.id).firstOrNull { it in products } ?: continue val languages = depot.optJSONArray("languages")?.strings().orEmpty() if (!languageSelected(languages, resolvedLanguage)) continue val bitness = depot.optJSONArray("osBitness")?.strings().orEmpty() @@ -108,7 +140,7 @@ internal object GogContent { } check(files.length() > 0) { "GOG build contains no matching files" } return old.copy(source = "depot", generation = build.generation, build = build.id, version = build.version, - branch = build.branch, language = resolvedLanguage, manifest = sanitize(manifest), files = files, products = products, verified = true) + branch = build.branch, language = resolvedLanguage, manifest = sanitize(manifest), files = files, products = products.toList(), verified = true) } fun languageSelected(list: List, selected: String): Boolean = list.any { it.equals("Neutral", true) } || StoreLanguage.depotMatchesResolved(list.toTypedArray(), selected) diff --git a/app/src/main/java/com/droiddeck/launcher/stores/gog/GogDownloadManager.java b/app/src/main/java/com/droiddeck/launcher/stores/gog/GogDownloadManager.java index 352fa5c1f6..a09629bc4c 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/gog/GogDownloadManager.java +++ b/app/src/main/java/com/droiddeck/launcher/stores/gog/GogDownloadManager.java @@ -125,211 +125,22 @@ public final class GogDownloadManager { private static Gen2Outcome runGen2(Context ctx, GogGame game, String token, String buildsJson, File installPath, Callback cb, AtomicBoolean cancelled, String language) { Gen2Outcome out = new Gen2Outcome(); try { - cb.onProgress("Fetching manifest…", 5); - Gen2Manifest gm = resolveGen2Manifest(buildsJson, token, cb); - if (gm == null) { out.error = "gen2 manifest unavailable"; return out; } - JSONObject manifest = gm.manifest; - JSONArray depots = manifest.optJSONArray("depots"); - if (depots == null) { out.error = "no depots in manifest"; return out; } - JSONArray products = manifest.optJSONArray("products"); - String tempExe = null; - if (products != null && products.length() > 0) { - tempExe = products.getJSONObject(0).optString("temp_executable", null); - if (tempExe != null && tempExe.isEmpty()) tempExe = null; - } - String baseProductId = manifest.optString("baseProductId", null); - if ((baseProductId == null || baseProductId.isEmpty()) && products != null && products.length() > 0) - baseProductId = products.getJSONObject(0).optString("productId", null); - if (baseProductId == null || baseProductId.isEmpty()) baseProductId = game.gameId; - JSONArray deps = manifest.optJSONArray("dependencies"); - List dependencies = new ArrayList<>(); - if (deps != null) for (int i = 0; i < deps.length(); i++) { String d = deps.optString(i, ""); if (!d.isEmpty()) dependencies.add(d); } - if (!dependencies.isEmpty()) cb.onLog("gog: build lists dependencies " + deps); - - cb.onProgress("Reading depot manifests…", 10); - List files = new ArrayList<>(); - List depotJsons = new ArrayList<>(); - JSONArray owned = new JSONArray(); - List ownedDepots = new ArrayList<>(); - for (int i = 0; i < depots.length(); i++) { - JSONObject depot = depots.getJSONObject(i); - String depotPid = depot.optString("productId", ""); - if (!depotPid.isEmpty() && !depotPid.equals(baseProductId)) continue; - owned.put(depot); - ownedDepots.add(depot); - } - String resolved = StoreLanguage.pick(StoreLanguage.codesFromDepots(owned), language); - cb.onLog("gog: language " + resolved); - for (int i = 0; i < ownedDepots.size(); i++) { - if (cancelled.get()) return out; - JSONObject depot = ownedDepots.get(i); - if (!depotSelected(depot, resolved)) continue; - String manifestHash = depot.optString("manifest"); - if (manifestHash == null || manifestHash.isEmpty()) continue; - byte[] dmRaw = fetchBytes("https://gog-cdn-fastly.gog.com/content-system/v2/meta/" + buildCdnPath(manifestHash), null); - if (dmRaw == null) { cb.onLog("gog: depot " + i + " meta fetch failed"); continue; } - String dmStr = decompressBytes(dmRaw); - if (dmStr == null) { cb.onLog("gog: depot " + i + " decompress failed"); continue; } - parseDepotManifest(dmStr, files); - depotJsons.add(dmStr); - } - if (files.isEmpty()) { out.error = "no depot files collected"; return out; } - - cb.onProgress("Fetching CDN link…", 15); - String secureLinkUrl = "https://content-system.gog.com/products/" + baseProductId + "/secure_link?_version=2&generation=2&path=/"; - String cdnBase = parseCdnUrl(httpGet(secureLinkUrl, token)); - if (cdnBase == null) { out.error = "secure link refused"; return out; } - - installPath.mkdirs(); - long plannedBytes = 0; - for (DepotFile df : files) plannedBytes += df.totalSize; - long remainingBytes = remainingInstallBytes(files, installPath); - if (remainingBytes > 0) { - long usable = com.droiddeck.launcher.stores.StoreInstallRoot.availableBytes(installPath); - if (com.droiddeck.launcher.stores.StoreInstallRoot.insufficientSpace(remainingBytes, usable)) { - out.hardStop = true; - out.error = "Not enough free space: need " + formatBytes(remainingBytes) + ", only " + formatBytes(usable) + " free"; - return out; - } - } - Collections.sort(files, (a, b) -> Long.compare(b.totalSize, a.totalSize)); - - final int total = files.size(); - final long planned = plannedBytes; - final AtomicInteger doneCount = new AtomicInteger(0); - final AtomicLong totalBytes = new AtomicLong(0); - final AtomicLong lastSpeedMs = new AtomicLong(System.currentTimeMillis()); - final AtomicLong lastSpeedB = new AtomicLong(0); - final AtomicLong speedBps = new AtomicLong(0); - final AtomicBoolean anyFailed = new AtomicBoolean(false); - final AtomicReference cdnBaseRef = new AtomicReference<>(cdnBase); - final AtomicInteger cdnRefreshCount = new AtomicInteger(0); - final int MAX_CDN_REFRESH = 5; - cb.onSizes(planned, planned); - cb.onBytes(0, planned, 0); - cb.onLog("gog: " + total + " files, " + formatBytes(planned) + ", base=" + baseProductId); - - int threads = downloadThreads(); - if (GogNative.isAvailable()) { - runNativeEngine(ctx, GogNative.KIND_GEN2_CHUNKS, "Verified…", depotJsons, installPath, cdnBaseRef, secureLinkUrl, token, - cdnRefreshCount, MAX_CDN_REFRESH, threads, threads, true, "gog base=" + baseProductId, - cancelled, anyFailed, doneCount, total, planned, totalBytes, lastSpeedMs, lastSpeedB, speedBps, cb); - } else { - cb.onLog("gog: engine=built-in (" + threads + " threads)"); - ExecutorService pool = Executors.newFixedThreadPool(threads, com.droiddeck.launcher.stores.download.DownloadQueue.workerFactory("gog-dl")); - List> futures = new ArrayList<>(); - for (DepotFile df : files) { - futures.add(pool.submit((Callable) () -> { - if (cancelled.get() || anyFailed.get()) return null; - File outFile = new File(installPath, df.relativePath); - outFile.getParentFile().mkdirs(); - if (fileVerified(outFile, df.totalSize, df.md5)) { - int done = doneCount.incrementAndGet(); - long tb = totalBytes.addAndGet(df.totalSize); - cb.onProgress("Verified…", 15 + (int) ((done / (float) total) * 80)); - cb.onBytes(tb, planned, speedBps.get()); - return null; - } - boolean ok = assembleDepotFile(df, outFile, cdnBaseRef, secureLinkUrl, token, cdnRefreshCount, MAX_CDN_REFRESH, cancelled, cb); - if (cancelled.get()) return null; - if (!ok) { cb.onLog("FAIL file=" + df.relativePath); anyFailed.set(true); return null; } - int done = doneCount.incrementAndGet(); - long tb = totalBytes.addAndGet(df.totalSize); - sampleSpeed(tb, lastSpeedMs, lastSpeedB, speedBps); - cb.onProgress("Downloading: " + baseName(df.relativePath) + speedSuffix(speedBps.get()), 15 + (int) ((done / (float) total) * 80)); - cb.onBytes(tb, planned, speedBps.get()); - return null; - })); - } - pool.shutdown(); - try { - for (Future f : futures) f.get(); - } catch (Exception e) { - pool.shutdownNow(); - out.error = "download pool error: " + e.getClass().getSimpleName(); - return out; - } - } - if (cancelled.get()) return out; - if (anyFailed.get()) { out.error = "one or more chunks failed to download"; return out; } - - cb.onProgress("Finishing…", 96); - try { - GogMaintenance.captureDepot(game.gameId, installPath, gm.buildId, buildsJson, manifest, new JSONArray(depotJsons), resolved); - } catch (Exception e) { - cb.onLog("gog: maintenance inventory not recorded: " + e.getMessage()); - } - writeFile(new File(installPath, MARKER), ("{\"gameId\":\"" + game.gameId + "\",\"buildId\":\"" + gm.buildId + "\"}").getBytes("UTF-8")); - deleteCounted(new File(installPath, ".gog_chunks"), cb); - String clientId = manifest.optString("clientId", null); - if (clientId != null && !clientId.isEmpty()) GogPrefs.get(ctx).edit().putString("client_id_" + game.gameId, clientId).apply(); - String clientSecret = manifest.optString("clientSecret", null); - if (clientSecret != null && !clientSecret.isEmpty()) GogPrefs.get(ctx).edit().putString("client_secret_" + game.gameId, clientSecret).apply(); - GameTask task = pickTask(installPath, game.gameId, tempExe, game.title); - out.result = new Result(task == null ? "" : task.path, gm.buildId, planned); - if (task != null) out.result.workDir = task.workDir; - out.result.dependencies = dependencies; - out.result.dependenciesRead = true; - out.result.language = resolved; - return out; + out.result = GogMaintenance.installContent(ctx, game, installPath, new JSONObject(buildsJson), language, cb, cancelled); + if (out.result == null && !cancelled.get()) out.error = "no usable public Windows build"; } catch (Exception e) { out.error = "exception: " + e.getClass().getSimpleName() + (e.getMessage() != null ? ": " + e.getMessage() : ""); return out; } - } - - private static final class Gen2Manifest { - final JSONObject manifest; final String buildId; - Gen2Manifest(JSONObject manifest, String buildId) { this.manifest = manifest; this.buildId = buildId; } - } - - private static Gen2Manifest resolveGen2Manifest(String buildsJson, String token, Callback cb) { - try { - JSONArray items = new JSONObject(buildsJson).optJSONArray("items"); - if (items == null || items.length() == 0) { cb.onLog("gog: no gen2 builds"); return null; } - String buildId = null, manifestUrl = null; - for (int i = 0; i < items.length(); i++) { - JSONObject item = items.getJSONObject(i); - if ("windows".equals(item.optString("os")) && (item.isNull("branch") || item.optString("branch").isEmpty())) { - buildId = item.optString("build_id"); - manifestUrl = item.optString("link"); - if (manifestUrl == null || manifestUrl.isEmpty()) manifestUrl = item.optString("meta_url"); - break; - } - } - if (buildId == null || manifestUrl == null || manifestUrl.isEmpty()) { cb.onLog("gog: no windows build"); return null; } - byte[] raw = fetchBytes(manifestUrl, token); - if (raw == null) { cb.onLog("gog: manifest fetch failed"); return null; } - String str = decompressBytes(raw); - if (str == null) { cb.onLog("gog: manifest decompress failed"); return null; } - return new Gen2Manifest(new JSONObject(str), buildId); - } catch (Exception e) { - cb.onLog("gog: manifest " + e.getClass().getSimpleName()); - return null; - } - } - - private static boolean depotSelected(JSONObject depot, String resolved) { - JSONArray languages = depot.optJSONArray("languages"); - if (languages == null || languages.length() == 0) return true; - String[] listed = new String[languages.length()]; - for (int i = 0; i < languages.length(); i++) listed[i] = languages.optString(i, ""); - return StoreLanguage.depotMatchesResolved(listed, resolved); + return out; } public static List listLanguages(Context ctx, String gameId) { try { String token = GogAuth.validToken(ctx); - String buildsUrl = "https://content-system.gog.com/products/" + gameId + "/os/windows/builds?generation=2"; - String buildsJson = httpGet(buildsUrl, null); - if (buildsJson == null) buildsJson = httpGet(buildsUrl, token); - if (buildsJson == null) return Collections.emptyList(); - Callback silent = new Callback() { - @Override public void onProgress(String message, int pct) {} - }; - Gen2Manifest gm = resolveGen2Manifest(buildsJson, token, silent); - if (gm == null) return Collections.emptyList(); - return StoreLanguage.codesFromDepots(gm.manifest.optJSONArray("depots")); + JSONObject builds = GogContent.builds(gameId, token); + GogBuild build = GogBuild.Companion.select(builds, "", ""); + if (build == null || build.getGeneration() != 2) return Collections.emptyList(); + return StoreLanguage.codesFromDepots(GogContent.json(build.getLink(), token).optJSONArray("depots")); } catch (Exception e) { return Collections.emptyList(); } @@ -398,7 +209,7 @@ public final class GogDownloadManager { int threads = downloadThreads(); if (GogNative.isAvailable()) { runNativeEngine(ctx, GogNative.KIND_GEN1_RANGES, "Resuming…", Collections.singletonList(manifestStr), installPath, - new AtomicReference<>(""), null, null, new AtomicInteger(0), 0, threads, threads, false, "gog gen1=" + game.gameId, + threads, threads, false, "gog gen1=" + game.gameId, cancelled, anyFailed, done, total, planned, totalBytes, lastSpeedMs, lastSpeedB, speedBps, cb); } else { ExecutorService pool = Executors.newFixedThreadPool(threads, com.droiddeck.launcher.stores.download.DownloadQueue.workerFactory("gog-dl")); @@ -659,8 +470,7 @@ public final class GogDownloadManager { private static void runNativeEngine(Context ctx, int kind, String verifiedMsg, List depotJsons, File installPath, - AtomicReference cdnBaseRef, String secureLinkUrl, String token, - AtomicInteger cdnRefreshCount, int maxCdnRefresh, int maxWorkers, int processWorkers, + int maxWorkers, int processWorkers, boolean sortLargestFirst, String label, AtomicBoolean cancelled, AtomicBoolean anyFailed, AtomicInteger doneCount, int total, long planned, AtomicLong totalBytes, AtomicLong lastSpeedMs, AtomicLong lastSpeedB, AtomicLong speedBps, Callback cb) { @@ -673,10 +483,8 @@ public final class GogDownloadManager { int run = 0; while (!cancelled.get()) { run++; - final String base = cdnBaseRef.get(); final CountDownLatch latch = new CountDownLatch(1); final AtomicBoolean okRef = new AtomicBoolean(false); - final AtomicBoolean linkExpiryRef = new AtomicBoolean(false); final AtomicReference errRef = new AtomicReference<>(""); final long runStart = totalBytes.get(); final AtomicLong verifiedInRun = new AtomicLong(0); @@ -702,12 +510,10 @@ public final class GogDownloadManager { @Override public void onLog(String line) { cb.onLog(line); } @Override public void onMaintenance(long networkBytes, long reusedBytes, long appliedBytes) {} @Override public void onComplete(boolean success, boolean wasCancelled, boolean linkExpiry, String error, long bytesWritten, int filesDone) { - okRef.set(success); linkExpiryRef.set(linkExpiry); errRef.set(error == null ? "" : error); latch.countDown(); + okRef.set(success); errRef.set(error == null ? "" : error); latch.countDown(); } }; - String[] bases = (base == null || base.isEmpty()) ? new String[0] : base.split("\n"); - if (run == 1) cb.onLog("gog: " + bases.length + " CDN host(s) offered"); - long handle = GogNative.start(kind, manifests, bases, installPath.getAbsolutePath(), donePaths.toArray(new String[0]), "", + long handle = GogNative.start(kind, manifests, new String[0], installPath.getAbsolutePath(), donePaths.toArray(new String[0]), "", workers, process, sortLargestFirst, label + " run=" + run, listener); if (handle == 0L) { cb.onLog("gog: native start failed"); anyFailed.set(true); return; } boolean interrupted = false; @@ -721,85 +527,13 @@ public final class GogDownloadManager { } if (interrupted) { Thread.currentThread().interrupt(); anyFailed.set(true); return; } if (cancelled.get() || okRef.get()) return; - String err = errRef.get(); - if (linkExpiryRef.get() && tryRefreshCdn(cdnBaseRef, base, secureLinkUrl, token, cdnRefreshCount, maxCdnRefresh, cb)) { - cb.onLog("gog: secure link expired (" + err + "); refreshed, running again for the remaining files"); - continue; - } - cb.onLog("gog: engine failed: " + err); + cb.onLog("gog: engine failed: " + errRef.get()); anyFailed.set(true); return; } } - private static void parseDepotManifest(String json, List out) { - try { - JSONObject depotObj = new JSONObject(json).optJSONObject("depot"); - if (depotObj == null) return; - JSONArray depot = depotObj.optJSONArray("items"); - if (depot == null) return; - for (int i = 0; i < depot.length(); i++) { - JSONObject entry = depot.getJSONObject(i); - String path = entry.optString("path", "").replace("\\", "/"); - if (path.startsWith("/")) path = path.substring(1); - JSONArray chunks = entry.optJSONArray("chunks"); - if (path.isEmpty() || chunks == null || chunks.length() == 0) continue; - DepotFile df = new DepotFile(path); - String fileMd5 = entry.optString("md5", null); - if (fileMd5 != null && !fileMd5.isEmpty()) df.md5 = fileMd5; - long fileTotal = 0; - for (int c = 0; c < chunks.length(); c++) { - JSONObject chunk = chunks.getJSONObject(c); - String compressedMd5 = chunk.optString("compressedMd5"), decMd5 = chunk.optString("md5"); - String hash = (compressedMd5 != null && !compressedMd5.isEmpty()) ? compressedMd5 : decMd5; - if (hash == null || hash.isEmpty()) continue; - long decSize = chunk.optLong("size", 0); - df.chunks.add(new DepotFile.ChunkRef(hash, compressedMd5, decMd5, chunk.optLong("compressedSize", 0), decSize)); - fileTotal += decSize; - } - df.totalSize = fileTotal; - if (!df.chunks.isEmpty()) out.add(df); - } - } catch (Exception e) { - Log.w(TAG, "depot manifest parse: " + e.getClass().getSimpleName()); - } - } - - static String buildCdnPath(String hash) { return hash.substring(0, 2) + "/" + hash.substring(2, 4) + "/" + hash; } - - private static boolean assembleDepotFile(DepotFile df, File outFile, AtomicReference cdnBaseRef, String secureLinkUrl, String token, - AtomicInteger cdnRefreshCount, int maxCdnRefresh, AtomicBoolean cancelled, Callback cb) { - File tmpFile = new File(outFile.getAbsolutePath() + ".bhtmp"); - File parent = outFile.getParentFile(); - if (parent != null) parent.mkdirs(); - tmpFile.delete(); - boolean ok = true; - try (FileOutputStream fos = new FileOutputStream(tmpFile)) { - for (DepotFile.ChunkRef chunk : df.chunks) { - if (cancelled.get()) { ok = false; break; } - byte[] inflated = fetchChunkVerified(cdnBaseRef, buildCdnPath(chunk.hash), chunk, secureLinkUrl, token, cdnRefreshCount, maxCdnRefresh, cancelled, cb); - if (inflated == null) { ok = false; break; } - fos.write(inflated); - } - } catch (Exception e) { - ok = false; - } - if (!ok || cancelled.get()) { tmpFile.delete(); return false; } - if (df.totalSize > 0 && tmpFile.length() != df.totalSize) { cb.onLog("size mismatch file=" + df.relativePath); tmpFile.delete(); return false; } - if (df.md5 != null && !df.md5.isEmpty()) { - String actual = md5HexFile(tmpFile); - if (actual == null || !actual.equalsIgnoreCase(df.md5)) { cb.onLog("md5 mismatch file=" + df.relativePath); tmpFile.delete(); return false; } - } - if (outFile.exists()) outFile.delete(); - return tmpFile.renameTo(outFile); - } - - private static String parseCdnUrl(String json) { - java.util.List all = parseCdnUrls(json); - return all.isEmpty() ? null : String.join("\n", all); - } - static java.util.List parseCdnUrls(String json) { java.util.List out = new ArrayList<>(); if (json == null) return out; @@ -824,18 +558,6 @@ public final class GogDownloadManager { return out; } - private static final AtomicInteger CDN_TURN = new AtomicInteger(); - - static String pickCdn(String set) { - String[] all = set.split("\n"); - return all[Math.floorMod(CDN_TURN.getAndIncrement(), all.length)]; - } - - static String firstCdn(String set) { - int nl = set.indexOf('\n'); - return nl < 0 ? set : set.substring(0, nl); - } - private static boolean downloadRange(String url, long offset, long size, File out) { try { HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); @@ -951,15 +673,6 @@ public final class GogDownloadManager { } } - private static String buildChunkUrl(String base, String chunkPath) { - int q = base.indexOf('?'); - return q >= 0 ? base.substring(0, q) + "/" + chunkPath + base.substring(q) : base + "/" + chunkPath; - } - - private static String md5Hex(byte[] data) { - try { return toHex(java.security.MessageDigest.getInstance("MD5").digest(data)); } catch (Exception e) { return null; } - } - private static String md5HexFile(File f) { try (FileInputStream fis = new FileInputStream(f)) { java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5"); @@ -988,58 +701,6 @@ public final class GogDownloadManager { return true; } - private static void sleepBackoff(int attempt) { - try { Thread.sleep(Math.min(1000L << Math.max(0, attempt - 1), 8000L)); } catch (InterruptedException ie) { Thread.currentThread().interrupt(); } - } - - private static boolean tryRefreshCdn(AtomicReference cdnBaseRef, String staleBase, String secureLinkUrl, String token, - AtomicInteger refreshCount, int cap, Callback cb) { - if (secureLinkUrl == null) return false; - synchronized (cdnBaseRef) { - String current = cdnBaseRef.get(); - if (staleBase == null || !staleBase.equals(current)) return true; - if (refreshCount.get() >= cap) { cb.onLog("gog: CDN refresh cap reached (" + cap + ")"); return false; } - String neu = parseCdnUrl(httpGet(secureLinkUrl, token)); - if (neu == null || neu.isEmpty()) { cb.onLog("gog: CDN refresh failed"); return false; } - cdnBaseRef.set(neu); - cb.onLog("gog: secure link refreshed #" + refreshCount.incrementAndGet()); - return true; - } - } - - private static byte[] fetchChunkVerified(AtomicReference cdnBaseRef, String chunkPath, DepotFile.ChunkRef chunk, String secureLinkUrl, String token, - AtomicInteger cdnRefreshCount, int maxCdnRefresh, AtomicBoolean cancelled, Callback cb) { - int hardFail = 0, guard = 0; - while (guard++ < 8) { - if (cancelled.get()) return null; - String base = cdnBaseRef.get(); - HttpResult res = fetchBytesEx(buildChunkUrl(pickCdn(base), chunkPath), null); - if (res.body == null) { - int code = res.status; - if ((code == 401 || code == 403 || code == 404 || code == 500) && tryRefreshCdn(cdnBaseRef, base, secureLinkUrl, token, cdnRefreshCount, maxCdnRefresh, cb)) continue; - cb.onLog("chunk http fail code=" + code); - if (++hardFail >= 3) return null; - sleepBackoff(hardFail); - continue; - } - byte[] raw = res.body; - if (chunk.compressedSize > 0 && raw.length != chunk.compressedSize) { if (++hardFail >= 3) return null; sleepBackoff(hardFail); continue; } - if (chunk.compressedMd5 != null && !chunk.compressedMd5.isEmpty()) { - String actual = md5Hex(raw); - if (actual == null || !actual.equalsIgnoreCase(chunk.compressedMd5)) { if (++hardFail >= 3) return null; sleepBackoff(hardFail); continue; } - } - byte[] inflated = inflateZlib(raw); - if (inflated == null) inflated = raw; - if (chunk.size > 0 && inflated.length != chunk.size) { if (++hardFail >= 3) return null; sleepBackoff(hardFail); continue; } - if (chunk.md5 != null && !chunk.md5.isEmpty()) { - String actual = md5Hex(inflated); - if (actual == null || !actual.equalsIgnoreCase(chunk.md5)) { if (++hardFail >= 3) return null; sleepBackoff(hardFail); continue; } - } - return inflated; - } - return null; - } - private static void sampleSpeed(long totalBytes, AtomicLong lastSpeedMs, AtomicLong lastSpeedB, AtomicLong speedBps) { long nowMs = System.currentTimeMillis(), prevMs = lastSpeedMs.get(); if (nowMs - prevMs >= 500 && lastSpeedMs.compareAndSet(prevMs, nowMs)) { @@ -1289,30 +950,6 @@ public final class GogDownloadManager { for (File f : files) if (f.isDirectory()) collectExeRecursive(f, out); } - static long remainingInstallBytes(List files, File installPath) { - long remaining = 0; - for (DepotFile file : files) { - File output = new File(installPath, file.relativePath); - if (!fileVerified(output, file.totalSize, file.md5)) remaining += file.totalSize; - } - return remaining; - } - - static final class DepotFile { - final String relativePath; - final List chunks = new ArrayList<>(); - String md5; - long totalSize; - DepotFile(String relativePath) { this.relativePath = relativePath; } - static final class ChunkRef { - final String hash, compressedMd5, md5; - final long compressedSize, size; - ChunkRef(String hash, String compressedMd5, String md5, long compressedSize, long size) { - this.hash = hash; this.compressedMd5 = compressedMd5; this.md5 = md5; this.compressedSize = compressedSize; this.size = size; - } - } - } - private static final class Gen1File { final String path, url; final long offset, size; Gen1File(String path, String url, long offset, long size) { this.path = path; this.url = url; this.offset = offset; this.size = size; } @@ -1382,7 +1019,11 @@ public final class GogDownloadManager { String resolved = StoreLanguage.pick(StoreLanguage.codesFromDepots(depots), StoreLanguage.preferredCode()); for (int i = 0; i < depots.length(); i++) { JSONObject depot = depots.getJSONObject(i); - if (depotSelected(depot, resolved)) total += depot.optLong("size", 0); + JSONArray languages = depot.optJSONArray("languages"); + String[] listed = new String[languages == null ? 0 : languages.length()]; + for (int j = 0; j < listed.length; j++) listed[j] = languages.optString(j, ""); + if (listed.length == 0 || StoreLanguage.depotMatchesResolved(listed, resolved)) + total += depot.optLong("size", 0); } return total > 0 ? total : -1; } catch (Exception e) { diff --git a/app/src/main/java/com/droiddeck/launcher/stores/gog/GogLibrary.kt b/app/src/main/java/com/droiddeck/launcher/stores/gog/GogLibrary.kt index 8f48740ee2..5b8be22303 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/gog/GogLibrary.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/gog/GogLibrary.kt @@ -58,16 +58,17 @@ object GogLibrary { val token = GogAuth.validToken(app) ?: return SyncResult.NotLoggedIn onStatus(app.getString(R.string.stores_sync_game_list)) val listing = ownedListing(token) + val account = ownedIds(token) val listed: List = listing?.games - ?: ownedIds(token)?.map { GogGame(it, "", "", "", "", "", 1, null, "", "") } + ?: account?.map { GogGame(it, "", "", "", "", "", 1, null, "", "") } ?: return SyncResult.Failed(app.getString(R.string.stores_sync_gog_unreachable)) val named = listing != null - StoresState.logLine(if (named) "GOG: listing ${listed.size} games" else "GOG: listing unreadable, ${listed.size} owned ids") - val ownedSet = listing?.owned ?: listed.mapTo(HashSet()) { it.gameId } val cachedList = cached(app) + val cachedIds = cachedList.mapTo(HashSet()) { it.gameId } + val ownedSet = ownedSet(listing?.owned ?: listed.mapTo(HashSet()) { it.gameId }, account, cachedIds) + StoresState.logLine(if (named) "GOG: listing ${listed.size} games, ${ownedSet.size} owned ids" else "GOG: listing unreadable, ${listed.size} owned ids") val stale = System.currentTimeMillis() - p.getLong(LAST_SYNC_KEY, 0L) >= THROTTLE_MS val heavy = force || cachedList.isEmpty() || stale - val cachedIds = cachedList.mapTo(HashSet()) { it.gameId } val skipped = java.util.Collections.newSetFromMap(java.util.concurrent.ConcurrentHashMap()) skipped.addAll(p.getStringSet(SKIPPED_KEY, emptySet()).orEmpty()) val idsToFetch = if (heavy) ownedSet.toList() else ownedSet.filter { it !in cachedIds && it !in skipped } @@ -133,6 +134,9 @@ object GogLibrary { return all.ifEmpty { null }?.let { Listing(it, owned) } } + fun ownedSet(listed: Set, account: List?, cached: Set = emptySet()): Set = + if (account == null) listed + cached else listed + account + private fun ownedIds(token: String): List? { val json = StoreNet.get(OWNED_URL, bearer = token, userAgent = GogAuth.GALAXY_UA) ?: return null val owned = runCatching { JSONObject(json).optJSONArray("owned") }.getOrNull() ?: return null diff --git a/app/src/main/java/com/droiddeck/launcher/stores/gog/GogMaintenance.kt b/app/src/main/java/com/droiddeck/launcher/stores/gog/GogMaintenance.kt index 84cb27802d..e024d7d951 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/gog/GogMaintenance.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/gog/GogMaintenance.kt @@ -377,16 +377,24 @@ internal object GogMaintenance { val build = GogBuild.select(builds) ?: return null folder.mkdirs() val old = GogInstallRecord(game.gameId, "depot", language = language) - val target = GogContent.snapshot(old, build, GogContent.token(context)) + var credentials: Pair? = null + val target = GogContent.snapshot(old, build, GogContent.token(context), onManifest = { manifest -> + credentials = GogContent.clientCredentials(manifest) + }) GogFiles.validate(target.files, folder) val bytes = target.files.objects().sumOf { it.optJSONArray("chunks")?.objects()?.sumOf { c -> c.optLong("compressedSize") }?.takeIf { n -> n > 0 } ?: it.optLong("size") } val disk = target.files.objects().sumOf { it.optLong("size") } - check(!StoreInstallRoot.insufficientSpace(disk, StoreInstallRoot.availableBytes(folder))) { "Not enough free space for GOG installation" } + val remaining = GogEstimate.calculate(folder, old, target, JSONArray(), reuseChunks = GogNative.isAvailable()).required + check(!StoreInstallRoot.insufficientSpace(remaining, StoreInstallRoot.availableBytes(folder))) { "Not enough free space for GOG installation" } callback.onSizes(bytes, disk) transfer(context, Proposal(folder, old, target, GogOperation.UPDATE, bytes, disk), null, cancelled, callback) if (cancelled.get()) return null target.write(folder) - val task = GogDownloadManager.pickTask(folder, game.gameId, null, game.title) + credentials?.let { (clientId, clientSecret) -> + GogPrefs.get(context).edit().putString("client_id_${game.gameId}", clientId) + .putString("client_secret_${game.gameId}", clientSecret).apply() + } + val task = GogDownloadManager.pickTask(folder, game.gameId, GogContent.tempExecutable(target.manifest), game.title) val result = GogDownloadManager.Result(task?.path ?: "", build.id, disk) result.workDir = task?.workDir result.language = target.language @@ -395,20 +403,6 @@ internal object GogMaintenance { return result } - @JvmStatic - fun captureDepot(id: String, folder: File, build: String, buildsJson: String, manifest: JSONObject, depots: JSONArray, language: String) { - val selected = GogBuild.select(JSONObject(buildsJson), pinned = build) - val files = JSONArray() - for (depot in depots.strings()) for (item in JSONObject(depot).getJSONObject("depot").getJSONArray("items").objects()) { - val chunks = item.optJSONArray("chunks") ?: continue - files.put(JSONObject().put("path", item.getString("path").replace('\\', '/')).put("size", chunks.objects().sumOf { it.optLong("size") }) - .put("md5", item.optString("md5")).put("chunks", chunks).put("product", id)) - } - GogFiles.validate(files, folder) - GogInstallRecord(id, "depot", 2, build, selected?.version.orEmpty(), selected?.branch.orEmpty(), language, - manifest = GogContent.sanitize(manifest), files = files).write(folder) - } - @JvmStatic fun captureOffline(id: String, folder: File, downloads: JSONObject) { val selected = GogOfflineDownload.parse(downloads, "installers").firstOrNull() ?: return diff --git a/app/src/main/java/com/droiddeck/launcher/stores/gog/GogNative.kt b/app/src/main/java/com/droiddeck/launcher/stores/gog/GogNative.kt index 80024ab6ac..0714117eb9 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/gog/GogNative.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/gog/GogNative.kt @@ -6,8 +6,6 @@ import com.droiddeck.launcher.stores.StoresNative object GogNative { private const val TAG = "GogNative" - const val KIND_GEN2_CHUNKS = 0 - const val KIND_GEN1_RANGES = 1 interface Listener { diff --git a/app/src/main/java/com/droiddeck/launcher/stores/ubisoft/UbisoftActivation.kt b/app/src/main/java/com/droiddeck/launcher/stores/ubisoft/UbisoftActivation.kt new file mode 100644 index 0000000000..d11fde4210 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/ubisoft/UbisoftActivation.kt @@ -0,0 +1,74 @@ +package com.droiddeck.launcher.stores.ubisoft + +import android.content.Context +import com.droiddeck.launcher.runtime.LinuxRuntime +import com.droiddeck.launcher.session.SessionState +import com.droiddeck.launcher.stores.StoresState +import org.json.JSONObject +import java.io.File + +/** + * Answers Denuvo activations for a running Ubisoft game. droiddeck-upc.exe writes the challenge + * the game's aux DLL sends to `bridge-/denuvo-.req`; the answer goes to `.rsp`. + */ +object UbisoftActivation { + private const val POLL_MS = 250L + private const val IDLE_MS = 60_000L + private val watching = HashMap() + + fun bridge(context: Context, product: Long) = File(LinuxRuntime.rootDir(context), "root/.cache/droiddeck/ubisoft/bridge-$product") + + /** Starts answering the product's activations; stale requests are cleared before this returns. */ + fun watch(app: Context, product: Long) { + val dir = bridge(app, product) + synchronized(watching) { + val running = watching.containsKey(product) + watching[product] = System.currentTimeMillis() + if (running) return + dir.mkdirs() + dir.listFiles()?.forEach { it.delete() } + } + Thread({ + var idleSince = 0L + while (true) { + runCatching { dir.listFiles { f -> f.name.startsWith("denuvo-") && f.name.endsWith(".req") }?.forEach { answer(app, product, it) } } + .onFailure { StoresState.logLine("Ubisoft: activation for $product: ${it.javaClass.simpleName}") } + val now = System.currentTimeMillis() + if (SessionState.running) idleSince = 0L + else if (idleSince == 0L) idleSince = now + else if (now - idleSince > IDLE_MS) synchronized(watching) { + if (now - (watching[product] ?: 0L) > IDLE_MS) { watching.remove(product); return@Thread } + } + runCatching { Thread.sleep(POLL_MS) } + } + }, "ubisoft-activation-$product").start() + } + + private fun answer(app: Context, product: Long, request: File) { + val challenge = runCatching { request.readBytes() }.getOrNull() ?: return + request.delete() + val reply = File(request.parentFile, request.name.removeSuffix(".req") + ".rsp") + val token = activate(app, product, challenge) + val part = File(reply.path + ".part") + part.writeBytes(token ?: ByteArray(0)) + part.renameTo(reply) + } + + private fun activate(app: Context, product: Long, challenge: ByteArray): ByteArray? { + val s = UbisoftAuth.current(app) ?: return null.also { StoresState.logLine("Ubisoft: activation for $product: signed out") } + val answer = UbisoftNative.call("denuvo", JSONObject().put("ticket", s.ticket).put("sessionId", s.sessionId) + .put("productId", product).put("challenge", hex(challenge))) + answer?.optString("error")?.takeIf { it.isNotEmpty() }?.let { + StoresState.logLine("Ubisoft: activation for $product: $it") + return null + } + val token = answer?.optString("token")?.takeIf { it.isNotEmpty() }?.let(::unhex) + StoresState.logLine("Ubisoft: activation for $product: ${token?.size ?: 0} B game token") + return token + } + + internal fun hex(b: ByteArray): String = b.joinToString("") { "%02x".format(it) } + + internal fun unhex(s: String): ByteArray? = + if (s.length % 2 != 0) null else runCatching { ByteArray(s.length / 2) { s.substring(it * 2, it * 2 + 2).toInt(16).toByte() } }.getOrNull() +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/ubisoft/UbisoftAuth.kt b/app/src/main/java/com/droiddeck/launcher/stores/ubisoft/UbisoftAuth.kt new file mode 100644 index 0000000000..f1cf2ea84d --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/ubisoft/UbisoftAuth.kt @@ -0,0 +1,176 @@ +package com.droiddeck.launcher.stores.ubisoft + +import android.content.Context +import android.util.Log +import com.droiddeck.launcher.stores.Store +import com.droiddeck.launcher.stores.StoreAccounts +import org.json.JSONArray +import org.json.JSONObject +import java.net.HttpURLConnection +import java.net.URL +import java.net.URLEncoder + +/** UbiServices sessions: a ticket from Ubisoft's own web sign-in, exchanged for a launcher session that remember-me renews. */ +object UbisoftAuth { + private const val TAG = "UbisoftAuth" + const val SESSIONS = "https://public-ubiservices.ubi.com/v3/profiles/sessions" + + /** The web app and genome connect.ubisoft.com's sign-in page accepts. */ + private const val WEB_APP_ID = "f35adcb5-1911-440c-b1c9-48fdc1701c68" + private const val WEB_GENOME_ID = "5b36b900-65d8-47f3-93c8-86bdaa48ab50" + + /** The Ubisoft Connect PC app id; its sessions are accepted by the library GraphQL and Demux. */ + const val APP_ID = "f68a4bb5-608a-4ff2-8123-be8ef797e0a6" + private const val REFRESH_MARGIN_MS = 10 * 60 * 1000L + private val lock = Any() + + class Session( + val ticket: String, + val sessionId: String, + val profileId: String, + val userId: String, + val name: String, + val expiresAt: Long, + val rememberMe: String, + ) + + class Reply(val status: Int, val body: String, val headers: Map) { + val ok get() = status in 200..299 + } + + private val loggedIn = "https://connect.ubisoft.com/logged-in.html?appId=$WEB_APP_ID&lang=en-US&genomeId=$WEB_GENOME_ID" + val loginUrl = "https://connect.ubisoft.com/login?appId=$WEB_APP_ID&genomeId=$WEB_GENOME_ID&lang=en-US&nextUrl=" + URLEncoder.encode(loggedIn, "UTF-8") + val SIGN_IN_URLS = listOf("https://connect.ubisoft.com", "https://account.ubisoft.com", "https://ubisoft.com") + + fun isLoggedInPage(url: String) = url.startsWith("https://connect.ubisoft.com/logged-in.html") + + /** Reads connect.ubisoft.com's stored sign-in; the WebAuth SDK keeps it under keys holding `loginData` and `rememberMe`. */ + const val PAGE_SCRIPT = "(function(){try{var o={};for(var i=0;i = emptyMap()): Reply { + val c = URL(url).openConnection() as HttpURLConnection + c.requestMethod = method + c.connectTimeout = 15_000 + c.readTimeout = 25_000 + c.setRequestProperty("Ubi-AppId", APP_ID) + c.setRequestProperty("Ubi-RequestedPlatformType", "uplay") + c.setRequestProperty("Content-Type", "application/json") + c.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36") + authorization?.let { c.setRequestProperty("Authorization", it) } + sessionId?.let { c.setRequestProperty("Ubi-SessionId", it) } + for ((k, v) in extra) c.setRequestProperty(k, v) + try { + if (body != null) { + c.doOutput = true + c.outputStream.use { it.write(body.toByteArray()) } + } + val status = c.responseCode + val text = (if (status in 200..299) c.inputStream else c.errorStream)?.use { it.readBytes().toString(Charsets.UTF_8) }.orEmpty() + val headers = c.headerFields.filterKeys { it != null }.mapKeys { it.key.lowercase() }.mapValues { it.value.joinToString(",") } + return Reply(status, text, headers) + } finally { + c.disconnect() + } + } + + /** POSTs a session request, replaying it once when UbiServices answers with a challenge. */ + private fun session(authorization: String): Reply { + val body = JSONObject().put("rememberMe", true).toString() + val first = request(SESSIONS, "POST", authorization, body = body) + val challenge = first.headers["ubi-challenge"] ?: return first + return request(SESSIONS, "POST", authorization, body = body, extra = mapOf("Ubi-Challenge" to challenge)) + } + + internal fun parseSession(body: String, rememberFallback: String = ""): Session? { + val o = runCatching { JSONObject(body) }.getOrNull() ?: return null + val ticket = o.optString("ticket").takeIf { it.isNotEmpty() && it != "null" } ?: return null + return Session( + ticket = ticket, + sessionId = o.optString("sessionId"), + profileId = o.optString("profileId"), + userId = o.optString("userId"), + name = o.optString("nameOnPlatform"), + expiresAt = parseTime(o.optString("expiration")), + rememberMe = o.optString("rememberMeTicket").takeIf { it.isNotEmpty() && it != "null" } ?: rememberFallback, + ) + } + + internal fun parseTime(s: String): Long = runCatching { java.time.OffsetDateTime.parse(s).toInstant().toEpochMilli() } + .recoverCatching { java.time.Instant.parse(s).toEpochMilli() }.getOrDefault(System.currentTimeMillis() + 60 * 60 * 1000L) + + /** Values the web sign-in stored, found wherever the SDK nests them. */ + internal fun webValues(dump: String): Map { + val out = HashMap() + fun walk(v: Any?) { + when (v) { + is JSONObject -> v.keys().forEach { k -> + val x = v.opt(k) + if (x is String && x.isNotEmpty() && k !in out) { + val nested = if (x.startsWith("{") || x.startsWith("[")) runCatching { org.json.JSONTokener(x).nextValue() }.getOrNull() else null + if (nested != null) walk(nested) else out[k] = x + } else walk(x) + } + is JSONArray -> for (i in 0 until v.length()) walk(v.opt(i)) + } + } + val top = runCatching { org.json.JSONTokener(dump).nextValue() }.getOrNull() + walk(if (top is String) runCatching { JSONObject(top) }.getOrNull() else top) + return out + } + + /** Turns the page's stored sign-in into a launcher session; returns null on success or a reason. */ + fun completeWebSignIn(context: Context, dump: String): String? { + val web = webValues(dump) + val ticket = web["ticket"] + val remember = web["rememberMeTicket"].orEmpty() + val fresh = when { + ticket != null -> session("ubi_v1 t=$ticket").let { r -> + parseSession(r.body, remember) ?: run { Log.w(TAG, "ticket exchange: ${r.status} ${r.body.take(200)}"); null } + } + else -> null + } ?: remember.takeIf { it.isNotEmpty() }?.let { rm -> parseSession(session("rm_v1 t=$rm").body, rm) } + ?: return "no ticket" + save(context, fresh) + com.droiddeck.launcher.session.SessionFiles.ensureUbisoftService(context) + return null + } + + private fun save(context: Context, s: Session) { + val name = s.name.ifEmpty { profileName(s) } + StoreAccounts.write(context.applicationContext, Store.UBISOFT, JSONObject() + .put("access_token", s.ticket) + .put("session_id", s.sessionId) + .put("profile_id", s.profileId) + .put("user_id", s.userId) + .put("display_name", name) + .put("expires_at", s.expiresAt) + .put("remember_me", s.rememberMe)) + } + + private fun profileName(s: Session): String = runCatching { + JSONObject(request("https://public-ubiservices.ubi.com/v3/profiles/me", authorization = "ubi_v1 t=${s.ticket}", sessionId = s.sessionId).body).optString("nameOnPlatform") + }.getOrDefault("") + + fun load(context: Context): Session? { + val o = StoreAccounts.read(context.applicationContext, Store.UBISOFT) ?: return null + val ticket = o.optString("access_token").takeIf { it.isNotEmpty() } ?: return null + return Session(ticket, o.optString("session_id"), o.optString("profile_id"), o.optString("user_id"), o.optString("display_name"), o.optLong("expires_at"), o.optString("remember_me")) + } + + /** A live session, renewed through remember-me shortly before it expires. */ + fun current(context: Context): Session? = synchronized(lock) { + val s = load(context) ?: return null + if (s.expiresAt - System.currentTimeMillis() > REFRESH_MARGIN_MS || s.rememberMe.isEmpty()) return s + val renewed = runCatching { session("rm_v1 t=${s.rememberMe}") }.getOrNull()?.let { parseSession(it.body, s.rememberMe) } + if (renewed == null) { + Log.w(TAG, "remember-me renewal failed") + return if (s.expiresAt > System.currentTimeMillis()) s else null + } + save(context, Session(renewed.ticket, renewed.sessionId, renewed.profileId.ifEmpty { s.profileId }, renewed.userId.ifEmpty { s.userId }, + renewed.name.ifEmpty { s.name }, renewed.expiresAt, renewed.rememberMe)) + load(context) + } + + fun signOut(context: Context) = synchronized(lock) { StoreAccounts.clear(context.applicationContext, Store.UBISOFT) } +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/ubisoft/UbisoftBackend.kt b/app/src/main/java/com/droiddeck/launcher/stores/ubisoft/UbisoftBackend.kt new file mode 100644 index 0000000000..09008e1927 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/ubisoft/UbisoftBackend.kt @@ -0,0 +1,246 @@ +package com.droiddeck.launcher.stores.ubisoft + +import android.content.Context +import android.content.Intent +import android.util.Log +import android.webkit.WebView +import com.droiddeck.launcher.R +import com.droiddeck.launcher.stores.CatalogItem +import com.droiddeck.launcher.stores.InstalledStoreGame +import com.droiddeck.launcher.stores.LoginFlows +import com.droiddeck.launcher.stores.Store +import com.droiddeck.launcher.stores.StoreBackend +import com.droiddeck.launcher.stores.StoreInstallRoot +import com.droiddeck.launcher.stores.download.DownloadEntry +import com.droiddeck.launcher.stores.download.DownloadQueue +import com.droiddeck.launcher.stores.StoreLoginActivity +import com.droiddeck.launcher.stores.StoresState +import com.droiddeck.launcher.stores.WebCookies +import org.json.JSONObject + +object UbisoftBackend : StoreBackend { + override val store: Store = Store.UBISOFT + + init { + LoginFlows.ubisoftFlow = { ua -> UbisoftLogin(ua) } + } + + override fun signIn(context: Context) { + context.startActivity( + Intent(context, StoreLoginActivity::class.java) + .putExtra(StoreLoginActivity.EXTRA_STORE, store.id) + .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK), + ) + } + + override fun signOut(context: Context) { + UbisoftAuth.signOut(context.applicationContext) + UbisoftLibrary.reset() + runCatching { WebCookies.expire(UbisoftAuth.SIGN_IN_URLS, ".ubisoft.com", listOf("/")) {} } + } + + override fun syncLibrary(context: Context, force: Boolean) { + val app = context.applicationContext + Thread({ UbisoftLibrary.sync(app, force) }, "ubisoft-sync").start() + } + + override fun loadShelves(context: Context, force: Boolean) {} + + override fun install(context: Context, item: CatalogItem, root: java.io.File) { + val app = context.applicationContext + item.extra[STEAM_APP]?.takeIf { item.extra["productId"] == null }?.let { steam -> + StoresState.logLine("Ubisoft: ${item.title} is owned on Steam; installing Steam app $steam") + installInSteam(app, steam) + return + } + if (!UbisoftNative.ready()) { + StoresState.logLine(UbisoftNative.ENGINE_MISSING) + StoresState.post { StoresState.problems[store] = UbisoftNative.ENGINE_MISSING } + return + } + val folder = StoreInstallRoot.folderFor(app, store, item.id, item.title, root) + val entry = DownloadEntry(store, item.id, item.title, cover = item.tallImageUrl ?: item.imageUrl, + bytesTotal = item.sizeBytes, location = StoreInstallRoot.labelFor(app, folder)) + DownloadQueue.enqueue(app, entry) { UbisoftInstaller(app, item, folder) } + } + + override fun uninstall(context: Context, game: InstalledStoreGame) {} + + const val STEAM_APP = "steamAppId" + + /** Steam asks to install a game it does not have when told to run it. */ + private fun installInSteam(app: Context, steamAppId: String) { + if (com.droiddeck.launcher.session.SessionState.running) { + if (!com.droiddeck.launcher.session.SessionService.launchGame(app, steamAppId)) StoresState.logLine("Ubisoft: Steam is busy; install Steam app $steamAppId there") + return + } + app.startActivity(Intent(app, com.droiddeck.launcher.SessionActivity::class.java) + .putExtra(com.droiddeck.launcher.session.SessionService.EXTRA_STEAM_URL, com.droiddeck.launcher.frontend.GameLaunchLink.steamUrl(steamAppId)) + .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)) + } +} + +internal class UbisoftLogin(override val userAgent: String) : StoreLoginActivity.Flow { + override val startUrl: String get() = UbisoftAuth.loginUrl + override val readsPage: Boolean get() = true + override val pageScript: String get() = UbisoftAuth.PAGE_SCRIPT + override fun pageReady(result: String): Boolean = result.contains("ticket", ignoreCase = true) + override fun isRedirect(url: String): Boolean = UbisoftAuth.isLoggedInPage(url) + + override fun forgetSignIn(done: () -> Unit) { + runCatching { WebCookies.expire(UbisoftAuth.SIGN_IN_URLS, ".ubisoft.com", listOf("/")) { done() } }.onFailure { done() } + } + + override fun finish(activity: StoreLoginActivity, view: WebView, url: String): String? = try { + UbisoftAuth.completeWebSignIn(activity, url)?.let { + Log.w("UbisoftLogin", "sign-in incomplete: $it") + activity.getString(R.string.stores_login_error_verification) + } + } catch (e: Exception) { + Log.w("UbisoftLogin", "sign-in failed: ${e.javaClass.simpleName}") + activity.getString(R.string.stores_login_error_generic) + } +} + +object UbisoftLibrary { + private const val GRAPHQL = "https://public-ubiservices.ubi.com/v1/profiles/me/uplay/graphql" + private const val PAGE = 50 + private const val QUERY = "query OwnedGames(\$limit: Int, \$offset: Int) { viewer { games(limit: \$limit, offset: \$offset) { totalCount nodes { id spaceId name coverUrl backgroundUrl bannerUrl releaseDate } } } }" + private val generation = java.util.concurrent.atomic.AtomicInteger() + private val running = java.util.concurrent.atomic.AtomicReference(null) + + /** Forgets the signed-out account: its sync results are dropped and a new account may sync at once. */ + fun reset() { + generation.incrementAndGet() + StoresState.post { StoresState.shelves.remove(Store.UBISOFT) } + } + + private fun post(gen: Int, block: () -> Unit) = StoresState.post { if (generation.get() == gen) block() } + + internal fun parsePage(body: String): Pair, Int> { + val o = JSONObject(body) + o.optJSONArray("errors")?.optJSONObject(0)?.let { throw IllegalStateException(it.optString("message")) } + val games = o.optJSONObject("data")?.optJSONObject("viewer")?.optJSONObject("games") ?: return emptyList() to 0 + val nodes = games.optJSONArray("nodes") + val items = (0 until (nodes?.length() ?: 0)).mapNotNull { nodes!!.optJSONObject(it) }.mapNotNull { n -> + val space = n.optString("spaceId").takeIf { it.isNotEmpty() } ?: return@mapNotNull null + fun s(k: String) = n.optString(k).takeIf { it.isNotEmpty() && it != "null" } + CatalogItem( + Store.UBISOFT, space, n.optString("name").trim(), s("backgroundUrl") ?: s("bannerUrl"), + tallImageUrl = s("coverUrl"), releaseDate = s("releaseDate")?.take(10).orEmpty(), owned = true, + extra = mapOf("spaceId" to space, "gameId" to n.optString("id")), + ) + } + return items to games.optInt("totalCount", items.size) + } + + /** Base games with a manifest, by UbiServices space; add-ons and bundles share spaces with their game. */ + internal fun ownedBySpace(owned: JSONObject?): Map { + val list = owned?.optJSONArray("games") ?: return emptyMap() + val games = (0 until list.length()).mapNotNull { list.optJSONObject(it) } + .filter { it.optString("manifest").isNotEmpty() && it.optInt("productType") == 0 && it.optBoolean("owned", true) } + val out = LinkedHashMap() + for (key in listOf("spaceId")) for (g in games) { + val space = g.optString(key).takeIf { it.isNotEmpty() } ?: continue + out.putIfAbsent(space, g) + } + return out + } + + private val STEAM_ID = Regex("""^\s*steam_app_id:\s*['"]?(\d+)""", RegexOption.MULTILINE) + private val THIRD_PARTY = Regex("""^\s*third_party_platform:\s*\r?\n\s+name:\s*['"]?Steam\b""", setOf(RegexOption.MULTILINE, RegexOption.IGNORE_CASE)) + + /** The Steam app of a product Ubisoft says is owned and installed through Steam. */ + internal fun steamApp(configuration: String): String? = + if (THIRD_PARTY.containsMatchIn(configuration)) STEAM_ID.find(configuration)?.groupValues?.get(1) else null + + /** Owned games Ubisoft delivers through Steam, by UbiServices space. */ + internal fun steamBySpace(owned: JSONObject?): Map { + val list = owned?.optJSONArray("games") ?: return emptyMap() + return (0 until list.length()).mapNotNull { list.optJSONObject(it) } + .filter { it.optInt("productType") == 0 && it.optBoolean("owned", true) && it.optString("spaceId").isNotEmpty() } + .mapNotNull { o -> steamApp(o.optString("configuration"))?.let { o.optString("spaceId") to it } }.toMap() + } + + private val RELATIVE = Regex("""relative:\s*['"]?([^'"\r\n]+?)['"]?\s*$""", RegexOption.MULTILINE) + + private val NAME = Regex("""^\s{1,4}name:\s*['"]?(.+?)['"]?\s*$""", RegexOption.MULTILINE) + + /** The product's name from its configuration, following a localisation key such as `l1` to its default text. */ + internal fun title(configuration: String): String? { + val raw = NAME.find(configuration)?.groupValues?.get(1)?.trim()?.takeIf { it.isNotEmpty() } ?: return null + if (!Regex("""l\d+""").matches(raw)) return raw + val local = configuration.indexOf("localizations:").takeIf { it >= 0 } ?: return raw + return Regex("""^\s+${raw}:\s*['"]?(.+?)['"]?\s*$""", RegexOption.MULTILINE).find(configuration, local)?.groupValues?.get(1)?.trim() ?: raw + } + + /** The first launch path under `start_game` in a product's configuration. */ + internal fun executable(configuration: String): String? { + val start = configuration.indexOf("start_game").takeIf { it >= 0 } ?: return null + return RELATIVE.find(configuration, start)?.groupValues?.get(1)?.trim()?.takeIf { it.endsWith(".exe", true) } + } + + fun sync(app: Context, force: Boolean) { + val gen = generation.get() + if (running.getAndSet(gen) == gen) return + try { + val s = UbisoftAuth.current(app) + if (s == null) { + post(gen) { StoresState.status.remove(Store.UBISOFT); StoresState.problems[Store.UBISOFT] = app.getString(R.string.stores_sync_sign_in) } + return + } + if (StoresState.library[Store.UBISOFT].isNullOrEmpty()) post(gen) { StoresState.status[Store.UBISOFT] = app.getString(R.string.stores_sync_game_list) } + val all = ArrayList() + var offset = 0 + while (true) { + val body = JSONObject().put("operationName", "OwnedGames").put("query", QUERY) + .put("variables", JSONObject().put("limit", PAGE).put("offset", offset)).toString() + val reply = UbisoftAuth.request(GRAPHQL, "POST", "Ubi_v1 t=${s.ticket}", s.sessionId, body) + if (reply.status == 401) StoresState.markSignInExpired(Store.UBISOFT) + if (!reply.ok) throw IllegalStateException("library ${reply.status}") + val (items, total) = parsePage(reply.body) + all += items + offset += PAGE + if (items.isEmpty() || offset >= total) break + } + val owned = UbisoftNative.call("owned", JSONObject().put("ticket", s.ticket).put("sessionId", s.sessionId)) + owned?.optString("error")?.takeIf { it.isNotEmpty() }?.let { StoresState.logLine("Ubisoft: ownership: $it") } + val bySpace = ownedBySpace(owned) + owned?.takeIf { it.optInt("manifestsAsked") > 0 }?.let { + StoresState.logLine("Ubisoft: latest manifests: ${it.optInt("manifestsFound")} of ${it.optInt("manifestsAsked")} ${it.optString("manifestError")}".trim()) + } + fun download(o: JSONObject) = listOfNotNull( + "productId" to o.optLong("productId").toString(), + o.optString("manifest").takeIf { it.isNotEmpty() }?.let { "manifest" to it }, + o.optString("appId").takeIf { it.isNotEmpty() }?.let { "appId" to it }, + executable(o.optString("configuration"))?.let { "exe" to it }, + ) + val listed = all.distinctBy { it.id } + val onSteam = steamBySpace(owned) + val matched = listed.map { g -> bySpace[g.id]?.let { g.copy(extra = g.extra + download(it)) } ?: onSteam[g.id]?.let { g.copy(extra = g.extra + (UbisoftBackend.STEAM_APP to it)) } ?: g } + .let { games -> games.filter { g -> g.extra["productId"] == null || games.first { it.extra["productId"] == g.extra["productId"] } === g } } + .let { games -> games.filter { g -> g.extra["productId"] != null || games.none { it !== g && it.title == g.title && it.extra["productId"] != null } } } + val products = matched.mapNotNull { it.extra["productId"] }.toSet() + val unlisted = bySpace.filterKeys { space -> listed.none { it.id == space } }.filterValues { it.optLong("productId").toString() !in products }.mapNotNull { (space, o) -> + val title = title(o.optString("configuration")) ?: return@mapNotNull null + CatalogItem(Store.UBISOFT, space, title, null, owned = true, extra = mapOf("spaceId" to space) + download(o)) + } + val games = (matched + unlisted).sortedBy { it.title.lowercase() } + StoresState.logLine("Ubisoft: ${bySpace.size} downloadable products; " + + games.filter { it.extra.containsKey("productId") }.joinToString(", ") { "${it.title}=${it.extra["productId"]}" }) + post(gen) { + StoresState.status.remove(Store.UBISOFT) + StoresState.problems.remove(Store.UBISOFT) + StoresState.library[Store.UBISOFT] = games + StoresState.shelves[Store.UBISOFT] = com.droiddeck.launcher.stores.StoreShelves( + trending = games.sortedByDescending { it.extra.containsKey("productId") }, art = games.mapNotNull { it.imageUrl }) + } + StoresState.logLine("Ubisoft: ${games.size} owned games; on Steam: " + games.filter { it.extra.containsKey(UbisoftBackend.STEAM_APP) }.joinToString(", ") { "${it.title}=${it.extra[UbisoftBackend.STEAM_APP]}" }) + } catch (e: Exception) { + StoresState.logLine("Ubisoft: library sync failed: ${e.javaClass.simpleName} ${e.message.orEmpty().take(200)}") + post(gen) { StoresState.status.remove(Store.UBISOFT); StoresState.problems[Store.UBISOFT] = app.getString(R.string.stores_sync_failed) } + } finally { + running.compareAndSet(gen, null) + } + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/ubisoft/UbisoftConnect.kt b/app/src/main/java/com/droiddeck/launcher/stores/ubisoft/UbisoftConnect.kt new file mode 100644 index 0000000000..23cc92cd20 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/ubisoft/UbisoftConnect.kt @@ -0,0 +1,18 @@ +package com.droiddeck.launcher.stores.ubisoft + +import android.content.Context +import com.droiddeck.launcher.frontend.AddedGames +import com.droiddeck.launcher.runtime.LinuxRuntime +import java.io.File + +/** The one prefix every Ubisoft game runs in, so Ubisoft Connect is installed and signed in once. */ +object UbisoftConnect { + const val APP_ID = 2929927072L + + fun prefix(context: Context) = File(LinuxRuntime.rootDir(context), "root/.local/share/Steam/steamapps/compatdata/$APP_ID") + + fun installed(context: Context) = File(prefix(context), "pfx/drive_c/Program Files (x86)/Ubisoft/Ubisoft Game Launcher/upc.exe").isFile + + fun launchOptions(context: Context): String = + AddedGames.guestPath(context, prefix(context))?.let { "STEAM_COMPAT_DATA_PATH=$it %command%" }.orEmpty() +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/ubisoft/UbisoftInstaller.kt b/app/src/main/java/com/droiddeck/launcher/stores/ubisoft/UbisoftInstaller.kt new file mode 100644 index 0000000000..1bf4c4e360 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/ubisoft/UbisoftInstaller.kt @@ -0,0 +1,104 @@ +package com.droiddeck.launcher.stores.ubisoft + +import android.content.Context +import com.droiddeck.launcher.R +import com.droiddeck.launcher.stores.CatalogItem +import com.droiddeck.launcher.stores.Store +import com.droiddeck.launcher.stores.StoreExe +import com.droiddeck.launcher.stores.StoreGameSidecar +import com.droiddeck.launcher.stores.StoreInstalls +import com.droiddeck.launcher.stores.StoreLanguage +import com.droiddeck.launcher.stores.amazon.AmazonNative +import com.droiddeck.launcher.stores.download.DownloadQueue +import com.droiddeck.launcher.stores.download.DownloadStage +import org.json.JSONObject +import java.io.File +import java.util.concurrent.atomic.AtomicBoolean +import java.util.concurrent.atomic.AtomicLong + +internal class UbisoftInstaller( + private val app: Context, + private val item: CatalogItem, + private val folder: File, +) : DownloadQueue.DownloadJob { + private val cancelled = AtomicBoolean(false) + + override fun run(handle: DownloadQueue.JobHandle): String? { + handle.stage(DownloadStage.MANIFEST, app.getString(R.string.stores_sync_sign_in)) + val s = UbisoftAuth.current(app) ?: throw IllegalStateException("Not signed in to Ubisoft") + val product = item.extra["productId"]?.toLongOrNull() ?: throw IllegalStateException("Ubisoft lists no download for ${item.title} on this account") + val cover = item.tallImageUrl ?: item.imageUrl + StoreInstalls.begin(folder, Store.UBISOFT, item.id, item.title, cover, item.imageUrl) + if (cancelled.get()) return null + val locale = StoreLanguage.localeFor(null, app) + val request = JSONObject() + .put("ticket", s.ticket) + .put("sessionId", s.sessionId) + .put("productId", product) + .put("manifest", item.extra["manifest"].orEmpty()) + .put("folder", folder.absolutePath) + .put("language", "${locale.language}-${locale.country}") + val shown = AtomicLong(-1) + val downloading = AtomicBoolean(false) + val listener = object : AmazonNative.InstallListener { + override fun onStatus(text: String) { handle.stage(DownloadStage.MANIFEST, text) } + override fun onCheck(doneBytes: Long, totalBytes: Long, files: Int, filesTotal: Int) { + handle.stageProgress(doneBytes, totalBytes, files, filesTotal, bytes = true) + } + override fun onSizes(pendingBytes: Long, totalBytes: Long) { handle.diskSize(totalBytes) } + override fun onProgress(done: Long, total: Long, files: Int, filesTotal: Int) { + if (downloading.compareAndSet(false, true)) handle.stage(DownloadStage.DOWNLOAD) + while (true) { + val before = shown.get() + if (done < before) return + if (shown.compareAndSet(before, done)) break + } + handle.progress(done, total, "Downloading ($files/$filesTotal files)") + } + override fun onLog(line: String) { handle.log(line) } + override fun beforeReplace(): Boolean = true + override fun onComplete(success: Boolean, cancelled: Boolean, error: String, result: String) = Unit + } + val outcome = UbisoftNative.install(request, listener, cancelled) + if (outcome.cancelled || cancelled.get() && !outcome.success) return null + if (!outcome.success) throw IllegalStateException(outcome.error.ifEmpty { "The Ubisoft install failed" }) + + handle.stage(DownloadStage.INSTALL, app.getString(R.string.stores_registering)) + val configured = resolve(folder, item.extra["exe"].orEmpty()) + val exe = configured ?: StoreExe.pick(folder, null, item.title).ifEmpty { "game.exe" } + handle.log("ubisoft: exe=$exe") + val sidecar = StoreGameSidecar( + Store.UBISOFT, item.id, item.title, exe = exe, + installVersion = item.extra["manifest"].orEmpty(), installedAt = System.currentTimeMillis(), + cover = cover, hero = item.imageUrl, cloud = false, + extra = item.extra.filterKeys { it in KEPT } + listOfNotNull( + outcome.result?.optJSONArray("chunks")?.let { UbisoftLaunch.CHUNKS to it.toString() }, + outcome.result?.optJSONObject("setup")?.let { UbisoftLaunch.SETUP to it.toString() }, + ), + ) + StoreInstalls.complete(app, folder, sidecar, onStep = { done, steps -> handle.stageProgress(done.toLong(), steps.toLong()) }) + return folder.path + } + + override fun cancel(deleteFiles: Boolean) { + cancelled.set(true) + if (deleteFiles) StoreInstalls.discard(app, folder, emptyList()) + } + + internal companion object { + val KEPT = setOf("productId", "spaceId", "manifest", "appId") + + /** A configured exe the way Windows finds it: any letter case, else the same file name anywhere in the install. */ + fun resolve(folder: File, configured: String): String? { + val rel = configured.replace('\\', '/').trimStart('/').takeIf { it.isNotEmpty() } ?: return null + var at = folder + for (part in rel.split('/')) { + at = at.listFiles()?.firstOrNull { it.name.equals(part, ignoreCase = true) } ?: return byName(folder, rel.substringAfterLast('/')) + } + return if (at.isFile) at.relativeTo(folder).invariantSeparatorsPath else byName(folder, rel.substringAfterLast('/')) + } + + private fun byName(folder: File, name: String): String? = + folder.walkTopDown().maxDepth(5).firstOrNull { it.isFile && it.name.equals(name, ignoreCase = true) }?.relativeTo(folder)?.invariantSeparatorsPath + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/ubisoft/UbisoftLaunch.kt b/app/src/main/java/com/droiddeck/launcher/stores/ubisoft/UbisoftLaunch.kt new file mode 100644 index 0000000000..279d3b3e43 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/ubisoft/UbisoftLaunch.kt @@ -0,0 +1,86 @@ +package com.droiddeck.launcher.stores.ubisoft + +import android.content.Context +import com.droiddeck.launcher.stores.StoreInstallRoot +import com.droiddeck.launcher.stores.Store +import com.droiddeck.launcher.stores.StoreGameSidecar +import com.droiddeck.launcher.stores.StoresState +import org.json.JSONArray +import org.json.JSONObject + +/** What the launcher service hands a starting Ubisoft game: the account, its products and a fresh game ticket. */ +object UbisoftLaunch { + private const val ENTITLEMENTS = "https://public-ubiservices.ubi.com/v1/profiles/me/global/ubiconnect/entitlement/api/entitlements" + private const val ME = "https://public-ubiservices.ubi.com/v3/users/me" + + fun prepare(app: Context, id: String): JSONObject { + val folder = StoreInstallRoot.gameFolders(app).firstOrNull { f -> + StoreGameSidecar.read(f)?.let { it.store == Store.UBISOFT && it.id == id } == true + } ?: return failure("not-installed") + val sidecar = StoreGameSidecar.read(folder) ?: return failure("not-installed") + val product = sidecar.extra["productId"]?.toLongOrNull() ?: return failure("no-product") + val s = UbisoftAuth.current(app) ?: return failure("signed-out") + val native = UbisoftNative.call("launch", JSONObject().put("ticket", s.ticket).put("sessionId", s.sessionId).put("productId", product)) + ?: return failure("engine-missing") + native.optString("error").takeIf { it.isNotEmpty() }?.let { + StoresState.logLine("Ubisoft: launch ticket for $product: $it") + return failure("ticket: $it") + } + val auth = "Ubi_v1 t=${s.ticket}" + val me = runCatching { UbisoftAuth.request(ME, "GET", auth, s.sessionId) }.getOrNull()?.takeIf { it.ok } + ?.let { runCatching { JSONObject(it.body) }.getOrNull() } + val account = JSONObject() + .put("userId", s.userId) + .put("username", me?.optString("username")?.takeIf { it.isNotEmpty() } ?: s.name) + .put("nameOnPlatform", me?.optString("nameOnPlatform")?.takeIf { it.isNotEmpty() } ?: s.name) + .put("email", me?.optString("email").orEmpty()) + .put("country", me?.optString("country")?.takeIf { it.isNotEmpty() } ?: "US") + val appId = runCatching { UbisoftAuth.request(ENTITLEMENTS, "GET", auth, s.sessionId) }.getOrNull()?.takeIf { it.ok } + ?.let { applicationId(it.body, product) } ?: sidecar.extra["appId"].orEmpty() + StoresState.logLine("Ubisoft: launch ticket for $product ready (${native.optJSONArray("owned")?.length() ?: 0} products, appId ${if (appId.isEmpty()) "unknown" else "found"})") + val language = com.droiddeck.launcher.stores.StoreLanguage.localeFor(null, app).let { "${it.language}-${it.country}" } + val cached = sidecar.extra[CHUNKS]?.let { runCatching { JSONArray(it) }.getOrNull() } + var setup = sidecar.extra[SETUP]?.let { runCatching { JSONObject(it) }.getOrNull() } + val chunks = if (cached != null && setup != null) cached else + UbisoftNative.call("chunks", JSONObject().put("ticket", s.ticket).put("sessionId", s.sessionId).put("productId", product) + .put("manifest", sidecar.extra["manifest"] ?: sidecar.installVersion).put("folder", folder.path).put("language", language)) + ?.let { r -> + r.optString("error").takeIf { it.isNotEmpty() }?.let { StoresState.logLine("Ubisoft: manifest for $product: $it") } + setup = r.optJSONObject("setup") + r.optJSONArray("chunks") + }?.also { c -> + runCatching { + StoreGameSidecar.read(folder)?.let { sc -> + sc.copy(extra = sc.extra + (CHUNKS to c.toString()) + listOfNotNull(setup?.let { SETUP to it.toString() })).write(folder) + } + com.droiddeck.launcher.stores.StoreLaunch.installedGame(app, Store.UBISOFT, id) + ?.let { com.droiddeck.launcher.frontend.AutoComponents.refreshLater(app, it) } + } + } ?: cached + setup?.let { StoresState.logLine("Ubisoft: setup for $product: ${it.toString().take(1500)}") } + UbisoftActivation.watch(app, product) + return JSONObject().put("ok", true) + .put("productId", product) + .put("ticket", native.optString("ticket")) + .put("owned", native.optJSONArray("owned") ?: JSONArray()) + .put("products", native.optJSONArray("products") ?: JSONArray()) + .put("account", account) + .put("appId", appId) + .put("language", language) + .put("chunks", chunks ?: JSONArray()) + .put("registry", setup?.optJSONArray("registry") ?: JSONArray()) + } + + const val CHUNKS = "chunks" + const val SETUP = "setup" + + /** The entitlement's `applicationId` for the product; the list sits under one of a few keys depending on the API version. */ + internal fun applicationId(body: String, product: Long): String? { + val root = runCatching { JSONObject(body) }.getOrNull() ?: return null + val list = sequenceOf("items", "entitlements", "products", "games", "results").firstNotNullOfOrNull { root.optJSONArray(it) } ?: return null + return (0 until list.length()).mapNotNull { list.optJSONObject(it) } + .firstOrNull { it.optLong("productId") == product }?.optString("applicationId")?.takeIf { it.isNotEmpty() } + } + + private fun failure(reason: String) = JSONObject().put("ok", false).put("reason", reason) +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/ubisoft/UbisoftNative.kt b/app/src/main/java/com/droiddeck/launcher/stores/ubisoft/UbisoftNative.kt new file mode 100644 index 0000000000..7a7d2eb881 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/ubisoft/UbisoftNative.kt @@ -0,0 +1,57 @@ +package com.droiddeck.launcher.stores.ubisoft + +import android.util.Log +import com.droiddeck.launcher.stores.StoresNative +import com.droiddeck.launcher.stores.amazon.AmazonNative +import org.json.JSONObject +import java.util.concurrent.CountDownLatch +import java.util.concurrent.TimeUnit +import java.util.concurrent.atomic.AtomicBoolean + +object UbisoftNative { + private const val TAG = "UbisoftNative" + const val ENGINE_MISSING = "Ubisoft: the store engine is not available" + + fun ready(): Boolean = StoresNative.ensureLoaded() + + fun call(op: String, request: JSONObject): JSONObject? { + if (!ready()) return null + val text = try { nativeCall(op, request.toString()) } catch (t: Throwable) { + Log.w(TAG, "$op: ${t.javaClass.simpleName}") + null + } ?: return null + return runCatching { JSONObject(text) }.getOrNull() + } + + fun install(request: JSONObject, events: AmazonNative.InstallListener, cancelled: AtomicBoolean): AmazonNative.InstallOutcome { + if (!ready()) return AmazonNative.InstallOutcome(false, false, ENGINE_MISSING, null) + val done = CountDownLatch(1) + val holder = arrayOfNulls(1) + val bridge = object : AmazonNative.InstallListener by events { + override fun onComplete(success: Boolean, cancelled: Boolean, error: String, result: String) { + holder[0] = AmazonNative.InstallOutcome(success, cancelled, error, result.takeIf { it.isNotEmpty() }?.let { runCatching { JSONObject(it) }.getOrNull() }) + done.countDown() + } + } + val handle = try { nativeInstall(request.toString(), bridge) } catch (t: Throwable) { + return AmazonNative.InstallOutcome(false, false, "The Ubisoft install could not start: ${t.javaClass.simpleName}", null) + } + if (handle == 0L) return holder[0] ?: AmazonNative.InstallOutcome(false, false, "The Ubisoft install could not start", null) + try { + var forwarded = false + while (true) { + val finished = try { done.await(250, TimeUnit.MILLISECONDS) } catch (e: InterruptedException) { cancelled.set(true); false } + if (finished) break + if (!forwarded && cancelled.get()) { nativeCancel(handle); forwarded = true } + } + } finally { + nativeRelease(handle) + } + return holder[0] ?: AmazonNative.InstallOutcome(false, false, "The Ubisoft install ended without a result", null) + } + + @JvmStatic private external fun nativeCall(op: String, request: String): String? + @JvmStatic private external fun nativeInstall(request: String, listener: AmazonNative.InstallListener): Long + @JvmStatic private external fun nativeCancel(handle: Long) + @JvmStatic private external fun nativeRelease(handle: Long) +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/xbox/XboxCredentials.kt b/app/src/main/java/com/droiddeck/launcher/stores/xbox/XboxCredentials.kt index c8b126519f..59403dac0f 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/xbox/XboxCredentials.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/xbox/XboxCredentials.kt @@ -64,15 +64,32 @@ internal class XboxCredentials(private val app: Context, expectedGeneration: Str if (root <= 0 || pid <= 0) return null val status = java.io.File("/proc/$pid/status").readLines() val tracer = status.first { it.startsWith("TracerPid:") }.substringAfter(':').trim().toInt() - val tracerGroup = java.io.File("/proc/$tracer/status").readLines() - .first { it.startsWith("Tgid:") }.substringAfter(':').trim().toInt() - if (tracerGroup != root) return null + if (tracer != 0) { + // under proot, every guest process is traced by the proot the app started + val tracerGroup = java.io.File("/proc/$tracer/status").readLines() + .first { it.startsWith("Tgid:") }.substringAfter(':').trim().toInt() + if (tracerGroup != root) return null + } else if (!descendsFrom(pid, root)) { + // without proot, the process the app started is the backend itself (or its parent) + return null + } val executable = android.system.Os.stat(binary.path) if (!mapsBackend(java.io.File("/proc/$pid/maps").readText(), binary.path, executable.st_ino)) return null return java.io.File("/proc/$pid/stat").readText().substringAfterLast(") ").split(' ')[19] + ":${executable.st_dev}:${executable.st_ino}" } + private fun descendsFrom(pid: Int, root: Int): Boolean { + var current = pid + repeat(32) { + if (current == root) return true + if (current <= 1) return false + current = java.io.File("/proc/$current/status").readLines() + .firstOrNull { it.startsWith("PPid:") }?.substringAfter(':')?.trim()?.toIntOrNull() ?: return false + } + return false + } + fun signalBackend(signal: Int) { val process = backend ?: return if (!process.isAlive) return diff --git a/app/src/main/java/com/droiddeck/launcher/stores/xbox/Xodus.kt b/app/src/main/java/com/droiddeck/launcher/stores/xbox/Xodus.kt index f803d06416..f27794d1b5 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/xbox/Xodus.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/xbox/Xodus.kt @@ -88,7 +88,8 @@ internal object Xodus { val fastPathPreloaded = runCatching { File(root, "etc/ld.so.preload").useLines { lines -> lines.any { it.trim() == ProotFastPath.LIBRARY } } }.getOrDefault(false) - val fastPathKey = if (ProotFastPath.enabled(app) && fastPathPreloaded && + val native = com.droiddeck.launcher.runtime.NativeGuest.forSession(app) + val fastPathKey = if (!native && ProotFastPath.enabled(app) && fastPathPreloaded && File(root, ProotFastPath.LIBRARY.removePrefix("/")).isFile) ProotFastPath.key(root, binds) else null val guestCommand = mutableListOf( "/usr/bin/env", "HOME=/root", "XODUS_LOG=off", "RUST_LOG=off", @@ -98,12 +99,12 @@ internal object Xodus { guestCommand.addAll(guestCommand.lastIndex, ProotFastPath.guestEnv(root, binds, key)) } guestCommand.addAll(args) - val command = LinuxRuntime.commandWithBinds(app, root, binds, guestCommand) - val builder = ProcessBuilder(command).directory(LinuxRuntime.rootDir(app)) + val builder = if (native) com.droiddeck.launcher.runtime.NativeGuest.processBuilder(app, root, binds, guestCommand) + else ProcessBuilder(LinuxRuntime.commandWithBinds(app, root, binds, guestCommand)).directory(LinuxRuntime.rootDir(app)) if (launchDirectory != null) builder.environment()["TMPDIR"] = launchDirectory.path builder.environment()["DROIDDECK_XBOX_CREDENTIAL_PORT"] = credentials.port.toString() builder.environment()["DROIDDECK_XBOX_CREDENTIAL_KEY"] = credentials.key - builder.environment().apply { + if (!native) builder.environment().apply { remove("LD_PRELOAD"); remove("LD_AUDIT"); remove("LD_LIBRARY_PATH") put("PROOT_LOADER", LinuxRuntime.prootLoader(app).path) put("PROOT_TMP_DIR", LinuxRuntime.prootTempDir(app).path) diff --git a/app/src/main/java/com/droiddeck/launcher/ui/AddAppDialog.kt b/app/src/main/java/com/droiddeck/launcher/ui/AddAppDialog.kt index 660e0723c0..8e7aaf9966 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/AddAppDialog.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/AddAppDialog.kt @@ -151,7 +151,7 @@ internal fun AddAppDialog(runtimeReady: Boolean, onDismiss: () -> Unit, onAdd: ( ?.let { UserApps.Request(UserApps.Source.Script(it), custom, iconPath) to (custom ?: it.nameWithoutExtension) } AddSource.APPIMAGE -> imagePath?.takeIf { it.endsWith(".appimage", ignoreCase = true) }?.let { File(it) } ?.let { UserApps.Request(UserApps.Source.AppImage(it), custom, iconPath) to (custom ?: it.nameWithoutExtension) } - AddSource.GITHUB -> repoId?.let { UserApps.Request(UserApps.Source.GitHub(it), custom, iconPath) to (custom ?: it) } + AddSource.GITHUB -> repoId?.let { UserApps.Request(UserApps.Source.GitHub(it, UserApps.githubAsset(repo)), custom, iconPath) to (custom ?: it) } AddSource.FLATPAK -> bundlePath?.takeIf { it.endsWith(".flatpak", ignoreCase = true) }?.let { File(it) } ?.let { UserApps.Request(UserApps.Source.FlatpakBundle(it), custom, iconPath) to (custom ?: it.nameWithoutExtension) } ?: flatpakId?.let { UserApps.Request(UserApps.Source.Flatpak(it), custom, iconPath) to (custom ?: it) } diff --git a/app/src/main/java/com/droiddeck/launcher/ui/AddonBrowser.kt b/app/src/main/java/com/droiddeck/launcher/ui/AddonBrowser.kt new file mode 100644 index 0000000000..89e84fe697 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/ui/AddonBrowser.kt @@ -0,0 +1,226 @@ +package com.droiddeck.launcher.ui + +import android.content.Context +import android.content.Intent +import android.net.Uri +import android.widget.Toast +import androidx.compose.foundation.LocalIndication +import androidx.compose.foundation.clickable +import androidx.compose.foundation.hoverable +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.height +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.lazy.grid.GridCells +import androidx.compose.foundation.lazy.grid.GridItemSpan +import androidx.compose.foundation.lazy.grid.LazyVerticalGrid +import androidx.compose.foundation.lazy.grid.items +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.filled.Check +import androidx.compose.material3.Icon +import androidx.compose.material3.LinearProgressIndicator +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableIntStateOf +import androidx.compose.runtime.mutableStateListOf +import androidx.compose.runtime.mutableStateMapOf +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.produceState +import androidx.compose.runtime.remember +import androidx.compose.runtime.rememberCoroutineScope +import androidx.compose.runtime.setValue +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.clip +import androidx.compose.ui.platform.LocalConfiguration +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.semantics.Role +import androidx.compose.ui.text.style.TextOverflow +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp +import com.droiddeck.launcher.R +import com.droiddeck.launcher.runtime.WowAddonCatalog +import com.droiddeck.launcher.runtime.WowAddons +import com.droiddeck.launcher.session.SessionState +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.delay +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext +import java.io.File + +private class AddonResults { + val items = mutableStateListOf() + var page by mutableIntStateOf(0) + var loading by mutableStateOf(false) + var failed by mutableStateOf(null) + var more by mutableStateOf(false) +} + +@Composable +internal fun AddonBrowserDialog(targets: List, initial: WowAddons.Target, onDismiss: () -> Unit) { + val context = LocalContext.current.applicationContext + val scope = rememberCoroutineScope() + val shown = rememberShown(onDismiss) + val close = { shown.targetState = false } + var target by remember { mutableStateOf(initial) } + var text by remember { mutableStateOf("") } + var category by remember { mutableStateOf(null) } + var sort by remember { mutableStateOf(WowAddonCatalog.Sort.POPULAR) } + val results = remember(text, category, sort, target) { AddonResults() } + val installing = remember { mutableStateMapOf() } + val installed = remember { mutableStateMapOf() } + val categories by produceState(emptyList()) { + value = withContext(Dispatchers.IO) { runCatching { WowAddonCatalog.categories(context) }.getOrDefault(emptyList()) } + } + fun load(page: Int) { + if (results.loading) return + results.loading = true + results.failed = null + val into = target + val words = text + val cat = category?.id + val order = sort + scope.launch { + val got = withContext(Dispatchers.IO) { + runCatching { WowAddonCatalog.search(context, WowAddonCatalog.Query(words, cat, WowAddonCatalog.client(into), order), page) } + } + got.onSuccess { + if (page == 0) results.items.clear() + results.items.addAll(it.items.filter { addon -> results.items.none { seen -> seen.id == addon.id } }) + results.page = page + results.more = it.more + }.onFailure { results.failed = it.message ?: it.javaClass.simpleName } + results.loading = false + } + } + LaunchedEffect(results) { + if (text.isNotEmpty()) delay(350) + load(0) + } + fun install(addon: WowAddonCatalog.Addon) { + val key = installKey(addon, target) + if (key in installing) return + if (SessionState.running) { + Toast.makeText(context, R.string.main_addons_close_session, Toast.LENGTH_LONG).show() + return + } + val into = target + installing[key] = 0 + scope.launch { + val message = withContext(Dispatchers.IO) { installAddon(context, addon, into) { pct -> scope.launch { if (key in installing) installing[key] = pct } } } + installing.remove(key) + if (message.second) installed[key] = true + Toast.makeText(context, message.first, Toast.LENGTH_LONG).show() + } + } + val sourceName = stringResource(R.string.addons_source_wowi) + AppDialog(shown, close, "addonBrowser", wide = true, scrollable = false) { + DialogTitleRow(stringResource(R.string.addons_download), close) + ModeHeader(sourceName, text, { text = it }, stringResource(R.string.stores_search_catalog, sourceName)) { + BrowseChip(stringResource(R.string.addons_source_curse), false) { + runCatching { context.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(WowAddonCatalog.curseForgeUrl(text))).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)) } + } + } + ChipRow { + BrowseDropdown(sortLabel(sort)) { first, done -> + WowAddonCatalog.Sort.entries.forEachIndexed { index, option -> + MenuItem(sortLabel(option), sort == option, focusRequester = if (index == 0) first else null) { sort = option; done() } + } + } + if (categories.isNotEmpty()) BrowseDropdown(category?.name ?: stringResource(R.string.addons_category_all)) { first, done -> + MenuItem(stringResource(R.string.addons_category_all), category == null, focusRequester = first) { category = null; done() } + categories.forEach { option -> MenuItem(option.name, category?.id == option.id) { category = option; done() } } + } + if (targets.size > 1) BrowseDropdown(target.flavor) { first, done -> + targets.forEachIndexed { index, option -> + MenuItem(option.flavor, option.addOns == target.addOns, focusRequester = if (index == 0) first else null) { target = option; done() } + } + } + } + val height = (LocalConfiguration.current.screenHeightDp * 0.62f).dp + LazyVerticalGrid( + GridCells.Adaptive(libraryTileWidth() + 40.dp), Modifier.fillMaxWidth().height(height), + horizontalArrangement = Arrangement.spacedBy(10.dp), verticalArrangement = Arrangement.spacedBy(10.dp), + ) { + items(results.items, key = { it.id }) { addon -> + AddonCard(addon, installing[installKey(addon, target)], installed[installKey(addon, target)] == true) { install(addon) } + } + item(key = "tail", span = { GridItemSpan(maxLineSpan) }) { + Box(Modifier.fillMaxWidth().padding(vertical = 8.dp), contentAlignment = Alignment.Center) { + when { + results.loading -> LinearProgressIndicator(Modifier.fillMaxWidth()) + results.failed != null -> BrowseChip(stringResource(R.string.stores_dl_retry), false) { load(if (results.items.isEmpty()) 0 else results.page + 1) } + results.more -> BrowseChip(stringResource(R.string.stores_itch_load_more), false) { load(results.page + 1) } + results.items.isEmpty() -> Text(stringResource(R.string.addons_none_found), fontSize = 13.sp, color = MaterialTheme.colorScheme.onSurfaceVariant) + } + } + } + } + } +} + +@Composable +private fun AddonCard(addon: WowAddonCatalog.Addon, progress: Int?, done: Boolean, onInstall: () -> Unit) { + val pal = LocalPalette.current + val src = remember { androidx.compose.foundation.interaction.MutableInteractionSource() } + val hot = rememberHot(src) + val shape = androidx.compose.foundation.shape.RoundedCornerShape(10.dp) + Column( + Modifier.fillMaxWidth().clip(shape).glideBorder(hot, shape, pal.signal, pal.line) + .hoverable(src) + .clickable(interactionSource = src, indication = LocalIndication.current, role = Role.Button, onClick = onInstall) + .controllerConfirm(onClick = onInstall), + ) { + ArtCard(addon.name, addon.thumb, null, null, hot, capsule = false, chip = progress?.let { "$it%" }) { + if (done) Icon(Icons.Filled.Check, stringResource(R.string.stores_installed_chip), tint = pal.good, modifier = Modifier.height(11.dp)) + else Text(stringResource(if (progress != null) R.string.store_installing else R.string.store_install), fontSize = 10.sp, color = pal.signal, maxLines = 1) + } + Text( + listOf(addon.author, formatCount(addon.downloads)).filter { it.isNotEmpty() }.joinToString(" · "), + fontSize = 11.sp, color = MaterialTheme.colorScheme.onSurfaceVariant, maxLines = 1, overflow = TextOverflow.Ellipsis, + modifier = Modifier.padding(horizontal = 8.dp, vertical = 6.dp), + ) + } +} + +@Composable +private fun sortLabel(sort: WowAddonCatalog.Sort): String = stringResource( + when (sort) { + WowAddonCatalog.Sort.POPULAR -> R.string.stores_itch_sort_popular + WowAddonCatalog.Sort.UPDATED -> R.string.stores_itch_sort_new + WowAddonCatalog.Sort.NAME -> R.string.addons_sort_name + WowAddonCatalog.Sort.DOWNLOADS -> R.string.addons_sort_downloads + }, +) + +private fun formatCount(count: Long): String = when { + count <= 0 -> "" + count >= 1_000_000 -> String.format(java.util.Locale.getDefault(), "%.1fM", count / 1_000_000.0) + count >= 1_000 -> String.format(java.util.Locale.getDefault(), "%.1fK", count / 1_000.0) + else -> count.toString() +} + +private fun installAddon(context: Context, addon: WowAddonCatalog.Addon, target: WowAddons.Target, progress: (Int) -> Unit): Pair { + val zip = File(File(context.cacheDir, "wowaddons"), "download-wowi-${addon.id.filter(Char::isLetterOrDigit)}-${java.util.UUID.randomUUID()}.zip") + return try { + WowAddonCatalog.download(context, addon, zip, progress) + if (SessionState.running) return context.getString(R.string.main_addons_close_session) to false + val names = WowAddons.import(zip, target) + if (names.isEmpty()) context.getString(R.string.main_addons_none, addon.name) to false + else context.resources.getQuantityString(R.plurals.main_addons_imported, names.size, names.size, target.flavor) to true + } catch (_: WowAddons.TooLarge) { + context.getString(R.string.main_addons_too_large, addon.name) to false + } catch (e: Exception) { + context.getString(R.string.stores_dl_failed_reason, e.message ?: e.javaClass.simpleName) to false + } finally { + zip.delete() + } +} + +private fun installKey(addon: WowAddonCatalog.Addon, target: WowAddons.Target) = addon.id + "|" + target.addOns.path diff --git a/app/src/main/java/com/droiddeck/launcher/ui/BottomNotificationsPage.kt b/app/src/main/java/com/droiddeck/launcher/ui/BottomNotificationsPage.kt deleted file mode 100644 index 266cb5a592..0000000000 --- a/app/src/main/java/com/droiddeck/launcher/ui/BottomNotificationsPage.kt +++ /dev/null @@ -1,116 +0,0 @@ -package com.droiddeck.launcher.ui - -import android.content.Intent -import android.provider.Settings -import androidx.compose.foundation.background -import androidx.compose.foundation.clickable -import androidx.compose.foundation.layout.Arrangement -import androidx.compose.foundation.layout.Box -import androidx.compose.foundation.layout.Column -import androidx.compose.foundation.layout.Row -import androidx.compose.foundation.layout.fillMaxSize -import androidx.compose.foundation.layout.fillMaxWidth -import androidx.compose.foundation.layout.height -import androidx.compose.foundation.layout.padding -import androidx.compose.foundation.lazy.LazyColumn -import androidx.compose.foundation.lazy.items -import androidx.compose.foundation.shape.RoundedCornerShape -import androidx.compose.material3.MaterialTheme -import androidx.compose.material3.Text -import androidx.compose.runtime.Composable -import androidx.compose.runtime.LaunchedEffect -import androidx.compose.runtime.getValue -import androidx.compose.runtime.mutableStateOf -import androidx.compose.runtime.remember -import androidx.compose.runtime.setValue -import androidx.compose.ui.Alignment -import androidx.compose.ui.Modifier -import androidx.compose.ui.draw.clip -import androidx.compose.ui.platform.LocalContext -import androidx.compose.ui.platform.testTag -import androidx.compose.ui.res.stringResource -import androidx.compose.ui.semantics.Role -import androidx.compose.ui.text.font.FontWeight -import androidx.compose.ui.text.style.TextAlign -import androidx.compose.ui.text.style.TextOverflow -import androidx.compose.ui.unit.dp -import androidx.compose.ui.unit.sp -import com.droiddeck.launcher.R -import com.droiddeck.launcher.session.BottomNotifications -import kotlinx.coroutines.delay - -internal fun notificationAccessIntent(context: android.content.Context): Intent = - if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.R) { - Intent(Settings.ACTION_NOTIFICATION_LISTENER_DETAIL_SETTINGS).putExtra( - Settings.EXTRA_NOTIFICATION_LISTENER_COMPONENT_NAME, - android.content.ComponentName(context, com.droiddeck.launcher.session.BottomNotificationListener::class.java).flattenToString(), - ) - } else Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS) - -@Composable -fun BottomNotificationsPage(openSettings: ((Intent, () -> Boolean) -> Unit)?) { - val context = LocalContext.current - val colors = MaterialTheme.colorScheme - val pal = LocalPalette.current - var granted by remember { mutableStateOf(BottomNotifications.granted(context)) } - LaunchedEffect(Unit) { - while (true) { - granted = BottomNotifications.granted(context) - delay(2_000L) - } - } - if (!granted) { - Column(Modifier.fillMaxSize().padding(24.dp), horizontalAlignment = Alignment.CenterHorizontally, - verticalArrangement = Arrangement.spacedBy(16.dp, Alignment.CenterVertically)) { - Text(stringResource(R.string.bottom_notifications_why), fontSize = 15.sp, color = colors.onBackground, textAlign = TextAlign.Center) - BottomPillButton(stringResource(R.string.bottom_notifications_allow), "bottom-notifications-allow") { - val intent = notificationAccessIntent(context) - if (openSettings != null) openSettings(intent) { BottomNotifications.granted(context) } - else runCatching { context.startActivity(intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)) } - } - } - return - } - val shown = BottomNotifications.items - if (shown.isEmpty()) { - Box(Modifier.fillMaxSize(), contentAlignment = Alignment.Center) { - Text(stringResource(R.string.bottom_notifications_none), fontSize = 15.sp, color = colors.onSurfaceVariant) - } - return - } - val timeFormat = remember { android.text.format.DateFormat.getTimeFormat(context) } - Column(Modifier.fillMaxSize(), verticalArrangement = Arrangement.spacedBy(8.dp)) { - Row(Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) { - Text(stringResource(R.string.bottom_notifications_count, shown.size), fontSize = 13.sp, color = colors.onSurfaceVariant, - modifier = Modifier.weight(1f)) - BottomPillButton(stringResource(R.string.bottom_notifications_clear), "bottom-notifications-clear") { BottomNotifications.clear() } - } - LazyColumn(Modifier.weight(1f).fillMaxWidth(), verticalArrangement = Arrangement.spacedBy(8.dp)) { - items(shown.toList(), key = { it.key }) { n -> - Column(Modifier.fillMaxWidth().clip(RoundedCornerShape(14.dp)).background(pal.surface).padding(horizontal = 14.dp, vertical = 10.dp), - verticalArrangement = Arrangement.spacedBy(4.dp)) { - Row(verticalAlignment = Alignment.CenterVertically) { - Text(n.app, fontSize = 12.sp, fontWeight = FontWeight.SemiBold, color = pal.signal, maxLines = 1, - overflow = TextOverflow.Ellipsis, modifier = Modifier.weight(1f)) - Text(timeFormat.format(java.util.Date(n.postedAt)), fontSize = 12.sp, color = colors.onSurfaceVariant) - } - if (n.title.isNotBlank()) Text(n.title, fontSize = 15.sp, fontWeight = FontWeight.SemiBold, color = colors.onBackground, - maxLines = 1, overflow = TextOverflow.Ellipsis) - if (n.text.isNotBlank()) Text(n.text, fontSize = 14.sp, color = colors.onBackground, maxLines = 4, overflow = TextOverflow.Ellipsis) - } - } - } - } -} - -@Composable -internal fun BottomPillButton(label: String, tag: String, onClick: () -> Unit) { - val pal = LocalPalette.current - Box( - Modifier.height(40.dp).clip(RoundedCornerShape(20.dp)).background(pal.signal.copy(alpha = 0.16f)) - .clickable(role = Role.Button, onClick = onClick).testTag(tag).padding(horizontal = 16.dp), - contentAlignment = Alignment.Center, - ) { - Text(label, fontSize = 14.sp, fontWeight = FontWeight.SemiBold, color = pal.signal, maxLines = 1) - } -} diff --git a/app/src/main/java/com/droiddeck/launcher/ui/BottomPages.kt b/app/src/main/java/com/droiddeck/launcher/ui/BottomPages.kt index a8884fdec7..2d2e401fa7 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/BottomPages.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/BottomPages.kt @@ -1,17 +1,31 @@ package com.droiddeck.launcher.ui +import androidx.compose.foundation.background +import androidx.compose.foundation.clickable +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.height +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material.icons.Icons import androidx.compose.material.icons.outlined.Download import androidx.compose.material.icons.outlined.Forum import androidx.compose.material.icons.outlined.Speed -import androidx.compose.material.icons.outlined.Notifications import androidx.compose.material.icons.outlined.PhotoLibrary +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.clip +import androidx.compose.ui.platform.testTag +import androidx.compose.ui.semantics.Role +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp import com.droiddeck.launcher.R class BottomPageActions( val takeScreenshot: ((onDone: (Boolean) -> Unit) -> Unit)? = null, val fps: () -> Float? = { null }, - val openSettings: ((intent: android.content.Intent, done: () -> Boolean) -> Unit)? = null, val runningGame: () -> String? = { null }, ) @@ -20,7 +34,18 @@ internal const val BOTTOM_PAGE_TELEMETRY = "telemetry" fun sessionBottomPages(actions: BottomPageActions): List = listOf( BottomPage(BOTTOM_PAGE_TELEMETRY, R.string.bottom_page_telemetry, Icons.Outlined.Speed) { BottomTelemetryPage(actions.fps, actions.runningGame) }, BottomPage("downloads", R.string.bottom_page_downloads, Icons.Outlined.Download) { BottomDownloadsPage() }, - BottomPage("notifications", R.string.bottom_page_notifications, Icons.Outlined.Notifications) { BottomNotificationsPage(actions.openSettings) }, BottomPage("screenshots", R.string.bottom_page_screenshots, Icons.Outlined.PhotoLibrary) { BottomScreenshotsPage(actions.takeScreenshot) }, BottomPage("chat", R.string.bottom_page_chat, Icons.Outlined.Forum) { BottomChatPage() }, ) + +@Composable +internal fun BottomPillButton(label: String, tag: String, onClick: () -> Unit) { + val pal = LocalPalette.current + Box( + Modifier.height(40.dp).clip(RoundedCornerShape(20.dp)).background(pal.signal.copy(alpha = 0.16f)) + .clickable(role = Role.Button, onClick = onClick).testTag(tag).padding(horizontal = 16.dp), + contentAlignment = Alignment.Center, + ) { + Text(label, fontSize = 14.sp, fontWeight = FontWeight.SemiBold, color = pal.signal, maxLines = 1) + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/ui/BottomScreenBar.kt b/app/src/main/java/com/droiddeck/launcher/ui/BottomScreenBar.kt index 4930b6db81..e4715b6a15 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/BottomScreenBar.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/BottomScreenBar.kt @@ -12,6 +12,7 @@ import androidx.compose.foundation.interaction.collectIsHoveredAsState import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.BoxWithConstraints +import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxSize @@ -47,6 +48,7 @@ import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import com.droiddeck.launcher.R +import com.droiddeck.launcher.input.DeckPanel import com.droiddeck.launcher.session.SessionService import com.droiddeck.launcher.session.SessionState @@ -59,6 +61,8 @@ internal class BarExtra( val onClick: () -> Unit, ) +internal class BarTabs(val labels: List, val selected: Int, val onSelect: (Int) -> Unit) + internal class BottomBarModel( val title: String?, val onClose: (() -> Unit)?, @@ -67,10 +71,22 @@ internal class BottomBarModel( val onQam: (() -> Unit)?, val extras: List = emptyList(), val menuActive: Boolean = false, + val tabs: BarTabs? = null, ) @Composable internal fun BottomScreenBar(model: BottomBarModel, modifier: Modifier = Modifier) { + val tabs = model.tabs + if (tabs == null) BarRow(model, modifier) else Column(modifier.fillMaxWidth().blockTouches()) { + BarRow(model) + Box(Modifier.fillMaxWidth().padding(start = BAR_PAD, end = BAR_PAD, bottom = 8.dp), contentAlignment = Alignment.Center) { + TabStrip(tabs.labels, tabs.selected, tabs.onSelect, Modifier.testTag("bottom-bar-tabs"), bumpers = false) + } + } +} + +@Composable +private fun BarRow(model: BottomBarModel, modifier: Modifier = Modifier) { val colors = MaterialTheme.colorScheme BoxWithConstraints(modifier.fillMaxWidth().height(BAR_HEIGHT).blockTouches()) { val buttons = listOfNotNull(model.onClose, model.onMenu, model.onQam).size + model.extras.size @@ -195,11 +211,15 @@ internal fun sessionBarModel( onSteam: () -> Unit, onQam: () -> Unit, extras: List = emptyList(), + tabs: BarTabs? = null, ): BottomBarModel { val steam = SessionState.mode == SessionService.MODE_STEAM - return BottomBarModel(title, onClose, onMenu, onSteam.takeIf { steam }, onQam.takeIf { steam }, extras) + return BottomBarModel(title, onClose, onMenu, onSteam.takeIf { steam }, onQam.takeIf { steam }, extras, tabs = tabs) } +internal fun deckPanelTabs(context: Context, selected: DeckPanel, onSelect: (DeckPanel) -> Unit) = + BarTabs(DeckPanel.entries.map { context.getString(it.label) }, selected.ordinal) { onSelect(DeckPanel.entries[it]) } + internal fun bottomScreenBarView(context: Context, model: () -> BottomBarModel): ComposeView = ComposeView(context).apply { setContent { DroidDeckTheme { BottomScreenBar(model()) } } } diff --git a/app/src/main/java/com/droiddeck/launcher/ui/BottomTelemetryPage.kt b/app/src/main/java/com/droiddeck/launcher/ui/BottomTelemetryPage.kt index 4aee7fc22f..08b4ae8ada 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/BottomTelemetryPage.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/BottomTelemetryPage.kt @@ -304,7 +304,8 @@ fun BottomTelemetryPage(fps: () -> Float?, runningGame: () -> String? = { null } val now = System.nanoTime() val live = frames.live val stats = frames.stats - val graph = FloatArray(FrameStats.GRAPH_FRAMES).also { if (live) stats.graph(it) } + val graph = FloatArray(FrameStats.GRAPH_FRAMES) + val graphEnd = if (live) stats.graph(graph) else 0L val hz = (frames.refreshMilliHz + 499) / 1000 val data = HudData( fps = fpsNow, avgFps = if (live) stats.averageFps() else null, lows = if (live) stats.lows(now) else FrameStats.Lows.EMPTY, @@ -317,6 +318,7 @@ fun BottomTelemetryPage(fps: () -> Float?, runningGame: () -> String? = { null } title = title, cover = cover, played = playedSeconds?.let(::playedText), clock = clock, fpsHistory = fpsHistory.snapshot(), cpuHistory = cpuHistory.snapshot(), gpuHistory = gpuHistory.snapshot(), historySeconds = maxOf(fpsHistory.spanSeconds(), cpuHistory.spanSeconds(), gpuHistory.spanSeconds()), + graphEnd = graphEnd, coreGroups = coreGroups, gameCores = gameCores, fahrenheit = tempDisplay.fahrenheit, tempColor = { sensor, c -> thresholds[sensor]?.let { Color(HudMetrics.tempColor(c.toFloat(), it, tempDisplay, TEMP_PLAIN)) } ?: Color(TEMP_PLAIN) diff --git a/app/src/main/java/com/droiddeck/launcher/ui/BuildWatermark.kt b/app/src/main/java/com/droiddeck/launcher/ui/BuildWatermark.kt index 648d754aac..a9d0c3d1a2 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/BuildWatermark.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/BuildWatermark.kt @@ -38,7 +38,8 @@ object BuildWatermark { if (text.length <= TITLE_LIMIT) text else text.take(TITLE_LIMIT - 1).trimEnd() + "…" fun install(app: Application) { - val lines = lines() ?: return + val lab = com.droiddeck.launcher.lab.LabOverlay.active?.let { "LAB | ${truncate(it.label)}" } + val lines = (listOfNotNull(lab) + lines().orEmpty()).ifEmpty { return } app.registerActivityLifecycleCallbacks(object : Application.ActivityLifecycleCallbacks { override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) { if (activity is QuestXrActivity) return diff --git a/app/src/main/java/com/droiddeck/launcher/ui/ComponentsPage.kt b/app/src/main/java/com/droiddeck/launcher/ui/ComponentsPage.kt index 2b93f2ad9c..b00edeb98a 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/ComponentsPage.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/ComponentsPage.kt @@ -1112,7 +1112,7 @@ private fun AutoComponentBody(label: String, lines: List Unit, onPick: (String?) -> Unit, allowInheritance: Boolean = false, + generalPreset: String = preset, modifier: Modifier = Modifier, ) { - val chosen = FexPreset.all.firstOrNull { it.id == preset } + val chosen = FexPreset.all.firstOrNull { it.id == if (inherited) generalPreset else preset } val label = when { preset == GameEnvironment.CUSTOM_FEX_PRESET -> stringResource(R.string.comp_game_fex_custom) chosen == null -> stringResource(FexPreset.byId("").label) - inherited -> stringResource(R.string.comp_game_use_general) else -> stringResource(chosen.label) } + val general = FexPreset.all.firstOrNull { it.id == generalPreset } Box { ValueChip(label, open, enabled = enabled, modifier = modifier.widthIn(max = 260.dp), onClick = onToggle) AnchoredMenu(open && enabled, onDismiss = onDismiss, title = stringResource(R.string.fex_preset_title)) { first -> @@ -1365,6 +1367,7 @@ internal fun FexPresetChip( MenuItem( stringResource(R.string.comp_game_use_general), checked = inherited, + detail = general?.let { stringResource(it.label) }, focusRequester = first, ) { onPick(null); onDismiss() } } diff --git a/app/src/main/java/com/droiddeck/launcher/ui/ControllerSettings.kt b/app/src/main/java/com/droiddeck/launcher/ui/ControllerSettings.kt index 6c5f7c230e..2afc537c97 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/ControllerSettings.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/ControllerSettings.kt @@ -1,111 +1,20 @@ package com.droiddeck.launcher.ui -import com.droiddeck.launcher.R -import androidx.compose.ui.res.stringResource -import androidx.compose.foundation.background -import androidx.compose.foundation.border -import androidx.compose.foundation.layout.Arrangement -import androidx.compose.foundation.layout.Box -import androidx.compose.foundation.layout.Row -import androidx.compose.foundation.layout.size -import androidx.compose.foundation.shape.CircleShape -import androidx.compose.foundation.layout.ColumnScope -import androidx.compose.runtime.Composable -import androidx.compose.ui.Modifier -import androidx.compose.ui.graphics.Color -import androidx.compose.ui.unit.dp +import androidx.compose.runtime.* +import androidx.compose.ui.platform.LocalContext import com.droiddeck.launcher.input.ControllerPrefs -import com.droiddeck.launcher.session.SessionPrefs -class ControllerActions( - val onOsc: (String) -> Unit, - val onTint: (Int) -> Unit, - val onOpacity: (Int) -> Unit, - val onSize: (Int) -> Unit, - val onStickClick: (Boolean) -> Unit, - val onAdaptiveSticks: (Boolean) -> Unit, - val onRumble: (Boolean) -> Unit, - val onDeadZone: (Int) -> Unit, - val onSteamButton: (Boolean) -> Unit, - val onQamButton: (Boolean) -> Unit, - val onKeyboardButton: (Boolean) -> Unit, - val onSelectSteam: (Boolean) -> Unit, - val onJoyConVr: (Boolean) -> Unit, - val onEditLayout: () -> Unit, - val onResetLayout: () -> Unit, - val onMapping: () -> Unit, - val onResetAll: () -> Unit, -) +class ControllerActions(val onMapping: () -> Unit) @Composable -private fun Swatch(color: Int) { - Box(Modifier.size(14.dp).background(Color(color), CircleShape).border(1.dp, Color.White.copy(alpha = 0.35f), CircleShape)) -} - -@Composable -fun ColumnScope.ControllerRows(host: MenuHost, oscMode: String, c: ControllerPrefs.Settings, a: ControllerActions) { - ChoiceRow( - host, "controller-osc", stringResource(R.string.mode_osc), stringResource(R.string.ctrl_osc_hint), - listOf(SessionPrefs.OSC_AUTO to stringResource(R.string.common_auto), SessionPrefs.OSC_ALWAYS to stringResource(R.string.common_always), SessionPrefs.OSC_STEAM_TOUCH to stringResource(R.string.osc_steam_touch), SessionPrefs.OSC_STEAM_QAM to stringResource(R.string.mode_osc_qam), SessionPrefs.OSC_NEVER to stringResource(R.string.common_never)), - oscMode, note = stringResource(R.string.mode_osc_note), onPick = a.onOsc, - ) - val tintOpen = host.open == "controller-tint" - SettingsRow(stringResource(R.string.ctrl_color), stringResource(R.string.ctrl_color_hint), highlighted = tintOpen) { - Box { - ValueChip(ControllerPrefs.tints.firstOrNull { it.first == c.tint }?.second?.let { stringResource(it) } ?: stringResource(R.string.ctrl_custom), tintOpen) { - host.open = if (tintOpen) null else "controller-tint" - } - AnchoredMenu(tintOpen, onDismiss = { if (host.open == "controller-tint") host.open = null }, title = stringResource(R.string.ctrl_color)) { firstItemFocus -> - ControllerPrefs.tints.forEachIndexed { index, (color, name) -> - MenuItem(stringResource(name), checked = c.tint == color, leading = { Swatch(color) }, focusRequester = if (index == 0) firstItemFocus else null) { - a.onTint(color) - host.open = null - } - } - } - } - } - ChoiceRow(host, "controller-opacity", stringResource(R.string.ctrl_opacity), null, ControllerPrefs.opacities.map { it to stringResource(R.string.ctrl_percent, it) }, c.opacity, onPick = a.onOpacity) - ChoiceRow(host, "controller-size", stringResource(R.string.ctrl_size), stringResource(R.string.ctrl_size_hint), ControllerPrefs.sizes.map { it to stringResource(R.string.ctrl_percent, it) }, c.size, onPick = a.onSize) - ToggleRow(host, "controller-stick-click", stringResource(R.string.ctrl_stick_click), stringResource(R.string.ctrl_stick_click_hint), c.stickClick, onChange = a.onStickClick) - ToggleRow(host, "controller-adaptive", stringResource(R.string.ctrl_adaptive), stringResource(R.string.ctrl_adaptive_hint), c.adaptiveSticks, onChange = a.onAdaptiveSticks) - ToggleRow(host, "controller-rumble", stringResource(R.string.ctrl_rumble), null, c.rumble, onChange = a.onRumble) - ChoiceRow(host, "controller-dead-zone", stringResource(R.string.ctrl_dead_zone), null, ControllerPrefs.deadZones.map { it to stringResource(R.string.ctrl_percent, it) }, c.deadZone, onPick = a.onDeadZone) - ToggleRow(host, "controller-steam", stringResource(R.string.ctrl_steam_button), null, c.steamButton, onChange = a.onSteamButton) - ToggleRow(host, "controller-qam", stringResource(R.string.ctrl_qam_button), null, c.qamButton, onChange = a.onQamButton) - ToggleRow(host, "controller-keyboard", stringResource(R.string.ctrl_keyboard_button), null, c.keyboardButton, onChange = a.onKeyboardButton) - ToggleRow(host, "controller-select-steam", stringResource(R.string.ctrl_select_steam), stringResource(R.string.ctrl_select_steam_hint), c.selectSteam, onChange = a.onSelectSteam) - ToggleRow(host, "controller-joycon-vr", stringResource(R.string.ctrl_joycon_vr), stringResource(R.string.ctrl_joycon_vr_hint), c.joyConVr, onChange = a.onJoyConVr) - SettingsRow(stringResource(R.string.ctrl_layout), if (c.customLayout) stringResource(R.string.ctrl_layout_custom) else stringResource(R.string.ctrl_layout_default)) { - Row(horizontalArrangement = Arrangement.spacedBy(6.dp)) { - SecondaryButton(stringResource(R.string.ctrl_edit)) { a.onEditLayout() } - SecondaryButton(stringResource(R.string.ctrl_reset), enabled = c.customLayout) { a.onResetLayout() } - } - } - val remapped = c.mapping.count { (id, target) -> id != target } - ActionRow(stringResource(R.string.ctrl_mapping), if (remapped == 0) stringResource(R.string.ctrl_mapping_default) else stringResource(R.string.ctrl_mapping_count, remapped, c.mapping.size), stringResource(R.string.ctrl_configure), a.onMapping) - ActionRow(stringResource(R.string.ctrl_reset_all), stringResource(R.string.ctrl_reset_all_hint), stringResource(R.string.ctrl_reset), a.onResetAll) -} - -@Composable -fun ControllerMappingPage(mapping: Map, onPick: (String, String) -> Unit, onReset: () -> Unit, onBack: () -> Unit) { - val host = rememberMenuHost() - SettingsPage( - host, title = stringResource(R.string.ctrl_mapping), eyebrow = stringResource(R.string.setup_controller), - lede = stringResource(R.string.ctrl_mapping_lede), - onBack = onBack, - ) { - SettingsGroup(stringResource(R.string.shortcuts_title)) { - PhysicalShortcutRows() - } - SettingsGroup(stringResource(R.string.ctrl_onscreen_buttons)) { - val targets = ControllerPrefs.targets.map { (id, label) -> id to stringResource(label) } - for ((id, name) in ControllerPrefs.mappable) { - ChoiceRow(host, "map-$id", stringResource(name), null, targets, mapping[id] ?: id) { onPick(id, it) } - } - } - SettingsGroup(stringResource(R.string.ctrl_defaults)) { - ActionRow(stringResource(R.string.ctrl_reset_mapping), stringResource(R.string.ctrl_mapping_default), stringResource(R.string.ctrl_reset), onReset) - } +internal fun rememberControllerSettings(): ControllerPrefs.Settings { + val context = LocalContext.current + var settings by remember { mutableStateOf(ControllerPrefs.read(context)) } + DisposableEffect(context) { + val prefs = ControllerPrefs.prefs(context) + val listener = android.content.SharedPreferences.OnSharedPreferenceChangeListener { _, _ -> settings = ControllerPrefs.read(context) } + prefs.registerOnSharedPreferenceChangeListener(listener) + onDispose { prefs.unregisterOnSharedPreferenceChangeListener(listener) } } + return settings } diff --git a/app/src/main/java/com/droiddeck/launcher/ui/DeckControlsPanel.kt b/app/src/main/java/com/droiddeck/launcher/ui/DeckControlsPanel.kt deleted file mode 100644 index 0193ce3201..0000000000 --- a/app/src/main/java/com/droiddeck/launcher/ui/DeckControlsPanel.kt +++ /dev/null @@ -1,720 +0,0 @@ -package com.droiddeck.launcher.ui - -import com.droiddeck.launcher.R -import android.annotation.SuppressLint -import android.content.Context -import android.graphics.Canvas -import android.graphics.Color -import android.graphics.Paint -import android.graphics.RadialGradient -import android.graphics.RectF -import android.graphics.Shader -import android.graphics.Typeface -import android.graphics.drawable.GradientDrawable -import android.view.Gravity -import android.view.HapticFeedbackConstants -import android.view.MotionEvent -import android.view.View -import android.view.ViewGroup -import android.widget.FrameLayout -import android.widget.HorizontalScrollView -import android.widget.LinearLayout -import android.widget.TextView -import com.droiddeck.launcher.input.DeckControls -import com.droiddeck.launcher.input.PadBridge -import com.droiddeck.launcher.input.PadState -import com.droiddeck.launcher.session.SessionService -import com.droiddeck.launcher.session.SessionPrefs -import com.droiddeck.launcher.session.SessionState -import kotlin.math.atan2 -import kotlin.math.hypot -import kotlin.math.min - -@SuppressLint("ViewConstructor") -class DeckControlsPanel( - context: Context, - private val onClose: () -> Unit, - private val onSteamMenu: () -> Unit, - private val onQam: () -> Unit, - private val pad: PadBridge? = null, - private val portraitDock: Boolean = false, - private val onMenu: (() -> Unit)? = null, - private val onSwap: (() -> Unit)? = null, -) : LinearLayout(context) { - private enum class Tab(val label: Int) { GAMEPAD(R.string.deck_tab_gamepad), GRIPS(R.string.deck_tab_grips), LEFT(R.string.deck_tab_left), RIGHT(R.string.deck_tab_right), BOTH(R.string.deck_tab_both) } - - private val content = FrameLayout(context) - val deckControls = pad == null || SessionState.deckPad - private val tabViews = HashMap() - private val topBar: View by lazy { tabsRow() } - - init { - orientation = VERTICAL - setBackgroundColor(Color.BLACK) - val title = context.getString(if (deckControls) R.string.second_mode_deck_controls else R.string.deck_tab_gamepad) - val extras = listOfNotNull(onSwap?.let { BarExtra(context.getString(R.string.swap_screens), icon = R.drawable.ic_bottom_swap, onClick = it) }) - addView(bottomScreenBarView(context) { sessionBarModel(title, onClose, onMenu, onSteamMenu, onQam, extras) }, - LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)) - addView(topBar, LayoutParams(LayoutParams.MATCH_PARENT, dp(52))) - addView(content, LayoutParams(LayoutParams.MATCH_PARENT, 0, 1f)) - select(if (pad != null && !deckControls) Tab.GAMEPAD else if (pad != null) selectedWithPad else selected) - } - - override fun onDetachedFromWindow() { - DeckControls.releaseAll() - super.onDetachedFromWindow() - } - - private fun tabsRow(): View = LinearLayout(context).apply { - orientation = HORIZONTAL - gravity = Gravity.CENTER - setPadding(dp(12), 0, dp(12), dp(8)) - val tabs = LinearLayout(context).apply { - orientation = HORIZONTAL - background = outline(CORNER_PILL, SURFACE) - setPadding(dp(4), dp(4), dp(4), dp(4)) - for (tab in Tab.values()) { - if (!deckControls && tab != Tab.GAMEPAD) continue - if (tab == Tab.GAMEPAD && pad == null) continue - val view = TextView(context).apply { - text = context.getString(tab.label) - textSize = 14f - typeface = Typeface.create(Typeface.DEFAULT, Typeface.BOLD) - gravity = Gravity.CENTER - isSingleLine = true - setPadding(dp(14), 0, dp(14), 0) - setOnClickListener { select(tab) } - } - tabViews[tab] = view - addView(view, LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT)) - } - } - val centre = HorizontalScrollView(context).apply { - isHorizontalScrollBarEnabled = false - overScrollMode = View.OVER_SCROLL_NEVER - addView(tabs, FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, dp(44))) - } - addView(centre, LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT)) - } - - private fun select(tab: Tab) { - if (pad != null) selectedWithPad = tab else selected = tab - DeckControls.releaseAll() - for ((each, view) in tabViews) { - val on = each == tab - view.background = if (on) filled(CORNER_PILL, ACCENT) else null - view.setTextColor(if (on) ON_ACCENT else TEXT_DIM) - } - tabViews[tab]?.let { selectedView -> - (selectedView.parent?.parent as? HorizontalScrollView)?.post { - val left = selectedView.left - val right = selectedView.right - val scroll = selectedView.parent?.parent as? HorizontalScrollView ?: return@post - when { - left < scroll.scrollX -> scroll.smoothScrollTo(left, 0) - right > scroll.scrollX + scroll.width -> scroll.smoothScrollTo(right - scroll.width, 0) - } - } - } - content.removeAllViews() - topBar.visibility = if (tab == Tab.GAMEPAD || tabViews.size < 2) View.GONE else View.VISIBLE - content.addView( - when (tab) { - Tab.GRIPS -> gripsGrid() - Tab.LEFT -> singlePad(right = false) - Tab.RIGHT -> singlePad(right = true) - Tab.BOTH -> bothPads() - Tab.GAMEPAD -> gamepad(pad ?: return) - }, - FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT), - ) - } - - private fun gripsGrid(): View = LinearLayout(context).apply { - orientation = HORIZONTAL - setPadding(dp(12), dp(6), dp(12), dp(12)) - fun column(upper: View, lower: View) = LinearLayout(context).apply { - orientation = VERTICAL - addView(upper, LayoutParams(LayoutParams.MATCH_PARENT, 0, 1f).apply { bottomMargin = dp(6) }) - addView(lower, LayoutParams(LayoutParams.MATCH_PARENT, 0, 1f).apply { topMargin = dp(6) }) - } - addView(column(GripButton(context, "L4", context.getString(R.string.deck_left_upper), DeckControls.L4), GripButton(context, "L5", context.getString(R.string.deck_left_lower), DeckControls.L5)), - LayoutParams(0, LayoutParams.MATCH_PARENT, 1f).apply { rightMargin = dp(6) }) - addView(column(GripButton(context, "R4", context.getString(R.string.deck_right_upper), DeckControls.R4), GripButton(context, "R5", context.getString(R.string.deck_right_lower), DeckControls.R5)), - LayoutParams(0, LayoutParams.MATCH_PARENT, 1f).apply { leftMargin = dp(6) }) - } - - private fun singlePad(right: Boolean): View = LinearLayout(context).apply { - orientation = VERTICAL - setPadding(dp(12), dp(6), dp(12), dp(12)) - addView(TrackpadView(context, right), LayoutParams(LayoutParams.MATCH_PARENT, 0, 1f).apply { bottomMargin = dp(12) }) - addView(ClickBar(context, right), LayoutParams(LayoutParams.MATCH_PARENT, dp(76))) - } - - private fun bothPads(): View = LinearLayout(context).apply { - orientation = VERTICAL - setPadding(dp(12), dp(6), dp(12), dp(12)) - val pads = LinearLayout(context).apply { - orientation = HORIZONTAL - addView(TrackpadView(context, right = false), LayoutParams(0, LayoutParams.MATCH_PARENT, 1f).apply { rightMargin = dp(6) }) - addView(TrackpadView(context, right = true), LayoutParams(0, LayoutParams.MATCH_PARENT, 1f).apply { leftMargin = dp(6) }) - } - addView(pads, LayoutParams(LayoutParams.MATCH_PARENT, 0, 1f).apply { bottomMargin = dp(12) }) - val grips = LinearLayout(context).apply { - orientation = HORIZONTAL - val row = listOf("L4" to DeckControls.L4, "L5" to DeckControls.L5, "R5" to DeckControls.R5, "R4" to DeckControls.R4) - row.forEachIndexed { index, (label, bit) -> - addView(GripButton(context, label, null, bit), LayoutParams(0, LayoutParams.MATCH_PARENT, 1f).apply { - if (index > 0) leftMargin = dp(6) - if (index < row.size - 1) rightMargin = dp(6) - }) - } - } - addView(grips, LayoutParams(LayoutParams.MATCH_PARENT, dp(84))) - } - - private fun gamepad(pad: PadBridge): View = GamepadLayout(context, portraitDock).apply { - fun pill(label: String, write: (PadState, Boolean) -> Unit) = PadButton(context, pad, label, write) - place(surfaceSlot(pad, right = false), GamepadGeometry.Slot.LEFT_MAIN) - place(DirectionPad(context, pad), GamepadGeometry.Slot.LEFT_SECOND) - place(surfaceSlot(pad, right = true), GamepadGeometry.Slot.RIGHT_MAIN) - place(FaceButtons(context, pad), GamepadGeometry.Slot.RIGHT_SECOND) - place(pill("LT") { st, d -> st.leftTrigger = if (d) 1f else 0f }, GamepadGeometry.Slot.LEFT_OUTER_SHOULDER) - place(pill("LB") { st, d -> st.press(PadState.LB, d) }, GamepadGeometry.Slot.LEFT_INNER_SHOULDER) - place(pill("RB") { st, d -> st.press(PadState.RB, d) }, GamepadGeometry.Slot.RIGHT_INNER_SHOULDER) - place(pill("RT") { st, d -> st.rightTrigger = if (d) 1f else 0f }, GamepadGeometry.Slot.RIGHT_OUTER_SHOULDER) - place(pill("VIEW") { st, d -> st.press(PadState.SELECT, d) }, GamepadGeometry.Slot.VIEW) - place(pill("MENU") { st, d -> st.press(PadState.START, d) }, GamepadGeometry.Slot.MENU) - if (deckControls) place(centreTap("\u25a6 " + context.getString(R.string.deck_panels), context.getString(R.string.deck_more_controls)) { select(Tab.BOTH) }, - GamepadGeometry.Slot.PANELS) - if (portraitDock && deckControls) { - place(GripButton(context, "L4", null, DeckControls.L4), GamepadGeometry.Slot.L4) - place(GripButton(context, "L5", null, DeckControls.L5), GamepadGeometry.Slot.L5) - place(GripButton(context, "R5", null, DeckControls.R5), GamepadGeometry.Slot.R5) - place(GripButton(context, "R4", null, DeckControls.R4), GamepadGeometry.Slot.R4) - } - } - - private fun centreTap(label: String, description: String, onClick: () -> Unit) = TextView(context).apply { - text = label - contentDescription = description - textSize = 13f - maxLines = 1 - setAutoSizeTextTypeUniformWithConfiguration(9, 13, 1, android.util.TypedValue.COMPLEX_UNIT_SP) - typeface = Typeface.create(Typeface.DEFAULT, Typeface.BOLD) - letterSpacing = 0.08f - gravity = Gravity.CENTER - setTextColor(TEXT) - background = outline(CORNER_PILL, SURFACE) - setOnClickListener { - performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY) - onClick() - } - } - - private fun surfaceSlot(pad: PadBridge, right: Boolean): View = FrameLayout(context).apply { - if (!deckControls) { - addView(StickView(context, pad, right), - FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT)) - return@apply - } - val slot = this - fun show() { - slot.removeAllViews() - val stick = SessionPrefs.gamepadSurface(context, right) == SessionPrefs.SURFACE_STICK - slot.addView(if (stick) StickView(context, pad, right) else TrackpadView(context, right, captioned = false), - FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT)) - val chip = TextView(context).apply { - text = "\u21c4" - contentDescription = context.getString(R.string.deck_surface_switch) - textSize = 14f - typeface = Typeface.create(Typeface.DEFAULT, Typeface.BOLD) - gravity = Gravity.CENTER - setTextColor(TEXT_DIM) - background = outline(CORNER_PILL, SURFACE) - setOnClickListener { - performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY) - SessionPrefs.setGamepadSurface(context, right, - if (stick) SessionPrefs.SURFACE_TRACKPAD else SessionPrefs.SURFACE_STICK) - show() - } - } - slot.addView(chip, FrameLayout.LayoutParams(dp(30), dp(30), - (if (right) Gravity.START else Gravity.END) or Gravity.TOP).apply { setMargins(dp(6), dp(6), dp(6), dp(6)) }) - } - show() - } - - fun takesTouchAt(x: Float, y: Float): Boolean = controlUnder(this, x, y) - - private fun controlUnder(view: View, x: Float, y: Float): Boolean { - if (view.visibility != VISIBLE || x < 0f || y < 0f || x >= view.width || y >= view.height) return false - if (view.isClickable || view is HorizontalScrollView || view is Surface || view is DirectionPad || view is FaceButtons || - view is androidx.compose.ui.platform.ComposeView) return true - val group = view as? ViewGroup ?: return false - return (group.childCount - 1 downTo 0).any { i -> - val child = group.getChildAt(i) - controlUnder(child, x + group.scrollX - child.x, y + group.scrollY - child.y) - } - } - - private fun outline(radius: Float, fill: Int) = outline(context, radius, fill) - - private fun filled(radius: Float, fill: Int) = GradientDrawable().apply { - cornerRadius = radius * resources.displayMetrics.density - setColor(fill) - } - - private fun dp(value: Int): Int = dp(context, value) - - - private abstract class Surface(context: Context) : View(context) { - protected val density = resources.displayMetrics.density - protected val rect = RectF() - private val fill = Paint(Paint.ANTI_ALIAS_FLAG) - private val stroke = Paint(Paint.ANTI_ALIAS_FLAG).apply { style = Paint.Style.STROKE } - protected val labelPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply { - textAlign = Paint.Align.CENTER - typeface = Typeface.create(Typeface.DEFAULT, Typeface.BOLD) - } - protected val captionPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply { - textAlign = Paint.Align.CENTER - color = TEXT_DIM - } - - protected fun drawSurface(canvas: Canvas, lit: Boolean) { - val inset = density - rect.set(inset, inset, width - inset, height - inset) - val radius = CORNER * density - fill.color = if (lit) ACCENT_WASH else SURFACE - canvas.drawRoundRect(rect, radius, radius, fill) - stroke.color = if (lit) ACCENT else LINE - stroke.strokeWidth = (if (lit) 2f else 1f) * density - canvas.drawRoundRect(rect, radius, radius, stroke) - } - } - - private open class HoldButton( - context: Context, - private val label: String, - private val caption: String?, - private val onChange: (Boolean) -> Unit, - ) : Surface(context) { - private var down = false - - override fun onDraw(canvas: Canvas) { - drawSurface(canvas, down) - val cx = width / 2f - labelPaint.textSize = min(height * 0.3f, 30 * density) - labelPaint.color = if (down) ACCENT else TEXT - val labelY = height / 2f + labelPaint.textSize * (if (caption == null) 0.35f else 0.1f) - canvas.drawText(label, cx, labelY, labelPaint) - if (caption != null) { - captionPaint.textSize = 12 * density - canvas.drawText(caption, cx, labelY + 22 * density, captionPaint) - } - } - - @SuppressLint("ClickableViewAccessibility") - override fun onTouchEvent(event: MotionEvent): Boolean { - when (event.actionMasked) { - MotionEvent.ACTION_DOWN -> set(true) - MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> set(false) - } - return true - } - - private fun set(pressed: Boolean) { - if (down == pressed) return - down = pressed - if (pressed) performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY) - onChange(pressed) - invalidate() - } - } - - private class GripButton(context: Context, label: String, caption: String?, bit: Int) : - HoldButton(context, label, caption, { DeckControls.setGrip(bit, it) }) - - private class ClickBar(context: Context, right: Boolean) : - HoldButton(context, context.getString(R.string.deck_click), if (right) context.getString(R.string.deck_right_trackpad) else context.getString(R.string.deck_left_trackpad), { DeckControls.setClick(right, it) }) - - private class TrackpadView(context: Context, private val right: Boolean, private val captioned: Boolean = true) : Surface(context) { - private var touching = false - private var clicked = false - private var fingerX = 0f - private var fingerY = 0f - private val guide = Paint(Paint.ANTI_ALIAS_FLAG).apply { style = Paint.Style.STROKE; color = GUIDE } - private val dot = Paint(Paint.ANTI_ALIAS_FLAG).apply { color = ACCENT } - private val glow = Paint(Paint.ANTI_ALIAS_FLAG) - - override fun onDraw(canvas: Canvas) { - drawSurface(canvas, clicked) - val cx = width / 2f - val cy = height / 2f - guide.strokeWidth = density - val reach = min(width, height) / 2f - 24 * density - canvas.drawCircle(cx, cy, reach, guide) - canvas.drawCircle(cx, cy, reach / 2f, guide) - canvas.drawLine(cx - reach, cy, cx + reach, cy, guide) - canvas.drawLine(cx, cy - reach, cx, cy + reach, guide) - captionPaint.textSize = 12 * density - captionPaint.color = if (touching) ACCENT else TEXT_DIM - if (captioned) canvas.drawText((if (right) context.getString(R.string.deck_right_trackpad) else context.getString(R.string.deck_left_trackpad)).uppercase(), cx, 28 * density, captionPaint) - if (touching) { - val radius = 56 * density - glow.shader = RadialGradient(fingerX, fingerY, radius, ACCENT_GLOW, Color.TRANSPARENT, Shader.TileMode.CLAMP) - canvas.drawCircle(fingerX, fingerY, radius, glow) - canvas.drawCircle(fingerX, fingerY, (if (clicked) 16 else 12) * density, dot) - } - } - - @SuppressLint("ClickableViewAccessibility") - override fun onTouchEvent(event: MotionEvent): Boolean { - when (event.actionMasked) { - MotionEvent.ACTION_DOWN, MotionEvent.ACTION_MOVE -> { - touch(event.getX(0), event.getY(0)) - click(event.pointerCount > 1) - } - MotionEvent.ACTION_POINTER_DOWN -> click(true) - MotionEvent.ACTION_POINTER_UP -> { - val stays = if (event.actionIndex == 0) 1 else 0 - touch(event.getX(stays), event.getY(stays)) - click(event.pointerCount - 1 > 1) - } - MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> { - click(false) - touching = false - DeckControls.setPad(right, touching = false) - invalidate() - } - } - return true - } - - private fun touch(x: Float, y: Float) { - fingerX = x.coerceIn(0f, width.toFloat()) - fingerY = y.coerceIn(0f, height.toFloat()) - touching = true - DeckControls.setPad(right, true, (fingerX - width / 2f) / (width / 2f), -(fingerY - height / 2f) / (height / 2f)) - invalidate() - } - - private fun click(down: Boolean) { - if (clicked == down) return - clicked = down - if (down) performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY) - DeckControls.setClick(right, down) - invalidate() - } - } - - private class PadButton( - context: Context, - private val pad: PadBridge, - label: String, - private val write: (PadState, Boolean) -> Unit, - ) : HoldButton(context, label, null, { down -> pad.applyTouch { write(it, down) } }) { - override fun onDetachedFromWindow() { - pad.applyTouch { write(it, false) } - super.onDetachedFromWindow() - } - } - - private class StickView(context: Context, private val pad: PadBridge, private val right: Boolean) : Surface(context) { - private var touching = false - private var clicking = false - private var lastUp = 0L - private var originX = 0f - private var originY = 0f - private var knobX = 0f - private var knobY = 0f - private val ring = Paint(Paint.ANTI_ALIAS_FLAG).apply { style = Paint.Style.STROKE } - private val knob = Paint(Paint.ANTI_ALIAS_FLAG) - - private fun reach() = min(width, height) * 0.30f - - override fun onDraw(canvas: Canvas) { - drawSurface(canvas, clicking) - val cx = if (touching) originX else width / 2f - val cy = if (touching) originY else height / 2f - ring.strokeWidth = 2 * density - ring.color = if (touching) ACCENT else LINE - canvas.drawCircle(cx, cy, reach(), ring) - knob.color = if (touching) ACCENT else KNOB - canvas.drawCircle(cx + knobX, cy + knobY, reach() * 0.45f, knob) - } - - @SuppressLint("ClickableViewAccessibility") - override fun onTouchEvent(event: MotionEvent): Boolean { - val r = reach().coerceAtLeast(1f) - when (event.actionMasked) { - MotionEvent.ACTION_DOWN -> { - originX = event.x.coerceIn(r, (width - r).coerceAtLeast(r)) - originY = event.y.coerceIn(r, (height - r).coerceAtLeast(r)) - touching = true - performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY) - if (event.eventTime - lastUp < CLICK_MS) setClick(true) - follow(event.x, event.y, r) - } - MotionEvent.ACTION_MOVE -> follow(event.getX(0), event.getY(0), r) - MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> { - touching = false - lastUp = event.eventTime - setClick(false) - move(0f, 0f, 1f) - } - } - return true - } - - private fun follow(x: Float, y: Float, r: Float) { - var dx = x - originX - var dy = y - originY - val length = hypot(dx, dy) - if (length > r) { dx *= r / length; dy *= r / length } - move(dx, dy, r) - } - - private fun move(dx: Float, dy: Float, r: Float) { - knobX = dx - knobY = dy - pad.applyTouch(if (right) PadBridge.RIGHT_STICK else PadBridge.LEFT_STICK) { s -> - if (right) { s.rightX = dx / r; s.rightY = dy / r } else { s.leftX = dx / r; s.leftY = dy / r } - } - invalidate() - } - - private fun setClick(down: Boolean) { - if (clicking == down) return - clicking = down - if (down) performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY) - pad.applyTouch { s -> s.press(if (right) PadState.R3 else PadState.L3, down) } - } - - override fun onDetachedFromWindow() { - setClick(false) - if (knobX != 0f || knobY != 0f) move(0f, 0f, 1f) - super.onDetachedFromWindow() - } - } - - private class DirectionPad(context: Context, private val pad: PadBridge) : View(context) { - private val density = resources.displayMetrics.density - private var held = 0 - private val arm = Paint(Paint.ANTI_ALIAS_FLAG) - private val rect = RectF() - - override fun onDraw(canvas: Canvas) { - val size = min(width, height) * 0.86f - val cx = width / 2f - val cy = height / 2f - val half = size / 2f - val thick = size / 3f / 2f - fun armRect(bit: Int, l: Float, t: Float, r: Float, b: Float) { - arm.color = if (held and bit != 0) ACCENT else KNOB - rect.set(l, t, r, b) - canvas.drawRoundRect(rect, 8 * density, 8 * density, arm) - } - armRect(1, cx - thick, cy - half, cx + thick, cy - thick) - armRect(4, cx - thick, cy + thick, cx + thick, cy + half) - armRect(8, cx - half, cy - thick, cx - thick, cy + thick) - armRect(2, cx + thick, cy - thick, cx + half, cy + thick) - arm.color = KNOB - canvas.drawRect(cx - thick, cy - thick, cx + thick, cy + thick, arm) - } - - @SuppressLint("ClickableViewAccessibility") - override fun onTouchEvent(event: MotionEvent): Boolean { - val bits = when (event.actionMasked) { - MotionEvent.ACTION_DOWN, MotionEvent.ACTION_MOVE -> { - val dx = event.getX(0) - width / 2f - val dy = event.getY(0) - height / 2f - if (hypot(dx, dy) < min(width, height) * 0.08f) 0 else { - val sector = ((Math.toDegrees(atan2(dy.toDouble(), dx.toDouble())) + 360 + 22.5) % 360 / 45).toInt() - intArrayOf(2, 2 or 4, 4, 4 or 8, 8, 8 or 1, 1, 1 or 2)[sector] - } - } - else -> 0 - } - set(bits) - return true - } - - private fun set(bits: Int) { - if (bits == held) return - if (bits and held.inv() != 0) performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY) - held = bits - pad.applyTouch { s -> - s.up = bits and 1 != 0; s.right = bits and 2 != 0; s.down = bits and 4 != 0; s.left = bits and 8 != 0 - } - invalidate() - } - - override fun onDetachedFromWindow() { - set(0) - super.onDetachedFromWindow() - } - } - - private class FaceButtons(context: Context, private val pad: PadBridge) : View(context) { - private val buttons = listOf(PadState.Y to "Y", PadState.B to "B", PadState.A to "A", PadState.X to "X") - private var held = emptySet() - private val fill = Paint(Paint.ANTI_ALIAS_FLAG) - private val text = Paint(Paint.ANTI_ALIAS_FLAG).apply { - textAlign = Paint.Align.CENTER - typeface = Typeface.create(Typeface.DEFAULT, Typeface.BOLD) - } - - private fun layout(): Pair>, Float> { - val cx = width / 2f - val cy = height / 2f - val spread = min(width, height) * 0.30f - val radius = min(width, height) * 0.16f - return listOf(cx to cy - spread, cx + spread to cy, cx to cy + spread, cx - spread to cy) to radius - } - - override fun onDraw(canvas: Canvas) { - val (centres, radius) = layout() - text.textSize = radius * 0.9f - buttons.forEachIndexed { i, (button, label) -> - val (x, y) = centres[i] - val down = button in held - fill.color = if (down) ACCENT else KNOB - canvas.drawCircle(x, y, radius, fill) - text.color = if (down) ON_ACCENT else TEXT - canvas.drawText(label, x, y + text.textSize * 0.35f, text) - } - } - - @SuppressLint("ClickableViewAccessibility") - override fun onTouchEvent(event: MotionEvent): Boolean { - val (centres, radius) = layout() - val now = HashSet() - val ending = event.actionMasked == MotionEvent.ACTION_UP || event.actionMasked == MotionEvent.ACTION_CANCEL - if (!ending) for (p in 0 until event.pointerCount) { - if (event.actionMasked == MotionEvent.ACTION_POINTER_UP && p == event.actionIndex) continue - val x = event.getX(p) - val y = event.getY(p) - val nearest = centres.indices.minByOrNull { hypot(centres[it].first - x, centres[it].second - y) } ?: continue - if (hypot(centres[nearest].first - x, centres[nearest].second - y) <= radius * 1.6f) now.add(buttons[nearest].first) - } - set(now) - return true - } - - private fun set(now: Set) { - if (now == held) return - if ((now - held).isNotEmpty()) performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY) - val before = held - held = now - pad.applyTouch { s -> for (b in before + now) s.press(b, b in now) } - invalidate() - } - - override fun onDetachedFromWindow() { - set(emptySet()) - super.onDetachedFromWindow() - } - } - - private class GamepadLayout(context: Context, private val portraitDock: Boolean) : ViewGroup(context) { - private val slots = LinkedHashMap() - - fun place(view: View, slot: GamepadGeometry.Slot) { - slots[view] = slot - addView(view) - } - - private fun boxes(w: Int, h: Int) = GamepadGeometry.layout(w.toFloat(), h.toFloat(), resources.displayMetrics.density, portraitDock) - - override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { - val w = MeasureSpec.getSize(widthMeasureSpec) - val h = MeasureSpec.getSize(heightMeasureSpec) - setMeasuredDimension(w, h) - val boxes = boxes(w, h) - for ((view, slot) in slots) { - val box = boxes[slot] - view.visibility = if (box == null) View.GONE else View.VISIBLE - if (box == null) continue - view.measure(MeasureSpec.makeMeasureSpec(box.width.toInt().coerceAtLeast(1), MeasureSpec.EXACTLY), - MeasureSpec.makeMeasureSpec(box.height.toInt().coerceAtLeast(1), MeasureSpec.EXACTLY)) - } - } - - override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) { - val boxes = boxes(r - l, b - t) - for ((view, slot) in slots) { - val box = boxes[slot] ?: continue - val left = box.left.toInt() - val top = box.top.toInt() - view.layout(left, top, left + box.width.toInt().coerceAtLeast(1), top + box.height.toInt().coerceAtLeast(1)) - } - } - } - - companion object { - private const val CLICK_MS = 300L - - private var selected = Tab.BOTH - private var selectedWithPad = Tab.BOTH - - private const val CORNER = 20f - private const val CORNER_PILL = 22f - private val ACCENT = Color.rgb(0x1A, 0x9F, 0xFF) - private val ACCENT_WASH = Color.argb(0x2E, 0x1A, 0x9F, 0xFF) - private val ACCENT_GLOW = Color.argb(0x66, 0x1A, 0x9F, 0xFF) - private val ON_ACCENT = Color.rgb(0x03, 0x11, 0x1F) - private val SURFACE = Color.rgb(0x0A, 0x0B, 0x0D) - private val LINE = Color.rgb(0x26, 0x29, 0x2E) - private val GUIDE = Color.rgb(0x16, 0x18, 0x1B) - private val TEXT = Color.rgb(0xF2, 0xF4, 0xF7) - private val TEXT_DIM = Color.rgb(0x7A, 0x80, 0x8A) - private val KNOB = Color.rgb(0x2A, 0x2D, 0x33) - - internal fun dp(context: Context, value: Int): Int = (value * context.resources.displayMetrics.density + 0.5f).toInt() - - internal fun outline(context: Context, radius: Float, fill: Int) = GradientDrawable().apply { - cornerRadius = radius * context.resources.displayMetrics.density - setColor(fill) - setStroke(dp(context, 1), LINE) - } - - internal fun roundButton(context: Context, label: String, description: String, onClick: () -> Unit, wide: Boolean = false) = - TextView(context).apply { - text = label - contentDescription = description - textSize = if (wide) 13f else 20f - typeface = Typeface.create(Typeface.DEFAULT, Typeface.BOLD) - letterSpacing = if (wide) 0.08f else 0f - gravity = Gravity.CENTER - setTextColor(TEXT) - background = outline(context, CORNER_PILL, SURFACE) - setPadding(if (wide) dp(context, 16) else 0, 0, if (wide) dp(context, 16) else 0, 0) - minWidth = dp(context, 44) - layoutParams = LinearLayout.LayoutParams(if (wide) LinearLayout.LayoutParams.WRAP_CONTENT else dp(context, 44), dp(context, 44)) - setOnClickListener { - performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY) - onClick() - } - } - - internal fun menuButton(context: Context, onClick: () -> Unit) = - iconButton(context, R.drawable.ic_stat_session, context.getString(R.string.touch_droiddeck_menu), onClick) - - internal fun iconButton(context: Context, icon: Int, description: String, onClick: () -> Unit) = android.widget.ImageView(context).apply { - setImageResource(icon) - setColorFilter(TEXT) - contentDescription = description - scaleType = android.widget.ImageView.ScaleType.CENTER_INSIDE - val pad = dp(context, 11) - setPadding(pad, pad, pad, pad) - background = outline(context, CORNER_PILL, SURFACE) - layoutParams = LinearLayout.LayoutParams(dp(context, 44), dp(context, 44)) - setOnClickListener { - performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY) - onClick() - } - } - } -} diff --git a/app/src/main/java/com/droiddeck/launcher/ui/EaUpdateButton.kt b/app/src/main/java/com/droiddeck/launcher/ui/EaUpdateButton.kt new file mode 100644 index 0000000000..9fe2a41155 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/ui/EaUpdateButton.kt @@ -0,0 +1,17 @@ +package com.droiddeck.launcher.ui + +import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.res.stringResource +import com.droiddeck.launcher.R +import com.droiddeck.launcher.stores.InstalledStoreGame +import com.droiddeck.launcher.stores.ea.EaUpdates + +@Composable +internal fun EaUpdateButton(installed: InstalledStoreGame, enabled: Boolean) { + val ctx = LocalContext.current + LaunchedEffect(installed.key, installed.sidecar.installVersion) { EaUpdates.check(ctx, installed) } + if (EaUpdates.available[installed.key] == null) return + SecondaryButton(stringResource(R.string.common_update), enabled = enabled) { EaUpdates.start(ctx, installed) } +} diff --git a/app/src/main/java/com/droiddeck/launcher/ui/FrontEndScreen.kt b/app/src/main/java/com/droiddeck/launcher/ui/FrontEndScreen.kt index f4900797ae..31a8fb28c8 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/FrontEndScreen.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/FrontEndScreen.kt @@ -115,6 +115,9 @@ class FrontEndState( val adbPaired: Boolean = false, val backgroundAppLimit: Int = -1, val memoryHud: String = "off", + val tpUbwcMode: String? = null, + val tpUbwcRestartRequired: Boolean = false, + val canRestartApp: Boolean = false, val steamGames: List, val steamGamesX64: Boolean = false, val emulators: List, @@ -141,6 +144,7 @@ class FrontEndState( val dualScreenMode: String = com.droiddeck.launcher.input.DualScreen.MODE_AUTO, val dualScreenSessions: String = com.droiddeck.launcher.input.DualScreen.MODE_AUTO, val dualScreenControls: Boolean = true, + val androidTextFieldKeyboard: Boolean = false, val packages: List? = null, val packageCatalogLoading: Boolean = false, val packageBusyId: String? = null, @@ -170,6 +174,7 @@ class FrontEndState( val downloadPrefix: String = "", val downloadProxy: String = "", val animationsEnabled: Boolean = true, + val lowStorageWarning: Boolean = true, val launcherIcon: String = com.droiddeck.launcher.LauncherIcon.DARK, val gameStoresEnabled: Boolean = true, val gameStoresSpeedTier: String = "fast", @@ -245,6 +250,8 @@ class FrontEndActions( val onSteamGpuDrawing: () -> Unit = {}, val onClearCachedApps: (Boolean) -> Unit = {}, val onMemoryHud: (String) -> Unit = {}, + val onTpUbwcMode: (String) -> Unit = {}, + val onRestartApp: () -> Unit = {}, val onBackgroundAppLimit: (Int) -> Unit = {}, val onShareLogs: () -> Unit = {}, val onShareSessionLog: (File) -> Unit = {}, @@ -262,9 +269,11 @@ class FrontEndActions( val onLauncherOrientation: (Int) -> Unit = {}, val onLauncherFullscreen: (Boolean) -> Unit = {}, val onAnimationsEnabled: (Boolean) -> Unit = {}, + val onLowStorageWarning: (Boolean) -> Unit = {}, val onDualScreenMode: (String) -> Unit = {}, val onDualScreenSessions: (String) -> Unit = {}, val onDualScreenControls: (Boolean) -> Unit = {}, + val onAndroidTextFieldKeyboard: (Boolean) -> Unit = {}, val onDownloadSource: (String) -> Unit = {}, val onDownloadMirror: (String) -> Unit = {}, val onDownloadPrefix: (String) -> Unit = {}, diff --git a/app/src/main/java/com/droiddeck/launcher/ui/FrontEndSetup.kt b/app/src/main/java/com/droiddeck/launcher/ui/FrontEndSetup.kt index 66dd4e0e1d..3532d3e771 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/FrontEndSetup.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/FrontEndSetup.kt @@ -245,7 +245,7 @@ internal fun SetupPanel( PageHeader(stringResource(R.string.setup_title)) { Box(Modifier.weight(1f)) { SheetChips(tabs, tab, chipFocus, onPick = pick) } } - Column(modifier = Modifier.weight(1f).fillMaxWidth().verticalScroll(rememberScrollState()).padding(bottom = 24.dp)) { + Column(modifier = Modifier.weight(1f).fillMaxWidth().then(if (tab == 1) Modifier else Modifier.verticalScroll(rememberScrollState())).padding(bottom = if (tab == 1) 0.dp else 24.dp)) { when (tab) { 0 -> { // Setup's "Safe mode" row, under the GPU row while the bridge is on and can be used here. @@ -413,9 +413,7 @@ internal fun SetupPanel( } 1 -> { val controller = s.controller - if (controller != null && a.controller != null) SettingsGroup(stringResource(R.string.setup_controller)) { - ControllerRows(host, s.oscMode, controller, a.controller) - } + if (controller != null && a.controller != null) InputSettingsPage(onBack = { pick(0) }, embedded = true) if (s.controller == null || a.controller == null) Note(stringResource(R.string.setup_controller_unavailable)) } 2 -> { @@ -452,6 +450,8 @@ internal fun SetupPanel( ) ToggleRow(host, "launcher-animations", stringResource(R.string.setup_animations), null, s.animationsEnabled, onChange = a.onAnimationsEnabled) + ToggleRow(host, "low-storage-warning", stringResource(R.string.setup_low_storage_warning), stringResource(R.string.setup_low_storage_warning_hint), + s.lowStorageWarning, onChange = a.onLowStorageWarning) val railContext = androidx.compose.ui.platform.LocalContext.current remember { RailLabels.load(railContext) } ToggleRow(host, "rail-labels", stringResource(R.string.setup_rail_labels), stringResource(R.string.setup_rail_labels_hint), RailLabels.shown) { RailLabels.set(railContext, it) } @@ -474,6 +474,20 @@ internal fun SetupPanel( com.droiddeck.launcher.core.AppUiPrefs.orientationOptions.map { (value, label) -> value to stringResource(label) }, s.launcherOrientation, onPick = a.onLauncherOrientation, ) + s.tpUbwcMode?.let { ubwc -> + ChoiceRow( + host, "tp-ubwc", stringResource(R.string.gpu_ubwc_title), stringResource(R.string.gpu_ubwc_hint), + listOf( + SessionPrefs.TP_UBWC_AUTO to stringResource(R.string.common_auto), + SessionPrefs.TP_UBWC_ON to stringResource(R.string.widgets_on), + SessionPrefs.TP_UBWC_OFF to stringResource(R.string.widgets_off), + ), ubwc, onPick = a.onTpUbwcMode, + ) + if (s.tpUbwcRestartRequired) { + if (s.canRestartApp) ActionRow(stringResource(R.string.gpu_ubwc_restart), null, stringResource(R.string.gpu_restart_now), a.onRestartApp) + else Note(stringResource(R.string.gpu_ubwc_restart)) + } + } ChoiceRow( host, "orientation", stringResource(R.string.setup_session_orientation), stringResource(R.string.setup_session_orientation_hint), SessionPrefs.orientationOptions.map { (value, label) -> value to stringResource(label) }, @@ -502,6 +516,8 @@ internal fun SetupPanel( ToggleRow(host, "dual-screen-controls", stringResource(R.string.setup_dual_screen_controls), stringResource(R.string.setup_dual_screen_controls_hint), s.dualScreenControls) { a.onDualScreenControls(it) } } + ToggleRow(host, "android-text-field-keyboard", stringResource(R.string.setup_android_text_field_keyboard), + null, s.androidTextFieldKeyboard) { a.onAndroidTextFieldKeyboard(it) } } } } @@ -835,6 +851,7 @@ internal fun GameSettingsMenus(host: MenuHost, game: com.droiddeck.launcher.fron ) val appSync = syncLabels[com.droiddeck.launcher.session.SessionPrefs.syncBackend(appContext)] val useDefault = stringResource(R.string.comp_game_use_general) + val generalFex = com.droiddeck.launcher.core.FexPreset.byId(com.droiddeck.launcher.session.SessionPrefs.fexPreset(appContext)) val custom = stringResource(R.string.comp_game_fex_custom) val customSync = stringResource(R.string.game_sync_custom) val resetDone = stringResource(R.string.comp_game_reset_done, game.name) @@ -849,7 +866,7 @@ internal fun GameSettingsMenus(host: MenuHost, game: com.droiddeck.launcher.fron MenuItem( stringResource(R.string.fex_preset_title), checked = false, enabled = ready, detail = when { - fex == null -> useDefault + fex == null -> "$useDefault · ${stringResource(generalFex.label)}" chosen == null -> custom else -> stringResource(chosen.label) }, @@ -881,7 +898,7 @@ internal fun GameSettingsMenus(host: MenuHost, game: com.droiddeck.launcher.fron } } AnchoredMenu(host.open == fexKey, onDismiss = { if (host.open == fexKey) host.open = null }, title = stringResource(R.string.fex_preset_title)) { first -> - MenuItem(useDefault, checked = fex == null, enabled = ready, focusRequester = first) { + MenuItem(useDefault, checked = fex == null, enabled = ready, detail = stringResource(generalFex.label), focusRequester = first) { host.open = key save { com.droiddeck.launcher.session.GameEnvironmentStore.setGameFexPreset(it, scope, null) } } diff --git a/app/src/main/java/com/droiddeck/launcher/ui/GameDetail.kt b/app/src/main/java/com/droiddeck/launcher/ui/GameDetail.kt index 390295f696..f42a85de51 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/GameDetail.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/GameDetail.kt @@ -430,6 +430,7 @@ internal fun GameActions( openUrl(ctx, item?.storeUrl.orEmpty().ifBlank { GameJoltApi.pageUrl("game", installed.sidecar.id) }) } } + if (store == Store.EA && installed != null) EaUpdateButton(installed, download == null && StoresState.uninstalling[installed.key] == null) if (store == Store.GOG && installed != null) GogMaintenanceActions(installed) if (store == Store.XBOX && id != null) XboxInstalledStream(id, game?.name ?: item?.title.orEmpty(), item) if (store == Store.XBOX && installed != null && id != null) { @@ -874,12 +875,15 @@ private fun SavesTab(g: Library.SteamGame, prefix: File, a: FrontEndActions) { @Composable private fun AddonsTab(g: Library.SteamGame, targets: List, a: FrontEndActions) { + var browsing by remember(g.gameId) { mutableStateOf(null) } + browsing?.let { AddonBrowserDialog(targets, it) { browsing = null } } Column(Modifier.fillMaxWidth().tabPanel(), verticalArrangement = Arrangement.spacedBy(2.dp)) { targets.forEach { target -> val names by produceState(emptyList(), target.addOns, WowAddons.imports) { value = withContext(Dispatchers.IO) { WowAddons.installed(target) } } if (targets.size > 1) SectionLabel(target.flavor) + MenuItem(stringResource(R.string.addons_download), checked = false) { browsing = target } MenuItem( stringResource(R.string.games_import_addons), checked = false, detail = pluralStringResource(R.plurals.games_addons_count, names.size, names.size), @@ -912,7 +916,7 @@ private fun AddonRow(name: String, target: WowAddons.Target, a: FrontEndActions) @Composable private fun CloudRows(g: Library.SteamGame) { - val store = Store.byId(g.source)?.takeIf { it != Store.AMAZON } ?: return + val store = Store.byId(g.source)?.takeIf { it != Store.AMAZON && it != Store.EA } ?: return val id = g.storeId ?: return val context = LocalContext.current.applicationContext var busy by remember(g.gameId) { mutableStateOf(null) } diff --git a/app/src/main/java/com/droiddeck/launcher/ui/GpuDriversPanel.kt b/app/src/main/java/com/droiddeck/launcher/ui/GpuDriversPanel.kt index 823c065aa6..06145d3bb2 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/GpuDriversPanel.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/GpuDriversPanel.kt @@ -1,7 +1,6 @@ package com.droiddeck.launcher.ui import com.droiddeck.launcher.R -import androidx.compose.animation.AnimatedVisibility import androidx.compose.foundation.focusGroup import androidx.compose.ui.geometry.Offset import androidx.compose.ui.layout.LayoutCoordinates @@ -134,8 +133,6 @@ class GpuDriversState( val runtime: DriverSlot = DriverSlot(), val canRestart: Boolean = true, val checkedAt: Long = 0L, - val tpUbwcMode: String? = null, - val tpUbwcRestartRequired: Boolean = false, ) { val activePair: PairRow? get() = pairs.firstOrNull { it.active } } @@ -156,7 +153,6 @@ class GpuDriversActions( val onRestoreBundled: () -> Unit = {}, val onMode: (String) -> Unit = {}, val onRestart: () -> Unit = {}, - val onTpUbwcMode: (String) -> Unit = {}, ) @Composable @@ -258,27 +254,6 @@ internal fun GpuDriversBody( GPU_MODE_PAIR -> PairBody(gpu, actions) else -> MixBody(single, actions, slotFocus, onOpenSlot) } - gpu.tpUbwcMode?.let { selected -> - var advanced by rememberSaveable { mutableStateOf(false) } - SecondaryButton(stringResource(R.string.gpu_advanced) + if (advanced) " ▴" else " ▾", modifier = Modifier.fillMaxWidth()) { advanced = !advanced } - AnimatedVisibility(advanced) { - Column(verticalArrangement = Arrangement.spacedBy(10.dp)) { - ChoiceRow( - rememberMenuHost(), "tp-ubwc", stringResource(R.string.gpu_ubwc_title), - stringResource(R.string.gpu_ubwc_hint), - listOf( - SessionPrefs.TP_UBWC_AUTO to stringResource(R.string.common_auto), - SessionPrefs.TP_UBWC_ON to stringResource(R.string.widgets_on), - SessionPrefs.TP_UBWC_OFF to stringResource(R.string.widgets_off), - ), selected, onPick = actions.onTpUbwcMode, - ) - if (gpu.tpUbwcRestartRequired) { - Text(stringResource(R.string.gpu_ubwc_restart), fontSize = 13.sp, color = AttentionAmber) - if (gpu.canRestart) SecondaryButton(stringResource(R.string.gpu_restart_now), onClick = actions.onRestart) - } - } - } - } } } diff --git a/app/src/main/java/com/droiddeck/launcher/ui/InputActionEditor.kt b/app/src/main/java/com/droiddeck/launcher/ui/InputActionEditor.kt new file mode 100644 index 0000000000..01464cef3b --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/ui/InputActionEditor.kt @@ -0,0 +1,136 @@ +package com.droiddeck.launcher.ui + +import androidx.activity.compose.BackHandler +import androidx.compose.foundation.layout.* +import androidx.compose.material3.* +import androidx.compose.runtime.* +import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.platform.testTag +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.unit.dp +import com.droiddeck.launcher.R +import com.droiddeck.launcher.input.* + +@Composable +internal fun InputActionEditor( + control: InputControl, current: InputAction, title: String, onBack: () -> Unit, onSave: (InputAction) -> Unit, + element: InputSurfaceElement? = null, nativeTargets: Boolean = true, detail: String? = null, + properties: (@Composable ColumnScope.(InputAction) -> Unit)? = null, + click: Boolean = false, +) { + val context = LocalContext.current + val host = rememberMenuHost() + var draft by remember(current, title) { mutableStateOf(current) } + val drafts = remember(current, title) { mutableMapOf() } + fun typeOf(action: InputAction): String = when (action) { + is InputAction.Native -> "native"; InputAction.Disabled -> "disabled" + is InputAction.MouseButton -> "mouse"; is InputAction.Command -> "command" + is InputAction.Pointer -> "pointer"; is InputAction.Scroll -> "scroll" + is InputAction.Directions -> "directions"; is InputAction.Keys -> "keys" + } + var localType by remember(current, title) { mutableStateOf(typeOf(current).takeUnless { it == "native" || it == "disabled" } ?: "keys") } + fun switch(type: String, initial: () -> InputAction) { + drafts[typeOf(draft)] = draft + draft = drafts[type] ?: initial() + if (type != "native" && type != "disabled") localType = type + } + var keys by remember { mutableStateOf(false) } + var direction by remember { mutableStateOf(null) } + fun valid(action: InputAction) = if (click) action.validForClick() else if (element != null) action.validFor(element) else action.validFor(control) + val nativeOptions = remember(control, element, nativeTargets, click) { + (listOf(InputAction.Native()) + if (nativeTargets) InputControl.entries.map { InputAction.Native(it) } else emptyList()).filter(::valid) + } + val route = when (draft) { is InputAction.Native -> 0; InputAction.Disabled -> 2; else -> 1 } + BackHandler(onBack = onBack) + SettingsPage(host, title, onBack, eyebrow = stringResource(R.string.input_properties), lede = detail, + action = { PrimaryButton(stringResource(R.string.gesture_save_changes), enabled = valid(draft), + modifier = Modifier.testTag("input-action-save")) { onSave(draft) } }) { + properties?.invoke(this, draft) + Spacer(Modifier.height(12.dp)) + SheetChips(listOf(stringResource(R.string.input_route_controller), stringResource(R.string.input_route_local), stringResource(R.string.input_disabled)), route) { + when (it) { + 0 -> switch("native") { nativeOptions.firstOrNull() ?: InputAction.Disabled } + 2 -> switch("disabled") { InputAction.Disabled } + else -> switch(localType) { InputAction.Keys(listOf(28)) } + } + } + Spacer(Modifier.height(16.dp)) + when (route) { + 0 -> SettingsGroup(stringResource(R.string.input_route_controller)) { + ChoiceRow(host, "native-target", stringResource(R.string.input_remap_native), stringResource(R.string.input_controller_route_hint), + nativeOptions.map { it to (it.target?.label ?: stringResource(if (nativeTargets) R.string.input_native else R.string.input_normal_binding)) }, + draft as InputAction.Native) { draft = it } + } + 2 -> Text(stringResource(R.string.input_disabled_hint), Modifier.padding(16.dp)) + else -> { + val types = listOf("keys" to stringResource(R.string.input_keys), "mouse" to stringResource(R.string.input_mouse_click), + "command" to stringResource(R.string.input_command)) + if (control.dimensions == 2) listOf( + "pointer" to stringResource(R.string.input_pointer), "scroll" to stringResource(R.string.input_scroll), + "directions" to stringResource(R.string.input_direction_keys)) else emptyList() + val type = when (draft) { is InputAction.MouseButton -> "mouse"; is InputAction.Command -> "command"; + is InputAction.Pointer -> "pointer"; is InputAction.Scroll -> "scroll"; is InputAction.Directions -> "directions"; else -> "keys" } + SettingsGroup(stringResource(R.string.input_action)) { + ChoiceRow(host, "action-type", stringResource(R.string.input_action), null, types, type) { + switch(it) { when (it) { "mouse" -> InputAction.MouseButton(272); "command" -> InputAction.Command(InputCommand.MENU); + "pointer" -> InputAction.Pointer(); "scroll" -> InputAction.Scroll(); + "directions" -> InputAction.Directions(103, 106, 108, 105); else -> InputAction.Keys(listOf(28)) } } + } + if (!click && draft !is InputAction.Pointer && draft !is InputAction.Scroll && draft !is InputAction.Directions) { + val hint = when { + control.isTrackpad -> R.string.input_contact_action_hint + control.dimensions == 2 -> R.string.input_axis_action_hint + control == InputControl.LT || control == InputControl.RT -> R.string.input_trigger_action_hint + else -> null + } + hint?.let { Text(stringResource(it), Modifier.padding(horizontal = 16.dp, vertical = 8.dp), + style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant) } + } + when (val action = draft) { + is InputAction.Keys -> ActionRow(stringResource(R.string.input_keys), actionLabel(action), stringResource(R.string.ctrl_edit), { keys = true }) + is InputAction.MouseButton -> ChoiceRow(host, "mouse-button", stringResource(R.string.input_mouse_click), null, + (272..274).map { it to actionLabel(InputAction.MouseButton(it)) }, action.code) { draft = InputAction.MouseButton(it) } + is InputAction.Command -> ChoiceRow(host, "command", stringResource(R.string.input_command), null, + InputCommand.entries.map { it to commandLabel(it) }, action.command) { draft = InputAction.Command(it) } + is InputAction.Pointer -> { + Text(stringResource(R.string.input_pointer_speed, action.speed.toInt()), Modifier.padding(horizontal = 16.dp, vertical = 8.dp), color = MaterialTheme.colorScheme.onSurface) + Slider(action.speed, { draft = action.copy(speed = it) }, valueRange = 100f..3000f, modifier = Modifier.padding(horizontal = 16.dp)) + } + is InputAction.Scroll -> { + ChoiceRow(host, "scroll-axis", stringResource(R.string.input_scroll_axis), null, + listOf(false to stringResource(R.string.input_vertical), true to stringResource(R.string.input_horizontal)), action.horizontal) { + draft = action.copy(horizontal = it) + } + Text(stringResource(R.string.input_scroll_speed, action.speed.toInt()), Modifier.padding(horizontal = 16.dp, vertical = 8.dp), color = MaterialTheme.colorScheme.onSurface) + Slider(action.speed, { draft = action.copy(speed = it) }, valueRange = 1f..60f, modifier = Modifier.padding(horizontal = 16.dp)) + } + is InputAction.Directions -> { + ToggleRow(host, "direction-diagonals", stringResource(R.string.input_diagonal_keys), stringResource(R.string.input_diagonal_keys_hint), action.diagonals != null) { + draft = action.copy(diagonals = if (it) action.diagonals ?: listOf(0, 0, 0, 0) else null) + } + val codes = listOf(action.up, action.right, action.down, action.left) + action.diagonals.orEmpty() + val arrows = listOf("↑", "→", "↓", "←", "↗", "↘", "↙", "↖") + codes.forEachIndexed { i, code -> + ActionRow(arrows[i], if (code == 0) stringResource(if (i < 4) R.string.input_disabled else R.string.input_diagonal_neighbors) else pcKeyLabel(context, code), stringResource(R.string.ctrl_edit), { direction = i }) + } + } + else -> Unit + } + } + } + } + } + if (keys) KeyCombinationPicker(onDismiss = { keys = false }, initial = (draft as? InputAction.Keys)?.codes.orEmpty(), + sequence = true, sequenceLimit = 8, title = stringResource(R.string.input_keys), confirm = stringResource(R.string.gesture_save_changes), onAdd = { + draft = InputAction.Keys(it.sortedBy { code -> if (code in PC_MODIFIERS) 0 else 1 }); keys = false + }) + direction?.let { at -> + val action = draft as? InputAction.Directions ?: return@let + val codes = listOf(action.up, action.right, action.down, action.left) + action.diagonals.orEmpty() + KeyCombinationPicker(onDismiss = { direction = null }, initial = listOf(codes[at]).filter { it != 0 }, sequence = true, sequenceLimit = 1, allowEmpty = true, + title = stringResource(R.string.input_direction_keys), confirm = stringResource(R.string.gesture_save_changes), onAdd = { + val next = codes.toMutableList().also { values -> values[at] = it.firstOrNull() ?: 0 } + draft = InputAction.Directions(next[0], next[1], next[2], next[3], if (action.diagonals != null) next.drop(4) else null); direction = null + }) + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/ui/InputBindingWidgets.kt b/app/src/main/java/com/droiddeck/launcher/ui/InputBindingWidgets.kt new file mode 100644 index 0000000000..a8eb95b347 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/ui/InputBindingWidgets.kt @@ -0,0 +1,79 @@ +package com.droiddeck.launcher.ui + +import androidx.compose.foundation.layout.Column +import androidx.compose.ui.focus.FocusRequester +import androidx.compose.ui.focus.focusRequester +import androidx.compose.material3.* +import androidx.compose.runtime.* +import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.window.DialogProperties +import com.droiddeck.launcher.R +import com.droiddeck.launcher.input.* + +@Composable +internal fun actionLabel(action: InputAction): String { + val context = LocalContext.current + return when (action) { + is InputAction.Native -> action.target?.let { stringResource(R.string.input_native_target, it.label) } ?: stringResource(R.string.input_native) + InputAction.Disabled -> stringResource(R.string.input_disabled) + is InputAction.Keys -> action.codes.joinToString(" + ") { pcKeyLabel(context, it) } + is InputAction.MouseButton -> stringResource(when (action.code) { 272 -> R.string.input_mouse_left; 273 -> R.string.input_mouse_right; else -> R.string.input_mouse_middle }) + is InputAction.Command -> commandLabel(action.command) + is InputAction.Directions -> listOf(action.up, action.right, action.down, action.left).joinToString(" / ") { pcKeyLabel(context, it) } + is InputAction.Pointer -> stringResource(R.string.input_pointer) + is InputAction.Scroll -> stringResource(if (action.horizontal) R.string.input_scroll_horizontal else R.string.input_scroll_vertical) + } +} + +@Composable +internal fun commandLabel(command: InputCommand) = stringResource(when (command) { + InputCommand.MENU -> R.string.input_command_menu + InputCommand.KEYBOARD -> R.string.input_command_keyboard + InputCommand.STEAM -> R.string.input_command_steam + InputCommand.QUICK_ACCESS -> R.string.input_command_qam +}) + +@Composable +internal fun InputCaptureDialog(onDismiss: () -> Unit, onPick: (PhysicalInputKey) -> Unit) { + val confirmFocus = remember { FocusRequester() } + var captured by remember { mutableStateOf(null) } + var capturedDevice by remember { mutableIntStateOf(-1) } + var ready by remember { mutableStateOf(false) } + AlertDialog(onDismissRequest = onDismiss, title = { Text(stringResource(R.string.input_capture)) }, + properties = DialogProperties(dismissOnBackPress = ready), + text = { + CaptureShortcutKeys { event -> + if (ready) { + if (event.keyCode == android.view.KeyEvent.KEYCODE_BACK || event.keyCode == android.view.KeyEvent.KEYCODE_BUTTON_B) { + if (event.action == android.view.KeyEvent.ACTION_DOWN && event.repeatCount == 0) onDismiss() + true + } else false + } else { + val key = PhysicalInputKey.capture(event) + if (key == null) false else { + if (captured == null && event.action == android.view.KeyEvent.ACTION_DOWN && event.repeatCount == 0) { + captured = key + capturedDevice = event.deviceId + } else if (captured == key && capturedDevice == event.deviceId && event.action == android.view.KeyEvent.ACTION_UP) ready = true + true + } + } + } + Column { + Text(captured?.label ?: stringResource(R.string.input_capture_hint)) + if (captured != null) Text(stringResource(R.string.input_capture_confirm)) + } + }, + confirmButton = { + TextButton(enabled = ready, modifier = Modifier.focusRequester(confirmFocus) + .controllerConfirm(ready) { captured?.let(onPick) }, onClick = { captured?.let(onPick) }) { Text(stringResource(R.string.ctrl_edit)) } + LaunchedEffect(ready) { if (ready) confirmFocus.requestFocus() } + }, + dismissButton = { + TextButton(modifier = Modifier.controllerConfirm { onDismiss() }, onClick = onDismiss) { Text(stringResource(R.string.gesture_cancel)) } + if (ready) TextButton(modifier = Modifier.controllerConfirm { captured = null; ready = false }, + onClick = { captured = null; ready = false }) { Text(stringResource(R.string.input_capture_retry)) } + }) +} diff --git a/app/src/main/java/com/droiddeck/launcher/ui/InputDisplayContent.kt b/app/src/main/java/com/droiddeck/launcher/ui/InputDisplayContent.kt new file mode 100644 index 0000000000..bf4674ddfa --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/ui/InputDisplayContent.kt @@ -0,0 +1,74 @@ +package com.droiddeck.launcher.ui + +import android.app.Presentation +import android.graphics.Color +import android.graphics.drawable.ColorDrawable +import android.os.Bundle +import android.view.Display +import android.view.WindowManager +import androidx.activity.ComponentActivity +import androidx.activity.setViewTreeOnBackPressedDispatcherOwner +import androidx.compose.runtime.* +import androidx.compose.ui.platform.ComposeView +import androidx.lifecycle.Lifecycle +import androidx.lifecycle.LifecycleEventObserver +import androidx.lifecycle.setViewTreeLifecycleOwner +import androidx.lifecycle.setViewTreeViewModelStoreOwner +import androidx.savedstate.setViewTreeSavedStateRegistryOwner + +internal val LocalInputActivity = staticCompositionLocalOf { null } + +fun ComponentActivity.screenIsOff(): Boolean = getSystemService(android.os.PowerManager::class.java)?.isInteractive == false + +@Composable +internal fun InputDisplayContent(host: ComponentActivity, display: Display, onDismiss: () -> Unit, content: @Composable () -> Unit) { + val parent = rememberCompositionContext() + val currentContent by rememberUpdatedState(content) + val dismiss by rememberUpdatedState(onDismiss) + DisposableEffect(host, display.displayId) { + val presentation = object : Presentation(host, display) { + var compose: ComposeView? = null + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + window?.apply { + setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT)) + clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND) + prepareInputEditor() + decorView.setViewTreeLifecycleOwner(host) + decorView.setViewTreeViewModelStoreOwner(host) + decorView.setViewTreeSavedStateRegistryOwner(host) + decorView.setViewTreeOnBackPressedDispatcherOwner(host) + } + // Presentation contexts cannot host TYPE_APPLICATION dialogs on Android 12+. + compose = ComposeView(host).also { + it.setParentCompositionContext(parent) + it.setContent { CompositionLocalProvider(LocalInputActivity provides host) { currentContent() } } + setContentView(it) + } + } + override fun dismiss() { compose?.disposeComposition(); compose = null; super.dismiss() } + } + presentation.setOnCancelListener { dismiss() } + fun showPresentation() { + if (!display.isValid) { dismiss(); return } + try { + presentation.show() + } catch (_: WindowManager.InvalidDisplayException) { + presentation.dismiss() + dismiss() + } + } + (host as? com.droiddeck.launcher.SessionActivity)?.inputWindowShown(display.displayId) + showPresentation() + val observer = LifecycleEventObserver { _, event -> + if (event == Lifecycle.Event.ON_STOP && !host.screenIsOff()) presentation.hide() + if (event == Lifecycle.Event.ON_START) showPresentation() + } + host.lifecycle.addObserver(observer) + onDispose { + host.lifecycle.removeObserver(observer) + presentation.dismiss() + (host as? com.droiddeck.launcher.SessionActivity)?.inputWindowClosed(display.displayId) + } + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/ui/InputElementProperties.kt b/app/src/main/java/com/droiddeck/launcher/ui/InputElementProperties.kt new file mode 100644 index 0000000000..406e6f7f5f --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/ui/InputElementProperties.kt @@ -0,0 +1,81 @@ +package com.droiddeck.launcher.ui + +import androidx.compose.foundation.layout.* +import androidx.compose.material3.* +import androidx.compose.runtime.Composable +import androidx.compose.runtime.remember +import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.platform.testTag +import androidx.compose.foundation.Canvas +import androidx.compose.ui.graphics.nativeCanvas +import com.droiddeck.launcher.input.* +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.unit.dp +import androidx.activity.compose.rememberLauncherForActivityResult +import androidx.activity.result.contract.ActivityResultContracts +import androidx.compose.runtime.getValue +import androidx.compose.runtime.rememberCoroutineScope +import androidx.compose.runtime.rememberUpdatedState +import com.droiddeck.launcher.R +import kotlin.math.roundToInt +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext + +@Composable +internal fun InputElementProperties(element: InputSurfaceElement, action: InputAction? = null, onChange: (InputSurfaceElement) -> Unit) { + val current by rememberUpdatedState(element) + val change by rememberUpdatedState(onChange) + OutlinedTextField(element.name, { onChange(element.copy(name = it.take(80))) }, label = { Text(stringResource(R.string.input_name)) }, + singleLine = true, modifier = Modifier.fillMaxWidth().padding(bottom = 16.dp)) + if (element.kind == InputElementKind.BUTTON || element.kind == InputElementKind.TRIGGER) { + val host = rememberMenuHost() + val context = LocalContext.current + val scope = rememberCoroutineScope() + val pickImage = rememberLauncherForActivityResult(ActivityResultContracts.GetContent()) { uri -> + if (uri != null) scope.launch { + withContext(Dispatchers.IO) { InputButtonImages.import(context.applicationContext, uri) }?.let { change(current.copy(icon = 0, image = it)) } + } + } + ToggleRow(host, "element-toggle", stringResource(R.string.input_toggle_button), stringResource(R.string.input_toggle_button_hint), element.toggle) { onChange(element.copy(toggle = it)) } + ChoiceRow(host, "element-shape", stringResource(R.string.input_button_shape), null, + InputButtonShape.entries.map { it to stringResource(when (it) { InputButtonShape.ROUNDED -> R.string.input_shape_rounded; InputButtonShape.PILL -> R.string.input_shape_pill; InputButtonShape.CIRCLE -> R.string.input_shape_circle; InputButtonShape.RECTANGLE -> R.string.input_shape_rectangle }) }, + element.buttonShape) { onChange(element.copy(buttonShape = it)) } + val open = host.open == "element-icon" + SettingsRow(stringResource(R.string.input_button_symbol), null, highlighted = open) { + Box { + ValueChip(if (element.image.isNotEmpty()) stringResource(R.string.input_imported_image) else if (element.icon == 0) stringResource(R.string.input_button_text) else InputButtonIcons.label(context, element.icon), open) { host.open = if (open) null else "element-icon" } + AnchoredMenu(open, onDismiss = { host.open = null }, title = stringResource(R.string.input_button_symbol)) { firstItemFocus -> + MenuItem(stringResource(R.string.input_button_text), element.icon == 0 && element.image.isEmpty(), focusRequester = firstItemFocus) { onChange(element.copy(icon = 0, image = "")); host.open = null } + MenuItem(stringResource(R.string.screen_key_image), element.image.isNotEmpty()) { host.open = null; pickImage.launch("image/*") } + for (code in InputButtonIcons.codes) MenuItem(InputButtonIcons.label(context, code), element.icon == code && element.image.isEmpty(), leading = { + val color = MaterialTheme.colorScheme.onSurface + val symbol = remember(context, code) { InputButtonIcons.drawable(context, code) } + val paint = remember { android.graphics.Paint(android.graphics.Paint.ANTI_ALIAS_FLAG) } + Canvas(Modifier.size(22.dp)) { + paint.color = android.graphics.Color.argb((color.alpha * 255).toInt(), (color.red * 255).toInt(), (color.green * 255).toInt(), (color.blue * 255).toInt()) + InputButtonIcons.draw(drawContext.canvas.nativeCanvas, code, size.width / 2, size.height / 2, size.minDimension, paint, symbol) + } + }) { onChange(element.copy(icon = code, image = "")); host.open = null } + } + } + } + } + if (element.kind == InputElementKind.TRACKPAD) { + ToggleRow(rememberMenuHost(), "element-mouse-gestures", stringResource(R.string.input_mouse_gestures), stringResource(R.string.input_mouse_gestures_hint), element.mouseGestures) { onChange(element.copy(mouseGestures = it)) } + } + if (element.kind == InputElementKind.STICK) { + val host = rememberMenuHost() + ToggleRow(host, "element-floating", stringResource(R.string.input_floating_stick), null, element.floating) { onChange(element.copy(floating = it)) } + ToggleRow(host, "element-hide-idle", stringResource(R.string.input_hide_idle), null, element.hideWhenIdle) { onChange(element.copy(hideWhenIdle = it)) } + } + val host = rememberMenuHost() + if ((element.kind == InputElementKind.BUTTON || element.kind == InputElementKind.TRIGGER) && element.icon == 0 && element.image.isEmpty() || + element.kind == InputElementKind.STICK && element.id.startsWith("legacy:") || + (element.kind == InputElementKind.STICK || element.kind == InputElementKind.DPAD) && (action as? InputAction.Directions) + ?.let { listOf(it.up, it.right, it.down, it.left).plus(if (element.kind == InputElementKind.STICK) it.diagonals.orEmpty() else emptyList()).any { code -> code != 0 } } == true) + ToggleRow(host, "element-label", stringResource(R.string.input_show_label), null, element.showLabel) { onChange(element.copy(showLabel = it)) } + SliderRow(stringResource(R.string.ctrl_opacity), null, (element.opacity * 100).roundToInt().coerceIn(20, 100), 20..100, step = 5, + format = { "$it%" }, modifier = Modifier.testTag("setting-element-opacity")) { onChange(element.copy(opacity = it / 100f)) } +} diff --git a/app/src/main/java/com/droiddeck/launcher/ui/InputFocus.kt b/app/src/main/java/com/droiddeck/launcher/ui/InputFocus.kt new file mode 100644 index 0000000000..def5101b7f --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/ui/InputFocus.kt @@ -0,0 +1,47 @@ +package com.droiddeck.launcher.ui + +import androidx.compose.foundation.LocalIndication +import androidx.compose.foundation.background +import androidx.compose.foundation.clickable +import androidx.compose.foundation.hoverable +import androidx.compose.foundation.interaction.MutableInteractionSource +import androidx.compose.foundation.interaction.collectIsFocusedAsState +import androidx.compose.foundation.interaction.collectIsHoveredAsState +import androidx.compose.foundation.layout.RowScope +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.material3.TextButton +import androidx.compose.runtime.Composable +import androidx.compose.runtime.remember +import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.clip +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.graphics.Shape +import androidx.compose.ui.semantics.Role +import androidx.compose.ui.semantics.selected +import androidx.compose.ui.semantics.semantics +import androidx.compose.ui.semantics.toggleableState +import androidx.compose.ui.state.ToggleableState +import androidx.compose.ui.unit.dp + +/** A row that shows focus and answers the controller's A button, like the rest of the settings widgets. */ +@Composable +internal fun Modifier.focusRow(shape: Shape = RoundedCornerShape(10.dp), role: Role = Role.Button, enabled: Boolean = true, checked: Boolean? = null, onClick: () -> Unit): Modifier { + val source = remember { MutableInteractionSource() } + val hot = source.collectIsFocusedAsState().value || source.collectIsHoveredAsState().value + return clip(shape) + .background(if (hot && enabled) Color.White.copy(alpha = 0.07f) else Color.Transparent) + .glideBorder(hot && enabled, shape, LocalPalette.current.signal) + .semantics(mergeDescendants = true) { + if (checked != null) { + if (role == Role.RadioButton) selected = checked else toggleableState = if (checked) ToggleableState.On else ToggleableState.Off + } + } + .hoverable(source) + .clickable(interactionSource = source, indication = LocalIndication.current, enabled = enabled, role = role, onClick = onClick) + .controllerConfirm(enabled = enabled, onClick = onClick) +} + +@Composable +internal fun PadTextButton(onClick: () -> Unit, modifier: Modifier = Modifier, enabled: Boolean = true, content: @Composable RowScope.() -> Unit) { + TextButton(onClick = onClick, enabled = enabled, modifier = modifier.controllerConfirm(enabled = enabled, onClick = onClick), content = content) +} diff --git a/app/src/main/java/com/droiddeck/launcher/ui/InputLayoutWidgets.kt b/app/src/main/java/com/droiddeck/launcher/ui/InputLayoutWidgets.kt new file mode 100644 index 0000000000..f35d3daa46 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/ui/InputLayoutWidgets.kt @@ -0,0 +1,92 @@ +package com.droiddeck.launcher.ui + +import android.content.Context +import android.graphics.Canvas +import android.view.View +import androidx.compose.foundation.background +import androidx.compose.foundation.border +import androidx.compose.foundation.layout.* +import androidx.compose.foundation.shape.CircleShape +import androidx.compose.material3.* +import androidx.compose.runtime.* +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.platform.testTag +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.unit.dp +import androidx.compose.ui.viewinterop.AndroidView +import com.droiddeck.launcher.R +import com.droiddeck.launcher.input.* + +@Composable +internal fun InputLayoutRow(layout: SavedInputLayout, role: InputRole, detail: String?, onManage: () -> Unit, onPick: () -> Unit) { + Row(Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) { + Row(Modifier.weight(1f).focusRow(onClick = onPick).padding(12.dp), + verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(12.dp)) { + InputLayoutThumbnail(layout, role) + Column(Modifier.weight(1f), verticalArrangement = Arrangement.spacedBy(4.dp)) { + Text(layout.name, style = MaterialTheme.typography.titleMedium) + Text(detail ?: stringResource(R.string.input_layout_count, layout.elements.size), + style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant) + } + } + PadTextButton(onClick = onManage, modifier = Modifier.testTag("layout-manage-${layout.id}")) { Text("⋮", style = MaterialTheme.typography.titleLarge) } + } +} + +@Composable +internal fun InputLayoutThumbnail(layout: SavedInputLayout, role: InputRole) { + AndroidView(factory = { InputLayoutPreviewView(it) }, modifier = Modifier.width(112.dp).height(76.dp).testTag("layout-preview-${layout.id}"), + update = { it.update(layout, role) }) +} + +internal class InputLayoutPreviewView(context: Context) : View(context) { + private val renderer = InputSurfaceView(context) + private var sourceHeight = 563 + fun update(layout: SavedInputLayout, role: InputRole) { + sourceHeight = if (role == InputRole.PRIMARY) 563 else 870 + renderer.updateCustomLayout(layout.elements) + renderer.updateStyle(layout.style.copy(opacity = 100)) + renderer.measure(MeasureSpec.makeMeasureSpec(1000, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(sourceHeight, MeasureSpec.EXACTLY)) + renderer.layout(0, 0, 1000, sourceHeight) + contentDescription = layout.name + invalidate() + } + override fun onDraw(canvas: Canvas) { + canvas.save() + canvas.clipRect(0, 0, width, height) + canvas.drawColor(android.graphics.Color.rgb(10, 11, 13)) + val scale = minOf(width / 1000f, height / sourceHeight.toFloat()) + canvas.translate((width - 1000 * scale) / 2, (height - sourceHeight * scale) / 2) + canvas.scale(scale, scale) + renderer.draw(canvas) + canvas.restore() + } +} + +@Composable +internal fun InputColorRow(host: MenuHost, value: Int?, onPick: (Int?) -> Unit) { + val open = host.open == "layout-color" + val tints = listOf>(null to R.string.ctrl_tint_gray) + ControllerPrefs.tints + SettingsRow(stringResource(R.string.ctrl_color), null, highlighted = open) { + Box { + ValueChip(tints.firstOrNull { it.first == value }?.second?.let { stringResource(it) } + ?: stringResource(R.string.ctrl_custom), open, modifier = Modifier.testTag("setting-layout-color")) { + host.open = if (open) null else "layout-color" + } + AnchoredMenu(open, onDismiss = { if (host.open == "layout-color") host.open = null }, title = stringResource(R.string.ctrl_color)) { firstItemFocus -> + tints.forEachIndexed { index, (color, name) -> + MenuItem(stringResource(name), checked = value == color, leading = { + Box(Modifier.size(14.dp).background(Color(color ?: GRAY_SWATCH), CircleShape).border(1.dp, Color.White.copy(alpha = .35f), CircleShape)) + }, focusRequester = if (index == 0) firstItemFocus else null) { + onPick(color) + host.open = null + } + } + } + } + } +} + +private const val GRAY_SWATCH = 0xFF26292E.toInt() diff --git a/app/src/main/java/com/droiddeck/launcher/ui/InputQuickSettings.kt b/app/src/main/java/com/droiddeck/launcher/ui/InputQuickSettings.kt new file mode 100644 index 0000000000..3e53e3cce1 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/ui/InputQuickSettings.kt @@ -0,0 +1,81 @@ +package com.droiddeck.launcher.ui + +import android.content.Context +import android.content.SharedPreferences +import androidx.compose.foundation.layout.* +import androidx.compose.foundation.BorderStroke +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.ui.Alignment +import androidx.compose.ui.unit.dp +import androidx.compose.ui.platform.testTag +import androidx.compose.material3.* +import androidx.compose.runtime.* +import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.res.stringResource +import com.droiddeck.launcher.R +import com.droiddeck.launcher.input.* + +@Composable +internal fun InputQuickSettings(host: MenuHost, desktop: Boolean, onChanged: (InputRole?) -> Unit = {}, onEdit: () -> Unit = {}, track: (String) -> Modifier = { Modifier }) { + val context = LocalContext.current + var library by remember { mutableStateOf(InputLibraries.read(context)) } + var expanded by remember { mutableStateOf(false) } + var naming by remember { mutableStateOf(false) } + var failure by remember { mutableStateOf(null) } + DisposableEffect(context) { + val preferences = context.getSharedPreferences("input-profiles", Context.MODE_PRIVATE) + val listener = SharedPreferences.OnSharedPreferenceChangeListener { _, key -> if (key == "library") library = InputLibraries.read(context) } + preferences.registerOnSharedPreferenceChangeListener(listener) + onDispose { preferences.unregisterOnSharedPreferenceChangeListener(listener) } + } + fun update(role: InputRole?, change: (InputLibrary) -> InputLibrary) { + runCatching { InputLibraries.update(context, change) }.onSuccess { library = it; onChanged(role) } + .onFailure { failure = it.message ?: context.getString(R.string.input_save_failed) } + } + val selection = library.selection(desktop) + val mixed = selection.parts.isNotEmpty() + val active = library.profile(library.activeId(desktop)) + val profiles = listOf(InputProfiles.AUTO to stringResource(R.string.input_quick_auto, if (desktop) library.profile(library.desktop).name else library.profile(library.steam).name)) + + library.profiles.map { it.id to it.name } + if (mixed) listOf("mixed" to stringResource(R.string.input_quick_mixed, active.name)) else emptyList() + Row(Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) { + Row(Modifier.weight(1f).focusRow { expanded = !expanded }.padding(14.dp).testTag("input-profile-expand"), + verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(10.dp)) { + Text(if (expanded) "▾" else "▸", color = MaterialTheme.colorScheme.onSurface) + Text(stringResource(R.string.input_profile), style = MaterialTheme.typography.titleMedium, color = MaterialTheme.colorScheme.onSurface) + } + Box(Modifier.padding(end = 14.dp).widthIn(max = 280.dp)) { + ChoiceChip(host, "input-profile", stringResource(R.string.input_profile), profiles, if (mixed) "mixed" else selection.profile, + modifier = track("input-profile")) { id -> if (id != "mixed") update(null) { it.selectProfile(desktop, id) } } + } + } + if (expanded) Surface( + modifier = Modifier.fillMaxWidth().padding(start = 18.dp, end = 12.dp, bottom = 14.dp).testTag("input-profile-parts"), + shape = RoundedCornerShape(12.dp), + color = MaterialTheme.colorScheme.primary.copy(alpha = .08f), + border = BorderStroke(1.dp, MaterialTheme.colorScheme.primary.copy(alpha = .35f)), + ) { + Column(Modifier.padding(vertical = 6.dp)) { + for (role in InputRole.entries) { + val options = library.bindings.filter { binding -> + if (role == InputRole.PHYSICAL) binding.physical + else !binding.physical && library.layouts.any { it.id == binding.layoutId && it.supports(role) } + }.map { it.id to it.name } + ChoiceRow(host, "input-part-${role.name}", roleLabel(role), null, options, library.activePart(desktop, role).bindings, + chipModifier = track("input-part-${role.name}")) { id -> update(role) { it.selectPart(desktop, role, id) } } + } + if (mixed) SecondaryButton(stringResource(R.string.input_save_combination)) { naming = true } + ActionRow(stringResource(R.string.input_settings), null, stringResource(R.string.ctrl_edit), onEdit, + modifier = track("input-settings").testTag("input-profile-edit")) + } + } + if (naming) { + var name by remember { mutableStateOf("") } + AlertDialog(onDismissRequest = { naming = false }, title = { Text(stringResource(R.string.input_save_combination)) }, + text = { OutlinedTextField(name, { name = it.take(80) }, singleLine = true, label = { Text(stringResource(R.string.input_name)) }) }, + confirmButton = { PadTextButton(enabled = name.isNotBlank(), onClick = { update(null) { it.saveSelection(desktop, name) }; naming = false }) { Text(stringResource(R.string.gesture_save_changes)) } }, + dismissButton = { PadTextButton(onClick = { naming = false }) { Text(stringResource(R.string.common_cancel)) } }) + } + failure?.let { message -> AlertDialog(onDismissRequest = { failure = null }, title = { Text(stringResource(R.string.input_save_failed)) }, + text = { Text(message) }, confirmButton = { PadTextButton(onClick = { failure = null }) { Text(stringResource(R.string.common_ok)) } }) } +} diff --git a/app/src/main/java/com/droiddeck/launcher/ui/InputSettingsPage.kt b/app/src/main/java/com/droiddeck/launcher/ui/InputSettingsPage.kt new file mode 100644 index 0000000000..812557f368 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/ui/InputSettingsPage.kt @@ -0,0 +1,504 @@ +package com.droiddeck.launcher.ui + +import android.content.Context +import android.net.Uri +import android.provider.OpenableColumns +import android.util.DisplayMetrics +import androidx.activity.compose.BackHandler +import androidx.activity.compose.rememberLauncherForActivityResult +import androidx.activity.result.contract.ActivityResultContracts +import androidx.compose.foundation.focusGroup +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.ui.focus.focusRequester +import androidx.compose.foundation.layout.* +import androidx.compose.foundation.rememberScrollState +import androidx.compose.foundation.verticalScroll +import androidx.compose.material3.* +import androidx.compose.runtime.* +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.platform.testTag +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.semantics.Role +import androidx.compose.ui.unit.dp +import androidx.compose.ui.window.Dialog +import androidx.compose.ui.window.DialogProperties +import com.droiddeck.launcher.R +import com.droiddeck.launcher.input.* +import com.droiddeck.launcher.session.SessionPrefs +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext + +@Composable +fun InputSettingsDialog(onBack: () -> Unit, onChanged: () -> Unit) { + val context = LocalContext.current + val activity = context.findActivity() as? com.droiddeck.launcher.SessionActivity + val target = activity?.inputEditorDisplay(InputRole.SECONDARY) + if (activity != null && target != null && target.displayId != activity.window.decorView.display?.displayId) { + InputDisplayContent(activity, target, onBack) { InputSettingsPane(onBack, onChanged) } + } else InputSettingsWindow(onBack, onChanged) +} + +@Composable +private fun InputSettingsWindow(onBack: () -> Unit, onChanged: () -> Unit) { + Dialog(onDismissRequest = onBack, properties = DialogProperties(usePlatformDefaultWidth = false)) { + InputSettingsPane(onBack, onChanged) + } +} + +@Composable +private fun InputSettingsPane(onBack: () -> Unit, onChanged: () -> Unit) { + var editingSurface by remember { mutableStateOf(false) } + val window = (androidx.compose.ui.platform.LocalView.current.parent as? androidx.compose.ui.window.DialogWindowProvider)?.window + DisposableEffect(window, editingSurface) { + window?.setDimAmount(if (editingSurface) 0f else .32f) + onDispose { } + } + BoxWithConstraints(Modifier.fillMaxSize().systemBarsPadding().padding(8.dp)) { + CompositionLocalProvider(LocalNarrowPane provides (maxWidth < NarrowPaneWidth)) { + Surface(Modifier.fillMaxSize(), color = if (editingSurface) androidx.compose.ui.graphics.Color.Transparent else MaterialTheme.colorScheme.surface) { + InputSettingsPage(onBack, onChanged, onSurfaceEditing = { editingSurface = it }) + } + } + } +} + +private enum class InputPage { HOME, PROFILE, SOURCE, LAYOUTS, REUSE } +private data class InputBindingEdit(val preset: String, val control: InputControl, val element: InputSurfaceElement? = null, val key: PhysicalInputKey? = null, val click: Boolean = false) +private data class InputNameRequest(val title: Int, val initial: String, val sources: List> = emptyList(), + val source: String = "", val offerSharing: Boolean = false, val save: (String, String, Boolean) -> Unit) +private data class InputConfirmation(val title: String, val detail: String, val action: () -> Unit) +private data class InputReuseRequest(val name: String, val layoutOnly: Boolean, val apply: (Boolean) -> Unit) + +@Composable +fun InputSettingsPage(onBack: () -> Unit, onChanged: () -> Unit = {}, embedded: Boolean = false, onSurfaceEditing: (Boolean) -> Unit = {}) { + CompositionLocalProvider(LocalContentColor provides MaterialTheme.colorScheme.onBackground) { + InputSettingsContent(onBack, onChanged, embedded, onSurfaceEditing) + } +} + +@Composable +private fun InputSettingsContent(onBack: () -> Unit, onChanged: () -> Unit, embedded: Boolean, onSurfaceEditing: (Boolean) -> Unit) { + val context = LocalContext.current + val host = rememberMenuHost() + var library by remember { mutableStateOf(InputLibraries.read(context)) } + var page by remember { mutableStateOf(InputPage.HOME) } + var bindingsTab by remember { mutableIntStateOf(0) } + var profileId by remember { mutableStateOf(InputProfile.GAME) } + var role by remember { mutableStateOf(InputRole.PHYSICAL) } + var edit by remember { mutableStateOf(null) } + var naming by remember { mutableStateOf(null) } + var confirmation by remember { mutableStateOf(null) } + var reuse by remember { mutableStateOf(null) } + var capture by remember { mutableStateOf(false) } + var surface by remember { mutableStateOf(false) } + LaunchedEffect(surface) { onSurfaceEditing(surface) } + DisposableEffect(Unit) { onDispose { onSurfaceEditing(false) } } + var managingLayout by remember { mutableStateOf(null) } + var exporting by remember { mutableStateOf(null) } + var error by remember { mutableStateOf(null) } + val scope = rememberCoroutineScope() + val controller = rememberControllerSettings() + var oscMode by remember { mutableStateOf(SessionPrefs.oscMode(context)) } + fun update(change: (InputLibrary) -> InputLibrary): Boolean = runCatching { InputLibraries.update(context, change) } + .onSuccess { library = it; onChanged() }.onFailure { error = it.message ?: context.getString(R.string.input_save_failed) }.isSuccess + fun go(next: InputPage) { page = next; host.open = null } + fun back() { + when { + edit != null -> edit = null + page == InputPage.HOME -> onBack() + page == InputPage.PROFILE -> go(InputPage.HOME) + page == InputPage.SOURCE -> go(InputPage.PROFILE) + else -> go(InputPage.SOURCE) + } + } + BackHandler(enabled = !embedded || page != InputPage.HOME || edit != null, onBack = ::back) + val profile = library.profile(profileId) + val preset = library.preset(profile.binding(role)) + val layout = library.layout(profileId, role) + val importer = rememberLauncherForActivityResult(ActivityResultContracts.OpenDocument()) { uri -> + if (uri == null) return@rememberLauncherForActivityResult + val target = role + scope.launch { + val raw = withContext(Dispatchers.IO) { readLayoutFile(context, uri) } + val metrics = inputDisplayMetrics(context, target) + val imported = raw?.let { InputLayoutFile.import(context, it, layoutFileName(context, uri) ?: context.getString(R.string.ctrl_layout), + if (target == InputRole.PRIMARY) InputLayoutScreen.MAIN else InputLayoutScreen.SECONDARY, layout?.style ?: InputSurfaceStyle(), + metrics.widthPixels, metrics.heightPixels, metrics.density) } + if (imported == null) error = context.getString(R.string.input_layout_import_failed) + else if (update { it.withLayout(imported.first).withPreset(imported.second).chooseLayout(profileId, target, imported.first.id) }) go(InputPage.SOURCE) + } + } + val exporter = rememberLauncherForActivityResult(ActivityResultContracts.CreateDocument("application/json")) { uri -> + val text = exporting + exporting = null + if (uri != null && text != null) scope.launch { + val saved = withContext(Dispatchers.IO) { runCatching { context.contentResolver.openOutputStream(uri, "wt")?.use { it.write(text.toByteArray()) } != null }.getOrDefault(false) } + if (!saved) error = context.getString(R.string.input_save_failed) + } + } + fun rename(title: Int, name: String, save: (String) -> Unit) { + naming = InputNameRequest(title, name) { value, _, _ -> save(value) } + } + fun newProfile(copy: Boolean) { + naming = InputNameRequest(if (copy) R.string.input_copy_profile else R.string.input_new_profile, + if (copy) profile.name else "", library.profiles.map { it.id to it.name }, profileId, offerSharing = true) { name, source, shared -> + val id = newInputId() + if (update { it.duplicateProfile(source, name, id, sharePhysical = shared) }) { profileId = id; go(InputPage.PROFILE) } + } + } + fun editPart(nextRole: InputRole) { role = nextRole; bindingsTab = 0; go(InputPage.SOURCE) } + val pageFocus = remember { androidx.compose.ui.focus.FocusRequester() } + val inputMode = androidx.compose.ui.platform.LocalInputModeManager.current + var settled by remember { mutableStateOf(false) } + LaunchedEffect(page, role, profileId, edit != null, surface) { + if (!settled) { settled = true; return@LaunchedEffect } + if (surface || inputMode.inputMode != androidx.compose.ui.input.InputMode.Keyboard) return@LaunchedEffect + repeat(2) { withFrameNanos { } } + runCatching { pageFocus.requestFocus() } + } + val editing = edit + Box(Modifier.focusRequester(pageFocus).focusGroup()) { + if (editing != null) { + val b = library.preset(editing.preset) + var elementProperties by remember(editing) { mutableStateOf(editing.element) } + val current = editing.element?.let { if (editing.click) b.clickAction(it) else b.action(it) } + ?: editing.key?.let { k -> b.keys.firstOrNull { it.key == k }?.action ?: InputAction.Disabled } + ?: b.controls[editing.control] ?: InputAction.Native() + InputActionEditor(editing.control, current, if (editing.click) stringResource(R.string.input_click_name, editing.element!!.name) + else editing.element?.name ?: editing.key?.label ?: editing.control.label, + properties = { action -> if (!editing.click) elementProperties?.let { e -> InputElementProperties(e, action) { elementProperties = it } } }, + onBack = { edit = null }, element = editing.element, nativeTargets = editing.key == null, click = editing.click, + detail = if (library.usersOfBindings(b.id).size > 1) stringResource(R.string.input_shared_detail, b.name, library.usersOfBindings(b.id).size) + else "${profile.name} · ${roleLabel(role)}", onSave = { action -> + if (update { l -> + val geometry = if (!editing.click && elementProperties != null && layout != null) l.withLayout(layout.copy(elements = layout.elements.map { + if (it.id == elementProperties!!.id) elementProperties!!.copy(name = elementProperties!!.name.trim().ifBlank { it.name }) else it + })) else l + geometry.withPreset(when { + editing.element != null && editing.click -> b.copy(clicks = b.clicks + (editing.element.id to action)) + editing.element != null -> b.copy(elements = b.elements + (editing.element.id to action)) + editing.key != null -> b.copy(keys = b.keys.filterNot { it.key == editing.key } + PhysicalInputBinding(editing.key, action)) + else -> b.copy(controls = b.controls + (editing.control to action)) + }) }) edit = null + }) + } else if (!surface) key(page, profileId, role) { + SettingsPage(host, title = when (page) { + InputPage.HOME -> if (embedded) null else stringResource(R.string.setup_controller) + InputPage.PROFILE -> profile.name + InputPage.SOURCE -> roleLabel(role) + InputPage.LAYOUTS -> stringResource(R.string.input_saved_layouts) + InputPage.REUSE -> stringResource(R.string.input_use_settings_from) + }, eyebrow = when (page) { + InputPage.SOURCE -> profile.name + InputPage.LAYOUTS, InputPage.REUSE -> "${profile.name} · ${roleLabel(role)}" + else -> null + }, + onBack = ::back, showBack = !(embedded && page == InputPage.HOME), compactLayout = true) { + when (page) { + InputPage.HOME -> { + SettingsGroup(stringResource(R.string.input_default_profiles)) { + ChoiceRow(host, "auto-desktop", stringResource(R.string.input_desktop_mode), null, + library.profiles.map { it.id to it.name }, library.desktop) { id -> update { it.copy(desktop = id) } } + ChoiceRow(host, "auto-game", stringResource(R.string.input_game_mode), null, + library.profiles.map { it.id to it.name }, library.steam) { id -> update { it.copy(steam = id) } } + for (desktop in listOf(true, false)) { + val selection = library.selection(desktop) + if (selection.profile != InputProfiles.AUTO || selection.parts.isNotEmpty()) InputHint(stringResource(R.string.input_quick_override_hint, + stringResource(if (desktop) R.string.input_desktop_mode else R.string.input_game_mode))) + } + } + if (InputLibraries.hasPendingScreenKeyMigration(context)) InputHint(stringResource(R.string.input_import_failed)) + if (InputLibraries.hasLibraryRecovery(context)) InputHint(stringResource(R.string.input_library_recovered)) + if (library.profiles.any { it.id.startsWith("migrated.screenkeys.") }) Text(stringResource(R.string.input_imported_keys_hint), + Modifier.padding(16.dp), style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant) + SettingsGroup(stringResource(R.string.input_profiles)) { + for (p in library.profiles) InputNavigationRow(p.name, when { + p.id == library.steam && p.id == library.desktop -> stringResource(R.string.input_default_both) + p.id == library.steam -> stringResource(R.string.input_default_game) + p.id == library.desktop -> stringResource(R.string.input_default_desktop) + else -> null + }, Modifier.testTag("input-profile-${p.id}")) { profileId = p.id; go(InputPage.PROFILE) } + PadTextButton(onClick = { newProfile(false) }, modifier = Modifier.padding(8.dp)) { Text(stringResource(R.string.input_new_profile)) } + } + SettingsGroup(stringResource(R.string.input_device_settings)) { + ToggleRow(host, "controller-rumble", stringResource(R.string.ctrl_rumble), null, controller.rumble) { ControllerPrefs.setRumble(context, it); onChanged() } + ChoiceRow(host, "controller-dead-zone", stringResource(R.string.ctrl_dead_zone), null, ControllerPrefs.deadZones.map { it to stringResource(R.string.ctrl_percent, it) }, controller.deadZone) { ControllerPrefs.setDeadZone(context, it); onChanged() } + ToggleRow(host, "controller-joycon-vr", stringResource(R.string.ctrl_joycon_vr), stringResource(R.string.ctrl_joycon_vr_hint), controller.joyConVr) { ControllerPrefs.setJoyConVr(context, it); onChanged() } + } + SettingsGroup(stringResource(R.string.shortcuts_title)) { PhysicalShortcutRows() } + SettingsGroup(stringResource(R.string.input_latency)) { + var measuring by remember { mutableStateOf(InputTiming.enabled) } + var timing by remember { mutableStateOf(InputTiming.report()) } + InputHint(stringResource(R.string.input_latency_hint)) + ToggleRow(host, "input-measure", stringResource(R.string.input_measure), null, measuring) { measuring = it; InputTiming.enabled = it; if (it) InputTiming.reset() } + var reading by remember { mutableStateOf(false) } + ActionRow(stringResource(R.string.input_measurements), stringResource(R.string.input_measure_instructions), + stringResource(R.string.input_read_timing), { timing = InputTiming.report(); reading = true }) + if (reading) AlertDialog(onDismissRequest = { reading = false }, title = { Text(stringResource(R.string.input_measurements)) }, + text = { Column(verticalArrangement = Arrangement.spacedBy(12.dp)) { + Text(timing) + Text(stringResource(R.string.input_measure_instructions), style = MaterialTheme.typography.bodySmall) + } }, confirmButton = { PadTextButton(onClick = { reading = false }) { Text(stringResource(R.string.common_close)) } }) + } + } + InputPage.PROFILE -> { + SettingsGroup(stringResource(R.string.input_profile_controls)) { + for (r in InputRole.entries) InputNavigationRow(roleLabel(r), + if (r == InputRole.PHYSICAL) stringResource(R.string.input_physical_hint) else library.layout(profileId, r)?.name, + Modifier.testTag("input-part-${r.name.lowercase()}"), leading = { + library.layout(profileId, r)?.let { InputLayoutThumbnail(it, r) } + }) { editPart(r) } + } + Spacer(Modifier.height(16.dp)) + Actions { + SecondaryButton(stringResource(R.string.input_rename)) { rename(R.string.input_rename, profile.name) { name -> update { it.withProfile(profile.copy(name = name)) } } } + SecondaryButton(stringResource(R.string.input_copy_profile)) { newProfile(true) } + SecondaryButton(stringResource(R.string.input_reset_actions)) { + confirmation = InputConfirmation(context.getString(R.string.input_reset_actions), context.getString(R.string.input_reset_profile_detail, profile.name)) { update { it.resetProfile(profileId) } } + } + if (profileId !in listOf(InputProfile.GAME, InputProfile.DESKTOP)) SecondaryButton(stringResource(R.string.input_delete)) { + confirmation = InputConfirmation(context.getString(R.string.input_delete), context.getString(R.string.input_delete_profile_detail, profile.name)) { + if (update { it.removeProfile(profileId) }) { profileId = InputProfile.GAME; go(InputPage.HOME) } + } + } + } + } + InputPage.SOURCE -> { + if (role == InputRole.PRIMARY) SettingsGroup(stringResource(R.string.input_screen_visibility)) { + ChoiceRow(host, "controller-osc", stringResource(R.string.mode_osc), stringResource(R.string.input_visibility_shared), + listOf(SessionPrefs.OSC_AUTO to stringResource(R.string.common_auto), SessionPrefs.OSC_ALWAYS to stringResource(R.string.common_always), + SessionPrefs.OSC_STEAM_TOUCH to stringResource(R.string.osc_steam_touch), SessionPrefs.OSC_STEAM_QAM to stringResource(R.string.mode_osc_qam), SessionPrefs.OSC_NEVER to stringResource(R.string.common_never)), + oscMode) { SessionPrefs.setOscMode(context, it); oscMode = it; onChanged() } + } + if (layout != null) SettingsGroup(stringResource(R.string.ctrl_layout)) { + if (layout.editable) InputNavigationRow(stringResource(R.string.input_edit_layout), layout.name, Modifier.testTag("input-surface-${role.name.lowercase()}")) { surface = true } + else InputHint(layout.name) + InputNavigationRow(stringResource(R.string.input_saved_layouts), stringResource(R.string.input_saved_layouts_hint)) { go(InputPage.LAYOUTS) } + if (layout.editable) { + InputColorRow(host, layout.style.tint) { color -> update { it.withLayout(layout.copy(style = layout.style.copy(tint = color))) } } + SliderRow(stringResource(R.string.ctrl_opacity), null, layout.style.opacity, 1..100, step = 1, + format = { "$it%" }) { opacity -> update { it.withLayout(layout.copy(style = layout.style.copy(opacity = opacity))) } } + for (button in layout.quickButtons) ToggleRow(host, "quick-${button.name.lowercase()}", stringResource(quickButtonLabel(button)), null, + button !in layout.style.hidden) { on -> update { it.withLayout(layout.withQuickButton(button, on)) } } + } + if (role == InputRole.SECONDARY) InputHint(stringResource(R.string.input_second_screen_enable)) + } + SettingsGroup(stringResource(R.string.input_assign_actions)) { + val users = library.usersOfBindings(preset.id) + if (users.size > 1) InputHint(stringResource(R.string.input_shared_with, users.filterNot { it.id == profileId }.joinToString { it.name })) + val elements = layout?.takeIf { it.editable }?.elements + if (elements != null) { + if (elements.isEmpty()) InputHint(stringResource(R.string.input_empty_elements)) + for (e in elements) { + InputNavigationRow(e.name, actionLabel(preset.action(e)), Modifier.testTag("input-element-${e.id}")) { edit = InputBindingEdit(preset.id, e.control, e) } + if (e.hasClick) InputNavigationRow(stringResource(R.string.input_click_name, e.name), actionLabel(preset.clickAction(e)), Modifier.testTag("input-click-${e.id}")) { edit = InputBindingEdit(preset.id, InputControl.A, e, click = true) } + } + } else { + Box(Modifier.padding(horizontal = 12.dp, vertical = 12.dp)) { + SheetChips(listOf(stringResource(R.string.input_buttons), stringResource(R.string.input_axes)) + + if (preset.physical) listOf(stringResource(R.string.input_extra_keys)) else emptyList(), bindingsTab) { bindingsTab = it } + } + val controls = InputControl.padControls.asList() + if (bindingsTab != 2) InputControlGrid(preset, controls, bindingsTab == 1) { edit = InputBindingEdit(preset.id, it) } + else { + for (b in preset.keys) { + InputNavigationRow(b.key.label, actionLabel(b.action)) { edit = InputBindingEdit(preset.id, InputControl.A, key = b.key) } + PadTextButton(onClick = { update { it.withPreset(preset.copy(keys = preset.keys - b)) } }) { Text(stringResource(R.string.input_remove_binding)) } + } + InputNavigationRow(stringResource(R.string.input_capture), stringResource(R.string.input_capture_hint)) { capture = true } + } + } + } + SettingsGroup(stringResource(R.string.input_reuse)) { + InputNavigationRow(stringResource(R.string.input_use_settings_from), stringResource(R.string.input_reuse_hint)) { go(InputPage.REUSE) } + if (library.usersOfBindings(preset.id).size > 1 || layout?.let { it.editable && library.usersOfLayout(it.id).size > 1 } == true) + InputNavigationRow(stringResource(R.string.input_make_independent), stringResource(R.string.input_independent_hint), Modifier.testTag("input-make-independent")) { + update { it.usePart(profileId, role, profileId, shared = false) } + } + } + } + InputPage.LAYOUTS -> { + InputHint(stringResource(R.string.input_layout_geometry_hint)) + SettingsGroup(stringResource(R.string.input_saved_layouts)) { + var showOther by remember { mutableStateOf(false) } + val available = library.layouts.filter { it.supports(role) } + val other = available.filter { saved -> !saved.id.startsWith("builtin.") && saved.id != layout?.id && + library.usersOfLayout(saved.id).isEmpty() && library.bindings.none { it.layoutId == saved.id && library.selectedBindings(it.id) } } + for (saved in available.filter { showOther || it !in other }) { + InputLayoutRow(saved, role, if (saved.id == layout?.id) stringResource(R.string.input_current_layout) else null, + onManage = { managingLayout = saved }) { + reuse = InputReuseRequest(saved.name, layoutOnly = true) { shared -> if (update { it.useLayout(profileId, role, saved.id, shared) }) go(InputPage.SOURCE) } + } + } + if (other.isNotEmpty()) PadTextButton(onClick = { showOther = !showOther }, modifier = Modifier.padding(8.dp)) { + Text(stringResource(if (showOther) R.string.input_hide_other_layouts else R.string.input_other_layouts, other.size)) + } + Row { + PadTextButton(onClick = { naming = InputNameRequest(R.string.input_new_layout, "") { name, _, _ -> + val created = SavedInputLayout(newInputId(), name, screen = if (role == InputRole.PRIMARY) InputLayoutScreen.MAIN else InputLayoutScreen.SECONDARY) + if (update { it.withLayout(created).useLayout(profileId, role, created.id, shared = true) }) { go(InputPage.SOURCE); surface = true } + } }, modifier = Modifier.padding(8.dp)) { Text(stringResource(R.string.input_new_layout)) } + PadTextButton(onClick = { importer.launch(arrayOf("application/json", "text/plain", "application/octet-stream", "*/*")) }, + modifier = Modifier.padding(8.dp).testTag("input-layout-import")) { Text(stringResource(R.string.boot_movie_import)) } + } + } + + } + InputPage.REUSE -> { + InputHint(stringResource(R.string.input_reuse_explanation, roleLabel(role))) + SettingsGroup(stringResource(R.string.input_profiles)) { + for (p in library.profiles.filterNot { it.id == profileId }) InputNavigationRow(p.name, library.layout(p.id, role)?.name) { + reuse = InputReuseRequest(p.name, layoutOnly = false) { shared -> if (update { it.usePart(profileId, role, p.id, shared) }) go(InputPage.SOURCE) } + } + } + } + + } + } + } + } + managingLayout?.let { saved -> AlertDialog(onDismissRequest = { managingLayout = null }, title = { Text(saved.name) }, + text = { Column(verticalArrangement = Arrangement.spacedBy(12.dp)) { + Text(stringResource(R.string.input_layout_count, saved.elements.size)) + SecondaryButton(stringResource(R.string.input_rename)) { + managingLayout = null + rename(R.string.input_rename_layout, saved.name) { name -> update { it.withLayout(saved.copy(name = name)) } } + } + val exported = remember(saved, library) { + val metrics = inputDisplayMetrics(context, if (saved.screen == InputLayoutScreen.SECONDARY) InputRole.SECONDARY else InputRole.PRIMARY) + InputLayoutFile.export(saved, library.bindings.firstOrNull { it.id == preset.id && it.layoutId == saved.id } + ?: library.bindings.firstOrNull { it.layoutId == saved.id }, metrics.widthPixels, metrics.heightPixels) + } + if (exported != null) SecondaryButton(stringResource(R.string.screen_key_layout_export)) { + managingLayout = null + exporting = exported + exporter.launch(InputLayoutFile.fileName(saved)) + } + val used = library.usersOfLayout(saved.id).isNotEmpty() || library.bindings.any { it.layoutId == saved.id && library.selectedBindings(it.id) } + if (!used && saved.editable) SecondaryButton(stringResource(R.string.input_delete)) { + managingLayout = null + confirmation = InputConfirmation(context.getString(R.string.input_delete), context.getString(R.string.input_delete_layout_detail, saved.name)) { update { it.removeLayout(saved.id) } } + } + } }, confirmButton = { PadTextButton(onClick = { managingLayout = null }) { Text(stringResource(R.string.common_close)) } }) } + if (capture) InputCaptureDialog(onDismiss = { capture = false }) { capture = false; edit = InputBindingEdit(preset.id, InputControl.A, key = it) } + if (surface) InputSurfaceDialog(profileId, onDismiss = { surface = false; library = InputLibraries.read(context) }, onChanged = { + library = InputLibraries.read(context); onChanged() + }, role = role, layoutId = layout?.id, bindingsId = preset.id) + naming?.let { request -> key(request) { InputNameDialog(request) { naming = null } } } + reuse?.let { request -> key(request) { InputReuseDialog(request) { reuse = null } } } + confirmation?.let { request -> AlertDialog(onDismissRequest = { confirmation = null }, title = { Text(request.title) }, + text = { Text(request.detail) }, confirmButton = { PadTextButton(onClick = { confirmation = null; request.action() }) { Text(stringResource(R.string.common_ok)) } }, + dismissButton = { PadTextButton(onClick = { confirmation = null }) { Text(stringResource(R.string.common_cancel)) } }) } + error?.let { message -> AlertDialog(onDismissRequest = { error = null }, title = { Text(stringResource(R.string.input_save_failed)) }, + text = { Text(message) }, confirmButton = { PadTextButton(onClick = { error = null }) { Text(stringResource(R.string.common_ok)) } }) } +} + +@Composable +private fun InputNameDialog(request: InputNameRequest, onDismiss: () -> Unit) { + var name by remember { mutableStateOf(request.initial) } + var source by remember { mutableStateOf(request.source) } + var shared by remember { mutableStateOf(false) } + val host = rememberMenuHost() + AlertDialog(onDismissRequest = onDismiss, title = { Text(stringResource(request.title)) }, text = { Column(Modifier.verticalScroll(rememberScrollState())) { + OutlinedTextField(name, { name = it.take(80) }, label = { Text(stringResource(R.string.input_name)) }, singleLine = true, modifier = Modifier.testTag("input-name")) + if (request.sources.isNotEmpty()) ChoiceRow(host, "input-copy-source", stringResource(R.string.input_start_from), null, request.sources, source) { source = it } + if (request.offerSharing) { + Row(Modifier.fillMaxWidth().focusRow(role = Role.Checkbox, checked = shared) { shared = !shared }.testTag("input-share-physical"), verticalAlignment = Alignment.CenterVertically) { + Checkbox(shared, null) + Text(stringResource(R.string.input_share_physical)) + } + Text(stringResource(R.string.input_new_independent_hint), style = MaterialTheme.typography.bodySmall) + } + } }, confirmButton = { PadTextButton(enabled = name.isNotBlank(), onClick = { request.save(name.trim(), source, shared); onDismiss() }) { Text(stringResource(R.string.gesture_save_changes)) } }, + dismissButton = { PadTextButton(onClick = onDismiss) { Text(stringResource(R.string.common_cancel)) } }) +} + +@Composable +private fun InputReuseDialog(request: InputReuseRequest, onDismiss: () -> Unit) { + var shared by remember { mutableStateOf(false) } + AlertDialog(onDismissRequest = onDismiss, title = { Text(request.name) }, text = { Column(Modifier.verticalScroll(rememberScrollState())) { + Text(stringResource(if (request.layoutOnly) R.string.input_reuse_layout_detail else R.string.input_reuse_part_detail)) + for (link in listOf(false, true)) Row(Modifier.fillMaxWidth().focusRow(role = Role.RadioButton, checked = shared == link) { shared = link }, verticalAlignment = Alignment.CenterVertically) { + RadioButton(shared == link, null) + Text(stringResource(if (link) R.string.input_keep_shared else R.string.input_independent_copy)) + } + } }, confirmButton = { PadTextButton(onClick = { request.apply(shared); onDismiss() }) { Text(stringResource(R.string.common_ok)) } }, + dismissButton = { PadTextButton(onClick = onDismiss) { Text(stringResource(R.string.common_cancel)) } }) +} + +@Composable +private fun InputControlGrid(preset: SavedInputBindings, available: List, axes: Boolean, onPick: (InputControl) -> Unit) { + val controls = available.filter { (it.dimensions == 2 || it == InputControl.LT || it == InputControl.RT) == axes } + BoxWithConstraints(Modifier.fillMaxWidth().padding(12.dp)) { + val columns = if (maxWidth < 480.dp) 2 else 4 + Column(verticalArrangement = Arrangement.spacedBy(8.dp)) { + for (row in controls.chunked(columns)) Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) { + for (control in row) OutlinedCard(onClick = { onPick(control) }, modifier = Modifier.weight(1f).controllerConfirm { onPick(control) }.testTag("input-binding-${control.id}")) { + Column(Modifier.padding(12.dp).heightIn(min = 48.dp)) { + Text(control.label, style = MaterialTheme.typography.titleMedium) + Text(actionLabel(preset.controls[control] ?: InputAction.Native()), style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant) + } + } + repeat(columns - row.size) { Spacer(Modifier.weight(1f)) } + } + } + } +} + +@Composable +private fun InputNavigationRow(title: String, detail: String?, modifier: Modifier = Modifier, leading: (@Composable () -> Unit)? = null, onClick: () -> Unit) { + Row(modifier.fillMaxWidth().focusRow(RoundedCornerShape(0.dp), onClick = onClick).heightIn(min = 64.dp).padding(14.dp), + verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(12.dp)) { + leading?.invoke() + Column(Modifier.weight(1f)) { + Text(title, style = MaterialTheme.typography.titleMedium) + if (detail != null) Text(detail, style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant) + } + Text("›", style = MaterialTheme.typography.headlineSmall, color = MaterialTheme.colorScheme.onSurfaceVariant) + } +} + +internal fun quickButtonLabel(button: InputCommand) = when (button) { + InputCommand.STEAM -> R.string.ctrl_steam_button + InputCommand.QUICK_ACCESS -> R.string.ctrl_qam_button + else -> R.string.ctrl_keyboard_button +} + +@Suppress("DEPRECATION") +internal fun inputDisplayMetrics(context: Context, role: InputRole): DisplayMetrics = DisplayMetrics().also { metrics -> + (context.findActivity() as? com.droiddeck.launcher.SessionActivity)?.inputEditorDisplay(role)?.getRealMetrics(metrics) + ?: metrics.setTo(context.resources.displayMetrics) +} + +private fun readLayoutFile(context: Context, uri: Uri): String? = runCatching { + context.contentResolver.openInputStream(uri)?.use { input -> + val out = java.io.ByteArrayOutputStream() + val buffer = ByteArray(8192) + while (true) { + val read = input.read(buffer) + if (read < 0) break + out.write(buffer, 0, read) + if (out.size() > 1_000_000) return@use null + } + out.toString(Charsets.UTF_8.name()) + } +}.getOrNull() + +private fun layoutFileName(context: Context, uri: Uri): String? = runCatching { + context.contentResolver.query(uri, arrayOf(OpenableColumns.DISPLAY_NAME), null, null, null)?.use { cursor -> + if (cursor.moveToFirst()) cursor.getString(0)?.substringBeforeLast('.')?.trim()?.takeIf { it.isNotEmpty() } else null + } +}.getOrNull() + +@Composable +internal fun roleLabel(role: InputRole) = stringResource(when (role) { + InputRole.PHYSICAL -> R.string.input_physical; InputRole.PRIMARY -> R.string.input_primary; InputRole.SECONDARY -> R.string.input_secondary +}) + +@Composable +private fun InputHint(text: String) { + Text(text, Modifier.padding(14.dp), style = MaterialTheme.typography.bodyMedium, color = MaterialTheme.colorScheme.onSurfaceVariant) +} diff --git a/app/src/main/java/com/droiddeck/launcher/ui/InputSurfaceDialog.kt b/app/src/main/java/com/droiddeck/launcher/ui/InputSurfaceDialog.kt new file mode 100644 index 0000000000..ddf5838be8 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/ui/InputSurfaceDialog.kt @@ -0,0 +1,383 @@ +package com.droiddeck.launcher.ui + +import android.app.Presentation +import android.hardware.display.DisplayManager +import android.os.Bundle +import android.os.Handler +import android.os.Looper +import android.view.Display +import android.view.View +import android.view.Window +import android.view.WindowManager +import androidx.activity.ComponentActivity +import androidx.activity.compose.BackHandler +import androidx.activity.setViewTreeOnBackPressedDispatcherOwner +import androidx.compose.animation.core.animateDpAsState +import androidx.compose.ui.draw.clipToBounds +import androidx.compose.animation.AnimatedVisibility +import androidx.compose.animation.expandVertically +import androidx.compose.animation.shrinkVertically +import androidx.compose.animation.fadeIn +import androidx.compose.animation.fadeOut +import androidx.compose.foundation.clickable +import androidx.compose.foundation.gestures.detectDragGestures +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.foundation.verticalScroll +import androidx.compose.foundation.background +import androidx.compose.foundation.horizontalScroll +import androidx.compose.foundation.layout.* +import androidx.compose.foundation.rememberScrollState +import androidx.compose.material3.* +import androidx.compose.runtime.* +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.geometry.Offset +import androidx.compose.ui.input.pointer.pointerInput +import androidx.compose.ui.layout.onSizeChanged +import androidx.compose.ui.platform.LocalDensity +import androidx.compose.ui.unit.IntOffset +import androidx.compose.ui.unit.IntSize +import kotlin.math.roundToInt +import androidx.compose.ui.platform.ComposeView +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.platform.LocalView +import androidx.compose.ui.platform.testTag +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.unit.dp +import androidx.compose.ui.viewinterop.AndroidView +import androidx.compose.ui.window.Dialog +import androidx.compose.ui.window.DialogProperties +import androidx.compose.ui.window.DialogWindowProvider +import androidx.lifecycle.Lifecycle +import androidx.lifecycle.LifecycleEventObserver +import androidx.lifecycle.setViewTreeLifecycleOwner +import androidx.lifecycle.setViewTreeViewModelStoreOwner +import androidx.savedstate.setViewTreeSavedStateRegistryOwner +import com.droiddeck.launcher.R +import com.droiddeck.launcher.SessionActivity +import com.droiddeck.launcher.input.* +import com.droiddeck.launcher.session.SessionState + +private data class SurfaceSnapshot(val elements: List, val actions: Map, + val clicks: Map, val scale: Float) + +private class GamepadScaler(private val context: android.content.Context, private val width: Int, private val height: Int) { + private fun generator(scale: Float) = LegacyGamepadLayout(context, width, height, scale = scale) + val ids = generator(1f).elements().map { it.id }.toSet() + fun rescale(stored: List, from: Float, to: Float) = generator(to).rescale(stored, generator(from)) + fun reset(stored: List) = generator(1f).regenerate(stored).associateBy { it.id }.let { placed -> stored.map { placed[it.id] ?: it } } +} + +private class SurfaceDraft(val layout: SavedInputLayout, val preset: SavedInputBindings, val defaults: List? = null, + private val gamepad: GamepadScaler? = null, snap: Boolean = true) { + var elements by mutableStateOf(layout.elements) + var actions by mutableStateOf(preset.elements) + var clicks by mutableStateOf(preset.clicks) + var selected by mutableStateOf(null) + var toolsExpanded by mutableStateOf(false) + var toolsX by mutableFloatStateOf(.02f) + var toolsY by mutableFloatStateOf(.02f) + var snap by mutableStateOf(snap) + var adding by mutableStateOf(false) + var properties by mutableStateOf(false) + var clickProperties by mutableStateOf(false) + var history by mutableStateOf>(emptyList()) + var layoutScale by mutableFloatStateOf(layout.scale) + private var scaleBase by mutableStateOf, List, Float>?>(null) + private var scaleValue by mutableIntStateOf(100) + private val rescales get() = gamepad != null && elements.any { it.id in gamepad.ids } + val scale get() = when { + scaleBase?.second == elements -> scaleValue + rescales -> (layoutScale * 100).roundToInt() + else -> 100 + } + val positioned get() = (if (rescales) gamepad!!.reset(elements) else defaults?.let { elements.withPositionsOf(it) }) + ?.takeIf { it != elements || rescales && layoutScale != 1f } + val element get() = elements.firstOrNull { it.id == selected } + val dirty get() = elements != layout.elements || actions != preset.elements || clicks != preset.clicks || layoutScale != layout.scale + fun checkpoint() { history = (history + SurfaceSnapshot(elements, actions, clicks, layoutScale)).takeLast(40) } + fun undo() { history.lastOrNull()?.let { elements = it.elements; actions = it.actions; clicks = it.clicks; layoutScale = it.scale; history = history.dropLast(1) } } + fun scaleAll(percent: Int) { + val base = scaleBase?.takeIf { it.second == elements } ?: Triple(elements, elements, if (rescales) layoutScale else 0f).also { checkpoint() } + val factor = percent / 100f + elements = if (base.third > 0f && gamepad != null) { + layoutScale = factor + val others = base.first.filterNot { it.id in gamepad.ids }.scaled(factor / base.third).associateBy { it.id } + gamepad.rescale(base.first, base.third, factor).map { others[it.id] ?: it } + } else base.first.scaled(factor) + scaleValue = percent; scaleBase = Triple(base.first, elements, base.third) + } + fun resetPositions() { positioned?.let { checkpoint(); elements = it; if (rescales) layoutScale = 1f } } + fun action(e: InputSurfaceElement) = preset.copy(elements = actions).action(e) + fun clickAction(e: InputSurfaceElement) = preset.copy(elements = actions, clicks = clicks).clickAction(e) +} + +@Suppress("DEPRECATION") +private fun displayAspect(display: Display): Float? = android.util.DisplayMetrics().also(display::getRealMetrics) + .takeIf { it.widthPixels > 0 && it.heightPixels > 0 }?.let { (it.widthPixels.toFloat() / it.heightPixels).coerceIn(.05f, 20f) } + +@Composable +fun InputSurfaceDialog(profile: String, onDismiss: () -> Unit, onChanged: () -> Unit, + role: InputRole = InputRole.SECONDARY, layoutId: String? = null, bindingsId: String? = null) { + val context = LocalContext.current + val host = LocalInputActivity.current ?: context.findActivity() as? ComponentActivity + val library = remember { InputLibraries.read(context) } + val savedLayout = remember { library.layouts.first { it.id == (layoutId ?: library.profile(profile).parts.getValue(role).layout) } } + val savedBindings = remember { library.preset(bindingsId ?: library.profile(profile).binding(role)) } + val layoutUsers = library.usersOfLayout(savedLayout.id).size + val bindingUsers = library.usersOfBindings(savedBindings.id).size + val draft = remember { + val (display, metrics) = InputLibraries.primaryDisplay(context) + val gamepad = savedLayout.elements.takeIf { e -> e.any { it.id.startsWith("legacy:") } }?.let { + val (w, h) = OnScreenLayout.generatedSize(context, savedLayout, metrics.widthPixels, metrics.heightPixels) ?: (metrics.widthPixels to metrics.heightPixels) + GamepadScaler(display, w, h) + } + SurfaceDraft(savedLayout, savedBindings, InputLibraries.defaultPositions(context, library, savedLayout), gamepad, ControllerPrefs.snap(context)) + } + val manager = remember { context.getSystemService(DisplayManager::class.java) } + var revision by remember { mutableIntStateOf(0) } + var discard by remember { mutableStateOf(false) } + var failure by remember { mutableStateOf(null) } + val target = remember(revision, role) { + if (host is SessionActivity) host.inputEditorDisplay(role) + else if (role == InputRole.PRIMARY) host?.window?.decorView?.display + else { + val displays = SecondScreenDisplays.available(manager, host?.window?.decorView?.display?.displayId ?: Display.DEFAULT_DISPLAY) + manager.getDisplay(displays.firstOrNull { it.id == SessionState.secondScreenDisplay }?.id ?: displays.firstOrNull()?.id ?: -1) + } + } + val remote = target != null && target.displayId != host?.window?.decorView?.display?.displayId + fun leave() { if (draft.dirty) discard = true else onDismiss() } + fun save() { + runCatching { + InputLibraries.update(context) { latest -> + val l = latest.layouts.first { it.id == savedLayout.id } + val p = latest.preset(savedBindings.id) + latest.withLayout(l.copy(elements = draft.elements, aspect = target?.let(::displayAspect) ?: l.aspect, scale = draft.layoutScale)).withPreset(p.copy(elements = draft.actions.filterKeys { id -> draft.elements.any { it.id == id } }, + clicks = draft.clicks.filterKeys { id -> draft.elements.any { it.id == id && it.hasClick } })) + } + }.onSuccess { onChanged(); onDismiss() }.onFailure { failure = it.message ?: context.getString(R.string.input_save_failed) } + } + DisposableEffect(manager) { + val listener = object : DisplayManager.DisplayListener { + override fun onDisplayAdded(id: Int) { revision++ } + override fun onDisplayRemoved(id: Int) { revision++ } + override fun onDisplayChanged(id: Int) = Unit + } + manager.registerDisplayListener(listener, Handler(Looper.getMainLooper())) + onDispose { manager.unregisterDisplayListener(listener) } + } + if (remote && host != null) DisposableEffect(target!!.displayId, host) { + val presentation = SurfaceEditorPresentation(host, target, transparent = host is SessionActivity && role == InputRole.PRIMARY) { + SurfaceCanvas(draft, transparent = host is SessionActivity && role == InputRole.PRIMARY) + } + (host as? SessionActivity)?.inputWindowShown(target.displayId) + runCatching { presentation.show() }.onFailure { failure = it.message } + val observer = LifecycleEventObserver { _, event -> + if (event == Lifecycle.Event.ON_STOP && !host.screenIsOff()) presentation.hide() + if (event == Lifecycle.Event.ON_START) runCatching { presentation.show() } + } + host.lifecycle.addObserver(observer) + onDispose { + host.lifecycle.removeObserver(observer) + presentation.dismiss() + (host as? SessionActivity)?.inputWindowClosed(target.displayId) + } + } + // Compose 1.6 custom dialog sizing subtracts system bars even when they are hidden. + Dialog(onDismissRequest = ::leave, properties = DialogProperties(usePlatformDefaultWidth = true, decorFitsSystemWindows = false)) { + val editorWindow = (LocalView.current.parent as? DialogWindowProvider)?.window + DisposableEffect(editorWindow) { + editorWindow?.prepareInputEditor() + if (host is SessionActivity && role == InputRole.PRIMARY) editorWindow?.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND) + onDispose { } + } + Surface(Modifier.fillMaxSize(), color = if (host is SessionActivity && role == InputRole.PRIMARY && !draft.properties && !remote) + androidx.compose.ui.graphics.Color.Transparent else MaterialTheme.colorScheme.surface) { + val element = draft.element + BackHandler { if (draft.properties) draft.properties = false else leave() } + when { + draft.properties && element != null -> { + var properties by remember(element.id) { mutableStateOf(element) } + InputActionEditor(if (draft.clickProperties) InputControl.A else element.control, + if (draft.clickProperties) draft.clickAction(element) else draft.action(element), + if (draft.clickProperties) stringResource(R.string.input_click_name, element.name) else element.name, + onBack = { draft.properties = false }, element = element, click = draft.clickProperties, + properties = { action -> + if (!draft.clickProperties) InputElementProperties(properties, action) { properties = it } + if (layoutUsers > 1) Text(stringResource(R.string.input_editor_shared, layoutUsers), + style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant, + modifier = Modifier.padding(bottom = 12.dp)) + }, detail = if (bindingUsers > 1) stringResource(R.string.input_shared_detail, savedBindings.name, bindingUsers) + else "${library.profile(profile).name} · ${roleLabel(role)}", onSave = { action -> + draft.checkpoint() + if (draft.clickProperties) draft.clicks = draft.clicks + (element.id to action) else draft.actions = draft.actions + (element.id to action) + draft.elements = draft.elements.map { if (it.id == element.id) properties.copy(name = properties.name.trim().ifBlank { element.name }) else it } + draft.properties = false + }) + } + target == null -> SettingsPage(rememberMenuHost(), savedLayout.name, ::leave) { + Text(stringResource(R.string.input_display_unavailable)) + PrimaryButton(stringResource(R.string.input_editor_done)) { save() } + } + remote -> SettingsPage(rememberMenuHost(), stringResource(R.string.input_editor_remote), ::leave, + lede = if (layoutUsers > 1) stringResource(R.string.input_editor_shared, layoutUsers) else savedLayout.name) { + SurfaceToolbar(draft, ::save, ::leave) + SurfaceSelection(draft) + } + else -> Box(Modifier.fillMaxSize()) { + SurfaceCanvas(draft, transparent = host is SessionActivity && role == InputRole.PRIMARY) + FloatingSurfaceTools(draft, ::save, ::leave) + } + } + } + } + if (draft.adding) AlertDialog(onDismissRequest = { draft.adding = false }, title = { Text(stringResource(R.string.input_add_element)) }, confirmButton = {}, text = { Column { + for (kind in InputElementKind.entries) PadTextButton(onClick = { draft.checkpoint() + val w = if (kind.control.dimensions == 2) .4f else .2f + val h = .3f + val name = context.getString(kind.label()) + " " + (draft.elements.count { it.kind == kind } + 1) + val element = InputSurfaceElement(kind.control, (1f - w) / 2, (1f - h) / 2, w, h, newInputId(), kind, name) + draft.elements = draft.elements + element; draft.selected = element.id + draft.adding = false; draft.toolsExpanded = false }) { Text(stringResource(kind.label())) } + } }) + if (discard) AlertDialog(onDismissRequest = { discard = false }, title = { Text(stringResource(R.string.input_editor_discard)) }, + confirmButton = { PadTextButton(onClick = onDismiss) { Text(stringResource(R.string.common_ok)) } }, + dismissButton = { PadTextButton(onClick = { discard = false }) { Text(stringResource(R.string.common_cancel)) } }) + failure?.let { AlertDialog(onDismissRequest = { failure = null }, title = { Text(stringResource(R.string.input_save_failed)) }, text = { Text(it) }, + confirmButton = { PadTextButton(onClick = { failure = null }) { Text(stringResource(R.string.common_ok)) } }) } +} + +@Composable +private fun SurfaceCanvas(draft: SurfaceDraft, transparent: Boolean = false) { + AndroidView(factory = { InputSurfaceEditorView(it) }, modifier = Modifier.fillMaxSize().testTag("input-layout-canvas"), + update = { view -> + view.setBackgroundColor(if (transparent) android.graphics.Color.TRANSPARENT else android.graphics.Color.rgb(10, 11, 13)) + view.style = draft.layout.style.copy(hidden = emptySet()); view.elements = draft.elements; view.selected = draft.selected; view.snap = draft.snap + view.isEnabled = !draft.properties && !draft.adding + view.onSelect = { if (!draft.properties) draft.selected = it } + view.onBeginEdit = draft::checkpoint + view.onChange = { e -> if (!draft.properties) draft.elements = draft.elements.map { if (it.id == e.id) e else it } } + + }) +} + +@Composable +private fun FloatingSurfaceTools(draft: SurfaceDraft, onSave: () -> Unit, onLeave: () -> Unit) { + BoxWithConstraints(Modifier.fillMaxSize()) { + val density = LocalDensity.current + val screenWidth = with(density) { maxWidth.toPx() } + val screenHeight = with(density) { maxHeight.toPx() } + val margin = with(density) { 8.dp.toPx() } + var panelSize by remember { mutableStateOf(IntSize.Zero) } + val x = (draft.toolsX * screenWidth).coerceIn(margin, (screenWidth - panelSize.width - margin).coerceAtLeast(margin)) + val y = (draft.toolsY * screenHeight).coerceIn(margin, (screenHeight - panelSize.height - margin).coerceAtLeast(margin)) + val position by rememberUpdatedState(Offset(x, y)) + val expandedWidth = minOf(420.dp, maxWidth - 16.dp) + val panelWidth by animateDpAsState(if (draft.toolsExpanded) expandedWidth else 152.dp, label = "input-tools-width") + Surface(Modifier.offset { IntOffset(x.roundToInt(), y.roundToInt()) } + .width(panelWidth).clipToBounds() + .heightIn(max = maxHeight - 16.dp).onSizeChanged { panelSize = it }, + shape = RoundedCornerShape(18.dp), color = MaterialTheme.colorScheme.surface, + tonalElevation = 6.dp, shadowElevation = 10.dp) { + Column { + Row(Modifier.fillMaxWidth().heightIn(min = 52.dp).testTag("input-editor-tools") + .pointerInput(screenWidth, screenHeight, panelSize) { + var dragPosition = Offset.Zero + detectDragGestures(onDragStart = { dragPosition = position }) { change, delta -> + change.consume() + dragPosition = Offset((dragPosition.x + delta.x).coerceIn(margin, (screenWidth - panelSize.width - margin).coerceAtLeast(margin)), + (dragPosition.y + delta.y).coerceIn(margin, (screenHeight - panelSize.height - margin).coerceAtLeast(margin))) + draft.toolsX = dragPosition.x / screenWidth + draft.toolsY = dragPosition.y / screenHeight + } + } + .clickable { draft.toolsExpanded = !draft.toolsExpanded }.padding(horizontal = 16.dp), + verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(12.dp)) { + Text("⠿", color = MaterialTheme.colorScheme.primary) + Text(stringResource(if (draft.toolsExpanded) R.string.input_hide_tools else R.string.input_tools), style = MaterialTheme.typography.titleSmall, maxLines = 1) + } + AnimatedVisibility(draft.toolsExpanded, enter = expandVertically(expandFrom = Alignment.Top) + fadeIn(), + exit = shrinkVertically(shrinkTowards = Alignment.Top) + fadeOut()) { + Column(Modifier.requiredWidth(expandedWidth).verticalScroll(rememberScrollState())) { + HorizontalDivider() + SurfaceToolbar(draft, onSave, onLeave) + HorizontalDivider() + SurfaceSelection(draft) + } + } + } + } + } +} + +@OptIn(ExperimentalLayoutApi::class) +@Composable +private fun SurfaceToolbar(draft: SurfaceDraft, onSave: () -> Unit, onLeave: () -> Unit) { + val context = LocalContext.current + FlowRow(Modifier.fillMaxWidth().padding(8.dp), horizontalArrangement = Arrangement.spacedBy(8.dp), verticalArrangement = Arrangement.spacedBy(4.dp)) { + SecondaryButton(stringResource(R.string.common_cancel), compact = true, onClick = onLeave) + SecondaryButton(stringResource(R.string.input_editor_add), enabled = draft.elements.size < 64, compact = true) { draft.adding = true } + SecondaryButton(stringResource(R.string.input_editor_undo), enabled = draft.history.isNotEmpty(), compact = true) { draft.undo() } + if (draft.defaults != null) SecondaryButton(stringResource(R.string.input_reset_positions), enabled = draft.positioned != null, compact = true) { draft.resetPositions() } + Row(verticalAlignment = Alignment.CenterVertically) { + Checkbox(draft.snap, { draft.snap = it; ControllerPrefs.setSnap(context, it) }) + Text(stringResource(R.string.ctrl_editor_snap)) + } + PrimaryButton(stringResource(R.string.input_editor_done), compact = true, onClick = onSave) + } + if (draft.elements.isNotEmpty()) SliderRow(stringResource(R.string.ctrl_size), null, draft.scale, 80..125, step = 5, + format = { "$it%" }, modifier = Modifier.testTag("input-editor-scale")) { draft.scaleAll(it) } +} + +@OptIn(ExperimentalLayoutApi::class) +@Composable +private fun SurfaceSelection(draft: SurfaceDraft) { + Column(Modifier.padding(12.dp), verticalArrangement = Arrangement.spacedBy(8.dp)) { + draft.element?.let { e -> + Text(e.name, style = MaterialTheme.typography.titleMedium) + FlowRow(horizontalArrangement = Arrangement.spacedBy(8.dp), verticalArrangement = Arrangement.spacedBy(4.dp)) { + PrimaryButton(stringResource(R.string.input_properties), compact = true) { draft.clickProperties = false; draft.properties = true } + if (e.hasClick) SecondaryButton(stringResource(R.string.input_click_properties), compact = true) { draft.clickProperties = true; draft.properties = true } + SecondaryButton(stringResource(R.string.input_remove_element), compact = true) { + draft.checkpoint(); draft.elements = draft.elements.filterNot { it.id == e.id }; draft.actions = draft.actions - e.id; draft.clicks = draft.clicks - e.id; draft.selected = null + } + } + } ?: Text(stringResource(R.string.input_editor_hint)) + } +} + +private fun InputElementKind.label() = when (this) { + InputElementKind.BUTTON -> R.string.input_element_button + InputElementKind.STICK -> R.string.input_element_stick + InputElementKind.TRACKPAD -> R.string.input_element_trackpad + InputElementKind.DPAD -> R.string.input_element_dpad + InputElementKind.TRIGGER -> R.string.input_element_trigger +} + +private class SurfaceEditorPresentation(private val host: ComponentActivity, display: Display, private val transparent: Boolean, private val content: @Composable () -> Unit) : Presentation(host, display) { + private var view: ComposeView? = null + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setCancelable(false) + window?.addFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) + window?.prepareInputEditor() + window?.decorView?.let { + it.setViewTreeLifecycleOwner(host); it.setViewTreeViewModelStoreOwner(host) + it.setViewTreeSavedStateRegistryOwner(host); it.setViewTreeOnBackPressedDispatcherOwner(host) + } + window?.setBackgroundDrawable(android.graphics.drawable.ColorDrawable(android.graphics.Color.TRANSPARENT)) + view = ComposeView(context).also { it.setContent { DroidDeckTheme { + Surface(Modifier.fillMaxSize(), color = if (transparent) androidx.compose.ui.graphics.Color.Transparent else MaterialTheme.colorScheme.surface) { content() } + } }; setContentView(it) } + } + override fun dismiss() { view?.disposeComposition(); view = null; super.dismiss() } +} + +@Suppress("DEPRECATION") +internal fun Window.prepareInputEditor() { + addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON or WindowManager.LayoutParams.FLAG_FULLSCREEN) + setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT) + decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY or View.SYSTEM_UI_FLAG_FULLSCREEN or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or + View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION +} diff --git a/app/src/main/java/com/droiddeck/launcher/ui/InputSurfaceEditorView.kt b/app/src/main/java/com/droiddeck/launcher/ui/InputSurfaceEditorView.kt new file mode 100644 index 0000000000..d03eaa6e9f --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/ui/InputSurfaceEditorView.kt @@ -0,0 +1,107 @@ +package com.droiddeck.launcher.ui + +import android.annotation.SuppressLint +import android.content.Context +import android.graphics.Canvas +import android.graphics.Color +import android.graphics.Paint +import android.view.HapticFeedbackConstants +import android.view.MotionEvent +import android.view.ViewConfiguration +import android.widget.FrameLayout +import com.droiddeck.launcher.input.InputSurfaceElement +import com.droiddeck.launcher.input.InputSurfaceStyle +import com.droiddeck.launcher.input.snapSurfaceElement +import kotlin.math.abs + +internal class InputSurfaceEditorView(context: Context) : FrameLayout(context) { + var elements: List = emptyList() + set(value) { field = value; preview.updateCustomLayout(value); invalidate() } + var style = InputSurfaceStyle() + set(value) { field = value; preview.updateStyle(value) } + var snap = true + set(value) { field = value; if (!value) clearGuides() } + private var guideX: Float? = null + private var guideY: Float? = null + var selected: String? = null + set(value) { field = value; invalidate() } + var onSelect: (String?) -> Unit = {} + var onBeginEdit: () -> Unit = {} + var onChange: (InputSurfaceElement) -> Unit = {} + private val preview = InputSurfaceView(context) + private val paint = Paint(Paint.ANTI_ALIAS_FLAG).apply { color = Color.rgb(26, 159, 255); strokeWidth = 2 * resources.displayMetrics.density } + private val radius = 14 * resources.displayMetrics.density + private var original: InputSurfaceElement? = null + private var downX = 0f + private var downY = 0f + private var corner = 0 + private var dragging = false + + init { + setBackgroundColor(Color.rgb(10, 11, 13)) + addView(preview, LayoutParams(-1, -1)) + } + + override fun dispatchDraw(canvas: Canvas) { + super.dispatchDraw(canvas) + paint.style = Paint.Style.STROKE + guideX?.let { canvas.drawLine(width * it, 0f, width * it, height.toFloat(), paint) } + guideY?.let { canvas.drawLine(0f, height * it, width.toFloat(), height * it, paint) } + val e = elements.firstOrNull { it.id == selected } ?: return + val l = width * e.x; val t = height * e.y; val r = l + width * e.width; val b = t + height * e.height + paint.style = Paint.Style.STROKE; canvas.drawRect(l, t, r, b, paint) + paint.style = Paint.Style.FILL + for (x in listOf(l, r)) for (y in listOf(t, b)) canvas.drawCircle(x, y, radius / 3, paint) + } + + private fun clearGuides() { guideX = null; guideY = null; invalidate() } + + override fun onInterceptTouchEvent(event: MotionEvent) = true + + @SuppressLint("ClickableViewAccessibility") + override fun onTouchEvent(event: MotionEvent): Boolean { + if (!isEnabled || width == 0 || height == 0) return true + when (event.actionMasked) { + MotionEvent.ACTION_DOWN -> { + clearGuides() + downX = event.x; downY = event.y; dragging = false; corner = 0 + val current = elements.firstOrNull { it.id == selected } + if (current != null) { + val left = width * current.x; val top = height * current.y + val right = left + width * current.width; val bottom = top + height * current.height + val horizontal = if (abs(downX - left) < radius) 1 else if (abs(downX - right) < radius) 2 else 0 + val vertical = if (abs(downY - top) < radius) 4 else if (abs(downY - bottom) < radius) 8 else 0 + if (horizontal != 0 && vertical != 0) corner = horizontal or vertical + } + original = if (corner != 0) current else elements.asReversed().firstOrNull { + downX / width in it.x..(it.x + it.width) && downY / height in it.y..(it.y + it.height) + } + selected = original?.id; onSelect(selected) + } + MotionEvent.ACTION_MOVE -> { + val e = original ?: return true + if (!dragging && abs(event.x - downX) + abs(event.y - downY) > ViewConfiguration.get(context).scaledTouchSlop) { + dragging = true; onBeginEdit() + } + if (!dragging) return true + val dx = (event.x - downX) / width; val dy = (event.y - downY) / height + val next = if (corner == 0) e.copy(x = (e.x + dx).coerceIn(0f, 1f - e.width), y = (e.y + dy).coerceIn(0f, 1f - e.height)) + else { + val left = if (corner and 1 != 0) (e.x + dx).coerceIn(0f, e.x + e.width - .03f) else e.x + val top = if (corner and 4 != 0) (e.y + dy).coerceIn(0f, e.y + e.height - .03f) else e.y + val right = if (corner and 2 != 0) (e.x + e.width + dx).coerceIn(left + .03f, 1f) else e.x + e.width + val bottom = if (corner and 8 != 0) (e.y + e.height + dy).coerceIn(top + .03f, 1f) else e.y + e.height + e.copy(x = left, y = top, width = right - left, height = bottom - top).bounded() + } + val aligned = if (snap) snapSurfaceElement(next, elements.filter { it.id != e.id }, 9 * resources.displayMetrics.density / width, + 9 * resources.displayMetrics.density / height, corner) else null + if (aligned != null && (aligned.x != null && aligned.x != guideX || aligned.y != null && aligned.y != guideY)) performHapticFeedback(HapticFeedbackConstants.CLOCK_TICK) + guideX = aligned?.x; guideY = aligned?.y + onChange(aligned?.element ?: next) + invalidate() + } + MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> { original = null; dragging = false; clearGuides() } + } + return true + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/ui/InputSurfaceView.kt b/app/src/main/java/com/droiddeck/launcher/ui/InputSurfaceView.kt new file mode 100644 index 0000000000..f121a9a73a --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/ui/InputSurfaceView.kt @@ -0,0 +1,789 @@ +package com.droiddeck.launcher.ui + +import com.droiddeck.launcher.R + +import android.annotation.SuppressLint +import android.content.Context +import android.graphics.BitmapFactory +import android.graphics.Canvas +import android.graphics.Color +import android.graphics.Paint +import android.graphics.Path +import android.graphics.RadialGradient +import android.graphics.RectF +import android.graphics.Shader +import android.graphics.Typeface +import android.graphics.drawable.GradientDrawable +import android.view.Gravity +import android.view.HapticFeedbackConstants +import android.view.ViewConfiguration +import android.view.MotionEvent +import com.droiddeck.launcher.input.TouchpadGestures +import com.droiddeck.launcher.input.PointerGestures +import android.view.View +import android.view.ViewGroup +import android.widget.FrameLayout +import android.widget.LinearLayout +import android.widget.TextView +import com.droiddeck.launcher.input.InputRole +import com.droiddeck.launcher.input.InputControl +import com.droiddeck.launcher.input.InputSurfaceElement +import com.droiddeck.launcher.input.InputSurfaceStyle +import com.droiddeck.launcher.input.pointerSubset +import com.droiddeck.launcher.input.InputSurfaceSource +import com.droiddeck.launcher.input.InputElementKind +import com.droiddeck.launcher.input.InputAction +import com.droiddeck.launcher.input.InputButtonIcons +import com.droiddeck.launcher.input.InputButtonShape +import com.droiddeck.launcher.input.InputButtonImages +import com.droiddeck.launcher.input.PadBridge +import com.droiddeck.launcher.input.pcKeyLabel +import com.droiddeck.launcher.session.SessionPrefs +import kotlin.math.atan2 +import kotlin.math.hypot +import kotlin.math.min + +@SuppressLint("ViewConstructor") +class InputSurfaceView( + context: Context, + private val pad: PadBridge? = null, + private var customLayout: List = emptyList(), + private val inputRole: InputRole = InputRole.SECONDARY, + private var surfaceStyle: InputSurfaceStyle = InputSurfaceStyle(), + private val haptics: Boolean = inputRole != InputRole.PRIMARY, +) : FrameLayout(context) { + private val content = FrameLayout(context) + private val elementSources = ArrayList() + private var customCanvas: ViewGroup? = null + private val owners = HashMap() + private var requestedLayout = customLayout + init { customLayout = surfaceStyle.shown(customLayout); addView(content, LayoutParams(-1, -1)); renderCustom(pad) } + + var onTouchMiss: ((MotionEvent) -> Unit)? = null + private var forwardedPointers = 0 + + override fun dispatchTouchEvent(event: MotionEvent): Boolean { + if (event.actionMasked == MotionEvent.ACTION_DOWN) { forwardedPointers = 0; cancelOwners(event) } + if (event.actionMasked == MotionEvent.ACTION_DOWN || event.actionMasked == MotionEvent.ACTION_POINTER_DOWN) { + val index = event.actionIndex + if (event.getToolType(index) == MotionEvent.TOOL_TYPE_MOUSE || !takesTouchAt(event.getX(index), event.getY(index))) + forwardedPointers = forwardedPointers or (1 shl event.getPointerId(index)) + else if (onTouchMiss != null && SessionPrefs.lowLatency(context)) requestUnbufferedDispatch(event) + } + if (forwardedPointers == 0) return route(event) + var all = 0 + for (index in 0 until event.pointerCount) all = all or (1 shl event.getPointerId(index)) + val routed = event.pointerSubset(all and forwardedPointers.inv())?.let { split -> + try { route(split) } finally { split.recycle() } + } ?: false + event.pointerSubset(all and forwardedPointers)?.let { split -> + try { onTouchMiss?.invoke(split) } finally { split.recycle() } + } + if (event.actionMasked == MotionEvent.ACTION_POINTER_UP) forwardedPointers = forwardedPointers and (1 shl event.getPointerId(event.actionIndex)).inv() + if (event.actionMasked == MotionEvent.ACTION_UP || event.actionMasked == MotionEvent.ACTION_CANCEL) forwardedPointers = 0 + return onTouchMiss != null || routed + } + + private fun route(event: MotionEvent): Boolean { + val action = event.actionMasked + if (action == MotionEvent.ACTION_CANCEL) { cancelOwners(event); return true } + val index = event.actionIndex + val pointer = event.getPointerId(index) + var taken = true + if (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_POINTER_DOWN) { + val target = elementAt(event.getX(index), event.getY(index)) + taken = target != null + when { + target is HoldButton -> { owners[pointer] = target; target.press(pointer, true) } + target != null && !(target is StickView && target in owners.values) -> owners[pointer] = target + } + } + if (action == MotionEvent.ACTION_MOVE) for (i in 0 until event.pointerCount) slide(event.getPointerId(i), event.getX(i), event.getY(i)) + for (view in owners.values.distinct()) { + if (view is HoldButton) continue + var bits = 0 + for ((id, owner) in owners) if (owner === view) bits = bits or (1 shl id) + event.pointerSubset(bits)?.let { split -> + try { split.offsetLocation(-originX(view), -originY(view)); view.dispatchTouchEvent(split) } finally { split.recycle() } + } + } + if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_POINTER_UP) { + (owners.remove(pointer) as? HoldButton)?.press(pointer, false, event.flags and MotionEvent.FLAG_CANCELED != 0) + } + return taken + } + + private fun slide(pointer: Int, x: Float, y: Float) { + val owner = owners[pointer] + if (owner != null && owner !is HoldButton) return + val over = elementAt(x, y) as? HoldButton + if (over === owner) return + (owner as? HoldButton)?.press(pointer, false, cancelled = true) + if (over == null) owners.remove(pointer) else { owners[pointer] = over; over.press(pointer, true) } + } + + private fun cancelOwners(event: MotionEvent) { + if (owners.isEmpty()) return + val views = owners.values.distinct() + owners.clear() + for (view in views) { + if (view is HoldButton) { view.cancel(); continue } + val cancel = MotionEvent.obtain(event.downTime, event.eventTime, MotionEvent.ACTION_CANCEL, 0f, 0f, 0) + try { view.dispatchTouchEvent(cancel) } finally { cancel.recycle() } + } + } + + fun updateStyle(style: InputSurfaceStyle) { + if (surfaceStyle == style) return + val relayout = surfaceStyle.hidden != style.hidden + surfaceStyle = style + if (relayout) updateCustomLayout(requestedLayout) + customCanvas?.let { canvas -> for (i in 0 until canvas.childCount) styleElement(canvas.getChildAt(i)) } + } + + private fun styleElement(view: View) { + view.alpha = surfaceStyle.opacity / 100f * ((view.tag as? InputSurfaceElement)?.opacity ?: 1f) + (view as? Surface)?.tint = surfaceStyle.tint + view.invalidate() + } + + fun updateCustomLayout(requested: List) { + requestedLayout = requested + val layout = surfaceStyle.shown(requested) + val sameElements = customLayout.map { it.copy(x = 0f, y = 0f, width = 1f, height = 1f) } == layout.map { it.copy(x = 0f, y = 0f, width = 1f, height = 1f) } + customLayout = layout + if (sameElements) customCanvas?.requestLayout() else { + elementSources.forEach { it.close() }; elementSources.clear() + owners.clear(); content.removeAllViews(); renderCustom(pad) + } + } + + fun releaseInputs() { + val now = android.os.SystemClock.uptimeMillis() + val cancel = MotionEvent.obtain(now, now, MotionEvent.ACTION_CANCEL, 0f, 0f, 0) + try { cancelOwners(cancel) } finally { cancel.recycle() } + forwardedPointers = 0 + elementSources.forEach { it.release() } + } + + override fun onVisibilityChanged(changedView: View, visibility: Int) { + super.onVisibilityChanged(changedView, visibility) + if (visibility != VISIBLE) releaseInputs() + } + + private fun renderCustom(pad: PadBridge?) { + val canvas = object : ViewGroup(context) { + override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { + val layout = customLayout + val w = MeasureSpec.getSize(widthMeasureSpec) + val h = MeasureSpec.getSize(heightMeasureSpec) + setMeasuredDimension(w, h) + for (i in layout.indices) getChildAt(i).measure(MeasureSpec.makeMeasureSpec((w * layout[i].width).toInt(), MeasureSpec.EXACTLY), + MeasureSpec.makeMeasureSpec((h * layout[i].height).toInt(), MeasureSpec.EXACTLY)) + } + override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) { + val layout = customLayout + for (i in layout.indices) { + val e = layout[i] + val child = getChildAt(i) + val x = (width * e.x).toInt(); val y = (height * e.y).toInt() + child.layout(x, y, x + child.measuredWidth, y + child.measuredHeight) + } + } + } + canvas.clipChildren = customLayout.none { it.kind == InputElementKind.STICK && (classic(it) || adaptive(it)) } + for (element in customLayout) { + val source = pad?.let { InputSurfaceSource(it, inputRole, element).also { s -> elementSources += s } } + canvas.addView(when (element.kind) { + InputElementKind.STICK -> StickView(context, element, source, element.floating, element.hideWhenIdle && pad != null) + InputElementKind.TRACKPAD -> TrackpadView(context, element.control == InputControl.RIGHT_PAD, source = source) + InputElementKind.DPAD -> DirectionPad(context, element, source) + else -> HoldButton(context, element, source) + }.apply { tag = element; contentDescription = element.name; styleElement(this); source?.onStateChanged = { postInvalidate() } }) + } + content.addView(canvas, FrameLayout.LayoutParams(-1, -1)) + customCanvas = canvas + } + + override fun onAttachedToWindow() { + super.onAttachedToWindow() + if (pad != null && elementSources.isEmpty()) { + owners.clear(); content.removeAllViews() + renderCustom(pad) + } + } + + override fun onDetachedFromWindow() { + elementSources.forEach { it.close() } + elementSources.clear() + super.onDetachedFromWindow() + } + + fun takesTouchAt(x: Float, y: Float): Boolean = elementAt(x, y) != null + + private fun elementAt(x: Float, y: Float): View? { + val canvas = customCanvas ?: return null + if (visibility != VISIBLE || x < 0f || y < 0f || x >= width || y >= height) return null + val views = (canvas.childCount - 1 downTo 0).map(canvas::getChildAt).filter { it.visibility == VISIBLE } + views.firstOrNull { !(it is StickView && it.adaptive) && hits(it, x, y, 0f) }?.let { return it } + views.filter { it is HoldButton && hits(it, x, y, slack(it)) }.minByOrNull { distance(it, x, y) }?.let { return it } + if (views.any { it is HoldButton && hits(it, x, y, slack(it) + ADAPTIVE_CLEARANCE_PX) }) return null + return views.filter { it is StickView && it.adaptive && it !in owners.values && (hits(it, x, y, 0f) || it.captures(x - originX(it), y - originY(it))) } + .minByOrNull { distance(it, x, y) } + } + + private fun hits(view: View, x: Float, y: Float, slack: Float): Boolean { + val left = originX(view) + val top = originY(view) + if ((view as? HoldButton)?.round == true && slack > 0f) { + val reach = min(view.width, view.height) / 2f + slack + return hypot(x - left - view.width / 2f, y - top - view.height / 2f) <= reach + } + return x >= left - slack && x < left + view.width + slack && y >= top - slack && y < top + view.height + slack + } + + private fun slack(view: View) = if ((view as? HoldButton)?.gamepad == true) min(view.width, view.height) / 2f * HIT_SLACK else 0f + + private fun distance(view: View, x: Float, y: Float): Float { + val cx = originX(view) + ((view as? StickView)?.centreX() ?: (view.width / 2f)) + return hypot(x - cx, y - originY(view) - view.height / 2f) + } + + private fun originX(view: View): Float { + var x = 0f + var at: View = view + while (at !== this) { x += at.left - ((at.parent as? View)?.scrollX ?: 0); at = at.parent as? View ?: break } + return x + } + + private fun originY(view: View): Float { + var y = 0f + var at: View = view + while (at !== this) { y += at.top - ((at.parent as? View)?.scrollY ?: 0); at = at.parent as? View ?: break } + return y + } + + private fun feedback(view: View, type: Int) { if (haptics) view.performHapticFeedback(type) } + + private abstract class Surface(context: Context) : View(context) { + var tint: Int? = null + protected val density = resources.displayMetrics.density + protected val rect = RectF() + private val fill = Paint(Paint.ANTI_ALIAS_FLAG) + private val stroke = Paint(Paint.ANTI_ALIAS_FLAG).apply { style = Paint.Style.STROKE } + protected val labelPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply { + textAlign = Paint.Align.CENTER + typeface = Typeface.create(Typeface.DEFAULT, Typeface.BOLD) + } + protected val captionPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply { + textAlign = Paint.Align.CENTER + color = TEXT_DIM + } + + protected fun drawSurface(canvas: Canvas, lit: Boolean, shape: InputButtonShape = InputButtonShape.ROUNDED, classic: Boolean = false) { + val inset = if (classic && tint != null) 0.75f * density else density + rect.set(inset, inset, width - inset, height - inset) + val radius = when (shape) { InputButtonShape.CIRCLE, InputButtonShape.PILL -> min(width, height) / 2f; InputButtonShape.RECTANGLE -> 2 * density; else -> CORNER * density } + val color = tint + if (classic && color != null) { + val corner = if (shape == InputButtonShape.PILL) radius * 0.55f else radius + fill.color = if (lit) shade(color, 160, 1f) else shade(color, 80, 0.12f) + canvas.drawRoundRect(rect, corner, corner, fill) + stroke.color = if (lit) light(color, 230, 0.7f) else light(color, 150, 0.25f) + stroke.strokeWidth = 1.5f * density + canvas.drawRoundRect(rect, corner, corner, stroke) + return + } + fill.color = tint?.let { color -> (color and 0x00ffffff) or ((if (lit) 140 else 48) shl 24) } ?: if (lit) ACCENT_WASH else SURFACE + canvas.drawRoundRect(rect, radius, radius, fill) + stroke.color = tint?.let { color -> (color and 0x00ffffff) or ((if (lit) 255 else 160) shl 24) } ?: if (lit) ACCENT else LINE + stroke.strokeWidth = (if (lit) 2f else 1f) * density + canvas.drawRoundRect(rect, radius, radius, stroke) + } + + protected fun drawCircle(canvas: Canvas, x: Float, y: Float, radius: Float, fillColor: Int, strokeColor: Int) { + fill.color = fillColor + canvas.drawCircle(x, y, radius, fill) + stroke.color = strokeColor + stroke.strokeWidth = 1.5f * density + canvas.drawCircle(x, y, radius, stroke) + } + + protected fun drawKeyboard(canvas: Canvas, x: Float, y: Float, lit: Boolean) { + val color = tint ?: return + stroke.color = if (lit) light(color, 230, 0.7f) else light(color, 150, 0.25f) + stroke.strokeWidth = 1.5f * density + rect.set(x - 10 * density, y - 7 * density, x + 10 * density, y + 7 * density) + canvas.drawRoundRect(rect, 2 * density, 2 * density, stroke) + for (row in 0..1) for (col in 0..2) { + val kx = x + (-6f + col * 5f) * density + val ky = y + (-3f + row * 4f) * density + canvas.drawLine(kx, ky, kx + 2 * density, ky, stroke) + } + canvas.drawLine(x - 5 * density, y + 4 * density, x + 5 * density, y + 4 * density, stroke) + } + + protected fun textColor(lit: Boolean) = if (lit) Color.WHITE else tint?.let { light(it, 210, 0.75f) } ?: TEXT + } + + private inner class HoldButton(context: Context, private val element: InputSurfaceElement, private val source: InputSurfaceSource?) : Surface(context) { + private var down = false + private val pointers = HashSet() + val round get() = element.buttonShape == InputButtonShape.CIRCLE + val gamepad get() = round || element.buttonShape == InputButtonShape.PILL + private val symbol = InputButtonIcons.drawable(context, element.icon) + private val bitmap = InputButtonImages.file(context, element.image)?.let { file -> + val size = BitmapFactory.Options().apply { inJustDecodeBounds = true } + BitmapFactory.decodeFile(file.path, size) + val options = BitmapFactory.Options().apply { + while (size.outWidth / inSampleSize.coerceAtLeast(1) > 512 || size.outHeight / inSampleSize.coerceAtLeast(1) > 512) + inSampleSize = inSampleSize.coerceAtLeast(1) * 2 + } + if (file.isFile) BitmapFactory.decodeFile(file.path, options) else null + } + private val imageRect = RectF() + + override fun onDraw(canvas: Canvas) { + val lit = if (element.toggle) source?.latched == true else down + val classic = classic(element) + drawSurface(canvas, lit, element.buttonShape, classic) + val cx = width / 2f + labelPaint.color = if (classic && tint != null) textColor(lit) else if (lit) ACCENT else TEXT + bitmap?.let { + val scale = min(width * .84f / it.width, height * .84f / it.height) + imageRect.set(cx - it.width * scale / 2, (height - it.height * scale) / 2, + cx + it.width * scale / 2, (height + it.height * scale) / 2) + canvas.drawBitmap(it, null, imageRect, labelPaint) + return + } + if (classic && element.id == "builtin:keyboard" && element.icon == InputButtonIcons.KEYBOARD) { drawKeyboard(canvas, cx, height / 2f, lit); return } + if (element.icon != 0 && InputButtonIcons.draw(canvas, element.icon, cx, height / 2f, min(width, height) * .68f, labelPaint, symbol)) return + if (!element.showLabel) return + labelPaint.style = Paint.Style.FILL + if (classic && element.name == element.control.label) { + val radius = min(width, height) / 2f + val direction = DIRECTIONS.indexOf(element.control) + if (direction >= 0) { drawArrow(canvas, cx, height / 2f, radius * 0.34f, direction, labelPaint); return } + val glyph = glyph(element.control) + labelPaint.textSize = when { + element.buttonShape == InputButtonShape.PILL -> height / 2f * 0.8f + glyph.length > 2 -> 11 * density + else -> radius * if (glyph.length == 2) 0.62f else 0.85f + } + canvas.drawText(glyph, cx, height / 2f + labelPaint.textSize * .35f, labelPaint) + return + } + labelPaint.textSize = min(height * .3f, 30 * density) + val labelWidth = labelPaint.measureText(element.name) + if (labelWidth > width * .84f) labelPaint.textSize *= width * .84f / labelWidth + canvas.drawText(element.name, cx, height / 2f + labelPaint.textSize * .35f, labelPaint) + } + + fun press(pointer: Int, pressed: Boolean, cancelled: Boolean = false) { + if (pressed) pointers.add(pointer) else pointers.remove(pointer) + set(pointers.isNotEmpty(), cancelled) + } + + fun cancel() { pointers.clear(); set(false, cancelled = true); source?.release() } + + override fun onDetachedFromWindow() { cancel(); super.onDetachedFromWindow() } + + private fun set(pressed: Boolean, cancelled: Boolean = false) { + if (down == pressed) return + down = pressed + if (pressed) feedback(this, HapticFeedbackConstants.VIRTUAL_KEY) + if (!pressed && cancelled) source?.cancel() else source?.update(if (pressed) 1f else 0f) + invalidate() + } + } + + private inner class TrackpadView(context: Context, private val right: Boolean, private val captioned: Boolean = true, + private val source: InputSurfaceSource? = null) : Surface(context) { + private var gestureX = 0f + private var gestureY = 0f + private val mouse = TouchpadGestures(ViewConfiguration.get(context).scaledTouchSlop.toFloat(), object : PointerGestures.Listener { + override fun onMove(x: Float, y: Float) { + source?.pointerDelta(2 * (x - gestureX) / width.coerceAtLeast(1), 2 * (y - gestureY) / height.coerceAtLeast(1)) + gestureX = x; gestureY = y + } + override fun onButton(button: Int, pressed: Boolean, x: Float, y: Float) { source?.gestureButton(button, pressed) } + override fun onWheel(steps: Int) { source?.gestureWheel(steps) } + override fun onLongPress() { feedback(this@TrackpadView, HapticFeedbackConstants.LONG_PRESS) } + }).apply { bounds = RectF(-1e9f, -1e9f, 1e9f, 1e9f) } + + private var touching = false + private var clicked = false + private var fingerX = 0f + private var fingerY = 0f + private val guide = Paint(Paint.ANTI_ALIAS_FLAG).apply { style = Paint.Style.STROKE; color = GUIDE } + private val dot = Paint(Paint.ANTI_ALIAS_FLAG).apply { color = ACCENT } + private val glow = Paint(Paint.ANTI_ALIAS_FLAG) + + override fun onDraw(canvas: Canvas) { + drawSurface(canvas, clicked) + val cx = width / 2f + val cy = height / 2f + guide.strokeWidth = density + val reach = min(width, height) / 2f - 24 * density + canvas.drawCircle(cx, cy, reach, guide) + canvas.drawCircle(cx, cy, reach / 2f, guide) + canvas.drawLine(cx - reach, cy, cx + reach, cy, guide) + canvas.drawLine(cx, cy - reach, cx, cy + reach, guide) + captionPaint.textSize = 12 * density + captionPaint.color = if (touching) ACCENT else TEXT_DIM + if (captioned) canvas.drawText((contentDescription?.toString() ?: if (right) context.getString(R.string.deck_right_trackpad) else context.getString(R.string.deck_left_trackpad)).uppercase(), cx, 28 * density, captionPaint) + if (touching) { + val radius = 56 * density + glow.shader = RadialGradient(fingerX, fingerY, radius, ACCENT_GLOW, Color.TRANSPARENT, Shader.TileMode.CLAMP) + canvas.drawCircle(fingerX, fingerY, radius, glow) + canvas.drawCircle(fingerX, fingerY, (if (clicked) 16 else 12) * density, dot) + } + } + + @SuppressLint("ClickableViewAccessibility") + override fun onTouchEvent(event: MotionEvent): Boolean { + if (source?.mouseGestures == true) { + if (event.actionMasked == MotionEvent.ACTION_DOWN) { + gestureX = 0f; gestureY = 0f; mouse.place(0f, 0f) + source.gestureContact(true) + } + touching = event.actionMasked != MotionEvent.ACTION_UP && event.actionMasked != MotionEvent.ACTION_CANCEL + fingerX = event.x; fingerY = event.y + mouse.onTouch(event) + if (!touching) source.gestureContact(false) + invalidate() + return true + } + when (event.actionMasked) { + MotionEvent.ACTION_DOWN, MotionEvent.ACTION_MOVE -> { + touch(event.getX(0), event.getY(0)) + click(event.pointerCount > 1) + } + MotionEvent.ACTION_POINTER_DOWN -> click(true) + MotionEvent.ACTION_POINTER_UP -> { + val stays = if (event.actionIndex == 0) 1 else 0 + touch(event.getX(stays), event.getY(stays)) + click(event.pointerCount - 1 > 1) + } + MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> { + click(false) + touching = false + source?.update(0f, 0f, false) + invalidate() + } + } + return true + } + + override fun onDetachedFromWindow() { mouse.cancel(); source?.release(); super.onDetachedFromWindow() } + + private fun touch(x: Float, y: Float) { + fingerX = x.coerceIn(0f, width.toFloat()) + fingerY = y.coerceIn(0f, height.toFloat()) + touching = true + val xAxis = (fingerX - width / 2f) / (width / 2f).coerceAtLeast(1f) + val yAxis = -(fingerY - height / 2f) / (height / 2f).coerceAtLeast(1f) + source?.update(xAxis, yAxis, true) + invalidate() + } + + private fun click(down: Boolean) { + if (clicked == down) return + clicked = down + if (down) feedback(this, HapticFeedbackConstants.VIRTUAL_KEY) + source?.click(down) + invalidate() + } + } + + private inner class StickView(context: Context, private val element: InputSurfaceElement, private val source: InputSurfaceSource? = null, private val floating: Boolean = true, private val hideWhenIdle: Boolean = false) : Surface(context) { + private var touching = false + private var clicking = false + private var lastUp = 0L + private var downAt = 0L + private var downX = 0f + private var downY = 0f + private var travel = 0f + private val tapSlop = ViewConfiguration.get(context).scaledTouchSlop.toFloat() + private var originX = 0f + private var originY = 0f + private var knobX = 0f + private var knobY = 0f + private val ring = Paint(Paint.ANTI_ALIAS_FLAG).apply { style = Paint.Style.STROKE } + private val knob = Paint(Paint.ANTI_ALIAS_FLAG) + + val adaptive get() = adaptive(element) + + fun reach() = min(width, height) * if (classic(element)) CLASSIC_REACH else PANEL_REACH + + fun captures(x: Float, y: Float): Boolean { + val cx = centreX() + val dx = if (element.control == InputControl.LS) maxOf(0f, x - cx) else x - cx + val limit = reach() * ADAPTIVE_REACH + return dx * dx + (y - height / 2f) * (y - height / 2f) <= limit * limit + } + + fun centreX() = when { + !classic(element) || width <= height -> width / 2f + element.control == InputControl.LS -> width - height / 2f + else -> height / 2f + } + + override fun onDraw(canvas: Canvas) { + if (hideWhenIdle && !touching) return + val cx = if (touching) originX else centreX() + val cy = if (touching) originY else height / 2f + val color = tint + if (classic(element) && color != null) { + val radius = min(width, height) / 2f - 0.75f * density + drawCircle(canvas, cx, cy, radius, + if (touching) shade(color, 160, 1f) else shade(color, 60, 0.12f), if (touching) light(color, 230, 0.7f) else light(color, 150, 0.25f)) + val knobRadius = radius * 0.46f + drawCircle(canvas, cx + knobX, cy + knobY, knobRadius, + if (touching || clicking) shade(color, 160, 1f) else shade(color, 120, 0.35f), if (touching) light(color, 230, 0.7f) else light(color, 150, 0.25f)) + if (!element.showLabel) return + labelPaint.color = textColor(touching) + if (drawDirectionLabels(canvas, source, false, cx, cy, radius * 0.66f, radius * 0.34f, labelPaint)) return + labelPaint.textSize = knobRadius * 0.8f + val label = if (element.name == element.control.label) glyph(element.control) else element.name + canvas.drawText(label, cx + knobX, cy + knobY + labelPaint.textSize * 0.35f, labelPaint) + return + } + drawSurface(canvas, clicking) + ring.strokeWidth = 2 * density + ring.color = if (touching) ACCENT else LINE + canvas.drawCircle(cx, cy, reach(), ring) + knob.color = if (touching) ACCENT else KNOB + canvas.drawCircle(cx + knobX, cy + knobY, reach() * 0.45f, knob) + if (!element.showLabel) return + labelPaint.color = if (touching) ACCENT else TEXT + drawDirectionLabels(canvas, source, false, cx, cy, reach() * 1.32f, reach() * 0.68f, labelPaint) + } + + @SuppressLint("ClickableViewAccessibility") + override fun onTouchEvent(event: MotionEvent): Boolean { + val r = reach().coerceAtLeast(1f) + when (event.actionMasked) { + MotionEvent.ACTION_DOWN -> { + downAt = event.eventTime + downX = event.x + downY = event.y + travel = 0f + originX = if (floating) event.x else centreX() + originY = if (floating) event.y else height / 2f + touching = true + feedback(this, HapticFeedbackConstants.VIRTUAL_KEY) + if (lastUp != 0L && event.eventTime - lastUp < CLICK_MS) setClick(true) + follow(event.x, event.y, r, force = true) + } + MotionEvent.ACTION_MOVE -> follow(event.getX(0), event.getY(0), r) + MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> { + touching = false + val cancelled = event.actionMasked == MotionEvent.ACTION_CANCEL || event.flags and MotionEvent.FLAG_CANCELED != 0 + travel = maxOf(travel, hypot(event.x - downX, event.y - downY)) + lastUp = if (!clicking && !cancelled && event.eventTime - downAt <= STICK_TAP_MS && travel <= tapSlop) event.eventTime else 0L + setClick(false) + move(0f, 0f, 1f) + } + } + return true + } + + private fun follow(x: Float, y: Float, r: Float, force: Boolean = false) { + travel = maxOf(travel, hypot(x - downX, y - downY)) + var dx = x - originX + var dy = y - originY + val length = hypot(dx, dy) + if (length > r) { dx *= r / length; dy *= r / length } + if (force || dx != knobX || dy != knobY) move(dx, dy, r) + } + + private fun move(dx: Float, dy: Float, r: Float) { + knobX = dx + knobY = dy + source?.update(dx / r, dy / r) + invalidate() + } + + private fun setClick(down: Boolean) { + if (clicking == down) return + clicking = down + if (down) feedback(this, HapticFeedbackConstants.VIRTUAL_KEY) + source?.click(down) + } + + override fun onDetachedFromWindow() { + setClick(false) + if (knobX != 0f || knobY != 0f) move(0f, 0f, 1f) + super.onDetachedFromWindow() + } + } + + private inner class DirectionPad(context: Context, private val element: InputSurfaceElement, private val source: InputSurfaceSource? = null) : View(context) { + private val density = resources.displayMetrics.density + private var held = 0 + private val arm = Paint(Paint.ANTI_ALIAS_FLAG) + private val label = Paint(Paint.ANTI_ALIAS_FLAG).apply { textAlign = Paint.Align.CENTER; typeface = Typeface.create(Typeface.DEFAULT, Typeface.BOLD); color = TEXT } + private val rect = RectF() + + override fun onDraw(canvas: Canvas) { + val size = min(width, height) * 0.86f + val cx = width / 2f + val cy = height / 2f + val half = size / 2f + val thick = size / 3f / 2f + fun armRect(bit: Int, l: Float, t: Float, r: Float, b: Float) { + arm.color = if (held and bit != 0) ACCENT else KNOB + rect.set(l, t, r, b) + canvas.drawRoundRect(rect, 8 * density, 8 * density, arm) + } + armRect(1, cx - thick, cy - half, cx + thick, cy - thick) + armRect(4, cx - thick, cy + thick, cx + thick, cy + half) + armRect(8, cx - half, cy - thick, cx - thick, cy + thick) + armRect(2, cx + thick, cy - thick, cx + half, cy + thick) + arm.color = KNOB + canvas.drawRect(cx - thick, cy - thick, cx + thick, cy + thick, arm) + if (element.showLabel) drawDirectionLabels(canvas, source, true, cx, cy, (half + thick) / 2f, thick * 1.7f, label) + } + + @SuppressLint("ClickableViewAccessibility") + override fun onTouchEvent(event: MotionEvent): Boolean { + val bits = when (event.actionMasked) { + MotionEvent.ACTION_DOWN, MotionEvent.ACTION_MOVE -> { + val dx = event.getX(0) - width / 2f + val dy = event.getY(0) - height / 2f + if (hypot(dx, dy) < min(width, height) * 0.08f) 0 else { + val sector = ((Math.toDegrees(atan2(dy.toDouble(), dx.toDouble())) + 360 + 22.5) % 360 / 45).toInt() + intArrayOf(2, 2 or 4, 4, 4 or 8, 8, 8 or 1, 1, 1 or 2)[sector] + } + } + else -> 0 + } + set(bits) + return true + } + + private fun set(bits: Int) { + if (bits == held) return + if (bits and held.inv() != 0) feedback(this, HapticFeedbackConstants.VIRTUAL_KEY) + held = bits + source?.update((if (bits and 2 != 0) 1f else 0f) - (if (bits and 8 != 0) 1f else 0f), + (if (bits and 4 != 0) 1f else 0f) - (if (bits and 1 != 0) 1f else 0f)) + + invalidate() + } + + override fun onDetachedFromWindow() { + set(0) + super.onDetachedFromWindow() + } + } + + companion object { + private const val CLICK_MS = 300L + private const val STICK_TAP_MS = 200L + private const val CLASSIC_REACH = 0.5f + private const val PANEL_REACH = 0.30f + private const val HIT_SLACK = 0.25f + private const val ADAPTIVE_CLEARANCE_PX = 5f + private val ADAPTIVE_REACH = 1.4f * kotlin.math.sqrt(1.5f) + + + private const val CORNER = 20f + private const val CORNER_PILL = 22f + private val ACCENT = Color.rgb(0x1A, 0x9F, 0xFF) + private val ACCENT_WASH = Color.argb(0x2E, 0x1A, 0x9F, 0xFF) + private val ACCENT_GLOW = Color.argb(0x66, 0x1A, 0x9F, 0xFF) + private val ON_ACCENT = Color.rgb(0x03, 0x11, 0x1F) + internal val SURFACE = Color.rgb(0x0A, 0x0B, 0x0D) + internal val LINE = Color.rgb(0x26, 0x29, 0x2E) + private val GUIDE = Color.rgb(0x16, 0x18, 0x1B) + internal val TEXT = Color.rgb(0xF2, 0xF4, 0xF7) + private val TEXT_DIM = Color.rgb(0x7A, 0x80, 0x8A) + private val KNOB = Color.rgb(0x2A, 0x2D, 0x33) + + private fun adaptive(element: InputSurfaceElement) = element.kind == InputElementKind.STICK && element.floating && element.hideWhenIdle + + private fun classic(element: InputSurfaceElement) = (element.id == "legacy:${element.control.id}" || element.id == "builtin:keyboard") && + (element.buttonShape == InputButtonShape.CIRCLE || element.buttonShape == InputButtonShape.PILL) + + private val DIRECTIONS = listOf(InputControl.UP, InputControl.RIGHT, InputControl.DOWN, InputControl.LEFT) + + internal fun shade(tint: Int, alpha: Int, f: Float) = + Color.argb(alpha, (Color.red(tint) * f).toInt(), (Color.green(tint) * f).toInt(), (Color.blue(tint) * f).toInt()) + + internal fun light(tint: Int, alpha: Int, f: Float) = Color.argb(alpha, (Color.red(tint) + (255 - Color.red(tint)) * f).toInt(), + (Color.green(tint) + (255 - Color.green(tint)) * f).toInt(), (Color.blue(tint) + (255 - Color.blue(tint)) * f).toInt()) + + private fun glyph(control: InputControl) = when (control) { + InputControl.LS -> "L" + InputControl.RS -> "R" + InputControl.SELECT -> "⧉" + InputControl.START -> "☰" + InputControl.GUIDE -> "◉" + InputControl.QAM -> "⋯" + else -> control.id.uppercase() + } + + private val CARDINAL = listOf(0f to -1f, 1f to 0f, 0f to 1f, -1f to 0f) + private val DIAGONAL = listOf(.7071f to -.7071f, .7071f to .7071f, -.7071f to .7071f, -.7071f to -.7071f) + + internal fun directionLabels(context: Context, action: InputAction.Directions?, cardinalOnly: Boolean): List, String>> { + if (action == null) return emptyList() + val codes = listOf(action.up, action.right, action.down, action.left).zip(CARDINAL) + + if (cardinalOnly) emptyList() else action.diagonals.orEmpty().zip(DIAGONAL) + return codes.filter { it.first != 0 }.map { (code, unit) -> unit to pcKeyLabel(context, code) } + } + + private fun View.drawDirectionLabels(canvas: Canvas, source: InputSurfaceSource?, cardinalOnly: Boolean, cx: Float, cy: Float, + distance: Float, box: Float, paint: Paint): Boolean { + val labels = directionLabels(context, source?.directions, cardinalOnly) + paint.style = Paint.Style.FILL + for ((unit, text) in labels) { + paint.textSize = box * .6f + val measured = paint.measureText(text) + if (measured > box) paint.textSize *= box / measured + canvas.drawText(text, cx + unit.first * distance, cy + unit.second * distance - (paint.ascent() + paint.descent()) / 2f, paint) + } + return labels.isNotEmpty() + } + + private fun drawArrow(canvas: Canvas, x: Float, y: Float, size: Float, direction: Int, paint: Paint) { + val (fx, fy) = when (direction) { 0 -> 0f to -1f; 1 -> 1f to 0f; 2 -> 0f to 1f; else -> -1f to 0f } + val path = Path() + path.moveTo(x + fx * size, y + fy * size) + path.lineTo(x - fx * size * 0.7f - fy * size, y - fy * size * 0.7f + fx * size) + path.lineTo(x - fx * size * 0.7f + fy * size, y - fy * size * 0.7f - fx * size) + path.close() + canvas.drawPath(path, paint) + } + + internal fun dp(context: Context, value: Int): Int = (value * context.resources.displayMetrics.density + 0.5f).toInt() + + internal fun outline(context: Context, radius: Float, fill: Int) = GradientDrawable().apply { + cornerRadius = radius * context.resources.displayMetrics.density + setColor(fill) + setStroke(dp(context, 1), LINE) + } + + internal fun roundButton(context: Context, label: String, description: String, onClick: () -> Unit, wide: Boolean = false) = + TextView(context).apply { + text = label + contentDescription = description + textSize = if (wide) 13f else 20f + typeface = Typeface.create(Typeface.DEFAULT, Typeface.BOLD) + letterSpacing = if (wide) 0.08f else 0f + gravity = Gravity.CENTER + setTextColor(TEXT) + background = outline(context, CORNER_PILL, SURFACE) + setPadding(if (wide) dp(context, 16) else 0, 0, if (wide) dp(context, 16) else 0, 0) + minWidth = dp(context, 44) + layoutParams = LinearLayout.LayoutParams(if (wide) LinearLayout.LayoutParams.WRAP_CONTENT else dp(context, 44), dp(context, 44)) + setOnClickListener { + performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY) + onClick() + } + } + + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/ui/ScreenKeyPicker.kt b/app/src/main/java/com/droiddeck/launcher/ui/KeyCombinationPicker.kt similarity index 95% rename from app/src/main/java/com/droiddeck/launcher/ui/ScreenKeyPicker.kt rename to app/src/main/java/com/droiddeck/launcher/ui/KeyCombinationPicker.kt index e70249eef2..3a06fbb3bc 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/ScreenKeyPicker.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/KeyCombinationPicker.kt @@ -52,13 +52,16 @@ import com.droiddeck.launcher.input.PcKey import com.droiddeck.launcher.input.pcKeyLabel @Composable -fun ScreenKeyPicker( +fun KeyCombinationPicker( onDismiss: () -> Unit, onAdd: (List) -> Unit, initial: List = emptyList(), sequence: Boolean = false, sequenceLimit: Int = 4, + allowEmpty: Boolean = false, slots: List = emptyList(), + title: String = stringResource(R.string.input_keys), + confirm: String = stringResource(R.string.gesture_save_changes), ) { var picked by remember { mutableStateOf(initial) } val p = LocalPalette.current @@ -86,7 +89,7 @@ fun ScreenKeyPicker( ) { Column { Row(Modifier.fillMaxWidth().padding(start = 16.dp, end = 6.dp), verticalAlignment = Alignment.CenterVertically) { - Text(stringResource(R.string.screen_keys_add), Modifier.weight(1f), style = MaterialTheme.typography.titleMedium, fontWeight = FontWeight.SemiBold) + Text(title, Modifier.weight(1f), style = MaterialTheme.typography.titleMedium, fontWeight = FontWeight.SemiBold) IconButton(onClick = onDismiss, modifier = Modifier.focusGlide(CircleShape)) { Icon(Icons.Default.Close, null) } } Row( @@ -136,10 +139,10 @@ fun ScreenKeyPicker( Row(Modifier.fillMaxWidth().padding(horizontal = 16.dp, vertical = 8.dp), horizontalArrangement = Arrangement.spacedBy(8.dp)) { Button( onClick = { onAdd(picked) }, - enabled = picked.isNotEmpty(), + enabled = allowEmpty || picked.isNotEmpty(), modifier = Modifier.weight(1f).focusGlide(CircleShape), ) { - Text(stringResource(R.string.screen_keys_add)) + Text(confirm) } } } diff --git a/app/src/main/java/com/droiddeck/launcher/ui/LabBuilds.kt b/app/src/main/java/com/droiddeck/launcher/ui/LabBuilds.kt new file mode 100644 index 0000000000..8bfc59306b --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/ui/LabBuilds.kt @@ -0,0 +1,162 @@ +package com.droiddeck.launcher.ui + +import android.text.format.DateUtils +import androidx.compose.foundation.background +import androidx.compose.foundation.border +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.height +import androidx.compose.foundation.layout.heightIn +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.size +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.outlined.Code +import androidx.compose.material3.Icon +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableIntStateOf +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.clip +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.text.style.TextOverflow +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp +import com.droiddeck.launcher.R +import com.droiddeck.launcher.core.WirelessAdbFix +import com.droiddeck.launcher.lab.LabOverlay + +/** + * Lab builds on this device: the one in use, one waiting for a restart, and the kept ones. + * [onRebuild] offers to build a kept one again for this install, when something can. + */ +@Composable +internal fun LabBuilds(sessionRunning: Boolean = false, onRebuild: ((LabOverlay.Info) -> Unit)? = null) { + val context = LocalContext.current + var version by remember { mutableIntStateOf(0) } + var busy by remember { mutableStateOf(false) } + var error by remember { mutableStateOf(null) } + val current = remember(version) { LabOverlay.current(context) } + val pending = remember(version) { LabOverlay.pending(context) } + val saved = remember(version) { LabOverlay.saved(context) } + if (current == null && pending == null && saved.isEmpty()) return + val active = LabOverlay.active + val paused = active == null && current != null && LabOverlay.skipped == LabOverlay.SKIPPED_APP_IMAGE + val restartFailed = stringResource(R.string.lab_restart_failed) + val canRestart = !busy && !sessionRunning + + /** Android may have given the APK an app image again, which a Lab build cannot load over. */ + fun restart() { + busy = true + error = null + Thread({ + val result = runCatching { + if (LabOverlay.needsRecompile(context)) WirelessAdbFix.shell(context, LabOverlay.recompileCommand(context)) + } + android.os.Handler(android.os.Looper.getMainLooper()).post { + busy = false + if (result.isSuccess) LabOverlay.restart(context) + else error = restartFailed.format(result.exceptionOrNull()?.message.orEmpty()) + } + }, "lab-restart").start() + } + + val colors = MaterialTheme.colorScheme + val pal = LocalPalette.current + Column(Modifier.fillMaxWidth().padding(top = 18.dp)) { + Text( + stringResource(R.string.lab_builds).uppercase(), fontSize = 12.sp, fontWeight = FontWeight.SemiBold, letterSpacing = 1.5.sp, + color = colors.onSurfaceVariant, modifier = Modifier.padding(start = 2.dp, top = 2.dp, bottom = 10.dp), + ) + Column(Modifier.fillMaxWidth().clip(Shape14).background(colors.surface).border(1.dp, pal.line, Shape14)) { + val rows = mutableListOf<@Composable () -> Unit>() + if (current != null) rows += { + BuildRow( + current, true, + when { + active != null -> stringResource(R.string.lab_in_use) + paused -> stringResource(R.string.lab_paused_row) + else -> stringResource(R.string.lab_not_loaded, LabOverlay.skipped.orEmpty()) + }, + warn = active == null, + ) { + if (paused) PrimaryButton(stringResource(R.string.lab_restart), compact = true, enabled = canRestart) { restart() } + SecondaryButton(stringResource(R.string.lab_turn_off), compact = true, enabled = canRestart) { LabOverlay.turnOff(context) } + } + } + if (pending != null) rows += { + BuildRow(pending, false, stringResource(R.string.lab_waiting), warn = false) { + PrimaryButton(stringResource(R.string.lab_restart), compact = true, enabled = canRestart) { restart() } + } + } + for (build in saved) rows += { + var armed by remember(build.dir) { mutableStateOf(false) } + val info = build.info + BuildRow( + info, false, + when { + armed -> stringResource(R.string.lab_delete_warning) + !build.usable -> stringResource(R.string.lab_other_install) + build.reason == LabOverlay.Reason.DID_NOT_START -> stringResource(R.string.lab_off_did_not_start) + else -> stringResource(R.string.lab_kept) + }, + warn = armed || !build.usable || build.reason == LabOverlay.Reason.DID_NOT_START, + ) { + if (!build.usable && !armed && info != null && onRebuild != null) SecondaryButton(stringResource(R.string.lab_rebuild), compact = true, enabled = !busy) { + onRebuild(info) + } + if (build.usable && !armed) SecondaryButton(stringResource(R.string.lab_use), compact = true, enabled = canRestart) { + runCatching { LabOverlay.stage(context, build) } + .onSuccess { version++; restart() } + .onFailure { error = it.message } + } + SecondaryButton(stringResource(if (armed) R.string.lab_delete_confirm else R.string.lab_delete), compact = true, enabled = !busy) { + if (armed) { + LabOverlay.delete(build) + version++ + } else armed = true + } + } + } + rows.forEachIndexed { index, row -> + row() + if (index < rows.lastIndex) Box(Modifier.fillMaxWidth().height(1.dp).background(pal.line)) + } + } + error?.let { Text(it, fontSize = 12.5.sp, color = AttentionAmber, modifier = Modifier.padding(start = 2.dp, top = 8.dp)) } + } +} + +@Composable +private fun BuildRow(info: LabOverlay.Info?, current: Boolean, status: String, warn: Boolean, actions: @Composable () -> Unit) { + val colors = MaterialTheme.colorScheme + val pal = LocalPalette.current + Row( + verticalAlignment = Alignment.CenterVertically, + horizontalArrangement = Arrangement.spacedBy(12.dp), + modifier = Modifier.fillMaxWidth().heightIn(min = 60.dp).padding(horizontal = 14.dp, vertical = 10.dp), + ) { + Box(contentAlignment = Alignment.Center, modifier = Modifier.size(34.dp).clip(Shape12) + .background(if (current) pal.signal.copy(alpha = 0.18f) else colors.surfaceVariant)) { + Icon(Icons.Outlined.Code, null, tint = if (current) pal.signal else colors.onSurfaceVariant, modifier = Modifier.size(20.dp)) + } + Column(Modifier.weight(1f)) { + Text(info?.label ?: "?", fontSize = 14.5.sp, fontWeight = FontWeight.SemiBold, color = colors.onBackground, + maxLines = 1, overflow = TextOverflow.Ellipsis) + val built = info?.built?.takeIf { it > 0 }?.let { DateUtils.getRelativeTimeSpanString(it * 1000, System.currentTimeMillis(), DateUtils.MINUTE_IN_MILLIS).toString() } + Text(listOfNotNull(status, built).joinToString(" · "), fontSize = 12.5.sp, color = if (warn) AttentionAmber else colors.onSurfaceVariant, + maxLines = 2, overflow = TextOverflow.Ellipsis) + } + Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) { actions() } + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/ui/LibraryPage.kt b/app/src/main/java/com/droiddeck/launcher/ui/LibraryPage.kt index 6550a2fd06..73dad7aa70 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/LibraryPage.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/LibraryPage.kt @@ -109,6 +109,7 @@ import com.droiddeck.launcher.stores.itch.ItchUpdates import com.droiddeck.launcher.stores.gog.GogMaintenance import com.droiddeck.launcher.stores.retainOrder import com.droiddeck.launcher.stores.xbox.XboxLibraryFilter +import com.droiddeck.launcher.stores.entryTime import com.droiddeck.launcher.stores.sortGames import kotlinx.coroutines.launch import androidx.compose.runtime.setValue @@ -296,8 +297,8 @@ internal fun LibraryPage(s: FrontEndState, a: FrontEndActions, selected: String, val sizesPending = sizeSort && !sizesWaited && candidates.any { sizeOf(it) == null } val sorted = sortGames( candidates, sort, title = { it.title }, releaseDate = { it.item?.releaseDate.orEmpty() }, - lastPlayed = { e -> e.game?.lastPlayed ?: e.storeKey?.let { lastPlayed[it] } ?: 0L }, - installedAt = { e -> e.storeKey?.let { installedAt[it] } ?: 0L }, + lastPlayed = { e -> entryTime(e.game?.lastPlayed, e.storeKey?.let { lastPlayed[it] }) }, + installedAt = { e -> entryTime(e.storeKey?.let { installedAt[it] }, e.game?.installedAt) }, size = { e -> sizeOf(e) ?: 0L }, ) var stable by remember { mutableStateOf(emptyList()) } @@ -741,7 +742,7 @@ internal fun LibraryMarker(e: LibraryEntry, download: DownloadEntry?, showDownlo } } -private fun libraryUpdateAvailable(e: LibraryEntry): Boolean = e.storeKey?.let { it in ItchUpdates.available || it in GameJoltUpdates.available } == true || +private fun libraryUpdateAvailable(e: LibraryEntry): Boolean = e.storeKey?.let { it in ItchUpdates.available || it in GameJoltUpdates.available || it in com.droiddeck.launcher.stores.ea.EaUpdates.available } == true || e.source == Store.GOG.id && (e.item?.id ?: e.game?.storeId)?.let { GogMaintenance.checks[it]?.state } == GogMaintenance.State.AVAILABLE @Composable diff --git a/app/src/main/java/com/droiddeck/launcher/ui/ModeSettingsDialog.kt b/app/src/main/java/com/droiddeck/launcher/ui/ModeSettingsDialog.kt index dfaf8b6f71..70fd435c94 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/ModeSettingsDialog.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/ModeSettingsDialog.kt @@ -120,6 +120,7 @@ class ModeSettingsActions( val onSteamDownloadsInBackground: (Boolean) -> Unit = {}, val onPipAutoEnter: (Boolean) -> Unit = {}, val onOsc: (String) -> Unit, + val onInputSettings: () -> Unit, val onBackHold: (Boolean) -> Unit = {}, val onBackActionsInverted: (Boolean) -> Unit = {}, val onDirectAudio: (Boolean) -> Unit, @@ -223,6 +224,8 @@ fun ModeSheetRows(s: ModeSettings, a: ModeSettingsActions, tab: String, extras: ChoiceRow(host, "picture-position", stringResource(R.string.display_picture_position), null, listOf("top" to stringResource(R.string.display_position_top), "bottom" to stringResource(R.string.display_position_bottom), + "left" to stringResource(R.string.display_position_left), + "right" to stringResource(R.string.display_position_right), "center" to stringResource(R.string.display_position_center)), position, onPick = a.onPicturePosition) } @@ -259,6 +262,7 @@ fun ModeSheetRows(s: ModeSettings, a: ModeSettingsActions, tab: String, extras: extras.drivers(s.gpuDrivers) } if (tab == SheetTab.CONTROLS) { + InputQuickSettings(host, desktop = s.mode == SessionService.MODE_DESKTOP, onEdit = a.onInputSettings) SettingsGroup(if (steam) stringResource(R.string.mode_touch_controls) else stringResource(R.string.mode_touch)) { ChoiceRow( host, "touch", stringResource(R.string.mode_touch), null, diff --git a/app/src/main/java/com/droiddeck/launcher/ui/PerformanceDialog.kt b/app/src/main/java/com/droiddeck/launcher/ui/PerformanceDialog.kt index 2c0927bc88..f8f44907a7 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/PerformanceDialog.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/PerformanceDialog.kt @@ -53,6 +53,7 @@ fun PerformancePage( ramCapAvailable: Boolean, prootNoSeccomp: Boolean, prootFastPath: Boolean, + nativeUserland: Boolean, cursorComposite: Boolean, guestHostname: String, phantomWarning: String?, @@ -72,6 +73,7 @@ fun PerformancePage( onVramCapMb: (Int) -> Unit, onProotNoSeccomp: (Boolean) -> Unit, onProotFastPath: (Boolean) -> Unit, + onNativeUserland: (Boolean) -> Unit, onCursorComposite: (Boolean) -> Unit, onGuestHostname: (String) -> Unit, onClientCore: (Int, Boolean) -> Unit, @@ -187,9 +189,18 @@ fun PerformancePage( ) ToggleRow( host, "fastpath", stringResource(R.string.perf_fastpath), - if (prootNoSeccomp) stringResource(R.string.perf_fastpath_needs_seccomp) - else stringResource(R.string.perf_fastpath_hint), - prootFastPath && !prootNoSeccomp, enabled = !prootNoSeccomp, onChange = onProotFastPath, + when { + nativeUserland -> stringResource(R.string.perf_fastpath_needs_proot) + prootNoSeccomp -> stringResource(R.string.perf_fastpath_needs_seccomp) + else -> stringResource(R.string.perf_fastpath_hint) + }, + prootFastPath && !prootNoSeccomp && !nativeUserland, enabled = !prootNoSeccomp && !nativeUserland, + onChange = onProotFastPath, + ) + ToggleRow( + host, "nativeuserland", stringResource(R.string.perf_native_userland), + stringResource(R.string.perf_native_userland_hint), + nativeUserland, onChange = onNativeUserland, ) ToggleRow( host, "cursorcomposite", stringResource(R.string.perf_cursor_composite), diff --git a/app/src/main/java/com/droiddeck/launcher/ui/PhysicalShortcutsDialog.kt b/app/src/main/java/com/droiddeck/launcher/ui/PhysicalShortcutsDialog.kt index 7331d49fb0..243b1a0bc8 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/PhysicalShortcutsDialog.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/PhysicalShortcutsDialog.kt @@ -36,6 +36,7 @@ import androidx.compose.ui.window.DialogProperties import androidx.compose.ui.window.DialogWindowProvider import com.droiddeck.launcher.R import com.droiddeck.launcher.input.PhysicalShortcuts +import com.droiddeck.launcher.session.SessionPrefs @Composable internal fun PhysicalShortcutRows( @@ -48,14 +49,22 @@ internal fun PhysicalShortcutRows( Column { for (action in PhysicalShortcuts.Action.entries) { val assigned = bindings.filter { it.action == action } + val inverted = SessionPrefs.backActionsInverted(context) + val default = stringResource(when (action) { + PhysicalShortcuts.Action.STEAM -> R.string.input_shortcut_guide + PhysicalShortcuts.Action.QAM -> if (inverted) R.string.input_shortcut_back_once else R.string.input_shortcut_back_twice + PhysicalShortcuts.Action.DROIDDECK -> if (inverted) R.string.input_shortcut_back_modes else R.string.input_shortcut_back_once + }) SettingsRow( stringResource(action.label), - assigned.joinToString { "${it.button.label} · ${it.button.name}" }.ifEmpty { null }, + assigned.joinToString { "${it.button.label} · ${it.button.name}" }.ifEmpty { + if (action == PhysicalShortcuts.Action.QAM && SessionPrefs.backHold(context)) context.getString(R.string.input_shortcut_hold_hint) else null + }, ) { SecondaryButton( - stringResource(if (assigned.isEmpty()) R.string.game_settings_default else R.string.ctrl_custom), + if (assigned.isEmpty()) default else assigned.map { it.button.label }.distinct().joinToString(" / "), compact = true, - modifier = modifierFor(action).width(LocalChipMinWidth.current) + modifier = modifierFor(action).widthIn(min = LocalChipMinWidth.current, max = 240.dp) .testTag("shortcuts-assign-${action.name}"), ) { editing = action } } @@ -165,6 +174,12 @@ internal fun PhysicalShortcutsDialog( modifier = Modifier.testTag("shortcuts-remove"), ) } + SecondaryButton(stringResource(R.string.input_restore_shortcut), enabled = bindings.any { it.action == action }, + modifier = Modifier.testTag("shortcuts-restore-default")) { + PhysicalShortcuts.restoreDefault(context, action) + onChanged() + onDismiss() + } SecondaryButton(stringResource(R.string.common_close), modifier = Modifier.testTag("shortcuts-cancel"), onClick = onDismiss) } @@ -174,7 +189,7 @@ internal fun PhysicalShortcutsDialog( } @Composable -private fun CaptureShortcutKeys(onKey: (KeyEvent) -> Boolean) { +internal fun CaptureShortcutKeys(onKey: (KeyEvent) -> Boolean) { val currentOnKey by rememberUpdatedState(onKey) val window = (LocalView.current.parent as DialogWindowProvider).window DisposableEffect(window) { diff --git a/app/src/main/java/com/droiddeck/launcher/ui/ScreenKeyEditor.kt b/app/src/main/java/com/droiddeck/launcher/ui/ScreenKeyEditor.kt deleted file mode 100644 index 94e1a5ff27..0000000000 --- a/app/src/main/java/com/droiddeck/launcher/ui/ScreenKeyEditor.kt +++ /dev/null @@ -1,368 +0,0 @@ -package com.droiddeck.launcher.ui - -import android.content.Context -import android.graphics.Bitmap -import android.graphics.BitmapFactory -import android.net.Uri -import androidx.activity.compose.rememberLauncherForActivityResult -import androidx.activity.result.contract.ActivityResultContracts -import androidx.compose.foundation.BorderStroke -import androidx.compose.foundation.background -import androidx.compose.foundation.clickable -import androidx.compose.foundation.gestures.detectDragGestures -import androidx.compose.foundation.layout.Arrangement -import androidx.compose.foundation.layout.Box -import androidx.compose.foundation.layout.Column -import androidx.compose.foundation.layout.Row -import androidx.compose.foundation.layout.fillMaxSize -import androidx.compose.foundation.layout.fillMaxWidth -import androidx.compose.foundation.layout.height -import androidx.compose.foundation.layout.offset -import androidx.compose.foundation.layout.padding -import androidx.compose.foundation.layout.systemBarsPadding -import androidx.compose.foundation.layout.width -import androidx.compose.foundation.layout.widthIn -import androidx.compose.foundation.shape.CircleShape -import androidx.compose.foundation.shape.RoundedCornerShape -import androidx.compose.foundation.text.BasicTextField -import androidx.compose.foundation.text.KeyboardOptions -import androidx.compose.material.icons.Icons -import androidx.compose.material.icons.filled.Close -import androidx.compose.material.icons.filled.Delete -import androidx.compose.material.icons.filled.DragIndicator -import androidx.compose.material.icons.filled.Image -import androidx.compose.material.icons.filled.Keyboard -import androidx.compose.material3.Icon -import androidx.compose.material3.IconButton -import androidx.compose.material3.MaterialTheme -import androidx.compose.material3.Slider -import androidx.compose.material3.Surface -import androidx.compose.material3.Switch -import androidx.compose.material3.Text -import androidx.compose.runtime.Composable -import androidx.compose.runtime.getValue -import androidx.compose.runtime.mutableStateOf -import androidx.compose.runtime.remember -import androidx.compose.runtime.setValue -import androidx.compose.ui.Alignment -import androidx.compose.ui.Modifier -import androidx.compose.ui.geometry.Offset -import androidx.compose.ui.focus.onFocusChanged -import androidx.compose.ui.input.pointer.pointerInput -import androidx.compose.ui.platform.LocalConfiguration -import androidx.compose.ui.platform.LocalContext -import androidx.compose.ui.platform.LocalDensity -import androidx.compose.ui.res.stringResource -import androidx.compose.ui.text.TextStyle -import androidx.compose.ui.text.font.FontWeight -import androidx.compose.ui.text.input.ImeAction -import androidx.compose.ui.text.style.TextOverflow -import androidx.compose.ui.unit.IntOffset -import androidx.compose.ui.unit.dp -import androidx.compose.ui.unit.sp -import com.droiddeck.launcher.R -import com.droiddeck.launcher.input.SCREEN_KEY_CIRCLE -import com.droiddeck.launcher.input.SCREEN_KEY_DPAD -import com.droiddeck.launcher.input.SCREEN_KEY_PILL -import com.droiddeck.launcher.input.SCREEN_KEY_RECT -import com.droiddeck.launcher.input.SCREEN_KEY_ROUND -import com.droiddeck.launcher.input.SCREEN_KEY_STICK -import com.droiddeck.launcher.input.SCREEN_KEY_TRIGGER -import com.droiddeck.launcher.input.ScreenKeys -import com.droiddeck.launcher.input.pcKeyLabel -import com.droiddeck.launcher.input.screenKeyDirections -import java.io.File -import kotlin.math.roundToInt - -private val PANEL_WIDTH = 600.dp -private val CHIP_PADDING = androidx.compose.foundation.layout.PaddingValues(horizontal = 6.dp, vertical = 2.dp) - -@Composable -fun ScreenKeyEditor( - codes: List, - keyY: Float, - size: Int, - opacity: Float, - shape: Int, - toggle: Boolean, - labels: Boolean, - text: String, - hasImage: Boolean, - onResize: (Int) -> Unit, - onOpacity: (Float) -> Unit, - onShape: (Int) -> Unit, - onToggle: (Boolean) -> Unit, - onLabels: (Boolean) -> Unit, - onText: (String) -> Unit, - onImage: (String) -> Unit, - onClearImage: () -> Unit, - onBind: () -> Unit, - onTextFocus: () -> Unit, - onTextBlur: () -> Unit, - onDirection: (Int) -> Unit, - onRemove: () -> Unit, - onDismiss: () -> Unit, -) { - val p = LocalPalette.current - val context = LocalContext.current - var offset by remember(codes) { mutableStateOf(Offset.Zero) } - val density = LocalDensity.current - val screen = LocalConfiguration.current - val panelWidthPx = with(density) { PANEL_WIDTH.toPx() } - val screenWidthPx = screen.screenWidthDp * density.density - val screenHeightPx = screen.screenHeightDp * density.density - val atTop = keyY > 0.45f - val button = shape <= SCREEN_KEY_RECT - val directions = screenKeyDirections(shape) - val pickImage = rememberLauncherForActivityResult(ActivityResultContracts.GetContent()) { uri -> - if (uri != null) importImage(context, uri, codes)?.let(onImage) - } - Box( - Modifier.fillMaxSize().systemBarsPadding().padding(horizontal = 10.dp, vertical = 6.dp), - contentAlignment = if (atTop) Alignment.TopCenter else Alignment.BottomCenter, - ) { - Surface( - Modifier.widthIn(max = PANEL_WIDTH).fillMaxWidth() - .offset { IntOffset(offset.x.roundToInt(), offset.y.roundToInt()) }, - shape = RoundedCornerShape(18.dp), - color = p.background, contentColor = p.onBackground, border = BorderStroke(1.dp, p.line2), - ) { - Column(Modifier.padding(start = 10.dp, end = 6.dp, top = 2.dp, bottom = 6.dp)) { - Row(Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) { - Icon( - Icons.Default.DragIndicator, null, - tint = p.onBackground.copy(alpha = 0.45f), - modifier = Modifier.pointerInput(panelWidthPx, screenWidthPx, screenHeightPx) { - detectDragGestures { change, drag -> - change.consume() - offset = Offset( - (offset.x + drag.x).coerceIn( - -(screenWidthPx - panelWidthPx) / 2f + 8f, - (screenWidthPx - panelWidthPx) / 2f - 8f, - ), - (offset.y + drag.y).coerceIn( - -screenHeightPx * if (atTop) 0f else 0.75f, - screenHeightPx * if (atTop) 0.75f else 0f, - ), - ) - } - }, - ) - Box( - Modifier.weight(1f).height(36.dp) - .background(p.line2.copy(alpha = 0.24f), RoundedCornerShape(9.dp)) - .padding(horizontal = 10.dp), - contentAlignment = Alignment.CenterStart, - ) { - BasicTextField( - value = text, - onValueChange = { onText(it.take(24)) }, - singleLine = true, - textStyle = TextStyle( - color = p.onBackground, fontSize = 13.sp, - fontWeight = FontWeight.Medium, - ), - cursorBrush = androidx.compose.ui.graphics.SolidColor(p.primary), - keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done), - modifier = Modifier.fillMaxWidth().onFocusChanged { - if (it.isFocused) onTextFocus() else onTextBlur() - }.focusGlide(), - ) - if (text.isEmpty()) { - Text( - stringResource(R.string.screen_key_text), - color = p.onBackground.copy(alpha = 0.4f), - style = MaterialTheme.typography.bodySmall, - ) - } - } - if (button) { - IconButton(onClick = onBind, modifier = Modifier.width(38.dp).focusGlide(CircleShape)) { - Icon( - Icons.Default.Keyboard, stringResource(R.string.screen_key_bind), - tint = p.onBackground.copy(alpha = 0.8f), - ) - } - } - IconButton(onClick = { pickImage.launch("image/*") }, modifier = Modifier.width(38.dp).focusGlide(CircleShape)) { - Icon( - Icons.Default.Image, stringResource(R.string.screen_key_image), - tint = p.onBackground.copy(alpha = 0.8f), - ) - } - if (hasImage) IconButton(onClick = onClearImage, modifier = Modifier.width(34.dp).focusGlide(CircleShape)) { - Icon(Icons.Default.Close, stringResource(R.string.screen_key_image_clear), tint = p.error) - } - IconButton(onClick = onRemove, modifier = Modifier.width(38.dp).focusGlide(CircleShape)) { - Icon(Icons.Default.Delete, stringResource(R.string.common_delete), tint = p.error) - } - IconButton(onClick = onDismiss, modifier = Modifier.width(34.dp).focusGlide(CircleShape)) { - Icon(Icons.Default.Close, null, tint = p.onBackground) - } - } - Row( - Modifier.fillMaxWidth().padding(vertical = 2.dp), - horizontalArrangement = Arrangement.spacedBy(4.dp), - verticalAlignment = Alignment.CenterVertically, - ) { - listOf>( - (if (button) SCREEN_KEY_PILL else null) to R.string.screen_key_type_button, - SCREEN_KEY_TRIGGER to R.string.screen_key_shape_trigger, - SCREEN_KEY_DPAD to R.string.touch_dpad, - SCREEN_KEY_STICK to R.string.deck_surface_stick, - ).forEach { (value, label) -> - val selected = if (value == null) button else shape == value - Chip(stringResource(label), selected, Modifier.weight(1f)) { - onShape(if (value == null) (if (button) shape else SCREEN_KEY_PILL) else value) - } - } - Box(Modifier.width(1.dp).height(20.dp).background(p.line2)) - listOf( - SCREEN_KEY_PILL to R.string.screen_key_shape_pill, - SCREEN_KEY_ROUND to R.string.screen_key_shape_round, - SCREEN_KEY_CIRCLE to R.string.screen_key_shape_circle, - SCREEN_KEY_RECT to R.string.screen_key_shape_rect, - ).forEach { (value, label) -> - Chip( - stringResource(label), button && shape == value, Modifier.weight(1f), enabled = button, - ) { onShape(value) } - } - Box(Modifier.width(1.dp).height(20.dp).background(p.line2)) - Text( - stringResource(R.string.screen_key_toggle), Modifier.padding(start = 6.dp, end = 2.dp), - style = MaterialTheme.typography.bodySmall, maxLines = 1, - ) - Switch(checked = toggle, onCheckedChange = onToggle, modifier = Modifier.height(30.dp).focusGlide()) - } - if (directions.isNotEmpty()) { - Row( - Modifier.fillMaxWidth().padding(vertical = 2.dp), - horizontalArrangement = Arrangement.spacedBy(4.dp), - verticalAlignment = Alignment.CenterVertically, - ) { - directions.forEachIndexed { index, arrow -> - val code = codes.getOrNull(index) ?: 0 - Chip( - if (code == 0) arrow else "$arrow ${pcKeyLabel(context, code)}", - code != 0, Modifier.weight(1f), - ) { onDirection(index) } - } - Text( - stringResource(R.string.screen_key_text), Modifier.padding(start = 6.dp, end = 2.dp), - style = MaterialTheme.typography.bodySmall, maxLines = 1, - ) - Switch(checked = labels, onCheckedChange = onLabels, modifier = Modifier.height(30.dp).focusGlide()) - } - } - Row( - Modifier.fillMaxWidth().padding(top = 2.dp), - horizontalArrangement = Arrangement.spacedBy(6.dp), - verticalAlignment = Alignment.CenterVertically, - ) { - KeySlider( - label = stringResource(R.string.screen_key_size), - value = "$size", - current = size.toFloat(), - min = ScreenKeys.MIN_SIZE.toFloat(), - max = ScreenKeys.MAX_SIZE.toFloat(), - modifier = Modifier.weight(1f), - ) { onResize(it.roundToInt()) } - KeySlider( - label = stringResource(R.string.screen_key_opacity), - value = "${(opacity * 100).roundToInt()}%", - current = opacity, - min = 0.2f, - max = 1f, - modifier = Modifier.weight(1f), - onChange = onOpacity, - ) - } - } - } - } -} - -@Composable -private fun KeySlider( - label: String, - value: String, - current: Float, - min: Float, - max: Float, - modifier: Modifier, - onChange: (Float) -> Unit, -) { - Row(modifier, verticalAlignment = Alignment.CenterVertically) { - Text(label, style = MaterialTheme.typography.bodySmall, maxLines = 1) - Slider(current, onChange, Modifier.weight(1f).height(28.dp).focusGlide(), valueRange = min..max) - Text( - value, Modifier.width(32.dp), style = MaterialTheme.typography.bodySmall, - fontWeight = FontWeight.SemiBold, maxLines = 1, - ) - } -} - -@Composable -private fun Chip( - label: String, - selected: Boolean, - modifier: Modifier = Modifier, - enabled: Boolean = true, - onClick: () -> Unit, -) { - val p = LocalPalette.current - Box( - modifier - .background( - if (selected) p.primary.copy(alpha = 0.18f) else p.line2.copy(alpha = 0.28f), - CircleShape, - ) - .focusGlide(RoundedCornerShape(8.dp)).clickable(enabled = enabled, onClick = onClick) - .padding(vertical = 6.dp), - contentAlignment = Alignment.Center, - ) { - Text( - label, style = MaterialTheme.typography.labelSmall, maxLines = 1, overflow = TextOverflow.Ellipsis, - color = when { - !enabled -> p.onBackground.copy(alpha = 0.28f) - selected -> p.primary - else -> p.onBackground.copy(alpha = 0.7f) - }, - ) - } -} - -private fun importImage(context: Context, uri: Uri, codes: List): String? { - val dir = File(context.filesDir, ScreenKeys.IMAGE_DIR).apply { mkdirs() } - val name = "key_" + codes.joinToString("_") + "_" + System.currentTimeMillis() + ".png" - val longest = (ScreenKeys.MAX_SIZE * context.resources.displayMetrics.density).toInt() - return runCatching { - val bounds = BitmapFactory.Options().apply { inJustDecodeBounds = true } - context.contentResolver.openInputStream(uri)?.use { BitmapFactory.decodeStream(it, null, bounds) } - val source = maxOf(bounds.outWidth, bounds.outHeight) - if (source <= 0) return@runCatching null - var sample = 1 - while (source / (sample * 2) >= longest) sample *= 2 - val bitmap = context.contentResolver.openInputStream(uri)?.use { - BitmapFactory.decodeStream(it, null, BitmapFactory.Options().apply { inSampleSize = sample }) - } ?: return@runCatching null - val scale = longest.toFloat() / maxOf(bitmap.width, bitmap.height) - val scaled = if (scale < 1f) { - Bitmap.createScaledBitmap( - bitmap, - (bitmap.width * scale).toInt().coerceAtLeast(1), - (bitmap.height * scale).toInt().coerceAtLeast(1), - true, - ) - } else { - bitmap - } - try { - val saved = File(dir, name).outputStream().use { scaled.compress(Bitmap.CompressFormat.PNG, 100, it) } - if (saved) name else null - } finally { - if (scaled !== bitmap) bitmap.recycle() - scaled.recycle() - } - }.getOrNull().also { if (it == null) File(dir, name).delete() } -} diff --git a/app/src/main/java/com/droiddeck/launcher/ui/ScreenLayoutPanel.kt b/app/src/main/java/com/droiddeck/launcher/ui/ScreenLayoutPanel.kt deleted file mode 100644 index 7bc78f7105..0000000000 --- a/app/src/main/java/com/droiddeck/launcher/ui/ScreenLayoutPanel.kt +++ /dev/null @@ -1,139 +0,0 @@ -package com.droiddeck.launcher.ui - -import androidx.compose.foundation.shape.CircleShape -import androidx.activity.compose.rememberLauncherForActivityResult -import androidx.activity.result.contract.ActivityResultContracts -import androidx.compose.foundation.BorderStroke -import androidx.compose.foundation.clickable -import androidx.compose.foundation.layout.Arrangement -import androidx.compose.foundation.layout.Box -import androidx.compose.foundation.layout.Column -import androidx.compose.foundation.layout.Row -import androidx.compose.foundation.layout.fillMaxSize -import androidx.compose.foundation.layout.fillMaxWidth -import androidx.compose.foundation.layout.heightIn -import androidx.compose.foundation.layout.padding -import androidx.compose.foundation.layout.systemBarsPadding -import androidx.compose.foundation.layout.widthIn -import androidx.compose.foundation.rememberScrollState -import androidx.compose.foundation.shape.RoundedCornerShape -import androidx.compose.foundation.text.KeyboardOptions -import androidx.compose.foundation.verticalScroll -import androidx.compose.material.icons.Icons -import androidx.compose.material.icons.filled.Close -import androidx.compose.material3.Button -import androidx.compose.material3.Icon -import androidx.compose.material3.IconButton -import androidx.compose.material3.MaterialTheme -import androidx.compose.material3.OutlinedTextField -import androidx.compose.material3.Surface -import androidx.compose.material3.Text -import androidx.compose.material3.TextButton -import androidx.compose.runtime.Composable -import androidx.compose.runtime.getValue -import androidx.compose.runtime.mutableStateOf -import androidx.compose.runtime.remember -import androidx.compose.runtime.setValue -import androidx.compose.ui.Alignment -import androidx.compose.ui.Modifier -import androidx.compose.ui.platform.LocalContext -import androidx.compose.ui.res.stringResource -import androidx.compose.ui.text.font.FontWeight -import androidx.compose.ui.text.input.ImeAction -import androidx.compose.ui.unit.dp -import com.droiddeck.launcher.ui.GlideDialog as Dialog -import androidx.compose.ui.window.DialogProperties -import com.droiddeck.launcher.R - -@Composable -fun ScreenLayoutPanel( - layouts: List, - onSave: (String) -> Unit, - onLoad: (String) -> Unit, - onDelete: (String) -> Unit, - onImport: (String) -> Unit, - exportText: () -> String, - onDismiss: () -> Unit, -) { - val p = LocalPalette.current - val context = LocalContext.current - var naming by remember { mutableStateOf(false) } - var name by remember { mutableStateOf("") } - - val importer = rememberLauncherForActivityResult(ActivityResultContracts.OpenDocument()) { uri -> - if (uri == null) return@rememberLauncherForActivityResult - val text = runCatching { - context.contentResolver.openInputStream(uri)?.use { it.readBytes().toString(Charsets.UTF_8) } - }.getOrNull() - if (text != null) onImport(text) - } - val exporter = rememberLauncherForActivityResult(ActivityResultContracts.CreateDocument("application/json")) { uri -> - if (uri == null) return@rememberLauncherForActivityResult - runCatching { context.contentResolver.openOutputStream(uri)?.use { it.write(exportText().toByteArray()) } } - } - - Dialog(onDismissRequest = onDismiss, properties = DialogProperties(usePlatformDefaultWidth = false)) { - Box(Modifier.fillMaxSize().systemBarsPadding().padding(16.dp), contentAlignment = Alignment.Center) { - Surface( - Modifier.widthIn(max = 520.dp).fillMaxWidth(), shape = RoundedCornerShape(22.dp), - color = p.background, contentColor = p.onBackground, border = BorderStroke(1.dp, p.line2), - ) { - Column(Modifier.padding(start = 20.dp, end = 8.dp, top = 6.dp, bottom = 12.dp)) { - Row(verticalAlignment = Alignment.CenterVertically) { - Text( - stringResource(R.string.screen_key_layout), Modifier.weight(1f), - style = MaterialTheme.typography.titleMedium, fontWeight = FontWeight.SemiBold, - ) - IconButton(onClick = onDismiss, modifier = Modifier.focusGlide(CircleShape)) { Icon(Icons.Default.Close, null) } - } - Row(Modifier.fillMaxWidth().padding(end = 12.dp), horizontalArrangement = Arrangement.spacedBy(8.dp)) { - TextButton(onClick = { naming = true }, modifier = Modifier.focusGlide(CircleShape)) { Text(stringResource(R.string.screen_key_layout_save)) } - TextButton(onClick = { importer.launch(arrayOf("application/json", "text/plain", "*/*")) }, modifier = Modifier.focusGlide(CircleShape)) { - Text(stringResource(R.string.boot_movie_import)) - } - TextButton(onClick = { exporter.launch("droiddeck-layout.icp") }, modifier = Modifier.focusGlide(CircleShape)) { - Text(stringResource(R.string.screen_key_layout_export)) - } - } - Column(Modifier.padding(end = 12.dp).heightIn(max = 320.dp).verticalScroll(rememberScrollState())) { - layouts.forEach { layout -> - Row(Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) { - Text( - layout, Modifier.weight(1f).clickable { onLoad(layout) }.padding(vertical = 14.dp), - style = MaterialTheme.typography.bodyLarge, - ) - TextButton(onClick = { onDelete(layout) }, modifier = Modifier.focusGlide(CircleShape)) { - Text(stringResource(R.string.common_delete), color = p.error) - } - } - } - } - } - } - } - } - - if (naming) { - Dialog(onDismissRequest = { naming = false }, properties = DialogProperties(usePlatformDefaultWidth = false)) { - Box(Modifier.fillMaxSize().systemBarsPadding().padding(16.dp), contentAlignment = Alignment.Center) { - Surface( - Modifier.widthIn(max = 520.dp).fillMaxWidth(), shape = RoundedCornerShape(22.dp), - color = p.background, contentColor = p.onBackground, border = BorderStroke(1.dp, p.line2), - ) { - Column(Modifier.padding(20.dp), verticalArrangement = Arrangement.spacedBy(12.dp)) { - OutlinedTextField( - name, { name = it.take(24) }, Modifier.fillMaxWidth().focusGlide(), singleLine = true, - keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done), - ) - Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.End) { - TextButton(onClick = { naming = false }, modifier = Modifier.focusGlide(CircleShape)) { Text(stringResource(R.string.common_cancel)) } - Button(onClick = { onSave(name.trim()); naming = false }, enabled = name.isNotBlank(), modifier = Modifier.focusGlide(CircleShape)) { - Text(stringResource(R.string.screen_key_layout_save)) - } - } - } - } - } - } - } -} diff --git a/app/src/main/java/com/droiddeck/launcher/ui/SecondScreenPresentation.kt b/app/src/main/java/com/droiddeck/launcher/ui/SecondScreenPresentation.kt index bd89179663..d9a4967d76 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/SecondScreenPresentation.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/SecondScreenPresentation.kt @@ -18,20 +18,19 @@ import androidx.activity.setViewTreeOnBackPressedDispatcherOwner import androidx.lifecycle.setViewTreeLifecycleOwner import androidx.lifecycle.setViewTreeViewModelStoreOwner import androidx.savedstate.setViewTreeSavedStateRegistryOwner +import com.droiddeck.launcher.input.DeckPanel import com.droiddeck.launcher.input.KeyboardHost import com.droiddeck.launcher.input.PadBridge import com.droiddeck.launcher.input.SecondScreenMode import com.droiddeck.launcher.SessionActivity import com.droiddeck.launcher.session.BottomDesktop +import com.droiddeck.launcher.session.SessionPrefs import com.droiddeck.launcher.session.SessionService import com.droiddeck.launcher.session.SessionState class SecondScreenPresentation( private val sessionActivity: SessionActivity, display: Display, - private val sendPointer: (x: Float, y: Float, width: Float, height: Float) -> Unit, - private val sendButton: (button: Int, pressed: Boolean) -> Unit, - private val sendWheel: (steps: Int) -> Unit, private val onSteamMenu: () -> Unit, private val onQam: () -> Unit, private val onClose: () -> Unit, @@ -42,6 +41,7 @@ class SecondScreenPresentation( private val dashboard: (@androidx.compose.runtime.Composable () -> Unit)? = null, private val onSwap: (() -> Unit)? = null, private val onMenuTouched: () -> Unit = {}, + private val onDeckPanel: (DeckPanel) -> Unit = { SessionPrefs.setDeckPanel(sessionActivity, it) }, ) : Presentation(sessionActivity, display) { private val frame = FrameLayout(context) private var menuView: androidx.compose.ui.platform.ComposeView? = null @@ -58,10 +58,12 @@ class SecondScreenPresentation( private var currentMode = SecondScreenMode.NONE private var keyboardHost: KeyboardHost? = null private val keyboardShown = androidx.compose.runtime.mutableStateOf(false) + private val deckPanel = androidx.compose.runtime.mutableStateOf(DeckPanel.BOTH) private var keyboardPending = false private var imeSeen = false private var fieldKeyboard = false private var terminalPanel: SessionTerminalPanel? = null + private var controlsView: InputSurfaceView? = null private var created = false init { @@ -161,7 +163,7 @@ class SecondScreenPresentation( view.disposeComposition() } - val showsKeyboard: Boolean get() = isShowing && !menuShown && currentMode == SecondScreenMode.KEYBOARD_TRACKPAD + val showsKeyboard: Boolean get() = isShowing && !menuShown && currentMode.controls val showsControls: Boolean get() = isShowing && currentMode != SecondScreenMode.NONE @@ -214,6 +216,7 @@ class SecondScreenPresentation( dropChat() keyboardHost?.hide() keyboardHost = null + controlsView = null dismiss() } @@ -236,6 +239,21 @@ class SecondScreenPresentation( return super.dispatchGenericMotionEvent(event) } + fun refreshControls() { + if (!created || !currentMode.controls) return + val controls = controlsView ?: return + val layout = panelLayout(currentMode) + controls.updateCustomLayout(layout?.elements.orEmpty()) + controls.updateStyle(layout?.style ?: com.droiddeck.launcher.input.InputSurfaceStyle()) + } + + fun showKeyboard(): Boolean { + val host = keyboardHost ?: return false + host.toggle() + keyboardShown.value = host.shown + return true + } + override fun onWindowFocusChanged(hasFocus: Boolean) { super.onWindowFocusChanged(hasFocus) if (hasFocus) { @@ -256,19 +274,29 @@ class SecondScreenPresentation( closeTerminal() closeDashboard() root.removeAllViews() + controlsView = null ownComposeViews() + val requested = currentMode val mode = when { - currentMode == SecondScreenMode.DECK_CONTROLS && !SessionState.deckPad -> SecondScreenMode.NONE - currentMode == SecondScreenMode.DASHBOARD && dashboard == null -> SecondScreenMode.NONE - currentMode == SecondScreenMode.DESKTOP && !BottomDesktop.available() -> SecondScreenMode.NONE - else -> currentMode + requested == SecondScreenMode.DECK_CONTROLS && !SessionState.deckPad -> SecondScreenMode.NONE + requested == SecondScreenMode.DASHBOARD && dashboard == null -> SecondScreenMode.NONE + requested == SecondScreenMode.DESKTOP && !BottomDesktop.available() -> SecondScreenMode.NONE + else -> requested } if (mode == SecondScreenMode.NONE) return when (mode) { - SecondScreenMode.KEYBOARD_TRACKPAD -> renderKeyboardAndTrackpad() SecondScreenMode.TERMINAL -> renderTerminal() - SecondScreenMode.DECK_CONTROLS -> root.addView(DeckControlsPanel(context, onClose, onSteamMenu, onQam, pad, onMenu = onMenu, onSwap = onSwap), weightParams()) + SecondScreenMode.CUSTOM_CONTROLS, SecondScreenMode.KEYBOARD_TRACKPAD, SecondScreenMode.DECK_CONTROLS -> { + if (mode == SecondScreenMode.DECK_CONTROLS) deckPanel.value = SessionPrefs.deckPanel(context) + bar(mode) + val layout = panelLayout(mode) + val controls = InputSurfaceView(context, pad, layout?.elements.orEmpty(), + surfaceStyle = layout?.style ?: com.droiddeck.launcher.input.InputSurfaceStyle()) + controlsView = controls + root.addView(controls, weightParams()) + attachKeyboard() + } SecondScreenMode.DASHBOARD -> renderDashboard() SecondScreenMode.DESKTOP -> { root.addView(BottomDesktopPanel(context, onClose, onMenu, @@ -278,17 +306,40 @@ class SecondScreenPresentation( } } - private fun bar(mode: SecondScreenMode, extras: () -> List = { emptyList() }) { - root.addView(bottomScreenBarView(context) { - sessionBarModel(context.getString(mode.label), onClose, onMenu, onSteamMenu, onQam, extras()) - }, LinearLayout.LayoutParams(-1, -2)) + private fun panelLayout(mode: SecondScreenMode) = com.droiddeck.launcher.input.InputLibraries.read(context) + .panelLayout(SessionState.mode == SessionService.MODE_DESKTOP, mode.panel(context)) + + private fun bar(mode: SecondScreenMode) { + val first = barModel(mode) ?: return + root.addView(bottomScreenBarView(context) { barModel(mode) ?: first }, LinearLayout.LayoutParams(-1, -2)) + } + + internal fun barModel(mode: SecondScreenMode): BottomBarModel? { + val swap = listOfNotNull(onSwap?.let { BarExtra(context.getString(R.string.swap_screens), icon = R.drawable.ic_bottom_swap, tag = "bottom-swap", onClick = it) }) + fun model(extras: List, tabs: BarTabs? = null) = + sessionBarModel(context.getString(mode.label), onClose, onMenu, onSteamMenu, onQam, extras, tabs) + return when (mode) { + SecondScreenMode.KEYBOARD_TRACKPAD -> model(listOf(BarExtra( + context.getString(if (keyboardShown.value) R.string.second_hide_keyboard else R.string.second_show_keyboard), + icon = R.drawable.ic_bottom_keyboard, active = keyboardShown.value, tag = "bottom-keyboard") { showKeyboard() })) + SecondScreenMode.DECK_CONTROLS -> model(swap, deckPanelTabs(context, deckPanel.value, ::selectDeckPanel)) + SecondScreenMode.DASHBOARD, SecondScreenMode.CUSTOM_CONTROLS -> model(swap) + SecondScreenMode.TERMINAL -> model(listOf(BarExtra(context.getString(R.string.drawer_keyboard), icon = R.drawable.ic_bottom_keyboard, + tag = "bottom-keyboard") { terminalPanel?.showKeyboard() })) + else -> null + } + } + + private fun selectDeckPanel(panel: DeckPanel) { + if (deckPanel.value == panel) return + deckPanel.value = panel + onDeckPanel(panel) + refreshControls() } private fun renderDashboard() { val content = dashboard ?: return - bar(SecondScreenMode.DASHBOARD) { - listOfNotNull(onSwap?.let { BarExtra(context.getString(R.string.swap_screens), icon = R.drawable.ic_bottom_swap, tag = "bottom-swap", onClick = it) }) - } + bar(SecondScreenMode.DASHBOARD) val view = androidx.compose.ui.platform.ComposeView(context).apply { setContent { content() } } dashboardView = view root.addView(view, weightParams()) @@ -299,17 +350,7 @@ class SecondScreenPresentation( dashboardView = null } - private fun renderKeyboardAndTrackpad() { - bar(SecondScreenMode.KEYBOARD_TRACKPAD) { - listOf(BarExtra(context.getString(if (keyboardShown.value) R.string.second_hide_keyboard else R.string.second_show_keyboard), - icon = R.drawable.ic_bottom_keyboard, active = keyboardShown.value, tag = "bottom-keyboard") { - keyboardHost?.let { host -> - host.toggle() - keyboardShown.value = host.shown - } - }) - } - root.addView(trackpadView(), weightParams()) + private fun attachKeyboard() { val host = KeyboardHost(context) { event -> val handled = sessionActivity.dispatchKeyEvent(event) if (fieldKeyboard && event.action == KeyEvent.ACTION_UP && @@ -321,14 +362,8 @@ class SecondScreenPresentation( root.addView(host, ViewGroup.LayoutParams(1, 1)) } - private fun trackpadView(): View = SessionTrackpadView(context, sendPointer, sendButton, sendWheel) - private fun renderTerminal() { - bar(SecondScreenMode.TERMINAL) { - listOf(BarExtra(context.getString(R.string.drawer_keyboard), icon = R.drawable.ic_bottom_keyboard, tag = "bottom-keyboard") { - terminalPanel?.showKeyboard() - }) - } + bar(SecondScreenMode.TERMINAL) val panel = SessionTerminalPanel(context) terminalPanel = panel root.addView(panel, weightParams()) diff --git a/app/src/main/java/com/droiddeck/launcher/ui/SessionDrawerPage.kt b/app/src/main/java/com/droiddeck/launcher/ui/SessionDrawerPage.kt index ec13f79997..07289bdf29 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/SessionDrawerPage.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/SessionDrawerPage.kt @@ -1,9 +1,9 @@ package com.droiddeck.launcher.ui internal enum class SessionDrawerPage(val firstControl: String) { - CONTROLLER("touch"), + CONTROLLER("input-profile"), GLASSES("glasses-game"), - SECOND_SCREEN("second-screen:keyboard-trackpad"), + SECOND_SCREEN("second-screen:custom-controls"), DISPLAY("fg"), HUD("hud"), EFFECTS("look"), diff --git a/app/src/main/java/com/droiddeck/launcher/ui/SessionOverlay.kt b/app/src/main/java/com/droiddeck/launcher/ui/SessionOverlay.kt index 17409c85bc..7315817c1c 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/SessionOverlay.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/SessionOverlay.kt @@ -209,6 +209,7 @@ private class DrawerFocus { class DrawerActions( val steam: Boolean, + val desktop: Boolean, val direct: Boolean = false, val title: String? = null, val isHomeApp: Boolean, @@ -218,7 +219,6 @@ class DrawerActions( val frameGen: FrameGen.Mode, val lossless: Lossless.State, val oscMode: String, - val onScreenButtonsVisible: Boolean, val suspendPolicy: String, val steamDownloadsInBackground: Boolean = false, val micAvailable: Boolean = false, @@ -275,9 +275,6 @@ class DrawerActions( val onEditSteamTouch: (() -> Unit)? = null, val controller: com.droiddeck.launcher.input.ControllerPrefs.Settings? = null, val onRumble: (Boolean) -> Unit = {}, - val onSteamButton: (Boolean) -> Unit = {}, - val onQamButton: (Boolean) -> Unit = {}, - val onKeyboardButton: (Boolean) -> Unit = {}, val onSuspendPolicy: (String) -> Unit, val onSteamDownloadsInBackground: (Boolean) -> Unit = {}, val onBackActionsInverted: (Boolean) -> Unit, @@ -290,14 +287,12 @@ class DrawerActions( val directCursor: Boolean = false, val onDirectCursor: (Boolean) -> Unit = {}, val onTouch: (String) -> Unit, + val onInputSettings: () -> Unit = {}, + val onInputSelection: (com.droiddeck.launcher.input.InputRole?) -> Unit = {}, val onGestureSettings: () -> Unit = {}, val touchActive: String = "", val directHoldBehavior: String = MOUSE_BEHAVIOR_CLICK, val onDirectHoldBehavior: (String) -> Unit = {}, - val onScreenKeys: (() -> Unit)? = null, - val onScreenLayouts: (() -> Unit)? = null, - val screenKeysShown: Boolean = true, - val onScreenKeysShown: (Boolean) -> Unit = {}, val onFexPreset: (String) -> Unit, val onUpscaler: (Int) -> Unit = {}, val onUpscaleSharpness: (Int) -> Unit = {}, @@ -318,6 +313,10 @@ class DrawerActions( val onPause: () -> Unit = {}, val onPipAutoEnter: (Boolean) -> Unit = {}, val unfoldedControls: Boolean? = null, + val adaptiveSticks: Boolean? = null, + val onAdaptiveSticks: (Boolean) -> Unit = {}, + val quickButtons: Map = emptyMap(), + val onQuickButton: (com.droiddeck.launcher.input.InputCommand, Boolean) -> Unit = { _, _ -> }, val onUnfoldedControls: (Boolean) -> Unit = {}, val unfoldedControlsWithController: Boolean = false, val onUnfoldedControlsWithController: (Boolean) -> Unit = {}, @@ -595,8 +594,11 @@ internal fun SessionDrawer(open: Boolean, requestedPage: SessionDrawerPage, cont } SessionDrawerPage.CONTROLLER -> { SettingsGroup(stringResource(R.string.drawer_page_controller)) { + InputQuickSettings(host, desktop = a.desktop, onChanged = a.onInputSelection, onEdit = a.onInputSettings) { key -> focus.track(page, key) } + } + SettingsGroup(stringResource(R.string.input_device_settings)) { a.padType?.let { type -> - ChoiceRow(host, "pad-type", stringResource(R.string.mode_controller), null, + ChoiceRow(host, "pad-type", stringResource(R.string.input_virtual_controller), stringResource(R.string.input_virtual_controller_hint), listOf(SessionPrefs.CONTROLLER_DECK to stringResource(R.string.mode_controller_deck), SessionPrefs.CONTROLLER_STEAM to stringResource(R.string.mode_controller_steam), SessionPrefs.CONTROLLER_DS4 to stringResource(R.string.mode_controller_ds4), @@ -604,75 +606,52 @@ internal fun SessionDrawer(open: Boolean, requestedPage: SessionDrawerPage, cont SessionPrefs.CONTROLLER_XBOX360 to stringResource(R.string.mode_controller_x360)), type, chipModifier = focus.track(page, "pad-type"), onPick = a.onPadType) } - ChoiceRow(host, "touch", stringResource(R.string.mode_touch), null, + a.controller?.let { c -> + ToggleRow(host, "rumble", stringResource(R.string.ctrl_rumble), null, c.rumble, + chipModifier = focus.track(page, "rumble"), onChange = a.onRumble) + + } + } + SettingsGroup(stringResource(R.string.input_touchscreen)) { + ChoiceRow(host, "touch", stringResource(R.string.input_touchscreen), stringResource(R.string.input_touchscreen_hint), listOf(SessionPrefs.TOUCH_AUTO to stringResource(R.string.drawer_touch_auto, a.touchAuto), SessionPrefs.TOUCH_PAD to stringResource(R.string.mode_touch_touchpad), SessionPrefs.TOUCH_DIRECT to stringResource(R.string.mode_touch_direct), SessionPrefs.TOUCH_GESTURES to stringResource(R.string.mode_touch_gestures), SessionPrefs.TOUCH_OFF to stringResource(R.string.widgets_off)), a.touchMode, chipModifier = focus.track(page, "touch"), onPick = a.onTouch) if (a.touchActive == SessionPrefs.TOUCH_PAD) RelativeMouseRow(host, a, focus.track(page, "relative-mouse")) if (a.touchActive == SessionPrefs.TOUCH_DIRECT) - ToggleRow(host, "direct-cursor", stringResource(R.string.drawer_direct_cursor), - null, a.directCursor, + ToggleRow(host, "direct-cursor", stringResource(R.string.input_touch_cursor), + stringResource(R.string.input_touch_cursor_hint), a.directCursor, chipModifier = focus.track(page, "direct-cursor"), onChange = a.onDirectCursor) when (a.touchActive) { SessionPrefs.TOUCH_GESTURES -> ActionRow(stringResource(R.string.gesture_settings), null, stringResource(R.string.ctrl_edit), a.onGestureSettings, modifier = focus.track(page, "gesture-settings")) - SessionPrefs.TOUCH_DIRECT -> ChoiceRow(host, "long-press", stringResource(R.string.gesture_long_press), null, + SessionPrefs.TOUCH_DIRECT -> ChoiceRow(host, "long-press", stringResource(R.string.input_touch_hold), stringResource(R.string.input_touch_hold_hint), listOf(MOUSE_BEHAVIOR_NONE to stringResource(R.string.gesture_hold_left), MOUSE_BEHAVIOR_CLICK to stringResource(R.string.gesture_right_click), MOUSE_BEHAVIOR_HOLD to stringResource(R.string.gesture_hold_right)), a.directHoldBehavior, chipModifier = focus.track(page, "long-press"), onPick = a.onDirectHoldBehavior) } - if (a.onScreenKeys != null) { - SettingsRow(stringResource(R.string.screen_keys), null, stacked = narrow) { - Row( - verticalAlignment = Alignment.CenterVertically, - horizontalArrangement = Arrangement.spacedBy(10.dp), - ) { - SecondaryButton(stringResource(R.string.ctrl_edit), enabled = a.screenKeysShown, - modifier = focus.track(page, "screen-keys"), onClick = a.onScreenKeys) - a.onScreenLayouts?.let { open -> - SecondaryButton(stringResource(R.string.screen_key_layout), - modifier = focus.track(page, "screen-key-layouts"), onClick = open) - } - ToggleSwitch(a.screenKeysShown, true, - modifier = focus.track(page, "screen-keys-shown"), onChange = a.onScreenKeysShown) - } - } - } + } + SettingsGroup(stringResource(R.string.input_screen_visibility)) { ChoiceRow(host, "osc", stringResource(R.string.mode_osc), null, if (a.steam) listOf(SessionPrefs.OSC_AUTO to stringResource(R.string.common_auto), SessionPrefs.OSC_ALWAYS to stringResource(R.string.common_always), SessionPrefs.OSC_STEAM_TOUCH to stringResource(R.string.osc_steam_touch), SessionPrefs.OSC_STEAM_QAM to stringResource(R.string.mode_osc_qam), SessionPrefs.OSC_NEVER to stringResource(R.string.common_never)) else listOf(SessionPrefs.OSC_AUTO to stringResource(R.string.common_auto), SessionPrefs.OSC_ALWAYS to stringResource(R.string.common_always), SessionPrefs.OSC_NEVER to stringResource(R.string.common_never)), if (!a.steam && a.oscMode == SessionPrefs.OSC_STEAM_TOUCH) SessionPrefs.OSC_AUTO else a.oscMode, chipModifier = focus.track(page, "osc"), onPick = a.onOsc) + a.adaptiveSticks?.let { on -> + ToggleRow(host, "adaptive-sticks", stringResource(R.string.ctrl_adaptive), null, on, + chipModifier = focus.track(page, "adaptive-sticks"), onChange = a.onAdaptiveSticks) + } + for ((button, shown) in a.quickButtons) { + val key = "quick-${button.name.lowercase()}" + ToggleRow(host, key, stringResource(quickButtonLabel(button)), null, shown, + chipModifier = focus.track(page, key)) { a.onQuickButton(button, it) } + } a.onEditSteamTouch?.let { edit -> ActionRow(stringResource(R.string.osc_steam_layout), stringResource(R.string.osc_steam_layout_hint), stringResource(R.string.ctrl_edit), edit) } - a.controller?.let { c -> - ToggleRow(host, "rumble", stringResource(R.string.ctrl_rumble), null, c.rumble, - chipModifier = focus.track(page, "rumble"), onChange = a.onRumble) - if (a.onScreenButtonsVisible) { - if (a.steam) { - ToggleRow(host, "steam-button", stringResource(R.string.ctrl_steam_button), null, c.steamButton, - chipModifier = focus.track(page, "steam-button"), onChange = a.onSteamButton) - ToggleRow(host, "qam-button", stringResource(R.string.ctrl_qam_button), null, c.qamButton, - chipModifier = focus.track(page, "qam-button"), onChange = a.onQamButton) - } - ToggleRow(host, "keyboard-button", stringResource(R.string.ctrl_keyboard_button), null, c.keyboardButton, - chipModifier = focus.track(page, "keyboard-button"), onChange = a.onKeyboardButton) - } - } - if (a.steam) ToggleRow(host, "back-hold", stringResource(R.string.back_hold), - stringResource(R.string.back_hold_hint), a.backHold, - chipModifier = focus.track(page, "back-hold"), onChange = a.onBackHold) - if (a.steam) ChoiceRow(host, "back-actions", stringResource(R.string.mode_back), null, - listOf(false to stringResource(SessionPrefs.BACK_MENU_THEN_QAM), true to stringResource(SessionPrefs.BACK_QAM_THEN_MENU)), - a.backActionsInverted, chipModifier = focus.track(page, "back-actions"), onPick = a.onBackActionsInverted) - if (a.steam) ChoiceRow(host, "back-double-tap", stringResource(R.string.back_double_tap_window), null, - SessionPrefs.BACK_DOUBLE_TAP_MS_OPTIONS.map { it to stringResource(R.string.back_tap_ms, it) }, - a.backDoubleTapMs, chipModifier = focus.track(page, "back-double-tap"), onPick = a.onBackDoubleTapMs) - PhysicalShortcutRows(modifierFor = { focus.track(page, "physical-shortcut-${it.name}") }) if (a.steam && a.unfoldedControls != null) ToggleRow(host, "unfolded-controls", stringResource(R.string.unfolded_controls), stringResource(R.string.unfolded_controls_hint), a.unfoldedControls, chipModifier = focus.track(page, "unfolded-controls"), onChange = a.onUnfoldedControls) @@ -682,6 +661,18 @@ internal fun SessionDrawer(open: Boolean, requestedPage: SessionDrawerPage, cont chipModifier = focus.track(page, "unfolded-controls-with-controller"), onChange = a.onUnfoldedControlsWithController) } + SettingsGroup(stringResource(R.string.shortcuts_title)) { + if (a.steam) ToggleRow(host, "back-hold", stringResource(R.string.back_hold), + stringResource(R.string.back_hold_hint), a.backHold, + chipModifier = focus.track(page, "back-hold"), onChange = a.onBackHold) + if (a.steam) ChoiceRow(host, "back-actions", stringResource(R.string.input_back_taps), stringResource(R.string.input_back_taps_hint), + listOf(false to stringResource(SessionPrefs.BACK_MENU_THEN_QAM), true to stringResource(SessionPrefs.BACK_QAM_THEN_MENU)), + a.backActionsInverted, chipModifier = focus.track(page, "back-actions"), onPick = a.onBackActionsInverted) + if (a.steam) ChoiceRow(host, "back-double-tap", stringResource(R.string.back_double_tap_window), null, + SessionPrefs.BACK_DOUBLE_TAP_MS_OPTIONS.map { it to stringResource(R.string.back_tap_ms, it) }, + a.backDoubleTapMs, chipModifier = focus.track(page, "back-double-tap"), onPick = a.onBackDoubleTapMs) + PhysicalShortcutRows(modifierFor = { focus.track(page, "physical-shortcut-${it.name}") }) + } SettingsGroup(stringResource(R.string.drawer_latency)) { ToggleRow(host, "low-latency", stringResource(R.string.drawer_low_latency), stringResource(R.string.drawer_low_latency_hint), a.lowLatency, @@ -1175,7 +1166,7 @@ private fun SteamVrRow(a: DrawerActions) { internal fun secondScreenModes(none: Boolean, deckPad: Boolean, dashboard: Boolean, desktop: Boolean): List = (if (none) listOf(SecondScreenMode.NONE) else emptyList()) + listOf(SecondScreenMode.KEYBOARD_TRACKPAD, SecondScreenMode.TERMINAL) + - (if (deckPad) listOf(SecondScreenMode.DECK_CONTROLS) else emptyList()) + + (if (deckPad) listOf(SecondScreenMode.DECK_CONTROLS) else emptyList()) + listOf(SecondScreenMode.CUSTOM_CONTROLS) + (if (dashboard) listOf(SecondScreenMode.DASHBOARD) else emptyList()) + (if (desktop) listOf(SecondScreenMode.DESKTOP) else emptyList()) @@ -1185,6 +1176,8 @@ private fun SecondScreenGroup(host: MenuHost, a: DrawerActions, track: (String) val pal = LocalPalette.current val modes = secondScreenModes(a.secondScreenNone, com.droiddeck.launcher.session.SessionState.deckPad, a.dashboardAvailable, a.desktopAvailable) SettingsGroup(stringResource(R.string.drawer_second_screen_mode)) { + Text(stringResource(R.string.input_second_mode_hint), Modifier.padding(14.dp), + style = MaterialTheme.typography.bodySmall, color = colors.onSurfaceVariant) Column(Modifier.fillMaxWidth().selectableGroup()) { modes.forEach { mode -> val selected = a.secondScreenMode == mode @@ -1254,6 +1247,7 @@ private fun DrawerFexPresetRow(host: MenuHost, a: DrawerActions, modifier: Modif } }, allowInheritance = appId != null, + generalPreset = a.fexPreset, modifier = modifier, ) } diff --git a/app/src/main/java/com/droiddeck/launcher/ui/SessionPanels.kt b/app/src/main/java/com/droiddeck/launcher/ui/SessionPanels.kt index c1e629251b..35d5538bfb 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/SessionPanels.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/SessionPanels.kt @@ -2,21 +2,17 @@ package com.droiddeck.launcher.ui import android.content.ClipboardManager import android.content.Context -import android.graphics.Canvas import android.graphics.Color import android.util.Log import android.view.Gravity import android.view.KeyEvent import android.view.MotionEvent import android.view.View -import android.view.ViewConfiguration import android.view.inputmethod.InputMethodManager import android.widget.HorizontalScrollView import android.widget.LinearLayout import android.widget.TextView import com.droiddeck.launcher.R -import com.droiddeck.launcher.input.PointerGestures -import com.droiddeck.launcher.input.TouchpadGestures import com.droiddeck.launcher.session.SessionService import com.droiddeck.launcher.session.SessionTerminal import com.termux.terminal.TerminalEmulator @@ -25,65 +21,6 @@ import com.termux.terminal.TerminalSessionClient import com.termux.view.TerminalView import com.termux.view.TerminalViewClient -class SessionTrackpadView( - context: Context, - private val sendPointer: (x: Float, y: Float, width: Float, height: Float) -> Unit, - private val sendButton: (button: Int, pressed: Boolean) -> Unit, - private val sendWheel: (steps: Int) -> Unit, -) : View(context) { - private fun dp(value: Int): Int = (value * resources.displayMetrics.density + 0.5f).toInt() - - private val gestures = TouchpadGestures(ViewConfiguration.get(context).scaledTouchSlop.toFloat(), - object : PointerGestures.Listener { - override fun onMove(x: Float, y: Float) { - sendPointer(x, y, width.toFloat(), height.toFloat()) - invalidate() - } - - override fun onButton(button: Int, pressed: Boolean, x: Float, y: Float) { - sendPointer(x, y, width.toFloat(), height.toFloat()) - sendButton(button, pressed) - } - - override fun onWheel(steps: Int) = sendWheel(steps) - override fun onLongPress() { performHapticFeedback(android.view.HapticFeedbackConstants.LONG_PRESS) } - }) - - init { - isFocusable = true - setBackgroundColor(Color.BLACK) - } - - override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) { - gestures.bounds = android.graphics.RectF(0f, 0f, w.toFloat(), h.toFloat()) - if (oldw == 0 || oldh == 0) gestures.place(w / 2f, h / 2f) - } - - private val surface = android.graphics.RectF() - private val fill = android.graphics.Paint(android.graphics.Paint.ANTI_ALIAS_FLAG).apply { color = Color.rgb(0x0A, 0x0B, 0x0D) } - private val edge = android.graphics.Paint(android.graphics.Paint.ANTI_ALIAS_FLAG).apply { - style = android.graphics.Paint.Style.STROKE - color = Color.rgb(0x26, 0x29, 0x2E) - strokeWidth = resources.displayMetrics.density - } - - override fun onDraw(canvas: Canvas) { - val inset = dp(12).toFloat() - surface.set(inset, 0f, width - inset, height - inset) - if (surface.height() <= inset) return - val radius = dp(20).toFloat() - canvas.drawRoundRect(surface, radius, radius, fill) - canvas.drawRoundRect(surface, radius, radius, edge) - } - - override fun onTouchEvent(event: MotionEvent): Boolean = gestures.onTouch(event) - - override fun onDetachedFromWindow() { - gestures.cancel() - super.onDetachedFromWindow() - } -} - class SessionTerminalPanel(context: Context) : LinearLayout(context) { private var terminalSession: TerminalSession? = null private var terminalView: TerminalView? = null @@ -123,7 +60,7 @@ class SessionTerminalPanel(context: Context) : LinearLayout(context) { val keys = LinearLayout(context).apply { orientation = HORIZONTAL; setPadding(dp(12), 0, dp(12), dp(8)) } fun key(label: String, input: String, onClick: ((TextView) -> Unit)? = null) { lateinit var button: TextView - button = DeckControlsPanel.roundButton(context, label, label, { if (onClick != null) onClick(button) else session.write(input) }, wide = true) + button = InputSurfaceView.roundButton(context, label, label, { if (onClick != null) onClick(button) else session.write(input) }, wide = true) if (keys.childCount > 0) keys.addView(View(context), LinearLayout.LayoutParams(dp(8), 1)) keys.addView(button) } diff --git a/app/src/main/java/com/droiddeck/launcher/ui/SettingsWidgets.kt b/app/src/main/java/com/droiddeck/launcher/ui/SettingsWidgets.kt index 2189b26ba1..b5595a52b7 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/SettingsWidgets.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/SettingsWidgets.kt @@ -509,16 +509,23 @@ fun ChoiceRow( stacked: Boolean = false, onPick: (T) -> Unit, ) { + SettingsRow(label, hint, highlighted = host.open == key, hintLines = hintLines, stacked = stacked) { + ChoiceChip(host, key, label, options, selected, enabled, note, chipModifier, onPick) + } +} + +@Composable +internal fun ChoiceChip(host: MenuHost, key: String, label: String, options: List>, selected: T, + enabled: Boolean = true, note: String? = null, modifier: Modifier = Modifier, onPick: (T) -> Unit) { val open = host.open == key - SettingsRow(label, hint, highlighted = open, hintLines = hintLines, stacked = stacked) { - Box { - ValueChip(options.firstOrNull { it.first == selected }?.second ?: "-", open, enabled, modifier = chipModifier.testTag("setting-$key")) { host.open = if (open) null else key } - AnchoredMenu(open, onDismiss = { if (host.open == key) host.open = null }, title = label, note = note) { firstItemFocus -> - options.forEachIndexed { index, (value, text) -> - MenuItem(text, checked = value == selected, focusRequester = if (index == 0) firstItemFocus else null) { - onPick(value) - host.open = null - } + Box { + ValueChip(options.firstOrNull { it.first == selected }?.second ?: "-", open, enabled, + modifier = modifier.testTag("setting-$key")) { host.open = if (open) null else key } + AnchoredMenu(open, onDismiss = { if (host.open == key) host.open = null }, title = label, note = note) { firstItemFocus -> + options.forEachIndexed { index, (value, text) -> + MenuItem(text, checked = value == selected, focusRequester = if (index == 0) firstItemFocus else null) { + onPick(value) + host.open = null } } } @@ -714,6 +721,7 @@ fun SettingsPage( scroll: androidx.compose.foundation.ScrollState? = null, scrollContent: Boolean = true, compactLayout: Boolean = false, + showBack: Boolean = true, header: (@Composable () -> Unit)? = null, modifier: Modifier = Modifier, content: @Composable ColumnScope.() -> Unit, @@ -736,7 +744,7 @@ fun SettingsPage( horizontalArrangement = Arrangement.spacedBy(12.dp), modifier = Modifier.fillMaxWidth(), ) { - BackLink(stringResource(R.string.mode_back), compact = true, onClick = onBack) + if (showBack) BackLink(stringResource(R.string.mode_back), compact = true, onClick = onBack) if (header != null) Box(Modifier.weight(1f)) { header() } } } @@ -746,7 +754,7 @@ fun SettingsPage( verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(12.dp), modifier = Modifier.padding(bottom = 6.dp), ) { - BackLink(stringResource(R.string.mode_back), compact = true, onClick = onBack) + if (showBack) BackLink(stringResource(R.string.mode_back), compact = true, onClick = onBack) Text( title, fontSize = 20.sp, fontWeight = FontWeight.Bold, color = colors.onBackground, maxLines = 1, overflow = TextOverflow.Ellipsis, modifier = Modifier.weight(1f), @@ -758,7 +766,7 @@ fun SettingsPage( } else { Rise(0) { Row(verticalAlignment = Alignment.CenterVertically) { - BackLink(stringResource(R.string.mode_back), compact = compactLayout, onClick = onBack) + if (showBack) BackLink(stringResource(R.string.mode_back), compact = compactLayout, onClick = onBack) if (eyebrow != null) { Spacer(Modifier.width(10.dp)) Eyebrow(eyebrow) diff --git a/app/src/main/java/com/droiddeck/launcher/ui/StatsHud.kt b/app/src/main/java/com/droiddeck/launcher/ui/StatsHud.kt index b14dd39dc3..0ed81319c1 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/StatsHud.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/StatsHud.kt @@ -98,6 +98,7 @@ internal class HudData( val tempColor: (HudMetrics.TempSensor, Int) -> Color, val redC: (HudMetrics.TempSensor) -> Int?, val historySeconds: Int = 0, + val graphEnd: Long = 0L, ) { val frametimeMs: Float? get() = fps?.takeIf { it > 0f }?.let { 1000f / it } val hz: Int get() = if (refreshHz > 0) refreshHz else 60 @@ -268,18 +269,26 @@ private fun lowsText(d: HudData, size: TextUnit, accent: Color, deep: Boolean = private val GRAPH_MIN_GAP = 3.dp @Composable -private fun Graph(values: List, lo: Float, hi: Float, color: Color, modifier: Modifier) { +private fun Graph(values: List, lo: Float, hi: Float, color: Color, modifier: Modifier, end: Long = 0L) { Canvas(modifier) { if (values.size < 2 || hi <= lo) return@Canvas val stride = ceil(GRAPH_MIN_GAP.toPx() * (values.size - 1) / size.width).toInt().coerceAtLeast(1) - val points = if (stride == 1) values else values.chunked(stride) { it.average().toFloat() } - if (points.size < 2) return@Canvas + // Buckets start at multiples of stride counted from the first sample ever drawn, so a sample keeps + // its bucket as the window scrolls; counted from the window's start, every new frame re-paired them. + val skip = Math.floorMod(values.size.toLong() - end, stride.toLong()).toInt() val path = Path() - points.forEachIndexed { i, v -> - val x = i / (points.size - 1f) * size.width - val y = size.height - ((v - lo) / (hi - lo)).coerceIn(0f, 1f) * size.height - if (i == 0) path.moveTo(x, y) else path.lineTo(x, y) + var drawn = 0 + var start = skip + while (start < values.size) { + val stop = minOf(start + stride, values.size) + var sum = 0f + for (i in start until stop) sum += values[i] + val x = (start + stop - 1) / 2f / (values.size - 1f) * size.width + val y = size.height - ((sum / (stop - start) - lo) / (hi - lo)).coerceIn(0f, 1f) * size.height + if (drawn++ == 0) path.moveTo(x, y) else path.lineTo(x, y) + start = stop } + if (drawn < 2) return@Canvas drawPath(path, color.copy(alpha = 0.22f), style = Stroke(width = 6.dp.toPx(), cap = StrokeCap.Round, join = StrokeJoin.Round)) drawPath(path, color, style = Stroke(width = 2.2f.dp.toPx(), cap = StrokeCap.Round, join = StrokeJoin.Round)) } @@ -287,7 +296,7 @@ private fun Graph(values: List, lo: Float, hi: Float, color: Color, modif @Composable private fun FrameGraph(d: HudData, accent: Color, height: Dp) { - Graph(d.graph.toList(), 0f, FrameStats.GRAPH_MAX_MS, lerp(accent, Color.White, 0.3f), Modifier.fillMaxWidth().height(height)) + Graph(d.graph.toList(), 0f, FrameStats.GRAPH_MAX_MS, lerp(accent, Color.White, 0.3f), Modifier.fillMaxWidth().height(height), d.graphEnd) } @Composable @@ -589,7 +598,7 @@ private fun ColumnScope.Pacing(d: HudData, accent: Color) { drawLine(color, Offset(0f, y), Offset(size.width, y), 1.dp.toPx(), pathEffect = dash) } } - Graph(d.graph.toList(), 0f, FrameStats.GRAPH_MAX_MS, graphColor, Modifier.fillMaxSize()) + Graph(d.graph.toList(), 0f, FrameStats.GRAPH_MAX_MS, graphColor, Modifier.fillMaxSize(), d.graphEnd) for ((ms, color) in listOf(target to HudInk.dim, target * 2 to HudInk.amber)) { Small("%.1f ms · %d fps".format(ms, (1000f / ms).roundToInt()), 11.sp, color, Modifier.align(Alignment.TopEnd).offset(y = maxHeight * (1f - ms / FrameStats.GRAPH_MAX_MS) - 17.dp)) diff --git a/app/src/main/java/com/droiddeck/launcher/ui/StoragePage.kt b/app/src/main/java/com/droiddeck/launcher/ui/StoragePage.kt index acf6753bf1..04ff93efc9 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/StoragePage.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/StoragePage.kt @@ -755,7 +755,7 @@ private fun ToolList(tools: List, sessionRunning: Boolean, onRemove: } @Composable -internal fun LowSpaceDialog(free: Long, onFreeUp: () -> Unit, onPlay: () -> Unit, onDismiss: () -> Unit) { +internal fun LowSpaceDialog(free: Long, warn: Boolean, onWarnChange: (Boolean) -> Unit, onFreeUp: () -> Unit, onPlay: () -> Unit, onDismiss: () -> Unit) { val colors = MaterialTheme.colorScheme val pal = LocalPalette.current val report = StorageState.report @@ -770,6 +770,7 @@ internal fun LowSpaceDialog(free: Long, onFreeUp: () -> Unit, onPlay: () -> Unit if (report != null && StorageState.safeBytes(report) > 0) Text( stringResource(R.string.storage_low_safe, gb(StorageState.safeBytes(report))), fontSize = 13.sp, color = pal.good, ) + DialogToggleRow(stringResource(R.string.storage_low_never), !warn) { onWarnChange(!it) } } }, confirmButton = { FocusText(stringResource(R.string.storage_free_up), pal.signal, modifier = Modifier.focusRequester(focus), onClick = onFreeUp) }, diff --git a/app/src/main/java/com/droiddeck/launcher/ui/StoreChips.kt b/app/src/main/java/com/droiddeck/launcher/ui/StoreChips.kt index 1d2aef9d6e..2fea8fc891 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/StoreChips.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/StoreChips.kt @@ -33,6 +33,8 @@ internal fun sourceColours(source: String): SourceColours = when (source) { Store.ITCH.id -> SourceColours(Color(0xFF3A1A1E), Color(0xFFFFB3B3), Color(0xFFFF5C5C)) Store.GAMEJOLT.id -> SourceColours(Color(0xFF232E00), Color(0xFFE3FF66), Color(0xFFCCFF00)) Store.BATTLENET.id -> SourceColours(Color(0xFF0A2440), Color(0xFF9DD1FF), Color(0xFF148EFF)) + Store.UBISOFT.id -> SourceColours(Color(0xFF0B1E44), Color(0xFFA8C6FF), Color(0xFF2E7DFF)) + Store.EA.id -> SourceColours(Color(0xFF3A0E12), Color(0xFFFF9A9A), Color(0xFFFF4747)) Library.SOURCE_STEAM -> SourceColours(Color(0xFF16293D), Color(0xFF7CC4FF), Color(0xFF1A9FFF)) FLATHUB -> SourceColours(Color(0xFF0B2E2B), Color(0xFF9FE3DD), FlathubTeal) else -> SourceColours(Color(0xFF1A1D22), Color(0xFF9AA3AF), Color(0xFF9AA3AF)) @@ -48,6 +50,8 @@ internal fun sourceLabel(source: String): String = when (source) { Store.ITCH.id -> Store.ITCH.label Store.GAMEJOLT.id -> Store.GAMEJOLT.label Store.BATTLENET.id -> Store.BATTLENET.label + Store.UBISOFT.id -> Store.UBISOFT.shortLabel + Store.EA.id -> Store.EA.label else -> stringResource(R.string.source_added) } diff --git a/app/src/main/java/com/droiddeck/launcher/ui/StoresPage.kt b/app/src/main/java/com/droiddeck/launcher/ui/StoresPage.kt index c5f31f5d32..7bc7c186d0 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/StoresPage.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/StoresPage.kt @@ -1157,6 +1157,8 @@ internal fun storePalette(base: Palette, store: Store?): Palette { Store.ITCH -> Color(0xFFFF5C5C) to Color(0xFFD93B3B) Store.GAMEJOLT -> Color(0xFFCCFF00) to Color(0xFFA3CC00) Store.BATTLENET -> Color(0xFF148EFF) to Color(0xFF0B6BD3) + Store.UBISOFT -> Color(0xFF2E7DFF) to Color(0xFF1A5BD6) + Store.EA -> Color(0xFFFF4747) to Color(0xFFE03030) null -> return base } val on = if (accent.luminance() > 0.18f) Color(0xFF0B0B0D) else Color.White diff --git a/app/src/main/java/com/droiddeck/launcher/ui/Theme.kt b/app/src/main/java/com/droiddeck/launcher/ui/Theme.kt index cb6d5ed22f..2e2c1049e4 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/Theme.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/Theme.kt @@ -58,6 +58,7 @@ object Themes { const val RGB_SPEEDS = 10 const val DEFAULT_RGB_SPEED = 3 const val RGB_HUE_STEP = 3f + const val WHITE_RED = "white_red" var rgbSpeed by mutableStateOf(DEFAULT_RGB_SPEED) private set @@ -127,6 +128,13 @@ object Themes { primary = Color(0xFF2E86FF), primary2 = Color(0xFF2684FF), onPrimary = Color(0xFF03111F), signal = Color(0xFF2E86FF), good = Color(0xFF4CD37F), ), + Palette( + WHITE_RED, R.string.theme_white_red, R.string.theme_white_red_detail, + background = Color(0xFFF8F8F8), surface = Color(0xFFFFFFFF), surfaceVariant = Color(0xFFF0F0F0), line = Color(0xFFDDDDDD), line2 = Color(0xFFC8C8C8), + onBackground = Color(0xFF191919), onSurfaceVariant = Color(0xFF555555), + primary = Color(0xFFD62828), primary2 = Color(0xFFB71C1C), onPrimary = Color(0xFFFFFFFF), signal = Color(0xFFD62828), + good = Color(0xFF248548), + ), innerGamer(0f), ThemeColors.palette(DEFAULT_PRIMARY, DEFAULT_SECONDARY, R.string.theme_gradient, R.string.theme_gradient_detail), ) diff --git a/app/src/main/java/com/droiddeck/launcher/ui/UpdatesPage.kt b/app/src/main/java/com/droiddeck/launcher/ui/UpdatesPage.kt index dd73939a7d..a1af06a863 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/UpdatesPage.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/UpdatesPage.kt @@ -120,11 +120,11 @@ internal fun UpdatesPage(s: FrontEndState, a: FrontEndActions, modifier: Modifie StatusPanel(s, u, ua, me) Box(Modifier.height(18.dp)) ChannelLabel() - ChannelPicker(u, ua) + ChannelPicker(u, ua, s.sessionRunning) } else { ChannelLabel() Row(horizontalArrangement = Arrangement.spacedBy(18.dp)) { - Box(Modifier.weight(1f)) { ChannelPicker(u, ua) } + Box(Modifier.weight(1f)) { ChannelPicker(u, ua, s.sessionRunning) } Box(Modifier.weight(1.15f)) { StatusPanel(s, u, ua, me) } } } @@ -262,7 +262,7 @@ private fun ChannelLabel() { } @Composable -private fun ChannelPicker(u: UpdatesState, ua: UpdatesActions) { +private fun ChannelPicker(u: UpdatesState, ua: UpdatesActions, sessionRunning: Boolean) { val colors = MaterialTheme.colorScheme val catalog = u.catalog val tests = catalog?.tests.orEmpty() @@ -285,6 +285,7 @@ private fun ChannelPicker(u: UpdatesState, ua: UpdatesActions) { tests.forEach { t -> TestRow(t, u.follow.pr == t.pr) { ua.onFollow(Follow(Channel.TEST, t.pr)) } } } } + LabBuilds(sessionRunning) } } diff --git a/app/src/main/java/com/droiddeck/launcher/ui/WinComponentsEditor.kt b/app/src/main/java/com/droiddeck/launcher/ui/WinComponentsEditor.kt index 4dff5d4d60..3fd3c9477c 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/WinComponentsEditor.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/WinComponentsEditor.kt @@ -56,7 +56,7 @@ import java.io.File private fun installable(name: String, all: Map): String = WinComponents.installable(name, all) @Composable -private fun originName(origin: String): String = if (origin == WinCompSources.Origin.XBOX.name) "Xbox" else stringResource( +private fun originName(origin: String): String = if (origin == WinCompSources.Origin.XBOX.name) "Xbox" else if (origin == WinCompSources.Origin.UBISOFT.name) "Ubisoft Connect" else stringResource( when (origin) { WinCompSources.Origin.GOG.name -> R.string.wincomp_src_gog WinCompSources.Origin.EPIC.name -> R.string.wincomp_src_epic @@ -71,6 +71,7 @@ private fun autoReason(r: WinComponents.AutoReason): String = when (r.kind) { WinCompSources.Origin.GOG.name -> stringResource(R.string.wincomp_auto_gog) WinCompSources.Origin.EPIC.name -> stringResource(R.string.wincomp_auto_epic) WinCompSources.Origin.AMAZON.name -> stringResource(R.string.wincomp_auto_amazon) + WinCompSources.Origin.UBISOFT.name -> "From Ubisoft's install steps for this game" WinCompSources.Origin.XBOX.name -> "Xbox games install it with the game (${r.args.getOrElse(0) { "VC14" }})" else -> "" } diff --git a/app/src/main/res/drawable/ic_input_quick_access.xml b/app/src/main/res/drawable/ic_input_quick_access.xml new file mode 100644 index 0000000000..1a1a6943e1 --- /dev/null +++ b/app/src/main/res/drawable/ic_input_quick_access.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/drawable/ic_input_steam.xml b/app/src/main/res/drawable/ic_input_steam.xml new file mode 100644 index 0000000000..4a423ce320 --- /dev/null +++ b/app/src/main/res/drawable/ic_input_steam.xml @@ -0,0 +1,8 @@ + + + + + + + diff --git a/app/src/main/res/values-ar/strings.xml b/app/src/main/res/values-ar/strings.xml index 88538697f1..c60023b2af 100644 --- a/app/src/main/res/values-ar/strings.xml +++ b/app/src/main/res/values-ar/strings.xml @@ -261,24 +261,8 @@ لمس Steam (تجريبي) تبدأ عناصر التحكم باللمس من Steam مع جلسة Steam التالية تخطيط لمس Steam - مفاتيح الشاشة - إضافة مفتاح - نص - الحجم - العتامة - مفتاح - زناد - تبديل تصدير - كبسولة - مستدير - دائرة - مستطيل - مفتاح صورة - مسح - التخطيط - حفظ انقل عناصر التحكم باللمس لهذه اللعبة أو غيّر حجمها أو أخفها؛ ويتم الحفظ في Steam وحدة التحكم كيف يرى Steam وحدة التحكم لديك. يُطبَّق في الجلسة التالية. @@ -563,6 +547,7 @@ لا يستخدمه أي DroidDeck-Proton تنتظر تحديثات Auto اتصالًا. تعذّر البحث عن إصدارات جديدة. + تعذّر تثبيت %1$s. تنتظر الإصدارات الجديدة اتصالًا غير محدود، وإيقاف توفير البطارية، وجهازًا غير ساخن. تم تحديث %1$s إلى %2$s. أغلق %1$s %2$s لعبة خلال ثوانٍ مرتين، لذا عاد Auto إلى %3$s. @@ -764,7 +749,6 @@ تلقائي تشغيل إيقاف - المؤشر لوحة مفاتيح الكمبيوتر Android الشاشة الثانية @@ -1123,43 +1107,25 @@ 3 · ADB من كمبيوتر (كخيار أخير) فقط إذا كان تصحيح الأخطاء اللاسلكي غير متاح. 3 · ADB من كمبيوتر (كخيار أخير) - متى تظهر عناصر التحكم باللمس في الجلسة اللون - درجة لون الأزرار على الشاشة مخصّص العتامة %1$d%% حجم الأزرار - 100% هو الحجم القياسي - ضغط العصا - انقر مرتين مع الاستمرار على عصا للحصول على L3 أو R3 عصي تكيّفية اهتزاز اللعبة المنطقة الميتة لعصا التحكم زر Steam زر QAM زر لوحة المفاتيح - اضغط على Select لفتح قائمة Steam - بدلًا من View/Select. أجهزة Joy-Con كوحدات تحكم VR أثناء تشغيل SteamVR، يصبح كل Joy-Con يدًا. وجّهه إلى الأمام واضغط مطولًا على Home أو Capture لإعادة توسيطه. الرسوم المتحركة - تظهر العصي حيث تلمس وتُخفى عند الرفع + التحذير عند انخفاض مساحة التخزين + اسأل قبل تشغيل Steam عندما تكون المساحة الحرة أقل من 10 جيجابايت. التخطيط - تم حفظ مواضع مخصّصة - مهيّأ لهذه الشاشة تعديل إعادة ضبط - تعيين الأزرار - يرسل كل زر إدخاله الخاص - تمت إعادة تعيين %1$d من %2$d أزرار - ضبط - إعادة ضبط وحدة التحكم - يستعيد كل إعدادات وحدة التحكم والتخطيط - ما يرسله كل زر على الشاشة إلى اللعبة. يؤدي إخفاؤه إلى إزالة الزر. - أزرار الشاشة - الافتراضية - إعادة ضبط التعيين تطبيقات Android %1$d تطبيق لم يتم العثور على تطبيقات Android قابلة للتشغيل. @@ -1243,7 +1209,6 @@ لا توجد برامج تشغيل مُدرَجة حتى الآن. حدِّث للبحث عنها. إخفاء برامج التشغيل لوحدات GPU الأخرى إظهار برامج التشغيل لوحدات GPU الأخرى - متقدّم توافق UBWC جرّب التشغيل أو الإيقاف إذا ظهرت خطوط أو وميض في Steam أو Android. يُطبَّق على برنامجَي التشغيل كليهما بعد إعادة تشغيل DroidDeck. أعِد تشغيل DroidDeck للتطبيق. @@ -1283,16 +1248,8 @@ يد التحكّم عصا التحكّم لوحة اللمس - التبديل بين عصا التحكّم ولوحة اللمس - المزيد من عناصر تحكّم Deck - اللوحات إغلاق عناصر التحكّم على الشاشة الثانية فتح قائمة Steam - يسار · علوي - يسار · سفلي - يمين · علوي - يمين · سفلي - نقر لوحة اللمس اليمنى لوحة اللمس اليسرى لوحة اللمس @@ -1874,6 +1831,7 @@ كهرماني أحمر وردي + رمادي الزر A الزر B الزر X @@ -1891,9 +1849,7 @@ لوحة الاتجاهات لليسار لوحة الاتجاهات لليمين مخفي - اسحب للنقل محاذاة - حفظ لا شيء لوحة المفاتيح + لوحة اللمس الوحدة الطرفية @@ -1906,13 +1862,6 @@ جارٍ تنزيل ذاكرة المظلِّلات المؤقتة متوقف مؤقتًا بانتظار البدء - الإشعارات - الشاشة السفلية لـ DroidDeck - اعرض الرسائل والإشعارات الأخرى هنا أثناء اللعب. تبقى على هذه الشاشة فقط ولا تُحفظ. يطلب Android الإذن مرة واحدة: تُفتح صفحة الوصول إلى الإشعارات على هذه الشاشة؛ فعِّل DroidDeck هناك وستعود هذه الصفحة تلقائيًا. - السماح بالوصول إلى الإشعارات - لا توجد إشعارات. - الإشعارات: %1$d - مسح لقطات الشاشة يتطلب FPS تشغيل واجهة الأداء السرعة @@ -2753,7 +2702,6 @@ الإنجازات غير متوفرة؛ تحقّق من سجلات الجلسة تم فتح الإنجاز لصق - لوحة الاتجاهات العصا اليسرى العصا اليمنى القائمة @@ -3017,6 +2965,8 @@ أعلى أسفل الوسط (افتراضي) + يسار + يمين التخزين جارٍ قياس التخزين… يستغرق القياس الأول بضع ثوانٍ. @@ -3123,6 +3073,7 @@ المتاح %1$s فقط يحتاج Steam مساحة للتحديثات وذاكرة الشيدرات. تحت 10 GB قد تفشل تحديثات الألعاب في المنتصف. يمكن تحرير %1$s بأمان. + لا تحذرني من هذا مرة أخرى تحرير مساحة اللعب على أي حال @@ -3839,4 +3790,17 @@ جارٍ الاستماع… الكتابة بالصوت غير متاحة على هذا الجهاز. يلزم الوصول إلى الميكروفون للكتابة بالصوت. + تنزيل الإضافات… + الاسم + الأكثر تنزيلًا + كل الفئات + لم يتم العثور على إضافات + تعذّر الوصول إلى %1$s + لا يوجد ملف لإصدار اللعبة هذا + إعادة تعيين المواضع + إظهار التسمية + تعذّر استيراد هذا التخطيط + + ثلج قرمزي + diff --git a/app/src/main/res/values-b+es+419/strings.xml b/app/src/main/res/values-b+es+419/strings.xml index 6c0a836ee1..441ce13dcb 100644 --- a/app/src/main/res/values-b+es+419/strings.xml +++ b/app/src/main/res/values-b+es+419/strings.xml @@ -261,24 +261,8 @@ Táctil de Steam (beta) Los controles táctiles de Steam se activan en la próxima sesión de Steam Disposición táctil de Steam - Teclas en pantalla - Agregar tecla - Texto - Tamaño - Opacidad - Tecla - Gatillo - Alternar Exportar - Píldora - Redondeado - Círculo - Rectángulo - Tecla Imagen - Borrar - Disposición - Guardar Mueve, cambia de tamaño u oculta los controles táctiles de este juego; se guarda en Steam Control Cómo ve Steam tu control. Se aplica en la próxima sesión. @@ -557,6 +541,7 @@ Ningún DroidDeck-Proton lo usa Las actualizaciones Auto esperan una conexión. No se pudieron buscar nuevas versiones. + No se pudo instalar %1$s. Las nuevas versiones esperan una conexión sin límite de datos, el ahorro de batería desactivado y el dispositivo frío. %1$s actualizado a %2$s. %1$s %2$s cerró un juego en segundos dos veces, así que Auto volvió a %3$s. @@ -782,7 +767,6 @@ Automático Activado Desactivado - Cursor Teclado de PC Android Segunda pantalla @@ -1132,23 +1116,17 @@ 3 · ADB desde una computadora (último recurso) Solo si la Depuración inalámbrica no está disponible. 3 · ADB desde una computadora (último recurso) - Cuándo aparecen los controles táctiles en una sesión Color - Tinte de los botones en pantalla Personalizado Opacidad %1$d%% Tamaño de botones - 100% es el tamaño estándar - Clic del stick - Toca dos veces y mantén un stick para L3 o R3 Sticks adaptables Vibración en juegos Zona muerta de la palanca Botón Steam Botón QAM Botón de teclado - Presiona Select para abrir el menú de Steam Joy-Con como controles de RV Mientras SteamVR está activo, cada Joy-Con es una mano. Apúntalo al frente y mantén Home o Captura para recentrarlo. Sin ningún mando conectado, la cámara del teléfono rastrea tus manos en realidad virtual: pellizca con el pulgar y el índice para hacer clic, cierra la mano para agarrar y gira la palma hacia ti y pellizca para abrir el menú. Con los mandos Joy-Con emparejados, la cámara rastrea su posición mientras los mandos Joy-Con proporcionan la rotación y las entradas de los botones. Las imágenes se procesan en este dispositivo y nunca se guardan ni se envían. @@ -1194,24 +1172,12 @@ La conexión con las XREAL se interrumpió o devolvió una respuesta no compatible. Reintentando… Compatibilidad experimental con las 1S. Usa el modo Follow y desactiva la estabilización en el menú de las gafas. Aún falta probar en el dispositivo las direcciones del seguimiento y los modos de pantalla. Seguimiento de cabeza experimental para Pro 2. Los ajustes de brillo y volumen y el cambio de modo de pantalla aún no son compatibles. - En lugar de View/Select. Animaciones - Los sticks aparecen donde tocas y se ocultan al soltar + Avisar cuando queda poco almacenamiento + Preguntar antes de iniciar Steam cuando el espacio libre es menor a 10 GB. Disposición - Posiciones personalizadas guardadas - Ajustada a esta pantalla Editar Restablecer - Asignación de botones - Cada botón envía su propia entrada - %1$d de %2$d botones reasignados - Configurar - Restablecer control - Restaura todos los ajustes del control y la disposición - Lo que cada botón en pantalla envía al juego. Oculto quita el botón. - Botones en pantalla - Predeterminados - Restablecer asignación Apps de Android %1$d apps No se encontraron apps de Android que se puedan abrir. @@ -1292,7 +1258,6 @@ Aún no hay controladores listados. Actualiza para buscarlos. Ocultar controladores de otras GPU Mostrar controladores de otras GPU - Avanzado Compatibilidad con UBWC Prueba Activado o Desactivado si Steam o Android muestran franjas o parpadeos. Se aplica a ambos controladores después de reiniciar DroidDeck. Reinicia DroidDeck para aplicarlo. @@ -1332,16 +1297,8 @@ Control Stick Trackpad - Cambiar entre stick y trackpad - Más controles de Deck - PANELES Cerrar controles de la segunda pantalla Abrir menú de Steam - Izquierdo · superior - Izquierdo · inferior - Derecho · superior - Derecho · inferior - Clic Trackpad derecho Trackpad izquierdo Trackpad @@ -1866,6 +1823,7 @@ Ámbar Rojo Rosa + Gris Botón A Botón B Botón X @@ -1883,9 +1841,7 @@ Cruceta izquierda Cruceta derecha Oculto - Arrastra para mover Ajustar - Guardar Nada Teclado + trackpad Terminal @@ -1898,13 +1854,6 @@ Descargando caché de shaders En pausa Esperando para empezar - Notificaciones - DroidDeck – pantalla inferior - Muestra aquí los mensajes y otras notificaciones mientras juegas. Solo permanecen en esta pantalla y no se guardan. Android lo pregunta una vez: su página de acceso a notificaciones se abre en esta pantalla; activa DroidDeck ahí y esta página volverá sola. - Permitir acceso a notificaciones - No hay notificaciones. - %1$d notificaciones - Borrar Capturas de pantalla Los FPS requieren el HUD activado Velocidad @@ -2690,7 +2639,6 @@ Logros no disponibles; revisa los registros de la sesión Logro desbloqueado Pegar - Pad direccional Stick izquierdo Stick derecho Menú @@ -2869,6 +2817,8 @@ Arriba Abajo Centro (predeterminado) + Izquierda + Derecha Almacenamiento Midiendo el almacenamiento… La primera vez tarda unos segundos. @@ -2975,6 +2925,7 @@ Solo quedan %1$s libres Steam necesita espacio para actualizaciones y cachés de shaders. Por debajo de 10 GB, las actualizaciones de juegos pueden fallar a medias. Se pueden liberar %1$s sin riesgo. + No volver a avisarme Liberar espacio Jugar igualmente @@ -3695,4 +3646,16 @@ Escuchando… El dictado por voz no está disponible en este dispositivo. Se necesita acceso al micrófono para el dictado por voz. + Descargar complementos… + Nombre + Más descargados + Todas las categorías + No se encontraron complementos + No se pudo conectar con %1$s + No hay archivo para esta versión del juego + Restablecer posiciones + Mostrar etiqueta + No se pudo importar esta disposición + Nieve escarlata + Superficies blancas con acentos rojos. diff --git a/app/src/main/res/values-bg/strings.xml b/app/src/main/res/values-bg/strings.xml index 736ed5fc67..6e43de6f09 100644 --- a/app/src/main/res/values-bg/strings.xml +++ b/app/src/main/res/values-bg/strings.xml @@ -261,24 +261,8 @@ Докосване в Steam (бета) Сензорните контроли на Steam ще стартират от следващата сесия на Steam Разположение на докосването в Steam - Клавиши на екрана - Добавяне на клавиш - Текст - Размер - Прозрачност - Клавиш - Спускът - Превключване Експорт - Капсула - Заоблено - Кръг - Правоъгълник - Клавиш Изображение - Изчистване - Разположение - Запиши Премествайте, преоразмерявайте или скривайте сензорните контроли на тази игра; записва се в Steam Контролер Как Steam разпознава контролера ви. Прилага се от следващата сесия. @@ -555,6 +539,7 @@ Нито един DroidDeck-Proton не го използва Актуализациите Auto чакат връзка. Новите версии не можаха да бъдат проверени. + %1$s не можа да бъде инсталиран. Новите версии чакат неотчитана връзка, изключен режим за пестене на батерията и охладено устройство. %1$s е обновен до %2$s. %1$s %2$s два пъти затвори игра до секунди, затова Auto се върна към %3$s. @@ -756,7 +741,6 @@ Автоматично Включено Изключено - Курсор Клавиатура за компютър Android Втори екран @@ -1099,43 +1083,25 @@ 3 · ADB от компютър (последна възможност) Само ако безжичното отстраняване на грешки не е достъпно. 3 · ADB от компютър (последна възможност) - Кога се появяват екранните контроли в сесия Цвят - Оттенък на екранните бутони Персонализиран Прозрачност %1$d%% Размер на бутоните - 100% е стандартният размер - Натискане на стика - Докоснете двукратно и задръжте стик за L3 или R3 Адаптивни стикове Вибрация от играта Мъртва зона на стика Бутон Steam Бутон QAM Бутон за клавиатура - Натиснете Select, за да отворите менюто на Steam - Вместо View/Select. Joy-Con като VR контролери Докато SteamVR работи, всеки Joy-Con става ръка. Насочете го напред и задръжте Home или Capture, за да го центрирате отново. Анимации - Стиковете се появяват там, където докоснете, и се скриват при отпускане + Предупреждение при малко място + Питай преди стартиране на Steam, когато свободното място е под 10 GB. Разположение - Запазени персонализирани позиции - Нагласено за този екран Редактиране Нулиране - Разпределение на бутоните - Всеки бутон изпраща собствения си сигнал - %1$d от %2$d бутона преназначени - Конфигуриране - Нулиране на контролера - Възстановява всички настройки на контролера и разположението - Какво изпраща всеки екранен бутон към играта. „Скрит“ премахва бутона. - Екранни бутони - Стандартни настройки - Нулиране на разпределението Приложения за Android %1$d приложения Не са намерени приложения за Android за стартиране. @@ -1215,7 +1181,6 @@ Все още няма изброени драйвери. Обновете, за да ги потърсите. Скриване на драйверите за други GPU Показване на драйверите за други GPU - Разширени Съвместимост с UBWC Опитайте Вкл. или Изкл., ако Steam или Android показва ивици или трептене. Прилага се и за двата драйвера след рестартиране на DroidDeck. Рестартирайте DroidDeck, за да се приложи. @@ -1255,16 +1220,8 @@ Геймпад Стик Тракпад - Превключване между стик и тракпад - Още контроли на Deck - ПАНЕЛИ Затваряне на контролите за втория екран Отваряне на менюто на Steam - Ляво · горе - Ляво · долу - Дясно · горе - Дясно · долу - Клик Десен тракпад Ляв тракпад Тракпад @@ -1778,6 +1735,7 @@ Кехлибарено Червено Розово + Сиво Бутон A Бутон B Бутон X @@ -1795,9 +1753,7 @@ D-pad наляво D-pad надясно Скрит - Плъзни, за да преместиш Прилепване - Запазване Нищо Клавиатура + тракпад Терминал @@ -1810,13 +1766,6 @@ Изтегляне на кеша на шейдърите На пауза Изчаква стартиране - Известия - Долен екран на DroidDeck - Показвайте тук съобщения и други известия, докато играете. Те остават само на този екран и не се запазват. Android пита веднъж: страницата за достъп до известията се отваря на този екран; включете DroidDeck там и тази страница ще се върне сама. - Разрешаване на достъпа до известията - Няма известия. - Известия: %1$d - Изчисти Екранни снимки За FPS трябва HUD да е включен Скорост @@ -2645,7 +2594,6 @@ Постиженията не са достъпни; проверете логовете на сесията Постижение отключено Поставяне - Кръстат бутон Ляв стик Десен стик Меню @@ -2901,6 +2849,8 @@ Горе Долу В средата (по подразбиране) + Наляво + Надясно Съхранение Измерване на съхранението… Първият път отнема няколко секунди. @@ -3007,6 +2957,7 @@ Свободни са само %1$s Steam се нуждае от място за актуализации и кеш на шейдърите. Под 10 GB актуализациите на игрите може да се провалят по средата. %1$s могат да се освободят безопасно. + Не ме предупреждавай повече Освободи място Играй въпреки това @@ -3683,4 +3634,17 @@ Слушане… Гласовото въвеждане не е налично на това устройство. За гласово въвеждане е нужен достъп до микрофона. + Изтегляне на добавки… + Име + Най-изтегляни + Всички категории + Няма намерени добавки + Няма връзка с %1$s + Няма файл за тази версия на играта + Нулиране на позициите + Показване на етикета + Това разположение не можа да бъде импортирано + + Ален сняг + diff --git a/app/src/main/res/values-cs/strings.xml b/app/src/main/res/values-cs/strings.xml index 64d6f8d7c8..d883b09c7c 100644 --- a/app/src/main/res/values-cs/strings.xml +++ b/app/src/main/res/values-cs/strings.xml @@ -261,24 +261,8 @@ Dotykové ovládání Steamu (beta) Dotykové ovládání Steamu se spustí od příští relace Steamu Rozvržení dotykového ovládání Steamu - Klávesy na obrazovce - Přidat klávesu - Text - Velikost - Průhlednost - Klávesa - Spoušť - Přepínač Exportovat - Kapsle - Zaoblené - Kruh - Obdélník - Klávesa Obrázek - Vymazat - Rozložení - Uložit Přesuňte, změňte velikost nebo skryjte dotykové ovládání této hry; uloží se do Steamu Ovladač Jak Steam vidí váš ovladač. Použije se od příští relace. @@ -559,6 +543,7 @@ Žádný DroidDeck-Proton to nepoužívá Aktualizace Auto čekají na připojení. Nová sestavení nelze zkontrolovat. + %1$s nelze nainstalovat. Nová sestavení čekají na neměřené připojení, vypnutou úsporu baterie a nepřehřáté zařízení. %1$s aktualizováno na %2$s. %1$s %2$s dvakrát zavřelo hru během pár sekund, proto se Auto vrátilo k %3$s. @@ -760,7 +745,6 @@ Automaticky Zapnuto Vypnuto - Kurzor Klávesnice PC Android Druhá obrazovka @@ -1111,43 +1095,25 @@ 3 · ADB z počítače (poslední možnost) Jen pokud Bezdrátové ladění není k dispozici. 3 · ADB z počítače (poslední možnost) - Kdy se dotykové ovládání zobrazí v relaci Barva - Odstín tlačítek na obrazovce Vlastní Průhlednost %1$d%% Velikost tlačítek - 100 % je standardní velikost - Kliknutí páčkou - Dvojitým dotykem a podržením páčky aktivujete L3 nebo R3 Adaptivní páčky Vibrace ve hře Mrtvá zóna páčky Tlačítko Steam Tlačítko QAM Tlačítko klávesnice - Otevřít nabídku Steam stisknutím Select - Místo View/Select. Joy-Cony jako ovladače VR Když běží SteamVR, každý Joy-Con slouží jako ruka. Namiřte ho dopředu a podržením Home nebo Capture ho znovu vycentrujete. Animace - Páčky se zobrazí tam, kde se dotknete, a skryjí se po uvolnění + Upozornit na nedostatek úložiště + Zeptat se před spuštěním Steamu, když je volné místo pod 10 GB. Rozložení - Uloženy vlastní pozice - Přizpůsobeno této obrazovce Upravit Obnovit - Mapování tlačítek - Každé tlačítko odesílá svůj vlastní vstup - %1$d z %2$d tlačítek přemapováno - Konfigurovat - Obnovit ovladač - Obnoví všechna nastavení ovladače a rozložení - Co každé tlačítko na obrazovce odesílá hře. Skryté tlačítko odstraní. - Tlačítka na obrazovce - Výchozí - Obnovit mapování Aplikace Android %1$d aplikací Nebyly nalezeny žádné spustitelné aplikace Android. @@ -1229,7 +1195,6 @@ Zatím žádné ovladače. Obnovte, aby se vyhledaly. Skrýt ovladače pro jiné GPU Zobrazit ovladače pro jiné GPU - Pokročilé Kompatibilita UBWC Pokud Steam nebo Android zobrazuje pruhy nebo blikání, zkuste Zapnuto nebo Vypnuto. Platí pro oba ovladače po restartu DroidDeck. Pro použití restartujte DroidDeck. @@ -1269,16 +1234,8 @@ Gamepad Páčka Trackpad - Přepnout mezi páčkou a trackpadem - Další ovládání Deck - PANELY Zavřít ovládání druhé obrazovky Otevřít nabídku Steamu - Levý · horní - Levý · dolní - Pravý · horní - Pravý · dolní - Kliknutí Pravý trackpad Levý trackpad Trackpad @@ -1826,6 +1783,7 @@ Ambrová Červená Růžová + Šedá Tlačítko A Tlačítko B Tlačítko X @@ -1843,9 +1801,7 @@ D-pad vlevo D-pad vpravo Skryté - Přetažením přesunete Přichytit - Uložit Žádný Klávesnice + trackpad Terminál @@ -1858,13 +1814,6 @@ Stahování mezipaměti shaderů Pozastaveno Čeká na spuštění - Oznámení - Spodní obrazovka DroidDeck - Zobrazujte zde zprávy a další oznámení během hraní. Zůstávají jen na této obrazovce a neukládají se. Android se zeptá jednou: na této obrazovce se otevře jeho stránka přístupu k oznámením; zapněte tam DroidDeck a tato stránka se sama vrátí. - Povolit přístup k oznámením - Žádná oznámení. - Oznámení: %1$d - Vymazat Snímky obrazovky FPS vyžaduje zapnutý HUD Rychlost @@ -2699,7 +2648,6 @@ Achievementy nejsou k dispozici; zkontrolujte protokoly relace Achievement odemčen Vložit - Křížový ovladač Levá páčka Pravá páčka Nabídka @@ -2959,6 +2907,8 @@ Nahoře Dole Uprostřed (výchozí) + Vlevo + Vpravo Úložiště Měření úložiště… Poprvé to trvá pár sekund. @@ -3065,6 +3015,7 @@ Volně jen %1$s Steam potřebuje místo na aktualizace a mezipaměť shaderů. Pod 10 GB mohou aktualizace her selhat v polovině. %1$s lze bezpečně uvolnit. + Už mě neupozorňovat Uvolnit místo Přesto hrát @@ -3761,4 +3712,17 @@ Poslouchám… Hlasové psaní není na tomto zařízení k dispozici. Pro hlasové psaní je potřeba přístup k mikrofonu. + Stáhnout doplňky… + Název + Nejstahovanější + Všechny kategorie + Nenalezeny žádné doplňky + Nelze se připojit k %1$s + Pro tuto verzi hry není k dispozici soubor + Obnovit pozice + Zobrazit popisek + Toto rozložení nelze importovat + + Šarlatový sníh + diff --git a/app/src/main/res/values-da/strings.xml b/app/src/main/res/values-da/strings.xml index 8f6e73c04b..52a2c1cecf 100644 --- a/app/src/main/res/values-da/strings.xml +++ b/app/src/main/res/values-da/strings.xml @@ -261,24 +261,8 @@ Steam-berøring (beta) Steams berøringskontroller starter med næste Steam-session Steam-berøringslayout - Skærmtaster - Tilføj tast - Tekst - Størrelse - Gennemsigtighed - Tast - Udløser - Skift Eksportér - Kapsel - Afrundet - Cirkel - Rektangel - Tast Billede - Ryd - Layout - Gem Flyt, tilpas størrelsen på eller skjul dette spils berøringskontroller; gemmes til Steam Controller Hvordan Steam ser din controller. Gælder fra næste session. @@ -555,6 +539,7 @@ Ingen DroidDeck-Proton bruger dette Auto-opdateringer venter på en forbindelse. Kunne ikke søge efter nye builds. + Kunne ikke installere %1$s. Nye builds venter på en ubegrænset forbindelse, slået batterisparer fra og en kølig enhed. %1$s opdateret til %2$s. %1$s %2$s lukkede et spil inden for sekunder to gange, så Auto gik tilbage til %3$s. @@ -756,7 +741,6 @@ Automatisk Til Fra - Markør PC-tastatur Android Anden skærm @@ -1099,43 +1083,25 @@ 3 · Computer-ADB (sidste udvej) Kun hvis Trådløs fejlretning ikke er tilgængelig. 3 · Computer-ADB (sidste udvej) - Hvornår berøringskontrollerne vises i en session Farve - Farvetone for knapperne på skærmen Brugerdefineret Gennemsigtighed %1$d%% Knapstørrelse - 100% er standardstørrelsen - Stick-klik - Dobbelttryk og hold en stick nede for L3 eller R3 Adaptive styrepinde Spilvibration Dødzone for styrepind Steam-knap QAM-knap Tastaturknap - Tryk på Select for at åbne Steam-menuen - I stedet for View/Select. Joy-Cons som VR-controllere Mens SteamVR kører, bliver hver Joy-Con en hånd. Peg den fremad, og hold Home eller Capture nede for at centrere den igen. Animationer - Sticks vises, hvor du rører skærmen, og skjules, når du slipper + Advar, når lagerpladsen er lav + Spørg, før Steam startes, når den ledige plads er under 10 GB. Layout - Brugerdefinerede positioner gemt - Tilpasset denne skærm Rediger Nulstil - Knapmapning - Hver knap sender sit eget input - %1$d af %2$d knapper ommappet - Konfigurer - Nulstil controller - Gendanner alle controllerindstillinger og layoutet - Hvad hver knap på skærmen sender til spillet. Skjult fjerner knappen. - Knapper på skærmen - Standarder - Nulstil mapning Android-apps %1$d apps Ingen Android-apps, der kan startes, fundet. @@ -1215,7 +1181,6 @@ Ingen drivere på listen endnu. Opdater for at søge efter dem. Skjul drivere til andre GPU’er Vis drivere til andre GPU’er - Avanceret UBWC-kompatibilitet Prøv Til eller Fra, hvis Steam eller Android viser striber eller flimmer. Gælder begge drivere efter genstart af DroidDeck. Genstart DroidDeck for at anvende. @@ -1255,16 +1220,8 @@ Gamepad Stick Trackpad - Skift mellem stick og trackpad - Flere Deck-kontroller - PANELER Luk kontrollerne til den anden skærm Åbn Steam-menuen - Venstre · øverst - Venstre · nederst - Højre · øverst - Højre · nederst - Klik Højre trackpad Venstre trackpad Trackpad @@ -1778,6 +1735,7 @@ Ravgul Rød Pink + Grå A-knap B-knap X-knap @@ -1795,9 +1753,7 @@ D-pad venstre D-pad højre Skjult - Træk for at flytte Fastgør - Gem Ingen Tastatur + trackpad Terminal @@ -1810,13 +1766,6 @@ Downloader shader-cache På pause Venter på at starte - Notifikationer - DroidDecks nederste skærm - Vis beskeder og andre notifikationer her, mens du spiller. De bliver kun på denne skærm og gemmes ikke. Android spørger én gang: siden for notifikationsadgang åbnes på denne skærm; slå DroidDeck til der, så vender denne side selv tilbage. - Tillad notifikationsadgang - Ingen notifikationer. - %1$d notifikationer - Ryd Skærmbilleder FPS kræver, at HUD er slået til Hastighed @@ -2645,7 +2594,6 @@ Bedrifter ikke tilgængelige; se sessionslogs Bedrift låst op Indsæt - Styrekryds Venstre stick Højre stick Menu @@ -2901,6 +2849,8 @@ Øverst Nederst Midten (standard) + Venstre + Højre Lager Måler lager… Første gang tager et par sekunder. @@ -3007,6 +2957,7 @@ Kun %1$s ledig Steam har brug for plads til opdateringer og shadercache. Under 10 GB kan spilopdateringer mislykkes halvvejs. %1$s kan frigøres sikkert. + Advar mig ikke igen Frigør plads Spil alligevel @@ -3683,4 +3634,17 @@ Lytter… Stemmeindtastning er ikke tilgængelig på denne enhed. Mikrofonadgang er nødvendig for stemmeindtastning. + Download tilføjelser… + Navn + Mest downloadet + Alle kategorier + Ingen tilføjelser fundet + Kunne ikke nå %1$s + Ingen fil til denne spilversion + Nulstil placeringer + Vis etiket + Dette layout kunne ikke importeres + + Skarlagensne + diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index e964cc761d..9a304514bf 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -261,24 +261,8 @@ Steam-Touch (Beta) Steams Touch-Steuerung startet mit der nächsten Steam-Sitzung Steam-Touch-Layout - Bildschirmtasten - Taste hinzufügen - Text - Größe - Deckkraft - Taste - Trigger - Umschalten Exportieren - Kapsel - Rund - Kreis - Rechteck - Taste Bild - Entfernen - Layout - Speichern Touch-Steuerung dieses Spiels verschieben, skalieren oder ausblenden; wird in Steam gespeichert Controller Wie Steam deinen Controller erkennt. Gilt ab der nächsten Sitzung. @@ -555,6 +539,7 @@ Kein DroidDeck-Proton nutzt dies Auto-Updates warten auf eine Verbindung. Neue Builds konnten nicht geprüft werden. + %1$s konnte nicht installiert werden. Neue Builds warten auf eine nicht getaktete Verbindung, ausgeschalteten Energiesparmodus und ein kühles Gerät. %1$s auf %2$s aktualisiert. %1$s %2$s hat ein Spiel zweimal nach wenigen Sekunden beendet, daher ist Auto zu %3$s zurückgekehrt. @@ -756,7 +741,6 @@ Automatisch An Aus - Zeiger PC-Tastatur Android Zweiter Bildschirm @@ -1091,43 +1075,25 @@ 3 · ADB am Computer (letzter Ausweg) Nur wenn „Debugging über WLAN“ nicht verfügbar ist. 3 · ADB am Computer (letzter Ausweg) - Wann die Touch-Steuerung in einer Sitzung erscheint Farbe - Farbton der Bildschirmtasten Benutzerdefiniert Deckkraft %1$d %% Tastengröße - Standardgröße: 100 % - Stick-Klick - Doppeltippen und Stick halten für L3 oder R3 Adaptive Sticks Spielvibration Stick-Totzone Steam-Taste QAM-Taste Tastatur-Taste - Mit Select das Steam-Menü öffnen - Statt View/Select. Joy-Cons als VR-Controller Während SteamVR läuft, wird jeder Joy-Con zu einer Hand. Richte ihn nach vorn und halte HOME oder Capture gedrückt, um ihn neu auszurichten. Animationen - Sticks erscheinen dort, wo du tippst, und verschwinden beim Loslassen + Bei knappem Speicher warnen + Vor dem Start von Steam nachfragen, wenn weniger als 10 GB frei sind. Layout - Eigene Positionen gespeichert - An diesen Bildschirm angepasst Bearbeiten Zurücksetzen - Tastenbelegung - Jede Taste sendet ihre eigene Eingabe - %1$d von %2$d Tasten neu belegt - Konfigurieren - Controller zurücksetzen - Stellt alle Controller-Einstellungen und das Layout wieder her - Was jede Bildschirmtaste an das Spiel sendet. „Ausgeblendet“ entfernt die Taste. - Bildschirmtasten - Standardwerte - Belegung zurücksetzen Android-Apps %1$d Apps Keine startbaren Android-Apps gefunden. @@ -1207,7 +1173,6 @@ Noch keine Treiber aufgeführt. Aktualisiere, um danach zu suchen. Treiber für andere GPUs ausblenden Treiber für andere GPUs anzeigen - Erweitert UBWC-Kompatibilität Probiere An oder Aus, wenn Steam oder Android Streifen oder Flackern zeigt. Gilt für beide Treiber nach einem Neustart von DroidDeck. Starte DroidDeck neu, um dies anzuwenden. @@ -1247,16 +1212,8 @@ Gamepad Stick Trackpad - Zwischen Stick und Trackpad wechseln - Weitere Deck-Steuerelemente - BEREICHE Steuerung auf dem Zweitbildschirm schließen Steam-Menü öffnen - Links · oben - Links · unten - Rechts · oben - Rechts · unten - Klick Rechtes Trackpad Linkes Trackpad Trackpad @@ -1770,6 +1727,7 @@ Bernstein Rot Pink + Grau A-Taste B-Taste X-Taste @@ -1787,9 +1745,7 @@ Steuerkreuz links Steuerkreuz rechts Ausgeblendet - Zum Verschieben ziehen Einrasten - Speichern Nichts Tastatur + Trackpad Terminal @@ -1802,13 +1758,6 @@ Shader-Cache wird heruntergeladen Pausiert Wartet auf Start - Benachrichtigungen - DroidDeck – unterer Bildschirm - Zeigt Nachrichten und andere Benachrichtigungen hier an, während du spielst. Sie bleiben nur auf diesem Bildschirm und werden nicht gespeichert. Android fragt einmal: Die Seite für den Benachrichtigungszugriff öffnet sich auf diesem Bildschirm; aktiviere dort DroidDeck, dann kehrt diese Seite von selbst zurück. - Benachrichtigungszugriff erlauben - Keine Benachrichtigungen. - %1$d Benachrichtigungen - Löschen Screenshots FPS benötigt das aktive HUD Geschwindigkeit @@ -2634,7 +2583,6 @@ Erfolge nicht verfügbar; Sitzungsprotokolle prüfen Erfolg freigeschaltet Einfügen - Richtungstasten Linker Stick Rechter Stick Menü @@ -2901,6 +2849,8 @@ Oben Unten Mitte (Standard) + Links + Rechts Speicher Speicher wird erfasst… Die erste Erfassung dauert ein paar Sekunden. @@ -3007,6 +2957,7 @@ Nur noch %1$s frei Steam braucht Platz für Updates und Shader-Cache. Unter 10 GB können Spiel-Updates mittendrin fehlschlagen. %1$s lässt sich sicher freigeben. + Nicht mehr warnen Platz schaffen Trotzdem spielen @@ -3683,4 +3634,17 @@ Hört zu… Spracheingabe ist auf diesem Gerät nicht verfügbar. Für die Spracheingabe wird Mikrofonzugriff benötigt. + Addons herunterladen… + Name + Meistgeladen + Alle Kategorien + Keine Addons gefunden + %1$s ist nicht erreichbar + Keine Datei für diese Spielversion + Positionen zurücksetzen + Beschriftung anzeigen + Dieses Layout konnte nicht importiert werden + + Scharlachroter Schnee + diff --git a/app/src/main/res/values-el/strings.xml b/app/src/main/res/values-el/strings.xml index d730e189ab..53744253c6 100644 --- a/app/src/main/res/values-el/strings.xml +++ b/app/src/main/res/values-el/strings.xml @@ -261,24 +261,8 @@ Αφή Steam (beta) Τα χειριστήρια αφής του Steam ξεκινούν από την επόμενη συνεδρία Steam Διάταξη αφής Steam - Πλήκτρα οθόνης - Προσθήκη πλήκτρου - Κείμενο - Μέγεθος - Διαφάνεια - Πλήκτρο - Σκανδάλη - Εναλλαγή Εξαγωγή - Κάψουλα - Στρογγυλεμένο - Κύκλος - Ορθογώνιο - Πλήκτρο Εικόνα - Εκκαθάριση - Διάταξη - Αποθήκευση Μετακινήστε, αλλάξτε μέγεθος ή αποκρύψτε τα χειριστήρια αφής αυτού του παιχνιδιού· αποθηκεύεται στο Steam Χειριστήριο Πώς βλέπει το Steam το χειριστήριό σας. Εφαρμόζεται στην επόμενη συνεδρία. @@ -555,6 +539,7 @@ Κανένα DroidDeck-Proton δεν το χρησιμοποιεί Οι ενημερώσεις Auto περιμένουν σύνδεση. Δεν ήταν δυνατός ο έλεγχος για νέες εκδόσεις. + Δεν ήταν δυνατή η εγκατάσταση του %1$s. Οι νέες εκδόσεις περιμένουν σύνδεση χωρίς όριο δεδομένων, απενεργοποιημένη εξοικονόμηση μπαταρίας και δροσερή συσκευή. Το %1$s ενημερώθηκε σε %2$s. Το %1$s %2$s έκλεισε ένα παιχνίδι μέσα σε δευτερόλεπτα δύο φορές, οπότε το Auto επέστρεψε στο %3$s. @@ -756,7 +741,6 @@ Αυτόματα Ενεργό Ανενεργό - Δείκτης Πληκτρολόγιο PC Android Δεύτερη οθόνη @@ -1099,43 +1083,25 @@ 3 · ADB υπολογιστή (τελευταία λύση) Μόνο αν ο Ασύρματος εντοπισμός σφαλμάτων δεν είναι διαθέσιμος. 3 · ADB υπολογιστή (τελευταία λύση) - Όταν εμφανίζονται τα χειριστήρια αφής σε μια συνεδρία Χρώμα - Απόχρωση των κουμπιών οθόνης Προσαρμοσμένο Διαφάνεια %1$d%% Μέγεθος κουμπιών - Το 100% είναι το κανονικό μέγεθος - Κλικ μοχλού - Πατήστε δύο φορές και κρατήστε έναν μοχλό για L3 ή R3 Προσαρμοστικοί μοχλοί Κραδασμός παιχνιδιού Νεκρή ζώνη μοχλού Κουμπί Steam Κουμπί QAM Κουμπί πληκτρολογίου - Πατήστε Select για να ανοίξετε το μενού Steam - Αντί για View/Select. Joy-Con ως χειριστήρια VR Όσο εκτελείται το SteamVR, κάθε Joy-Con γίνεται χέρι. Στρέψτε το μπροστά και κρατήστε πατημένο το Home ή το Capture για να το επανακεντράρετε. Κινήσεις - Οι μοχλοί εμφανίζονται όπου αγγίζετε και εξαφανίζονται όταν αφήνετε + Προειδοποίηση για χαμηλό χώρο αποθήκευσης + Ερώτηση πριν από την εκκίνηση του Steam όταν ο ελεύθερος χώρος είναι κάτω από 10 GB. Διάταξη - Αποθηκεύτηκαν προσαρμοσμένες θέσεις - Προσαρμοσμένο σε αυτή την οθόνη Επεξεργασία Επαναφορά - Αντιστοίχιση κουμπιών - Κάθε κουμπί στέλνει τη δική του είσοδο - %1$d από %2$d κουμπιά επαναντιστοιχίστηκαν - Διαμόρφωση - Επαναφορά χειριστηρίου - Επαναφέρει όλες τις ρυθμίσεις χειριστηρίου και τη διάταξη - Τι στέλνει κάθε κουμπί οθόνης στο παιχνίδι. Το Κρυμμένο αφαιρεί το κουμπί. - Κουμπιά οθόνης - Προεπιλογές - Επαναφορά αντιστοίχισης Εφαρμογές Android %1$d εφαρμογές Δεν βρέθηκαν εφαρμογές Android για εκκίνηση. @@ -1215,7 +1181,6 @@ Δεν υπάρχουν ακόμα προγράμματα οδήγησης. Ανανεώστε για να τα αναζητήσετε. Απόκρυψη προγραμμάτων οδήγησης για άλλες GPU Εμφάνιση προγραμμάτων οδήγησης για άλλες GPU - Για προχωρημένους Συμβατότητα UBWC Δοκιμάστε Ενεργό ή Ανενεργό αν το Steam ή το Android εμφανίζει γραμμές ή τρεμόπαιγμα. Ισχύει και για τα δύο προγράμματα οδήγησης μετά την επανεκκίνηση του DroidDeck. Κάντε επανεκκίνηση του DroidDeck για εφαρμογή. @@ -1255,16 +1220,8 @@ Χειριστήριο Μοχλός Πινακίδα αφής - Εναλλαγή μεταξύ μοχλού και πινακίδας αφής - Περισσότερα χειριστήρια Deck - ΠΙΝΑΚΕΣ Κλείσιμο χειριστηρίων δεύτερης οθόνης Άνοιγμα μενού Steam - Αριστερά · πάνω - Αριστερά · κάτω - Δεξιά · πάνω - Δεξιά · κάτω - Κλικ Δεξιά πινακίδα αφής Αριστερή πινακίδα αφής Πινακίδα αφής @@ -1778,6 +1735,7 @@ Κεχριμπαρί Κόκκινο Ροζ + Γκρι Κουμπί A Κουμπί B Κουμπί X @@ -1795,9 +1753,7 @@ D-pad αριστερά D-pad δεξιά Κρυφό - Σύρετε για μετακίνηση Προσκόλληση - Αποθήκευση Κανένα Πληκτρολόγιο + πινακίδα αφής Τερματικό @@ -1810,13 +1766,6 @@ Λήψη cache shader Σε παύση Αναμονή για έναρξη - Ειδοποιήσεις - DroidDeck – κάτω οθόνη - Εμφάνιση μηνυμάτων και άλλων ειδοποιήσεων εδώ ενώ παίζετε. Παραμένουν μόνο σε αυτή την οθόνη και δεν αποθηκεύονται. Το Android ρωτά μία φορά: η σελίδα πρόσβασης ειδοποιήσεων ανοίγει σε αυτή την οθόνη· ενεργοποιήστε εκεί το DroidDeck και αυτή η σελίδα επιστρέφει μόνη της. - Αποδοχή πρόσβασης ειδοποιήσεων - Δεν υπάρχουν ειδοποιήσεις. - %1$d ειδοποιήσεις - Απαλοιφή Στιγμιότυπα οθόνης Τα FPS απαιτούν ενεργό HUD Ταχύτητα @@ -2645,7 +2594,6 @@ Τα επιτεύγματα δεν είναι διαθέσιμα· ελέγξτε τα αρχεία καταγραφής της συνεδρίας Το επίτευγμα ξεκλειδώθηκε Επικόλληση - Σταυρός κατεύθυνσης Αριστερό στικ Δεξί στικ Μενού @@ -2901,6 +2849,8 @@ Πάνω Κάτω Κέντρο (προεπιλογή) + Αριστερά + Δεξιά Αποθήκευση Μέτρηση αποθήκευσης… Την πρώτη φορά παίρνει λίγα δευτερόλεπτα. @@ -3007,6 +2957,7 @@ Μόνο %1$s ελεύθερα Το Steam χρειάζεται χώρο για ενημερώσεις και προσωρινή μνήμη shaders. Κάτω από 10 GB οι ενημερώσεις παιχνιδιών μπορεί να αποτύχουν στη μέση. Τα %1$s μπορούν να ελευθερωθούν με ασφάλεια. + Να μην εμφανιστεί ξανά η προειδοποίηση Ελευθέρωσε χώρο Παίξε παρόλα αυτά @@ -3683,4 +3634,17 @@ Ακρόαση… Η φωνητική πληκτρολόγηση δεν είναι διαθέσιμη σε αυτή τη συσκευή. Απαιτείται πρόσβαση στο μικρόφωνο για φωνητική πληκτρολόγηση. + Λήψη πρόσθετων… + Όνομα + Περισσότερες λήψεις + Όλες οι κατηγορίες + Δεν βρέθηκαν πρόσθετα + Δεν ήταν δυνατή η σύνδεση με %1$s + Δεν υπάρχει αρχείο για αυτή την έκδοση του παιχνιδιού + Επαναφορά θέσεων + Εμφάνιση ετικέτας + Δεν ήταν δυνατή η εισαγωγή αυτής της διάταξης + + Κατακόκκινο χιόνι + diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml index 9d82f26077..4d25cdafe3 100644 --- a/app/src/main/res/values-es/strings.xml +++ b/app/src/main/res/values-es/strings.xml @@ -261,24 +261,8 @@ Táctil de Steam (beta) Los controles táctiles de Steam se activan en la próxima sesión de Steam Disposición táctil de Steam - Teclas en pantalla - Añadir tecla - Texto - Tamaño - Opacidad - Tecla - Gatillo - Alternar Exportar - Píldora - Redondeado - Círculo - Rectángulo - Tecla Imagen - Borrar - Disposición - Guardar Mueve, cambia de tamaño u oculta los controles táctiles de este juego; se guarda en Steam Mando Cómo ve Steam tu mando. Se aplica en la próxima sesión. @@ -557,6 +541,7 @@ Ningún DroidDeck-Proton lo usa Las actualizaciones Auto esperan una conexión. No se pudieron buscar nuevas versiones. + No se pudo instalar %1$s. Las nuevas versiones esperan una conexión sin límite de datos, el ahorro de batería desactivado y el dispositivo frío. %1$s actualizado a %2$s. %1$s %2$s cerró un juego en segundos dos veces, así que Auto volvió a %3$s. @@ -758,7 +743,6 @@ Automático Activado Desactivado - Cursor Teclado de PC Android Segunda pantalla @@ -1105,43 +1089,25 @@ 3 · ADB desde ordenador (último recurso) Solo si la Depuración inalámbrica no está disponible. 3 · ADB desde ordenador (último recurso) - Cuándo aparecen los controles táctiles en una sesión Color - Tinte de los botones en pantalla Personalizado Opacidad %1$d%% Tamaño de botones - 100% es el tamaño estándar - Clic del stick - Toca dos veces y mantén un stick para L3 o R3 Sticks adaptables Vibración en juegos Zona muerta de la palanca Botón Steam Botón QAM Botón de teclado - Pulsa Select para abrir el menú de Steam - En lugar de View/Select. Joy-Con como mandos de RV Mientras SteamVR está activo, cada Joy-Con es una mano. Apúntalo al frente y mantén Home o Captura para recentrarlo. Animaciones - Los sticks aparecen donde tocas y se ocultan al soltar + Avisar cuando queda poco almacenamiento + Preguntar antes de iniciar Steam cuando el espacio libre es inferior a 10 GB. Disposición - Posiciones personalizadas guardadas - Ajustada a esta pantalla Editar Restablecer - Asignación de botones - Cada botón envía su propia entrada - %1$d de %2$d botones reasignados - Configurar - Restablecer mando - Restaura todos los ajustes del mando y la disposición - Lo que cada botón en pantalla envía al juego. Oculto quita el botón. - Botones en pantalla - Predeterminados - Restablecer asignación Apps de Android %1$d apps No se encontraron apps de Android que se puedan abrir. @@ -1222,7 +1188,6 @@ Aún no hay controladores listados. Actualiza para buscarlos. Ocultar controladores de otras GPU Mostrar controladores de otras GPU - Avanzado Compatibilidad con UBWC Prueba Activado o Desactivado si Steam o Android muestran franjas o parpadeos. Se aplica a ambos controladores después de reiniciar DroidDeck. Reinicia DroidDeck para aplicarlo. @@ -1262,16 +1227,8 @@ Mando Stick Trackpad - Cambiar entre stick y trackpad - Más controles de Deck - PANELES Cerrar controles de la segunda pantalla Abrir menú de Steam - Izquierdo · superior - Izquierdo · inferior - Derecho · superior - Derecho · inferior - Clic Trackpad derecho Trackpad izquierdo Trackpad @@ -1802,6 +1759,7 @@ Ámbar Rojo Rosa + Gris Botón A Botón B Botón X @@ -1819,9 +1777,7 @@ Cruceta izquierda Cruceta derecha Oculto - Arrastra para mover Ajustar - Guardar Nada Teclado + trackpad Terminal @@ -1834,13 +1790,6 @@ Descargando caché de shaders En pausa Esperando para empezar - Notificaciones - DroidDeck – pantalla inferior - Muestra aquí los mensajes y otras notificaciones mientras juegas. Solo permanecen en esta pantalla y no se guardan. Android lo pregunta una vez: su página de acceso a notificaciones se abre en esta pantalla; activa DroidDeck ahí y esta página volverá sola. - Permitir acceso a notificaciones - No hay notificaciones. - %1$d notificaciones - Borrar Capturas de pantalla Los FPS requieren el HUD activado Velocidad @@ -2669,7 +2618,6 @@ Logros no disponibles; revisa los registros de la sesión Logro desbloqueado Pegar - Pad direccional Stick izquierdo Stick derecho Menú @@ -2930,6 +2878,8 @@ Arriba Abajo Centro (predeterminado) + Izquierda + Derecha Almacenamiento Midiendo el almacenamiento… La primera vez tarda unos segundos. @@ -3036,6 +2986,7 @@ Solo quedan %1$s libres Steam necesita espacio para actualizaciones y cachés de shaders. Por debajo de 10 GB, las actualizaciones de juegos pueden fallar a medias. Se pueden liberar %1$s sin riesgo. + No volver a avisarme Liberar espacio Jugar igualmente @@ -3722,4 +3673,17 @@ Escuchando… El dictado por voz no está disponible en este dispositivo. Se necesita acceso al micrófono para el dictado por voz. + Descargar complementos… + Nombre + Más descargados + Todas las categorías + No se han encontrado complementos + No se pudo conectar con %1$s + No hay ningún archivo para esta versión del juego + Restablecer posiciones + Mostrar etiqueta + No se ha podido importar esta disposición + + Nieve escarlata + diff --git a/app/src/main/res/values-fi/strings.xml b/app/src/main/res/values-fi/strings.xml index 66d8ae0657..84629a9478 100644 --- a/app/src/main/res/values-fi/strings.xml +++ b/app/src/main/res/values-fi/strings.xml @@ -261,24 +261,8 @@ Steam-kosketus (beta) Steamin kosketusohjaimet otetaan käyttöön seuraavassa Steam-istunnossa Steamin kosketusasettelu - Näytön näppäimet - Lisää näppäin - Teksti - Koko - Peittävyys - Näppäin - Liipaisin - Vaihda Vie - Kapseli - Pyöristetty - Ympyrä - Suorakaide - Näppäin Kuva - Tyhjennä - Asettelu - Tallenna Siirrä, muuta kokoa tai piilota tämän pelin kosketusohjaimet; tallennetaan Steamiin Ohjain Miten Steam näkee ohjaimesi. Tulee voimaan seuraavassa istunnossa. @@ -555,6 +539,7 @@ Mikään DroidDeck-Proton ei käytä tätä Auto-päivitykset odottavat yhteyttä. Uusia versioita ei voitu tarkistaa. + Kohdetta %1$s ei voitu asentaa. Uudet versiot odottavat rajoittamatonta yhteyttä, virransäästön poistoa ja viileää laitetta. %1$s päivitetty versioon %2$s. %1$s %2$s sulki pelin sekunneissa kahdesti, joten Auto palasi versioon %3$s. @@ -756,7 +741,6 @@ Automaattinen Päällä Pois - Osoitin PC-näppäimistö Android Toinen näyttö @@ -1099,43 +1083,25 @@ 3 · Tietokoneen ADB (viimeinen keino) Vain jos Langaton virheenkorjaus ei ole käytettävissä. 3 · Tietokoneen ADB (viimeinen keino) - Kun kosketusohjaimet näkyvät istunnossa Väri - Näytön painikkeiden sävy Mukautettu Peittävyys %1$d %% Painikkeiden koko - 100% on vakiokoko - Sauvan napsautus - Kaksoisnapauta ja pidä sauvaa pohjassa saadaksesi L3:n tai R3:n Mukautuvat sauvat Pelin tärinä Ohjaussauvan kuollut alue Steam-painike QAM-painike Näppäimistöpainike - Avaa Steam-valikko Select-painikkeella - View/Select-painikkeen sijaan. Joy-Conit VR-ohjaimina Kun SteamVR on käynnissä, jokainen Joy-Con toimii kätenä. Osoita sillä eteenpäin ja pidä Home- tai Capture-painiketta pohjassa keskittääksesi sen uudelleen. Animaatiot - Sauvat tulevat näkyviin siihen, mihin kosketat, ja piiloutuvat, kun vapautat + Varoita, kun tallennustila on vähissä + Kysy ennen Steamin käynnistämistä, kun vapaata tilaa on alle 10 Gt. Asettelu - Mukautetut sijainnit tallennettu - Sovitettu tähän näyttöön Muokkaa Palauta - Painikkeiden kartoitus - Jokainen painike lähettää oman syötteensä - %1$d/%2$d painiketta uudelleenkartoitettu - Määritä - Palauta ohjain - Palauttaa kaikki ohjaimen asetukset ja asettelun - Mitä mikä näytön painike lähettää pelille. Piilotettu poistaa painikkeen. - Näytön painikkeet - Oletukset - Palauta kartoitus Android-sovellukset %1$d sovellusta Käynnistettäviä Android-sovelluksia ei löytynyt. @@ -1215,7 +1181,6 @@ Ei ajureita listattuna vielä. Päivitä etsiäksesi niitä. Piilota muiden GPU:iden ajurit Näytä muiden GPU:iden ajurit - Lisäasetukset UBWC-yhteensopivuus Kokeile Päällä tai Pois, jos Steamissa tai Androidissa näkyy raitoja tai välkyntää. Koskee molempia ajureita DroidDeckin uudelleenkäynnistyksen jälkeen. Käynnistä DroidDeck uudelleen, jotta muutos tulee voimaan. @@ -1255,16 +1220,8 @@ Peliohjain Tatti Kosketuslevy - Vaihda tatin ja kosketuslevyn välillä - Lisää Deck-ohjaimia - PANEELIT Sulje toisen näytön ohjaimet Avaa Steam-valikko - Vasen · ylä - Vasen · ala - Oikea · ylä - Oikea · ala - Napsautus Oikea kosketuslevy Vasen kosketuslevy Kosketuslevy @@ -1778,6 +1735,7 @@ Meripihka Punainen Pinkki + Harmaa A-painike B-painike X-painike @@ -1795,9 +1753,7 @@ D-padi vasen D-padi oikea Piilotettu - Vedä siirtääksesi Kohdistus - Tallenna Ei mitään Näppäimistö + kosketuslevy Pääte @@ -1810,13 +1766,6 @@ Ladataan sävytinvälimuistia Keskeytetty Odottaa alkamista - Ilmoitukset - DroidDeckin alanäyttö - Näytä viestit ja muut ilmoitukset täällä pelatessasi. Ne näkyvät vain tällä näytöllä, eikä niitä tallenneta. Android kysyy kerran: sen ilmoitusten käyttöoikeussivu avautuu tälle näytölle; kytke DroidDeck siellä päälle, niin tämä sivu palaa itsestään. - Salli ilmoitusten käyttö - Ei ilmoituksia. - Ilmoituksia: %1$d - Tyhjennä Kuvakaappaukset FPS vaatii HUD:n päälle Nopeus @@ -2645,7 +2594,6 @@ Saavutukset ei käytettävissä; tarkista istunnon lokit Saavutus avattu Liitä - Ristiohjain Vasen tatti Oikea tatti Valikko @@ -2901,6 +2849,8 @@ Ylhäällä Alhaalla Keskellä (oletus) + Vasen + Oikea Tallennustila Mitataan tallennustilaa… Ensimmäisellä kerralla kestää muutaman sekunnin. @@ -3007,6 +2957,7 @@ Vain %1$s vapaana Steam tarvitsee tilaa päivityksille ja shadervälimuistille. Alle 10 GB:ssä pelipäivitykset voivat keskeytyä puolivälissä. %1$s voidaan vapauttaa turvallisesti. + Älä varoita enää Vapauta tilaa Pelaa silti @@ -3683,4 +3634,17 @@ Kuunnellaan… Puhekirjoitus ei ole käytettävissä tällä laitteella. Puhekirjoitus vaatii mikrofonin käyttöoikeuden. + Lataa lisäosia… + Nimi + Ladatuimmat + Kaikki luokat + Lisäosia ei löytynyt + Yhteyttä palveluun %1$s ei saatu + Tälle peliversiolle ei ole tiedostoa + Palauta sijainnit + Näytä nimi + Tätä asettelua ei voitu tuoda + + Helakanpunainen lumi + diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index 138b28c6a5..51c6332266 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -261,24 +261,8 @@ Tactile Steam (bêta) Les commandes tactiles de Steam s\'activeront à la prochaine session Steam Disposition tactile Steam - Touches à l\'écran - Ajouter une touche - Texte - Taille - Opacité - Touche - Gâchette - Basculer Exporter - Pilule - Arrondi - Cercle - Rectangle - Touche Image - Effacer - Disposition - Enregistrer Déplacez, redimensionnez ou masquez les commandes tactiles de ce jeu ; enregistré dans Steam Manette Comment Steam reconnaît votre manette. Prendra effet à la prochaine session. @@ -557,6 +541,7 @@ Aucun DroidDeck-Proton ne l\'utilise Les mises à jour Auto attendent une connexion. Impossible de vérifier les nouvelles versions. + Impossible d\'installer %1$s. Les nouvelles versions attendent une connexion non limitée, l\'économiseur de batterie désactivé et un appareil frais. %1$s mis à jour vers %2$s. %1$s %2$s a fermé un jeu en quelques secondes deux fois, Auto est donc revenu à %3$s. @@ -758,7 +743,6 @@ Automatique Activé Désactivé - Curseur Clavier PC Android Deuxième écran @@ -1105,43 +1089,25 @@ 3 · ADB sur PC (en dernier recours) À utiliser uniquement si le débogage sans fil n\'est pas disponible. 3 · ADB sur PC (en dernier recours) - Lors de l\'affichage des commandes à l\'écran en session Couleur - Teinte des boutons à l\'écran Personnalisée Opacité %1$d%% Taille des boutons - 100 % correspond à la taille standard - Clic du joystick - Double-cliquez et maintenez le joystick pour déclencher L3 ou R3 Joystick adaptatif Vibration du jeu Zone morte du stick Bouton Steam Bouton QAM Bouton clavier - Appuyez sur Select pour ouvrir le menu Steam - À la place de View/Select. Joy-Con comme manettes VR Pendant que SteamVR tourne, chaque Joy-Con devient une main. Pointez-le devant vous et maintenez Home ou Capture pour le recentrer. Animations - Le joystick apparaîtra là où vous touchez et se masquera une fois relâché + Avertir quand l\'espace de stockage est faible + Demander avant de lancer Steam lorsque l\'espace libre est inférieur à 10 Go. Disposition - Position personnalisée enregistrée - Adapté à cet écran Modifier Réinitialiser - Mappage des boutons - Chaque bouton envoie l\'entrée d\'origine correspondante - %1$d/%2$d boutons remappés - Configurer - Réinitialiser la manette - Restaurer tous les paramètres et dispositions de la manette - Définissez l\'entrée que chaque bouton à l\'écran envoie au jeu. « Masquer » supprimera ce bouton. - Boutons à l\'écran - Valeurs par défaut - Réinitialiser le mappage Applications Android %1$d applications Aucune application Android lançable trouvée. @@ -1222,7 +1188,6 @@ Aucun pilote répertorié pour le moment. Actualisez pour rechercher des pilotes. Masquer les pilotes des autres GPU Afficher les pilotes des autres GPU - Avancé Compatibilité UBWC Essayez Activé ou Désactivé si Steam ou Android affiche des bandes ou des scintillements. S\'applique aux deux pilotes après le redémarrage de DroidDeck. Redémarrez DroidDeck pour appliquer. @@ -1262,16 +1227,8 @@ Manette Joystick Pavé tactile - Basculer entre joystick et pavé tactile - Plus de commandes Deck - PANNEAUX Fermer les commandes du deuxième écran Ouvrir le menu Steam - Gauche · Haut - Gauche · Bas - Droite · Haut - Droite · Bas - Cliquer Pavé tactile droit Pavé tactile gauche Pavé tactile @@ -1802,6 +1759,7 @@ Ambre Rouge Rose + Gris Bouton A Bouton B Bouton X @@ -1819,9 +1777,7 @@ Croix gauche Croix droite Masqué - Faites glisser pour déplacer Aimanter - Enregistrer Aucun Clavier + pavé tactile Terminal @@ -1834,13 +1790,6 @@ Téléchargement du cache de shaders En pause En attente de démarrage - Notifications - Écran inférieur DroidDeck - Affichez ici les messages et autres notifications pendant que vous jouez. Ils restent uniquement sur cet écran et ne sont pas enregistrés. Android ne le demande qu\'une fois : sa page d\'accès aux notifications s\'ouvre sur cet écran ; activez-y DroidDeck et cette page reviendra d\'elle-même. - Autoriser l\'accès aux notifications - Aucune notification. - %1$d notifications - Effacer Captures d\'écran Les FPS nécessitent d\'activer le HUD Vitesse @@ -2669,7 +2618,6 @@ Succès indisponibles ; consultez les journaux de session Succès débloqué Coller - Pavé directionnel Stick gauche Stick droit Menu @@ -2930,6 +2878,8 @@ Haut Bas Centre (par défaut) + Gauche + Droite Stockage Mesure du stockage… Cela prend quelques secondes la première fois. @@ -3036,6 +2986,7 @@ Seulement %1$s libres Steam a besoin de place pour les mises à jour et les caches de shaders. Sous 10 Go, les mises à jour de jeux peuvent échouer en cours de route. %1$s peuvent être libérés sans risque. + Ne plus m\'avertir Libérer de l\'espace Jouer quand même @@ -3722,4 +3673,17 @@ Écoute… La saisie vocale n\'est pas disponible sur cet appareil. L\'accès au micro est nécessaire pour la saisie vocale. + Télécharger des addons… + Nom + Les plus téléchargés + Toutes les catégories + Aucun addon trouvé + Impossible de joindre %1$s + Aucun fichier pour cette version du jeu + Réinitialiser les positions + Afficher le libellé + Impossible d’importer cette disposition + + Neige écarlate + diff --git a/app/src/main/res/values-hi/strings.xml b/app/src/main/res/values-hi/strings.xml index 60d50cd386..2217a027d4 100644 --- a/app/src/main/res/values-hi/strings.xml +++ b/app/src/main/res/values-hi/strings.xml @@ -261,24 +261,8 @@ Steam टच (बीटा) Steam के टच कंट्रोल अगले Steam सेशन से शुरू होंगे Steam टच लेआउट - स्क्रीन कुंजियाँ - कुंजी जोड़ें - टेक्स्ट - आकार - ओपैसिटी - कुंजी - ट्रिगर - टॉगल निर्यात - कैप्सूल - गोल - वृत्त - आयत - कुंजी छवि - हटाएं - लेआउट - सेव करें इस गेम के टच कंट्रोल को घुमाएं, आकार बदलें या छिपाएं; Steam में सेव होता है कंट्रोलर Steam आपके कंट्रोलर को किस तरह पहचानता है। अगले सेशन से लागू होता है। @@ -555,6 +539,7 @@ कोई DroidDeck-Proton इसका उपयोग नहीं करता Auto अपडेट कनेक्शन का इंतज़ार कर रहे हैं। नए बिल्ड नहीं जाँचे जा सके। + %1$s इंस्टॉल नहीं हो सका। नए बिल्ड बिना सीमा वाले कनेक्शन, बंद बैटरी सेवर और ठंडे डिवाइस का इंतज़ार करते हैं। %1$s को %2$s पर अपडेट किया गया। %1$s %2$s ने दो बार कुछ सेकंड में गेम बंद कर दिया, इसलिए Auto %3$s पर लौट गया। @@ -756,7 +741,6 @@ स्वचालित चालू बंद - कर्सर पीसी कीबोर्ड Android दूसरी स्क्रीन @@ -1099,43 +1083,25 @@ 3 · कंप्यूटर ADB (आख़िरी विकल्प) सिर्फ़ तब जब वायरलेस डीबगिंग उपलब्ध न हो। 3 · कंप्यूटर ADB (आख़िरी विकल्प) - सेशन में टच कंट्रोल कब दिखते हैं रंग - ऑन-स्क्रीन बटन का रंग कस्टम ओपैसिटी %1$d%% बटन का आकार - 100% मानक आकार है - स्टिक क्लिक - L3 या R3 के लिए किसी स्टिक को डबल-टैप करके दबाए रखें एडैप्टिव स्टिक गेम वाइब्रेशन स्टिक का डेड ज़ोन Steam बटन QAM बटन कीबोर्ड बटन - Steam मेनू खोलने के लिए Select दबाएं - View/Select के बदले। Joy-Con को VR कंट्रोलर की तरह SteamVR चलते समय हर Joy-Con एक हाथ बन जाता है। उसे सामने की ओर करें और फिर से केंद्रित करने के लिए Home या Capture दबाए रखें। एनिमेशन - आप जहां टच करें वहां स्टिक दिखते हैं और छोड़ने पर छिप जाते हैं + स्टोरेज कम होने पर चेतावनी दें + खाली जगह 10 GB से कम होने पर Steam शुरू करने से पहले पूछें। लेआउट - कस्टम पोज़िशन सेव हैं - इस स्क्रीन के हिसाब से फ़िट किया गया संपादित करें रीसेट करें - बटन मैपिंग - हर बटन अपना इनपुट भेजता है - %2$d में से %1$d बटन रीमैप किए गए - कॉन्फ़िगर करें - कंट्रोलर रीसेट करें - सभी कंट्रोलर सेटिंग और लेआउट वापस लाता है - हर ऑन-स्क्रीन बटन गेम को क्या भेजता है। Hidden बटन हटा देता है। - ऑन-स्क्रीन बटन - डिफ़ॉल्ट - मैपिंग रीसेट करें Android ऐप %1$d ऐप लॉन्च करने लायक कोई Android ऐप नहीं मिला। @@ -1215,7 +1181,6 @@ अभी कोई ड्राइवर सूचीबद्ध नहीं है। उन्हें देखने के लिए रीफ़्रेश करें। दूसरे GPU के ड्राइवर छिपाएं दूसरे GPU के ड्राइवर दिखाएं - एडवांस्ड UBWC कम्पैटिबिलिटी अगर Steam या Android में धारियां या झिलमिलाहट दिखे तो चालू या बंद आज़माएं। DroidDeck रीस्टार्ट करने के बाद दोनों ड्राइवर पर लागू होता है। लागू करने के लिए DroidDeck रीस्टार्ट करें। @@ -1255,16 +1220,8 @@ गेमपैड स्टिक ट्रैकपैड - स्टिक और ट्रैकपैड के बीच बदलें - और Deck कंट्रोल - पैनल दूसरी स्क्रीन के कंट्रोल बंद करें Steam मेनू खोलें - बायां · ऊपर - बायां · नीचे - दायां · ऊपर - दायां · नीचे - क्लिक दायां ट्रैकपैड बायां ट्रैकपैड ट्रैकपैड @@ -1778,6 +1735,7 @@ एम्बर लाल गुलाबी + धूसर A बटन B बटन X बटन @@ -1795,9 +1753,7 @@ D-pad बायां D-pad दायां छिपा हुआ - ले जाने के लिए ड्रैग करें स्नैप - सेव करें कोई नहीं कीबोर्ड + ट्रैकपैड टर्मिनल @@ -1810,13 +1766,6 @@ शेडर कैश डाउनलोड हो रहा है रुका हुआ शुरू होने का इंतज़ार - नोटिफ़िकेशन - DroidDeck नीचे की स्क्रीन - खेलते समय मैसेज और अन्य नोटिफ़िकेशन यहां दिखाएं। वे सिर्फ़ इसी स्क्रीन पर रहते हैं और सेव नहीं होते। Android एक बार पूछता है: उसका नोटिफ़िकेशन ऐक्सेस पेज इसी स्क्रीन पर खुलता है; वहां DroidDeck चालू करें और यह पेज अपने-आप वापस आ जाएगा। - नोटिफ़िकेशन ऐक्सेस की अनुमति दें - कोई नोटिफ़िकेशन नहीं। - %1$d नोटिफ़िकेशन - साफ़ करें स्क्रीनशॉट FPS के लिए HUD चालू होना चाहिए स्पीड @@ -2645,7 +2594,6 @@ उपलब्धियां उपलब्ध नहीं; सेशन लॉग देखें उपलब्धि अनलॉक हुई पेस्ट करें - डी-पैड बायां स्टिक दायां स्टिक मेन्यू @@ -2901,6 +2849,8 @@ ऊपर नीचे बीच में (डिफ़ॉल्ट) + बाएँ + दाएँ स्टोरेज स्टोरेज मापा जा रहा है… पहली बार कुछ सेकंड लगते हैं। @@ -3007,6 +2957,7 @@ सिर्फ़ %1$s खाली Steam को अपडेट और शेडर कैश के लिए जगह चाहिए। 10 GB से कम पर गेम अपडेट बीच में विफल हो सकते हैं। %1$s सुरक्षित रूप से खाली किया जा सकता है। + मुझे दोबारा चेतावनी न दें जगह खाली करें फिर भी खेलें @@ -3683,4 +3634,17 @@ सुन रहा है… इस डिवाइस पर आवाज़ से टाइपिंग उपलब्ध नहीं है। आवाज़ से टाइपिंग के लिए माइक्रोफ़ोन की अनुमति चाहिए। + ऐडऑन डाउनलोड करें… + नाम + सबसे ज़्यादा डाउनलोड + सभी श्रेणियाँ + कोई ऐडऑन नहीं मिला + %1$s तक नहीं पहुँच सके + गेम के इस संस्करण के लिए कोई फ़ाइल नहीं है + स्थितियाँ रीसेट करें + लेबल दिखाएँ + यह लेआउट इम्पोर्ट नहीं किया जा सका + + किरमिज़ी बर्फ + diff --git a/app/src/main/res/values-hu/strings.xml b/app/src/main/res/values-hu/strings.xml index b67b5dfeaf..05021ad2f9 100644 --- a/app/src/main/res/values-hu/strings.xml +++ b/app/src/main/res/values-hu/strings.xml @@ -261,24 +261,8 @@ Steam érintés (béta) A Steam érintésvezérlői a következő Steam-munkamenettől lépnek életbe Steam érintéskiosztás - Képernyőgombok - Gomb hozzáadása - Szöveg - Méret - Átlátszatlanság - Gomb - Ravasz - Váltás Exportálás - Kapszula - Lekerekített - Kör - Téglalap - Billentyű Kép - Törlés - Elrendezés - Mentés Mozgassa, méretezze vagy rejtse el a játék érintésvezérlőit; a Steamben mentve Kontroller Hogyan látja a Steam a kontrollerét. A következő munkamenettől érvényes. @@ -555,6 +539,7 @@ Egyik DroidDeck-Proton sem használja Az Auto frissítések kapcsolatra várnak. Nem sikerült új buildeket keresni. + Nem sikerült telepíteni: %1$s. Az új buildek nem forgalomkorlátos kapcsolatra, kikapcsolt energiatakarékos módra és hűvös eszközre várnak. %1$s frissítve: %2$s. %1$s %2$s kétszer is másodperceken belül bezárt egy játékot, ezért az Auto visszaállt erre: %3$s. @@ -756,7 +741,6 @@ Automatikus Be Ki - Kurzor PC-billentyűzet Android Második képernyő @@ -1099,43 +1083,25 @@ 3 · Számítógépes ADB (végső megoldás) Csak ha a vezeték nélküli hibakeresés nem elérhető. 3 · Számítógépes ADB (végső megoldás) - Mikor jelennek meg az érintésvezérlők egy munkamenetben Szín - A képernyőn megjelenő gombok árnyalata Egyéni Átlátszatlanság %1$d%% Gombméret - A 100% az alapértelmezett méret - Kar kattintása - Koppintson duplán, és tartsa lenyomva a kart az L3 vagy R3 érdekében Adaptív karok Játékvibráció Kar holtzónája Steam gomb QAM gomb Billentyűzet gomb - Nyomja meg a Select gombot a Steam-menü megnyitásához - A View/Select helyett. Joy-Conok VR-kontrollerként Amíg a SteamVR fut, minden Joy-Con egy kéz lesz. Irányítsa előre, és tartsa lenyomva a Home vagy a Capture gombot az újraközpontosításhoz. Animációk - A karok ott jelennek meg, ahol megérinti őket, és elengedéskor elrejtőznek + Figyelmeztetés kevés tárhely esetén + Rákérdezés a Steam indítása előtt, ha 10 GB-nál kevesebb a szabad hely. Elrendezés - Egyéni pozíciók mentve - Ehhez a képernyőhöz illesztve Szerkesztés Visszaállítás - Gombkiosztás - Minden gomb a saját bemenetét küldi - %1$d/%2$d gomb átkiosztva - Beállítás - Kontroller visszaállítása - Visszaállítja az összes kontrollerbeállítást és az elrendezést - Mit küld a játéknak az egyes képernyőgombok. A Rejtett eltávolítja a gombot. - Képernyőgombok - Alapértelmezések - Kiosztás visszaállítása Android-alkalmazások %1$d alkalmazás Nem található indítható Android-alkalmazás. @@ -1215,7 +1181,6 @@ Még nincs felsorolt illesztőprogram. Frissítsen a kereséshez. Más GPU-k illesztőprogramjainak elrejtése Más GPU-k illesztőprogramjainak megjelenítése - Speciális UBWC-kompatibilitás Próbálja a Be vagy Ki beállítást, ha a Steam vagy az Android csíkokat vagy villódzást mutat. A DroidDeck újraindítása után mindkét illesztőprogramra érvényes. Indítsa újra a DroidDecket az alkalmazáshoz. @@ -1255,16 +1220,8 @@ Kontroller Kar Érintőpad - Váltás a kar és az érintőpad között - További Deck-vezérlők - PANELEK Második képernyős vezérlők bezárása Steam-menü megnyitása - Bal · felső - Bal · alsó - Jobb · felső - Jobb · alsó - Kattintás Jobb érintőpad Bal érintőpad Érintőpad @@ -1778,6 +1735,7 @@ Borostyán Piros Rózsaszín + Szürke A gomb B gomb X gomb @@ -1795,9 +1753,7 @@ D-pad balra D-pad jobbra Rejtett - Húzza a mozgatáshoz Rögzítés - Mentés Nincs Billentyűzet + érintőpad Terminál @@ -1810,13 +1766,6 @@ Shader-gyorsítótár letöltése Szüneteltetve Indításra vár - Értesítések - DroidDeck alsó képernyő - Itt jelenítheti meg az üzeneteket és más értesítéseket játék közben. Csak ezen a képernyőn láthatók, és nem kerülnek mentésre. Az Android egyszer kérdez rá: az értesítési hozzáférés oldala ezen a képernyőn nyílik meg; kapcsolja be ott a DroidDecket, és ez az oldal magától visszatér. - Értesítési hozzáférés engedélyezése - Nincsenek értesítések. - %1$d értesítés - Törlés Képernyőképek Az FPS-hez be kell kapcsolni a HUD-ot Sebesség @@ -2645,7 +2594,6 @@ Eredmények nem elérhetők; nézd meg a munkamenet-naplókat Eredmény feloldva Beillesztés - Iránykereszt Bal kar Jobb kar Menü @@ -2901,6 +2849,8 @@ Fent Lent Középen (alapértelmezett) + Bal + Jobb Tárhely Tárhely mérése… Első alkalommal pár másodpercig tart. @@ -3007,6 +2957,7 @@ Csak %1$s szabad A Steamnek hely kell a frissítésekhez és a shader-gyorsítótárhoz. 10 GB alatt a játékfrissítések félúton meghiúsulhatnak. %1$s biztonságosan felszabadítható. + Ne figyelmeztessen többé Hely felszabadítása Játék így is @@ -3683,4 +3634,17 @@ Figyelés… A hangalapú gépelés nem érhető el ezen az eszközön. A hangalapú gépeléshez mikrofon-hozzáférés szükséges. + Kiegészítők letöltése… + Név + Legtöbbet letöltött + Minden kategória + Nem található kiegészítő + A(z) %1$s nem érhető el + Nincs fájl ehhez a játékverzióhoz + Pozíciók visszaállítása + Címke megjelenítése + Ez az elrendezés nem importálható + + Skarlátvörös hó + diff --git a/app/src/main/res/values-in/strings.xml b/app/src/main/res/values-in/strings.xml index 967a7d5e34..b995a41af6 100644 --- a/app/src/main/res/values-in/strings.xml +++ b/app/src/main/res/values-in/strings.xml @@ -261,24 +261,8 @@ Sentuhan Steam (beta) Kontrol sentuh Steam dimulai pada sesi Steam berikutnya Tata letak sentuh Steam - Tombol layar - Tambah tombol - Teks - Ukuran - Opasitas - Tombol - Pemicu - Alih Ekspor - Kapsul - Membulat - Lingkaran - Persegi panjang - Tombol Gambar - Hapus - Tata letak - Simpan Pindahkan, ubah ukuran, atau sembunyikan kontrol sentuh gim ini; disimpan ke Steam Pengontrol Cara Steam mendeteksi pengontrol Anda. Berlaku pada sesi berikutnya. @@ -553,6 +537,7 @@ Tidak ada DroidDeck-Proton yang memakainya Pembaruan Auto menunggu koneksi. Tidak dapat memeriksa build baru. + Tidak dapat memasang %1$s. Build baru menunggu koneksi tanpa kuota, penghemat baterai mati, dan perangkat yang dingin. %1$s diperbarui ke %2$s. %1$s %2$s menutup game dalam hitungan detik dua kali, jadi Auto kembali ke %3$s. @@ -754,7 +739,6 @@ Otomatis Aktif Nonaktif - Kursor Keyboard PC Android Layar kedua @@ -1093,43 +1077,25 @@ 3 · ADB komputer (upaya terakhir) Hanya jika Debugging nirkabel tidak tersedia. 3 · ADB komputer (upaya terakhir) - Saat kontrol sentuh muncul dalam sesi Warna - Warna tombol di layar Khusus Opasitas %1$d%% Ukuran tombol - 100% adalah ukuran standar - Klik stik - Ketuk dua kali dan tahan sebuah stik untuk L3 atau R3 Stik adaptif Getaran gim Zona mati stik Tombol Steam Tombol QAM Tombol keyboard - Tekan Select untuk membuka menu Steam - Bukan View/Select. Joy-Con sebagai pengontrol VR Saat SteamVR berjalan, setiap Joy-Con menjadi tangan. Arahkan ke depan lalu tahan Home atau Capture untuk memusatkannya kembali. Animasi - Stik muncul di tempat Anda menyentuh dan tersembunyi saat dilepas + Peringatkan saat penyimpanan hampir penuh + Tanya sebelum memulai Steam saat ruang kosong di bawah 10 GB. Tata letak - Posisi khusus disimpan - Disesuaikan dengan layar ini Edit Reset - Pemetaan tombol - Setiap tombol mengirim input-nya sendiri - %1$d dari %2$d tombol dipetakan ulang - Konfigurasi - Reset pengontrol - Mengembalikan semua setelan pengontrol dan tata letak - Apa yang dikirim setiap tombol di layar ke gim. Tersembunyi menghapus tombol tersebut. - Tombol di layar - Default - Reset pemetaan Aplikasi Android %1$d aplikasi Tidak ada aplikasi Android yang dapat dijalankan. @@ -1208,7 +1174,6 @@ Belum ada driver yang terdaftar. Segarkan untuk mencarinya. Sembunyikan driver untuk GPU lain Tampilkan driver untuk GPU lain - Lanjutan Kompatibilitas UBWC Coba Aktif atau Nonaktif jika Steam atau Android menampilkan garis-garis atau berkedip. Berlaku untuk kedua driver setelah DroidDeck dimulai ulang. Mulai ulang DroidDeck untuk menerapkan. @@ -1248,16 +1213,8 @@ Gamepad Stik Trackpad - Beralih antara stik dan trackpad - Kontrol Deck lainnya - PANEL Tutup kontrol layar kedua Buka menu Steam - Kiri · atas - Kiri · bawah - Kanan · atas - Kanan · bawah - Klik Trackpad kanan Trackpad kiri Trackpad @@ -1754,6 +1711,7 @@ Ambar Merah Merah muda + Abu-abu Tombol A Tombol B Tombol X @@ -1771,9 +1729,7 @@ D-pad kiri D-pad kanan Tersembunyi - Seret untuk memindahkan Ratakan - Simpan Tidak ada Keyboard + trackpad Terminal @@ -1786,13 +1742,6 @@ Mengunduh cache shader Dijeda Menunggu dimulai - Notifikasi - Layar bawah DroidDeck - Tampilkan pesan dan notifikasi lain di sini saat Anda bermain. Notifikasi hanya muncul di layar ini dan tidak disimpan. Android hanya meminta sekali: halaman akses notifikasinya terbuka di layar ini; aktifkan DroidDeck di sana dan halaman ini akan kembali dengan sendirinya. - Izinkan akses notifikasi - Tidak ada notifikasi. - %1$d notifikasi - Hapus Tangkapan layar FPS memerlukan HUD aktif Kecepatan @@ -2618,7 +2567,6 @@ Pencapaian tidak tersedia; periksa log sesi Pencapaian terbuka Tempel - Tombol arah Stik kiri Stik kanan Menu @@ -2872,6 +2820,8 @@ Atas Bawah Tengah (bawaan) + Kiri + Kanan Penyimpanan Mengukur penyimpanan… Butuh beberapa detik pada kali pertama. @@ -2978,6 +2928,7 @@ Hanya %1$s kosong Steam butuh ruang untuk pembaruan dan cache shader. Di bawah 10 GB, pembaruan gim bisa gagal di tengah. %1$s bisa dikosongkan dengan aman. + Jangan peringatkan saya lagi Kosongkan ruang Tetap main @@ -3653,4 +3604,17 @@ Mendengarkan… Ketik dengan suara tidak tersedia di perangkat ini. Akses mikrofon diperlukan untuk mengetik dengan suara. + Unduh addon… + Nama + Paling banyak diunduh + Semua kategori + Tidak ada addon ditemukan + Tidak dapat menjangkau %1$s + Tidak ada file untuk versi game ini + Atur ulang posisi + Tampilkan label + Tata letak ini tidak dapat diimpor + + Salju Kirmizi + diff --git a/app/src/main/res/values-it/strings.xml b/app/src/main/res/values-it/strings.xml index 13570c0b86..0e91246f09 100644 --- a/app/src/main/res/values-it/strings.xml +++ b/app/src/main/res/values-it/strings.xml @@ -261,24 +261,8 @@ Touch di Steam (beta) I controlli touch di Steam partono dalla prossima sessione Steam Layout touch di Steam - Tasti a schermo - Aggiungi tasto - Testo - Dimensione - Opacità - Tasto - Grilletto - Attiva Esporta - Capsula - Arrotondato - Cerchio - Rettangolo - Tasto Immagine - Cancella - Layout - Salva Sposta, ridimensiona o nascondi i controlli touch di questo gioco; vengono salvati su Steam Controller Come Steam vede il tuo controller. Si applica dalla prossima sessione. @@ -557,6 +541,7 @@ Nessun DroidDeck-Proton lo usa Gli aggiornamenti Auto attendono una connessione. Impossibile cercare nuove build. + Impossibile installare %1$s. Le nuove build attendono una connessione non a consumo, il risparmio batteria disattivato e un dispositivo fresco. %1$s aggiornato a %2$s. %1$s %2$s ha chiuso un gioco in pochi secondi per due volte, quindi Auto è tornato a %3$s. @@ -758,7 +743,6 @@ Automatico Attivo Disattivato - Cursore Tastiera PC Android Secondo schermo @@ -1097,43 +1081,25 @@ 3 · ADB da computer (ultima risorsa) Solo se il Debug wireless non è disponibile. 3 · ADB da computer (ultima risorsa) - Quando i controlli touch appaiono in una sessione Colore - Tonalità dei tasti sullo schermo Personalizzato Opacità %1$d%% Dimensione dei tasti - 100% è la dimensione standard - Clic levetta - Tocca due volte e tieni premuta una levetta per L3 o R3 Levette adattive Vibrazione di gioco Zona morta dello stick Tasto Steam Tasto QAM Tasto tastiera - Premi Select per aprire il menu Steam - Invece di View/Select. Joy-Con come controller VR Mentre SteamVR è in esecuzione, ogni Joy-Con diventa una mano. Puntalo in avanti e tieni premuto Home o Capture per ricentrarlo. Animazioni - Le levette appaiono dove tocchi e si nascondono al rilascio + Avvisa quando lo spazio è scarso + Chiedi conferma prima di avviare Steam quando lo spazio libero è inferiore a 10 GB. Layout - Posizioni personalizzate salvate - Adattato a questo schermo Modifica Ripristina - Mappatura dei tasti - Ogni tasto invia il proprio input - %1$d di %2$d tasti rimappati - Configura - Ripristina controller - Ripristina tutte le impostazioni del controller e il layout - Cosa invia al gioco ciascun tasto sullo schermo. Nascosto rimuove il tasto. - Tasti sullo schermo - Predefiniti - Ripristina mappatura App Android %1$d app Nessuna app Android avviabile trovata. @@ -1214,7 +1180,6 @@ Ancora nessun driver elencato. Aggiorna per cercarli. Nascondi i driver per altre GPU Mostra i driver per altre GPU - Avanzate Compatibilità UBWC Prova Attivo o Disattivo se Steam o Android mostrano strisce o sfarfallii. Si applica a entrambi i driver dopo il riavvio di DroidDeck. Riavvia DroidDeck per applicare. @@ -1254,16 +1219,8 @@ Gamepad Levetta Trackpad - Alterna tra levetta e trackpad - Altri controlli Deck - PANNELLI Chiudi i controlli sul secondo schermo Apri il menu Steam - Sinistra · in alto - Sinistra · in basso - Destra · in alto - Destra · in basso - Clic Trackpad destro Trackpad sinistro Trackpad @@ -1794,6 +1751,7 @@ Ambra Rosso Rosa + Grigio Pulsante A Pulsante B Pulsante X @@ -1811,9 +1769,7 @@ Croce sinistra Croce destra Nascosto - Trascina per spostare Aggancia - Salva Nessuno Tastiera + trackpad Terminale @@ -1826,13 +1782,6 @@ Download della cache degli shader In pausa In attesa di avvio - Notifiche - Schermo inferiore DroidDeck - Mostra qui messaggi e altre notifiche mentre giochi. Restano solo su questo schermo e non vengono salvate. Android lo chiede una sola volta: la sua pagina di accesso alle notifiche si apre su questo schermo; attiva DroidDeck lì e questa pagina torna da sola. - Consenti accesso alle notifiche - Nessuna notifica. - %1$d notifiche - Cancella Screenshot Gli FPS richiedono l\'HUD attivo Velocità @@ -2661,7 +2610,6 @@ Obiettivi non disponibili; controlla i log della sessione Obiettivo sbloccato Incolla - Croce direzionale Stick sinistro Stick destro Menu @@ -2930,6 +2878,8 @@ In alto In basso Centro (predefinito) + Sinistra + Destra Archiviazione Misurazione dell\'archiviazione… La prima volta richiede qualche secondo. @@ -3036,6 +2986,7 @@ Solo %1$s liberi Steam ha bisogno di spazio per aggiornamenti e cache shader. Sotto i 10 GB gli aggiornamenti dei giochi possono fallire a metà. %1$s si possono liberare senza rischi. + Non avvisarmi più Libera spazio Gioca comunque @@ -3722,4 +3673,17 @@ In ascolto… La digitazione vocale non è disponibile su questo dispositivo. Per la digitazione vocale serve l\'accesso al microfono. + Scarica addon… + Nome + Più scaricati + Tutte le categorie + Nessun addon trovato + Impossibile raggiungere %1$s + Nessun file per questa versione del gioco + Ripristina posizioni + Mostra etichetta + Impossibile importare questo layout + + Neve scarlatta + diff --git a/app/src/main/res/values-ja/strings.xml b/app/src/main/res/values-ja/strings.xml index 948e062bb6..fc4fa0ffba 100644 --- a/app/src/main/res/values-ja/strings.xml +++ b/app/src/main/res/values-ja/strings.xml @@ -261,24 +261,8 @@ Steamタッチ(ベータ版) Steamのタッチコントロールは次回のSteamセッションから有効になります Steamタッチのレイアウト - 画面のキー - キーを追加 - テキスト - サイズ - 不透明度 - キー - トリガー - トグル エクスポート - カプセル - 角丸 - 円 - 四角 - キー 画像 - クリア - レイアウト - 保存 このゲームのタッチコントロールを移動、サイズ変更、非表示にします。Steamに保存されます コントローラー Steamがコントローラーを認識する方法。次回のセッションで有効になります。 @@ -553,6 +537,7 @@ これを使う DroidDeck-Proton はありません Auto 更新は接続を待っています。 新しいビルドを確認できませんでした。 + %1$s をインストールできませんでした。 新しいビルドは従量制でない接続、バッテリーセーバーのオフ、端末が熱くない状態を待ちます。 %1$s を %2$s に更新しました。 %1$s %2$s でゲームが数秒で 2 回終了したため、Auto は %3$s に戻しました。 @@ -754,7 +739,6 @@ 自動 オン オフ - カーソル PCキーボード Android セカンドスクリーン @@ -1093,43 +1077,25 @@ 3 · PC ADB(最終手段) ワイヤレスデバッグが利用できない場合にのみ使用してください。 3 · PC ADB(最終手段) - セッション中に画面コントローラーを表示する場合 色 - 画面ボタンの色調 カスタム 不透明度 %1$d%% ボタンのサイズ - 100% が標準サイズです - スティッククリック - スティックをダブルタップして長押しすると、L3またはR3がトリガーされます アダプティブスティック ゲームの振動 スティックのデッドゾーン Steamボタン QAMボタン キーボードボタン - Selectを押してSteamメニューを開く - View/Selectの代わり。 Joy-ConをVRコントローラーとして使う SteamVRの実行中は各Joy-Conが手になります。前に向けてHOMEボタンかキャプチャーボタンを長押しすると、向きを合わせ直します。 アニメーション - スティックは触れた位置に表示され、離すと非表示になります + ストレージ残量が少ないときに警告する + 空き容量が 10 GB 未満のとき、Steam を起動する前に確認します。 レイアウト - カスタム位置を保存済み - この画面に最適化済み 編集 リセット - キーマッピング - 各ボタンは対応する元の入力を送信します - %1$d/%2$d 個のボタンを再マッピング済み - 設定 - コントローラーをリセット - すべてのコントローラー設定とレイアウトを復元 - 各画面ボタンがゲームに送信する入力を設定します。「非表示」にするとそのボタンが削除されます。 - 画面ボタン - デフォルト - マッピングをリセット Androidアプリ %1$d 個のアプリ 起動可能なAndroidアプリが見つかりませんでした。 @@ -1208,7 +1174,6 @@ ドライバーがまだ一覧表示されていません。更新してドライバーを検索してください。 他のGPUのドライバーを非表示 他のGPUのドライバーを表示 - 詳細設定 UBWC互換性 SteamやAndroidで縞模様やちらつきが出る場合は、オンまたはオフを試してください。DroidDeckの再起動後、両方のドライバーに適用されます。 適用するにはDroidDeckを再起動してください。 @@ -1248,16 +1213,8 @@ ゲームパッド スティック トラックパッド - スティックとトラックパッドを切り替え - その他のDeckコントロール - パネル セカンドスクリーンコントロールを閉じる Steamメニューを開く - 左 · 上 - 左 · 下 - 右 · 上 - 右 · 下 - クリック 右トラックパッド 左トラックパッド トラックパッド @@ -1754,6 +1711,7 @@ アンバー レッド ピンク + グレー Aボタン Bボタン Xボタン @@ -1771,9 +1729,7 @@ 十字キー左 十字キー右 非表示 - ドラッグして移動 スナップ - 保存 なし キーボード + トラックパッド ターミナル @@ -1786,13 +1742,6 @@ シェーダーキャッシュをダウンロード中 一時停止中 開始待ち - 通知 - DroidDeck下部画面 - プレイ中、メッセージなどの通知をここに表示します。通知はこの画面にのみ表示され、保存されません。Androidによる確認は一度だけです。この画面に通知へのアクセスのページが開くので、そこでDroidDeckをオンにすると、このページに自動的に戻ります。 - 通知へのアクセスを許可 - 通知はありません。 - %1$d件の通知 - クリア スクリーンショット FPSの表示にはHUDをオンにする必要があります 速度 @@ -2618,7 +2567,6 @@ 実績を利用できません。セッションログを確認してください 実績を解除しました 貼り付け - 十字キー 左スティック 右スティック メニュー @@ -2872,6 +2820,8 @@ 上 下 中央(既定) + 左 + 右 ストレージ ストレージを集計しています… 初回の集計には数秒かかります。 @@ -2978,6 +2928,7 @@ 空きが %1$s のみです Steam は更新とシェーダーキャッシュ用の空き容量を必要とします。10 GB を下回ると、ゲームの更新が途中で失敗することがあります。 %1$s を安全に解放できます。 + 今後は警告しない 容量を空ける そのままプレイ @@ -3653,4 +3604,17 @@ 聞き取り中… このデバイスでは音声入力を使用できません。 音声入力にはマイクへのアクセスが必要です。 + アドオンをダウンロード… + 名前 + ダウンロード数順 + すべてのカテゴリ + アドオンが見つかりません + %1$s に接続できませんでした + このゲームバージョン用のファイルがありません + 位置をリセット + ラベルを表示 + このレイアウトをインポートできませんでした + + 緋色の雪 + diff --git a/app/src/main/res/values-ko/strings.xml b/app/src/main/res/values-ko/strings.xml index 3a42df73dd..d3363c1ae9 100644 --- a/app/src/main/res/values-ko/strings.xml +++ b/app/src/main/res/values-ko/strings.xml @@ -261,24 +261,8 @@ Steam 터치(베타) Steam 터치 컨트롤은 다음 Steam 세션부터 적용됩니다 Steam 터치 레이아웃 - 화면 키 - 키 추가 - 텍스트 - 크기 - 불투명도 - 키 - 트리거 - 토글 내보내기 - 캡슐 - 둥근 모서리 - 원 - 사각형 - 키 이미지 - 지우기 - 레이아웃 - 저장 이 게임의 터치 컨트롤을 옮기거나 크기를 바꾸거나 숨깁니다. Steam에 저장됩니다 컨트롤러 Steam이 컨트롤러를 인식하는 방식입니다. 다음 세션에 적용됩니다. @@ -553,6 +537,7 @@ 이를 사용하는 DroidDeck-Proton이 없습니다 Auto 업데이트가 연결을 기다리는 중입니다. 새 빌드를 확인할 수 없습니다. + %1$s을(를) 설치할 수 없습니다. 새 빌드는 데이터 무제한 연결, 배터리 세이버 꺼짐, 기기가 뜨겁지 않은 상태를 기다립니다. %1$s을(를) %2$s(으)로 업데이트했습니다. %1$s %2$s(으)로 게임이 몇 초 만에 두 번 종료되어 Auto가 %3$s(으)로 되돌렸습니다. @@ -754,7 +739,6 @@ 자동 켜기 끄기 - 커서 PC 키보드 Android 보조 화면 @@ -1093,43 +1077,25 @@ 3 · PC ADB(최후의 수단) 무선 디버깅을 사용할 수 없을 때만 사용하세요. 3 · PC ADB(최후의 수단) - 세션에서 화면 컨트롤을 표시할 때 색상 - 화면 버튼의 색조 사용자 정의 불투명도 %1$d%% 버튼 크기 - 100%가 표준 크기입니다 - 스틱 클릭 - 스틱을 두 번 탭한 후 길게 누르면 L3 또는 R3이 입력됩니다 적응형 스틱 게임 진동 스틱 데드존 Steam 버튼 QAM 버튼 키보드 버튼 - Select를 눌러 Steam 메뉴 열기 - View/Select 대신. Joy-Con을 VR 컨트롤러로 사용 SteamVR이 실행되는 동안 각 Joy-Con이 손이 됩니다. 앞을 향하게 한 채 HOME 또는 캡처 버튼을 길게 누르면 다시 가운데로 맞춥니다. 애니메이션 - 스틱이 터치한 위치에 나타나며 손을 떼면 숨겨집니다 + 저장 공간이 부족할 때 경고 + 여유 공간이 10GB 미만이면 Steam을 시작하기 전에 확인합니다. 레이아웃 - 사용자 정의 위치 저장됨 - 이 화면에 맞게 조정됨 편집 초기화 - 키 매핑 - 각 버튼은 해당하는 원본 입력을 전송합니다 - %1$d/%2$d개 버튼 다시 매핑됨 - 구성 - 컨트롤러 초기화 - 모든 컨트롤러 설정 및 레이아웃 복원 - 각 화면 버튼이 게임에 전송할 입력을 설정합니다. \"숨기기\"를 선택하면 해당 버튼이 제거됩니다. - 화면 버튼 - 기본값 - 매핑 초기화 Android 앱 %1$d개 앱 실행 가능한 Android 앱을 찾을 수 없습니다. @@ -1208,7 +1174,6 @@ 아직 나열된 드라이버가 없습니다. 새로 고쳐 드라이버를 찾으세요. 다른 GPU의 드라이버 숨기기 다른 GPU의 드라이버 표시 - 고급 UBWC 호환성 Steam이나 Android에 줄무늬나 깜박임이 보이면 켜기 또는 끄기를 시도하세요. DroidDeck을 다시 시작한 후 두 드라이버 모두에 적용됩니다. 적용하려면 DroidDeck을 다시 시작하세요. @@ -1248,16 +1213,8 @@ 게임패드 스틱 터치패드 - 스틱과 터치패드 간 전환 - Deck 컨트롤 더 보기 - 패널 보조 화면 컨트롤 닫기 Steam 메뉴 열기 - 왼쪽 · 위 - 왼쪽 · 아래 - 오른쪽 · 위 - 오른쪽 · 아래 - 클릭 오른쪽 터치패드 왼쪽 터치패드 터치패드 @@ -1754,6 +1711,7 @@ 호박색 빨간색 분홍색 + 회색 A 버튼 B 버튼 X 버튼 @@ -1771,9 +1729,7 @@ 방향 패드 왼쪽 방향 패드 오른쪽 숨김 - 드래그하여 이동 맞춤 - 저장 없음 키보드 + 터치패드 터미널 @@ -1786,13 +1742,6 @@ 셰이더 캐시 다운로드 중 일시 중지됨 시작 대기 중 - 알림 - DroidDeck 하단 화면 - 플레이하는 동안 메시지와 기타 알림을 여기에 표시합니다. 알림은 이 화면에만 표시되며 저장되지 않습니다. Android에서 한 번만 묻습니다. 이 화면에 알림 접근 권한 페이지가 열리면 DroidDeck을 켜세요. 그러면 이 페이지로 자동으로 돌아옵니다. - 알림 접근 허용 - 알림이 없습니다. - 알림 %1$d개 - 지우기 스크린샷 FPS를 보려면 HUD를 켜야 합니다 속도 @@ -2618,7 +2567,6 @@ 도전 과제를 사용할 수 없습니다. 세션 로그를 확인하세요 도전 과제 잠금 해제됨 붙여넣기 - 방향패드 왼쪽 스틱 오른쪽 스틱 메뉴 @@ -2872,6 +2820,8 @@ 위 아래 가운데(기본) + 왼쪽 + 오른쪽 저장 공간 저장 공간 계산 중… 처음 계산할 때는 몇 초 걸립니다. @@ -2978,6 +2928,7 @@ %1$s만 남았습니다 Steam은 업데이트와 셰이더 캐시에 공간이 필요합니다. 10 GB 미만이면 게임 업데이트가 중간에 실패할 수 있습니다. %1$s 안전하게 확보 가능. + 다시 경고하지 않기 공간 확보 그래도 플레이 @@ -3653,4 +3604,17 @@ 듣는 중… 이 기기에서는 음성 입력을 사용할 수 없습니다. 음성 입력을 하려면 마이크 접근 권한이 필요합니다. + 애드온 다운로드… + 이름 + 다운로드 많은 순 + 모든 카테고리 + 애드온을 찾을 수 없음 + %1$s에 연결할 수 없습니다 + 이 게임 버전용 파일이 없습니다 + 위치 초기화 + 라벨 표시 + 이 레이아웃을 가져올 수 없습니다 + + 진홍빛 눈 + diff --git a/app/src/main/res/values-ms/strings.xml b/app/src/main/res/values-ms/strings.xml index bbe675e0a6..41de91e145 100644 --- a/app/src/main/res/values-ms/strings.xml +++ b/app/src/main/res/values-ms/strings.xml @@ -261,24 +261,8 @@ Sentuhan Steam (beta) Kawalan sentuh Steam bermula pada sesi Steam seterusnya Susun atur sentuh Steam - Kekunci skrin - Tambah kekunci - Teks - Saiz - Kelegapan - Kekunci - Pencetus - Togol Eksport - Kapsul - Bulat - Bulatan - Segi empat - Kekunci Imej - Kosongkan - Susun atur - Simpan Gerakkan, ubah saiz atau sembunyikan kawalan sentuh permainan ini; disimpan ke Steam Pengawal Cara Steam mengesan pengawal anda. Berkuat kuasa pada sesi seterusnya. @@ -553,6 +537,7 @@ Tiada DroidDeck-Proton yang menggunakannya Kemas kini Auto menunggu sambungan. Tidak dapat menyemak binaan baharu. + Tidak dapat memasang %1$s. Binaan baharu menunggu sambungan tanpa meter, penjimat bateri dimatikan dan peranti yang sejuk. %1$s dikemas kini ke %2$s. %1$s %2$s menutup permainan dalam beberapa saat dua kali, jadi Auto kembali ke %3$s. @@ -754,7 +739,6 @@ Automatik Hidup Mati - Kursor Papan kekunci PC Android Skrin kedua @@ -1093,43 +1077,25 @@ 3 · ADB komputer (pilihan akhir) Hanya jika Debugging Wayarles tidak tersedia. 3 · ADB komputer (pilihan akhir) - Bila kawalan sentuh muncul dalam sesi Warna - Rona butang pada skrin Tersuai Kelegapan %1$d%% Saiz butang - 100% ialah saiz standard - Klik stik - Ketik dua kali dan tahan stik untuk L3 atau R3 Stik adaptif Getaran permainan Zon mati kayu bedik Butang Steam Butang QAM Butang papan kekunci - Tekan Select untuk membuka menu Steam - Sebagai ganti View/Select. Joy-Con sebagai pengawal VR Semasa SteamVR berjalan, setiap Joy-Con menjadi tangan. Halakan ke hadapan dan tahan Home atau Capture untuk memusatkannya semula. Animasi - Stik muncul di tempat anda sentuh dan tersembunyi apabila dilepaskan + Beri amaran apabila storan rendah + Tanya sebelum memulakan Steam apabila ruang kosong kurang daripada 10 GB. Susun atur - Kedudukan tersuai disimpan - Disesuaikan dengan skrin ini Sunting Tetapkan semula - Pemetaan butang - Setiap butang menghantar inputnya sendiri - %1$d daripada %2$d butang dipetakan semula - Konfigurasikan - Tetapkan semula pengawal - Memulihkan semua tetapan pengawal dan susun atur - Apa yang setiap butang pada skrin hantar kepada permainan. Tersembunyi mengalih keluar butang itu. - Butang pada skrin - Lalai - Tetapkan semula pemetaan Apl Android %1$d apl Tiada apl Android boleh dilancarkan dijumpai. @@ -1208,7 +1174,6 @@ Belum ada pemacu disenaraikan. Segarkan untuk mencarinya. Sembunyikan pemacu untuk GPU lain Papar pemacu untuk GPU lain - Lanjutan Keserasian UBWC Cuba Hidup atau Mati jika Steam atau Android memaparkan jalur atau kelipan. Berkuat kuasa pada kedua-dua pemacu selepas DroidDeck dimulakan semula. Mulakan semula DroidDeck untuk menggunakannya. @@ -1248,16 +1213,8 @@ Gamepad Stik Trackpad - Tukar antara stik dan trackpad - Lagi kawalan Deck - PANEL Tutup kawalan skrin kedua Buka menu Steam - Kiri · atas - Kiri · bawah - Kanan · atas - Kanan · bawah - Klik Trackpad kanan Trackpad kiri Trackpad @@ -1754,6 +1711,7 @@ Ambar Merah Merah jambu + Kelabu Butang A Butang B Butang X @@ -1771,9 +1729,7 @@ D-pad kiri D-pad kanan Tersembunyi - Seret untuk alih Lekat - Simpan Tiada Papan kekunci + trackpad Terminal @@ -1786,13 +1742,6 @@ Memuat turun cache shader Dijeda Menunggu untuk bermula - Pemberitahuan - Skrin bawah DroidDeck - Paparkan mesej dan pemberitahuan lain di sini semasa anda bermain. Ia hanya kekal pada skrin ini dan tidak disimpan. Android bertanya sekali sahaja: halaman akses pemberitahuannya dibuka pada skrin ini; hidupkan DroidDeck di sana dan halaman ini akan kembali dengan sendirinya. - Benarkan akses pemberitahuan - Tiada pemberitahuan. - %1$d pemberitahuan - Kosongkan Tangkapan skrin FPS memerlukan HUD dihidupkan Kelajuan @@ -2618,7 +2567,6 @@ Pencapaian tidak tersedia; semak log sesi Pencapaian dibuka kunci Tampal - Pad arah Kayu bedik kiri Kayu bedik kanan Menu @@ -2872,6 +2820,8 @@ Atas Bawah Tengah (lalai) + Kiri + Kanan Storan Mengukur storan… Mengambil beberapa saat pada kali pertama. @@ -2978,6 +2928,7 @@ Hanya %1$s kosong Steam memerlukan ruang untuk kemas kini dan cache shader. Bawah 10 GB, kemas kini permainan boleh gagal di tengah jalan. %1$s boleh dikosongkan dengan selamat. + Jangan beri amaran lagi Kosongkan ruang Main juga @@ -3653,4 +3604,17 @@ Mendengar… Taip dengan suara tidak tersedia pada peranti ini. Akses mikrofon diperlukan untuk menaip dengan suara. + Muat turun addon… + Nama + Paling banyak dimuat turun + Semua kategori + Tiada addon ditemui + Tidak dapat mencapai %1$s + Tiada fail untuk versi permainan ini + Tetapkan semula kedudukan + Tunjukkan label + Susun atur ini tidak dapat diimport + + Salji Merah Skarlet + diff --git a/app/src/main/res/values-nb/strings.xml b/app/src/main/res/values-nb/strings.xml index 089a200d1f..61ad3ff2ee 100644 --- a/app/src/main/res/values-nb/strings.xml +++ b/app/src/main/res/values-nb/strings.xml @@ -261,24 +261,8 @@ Steam-berøring (beta) Steams berøringskontroller starter med neste Steam-sesjon Steam-berøringslayout - Skjermtaster - Legg til tast - Tekst - Størrelse - Opasitet - Tast - Utløser - Veksle Eksporter - Kapsel - Avrundet - Sirkel - Rektangel - Tast Bilde - Fjern - Oppsett - Lagre Flytt, endre størrelse på eller skjul dette spillets berøringskontroller; lagres til Steam Kontroller Hvordan Steam ser kontrolleren din. Gjelder fra neste sesjon. @@ -555,6 +539,7 @@ Ingen DroidDeck-Proton bruker dette Auto-oppdateringer venter på en tilkobling. Kunne ikke se etter nye bygg. + Kunne ikke installere %1$s. Nye bygg venter på en ubegrenset tilkobling, avslått batterisparing og en kjølig enhet. %1$s oppdatert til %2$s. %1$s %2$s lukket et spill innen sekunder to ganger, så Auto gikk tilbake til %3$s. @@ -756,7 +741,6 @@ Automatisk På Av - Peker PC-tastatur Android Andre skjerm @@ -1099,43 +1083,25 @@ 3 · ADB fra datamaskin (siste utvei) Bare hvis trådløs feilsøking ikke er tilgjengelig. 3 · ADB fra datamaskin (siste utvei) - Når skjermkontrollene vises i en sesjon Farge - Fargetone på skjermknappene Egendefinert Opasitet %1$d%% Knappestørrelse - 100% er standardstørrelsen - Stikke-klikk - Dobbelttrykk og hold en stikke for L3 eller R3 Adaptive stikker Spillvibrasjon Dødsone for styrespak Steam-knapp QAM-knapp Tastatur-knapp - Trykk på Select for å åpne Steam-menyen - I stedet for View/Select. Joy-Con som VR-kontrollere Mens SteamVR kjører, blir hver Joy-Con en hånd. Pek den fremover og hold inne Home eller Capture for å sentrere den på nytt. Animasjoner - Stikker vises der du trykker og skjules når du slipper + Advar når lagringsplassen er lav + Spør før Steam startes når ledig plass er under 10 GB. Oppsett - Egendefinerte posisjoner lagret - Tilpasset denne skjermen Rediger Nullstill - Knappetilordning - Hver knapp sender sin egen inndata - %1$d av %2$d knapper omtilordnet - Konfigurer - Nullstill kontroller - Gjenoppretter alle kontrollerinnstillinger og oppsettet - Hva hver skjermknapp sender til spillet. Skjult fjerner knappen. - Skjermknapper - Standardverdier - Nullstill tilordning Android-apper %1$d apper Fant ingen startbare Android-apper. @@ -1215,7 +1181,6 @@ Ingen drivere listet ennå. Oppdater for å se etter dem. Skjul drivere for andre GPU-er Vis drivere for andre GPU-er - Avansert UBWC-kompatibilitet Prøv På eller Av hvis Steam eller Android viser striper eller flimring. Gjelder begge driverne etter at DroidDeck startes på nytt. Start DroidDeck på nytt for å bruke endringen. @@ -1255,16 +1220,8 @@ Kontroller Stikke Styreflate - Bytt mellom stikke og styreflate - Flere Deck-kontroller - PANELER Lukk kontroller for andre skjerm Åpne Steam-menyen - Venstre · øvre - Venstre · nedre - Høyre · øvre - Høyre · nedre - Klikk Høyre styreflate Venstre styreflate Styreflate @@ -1778,6 +1735,7 @@ Amber Rød Rosa + Grå A-knapp B-knapp X-knapp @@ -1795,9 +1753,7 @@ D-pad venstre D-pad høyre Skjult - Dra for å flytte Fest - Lagre Ingen Tastatur + styreflate Terminal @@ -1810,13 +1766,6 @@ Laster ned shaderbuffer Pauset Venter på å starte - Varslinger - DroidDeck nedre skjerm - Vis meldinger og andre varslinger her mens du spiller. De vises bare på denne skjermen og lagres ikke. Android spør én gang: siden for varseltilgang åpnes på denne skjermen; slå på DroidDeck der, så kommer denne siden tilbake av seg selv. - Tillat varseltilgang - Ingen varslinger. - %1$d varslinger - Fjern Skjermbilder FPS krever at HUD-en er på Fart @@ -2645,7 +2594,6 @@ Prestasjoner ikke tilgjengelig; sjekk sesjonsloggene Prestasjon låst opp Lim inn - Styrekors Venstre stikke Høyre stikke Meny @@ -2901,6 +2849,8 @@ Øverst Nederst Midten (standard) + Venstre + Høyre Lagring Måler lagring… Første gang tar noen sekunder. @@ -3007,6 +2957,7 @@ Bare %1$s ledig Steam trenger plass til oppdateringer og shadercache. Under 10 GB kan spilloppdateringer mislykkes halvveis. %1$s kan frigjøres trygt. + Ikke advar meg igjen Frigjør plass Spill likevel @@ -3683,4 +3634,17 @@ Lytter… Taleinntasting er ikke tilgjengelig på denne enheten. Mikrofontilgang trengs for taleinntasting. + Last ned tillegg… + Navn + Mest nedlastet + Alle kategorier + Fant ingen tillegg + Fikk ikke kontakt med %1$s + Ingen fil for denne spillversjonen + Tilbakestill plasseringer + Vis etikett + Kunne ikke importere dette oppsettet + + Skarlagenssnø + diff --git a/app/src/main/res/values-nl/strings.xml b/app/src/main/res/values-nl/strings.xml index e97cc49dd1..bbc43ed4ee 100644 --- a/app/src/main/res/values-nl/strings.xml +++ b/app/src/main/res/values-nl/strings.xml @@ -261,24 +261,8 @@ Steam-aanraakbediening (bèta) De aanraakbediening van Steam start bij de volgende Steam-sessie Steam-aanraakindeling - Schermtoetsen - Toets toevoegen - Tekst - Grootte - Transparantie - Toets - Trigger - Schakelen Exporteren - Capsule - Afgerond - Cirkel - Rechthoek - Toets Afbeelding - Wissen - Indeling - Opslaan Verplaats, schaal of verberg de aanraakbediening van deze game; opgeslagen in Steam Controller Hoe Steam je controller ziet. Geldt vanaf de volgende sessie. @@ -555,6 +539,7 @@ Geen DroidDeck-Proton gebruikt dit Auto-updates wachten op een verbinding. Kon niet op nieuwe builds controleren. + Kon %1$s niet installeren. Nieuwe builds wachten op een onbeperkte verbinding, batterijbesparing uit en een koel apparaat. %1$s bijgewerkt naar %2$s. %1$s %2$s sloot een game twee keer binnen enkele seconden, dus Auto ging terug naar %3$s. @@ -756,7 +741,6 @@ Automatisch Aan Uit - Cursor Pc-toetsenbord Android Tweede scherm @@ -1099,43 +1083,25 @@ 3 · Computer-ADB (laatste redmiddel) Alleen als Draadloos debuggen niet beschikbaar is. 3 · Computer-ADB (laatste redmiddel) - Wanneer de aanraakbediening in een sessie verschijnt Kleur - Tint van de knoppen op het scherm Aangepast Transparantie %1$d%% Knopgrootte - 100% is de standaardgrootte - Stickklik - Dubbeltik en houd een stick ingedrukt voor L3 of R3 Adaptieve sticks Trillen in games Dode zone van stick Steam-knop QAM-knop Toetsenbordknop - Druk op Select om het Steam-menu te openen - In plaats van View/Select. Joy-Cons als VR-controllers Zolang SteamVR draait, wordt elke Joy-Con een hand. Richt hem naar voren en houd Home of Capture ingedrukt om hem opnieuw te centreren. Animaties - Sticks verschijnen waar je aanraakt en verdwijnen als je loslaat + Waarschuwen bij weinig opslagruimte + Vragen voordat Steam start als er minder dan 10 GB vrij is. Indeling - Aangepaste posities opgeslagen - Aangepast aan dit scherm Bewerken Resetten - Knoppenindeling - Elke knop stuurt zijn eigen invoer - %1$d van %2$d knoppen opnieuw toegewezen - Configureren - Controller resetten - Herstelt alle controllerinstellingen en de indeling - Wat elke schermknop naar de game stuurt. Verborgen verwijdert de knop. - Schermknoppen - Standaardinstellingen - Indeling resetten Android-apps %1$d apps Geen startbare Android-apps gevonden. @@ -1215,7 +1181,6 @@ Nog geen drivers vermeld. Vernieuw om ernaar te zoeken. Drivers voor andere GPU’s verbergen Drivers voor andere GPU’s tonen - Geavanceerd UBWC-compatibiliteit Probeer Aan of Uit als Steam of Android strepen of flikkering toont. Geldt voor beide drivers na het herstarten van DroidDeck. Start DroidDeck opnieuw om dit toe te passen. @@ -1255,16 +1220,8 @@ Gamepad Stick Trackpad - Wisselen tussen stick en trackpad - Meer Deck-bediening - PANELEN Bediening op tweede scherm sluiten Steam-menu openen - Links · boven - Links · onder - Rechts · boven - Rechts · onder - Klik Rechtertrackpad Linkertrackpad Trackpad @@ -1778,6 +1735,7 @@ Amber Rood Roze + Grijs A-knop B-knop X-knop @@ -1795,9 +1753,7 @@ D-pad links D-pad rechts Verborgen - Sleep om te verplaatsen Uitlijnen - Opslaan Geen Toetsenbord + trackpad Terminal @@ -1810,13 +1766,6 @@ Shadercache downloaden Gepauzeerd Wacht om te starten - Meldingen - DroidDeck onderste scherm - Toon berichten en andere meldingen hier terwijl je speelt. Ze blijven alleen op dit scherm en worden niet opgeslagen. Android vraagt dit één keer: de pagina voor meldingstoegang opent op dit scherm; zet DroidDeck daar aan en deze pagina komt vanzelf terug. - Meldingstoegang toestaan - Geen meldingen. - %1$d meldingen - Wissen Screenshots Voor FPS moet de HUD aan staan Snelheid @@ -2645,7 +2594,6 @@ Prestaties niet beschikbaar; controleer sessielogs Prestatie ontgrendeld Plakken - Richtingstoetsen Linkerstick Rechterstick Menu @@ -2901,6 +2849,8 @@ Boven Onder Midden (standaard) + Links + Rechts Opslag Opslag meten… De eerste keer duurt een paar seconden. @@ -3007,6 +2957,7 @@ Slechts %1$s vrij Steam heeft ruimte nodig voor updates en shadercache. Onder 10 GB kunnen game-updates halverwege mislukken. %1$s is veilig vrij te maken. + Niet meer waarschuwen Ruimte vrijmaken Toch spelen @@ -3683,4 +3634,17 @@ Luisteren… Spraakinvoer is niet beschikbaar op dit apparaat. Voor spraakinvoer is toegang tot de microfoon nodig. + Add-ons downloaden… + Naam + Meest gedownload + Alle categorieën + Geen add-ons gevonden + Kan %1$s niet bereiken + Geen bestand voor deze gameversie + Posities herstellen + Label tonen + Deze indeling kan niet worden geïmporteerd + + Scharlakenrode sneeuw + diff --git a/app/src/main/res/values-pl/strings.xml b/app/src/main/res/values-pl/strings.xml index eb3214faab..476dcc18bd 100644 --- a/app/src/main/res/values-pl/strings.xml +++ b/app/src/main/res/values-pl/strings.xml @@ -261,24 +261,8 @@ Dotyk Steam (beta) Sterowanie dotykowe Steam zacznie działać od następnej sesji Steam Układ dotykowy Steam - Klawisze ekranu - Dodaj klawisz - Tekst - Rozmiar - Przezroczystość - Klawisz - Spust - Przełącznik Eksportuj - Kapsułka - Zaokrąglony - Koło - Prostokąt - Klawisz Obraz - Wyczyść - Układ - Zapisz Przesuwaj, zmieniaj rozmiar lub ukrywaj sterowanie dotykowe tej gry; zapisywane w Steam Kontroler Jak Steam widzi Twój kontroler. Działa od następnej sesji. @@ -559,6 +543,7 @@ Żaden DroidDeck-Proton tego nie używa Aktualizacje Auto czekają na połączenie. Nie udało się sprawdzić nowych wersji. + Nie udało się zainstalować %1$s. Nowe wersje czekają na połączenie bez limitu, wyłączone oszczędzanie baterii i chłodne urządzenie. %1$s zaktualizowano do %2$s. %1$s %2$s dwa razy zamknął grę po kilku sekundach, więc Auto wróciło do %3$s. @@ -760,7 +745,6 @@ Automatycznie Włączone Wyłączone - Kursor Klawiatura PC Android Drugi ekran @@ -1111,43 +1095,25 @@ 3 · Komputer z ADB (ostatnia opcja) Tylko jeśli debugowanie bezprzewodowe jest niedostępne. 3 · Komputer z ADB (ostatnia opcja) - Gdy w sesji pojawiają się sterowanie dotykowe Kolor - Odcień przycisków na ekranie Własny Przezroczystość %1$d%% Rozmiar przycisków - 100% to rozmiar standardowy - Kliknięcie gałki - Dotknij dwukrotnie i przytrzymaj gałkę, aby uzyskać L3 lub R3 Adaptacyjne gałki Wibracje gry Martwa strefa drążka Przycisk Steam Przycisk QAM Przycisk klawiatury - Naciśnij Select, aby otworzyć menu Steam - Zamiast View/Select. Joy-Cony jako kontrolery VR Gdy działa SteamVR, każdy Joy-Con staje się dłonią. Skieruj go przed siebie i przytrzymaj Home lub Capture, aby go wyśrodkować. Animacje - Gałki pojawiają się tam, gdzie dotykasz, i chowają się po puszczeniu + Ostrzegaj o małej ilości miejsca + Pytaj przed uruchomieniem Steam, gdy wolne miejsce wynosi mniej niż 10 GB. Układ - Zapisano własne pozycje - Dopasowany do tego ekranu Edytuj Resetuj - Mapowanie przycisków - Każdy przycisk wysyła własny sygnał - Zmapowano %1$d z %2$d przycisków - Skonfiguruj - Resetuj kontroler - Przywraca wszystkie ustawienia kontrolera i układ - Co wysyła do gry każdy przycisk na ekranie. Ukryty usuwa przycisk. - Przyciski na ekranie - Domyślne - Resetuj mapowanie Aplikacje Android %1$d aplikacji Nie znaleziono żadnych aplikacji Android do uruchomienia. @@ -1229,7 +1195,6 @@ Brak sterowników na liście. Odśwież, aby ich poszukać. Ukryj sterowniki innych GPU Pokaż sterowniki innych GPU - Zaawansowane Zgodność UBWC Wypróbuj Włączone lub Wyłączone, jeśli Steam lub Android pokazuje pasy lub migotanie. Dotyczy obu sterowników po ponownym uruchomieniu DroidDeck. Uruchom ponownie DroidDeck, aby zastosować. @@ -1269,16 +1234,8 @@ Gamepad Gałka Trackpad - Przełącz między gałką a trackpadem - Więcej sterowania Deck - PANELE Zamknij sterowanie na drugim ekranie Otwórz menu Steam - Lewy · górny - Lewy · dolny - Prawy · górny - Prawy · dolny - Kliknięcie Prawy trackpad Lewy trackpad Trackpad @@ -1826,6 +1783,7 @@ Bursztynowy Czerwony Różowy + Szary Przycisk A Przycisk B Przycisk X @@ -1843,9 +1801,7 @@ Krzyżak w lewo Krzyżak w prawo Ukryty - Przeciągnij, aby przenieść Przyciąganie - Zapisz Brak Klawiatura + trackpad Terminal @@ -1858,13 +1814,6 @@ Pobieranie pamięci podręcznej shaderów Wstrzymano Oczekuje na rozpoczęcie - Powiadomienia - Dolny ekran DroidDeck - Wyświetlaj tu wiadomości i inne powiadomienia podczas gry. Pozostają tylko na tym ekranie i nie są zapisywane. Android zapyta raz: na tym ekranie otworzy się jego strona dostępu do powiadomień; włącz tam DroidDeck, a ta strona wróci sama. - Zezwól na dostęp do powiadomień - Brak powiadomień. - Powiadomienia: %1$d - Wyczyść Zrzuty ekranu FPS wymaga włączonego HUD Prędkość @@ -2699,7 +2648,6 @@ Osiągnięcia niedostępne; sprawdź dzienniki sesji Osiągnięcie odblokowane Wklej - Krzyżak Lewy drążek Prawy drążek Menu @@ -2959,6 +2907,8 @@ Góra Dół Środek (domyślnie) + Lewo + Prawo Pamięć Mierzenie pamięci… Pierwszy raz trwa kilka sekund. @@ -3065,6 +3015,7 @@ Wolne tylko %1$s Steam potrzebuje miejsca na aktualizacje i pamięć podręczną shaderów. Poniżej 10 GB aktualizacje gier mogą się przerwać w połowie. %1$s można bezpiecznie zwolnić. + Nie ostrzegaj ponownie Zwolnij miejsce Mimo to zagraj @@ -3761,4 +3712,17 @@ Słucham… Pisanie głosowe nie jest dostępne na tym urządzeniu. Pisanie głosowe wymaga dostępu do mikrofonu. + Pobierz dodatki… + Nazwa + Najczęściej pobierane + Wszystkie kategorie + Nie znaleziono dodatków + Nie można połączyć się z %1$s + Brak pliku dla tej wersji gry + Resetuj położenie + Pokaż etykietę + Nie można zaimportować tego układu + + Szkarłatny śnieg + diff --git a/app/src/main/res/values-pt-rPT/strings.xml b/app/src/main/res/values-pt-rPT/strings.xml index f4556ee927..a2991862fd 100644 --- a/app/src/main/res/values-pt-rPT/strings.xml +++ b/app/src/main/res/values-pt-rPT/strings.xml @@ -261,24 +261,8 @@ Controlos táteis do Steam (beta) Os controlos táteis do Steam começam na próxima sessão do Steam Esquema tátil do Steam - Teclas no ecrã - Adicionar tecla - Texto - Tamanho - Opacidade - Tecla - Gatilho - Alternar Exportar - Cápsula - Arredondado - Círculo - Retângulo - Tecla Imagem - Limpar - Esquema - Guardar Mova, redimensione ou oculte os controlos táteis deste jogo; é guardado no Steam Comando Como o Steam vê o seu comando. Aplica-se na próxima sessão. @@ -557,6 +541,7 @@ Nenhum DroidDeck-Proton usa isto As atualizações Auto aguardam uma ligação. Não foi possível procurar novas versões. + Não foi possível instalar %1$s. As novas versões aguardam uma ligação não limitada, a poupança de bateria desligada e o dispositivo fresco. %1$s atualizado para %2$s. %1$s %2$s fechou um jogo em segundos duas vezes, por isso Auto voltou a %3$s. @@ -758,7 +743,6 @@ Automático Ativado Desativado - Cursor Teclado de PC Android Segundo ecrã @@ -1097,43 +1081,25 @@ 3 · ADB num computador (último recurso) Apenas se a Depuração sem fios não estiver disponível. 3 · ADB num computador (último recurso) - Quando os controlos táteis aparecem numa sessão Cor - Tonalidade dos botões no ecrã Personalizada Opacidade %1$d%% Tamanho dos botões - O tamanho padrão é 100% - Clique do analógico - Toque duas vezes num analógico e mantenha para L3 ou R3 Analógicos adaptáveis Vibração do jogo Zona morta do manípulo Botão Steam Botão QAM Botão do teclado - Premir Select para abrir o menu do Steam - Em vez de View/Select. Joy-Con como comandos de RV Enquanto o SteamVR está a correr, cada Joy-Con torna-se uma mão. Aponte-o para a frente e mantenha Home ou Capture premido para o recentrar. Animações - Os analógicos aparecem onde tocar e ocultam-se ao soltar + Avisar quando o armazenamento estiver baixo + Perguntar antes de iniciar o Steam quando o espaço livre for inferior a 10 GB. Esquema - Posições personalizadas guardadas - Ajustado a este ecrã Editar Repor - Mapeamento de botões - Cada botão envia a sua própria entrada - %1$d de %2$d botões remapeados - Configurar - Repor comando - Restaura todas as definições do comando e o esquema - O que cada botão no ecrã envia para o jogo. Oculto remove o botão. - Botões no ecrã - Valores padrão - Repor mapeamento Apps Android %1$d apps Não foram encontradas apps Android para iniciar. @@ -1214,7 +1180,6 @@ Ainda não há controladores listados. Atualize para os procurar. Ocultar controladores para outras GPUs Mostrar controladores para outras GPUs - Avançado Compatibilidade UBWC Experimente Ativado ou Desativado se o Steam ou o Android mostrarem riscas ou cintilação. Aplica-se aos dois controladores após reiniciar o DroidDeck. Reinicie o DroidDeck para aplicar. @@ -1254,16 +1219,8 @@ Comando Joystick Trackpad - Alternar entre joystick e trackpad - Mais controlos do Deck - PAINÉIS Fechar controlos do segundo ecrã Abrir menu Steam - Esquerda · superior - Esquerda · inferior - Direita · superior - Direita · inferior - Clique Trackpad direito Trackpad esquerdo Trackpad @@ -1794,6 +1751,7 @@ Âmbar Vermelho Rosa + Cinzento Botão A Botão B Botão X @@ -1811,9 +1769,7 @@ Direcional para a esquerda Direcional para a direita Oculto - Arraste para mover Encaixar - Guardar Nenhum Teclado + trackpad Terminal @@ -1826,13 +1782,6 @@ A transferir a cache de shaders Em pausa A aguardar início - Notificações - Ecrã inferior do DroidDeck - Mostre aqui mensagens e outras notificações enquanto joga. Ficam apenas neste ecrã e não são guardadas. O Android pergunta uma vez: a página de acesso às notificações abre neste ecrã; ative lá o DroidDeck e esta página volta automaticamente. - Permitir acesso às notificações - Sem notificações. - %1$d notificações - Limpar Capturas de ecrã O FPS requer o HUD ativado Velocidade @@ -2661,7 +2610,6 @@ Conquistas indisponíveis; verifica os registos da sessão Conquista desbloqueada Colar - Direcional Stick esquerdo Stick direito Menu @@ -2928,6 +2876,8 @@ Espaço livre desconhecido Posição da imagem Topo + Esquerda + Direita Fundo Centro (predefinido) Armazenamento @@ -3036,6 +2986,7 @@ Apenas %1$s livres O Steam precisa de espaço para atualizações e cache de shaders. Abaixo de 10 GB, as atualizações dos jogos podem falhar a meio. %1$s podem ser libertados sem risco. + Não voltar a avisar Libertar espaço Jogar mesmo assim @@ -3722,4 +3673,17 @@ A ouvir… A escrita por voz não está disponível neste dispositivo. É necessário acesso ao microfone para a escrita por voz. + Transferir addons… + Nome + Mais transferidos + Todas as categorias + Nenhum addon encontrado + Não foi possível contactar %1$s + Não há ficheiro para esta versão do jogo + Repor posições + Mostrar etiqueta + Não foi possível importar este esquema + + Neve Escarlate + diff --git a/app/src/main/res/values-pt/strings.xml b/app/src/main/res/values-pt/strings.xml index 0cd77536ce..55b9476d47 100644 --- a/app/src/main/res/values-pt/strings.xml +++ b/app/src/main/res/values-pt/strings.xml @@ -261,24 +261,8 @@ Toque da Steam (beta) Os controles de toque da Steam começam na próxima sessão da Steam Layout de toque da Steam - Teclas na tela - Adicionar tecla - Texto - Tamanho - Opacidade - Tecla - Gatilho - Alternar Exportar - Cápsula - Arredondado - Círculo - Retângulo - Tecla Imagem - Limpar - Layout - Salvar Mova, redimensione ou oculte os controles de toque deste jogo; salvo na Steam Controle Como a Steam vê seu controle. Vale a partir da próxima sessão. @@ -557,6 +541,7 @@ Nenhum DroidDeck-Proton usa isto As atualizações Auto aguardam uma conexão. Não foi possível procurar novas versões. + Não foi possível instalar %1$s. Novas versões aguardam uma conexão não limitada, a economia de bateria desligada e o aparelho frio. %1$s atualizado para %2$s. %1$s %2$s fechou um jogo em segundos duas vezes, então Auto voltou para %3$s. @@ -758,7 +743,6 @@ Automático Ativado Desativado - Cursor Teclado de PC Android Segunda tela @@ -1097,43 +1081,25 @@ 3 · ADB pelo computador (último recurso) Só se a Depuração por Wi-Fi não estiver disponível. 3 · ADB pelo computador (último recurso) - Quando os controles de toque aparecem em uma sessão Cor - Tom dos botões na tela Personalizada Opacidade %1$d%% Tamanho dos botões - 100% é o tamanho padrão - Clique do analógico - Toque duas vezes e segure um analógico para L3 ou R3 Analógicos adaptáveis Vibração do jogo Zona morta do analógico Botão Steam Botão QAM Botão do teclado - Pressione Select para abrir o menu Steam - Em vez de View/Select. Joy-Cons como controles de RV Enquanto o SteamVR está rodando, cada Joy-Con vira uma mão. Aponte-o para a frente e segure Home ou Capture para recentralizá-lo. Animações - Os analógicos aparecem onde você toca e somem ao soltar + Avisar quando o armazenamento estiver baixo + Perguntar antes de iniciar o Steam quando o espaço livre for inferior a 10 GB. Layout - Posições personalizadas salvas - Ajustado a esta tela Editar Redefinir - Mapeamento de botões - Cada botão envia o próprio comando - %1$d de %2$d botões remapeados - Configurar - Redefinir controle - Restaura todas as configurações do controle e o layout - O que cada botão na tela envia ao jogo. Oculto remove o botão. - Botões na tela - Padrões - Redefinir mapeamento Apps Android %1$d apps Nenhum app Android que possa ser aberto foi encontrado. @@ -1214,7 +1180,6 @@ Nenhum driver listado ainda. Atualize para procurá-los. Ocultar drivers de outras GPUs Mostrar drivers de outras GPUs - Avançado Compatibilidade com UBWC Experimente Ativado ou Desativado se a Steam ou o Android mostrarem listras ou cintilação. Vale para os dois drivers após reiniciar o DroidDeck. Reinicie o DroidDeck para aplicar. @@ -1254,16 +1219,8 @@ Controle Analógico Trackpad - Alternar entre analógico e trackpad - Mais controles do Deck - PAINÉIS Fechar controles da segunda tela Abrir o menu Steam - Esquerdo · superior - Esquerdo · inferior - Direito · superior - Direito · inferior - Clique Trackpad direito Trackpad esquerdo Trackpad @@ -1794,6 +1751,7 @@ Âmbar Vermelho Rosa + Cinza Botão A Botão B Botão X @@ -1811,9 +1769,7 @@ Direcional para a esquerda Direcional para a direita Oculto - Arraste para mover Encaixar - Salvar Nenhum Teclado + trackpad Terminal @@ -1826,13 +1782,6 @@ Baixando o cache de shaders Pausado Aguardando para começar - Notificações - Tela inferior do DroidDeck - Mostre aqui mensagens e outras notificações enquanto você joga. Elas ficam só nesta tela e não são salvas. O Android pergunta uma vez: a página de acesso a notificações dele abre nesta tela; ative o DroidDeck lá e esta página volta sozinha. - Permitir acesso a notificações - Nenhuma notificação. - %1$d notificações - Limpar Capturas de tela O FPS precisa do HUD ativado Velocidade @@ -2661,7 +2610,6 @@ Conquistas indisponíveis; veja os registros da sessão Conquista desbloqueada Colar - Direcional Analógico esquerdo Analógico direito Menu @@ -2928,6 +2876,8 @@ Espaço livre desconhecido Posição da imagem Topo + Esquerda + Direita Base Centro (padrão) Armazenamento @@ -3036,6 +2986,7 @@ Só %1$s livres A Steam precisa de espaço para atualizações e cache de shaders. Abaixo de 10 GB, atualizações de jogos podem falhar no meio. Dá para liberar %1$s sem risco. + Não avisar novamente Liberar espaço Jogar assim mesmo @@ -3722,4 +3673,17 @@ Ouvindo… A digitação por voz não está disponível neste dispositivo. É preciso acesso ao microfone para a digitação por voz. + Baixar addons… + Nome + Mais baixados + Todas as categorias + Nenhum addon encontrado + Não foi possível acessar %1$s + Não há arquivo para esta versão do jogo + Redefinir posições + Mostrar rótulo + Não foi possível importar este layout + + Neve Escarlate + diff --git a/app/src/main/res/values-ro/strings.xml b/app/src/main/res/values-ro/strings.xml index 682df60c69..cea5ca1329 100644 --- a/app/src/main/res/values-ro/strings.xml +++ b/app/src/main/res/values-ro/strings.xml @@ -261,24 +261,8 @@ Atingere Steam (beta) Comenzile tactile Steam pornesc odată cu următoarea sesiune Steam Aranjament tactil Steam - Taste pe ecran - Adaugă tastă - Text - Dimensiune - Opacitate - Tastă - Trăgaci - Comutare Exportă - Capsulă - Rotunjit - Cerc - Dreptunghi - Tastă Imagine - Șterge - Aranjament - Salvează Mută, redimensionează sau ascunde comenzile tactile ale acestui joc; salvate în Steam Controller Cum vede Steam controllerul tău. Se aplică la sesiunea următoare. @@ -557,6 +541,7 @@ Niciun DroidDeck-Proton nu folosește asta Actualizările Auto așteaptă o conexiune. Nu s-au putut căuta versiuni noi. + Nu s-a putut instala %1$s. Versiunile noi așteaptă o conexiune fără limită de date, economisirea bateriei oprită și un dispozitiv răcit. %1$s actualizat la %2$s. %1$s %2$s a închis un joc în câteva secunde de două ori, așa că Auto a revenit la %3$s. @@ -758,7 +743,6 @@ Automat Pornit Oprit - Cursor Tastatură de PC Android Al doilea ecran @@ -1105,43 +1089,25 @@ 3 · ADB pe computer (ultima soluție) Doar dacă Depanarea wireless nu este disponibilă. 3 · ADB pe computer (ultima soluție) - Când apar comenzile tactile într-o sesiune Culoare - Tenta butoanelor de pe ecran Personalizat Opacitate %1$d%% Dimensiunea butoanelor - 100% este dimensiunea standard - Apăsarea manșei - Apasă de două ori și ține apăsată o manșă pentru L3 sau R3 Manșe adaptive Vibrații de joc Zonă moartă a manetei Butonul Steam Butonul QAM Butonul tastaturii - Apasă Select pentru a deschide meniul Steam - În loc de View/Select. Joy-Con ca controllere VR Cât timp rulează SteamVR, fiecare Joy-Con devine o mână. Îndreaptă-l înainte și ține apăsat Home sau Capture pentru a-l recentra. Animații - Manșele apar unde atingi ecranul și se ascund la eliberare + Avertizează când spațiul de stocare este redus + Întreabă înainte de pornirea Steam când spațiul liber este sub 10 GB. Aranjament - Poziții personalizate salvate - Adaptat pentru acest ecran Editează Resetează - Maparea butoanelor - Fiecare buton trimite propria intrare - %1$d din %2$d butoane remapate - Configurează - Resetează controllerul - Restaurează toate setările controllerului și aranjamentul - Ce trimite jocului fiecare buton de pe ecran. Ascuns elimină butonul. - Butoane pe ecran - Valori implicite - Resetează maparea Aplicații Android %1$d aplicații Nu s-a găsit nicio aplicație Android care poate fi lansată. @@ -1222,7 +1188,6 @@ Niciun driver listat încă. Reîmprospătează pentru a le căuta. Ascunde driverele pentru alte GPU-uri Arată driverele pentru alte GPU-uri - Avansat Compatibilitate UBWC Încearcă Activat sau Dezactivat dacă Steam sau Android afișează dungi ori pâlpâiri. Se aplică ambelor drivere după repornirea DroidDeck. Repornește DroidDeck pentru a aplica. @@ -1262,16 +1227,8 @@ Gamepad Manșă Trackpad - Comută între manșă și trackpad - Mai multe comenzi Deck - PANOURI Închide comenzile celui de-al doilea ecran Deschide meniul Steam - Stânga · sus - Stânga · jos - Dreapta · sus - Dreapta · jos - Clic Trackpad-ul drept Trackpad-ul stâng Trackpad @@ -1802,6 +1759,7 @@ Chihlimbariu Roșu Roz + Gri Butonul A Butonul B Butonul X @@ -1819,9 +1777,7 @@ D-pad stânga D-pad dreapta Ascuns - Trage pentru a mișca Aliniere - Salvează Niciunul Tastatură + trackpad Terminal @@ -1834,13 +1790,6 @@ Se descarcă cache-ul de shadere Întrerupt Așteaptă să înceapă - Notificări - Ecranul de jos DroidDeck - Afișează aici mesajele și alte notificări în timp ce te joci. Rămân doar pe acest ecran și nu sunt salvate. Android întreabă o singură dată: pagina sa de acces la notificări se deschide pe acest ecran; activează DroidDeck acolo, iar această pagină revine singură. - Permite accesul la notificări - Nicio notificare. - Notificări: %1$d - Șterge Capturi de ecran FPS necesită HUD-ul activat Viteză @@ -2672,7 +2621,6 @@ Realizări indisponibile; verifică jurnalele sesiunii Realizare deblocată Inserează - Cruce directională Stick stânga Stick dreapta Meniu @@ -2930,6 +2878,8 @@ Sus Jos Centru (implicit) + Stânga + Dreapta Stocare Se măsoară stocarea… Prima dată durează câteva secunde. @@ -3036,6 +2986,7 @@ Doar %1$s liberi Steam are nevoie de spațiu pentru actualizări și cache-ul de shadere. Sub 10 GB, actualizările jocurilor pot eșua la mijloc. %1$s pot fi eliberați în siguranță. + Nu mă mai avertiza Eliberează spațiu Joacă oricum @@ -3722,4 +3673,17 @@ Se ascultă… Tastarea vocală nu este disponibilă pe acest dispozitiv. Pentru tastarea vocală este necesar accesul la microfon. + Descarcă suplimente… + Nume + Cele mai descărcate + Toate categoriile + Nu s-au găsit suplimente + Nu s-a putut accesa %1$s + Nu există fișier pentru această versiune a jocului + Resetează pozițiile + Afișează eticheta + Acest aranjament nu a putut fi importat + + Zăpadă stacojie + diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml index b4fb1880a1..3206ad09ae 100644 --- a/app/src/main/res/values-ru/strings.xml +++ b/app/src/main/res/values-ru/strings.xml @@ -261,24 +261,8 @@ Сенсорное управление Steam (бета) Сенсорное управление Steam включится со следующей сессии Steam Сенсорная раскладка Steam - Экранные клавиши - Добавить клавишу - Текст - Размер - Непрозрачность - Клавиша - Триггер - Переключение Экспорт - Капсула - Скругление - Круг - Прямоугольник - Клавиша Изображение - Очистить - Раскладка - Сохранить Перемещайте, меняйте размер или скрывайте сенсорные кнопки этой игры; сохраняется в Steam Контроллер Как Steam распознает ваш контроллер. Вступит в силу в следующей сессии. @@ -578,6 +562,7 @@ Ни один DroidDeck-Proton это не использует Обновления Auto ждут подключения. Не удалось проверить новые сборки. + Не удалось установить %1$s. Новые сборки ждут безлимитного подключения, выключенной экономии заряда и остывшего устройства. %1$s обновлён до %2$s. %1$s %2$s дважды закрыл игру за несколько секунд, поэтому Auto вернулся к %3$s. @@ -779,7 +764,6 @@ Автоматически Включено Выключено - Курсор Клавиатура ПК Android Второй экран @@ -1148,43 +1132,25 @@ 3 · ADB на ПК (крайняя мера) Используйте только если отладка по Wi-Fi недоступна. 3 · ADB на ПК (крайняя мера) - Когда показывать экранные элементы управления в сессии Цвет - Оттенок экранных кнопок Пользовательский Непрозрачность %1$d%% Размер кнопок - 100% — стандартный размер - Нажатие стика - Дважды нажмите и удерживайте стик для L3 или R3 Адаптивные стики Вибрация в играх Мёртвая зона стика Кнопка Steam Кнопка QAM Кнопка клавиатуры - Нажмите Select, чтобы открыть меню Steam - Вместо View/Select. Joy-Con как VR-контроллеры Пока работает SteamVR, каждый Joy-Con становится рукой. Направьте его вперёд и удерживайте Home или Capture, чтобы снова его отцентрировать. Анимации - Стики появляются в месте касания и исчезают после отпускания + Предупреждать о нехватке места + Спрашивать перед запуском Steam, если свободно меньше 10 ГБ. Раскладка - Сохранено свое расположение - Адаптировано под этот экран Изменить Сбросить - Назначение кнопок - Каждая кнопка отправляет свой ввод - Переназначено кнопок: %1$d из %2$d - Настроить - Сбросить контроллер - Восстанавливает все настройки контроллера и раскладку - Что каждая экранная кнопка отправляет в игру. Значение «Скрыто» убирает кнопку. - Экранные кнопки - По умолчанию - Сбросить назначение Приложения Android Приложений: %1$d Не найдено приложений Android для запуска. @@ -1266,7 +1232,6 @@ Драйверы пока не перечислены. Обновите, чтобы найти драйверы. Скрыть драйверы других GPU Показать драйверы других GPU - Дополнительно Совместимость UBWC Попробуйте «Вкл.» или «Выкл.», если в Steam или Android появляются полосы или мерцание. Применяется к обоим драйверам после перезапуска DroidDeck. Перезапустите DroidDeck, чтобы применить. @@ -1306,16 +1271,8 @@ Геймпад Стик Тачпад - Переключение между стиком и тачпадом - Другие элементы управления Deck - ПАНЕЛИ Закрыть управление вторым экраном Открыть меню Steam - Слева · сверху - Слева · снизу - Справа · сверху - Справа · снизу - Клик Правый тачпад Левый тачпад Тачпад @@ -1872,6 +1829,7 @@ Янтарный Красный Розовый + Серый Кнопка A Кнопка B Кнопка X @@ -1889,9 +1847,7 @@ Крестовина влево Крестовина вправо Скрыто - Перетащите для перемещения Привязка - Сохранить Ничего Клавиатура + тачпад Терминал @@ -1921,13 +1877,6 @@ Загрузка кэша шейдеров Приостановлено Ожидает начала - Уведомления - Нижний экран DroidDeck - Показывает сообщения и другие уведомления здесь, пока вы играете. Они отображаются только на этом экране и не сохраняются. Android спросит один раз: на этом экране откроется страница доступа к уведомлениям; включите там DroidDeck, и эта страница вернется сама. - Разрешить доступ к уведомлениям - Нет уведомлений. - Уведомлений: %1$d - Очистить Скриншоты Для FPS нужно включить HUD Скорость @@ -2779,7 +2728,6 @@ Достижения недоступны; проверьте журналы сессии Достижение открыто Вставить - Крестовина Левый стик Правый стик Меню @@ -3042,6 +2990,8 @@ Сверху Снизу По центру (по умолчанию) + Слева + Справа Хранилище Измерение хранилища… В первый раз занимает несколько секунд. @@ -3148,6 +3098,7 @@ Свободно только %1$s Steam нужно место для обновлений и кэша шейдеров. Ниже 10 ГБ обновления игр могут срываться на середине. %1$s можно освободить без риска. + Больше не предупреждать Освободить место Всё равно играть @@ -3844,4 +3795,17 @@ Слушаю… Голосовой ввод недоступен на этом устройстве. Для голосового ввода нужен доступ к микрофону. + Скачать аддоны… + Название + Самые скачиваемые + Все категории + Аддоны не найдены + Не удалось подключиться к %1$s + Нет файла для этой версии игры + Сбросить положение + Показывать подпись + Не удалось импортировать эту раскладку + + Алый снег + diff --git a/app/src/main/res/values-sv/strings.xml b/app/src/main/res/values-sv/strings.xml index f75a36cd1d..e8934ac815 100644 --- a/app/src/main/res/values-sv/strings.xml +++ b/app/src/main/res/values-sv/strings.xml @@ -261,24 +261,8 @@ Steam-pekstyrning (beta) Steams pekstyrning startar med nästa Steam-session Steams peklayout - Skärmtangenter - Lägg till tangent - Text - Storlek - Opacitet - Tangent - Avtryckare - Växla Exportera - Kapsel - Rundad - Cirkel - Rektangel - Tangent Bild - Rensa - Layout - Spara Flytta, ändra storlek på eller dölj spelets pekkontroller; sparas i Steam Handkontroll Hur Steam ser din handkontroll. Gäller från nästa session. @@ -555,6 +539,7 @@ Ingen DroidDeck-Proton använder detta Auto-uppdateringar väntar på en anslutning. Kunde inte söka efter nya byggen. + Kunde inte installera %1$s. Nya byggen väntar på en obegränsad anslutning, avstängt batterisparläge och en sval enhet. %1$s uppdaterat till %2$s. %1$s %2$s stängde ett spel inom sekunder två gånger, så Auto gick tillbaka till %3$s. @@ -756,7 +741,6 @@ Automatiskt På Av - Markör PC-tangentbord Android Andra skärmen @@ -1099,43 +1083,25 @@ 3 · Dator-ADB (sista utväg) Bara om Trådlös felsökning inte är tillgänglig. 3 · Dator-ADB (sista utväg) - När skärmkontrollerna visas i en session Färg - Nyans på skärmknapparna Anpassad Opacitet %1$d %% Knappstorlek - 100 % är standardstorleken - Spakklick - Dubbeltryck och håll en spak för L3 eller R3 Adaptiva spakar Spelvibration Styrspakens dödzon Steam-knappen QAM-knappen Tangentbordsknappen - Tryck på Select för att öppna Steam-menyn - I stället för View/Select. Joy-Con som VR-handkontroller Medan SteamVR körs blir varje Joy-Con en hand. Rikta den framåt och håll in Home eller Capture för att centrera den igen. Animationer - Spakarna visas där du trycker och döljs när du släpper + Varna när lagringsutrymmet är lågt + Fråga innan Steam startas när det lediga utrymmet är under 10 GB. Layout - Anpassade positioner sparade - Anpassad till den här skärmen Redigera Återställ - Knappmappning - Varje knapp skickar sin egen inmatning - %1$d av %2$d knappar ommappade - Konfigurera - Återställ handkontroll - Återställer alla handkontrollsinställningar och layouten - Vad varje skärmknapp skickar till spelet. Dold tar bort knappen. - Skärmknappar - Standardvärden - Återställ mappning Android-appar %1$d appar Inga startbara Android-appar hittades. @@ -1215,7 +1181,6 @@ Inga drivrutiner listade än. Uppdatera för att söka efter dem. Dölj drivrutiner för andra GPU:er Visa drivrutiner för andra GPU:er - Avancerat UBWC-kompatibilitet Prova På eller Av om Steam eller Android visar ränder eller flimmer. Gäller båda drivrutinerna efter omstart av DroidDeck. Starta om DroidDeck för att tillämpa. @@ -1255,16 +1220,8 @@ Handkontroll Styrspak Styrplatta - Växla mellan styrspak och styrplatta - Fler Deck-kontroller - PANELER Stäng kontrollerna på andra skärmen Öppna Steam-menyn - Vänster · övre - Vänster · nedre - Höger · övre - Höger · nedre - Klick Höger styrplatta Vänster styrplatta Styrplatta @@ -1778,6 +1735,7 @@ Bärnsten Röd Rosa + Grå A-knappen B-knappen X-knappen @@ -1795,9 +1753,7 @@ D-pad vänster D-pad höger Dold - Dra för att flytta Fäst - Spara Inget Tangentbord + styrplatta Terminal @@ -1810,13 +1766,6 @@ Laddar ned shadercache Pausad Väntar på att starta - Aviseringar - DroidDecks nedre skärm - Visa meddelanden och andra aviseringar här medan du spelar. De visas bara på den här skärmen och sparas inte. Android frågar en gång: sidan för aviseringsåtkomst öppnas på den här skärmen; slå på DroidDeck där så kommer den här sidan tillbaka av sig själv. - Tillåt aviseringsåtkomst - Inga aviseringar. - %1$d aviseringar - Rensa Skärmbilder FPS kräver att HUD:en är på Hastighet @@ -2645,7 +2594,6 @@ Prestationer inte tillgängliga; kontrollera sessionsloggarna Prestation upplåst Klistra in - Styrkors Vänster styrspak Höger styrspak Meny @@ -2901,6 +2849,8 @@ Överst Nederst Mitten (standard) + Vänster + Höger Lagring Mäter lagring… Första gången tar några sekunder. @@ -3007,6 +2957,7 @@ Bara %1$s ledigt Steam behöver utrymme för uppdateringar och shadercache. Under 10 GB kan speluppdateringar avbrytas halvvägs. %1$s kan frigöras säkert. + Varna mig inte igen Frigör utrymme Spela ändå @@ -3683,4 +3634,17 @@ Lyssnar… Röstinmatning är inte tillgänglig på den här enheten. Mikrofonåtkomst krävs för röstinmatning. + Ladda ner tillägg… + Namn + Mest nedladdade + Alla kategorier + Inga tillägg hittades + Kunde inte nå %1$s + Ingen fil för den här spelversionen + Återställ positioner + Visa etikett + Det gick inte att importera layouten + + Scharlakansröd snö + diff --git a/app/src/main/res/values-th/strings.xml b/app/src/main/res/values-th/strings.xml index dcb8bc92f9..2396faf7cf 100644 --- a/app/src/main/res/values-th/strings.xml +++ b/app/src/main/res/values-th/strings.xml @@ -261,24 +261,8 @@ การสัมผัสของ Steam (ทดลอง) ปุ่มสัมผัสของ Steam จะเริ่มใช้งานในเซสชัน Steam ถัดไป รูปแบบปุ่มสัมผัสของ Steam - ปุ่มบนหน้าจอ - เพิ่มปุ่ม - ข้อความ - ขนาด - ความโปร่งใส - ปุ่ม - ไก - สลับ ส่งออก - แคปซูล - มน - วงกลม - สี่เหลี่ยม - ปุ่ม รูปภาพ - ล้าง - การจัดวาง - บันทึก ย้าย ปรับขนาด หรือซ่อนปุ่มสัมผัสของเกมนี้ บันทึกไปยัง Steam คอนโทรลเลอร์ วิธีที่ Steam มองเห็นคอนโทรลเลอร์ของคุณ จะมีผลในเซสชันถัดไป @@ -553,6 +537,7 @@ ไม่มี DroidDeck-Proton ที่ใช้สิ่งนี้ การอัปเดต Auto กำลังรอการเชื่อมต่อ ตรวจหาบิลด์ใหม่ไม่ได้ + ติดตั้ง %1$s ไม่ได้ บิลด์ใหม่จะรอการเชื่อมต่อที่ไม่จำกัดปริมาณ ปิดโหมดประหยัดแบตเตอรี่ และอุปกรณ์ที่ไม่ร้อน อัปเดต %1$s เป็น %2$s แล้ว %1$s %2$s ทำให้เกมปิดภายในไม่กี่วินาทีสองครั้ง Auto จึงกลับไปใช้ %3$s @@ -754,7 +739,6 @@ อัตโนมัติ เปิด ปิด - เคอร์เซอร์ แป้นพิมพ์พีซี Android หน้าจอที่สอง @@ -1093,43 +1077,25 @@ 3 · ADB จากคอมพิวเตอร์ (ทางเลือกสุดท้าย) ใช้เฉพาะเมื่อ Wireless debugging ใช้งานไม่ได้ 3 · ADB จากคอมพิวเตอร์ (ทางเลือกสุดท้าย) - เมื่อปุ่มสัมผัสจะปรากฏในเซสชัน สี - สีของปุ่มบนหน้าจอ กำหนดเอง ความโปร่งใส %1$d%% ขนาดปุ่ม - 100% คือขนาดมาตรฐาน - การกดสติ๊ก - แตะสองครั้งแล้วกดสติ๊กค้างไว้สำหรับ L3 หรือ R3 สติ๊กแบบปรับอัตโนมัติ การสั่นของเกม เดดโซนของแอนะล็อกสติ๊ก ปุ่ม Steam ปุ่ม QAM ปุ่มแป้นพิมพ์ - กด Select เพื่อเปิดเมนู Steam - แทน View/Select ใช้ Joy-Con เป็นคอนโทรลเลอร์ VR ขณะที่ SteamVR ทำงาน Joy-Con แต่ละตัวจะกลายเป็นมือ ชี้ไปข้างหน้าแล้วกด Home หรือ Capture ค้างไว้เพื่อจัดให้อยู่ตรงกลางอีกครั้ง ภาพเคลื่อนไหว - สติ๊กจะปรากฏที่ตำแหน่งที่คุณแตะและซ่อนเมื่อปล่อย + เตือนเมื่อพื้นที่เก็บข้อมูลเหลือน้อย + ถามก่อนเริ่ม Steam เมื่อพื้นที่ว่างน้อยกว่า 10 GB การจัดวาง - บันทึกตำแหน่งกำหนดเองไว้แล้ว - ปรับให้พอดีกับหน้าจอนี้ แก้ไข รีเซ็ต - การกำหนดปุ่ม - ทุกปุ่มส่งอินพุตของตัวเอง - กำหนดใหม่ %1$d จาก %2$d ปุ่ม - ตั้งค่า - รีเซ็ตคอนโทรลเลอร์ - คืนค่าการตั้งค่าคอนโทรลเลอร์ทั้งหมดและการจัดวาง - สิ่งที่ปุ่มบนหน้าจอแต่ละปุ่มส่งไปยังเกม ซ่อนจะเอาปุ่มนั้นออก - ปุ่มบนหน้าจอ - ค่าเริ่มต้น - รีเซ็ตการกำหนดปุ่ม แอป Android %1$d แอป ไม่พบแอป Android ที่เปิดได้ @@ -1208,7 +1174,6 @@ ยังไม่มีไดรเวอร์ในรายการ รีเฟรชเพื่อค้นหา ซ่อนไดรเวอร์สำหรับ GPU อื่น แสดงไดรเวอร์สำหรับ GPU อื่น - ขั้นสูง ความเข้ากันได้ของ UBWC ลองเปิดหรือปิดหาก Steam หรือ Android แสดงเป็นแถบลายหรือกะพริบ มีผลกับไดรเวอร์ทั้งสองหลังรีสตาร์ท DroidDeck รีสตาร์ท DroidDeck เพื่อใช้การเปลี่ยนแปลง @@ -1248,16 +1213,8 @@ เกมแพด สติ๊ก แทร็กแพด - สลับระหว่างสติ๊กและแทร็กแพด - การควบคุม Deck เพิ่มเติม - แผง ปิดการควบคุมหน้าจอที่สอง เปิดเมนู Steam - ซ้าย · บน - ซ้าย · ล่าง - ขวา · บน - ขวา · ล่าง - คลิก แทร็กแพดขวา แทร็กแพดซ้าย แทร็กแพด @@ -1754,6 +1711,7 @@ อำพัน แดง ชมพู + เทา ปุ่ม A ปุ่ม B ปุ่ม X @@ -1771,9 +1729,7 @@ D-pad ซ้าย D-pad ขวา ซ่อน - ลากเพื่อย้าย จัดให้พอดี - บันทึก ไม่มี แป้นพิมพ์ + แทร็กแพด เทอร์มินัล @@ -1786,13 +1742,6 @@ กำลังดาวน์โหลดแคชเชเดอร์ หยุดชั่วคราว กำลังรอเริ่ม - การแจ้งเตือน - หน้าจอล่างของ DroidDeck - แสดงข้อความและการแจ้งเตือนอื่นๆ ที่นี่ขณะที่คุณเล่น การแจ้งเตือนจะแสดงบนหน้าจอนี้เท่านั้นและจะไม่ถูกบันทึก Android จะถามเพียงครั้งเดียว: หน้าการเข้าถึงการแจ้งเตือนจะเปิดขึ้นบนหน้าจอนี้ เปิด DroidDeck ที่นั่น แล้วหน้านี้จะกลับมาเอง - อนุญาตการเข้าถึงการแจ้งเตือน - ไม่มีการแจ้งเตือน - การแจ้งเตือน %1$d รายการ - ล้าง ภาพหน้าจอ ต้องเปิด HUD เพื่อแสดง FPS ความเร็ว @@ -2618,7 +2567,6 @@ ไม่พบความสำเร็จ ตรวจสอบบันทึกเซสชัน ปลดล็อกความสำเร็จแล้ว วาง - ปุ่มกากบาท อนาล็อกซ้าย อนาล็อกขวา เมนู @@ -2872,6 +2820,8 @@ บน ล่าง กลาง (ค่าเริ่มต้น) + ซ้าย + ขวา พื้นที่จัดเก็บ กำลังวัดพื้นที่… ครั้งแรกใช้เวลาสองสามวินาที @@ -2978,6 +2928,7 @@ เหลือเพียง %1$s Steam ต้องใช้พื้นที่สำหรับอัปเดตและแคชเชเดอร์ ถ้าต่ำกว่า 10 GB การอัปเดตเกมอาจล้มเหลวกลางทาง ล้าง %1$s ได้อย่างปลอดภัย + ไม่ต้องเตือนอีก เพิ่มพื้นที่ว่าง เล่นต่อ @@ -3653,4 +3604,17 @@ กำลังฟัง… อุปกรณ์นี้ไม่รองรับการพิมพ์ด้วยเสียง ต้องอนุญาตให้เข้าถึงไมโครโฟนเพื่อพิมพ์ด้วยเสียง + ดาวน์โหลดแอดออน… + ชื่อ + ดาวน์โหลดมากที่สุด + ทุกหมวดหมู่ + ไม่พบแอดออน + ไม่สามารถเชื่อมต่อ %1$s ได้ + ไม่มีไฟล์สำหรับเกมเวอร์ชันนี้ + รีเซ็ตตำแหน่ง + แสดงป้ายกำกับ + ไม่สามารถนำเข้าการจัดวางนี้ได้ + + หิมะสีแดงชาด + diff --git a/app/src/main/res/values-tr/strings.xml b/app/src/main/res/values-tr/strings.xml index cfcccbda26..7fa958f6a9 100644 --- a/app/src/main/res/values-tr/strings.xml +++ b/app/src/main/res/values-tr/strings.xml @@ -261,24 +261,8 @@ Steam dokunmatik (beta) Steam\'in dokunmatik kontrolleri sonraki Steam oturumunda başlar Steam dokunmatik düzeni - Ekran tuşları - Tuş ekle - Metin - Boyut - Opaklık - Tuş - Tetik - Aç/Kapa Dışa aktar - Kapsül - Yuvarlak - Daire - Dikdörtgen - Tuş Görsel - Temizle - Düzen - Kaydet Bu oyunun dokunmatik kontrollerini taşıyın, boyutlandırın veya gizleyin; Steam\'e kaydedilir Kumanda Steam\'in kumandanızı nasıl gördüğü. Sonraki oturumda uygulanır. @@ -555,6 +539,7 @@ Hiçbir DroidDeck-Proton bunu kullanmıyor Auto güncellemeleri bağlantı bekliyor. Yeni sürümler denetlenemedi. + %1$s yüklenemedi. Yeni sürümler sınırsız bir bağlantı, kapalı pil tasarrufu ve serin bir cihaz bekler. %1$s, %2$s sürümüne güncellendi. %1$s %2$s bir oyunu iki kez saniyeler içinde kapattı, bu yüzden Auto %3$s sürümüne döndü. @@ -756,7 +741,6 @@ Otomatik Açık Kapalı - İmleç PC klavyesi Android İkinci ekran @@ -1091,43 +1075,25 @@ 3 · Bilgisayardan ADB (son yöntem) Yalnızca Kablosuz hata ayıklama kullanılamıyorsa. 3 · Bilgisayardan ADB (son yöntem) - Dokunmatik kontrollerin bir oturumda ne zaman görüneceği Renk - Ekrandaki düğmelerin rengi Özel Opaklık %1$d%% Düğme boyutu - %100 standart boyuttur - Çubuk tıklaması - L3 veya R3 için bir çubuğa çift dokunup basılı tutun Uyarlanabilir çubuklar Oyun titreşimi Çubuk ölü bölgesi Steam düğmesi QAM düğmesi Klavye düğmesi - Steam menüsünü açmak için Select\'e basın - Görünüm/Select yerine. VR kumandası olarak Joy-Con\'lar SteamVR çalışırken her Joy-Con bir el olur. Öne doğrultun ve yeniden ortalamak için Home veya Capture düğmesini basılı tutun. Animasyonlar - Çubuklar dokunduğunuz yerde görünür, bıraktığınızda gizlenir + Depolama azaldığında uyar + Boş alan 10 GB\'ın altındayken Steam\'i başlatmadan önce sor. Düzen - Özel konumlar kaydedildi - Bu ekrana uyarlandı Düzenle Sıfırla - Düğme eşlemesi - Her düğme kendi girdisini gönderir - %2$d düğmeden %1$d\'i yeniden eşlendi - Yapılandır - Kumandayı sıfırla - Tüm kumanda ayarlarını ve düzeni geri yükler - Ekrandaki her düğmenin oyuna gönderdiği girdi. Gizli, düğmeyi kaldırır. - Ekrandaki düğmeler - Varsayılanlar - Eşlemeyi sıfırla Android uygulamaları %1$d uygulama Başlatılabilir Android uygulaması bulunamadı. @@ -1207,7 +1173,6 @@ Henüz listelenen sürücü yok. Aramak için yenileyin. Diğer GPU\'lara ait sürücüleri gizle Diğer GPU\'lara ait sürücüleri göster - Gelişmiş UBWC uyumluluğu Steam veya Android\'de şeritler ya da titreme görülürse Açık veya Kapalı\'yı deneyin. DroidDeck yeniden başlatıldıktan sonra iki sürücüye de uygulanır. Uygulamak için DroidDeck\'i yeniden başlatın. @@ -1247,16 +1212,8 @@ Oyun kolu Çubuk Dokunmatik yüzey - Çubuk ile dokunmatik yüzey arasında geçiş yap - Diğer Deck kontrolleri - PANELLER İkinci ekran kontrollerini kapat Steam menüsünü aç - Sol · üst - Sol · alt - Sağ · üst - Sağ · alt - Tıkla Sağ dokunmatik yüzey Sol dokunmatik yüzey Dokunmatik yüzey @@ -1770,6 +1727,7 @@ Kehribar Kırmızı Pembe + Gri A düğmesi B düğmesi X düğmesi @@ -1787,9 +1745,7 @@ Yön tuşu sol Yön tuşu sağ Gizli - Taşımak için sürükleyin Hizala - Kaydet Hiçbiri Klavye + dokunmatik yüzey Terminal @@ -1802,13 +1758,6 @@ Gölgelendirici önbelleği indiriliyor Duraklatıldı Başlamayı bekliyor - Bildirimler - DroidDeck alt ekranı - Oynarken mesajları ve diğer bildirimleri burada görün. Yalnızca bu ekranda kalırlar ve kaydedilmezler. Android bunu bir kez sorar: bildirim erişimi sayfası bu ekranda açılır; orada DroidDeck\'i açın, bu sayfa kendiliğinden geri gelir. - Bildirim erişimine izin ver - Bildirim yok. - %1$d bildirim - Temizle Ekran görüntüleri FPS için HUD açık olmalı Hız @@ -2634,7 +2583,6 @@ Başarımlar kullanılamıyor; oturum günlüklerini kontrol et Başarımın kilidi açıldı Yapıştır - Yön tuşu Sol analog çubuk Sağ analog çubuk Menü @@ -2901,6 +2849,8 @@ Üst Alt Orta (varsayılan) + Sol + Sağ Depolama Depolama ölçülüyor… İlk seferde birkaç saniye sürer. @@ -3007,6 +2957,7 @@ Yalnızca %1$s boş Steam\'in güncellemeler ve gölgelendirici önbellekleri için yere ihtiyacı var. 10 GB\'ın altında oyun güncellemeleri yarıda başarısız olabilir. %1$s güvenle boşaltılabilir. + Bir daha uyarma Yer aç Yine de oyna @@ -3683,4 +3634,17 @@ Dinleniyor… Sesle yazma bu cihazda kullanılamıyor. Sesle yazma için mikrofon erişimi gerekiyor. + Eklenti indir… + Ad + En çok indirilen + Tüm kategoriler + Eklenti bulunamadı + %1$s adresine ulaşılamadı + Bu oyun sürümü için dosya yok + Konumları sıfırla + Etiketi göster + Bu düzen içe aktarılamadı + + Kızıl Kar + diff --git a/app/src/main/res/values-uk/strings.xml b/app/src/main/res/values-uk/strings.xml index 96f6ecb14b..f96bcee46c 100644 --- a/app/src/main/res/values-uk/strings.xml +++ b/app/src/main/res/values-uk/strings.xml @@ -261,24 +261,8 @@ Сенсорне керування Steam (бета) Сенсорне керування Steam увімкнеться з наступним сеансом Steam Розкладка сенсорного керування Steam - Екранні клавіші - Додати клавішу - Текст - Розмір - Непрозорість - Клавіша - Тригер - Перемикання Експорт - Капсула - Заокруглення - Коло - Прямокутник - Клавіша Зображення - Очистити - Розкладка - Зберегти Переміщуйте, змінюйте розмір або приховуйте сенсорні елементи керування цієї гри; зберігається в Steam Контролер Яким Steam бачить ваш контролер. Застосовується в наступному сеансі. @@ -559,6 +543,7 @@ Жоден DroidDeck-Proton цього не використовує Оновлення Auto чекають на з’єднання. Не вдалося перевірити нові збірки. + Не вдалося встановити %1$s. Нові збірки чекають на безлімітне з’єднання, вимкнену економію заряду та охолоджений пристрій. %1$s оновлено до %2$s. %1$s %2$s двічі закрив гру за кілька секунд, тому Auto повернувся до %3$s. @@ -760,7 +745,6 @@ Автоматично Увімкнено Вимкнено - Курсор Клавіатура ПК Android Другий екран @@ -1111,43 +1095,25 @@ 3 · ADB з комп’ютера (останній варіант) Лише якщо бездротове налагодження недоступне. 3 · ADB з комп’ютера (останній варіант) - Коли сенсорне керування з’являється в сеансі Колір - Відтінок екранних кнопок Власний Непрозорість %1$d%% Розмір кнопок - 100% — стандартний розмір - Натискання стика - Двічі торкніться і затримайте стик, щоб отримати L3 або R3 Адаптивні стики Вібрація гри Мертва зона стіка Кнопка Steam Кнопка QAM Кнопка клавіатури - Натисніть Select, щоб відкрити меню Steam - Замість View/Select. Joy-Con як VR-контролери Поки працює SteamVR, кожен Joy-Con стає рукою. Спрямуйте його вперед і утримуйте Home або Capture, щоб знову його відцентрувати. Анімації - Стики з’являються там, де ви торкаєтеся, і зникають після відпускання + Попереджати про брак місця + Питати перед запуском Steam, якщо вільно менше 10 ГБ. Розкладка - Збережено власне розташування - Підлаштовано під цей екран Змінити Скинути - Призначення кнопок - Кожна кнопка надсилає власне введення - Перепризначено %1$d з %2$d кнопок - Налаштувати - Скинути контролер - Відновлює всі налаштування контролера та розкладку - Що кожна екранна кнопка надсилає грі. «Приховано» прибирає кнопку. - Екранні кнопки - Стандартні - Скинути призначення Додатки Android %1$d додатків Не знайдено жодного додатка Android для запуску. @@ -1229,7 +1195,6 @@ Драйверів у списку ще немає. Оновіть, щоб їх знайти. Приховати драйвери для інших GPU Показати драйвери для інших GPU - Додатково Сумісність UBWC Спробуйте «Увімк.» або «Вимк.», якщо в Steam чи Android зʼявляються смуги або мерехтіння. Застосовується до обох драйверів після перезапуску DroidDeck. Перезапустіть DroidDeck, щоб застосувати. @@ -1269,16 +1234,8 @@ Геймпад Стік Трекпад - Перемкнути між стіком і трекпадом - Більше елементів керування Deck - ПАНЕЛІ Закрити керування на другому екрані Відкрити меню Steam - Ліва · верхня - Ліва · нижня - Права · верхня - Права · нижня - Натискання Правий трекпад Лівий трекпад Трекпад @@ -1826,6 +1783,7 @@ Бурштиновий Червоний Рожевий + Сірий Кнопка A Кнопка B Кнопка X @@ -1843,9 +1801,7 @@ Хрестовина вліво Хрестовина вправо Приховано - Перетягніть, щоб перемістити Прив’язка - Зберегти Нічого Клавіатура + трекпад Термінал @@ -1858,13 +1814,6 @@ Завантаження кешу шейдерів Призупинено Очікує початку - Сповіщення - Нижній екран DroidDeck - Показуйте тут повідомлення та інші сповіщення під час гри. Вони лишаються лише на цьому екрані й не зберігаються. Android запитає один раз: сторінка доступу до сповіщень відкриється на цьому екрані; увімкніть там DroidDeck, і ця сторінка повернеться сама. - Дозволити доступ до сповіщень - Немає сповіщень. - Сповіщень: %1$d - Очистити Знімки екрана Для FPS потрібно ввімкнути HUD Швидкість @@ -2699,7 +2648,6 @@ Досягнення недоступні; перевірте журнали сесії Досягнення відкрито Вставити - Хрестовина Лівий стик Правий стик Меню @@ -2959,6 +2907,8 @@ Угорі Унизу По центру (типово) + Ліворуч + Праворуч Сховище Вимірювання сховища… Уперше триває кілька секунд. @@ -3065,6 +3015,7 @@ Вільно лише %1$s Steam потрібне місце для оновлень і кешу шейдерів. Нижче 10 GB оновлення ігор можуть зірватися на півдорозі. %1$s можна безпечно звільнити. + Більше не попереджати Звільнити місце Грати все одно @@ -3761,4 +3712,17 @@ Слухаю… Голосове введення недоступне на цьому пристрої. Для голосового введення потрібен доступ до мікрофона. + Завантажити аддони… + Назва + Найбільше завантажень + Усі категорії + Аддонів не знайдено + Не вдалося під’єднатися до %1$s + Немає файлу для цієї версії гри + Скинути розташування + Показувати підпис + Не вдалося імпортувати цю розкладку + + Багряний сніг + diff --git a/app/src/main/res/values-vi/strings.xml b/app/src/main/res/values-vi/strings.xml index dc5525454a..1f0513786d 100644 --- a/app/src/main/res/values-vi/strings.xml +++ b/app/src/main/res/values-vi/strings.xml @@ -261,24 +261,8 @@ Chạm Steam (beta) Các nút chạm của Steam bắt đầu từ phiên Steam kế tiếp Bố cục chạm Steam - Phím trên màn hình - Thêm phím - Văn bản - Kích thước - Độ mờ - Phím - Cò - Bật tắt Xuất - Viên nang - Bo tròn - Tròn - Chữ nhật - Phím Hình ảnh - Xóa - Bố cục - Lưu Di chuyển, đổi kích thước hoặc ẩn các nút chạm của game này; được lưu vào Steam Tay cầm Cách Steam nhận dạng tay cầm của bạn. Áp dụng từ phiên sau. @@ -553,6 +537,7 @@ Không DroidDeck-Proton nào dùng thành phần này Cập nhật Auto đang chờ kết nối. Không thể kiểm tra bản dựng mới. + Không thể cài đặt %1$s. Bản dựng mới chờ kết nối không đo lưu lượng, tắt tiết kiệm pin và thiết bị đủ mát. Đã cập nhật %1$s lên %2$s. %1$s %2$s làm trò chơi đóng sau vài giây hai lần, nên Auto đã quay lại %3$s. @@ -754,7 +739,6 @@ Tự động Bật Tắt - Con trỏ Bàn phím PC Android Màn hình thứ hai @@ -1093,43 +1077,25 @@ 3 · ADB từ máy tính (phương án cuối) Chỉ khi Gỡ lỗi qua mạng không dây không khả dụng. 3 · ADB từ máy tính (phương án cuối) - Khi các nút điều khiển chạm xuất hiện trong một phiên Màu - Màu sắc của các nút trên màn hình Tùy chỉnh Độ mờ %1$d%% Kích thước nút - 100% là kích thước chuẩn - Nhấn cần - Chạm hai lần và giữ một cần để dùng L3 hoặc R3 Cần thích ứng Rung khi chơi game Vùng chết của cần điều khiển Nút Steam Nút QAM Nút bàn phím - Nhấn Select để mở menu Steam - Thay cho View/Select. Dùng Joy-Con làm tay cầm VR Khi SteamVR đang chạy, mỗi Joy-Con trở thành một bàn tay. Hướng nó về phía trước rồi giữ Home hoặc Capture để căn giữa lại. Hiệu ứng chuyển động - Cần xuất hiện ở nơi bạn chạm và ẩn khi thả ra + Cảnh báo khi dung lượng lưu trữ thấp + Hỏi trước khi khởi động Steam khi dung lượng trống dưới 10 GB. Bố cục - Vị trí tùy chỉnh đã lưu - Vừa khớp với màn hình này Sửa Đặt lại - Gán nút - Mỗi nút gửi tín hiệu riêng của nó - %1$d trong %2$d nút đã được gán lại - Cấu hình - Đặt lại tay cầm - Khôi phục mọi thiết lập tay cầm và bố cục - Mỗi nút trên màn hình gửi gì đến game. Ẩn sẽ xóa nút đó. - Nút trên màn hình - Mặc định - Đặt lại gán nút Ứng dụng Android %1$d ứng dụng Không tìm thấy ứng dụng Android nào có thể khởi chạy. @@ -1208,7 +1174,6 @@ Chưa có driver nào được liệt kê. Làm mới để tìm. Ẩn driver cho GPU khác Hiện driver cho GPU khác - Nâng cao Tương thích UBWC Thử Bật hoặc Tắt nếu Steam hoặc Android hiện sọc hay nhấp nháy. Áp dụng cho cả hai driver sau khi khởi động lại DroidDeck. Khởi động lại DroidDeck để áp dụng. @@ -1248,16 +1213,8 @@ Tay cầm Cần Trackpad - Chuyển giữa cần và trackpad - Thêm điều khiển Deck - BẢNG Đóng điều khiển màn hình phụ Mở menu Steam - Trái · trên - Trái · dưới - Phải · trên - Phải · dưới - Nhấn Trackpad phải Trackpad trái Trackpad @@ -1754,6 +1711,7 @@ Hổ phách Đỏ Hồng + Xám Nút A Nút B Nút X @@ -1771,9 +1729,7 @@ D-pad trái D-pad phải Ẩn - Kéo để di chuyển Hít vào lưới (snap) - Lưu Không có Bàn phím + trackpad Terminal @@ -1786,13 +1742,6 @@ Đang tải bộ nhớ đệm shader Đã tạm dừng Đang chờ bắt đầu - Thông báo - Màn hình dưới DroidDeck - Hiển thị tin nhắn và các thông báo khác ở đây khi bạn chơi. Chúng chỉ ở trên màn hình này và không được lưu. Android chỉ hỏi một lần: trang quyền truy cập thông báo sẽ mở trên màn hình này; hãy bật DroidDeck ở đó và trang này sẽ tự quay lại. - Cho phép truy cập thông báo - Không có thông báo. - %1$d thông báo - Xóa Ảnh chụp màn hình Cần bật HUD để xem FPS Tốc độ @@ -2618,7 +2567,6 @@ Không có thành tích; hãy kiểm tra nhật ký phiên Đã mở khóa thành tích Dán - Phím chữ thập Cần analog trái Cần analog phải Menu @@ -2872,6 +2820,8 @@ Trên Dưới Giữa (mặc định) + Trái + Phải Lưu trữ Đang đo dung lượng… Lần đầu mất vài giây. @@ -2978,6 +2928,7 @@ Chỉ còn %1$s Steam cần chỗ cho bản cập nhật và bộ nhớ đệm shader. Dưới 10 GB, cập nhật game có thể hỏng giữa chừng. Có thể dọn an toàn %1$s. + Không cảnh báo lại Dọn chỗ trống Vẫn chơi @@ -3653,4 +3604,17 @@ Đang nghe… Thiết bị này không hỗ trợ nhập bằng giọng nói. Cần quyền truy cập micrô để nhập bằng giọng nói. + Tải addon… + Tên + Tải nhiều nhất + Tất cả danh mục + Không tìm thấy addon + Không thể kết nối tới %1$s + Không có tệp cho phiên bản trò chơi này + Đặt lại vị trí + Hiện nhãn + Không thể nhập bố cục này + + Tuyết đỏ thẫm + diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index 6a01211ade..fbf54f0571 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -261,24 +261,8 @@ Steam 触控(测试版) Steam 的触控按键将从下次 Steam 会话开始生效 Steam 触控布局 - 屏幕按键 - 添加按键 - 文本 - 大小 - 透明 - 单键 - 扳机 - 切换 导出 - 胶囊 - 圆角 - 圆形 - 矩形 - 绑定 图片 - 清除 - 布局 - 保存 移动、调整大小或隐藏此游戏的触控按键;保存到 Steam 手柄 决定 Steam 把你的手柄识别为哪种类型,将在下次会话生效。 @@ -553,6 +537,7 @@ 没有 DroidDeck-Proton 使用它 Auto 更新正在等待网络连接。 无法检查新版本。 + 无法安装 %1$s。 新版本将等待不按流量计费的网络、关闭省电模式且设备不过热时下载。 %1$s 已更新到 %2$s。 %1$s %2$s 两次在几秒内关闭了游戏,因此 Auto 已退回到 %3$s。 @@ -754,7 +739,6 @@ 自动 开 关 - 光标 PC 键盘 Android 副屏 @@ -1177,43 +1161,25 @@ 3 · 电脑 ADB(最后手段) 仅在无线调试不可用时使用。 3 · 电脑 ADB(最后手段) - 会话中屏幕控制何时出现 颜色 - 屏幕按钮的色调 自定义 不透明度 %1$d%% 按钮大小 - 100% 为标准大小 - 摇杆按击 - 双击并按住摇杆可触发 L3 或 R3 自适应摇杆 游戏震动 摇杆死区 Steam 按钮 QAM 按钮 键盘按钮 - 按 Select 打开 Steam 菜单 - 代替 View/Select。 将 Joy-Con 用作 VR 手柄 SteamVR 运行时,每个 Joy-Con 都会成为一只手。将其指向前方并按住 HOME 键或截图键即可重新居中。 动画 - 摇杆出现在触摸处,松手后隐藏 + 存储空间不足时提醒 + 可用空间不足 10 GB 时,启动 Steam 前先询问。 布局 - 已保存自定义位置 - 已适配此屏幕 编辑 重置 - 按键映射 - 每个按钮发送各自的输入 - %2$d 个按钮中已重映射 %1$d 个 - 配置 - 重置手柄 - 恢复所有手柄设置和布局 - 每个屏幕按钮发送给游戏的内容。“隐藏”会移除该按钮。 - 屏幕按钮 - 默认值 - 重置映射 Android 应用 %1$d 个应用 未找到可启动的 Android 应用。 @@ -1292,7 +1258,6 @@ 尚未列出任何驱动。刷新以查找。 隐藏其他 GPU 的驱动 显示其他 GPU 的驱动 - 高级 UBWC 兼容性 如果 Steam 或 Android 出现条纹或闪烁,请尝试开启或关闭。重启 DroidDeck 后对两种驱动均生效。 重启 DroidDeck 以应用。 @@ -1332,16 +1297,8 @@ 手柄 摇杆 触控板 - 在摇杆与触控板之间切换 - 更多 Deck 控制 - 面板 关闭副屏控制 打开 Steam 菜单 - 左 · 上 - 左 · 下 - 右 · 上 - 右 · 下 - 点击 右触控板 左触控板 触控板 @@ -1832,6 +1789,7 @@ 琥珀 红色 粉色 + 灰色 A 键 B 键 X 键 @@ -1849,9 +1807,7 @@ 方向键左 方向键右 隐藏 - 拖动以移动 对齐 - 保存 无 键盘 + 触控板 终端 @@ -1864,13 +1820,6 @@ 正在下载着色器缓存 已暂停 等待开始 - 通知 - DroidDeck 底部屏幕 - 在游戏时于此处显示消息和其他通知。通知只显示在此屏幕上,不会被保存。Android 只会询问一次:其通知使用权页面会在此屏幕上打开,在那里开启 DroidDeck 后,会自动返回此页面。 - 允许通知使用权 - 没有通知。 - %1$d 条通知 - 清除 截图 显示 FPS 需要开启 HUD 速度 @@ -2622,7 +2571,6 @@ 成就不可用;请查看会话日志 成就已解锁 粘贴 - 方向键 左摇杆 右摇杆 菜单 @@ -2876,6 +2824,8 @@ 置顶 置底 居中(默认) + 左侧 + 右侧 存储 正在统计存储… 首次统计需要几秒钟。 @@ -2982,6 +2932,7 @@ 仅剩 %1$s 可用 Steam 需要空间用于更新与着色器缓存。低于 10 GB 时,游戏更新可能中途失败。 %1$s 可安全释放。 + 不再提醒 释放空间 仍然游玩 @@ -3657,4 +3608,17 @@ 正在聆听… 此设备不支持语音输入。 语音输入需要麦克风权限。 + 下载插件… + 名称 + 下载最多 + 所有类别 + 未找到插件 + 无法连接到 %1$s + 没有适用于此游戏版本的文件 + 重置位置 + 显示标签 + 无法导入此布局 + + 猩红之雪 + diff --git a/app/src/main/res/values-zh-rHK/strings.xml b/app/src/main/res/values-zh-rHK/strings.xml index 15b64ac3d6..d089696fb2 100644 --- a/app/src/main/res/values-zh-rHK/strings.xml +++ b/app/src/main/res/values-zh-rHK/strings.xml @@ -261,24 +261,8 @@ Steam 觸控(測試版) Steam 的觸控控制會在下次 Steam 會話啟用 Steam 觸控佈局 - 螢幕按鍵 - 新增按鍵 - 文本 - 大小 - 透明 - 單鍵 - 扳機 - 切換 匯出 - 膠囊 - 圓角 - 圓形 - 矩形 - 綁定 圖片 - 清除 - 佈局 - 儲存 移動、調整大小或隱藏此遊戲的觸控控制;會儲存至 Steam 控制器 Steam 如何識別你的控制器。下次會話生效。 @@ -553,6 +537,7 @@ 沒有 DroidDeck-Proton 使用它 Auto 更新正在等待網絡連線。 無法檢查新版本。 + 無法安裝 %1$s。 新版本將等待非按流量收費的網絡、關閉省電模式且裝置不過熱時下載。 %1$s 已更新至 %2$s。 %1$s %2$s 兩次在幾秒內關閉了遊戲,因此 Auto 已退回到 %3$s。 @@ -754,7 +739,6 @@ 自動 開 關 - 游標 電腦鍵盤 Android 第二螢幕 @@ -1093,43 +1077,25 @@ 3 · 電腦 ADB(最後手段) 只在無法使用無線偵錯時使用。 3 · 電腦 ADB(最後手段) - 會話中何時顯示觸控控制 顏色 - 螢幕按鍵的色調 自訂 不透明度 %1$d%% 按鍵大小 - 100% 為標準大小 - 按下搖桿 - 在搖桿上輕按兩下並按住,即可觸發 L3 或 R3 自動調整搖桿 遊戲震動 搖桿死區 Steam 按鈕 QAM 按鈕 鍵盤按鈕 - 按 Select 開啟 Steam 選單 - 代替 View/Select。 將 Joy-Con 用作 VR 控制器 SteamVR 執行時,每個 Joy-Con 都會變成一隻手。將它指向前方並按住 HOME 鍵或擷取鍵,即可重新置中。 動畫 - 搖桿會在你觸控的位置出現,放開後隱藏 + 儲存空間不足時提醒 + 可用空間少於 10 GB 時,啟動 Steam 前先詢問。 佈局 - 已儲存自訂位置 - 已配合此螢幕 編輯 重設 - 按鍵對應 - 每個按鍵都發送其原本的輸入 - 已重新對應 %1$d/%2$d 個按鍵 - 設定 - 重設控制器 - 還原所有控制器設定和佈局 - 每個螢幕按鍵向遊戲發送的輸入。「隱藏」會移除該按鍵。 - 螢幕按鍵 - 預設值 - 重設對應 Android 應用程式 %1$d 個應用程式 找不到可啟動的 Android 應用程式。 @@ -1208,7 +1174,6 @@ 尚未列出驅動程式。請重新整理以尋找。 隱藏其他 GPU 的驅動程式 顯示其他 GPU 的驅動程式 - 進階 UBWC 相容性 如果 Steam 或 Android 出現條紋或閃爍,請試試開啟或關閉。重新啟動 DroidDeck 後會套用到兩種驅動程式。 重新啟動 DroidDeck 以套用。 @@ -1248,16 +1213,8 @@ 控制器 搖桿 觸控板 - 在搖桿和觸控板之間切換 - 更多 Deck 控制 - 面板 關閉第二螢幕控制 開啟 Steam 選單 - 左側 · 上方 - 左側 · 下方 - 右側 · 上方 - 右側 · 下方 - 按下 右側觸控板 左側觸控板 觸控板 @@ -1754,6 +1711,7 @@ 琥珀色 紅色 粉紅色 + 灰色 A 鍵 B 鍵 X 鍵 @@ -1771,9 +1729,7 @@ 十字鍵左 十字鍵右 隱藏 - 拖曳以移動 對齊 - 儲存 無 鍵盤 + 觸控板 終端機 @@ -1786,13 +1742,6 @@ 正在下載著色器快取 已暫停 等待開始 - 通知 - DroidDeck 下方螢幕 - 在遊玩時於此處顯示訊息和其他通知。通知只會顯示在這個螢幕上,不會儲存。Android 只會詢問一次:通知存取權頁面會在這個螢幕上開啟,在那裡開啟 DroidDeck 後,就會自動回到此頁面。 - 允許通知存取權 - 沒有通知。 - %1$d 則通知 - 清除 螢幕截圖 顯示 FPS 需要開啟 HUD 速度 @@ -2618,7 +2567,6 @@ 成就不可用;請查看工作階段日誌 成就已解鎖 貼上 - 方向鍵 左搖桿 右搖桿 選單 @@ -2872,6 +2820,8 @@ 置頂 置底 置中(預設) + 左側 + 右側 儲存空間 正在統計儲存空間… 首次統計需要幾秒鐘。 @@ -2978,6 +2928,7 @@ 僅剩 %1$s 可用 Steam 需要空間用於更新與著色器快取。低於 10 GB 時,遊戲更新可能中途失敗。 %1$s 可安全釋放。 + 不再提醒 釋放空間 仍要遊玩 @@ -3656,4 +3607,17 @@ 正在聆聽… 此裝置不支援語音輸入。 語音輸入需要咪高峰權限。 + 下載插件… + 名稱 + 下載最多 + 所有類別 + 找不到插件 + 無法連接 %1$s + 沒有適用於此遊戲版本的檔案 + 重設位置 + 顯示標籤 + 無法匯入此佈局 + + 緋紅之雪 + diff --git a/app/src/main/res/values-zh-rTW/strings.xml b/app/src/main/res/values-zh-rTW/strings.xml index 98fd51506c..1b5dbb35dc 100644 --- a/app/src/main/res/values-zh-rTW/strings.xml +++ b/app/src/main/res/values-zh-rTW/strings.xml @@ -261,24 +261,8 @@ Steam 觸控(測試版) Steam 觸控控制會在下次 Steam 工作階段啟用 Steam 觸控版面配置 - 螢幕按鍵 - 新增按鍵 - 文本 - 大小 - 透明 - 單鍵 - 扳機 - 切換 匯出 - 膠囊 - 圓角 - 圓形 - 矩形 - 綁定 圖片 - 清除 - 版面配置 - 儲存 移動、調整大小或隱藏這款遊戲的觸控控制;會儲存到 Steam 控制器 Steam 辨識你的控制器的方式。下次工作階段生效。 @@ -553,6 +537,7 @@ 沒有 DroidDeck-Proton 使用它 Auto 更新正在等待網路連線。 無法檢查新版本。 + 無法安裝 %1$s。 新版本將等待非計量網路、關閉省電模式且裝置不過熱時下載。 %1$s 已更新至 %2$s。 %1$s %2$s 兩次在幾秒內關閉了遊戲,因此 Auto 已退回到 %3$s。 @@ -754,7 +739,6 @@ 自動 開 關 - 游標 電腦鍵盤 Android 第二螢幕 @@ -1093,43 +1077,25 @@ 3 · 電腦 ADB(最後手段) 僅在無線偵錯無法使用時使用。 3 · 電腦 ADB(最後手段) - 工作階段中顯示螢幕控制時 顏色 - 螢幕按鍵的色調 自訂 不透明度 %1$d%% 按鍵大小 - 100% 為標準大小 - 搖桿點擊 - 點兩下並按住搖桿可觸發 L3 或 R3 自適應搖桿 遊戲震動 搖桿死區 Steam 按鈕 QAM 按鈕 鍵盤按鈕 - 按 Select 開啟 Steam 功能表 - 代替 View/Select。 將 Joy-Con 當作 VR 控制器 SteamVR 執行時,每個 Joy-Con 都會變成一隻手。將它指向前方並按住 HOME 鍵或擷取鍵,即可重新置中。 動畫 - 搖桿會出現在你觸控的位置,並在放開後隱藏 + 儲存空間不足時提醒 + 可用空間少於 10 GB 時,啟動 Steam 前先詢問。 版面配置 - 已儲存自訂位置 - 已適應此螢幕 編輯 重設 - 按鍵映射 - 每個按鍵傳送對應的原始輸入 - %1$d/%2$d 個按鍵已重新映射 - 設定 - 重設控制器 - 還原所有控制器設定和版面配置 - 設定每個螢幕按鍵向遊戲傳送的輸入。「隱藏」會移除該按鍵。 - 螢幕按鍵 - 預設值 - 重設映射 Android 應用程式 %1$d 個應用程式 沒有找到可啟動的 Android 應用程式。 @@ -1208,7 +1174,6 @@ 尚未列出驅動程式。重新整理以尋找驅動程式。 隱藏其他 GPU 的驅動程式 顯示其他 GPU 的驅動程式 - 進階 UBWC 相容性 如果 Steam 或 Android 出現條紋或閃爍,請試試開啟或關閉。重新啟動 DroidDeck 後會套用到兩種驅動程式。 重新啟動 DroidDeck 以套用。 @@ -1248,16 +1213,8 @@ 手把 搖桿 觸控板 - 在搖桿與觸控板之間切換 - 更多 Deck 控制 - 面板 關閉第二螢幕控制 開啟 Steam 選單 - 左側 · 上方 - 左側 · 下方 - 右側 · 上方 - 右側 · 下方 - 點擊 右側觸控板 左側觸控板 觸控板 @@ -1754,6 +1711,7 @@ 琥珀 紅色 粉紅 + 灰色 A 按鈕 B 按鈕 X 按鈕 @@ -1771,9 +1729,7 @@ 方向鍵左 方向鍵右 隱藏 - 拖曳以移動 對齊 - 儲存 無 鍵盤 + 觸控板 終端機 @@ -1786,13 +1742,6 @@ 正在下載著色器快取 已暫停 等待開始 - 通知 - DroidDeck 底部螢幕 - 在遊玩時於此處顯示訊息和其他通知。通知只會顯示在這個螢幕上,不會儲存。Android 只會詢問一次:通知存取權頁面會在這個螢幕上開啟,在那裡開啟 DroidDeck 後,就會自動回到此頁面。 - 允許通知存取權 - 沒有通知。 - %1$d 則通知 - 清除 螢幕擷取畫面 顯示 FPS 需要開啟 HUD 速度 @@ -2618,7 +2567,6 @@ 成就無法使用;請查看工作階段記錄 成就已解鎖 貼上 - 方向鍵 左搖桿 右搖桿 選單 @@ -2872,6 +2820,8 @@ 置頂 置底 置中(預設) + 左側 + 右側 儲存空間 正在統計儲存空間… 首次統計需要幾秒鐘。 @@ -2978,6 +2928,7 @@ 僅剩 %1$s 可用 Steam 需要空間用於更新與著色器快取。低於 10 GB 時,遊戲更新可能中途失敗。 %1$s 可安全釋放。 + 不再提醒 釋放空間 仍要遊玩 @@ -3656,4 +3607,17 @@ 正在聆聽… 此裝置不支援語音輸入。 語音輸入需要麥克風權限。 + 下載插件… + 名稱 + 下載最多 + 所有類別 + 找不到插件 + 無法連線至 %1$s + 沒有適用於此遊戲版本的檔案 + 重設位置 + 顯示標籤 + 無法匯入此版面配置 + + 緋紅之雪 + diff --git a/app/src/main/res/values/input_library.xml b/app/src/main/res/values/input_library.xml new file mode 100644 index 0000000000..27d018b6e8 --- /dev/null +++ b/app/src/main/res/values/input_library.xml @@ -0,0 +1,132 @@ + + + Unreadable input settings were preserved in a recovery copy. You can continue editing the restored profiles. + Properties + Click properties + %1$s · Click + Buttons + Sticks, pads and triggers + Hide tools + Controller + Send controller input to Steam Input or the game. + This element sends no controller, mouse or keyboard input. + Mouse click + DroidDeck action + Action + Scroll speed: %1$d steps/s + + New profile + + New layout + %1$s · Used by %2$d profiles. Changes apply to every profile using these bindings. + Rename + Delete + Add elements to the layout before assigning their actions. + Could not save input settings + Name + Start from + Button + Stick + Trackpad + D-pad + Trigger + Done + Undo + + Add + Drag to move. Drag a corner to resize. Tap Properties to assign an action. + Editing on your second screen + The selected display is not available. Connect it to edit this layout at its actual size. + Discard unsaved layout changes? + This layout is used by %1$d profiles. Saved changes apply to all of them. + Default profiles + Desktop mode + Game mode + Default for Desktop and Game + Default for Game mode + Default for Desktop mode + Device settings + Controls + Buttons, sticks, triggers and extra keys + Copy profile + Reset actions + Reset the actions in %1$s to its base profile defaults? Layouts and other profiles will stay unchanged. + Delete %1$s? Modes using it will return to their built-in default profile. Saved layouts and shared settings will remain available. + Visibility + Applies to all profiles. Auto shows the main-screen controls when no gamepad is connected in Game mode. + Edit on screen + Saved layouts… + Choose a layout or create your own + Choose Profile controls in the second-screen session menu. The Profile and Second screen selectors in the Controller menu choose the controls to show. + Button actions + Shared with %1$s. Edits apply to those profiles too. + Reuse settings + Use settings from… + Copy from another profile or keep settings shared + Make independent + Keep the current setup and stop sharing changes + Layouts store elements and their positions. Each profile can assign its own actions. Edit at full size on the target screen. + Current layout + Delete the unused layout %1$s and its saved actions? + Rename current layout + Reuse %1$s from another profile. The other control sources will stay unchanged. + Keep physical controls shared + By default, all controls and custom layouts are independent copies. Sharing only links the physical controls. + Use these elements and positions. Sharing keeps future geometry edits in sync; your button actions remain independent. A different layout starts with unassigned actions. + Copy these controls and actions, or link them so future edits apply to both profiles. + Keep shared + Independent copy + Start stick at touch position + Hide stick when not in use + Tools + Auto (%1$s) + Custom (%1$s) + Save as new profile + Profile controls + Profile + Mouse touchpad gestures + When assigned to the mouse pointer: tap to click, hold to drag, and scroll with two fingers. + %1$s has a quick-menu override. Select Auto there to use its default profile and all three saved parts. + DroidDeck Input + Held while the stick is moved in any direction: press above 55%, release below 35%. Use Direction keys to assign each direction separately. DroidDeck commands run once per deflection. The stick click is configured separately. + Held while touching the trackpad; released when you lift your finger. DroidDeck commands run once per touch. The trackpad click is configured separately. + Held when the trigger passes 55%; released below 35%. DroidDeck commands run once per press. + Scroll direction + Vertical + Horizontal + Scroll horizontally + Measurements + Enable recording, use controls in a running session, then return and read the results. Measures DroidDeck dispatch time only, not display latency. Recording ends when the app process closes. + %1$d controls + Other saved layouts (%1$d) ▾ + Hide other saved layouts (%1$d) ▴ + Home / Guide + Back · single tap + Back · double tap + Back · twice in Steam, once in Desktop + In Steam mode. Holding Back also opens quick access. + Restore to default + Your panel choice is remembered. Desktop opens a second workspace; it does not switch the session or its input profile. + Virtual controller + The controller type seen by Steam or the game. + Touchscreen + How touches on the game screen are handled. Profile buttons keep their assigned actions. + Touch cursor + Show a pointer for direct touches on the game screen. + Touch and hold + Mouse action when holding a finger on the game screen. + Back button taps + First action: single tap. Second action: double tap. + Toggle button + Tap to hold, tap again to release. Opening a menu or switching profiles releases the button. + Shape + Rounded + Pill + Circle + Rectangle + Button label + Text + Imported image + Separate diagonal keys + Assign a key to each diagonal. An empty diagonal presses its two neighboring directions. + Neighboring directions + Your old screen keys are saved as imported profiles below. Select one to use or edit it. Your current profile selections have been kept. + The old screen keys could not be imported automatically. The original settings have been kept. + diff --git a/app/src/main/res/values/input_strings.xml b/app/src/main/res/values/input_strings.xml new file mode 100644 index 0000000000..bc18c561a7 --- /dev/null +++ b/app/src/main/res/values/input_strings.xml @@ -0,0 +1,37 @@ + + Input profiles + Profiles + Physical controller + Main screen + Second screen + Native / Steam Input + Native: %1$s + Send another controller control + Disabled + Key or key combination + Directional keys + Move pointer + Pointer speed: %1$d + Scroll + Scroll vertically + Left mouse button + Right mouse button + Middle mouse button + Open DroidDeck menu + Show keyboard + Open Steam menu + Open Steam quick access + Remove control + Add control + Device-specific buttons + Assign a physical button + Release the button, then choose Edit, Retry, or Cancel. B or Back cancels after release. + Retry + Press a button, including a back button reported as a keyboard key. Volume and Android system keys are reserved. The assignment applies only to this device. + Remove assignment + Input diagnostics + Record input dispatch time + Read measurements + Measures Android dispatch through publication to the existing input transport. This excludes Bluetooth arrival, guest polling, rendering and display latency. Recording is off by default. + Use normal input binding + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 145f52289d..ec7fe245bf 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -150,8 +150,8 @@ FEX preset Applies on the next game launch. Games - FEX defaults - Uses FEX defaults. + Stock FEX + No FEX tuning applied. Stability Safest, slowest settings. Compatibility @@ -321,23 +321,7 @@ Steam touch (beta) Steam\'s touch controls start with the next Steam session Steam touch layout - Screen keys - Add key - Text - Size - Alpha - Key - Pill - Round - Circle - Rect - Trigger - Bind Image - Clear - Toggle - Layout - Save Export Move, resize or hide this game\'s touch controls; saved to Steam Controller @@ -634,6 +618,7 @@ No DroidDeck-Proton uses this Auto updates wait for a connection. Couldn\'t check for new builds. + Couldn\'t install %1$s. New builds wait for an unmetered connection, battery saver off and a cool device. %1$s updated to %2$s. %1$s %2$s closed a game within seconds twice, so Auto went back to %3$s. @@ -683,7 +668,10 @@ Disables Proton\'s gamepad navigation helper, which costs CPU in every game. Run proot without seccomp May fix missing-syscall errors on some kernels. Can reduce performance. + Run without proot (experimental) + Starts the Linux side directly on Android. Applies to the next session. Turn off if something fails to start. proot fast path + Not used without proot. Needs proot\'s seccomp filter. Faster file lookups and less stutter. Turn off if a program can\'t find its files. Draw the cursor inside gamescope @@ -859,7 +847,6 @@ Auto On Off - Cursor PC keyboard Android Second screen @@ -1034,6 +1021,7 @@ On Off Bottom screen controls in games + Android keyboard for text fields Opens a panel on the second screen when a session starts: the Deck buttons and trackpads with a Steam Deck or Steam Controller, otherwise the one you used last. Typing on the top screen Done @@ -1153,6 +1141,29 @@ Test builds Try a fix before it\'s released None right now + Lab build + Lab builds + In use + Paused after Android re-optimized DroidDeck + Not loaded: %1$s + Waiting for a restart + Kept + For another version + Rebuild + Deletes this build from the device + Restart on it + Turn off + Use + Delete + Delete now + Could not prepare the restart: %1$s. Pair wireless debugging in Setup and try again. + Lab build installed + DroidDeck restarted on %1$s + Lab build paused + Android re-optimized DroidDeck, so %1$s is not loaded. Open Updates to restart on it. + Lab build turned off + It was built for another version of DroidDeck. It is kept in Updates. + DroidDeck did not start on it twice PR #%1$d Not built for this copy of DroidDeck Updated %1$s @@ -1238,43 +1249,25 @@ 3 · Computer ADB (last resort) Only if Wireless debugging is unavailable. 3 · Computer ADB (last resort) - When the touch controls appear in a session Color - Tint of the on-screen buttons Custom Opacity %1$d%% Button size - 100% is the standard size - Stick click - Double-tap and hold a stick for L3 or R3 Adaptive sticks Game vibration Stick dead zone Steam button QAM button Keyboard button - Press Select to open the Steam menu - Instead of View/Select. Joy-Cons as VR controllers While SteamVR runs, each Joy-Con becomes a hand. Point it ahead and hold Home or Capture to recenter it. Animations - Sticks appear where you touch and hide on release + Warn when storage is low + Ask before starting Steam when free space is under 10 GB. Layout - Custom positions saved - Fitted to this screen Edit Reset - Button mapping - Every button sends its own input - %1$d of %2$d buttons remapped - Configure - Reset controller - Restores all controller settings and the layout - What each on-screen button sends to the game. Hidden removes the button. - On-screen buttons - Defaults - Reset mapping Android apps %1$d apps No launchable Android apps found. @@ -1396,7 +1389,6 @@ No drivers listed yet. Refresh to look for them. Hide drivers for other GPUs Show drivers for other GPUs - Advanced UBWC compatibility Try On or Off if Steam or Android shows stripes or flickering. Applies to both drivers after restarting DroidDeck. Restart DroidDeck to apply. @@ -1436,16 +1428,8 @@ Gamepad Stick Trackpad - Switch between stick and trackpad - More Deck controls - PANELS Close second-screen controls Open Steam menu - Left · upper - Left · lower - Right · upper - Right · lower - Click Right trackpad Left trackpad Trackpad @@ -1968,6 +1952,7 @@ Amber Red Pink + Gray A button B button X button @@ -1996,9 +1981,7 @@ D-pad left D-pad right Hidden - Drag to move Snap - Save None Keyboard + trackpad Terminal @@ -2028,13 +2011,6 @@ Downloading shader cache Paused Waiting to start - Notifications - DroidDeck bottom screen - Show messages and other notifications here while you play. They stay on this screen only and are not saved. Android asks once: its notification access page opens on this screen; turn DroidDeck on there and this page comes back by itself. - Allow notification access - No notifications. - %1$d notifications - Clear Screenshots FPS needs the HUD on Speed @@ -2933,7 +2909,6 @@ Mali bridge: on %1$s is not an Adreno. The experimental Vulkan bridge is on: the session draws through the phone\'s own driver. Expect bugs; send the session logs. Paste - D-pad Left stick Right stick Menu @@ -3177,6 +3152,8 @@ Experimental Pro 2 head tracking. Brightness, volume, and display switching are not supported yet. Picture position Top + Left + Right Bottom Center (default) 6DoF virtual screen @@ -3294,6 +3271,7 @@ Only %1$s free Steam needs room for updates and shader caches. Below 10 GB, game updates can fail partway. %1$s can be freed safely. + Don\'t warn me about this again Free up space Play anyway @@ -3872,4 +3850,19 @@ Listening… Voice typing is not available on this device. Microphone access is needed for voice typing. + Download addons… + Name + Most downloaded + All categories + No addons found + Could not reach %1$s + No file for this game version + WoWInterface + CurseForge + Reset positions + Show label + Could not import this layout + + Scarlet Snow + White surfaces with red accents. diff --git a/app/src/main/resources/ea/graphql/GameImages.gql b/app/src/main/resources/ea/graphql/GameImages.gql new file mode 100644 index 0000000000..92d8a50b87 --- /dev/null +++ b/app/src/main/resources/ea/graphql/GameImages.gql @@ -0,0 +1,69 @@ +query GameImages($gameSlug: String!, $locale: Locale!) { + game(slug: $gameSlug, locale: $locale) { + id + slug + title + keyArt { + ...imageRenditionData + __typename + } + packArt { + ...PackArt + __typename + } + ...PrimaryLogo + __typename + } +} + +fragment imageRenditionData on ImageRendition { + path + aspect9x16Image { + path + __typename + } + aspect16x9Image { + path + __typename + } + aspect1x1Image { + path + __typename + } + largestImage { + path + __typename + } + rawImages { + path + __typename + } + __typename +} + +fragment PrimaryLogo on GameV2 { + primaryLogo(uiClientContexts: ["juno-web-game-tile"]) { + path + title + largestImage { + path + width + height + __typename + } + __typename + } + __typename +} + +fragment PackArt on ImageRendition { + aspect2x1Image { + path + __typename + } + aspect9x16Image { + path + __typename + } + __typename +} diff --git a/app/src/main/resources/ea/graphql/GetUserPlayer.gql b/app/src/main/resources/ea/graphql/GetUserPlayer.gql new file mode 100644 index 0000000000..4094c264ab --- /dev/null +++ b/app/src/main/resources/ea/graphql/GetUserPlayer.gql @@ -0,0 +1,49 @@ +query GetUserPlayer { + me { + id: pd + pd + player { + ...Player + __typename + } + __typename + } +} + +fragment Player on Player { + id: pd + pd + psd + displayName + uniqueName + nickname + avatar { + ...Avatar + __typename + } + relationship + __typename +} + +fragment Avatar on AvatarList { + large { + ...image + __typename + } + medium { + ...image + __typename + } + small { + ...image + __typename + } + __typename +} + +fragment image on Image { + height + width + path + __typename +} diff --git a/app/src/main/resources/ea/graphql/availableBuilds.gql b/app/src/main/resources/ea/graphql/availableBuilds.gql new file mode 100644 index 0000000000..b410f0a7f9 --- /dev/null +++ b/app/src/main/resources/ea/graphql/availableBuilds.gql @@ -0,0 +1,9 @@ +query availableBuilds($offerId: String!) { + availableBuilds(offerId: $offerId) { + buildId + downloadType + gameVersion + buildLiveDate + __typename + } +} diff --git a/app/src/main/resources/ea/graphql/downloadUrl.gql b/app/src/main/resources/ea/graphql/downloadUrl.gql new file mode 100644 index 0000000000..0110c64f93 --- /dev/null +++ b/app/src/main/resources/ea/graphql/downloadUrl.gql @@ -0,0 +1,7 @@ +query downloadUrl($offerId: String!, $buildId: String) { + downloadUrl(offerId: $offerId, buildId: $buildId) { + url + syncUrl + __typename + } +} diff --git a/app/src/main/resources/ea/graphql/getLegacyCatalogDefs.gql b/app/src/main/resources/ea/graphql/getLegacyCatalogDefs.gql new file mode 100644 index 0000000000..6d3da0d9f8 --- /dev/null +++ b/app/src/main/resources/ea/graphql/getLegacyCatalogDefs.gql @@ -0,0 +1,66 @@ +query getLegacyCatalogDefs($offerIds: [String!]!, $locale: Locale) { + legacyOffers(offerIds: $offerIds, locale: $locale) { + offerId: id + contentId + basePlatform + primaryMasterTitleId + mdmProjectNumber + achievementSetOverride + gameLauncherURL + gameLauncherURLClientID + stagingKeyPath + mdmTitleIds + multiplayerId + executePathOverride + installationDirectory + installCheckOverride + monitorPlay + displayName + displayType + igoBrowserDefaultUrl + executeParameters + softwareLocales + dipManifestRelativePath + metadataInstallLocation + distributionSubType + downloads { + igoApiEnabled + downloadType + version + executeElevated + buildReleaseVersion + buildLiveDate + buildMetaData + gameVersion + treatUpdatesAsMandatory + enableDifferentialUpdate + __typename + } + locale + greyMarketControls + isDownloadable + isPreviewDownload + downloadStartDate + releaseDate + useEndDate + subscriptionUnlockDate + subscriptionUseEndDate + softwarePlatform + softwareId + downloadPackageType + installerPath + processorArchitecture + macBundleID + gameEditionTypeFacetKeyRankDesc + appliedCountryCode + cloudSaveConfigurationOverride + firstParties{ + partner + partnerId + partnerIdType + __typename + } + suppressedOfferIds + __typename + } +} diff --git a/app/src/main/resources/ea/graphql/getLegacyEntitlements.gql b/app/src/main/resources/ea/graphql/getLegacyEntitlements.gql new file mode 100644 index 0000000000..d4a6de1012 --- /dev/null +++ b/app/src/main/resources/ea/graphql/getLegacyEntitlements.gql @@ -0,0 +1,33 @@ +query getLegacyEntitlements($limit: Int, $next: String, $storeFronts: [UserLegacyEntitlementStorefront!]!) { + me { + legacyEntitlements( + paging: {limit: $limit, next: $next}, + storefronts: $storeFronts, + status: [ACTIVE, DISABLED] + ) { + next + items { + cdKey + entitlementTag + entitlementType + externalType + grantDate + groupName + id + isSubscription + isSubscriptionExpired + lastModifiedDate + originPermissions + productId + status + statusReasonCode + terminationDate + trialTimeRemainingSeconds + useCount + version + __typename + } + __typename + } + } +} \ No newline at end of file diff --git a/app/src/main/resources/ea/graphql/getPreloadedOwnedGames.gql b/app/src/main/resources/ea/graphql/getPreloadedOwnedGames.gql new file mode 100644 index 0000000000..b3a5a488eb --- /dev/null +++ b/app/src/main/resources/ea/graphql/getPreloadedOwnedGames.gql @@ -0,0 +1,69 @@ +query getPreloadedOwnedGames($next: String, $locale: Locale, $limit: Int, $type: [GameProductType!]!, $entitlementEnabled: Boolean, $isMac: Boolean = false, $storefronts: [UserGameProductStorefront!], $ownershipMethods: [OwnershipMethod!], $processorArchitectures: [ProcessorArchitecture!], $platforms: [GamePlatform!]!) { + me { + id: pd + ownedGameProducts(storefronts: $storefronts, locale: $locale, paging: {limit: $limit, next: $next}, productFound: true, orderBy: {field: NAME, direction: ASC}, ownershipMethod: $ownershipMethods, processorArchitectures: $processorArchitectures, type: $type, downloadableOnly: false, entitlementEnabled: $entitlementEnabled, platforms: $platforms) { + next + totalCount + items { + ...userGameProductFragment + __typename + } + __typename + } + __typename + } +} + +fragment userGameProductFragment on UserGameProduct { + id: originOfferId + originOfferId + status + product { + ...OwnedGameProductFragment + __typename + } + __typename +} + +fragment OwnedGameProductFragment on GameProduct { + id + name + downloadable + gameSlug + trialDetails { + trialType + __typename + } + baseItem(availabilities: [VISIBLE]) { + title + id + baseGameSlug + gameType + __typename + } + gamePlatformDetails @include(if: $isMac) { + gamePlatform + __typename + } + processorArchitectureDetails @include(if: $isMac) { + processorArchitecture + platform + __typename + } + gameProductUser(storefronts: $storefronts) { + ownershipMethods + initialEntitlementDate + entitlementId + gameProductUserTrial { + trialTimeRemainingSeconds + __typename + } + status + __typename + } + purchaseStatus { + repurchasable + __typename + } + __typename +} diff --git a/app/src/main/resources/ea/graphql/sdkEntitlements.gql b/app/src/main/resources/ea/graphql/sdkEntitlements.gql new file mode 100644 index 0000000000..df6613663e --- /dev/null +++ b/app/src/main/resources/ea/graphql/sdkEntitlements.gql @@ -0,0 +1,27 @@ +query sdkEntitlements($productIds: [String!]!, $includeChildGroups: Boolean!, $groupNames: [String!]!, $entitlementTag: String!, $pageSize: Int!, $pageNumber: Int!) { + me { + sdkEntitlements( + productIds: $productIds, + includeChildGroups: $includeChildGroups, + groupNames: $groupNames, + entitlementTag: $entitlementTag, + paging: { + pageSize: $pageSize, pageNumber: $pageNumber + } + ) { + entitlements { + entitlementTag + entitlementType + grantDate + groupName + id + productId + terminationDate + useCount + version + __typename + } + __typename + } + } +} diff --git a/app/src/main/rust/stores/Cargo.lock b/app/src/main/rust/stores/Cargo.lock index 2fac37c5aa..6a470b3bea 100644 --- a/app/src/main/rust/stores/Cargo.lock +++ b/app/src/main/rust/stores/Cargo.lock @@ -195,10 +195,13 @@ dependencies = [ "libc", "lzma-rs", "reqwest", + "ruzstd", "serde_json", "sha1", "sha2", "tokio", + "tokio-rustls", + "webpki-roots", ] [[package]] @@ -897,6 +900,15 @@ version = "1.0.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d" +[[package]] +name = "ruzstd" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7c1c839d570d835527c9a5e4db7cb2198683a988cb9d7293fc8674e6bd58fc8" +dependencies = [ + "twox-hash", +] + [[package]] name = "ryu" version = "1.0.23" @@ -1243,6 +1255,12 @@ version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" +[[package]] +name = "twox-hash" +version = "2.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "86a801b3cea342a06d468c8710662aa29e5e05e4f5c0d62f00bbb7f2ad7941c2" + [[package]] name = "typenum" version = "1.20.0" diff --git a/app/src/main/rust/stores/Cargo.toml b/app/src/main/rust/stores/Cargo.toml index 61d121a22a..97a27659a1 100644 --- a/app/src/main/rust/stores/Cargo.toml +++ b/app/src/main/rust/stores/Cargo.toml @@ -15,11 +15,14 @@ libc = "0.2" lzma-rs = { version = "0.3", default-features = false } flate2 = { version = "1", default-features = false, features = ["rust_backend"] } reqwest = { version = "0.12", default-features = false, features = ["rustls-tls", "gzip"] } -tokio = { version = "1", default-features = false, features = ["rt", "net", "time", "sync"] } +tokio = { version = "1", default-features = false, features = ["rt", "net", "time", "sync", "io-util"] } futures-util = { version = "0.3", default-features = false, features = ["std"] } serde_json = { version = "1", features = ["preserve_order"] } sha1 = "0.10" sha2 = "0.10" +tokio-rustls = { version = "0.26", default-features = false, features = ["ring", "tls12"] } +webpki-roots = "1" +ruzstd = "0.8" [build-dependencies] cc = "1" diff --git a/app/src/main/rust/stores/src/ea/ffi.rs b/app/src/main/rust/stores/src/ea/ffi.rs new file mode 100644 index 0000000000..7656c6b33e --- /dev/null +++ b/app/src/main/rust/stores/src/ea/ffi.rs @@ -0,0 +1,207 @@ +use std::panic::{catch_unwind, AssertUnwindSafe}; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::Arc; + +use jni::objects::{GlobalRef, JClass, JObject, JString, JValue}; +use jni::sys::{jlong, jstring, JNI_FALSE, JNI_TRUE}; +use jni::{JNIEnv, JavaVM}; +use serde_json::json; + +use super::install; +use crate::amazon::install::{Events, Stop}; + +fn read_string(env: &mut JNIEnv, value: &JString) -> String { + if value.is_null() { + return String::new(); + } + env.get_string(value).map(|s| s.to_string_lossy().into_owned()).unwrap_or_default() +} + +fn new_string(env: &mut JNIEnv, text: &str) -> jstring { + env.new_string(text).map(|s| s.into_raw()).unwrap_or(std::ptr::null_mut()) +} + +fn call(op: &str, request: &str) -> String { + let parsed: serde_json::Value = serde_json::from_str(request).unwrap_or(serde_json::Value::Null); + let url = parsed.get("url").and_then(|v| v.as_str()).unwrap_or(""); + let answer = match op { + "zip.size" => install::size(url).unwrap_or_else(|e| json!({ "error": e })), + _ => json!({ "error": format!("unknown operation {op}") }), + }; + answer.to_string() +} + +#[no_mangle] +pub extern "system" fn Java_com_droiddeck_launcher_stores_ea_EaNative_nativeCall( + mut env: JNIEnv, + _class: JClass, + op: JString, + request: JString, +) -> jstring { + let op = read_string(&mut env, &op); + let request = read_string(&mut env, &request); + match catch_unwind(AssertUnwindSafe(|| call(&op, &request))) { + Ok(text) => new_string(&mut env, &text), + Err(_) => std::ptr::null_mut(), + } +} + +struct Listener { + vm: JavaVM, + target: GlobalRef, +} + +impl Listener { + fn with_env(&self, fallback: R, f: impl FnOnce(&mut JNIEnv) -> R) -> R { + let Ok(mut env) = self.vm.attach_current_thread_as_daemon() else { + return fallback; + }; + let out = f(&mut env); + if env.exception_check().unwrap_or(false) { + let _ = env.exception_clear(); + } + out + } + + fn text(&self, method: &str, text: &str) { + self.with_env((), |env| { + let Ok(s) = env.new_string(text) else { return }; + let s = JObject::from(s); + let _ = env.call_method(self.target.as_obj(), method, "(Ljava/lang/String;)V", &[JValue::Object(&s)]); + let _ = env.delete_local_ref(s); + }); + } + + fn counts(&self, method: &str, a: u64, b: u64, c: u32, d: u32) { + self.with_env((), |env| { + let _ = env.call_method( + self.target.as_obj(), + method, + "(JJII)V", + &[ + JValue::Long(a.min(i64::MAX as u64) as i64), + JValue::Long(b.min(i64::MAX as u64) as i64), + JValue::Int(c.min(i32::MAX as u32) as i32), + JValue::Int(d.min(i32::MAX as u32) as i32), + ], + ); + }); + } + + fn complete(&self, success: bool, cancelled: bool, error: &str, result: &str) { + self.with_env((), |env| { + let (Ok(e), Ok(r)) = (env.new_string(error), env.new_string(result)) else { return }; + let (e, r) = (JObject::from(e), JObject::from(r)); + let _ = env.call_method( + self.target.as_obj(), + "onComplete", + "(ZZLjava/lang/String;Ljava/lang/String;)V", + &[ + JValue::Bool(if success { JNI_TRUE } else { JNI_FALSE }), + JValue::Bool(if cancelled { JNI_TRUE } else { JNI_FALSE }), + JValue::Object(&e), + JValue::Object(&r), + ], + ); + let _ = env.delete_local_ref(e); + let _ = env.delete_local_ref(r); + }); + } +} + +impl Events for Listener { + fn log(&self, line: &str) { + self.text("onLog", &crate::amazon::net::scrub_urls(line)); + } + + fn status(&self, text: &str) { + self.text("onStatus", text); + } + + fn checking(&self, done_bytes: u64, total_bytes: u64, files: u32, files_total: u32) { + self.counts("onCheck", done_bytes, total_bytes, files, files_total); + } + + fn sizes(&self, pending_bytes: u64, total_bytes: u64) { + self.with_env((), |env| { + let _ = env.call_method( + self.target.as_obj(), + "onSizes", + "(JJ)V", + &[ + JValue::Long(pending_bytes.min(i64::MAX as u64) as i64), + JValue::Long(total_bytes.min(i64::MAX as u64) as i64), + ], + ); + }); + } + + fn progress(&self, done: u64, total: u64, files: u32, files_total: u32) { + self.counts("onProgress", done, total, files, files_total); + } + + fn before_replace(&self) -> bool { + self.with_env(false, |env| match env.call_method(self.target.as_obj(), "beforeReplace", "()Z", &[]) { + Ok(v) => v.z().unwrap_or(false), + Err(_) => false, + }) + } +} + +#[no_mangle] +pub extern "system" fn Java_com_droiddeck_launcher_stores_ea_EaNative_nativeInstall( + mut env: JNIEnv, + _class: JClass, + request: JString, + listener: JObject, +) -> jlong { + if listener.is_null() { + return 0; + } + let Ok(vm) = env.get_java_vm() else { return 0 }; + let Ok(target) = env.new_global_ref(&listener) else { return 0 }; + let listener = Listener { vm, target }; + let text = read_string(&mut env, &request); + let parsed = serde_json::from_str::(&text) + .map_err(|_| "The install request could not be read".to_string()) + .and_then(|v| install::Request::from_json(&v)); + let request = match parsed { + Ok(r) => r, + Err(e) => { + listener.complete(false, false, &e, ""); + return 0; + } + }; + let cancel = Arc::new(AtomicBool::new(false)); + let worker_cancel = Arc::clone(&cancel); + let spawned = std::thread::Builder::new().name("ea-install".into()).spawn(move || { + crate::priority::background(); + let result = catch_unwind(AssertUnwindSafe(|| install::run(&request, &worker_cancel, &listener))) + .unwrap_or_else(|_| Err(Stop::Failed("The EA install stopped on an internal error".into()))); + match result { + Ok(done) => listener.complete(true, false, "", &done.to_json().to_string()), + Err(Stop::Cancelled) => listener.complete(false, true, "cancelled", ""), + Err(Stop::Failed(e)) => listener.complete(false, false, &e, ""), + } + }); + if spawned.is_err() { + return 0; + } + Box::into_raw(Box::new(cancel)) as jlong +} + +#[no_mangle] +pub extern "system" fn Java_com_droiddeck_launcher_stores_ea_EaNative_nativeCancel(_env: JNIEnv, _class: JClass, handle: jlong) { + let ptr = handle as *const Arc; + if let Some(cancel) = unsafe { ptr.as_ref() } { + cancel.store(true, Ordering::SeqCst); + } +} + +#[no_mangle] +pub extern "system" fn Java_com_droiddeck_launcher_stores_ea_EaNative_nativeRelease(_env: JNIEnv, _class: JClass, handle: jlong) { + let ptr = handle as *mut Arc; + if !ptr.is_null() { + drop(unsafe { Box::from_raw(ptr) }); + } +} diff --git a/app/src/main/rust/stores/src/ea/install.rs b/app/src/main/rust/stores/src/ea/install.rs new file mode 100644 index 0000000000..fb2cd80ef5 --- /dev/null +++ b/app/src/main/rust/stores/src/ea/install.rs @@ -0,0 +1,850 @@ +// SPDX-License-Identifier: GPL-3.0-or-later +//! Installs or updates an EA game from its build archive: files whose size and CRC already match +//! are kept, everything else is fetched entry by entry with ranged reads and inflated in place. + +use std::collections::{HashMap, HashSet}; +use std::fs::{self, File, OpenOptions}; +use std::io::{Read, Write}; +use std::os::unix::fs::FileExt; +use std::path::{Path, PathBuf}; +use std::sync::atomic::{AtomicBool, AtomicU64, AtomicUsize, Ordering}; +use std::sync::Mutex; +use std::time::{Duration, Instant, UNIX_EPOCH}; + +use flate2::{Crc, Decompress, FlushDecompress, Status}; +use serde_json::{json, Value}; + +use super::net; +use super::zip::{self, Entry}; +use crate::amazon::install::check::{free_bytes, space_problem}; +use crate::amazon::install::{Events, ReplaceGate, Stop}; +use crate::fetch_core::{distinct_host_count, run_fetch, FetchItem, FetchOptions, FetchSink, SinkError}; + +pub const LEDGER: &str = ".droiddeck-ea-files"; +pub const IN_PROGRESS_MARKER: &str = ".droiddeck-ea-downloading"; +const TEMP_SUFFIX: &str = ".ddpart"; +const PROGRESS_EVERY_MS: u64 = 50; +const CHECK_REPORT_EVERY: Duration = Duration::from_millis(200); +const REQUEST_TIMEOUT: Duration = Duration::from_secs(120); +const INFLATE_BUFFER: usize = 256 * 1024; +/// Records larger than this are fetched as parallel ranges into a scratch file, then inflated, +/// so one big file does not ride a single connection. +pub const PART_BYTES: u64 = 16 * 1024 * 1024; +const SPLIT_SUFFIX: &str = ".ddzip"; + +struct Split { + tmp: PathBuf, + file: Mutex>, + received: Vec, + left: AtomicUsize, +} + +#[derive(Clone, Copy)] +enum Work { + Whole(usize), + Part { job: usize, part: usize, start: u64, len: u64 }, +} + +/// The fetches for each job: whole records, or byte ranges of one when it is large. +fn works(jobs: &[&Planned], part_bytes: u64) -> Vec { + let mut out = Vec::new(); + for (i, p) in jobs.iter().enumerate() { + let span = p.range_end - p.entry.local_offset + 1; + if span <= part_bytes * 2 { + out.push(Work::Whole(i)); + continue; + } + let mut start = 0; + let mut part = 0; + while start < span { + let len = part_bytes.min(span - start); + out.push(Work::Part { job: i, part, start, len }); + start += len; + part += 1; + } + } + out +} +pub const REPLACE_REFUSED: &str = "The install record could not be switched to updating; nothing was replaced"; + +pub struct Request { + pub url: String, + pub game_dir: PathBuf, + pub max_workers: usize, + pub process_workers: usize, + pub ca_bundle: String, + pub part_bytes: u64, +} + +impl Request { + pub fn from_json(v: &Value) -> Result { + let text = |k: &str| v.get(k).and_then(|x| x.as_str()).unwrap_or("").to_string(); + let game_dir = text("gameDir"); + if game_dir.is_empty() || !game_dir.starts_with('/') { + return Err("No install folder was given".into()); + } + let url = text("url"); + if !url.starts_with("https://") && !url.starts_with("http://") { + return Err("EA gave no download for this game".into()); + } + Ok(Request { + url, + game_dir: PathBuf::from(game_dir), + max_workers: v.get("maxWorkers").and_then(|n| n.as_u64()).unwrap_or(8) as usize, + process_workers: v.get("processWorkers").and_then(|n| n.as_u64()).unwrap_or(2) as usize, + ca_bundle: text("caBundle"), + part_bytes: v.get("partBytes").and_then(|n| n.as_u64()).filter(|n| *n > 0).unwrap_or(PART_BYTES), + }) + } +} + +#[derive(Debug, Default)] +pub struct Finished { + pub total_bytes: u64, + pub download_bytes: u64, + pub file_count: usize, + pub files_written: usize, + pub bytes_written: u64, + pub removed: usize, +} + +impl Finished { + pub fn to_json(&self) -> Value { + json!({ + "totalBytes": self.total_bytes, + "downloadBytes": self.download_bytes, + "fileCount": self.file_count, + "filesWritten": self.files_written, + "bytesWritten": self.bytes_written, + "removed": self.removed, + }) + } +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +struct Known { + crc: u32, + size: u64, + modified: u64, +} + +fn modified_ms(path: &Path) -> u64 { + fs::metadata(path) + .and_then(|m| m.modified()) + .ok() + .and_then(|t| t.duration_since(UNIX_EPOCH).ok()) + .map(|d| d.as_millis() as u64) + .unwrap_or(0) +} + +fn read_ledger(dir: &Path) -> HashMap { + let Ok(text) = fs::read_to_string(dir.join(LEDGER)) else { return HashMap::new() }; + let mut out = HashMap::new(); + for line in text.lines() { + let mut parts = line.splitn(4, '\t'); + let (Some(crc), Some(size), Some(modified), Some(path)) = (parts.next(), parts.next(), parts.next(), parts.next()) else { + continue; + }; + let (Ok(crc), Ok(size), Ok(modified)) = (u32::from_str_radix(crc, 16), size.parse(), modified.parse()) else { + continue; + }; + out.insert(path.to_string(), Known { crc, size, modified }); + } + out +} + +fn ledger_line(path: &str, k: Known) -> String { + format!("{:08x}\t{}\t{}\t{path}\n", k.crc, k.size, k.modified) +} + +fn crc_of(path: &Path, cancel: &AtomicBool) -> Result, ()> { + let Ok(mut f) = File::open(path) else { return Ok(None) }; + let mut crc = Crc::new(); + let mut buf = vec![0u8; 1024 * 1024]; + loop { + if cancel.load(Ordering::Relaxed) { + return Err(()); + } + match f.read(&mut buf) { + Ok(0) => return Ok(Some(crc.sum())), + Ok(n) => crc.update(&buf[..n]), + Err(e) if e.kind() == std::io::ErrorKind::Interrupted => {} + Err(_) => return Ok(None), + } + } +} + +/// Whether two paths name one file, as they do for a case-only rename on case-insensitive storage. +fn same_file(a: &Path, b: &Path) -> bool { + use std::os::unix::fs::MetadataExt; + match (fs::metadata(a), fs::metadata(b)) { + (Ok(x), Ok(y)) => x.dev() == y.dev() && x.ino() == y.ino(), + _ => false, + } +} + +fn temp_path(dest: &Path) -> PathBuf { + let mut name = dest.as_os_str().to_owned(); + name.push(TEMP_SUFFIX); + PathBuf::from(name) +} + +pub struct Planned { + pub entry: Entry, + pub path: String, + pub range_end: u64, +} + +/// Every file entry with its safe path and the last byte its local record can occupy. +pub fn plan(entries: &[Entry], directory_offset: u64) -> Result, String> { + let mut offsets: Vec = entries.iter().map(|e| e.local_offset).collect(); + offsets.push(directory_offset); + offsets.sort_unstable(); + offsets.dedup(); + let mut seen = HashSet::new(); + let mut out = Vec::new(); + for e in entries.iter().filter(|e| !e.is_dir()) { + if e.method != zip::STORED && e.method != zip::DEFLATED { + return Err(format!("{} uses an unsupported compression method ({})", e.name, e.method)); + } + let path = zip::safe_path(&e.name).ok_or_else(|| format!("The archive names a file outside the game folder: {}", e.name))?; + if !seen.insert(path.to_lowercase()) { + continue; + } + let at = offsets.partition_point(|&o| o <= e.local_offset); + let next = offsets.get(at).copied().unwrap_or(directory_offset); + let minimum = e.local_offset + zip::LOCAL_FIXED as u64 + e.compressed; + if next < minimum { + return Err(format!("{} overlaps the next record in the archive", e.name)); + } + out.push(Planned { entry: e.clone(), path, range_end: next - 1 }); + } + Ok(out) +} + +struct Slot { + header: Vec, + payload_from: Option, + consumed: u64, + file: File, + tmp: PathBuf, + crc: Crc, + written: u64, + inflate: Option, + buffer: Vec, +} + +struct Placer<'a> { + jobs: &'a [&'a Planned], + game_dir: &'a Path, + slots: Vec>>, + splits: Vec>, + works: Vec, + placed: Vec, + ledger: Mutex>, + events: &'a dyn Events, + gate: &'a ReplaceGate, + stop: &'a AtomicBool, + failure: Mutex>, + base_done: u64, + base_files: u32, + total: u64, + total_files: u32, + placed_bytes: AtomicU64, + in_flight: AtomicU64, + shown: AtomicU64, + bytes_written: AtomicU64, + files_placed: AtomicUsize, + last_emit: AtomicU64, + started: Instant, +} + +fn discard(slot: Slot) { + drop(slot.file); + let _ = fs::remove_file(&slot.tmp); +} + +impl Placer<'_> { + fn fail(&self, message: String) -> String { + let mut slot = self.failure.lock().expect("failure lock"); + if slot.is_none() { + *slot = Some(message.clone()); + } + drop(slot); + self.stop.store(true, Ordering::SeqCst); + message + } + + fn emit(&self, force: bool) { + let now = self.started.elapsed().as_millis() as u64; + let last = self.last_emit.load(Ordering::Relaxed); + if !force && now < last + PROGRESS_EVERY_MS { + return; + } + if !force && self.last_emit.compare_exchange(last, now, Ordering::Relaxed, Ordering::Relaxed).is_err() { + return; + } + self.last_emit.store(now, Ordering::Relaxed); + let value = self.base_done + self.placed_bytes.load(Ordering::Relaxed) + self.in_flight.load(Ordering::Relaxed); + let shown = self.shown.fetch_max(value, Ordering::Relaxed).max(value); + self.events.progress( + shown.min(self.total), + self.total, + self.base_files + self.files_placed.load(Ordering::Relaxed) as u32, + self.total_files, + ); + } + + fn open(&self, job: &Planned) -> Result { + let dest = self.game_dir.join(&job.path); + if let Some(parent) = dest.parent() { + fs::create_dir_all(parent).map_err(|e| format!("Could not create the folder for {}: {e}", job.path))?; + } + let tmp = temp_path(&dest); + let file = File::create(&tmp).map_err(|e| format!("Could not write {}: {e}", job.path))?; + Ok(Slot { + header: Vec::new(), + payload_from: None, + consumed: 0, + file, + tmp, + crc: Crc::new(), + written: 0, + inflate: (job.entry.method == zip::DEFLATED).then(|| Decompress::new(false)), + buffer: Vec::new(), + }) + } + + fn write_out(&self, job: &Planned, slot: &mut Slot, data: &[u8]) -> Result<(), SinkError> { + if slot.written + data.len() as u64 > job.entry.size { + return Err(SinkError::Retry(format!("{}: more data than the archive lists", job.path))); + } + slot.file + .write_all(data) + .map_err(|e| SinkError::Fatal(self.fail(format!("Could not write {}: {e}", job.path))))?; + slot.crc.update(data); + slot.written += data.len() as u64; + self.bytes_written.fetch_add(data.len() as u64, Ordering::Relaxed); + Ok(()) + } + + fn feed(&self, job: &Planned, slot: &mut Slot, payload: &[u8], finish: bool) -> Result<(), SinkError> { + let Some(mut inflate) = slot.inflate.take() else { + return self.write_out(job, slot, payload); + }; + let mut out = std::mem::take(&mut slot.buffer); + out.resize(INFLATE_BUFFER, 0); + let result = self.inflate_into(job, slot, &mut inflate, &mut out, payload, finish); + slot.inflate = Some(inflate); + slot.buffer = out; + result + } + + fn inflate_into(&self, job: &Planned, slot: &mut Slot, inflate: &mut Decompress, out: &mut [u8], mut input: &[u8], finish: bool) -> Result<(), SinkError> { + let mode = if finish { FlushDecompress::Finish } else { FlushDecompress::None }; + loop { + let (in_before, out_before) = (inflate.total_in(), inflate.total_out()); + let status = inflate + .decompress(input, out, mode) + .map_err(|e| SinkError::Retry(format!("{}: the compressed data is damaged ({e})", job.path)))?; + let used = (inflate.total_in() - in_before) as usize; + let produced = (inflate.total_out() - out_before) as usize; + input = &input[used..]; + if produced > 0 { + self.write_out(job, slot, &out[..produced])?; + } + if status == Status::StreamEnd || (used == 0 && produced == 0) { + return Ok(()); + } + if input.is_empty() && !finish && produced < out.len() { + return Ok(()); + } + } + } + + fn piece(&self, i: usize, offset: u64, data: &[u8]) -> Result<(), SinkError> { + let job = self.jobs[i]; + let mut guard = self.slots[i].lock().expect("slot lock"); + if offset == 0 { + if let Some(old) = guard.take() { + self.in_flight.fetch_sub(old.consumed, Ordering::Relaxed); + discard(old); + } + *guard = Some(self.open(job).map_err(|e| SinkError::Fatal(self.fail(e)))?); + } + let Some(slot) = guard.as_mut() else { + return Err(SinkError::Retry(format!("{}: data arrived before the start of the record", job.path))); + }; + self.consume(job, slot, data) + } + + fn consume(&self, job: &Planned, slot: &mut Slot, data: &[u8]) -> Result<(), SinkError> { + if slot.payload_from.is_none() { + slot.header.extend_from_slice(data); + match zip::local_header_len(&slot.header) { + None => return Ok(()), + Some(Err(e)) => return Err(SinkError::Retry(format!("{}: {e}", job.path))), + Some(Ok(len)) if slot.header.len() < len => return Ok(()), + Some(Ok(len)) => { + slot.payload_from = Some(len); + let header = std::mem::take(&mut slot.header); + let rest = header[len..].to_vec(); + return self.payload(job, slot, &rest); + } + } + } + self.payload(job, slot, data) + } + + fn payload(&self, job: &Planned, slot: &mut Slot, data: &[u8]) -> Result<(), SinkError> { + let left = job.entry.compressed.saturating_sub(slot.consumed); + let take = &data[..(left.min(data.len() as u64) as usize)]; + if take.is_empty() { + return Ok(()); + } + slot.consumed += take.len() as u64; + self.in_flight.fetch_add(take.len() as u64, Ordering::Relaxed); + self.feed(job, slot, take, false)?; + self.emit(false); + Ok(()) + } + + fn finish(&self, i: usize) -> Result { + let job = self.jobs[i]; + let taken = self.slots[i].lock().expect("slot lock").take(); + let Some(slot) = taken else { + return Err(SinkError::Retry(format!("{}: the record arrived empty", job.path))); + }; + self.complete(i, slot) + } + + fn complete(&self, i: usize, mut slot: Slot) -> Result { + let job = self.jobs[i]; + self.in_flight.fetch_sub(slot.consumed, Ordering::Relaxed); + if slot.consumed != job.entry.compressed { + let got = slot.consumed; + discard(slot); + return Err(SinkError::Retry(format!("{}: {got} of {} compressed bytes arrived", job.path, job.entry.compressed))); + } + if slot.inflate.is_some() { + if let Err(e) = self.feed(job, &mut slot, &[], true) { + discard(slot); + return Err(e); + } + } + if slot.written != job.entry.size || slot.crc.sum() != job.entry.crc32 { + let got = slot.written; + discard(slot); + return Err(SinkError::Retry(format!("{}: {got} bytes inflated with a CRC mismatch", job.path))); + } + let Slot { file, tmp, .. } = slot; + if let Err(e) = file.sync_all() { + drop(file); + let _ = fs::remove_file(&tmp); + return Err(SinkError::Fatal(self.fail(format!("Could not write {}: {e}", job.path)))); + } + drop(file); + if self.stop.load(Ordering::SeqCst) { + let _ = fs::remove_file(&tmp); + return Ok(0); + } + if !self.gate.allow(self.events) { + let _ = fs::remove_file(&tmp); + return Err(SinkError::Fatal(self.fail(REPLACE_REFUSED.into()))); + } + let dest = self.game_dir.join(&job.path); + if let Err(e) = fs::rename(&tmp, &dest) { + let _ = fs::remove_file(&tmp); + return Err(SinkError::Fatal(self.fail(format!("Could not put {} in place: {e}", job.path)))); + } + self.record(job, &dest); + self.placed[i].store(true, Ordering::SeqCst); + self.placed_bytes.fetch_add(job.entry.compressed, Ordering::Relaxed); + self.files_placed.fetch_add(1, Ordering::Relaxed); + self.emit(true); + Ok(0) + } + + fn part_chunk(&self, i: usize, part: usize, start: u64, offset: u64, data: &[u8]) -> Result<(), SinkError> { + let job = self.jobs[i]; + let Some(split) = self.splits[i].as_ref() else { + return Err(SinkError::Fatal(format!("{}: no split state", job.path))); + }; + let mut file = split.file.lock().expect("split lock"); + if file.is_none() { + let dest = self.game_dir.join(&job.path); + if let Some(parent) = dest.parent() { + fs::create_dir_all(parent).map_err(|e| SinkError::Fatal(self.fail(format!("Could not create the folder for {}: {e}", job.path))))?; + } + *file = Some(File::create(&split.tmp).map_err(|e| SinkError::Fatal(self.fail(format!("Could not write {}: {e}", job.path))))?); + } + let handle = file.as_ref().expect("split file"); + if offset == 0 { + let old = split.received[part].swap(0, Ordering::Relaxed); + self.in_flight.fetch_sub(old, Ordering::Relaxed); + } + handle + .write_all_at(data, start + offset) + .map_err(|e| SinkError::Fatal(self.fail(format!("Could not write {}: {e}", job.path))))?; + drop(file); + split.received[part].fetch_add(data.len() as u64, Ordering::Relaxed); + self.in_flight.fetch_add(data.len() as u64, Ordering::Relaxed); + self.emit(false); + Ok(()) + } + + fn part_finish(&self, i: usize, part: usize, len: u64) -> Result { + let job = self.jobs[i]; + let Some(split) = self.splits[i].as_ref() else { + return Err(SinkError::Fatal(format!("{}: no split state", job.path))); + }; + let got = split.received[part].load(Ordering::Relaxed); + if got != len { + return Err(SinkError::Retry(format!("{}: part {part} got {got} of {len} bytes", job.path))); + } + if split.left.fetch_sub(1, Ordering::SeqCst) != 1 { + return Ok(0); + } + let received: u64 = split.received.iter().map(|r| r.swap(0, Ordering::Relaxed)).sum(); + self.in_flight.fetch_sub(received, Ordering::Relaxed); + let result = self.assemble(i, split); + let _ = fs::remove_file(&split.tmp); + *split.file.lock().expect("split lock") = None; + result.map_err(|e| match e { + SinkError::Retry(m) => SinkError::Fatal(self.fail(m)), + fatal => fatal, + }) + } + + fn assemble(&self, i: usize, split: &Split) -> Result { + let job = self.jobs[i]; + let mut source = File::open(&split.tmp).map_err(|e| SinkError::Fatal(self.fail(format!("Could not read {}: {e}", job.path))))?; + let mut slot = self.open(job).map_err(|e| SinkError::Fatal(self.fail(e)))?; + let mut buf = vec![0u8; 1024 * 1024]; + loop { + if self.stop.load(Ordering::Relaxed) { + self.in_flight.fetch_sub(slot.consumed, Ordering::Relaxed); + discard(slot); + return Ok(0); + } + let n = match source.read(&mut buf) { + Ok(0) => break, + Ok(n) => n, + Err(e) if e.kind() == std::io::ErrorKind::Interrupted => continue, + Err(e) => { + self.in_flight.fetch_sub(slot.consumed, Ordering::Relaxed); + discard(slot); + return Err(SinkError::Fatal(self.fail(format!("Could not read {}: {e}", job.path)))); + } + }; + if let Err(e) = self.consume(job, &mut slot, &buf[..n]) { + self.in_flight.fetch_sub(slot.consumed, Ordering::Relaxed); + discard(slot); + return Err(e); + } + if slot.consumed >= job.entry.compressed { + break; + } + } + self.complete(i, slot) + } + + fn record(&self, job: &Planned, dest: &Path) { + let known = Known { crc: job.entry.crc32, size: job.entry.size, modified: modified_ms(dest) }; + if let Some(file) = self.ledger.lock().expect("ledger lock").as_mut() { + let _ = file.write_all(ledger_line(&job.path, known).as_bytes()); + } + } +} + +struct Sink<'p, 'a>(&'p Placer<'a>); + +impl FetchSink for Sink<'_, '_> { + fn process(&self, item: &FetchItem, _body: Vec) -> Result { + Err(SinkError::Fatal(format!("item {} arrived whole; streaming was expected", item.id))) + } + + fn on_chunk(&self, item: &FetchItem, offset: u64, data: &[u8]) -> Result<(), SinkError> { + match self.0.works[item.id as usize] { + Work::Whole(i) => self.0.piece(i, offset, data), + Work::Part { job, part, start, .. } => self.0.part_chunk(job, part, start, offset, data), + } + } + + fn on_finish(&self, item: &FetchItem, _total_len: u64) -> Result { + match self.0.works[item.id as usize] { + Work::Whole(i) => self.0.finish(i), + Work::Part { job, part, len, .. } => self.0.part_finish(job, part, len), + } + } +} + +fn origin(url: &str) -> String { + let after = url.find("://").map(|i| i + 3).unwrap_or(0); + let end = url[after..].find('/').map(|i| after + i).unwrap_or(url.len()); + url[..end].to_string() +} + +fn cancelled(cancel: &AtomicBool) -> Result<(), Stop> { + if cancel.load(Ordering::SeqCst) { + Err(Stop::Cancelled) + } else { + Ok(()) + } +} + +pub fn run(req: &Request, cancel: &AtomicBool, ev: &dyn Events) -> Result { + let failed = Stop::Failed; + let started = Instant::now(); + fs::create_dir_all(&req.game_dir).map_err(|e| failed(format!("Could not create the install folder: {e}")))?; + let _ = File::create(req.game_dir.join(IN_PROGRESS_MARKER)); + + ev.status("Reading the build…"); + let archive = net::read_archive(&req.url).map_err(|e| { + ev.log(&format!("archive: {e}")); + failed(format!("The EA build could not be read: {e}")) + })?; + cancelled(cancel)?; + let planned = plan(&archive.entries, archive.directory.offset).map_err(failed)?; + let total_size: u64 = planned.iter().map(|p| p.entry.size).sum(); + let total_compressed: u64 = planned.iter().map(|p| p.entry.compressed).sum(); + ev.log(&format!( + "archive bytes={} entries={} files={} size={total_size} compressed={total_compressed}", + archive.total, + archive.entries.len(), + planned.len() + )); + + for p in &planned { + let dest = req.game_dir.join(&p.path); + let _ = fs::remove_file(temp_path(&dest)); + let mut split = dest.into_os_string(); + split.push(SPLIT_SUFFIX); + let _ = fs::remove_file(split); + } + + ev.status("Checking files…"); + let mut ledger = read_ledger(&req.game_dir); + let mut pending: Vec<&Planned> = Vec::new(); + let mut kept: Vec<(String, Known)> = Vec::new(); + let mut checked = 0u64; + let mut last_report = Instant::now(); + for (n, p) in planned.iter().enumerate() { + let dest = req.game_dir.join(&p.path); + let size_ok = fs::symlink_metadata(&dest).map(|m| m.is_file() && m.len() == p.entry.size).unwrap_or(false); + let mut complete = false; + if size_ok { + let modified = modified_ms(&dest); + let known = ledger.get(&p.path).copied(); + if known == Some(Known { crc: p.entry.crc32, size: p.entry.size, modified }) { + complete = true; + } else { + match crc_of(&dest, cancel) { + Err(()) => return Err(Stop::Cancelled), + Ok(Some(crc)) if crc == p.entry.crc32 => { + complete = true; + kept.push((p.path.clone(), Known { crc, size: p.entry.size, modified })); + } + Ok(_) => {} + } + } + } + if !complete { + pending.push(p); + } + checked += p.entry.size; + if last_report.elapsed() >= CHECK_REPORT_EVERY || n + 1 == planned.len() { + ev.checking(checked, total_size, (n + 1) as u32, planned.len() as u32); + last_report = Instant::now(); + } + cancelled(cancel)?; + } + for (path, known) in kept { + ledger.insert(path, known); + } + + let pending_size: u64 = pending.iter().map(|p| p.entry.size).sum(); + let pending_compressed: u64 = pending.iter().map(|p| p.entry.compressed).sum(); + ev.log(&format!( + "resume kept_files={} pending_files={} pending_bytes={pending_size} pending_download={pending_compressed}", + planned.len() - pending.len(), + pending.len() + )); + ev.sizes(pending_size, total_size); + if let Some(message) = space_problem(pending_size, free_bytes(&req.game_dir)) { + return Err(failed(message)); + } + + let gate = ReplaceGate::new(); + let (empty, remote): (Vec<&Planned>, Vec<&Planned>) = pending.into_iter().partition(|p| p.entry.size == 0 && p.entry.crc32 == 0); + for p in &empty { + let dest = req.game_dir.join(&p.path); + if let Some(parent) = dest.parent() { + fs::create_dir_all(parent).map_err(|e| failed(format!("Could not create the folder for {}: {e}", p.path)))?; + } + if !gate.allow(ev) { + return Err(failed(REPLACE_REFUSED.into())); + } + File::create(&dest).map_err(|e| failed(format!("Could not write {}: {e}", p.path)))?; + ledger.insert(p.path.clone(), Known { crc: 0, size: 0, modified: modified_ms(&dest) }); + } + + let mut text = String::new(); + for (path, known) in &ledger { + text.push_str(&ledger_line(path, *known)); + } + fs::write(req.game_dir.join(LEDGER), text).map_err(|e| failed(format!("Could not write the install record: {e}")))?; + let ledger_file = OpenOptions::new().append(true).open(req.game_dir.join(LEDGER)).ok(); + + let host = origin(&req.url); + let works = works(&remote, req.part_bytes); + let splits: Vec> = remote + .iter() + .enumerate() + .map(|(i, p)| { + let parts = works.iter().filter(|w| matches!(w, Work::Part { job, .. } if *job == i)).count(); + (parts > 0).then(|| { + let mut name = req.game_dir.join(&p.path).into_os_string(); + name.push(SPLIT_SUFFIX); + Split { + tmp: PathBuf::from(name), + file: Mutex::new(None), + received: (0..parts).map(|_| AtomicU64::new(0)).collect(), + left: AtomicUsize::new(parts), + } + }) + }) + .collect(); + let placer = Placer { + jobs: &remote, + game_dir: &req.game_dir, + slots: remote.iter().map(|_| Mutex::new(None)).collect(), + splits, + works: works.clone(), + placed: remote.iter().map(|_| AtomicBool::new(false)).collect(), + ledger: Mutex::new(ledger_file), + events: ev, + gate: &gate, + stop: cancel, + failure: Mutex::new(None), + base_done: total_compressed - pending_compressed, + base_files: (planned.len() - remote.len()) as u32, + total: total_compressed, + total_files: planned.len() as u32, + placed_bytes: AtomicU64::new(0), + in_flight: AtomicU64::new(0), + shown: AtomicU64::new(0), + bytes_written: AtomicU64::new(0), + files_placed: AtomicUsize::new(0), + last_emit: AtomicU64::new(0), + started: Instant::now(), + }; + let cores = std::thread::available_parallelism().map(|n| n.get()).unwrap_or(2); + let window = req.max_workers.clamp(1, 64); + let hosts = vec![host.clone()]; + let options = FetchOptions { + max_workers: window, + per_host_cap: window, + timeout: REQUEST_TIMEOUT, + headers: vec![("User-Agent".to_string(), net::user_agent())], + ca_bundle_path: req.ca_bundle.clone(), + process_workers: req.process_workers.max(cores / 2).max(1), + label: "ea".to_string(), + stream: true, + }; + let items: Vec = works + .iter() + .enumerate() + .map(|(n, w)| { + let range = match *w { + Work::Whole(i) => (remote[i].entry.local_offset, remote[i].range_end), + Work::Part { job, start, len, .. } => { + let at = remote[job].entry.local_offset + start; + (at, at + len - 1) + } + }; + FetchItem { id: n as u64, urls: vec![req.url.clone()], reserve: 0, range: Some(range) } + }) + .collect(); + ev.status("Downloading…"); + ev.log(&format!("engine files={} requests={} window={window} hosts={}", remote.len(), items.len(), distinct_host_count(&hosts))); + placer.emit(true); + let log = |line: &str| ev.log(line); + let outcome = if items.is_empty() { + None + } else { + Some(run_fetch(items, &hosts, &options, &Sink(&placer), cancel, &|_: u64, _: u64| {}, &log)) + }; + for slot in &placer.slots { + if let Some(active) = slot.lock().expect("slot lock").take() { + discard(active); + } + } + for split in placer.splits.iter().flatten() { + *split.file.lock().expect("split lock") = None; + let _ = fs::remove_file(&split.tmp); + } + placer.emit(true); + let placed_all = placer.placed.iter().all(|p| p.load(Ordering::SeqCst)); + let bytes_written = placer.bytes_written.load(Ordering::Relaxed); + let files_written = placer.files_placed.load(Ordering::Relaxed) + empty.len(); + let elapsed = started.elapsed().as_secs_f64().max(0.001); + ev.log(&format!( + "summary files_written={files_written} bytes_written={bytes_written} elapsed_s={elapsed:.1} avg_mbps={:.1}", + bytes_written as f64 / 1_048_576.0 / elapsed + )); + if let Some(message) = placer.failure.lock().expect("failure lock").take() { + return Err(Stop::Failed(message)); + } + if !placed_all { + if let Some(error) = outcome.as_ref().and_then(|o| o.error.clone()) { + ev.log(&format!("download failed: {error}")); + return Err(Stop::Failed("A file failed to download".into())); + } + if cancel.load(Ordering::SeqCst) { + return Err(Stop::Cancelled); + } + return Err(Stop::Failed("The download ended before every file was written".into())); + } + + let wanted: HashSet<&str> = planned.iter().map(|p| p.path.as_str()).collect(); + let by_case: HashMap = planned.iter().map(|p| (p.path.to_lowercase(), p.path.as_str())).collect(); + let mut removed = 0; + let current = read_ledger(&req.game_dir); + let mut text = String::new(); + for (path, known) in ¤t { + if wanted.contains(path.as_str()) { + text.push_str(&ledger_line(path, *known)); + } else if by_case.get(&path.to_lowercase()).is_some_and(|kept| same_file(&req.game_dir.join(kept), &req.game_dir.join(path))) { + continue; + } else if zip::safe_path(path).as_deref() == Some(path.as_str()) && fs::remove_file(req.game_dir.join(path)).is_ok() { + removed += 1; + } + } + let _ = fs::write(req.game_dir.join(LEDGER), text); + if removed > 0 { + ev.log(&format!("removed {removed} files the new build no longer has")); + } + let _ = fs::remove_file(req.game_dir.join(IN_PROGRESS_MARKER)); + Ok(Finished { + total_bytes: total_size, + download_bytes: total_compressed, + file_count: planned.len(), + files_written, + bytes_written, + removed, + }) +} + +pub fn size(url: &str) -> Result { + let archive = net::read_archive(url)?; + let planned = plan(&archive.entries, archive.directory.offset)?; + Ok(json!({ + "totalBytes": planned.iter().map(|p| p.entry.size).sum::(), + "downloadBytes": planned.iter().map(|p| p.entry.compressed).sum::(), + "fileCount": planned.len(), + })) +} + +#[cfg(test)] +mod tests; diff --git a/app/src/main/rust/stores/src/ea/install/tests.rs b/app/src/main/rust/stores/src/ea/install/tests.rs new file mode 100644 index 0000000000..baf15a4345 --- /dev/null +++ b/app/src/main/rust/stores/src/ea/install/tests.rs @@ -0,0 +1,258 @@ +use std::io::{BufRead, BufReader, Write}; +use std::net::{TcpListener, TcpStream}; +use std::sync::atomic::{AtomicBool, AtomicUsize}; +use std::sync::{Arc, Mutex}; + +use flate2::write::DeflateEncoder; +use flate2::Compression; + +use super::*; + +struct Built { + bytes: Vec, +} + +fn build_zip(files: &[(&str, Vec, bool)]) -> Built { + let mut out = Vec::new(); + let mut central = Vec::new(); + for (name, data, deflate) in files { + let mut crc = Crc::new(); + crc.update(data); + let body = if *deflate { + let mut enc = DeflateEncoder::new(Vec::new(), Compression::default()); + enc.write_all(data).unwrap(); + enc.finish().unwrap() + } else { + data.clone() + }; + let method: u16 = if *deflate { 8 } else { 0 }; + let offset = out.len() as u32; + let extra = [0xAAu8, 0xBB, 2, 0, 1, 2]; + out.extend_from_slice(&0x0403_4b50u32.to_le_bytes()); + out.extend_from_slice(&[20, 0, 0, 0]); + out.extend_from_slice(&method.to_le_bytes()); + out.extend_from_slice(&[0, 0, 0, 0]); + out.extend_from_slice(&crc.sum().to_le_bytes()); + out.extend_from_slice(&(body.len() as u32).to_le_bytes()); + out.extend_from_slice(&(data.len() as u32).to_le_bytes()); + out.extend_from_slice(&(name.len() as u16).to_le_bytes()); + out.extend_from_slice(&(extra.len() as u16).to_le_bytes()); + out.extend_from_slice(name.as_bytes()); + out.extend_from_slice(&extra); + out.extend_from_slice(&body); + + central.extend_from_slice(&0x0201_4b50u32.to_le_bytes()); + central.extend_from_slice(&[20, 0, 20, 0, 0, 0]); + central.extend_from_slice(&method.to_le_bytes()); + central.extend_from_slice(&[0, 0, 0, 0]); + central.extend_from_slice(&crc.sum().to_le_bytes()); + central.extend_from_slice(&(body.len() as u32).to_le_bytes()); + central.extend_from_slice(&(data.len() as u32).to_le_bytes()); + central.extend_from_slice(&(name.len() as u16).to_le_bytes()); + central.extend_from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]); + central.extend_from_slice(&offset.to_le_bytes()); + central.extend_from_slice(name.as_bytes()); + } + let cd_offset = out.len() as u32; + let count = files.len() as u16; + out.extend_from_slice(¢ral); + out.extend_from_slice(&0x0605_4b50u32.to_le_bytes()); + out.extend_from_slice(&[0, 0, 0, 0]); + out.extend_from_slice(&count.to_le_bytes()); + out.extend_from_slice(&count.to_le_bytes()); + out.extend_from_slice(&(central.len() as u32).to_le_bytes()); + out.extend_from_slice(&cd_offset.to_le_bytes()); + out.extend_from_slice(&[0, 0]); + Built { bytes: out } +} + +struct Server { + url: String, + requests: Arc, +} + +fn serve(body: Vec) -> Server { + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let addr = listener.local_addr().unwrap(); + let body = Arc::new(body); + let requests = Arc::new(AtomicUsize::new(0)); + let counter = Arc::clone(&requests); + std::thread::spawn(move || { + for stream in listener.incoming() { + let Ok(stream) = stream else { continue }; + let body = Arc::clone(&body); + counter.fetch_add(1, Ordering::SeqCst); + std::thread::spawn(move || answer(stream, &body)); + } + }); + Server { url: format!("http://{addr}/build/game.zip?sig=abc"), requests } +} + +fn answer(mut stream: TcpStream, body: &[u8]) { + let mut reader = BufReader::new(stream.try_clone().unwrap()); + let mut first = String::new(); + if reader.read_line(&mut first).is_err() { + return; + } + let mut range = None; + loop { + let mut line = String::new(); + match reader.read_line(&mut line) { + Ok(0) | Err(_) => break, + Ok(_) if line == "\r\n" => break, + Ok(_) => { + if let Some(v) = line.to_ascii_lowercase().strip_prefix("range: bytes=") { + range = Some(v.trim().to_string()); + } + } + } + } + let total = body.len() as u64; + let Some(range) = range else { + let _ = stream.write_all(format!("HTTP/1.1 200 OK\r\nContent-Length: {total}\r\nConnection: close\r\n\r\n").as_bytes()); + let _ = stream.write_all(body); + return; + }; + let (a, b) = range.split_once('-').unwrap(); + let (start, end) = if a.is_empty() { + let n: u64 = b.parse().unwrap(); + (total.saturating_sub(n), total - 1) + } else { + let start: u64 = a.parse().unwrap(); + let end = if b.is_empty() { total - 1 } else { b.parse::().unwrap().min(total - 1) }; + (start, end) + }; + let slice = &body[start as usize..=end as usize]; + let head = format!( + "HTTP/1.1 206 Partial Content\r\nContent-Length: {}\r\nContent-Range: bytes {start}-{end}/{total}\r\nConnection: close\r\n\r\n", + slice.len() + ); + let _ = stream.write_all(head.as_bytes()); + let _ = stream.write_all(slice); +} + +#[derive(Default)] +struct Recorder { + logs: Mutex>, + gates: AtomicUsize, + last: Mutex<(u64, u64)>, +} + +impl Events for Recorder { + fn log(&self, line: &str) { + self.logs.lock().unwrap().push(line.to_string()); + } + fn status(&self, _text: &str) {} + fn checking(&self, _d: u64, _t: u64, _f: u32, _ft: u32) {} + fn sizes(&self, _pending: u64, _total: u64) {} + fn progress(&self, done: u64, total: u64, _files: u32, _files_total: u32) { + *self.last.lock().unwrap() = (done, total); + } + fn before_replace(&self) -> bool { + self.gates.fetch_add(1, Ordering::SeqCst); + true + } +} + +fn sample() -> Vec<(&'static str, Vec, bool)> { + let mut seed = 0x1234_5678u32; + let big: Vec = (0..900_000u32).map(|_| { seed = seed.wrapping_mul(1_103_515_245).wrapping_add(12_345); (seed >> 16) as u8 }).collect(); + vec![ + ("__Installer/installerdata.xml", b"".to_vec(), true), + ("Game.exe", big, true), + ("Data\\stored.bin", vec![9u8; 4096], false), + ("Data/", Vec::new(), false), + ("empty.txt", Vec::new(), false), + ] +} + +fn request(url: &str, dir: &Path) -> Request { + Request { url: url.to_string(), game_dir: dir.to_path_buf(), max_workers: 4, process_workers: 2, ca_bundle: String::new(), part_bytes: 64 * 1024 } +} + +fn temp_dir(name: &str) -> PathBuf { + let dir = std::env::temp_dir().join(format!("droiddeck-ea-{name}-{}", std::process::id())); + let _ = fs::remove_dir_all(&dir); + fs::create_dir_all(&dir).unwrap(); + dir +} + +#[test] +fn installs_every_file_and_keeps_them_on_the_next_run() { + let files = sample(); + let zip = build_zip(&files); + let server = serve(zip.bytes); + let dir = temp_dir("install"); + let ev = Recorder::default(); + let done = run(&request(&server.url, &dir), &AtomicBool::new(false), &ev).unwrap(); + assert_eq!(done.file_count, 4); + assert_eq!(fs::read(dir.join("Game.exe")).unwrap(), files[1].1); + assert_eq!(fs::read(dir.join("Data/stored.bin")).unwrap(), vec![9u8; 4096]); + assert!(dir.join("empty.txt").is_file()); + assert!(dir.join("__Installer/installerdata.xml").is_file()); + assert!(!dir.join(IN_PROGRESS_MARKER).exists()); + let (shown, total) = *ev.last.lock().unwrap(); + assert_eq!(shown, total); + + let before = server.requests.load(Ordering::SeqCst); + let again = run(&request(&server.url, &dir), &AtomicBool::new(false), &Recorder::default()).unwrap(); + assert_eq!(again.files_written, 0); + assert!(server.requests.load(Ordering::SeqCst) - before <= 2); + let _ = fs::remove_dir_all(&dir); +} + +#[test] +fn an_update_replaces_changed_files_and_drops_removed_ones() { + let dir = temp_dir("update"); + let first = serve(build_zip(&sample()).bytes); + run(&request(&first.url, &dir), &AtomicBool::new(false), &Recorder::default()).unwrap(); + + let mut next = sample(); + next[1].1[10] ^= 0xFF; + next.retain(|f| f.0 != "Data\\stored.bin"); + let second = serve(build_zip(&next).bytes); + let ev = Recorder::default(); + let done = run(&request(&second.url, &dir), &AtomicBool::new(false), &ev).unwrap(); + assert_eq!(done.files_written, 1); + assert_eq!(done.removed, 1); + assert_eq!(fs::read(dir.join("Game.exe")).unwrap(), next[1].1); + assert!(!dir.join("Data/stored.bin").exists()); + assert_eq!(ev.gates.load(Ordering::SeqCst), 1); + let _ = fs::remove_dir_all(&dir); +} + +#[test] +fn a_damaged_file_on_disk_is_downloaded_again() { + let dir = temp_dir("repair"); + let server = serve(build_zip(&sample()).bytes); + run(&request(&server.url, &dir), &AtomicBool::new(false), &Recorder::default()).unwrap(); + let mut data = fs::read(dir.join("Game.exe")).unwrap(); + data[5] ^= 1; + fs::write(dir.join("Game.exe"), &data).unwrap(); + let done = run(&request(&server.url, &dir), &AtomicBool::new(false), &Recorder::default()).unwrap(); + assert_eq!(done.files_written, 1); + assert_eq!(fs::read(dir.join("Game.exe")).unwrap(), sample()[1].1); + let _ = fs::remove_dir_all(&dir); +} + +#[test] +fn reports_the_archive_size() { + let server = serve(build_zip(&sample()).bytes); + let answer = size(&server.url).unwrap(); + assert_eq!(answer["fileCount"], 4); + let expected: usize = sample().iter().map(|f| f.1.len()).sum(); + assert_eq!(answer["totalBytes"], expected as u64); +} + +#[test] +fn large_records_arrive_in_parallel_parts() { + let dir = temp_dir("parts"); + let server = serve(build_zip(&sample()).bytes); + let before = server.requests.load(Ordering::SeqCst); + let done = run(&request(&server.url, &dir), &AtomicBool::new(false), &Recorder::default()).unwrap(); + assert_eq!(done.files_written, 4); + assert!(server.requests.load(Ordering::SeqCst) - before > 6); + assert_eq!(fs::read(dir.join("Game.exe")).unwrap(), sample()[1].1); + assert!(fs::read_dir(&dir).unwrap().all(|e| !e.unwrap().file_name().to_string_lossy().ends_with(".ddzip"))); + let _ = fs::remove_dir_all(&dir); +} diff --git a/app/src/main/rust/stores/src/ea/mod.rs b/app/src/main/rust/stores/src/ea/mod.rs new file mode 100644 index 0000000000..73ccb48ad4 --- /dev/null +++ b/app/src/main/rust/stores/src/ea/mod.rs @@ -0,0 +1,4 @@ +pub mod ffi; +pub mod install; +pub mod net; +pub mod zip; diff --git a/app/src/main/rust/stores/src/ea/net.rs b/app/src/main/rust/stores/src/ea/net.rs new file mode 100644 index 0000000000..8b31895472 --- /dev/null +++ b/app/src/main/rust/stores/src/ea/net.rs @@ -0,0 +1,98 @@ +// SPDX-License-Identifier: GPL-3.0-or-later +//! Ranged reads of EA's build archives. + +use std::time::Duration; + +use super::zip::{self, Directory, Entry}; + +const CONNECT_TIMEOUT: Duration = Duration::from_secs(30); +const READ_TIMEOUT: Duration = Duration::from_secs(60); +const MAX_DIRECTORY: u64 = 512 * 1024 * 1024; + +pub fn user_agent() -> String { + crate::amazon::net::user_agent() +} + +pub struct Ranged { + pub body: Vec, + pub start: u64, + pub total: u64, +} + +fn content_range(value: &str) -> Option<(u64, u64)> { + let rest = value.trim().strip_prefix("bytes")?.trim_start(); + let (span, total) = rest.split_once('/')?; + let start = span.split_once('-')?.0.trim().parse().ok()?; + Some((start, total.trim().parse().ok()?)) +} + +pub fn get_range(url: &str, range: &str) -> Result { + let runtime = tokio::runtime::Builder::new_current_thread() + .enable_all() + .build() + .map_err(|e| format!("runtime: {e}"))?; + runtime.block_on(async { + let client = reqwest::Client::builder() + .user_agent(user_agent()) + .connect_timeout(CONNECT_TIMEOUT) + .read_timeout(READ_TIMEOUT) + .build() + .map_err(|e| format!("http client: {}", e.without_url()))?; + let response = client + .get(url) + .header(reqwest::header::RANGE, format!("bytes={range}")) + .send() + .await + .map_err(|e| format!("request failed: {}", e.without_url()))?; + let status = response.status().as_u16(); + if status == 403 || status == 404 { + return Err(format!("the download link was refused ({status})")); + } + if status != 206 { + return Err(format!("the server did not answer a ranged read ({status})")); + } + let (start, total) = response + .headers() + .get(reqwest::header::CONTENT_RANGE) + .and_then(|v| v.to_str().ok()) + .and_then(content_range) + .ok_or("the server gave no content range")?; + let body = response.bytes().await.map_err(|e| format!("answer could not be read: {}", e.without_url()))?.to_vec(); + Ok(Ranged { body, start, total }) + }) +} + +pub struct Archive { + pub total: u64, + pub directory: Directory, + pub entries: Vec, +} + +pub fn read_archive(url: &str) -> Result { + let tail = get_range(url, &format!("-{}", zip::TAIL_LEN))?; + let directory = zip::locate(&tail.body, tail.start)?; + if directory.size > MAX_DIRECTORY || directory.offset + directory.size > tail.total { + return Err("The archive directory is out of bounds".into()); + } + let bytes = if directory.offset >= tail.start { + let at = (directory.offset - tail.start) as usize; + let end = at + directory.size as usize; + tail.body.get(at..end).ok_or("The archive directory is cut short")?.to_vec() + } else { + let end = directory.offset + directory.size - 1; + get_range(url, &format!("{}-{end}", directory.offset))?.body + }; + let entries = zip::parse(&bytes, directory.entries)?; + Ok(Archive { total: tail.total, directory, entries }) +} + +#[cfg(test)] +mod tests { + use super::content_range; + + #[test] + fn reads_content_range() { + assert_eq!(content_range("bytes 100-199/5000"), Some((100, 5000))); + assert_eq!(content_range("bytes */5000"), None); + } +} diff --git a/app/src/main/rust/stores/src/ea/zip.rs b/app/src/main/rust/stores/src/ea/zip.rs new file mode 100644 index 0000000000..d7a8203e73 --- /dev/null +++ b/app/src/main/rust/stores/src/ea/zip.rs @@ -0,0 +1,288 @@ +// SPDX-License-Identifier: GPL-3.0-or-later +//! The central directory of a remote ZIP archive, which is how EA publishes a game build. + +const EOCD: u32 = 0x0605_4b50; +const EOCD64: u32 = 0x0606_4b50; +const EOCD64_LOCATOR: u32 = 0x0706_4b50; +const CENTRAL: u32 = 0x0201_4b50; +const LOCAL: u32 = 0x0403_4b50; +const EOCD_LEN: usize = 22; +const LOCATOR_LEN: usize = 20; +const EOCD64_LEN: usize = 56; +pub const LOCAL_FIXED: usize = 30; +pub const TAIL_LEN: u64 = 128 * 1024; + +pub const STORED: u16 = 0; +pub const DEFLATED: u16 = 8; + +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Entry { + pub name: String, + pub crc32: u32, + pub method: u16, + pub compressed: u64, + pub size: u64, + pub local_offset: u64, +} + +impl Entry { + pub fn is_dir(&self) -> bool { + self.name.ends_with('/') || self.name.ends_with('\\') + } +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Directory { + pub offset: u64, + pub size: u64, + pub entries: u64, +} + +fn u16_at(b: &[u8], at: usize) -> Option { + b.get(at..at + 2).map(|s| u16::from_le_bytes([s[0], s[1]])) +} + +fn u32_at(b: &[u8], at: usize) -> Option { + b.get(at..at + 4).map(|s| u32::from_le_bytes([s[0], s[1], s[2], s[3]])) +} + +fn u64_at(b: &[u8], at: usize) -> Option { + b.get(at..at + 8).map(|s| u64::from_le_bytes(s.try_into().unwrap_or([0; 8]))) +} + +/// Finds the central directory from the last bytes of the archive. `tail_start` is where `tail` +/// begins in the whole file. +pub fn locate(tail: &[u8], tail_start: u64) -> Result { + if tail.len() < EOCD_LEN { + return Err("The archive is too short to be a ZIP".into()); + } + let mut at = tail.len() - EOCD_LEN; + let found = loop { + if u32_at(tail, at) == Some(EOCD) { + let comment = u16_at(tail, at + 20).unwrap_or(0) as usize; + if at + EOCD_LEN + comment <= tail.len() { + break at; + } + } + if at == 0 { + return Err("The archive has no end of central directory".into()); + } + at -= 1; + }; + let entries = u16_at(tail, found + 10).unwrap_or(0) as u64; + let size = u32_at(tail, found + 12).unwrap_or(0) as u64; + let offset = u32_at(tail, found + 16).unwrap_or(0) as u64; + let wide = entries == 0xFFFF || size == 0xFFFF_FFFF || offset == 0xFFFF_FFFF; + if !wide { + return Ok(Directory { offset, size, entries }); + } + let locator = found.checked_sub(LOCATOR_LEN).filter(|&l| u32_at(tail, l) == Some(EOCD64_LOCATOR)); + let Some(locator) = locator else { + return Err("The archive needs ZIP64 but has no ZIP64 locator".into()); + }; + let record_at = u64_at(tail, locator + 8).unwrap_or(0); + let Some(rel) = record_at.checked_sub(tail_start).map(|r| r as usize) else { + return Err("The ZIP64 directory record lies before the fetched tail".into()); + }; + if u32_at(tail, rel) != Some(EOCD64) || rel + EOCD64_LEN > tail.len() { + return Err("The ZIP64 directory record is damaged".into()); + } + Ok(Directory { + entries: u64_at(tail, rel + 32).unwrap_or(0), + size: u64_at(tail, rel + 40).unwrap_or(0), + offset: u64_at(tail, rel + 48).unwrap_or(0), + }) +} + +fn wide_values(extra: &[u8], need_size: bool, need_compressed: bool, need_offset: bool) -> (Option, Option, Option) { + let mut at = 0; + while at + 4 <= extra.len() { + let id = u16_at(extra, at).unwrap_or(0); + let len = u16_at(extra, at + 2).unwrap_or(0) as usize; + let body = &extra[(at + 4).min(extra.len())..(at + 4 + len).min(extra.len())]; + if id == 0x0001 { + let mut p = 0; + let mut take = |want: bool| { + if !want { + return None; + } + let v = u64_at(body, p); + p += 8; + v + }; + let size = take(need_size); + let compressed = take(need_compressed); + let offset = take(need_offset); + return (size, compressed, offset); + } + at += 4 + len; + } + (None, None, None) +} + +pub fn parse(directory: &[u8], expected: u64) -> Result, String> { + let mut entries = Vec::with_capacity(expected.min(1 << 20) as usize); + let mut at = 0usize; + while at + 46 <= directory.len() { + if u32_at(directory, at) != Some(CENTRAL) { + break; + } + let flags = u16_at(directory, at + 8).unwrap_or(0); + let method = u16_at(directory, at + 10).unwrap_or(0); + let crc32 = u32_at(directory, at + 16).unwrap_or(0); + let compressed32 = u32_at(directory, at + 20).unwrap_or(0); + let size32 = u32_at(directory, at + 24).unwrap_or(0); + let name_len = u16_at(directory, at + 28).unwrap_or(0) as usize; + let extra_len = u16_at(directory, at + 30).unwrap_or(0) as usize; + let comment_len = u16_at(directory, at + 32).unwrap_or(0) as usize; + let offset32 = u32_at(directory, at + 42).unwrap_or(0); + let name_at = at + 46; + let end = name_at + name_len + extra_len + comment_len; + if end > directory.len() { + return Err("The archive directory is cut short".into()); + } + let raw = &directory[name_at..name_at + name_len]; + let name = if flags & 0x0800 != 0 { + String::from_utf8_lossy(raw).into_owned() + } else { + raw.iter().map(|&b| b as char).collect() + }; + let extra = &directory[name_at + name_len..name_at + name_len + extra_len]; + let (size, compressed, offset) = + wide_values(extra, size32 == 0xFFFF_FFFF, compressed32 == 0xFFFF_FFFF, offset32 == 0xFFFF_FFFF); + if flags & 0x0001 != 0 { + return Err(format!("{name} is encrypted")); + } + entries.push(Entry { + name, + crc32, + method, + compressed: compressed.unwrap_or(compressed32 as u64), + size: size.unwrap_or(size32 as u64), + local_offset: offset.unwrap_or(offset32 as u64), + }); + at = end; + } + if expected != 0 && entries.len() as u64 != expected { + return Err(format!("The archive directory lists {} of {expected} entries", entries.len())); + } + Ok(entries) +} + +/// Bytes before the compressed data of an entry, read from its local header. +pub fn local_header_len(header: &[u8]) -> Option> { + if header.len() < LOCAL_FIXED { + return None; + } + if u32_at(header, 0) != Some(LOCAL) { + return Some(Err("An entry has no local header where the directory points".into())); + } + let name = u16_at(header, 26).unwrap_or(0) as usize; + let extra = u16_at(header, 28).unwrap_or(0) as usize; + Some(Ok(LOCAL_FIXED + name + extra)) +} + +/// A relative path inside the install folder, or None for names that would leave it. +pub fn safe_path(name: &str) -> Option { + let unified = name.replace('\\', "/"); + if unified.starts_with('/') || unified.contains(':') || unified.contains('\0') { + return None; + } + let parts: Vec<&str> = unified.split('/').filter(|p| !p.is_empty() && *p != ".").collect(); + if parts.is_empty() || parts.iter().any(|p| *p == "..") { + return None; + } + Some(parts.join("/")) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn central(name: &str, method: u16, crc: u32, compressed: u32, size: u32, offset: u32, extra: &[u8]) -> Vec { + let mut v = Vec::new(); + v.extend_from_slice(&CENTRAL.to_le_bytes()); + v.extend_from_slice(&[20, 0, 20, 0, 0, 0]); + v.extend_from_slice(&method.to_le_bytes()); + v.extend_from_slice(&[0, 0, 0, 0]); + v.extend_from_slice(&crc.to_le_bytes()); + v.extend_from_slice(&compressed.to_le_bytes()); + v.extend_from_slice(&size.to_le_bytes()); + v.extend_from_slice(&(name.len() as u16).to_le_bytes()); + v.extend_from_slice(&(extra.len() as u16).to_le_bytes()); + v.extend_from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]); + v.extend_from_slice(&offset.to_le_bytes()); + v.extend_from_slice(name.as_bytes()); + v.extend_from_slice(extra); + v + } + + #[test] + fn reads_a_plain_directory() { + let mut dir = central("Game.exe", DEFLATED, 7, 10, 20, 0, &[]); + dir.extend(central("__Installer/", STORED, 0, 0, 0, 40, &[])); + let mut tail = vec![0u8; 5]; + tail.extend_from_slice(&dir); + let cd_size = dir.len() as u32; + tail.extend_from_slice(&EOCD.to_le_bytes()); + tail.extend_from_slice(&[0, 0, 0, 0, 2, 0, 2, 0]); + tail.extend_from_slice(&cd_size.to_le_bytes()); + tail.extend_from_slice(&1000u32.to_le_bytes()); + tail.extend_from_slice(&[0, 0]); + let found = locate(&tail, 995).unwrap(); + assert_eq!(found, Directory { offset: 1000, size: cd_size as u64, entries: 2 }); + let entries = parse(&dir, 2).unwrap(); + assert_eq!(entries[0].name, "Game.exe"); + assert_eq!(entries[0].size, 20); + assert!(entries[1].is_dir()); + } + + #[test] + fn reads_zip64_values() { + let mut extra = vec![1, 0, 24, 0]; + extra.extend_from_slice(&6_000_000_000u64.to_le_bytes()); + extra.extend_from_slice(&5_000_000_000u64.to_le_bytes()); + extra.extend_from_slice(&7_000_000_000u64.to_le_bytes()); + let dir = central("big.pak", DEFLATED, 1, 0xFFFF_FFFF, 0xFFFF_FFFF, 0xFFFF_FFFF, &extra); + let e = &parse(&dir, 1).unwrap()[0]; + assert_eq!((e.size, e.compressed, e.local_offset), (6_000_000_000, 5_000_000_000, 7_000_000_000)); + + let mut tail = Vec::new(); + let record_at = 9_000_000_000u64; + tail.extend_from_slice(&EOCD64.to_le_bytes()); + tail.extend_from_slice(&44u64.to_le_bytes()); + tail.extend_from_slice(&[45, 0, 45, 0, 0, 0, 0, 0, 0, 0, 0, 0]); + tail.extend_from_slice(&3u64.to_le_bytes()); + tail.extend_from_slice(&3u64.to_le_bytes()); + tail.extend_from_slice(&500u64.to_le_bytes()); + tail.extend_from_slice(&8_000_000_000u64.to_le_bytes()); + tail.extend_from_slice(&EOCD64_LOCATOR.to_le_bytes()); + tail.extend_from_slice(&0u32.to_le_bytes()); + tail.extend_from_slice(&record_at.to_le_bytes()); + tail.extend_from_slice(&1u32.to_le_bytes()); + tail.extend_from_slice(&EOCD.to_le_bytes()); + tail.extend_from_slice(&[0, 0, 0, 0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0, 0]); + let found = locate(&tail, record_at).unwrap(); + assert_eq!(found, Directory { offset: 8_000_000_000, size: 500, entries: 3 }); + } + + #[test] + fn refuses_paths_that_leave_the_folder() { + assert_eq!(safe_path("Data\\Win32\\a.big").as_deref(), Some("Data/Win32/a.big")); + assert_eq!(safe_path("./x//y").as_deref(), Some("x/y")); + assert!(safe_path("../evil").is_none()); + assert!(safe_path("/etc/passwd").is_none()); + assert!(safe_path("C:/x").is_none()); + assert!(safe_path("a/../../b").is_none()); + } + + #[test] + fn local_header_length_counts_name_and_extra() { + let mut h = LOCAL.to_le_bytes().to_vec(); + h.extend_from_slice(&[0u8; 22]); + h.extend_from_slice(&5u16.to_le_bytes()); + h.extend_from_slice(&9u16.to_le_bytes()); + assert_eq!(local_header_len(&h), Some(Ok(44))); + assert_eq!(local_header_len(&h[..10]), None); + } +} diff --git a/app/src/main/rust/stores/src/lib.rs b/app/src/main/rust/stores/src/lib.rs index 87d278ab06..12a351138f 100644 --- a/app/src/main/rust/stores/src/lib.rs +++ b/app/src/main/rust/stores/src/lib.rs @@ -3,6 +3,8 @@ #![allow(clippy::missing_safety_doc, clippy::result_large_err)] pub mod amazon; +pub mod ubisoft; +pub mod ea; pub mod epic; pub mod fetch_core; pub mod fetch_tuning; diff --git a/app/src/main/rust/stores/src/ubisoft/demux.rs b/app/src/main/rust/stores/src/ubisoft/demux.rs new file mode 100644 index 0000000000..b54fee4430 --- /dev/null +++ b/app/src/main/rust/stores/src/ubisoft/demux.rs @@ -0,0 +1,154 @@ +use std::sync::Arc; +use std::time::Duration; + +use tokio::io::{AsyncReadExt, AsyncWriteExt}; +use tokio::net::TcpStream; +use tokio_rustls::client::TlsStream; +use tokio_rustls::rustls::{self, pki_types::ServerName, ClientConfig, RootCertStore}; +use tokio_rustls::TlsConnector; + +use super::pb::{self, Msg}; + +const HOST: &str = "dmx.upc.ubisoft.com"; +const PORT: u16 = 443; +const CLIENT_ID: &str = "uplay_pc"; +/// The Connect build number Demux expects first on a socket; older ones get ClientOutdatedPush and a closed socket. +pub const CLIENT_VERSION: u32 = 13247; +const READ_TIMEOUT: Duration = Duration::from_secs(20); +const MAX_FRAME: usize = 64 << 20; + +pub struct Demux { + tls: TlsStream, + next_id: u32, +} + +impl Demux { + pub async fn connect(ticket: &str) -> Result { + let mut roots = RootCertStore::empty(); + roots.extend(webpki_roots::TLS_SERVER_ROOTS.iter().cloned()); + let provider = Arc::new(rustls::crypto::ring::default_provider()); + let config = ClientConfig::builder_with_provider(provider) + .with_safe_default_protocol_versions() + .map_err(|e| format!("TLS setup: {e}"))? + .with_root_certificates(roots) + .with_no_client_auth(); + let tcp = tokio::time::timeout(READ_TIMEOUT, TcpStream::connect((HOST, PORT))) + .await + .map_err(|_| "Ubisoft's download service did not answer".to_string())? + .map_err(|e| format!("Could not reach Ubisoft's download service: {e}"))?; + let name = ServerName::try_from(HOST).map_err(|e| e.to_string())?.to_owned(); + let tls = TlsConnector::from(Arc::new(config)) + .connect(name, tcp) + .await + .map_err(|e| format!("TLS with Ubisoft's download service failed: {e}"))?; + let mut d = Demux { tls, next_id: 1 }; + d.send(Msg::new().msg(2, Msg::new().msg(4, Msg::new().uint(1, CLIENT_VERSION as u64)))).await?; + let id = d.id(); + let auth = Msg::new().uint(1, id as u64).msg( + 2, + Msg::new() + .msg(1, Msg::new().text(3, ticket)) + .text(2, CLIENT_ID) + .flag(4, false), + ); + d.send(Msg::new().msg(1, auth)).await?; + let rsp = d.response(id).await?; + let a = pb::fields(pb::sub(&pb::fields(&rsp)?, 2).ok_or("Ubisoft did not answer the sign-in")?)?; + if pb::num(&a, 1) != Some(1) { + let expired = pb::num(&a, 2) == Some(1); + return Err(if expired { "The Ubisoft sign-in expired".into() } else { "Ubisoft refused the sign-in".into() }); + } + Ok(d) + } + + fn id(&mut self) -> u32 { + let id = self.next_id; + self.next_id += 1; + id + } + + async fn send(&mut self, up: Msg) -> Result<(), String> { + let len = (up.0.len() as u32).to_be_bytes(); + self.tls.write_all(&len).await.map_err(|e| e.to_string())?; + self.tls.write_all(&up.0).await.map_err(|e| e.to_string())?; + self.tls.flush().await.map_err(|e| e.to_string()) + } + + async fn frame(&mut self) -> Result, String> { + let read = async { + let mut len = [0u8; 4]; + self.tls.read_exact(&mut len).await.map_err(|e| format!("Ubisoft closed the connection: {e}"))?; + let n = u32::from_be_bytes(len) as usize; + if n > MAX_FRAME { + return Err(format!("Ubisoft sent a {n}-byte frame")); + } + let mut body = vec![0u8; n]; + self.tls.read_exact(&mut body).await.map_err(|e| e.to_string())?; + Ok(body) + }; + tokio::time::timeout(READ_TIMEOUT, read).await.map_err(|_| "Ubisoft's download service stopped answering".to_string())? + } + + fn check_push(down: &[(u32, pb::Value<'_>)]) -> Result<(), String> { + if let Some(push) = pb::sub(down, 2) { + let p = pb::fields(push)?; + if pb::first(&p, 5).is_some() { + return Err("Ubisoft says this client version is outdated".into()); + } + } + Ok(()) + } + + async fn response(&mut self, id: u32) -> Result, String> { + for _ in 0..64 { + let frame = self.frame().await?; + let down = pb::fields(&frame)?; + Self::check_push(&down)?; + if let Some(rsp) = pb::sub(&down, 1) { + if pb::num(&pb::fields(rsp)?, 1) == Some(id as u64) { + return Ok(rsp.to_vec()); + } + } + } + Err("Ubisoft never answered the request".into()) + } + + pub async fn open(&mut self, service: &str) -> Result { + let id = self.id(); + self.send(Msg::new().msg(1, Msg::new().uint(1, id as u64).msg(3, Msg::new().text(2, service)))).await?; + let rsp = self.response(id).await?; + let o = pb::fields(pb::sub(&pb::fields(&rsp)?, 3).ok_or("no connection answer")?)?; + if pb::num(&o, 2) != Some(1) { + return Err(format!("Ubisoft refused the {service} connection")); + } + pb::num(&o, 1).map(|c| c as u32).ok_or_else(|| "no connection id".into()) + } + + /// One service request on a connection; the payload is length-prefixed inside the DataMessage both ways. + pub async fn call(&mut self, connection: u32, payload: Msg) -> Result, String> { + let mut framed = (payload.0.len() as u32).to_be_bytes().to_vec(); + framed.extend_from_slice(&payload.0); + self.send(Msg::new().msg(2, Msg::new().msg(1, Msg::new().uint(1, connection as u64).bytes(2, &framed)))).await?; + for _ in 0..64 { + let frame = self.frame().await?; + let down = pb::fields(&frame)?; + Self::check_push(&down)?; + let Some(push) = pb::sub(&down, 2) else { continue }; + let p = pb::fields(push)?; + if let Some(closed) = pb::sub(&p, 2) { + let c = pb::fields(closed)?; + if pb::num(&c, 1) == Some(connection as u64) { + return Err(format!("Ubisoft closed the service connection (code {})", pb::num(&c, 2).unwrap_or(0))); + } + } + let Some(data) = pb::sub(&p, 1) else { continue }; + let d = pb::fields(data)?; + if pb::num(&d, 1) != Some(connection as u64) { + continue; + } + let body = pb::sub(&d, 2).unwrap_or_default(); + return Ok(if body.len() >= 4 { body[4..].to_vec() } else { body.to_vec() }); + } + Err("Ubisoft never answered the service request".into()) + } +} diff --git a/app/src/main/rust/stores/src/ubisoft/ffi.rs b/app/src/main/rust/stores/src/ubisoft/ffi.rs new file mode 100644 index 0000000000..30df1ccd4f --- /dev/null +++ b/app/src/main/rust/stores/src/ubisoft/ffi.rs @@ -0,0 +1,269 @@ +use std::panic::{catch_unwind, AssertUnwindSafe}; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::Arc; + +use jni::objects::{GlobalRef, JClass, JObject, JString, JValue}; +use jni::sys::{jlong, jstring, JNI_FALSE, JNI_TRUE}; +use jni::{JNIEnv, JavaVM}; +use serde_json::json; + +use super::install; +use crate::amazon::install::{Events, Stop}; + +fn read_string(env: &mut JNIEnv, value: &JString) -> String { + if value.is_null() { + return String::new(); + } + env.get_string(value).map(|s| s.to_string_lossy().into_owned()).unwrap_or_default() +} + +fn new_string(env: &mut JNIEnv, text: &str) -> jstring { + env.new_string(text).map(|s| s.into_raw()).unwrap_or(std::ptr::null_mut()) +} + +fn owned(ticket: &str, session: &str) -> Result { + let rt = tokio::runtime::Builder::new_current_thread().enable_all().build().map_err(|e| e.to_string())?; + rt.block_on(async { + let mut d = super::demux::Demux::connect(ticket).await?; + let (mut ownership, mut games) = super::service::Ownership::open(&mut d, ticket, session).await?; + let missing: Vec = games.iter().filter(|g| g.owned && g.product_type == 0 && g.manifest.is_empty()).map(|g| g.product_id).collect(); + let (found, manifest_error) = match ownership.latest_manifests(&mut d, &missing).await { + Ok(m) => (m, String::new()), + Err(e) => (Vec::new(), e), + }; + for (product, manifest) in &found { + if let Some(g) = games.iter_mut().find(|g| g.product_id == *product) { + g.manifest = manifest.clone(); + } + } + Ok(json!({ "games": games.iter().map(|g| g.to_json()).collect::>(), + "manifestsAsked": missing.len(), "manifestsFound": found.len(), "manifestError": manifest_error })) + }) +} + +fn launch(ticket: &str, session: &str, product: u32) -> Result { + let rt = tokio::runtime::Builder::new_current_thread().enable_all().build().map_err(|e| e.to_string())?; + rt.block_on(async { + let mut d = super::demux::Demux::connect(ticket).await?; + let (mut ownership, games) = super::service::Ownership::open(&mut d, ticket, session).await?; + if !games.iter().any(|g| g.product_id == product && g.owned) { + return Err("Ubisoft says this account does not own the game".into()); + } + let pc_ticket = ownership.pc_ticket(&mut d, product, ticket, session).await?; + let owned: Vec = games.iter().filter(|g| g.in_session()).map(|g| g.product_id).collect(); + let products: Vec<_> = games.iter().filter(|g| g.in_session()).map(|g| json!({ "id": g.product_id, "type": g.product_type })).collect(); + Ok(json!({ "ticket": pc_ticket, "owned": owned, "products": products })) + }) +} + +fn unhex(s: &str) -> Result, String> { + if s.len() % 2 != 0 { + return Err("odd hex length".into()); + } + (0..s.len()).step_by(2).map(|i| u8::from_str_radix(&s[i..i + 2], 16).map_err(|e| e.to_string())).collect() +} + +fn denuvo(ticket: &str, session: &str, product: u32, challenge: &str) -> Result { + let challenge = unhex(challenge).map_err(|e| format!("challenge: {e}"))?; + let rt = tokio::runtime::Builder::new_current_thread().enable_all().build().map_err(|e| e.to_string())?; + rt.block_on(async { + let mut d = super::demux::Demux::connect(ticket).await?; + let (mut ownership, _) = super::service::Ownership::open(&mut d, ticket, session).await?; + let token = ownership.token(&mut d, product).await?; + let game = super::service::denuvo_token(&mut d, &token, &challenge).await?; + Ok(json!({ "token": game.iter().map(|b| format!("{b:02x}")).collect::() })) + }) +} + +fn call(op: &str, request: &str) -> String { + let parsed: serde_json::Value = serde_json::from_str(request).unwrap_or(serde_json::Value::Null); + let s = |k: &str| parsed.get(k).and_then(|v| v.as_str()).unwrap_or("").to_string(); + let answer = match op { + "owned" => owned(&s("ticket"), &s("sessionId")).unwrap_or_else(|e| json!({ "error": e })), + "denuvo" => denuvo(&s("ticket"), &s("sessionId"), parsed.get("productId").and_then(|v| v.as_u64()).unwrap_or(0) as u32, &s("challenge")) + .unwrap_or_else(|e| json!({ "error": e })), + "chunks" => install::Request::from_json(&parsed) + .and_then(|r| install::chunks(&r)) + .map(|m| json!({ "chunks": install::chunks_json(&m.chunks), "setup": install::setup_json(&m) })) + .unwrap_or_else(|e| json!({ "error": e })), + "launch" => launch(&s("ticket"), &s("sessionId"), parsed.get("productId").and_then(|v| v.as_u64()).unwrap_or(0) as u32) + .unwrap_or_else(|e| json!({ "error": e })), + _ => json!({ "error": format!("unknown operation {op}") }), + }; + answer.to_string() +} + +#[no_mangle] +pub extern "system" fn Java_com_droiddeck_launcher_stores_ubisoft_UbisoftNative_nativeCall( + mut env: JNIEnv, + _class: JClass, + op: JString, + request: JString, +) -> jstring { + let op = read_string(&mut env, &op); + let request = read_string(&mut env, &request); + match catch_unwind(AssertUnwindSafe(|| call(&op, &request))) { + Ok(text) => new_string(&mut env, &text), + Err(_) => std::ptr::null_mut(), + } +} + +struct Listener { + vm: JavaVM, + target: GlobalRef, +} + +impl Listener { + fn with_env(&self, fallback: R, f: impl FnOnce(&mut JNIEnv) -> R) -> R { + let Ok(mut env) = self.vm.attach_current_thread_as_daemon() else { + return fallback; + }; + let out = f(&mut env); + if env.exception_check().unwrap_or(false) { + let _ = env.exception_clear(); + } + out + } + + fn text(&self, method: &str, text: &str) { + self.with_env((), |env| { + let Ok(s) = env.new_string(text) else { return }; + let s = JObject::from(s); + let _ = env.call_method(self.target.as_obj(), method, "(Ljava/lang/String;)V", &[JValue::Object(&s)]); + let _ = env.delete_local_ref(s); + }); + } + + fn counts(&self, method: &str, a: u64, b: u64, c: u32, d: u32) { + self.with_env((), |env| { + let _ = env.call_method( + self.target.as_obj(), + method, + "(JJII)V", + &[ + JValue::Long(a.min(i64::MAX as u64) as i64), + JValue::Long(b.min(i64::MAX as u64) as i64), + JValue::Int(c.min(i32::MAX as u32) as i32), + JValue::Int(d.min(i32::MAX as u32) as i32), + ], + ); + }); + } + + fn complete(&self, success: bool, cancelled: bool, error: &str, result: &str) { + self.with_env((), |env| { + let (Ok(e), Ok(r)) = (env.new_string(error), env.new_string(result)) else { return }; + let (e, r) = (JObject::from(e), JObject::from(r)); + let _ = env.call_method( + self.target.as_obj(), + "onComplete", + "(ZZLjava/lang/String;Ljava/lang/String;)V", + &[ + JValue::Bool(if success { JNI_TRUE } else { JNI_FALSE }), + JValue::Bool(if cancelled { JNI_TRUE } else { JNI_FALSE }), + JValue::Object(&e), + JValue::Object(&r), + ], + ); + let _ = env.delete_local_ref(e); + let _ = env.delete_local_ref(r); + }); + } +} + +impl Events for Listener { + fn log(&self, line: &str) { + self.text("onLog", &crate::amazon::net::scrub_urls(line)); + } + + fn status(&self, text: &str) { + self.text("onStatus", text); + } + + fn checking(&self, done_bytes: u64, total_bytes: u64, files: u32, files_total: u32) { + self.counts("onCheck", done_bytes, total_bytes, files, files_total); + } + + fn sizes(&self, pending_bytes: u64, total_bytes: u64) { + self.with_env((), |env| { + let _ = env.call_method( + self.target.as_obj(), + "onSizes", + "(JJ)V", + &[ + JValue::Long(pending_bytes.min(i64::MAX as u64) as i64), + JValue::Long(total_bytes.min(i64::MAX as u64) as i64), + ], + ); + }); + } + + fn progress(&self, done: u64, total: u64, files: u32, files_total: u32) { + self.counts("onProgress", done, total, files, files_total); + } + + fn before_replace(&self) -> bool { + self.with_env(false, |env| match env.call_method(self.target.as_obj(), "beforeReplace", "()Z", &[]) { + Ok(v) => v.z().unwrap_or(false), + Err(_) => false, + }) + } +} + +#[no_mangle] +pub extern "system" fn Java_com_droiddeck_launcher_stores_ubisoft_UbisoftNative_nativeInstall( + mut env: JNIEnv, + _class: JClass, + request: JString, + listener: JObject, +) -> jlong { + if listener.is_null() { + return 0; + } + let Ok(vm) = env.get_java_vm() else { return 0 }; + let Ok(target) = env.new_global_ref(&listener) else { return 0 }; + let listener = Listener { vm, target }; + let text = read_string(&mut env, &request); + let parsed = serde_json::from_str::(&text) + .map_err(|_| "The install request could not be read".to_string()) + .and_then(|v| install::Request::from_json(&v)); + let request = match parsed { + Ok(r) => r, + Err(e) => { + listener.complete(false, false, &e, ""); + return 0; + } + }; + let cancel = Arc::new(AtomicBool::new(false)); + let worker_cancel = Arc::clone(&cancel); + let spawned = std::thread::Builder::new().name("ubisoft-install".into()).spawn(move || { + crate::priority::background(); + let result = catch_unwind(AssertUnwindSafe(|| install::run(&request, &worker_cancel, &listener))) + .unwrap_or_else(|_| Err(Stop::Failed("The Ubisoft install stopped on an internal error".into()))); + match result { + Ok(done) => listener.complete(true, false, "", &done.to_json().to_string()), + Err(Stop::Cancelled) => listener.complete(false, true, "cancelled", ""), + Err(Stop::Failed(e)) => listener.complete(false, false, &e, ""), + } + }); + if spawned.is_err() { + return 0; + } + Box::into_raw(Box::new(cancel)) as jlong +} + +#[no_mangle] +pub extern "system" fn Java_com_droiddeck_launcher_stores_ubisoft_UbisoftNative_nativeCancel(_env: JNIEnv, _class: JClass, handle: jlong) { + let ptr = handle as *const Arc; + if let Some(cancel) = unsafe { ptr.as_ref() } { + cancel.store(true, Ordering::SeqCst); + } +} + +#[no_mangle] +pub extern "system" fn Java_com_droiddeck_launcher_stores_ubisoft_UbisoftNative_nativeRelease(_env: JNIEnv, _class: JClass, handle: jlong) { + let ptr = handle as *mut Arc; + if !ptr.is_null() { + drop(unsafe { Box::from_raw(ptr) }); + } +} diff --git a/app/src/main/rust/stores/src/ubisoft/install.rs b/app/src/main/rust/stores/src/ubisoft/install.rs new file mode 100644 index 0000000000..fdb4357fab --- /dev/null +++ b/app/src/main/rust/stores/src/ubisoft/install.rs @@ -0,0 +1,408 @@ +use std::collections::HashMap; +use std::io::{Read, Seek, SeekFrom, Write}; +use std::path::{Component, Path, PathBuf}; +use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; +use std::sync::Mutex; +use std::time::{Duration, Instant}; + +use futures_util::stream::{self, StreamExt}; +use serde_json::{json, Value as Json}; +use sha1::{Digest, Sha1}; + +use super::demux::Demux; +use super::manifest::{self, File}; +use super::service::{Downloads, Ownership}; +use crate::amazon::install::{Events, Stop}; + +const SIGN_BATCH: usize = 200; +const WINDOW: usize = 16; +const TRIES: usize = 4; + +pub struct Request { + pub ticket: String, + pub session_id: String, + pub product_id: u32, + pub manifest: String, + pub folder: PathBuf, + pub language: String, +} + +impl Request { + pub fn from_json(v: &Json) -> Result { + let s = |k: &str| v.get(k).and_then(|x| x.as_str()).unwrap_or("").to_string(); + let product_id = v.get("productId").and_then(|x| x.as_u64()).ok_or("no product id")? as u32; + let folder = PathBuf::from(s("folder")); + if s("ticket").is_empty() || folder.as_os_str().is_empty() { + return Err("The install request is incomplete".into()); + } + Ok(Request { ticket: s("ticket"), session_id: s("sessionId"), product_id, manifest: s("manifest"), folder, language: s("language") }) + } +} + +pub struct Done { + pub files: usize, + pub bytes: u64, + pub chunks: Vec, + pub setup: Json, +} + +pub fn chunks_json(chunks: &[manifest::Chunk]) -> Json { + Json::Array(chunks.iter().map(|c| json!({ "id": c.id, "tags": c.tags })).collect()) +} + +pub fn setup_json(m: &manifest::Manifest) -> Json { + json!({ + "installRuns": m.install_runs.iter().map(|(exe, args, what)| json!({ "exe": exe, "arguments": args, "description": what })).collect::>(), + "registry": m.registry.iter().map(|(key, values)| json!({ "key": key, "values": values.iter().map(|(n, v)| json!({ "name": n, "value": v })).collect::>() })).collect::>(), + }) +} + +impl Done { + pub fn to_json(&self) -> Json { + json!({ "files": self.files, "bytes": self.bytes, "chunks": chunks_json(&self.chunks), "setup": self.setup.clone() }) + } +} + +pub fn safe_path(root: &Path, name: &str) -> Result { + let rel = Path::new(name.trim_start_matches('/')); + if rel.components().any(|c| !matches!(c, Component::Normal(_))) { + return Err(format!("The manifest names an unsafe path: {name}")); + } + Ok(root.join(rel)) +} + +fn sha1(b: &[u8]) -> Vec { + Sha1::digest(b).to_vec() +} + +/// The slice as served and as unpacked must both match the manifest, so a bad proxy or a truncated body never lands in the game. +pub fn checked(method: u64, raw: &[u8], s: &manifest::Slice) -> Result, String> { + if let Some(n) = s.download_size { + if raw.len() as u32 != n { + return Err(format!("a slice is {} bytes, the manifest says {n}", raw.len())); + } + } + if !s.download_sha1.is_empty() && sha1(raw) != s.download_sha1 { + return Err("a slice failed its download checksum".into()); + } + let out = manifest::decompress(method, raw)?; + if s.size != 0 && out.len() as u32 != s.size { + return Err(format!("a slice unpacked to {} bytes, the manifest says {}", out.len(), s.size)); + } + if s.content_sha1.len() == 20 && sha1(&out) != s.content_sha1 { + return Err("a slice failed its content checksum".into()); + } + Ok(out) +} + +/// Whether a file of the right size already holds every slice the manifest lists for it; a file that cannot be checked is fetched again. +fn already_installed(path: &Path, f: &File) -> bool { + if f.slices.iter().any(|s| s.content_sha1.len() != 20 || s.size == 0) { + return false; + } + let Ok(mut file) = std::fs::File::open(path) else { return false }; + let mut buf = Vec::new(); + f.slices.iter().all(|s| { + buf.resize(s.size as usize, 0); + file.seek(SeekFrom::Start(s.offset)).is_ok() && file.read_exact(&mut buf).is_ok() && sha1(&buf) == s.content_sha1 + }) +} + +fn slice_hash(s: &manifest::Slice) -> String { + manifest::hex_upper(if s.download_sha1.is_empty() { &s.content_sha1 } else { &s.download_sha1 }) +} + +struct Job { + file: usize, + slice: usize, + path: String, +} + +async fn fetch(client: &reqwest::Client, urls: &[String]) -> Result, String> { + let mut last = String::from("no URL"); + for attempt in 0..TRIES { + for u in urls { + match client.get(u).send().await { + Ok(r) if r.status().is_success() => match r.bytes().await { + Ok(b) => return Ok(b.to_vec()), + Err(e) => last = e.to_string(), + }, + Ok(r) => last = format!("HTTP {}", r.status().as_u16()), + Err(e) => last = e.to_string(), + } + } + tokio::time::sleep(Duration::from_millis(500 << attempt)).await; + } + Err(format!("a slice could not be downloaded ({last})")) +} + +/// A Demux login holding a download_service connection for one product. Ubisoft drops idle sockets +/// while slices download, so signing reconnects and retries when the connection is gone. +struct Session { + d: Demux, + dl: Downloads, +} + +const RECONNECTS: usize = 4; + +impl Session { + /// Opens a session, retrying a few times: Demux drops a connection now and then right after another closes. + async fn open(req: &Request) -> Result<(Self, Vec), String> { + let mut last = String::new(); + for attempt in 0..3u32 { + if attempt > 0 { + tokio::time::sleep(Duration::from_secs(2 << attempt)).await; + } + match Self::open_once(req).await { + Ok(s) => return Ok(s), + Err(e) => last = e, + } + } + Err(last) + } + + async fn open_once(req: &Request) -> Result<(Self, Vec), String> { + let mut d = Demux::connect(&req.ticket).await?; + let (mut own, owned) = Ownership::open(&mut d, &req.ticket, &req.session_id).await?; + let token = own.token(&mut d, req.product_id).await?; + let dl = Downloads::open(&mut d, &token).await?; + Ok((Session { d, dl }, owned)) + } + + async fn sign(&mut self, req: &Request, paths: &[String], events: &dyn Events) -> Result>, String> { + let mut last = String::new(); + for attempt in 0..=RECONNECTS { + if attempt > 0 { + events.log(&format!("ubisoft: reconnecting after: {last}")); + tokio::time::sleep(Duration::from_millis(500 << attempt.min(4))).await; + match Session::open(req).await { + Ok((fresh, _)) => *self = fresh, + Err(e) => { + last = e; + continue; + } + } + } + match self.dl.sign(&mut self.d, req.product_id, paths).await { + Ok(urls) => return Ok(urls), + Err(e) if e.contains("does not own") => return Err(e), + Err(e) => last = e, + } + } + Err(last) + } +} + +/// The manifest of an install, without downloading the game. +pub fn chunks(req: &Request) -> Result { + let rt = tokio::runtime::Builder::new_current_thread().enable_all().build().map_err(|e| e.to_string())?; + rt.block_on(async { + let (mut sess, owned) = Session::open(req).await?; + let manifest_id = if req.manifest.is_empty() { + owned.iter().find(|o| o.product_id == req.product_id).map(|o| o.manifest.clone()).unwrap_or_default() + } else { + req.manifest.clone() + }; + if manifest_id.is_empty() { + return Err("Ubisoft lists no download for this game".to_string()); + } + let client = reqwest::Client::builder().timeout(Duration::from_secs(120)).build().map_err(|e| e.to_string())?; + struct Quiet; + impl Events for Quiet { + fn log(&self, _: &str) {} + fn status(&self, _: &str) {} + fn checking(&self, _: u64, _: u64, _: u32, _: u32) {} + fn sizes(&self, _: u64, _: u64) {} + fn progress(&self, _: u64, _: u64, _: u32, _: u32) {} + fn before_replace(&self) -> bool { false } + } + let urls = sess.sign(req, &[format!("manifests/{manifest_id}.manifest")], &Quiet).await?; + let raw = fetch(&client, urls.first().map(|v| v.as_slice()).unwrap_or_default()).await?; + let language = if req.language.is_empty() { "en-US" } else { req.language.as_str() }; + let mut owned_ids: Vec = owned.iter().filter(|o| o.owned).map(|o| o.product_id).collect(); + owned_ids.push(req.product_id); + Ok(manifest::parse_owned(&manifest::decode(&raw)?, language, &owned_ids)?) + }) +} + +pub fn run(req: &Request, cancel: &AtomicBool, events: &dyn Events) -> Result { + let rt = tokio::runtime::Builder::new_current_thread().enable_all().build().map_err(|e| Stop::Failed(e.to_string()))?; + rt.block_on(install(req, cancel, events)) +} + +async fn install(req: &Request, cancel: &AtomicBool, events: &dyn Events) -> Result { + let fail = Stop::Failed; + events.status("Connecting to Ubisoft"); + let (mut sess, owned) = Session::open(req).await.map_err(fail)?; + let manifest_id = if req.manifest.is_empty() { + owned.iter().find(|o| o.product_id == req.product_id).map(|o| o.manifest.clone()).unwrap_or_default() + } else { + req.manifest.clone() + }; + if manifest_id.is_empty() { + return Err(Stop::Failed("Ubisoft lists no download for this game".into())); + } + let client = reqwest::Client::builder().timeout(Duration::from_secs(120)).build().map_err(|e| Stop::Failed(e.to_string()))?; + + events.status("Reading the game's file list"); + let urls = sess.sign(req, &[format!("manifests/{manifest_id}.manifest")], events).await.map_err(fail)?; + let raw = fetch(&client, urls.first().map(|v| v.as_slice()).unwrap_or_default()).await.map_err(fail)?; + let decoded = manifest::decode(&raw).map_err(fail)?; + let language = if req.language.is_empty() { "en-US" } else { req.language.as_str() }; + let mut owned_ids: Vec = owned.iter().filter(|o| o.owned).map(|o| o.product_id).collect(); + owned_ids.push(req.product_id); + let m = manifest::parse_owned(&decoded, language, &owned_ids).map_err(fail)?; + events.log(&format!("ubisoft: manifest {manifest_id} files={} chunks={} skipped={} compression={}", m.files.len(), m.chunks.len(), m.skipped.len(), m.compression)); + for name in &m.skipped { + if let Ok(path) = safe_path(&req.folder, name) { + if path.is_file() && std::fs::remove_file(&path).is_ok() { + events.log(&format!("ubisoft: removed {name}, which this install leaves out")); + } + } + } + + std::fs::create_dir_all(&req.folder).map_err(|e| Stop::Failed(format!("Could not create the game folder: {e}")))?; + let total: u64 = m.files.iter().map(|f| f.size).sum(); + let files_total = m.files.len() as u32; + let mut pending: Vec = Vec::new(); + let mut done_bytes = 0u64; + let mut done_files = 0u32; + for (i, f) in m.files.iter().enumerate() { + let path = safe_path(&req.folder, &f.name).map_err(fail)?; + if f.dir { + std::fs::create_dir_all(&path).map_err(|e| Stop::Failed(e.to_string()))?; + done_files += 1; + continue; + } + if std::fs::metadata(&path).map(|md| md.len() == f.size).unwrap_or(false) && already_installed(&path, f) { + done_bytes += f.size; + done_files += 1; + continue; + } + pending.push(i); + } + let pending_bytes: u64 = pending.iter().map(|&i| m.files[i].size).sum(); + events.sizes(pending_bytes, total); + events.log(&format!("ubisoft: {} of {} files to fetch, {pending_bytes} bytes", pending.len(), m.files.len())); + + let bucketed = match pending.iter().flat_map(|&i| m.files[i].slices.first()).next() { + None => true, + Some(s) => { + let h = slice_hash(s); + let probe = sess.sign(req, &[manifest::slice_path(true, &h), manifest::slice_path(false, &h)], events).await.map_err(fail)?; + let mut v3 = true; + for (layout, list) in [true, false].into_iter().zip(probe.iter()) { + if let Some(u) = list.first() { + if client.get(u).send().await.map(|r| r.status().is_success()).unwrap_or(false) { + v3 = layout; + break; + } + } + } + v3 + } + }; + + let mut parts: HashMap> = HashMap::new(); + let mut remaining: HashMap = HashMap::new(); + let mut jobs = Vec::new(); + for &i in &pending { + let f: &File = &m.files[i]; + let path = safe_path(&req.folder, &f.name).map_err(fail)?; + if let Some(parent) = path.parent() { + std::fs::create_dir_all(parent).map_err(|e| Stop::Failed(e.to_string()))?; + } + let part = path.with_extension(format!("{}part", path.extension().map(|e| format!("{}.", e.to_string_lossy())).unwrap_or_default())); + let file = std::fs::File::create(&part).map_err(|e| Stop::Failed(format!("Could not write {}: {e}", f.name)))?; + file.set_len(f.size).map_err(|e| Stop::Failed(e.to_string()))?; + parts.insert(i, Mutex::new(file)); + remaining.insert(i, f.slices.len()); + for (si, s) in f.slices.iter().enumerate() { + jobs.push(Job { file: i, slice: si, path: manifest::slice_path(bucketed, &slice_hash(s)) }); + } + } + + let progress = AtomicU64::new(done_bytes); + let mut finished_files = done_files; + let mut last_report = Instant::now(); + events.status("Downloading"); + for batch in jobs.chunks(SIGN_BATCH) { + if cancel.load(Ordering::SeqCst) { + return Err(Stop::Cancelled); + } + let paths: Vec = batch.iter().map(|j| j.path.clone()).collect(); + let signed = sess.sign(req, &paths, events).await.map_err(fail)?; + let mut slices = stream::iter(batch.iter().zip(signed.into_iter())) + .map(|(job, urls)| { + let client = &client; + let m = &m; + async move { + let s = &m.files[job.file].slices[job.slice]; + let raw = fetch(client, &urls).await?; + let out = checked(m.compression, &raw, s).map_err(|e| format!("{}: {e}", m.files[job.file].name))?; + Ok::<_, String>((job.file, job.slice, out)) + } + }) + .buffer_unordered(WINDOW); + while let Some(r) = slices.next().await { + if cancel.load(Ordering::SeqCst) { + return Err(Stop::Cancelled); + } + let (fi, si, data) = r.map_err(fail)?; + let s = &m.files[fi].slices[si]; + { + let mut f = parts[&fi].lock().unwrap(); + f.seek(SeekFrom::Start(s.offset)).and_then(|_| f.write_all(&data)).map_err(|e| Stop::Failed(format!("Could not write {}: {e}", m.files[fi].name)))?; + } + progress.fetch_add(data.len() as u64, Ordering::Relaxed); + let left = remaining.get_mut(&fi).unwrap(); + *left -= 1; + if *left == 0 { + let f = parts.remove(&fi).unwrap().into_inner().unwrap(); + f.sync_all().map_err(|e| Stop::Failed(e.to_string()))?; + drop(f); + let path = safe_path(&req.folder, &m.files[fi].name).map_err(fail)?; + let part = path.with_extension(format!("{}part", path.extension().map(|e| format!("{}.", e.to_string_lossy())).unwrap_or_default())); + std::fs::rename(&part, &path).map_err(|e| Stop::Failed(format!("Could not finish {}: {e}", m.files[fi].name)))?; + finished_files += 1; + } + if last_report.elapsed() > Duration::from_millis(250) { + events.progress(progress.load(Ordering::Relaxed), total, finished_files, files_total); + last_report = Instant::now(); + } + } + } + for i in pending.iter().copied().filter(|i| m.files[*i].slices.is_empty()) { + let path = safe_path(&req.folder, &m.files[i].name).map_err(fail)?; + parts.remove(&i); + let part = path.with_extension(format!("{}part", path.extension().map(|e| format!("{}.", e.to_string_lossy())).unwrap_or_default())); + std::fs::rename(&part, &path).map_err(|e| Stop::Failed(e.to_string()))?; + finished_files += 1; + } + events.progress(total, total, finished_files, files_total); + Ok(Done { files: m.files.len(), bytes: total, chunks: m.chunks.clone(), setup: setup_json(&m) }) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn refuses_paths_that_leave_the_folder() { + let root = Path::new("/games/x"); + assert!(safe_path(root, "../evil").is_err()); + assert!(safe_path(root, "a/../../evil").is_err()); + assert_eq!(safe_path(root, "/bin/game.exe").unwrap(), root.join("bin/game.exe")); + } + + #[test] + fn checks_a_slice_as_served_and_unpacked() { + let data = b"slice".to_vec(); + let ok = manifest::Slice { size: 5, download_size: Some(5), download_sha1: sha1(&data), content_sha1: Vec::new(), offset: 0 }; + assert_eq!(checked(0, &data, &ok).unwrap(), data); + let bad = manifest::Slice { download_sha1: vec![0; 20], ..ok.clone() }; + assert!(checked(0, &data, &bad).is_err()); + let short = manifest::Slice { download_size: Some(4), ..ok }; + assert!(checked(0, &data, &short).is_err()); + } +} diff --git a/app/src/main/rust/stores/src/ubisoft/manifest.rs b/app/src/main/rust/stores/src/ubisoft/manifest.rs new file mode 100644 index 0000000000..0487a58564 --- /dev/null +++ b/app/src/main/rust/stores/src/ubisoft/manifest.rs @@ -0,0 +1,230 @@ +use std::io::Read; + +use super::pb; + +/// Bytes Ubisoft prepends to a downloaded `.manifest` before the zlib stream. +pub const HEADER: usize = 356; +pub const DEFLATE: u64 = 1; +pub const ZSTD: u64 = 3; + +#[derive(Clone, Debug, PartialEq)] +pub struct Slice { + pub size: u32, + pub download_size: Option, + pub download_sha1: Vec, + pub content_sha1: Vec, + pub offset: u64, +} + +#[derive(Clone, Debug, PartialEq)] +pub struct File { + pub name: String, + pub size: u64, + pub dir: bool, + pub slices: Vec, +} + +/// A chunk the install keeps, as the game's launcher session reports it. +#[derive(Clone, Debug, PartialEq)] +pub struct Chunk { + pub id: u32, + pub tags: String, +} + +#[derive(Debug)] +pub struct Manifest { + pub compression: u64, + pub files: Vec, + pub chunks: Vec, + /// Files of chunks left out: other languages, and add-ons the account does not own. + pub skipped: Vec, + /// Prerequisite installers Connect runs after an install: exe, arguments, description. + pub install_runs: Vec<(String, String, String)>, + /// Registry keys Connect writes for the game, with their string values as (name, value). + pub registry: Vec<(String, Vec<(String, String)>)>, +} + +fn file(b: &[u8]) -> Result { + let f = pb::fields(b)?; + let hashes = pb::subs(&f, 4); + let mut offset = 0u64; + let mut slices = Vec::new(); + for (i, s) in pb::subs(&f, 7).into_iter().enumerate() { + let s = pb::fields(s)?; + let size = pb::num(&s, 1).unwrap_or(0) as u32; + let at = pb::num(&s, 4).unwrap_or(offset); + slices.push(Slice { + size, + download_size: pb::num(&s, 2).map(|v| v as u32), + download_sha1: pb::sub(&s, 3).unwrap_or_default().to_vec(), + content_sha1: hashes.get(i).map(|h| h.to_vec()).unwrap_or_default(), + offset: at, + }); + offset = at + size as u64; + } + if slices.is_empty() { + for h in &hashes { + slices.push(Slice { size: 0, download_size: None, download_sha1: Vec::new(), content_sha1: h.to_vec(), offset: 0 }); + } + } + Ok(File { + name: pb::string(&f, 1).unwrap_or_default().replace('\\', "/"), + size: pb::num(&f, 2).unwrap_or(0), + dir: pb::num(&f, 3) == Some(1), + slices, + }) +} + +/// Files of the chunks to install: those for every language plus the language asked for, of the +/// game itself or an optional add-on in `owned` (every add-on when `owned` is empty). +pub fn parse(decoded: &[u8], language: &str) -> Result { + parse_owned(decoded, language, &[]) +} + +pub fn parse_owned(decoded: &[u8], language: &str, owned: &[u32]) -> Result { + let m = pb::fields(decoded)?; + let wanted = language.to_ascii_lowercase(); + let prefix = wanted.split(['-', '_']).next().unwrap_or("").to_string(); + let mut files = Vec::new(); + let mut chunks = Vec::new(); + let mut skipped = Vec::new(); + for c in pb::subs(&m, 4) { + let c = pb::fields(c)?; + let lang = pb::string(&c, 5).unwrap_or_default().to_ascii_lowercase(); + let mut products: Vec = c.iter().filter_map(|(n, v)| match v { pb::Value::Varint(x) if *n == 8 => Some(*x as u32), _ => None }).collect(); + products.extend(pb::num(&c, 4).map(|v| v as u32)); + let in_language = lang.is_empty() || lang == wanted || (!prefix.is_empty() && lang.split(['-', '_']).next() == Some(prefix.as_str())); + let optional = pb::num(&c, 2) == Some(1); + let owned_product = !optional || owned.is_empty() || products.is_empty() || products.iter().any(|p| owned.contains(p)); + if !(in_language && owned_product) { + for f in pb::subs(&c, 3) { + skipped.push(file(f)?.name); + } + continue; + } + chunks.push(Chunk { id: pb::num(&c, 1).unwrap_or(0) as u32, tags: pb::string(&c, 7).unwrap_or_default() }); + for f in pb::subs(&c, 3) { + files.push(file(f)?); + } + } + let kept: std::collections::HashSet<&str> = files.iter().map(|f| f.name.as_str()).collect(); + skipped.retain(|n| !kept.contains(n.as_str())); + let mut install_runs = Vec::new(); + for r in pb::subs(&m, 2) { + let r = pb::fields(r)?; + install_runs.push((pb::string(&r, 1).unwrap_or_default(), pb::string(&r, 3).unwrap_or_default(), pb::string(&r, 4).unwrap_or_default())); + } + let mut registry = Vec::new(); + for r in pb::subs(&m, 5) { + let r = pb::fields(r)?; + let mut values = Vec::new(); + for e in pb::subs(&r, 3) { + let e = pb::fields(e)?; + values.push((pb::string(&e, 1).unwrap_or_default(), pb::string(&e, 2).unwrap_or_default())); + } + registry.push((pb::string(&r, 1).unwrap_or_default(), values)); + } + Ok(Manifest { compression: pb::num(&m, 21).unwrap_or(0), files, chunks, skipped, install_runs, registry }) +} + +pub fn decode(downloaded: &[u8]) -> Result, String> { + if downloaded.len() <= HEADER { + return Err(format!("the manifest is only {} bytes", downloaded.len())); + } + let mut out = Vec::new(); + flate2::read::ZlibDecoder::new(&downloaded[HEADER..]).read_to_end(&mut out).map_err(|e| format!("the manifest did not inflate: {e}"))?; + Ok(out) +} + +pub fn hex_upper(b: &[u8]) -> String { + b.iter().map(|x| format!("{x:02X}")).collect() +} + +/// The CDN bucket of a slice: the first byte of its hash with nibbles swapped, mapped onto base32. +pub fn bucket(hash_upper: &str) -> char { + const BASE32: &[u8; 32] = b"0123456789abcdefghijklmnopqrstuv"; + let b = hash_upper.as_bytes(); + let swapped = format!("{}{}", *b.get(1).unwrap_or(&b'0') as char, *b.first().unwrap_or(&b'0') as char); + let v = u8::from_str_radix(&swapped, 16).unwrap_or(0); + BASE32[(v / 16) as usize + if v % 2 == 0 { 0 } else { 16 }] as char +} + +pub fn slice_path(bucketed: bool, hash_upper: &str) -> String { + if bucketed { format!("slices_v3/{}/{}", bucket(hash_upper), hash_upper) } else { format!("slices/{hash_upper}") } +} + +pub fn decompress(method: u64, data: &[u8]) -> Result, String> { + match method { + ZSTD => { + let mut out = Vec::new(); + ruzstd::decoding::StreamingDecoder::new(data).map_err(|e| format!("zstd: {e}"))?.read_to_end(&mut out).map_err(|e| format!("zstd: {e}"))?; + Ok(out) + } + DEFLATE => { + let zlib = data.len() >= 2 && data[0] == 0x78; + let inflate = |z: bool| { + let mut out = Vec::new(); + let r = if z { flate2::read::ZlibDecoder::new(data).read_to_end(&mut out) } else { flate2::read::DeflateDecoder::new(data).read_to_end(&mut out) }; + r.map(|_| out) + }; + inflate(zlib).or_else(|_| inflate(!zlib)).map_err(|e| format!("deflate: {e}")) + } + _ => Ok(data.to_vec()), + } +} + +#[cfg(test)] +mod tests { + use super::super::pb::Msg; + use super::*; + + #[test] + fn buckets_follow_the_swapped_first_byte() { + assert_eq!(bucket("10ABCDEF"), 'g'); + assert_eq!(bucket("01ABCDEF"), '1'); + assert_eq!(slice_path(true, "01AB"), "slices_v3/1/01AB"); + assert_eq!(slice_path(false, "01AB"), "slices/01AB"); + } + + #[test] + fn keeps_shared_and_wanted_language_chunks() { + let f = |name: &str| Msg::new().text(1, name).uint(2, 10).flag(3, false) + .bytes(4, &[0xAA]).msg(7, Msg::new().uint(1, 10).uint(2, 4).bytes(3, &[0xBB])); + let m = Msg::new() + .msg(4, Msg::new().uint(1, 1).uint(2, 0).msg(3, f("Game\\game.exe"))) + .msg(4, Msg::new().uint(1, 2).uint(2, 1).msg(3, f("en.pak")).text(5, "en-US")) + .msg(4, Msg::new().uint(1, 3).uint(2, 1).msg(3, f("fr.pak")).text(5, "fr-FR")) + .uint(21, ZSTD); + let parsed = parse(&m.0, "en-US").unwrap(); + let names: Vec<_> = parsed.files.iter().map(|f| f.name.as_str()).collect(); + assert_eq!(names, ["Game/game.exe", "en.pak"]); + assert_eq!(parsed.compression, ZSTD); + assert_eq!(parsed.files[0].slices[0], Slice { size: 10, download_size: Some(4), download_sha1: vec![0xBB], content_sha1: vec![0xAA], offset: 0 }); + } + + #[test] + fn leaves_out_add_ons_the_account_does_not_own() { + let f = |name: &str| Msg::new().text(1, name).uint(2, 10).flag(3, false); + let m = Msg::new() + .msg(4, Msg::new().uint(1, 1).uint(2, 0).msg(3, f("base.forge")).uint(4, 9999)) + .msg(4, Msg::new().uint(1, 2).uint(2, 1).msg(3, f("dlc_24/east.forge")).uint(4, 5450)) + .msg(4, Msg::new().uint(1, 3).uint(2, 1).msg(3, f("dlc_25/west.forge")).uint(8, 5451).uint(8, 5452)) + .uint(21, ZSTD); + let parsed = parse_owned(&m.0, "en-US", &[5405, 5452]).unwrap(); + let names: Vec<_> = parsed.files.iter().map(|f| f.name.as_str()).collect(); + assert_eq!(names, ["base.forge", "dlc_25/west.forge"]); + assert_eq!(parsed.skipped, ["dlc_24/east.forge"]); + assert_eq!(parsed.chunks.iter().map(|c| c.id).collect::>(), [1, 3]); + } + + #[test] + fn inflates_both_deflate_framings() { + use std::io::Write; + let mut z = flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::default()); + z.write_all(b"hello").unwrap(); + assert_eq!(decompress(DEFLATE, &z.finish().unwrap()).unwrap(), b"hello"); + let mut r = flate2::write::DeflateEncoder::new(Vec::new(), flate2::Compression::default()); + r.write_all(b"hello").unwrap(); + assert_eq!(decompress(DEFLATE, &r.finish().unwrap()).unwrap(), b"hello"); + } +} diff --git a/app/src/main/rust/stores/src/ubisoft/mod.rs b/app/src/main/rust/stores/src/ubisoft/mod.rs new file mode 100644 index 0000000000..eff0a806f0 --- /dev/null +++ b/app/src/main/rust/stores/src/ubisoft/mod.rs @@ -0,0 +1,6 @@ +pub mod demux; +pub mod ffi; +pub mod install; +pub mod manifest; +pub mod pb; +pub mod service; diff --git a/app/src/main/rust/stores/src/ubisoft/pb.rs b/app/src/main/rust/stores/src/ubisoft/pb.rs new file mode 100644 index 0000000000..f967cb60ca --- /dev/null +++ b/app/src/main/rust/stores/src/ubisoft/pb.rs @@ -0,0 +1,104 @@ +pub use crate::amazon::proto::{Fields, Value}; + +#[derive(Default)] +pub struct Msg(pub Vec); + +impl Msg { + pub fn new() -> Self { + Msg(Vec::new()) + } + + fn varint(&mut self, mut v: u64) { + loop { + let b = (v & 0x7f) as u8; + v >>= 7; + if v == 0 { + self.0.push(b); + return; + } + self.0.push(b | 0x80); + } + } + + fn key(&mut self, field: u32, wire: u8) { + self.varint((field as u64) << 3 | wire as u64); + } + + pub fn uint(mut self, field: u32, v: u64) -> Self { + self.key(field, 0); + self.varint(v); + self + } + + pub fn flag(self, field: u32, v: bool) -> Self { + self.uint(field, v as u64) + } + + pub fn bytes(mut self, field: u32, data: &[u8]) -> Self { + self.key(field, 2); + self.varint(data.len() as u64); + self.0.extend_from_slice(data); + self + } + + pub fn text(self, field: u32, s: &str) -> Self { + self.bytes(field, s.as_bytes()) + } + + pub fn msg(self, field: u32, m: Msg) -> Self { + self.bytes(field, &m.0) + } +} + +/// Every field of a message, in wire order; repeated fields appear once per value. +pub fn fields(data: &[u8]) -> Result)>, String> { + let mut out = Vec::new(); + let mut f = Fields::new(data); + while let Some(v) = f.next_field()? { + out.push(v); + } + Ok(out) +} + +pub fn first<'a>(all: &[(u32, Value<'a>)], field: u32) -> Option> { + all.iter().find(|(n, _)| *n == field).map(|(_, v)| *v) +} + +pub fn sub<'a>(all: &[(u32, Value<'a>)], field: u32) -> Option<&'a [u8]> { + match first(all, field) { + Some(Value::Bytes(b)) => Some(b), + _ => None, + } +} + +pub fn subs<'a>(all: &[(u32, Value<'a>)], field: u32) -> Vec<&'a [u8]> { + all.iter().filter_map(|(n, v)| match (n, v) { (n, Value::Bytes(b)) if *n == field => Some(*b), _ => None }).collect() +} + +pub fn num(all: &[(u32, Value<'_>)], field: u32) -> Option { + match first(all, field) { + Some(Value::Varint(v)) | Some(Value::Fixed64(v)) => Some(v), + Some(Value::Fixed32(v)) => Some(v as u64), + _ => None, + } +} + +pub fn string(all: &[(u32, Value<'_>)], field: u32) -> Option { + sub(all, field).map(|b| String::from_utf8_lossy(b).into_owned()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn round_trips_nested_messages() { + let m = Msg::new().uint(1, 300).msg(2, Msg::new().text(1, "a").text(1, "b")).flag(3, true); + let all = fields(&m.0).unwrap(); + assert_eq!(num(&all, 1), Some(300)); + assert_eq!(num(&all, 3), Some(1)); + let inner = fields(sub(&all, 2).unwrap()).unwrap(); + let texts: Vec<_> = subs(&inner, 1).iter().map(|b| String::from_utf8_lossy(b).into_owned()).collect(); + assert_eq!(texts, ["a", "b"]); + } +} diff --git a/app/src/main/rust/stores/src/ubisoft/service.rs b/app/src/main/rust/stores/src/ubisoft/service.rs new file mode 100644 index 0000000000..bfdddc29a9 --- /dev/null +++ b/app/src/main/rust/stores/src/ubisoft/service.rs @@ -0,0 +1,206 @@ +use serde_json::{json, Value as Json}; + +use super::demux::Demux; +use super::pb::{self, Msg}; + +pub struct Owned { + pub product_id: u32, + pub space_id: String, + pub manifest: String, + pub product_type: u32, + pub state: u32, + pub owned: bool, + pub configuration: String, + pub app_id: String, + pub partner: u32, + pub activation: u32, + pub locked_by_subscription: bool, + pub associations: Vec, + pub download_id: u32, +} + +impl Owned { + /// Whether Connect lists the product among the session's owned ids: owned games and add-ons that are released. + pub fn in_session(&self) -> bool { + self.owned && self.product_type <= 1 && (self.state == 0 || self.state == 3) + } + + pub fn to_json(&self) -> Json { + json!({ + "productId": self.product_id, "spaceId": self.space_id, "manifest": self.manifest, + "productType": self.product_type, "state": self.state, "owned": self.owned, + "configuration": self.configuration, "appId": self.app_id, + "partner": self.partner, "activation": self.activation, "lockedBySubscription": self.locked_by_subscription, + "associations": self.associations, "downloadId": self.download_id, + }) + } +} + +pub struct Ownership { + conn: u32, + next: u64, +} + +fn owned_game(b: &[u8]) -> Result { + let f = pb::fields(b)?; + Ok(Owned { + product_id: pb::num(&f, 1).unwrap_or(0) as u32, + space_id: pb::string(&f, 30).unwrap_or_default(), + manifest: pb::string(&f, 21).unwrap_or_default(), + product_type: pb::num(&f, 8).unwrap_or(0) as u32, + state: pb::num(&f, 13).unwrap_or(0) as u32, + owned: pb::num(&f, 20).map(|v| v == 1).unwrap_or(true), + configuration: pb::string(&f, 17).unwrap_or_default(), + app_id: pb::string(&f, 31).unwrap_or_default(), + partner: pb::num(&f, 38).unwrap_or(0) as u32, + activation: pb::num(&f, 36).unwrap_or(0) as u32, + locked_by_subscription: pb::num(&f, 37) == Some(1), + associations: f.iter().filter_map(|(n, v)| match v { pb::Value::Varint(x) if *n == 16 => Some(*x as u32), _ => None }).collect(), + download_id: pb::num(&f, 5).unwrap_or(0) as u32, + }) +} + +impl Ownership { + /// Opens ownership_service and initializes it, which also returns the account's products. + pub async fn open(d: &mut Demux, ticket: &str, session_id: &str) -> Result<(Self, Vec), String> { + let conn = d.open("ownership_service").await?; + let init = Msg::new().flag(2, true).uint(3, 7); + let mut req = Msg::new().uint(1, 1).msg(2, init).text(30, ticket); + if !session_id.is_empty() { + req = req.text(31, session_id); + } + let raw = d.call(conn, Msg::new().msg(1, req)).await?; + let rsp = pb::fields(pb::sub(&pb::fields(&raw)?, 1).ok_or("no ownership answer")?)?; + let init = pb::fields(pb::sub(&rsp, 2).ok_or("no ownership initialize answer")?)?; + if pb::num(&init, 1) != Some(1) { + return Err("Ubisoft would not list the account's games".into()); + } + let mut games = Vec::new(); + if let Some(list) = pb::sub(&init, 2) { + for g in pb::subs(&pb::fields(list)?, 1) { + games.push(owned_game(g)?); + } + } + Ok((Ownership { conn, next: 2 }, games)) + } + + /// The latest manifest of each product that has one, for games whose ownership entry leaves it out. + pub async fn latest_manifests(&mut self, d: &mut Demux, products: &[u32]) -> Result, String> { + if products.is_empty() { + return Ok(Vec::new()); + } + let id = self.next; + self.next += 1; + let mut req = Msg::new(); + for p in products { + req = req.uint(1, *p as u64); + } + let raw = d.call(self.conn, Msg::new().msg(1, Msg::new().uint(1, id).msg(10, req))).await?; + let rsp = pb::fields(pb::sub(&pb::fields(&raw)?, 1).ok_or("no ownership answer")?)?; + let m = pb::fields(pb::sub(&rsp, 10).ok_or("no manifest answer")?)?; + if pb::num(&m, 1) != Some(1) { + return Err("Ubisoft would not list the latest manifests".into()); + } + let mut out = Vec::new(); + for entry in pb::subs(&m, 2) { + let e = pb::fields(entry)?; + if pb::num(&e, 1) != Some(1) { + continue; + } + if let (Some(product), Some(manifest)) = (pb::num(&e, 2), pb::string(&e, 3).filter(|s| !s.is_empty())) { + out.push((product as u32, manifest)); + } + } + Ok(out) + } + + /// The ticket a game receives in its launcher session (`GetUplayPCTicket`). + pub async fn pc_ticket(&mut self, d: &mut Demux, product_id: u32, ticket: &str, session_id: &str) -> Result { + let id = self.next; + self.next += 1; + let info = Msg::new().uint(1, product_id as u64).uint(2, 0).uint(3, product_id as u64); + let mut req = Msg::new().uint(1, id).msg(14, info).text(30, ticket); + if !session_id.is_empty() { + req = req.text(31, session_id); + } + let raw = d.call(self.conn, Msg::new().msg(1, req)).await?; + let rsp = pb::fields(pb::sub(&pb::fields(&raw)?, 1).ok_or("no ownership answer")?)?; + let t = pb::fields(pb::sub(&rsp, 13).ok_or("no game ticket answer")?)?; + if pb::num(&t, 1) != Some(1) { + return Err(format!("Ubisoft would not issue a game ticket (code {})", pb::num(&t, 3).unwrap_or(0))); + } + pb::string(&t, 2).filter(|s| !s.is_empty()).ok_or_else(|| "Ubisoft sent an empty game ticket".into()) + } + + pub async fn token(&mut self, d: &mut Demux, product_id: u32) -> Result { + let id = self.next; + self.next += 1; + let raw = d.call(self.conn, Msg::new().msg(1, Msg::new().uint(1, id).msg(28, Msg::new().uint(1, product_id as u64)))).await?; + let rsp = pb::fields(pb::sub(&pb::fields(&raw)?, 1).ok_or("no ownership answer")?)?; + let t = pb::fields(pb::sub(&rsp, 27).ok_or("no ownership token answer")?)?; + if pb::num(&t, 1) != Some(1) { + return Err("Ubisoft says this account does not own the game".into()); + } + pb::string(&t, 2).filter(|s| !s.is_empty()).ok_or_else(|| "Ubisoft sent an empty ownership token".into()) + } +} + +pub struct Downloads { + conn: u32, + next: u64, +} + +impl Downloads { + pub async fn open(d: &mut Demux, ownership_token: &str) -> Result { + let conn = d.open("download_service").await?; + let raw = d.call(conn, Msg::new().msg(1, Msg::new().uint(1, 1).msg(2, Msg::new().text(5, ownership_token)))).await?; + let rsp = pb::fields(pb::sub(&pb::fields(&raw)?, 1).ok_or("no download service answer")?)?; + let ok = pb::sub(&rsp, 2).map(pb::fields).transpose()?.and_then(|i| pb::num(&i, 1)) == Some(1); + if !ok { + return Err("Ubisoft's download service refused the ownership token".into()); + } + Ok(Downloads { conn, next: 2 }) + } + + /// Signed CDN URLs for paths under the product's download root, in request order; empty where none was given. + pub async fn sign(&mut self, d: &mut Demux, product_id: u32, paths: &[String]) -> Result>, String> { + let id = self.next; + self.next += 1; + let mut request = Msg::new().uint(1, product_id as u64); + for p in paths { + request = request.text(2, p); + } + let raw = d.call(self.conn, Msg::new().msg(1, Msg::new().uint(1, id).msg(3, Msg::new().msg(1, request)))).await?; + let rsp = pb::fields(pb::sub(&pb::fields(&raw)?, 1).ok_or("no download service answer")?)?; + let urls = pb::fields(pb::sub(&rsp, 3).ok_or("no signed URLs")?)?; + let mut out = Vec::new(); + for r in pb::subs(&urls, 1) { + let r = pb::fields(r)?; + if pb::num(&r, 1).unwrap_or(0) != 0 { + return Err("Ubisoft says this account does not own the game".into()); + } + for group in pb::subs(&r, 2) { + out.push(pb::subs(&pb::fields(group)?, 1).iter().map(|u| String::from_utf8_lossy(u).into_owned()).collect()); + } + } + if out.len() != paths.len() { + return Err(format!("Ubisoft signed {} of {} paths", out.len(), paths.len())); + } + Ok(out) + } +} + +/// Denuvo's activation for a game the account owns: the game's challenge and the product's ownership +/// token go to `denuvo_service`, which answers with the game token the game's aux DLL waits for. +pub async fn denuvo_token(d: &mut Demux, ownership_token: &str, challenge: &[u8]) -> Result, String> { + let conn = d.open("denuvo_service").await?; + let req = Msg::new().uint(1, 1).msg(2, Msg::new().text(1, ownership_token).bytes(2, challenge)); + let raw = d.call(conn, Msg::new().msg(1, req)).await?; + let rsp = pb::fields(pb::sub(&pb::fields(&raw)?, 1).ok_or("no denuvo answer")?)?; + let result = pb::num(&rsp, 2).unwrap_or(0); + if result != 0 { + return Err(format!("Ubisoft's activation service refused the game (result {result})")); + } + let t = pb::fields(pb::sub(&rsp, 3).ok_or("no game token answer")?)?; + pb::sub(&t, 1).filter(|b| !b.is_empty()).map(<[u8]>::to_vec).ok_or_else(|| "Ubisoft sent an empty game token".into()) +} diff --git a/app/src/test/java/com/droiddeck/launcher/DriverMenusAutoTest.kt b/app/src/test/java/com/droiddeck/launcher/DriverMenusAutoTest.kt index 4be7a14e6c..923c1f41fd 100644 --- a/app/src/test/java/com/droiddeck/launcher/DriverMenusAutoTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/DriverMenusAutoTest.kt @@ -7,6 +7,7 @@ import android.os.Looper import com.droiddeck.launcher.session.QuestExperimental import com.droiddeck.launcher.session.SessionPrefs import com.droiddeck.launcher.gpu.TpUbwcHint +import com.droiddeck.launcher.gpu.TurnipDriver import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse import org.junit.Assert.assertNull @@ -34,6 +35,31 @@ class DriverMenusAutoTest { ShadowBuild.setDevice("eureka") } + @Test fun pickingTheAutoDisplayHandsBothHalvesToAuto() { + val activity = Robolectric.buildActivity(Activity::class.java).setup().get() + SessionPrefs.setGpuDriverMode(activity, SessionPrefs.GPU_DRIVERS_MIX) + SessionPrefs.setAndroidDriver(activity, "chosen-display") + SessionPrefs.setLinuxDriver(activity, "chosen-linux") + val menus = DriverMenus(activity, Handler(Looper.getMainLooper())) + menus.selectDriver(TurnipDriver.AUTO, linux = false) + assertEquals(SessionPrefs.GPU_DRIVERS_AUTO, SessionPrefs.gpuDriverMode(activity)) + assertEquals(QuestExperimental.DRIVER, SessionPrefs.androidDriver(activity)) + assertEquals("", SessionPrefs.linuxDriver(activity)) + } + + @Test fun pickingTheAutoDisplayWithNoPairForTheGpuStillPutsTheDisplayOnAuto() { + ShadowBuild.setModel("Generic phone") + ShadowBuild.setDevice("generic") + val activity = Robolectric.buildActivity(Activity::class.java).setup().get() + SessionPrefs.setGpuDriverMode(activity, SessionPrefs.GPU_DRIVERS_MIX) + SessionPrefs.setAndroidDriver(activity, "chosen-display") + SessionPrefs.setLinuxDriver(activity, "chosen-linux") + DriverMenus(activity, Handler(Looper.getMainLooper())).selectDriver(TurnipDriver.AUTO, linux = false) + assertEquals(SessionPrefs.GPU_DRIVERS_AUTO, SessionPrefs.gpuDriverMode(activity)) + assertEquals(TurnipDriver.AUTO, SessionPrefs.androidDriver(activity)) + assertEquals("chosen-linux", SessionPrefs.linuxDriver(activity)) + } + @Test fun selectingAutoUsesTheQuestPairWithoutStartingADownload() { val activity = Robolectric.buildActivity(Activity::class.java).setup().get() SessionPrefs.setGpuDriverMode(activity, SessionPrefs.GPU_DRIVERS_MANUAL) @@ -71,7 +97,7 @@ class DriverMenusAutoTest { val menus = DriverMenus(activity, Handler(Looper.getMainLooper())) menus.refreshDrivers() assertEquals(SessionPrefs.TP_UBWC_ON, menus.tpUbwcMode) - assertFalse(menus.state().tpUbwcRestartRequired) + assertFalse(menus.tpUbwcRestartRequired()) } @Test fun changingUbwcBeforeTheFirstSessionDoesNotRequireRestart() { @@ -80,7 +106,7 @@ class DriverMenusAutoTest { menus.refreshDrivers() SessionPrefs.setTpUbwcMode(activity, SessionPrefs.TP_UBWC_ON) menus.refreshDrivers() - assertFalse(menus.state().tpUbwcRestartRequired) + assertFalse(menus.tpUbwcRestartRequired()) } @Test fun recreatingTheMenuKeepsTheActiveHintRestartWarning() { @@ -89,6 +115,6 @@ class DriverMenusAutoTest { SessionPrefs.setTpUbwcMode(activity, SessionPrefs.TP_UBWC_ON) val menus = DriverMenus(activity, Handler(Looper.getMainLooper())) menus.refreshDrivers() - assertTrue(menus.state().tpUbwcRestartRequired) + assertTrue(menus.tpUbwcRestartRequired()) } } diff --git a/app/src/test/java/com/droiddeck/launcher/PermissionAssumptions.kt b/app/src/test/java/com/droiddeck/launcher/PermissionAssumptions.kt new file mode 100644 index 0000000000..18f0cc312e --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/PermissionAssumptions.kt @@ -0,0 +1,16 @@ +package com.droiddeck.launcher + +import java.nio.file.Files +import org.junit.Assume.assumeTrue + +/** Root ignores file mode bits, so tests that rely on a denied read or write cannot run as root or without POSIX permissions. */ +fun assumeFilePermissionsEnforced() { + val probe = Files.createTempDirectory("permission-probe").toFile() + try { + assumeTrue("no POSIX file permissions", Files.getFileStore(probe.toPath()).supportsFileAttributeView("posix")) + assumeTrue("file permissions are not enforced for this user", probe.setReadable(false, false) && !probe.canRead()) + } finally { + probe.setReadable(true, false) + probe.delete() + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/ProtonMenuAutoTest.kt b/app/src/test/java/com/droiddeck/launcher/ProtonMenuAutoTest.kt index 7aff486917..14dbdb8338 100644 --- a/app/src/test/java/com/droiddeck/launcher/ProtonMenuAutoTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/ProtonMenuAutoTest.kt @@ -83,7 +83,9 @@ class ProtonMenuAutoTest { private fun install(name: String): File = File(root, "root/.local/share/Steam/compatibilitytools.d/$name").apply { mkdirs() - File(this, "toolmanifest.vdf").writeText("manifest") + File(this, "toolmanifest.vdf").writeText("\"manifest\" { \"commandline\" \"/proton %verb%\" }") + File(this, "compatibilitytool.vdf").writeText("\"from_oslist\" \"windows\"") + File(this, "proton").apply { writeText(""); setExecutable(true) } } private fun waitUntil(done: () -> Boolean) { diff --git a/app/src/test/java/com/droiddeck/launcher/core/DownloaderCancelTest.kt b/app/src/test/java/com/droiddeck/launcher/core/DownloaderCancelTest.kt new file mode 100644 index 0000000000..8d49cdeb00 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/core/DownloaderCancelTest.kt @@ -0,0 +1,77 @@ +package com.droiddeck.launcher.core + +import org.junit.After +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Test +import java.io.InputStream +import java.util.concurrent.CopyOnWriteArrayList +import java.util.concurrent.CountDownLatch +import java.util.concurrent.ExecutorService +import java.util.concurrent.ThreadPoolExecutor +import java.util.concurrent.TimeUnit + +class DownloaderCancelTest { + private val previousHandler = Thread.getDefaultUncaughtExceptionHandler() + private val uncaught = CopyOnWriteArrayList() + + @After fun tearDown() { + Thread.setDefaultUncaughtExceptionHandler(previousHandler) + } + + private val cancelExecutor: ExecutorService + get() = Downloader::class.java.getDeclaredField("CANCEL_EXECUTOR") + .apply { isAccessible = true } + .get(null) as ExecutorService + + private fun cancelWhileClosingThrows(failure: RuntimeException): Thread { + Thread.setDefaultUncaughtExceptionHandler { _, error -> uncaught += error } + val closed = CountDownLatch(1) + var closer: Thread? = null + val stream = object : InputStream() { + override fun read(): Int = -1 + override fun close() { + closer = Thread.currentThread() + closed.countDown() + throw failure + } + } + val cancellation = Downloader.Cancellation() + Downloader.Cancellation::class.java.getDeclaredMethod("attach", InputStream::class.java) + .apply { isAccessible = true } + .invoke(cancellation, stream) + + cancellation.cancel() + + assertTrue(closed.await(5, TimeUnit.SECONDS)) + return closer!! + } + + private fun survivesItsTask(closer: Thread): Boolean { + val pool = cancelExecutor as ThreadPoolExecutor + val deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(5) + while (System.nanoTime() < deadline) { + if (!closer.isAlive) return false + if (pool.activeCount == 0 && closer.state == Thread.State.TIMED_WAITING) return true + Thread.sleep(5) + } + error("the cancel thread neither ended nor went back to waiting for work") + } + + @Test fun unbalancedTimeoutOnCancelDoesNotCrash() { + val closer = cancelWhileClosingThrows(IllegalStateException("Unbalanced enter/exit")) + + assertTrue(survivesItsTask(closer)) + assertEquals(emptyList(), uncaught.toList()) + } + + @Test fun otherCloseFailuresStillSurface() { + val failure = UnsupportedOperationException("close") + val closer = cancelWhileClosingThrows(failure) + + closer.join(TimeUnit.SECONDS.toMillis(5)) + assertFalse(closer.isAlive) + assertEquals(listOf(failure), uncaught.toList()) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/files/FileOpsCopyTest.kt b/app/src/test/java/com/droiddeck/launcher/files/FileOpsCopyTest.kt index e92ca2aec7..71aa842b3e 100644 --- a/app/src/test/java/com/droiddeck/launcher/files/FileOpsCopyTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/files/FileOpsCopyTest.kt @@ -1,5 +1,6 @@ package com.droiddeck.launcher.files +import com.droiddeck.launcher.assumeFilePermissionsEnforced import java.io.File import java.util.concurrent.CancellationException import java.nio.file.Files @@ -114,6 +115,7 @@ class FileOpsCopyTest { } @Test fun anOverwriteStillRefusesAReadOnlyDestination() { + assumeFilePermissionsEnforced() assumeTrue(Files.getFileStore(tmp.root.toPath()).supportsFileAttributeView("posix")) val src = tmp.newFile("source").apply { writeText("replacement") } val dst = tmp.newFile("dest").apply { writeText("original") } diff --git a/app/src/test/java/com/droiddeck/launcher/files/StorageRootsVolumeTest.kt b/app/src/test/java/com/droiddeck/launcher/files/StorageRootsVolumeTest.kt new file mode 100644 index 0000000000..b7d9cf520c --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/files/StorageRootsVolumeTest.kt @@ -0,0 +1,55 @@ +package com.droiddeck.launcher.files + +import android.os.Environment +import android.os.Process +import android.os.storage.StorageManager +import org.junit.Assert.assertEquals +import org.junit.Assert.assertTrue +import org.junit.Rule +import org.junit.Test +import org.junit.rules.TemporaryFolder +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.Shadows.shadowOf +import org.robolectric.annotation.Config +import org.robolectric.shadows.StorageVolumeBuilder +import java.io.File + +@RunWith(RobolectricTestRunner::class) +@Config(manifest = Config.NONE) +class StorageRootsVolumeTest { + @get:Rule val temporary = TemporaryFolder() + + private fun addUsbVolume(): File { + val mount = temporary.newFolder("usbstick") + val context = RuntimeEnvironment.getApplication() + val volume = StorageVolumeBuilder("public:8,1", mount, "USB drive", Process.myUserHandle(), Environment.MEDIA_MOUNTED) + .setIsPrimary(false) + .setIsEmulated(false) + .setIsRemovable(true) + .build() + shadowOf(context.getSystemService(StorageManager::class.java)).addStorageVolume(volume) + return mount + } + + @Config(sdk = [29]) + @Test fun volumesWithoutDirectoryApiAreListedOnAndroid10() { + addUsbVolume() + + val usb = StorageRoots.list(RuntimeEnvironment.getApplication()).single { it.label == "USB drive" } + + assertTrue(usb.removable) + } + + @Config(sdk = [30]) + @Test fun volumeDirectoryIsStillUsedOnAndroid11() { + val mount = addUsbVolume() + + val usb = StorageRoots.list(RuntimeEnvironment.getApplication()).single { it.label == "USB drive" } + + assertTrue(usb.removable) + assertTrue(usb.readable) + assertEquals(mount.absoluteFile, usb.dir.absoluteFile) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/frontend/AddedExesTest.kt b/app/src/test/java/com/droiddeck/launcher/frontend/AddedExesTest.kt index 5e8eed0c83..d8626a3bb0 100644 --- a/app/src/test/java/com/droiddeck/launcher/frontend/AddedExesTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/frontend/AddedExesTest.kt @@ -1,5 +1,6 @@ package com.droiddeck.launcher.frontend +import com.droiddeck.launcher.assumeFilePermissionsEnforced import android.os.Environment import com.droiddeck.launcher.session.SessionPrefs import java.io.File @@ -308,6 +309,7 @@ class AddedExesTest { } @Test fun anExeInAFolderTheAppCannotReadIsNotAdded() { + assumeFilePermissionsEnforced() noGamesFolders() val outside = exe(File(tmp.newFolder("elsewhere"), "Game/Game.exe")) outside.parentFile!!.setReadable(false) diff --git a/app/src/test/java/com/droiddeck/launcher/frontend/LibraryShortcutsLastPlayedTest.kt b/app/src/test/java/com/droiddeck/launcher/frontend/LibraryShortcutsLastPlayedTest.kt index cc9a1ac7df..baacb804e3 100644 --- a/app/src/test/java/com/droiddeck/launcher/frontend/LibraryShortcutsLastPlayedTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/frontend/LibraryShortcutsLastPlayedTest.kt @@ -45,6 +45,30 @@ class LibraryShortcutsLastPlayedTest { assertEquals(mapOf(3388195926L to 1792931476L, 1234L to 1700000000L), Library.shortcutsLastPlayed(root)) } + @Test fun aLaunchedAddedGameRemembersItsPlayTime() { + val context = org.robolectric.RuntimeEnvironment.getApplication() + val added = Library.SteamGame(-1, "World of Warcraft", null, Library.ADDED, gameId = 0xC0FFEE0002000000uL.toLong()) + val steam = Library.SteamGame(570, "Dota 2", null, "/steam") + assertEquals(0L, Library.recordedPlay(context, added.gameId)) + val before = System.currentTimeMillis() / 1000 + Library.recordPlayed(context, added) + Library.recordPlayed(context, steam) + assert(Library.recordedPlay(context, added.gameId) >= before) + assertEquals(0L, Library.recordedPlay(context, steam.gameId)) + } + + @Test fun anAddedGameBuiltAgainKeepsItsRecordedPlayTime() { + val context = org.robolectric.RuntimeEnvironment.getApplication() + val dir = tmp.newFolder("Celeste") + val exe = File(dir, "Celeste.exe").apply { writeText("x") } + val g = AddedGames.Game(dir, "Celeste", exe, "/g/Celeste.exe", "/g", 0x82000003L, 0x82000003L, emptyList()) + val shown = Library.addedGame(context, g, emptyList()) + assertEquals(0L, shown.lastPlayed) + Library.recordPlayed(context, shown) + assertEquals(Library.recordedPlay(context, g.gameId), Library.addedGame(context, g, emptyList()).lastPlayed) + assertEquals(1900000000L, Library.addedGame(context, g, emptyList(), lastPlayed = 1900000000L).lastPlayed) + } + @Test fun missingOrBrokenShortcutsGiveNothing() { val root = steamRoot() assertEquals(emptyMap(), Library.shortcutsLastPlayed(root)) diff --git a/app/src/test/java/com/droiddeck/launcher/gpu/AutoPicksTest.kt b/app/src/test/java/com/droiddeck/launcher/gpu/AutoPicksTest.kt index 83f32a36eb..2f5c427679 100644 --- a/app/src/test/java/com/droiddeck/launcher/gpu/AutoPicksTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/gpu/AutoPicksTest.kt @@ -56,7 +56,7 @@ class AutoPicksTest { } @Test fun aGpuTheManifestDoesNotCoverUsesTheBuiltInTable() { - assertEquals(DriverPairs.WN_BALANCED, DriverPairs.recommendedKey(gpu(830), pairs, AutoPicks.parse(manifest)!!)) + assertEquals(DriverPairs.ddTurnip("DD-Turnip-v0.3.0"), DriverPairs.recommendedKey(gpu(830), pairs, AutoPicks.parse(manifest)!!)) } @Test fun anotherSchemaOrNoUsableRulesIsIgnored() { diff --git a/app/src/test/java/com/droiddeck/launcher/gpu/DriverBundleTest.kt b/app/src/test/java/com/droiddeck/launcher/gpu/DriverBundleTest.kt index b9c0ce819b..24d47f60e4 100644 --- a/app/src/test/java/com/droiddeck/launcher/gpu/DriverBundleTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/gpu/DriverBundleTest.kt @@ -101,6 +101,33 @@ class DriverBundleTest { assertTrue(DriverBundle.all(context).isEmpty()) } + @Test fun aDisplayPickedAwayFromABundleTakesItsRuntimeHalfWithIt() { + val b = DriverBundle.install(context, bundle()) + DriverBundle.select(context, b) + DriverBundle.selectDisplay(context, TurnipDriver.AUTO) + assertEquals(TurnipDriver.AUTO, SessionPrefs.androidDriver(context)) + assertEquals("", SessionPrefs.linuxDriver(context)) + assertNull(DriverBundle.active(context)) + + DriverBundle.select(context, b) + DriverBundle.selectDisplay(context, "turnip25.1.0") + assertEquals("turnip25.1.0", SessionPrefs.androidDriver(context)) + assertEquals("", SessionPrefs.linuxDriver(context)) + } + + @Test fun aDisplayPickOutsideABundleKeepsTheChosenRuntime() { + SessionPrefs.setLinuxDriver(context, "my-linux-driver") + SessionPrefs.setAndroidDriver(context, "turnip25.1.0") + DriverBundle.selectDisplay(context, TurnipDriver.AUTO) + assertEquals(TurnipDriver.AUTO, SessionPrefs.androidDriver(context)) + assertEquals("my-linux-driver", SessionPrefs.linuxDriver(context)) + + val b = DriverBundle.install(context, bundle()) + DriverBundle.select(context, b) + DriverBundle.selectDisplay(context, b.androidId) + assertEquals(b.linuxId, SessionPrefs.linuxDriver(context)) + } + @Test fun aRefusedHalfLeavesNothingInstalled() { try { DriverBundle.install(context, bundle(linuxLibc = "libc.so")) diff --git a/app/src/test/java/com/droiddeck/launcher/gpu/DriverPairsTest.kt b/app/src/test/java/com/droiddeck/launcher/gpu/DriverPairsTest.kt index d1c048b61b..b234260030 100644 --- a/app/src/test/java/com/droiddeck/launcher/gpu/DriverPairsTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/gpu/DriverPairsTest.kt @@ -116,7 +116,7 @@ class DriverPairsTest { } @Test - fun adreno840GetsTheNewestOfficialDdTurnip() { + fun everyAdrenoGenerationGetsTheNewestOfficialDdTurnip() { fun bundle(tag: String) = TurnipReleases.Asset("DroidDeck", tag, "$tag.zip", "https://example/$tag", 6_000_000, false, "Android + Linux", "0".repeat(64), DriverPairs.ddTurnip(tag), bundle = true) val pairs = DriverPairs.from(TurnipReleases.Check( @@ -126,12 +126,12 @@ class DriverPairsTest { emptyList(), emptyList(), 0L, )) assertEquals(DriverPairs.ddTurnip("DD-Turnip-v1.0.0"), DriverPairs.recommendedKey(gpu(840), pairs)) - assertEquals("other 8xx keep WinNative", DriverPairs.WN_BALANCED, DriverPairs.recommendedKey(gpu(830), pairs)) - assertEquals(DriverPairs.WN_BALANCED, DriverPairs.recommendedKey(gpu(825), pairs)) + assertEquals(DriverPairs.ddTurnip("DD-Turnip-v1.0.0"), DriverPairs.recommendedKey(gpu(830), pairs)) + assertEquals(DriverPairs.ddTurnip("DD-Turnip-v1.0.0"), DriverPairs.recommendedKey(gpu(825), pairs)) assertEquals(DriverPairs.ddTurnip("DD-Turnip-v1.0.0"), DriverPairs.recommendedKey(gpu(740), pairs)) assertEquals(DriverPairs.ddTurnip("DD-Turnip-v1.0.0"), DriverPairs.recommendedKey(gpu(722), pairs)) - assertEquals(DriverPairs.BANNER_ONEUI, DriverPairs.recommendedKey(gpu(740, oneUi = true), pairs)) - assertEquals(DriverPairs.BANNER, DriverPairs.recommendedKey(gpu(650), pairs)) + assertEquals(DriverPairs.ddTurnip("DD-Turnip-v1.0.0"), DriverPairs.recommendedKey(gpu(740, oneUi = true), pairs)) + assertEquals(DriverPairs.ddTurnip("DD-Turnip-v1.0.0"), DriverPairs.recommendedKey(gpu(650), pairs)) } @Test diff --git a/app/src/test/java/com/droiddeck/launcher/gpu/SystemVulkanPruneAbsentRuntimeTest.kt b/app/src/test/java/com/droiddeck/launcher/gpu/SystemVulkanPruneAbsentRuntimeTest.kt new file mode 100644 index 0000000000..8a740f1681 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/gpu/SystemVulkanPruneAbsentRuntimeTest.kt @@ -0,0 +1,26 @@ +package com.droiddeck.launcher.gpu + +import android.content.Context +import java.io.File +import org.junit.Assert.assertTrue +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.annotation.Config + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [28], manifest = Config.NONE) +class SystemVulkanPruneAbsentRuntimeTest { + @Test fun pruningWithoutTheRuntimeDoesNotThrowAndLeavesFilesAlone() { + val context: Context = RuntimeEnvironment.getApplication() + val base = File(context.filesDir, "system_vulkan").apply { mkdirs() } + val stage = File(base, ".stage-" + java.util.UUID.randomUUID()).apply { mkdir() } + val version = File(base, "a".repeat(64)).apply { mkdir() } + + SystemVulkanDriver.pruneUnused(context, true) + + assertTrue(stage.exists()) + assertTrue(version.exists()) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/gpu/TurnipReleasesCarryOverTest.kt b/app/src/test/java/com/droiddeck/launcher/gpu/TurnipReleasesCarryOverTest.kt new file mode 100644 index 0000000000..9bd3e0d655 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/gpu/TurnipReleasesCarryOverTest.kt @@ -0,0 +1,103 @@ +package com.droiddeck.launcher.gpu + +import android.content.Context +import androidx.test.core.app.ApplicationProvider +import org.json.JSONArray +import org.json.JSONObject +import org.junit.Assert.assertEquals +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config + +@RunWith(RobolectricTestRunner::class) +@Config(manifest = Config.NONE) +class TurnipReleasesCarryOverTest { + private val context: Context = ApplicationProvider.getApplicationContext() + private val a644 = GpuInfo("Adreno 644", 644, GpuInfo.familyOf(true, 644), "", false) + + private fun asset(source: String, tag: String, name: String, pair: String, linux: Boolean = false, bundle: Boolean = false) = + JSONObject().put("source", source).put("tag", tag).put("name", name).put("url", "https://example/$name").put("size", 1L) + .put("linux", linux).put("label", "label").put("sha256", "0".repeat(64)).put("pair", pair).put("bundle", bundle) + + private val dd = asset("DroidDeck", "v0.4.0", "DD-Turnip-v0.4.0.zip", DriverPairs.ddTurnip("v0.4.0"), bundle = true) + private fun banner(tag: String) = listOf(asset("Banners-Turnip", tag, "Turnip-$tag.zip", DriverPairs.BANNER), + asset("Banners-Turnip", tag, "Turnip-$tag-Linux.zip", DriverPairs.BANNER, linux = true)) + + private fun previous() = JSONObject().put("assets", JSONArray().put(dd).apply { banner("r5").forEach(::put) }) + .put("latest", JSONArray().put(JSONObject().put("source", "DroidDeck").put("tag", "v0.4.0")) + .put(JSONObject().put("source", "Banners-Turnip").put("tag", "r5"))) + + private fun pick(assets: JSONArray, latest: JSONArray, failed: List): String? { + val stored = JSONObject().put("assets", assets).put("latest", latest).put("failed", JSONArray(failed)).put("checkedAt", 1L) + context.getSharedPreferences("turnip_releases", Context.MODE_PRIVATE).edit().putString("latest", stored.toString()).commit() + return DriverPairs.recommendedKey(a644, DriverPairs.from(TurnipReleases.cached(context))) + } + + @Test fun aRepoThatFailedToAnswerKeepsItsDriversFromTheLastCheck() { + val assets = JSONArray().apply { banner("r6").forEach(::put) } + val latest = JSONArray().put(JSONObject().put("source", "Banners-Turnip").put("tag", "r6")) + TurnipReleases.carryOver(listOf("DroidDeck"), previous(), assets, latest) + assertEquals(DriverPairs.ddTurnip("v0.4.0"), pick(assets, latest, listOf("DroidDeck"))) + assertEquals(listOf("r6", "r6", "v0.4.0"), (0 until assets.length()).map { assets.getJSONObject(it).getString("tag") }) + assertEquals(listOf("Banners-Turnip" to "r6", "DroidDeck" to "v0.4.0"), + (0 until latest.length()).map { latest.getJSONObject(it).let { l -> l.getString("source") to l.getString("tag") } }) + } + + @Test fun reposThatAnsweredAreNotDuplicatedOrRevived() { + val assets = JSONArray().apply { banner("r6").forEach(::put) } + val latest = JSONArray() + TurnipReleases.carryOver(emptyList(), previous(), assets, latest) + assertEquals(2, assets.length()) + assertEquals(DriverPairs.BANNER, pick(assets, latest, emptyList())) + } + + @Test fun withNoEarlierCheckAFailedRepoContributesNothing() { + val assets = JSONArray().apply { banner("r6").forEach(::put) } + val latest = JSONArray() + TurnipReleases.carryOver(listOf("DroidDeck"), null, assets, latest) + assertEquals(2, assets.length()) + assertEquals(0, latest.length()) + } + + @Test fun refreshKeepsTheDriversOfARepoThatFailedToAnswer() { + context.getSharedPreferences("turnip_releases", Context.MODE_PRIVATE).edit().putString("latest", previous().toString()).commit() + val release = JSONObject().put("tag_name", "r6").put("assets", JSONArray().apply { + for (name in listOf("Turnip-r6.zip", "Turnip-r6-Linux.zip")) put(JSONObject().put("name", name) + .put("browser_download_url", "https://example/$name").put("size", 1L).put("digest", "sha256:" + "0".repeat(64))) + }) + val original = TurnipReleases.fetch + TurnipReleases.fetch = { _, _, url -> + if ("Banners-Turnip" in url) JSONArray().put(release).toString() else throw java.io.IOException("rate limited") + } + try { + val check = TurnipReleases.refresh(context) + assertEquals(listOf("r6", "r6", "v0.4.0"), check.assets.map { it.tag }) + assertEquals(listOf("Banners-Turnip" to "r6", "DroidDeck" to "v0.4.0"), check.latest) + assertEquals(true, "DroidDeck" in check.failed) + assertEquals(DriverPairs.ddTurnip("v0.4.0"), DriverPairs.recommendedKey(a644, DriverPairs.from(check))) + } finally { TurnipReleases.fetch = original } + } + + @Test fun aRepoWhoseAnswerBreaksHalfwayKeepsOnlyItsDriversFromTheLastCheck() { + context.getSharedPreferences("turnip_releases", Context.MODE_PRIVATE).edit().putString("latest", previous().toString()).commit() + val release = JSONObject().put("tag_name", "r6").put("assets", JSONArray().apply { + for (name in listOf("Turnip-r6.zip", "Turnip-r6-Linux.zip")) put(JSONObject().put("name", name) + .put("browser_download_url", "https://example/$name").put("size", 1L).put("digest", "sha256:" + "0".repeat(64))) + }) + val original = TurnipReleases.fetch + TurnipReleases.fetch = { _, _, url -> + when { + "Banners-Turnip" in url -> JSONArray().put(release).put(JSONObject().put("assets", JSONArray())).toString() + "api.github.com" in url -> "[]" + else -> throw java.io.IOException("offline") + } + } + try { + val check = TurnipReleases.refresh(context) + assertEquals(true, "Banners-Turnip" in check.failed) + assertEquals(listOf("r5", "r5"), check.assets.map { it.tag }) + assertEquals(listOf("Banners-Turnip" to "r5"), check.latest) + } finally { TurnipReleases.fetch = original } + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/hud/DroidDeckHudPanelTest.kt b/app/src/test/java/com/droiddeck/launcher/hud/DroidDeckHudPanelTest.kt index 05c6d2e679..a439e703b9 100644 --- a/app/src/test/java/com/droiddeck/launcher/hud/DroidDeckHudPanelTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/hud/DroidDeckHudPanelTest.kt @@ -2,12 +2,15 @@ package com.droiddeck.launcher.hud import android.graphics.Bitmap import android.graphics.Canvas +import android.graphics.RectF import android.os.Looper import android.os.SystemClock import android.view.MotionEvent import android.view.View import android.view.ViewConfiguration import android.widget.FrameLayout +import com.droiddeck.launcher.session.SessionPrefs +import com.droiddeck.launcher.ui.Themes import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse import org.junit.Assert.assertTrue @@ -169,6 +172,40 @@ class DroidDeckHudPanelTest { after.getPixel(x, graph.top.toInt() + 2), after.getPixel(x, y)) } + @Test fun stripMarkUpdatesAfterThemeChangesWithoutChangingSize() { + val app = RuntimeEnvironment.getApplication() + val oldTheme = SessionPrefs.theme(app) + val parent = FrameLayout(app) + val view = DroidDeckHudView(app) + parent.addView(view) + parent.layout(0, 0, 2400, 1080) + fun render(theme: String): Bitmap { + SessionPrefs.setTheme(app, theme) + view.applyConfig(DroidDeckHudConfig.encode(DroidDeckHudConfig.DEFAULTS + ("hudSize" to "strip"))) + view.showForTest(SNAPSHOT, "DXVK", 0L) + val unspecified = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED) + view.measure(unspecified, unspecified) + view.layout(0, 0, view.measuredWidth, view.measuredHeight) + return Bitmap.createBitmap(view.width, view.height, Bitmap.Config.ARGB_8888).also { view.draw(Canvas(it)) } + } + try { + val first = render(Themes.GRAPHITE) + val mark = DroidDeckHudView::class.java.getDeclaredField("headerMark").let { + it.isAccessible = true + RectF(it.get(view) as RectF) + } + val second = render(Themes.ELECTRIC) + val x = (mark.left + mark.width() / 2f).toInt() + val y = mark.centerY().toInt() + assertEquals(first.width, second.width) + assertEquals(first.height, second.height) + assertTrue("the mark orb follows the current theme", first.getPixel(x, y) != second.getPixel(x, y)) + assertEquals(View.LAYER_TYPE_NONE, view.layerType) + } finally { + SessionPrefs.setTheme(app, oldTheme) + } + } + @Test fun stripFollowsTheHudScaleInHeightAndKeepsTheFullWidth() { val app = RuntimeEnvironment.getApplication() val parent = FrameLayout(app) diff --git a/app/src/test/java/com/droiddeck/launcher/input/BottomScreenGuestsTest.kt b/app/src/test/java/com/droiddeck/launcher/input/BottomScreenGuestsTest.kt index d9c959dae9..3bc8e76afa 100644 --- a/app/src/test/java/com/droiddeck/launcher/input/BottomScreenGuestsTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/input/BottomScreenGuestsTest.kt @@ -48,6 +48,17 @@ class BottomScreenGuestsTest { assertEquals(null, BottomScreenGuests.holder(null, null, homes)) } + @Test fun anAppThatArrivedBeforeDroidDeckCoveredTheScreenAgainNoLongerHoldsIt() { + assertFalse(BottomScreenGuests.current(arrivedAt = 1_000, ownShownAt = 2_000)) + assertTrue(BottomScreenGuests.current(arrivedAt = 3_000, ownShownAt = 2_000)) + assertTrue(BottomScreenGuests.current(arrivedAt = 2_000, ownShownAt = 2_000)) + assertTrue(BottomScreenGuests.current(arrivedAt = 1_000, ownShownAt = null)) + fun holder(arrivedAt: Long, ownShownAt: Long) = BottomScreenGuests.holder(null, + "com.android.chrome".takeIf { BottomScreenGuests.current(arrivedAt, ownShownAt) }, homes) + assertEquals(null, holder(arrivedAt = 1_000, ownShownAt = 2_000)) + assertEquals("com.android.chrome", holder(arrivedAt = 3_000, ownShownAt = 2_000)) + } + @Test fun keyboardsAndOverlaysNeverTakeTheScreen() { assertTrue(BottomScreenGuests.ignorable(AccessibilityWindowInfo.TYPE_INPUT_METHOD)) assertTrue(BottomScreenGuests.ignorable(AccessibilityWindowInfo.TYPE_ACCESSIBILITY_OVERLAY)) diff --git a/app/src/test/java/com/droiddeck/launcher/input/DirectTouchHoldTest.kt b/app/src/test/java/com/droiddeck/launcher/input/DirectTouchHoldTest.kt index 9ef44f3607..20c7fe451f 100644 --- a/app/src/test/java/com/droiddeck/launcher/input/DirectTouchHoldTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/input/DirectTouchHoldTest.kt @@ -141,6 +141,26 @@ class DirectTouchHoldTest { assertEquals(listOf("true at 40.0,30.0", "false at 40.0,30.0"), buttons) } + @Test fun cancelTouchEndsALiveTouchAndKeepsAHeldButton() { + touch(MotionEvent.ACTION_DOWN) + touch(MotionEvent.ACTION_MOVE, 60f) + hold.cancelTouch() + assertEquals(listOf("down 0 at 40.0,30.0", "move 0 at 60.0,30.0", "cancel"), touches) + assertTrue(buttons.isEmpty()) + + touches.clear() + hold.configure(TouchGestureConfig(longPressDelay = 500, + directHoldBehavior = TouchGestureConfig.MOUSE_BEHAVIOR_HOLD)) + touch(MotionEvent.ACTION_DOWN) + advance(500) + hold.cancelTouch() + advance(1000) + assertEquals(listOf("true at 40.0,30.0"), buttons) + assertTrue(touches.isEmpty()) + touch(MotionEvent.ACTION_UP) + assertEquals(listOf("true at 40.0,30.0", "false at 40.0,30.0"), buttons) + } + @Test fun cancelForwardsACancelWhenTheTouchIsLive() { touch(MotionEvent.ACTION_DOWN) touch(MotionEvent.ACTION_MOVE, 60f) diff --git a/app/src/test/java/com/droiddeck/launcher/input/DualScreenTest.kt b/app/src/test/java/com/droiddeck/launcher/input/DualScreenTest.kt index e50fcbadf4..c510dc72f3 100644 --- a/app/src/test/java/com/droiddeck/launcher/input/DualScreenTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/input/DualScreenTest.kt @@ -12,7 +12,10 @@ import org.junit.Assert.assertTrue import org.junit.Test import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner +import org.robolectric.Shadows.shadowOf import org.robolectric.annotation.Config +import org.robolectric.shadows.ShadowDisplayManager +import org.robolectric.util.ReflectionHelpers @RunWith(RobolectricTestRunner::class) @Config(manifest = Config.NONE) @@ -51,6 +54,75 @@ class DualScreenTest { assertTrue(DualScreen.hasOwnTouchPanel(main, null, listOf(1920 to 1080))) } + @Test @Config(sdk = [34]) fun aWiredExternalDisplayDocksTheDevice() { + withDisplay(EXTERNAL, "HDMI Screen") { assertTrue(DualScreen.dockedToDisplay(displays, Display.DEFAULT_DISPLAY)) } + assertFalse(DualScreen.dockedToDisplay(displays, Display.DEFAULT_DISPLAY)) + } + + @Test @Config(sdk = [34]) fun glassesVirtualOverlayAndSwitchedOffDisplaysDoNotDock() { + withDisplay(EXTERNAL, "VITURE Luma Ultra") { assertFalse(DualScreen.dockedToDisplay(displays, Display.DEFAULT_DISPLAY)) } + withDisplay(VIRTUAL, "Screen recording") { assertFalse(DualScreen.dockedToDisplay(displays, Display.DEFAULT_DISPLAY)) } + withDisplay(OVERLAY, "Overlay #1") { assertFalse(DualScreen.dockedToDisplay(displays, Display.DEFAULT_DISPLAY)) } + withDisplay(EXTERNAL, "HDMI Screen") { display -> + shadowOf(display).setState(Display.STATE_OFF) + assertFalse(DualScreen.dockedToDisplay(displays, Display.DEFAULT_DISPLAY)) + } + } + + @Test @Config(sdk = [34]) fun dockingLeavesTheBuiltInPanelWhenDualScreenIsForcedOn() { + withDisplay(INTERNAL, "Bottom screen") { bottom -> + withDisplay(EXTERNAL, "HDMI Screen") { + assertTrue(DualScreen.dockedToDisplay(displays, Display.DEFAULT_DISPLAY)) + assertEquals(bottom.displayId, DualScreen.display(context, displays, Display.DEFAULT_DISPLAY, DualScreen.MODE_ON)?.displayId) + assertNull(DualScreen.display(context, displays, Display.DEFAULT_DISPLAY, DualScreen.MODE_AUTO)) + assertNull(DualScreen.display(context, displays, Display.DEFAULT_DISPLAY, DualScreen.MODE_OFF)) + } + } + } + + @Test @Config(sdk = [34]) fun glassesOnUsbDoNotHideAnUnnamedDockDisplay() { + val state = ReflectionHelpers.getField>( + com.droiddeck.launcher.glasses.Glasses, "_state") + val before = state.value + try { + state.value = before.copy(present = true) + withDisplay(EXTERNAL, "") { assertFalse(DualScreen.dockedToDisplay(displays, Display.DEFAULT_DISPLAY)) } + withDisplay(EXTERNAL, "VITURE Luma Ultra") { + withDisplay(EXTERNAL, "") { assertTrue(DualScreen.dockedToDisplay(displays, Display.DEFAULT_DISPLAY)) } + } + withDisplay(EXTERNAL, "") { glasses -> + state.value = before.copy(present = true, displayId = glasses.displayId) + withDisplay(EXTERNAL, "") { assertTrue(DualScreen.dockedToDisplay(displays, Display.DEFAULT_DISPLAY)) } + } + } finally { state.value = before } + } + + @Test fun onlyTheMomentOfDockingClosesAnAutomaticBottomPanel() { + assertTrue(DualScreen.leavesBottomOnDock(wasDocked = false, docked = true, DualScreen.MODE_AUTO, automaticOnBuiltIn = true)) + assertFalse(DualScreen.leavesBottomOnDock(wasDocked = true, docked = true, DualScreen.MODE_AUTO, automaticOnBuiltIn = true)) + assertFalse(DualScreen.leavesBottomOnDock(wasDocked = false, docked = false, DualScreen.MODE_AUTO, automaticOnBuiltIn = true)) + assertFalse(DualScreen.leavesBottomOnDock(wasDocked = false, docked = true, DualScreen.MODE_AUTO, automaticOnBuiltIn = false)) + assertFalse(DualScreen.leavesBottomOnDock(wasDocked = false, docked = true, DualScreen.MODE_ON, automaticOnBuiltIn = true)) + assertFalse(DualScreen.leavesBottomOnDock(wasDocked = false, docked = true, DualScreen.MODE_OFF, automaticOnBuiltIn = true)) + } + + private companion object { + const val INTERNAL = 1 + const val EXTERNAL = 2 + const val OVERLAY = 4 + const val VIRTUAL = 5 + } + + private fun withDisplay(type: Int, name: String, check: (Display) -> Unit) { + val id = ShadowDisplayManager.addDisplay("w640dp-h480dp", type) + try { + val display = displays.getDisplay(id) + shadowOf(display).setFlags(Display.FLAG_PRESENTATION) + shadowOf(display).setName(name) + check(display) + } finally { ShadowDisplayManager.removeDisplay(id) } + } + @Test fun oneScreenDevicesGetNoSecondScreenInAnyMode() { for (mode in DualScreen.modes) { SessionPrefs.setDualScreenMode(context, mode) diff --git a/app/src/test/java/com/droiddeck/launcher/input/GesturePrefsTest.kt b/app/src/test/java/com/droiddeck/launcher/input/GesturePrefsTest.kt index 9f03811123..a00594f781 100644 --- a/app/src/test/java/com/droiddeck/launcher/input/GesturePrefsTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/input/GesturePrefsTest.kt @@ -17,6 +17,19 @@ class GesturePrefsTest { assertEquals(TouchGestureConfig.RELATIVE_MOUSE_AUTO, TouchGestureConfig.fromJson("{}").relativeMouseMode) } + @Test fun autoNeverGoesRelativeForDirectTouchWithoutAMouse() { + val auto = TouchGestureConfig.RELATIVE_MOUSE_AUTO + assertEquals(TouchGestureConfig.RELATIVE_MOUSE_OFF, TouchGestureConfig.gamescopeRelativeMouse(auto, mouseAttached = false, touchpad = false)) + assertEquals(auto, TouchGestureConfig.gamescopeRelativeMouse(auto, mouseAttached = true, touchpad = false)) + assertEquals(auto, TouchGestureConfig.gamescopeRelativeMouse(auto, mouseAttached = false, touchpad = true)) + } + + @Test fun aChosenRelativeMouseModeIsSentAsIs() { + for (mode in listOf(TouchGestureConfig.RELATIVE_MOUSE_ON, TouchGestureConfig.RELATIVE_MOUSE_OFF)) + for (mouse in listOf(false, true)) for (pad in listOf(false, true)) + assertEquals(mode, TouchGestureConfig.gamescopeRelativeMouse(mode, mouse, pad)) + } + @Test fun removedOptionsAreIgnoredWhenLoadingExistingSettings() { val old = """{"holdMouseButtonWhileTouchingEnabled":true,"holdMouseButtonWhileTouchingAction":"right_click", "showClickHighlight":true,"showCursorInTouchscreenMode":true,"tapAction":"key_E","longPressDelay":650}""" diff --git a/app/src/test/java/com/droiddeck/launcher/input/InputButtonIconsTest.kt b/app/src/test/java/com/droiddeck/launcher/input/InputButtonIconsTest.kt new file mode 100644 index 0000000000..2969933db5 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/input/InputButtonIconsTest.kt @@ -0,0 +1,36 @@ +package com.droiddeck.launcher.input + +import android.graphics.Bitmap +import android.graphics.Canvas +import android.graphics.Color +import android.graphics.Paint +import org.junit.Assert.* +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.annotation.Config +import org.robolectric.annotation.GraphicsMode + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [34]) +@GraphicsMode(GraphicsMode.Mode.NATIVE) +class InputButtonIconsTest { + @Test fun commandSymbolsRenderUsingTheButtonTintAndHaveReadableNames() { + val context = RuntimeEnvironment.getApplication() + for (code in listOf(InputButtonIcons.MENU, InputButtonIcons.KEYBOARD, InputButtonIcons.STEAM, InputButtonIcons.QUICK_ACCESS)) { + val drawable = InputButtonIcons.drawable(context, code) + assertNotNull(drawable) + assertNotEquals(code.toString(), InputButtonIcons.label(context, code)) + val bitmap = Bitmap.createBitmap(64, 64, Bitmap.Config.ARGB_8888) + try { + val paint = Paint(Paint.ANTI_ALIAS_FLAG).apply { color = Color.GREEN } + assertTrue(InputButtonIcons.draw(Canvas(bitmap), code, 32f, 32f, 48f, paint, drawable)) + val pixels = IntArray(64 * 64) + bitmap.getPixels(pixels, 0, 64, 0, 0, 64, 64) + assertTrue(pixels.count { Color.alpha(it) > 0 } > 30) + assertTrue(pixels.filter { Color.alpha(it) > 0 }.all { Color.green(it) > 0 && Color.red(it) == 0 && Color.blue(it) == 0 }) + } finally { bitmap.recycle() } + } + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/input/InputLayoutFileTest.kt b/app/src/test/java/com/droiddeck/launcher/input/InputLayoutFileTest.kt new file mode 100644 index 0000000000..725cea500c --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/input/InputLayoutFileTest.kt @@ -0,0 +1,95 @@ +package com.droiddeck.launcher.input + +import android.content.Context +import org.json.JSONObject +import org.junit.Assert.* +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.annotation.Config + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [34], manifest = Config.NONE) +class InputLayoutFileTest { + private val context: Context get() = RuntimeEnvironment.getApplication() + @Before fun reset() { + for (name in listOf("input-profiles", "input-surfaces", "controller")) context.getSharedPreferences(name, Context.MODE_PRIVATE).edit().clear().commit() + } + + private fun import(raw: String) = InputLayoutFile.import(context, raw, "Fallback", InputLayoutScreen.MAIN, InputSurfaceStyle(), 1000, 500, 2f) + + @Test fun exportThenImportKeepsKeysShapesCentresSizesAndToggles() { + val keys = LegacyScreenKeyMigration.convert(context, + "28:0.25,0.6,50::::0.8:2:0:1;29+33:0.7,0.8,40::Find::0.6:3:1:1;103+0+106+0+108+0+105+0:0.5,0.5,80::::1:6:0:1;17+32+31+30:0.2,0.3,70::::1:5:0:1", + 1000, 500, 2f) + val layout = SavedInputLayout("source", "My layout", keys.map { it.first }) + val bindings = SavedInputBindings("source.bindings", "My layout", false, layout.id, elements = keys.associate { it.first.id to it.second }) + val raw = InputLayoutFile.export(layout, bindings, 1000, 500)!! + assertEquals("My layout", JSONObject(raw).getString("name")) + val (imported, actions) = import(raw)!! + assertEquals("My layout", imported.name) + assertEquals(InputLayoutScreen.MAIN, imported.screen) + assertEquals(layout.elements.map { it.kind }, imported.elements.map { it.kind }) + for ((before, after) in layout.elements.zip(imported.elements)) { + assertEquals(before.x + before.width / 2, after.x + after.width / 2, .002f) + assertEquals(before.y + before.height / 2, after.y + after.height / 2, .002f) + assertEquals(before.height, after.height, .005f) + assertEquals(before.toggle, after.toggle) + assertEquals(before.opacity, after.opacity, .001f) + } + assertEquals(InputButtonShape.CIRCLE, imported.elements[0].buttonShape) + assertEquals(InputButtonShape.RECTANGLE, imported.elements[1].buttonShape) + assertEquals("Find", imported.elements[1].name) + val codes = imported.elements.map { actions.action(it) } + assertEquals(InputAction.Keys(listOf(28)), codes[0]) + assertEquals(InputAction.Keys(listOf(29, 33)), codes[1]) + assertEquals(listOf(103, 106, 108, 105), (codes[2] as InputAction.Directions).let { listOf(it.up, it.right, it.down, it.left) }) + assertEquals(InputAction.Directions(17, 32, 31, 30), codes[3]) + InputLibraries.update(context) { it.withLayout(imported).withPreset(actions).chooseLayout(InputProfile.GAME, InputRole.PRIMARY, imported.id) } + assertEquals(imported.id, InputLibraries.read(context).layout(InputProfile.GAME, InputRole.PRIMARY)!!.id) + } + + @Test fun importsOlderDroidDeckAndWinlatorFiles() { + val raw = """{"id":0,"name":"DroidDeck","cursorSpeed":1.5,"elements":[ + {"type":"BUTTON","shape":"CIRCLE","bindings":["KEY_SPACE"],"scale":1.2,"x":0.8,"y":0.7,"toggleSwitch":false,"text":"Jump","opacity":0.5}, + {"type":"BUTTON","shape":"ROUND_RECT","bindings":["KEY_CTRL_L","KEY_C"],"scale":1.0,"x":0.3,"y":0.2,"toggleSwitch":true,"text":""}, + {"type":"STICK","shape":"ROUND_RECT","bindings":["KEY_W","","KEY_D","","KEY_S","","KEY_A",""],"scale":1.0,"x":0.2,"y":0.7}, + {"type":"STICK","bindings":["KEY_UP","KEY_RIGHT","KEY_DOWN","KEY_LEFT"],"scale":1.0,"x":0.6,"y":0.5}, + {"type":"D_PAD","bindings":["KEY_I","KEY_L","KEY_K","KEY_J"],"scale":1.0,"x":0.4,"y":0.5}, + {"type":"TRACKPAD","bindings":["MOUSE_MOVE_LEFT"]}, + {"type":"BUTTON","bindings":["GAMEPAD_BUTTON_A"],"x":0.1,"y":0.1} + ]}""" + val (layout, actions) = import(raw)!! + assertEquals("DroidDeck", layout.name) + assertEquals(listOf(InputElementKind.BUTTON, InputElementKind.BUTTON, InputElementKind.STICK, InputElementKind.STICK, InputElementKind.DPAD), layout.elements.map { it.kind }) + val jump = layout.elements[0] + assertEquals("Jump", jump.name) + assertEquals(.5f, jump.opacity, .001f) + assertEquals(InputButtonShape.CIRCLE, jump.buttonShape) + assertEquals(.8f, jump.x + jump.width / 2, .001f) + assertEquals(6f * 10f * 1.2f / 500f, jump.height, .002f) + assertTrue(layout.elements[1].toggle) + assertEquals(InputAction.Keys(listOf(29, 46)), actions.action(layout.elements[1])) + assertEquals(InputAction.Directions(17, 32, 31, 30, listOf(0, 0, 0, 0)), actions.action(layout.elements[2])) + assertEquals(InputAction.Directions(103, 106, 108, 105, listOf(0, 0, 0, 0)), actions.action(layout.elements[3])) + assertEquals(InputAction.Directions(23, 38, 37, 36), actions.action(layout.elements[4])) + } + + @Test fun rejectsFilesWithoutUsableControls() { + assertNull(import("not json")) + assertNull(import("""{"name":"x"}""")) + assertNull(import("""{"elements":[{"type":"BUTTON","bindings":["GAMEPAD_BUTTON_A"]}]}""")) + val (layout, _) = import("""{"elements":[{"type":"BUTTON","bindings":["KEY_E"],"x":"bad","scale":-1}]}""")!! + assertEquals("Fallback", layout.name) + assertEquals(.5f, layout.elements.single().x + layout.elements.single().width / 2, .001f) + } + + @Test fun layoutsWithoutKeyBindingsHaveNothingToExport() { + val library = InputLibraries.read(context) + val gamepad = library.layouts.first { it.id == InputLibraries.OVERLAY } + assertNull(InputLayoutFile.export(gamepad, library.bindings.firstOrNull { it.layoutId == gamepad.id }, 1000, 500)) + assertEquals("A_B_.icp", InputLayoutFile.fileName(gamepad.copy(name = "A/B?"))) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/input/InputLayoutToolsTest.kt b/app/src/test/java/com/droiddeck/launcher/input/InputLayoutToolsTest.kt new file mode 100644 index 0000000000..44ad82b904 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/input/InputLayoutToolsTest.kt @@ -0,0 +1,194 @@ +package com.droiddeck.launcher.input + +import android.content.Context +import org.junit.Assert.* +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.annotation.Config + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [34], manifest = Config.NONE) +class InputLayoutToolsTest { + private val context: Context get() = RuntimeEnvironment.getApplication() + @Before fun reset() { + for (name in listOf("input-profiles", "input-surfaces", "controller")) context.getSharedPreferences(name, Context.MODE_PRIVATE).edit().clear().commit() + } + + @Test fun scaleAllResizesEveryElementAroundItsCentreInsideTheScreen() { + val elements = listOf(InputSurfaceElement(InputControl.A, .4f, .4f, .2f, .2f, "a", name = "A"), + InputSurfaceElement(InputControl.A, 0f, .9f, .1f, .1f, "b", name = "B")) + val bigger = elements.scaled(1.25f) + assertEquals(.25f, bigger[0].width, .0001f) + assertEquals(.5f, bigger[0].x + bigger[0].width / 2, .0001f) + assertEquals(.5f, bigger[0].y + bigger[0].height / 2, .0001f) + assertEquals(0f, bigger[1].x, .0001f) + assertEquals(1f, bigger[1].y + bigger[1].height, .0001f) + assertTrue(bigger.all { it == it.bounded() }) + assertEquals(.16f, elements.scaled(.8f)[0].height, .0001f) + assertEquals(elements.map { it.name }, bigger.map { it.name }) + } + + @Test fun resetPositionsOnlyRestoresGeometryOfElementsTheDefaultsKnow() { + val defaults = listOf(InputSurfaceElement(InputControl.A, .1f, .1f, .2f, .2f, "legacy:a")) + val moved = listOf(defaults[0].copy(x = .5f, y = .6f, width = .3f, name = "Jump", opacity = .5f), + InputSurfaceElement(InputControl.A, .3f, .3f, .1f, .1f, "custom", name = "Custom")) + val reset = moved.withPositionsOf(defaults) + assertEquals(defaults[0].copy(name = "Jump", opacity = .5f), reset[0]) + assertEquals(moved[1], reset[1]) + } + + @Test fun copiesRememberTheirOriginSoStockPositionsCanBeRestored() { + val library = InputLibraries.read(context) + val copied = library.copyLayout(InputLibraries.DECK, "Mine", "mine") + val mine = copied.layouts.first { it.id == "mine" } + assertEquals(InputLibraries.DECK, mine.origin) + assertEquals(InputLibraries.DECK, copied.copyLayout("mine", "Again", "again").layouts.first { it.id == "again" }.origin) + val independent = library.useLayout(InputProfile.GAME, InputRole.SECONDARY, InputLibraries.DECK, shared = false) + assertEquals(InputLibraries.DECK, independent.layout(InputProfile.GAME, InputRole.SECONDARY)!!.origin) + val stock = InputStockLayouts.layouts().first { it.id == InputLibraries.DECK }.elements + val dragged = mine.copy(elements = mine.elements.map { it.copy(x = 0f, y = 0f) }) + assertEquals(stock, InputLibraries.defaultPositions(context, copied, dragged)) + assertEquals(mine.elements, dragged.elements.withPositionsOf(stock)) + assertEquals(copied, InputLibraries.decode(InputLibraries.encode(copied))) + } + + @Test fun builtInGamepadDefaultsComeFromTheGamepadGenerator() { + val library = InputLibraries.read(context) + val gamepad = library.layouts.first { it.id == InputLibraries.OVERLAY } + val defaults = InputLibraries.defaultPositions(context, library, gamepad)!! + assertEquals(gamepad.elements.map { it.id }.toSet(), defaults.map { it.id }.toSet()) + assertNull(InputLibraries.defaultPositions(context, library, SavedInputLayout("new", "New"))) + } + + @Test fun quickButtonsHideWithoutRemovingElementsAndPersist() { + val library = InputLibraries.read(context) + val gamepad = library.layouts.first { it.id == InputLibraries.OVERLAY } + assertTrue(InputCommand.KEYBOARD in gamepad.quickButtons) + assertTrue(InputLibraries.setQuickButton(context, gamepad.id, InputCommand.KEYBOARD, false)) + val hidden = InputLibraries.read(context).layouts.first { it.id == gamepad.id } + assertEquals(gamepad.elements, hidden.elements) + assertEquals(setOf(InputCommand.KEYBOARD), hidden.style.hidden) + val shown = hidden.style.shown(hidden.elements) + assertEquals(gamepad.elements.filterNot { it.id == "builtin:keyboard" }, shown) + assertTrue(InputLibraries.setQuickButton(context, gamepad.id, InputCommand.KEYBOARD, true)) + assertEquals(gamepad, InputLibraries.read(context).layouts.first { it.id == gamepad.id }) + } + + @Test fun onlySteamQuickAccessAndKeyboardCanBeHidden() { + val library = InputLibraries.read(context) + val gamepad = library.layouts.first { it.id == InputLibraries.OVERLAY } + assertThrows(IllegalArgumentException::class.java) { + library.withLayout(gamepad.copy(style = gamepad.style.copy(hidden = setOf(InputCommand.MENU)))).validate() + } + val custom = InputSurfaceElement(InputControl.A, .1f, .1f, .1f, .1f, "custom", name = "Steam") + assertFalse(InputSurfaceStyle(hidden = QUICK_BUTTONS.toSet()).hides(custom)) + } + + private fun overlapping(elements: List, width: Int, height: Int): List> { + fun round(e: InputSurfaceElement) = e.buttonShape == InputButtonShape.CIRCLE && e.kind != InputElementKind.STICK + val result = mutableListOf>() + for ((i, a) in elements.withIndex()) for (b in elements.drop(i + 1)) { + val ax = (a.x + a.width / 2) * width; val ay = (a.y + a.height / 2) * height + val bx = (b.x + b.width / 2) * width; val by = (b.y + b.height / 2) * height + val hit = if (round(a) && round(b)) { + val ra = minOf(a.width * width, a.height * height) / 2; val rb = minOf(b.width * width, b.height * height) / 2 + kotlin.math.hypot(ax - bx, ay - by) < ra + rb - .5f + } else kotlin.math.abs(ax - bx) < (a.width + b.width) * width / 2 - .5f && kotlin.math.abs(ay - by) < (a.height + b.height) * height / 2 - .5f + if (hit) result += a.id to b.id + } + return result + } + + @Test fun generatedGamepadSpacingFollowsButtonSize() { + for ((w, h) in listOf(1920 to 1080, 1280 to 960)) { + val normal = LegacyGamepadLayout(context, w, h).elements() + for (scale in listOf(.8f, 1.25f)) { + val elements = LegacyGamepadLayout(context, w, h, scale = scale).elements() + assertEquals("$w x $h at $scale", emptyList>(), overlapping(elements, w, h)) + val face = listOf("legacy:a", "legacy:y").map { id -> elements.first { it.id == id } } + val faceNormal = listOf("legacy:a", "legacy:y").map { id -> normal.first { it.id == id } } + val gap = (face[0].y - face[1].y) / (faceNormal[0].y - faceNormal[1].y) + if (scale > 1f) assertTrue(gap > 1f) else assertTrue(gap < 1f) + } + } + } + + @Test fun scalingACustomLayoutScalesTheGapsBetweenNeighbours() { + val left = listOf(InputSurfaceElement(InputControl.A, .05f, .5f, .08f, .1f, "one", name = "One"), + InputSurfaceElement(InputControl.A, .16f, .5f, .08f, .1f, "two", name = "Two")) + val right = listOf(InputSurfaceElement(InputControl.A, .7f, .2f, .1f, .1f, "three", name = "Three"), + InputSurfaceElement(InputControl.A, .85f, .2f, .1f, .1f, "four", name = "Four")) + for (pair in listOf(left, right)) { + val gap = pair[1].x - (pair[0].x + pair[0].width) + val bigger = pair.scaled(1.25f) + assertEquals(gap * 1.25f, bigger[1].x - (bigger[0].x + bigger[0].width), .0001f) + val smaller = pair.scaled(.8f) + assertEquals(gap * .8f, smaller[1].x - (smaller[0].x + smaller[0].width), .0001f) + assertTrue((bigger + smaller).all { it.x >= 0f && it.x + it.width <= 1.0001f && it.y >= 0f && it.y + it.height <= 1.0001f }) + } + } + + @Test fun aScaledGamepadStaysGeneratedAndRegeneratesOnRotation() { + val (display, metrics) = InputLibraries.primaryDisplay(context) + val long = maxOf(metrics.widthPixels, metrics.heightPixels) + val short = minOf(metrics.widthPixels, metrics.heightPixels) + val gamepad = InputLibraries.read(context).layouts.first { it.id == InputLibraries.OVERLAY } + val scaled = gamepad.copy(scale = 1.25f, elements = LegacyGamepadLayout(display, long, short, scale = 1.25f).regenerate(gamepad.elements)) + assertEquals(long to short, OnScreenLayout.generatedSize(context, scaled, long, short)) + val rotated = OnScreenLayout.elements(context, scaled, short, long, android.graphics.Rect()) + assertEquals(LegacyGamepadLayout(context, short, long, scale = 1.25f).regenerate(scaled.elements), rotated) + assertNull(OnScreenLayout.generatedSize(context, scaled.copy(scale = 1f), long, short)) + InputLibraries.update(context) { it.withLayout(scaled) } + assertEquals(1.25f, InputLibraries.read(context).layouts.first { it.id == gamepad.id }.scale) + } + + private fun assertCross(elements: List, width: Int, height: Int, ids: List) { + val (up, right, down, left) = ids.map { id -> elements.first { it.id == id }.let { (it.x + it.width / 2) * width to (it.y + it.height / 2) * height } } + val cx = (up.first + right.first + down.first + left.first) / 4 + val cy = (up.second + right.second + down.second + left.second) / 4 + assertEquals(up.first, down.first, 1f) + assertEquals(left.second, right.second, 1f) + assertTrue(up.second < down.second && left.first < right.first) + val reach = listOf(up, right, down, left).map { kotlin.math.hypot(it.first - cx, it.second - cy) } + reach.forEach { assertEquals(reach[0], it, 1f) } + } + + private fun assertGamepadShape(elements: List, width: Int, height: Int, label: String) { + assertCross(elements, width, height, listOf("legacy:y", "legacy:b", "legacy:a", "legacy:x")) + assertCross(elements, width, height, listOf("legacy:up", "legacy:right", "legacy:down", "legacy:left")) + assertEquals(label, emptyList>(), overlapping(elements, width, height)) + } + + @Test @Config(qualifiers = "w833dp-h468dp-land-369dpi") + fun buttonSizeKeepsTheGamepadShapeAtHandheldDensity() { + for ((w, h) in listOf(1920 to 1080, 1920 to 1200)) { + val normal = LegacyGamepadLayout(context, w, h).elements() + assertGamepadShape(normal, w, h, "$w x $h") + val right = setOf("legacy:a", "legacy:b", "legacy:x", "legacy:y", "legacy:rs", "legacy:rb", "legacy:rt", "legacy:start") + val left = setOf("legacy:up", "legacy:right", "legacy:down", "legacy:left", "legacy:ls", "legacy:lb", "legacy:lt", "legacy:select") + val moved = normal.map { e -> + when (e.id) { + in right -> e.copy(x = e.x + 21f / w, y = e.y + 18f / h) + in left -> e.copy(x = e.x - 20f / w, y = e.y + 15f / h) + else -> e + } + } + for (scale in listOf(.8f, 1.25f)) { + val generator = LegacyGamepadLayout(context, w, h, scale = scale) + assertGamepadShape(generator.elements(), w, h, "$w x $h generated at $scale") + val rescaled = generator.rescale(moved, LegacyGamepadLayout(context, w, h)) + assertGamepadShape(rescaled, w, h, "$w x $h moved at $scale") + val a = rescaled.first { it.id == "legacy:a" } + val generatedA = generator.elements().first { it.id == "legacy:a" } + assertEquals(generatedA.x + 21f / w, a.x, .001f) + assertEquals(generatedA.y + 18f / h, a.y, .001f) + assertEquals(moved, LegacyGamepadLayout(context, w, h).rescale(rescaled, generator).let { back -> + back.zip(moved).map { (b, m) -> if (kotlin.math.abs(b.x - m.x) < .001f && kotlin.math.abs(b.y - m.y) < .001f && kotlin.math.abs(b.width - m.width) < .001f) m else b } + }) + } + } + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/input/InputLibraryTest.kt b/app/src/test/java/com/droiddeck/launcher/input/InputLibraryTest.kt new file mode 100644 index 0000000000..4bfa52f518 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/input/InputLibraryTest.kt @@ -0,0 +1,595 @@ +package com.droiddeck.launcher.input + +import android.content.Context +import org.json.JSONObject +import org.junit.Assert.* +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.annotation.Config + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [34], manifest = Config.NONE) +class InputLibraryTest { + private val context: Context get() = RuntimeEnvironment.getApplication() + private val prefs get() = context.getSharedPreferences("input-profiles", Context.MODE_PRIVATE) + @Before fun reset() { + for (name in listOf("input-profiles", "input-surfaces", "controller")) context.getSharedPreferences(name, Context.MODE_PRIVATE).edit().clear().commit() + } + + @Test fun freshLibraryHasTheMainGamepadAndTheStockLowerScreenLayouts() { + val library = InputLibraries.read(context) + assertEquals(listOf("game", "desktop"), library.profiles.map { it.id }) + assertEquals(setOf(InputLibraries.OVERLAY, InputLibraries.TRACKPAD) + DeckPanel.entries.map { it.layout }, library.layouts.map { it.id }.toSet()) + assertEquals(10, library.bindings.size) + for (panel in DeckPanel.entries) assertEquals(panel.layout, library.panelLayout(false, panel.bindings)?.id) + assertEquals(InputLibraries.DECK, library.activeLayout(false, InputRole.SECONDARY)?.id) + assertEquals(InputLibraries.TRACKPAD, library.activeLayout(true, InputRole.SECONDARY)?.id) + assertEquals(InputAction.Pointer(), library.compileActive(true).action(InputRole.PHYSICAL, InputControl.LS)) + assertEquals(InputAction.Native(), library.compileActive(false).action(InputRole.PHYSICAL, InputControl.LS)) + + val desktop = library.activeLayout(true, InputRole.SECONDARY)!! + val pad = desktop.elements.single { it.kind == InputElementKind.TRACKPAD } + assertTrue(pad.mouseGestures) + assertEquals(1, desktop.elements.size) + assertEquals(InputAction.Pointer(), library.compileActive(true).elementAction(InputRole.SECONDARY, pad.id, pad.control)) + assertEquals(InputAction.MouseButton(272), library.compileActive(true).elementClickAction(InputRole.SECONDARY, pad)) + val game = library.activeLayout(false, InputRole.SECONDARY)!! + assertEquals(6, game.elements.size) + assertEquals(setOf(InputControl.LEFT_PAD, InputControl.RIGHT_PAD), game.elements.filter { it.kind == InputElementKind.TRACKPAD }.map { it.control }.toSet()) + for (control in listOf(InputControl.L4, InputControl.L5, InputControl.R4, InputControl.R5)) { + val element = game.elements.single { it.control == control } + assertEquals(InputAction.Native(), library.compileActive(false).elementAction(InputRole.SECONDARY, element.id, element.control)) + } + for (layout in InputStockLayouts.layouts()) assertTrue(layout.elements.none { it.id.startsWith("command:") }) + assertTrue(game.elements.filter { it.kind == InputElementKind.TRACKPAD }.all { it.y == .02f }) + assertEquals(library, InputLibraries.decode(InputLibraries.encode(library))) + } + + @Test fun legacyLowerScreenBindingsKeepTheirOriginalControlsWithoutSavedGeometry() { + prefs.edit().putBoolean("migrated", true).putString("profile.game", + """{"version":1,"bindings":[{"role":"SECONDARY","control":"a","action":{"type":"keys","codes":[57]}}]}""").commit() + val library = InputLibraries.read(context) + assertEquals(InputSurfaceLayout.defaults(), library.layout("game", InputRole.SECONDARY)?.elements) + assertEquals(InputAction.Keys(listOf(57)), library.compile("game").elementAction(InputRole.SECONDARY, "legacy:a", InputControl.A)) + } + + @Test fun existingLibrariesKeepTheirSavedLayoutsBindingsAndSelections() { + var original = InputLibraries.read(context).duplicateProfile("desktop", "Browser", "browser") + val layout = SavedInputLayout("builtin.left-pad", "My left pad", InputSurfaceLayout.defaults(), screen = InputLayoutScreen.SECONDARY) + original = original.withLayout(layout).withPreset(SavedInputBindings("preset.left-pad", "My left pad", false, layout.id, + controls = mapOf(InputControl.LEFT_PAD to InputAction.Pointer(700f)))) + .selectProfile(true, "browser").selectPart(true, InputRole.SECONDARY, "preset.left-pad").validate() + val raw = org.json.JSONObject(InputLibraries.encode(original)).put("version", 6).toString() + prefs.edit().putString("library", raw).commit() + assertEquals(original, InputLibraries.read(context)) + assertEquals(original, InputLibraries.read(context)) + } + + @Test fun resettingDefaultActionsKeepsTheStockPanelActions() { + val library = InputLibraries.read(context) + for (id in listOf("game", "desktop")) { + val reset = library.resetProfile(id).validate() + for (element in library.layout(id, InputRole.SECONDARY)!!.elements) { + assertEquals(library.compile(id).elementAction(InputRole.SECONDARY, element.id, element.control), + reset.compile(id).elementAction(InputRole.SECONDARY, element.id, element.control)) + } + } + } + + @Test fun migrationPreservesExistingBindingsGeometryAndActiveSelection() { + prefs.edit().putString("selected", "desktop").putBoolean("migrated", true) + .putString("profile.desktop", """{"version":1,"bindings":[{"role":"PHYSICAL","control":"a","action":{"type":"keys","codes":[29,33]}}]}""").commit() + InputSurfaceLayout.saveLegacy(context, "desktop", listOf(InputSurfaceElement(InputControl.B, .3f, .2f, .4f, .4f))) + val legacy = InputProfiles.readLegacy(context, "desktop") + assertEquals(InputAction.Keys(listOf(29, 33)), legacy.action(InputRole.PHYSICAL, InputControl.A)) + val library = InputLibraries.read(context) + assertEquals(legacy.bindings, library.compile("desktop").bindings) + assertEquals(legacy.keys, library.compile("desktop").keys) + assertEquals("desktop", library.selected) + assertEquals(.3f, library.layout("desktop", InputRole.SECONDARY)!!.elements.single().x) + assertEquals(library, InputLibraries.decode(InputLibraries.encode(library))) + } + + @Test fun profilesSharePhysicalBindingsWhileUsingDifferentTouchActions() { + val original = InputLibraries.read(context) + val shared = original.duplicateProfile("desktop", "Editor", "editor", sharePhysical = true) + val p = shared.preset(shared.profile("desktop").binding(InputRole.PHYSICAL)) + val updated = shared.withPreset(p.copy(controls = p.controls + (InputControl.R3 to InputAction.Keys(listOf(29, 33))))) + .copyBindings(shared.profile("editor").binding(InputRole.SECONDARY), "Editor touch", "editor", InputRole.SECONDARY, "editor.touch") + val touch = updated.preset("editor.touch") + val final = updated.withPreset(touch.copy(controls = touch.controls + (InputControl.A to InputAction.Keys(listOf(28))))).validate() + assertEquals(InputAction.Keys(listOf(29, 33)), final.compile("editor").action(InputRole.PHYSICAL, InputControl.R3)) + assertEquals(InputAction.Keys(listOf(29, 33)), final.compile("desktop").action(InputRole.PHYSICAL, InputControl.R3)) + assertEquals(InputAction.MouseButton(272), final.compile("desktop").action(InputRole.SECONDARY, InputControl.A)) + assertEquals(InputAction.Keys(listOf(28)), final.compile("editor").action(InputRole.SECONDARY, InputControl.A)) + assertEquals(final.layout("desktop", InputRole.SECONDARY)!!.elements, final.layout("editor", InputRole.SECONDARY)!!.elements) + assertNotEquals(final.layout("desktop", InputRole.SECONDARY)!!.id, final.layout("editor", InputRole.SECONDARY)!!.id) + } + + @Test fun copyingPhysicalBindingsOnlyRelinksTheSelectedProfile() { + val library = InputLibraries.read(context).duplicateProfile("desktop", "Editor", "editor") + val id = library.profile("desktop").binding(InputRole.PHYSICAL) + val copied = library.copyBindings(id, "Editor controller", "editor", InputRole.PHYSICAL, "private") + assertEquals(id, copied.profile("desktop").binding(InputRole.PHYSICAL)) + assertEquals("private", copied.profile("editor").binding(InputRole.PHYSICAL)) + assertEquals(library.preset(id).controls, copied.preset("private").controls) + assertEquals(library.profile("editor").parts, library.copyBindings(id, "Library copy").profile("editor").parts) + } + + @Test fun copyingProfileIsIndependentAndPreservesElementActions() { + val original = InputLibraries.read(context) + val library = original.duplicateProfile("desktop", "Editor", "editor").validate() + for (role in InputRole.entries) { + assertNotEquals(library.profile("desktop").binding(role), library.profile("editor").binding(role)) + assertEquals(library.compile("desktop").bindings[role], library.compile("editor").bindings[role]) + if (library.layout("desktop", role)?.editable == true) { + assertNotEquals(library.layout("desktop", role)!!.id, library.layout("editor", role)!!.id) + assertEquals(library.layout("desktop", role)!!.elements, library.layout("editor", role)!!.elements) + } + } + assertEquals(original.profiles, library.profiles.filterNot { it.id == "editor" }) + assertEquals(original.selected, library.selected) + } + + @Test fun sharingGeometryKeepsActionsIndependentAndDetachingCopiesBoth() { + var library = InputLibraries.read(context).duplicateProfile("desktop", "Editor", "editor") + val source = library.layout("desktop", InputRole.SECONDARY)!! + library = library.useLayout("editor", InputRole.SECONDARY, source.id, shared = true).validate() + assertEquals(source.id, library.layout("editor", InputRole.SECONDARY)!!.id) + assertNotEquals(library.profile("desktop").binding(InputRole.SECONDARY), library.profile("editor").binding(InputRole.SECONDARY)) + library = library.usePart("editor", InputRole.SECONDARY, "desktop", shared = true).validate() + assertEquals(library.profile("desktop").parts[InputRole.SECONDARY], library.profile("editor").parts[InputRole.SECONDARY]) + val detached = library.usePart("editor", InputRole.SECONDARY, "editor", shared = false).validate() + assertNotEquals(source.id, detached.layout("editor", InputRole.SECONDARY)!!.id) + assertNotEquals(detached.profile("desktop").binding(InputRole.SECONDARY), detached.profile("editor").binding(InputRole.SECONDARY)) + assertEquals(library.compile("editor"), detached.compile("editor")) + } + + @Test fun elementIdsSurviveGeometryEditsAndDeletionPrunesEveryLinkedPreset() { + val library = InputLibraries.read(context) + val layout = library.layout("game", InputRole.SECONDARY)!! + val e = InputSurfaceElement(InputControl.A, .1f, .1f, .2f, .2f, "button-1") + val b = library.preset(library.profile("game").binding(InputRole.SECONDARY)) + val saved = library.withLayout(layout.copy(elements = listOf(e))) + .withPreset(b.copy(elements = mapOf(e.id to InputAction.Keys(listOf(57))))) + .copyBindings(b.id, "Alternative", newId = "alternative").validate() + val moved = saved.withLayout(layout.copy(elements = listOf(e.copy(x = .6f)))).validate() + assertEquals(InputAction.Keys(listOf(57)), moved.preset(b.id).action(e)) + val deleted = moved.withLayout(layout.copy(elements = emptyList())).validate() + assertTrue(deleted.preset(b.id).elements.isEmpty()) + assertTrue(deleted.preset("alternative").elements.isEmpty()) + } + + @Test fun missingAndIncompatibleReferencesAreRejected() { + val library = InputLibraries.read(context) + assertThrows(IllegalArgumentException::class.java) { library.removeBindings(library.profile("game").binding(InputRole.PHYSICAL)) } + assertThrows(IllegalArgumentException::class.java) { library.removeLayout(library.layout("game", InputRole.SECONDARY)!!.id) } + assertThrows(IllegalArgumentException::class.java) { library.chooseBindings("game", InputRole.PHYSICAL, library.profile("game").binding(InputRole.SECONDARY)) } + assertThrows(IllegalArgumentException::class.java) { library.chooseLayout("game", InputRole.PHYSICAL, InputLibraries.DECK) } + } + + @Test fun deletingActiveProfileFallsBackWithoutDeletingSharedParts() { + val library = InputLibraries.read(context).duplicateProfile("desktop", "Editor", "editor") + val removed = library.copy(selected = "editor", desktop = "editor").removeProfile("editor").validate() + assertEquals(InputProfiles.AUTO, removed.selected) + assertEquals("desktop", removed.desktop) + assertEquals(library.layouts, removed.layouts) + assertEquals(library.bindings, removed.bindings) + } + + @Test fun futureLibrariesAreNotOverwritten() { + val future = org.json.JSONObject(InputLibraries.encode(InputLibraries.read(context))).put("version", 999).toString() + prefs.edit().putString("library", future).commit() + InputLibraries.read(context) + assertThrows(IllegalArgumentException::class.java) { InputLibraries.update(context) { it.copy(selected = "desktop") } } + assertEquals(future, prefs.getString("library", null)) + } + + @Test fun unreadableLibrariesKeepStableEditorReferencesUntilAnEditPreservesRecoveryData() { + val valid = InputLibraries.encode(InputLibraries.read(context)) + val invalidValues = listOf("{broken", org.json.JSONObject(valid).put("selected", "missing").toString()) + for (raw in invalidValues) { + prefs.edit().putString("library", raw).commit() + val first = InputLibraries.read(context) + val second = InputLibraries.read(context) + assertEquals(first, second) + val id = first.profile("game").binding(InputRole.PRIMARY) + assertEquals(first.preset(id), second.preset(id)) + assertEquals(raw, prefs.getString("library", null)) + val updated = InputLibraries.update(context) { it.copy(selected = "desktop") } + assertEquals("desktop", InputLibraries.read(context).selected) + assertEquals(updated, InputLibraries.read(context)) + assertTrue(prefs.all.filterKeys { it.startsWith("library-recovery.") }.containsValue(raw)) + } + assertTrue(InputLibraries.hasLibraryRecovery(context)) + assertEquals(invalidValues.toSet(), prefs.all.filterKeys { it.startsWith("library-recovery.") }.values.toSet()) + } + + @Test fun resettingAProfileDoesNotResetSharedBindingsOrAccumulatePrivateCopies() { + var library = InputLibraries.read(context).duplicateProfile("desktop", "Editor", "editor", sharePhysical = true) + val common = library.preset(library.profile("desktop").binding(InputRole.PHYSICAL)) + library = library.withPreset(common.copy(controls = common.controls + (InputControl.A to InputAction.Keys(listOf(57))))) + .resetProfile("editor").validate() + val count = library.bindings.size + repeat(100) { library = library.resetProfile("editor").validate() } + assertEquals(count, library.bindings.size) + assertEquals(InputAction.Keys(listOf(57)), library.compile("desktop").action(InputRole.PHYSICAL, InputControl.A)) + assertEquals(InputAction.MouseButton(272), library.compile("editor").action(InputRole.PHYSICAL, InputControl.A)) + } + + @Test fun clickActionsRoundTripAndArePrunedWithTheirElement() { + val library = InputLibraries.read(context) + val layout = library.layout("desktop", InputRole.SECONDARY)!! + val pad = layout.elements.first { it.hasClick } + val preset = library.preset(library.profile("desktop").binding(InputRole.SECONDARY)) + val updated = library.withPreset(preset.copy(clicks = mapOf(pad.id to InputAction.Keys(listOf(29, 33))))).validate() + val roundTrip = InputLibraries.decode(InputLibraries.encode(updated))!! + assertEquals(InputAction.Keys(listOf(29, 33)), roundTrip.compile("desktop").elementClickAction(InputRole.SECONDARY, pad)) + assertEquals(preset.action(pad), roundTrip.preset(preset.id).action(pad)) + assertTrue(roundTrip.withLayout(layout.copy(elements = emptyList())).preset(preset.id).clicks.isEmpty()) + } + @Test fun versionTwoFixedGamepadMigratesOnceWithoutChangingOtherProfiles() { + val original = InputLibraries.read(context) + val fixed = original.copy(layouts = original.layouts.map { if (it.id == InputLibraries.OVERLAY) it.copy(elements = emptyList(), template = InputLayoutTemplate.OVERLAY) else it }, + bindings = original.bindings.map { if (it.layoutId == InputLibraries.OVERLAY) it.copy(layoutId = null, elements = emptyMap(), clicks = emptyMap()) else it }) + val raw = org.json.JSONObject(InputLibraries.encode(fixed)).put("version", 2).toString() + prefs.edit().putString("library", raw).commit() + ControllerPrefs.setStickClick(context, false) + ControllerPrefs.setKeyboardButton(context, true) + val size = context.resources.displayMetrics + ControllerPrefs.setLayout(context, size.widthPixels, size.heightPixels, mapOf("a" to (.5f to .5f), "ls" to (.3f to .4f))) + ControllerPrefs.setLayout(context, 1600, 900, mapOf("ls" to (.3f to .4f))) + val migrated = InputLibraries.read(context) + val saved = prefs.getString("library", null) + assertEquals(migrated, InputLibraries.read(context)) + assertEquals(saved, prefs.getString("library", null)) + assertFalse(migrated.layouts.any { it.template == InputLayoutTemplate.OVERLAY }) + assertTrue(migrated.layouts.any { it.id == "migrated.layout.1600x900" }) + assertEquals(original.layout("game", InputRole.SECONDARY), migrated.layout("game", InputRole.SECONDARY)) + assertEquals(original.preset(original.profile("game").binding(InputRole.PHYSICAL)), migrated.preset(migrated.profile("game").binding(InputRole.PHYSICAL))) + val layout = migrated.layout("game", InputRole.PRIMARY)!! + val preset = migrated.preset(migrated.profile("game").binding(InputRole.PRIMARY)) + assertTrue(layout.editable) + assertEquals(InputAction.Command(InputCommand.KEYBOARD), preset.elements["builtin:keyboard"]) + assertEquals(InputAction.Disabled, preset.clickAction(layout.elements.first { it.control == InputControl.LS })) + } + + @Test fun oldGamepadPlacementAndVisibilityBecomeEditableElements() { + ControllerPrefs.setLayout(context, 1200, 800, mapOf("ls" to (.4f to .5f))) + ControllerPrefs.setSteamButton(context, false) + ControllerPrefs.setTarget(context, "b", ControllerPrefs.OFF) + val elements = LegacyGamepadLayout(context, 1200, 800).elements() + val stick = elements.first { it.control == InputControl.LS } + assertEquals(.4f, stick.x + stick.width - stick.height * 800f / 1200f / 2f, .0001f) + assertEquals(.5f, stick.y + stick.height / 2f, .0001f) + assertFalse(elements.any { it.control == InputControl.GUIDE || it.control == InputControl.B }) + assertTrue(elements.all { it == it.bounded() }) + } + + @Test fun gamepadControlsAreRoundWithShoulderPillsAndACompactKeyboard() { + val elements = LegacyGamepadLayout(context, 1200, 800).elements() + val density = context.resources.displayMetrics.density + for (element in elements.filter { it.id.startsWith("legacy:") }) { + val shoulder = element.control in setOf(InputControl.LB, InputControl.RB, InputControl.LT, InputControl.RT) + assertEquals(element.control.id, if (shoulder) InputButtonShape.PILL else InputButtonShape.CIRCLE, element.buttonShape) + } + val keyboard = elements.single { it.id == "builtin:keyboard" } + assertEquals(InputButtonIcons.KEYBOARD, keyboard.icon) + assertEquals(InputButtonShape.CIRCLE, keyboard.buttonShape) + assertEquals(36 * density / 1200f, keyboard.width, .0001f) + assertEquals(90 * density / 1200f, keyboard.x + keyboard.width / 2f, .0001f) + assertEquals(1f - 34 * density / 800f, keyboard.y + keyboard.height / 2f, .0001f) + } + + @Test fun dpadButtonsMatchTheFaceButtonsInSizeAndSpacing() { + val elements = LegacyGamepadLayout(context, 1200, 800).elements() + fun centre(c: InputControl) = elements.single { it.id == "legacy:${c.id}" }.let { (it.x + it.width / 2f) * 1200 to (it.y + it.height / 2f) * 800 } + fun size(c: InputControl) = elements.single { it.id == "legacy:${c.id}" }.width * 1200 + assertEquals(size(InputControl.A), size(InputControl.UP), .01f) + assertEquals(centre(InputControl.A).second - centre(InputControl.Y).second, centre(InputControl.DOWN).second - centre(InputControl.UP).second, .01f) + assertEquals(centre(InputControl.B).first - centre(InputControl.X).first, centre(InputControl.RIGHT).first - centre(InputControl.LEFT).first, .01f) + } + + @Test fun anUntouchedSavedDpadIsResizedAroundItsCentreAndAMovedOneIsKept() { + val fresh = LegacyGamepadLayout(context, 1200, 800) + val target = fresh.elements().filter { it.control in setOf(InputControl.UP, InputControl.RIGHT, InputControl.DOWN, InputControl.LEFT) } + val cx = target.map { it.x + it.width / 2f }.average().toFloat() + val cy = target.map { it.y + it.height / 2f }.average().toFloat() + val scale = 26f / 30f + val old = target.map { e -> + val ex = cx + (e.x + e.width / 2f - cx) * 52f / 50f + val ey = cy + (e.y + e.height / 2f - cy) * 52f / 50f + e.copy(x = ex - e.width * scale / 2f, y = ey - e.height * scale / 2f, width = e.width * scale, height = e.height * scale, buttonShape = InputButtonShape.ROUNDED) + } + val restyled = fresh.restyle(old) + for ((want, got) in target.zip(restyled)) { + assertEquals(want.x, got.x, .0005f); assertEquals(want.y, got.y, .0005f) + assertEquals(want.width, got.width, .0005f); assertEquals(InputButtonShape.CIRCLE, got.buttonShape) + } + val moved = old.mapIndexed { i, e -> if (i == 0) e.copy(buttonShape = InputButtonShape.RECTANGLE) else e } + assertEquals(moved.drop(1).map { it.width }, fresh.restyle(moved).drop(1).map { it.width }) + val shifted = old.mapIndexed { i, e -> if (i == 0) e.copy(x = e.x + .03f) else e } + assertEquals(shifted.map { listOf(it.x, it.y, it.width, it.height) }, fresh.restyle(shifted).map { listOf(it.x, it.y, it.width, it.height) }) + } + + @Test fun stickTouchAreasReachFurtherOutwardButKeepTheirInnerEdge() { + val elements = LegacyGamepadLayout(context, 1200, 800).elements() + for (control in listOf(InputControl.LS, InputControl.RS)) { + val stick = elements.single { it.control == control } + assertEquals(control.id, stick.height * 800f * 1.25f, stick.width * 1200f, 1f) + } + val left = elements.single { it.control == InputControl.LS } + val right = elements.single { it.control == InputControl.RS } + ControllerPrefs.setLayout(context, 1200, 800, mapOf("ls" to (.3f to .5f), "rs" to (.7f to .5f))) + val placed = LegacyGamepadLayout(context, 1200, 800).elements() + val ls = placed.single { it.control == InputControl.LS } + val rs = placed.single { it.control == InputControl.RS } + assertEquals(.3f, ls.x + ls.width - ls.height * 800f / 1200f / 2f, .0001f) + assertEquals(.7f, rs.x + rs.height * 800f / 1200f / 2f, .0001f) + assertTrue(left.width > left.height * 800f / 1200f && right.width > right.height * 800f / 1200f) + } + + @Test fun adaptiveSticksToggleChangesOnlyTheSticksOfThatLayout() { + val library = InputLibraries.read(context) + val overlay = library.layouts.single { it.id == InputLibraries.OVERLAY } + assertEquals(true, overlay.adaptiveSticks) + InputLibraries.setAdaptiveSticks(context, overlay.id, false) + val off = InputLibraries.read(context) + val changed = off.layouts.single { it.id == overlay.id } + assertEquals(false, changed.adaptiveSticks) + assertFalse(ControllerPrefs.read(context).adaptiveSticks) + assertEquals(overlay.elements.filter { it.kind != InputElementKind.STICK }, changed.elements.filter { it.kind != InputElementKind.STICK }) + assertEquals(library.layouts.filter { it.id != overlay.id }, off.layouts.filter { it.id != overlay.id }) + assertNull(off.layouts.single { it.id == InputLibraries.DECK }.adaptiveSticks) + assertTrue(InputLibraries.setAdaptiveSticks(context, overlay.id, true)) + assertEquals(true, InputLibraries.read(context).layouts.single { it.id == overlay.id }.adaptiveSticks) + prefs.edit().putString("library", JSONObject().put("version", 99).toString()).commit() + assertFalse(InputLibraries.setAdaptiveSticks(context, overlay.id, false)) + assertTrue(ControllerPrefs.read(context).adaptiveSticks) + } + + @Test fun savedGamepadsFromTheFirstUnifiedBuildGetTheRoundLookOnce() { + val fresh = InputLibraries.read(context) + val overlay = fresh.layouts.single { it.id == InputLibraries.OVERLAY } + val old = overlay.copy(elements = overlay.elements.map { e -> + if (e.id == "builtin:keyboard") InputSurfaceElement(InputControl.A, .02f, .9f, .12f, .08f, "builtin:keyboard", name = "Keyboard") + else if (e.control == InputControl.A) e.copy(buttonShape = InputButtonShape.RECTANGLE, x = .5f) + else e.copy(buttonShape = InputButtonShape.ROUNDED) + }) + val custom = SavedInputLayout("mine", "Mine", listOf(InputSurfaceElement(InputControl.B, .1f, .1f, .1f, .1f))) + val stored = fresh.withLayout(old).withLayout(custom) + val renamed = InputSurfaceElement(InputControl.A, .02f, .9f, .12f, .08f, "builtin:keyboard", name = "Keys") + assertEquals(renamed, LegacyGamepadLayout(context, 1200, 800).restyle(listOf(renamed)).single()) + prefs.edit().putString("library", JSONObject(InputLibraries.encode(stored)).put("version", 8).toString()).commit() + val upgraded = InputLibraries.read(context) + val restyled = upgraded.layouts.single { it.id == InputLibraries.OVERLAY }.elements + assertEquals(overlay.elements.single { it.id == "builtin:keyboard" }, restyled.single { it.id == "builtin:keyboard" }) + assertEquals(InputButtonShape.RECTANGLE, restyled.single { it.control == InputControl.A && it.id.startsWith("legacy:") }.buttonShape) + assertEquals(.5f, restyled.single { it.control == InputControl.A && it.id.startsWith("legacy:") }.x, .0001f) + assertEquals(InputButtonShape.PILL, restyled.single { it.control == InputControl.LB }.buttonShape) + assertEquals(InputButtonShape.CIRCLE, restyled.single { it.control == InputControl.UP }.buttonShape) + assertEquals(InputButtonShape.ROUNDED, upgraded.layouts.single { it.id == "mine" }.elements.single().buttonShape) + assertEquals(upgraded, InputLibraries.read(context)) + for (desktop in listOf(false, true)) + assertEquals(InputAction.Command(InputCommand.KEYBOARD), upgraded.compileActive(desktop).elementAction(InputRole.PRIMARY, "builtin:keyboard", InputControl.A)) + } + + @Test fun quickPartsAreModeSpecificAndDoNotEditSavedProfiles() { + val original = InputLibraries.read(context) + val mixed = original.selectPart(true, InputRole.SECONDARY, InputStockLayouts.TRACKPAD).validate() + assertEquals(original.profiles, mixed.profiles) + assertEquals(original.compileActive(false), mixed.compileActive(false)) + assertEquals(InputLibraries.TRACKPAD, mixed.activeLayout(true, InputRole.SECONDARY)?.id) + assertEquals(InputAction.Pointer(), mixed.compileActive(true).elementAction(InputRole.SECONDARY, "legacy:right-pad", InputControl.RIGHT_PAD)) + assertEquals(mixed, InputLibraries.decode(InputLibraries.encode(mixed))) + val restored = mixed.selectProfile(true, InputProfiles.AUTO) + assertTrue(restored.selection(true).parts.isEmpty()) + assertEquals(original.compileActive(true), restored.compileActive(true)) + } + + @Test fun explicitAutoSelectionStaysDistinctFromAnInheritedProfileAfterSaving() { + val original = InputLibraries.read(context) + val binding = original.profile("game").binding(InputRole.PHYSICAL) + val inherited = original.selectPart(false, InputRole.PHYSICAL, binding) + val explicit = original.selectProfile(false, InputProfiles.AUTO).selectPart(false, InputRole.PHYSICAL, binding) + assertFalse(InputLibraries.decode(InputLibraries.encode(inherited))!!.selection(false).profileSelected) + assertTrue(InputLibraries.decode(InputLibraries.encode(explicit))!!.selection(false).profileSelected) + assertEquals(inherited.compileActive(false), explicit.compileActive(false)) + } + + @Test fun versionSevenSelectionsRetainKnownProfileAndPartChoices() { + val original = InputLibraries.read(context) + val binding = original.profile("game").binding(InputRole.PHYSICAL) + val choices = listOf( + original.selectPart(false, InputRole.PHYSICAL, binding) to false, + original.selectProfile(false, InputProfiles.AUTO) to true, + original.selectProfile(false, "game").selectPart(false, InputRole.PHYSICAL, binding) to true, + original.selectPart(false, InputRole.SECONDARY, InputStockLayouts.TRACKPAD) to false, + ) + for ((library, selected) in choices) { + val old = org.json.JSONObject(InputLibraries.encode(library)).put("version", 7) + old.getJSONObject("selections").getJSONObject("game").remove("profileSelected") + prefs.edit().putString("library", old.toString()).commit() + val restored = InputLibraries.read(context) + assertEquals(selected, restored.selection(false).profileSelected) + assertEquals(library, restored) + assertEquals(restored, InputLibraries.read(context)) + } + } + + @Test fun savedQuickCombinationOwnsIndependentCopiesOfAllThreeParts() { + val original = InputLibraries.read(context) + val mixed = original.selectPart(true, InputRole.SECONDARY, InputStockLayouts.TRACKPAD) + val saved = mixed.saveSelection(true, "Writing", "writing").validate() + assertEquals("writing", saved.activeId(true)) + assertTrue(saved.selection(true).parts.isEmpty()) + for (role in InputRole.entries) { + val source = mixed.activePart(true, role) + val copy = saved.activePart(true, role) + assertNotEquals(source.bindings, copy.bindings) + if (role != InputRole.PHYSICAL) assertNotEquals(source.layout, copy.layout) + assertEquals(mixed.preset(source.bindings).elements, saved.preset(copy.bindings).elements) + } + assertEquals(original.profile("desktop"), saved.profile("desktop")) + } + + @Test fun activeQuickPartsCannotBeDeletedAndInvalidCombinationsAreRejected() { + val library = InputLibraries.read(context).selectPart(false, InputRole.SECONDARY, InputStockLayouts.TRACKPAD) + assertThrows(IllegalArgumentException::class.java) { library.removeBindings(InputStockLayouts.TRACKPAD) } + assertThrows(IllegalArgumentException::class.java) { library.removeLayout(InputLibraries.TRACKPAD) } + assertThrows(IllegalArgumentException::class.java) { library.selectPart(false, InputRole.PHYSICAL, InputStockLayouts.TRACKPAD).validate() } + assertThrows(IllegalArgumentException::class.java) { library.selectProfile(false, "missing").validate() } + } + + @Test fun oldFixedLowerPanelsMigrateIntoEditableLayoutsWithWorkingCommands() { + val current = InputLibraries.read(context) + val profile = current.profile("game") + val old = current.copy(layouts = current.layouts.map { if (it.id == InputLibraries.DECK) it.copy(elements = emptyList(), template = InputLayoutTemplate.DECK) else it }, + bindings = current.bindings.filterNot { it.layoutId == InputLibraries.DECK }) + .withPreset(SavedInputBindings("old-deck", "Old Deck", false, controls = mapOf(InputControl.L4 to InputAction.Keys(listOf(57))))) + .withProfile(profile.copy(parts = profile.parts + (InputRole.SECONDARY to InputPart("old-deck", InputLibraries.DECK)))).validate() + val raw = org.json.JSONObject(InputLibraries.encode(old)).put("version", 3).toString() + context.getSharedPreferences("input-profiles", Context.MODE_PRIVATE).edit().putString("library", raw).commit() + val upgraded = InputLibraries.read(context) + assertTrue(upgraded.layout("game", InputRole.SECONDARY)!!.editable) + val output = upgraded.compile("game") + assertEquals(InputAction.Keys(listOf(57)), output.elementAction(InputRole.SECONDARY, "legacy:l4", InputControl.L4)) + assertEquals(InputStockLayouts.layouts().single { it.id == InputLibraries.DECK }.elements, upgraded.layout("game", InputRole.SECONDARY)!!.elements) + assertEquals(upgraded, InputLibraries.read(context)) + } + + @Test fun stockPresetsAreScopedToTheSecondScreenAndCopiedWithTheirActions() { + val library = InputLibraries.read(context) + for (layout in InputStockLayouts.layouts()) { + val role = InputRole.SECONDARY + assertFalse(library.layouts.first { it.id == layout.id }.supports(InputRole.PRIMARY)) + val changed = library.useLayout("game", role, layout.id, shared = false).validate() + val copy = changed.layout("game", role)!! + assertNotEquals(layout.id, copy.id) + assertEquals(layout.elements, copy.elements) + val stock = InputStockLayouts.presets(listOf(layout)).single() + for (element in layout.elements) assertEquals(stock.elements[element.id] ?: stock.controls[element.control] ?: InputAction.Native(), + changed.compile("game").elementAction(role, element.id, element.control)) + } + } + + @Test fun versionFourScopingPreservesLayoutsAlreadySharedAcrossScreens() { + val original = InputLibraries.read(context) + val deck = original.layouts.first { it.id == InputLibraries.DECK }.copy(screen = InputLayoutScreen.BOTH) + val shared = original.withLayout(deck).useLayout("game", InputRole.PRIMARY, deck.id, shared = true) + .useLayout("game", InputRole.SECONDARY, deck.id, shared = true) + val json = org.json.JSONObject(InputLibraries.encode(shared)).put("version", 4) + val layouts = json.getJSONArray("layouts") + for (i in 0 until layouts.length()) layouts.getJSONObject(i).remove("screen") + context.getSharedPreferences("input-profiles", Context.MODE_PRIVATE).edit().putString("library", json.toString()).commit() + val migrated = InputLibraries.read(context) + assertEquals(InputLayoutScreen.BOTH, migrated.layouts.first { it.id == deck.id }.screen) + assertEquals(shared.profiles, migrated.profiles) + assertEquals(shared.bindings, migrated.bindings) + assertEquals(migrated, InputLibraries.read(context)) + } + + @Test fun horizontalScrollAndLayoutTargetSurviveSerialization() { + val library = InputLibraries.read(context) + val preset = library.preset(library.profile("desktop").binding(InputRole.PHYSICAL)) + val changed = library.withPreset(preset.copy(controls = preset.controls + (InputControl.RS to InputAction.Scroll(17f, true)))) + assertEquals(changed, InputLibraries.decode(InputLibraries.encode(changed))) + } + + + @Test fun bottomScreenLayoutsKeepTheNeutralLookAndPickedColorsSurvive() { + val fresh = InputLibraries.read(context) + assertNull(fresh.layouts.single { it.id == InputLibraries.DECK }.style.tint) + assertNull(fresh.layouts.single { it.id == InputLibraries.TRACKPAD }.style.tint) + assertEquals(ControllerPrefs.STEAM_BLUE, fresh.layouts.single { it.id == InputLibraries.OVERLAY }.style.tint) + val red = ControllerPrefs.tints.last().first + val blue = fresh.layouts.map { it.copy(style = it.style.copy(tint = if (it.id == InputLibraries.TRACKPAD) red else ControllerPrefs.STEAM_BLUE)) } + val mine = SavedInputLayout("mine", "Mine", listOf(InputSurfaceElement(InputControl.B, .1f, .1f, .1f, .1f))) + val old = JSONObject(InputLibraries.encode(blue.fold(fresh) { library, layout -> library.withLayout(layout) }.withLayout(mine))).put("version", 8) + prefs.edit().putString("library", old.toString()).commit() + val upgraded = InputLibraries.read(context) + assertNull(upgraded.layouts.single { it.id == InputLibraries.DECK }.style.tint) + assertEquals(red, upgraded.layouts.single { it.id == InputLibraries.TRACKPAD }.style.tint) + assertEquals(ControllerPrefs.STEAM_BLUE, upgraded.layouts.single { it.id == InputLibraries.OVERLAY }.style.tint) + assertEquals(ControllerPrefs.STEAM_BLUE, upgraded.layouts.single { it.id == "mine" }.style.tint) + InputLibraries.update(context) { it.withLayout(it.layouts.single { l -> l.id == "mine" }.let { l -> l.copy(style = l.style.copy(tint = null)) }) } + assertNull(InputLibraries.read(context).layouts.single { it.id == "mine" }.style.tint) + assertEquals(InputLibraries.read(context), InputLibraries.read(context)) + } + + @Test fun versionTenStockPanelsReturnToGrayOnce() { + val fresh = InputLibraries.read(context) + val blue = fresh.layouts.filter { it.id == InputLibraries.DECK || it.id == InputLibraries.TRACKPAD } + .map { it.copy(style = it.style.copy(tint = ControllerPrefs.STEAM_BLUE)) } + val old = JSONObject(InputLibraries.encode(blue.fold(fresh) { library, layout -> library.withLayout(layout) })).put("version", 10) + prefs.edit().putString("library", old.toString()).commit() + val upgraded = InputLibraries.read(context) + assertNull(upgraded.layouts.single { it.id == InputLibraries.DECK }.style.tint) + assertNull(upgraded.layouts.single { it.id == InputLibraries.TRACKPAD }.style.tint) + assertEquals(ControllerPrefs.STEAM_BLUE, upgraded.layouts.single { it.id == InputLibraries.OVERLAY }.style.tint) + assertEquals(upgraded, InputLibraries.read(context)) + } + private fun versionElevenPanel(layout: SavedInputLayout, commands: List, padTop: Float = .14f) = layout.copy(elements = commands.mapIndexed { position, i -> + InputSurfaceElement(InputControl.A, .02f + position * .3f, .01f, .28f, .11f, "command:$i", name = "Command $i", icon = InputButtonIcons.MENU) + } + layout.elements.map { if (it.kind == InputElementKind.TRACKPAD) it.copy(y = padTop, height = it.y + it.height - padTop) else it }) + + private fun versionElevenLibrary(change: (InputLibrary) -> InputLibrary = { it }): InputLibrary { + val fresh = InputLibraries.read(context) + val stock = InputStockLayouts.layouts() + var old = fresh.copy(layouts = fresh.layouts.filter { it.id == InputLibraries.OVERLAY || it.id == InputLibraries.DECK || it.id == InputLibraries.TRACKPAD }, + bindings = fresh.bindings.filter { b -> DeckPanel.entries.none { it != DeckPanel.BOTH && it.bindings == b.id } }) + old = old.withLayout(versionElevenPanel(stock.single { it.id == InputLibraries.DECK }, listOf(2, 3, 0))) + .withLayout(versionElevenPanel(stock.single { it.id == InputLibraries.TRACKPAD }, listOf(0, 1))) + old = old.copy(bindings = old.bindings.map { b -> + when (b.layoutId) { + InputLibraries.DECK -> b.copy(elements = b.elements + mapOf("command:0" to InputAction.Command(InputCommand.MENU), "command:2" to InputAction.Command(InputCommand.STEAM))) + InputLibraries.TRACKPAD -> b.copy(elements = b.elements + ("command:1" to InputAction.Command(InputCommand.KEYBOARD))) + else -> b + } + }) + return change(old).validate() + } + + private fun storeVersionEleven(library: InputLibrary) { + prefs.edit().putString("library", JSONObject(InputLibraries.encode(library)).put("version", 11).toString()).commit() + } + + @Test fun versionTwelveMovesStockPanelCommandsToTheBarAndAddsTheDeckTabs() { + val mine = SavedInputLayout("mine", "Mine", listOf(InputSurfaceElement(InputControl.A, .1f, .1f, .2f, .2f, "command:0", name = "Menu")), + screen = InputLayoutScreen.SECONDARY) + val minePreset = SavedInputBindings("mine.bindings", "Mine", false, mine.id, elements = mapOf("command:0" to InputAction.Command(InputCommand.MENU))) + val old = versionElevenLibrary { it.withLayout(mine).withPreset(minePreset) } + assertTrue(old.layouts.single { it.id == InputLibraries.DECK }.elements.any { it.id.startsWith("command:") }) + storeVersionEleven(old) + val upgraded = InputLibraries.read(context) + val stock = InputStockLayouts.layouts() + for (id in listOf(InputLibraries.DECK, InputLibraries.TRACKPAD)) + assertEquals(stock.single { it.id == id }.elements, upgraded.layouts.single { it.id == id }.elements) + assertTrue(upgraded.bindings.filter { it.layoutId == InputLibraries.DECK || it.layoutId == InputLibraries.TRACKPAD } + .all { b -> b.elements.keys.none { it.startsWith("command:") } }) + assertEquals(InputAction.Pointer(), upgraded.preset(InputStockLayouts.TRACKPAD).elements["legacy:right-pad"]) + assertEquals(mine, upgraded.layouts.single { it.id == mine.id }) + assertEquals(minePreset, upgraded.preset(minePreset.id)) + for (panel in DeckPanel.entries) { + assertEquals(stock.single { it.id == panel.layout }, upgraded.layouts.single { it.id == panel.layout }) + assertEquals(panel.layout, upgraded.preset(panel.bindings).layoutId) + } + assertEquals(old.profiles, upgraded.profiles) + assertEquals(upgraded, InputLibraries.read(context)) + } + + @Test fun versionTwelveLeavesEditedStockPanelsAlone() { + val old = versionElevenLibrary { library -> + val trackpad = library.layouts.single { it.id == InputLibraries.TRACKPAD } + library.withLayout(trackpad.copy(elements = trackpad.elements.map { if (it.kind == InputElementKind.TRACKPAD) it.copy(x = .1f, width = .8f) else it })) + } + storeVersionEleven(old) + val upgraded = InputLibraries.read(context) + assertEquals(old.layouts.single { it.id == InputLibraries.TRACKPAD }, upgraded.layouts.single { it.id == InputLibraries.TRACKPAD }) + assertEquals(old.preset(InputStockLayouts.TRACKPAD), upgraded.preset(InputStockLayouts.TRACKPAD)) + assertEquals(InputStockLayouts.layouts().single { it.id == InputLibraries.DECK }.elements, upgraded.layouts.single { it.id == InputLibraries.DECK }.elements) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/input/InputProfilesTest.kt b/app/src/test/java/com/droiddeck/launcher/input/InputProfilesTest.kt new file mode 100644 index 0000000000..e5fc41f09a --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/input/InputProfilesTest.kt @@ -0,0 +1,83 @@ +package com.droiddeck.launcher.input + +import android.content.Context +import org.junit.Assert.* +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.annotation.Config + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [34], manifest = Config.NONE) +class InputProfilesTest { + private val context: Context get() = RuntimeEnvironment.getApplication() + @Before fun reset() { + context.getSharedPreferences("input-profiles", Context.MODE_PRIVATE).edit().clear().commit() + context.getSharedPreferences("controller", Context.MODE_PRIVATE).edit().clear().commit() + context.getSharedPreferences("input-surfaces", Context.MODE_PRIVATE).edit().clear().commit() + } + + @Test fun migrationKeepsLegacyTouchMappingSeparateFromPhysicalControls() { + ControllerPrefs.setTarget(context, "a", "b") + ControllerPrefs.setTarget(context, "x", "off") + ControllerPrefs.setSelectSteam(context, true) + val game = InputProfiles.read(context, InputProfile.GAME) + assertEquals(InputAction.Native(InputControl.B), game.action(InputRole.PRIMARY, InputControl.A)) + assertEquals(InputAction.Disabled, game.action(InputRole.PRIMARY, InputControl.X)) + assertEquals(InputAction.Native(), game.action(InputRole.PHYSICAL, InputControl.A)) + assertEquals(InputAction.Native(InputControl.GUIDE), game.action(InputRole.PHYSICAL, InputControl.SELECT)) + assertEquals(InputAction.Native(InputControl.GUIDE), InputProfiles.read(context, InputProfile.DESKTOP) + .action(InputRole.PHYSICAL, InputControl.SELECT)) + InputProfiles.reset(context, InputProfile.GAME) + assertEquals(InputAction.Native(), InputProfiles.read(context, InputProfile.GAME).action(InputRole.PRIMARY, InputControl.A)) + } + + @Test fun migrationPreservesSavedDesktopSelectBinding() { + ControllerPrefs.setSelectSteam(context, true) + context.getSharedPreferences("input-profiles", Context.MODE_PRIVATE).edit().putString("profile.desktop", + """{"version":1,"bindings":[{"role":"PHYSICAL","control":"select","action":{"type":"disabled"}}]}""").commit() + assertEquals(InputAction.Disabled, InputProfiles.read(context, InputProfile.DESKTOP) + .action(InputRole.PHYSICAL, InputControl.SELECT)) + } + + @Test fun migrationKeepsDesktopKeyboardWhenSelectToSteamIsOff() { + assertEquals(InputAction.Command(InputCommand.KEYBOARD), InputProfiles.read(context, InputProfile.DESKTOP) + .action(InputRole.PHYSICAL, InputControl.SELECT)) + } + + @Test fun bindingsAndDeviceKeysRoundTripWithoutAffectingOtherProfile() { + val game = InputProfile.defaults(InputProfile.GAME) + .withBinding(InputRole.SECONDARY, InputControl.A, InputAction.Keys(listOf(29, 30))) + .copy(keys = listOf(PhysicalInputBinding(PhysicalInputKey("1:2:descriptor", "Thor", 131, 59), InputAction.Command(InputCommand.MENU)))) + InputProfiles.save(context, game) + assertEquals(game, InputProfiles.read(context, InputProfile.GAME)) + assertEquals(InputAction.Pointer(), InputProfiles.read(context, InputProfile.DESKTOP).action(InputRole.PHYSICAL, InputControl.LS)) + } + + @Test fun profileSelectionIsExplicitAndNeverDependsOnDisplayPresence() { + assertEquals(InputProfile.DESKTOP, InputProfiles.active(context, true).id) + assertEquals(InputProfile.GAME, InputProfiles.active(context, false).id) + InputProfiles.select(context, InputProfile.GAME) + assertEquals(InputProfile.GAME, InputProfiles.active(context, true).id) + } + + @Test fun unknownVersionDoesNotExecuteBindings() { + context.getSharedPreferences("input-profiles", Context.MODE_PRIVATE).edit() + .putString("profile.game", """{"version":999,"bindings":[]} """).commit() + val profile = InputProfiles.read(context, InputProfile.GAME) + assertEquals(InputProfile.defaults(InputProfile.GAME).bindings, profile.bindings) + assertEquals(mapOf( + InputRole.PRIMARY to mapOf("builtin:keyboard" to InputAction.Command(InputCommand.KEYBOARD)), + ), profile.elements) + } + + @Test fun layoutsPersistIndependentOfBindingsAndStayInsideScreen() { + InputSurfaceLayout.save(context, InputProfile.GAME, listOf(InputSurfaceElement(InputControl.A, .95f, -.1f, 2f, 2f))) + val element = InputSurfaceLayout.read(context, InputProfile.GAME).single() + assertEquals(.95f, element.x); assertEquals(0f, element.y) + assertTrue(element.x + element.width <= 1f) + assertEquals(InputAction.Native(), InputProfiles.read(context, InputProfile.GAME).action(InputRole.SECONDARY, InputControl.A)) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/input/InputRouterTest.kt b/app/src/test/java/com/droiddeck/launcher/input/InputRouterTest.kt new file mode 100644 index 0000000000..1b0e86fbbe --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/input/InputRouterTest.kt @@ -0,0 +1,260 @@ +package com.droiddeck.launcher.input + +import org.junit.Assert.* +import org.junit.Test + +class InputRouterTest { + private class Output : InputOutput { + val events = mutableListOf() + override fun key(code: Int, down: Boolean) { events += "key $code $down" } + override fun button(code: Int, down: Boolean) { events += "button $code $down" } + override fun pointer(dx: Float, dy: Float) { events += "pointer $dx $dy" } + override fun scroll(steps: Int) { events += "scroll $steps" } + override fun scrollHorizontal(steps: Int) { events += "horizontal $steps" } + override fun command(command: InputCommand) { events += command.name } + override fun requestMotion() { events += "motion" } + } + private val output = Output() + private val router = InputRouter(output) + private fun bind(control: InputControl, action: InputAction, role: InputRole = InputRole.PHYSICAL) = + InputProfile.defaults(InputProfile.GAME).withBinding(role, control, action) + + @Test fun radialKeysKeepCustomDiagonalsAndReleaseSharedNeighborKeys() { + router.setProfile(bind(InputControl.LS, InputAction.Directions(103, 106, 0, 105, listOf(30, 0, 0, 0)))) + val stick = router.channel(InputRole.PHYSICAL, InputControl.LS) + stick.update(.7f, -.7f) + assertEquals(listOf("key 30 true"), output.events) + stick.update(-.7f, -.7f) + assertEquals(listOf("key 30 true", "key 30 false", "key 105 true", "key 103 true"), output.events) + output.events.clear() + stick.update(0f, -1f) + assertEquals(listOf("key 105 false"), output.events) + router.releaseAll() + assertEquals(listOf("key 105 false", "key 103 false"), output.events) + } + + @Test fun horizontalStickUsesXAndKeepsTheVerticalWheelIndependent() { + router.setProfile(bind(InputControl.RS, InputAction.Scroll(20f, true)).withBinding(InputRole.PHYSICAL, InputControl.LS, InputAction.Scroll(20f))) + val horizontal = router.channel(InputRole.PHYSICAL, InputControl.RS) + val vertical = router.channel(InputRole.PHYSICAL, InputControl.LS) + horizontal.update(1f, 0f); vertical.update(0f, -1f) + router.tick(.05f) + assertTrue(output.events.contains("horizontal 1")) + assertTrue(output.events.contains("scroll -1")) + output.events.clear() + horizontal.update(0f, 1f); vertical.update(1f, 0f) + assertFalse(router.tick(.05f)) + assertTrue(output.events.isEmpty()) + } + + @Test fun horizontalTouchScrollFollowsContactAndDropsRemainderOnSuspend() { + val channel = router.channel(InputRole.SECONDARY, InputControl.RIGHT_PAD, InputAction.Scroll(10f, true)) + channel.update(0f, 0f, true) + channel.update(.25f, .8f, true) + assertEquals(listOf("horizontal -2"), output.events) + router.releaseAll() + output.events.clear() + channel.update(0f, 0f, false) + channel.update(0f, 0f, true) + channel.update(.06f, 0f, true) + assertTrue(output.events.isEmpty()) + } + + @Test fun nativeProfileReturnsOriginalStateWithoutScheduling() { + val source = router.pad(InputRole.PHYSICAL) + val state = PadState().apply { leftX = .7f; press(PadState.A, true) } + repeat(1000) { assertSame(state, source.route(state)) } + assertTrue(output.events.isEmpty()) + } + + @Test fun retainedPadReturnsToNativeFastPathAfterHeldControlsReachNeutral() { + val raw = PadState().apply { press(PadState.A, true); leftX = .8f } + val source = router.pad(InputRole.PHYSICAL, raw) + raw.press(PadState.B, true) + val blocked = source.route(raw) + assertFalse(blocked.isDown(PadState.A)) + assertEquals(0f, blocked.leftX) + assertTrue(blocked.isDown(PadState.B)) + raw.clear() + source.route(raw) + raw.press(PadState.A, true) + repeat(1000) { assertSame(raw, source.route(raw)) } + assertTrue(output.events.isEmpty()) + } + + @Test fun localBindingConsumesOnlyItsOwnNativeControlAndReleasesModifiersLast() { + router.setProfile(bind(InputControl.A, InputAction.Keys(listOf(29, 30)))) + val source = router.pad(InputRole.PHYSICAL) + val state = PadState().apply { press(PadState.A, true); press(PadState.B, true); leftX = .4f } + val routed = source.route(state) + assertFalse(routed.isDown(PadState.A)); assertTrue(routed.isDown(PadState.B)) + assertEquals(.4f, routed.leftX) + source.route(state) + state.press(PadState.A, false); source.route(state) + assertEquals(listOf("key 29 true", "key 30 true", "key 30 false", "key 29 false"), output.events) + } + + @Test fun twoSourcesHoldingOneKeyDoNotReleaseEachOther() { + val p = bind(InputControl.A, InputAction.Keys(listOf(57))) + .withBinding(InputRole.SECONDARY, InputControl.B, InputAction.Keys(listOf(57))) + router.setProfile(p) + val physical = router.pad(InputRole.PHYSICAL) + val touch = router.pad(InputRole.SECONDARY) + physical.route(PadState().apply { press(PadState.A, true) }) + touch.route(PadState().apply { press(PadState.B, true) }) + touch.close() + assertEquals(listOf("key 57 true"), output.events) + physical.release() + assertEquals(listOf("key 57 true", "key 57 false"), output.events) + } + + @Test fun remapCombinesHeldTargetsRatherThanLastButtonWinning() { + router.setProfile(bind(InputControl.A, InputAction.Native(InputControl.B))) + val source = router.pad(InputRole.PHYSICAL) + val raw = PadState().apply { press(PadState.A, true) } + assertTrue(source.route(raw).isDown(PadState.B)) + raw.press(PadState.B, true); raw.press(PadState.A, false) + assertTrue(source.route(raw).isDown(PadState.B)) + raw.press(PadState.B, false) + assertFalse(source.route(raw).isDown(PadState.B)) + } + + @Test fun profileChangeBlocksHeldNativeInputUntilNeutral() { + val source = router.pad(InputRole.PHYSICAL) + val raw = PadState().apply { press(PadState.A, true) } + source.route(raw) + router.setProfile(bind(InputControl.A, InputAction.Keys(listOf(1)))) + source.route(raw) + assertTrue(output.events.isEmpty()) + raw.press(PadState.A, false); source.route(raw) + raw.press(PadState.A, true); source.route(raw) + assertEquals(listOf("key 1 true"), output.events) + } + + @Test fun changingLocalBindingReleasesOldKeyWithoutPressingNewOne() { + router.setProfile(bind(InputControl.A, InputAction.Keys(listOf(1)))) + val source = router.pad(InputRole.PHYSICAL) + val raw = PadState().apply { press(PadState.A, true) } + source.route(raw) + router.setProfile(bind(InputControl.A, InputAction.Keys(listOf(28)))) + source.route(raw) + assertEquals(listOf("key 1 true", "key 1 false"), output.events) + } + + @Test fun triggersUseHysteresisAndCommandsFireOnce() { + router.setProfile(bind(InputControl.LT, InputAction.Command(InputCommand.MENU))) + val source = router.pad(InputRole.PHYSICAL) + val raw = PadState() + for (value in listOf(.6f, .5f, .4f, .6f, .3f, .6f)) { raw.leftTrigger = value; source.route(raw) } + assertEquals(listOf("MENU"), output.events) + raw.leftTrigger = 0f; source.route(raw) + assertEquals(listOf("MENU", "MENU"), output.events) + } + + @Test fun commandsFireOnReleaseAndNotWhenCancelled() { + router.setProfile(bind(InputControl.A, InputAction.Command(InputCommand.KEYBOARD), InputRole.PRIMARY)) + val button = router.channel(InputRole.PRIMARY, InputControl.A) + button.update(1f) + assertTrue(output.events.isEmpty()) + button.update(0f) + assertEquals(listOf("KEYBOARD"), output.events) + button.update(1f) + button.release() + button.update(0f) + router.releaseAll() + button.update(1f) + router.releaseAll() + button.update(0f) + assertEquals(listOf("KEYBOARD"), output.events) + } + + @Test fun pointerTicksOnlyWhileMovingAndOrdinaryKeysDoNotWaitForTick() { + router.setProfile(InputProfile.defaults(InputProfile.DESKTOP)) + val source = router.pad(InputRole.PHYSICAL) + val raw = PadState().apply { leftX = 1f; press(PadState.X, true) } + source.route(raw) + assertTrue(output.events.contains("key 28 true")) + assertFalse(output.events.any { it.startsWith("pointer") }) + assertTrue(router.tick(.01f)); assertTrue(output.events.contains("pointer 9.0 0.0")) + raw.clear(); source.route(raw) + assertFalse(router.tick(.01f)) + } + + @Test fun trackpadUsesContactRelativeDeltasWithoutFirstTouchJump() { + router.setProfile(bind(InputControl.RIGHT_PAD, InputAction.Pointer(1000f), InputRole.SECONDARY)) + val channel = router.channel(InputRole.SECONDARY, InputControl.RIGHT_PAD) + assertNull(channel.update(.5f, .5f, true)) + assertTrue(output.events.isEmpty()) + channel.update(.6f, .6f, true) + assertEquals(1, output.events.size) + assertTrue(output.events[0].startsWith("pointer 100.")) + channel.update(0f, 0f, false) + channel.update(-.9f, -.9f, true) + assertEquals(1, output.events.size) + } + + @Test fun stickScrollFollowsUpAndDownForEveryInputRoleAndStopsOnRelease() { + for (role in InputRole.entries) { + router.setProfile(bind(InputControl.RS, InputAction.Scroll(10f), role)) + val channel = router.channel(role, InputControl.RS) + output.events.clear() + channel.update(0f, -1f) + repeat(2) { assertTrue(router.tick(.05f)) } + assertEquals(listOf("motion", "scroll -1"), output.events) + channel.update(0f, 1f) + repeat(2) { assertTrue(router.tick(.05f)) } + assertEquals("scroll 1", output.events.last()) + channel.release() + val count = output.events.size + assertFalse(router.tick(.05f)) + assertEquals(count, output.events.size) + channel.close() + } + } + + @Test fun trackpadScrollKeepsItsContactDirectionAndDoesNotScheduleStickMotion() { + router.setProfile(bind(InputControl.LEFT_PAD, InputAction.Scroll(10f), InputRole.SECONDARY)) + val channel = router.channel(InputRole.SECONDARY, InputControl.LEFT_PAD) + channel.update(0f, 0f, true) + channel.update(0f, .5f, true) + channel.update(0f, 0f, true) + assertEquals(listOf("scroll 5", "scroll -5"), output.events) + assertFalse(router.tick(.05f)) + } + + @Test fun disablingRoutingReleasesKeysAndBlocksHeldInputOnResume() { + router.setProfile(bind(InputControl.A, InputAction.Keys(listOf(1)))) + val source = router.pad(InputRole.PHYSICAL) + val raw = PadState().apply { press(PadState.A, true) } + source.route(raw); router.setEnabled(false); source.route(raw); router.setEnabled(true); source.route(raw) + assertEquals(listOf("key 1 true", "key 1 false"), output.events) + raw.clear(); source.route(raw); raw.press(PadState.A, true); source.route(raw) + assertEquals("key 1 true", output.events.last()) + } + + @Test fun disabledSourcesDoNotLeakNativeButtons() { + val source = router.pad(InputRole.PHYSICAL) + router.setEnabled(false) + assertFalse(source.route(PadState().apply { press(PadState.A, true) }).isDown(PadState.A)) + } + + @Test fun lifecycleReleaseAlsoBlocksNativeIdentityRoute() { + val source = router.pad(InputRole.PHYSICAL) + val raw = PadState().apply { press(PadState.A, true) } + source.route(raw); router.releaseAll() + assertFalse(source.route(raw).isDown(PadState.A)) + } + + @Test fun mergedPadKeepsPhysicalButtonAndWholeStickVector() { + val physical = PadState().apply { press(PadState.A, true); leftX = .8f; leftY = -.6f } + physical.merge(PadState().apply { leftX = -.2f; leftY = .1f }) + assertTrue(physical.isDown(PadState.A)); assertEquals(.8f, physical.leftX); assertEquals(-.6f, physical.leftY) + } + + @Test fun outputOwnershipKeepsKeyboardAndMappingHoldsIndependent() { + val held = HeldInput(output::key) + held.set(1, 29, true); held.set(4, 29, true); held.release(4) + assertEquals(listOf("key 29 true"), output.events) + held.clear(); assertEquals("key 29 false", output.events.last()) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/input/InputSourceOwnershipTest.kt b/app/src/test/java/com/droiddeck/launcher/input/InputSourceOwnershipTest.kt new file mode 100644 index 0000000000..b200a195d7 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/input/InputSourceOwnershipTest.kt @@ -0,0 +1,291 @@ +package com.droiddeck.launcher.input + +import java.io.File +import org.junit.Assert.* +import org.junit.After +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.annotation.Config +import org.robolectric.util.ReflectionHelpers + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [34], manifest = Config.NONE, shadows = [JoyConPadTest.RecordingWriter::class]) +class InputSourceOwnershipTest { + private val bridge = PadBridge(File(RuntimeEnvironment.getApplication().cacheDir, "input-ownership")) + private fun state() = JoyConPadTest.RecordingWriter.pads[0]!! + @After fun close() { bridge.stop(); DeckControls.releaseAll(); DeckControls.setRouter(null) } + + @Test fun profileAndEnableChangesLeaveRingClosedUntilInputArrives() { + bridge.setInputRouter(InputRouter(RecordingOutput())) + bridge.setInputProfile(InputProfile.defaults(InputProfile.DESKTOP)) + bridge.setInputEnabled(false) + bridge.setInputEnabled(true) + assertFalse(ReflectionHelpers.getField(bridge, "open")) + bridge.applyExternal({ it.press(PadState.X, true) }, true) + assertTrue(ReflectionHelpers.getField(bridge, "open")) + } + + @Test fun disablingRoutingPublishesReleaseToAnAlreadyOpenRing() { + bridge.setInputRouter(InputRouter(RecordingOutput())) + bridge.applyExternal({ it.press(PadState.A, true) }, true) + assertTrue(state().isDown(PadState.A)) + bridge.setInputEnabled(false) + assertFalse(state().isDown(PadState.A)) + bridge.setInputEnabled(true) + assertFalse(state().isDown(PadState.A)) + } + + @Test fun repeatedEnableStateDoesNotRepublishPadOrDeckReports() { + val router = InputRouter(RecordingOutput()) + bridge.setInputRouter(router) + DeckControls.setRouter(router) + bridge.applyExternal({ it.press(PadState.A, true) }, true) + DeckControls.setGrip(DeckControls.L4, true) + for (enabled in listOf(true, false, true)) { + bridge.setInputEnabled(enabled) + val padWrites = JoyConPadTest.RecordingWriter.padWrites + val deckWrites = JoyConPadTest.RecordingWriter.deckWrites + repeat(10) { bridge.setInputEnabled(enabled) } + assertEquals(padWrites, JoyConPadTest.RecordingWriter.padWrites) + assertEquals(deckWrites, JoyConPadTest.RecordingWriter.deckWrites) + } + assertFalse(state().isDown(PadState.A)) + assertEquals(0, JoyConPadTest.RecordingWriter.grips) + bridge.applyExternal({ it.press(PadState.A, false) }, true) + DeckControls.setGrip(DeckControls.L4, false) + bridge.applyExternal({ it.press(PadState.A, true) }, true) + DeckControls.setGrip(DeckControls.L4, true) + assertTrue(state().isDown(PadState.A)) + assertEquals(DeckControls.L4, JoyConPadTest.RecordingWriter.grips) + } + + @Test fun replacingADisabledRouterAllowsTheNewRouterToBeDisabled() { + bridge.setInputRouter(InputRouter(RecordingOutput())) + bridge.setInputEnabled(false) + bridge.setInputRouter(InputRouter(RecordingOutput())) + bridge.applyExternal({ it.press(PadState.A, true) }, true) + assertTrue(state().isDown(PadState.A)) + bridge.setInputEnabled(false) + assertFalse(state().isDown(PadState.A)) + } + + @Test fun attachingADisabledRouterAllowsItToBeEnabled() { + bridge.setInputRouter(InputRouter(RecordingOutput()).also { it.setEnabled(false) }) + bridge.setInputEnabled(true) + bridge.applyExternal({ it.press(PadState.A, true) }, true) + assertTrue(state().isDown(PadState.A)) + } + + @Test fun replacingRouterReleasesRetainedPhysicalAndTouchHoldsUntilNeutral() { + val before = RecordingOutput() + val after = RecordingOutput() + val profile = InputProfile.defaults(InputProfile.GAME) + .withBinding(InputRole.PHYSICAL, InputControl.A, InputAction.Keys(listOf(29))) + .withBinding(InputRole.SECONDARY, InputControl.B, InputAction.Keys(listOf(29))) + bridge.setInputRouter(InputRouter(before).also { it.setProfile(profile) }) + bridge.applyExternal({ it.press(PadState.A, true) }, true) + val touch = Any() + bridge.applyTouch(touch, InputRole.SECONDARY) { it.press(PadState.B, true) } + bridge.setInputRouter(InputRouter(after).also { it.setProfile(profile) }) + bridge.applyExternal({ it.press(PadState.X, true) }, true) + assertEquals(listOf("key 29 true", "key 29 false"), before.events) + assertTrue(after.events.isEmpty()) + bridge.applyExternal({ it.press(PadState.A, false) }, true) + bridge.applyExternal({ it.press(PadState.A, true) }, true) + assertEquals(listOf("key 29 true"), after.events) + bridge.applyExternal({ it.press(PadState.A, false) }, true) + assertEquals(listOf("key 29 true", "key 29 false"), after.events) + bridge.applyTouch(touch, InputRole.SECONDARY) { it.press(PadState.B, false) } + bridge.applyTouch(touch, InputRole.SECONDARY) { it.press(PadState.B, true) } + assertEquals("key 29 true", after.events.last()) + } + + @Test fun replacingRouterReleasesRetainedNativeButtonsAndStick() { + bridge.setInputRouter(InputRouter(RecordingOutput())) + bridge.applyExternal({ it.press(PadState.A, true); it.leftX = .8f }, true) + bridge.setInputRouter(InputRouter(RecordingOutput())) + assertFalse(state().isDown(PadState.A)) + assertEquals(0f, state().leftX) + bridge.applyExternal({ it.press(PadState.B, true) }, true) + assertTrue(state().isDown(PadState.B)) + assertFalse(state().isDown(PadState.A)) + assertEquals(0f, state().leftX) + bridge.applyExternal({ it.press(PadState.A, false); it.leftX = 0f }, true) + bridge.applyExternal({ it.press(PadState.A, true); it.leftX = .8f }, true) + assertTrue(state().isDown(PadState.A)) + assertEquals(.8f, state().leftX) + } + + @Test fun replacingDeckRouterBlocksExistingGripAndContactUntilRelease() { + DeckControls.setRouter(InputRouter(RecordingOutput())) + val surface = DeckControls.source(InputRole.SECONDARY) + try { + surface.setGrip(DeckControls.L4, true) + surface.setPad(false, true, .3f, .4f) + DeckControls.setRouter(InputRouter(RecordingOutput())) + assertEquals(0, JoyConPadTest.RecordingWriter.grips) + surface.setPad(false, true, .5f, .6f) + assertEquals(0, JoyConPadTest.RecordingWriter.grips) + surface.setGrip(DeckControls.L4, false) + surface.setPad(false, false) + surface.setGrip(DeckControls.L4, true) + surface.setPad(false, true, .2f, .3f) + assertEquals(DeckControls.L4 or 16, JoyConPadTest.RecordingWriter.grips) + } finally { surface.close() } + } + + @Test fun clearingOldDeckOwnerPreservesReplacementBindingsAndHeldKeys() { + val previous = InputRouter(RecordingOutput()) + val output = RecordingOutput() + val replacement = InputRouter(output).also { + it.setProfile(InputProfile.defaults(InputProfile.GAME) + .withBinding(InputRole.PHYSICAL, InputControl.L4, InputAction.Keys(listOf(57)))) + } + DeckControls.setRouter(previous) + DeckControls.setRouter(replacement) + DeckControls.setGrip(DeckControls.L4, true) + DeckControls.clearRouter(previous) + DeckControls.clearRouter(null) + assertEquals(listOf("key 57 true"), output.events) + DeckControls.setGrip(DeckControls.L4, false) + DeckControls.setGrip(DeckControls.L4, true) + assertEquals(listOf("key 57 true", "key 57 false", "key 57 true"), output.events) + assertEquals(0, JoyConPadTest.RecordingWriter.grips) + } + + @Test fun clearingCurrentDeckOwnerReleasesItsKeysAndRemovesBindings() { + val output = RecordingOutput() + val router = InputRouter(output).also { + it.setProfile(InputProfile.defaults(InputProfile.GAME) + .withBinding(InputRole.PHYSICAL, InputControl.L4, InputAction.Keys(listOf(57)))) + } + DeckControls.setRouter(router) + DeckControls.setGrip(DeckControls.L4, true) + DeckControls.clearRouter(router) + DeckControls.clearRouter(router) + DeckControls.setGrip(DeckControls.L4, false) + DeckControls.setGrip(DeckControls.L4, true) + assertEquals(listOf("key 57 true", "key 57 false"), output.events) + assertEquals(DeckControls.L4, JoyConPadTest.RecordingWriter.grips) + } + + @Test fun releasingAllKeepsPhysicalGripsBlockedUntilNeutral() { + val router = InputRouter(RecordingOutput()) + bridge.setInputRouter(router) + DeckControls.setRouter(router) + DeckControls.setGrip(DeckControls.L4, true) + assertEquals(DeckControls.L4, JoyConPadTest.RecordingWriter.grips) + bridge.releaseAll() + DeckControls.setGrip(DeckControls.L4, true) + assertEquals(0, JoyConPadTest.RecordingWriter.grips) + DeckControls.setGrip(DeckControls.L4, false) + DeckControls.setGrip(DeckControls.L4, true) + assertEquals(DeckControls.L4, JoyConPadTest.RecordingWriter.grips) + } + + @Test fun cancellingHomeReleasesDeckOutputAndKeepsGripsBlockedUntilNeutral() { + val router = InputRouter(RecordingOutput()) + bridge.setInputRouter(router) + DeckControls.setRouter(router) + bridge.setHomeGuidePressed(true) + DeckControls.setGrip(DeckControls.L4, true) + assertEquals(DeckControls.L4, JoyConPadTest.RecordingWriter.grips) + bridge.cancelHomeGuide() + assertEquals(0, JoyConPadTest.RecordingWriter.grips) + DeckControls.setGrip(DeckControls.L4, true) + assertEquals(0, JoyConPadTest.RecordingWriter.grips) + DeckControls.setGrip(DeckControls.L4, false) + DeckControls.setGrip(DeckControls.L4, true) + assertEquals(DeckControls.L4, JoyConPadTest.RecordingWriter.grips) + } + + private class RecordingOutput : InputOutput { + val events = mutableListOf() + override fun key(code: Int, down: Boolean) { events += "key $code $down" } + override fun button(code: Int, down: Boolean) { events += "button $code $down" } + override fun pointer(dx: Float, dy: Float) { events += "pointer" } + override fun scroll(steps: Int) { events += "scroll" } + override fun command(command: InputCommand) { events += command.name } + override fun requestMotion() { events += "motion" } + } + + @Test fun removingLastTouchSurfaceReleasesLegacyMouseRoute() { + val routed = PadState() + bridge.setTouchRoute(object : PadBridge.TouchRoute { + override fun active() = true + override fun accept(state: PadState) { routed.copyFrom(state) } + }) + val first = Any(); val second = Any() + bridge.applyTouch(first, InputRole.PRIMARY) { it.press(PadState.A, true) } + bridge.applyTouch(second, InputRole.SECONDARY) { it.press(PadState.A, true) } + bridge.releaseTouch(first) + assertTrue(routed.isDown(PadState.A)) + bridge.releaseTouch(second) + assertFalse(routed.isDown(PadState.A)) + } + + @Test fun nativeTrackpadKeepsItsOwnerUntilThatContactEnds() { + val first = DeckControls.source(InputRole.PRIMARY) + val second = DeckControls.source(InputRole.SECONDARY) + try { + first.setPad(false, true, .25f, .5f) + val firstPosition = JoyConPadTest.RecordingWriter.deckPads.copyOf() + second.setPad(false, true, -.75f, -.5f) + assertArrayEquals(firstPosition, JoyConPadTest.RecordingWriter.deckPads) + first.setPad(false, false) + assertTrue(JoyConPadTest.RecordingWriter.deckPads[0] < 0) + second.close() + assertEquals(0, JoyConPadTest.RecordingWriter.grips and 16) + } finally { first.close(); second.close() } + } + + @Test fun closingOneSurfaceDoesNotReleaseAnotherSurfaceOrPhysicalPad() { + val a = Any(); val b = Any() + bridge.applyExternal({ it.press(PadState.A, true) }, true) + bridge.applyTouch(a, InputRole.PRIMARY) { it.press(PadState.A, true); it.press(PadState.B, true) } + bridge.applyTouch(b, InputRole.SECONDARY) { it.press(PadState.X, true) } + bridge.releaseTouch(a) + assertTrue(state().isDown(PadState.A)); assertTrue(state().isDown(PadState.X)); assertFalse(state().isDown(PadState.B)) + bridge.releaseTouch(b) + assertTrue(state().isDown(PadState.A)); assertFalse(state().isDown(PadState.X)) + } + + @Test fun disconnectingSecondaryDeckSurfaceKeepsPhysicalGripHeld() { + val surface = DeckControls.source(InputRole.SECONDARY) + DeckControls.setGrip(DeckControls.L4, true) + surface.setGrip(DeckControls.L4, true) + surface.close() + assertEquals(DeckControls.L4, JoyConPadTest.RecordingWriter.grips) + DeckControls.setGrip(DeckControls.L4, false) + assertEquals(0, JoyConPadTest.RecordingWriter.grips) + } + + @Test fun nativePadAndTouchMappingsUseDifferentRoles() { + val output = object : InputOutput { + override fun key(code: Int, down: Boolean) {} + override fun button(code: Int, down: Boolean) {} + override fun pointer(dx: Float, dy: Float) {} + override fun scroll(steps: Int) {} + override fun command(command: InputCommand) {} + override fun requestMotion() {} + } + val router = InputRouter(output) + bridge.setInputRouter(router) + bridge.setInputProfile(InputProfile.defaults(InputProfile.GAME).withBinding(InputRole.SECONDARY, InputControl.A, InputAction.Disabled)) + val touch = Any() + bridge.applyTouch(touch, InputRole.SECONDARY) { it.press(PadState.A, true) } + assertFalse(state().isDown(PadState.A)) + bridge.applyExternal({ it.press(PadState.A, true) }, true) + assertTrue(state().isDown(PadState.A)) + bridge.releaseAll() + assertFalse(state().isDown(PadState.A)) + bridge.applyExternal({ it.press(PadState.B, true) }, true) + assertFalse(state().isDown(PadState.A)) + assertTrue(state().isDown(PadState.B)) + bridge.applyExternal({ it.press(PadState.A, false) }, true) + bridge.applyExternal({ it.press(PadState.A, true) }, true) + assertTrue(state().isDown(PadState.A)) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/input/InputSurfaceSnapTest.kt b/app/src/test/java/com/droiddeck/launcher/input/InputSurfaceSnapTest.kt new file mode 100644 index 0000000000..be726ff63c --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/input/InputSurfaceSnapTest.kt @@ -0,0 +1,33 @@ +package com.droiddeck.launcher.input + +import org.junit.Assert.* +import org.junit.Test + +class InputSurfaceSnapTest { + private val element = InputSurfaceElement(InputControl.A, .395f, .3f, .2f, .2f) + @Test fun snapsToCanvasCenterWithoutChangingSize() { + val result = snapSurfaceElement(element, emptyList(), .01f, .01f) + assertEquals(.4f, result.element.x, .0001f) + assertEquals(.5f, result.x!!, 0f) + assertEquals(element.width, result.element.width, 0f) + } + @Test fun alignsEdgesWithOtherElementsAndLeavesDistantPositionsFree() { + val other = element.copy(x = .1f, y = .65f) + val aligned = snapSurfaceElement(element.copy(x = .105f), listOf(other), .01f, .01f) + assertEquals(.1f, aligned.element.x, .0001f) + val free = snapSurfaceElement(element.copy(x = .36f, y = .34f), listOf(other), .01f, .01f) + assertNull(free.x); assertNull(free.y) + } + @Test fun resizingSnapsTheDraggedEdgeAndKeepsTheOppositeEdge() { + val result = snapSurfaceElement(element.copy(x = .2f, width = .295f), emptyList(), .01f, .01f, 2 or 8) + assertEquals(.2f, result.element.x, 0f) + assertEquals(.3f, result.element.width, .0001f) + } + @Test fun boundsPreventFalseGuidesAndNegativeSize() { + val e = element.copy(x = .01f, width = .98f) + val result = snapSurfaceElement(e, listOf(element.copy(x = .995f)), .02f, .01f, 1 or 4) + assertTrue(result.element.x >= 0f) + assertTrue(result.element.x + result.element.width <= 1f) + assertTrue(result.element.width >= .03f) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/input/InputSurfaceSourceTest.kt b/app/src/test/java/com/droiddeck/launcher/input/InputSurfaceSourceTest.kt new file mode 100644 index 0000000000..9ccbafe5b2 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/input/InputSurfaceSourceTest.kt @@ -0,0 +1,174 @@ +package com.droiddeck.launcher.input + +import java.io.File +import org.junit.After +import org.junit.Assert.* +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.annotation.Config + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [34], manifest = Config.NONE, shadows = [JoyConPadTest.RecordingWriter::class]) +class InputSurfaceSourceTest { + private val events = mutableListOf() + private val router = InputRouter(object : InputOutput { + override fun key(code: Int, down: Boolean) { events += "key:$code:$down" } + override fun button(code: Int, down: Boolean) { events += "button:$code:$down" } + override fun pointer(dx: Float, dy: Float) { events += "pointer" } + override fun scroll(steps: Int) { events += "scroll" } + override fun command(command: InputCommand) { events += command.name } + override fun command(command: InputCommand, role: InputRole) { events += "${command.name}:$role" } + override fun requestMotion() {} + }) + private val bridge = PadBridge(File(RuntimeEnvironment.getApplication().cacheDir, "element-input")) + private val sources = mutableListOf() + @Before fun setup() { bridge.setInputRouter(router); DeckControls.setRouter(router) } + @After fun close() { sources.forEach { it.close() }; bridge.stop(); DeckControls.setRouter(null); DeckControls.releaseAll() } + private fun button(id: String) = InputSurfaceElement(InputControl.A, .1f, .1f, .2f, .2f, id) + private fun source(e: InputSurfaceElement) = InputSurfaceSource(bridge, InputRole.SECONDARY, e).also { sources += it } + private fun bind(vararg actions: Pair) { + bridge.setInputProfile(InputProfile("test", elements = mapOf(InputRole.SECONDARY to mapOf(*actions)))) + } + private fun state() = JoyConPadTest.RecordingWriter.pads[0] ?: PadState() + + @Test fun aCommandCarriesTheRoleOfTheSurfaceThatSentIt() { + val keyboard = InputAction.Command(InputCommand.KEYBOARD) + bridge.setInputProfile(InputProfile("test", elements = mapOf(InputRole.PRIMARY to mapOf("builtin:keyboard" to keyboard), + InputRole.SECONDARY to mapOf("builtin:keyboard" to keyboard)))) + val element = button("builtin:keyboard") + val primary = InputSurfaceSource(bridge, InputRole.PRIMARY, element).also { sources += it } + val secondary = source(element) + primary.update(1f); primary.update(0f) + secondary.update(1f); secondary.update(0f) + assertEquals(listOf("KEYBOARD:PRIMARY", "KEYBOARD:SECONDARY"), events) + } + + @Test fun toggleUsesSharedOwnershipAndReleasesOnMenuAndProfileChange() { + bind("first" to InputAction.Keys(listOf(29)), "second" to InputAction.Keys(listOf(29))) + val a = source(button("first").copy(toggle = true)); val b = source(button("second")) + a.update(1f); a.update(0f) + assertTrue(a.latched) + assertEquals(listOf("key:29:true"), events) + b.update(1f) + a.update(1f); a.update(0f) + assertFalse(a.latched) + assertEquals(1, events.size) + b.update(0f) + assertEquals("key:29:false", events.last()) + a.update(1f); a.update(0f) + bridge.setInputEnabled(false) + assertFalse(a.latched) + assertEquals("key:29:false", events.last()) + bridge.setInputEnabled(true) + a.update(1f); a.update(0f) + assertEquals("key:29:true", events.last()) + bind("first" to InputAction.Keys(listOf(57))) + assertFalse(a.latched) + assertEquals("key:29:false", events.last()) + a.update(1f); a.update(0f) + assertEquals("key:57:true", events.last()) + a.release() + assertEquals("key:57:false", events.last()) + } + + @Test fun nativeToggleReleasesWithoutRepressingAcrossProfileChange() { + bind("first" to InputAction.Native(InputControl.B)) + val a = source(button("first").copy(toggle = true)) + a.update(1f); a.update(0f) + assertTrue(state().isDown(PadState.B)) + bind("first" to InputAction.Native(InputControl.X)) + assertFalse(state().isDown(PadState.B)) + a.update(1f); a.update(0f) + assertTrue(state().isDown(PadState.X)) + a.close() + assertFalse(state().isDown(PadState.X)) + } + + @Test fun twoButtonsWithTheSameShapeHaveIndependentActions() { + bind("first" to InputAction.Keys(listOf(29)), "second" to InputAction.Keys(listOf(57))) + val a = source(button("first")); val b = source(button("second")) + a.update(1f); b.update(1f); a.update(0f); b.update(0f) + assertEquals(listOf("key:29:true", "key:57:true", "key:29:false", "key:57:false"), events) + } + + @Test fun sharedLocalButtonRemainsHeldUntilBothElementsRelease() { + bind("first" to InputAction.MouseButton(272), "second" to InputAction.MouseButton(272)) + val a = source(button("first")); val b = source(button("second")) + a.update(1f); b.update(1f); a.close() + assertEquals(listOf("button:272:true"), events) + b.update(0f) + assertEquals(listOf("button:272:true", "button:272:false"), events) + a.update(1f) + assertEquals(2, events.size) + } + + @Test fun nativeTouchOutputIsNotRemappedTwiceAndKeepsOtherOwners() { + bridge.setInputProfile(InputProfile("test", bindings = mapOf(InputRole.SECONDARY to mapOf(InputControl.B to InputAction.Disabled)), + elements = mapOf(InputRole.SECONDARY to mapOf("first" to InputAction.Native(InputControl.B), "second" to InputAction.Native(InputControl.B))))) + val a = source(button("first")); val b = source(button("second")) + a.update(1f); b.update(1f) + assertTrue(state().isDown(PadState.B)) + a.close() + assertTrue(state().isDown(PadState.B)) + b.update(0f) + assertFalse(state().isDown(PadState.B)) + } + + @Test fun profileChangeReleasesOldOutputAndRequiresNeutralBeforeNewAction() { + bind("first" to InputAction.Keys(listOf(29))) + val a = source(button("first")); a.update(1f) + bind("first" to InputAction.Keys(listOf(57))) + a.update(1f) + assertEquals(listOf("key:29:true", "key:29:false"), events) + a.update(0f); a.update(1f) + assertEquals("key:57:true", events.last()) + } + + @Test fun unmappedNewButtonsDoNotInheritLegacyBindings() { + bridge.setInputProfile(InputProfile.defaults(InputProfile.DESKTOP)) + source(button("new")).update(1f) + assertTrue(events.isEmpty()) + } + + @Test fun stickClicksAreSuspendedWithTheRestOfInput() { + val e = InputSurfaceElement(InputControl.LS, .1f, .1f, .3f, .3f, "stick", InputElementKind.STICK) + bind(e.id to InputAction.Native(InputControl.RS)) + val stick = source(e) + stick.click(true) + assertTrue(state().isDown(PadState.R3)) + bridge.setInputEnabled(false) + assertFalse(state().isDown(PadState.R3)) + bridge.setInputEnabled(true); stick.click(true) + assertFalse(state().isDown(PadState.R3)) + stick.click(false); stick.click(true) + assertTrue(state().isDown(PadState.R3)) + stick.close(); stick.click(true) + assertFalse(state().isDown(PadState.R3)) + } + + @Test fun customTrackpadMovementAndClickHaveIndependentActions() { + val e = InputSurfaceElement(InputControl.LEFT_PAD, .1f, .1f, .4f, .4f, "mouse", InputElementKind.TRACKPAD) + bridge.setInputProfile(InputProfile("test", elements = mapOf(InputRole.SECONDARY to mapOf(e.id to InputAction.Pointer())), + clicks = mapOf(InputRole.SECONDARY to mapOf(e.id to InputAction.MouseButton(272))))) + val surface = source(e) + surface.update(0f, 0f, true); surface.update(.2f, .2f, true); surface.click(true) + assertEquals(listOf("pointer", "button:272:true"), events) + bridge.setInputEnabled(false) + assertEquals("button:272:false", events.last()) + bridge.setInputEnabled(true); surface.click(true) + assertEquals(3, events.size) + surface.click(false); surface.click(true); surface.close() + assertEquals(listOf("pointer", "button:272:true", "button:272:false", "button:272:true", "button:272:false"), events) + } + + @Test fun migratedTrackpadClickStillUsesTheExistingLogicalBinding() { + bridge.setInputProfile(InputProfile.defaults(InputProfile.DESKTOP)) + val e = InputSurfaceElement(InputControl.RIGHT_PAD, .1f, .1f, .4f, .4f) + val surface = source(e) + surface.click(true); surface.click(false) + assertEquals(listOf("button:272:true", "button:272:false"), events) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/input/JoyConPadTest.kt b/app/src/test/java/com/droiddeck/launcher/input/JoyConPadTest.kt index a325632132..0b8c930ed0 100644 --- a/app/src/test/java/com/droiddeck/launcher/input/JoyConPadTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/input/JoyConPadTest.kt @@ -240,16 +240,26 @@ class JoyConPadTest { @Implementation fun open(): Boolean = true @Implementation fun close() = Unit @Implementation fun destroy() = Unit - @Implementation fun writePad(state: PadState) { pads[slot] = PadState().apply { copyFrom(state) } } + @Implementation fun writePad(state: PadState) { + pads[slot] = PadState().apply { copyFrom(state) } + padWrites++ + } companion object { val pads = HashMap() + var padWrites = 0 + var deckWrites = 0 var grips = 0 + var deckPads = ShortArray(4) @JvmStatic @Implementation fun __staticInitializer__() = Unit @JvmStatic @Implementation - fun writeDeckControls(slot: Int, controls: Int, pads: ShortArray, pressure: ShortArray) { grips = controls } + fun writeDeckControls(slot: Int, controls: Int, pads: ShortArray, pressure: ShortArray) { + grips = controls + deckPads = pads.copyOf() + deckWrites++ + } } } } diff --git a/app/src/test/java/com/droiddeck/launcher/input/LegacyScreenKeyMigrationTest.kt b/app/src/test/java/com/droiddeck/launcher/input/LegacyScreenKeyMigrationTest.kt new file mode 100644 index 0000000000..6d72190ce4 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/input/LegacyScreenKeyMigrationTest.kt @@ -0,0 +1,95 @@ +package com.droiddeck.launcher.input + +import android.content.Context +import org.json.JSONObject +import org.junit.Assert.* +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.annotation.Config + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [34], manifest = Config.NONE) +class LegacyScreenKeyMigrationTest { + private val context: Context get() = RuntimeEnvironment.getApplication() + private val prefs get() = context.getSharedPreferences("input-profiles", Context.MODE_PRIVATE) + @Before fun reset() { + for (name in listOf("input-profiles", "input-surfaces", "controller")) context.getSharedPreferences(name, Context.MODE_PRIVATE).edit().clear().commit() + } + + @Test fun upgradeImportsEverySavedSetWithoutChangingExistingProfilesAndIsIdempotent() { + val original = InputLibraries.read(context).selectProfile(false, "desktop") + prefs.edit().putString("library", JSONObject(InputLibraries.encode(original)).put("version", 5).toString()).commit() + ControllerPrefs.prefs(context).edit().putString("screenKeys", "29+33:0.3,0.7,44::Find::0.6:1:1:1") + .putBoolean("screenKeysShown", false).putString("screenLayout.Editor", "28:0.8,0.6,50::::1.0:2:0:1").commit() + val migrated = InputLibraries.read(context) + assertEquals(original.profiles, migrated.profiles.take(original.profiles.size)) + assertEquals(original.selections, migrated.selections) + assertEquals(2, migrated.profiles.count { it.id.startsWith("migrated.screenkeys.") }) + val imported = migrated.profiles.first { it.name == "Screen keys (imported)" } + val layout = migrated.layout(imported.id, InputRole.PRIMARY)!! + assertEquals(InputLayoutScreen.MAIN, layout.screen) + assertTrue(layout.elements.single().toggle) + assertEquals(InputAction.Keys(listOf(29, 33)), migrated.compile(imported.id).elements[InputRole.PRIMARY]!!.values.single()) + assertFalse(ControllerPrefs.prefs(context).contains("screenKeys")) + assertTrue(prefs.contains(LegacyScreenKeyMigration.BACKUP)) + assertEquals(migrated, InputLibraries.read(context)) + assertEquals(migrated, InputLibraries.decode(InputLibraries.encode(migrated))) + } + + @Test fun conversionRetainsCoordinatesImagesOpacitySymbolsAndIndependentDiagonalKeys() { + val values = LegacyScreenKeyMigration.convert(context, + "28:0.25,0.6,50::::0.8:2:0:1;29:0.7,0.8,40::Ctrl:custom.png:0.4:3:1:1;103+30+106+0+108+0+105+0:0.5,0.5,80::::1:6:0:1", 1000, 500, 2f) + val enter = values[0].first + assertEquals(.25f, enter.x + enter.width / 2, .0001f) + assertEquals(.6f, enter.y + enter.height / 2, .0001f) + assertEquals(.1f, enter.width, .0001f) + assertEquals(.2f, enter.height, .0001f) + assertEquals(28, enter.icon) + assertEquals(InputButtonShape.CIRCLE, enter.buttonShape) + assertEquals("custom.png", values[1].first.image) + assertEquals(.4f, values[1].first.opacity) + assertTrue(values[1].first.toggle) + assertEquals(InputAction.Directions(103, 106, 108, 105, listOf(30, 0, 0, 0)), values[2].second) + assertFalse(values[2].first.floating) + } + + @Test fun invalidMigrationDoesNotEraseTheOriginalSavedLibraryOrKeys() { + val original = JSONObject(InputLibraries.encode(InputLibraries.read(context))).put("version", 5).toString() + prefs.edit().putString("library", original).commit() + ControllerPrefs.prefs(context).edit().putString("screenKeys", "28:NaN,0.5,44").commit() + InputLibraries.read(context) + assertEquals(original, prefs.getString("library", null)) + assertEquals("28:NaN,0.5,44", ControllerPrefs.prefs(context).getString("screenKeys", null)) + assertFalse(prefs.contains(LegacyScreenKeyMigration.BACKUP)) + } + + @Test fun failedScreenKeyMigrationKeepsUpgradedBindingIdsStableForEditorLookup() { + val original = InputLibraries.read(context).chooseLayout("game", InputRole.SECONDARY, InputLibraries.DECK) + val fixed = original.copy(layouts = original.layouts.map { + when (it.id) { + InputLibraries.OVERLAY -> it.copy(elements = emptyList(), template = InputLayoutTemplate.OVERLAY) + InputLibraries.DECK -> it.copy(elements = emptyList(), template = InputLayoutTemplate.DECK) + else -> it + } + }, bindings = original.bindings.map { + if (it.layoutId in setOf(InputLibraries.OVERLAY, InputLibraries.DECK)) it.copy(elements = emptyMap(), clicks = emptyMap()) else it + }) + val raw = JSONObject(InputLibraries.encode(fixed)).put("version", 2).toString() + prefs.edit().putString("library", raw).commit() + ControllerPrefs.prefs(context).edit().putString("screenKeys", "28:NaN,0.5,44").commit() + val first = InputLibraries.read(context) + val second = InputLibraries.read(context) + assertEquals(first, second) + for (role in listOf(InputRole.PRIMARY, InputRole.SECONDARY)) { + val id = first.profile("game").binding(role) + assertEquals(first.preset(id), second.preset(id)) + assertTrue(second.layout("game", role)!!.editable) + } + assertEquals(raw, prefs.getString("library", null)) + assertTrue(InputLibraries.hasPendingScreenKeyMigration(context)) + assertEquals("28:NaN,0.5,44", ControllerPrefs.prefs(context).getString("screenKeys", null)) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/input/MouseBackInputTest.kt b/app/src/test/java/com/droiddeck/launcher/input/MouseBackInputTest.kt index 625f93e7cf..ba7e666c6c 100644 --- a/app/src/test/java/com/droiddeck/launcher/input/MouseBackInputTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/input/MouseBackInputTest.kt @@ -8,6 +8,7 @@ import org.junit.Test import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner import org.robolectric.annotation.Config +import org.robolectric.shadows.InputDeviceBuilder @RunWith(RobolectricTestRunner::class) @Config(sdk = [34], manifest = Config.NONE) @@ -151,4 +152,47 @@ class MouseBackInputTest { assertFalse(input.onKeyEvent(key(KeyEvent.ACTION_UP), toGuest = true)) assertTrue(presses.isEmpty()) } + + private fun device(name: String, sources: Int, external: Boolean = true, virtual: Boolean = false) = + InputDeviceBuilder.newBuilder().setId(if (virtual) -1 else 11).setName(name).setDescriptor(name) + .setExternal(external).setSources(sources).build() + + @Test fun anExternalMouseCounts() { + assertTrue(MouseBackInput.isExternalMouse(device("USB mouse", InputDevice.SOURCE_MOUSE))) + assertTrue(MouseBackInput.isExternalMouse(device("Bluetooth mouse", InputDevice.SOURCE_MOUSE_RELATIVE))) + } + + @Test fun theLoggedControllerDeviceDoesNotCountAsAnExternalMouse() { + val odinController = device("Odin Controller", 0x1000511) + assertTrue(PadBridge.isFromController(odinController)) + assertFalse(MouseBackInput.isExternalMouse(odinController)) + } + + @Test fun virtualTouchpadTouchscreenUinputAndNonMouseDevicesDoNotCount() { + assertFalse(MouseBackInput.isExternalMouse(device("virtual mouse", InputDevice.SOURCE_MOUSE, virtual = true))) + assertFalse(MouseBackInput.isExternalMouse(device("touchpad", InputDevice.SOURCE_MOUSE or InputDevice.SOURCE_TOUCHPAD))) + assertFalse(MouseBackInput.isExternalMouse(device("touchscreen", InputDevice.SOURCE_MOUSE or InputDevice.SOURCE_TOUCHSCREEN))) + assertFalse(MouseBackInput.isExternalMouse(device("uinput-mouse", InputDevice.SOURCE_MOUSE))) + assertFalse(MouseBackInput.isExternalMouse(device("gamepad", InputDevice.SOURCE_GAMEPAD))) + assertFalse(MouseBackInput.isExternalMouse(null)) + } + + @Test fun aMouseThatNamesItselfVirtualDoesNotCount() { + assertFalse(MouseBackInput.isExternalMouse(device("ODIN Station Virtual Mouse", 0x20004))) + assertFalse(MouseBackInput.isExternalMouse(device("VIRTUAL pointer", InputDevice.SOURCE_MOUSE))) + assertTrue(MouseBackInput.isExternalMouse(device("Virtuality Pro Mouse", InputDevice.SOURCE_MOUSE))) + assertTrue(MouseBackInput.isExternalMouse(device("Logitech MX Master 3", InputDevice.SOURCE_MOUSE))) + } + + @Test fun injectedBackIsLeftAloneWithoutAnExternalMouse() { + val odinController = device("Odin Controller", 0x1000511) + val controllerOnly = MouseBackInput(presses::add) { MouseBackInput.isExternalMouse(odinController) } + assertFalse(controllerOnly.onKeyEvent(injected(KeyEvent.ACTION_DOWN), toGuest = true)) + assertTrue(presses.isEmpty()) + val external = MouseBackInput(presses::add) { + MouseBackInput.isExternalMouse(device("USB mouse", InputDevice.SOURCE_MOUSE)) + } + assertTrue(external.onKeyEvent(injected(KeyEvent.ACTION_DOWN), toGuest = true)) + assertEquals(listOf(true), presses) + } } diff --git a/app/src/test/java/com/droiddeck/launcher/input/OnScreenControlsTest.kt b/app/src/test/java/com/droiddeck/launcher/input/OnScreenControlsTest.kt index b071b04906..2c9745693e 100644 --- a/app/src/test/java/com/droiddeck/launcher/input/OnScreenControlsTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/input/OnScreenControlsTest.kt @@ -1,16 +1,11 @@ package com.droiddeck.launcher.input -import kotlin.math.abs -import kotlin.math.sqrt import android.content.Context -import android.graphics.Bitmap -import android.graphics.Canvas -import android.graphics.Rect -import android.view.DisplayCutout import android.view.MotionEvent import android.view.View -import android.view.WindowInsets import android.widget.FrameLayout +import java.io.File +import org.junit.After import org.junit.Assert.* import org.junit.Before import org.junit.Test @@ -18,506 +13,226 @@ import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner import org.robolectric.RuntimeEnvironment import org.robolectric.annotation.Config -import org.robolectric.annotation.GraphicsMode @RunWith(RobolectricTestRunner::class) -@Config(sdk = [28], manifest = Config.NONE, qualifiers = "mdpi") -@GraphicsMode(GraphicsMode.Mode.NATIVE) +@Config(sdk = [34], manifest = Config.NONE, shadows = [JoyConPadTest.RecordingWriter::class]) class OnScreenControlsTest { - private lateinit var context: Context + private val context: Context get() = RuntimeEnvironment.getApplication() + private val bridge = PadBridge(File(context.cacheDir, "surface-overlay")) private lateinit var view: OnScreenControls + private val left = InputSurfaceElement(InputControl.LS, .1f, .3f, .2f, .4f) + private val button = InputSurfaceElement(InputControl.A, .7f, .3f, .2f, .3f) private var time = 1000L - - @Before fun setUp() { - context = RuntimeEnvironment.getApplication() - context.getSharedPreferences("controller", Context.MODE_PRIVATE).edit().clear().commit() - ControllerPrefs.setLayout(context, 1200, 800, mapOf("ls" to (0.45f to 0.5f), "rs" to (0.55f to 0.5f))) - view = OnScreenControls(context, null) - view.layout(0, 0, 1200, 800) + @Before fun setup() { + view = OnScreenControls(context, bridge) + view.updateLayout(SavedInputLayout("test", "Test", listOf(left, button), aspect = ASPECT)) + measure(view) + } + @After fun close() { view.releaseAll(); bridge.stop() } + private fun state() = JoyConPadTest.RecordingWriter.pads[0] ?: PadState() + private fun measure(target: View) { + target.measure(View.MeasureSpec.makeMeasureSpec(1000, View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(600, View.MeasureSpec.EXACTLY)) + target.layout(0, 0, 1000, 600) } - @Test fun editorSnapsToOtherGroupCentreLines() { - editor() - val button = control("lb") - val target = control("rb") - val x = value(button, "cx") - val y = value(button, "cy") - touch(MotionEvent.ACTION_DOWN, 1 to (x to y)) - touch(MotionEvent.ACTION_MOVE, 1 to (x to y + 5f)) - assertEquals(value(target, "cy"), value(button, "cy"), 0f) - assertEquals(value(target, "cy"), nullableFloat(view, "snapY")!!, 0f) - touch(MotionEvent.ACTION_UP, 1 to (x to y + 5f)) - assertNull(nullableFloat(view, "snapY")) + @Test fun independentFingersHoldStickAndButtonAndCancelReleasesBoth() { + touch(MotionEvent.ACTION_DOWN, 3 to (200f to 300f)) + touch(MotionEvent.ACTION_MOVE, 3 to (245f to 300f)) + touch(MotionEvent.ACTION_POINTER_DOWN or (1 shl 8), 3 to (245f to 300f), 8 to (800f to 250f)) + assertTrue(state().leftX > .5f) + assertTrue(state().isDown(PadState.A)) + touch(MotionEvent.ACTION_POINTER_UP or (1 shl 8), 3 to (245f to 300f), 8 to (800f to 250f)) + assertFalse(state().isDown(PadState.A)) + assertTrue(state().leftX > .5f) + touch(MotionEvent.ACTION_CANCEL, 3 to (245f to 300f)) + assertEquals(0f, state().leftX, 0f) } - @Test fun editorSnapsToClusterCentresNotTheirOuterButtons() { - editor() - val button = control("lb") - val x = value(button, "cx") - val row = value(control("y"), "cy") + 3f - touch(MotionEvent.ACTION_DOWN, 1 to (x to value(button, "cy"))) - touch(MotionEvent.ACTION_MOVE, 1 to (x to row)) - assertEquals(row, value(button, "cy"), 0f) - assertNull(nullableFloat(view, "snapY")) - touch(MotionEvent.ACTION_MOVE, 1 to (x to value(control("x"), "cy") + 3f)) - assertEquals(value(control("x"), "cy"), value(button, "cy"), 0f) - touch(MotionEvent.ACTION_UP, 1 to (x to row)) - } - - @Test fun editorDoesNotSnapBeyondDistanceAndCanDragFreeFromGuide() { - editor() - val button = control("lb") - val x = value(button, "cx") - val y = value(button, "cy") - val otherYs = (field(view, "controls") as List<*>) - .filter { field(it!!, "group") != "lb" } - .map { value(it!!, "cy") } - val freeY = (100..700 step 31).map(Int::toFloat).first { - otherYs.minOf { targetY -> abs(it - targetY) } > 12f - } - touch(MotionEvent.ACTION_DOWN, 1 to (x to y)) - touch(MotionEvent.ACTION_MOVE, 1 to (x to freeY)) - assertEquals(freeY, value(button, "cy"), 0f) - assertNull(nullableFloat(view, "snapY")) - val target = value(control("rb"), "cy") - touch(MotionEvent.ACTION_MOVE, 1 to (x to target + 5f)) - assertEquals(target, value(button, "cy"), 0f) - touch(MotionEvent.ACTION_MOVE, 1 to (x to freeY)) - assertEquals(freeY, value(button, "cy"), 0f) - assertNull(nullableFloat(view, "snapY")) - touch(MotionEvent.ACTION_UP, 1 to (x to freeY)) - } - - @Test fun editorSnapsToScreenCentreAndCanDisableSnapping() { - editor() - val button = control("lb") - val y = value(button, "cy") - touch(MotionEvent.ACTION_DOWN, 1 to (value(button, "cx") to y)) - touch(MotionEvent.ACTION_MOVE, 1 to (605f to y)) - assertEquals(600f, value(button, "cx"), 0f) - assertEquals(600f, nullableFloat(view, "snapX")!!, 0f) - touch(MotionEvent.ACTION_UP, 1 to (605f to y)) - - ControllerPrefs.setSnap(context, false) - editor() - val unsnapped = control("lb") - val x = value(unsnapped, "cx") - val startY = value(unsnapped, "cy") - touch(MotionEvent.ACTION_DOWN, 1 to (x to startY)) - touch(MotionEvent.ACTION_MOVE, 1 to (605f to startY)) - assertEquals(605f, value(unsnapped, "cx"), 0f) - assertNull(nullableFloat(view, "snapX")) - touch(MotionEvent.ACTION_UP, 1 to (605f to startY)) - } - - @Test fun editorSnapPreferenceDefaultsOnAndCanBeChanged() { - assertTrue(ControllerPrefs.read(context).snap) - ControllerPrefs.setSnap(context, false) - assertFalse(ControllerPrefs.read(context).snap) - ControllerPrefs.resetAll(context) - assertTrue(ControllerPrefs.read(context).snap) - } - - @Test fun adaptiveStickHidesUntilTouchedAndReturnsToNeutralOnRelease() { - val left = control("ls") - assertEquals(0, pixel(value(left, "cx"), value(left, "cy"))) - assertTrue(touch(MotionEvent.ACTION_DOWN, 7 to (540f to 400f))) - assertEquals(7, field(left, "pressedBy")) - assertEquals(540f, value(left, "ax"), 0f) - assertEquals(400f, value(left, "ay"), 0f) - assertNotEquals(0, pixel(540f, 400f)) - touch(MotionEvent.ACTION_MOVE, 7 to (580f to 420f)) - assertTrue(value(left, "kx") > 0f) - assertTrue(value(left, "ky") > 0f) - touch(MotionEvent.ACTION_UP, 7 to (580f to 420f)) - assertEquals(-1, field(left, "pressedBy")) - assertEquals(0f, value(left, "kx"), 0f) - assertEquals(0f, value(left, "ky"), 0f) - assertEquals(0, pixel(540f, 400f)) - } - - @Test fun simultaneousSticksKeepTheirPointersAndReleaseIndependently() { - val left = control("ls") - val right = control("rs") - touch(MotionEvent.ACTION_DOWN, 7 to (540f to 400f)) - touch(MotionEvent.ACTION_POINTER_DOWN or (1 shl 8), 7 to (540f to 400f), 9 to (660f to 400f)) - assertEquals(7, field(left, "pressedBy")) - assertEquals(9, field(right, "pressedBy")) - touch(MotionEvent.ACTION_POINTER_DOWN or (2 shl 8), 7 to (540f to 400f), 9 to (660f to 400f), 11 to (500f to 400f)) - assertEquals(7, field(left, "pressedBy")) - touch(MotionEvent.ACTION_POINTER_UP or (2 shl 8), 7 to (540f to 400f), 9 to (660f to 400f), 11 to (500f to 400f)) - touch(MotionEvent.ACTION_MOVE, 7 to (700f to 400f), 9 to (500f to 400f)) - assertTrue(value(left, "kx") > 0f) - assertTrue(value(right, "kx") < 0f) - touch(MotionEvent.ACTION_POINTER_UP, 7 to (700f to 400f), 9 to (500f to 400f)) - assertEquals(-1, field(left, "pressedBy")) - assertEquals(9, field(right, "pressedBy")) - touch(MotionEvent.ACTION_CANCEL, 9 to (500f to 400f)) - for (stick in listOf(left, right)) { - assertEquals(-1, field(stick, "pressedBy")) - assertEquals(0f, value(stick, "kx"), 0f) - } - } - - @Test fun cancelledTouchDoesNotBecomeAStickClick() { - touch(MotionEvent.ACTION_DOWN, 1 to (540f to 400f)) - touch(MotionEvent.ACTION_CANCEL, 1 to (540f to 400f)) - touch(MotionEvent.ACTION_DOWN, 1 to (540f to 400f)) - assertEquals(false, field(control("ls"), "clicked")) - touch(MotionEvent.ACTION_UP, 1 to (540f to 400f)) - touch(MotionEvent.ACTION_DOWN, 1 to (540f to 400f)) - assertEquals(true, field(control("ls"), "clicked")) - } - - @Test fun aCancelledSecondFingerDoesNotArmAStickClick() { - touch(MotionEvent.ACTION_DOWN, 7 to (660f to 400f)) - touch(MotionEvent.ACTION_POINTER_DOWN or (1 shl 8), 7 to (660f to 400f), 9 to (540f to 400f)) - touch(MotionEvent.ACTION_POINTER_UP or (1 shl 8), 7 to (660f to 400f), 9 to (540f to 400f), flags = MotionEvent.FLAG_CANCELED) - touch(MotionEvent.ACTION_POINTER_DOWN or (1 shl 8), 7 to (660f to 400f), 9 to (540f to 400f)) - assertEquals(false, field(control("ls"), "clicked")) - touch(MotionEvent.ACTION_POINTER_UP or (1 shl 8), 7 to (660f to 400f), 9 to (540f to 400f)) - touch(MotionEvent.ACTION_POINTER_DOWN or (1 shl 8), 7 to (660f to 400f), 9 to (540f to 400f)) - assertEquals(true, field(control("ls"), "clicked")) - } - - @Test fun movingTheStickAndTouchingItAgainDoesNotClickIt() { - touch(MotionEvent.ACTION_DOWN, 1 to (540f to 400f)) - touch(MotionEvent.ACTION_MOVE, 1 to (600f to 400f)) - touch(MotionEvent.ACTION_UP, 1 to (600f to 400f)) - touch(MotionEvent.ACTION_DOWN, 1 to (540f to 400f)) - assertEquals(false, field(control("ls"), "clicked")) - } - - @Test fun liftingFarFromTheTouchPointDoesNotArmAStickClick() { - touch(MotionEvent.ACTION_DOWN, 1 to (540f to 400f)) - touch(MotionEvent.ACTION_UP, 1 to (600f to 400f)) - touch(MotionEvent.ACTION_DOWN, 1 to (540f to 400f)) - assertEquals(false, field(control("ls"), "clicked")) - } - - @Test fun aLongHoldThenAQuickTouchDoesNotClickTheStick() { - touch(MotionEvent.ACTION_DOWN, 1 to (540f to 400f)) - time += 600 - touch(MotionEvent.ACTION_UP, 1 to (540f to 400f)) - touch(MotionEvent.ACTION_DOWN, 1 to (540f to 400f)) - assertEquals(false, field(control("ls"), "clicked")) - } - - @Test fun aFastDoubleTapClicksTheStickAndCanBeHeldWhileMoving() { - touch(MotionEvent.ACTION_DOWN, 1 to (540f to 400f)) - touch(MotionEvent.ACTION_UP, 1 to (540f to 400f)) - touch(MotionEvent.ACTION_DOWN, 1 to (540f to 400f)) - touch(MotionEvent.ACTION_MOVE, 1 to (600f to 400f)) - assertEquals(true, field(control("ls"), "clicked")) - touch(MotionEvent.ACTION_UP, 1 to (600f to 400f)) - touch(MotionEvent.ACTION_DOWN, 1 to (540f to 400f)) - assertEquals(false, field(control("ls"), "clicked")) - } - - @Test fun aSlowSecondTapDoesNotClickTheStick() { - touch(MotionEvent.ACTION_DOWN, 1 to (540f to 400f)) - touch(MotionEvent.ACTION_UP, 1 to (540f to 400f)) - time += 400 - touch(MotionEvent.ACTION_DOWN, 1 to (540f to 400f)) - assertEquals(false, field(control("ls"), "clicked")) - } - - @Test fun buttonsTakePriorityOverAdaptiveRegions() { - val button = control("a") - touch(MotionEvent.ACTION_DOWN, 3 to (value(button, "cx") to value(button, "cy"))) - assertEquals(3, field(button, "pressedBy")) - assertEquals(-1, field(control("rs"), "pressedBy")) - } - - @Test fun takesTouchAtMatchesWhatATouchDoes() { - var checked = 0 - for (x in 0 until 1200 step 40) for (y in 0 until 800 step 40) { - val expected = view.takesTouchAt(x.toFloat(), y.toFloat()) - assertEquals("($x, $y)", expected, touch(MotionEvent.ACTION_DOWN, 0 to (x.toFloat() to y.toFloat()))) - touch(MotionEvent.ACTION_UP, 0 to (x.toFloat() to y.toFloat())) - if (expected) checked++ - } - assertTrue("some points land on controls", checked > 0) - } - - @Test fun fixedSticksAndEditorRemainVisibleAtSavedPositions() { - ControllerPrefs.setAdaptiveSticks(context, false) + @Test fun reloadCancelsHeldViewsSoTheNextPressWorks() { + touch(MotionEvent.ACTION_DOWN, 0 to (800f to 250f)) + assertTrue(state().isDown(PadState.A)) view.reload() - val left = control("ls") - val x = value(left, "cx") - val y = value(left, "cy") - assertNotEquals(0, pixel(x, y)) - assertFalse(touch(MotionEvent.ACTION_DOWN, 1 to (0f to y))) - assertFalse(touch(MotionEvent.ACTION_DOWN, 1 to (600f to 200f))) - touch(MotionEvent.ACTION_DOWN, 1 to (x + 10f to y)) - assertEquals(x, value(left, "ax"), 0f) - assertEquals(10f, value(left, "kx"), 0f) - ControllerPrefs.setAdaptiveSticks(context, true) - view = OnScreenControls(context, null, editing = true) - view.layout(0, 0, 1200, 800) - assertNotEquals(0, pixel(value(control("ls"), "cx"), value(control("ls"), "cy"))) + assertFalse(state().isDown(PadState.A)) + touch(MotionEvent.ACTION_DOWN, 0 to (800f to 250f)) + assertTrue(state().isDown(PadState.A)) } - @Test fun buttonsOnlyAndReloadReleaseAdaptiveSticks() { - touch(MotionEvent.ACTION_DOWN, 1 to (540f to 400f)) - touch(MotionEvent.ACTION_MOVE, 1 to (570f to 400f)) - view.reload() - assertEquals(-1, field(control("ls"), "pressedBy")) - assertEquals(0f, value(control("ls"), "kx"), 0f) - view.setButtonsOnly(true) - assertFalse(touch(MotionEvent.ACTION_DOWN, 1 to (600f to 200f))) + @Test fun replacingTheLayoutReleasesOldSourcesAndHitRegions() { + touch(MotionEvent.ACTION_DOWN, 0 to (800f to 250f)) + view.updateLayout(SavedInputLayout("empty", "Empty")) + measure(view) + assertFalse(state().isDown(PadState.A)) + assertFalse(view.takesTouchAt(800f, 250f)) } - @Test fun adaptiveActivationKeepsItsVerticalAndInwardLimits() { - val left = control("ls") - val x = value(left, "cx") - val y = value(left, "cy") - val reach = activationReach(left) - assertFalse(touch(MotionEvent.ACTION_DOWN, 1 to (x to y - reach - 1f))) - assertTrue(touch(MotionEvent.ACTION_DOWN, 1 to (x to y - reach + 1f))) - assertEquals(y - reach + 1f, value(left, "ay"), 0.001f) - touch(MotionEvent.ACTION_UP, 1 to (x to y - reach + 1f)) - assertFalse(touch(MotionEvent.ACTION_DOWN, 1 to (x + reach to y - reach))) - assertFalse(touch(MotionEvent.ACTION_DOWN, 1 to (600f to 200f))) - } - - @Test fun leftAdaptiveStickCanStartAtTheLeftEdgeAndReleaseNormally() { - val left = control("ls") - val y = value(left, "cy") - val reach = activationReach(left) - assertFalse(touch(MotionEvent.ACTION_DOWN, 1 to (0f to y - reach - 1f))) - assertTrue(touch(MotionEvent.ACTION_DOWN, 1 to (0f to y))) - assertEquals(1, field(left, "pressedBy")) - assertEquals(0f, value(left, "ax"), 0f) - assertEquals(0f, value(left, "kx"), 0f) - touch(MotionEvent.ACTION_MOVE, 1 to (30f to y)) - assertEquals(30f, value(left, "kx"), 0f) - touch(MotionEvent.ACTION_CANCEL, 1 to (30f to y)) - assertEquals(-1, field(left, "pressedBy")) - assertEquals(0f, value(left, "kx"), 0f) - assertEquals(-1, field(control("rs"), "pressedBy")) - } - - @Test fun rightAdaptiveStickKeepsItsSavedActivationCircle() { - val right = control("rs") - val x = value(right, "cx") - val y = value(right, "cy") - val reach = activationReach(right) - assertFalse(touch(MotionEvent.ACTION_DOWN, 1 to (1199f to y))) - assertFalse(touch(MotionEvent.ACTION_DOWN, 1 to (x + reach + 1f to y))) - assertTrue(touch(MotionEvent.ACTION_DOWN, 1 to (x + reach - 1f to y))) - assertEquals(1, field(right, "pressedBy")) - } - - @Test - @Config(sdk = [30]) - @Suppress("DEPRECATION") - fun leftActivationStopsAtTheSafeCutoutEdge() { - val insets = WindowInsets.Builder() - .setDisplayCutout(DisplayCutout(Rect(24, 0, 0, 0), listOf(Rect(0, 380, 24, 420)))) - .build() - view.dispatchApplyWindowInsets(insets) - val left = control("ls") - val y = value(left, "cy") - assertFalse(touch(MotionEvent.ACTION_DOWN, 1 to (23f to y))) - assertTrue(touch(MotionEvent.ACTION_DOWN, 1 to (24f to y))) - assertEquals(1, field(left, "pressedBy")) - } - - @Test fun touchesOutsideActivationAreasStayWithTrackpad() { + @Test fun emptySpaceDoesNotConsumeTheUnderlyingTrackpadStream() { val received = mutableListOf() - val trackpad = View(context).apply { - setOnTouchListener { _, event -> received.add(event.actionMasked); true } - } val root = FrameLayout(context).apply { - addView(trackpad, FrameLayout.LayoutParams(1200, 800)) - addView(view, FrameLayout.LayoutParams(1200, 800)) - measure(View.MeasureSpec.makeMeasureSpec(1200, View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(800, View.MeasureSpec.EXACTLY)) - layout(0, 0, 1200, 800) + addView(View(context).apply { setOnTouchListener { _, e -> received += e.actionMasked; true } }, FrameLayout.LayoutParams(-1, -1)) + addView(view, FrameLayout.LayoutParams(-1, -1)) } - for ((action, position) in listOf( - MotionEvent.ACTION_DOWN to (600f to 200f), - MotionEvent.ACTION_MOVE to (540f to 400f), - MotionEvent.ACTION_UP to (540f to 400f), - )) { - val event = MotionEvent.obtain(1000L, time++, action, position.first, position.second, 0) + measure(root) + for (action in listOf(MotionEvent.ACTION_DOWN, MotionEvent.ACTION_MOVE, MotionEvent.ACTION_UP)) { + val event = MotionEvent.obtain(1000, time++, action, 500f, 100f, 0) try { assertTrue(root.dispatchTouchEvent(event)) } finally { event.recycle() } } assertEquals(listOf(MotionEvent.ACTION_DOWN, MotionEvent.ACTION_MOVE, MotionEvent.ACTION_UP), received) - assertEquals(-1, field(control("ls"), "pressedBy")) } - @Test fun trackpadFingerKeepsItsOwnStreamWhileAStickIsHeld() { - val forwarded = mutableListOf>() - view.onTouchMiss = { forwarded += Triple(it.actionMasked, it.getPointerId(0), it.getX(0)) } - touch(MotionEvent.ACTION_DOWN, 7 to (540f to 400f)) - assertTrue(touch(MotionEvent.ACTION_POINTER_DOWN or (1 shl 8), 7 to (540f to 400f), 9 to (600f to 200f))) - touch(MotionEvent.ACTION_MOVE, 7 to (500f to 400f), 9 to (650f to 200f)) - touch(MotionEvent.ACTION_POINTER_UP or (1 shl 8), 7 to (500f to 400f), 9 to (650f to 200f)) - assertEquals(listOf( - Triple(MotionEvent.ACTION_DOWN, 9, 600f), - Triple(MotionEvent.ACTION_MOVE, 9, 650f), - Triple(MotionEvent.ACTION_UP, 9, 650f), - ), forwarded) - assertEquals(7, field(control("ls"), "pressedBy")) - assertTrue(value(control("ls"), "kx") < 0f) + @Test fun backgroundFingerIsForwardedWhileAStickIsHeld() { + val received = mutableListOf>() + view.onTouchMiss = { received += it.actionMasked to it.getPointerId(0) } + touch(MotionEvent.ACTION_DOWN, 3 to (200f to 300f)) + touch(MotionEvent.ACTION_POINTER_DOWN or (1 shl 8), 3 to (200f to 300f), 8 to (500f to 100f)) + touch(MotionEvent.ACTION_MOVE, 3 to (245f to 300f), 8 to (520f to 100f)) + touch(MotionEvent.ACTION_POINTER_UP or (1 shl 8), 3 to (245f to 300f), 8 to (520f to 100f)) + assertEquals(listOf(MotionEvent.ACTION_DOWN to 8, MotionEvent.ACTION_MOVE to 8, MotionEvent.ACTION_UP to 8), received) + assertTrue(state().leftX > .5f) } - @Test fun stickPressedAfterTrackpadFingerReachesTheControls() { - val received = mutableListOf>() - val trackpad = View(context).apply { - setOnTouchListener { _, event -> received += Triple(event.actionMasked, event.pointerCount, event.getPointerId(0)); true } - } - val root = FrameLayout(context).apply { - isMotionEventSplittingEnabled = true - addView(trackpad, FrameLayout.LayoutParams(1200, 800)) - addView(view, FrameLayout.LayoutParams(1200, 800)) - measure(View.MeasureSpec.makeMeasureSpec(1200, View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(800, View.MeasureSpec.EXACTLY)) - layout(0, 0, 1200, 800) - } - fun dispatch(action: Int, vararg pointers: Pair>) { - val properties = pointers.map { (id, _) -> MotionEvent.PointerProperties().apply { this.id = id; toolType = MotionEvent.TOOL_TYPE_FINGER } }.toTypedArray() - val coords = pointers.map { (_, position) -> MotionEvent.PointerCoords().apply { x = position.first; y = position.second; pressure = 1f; size = 1f } }.toTypedArray() - val event = MotionEvent.obtain(1000L, time++, action, pointers.size, properties, coords, 0, 0, 1f, 1f, 0, 0, 0, 0) - try { assertTrue(root.dispatchTouchEvent(event)) } finally { event.recycle() } - } - dispatch(MotionEvent.ACTION_DOWN, 0 to (600f to 200f)) - dispatch(MotionEvent.ACTION_POINTER_DOWN or (1 shl 8), 0 to (600f to 200f), 1 to (540f to 400f)) - dispatch(MotionEvent.ACTION_MOVE, 0 to (640f to 220f), 1 to (500f to 400f)) - assertEquals(1, field(control("ls"), "pressedBy")) - assertTrue(value(control("ls"), "kx") < 0f) - dispatch(MotionEvent.ACTION_POINTER_UP or (1 shl 8), 0 to (640f to 220f), 1 to (500f to 400f)) - dispatch(MotionEvent.ACTION_UP, 0 to (640f to 220f)) - assertEquals(-1, field(control("ls"), "pressedBy")) - assertEquals(Triple(MotionEvent.ACTION_DOWN, 1, 0), received.first()) - assertEquals(Triple(MotionEvent.ACTION_UP, 1, 0), received.last()) - assertTrue(received.all { it.second == 1 && it.third == 0 }) + @Test fun aBackgroundStreamDoesNotTurnIntoAButtonWhenItCrossesOne() { + val received = mutableListOf() + view.onTouchMiss = { received += it.actionMasked } + touch(MotionEvent.ACTION_DOWN, 3 to (500f to 100f)) + touch(MotionEvent.ACTION_MOVE, 3 to (800f to 250f)) + touch(MotionEvent.ACTION_UP, 3 to (800f to 250f)) + assertFalse(state().isDown(PadState.A)) + assertEquals(listOf(MotionEvent.ACTION_DOWN, MotionEvent.ACTION_MOVE, MotionEvent.ACTION_UP), received) } - @Test fun adaptiveActivationLeavesFivePixelsAroundButtonHitTargets() { - for (id in listOf("a", "rb")) { - val button = control(id) - val radius = value(button, "radius") - val edge = value(button, "cy") - radius * if (id == "rb") 1.05f else 1.25f - val x = value(button, "cx") - val left = control("ls") - moveControl(left, x, edge - 10f) - assertFalse(touch(MotionEvent.ACTION_DOWN, 1 to (x to edge - 4f))) - assertEquals(-1, field(left, "pressedBy")) - assertTrue(touch(MotionEvent.ACTION_DOWN, 1 to (x to edge - 6f))) - assertEquals(1, field(left, "pressedBy")) - touch(MotionEvent.ACTION_UP, 1 to (x to edge - 6f)) + @Test fun floatingAndFixedSticksUseTheirConfiguredOrigin() { + for (floating in listOf(true, false)) { + view.updateLayout(SavedInputLayout("test", "Test", listOf(left.copy(floating = floating)), aspect = ASPECT)) + measure(view) + touch(MotionEvent.ACTION_DOWN, 3 to (230f to 300f)) + assertEquals(if (floating) 0f else .5f, state().leftX, .001f) + touch(MotionEvent.ACTION_UP, 3 to (230f to 300f)) } } - @Test fun leftExtensionKeepsButtonPriorityAndClearance() { - val button = control("left") - val x = value(button, "cx") - val y = value(button, "cy") - val edge = y - value(button, "radius") * 1.25f - val left = control("ls") - moveControl(left, x + 200f, y) - assertTrue(touch(MotionEvent.ACTION_DOWN, 1 to (x to y))) - assertEquals(1, field(button, "pressedBy")) - assertEquals(-1, field(left, "pressedBy")) - touch(MotionEvent.ACTION_UP, 1 to (x to y)) - assertFalse(touch(MotionEvent.ACTION_DOWN, 1 to (x to edge - 4f))) - assertTrue(touch(MotionEvent.ACTION_DOWN, 1 to (x to edge - 6f))) - assertEquals(1, field(left, "pressedBy")) - } - - private fun control(id: String): Any = (field(view, "controls") as List<*>).first { field(it!!, "id") == id }!! - private fun editor() { - view = OnScreenControls(context, null, editing = true) - view.layout(0, 0, 1200, 800) - } - - @Test fun keyboardShortcutWorksWithHiddenPadAndButtonsOnly() { - var opened = 0 - view = OnScreenControls(context, null, onKeyboard = { opened++ }) - view.layout(0, 0, 1200, 800) - view.setQuickHidden(true) - assertNotEquals(0, pixel(90f, 766f)) - touch(MotionEvent.ACTION_DOWN, 1 to (90f to 766f)) - assertEquals(0, opened) - touch(MotionEvent.ACTION_UP, 1 to (90f to 766f)) - assertEquals(1, opened) - assertFalse(touch(MotionEvent.ACTION_DOWN, 1 to (600f to 400f))) + @Test fun systemOnlyVisibilityMovesSteamAndQamToTheBottomCornersAndReleasesOtherButtons() { + val guide = InputSurfaceElement(InputControl.GUIDE, .4f, .7f, .1f, .2f) + view.updateLayout(SavedInputLayout("test", "Test", listOf(button, guide), aspect = ASPECT)) + measure(view) + touch(MotionEvent.ACTION_DOWN, 0 to (800f to 250f)) view.setButtonsOnly(true) - touch(MotionEvent.ACTION_DOWN, 1 to (600f to 756f)) - touch(MotionEvent.ACTION_UP, 1 to (600f to 756f)) - assertEquals(2, opened) - assertEquals(-1, field(control("guide"), "pressedBy")) + measure(view) + assertFalse(state().isDown(PadState.A)) + assertFalse(view.takesTouchAt(800f, 250f)) + assertFalse(view.takesTouchAt(450f, 480f)) + assertTrue(view.takesTouchAt(44f, 556f)) + assertTrue(view.takesTouchAt(956f, 556f)) } - @Test fun cancelledDraggedOrDisabledKeyboardShortcutDoesNotOpen() { - var opened = 0 - view = OnScreenControls(context, null, onKeyboard = { opened++ }) - view.layout(0, 0, 1200, 800) - touch(MotionEvent.ACTION_DOWN, 1 to (90f to 766f)) - touch(MotionEvent.ACTION_CANCEL, 1 to (90f to 766f)) - touch(MotionEvent.ACTION_UP, 1 to (90f to 766f)) - assertEquals(0, opened) - touch(MotionEvent.ACTION_DOWN, 1 to (90f to 766f)) - touch(MotionEvent.ACTION_MOVE, 1 to (180f to 766f)) - touch(MotionEvent.ACTION_UP, 1 to (180f to 766f)) - assertEquals(0, opened) - ControllerPrefs.setKeyboardButton(context, false) - view.reload() + @Test fun aCancelledSecondFingerDoesNotArmAStickClick() { + touch(MotionEvent.ACTION_DOWN, 7 to (800f to 250f)) + touch(MotionEvent.ACTION_POINTER_DOWN or (1 shl 8), 7 to (800f to 250f), 9 to (200f to 300f)) + touch(MotionEvent.ACTION_POINTER_UP or (1 shl 8), 7 to (800f to 250f), 9 to (200f to 300f), flags = MotionEvent.FLAG_CANCELED) + touch(MotionEvent.ACTION_POINTER_DOWN or (1 shl 8), 7 to (800f to 250f), 9 to (200f to 300f)) + assertFalse(state().isDown(PadState.L3)) + touch(MotionEvent.ACTION_POINTER_UP or (1 shl 8), 7 to (800f to 250f), 9 to (200f to 300f)) + touch(MotionEvent.ACTION_POINTER_DOWN or (1 shl 8), 7 to (800f to 250f), 9 to (200f to 300f)) + assertTrue(state().isDown(PadState.L3)) + } + + @Test fun movingTheStickAndTouchingItAgainDoesNotClickIt() { + touch(MotionEvent.ACTION_DOWN, 1 to (200f to 300f)) + touch(MotionEvent.ACTION_MOVE, 1 to (250f to 300f)) + touch(MotionEvent.ACTION_UP, 1 to (250f to 300f)) + touch(MotionEvent.ACTION_DOWN, 1 to (200f to 300f)) + assertFalse(state().isDown(PadState.L3)) + } + + @Test fun liftingFarFromTheTouchPointDoesNotArmAStickClick() { + touch(MotionEvent.ACTION_DOWN, 1 to (200f to 300f)) + touch(MotionEvent.ACTION_UP, 1 to (250f to 300f)) + touch(MotionEvent.ACTION_DOWN, 1 to (200f to 300f)) + assertFalse(state().isDown(PadState.L3)) + } + + @Test fun aLongHoldThenAQuickTouchDoesNotClickTheStick() { + touch(MotionEvent.ACTION_DOWN, 1 to (200f to 300f)) + time += 600 + touch(MotionEvent.ACTION_UP, 1 to (200f to 300f)) + touch(MotionEvent.ACTION_DOWN, 1 to (200f to 300f)) + assertFalse(state().isDown(PadState.L3)) + } + + @Test fun aFastDoubleTapClicksTheStickAndCanBeHeldWhileMoving() { + touch(MotionEvent.ACTION_DOWN, 1 to (200f to 300f)) + touch(MotionEvent.ACTION_UP, 1 to (200f to 300f)) + touch(MotionEvent.ACTION_DOWN, 1 to (200f to 300f)) + touch(MotionEvent.ACTION_MOVE, 1 to (250f to 300f)) + assertTrue(state().isDown(PadState.L3)) + touch(MotionEvent.ACTION_UP, 1 to (250f to 300f)) + touch(MotionEvent.ACTION_DOWN, 1 to (200f to 300f)) + assertFalse(state().isDown(PadState.L3)) + } + + @Test fun aSlowSecondTapDoesNotClickTheStick() { + touch(MotionEvent.ACTION_DOWN, 1 to (200f to 300f)) + touch(MotionEvent.ACTION_UP, 1 to (200f to 300f)) + time += 400 + touch(MotionEvent.ACTION_DOWN, 1 to (200f to 300f)) + assertFalse(state().isDown(PadState.L3)) + } + + @Test fun theQuickToggleHidesEverythingButTheKeyboardAndShowsItAgain() { + val keyboard = InputSurfaceElement(InputControl.A, .4f, .4f, .1f, .1f, "builtin:keyboard", name = "Keyboard") + view.updateLayout(SavedInputLayout("test", "Test", listOf(left, button, keyboard), aspect = ASPECT)) + measure(view) + val density = context.resources.displayMetrics.density + val x = 34 * density + val y = 600 - 34 * density + assertTrue(view.takesTouchAt(x, y)) + touch(MotionEvent.ACTION_UP, 4 to (x to y)) + touch(MotionEvent.ACTION_DOWN, 5 to (x to y)); touch(MotionEvent.ACTION_CANCEL, 5 to (x to y)); touch(MotionEvent.ACTION_UP, 5 to (x to y)) + assertFalse(view.isQuickHidden) + touch(MotionEvent.ACTION_DOWN, 1 to (x to y)); touch(MotionEvent.ACTION_UP, 1 to (x to y)) + measure(view) + assertTrue(view.isQuickHidden) + assertFalse(view.takesTouchAt(800f, 250f)) + assertTrue(view.takesTouchAt(450f, 270f)) + touch(MotionEvent.ACTION_DOWN, 2 to (800f to 250f)); touch(MotionEvent.ACTION_UP, 2 to (800f to 250f)) + assertTrue(view.isQuickHidden) + touch(MotionEvent.ACTION_DOWN, 3 to (x to y)); touch(MotionEvent.ACTION_UP, 3 to (x to y)) + measure(view) + assertFalse(view.isQuickHidden) + assertTrue(view.takesTouchAt(800f, 250f)) + view.setButtonsOnly(true) + measure(view) + touch(MotionEvent.ACTION_DOWN, 6 to (x to y)); touch(MotionEvent.ACTION_UP, 6 to (x to y)) + assertFalse(view.isQuickHidden) + } + + @Test fun hidingTheGamepadKeepsAlwaysShownKeysDrawnAndTouchable() { + val key = InputSurfaceElement(InputControl.A, .45f, .1f, .1f, .1f, "imported:0", name = "Find", alwaysShown = true) + view.updateLayout(SavedInputLayout("test", "Test", listOf(left, button, key), aspect = ASPECT)) + measure(view) + assertTrue(view.hasPinned) + view.setPinnedOnly(true) + measure(view) + assertTrue(view.takesTouchAt(500f, 90f)) + assertFalse(view.takesTouchAt(800f, 250f)) + assertFalse(view.takesTouchAt(200f, 300f)) + view.setPinnedOnly(false) + measure(view) + assertTrue(view.takesTouchAt(800f, 250f)) + assertTrue(view.takesTouchAt(500f, 90f)) view.setQuickHidden(true) - assertEquals(0, pixel(90f, 766f)) - assertFalse(touch(MotionEvent.ACTION_DOWN, 1 to (90f to 766f))) + measure(view) + assertTrue(view.takesTouchAt(500f, 90f)) + assertFalse(view.takesTouchAt(800f, 250f)) + view.updateLayout(SavedInputLayout("test", "Test", listOf(left, button), aspect = ASPECT)) + assertFalse(view.hasPinned) } - @Test fun hidingSystemButtonsRemovesHitTargetsInBothOverlayModes() { - for (buttonsOnly in listOf(false, true)) { - ControllerPrefs.setSteamButton(context, true) - ControllerPrefs.setQamButton(context, true) - view.reload() - view.setButtonsOnly(buttonsOnly) - val guide = control("guide") - val qam = control("qam") - val gx = value(guide, "cx"); val gy = value(guide, "cy") - val qx = value(qam, "cx"); val qy = value(qam, "cy") - assertNotEquals(0, pixel(gx, gy)) - assertNotEquals(0, pixel(qx, qy)) - touch(MotionEvent.ACTION_DOWN, 1 to (gx to gy)) - ControllerPrefs.setSteamButton(context, false) - view.reload() - assertEquals(-1, field(guide, "pressedBy")) - assertEquals(0, pixel(gx, gy)) - assertFalse(touch(MotionEvent.ACTION_DOWN, 1 to (gx to gy))) - assertTrue(touch(MotionEvent.ACTION_DOWN, 1 to (qx to qy))) - touch(MotionEvent.ACTION_UP, 1 to (qx to qy)) - ControllerPrefs.setQamButton(context, false) - view.reload() - assertEquals(0, pixel(qx, qy)) - assertFalse(touch(MotionEvent.ACTION_DOWN, 1 to (qx to qy))) - } - } - private fun field(owner: Any, name: String): Any = owner.javaClass.getDeclaredField(name).apply { isAccessible = true }.get(owner)!! - private fun nullableFloat(owner: Any, name: String) = owner.javaClass.getDeclaredField(name).apply { isAccessible = true }.get(owner) as Float? - private fun value(owner: Any, name: String) = field(owner, name) as Float - - private fun activationReach(control: Any) = value(control, "radius") * 1.4f * sqrt(1.5f) - - private fun moveControl(control: Any, x: Float, y: Float) { - control.javaClass.getDeclaredField("cx").apply { isAccessible = true }.setFloat(control, x) - control.javaClass.getDeclaredField("cy").apply { isAccessible = true }.setFloat(control, y) - } - - private fun pixel(x: Float, y: Float): Int { - val bitmap = Bitmap.createBitmap(view.width, view.height, Bitmap.Config.ARGB_8888) - view.draw(Canvas(bitmap)) - val color = bitmap.getPixel(x.toInt(), y.toInt()) - bitmap.recycle() - return color - } + private companion object { const val ASPECT = 1000f / 600f } private fun touch(action: Int, vararg pointers: Pair>, flags: Int = 0): Boolean { val properties = pointers.map { (id, _) -> MotionEvent.PointerProperties().apply { this.id = id; toolType = MotionEvent.TOOL_TYPE_FINGER } }.toTypedArray() val coords = pointers.map { (_, position) -> MotionEvent.PointerCoords().apply { x = position.first; y = position.second; pressure = 1f; size = 1f } }.toTypedArray() val event = MotionEvent.obtain(1000L, time++, action, pointers.size, properties, coords, 0, 0, 1f, 1f, 0, 0, 0, flags) - return try { view.onTouchEvent(event) } finally { event.recycle() } + return try { view.dispatchTouchEvent(event) } finally { event.recycle() } } } diff --git a/app/src/test/java/com/droiddeck/launcher/input/OnScreenLayoutTest.kt b/app/src/test/java/com/droiddeck/launcher/input/OnScreenLayoutTest.kt new file mode 100644 index 0000000000..0a95382937 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/input/OnScreenLayoutTest.kt @@ -0,0 +1,292 @@ +package com.droiddeck.launcher.input + +import android.content.Context +import android.graphics.Rect +import org.json.JSONObject +import org.junit.Assert.* +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.annotation.Config + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [34], manifest = Config.NONE) +class OnScreenLayoutTest { + private val context: Context get() = RuntimeEnvironment.getApplication() + private val prefs get() = context.getSharedPreferences("input-profiles", Context.MODE_PRIVATE) + private val density get() = context.resources.displayMetrics.density + + @Before fun reset() { + for (name in listOf("input-profiles", "input-surfaces", "controller", "session")) context.getSharedPreferences(name, Context.MODE_PRIVATE).edit().clear().commit() + } + + private fun gamepad() = InputLibraries.read(context).layouts.first { it.id == InputLibraries.OVERLAY } + + private fun px(e: InputSurfaceElement, w: Int, h: Int) = floatArrayOf(e.x * w, e.y * h, e.width * w, e.height * h) + + private fun assertSquare(e: InputSurfaceElement, w: Int, h: Int) = assertEquals(e.width * w, e.height * h, 1.5f) + + private fun assertInside(elements: List, w: Int, h: Int, safe: Rect = Rect()) { + for (e in elements) { + val (x, y, ew, eh) = px(e, w, h).toList() + assertTrue("${e.id} left", x >= safe.left - .5f) + assertTrue("${e.id} top", y >= safe.top - .5f) + assertTrue("${e.id} right", x + ew <= w - safe.right + .5f) + assertTrue("${e.id} bottom", y + eh <= h - safe.bottom + .5f) + } + } + + @Test fun theBuiltInGamepadIsRegeneratedForEverySizeWithoutStretching() { + val layout = gamepad() + for ((w, h) in listOf(1200 to 540, 540 to 1200, 900 to 850)) { + val placed = OnScreenLayout.elements(context, layout, w, h, Rect()) + assertEquals(layout.elements.map { it.id }, placed.map { it.id }) + assertEquals(LegacyGamepadLayout(context, w, h).regenerate(layout.elements), placed) + for (id in listOf("legacy:a", "legacy:up", "legacy:guide")) assertSquare(placed.first { it.id == id }, w, h) + assertInside(placed, w, h) + } + } + + @Test fun regenerationKeepsTheUsersButtonStylesAndRemovedButtons() { + val layout = gamepad().let { g -> g.copy(elements = g.elements.filterNot { it.id == "legacy:y" }.map { if (it.id == "legacy:a") it.copy(opacity = .4f, icon = 0, name = "Jump") else it }) } + val placed = OnScreenLayout.elements(context, layout, 2000, 1000, Rect()) + assertNull(placed.firstOrNull { it.id == "legacy:y" }) + val a = placed.first { it.id == "legacy:a" } + assertEquals(.4f, a.opacity) + assertEquals("Jump", a.name) + assertEquals(LegacyGamepadLayout(context, 2000, 1000).elements().first { it.id == "legacy:a" }.x, a.x, .0001f) + } + + @Test fun anEditedLayoutKeepsItsAspectAndCornersWhenTheScreenRotates() { + val corner = InputSurfaceElement(InputControl.A, .85f, .7f, .1f, .2f) + val left = InputSurfaceElement(InputControl.LS, .05f, .5f, .2f, .4f) + val middle = InputSurfaceElement(InputControl.GUIDE, .45f, .05f, .1f, .2f) + val layout = SavedInputLayout("edited", "Edited", listOf(left, corner, middle), aspect = 2f) + assertEquals(layout.elements, OnScreenLayout.elements(context, layout, 2000, 1000, Rect()).map { it.copy(x = round(it.x), y = round(it.y), width = round(it.width), height = round(it.height)) }) + val portrait = OnScreenLayout.elements(context, layout, 1000, 2000, Rect()) + val (lx, ly, lw, lh) = px(portrait[0], 1000, 2000).toList() + val (cx, cy, cw, ch) = px(portrait[1], 1000, 2000).toList() + assertEquals(lw, lh, 1f) + assertEquals(cw, ch, 1f) + assertEquals(200f, lw, 1f) + assertEquals(50f, lx, 1f) + assertEquals(2000f, ly + lh + 50f, 1f) + assertEquals(1000f - 50f, cx + cw, 1f) + assertEquals(2000f - 50f, cy + ch, 1f) + assertEquals(500f, px(portrait[2], 1000, 2000).let { it[0] + it[2] / 2f }, 1f) + assertInside(portrait, 1000, 2000) + } + + private fun round(v: Float) = Math.round(v * 10000f) / 10000f + + @Test fun controlsStayOutOfTheDisplayCutout() { + val safe = Rect(120, 0, 0, 40) + val generated = OnScreenLayout.elements(context, gamepad(), 2400, 1080, safe) + assertInside(generated, 2400, 1080, safe) + assertNotEquals(OnScreenLayout.elements(context, gamepad(), 2400, 1080, Rect()), generated) + val edge = SavedInputLayout("edited", "Edited", listOf(InputSurfaceElement(InputControl.B, 0f, .9f, .05f, .1f)), aspect = 2400f / 1080) + assertInside(OnScreenLayout.elements(context, edge, 2400, 1080, safe), 2400, 1080, safe) + } + + @Test fun steamAndQamOnlyModePutsLargeButtonsInTheBottomCornersAndTheKeyboardBetween() { + val safe = Rect(80, 0, 30, 20) + val placed = OnScreenLayout.elements(context, gamepad(), 1000, 500, safe, buttonsOnly = true) + assertEquals(setOf(InputControl.GUIDE, InputControl.QAM, InputControl.A), placed.map { it.control }.toSet()) + val radius = 30 * density + val inset = 44 * density + fun centre(e: InputSurfaceElement) = (e.x + e.width / 2f) * 1000 to (e.y + e.height / 2f) * 500 + val guide = placed.first { it.control == InputControl.GUIDE } + val qam = placed.first { it.control == InputControl.QAM } + val keyboard = placed.first { it.id == "builtin:keyboard" } + assertEquals(2 * radius, guide.width * 1000, .5f) + assertEquals(80 + inset, centre(guide).first, .5f) + assertEquals(500 - 20 - inset, centre(guide).second, .5f) + assertEquals(1000 - 30 - inset, centre(qam).first, .5f) + assertEquals(500f, centre(keyboard).first, .5f) + assertEquals(500 - 20 - inset, centre(keyboard).second, .5f) + val user = SavedInputLayout("edited", "Edited", listOf(InputSurfaceElement(InputControl.A, .5f, .5f, .1f, .1f))) + assertEquals(setOf(InputControl.GUIDE, InputControl.QAM), OnScreenLayout.elements(context, user, 2000, 1000, Rect(), buttonsOnly = true).map { it.control }.toSet()) + } + + @Test fun theUnfoldedStripUsesTheDockGeometryInLandscapeAndPortrait() { + for ((size, portrait) in listOf((2208 to 800) to false, (1080 to 1300) to true)) { + val (w, h) = size + val placed = OnScreenLayout.elements(context, gamepad(), w, h, Rect(), dock = OnScreenLayout.Dock(portrait, grips = true)) + val boxes = GamepadGeometry.layout(w.toFloat(), h.toFloat(), density, portrait) + val ls = placed.first { it.id == "legacy:ls" } + val box = boxes.getValue(GamepadGeometry.Slot.LEFT_MAIN) + assertEquals(box.left, ls.x * w, 1f) + assertEquals(box.width, ls.width * w, 1f) + assertEquals(box.height, ls.height * h, 1f) + assertSquare(placed.first { it.id == "legacy:a" }, w, h) + assertNull(placed.firstOrNull { it.control == InputControl.GUIDE || it.control == InputControl.QAM }) + assertEquals(portrait, placed.any { it.control == InputControl.L4 }) + assertInside(placed, w, h) + val solid = placed.filter { it.kind != InputElementKind.STICK } + for (a in solid) for (b in solid) if (a !== b) { + val overlap = if (a.buttonShape == InputButtonShape.CIRCLE && b.buttonShape == InputButtonShape.CIRCLE) + Math.hypot(((a.x + a.width / 2f) - (b.x + b.width / 2f)) * w.toDouble(), ((a.y + a.height / 2f) - (b.y + b.height / 2f)) * h.toDouble()) < + (a.width * w + b.width * w) / 2f - .5f + else a.x < b.x + b.width - .0001f && b.x < a.x + a.width - .0001f && a.y < b.y + b.height - .0001f && b.y < a.y + a.height - .0001f + assertFalse("${a.id} overlaps ${b.id}", overlap) + } + } + val user = SavedInputLayout("edited", "Edited", listOf(InputSurfaceElement(InputControl.A, .8f, .6f, .1f, .2f)), aspect = 2208f / 1768) + val fitted = OnScreenLayout.elements(context, user, 2208, 800, Rect(), dock = OnScreenLayout.Dock(false, false)).single() + assertEquals(.1f * 2208f / 1768 / .2f, fitted.width * 2208 / (fitted.height * 800), .01f) + } + + private val legacyKeys = "29+33:0.3,0.7,44::Find::0.6:1:1:1" + + private fun importedOn(library: InputLibrary) = library.activeLayout(false, InputRole.PRIMARY)!!.elements.filter { it.id.startsWith("imported:") } + + @Test fun shownScreenKeysAreDrawnWithTheGamepadAfterMigration() { + val before = gamepad() + prefs.edit().clear().commit() + ControllerPrefs.prefs(context).edit().putString("screenKeys", legacyKeys).commit() + val migrated = InputLibraries.read(context) + val layout = migrated.activeLayout(false, InputRole.PRIMARY)!! + assertEquals(InputLibraries.OVERLAY, layout.id) + assertEquals(before.elements, layout.elements.filterNot { it.id.startsWith("imported:") }) + assertEquals(1, importedOn(migrated).size) + assertEquals(InputAction.Keys(listOf(29, 33)), migrated.compileActive(false).elementAction(InputRole.PRIMARY, "imported:0", InputControl.A)) + assertEquals(InputAction.Native(), migrated.compileActive(false).elementAction(InputRole.PRIMARY, "legacy:a", InputControl.A)) + assertTrue(migrated.profiles.any { it.id == "migrated.screenkeys.0" }) + assertTrue(prefs.getBoolean(LegacyScreenKeyMigration.MERGED, false)) + assertEquals(migrated, InputLibraries.read(context)) + val placed = OnScreenLayout.elements(context, layout, 2400, 1080, Rect()) + assertEquals(layout.elements.map { it.id }, placed.map { it.id }) + } + + @Test fun librariesMigratedBeforeTheMergeGetTheirScreenKeysBackOnceWithoutLosingEdits() { + val fresh = InputLibraries.read(context) + val backup = JSONObject().put("screenKeys", legacyKeys).toString() + val old = LegacyScreenKeyMigration.migrate(context, fresh, backup) + val bindings = old.activePart(false, InputRole.PRIMARY).bindings + val edited = old.withPreset(old.preset(bindings).let { it.copy(controls = it.controls + (InputControl.A to InputAction.Keys(listOf(57)))) }) + prefs.edit().putString("library", InputLibraries.encode(edited)).putString(LegacyScreenKeyMigration.BACKUP, backup).commit() + val merged = InputLibraries.read(context) + assertEquals(1, importedOn(merged).size) + assertEquals(InputAction.Keys(listOf(57)), merged.compileActive(false).action(InputRole.PRIMARY, InputControl.A)) + assertEquals(edited.profiles, merged.profiles) + assertEquals(edited.layouts.map { it.id }.toSet(), merged.layouts.map { it.id }.toSet()) + assertEquals(edited.layouts.first { it.id == "migrated.screenkeys.0.layout" }, merged.layouts.first { it.id == "migrated.screenkeys.0.layout" }) + assertEquals(backup, prefs.getString(LegacyScreenKeyMigration.BACKUP, null)) + assertEquals(merged, InputLibraries.read(context)) + prefs.edit().remove(LegacyScreenKeyMigration.MERGED).commit() + assertEquals(1, importedOn(InputLibraries.read(context)).size) + } + + @Test fun hiddenScreenKeysOrAProfileThatAlreadyShowsThemAreLeftAlone() { + val fresh = InputLibraries.read(context) + val hidden = JSONObject().put("screenKeys", legacyKeys).put("screenKeysShown", false).toString() + val old = LegacyScreenKeyMigration.migrate(context, fresh, hidden) + assertEquals(old, LegacyScreenKeyMigration.merge(old, hidden)) + val shown = JSONObject().put("screenKeys", legacyKeys).toString() + val selected = old.selectProfile(false, "migrated.screenkeys.0").selectProfile(true, "migrated.screenkeys.0") + assertEquals(selected, LegacyScreenKeyMigration.merge(selected, shown)) + } + + @Test fun alwaysShownKeysStayInButtonsOnlyModeAndLeaveTheUnfoldedStrip() { + val key = InputSurfaceElement(InputControl.A, .45f, .1f, .1f, .1f, "imported:0", name = "Find", alwaysShown = true) + val plain = InputSurfaceElement(InputControl.A, .45f, .4f, .1f, .1f, "imported:1", name = "Jump") + val layout = gamepad().let { it.copy(elements = it.elements + key + plain) } + val buttons = OnScreenLayout.elements(context, layout, 1000, 500, Rect(), buttonsOnly = true) + assertTrue(buttons.any { it.id == key.id }) + assertFalse(buttons.any { it.id == plain.id }) + assertEquals(OnScreenLayout.elements(context, gamepad(), 1000, 500, Rect(), buttonsOnly = true), buttons.filterNot { it.id == key.id }) + val strip = OnScreenLayout.elements(context, layout, 2208, 800, Rect(), dock = OnScreenLayout.Dock(false, grips = false)) + assertFalse(strip.any { it.id == key.id }) + assertTrue(strip.any { it.id == plain.id }) + assertTrue(OnScreenLayout.elements(context, layout, 1000, 500, Rect()).map { it.id }.containsAll(listOf(key.id, plain.id))) + } + + @Test fun migratedScreenKeysAreAlwaysShownButOtherElementsAreNot() { + prefs.edit().clear().commit() + ControllerPrefs.prefs(context).edit().putString("screenKeys", legacyKeys).commit() + val migrated = InputLibraries.read(context) + assertTrue(importedOn(migrated).all { it.alwaysShown }) + assertTrue(migrated.layouts.first { it.id == "migrated.screenkeys.0.layout" }.elements.all { it.alwaysShown }) + assertTrue(migrated.layouts.flatMap { it.elements }.filterNot { it.id.startsWith("imported:") }.none { it.alwaysShown }) + assertFalse(LegacyScreenKeyMigration.convert(context, legacyKeys, 1000, 500, 2f).any { it.first.alwaysShown }) + } + + @Test fun keysMergedByAnEarlierVersionBecomeAlwaysShownOnUpgrade() { + val fresh = InputLibraries.read(context) + val backup = JSONObject().put("screenKeys", legacyKeys).toString() + val unpinned = LegacyScreenKeyMigration.merge(LegacyScreenKeyMigration.migrate(context, fresh, backup), backup).let { library -> + library.copy(layouts = library.layouts.map { l -> l.copy(elements = l.elements.map { it.copy(alwaysShown = false) }) }) + } + val other = SavedInputLayout("user.keys", "Mine", listOf(InputSurfaceElement(InputControl.A, .1f, .1f, .1f, .1f, "imported:0", name = "Other"))) + val stored = unpinned.withLayout(other) + prefs.edit().putString("library", JSONObject(InputLibraries.encode(stored)).put("version", 12).toString()) + .putString(LegacyScreenKeyMigration.BACKUP, backup).putBoolean(LegacyScreenKeyMigration.MERGED, true).commit() + val upgraded = InputLibraries.read(context) + assertEquals(1, importedOn(upgraded).size) + assertTrue(importedOn(upgraded).single().alwaysShown) + assertTrue(upgraded.layouts.first { it.id == "migrated.screenkeys.0.layout" }.elements.all { it.alwaysShown }) + assertFalse(upgraded.layouts.first { it.id == "user.keys" }.elements.single().alwaysShown) + assertEquals(stored.layouts.map { l -> l.elements.map { it.copy(alwaysShown = false) } }, upgraded.layouts.filter { l -> stored.layouts.any { it.id == l.id } } + .map { l -> l.elements.map { it.copy(alwaysShown = false) } }) + } + + @Test fun aDeckStripPutsTheDeckGamepadSurfacesInTheStickSlotsAndOtherPadsKeepSticks() { + val stock = InputStockLayouts.layouts().first { it.id == DeckPanel.GAMEPAD.layout } + val deck = OnScreenLayout.Dock.deck(false, stock) + assertEquals(OnScreenLayout.Dock(false, true, InputControl.LS, InputControl.RIGHT_PAD), deck) + assertEquals(deck, OnScreenLayout.Dock.deck(false, null)) + val (w, h) = 2208 to 800 + val boxes = GamepadGeometry.layout(w.toFloat(), h.toFloat(), density, false) + val placed = OnScreenLayout.elements(context, gamepad(), w, h, Rect(), dock = deck) + val pad = placed.single { it.control == InputControl.RIGHT_PAD } + assertEquals(InputElementKind.TRACKPAD, pad.kind) + assertEquals(boxes.getValue(GamepadGeometry.Slot.RIGHT_MAIN).left, pad.x * w, 1f) + assertNull(placed.firstOrNull { it.control == InputControl.RS }) + assertTrue(placed.any { it.control == InputControl.LS }) + val plain = OnScreenLayout.elements(context, gamepad(), w, h, Rect(), dock = OnScreenLayout.Dock(false, false)) + assertTrue(plain.any { it.control == InputControl.RS }) + assertNull(plain.firstOrNull { it.kind == InputElementKind.TRACKPAD }) + val swapped = stock.copy(elements = stock.elements.map { + when (it.control) { + InputControl.LS -> it.copy(control = InputControl.LEFT_PAD, id = "legacy:left-pad", kind = InputElementKind.TRACKPAD) + InputControl.RIGHT_PAD -> it.copy(control = InputControl.RS, id = "legacy:rs", kind = InputElementKind.STICK) + else -> it + } + }) + assertEquals(OnScreenLayout.Dock(false, true, InputControl.LEFT_PAD, InputControl.RS), OnScreenLayout.Dock.deck(false, swapped)) + } + + private fun deckGamepad(library: InputLibrary) = library.layouts.first { it.id == DeckPanel.GAMEPAD.layout } + + private fun upgradeWithSurfaces(left: String?, right: String?, change: (InputLibrary) -> InputLibrary = { it }): InputLibrary { + val stored = change(InputLibraries.read(context)) + context.getSharedPreferences("session", Context.MODE_PRIVATE).edit().putString("gamepadLeft", left).putString("gamepadRight", right).commit() + prefs.edit().putString("library", JSONObject(InputLibraries.encode(stored)).put("version", 13).toString()).commit() + return InputLibraries.read(context) + } + + @Test fun theSavedDeckGamepadStickOrTrackpadChoiceMovesIntoTheUntouchedDeckGamepadOnce() { + val upgraded = upgradeWithSurfaces("trackpad", "stick") + val elements = deckGamepad(upgraded).elements + assertEquals(InputStockLayouts.gamepad(leftPad = true, rightStick = true), elements) + assertEquals(InputElementKind.TRACKPAD, elements.single { it.control == InputControl.LEFT_PAD }.kind) + assertEquals(InputElementKind.STICK, elements.single { it.control == InputControl.RS }.kind) + assertNull(elements.firstOrNull { it.control == InputControl.LS || it.control == InputControl.RIGHT_PAD }) + assertEquals(OnScreenLayout.Dock(false, true, InputControl.LEFT_PAD, InputControl.RS), OnScreenLayout.Dock.deck(false, deckGamepad(upgraded))) + assertEquals(upgraded, InputLibraries.read(context)) + } + + @Test fun theDefaultDeckGamepadChoiceOrAnEditedDeckGamepadIsLeftAlone() { + val stock = InputStockLayouts.layouts().first { it.id == DeckPanel.GAMEPAD.layout } + assertEquals(stock.elements, deckGamepad(upgradeWithSurfaces(null, null)).elements) + assertEquals(stock.elements, deckGamepad(upgradeWithSurfaces("stick", "trackpad")).elements) + val moved = stock.elements.map { if (it.control == InputControl.A) it.copy(x = .7f) else it } + val edited = upgradeWithSurfaces("trackpad", "stick") { it.withLayout(deckGamepad(it).copy(elements = moved)) } + assertEquals(moved, deckGamepad(edited).elements) + val others = InputLibraries.read(context).layouts.filter { it.id != DeckPanel.GAMEPAD.layout } + assertEquals(InputStockLayouts.layouts().filter { it.id != DeckPanel.GAMEPAD.layout }.map { it.elements }, + others.filter { l -> InputStockLayouts.layouts().any { it.id == l.id } }.map { it.elements }) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/input/PadBridgeQamTest.kt b/app/src/test/java/com/droiddeck/launcher/input/PadBridgeQamTest.kt index 3795c25e0a..a0c65427f5 100644 --- a/app/src/test/java/com/droiddeck/launcher/input/PadBridgeQamTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/input/PadBridgeQamTest.kt @@ -375,6 +375,37 @@ class PadBridgeQamTest { } } + @Test fun playerSlotHomeGuidePreservesRemappedInputAndTheEnableGate() { + PadBridge.setFirstSlot(1) + try { + bridge.setBuiltinPad(false) + routed(InputProfile.defaults(InputProfile.GAME) + .withBinding(InputRole.PHYSICAL, InputControl.A, InputAction.Native(InputControl.B)) + .withBinding(InputRole.PHYSICAL, InputControl.LS, InputAction.Native(InputControl.RS))) + val device = controller() + bridge.onKeyEvent(key(device, KeyEvent.KEYCODE_BUTTON_A)) + motion(device, MotionEvent.AXIS_X, .6f) + bridge.setHomeGuidePressed(true) + val held = requireNotNull(JoyConPadTest.RecordingWriter.pads[1]) + assertTrue(held.isDown(PadState.GUIDE)) + assertTrue(held.isDown(PadState.B)) + assertFalse(held.isDown(PadState.A)) + assertEquals(0f, held.leftX) + assertEquals(.6f, held.rightX) + bridge.setInputEnabled(false) + val disabled = requireNotNull(JoyConPadTest.RecordingWriter.pads[1]) + assertTrue(disabled.isDown(PadState.GUIDE)) + assertFalse(disabled.isDown(PadState.A)) + assertFalse(disabled.isDown(PadState.B)) + assertEquals(0f, disabled.leftX) + assertEquals(0f, disabled.rightX) + bridge.cancelHomeGuide() + assertFalse(requireNotNull(JoyConPadTest.RecordingWriter.pads[1]).isDown(PadState.GUIDE)) + } finally { + PadBridge.setFirstSlot(0) + } + } + @Test fun removingThePrimaryPlayerForgetsItsHomeGuide() { PadBridge.setFirstSlot(1) try { @@ -440,6 +471,117 @@ class PadBridgeQamTest { assertFalse(pad().isDown(PadState.GUIDE)) } + @Test fun ordinaryRoutedInputDoesNotWaitForTheChordQueue() { + routed(InputProfile.defaults(InputProfile.GAME)) + bridge.applyExternal({ it.press(PadState.A, true); it.leftX = .6f }, true) + assertTrue(pad().isDown(PadState.A)) + assertEquals(.6f, pad().leftX) + bridge.applyExternal({ it.press(PadState.A, false); it.leftX = 0f }, true) + assertFalse(pad().isDown(PadState.A)) + assertEquals(0f, pad().leftX) + } + + @Test fun routedQamDuringAnActiveChordPublishesOtherInputImmediately() { + ReflectionHelpers.setStaticField(FakeInputWriter::class.java, "padType", FakeInputWriter.PAD_TYPE_XBOX) + routed(InputProfile.defaults(InputProfile.GAME) + .withBinding(InputRole.PHYSICAL, InputControl.X, InputAction.Native(InputControl.QAM))) + bridge.triggerQam() + assertTrue(pad().isDown(PadState.GUIDE)) + bridge.applyExternal({ it.press(PadState.X, true); it.press(PadState.B, true); it.leftX = .6f }, true) + assertTrue(pad().isDown(PadState.GUIDE)) + assertTrue(pad().isDown(PadState.B)) + assertEquals(.6f, pad().leftX) + assertFalse(pad().isDown(PadState.QAM)) + bridge.applyExternal({ it.press(PadState.X, false); it.press(PadState.B, false); it.leftX = 0f }, true) + assertFalse(pad().isDown(PadState.B)) + assertEquals(0f, pad().leftX) + advanceTime() + assertFalse(pad().isDown(PadState.GUIDE)) + assertFalse(pad().isDown(PadState.A)) + } + + @Test fun queuedChordsKeepRoutedAndTouchAxesInsteadOfRawPhysicalAxes() { + routed(InputProfile.defaults(InputProfile.GAME) + .withBinding(InputRole.PHYSICAL, InputControl.LS, InputAction.Native(InputControl.RS))) + bridge.setBackQamPressed(true) + bridge.applyExternal({ it.leftY = -.8f }, true) + val touch = Any() + bridge.applyTouch(touch, InputRole.SECONDARY) { it.leftX = .5f } + advance(20) + bridge.applyExternal({ it.leftY = -.25f }, true) + advance(60) + assertEquals(.5f, pad().leftX) + assertEquals(0f, pad().leftY) + assertEquals(-.25f, pad().rightY) + } + + @Test fun queuedChordsDoNotLeakDesktopAxesToSteam() { + routed(InputProfile.defaults(InputProfile.DESKTOP)) + bridge.setHomeGuidePressed(true) + bridge.applyExternal({ it.leftY = -1f; it.rightY = .8f }, true) + advance(80) + assertEquals(0f, pad().leftY) + assertEquals(0f, pad().rightY) + } + + @Test fun disablingRoutingDiscardsQueuedNativeButtons() { + routed(InputProfile.defaults(InputProfile.GAME)) + bridge.setBackQamPressed(true) + bridge.applyExternal({ it.press(PadState.A, true) }, true) + advance(20) + bridge.setInputEnabled(false) + advance(60) + assertFalse(pad().isDown(PadState.A)) + bridge.setInputEnabled(true) + assertFalse(pad().isDown(PadState.A)) + } + + @Test fun changingProfileDiscardsQueuedNativeButtons() { + routed(InputProfile.defaults(InputProfile.GAME)) + bridge.setHomeGuidePressed(true) + bridge.applyExternal({ it.press(PadState.A, true) }, true) + advance(20) + bridge.setInputProfile(InputProfile.defaults(InputProfile.DESKTOP)) + advance(60) + assertFalse(pad().isDown(PadState.A)) + } + + @Test fun cancellingHomeKeepsHeldInputBlockedUntilNeutral() { + routed(InputProfile.defaults(InputProfile.GAME)) + bridge.setHomeGuidePressed(true) + bridge.applyExternal({ it.leftY = -1f }, true) + bridge.cancelHomeGuide() + bridge.applyExternal({ it.leftY = -.9f }, true) + assertEquals(0f, pad().leftY) + bridge.applyExternal({ it.leftY = 0f }, true) + bridge.applyExternal({ it.leftY = -.9f }, true) + assertEquals(-.9f, pad().leftY) + } + + @Test fun cancellingBackKeepsHeldInputBlockedUntilNeutral() { + routed(InputProfile.defaults(InputProfile.GAME)) + bridge.setBackQamPressed(true) + bridge.applyExternal({ it.leftY = -1f }, true) + bridge.cancelBackQam() + bridge.applyExternal({ it.leftY = -.9f }, true) + assertEquals(0f, pad().leftY) + bridge.applyExternal({ it.leftY = 0f }, true) + bridge.applyExternal({ it.leftY = -.9f }, true) + assertEquals(-.9f, pad().leftY) + } + + private fun routed(profile: InputProfile) { + bridge.setInputRouter(InputRouter(object : InputOutput { + override fun key(code: Int, down: Boolean) {} + override fun button(code: Int, down: Boolean) {} + override fun pointer(dx: Float, dy: Float) {} + override fun scroll(steps: Int) {} + override fun command(command: InputCommand) {} + override fun requestMotion() {} + })) + bridge.setInputProfile(profile) + } + @Test fun zeroDeadZoneDriftDoesNotKeepAxesBlockedAfterBackRelease() { val device = controller() bridge.setStickDeadZone(0) diff --git a/app/src/test/java/com/droiddeck/launcher/input/PadPartialAxesTest.kt b/app/src/test/java/com/droiddeck/launcher/input/PadPartialAxesTest.kt index a0c5bc986b..2a2bade1c4 100644 --- a/app/src/test/java/com/droiddeck/launcher/input/PadPartialAxesTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/input/PadPartialAxesTest.kt @@ -94,10 +94,71 @@ class PadPartialAxesTest { assertTrue(pad().down) } + @Test fun analogTriggerAxesWinOverTheirKeys() { + val device = device(MotionEvent.AXIS_LTRIGGER, MotionEvent.AXIS_RTRIGGER) + motion(device, MotionEvent.AXIS_LTRIGGER to 0.3f) + key(device, KeyEvent.KEYCODE_BUTTON_L2) + assertEquals(0.3f, pad().leftTrigger) + key(device, KeyEvent.KEYCODE_BUTTON_L2, repeat = 1) + assertEquals(0.3f, pad().leftTrigger) + motion(device, MotionEvent.AXIS_LTRIGGER to 0.6f) + key(device, KeyEvent.KEYCODE_BUTTON_L2, repeat = 2) + assertEquals(0.6f, pad().leftTrigger) + motion(device, MotionEvent.AXIS_LTRIGGER to 0f) + key(device, KeyEvent.KEYCODE_BUTTON_L2, KeyEvent.ACTION_UP) + assertEquals(0f, pad().leftTrigger) + } + + @Test fun bothAnalogTriggersHeldStayIndependent() { + val device = device(MotionEvent.AXIS_BRAKE, MotionEvent.AXIS_GAS) + motion(device, MotionEvent.AXIS_BRAKE to 0.4f) + key(device, KeyEvent.KEYCODE_BUTTON_L2) + motion(device, MotionEvent.AXIS_BRAKE to 0.4f, MotionEvent.AXIS_GAS to 0.7f) + key(device, KeyEvent.KEYCODE_BUTTON_R2) + key(device, KeyEvent.KEYCODE_BUTTON_R2, repeat = 1) + assertEquals(0.4f, pad().leftTrigger) + assertEquals(0.7f, pad().rightTrigger) + motion(device, MotionEvent.AXIS_BRAKE to 0.9f, MotionEvent.AXIS_GAS to 0.2f) + key(device, KeyEvent.KEYCODE_BUTTON_R2, repeat = 2) + assertEquals(0.9f, pad().leftTrigger) + assertEquals(0.2f, pad().rightTrigger) + } + + @Test fun wiredXboxTriggerKeysDoNotOverrideTheirAxes() { + val device = device(MotionEvent.AXIS_RX, MotionEvent.AXIS_RY, MotionEvent.AXIS_Z, MotionEvent.AXIS_RZ, wired = true) + motion(device, MotionEvent.AXIS_Z to 0f, MotionEvent.AXIS_RZ to -1f) + key(device, KeyEvent.KEYCODE_BUTTON_L2) + key(device, KeyEvent.KEYCODE_BUTTON_R2) + assertEquals(0.5f, pad().leftTrigger) + assertEquals(0f, pad().rightTrigger) + } + + @Test fun digitalTriggerKeysStillPressAndRelease() { + val device = device() + key(device, KeyEvent.KEYCODE_BUTTON_L2) + key(device, KeyEvent.KEYCODE_BUTTON_R2) + key(device, KeyEvent.KEYCODE_BUTTON_L2, repeat = 1) + assertEquals(1f, pad().leftTrigger) + assertEquals(1f, pad().rightTrigger) + key(device, KeyEvent.KEYCODE_BUTTON_L2, KeyEvent.ACTION_UP) + assertEquals(0f, pad().leftTrigger) + assertEquals(1f, pad().rightTrigger) + key(device, KeyEvent.KEYCODE_BUTTON_R2, KeyEvent.ACTION_UP) + assertEquals(0f, pad().rightTrigger) + } + + @Test fun keyTriggerDecision() { + assertEquals(1f, PadBridge.keyTrigger(0f, true, 0, false)) + assertEquals(0f, PadBridge.keyTrigger(1f, false, 0, false)) + assertEquals(0.25f, PadBridge.keyTrigger(0.25f, true, 1, false)) + assertEquals(0.25f, PadBridge.keyTrigger(0.25f, true, 0, true)) + assertEquals(0.25f, PadBridge.keyTrigger(0.25f, false, 0, true)) + } + private fun pad(): PadState = requireNotNull(JoyConPadTest.RecordingWriter.pads[0]) - private fun key(device: InputDevice, code: Int) { - assertTrue(bridge.onKeyEvent(KeyEvent(0L, 0L, KeyEvent.ACTION_DOWN, code, 0, 0, + private fun key(device: InputDevice, code: Int, action: Int = KeyEvent.ACTION_DOWN, repeat: Int = 0) { + assertTrue(bridge.onKeyEvent(KeyEvent(0L, 0L, action, code, repeat, 0, device.id, 0, 0, InputDevice.SOURCE_GAMEPAD))) } diff --git a/app/src/test/java/com/droiddeck/launcher/input/PhysicalInputBindingsTest.kt b/app/src/test/java/com/droiddeck/launcher/input/PhysicalInputBindingsTest.kt new file mode 100644 index 0000000000..71a0985787 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/input/PhysicalInputBindingsTest.kt @@ -0,0 +1,70 @@ +package com.droiddeck.launcher.input + +import android.hardware.input.InputManager +import android.view.InputDevice +import android.view.KeyEvent +import org.junit.Assert.* +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.Shadows.shadowOf +import org.robolectric.annotation.Config +import org.robolectric.shadows.InputDeviceBuilder + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [34], manifest = Config.NONE, shadows = [PadBridgeRoutingTest.RegisteredKeyEvent::class]) +class PhysicalInputBindingsTest { + private val events = ArrayList() + private val router = InputRouter(object : InputOutput { + override fun key(code: Int, down: Boolean) { events += "$code $down" } + override fun button(code: Int, down: Boolean) {} + override fun pointer(dx: Float, dy: Float) {} + override fun scroll(steps: Int) {} + override fun command(command: InputCommand) { events += command.name } + override fun requestMotion() {} + }) + private val bindings = PhysicalInputBindings(router) + + private fun device(id: Int): InputDevice { + val device = InputDeviceBuilder.newBuilder().setId(id).setName("Thor extra keys").setDescriptor("keys-$id") + .setSources(InputDevice.SOURCE_KEYBOARD).setKeyboardType(InputDevice.KEYBOARD_TYPE_NON_ALPHABETIC).build() + val manager = RuntimeEnvironment.getApplication().getSystemService(InputManager::class.java) + shadowOf(manager).addInputDevice(device) + return device + } + private fun event(id: Int, down: Boolean, code: Int = KeyEvent.KEYCODE_F1, repeat: Int = 0) = + KeyEvent(0L, 0L, if (down) KeyEvent.ACTION_DOWN else KeyEvent.ACTION_UP, code, repeat, 0, id, 59, 0, InputDevice.SOURCE_KEYBOARD) + + @Test fun captureReservesSystemKeysButAcceptsKeyboardBackButtons() { + val device = device(40) + assertNotNull(PhysicalInputKey.capture(event(device.id, true))) + assertNull(PhysicalInputKey.capture(event(device.id, true, KeyEvent.KEYCODE_VOLUME_UP))) + assertNull(PhysicalInputKey.capture(event(device.id, true, KeyEvent.KEYCODE_HOME))) + } + + @Test fun bindingMatchesOnlyItsDeviceAndReleasesAfterEditingProfile() { + val device = device(40); val other = device(41) + val key = PhysicalInputKey.capture(event(device.id, true))!! + bindings.configure(listOf(PhysicalInputBinding(key, InputAction.Keys(listOf(29, 30))))) + assertFalse(bindings.onKey(event(other.id, true), true)) + assertTrue(bindings.onKey(event(device.id, true), true)) + bindings.configure(listOf(PhysicalInputBinding(key, InputAction.Keys(listOf(28))))) + assertTrue(bindings.onKey(event(device.id, true, repeat = 1), true)) + assertTrue(bindings.onKey(event(device.id, false), true)) + assertEquals(listOf("29 true", "30 true", "30 false", "29 false"), events) + bindings.onKey(event(device.id, true), true) + assertEquals("28 true", events.last()) + } + + @Test fun releasesRemainConsumedAfterOpeningMenuAndVirtualKeysAreUntouched() { + val device = device(40) + val key = PhysicalInputKey.capture(event(device.id, true))!! + bindings.configure(listOf(PhysicalInputBinding(key, InputAction.Keys(listOf(57))))) + assertFalse(bindings.onKey(event(-1, true), true)) + bindings.onKey(event(device.id, true), true) + bindings.release() + assertTrue(bindings.onKey(event(device.id, false), false)) + assertEquals(listOf("57 true", "57 false"), events) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/input/ScreenKeysTest.kt b/app/src/test/java/com/droiddeck/launcher/input/ScreenKeysTest.kt deleted file mode 100644 index 14905a5964..0000000000 --- a/app/src/test/java/com/droiddeck/launcher/input/ScreenKeysTest.kt +++ /dev/null @@ -1,140 +0,0 @@ -package com.droiddeck.launcher.input - -import android.view.InputDevice -import android.view.KeyEvent -import android.view.MotionEvent -import android.view.View -import org.junit.Assert.assertEquals -import org.junit.Assert.assertFalse -import org.junit.Assert.assertTrue -import org.junit.Before -import org.junit.Test -import org.junit.runner.RunWith -import org.robolectric.RobolectricTestRunner -import org.robolectric.RuntimeEnvironment -import org.robolectric.annotation.Config - -@RunWith(RobolectricTestRunner::class) -@Config(sdk = [28], manifest = Config.NONE) -class ScreenKeysTest { - private val context = RuntimeEnvironment.getApplication() - - @Before fun clearKeys() { - ControllerPrefs.setScreenKeys(context, emptyList()) - } - - @Test fun missesAreForwardedAsTheirOwnTouchStreamWhileAKeyIsHeld() { - val sent = mutableListOf>() - val keys = ScreenKeys(context) { code, down -> sent += code to down } - keys.layout(0, 0, 1000, 600) - keys.add(listOf(KeyEvent.KEYCODE_W)) - val forwarded = mutableListOf>() - keys.onTouchMiss = { forwarded += it.actionMasked to it.getPointerId(0) } - - send(keys, MotionEvent.ACTION_DOWN, listOf(0), listOf(300f), listOf(372f)) - send(keys, MotionEvent.ACTION_POINTER_DOWN or (1 shl MotionEvent.ACTION_POINTER_INDEX_SHIFT), listOf(0, 1), listOf(300f, 900f), listOf(372f, 550f)) - send(keys, MotionEvent.ACTION_MOVE, listOf(0, 1), listOf(310f, 880f), listOf(372f, 530f)) - send(keys, MotionEvent.ACTION_POINTER_UP or (1 shl MotionEvent.ACTION_POINTER_INDEX_SHIFT), listOf(0, 1), listOf(310f, 880f), listOf(372f, 530f)) - send(keys, MotionEvent.ACTION_UP, listOf(0), listOf(310f), listOf(372f)) - - assertEquals( - listOf( - MotionEvent.ACTION_DOWN to 1, - MotionEvent.ACTION_MOVE to 1, - MotionEvent.ACTION_UP to 1, - ), - forwarded, - ) - assertEquals(listOf(KeyEvent.KEYCODE_W to true, KeyEvent.KEYCODE_W to false), sent) - } - - @Test fun gestureRecognizerKeepsOneSplitTargetOwnerUntilItsStreamEnds() { - val gate = GestureTouchStreamGate() - fun accepts(source: GestureTouchStreamGate.Source, action: Int): Boolean { - val event = MotionEvent.obtain(0L, 1L, action, 1f, 1f, 0) - return try { gate.accepts(source, event) } finally { event.recycle() } - } - - assertTrue(accepts(GestureTouchStreamGate.Source.SURFACE, MotionEvent.ACTION_DOWN)) - assertFalse(accepts(GestureTouchStreamGate.Source.SCREEN_KEYS, MotionEvent.ACTION_DOWN)) - assertTrue(accepts(GestureTouchStreamGate.Source.SURFACE, MotionEvent.ACTION_MOVE)) - assertTrue(accepts(GestureTouchStreamGate.Source.SURFACE, MotionEvent.ACTION_UP)) - assertTrue(accepts(GestureTouchStreamGate.Source.SCREEN_KEYS, MotionEvent.ACTION_DOWN)) - assertFalse(accepts(GestureTouchStreamGate.Source.SURFACE, MotionEvent.ACTION_DOWN)) - assertTrue(accepts(GestureTouchStreamGate.Source.SCREEN_KEYS, MotionEvent.ACTION_CANCEL)) - assertTrue(accepts(GestureTouchStreamGate.Source.SURFACE, MotionEvent.ACTION_DOWN)) - assertFalse(accepts(GestureTouchStreamGate.Source.SCREEN_KEYS, MotionEvent.ACTION_DOWN)) - gate.reset() - assertTrue(accepts(GestureTouchStreamGate.Source.SURFACE, MotionEvent.ACTION_DOWN)) - } - - @Test fun takesTouchAtMatchesWhatATouchDoes() { - val keys = ScreenKeys(context) { _, _ -> } - keys.layout(0, 0, 1000, 600) - keys.add(listOf(KeyEvent.KEYCODE_W)) - var checked = 0 - for (x in 0 until 1000 step 25) for (y in 0 until 600 step 25) { - val expected = keys.takesTouchAt(x.toFloat(), y.toFloat()) - val down = MotionEvent.obtain(0L, 0L, MotionEvent.ACTION_DOWN, x.toFloat(), y.toFloat(), 0) - assertEquals("($x, $y)", expected, keys.onTouchEvent(down)) - down.recycle() - MotionEvent.obtain(0L, 1L, MotionEvent.ACTION_UP, x.toFloat(), y.toFloat(), 0).also { keys.onTouchEvent(it); it.recycle() } - if (expected) checked++ - } - assertTrue("some points land on the key", checked > 0) - } - - @Test fun loadingLayoutReleasesLatchedKeysBeforeReplacingEntries() { - val sent = mutableListOf>() - val keys = ScreenKeys(context) { code, down -> sent += code to down } - keys.layout(0, 0, 1000, 600) - keys.add(listOf(KeyEvent.KEYCODE_SHIFT_LEFT)) - keys.setToggle(listOf(KeyEvent.KEYCODE_SHIFT_LEFT), true) - send(keys, MotionEvent.ACTION_DOWN, listOf(0), listOf(300f), listOf(372f)) - send(keys, MotionEvent.ACTION_UP, listOf(0), listOf(300f), listOf(372f)) - - keys.load("") - - assertEquals(listOf(KeyEvent.KEYCODE_SHIFT_LEFT to true, KeyEvent.KEYCODE_SHIFT_LEFT to false), sent) - } - - @Test fun hidingTheViewReleasesLatchedKeys() { - val sent = mutableListOf>() - val keys = ScreenKeys(context) { code, down -> sent += code to down } - keys.layout(0, 0, 1000, 600) - keys.add(listOf(KeyEvent.KEYCODE_SHIFT_LEFT)) - keys.setToggle(listOf(KeyEvent.KEYCODE_SHIFT_LEFT), true) - send(keys, MotionEvent.ACTION_DOWN, listOf(0), listOf(300f), listOf(372f)) - send(keys, MotionEvent.ACTION_UP, listOf(0), listOf(300f), listOf(372f)) - - keys.visibility = View.GONE - - assertEquals(listOf(KeyEvent.KEYCODE_SHIFT_LEFT to true, KeyEvent.KEYCODE_SHIFT_LEFT to false), sent) - } - - private fun send(view: View, action: Int, ids: List, xs: List, ys: List) { - val properties = Array(ids.size) { index -> - MotionEvent.PointerProperties().apply { - id = ids[index] - toolType = MotionEvent.TOOL_TYPE_FINGER - } - } - val coordinates = Array(ids.size) { index -> - MotionEvent.PointerCoords().apply { - x = xs[index] - y = ys[index] - pressure = 1f - size = 1f - } - } - val event = MotionEvent.obtain( - 0L, 1L, action, ids.size, properties, coordinates, 0, 0, - 1f, 1f, 0, 0, InputDevice.SOURCE_TOUCHSCREEN, 0, - ) - try { - view.onTouchEvent(event) - } finally { - event.recycle() - } - } -} diff --git a/app/src/test/java/com/droiddeck/launcher/input/SecondScreenModeTest.kt b/app/src/test/java/com/droiddeck/launcher/input/SecondScreenModeTest.kt index f0df9309de..9956f1aa6d 100644 --- a/app/src/test/java/com/droiddeck/launcher/input/SecondScreenModeTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/input/SecondScreenModeTest.kt @@ -26,6 +26,12 @@ class SecondScreenModeTest { assertEquals(SecondScreenMode.KEYBOARD_TRACKPAD, SecondScreenMode.opening("deck-controls", deckPad = false, dashboard = true)) } + @Test fun customControlsReopenWithEitherControllerType() { + for (deckPad in listOf(false, true)) { + assertEquals(SecondScreenMode.CUSTOM_CONTROLS, SecondScreenMode.opening("custom-controls", deckPad)) + } + } + @Test fun anUnknownOrClosedChoiceFallsBackToTheDefault() { assertEquals(SecondScreenMode.DECK_CONTROLS, SecondScreenMode.opening("none", deckPad = true, dashboard = true)) assertEquals(SecondScreenMode.DASHBOARD, SecondScreenMode.opening("gone", deckPad = false, dashboard = true)) @@ -40,13 +46,31 @@ class SecondScreenModeTest { } @Test fun theDesktopOpensAgainOnlyWhileItCanRun() { - assertEquals(SecondScreenMode.DESKTOP, SecondScreenMode.opening("desktop", deckPad = true, dashboard = true, desktop = true)) + assertEquals(SecondScreenMode.DESKTOP, SecondScreenMode.opening("desktop", deckPad = true, dashboard = true, bottomDesktop = true)) assertEquals(SecondScreenMode.DECK_CONTROLS, SecondScreenMode.opening("desktop", deckPad = true, dashboard = true)) assertEquals(SecondScreenMode.KEYBOARD_TRACKPAD, SecondScreenMode.opening("desktop", deckPad = false)) } @Test fun withNothingRememberedTheDesktopIsNeverPicked() { - assertEquals(SecondScreenMode.DASHBOARD, SecondScreenMode.opening(null, deckPad = false, dashboard = true, desktop = true)) - assertEquals(SecondScreenMode.KEYBOARD_TRACKPAD, SecondScreenMode.opening(null, deckPad = false, desktop = true)) + assertEquals(SecondScreenMode.DASHBOARD, SecondScreenMode.opening(null, deckPad = false, dashboard = true, bottomDesktop = true)) + assertEquals(SecondScreenMode.KEYBOARD_TRACKPAD, SecondScreenMode.opening(null, deckPad = false, bottomDesktop = true)) + } + + @Test fun desktopSessionsOpenTheTrackpadUntilTheUserPicksAnotherPanel() { + for (deckPad in listOf(false, true)) for (dashboard in listOf(false, true)) { + assertEquals(SecondScreenMode.KEYBOARD_TRACKPAD, SecondScreenMode.opening(null, deckPad, dashboard, desktopSession = true)) + assertEquals(SecondScreenMode.KEYBOARD_TRACKPAD, SecondScreenMode.opening("gone", deckPad, dashboard, desktopSession = true)) + assertEquals(SecondScreenMode.CUSTOM_CONTROLS, SecondScreenMode.opening("custom-controls", deckPad, dashboard, desktopSession = true)) + assertEquals(SecondScreenMode.TERMINAL, SecondScreenMode.opening("terminal", deckPad, dashboard, desktopSession = true)) + } + } + + @Test fun theEditorOpensOnTheChosenBottomScreenUnlessAnotherAppHoldsIt() { + val free = { _: Int -> false } + assertEquals(listOf(2, 3), SecondScreenDisplays.editorCandidates(2, listOf(2, 3), free)) + assertEquals(listOf(3, 2), SecondScreenDisplays.editorCandidates(3, listOf(2, 3), free)) + assertEquals(listOf(-1, 2), SecondScreenDisplays.editorCandidates(-1, listOf(2), free)) + assertEquals(listOf(3), SecondScreenDisplays.editorCandidates(2, listOf(2, 3)) { it == 2 }) + assertEquals(emptyList(), SecondScreenDisplays.editorCandidates(2, listOf(2)) { it == 2 }) } } diff --git a/app/src/test/java/com/droiddeck/launcher/input/SecondScreenProfileMigrationTest.kt b/app/src/test/java/com/droiddeck/launcher/input/SecondScreenProfileMigrationTest.kt new file mode 100644 index 0000000000..669db39752 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/input/SecondScreenProfileMigrationTest.kt @@ -0,0 +1,150 @@ +package com.droiddeck.launcher.input + +import android.content.Context +import android.content.Intent +import android.hardware.display.DisplayManager +import android.view.Display +import androidx.compose.runtime.MutableState +import com.droiddeck.launcher.SessionActivity +import com.droiddeck.launcher.session.SessionPrefs +import com.droiddeck.launcher.session.SessionService +import com.droiddeck.launcher.session.SessionState +import org.junit.After +import org.junit.Assert.* +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.Robolectric +import org.robolectric.RobolectricTestRunner +import org.robolectric.Shadows.shadowOf +import org.robolectric.annotation.Config +import org.robolectric.util.ReflectionHelpers + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [28], shadows = [SessionInputPointerTest.WriterShadow::class]) +class SecondScreenProfileMigrationTest { + private lateinit var activity: SessionActivity + private val previousMode = SessionState.mode + private val previousPanel = SessionState.secondScreenMode + private val previousDisplay = SessionState.secondScreenDisplay + private val previousDeckPad = SessionState.deckPad + + @Before fun setUp() { + SessionState.secondScreenMode = SecondScreenMode.NONE + activity = Robolectric.buildActivity(SessionActivity::class.java).get() + val displays = activity.getSystemService(DisplayManager::class.java) + shadowOf(displays.getDisplay(Display.DEFAULT_DISPLAY)).setFlags(Display.FLAG_PRESENTATION) + ReflectionHelpers.setField(activity, "displayManager", displays) + ReflectionHelpers.setField(activity, "started", true) + val display: MutableState = ReflectionHelpers.getField(activity, "selectedSecondScreenDisplay\$delegate") + display.value = Display.DEFAULT_DISPLAY + } + + @After fun tearDown() { + ReflectionHelpers.callInstanceMethod(activity, "closeSecondScreen", ReflectionHelpers.ClassParameter.from(Boolean::class.javaPrimitiveType, true)) + SessionState.mode = previousMode + SessionState.secondScreenMode = previousPanel + SessionState.secondScreenDisplay = previousDisplay + SessionState.deckPad = previousDeckPad + } + + @Test fun firstOpeningKeepsTheLibraryAndOpensTheTrackpadOnDesktopAndTheControllerPanelInGames() { + for (desktop in listOf(false, true)) for (deckPad in listOf(false, true)) { + val original = customLibrary(desktop) + open(desktop, deckPad) + assertEquals(original, InputLibraries.read(activity)) + assertNull(SessionPrefs.secondScreenPanel(activity, desktop)) + val dashboard = ReflectionHelpers.callInstanceMethod>(activity, "bottomPages").isNotEmpty() + val expected = when { + desktop -> SecondScreenMode.KEYBOARD_TRACKPAD + deckPad -> SecondScreenMode.DECK_CONTROLS + dashboard -> SecondScreenMode.DASHBOARD + else -> SecondScreenMode.KEYBOARD_TRACKPAD + } + assertEquals(expected, SessionState.secondScreenMode) + } + } + + @Test fun desktopAndGamesEachReopenTheirOwnLastPanel() { + customLibrary(desktop = false) + SessionPrefs.setSecondScreenPanel(activity, true, SecondScreenMode.TERMINAL.id) + SessionPrefs.setSecondScreenPanel(activity, false, SecondScreenMode.CUSTOM_CONTROLS.id) + open(desktop = true, deckPad = true) + assertEquals(SecondScreenMode.TERMINAL, SessionState.secondScreenMode) + open(desktop = false, deckPad = true) + assertEquals(SecondScreenMode.CUSTOM_CONTROLS, SessionState.secondScreenMode) + } + + @Test fun theSharedPanelChoiceCarriesOverToGamesExceptTheForcedProfileControls() { + val prefs = activity.getSharedPreferences("session", Context.MODE_PRIVATE) + prefs.edit().putString("secondScreenPanel", SecondScreenMode.TERMINAL.id).commit() + assertEquals(SecondScreenMode.TERMINAL.id, SessionPrefs.secondScreenPanel(activity, false)) + assertNull(SessionPrefs.secondScreenPanel(activity, true)) + customLibrary(desktop = true) + open(desktop = true, deckPad = true) + assertEquals(SecondScreenMode.KEYBOARD_TRACKPAD, SessionState.secondScreenMode) + prefs.edit().putString("secondScreenPanel", SecondScreenMode.CUSTOM_CONTROLS.id).commit() + for (desktop in listOf(false, true)) assertNull(SessionPrefs.secondScreenPanel(activity, desktop)) + } + + @Test fun trackpadAndDeckPanelsUseTheirStockBindingsWithoutChangingTheProfile() { + for (desktop in listOf(false, true)) for (mode in listOf(SecondScreenMode.KEYBOARD_TRACKPAD, SecondScreenMode.DECK_CONTROLS)) { + val original = customLibrary(desktop) + SessionState.deckPad = true + val stock = original.preset(mode.bindings!!) + show(mode) + assertEquals(original, InputLibraries.read(activity)) + assertEquals(stock.layoutId, routedLayout()?.id) + assertEquals(stock.elements, routedProfile().elements[InputRole.SECONDARY].orEmpty()) + show(SecondScreenMode.CUSTOM_CONTROLS) + assertEquals("custom.layout", routedLayout()?.id) + assertEquals(InputAction.Keys(listOf(57)), routedProfile().elementAction(InputRole.SECONDARY, "custom.button", InputControl.A)) + } + } + + @Test fun aDeckPanelLeftOverWithoutTheDeckPadOpensTheFallbackInstead() { + for (desktop in listOf(false, true)) { + customLibrary(desktop) + SessionState.deckPad = false + show(SecondScreenMode.DECK_CONTROLS) + val fallback = ReflectionHelpers.callInstanceMethod(activity, "openingPanel") + assertNotEquals(SecondScreenMode.DECK_CONTROLS, fallback) + if (desktop) assertEquals(SecondScreenMode.KEYBOARD_TRACKPAD, fallback) + assertEquals(fallback, SessionState.secondScreenMode) + assertNotEquals(InputLibraries.DECK, routedLayout()?.id) + } + } + + private fun customLibrary(desktop: Boolean): InputLibrary { + session(desktop) + return InputLibraries.update(activity) { library -> + val id = if (desktop) InputProfile.DESKTOP else InputProfile.GAME + val element = InputSurfaceElement(InputControl.A, .3f, .2f, .2f, .2f, "custom.button") + val layout = SavedInputLayout("custom.layout", "Custom controls", listOf(element), screen = InputLayoutScreen.SECONDARY) + val bindings = SavedInputBindings("custom.bindings", "Custom actions", false, layout.id, + elements = mapOf(element.id to InputAction.Keys(listOf(57)))) + val profile = library.profile(id) + library.withLayout(layout).withPreset(bindings) + .withProfile(profile.copy(parts = profile.parts + (InputRole.SECONDARY to InputPart(bindings.id, layout.id)))) + .copy(selections = emptyMap()) + } + } + + private fun routedProfile(): InputProfile = ReflectionHelpers.getField(activity, "inputProfile") + + private fun routedLayout(): SavedInputLayout? = ReflectionHelpers.getField>(activity, "secondaryInputLayout\$delegate").value + + private fun session(desktop: Boolean) { + SessionState.mode = if (desktop) SessionService.MODE_DESKTOP else SessionService.MODE_STEAM + activity.intent = Intent().putExtra(SessionService.EXTRA_MODE, SessionState.mode) + } + + private fun open(desktop: Boolean, deckPad: Boolean) { + session(desktop) + SessionState.deckPad = deckPad + show(ReflectionHelpers.callInstanceMethod(activity, "openingPanel")) + } + + private fun show(mode: SecondScreenMode) = + ReflectionHelpers.callInstanceMethod(activity, "showSecondScreen", ReflectionHelpers.ClassParameter.from(SecondScreenMode::class.java, mode)) +} diff --git a/app/src/test/java/com/droiddeck/launcher/input/SelectProfileRoutingTest.kt b/app/src/test/java/com/droiddeck/launcher/input/SelectProfileRoutingTest.kt new file mode 100644 index 0000000000..3cbdaacc4d --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/input/SelectProfileRoutingTest.kt @@ -0,0 +1,110 @@ +package com.droiddeck.launcher.input + +import android.hardware.input.InputManager +import android.view.InputDevice +import android.view.KeyEvent +import java.io.File +import org.junit.After +import org.junit.Assert.* +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.Shadows.shadowOf +import org.robolectric.annotation.Config +import org.robolectric.shadows.InputDeviceBuilder + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [34], manifest = Config.NONE, shadows = [JoyConPadTest.RecordingWriter::class, + PadBridgeRoutingTest.RegisteredKeyEvent::class]) +class SelectProfileRoutingTest { + private val context get() = RuntimeEnvironment.getApplication() + private val bridge = PadBridge(File(context.cacheDir, "select-profile")) + private val events = mutableListOf() + private val output = object : InputOutput { + override fun key(code: Int, down: Boolean) { events += "key $code $down" } + override fun button(code: Int, down: Boolean) = Unit + override fun pointer(dx: Float, dy: Float) = Unit + override fun scroll(steps: Int) = Unit + override fun command(command: InputCommand) { events += command.name } + override fun requestMotion() = Unit + } + private val device = InputDeviceBuilder.newBuilder().setId(42).setName("Select test pad") + .setDescriptor("select-test").setSources(InputDevice.SOURCE_GAMEPAD).build() + + @Before fun setUp() { + context.getSharedPreferences("input-profiles", 0).edit().clear().commit() + context.getSharedPreferences("controller", 0).edit().clear().commit() + ControllerPrefs.setSelectSteam(context, true) + shadowOf(context.getSystemService(InputManager::class.java)).addInputDevice(device) + bridge.setInputRouter(InputRouter(output)) + bridge.setInputProfile(InputProfiles.active(context, desktop = false)) + } + + @After fun close() { bridge.stop() } + + @Test fun migratedSteamSelectReachesGuideAndReleasesWithoutReleasingPhysicalGuide() { + key(KeyEvent.KEYCODE_BUTTON_SELECT, true) + assertTrue(state().isDown(PadState.GUIDE)) + assertFalse(state().isDown(PadState.SELECT)) + key(KeyEvent.KEYCODE_BUTTON_MODE, true) + key(KeyEvent.KEYCODE_BUTTON_SELECT, false) + assertTrue(state().isDown(PadState.GUIDE)) + key(KeyEvent.KEYCODE_BUTTON_MODE, false) + assertFalse(state().isDown(PadState.GUIDE)) + } + + @Test fun profileResetAndExplicitNativeBindingOverrideTheMigratedSetting() { + val migrated = InputProfiles.active(context, desktop = false) + InputProfiles.save(context, migrated.withBinding(InputRole.PHYSICAL, InputControl.SELECT, InputAction.Native())) + bridge.setInputProfile(InputProfiles.active(context, desktop = false)) + assertNativeSelect() + InputProfiles.reset(context, InputProfile.GAME) + bridge.setInputProfile(InputProfiles.active(context, desktop = false)) + assertNativeSelect() + assertTrue(ControllerPrefs.read(context).selectSteam) + } + + @Test fun disabledAndLocalSelectBindingsDoNotLeakGuide() { + for (action in listOf(InputAction.Disabled, InputAction.Keys(listOf(28)), InputAction.Command(InputCommand.KEYBOARD))) { + bridge.setInputProfile(InputProfile.defaults(InputProfile.GAME).withBinding(InputRole.PHYSICAL, InputControl.SELECT, action)) + key(KeyEvent.KEYCODE_BUTTON_SELECT, true) + assertFalse(state().isDown(PadState.GUIDE)) + assertFalse(state().isDown(PadState.SELECT)) + key(KeyEvent.KEYCODE_BUTTON_SELECT, false) + } + assertEquals(listOf("key 28 true", "key 28 false", "KEYBOARD"), events) + } + + @Test fun replacingOrRemovingRouterWhileSelectIsHeldNeverLeavesGuideStuck() { + key(KeyEvent.KEYCODE_BUTTON_SELECT, true) + bridge.setInputRouter(null) + assertFalse(state().isDown(PadState.GUIDE)) + key(KeyEvent.KEYCODE_BUTTON_SELECT, false) + assertFalse(state().isDown(PadState.SELECT)) + key(KeyEvent.KEYCODE_BUTTON_SELECT, true) + bridge.setInputRouter(InputRouter(output).also { it.setProfile(InputProfiles.active(context, desktop = false)) }) + assertFalse(state().isDown(PadState.GUIDE)) + key(KeyEvent.KEYCODE_BUTTON_SELECT, false) + assertFalse(state().isDown(PadState.GUIDE)) + key(KeyEvent.KEYCODE_BUTTON_SELECT, true) + assertTrue(state().isDown(PadState.GUIDE)) + key(KeyEvent.KEYCODE_BUTTON_SELECT, false) + assertFalse(state().isDown(PadState.GUIDE)) + } + + private fun assertNativeSelect() { + key(KeyEvent.KEYCODE_BUTTON_SELECT, true) + assertTrue(state().isDown(PadState.SELECT)) + assertFalse(state().isDown(PadState.GUIDE)) + key(KeyEvent.KEYCODE_BUTTON_SELECT, false) + } + + private fun key(code: Int, down: Boolean) { + assertTrue(bridge.onKeyEvent(KeyEvent(0, 0, if (down) KeyEvent.ACTION_DOWN else KeyEvent.ACTION_UP, + code, 0, 0, device.id, 0, 0, InputDevice.SOURCE_GAMEPAD))) + } + + private fun state() = JoyConPadTest.RecordingWriter.pads[0]!! +} diff --git a/app/src/test/java/com/droiddeck/launcher/input/SessionBackBindingTest.kt b/app/src/test/java/com/droiddeck/launcher/input/SessionBackBindingTest.kt new file mode 100644 index 0000000000..4e6669d928 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/input/SessionBackBindingTest.kt @@ -0,0 +1,141 @@ +package com.droiddeck.launcher.input + +import android.hardware.input.InputManager +import android.os.Looper +import android.view.InputDevice +import android.view.KeyEvent +import androidx.compose.runtime.MutableState +import com.droiddeck.launcher.SessionActivity +import com.droiddeck.launcher.session.LoadingState +import com.droiddeck.launcher.session.SessionService +import com.droiddeck.launcher.session.SessionState +import java.io.File +import java.time.Duration +import org.junit.After +import org.junit.Assert.* +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.Robolectric +import org.robolectric.RobolectricTestRunner +import org.robolectric.Shadows.shadowOf +import org.robolectric.annotation.Config +import org.robolectric.shadows.InputDeviceBuilder +import org.robolectric.util.ReflectionHelpers + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [34], manifest = Config.NONE, shadows = [JoyConPadTest.RecordingWriter::class, + PadBridgeRoutingTest.RegisteredKeyEvent::class]) +class SessionBackBindingTest { + private val activity = Robolectric.buildActivity(SessionActivity::class.java).get() + private val bridge = PadBridge(File(activity.cacheDir, "back-binding")) + private val events = mutableListOf>() + private val router = InputRouter(object : InputOutput { + override fun key(code: Int, down: Boolean) { events += code to down } + override fun button(code: Int, down: Boolean) {} + override fun pointer(dx: Float, dy: Float) {} + override fun scroll(steps: Int) {} + override fun command(command: InputCommand) {} + override fun requestMotion() {} + }) + private val bindings = PhysicalInputBindings(router) + private val previousMode = SessionState.mode + private val previousRunning = SessionState.running + private val previousFrame = SessionState.firstFrameSeen + private val previousDeck = SessionState.deckPad + private val previousSuspended = SessionState.suspended + + @Before fun setUp() { + SessionState.mode = SessionService.MODE_STEAM + SessionState.running = true + SessionState.firstFrameSeen = true + SessionState.deckPad = true + SessionState.suspended = false + ReflectionHelpers.setStaticField(FakeInputWriter::class.java, "padType", FakeInputWriter.PAD_TYPE_DECK) + ReflectionHelpers.setField(activity, "padBridge", bridge) + ReflectionHelpers.setField(activity, "physicalInputBindings", bindings) + ReflectionHelpers.setField(activity, "loading", LoadingState(activity).apply { visible = false }) + ReflectionHelpers.getField>(activity, "resumed\$delegate").value = true + val device = InputDeviceBuilder.newBuilder().setId(42).setName("Back key").setDescriptor("back-key") + .setSources(InputDevice.SOURCE_KEYBOARD).setKeyboardType(InputDevice.KEYBOARD_TYPE_NON_ALPHABETIC).build() + shadowOf(activity.getSystemService(InputManager::class.java)).addInputDevice(device) + bridge.setInputRouter(router) + bridge.start() + } + + @After fun close() { + ReflectionHelpers.getField(activity, "backQamInput").cancel() + bindings.close() + bridge.stop() + SessionState.mode = previousMode + SessionState.running = previousRunning + SessionState.firstFrameSeen = previousFrame + SessionState.deckPad = previousDeck + SessionState.suspended = previousSuspended + } + + @Test fun capturedBackBindingTakesPrecedenceOverTheDefaultQamShortcut() { + bindings.configure(listOf(PhysicalInputBinding(PhysicalInputKey.capture(event(true))!!, InputAction.Keys(listOf(28))))) + assertTrue(activity.dispatchKeyEvent(event(true))) + assertEquals(listOf(28 to true), events) + advance(700) + assertFalse(JoyConPadTest.RecordingWriter.pads[0]?.isDown(PadState.QAM) == true) + assertTrue(activity.dispatchKeyEvent(event(false))) + assertEquals(listOf(28 to true, 28 to false), events) + } + + @Test fun anUnmappedBackKeyStillHoldsAndReleasesQam() { + assertTrue(activity.dispatchKeyEvent(event(true))) + advance(700) + assertTrue(JoyConPadTest.RecordingWriter.pads[0]!!.isDown(PadState.QAM)) + assertTrue(activity.dispatchKeyEvent(event(false))) + advance(1000) + assertFalse(JoyConPadTest.RecordingWriter.pads[0]!!.isDown(PadState.QAM)) + assertTrue(events.isEmpty()) + } + + @Test fun capturedProfileBindingTakesPrecedenceOverGlobalShortcut() { + assignMenuShortcut() + bindings.configure(listOf(PhysicalInputBinding(PhysicalInputKey.capture(event(true))!!, InputAction.Keys(listOf(28))))) + assertTrue(activity.dispatchKeyEvent(event(true))) + assertTrue(activity.dispatchKeyEvent(event(false))) + assertEquals(listOf(28 to true, 28 to false), events) + assertFalse(drawerOpen()) + } + + @Test fun globalShortcutOverridesDefaultBackQamAndConsumesReleaseAcrossMenuChange() { + assignMenuShortcut() + assertTrue(activity.dispatchKeyEvent(event(true))) + assertTrue(drawerOpen()) + advance(700) + assertFalse(JoyConPadTest.RecordingWriter.pads[0]?.isDown(PadState.QAM) == true) + assertTrue(activity.dispatchKeyEvent(event(false))) + assertTrue(drawerOpen()) + assertTrue(activity.dispatchKeyEvent(event(true))) + assertFalse(drawerOpen()) + assertTrue(activity.dispatchKeyEvent(event(false))) + assertTrue(events.isEmpty()) + } + + @Test fun inputSettingsDoNotExecuteGlobalShortcuts() { + assignMenuShortcut() + ReflectionHelpers.getField>(activity, "inputSettingsOpen\$delegate").value = true + activity.dispatchKeyEvent(event(true)) + activity.dispatchKeyEvent(event(false)) + assertFalse(drawerOpen()) + assertTrue(events.isEmpty()) + } + + private fun assignMenuShortcut() { + ReflectionHelpers.getField(activity, "physicalShortcuts").bindings = + listOf(PhysicalShortcuts.Binding(PhysicalShortcuts.button(event(true))!!, PhysicalShortcuts.Action.DROIDDECK)) + } + + private fun drawerOpen(): Boolean = + ReflectionHelpers.getField>(activity, "drawerOpenState\$delegate").value + + private fun event(down: Boolean) = KeyEvent(0L, 0L, if (down) KeyEvent.ACTION_DOWN else KeyEvent.ACTION_UP, + KeyEvent.KEYCODE_BACK, 0, 0, 42, 158, 0, InputDevice.SOURCE_KEYBOARD) + + private fun advance(ms: Long) = shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(ms)) +} diff --git a/app/src/test/java/com/droiddeck/launcher/input/SessionInputPointerTest.kt b/app/src/test/java/com/droiddeck/launcher/input/SessionInputPointerTest.kt new file mode 100644 index 0000000000..835eca2e9b --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/input/SessionInputPointerTest.kt @@ -0,0 +1,302 @@ +package com.droiddeck.launcher.input + +import android.os.Looper +import android.view.SurfaceView +import androidx.compose.runtime.MutableState +import androidx.compose.runtime.snapshots.Snapshot +import androidx.compose.ui.geometry.Offset +import com.droiddeck.launcher.SessionActivity +import com.droiddeck.launcher.session.SessionService +import com.droiddeck.launcher.session.SessionState +import com.droiddeck.launcher.wayland.WaylandCompositor +import org.junit.After +import org.junit.Assert.* +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.Robolectric +import org.robolectric.RobolectricTestRunner +import org.robolectric.Shadows.shadowOf +import org.robolectric.annotation.Config +import org.robolectric.annotation.Implementation +import org.robolectric.annotation.Implements +import org.robolectric.util.ReflectionHelpers +import java.util.Collections +import java.util.concurrent.CountDownLatch +import java.util.concurrent.Executors +import java.util.concurrent.TimeUnit + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [28], manifest = Config.NONE, shadows = [SessionInputPointerTest.CompositorShadow::class, SessionInputPointerTest.WriterShadow::class]) +class SessionInputPointerTest { + private lateinit var activity: SessionActivity + private lateinit var output: InputOutput + private var previousSize = SessionState.outputSize + private var previousMode = SessionState.mode + + @Before fun setUp() { + activity = Robolectric.buildActivity(SessionActivity::class.java).get() + output = ReflectionHelpers.getField(activity, "inputOutput") + ReflectionHelpers.setField(activity, "surfaceView", SurfaceView(activity).apply { layout(0, 0, 1000, 600) }) + SessionState.outputSize = 1600 to 900 + SessionState.mode = SessionService.MODE_DESKTOP + InputProfiles.resetAll(activity) + InputProfiles.select(activity, InputProfile.DESKTOP) + refresh() + CompositorShadow.pointers.clear() + CompositorShadow.events.clear() + CompositorShadow.keys.clear() + WriterShadow.mouse.clear() + WriterShadow.beforeWheel = null + } + + @After fun tearDown() { + shadowOf(Looper.getMainLooper()).idle() + SessionState.outputSize = previousSize + SessionState.mode = previousMode + } + + @Test fun desktopMouseButtonsKeepTheVisibleCursorAndDeliverImmediately() { + state("cursorPos").value = Offset(500f, 300f) + state("cursorVisible").value = true + output.button(272, true) + invoke("onPadInput") + output.button(272, false) + assertTrue(state("cursorVisible").value) + assertEquals(listOf(Triple(3, 272, 1), Triple(3, 272, 0)), CompositorShadow.events) + } + + @Test fun nativeGameInputStillHidesCursor() { + InputProfiles.select(activity, InputProfile.GAME) + refresh() + state("cursorVisible").value = true + invoke("onPadInput") + assertFalse(state("cursorVisible").value) + } + + @Test fun lowerScreenPointerBindingAlsoKeepsCursorVisible() { + InputProfiles.save(activity, InputProfile(InputProfile.GAME).withBinding(InputRole.SECONDARY, InputControl.RIGHT_PAD, InputAction.Pointer())) + InputProfiles.select(activity, InputProfile.GAME) + refresh() + state("cursorVisible").value = true + invoke("onPadInput") + assertTrue(state("cursorVisible").value) + } + + @Test fun stickContinuesFromTrackpadPositionAndUpdatesVisibleAndGuestCoordinates() { + output.pointer(-400f, -225f) + output.pointer(160f, 90f) + invoke("onPadInput") + assertEquals(Offset(350f, 215.625f), state("cursorPos").value) + assertTrue(state("cursorVisible").value) + assertEquals(Triple(1, InputSpace.x(350f, 1000), InputSpace.y(215.625f, 600)), CompositorShadow.pointers.last()) + } + + @Test fun firstStickMovementStartsAtCenterAndClampsToPictureBounds() { + output.pointer(160f, 90f) + assertEquals(Offset(600f, 356.25f), state("cursorPos").value) + output.pointer(10000f, -10000f) + assertEquals(Offset(1000f, 18.75f), state("cursorPos").value) + } + + @Test fun stickFrameNotifiesCursorDrawingBeforeReturningToTheLooper() { + val position = state("cursorPos") + Snapshot.sendApplyNotifications() + var changed = false + val observer = Snapshot.registerApplyObserver { objects, _ -> + if (position in objects) changed = true + } + try { + output.pointer(20f, 10f) + assertTrue(changed) + } finally { observer.dispose() } + } + + @Test fun pointerLockKeepsRelativeMotionAndHiddenCursor() { + ReflectionHelpers.setField(activity, "pointerLocked", true) + state("cursorVisible").value = true + output.pointer(2.5f, -1.25f) + assertEquals(listOf(Triple(6, 640, -320)), CompositorShadow.events) + assertTrue(CompositorShadow.pointers.isEmpty()) + assertFalse(state("cursorVisible").value) + } + + @Test fun clientRequestedHiddenCursorStaysHidden() { + ReflectionHelpers.setField(activity, "clientHidesCursor", true) + output.pointer(1f, 1f) + assertFalse(state("cursorVisible").value) + } + + @Test fun mappedPointerStaysVisibleWithDirectTouchSelected() { + ReflectionHelpers.setField(activity, "routedTouchMode", com.droiddeck.launcher.session.SessionPrefs.TOUCH_DIRECT) + state("directCursorOn").value = false + output.pointer(20f, 10f) + assertTrue(state("cursorVisible").value) + } + + @Test fun touchpadZoomDoesNotReleaseCtrlHeldByTheProfileOrScreenKeys() { + val held = ReflectionHelpers.getField(activity, "heldKeys") + output.key(29, true) + held.set(64, 29, true) + ReflectionHelpers.getField(activity, "pointerListener").onZoom(1) + assertEquals(listOf(29 to 1), CompositorShadow.keys) + output.key(29, false) + assertEquals(listOf(29 to 1), CompositorShadow.keys) + held.set(64, 29, false) + assertEquals(listOf(29 to 1, 29 to 0), CompositorShadow.keys) + assertEquals(listOf(Triple(4, 1, 0)), CompositorShadow.events) + } + + @Test fun desktopRightCtrlUsesUpstreamAliasWithoutReleasingTheProfileHold() { + fun hardwareCtrl(down: Boolean) = ReflectionHelpers.callInstanceMethod(activity, "sendGuestKey", + ReflectionHelpers.ClassParameter.from(Int::class.javaPrimitiveType, 97), + ReflectionHelpers.ClassParameter.from(Boolean::class.javaPrimitiveType, down), + ReflectionHelpers.ClassParameter.from(Int::class.javaPrimitiveType, 8)) + output.key(29, true) + hardwareCtrl(true) + hardwareCtrl(false) + assertEquals(listOf(29 to 1), CompositorShadow.keys) + output.key(29, false) + assertEquals(listOf(29 to 1, 29 to 0), CompositorShadow.keys) + } + + @Test fun routedMouseUsesRawBackendAndKeepsSharedButtonOwnership() { + ReflectionHelpers.setField(activity, "mouseWriter", FakeInputWriter(activity.cacheDir.path, FakeInputWriter.MOUSE_SLOT)) + ReflectionHelpers.setField(activity, "evdevMouseActive", true) + ReflectionHelpers.setField(activity, "pointerLocked", true) + val held = ReflectionHelpers.getField(activity, "heldButtons") + output.button(272, true) + held.set(2, 272, true) + output.button(272, false) + assertEquals(listOf("button:272:true"), WriterShadow.mouse) + held.set(2, 272, false) + output.pointer(2.5f, -1.25f) + output.scroll(2) + assertEquals(listOf("button:272:true", "button:272:false", "delta:2.5:-1.25", "wheel:-2:0:120"), WriterShadow.mouse) + assertTrue(CompositorShadow.events.isEmpty()) + } + + @Test fun focusCleanupReleasesHardwareAndKeyboardKeysWithoutReleasingAnotherOwner() { + val held = ReflectionHelpers.getField(activity, "heldKeys") + output.key(29, true) + for ((owner, code) in listOf(8 to 30, 16 to 31, 32 to 32, 16 to 29)) held.set(owner, code, true) + ReflectionHelpers.callInstanceMethod(activity, "releaseHeldGuestInput") + assertEquals(setOf(30 to 0, 31 to 0, 32 to 0), CompositorShadow.keys.filter { it.second == 0 }.toSet()) + output.key(29, false) + assertEquals(29 to 0, CompositorShadow.keys.last()) + } + + @Test fun horizontalWheelUsesTheSameDirectionInBothMouseBackends() { + output.scrollHorizontal(2) + assertEquals(listOf(Triple(11, 0, 240)), CompositorShadow.events) + CompositorShadow.events.clear() + ReflectionHelpers.setField(activity, "mouseWriter", FakeInputWriter(activity.cacheDir.path, FakeInputWriter.MOUSE_SLOT)) + ReflectionHelpers.setField(activity, "evdevMouseActive", true) + output.scrollHorizontal(2) + assertEquals(listOf("wheel:0:2:120"), WriterShadow.mouse) + assertTrue(CompositorShadow.events.isEmpty()) + } + + @Test fun leavingRawMouseReleasesItsHoldBeforeReturningToTheWaylandSeat() { + ReflectionHelpers.setField(activity, "mouseWriter", FakeInputWriter(activity.cacheDir.path, FakeInputWriter.MOUSE_SLOT)) + ReflectionHelpers.setField(activity, "evdevMouseActive", true) + output.button(272, true) + invoke("closeEvdevMouse") + output.button(272, false) + assertEquals(listOf("button:272:true", "button:272:false", "release"), WriterShadow.mouse) + assertTrue(CompositorShadow.events.isEmpty()) + output.button(272, true) + output.button(272, false) + assertEquals(listOf(Triple(3, 272, 1), Triple(3, 272, 0)), CompositorShadow.events) + } + + @Test fun wowFieldSelectionPreservesCtrlHeldByTheInputProfile() { + output.key(29, true) + ReflectionHelpers.callInstanceMethod(activity, "selectGameField", + ReflectionHelpers.ClassParameter.from(IntArray::class.java, intArrayOf(46))) + assertEquals(listOf(29 to 1, 30 to 1, 30 to 0, 46 to 1, 46 to 0), CompositorShadow.keys) + output.key(29, false) + assertEquals(29 to 0, CompositorShadow.keys.last()) + } + + @Test fun horizontalWheelFinishesBeforeRawMouseDestruction() { + checkWheelTransition(horizontal = true, transition = "releaseEvdevMouse", marker = "destroy") + } + + @Test fun verticalWheelFinishesBeforeRawMouseSoftRelease() { + checkWheelTransition(horizontal = false, transition = "closeEvdevMouse", marker = "release") + } + + private fun checkWheelTransition(horizontal: Boolean, transition: String, marker: String) { + ReflectionHelpers.setField(activity, "mouseWriter", FakeInputWriter(activity.cacheDir.path, FakeInputWriter.MOUSE_SLOT)) + ReflectionHelpers.setField(activity, "evdevMouseActive", true) + val entered = CountDownLatch(1) + val continueWrite = CountDownLatch(1) + val transitioning = CountDownLatch(1) + val finished = CountDownLatch(1) + WriterShadow.beforeWheel = { + entered.countDown() + check(continueWrite.await(5, TimeUnit.SECONDS)) + } + val executor = Executors.newFixedThreadPool(2) + fun scroll() { if (horizontal) output.scrollHorizontal(2) else output.scroll(2) } + try { + val writing = executor.submit { scroll() } + assertTrue(entered.await(5, TimeUnit.SECONDS)) + val closing = executor.submit { + transitioning.countDown() + invoke(transition) + finished.countDown() + } + assertTrue(transitioning.await(5, TimeUnit.SECONDS)) + assertFalse(finished.await(100, TimeUnit.MILLISECONDS)) + continueWrite.countDown() + writing.get(5, TimeUnit.SECONDS) + closing.get(5, TimeUnit.SECONDS) + assertEquals(listOf(if (horizontal) "wheel:0:2:120" else "wheel:-2:0:120", marker), WriterShadow.mouse) + scroll() + assertEquals(2, WriterShadow.mouse.size) + assertEquals(listOf(if (horizontal) Triple(11, 0, 240) else Triple(4, 2, 0)), CompositorShadow.events) + } finally { + continueWrite.countDown() + executor.shutdownNow() + executor.awaitTermination(5, TimeUnit.SECONDS) + WriterShadow.beforeWheel = null + } + } + + private fun refresh() = invoke("refreshInputProfile") + private fun invoke(name: String) = ReflectionHelpers.callInstanceMethod(activity, name) + private fun state(name: String): MutableState = ReflectionHelpers.getField(activity, "$name\$delegate") + + @Implements(value = WaylandCompositor::class, isInAndroidSdk = false) + class CompositorShadow { + companion object { + val pointers = mutableListOf>() + val events = mutableListOf>() + val keys = mutableListOf>() + @JvmStatic @Implementation fun __staticInitializer__() {} + @JvmStatic @Implementation fun nativeSendPointer(action: Int, x: Int, y: Int) { pointers += Triple(action, x, y) } + @JvmStatic @Implementation fun nativeSendSceneInput(type: Int, code: Int, value: Int) { events += Triple(type, code, value) } + @JvmStatic @Implementation fun nativeSendKey(code: Int, value: Int) { keys += code to value } + } + } + + @Implements(value = FakeInputWriter::class, isInAndroidSdk = false) + class WriterShadow { + @Implementation fun writeMouseButton(code: Short, down: Boolean) { mouse += "button:$code:$down" } + @Implementation fun writeMouseDelta(dx: Float, dy: Float) { mouse += "delta:$dx:$dy" } + @Implementation fun writeMouseWheel(vertical: Int, horizontal: Int, value120: Int) { + beforeWheel?.invoke() + mouse += "wheel:$vertical:$horizontal:$value120" + } + @Implementation fun softRelease() { mouse += "release" } + @Implementation fun destroy() { mouse += "destroy" } + companion object { + val mouse: MutableList = Collections.synchronizedList(mutableListOf()) + @Volatile var beforeWheel: (() -> Unit)? = null + @JvmStatic @Implementation fun __staticInitializer__() {} + @JvmStatic @Implementation fun writeDeckControls(index: Int, mask: Int, pads: ShortArray, pressure: ShortArray) {} + } + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/input/SessionSystemInputTest.kt b/app/src/test/java/com/droiddeck/launcher/input/SessionSystemInputTest.kt new file mode 100644 index 0000000000..167f486816 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/input/SessionSystemInputTest.kt @@ -0,0 +1,90 @@ +package com.droiddeck.launcher.input + +import android.os.Handler +import android.os.Looper +import com.droiddeck.launcher.SessionActivity +import java.io.File +import java.time.Duration +import org.junit.After +import org.junit.Assert.* +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.Robolectric +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.Shadows.shadowOf +import org.robolectric.annotation.Config +import org.robolectric.annotation.LooperMode +import org.robolectric.util.ReflectionHelpers + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [34], manifest = Config.NONE, shadows = [JoyConPadTest.RecordingWriter::class]) +@LooperMode(LooperMode.Mode.PAUSED) +class SessionSystemInputTest { + private val bridge = PadBridge(File(RuntimeEnvironment.getApplication().cacheDir, "system-input")) + private val activity = Robolectric.buildActivity(SessionActivity::class.java).get().also { + ReflectionHelpers.setField(it, "padBridge", bridge) + } + private val output = RecordingOutput() + private val router = InputRouter(output).also { + bridge.setInputRouter(it) + bridge.setInputProfile(InputProfile.defaults(InputProfile.GAME) + .withBinding(InputRole.PRIMARY, InputControl.GUIDE, InputAction.Disabled) + .withBinding(InputRole.PRIMARY, InputControl.L3, InputAction.Keys(listOf(29))) + .withBinding(InputRole.PRIMARY, InputControl.R3, InputAction.Command(InputCommand.MENU))) + } + + @After fun close() { bridge.stop(); advance(500) } + + @Test fun steamMenuBypassesPrimaryBindingsAndDisabledRouting() { + bridge.setInputEnabled(false) + invoke("sendSteamGuide") + assertTrue(state().isDown(PadState.GUIDE)) + advance(90) + assertFalse(state().isDown(PadState.GUIDE)) + assertTrue(output.events.isEmpty()) + } + + @Test fun vrStickSwitchBypassesPrimaryBindingsAndPreservesPhysicalHold() { + bridge.applyExternal({ it.press(PadState.L3, true) }, true) + invoke("switchVrSticks") + assertTrue(state().isDown(PadState.L3)) + assertTrue(state().isDown(PadState.R3)) + advance(120) + assertTrue(state().isDown(PadState.L3)) + assertFalse(state().isDown(PadState.R3)) + assertTrue(output.events.isEmpty()) + } + + @Test fun releasingSystemActionsCancelsOldVrReleaseWithoutEndingNewTap() { + invoke("switchVrSticks") + advance(60) + bridge.releaseAll() + assertFalse(state().isDown(PadState.L3)) + assertFalse(state().isDown(PadState.R3)) + invoke("switchVrSticks") + advance(60) + assertTrue(state().isDown(PadState.L3)) + assertTrue(state().isDown(PadState.R3)) + advance(60) + assertFalse(state().isDown(PadState.L3)) + assertFalse(state().isDown(PadState.R3)) + } + + private fun state() = JoyConPadTest.RecordingWriter.pads[0]!! + private fun invoke(name: String) = ReflectionHelpers.callInstanceMethod(activity, name) + private fun advance(ms: Long) { + shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(ms)) + shadowOf(ReflectionHelpers.getStaticField(PadBridge::class.java, "chordHandler").looper).idle() + } + + private class RecordingOutput : InputOutput { + val events = mutableListOf() + override fun key(code: Int, down: Boolean) { events += "key $code $down" } + override fun button(code: Int, down: Boolean) { events += "button $code $down" } + override fun pointer(dx: Float, dy: Float) { events += "pointer" } + override fun scroll(steps: Int) { events += "scroll" } + override fun command(command: InputCommand) { events += command.name } + override fun requestMotion() { events += "motion" } + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/input/SteamControllerBleTest.kt b/app/src/test/java/com/droiddeck/launcher/input/SteamControllerBleTest.kt new file mode 100644 index 0000000000..9d05efea50 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/input/SteamControllerBleTest.kt @@ -0,0 +1,177 @@ +package com.droiddeck.launcher.input + +import android.bluetooth.BluetoothAdapter +import android.bluetooth.BluetoothGatt +import android.bluetooth.BluetoothGattCallback +import android.bluetooth.BluetoothGattCharacteristic +import android.bluetooth.BluetoothGattDescriptor +import android.os.Handler +import org.junit.After +import org.junit.Assert.* +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.Shadows.shadowOf +import org.robolectric.annotation.Config +import org.robolectric.annotation.Implementation +import org.robolectric.annotation.Implements +import org.robolectric.util.ReflectionHelpers +import org.robolectric.shadows.ShadowBluetoothGatt +import java.util.UUID + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [34], manifest = Config.NONE, shadows = [SteamControllerBleTest.Writer::class]) +class SteamControllerBleTest { + private val keys = ArrayList>() + private val router = InputRouter(object : InputOutput { + override fun key(code: Int, down: Boolean) { keys += code to down } + override fun button(code: Int, down: Boolean) {} + override fun pointer(dx: Float, dy: Float) {} + override fun scroll(steps: Int) {} + override fun command(command: InputCommand) {} + override fun requestMotion() {} + }) + private lateinit var controller: SteamControllerBle + private lateinit var handler: Handler + + @Before fun setUp() { + Writer.reports.clear() + Writer.connections.clear() + Writer.features = 0 + router.setProfile(InputProfile.defaults(InputProfile.GAME) + .withBinding(InputRole.PHYSICAL, InputControl.A, InputAction.Keys(listOf(57)))) + controller = SteamControllerBle(RuntimeEnvironment.getApplication(), router) + handler = ReflectionHelpers.getField(controller, "handler") + shadowOf(handler.looper).pause() + controller.start() + } + + @After fun close() { + controller.stop() + handler.looper.quit() + } + + @Test fun stopReleasesAndDetachesInputBeforeQueuedCallbacksCanPublish() { + state(ByteArray(45).apply { this[1] = 1 }) + assertEquals(listOf(57 to true), keys) + handler.post { state(ByteArray(45).apply { this[1] = 1 }) } + controller.stop() + assertEquals(listOf(57 to true, 57 to false), keys) + assertNull(ReflectionHelpers.getField(controller, "routedInput")) + assertTrue(Writer.reports.last().all { it == 0.toByte() }) + val reports = Writer.reports.size + val connections = Writer.connections.toList() + shadowOf(handler.looper).idle() + state(ByteArray(45)) + state(ByteArray(45).apply { this[1] = 1 }) + ReflectionHelpers.callInstanceMethod(controller, "release") + ReflectionHelpers.setField(controller, "featureRead", 7L) + ReflectionHelpers.callInstanceMethod(controller, "onFeatureRead", ReflectionHelpers.ClassParameter.from(ByteArray::class.java, ByteArray(2))) + controller.stop() + assertEquals(reports, Writer.reports.size) + assertEquals(connections, Writer.connections) + assertEquals(0, Writer.features) + assertEquals(listOf(57 to true, 57 to false), keys) + } + + @Test fun stoppingBeforeQueuedStartPreservesAnotherSourcesHeldKey() { + val other = router.channel(InputRole.PRIMARY, InputControl.B, InputAction.Keys(listOf(57))) + other.update(1f) + state(ByteArray(45).apply { this[1] = 1 }) + controller.start() + controller.stop() + shadowOf(handler.looper).idle() + assertFalse(ReflectionHelpers.getField(controller, "running")) + assertEquals(listOf(57 to true), keys) + other.close() + assertEquals(listOf(57 to true, 57 to false), keys) + } + + @Test fun staleControllerCannotClearOrPublishIntoItsReplacementsSlot() { + state(ByteArray(45).apply { this[1] = 1 }) + assertEquals(listOf(57 to true), keys) + val replacement = SteamControllerBle(RuntimeEnvironment.getApplication()) + val replacementHandler = ReflectionHelpers.getField(replacement, "handler") + shadowOf(replacementHandler.looper).pause() + try { + replacement.start() + subscribe(replacement) + val report = ByteArray(45).apply { this[1] = 2 } + state(report, replacement) + val reports = Writer.reports.size + val connections = Writer.connections.toList() + controller.start() + shadowOf(handler.looper).idle() + subscribe(controller) + state(ByteArray(45)) + ReflectionHelpers.setField(controller, "featureRead", 7L) + ReflectionHelpers.callInstanceMethod(controller, "onFeatureRead", + ReflectionHelpers.ClassParameter.from(ByteArray::class.java, ByteArray(2))) + ReflectionHelpers.callInstanceMethod(controller, "disconnect", + ReflectionHelpers.ClassParameter.from(String::class.java, "old link lost")) + controller.stop() + shadowOf(handler.looper).idle() + assertTrue(SteamControllerBle.connected) + assertEquals(connections, Writer.connections) + assertEquals(reports, Writer.reports.size) + assertArrayEquals(report, Writer.reports.last()) + assertEquals(0, Writer.features) + assertEquals(listOf(57 to true, 57 to false), keys) + assertNull(ReflectionHelpers.getField(controller, "routedInput")) + replacement.stop() + assertFalse(SteamControllerBle.connected) + assertFalse(Writer.connections.last()) + assertTrue(Writer.reports.last().all { it == 0.toByte() }) + } finally { + replacement.stop() + replacementHandler.looper.quit() + } + } + + @Test fun stoppingAnUnstartedControllerPreservesTheActiveSlot() { + subscribe(controller) + val unused = SteamControllerBle(RuntimeEnvironment.getApplication()) + try { + val reports = Writer.reports.size + val connections = Writer.connections.toList() + unused.stop() + assertTrue(SteamControllerBle.connected) + assertEquals(connections, Writer.connections) + assertEquals(reports, Writer.reports.size) + } finally { + unused.stop() + ReflectionHelpers.getField(unused, "handler").looper.quit() + } + } + + private fun subscribe(target: SteamControllerBle) { + val device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice("00:11:22:33:44:55") + val gatt = ShadowBluetoothGatt.newInstance(device) + val input = UUID.fromString("100F6C7A-1735-4313-B402-38567131E5F3") + val characteristic = BluetoothGattCharacteristic(input, BluetoothGattCharacteristic.PROPERTY_NOTIFY, 0) + val descriptor = BluetoothGattDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"), 0) + characteristic.addDescriptor(descriptor) + ReflectionHelpers.setField(target, "gatt", gatt) + ReflectionHelpers.setField(target, "input", input) + ReflectionHelpers.setField(target, "reportId", 0x45) + ReflectionHelpers.getField(target, "callback") + .onDescriptorWrite(gatt, descriptor, BluetoothGatt.GATT_SUCCESS) + } + + private fun state(bytes: ByteArray, target: SteamControllerBle = controller) = ReflectionHelpers.callInstanceMethod(target, "onState", + ReflectionHelpers.ClassParameter.from(ByteArray::class.java, bytes)) + + @Implements(FakeInputWriter::class) + class Writer { + companion object { + val reports = ArrayList() + val connections = ArrayList() + var features = 0 + @JvmStatic @Implementation fun writeTritonState(slot: Int, reportId: Int, report: ByteArray) { reports += report.copyOf() } + @JvmStatic @Implementation fun setTritonConnected(slot: Int, connected: Boolean) { connections += connected } + @JvmStatic @Implementation fun writeTritonFeatureReply(slot: Int, request: Long, report: ByteArray?) { features++ } + } + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/input/StickDeadZoneTest.kt b/app/src/test/java/com/droiddeck/launcher/input/StickDeadZoneTest.kt index 38ebeefca9..cfa85a0de0 100644 --- a/app/src/test/java/com/droiddeck/launcher/input/StickDeadZoneTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/input/StickDeadZoneTest.kt @@ -400,22 +400,26 @@ class StickDeadZoneTest { for (id in listOf("ls", "rs")) { motion(device, MotionEvent.AXIS_X to 0.6f, MotionEvent.AXIS_Z to 0.7f) val view = OnScreenControls(context, bridge) + val elements = LegacyGamepadLayout(context, 1200, 800).elements() + view.updateLayout(SavedInputLayout("test", "Test", elements)) + view.measure(android.view.View.MeasureSpec.makeMeasureSpec(1200, android.view.View.MeasureSpec.EXACTLY), android.view.View.MeasureSpec.makeMeasureSpec(800, android.view.View.MeasureSpec.EXACTLY)) view.layout(0, 0, 1200, 800) - val controls = ReflectionHelpers.getField>(view, "controls") - val control = controls.first { ReflectionHelpers.getField(it, "id") == id } - val x = ReflectionHelpers.getField(control, "cx") - val y = ReflectionHelpers.getField(control, "cy") + val control = elements.first { it.control.id == id } + val left = (control.x * 1200).toInt() + val top = (control.y * 800).toInt() + val height = (control.height * 800).toInt() + val x = left + if (id == "ls") (control.width * 1200).toInt() - height / 2f else height / 2f + val y = top + height / 2f val down = MotionEvent.obtain(1000, 1000, MotionEvent.ACTION_DOWN, x, y, 0) - try { assertTrue(view.onTouchEvent(down)) } finally { down.recycle() } - assertFalse(ReflectionHelpers.getField(control, "dirty")) + try { assertTrue(view.dispatchTouchEvent(down)) } finally { down.recycle() } assertEquals(0f, if (id == "ls") pad().leftX else pad().rightX, 0f) assertTrue((if (id == "ls") pad().rightX else pad().leftX) > 0f) bridge.setStickDeadZone(25) assertEquals(0f, if (id == "ls") pad().leftX else pad().rightX, 0f) motion(device, MotionEvent.AXIS_X to 0.6f, MotionEvent.AXIS_Z to 0.7f) - val button = controls.first { ReflectionHelpers.getField(it, "id") == "a" } - val bx = ReflectionHelpers.getField(button, "cx") - val by = ReflectionHelpers.getField(button, "cy") + val button = elements.first { it.control == InputControl.A } + val bx = (button.x + button.width / 2) * 1200 + val by = (button.y + button.height / 2) * 800 fun buttonTouch(action: Int, time: Long) { val event = MotionEvent.obtain(1000, time, action or (1 shl MotionEvent.ACTION_POINTER_INDEX_SHIFT), 2, arrayOf(MotionEvent.PointerProperties().apply { this.id = 0; toolType = MotionEvent.TOOL_TYPE_FINGER }, @@ -423,7 +427,7 @@ class StickDeadZoneTest { arrayOf(MotionEvent.PointerCoords().apply { this.x = x; this.y = y }, MotionEvent.PointerCoords().apply { this.x = bx; this.y = by }), 0, 0, 1f, 1f, 0, 0, InputDevice.SOURCE_TOUCHSCREEN, 0) - try { assertTrue(view.onTouchEvent(event)) } finally { event.recycle() } + try { assertTrue(view.dispatchTouchEvent(event)) } finally { event.recycle() } } buttonTouch(MotionEvent.ACTION_POINTER_DOWN, 1030) assertTrue(pad().isDown(PadState.A)) @@ -435,7 +439,7 @@ class StickDeadZoneTest { assertEquals(0.6f, pad().leftX, 0f) assertEquals(0.7f, pad().rightX, 0f) val up = MotionEvent.obtain(1000, 1100, MotionEvent.ACTION_UP, x, y, 0) - try { assertTrue(view.onTouchEvent(up)) } finally { up.recycle() } + try { assertTrue(view.dispatchTouchEvent(up)) } finally { up.recycle() } } } } diff --git a/app/src/test/java/com/droiddeck/launcher/input/TouchRouterTest.kt b/app/src/test/java/com/droiddeck/launcher/input/TouchRouterTest.kt new file mode 100644 index 0000000000..38b270e090 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/input/TouchRouterTest.kt @@ -0,0 +1,23 @@ +package com.droiddeck.launcher.input + +import com.droiddeck.launcher.session.SessionPrefs +import org.junit.Assert.assertEquals +import org.junit.Test + +class TouchRouterTest { + @Test fun gesturesAndOffAreNotTouchpadRoutes() { + for (mode in listOf(SessionPrefs.TOUCH_GESTURES, SessionPrefs.TOUCH_OFF)) { + assertEquals(TouchRouter.Route.GESTURES.takeIf { mode == SessionPrefs.TOUCH_GESTURES } ?: TouchRouter.Route.OFF, + TouchRouter.decide(TouchRouter.Snapshot(false, mode, false, false, false))) + assertEquals(TouchRouter.Route.GESTURES.takeIf { mode == SessionPrefs.TOUCH_GESTURES } ?: TouchRouter.Route.OFF, + TouchRouter.decide(TouchRouter.Snapshot(false, mode, false, true, true))) + } + } + + @Test fun autoUsesTouchpadOnlyWhenRouterSelectsTouchpad() { + assertEquals(TouchRouter.Route.TOUCHPAD, + TouchRouter.decide(TouchRouter.Snapshot(false, SessionPrefs.TOUCH_AUTO, false, false, false))) + assertEquals(TouchRouter.Route.DIRECT, + TouchRouter.decide(TouchRouter.Snapshot(false, SessionPrefs.TOUCH_AUTO, false, true, false))) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/input/TritonInputRouterTest.kt b/app/src/test/java/com/droiddeck/launcher/input/TritonInputRouterTest.kt new file mode 100644 index 0000000000..a267d6d8ce --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/input/TritonInputRouterTest.kt @@ -0,0 +1,47 @@ +package com.droiddeck.launcher.input + +import org.junit.Assert.* +import org.junit.Test + +class TritonInputRouterTest { + private val events = ArrayList() + private val router = InputRouter(object : InputOutput { + override fun key(code: Int, down: Boolean) { events += "$code $down" } + override fun button(code: Int, down: Boolean) {} + override fun pointer(dx: Float, dy: Float) {} + override fun scroll(steps: Int) {} + override fun command(command: InputCommand) {} + override fun requestMotion() {} + }) + + @Test fun nativeReportIsPassedThroughWithoutCopyingOrReencoding() { + val adapter = TritonInputRouter(router) + val bytes = ByteArray(64) { (it * 11).toByte() } + assertSame(bytes, adapter.route(bytes)) + assertTrue(events.isEmpty()) + adapter.close() + } + + @Test fun consumedButtonDoesNotChangeOtherBitsAxesPressureMotionOrExtension() { + router.setProfile(InputProfile.defaults(InputProfile.GAME).withBinding(InputRole.PHYSICAL, InputControl.A, InputAction.Keys(listOf(57)))) + val adapter = TritonInputRouter(router) + val bytes = ByteArray(64) { (it * 7).toByte() } + bytes[1] = (bytes[1].toInt() or 1).toByte() + val expected = bytes.copyOf().apply { this[1] = (this[1].toInt() and 1.inv()).toByte() } + assertArrayEquals(expected, adapter.route(bytes)) + assertEquals(listOf("57 true"), events) + adapter.release() + assertEquals(listOf("57 true", "57 false"), events) + } + + @Test fun switchingProfileWhileNativeButtonHeldRequiresRelease() { + val adapter = TritonInputRouter(router) + val held = ByteArray(45).apply { this[1] = 1 } + adapter.route(held) + router.setProfile(InputProfile.defaults(InputProfile.GAME).withBinding(InputRole.PHYSICAL, InputControl.A, InputAction.Keys(listOf(57)))) + adapter.route(held) + assertTrue(events.isEmpty()) + adapter.route(ByteArray(45)); adapter.route(held) + assertEquals(listOf("57 true"), events) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/lab/LabOverlayTest.kt b/app/src/test/java/com/droiddeck/launcher/lab/LabOverlayTest.kt new file mode 100644 index 0000000000..ee60999fbc --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/lab/LabOverlayTest.kt @@ -0,0 +1,154 @@ +package com.droiddeck.launcher.lab + +import android.app.Application +import com.droiddeck.launcher.BuildConfig +import org.junit.After +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertNotNull +import org.junit.Assert.assertNull +import org.junit.Assert.assertTrue +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.annotation.Config +import java.io.File + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [33], manifest = Config.NONE) +class LabOverlayTest { + private lateinit var app: Application + private lateinit var root: File + private val injected = mutableListOf() + private val originalInject = LabOverlay.injectDex + private val originalResources = LabOverlay.openResources + + @Before fun prepare() { + app = RuntimeEnvironment.getApplication() + app.applicationInfo.sourceDir = "/data/app/~~test==/com.droiddeck.launcher-test==/base.apk" + root = LabOverlay.root(app).apply { deleteRecursively(); mkdirs() } + LabOverlay.injectDex = { _, apk -> injected += apk } + LabOverlay.openResources = { null } + } + + @After fun restore() { + LabOverlay.injectDex = originalInject + LabOverlay.openResources = originalResources + root.deleteRecursively() + } + + private fun stage(dir: String, commit: String = BuildConfig.SOURCE_COMMIT, apk: String = app.applicationInfo.sourceDir) = + File(root, dir).apply { + mkdirs() + File(this, "overlay.apk").writeText("dex") + File(this, "overlay.json").writeText("""{"label":"feat/x @ abc1234","baseCommit":"$commit","baseApk":"$apk"}""") + } + + @Test fun aBuildForThisInstallIsLoaded() { + stage("active") + LabOverlay.load(app, mainProcess = true) + assertEquals("feat/x @ abc1234", LabOverlay.active?.label) + assertEquals(listOf(File(root, "active/overlay.apk")), injected) + assertEquals(1, LabOverlay.failedStarts(root)) + } + + @Test fun aStagedBuildIsPromotedAtStart() { + stage("next") + File(root, "starts").writeText("2") + LabOverlay.load(app, mainProcess = true) + assertFalse(File(root, "next").exists()) + assertNotNull(LabOverlay.active) + assertEquals(1, LabOverlay.failedStarts(root)) + } + + @Test fun aBuildForAnotherInstallIsTurnedOff() { + stage("active", commit = "0000000") + LabOverlay.load(app, mainProcess = true) + assertNull(LabOverlay.active) + assertTrue(injected.isEmpty()) + assertFalse(File(root, "active").exists()) + assertEquals("OTHER_INSTALL", File(root, "last-off-reason").readText()) + } + + @Test fun aBuildForAnEarlierInstallOfTheSameCommitIsTurnedOff() { + stage("active", apk = "/data/app/old/base.apk") + LabOverlay.load(app, mainProcess = true) + assertNull(LabOverlay.active) + assertEquals("OTHER_INSTALL", File(root, "last-off-reason").readText()) + } + + @Test fun aBuildThatKeepsFailingToStartIsTurnedOff() { + stage("active") + File(root, "starts").writeText("2") + LabOverlay.load(app, mainProcess = true) + assertNull(LabOverlay.active) + assertEquals("DID_NOT_START", File(root, "last-off-reason").readText()) + } + + @Test fun otherProcessesLoadWithoutCountingStarts() { + stage("active") + LabOverlay.load(app, mainProcess = false) + assertNotNull(LabOverlay.active) + assertEquals(0, LabOverlay.failedStarts(root)) + } + + @Test fun noBuildMeansNothingIsLoaded() { + LabOverlay.load(app, mainProcess = true) + assertNull(LabOverlay.active) + assertTrue(injected.isEmpty()) + assertFalse(LabOverlay.promoted) + } + + @Test fun aNewBuildKeepsTheOneItReplaces() { + stage("active") + stage("next") + LabOverlay.load(app, mainProcess = true) + assertTrue(LabOverlay.promoted) + val saved = LabOverlay.saved(app).single() + assertEquals(LabOverlay.Reason.REPLACED, saved.reason) + assertTrue(saved.usable) + } + + @Test fun aRefusedBuildIsKeptButNotUsable() { + stage("active", commit = "0000000") + LabOverlay.load(app, mainProcess = true) + val saved = LabOverlay.saved(app).single() + assertEquals(LabOverlay.Reason.OTHER_INSTALL, saved.reason) + assertFalse(saved.usable) + } + + @Test fun aKeptBuildCanBeStagedAgain() { + stage("active", commit = "0000000") + LabOverlay.load(app, mainProcess = true) + stage("active") + LabOverlay.archive(root, File(root, "active"), LabOverlay.Reason.TURNED_OFF) + val usable = LabOverlay.saved(app).first { it.usable } + LabOverlay.stage(app, usable) + assertNotNull(LabOverlay.pending(app)) + assertEquals(1, LabOverlay.saved(app).size) + LabOverlay.load(app, mainProcess = true) + assertTrue(LabOverlay.promoted) + assertNotNull(LabOverlay.active) + } + + @Test fun stagingAKeptBuildKeepsTheOneThatWasWaiting() { + stage("builds/1") + stage("next") + File(root, "next/overlay.json").writeText("""{"label":"feat/y @ def5678","baseCommit":"${BuildConfig.SOURCE_COMMIT}","baseApk":"${app.applicationInfo.sourceDir}"}""") + LabOverlay.stage(app, LabOverlay.saved(app).single()) + assertEquals("feat/x @ abc1234", LabOverlay.pending(app)?.label) + val kept = LabOverlay.saved(app).single() + assertEquals("feat/y @ def5678", kept.info?.label) + assertEquals(LabOverlay.Reason.REPLACED, kept.reason) + } + + @Test fun onlyTheNewestBuildsAreKept() { + repeat(15) { + stage("active") + LabOverlay.archive(root, File(root, "active"), LabOverlay.Reason.TURNED_OFF) + } + assertEquals(12, LabOverlay.saved(app).size) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/runtime/BwrapSpawnerTest.kt b/app/src/test/java/com/droiddeck/launcher/runtime/BwrapSpawnerTest.kt index a71063bf35..23db657ed5 100644 --- a/app/src/test/java/com/droiddeck/launcher/runtime/BwrapSpawnerTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/runtime/BwrapSpawnerTest.kt @@ -55,6 +55,26 @@ class BwrapSpawnerTest { assertTrue(Bind("/rootfs/etc/droiddeck/proc/stat", "/newproc/stat") in binds) } + @Test fun theSessionsRootfsSettingsStayOutOfTheSandbox() { + val rootfs = "/data/app/linuxfs" + val env = mutableMapOf( + "DDNATV_ROOT" to rootfs, "DDNATV_BINDS" to "/dev:/dev", "HOME" to "/root", + "SPA_PLUGIN_DIR" to "/usr/local/lib/droiddeck-pipewire/lib/spa-0.2", + "LD_PRELOAD" to "/run/droiddeck-preload/libblsession.so:$rootfs/usr/local/lib/libblsession.so:$rootfs/usr/lib/libddnatv-hook.so", + ) + BwrapSpawner.withoutRootfsSettings(env, rootfs) + assertEquals(mapOf("HOME" to "/root", "LD_PRELOAD" to "/run/droiddeck-preload/libblsession.so"), env) + val onlySession = mutableMapOf("LD_PRELOAD" to "$rootfs/usr/lib/libddnatv-hook.so") + BwrapSpawner.withoutRootfsSettings(onlySession, rootfs) + assertTrue(onlySession.isEmpty()) + val wayland = mutableMapOf("SDL_VIDEODRIVER" to "x11", "WAYLAND_DISPLAY" to "wayland-0") + BwrapSpawner.withoutRootfsSettings(wayland, rootfs) + assertEquals(mapOf("WAYLAND_DISPLAY" to "wayland-0"), wayland) + val x11 = mutableMapOf("SDL_VIDEODRIVER" to "x11", "WAYLAND_DISPLAY" to "wayland-0", "DISPLAY" to ":99") + BwrapSpawner.withoutRootfsSettings(x11, rootfs) + assertEquals("x11", x11["SDL_VIDEODRIVER"]) + } + @Test fun normalizeResolvesDotsLexically() { assertEquals("/usr/lib/x", BwrapSpawner.normalize("/usr/lib/GL/../x")) assertEquals("/", BwrapSpawner.normalize("/../..")) diff --git a/app/src/test/java/com/droiddeck/launcher/runtime/ElfSeccompPatchTest.kt b/app/src/test/java/com/droiddeck/launcher/runtime/ElfSeccompPatchTest.kt new file mode 100644 index 0000000000..386017d154 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/runtime/ElfSeccompPatchTest.kt @@ -0,0 +1,53 @@ +package com.droiddeck.launcher.runtime + +import java.nio.ByteBuffer +import java.nio.ByteOrder +import org.junit.Assert.assertEquals +import org.junit.Test + +class ElfSeccompPatchTest { + private fun elfWith(vararg words: Int): ByteArray { + val data = ByteArray(0x200) + val b = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN) + b.put(0, 0x7f); b.put(1, 'E'.code.toByte()); b.put(2, 'L'.code.toByte()); b.put(3, 'F'.code.toByte()) + b.put(4, 2); b.put(5, 1) + b.putShort(18, 183) + b.putLong(32, 64) + b.putShort(54, 56); b.putShort(56, 1) + b.putInt(64, 1); b.putInt(68, 5) + b.putLong(72, 0x100); b.putLong(96, 0x100) + words.forEachIndexed { i, w -> b.putInt(0x100 + i * 4, w) } + return data + } + + @Test + fun theSvcAfterEachBlockedCallIsAnsweredInPlace() { + val svc = 0xd4000001.toInt() + val data = elfWith(0xd2800c68.toInt(), 0xaa0003e1.toInt(), svc, 0xd28024a8.toInt(), svc, 0xd2803668.toInt(), svc) + assertEquals(3, ElfSeccompPatch.patch(data)) + val b = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN) + assertEquals(0xd503201f.toInt(), b.getInt(0x108)) + assertEquals(0x928004a0.toInt(), b.getInt(0x110)) + assertEquals(0x928004a0.toInt(), b.getInt(0x118)) + assertEquals(0, ElfSeccompPatch.patch(data)) + } + + @Test + fun aSecondRunLeavesTheNextCallAlone() { + val svc = 0xd4000001.toInt() + val exit = 0xd2800ba8.toInt() + for (blocked in listOf(0xd2803668.toInt(), 0xd2800c68.toInt())) { + val data = elfWith(blocked, svc, 0xaa1303e0.toInt(), exit, svc) + assertEquals(1, ElfSeccompPatch.patch(data)) + assertEquals(0, ElfSeccompPatch.patch(data)) + assertEquals(svc, ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN).getInt(0x110)) + } + } + + @Test + fun otherSystemCallsAreLeftAlone() { + val svc = 0xd4000001.toInt() + val data = elfWith(0xd2800ba8.toInt(), svc) + assertEquals(0, ElfSeccompPatch.patch(data)) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/runtime/LegacyNamesTest.kt b/app/src/test/java/com/droiddeck/launcher/runtime/LegacyNamesTest.kt index 82487c6a1e..a921259b16 100644 --- a/app/src/test/java/com/droiddeck/launcher/runtime/LegacyNamesTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/runtime/LegacyNamesTest.kt @@ -79,4 +79,18 @@ class LegacyNamesTest { assertEquals("Exec=/usr/local/bin/bannerlator-steam-launch\n", File(root, "usr/local/share/applications/firefox.desktop").readText()) } + + @Test fun catalogAppImageMenusStartThroughTheSharedLauncher() { + write("usr/share/applications/droiddeck-rpcs3.desktop", + "[Desktop Entry]\nName=RPCS3\nExec=env APPIMAGE_EXTRACT_AND_RUN=1 /opt/appimages/rpcs3.AppImage\nCategories=Emulator;\n") + write("usr/share/applications/droiddeck-steam-x64.desktop", "[Desktop Entry]\nExec=droiddeck-steam-x64\n") + write("usr/share/applications/other.desktop", "Exec=env APPIMAGE_EXTRACT_AND_RUN=1 /opt/appimages/other.AppImage\n") + LegacyNames.migrateRootfs(root) + LegacyNames.migrateRootfs(root) + assertEquals("[Desktop Entry]\nName=RPCS3\nExec=/usr/local/bin/droiddeck-appimage-exec /opt/appimages/rpcs3.AppImage\nCategories=Emulator;\n", + File(root, "usr/share/applications/droiddeck-rpcs3.desktop").readText()) + assertEquals("[Desktop Entry]\nExec=droiddeck-steam-x64\n", File(root, "usr/share/applications/droiddeck-steam-x64.desktop").readText()) + assertEquals("Exec=env APPIMAGE_EXTRACT_AND_RUN=1 /opt/appimages/other.AppImage\n", + File(root, "usr/share/applications/other.desktop").readText()) + } } diff --git a/app/src/test/java/com/droiddeck/launcher/runtime/LinuxFexTest.kt b/app/src/test/java/com/droiddeck/launcher/runtime/LinuxFexTest.kt index e63bbcef53..139eb330d8 100644 --- a/app/src/test/java/com/droiddeck/launcher/runtime/LinuxFexTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/runtime/LinuxFexTest.kt @@ -64,4 +64,36 @@ class LinuxFexTest { assertEquals("App-x86_64.AppImage", UserApps.pickAsset(listOf("App-x86_64.AppImage", "App-armhf.AppImage", "notes.txt"))) assertNull(UserApps.pickAsset(listOf("App-armhf.AppImage", "App-i686.AppImage"))) } + + @Test fun aLinkedAssetPicksTheSameVariantInEveryRelease() { + val link = "https://github.com/Djihads80/Eglantine/releases/download/build-129/eglantine-a407703f6d29a5ab73328b4ededb12cf29b2758d-linux-aarch64.AppImage" + val wanted = UserApps.githubAsset(link) + assertEquals("eglantine-a407703f6d29a5ab73328b4ededb12cf29b2758d-linux-aarch64.AppImage", wanted) + assertEquals("Djihads80/Eglantine", UserApps.githubRepo(link)) + val build = listOf("eglantine-a407703f6d29a5ab73328b4ededb12cf29b2758d-linux-aarch64-pgo.AppImage", + "eglantine-a407703f6d29a5ab73328b4ededb12cf29b2758d-linux-aarch64.AppImage") + assertEquals(build[1], UserApps.pickAsset(build, wanted)) + val daily = listOf("eglantine-linux-aarch64-pgo.AppImage", "eglantine-linux-aarch64.AppImage", "eglantine-linux-x86_64.AppImage") + assertEquals("eglantine-linux-aarch64.AppImage", UserApps.pickAsset(daily, wanted)) + assertEquals("eglantine-linux-aarch64-pgo.AppImage", UserApps.pickAsset(daily, "eglantine-0f3c2a91-linux-aarch64-pgo.AppImage")) + assertEquals("eglantine-linux-aarch64-pgo.AppImage", UserApps.pickAsset(daily, "Other-v2.1-x86_64.AppImage")) + assertEquals("eglantine-linux-aarch64-pgo.AppImage", UserApps.pickAsset(daily)) + } + + @Test fun onlyReleaseDownloadLinksNameAnAsset() { + assertNull(UserApps.githubAsset("https://github.com/Djihads80/Eglantine")) + assertNull(UserApps.githubAsset("Djihads80/Eglantine")) + assertNull(UserApps.githubAsset("https://github.com/o/r/releases/download/v1/notes.txt")) + assertEquals("My App-1.0-arm64.AppImage", UserApps.githubAsset("https://github.com/o/r/releases/download/v1.0/My%20App-1.0-arm64.AppImage")) + assertEquals("App-arm64.AppImage", UserApps.githubAsset("https://github.com/o/r/releases/latest/download/App-arm64.AppImage")) + assertEquals("o/r", UserApps.githubRepo("https://github.com/o/r/releases/latest/download/App-arm64.AppImage")) + assertNull(UserApps.githubAsset("https://github.com/o/r/releases/download/v1/bad%ZZ.AppImage")) + } + + @Test fun theLinkedArchitectureWidthIsKept() { + val release = listOf("App-2.0-x86-32.AppImage", "App-2.0-x86_64.AppImage", "App-2.0-i686.AppImage") + assertEquals("App-2.0-x86_64.AppImage", UserApps.pickAsset(release, "App-1.0-x86_64.AppImage")) + assertEquals("App-2.0-x86-32.AppImage", UserApps.pickAsset(release, "App-1.0-x86-32.AppImage")) + assertEquals("App-2.0-amd64.AppImage", UserApps.pickAsset(listOf("App-2.0-i386.AppImage", "App-2.0-amd64.AppImage"), "App-1.0-x86_64.AppImage")) + } } diff --git a/app/src/test/java/com/droiddeck/launcher/runtime/NativeGuestTest.kt b/app/src/test/java/com/droiddeck/launcher/runtime/NativeGuestTest.kt new file mode 100644 index 0000000000..3188eb4e62 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/runtime/NativeGuestTest.kt @@ -0,0 +1,99 @@ +package com.droiddeck.launcher.runtime + +import java.io.File +import java.nio.file.Files +import org.junit.Assert.assertEquals +import org.junit.Assert.assertTrue +import org.junit.Test + +class NativeGuestTest { + private fun rootWithLoader(): File = Files.createTempDirectory("native-root").toFile().also { + File(it, "usr/lib").mkdirs() + File(it, "usr/lib/ld-linux-aarch64.so.1").writeText("") + } + + @Test + fun theSeccompSwitchNamesAHelperAndItsArgumentsInTheRootfs() { + val root = Files.createTempDirectory("rootfs").toFile() + assertEquals(null, NativeGuest.simulator(root)) + File(root, "usr/local/bin").mkdirs() + File(root, "usr/local/bin/seccomp-sim").writeText("") + File(root, "etc/droiddeck").mkdirs() + File(root, "etc/droiddeck/seccomp-sim").writeText("/usr/local/bin/seccomp-sim -f /etc/droiddeck/app.bpf\n") + assertEquals(listOf(File(root, "usr/local/bin/seccomp-sim").path, "-f", File(root, "etc/droiddeck/app.bpf").path), + NativeGuest.simulator(root)) + File(root, "etc/droiddeck/seccomp-sim").writeText("/usr/local/bin/missing") + assertEquals(null, NativeGuest.simulator(root)) + } + + @Test + fun aWorkingDirectoryInAMountIsItsSource() { + val root = Files.createTempDirectory("rootfs").toFile() + val storage = Files.createTempDirectory("storage").toFile() + File(root, "root").mkdirs() + // a mount spec is "src:dst"; Windows' drive letters cannot be written in one + org.junit.Assume.assumeFalse(storage.path.contains(':')) + val binds = listOf("${storage.path}:/root/Storage", "/proc") + assertEquals(storage, NativeGuest.workingDirectory(root, binds, "/root/Storage")) + assertEquals(File(root, "root"), NativeGuest.workingDirectory(root, binds, "/root")) + assertEquals(root, NativeGuest.workingDirectory(root, binds, "/nowhere")) + } + + @Test + fun commandRunsTheProgramThroughTheRootfsLoader() { + val root = rootWithLoader() + val command = NativeGuest.command(root, "/usr/bin/env", listOf("-i", "A=1")) + assertEquals(File(root, "usr/lib/ld-linux-aarch64.so.1").path, command[0]) + assertEquals(listOf("--argv0", "env", File(root, "usr/bin/env").path, "-i", "A=1"), command.drop(1)) + } + + @Test + fun mountsWithoutADestinationMapToThemselves() { + val root = rootWithLoader() + val env = NativeGuest.sessionEnvironment(root, listOf("/dev", "/a:/b")) + assertEquals("/dev:/dev|/a:/b", env["DDNATV_BINDS"]) + assertEquals(root.path, env["DDNATV_ROOT"]) + assertTrue(env.getValue("LD_PRELOAD").replace(File.separatorChar, '/').endsWith("usr/lib/libddnatv-hook.so")) + } + + private val linkRoot = "/data/user/0/app/files/linuxfs" + private val sdBinds = listOf( + "/mnt/media_rw/CARD/files/steam:/mnt/droiddeck-sd", + "/data/user/0/app/files/steam-libraries/x/steamapps/compatdata:/mnt/droiddeck-sd/steamapps/compatdata", + "/storage/emulated/0", + ) + + @Test + fun aDriveLinkToTheCardGetsItsGuestPathBack() { + assertEquals("/mnt/droiddeck-sd", NativeGuest.guestLinkTarget("/mnt/media_rw/CARD/files/steam", linkRoot, sdBinds)) + assertEquals("/mnt/droiddeck-sd/steamapps/compatdata/1", + NativeGuest.guestLinkTarget("/data/user/0/app/files/steam-libraries/x/steamapps/compatdata/1", linkRoot, sdBinds)) + } + + @Test + fun aDriveLinkIntoTheRootfsGetsItsGuestPathBack() { + assertEquals("/", NativeGuest.guestLinkTarget(linkRoot, linkRoot, sdBinds)) + assertEquals("/root/Games", NativeGuest.guestLinkTarget("$linkRoot/root/Games", linkRoot, sdBinds)) + } + + @Test + fun guestPathsStayAsTheyAre() { + assertEquals(null, NativeGuest.guestLinkTarget("/mnt/droiddeck-sd", linkRoot, sdBinds)) + assertEquals(null, NativeGuest.guestLinkTarget("/", linkRoot, sdBinds)) + assertEquals(null, NativeGuest.guestLinkTarget("../drive_c", linkRoot, sdBinds)) + assertEquals(null, NativeGuest.guestLinkTarget("/storage/emulated/0/Games", linkRoot, sdBinds)) + } + + @Test + fun aSiblingOfAMountSourceIsNotPartOfIt() { + assertEquals(null, NativeGuest.guestLinkTarget("/mnt/media_rw/CARD/files/steamx", linkRoot, sdBinds)) + assertEquals(null, NativeGuest.guestLinkTarget("${linkRoot}x/root", linkRoot, sdBinds)) + } + + @Test + fun theFirstOfMountsSharingASourceIsTheGuestPath() { + val binds = sdBinds + "/data/user/0/app/files/steam-libraries/x/steamapps/compatdata:/mnt/bannerlator-sd/steamapps/compatdata" + assertEquals("/mnt/droiddeck-sd/steamapps/compatdata/2", + NativeGuest.guestLinkTarget("/data/user/0/app/files/steam-libraries/x/steamapps/compatdata/2", linkRoot, binds)) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/runtime/ProotFastPathTest.kt b/app/src/test/java/com/droiddeck/launcher/runtime/ProotFastPathTest.kt index 12fb45bf91..f413618942 100644 --- a/app/src/test/java/com/droiddeck/launcher/runtime/ProotFastPathTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/runtime/ProotFastPathTest.kt @@ -33,4 +33,8 @@ class ProotFastPathTest { assertFalse(ProotFastPath.allows(preference = false, noSeccomp = false, source = LinuxRuntime.PROOT_PACK)) assertFalse(ProotFastPath.allows(preference = true, noSeccomp = true, source = LinuxRuntime.PROOT_PACK)) } + + @Test fun fastPathStaysOffWithoutProot() { + assertFalse(ProotFastPath.allows(preference = true, noSeccomp = false, source = LinuxRuntime.PROOT_PACK, nativeUserland = true)) + } } diff --git a/app/src/test/java/com/droiddeck/launcher/runtime/RuntimeRemovalTest.kt b/app/src/test/java/com/droiddeck/launcher/runtime/RuntimeRemovalTest.kt index 83d43250fd..5b803dad88 100644 --- a/app/src/test/java/com/droiddeck/launcher/runtime/RuntimeRemovalTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/runtime/RuntimeRemovalTest.kt @@ -1,5 +1,6 @@ package com.droiddeck.launcher.runtime +import com.droiddeck.launcher.assumeFilePermissionsEnforced import java.io.File import java.nio.file.Files import java.nio.file.LinkOption.NOFOLLOW_LINKS @@ -128,6 +129,7 @@ class RuntimeRemovalTest { } @Test fun aPartialRemovalStaysQuarantinedAndRetryable() { + assumeFilePermissionsEnforced() val root = runtime() try { val removal = LinuxRuntimeInstaller.beginUninstall(null, root)!! @@ -146,6 +148,7 @@ class RuntimeRemovalTest { } @Test fun reportsFailureWhenTheRuntimeCannotBeQuarantinedAndAllowsRetry() { + assumeFilePermissionsEnforced() val root = runtime() Files.setPosixFilePermissions(dir.toPath(), PosixFilePermissions.fromString("r-xr-xr-x")) try { diff --git a/app/src/test/java/com/droiddeck/launcher/runtime/RuntimeUserDataCarryTest.kt b/app/src/test/java/com/droiddeck/launcher/runtime/RuntimeUserDataCarryTest.kt index 15961981c7..cc1a1ad826 100644 --- a/app/src/test/java/com/droiddeck/launcher/runtime/RuntimeUserDataCarryTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/runtime/RuntimeUserDataCarryTest.kt @@ -1,5 +1,6 @@ package com.droiddeck.launcher.runtime +import com.droiddeck.launcher.assumeFilePermissionsEnforced import java.io.File import java.io.IOException import java.nio.file.Files @@ -155,6 +156,7 @@ class RuntimeUserDataCarryTest { } @Test fun anUnreadableAppImageDirectoryAbortsBeforeMovingHome() { + assumeFilePermissionsEnforced() assumeTrue(Files.getFileStore(tmp.root.toPath()).supportsFileAttributeView("posix")) val old = userTree() val images = File(old, "opt/appimages") @@ -264,6 +266,7 @@ class RuntimeUserDataCarryTest { } @Test fun anInaccessibleLiveHomeIsNeverTreatedAsMissing() { + assumeFilePermissionsEnforced() assumeTrue(Files.getFileStore(tmp.root.toPath()).supportsFileAttributeView("posix")) val root = tmp.newFolder("runtime") val staging = tmp.newFolder("runtime.new") diff --git a/app/src/test/java/com/droiddeck/launcher/runtime/WowAddonCatalogTest.kt b/app/src/test/java/com/droiddeck/launcher/runtime/WowAddonCatalogTest.kt new file mode 100644 index 0000000000..43bb29bb48 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/runtime/WowAddonCatalogTest.kt @@ -0,0 +1,199 @@ +package com.droiddeck.launcher.runtime + +import android.app.Application +import org.junit.Assert.* +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.annotation.Config +import java.io.File + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [33], manifest = Config.NONE, application = Application::class) +class WowAddonCatalogTest { + private val context get() = RuntimeEnvironment.getApplication() + + private val files = """[ + {"UID":"1","UICATID":"19","UIVersion":"1.0","UIDate":300,"UIName":"Bartender","UIAuthorName":"Nevcairiel","UIDownloadTotal":"500","UICompatibility":[{"version":"12.0.0","name":"Midnight"}],"UIIMG_Thumbs":["https://cdn.example/a.jpg"]}, + {"UID":"2","UICATID":"20","UIVersion":"2.0","UIDate":100,"UIName":"Zygor","UIAuthorName":"Kappa","UIDownloadTotal":"900","UICompatibility":[{"version":"1.15.7","name":"Classic"},{"version":"4.4.2","name":"Cataclysm Classic"}],"UIIMG_Thumbs":null}, + {"UID":"3","UICATID":"21","UIVersion":"3.0","UIDate":200,"UIName":"Alpha","UIAuthorName":"bartuser","UIDownloadTotal":"10","UIDownloadMonthly":"9","UICompatibility":null}, + {"UID":"4","UICATID":"19","UIVersion":"4.0","UIDate":400,"UIName":"TBCOnly","UIAuthorName":"x","UIDownloadTotal":"20","UIDownloadMonthly":"5","UICompatibility":[{"version":"2.5.4","name":"The Burning Crusade Classic"}]} + ]""" + + private val cats = """[ + {"UICATID":"19","UICATTitle":"Action Bar Mods","UICATParentIDs":["0","23"]}, + {"UICATID":"20","UICATTitle":"Bags","UICATParentIDs":["0","23"]}, + {"UICATID":"21","UICATTitle":"Child","UICATParentIDs":["0","20"]} + ]""" + + private fun c(folder: String, version: String? = null) = WowAddonCatalog.client(folder, version) + + private fun run(q: WowAddonCatalog.Query) = WowAddonCatalog.filterWowi(WowAddonCatalog.parseWowiFiles(files), WowAddonCatalog.parseWowiCategories(cats), q).map { it.id } + + @Test fun parsesFileListEntries() { + val e = WowAddonCatalog.parseWowiFiles(files) + assertEquals(4, e.size) + val a = e[0].addon + assertEquals("Bartender", a.name) + assertEquals("Nevcairiel", a.author) + assertEquals(500L, a.downloads) + assertEquals(300L, a.updated) + assertEquals("https://cdn.example/a.jpg", a.thumb) + assertNull(e[1].addon.thumb) + assertEquals("19", e[0].category) + } + + @Test fun versionsMapToFlavors() { + assertEquals("_retail_", WowAddonCatalog.flavorOf("12.1.0")) + assertEquals("_retail_", WowAddonCatalog.flavorOf("11.0.5")) + assertEquals("_classic_era_", WowAddonCatalog.flavorOf("1.15.7")) + assertEquals("_anniversary_", WowAddonCatalog.flavorOf("2.5.4")) + assertEquals("_classic_", WowAddonCatalog.flavorOf("4.4.2")) + assertNull(WowAddonCatalog.flavorOf("9.2.7")) + } + + @Test fun flavorFilterKeepsNullCompatibilityEverywhere() { + assertEquals(setOf("1", "3"), run(WowAddonCatalog.Query(client = c("_retail_"))).toSet()) + assertEquals(setOf("2", "3"), run(WowAddonCatalog.Query(client = c("_classic_era_"))).toSet()) + assertEquals(setOf("2", "3"), run(WowAddonCatalog.Query(client = c("_classic_"))).toSet()) + assertEquals(setOf("3", "4"), run(WowAddonCatalog.Query(client = c("_anniversary_"))).toSet()) + } + + @Test fun categoryIncludesChildren() { + assertEquals(setOf("2", "3"), run(WowAddonCatalog.Query(category = "20", client = c("_classic_"))).toSet()) + assertEquals(setOf("3"), run(WowAddonCatalog.Query(category = "21", client = c("_classic_"))).toSet()) + assertEquals(setOf("1"), run(WowAddonCatalog.Query(category = "19", client = c("_retail_"))).toSet()) + } + + @Test fun textMatchesNameOrAuthorIgnoringCase() { + assertEquals(setOf("1", "3"), run(WowAddonCatalog.Query(text = "BART", client = c("_retail_"))).toSet()) + assertEquals(listOf("2"), run(WowAddonCatalog.Query(text = "kappa", client = c("_classic_era_")))) + } + + @Test fun sortOrders() { + val f = "_anniversary_" + assertEquals(listOf("3", "4"), run(WowAddonCatalog.Query(client = c(f), sort = WowAddonCatalog.Sort.POPULAR))) + assertEquals(listOf("4", "3"), run(WowAddonCatalog.Query(client = c(f), sort = WowAddonCatalog.Sort.DOWNLOADS))) + assertEquals(listOf("4", "3"), run(WowAddonCatalog.Query(client = c(f), sort = WowAddonCatalog.Sort.UPDATED))) + assertEquals(listOf("3", "4"), run(WowAddonCatalog.Query(client = c(f), sort = WowAddonCatalog.Sort.NAME))) + assertEquals(listOf("2", "1", "4", "3"), WowAddonCatalog.filterWowi( + WowAddonCatalog.parseWowiFiles(files).map { WowAddonCatalog.Entry(it.addon, it.category, null) }, emptyList(), + WowAddonCatalog.Query(client = c(f), sort = WowAddonCatalog.Sort.DOWNLOADS), + ).map { it.id }) + } + + @Test fun pagingReportsMore() { + val all = WowAddonCatalog.parseWowiFiles(files).map { it.addon } + val p0 = WowAddonCatalog.pageOf(all, 0, 3) + assertEquals(3, p0.items.size) + assertTrue(p0.more) + val p1 = WowAddonCatalog.pageOf(all, 1, 3) + assertEquals(1, p1.items.size) + assertFalse(p1.more) + assertTrue(WowAddonCatalog.pageOf(all, 5, 3).items.isEmpty()) + } + + @Test fun clientVersionDecidesTheFamilyBeforeTheFolderName() { + assertEquals("_retail_", c("_forever_").family) + assertEquals("_classic_era_", c("_forever_", "1.15.8.61000").family) + assertEquals("_classic_era_", c("_anniversary_", "1.15.7.60000").family) + assertEquals("_classic_", c("_classic_beta_").family) + assertEquals("_retail_", c("_retail_ptr_").family) + assertNull(c("_mystery_").family) + assertEquals("_anniversary_", c("_mystery_", "2.5.5.1").family) + } + + @Test fun foreverShowsMainlineAddonsAndHidesClassicOnlyOnes() { + assertEquals(setOf("1", "3"), run(WowAddonCatalog.Query(client = c("_forever_"))).toSet()) + assertEquals(setOf("2", "3"), run(WowAddonCatalog.Query(client = c("_forever_", "1.15.8.1"))).toSet()) + } + + @Test fun unknownFamilyShowsEverything() { + assertEquals(setOf("1", "2", "3", "4"), run(WowAddonCatalog.Query(client = c("_mystery_"))).toSet()) + } + + private val buildInfo = """Branch!STRING:0|Active!DEC:1|Build Key!HEX:16|CDN Key!HEX:16|Install Key!HEX:16|IM Size!DEC:4|CDN Path!STRING:0|CDN Hosts!STRING:0|CDN Servers!STRING:0|Tags!STRING:0|Armadillo!STRING:0|Last Activated!STRING:0|Version!STRING:0|KeyRing!HEX:16|Product!STRING:0 +us|1|aa|bb|cc||tpr/wow|h|s|t||1|12.0.7.64000|kk|wow +us|1|aa|bb|cc||tpr/wow|h|s|t||1|1.15.8.63000|kk|wow_classic_era +us|1|aa|bb|cc||tpr/wow|h|s|t||1|12.1.0.65000|kk|wowf +""" + + @Test fun buildInfoAndFlavorInfoGiveTheInstalledVersion() { + assertEquals("12.1.0.65000", WowAddonCatalog.buildInfoVersion(buildInfo, "wowf")) + assertEquals("1.15.8.63000", WowAddonCatalog.buildInfoVersion(buildInfo.replace("\n", "\r\n"), "WOW_CLASSIC_ERA")) + assertNull(WowAddonCatalog.buildInfoVersion(buildInfo, "wow_beta")) + assertNull(WowAddonCatalog.buildInfoVersion("garbage", "wow")) + assertNull(WowAddonCatalog.buildInfoVersion("", "wow")) + assertEquals("wowf", WowAddonCatalog.flavorProduct("Product Flavor!STRING:0\nwowf\n")) + assertNull(WowAddonCatalog.flavorProduct("Product Flavor!STRING:0\n")) + } + + @Test fun clientReadsTheInstallOnDisk() { + val root = File(context.cacheDir, "wowaddon-client-test") + val flavor = File(root, "_forever_") + File(flavor, "Interface/AddOns").mkdirs() + File(root, ".build.info").writeText(buildInfo.replace("12.1.0.65000", "1.15.9.1")) + File(flavor, ".flavor.info").writeText("Product Flavor!STRING:0\nwowf\n") + val target = WowAddons.Target("_forever_", File(flavor, "Interface/AddOns")) + assertEquals("1.15.9.1", WowAddonCatalog.clientVersion(flavor)) + assertEquals("_classic_era_", WowAddonCatalog.client(target).family) + File(flavor, ".flavor.info").delete() + assertNull(WowAddonCatalog.clientVersion(flavor)) + assertEquals("_retail_", WowAddonCatalog.client(target).family) + File(root, ".build.info").delete() + File(flavor, "Interface/AddOns").delete() + File(flavor, "Interface").delete() + flavor.delete() + root.delete() + } + + @Test fun wowiDetailsGiveUrlAndMd5() { + val d = WowAddonCatalog.parseWowiDetails("""[{"UID":"1","UIMD5":"abc","UIDownload":"https://cdn.wowinterface.com/downloads/getfile.php?id=1"}]""") + assertEquals("https://cdn.wowinterface.com/downloads/getfile.php?id=1", d?.first) + assertEquals("abc", d?.second) + assertNull(WowAddonCatalog.parseWowiDetails("""[{"UID":"1","UIDownload":""}]""")) + assertNull(WowAddonCatalog.parseWowiDetails("[]")) + } + + @Test fun nonHttpsDownloadsAreRefused() { + assertFalse(WowAddonCatalog.isHttps("http://example.com/a.zip")) + assertFalse(WowAddonCatalog.isHttps("file:///etc/passwd")) + assertTrue(WowAddonCatalog.isHttps("https://example.com/a.zip")) + } + + @Test fun curseForgeUrlAddsTheSearchText() { + assertEquals("https://www.curseforge.com/wow/search?class=addons", WowAddonCatalog.curseForgeUrl(" ")) + assertEquals("https://www.curseforge.com/wow/search?class=addons&search=deadly+boss+%26+mods", WowAddonCatalog.curseForgeUrl(" deadly boss & mods ")) + } + + @Test fun md5Check() { + val f = File(context.cacheDir, "wowaddon-md5-test.zip") + f.writeText("hello") + assertFalse(WowAddonCatalog.md5Matches(f, "00000000000000000000000000000000")) + assertTrue(WowAddonCatalog.md5Matches(f, "5D41402ABC4B2A76B9719D911017C592")) + assertTrue(WowAddonCatalog.md5Matches(f, null)) + f.delete() + } + + @Test fun aFreshBodyIsReturnedWhenTheCacheCannotBeWritten() { + val blocker = File(context.cacheDir, "wowaddon-cache-blocker").apply { parentFile!!.mkdirs(); writeText("") } + assertEquals("[1]", WowAddonCatalog.cached(File(blocker, "x.json"), 1_000L) { "[1]" }) + blocker.delete() + } + + @Test fun staleCacheIsUsedWhenOfflineAndFreshCacheSkipsTheNetwork() { + val f = File(context.cacheDir, "wowaddon-cache-test/x.json") + assertEquals("[1]", WowAddonCatalog.cached(f, 1_000L) { "[1]" }) + assertEquals("[1]", WowAddonCatalog.cached(f, 2_000L) { "[2]" }) + val later = 1_000L + WowAddonCatalog.FRESH_MS + 1 + assertEquals("[2]", WowAddonCatalog.cached(f, later) { "[2]" }) + assertEquals("[2]", WowAddonCatalog.cached(f, later + WowAddonCatalog.FRESH_MS * 2) { null }) + assertEquals("[2]", WowAddonCatalog.cached(f, later * 4) { "busy" }) + assertEquals("[2]", WowAddonCatalog.cached(f, later * 4) { "{\"error\":\"busy\"}" }) + f.writeText("[2") + assertEquals("[3]", WowAddonCatalog.cached(f, later * 4 + 1) { "[3]" }) + assertEquals(listOf("x.json"), f.parentFile!!.list()!!.toList()) + f.delete() + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/session/BottomNotificationsTest.kt b/app/src/test/java/com/droiddeck/launcher/session/BottomNotificationsTest.kt deleted file mode 100644 index 19046dc27b..0000000000 --- a/app/src/test/java/com/droiddeck/launcher/session/BottomNotificationsTest.kt +++ /dev/null @@ -1,38 +0,0 @@ -package com.droiddeck.launcher.session - -import org.junit.Assert.assertEquals -import org.junit.Assert.assertFalse -import org.junit.Assert.assertTrue -import org.junit.Test - -class BottomNotificationsTest { - private val own = "com.droiddeck.launcher" - - @Test fun messagesFromOtherAppsAreKept() { - assertTrue(BottomNotifications.keep(own, "com.discord", ongoing = false, groupSummary = false, title = "Kurt", text = "ready?")) - } - - @Test fun ownOngoingSummaryAndEmptyNotificationsAreSkipped() { - assertFalse(BottomNotifications.keep(own, own, ongoing = false, groupSummary = false, title = "Session", text = "running")) - assertFalse(BottomNotifications.keep(own, "com.spotify", ongoing = true, groupSummary = false, title = "Song", text = "Artist")) - assertFalse(BottomNotifications.keep(own, "com.discord", ongoing = false, groupSummary = true, title = "2 messages", text = "")) - assertFalse(BottomNotifications.keep(own, "com.discord", ongoing = false, groupSummary = false, title = " ", text = "")) - } - - @Test fun anUpdatedNotificationMovesToTheTopOnce() { - val list = mutableListOf( - BottomNotification("a", "App", "one", "", 1L), - BottomNotification("b", "App", "two", "", 2L), - ) - BottomNotifications.add(list, BottomNotification("b", "App", "two again", "", 3L)) - assertEquals(listOf("b", "a"), list.map { it.key }) - assertEquals("two again", list.first().title) - } - - @Test fun theListKeepsTheNewestThirty() { - val list = mutableListOf() - repeat(35) { BottomNotifications.add(list, BottomNotification("k$it", "App", "t$it", "", it.toLong())) } - assertEquals(30, list.size) - assertEquals("k34", list.first().key) - } -} diff --git a/app/src/test/java/com/droiddeck/launcher/session/EnhancedSessionLogShareTest.kt b/app/src/test/java/com/droiddeck/launcher/session/EnhancedSessionLogShareTest.kt index f9fea7c9b0..05ef3bdd33 100644 --- a/app/src/test/java/com/droiddeck/launcher/session/EnhancedSessionLogShareTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/session/EnhancedSessionLogShareTest.kt @@ -1,5 +1,6 @@ package com.droiddeck.launcher.session +import com.droiddeck.launcher.assumeFilePermissionsEnforced import android.content.Intent import org.json.JSONObject import org.junit.Assert.assertEquals @@ -403,6 +404,7 @@ class EnhancedSessionLogShareTest { } @Test fun unreadableDiscoverySubtreeKeepsOtherLogsAndMarksTheManifestPartial() { + assumeFilePermissionsEnforced() assumeFalse(System.getProperty("os.name").startsWith("Windows")) val session = tmp.newFolder("2026-11-04-12-partial-discovery") session.resolve("session.log").writeText("safe\n") diff --git a/app/src/test/java/com/droiddeck/launcher/session/ForcedPackageTest.kt b/app/src/test/java/com/droiddeck/launcher/session/ForcedPackageTest.kt index 966934ef04..52eaaa585f 100644 --- a/app/src/test/java/com/droiddeck/launcher/session/ForcedPackageTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/session/ForcedPackageTest.kt @@ -21,6 +21,7 @@ import java.io.File @Config(sdk = [28], manifest = Config.NONE) class ForcedPackageTest { private val dxvk2 = "dxvk-2.7.1-linux.wcp" + private val gplasync = "dxvk-gplasync-2.7.1-1-linux.wcp" private lateinit var context: Context @Before fun setUp() { @@ -54,6 +55,74 @@ class ForcedPackageTest { assertTrue(ComponentsManager.FORCED_PACKAGES.none { GpuInfo.Family.A7XX in it.families }) } + @Test fun onlyAdreno6xxPinsDxvkAutoToGplasync() { + val rule = ComponentsManager.autoDxvk(gpu(644, GpuInfo.Family.A6XX))!! + assertEquals(gplasync, rule.file) + assertEquals("Dxvk-gplasync-Linux", rule.release) + assertNull(rule.autoRule()) + assertNull(ComponentsManager.autoDxvk(gpu(740, GpuInfo.Family.A7XX))) + assertNull(ComponentsManager.autoDxvk(gpu(830, GpuInfo.Family.A8XX))) + } + + @Test fun dxvkAutoGivesDroidDeckProtonGplasyncAndOtherProtonsKeepDxvk2() { + proton("dd-proton") + proton("valve") + val fetched = mutableListOf() + val plain = mutableListOf() + val auto = mutableListOf() + installAuto( + gpu(644, GpuInfo.Family.A6XX), + fetch = { fetched += it.file }, + apply = { id, file -> plain += "$id $file"; "" }, + applyAuto = { id, file -> auto += "$id $file"; "" }, + ) + assertEquals(listOf("valve $dxvk2"), plain) + assertEquals(listOf("dd-proton $gplasync"), auto) + assertEquals(listOf(dxvk2, gplasync), fetched) + } + + @Test fun dxvkAutoReplacesTheForcedDxvk2ButKeepsAUserChoice() { + proton("dd-proton") + proton("dd-picked") + proton("valve") + writeState("dd-proton", dxvk2, "experimental-11") + writeState("dd-picked", "dxvk-3.1-linux.wcp", "experimental-11") + writeState("valve", dxvk2, "experimental-11") + val plain = mutableListOf() + val auto = mutableListOf() + installAuto( + gpu(644, GpuInfo.Family.A6XX), + apply = { id, file -> plain += "$id $file"; "" }, + applyAuto = { id, file -> auto += "$id $file"; "" }, + ) + assertTrue(plain.isEmpty()) + assertEquals(listOf("dd-proton $gplasync"), auto) + } + + @Test fun dxvkAutoLeavesGplasyncInPlace() { + proton("dd-proton") + writeState("dd-proton", gplasync, "experimental-11") + assertNull(installAuto(gpu(644, GpuInfo.Family.A6XX))) + } + + @Test fun dxvkManualKeepsDxvk2ForEveryProton() { + proton("dd-proton") + proton("valve") + val plain = mutableListOf() + installAuto( + gpu(644, GpuInfo.Family.A6XX), + apply = { id, file -> plain += "$id $file"; "" }, + dxvkAuto = false, + ) + assertEquals(listOf("dd-proton $dxvk2", "valve $dxvk2"), plain) + } + + @Test fun dxvkAutoLeavesAdreno7xxAnd8xxAlone() { + proton("dd-proton") + assertNull(installAuto(gpu(740, GpuInfo.Family.A7XX))) + assertNull(installAuto(gpu(830, GpuInfo.Family.A8XX))) + } + @Test fun aModelListOnlyMatchesThoseChips() { val rule = ComponentsManager.ForcedPackage( setOf(GpuInfo.Family.A7XX_LOW), "dxvk", dxvk2, "Dxvk-Linux", setOf(710), @@ -517,6 +586,17 @@ class ForcedPackageTest { apply: (String, String) -> String = ::refuseApply, ) = ComponentsManager.ensureForcedPackages(context, gpu, lookup, fetch, apply) + private fun installAuto( + gpu: GpuInfo, + fetch: (ComponentsManager.CatalogItem) -> Unit = {}, + apply: (String, String) -> String = ::refuseApply, + applyAuto: (String, String) -> String = ::refuseApply, + dxvkAuto: Boolean = true, + ) = ComponentsManager.ensureForcedPackages( + context, gpu, { file, release -> item(file, release) }, fetch, apply, + applyAuto = applyAuto, dxvkAuto = dxvkAuto, follows = { it.id.startsWith("dd-") }, + ) + private fun refuseLookup(file: String, release: String): ComponentsManager.CatalogItem? = error("catalog $file $release") private fun refuseFetch(item: ComponentsManager.CatalogItem) { error("fetch ${item.file}") } diff --git a/app/src/test/java/com/droiddeck/launcher/session/GameStorageTest.kt b/app/src/test/java/com/droiddeck/launcher/session/GameStorageTest.kt index 88eb0e0ae2..b5060392a5 100644 --- a/app/src/test/java/com/droiddeck/launcher/session/GameStorageTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/session/GameStorageTest.kt @@ -81,6 +81,28 @@ class GameStorageTest { assertFalse(GameStorage.attached(volume, Environment.MEDIA_UNKNOWN, externalStoragePath = true)) } + @Test fun aRawVolumePathIsCheckedAsItsStorageVolume() { + val asked = mutableListOf() + val mounted = { file: File -> asked += file.path; if (file.path.startsWith("/storage/B2BD-BCFC")) Environment.MEDIA_MOUNTED else Environment.MEDIA_UNKNOWN } + assertTrue(GameStorage.attached(File("/mnt/media_rw/B2BD-BCFC/STEAM GAMES"), mounted)) + assertEquals("/storage/B2BD-BCFC/STEAM GAMES", asked.single()) + assertTrue(GameStorage.attached(File("/storage/B2BD-BCFC/STEAM GAMES"), mounted)) + } + + @Test fun aRawVolumePathOnARemovedCardIsNotAttached() { + assertFalse(GameStorage.attached(File("/mnt/media_rw/B2BD-BCFC/STEAM GAMES")) { Environment.MEDIA_REMOVED }) + assertFalse(GameStorage.attached(File("/mnt/media_rw/B2BD-BCFC/STEAM GAMES")) { Environment.MEDIA_UNKNOWN }) + assertFalse(GameStorage.attached(File("/mnt/media_rw/B2BD-BCFC/STEAM GAMES")) { null }) + } + + @Test fun aSavedFolderThatIsThereIsAttachedWhateverItsVolumeReports() { + val folder = tmp.newFolder("card", "STEAM GAMES") + listOf(Environment.MEDIA_UNKNOWN, Environment.MEDIA_UNMOUNTED, Environment.MEDIA_REMOVED, null).forEach { + assertTrue(GameStorage.attached(folder, it, externalStoragePath = true)) + } + assertTrue(GameStorage.attached(folder) { Environment.MEDIA_UNKNOWN }) + } + @Test fun onlyASavedDriveThatIsMissingCountsAsDisconnected() { SessionPrefs.setGameStorage(app, "", "") assertFalse(GameStorage.disconnected(app)) diff --git a/app/src/test/java/com/droiddeck/launcher/session/PinnedDxvkStatusTest.kt b/app/src/test/java/com/droiddeck/launcher/session/PinnedDxvkStatusTest.kt new file mode 100644 index 0000000000..78e7aa61d3 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/session/PinnedDxvkStatusTest.kt @@ -0,0 +1,37 @@ +package com.droiddeck.launcher.session + +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Test + +class PinnedDxvkStatusTest { + private val pinned = "dxvk-gplasync-2.7.1-1-linux.wcp" + private val base = "dxvk-2.7.1-linux.wcp" + private fun choice(file: String, version: String, auto: Boolean = true) = mapOf("dxvk" to ComponentsManager.Choice(file, auto, version)) + + @Test fun thePinIsHeldOnlyWhenEveryFollowingProtonRunsIt() { + val followers = listOf("a" to "10.0-1", "b" to "10.0-2") + assertTrue(ComponentUpdater.pinnedHeld(pinned, followers, mapOf("a" to choice(pinned, "10.0-1"), "b" to choice(pinned, "10.0-2")), base)) + assertFalse(ComponentUpdater.pinnedHeld(pinned, followers, mapOf("a" to choice(pinned, "10.0-1")), base)) + assertFalse(ComponentUpdater.pinnedHeld(pinned, followers, mapOf("a" to choice(pinned, "10.0-1"), "b" to choice("DD-dxvk-arm64ec-3.1.1.wcp", "10.0-2")), base)) + assertFalse(ComponentUpdater.pinnedHeld(pinned, followers, mapOf("a" to choice(pinned, "9.0-1"), "b" to choice(pinned, "10.0-2")), base)) + } + + @Test fun noFollowingProtonMeansNothingToReport() { + assertTrue(ComponentUpdater.pinnedHeld(pinned, emptyList(), emptyMap(), base)) + } + + @Test fun aDxvkThePlayerPickedIsTheirChoiceNotAFailedInstall() { + val followers = listOf("a" to "10.0-1", "b" to "10.0-2") + val own = choice("DD-dxvk-arm64ec-3.1.1.wcp", "10.0-2", auto = false) + assertTrue(ComponentUpdater.pinnedHeld(pinned, followers, mapOf("a" to choice(pinned, "10.0-1"), "b" to own), base)) + assertTrue(ComponentUpdater.ownChoice(own.getValue("dxvk"), "10.0-2", pinned, base)) + assertFalse(ComponentUpdater.ownChoice(own.getValue("dxvk"), "10.0-3", pinned, base)) + assertFalse(ComponentUpdater.ownChoice(choice("DD-dxvk-arm64ec-3.1.1.wcp", "10.0-2").getValue("dxvk"), "10.0-2", pinned, base)) + } + + @Test fun theForcedDxvkThePinReplacesStillCountsAsNotInstalled() { + val followers = listOf("a" to "10.0-1") + assertFalse(ComponentUpdater.pinnedHeld(pinned, followers, mapOf("a" to choice(base, "10.0-1", auto = false)), base)) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/session/ProtonExtrasTest.kt b/app/src/test/java/com/droiddeck/launcher/session/ProtonExtrasTest.kt index 411268ef69..43571a3ff0 100644 --- a/app/src/test/java/com/droiddeck/launcher/session/ProtonExtrasTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/session/ProtonExtrasTest.kt @@ -44,6 +44,8 @@ class ProtonExtrasTest { private fun installTool(parent: File, name: String) = File(parent, name).apply { mkdirs() File(this, "toolmanifest.vdf").writeText("\"manifest\" { \"commandline\" \"/proton %verb%\" }") + File(this, "compatibilitytool.vdf").writeText("\"from_oslist\" \"windows\"") + File(this, "proton").apply { writeText(""); setExecutable(true) } } @Test @@ -120,6 +122,17 @@ class ProtonExtrasTest { assertEquals(emptyList(), ProtonExtras.installedIn(File(tools, "missing"), tool("ge"))) } + @Test + fun interruptedBuildsAreNotReportedAsInstalled() { + val tools = temp.newFolder("compatibilitytools.d") + installTool(tools, "GE-Proton11-10") + File(tools, "GE-Proton11-11").apply { + mkdirs() + File(this, "toolmanifest.vdf").writeText("\"manifest\" { \"commandline\" \"/proton %verb%\" }") + } + assertEquals(listOf("GE-Proton11-10"), ProtonExtras.installedIn(tools, tool("ge"))) + } + @Test fun anUpdateIsANewerReleaseThanTheNewestInstalledBuild() { assertEquals("DroidDeck-Proton-11-7.6", ProtonExtras.updateFor(listOf("DroidDeck-Proton 11-7.5"), "DroidDeck-Proton-11-7.6")) diff --git a/app/src/test/java/com/droiddeck/launcher/session/SessionDisplayTest.kt b/app/src/test/java/com/droiddeck/launcher/session/SessionDisplayTest.kt index 82512c0b91..7f3eec4092 100644 --- a/app/src/test/java/com/droiddeck/launcher/session/SessionDisplayTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/session/SessionDisplayTest.kt @@ -60,6 +60,13 @@ class SessionDisplayTest { assertEquals(864, SessionDisplay.unfoldedGameHeight(1920, 1080)) } + @Test fun aTabRowUnderTheBarComesOutOfTheGameNotTheControls() { + assertEquals(1376, SessionDisplay.unfoldedGameHeight(2448, 1848, 0)) + assertEquals(1240, SessionDisplay.unfoldedGameHeight(2448, 1848, 136)) + assertEquals(1376, SessionDisplay.unfoldedGameHeight(2448, 1848, -20)) + assertEquals(0, SessionDisplay.unfoldedGameHeight(200, 100, 500)) + } + private fun unfoldedWanted( enabled: Boolean = true, controller: Boolean = false, diff --git a/app/src/test/java/com/droiddeck/launcher/session/SessionLoadingDecisionTest.kt b/app/src/test/java/com/droiddeck/launcher/session/SessionLoadingDecisionTest.kt new file mode 100644 index 0000000000..bdfd2fb5f9 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/session/SessionLoadingDecisionTest.kt @@ -0,0 +1,53 @@ +package com.droiddeck.launcher.session + +import android.content.Intent +import org.junit.After +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNull +import org.junit.Before +import org.junit.Test + +class SessionLoadingDecisionTest { + private var previousSessionId: String? = null + private var previousRelaunch: Intent? = null + + @Before + fun setUp() { + previousSessionId = SessionState.sessionId + previousRelaunch = SessionState.relaunch + SessionState.clearRelaunch() + } + + @After + fun tearDown() { + SessionState.sessionId = previousSessionId + SessionState.relaunch = previousRelaunch + SessionState.clearRelaunch() + if (previousRelaunch != null) SessionState.queueRelaunch(previousRelaunch!!) + } + + @Test + fun loadingTimesOutOnlyWhenNoSessionStarted() { + assertEquals(false, SessionLoadingDecision.shouldReturnToLauncher(100L, 10_099L, false, SessionPhase.IDLE)) + assertEquals(true, SessionLoadingDecision.shouldReturnToLauncher(100L, 10_100L, false, SessionPhase.IDLE)) + assertEquals(false, SessionLoadingDecision.shouldReturnToLauncher(100L, 10_100L, true, SessionPhase.IDLE)) + assertEquals(false, SessionLoadingDecision.shouldReturnToLauncher(100L, 10_100L, false, SessionPhase.PREPARING)) + } + + @Test + fun relaunchRequiresTheCurrentSessionToHaveRequestedTheStop() { + SessionState.sessionId = "session-a" + val next = Intent().apply { action = "next" } + SessionState.queueRelaunch(next) + assertNull(SessionState.takeRelaunch()) + SessionState.queueRelaunch(next) + SessionState.confirmRelaunchStop() + assertEquals(next, SessionState.takeRelaunch()) + assertNull(SessionState.takeRelaunch()) + + SessionState.queueRelaunch(next) + SessionState.confirmRelaunchStop() + SessionState.sessionId = "session-b" + assertNull(SessionState.takeRelaunch()) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/session/SessionLogShareTest.kt b/app/src/test/java/com/droiddeck/launcher/session/SessionLogShareTest.kt index d451abc9de..e3c9f9b5bd 100644 --- a/app/src/test/java/com/droiddeck/launcher/session/SessionLogShareTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/session/SessionLogShareTest.kt @@ -1,5 +1,6 @@ package com.droiddeck.launcher.session +import com.droiddeck.launcher.assumeFilePermissionsEnforced import com.droiddeck.launcher.core.LogRedactor import org.junit.Assert.assertArrayEquals import org.junit.Assert.assertEquals @@ -147,6 +148,7 @@ class SessionLogShareTest { val folder = tmp.newFolder("2026-10-06-06-steam") File(folder, "session.log").writeText("$secret\n") org.junit.Assume.assumeFalse(System.getProperty("os.name").startsWith("Windows")) + assumeFilePermissionsEnforced() folder.setWritable(false) try { SessionArtifacts.scrubAndMark(folder) diff --git a/app/src/test/java/com/droiddeck/launcher/session/SettingsResetTest.kt b/app/src/test/java/com/droiddeck/launcher/session/SettingsResetTest.kt index e95a49608a..4220961071 100644 --- a/app/src/test/java/com/droiddeck/launcher/session/SettingsResetTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/session/SettingsResetTest.kt @@ -4,6 +4,12 @@ import android.content.Context import com.droiddeck.launcher.core.AppUiPrefs import com.droiddeck.launcher.gpu.FrameGen import com.droiddeck.launcher.input.ControllerPrefs +import com.droiddeck.launcher.input.InputAction +import com.droiddeck.launcher.input.InputControl +import com.droiddeck.launcher.input.InputProfile +import com.droiddeck.launcher.input.InputProfiles +import com.droiddeck.launcher.input.InputRole +import com.droiddeck.launcher.input.InputSurfaceLayout import org.junit.Assert.* import org.junit.Before import org.junit.Test @@ -20,7 +26,7 @@ class SettingsResetTest { private val session get() = prefs("session") @Before fun clear() { - listOf("session", "controller", "app-ui", "frame_gen", "steam_touch", "agent").forEach { + listOf("session", "controller", "input-profiles", "input-surfaces", "app-ui", "frame_gen", "steam_touch", "agent").forEach { prefs(it).edit().clear().commit() } } @@ -119,6 +125,39 @@ class SettingsResetTest { assertEquals("a:1,2", prefs("steam_touch").getString("copiedLayout", null)) } + @Test fun inputProfilesAndSurfacesResetWithoutRestoringLegacyMappings() { + val defaultGame = InputProfiles.read(app, InputProfile.GAME) + val defaultDesktop = InputProfiles.read(app, InputProfile.DESKTOP) + val defaultSurface = InputSurfaceLayout.read(app, InputProfile.DESKTOP) + ControllerPrefs.setTarget(app, "a", "b") + InputProfiles.select(app, InputProfile.DESKTOP) + InputProfiles.save(app, InputProfile.defaults(InputProfile.GAME) + .withBinding(InputRole.PHYSICAL, InputControl.A, InputAction.Disabled)) + InputSurfaceLayout.save(app, InputProfile.DESKTOP, emptyList()) + + assertTrue(SettingsReset.resetAll(app, gameProfiles = false)) + + assertEquals(InputProfiles.AUTO, InputProfiles.selection(app)) + assertEquals(defaultGame, InputProfiles.read(app, InputProfile.GAME)) + assertEquals(defaultDesktop, InputProfiles.read(app, InputProfile.DESKTOP)) + assertEquals(defaultSurface, InputSurfaceLayout.read(app, InputProfile.DESKTOP)) + } + + @Test fun inputLayoutsAndQuickButtonVisibilityReset() { + val original = com.droiddeck.launcher.input.InputLibraries.read(app) + val gamepad = original.layouts.first { it.id == com.droiddeck.launcher.input.InputLibraries.OVERLAY } + com.droiddeck.launcher.input.InputLibraries.update(app) { library -> + library.withLayout(gamepad.withQuickButton(com.droiddeck.launcher.input.InputCommand.STEAM, false) + .copy(elements = gamepad.elements.map { it.copy(x = 0f, y = 0f) })).copyLayout(gamepad.id, "Mine", "mine") + } + + assertTrue(SettingsReset.resetAll(app, gameProfiles = false)) + + val reset = com.droiddeck.launcher.input.InputLibraries.read(app) + assertEquals(original.layouts.map { it.id }.toSet(), reset.layouts.map { it.id }.toSet()) + assertEquals(gamepad, reset.layouts.first { it.id == gamepad.id }) + } + @Test fun gameTextureFilteringIsKeptUnlessGameProfilesAreReset() { session.edit().putString("gameTextureFiltering", """{"440":{"anisotropy":16}}""").commit() SettingsReset.resetAll(app, gameProfiles = false) diff --git a/app/src/test/java/com/droiddeck/launcher/storage/LowStorageWarningTest.kt b/app/src/test/java/com/droiddeck/launcher/storage/LowStorageWarningTest.kt new file mode 100644 index 0000000000..5c74114888 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/storage/LowStorageWarningTest.kt @@ -0,0 +1,85 @@ +package com.droiddeck.launcher.storage + +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.setValue +import androidx.compose.ui.test.onNodeWithText +import androidx.compose.ui.test.performClick +import com.droiddeck.launcher.R +import com.droiddeck.launcher.ui.createRobolectricComposeRule +import com.droiddeck.launcher.session.SessionPrefs +import com.droiddeck.launcher.session.SettingsReset +import com.droiddeck.launcher.ui.LowSpaceDialog +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Before +import org.junit.Rule +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.annotation.Config + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [34], qualifiers = "w1200dp-h752dp-land", application = android.app.Application::class) +class LowStorageWarningTest { + @get:Rule val compose = createRobolectricComposeRule() + private val ctx get() = RuntimeEnvironment.getApplication() + private val low = StorageState.LOW_SPACE_BYTES - 1 + private val roomy = StorageState.LOW_SPACE_BYTES + + @Before fun clear() { + ctx.getSharedPreferences("session", 0).edit().clear().commit() + } + + @Test fun warningIsOnByDefault() { + assertTrue(SessionPrefs.lowStorageWarning(ctx)) + } + + @Test fun settingRoundTrips() { + SessionPrefs.setLowStorageWarning(ctx, false) + assertFalse(SessionPrefs.lowStorageWarning(ctx)) + SessionPrefs.setLowStorageWarning(ctx, true) + assertTrue(SessionPrefs.lowStorageWarning(ctx)) + } + + @Test fun resettingSettingsTurnsTheWarningBackOn() { + SessionPrefs.setLowStorageWarning(ctx, false) + assertTrue(SettingsReset.resetAll(ctx, gameProfiles = false)) + assertTrue(SessionPrefs.lowStorageWarning(ctx)) + } + + @Test fun warnsOnlyBelowTheThresholdWhenEnabled() { + assertTrue(StorageState.shouldWarnLowSpace(true, dismissed = false, sessionRunning = false, freeBytes = low)) + assertFalse(StorageState.shouldWarnLowSpace(true, dismissed = false, sessionRunning = false, freeBytes = roomy)) + } + + @Test fun optingOutSuppressesTheWarning() { + assertFalse(StorageState.shouldWarnLowSpace(false, dismissed = false, sessionRunning = false, freeBytes = low)) + } + + @Test fun existingSuppressionsStillApply() { + assertFalse(StorageState.shouldWarnLowSpace(true, dismissed = true, sessionRunning = false, freeBytes = low)) + assertFalse(StorageState.shouldWarnLowSpace(true, dismissed = false, sessionRunning = true, freeBytes = low)) + } + + @Test fun dialogToggleFlipsTheSetting() { + var warn by mutableStateOf(SessionPrefs.lowStorageWarning(ctx)) + compose.setContent { + LowSpaceDialog( + free = low, warn = warn, + onWarnChange = { SessionPrefs.setLowStorageWarning(ctx, it); warn = it }, + onFreeUp = {}, onPlay = {}, onDismiss = {}, + ) + } + val label = ctx.getString(R.string.storage_low_never) + compose.onNodeWithText(label).performClick() + compose.waitForIdle() + assertFalse(SessionPrefs.lowStorageWarning(ctx)) + assertFalse(warn) + compose.onNodeWithText(label).performClick() + compose.waitForIdle() + assertEquals(true, SessionPrefs.lowStorageWarning(ctx)) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/stores/SortCatalogItemsTest.kt b/app/src/test/java/com/droiddeck/launcher/stores/SortCatalogItemsTest.kt index 8a315f3a1a..23a0c16dec 100644 --- a/app/src/test/java/com/droiddeck/launcher/stores/SortCatalogItemsTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/stores/SortCatalogItemsTest.kt @@ -20,6 +20,14 @@ class SortCatalogItemsTest { assertEquals(listOf("Zelda", "Beta", "alpha"), sortCatalogItems(items, "titleDesc").map { it.title }) } + @Test fun anEntryTimeFallsBackWhenTheFirstSourceHasNone() { + assertEquals(30L, entryTime(30L, 20L)) + assertEquals(20L, entryTime(0L, 20L)) + assertEquals(20L, entryTime(null, 20L)) + assertEquals(0L, entryTime(0L, null)) + assertEquals(0L, entryTime(null, 0L)) + } + @Test fun playedAndInstalledComeFirstWithTitlesBreakingTies() { val items = listOf(item("1", "Zelda"), item("2", "alpha"), item("3", "Beta")) val played = mapOf("gog:3" to 900L, "gog:1" to 900L) diff --git a/app/src/test/java/com/droiddeck/launcher/stores/ea/EaTest.kt b/app/src/test/java/com/droiddeck/launcher/stores/ea/EaTest.kt new file mode 100644 index 0000000000..a52485035e --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/stores/ea/EaTest.kt @@ -0,0 +1,323 @@ +package com.droiddeck.launcher.stores.ea + +import com.droiddeck.launcher.stores.Store +import com.droiddeck.launcher.stores.StoreGameSidecar +import com.droiddeck.launcher.stores.StoreLaunch +import org.json.JSONObject +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertNotNull +import org.junit.Assert.assertNull +import org.junit.Assert.assertTrue +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import java.io.File +import java.nio.file.Files +import java.time.LocalDate + +@RunWith(RobolectricTestRunner::class) +class EaTest { + private val wine = EaOoa.Hardware( + boardManufacturer = "Wine", boardSerial = "None", biosManufacturer = "The Wine Project", biosSerial = "0", + osInstallDate = "20240101000000.000000+000", osSerial = "12345-OEM", volumeSerial = "43000000", + gpuPnpId = "PCI\\VEN_5143&DEV_0001&SUBSYS_00000000&REV_00\\0&DEADBEEF&0&DEAD", + cpuVendor = "GenuineIntel", cpuEdx = "BFEBFBFF", cpuEax = "000906EA", cpuEcx = "7ffafbbf", brand = "FEX CPU", hostname = "myhost", + ) + + @Test fun machineHashFollowsTheActivationVersion() { + assertEquals("01804062be939efcd4462c65de292af3a56359b5", EaOoa.machineHash(wine, 4)) + assertEquals("41fa9c614390c5a82534becd338b798197cc5b", EaOoa.machineHash(wine, 3)) + } + + @Test fun launchCodeAndSessionKeyMatchTheProtocol() { + assertEquals(1196115787L, EaCrypto.rtpCode(LocalDate.of(2026, 11, 7))) + assertEquals("f057eb86a0bf8c57537b1aa6b98c6495", EaCrypto.hex(EaCrypto.lsxKey(0x6162))) + assertEquals(EaCrypto.hex(EaCrypto.LSX_DEFAULT_KEY), EaCrypto.hex(EaCrypto.lsxKey(0))) + val answer = EaCrypto.challengeAnswer("abc") + assertEquals("abc", String(EaCrypto.ecbDecrypt(EaCrypto.unhex(answer), EaCrypto.LSX_DEFAULT_KEY))) + assertEquals((answer[0].code shl 8) or answer[1].code, EaCrypto.challengeSeed("3", answer)) + assertNull(EaCrypto.challengeSeed("9", answer)) + } + + @Test fun licenceRoundTripsThroughTheFileFormat() { + val xml = "kh" + + "12349tokgsn" + val l = EaOoa.parseLicense(xml, "c2ln") + assertEquals("tok", l.gameToken) + assertEquals("1234", l.contentId) + val blob = EaOoa.licenseBlob(l, encoded = true) + assertEquals("c2ln", String(blob.copyOfRange(0, 4))) + assertEquals(0, blob[64].toInt()) + val back = String(EaCrypto.licenseDecrypt(blob.copyOfRange(65, blob.size))) + assertTrue(back.contains("tok")) + assertEquals("sig", String(EaOoa.licenseBlob(l, encoded = false).copyOfRange(0, 3))) + } + + @Test fun readsTheVersionOfTheActivationLibrary() { + val pe = ByteArray(64) + val fixed = byteArrayOf(0xBD.toByte(), 0x04, 0xEF.toByte(), 0xFE.toByte(), 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 4, 0, 0, 0, 6, 0) + System.arraycopy(fixed, 0, pe, 10, fixed.size) + assertEquals(Triple(4, 7, 6), EaOoa.productVersion(pe)) + } + + @Test fun manifestNamesTheLauncherAndTouchup() { + val xml = """ + + + + [HKEY_LOCAL_MACHINE\SOFTWARE\EA Games\Game\Install Dir]trial.exetrue + [HKEY_LOCAL_MACHINE\SOFTWARE\EA Games\Game\Install Dir]bin\Game.exe-dx12false + + /__Installer/Touchup.exe-locale {locale} -installPath "{installLocation}" + """ + val m = EaManifest.parse(xml) + assertEquals("1.0.64.43203", m.version) + assertEquals("bin/Game.exe", EaPaths.relative(m.launcher()!!.path)) + assertEquals("-dx12", m.launcher()!!.parameters) + assertEquals("/__Installer/Touchup.exe", m.touchupPath) + val r = EaPaths.registered(m.launcher()!!.path)!! + assertEquals("HKEY_LOCAL_MACHINE\\SOFTWARE\\EA Games\\Game", r.key) + assertEquals("Install Dir", r.value) + val utf16 = byteArrayOf(0xFF.toByte(), 0xFE.toByte()) + "".toByteArray(Charsets.UTF_16LE) + assertEquals("1.0", EaManifest.parse(EaManifest.decode(utf16)).version) + } + + @Test fun findsFilesWhateverTheirCase() { + val dir = Files.createTempDirectory("ea").toFile() + File(dir, "Bin").mkdirs() + File(dir, "Bin/GAME.exe").writeText("x") + assertEquals("Bin/GAME.exe", EaPaths.relativeOnDisk(dir, "bin/game.exe")) + assertNull(EaPaths.relativeOnDisk(dir, "bin/other.exe")) + assertEquals(listOf("-a", "two words", "-c"), EaPaths.splitArgs("-a \"two words\" -c")) + dir.deleteRecursively() + } + + @Test fun lsxRequestsAreParsed() { + val m = EaLsx.parse("")!! + assertEquals("GetSetting", m.name) + assertEquals("4", m.id) + assertEquals("ENVIRONMENT", m.attrs["SettingId"]) + val c = EaLsx.parse("1Game")!! + assertEquals(listOf("ContentId" to "1", "Title" to "Game"), c.children) + assertNull(EaLsx.parse("")) + } + + @Test fun socketOwnerIsReadFromTheProcTable() { + val rows = listOf( + " sl local_address rem_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode", + " 0: 0100007F:C350 0100007F:0C90 01 00000000:00000000 00:00000000 00000000 10234 0 1 1 0", + ) + assertEquals(10234, EaLsx.owner(rows, 0xC350)) + assertNull(EaLsx.owner(rows, 1234)) + assertNull(EaLsx.owner(emptyList(), 0xC350)) + } + + @Test fun rapidConnectionsNeverPassTheClientCap() { + fun session() = EaLsx.Session(java.net.Socket(), java.io.ByteArrayOutputStream(), false) + EaLsx.sessions.clear() + val first = (1..8).map { session().also { s -> assertTrue(EaLsx.admit(s)) } } + first[0].claimed = true + repeat(20) { assertTrue(EaLsx.admit(session())) } + assertEquals(8, EaLsx.sessions.count { !it.evicted }) + assertFalse(first[0].evicted) + EaLsx.sessions.forEach { it.trusted = true } + assertFalse(EaLsx.admit(session())) + EaLsx.sessions.clear() + } + + @Test fun anEvictedSessionCannotBecomeTrusted() { + val s = EaLsx.Session(java.net.Socket(), java.io.ByteArrayOutputStream(), true) + s.evicted = true + EaLsx.promote(s, true) + assertFalse(s.trusted) + val ok = EaLsx.Session(java.net.Socket(), java.io.ByteArrayOutputStream(), true) + EaLsx.promote(ok, true) + assertTrue(ok.trusted) + EaLsx.stop() + } + + @Test fun aPeerAndroidCannotNameIsTrustedOnlyForTheClaimedGame() { + val hidden = EaLsx.Session(java.net.Socket(), java.io.ByteArrayOutputStream(), false, unverifiable = true) + assertFalse(hidden.mayTrust) + hidden.claimed = true + assertTrue(hidden.mayTrust) + val other = EaLsx.Session(java.net.Socket(), java.io.ByteArrayOutputStream(), false) + other.claimed = true + assertFalse(other.mayTrust) + } + + @Test fun aSessionFromAnOlderLaunchCannotBecomeTrusted() { + val old = EaLsx.Session(java.net.Socket(), java.io.ByteArrayOutputStream(), true) + EaLsx.generation++ + EaLsx.promote(old, true) + assertFalse(old.trusted) + EaLsx.stop() + } + + @Test fun lateExitDoesNotStopANewerLaunch() { + val game = EaLsx.Game("Origin.OFR.2", "2", "Two", "1", "en_US", null, 0) + EaLsx.game = game + EaLsx.stop("Origin.OFR.1") + assertEquals(game, EaLsx.game) + EaLsx.stop("Origin.OFR.2") + assertNull(EaLsx.game) + } + + @Test fun redirectCarriesTheCode() { + assertEquals("QUOX", EaAuth.redirectValue("qrc:///html/login_successful.html?code=QUOX&state=1", "code")) + assertEquals("tok", EaAuth.redirectValue("qrc:/html/login_successful.html#access_token=tok&token_type=Bearer", "access_token")) + assertNull(EaAuth.redirectValue("https://signin.ea.com/p/x", "code")) + } + + @Test fun pcSignIsSigned() { + val sign = EaAuth.pcSign("ab12", java.util.Date(0), "v1") + val (payload, signature) = sign.split('.') + val json = JSONObject(String(java.util.Base64.getUrlDecoder().decode(payload))) + assertEquals("v1", json.getString("sv")) + assertEquals("1970-01-01 00:00:00:000", json.getString("ts")) + assertEquals(EaCrypto.base64Url(EaCrypto.hmacSha256("ISa3dpGOc8wW7Adn4auACSQmaccrOyR2".toByteArray(), payload.toByteArray())), signature) + } + + @Test fun libraryKeepsOneEntryPerGame() { + fun owned(id: String, slug: String, base: String = "", type: String = "BASE_GAME", trial: Boolean = false) = + EaApi.OwnedProduct(id, "p$id", "Game $id", slug, base, type, true, trial, emptyList()) + fun offer(id: String, type: String = "FullGame") = EaApi.Offer(id, "c$id", "", type, true, "", "", "", "", "", "1.0", "", emptyList()) + val list = listOf(owned("1", "game"), owned("2", "game-deluxe", base = "game"), owned("3", "other"), owned("4", "trial", trial = true), owned("5", "dlc", type = "EXTRA_CONTENT")) + val picked = EaLibrary.pick(list, list.associate { it.offerId to offer(it.offerId) }) + assertEquals(listOf("1", "3"), picked.map { it.first.offerId }) + } + + @Test fun ownedPageParses() { + val page = JSONObject( + """{"me":{"ownedGameProducts":{"next":"100","items":[{"originOfferId":"Origin.OFR.50.0001","product":{"id":"x","name":"N","downloadable":true,"gameSlug":"titanfall-2", + "baseItem":{"title":"Titanfall 2","gameType":"BASE_GAME","baseGameSlug":null},"gameProductUser":{"ownershipMethods":["PURCHASE"]}}}]}}}""", + ) + val (items, next) = EaApi.parseOwned(page) + assertEquals("100", next) + assertEquals("Titanfall 2", items.single().name) + assertEquals("", items.single().baseSlug) + val offers = EaApi.parseOffers(JSONObject("""{"legacyOffers":[{"offerId":"Origin.OFR.50.0001","contentId":"1035052","displayType":"FullGame","isDownloadable":true, + "downloads":[{"downloadType":"LIVE","gameVersion":"2.0.11.0"}],"softwareLocales":["en_US","fr_FR"],"executeParameters":null}]}""")) + assertEquals("2.0.11.0", offers.single().gameVersion) + assertEquals("", offers.single().executeParameters) + assertNotNull(EaApi.query("getPreloadedOwnedGames")) + } + + @Test fun launcherReadsTheSessionFileOnce() { + val text = StoreLaunch.launcherText(StoreGameSidecar(Store.EA, "Origin.OFR.1", "Game", exe = "bin/Game.exe", args = listOf("-dx12"))) + assertTrue(text.contains("for /f \"usebackq tokens=1,* delims==\" %%a in (\"%~dp0.droiddeck-ea-env\") do set \"%%a=%%b\"")) + assertTrue(text.contains("del \"%~dp0.droiddeck-ea-env\"")) + assertTrue(text.contains("start \"\" \"${StoreLaunch.EA_ORIGIN}\"\r\n\"%~dp0bin\\Game.exe\" -dx12\r\n")) + assertTrue(text.trimEnd().endsWith("taskkill /f /im Origin.exe >nul 2>&1")) + } + + @Test fun environmentCarriesTheSession() { + val account = EaAuth.Account("a", "r", 0, "1001", "2002", "Name", "EaId") + val env = EaLaunch.environment("Origin.OFR.1", "1035052", "fr_FR", "generic", "jwt", "opaque", account, "session") + assertEquals("generic", env["EAGenericAuthToken"]) + assertEquals("jwt", env["EAAccessTokenJWS"]) + assertEquals("3216", env["EALsxPort"]) + assertEquals("opaque", env["EALaunchUserAuthToken"]) + assertEquals("EaId", env["EALaunchEAID"]) + assertEquals("1035052", env["ContentId"]) + assertEquals("fr_FR", env["EAGameLocale"]) + } + + @Test fun activationCoreSitsBesideTheExe() { + val dir = Files.createTempDirectory("ea").toFile() + File(dir, "Binaries/Win32/Core").mkdirs() + File(dir, "Binaries/Win32/Core/EACore.dll").writeText("x") + val core = EaOoa.coreDir(dir, "Binaries/Win32/MassEffect3.exe") + assertEquals(File(dir, "Binaries/Win32/Core").canonicalPath, core?.canonicalPath) + assertTrue(EaOoa.legacyCore(core)) + assertNull(EaOoa.coreDir(dir, "Game.exe")) + File(dir, "Core").mkdirs() + assertEquals(File(dir, "Core").canonicalPath, EaOoa.coreDir(dir, "Game.exe")?.canonicalPath) + dir.deleteRecursively() + } + + @Test fun anIncompleteProbeIsNotHashed() { + val empty = EaOoa.Hardware.fromJson(JSONObject()) + assertTrue(!empty.complete(0) && !empty.complete(4)) + val wmiOnly = EaOoa.Hardware.fromJson(JSONObject().put("boardManufacturer", "Intel Corporation").put("biosManufacturer", "The Wine Project") + .put("osSerial", "00330").put("osInstallDate", "2026").put("hostname", "DROIDDECK")) + assertTrue(wmiOnly.complete(0)) + assertTrue(!wmiOnly.complete(4)) + assertTrue(wine.complete(4)) + assertTrue(wine.complete(2)) + val noInstallDate = EaOoa.Hardware.fromJson(JSONObject().put("boardManufacturer", "Wine").put("biosManufacturer", "Wine").put("osSerial", "1") + .put("volumeSerial", "43000000").put("cpuVendor", "GenuineIntel").put("cpuEdx", "BFEBFBFF").put("cpuEax", "000906EA").put("cpuEcx", "7ffafbbf")) + assertTrue(!noInstallDate.complete(2)) + assertTrue(noInstallDate.complete(3) && noInstallDate.complete(4)) + } + + @Test fun legacyManifestNamesItsContentAndTitle() { + val xml = """7107371183 + RUMass Effect 3 + /__Installer/touchup.exeinstall -locale {locale}""" + val m = EaManifest.parse(xml) + assertEquals(listOf("71073", "71183"), m.contentIds) + assertEquals("Mass Effect 3", m.title("en_US")) + assertEquals("RU", m.title("ru_RU")) + assertEquals("Mass Effect 3", m.title("de_DE")) + assertEquals("/__Installer/touchup.exe", m.touchupPath) + } + + @Test fun aNumericTrialFlagKeepsTheTrialExeOut() { + val xml = """ + [HKEY_LOCAL_MACHINE\SOFTWARE\EA Games\Need for Speed Heat\Install Dir]NeedForSpeedHeatTrial.exe1 + [HKEY_LOCAL_MACHINE\SOFTWARE\EA Games\Need for Speed Heat\Install Dir]NeedForSpeedHeat.exe0 + """ + assertTrue(EaManifest.parse(xml).launcher()!!.path.endsWith("NeedForSpeedHeat.exe")) + } + + @Test fun aSyncRequestedWhileOneRunsIsNotLost() { + val gate = SyncGate() + assertTrue(gate.enter()) + assertTrue(!gate.enter()) + assertTrue(gate.leave()) + assertTrue(!gate.leave()) + assertTrue(gate.enter()) + assertTrue(!gate.leave()) + } + + @Test fun aRequestRacingTheRunnersExitIsPickedUpBySomeone() { + repeat(200) { + val gate = SyncGate() + val runs = java.util.concurrent.atomic.AtomicInteger() + assertTrue(gate.enter()) + val latch = java.util.concurrent.CountDownLatch(1) + val requester = Thread { latch.await(); if (gate.enter()) { runs.incrementAndGet(); while (gate.leave()) runs.incrementAndGet() } } + requester.start() + latch.countDown() + while (gate.leave()) runs.incrementAndGet() + requester.join() + assertTrue("the second request was dropped", runs.get() >= 1) + } + } + + @Test fun aSyncThatSnapshottedBeforeSignOutCannotWriteItsLibrary() { + val app = org.robolectric.RuntimeEnvironment.getApplication() + val game = EaGame("Origin.OFR.1", "1", "Old account game", "old") + val (gen, _) = EaLibrary.snapshot(app) + EaLibrary.reset(app) + assertTrue(!EaLibrary.storeCache(app, listOf(game), gen)) + assertTrue(EaLibrary.cached(app).isEmpty()) + val (fresh, cache) = EaLibrary.snapshot(app) + assertTrue(cache.isEmpty()) + assertTrue(EaLibrary.storeCache(app, listOf(game), fresh)) + assertEquals("Old account game", EaLibrary.cached(app).single().title) + EaLibrary.reset(app) + } + + @Test fun aRefreshFinishingAfterSignOutDoesNotRestoreTheSignIn() { + val app = org.robolectric.RuntimeEnvironment.getApplication() + com.droiddeck.launcher.stores.StoreAccounts.write(app, Store.EA, JSONObject().put("access_token", "old").put("refresh_token", "r").put("user_id", "1")) + assertTrue(EaAuth.storeRefreshed(app, "newer", 3600, "r2")) + assertEquals("newer", EaAuth.load(app)?.accessToken) + EaAuth.signOut(app) + assertTrue(!EaAuth.storeRefreshed(app, "late", 3600, "r3")) + assertNull(EaAuth.load(app)) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/stores/gog/GogCdnTest.kt b/app/src/test/java/com/droiddeck/launcher/stores/gog/GogCdnTest.kt index 324338e17e..424e6a576f 100644 --- a/app/src/test/java/com/droiddeck/launcher/stores/gog/GogCdnTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/stores/gog/GogCdnTest.kt @@ -15,16 +15,12 @@ class GogCdnTest { {"endpoint_name":"lumen","url_format":"https://cdn.gog.com/{path}","parameters":{}} ]}""" - @Test fun allHostsAreParsedInOrderAndTakenInTurn() { + @Test fun allHostsAreParsedInOrder() { val all = GogDownloadManager.parseCdnUrls(answer) assertEquals(3, all.size) assertEquals("https://gog-cdn-fastly.gog.com/token=nva=1~token=t", all[0]) assertEquals("https://gog-cdn-akamai.gog.com/s", all[1]) assertEquals("https://cdn.gog.com", all[2]) - val set = all.joinToString("\n") - assertEquals(all[0], GogDownloadManager.firstCdn(set)) - val picks = (0 until 6).map { GogDownloadManager.pickCdn(set) }.toSet() - assertEquals(all.toSet(), picks) assertEquals(0, GogDownloadManager.parseCdnUrls("not json").size) } } diff --git a/app/src/test/java/com/droiddeck/launcher/stores/gog/GogListingTest.kt b/app/src/test/java/com/droiddeck/launcher/stores/gog/GogListingTest.kt index 5aace971f6..86e54aad06 100644 --- a/app/src/test/java/com/droiddeck/launcher/stores/gog/GogListingTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/stores/gog/GogListingTest.kt @@ -24,6 +24,23 @@ class GogListingTest { assertEquals("", shadowrun.description) } + @Test + fun ownedGamesTheListingLeavesOutStayOwned() { + assertEquals(setOf("1", "2", "1086370078"), GogLibrary.ownedSet(setOf("1", "2"), listOf("2", "1086370078"))) + } + + @Test + fun withoutTheAccountListTheListingStillCounts() { + assertEquals(setOf("1", "2"), GogLibrary.ownedSet(setOf("1", "2"), null)) + assertEquals(setOf("3"), GogLibrary.ownedSet(emptySet(), listOf("3"))) + } + + @Test + fun cachedGamesStayWhenTheAccountListIsUnavailable() { + assertEquals(setOf("1", "2", "1086370078"), GogLibrary.ownedSet(setOf("1", "2"), null, setOf("1086370078"))) + assertEquals(setOf("1", "2", "3"), GogLibrary.ownedSet(setOf("1"), listOf("2", "3"), setOf("1086370078"))) + } + @Test fun moviesOtherPlatformsAndGalaxysOwnEntryAreLeftOut() { assertEquals(listOf("1207660413", "1207664663"), GogLibrary.parseListing(PAGE)!!.games.map { it.gameId }) diff --git a/app/src/test/java/com/droiddeck/launcher/stores/gog/GogMaintenanceTest.kt b/app/src/test/java/com/droiddeck/launcher/stores/gog/GogMaintenanceTest.kt index 1d755a6db0..06c88bfd11 100644 --- a/app/src/test/java/com/droiddeck/launcher/stores/gog/GogMaintenanceTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/stores/gog/GogMaintenanceTest.kt @@ -26,6 +26,45 @@ import java.util.concurrent.atomic.AtomicBoolean class GogMaintenanceTest { @get:Rule val temporary = TemporaryFolder() + @Test fun ownedDlcDepotIsSelected() { + val allowed = GogContent.installProducts("1", JSONArray("[{\"gameIDs\":[\"2\"]}]"), emptyList(), "unused") { setOf("2") } + assertEquals(setOf("1", "2"), allowed) + } + + @Test fun unownedDlcDepotIsSkipped() { + val allowed = GogContent.installProducts("1", JSONArray("[{\"productId\":\"2\"}]"), emptyList(), "unused") { emptySet() } + assertEquals(setOf("1"), allowed) + } + + @Test fun ownershipFetchFailureFallsBackToBaseOnly() { + val allowed = GogContent.installProducts("1", JSONArray("[{\"productId\":\"2\"}]"), emptyList(), "unused") { null } + assertEquals(setOf("1"), allowed) + } + + @Test fun installManifestCredentialsAndTempExecutableAreExtractedBeforeSanitizing() { + val manifest = JSONObject("""{"clientId":"client","clientSecret":"secret","products":[{"temp_executable":"bin/game.exe"}]}""") + assertEquals("client" to "secret", GogContent.clientCredentials(manifest)) + assertEquals("bin/game.exe", GogContent.tempExecutable(GogContent.sanitize(manifest))) + assertFalse(GogContent.sanitize(manifest).toString().contains("secret")) + } + + @Test fun emptyProductIdDepotUsesBaseAndDlcFilesKeepTheirProduct() { + val build = GogBuild("1", "", 2, "", "https://example.invalid/build") + val snapshot = GogContent.snapshot(GogInstallRecord("1", "depot"), build, "unused", ownership = { setOf("2") }, metadata = { url, _ -> + when (url) { + build.link -> JSONObject("{\"depots\":[{\"gameIDs\":[\"1\"],\"manifest\":\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"},{\"gameIDs\":[\"2\"],\"manifest\":\"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"},{\"productId\":\"\",\"manifest\":\"cccccccccccccccccccccccccccccccc\"}]}") + "https://gog-cdn-fastly.gog.com/content-system/v2/meta/aa/aa/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" -> JSONObject("{\"depot\":{\"items\":[{\"path\":\"base.bin\",\"chunks\":[{\"size\":1}]}]}}") + "https://gog-cdn-fastly.gog.com/content-system/v2/meta/bb/bb/bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" -> JSONObject("{\"depot\":{\"items\":[{\"path\":\"dlc.bin\",\"chunks\":[{\"size\":1}]}]}}") + "https://gog-cdn-fastly.gog.com/content-system/v2/meta/cc/cc/cccccccccccccccccccccccccccccccc" -> JSONObject("{\"depot\":{\"items\":[{\"path\":\"empty.bin\",\"chunks\":[{\"size\":1}]}]}}") + else -> error("Unexpected metadata: $url") + } + }) + assertEquals(setOf("1", "2"), snapshot.products.toSet()) + assertEquals("1", snapshot.files.getJSONObject(0).getString("product")) + assertEquals("2", snapshot.files.getJSONObject(1).getString("product")) + assertEquals("1", snapshot.files.getJSONObject(2).getString("product")) + } + @Test fun selectsPublicBranchAndTreatsBuildIdsAsOpaque() { val builds = JSONObject("""{"items":[ {"os":"windows","branch":"beta","build_id":"99","generation":2,"link":"https://example.invalid/beta"}, @@ -213,7 +252,7 @@ class GogMaintenanceTest { fun fixture(name: String) = JSONObject(javaClass.classLoader!!.getResource("gog/$name")!!.readText()) val build = GogBuild("3301", "", 1, "", "https://example.invalid/repository.json") val requested = ArrayList() - val snapshot = GogContent.snapshot(GogInstallRecord("1207658940", "depot"), build, "unused") { url, _ -> + val snapshot = GogContent.snapshot(GogInstallRecord("1207658940", "depot"), build, "unused", metadata = { url, _ -> requested.add(url) when { url == build.link -> fixture("rayman-gen1-repository.json") @@ -221,7 +260,7 @@ class GogMaintenanceTest { url.endsWith("ec88140c-2589-41a0-bbf3-8fc1f215fe5a.json") -> fixture("rayman-gen1-english-excerpt.json") else -> error("Unexpected metadata: $url") } - } + }) assertEquals("3301", snapshot.build) assertEquals(1, snapshot.generation) assertEquals(4, snapshot.files.length()) @@ -269,6 +308,17 @@ class GogMaintenanceTest { assertThrows(IllegalArgumentException::class.java) { GogFiles.inside(folder, "link/game.exe") } } + @Test fun resumedInstallSpaceCountsOnlyFilesStillMissing() { + val folder = temporary.newFolder() + val done = File(folder, "done.bin").apply { writeText("verified") } + val files = JSONArray() + .put(JSONObject().put("path", done.name).put("size", done.length()).put("md5", GogFiles.hash(done))) + .put(JSONObject().put("path", "missing.bin").put("size", 12L).put("md5", "0".repeat(32))) + val missingOnly = GogInstallRecord("1", "depot", files = JSONArray().put(files.getJSONObject(1))) + fun required(target: GogInstallRecord) = GogEstimate.calculate(folder, GogInstallRecord("1", "depot"), target, JSONArray(), reuseChunks = false).required + assertEquals(required(missingOnly), required(GogInstallRecord("1", "depot", files = files))) + } + @Test fun sameSizeCorruptionFailsVerificationAndLanguageVariantsRemainDistinct() { val folder = temporary.newFolder() val file = File(folder, "game.bin").apply { writeText("original") } diff --git a/app/src/test/java/com/droiddeck/launcher/stores/gog/GogResumeSpaceTest.kt b/app/src/test/java/com/droiddeck/launcher/stores/gog/GogResumeSpaceTest.kt deleted file mode 100644 index 4934b58980..0000000000 --- a/app/src/test/java/com/droiddeck/launcher/stores/gog/GogResumeSpaceTest.kt +++ /dev/null @@ -1,43 +0,0 @@ -package com.droiddeck.launcher.stores.gog - -import java.io.File -import java.security.MessageDigest -import org.junit.Assert.assertEquals -import org.junit.Assert.assertFalse -import org.junit.Assert.assertTrue -import org.junit.Test -import org.junit.runner.RunWith -import org.robolectric.RobolectricTestRunner -import org.robolectric.RuntimeEnvironment -import org.robolectric.annotation.Config -import org.robolectric.shadows.ShadowStatFs -import com.droiddeck.launcher.stores.StoreInstallRoot - -@RunWith(RobolectricTestRunner::class) -@Config(sdk = [33]) -class GogResumeSpaceTest { - @Test fun remainingWorkRejectsMeasuredZeroButNotUnknownSpace() { - val root = File(RuntimeEnvironment.getApplication().filesDir, "gog-zero").apply { mkdirs() } - ShadowStatFs.registerStats(root.path, 1024, 0, 0) - val files = listOf(GogDownloadManager.DepotFile("missing").apply { totalSize = 4 }) - val needed = GogDownloadManager.remainingInstallBytes(files, root) - assertEquals(4L, needed) - assertTrue(StoreInstallRoot.insufficientSpace(needed, StoreInstallRoot.availableBytes(root))) - assertFalse(StoreInstallRoot.insufficientSpace(needed, StoreInstallRoot.SPACE_UNKNOWN)) - } - - @Test fun remainingSpaceExcludesVerifiedFilesButIncludesCorruptAndPartialFiles() { - val root = File(RuntimeEnvironment.getApplication().filesDir, "gog-resume").apply { mkdirs() } - val data = byteArrayOf(1, 2, 3, 4) - val hash = MessageDigest.getInstance("MD5").digest(data).joinToString("") { "%02x".format(it) } - val files = listOf("good", "corrupt", "partial", "missing").map { - GogDownloadManager.DepotFile(it).apply { totalSize = 4; md5 = hash } - } - File(root, "good").writeBytes(data) - File(root, "corrupt").writeBytes(ByteArray(4)) - File(root, "partial").writeBytes(byteArrayOf(1)) - assertEquals(12L, GogDownloadManager.remainingInstallBytes(files, root)) - for (file in files) File(root, file.relativePath).writeBytes(data) - assertEquals(0L, GogDownloadManager.remainingInstallBytes(files, root)) - } -} diff --git a/app/src/test/java/com/droiddeck/launcher/stores/ubisoft/UbisoftTest.kt b/app/src/test/java/com/droiddeck/launcher/stores/ubisoft/UbisoftTest.kt new file mode 100644 index 0000000000..58e361db81 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/stores/ubisoft/UbisoftTest.kt @@ -0,0 +1,60 @@ +package com.droiddeck.launcher.stores.ubisoft + +import com.droiddeck.launcher.stores.Store +import com.droiddeck.launcher.stores.StoreGameSidecar +import com.droiddeck.launcher.stores.StoreLaunch +import org.json.JSONArray +import org.json.JSONObject +import org.junit.Assert.assertArrayEquals +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNull +import org.junit.Assert.assertTrue +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [33], application = android.app.Application::class) +class UbisoftTest { + private val steamConfig = "version: 2.0\r\nroot:\r\n name: Assassin's Creed Syndicate\r\n start_game:\r\n steam:\r\n steam_app_id: 368500\r\n" + + " third_party_platform:\r\n name: Steam\r\n override_uplayid: 1875\r\n" + + @Test fun aProductUbisoftDeliversThroughSteamNamesItsSteamApp() { + assertEquals("368500", UbisoftLibrary.steamApp(steamConfig)) + assertNull(UbisoftLibrary.steamApp("root:\n start_game:\n online:\n executables:\n - path:\n relative: bin/game.exe\n")) + } + + @Test fun steamOwnedGamesAreKeyedByTheirSpace() { + val owned = JSONObject().put("games", JSONArray() + .put(JSONObject().put("productId", 1957).put("spaceId", "space-s").put("productType", 0).put("owned", true).put("configuration", steamConfig)) + .put(JSONObject().put("productId", 109).put("spaceId", "space-sc").put("productType", 0).put("owned", true).put("manifest", "AB").put("configuration", "root: {}")) + .put(JSONObject().put("productId", 1961).put("spaceId", "space-addon").put("productType", 1).put("owned", true).put("configuration", steamConfig))) + assertEquals(mapOf("space-s" to "368500"), UbisoftLibrary.steamBySpace(owned)) + assertEquals(setOf("space-sc"), UbisoftLibrary.ownedBySpace(owned).keys) + } + + @Test fun theEntitlementNamesTheGamesApplication() { + val body = JSONObject().put("items", JSONArray() + .put(JSONObject().put("productId", 82).put("applicationId", "app-82")) + .put(JSONObject().put("productId", 109).put("applicationId", "app-109"))).toString() + assertEquals("app-109", UbisoftLaunch.applicationId(body, 109)) + assertNull(UbisoftLaunch.applicationId(body, 1651)) + } + + @Test fun theLauncherStartsTheServiceOnlyWithAFreshLaunchFile() { + val text = StoreLaunch.launcherText(StoreGameSidecar(Store.UBISOFT, "space-sc", "Splinter Cell", "system/SplinterCell.exe")) + val lines = text.trimEnd().split("\r\n") + assertTrue(lines.contains("if not exist \"%~dp0.droiddeck-ubisoft.json\" goto direct")) + assertTrue(lines.contains("\"Z:\\usr\\local\\lib\\droiddeck-ubisoft\\droiddeck-upc.exe\" \"%~dp0.droiddeck-ubisoft.json\"")) + assertEquals("\"%~dp0system\\SplinterCell.exe\"", lines.last()) + assertTrue(lines.indexOf(":direct") < lines.lastIndex) + } + + @Test fun activationPayloadsSurviveTheHexHop() { + val bytes = byteArrayOf(0, 1, 0x7f, -1, -128, 42) + assertEquals("00017fff802a", UbisoftActivation.hex(bytes)) + assertArrayEquals(bytes, UbisoftActivation.unhex("00017fff802a")) + assertNull(UbisoftActivation.unhex("abc")) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/ui/BottomDashboardTest.kt b/app/src/test/java/com/droiddeck/launcher/ui/BottomDashboardTest.kt index 90f39c2c5a..4598643681 100644 --- a/app/src/test/java/com/droiddeck/launcher/ui/BottomDashboardTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/ui/BottomDashboardTest.kt @@ -16,7 +16,7 @@ class BottomDashboardTest { } @Test fun withNothingPickedTheStatsOpen() { - val ids = listOf("telemetry", "downloads", "notifications", "screenshots", "chat") + val ids = listOf("telemetry", "downloads", "screenshots", "chat") assertEquals("telemetry", pickBottomPage(null, ids)) assertEquals("telemetry", pickBottomPage("gone", ids)) assertEquals("chat", pickBottomPage("chat", ids)) diff --git a/app/src/test/java/com/droiddeck/launcher/ui/ComponentsPageProfileTest.kt b/app/src/test/java/com/droiddeck/launcher/ui/ComponentsPageProfileTest.kt index 8d9d59f8d7..90c8d9685a 100644 --- a/app/src/test/java/com/droiddeck/launcher/ui/ComponentsPageProfileTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/ui/ComponentsPageProfileTest.kt @@ -168,8 +168,8 @@ class ComponentsPageProfileTest { compose.onNodeWithText(context.getString(R.string.gpu_system_powervr)).assertIsNotEnabled() compose.onNodeWithText(context.getString(R.string.cmgr_powervr_dxvk_managed)).assertExists() val inheritedChoices = compose.onAllNodesWithText(context.getString(R.string.comp_game_use_general)) - inheritedChoices.assertCountEquals(4) // FEX preset, FEX package, Proton and VKD3D remain selectable. - repeat(4) { inheritedChoices[it].assertIsEnabled() } + inheritedChoices.assertCountEquals(3) // FEX package, Proton and VKD3D remain selectable. + repeat(3) { inheritedChoices[it].assertIsEnabled() } val runtimeOptions = compose.onAllNodesWithText(context.getString(R.string.comp_game_runtime_options)) runtimeOptions.assertCountEquals(2) repeat(2) { runtimeOptions[it].assertIsEnabled() } diff --git a/app/src/test/java/com/droiddeck/launcher/ui/DeckControlsPanelTest.kt b/app/src/test/java/com/droiddeck/launcher/ui/DeckControlsPanelTest.kt deleted file mode 100644 index 45f6566977..0000000000 --- a/app/src/test/java/com/droiddeck/launcher/ui/DeckControlsPanelTest.kt +++ /dev/null @@ -1,59 +0,0 @@ -package com.droiddeck.launcher.ui - -import android.content.ComponentName -import android.view.MotionEvent -import androidx.activity.ComponentActivity -import android.view.View -import com.droiddeck.launcher.input.JoyConPadTest -import com.droiddeck.launcher.input.PadBridge -import com.droiddeck.launcher.session.SessionState -import org.junit.Assert.assertEquals -import org.junit.Assert.assertTrue -import org.junit.Test -import org.junit.runner.RunWith -import org.robolectric.Robolectric -import org.robolectric.RobolectricTestRunner -import org.robolectric.RuntimeEnvironment -import org.robolectric.Shadows.shadowOf -import org.robolectric.annotation.Config -import java.nio.file.Files - -@RunWith(RobolectricTestRunner::class) -@Config(sdk = [34], manifest = Config.NONE, instrumentedPackages = ["com.droiddeck.launcher.input.FakeInputWriter"], - shadows = [JoyConPadTest.RecordingWriter::class]) -class DeckControlsPanelTest { - @Test fun takesTouchAtMatchesWhatATouchDoes() { - val directory = Files.createTempDirectory("deck-panel").toFile() - try { - SessionState.deckPad = false - val app = RuntimeEnvironment.getApplication() - shadowOf(app.packageManager).addActivityIfNotPresent(ComponentName(app, ComponentActivity::class.java)) - val activity = Robolectric.buildActivity(ComponentActivity::class.java).setup().get() - for (pad in listOf(null, PadBridge(directory))) for (portraitDock in listOf(false, true)) { - val name = "pad=${pad != null} portraitDock=$portraitDock" - val panel = DeckControlsPanel(activity, onClose = {}, onSteamMenu = {}, onQam = {}, - pad = pad, portraitDock = portraitDock) - activity.setContentView(panel) - assertEquals(name, pad == null, panel.deckControls) - val (w, h) = if (portraitDock) 720 to 1000 else 1200 to 480 - panel.measure(View.MeasureSpec.makeMeasureSpec(w, View.MeasureSpec.EXACTLY), - View.MeasureSpec.makeMeasureSpec(h, View.MeasureSpec.EXACTLY)) - panel.layout(0, 0, w, h) - var taken = 0 - var missed = 0 - for (x in 0 until w step w / 40) for (y in 0 until h step h / 24) { - val expected = panel.takesTouchAt(x.toFloat(), y.toFloat()) - val down = MotionEvent.obtain(0L, 0L, MotionEvent.ACTION_DOWN, x.toFloat(), y.toFloat(), 0) - assertEquals("$name ($x, $y)", expected, panel.dispatchTouchEvent(down)) - down.recycle() - MotionEvent.obtain(0L, 1L, MotionEvent.ACTION_CANCEL, x.toFloat(), y.toFloat(), 0).also { panel.dispatchTouchEvent(it); it.recycle() } - if (expected) taken++ else missed++ - } - assertTrue("$name: some points land on controls", taken > 0) - assertTrue("$name: some points land between them", missed > 0) - } - } finally { - directory.deleteRecursively() - } - } -} diff --git a/app/src/test/java/com/droiddeck/launcher/ui/GamepadGeometryTest.kt b/app/src/test/java/com/droiddeck/launcher/ui/GamepadGeometryTest.kt index 06f4dedfd7..dbb153bce9 100644 --- a/app/src/test/java/com/droiddeck/launcher/ui/GamepadGeometryTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/ui/GamepadGeometryTest.kt @@ -1,7 +1,8 @@ package com.droiddeck.launcher.ui import com.droiddeck.launcher.session.SessionDisplay -import com.droiddeck.launcher.ui.GamepadGeometry.Slot +import com.droiddeck.launcher.input.GamepadGeometry +import com.droiddeck.launcher.input.GamepadGeometry.Slot import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse import org.junit.Assert.assertTrue diff --git a/app/src/test/java/com/droiddeck/launcher/ui/InputCaptureDialogTest.kt b/app/src/test/java/com/droiddeck/launcher/ui/InputCaptureDialogTest.kt new file mode 100644 index 0000000000..3afeba08bf --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/ui/InputCaptureDialogTest.kt @@ -0,0 +1,84 @@ +package com.droiddeck.launcher.ui + +import android.hardware.input.InputManager +import android.view.InputDevice +import android.view.KeyCharacterMap +import android.view.KeyEvent +import androidx.compose.runtime.mutableStateOf +import androidx.compose.ui.test.* +import com.droiddeck.launcher.R +import com.droiddeck.launcher.input.PadBridgeRoutingTest +import com.droiddeck.launcher.input.PhysicalInputKey +import org.junit.Assert.* +import org.junit.Before +import org.junit.Rule +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.Shadows.shadowOf +import org.robolectric.annotation.Config +import org.robolectric.shadows.InputDeviceBuilder +import org.robolectric.shadows.ShadowDialog + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [34], qualifiers = "w960dp-h640dp-land", shadows = [PadBridgeRoutingTest.RegisteredKeyEvent::class]) +class InputCaptureDialogTest { + @get:Rule val compose = createRobolectricComposeRule() + private val context get() = RuntimeEnvironment.getApplication() + private val open = mutableStateOf(true) + private var selected: PhysicalInputKey? = null + private lateinit var device: InputDevice + + @Before fun setUp() { + device = InputDeviceBuilder.newBuilder().setId(10).setName("Thor buttons") + .setDescriptor("thor-extra").setSources(InputDevice.SOURCE_KEYBOARD) + .setKeyboardType(InputDevice.KEYBOARD_TYPE_ALPHABETIC) + .setKeyCharacterMap(KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD)).build() + shadowOf(context.getSystemService(InputManager::class.java)).addInputDevice(device) + compose.setContent { DroidDeckTheme { + if (open.value) InputCaptureDialog({ open.value = false }) { selected = it; open.value = false } + } } + } + + @Test fun physicalBackCanBeCapturedAndConfirmedWithControllerA() { + send(KeyEvent.KEYCODE_BACK, KeyEvent.ACTION_DOWN) + compose.onNodeWithText(context.getString(R.string.ctrl_edit)).assertIsNotEnabled() + assertTrue(open.value) + send(KeyEvent.KEYCODE_BACK, KeyEvent.ACTION_UP) + compose.onNodeWithText(context.getString(R.string.ctrl_edit)).assertIsEnabled() + assertNull(selected) + send(KeyEvent.KEYCODE_BUTTON_A, KeyEvent.ACTION_DOWN) + assertEquals(KeyEvent.KEYCODE_BACK, selected?.keyCode) + assertFalse(open.value) + } + + @Test fun controllerBCanBeCapturedAndAnotherPressCancelsWithoutSaving() { + send(KeyEvent.KEYCODE_BUTTON_B, KeyEvent.ACTION_DOWN) + send(KeyEvent.KEYCODE_BUTTON_B, KeyEvent.ACTION_UP) + assertTrue(open.value) + send(KeyEvent.KEYCODE_BUTTON_B, KeyEvent.ACTION_DOWN) + assertFalse(open.value) + assertNull(selected) + } + + @Test fun retryAllowsAnotherButtonAndConsumesItsReleaseBeforeConfirmation() { + send(KeyEvent.KEYCODE_F1, KeyEvent.ACTION_DOWN) + send(KeyEvent.KEYCODE_F1, KeyEvent.ACTION_UP) + compose.onNodeWithText(context.getString(R.string.input_capture_retry)).performClick() + send(KeyEvent.KEYCODE_BUTTON_A, KeyEvent.ACTION_DOWN) + send(KeyEvent.KEYCODE_BUTTON_A, KeyEvent.ACTION_UP) + assertNull(selected) + assertTrue(open.value) + send(KeyEvent.KEYCODE_BUTTON_A, KeyEvent.ACTION_DOWN) + assertEquals(KeyEvent.KEYCODE_BUTTON_A, selected?.keyCode) + } + + private fun send(code: Int, action: Int) { + compose.runOnIdle { + assertTrue(ShadowDialog.getLatestDialog().window!!.callback.dispatchKeyEvent( + KeyEvent(100L, 100L, action, code, 0, 0, device.id, 700, 0, InputDevice.SOURCE_KEYBOARD))) + } + compose.waitForIdle() + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/ui/InputSettingsTest.kt b/app/src/test/java/com/droiddeck/launcher/ui/InputSettingsTest.kt new file mode 100644 index 0000000000..8def5601a4 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/ui/InputSettingsTest.kt @@ -0,0 +1,555 @@ +package com.droiddeck.launcher.ui + +import android.content.Context +import android.app.Presentation +import android.hardware.display.DisplayManager +import android.view.Display +import androidx.compose.runtime.mutableStateOf +import androidx.compose.foundation.verticalScroll +import androidx.compose.ui.test.* +import com.droiddeck.launcher.input.* +import com.droiddeck.launcher.R +import com.droiddeck.launcher.session.SessionState +import com.droiddeck.launcher.session.SessionService +import org.junit.Assert.* +import org.junit.Before +import org.junit.Rule +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.Shadows.shadowOf +import org.robolectric.shadows.ShadowDialog +import org.robolectric.shadows.ShadowDisplayManager +import org.robolectric.util.ReflectionHelpers +import org.robolectric.annotation.Config +import org.robolectric.annotation.GraphicsMode + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [28], qualifiers = "w1000dp-h900dp") +@GraphicsMode(GraphicsMode.Mode.NATIVE) +class InputSettingsTest { + @get:Rule val compose = createRobolectricComposeRule() + private val context get() = RuntimeEnvironment.getApplication() + @Before fun reset() { + context.getSharedPreferences("input-profiles", Context.MODE_PRIVATE).edit().clear().commit() + context.getSharedPreferences("input-surfaces", Context.MODE_PRIVATE).edit().clear().commit() + context.getSharedPreferences("controller", Context.MODE_PRIVATE).edit().clear().commit() + } + + @Test fun commandSymbolsAreSelectableForAnyCustomButton() { + val element = mutableStateOf(InputSurfaceElement(InputControl.A, .1f, .1f, .2f, .2f, name = "Custom button")) + compose.setContent { DroidDeckTheme { + androidx.compose.foundation.layout.Column { InputElementProperties(element.value) { element.value = it } } + } } + var current = context.getString(R.string.input_button_text) + for (code in listOf(InputButtonIcons.MENU, InputButtonIcons.KEYBOARD, InputButtonIcons.STEAM, InputButtonIcons.QUICK_ACCESS)) { + compose.onNodeWithText(current).performClick() + current = InputButtonIcons.label(context, code) + compose.onNodeWithText(current).performScrollTo().performClick() + compose.runOnIdle { assertEquals(code, element.value.icon) } + } + } + + @Test fun steamAndDesktopQuickSettingsOpenTheSameInputLibrary() { + val mode = mutableStateOf(SessionService.MODE_STEAM) + var opened = 0 + compose.setContent { DroidDeckTheme { + ModeSheetRows(ModeSettings(mode = mode.value, resolution = "auto", hdr = false, hdrReason = null, + touchMode = "auto", suspendPolicy = "auto", oscMode = "auto", directAudio = null, mic = null), + ModeSettingsActions(onResolution = {}, onHdr = {}, onTouch = {}, onSuspendPolicy = {}, onOsc = {}, + onInputSettings = { opened++ }, onDirectAudio = {}, onMic = {}, onDismiss = {}), SheetTab.CONTROLS) + } } + compose.onNodeWithTag("input-profile-expand").performClick() + compose.onNodeWithText(context.getString(R.string.ctrl_edit)).performClick() + compose.runOnIdle { assertEquals(1, opened); mode.value = SessionService.MODE_DESKTOP } + compose.onNodeWithText(context.getString(R.string.ctrl_edit)).performClick() + compose.runOnIdle { assertEquals(2, opened) } + } + + @Test fun quickMenuCombinesPartsWithoutChangingTheSavedProfile() { + val original = InputLibraries.read(context) + compose.setContent { DroidDeckTheme { + androidx.compose.foundation.layout.Column { InputQuickSettings(rememberMenuHost(), desktop = true) } + } } + compose.onNodeWithTag("setting-input-profile").assertExists() + compose.onNodeWithTag("setting-input-part-PHYSICAL").assertDoesNotExist() + compose.onNodeWithTag("input-profile-expand").performClick() + compose.onNodeWithTag("setting-input-part-PHYSICAL").assertExists() + compose.onNodeWithTag("setting-input-part-PRIMARY").assertExists() + compose.onNodeWithTag("setting-input-part-SECONDARY").performClick() + compose.onNodeWithText("Deck grips + trackpads").performScrollTo().performClick() + compose.runOnIdle { + val changed = InputLibraries.read(context) + assertEquals(InputStockLayouts.DECK, changed.activePart(true, InputRole.SECONDARY).bindings) + assertEquals(original.profiles, changed.profiles) + assertEquals(original.compileActive(false), changed.compileActive(false)) + } + compose.onNodeWithText(context.getString(R.string.input_save_combination)).performClick() + compose.onNode(hasSetTextAction()).performTextInput("Reading") + compose.onNodeWithText(context.getString(R.string.gesture_save_changes)).performClick() + compose.runOnIdle { + val saved = InputLibraries.read(context) + assertEquals("Reading", saved.profile(saved.activeId(true)).name) + assertTrue(saved.selection(true).parts.isEmpty()) + assertEquals(original.profile("desktop"), saved.profile("desktop")) + } + } + + @Test fun actionDraftSurvivesRouteAndTypeChanges() { + var saved: InputAction? = null + compose.setContent { DroidDeckTheme { InputActionEditor(InputControl.RS, InputAction.Scroll(27f, true), "Right stick", {}, { saved = it }) } } + compose.onNodeWithText(context.getString(R.string.input_route_controller), substring = false).performClick() + compose.onNodeWithText(context.getString(R.string.input_disabled), substring = false).performClick() + compose.onNodeWithText(context.getString(R.string.input_route_local), substring = false).performClick() + compose.onNodeWithText(context.getString(R.string.input_scroll_speed, 27)).assertExists() + compose.onNodeWithTag("setting-action-type").performClick() + compose.onNodeWithText(context.getString(R.string.input_mouse_click)).performClick() + compose.onNodeWithTag("setting-action-type").performClick() + compose.onNodeWithText(context.getString(R.string.input_scroll)).performClick() + compose.onNodeWithTag("input-action-save").performClick() + compose.runOnIdle { assertEquals(InputAction.Scroll(27f, true), saved) } + } + + @Test fun bindingEditorPersistsDisabledWithoutChangingTouchControls() { + var changed = false + compose.setContent { DroidDeckTheme { InputSettingsPage({}, { changed = true }) } } + compose.onNodeWithTag("input-profile-game").performScrollTo().performClick() + compose.onNodeWithTag("input-part-physical").performScrollTo().performClick() + compose.onNodeWithTag("input-binding-a").performScrollTo().performClick() + compose.onNodeWithText("Disabled", substring = false).performScrollTo().performClick() + compose.runOnIdle { assertFalse(changed) } + compose.onNodeWithTag("input-action-save").performClick() + compose.runOnIdle { + assertTrue(changed) + val profile = InputProfiles.read(context, InputProfile.GAME) + assertEquals(InputAction.Disabled, profile.action(InputRole.PHYSICAL, InputControl.A)) + assertEquals(InputAction.Native(), profile.action(InputRole.SECONDARY, InputControl.A)) + } + } + + @Test fun addingCreatesAnUnassignedElementAtTheCanvasCenterWithoutAnotherTouch() { + var changed = false + val library = InputLibraries.read(context) + val layout = library.layout(InputProfile.GAME, InputRole.SECONDARY)!! + compose.setContent { DroidDeckTheme { InputSurfaceDialog(InputProfile.GAME, {}, { changed = true }, + role = InputRole.PRIMARY, layoutId = layout.id, bindingsId = library.profile(InputProfile.GAME).binding(InputRole.SECONDARY)) } } + compose.onNodeWithTag("input-editor-tools").performClick() + compose.onNodeWithText(context.getString(R.string.input_editor_add)).performClick() + compose.onNodeWithText(context.getString(R.string.input_element_button), substring = false).performClick() + compose.onNodeWithTag("input-editor-tools").performClick() + compose.onNodeWithText(context.getString(R.string.input_properties)).assertExists() + compose.onNodeWithText(context.getString(R.string.input_editor_done)).performClick() + compose.runOnIdle { + assertTrue(changed) + val updated = InputLibraries.read(context) + val added = updated.layout(InputProfile.GAME, InputRole.SECONDARY)!!.elements.single { e -> layout.elements.none { it.id == e.id } } + assertEquals(InputAction.Disabled, updated.compile(InputProfile.GAME).elementAction(InputRole.SECONDARY, added.id, added.control)) + assertEquals(.5f, added.x + added.width / 2, .01f) + } + } + + @Test fun aNewProfileIsIndependentAndDoesNotBecomeActive() { + compose.setContent { DroidDeckTheme { InputSettingsPage({}) } } + compose.onNodeWithText(context.getString(R.string.input_new_profile)).performScrollTo().performClick() + compose.onNodeWithTag("input-name").performTextInput("Minecraft") + compose.onNodeWithText(context.getString(R.string.gesture_save_changes)).performClick() + compose.runOnIdle { + val library = InputLibraries.read(context) + val created = library.profiles.single { it.name == "Minecraft" } + for (role in InputRole.entries) assertNotEquals(library.profile(InputProfile.GAME).binding(role), created.binding(role)) + assertNotEquals(library.layout(InputProfile.GAME, InputRole.SECONDARY)!!.id, library.layout(created.id, InputRole.SECONDARY)!!.id) + assertEquals(InputProfiles.AUTO, library.selected) + } + } + + @Test fun openingAProfileDoesNotActivateItAndBackKeepsItsContext() { + compose.setContent { DroidDeckTheme { InputSettingsPage({}) } } + compose.onNodeWithTag("input-profile-desktop").performScrollTo().performClick() + compose.onNodeWithTag("input-part-physical").performScrollTo().performClick() + compose.onNodeWithTag("input-binding-a").performScrollTo().performClick() + compose.onNodeWithTag("input-action-save").performClick() + compose.onNodeWithTag("input-binding-a").assertExists() + compose.onNodeWithText(context.getString(R.string.mode_back)).performClick() + compose.onNodeWithTag("input-part-secondary").assertExists() + compose.runOnIdle { assertEquals(InputProfiles.AUTO, InputLibraries.read(context).selected) } + } + + @Test fun profileCreationCanExplicitlyShareOnlyPhysicalControls() { + compose.setContent { DroidDeckTheme { InputSettingsPage({}) } } + compose.onNodeWithText(context.getString(R.string.input_new_profile)).performScrollTo().performClick() + compose.onNodeWithTag("input-name").performTextInput("Shared controls") + compose.onNodeWithTag("input-share-physical").performClick() + compose.onNodeWithText(context.getString(R.string.gesture_save_changes)).performClick() + compose.runOnIdle { + val library = InputLibraries.read(context) + val created = library.profiles.single { it.name == "Shared controls" } + assertEquals(library.profile("game").binding(InputRole.PHYSICAL), created.binding(InputRole.PHYSICAL)) + assertNotEquals(library.profile("game").binding(InputRole.SECONDARY), created.binding(InputRole.SECONDARY)) + } + } + + @Test fun hidingEditorToolsPreservesPlacedElements() { + val library = InputLibraries.read(context) + val layout = library.layout("game", InputRole.SECONDARY)!! + compose.setContent { DroidDeckTheme { InputSurfaceDialog("game", {}, {}, role = InputRole.PRIMARY, + layoutId = layout.id, bindingsId = library.profile("game").binding(InputRole.SECONDARY)) } } + compose.onNodeWithTag("input-editor-tools").performClick() + compose.onNodeWithText(context.getString(R.string.input_editor_add)).performClick() + compose.onNodeWithText(context.getString(R.string.input_element_button)).performClick() + compose.onNodeWithTag("input-layout-canvas").performTouchInput { click(center) } + compose.onNodeWithTag("input-editor-tools").performClick() + compose.onNodeWithText(context.getString(R.string.input_hide_tools)).performClick() + compose.onNodeWithText(context.getString(R.string.input_editor_add)).assertDoesNotExist() + compose.onNodeWithTag("input-editor-tools").performClick() + compose.onNodeWithText(context.getString(R.string.input_editor_done)).performClick() + compose.runOnIdle { assertEquals(layout.elements.size + 1, InputLibraries.read(context).layout("game", InputRole.SECONDARY)!!.elements.size) } + } + + @Test fun localToolsCanMoveAndExpandWithoutMovingTheCanvas() { + val library = InputLibraries.read(context) + compose.setContent { DroidDeckTheme { InputSurfaceDialog("game", {}, {}, role = InputRole.PRIMARY, + layoutId = library.layout("game", InputRole.SECONDARY)!!.id, bindingsId = library.profile("game").binding(InputRole.SECONDARY)) } } + compose.runOnIdle { + val window = ShadowDialog.getLatestDialog().window!! + assertEquals(android.view.WindowManager.LayoutParams.MATCH_PARENT, window.attributes.width) + assertEquals(android.view.WindowManager.LayoutParams.MATCH_PARENT, window.attributes.height) + } + val canvas = compose.onNodeWithTag("input-layout-canvas").fetchSemanticsNode().boundsInRoot + val before = compose.onNodeWithTag("input-editor-tools").fetchSemanticsNode().boundsInRoot + compose.onNodeWithTag("input-editor-tools").performTouchInput { swipe(center, center + androidx.compose.ui.geometry.Offset(250f, 150f), 500) } + val after = compose.onNodeWithTag("input-editor-tools").fetchSemanticsNode().boundsInRoot + assertTrue(after.left > before.left + 100f) + compose.onNodeWithTag("input-editor-tools").performClick() + compose.onNodeWithText(context.getString(R.string.input_editor_add)).assertIsDisplayed() + assertEquals(canvas, compose.onNodeWithTag("input-layout-canvas").fetchSemanticsNode().boundsInRoot) + compose.onNodeWithTag("input-editor-tools").performClick() + compose.onNodeWithText(context.getString(R.string.input_editor_add)).assertDoesNotExist() + } + + @Test fun deckPresetOffersItsRealElementsAndTheCommonEditor() { + InputLibraries.update(context) { it.useLayout("game", InputRole.SECONDARY, InputLibraries.DECK, shared = true) } + compose.setContent { DroidDeckTheme { InputSettingsPage({}) } } + compose.onNodeWithTag("input-profile-game").performScrollTo().performClick() + compose.onNodeWithTag("input-part-secondary").performScrollTo().performClick() + compose.onNodeWithTag("input-element-legacy:l4").performScrollTo().assertIsDisplayed() + compose.onNodeWithTag("input-binding-a").assertDoesNotExist() + } + + @Test fun mouseTrackpadPresetIsEditableLikeAnyOtherLayout() { + InputLibraries.update(context) { it.useLayout("game", InputRole.SECONDARY, InputLibraries.TRACKPAD, shared = true) } + compose.setContent { DroidDeckTheme { InputSettingsPage({}) } } + compose.onNodeWithTag("input-profile-game").performScrollTo().performClick() + compose.onNodeWithTag("input-part-secondary").performScrollTo().performClick() + compose.onNodeWithTag("input-element-legacy:right-pad").performScrollTo().assertIsDisplayed() + compose.onNodeWithTag("input-surface-secondary").performScrollTo().assertIsDisplayed() + compose.onNodeWithTag("input-binding-a").assertDoesNotExist() + } + + @Test fun primaryScreenOffersItsActualElementsAndTheCommonEditor() { + compose.setContent { DroidDeckTheme { InputSettingsPage({}) } } + compose.onNodeWithTag("input-profile-game").performScrollTo().performClick() + compose.onNodeWithTag("input-part-primary").performScrollTo().performClick() + compose.onNodeWithTag("input-element-legacy:a").performScrollTo().assertIsDisplayed() + compose.onNodeWithTag("input-binding-a").assertDoesNotExist() + compose.onNodeWithTag("input-surface-primary").performScrollTo().performClick() + compose.onNodeWithTag("input-editor-tools").performClick() + compose.onNodeWithText(context.getString(R.string.input_editor_add)).assertIsDisplayed() + compose.onNodeWithTag("input-layout-canvas").assertExists() + } + + @Test fun deviceSettingsAreInlineBelowProfiles() { + compose.setContent { DroidDeckTheme { InputSettingsPage({}) } } + compose.onNodeWithTag("input-more").assertDoesNotExist() + compose.onNodeWithText(context.getString(R.string.ctrl_dead_zone)).performScrollTo().assertIsDisplayed() + compose.onNodeWithTag("input-profile-game").performScrollTo().assertIsDisplayed() + } + + @Test fun migratedPadClicksRemainEditableWithoutChangingMovement() { + compose.setContent { DroidDeckTheme { InputSettingsPage({}) } } + compose.onNodeWithTag("input-profile-desktop").performScrollTo().performClick() + compose.onNodeWithTag("input-part-secondary").performScrollTo().performClick() + compose.onNodeWithTag("input-click-legacy:right-pad").performScrollTo().performClick() + compose.onNodeWithText("Disabled", substring = false).performScrollTo().performClick() + compose.onNodeWithTag("input-action-save").performClick() + compose.runOnIdle { + val library = InputLibraries.read(context) + val pad = library.layout("desktop", InputRole.SECONDARY)!!.elements.first { it.control == InputControl.RIGHT_PAD } + val profile = library.compile("desktop") + assertEquals(InputAction.Disabled, profile.elementClickAction(InputRole.SECONDARY, pad)) + assertEquals(InputAction.Pointer(), profile.elementAction(InputRole.SECONDARY, pad.id, pad.control)) + } + } + + @Test @Config(sdk = [34]) fun secondarySettingsCanOpenAnActivityDialogAndCloseCleanly() { + val displayId = ShadowDisplayManager.addDisplay("w640dp-h480dp", ReflectionHelpers.getStaticField(Display::class.java, "TYPE_EXTERNAL")) + val display = context.getSystemService(DisplayManager::class.java).getDisplay(displayId) + shadowOf(display).setFlags(Display.FLAG_PRESENTATION) + val open = mutableStateOf(true) + try { + compose.setContent { DroidDeckTheme { + val host = androidx.compose.ui.platform.LocalContext.current.findActivity() as androidx.activity.ComponentActivity + if (open.value) InputDisplayContent(host, display, { open.value = false }) { InputSettingsPage({ open.value = false }) } + } } + compose.onNodeWithText(context.getString(R.string.input_new_profile)).performScrollTo().performClick() + compose.onNodeWithTag("input-name").assertExists() + compose.runOnIdle { + assertTrue(ShadowDialog.getShownDialogs().filterIsInstance().any { it.isShowing && it.display.displayId == displayId }) + val dialog = ShadowDialog.getShownDialogs().last { it !is Presentation && it.isShowing } + assertEquals(Display.DEFAULT_DISPLAY, dialog.window?.decorView?.display?.displayId) + open.value = false + } + compose.runOnIdle { assertTrue(ShadowDialog.getShownDialogs().none { it.isShowing }) } + } finally { ShadowDisplayManager.removeDisplay(displayId) } + } + + @Test @Config(sdk = [34]) fun unavailableDisplayDismissesSettingsWithoutCrashing() { + val displayId = ShadowDisplayManager.addDisplay("w640dp-h480dp", ReflectionHelpers.getStaticField(Display::class.java, "TYPE_EXTERNAL")) + val display = context.getSystemService(DisplayManager::class.java).getDisplay(displayId) + shadowOf(display).setFlags(Display.FLAG_PRESENTATION) + ShadowDisplayManager.removeDisplay(displayId) + val open = mutableStateOf(true) + compose.setContent { DroidDeckTheme { + val host = androidx.compose.ui.platform.LocalContext.current.findActivity() as androidx.activity.ComponentActivity + if (open.value) InputDisplayContent(host, display, { open.value = false }) { InputSettingsPage({}) } + } } + compose.runOnIdle { + assertFalse(open.value) + assertTrue(ShadowDialog.getShownDialogs().none { it.isShowing }) + } + } + + @Test @Config(sdk = [34]) fun mainEditorOpenedFromSecondarySettingsUsesAnActivityWindow() { + val displayId = ShadowDisplayManager.addDisplay("w640dp-h480dp", ReflectionHelpers.getStaticField(Display::class.java, "TYPE_EXTERNAL")) + val display = context.getSystemService(DisplayManager::class.java).getDisplay(displayId) + shadowOf(display).setFlags(Display.FLAG_PRESENTATION) + val open = mutableStateOf(true) + try { + compose.setContent { DroidDeckTheme { + val host = androidx.compose.ui.platform.LocalContext.current.findActivity() as androidx.activity.ComponentActivity + if (open.value) InputDisplayContent(host, display, { open.value = false }) { + InputSurfaceDialog("game", { open.value = false }, {}, role = InputRole.PRIMARY) + } + } } + compose.onNodeWithTag("input-layout-canvas").assertExists() + compose.runOnIdle { + assertTrue(ShadowDialog.getShownDialogs().none { it is Presentation && it.display.displayId == Display.DEFAULT_DISPLAY }) + val editor = ShadowDialog.getShownDialogs().last { it !is Presentation && it.isShowing } + assertEquals(Display.DEFAULT_DISPLAY, editor.window?.decorView?.display?.displayId) + open.value = false + } + compose.runOnIdle { assertTrue(ShadowDialog.getShownDialogs().none { it.isShowing }) } + } finally { ShadowDisplayManager.removeDisplay(displayId) } + } + + @Test @Config(sdk = [34]) fun auxiliaryEditorUsesThatDisplayAndSurvivesItsRemoval() { + val displayId = ShadowDisplayManager.addDisplay("w640dp-h480dp", ReflectionHelpers.getStaticField(Display::class.java, "TYPE_EXTERNAL")) + val display = context.getSystemService(DisplayManager::class.java).getDisplay(displayId) + shadowOf(display).setFlags(Display.FLAG_PRESENTATION) + assertTrue(SecondScreenDisplays.available(context.getSystemService(DisplayManager::class.java)).any { it.id == displayId }) + val previous = SessionState.secondScreenDisplay + SessionState.secondScreenDisplay = displayId + val open = mutableStateOf(true) + try { + compose.setContent { DroidDeckTheme { if (open.value) InputSurfaceDialog("game", { open.value = false }, {}) } } + compose.onNodeWithText(context.getString(R.string.input_editor_remote)).assertExists() + compose.onAllNodesWithText(context.getString(R.string.input_editor_add)).assertCountEquals(1) + compose.onNodeWithText(context.getString(R.string.input_hide_tools)).assertDoesNotExist() + assertEquals(0f, compose.onNodeWithTag("input-layout-canvas").fetchSemanticsNode().boundsInRoot.top, 1f) + compose.runOnIdle { + assertTrue(ShadowDialog.getShownDialogs().filterIsInstance().any { it.isShowing && it.display.displayId == displayId }) + } + compose.onNodeWithTag("input-layout-canvas").performTouchInput { click(percentOffset(.25f, .25f)) } + compose.onAllNodesWithText(context.getString(R.string.input_properties)).onFirst().performClick() + compose.onNodeWithTag("input-action-save").assertIsDisplayed() + compose.onNodeWithText("Disabled", substring = false).performScrollTo().performClick() + compose.runOnIdle { + assertTrue(ShadowDialog.getShownDialogs().any { it !is Presentation && it.isShowing && it.window?.decorView?.display?.displayId == Display.DEFAULT_DISPLAY }) + ShadowDisplayManager.removeDisplay(displayId) + } + compose.onNodeWithTag("input-action-save").assertIsDisplayed() + compose.onNodeWithTag("input-action-save").performClick() + compose.onNodeWithText(context.getString(R.string.input_display_unavailable)).assertExists() + compose.onNodeWithText(context.getString(R.string.input_editor_done)).performScrollTo().performClick() + compose.runOnIdle { + assertFalse(open.value) + assertEquals(InputAction.Disabled, InputProfiles.read(context, "game").elementAction(InputRole.SECONDARY, "legacy:left-pad", InputControl.LEFT_PAD)) + } + } finally { + SessionState.secondScreenDisplay = previous + if (display.isValid) ShadowDisplayManager.removeDisplay(displayId) + } + } + + @Test fun elementPropertiesEditOpacityLabelsAndOfferImages() { + val element = mutableStateOf(InputSurfaceElement(InputControl.A, .1f, .1f, .2f, .2f, "custom", name = "Custom button")) + val action = mutableStateOf(null) + compose.setContent { DroidDeckTheme { + androidx.compose.foundation.layout.Column(androidx.compose.ui.Modifier.verticalScroll(androidx.compose.foundation.rememberScrollState())) { + InputElementProperties(element.value, action.value) { element.value = it } + } + } } + compose.onNodeWithTag("setting-element-opacity").performScrollTo().performSemanticsAction(androidx.compose.ui.semantics.SemanticsActions.SetProgress) { it(60f) } + compose.runOnIdle { assertEquals(.6f, element.value.opacity, .001f) } + compose.onNodeWithTag("setting-element-label").performScrollTo().performClick() + compose.runOnIdle { assertFalse(element.value.showLabel) } + compose.onNodeWithText(context.getString(R.string.input_button_text)).performScrollTo().performClick() + compose.onNodeWithText(context.getString(R.string.screen_key_image)).assertExists() + compose.runOnIdle { element.value = element.value.copy(icon = InputButtonIcons.MENU) } + compose.onNodeWithTag("setting-element-label").assertDoesNotExist() + compose.runOnIdle { element.value = InputSurfaceElement(InputControl.LEFT_PAD, .1f, .1f, .4f, .4f, "pad", InputElementKind.TRACKPAD, "Pad") } + compose.onNodeWithTag("setting-element-label").assertDoesNotExist() + compose.onNodeWithTag("setting-element-opacity").assertExists() + for (kind in listOf(InputElementKind.DPAD, InputElementKind.STICK)) { + compose.runOnIdle { element.value = InputSurfaceElement(kind.control, .1f, .1f, .4f, .4f, "imported:0", kind, "WASD"); action.value = InputAction.Directions(17, 32, 31, 30) } + compose.onNodeWithTag("setting-element-label").performScrollTo().performClick() + compose.runOnIdle { assertFalse(element.value.showLabel); action.value = InputAction.Native() } + compose.onNodeWithTag("setting-element-label").assertDoesNotExist() + compose.runOnIdle { action.value = InputAction.Directions(0, 0, 0, 0, listOf(16, 18, 44, 46)) } + if (kind == InputElementKind.STICK) compose.onNodeWithTag("setting-element-label").assertExists() + else compose.onNodeWithTag("setting-element-label").assertDoesNotExist() + } + compose.runOnIdle { element.value = InputSurfaceElement(InputControl.LS, .1f, .1f, .4f, .4f, "legacy:left-stick", InputElementKind.STICK, "Move") } + compose.onNodeWithTag("setting-element-label").assertExists() + } + + @Test fun editorScalesEveryControlAndRestoresStockPositions() { + val library = InputLibraries.read(context) + val layout = library.layout("game", InputRole.SECONDARY)!! + assertEquals(InputLibraries.DECK, layout.id) + val round = mutableStateOf(0) + compose.setContent { DroidDeckTheme { androidx.compose.runtime.key(round.value) { InputSurfaceDialog("game", {}, {}, role = InputRole.PRIMARY, + layoutId = layout.id, bindingsId = library.profile("game").binding(InputRole.SECONDARY)) } } } + compose.onNodeWithTag("input-editor-tools").performClick() + compose.onNodeWithText(context.getString(R.string.input_reset_positions)).performScrollTo().assertIsNotEnabled() + compose.onNodeWithTag("input-editor-scale").performScrollTo().performSemanticsAction(androidx.compose.ui.semantics.SemanticsActions.SetProgress) { it(80f) } + compose.onNodeWithText(context.getString(R.string.input_editor_done)).performScrollTo().performClick() + val scaled = InputLibraries.read(context).layouts.first { it.id == layout.id } + assertEquals(layout.elements.scaled(.8f), scaled.elements) + compose.runOnIdle { round.value = 1 } + compose.onNodeWithTag("input-editor-tools").performClick() + compose.onNodeWithText(context.getString(R.string.input_reset_positions)).performScrollTo().performClick() + compose.onNodeWithText(context.getString(R.string.input_editor_done)).performScrollTo().performClick() + compose.runOnIdle { assertEquals(layout.elements, InputLibraries.read(context).layouts.first { it.id == layout.id }.elements) } + } + + @Test fun editorSnapChoiceIsRemembered() { + com.droiddeck.launcher.input.ControllerPrefs.setSnap(context, true) + val library = InputLibraries.read(context) + val layout = library.layout("game", InputRole.SECONDARY)!! + val round = mutableStateOf(0) + compose.setContent { DroidDeckTheme { androidx.compose.runtime.key(round.value) { InputSurfaceDialog("game", {}, {}, role = InputRole.PRIMARY, + layoutId = layout.id, bindingsId = library.profile("game").binding(InputRole.SECONDARY)) } } } + val snap = isToggleable() and hasAnySibling(hasText(context.getString(R.string.ctrl_editor_snap))) + compose.onNodeWithTag("input-editor-tools").performClick() + compose.onNode(snap).performScrollTo().assertIsOn().performClick() + compose.runOnIdle { assertFalse(com.droiddeck.launcher.input.ControllerPrefs.snap(context)) } + compose.runOnIdle { round.value = 1 } + compose.onNodeWithTag("input-editor-tools").performClick() + compose.onNode(snap).performScrollTo().assertIsOff() + com.droiddeck.launcher.input.ControllerPrefs.setSnap(context, true) + } + + @Test fun quickButtonTogglesHideBuiltInButtonsWithoutDeletingThem() { + compose.setContent { DroidDeckTheme { InputSettingsPage({}) } } + compose.onNodeWithTag("input-profile-game").performScrollTo().performClick() + compose.onNodeWithTag("input-part-primary").performScrollTo().performClick() + val before = InputLibraries.read(context).layout("game", InputRole.PRIMARY)!! + compose.onNodeWithTag("setting-quick-keyboard").performScrollTo().performClick() + compose.runOnIdle { + val after = InputLibraries.read(context).layout("game", InputRole.PRIMARY)!! + assertEquals(before.elements, after.elements) + assertEquals(setOf(InputCommand.KEYBOARD), after.style.hidden) + } + compose.onNodeWithTag("setting-quick-keyboard").performScrollTo().performClick() + compose.runOnIdle { assertTrue(InputLibraries.read(context).layout("game", InputRole.PRIMARY)!!.style.hidden.isEmpty()) } + } + + @Test fun layoutsCanBeImportedAndExported() { + val source = java.io.File(context.cacheDir, "jump.icp").apply { + writeText("""{"name":"Jump keys","elements":[{"type":"BUTTON","shape":"CIRCLE","bindings":["KEY_SPACE"],"scale":1.0,"x":0.8,"y":0.7,"text":"Jump"}]}""") + } + val target = java.io.File(context.cacheDir, "out.icp").apply { delete() } + val registry = object : androidx.activity.result.ActivityResultRegistry() { + override fun onLaunch(requestCode: Int, contract: androidx.activity.result.contract.ActivityResultContract, input: I, + options: androidx.core.app.ActivityOptionsCompat?) { + dispatchResult(requestCode, android.net.Uri.fromFile(if (input is String) target else source)) + } + } + val owner = object : androidx.activity.result.ActivityResultRegistryOwner { override val activityResultRegistry = registry } + compose.setContent { androidx.compose.runtime.CompositionLocalProvider(androidx.activity.compose.LocalActivityResultRegistryOwner provides owner) { + DroidDeckTheme { InputSettingsPage({}) } + } } + compose.onNodeWithTag("input-profile-game").performScrollTo().performClick() + compose.onNodeWithTag("input-part-primary").performScrollTo().performClick() + compose.onNodeWithText(context.getString(R.string.input_saved_layouts)).performScrollTo().performClick() + compose.onNodeWithTag("layout-manage-${InputLibraries.OVERLAY}").performScrollTo().performClick() + compose.onNodeWithText(context.getString(R.string.screen_key_layout_export)).assertDoesNotExist() + compose.onNodeWithText(context.getString(R.string.common_close)).performClick() + compose.onNodeWithTag("input-layout-import").performScrollTo().performClick() + compose.waitUntil(5_000) { InputLibraries.read(context).layout("game", InputRole.PRIMARY)!!.name == "Jump keys" } + val imported = InputLibraries.read(context).layout("game", InputRole.PRIMARY)!! + assertEquals(InputLayoutScreen.MAIN, imported.screen) + assertEquals(InputAction.Keys(listOf(57)), InputLibraries.read(context).compile("game").elementAction(InputRole.PRIMARY, imported.elements.single().id, InputControl.A)) + compose.onNodeWithText(context.getString(R.string.input_saved_layouts)).performScrollTo().performClick() + compose.onNodeWithTag("layout-manage-${imported.id}").performScrollTo().performClick() + compose.onNodeWithText(context.getString(R.string.screen_key_layout_export)).performClick() + compose.waitUntil(5_000) { target.isFile && target.length() > 0 } + assertEquals(source.readText().let { org.json.JSONObject(it).getString("name") }, org.json.JSONObject(target.readText()).getString("name")) + assertEquals("KEY_SPACE", org.json.JSONObject(target.readText()).getJSONArray("elements").getJSONObject(0).getJSONArray("bindings").getString(0)) + } + + @Test fun buttonSizeRegeneratesTheGamepadAndUndoesAsOneStep() { + val library = InputLibraries.read(context) + val layout = library.layout("game", InputRole.PRIMARY)!! + val (_, metrics) = InputLibraries.primaryDisplay(context) + assertNotNull(OnScreenLayout.generatedSize(context, layout, metrics.widthPixels, metrics.heightPixels)) + compose.setContent { DroidDeckTheme { InputSurfaceDialog("game", {}, {}, role = InputRole.PRIMARY, + layoutId = layout.id, bindingsId = library.profile("game").binding(InputRole.PRIMARY)) } } + compose.onNodeWithTag("input-editor-tools").performClick() + val slider = compose.onNodeWithTag("input-editor-scale").performScrollTo() + slider.assert(SemanticsMatcher.expectValue(androidx.compose.ui.semantics.SemanticsProperties.StateDescription, "100%")) + for (value in listOf(110f, 125f)) slider.performSemanticsAction(androidx.compose.ui.semantics.SemanticsActions.SetProgress) { it(value) } + compose.onNodeWithText(context.getString(R.string.input_editor_undo)).performScrollTo().performClick() + slider.assert(SemanticsMatcher.expectValue(androidx.compose.ui.semantics.SemanticsProperties.StateDescription, "100%")) + slider.performSemanticsAction(androidx.compose.ui.semantics.SemanticsActions.SetProgress) { it(125f) } + compose.onNodeWithText(context.getString(R.string.input_editor_done)).performScrollTo().performClick() + compose.runOnIdle { + val saved = InputLibraries.read(context).layouts.first { it.id == layout.id } + assertEquals(1.25f, saved.scale) + assertNotNull(OnScreenLayout.generatedSize(context, saved, metrics.widthPixels, metrics.heightPixels)) + assertNotEquals(layout.elements.first { it.id == "legacy:a" }.y, saved.elements.first { it.id == "legacy:a" }.y) + } + } + + @Test @Config(qualifiers = "w833dp-h468dp-land-369dpi") + fun buttonSizeKeepsMovedGamepadGroupsInShape() { + val face = setOf("legacy:a", "legacy:b", "legacy:x", "legacy:y") + val original = InputLibraries.read(context) + val gamepad = original.layout("game", InputRole.PRIMARY)!! + val moved = gamepad.copy(elements = gamepad.elements.map { if (it.id in face) it.copy(x = it.x + .012f, y = it.y + .017f) else it }) + InputLibraries.update(context) { it.withLayout(moved) } + val (_, metrics) = InputLibraries.primaryDisplay(context) + assertNull(OnScreenLayout.generatedSize(context, moved, metrics.widthPixels, metrics.heightPixels)) + val library = InputLibraries.read(context) + compose.setContent { DroidDeckTheme { InputSurfaceDialog("game", {}, {}, role = InputRole.PRIMARY, + layoutId = moved.id, bindingsId = library.profile("game").binding(InputRole.PRIMARY)) } } + compose.onNodeWithTag("input-editor-tools").performClick() + compose.onNodeWithTag("input-editor-scale").performScrollTo().performSemanticsAction(androidx.compose.ui.semantics.SemanticsActions.SetProgress) { it(125f) } + compose.onNodeWithText(context.getString(R.string.input_editor_done)).performScrollTo().performClick() + compose.runOnIdle { + val saved = InputLibraries.read(context).layouts.first { it.id == moved.id } + assertEquals(1.25f, saved.scale) + val w = metrics.widthPixels; val h = metrics.heightPixels + fun centre(id: String) = saved.elements.first { it.id == id }.let { (it.x + it.width / 2) * w to (it.y + it.height / 2) * h } + assertEquals(centre("legacy:y").first, centre("legacy:a").first, 1f) + assertEquals(centre("legacy:x").second, centre("legacy:b").second, 1f) + assertTrue(centre("legacy:y").second < centre("legacy:x").second && centre("legacy:x").second < centre("legacy:a").second) + assertEquals(centre("legacy:a").second - centre("legacy:y").second, centre("legacy:b").first - centre("legacy:x").first, 1f) + assertEquals(centre("legacy:up").first, centre("legacy:down").first, 1f) + assertEquals(centre("legacy:left").second, centre("legacy:right").second, 1f) + val before = moved.elements.first { it.id == "legacy:a" } + assertTrue(saved.elements.first { it.id == "legacy:a" }.width > before.width * 1.2f) + } + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/ui/InputSurfaceGesturesTest.kt b/app/src/test/java/com/droiddeck/launcher/ui/InputSurfaceGesturesTest.kt new file mode 100644 index 0000000000..78cdd27115 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/ui/InputSurfaceGesturesTest.kt @@ -0,0 +1,120 @@ +package com.droiddeck.launcher.ui + +import android.os.Looper +import android.view.MotionEvent +import android.view.View +import android.view.ViewGroup +import androidx.activity.ComponentActivity +import com.droiddeck.launcher.input.* +import org.junit.After +import org.junit.Assert.* +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.Robolectric +import org.robolectric.RobolectricTestRunner +import org.robolectric.Shadows.shadowOf +import org.robolectric.annotation.Config +import java.time.Duration + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [34], shadows = [JoyConPadTest.RecordingWriter::class]) +class InputSurfaceGesturesTest { + private val controller = Robolectric.buildActivity(ComponentActivity::class.java).setup() + private val activity = controller.get() + private val events = mutableListOf() + private val moves = mutableListOf>() + private val bridge = PadBridge(activity.cacheDir) + private val router = InputRouter(object : InputOutput { + override fun key(code: Int, down: Boolean) { events += "key:$code:$down" } + override fun button(code: Int, down: Boolean) { events += "button:$code:$down" } + override fun pointer(dx: Float, dy: Float) { moves += dx to dy } + override fun scroll(steps: Int) { events += "scroll:$steps" } + override fun command(command: InputCommand) {} + override fun requestMotion() { error("Touchpad must not schedule stick frames") } + }) + private lateinit var panel: InputSurfaceView + private lateinit var pad: View + private var time = 1000L + private val element = InputSurfaceElement(InputControl.RIGHT_PAD, 0f, 0f, 1f, 1f, mouseGestures = true) + private fun profile(action: InputAction = InputAction.Pointer()) = InputProfile("test", + elements = mapOf(InputRole.SECONDARY to mapOf(element.id to action)), + clicks = mapOf(InputRole.SECONDARY to mapOf(element.id to InputAction.MouseButton(272)))) + + @Before fun setup() { + bridge.setInputRouter(router) + bridge.setInputProfile(profile()) + panel = InputSurfaceView(activity, bridge, listOf(element)) + activity.setContentView(panel) + panel.measure(View.MeasureSpec.makeMeasureSpec(1000, View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(600, View.MeasureSpec.EXACTLY)) + panel.layout(0, 0, 1000, 600) + pad = descendants(panel).first { it.javaClass.simpleName == "TrackpadView" } + } + @After fun close() { panel.releaseInputs(); bridge.stop(); controller.pause().stop().destroy() } + + @Test fun tapClicksWithoutRepositioningCursorFromAnotherSource() { + touch(MotionEvent.ACTION_DOWN); touch(MotionEvent.ACTION_UP) + shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(100)) + assertEquals(listOf("button:272:true", "button:272:false"), events) + assertTrue(moves.isEmpty()) + val other = router.channel(InputRole.PHYSICAL, InputControl.LS, fixedAction = InputAction.Pointer()) + other.update(0f, 0f, true); other.pointerDelta(.1f, .2f); other.update(0f) + val positionChanges = moves.toList() + time += 1000 + touch(MotionEvent.ACTION_DOWN); touch(MotionEvent.ACTION_UP) + assertEquals(positionChanges, moves) + other.close() + } + + @Test fun tapUsesTheConfiguredElementClick() { + bridge.setInputProfile(profile().copy(clicks = mapOf(InputRole.SECONDARY to mapOf(element.id to InputAction.Keys(listOf(57)))))) + touch(MotionEvent.ACTION_DOWN); touch(MotionEvent.ACTION_UP) + shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(100)) + assertEquals(listOf("key:57:true", "key:57:false"), events) + } + + @Test fun motionDeliversImmediatelyAndTwoFingerScrollDoesNotMoveThePointer() { + touch(MotionEvent.ACTION_DOWN) + touch(MotionEvent.ACTION_MOVE, y = 230f) + assertTrue(moves.isNotEmpty()) + touch(MotionEvent.ACTION_UP) + time += 1000 + touch(MotionEvent.ACTION_DOWN) + touch(MotionEvent.ACTION_POINTER_DOWN or (1 shl MotionEvent.ACTION_POINTER_INDEX_SHIFT), fingers = 2) + val before = moves.size + touch(MotionEvent.ACTION_MOVE, y = 270f, fingers = 2) + assertEquals(before, moves.size) + assertTrue(events.any { it.startsWith("scroll:") }) + } + + @Test fun profileChangeAndMenuSuspensionSuppressPendingGesturePresses() { + touch(MotionEvent.ACTION_DOWN) + bridge.setInputEnabled(false) + bridge.setInputEnabled(true) + shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(1000)) + assertTrue(events.isEmpty()) + touch(MotionEvent.ACTION_CANCEL) + time += 1000 + touch(MotionEvent.ACTION_DOWN) + bridge.setInputProfile(profile(InputAction.Disabled)) + shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(1000)) + assertTrue(events.isEmpty()) + panel.releaseInputs() + bridge.setInputProfile(profile()) + time += 1000 + touch(MotionEvent.ACTION_DOWN); touch(MotionEvent.ACTION_UP) + shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(100)) + assertEquals(listOf("button:272:true", "button:272:false"), events) + } + + private fun touch(action: Int, y: Float = 200f, fingers: Int = 1) { + val properties = Array(fingers) { i -> MotionEvent.PointerProperties().apply { id = i; toolType = MotionEvent.TOOL_TYPE_FINGER } } + val coords = Array(fingers) { i -> MotionEvent.PointerCoords().apply { x = 200f + 100 * i; this.y = y; pressure = 1f; size = 1f } } + val event = MotionEvent.obtain(time - 20, time, action, fingers, properties, coords, 0, 0, 1f, 1f, 0, 0, 0, 0) + try { assertTrue(pad.onTouchEvent(event)) } finally { event.recycle(); time += 10 } + } + private fun descendants(view: View): Sequence = sequence { + yield(view) + if (view is ViewGroup) for (i in 0 until view.childCount) yieldAll(descendants(view.getChildAt(i))) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/ui/InputSurfaceOwnershipTest.kt b/app/src/test/java/com/droiddeck/launcher/ui/InputSurfaceOwnershipTest.kt new file mode 100644 index 0000000000..d1275581f7 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/ui/InputSurfaceOwnershipTest.kt @@ -0,0 +1,151 @@ +package com.droiddeck.launcher.ui + +import androidx.activity.ComponentActivity +import android.view.MotionEvent +import android.view.View +import android.view.ViewGroup +import android.widget.FrameLayout +import android.widget.TextView +import com.droiddeck.launcher.R +import com.droiddeck.launcher.input.* +import com.droiddeck.launcher.input.JoyConPadTest +import com.droiddeck.launcher.input.PadBridge +import com.droiddeck.launcher.input.PadState +import com.droiddeck.launcher.session.SessionPrefs +import com.droiddeck.launcher.session.SessionState +import java.io.File +import org.junit.After +import org.junit.Assert.* +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.Robolectric +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [34], shadows = [JoyConPadTest.RecordingWriter::class]) +class InputSurfaceOwnershipTest { + private val controller = Robolectric.buildActivity(ComponentActivity::class.java).setup() + private val activity = controller.get() + private val root = FrameLayout(activity) + private val bridge = PadBridge(File(activity.cacheDir, "panel-ownership")) + private lateinit var panel: InputSurfaceView + private val previousDeckPad = SessionState.deckPad + + @Before fun setUp() { + SessionState.deckPad = true + DeckControls.releaseAll() + bridge.setInputRouter(InputRouter(object : InputOutput { + override fun key(code: Int, down: Boolean) {} + override fun button(code: Int, down: Boolean) {} + override fun pointer(dx: Float, dy: Float) {} + override fun scroll(steps: Int) {} + override fun command(command: InputCommand) {} + override fun requestMotion() {} + })) + val elements = listOf(InputControl.RIGHT_PAD, InputControl.LS, InputControl.A).mapIndexed { i, c -> + InputSurfaceElement(c, .02f + i * .3f, .1f, .28f, .4f) + } + InputSurfaceElement(InputControl.LS, .02f, .6f, .28f, .3f, "dpad", InputElementKind.DPAD) + bridge.setInputProfile(InputProfile("native", elements = mapOf(InputRole.SECONDARY to mapOf("dpad" to InputAction.Native())))) + activity.setContentView(root) + panel = InputSurfaceView(activity, bridge, elements) + root.addView(panel) + val trackpad = child("TrackpadView") + trackpad.layout(0, 0, 300, 300) + touch(trackpad, MotionEvent.ACTION_DOWN, 200f, 100f) + touch(trackpad, MotionEvent.ACTION_POINTER_DOWN or (1 shl MotionEvent.ACTION_POINTER_INDEX_SHIFT), 200f, 100f, fingers = 2) + assertEquals(32 or 128, JoyConPadTest.RecordingWriter.grips) + } + + @After fun close() { + root.removeAllViews() + bridge.stop() + DeckControls.releaseAll() + controller.pause().stop().destroy() + SessionState.deckPad = previousDeckPad + } + + @Test fun removingTheStickKeepsTrackpadContactAndClickHeld() { + val stick = child("StickView") + stick.layout(0, 0, 300, 300) + touch(stick, MotionEvent.ACTION_DOWN, 150f, 150f) + touch(stick, MotionEvent.ACTION_MOVE, 240f, 150f) + assertTrue(state().leftX > 0f) + val position = JoyConPadTest.RecordingWriter.deckPads.copyOf() + (stick.parent as ViewGroup).removeView(stick) + assertEquals(0f, state().leftX) + assertTrackpadStillHeld(position) + root.removeView(panel) + assertEquals(0, JoyConPadTest.RecordingWriter.grips) + } + + @Test fun removingDirectionPadReleasesOnlyItsOwnInput() { + val dpad = child("DirectionPad") + dpad.layout(0, 0, 300, 300) + touch(dpad, MotionEvent.ACTION_DOWN, 150f, 30f) + assertTrue(state().up) + val position = JoyConPadTest.RecordingWriter.deckPads.copyOf() + (dpad.parent as ViewGroup).removeView(dpad) + assertFalse(state().up) + assertTrackpadStillHeld(position) + } + + @Test fun removingAButtonReleasesOnlyItsOwnInput() { + val buttons = child("HoldButton") + press(buttons) + assertTrue(state().isDown(PadState.A)) + val position = JoyConPadTest.RecordingWriter.deckPads.copyOf() + (buttons.parent as ViewGroup).removeView(buttons) + assertFalse(state().isDown(PadState.A)) + assertTrackpadStillHeld(position) + } + + @Test fun replacingTheLayoutReleasesEveryOldControl() { + holdFaceButton() + assertTrue(state().isDown(PadState.A)) + panel.updateCustomLayout(emptyList()) + assertFalse(state().isDown(PadState.A)) + assertEquals(0, JoyConPadTest.RecordingWriter.grips) + bridge.applyExternal({ it.press(PadState.B, true) }, true) + assertTrue(state().isDown(PadState.B)) + assertFalse(state().isDown(PadState.A)) + } + + @Test fun removingThePanelLeavesNoTouchSourceForLaterPhysicalInput() { + holdFaceButton() + root.removeView(panel) + assertFalse(state().isDown(PadState.A)) + bridge.applyExternal({ it.press(PadState.B, true) }, true) + assertTrue(state().isDown(PadState.B)) + } + + private fun holdFaceButton() { + val buttons = child("HoldButton") + press(buttons) + } + + private fun assertTrackpadStillHeld(position: ShortArray) { + assertEquals(32 or 128, JoyConPadTest.RecordingWriter.grips) + assertArrayEquals(position, JoyConPadTest.RecordingWriter.deckPads) + } + + private fun state() = JoyConPadTest.RecordingWriter.pads[0]!! + private fun child(name: String) = descendants(panel).first { it.javaClass.simpleName == name } + private fun descendants(view: View): Sequence = sequence { + yield(view) + if (view is ViewGroup) for (i in 0 until view.childCount) yieldAll(descendants(view.getChildAt(i))) + } + + private fun press(button: View) { + button.javaClass.getDeclaredMethod("press", Int::class.java, Boolean::class.java, Boolean::class.java) + .apply { isAccessible = true }.invoke(button, 0, true, false) + } + + private fun touch(view: View, action: Int, x: Float, y: Float, fingers: Int = 1) { + val properties = Array(fingers) { i -> MotionEvent.PointerProperties().apply { id = i; toolType = MotionEvent.TOOL_TYPE_FINGER } } + val coords = Array(fingers) { MotionEvent.PointerCoords().apply { this.x = x; this.y = y; pressure = 1f; size = 1f } } + val event = MotionEvent.obtain(1000, 1010, action, fingers, properties, coords, 0, 0, 1f, 1f, 0, 0, 0, 0) + try { assertTrue(view.onTouchEvent(event)) } finally { event.recycle() } + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/ui/InputSurfaceTouchTest.kt b/app/src/test/java/com/droiddeck/launcher/ui/InputSurfaceTouchTest.kt new file mode 100644 index 0000000000..27f0669620 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/ui/InputSurfaceTouchTest.kt @@ -0,0 +1,235 @@ +package com.droiddeck.launcher.ui + +import android.content.Context +import android.view.HapticFeedbackConstants +import android.view.MotionEvent +import android.view.View +import android.view.ViewGroup +import androidx.activity.ComponentActivity +import com.droiddeck.launcher.input.* +import java.io.File +import org.junit.After +import org.junit.Assert.* +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.Robolectric +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.Shadows.shadowOf +import org.robolectric.annotation.Config + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [34], manifest = Config.NONE, shadows = [JoyConPadTest.RecordingWriter::class]) +class InputSurfaceTouchTest { + private val context: Context get() = RuntimeEnvironment.getApplication() + private val bridge = PadBridge(File(context.cacheDir, "surface-touch")) + private val events = mutableListOf() + private val router = InputRouter(object : InputOutput { + override fun key(code: Int, down: Boolean) { events += "key $code $down" } + override fun button(code: Int, down: Boolean) {} + override fun pointer(dx: Float, dy: Float) {} + override fun scroll(steps: Int) {} + override fun command(command: InputCommand) { events += command.name } + override fun command(command: InputCommand, role: InputRole) { events += "${command.name}:$role" } + override fun requestMotion() {} + }) + private lateinit var view: OnScreenControls + private val a = InputSurfaceElement(InputControl.A, .7f, .3f, .2f, .2f) + private val b = InputSurfaceElement(InputControl.B, .7f, .6f, .2f, .2f) + private val stick = InputSurfaceElement(InputControl.LS, .1f, .3f, .2f, .4f, floating = false) + private val keyboard = InputSurfaceElement(InputControl.A, .4f, .8f, .1f, .1f, "builtin:keyboard", name = "Keyboard") + private var time = 1000L + + @Before fun setup() { + bridge.setInputRouter(router) + bridge.setInputProfile(InputProfile("touch", elements = mapOf(InputRole.PRIMARY to mapOf( + "builtin:keyboard" to InputAction.Command(InputCommand.KEYBOARD), "latch" to InputAction.Keys(listOf(42)))))) + view = OnScreenControls(context, bridge) + show(a, b, stick, keyboard) + } + + @After fun close() { view.releaseAll(); bridge.stop() } + + private fun show(vararg elements: InputSurfaceElement) { + view.updateLayout(SavedInputLayout("touch", "Touch", elements.toList(), aspect = 1000f / 600f)) + measure(view) + } + + private fun measure(target: View) { + target.measure(View.MeasureSpec.makeMeasureSpec(1000, View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(600, View.MeasureSpec.EXACTLY)) + target.layout(0, 0, 1000, 600) + } + + private fun state() = JoyConPadTest.RecordingWriter.pads[0] ?: PadState() + + @Test fun aFingerSlidingBetweenButtonsMovesThePress() { + touch(MotionEvent.ACTION_DOWN, 1 to (800f to 240f)) + assertTrue(state().isDown(PadState.A)) + touch(MotionEvent.ACTION_MOVE, 1 to (800f to 420f)) + assertFalse(state().isDown(PadState.A)) + assertTrue(state().isDown(PadState.B)) + touch(MotionEvent.ACTION_MOVE, 1 to (600f to 100f)) + assertFalse(state().isDown(PadState.B)) + touch(MotionEvent.ACTION_MOVE, 1 to (800f to 240f)) + assertTrue(state().isDown(PadState.A)) + touch(MotionEvent.ACTION_UP, 1 to (800f to 240f)) + assertFalse(state().isDown(PadState.A)) + } + + @Test fun aStickKeepsItsFingerWhenItCrossesAButton() { + touch(MotionEvent.ACTION_DOWN, 1 to (200f to 300f)) + touch(MotionEvent.ACTION_MOVE, 1 to (800f to 240f)) + assertFalse(state().isDown(PadState.A)) + assertEquals(1f, state().leftX, .01f) + touch(MotionEvent.ACTION_UP, 1 to (800f to 240f)) + assertEquals(0f, state().leftX, 0f) + } + + @Test fun mouseClicksPassThroughToTheGame() { + val missed = mutableListOf() + view.onTouchMiss = { missed += it.actionMasked } + touch(MotionEvent.ACTION_DOWN, 1 to (800f to 240f), tool = MotionEvent.TOOL_TYPE_MOUSE) + touch(MotionEvent.ACTION_UP, 1 to (800f to 240f), tool = MotionEvent.TOOL_TYPE_MOUSE) + assertFalse(state().isDown(PadState.A)) + assertEquals(listOf(MotionEvent.ACTION_DOWN, MotionEvent.ACTION_UP), missed) + view.onTouchMiss = null + assertFalse(touch(MotionEvent.ACTION_DOWN, 1 to (800f to 240f), tool = MotionEvent.TOOL_TYPE_MOUSE)) + assertFalse(state().isDown(PadState.A)) + } + + @Test fun keyboardShortcutOpensOnRelease() { + touch(MotionEvent.ACTION_DOWN, 1 to (450f to 510f)) + assertTrue(events.isEmpty()) + touch(MotionEvent.ACTION_UP, 1 to (450f to 510f)) + assertEquals(listOf("KEYBOARD:PRIMARY"), events) + } + + @Test fun cancelledDraggedOrDisabledKeyboardShortcutDoesNotOpen() { + touch(MotionEvent.ACTION_DOWN, 1 to (450f to 510f)) + touch(MotionEvent.ACTION_CANCEL, 1 to (450f to 510f)) + touch(MotionEvent.ACTION_UP, 1 to (450f to 510f)) + touch(MotionEvent.ACTION_DOWN, 1 to (450f to 510f)) + touch(MotionEvent.ACTION_UP, 1 to (450f to 510f), flags = MotionEvent.FLAG_CANCELED) + touch(MotionEvent.ACTION_DOWN, 1 to (450f to 510f)) + touch(MotionEvent.ACTION_MOVE, 1 to (600f to 510f)) + touch(MotionEvent.ACTION_UP, 1 to (600f to 510f)) + assertTrue(events.isEmpty()) + show(a, b, stick) + view.setQuickHidden(true) + measure(view) + assertFalse(view.takesTouchAt(450f, 510f)) + assertFalse(touch(MotionEvent.ACTION_DOWN, 1 to (450f to 510f))) + assertTrue(events.isEmpty()) + } + + @Test fun keyboardShortcutWorksWithHiddenPadAndButtonsOnly() { + view.setQuickHidden(true) + measure(view) + assertFalse(view.takesTouchAt(800f, 240f)) + touch(MotionEvent.ACTION_DOWN, 1 to (450f to 510f)) + touch(MotionEvent.ACTION_UP, 1 to (450f to 510f)) + view.setButtonsOnly(true) + measure(view) + val (x, y) = centre(element(view, "builtin:keyboard")) + assertFalse(view.takesTouchAt(800f, 240f)) + touch(MotionEvent.ACTION_DOWN, 1 to (x to y)) + touch(MotionEvent.ACTION_UP, 1 to (x to y)) + assertEquals(listOf("KEYBOARD:PRIMARY", "KEYBOARD:PRIMARY"), events) + } + + @Test fun hidingTheViewReleasesLatchedKeys() { + val controller = Robolectric.buildActivity(ComponentActivity::class.java).setup() + try { + controller.get().setContentView(view) + show(InputSurfaceElement(InputControl.A, .7f, .3f, .2f, .2f, "latch", toggle = true)) + touch(MotionEvent.ACTION_DOWN, 1 to (800f to 240f)) + touch(MotionEvent.ACTION_UP, 1 to (800f to 240f)) + assertEquals(listOf("key 42 true"), events) + view.visibility = View.GONE + assertEquals(listOf("key 42 true", "key 42 false"), events) + } finally { controller.pause().stop().destroy() } + } + + @Test fun onlyPanelsVibrateOnPress() { + touch(MotionEvent.ACTION_DOWN, 1 to (800f to 240f)) + assertTrue(state().isDown(PadState.A)) + assertNotEquals(HapticFeedbackConstants.VIRTUAL_KEY, shadowOf(button(view)).lastHapticFeedbackPerformed()) + touch(MotionEvent.ACTION_UP, 1 to (800f to 240f)) + val panel = InputSurfaceView(context, bridge, listOf(a)) + measure(panel) + touch(MotionEvent.ACTION_DOWN, 1 to (800f to 240f), target = panel) + assertEquals(HapticFeedbackConstants.VIRTUAL_KEY, shadowOf(button(panel)).lastHapticFeedbackPerformed()) + touch(MotionEvent.ACTION_CANCEL, 1 to (800f to 240f), target = panel) + } + + @Test fun classicSticksTravelTheirFullRadius() { + show(stick.copy(buttonShape = InputButtonShape.CIRCLE)) + touch(MotionEvent.ACTION_DOWN, 1 to (200f to 300f)) + touch(MotionEvent.ACTION_MOVE, 1 to (250f to 300f)) + assertEquals(.5f, state().leftX, .01f) + touch(MotionEvent.ACTION_MOVE, 1 to (300f to 300f)) + assertEquals(1f, state().leftX, .01f) + touch(MotionEvent.ACTION_UP, 1 to (300f to 300f)) + } + + @Test fun buttonsAcceptTouchesJustOutsideTheirEdge() { + show(InputSurfaceElement(InputControl.A, .7f, .3f, .1f, 1f / 6f, buttonShape = InputButtonShape.CIRCLE), + InputSurfaceElement(InputControl.B, .3f, .3f, .2f, .1f, buttonShape = InputButtonShape.PILL), + InputSurfaceElement(InputControl.X, .3f, .6f, .2f, .1f)) + assertTrue(view.takesTouchAt(750f, 285f)) + assertTrue(view.takesTouchAt(805f, 230f)) + assertFalse(view.takesTouchAt(750f, 300f)) + assertFalse(view.takesTouchAt(805f, 285f)) + assertTrue(view.takesTouchAt(400f, 245f)) + assertFalse(view.takesTouchAt(400f, 255f)) + assertFalse(view.takesTouchAt(400f, 359f)) + assertTrue(view.takesTouchAt(400f, 361f)) + touch(MotionEvent.ACTION_DOWN, 1 to (750f to 285f)) + assertTrue(state().isDown(PadState.A)) + touch(MotionEvent.ACTION_UP, 1 to (750f to 285f)) + } + + @Test fun adaptiveSticksCaptureAroundThemAndTheLeftOneToTheEdge() { + val left = InputSurfaceElement(InputControl.LS, .3f, .3f, .2f, .4f, hideWhenIdle = true) + val right = InputSurfaceElement(InputControl.RS, .6f, .3f, .2f, .4f, hideWhenIdle = true) + show(left, right) + assertTrue(view.takesTouchAt(10f, 300f)) + assertTrue(view.takesTouchAt(10f, 390f)) + assertFalse(view.takesTouchAt(10f, 420f)) + assertTrue(view.takesTouchAt(795f, 300f)) + assertFalse(view.takesTouchAt(815f, 300f)) + val missed = mutableListOf() + view.onTouchMiss = { missed += it.getPointerId(0) } + touch(MotionEvent.ACTION_DOWN, 1 to (50f to 300f)) + touch(MotionEvent.ACTION_POINTER_DOWN or (1 shl MotionEvent.ACTION_POINTER_INDEX_SHIFT), 1 to (50f to 300f), 2 to (60f to 310f)) + assertEquals(listOf(2), missed) + touch(MotionEvent.ACTION_MOVE, 1 to (150f to 300f), 2 to (60f to 310f)) + assertTrue(state().leftX > .9f) + show(left.copy(hideWhenIdle = false), right.copy(hideWhenIdle = false)) + assertFalse(view.takesTouchAt(10f, 300f)) + assertFalse(view.takesTouchAt(815f, 300f)) + } + + private fun element(root: View, id: String): View = descendants(root).first { (it.tag as? InputSurfaceElement)?.id == id } + private fun centre(target: View): Pair { + var x = target.width / 2f + var y = target.height / 2f + var at: View = target + while (at !== view) { x += at.left; y += at.top; at = at.parent as View } + return x to y + } + private fun button(root: View): View = descendants(root).first { it.javaClass.simpleName == "HoldButton" } + private fun descendants(root: View): Sequence = sequence { + yield(root) + if (root is ViewGroup) for (i in 0 until root.childCount) yieldAll(descendants(root.getChildAt(i))) + } + + private fun touch(action: Int, vararg pointers: Pair>, flags: Int = 0, + tool: Int = MotionEvent.TOOL_TYPE_FINGER, target: View = view): Boolean { + val properties = pointers.map { (id, _) -> MotionEvent.PointerProperties().apply { this.id = id; toolType = tool } }.toTypedArray() + val coords = pointers.map { (_, position) -> MotionEvent.PointerCoords().apply { x = position.first; y = position.second; pressure = 1f; size = 1f } }.toTypedArray() + val event = MotionEvent.obtain(1000L, time++, action, pointers.size, properties, coords, 0, 0, 1f, 1f, 0, 0, 0, flags) + return try { target.dispatchTouchEvent(event) } finally { event.recycle() } + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/ui/InputSurfaceViewTest.kt b/app/src/test/java/com/droiddeck/launcher/ui/InputSurfaceViewTest.kt new file mode 100644 index 0000000000..6bf1d9ddaf --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/ui/InputSurfaceViewTest.kt @@ -0,0 +1,103 @@ +package com.droiddeck.launcher.ui + +import android.content.ComponentName +import android.view.MotionEvent +import androidx.activity.ComponentActivity +import android.view.View +import android.widget.FrameLayout +import com.droiddeck.launcher.input.* +import com.droiddeck.launcher.input.JoyConPadTest +import com.droiddeck.launcher.input.PadBridge +import com.droiddeck.launcher.session.SessionState +import org.junit.Assert.assertEquals +import org.junit.Assert.assertTrue +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.Robolectric +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.Shadows.shadowOf +import org.robolectric.annotation.Config +import java.nio.file.Files + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [34], manifest = Config.NONE, instrumentedPackages = ["com.droiddeck.launcher.input.FakeInputWriter"], + shadows = [JoyConPadTest.RecordingWriter::class, SessionInputPointerTest.CompositorShadow::class]) +class InputSurfaceViewTest { + @Test fun customControlsReleaseOnDetachAndWorkWhenTheSameViewReturns() { + val controller = Robolectric.buildActivity(ComponentActivity::class.java).setup() + val activity = controller.get() + val events = mutableListOf() + val bridge = PadBridge(activity.cacheDir) + val router = InputRouter(object : InputOutput { + override fun key(code: Int, down: Boolean) { events += down } + override fun button(code: Int, down: Boolean) {} + override fun pointer(dx: Float, dy: Float) {} + override fun scroll(steps: Int) {} + override fun command(command: InputCommand) {} + override fun requestMotion() {} + }) + bridge.setInputRouter(router) + bridge.setInputProfile(InputProfile("test", elements = mapOf(InputRole.SECONDARY to mapOf("one" to InputAction.Keys(listOf(57)))))) + val root = FrameLayout(activity) + activity.setContentView(root) + val panel = InputSurfaceView(activity, bridge, + listOf(InputSurfaceElement(InputControl.A, .2f, .2f, .2f, .2f, "one"))) + try { + repeat(2) { + root.addView(panel) + panel.measure(View.MeasureSpec.makeMeasureSpec(600, View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(600, View.MeasureSpec.EXACTLY)) + panel.layout(0, 0, 600, 600) + val down = MotionEvent.obtain(0, 1, MotionEvent.ACTION_DOWN, 150f, 150f, 0) + try { assertTrue(panel.dispatchTouchEvent(down)) } finally { down.recycle() } + assertEquals(true, events.last()) + root.removeView(panel) + assertEquals(false, events.last()) + } + assertEquals(listOf(true, false, true, false), events) + } finally { root.removeAllViews(); bridge.stop(); controller.pause().stop().destroy() } + } + + @Test fun keyedDirectionsAreLabelledWithTheirKeysAndOtherActionsAreNot() { + val context = RuntimeEnvironment.getApplication() + val wasd = InputAction.Directions(17, 32, 31, 30, listOf(18, 0, 0, 16)) + assertEquals(listOf((0f to -1f) to "W", (1f to 0f) to "D", (0f to 1f) to "S", (-1f to 0f) to "A"), + InputSurfaceView.directionLabels(context, wasd, cardinalOnly = true)) + assertEquals(listOf("W", "D", "S", "A", "E", "Q"), InputSurfaceView.directionLabels(context, wasd, cardinalOnly = false).map { it.second }) + assertEquals(listOf("W", "A"), InputSurfaceView.directionLabels(context, InputAction.Directions(17, 0, 0, 30), cardinalOnly = false).map { it.second }) + assertTrue(InputSurfaceView.directionLabels(context, null, cardinalOnly = false).isEmpty()) + } + + @Test fun takesTouchAtMatchesWhatATouchDoes() { + val directory = Files.createTempDirectory("deck-panel").toFile() + try { + SessionState.deckPad = false + val app = RuntimeEnvironment.getApplication() + shadowOf(app.packageManager).addActivityIfNotPresent(ComponentName(app, ComponentActivity::class.java)) + val activity = Robolectric.buildActivity(ComponentActivity::class.java).setup().get() + for (pad in listOf(null, PadBridge(directory))) for (portraitDock in listOf(false, true)) for (layout in InputStockLayouts.layouts()) { + val name = "pad=${pad != null} portraitDock=$portraitDock" + val panel = InputSurfaceView(activity, pad, layout.elements) + activity.setContentView(panel) + val (w, h) = if (portraitDock) 720 to 1000 else 1200 to 480 + panel.measure(View.MeasureSpec.makeMeasureSpec(w, View.MeasureSpec.EXACTLY), + View.MeasureSpec.makeMeasureSpec(h, View.MeasureSpec.EXACTLY)) + panel.layout(0, 0, w, h) + var taken = 0 + var missed = 0 + for (x in 0 until w step w / 40) for (y in 0 until h step h / 24) { + val expected = panel.takesTouchAt(x.toFloat(), y.toFloat()) + val down = MotionEvent.obtain(0L, 0L, MotionEvent.ACTION_DOWN, x.toFloat(), y.toFloat(), 0) + assertEquals("$name ($x, $y)", expected, panel.dispatchTouchEvent(down)) + down.recycle() + MotionEvent.obtain(0L, 1L, MotionEvent.ACTION_CANCEL, x.toFloat(), y.toFloat(), 0).also { panel.dispatchTouchEvent(it); it.recycle() } + if (expected) taken++ else missed++ + } + assertTrue("$name: some points land on controls", taken > 0) + assertTrue("$name: some points land between them", missed > 0) + } + } finally { + directory.deleteRecursively() + } + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/ui/InputUiLayoutTest.kt b/app/src/test/java/com/droiddeck/launcher/ui/InputUiLayoutTest.kt new file mode 100644 index 0000000000..9f28c9ccc1 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/ui/InputUiLayoutTest.kt @@ -0,0 +1,130 @@ +package com.droiddeck.launcher.ui + +import android.content.Context +import android.graphics.Bitmap +import android.graphics.Canvas +import android.view.View +import androidx.compose.foundation.layout.BoxWithConstraints +import androidx.compose.material3.Surface +import androidx.compose.runtime.CompositionLocalProvider +import androidx.compose.ui.platform.LocalView +import androidx.compose.ui.semantics.SemanticsActions +import androidx.compose.ui.test.* +import androidx.compose.ui.unit.dp +import com.droiddeck.launcher.R +import com.droiddeck.launcher.input.ControllerPrefs +import com.droiddeck.launcher.input.InputLibraries +import com.droiddeck.launcher.input.InputProfiles +import org.junit.Assert.assertEquals +import com.droiddeck.launcher.input.InputAction +import com.droiddeck.launcher.input.InputControl +import org.junit.Before +import org.junit.Rule +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.annotation.Config +import org.robolectric.annotation.GraphicsMode +import java.io.File + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [28]) +@GraphicsMode(GraphicsMode.Mode.NATIVE) +class InputUiLayoutTest { + @get:Rule val compose = createRobolectricComposeRule() + private lateinit var root: View + @Before fun reset() { + val context = RuntimeEnvironment.getApplication() + for (name in listOf("input-profiles", "input-surfaces", "controller")) context.getSharedPreferences(name, Context.MODE_PRIVATE).edit().clear().commit() + } + + @Test fun layoutThumbnailDoesNotPaintOverItsRowOrAdjacentControls() { + val context = RuntimeEnvironment.getApplication() + val library = InputLibraries.read(context) + val preview = InputLayoutPreviewView(context) + preview.update(library.layout("game", com.droiddeck.launcher.input.InputRole.SECONDARY)!!, com.droiddeck.launcher.input.InputRole.SECONDARY) + preview.measure(View.MeasureSpec.makeMeasureSpec(112, View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(76, View.MeasureSpec.EXACTLY)) + preview.layout(0, 0, 112, 76) + val bitmap = Bitmap.createBitmap(240, 160, Bitmap.Config.ARGB_8888) + bitmap.eraseColor(android.graphics.Color.MAGENTA) + preview.draw(Canvas(bitmap)) + assertEquals(android.graphics.Color.MAGENTA, bitmap.getPixel(150, 30)) + assertEquals(android.graphics.Color.MAGENTA, bitmap.getPixel(30, 120)) + bitmap.recycle() + } + + @Test @Config(qualifiers = "w360dp-h800dp") + fun phoneKeepsLibraryNavigationAndProfileEditingReachable() { + compose.setContent { root = LocalView.current; DroidDeckTheme { Surface { BoxWithConstraints { + CompositionLocalProvider(LocalNarrowPane provides (maxWidth < 600.dp)) { InputSettingsPage({}) } + } } } } + compose.onNodeWithTag("input-profile-desktop").performScrollTo().assertIsDisplayed().performClick() + compose.onNodeWithTag("input-part-physical").performScrollTo().assertIsDisplayed().performClick() + compose.onNodeWithTag("input-binding-a").performScrollTo().assertIsDisplayed().performClick() + compose.onNodeWithTag("input-action-save").assertIsDisplayed() + screenshot("phone-properties") + } + + @Test @Config(qualifiers = "w640dp-h360dp") + fun handheldPropertiesLeaveSaveVisibleAboveScrollableContent() { + compose.setContent { root = LocalView.current; DroidDeckTheme { Surface { InputActionEditor(InputControl.LS, InputAction.Pointer(), "Left stick", {}, {}) } } } + compose.onNodeWithTag("input-action-save").assertIsDisplayed() + compose.onNode(SemanticsMatcher.keyIsDefined(SemanticsActions.SetProgress)).performScrollTo().assertIsDisplayed() + screenshot("handheld-properties") + } + + @Test @Config(qualifiers = "w960dp-h600dp") + fun largeScreenShowsTheProfileLibraryInTheExistingTheme() { + compose.setContent { root = LocalView.current; DroidDeckTheme { Surface { InputSettingsPage({}) } } } + compose.onNodeWithTag("input-profile-game").assertIsDisplayed() + screenshot("profiles") + } + + @Test @Config(qualifiers = "w960dp-h600dp") + fun controllerTabKeepsSetupNavigationWithoutDuplicateEditors() { + val context = RuntimeEnvironment.getApplication() + compose.setContent { + root = LocalView.current + DroidDeckTheme { + CompositionLocalProvider(LocalFrontFocus provides FrontFocus(), LocalNarrowPane provides false) { + FocusGlideHost { + SetupPanel(FrontEndState( + installed = null, ready = false, available = null, busy = false, stage = "", percent = -1, + desktopInstalled = false, offlineAccount = null, offline = false, frameGenLabel = "", + romsDir = null, logsEnabled = false, steamGames = emptyList(), emulators = emptyList(), running = null, + controller = ControllerPrefs.read(context), + ), FrontEndActions( + onPlay = {}, onPlayDesktopUi = {}, onSteamGame = {}, onDesktop = {}, onEmulator = {}, + onRom = {}, onResume = {}, onSteamSettings = {}, onDesktopSettings = {}, + onInstallPackage = {}, onRemovePackage = {}, onRuntime = {}, onFrameGenPick = {}, + onImportLossless = {}, onProtons = {}, onComponents = {}, onPerformance = {}, + onRoms = {}, onFiles = {}, onLogs = {}, onOffline = {}, + onWirelessAdbPair = { _, _, _, _ -> }, onWirelessAdbApply = { _, _, _, _ -> }, + onSetPhantomProcessLimit = { _, _ -> }, controller = ControllerActions({}), + ), onOpenDeveloperOptions = {}, onRequestWirelessAdb = {}, onPairWirelessDebugging = {}) + } + } + } + } + compose.onNodeWithText(context.getString(R.string.setup_tab_controller)).performClick() + compose.onNodeWithTag("input-profile-game").assertIsDisplayed() + compose.onNodeWithText(context.getString(R.string.ctrl_layout)).assertDoesNotExist() + screenshot("controller-tab") + compose.onNodeWithTag("input-profile-game").performClick() + compose.onNodeWithTag("input-part-physical").performScrollTo().performClick() + compose.onNodeWithText(context.getString(R.string.setup_tab_launcher)).assertIsDisplayed() + screenshot("controller-physical") + compose.runOnIdle { assertEquals(InputProfiles.AUTO, InputLibraries.read(context).selected) } + } + + private fun screenshot(name: String) { + compose.runOnIdle { + val image = Bitmap.createBitmap(root.width, root.height, Bitmap.Config.ARGB_8888) + root.draw(Canvas(image)) + val file = File("build/reports/input-ui/$name.png").apply { parentFile?.mkdirs() } + file.outputStream().use { image.compress(Bitmap.CompressFormat.PNG, 100, it) } + image.recycle() + } + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/ui/PhysicalShortcutsDialogTest.kt b/app/src/test/java/com/droiddeck/launcher/ui/PhysicalShortcutsDialogTest.kt index a9afe50287..864eb0ef64 100644 --- a/app/src/test/java/com/droiddeck/launcher/ui/PhysicalShortcutsDialogTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/ui/PhysicalShortcutsDialogTest.kt @@ -66,10 +66,11 @@ class PhysicalShortcutsDialogTest { } } - @Test fun buttonMappingPageOpensPhysicalShortcutAssignments() { + @Test fun inputSettingsPageOpensPhysicalShortcutAssignments() { compose.setContent { - DroidDeckTheme { ControllerMappingPage(emptyMap(), { _, _ -> }, {}, {}) } + DroidDeckTheme { InputSettingsPage(onBack = {}) } } + compose.onNodeWithTag("shortcuts-assign-STEAM").performScrollTo() compose.onNodeWithTag("shortcuts-assign-STEAM").assertExists() compose.onNodeWithTag("shortcuts-assign-QAM").assertExists() compose.onNodeWithTag("shortcuts-assign-DROIDDECK").assertExists() @@ -134,12 +135,12 @@ class PhysicalShortcutsDialogTest { compose.setContent { DroidDeckTheme { PhysicalShortcutRows() } } assign(PhysicalShortcuts.Action.STEAM) send(KeyEvent.KEYCODE_F3, KeyEvent.ACTION_DOWN) - compose.onNodeWithTag("shortcuts-remove").performScrollTo().performClick() + compose.onNodeWithTag("shortcuts-restore-default").performScrollTo().performClick() compose.runOnIdle { assertEquals(PhysicalShortcuts.Action.QAM, PhysicalShortcuts.read(context).single().action) assertTrue(ControllerPrefs.read(context).selectSteam) } - compose.onNodeWithTag("shortcuts-assign-STEAM").assertTextContains(context.getString(R.string.game_settings_default)) + compose.onNodeWithTag("shortcuts-assign-STEAM").assertTextContains(context.getString(R.string.input_shortcut_guide)) } @Test fun retryRecoversFromAMissingReleaseWithoutSavingTheOldButton() { @@ -172,7 +173,7 @@ class PhysicalShortcutsDialogTest { val label = compose.onNodeWithText(context.getString(R.string.drawer_steam_menu)).getUnclippedBoundsInRoot() assertTrue(control.right - control.left < 200.dp) assertTrue("Menu label must not wrap vertically: $label", label.bottom - label.top <= 48.dp) - compose.onNodeWithTag("shortcuts-assign-STEAM").assertTextContains(context.getString(R.string.ctrl_custom)) + compose.onNodeWithTag("shortcuts-assign-STEAM").assertTextContains(button.label) compose.onNodeWithText("${button.label} · ${button.name}").assertExists() } diff --git a/app/src/test/java/com/droiddeck/launcher/ui/SecondScreenBarTest.kt b/app/src/test/java/com/droiddeck/launcher/ui/SecondScreenBarTest.kt new file mode 100644 index 0000000000..4779843153 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/ui/SecondScreenBarTest.kt @@ -0,0 +1,276 @@ +package com.droiddeck.launcher.ui + +import android.hardware.display.DisplayManager +import android.view.Display +import android.view.View +import android.view.ViewGroup +import androidx.compose.runtime.MutableState +import androidx.compose.runtime.currentComposer +import androidx.compose.ui.platform.ComposeView +import androidx.compose.ui.test.assertIsDisplayed +import androidx.compose.ui.test.onNodeWithContentDescription +import androidx.compose.ui.test.onNodeWithTag +import androidx.compose.ui.test.onNodeWithText +import androidx.compose.ui.test.performClick +import com.droiddeck.launcher.R +import com.droiddeck.launcher.SessionActivity +import com.droiddeck.launcher.input.DeckPanel +import com.droiddeck.launcher.input.InputLibraries +import com.droiddeck.launcher.input.InputSurfaceElement +import com.droiddeck.launcher.input.SecondScreenMode +import com.droiddeck.launcher.session.SessionPrefs +import com.droiddeck.launcher.session.SessionService +import com.droiddeck.launcher.session.SessionState +import org.junit.After +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertNotNull +import org.junit.Assert.assertNull +import org.junit.Assert.assertSame +import org.junit.Assert.assertTrue +import org.junit.Before +import org.junit.Rule +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.Robolectric +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.annotation.Config +import org.robolectric.annotation.GraphicsMode +import org.robolectric.util.ReflectionHelpers + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [28], qualifiers = "w800dp-h600dp") +@GraphicsMode(GraphicsMode.Mode.NATIVE) +class SecondScreenBarTest { + @get:Rule val compose = createRobolectricComposeRule() + private val context get() = RuntimeEnvironment.getApplication() + private var previousMode = "" + private var previousDeckPad = false + + @Before fun setUp() { + previousMode = SessionState.mode + previousDeckPad = SessionState.deckPad + context.getSharedPreferences("input-profiles", android.content.Context.MODE_PRIVATE).edit().clear().commit() + SessionPrefs.setDeckPanel(context, DeckPanel.BOTH) + } + + @After fun tearDown() { + SessionState.mode = previousMode + SessionState.deckPad = previousDeckPad + } + + private fun presentation(swap: (() -> Unit)? = {}): SecondScreenPresentation { + val activity = Robolectric.buildActivity(SessionActivity::class.java).get() + val display = activity.getSystemService(DisplayManager::class.java).getDisplay(Display.DEFAULT_DISPLAY) + return SecondScreenPresentation(activity, display, {}, {}, {}, onMenu = {}, onSwap = swap) + } + + private fun descendants(view: View): Sequence = sequence { + yield(view) + if (view is ViewGroup) for (index in 0 until view.childCount) yieldAll(descendants(view.getChildAt(index))) + } + + private fun rootChildren(panel: SecondScreenPresentation): List { + val root: ViewGroup = ReflectionHelpers.getField(panel, "root") + return (0 until root.childCount).map { root.getChildAt(it) } + } + + @Test fun trackpadPanelHasTheBarWithTheKeyboardToggleState() { + SessionState.mode = SessionService.MODE_STEAM + val panel = presentation() + try { + panel.show() + panel.showMode(SecondScreenMode.KEYBOARD_TRACKPAD) + val children = rootChildren(panel) + assertTrue(children[0] is ComposeView) + assertTrue(children[1] is InputSurfaceView) + val bar = panel.barModel(SecondScreenMode.KEYBOARD_TRACKPAD)!! + assertEquals(context.getString(R.string.second_mode_keyboard_trackpad), bar.title) + assertNotNull(bar.onClose); assertNotNull(bar.onMenu); assertNotNull(bar.onSteam); assertNotNull(bar.onQam) + assertNull(bar.tabs) + val keyboard = bar.extras.single() + assertEquals(context.getString(R.string.second_show_keyboard), keyboard.description) + assertFalse(keyboard.active) + ReflectionHelpers.getField>(panel, "keyboardShown").value = true + val shown = panel.barModel(SecondScreenMode.KEYBOARD_TRACKPAD)!!.extras.single() + assertEquals(context.getString(R.string.second_hide_keyboard), shown.description) + assertTrue(shown.active) + } finally { panel.closeControls() } + } + + @Test fun deckPanelHasTheBarWithSwapAndTabsAndProfileControlsKeepTheBar() { + SessionState.mode = SessionService.MODE_STEAM + SessionState.deckPad = true + var swaps = 0 + val panel = presentation { swaps++ } + try { + panel.show() + panel.showMode(SecondScreenMode.DECK_CONTROLS) + assertTrue(rootChildren(panel)[0] is ComposeView) + val bar = panel.barModel(SecondScreenMode.DECK_CONTROLS)!! + assertEquals(context.getString(R.string.second_mode_deck_controls), bar.title) + assertEquals(listOf(context.getString(R.string.swap_screens)), bar.extras.map { it.description }) + bar.extras.single().onClick() + assertEquals(1, swaps) + assertEquals(DeckPanel.entries.map { context.getString(it.label) }, bar.tabs!!.labels) + assertEquals(DeckPanel.BOTH.ordinal, bar.tabs!!.selected) + panel.showMode(SecondScreenMode.CUSTOM_CONTROLS) + val profile = panel.barModel(SecondScreenMode.CUSTOM_CONTROLS)!! + assertEquals(context.getString(R.string.input_profile_controls_mode), profile.title) + assertEquals(listOf(context.getString(R.string.swap_screens)), profile.extras.map { it.description }) + assertNotNull(profile.onClose); assertNotNull(profile.onMenu) + profile.extras.single().onClick() + assertEquals(2, swaps) + assertTrue(rootChildren(panel)[0] is ComposeView) + assertTrue(rootChildren(panel)[1] is InputSurfaceView) + } finally { panel.closeControls() } + val noSwap = presentation(swap = null) + try { + noSwap.show() + noSwap.showMode(SecondScreenMode.DECK_CONTROLS) + assertTrue(noSwap.barModel(SecondScreenMode.DECK_CONTROLS)!!.extras.isEmpty()) + } finally { noSwap.closeControls() } + } + + @Test fun nonSteamSessionsHideSteamAndQuickAccess() { + for (mode in listOf(SessionService.MODE_DESKTOP, SessionService.MODE_RUN)) { + SessionState.mode = mode + SessionState.deckPad = true + val panel = presentation() + try { + panel.show() + for (panelMode in listOf(SecondScreenMode.KEYBOARD_TRACKPAD, SecondScreenMode.DECK_CONTROLS)) { + panel.showMode(panelMode) + val bar = panel.barModel(panelMode)!! + assertNull(bar.onSteam); assertNull(bar.onQam) + assertNotNull(bar.onClose); assertNotNull(bar.onMenu) + } + } finally { panel.closeControls() } + } + val stock = InputLibraries.read(context).layouts.filter { it.id == InputLibraries.DECK || it.id == InputLibraries.TRACKPAD } + assertTrue(stock.all { layout -> layout.elements.none { it.id.startsWith("command:") } }) + } + + @Test fun deckTabsSwitchTheLayoutAndAreRemembered() { + SessionState.mode = SessionService.MODE_STEAM + SessionState.deckPad = true + val library = InputLibraries.read(context) + fun shown(panel: SecondScreenPresentation): List { + val controls = descendants(panel.window!!.decorView).filterIsInstance().single() + return descendants(controls).mapNotNull { it.tag as? InputSurfaceElement }.toList() + } + val first = presentation() + try { + first.show() + first.showMode(SecondScreenMode.DECK_CONTROLS) + assertEquals(library.layouts.single { it.id == DeckPanel.BOTH.layout }.elements, shown(first)) + val controls = descendants(first.window!!.decorView).filterIsInstance().single() + for (panel in listOf(DeckPanel.GRIPS, DeckPanel.LEFT, DeckPanel.RIGHT, DeckPanel.GAMEPAD)) { + first.barModel(SecondScreenMode.DECK_CONTROLS)!!.tabs!!.onSelect(panel.ordinal) + assertEquals(panel, SessionPrefs.deckPanel(context)) + assertEquals(panel.ordinal, first.barModel(SecondScreenMode.DECK_CONTROLS)!!.tabs!!.selected) + assertSame(controls, descendants(first.window!!.decorView).filterIsInstance().single()) + assertEquals(library.layouts.single { it.id == panel.layout }.elements, shown(first)) + } + first.barModel(SecondScreenMode.DECK_CONTROLS)!!.tabs!!.onSelect(DeckPanel.GRIPS.ordinal) + } finally { first.closeControls() } + val second = presentation() + try { + second.show() + second.showMode(SecondScreenMode.DECK_CONTROLS) + assertEquals(DeckPanel.GRIPS.ordinal, second.barModel(SecondScreenMode.DECK_CONTROLS)!!.tabs!!.selected) + assertEquals(library.layouts.single { it.id == DeckPanel.GRIPS.layout }.elements, shown(second)) + second.showMode(SecondScreenMode.KEYBOARD_TRACKPAD) + assertEquals(library.layouts.single { it.id == InputLibraries.TRACKPAD }.elements, shown(second)) + } finally { second.closeControls() } + assertEquals(DeckPanel.GRIPS.bindings, SecondScreenMode.DECK_CONTROLS.panel(context)) + assertEquals(SecondScreenMode.KEYBOARD_TRACKPAD.bindings, SecondScreenMode.KEYBOARD_TRACKPAD.panel(context)) + } + + @Test fun barTabRowSelectsTabs() { + var picked = -1 + compose.setContent { + DroidDeckTheme { + BottomScreenBar(BottomBarModel("Deck", {}, {}, null, null, tabs = deckPanelTabs(context, DeckPanel.BOTH) { picked = it.ordinal })) + } + } + for (panel in DeckPanel.entries) compose.onNodeWithText(context.getString(panel.label)).assertIsDisplayed() + compose.onNodeWithTag("tab-${DeckPanel.LEFT.ordinal}").performClick() + compose.runOnIdle { assertEquals(DeckPanel.LEFT.ordinal, picked) } + } + + @Test fun swappedTopScreenShowsTheTerminalWithItsBar() { + SessionState.mode = SessionService.MODE_STEAM + val activity = Robolectric.buildActivity(SessionActivity::class.java).get() + ReflectionHelpers.getField>(activity, "secondScreenMode\$delegate").value = SecondScreenMode.TERMINAL + val method = SessionActivity::class.java.getDeclaredMethod("SwappedTopPanel", androidx.compose.runtime.Composer::class.java, Int::class.javaPrimitiveType) + method.isAccessible = true + compose.setContent { DroidDeckTheme { method.invoke(activity, currentComposer, 0) } } + compose.onNodeWithText(context.getString(R.string.second_mode_terminal)).assertIsDisplayed() + compose.onNodeWithContentDescription(context.getString(R.string.drawer_keyboard)).assertIsDisplayed() + compose.onNodeWithContentDescription(context.getString(R.string.swap_screens)).assertIsDisplayed() + compose.onNodeWithTag("bottom-bar-steam").assertIsDisplayed() + } + + @Test fun swappedTopScreenDeckPanelHasTabs() { + SessionState.mode = SessionService.MODE_DESKTOP + SessionState.deckPad = true + val activity = Robolectric.buildActivity(SessionActivity::class.java).get() + ReflectionHelpers.getField>(activity, "secondScreenMode\$delegate").value = SecondScreenMode.DECK_CONTROLS + val method = SessionActivity::class.java.getDeclaredMethod("SwappedTopPanel", androidx.compose.runtime.Composer::class.java, Int::class.javaPrimitiveType) + method.isAccessible = true + compose.setContent { DroidDeckTheme { method.invoke(activity, currentComposer, 0) } } + compose.onNodeWithText(context.getString(R.string.second_mode_deck_controls)).assertIsDisplayed() + compose.onNodeWithTag("bottom-bar-tabs").assertIsDisplayed() + compose.onNodeWithTag("bottom-bar-steam").assertDoesNotExist() + compose.onNodeWithTag("bottom-bar-close").assertIsDisplayed() + } + + @Test fun swappedTopScreenProfileControlsHaveTheBarWithSwap() { + SessionState.mode = SessionService.MODE_STEAM + val activity = Robolectric.buildActivity(SessionActivity::class.java).get() + ReflectionHelpers.getField>(activity, "secondScreenMode\$delegate").value = SecondScreenMode.CUSTOM_CONTROLS + val method = SessionActivity::class.java.getDeclaredMethod("SwappedTopPanel", androidx.compose.runtime.Composer::class.java, Int::class.javaPrimitiveType) + method.isAccessible = true + compose.setContent { DroidDeckTheme { method.invoke(activity, currentComposer, 0) } } + compose.onNodeWithText(context.getString(R.string.input_profile_controls_mode)).assertIsDisplayed() + compose.onNodeWithContentDescription(context.getString(R.string.swap_screens)).assertIsDisplayed() + compose.onNodeWithTag("bottom-bar-close").assertIsDisplayed() + } + + @Test fun theBottomPanelWaitsUnderAnOpenInputEditorAndReturnsWhenItCloses() { + SessionState.mode = SessionService.MODE_STEAM + val id = org.robolectric.shadows.ShadowDisplayManager.addDisplay("w1280dp-h960dp") + val activity = Robolectric.buildActivity(SessionActivity::class.java).get() + val displays = activity.getSystemService(DisplayManager::class.java) + org.robolectric.Shadows.shadowOf(displays.getDisplay(id)).setFlags(Display.FLAG_PRESENTATION) + ReflectionHelpers.setField(activity, "displayManager", displays) + ReflectionHelpers.setField(activity, "started", true) + ReflectionHelpers.getField>(activity, "selectedSecondScreenDisplay\$delegate").value = id + val mode = ReflectionHelpers.getField>(activity, "secondScreenMode\$delegate") + mode.value = SecondScreenMode.KEYBOARD_TRACKPAD + fun panel() = ReflectionHelpers.getField(activity, "secondScreenPresentation") + fun show() = ReflectionHelpers.callInstanceMethod(activity, "showSecondScreen", + ReflectionHelpers.ClassParameter.from(SecondScreenMode::class.java, SecondScreenMode.KEYBOARD_TRACKPAD)) + activity.inputWindowShown(Display.DEFAULT_DISPLAY) + activity.inputWindowShown(id) + activity.inputWindowShown(id) + show() + assertNull(panel()) + assertEquals(SecondScreenMode.KEYBOARD_TRACKPAD, mode.value) + activity.inputWindowClosed(id) + assertNull(panel()) + activity.inputWindowClosed(id) + try { + assertEquals(id, panel()!!.display.displayId) + assertTrue(panel()!!.isShowing) + } finally { panel()?.closeControls() } + } + + @Test fun unfoldedControlsGetTheSessionBar() { + SessionState.mode = SessionService.MODE_STEAM + val activity = Robolectric.buildActivity(SessionActivity::class.java).get() + assertTrue(ReflectionHelpers.callInstanceMethod(activity, "unfoldedBar") is ComposeView) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/ui/SessionSecondScreenTest.kt b/app/src/test/java/com/droiddeck/launcher/ui/SessionSecondScreenTest.kt index 9891ac1b0d..dc2ff1cc6c 100644 --- a/app/src/test/java/com/droiddeck/launcher/ui/SessionSecondScreenTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/ui/SessionSecondScreenTest.kt @@ -5,12 +5,14 @@ import android.hardware.input.InputManager import android.view.Display import android.view.InputDevice import android.view.KeyEvent +import android.view.MotionEvent import android.view.View import android.view.ViewGroup import android.widget.TextView import androidx.compose.ui.test.* import androidx.compose.ui.semantics.SemanticsProperties.StateDescription import androidx.compose.runtime.MutableState +import androidx.compose.ui.geometry.Offset import com.droiddeck.launcher.R import com.droiddeck.launcher.SessionActivity import com.droiddeck.launcher.gpu.FrameGen @@ -21,6 +23,14 @@ import com.droiddeck.launcher.input.PadBridgeRoutingTest import com.droiddeck.launcher.session.SessionService import com.droiddeck.launcher.session.SessionState import org.junit.After +import com.droiddeck.launcher.input.InputLibraries +import com.droiddeck.launcher.input.InputRole +import com.droiddeck.launcher.input.InputElementKind +import com.droiddeck.launcher.input.InputSurfaceElement +import com.droiddeck.launcher.input.InputSurfaceStyle +import com.droiddeck.launcher.input.InputControl +import org.junit.Assert.assertSame +import org.junit.Assert.assertNotNull import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse import org.junit.Assert.assertTrue @@ -61,6 +71,28 @@ class SessionSecondScreenTest { @Test fun steamMenuKeepsTheExistingTrackpadAndTerminal() = checkModes(steam = true) + @Test fun desktopQuickMenuChangesTheRunningDesktopLayout() = checkQuickSelection(steam = false, desktop = true, direct = true) + + @Test fun steamQuickMenuLeavesTheDesktopSelectionAlone() = checkQuickSelection(steam = true, desktop = false, direct = false) + + @Test fun directGameQuickMenuLeavesTheDesktopSelectionAlone() = checkQuickSelection(steam = false, desktop = false, direct = true) + + @Test fun closingTheDockedDrawerKeepsTheSelectedControlsPanel() { + SessionState.mode = SessionService.MODE_STEAM + var closes = 0 + var selected = SecondScreenMode.CUSTOM_CONTROLS + compose.setContent { + DroidDeckTheme { + SessionDrawer(true, SessionDrawerPage.SECOND_SCREEN, false, {}, + actions(true, listOf(SecondScreenDisplay(1, "Bottom screen")), + onClose = { closes++ }, select = { selected = it }), docked = true) + } + } + compose.onNodeWithContentDescription(context.getString(R.string.common_close)).performClick() + assertEquals(1, closes) + assertEquals(SecondScreenMode.CUSTOM_CONTROLS, selected) + } + @Test fun desktopWithoutASecondDisplayFallsBackToController() { SessionState.mode = SessionService.MODE_DESKTOP var page = SessionDrawerPage.SECOND_SCREEN @@ -104,7 +136,7 @@ class SessionSecondScreenTest { SessionState.mode = SessionService.MODE_STEAM val activity = Robolectric.buildActivity(SessionActivity::class.java).get() val display = activity.getSystemService(DisplayManager::class.java).getDisplay(Display.DEFAULT_DISPLAY) - val panel = SecondScreenPresentation(activity, display, { _, _, _, _ -> }, { _, _ -> }, {}, {}, {}, {}) + val panel = SecondScreenPresentation(activity, display, {}, {}, {}) try { panel.show() panel.showMode(SecondScreenMode.KEYBOARD_TRACKPAD) @@ -123,6 +155,74 @@ class SessionSecondScreenTest { } } + @Test fun profileRefreshLeavesTerminalAndDashboardIntact() { + val activity = Robolectric.buildActivity(SessionActivity::class.java).get() + val display = activity.getSystemService(DisplayManager::class.java).getDisplay(Display.DEFAULT_DISPLAY) + val panel = SecondScreenPresentation(activity, display, {}, {}, {}, dashboard = { androidx.compose.material3.Text("Dashboard") }) + try { + panel.show() + for (mode in listOf(SecondScreenMode.TERMINAL, SecondScreenMode.DASHBOARD)) { + panel.showMode(mode) + val root = panel.window!!.decorView + val before = descendants(root).toList() + val terminal: Any? = ReflectionHelpers.getField(panel, "terminalPanel") + panel.refreshControls() + assertEquals(before, descendants(root).toList()) + assertSame(terminal, ReflectionHelpers.getField(panel, "terminalPanel")) + } + } finally { panel.closeControls() } + } + + @Test fun profileRefreshUpdatesControlsWithoutClosingTheFieldKeyboard() { + SessionState.mode = SessionService.MODE_STEAM + val activity = Robolectric.buildActivity(SessionActivity::class.java).get() + val display = activity.getSystemService(DisplayManager::class.java).getDisplay(Display.DEFAULT_DISPLAY) + var closed = 0 + val panel = SecondScreenPresentation(activity, display, {}, {}, {}, onKeyboardClosed = { closed++ }) + try { + panel.show() + panel.showMode(SecondScreenMode.CUSTOM_CONTROLS) + panel.requestKeyboard(forField = true) + val host: Any = ReflectionHelpers.getField(panel, "keyboardHost") + val shown: MutableState = ReflectionHelpers.getField(panel, "keyboardShown") + shown.value = true + ReflectionHelpers.setField(panel, "imeSeen", true) + val controls = descendants(panel.window!!.decorView).filterIsInstance().single() + val element = InputSurfaceElement(InputControl.A, .3f, .2f, .2f, .2f, "updated") + InputLibraries.update(activity) { library -> + val layout = library.activeLayout(false, InputRole.SECONDARY)!! + library.withLayout(layout.copy(elements = listOf(element), style = InputSurfaceStyle(opacity = 35))) + } + panel.refreshControls() + assertSame(controls, descendants(panel.window!!.decorView).filterIsInstance().single()) + val child = descendants(controls).single { it.tag == element } + assertEquals(.35f, child.alpha, .001f) + assertSame(host, ReflectionHelpers.getField(panel, "keyboardHost")) + assertTrue(panel.fieldKeyboardOpen) + assertTrue(shown.value) + assertTrue(ReflectionHelpers.getField(panel, "imeSeen")) + assertEquals(0, closed) + } finally { panel.closeControls() } + } + + @Test fun profileRefreshPreservesTheLowerScreenChatPanel() { + val activity = Robolectric.buildActivity(SessionActivity::class.java).get() + val display = activity.getSystemService(DisplayManager::class.java).getDisplay(Display.DEFAULT_DISPLAY) + var lost = 0 + val panel = SecondScreenPresentation(activity, display, {}, {}, {}) + try { + panel.show() + panel.showMode(SecondScreenMode.CUSTOM_CONTROLS) + panel.showChat(onLost = { lost++ }) { androidx.compose.material3.Text("Chat draft") } + val chat: View? = ReflectionHelpers.getField(panel, "chatView") + assertNotNull(chat) + panel.refreshControls() + assertSame(chat, ReflectionHelpers.getField(panel, "chatView")) + assertTrue(descendants(panel.window!!.decorView).any { it === chat }) + assertEquals(0, lost) + } finally { panel.closeControls() } + } + @Test @Config(sdk = [34], shadows = [PadBridgeRoutingTest.RegisteredKeyEvent::class]) fun controllerBumpersReachTheSecondScreenInBothSessionModes() { @@ -208,6 +308,43 @@ class SessionSecondScreenTest { compose.onNodeWithText(context.getString(R.string.mode_mic)).assertDoesNotExist() } + private fun checkQuickSelection(steam: Boolean, desktop: Boolean, direct: Boolean) { + SessionState.mode = if (desktop) SessionService.MODE_DESKTOP else if (steam) SessionService.MODE_STEAM else SessionService.MODE_RUN + val original = InputLibraries.update(context) { it.chooseLayout("desktop", InputRole.SECONDARY, InputLibraries.DECK) } + val activity = Robolectric.buildActivity(SessionActivity::class.java).get() + val display = activity.getSystemService(DisplayManager::class.java).getDisplay(Display.DEFAULT_DISPLAY) + val panel = SecondScreenPresentation(activity, display, {}, {}, {}) + val open = androidx.compose.runtime.mutableStateOf(true) + try { + panel.show() + panel.showMode(SecondScreenMode.CUSTOM_CONTROLS) + compose.setContent { + DroidDeckTheme { + SessionDrawer(open.value, SessionDrawerPage.CONTROLLER, false, {}, + actions(steam, emptyList(), desktop = desktop, direct = direct, + onInputSelection = { panel.refreshControls() }, onClose = { open.value = false })) + } + } + compose.onNodeWithTag("setting-input-profile").performScrollTo().performClick() + compose.onNodeWithText("Desktop").performScrollTo().performClick() + compose.onNodeWithTag("input-profile-expand").performScrollTo().performClick() + compose.onNodeWithTag("setting-input-part-SECONDARY").performScrollTo().performClick() + compose.onNodeWithText("Trackpad + keyboard").performScrollTo().performClick() + compose.onNode(hasContentDescription(context.getString(R.string.common_close)) and + SemanticsMatcher.expectValue(androidx.compose.ui.semantics.SemanticsProperties.Role, androidx.compose.ui.semantics.Role.Button)).performClick() + compose.runOnIdle { + val updated = InputLibraries.read(context) + assertEquals("desktop", updated.selection(desktop).profile) + assertEquals(InputLibraries.TRACKPAD, updated.activeLayout(desktop, InputRole.SECONDARY)?.id) + assertEquals(original.selection(!desktop), updated.selection(!desktop)) + assertEquals(original.profiles, updated.profiles) + val controls = descendants(panel.window!!.decorView).filterIsInstance().single() + val pads = descendants(controls).mapNotNull { it.tag as? InputSurfaceElement }.filter { it.kind == InputElementKind.TRACKPAD }.toList() + assertEquals(1, pads.size) + } + } finally { panel.closeControls() } + } + private fun checkModes(steam: Boolean) { SessionState.mode = if (steam) SessionService.MODE_STEAM else SessionService.MODE_DESKTOP val selected = mutableListOf() @@ -217,11 +354,12 @@ class SessionSecondScreenTest { SessionDrawer(true, SessionDrawerPage.SECOND_SCREEN, false, {}, actions(steam, displays) { selected += it }) } } - for (mode in listOf(SecondScreenMode.KEYBOARD_TRACKPAD, SecondScreenMode.TERMINAL)) { + val modes = listOf(SecondScreenMode.KEYBOARD_TRACKPAD, SecondScreenMode.TERMINAL, SecondScreenMode.CUSTOM_CONTROLS) + for (mode in modes) { compose.onNodeWithText(context.getString(mode.label)).performScrollTo().assertIsDisplayed().performClick() } compose.onNodeWithText(context.getString(SecondScreenMode.DECK_CONTROLS.label)).assertDoesNotExist() - assertEquals(listOf(SecondScreenMode.KEYBOARD_TRACKPAD, SecondScreenMode.TERMINAL), selected) + assertEquals(modes, selected) } private fun descendants(view: View): Sequence = sequence { @@ -231,15 +369,18 @@ class SessionSecondScreenTest { private fun actions(steam: Boolean, displays: List, micAvailable: Boolean = false, micEnabled: Boolean = false, onMic: (Boolean) -> Unit = {}, + desktop: Boolean = !steam, direct: Boolean = false, onInputSelection: (InputRole?) -> Unit = {}, + onClose: () -> Unit = {}, select: (SecondScreenMode) -> Unit = {}) = - DrawerActions(steam = steam, isHomeApp = false, androidApps = emptyList(), hudOn = false, + DrawerActions(steam = steam, desktop = desktop, direct = direct, onInputSelection = onInputSelection, + isHomeApp = false, androidApps = emptyList(), hudOn = false, micAvailable = micAvailable, micEnabled = micEnabled, onMicEnabled = onMic, frameGen = FrameGen.Mode.OFF, lossless = Lossless.State.NONE, oscMode = "never", - onScreenButtonsVisible = false, suspendPolicy = "auto", backActionsInverted = false, + suspendPolicy = "auto", backActionsInverted = false, touchMode = "auto", touchAuto = "trackpad", fexPreset = "default", secondScreenMode = SecondScreenMode.NONE, secondScreenDisplays = displays, selectedSecondScreenDisplay = 1, onHud = {}, onFrameGenPick = {}, onImportLossless = {}, onKeyboard = {}, onHardwareKeyboard = {}, onSteamMenu = null, onQam = null, onOsc = {}, onSuspendPolicy = {}, onBackActionsInverted = {}, onTouch = {}, onFexPreset = {}, onSecondScreenMode = select, onSecondScreenDisplay = {}, - onLaunchAndroidApp = { _, _ -> }, onBackground = {}, onShareLogs = {}, onStop = {}, onClose = {}) + onLaunchAndroidApp = { _, _ -> }, onBackground = {}, onShareLogs = {}, onStop = {}, onClose = onClose) } diff --git a/docs/development/ea-store.md b/docs/development/ea-store.md new file mode 100644 index 0000000000..89a345c80f --- /dev/null +++ b/docs/development/ea-store.md @@ -0,0 +1,31 @@ +# EA store + +EA games install, update and start without the EA app. The research behind it is in +[ea-ubisoft-stores.md](ea-ubisoft-stores.md). + +## Pieces + +| Piece | Where | What it does | +|---|---|---| +| Sign-in | `stores/ea/EaAuth.kt`, `EaLogin` in `EaBackend.kt` | `JUNO_PC_CLIENT` OAuth with PKCE in the store login WebView; the code comes back on EA's `qrc:///html/login_successful.html` redirect. Tokens refresh with the refresh token. | +| Library | `EaApi.kt`, `EaLibrary.kt` | Owned games (`getPreloadedOwnedGames`, EA storefront), their offers (`getLegacyCatalogDefs`) and art (`GameImages`, SteamGridDB when EA has no portrait). Editions of one game collapse onto the base offer; trials, DLC and non-downloadable offers are left out. | +| Queries | `app/src/main/resources/ea/graphql/*.gql` | EA's own query texts, byte for byte. The gateway matches persisted queries by SHA-256, so these files must not be reformatted (`-text` in `.gitattributes`). | +| Downloads | Rust `src/ea/` in `libdroiddeckstores.so`, `EaInstaller.kt` | A build is one ZIP on EA's CDN. The engine reads its central directory with ranged reads, keeps files whose size and CRC match, and streams every other entry through inflate into place. `.droiddeck-ea-files` records what it placed, so a resume or update does not re-hash those files; files a new build drops are removed. | +| Updates | `EaUpdates` in `EaBackend.kt` | The game page compares the installed version with the offer's live `gameVersion` (every 30 minutes at most). Update runs the same install, which only fetches changed files. | +| Launch, guest | `droiddeck-store-launch` (`prepare_ea`) | Creates the prefix, reads the machine as Wine reports it (`wmic`, `vol`, `hostname`; cached in `pfx/.droiddeck-ea-hw.json`), writes EA app presence keys and the game's `Install Dir` keys in both registry views, runs the build's Touchup once per installed build, then asks the app for the session and writes any licence files. | +| Launch, app | `EaLaunch.kt`, `EaOoa.kt`, `EaLsx.kt` | Answers `ea-prepare`: starts the LSX endpoint on `127.0.0.1:3216`, mints the short-lived opaque token, writes `.droiddeck-ea-env` (read once and deleted by the launcher `.bat`), and for games with a `Core` folder requests an online-activation licence for the machine hash. | +| LSX | `EaLsx.kt` | The local endpoint EA's SDK talks to instead of the EA app: challenge, AES session key, config, profile, settings, game info, auth codes for the game's own client ids, licences with Denuvo tokens, entitlements. Friends, presence and the overlay answer empty. | + +## Machine hash + +The licence names a hash of the machine the game's activation layer computes itself. The hash +version follows the activation library's product version (`Core/activation.dll` or `awc.dll`): +5.x → 4, 4.7.6+ → 3, 4.7.x → 2, 4.x → 1. Versions 2 and 3 also need CPUID leaf 1 ECX, which Wine's +WMI does not expose, so those use `00000000` unless the probe learns it. + +## Not done yet + +- Cloud saves (`cloudsync.juno.ea.com`). +- Steam copies of EA games still install the EA app (#2036). +- Pre-DiP games (no `installerdata.xml`) fall back to the exe picker. +- Javelin anti-cheat games do not run under Proton. diff --git a/docs/development/ea-ubisoft-stores.md b/docs/development/ea-ubisoft-stores.md new file mode 100644 index 0000000000..4d13710a3d --- /dev/null +++ b/docs/development/ea-ubisoft-stores.md @@ -0,0 +1,245 @@ +# EA app and Ubisoft Connect as stores: research and plan + +Research as of 2026-11-07, against `origin/main` at `914b3588e`. Nothing here is implemented yet. + +## Summary + +Both stores can follow the path Battle.net took (#979, then #1602): first the vendor client in its own +Proton prefix, then more of it done by DroidDeck itself. Unlike Battle.net, both now have open-source +clients that already handle sign-in, library and downloads without the vendor app. So the second step +can go further than Battle.net's did. + +| | EA | Ubisoft | +|---|---|---| +| Sign-in without the client | Yes: OAuth `JUNO_PC_CLIENT` in a WebView | Yes: UbiServices session (ticket + remember-me) through Ubisoft's WebAuth page | +| Owned library without the client | Yes: juno GraphQL `ownedGameProducts` | Yes: Demux ownership service (protobuf over TLS), or the GraphQL library | +| Downloads without the client | Yes: a build is one ZIP on EA's CDN, read with HTTP Range per entry (Maxima) | Yes: Demux download service → manifest → zstd/deflate slices (Optima, ubi-cli-mvp) | +| Launch without the client | **Yes, legitimately**: LSX server on `127.0.0.1:3216` plus a real OOA licence from `proxy.novafusion.ea.com` (Maxima) | **Only by replacing Ubisoft's DRM DLLs** (Optima, Re0xCat loaders). Don't ship that. Modern titles need the real Connect anyway | +| Cloud saves without the client | Yes: `cloudsync.juno.ea.com` (Maxima) | Not researched; Connect handles them | +| Effectively out of scope | Javelin kernel anti-cheat titles (Battlefield, FC, Apex) | Kernel AC and online-only titles that misbehave under Proton | + +**Recommendation** + +- **EA should go fully native in-app**: sign-in, library, downloads, launch and cloud saves in Kotlin + and Rust, with no EA app in the prefix. The EA app stays as an optional fallback, like the Epic + launcher option (`EpicClient`). The LSX and licence half also fixes Steam's EA titles, which today + install the whole EA app (#2036) just to answer `link2ea://`. +- **Ubisoft should be hybrid**: native sign-in, library and downloads in Rust, then **register the + files with the real Ubisoft Connect and launch through `uplay://`**. That means a Connect prefix + managed the way Battle.net's is. +- Ubisoft's launch step is the biggest open question (see "Unknowns"). If Connect can't be made to + adopt natively downloaded files cleanly, start with the Connect-driven download, as #979 did for + Battle.net. + +## What DroidDeck already has, and how much of it each store reuses + +| Existing piece | Where | EA | Ubisoft | +|---|---|---|---| +| `Store` enum + `StoreBackend` (signIn, syncLibrary, loadShelves, install, uninstall) | `stores/Store.kt`, `stores/StoresState.kt:355` | Reuse as is: new `EA` entry | Reuse as is: new `UBISOFT` entry | +| WebView sign-in with redirect capture (`StoreLoginActivity.Flow`) | `stores/LoginFlows.kt`, `StoreLoginActivity.kt` | Reuse: the redirect is `qrc:///html/login_successful.html?code=…`, caught in `isRedirect` | Reuse: capture the `ubi_v1` ticket from the WebAuth page, then exchange it for a full session | +| Keystore-backed account store | `stores/StoreAccounts.kt`, `CredentialCipher.kt` | Reuse | Reuse | +| Rust store engine `libdroiddeckstores.so` (JNI, `fetch_core.rs`, hand-rolled protobuf in `amazon/proto.rs`) | `app/src/main/rust/stores` | Reuse: ZIP central directory + Range fetch + inflate (`flate2` is already a dependency) | Reuse `fetch_core`. **New:** a TLS socket client for Demux, a protobuf schema, and a zstd decoder (`ruzstd` is pure Rust; the `zstd` crate needs `cc`, which the crate already has) | +| Download queue: pause, resume, cancel, tiers, Downloads tab | `stores/download/*` | Reuse | Reuse | +| Install roots: internal, other storage, Games folder | `StoreInstallRoot.kt` | Reuse | Reuse | +| Cloud saves: plan, paths, transport | `stores/CloudSaves.kt`, `CloudPlan.kt`, `CloudSavePaths.kt` | Reuse with a new EA transport | Later, or leave to Connect | +| Vendor client in its own prefix, a guest Python driver, a headless helper under gamescope, FEX choice, memory policy | `runtime/BattleNet.kt`, `tools/linuxfs/overlay/usr/local/bin/droiddeck-battlenet` | Fallback only | **Needed**: `droiddeck-ubisoft` modelled on `droiddeck-battlenet` | +| "Use the vendor launcher instead" option | `stores/epic/EpicClient*.kt` | Pattern for the EA app fallback | Pattern for "Open Ubisoft Connect" | +| Steam install-script seeding that lets EA app and Ubisoft Connect installers run | `droiddeck-seed-redists` (#1449, #2036) | Replaced by the LSX stub for Steam EA titles | Unchanged | +| Ubisoft registry handoff for Ubisoft titles bought on Epic | open PR #1445 | n/a | Same registry key (`HKLM\SOFTWARE\WOW6432Node\Ubisoft\Launcher\Installs\\InstallDir`); merge or share it | +| Library, Steam shortcuts, art, game page, sidecars | `frontend/Library.kt`, `SteamLiveShortcuts.kt`, `StoreArt.kt`, `StoreGameSidecar.kt` | Reuse | Reuse | +| Clean-room process for store modules | `docs/development/epic-cleanroom.md`, `amazon-cleanroom.md` | Use: spec from Maxima, then implement from the spec | Use: spec from Optima/ubi-cli-mvp/YoobieRE | + +Genuinely new engineering: + +1. **An LSX server (EA).** EA's local SDK protocol is XML over TCP on 3216, with a challenge handshake, + GetConfig, GetProfile, GetGameInfo, license and entitlement requests. Add a Wine-side stub registered + as the `link2ea://` / `origin2://` handler with EA app presence keys, plus OOA licence requests. + This half is new; the other EA parts are the familiar store pattern. +2. **The EA machine hash.** Licences are bound to a `machineHash` built from WMI/BIOS/disk/CPUID + values. They have to match what the game computes inside Wine under FEX on the phone. + Nobody has shown that on Android yet, so it has to be measured on device. +3. **A Demux client (Ubisoft).** This is a TLS socket to `dmx.upc.ubisoft.com` speaking protobuf, with + patch negotiation, auth, ownership tokens and download-service URL signing. No store we have uses + a socket protocol. +4. **A way for Connect to adopt files (Ubisoft).** Connect keeps its own DPAPI-encrypted sign-in, tied + to the Wine `MachineGuid`. Our session can't be handed to it (ubi-cli-mvp checked). Connect also has + to consider the game installed before `uplay://launch` works. + +## Prior art + +### EA + +| Project | Licence | State | What it gives us | +|---|---|---|---| +| [ArmchairDevelopers/Maxima](https://github.com/ArmchairDevelopers/Maxima) (Rust, `maxima-lib`) | GPL-3.0 | Pre-alpha by its own README; last commit 2026-01; open issues through 2026-11. Backs KYBER | The whole stack: OAuth (`JUNO_PC_CLIENT`, localhost 31033 redirect), juno GraphQL (`.gql` files for owned games, `downloadUrl`, `availableBuilds`, achievements), the ZIP/Range downloader, OOA licences (`ooa/mod.rs`, `machineHash`), the LSX server (`lsx/`), cloud sync, the hardware hash (`auth/hardware.rs`), Wine registry setup. Unsupported: Battlefield 3/4 (Battlelog), pre-DiP games (Dead Space 2, BFBC2), DLC installs | +| [Maxima #71](https://github.com/ArmchairDevelopers/Maxima/issues/71) | | 2026-11 | Games never connect to LSX unless `HKLM\SOFTWARE\WOW6432Node\Origin` is right, and `INSTALLED_LANGUAGE` must be answered. Check ours against it | +| [Maxima #49](https://github.com/ArmchairDevelopers/Maxima/issues/49) | | | ARM64: the hash must match what `Activation.dll` reads from CPUID under Rosetta/Box64/FEX. Directly our problem | +| unifideck `docs/feasibility/ea.md` | GPL-3.0 | 2026-07 | EA app installer runs unattended (`/quiet`), login renders on GE-Proton11-1, `origin2://game/launch?offerIds=&autoDownload=1` is the install idiom, ~2.5 GB client, aggressive self-update | +| Lutris `services/ea_app.py` | GPL-3.0 | Maintained | juno queries, `installerdata.xml` scan for installed games | +| BellezaEmporium/galaxy-integration-ead | **none** | Maintained | Facts only: OAuth params, `pc_sign` fallback | +| erri120/GameFinder wiki | GPL-3.0 | | Encrypted `IS` file format (only for reading EA app installs) | +| Heroic | | wontfix #352 | No EA support beyond Epic-hosted EA titles | + +### Ubisoft + +| Project | Licence | State | What it gives us | +|---|---|---|---| +| [xXJSONDeruloXx/ubi-cli-mvp](https://github.com/xXJSONDeruloXx/ubi-cli-mvp) (yours, TypeScript) | MIT | 2026-07 | The best-tested data plane: session login with `Ubi-Challenge` replay, `ubi_2fa_v1`, refresh; Demux ownership, ownership tokens and signed URLs; manifest decode; slice layouts (`slices_v3//` and flat `slices/`); zstd and both deflates; integrity and path safety. On the Connect side: `connect-install` (`uplay://install/`), **`connect-seed`** (let Connect start the official download, pause, exit, then fill the staging area with verified files), remembered-auth migration between prefixes, `ubi play` via `uplay://`. Docs: `research.md`, `validation.md`, `DEMUX_JOURNAL.md`, `optima-comparison.md` | +| [Starkka15/Optima](https://github.com/Starkka15/Optima) (Rust) | GPL-3.0 | 0.2.0, 2026-08 | Rust version of the same data plane (`auth.rs`, `demux.rs`, `install.rs`, `proto/*.proto`), plus launcherless launch through replacement `uplay_r1_loader` / `ubiorbitapi_r2_loader` DLLs, a self-signed certificate that mimics Ubisoft's (imported into the prefix's Root store so Watch_Dogs accepts it), and an EAX shim. Powers GameVault's Ubisoft extension | +| YoobieRE: `ubisoft-demux-node`, `ubisoft-dmx-decode`, `ubisoft-manifest-downloader`, `uplay-install-reverse`, `product-db-archive` | mixed or none | 2022–2024 | The original protocol reversing that both of the above build on | +| Re0xCat `uplay-r1-loader`, `ubiorbitapi-r2-loader` | | | The DRM-DLL replacements (see Risks) | +| [mubaraknumann/unifideck](https://github.com/mubaraknumann/unifideck) `stores/ubisoft/` | GPL-3.0 | Shipping, active | The full Connect-wrapper archetype in Python: three-prefix model (auth / template / per-game, shared `MachineGuid`), credential capture and fan-out, parsing Connect's binary `configurations` and `ownership` catalogs, leveldb `spaceId ↔ ubisoftConnectGameId`, a manual-install watcher, `uplay://launch//0`, session capture after exit. `docs/ubisoft-store-spec.md` is a good map of what breaks | +| Heroic | | | Installs Ubisoft Connect automatically for Ubisoft titles from Epic; no Ubisoft store | +| DroidDeck #1445 (open), #1449, #2286 / #2295 / #2317 / #2292 / #2050 | | | Our own Ubisoft-under-Proton knowledge: the `Installs` registry key, `UplayLaunch.exe` being a stub, the bundled `UplayInstaller.exe`, legacy installer layouts, SD-card staging, helper processes during the exit watch | + +Licences: Maxima, Optima, unifideck and Lutris are GPL-3.0, which is compatible with +DroidDeck (GPL-3.0). ubi-cli-mvp is MIT. Even so, the Epic and Amazon modules were written clean-room, +so do the same here: write the spec from these sources, implement from the spec, and credit them in +the README the way #1602 does. + +## Design + +### EA + +```text +StoreLoginActivity (OAuth JUNO_PC_CLIENT) ─► EaAccount (access + refresh token, persona) +EaLibrary ── juno GraphQL ownedGameProducts / legacyOffers ─► CatalogItem(offerId, contentId, slug) +EaInstaller (DownloadQueue job) ── downloadUrl ─► build ZIP ─► Rust: central directory, Range per entry, inflate, CRC + └─ writes __Installer/installerdata.xml as EA's own layout expects +Launch: droiddeck-ea (guest) ── registers stub for link2ea:// origin2:// + Origin/EA Desktop keys + app-side EaLsxServer on 3216 ◄── game (OriginSDK / EA SDK) + before launch: OOA licence (novafusion, machineHash) → ProgramData/Electronic Arts/EA Services/License + before/after: cloudsync pull / push through CloudSaves +``` + +- **Where the LSX server runs.** In the Android app. DroidDeck's guest shares + the network namespace, so an app-side server on 127.0.0.1:3216 is reachable from Wine. Keeping it + in Kotlin or Rust, not a guest daemon, matches how DroidDeck owns other store logic. Check this + against proot networking first. +- **The stub.** A protocol handler for `link2ea://` / `origin2://` that hands the request to DroidDeck. +- **Steam's EA titles.** Use the same stub and LSX path, keyed by `link2ea://` in the launch config, + plus entitlement refresh and Steam account linking. #2036's EA app install becomes + the fallback. +- **The EA app fallback.** Its own prefix (a `compatdata` id like Battle.net's `2596113170`), installed + with `EAappInstaller.exe /quiet`, opened from the store row. That's for titles the LSX path can't + start (pre-DiP, Battlelog) and for buying games. + +### Ubisoft + +```text +StoreLoginActivity (Ubisoft WebAuth) ─► ticket ─► full session (expiry, rememberMe); refresh ~3 h +UbisoftLibrary ── Demux ownership ─► products + config YAML (name, exe, uplay id, space id) +UbisoftInstaller (DownloadQueue job) ── Rust Demux: ownership token → download_service urlReq + → manifest (356-byte header, zlib) → slices (zstd/deflate) → files, verified +Register + launch: droiddeck-ubisoft (guest, like droiddeck-battlenet) + Connect in its own prefix, signed in once in its own window (DPAPI/MachineGuid bound) + adopt files: Installs\\InstallDir registry (+ whatever state Connect checks) + or ubi-cli-mvp's connect-seed: official download starts, pause, seed, let Connect verify + launch: upc.exe uplay://launch//0, Connect closed or minimised once the game runs (#979's 20 s rule) +``` + +- **One Connect prefix, not one per game.** unifideck uses per-game prefixes because Steam on the + Deck gives each shortcut its own. DroidDeck already runs Battle.net games inside Battle.net's prefix, + which avoids unifideck's whole credential fan-out problem. +- **Two stages, like Battle.net:** + 1. Connect in its prefix, install by `uplay://install/`, and Downloads shows an indeterminate + "Installing in Ubisoft Connect" until the folder settles. That is #979's level. + 2. Native downloads in the Rust engine with real progress, pause and resume, then Connect adopts the + files. Like #1602, but we own the transfer instead of driving the vendor's downloader. If Connect + has a scriptable local API (Battle.net's Agent had one), that's a cheaper middle stage, but none is + known. +- **Launcherless DRM shims: don't ship them.** See Risks. + +## Known + +- EA sign-in, library, downloads, licences, LSX and cloud sync all work outside the EA app on Linux + (Maxima). +- The EA app installs unattended and its login renders under GE-Proton 11 on x86 (unifideck). +- Ubisoft sign-in, refresh, Demux ownership, signed URLs, manifests and slice reconstruction work + (ubi-cli-mvp `validation.md`, Optima). A session ticket lasts about 3 hours and remember-me renews it. +- Connect's sign-in can't be created from our session. It is DPAPI-encrypted and tied to + `MachineGuid`, but it can be copied between prefixes that share that identity. +- Connect has repeatedly broken under Proton after its own updates (2023, and a single-source April 2026 + report). #1449 and the open Ubisoft PRs show the same churn here. +- Javelin titles don't run under Proton. EA's March 2026 job listing mentions future Linux/Proton work; + nothing has shipped. + +## Unknowns, in the order to settle them + +| # | Question | How to find out | Blocks | +|---|---|---|---| +| U1 | Does an EA OOA licence fetched with our `machineHash` validate inside DroidDeck-Proton under FEX on Adreno phones? Which WMI/CPUID values does the game see? | Prototype: Maxima's hash code against values read through Wine in our prefix; one DiP single-player title and one Denuvo title. | EA launch | +| U2 | Can the app-side LSX server on 127.0.0.1:3216 be reached from proot + Wine, and do games connect with our registry keys (Maxima #71)? | Spike with a Steam EA title already in the library | EA launch, Steam EA titles | +| U3 | What does EA's CDN serve for a current title: one ZIP, multiple, or delta? How do language selection and `installerdata.xml` work? | Maxima `content/` + `downloadUrl.gql`, one real download through the Rust engine | EA install | +| U4 | Does Connect accept a tree we downloaded once the `Installs` registry key is written, or does it re-verify or re-download? Does `connect-seed` work under FEX on a phone? | Spike with a small owned title (ubi-cli-mvp used Splinter Cell / product 109). #1445's registry work is the start | Ubisoft stage 2 | +| U5 | Does Ubisoft Connect (Qt + CEF) run acceptably under FEX in our prefix? How much RAM does it hold while a game runs? | Install it like Battle.net; measure with the Memory readout from #979 | Ubisoft stage 1 | +| U6 | Ubisoft's WebAuth page in an Android WebView: captcha, 2FA, `Ubi-Challenge` | Real sign-in on device | Ubisoft sign-in | +| U7 | Demux over TLS from Android (certificate pinning, patch-version negotiation drift) | Rust spike against `dmx.upc.ubisoft.com` | Ubisoft library and downloads | +| U8 | Ubisoft+ subscription entitlements in Demux ownership | Account with Ubisoft+ | Library completeness | +| U9 | EA Play subscription entitlements in `ownedGameProducts` | Account with EA Play | Library completeness | +| U10 | Ubisoft cloud saves outside Connect | Not researched; Connect handles them while it's in the path | Later | + +## Plan + +Each step is its own PR, tested on device as usual (REDMAGIC tablet, Fold7, Thor). + +### EA + +1. **Spike (no PR):** settle U1–U3 with throwaway code against one owned DiP title and one Steam EA title. +2. **Steam EA titles without the EA app:** the `link2ea` stub, app-side LSX server, OOA licence and + entitlement refresh. It replaces #2036's EA app install where it works and falls back to it otherwise. + This ships value first and proves the hard half. +3. **EA store: sign-in, library, shelves:** a `Store.EA` entry, OAuth flow, juno library, art, + a "not installable here" flag for Javelin titles. +4. **EA downloads:** a Rust `ea` module (ZIP/Range/inflate on `fetch_core`), with Downloads progress, + pause, resume, cancel, updates and uninstall. +5. **EA cloud saves** through `CloudSaves`. +6. **EA app fallback** prefix and store-row button, for pre-DiP and Battlelog titles. +7. Clean-room record: `docs/development/ea-cleanroom.md`. + +### Ubisoft + +1. **Spike (no PR):** U4, U5 and U7. +2. **Ubisoft Connect in its prefix (#979 level):** `droiddeck-ubisoft`, the store row, sign-in inside + Connect, the installed-games scan (registry `Installs` keys), Play by `uplay://launch`, Connect closed + once the game runs, install handed to Connect with indeterminate progress. Fold in #1445. +3. **Ubisoft store sign-in and library natively:** the WebAuth flow, session refresh, a Rust Demux + client, the owned library and art, without opening Connect. +4. **Native Ubisoft downloads:** a Rust `ubisoft` module (manifest, slices, zstd), then hand off to + Connect for registration (U4). Updates come from the manifest version. +5. Clean-room record: `docs/development/ubisoft-cleanroom.md`. + +The two stores share almost nothing beyond the existing store layer, so they can run in parallel. +EA step 2 has the best ratio of value to risk and could start first. + +## Risks + +- **DRM circumvention.** Optima's launcherless mode replaces Ubisoft's DRM loader DLLs, answers the + game's ownership calls itself, and installs a certificate mimicking Ubisoft's so Authenticode checks + pass. Gating it on real ownership doesn't change that it bypasses Ubisoft's access control. Keep it + out of DroidDeck. EA is different: Maxima requests real licences from EA's licence + service and speak EA's documented-by-reversing launcher protocol. Nothing is forged. +- **Terms of service.** Both publishers' terms broadly forbid unofficial clients. No bans are known + for Linux or alt-launcher users (Maxima, Lutris, unifideck, GameVault). This is the same position as + our Epic, GOG and Amazon modules. +- **Vendor churn:** juno schema changes (EA already killed the Origin-era integrations once), Demux + patch versions, and Connect or EA app updates that break under Proton. Plan for tests on recorded + responses, as Epic has. +- **Memory.** A resident Connect costs a phone RAM, so reuse #979's close-on-launch policy and zRAM work + (#1882). EA's native path avoids the client entirely. +- **FEX hardware fingerprint (EA, U1)**, if FEX's CPUID differs from what licences are bound to. +- **Kernel anti-cheat:** Javelin, and whatever Ubisoft titles use one. Flag them in the catalog + instead of letting launches fail. + +## Sources + +- DroidDeck: #979, #1602, #1610, #1611, #1662, #2087 (Battle.net); #1449, #2036 (Steam Ubisoft/EA + installers); #1445, #2050, #2286, #2292, #2295, #2317 (Ubisoft); `docs/development/epic-cleanroom.md`. +- Maxima: README, `maxima-lib/src/{core/launch.rs, core/endpoints.rs, ooa/mod.rs, core/auth/*, content/*, lsx/*}`, issues #49, #71. +- Optima: README, `TODO-hardening.md`, `src/install.rs`, `src/launch.rs`. +- ubi-cli-mvp: README, `docs/optima-comparison.md`, `research.md`, `validation.md`. +- unifideck: `docs/feasibility/{README,ea,battlenet}.md`, `docs/ubisoft-store-spec.md`. +- [Heroic: Ubisoft Connect on Linux and Mac](https://github.com/Heroic-Games-Launcher/HeroicGamesLauncher/wiki/How-to-install-Ubisoft-Connect-on-Linux-and-Mac). +- [GamingOnLinux: EA Javelin job listing mentions Linux and Proton (2026-03)](https://gamingonlinux.com/2026/03/ea-javelin-anticheat-job-listing-mentions-future-support-for-linux-and-proton). +- [GamingOnLinux: KYBER goes open source (2026-01)](https://www.gamingonlinux.com/2026/01/star-wars-battlefront-ii-private-server-and-mod-launcher-kyber-goes-open-source). +- [PC Gamer: Ubisoft's launcher broke Steam games on Linux and Steam Deck](https://www.pcgamer.com/ubisofts-launcher-broke-steam-games-on-linux-and-steam-deck). diff --git a/docs/development/game-environment.md b/docs/development/game-environment.md index de215be39a..79206aa03a 100644 --- a/docs/development/game-environment.md +++ b/docs/development/game-environment.md @@ -55,8 +55,9 @@ profile are never removed. Steam's process log tells it how long each game ran. for at least a minute on the previous build and then exits within 30 seconds twice on the new one rolls back every component line that changed since that boot, and each failed build is skipped until a newer one is published. Games with their own component override are -not counted. DXVK stays on Manual while a GPU rule pins a DXVK version (Adreno 6xx and -the Vulkan bridge). Manual keeps the existing download, import and restore lists. +not counted. DXVK stays on Manual while the Vulkan bridge or the PowerVR runtime chooses DXVK. +On an Adreno 6xx, DXVK Auto keeps every DroidDeck-Proton build on DXVK-gplasync 2.7.1 instead of +following the nightlies (see below). Manual keeps the existing download, import and restore lists. Before publishing game component choices, DroidDeck preserves each Proton build's original components, including an explicitly empty bundle when a component is not @@ -225,6 +226,9 @@ On an Adreno 6xx, Turnip does not expose `storageBuffer8BitAccess`, which DXVK 3 requires. A Steam or desktop session installs DXVK 2.7.1 into any Proton whose DXVK is still the copy that Proton shipped, including after a Proton update put that copy back. A DXVK package chosen in Components stays for that Proton build. +With DXVK on Auto, DroidDeck-Proton builds get DXVK-gplasync 2.7.1 instead, replacing the +shipped copy, the forced DXVK 2.7.1 or an older Auto choice; other Proton builds keep DXVK 2.7.1. +Adreno 7xx and 8xx follow the DXVK nightlies as before. The variable-name picker offers 24 predefined variables and a Custom entry. Like WinNative, known variables use toggles, value dropdowns, multi-select lists, or diff --git a/docs/development/stores.md b/docs/development/stores.md index 78855ea749..278c0cbd8d 100644 --- a/docs/development/stores.md +++ b/docs/development/stores.md @@ -244,6 +244,15 @@ self-extractor stops at "Path not found", so the values it installs are applied `witcher.exe` exits during startup. The language comes from the installed product's `goggame-.info`, and a script's own `setRegistry` entries always win over the built-in table. +The script's `supportData` actions run right after: files and folders from the game folder +(`{app}`) or its support files (`{supportDir}`, `support//`) are copied to `{userdocs}` and +`{localappdata}` in the prefix, and `type: folder` without a source creates the folder. Mods such +as one-click editions ship their `Plugins.txt` and INI files this way. Paths match without regard +to case, links are copied as links, and a path outside those folders is refused. A file the +player already has is replaced only when the action says `overwrite`. `.droiddeck-gog-support` in +the prefix holds the actions' hash, so they run once per script revision and a player's later +edits stay. + A GOG game's exe comes from its own manifest first: `goggame-.info`'s play task in category `game` (`isPrimary` first), or a primary `launcher` task for launcher games, whose `path` stays inside the folder; any other `goggame-*.info` in the folder supplies the task only when the diff --git a/docs/development/thor-sd-storage-investigation.md b/docs/development/thor-sd-storage-investigation.md index b2cfbce558..e2cee625a5 100644 --- a/docs/development/thor-sd-storage-investigation.md +++ b/docs/development/thor-sd-storage-investigation.md @@ -377,3 +377,77 @@ diagnostics remained enabled. Temporary probe files, executables and obsolete partial staging were removed after validation. Original effective mount options were restored; no app-specific Magisk root policy remained. The primary checkout's unrelated `SessionService.kt` modification was preserved. + +## Install phases on current main (2026-11-07) + +A fresh install of 1000xRESIST (app 1675830, 2,216 files, 7.45 GB) to the same Thor SD +card, on the build from main at the time (`0652b0d`), with the fast path and storage +shim from the earlier work. Phase times come from Steam's `content_log.txt`. + +| Run | Reserving space | Download and stage | Commit | Total | +|---|---|---|---|---| +| Clean | 78 s | 25 min 01 s | 205 s | 29 min 46 s | +| Profiled | 79 s | 18 min 43 s | 146 s | 22 min 28 s | + +Reserving space was 404 s on 2026-10-04 and is now about 36 ms per file, which is the +file-creation floor measured above (25-36 ms). The two runs differ by 7 minutes because +the download phase follows the network: Steam logged 12 to 83 Mbps for the whole phase. +Raw sequential write to the card with `dd conv=fsync` was 15 MB/s (120 Mbps), so the +card is not the limit during most of the download. The run-to-run spread is larger than +any change a path-translation fix could make, so install totals are not used to judge +the fast path changes below. + +Kernel stacks of Steam's blocked threads, sampled every 0.2 s from `/proc//task/*`: + +- **Commit:** every sampled thread was inside `renameat`, waiting on synchronous + directory entry writes (`exfat_rename`, `exfat_init_ext_entry`, `exfat_remove_entries`, + `exfat_free_dentry_set`, all through `__sync_dirty_buffer`). That is the `dirsync` + mount option, at about 65 to 92 ms per file. PRoot is not involved in that time. +- **Download:** blocked threads were in `pwrite64`, in `exfat_get_block`, + `exfat_alloc_cluster` and `cont_write_begin`, which allocate clusters as the unreserved + files grow. + +PRoot's own handler time during the reserving phase (profiler, 1 ms slow threshold) was +7.3 s of the 79 s, of which 6.3 s was `openat` handler visits. Almost all of those came +from the HUD reading the three bound thermal files (22,326 fast path misses logged +against `/sys/class/thermal/thermal_zone{22,26,28}/temp`), not from Steam. `fchownat` +accounted for 2,795 visits at about 70 us. Steam's own calls were answered by the fast +path. + +### Fast path changes + +- `fchownat`, `chown` and `lchown` are answered in the fast path, through the libc + wrappers and the patched `svc` sites. The site cache name moves to `svc-v3` so cached + site lists from older builds are rebuilt. +- A path that names a bound file exactly, spelled through a symlinked directory + (`/sys/class/thermal/thermal_zone28/temp`, where `thermal_zone28` is a symlink), now + resolves to the bind's host path. Before, any path through a symlink with a binding + beneath it fell back to PRoot. Paths with `..`, a trailing slash or any other ending + still fall back. + +Same workloads on the Thor, stock library against the new one (session started with +`droiddeckctl override libblfastpath.so`, PRoot profiler on): + +| Workload | Stock | New | +|---|---|---| +| `chown` of 2,000 files, internal storage | 40-50 us each | 6 us each | +| `chown` of 2,000 files, SD card | 61-62 us each | 9 us each | +| Open of the three bound thermal files, 6,000 opens | 128-133 us each | 24-25 us each | +| PRoot `openat` handler visits for both workloads | 15,496 (1,205 ms) | 7,458 (366 ms) | +| PRoot `fchownat` handler visits | 6,873 (288 ms) | 0 | + +`bench/chown.py` compares 87 `chown` results (paths, links, dangling and looping links, +descriptors, directory descriptors, foreign owners, trailing slashes) with the fast path +on and with `PROOT_FP_OFF=1`; they were identical. The thermal paths were compared the +same way for every spelling the HUD can use (absolute, doubled slashes, `.` and `..` +components, relative to a working directory or descriptor on the parent directory) and +matched. They differ only when the working directory or descriptor is inside the +symlinked directory, or the path uses its canonical `/sys/devices/virtual/...` name: PRoot +returns the bound file there and the fast path returns the real node. That difference +existed before these changes, and the HUD does not read those spellings. + +What this does not change: the commit and the file creation costs above are the card's +synchronous directory updates, so SD installs stay bound by them (about 4 to 5 minutes per +2,000 files of reserving plus commit on this card). Removing those costs needs fewer +directory operations per file, not faster path translation. + diff --git a/docs/development/ubisoft-launch.md b/docs/development/ubisoft-launch.md new file mode 100644 index 0000000000..5c18429758 --- /dev/null +++ b/docs/development/ubisoft-launch.md @@ -0,0 +1,112 @@ +# Ubisoft launch without Ubisoft Connect + +A Ubisoft game never holds its own ticket. Its DRM loader opens Ubisoft's API DLL from the Connect +folder, and that DLL asks the running launcher for the session over local named pipes. DroidDeck +answers those pipes with `droiddeck-upc.exe`, a small Windows program that runs in the game's prefix. +The game and every Ubisoft DLL stay unmodified, and there is no Connect client, CEF or background +service. The session the game receives is real: its ticket comes from Ubisoft's ownership service for +the signed-in account. + +This file is the specification `tools/ubisoft-launcher` was written from. The facts come from the +protobuf descriptors embedded in Ubisoft's own `uplay_r1.dll` (`proto_uplaydll/UplayDll.proto`) and +from the public notes in [Foxtrot47/Ubisoft-Connect-RE](https://github.com/Foxtrot47/Ubisoft-Connect-RE) +(`docs/06-launch.md`, `docs/10-denuvo.md`). No code was taken from either. + +## Pieces + +| Piece | Where | Job | +|---|---|---| +| Ubisoft runtime | `C:\Program Files (x86)\Ubisoft\Ubisoft Game Launcher\` in the shared prefix | Ubisoft's own `uplay_r1(64).dll`, `uplay_aux_r1(64).dll`, `uplay_r2(64).dll`, `ubiorbitapi_r2(64).dll`, `UbisoftGameLauncher(64).exe`, the starter's imports (`discord-rpc.x86/x64.dll`, `steam_api(64).dll`) and `overlay(64).dll`. `droiddeck-store-launch` extracts exactly these 16 files with `7z` from the official `UbisoftConnectInstaller.exe`, then deletes the installer | +| Registry | `HKLM\Software\[Wow6432Node\]Ubisoft\Launcher` | `InstallDir` points at the runtime folder in both views; `Installs\\InstallDir` points at the game, with forward slashes and a trailing slash, as Connect writes it | +| Shared prefix | `compatdata/2929927072` | Every Ubisoft game runs here (`UbisoftConnect.launchOptions` sets `STEAM_COMPAT_DATA_PATH`), so the runtime is extracted once | +| Launch file | `/.droiddeck-ubisoft.json` | Written by `droiddeck-store-launch` before every launch from the app's `ubisoft-launch` answer: the account, the owned product ids, a fresh PC ticket, the entitlement's application id and the save folder. Removed first, so a failed refresh falls back to starting the game directly | +| Launcher | `/.droiddeck-launch.bat` | Runs `Z:\usr\local\lib\droiddeck-ubisoft\droiddeck-upc.exe ` when both exist | +| Tickets | app, Rust `ubisoft/service.rs` | Demux `ownership_service`: initialize (owned products), then `GetUplayPCTicketReq` (field 14) answered in field 13 `{1: success, 2: ticket}` | + +## Pipes + +All pipes are `\\.\pipe\terminal__`, where `` is +`ProcessIdToSessionId(GetCurrentProcessId())` (1 under Wine). They are byte-mode duplex pipes with +unlimited instances, created before the game starts. Every message is a 4-byte big-endian length +followed by a proto2 message, except on `uplay_service_ipc_pipe_`. + +| Pipe | Client | Exchange | +|---|---|---| +| `game_start_ipc_pipe_` | `UbisoftGameLauncher.exe` | `game_starter.Upstream{1:{1:{1: build, 2: product, 10: exe, 11: arguments, ...}}}` → `Downstream{2:{1:{1:0 (overlay off), 2:0, 3: / + "/" + , 4: /, 8:3, 9:{1:{1,113,0,1,0} 1:{3,123,0,0,0}}}}}` | +| `uplay_api_process_ipc_pipe_` | game | `Req.initProcessReq{1: product, 2: game pid, 5: env set}` → `Rsp.initProcessRsp(13){1:1, 3:0 (overlay off), 4:3, 6:0}`. Kept open. Answering it is what releases the starter (below). Far Cry 3 Blood Dragon sends only `{2: pid, 5:1}`; a missing pid is taken from the pipe's client process | +| `uplay_ipc_pipe_` | game (`uplay_r1.dll`, `uplay_r2.dll`) | `Req.initReq(1)` → `Rsp.initRsp(1)`, answered every time it is asked. `Req.installerReq(13)` with `initInstallerReq` → `Rsp.installerRsp(10){1:{1:1, 2:"", 3: language}}`. `Req.userReq(11).getCredentialsReq(1){1: id}` → `Rsp.userRsp(8).getCredentialsRsp(1){1: id, 2: username, 3: "" (password), 4: userId, 5: email}`. `Req.streamingReq(27)` → `Rsp.streamingRsp(20){1: id, 2:1, 4:{1:0}}` for the streaming type (not streamed), `{1: id, 2:4}` for any other streaming query | +| `uplay_aux_ipc_pipe_` | `uplay_aux_r1(64).dll` (Denuvo) | `{1: rid, 2: init}` → `{1: rid, 2:{1:1, 2: pid, 3:3367, 3:3368, 3:3962}}`; `{1: rid, 3:{2:{2: challenge}}}` → `{1: rid, 3:{2:{2:1, 3:0, 4: game token, 5:""}}}` | +| `uplay_service_ipc_pipe_` | Ubisoft helpers | Server sends `{3:{1:1, 3:{1: }}}` once, then only reads, unframed | +| `orbit_ipc_pipe_` | game (`ubiorbitapi_r2.dll`) | `mg.protocol.orbitdll` from the DLL's own `proto_orbitdll/OrbitDll.proto`. `Req{1: id, 12: init{1: product}}` → `Rsp{1: id, 12:{1: save folder, 2:1}}`; `getLoginDetails(2)` → `{1: username, 2:"", 3:""}`; `getOsi(3){1: sub}` → `{2:1}` (failed, no online-services pipe) | +| `upc_api_process_ipc_pipe_standard`, `uplay_protocol_ipc_pipe_`, `uplay_overlay_ipc_pipe_`, `uplay_crash_reporter_ipc_pipe_` | — | Held open, never answered | + +### The session (`InitRsp`) + +| Field | Value | +|---|---| +| 1 result | 1 (`InitResult_Success`) | +| 2 account | `{1: userId, 2: username, 3: nameOnPlatform, 4: email, 5: "" (password), 6: country}` | +| 3 connected | 1 | +| 4 gameUplayId | the product | +| 5 uplayIds | every owned product whose ownership entry is owned, of type 0 or 1, in state 0 or 3 | +| 6 gameCdKeys | `{1: product, 2: ""}` | +| 7 isInOfflineMode | 0 | +| 8 upcTicket | the PC ticket from `GetUplayPCTicket`, never the Denuvo token | +| 11, 12, 15, 16, 18, 19, 21, 23 | the values a real launch carries (`{1:0}`, 0, overlay `{1:0, 2:3}`, hardware score `{6211, 22395, 1.0f}`, empty, `{1:1, 2:1}`, `{2:1}`, empty) | +| 14 gameInstall | `{1:1, 2: installed chunks, 3: language}` | +| 17 storage | `{1: "C:/users/steamuser/Saved Games/Ubisoft///"}`; the game appends file names directly, so the trailing slash matters | +| 22 ubiServices | `{1: applicationId}` from the account's entitlements, left out when unknown | + +### R2 titles + +`uplay_r2.dll` speaks the same `mg.protocol.uplaydll` messages (its embedded `UplayDll.proto` is identical to +`uplay_r1.dll`'s), but its `InitReq` names the subsystems the game uses: fields 4 to 11 (friend, overlay, +product, storage, install, multiplayer, store, streaming). Its `ApiContext` creates one object per requested +subsystem and copies the session into them. Friends, overlay, game session, store and crossboot are checked +for a missing object first; install and storage are not. A `gameInstall` sent to a game that did not ask for +install is copied into a null object: Prince of Persia: The Lost Crown (6145) asks for overlay, product and store +only, and crashed reading `0x68` in `uplay_r264.dll+0xaca2` two seconds after the session arrived. + +So when the request names subsystems, 14 is sent only with `subSystemInstall` (8), 17 only with +`subSystemStorage` (7), and an empty `multiplayerInit` (20) is added, which `uplay_r2.dll` asks for even when +empty. A request that names none gets the session above unchanged. The Lost Crown also opens a second context +that asks for every subsystem, and it gets the full session. + +The overlay is reported off in all three places. `uplay_r1.dll` still loads `overlay.dll` from the +runtime folder right after the session arrives, whatever the session says; without that file it gives up +with `UplayAPI [ERROR]: Startup failed: cannot load uplay library` (Assassin's Creed Chronicles: China +then sits on a black screen and aborts with code 3 after about 160 s), so the runtime includes it. + +### Orbit titles + +Older titles such as Far Cry 3 Blood Dragon (205) load `ubiorbitapi_r2.dll` next to `uplay_r1.dll` and ask +`orbit_ipc_pipe_` for `init` before anything else; unanswered, the game waits forever on a black screen. The DLL +keeps savegames itself as `*.save` files in the folder `init` returns, the same save folder as field 17 above. + +## Start sequence + +1. `droiddeck-upc.exe` creates the pipes and an auto-reset event marked inheritable, then starts the + game's own exe with handle inheritance, in the install folder, with no extra arguments. +2. The exe sees it was not started by Connect and starts `UbisoftGameLauncher.exe + -gamelauncher_wait_handle -upc_uplay_id ...` (it exits with code 3; that is normal). +3. The starter asks `game_start` and starts the real game from the reply. +4. The game announces itself on `uplay_api_process`; the answer sets the event, which lets the starter + exit. Without it the starter times out and its job object ends the game after about 15 seconds. +5. `uplay_r1.dll` asks `uplay_ipc` for the session. +6. `droiddeck-upc.exe` waits for the announced process, allows 15 s for a restart, then exits with the + game's code so Steam sees the game end. + +A log of every exchange is written to `~/.cache/droiddeck/ubisoft/upc-.log` in the guest. + +## Denuvo + +For protected titles the aux pipe carries the activation challenge. `droiddeck-upc.exe` writes it to +`~/.cache/droiddeck/ubisoft/bridge-/denuvo-.req` and waits 90 s for `.rsp`. Answering it +needs `denuvo_service` with an ownership token (`OwnershipTokenReq`, field 28) and is the next step; +titles without Denuvo never open the aux pipe. + +## Not covered yet + +- R2 titles other than The Lost Crown, and R2 requests beyond the session, credentials, product list and streaming type. +- Orbit's online-services pipe (`getOsi` is declined) and Orbit titles other than Far Cry 3 Blood Dragon. +- Achievements, friends, overlay, cloud saves. diff --git a/docs/development/unified-input-proposal.md b/docs/development/unified-input-proposal.md new file mode 100644 index 0000000000..1d2100b2d9 --- /dev/null +++ b/docs/development/unified-input-proposal.md @@ -0,0 +1,393 @@ +# Shared input profiles and configurable control surfaces + +This change implements a shared routing model for physical controllers and local +screen controls. It is intended for review of the architectural direction, with +a functional configuration UI and hardware validation on AYN Thor. The related +maintainer discussions motivate this work; they are not approval of this +implementation. + +The configuration UI keeps the existing Setup tabs. Controller contains mode +defaults, one profile list and device settings. Reusable layouts and actions are +stored separately but are managed within each profile source. Direct editing on +the target display has been exercised on AYN Thor, including editing above a live +Steam session and keeping the secondary canvas separate from the main-screen +tools. Revision-specific checks and remaining limits are recorded in the +validation report. + +## Why a shared system + +Desktop sessions need mouse, keyboard and application actions from a controller. +A dual-screen device may already have a comfortable lower-screen mouse, leaving +its physical sticks available for other actions. The same logical button or pad +can appear on the main screen, lower screen or a physical device; its location +should not dictate which actions it can perform. + +The model separates three concerns: + +- **Source role:** physical controller, game overlay (primary) or auxiliary panel + (secondary). Swapping the game and auxiliary panel between displays preserves + the panel's role and bindings. +- **Profile:** references to a physical binding preset and a layout/binding pair + for each touch role. Game and Desktop are editable bases; named profiles can + share parts. Auto has separate Steam and Desktop assignments. Opening a profile + for editing does not activate it. +- **Surface layout:** stable element IDs, types, names, positions and sizes. + Named custom layouts can be used on either touch role. Actions belong to a + separate binding preset associated with that layout. Two desktop profiles can + share their physical bindings and layout while using different touch actions. + +A binding selects native controller output, a DroidDeck action, or disabled. +Native output continues through the existing game/Steam path, optionally with a +compatible gamepad remap. A local action consumes that control so, for example, +R3 assigned to Ctrl+F does not also press the guest's R3 button. + +```mermaid +flowchart TD + Physical[Android controller events] --> PadBridge + Primary[Primary on-screen controls] --> PadBridge + Secondary[Secondary custom controls] --> PadBridge + Deck[Deck pads and grips] --> DeckControls + BLE[Steam Controller BLE reports] --> TritonInputRouter + Library[Saved profiles, layouts and binding presets] --> Profiles[Resolved active profile by source role] + Profiles --> Router[InputRouter] + PadBridge --> Router + DeckControls --> Router + TritonInputRouter --> Router + Router --> Native[Existing pad / Deck report publication] + Router --> Held[Shared key and mouse ownership] + Router --> Commands[Existing menu and keyboard hosts] + Router --> Motion[InputMotion: active stick pointer / scroll only] + Motion --> Held + Held --> Guest[Existing guest keyboard and mouse output] + Native --> Steam[Steam Input or guest controller consumer] + Layout[Independent surface geometry and element IDs] --> Secondary + Layout --> Primary +``` + +These are routing relationships, not additional asynchronous queues. Native +publication is performed by the existing source adapters and writers. + +## Implementation + +| Component | Responsibility | +| --- | --- | +| `InputProfile` | Logical controls, source roles and supported actions | +| `InputLibraries` / `InputLibrary` | Versioned reusable parts, validated references, selection and migration | +| `InputProfiles` | Compatibility API for the compiled active profile and legacy migration | +| `InputSurfaceLayout` | Bounded normalized touch geometry and legacy layout migration | +| `InputSurfaceSource` | Independently owned per-element routing through existing pad/Deck publishers | +| `InputRouter` | Resolve channels on profile changes, route events, track held outputs | +| `PadBridge` | Existing player slots plus independently owned local touch sources | +| `DeckControls` | Source ownership for Deck grips, contacts, coordinates and clicks | +| `TritonInputRouter` | Apply routing before Steam Controller BLE report publication | +| `PhysicalInputBindings` | Capture additional Android keys with device identity | +| `HeldInput` | Coordinate router outputs with existing keyboard and gesture outputs | +| `InputMotion` | Frame callback for active synthesized stick pointer/scroll movement | +| `InputTiming` | Optional bounded dispatch-to-publication diagnostics | + +Supported actions are native output, compatible native remapping, disabled, +keyboard keys/combinations, mouse buttons, pointer movement, scrolling, four +keyboard directions, and existing menu/keyboard/Steam/quick-access commands. +Analog controls used as buttons have separate activation and release thresholds. +Key combinations press modifiers first and release in reverse order. + +Standard controller assignments are shared by the physical role. Additional +captured keys match a device identity plus Android keycode/scancode, rather than +persisting a transient Android device ID. System-reserved keys remain with +Android. This is not a per-device calibration system. + +### Controller navigation + +Setup keeps its existing top-level tabs. Controller starts with Desktop and Game +mode defaults, followed by one profile list and inline device settings. Opening +or editing a profile does not activate it; the session menu owns the active +profile override. Quick-menu overrides are called out beside the default selectors. + +Each profile opens Physical controller, Main screen or Second screen. Control +cards and touch element rows open the same large properties editor. Layout +editing uses the full-size canvas and one draggable, expandable Tools palette. Back +returns to the source and profile being edited, rather than the library root. +Saved layouts and reuse commands live inside the relevant source editor; there +are no parallel top-level layout, binding and profile catalogs. + +New profiles copy actions and custom geometry independently by default. Users +can explicitly keep physical controls shared at creation, or copy/link a source +from another profile later. Reusing a saved layout can link geometry without +linking actions. Make independent copies both geometry and actions for that +source. Reset actions affects only the selected profile, preserving its layouts +and detaching shared actions before resetting them. + +Main-screen visibility remains a global device setting. Color and opacity belong +to each saved touch layout. The standard gamepad is an ordinary editable layout: +its legacy positions, appearance and stick options are migrated into elements. +Other saved resolutions remain selectable layouts. The previous fixed gamepad +renderer and its separate editor are removed; only its placement calculation is +retained for migration. Vibration, radial stick dead zone, Joy-Con VR, global +shortcuts and timing diagnostics are inline below the profile list. This adds no +settings reads or queues to input dispatch. + +### Session profile composition + +Both the session drawer and launcher controls sheet expose Profile, Physical +controller, Main screen and Second screen. Choosing a profile loads its three +saved parts; choosing one part creates a mode-specific override without editing +the source profile. Game and Desktop remember their choices separately. Auto +resolves the corresponding default profile. Selecting a complete profile clears +part overrides. Save as new profile creates independent copies of all three +parts, including geometry, appearance and actions. + +Fixed lower-screen tabs, side selectors and click bars are removed. Deck pads +and grips, one or two Deck pads, grips alone, the mouse trackpad with keyboard, +and a compact gamepad are ordinary editable layout/binding presets. Their menu, +keyboard and click buttons are ordinary elements too. The compact gamepad reuses +the previous panel geometry calculation. Existing fixed-panel selections migrate +to profile controls on first use. Schema 4 also preserves version-3 custom +layouts and converts legacy fixed layout references without changing physical +mappings. Active quick-menu parts cannot be deleted. + +Mouse touchpad gestures reuse `TouchpadGestures` for relative movement, tap, +hold-drag, two-finger scrolling and secondary clicks. A tap uses the element's +configured click action; it does not reposition the pointer. Gesture outputs use +owned router channels and are suppressed while input is suspended or until a +held contact returns to neutral after a profile change. Native Deck pad mode +retains contact/position reporting and click behavior for Steam Input. + +### Reuse and migration + +Existing `FakeInputWriter` transports, player assignment, guest keyboard/mouse +output, `KeyboardHost` and second-screen presentation are reused. The existing +button, stick, d-pad and trackpad views now live in `InputSurfaceView`; there is +no parallel fixed controller panel. The custom editor arranges the existing button, stick, d-pad and trackpad +views; it does not create another emulated controller. Trigger elements provide +press/release input. A new element is unassigned until an action is explicitly +saved. Native sticks and pads retain their existing click gestures. +Movement/contact and click are separate assignments for sticks and trackpads. +Both use the same properties editor and router ownership rules; migrated click +bindings remain editable, and a custom mouse pad can assign its click separately. + +The editor uses the target display at actual size. On a dual-screen device, +Add, Undo, Snap, Save and element properties stay on the activity display; the +auxiliary display is a full-size canvas with no toolbar inset. On one display, +a draggable Tools bubble expands into those same tools, including element +properties, and collapses to leave the canvas accessible. Properties replace the +canvas and return to the same draft. Snapping aligns centers and edges with +other elements and the display, including during resize. Done persists geometry and actions together; Cancel discards the +draft after confirmation. The preview has no input sources and cannot send +input to the running session. + +Primary custom layouts follow the existing on-screen-controls visibility setting; +Steam's touch-controller mode retains priority. Secondary layouts use Profile controls mode. Terminal, desktop and dashboard +remain separate panel modes. Steam-managed touch controls keep their own editor; a new editable layout +can start empty or copy a saved custom layout. + +Legacy Game/Desktop mappings, key captures, active selection and lower-screen +geometry migrate to the library together. Legacy element IDs preserve their +existing logical bindings. New elements use stable independent IDs, so two +buttons can have different actions or hold the same output without releasing +one another. Referenced parts cannot be deleted, and editing an unsupported +library version cannot overwrite it. Shared edits show their scope; copying a +binding preset from a profile relinks only that profile's selected role. + +Library decoding, validation and preset resolution happen during configuration +or profile activation. Dispatch uses preconfigured router channels, with no +preference reads, profile lookups or new asynchronous queues. The physical native +identity path and frame-based stick motion remain in place. These properties do +not substitute for a fresh latency comparison on hardware. + +The old touch-only mapping and Select-as-Steam settings migrate once into Game's +primary-screen and physical bindings respectively. Their redundant UI is removed. +`PadBridge` retains raw SELECT state; only the profile maps it to GUIDE. The old +flag is read for migration, not applied again during event handling, so explicit +native/disabled/local bindings and profile resets remain authoritative. Attaching +or removing a router cannot change which raw button a SELECT release clears. +Invalid stored entries fall back to defaults. The global settings reset clears +both profile and surface settings without reimporting the old mappings. + +The shared PC-key definitions are adapted from kingokksa's +[#1305](https://github.com/Droid-Deck/DroidDeck/pull/1305) at `76fc9b31`. +Device capture and identity handling build on the approach in jmarti326's +[#1202](https://github.com/Droid-Deck/DroidDeck/pull/1202) at `42db788f`. +The initial implementation adapted these parts rather than merging either PR +wholesale. Upstream subsequently merged #1202; the October 30 integration retains +its global physical shortcuts, saved assignments and configuration UI alongside +the profile bindings. + +The November 4 integration reuses upstream's common bottom-screen bar, extracted +trackpad/terminal panels and screen-swap presentation. Custom controls coexist +with the dashboard and KDE desktop panels. Routed on-screen stick updates retain +upstream's throttled visual redraws without deferring pad publication. Upstream +hardware-key translation and mouse capture use the existing output ownership and +release capture while the input-profile editor is open. + +Explicit captured bindings in the active profile take precedence over a global +physical shortcut for the same key. Otherwise the global shortcut precedes the +default Back/QAM handling and logical controller routing. A consumed shortcut +does not also reach the guest, and its release remains consumed across menu +changes. Shortcut capture remains available from Input profiles, the session +menu and mode settings. Profile editing gates execution of these shortcuts. + +### Ownership and lifecycle + +Each source retains its own contribution. Releasing a physical mouse button does +not release an identical button still held on the lower screen. Closing a surface +releases that surface, including the final release on the existing Epic launcher +mouse route. Existing gesture and keyboard outputs participate in shared key and +mouse-button ownership. + +Native pad merging uses OR for buttons, the maximum trigger value, and the +strongest complete stick vector. A native Deck trackpad retains one source's +coordinates until that contact ends, then accepts a waiting contact. Concurrent +surfaces do not overwrite its coordinates during the same contact. + +Profile changes, suspension and menu entry release old local outputs. Held +controls must return to neutral before activating their new bindings. Device +removal and surface disposal remove their owned state. The existing menu +navigation path remains available independently of gameplay assignments. + +When an adapter attaches retained input state to a replacement router, it seeds +the new channels without emitting presses and requires held controls to return +to neutral. Already-open pad rings receive the released state immediately; +attaching a router, changing a profile or enabling input does not open an unused +ring. Steam-menu and VR stick-switch requests use separate system-owned button +pulses, so primary-surface bindings cannot suppress or remap those commands. + +The menu and keyboard commands currently open their respective UI; they are not +toggles. Back/B closes the menu. This can be reconsidered with the UX work. + +### Hot path and scheduling + +Profile JSON, preferences and layout geometry are processed outside event +routing. Channels resolve their actions when configuration changes. The current +router uses mutable per-source state under one synchronized monitor; it is not an +immutable compiled plan. This keeps shared ownership consistent, but contention +with simultaneous BLE input has not yet been measured on hardware. + +For an unchanged Game binding, `PadSource` returns the original `PadState`. +The BLE identity route returns its original report array. Modified reports use +reusable buffers and preserve unrelated timestamp, motion, pressure and extension +fields. Normal controller events do not wait for a frame, coroutine or timer. + +Only synthesized stick-to-pointer/scroll motion uses `Choreographer`, because a +held stick must keep moving without new device events. Trackpad deltas and button +changes publish synchronously on the normal UI input path. Pointer output arriving +from an off-main-thread adapter is marshalled to the existing UI cursor path; +application commands also use the main handler. Neither schedules native forwarding. + +The mapped cursor reuses the existing compositor coordinates and Android cursor +layer. Trackpad contact starts from the current pointer position, preventing +jumps when switching sources. Publishing pending Compose snapshot notifications +before drawing avoids the observed every-other-frame cursor update without +changing the stick response curve or adding smoothing. + +See [validation](unified-input-validation.md) for measurements and their limits. +The evidence supports no clear typical latency regression in the tested native +path, not zero overhead or a complete input-to-display latency guarantee. + +## Steam ownership and boundaries + +Steam remains responsible for game mappings, action sets, layers, radial menus +and its cloud profiles. Native/Steam-managed here means forwarding the controller +through the existing path; it is not a new integration with Steam's profile API. +Steam Mobile Touch retains its own Steam-managed layout and protocol. Its widgets +do not pass through this local remapper. The VR-specific input path also remains +separate. Existing gestures retain their recognizers and share output ownership. + +The local UI revision includes named profiles, reusable binding presets and +custom layouts edited on either display. Import/export, automatic per-app +selection, macros, configurable tap/hold behavior and multi-button activation +chords remain deferred. One button emitting Ctrl+F is supported; pressing +multiple buttons to activate a new binding is not implemented. Built-in Steam +touch layouts retain their separate Steam-managed editor. + +Physical native remaps stay within compatible report families. Generic custom +buttons choose a supported button output explicitly. Additional hardware +adapters must join before publishing consumed controls; an Activity-only +interceptor cannot cover every raw device. Future adapters should use this +contract without replacing device-specific calibration or protocol handling. + +## Related upstream work + +Reviewed on 2026-10-24. Several related proposals remain open; this is not a claim +that no other controller work exists or that this implementation supersedes it. + +- [#1202](https://github.com/Droid-Deck/DroidDeck/pull/1202): physical shortcuts and + capture. The maintainer's + [centralization feedback](https://github.com/Droid-Deck/DroidDeck/pull/1202#issuecomment-6299647180) + motivates one shared assignment model. Questions there about overlapping + actions inform consumption/ownership; multi-button activation remains deferred. +- [#1305](https://github.com/Droid-Deck/DroidDeck/pull/1305): screen key mappings, + combinations and layout management. This change shares the action model across + physical and screen sources, but does not implement that PR's full layout UX. +- [#1414](https://github.com/Droid-Deck/DroidDeck/pull/1414): external-controller + button and touchpad-press capture, including devices exposing a touchpad press + as pointer input. Our key capture does not yet implement that pointer-contact + adapter. It is a concrete area for coordination. +- [#860](https://github.com/Droid-Deck/DroidDeck/issues/860) and + [#1356](https://github.com/Droid-Deck/DroidDeck/issues/1356): desktop controller + input. This implements controller-driven pointer/keyboard actions, not every + gesture or automatic keyboard behavior requested in #1356. +- [#581](https://github.com/Droid-Deck/DroidDeck/pull/581): hardware calibration and + device profiles remain a separate responsibility from action profiles. +- [#1226](https://github.com/Droid-Deck/DroidDeck/pull/1226): keyboard host selection; + the local action reuses the existing host rather than implementing another IME. +- [#1150](https://github.com/Droid-Deck/DroidDeck/pull/1150): raw DualSense input. + That proposed adapter will need integration before publication; it is not + covered merely by the current Android key capture. + +## Review focus + +The main questions are whether source roles and profile boundaries match the +project's direction, whether adapters join at the right point, and whether the +shared ownership/lifecycle contract is suitable for future input sources. The +validation document distinguishes device-tested behavior, host regressions and +remaining coverage. UX/UI refinement can follow agreement on those decisions. + +### Settings polish and display targeting + +The session Profile row expands to show the three saved parts and the editor. +Choosing the profile remains a separate selector. Session editing opens its +settings on the spare display, when available; element properties and editor +commands stay on the activity display. Main-screen placement over a running +session uses a transparent canvas so controls can be arranged around game UI. +Existing visible controls are suspended while editing. + +Schema 5 adds an explicit main/secondary/both target to saved layouts. Migration +infers the target from existing profile and quick-menu references, preserving +layouts already used on both screens. Layout rows render a miniature with the +same `InputSurfaceView` renderer; unused legacy copies are retained under Other +saved layouts. Newly created layouts belong to the source being edited. + +Action editing retains a draft for each route and action type. Sticks assigned +to a digital action press above 55% on either axis and release below 35%; a +trackpad holds a digital action during contact. These semantics are explained +next to the action. Horizontal scroll uses the same router and existing evdev +REL_HWHEEL / Wayland axis transport, with independent fractional accumulation. +No additional event thread or queue is introduced. + +### Screen-key consolidation + +Schema 6 imports the retired screen-key overlay into normal saved profiles, +layouts and element bindings. The active old set and each named saved set become +separate imported profiles. Current Game/Desktop defaults and quick selections +remain unchanged; the settings page explains where to find the imported profiles. +The old editor, overlay View, guest-key dispatch path and session-menu entries +are removed. Only a one-time reader of the old preferences remains. + +Migration converts normalized centers and density-based sizes into the shared +layout geometry. Key chords, four-way directions, separate eight-way stick +bindings, toggle buttons, built-in symbols, custom image references, shape and +opacity are retained. Blank stick diagonals keep their previous behavior of +pressing neighboring cardinal directions. Imported images are decoded once per +view, sampled to at most 512 pixels, and read only from the app's existing image +directory. New buttons can choose text or an existing built-in key symbol. + +A converted library and the original raw preferences are committed together +before old preference entries are removed. Invalid data or capacity failures do +not erase the old library or keys, and settings show a pending-import notice. +The backup is recovery data, not a second runtime input configuration. + +Toggle buttons use `InputSurfaceSource` and the same router ownership as every +other element. Releasing one owner cannot release another owner's held key. +Menu suspension, profile changes, cancellation and detach release latched output. +Physical native input continues through its existing fast path; no queue, timer +or additional event thread is introduced for these features. diff --git a/docs/development/unified-input-validation.md b/docs/development/unified-input-validation.md new file mode 100644 index 0000000000..f5dfdbfa80 --- /dev/null +++ b/docs/development/unified-input-validation.md @@ -0,0 +1,703 @@ +# Unified input validation + +Recorded on 2026-10-24. These results distinguish the device-tested candidate from +the later upstream rebase and host regression fixes. They do not establish zero +latency or complete coverage of every controller and session mode. + +## UI publication candidate, 2026-11-06 + +The implementation was updated through upstream `a1bc8467f` (merge `aa3757eff`). +The generic action selector now says **Scroll**; binding summaries still state +the configured direction. Closing the docked session drawer now invokes its +ordinary close action instead of disabling the second-screen presentation. +The global-reset regression compares against a pristine compiled library, which +includes the editable gamepad's keyboard element. + +- All **944 input/UI tests in 125 suites passed**. The final full JVM run completed + **3,598 tests in 493 suites: 3,573 passed, 25 failed, no errors or skips**. + The failures are the same unchanged upstream log-export tests (24) and + case-sensitive WoW add-on import test (1) described below. Input/UI and global + settings reset passed in this full run. +- Initial full runs exhausted this Mac's free disk and produced secondary native + runtime/idle failures. Obsolete local test APKs were removed. The completed run + used a local Gradle init script with one worker and a fresh test JVM every 25 + classes to bound temporary-file use. No repository test configuration was + weakened and no tests were excluded from the completed run. +- The APK passed runtime-input, session-asset, native dependency, alignment and + v1/v2/v3 signature checks. SHA-256: + `611814f11cbac4bab7f97dac5c0b0657d85d8dfbd6b40b741bda1fc13efa6f83`. + It was installed over the separate Input Test app with its environment intact; + as with earlier local candidates, Rust store engines were omitted. +- On AYN Thor, closing the lower-screen menu with its X restored the existing + controls instead of revealing Android. The live Steam editor, separate lower + canvas and upper tools/properties, profile previews, saved layouts, and action + pages were exercised. A single Trigger selection added a centered, selected + element; the reported occasional extra tap was not reproduced. The temporary + editor change was discarded. +- The user also verified native controls, profile switching, trackpad/pointer + and scrolling behavior on the immediately preceding UI candidate. This is + distinct from automated UI navigation and the historical latency measurements. + +Actual display captures and test reports are retained locally under +`app/build/reports/input-publish/`. Screenshot publication is subject to the +user's review. No new latency measurement was made for this UI revision. + +The follow-up router-ownership fix passed **47 tests** in `InputSourceOwnershipTest`, +`InputSurfaceSourceTest`, and `InputRouterTest`, including two new regressions. +An obsolete activity (or one without a router) cannot detach the replacement +activity's Deck routes; the current owner still releases its held keys on cleanup. +GitHub's complete Build APK workflow passed for `7b7722bd2`. + +### Review follow-up + +- Upgrade binding IDs are deterministic across unsuccessful reads. Editor lookup + from an earlier read remains valid when legacy screen-key import fails. +- An edit of malformed supported-version data commits both the restored library + and a recovery copy of the original text. Reads alone preserve the original; + newer unsupported versions remain protected against overwrite. Settings show + that recovery data was retained. +- Focus/capture loss releases hardware, Android keyboard and PC-keyboard owners + without releasing another source's hold. Secondary settings handle an invalid + presentation display at initial show and resume. +- Physical-button capture waits for release before confirmation/navigation. + Back and B remain assignable; another Back/B press cancels, A confirms, and + Retry starts a new capture. The existing dialog-window key hook is reused. +- Steam Controller BLE teardown synchronously closes its routed source and + gates state/feature publication. Queued callbacks and starts cannot reactivate + it after stop returns; disconnect work remains on the existing BLE thread. + +The input/UI/global-reset run passed **959 tests in 127 suites**. After the +additional BLE finding, **5 BLE/routing tests passed**, including two regressions +for queued callbacks, synchronous release, source detachment and shared holds. +The final APK passed packaging, runtime/native dependency and signature checks: +`a5ec9120be120b829748f8489782a0c7158360d7f8deb61777fe75d601708d20`. +It was installed over Input Test; saved profile preferences remained byte-for-byte +identical and the existing environment was retained. +The button-capture dialog opened and cancelled on Thor without a crash. No +physical BLE-controller check or new latency measurement was made for these +follow-ups. + +### Replacement-controller and mouse-backend follow-up + +The Triton slot now has a single owner across activity instances. Late reports, +subscription callbacks, feature replies and teardown from a replaced controller +cannot overwrite the current controller's report or connection flag. Every +instance still closes its own routed source. Claiming the slot happens once at +start; a queued or repeated old start cannot take it back. + +Legacy Select-to-Steam also migrates to the Desktop physical profile when there +is no saved Desktop profile. An existing Desktop assignment is preserved. +Mouse backend selection and publication share the button-owner lock with raw +channel open, soft release and destruction. Horizontal and vertical scrolling +use the same path and fall back to Wayland after leaving raw mode. + +The complete input/UI/global-reset run passed **967 tests in 128 suites**. +The focused run passed **28 tests**, including replacement subscription before +old teardown, unstarted-instance teardown, saved/default Desktop migration, and +concurrent scroll versus raw-channel release/destruction. These changes add no +input queue or timer. Physical BLE hardware and latency remain unmeasured for +this follow-up. + +### Upstream integration, 2026-11-07 + +Merged upstream through `542bbf89c`. The shared stick renderer retains the new +fast-double-tap rule from #2173, including cancelled, dragged, long-held and slow +second taps; the six upstream regressions were adapted to the profile-backed +renderer. WoW chat uses the existing custom lower-screen presentation and shared +key ownership, preserving Ctrl held by another input source. The new platform +pointer, PiP, component-update and store-launch changes are retained. + +The merged candidate passed **1017 tests in 133 suites**, with no failures, +errors or skips: all input/UI tests, global reset, component updates, PiP policy, +runtime auto-update and Proton menu auto-selection. The gamescope p22 archive +was downloaded from the newly pinned component repository and verified against +upstream's SHA-256 before refreshing the local runtime inputs. + +### Second-screen refresh follow-up, 2026-11-07 + +Refreshing a profile now updates the existing custom control surface in place. +Terminal and dashboard modes are untouched, and the custom surface keeps its +keyboard host, field-keyboard state and WoW chat panel. Actual mode changes +continue to use the existing full teardown/rebuild path. + +All **35 tests in 5 suites passed** across `SessionSecondScreenTest`, the +surface UI suites and `InputSurfaceSourceTest`. Three new regressions cover +terminal/dashboard identity, changed elements/style with an open field keyboard, +and preservation of the lower-screen chat view and its dismissal callback. + +## Input settings polish, 2026-11-06 + +The final input/UI host run passed **941 tests in 124 suites, zero failures**. +New coverage includes retained route/action drafts, centered element creation +without a placement tap, collapsed quick-profile settings, schema-4 migration +with a layout shared across both screens, target/scroll serialization, and +horizontal scrolling through both guest mouse backends. Existing physical native +fast-path and ownership tests remain green. + +The settings use stable DroidDeck Input labels, contextual axis/contact hints, +color swatches, opacity sliders and rendered layout thumbnails. Diagnostics now +open a result dialog and explain how to record a sample and what it measures. +Shortcut rows show the actual assignment and offer Restore to default. + +No new latency comparison was taken for this UI revision. The normal native +controller path is unchanged. New horizontal wheel work happens only for a local +scroll action. The user's environment installation is retained. Device verification is recorded below. + +### Screen-key removal and Thor verification + +The old screen-key overlay, standalone editor, layout panel and separate runtime +key sender are removed. Schema 6 imports the old active set and every named set +as editable profiles. Current defaults and selections are kept. Tests cover +idempotent import, chords, normalized geometry, per-element opacity, shapes, +image references, symbols, custom diagonals, and retaining the original data +when migration cannot complete. A backup and the converted library are committed +together before the old preferences are removed. + +New shared element tests exercise latched keyboard and native controller buttons, +shared ownership, cancellation, menu suspension and profile replacement. Separate +diagonal bindings release previous keys and preserve neighboring-direction +fallbacks. No additional input queue or thread is used. + +Final APK installed over `com.droiddeck.launcher.inputtest` on AYN Thor: +`6618be55c28a52746b6504656a480845d25d8a34114daa1829aad5dbb5b5e729`. +Packaging checked runtime inputs, session assets, native library dependencies and +v1/v2/v3 signatures. As with the earlier test packages, Rust store engines are +omitted; the Steam/Desktop environment is retained. + +On-device checks: + +- The expanded Profile has readable text and one visibly grouped block. +- Profile source rows and saved-layout rows show bounded thumbnails. The previous + black rectangle overflowing the list no longer appears. +- The original anchored color menu shows its color swatches. +- Steam → Controller → Edit opens settings on the spare display. +- Main-screen editing opens on the activity display over live Steam. A second + on-device crash was found and fixed: returning from lower-display settings must + use the existing activity dialog, not a Presentation on the default display. +- Adding a button immediately centers and selects it. Its properties and symbol + picker open on the main display. +- Secondary-screen editing leaves only the canvas below and tools above. Adding + a trackpad centers and selects it without requiring another placement touch. +- Temporary edits were discarded. Comparing the before/after saved libraries, + excluding new default schema fields, found no changes to the user's profiles, + geometry, bindings or selections. Schema changed from 5 to 6. +- Steam remained READY after closing the editors; the final device log contained + no new fatal exception. + +The test device had no old screen-key sets to import. Legacy content migration +and toggle ownership were verified by host regression tests, not by a new +physical-input latency or end-to-end game test. Evidence is under +`app/build/reports/input-polish/`; the final installable copy is +`app/build/input-ui/DroidDeck-Input-Test-unified-screenkeys-2026-11-06.apk`. + +## Unified editor and session profile composition, 2026-11-06 + +The fixed gamepad and its separate editor are replaced with a saved editable +layout rendered by `InputSurfaceView` on either display. The old fixed lower +panel, tab switches and separate mouse trackpad view are removed. Deck pads, +grips, single pads, the mouse trackpad and a compact gamepad are editable presets; +their menu, keyboard and click buttons are normal elements. The compact preset +reuses the previous panel geometry calculation. + +Schema 4 migrates version-2 overlay references, mappings, saved positions, +appearance and stick-click preferences, and version-3 fixed lower layouts. +Other resolution-specific positions remain saved layouts. Future or invalid +libraries are not overwritten. Physical mappings retain their identities and +assignments. Existing custom layouts remain intact. + +On two displays, the activity hosts Add, Undo, Snap, Done and full-size properties; +the other display contains only the canvas, without a reserved toolbar strip. +On the main display a draggable Tools bubble expands into the same tools, +including Properties. The editor snaps centers and edges while moving and +resizing. Both source pages list their actual elements. Device settings are +inline below the profiles. + +The quick menu selects a whole profile or overrides Physical controller, Main +screen and Second screen independently. Game and Desktop selections are separate. +Changes do not mutate the source profiles; saving a combination creates independent +copies. Returning to Auto clears the overrides. Tests cover serialization, +reference validation, deletion protection and preservation of source profiles. + +The complete input/UI host run passed **931 tests in 124 suites, zero failures**. +Coverage includes full-size shared editing, floating tools, display removal, +geometry migration, source ownership during detach/layout replacement, quick-menu +composition and saving, native Deck clicks and mouse gestures. Tap and hold use +the configured element click; ordinary taps do not reposition the cursor. Profile +changes and menu suspension suppress delayed gesture presses until neutral. +The neutral-stick/physical-input regression remains covered. + +No new end-to-end latency measurement was performed for this revision. The +physical controller path remains unchanged. Touch output uses the existing +owned router and guest transports without an additional event queue. Mouse +movement is delivered synchronously; tap/hold recognition reuses the existing +`TouchpadGestures` timings. The local APK omits the Rust store engines and uses +the separate Input Test application ID. + +Device verification on AYN Thor confirmed the four quick selectors and saved +second-screen presets, draggable/expandable tools, full-screen properties and a +canvas-only secondary display. WindowManager reports the main editor at +`[0,0][1920,1080]`; the secondary canvas uses the full `1240×1080` display. +The installed library migrated to schema 4 with existing profile references, +bindings and custom layouts preserved. A previous test migration's unchanged +main overlay geometry was corrected from its window-size copy to its saved +1920×1080 version. No user-edited geometry was replaced. + +Final APK SHA-256: `c13326d11f5e8de3b878d83b296ba2f77495baf5c35d1d00dbb0327333c0277d`. +Runtime assets, native library dependencies and v1/v2/v3 APK signatures passed +verification. Focused editor/input checks passed again after fixing Compose 1.6 +fullscreen dialog sizing. Live game/desktop input and perceived latency with this +final build still need user testing; the device checks above cover UI and migration. + +## Controller integration and upstream update, 2026-11-06 + +Merged upstream `bf693d7f7` into the working branch as `ba5876bf6` before the UI +integration. This includes 90 upstream commits, including radial stick dead zones +and mouse-button handling. The merge keeps routed source ownership and the new +upstream raw-stick behavior; the targeted input merge checks passed. + +Controller now contains two default-profile selectors, one profile list and a +Device settings entry. Source-specific action editing and saved layouts are +inside each profile. New profiles are independent by default, with an explicit +option to share physical controls. Direct editing and Hide tools / Show tools +are retained. No input-event queue or preference lookup was added by this UI +reorganization. + +The first full host run executed 3,580 tests in 490 suites: 3,555 passed and 25 +failed, with no skips. All 926 input and UI tests in that run passed. The failures +are in unchanged upstream log-export tests (24) and WoW add-on import tests (1). +The log fixtures require `SecureDirectoryStream`, unavailable in this macOS JDK; +the WoW test assumes `Foo` and `foo` refer to different paths, while this host +filesystem is case insensitive. A direct Java filesystem probe confirmed both +conditions. The log-export failures also reproduce when run separately. The +affected production and test sources match `origin/main`. These host limitations +have not been hidden by disabling tests or changing production code. + +The final input/UI-only run passed all 927 tests in 122 suites. + +Additional final checks cover integration in the existing Setup tabs, the lack +of duplicate Layout/Configure entries, back navigation, independent creation, +explicit physical sharing, and preserving elements while hiding editor tools. +Rendered UI checks cover 360×800, 640×360 and 960×600 dp. Earlier latency results +below are not a fresh measurement of this revision. + +The final 26 profile/library/editor checks passed again after the source-specific +control lists and sharing labels were finalized. The APK passed session-asset +matching, runtime-input attestation, native dependency/export checks, alignment +and signing validation. It uses the same local HTTPS/direct-release download +adaptations as the previous build and still omits the store Rust engines. + +Installed the update over `com.droiddeck.launcher.inputtest` on AYN Thor without +clearing data. Verified Setup → Controller → Desktop → Second screen → Edit on +screen; the canvas opened on the lower display, Hide tools / Show tools worked, +and selecting the right trackpad opened large properties on the main display. +Returned without saving; the stored input-profile preferences matched the backup +exactly. Device settings had no persisted overrides before or after this check. +No fresh gameplay or latency measurement was performed in this UI pass. + +Final APK SHA-256: +`f1d16a503661055a0abe8cc954d3fc94f6b9a1c1c626ac38b6c88dba478810d1`. + +## Local UI revision, 2026-11-05 + +This revision adds the Profiles / Layouts / Bindings libraries, migration of the +previous settings, independently identified touch elements, and a direct editor +on either display with properties on the activity display. + +- The complete JVM/Compose run passed 3,306 tests across 467 suites, with no + failures, errors or skipped tests. The native identity fast-path checks remain + in that suite. After the final editor-window adjustment, all nine editor and + adaptive-layout UI tests passed again. +- New coverage includes legacy mapping/geometry/selection migration, shared + physical presets with separate touch actions, explicit private copies, safe + deletion/reset, unsupported schema preservation, independent element actions, + overlapping held outputs, suppression across profile/menu changes, and inert + editor previews, independent stick/trackpad click bindings, same-view + detach/reattach, and saving a draft after the auxiliary display disappears. +- Both Steam and Desktop quick settings expose the same input library entry. +- Compose navigation was exercised at 360×800, 640×360 and 960×600 dp. Rendered + images are generated under `app/build/reports/input-ui/`. The properties Save + control stays outside the scrollable content on the short landscape screen. +- Updated the existing `com.droiddeck.launcher.inputtest` installation on AYN + Thor from 0.6.0 to the local 0.6.9 build without clearing its data. The official + package was not replaced. A local backup of the two input preference files was + taken before migration; all 61 saved Desktop bindings matched after migration. +- On Thor, exercised the quick-settings entry, library navigation, full-size + editing on both displays, selection on the lower display with properties on + the main display, moving an element, Undo and Done. After undoing the move and + saving, the complete stored library matched the pre-edit data. +- The earlier Thor latency measurements below apply to the earlier candidate, + not to this library/editor revision. No new end-to-end or dispatch latency + claim is made. + +The complete local runtime build passed session-asset matching, runtime-input +attestation, native export/dependency validation, alignment and signature checks. +The local build-script copy used HTTPS for the Debian archive and direct GitHub +release downloads when the unauthenticated API was rate limited; checksum and +revocation checks remained enabled. No build-script changes are included in the +source diff. As before, the test APK excludes the Epic/GOG/Amazon Rust engines. +The installed APK SHA-256 is +`3e3f41589d45b70294f2d59404f591ae76f2ceba8488657e8821aab1bca224b3`. + +Before publishing, also test on Thor: create two Desktop profiles sharing physical +bindings; give them different touch actions; edit and resize on the actual lower +display; open properties on the top display; save/cancel; swap screens; return +from Android Home; disconnect/reconnect the auxiliary display; and repeat the +paired physical-button latency measurement. Verify primary custom overlays with +on-screen controls enabled and secondary layouts in Custom controls mode. + +## Revisions and environment + +The Thor candidate was `5021e7ac` on base `ac0909ea`, with the local temporary-file, +cursor visibility/handoff/frame cadence, scroll direction and button label fixes +subsequently committed on this branch. Its final APK SHA-256 was +`bb211095d6443e9f85ed1f5d8c5674f527df2937ac6b0efe7d6fc4adcb9dcf62`. + +The branch was then rebased onto upstream `906e0d1f`. Review added regressions and +fixes for final touch-source release, concurrent native trackpad contact ownership, +and resetting the new preferences. Those final changes and the rebase have host +test coverage; the installed Thor APK was not silently replaced and those exact +changes have not yet been revalidated on the device. + +Local Android builds use Java 17 and NDK `28.2.13676358`. The Thor test APK used a +local Gradle init script to install alongside the official app; no application ID +or app-name changes are part of this PR. Local builds use `-PskipRust=true`, so +Steam/Desktop checks do not establish the behavior of the Epic/GOG/Amazon store +engines in a complete release build. + +## Automated checks + +On the rebased implementation, the complete `:app:testDebugUnitTest` run passed: +**2,230 tests across 333 suites, zero failures/errors/skips**. + +The supported `tools/build_local.sh` debug build also passed, including session +asset matching, runtime-input attestation, native export/dependency validation, +ZIP alignment and signature verification. The APK SHA-256 was +`a7d440b4029f9ad67ca6f44c92ccdd6b9eba9e9a758b28ab0e432256697d49b9`. +The only local environment repair was moving a Finder `.DS_Store` file out of the +NDK's host-toolchain directory; the upstream script counted it as another host. +No build checks were disabled to get past the stale-runtime check after rebasing. + +Commands, with Java/SDK paths configured for the local machine: + +```sh +./gradlew :app:testDebugUnitTest -PskipRust=true -PndkVersion=28.2.13676358 +DROIDDECK_BUILD_VARIANT=debug DROIDDECK_SKIP_RUST=1 \ + DROIDDECK_NDK_VERSION=28.2.13676358 bash tools/build_local.sh +# Linux with gcc and g++ available: +python3 -m unittest tools.tests.test_fakeinput_open -v +``` + +Coverage includes profile persistence and migration, invalid bindings, shared +holds, key order, trigger/direction hysteresis, neutral gating on profile changes, +source disposal, raw BLE report preservation, physical key capture, global reset, +existing controller/gesture/keyboard paths, pointer handoff, and Compose settings +and layout editor behavior. + +Two additional ownership regressions were demonstrated failing before their +fixes, then passed in the full suite: + +- Removing the last local touch surface must send the final release to the legacy + Epic mouse route, while removing only one of two holds must preserve the other. +- Concurrent native Deck pad surfaces must retain the first contact's coordinates + until it ends, then transfer ownership and release correctly. + +The native preload regression probe compiles the actual library and compares +`open`, `openat`, `open64` and `openat64` with libc. It covers ordinary file +creation, `O_TMPFILE` permissions, and opening a directory without a mode argument +(12 combinations). The temporary-file fix addresses the KDE cache permissions +problem encountered during the Desktop test; it does not change controller +transport or publication ordering. + +All three test methods passed on Linux x86-64 after rebasing. The same 12 +combinations also passed in the earlier i386 probe. + +Earlier native testing also exposed three i386 ring-test failures, reproduced on +the unmodified pre-feature baseline. These are not presented as passing or fixed +by this PR. Physical BLE coverage is likewise not implied by synthetic tests. + +### PR review follow-up + +After the first CodeRabbit review of PR #1439, nine regression tests were added +for deferred pad-ring opening, releases to an already-open ring, system-owned +Steam/VR actions, and retained input when replacing a router. Seven of the new +scenarios reproduced failures before the fixes. The retained-input tests cover +physical and touch button ownership, native sticks, Deck grips and trackpad +contacts; a separate regression verifies return to the native identity fast path +without scheduling output after held controls reach neutral. + +The full `:app:testDebugUnitTest` run now passes **2,239 tests across 334 suites, +zero failures/errors/skips**. `:app:assembleDebug` also passes with the existing +verified native inputs and the same Java/SDK/NDK and `-PskipRust=true` settings. +The resulting APK passes `check_session_assets.py` and `runtime_inputs.py apk`; +its SHA-256 is +`1c77c85d5d08c4a054db0528ade33d60eaf8755c09a56a598f342207b27c85d4`. + +These follow-up changes have not been installed on Thor or benchmarked on the +device. Retained-state initialization runs when sources attach; ordinary input +keeps its existing synchronous routing and native identity path. The VR command +uses the existing chord handler for its release, retaining the 120 ms pulse. + +### SELECT and panel ownership follow-up + +The next review covered SELECT-to-GUIDE routing and child-view ownership. The +existing one-time Select-as-Steam migration is exercised through real controller +key events, including simultaneous physical GUIDE ownership. Explicit native, +disabled and local SELECT assignments, and resetting the Game profile, remain +authoritative. Removing the obsolete bridge flag keeps raw SELECT state stable +when a router is attached or removed while the button is held. + +Panel regressions use an attached Android view hierarchy and a two-finger +trackpad contact. Switching the other stick to a trackpad, or removing the D-pad +or face buttons, must release only that child's controls and preserve the other +trackpad's contact, coordinates and click. Whole-panel removal still releases +the shared source. These three panel scenarios and the retained SELECT scenario +failed before the fixes; all seven new tests pass after them. + +The complete JVM run passes **2,246 tests across 336 suites, zero failures/errors/ +skips**. The debug APK build, session-asset check and runtime-input verification +pass with the same local build settings. APK SHA-256: +`0d85b4003cd6b31084e50d603e8b00f239775c9c97f3ec17327264db5dd50612`. +These changes have host coverage only; Thor validation and new latency +measurements remain pending. + +### Panel tab teardown follow-up + +Tab changes now remove the old child views before releasing the panel's touch +source. Child detach callbacks can publish button releases, so source cleanup +must run after those callbacks to avoid retaining a neutral source. + +The new tab-switch regression fails before this fix and passes after it. It +checks held-button release, removal of the old source before later physical +input, and touch input after returning to the Gamepad tab. A second regression +confirms that whole-panel removal already releases the source correctly. +All **22 targeted tests** in the panel ownership, input ownership and second-screen +suites pass. The debug APK build, session-asset check and runtime-input +verification also pass. This follow-up has not been tested on Thor or benchmarked +on the device. + +### Upstream integration (2026-10-27) + +Merged upstream `43a0ce93` while preserving the published PR history. The merge +retains upstream screen keys, portrait controls and Home/Back shortcuts alongside +the input profiles and custom second-screen layouts. Screen keys have their own +held-key owner, and explicit physical Back bindings take precedence over the +default QAM shortcut. + +Queued Home/Back chord reports now use current routed axes, including touch +sources, rather than raw physical axes. Router/profile changes and disabled +routing discard queued reports. Cancelling a chord or releasing all input keeps +held controls blocked until neutral, including Deck grips. Ordinary routed input +still publishes synchronously without waiting for the chord queue. + +Eleven integration regressions cover these interactions. Four routed-axis and +cancellation scenarios failed before their fixes. The existing panel ownership +fixture now explicitly selects a Deck controller, matching upstream's restriction +of native trackpad controls to that controller type. + +The complete `:app:testDebugUnitTest` run passes **2,632 tests across 385 suites, +zero failures/errors/skips**, using Java 17, NDK `28.2.13676358` and +`-PskipRust=true`. This compiles the Android Java/Kotlin code and resources; a new +APK build, Thor validation and device latency measurements were not performed +for this merge. + +### QAM and repeated enable-state review (2026-10-27) + +A routed QAM press during an existing emulated chord now publishes simultaneous +button and stick changes immediately. Repeated enable/disable requests no longer +republish unchanged pad or Deck reports; the router reports whether its state +changed, so the bridge does not keep a second copy of that state. + +Both regressions failed before the fixes. Two additional tests cover replacing a +disabled router and enabling a router that was disabled before attachment. The +complete `:app:testDebugUnitTest` run passes **2,636 tests across 385 suites, zero +failures/errors/skips**, with the same Java/SDK/NDK settings and `-PskipRust=true`. +These fixes have not been tested or benchmarked on Thor. + +### Upstream Home/player-slot integration (2026-10-28) + +Merged upstream `213c2da5` without rewriting the published branch. Home/Guide now +follows upstream's primary-player slot selection when the built-in pad is off, +while its report retains routed buttons and axes. The input-settings overlay +continues to gate Home shortcuts and now also gates upstream's Steam volume-key +handling. + +A new regression combines Home on player slot 1 with button/stick remapping and +disabled routing. The complete `:app:testDebugUnitTest` run passes **2,707 tests +across 389 suites, zero failures/errors/skips**, using the same Java/SDK/NDK +settings and `-PskipRust=true`. A new APK build, Thor validation and latency +measurement were not performed for this merge. + +### Upstream shortcuts and dual-screen integration (2026-10-30) + +Merged upstream `6ffe675a` without rewriting the published branch. The merge +preserves upstream's docked second-screen menu, keyboard routing, remembered +panel selection, touch routing and physical shortcuts from #1202, together with +the active input profiles and custom lower-screen layouts. + +Captured profile keys take precedence over global physical shortcuts, which in +turn precede default Back/QAM handling. Both use the original device/key identity +before Joy-Con normalization. Input-profile editing blocks shortcut execution; +consumed releases remain consumed when a shortcut opens or closes the menu. +The upstream shortcut configuration remains accessible from the input settings. + +Mapped mouse output uses upstream's existing Wayland/raw-mouse helpers, retaining +shared button ownership. Changing the raw-mouse backend releases held buttons on +the outgoing backend. Touchpad zoom has an independent Ctrl owner, so it cannot +release Ctrl held by the profile or screen keys. Mapped pointer movement retains +the frame-callback rendering update and cursor visibility behavior. + +Eight new regressions cover these interactions and reopening a custom panel with +either controller type. The upstream shortcut-page UI test now exercises the +replacement Input profiles page. The complete `:app:testDebugUnitTest` run passes +**2,918 tests across 413 suites, zero failures/errors/skips**, using Java 17, +NDK `28.2.13676358` and `-PskipRust=true`. The PR diff against upstream also passes +`git diff --check origin/main`. + +This run compiles Android Java/Kotlin and resources. A new APK build, Thor test +and latency measurement were not performed for this merge; the full release +build remains an upstream CI check. + +### Upstream PC input and bottom-screen integration (2026-11-04 UTC) + +Merged upstream `f0052415` without rewriting the published branch. The merge +retains the common bottom-screen bar, dashboard, KDE desktop panel, game-screen +swap, hardware mouse/keyboard fixes and on-screen stick redraw optimization. +Custom controls use the same bar and preserve their secondary-source bindings +when the game and panel swap displays. Pointer handoff now lives in upstream's +shared `SessionTrackpadView`, used in either display arrangement. + +Physical/IME key translation reuses `GuestKeys`, including keyboard Escape scan +codes and the Desktop right-Ctrl alias, while preserving shared key ownership. +The input-profile editor releases upstream's hardware mouse capture. Routed +on-screen stick movement uses the same redraw throttling as the native overlay; +pad publication remains synchronous, and mapped cursor frame updates keep their +existing cadence mechanism. + +The pointer-handoff regression now exercises the extracted trackpad view. Menu +tests cover the custom panel alongside upstream's panel choices. A new ownership +regression checks that releasing a hardware right-Ctrl alias cannot release Ctrl +still held by the active profile. Panel teardown tests now use `ComponentActivity` +to supply the lifecycle required by upstream's Compose bar. + +The complete `:app:testDebugUnitTest` run passes **3,281 tests across 464 suites, +zero failures/errors/skips**, using Java 17, NDK `28.2.13676358` and +`-PskipRust=true`. Upstream's pinned MaliBridge submodule is initialized and its +required Android server build also succeeds. The PR diff passes +`git diff --check origin/main`. + +No new APK installation, Thor regression pass or latency measurement was performed +for this merge. The earlier device measurements do not validate this exact tree. + +## Manual AYN Thor results + +The user performed the following checks in the installed candidate. The desktop +application was KWrite; native gamepad input was checked in Steam's controller +input test. A loaded game's input-to-display response was not measured. + +| Area | Observed result | +| --- | --- | +| Native Game profile | Physical buttons, D-pad, both sticks/clicks and triggers work without observed missed input or stuck controls | +| Desktop defaults | Left stick moves pointer; A/B click; X/Y provide Enter/Escape; D-pad supplies arrows; right stick scrolls and stops at neutral | +| Scroll direction | Stick down advances to higher line numbers; stick up returns to earlier lines | +| Cursor visibility | Controller buttons and stick movement no longer hide the mapped pointer | +| Pointer handoff | Lower trackpad and stick can alternate without a jump; click position matches cursor | +| Cursor cadence | User confirmed smooth movement after the frame update fix; measurements below | +| Android keyboard | Text entry works on the lower screen, including while moving the pointer; controls continue after hiding the keyboard | +| Configured shortcut | R3 assigned to Ctrl+F through the settings UI repeatedly opens search; Y/Escape closes it; subsequent typing has no stuck Ctrl | +| Combined keyboard input | Physical modifier with Android keyboard supports Ctrl+A, Ctrl+X, Ctrl+V and Ctrl+S | +| Custom lower screen | Right pad moves pointer, left pad scrolls, A/B click, and View/Select opens the keyboard | +| Shared mouse hold | Physical A and screen A can hold together; releasing the physical button preserves selection until the screen button is released | +| Menu/held input | User reported successful menu/Ctrl release and ordinary typing afterward | +| Android Home/resume | Physical stick, lower trackpad, clicks and keyboard shortcuts work after returning | +| Layout persistence | Removing/readding a button through the editor persists across APK update and restart; defaults restored through the editor | +| Label rendering | Long View/Select and Menu/Start labels fit within their buttons | +| Native Deck lower screen | Both trackpads, clicks, L4/L5/R4/R5 and physical controls work in Steam's input test | + +The editor's individual compatible-native and disabled options are host-tested, +not an exhaustive manual hardware matrix. Menu is currently an open command; +Back/B closes it. Multi-button activation chords are not implemented. + +## Relative latency measurement + +### What was measured + +On the same Thor, timestamps from the built-in controller's evdev stream using +`CLOCK_MONOTONIC` were matched to a read-only guest observer of the published +controller ring. The observer polled with a requested 250 microsecond sleep, +roughly 0.31 ms in practice. Each observation bounds receipt between polls; the +table reports the **upper bound**, not an exact physical latency. + +This path includes Android dispatch and publication. It excludes controller +firmware, Steam Input/game processing, rendering and display scanout. USB was +used for control and collection, not as the timed input transport. This is not a +button-to-photon test. + +The public 0.6.0 build (`22eac083`) was sampled first. A locally instrumented +baseline at `ac0909ea` was then compared with the feature candidate; their relevant +baseline Java/native input implementation matched. Both used Steam Deck emulation, +1280×720 at 60 Hz, Steam's main screen, and the same disabled low-latency options. + +The manual samples used physical B presses. Automatic samples injected B edges +into the built-in controller's evdev path, continuing through normal Android +input dispatch. Settled paired runs used a 150-press warmup before 150 measured +presses (300 edges) at approximately 33°C battery temperature. Earlier preliminary +runs were affected by background preparation and are kept separate. + +### Results + +All values below are milliseconds, kernel event to observer upper bound. + +| Run | Matched edges | p50 | p95 | p99 | Max | +| --- | ---: | ---: | ---: | ---: | ---: | +| Public 0.6.0, manual | 84 | 4.379 | 8.155 | 9.334 | 9.729 | +| Local baseline, manual | 82 | 3.876 | 8.322 | 10.667 | 12.447 | +| Baseline automatic, preliminary | 300 | 3.213 | 6.774 | 7.960 | 8.688 | +| Candidate automatic, preliminary | 300 | 2.694 | 5.197 | 6.725 | 8.164 | +| Candidate automatic, settled 1 | 300 | 3.403 | 6.503 | 9.114 | 10.167 | +| Baseline automatic, settled | 300 | 3.061 | 6.269 | 7.160 | 7.850 | +| Candidate automatic, settled 2 | 300 | 2.999 | 5.620 | 8.171 | 13.653 | +| Final installed candidate, extra check | 300 | 3.039 | 5.905 | 7.470 | 11.684 | + +The extra final-APK check matched all 300 edges without an observed overrun or +extra/missing edge. Its p50 interval was 2.692–3.039 ms. It ran at 32°C with internal +timing disabled, without another paired baseline or the explicit 150-press warmup; +it is supporting regression evidence, not a controlled speed comparison. + +A separate internal sample of 600 dispatch-to-publication observations reported: + +| Build | p50 (µs) | p95 (µs) | p99 (µs) | +| --- | ---: | ---: | ---: | +| Baseline | 112.6 | 274.2 | 442.2 | +| Candidate | 116.9 | 315.2 | 561.7 | + +The 4.3 µs median difference is the difference between those runs, **not an +isolated measurement of router cost**. The tested typical native path remained +within baseline variation. Candidate tail values were higher in some runs and +were not isolated to a cause. No claim of a speedup, zero cost, or unchanged worst +case is supported by these samples. + +### Cursor frame cadence + +The initial stick-driven cursor updated at a median 33.333 ms interval, while +the lower trackpad updated at 16.667 ms. Publishing pending Compose snapshot +notifications before the frame's draw traversal changed the stick median to +16.667 ms. Of 119 intervals, 115 were 14–20 ms and none were 30–37 ms. The remaining +four included longer start/stop gaps, so these are not a continuous-load frame-time +distribution. The user confirmed the visible improvement. Speed, sensitivity and +the stick response curve were unchanged. + +This is a rendering cadence check, separate from event propagation latency. + +## Remaining validation + +- Repeat the short Thor regression pass on the rebased final branch. +- Measure simultaneous BLE and touch input, contention and allocations on hardware. +- Compare Desktop key/pointer and native Deck touch latency quantitatively. +- Repeat under a loaded game/rendering workload and, if needed, measure display + response with high-speed video. +- Exercise physical Bluetooth devices and other device-specific adapters. +- Let upstream CI build and test the complete release configuration, including + store engines omitted from the local Steam/Desktop APK. + +These limits should remain visible in the PR while maintainers review the +architecture. Host tests and a successful APK build do not replace those checks. diff --git a/docs/lab-builds.md b/docs/lab-builds.md new file mode 100644 index 0000000000..4f2ddfb5f0 --- /dev/null +++ b/docs/lab-builds.md @@ -0,0 +1,63 @@ +# Lab builds + +A Lab build is DroidDeck app code and resources built on the device itself, by the on-device agent +or by hand, and loaded over the installed APK at the next start. Nothing is reinstalled or +re-signed, so a tester on a release build keeps their games, sign-ins and settings while trying a +change. + +## How it works + +- `tools/agent/dd-lab-build` builds the checkout with Gradle on the device (`processReleaseResources` + and `mergeDexRelease`), packs the linked resources and dex files into `overlay.apk`, writes + `overlay.json`, and stages both in the app's `files/lab/next`. It then recompiles the app without + an app image and restarts DroidDeck. +- Run by the app's own agent (`DD_AGENT` set), it writes `files/agent/lab-request` instead, and the + app runs the build in a process of its own, logging to `~/.cache/dd-lab-build.log`; the agent's + copy follows that log and exits with the build. A turn's processes end with the turn, so a build + started there would otherwise die when the agent stops or finishes early. +- At start, `LabOverlay` (called from `App.attachBaseContext`) promotes `next` to `active`, checks + that the build was made for this install, puts its dex files ahead of the APK's, and adds its + resources through `ResourcesLoader` to the app and every activity. Contexts made from them + (presentations on the second screen) inherit the loader. +- The watermark shows `LAB | @ `. When the app comes back on a new build it posts + a "Lab build installed" notification, so the restart does not look like a crash. +- A build that is replaced, turned off or refused is kept in `files/lab/builds` (the newest 12). + Updates lists them under Lab builds: the one in use can be turned off, one built for this + install can be used again, and any can be deleted. + +## What a Lab build cannot change + +The manifest, native code (`cpp`, `jniLibs`, Rust), Gradle files, `App`, `AppLanguage`, and `LabOverlay` itself: +they are loaded before a Lab build can be, or are not part of it. `dd-lab-build` refuses those +changes; they need a new APK. + +## Safety + +- A build is only used on the install it was built for: `overlay.json` carries the APK's commit + (`apkCommit` in `droiddeckctl state`) and the APK's path, which changes with every install and + update. A build for another install is turned off. +- Two starts in a row that do not reach a resumed activity for 10 seconds turn the build off. +- It needs Android 11 or later (`ResourcesLoader`) and no app image (classes in one load before the + Lab build). `dd-lab-build` runs `cmd package compile -m verify` before restarting. Android's + background optimization can bring an app image back; the build is then paused, a notification + says so, and Restart in Updates recompiles through the app's wireless debugging pairing. + +## Setting up a device + +From the DroidDeck terminal, in a checkout of this repository: + +```sh +tools/agent/setup +``` + +It installs Claude Code, git, gh and loopback adb, plus the build toolchain: JDK 17, the Android +SDK platform, and Google's x86-64 `aapt2` run under `qemu-user`. The first Lab build compiles the +whole app and takes about 8 minutes on a Snapdragon 865; later ones compile only what changed. +That needs the Kotlin daemon: Kotlin's in-process compiles always redo the whole app. BuildConfig +is given the installed commit, so committing a change does not change it. Setup +does that first build ahead of time with `dd-lab-build --prepare`, which compiles the installed +commit without making a Lab build. +`~/.cache/dd-lab-build.json` reports the stage, the current task, progress in thousandths and the +seconds left, estimated from how long each task took the last time (`~/.cache/dd-lab-build-times`). +`~/.cache/dd-lab-build-base` holds the installed commit the last successful build compiled, so the +Agent tab can offer to prepare again after DroidDeck updates. diff --git a/docs/releases/0.7.0.md b/docs/releases/0.7.0.md new file mode 100644 index 0000000000..c798d0d9d0 --- /dev/null +++ b/docs/releases/0.7.0.md @@ -0,0 +1,166 @@ +# DroidDeck 0.7.0 + +

+ Download DroidDeck 0.7.0 + Join the DroidDeck Discord +

+ +**DroidDeck is 100% open source. No ads. No telemetry. No proprietary components. No source withheld.** + +Since 0.6.9, **39 people** sent **194 pull requests**, and **20 of them are first-time contributors**. Our community is strongest when we can build together, and with DroidDeck 0.7.0, our community is about to get A LOT bigger! + +0.7.0 introduces preliminary support for **Mali, PowerVR and Xclipse** GPUs! In addition, we now have GameJolt library and store, dual screen game mode + KDE Desktop, Syncthing integration, improvements for devices with low ram, customizable controller profiles and presets, hardware mouse & keyboard support, live wallpapers, and even more. Click the link in the comments below to... I mean scroll down to learn more: + +### Features + +

A phone showing DroidDeck Setup with GPU via Mali bridge turned on, in front of handhelds, one of them running Steam

+ +- Initial **Mali, PowerVR and Xclipse support**. DroidDeck detects the GPU and picks a driver for it, so these devices can run sessions, with MaliBridge and Venus on Mali and open driver pairs elsewhere. (@ASEbrahim, @mr-abccc, @The412Banner, @wallentx, @Leask, @stefanz92, @DavidCimo, @maxjivi05, @K11MCH1, @xXJSONDeruloXx, [#1587](https://github.com/Droid-Deck/DroidDeck/pull/1587), [#2027](https://github.com/Droid-Deck/DroidDeck/pull/2027), [#1950](https://github.com/Droid-Deck/DroidDeck/pull/1950), [#2029](https://github.com/Droid-Deck/DroidDeck/pull/2029), [#2157](https://github.com/Droid-Deck/DroidDeck/pull/2157)) + +

A Retroid Pocket Duo with the KDE desktop on the top screen and Rayman 2 running on the bottom screen The KDE desktop on the bottom screen of an AYN Thor running fastfetch, then opening a file manager, while Steam stays on the top screen

+ + +- Added the **KDE desktop on the bottom screen** beside a Steam session or a game, next to Dashboard, Keyboard + trackpad, Terminal and Deck grips. (@xXJSONDeruloXx, [#1966](https://github.com/Droid-Deck/DroidDeck/pull/1966)) +- Added **swap screens**: play on the bottom screen while the top screen shows the Dashboard or the desktop. Use the swap button in the panel bar or **Show the game on** in the session menu. (@The412Banner, [#2006](https://github.com/Droid-Deck/DroidDeck/pull/2006)) + +

An AYN Thor with Steam's Storage page on the top screen listing internal storage, the SD card and a shared library, and the Storage Manager on the bottom screen

+ +- Added **shared Steam libraries with other pc gaming apps**. DroidDeck can now **find Steam libraries that other apps already made** on your internal storage or card and suggests them, each with its game count, so one install of a game can serve both apps. You can add up to four more libraries beside internal storage and the card, rename them, and remove them any time. (@xXJSONDeruloXx, [#1967](https://github.com/Droid-Deck/DroidDeck/pull/1967), [#2023](https://github.com/Droid-Deck/DroidDeck/pull/2023), [#2054](https://github.com/Droid-Deck/DroidDeck/pull/2054)) + +

The Game Jolt store in the Stores tab with price, platform, maturity and status filters above a grid of games

+ +- Added the **Game Jolt store**: browse, search, install, update and keep a library, in the same style as itch.io. (@FX192, [#1986](https://github.com/Droid-Deck/DroidDeck/pull/1986), [#1989](https://github.com/Droid-Deck/DroidDeck/pull/1989)) + +

The Syncthing page in Setup with Open web UI, Pause, Add folder and a Running switch

+ +- Added built-in **Syncthing**, so you can sync saves and games from DroidDeck’s own storage without another app. (@superboo07, [#1442](https://github.com/Droid-Deck/DroidDeck/pull/1442)) + +

An AYN Thor: Redout on the top screen and the Dashboard with In-game stats on the bottom screen

+ +- Added **bottom-screen Dashboard** for dual-screen handhelds like the Retroid Pocket Duo and AYN Thor: downloads, notifications, screenshots, Steam chat with voice controls, and **In-game stats** that draw a racing game’s own speed, gear, rpm and pedals from its data output. (@The412Banner, [#1920](https://github.com/Droid-Deck/DroidDeck/pull/1920), [#1933](https://github.com/Droid-Deck/DroidDeck/pull/1933), [#1968](https://github.com/Droid-Deck/DroidDeck/pull/1968)) + +

The Strip HUD across the top of Steam on an AYN Thor: FPS, frame-time graph, lows, GPU, CPU, VRAM, RAM, battery and clock

+ +

Dragging the HUD to the screen edge shrinks it into a small FPS tab, and tapping the tab opens it again

+ +- Added a **Horizontal DroidDeck HUD Style**: one line across the top or bottom edge with FPS, frame-time graph, lows, GPU, CPU, VRAM, RAM, battery and the clock. All DD HUDs can now **shrinks into its tab as you drag it off an edge** and snaps to the sides. The Strip is lighter to draw. (@xXJSONDeruloXx, @The412Banner, @KeegantheVeganat0r, [#1935](https://github.com/Droid-Deck/DroidDeck/pull/1935), [#1937](https://github.com/Droid-Deck/DroidDeck/pull/1937), [#1982](https://github.com/Droid-Deck/DroidDeck/pull/1982), [#2194](https://github.com/Droid-Deck/DroidDeck/pull/2194), [#2235](https://github.com/Droid-Deck/DroidDeck/pull/2235)) +- A **hardware mouse and keyboard work like a PC**: the camera no longer freezes in games that hide the cursor, the cursor shows in Steam sessions, and clicks land where you point. (@maxjivi05, [#2053](https://github.com/Droid-Deck/DroidDeck/pull/2053), [#2133](https://github.com/Droid-Deck/DroidDeck/pull/2133), [#2139](https://github.com/Droid-Deck/DroidDeck/pull/2139)) +

The layout editor on the second screen of a dual-screen handheld, with the right trackpad selected and its Properties, Click properties and Remove control buttons above the full-size canvas

+ +- Added **unified controller profiles** for Game and Desktop sessions: named profiles combine a physical controller layout with main and second screen layouts, with one full-size editor for both screens and a profile picker in the session menu. (@Delete228, @maxjivi05, @xXJSONDeruloXx, [#1439](https://github.com/Droid-Deck/DroidDeck/pull/1439), [#2215](https://github.com/Droid-Deck/DroidDeck/pull/2215), [#2221](https://github.com/Droid-Deck/DroidDeck/pull/2221), [#2238](https://github.com/Droid-Deck/DroidDeck/pull/2238), [#2242](https://github.com/Droid-Deck/DroidDeck/pull/2242)) +- Added **Startup** choices: Never, Steam or Desktop. (@xXJSONDeruloXx, [#1993](https://github.com/Droid-Deck/DroidDeck/pull/1993)) +- Added a **low-storage warning** switch, and an **Android keyboard for game text fields** setting for dual-screen sessions, off by default so Steam keeps its own keyboard. (@xXJSONDeruloXx, [#2244](https://github.com/Droid-Deck/DroidDeck/pull/2244), [#2236](https://github.com/Droid-Deck/DroidDeck/pull/2236)) +- Added **live wallpapers** to Android’s wallpaper picker. (@xXJSONDeruloXx, [#2012](https://github.com/Droid-Deck/DroidDeck/pull/2012)) + +### Improvements + +- **Auto picks DD-Turnip on Adreno 7xx**, and the **upstream RADV pair on Xclipse 950 and 960**. Auto follows the published table on Adreno 6xx and 8xx, and a driver source that is down no longer changes the pick. (@maxjivi05, @xXJSONDeruloXx, [#1978](https://github.com/Droid-Deck/DroidDeck/pull/1978), [#2158](https://github.com/Droid-Deck/DroidDeck/pull/2158), [#2151](https://github.com/Droid-Deck/DroidDeck/pull/2151), [#2233](https://github.com/Droid-Deck/DroidDeck/pull/2233), [#2255](https://github.com/Droid-Deck/DroidDeck/pull/2255), [#2226](https://github.com/Droid-Deck/DroidDeck/pull/2226)) +- A **configurable stick dead zone** for physical controllers, 0 to 25%. (@vitamin663-sys, [#1780](https://github.com/Droid-Deck/DroidDeck/pull/1780)) +- The drawer’s **frame limit works in desktop mode**, up to 165 fps. (@K11MCH1, [#1885](https://github.com/Droid-Deck/DroidDeck/pull/1885)) +- **Faster Steam**: compatibility mapping and app info load in batches, and Steam shows cached library art when it starts without a connection. SD and USB libraries answer missing-file checks from a directory cache. (@ddd-ms, @xXJSONDeruloXx, [#1755](https://github.com/Droid-Deck/DroidDeck/pull/1755), [#1756](https://github.com/Droid-Deck/DroidDeck/pull/1756), [#1985](https://github.com/Droid-Deck/DroidDeck/pull/1985), [#2212](https://github.com/Droid-Deck/DroidDeck/pull/2212), [#2203](https://github.com/Droid-Deck/DroidDeck/pull/2203)) +- Added **Enhanced diagnostics**, a one-session setting that bundles redacted session, graphics and game logs for bug reports, plus **session log management** in Setup. (@wallentx, @xXJSONDeruloXx, [#2129](https://github.com/Droid-Deck/DroidDeck/pull/2129), [#2099](https://github.com/Droid-Deck/DroidDeck/pull/2099), [#2131](https://github.com/Droid-Deck/DroidDeck/pull/2131)) +- Added **auto updates for FEX, DXVK and VKD3D-Proton in DroidDeck-Proton**, so it gets the newest builds without a new DroidDeck release. Other Protons keep the components they ship with, old DroidDeck-Proton builds are removed, and a game set to a removed build moves to the newest one. (@maxjivi05, @xXJSONDeruloXx, [#2156](https://github.com/Droid-Deck/DroidDeck/pull/2156), [#2190](https://github.com/Droid-Deck/DroidDeck/pull/2190), [#2193](https://github.com/Droid-Deck/DroidDeck/pull/2193), [#2223](https://github.com/Droid-Deck/DroidDeck/pull/2223)) +- An installed **DroidDeck-Proton becomes the default** for Steam games and added shortcuts when nobody picked Proton Experimental. A Proton you choose in DroidDeck or in Steam is kept, and both stay in sync. (@maxjivi05, [#2266](https://github.com/Droid-Deck/DroidDeck/pull/2266)) + +- **Little Nightmares** keeps its render thread, **+12.7% FPS** on the Retroid Pocket Duo, and **Adreno 722** has fewer DXVK stalls. (@maxjivi05, [#1930](https://github.com/Droid-Deck/DroidDeck/pull/1930), [#2066](https://github.com/Droid-Deck/DroidDeck/pull/2066)) +- A resumed session reads its memory back from zram at once, which ends minutes of stutter in heavy games like Yakuza. DroidDeck also frees launcher memory earlier under pressure, and idle game memory is compressed before low-RAM devices run out. (@l4nr3, @xXJSONDeruloXx, [#1839](https://github.com/Droid-Deck/DroidDeck/pull/1839), [#2138](https://github.com/Droid-Deck/DroidDeck/pull/2138), [#2187](https://github.com/Droid-Deck/DroidDeck/pull/2187)) +- On-screen sticks are lighter to hold, and **Low latency off** is honored. (@maxjivi05, [#2048](https://github.com/Droid-Deck/DroidDeck/pull/2048)) +- The HUD cycles colors with the Inner Gamer theme, the docked FPS tab and bottom-screen frame graph look better, and the watermark is gone with the wordmark in headers. (@xXJSONDeruloXx, [#1932](https://github.com/Droid-Deck/DroidDeck/pull/1932), [#1976](https://github.com/Droid-Deck/DroidDeck/pull/1976), [#2013](https://github.com/Droid-Deck/DroidDeck/pull/2013), [#2011](https://github.com/Droid-Deck/DroidDeck/pull/2011), [#2003](https://github.com/Droid-Deck/DroidDeck/pull/2003), [#1983](https://github.com/Droid-Deck/DroidDeck/pull/1983)) +- Every session log now records **CPU clocks, thermal status and battery temperature**, and `droiddeckctl` gained `thermal` and `wait cool`. (@angelrenefm, [#2060](https://github.com/Droid-Deck/DroidDeck/pull/2060), [#2061](https://github.com/Droid-Deck/DroidDeck/pull/2061), [#2062](https://github.com/Droid-Deck/DroidDeck/pull/2062)) +- Added games scan **one at a time and keep what they found** across launches, and imported Steam games with no known build update only when launched. (@K11MCH1, [#2067](https://github.com/Droid-Deck/DroidDeck/pull/2067), [#2064](https://github.com/Droid-Deck/DroidDeck/pull/2064)) +- Steam’s **VRAM budget** is applied through Vulkan without rejecting allocations, and the **download monitor** no longer freezes a session on slow storage. D3D12 support is detected per device. (@xXJSONDeruloXx, [#2014](https://github.com/Droid-Deck/DroidDeck/pull/2014), [#2020](https://github.com/Droid-Deck/DroidDeck/pull/2020), [#2134](https://github.com/Droid-Deck/DroidDeck/pull/2134)) +- Every bottom-screen panel now has the **same bar**, and X or Back opens the in-session menu there instead of dropping you on the Android launcher. (@xXJSONDeruloXx, [#2084](https://github.com/Droid-Deck/DroidDeck/pull/2084), [#2110](https://github.com/Droid-Deck/DroidDeck/pull/2110)) +- On the Duo and Thor, DroidDeck switches to **single-screen mode** while another window holds the bottom screen, such as Retroid’s performance panel, and back when it is free, including when docked. The trackpad and Deck panels are back on the second screen, and the bottom-screen menu and layout editor stay open when the screen turns off. (@maxjivi05, [#1979](https://github.com/Droid-Deck/DroidDeck/pull/1979), [#2232](https://github.com/Droid-Deck/DroidDeck/pull/2232), [#2229](https://github.com/Droid-Deck/DroidDeck/pull/2229), [#2247](https://github.com/Droid-Deck/DroidDeck/pull/2247)) +- The HUD shows **GPU load and clock without root** on phones where Android blocks them, such as the Retroid Pocket Duo and Adreno 8xx, and in Steam’s overlay. (@The412Banner, @angelrenefm, [#1969](https://github.com/Droid-Deck/DroidDeck/pull/1969), [#1981](https://github.com/Droid-Deck/DroidDeck/pull/1981), [#2059](https://github.com/Droid-Deck/DroidDeck/pull/2059)) +- Added independent **1% and 0.1% low** toggles to the HUD. (@KeegantheVeganat0r, [#2127](https://github.com/Droid-Deck/DroidDeck/pull/2127)) +- **EA games install the EA app** through Steam, and the EA sign-in stays in front of the launcher’s splash screen. (@K11MCH1, [#2036](https://github.com/Droid-Deck/DroidDeck/pull/2036), [#2037](https://github.com/Droid-Deck/DroidDeck/pull/2037), [#2045](https://github.com/Droid-Deck/DroidDeck/pull/2045)) +- The **desktop session follows the app language**, so Plasma and its apps are no longer English only. (@xXJSONDeruloXx, [#1971](https://github.com/Droid-Deck/DroidDeck/pull/1971)) +- The desktop shows **programs installed by Windows installers** in its menu and as icons. (@K11MCH1, [#2146](https://github.com/Droid-Deck/DroidDeck/pull/2146)) +- **Follow screen** resolution can be scaled down: ÷1.5, ÷2, ÷2.5, ÷3, ÷4 or your own factor. (@xXJSONDeruloXx, [#1964](https://github.com/Droid-Deck/DroidDeck/pull/1964)) +- Added a **Hold Back for Steam shortcuts** switch, for people who map a held Back with another app. (@xXJSONDeruloXx, [#1955](https://github.com/Droid-Deck/DroidDeck/pull/1955)) +- Added **size, opacity and hidden keys** settings for the on-screen PC keyboard. (@Dantalian-dev, [#1897](https://github.com/Droid-Deck/DroidDeck/pull/1897)) +- Added **World of Warcraft addon import** from a zip, plus **voice type and a keyboard chat bar**. Addons also download from WoWInterface, and CurseForge opens in the browser. (@maxjivi05, [#2111](https://github.com/Droid-Deck/DroidDeck/pull/2111), [#2171](https://github.com/Droid-Deck/DroidDeck/pull/2171), [#2175](https://github.com/Droid-Deck/DroidDeck/pull/2175), [#2219](https://github.com/Droid-Deck/DroidDeck/pull/2219)) +- **UBWC compatibility** moved from Components to Settings, and FEX’s **Use Default** shows which preset it resolves to. (@xXJSONDeruloXx, [#2008](https://github.com/Droid-Deck/DroidDeck/pull/2008), [#2225](https://github.com/Droid-Deck/DroidDeck/pull/2225)) +- **Steam launch options** show on the Launch tab. (@wallentx, [#2163](https://github.com/Droid-Deck/DroidDeck/pull/2163)) +- **GOG achievements** are on by default. (@xXJSONDeruloXx, [#2168](https://github.com/Droid-Deck/DroidDeck/pull/2168)) +- **Translations:** every missing string is translated, with a fresh Russian pass. (@maxjivi05, @flexeykinDev, [#1965](https://github.com/Droid-Deck/DroidDeck/pull/1965), [#2116](https://github.com/Droid-Deck/DroidDeck/pull/2116)) + +### Fixed + +- Fixed **“DroidDeck isn’t responding”** on dual-screen handhelds after the bottom screen sat idle, and opening DroidDeck from iisu on the AYN Thor no longer bounces back. (@maxjivi05, @0x1428571429, [#1917](https://github.com/Droid-Deck/DroidDeck/pull/1917), [#1901](https://github.com/Droid-Deck/DroidDeck/pull/1901)) +- Fixed **lag and heat on the Galaxy S26** from switching between display modes with the same refresh rate, which started in 0.6.7. (@maxjivi05, [#2192](https://github.com/Droid-Deck/DroidDeck/pull/2192)) +- Fixed **Steam starting on Quest 3**, and the gamescope abort at start on Quest. (@xXJSONDeruloXx, @Mi-Yomi, [#2050](https://github.com/Droid-Deck/DroidDeck/pull/2050), [#1938](https://github.com/Droid-Deck/DroidDeck/pull/1938)) +- Fixed **Steam libraries** with installs but no app manifest not being found, and **a stale session’s startup failure** stopping the newer session. (@xXJSONDeruloXx, [#2054](https://github.com/Droid-Deck/DroidDeck/pull/2054), [#1984](https://github.com/Droid-Deck/DroidDeck/pull/1984)) +- Fixed an **unplugged game drive**: installs fall back to internal storage instead of failing. (@maxjivi05, [#1913](https://github.com/Droid-Deck/DroidDeck/pull/1913)) +- Fixed **GOG**: an install launches the game and not its config tool, cloud saves upload in Galaxy’s format, and a game whose installer cannot run gets its install registry. (@moronigranja, [#1644](https://github.com/Droid-Deck/DroidDeck/pull/1644), [#1952](https://github.com/Droid-Deck/DroidDeck/pull/1952), [#2022](https://github.com/Droid-Deck/DroidDeck/pull/2022)) +- Fixed **Battle.net**: Flathub installs outside a session, World of Warcraft: Forever in the Library, the right artwork for each game, games installed inside its folder, Chinese servers and installs, and launching store games from any game page tab. Its pages no longer play videos on the CPU. (@maxjivi05, @visail, [#1923](https://github.com/Droid-Deck/DroidDeck/pull/1923), [#2018](https://github.com/Droid-Deck/DroidDeck/pull/2018), [#2092](https://github.com/Droid-Deck/DroidDeck/pull/2092), [#2087](https://github.com/Droid-Deck/DroidDeck/pull/2087), [#1660](https://github.com/Droid-Deck/DroidDeck/pull/1660), [#1662](https://github.com/Droid-Deck/DroidDeck/pull/1662), [#2185](https://github.com/Droid-Deck/DroidDeck/pull/2185), [#2195](https://github.com/Droid-Deck/DroidDeck/pull/2195)) +- Fixed **game-specific problems**: Codemasters EGO games, DOOM (2016), Project Zomboid, Supergiant games and Ubisoft Connect. (@The412Banner, @xXJSONDeruloXx, [#1936](https://github.com/Droid-Deck/DroidDeck/pull/1936), [#1987](https://github.com/Droid-Deck/DroidDeck/pull/1987), [#2016](https://github.com/Droid-Deck/DroidDeck/pull/2016), [#2143](https://github.com/Droid-Deck/DroidDeck/pull/2143), [#2090](https://github.com/Droid-Deck/DroidDeck/pull/2090)) +- Fixed the **Linux desktop** disappearing after a runtime update, language packs, a game’s window following its own window mode, and Steam’s own header moving its windows. (@maxjivi05, [#2021](https://github.com/Droid-Deck/DroidDeck/pull/2021), [#2096](https://github.com/Droid-Deck/DroidDeck/pull/2096), [#2094](https://github.com/Droid-Deck/DroidDeck/pull/2094), [#2097](https://github.com/Droid-Deck/DroidDeck/pull/2097)) +- Fixed **repaired components** not reaching prefixes that already had them, and a native-only override breaking a prefix with no native DLL. (@maxjivi05, [#2121](https://github.com/Droid-Deck/DroidDeck/pull/2121), [#2142](https://github.com/Droid-Deck/DroidDeck/pull/2142)) +- Fixed **Windows installs on a natively mounted card** asking for an administrator. (@maxjivi05, [#2100](https://github.com/Droid-Deck/DroidDeck/pull/2100)) +- Fixed the **Home screen setting** so DroidDeck stays in RedMagic Game Space, and the right click acting as Back in RedMagic cast mode. (@maxjivi05, @OmarAlTaki1, [#2083](https://github.com/Droid-Deck/DroidDeck/pull/2083), [#1870](https://github.com/Droid-Deck/DroidDeck/pull/1870)) +- Fixed **Steam’s UI crashing on PowerVR** when scrolling the Home shelf fast. (@coffeemeist3r, [#2107](https://github.com/Droid-Deck/DroidDeck/pull/2107)) +- Fixed **the side menu freezing the launcher** while moving through the Stores tab, and **controller focus** not moving into an opened folder. (@kmnkv1, @danielnoam, [#2105](https://github.com/Droid-Deck/DroidDeck/pull/2105), [#1962](https://github.com/Droid-Deck/DroidDeck/pull/1962)) +- Fixed **VPN and network** problems: links without a gateway, and network queries inside a session. (@uubooster-team, @xXJSONDeruloXx, [#2038](https://github.com/Droid-Deck/DroidDeck/pull/2038), [#2141](https://github.com/Droid-Deck/DroidDeck/pull/2141)) +- Fixed **picture-in-picture** output being cropped after a resize and not scaling to full size, and the **glasses pointer** not reaching the full screen or drawing in the picture. (@xXJSONDeruloXx, @moronigranja, [#2125](https://github.com/Droid-Deck/DroidDeck/pull/2125), [#2145](https://github.com/Droid-Deck/DroidDeck/pull/2145), [#1925](https://github.com/Droid-Deck/DroidDeck/pull/1925), [#2174](https://github.com/Droid-Deck/DroidDeck/pull/2174)) +- Fixed **World of Warcraft and other added games sorting last** in the Library. Battle.net and added games now sort by when they were played and installed, like Steam games. (@maxjivi05, [#2256](https://github.com/Droid-Deck/DroidDeck/pull/2256)) +- Fixed the **Linux runtime showing as not installed after an app update** that brings a new PRoot pack. Only the changed add-on packs download now. (@maxjivi05, [#2270](https://github.com/Droid-Deck/DroidDeck/pull/2270)) +- Fixed **desktop apps that would not start**: RPCS3 and other catalog AppImages from the desktop menu, D-Bus programs such as melonDS, x86 programs that a native script starts, and Flatpak apps that Flatpak no longer has now uninstall cleanly. Add from GitHub installs **the AppImage you linked** instead of the release’s first one. (@maxjivi05, [#2274](https://github.com/Droid-Deck/DroidDeck/pull/2274), [#2275](https://github.com/Droid-Deck/DroidDeck/pull/2275), [#2217](https://github.com/Droid-Deck/DroidDeck/pull/2217), [#2216](https://github.com/Droid-Deck/DroidDeck/pull/2216), [#2273](https://github.com/Droid-Deck/DroidDeck/pull/2273)) +- Fixed **controller navigation** across the Controller tab and profile menus. (@xXJSONDeruloXx, [#2231](https://github.com/Droid-Deck/DroidDeck/pull/2231)) +- Fixed **games listed with a bundled arm64 Linux build** defaulting to the wrong runner. They now start with FEX. (@phj1081, [#2126](https://github.com/Droid-Deck/DroidDeck/pull/2126)) +- Fixed smaller things: left adaptive stick activation at the screen edge, game buffers upscaled before composition, the Steam applying-update stage, apps pinned when all cores are selected, runtime PATH kept for guest binaries, Xwayland orphans, GPU environment values with spaces, the navigation bar staying dark, the Android pointer showing after hovering session menus, touch sticks clicking without a fast double tap, wall scrims leaving edge pixels unshaded, the PowerVR bundled PPSSPP install, L2 and R2 keys overriding analog triggers, taps outside the PC keyboard not reaching the game, direct-touch drags in games that hide the cursor, library scan and download cancel crashes on Android 10, and a Second library on a raw media path or an unmounted volume state. (@sanogueralorenzo, @cphillam, @HCOOKIEie, @w1novnik, @asdf17128, @LingWindT, @Systina12, @KirkPatrickJunsay, @realRurichan, @maxjivi05, @xXJSONDeruloXx, [#1455](https://github.com/Droid-Deck/DroidDeck/pull/1455), [#1367](https://github.com/Droid-Deck/DroidDeck/pull/1367), [#1614](https://github.com/Droid-Deck/DroidDeck/pull/1614), [#1759](https://github.com/Droid-Deck/DroidDeck/pull/1759), [#1826](https://github.com/Droid-Deck/DroidDeck/pull/1826), [#1716](https://github.com/Droid-Deck/DroidDeck/pull/1716), [#1931](https://github.com/Droid-Deck/DroidDeck/pull/1931), [#1975](https://github.com/Droid-Deck/DroidDeck/pull/1975), [#2113](https://github.com/Droid-Deck/DroidDeck/pull/2113), [#2176](https://github.com/Droid-Deck/DroidDeck/pull/2176), [#2173](https://github.com/Droid-Deck/DroidDeck/pull/2173), [#1994](https://github.com/Droid-Deck/DroidDeck/pull/1994), [#2205](https://github.com/Droid-Deck/DroidDeck/pull/2205), [#2257](https://github.com/Droid-Deck/DroidDeck/pull/2257), [#2220](https://github.com/Droid-Deck/DroidDeck/pull/2220), [#2204](https://github.com/Droid-Deck/DroidDeck/pull/2204), [#2224](https://github.com/Droid-Deck/DroidDeck/pull/2224), [#2245](https://github.com/Droid-Deck/DroidDeck/pull/2245), [#2258](https://github.com/Droid-Deck/DroidDeck/pull/2258)) + +### DroidDeck-Proton, DD-Turnip and components + +These ship on their own and update in the app, so 0.7.0 users get them too. + +- **DroidDeck-Proton 11-12** moves to GE-Proton11-12 and Valve’s newer Wine: XInput rescans only when a controller could have changed, DualSense haptics and adaptive triggers together over USB, MSFS 2024 and other intro videos play normally, an ntsync fix stops waiters blocking forever on an abandoned mutex, plus DXVK 3.1.1, VKD3D-Proton 3.1 and newer FEX. (@The412Banner, @maxjivi05, with GloriousEggroll, Valve and the GE contributors upstream) +- **Faster loads in 32-bit and ARM64EC games**: FEX keeps its translated code on empty reads and stays off Wine’s memory lock. Entering the world in Dungeons & Dragons Online went from 50 s to 4 s. (@K11MCH1, [DroidDeck-Proton#9](https://github.com/Droid-Deck/DroidDeck-Proton/pull/9)) +- **32-bit games load at the address they were built for**, so game and mod loaders that patch them at start work again. (@maxjivi05, [DroidDeck-Proton#10](https://github.com/Droid-Deck/DroidDeck-Proton/pull/10)) +- **DiRT 3 and DiRT Showdown** keep running with their telemetry on, for the In-game stats dash. (@The412Banner) +- About 3,000 system functions no longer look **hooked** to x64 games and launchers that check for that, while ARM64EC callers still skip the emulator (11-12.2). Text fields in Wine programs now tell DroidDeck when they have focus, which brings up the keyboard for World of Warcraft chat (11-12.3). (@maxjivi05) +- **DD-Turnip 0.4.0**: newer Mesa, and indirect draws no longer read their arguments too early on Adreno 8xx, which fixes the black or over-shadowed ground in **Mortal Shell 2** and other UE5 games through VKD3D-Proton. (@K11MCH1, [Drivers#14](https://github.com/Droid-Deck/Drivers/pull/14)) +- **Components**: the full Visual C++ 2010 runtime and MFC for 2015–2022 are installed, the DXVK and VKD3D-Proton nightlies are built as ARM64X, and esync packs give Windows users read access to their folders. (@xXJSONDeruloXx, @maxjivi05, [DroidDeck-Components#10](https://github.com/Droid-Deck/DroidDeck-Components/pull/10), [#11](https://github.com/Droid-Deck/DroidDeck-Components/pull/11), [#12](https://github.com/Droid-Deck/DroidDeck-Components/pull/12), [#13](https://github.com/Droid-Deck/DroidDeck-Components/pull/13)) + +### Community + +DroidDeck is built in the open by the people who use it. These are everyone whose code is in 0.7.0: + +| Contributor | Pull requests | +|---|---| +| @xXJSONDeruloXx | 65: KDE desktop on the bottom screen, more Steam libraries, HUD Strip and docking, live wallpapers, startup choice, diagnostics, PowerVR add-on pack and more | +| @maxjivi05 | 70: hardware mouse and keyboard, single-screen mode, Battle.net, desktop fixes, performance and more | +| @The412Banner | 9: bottom-screen Dashboard, swap screens, bottom-screen HUD, GPU load without root, game fixes | +| @K11MCH1 | 6: EA app, installed Windows programs, scan and frame limit fixes | +| @moronigranja | 4: GOG saves and installs, glasses cursor | +| @ASEbrahim, @mr-abccc, @The412Banner | MaliBridge for Mali GPUs ([#2027](https://github.com/Droid-Deck/DroidDeck/pull/2027), [#1950](https://github.com/Droid-Deck/DroidDeck/pull/1950)) | +| @wallentx, @Leask, @stefanz92, @DavidCimo | Non-Adreno graphics stack and diagnostics ([#2029](https://github.com/Droid-Deck/DroidDeck/pull/2029), [#2129](https://github.com/Droid-Deck/DroidDeck/pull/2129)) | +| @angelrenefm | 4: session clocks, GPU clock in Steam’s overlay, `droiddeckctl` | +| @FX192 | 2: Game Jolt store | +| @visail, @ddd-ms, @kmnkv1 | 2 each: Battle.net China, Steam speedups, Stores freeze and build fix | +| @superboo07, @vitamin663-sys, @Dantalian-dev, @KeegantheVeganat0r, @l4nr3, @phj1081, @danielnoam, @cphillam, @asdf17128, @Mi-Yomi, @LingWindT, @KirkPatrickJunsay, @0x1428571429, @OmarAlTaki1, @sanogueralorenzo, @HCOOKIEie, @w1novnik, @Systina12, @coffeemeist3r, @realRurichan, @flexeykinDev, @uubooster-team, @Delete228 | Syncthing, stick dead zone, PC keyboard settings, unified controller profiles, HUD lows, zram resume, FEX default, Stores freeze, folder focus, upscaling, PATH, Quest, Xwayland, iisu, VPN, PowerVR crash, translations and more | + +### New contributors + +- @cphillam made their first contribution in [#1367](https://github.com/Droid-Deck/DroidDeck/pull/1367). +- @sanogueralorenzo made their first contribution in [#1455](https://github.com/Droid-Deck/DroidDeck/pull/1455). +- @HCOOKIEie made their first contribution in [#1614](https://github.com/Droid-Deck/DroidDeck/pull/1614). +- @visail made their first contribution in [#1660](https://github.com/Droid-Deck/DroidDeck/pull/1660). +- @LingWindT made their first contribution in [#1716](https://github.com/Droid-Deck/DroidDeck/pull/1716). +- @w1novnik made their first contribution in [#1759](https://github.com/Droid-Deck/DroidDeck/pull/1759). +- @vitamin663-sys made their first contribution in [#1780](https://github.com/Droid-Deck/DroidDeck/pull/1780). +- @OmarAlTaki1 made their first contribution in [#1870](https://github.com/Droid-Deck/DroidDeck/pull/1870). +- @Systina12 made their first contribution in [#1931](https://github.com/Droid-Deck/DroidDeck/pull/1931). +- @Mi-Yomi made their first contribution in [#1938](https://github.com/Droid-Deck/DroidDeck/pull/1938). +- @mr-abccc made their first contribution in [#1950](https://github.com/Droid-Deck/DroidDeck/pull/1950). +- @danielnoam made their first contribution in [#1962](https://github.com/Droid-Deck/DroidDeck/pull/1962). +- @DavidCimo made their first contribution in [#2029](https://github.com/Droid-Deck/DroidDeck/pull/2029). +- @Leask made their first contribution in [#2029](https://github.com/Droid-Deck/DroidDeck/pull/2029). +- @stefanz92 made their first contribution in [#2029](https://github.com/Droid-Deck/DroidDeck/pull/2029). +- @uubooster-team made their first contribution in [#2038](https://github.com/Droid-Deck/DroidDeck/pull/2038). +- @realRurichan made their first contribution in [#2113](https://github.com/Droid-Deck/DroidDeck/pull/2113). +- @flexeykinDev made their first contribution in [#2116](https://github.com/Droid-Deck/DroidDeck/pull/2116). +- @phj1081 made their first contribution in [#2126](https://github.com/Droid-Deck/DroidDeck/pull/2126). +- @KeegantheVeganat0r made their first contribution in [#2127](https://github.com/Droid-Deck/DroidDeck/pull/2127). + +[Full changelog](https://github.com/Droid-Deck/DroidDeck/compare/0.6.9...0.7.0) diff --git a/docs/releases/0.7.1.md b/docs/releases/0.7.1.md new file mode 100644 index 0000000000..5633edbb89 --- /dev/null +++ b/docs/releases/0.7.1.md @@ -0,0 +1,55 @@ +# DroidDeck 0.7.1 + +

+ Download DroidDeck 0.7.1 + Join the DroidDeck Discord +

+ +**DroidDeck is 100% open source. No ads. No telemetry. No proprietary components. No source withheld.** + +0.7.1 is a fix release for 0.7.0. Since 0.7.0, **4 people** sent **33 pull requests**, and **2 of them are first-time contributors**. It fixes Steam launches that hung at "Processing install script", the endless loading screen after quitting a game and colour on PowerVR, plus GOG DLC and a new launcher theme. + +### Features + +- Added the **Scarlet snow** launcher theme, DroidDeck’s first light theme. (@EnzoDLofi, [#2262](https://github.com/Droid-Deck/DroidDeck/pull/2262)) +- Added **left and right picture alignment** next to top and bottom. The controller pointer and direct touch follow the moved picture, also with glasses. (@EnzoDLofi, [#2384](https://github.com/Droid-Deck/DroidDeck/pull/2384)) +- **GOG installs the DLC you own**, and the GOG search reaches GOG’s whole catalog. (@maxjivi05, [#2348](https://github.com/Droid-Deck/DroidDeck/pull/2348), [#2296](https://github.com/Droid-Deck/DroidDeck/pull/2296)) + +### Fixed + +- Fixed **Steam games stuck at “Processing install script”** or a download that never finishes, and the black screen with “Abort game” after quitting, which started in 0.7.0. (@xXJSONDeruloXx, [#2303](https://github.com/Droid-Deck/DroidDeck/pull/2303)) +- Fixed **a game launched from DroidDeck stalling at Steam’s launch screen**: DroidDeck now waits for Steam to be ready. (@maxjivi05, [#2362](https://github.com/Droid-Deck/DroidDeck/pull/2362)) +- Fixed **an endless loading screen** after a game exits back to Steam. (@maxjivi05, [#2359](https://github.com/Droid-Deck/DroidDeck/pull/2359)) +- The bottom-screen Dashboard’s **Notifications page is removed**, so DroidDeck no longer asks to read your notifications. (@xXJSONDeruloXx, [#2342](https://github.com/Droid-Deck/DroidDeck/pull/2342)) +- Fixed **grayscale Steam and games on PowerVR** (Pixel 10 Pro and Pro XL) since the Ubuntu 26.04 runtime. (@maxjivi05, [#2287](https://github.com/Droid-Deck/DroidDeck/pull/2287)) +- Fixed **games that need EasyAntiCheat** never starting: Linux-only tools such as the Proton EasyAntiCheat Runtime keep their native platform. (@maxjivi05, [#2313](https://github.com/Droid-Deck/DroidDeck/pull/2313)) +- Fixed **a Proton picked in Steam**, such as Proton Experimental, being switched back to DroidDeck-Proton, and **store apps launched before Steam opens** reporting that no Proton is installed. (@maxjivi05, [#2284](https://github.com/Droid-Deck/DroidDeck/pull/2284), [#2392](https://github.com/Droid-Deck/DroidDeck/pull/2392)) +- Fixed **games failing with `c000007b`** after an installer put an x64 system DLL into an ARM64 prefix. (@maxjivi05, [#2318](https://github.com/Droid-Deck/DroidDeck/pull/2318)) +- Fixed **emulators and other AppImage, Flatpak and Snap programs** rendering through system memory on Adreno. They get the game GPU setting, and Flatpak apps open their window instead of playing audio only. (@maxjivi05, [#2288](https://github.com/Droid-Deck/DroidDeck/pull/2288), [#2295](https://github.com/Droid-Deck/DroidDeck/pull/2295), [#2391](https://github.com/Droid-Deck/DroidDeck/pull/2391)) +- Fixed **Linux emulators that run guest code natively** hanging on launch. (@maxjivi05, [#2279](https://github.com/Droid-Deck/DroidDeck/pull/2279)) +- Fixed **Firefox tabs crashing** and the **desktop browser hanging** in the Desktop session. (@xXJSONDeruloXx, @maxjivi05, [#2360](https://github.com/Droid-Deck/DroidDeck/pull/2360), [#2304](https://github.com/Droid-Deck/DroidDeck/pull/2304)) +- Fixed **taps on pop-up dialogs** and windows away from the screen’s top-left corner doing nothing. (@maxjivi05, [#2280](https://github.com/Droid-Deck/DroidDeck/pull/2280), [#2285](https://github.com/Droid-Deck/DroidDeck/pull/2285)) +- Fixed **direct touch with Relative mouse: Auto** turning taps into small mouse movements when no mouse is connected. (@maxjivi05, [#2305](https://github.com/Droid-Deck/DroidDeck/pull/2305)) +- Fixed **the Controls below the game tabs** being cut off, which left the panel stuck on the trackpads. (@maxjivi05, [#2302](https://github.com/Droid-Deck/DroidDeck/pull/2302)) +- Fixed **GOG**: owned games missing from the library since 0.6.0 are back, and install scripts’ support files reach the prefix, so modded editions start as installed. (@maxjivi05, [#2296](https://github.com/Droid-Deck/DroidDeck/pull/2296), [#2294](https://github.com/Droid-Deck/DroidDeck/pull/2294)) +- Fixed **stutter on devices with 13 GB of RAM or less**: game memory the game reads straight back is no longer compressed again every 30 s. (@maxjivi05, [#2312](https://github.com/Droid-Deck/DroidDeck/pull/2312)) +- Fixed **the frame graph jumping around** on the bottom-screen Dashboard’s In-game stats. (@xXJSONDeruloXx, [#2345](https://github.com/Droid-Deck/DroidDeck/pull/2345)) +- Fixed smaller things: programs whose allocator calls a hooked function at startup no longer abort with the input preload, `droiddeckctl` works on shells without the `content` command such as the Retroid Pocket Duo’s, a full disk no longer crashes the app on an agent call, and ending a session without the graphics runtime pack no longer logs an error. (@xXJSONDeruloXx, @maxjivi05, [#2361](https://github.com/Droid-Deck/DroidDeck/pull/2361), [#2321](https://github.com/Droid-Deck/DroidDeck/pull/2321), [#2325](https://github.com/Droid-Deck/DroidDeck/pull/2325), [#2316](https://github.com/Droid-Deck/DroidDeck/pull/2316)) + +### Community + +These are everyone whose code is in 0.7.1: + +| Contributor | Pull requests | +|---|---| +| @maxjivi05 | 22: Steam launch readiness, PowerVR colour, GOG DLC and library, AppImage and Flatpak GPU setting, dialog taps, EasyAntiCheat and more | +| @xXJSONDeruloXx | 8: Steam session bus, notification listener removal, Firefox in the desktop, `droiddeckctl` and more | +| @EnzoDLofi | 2: Scarlet snow theme, left and right picture alignment | +| @GiovanniCarbinatti | 1: README GPU support | + +### New contributors + +- @EnzoDLofi made their first contribution in [#2262](https://github.com/Droid-Deck/DroidDeck/pull/2262). +- @GiovanniCarbinatti made their first contribution in [#2311](https://github.com/Droid-Deck/DroidDeck/pull/2311). + +[Full changelog](https://github.com/Droid-Deck/DroidDeck/compare/0.7.0...0.7.1) diff --git a/docs/releases/media/0.7.0/controller-profiles.jpg b/docs/releases/media/0.7.0/controller-profiles.jpg new file mode 100644 index 0000000000..0bb15df239 Binary files /dev/null and b/docs/releases/media/0.7.0/controller-profiles.jpg differ diff --git a/docs/releases/media/0.7.0/dashboard.jpg b/docs/releases/media/0.7.0/dashboard.jpg new file mode 100644 index 0000000000..54cf4c35e9 Binary files /dev/null and b/docs/releases/media/0.7.0/dashboard.jpg differ diff --git a/docs/releases/media/0.7.0/game-jolt.jpg b/docs/releases/media/0.7.0/game-jolt.jpg new file mode 100644 index 0000000000..7671962153 Binary files /dev/null and b/docs/releases/media/0.7.0/game-jolt.jpg differ diff --git a/docs/releases/media/0.7.0/hud-dock.gif b/docs/releases/media/0.7.0/hud-dock.gif new file mode 100644 index 0000000000..f544c65a53 Binary files /dev/null and b/docs/releases/media/0.7.0/hud-dock.gif differ diff --git a/docs/releases/media/0.7.0/hud-strip.jpg b/docs/releases/media/0.7.0/hud-strip.jpg new file mode 100644 index 0000000000..d203338441 Binary files /dev/null and b/docs/releases/media/0.7.0/hud-strip.jpg differ diff --git a/docs/releases/media/0.7.0/kde-desktop.gif b/docs/releases/media/0.7.0/kde-desktop.gif new file mode 100644 index 0000000000..fbd3543288 Binary files /dev/null and b/docs/releases/media/0.7.0/kde-desktop.gif differ diff --git a/docs/releases/media/0.7.0/mali-support.jpg b/docs/releases/media/0.7.0/mali-support.jpg new file mode 100644 index 0000000000..9f7de5d2cb Binary files /dev/null and b/docs/releases/media/0.7.0/mali-support.jpg differ diff --git a/docs/releases/media/0.7.0/steam-libraries.jpg b/docs/releases/media/0.7.0/steam-libraries.jpg new file mode 100644 index 0000000000..9fd2964757 Binary files /dev/null and b/docs/releases/media/0.7.0/steam-libraries.jpg differ diff --git a/docs/releases/media/0.7.0/swap-game-bottom.jpg b/docs/releases/media/0.7.0/swap-game-bottom.jpg new file mode 100644 index 0000000000..79560e02da Binary files /dev/null and b/docs/releases/media/0.7.0/swap-game-bottom.jpg differ diff --git a/docs/releases/media/0.7.0/syncthing.jpg b/docs/releases/media/0.7.0/syncthing.jpg new file mode 100644 index 0000000000..37de627ddc Binary files /dev/null and b/docs/releases/media/0.7.0/syncthing.jpg differ diff --git a/docs/testing/hud-strip-hybrid-2026-11-07.json b/docs/testing/hud-strip-hybrid-2026-11-07.json new file mode 100644 index 0000000000..b3c978d7d1 --- /dev/null +++ b/docs/testing/hud-strip-hybrid-2026-11-07.json @@ -0,0 +1,110 @@ +{ + "cpuUnits": "100 percent equals one occupied CPU core", + "sampleSeconds": 25, + "warmupSeconds": 5, + "navigationIntervalSeconds": 0.125, + "measurements": [ + { + "run": 1, + "mode": "software15", + "cpuPercentOneCore": 31.663757784918452, + "mainCpuPercentOneCore": 7.044499908393501, + "drawP95Ms": 2.094739, + "frameP95Ms": 5.356661, + "gpuP95Ms": 1.383803, + "navigationInputs": 244, + "layerType": 1, + "canvasHardware": false, + "windowFrames": 401, + "focusLostSamples": 0, + "droppedMetricReports": 0, + "thermalStart": 0, + "thermalEnd": 0 + }, + { + "run": 2, + "mode": "hardware15", + "cpuPercentOneCore": 31.57198290945421, + "mainCpuPercentOneCore": 6.232491898227486, + "drawP95Ms": 1.394844, + "frameP95Ms": 4.569843, + "gpuP95Ms": 1.37625, + "navigationInputs": 244, + "layerType": 0, + "canvasHardware": true, + "windowFrames": 397, + "focusLostSamples": 0, + "droppedMetricReports": 0, + "thermalStart": 0, + "thermalEnd": 0 + }, + { + "run": 3, + "mode": "hardware15", + "cpuPercentOneCore": 31.61184616790523, + "mainCpuPercentOneCore": 6.104517009710966, + "drawP95Ms": 1.327395, + "frameP95Ms": 4.548805, + "gpuP95Ms": 1.360052, + "navigationInputs": 244, + "layerType": 0, + "canvasHardware": true, + "windowFrames": 395, + "focusLostSamples": 0, + "droppedMetricReports": 0, + "thermalStart": 0, + "thermalEnd": 0 + }, + { + "run": 4, + "mode": "software15", + "cpuPercentOneCore": 33.22786890143692, + "mainCpuPercentOneCore": 6.873449737216468, + "drawP95Ms": 2.024947, + "frameP95Ms": 5.430132, + "gpuP95Ms": 1.425989, + "navigationInputs": 244, + "layerType": 1, + "canvasHardware": false, + "windowFrames": 397, + "focusLostSamples": 0, + "droppedMetricReports": 0, + "thermalStart": 0, + "thermalEnd": 0 + }, + { + "run": 5, + "mode": "software15", + "cpuPercentOneCore": 33.472465000822694, + "mainCpuPercentOneCore": 6.952114615054318, + "drawP95Ms": 2.045417, + "frameP95Ms": 5.403243, + "gpuP95Ms": 1.356354, + "navigationInputs": 244, + "layerType": 1, + "canvasHardware": false, + "windowFrames": 402, + "focusLostSamples": 0, + "droppedMetricReports": 0, + "thermalStart": 0, + "thermalEnd": 0 + }, + { + "run": 6, + "mode": "hardware15", + "cpuPercentOneCore": 29.40371307271493, + "mainCpuPercentOneCore": 5.991546211883758, + "drawP95Ms": 1.347709, + "frameP95Ms": 4.509034, + "gpuP95Ms": 1.302135, + "navigationInputs": 244, + "layerType": 0, + "canvasHardware": true, + "windowFrames": 401, + "focusLostSamples": 0, + "droppedMetricReports": 0, + "thermalStart": 0, + "thermalEnd": 0 + } + ] +} diff --git a/docs/testing/hud-strip-hybrid-2026-11-07.md b/docs/testing/hud-strip-hybrid-2026-11-07.md new file mode 100644 index 0000000000..b5b8646da1 --- /dev/null +++ b/docs/testing/hud-strip-hybrid-2026-11-07.md @@ -0,0 +1,85 @@ +# Strip hybrid rendering versus PR2194 + +PR2194 avoids the observed Strip flicker by rendering the entire Strip in a +software View layer. This follow-up keeps the View hardware accelerated and +rasterizes only its graph and logo into small bitmaps. The graph bitmap is +reused and cleared before each trace; the logo bitmap updates when its size +or colours change. Text and the rest of the HUD use the normal hardware path. +The 66 ms graph invalidation cap from PR2194 is retained. + +This reduces the amount of HUD drawing that needs a software layer while +retaining bitmap rendering for the vector content used by the previously +working hybrid approach. It preserves Strip's layout and settings. + +## Device comparison + +On 2026-11-07, six samples were captured on a HONOR BKQ-N49 with Adreno 840 +and Android 17. Both renderers were selected in the same comparison APK, +based on upstream `e5434117f`, using the same cached published-preview runtime +components. The later native presentation and producer-wait experiments were +excluded. The local build reused those components with source-provenance +warnings; this was a matched HUD comparison, not validation of newly built +runtime components from upstream main. + +The debug harness selected either `LAYER_TYPE_SOFTWARE` with direct graph/logo +Paths, matching PR2194, or `LAYER_TYPE_NONE` with the hybrid bitmap graph/logo. +The software baseline therefore did not pay for the hybrid bitmap rendering +as well as its software View layer. + +Each sample had a five-second warm-up and a 25-second measurement. Real +DPAD Right/Left inputs alternated at a target interval of 0.125 seconds, +keeping Steam navigation active. Each run delivered 244 inputs over its +30.5-second navigation window, including warm-up. The order was software, +hybrid, hybrid, software, software, hybrid. The same full-width Strip layout +and settings were used throughout. Display settings were temporarily held at +60 Hz and the original adaptive settings were restored and read back afterward. +No screen recording ran during measurement. + +Values below are means of the three per-run measurements. CPU percentages +use 100% for one occupied CPU core. Draw and frame values are means of the +individual runs' p95 values, not pooled percentiles. + +| Metric | PR2194 software | Hybrid | Relative change | +| --- | ---: | ---: | ---: | +| UI-thread CPU | 6.957% | 6.110% | -12.2% | +| App-process CPU | 32.788% | 30.863% | -5.9% | +| App-window draw p95 | 2.055 ms | 1.357 ms | -34.0% | +| App-window frame p95 | 5.397 ms | 4.543 ms | -15.8% | +| Reported GPU p95 | 1.389 ms | 1.346 ms | -3.1% | + +| Run | Renderer | UI-thread CPU | App-process CPU | Draw p95 | Frame p95 | +| --- | --- | ---: | ---: | ---: | ---: | +| 1 | Software | 7.044% | 31.664% | 2.095 ms | 5.357 ms | +| 2 | Hybrid | 6.232% | 31.572% | 1.395 ms | 4.570 ms | +| 3 | Hybrid | 6.105% | 31.612% | 1.327 ms | 4.549 ms | +| 4 | Software | 6.873% | 33.228% | 2.025 ms | 5.430 ms | +| 5 | Software | 6.952% | 33.472% | 2.045 ms | 5.403 ms | +| 6 | Hybrid | 5.992% | 29.404% | 1.348 ms | 4.509 ms | + +All three pairs favoured hybrid rendering for UI-thread CPU and draw p95. +All runs retained Steam focus, reported thermal status 0, and lost no frame +metric reports. Layer type and Canvas hardware acceleration were checked +for each mode. The per-run measurements are in +[the accompanying JSON](hud-strip-hybrid-2026-11-07.json). + +## Validation and limits + +The 12 HUD regression checks passed, including graph trace clearing, scaling, +and a new check that a cached Strip logo updates its colours after a theme +change without resizing. The comparison APK built successfully. + +An earlier saved APK using this hybrid approach was physically confirmed +flicker-free by the tester. Physical confirmation of the rebuilt comparison +APK is separate from the automated measurements and remains pending. +Frame metrics cannot establish absence of visible corruption. + +These short samples on one device show a reduction in measured HUD rendering +cost. App-process CPU excludes other guest processes and is not whole-device +CPU usage. Sustained game performance, battery life, and temperature savings +were not established. This change targets Strip; it does not address the +separately observed flicker while moving a docked FPS tab or shrinking other +HUD modes. + +Comparison APK SHA256: +`8df2d75216c40f24353a22a7c830341cb62a5ce6423ad2207477a0d769e22ae2`. +Temporary benchmark controls were excluded from the proposed production patch. diff --git a/tools/agent/android-open b/tools/agent/android-open new file mode 100755 index 0000000000..9d8c550486 --- /dev/null +++ b/tools/agent/android-open @@ -0,0 +1,9 @@ +#!/bin/sh +# Opens a URL in Android's browser on the bottom screen. +url=$1 +[ -n "$url" ] || exit 1 +serial=${ADB_SERIAL:-127.0.0.1:5555} +adb connect "$serial" >/dev/null 2>&1 +quoted=$(printf "%s" "$url" | sed "s/'/'\\\\''/g") +adb -s "$serial" shell "am start --display 2 -a android.intent.action.VIEW -d '$quoted'" >/dev/null 2>&1 || + echo "Open this on any device: $url" >&2 diff --git a/tools/agent/dd-agent b/tools/agent/dd-agent new file mode 100755 index 0000000000..9a1b4d4132 --- /dev/null +++ b/tools/agent/dd-agent @@ -0,0 +1,5 @@ +#!/bin/sh +# Claude Code in the DroidDeck checkout, with sign-in links opened in Android's browser. +export ADB_SERIAL=${ADB_SERIAL:-127.0.0.1:5555} PATH="$HOME/.local/bin:$PATH" BROWSER="$HOME/.local/bin/android-open" +adb connect "$ADB_SERIAL" >/dev/null 2>&1 +cd "$HOME/DroidDeck" && exec claude "$@" diff --git a/tools/agent/dd-gh-login b/tools/agent/dd-gh-login new file mode 100755 index 0000000000..adf816a247 --- /dev/null +++ b/tools/agent/dd-gh-login @@ -0,0 +1,24 @@ +#!/bin/bash +# GitHub sign-in through Android's browser on the bottom screen, with the one-time code typed in. +serial=${ADB_SERIAL:-127.0.0.1:5555} +adb connect "$serial" >/dev/null 2>&1 +coproc GH { BROWSER=true gh auth login --hostname github.com --git-protocol https --web 2>&1; } +gh_pid=$GH_PID +typed= +while IFS= read -r line <&"${GH[0]}"; do + printf '%s\n' "$line" + code=$(grep -oE '[A-Z0-9]{4}-[A-Z0-9]{4}' <<<"$line" | head -1) + if [ -n "$code" ] && [ -z "$typed" ]; then + typed=1 + echo >&"${GH[1]}" + adb -s "$serial" shell "am start --display 2 -a android.intent.action.VIEW -d https://github.com/login/device" >/dev/null 2>&1 + # Chrome drops characters from `input text` and from fast key events on this page. + keys= + for ((i = 0; i < ${#code}; i++)); do + c=${code:i:1} + [ "$c" = - ] || keys+="input -d 2 keyevent KEYCODE_$c; sleep 0.4; " + done + (sleep 7; adb -s "$serial" shell "$keys" >/dev/null 2>&1) & + fi +done +wait "$gh_pid" diff --git a/tools/agent/dd-lab-build b/tools/agent/dd-lab-build new file mode 100755 index 0000000000..44b62bd6b8 --- /dev/null +++ b/tools/agent/dd-lab-build @@ -0,0 +1,226 @@ +#!/bin/bash +# Builds this checkout as a Lab build of the installed DroidDeck and restarts the app on it. +# Usage: dd-lab-build [--no-restart | --prepare] [--explain] +# --prepare compiles the installed commit without making a Lab build, so later builds only +# compile what changed. Setup runs it once; it takes the longest. +# --explain logs why each task ran and Kotlin's build report, to find out what makes a build slow. +set -euo pipefail +pkg=com.droiddeck.launcher +serial=${ADB_SERIAL:-127.0.0.1:5555} +mode=build +explain=() +for arg in "$@"; do + case $arg in + --no-restart) mode=no-restart ;; + --prepare) mode=prepare ;; + --explain) explain=(--info -Pkotlin.build.report.output=file) ;; + esac +done +started=$(date +%s) +cache=$HOME/.cache +progress_file=$cache/dd-lab-build.json +times_file=$cache/dd-lab-build-times +mkdir -p "$cache" +# The Agent tab reads this file to show how far the build has got and how long is left. +progress() { # stage [permille task done total eta] + local task=${3:-}; task=${task//\\/}; task=${task//\"/\'}; task=${task//$'\n'/ } + printf '{"stage":"%s","mode":"%s","permille":%d,"task":"%s","done":%d,"total":%d,"eta":%d,"elapsed":%d,"pid":%d}\n' \ + "$1" "$mode" "${2:-0}" "$task" "${4:-0}" "${5:-0}" "${6:--1}" $(( $(date +%s) - started )) $$ > "$progress_file.part.$BASHPID" + mv "$progress_file.part.$BASHPID" "$progress_file" +} +fail() { progress failed 0 "$*"; echo "dd-lab-build: $*" >&2; exit 1; } +trap 'progress failed 0 stopped; exit 143' INT TERM +trap 'progress failed 0 "a command failed on line $LINENO"' ERR +device() { adb -s "$serial" shell "$@" | tr -d '\r'; } + +# The app's agent loses every process it started when its turn ends, a build included, so it asks +# the app to run the build in a process of its own and follows that build here. +agent=/data/user/0/$pkg/files/agent +if [ -n "${DD_AGENT:-}" ] && [ -z "${DD_LAB_DETACHED:-}" ] && [ -d "$agent" ]; then + trap - ERR + log=$cache/dd-lab-build.log + rm -f "$progress_file" + python3 -c 'import json,os,sys; print(json.dumps({"cwd": os.getcwd(), "args": sys.argv[1:]}))' "$@" > "$agent/lab-request.part" + mv "$agent/lab-request.part" "$agent/lab-request" + echo "DroidDeck runs this build itself, so it keeps going if this turn ends. Its log follows." + for _ in $(seq 1 30); do [ -f "$progress_file" ] && break; sleep 1; done + [ -f "$progress_file" ] || { echo "dd-lab-build: DroidDeck did not start the build; is it running?" >&2; exit 1; } + tail -n +1 -F "$log" 2>/dev/null & follow=$! + result=1 + while sleep 2; do + stage=$(sed -n 's/.*"stage":"\([a-z]*\)".*/\1/p' "$progress_file") + pid=$(sed -n 's/.*"pid":\([0-9]*\).*/\1/p' "$progress_file") + case $stage in + ready|prepared) result=0; break ;; + failed) break ;; + esac + [ -n "$pid" ] && [ -d "/proc/$pid" ] || break + done + sleep 1; kill $follow 2>/dev/null || true + exit $result +fi + +progress checking +repo=$(git rev-parse --show-toplevel) || fail "run it inside the DroidDeck checkout" +cd "$repo" +adb connect "$serial" >/dev/null 2>&1 || true + +state=$(python3 tools/droiddeckctl state 2>/dev/null | sed -n '/^{/,$p') || fail "could not read DroidDeck's state; is it running and is adb connected?" +# A Lab build reports its own label, so the APK's commit comes from apkCommit when the app has it. +label=$(python3 -c 'import json,sys; s=json.load(sys.stdin); print(s.get("apkCommit") or s["build"].rsplit(" at ", 1)[-1])' <<<"$state") || + fail "DroidDeck's state did not say which build is installed" +installed_label=$(python3 -c 'import json,re,sys; print(re.sub(r"^(?:\w+ \w+ · |lab on )+", "", json.load(sys.stdin)["build"]))' <<<"$state") +if ! base=$(git rev-parse --verify --quiet "$label^{commit}"); then + progress fetching 0 "the installed commit" + git fetch --quiet --tags origin >/dev/null 2>&1 || true + git fetch --quiet origin main >/dev/null 2>&1 || true + base=$(git rev-parse --verify --quiet "$label^{commit}") || fail "the installed build ($label) is not in this checkout or on GitHub's main; fetch it first" +fi + +if [ $mode = prepare ]; then + # Compiling the installed commit itself gives later builds of branches on it incremental caches. + if ! git merge-base --is-ancestor "$base" HEAD; then + git diff --quiet && git diff --cached --quiet || fail "HEAD does not build on the installed commit and has uncommitted changes" + was=$(git branch --show-current) + git checkout --quiet --detach "$base" + echo "Checked out the installed commit ${base:0:9} to prepare it${was:+; $was is unchanged}" + fi +else + git diff --quiet && git diff --cached --quiet || fail "commit your changes first" + git merge-base --is-ancestor "$base" HEAD || fail "HEAD does not build on the installed commit ${base:0:9}; branch from it" + blocked=$(git diff --name-only "$base" HEAD | grep -E 'AndroidManifest\.xml$|/cpp/|/jniLibs/|\.rs$|(^|/)build\.gradle$|/App\.kt$|/AppLanguage\.kt$|/lab/LabOverlay\.kt$' || true) + [ -z "$blocked" ] || fail "these need a new APK, not a Lab build: $(echo $blocked)" +fi +apk=$(device pm path $pkg | sed -n 's/^package://p' | grep 'base\.apk$' | head -1) +[ -n "$apk" ] || fail "DroidDeck is not installed" + +export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-arm64 ANDROID_HOME=$HOME/android-sdk +# BuildConfig describes the installed base, not each Lab commit; its commit changing would make the +# Kotlin compile redo the whole app (the overlay's own label names the Lab build). Updates reads the +# installed variant (non-adreno) from the build label. +export DROIDDECK_SOURCE_COMMIT=$base DROIDDECK_BUILD_LABEL="lab on $installed_label" DROIDDECK_BUILD_TREE_STATE=clean GITHUB_HEAD_REF=lab +printf 'sdk.dir=%s\n' "$ANDROID_HOME" > local.properties +if [ ! -d "$ANDROID_HOME/platforms/android-34" ]; then + progress sdk + yes | "$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager" --licenses >/dev/null + "$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager" "platforms;android-34" "build-tools;35.0.0" >/dev/null +fi + +# A full Kotlin compile of the app needs about 2.5 GB of heap and Gradle about 1 GB; smaller devices get +# what they can spare. Both daemons end with this script's process when the build is done. +total_mb=$(awk '/^MemTotal:/ {print int($2 / 1024)}' /proc/meminfo) +heap=$(( total_mb / 2 )); [ $heap -gt 4096 ] && heap=4096; [ $heap -lt 2048 ] && heap=2048 +heap=${DD_LAB_HEAP_MB:-$heap} +kotlin_heap=$(( heap * 2 / 3 )); gradle_heap=$(( heap - kotlin_heap )) + +# Only the Kotlin daemon compiles incrementally; in-process compiles always redo the whole app, and the +# caches they leave behind make the daemon's first incremental compile fail. +if [ "$(cat "$cache/dd-lab-build-strategy" 2>/dev/null)" != daemon ]; then + rm -rf app/build/kotlin/compileReleaseKotlin + echo daemon > "$cache/dd-lab-build-strategy" +fi + +# Google publishes aapt2 for x86-64 Linux only; setup wraps it in qemu-user at ~/aapt2/aapt2. +gradle=(./gradlew :app:processReleaseResources :app:mergeDexRelease --max-workers=2 -PskipRust=true -x buildVkBridgeServer + -Pandroid.aapt2FromMavenOverride=$HOME/aapt2/aapt2 -Pkotlin.compiler.execution.strategy=daemon + -Pkotlin.daemon.jvmargs=-Xmx${kotlin_heap}m -Dorg.gradle.jvmargs=-Xmx${gradle_heap}m --console=plain) +progress planning +mapfile -t plan < <("${gradle[@]}" --dry-run -q 2>/dev/null | awk '/^:/ {print $1}' || true) +total=${#plan[@]} + +# Each task's estimate is how long it took last time; a full compile of the app takes about 15 minutes. +declare -A estimate +[ -f "$times_file" ] && while read -r t s; do estimate[$t]=$s; done < "$times_file" +estimate_of() { local e=${estimate[$1]:-}; [ -n "$e" ] && echo "$e" || { [[ $1 == *compileReleaseKotlin ]] && echo 900 || echo 2; }; } +planned=0 +for t in "${plan[@]}"; do planned=$(( planned + $(estimate_of "$t") )); done +[ $planned -gt 0 ] || planned=1 + +current=$(mktemp) +measured=$(mktemp) +# " " +echo "0 starting $(date +%s) 0 0" > "$current" +report() { + local n task since done_s cur_est now part permille left + read -r n task since done_s cur_est < "$current" + now=$(date +%s) + part=$(( now - since )); [ $part -gt $(( cur_est * 95 / 100 )) ] && part=$(( cur_est * 95 / 100 )) + permille=$(( (done_s + part) * 1000 / planned )); [ $permille -gt 990 ] && permille=990 + left=$(( planned - done_s - part )); [ $left -lt 0 ] && left=0 + progress building "$permille" "$task" "$n" "$total" "$left" + echo "$n $task $(( (now - started) / 60 )) $left" +} +( + beat=0 + while sleep 5; do + set -- $(report) + beat=$(( beat + 1 )) + [ $(( beat % 12 )) = 0 ] && echo "[build $1/$total] still on $2, $3m in, about $(( $4 / 60 + 1 ))m left" + done +) & +heartbeat=$! +trap 'kill $heartbeat 2>/dev/null || true; rm -f "$current" "$measured"' EXIT +set +o pipefail +"${gradle[@]}" ${explain[@]+"${explain[@]}"} 2>&1 | { + n=0 done_s=0 task="" since=0 cur_est=0 ran=0 + while IFS= read -r line; do + printf '%s\n' "$line" + case $line in + "> Task "*) + now=$(date +%s) + # A task's line comes when it starts, or when it ends for one that did no work. + if [ -n "$task" ]; then + done_s=$(( done_s + cur_est )) + [ $ran = 1 ] && echo "$task $(( now - since ))" >> "$measured" + fi + rest=${line#> Task }; task=${rest%% *} + case $rest in *" "*) ran=0 ;; *) ran=1 ;; esac + n=$((n + 1)); since=$now; cur_est=$(estimate_of "$task") + echo "$n $task $since $done_s $cur_est" > "$current" + [ $ran = 1 ] && report >/dev/null && echo "[build $n/$total] $task" ;; + esac + done + if [ -n "$task" ] && [ $ran = 1 ]; then echo "$task $(( $(date +%s) - since ))" >> "$measured"; fi +} +status=${PIPESTATUS[0]} +set -o pipefail +kill $heartbeat 2>/dev/null || true +[ "$status" = 0 ] || fail "Gradle failed (exit $status); the reason is above" +# Tasks that did no work this time keep their earlier time. +{ [ -f "$times_file" ] && cat "$times_file"; cat "$measured"; } | awk '{t[$1] = $2} END {for (k in t) print k, t[k]}' > "$times_file.part" +mv "$times_file.part" "$times_file" +# The app reads which installed commit the build caches are warm for. +echo "$base" > "$cache/dd-lab-build-base" + +if [ $mode = prepare ]; then + progress prepared 1000 + echo "Prepared: Lab builds of branches on ${base:0:9} now compile only what changed ($(( ($(date +%s) - started) / 60 ))m)" + exit 0 +fi + +progress packaging 995 +linked=$(ls app/build/intermediates/linked_resources_binary_format/release/processReleaseResources/*.ap_ | head -1) +work=$(mktemp -d) +cp "$linked" "$work/overlay.apk" +(cd app/build/intermediates/dex/release/mergeDexRelease && zip -q "$work/overlay.apk" classes*.dex) +name="$(git branch --show-current) @ $(git rev-parse --short HEAD)" +python3 -c 'import json,sys,time; print(json.dumps({"label": sys.argv[1], "baseCommit": sys.argv[2], "baseApk": sys.argv[3], "built": int(time.time())}))' \ + "$name" "$label" "$apk" > "$work/overlay.json" + +lab=/data/user/0/$pkg/files/lab +mkdir -p "$lab" +rm -rf "$lab/next.part" "$lab/next" +mv "$work" "$lab/next.part" +mv "$lab/next.part" "$lab/next" +progress ready 1000 "$name" +echo "Lab build ready: $name ($(du -h "$lab/next/overlay.apk" | cut -f1)) in $(( ($(date +%s) - started) / 60 ))m" +[ $mode = build ] || exit 0 + +# Classes in an app image load before the Lab build can, so the app is recompiled without one. +device cmd package compile -m verify -f $pkg >/dev/null +progress restarting 1000 "$name" +echo "Restarting DroidDeck on the Lab build; it shows a notification when it is back" +# The app's own agent ends with the app, so it is told to carry on once the app is back. +if [ -n "${DD_AGENT:-}" ]; then mkdir -p "/data/user/0/$pkg/files/agent" && : > "/data/user/0/$pkg/files/agent/resume"; fi +device "nohup sh -c 'sleep 3; monkey -p $pkg -c android.intent.category.LAUNCHER 1' >/dev/null 2>&1 &" +for pid in $(device pidof $pkg); do kill "$pid" 2>/dev/null || true; done diff --git a/tools/agent/setup b/tools/agent/setup new file mode 100755 index 0000000000..49dd82592a --- /dev/null +++ b/tools/agent/setup @@ -0,0 +1,76 @@ +#!/bin/bash +# Sets up the on-device agent inside DroidDeck's Linux userland: Claude Code, git and gh, loopback +# adb for droiddeckctl, and the toolchain dd-lab-build uses to build Lab builds on the device. +# Run it from the DroidDeck terminal, inside a checkout of the DroidDeck repository. +set -euo pipefail +here=$(cd "$(dirname "$0")" && pwd) +export DEBIAN_FRONTEND=noninteractive +tmp=$(mktemp -d) +trap 'rm -rf "$tmp"' EXIT + +apt-get install -y --no-install-recommends git gh adb python3 zip unzip curl openjdk-17-jdk-headless qemu-user zipalign apksigner + +command -v claude >/dev/null || [ -x "$HOME/.local/bin/claude" ] || + { curl -fsSL -o "$tmp/install-claude.sh" https://claude.ai/install.sh && bash "$tmp/install-claude.sh"; } + +mkdir -p "$HOME/.local/bin" +for tool in dd-agent dd-lab-build dd-gh-login android-open; do install -m755 "$here/$tool" "$HOME/.local/bin/$tool"; done +grep -q 'droiddeck-agent-path' "$HOME/.bashrc" 2>/dev/null || + printf '\n# droiddeck-agent-path\nexport PATH="$HOME/.local/bin:$PATH" ADB_SERIAL=127.0.0.1:5555 BROWSER="$HOME/.local/bin/android-open"\n' >> "$HOME/.bashrc" + +# Google publishes aapt2 for x86-64 Linux only; it runs here under qemu-user with Ubuntu's x86-64 runtime. +x86=$HOME/x86root +mkdir -p "$HOME/aapt2/x86" "$x86/etc" +if [ ! -x "$HOME/aapt2/x86/aapt2" ]; then + agp=$(sed -n "s/.*com.android.tools.build:gradle:\([0-9.]*\).*/\1/p" "$here/../../build.gradle" | head -1) + [ -n "$agp" ] || { echo "setup: no Android Gradle plugin version in build.gradle" >&2; exit 1; } + aapt2=$(curl -fsSL "https://dl.google.com/android/maven2/com/android/tools/build/aapt2/maven-metadata.xml" | + { grep -o "$agp-[0-9]*" || true; } | tail -1 | sed 's/<[^>]*>//g') + [ -n "$aapt2" ] || { echo "setup: Google publishes no aapt2 for AGP $agp" >&2; exit 1; } + curl -fsSL -o "$tmp/aapt2.jar" "https://dl.google.com/android/maven2/com/android/tools/build/aapt2/$aapt2/aapt2-$aapt2-linux.jar" + unzip -o -q "$tmp/aapt2.jar" aapt2 -d "$HOME/aapt2/x86" + chmod +x "$HOME/aapt2/x86/aapt2" +fi +if [ ! -e "$x86/lib64/ld-linux-x86-64.so.2" ]; then + codename=$(. /etc/os-release && echo "$VERSION_CODENAME") + curl -fsSL "https://archive.ubuntu.com/ubuntu/dists/$codename/main/binary-amd64/Packages.gz" | gunzip > "$tmp/amd64-packages" + for package in libc6 libgcc-s1 libstdc++6 zlib1g; do + read -r file sum < <(awk -v p="$package" '$0 == "Package: " p {found = 1} found && /^Filename:/ {f = $2} + found && /^SHA256:/ {s = $2} found && /^$/ {exit} END {print f, s}' "$tmp/amd64-packages") + [ -n "$sum" ] || { echo "setup: $package is not in Ubuntu $codename's amd64 index" >&2; exit 1; } + curl -fsSL -o "$tmp/$package.deb" "https://archive.ubuntu.com/ubuntu/$file" + echo "$sum $tmp/$package.deb" | sha256sum -c --quiet - + dpkg-deb -x "$tmp/$package.deb" "$x86" + done + ln -sfn usr/lib64 "$x86/lib64" + ln -sfn usr/lib "$x86/lib" + : > "$x86/etc/ld.so.preload" +fi +# AGP only accepts a custom aapt2 whose file is named aapt2. +printf '#!/bin/sh\nexec qemu-x86_64 -L %s %s/aapt2/x86/aapt2 "$@"\n' "$x86" "$HOME" > "$HOME/aapt2/aapt2" +chmod +x "$HOME/aapt2/aapt2" +"$HOME/aapt2/aapt2" version + +sdk=$HOME/android-sdk +if [ ! -x "$sdk/cmdline-tools/latest/bin/sdkmanager" ]; then + mkdir -p "$sdk/cmdline-tools" + curl -fsSL -o "$tmp/cmdline-tools.zip" https://dl.google.com/android/repository/commandlinetools-linux-13114758_latest.zip + unzip -q -o "$tmp/cmdline-tools.zip" -d "$sdk/cmdline-tools" + rm -rf "$sdk/cmdline-tools/latest" + mv "$sdk/cmdline-tools/cmdline-tools" "$sdk/cmdline-tools/latest" +fi +export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-arm64 +yes | "$sdk/cmdline-tools/latest/bin/sdkmanager" --licenses >/dev/null +"$sdk/cmdline-tools/latest/bin/sdkmanager" "platforms;android-34" "build-tools;35.0.0" >/dev/null + +adb connect 127.0.0.1:5555 || true +# The first build compiles the whole app; doing it now makes the agent's Lab builds incremental. +if [ "${DD_SKIP_PREPARE:-}" != 1 ]; then + echo "Approve the USB debugging prompt if one appears." + if timeout 120 adb -s 127.0.0.1:5555 wait-for-device; then + "$here/dd-lab-build" --prepare || echo "The first Lab build will compile the whole app" + else + echo "adb did not connect, so the first Lab build will compile the whole app" + fi +fi +echo "Done. Run dd-agent." diff --git a/tools/build_local.sh b/tools/build_local.sh index 041d6b3131..d564f1e3c0 100755 --- a/tools/build_local.sh +++ b/tools/build_local.sh @@ -133,6 +133,7 @@ docker run --rm --platform "${docker_platform}" \ 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" + bash tools/nativeguest/build.sh "$d/libddnatv-hook.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" @@ -424,6 +425,15 @@ if ! restore_step comet "${comet_key}" "${comet_out}"; then DROIDDECK_BUILD_IMAGE="${image_name}" bash tools/comet/build-local.sh save_step comet "${comet_key}" "${comet_out}" fi +if [[ "${DROIDDECK_SKIP_RUST:-0}" != 1 ]]; then + upc_out="${linuxfs_dir}/usr/local/lib/droiddeck-ubisoft" + upc_key=$(inputs_hash "x86_64-pc-windows-gnu" tools/ubisoft-launcher/src tools/ubisoft-launcher/Cargo.toml tools/ubisoft-launcher/Cargo.lock tools/ubisoft-launcher/build.sh) + if ! restore_step ubisoft "${upc_key}" "${upc_out}"; then + command -v x86_64-w64-mingw32-gcc >/dev/null 2>&1 || { echo "x86_64-w64-mingw32-gcc is required for the Ubisoft launcher service (droiddeck-upc.exe)." >&2; exit 1; } + CARGO_TARGET_DIR="${cache_dir}/ubisoft-target" bash tools/ubisoft-launcher/build.sh "${upc_out}" + save_step ubisoft "${upc_key}" "${upc_out}" + fi +fi python3 tools/powervr/fetch.py bash tools/packs/build.sh app/build/packs python3 tools/release/runtime_inputs.py record diff --git a/tools/ci/install-ubuntu-packages.sh b/tools/ci/install-ubuntu-packages.sh index 1597613aaa..d1bd5e698a 100644 --- a/tools/ci/install-ubuntu-packages.sh +++ b/tools/ci/install-ubuntu-packages.sh @@ -10,9 +10,13 @@ if [ "${#missing[@]}" = 0 ]; then echo "Ubuntu build dependencies are already installed." exit 0 fi -options=( - -o Dir::Etc::sourcelist=sources.list.d/ubuntu.sources - -o Dir::Etc::sourceparts=- +options=() +# Runners without the deb822 Ubuntu sources file keep their own apt configuration. +ubuntu_sources=${UBUNTU_SOURCES_FILE:-/etc/apt/sources.list.d/ubuntu.sources} +if [ -f "$ubuntu_sources" ]; then + options+=(-o "Dir::Etc::sourcelist=$ubuntu_sources" -o Dir::Etc::sourceparts=-) +fi +options+=( -o Acquire::Retries=1 -o Acquire::http::Timeout=15 -o Acquire::https::Timeout=15 @@ -27,14 +31,22 @@ install_packages() { if install_packages; then exit 0 fi -echo "::warning::Runner mirror failed or timed out; retrying with the official Ubuntu mirror." +[ -f "$ubuntu_sources" ] || exit 1 sources=$(mktemp --suffix=.sources) trap 'rm -f "$sources"' EXIT -mirror=https://archive.ubuntu.com/ubuntu +# The last mirror of each list is outside Canonical's network, so it still answers when runners cannot reach Canonical. +mirrors=(https://archive.ubuntu.com/ubuntu https://mirrors.edge.kernel.org/ubuntu) case "$(dpkg --print-architecture)" in - arm64) mirror=https://ports.ubuntu.com/ubuntu-ports ;; + arm64) mirrors=(https://ports.ubuntu.com/ubuntu-ports https://mirrors.ocf.berkeley.edu/ubuntu-ports) ;; esac -sed -E "s@^URIs:.*@URIs: $mirror@" /etc/apt/sources.list.d/ubuntu.sources > "$sources" -chmod 644 "$sources" -options[1]="Dir::Etc::sourcelist=$sources" -install_packages +status=1 +for mirror in "${mirrors[@]}"; do + echo "::warning::Ubuntu mirror failed or timed out; retrying with $mirror." + sed -E "s@^URIs:.*@URIs: $mirror@" "$ubuntu_sources" > "$sources" + chmod 644 "$sources" + options[1]="Dir::Etc::sourcelist=$sources" + status=0 + install_packages || status=$? + [ "$status" = 0 ] && exit 0 +done +exit "$status" diff --git a/tools/components/build-arm64-preloads.sh b/tools/components/build-arm64-preloads.sh index a38a1edaaa..e06aa2afc1 100644 --- a/tools/components/build-arm64-preloads.sh +++ b/tools/components/build-arm64-preloads.sh @@ -12,6 +12,7 @@ aarch64-linux-gnu-strip --strip-unneeded "$D/libblsession.so" aarch64-linux-gnu-gcc -shared -fPIC -O2 -Wall -pthread \ -o "$D/libblfastpath.so" tools/proot/fastpath/fastpath.c -ldl aarch64-linux-gnu-strip --strip-unneeded "$D/libblfastpath.so" +bash tools/nativeguest/build.sh "$D/libddnatv-hook.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" diff --git a/tools/components/preloads.py b/tools/components/preloads.py index 808d510463..36d0b3dcc8 100644 --- a/tools/components/preloads.py +++ b/tools/components/preloads.py @@ -51,6 +51,7 @@ def source_files(group, root=ROOT, commit=None, patterns=None): if not paths: raise ValueError('No preload inputs found') result = {} + listed = set(paths) pending = list(paths) while pending: path = pending.pop() @@ -58,7 +59,13 @@ def source_files(group, root=ROOT, commit=None, patterns=None): continue if path.startswith('../') or path.startswith('/'): raise ValueError('Preload include escapes repository') - content = read(path).replace(b'\r\n', b'\n') + try: + content = read(path).replace(b'\r\n', b'\n') + except (OSError, subprocess.CalledProcessError): + # a header the build generates (gosys/gosys_blob.h, included behind __has_include) + if path in listed: + raise + continue result[path] = sha(content) for include in re.findall(rb'^\s*#\s*include\s*"([^"\n]+)"', content, re.M): pending.append(posixpath.normpath(posixpath.join(posixpath.dirname(path), include.decode()))) @@ -126,13 +133,15 @@ def artifact_bytes(origin, cache): if len(names) != 1: raise ValueError('Expected one original APK') with ZipFile(io.BytesIO(archive.read(names[0]))) as apk: - return {name: apk.read('assets/linuxfs/' + name) for name in FILES} + present = set(apk.namelist()) + return {name: apk.read('assets/linuxfs/' + name) for name in FILES if 'assets/linuxfs/' + name in present} def validate_lock(data): if data.get('version') != 1 or set(data['groups']) != set(INPUTS): raise ValueError('Unsupported preload snapshot') - if set(data['outputs']) != FILES: + # A lock made before a recipe gained an output lacks it; that group is rebuilt (restore) + if not set(data['outputs']) <= FILES: raise ValueError('Unexpected preload output set') for digest in [data['archive_sha256'], *data['outputs'].values()]: if len(digest) != 64 or any(c not in '0123456789abcdef' for c in digest): @@ -160,7 +169,7 @@ def producer_sources(group, commit, root=ROOT): def prove_origin(data, cache, root=ROOT): content = artifact_bytes(data['origin'], cache) - if {name: sha(value) for name, value in content.items()} != data['outputs']: + if {name: sha(value) for name, value in content.items() if name in data['outputs']} != data['outputs']: raise ValueError('Snapshot binaries differ from the original main APK') commit = data['origin']['commit'] run('git', 'fetch', '--no-tags', 'origin', commit, root=root) @@ -183,7 +192,7 @@ def unpack(content, data): total = 0 with tarfile.open(fileobj=io.BytesIO(content), mode='r:gz') as archive: for entry in archive: - if entry.name not in FILES or entry.name in result or not entry.isfile() or entry.size > 32 * 1024 * 1024: + if entry.name not in data['outputs'] or entry.name in result or not entry.isfile() or entry.size > 32 * 1024 * 1024: raise ValueError('Unsafe or unexpected preload archive entry') total += entry.size if total > 64 * 1024 * 1024: @@ -192,7 +201,7 @@ def unpack(content, data): if sha(value) != data['outputs'][entry.name]: raise ValueError('Preload binary checksum mismatch') result[entry.name] = value - if set(result) != FILES: + if set(result) != set(data['outputs']): raise ValueError('Incomplete preload archive') return result @@ -203,6 +212,8 @@ def restore(root=ROOT): matching = {group: source_files(group, root) == data['groups'][group] for group in INPUTS} matching['arm64'] &= recipe(root) == data['arm64_recipe'] matching['x86'] &= x86_recipe(root) == data.get('x86_recipe') + for group in matching: + matching[group] &= all(name in data['outputs'] for name in OUTPUTS[group]) if os.environ.get('FORCE_PRELOAD_REBUILD') == 'true': matching = dict.fromkeys(INPUTS, False) if any(matching.values()): diff --git a/tools/components/recipes.json b/tools/components/recipes.json index 8278e966f3..92fcc548a4 100644 --- a/tools/components/recipes.json +++ b/tools/components/recipes.json @@ -139,6 +139,9 @@ "app/src/main/jniLibs/arm64-v8a/libproot.so", "app/src/main/jniLibs/arm64-v8a/libproot-loader.so" ], + "packages": [ + "gawk" + ], "ndk": true }, "comet": { @@ -161,7 +164,8 @@ "tools/components/build-arm64-preloads.sh", "tools/linuxfs/preload", "app/src/main/cpp/fakeinput_steam.cpp", - "tools/components/recipes/arm64-preloads.sh" + "tools/components/recipes/arm64-preloads.sh", + "tools/nativeguest" ], "outputs": [ "app/src/main/assets/linuxfs/libfakeinput.so", @@ -169,7 +173,8 @@ "app/src/main/assets/linuxfs/libblfastpath.so", "app/src/main/assets/linuxfs/libblaudit.so", "app/src/main/assets/linuxfs/libssbs.so", - "app/src/main/assets/linuxfs/usr/local/bin/droiddeck-clipboard" + "app/src/main/assets/linuxfs/usr/local/bin/droiddeck-clipboard", + "app/src/main/assets/linuxfs/libddnatv-hook.so" ], "packages": [ "gcc-aarch64-linux-gnu", @@ -184,7 +189,8 @@ "tools/quest/kgsl_compat.h", "tools/proot/fastpath", "tools/linuxfs/ssbs", - "tools/linuxfs/clipboard" + "tools/linuxfs/clipboard", + "tools/nativeguest" ], "snapshot_outputs": [ "libfakeinput.so", @@ -192,7 +198,8 @@ "libblfastpath.so", "libblaudit.so", "libssbs.so", - "usr/local/bin/droiddeck-clipboard" + "usr/local/bin/droiddeck-clipboard", + "libddnatv-hook.so" ] }, "preload-snapshot": { diff --git a/tools/droiddeckctl b/tools/droiddeckctl index 65fb274cef..3e37811d28 100755 --- a/tools/droiddeckctl +++ b/tools/droiddeckctl @@ -9,6 +9,7 @@ import gzip import json import os import re +import shlex import shutil import subprocess import struct @@ -98,6 +99,33 @@ def encode_request(request: dict) -> str: return encoded +def receiver_call(adb: str, serial: str, method: str, arg: str | None, request: dict | None, timeout: float) -> dict: + """The agent bridge through a broadcast, for shells without the `content` command.""" + # Its receiver is registered by the running app; a broadcast does not start the app the way `content call` does + running = run_adb(adb, serial, "shell", f"pidof {PACKAGE}", timeout=30) + if not running.stdout.decode(errors="replace").strip(): + raise ControlError(f"{PACKAGE} is not running and this shell has no `content` command to start it; open DroidDeck once", "APP_NOT_RUNNING") + call_id = uuid.uuid4().hex[:16] + remote = f"/sdcard/Android/data/{PACKAGE}/files/agent-calls/{call_id}.json" + command = ["shell", "am", "broadcast", "-a", f"{PACKAGE}.AGENT_CALL", "-p", PACKAGE, + "--es", "method", method, "--es", "id", call_id] + if arg is not None: + command += ["--es", "arg", shlex.quote(arg)] + if request is not None: + command += ["--es", "request", encode_request(request)] + started = run_adb(adb, serial, *command, timeout=timeout + 30) + if started.returncode != 0: + raise ControlError((started.stdout + started.stderr).decode(errors="replace").strip() or "ADB broadcast failed", "ADB_FAILED") + deadline = time.monotonic() + timeout + while time.monotonic() < deadline: + result = run_adb(adb, serial, "shell", f"cat {remote} 2>/dev/null && rm -f {remote}", timeout=30) + text = result.stdout.decode(errors="replace").strip() + if result.returncode == 0 and text: + return json.loads(text) + time.sleep(0.25) + raise ControlError(f"No answer to {method} within {timeout:g} s", "TIMEOUT") + + def provider_call(adb: str, serial: str, method: str, arg: str | None = None, request: dict | None = None, timeout: float = 30) -> dict: command = ["shell", "content", "call", "--uri", AUTHORITY, "--method", method] @@ -108,6 +136,9 @@ def provider_call(adb: str, serial: str, method: str, arg: str | None = None, re command.extend(["--extra", f"request:s:{encoded}"]) result = run_adb(adb, serial, *command, timeout=timeout) output = (result.stdout + result.stderr).decode(errors="replace") + # Only the shell's own answer for a missing `content` binary, never a command's output that quotes it + if result.returncode == 127 and "Result:" not in output and "content: inaccessible or not found" in result.stderr.decode(errors="replace") + output: + return receiver_call(adb, serial, method, arg, request, timeout) response = _content_result(output) if result.returncode != 0 and response.get("ok") is not False: raise ControlError(output.strip() or "ADB content call failed", "ADB_FAILED") @@ -140,6 +171,9 @@ def start_agent_activity(adb: str, serial: str, request: dict) -> None: output = (result.stdout + result.stderr).decode(errors="replace") if result.returncode != 0 or "status: ok" not in output.lower(): raise ControlError(output.strip() or "The agent start Activity did not launch", "ACTIVITY_START_FAILED") + if "activity not started" in output.lower(): + raise ControlError("Android handed the start request to an existing DroidDeck task instead of starting it: " + + output.strip(), "ACTIVITY_START_FAILED") def confirm_start(adb: str, serial: str, request: dict, previous: dict) -> dict: @@ -237,10 +271,15 @@ def displays(adb: str, serial: str) -> list[dict]: device = re.search(r"mPrimaryDisplayDevice=(.+)", block) size = re.search(r"real (\d+) x (\d+)", block) name = device.group(1).strip() if device else None + physical = devices.get(name) if name else None + if name and not physical: + # Some ROMs (Retroid Pocket Duo) print the device as "Built-in Screen(local:)" + inline = re.search(r"\(local:(\d+)\)$", name) + physical = inline.group(1) if inline else None out.append({ "display": logical, "name": name, - "physicalId": devices.get(name) if name else None, + "physicalId": physical, "size": [int(size.group(1)), int(size.group(2))] if size else None, }) return out @@ -295,6 +334,7 @@ def record(adb: str, serial: str, path: str, seconds: float, display: int | None def pacing_summary(text: str, package: str = PACKAGE) -> dict: + text = text.replace("\r\n", "\n") records = {"game": [], "surface": []} for part in re.split(r"^displayRefreshRate = ", text, flags=re.M)[1:]: rate = re.match(r"(\d+)", part) @@ -749,6 +789,28 @@ def execute(args: argparse.Namespace, adb: str, serial: str) -> dict: f"{(response.get('stderr') or {}).get('text', '')[-400:]}", "GUEST_EXIT") return add_serial(response, serial) + if command == "native": + argv = args.argv[1:] if args.argv[:1] == ["--"] else args.argv + if not argv: + raise ControlError("native needs a command after --", "INVALID_ARGUMENTS") + if not 0 < args.timeout <= 600: + raise ControlError("--timeout must be more than 0 and at most 600 seconds", "INVALID_ARGUMENTS") + env = {} + for item in args.env or []: + key, sep, value = item.partition("=") + if not sep: + raise ControlError(f"--env takes KEY=VALUE, not {item}", "INVALID_ARGUMENTS") + env[key] = value + request = {"argv": argv, "timeout": args.timeout, "env": env} + if args.cwd: + request["cwd"] = args.cwd + if args.stdin is not None: + request["stdin"] = args.stdin + if args.preload: + request["preload"] = args.preload + response = checked(provider_call(adb, serial, "native", request=request, timeout=args.timeout + 40), "NATIVE_FAILED") + return add_serial(response, serial) + if command == "cdp": if args.cdp_op == "targets": request = {"op": "targets"} @@ -983,6 +1045,14 @@ def build_parser() -> argparse.ArgumentParser: guest.add_argument("--check", action="store_true", help="fail unless the command exits 0") guest.add_argument("argv", nargs=argparse.REMAINDER) + native = commands.add_parser("native", help="run a command in the rootfs without proot (debugging tools)") + native.add_argument("--timeout", type=float, default=30) + native.add_argument("--env", action="append", metavar="KEY=VALUE") + native.add_argument("--cwd") + native.add_argument("--stdin") + native.add_argument("--preload", action="append", help="guest path of another library to preload before the hook") + native.add_argument("argv", nargs=argparse.REMAINDER) + cdp = commands.add_parser("cdp", help="the Steam client's DevTools (debugging tools)") cdp_ops = cdp.add_subparsers(dest="cdp_op", required=True) cdp_ops.add_parser("targets", help="list DevTools targets") diff --git a/tools/gamescope/PATCHES.md b/tools/gamescope/PATCHES.md index 4736d32320..8608d7c260 100644 --- a/tools/gamescope/PATCHES.md +++ b/tools/gamescope/PATCHES.md @@ -302,6 +302,7 @@ For games that confine a visible cursor and warp it, which the automatic rule ca The rule only counted a lock with a cursor position hint. A game that hides the cursor and confines the pointer through a grab (Wine's ClipCursor) has a constraint with no hint, so the nested pointer stayed absolute, the constraint held the X pointer still and no raw motion was produced: the camera froze. +0133 narrows this, and the upstream hint rule, to games that hold the X pointer. ## 0129 steamcompmgr, wayland: release a pointer lock at the game's pointer @@ -326,8 +327,26 @@ each frame's motion to the X pointer itself until the game stops warping. Absolu motion, as a real compositor does, so XInput2 raw motion reaches games that read only that. A touch moves the pointer without relative motion, so tapping the screen does not turn the camera. +The X pointer is read in root coordinates and the seat position is local to the focused window, so the X pointer +is moved into the focused window's coordinates before the two are compared. Without that, any window not at the +root origin (a dialog, or a windowed program) looked like a game warping the pointer, following started, and the +pointer was pinned to the window's corner. A touch always lands where the finger is and ends following, so taps +work in every window. + ## 0132 sdl, steamcompmgr: follow a game's window mode With `--follow-game-window-mode`, the outer window of a nested SDL gamescope follows the focused game window. A window asking for `_NET_WM_STATE_FULLSCREEN`, or a Steam game covering the X screen without minimise or maximise functions or decorations in `_MOTIF_WM_HINTS`, makes the outer window fullscreen on the desktop. Anything else makes it a normal window of the game window's size, or a maximised one when that size does not fit the desktop with its title bar and panel. A change is applied only when the wanted mode changes, so a manual resize or Super+F stays. + +## 0133 steamcompmgr, wlserver: a pointer constraint is relative only while the game holds the X pointer + +A game that hides the system cursor, draws its own and grabs the pointer to its window (Stardew Valley, Balatro) gets a lock +from Xwayland, which emulates the game's warps with a cursor position hint. Upstream kept any hinted lock relative and 0128 +added every other constraint, so gamescope held the host's pointer lock and DroidDeck turned every direct touch swipe into +camera motion: dragging in those games only moved their cursor. Without relative mode such a lock still follows gamescope's +relative motion and touches still place the pointer. gamescope now compares the motion it sends (seat movement, or relative +motion while the seat is locked) with how the X pointer moves. A hinted constraint starts relative, as upstream, also when its hint arrives with the game's first warp, and is released +after 30 moving frames on which the X pointer follows. Any constraint goes relative once the X pointer moves less than a quarter +of that motion on three frames within half a second, as it does when the game recentres it or clips it to a small rectangle +(PEAK). `tools/tests/test_gamescope_pointer_hold.py` compiles the rule from this patch. diff --git a/tools/gamescope/patches/0131-follow-game-pointer-warps.patch b/tools/gamescope/patches/0131-follow-game-pointer-warps.patch index 9bbc592f54..844bb0de48 100644 --- a/tools/gamescope/patches/0131-follow-game-pointer-warps.patch +++ b/tools/gamescope/patches/0131-follow-game-pointer-warps.patch @@ -1,5 +1,5 @@ diff --git a/src/steamcompmgr.cpp b/src/steamcompmgr.cpp -index 17bae3a..c468fb3 100644 +index 17bae3a..91ec0be 100644 --- a/src/steamcompmgr.cpp +++ b/src/steamcompmgr.cpp @@ -32,6 +32,7 @@ @@ -18,7 +18,7 @@ index 17bae3a..c468fb3 100644 static bool relative_mouse_forced(); void MouseCursor::checkSuspension() -@@ -3083,12 +3085,89 @@ static gamescope::ConVar cv_relative_mouse( "relative_mouse", 0, +@@ -3083,12 +3085,101 @@ static gamescope::ConVar cv_relative_mouse( "relative_mouse", 0, "0 = Leave it to the game and the nested hints. 1 = Send host pointer motion as relative movement, for games that confine the cursor and warp it back to read movement. 2 = Never go relative." ); static bool relative_mouse_forced() { @@ -34,16 +34,28 @@ index 17bae3a..c468fb3 100644 + static double s_flLastX = 0.0; + static double s_flLastY = 0.0; + -+ int nRootX = 0; -+ int nRootY = 0; ++ int nClientX = 0; ++ int nClientY = 0; + if ( bAllowed && pFocus->cursor ) -+ pFocus->cursor->QueryRootPosition( &nRootX, &nRootY ); ++ pFocus->cursor->QueryRootPosition( &nClientX, &nClientY ); + else + bAllowed = false; + ++ steamcompmgr_win_t *pInputWindow = pFocus->inputFocusWindow; ++ if ( pInputWindow && pInputWindow->type == steamcompmgr_win_type_t::XWAYLAND ) ++ { ++ nClientX -= pInputWindow->xwayland().a.x; ++ nClientY -= pInputWindow->xwayland().a.y; ++ } ++ + wlserver_lock(); + const double flX = wlserver.mouse_surface_cursorx; + const double flY = wlserver.mouse_surface_cursory; ++ if ( s_bFollowClientPointer && !wlserver.bFollowClientPointer ) ++ { ++ s_nAwayFrames = 0; ++ s_bFollowClientPointer = false; ++ } + if ( !bAllowed ) + { + s_nAwayFrames = 0; @@ -53,7 +65,7 @@ index 17bae3a..c468fb3 100644 + else if ( !s_bFollowClientPointer ) + { + const bool bMoved = flX != s_flLastX || flY != s_flLastY; -+ if ( bMoved && ( std::abs( nRootX - flX ) > 48.0 || std::abs( nRootY - flY ) > 48.0 ) ) ++ if ( bMoved && ( std::abs( nClientX - flX ) > 48.0 || std::abs( nClientY - flY ) > 48.0 ) ) + { + const uint64_t ulNow = get_time_in_nanos(); + if ( ulNow - s_ulLastAway > 500'000'000ul ) @@ -70,12 +82,12 @@ index 17bae3a..c468fb3 100644 + wlserver.bFollowClientPointer = true; + wlserver.flFollowDX = 0.0; + wlserver.flFollowDY = 0.0; -+ wlserver_mousewarp( nRootX, nRootY, 0, true ); ++ wlserver_mousewarp( nClientX, nClientY, 0, true ); + } + } + else + { -+ const bool bWarped = std::abs( nRootX - flX ) > 2.0 || std::abs( nRootY - flY ) > 2.0; ++ const bool bWarped = std::abs( nClientX - flX ) > 2.0 || std::abs( nClientY - flY ) > 2.0; + const double flDX = std::trunc( wlserver.flFollowDX ); + const double flDY = std::trunc( wlserver.flFollowDY ); + if ( bWarped ) @@ -87,9 +99,9 @@ index 17bae3a..c468fb3 100644 + { + wlserver.flFollowDX -= flDX; + wlserver.flFollowDY -= flDY; -+ wlserver_mousewarp( nRootX, nRootY, 0, true ); ++ wlserver_mousewarp( nClientX, nClientY, 0, true ); + if ( flDX != 0.0 || flDY != 0.0 ) -+ wlserver_mousewarp( nRootX + flDX, nRootY + flDY, 0, true ); ++ wlserver_mousewarp( nClientX + flDX, nClientY + flDY, 0, true ); + } + + if ( s_nQuietFrames >= 60 ) @@ -110,7 +122,7 @@ index 17bae3a..c468fb3 100644 return true; global_focus_t *pFocus = GetCurrentFocus(); -@@ -10892,9 +10971,15 @@ steamcompmgr_main(int argc, char **argv) +@@ -10892,9 +10983,15 @@ steamcompmgr_main(int argc, char **argv) const int nRelativeMouse = cv_relative_mouse.Get(); const bool bForcedRelativeMouse = nRelativeMouse == 1 && !window_is_steam( pPaintFocus->inputFocusWindow ); @@ -128,7 +140,7 @@ index 17bae3a..c468fb3 100644 static bool s_bRelativeMouseMode = false; if ( s_bRelativeMouseMode && !bRelativeMouseMode && pPaintFocus->cursor && diff --git a/src/wlserver.cpp b/src/wlserver.cpp -index 5733871..cc75f8a 100644 +index 5733871..df23f8a 100644 --- a/src/wlserver.cpp +++ b/src/wlserver.cpp @@ -1,3 +1,4 @@ @@ -153,7 +165,7 @@ index 5733871..cc75f8a 100644 if ( !wlserver_apply_constraint( &dx, &dy ) ) { wlr_seat_pointer_notify_frame( wlserver.wlr.seat ); -@@ -3110,10 +3121,18 @@ void wlserver_mousemotion( double dx, double dy, uint32_t time ) +@@ -3110,10 +3121,22 @@ void wlserver_mousemotion( double dx, double dy, uint32_t time ) wlr_seat_pointer_notify_frame( wlserver.wlr.seat ); } @@ -163,7 +175,11 @@ index 5733871..cc75f8a 100644 assert( wlserver_is_lock_held() ); + if ( !bSynthetic && wlserver.bFollowClientPointer ) -+ return; ++ { ++ if ( !bFromTouch ) ++ return; ++ wlserver.bFollowClientPointer = false; ++ } + + const double flStepX = x - wlserver.mouse_surface_cursorx; + const double flStepY = y - wlserver.mouse_surface_cursory; @@ -173,7 +189,7 @@ index 5733871..cc75f8a 100644 wlserver.mouse_surface_cursorx = x; wlserver.mouse_surface_cursory = y; -@@ -3304,7 +3323,7 @@ void wlserver_touchmotion( double x, double y, int touch_id, uint32_t time, bool +@@ -3304,7 +3327,7 @@ void wlserver_touchmotion( double x, double y, int touch_id, uint32_t time, bool { g_bPendingTouchMovement = true; @@ -182,7 +198,7 @@ index 5733871..cc75f8a 100644 } } -@@ -3352,7 +3371,7 @@ void wlserver_touchdown( double x, double y, int touch_id, uint32_t time, gamesc +@@ -3352,7 +3375,7 @@ void wlserver_touchdown( double x, double y, int touch_id, uint32_t time, gamesc if ( eMode != gamescope::TouchClickModes::Trackpad ) { diff --git a/tools/gamescope/patches/0133-constraint-relative-when-held.patch b/tools/gamescope/patches/0133-constraint-relative-when-held.patch new file mode 100644 index 0000000000..91fa1b1ac6 --- /dev/null +++ b/tools/gamescope/patches/0133-constraint-relative-when-held.patch @@ -0,0 +1,173 @@ +diff --git a/src/steamcompmgr.cpp b/src/steamcompmgr.cpp +--- a/src/steamcompmgr.cpp ++++ b/src/steamcompmgr.cpp +@@ -3165,6 +3165,128 @@ + wlserver_unlock(); + } + ++struct PointerHold ++{ ++ int nAwayFrames = 0; ++ int nFreeFrames = 0; ++ uint64_t ulFirstAway = 0; ++ bool bHeld = false; ++}; ++ ++static bool pointer_hold_step( PointerHold &state, bool bConstrained, double flSeatDX, double flSeatDY, double flPointerDX, double flPointerDY, uint64_t ulNow ) ++{ ++ if ( !bConstrained ) ++ { ++ state = PointerHold{}; ++ return false; ++ } ++ ++ const double flSeat = std::max( std::abs( flSeatDX ), std::abs( flSeatDY ) ); ++ if ( flSeat < 4.0 ) ++ return state.bHeld; ++ ++ const double flMiss = std::max( std::abs( flSeatDX - flPointerDX ), std::abs( flSeatDY - flPointerDY ) ); ++ if ( flMiss > flSeat * 0.75 ) ++ { ++ if ( state.nAwayFrames == 0 || ulNow - state.ulFirstAway > 500'000'000ul ) ++ { ++ state.nAwayFrames = 0; ++ state.ulFirstAway = ulNow; ++ } ++ state.nFreeFrames = 0; ++ if ( ++state.nAwayFrames >= 3 ) ++ state.bHeld = true; ++ } ++ else if ( state.bHeld && ++state.nFreeFrames >= 30 ) ++ { ++ state = PointerHold{}; ++ } ++ ++ return state.bHeld; ++} ++ ++static bool s_bPointerHeld = false; ++ ++static void update_pointer_hold( global_focus_t *pFocus, bool bConstrained, bool bHinted ) ++{ ++ static PointerHold s_Hold; ++ static bool s_bTracking = false; ++ static double s_flSeatX = 0.0; ++ static double s_flSeatY = 0.0; ++ static int s_nRootX = 0; ++ static int s_nRootY = 0; ++ static bool s_bHinted = false; ++ static const steamcompmgr_win_t *s_pWindow = nullptr; ++ static const struct wlr_pointer_constraint_v1 *s_pConstraint = nullptr; ++ ++ wlserver_lock(); ++ const double flX = wlserver.mouse_surface_cursorx; ++ const double flY = wlserver.mouse_surface_cursory; ++ const double flSentX = wlserver.flSentDX; ++ const double flSentY = wlserver.flSentDY; ++ const struct wlr_pointer_constraint_v1 *pConstraint = wlserver.GetCursorConstraint(); ++ wlserver.flSentDX = 0.0; ++ wlserver.flSentDY = 0.0; ++ wlserver_unlock(); ++ ++ if ( !bConstrained || !pFocus->cursor ) ++ { ++ s_bTracking = false; ++ s_bHinted = false; ++ s_bPointerHeld = pointer_hold_step( s_Hold, false, 0.0, 0.0, 0.0, 0.0, 0 ); ++ return; ++ } ++ ++ const steamcompmgr_win_t *pWindow = pFocus->inputFocusWindow; ++ if ( pWindow != s_pWindow || pConstraint != s_pConstraint ) ++ { ++ s_bTracking = false; ++ s_bHinted = false; ++ } ++ s_pWindow = pWindow; ++ s_pConstraint = pConstraint; ++ ++ if ( s_bTracking && bHinted && !s_bHinted ) ++ { ++ s_Hold = PointerHold{}; ++ s_Hold.bHeld = true; ++ s_bPointerHeld = true; ++ } ++ s_bHinted = bHinted; ++ ++ double flDX = flX - s_flSeatX; ++ double flDY = flY - s_flSeatY; ++ if ( flDX == 0.0 && flDY == 0.0 ) ++ { ++ flDX = flSentX; ++ flDY = flSentY; ++ } ++ ++ if ( s_bTracking && flDX == 0.0 && flDY == 0.0 ) ++ return; ++ ++ int nRootX = 0; ++ int nRootY = 0; ++ pFocus->cursor->QueryRootPosition( &nRootX, &nRootY ); ++ ++ if ( s_bTracking ) ++ { ++ s_bPointerHeld = pointer_hold_step( s_Hold, true, flDX, flDY, nRootX - s_nRootX, nRootY - s_nRootY, get_time_in_nanos() ); ++ } ++ else ++ { ++ s_Hold = PointerHold{}; ++ s_Hold.bHeld = bHinted; ++ s_bPointerHeld = bHinted; ++ } ++ ++ s_bTracking = true; ++ s_flSeatX = flX; ++ s_flSeatY = flY; ++ s_nRootX = nRootX; ++ s_nRootY = nRootY; ++} ++ + bool ShouldDrawCursor() + { + if ( relative_mouse_forced() ) +@@ -11029,9 +11151,15 @@ + nRelativeMouse != 2 && !bExcludedAppId && !bHasPointerConstraint && !window_is_steam( pPaintFocus->inputFocusWindow ) ); + } + ++ if ( vblank ) ++ update_pointer_hold( pPaintFocus, bHasPointerConstraint && bImageEmpty && nRelativeMouse != 2 && !bExcludedAppId, ++ pPaintFocus->cursor && pPaintFocus->cursor->IsConstrained() ); ++ ++ const bool bGameHoldsPointer = bHasPointerConstraint && s_bPointerHeld; ++ + const bool bRelativeMouseMode = nRelativeMouse == 2 + ? false +- : ( bForcedRelativeMouse || s_bFollowClientPointer || ( bImageEmpty && bHasPointerConstraint && !bExcludedAppId ) ); ++ : ( bForcedRelativeMouse || s_bFollowClientPointer || ( bImageEmpty && bGameHoldsPointer && !bExcludedAppId ) ); + + static bool s_bRelativeMouseMode = false; + if ( s_bRelativeMouseMode && !bRelativeMouseMode && pPaintFocus->cursor && +diff --git a/src/wlserver.cpp b/src/wlserver.cpp +--- a/src/wlserver.cpp ++++ b/src/wlserver.cpp +@@ -346,6 +346,8 @@ + { + assert( wlserver_is_lock_held() ); + ++ wlserver.flSentDX += unaccel_dx; ++ wlserver.flSentDY += unaccel_dy; + wlr_relative_pointer_manager_v1_send_relative_motion( wlserver.relative_pointer_manager, wlserver.wlr.seat, 0, unaccel_dx, unaccel_dy, unaccel_dx, unaccel_dy ); + } + +diff --git a/src/wlserver.hpp b/src/wlserver.hpp +--- a/src/wlserver.hpp ++++ b/src/wlserver.hpp +@@ -171,6 +171,8 @@ + bool bFollowClientPointer = false; + double flFollowDX = 0.0; + double flFollowDY = 0.0; ++ double flSentDX = 0.0; ++ double flSentDY = 0.0; + + bool button_held[ WLSERVER_BUTTON_COUNT ]; + std::set touch_down_ids; diff --git a/tools/gamescope/release.env b/tools/gamescope/release.env index 88f8ff1cf9..bf5211b032 100644 --- a/tools/gamescope/release.env +++ b/tools/gamescope/release.env @@ -1,3 +1,3 @@ GAMESCOPE_REPO=Droid-Deck/DroidDeck-Components -GAMESCOPE_TAG=gamescope-3.16.29-p22 -GAMESCOPE_SHA256=2b7e7481df5f5f7355d224d3a0d4205557367172653dff1d7206a230b6c78b46 +GAMESCOPE_TAG=gamescope-3.16.29-p23 +GAMESCOPE_SHA256=f5f2b8f658234ad2910a1be53c238188db7e7b2a9079f8cc5fa9ea006bebbe7e diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-agent b/tools/linuxfs/overlay/usr/local/bin/droiddeck-agent index 234be2f001..5ae8031e68 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-agent +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-agent @@ -927,6 +927,33 @@ def steam_shortcut_state(agent_dir, game_id, timeout=6): time.sleep(min(0.5, max(0, deadline - time.monotonic()))) +def steam_ready(agent_dir): + expression = """(() => { + const store = globalThis.appStore; + return !!store?.m_bIsInitialized && typeof globalThis.SteamClient?.Apps?.ContinueGameAction === 'function'; + })()""" + return cdp_eval(agent_dir, expression, await_promise=False, timeout=3).get("value") is True + + +def steam_clear_pending_interstitial(agent_dir, app_id): + if not isinstance(app_id, str) or not app_id.isascii() or not app_id.isdigit() or int(app_id) <= 0: + raise AgentError("INVALID_APP_ID", "appId must be a positive decimal Steam app id") + expression = """(async () => { + const apps = globalThis.SteamClient?.Apps; + if (typeof apps?.GetActiveGameActions !== 'function' || typeof apps.ContinueGameAction !== 'function') return false; + const active = await apps.GetActiveGameActions() || []; + for (const action of active) { + const appid = String(action?.strAppID ?? action?.gameid ?? action?.nAppID ?? ''); + if (appid === %s && action?.strTaskName === 'ShowInterstitials') { + apps.ContinueGameAction(action.nGameActionID, appid, 'ShowInterstitials'); + return true; + } + } + return false; + })()""" % json.dumps(app_id) + return cdp_eval(agent_dir, expression, timeout=3).get("value") is True + + def game_pids(app_id): wanted = [("SteamAppId=%s" % app_id).encode(), ("SteamGameId=%s" % app_id).encode()] me = os.getpid() @@ -1261,6 +1288,19 @@ def main(argv): return 1 print(state) return {"ready": 0, "missing": 4, "not-shortcut": 3}.get(state, 1) + if argv and argv[0] == "--steam-ready": + try: + return 0 if steam_ready(Path(base) / "agent") else 1 + except (AgentError, OSError, ValueError): + return 1 + if argv and argv[0] == "--steam-clear-pending-interstitial": + if len(argv) != 2: + print("usage: droiddeck-agent --steam-clear-pending-interstitial ", file=sys.stderr) + return 2 + try: + return 0 if steam_clear_pending_interstitial(Path(base) / "agent", argv[1]) else 1 + except (AgentError, OSError, ValueError): + return 1 serve(Path(base) / "agent", focus="--no-focus" not in argv) return 0 diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-appimage-exec b/tools/linuxfs/overlay/usr/local/bin/droiddeck-appimage-exec new file mode 100755 index 0000000000..454333efa9 --- /dev/null +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-appimage-exec @@ -0,0 +1,16 @@ +#!/bin/bash +set -u +image=${1:?usage: droiddeck-appimage-exec [args...]} +shift +export APPIMAGE_EXTRACT_AND_RUN=1 +chmod +x "$image" 2>/dev/null +if [ -x /usr/local/bin/droiddeck-fex ]; then + case $(od -An -tu2 -j18 -N2 "$image" 2>/dev/null | tr -d ' ') in + 3|62) exec /usr/local/bin/droiddeck-fex run --mode on -- "$image" "$@" ;; + esac +fi +if [ -x /usr/local/lib/droiddeck/uruntime ]; then + export TARGET_APPIMAGE=$image + exec /usr/local/lib/droiddeck/uruntime "$@" +fi +exec "$image" "$@" diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-appimage-run b/tools/linuxfs/overlay/usr/local/bin/droiddeck-appimage-run index b23023b2e9..b4a1910f24 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-appimage-run +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-appimage-run @@ -55,6 +55,9 @@ if [ -x /usr/local/bin/droiddeck-fex ]; then *) run=(/usr/local/bin/droiddeck-fex run --mode "$fex" --for "$APPDIR" -- "$APPDIR/AppRun") ;; esac fi +gpu=("${BASH_SOURCE[0]%/*}/droiddeck-game-gpu") +[ ${#flags[@]} -gt 0 ] && gpu+=(--chromium) +run=("${gpu[@]}" -- "${run[@]}") echo "== appimage $ARGV0 ($APPDIR) ${flags[*]} FEX $fex Qt $qt${QT_QPA_PLATFORM:+ ($QT_QPA_PLATFORM)}" cd "$APPDIR" || exit 1 started=$(mktemp "${TMPDIR:-/tmp}/droiddeck-appimage.XXXXXX") diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-flatpak b/tools/linuxfs/overlay/usr/local/bin/droiddeck-flatpak index 8c23140efa..627471d312 100644 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-flatpak +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-flatpak @@ -302,10 +302,24 @@ def install_bundle(path): return rc +def stale_app_dir(inst, app_id): + if not app_id or "/" in app_id or app_id in (".", ".."): + return None + path = os.path.join(inst.get_path().get_path(), "app", app_id) + return path if os.path.isdir(path) and not os.path.islink(path) else None + + def uninstall(app_id): inst = installation() ref = app_ref(inst, app_id) if ref is None: + stale = stale_app_dir(inst, app_id) + if stale: + try: + shutil.rmtree(stale) + except OSError as e: + emit(e="error", message=str(e)) + return 1 emit(e="done", note="not installed") return 0 t, state = transaction(inst) diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-flatpak-run b/tools/linuxfs/overlay/usr/local/bin/droiddeck-flatpak-run index 08e6e7a265..b8210c3550 100644 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-flatpak-run +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-flatpak-run @@ -1,6 +1,7 @@ #!/bin/bash set -u export FLATPAK_BWRAP=/usr/local/bin/droiddeck-bwrap +unset MESA_LOADER_DRIVER_OVERRIDE GALLIUM_DRIVER LIBGL_KOPPER_DRI2 VK_ICD_FILENAMES VK_DRIVER_FILES case ":${XDG_DATA_DIRS:-}:" in *"/flatpak/exports/share:"*) ;; *) export XDG_DATA_DIRS=$HOME/.local/share/flatpak/exports/share:${XDG_DATA_DIRS:-/usr/local/share:/usr/share} ;; @@ -11,8 +12,16 @@ if [ -n "${BL_INSIDE:-}" ] || [ -n "${GAMESCOPE_WAYLAND_DISPLAY:-}" ]; then export XDG_SESSION_TYPE=x11 socket=(--nosocket=wayland --socket=x11) fi +app= +for arg in "$@"; do + case $arg in -*) ;; *) app=$arg; break ;; esac +done +gpu=("${BASH_SOURCE[0]%/*}/droiddeck-game-gpu") +location= +[ -n "$app" ] && location=$(flatpak info --show-location "$app" 2>/dev/null) +[ -n "$location" ] && gpu+=(--for "$location/files") echo "== flatpak run ${socket[*]} $*" if [ -z "${DBUS_SESSION_BUS_ADDRESS:-}" ] && command -v dbus-run-session >/dev/null 2>&1; then - exec dbus-run-session -- flatpak run "${socket[@]}" "$@" + exec "${gpu[@]}" -- dbus-run-session -- flatpak run "${socket[@]}" "$@" fi -exec flatpak run "${socket[@]}" "$@" +exec "${gpu[@]}" -- flatpak run "${socket[@]}" "$@" diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-game-env b/tools/linuxfs/overlay/usr/local/bin/droiddeck-game-env index 9fb3c39c66..175b8d8d1c 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-game-env +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-game-env @@ -11,22 +11,43 @@ import tempfile from pathlib import Path GAME_TU_DEBUG = "DROIDDECK_GAME_TU_DEBUG" +CHROMIUM_FILES = {"chrome-sandbox", "chrome_100_percent.pak", "v8_context_snapshot.bin"} SYNC_KEYS = ("BL_SYNC", "BL_SYNC_FALLBACK", "BL_FSYNC_FIRST", "BL_SYNC_MIN_NOFILE", "PROTON_NO_ESYNC", "PROTON_NO_FSYNC", "PROTON_NO_NTSYNC", "WINEESYNC") SEED_REDISTS = "/usr/local/bin/droiddeck-seed-redists" PACK_KEYS = ("WINESERVER", "BL_SYNC_PACK", "WINEESYNC", "PROTON_NO_ESYNC", "PROTON_NO_FSYNC") +# Prefixes several games share; the game itself is the Steam app being launched. +SHARED_PREFIXES = {"2929927072"} + + +def app_number(value): + if not re.fullmatch(r"-?[0-9]+", value or ""): + return None + number = int(value) + if not -(2**31) <= number < 2**32 or number == 0: + return None + return str(number % 2**32) + + def game_id(env): prefix = env.get("STEAM_COMPAT_DATA_PATH", "").rstrip("/") if not prefix: return None - value = prefix.rsplit("/", 1)[-1] - if not re.fullmatch(r"-?[0-9]+", value): + found = app_number(prefix.rsplit("/", 1)[-1]) + if found in SHARED_PREFIXES: + return app_number(env.get("SteamAppId", "")) or shortcut_number(env.get("SteamGameId", "")) or found + return found + + +def shortcut_number(value): + """The shortcut appid inside Steam's 64-bit shortcut game id `(appid << 32) | 0x02000000`.""" + if not re.fullmatch(r"[0-9]{1,20}", value or ""): return None number = int(value) - if not -(2**31) <= number < 2**32 or number == 0: + if number >= 2**64 or number & 0xFFFFFFFF != 0x02000000: return None - return str(number % 2**32) + return app_number(str(number >> 32)) DOTNET_FEX = { @@ -332,12 +353,12 @@ def engine_fixes(exe, args): return env, extra, notes -def win_components(compat, app_id): +def win_components(compat, app_id, proton=None, env=None): if not compat: return "" try: module = runpy.run_path(str(Path(__file__).resolve().parent / "droiddeck-wincomponents")) - names, notes = module["apply"](compat.rstrip("/"), app_id) + names, notes = module["apply"](compat.rstrip("/"), app_id, proton=proton, env=env or os.environ) for note in notes: print("droiddeck-game-env: " + note, file=sys.stderr) return module["overrides_value"](names) @@ -346,11 +367,23 @@ def win_components(compat, app_id): return "" -def game_gpu(env): +def chromium_app(folder): + root = os.path.realpath(folder) + for top, dirs, files in os.walk(root): + if CHROMIUM_FILES.intersection(files) or CHROMIUM_FILES.intersection(dirs): + return True + if os.path.relpath(top, root).count(os.sep) >= 2: + dirs[:] = [] + return False + + +def game_gpu(env, keep=False): if GAME_TU_DEBUG not in env: return env result = env.copy() value = result.pop(GAME_TU_DEBUG) + if keep: + return result if value: result["TU_DEBUG"] = value else: @@ -846,6 +879,60 @@ WINE_DLL_DIRS = ("/lib/wine/i386-windows", "/lib/wine/x86_64-windows", "/lib/win "/lib/wine/fakedlls", "/lib64/wine", "/lib64/wine/fakedlls", "/lib64/wine/x86_64-windows") +FOREIGN_SUFFIX = ".foreign-arch" +PE = runpy.run_path(str(Path(__file__).resolve().parent / "droiddeck-wine-pe")) +pe_machine = PE["pe_machine"] + + +def repair_foreign_system_dlls(prefix, proton, env=None): + dist = Path(proton).parent / "files/lib/wine" + windows = Path(prefix) / "pfx/drive_c/windows" + machines_by_folder = PE["proton_pe_machines"](proton, env or {}) + if machines_by_folder is None: + return [] + repaired = [] + for folder, (libdir, machines) in machines_by_folder.items(): + builtins = dist / libdir + try: + builtin_names = {name.lower(): name for name in os.listdir(builtins)} + except OSError: + continue + try: + names = sorted(os.listdir(windows / folder)) + except OSError: + continue + for name in names: + path = windows / folder / name + builtin_name = builtin_names.get(name.lower()) + source = builtins / builtin_name if builtin_name else builtins / name + if path.is_symlink() or not name.lower().endswith(".dll") or not source.is_file() or not path.is_file(): + continue + machine = pe_machine(path) + if machine is None or PE["machine_compatible"](path, machines, machine): + continue + link = windows / folder / (name + ".bl-new") + backup = windows / folder / (name + FOREIGN_SUFFIX) + serial = 1 + while os.path.lexists(backup): + backup = windows / folder / (name + FOREIGN_SUFFIX + ".%d" % serial) + serial += 1 + try: + os.symlink(os.path.relpath(source, path.parent), link) + os.replace(path, backup) + try: + os.replace(link, path) + except OSError: + if not os.path.lexists(path) and os.path.lexists(backup): + os.replace(backup, path) + raise + except OSError: + if link.is_symlink(): + link.unlink() + continue + repaired.append("%s/%s (machine 0x%x)" % (folder, name, machine)) + return repaired + + def is_wine_dll_link(path): try: return os.path.islink(path) and os.path.dirname(os.readlink(path)).endswith(WINE_DLL_DIRS) @@ -1143,16 +1230,54 @@ def main(): env = game_gpu(os.environ.copy()) mic_enabled = env.get("BL_MIC_ENABLED") debug_dir = env.get("BL_DEBUG_DIR", "") + prefix_path = env.get("STEAM_COMPAT_DATA_PATH", "").rstrip("/") + app_id = game_id(env) + components = "" + waiting = bool(app_id and len(command) > 1 and command[1] == "waitforexitandrun") + config = {} + launch_config = None + launch_config_valid = True + effective_env = env + if waiting: + path = Path.home() / ".config/droiddeck/game-environment.json" + try: + with path.open(encoding="utf-8") as source: + config = json.load(source) + launch_config = config + except FileNotFoundError: + launch_config = {"version": 1} + except (OSError, ValueError, TypeError, AttributeError): + launch_config_valid = False + if launch_config_valid: + try: + effective_env = apply_config(env, launch_config, app_id) + except (OSError, ValueError, TypeError, AttributeError): + launch_config_valid = False + if not launch_config_valid: + print("droiddeck-game-env: invalid configuration; using inherited environment", file=sys.stderr) + if waiting: + components = win_components(prefix_path, app_id, command[0], effective_env) + if prefix_path and command: + try: + fixed = repair_foreign_system_dlls(prefix_path, command[0], effective_env) + except OSError as error: + fixed = [] + print("droiddeck-game-env: prefix system DLL check skipped (%s)" % error, file=sys.stderr) + if fixed: + print("droiddeck-game-env: replaced foreign-architecture system DLLs with this Proton's own: %s" % ", ".join(fixed), + file=sys.stderr) scripts = mark_install_scripts(command, env) if scripts and install_scripts_done(scripts, env): print("droiddeck-game-env: install scripts already run in this prefix; evaluator skipped", file=sys.stderr) return 0 + if scripts and not env.get("DBUS_SESSION_BUS_ADDRESS"): + # libdbus would autolaunch a session bus under the evaluator's reaper, which then waits for it + # forever: Steam keeps seeing the app as running and cannot move or update it + env["DBUS_SESSION_BUS_ADDRESS"] = "disabled:" try: digest = scripts_digest(scripts) if scripts else None except OSError: digest = None - app_id = game_id(env) - waiting = bool(app_id and len(command) > 1 and command[1] == "waitforexitandrun") diagnostic_app_id = app_id if waiting and os.environ.get("DROIDDECK_ENHANCED_DIAGNOSTICS") == "1" else None dirt_prefix = None if app_id: @@ -1170,7 +1295,6 @@ def main(): for note in notes: print("droiddeck-game-env: " + note, file=sys.stderr) command += extra - components = win_components(env.get("STEAM_COMPAT_DATA_PATH", ""), app_id) if components: own = fixes.get("WINEDLLOVERRIDES") fixes["WINEDLLOVERRIDES"] = components + ";" + own if own else components @@ -1178,22 +1302,21 @@ def main(): if dirt_prefix: own = fixes.get("WINEDLLOVERRIDES") fixes["WINEDLLOVERRIDES"] = own + ";ws2_32=n,b" if own else "ws2_32=n,b" - path = Path.home() / ".config/droiddeck/game-environment.json" - config = {} - try: - with path.open(encoding="utf-8") as source: - config = json.load(source) - if waiting: + if waiting: + if launch_config_valid: if "vblank_mode" in config.get("shared", {}): fixes.pop("vblank_mode", None) - env = apply_config(env, config, app_id, fixes) - else: + env = apply_config(env, launch_config, app_id, fixes) + else: + path = Path.home() / ".config/droiddeck/game-environment.json" + try: + with path.open(encoding="utf-8") as source: + config = json.load(source) env = apply_config(env, sync_config(config, app_id), app_id) - except FileNotFoundError: - if waiting: - env = apply_config(env, {"version": 1}, app_id, fixes) - except (OSError, ValueError, TypeError, AttributeError): - print("droiddeck-game-env: invalid configuration; using inherited environment", file=sys.stderr) + except FileNotFoundError: + pass + except (OSError, ValueError, TypeError, AttributeError): + print("droiddeck-game-env: invalid configuration; using inherited environment", file=sys.stderr) if waiting: before_debug = env env = apply_game_debug_logs(env, config, debug_dir) diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-game-gpu b/tools/linuxfs/overlay/usr/local/bin/droiddeck-game-gpu new file mode 100755 index 0000000000..40055010d0 --- /dev/null +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-game-gpu @@ -0,0 +1,42 @@ +#!/usr/bin/python3 +import os +import runpy +import sys + +BIN_DIR = os.path.dirname(os.path.abspath(__file__)) + + +def main(argv): + keep = False + folder = None + while argv and argv[0] != "--": + if argv[0] == "--chromium": + keep = True + argv = argv[1:] + elif argv[0] == "--for" and len(argv) > 1: + folder = argv[1] + argv = argv[2:] + else: + break + if argv and argv[0] == "--": + argv = argv[1:] + if not argv: + print("usage: droiddeck-game-gpu [--chromium] [--for FOLDER] -- PROGRAM [ARGS...]", file=sys.stderr) + return 2 + env = dict(os.environ) + try: + game_env = runpy.run_path(os.path.join(BIN_DIR, "droiddeck-game-env")) + if folder and not keep: + keep = game_env["chromium_app"](folder) + env = game_env["game_gpu"](env, keep) + except Exception as error: + print("droiddeck-game-gpu: the game GPU setting was not applied (%s)" % error, file=sys.stderr) + try: + os.execvpe(argv[0], argv, env) + except OSError as error: + print("droiddeck-game-gpu: %s: %s" % (argv[0], error.strerror), file=sys.stderr) + return 127 + + +if __name__ == "__main__": + sys.exit(main(sys.argv[1:])) diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-open b/tools/linuxfs/overlay/usr/local/bin/droiddeck-open index 45f806297c..bf0aaff7c1 100644 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-open +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-open @@ -370,7 +370,7 @@ def main(argv): what = "steam" elif what == "script" and for_x86(path): what = "x86 script" - elif what in ("script", "elf") and game_folder(path): + elif what in ("script", "elf") and not (mode == "exec" and machine(head(path)) in EMULATED) and game_folder(path): what = "folder" try: cmd, folder = command(path, argv[1:], what, mode == "window", appid) @@ -381,10 +381,11 @@ def main(argv): cmd = placed(cmd, what, mode, os.environ) if mode != "exec" or not sys.stderr.isatty(): say("%s (%s): %s" % (path, what, " ".join(cmd[:4]))) - try: - os.chdir(folder) - except OSError: - pass + if not (mode == "exec" and what in ("elf", "script", "x86 script")): + try: + os.chdir(folder) + except OSError: + pass if mode == "exec": os.execv(cmd[0], cmd) started = time.monotonic() diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-proton-run b/tools/linuxfs/overlay/usr/local/bin/droiddeck-proton-run index 237457d947..164dbd0288 100644 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-proton-run +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-proton-run @@ -70,8 +70,19 @@ def mapping(root): def tools(root): + c = compat() + wrapper = os.path.join(root, "compatibilitytools.d", c["TOOL"]) + complete = os.access(os.path.join(wrapper, c["LAUNCHER"]), os.X_OK) and os.path.isfile(os.path.join(wrapper, "toolmanifest.vdf")) + if c["TOOL"].lower() not in c["local_tools"](root) or not complete: + depot = c["find_source"](root, runnable=True) + if depot is not None: + try: + c["build_tool"](wrapper, depot) + except OSError: + pass found = {} - for base in [os.path.join(root, "compatibilitytools.d")] + list(SYSTEM_TOOLS): + bases = [os.path.join(root, "compatibilitytools.d")] + list(SYSTEM_TOOLS) + for base in bases: try: entries = sorted(os.listdir(base)) except OSError: @@ -98,13 +109,25 @@ def state_default(): return None +def newest_own(available): + c = compat() + builds = c["own_builds"]([{"name": name, "dir": os.path.basename(folder)} for name, (folder, _) in available.items()]) + return max(builds, key=lambda t: (c["build_key"](t["dir"]), t["dir"]))["name"] if builds else None + + def choose_tool(appid, current, available): + c = compat() + newest = newest_own(available) for key, why in ((appid, "Steam's choice for this title"), ("0", "Steam's default")): name = current.get(key) if name in available: return name, why - c = compat() - for name in (state_default(), c["TOOL"], c["TOOL_11"]): + if newest and isinstance(name, str) and name.startswith(c["OWN_PROTON"]): + return newest, "%s, replacing the removed %s" % (why, name) + default = state_default() + if newest and isinstance(default, str) and default.startswith(c["OWN_PROTON"]) and default not in available: + return newest, "the default, replacing the removed %s" % default + for name in (default, c["TOOL"], c["TOOL_11"]): if name in available: return name, "the default ARM64 Proton" return (sorted(available)[0], "the only one installed") if available else (None, None) diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-script-run b/tools/linuxfs/overlay/usr/local/bin/droiddeck-script-run index 55606d8c5a..3fe50f5d35 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-script-run +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-script-run @@ -16,6 +16,7 @@ run=("${interp[@]}" "./$entry") if [ -x /usr/local/bin/droiddeck-fex ]; then run=(/usr/local/bin/droiddeck-fex run --mode "$fex" --for "$folder" -- "${interp[@]}" "./$entry") fi +if [ "$fex" = off ]; then unset BL_BINFMT; else export BL_BINFMT=1; fi echo "== script $entry in $folder (${interp[*]:-a program}, FEX $fex)" case ${DBUS_SESSION_BUS_ADDRESS:-} in unix:*) "${run[@]}" "$@" ;; diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-session b/tools/linuxfs/overlay/usr/local/bin/droiddeck-session index c468dbf5f4..df301aadaf 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-session +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-session @@ -56,6 +56,16 @@ sync_timezone() { } 2>/dev/null sync_timezone +ensure_machine_id() { + local id= + id=$(< /etc/machine-id) + [[ $id =~ ^[0-9a-f]{32}$ && $id != 00000000000000000000000000000000 ]] && return 0 + id=$(dbus-uuidgen) || return 0 + [[ $id =~ ^[0-9a-f]{32}$ ]] || return 0 + printf '%s\n' "$id" > /etc/machine-id.droiddeck && mv -f /etc/machine-id.droiddeck /etc/machine-id +} 2>/dev/null +ensure_machine_id + write_system_bus_config() { mkdir -p /run/dbus /usr/local/share/droiddeck cat > /usr/local/share/droiddeck/system-bus.conf <<'BUSCONF' @@ -642,13 +652,7 @@ case $mode in fi case ${target,,} in *.appimage) - export APPIMAGE_EXTRACT_AND_RUN=1 - chmod +x "$target" 2>/dev/null - if [ ${#fex[@]} -eq 0 ] && [ -x /usr/local/lib/droiddeck/uruntime ]; then - export TARGET_APPIMAGE=$target - run_program /usr/local/lib/droiddeck/uruntime "$@" - fi - run_program "${fex[@]}" "$target" "$@" + run_program /usr/local/bin/droiddeck-appimage-exec "$target" "$@" ;; *.sh) if [ -x /usr/local/bin/droiddeck-fex ]; then @@ -898,21 +902,21 @@ case $mode in bl_session_pressure() { bl_memory_read [ "$_bl_game" = 1 ] && bl_low_ram && bl_memory_critical || return 0 - local _wait=$_bl_tier_backoff _urgent=0 _before _pids _targets - [ "$_bl_available" -ge $((_bl_headroom / 2)) ] || _urgent=1 _wait=30 + local _wait=$_bl_tier_backoff _before _pids _targets + [ "$_bl_available" -ge $((_bl_headroom / 2)) ] || [ "$_bl_tier_pushed" -lt 0 ] || _wait=30 [ "$_bl_tier_at" -lt 0 ] || [ $((SECONDS - _bl_tier_at)) -ge "$_wait" ] || return 0 if [ "$_bl_tier_at" -ge 0 ] && [ "$_bl_tier_pushed" -gt 0 ]; then bl_tier_readback if [ "$_bl_tier_back" -le $((_bl_tier_pushed / 2)) ]; then _bl_tier_backoff=30 - elif [ "$_urgent" = 0 ]; then + else _bl_tier_backoff=$((_bl_tier_backoff * 2 > 300 ? 300 : _bl_tier_backoff * 2)) - _bl_tier_at=$SECONDS _bl_tier_pushed=0 + _bl_tier_at=$SECONDS _bl_tier_pushed=-1 step "zRAM: the game read back most of its last page-out, next try in ${_bl_tier_backoff} s" return 0 fi fi - _bl_tier_at=$SECONDS + _bl_tier_at=$SECONDS _bl_tier_pushed=0 bl_zram_pick game bl_zram_usage "${_pids[@]}" > /dev/null [ "$_bl_rss" -ge $((256 * 1024)) ] || return 0 @@ -1240,7 +1244,14 @@ case $mode in sleep 1 done [ "${#game}" -gt 10 ] && shortcut=1 - [ -n "$shortcut" ] || sleep 4 + _bl_ready_end=$((SECONDS + 30)) + while [ "$waited" -lt 30 ] && [ "$((_bl_ready_end - SECONDS))" -gt 0 ]; do + timeout "$((_bl_ready_end - SECONDS))" /usr/local/bin/droiddeck-agent --steam-ready >/dev/null 2>&1 && break + [ "$((_bl_ready_end - SECONDS))" -gt 1 ] || break + sleep 1 + waited=$((waited + 1)) + done + waited=0 echo "== steam: interface up; launching the requested game $game" _bl_hand_off [ -n "$shortcut" ] || return 0 @@ -1260,7 +1271,9 @@ case $mode in elif _bl_starting; then echo "== steam: the requested game started while waiting; not launching again" elif [ "$(_bl_focused)" = 769 ]; then - echo "== steam: launching the requested game again $game (overview status $status)" + echo "== steam: clearing the pending launch interstitial for $game (overview status $status)" + /usr/local/bin/droiddeck-agent --steam-clear-pending-interstitial "$(((game >> 32) & 0xffffffff))" >/dev/null 2>&1 && return 0 + echo "== steam: launching the requested game again $game" _bl_hand_off else echo "== steam: the game took focus while waiting; not launching again" @@ -1334,6 +1347,23 @@ case $mode in echo "== network: no dbus-daemon or netmanager in this runtime; the client will show no connection" exit 69 fi + # Without a session bus libdbus autolaunches one under the game; it never exits and Steam's + # reaper waits on it, so the launch or exit never finishes. + if [ -z "${DBUS_SESSION_BUS_ADDRESS:-}" ]; then + _bl_bus=${XDG_RUNTIME_DIR:-/run/droiddeck}/bus + rm -f "$_bl_bus" + dbus-daemon --session --address="unix:path=$_bl_bus" --nofork --nopidfile & + for _ in $(seq 1 50); do + [ -S "$_bl_bus" ] && break + nap 0.1 + done + if [ -S "$_bl_bus" ]; then + export DBUS_SESSION_BUS_ADDRESS="unix:path=$_bl_bus" + echo "== session bus up at $_bl_bus" + else + echo "== session bus did not come up; programs may start their own" + fi + fi _bl_compat_env=(env -u LD_LIBRARY_PATH -u STEAM_RUNTIME -u STEAM_STEAMRT_RECURSION_GUARD) [ "${LD_LIBRARY_PATH+x}" ] && _bl_compat_env+=("LD_LIBRARY_PATH=$LD_LIBRARY_PATH") diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-snap-run b/tools/linuxfs/overlay/usr/local/bin/droiddeck-snap-run index 6529005a24..9e8acf9cc8 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-snap-run +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-snap-run @@ -1,11 +1,13 @@ #!/usr/bin/python3 import os import re +import runpy import shlex import sys import yaml +BIN_DIR = os.path.dirname(os.path.abspath(__file__)) SNAP_DIR = "/snap" HOST_DIRS = ("/etc", "/home", "/root", "/tmp", "/run", "/var/snap", "/var/lib/snapd", "/var/tmp", "/var/log", "/media", "/mnt", "/snap", "/usr/src", "/lib/modules") @@ -59,6 +61,15 @@ def content_binds(name, meta, env): return out +def game_gpu(env, snap): + try: + game_env = runpy.run_path(os.path.join(BIN_DIR, "droiddeck-game-env")) + return game_env["game_gpu"](env, game_env["chromium_app"](snap)) + except Exception as error: + print(f"droiddeck-snap-run: the game GPU setting was not applied ({error})", file=sys.stderr) + return env + + def main(argv): if len(argv) < 3: print("usage: droiddeck-snap-run SNAP APP [ARG...]", file=sys.stderr) @@ -72,7 +83,7 @@ def main(argv): revision = os.path.basename(os.path.realpath(f"{SNAP_DIR}/{name}/current")) home = os.environ.get("HOME", "/root") snap = f"{SNAP_DIR}/{name}/{revision}" - env = dict(os.environ) + env = game_gpu(dict(os.environ), snap) for k in ("LD_PRELOAD", "LD_LIBRARY_PATH"): env.pop(k, None) env.update({ diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-steam-compat b/tools/linuxfs/overlay/usr/local/bin/droiddeck-steam-compat index 97e2b8525b..e96ce1c553 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-steam-compat +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-steam-compat @@ -760,18 +760,50 @@ class Watcher: def __init__(self, files): self.files = files self.fd = -1 + self.libc = None + wanted = {os.path.dirname(f) for f in files} + self.missing = {d for d in wanted if not os.path.isdir(d)} + self.watched = set() try: libc = ctypes.CDLL(None, use_errno=True) fd = libc.inotify_init1(os.O_NONBLOCK | os.O_CLOEXEC) - dirs = sorted({os.path.dirname(f) for f in files if os.path.isdir(os.path.dirname(f))}) + dirs = sorted((wanted - self.missing) | {self.existing_parent(d) for d in self.missing} - {""}) if fd >= 0 and dirs and all(libc.inotify_add_watch(fd, d.encode(), self.MASK) >= 0 for d in dirs): + self.watched = set(dirs) self.fd = fd + self.libc = libc elif fd >= 0: os.close(fd) except (OSError, AttributeError): self.fd = -1 self.stamp = self.snapshot() + @staticmethod + def existing_parent(path): + while path and not os.path.isdir(path): + parent = os.path.dirname(path) + if parent == path: + return "" + path = parent + return path + + def watch_new_dirs(self): + appeared = False + progressed = True + while progressed: + progressed = False + for d in sorted(self.missing): + parent = d if os.path.isdir(d) else self.existing_parent(d) + if not parent or parent in self.watched: + continue + if self.libc.inotify_add_watch(self.fd, parent.encode(), self.MASK) >= 0: + self.watched.add(parent) + progressed = True + appeared = appeared or parent == d + for d in [d for d in self.missing if d in self.watched]: + self.missing.discard(d) + return appeared + def snapshot(self): stamp = [] for f in self.files: @@ -803,8 +835,9 @@ class Watcher: at += 16 + size except BlockingIOError: pass + appeared = self.watch_new_dirs() if self.missing else False wanted = {os.path.basename(f) for f in self.files} - return any(n in wanted or n.startswith("appmanifest_") for n in names) + return appeared or any(n in wanted or n.startswith("appmanifest_") for n in names) class XMessage(ctypes.Structure): @@ -1078,10 +1111,13 @@ class Helper: wanted = COMPAT["requested_tool"](request, catalog) newer = isinstance(request.get("seq"), int) and request["seq"] > int(state.get("applied", 0) or 0) waiting = newer and wanted is not None and wanted not in self.known - default, state = COMPAT["choose_default"](top["selected"], state, request, live, apply_request=not waiting) + listed = [t["name"] for t in live] + default, state = COMPAT["choose_default"](COMPAT["built_from"](top["selected"], catalog, listed) or top["selected"], + state, request, catalog, apply_request=not waiting, listed=listed) installed = COMPAT["installed_apps"](self.steam_root) self.installed = installed picks = COMPAT["valve_picks"](self.steam_root) + tools = COMPAT["tool_platforms"](self.steam_root) prior_auto = state.get("auto", {}) if not isinstance(prior_auto, dict): prior_auto = {} @@ -1089,7 +1125,7 @@ class Helper: native_choices = set(prior_native if isinstance(prior_native, (list, tuple, set)) else ()) picked = COMPAT["picked_apps"](state) runnable = {t["name"].lower() for t in live} - apps = sorted(set(installed) | set(picks) | set(prior_auto) | native_choices, key=COMPAT["app_order"]) + apps = sorted(set(installed) | set(picks) | set(prior_auto) | native_choices | set(tools), key=COMPAT["app_order"]) asked = [app for app, request in COMPAT["game_requests"]().items() if COMPAT["valid_game_request"](app, request)] apps = sorted(set(apps) | set(asked), key=COMPAT["app_order"]) current = dict(page.compat("many(%s)" % json.dumps(apps)) or {}) @@ -1103,6 +1139,7 @@ class Helper: if isinstance(state.get("auto"), dict): state["auto"] = prior_auto native_choices -= refused + tools = {app: platforms for app, platforms in tools.items() if app not in refused} current["0"] = top["selected"] native = COMPAT["native_targets"](self.steam_root, apps) raw_mapping = COMPAT["read_config_mapping"]( @@ -1123,13 +1160,20 @@ class Helper: current[app] = "" elif not current.get(app): current.pop(app, None) + moved = COMPAT["rebuilt_picks"](current, catalog, set(prior_auto) | picked, listed) + if moved: + following = COMPAT["followers"](state, current) + state["auto"] = {app: name for app, name in following.items() if app not in moved} if isinstance(following, dict) else {} + prior_auto = {app: name for app, name in prior_auto.items() if app not in moved} + current.update(moved) requests = COMPAT["settled_game_requests"](state, current, catalog) - overrides = COMPAT["requested_games"](requests or {}, catalog, [t["name"] for t in live]) + overrides = COMPAT["requested_games"](requests or {}, catalog, listed) overrides = {app: tool for app, tool in overrides.items() if app not in refused} changes, auto = COMPAT["plan_mapping"]( - current, installed, [t["name"] for t in live], default, + current, installed, listed, default, COMPAT["followers"](state, current), native, picks, overrides, - prior_auto.keys(), native_choices) + prior_auto.keys(), native_choices, tools) + changes = dict(moved, **changes) for app, tool in sorted(changes.items(), key=lambda item: COMPAT["app_order"](item[0])): page.compat("set(%d, %s)" % (int(app), json.dumps(tool if tool is not None else ""))) say("%s -> %s" % ("default" if app == "0" else "app %s" % app, @@ -1137,7 +1181,7 @@ class Helper: state.update( auto={app: tool for app, tool in auto.items() if tool is not COMPAT["NATIVE_CHOICE"]}, native_choices=sorted(refused_native | {app for app, tool in auto.items() if tool is COMPAT["NATIVE_CHOICE"]}), - picked=COMPAT["picked_after"](current, changes), tools=catalog, live=True, + picked=COMPAT["picked_after"](current, changes), tools=catalog, live=True, written=default, games_applied=COMPAT["applied_game_requests"](requests, overrides, state)) state.pop("native", None) if waiting: @@ -1292,7 +1336,8 @@ def main(): failures += 1 time.sleep(min(2 * max(failures, 1), 10)) os.makedirs(COMPAT["COMPAT_DIR"], exist_ok=True) - watcher = Watcher([COMPAT["state_path"]("request.json"), COMPAT["state_path"]("games.json")] + + watcher = Watcher([COMPAT["state_path"]("request.json"), COMPAT["state_path"]("games.json"), + os.path.join(steam_root, "appcache", "appinfo.vdf")] + [os.path.join(lib, "steamapps", "libraryfolders.vdf") for lib in (steam_root, COMPAT["LIBRARY"] + "-sd")]) steamos = os.environ.get("BL_STEAMDECK") == "1" and os.environ.get("BL_STEAM_UI") != "desktop" helper = Helper(steam_root, port, steamos) diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-steam-shortcuts b/tools/linuxfs/overlay/usr/local/bin/droiddeck-steam-shortcuts index 9ad14d8385..5e37501b35 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-steam-shortcuts +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-steam-shortcuts @@ -355,8 +355,9 @@ def main(): "linux_tool": "droiddeck-direct"} else: protect = compat.adopt_extras(tools) - used = compat.register_default(cfg, apps=[str(int(g["appid"]) & 0xFFFFFFFF) for g in games if g.get("exe") and not g.get("native")] + [str(appid) for appid in set(routes.values())], - protect=protect, **own) + owned = [str(int(g["appid"]) & 0xFFFFFFFF) for g in games if g.get("exe") and not g.get("native")] + used = compat.register_default(cfg, apps=owned + [str(appid) for appid in set(routes.values())], + protect=protect, owned=owned, **own) print("droiddeck-steam-shortcuts: mapped them to %s" % used) except Exception as e: print("droiddeck-steam-shortcuts: compat mapping skipped (%s)" % e) diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-store-launch b/tools/linuxfs/overlay/usr/local/bin/droiddeck-store-launch index 131c049062..ca6ddebc84 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-store-launch +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-store-launch @@ -5,6 +5,8 @@ import binascii import json import os import re +import shlex +import shutil import sys import time import subprocess @@ -275,6 +277,10 @@ def exited(argv, code=0): subprocess.run([EPIC_CLIENT, "exited", "--code", str(code)], stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, timeout=10) except (OSError, subprocess.TimeoutExpired): pass + if found is not None and launch_dir and str(found[1].get("store")) == "ea": + ask(Path(launch_dir) / "stores", {"op": "ea-exited", "store": "ea", "id": str(found[1].get("id"))}, timeout=1.0) + log("ea %s exited" % found[1].get("id")) + return 0 if found is None or not launch_dir or not cloud_on(found[1]): return 0 store, game = str(found[1].get("store")), str(found[1].get("id")) @@ -283,6 +289,218 @@ def exited(argv, code=0): return 0 +UBISOFT_DIR = "drive_c/Program Files (x86)/Ubisoft/Ubisoft Game Launcher" +UBISOFT_WINDOWS_DIR = "C:\\Program Files (x86)\\Ubisoft\\Ubisoft Game Launcher\\" +UBISOFT_INSTALLER_URL = "https://static3.cdn.ubi.com/orbit/launcher_installer/UbisoftConnectInstaller.exe" +UBISOFT_CACHE = Path("/root/.cache/droiddeck/ubisoft") +UBISOFT_LAUNCH = ".droiddeck-ubisoft.json" +UBISOFT_SERVICE = Path("/usr/local/lib/droiddeck-ubisoft/droiddeck-upc.exe") +UBISOFT_ASK_WAIT = 45.0 +UBISOFT_EXTRACT_WAIT = 300 +# Ubisoft's own game-side runtime from the Connect installer, unmodified: the API and aux DLLs the +# game's loader opens from InstallDir, the starter it re-executes into and its imports, and the overlay +# DLLs uplay_r1.dll loads even with the overlay off (its startup fails without them). +UBISOFT_RUNTIME = ("uplay_r1.dll", "uplay_r164.dll", "uplay_aux_r1.dll", "uplay_aux_r164.dll", + "uplay_r2.dll", "uplay_r264.dll", "ubiorbitapi_r2.dll", "ubiorbitapi_r264.dll", + "UbisoftGameLauncher.exe", "UbisoftGameLauncher64.exe", "discord-rpc.x86.dll", + "discord-rpc.x64.dll", "steam_api.dll", "steam_api64.dll", "overlay.dll", "overlay64.dll") +UBISOFT_VIEWS = ("[Software\\\\Ubisoft\\\\Launcher%s]", "[Software\\\\Wow6432Node\\\\Ubisoft\\\\Launcher%s]") +MIN_INSTALLER = 1 << 20 + + +def ubisoft_runtime_dir(compat_data): + return Path(compat_data) / "pfx" / UBISOFT_DIR + + +def ubisoft_runtime_ready(compat_data): + folder = ubisoft_runtime_dir(compat_data) + return all((folder / name).is_file() for name in UBISOFT_RUNTIME) + + +def fetch_installer(url=UBISOFT_INSTALLER_URL, folder=UBISOFT_CACHE): + target = folder / "UbisoftConnectInstaller.exe" + if target.is_file() and target.stat().st_size >= MIN_INSTALLER: + return target + import urllib.request + folder.mkdir(parents=True, exist_ok=True) + part = target.with_name(target.name + ".part") + request = urllib.request.Request(url, headers={"User-Agent": "Mozilla/5.0"}) + with urllib.request.urlopen(request, timeout=60) as reply, open(part, "wb") as out: + shutil.copyfileobj(reply, out) + if part.stat().st_size < MIN_INSTALLER: + part.unlink() + raise OSError("installer too small") + os.replace(part, target) + return target + + +def install_runtime(compat_data, wait=UBISOFT_EXTRACT_WAIT): + if ubisoft_runtime_ready(compat_data): + return "present" + if not (Path(compat_data) / "pfx").is_dir(): + return "no-prefix" + try: + installer = fetch_installer() + except OSError as error: + return "download-failed-" + type(error).__name__ + target = ubisoft_runtime_dir(compat_data) + target.mkdir(parents=True, exist_ok=True) + with tempfile.TemporaryDirectory(prefix="droiddeck-ubisoft-", dir=str(UBISOFT_CACHE)) as tmp: + try: + subprocess.run(["7z", "e", "-y", "-o" + tmp, str(installer)] + list(UBISOFT_RUNTIME), stdin=subprocess.DEVNULL, + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=wait) + except subprocess.TimeoutExpired: + return "extract-timeout" + except OSError: + return "no-7z" + missing = [name for name in UBISOFT_RUNTIME if not (Path(tmp) / name).is_file()] + if missing: + return "extract-missing-" + ",".join(missing) + for name in UBISOFT_RUNTIME: + shutil.copyfile(Path(tmp) / name, target / name) + if not ubisoft_runtime_ready(compat_data): + return "copy-failed" + try: + installer.unlink() + except OSError: + pass + return "installed" + + +def ubisoft_product(sidecar): + extra = sidecar.get("extra") if isinstance(sidecar.get("extra"), dict) else {} + try: + return int(str(extra.get("productId") or "")) + except ValueError: + return None + + +def ubisoft_install_dir(folder): + return windows_path(str(folder)).replace("\\", "/").rstrip("/") + "/" + + +def register_ubisoft(folder, product, compat_data): + reg = Path(compat_data) / "pfx" / "system.reg" + if not reg.is_file(): + return "no-registry" + if not wait_for_server(reg.parent): + return "server-running" + for view in UBISOFT_VIEWS: + set_value(reg, view % "", "InstallDir", UBISOFT_WINDOWS_DIR) + set_value(reg, view % ("\\\\Installs\\\\%d" % product), "InstallDir", ubisoft_install_dir(folder)) + return "registered %d" % product + + +UBISOFT_HIVES = {"HKEY_LOCAL_MACHINE": "system.reg", "HKEY_CURRENT_USER": "user.reg"} + + +def manifest_value(values, folder, language): + """The value Connect writes for a manifest registry entry: the install folder, the entry for the + language being played, or the only entry there is.""" + entries = [v for v in values if isinstance(v, dict)] + if not entries: + return None + names = [str(v.get("name") or "") for v in entries] + if "$INSTALL_DIR" in names: + return windows_path(str(folder)).rstrip("\\") + "\\" + for v in entries: + if str(v.get("value") or "").lower() == language.lower(): + return str(v.get("value")) + prefix = language.split("-")[0].lower() + for v in entries: + if str(v.get("value") or "").lower().split("-")[0] == prefix: + return str(v.get("value")) + return str(entries[0].get("value") or entries[0].get("name") or "") + + +def apply_manifest_registry(compat_data, registry, folder, language): + written = 0 + for item in registry or []: + if not isinstance(item, dict): + continue + path = str(item.get("key") or "").replace("/", "\\") + hive, _, rest = path.partition("\\") + target = UBISOFT_HIVES.get(hive.upper()) + if not target or "\\" not in rest: + continue + subkey, _, name = rest.rpartition("\\") + value = manifest_value(item.get("values"), folder, language) + if value is None or not name: + continue + reg = Path(compat_data) / "pfx" / target + parts = subkey.split("\\") + views = [parts] + if target == "system.reg" and len(parts) > 1 and parts[0].lower() == "software" and parts[1].lower() != "wow6432node": + views.append(parts[:1] + ["Wow6432Node"] + parts[1:]) + for view in views: + key = "[" + "\\\\".join(view) + "]" + if set_value(reg, key, name, value): + written += 1 + return written + + +def ubisoft_launch(folder, sidecar, product, compat_data, answer): + """The launch file droiddeck-upc.exe reads: the game, the account and this launch's ticket.""" + account = answer.get("account") if isinstance(answer.get("account"), dict) else {} + user = str(account.get("userId") or "user") + saves = Path(compat_data) / "pfx" / "drive_c/users/steamuser/Saved Games/Ubisoft" / user / str(product) + saves.mkdir(parents=True, exist_ok=True) + exe = str(sidecar.get("exe") or "").replace("\\", "/") + workdir = folder / str(sidecar.get("workdir")) if sidecar.get("workdir") else folder + UBISOFT_CACHE.mkdir(parents=True, exist_ok=True) + return { + "productId": product, + "exe": windows_path(str(folder / exe)), + "args": " ".join(str(a) for a in sidecar.get("args") or []), + "workdir": windows_path(str(workdir)), + "account": account, + "ticket": str(answer.get("ticket") or ""), + "owned": [int(i) for i in answer.get("owned") or [] if str(i).isdigit()], + "products": [p for p in answer.get("products") or [] if isinstance(p, dict) and isinstance(p.get("id"), int)], + "appId": str(answer.get("appId") or ""), + "chunks": [c for c in answer.get("chunks") or [] if isinstance(c, dict) and isinstance(c.get("id"), int)], + "saves": "C:/users/steamuser/Saved Games/Ubisoft/%s/%d/" % (user, product), + "language": str(answer.get("language") or "en-US"), + "bridge": windows_path(str(UBISOFT_CACHE / ("bridge-%d" % product))), + "log": windows_path(str(UBISOFT_CACHE / ("upc-%d.log" % product))), + } + + +def prepare_ubisoft(folder, sidecar, compat_data, launch_dir, proton="", ask_app=None): + launch = folder / UBISOFT_LAUNCH + try: + launch.unlink() + except OSError: + pass + if not compat_data: + return "no-prefix" + product = ubisoft_product(sidecar) + if product is None: + return "no-product" + if not UBISOFT_SERVICE.is_file(): + return "no-service" + made = create_prefix(compat_data, proton) if not (Path(compat_data) / "pfx/user.reg").is_file() else "present" + if made not in ("present", "created"): + return "prefix " + made + runtime = install_runtime(compat_data) + if not ubisoft_runtime_ready(compat_data): + return "runtime " + runtime + registry = register_ubisoft(folder, product, compat_data) + if not launch_dir: + return "runtime %s, %s, no-channel" % (runtime, registry) + answer = (ask_app or ask)(Path(launch_dir) / "stores", {"op": "ubisoft-launch", "store": "ubisoft", "id": str(sidecar.get("id"))}, + timeout=UBISOFT_ASK_WAIT) + if not isinstance(answer, dict) or not answer.get("ok") or not answer.get("ticket"): + reason = "timeout" if answer is None else str(answer.get("reason") or "refused") + return "runtime %s, %s, session %s" % (runtime, registry, reason) + written = 0 + if answer.get("registry") and wait_for_server(Path(compat_data) / "pfx"): + written = apply_manifest_registry(compat_data, answer.get("registry"), folder, str(answer.get("language") or "en-US")) + tmp = launch.with_name(launch.name + ".tmp") + tmp.write_text(json.dumps(ubisoft_launch(folder, sidecar, product, compat_data, answer))) + os.replace(tmp, launch) + return "runtime %s, %s, %d manifest value(s), session ready" % (runtime, registry, written) + COMET_DIR = Path("/usr/local/lib/droiddeck-comet") @@ -352,6 +570,7 @@ def prepare_gog(sidecar, compat_data, launch_dir): GOG_SCRIPTS = "goggame-*.script" GOG_REGISTRY_MARK = ".droiddeck-gog-registry" +GOG_SUPPORT_MARK = ".droiddeck-gog-support" GOG_ROOTS = {"HKEY_LOCAL_MACHINE": "HKEY_LOCAL_MACHINE", "HKLM": "HKEY_LOCAL_MACHINE", "HKEY_CURRENT_USER": "HKEY_CURRENT_USER", "HKCU": "HKEY_CURRENT_USER", "HKEY_CLASSES_ROOT": "HKEY_CLASSES_ROOT", "HKCR": "HKEY_CLASSES_ROOT"} @@ -611,6 +830,458 @@ def apply_gog_registry(folder, compat_data, proton, timeout=90, installation=Non return "import-failed" + note +EA_HARDWARE = ".droiddeck-ea-hw.json" +EA_CLIENT = "C:\\Program Files (x86)\\Origin\\Origin.exe" +EA_WMI = ( + ("boardManufacturer", "Win32_BaseBoard", "Manufacturer"), + ("boardSerial", "Win32_BaseBoard", "SerialNumber"), + ("biosManufacturer", "Win32_BIOS", "Manufacturer"), + ("biosSerial", "Win32_BIOS", "SerialNumber"), + ("osInstallDate", "Win32_OperatingSystem", "InstallDate"), + ("osSerial", "Win32_OperatingSystem", "SerialNumber"), + ("gpuPnpId", "Win32_VideoController", "PNPDeviceID"), + ("cpuManufacturer", "Win32_Processor", "Manufacturer"), + ("processorId", "Win32_Processor", "ProcessorId"), + ("brand", "Win32_Processor", "Name"), +) +EA_ROOTS = {"HKLM": "HKEY_LOCAL_MACHINE", "HKCU": "HKEY_CURRENT_USER"} + + +def decode_output(data): + if data[:2] in (b"\xff\xfe", b"\xfe\xff") or (len(data) > 1 and data[1:2] == b"\x00"): + return data.decode("utf-16", "replace") + return data.decode("utf-8", "replace") + + +def wmi_value(text): + rows = [row.strip() for row in text.replace("\r", "\n").split("\n") if row.strip()] + return rows[1] if len(rows) > 1 else "" + + +CPUID_LEAVES = (0, 1, 0x80000002, 0x80000003, 0x80000004) + + +def cpuid_exe(): + """A 32-bit console program that writes the raw CPUID leaves an x86 game sees under the emulator.""" + import struct + base, rva = 0x400000, 0x1000 + buf, written = base + rva + 0x180, base + rva + 0x1D0 + iat = base + rva + 0x138 + code = b"\x53\x57\xbf" + struct.pack(" "%s"' % (cls, prop, windows_path(str(folder / (key + ".txt"))))) + lines.append('vol C: > "%s"' % windows_path(str(folder / "volume.txt"))) + lines.append('hostname > "%s"' % windows_path(str(folder / "hostname.txt"))) + (folder / "cpuid.exe").write_bytes(cpuid_exe()) + lines.append('"%s" > "%s"' % (windows_path(str(folder / "cpuid.exe")), windows_path(str(folder / "cpuid.bin")))) + bat = folder / "probe.bat" + bat.write_bytes(("\r\n".join(lines) + "\r\n").encode()) + try: + subprocess.run([proton, "run", "cmd.exe", "/d", "/c", windows_path(str(bat))], stdin=subprocess.DEVNULL, + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=timeout) + except subprocess.TimeoutExpired: + return {}, "timeout" + except OSError: + return {}, "failed" + for key, _, _ in EA_WMI: + try: + values[key] = wmi_value(decode_output((folder / (key + ".txt")).read_bytes())) + except OSError: + values[key] = "" + try: + found = re.search(r"([0-9A-Fa-f]{4})-([0-9A-Fa-f]{4})", decode_output((folder / "volume.txt").read_bytes())) + values["volumeSerial"] = (found.group(1) + found.group(2)).lower() if found else "" + except OSError: + values["volumeSerial"] = "" + try: + values["hostname"] = decode_output((folder / "hostname.txt").read_bytes()).strip().splitlines()[0].strip() + except (OSError, IndexError): + values["hostname"] = "" + try: + values.update(cpuid_values((folder / "cpuid.bin").read_bytes())) + except OSError: + pass + if probe_complete(values): + try: + cache.write_text(json.dumps(values)) + except OSError: + pass + return values, "probed" + return values, "incomplete" + + +def ea_registry_entries(folder, sidecar): + install = windows_path(str(folder)).rstrip("\\") + "\\" + extra = sidecar.get("extra") if isinstance(sidecar.get("extra"), dict) else {} + title = str(extra.get("displayName") or sidecar.get("title") or folder.name).strip() + locale = str(extra.get("locale") or "en_US") + entries = [ + ("HKEY_LOCAL_MACHINE\\SOFTWARE\\Electronic Arts\\EA Desktop", "InstallSuccessful", "true"), + ("HKEY_LOCAL_MACHINE\\SOFTWARE\\Electronic Arts\\Origin", "InstallSuccessful", "true"), + ("HKEY_LOCAL_MACHINE\\SOFTWARE\\Electronic Arts\\Origin", "ClientPath", EA_CLIENT), + ("HKEY_LOCAL_MACHINE\\SOFTWARE\\Origin", "ClientPath", EA_CLIENT), + ] + for content in [c.strip() for c in str(extra.get("contentIds") or extra.get("contentId") or "").split(",")]: + if re.fullmatch(r"[A-Za-z0-9._-]{1,64}", content): + key = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Origin Games\\" + content + entries += [(key, "DisplayName", title), (key, "Install Dir", install), (key, "Locale", locale)] + try: + keys = json.loads(extra.get("registry") or "[]") + except ValueError: + keys = [] + for pair in keys if isinstance(keys, list) else []: + if not isinstance(pair, list) or len(pair) != 2: + continue + key, name = str(pair[0]), str(pair[1]) + root, _, rest = key.partition("\\") + key = EA_ROOTS.get(root.upper(), root) + ("\\" + rest if rest else "") + if key.upper().startswith(("HKEY_LOCAL_MACHINE\\", "HKEY_CURRENT_USER\\")) and name and "\n" not in key + name: + entries.append((key, name, install)) + if title: + entries.append((key, "DisplayName", title)) + entries.append((key, "Locale", locale)) + return entries + + +def ea_registry(folder, sidecar, compat_data, proton, timeout=90): + if not compat_data or not proton or not Path(proton).is_file(): + return "no-proton" + pfx = Path(compat_data) / "pfx" + text = "Windows Registry Editor Version 5.00\r\n\r\n" + "".join( + '[%s]\r\n"%s"="%s"\r\n\r\n' % (key, reg_escape(name), reg_escape(value)) for key, name, value in ea_registry_entries(folder, sidecar)) + stamp = hashlib.sha256(text.encode("utf-8")).hexdigest() + marker = pfx / (".droiddeck-ea-registry-" + re.sub(r"[^A-Za-z0-9._-]", "_", str(sidecar.get("id")))) + try: + if marker.is_file() and marker.read_text() == stamp and (pfx / "system.reg").is_file(): + return "ready" + with tempfile.TemporaryDirectory(prefix="droiddeck-ea-") as tmp: + source = Path(tmp) / "registry.reg" + source.write_bytes(b"\xff\xfe" + text.encode("utf-16-le")) + executable = pfx / "drive_c/windows/system32/reg.exe" + for view in ("/reg:32", "/reg:64"): + run = subprocess.run([proton, "run", str(executable) if executable.is_file() else "reg.exe", "import", windows_path(str(source)), view], + stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=timeout) + if run.returncode: + return "import-failed" + marker.write_text(stamp) + return "written" + except subprocess.TimeoutExpired: + return "timeout" + except OSError: + return "import-failed" + + +def find_case(folder, relative): + current = Path(folder) + for part in [p for p in relative.replace("\\", "/").split("/") if p and p != "."]: + exact = current / part + if exact.exists(): + current = exact + continue + try: + current = next(c for c in current.iterdir() if c.name.lower() == part.lower()) + except (OSError, StopIteration): + return None + return current if current != Path(folder) else None + + +def touchup_args(raw, locale, install): + raw = raw.replace("{locale}", locale) + raw = raw.replace('"{installLocation}"', '"%s"' % install).replace("{installLocation}", install) + try: + return shlex.split(raw.replace("\\", "\\\\")) + except ValueError: + return raw.split() + + +def ea_touchup(folder, sidecar, compat_data, proton, timeout=900): + extra = sidecar.get("extra") if isinstance(sidecar.get("extra"), dict) else {} + relative = str(extra.get("touchup") or "") + if not relative: + return "none" + exe = find_case(folder, relative) + if exe is None or not exe.is_file(): + return "missing" + if not compat_data or not proton or not Path(proton).is_file(): + return "no-proton" + pfx = Path(compat_data) / "pfx" + stamp = str(sidecar.get("installVersion") or "") + " " + str(sidecar.get("installedAt") or "") + marker = pfx / (".droiddeck-ea-touchup-" + re.sub(r"[^A-Za-z0-9._-]", "_", str(sidecar.get("id")))) + try: + if marker.is_file() and marker.read_text() == stamp: + return "done" + except OSError: + pass + args = touchup_args(str(extra.get("touchupArgs") or ""), str(extra.get("locale") or "en_US"), windows_path(str(folder)).rstrip("\\")) + try: + run = subprocess.run([proton, "run", str(exe)] + args, cwd=str(exe.parent), stdin=subprocess.DEVNULL, + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=timeout) + result = "ok" if run.returncode == 0 else "exit-%d" % run.returncode + except subprocess.TimeoutExpired: + result = "timeout" + except OSError: + return "failed" + try: + marker.write_text(stamp) + except OSError: + pass + return result + + +def write_licenses(compat_data, licenses): + if not compat_data: + return 0 + root = Path(compat_data) / "pfx/drive_c" + written = 0 + for item in licenses or []: + relative = str(item.get("path") or "") if isinstance(item, dict) else "" + if not relative or relative.startswith("/") or ".." in relative.split("/"): + continue + target = root / relative + try: + target.parent.mkdir(parents=True, exist_ok=True) + target.write_bytes(base64.b64decode(item.get("data") or "")) + written += 1 + except (OSError, binascii.Error): + pass + return written + + +def prepare_ea(folder, sidecar, compat_data, launch_dir): + game = str(sidecar.get("id")) + proton = os.environ.get("BL_STORE_PROTON", "") + made = create_prefix(compat_data, proton) + ready = made in ("present", "created") or (compat_data and (Path(compat_data) / "pfx/user.reg").is_file()) + stub = install_origin_stub(compat_data) if ready else "no-prefix" + hardware, probe = ea_hardware(compat_data, proton) if ready else ({}, "no-prefix") + answer = None + if launch_dir: + answer = ask(Path(launch_dir) / "stores", {"op": "ea-prepare", "store": "ea", "id": game, "hardware": hardware}, timeout=40.0) + sidecar = sidecar_at(Path(folder)) or sidecar + log("ea %s prefix=%s stub=%s hardware=%s registry=%s touchup=%s" % ( + game, made, stub, probe, ea_registry(folder, sidecar, compat_data, proton), ea_touchup(folder, sidecar, compat_data, proton))) + if answer is None: + log("ea %s session=no reason=%s" % (game, "timeout" if launch_dir else "no-channel")) + return + written = write_licenses(compat_data, answer.get("licenses")) + log("ea %s session=%s reason=%s lsx=%s licence=%s files=%d" % ( + game, "yes" if answer.get("ok") else "no", answer.get("reason", ""), answer.get("lsx"), answer.get("license", ""), written)) + + +def gog_support_actions(folder): + found = [] + for script in sorted(Path(folder).glob(GOG_SCRIPTS)): + product = script.name[len("goggame-"):-len(".script")] + try: + actions = json.loads(script.read_text(encoding="utf-8-sig")).get("actions") + except (OSError, ValueError, AttributeError): + continue + for action in actions if isinstance(actions, list) else []: + install = action.get("install") if isinstance(action, dict) else None + if isinstance(install, dict) and install.get("action") == "supportData" \ + and isinstance(install.get("arguments"), dict): + found.append((product, install["arguments"])) + return found + + +def gog_support_bases(folder, product, pfx): + user = Path(pfx) / "drive_c" / "users" / "steamuser" + support = Path(folder) / "support" / product + return {"app": Path(folder), "supportdir": support, "support": support, + "userdocs": user / "Documents", "localappdata": user / "AppData" / "Local"} + + +def gog_inside(path, base): + root = os.path.realpath(base) + real = os.path.realpath(path) + return real == root or real.startswith(root + os.sep) + + +def gog_support_path(text, bases): + match = re.fullmatch(r"\{(\w+)\}(.*)", text) if isinstance(text, str) else None + if not match or match.group(1).lower() not in bases: + return None + parts = [part for part in re.split(r"[\\/]+", match.group(2)) if part] + if any(part in (".", "..") for part in parts): + return None + base = bases[match.group(1).lower()] + path = base + for part in parts: + exact = path / part + if not exact.exists() and path.is_dir(): + exact = next((child for child in path.iterdir() if child.name.lower() == part.lower()), exact) + path = exact + if path != base and not gog_inside(path.parent, base): + return None + return path, base + + +def gog_copy_file(source, target, root): + if not gog_inside(target.parent, root): + raise OSError("outside " + str(root)) + target.parent.mkdir(parents=True, exist_ok=True) + if target.is_symlink(): + target.unlink() + shutil.copy2(source, target, follow_symlinks=False) + + +def gog_support_data(folder, compat_data, proton): + actions = gog_support_actions(folder) + if not actions: + return "none" + pfx = Path(compat_data) / "pfx" if compat_data else None + if not pfx or not (pfx / "drive_c" / "users" / "steamuser").is_dir(): + made = create_prefix(compat_data, proton) + if made not in ("present", "created") or not (pfx / "drive_c" / "users" / "steamuser").is_dir(): + return "no-prefix" + stamp = hashlib.sha256(json.dumps(actions, sort_keys=True).encode("utf-8")).hexdigest() + marker = pfx / GOG_SUPPORT_MARK + try: + if marker.is_file() and marker.read_text() == stamp: + return "ready" + except OSError: + pass + copied = skipped = missing = 0 + for product, arguments in actions: + bases = gog_support_bases(folder, product, pfx) + found = gog_support_path(arguments.get("target"), bases) + origin = gog_support_path(arguments["source"], bases) if "source" in arguments else None + if found is None or ("source" in arguments and origin is None): + skipped += 1 + continue + target, root = found + source = origin[0] if origin else None + try: + if source is None: + target.mkdir(parents=True, exist_ok=True) + elif not source.exists(): + missing += 1 + elif source.is_dir() and not source.is_symlink(): + for top, dirs, files in os.walk(source): + for name in sorted(files + [d for d in dirs if (Path(top) / d).is_symlink()]): + item = Path(top) / name + destination = target / item.relative_to(source) + if (destination.exists() or destination.is_symlink()) and not arguments.get("overwrite"): + continue + gog_copy_file(item, destination, root) + copied += 1 + target.mkdir(parents=True, exist_ok=True) + elif (target.exists() or target.is_symlink()) and not arguments.get("overwrite"): + continue + else: + gog_copy_file(source, target, root) + copied += 1 + except OSError: + skipped += 1 + note = "".join(" %s=%d" % (name, count) for name, count in (("skipped", skipped), ("missing", missing)) if count) + if not skipped: + try: + marker.write_text(stamp) + except OSError: + pass + return "copied=%d%s" % (copied, note) + + def main(argv): if os.environ.get("DROIDDECK_GOG_MAINTENANCE") == "1": return 0 @@ -654,6 +1325,16 @@ def main(argv): if store == "gog": prepare_gog(sidecar, compat_data, launch_dir) log("gog registry: " + apply_gog_registry(folder, compat_data, os.environ.get("BL_STORE_PROTON", ""))) + log("gog support data: " + gog_support_data(folder, compat_data, os.environ.get("BL_STORE_PROTON", ""))) + if store == "ubisoft": + try: + log("ubisoft %s: %s" % (game, prepare_ubisoft(folder, sidecar, compat_data, launch_dir, os.environ.get("BL_STORE_PROTON", "")))) + except OSError as e: + try: + (folder / UBISOFT_LAUNCH).unlink() + except OSError: + pass + log("ubisoft %s: not prepared (%s), starting directly" % (game, e)) if store == "epic": handled = os.environ.get(EPIC_HANDLED) == "1" and (folder / EPIC_ARGS).is_file() if not handled: @@ -691,6 +1372,9 @@ def main(argv): got = bool(answer.get("code")) log("epic launch id=%s code=%s reason=%s" % (game, "yes" if got else "no", answer.get("reason", ""))) log("epic prefix=%s %s" % (prefix_id or "?", provision_prefix(compat_data))) + if store == "ea": + prepare_ea(folder, sidecar, compat_data, launch_dir) + return STORE_GAME if not cloud_on(sidecar): if store in CLOUD_STORES: log("cloud %s %s down result=skipped reason=off" % (store, game)) diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-wincomponents b/tools/linuxfs/overlay/usr/local/bin/droiddeck-wincomponents index 393f971024..dde2d6e6be 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-wincomponents +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-wincomponents @@ -2,11 +2,17 @@ import json import os import re +import runpy import shutil import sys import time from pathlib import Path +PE = runpy.run_path(str(Path(__file__).resolve().parent / "droiddeck-wine-pe")) +PE_MACHINES = PE["PE_MACHINES"] +pe_machine = PE["pe_machine"] +machine_compatible = PE["machine_compatible"] + STORE = Path("/opt/droiddeck/wincomponents") SELECTION = Path.home() / ".config/droiddeck/wincomponents.json" PENDING = Path.home() / ".config/droiddeck/wincomponents-pending.json" @@ -473,6 +479,15 @@ def save_state(compat, state): os.replace(temp, compat / STATE) +def foreign_component(rel, path, machines): + parts = Path(rel).parts + folder = parts[2] if len(parts) > 2 and parts[:2] == ("drive_c", "windows") else None + if not machines or folder not in PE_MACHINES or not rel.lower().endswith(".dll"): + return False + machine = pe_machine(path) + return machine is not None and not machine_compatible(path, machines[folder][1], machine) + + def pending_for(app_id, pending=PENDING): try: with open(pending, encoding="utf-8") as source: @@ -514,7 +529,8 @@ def wait_for_pending(app_id, pending=PENDING, store=STORE, timeout=PENDING_WAIT, return "wincomp: %s waited %dms for %s" % (app_id, waited, ",".join(asked)) -def apply(compat_path, app_id, selection=SELECTION, store=STORE, pending=PENDING, wait=wait_for_pending): +def apply(compat_path, app_id, selection=SELECTION, store=STORE, pending=PENDING, wait=wait_for_pending, + proton=None, env=None): compat = Path(compat_path) pfx = compat / "pfx" waited = wait(app_id, pending, store) if wait else None @@ -527,6 +543,9 @@ def apply(compat_path, app_id, selection=SELECTION, store=STORE, pending=PENDING installed = [] values = [] held = set() + skipped = set() + placed_stems = set() + machines = PE["proton_pe_machines"](proton, env) if proton else None saved_overrides = state.get("overrides") if isinstance(state.get("overrides"), dict) else {} saved_requires = state.get("requires") if isinstance(state.get("requires"), dict) else {} recorded = [c for c in state.get("components", []) if isinstance(c, str) and valid_id(c)] @@ -566,9 +585,17 @@ def apply(compat_path, app_id, selection=SELECTION, store=STORE, pending=PENDING kept_requires[cid] = list(requires) values += [(cid, value) for value in registry] for rel, src in files: - if place(src, pfx / rel, None if rel in state["files"] else compat / BACKUP / rel): + foreign = foreign_component(rel, src, machines) + if foreign: + skipped.add(Path(rel).stem.lower()) + changed = False + else: + changed = place(src, pfx / rel, None if rel in state["files"] else compat / BACKUP / rel) + placed_stems.add(Path(rel).stem.lower()) + if changed: copied += 1 - keep[rel] = cid + if not foreign: + keep[rel] = cid removed = restored = 0 for rel, cid in state["files"].items(): if rel in keep: @@ -579,6 +606,10 @@ def apply(compat_path, app_id, selection=SELECTION, store=STORE, pending=PENDING path = existing(pfx / rel) saved = compat / BACKUP / rel if saved.is_file(): + if foreign_component(rel, saved, machines): + if path is not None and not path.is_symlink(): + path.unlink() + continue target = path or (pfx / rel) os.replace(saved, target) restored += 1 @@ -590,6 +621,14 @@ def apply(compat_path, app_id, selection=SELECTION, store=STORE, pending=PENDING (compat / "config_info").unlink() except FileNotFoundError: pass + def override_name(entry): + name = entry.partition("=")[0].strip().lower() + return name[:-4] if name.endswith(".dll") else name + + dropped = skipped - placed_stems + overrides = [entry for entry in overrides if override_name(entry) not in dropped] + for cid in kept_overrides: + kept_overrides[cid] = [entry for entry in kept_overrides[cid] if override_name(entry) not in dropped] compat.mkdir(parents=True, exist_ok=True) new_state = {"files": keep, "components": installed + sorted(held), "overrides": kept_overrides, "requires": kept_requires, "registry": state.get("registry", {})} diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-wine-pe b/tools/linuxfs/overlay/usr/local/bin/droiddeck-wine-pe new file mode 100755 index 0000000000..65c3446a2a --- /dev/null +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-wine-pe @@ -0,0 +1,103 @@ +#!/usr/bin/python3 +import re +from pathlib import Path + + +PE_MACHINES = { + "system32": ("aarch64-windows", frozenset((0xAA64, 0xA641))), + "syswow64": ("i386-windows", frozenset((0x14C,))), +} + + +def pe_machine(path): + try: + with open(path, "rb") as image: + head = image.read(64) + if head[:2] != b"MZ" or len(head) < 64: + return None + image.seek(int.from_bytes(head[60:64], "little")) + header = image.read(6) + except OSError: + return None + if header[:4] != b"PE\0\0" or len(header) < 6: + return None + return int.from_bytes(header[4:6], "little") + + +def pe_has_arm64ec_metadata(path): + try: + with open(path, "rb") as image: + image.seek(60) + raw = image.read(4) + if len(raw) != 4: + return False + pe_offset = int.from_bytes(raw, "little") + image.seek(pe_offset) + if image.read(4) != b"PE\0\0": + return False + coff = image.read(20) + if len(coff) != 20: + return False + section_count = int.from_bytes(coff[2:4], "little") + if section_count > 96: + return False + optional_size = int.from_bytes(coff[16:18], "little") + optional_offset = pe_offset + 24 + image.seek(optional_offset) + optional = image.read(optional_size) + if len(optional) != optional_size or int.from_bytes(optional[:2], "little") != 0x20B: + return False + directory_count = int.from_bytes(optional[108:112], "little") + directory_offset = 112 + 10 * 8 + if directory_count <= 10 or directory_offset + 8 > len(optional): + return False + load_config_rva = int.from_bytes(optional[directory_offset:directory_offset + 4], "little") + load_config_size = int.from_bytes(optional[directory_offset + 4:directory_offset + 8], "little") + if not load_config_rva or load_config_size < 0xD0: + return False + section_offset = optional_offset + optional_size + for index in range(section_count): + image.seek(section_offset + index * 40) + section = image.read(40) + if len(section) != 40: + return False + virtual_address = int.from_bytes(section[12:16], "little") + raw_size = int.from_bytes(section[16:20], "little") + raw_offset = int.from_bytes(section[20:24], "little") + relative = load_config_rva - virtual_address + if 0 <= relative <= raw_size - 0xD0: + image.seek(raw_offset + relative) + load_config = image.read(0xD0) + if len(load_config) != 0xD0 or int.from_bytes(load_config[:4], "little") < 0xD0: + return False + return int.from_bytes(load_config[0xC8:0xD0], "little") != 0 + except (OSError, OverflowError, ValueError): + return False + return False + + +# Only x64 code and members of the same runtime family import these, so an x64 copy loads in an +# ARM64EC prefix, and games check for a newer version than Wine's builtins report. +VC_RUNTIME = re.compile(r"(msvc[pr]\d.*|vcruntime\d.*|vcomp\d*|vcamp\d.*|vccorlib\d.*|concrt\d.*|mfcm?\d.*)\.dll", + re.IGNORECASE) + + +def machine_compatible(path, machines, machine=None): + machine = pe_machine(path) if machine is None else machine + if machine == 0x8664 and 0xA641 in machines and VC_RUNTIME.fullmatch(Path(path).name): + return True + if machine == 0x8664 and pe_has_arm64ec_metadata(path): + return 0xA641 in machines + return machine in machines + + +def proton_pe_machines(proton, env=None): + dist = Path(proton).parent / "files/lib/wine" + if (dist / "aarch64-unix").is_dir() and (env or {}).get("PROTON_USE_ARM64", "1") == "1": + return PE_MACHINES + if (dist / "x86_64-unix").is_dir(): + return { + "system32": ("x86_64-windows", frozenset((0x8664,))), + "syswow64": PE_MACHINES["syswow64"], + } + return None diff --git a/tools/linuxfs/overlay/usr/local/bin/steam-compatibility b/tools/linuxfs/overlay/usr/local/bin/steam-compatibility index 8247201a34..426b6c173b 100755 --- a/tools/linuxfs/overlay/usr/local/bin/steam-compatibility +++ b/tools/linuxfs/overlay/usr/local/bin/steam-compatibility @@ -16,6 +16,8 @@ LEGACY_LAUNCHER = "bannerlator-proton" LIBRARY = "/mnt/droiddeck" SOURCES = ("Proton Experimental (ARM64)", "Proton 11.0 (ARM64)") OWN_TOOLS = ((TOOL, SOURCES), (TOOL_11, ("Proton 11.0 (ARM64)",))) +VALVE_DEPOTS = {"proton_experimental": SOURCES[0], "proton-experimental-arm64": SOURCES[0], + "proton_11": SOURCES[1], "proton_11-arm64": SOURCES[1]} FEX_TOOL = "droiddeck-fex" FEX_LAUNCHER = "droiddeck-fex-tool" FEX_FILES = ("toolmanifest.vdf", "compatibilitytool.vdf", FEX_LAUNCHER) @@ -150,6 +152,13 @@ bl_components() { else cm_app="" fi + if [ "$cm_app" = 2929927072 ] && [[ "${SteamAppId:-}" =~ ^[0-9]{1,10}$ ]] && (( 10#$SteamAppId > 0 && 10#$SteamAppId <= 4294967295 )); then + cm_app=$((10#$SteamAppId)) + elif [ "$cm_app" = 2929927072 ] && [[ "${SteamGameId:-}" =~ ^[1-9][0-9]{9,19}$ ]] && + { [ ${#SteamGameId} -lt 20 ] || [[ ! "$SteamGameId" > 18446744073709551615 ]]; } && + (( (10#$SteamGameId & 0xFFFFFFFF) == 0x02000000 && ((10#$SteamGameId >> 32) & 0xFFFFFFFF) != 0 )); then + cm_app=$(( (10#$SteamGameId >> 32) & 0xFFFFFFFF )) + fi if [ -z "$cm_app" ]; then echo "droiddeck-proton: components: invalid game prefix, left as it is" >&2 return 0 @@ -732,6 +741,7 @@ def libraries(steam_root): def installed_apps(steam_root): + entries = appinfo_entries(os.path.join(steam_root, "appcache", "appinfo.vdf")) apps = set() for library in libraries(steam_root): steamapps = os.path.join(library, "steamapps") @@ -741,7 +751,7 @@ def installed_apps(steam_root): continue for name in names: m = re.fullmatch(r"appmanifest_(\d+)\.acf", name) - if m and m.group(1) not in NOT_GAMES: + if m and m.group(1) not in NOT_GAMES and entries.get(m.group(1), {}).get("type") != "tool": apps.add(m.group(1)) return sorted(apps, key=int) @@ -925,6 +935,12 @@ def valve_picks(steam_root): return sorted(picks, key=int) +def tool_platforms(steam_root): + entries = appinfo_entries(os.path.join(steam_root, "appcache", "appinfo.vdf")) + return {app: entry["oslist"] for app, entry in entries.items() + if entry["type"] == "tool" and entry["oslist"] & {"linux", "windows"}} + + def native_targets(steam_root, apps): wanted = {str(app) for app in apps if str(app) not in NOT_GAMES} if not wanted: @@ -1062,10 +1078,11 @@ NATIVE_CHOICE = object() def plan_mapping(current, installed, runnable, default, auto, native=None, picks=(), overrides=None, managed=None, - native_choices=()): + native_choices=(), tools=None): + tools = tools or {} runnable = {n.lower() for n in runnable} installed = set(installed) - picks = set(picks) + picks = set(picks) | {app for app, platforms in tools.items() if "linux" not in platforms} native = native or {} overrides = overrides or {} managed = set(auto) if managed is None else set(managed) @@ -1073,8 +1090,12 @@ def plan_mapping(current, installed, runnable, default, auto, native=None, picks changes, kept = {}, {} if current.get("0", "") != default: changes["0"] = default - for app in sorted((set(current) | installed | picks | set(auto) | set(overrides) | native_choices) - {"0"}, key=app_order): + for app in sorted((set(current) | installed | picks | set(auto) | set(overrides) | native_choices | set(tools)) - {"0"}, key=app_order): name = current.get(app, "") + if app in tools and "linux" in tools[app] and "windows" not in tools[app]: + if name and (name.lower() in runnable or "proton" in name.lower()): + changes[app] = None + continue if app in overrides: target = overrides[app] if target is DEFERRED_GAME_TOOL: @@ -1160,6 +1181,18 @@ def tool_catalog(steam_root, extras=None): return catalog +def built_from(name, catalog, runnable=None): + depot = VALVE_DEPOTS.get(name) + tools = [t["name"] for t in catalog if depot and t["valve"] and t["dir"] == depot and + (runnable is None or t["name"] in runnable)] + return tools[-1] if tools else None + + +def rebuilt_picks(current, catalog, known, runnable=None): + picks = {app: built_from(name, catalog, runnable) for app, name in current.items() if app in known} + return {app: tool for app, tool in picks.items() if tool} + + def requested_tool(request, catalog): name = request.get("name") if isinstance(name, str) and any(t["name"] == name for t in catalog): @@ -1307,9 +1340,13 @@ def now_ms(): return int(time.time() * 1000) -def choose_default(steam_default, state, request, catalog, adopt_steam=True, apply_request=True, now=None): +def choose_default(steam_default, state, request, catalog, adopt_steam=True, apply_request=True, now=None, listed=None): state = dict(state) names = [t["name"] for t in catalog] + listed = names if listed is None else listed + if "written" not in state and state.get("source") == "steam": + state["source"] = "fallback" + hidden = state.get("default") in names and state.get("default") not in listed seq = request.get("seq") seq = seq if isinstance(seq, int) else 0 if apply_request and seq > int(state.get("applied", 0) or 0): @@ -1321,11 +1358,12 @@ def choose_default(steam_default, state, request, catalog, adopt_steam=True, app else: print("steam-compatibility: the app asked for %s, which is not installed; kept %s" % ( request.get("name") or request.get("dir"), state.get("default") or TOOL)) - elif adopt_steam and steam_default in names and steam_default != state.get("default"): + elif adopt_steam and steam_default in names and not hidden and steam_default not in ( + state.get("default"), state.get("written", state.get("default") or TOOL)): state.update(default=steam_default, seq=now or now_ms(), source="steam") - if state.get("default") not in names: - state["default"] = TOOL - return state["default"], state + if state.get("default") not in names and state.get("default") != TOOL: + state.update(default=TOOL, source="fallback") + return state["default"] if state["default"] in listed else TOOL, state def mapping_block(tokens): @@ -1482,7 +1520,7 @@ def live_helper_running(directory=None): def register_default(config_path, apps=(), protect=(), default=None, catalog=None, directory=None, settle=False, picks=(), - linux_tool=FEX_TOOL): + linux_tool=FEX_TOOL, owned=None): with locked(directory): state = read_json(state_path("state.json", directory)) with open(config_path, encoding="utf-8", errors="replace") as f: @@ -1497,24 +1535,38 @@ def register_default(config_path, apps=(), protect=(), default=None, catalog=Non dict(protect) if isinstance(protect, dict) else {name: name for name in protect}) names = [t["name"] for t in catalog] newest, gone = own_proton_moves(state, catalog) - previous, applied, chosen = state.get("default"), state.get("applied"), state.get("seq") + previous = state.get("default") + if owned is not None: + auto = followers(state, current) + known = {app for app in state.get("owned", ()) if isinstance(app, str)} if isinstance(state.get("owned"), list) else set() + pinned = {app: current[app] for app in set(owned) - known + if current.get(app) in {TOOL, previous} | gone and app not in auto} + if pinned and isinstance(auto, dict): + state["auto"] = dict(auto, **pinned) + state["owned"] = sorted(set(owned), key=app_order) if default is None: if settle: - default, state = choose_default(current.get("0", ""), state, read_json(state_path("request.json", directory)), catalog) + default, state = choose_default(built_from(current.get("0", ""), catalog) or current.get("0", ""), state, + read_json(state_path("request.json", directory)), catalog) else: default = state.get("default") if state.get("default") in names else TOOL - if settle and newest and default == TOOL and state.get("applied") == applied and state.get("seq") == chosen and ( - previous in gone or not state.get("own_proton")): + if settle and newest and default == TOOL and state.get("source") not in ("app", "steam"): default = newest state.update(default=newest, seq=now_ms(), source="update") - if settle and newest: - state["own_proton"] = True + state.pop("own_proton", None) if newest: replacements = {app: newest for app, name in current.items() if app != "0" and name in gone} set_mappings(tokens, mapping, replacements) current.update(replacements) if isinstance(state.get("auto"), dict): state["auto"] = {app: newest if name in gone else name for app, name in state["auto"].items()} + following = followers(state, current) + following = following if isinstance(following, dict) else {} + moved = rebuilt_picks(current, catalog, set(following) | picked_apps(state)) + if moved: + state["auto"] = {app: name for app, name in following.items() if app not in moved} + set_mappings(tokens, mapping, moved) + current.update(moved) steam_root = os.path.dirname(os.path.dirname(os.path.abspath(config_path))) requests = settled_game_requests(state, current, catalog, not steam_running(), directory) overrides = requested_games(requests or {}, catalog) @@ -1527,7 +1579,7 @@ def register_default(config_path, apps=(), protect=(), default=None, catalog=Non current.setdefault(app, "") changes, auto = plan_mapping( current, apps, names, default, followers(state, current), native, picks, - overrides, managed, native_choices) + overrides, managed, native_choices, tool_platforms(steam_root)) set_mappings(tokens, mapping, changes) text = serialize(tokens) if text != serialize(tokenize(original)): @@ -1539,6 +1591,8 @@ def register_default(config_path, apps=(), protect=(), default=None, catalog=Non state.pop("native", None) state["tools"] = catalog state.setdefault("default", default) + if settle: + state["written"] = default write_json(state_path("state.json", directory), state) return default @@ -1552,10 +1606,11 @@ def depot_build(depot): return int(first[0]) if first and first[0].isdigit() else 0 -def find_source(steam_root, sources=SOURCES): +def find_source(steam_root, sources=SOURCES, runnable=False): for name in sources: found = [os.path.join(root, "steamapps", "common", name) for root in libraries(steam_root) - if os.path.isdir(os.path.join(root, "steamapps", "common", name, "files", "bin-arm64"))] + if os.path.isdir(os.path.join(root, "steamapps", "common", name, "files", "bin-arm64")) + and (not runnable or os.access(os.path.join(root, "steamapps", "common", name, "proton"), os.X_OK))] if found: return max(found, key=depot_build) return None diff --git a/tools/linuxfs/preload/binfmt.c b/tools/linuxfs/preload/binfmt.c index 7aee2ee2fa..dbe2977265 100644 --- a/tools/linuxfs/preload/binfmt.c +++ b/tools/linuxfs/preload/binfmt.c @@ -18,14 +18,20 @@ typedef int (*spawn_fn)(pid_t *, const char *, const posix_spawn_file_actions_t *, const posix_spawnattr_t *, char *const[], char *const[]); +__attribute__((visibility("hidden"))) +char *const *bl_webhelper_argv(const char *path, char *const argv[], char *const envp[]); #if defined(__aarch64__) || defined(BINFMT_TEST) -static int desktop(void) { - const char *value = getenv("BL_DESKTOP"); +static int flag(const char *name) { + const char *value = getenv(name); return value && value[0] == '1' && value[1] == 0; } +static int handing_off(void) { + return flag("BL_DESKTOP") || flag("BL_BINFMT"); +} + static int foreign(const char *path, int error) { if (!path || !strcmp(path, BINFMT_HANDLER)) return 0; int fd = open(path, O_RDONLY | O_CLOEXEC); @@ -49,7 +55,7 @@ static int foreign(const char *path, int error) { } static int wanted(const char *path, int error) { - return (error == ENOEXEC || error == ENOENT || error == EACCES) && desktop() && foreign(path, error); + return (error == ENOEXEC || error == ENOENT || error == EACCES) && handing_off() && foreign(path, error); } static int lookup(const char *file, char *out, size_t size) { @@ -100,7 +106,7 @@ static int spawn_handler(pid_t *pid, const char *path, const posix_spawn_file_ac } __attribute__((visibility("hidden"))) void bl_binfmt_before(const char *path, char *const argv[], char *const envp[]) { - if (desktop() && foreign(path, ENOEXEC)) hand_off(path, argv, envp); + if (handing_off() && foreign(path, ENOEXEC)) hand_off(path, argv, envp); } __attribute__((visibility("hidden"))) int bl_binfmt_retry(const char *path, char *const argv[], char *const envp[], int error) { @@ -111,18 +117,18 @@ __attribute__((visibility("hidden"))) int bl_binfmt_retry(const char *path, char __attribute__((visibility("hidden"))) void bl_binfmt_before_search(const char *file, char *const argv[], char *const envp[]) { char path[PATH_MAX]; - if (desktop() && lookup(file, path, sizeof path)) bl_binfmt_before(path, argv, envp); + if (handing_off() && lookup(file, path, sizeof path)) bl_binfmt_before(path, argv, envp); } __attribute__((visibility("hidden"))) int bl_binfmt_after_search(const char *file, char *const argv[], char *const envp[], int error) { char path[PATH_MAX]; - if (desktop() && lookup(file, path, sizeof path)) return bl_binfmt_retry(path, argv, envp, error); + if (handing_off() && lookup(file, path, sizeof path)) return bl_binfmt_retry(path, argv, envp, error); errno = error; return -1; } __attribute__((visibility("hidden"))) int bl_binfmt_runnable(const char *path, int mode) { - if (mode != X_OK || !desktop()) return 0; + if (mode != X_OK || !handing_off()) return 0; int runs = foreign(path, EACCES) != 0; errno = EACCES; return runs; @@ -131,27 +137,32 @@ __attribute__((visibility("hidden"))) int bl_binfmt_runnable(const char *path, i __attribute__((visibility("hidden"))) int bl_binfmt_spawn(pid_t *pid, const char *path, const posix_spawn_file_actions_t *fa, const posix_spawnattr_t *attr, char *const argv[], char *const envp[], spawn_fn real) { - if (desktop() && foreign(path, ENOEXEC)) return spawn_handler(pid, path, fa, attr, argv, envp, real); + if (handing_off() && foreign(path, ENOEXEC)) return spawn_handler(pid, path, fa, attr, argv, envp, real); int error = real(pid, path, fa, attr, argv, envp); return error && wanted(path, error) ? spawn_handler(pid, path, fa, attr, argv, envp, real) : error; } int execvpe(const char *file, char *const argv[], char *const envp[]) { + char resolved[PATH_MAX]; + const char *path = lookup(file, resolved, sizeof resolved) ? resolved : file; + char *const *adjusted = bl_webhelper_argv(path, argv, envp); static int (*real)(const char *, char *const[], char *const[]); if (!real) real = (int (*)(const char *, char *const[], char *const[]))dlsym(RTLD_NEXT, "execvpe"); - bl_binfmt_before_search(file, argv, envp); - real(file, argv, envp); - return bl_binfmt_after_search(file, argv, envp, errno); + bl_binfmt_before_search(file, (char *const *)adjusted, envp); + real(file, (char *const *)adjusted, envp); + return bl_binfmt_after_search(file, (char *const *)adjusted, envp, errno); } int posix_spawnp(pid_t *pid, const char *file, const posix_spawn_file_actions_t *fa, const posix_spawnattr_t *attr, char *const argv[], char *const envp[]) { + char path[PATH_MAX]; + int found = lookup(file, path, sizeof path); + char *const *adjusted = bl_webhelper_argv(found ? path : file, argv, envp); static spawn_fn real, spawn; if (!real) real = (spawn_fn)dlsym(RTLD_NEXT, "posix_spawnp"); if (!spawn) spawn = (spawn_fn)dlsym(RTLD_NEXT, "posix_spawn"); - char path[PATH_MAX]; - if (!desktop() || !lookup(file, path, sizeof path)) return real(pid, file, fa, attr, argv, envp); - return bl_binfmt_spawn(pid, path, fa, attr, argv, envp, spawn); + if (!handing_off() || !found) return real(pid, file, fa, attr, (char *const *)adjusted, envp); + return bl_binfmt_spawn(pid, path, fa, attr, (char *const *)adjusted, envp, spawn); } #else diff --git a/tools/linuxfs/preload/dircache.c b/tools/linuxfs/preload/dircache.c index 7d6cffd124..296faa616b 100644 --- a/tools/linuxfs/preload/dircache.c +++ b/tools/linuxfs/preload/dircache.c @@ -22,8 +22,12 @@ #define MAX_LISTINGS 16 #define MAX_ENTRIES 100000 #define MAX_HANDLES 64 +#define MAX_UP 32 #define FUSE_MAGIC 0x65735546 #define WATCHED (IN_CREATE | IN_DELETE | IN_MOVED_FROM | IN_MOVED_TO | IN_DELETE_SELF | IN_MOVE_SELF | IN_ONLYDIR) +#define ABOVE (IN_DELETE_SELF | IN_MOVE_SELF | IN_ONLYDIR | IN_MASK_ADD) +#define GONE (IN_DELETE_SELF | IN_MOVE_SELF | IN_IGNORED) +#define LINKED (IN_DELETE_SELF | IN_MOVE_SELF | IN_DONT_FOLLOW | IN_MASK_ADD) #if __SIZEOF_POINTER__ == 8 @@ -42,10 +46,14 @@ struct listing { int refs; int detached; int wd; + int up[MAX_UP], nup; + int blind; size_t n, cap; struct entry *e; char *recs; size_t len; + char *where; + int folds; }; struct handle { @@ -55,6 +63,8 @@ struct handle { size_t pos; int fd; int wd; + int up[MAX_UP], nup; + int blind; int dirty; char *path; struct dirent64 out; @@ -147,6 +157,7 @@ static void destroy(struct listing *l) { for (size_t i = 0; i < l->n; i++) free(l->e[i].name); free(l->e); free(l->recs); + free(l->where); free(l); } @@ -155,15 +166,26 @@ static void unref(struct listing *l) { } +static int under(const int *up, int nup, int wd) { + for (int i = 0; i < nup; i++) + if (up[i] == wd) return 1; + return 0; +} + static void release(int wd) { if (wd < 0 || notify < 0) return; for (int i = 0; i < MAX_LISTINGS; i++) - if (table[i] && table[i]->wd == wd) return; + if (table[i] && (table[i]->wd == wd || under(table[i]->up, table[i]->nup, wd))) return; for (int i = 0; i < high_water; i++) - if (handles[i] && handles[i]->wd == wd) return; + if (handles[i] && (handles[i]->wd == wd || under(handles[i]->up, handles[i]->nup, wd))) return; inotify_rm_watch(notify, wd); } +static void let_go(int wd, const int *up, int nup) { + release(wd); + for (int i = 0; i < nup; i++) release(up[i]); +} + static void detach(struct listing *l) { __atomic_sub_fetch(&cached_listings, 1, __ATOMIC_RELEASE); l->detached = 1; @@ -172,17 +194,32 @@ static void detach(struct listing *l) { static void drop(int slot) { struct listing *l = table[slot]; - int wd = l->wd; + int wd = l->wd, up[MAX_UP], nup = l->nup; + memcpy(up, l->up, sizeof(up)); table[slot] = NULL; detach(l); - release(wd); + let_go(wd, up, nup); } -static void changed(int wd) { +static void changed(int wd, uint32_t mask) { + int gone = (mask & GONE) != 0; for (int i = 0; i < MAX_LISTINGS; i++) - if (table[i] && (table[i]->wd == wd || wd < 0)) drop(i); + if (table[i] && (table[i]->wd == wd || wd < 0 || (gone && under(table[i]->up, table[i]->nup, wd)))) drop(i); for (int i = 0; i < high_water; i++) - if (handles[i] && (handles[i]->wd == wd || wd < 0)) __atomic_store_n(&handles[i]->dirty, 1, __ATOMIC_RELAXED); + if (handles[i] && (handles[i]->wd == wd || wd < 0 || (gone && under(handles[i]->up, handles[i]->nup, wd)))) + __atomic_store_n(&handles[i]->dirty, 1, __ATOMIC_RELAXED); +} + +static void forget_below(const char *path) { + if (path == NULL || path[0] != '/') return; + size_t n = strlen(path); + while (n > 1 && path[n - 1] == '/') n--; + for (int i = 0; i < MAX_LISTINGS; i++) { + struct listing *l = table[i]; + if (l == NULL || l->where == NULL) continue; + int prefix = l->folds ? strncasecmp(l->where, path, n) == 0 : strncmp(l->where, path, n) == 0; + if (prefix && (l->where[n] == '/' || l->where[n] == 0)) drop(i); + } } static void pump(struct expect *x, int nx) { @@ -194,7 +231,7 @@ static void pump(struct expect *x, int nx) { if (n <= 0) { if (n < 0 && errno != EAGAIN && errno != EINTR) { notify = -2; - changed(-1); + changed(-1, 0); } break; } @@ -202,7 +239,7 @@ static void pump(struct expect *x, int nx) { struct inotify_event *ev = (struct inotify_event *)p; p += sizeof(*ev) + ev->len; if (ev->mask & IN_Q_OVERFLOW) { - changed(-1); + changed(-1, 0); continue; } int k = 0; @@ -212,21 +249,21 @@ static void pump(struct expect *x, int nx) { for (int i = 0; i < high_water; i++) if (handles[i] && handles[i]->wd == ev->wd) __atomic_store_n(&handles[i]->dirty, 1, __ATOMIC_RELAXED); } else { - changed(ev->wd); + changed(ev->wd, ev->mask); } } } errno = saved; } -static int watch(const char *path) { +static int watch(const char *path, uint32_t mask) { if (notify == -1) notify = inotify_init1(IN_NONBLOCK | IN_CLOEXEC); if (notify < 0) { notify = -2; return -1; } int saved = errno; - int wd = inotify_add_watch(notify, path, WATCHED); + int wd = inotify_add_watch(notify, path, mask); errno = saved; return wd; } @@ -257,10 +294,11 @@ static void install(struct listing *l) { int slot = find(l->dev, l->ino); if (slot >= 0) { struct listing *old = table[slot]; - int old_wd = old->wd; + int old_wd = old->wd, up[MAX_UP], nup = old->nup; + memcpy(up, old->up, sizeof(up)); table[slot] = l; detach(old); - if (old_wd != l->wd) release(old_wd); + let_go(old_wd, up, nup); return; } int victim = -1; @@ -387,11 +425,47 @@ static DIR *hit(const struct stat *st, struct handle *h, long long now) { return NULL; } +static int above(struct handle *h, const char *path) { + char up[PATH_MAX]; + size_t n = strlen(path); + while (n > 1 && path[n - 1] == '/') n--; + if (n >= sizeof(up)) return 0; + memcpy(up, path, n); + up[n] = 0; + size_t root = (size_t)(strstr(path, "/steamapps") - path) + 10; + for (size_t i = 1; i <= n; i++) { + if (up[i] != '/' && up[i] != 0) continue; + char at = up[i]; + up[i] = 0; + int wd = i < n ? watch(up, ABOVE) : h->wd; + int link = watch(up, LINKED); + up[i] = at; + if (link < 0) h->blind = 1; + else if (link != wd) { + h->blind = 1; + release(link); + } + if (i == n) break; + if (wd < 0 && i < root) { + h->blind = 1; + continue; + } + if (wd < 0) return 0; + if (wd == h->wd || under(h->up, h->nup, wd)) continue; + if (h->nup == MAX_UP) { + release(wd); + return 0; + } + h->up[h->nup++] = wd; + } + return 1; +} + static int track(struct handle *h, const char *path) { pthread_mutex_lock(&lock); - h->wd = watch(path); - DIR *d = h->wd >= 0 ? wrap(h) : NULL; - if (d == NULL && h->wd >= 0) release(h->wd); + h->wd = watch(path, WATCHED); + DIR *d = h->wd >= 0 && above(h, path) ? wrap(h) : NULL; + if (d == NULL) let_go(h->wd, h->up, h->nup); pthread_mutex_unlock(&lock); return d != NULL; } @@ -404,6 +478,10 @@ static void filling(struct handle *h, DIR *real, const struct stat *st, long lon h->fill->filled = now; h->fill->used = now; h->fill->wd = h->wd; + memcpy(h->fill->up, h->up, sizeof(h->up)); + h->fill->nup = h->nup; + h->fill->blind = h->blind; + h->fill->where = strdup(h->path); } } @@ -411,7 +489,7 @@ static void untrack(struct handle *h) { pthread_mutex_lock(&lock); int i = registered((DIR *)h); if (i >= 0) __atomic_store_n(&handles[i], NULL, __ATOMIC_RELEASE); - release(h->wd); + let_go(h->wd, h->up, h->nup); pthread_mutex_unlock(&lock); } @@ -454,11 +532,27 @@ static void abandon(struct handle *h) { h->fill = NULL; } +static int folds(const char *path, const struct stat *st) { + char swapped[PATH_MAX]; + struct stat b; + const char *at = strstr(path, "/steamapps"); + size_t n = strlen(path); + if (at == NULL || n >= sizeof(swapped)) return 0; + memcpy(swapped, path, n + 1); + for (char *c = swapped + (at - path); *c; c++) + if ((*c >= 'a' && *c <= 'z') || (*c >= 'A' && *c <= 'Z')) *c ^= 0x20; + int saved = errno; + int same_dir = stat(swapped, &b) == 0 && b.st_dev == st->st_dev && b.st_ino == st->st_ino; + errno = saved; + return same_dir; +} + static void finish(struct handle *h) { struct listing *l = h->fill; struct stat st; h->fill = NULL; int current = stat(h->path, &st) == 0 && same(l, &st); + if (current) l->folds = folds(h->path, &st); pthread_mutex_lock(&lock); if (!changing) pump(NULL, 0); if (current && !__atomic_load_n(&h->dirty, __ATOMIC_RELAXED)) install(l); @@ -536,7 +630,7 @@ int closedir(DIR *d) { if (h) { __atomic_store_n(&handles[i], NULL, __ATOMIC_RELEASE); if (h->snap) unref(h->snap); - release(h->wd); + let_go(h->wd, h->up, h->nup); } pthread_mutex_unlock(&lock); if (h == NULL) return next_closedir(d); @@ -658,6 +752,11 @@ static int patch(struct side *s, int what, uint32_t event, int ok, int error, co copy->filled = l->filled; copy->used = l->used; copy->wd = l->wd; + memcpy(copy->up, l->up, sizeof(l->up)); + copy->nup = l->nup; + copy->blind = l->blind; + copy->where = l->where ? strdup(l->where) : NULL; + copy->folds = l->folds; copy->detached = 0; stamp(copy, &s->before); table[slot] = copy; @@ -738,6 +837,7 @@ int bl_dircache_create(int dirfd, const char *path, int flags, mode_t mode, struct expect x; \ pthread_mutex_lock(&lock); \ int nx = patch(&s, what, event, r == 0, saved, what == ADDED ? path : NULL, &x); \ + if (r == 0 && what == REMOVED) forget_below(path); \ pthread_mutex_unlock(&lock); \ end(&x, nx); \ pthread_mutex_unlock(&oplock); \ @@ -770,6 +870,7 @@ ONE_PATH(remove, REMOVED, IN_DELETE, next_remove(path)) if (track_a && strcmp(a.dir, b.dir) == 0) b.known = stat(b.dir, &b.before) == 0; \ nx += patch(&b, ADDED, event, r == 0, saved, to, &x[nx]); \ } \ + if (r == 0 && gone) { forget_below(from); forget_below(to); } \ pthread_mutex_unlock(&lock); \ end(x, nx); \ pthread_mutex_unlock(&oplock); \ @@ -784,8 +885,62 @@ TWO_PATHS(rename, 1, IN_MOVED_TO, next_rename) TWO_PATHS(link, 0, IN_CREATE, link_or_lock) TWO_PATHS(symlink, 0, IN_CREATE, next_symlink) +static int named(const struct listing *l, const char *name) { + for (size_t i = 0; i < l->n; i++) + if (strcasecmp(l->e[i].name, name) == 0) return 1; + return 0; +} + +static int folded_match(const char *where, const char *dir) { + const char *at = strstr(where, "/steamapps"); + if (at == NULL) return 0; + size_t n = (size_t)(at - where); + return strncmp(where, dir, n) == 0 && strcasecmp(where + n, dir + n) == 0; +} + +static int same_dir(const struct listing *l, const char *dir) { + struct stat st; + int saved = errno; + int same = stat(dir, &st) == 0 && st.st_dev == l->dev && st.st_ino == l->ino; + errno = saved; + return same; +} + +static int listed_absent(const char *dir, const char *name, long long now) { + for (int i = 0; i < MAX_LISTINGS; i++) { + struct listing *l = table[i]; + if (l == NULL || l->where == NULL || l->blind || now - l->filled >= DIR_TTL_NS) continue; + if (strcmp(l->where, dir) == 0) return !named(l, name); + if (l->folds && folded_match(l->where, dir)) return same_dir(l, dir) && !named(l, name); + } + return 0; +} + +int bl_dircache_absent(const char *path) __attribute__((visibility("hidden"))); +int bl_dircache_absent(const char *path) { + struct side s; + if (!watching(path) || !split(path, &s)) return 0; + for (const char *c = s.name; *c; c++) + if ((unsigned char)*c >= 0x80) return 0; + long long now = now_ns(); + pthread_mutex_lock(&lock); + int absent = 0; + if (!changing) { + pump(NULL, 0); + absent = listed_absent(s.dir, s.name, now); + } + pthread_mutex_unlock(&lock); + return absent; +} + #else +int bl_dircache_absent(const char *path) __attribute__((visibility("hidden"))); +int bl_dircache_absent(const char *path) { + (void)path; + return 0; +} + int bl_dircache_create(int dirfd, const char *path, int flags, mode_t mode, int (*real)(int, const char *, int, mode_t)) __attribute__((visibility("hidden"))); int bl_dircache_create(int dirfd, const char *path, int flags, mode_t mode, diff --git a/tools/linuxfs/preload/net.c b/tools/linuxfs/preload/net.c index 4946db7f9c..e45ed9c5c0 100644 --- a/tools/linuxfs/preload/net.c +++ b/tools/linuxfs/preload/net.c @@ -271,7 +271,8 @@ __attribute__((visibility("hidden"))) void bl_binfmt_before(const char *path, ch __attribute__((visibility("hidden"))) int bl_binfmt_retry(const char *path, char *const argv[], char *const envp[], int error); __attribute__((visibility("hidden"))) void bl_binfmt_before_search(const char *file, char *const argv[], char *const envp[]); __attribute__((visibility("hidden"))) int bl_binfmt_after_search(const char *file, char *const argv[], char *const envp[], int error); -__attribute__((visibility("hidden"))) char *const *bl_webhelper_argv(const char *path, char *const argv[]); +__attribute__((visibility("hidden"))) +char *const *bl_webhelper_argv(const char *path, char *const argv[], char *const envp[]); __attribute__((visibility("hidden"))) int bl_binfmt_spawn(pid_t *pid, const char *path, const posix_spawn_file_actions_t *fa, const posix_spawnattr_t *attr, char *const argv[], char *const envp[], spawn_fn real); @@ -341,7 +342,7 @@ static char **web_helper_redirect(const char *path, char *const argv[], const ch } int execve(const char *path, char *const argv_in[], char *const envp[]) { - char *const *argv = bl_webhelper_argv(path, argv_in); + char *const *argv = bl_webhelper_argv(path, argv_in, envp); static int (*real)(const char *, char *const[], char *const[]); if (!real) real = (int (*)(const char *, char *const[], char *const[]))dlsym(RTLD_NEXT, "execve"); maybe_answer_lsof(path, argv); @@ -372,7 +373,7 @@ static int answer_lsof_via_spawn(const char *path, char *const argv[], pid_t *pi } int execvp(const char *file, char *const argv_in[]) { - char *const *argv = bl_webhelper_argv(file, argv_in); + char *const *argv = bl_webhelper_argv(file, argv_in, environ); static int (*real)(const char *, char *const[]); if (!real) real = (int (*)(const char *, char *const[]))dlsym(RTLD_NEXT, "execvp"); maybe_answer_lsof(file, argv); @@ -382,7 +383,7 @@ int execvp(const char *file, char *const argv_in[]) { } int execv(const char *path, char *const argv_in[]) { - char *const *argv = bl_webhelper_argv(path, argv_in); + char *const *argv = bl_webhelper_argv(path, argv_in, environ); static int (*real)(const char *, char *const[]); if (!real) real = (int (*)(const char *, char *const[]))dlsym(RTLD_NEXT, "execv"); maybe_answer_lsof(path, argv); @@ -406,5 +407,6 @@ int posix_spawn(pid_t *pid, const char *path, const posix_spawn_file_actions_t * } static spawn_fn real; if (!real) real = (spawn_fn)dlsym(RTLD_NEXT, "posix_spawn"); - return bl_binfmt_spawn(pid, path, fa, attr, argv, envp, real); + char *const *adjusted = bl_webhelper_argv(path, argv, envp); + return bl_binfmt_spawn(pid, path, fa, attr, (char *const *)adjusted, envp, real); } diff --git a/tools/linuxfs/preload/netif.c b/tools/linuxfs/preload/netif.c index d7f56e32ab..aeeeb9eb24 100644 --- a/tools/linuxfs/preload/netif.c +++ b/tools/linuxfs/preload/netif.c @@ -29,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -1655,6 +1656,7 @@ static void reply_attr(struct nl_reply *out, unsigned char *message, int type, return; } attr = (struct rtattr *) (message + offset); + memset(attr, 0, total); attr->rta_type = (unsigned short) type; attr->rta_len = (unsigned short) RTA_LENGTH(len); if (len > 0) memcpy(RTA_DATA(attr), data, len); @@ -2155,3 +2157,29 @@ ssize_t send(int fd, const void *buffer, size_t length, int flags) { } return real(fd, buffer, length, flags); } + +ssize_t write(int fd, const void *buffer, size_t length) { + static ssize_t (*real)(int, const void *, size_t); + ssize_t rc; + if (bl_nl_owns(fd) && deliver(fd, buffer, length, 0, &rc)) return rc; + if (real == NULL) real = (ssize_t (*)(int, const void *, size_t)) dlsym(RTLD_NEXT, "write"); + if (real == NULL) { + errno = ENOSYS; + return -1; + } + return real(fd, buffer, length); +} + +ssize_t writev(int fd, const struct iovec *iov, int iovcnt) { + static ssize_t (*real)(int, const struct iovec *, int); + if (iovcnt >= 0 && bl_nl_owns(fd)) { + struct msghdr msg = { .msg_iov = (struct iovec *) iov, .msg_iovlen = (size_t) iovcnt }; + return sendmsg_request(fd, &msg, 0); + } + if (real == NULL) real = (ssize_t (*)(int, const struct iovec *, int)) dlsym(RTLD_NEXT, "writev"); + if (real == NULL) { + errno = ENOSYS; + return -1; + } + return real(fd, iov, iovcnt); +} diff --git a/tools/linuxfs/preload/pathcache.c b/tools/linuxfs/preload/pathcache.c index 928fd7d24d..59f2cbf069 100644 --- a/tools/linuxfs/preload/pathcache.c +++ b/tools/linuxfs/preload/pathcache.c @@ -111,6 +111,8 @@ char *bl_cached_realpath(const char *path, char *resolved, char *(*real)(const c return strdup(out); } +int bl_dircache_absent(const char *path) __attribute__((weak, visibility("hidden"))); + static int cached_access(const char *path, int mode, int flags, int (*check)(const char *, int, int)) { unsigned h; int hit; @@ -122,6 +124,10 @@ static int cached_access(const char *path, int mode, int flags, int (*check)(con hit = find(path, KIND_ACCESS, mode, flags, h) != NULL; pthread_mutex_unlock(&lock); if (hit) return 0; + if (bl_dircache_absent && bl_dircache_absent(path)) { + errno = ENOENT; + return -1; + } result = check(path, mode, flags); if (result == 0) { pthread_mutex_lock(&lock); diff --git a/tools/linuxfs/preload/tracer.c b/tools/linuxfs/preload/tracer.c index 572cf13e09..7f5ac8e8cd 100644 --- a/tools/linuxfs/preload/tracer.c +++ b/tools/linuxfs/preload/tracer.c @@ -81,7 +81,10 @@ __attribute__((visibility("hidden"))) int bl_status_without_tracer(const char *p long pid; if ((flags & O_ACCMODE) != O_RDONLY || (pid = status_pid(path)) < 0 || is_web_helper(caller) || bl_from_ntdll(caller)) return -1; - int in = (int)syscall(SYS_openat, AT_FDCWD, path, O_RDONLY | O_CLOEXEC); + // through the next open(): the native userland's hook gives the app's ids as root's there + static int (*next_open)(const char *, int, ...) = NULL; + if (!next_open) next_open = (int (*)(const char *, int, ...))dlsym(RTLD_NEXT, "open"); + int in = next_open ? next_open(path, O_RDONLY | O_CLOEXEC) : (int)syscall(SYS_openat, AT_FDCWD, path, O_RDONLY | O_CLOEXEC); if (in < 0) goto done; for (;;) { ssize_t n = read(in, text + len, sizeof(text) - 1 - len); diff --git a/tools/linuxfs/preload/webhelper.c b/tools/linuxfs/preload/webhelper.c index 9494872797..7642a34632 100644 --- a/tools/linuxfs/preload/webhelper.c +++ b/tools/linuxfs/preload/webhelper.c @@ -26,6 +26,14 @@ static char *join_features(const char *have, const char *add) { return strcat(out, add); } +static int electron_node_mode(char *const envp[]) { + static const char variable[] = "ELECTRON_RUN_AS_NODE="; + for (int i = 0; envp && envp[i]; i++) + if (!strncmp(envp[i], variable, sizeof variable - 1)) + return envp[i][sizeof variable - 1] != 0; + return 0; +} + static char *const *chromium_argv(const char *path, char *const argv[]) { const char *slash = path ? strrchr(path, '/') : NULL; if (!slash || !argv || !argv[0]) return argv; @@ -48,7 +56,9 @@ static char *const *chromium_argv(const char *path, char *const argv[]) { return out; } -__attribute__((visibility("hidden"))) char *const *bl_webhelper_argv(const char *path, char *const argv[]) { +__attribute__((visibility("hidden"))) +char *const *bl_webhelper_argv(const char *path, char *const argv[], char *const envp[]) { + if (electron_node_mode(envp)) return argv; if (!is_webhelper_browser(path, argv)) return chromium_argv(path, argv); const char *spec = getenv("BL_WEBHELPER_ARGS"); if (!spec || !*spec) spec = WEBHELPER_DEFAULT; diff --git a/tools/nativeguest/LICENSE b/tools/nativeguest/LICENSE new file mode 100644 index 0000000000..d645695673 --- /dev/null +++ b/tools/nativeguest/LICENSE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/tools/nativeguest/README.md b/tools/nativeguest/README.md new file mode 100644 index 0000000000..19571de96c --- /dev/null +++ b/tools/nativeguest/README.md @@ -0,0 +1,34 @@ +# Native userland (no proot) + +`libddnatv-hook.so` is an `LD_PRELOAD` library that lets the glibc rootfs run directly on Android: +it maps the guest's absolute paths into the rootfs (plus the mounts proot used to provide, from +`DDNATV_BINDS`), starts every program through the rootfs's `ld.so`, and answers the system calls +Android's seccomp policy kills a process for (`SIGSYS` handler). There is no tracer. The app side +is `runtime/NativeGuest.kt`; the setting is Performance > "Run without proot (experimental)". + +The hook is derived from the Apache-2.0 `libcortex-hook` of +[ssfuisu/cortex](https://github.com/ssfuisu/cortex) (see `LICENSE`), renamed (`cortex` -> `ddnatv`, +same length: the code compares environment names by length) and extended with: + +- `DDNATV_BINDS` mounts, `DDNATV_PRELOAD` (what `/etc/ld.so.preload` does for proot), and mapping of + guest paths inside `LD_PRELOAD`; +- POSIX `shm_open`/`shm_unlink` through `/dev/shm`; +- starting Wine without its static preloader, which opens files with raw system calls. + +Build: `tools/nativeguest/build.sh out.so` with an aarch64 glibc cross compiler. + +## Testing under Android's seccomp filter + +Some devices (eng builds, `setenforce 0` ROMs) run apps with no seccomp filter, so a hook that +passes there can still die with SIGSYS on a stock phone (`GUEST_EXIT 159`). `seccomp-sim.c` applies +a filter that answers the calls Android blocks with SIGSYS, then runs the program: + +``` +aarch64-linux-gnu-gcc -static -O2 -o seccomp-sim tools/nativeguest/seccomp-sim.c +# copy it into the rootfs, then name it for every native launch: +echo /usr/local/bin/seccomp-sim > /etc/droiddeck/seccomp-sim +``` + +`grep Seccomp: /proc//status` should then show `2` for the session's processes. Empty the file +to turn it off. `seccomp-sim -a NR,NR PROGRAM` leaves the listed calls allowed, to find which one +kills a program. diff --git a/tools/nativeguest/app-seccomp-dump.c b/tools/nativeguest/app-seccomp-dump.c new file mode 100644 index 0000000000..1ec09ea256 --- /dev/null +++ b/tools/nativeguest/app-seccomp-dump.c @@ -0,0 +1,50 @@ +// Writes the seccomp filter Android's zygote gives an app (from the device's libandroid_runtime.so) +// to a file, as the raw sock_filter array, for seccomp-sim -f on devices whose zygote skips it +// (a permissive SELinux). A bionic program, built with the NDK and run from adb shell: +// aarch64-linux-android33-clang -O2 -rdynamic -o app-seccomp-dump app-seccomp-dump.c -ldl +// app-seccomp-dump OUT +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static const char *g_out; +static int g_dumped; + +// the runtime's own prctl() call lands here first +__attribute__((visibility("default"))) int prctl(int option, ...) { + va_list ap; + va_start(ap, option); + unsigned long a2 = va_arg(ap, unsigned long), a3 = va_arg(ap, unsigned long); + unsigned long a4 = va_arg(ap, unsigned long), a5 = va_arg(ap, unsigned long); + va_end(ap); + if (option == PR_SET_SECCOMP && a2 == SECCOMP_MODE_FILTER && g_out) { + const struct sock_fprog *prog = (const struct sock_fprog *)a3; + FILE *f = fopen(g_out, "wb"); + if (f) { + fwrite(prog->filter, sizeof(prog->filter[0]), prog->len, f); + fclose(f); + fprintf(stderr, "app-seccomp-dump: %u instructions to %s\n", prog->len, g_out); + g_dumped = 1; + } + return 0; // not installed in this process + } + return (int)syscall(SYS_prctl, option, a2, a3, a4, a5); +} + +int main(int argc, char **argv) { + if (argc != 2) { fprintf(stderr, "usage: %s OUT\n", argv[0]); return 2; } + g_out = argv[1]; + void *rt = dlopen("libandroid_runtime.so", RTLD_NOW); + if (!rt) { fprintf(stderr, "%s\n", dlerror()); return 2; } + int (*set_app_filter)(void) = (int (*)(void))dlsym(rt, "_Z22set_app_seccomp_filterv"); + if (!set_app_filter) { fprintf(stderr, "no set_app_seccomp_filter\n"); return 2; } + set_app_filter(); + return g_dumped ? 0 : 1; +} diff --git a/tools/nativeguest/build-gosys.sh b/tools/nativeguest/build-gosys.sh new file mode 100755 index 0000000000..ce05326c1e --- /dev/null +++ b/tools/nativeguest/build-gosys.sh @@ -0,0 +1,82 @@ +#!/usr/bin/env bash +# Builds the code the native userland adds to static Go programs (gosys/gosys.c) into the C header +# the hook includes (gosys/gosys_blob.h, not kept in the repository; build.sh makes it each time). +# +# The code is AArch64 whatever the machine this runs on: it is compiled freestanding with an +# aarch64 cross compiler (gcc-aarch64-linux-gnu: the one that builds the hook; or clang with lld). +# +# usage: tools/nativeguest/build-gosys.sh [output header] +set -euo pipefail + +here=$(cd "$(dirname "$0")" && pwd) +src=$here/gosys +out=${1:-$src/gosys_blob.h} + +if command -v aarch64-linux-gnu-gcc >/dev/null 2>&1; then + CC=(aarch64-linux-gnu-gcc) + LD=(aarch64-linux-gnu-ld) + OBJCOPY=(aarch64-linux-gnu-objcopy) + NM=(aarch64-linux-gnu-nm) + OBJDUMP=(aarch64-linux-gnu-objdump) +elif command -v clang >/dev/null 2>&1 && command -v ld.lld >/dev/null 2>&1; then + CC=(clang --target=aarch64-linux-gnu) + LD=(ld.lld) + OBJCOPY=(llvm-objcopy) + NM=(llvm-nm) + OBJDUMP=(llvm-objdump) +else + echo "build-gosys.sh: needs aarch64-linux-gnu-gcc (apt install gcc-aarch64-linux-gnu) or clang with lld" >&2 + exit 1 +fi + +tmp=$(mktemp -d) +trap 'rm -rf "$tmp"' EXIT + +"${CC[@]}" -std=gnu11 -O2 -Wall -Wextra -Werror \ + -ffreestanding -fno-builtin -nostdlib -fPIE -fvisibility=hidden -fno-stack-protector \ + -fno-asynchronous-unwind-tables -fno-unwind-tables -fno-tree-loop-distribute-patterns \ + -mcmodel=tiny -mgeneral-regs-only -mno-outline-atomics -ffixed-x18 -ffixed-x28 \ + -c "$src/gosys.c" -o "$tmp/gosys.o" +"${LD[@]}" -T "$src/gosys.ld" -nostdlib -o "$tmp/gosys.elf" "$tmp/gosys.o" +"${OBJCOPY[@]}" -O binary "$tmp/gosys.elf" "$tmp/gosys.bin" + +# It must stand on its own: nothing undefined, no writable data, and nothing that depends on the +# address it is loaded at (adrp assumes the page offset of the link address; adr does not) +if [ -n "$("${NM[@]}" -u "$tmp/gosys.elf")" ]; then + echo "build-gosys.sh: gosys.c needs symbols it does not define:" >&2 + "${NM[@]}" -u "$tmp/gosys.elf" >&2 + exit 1 +fi +if "${OBJDUMP[@]}" -d "$tmp/gosys.elf" | grep -q -w adrp; then + echo "build-gosys.sh: gosys.c uses adrp, which is not position-independent at any address" >&2 + exit 1 +fi +sections=$("${OBJDUMP[@]}" -h "$tmp/gosys.elf" | awk '$1 ~ /^[0-9]+$/ {print $2}' | tr '\n' ' ') +for s in $sections; do + case $s in + .hdr|.thunks|.text|.rodata|.trailer|.symtab|.strtab|.shstrtab) ;; + *) echo "build-gosys.sh: unexpected section $s in gosys.elf (sections: $sections)" >&2; exit 1 ;; + esac +done + +size=$(stat -c %s "$tmp/gosys.bin") +entry=$("${NM[@]}" "$tmp/gosys.elf" | awk '$3 == "cxgo_entry" {print $1}') +thunks=$("${NM[@]}" "$tmp/gosys.elf" | awk '$3 == "g_thunks" {print $1}') +if [ -z "$entry" ] || [ -z "$thunks" ]; then + echo "build-gosys.sh: cxgo_entry or g_thunks not found" >&2 + exit 1 +fi +[ "$(head -c 8 "$tmp/gosys.bin")" = "CXGOSYS1" ] || { echo "build-gosys.sh: the header is not first" >&2; exit 1; } +[ "$(tail -c 64 "$tmp/gosys.bin" | head -c 8)" = "CXGOEND1" ] || { echo "build-gosys.sh: the trailer is not last" >&2; exit 1; } + +{ + echo "// Generated by tools/nativeguest/build-gosys.sh from gosys/gosys.c. Do not edit." + echo "#define GOSYS_BLOB_SIZE $size" + echo "#define GOSYS_ENTRY_OFF 0x$entry" + echo "#define GOSYS_THUNKS_OFF 0x$thunks" + echo "static const unsigned char gosys_blob[GOSYS_BLOB_SIZE] = {" + od -An -v -tx1 "$tmp/gosys.bin" | sed -e 's/^ *//' -e 's/ /, 0x/g' -e 's/^/0x/' -e 's/$/,/' + echo "};" +} > "$out.tmp" +mv "$out.tmp" "$out" +echo "build-gosys.sh: $out ($size bytes, entry 0x$entry)" diff --git a/tools/nativeguest/build.sh b/tools/nativeguest/build.sh new file mode 100755 index 0000000000..46002f248c --- /dev/null +++ b/tools/nativeguest/build.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash +# Builds libddnatv-hook.so, the library that lets the glibc rootfs run on Android without proot. +# usage: tools/nativeguest/build.sh [compiler, default aarch64-linux-gnu-gcc] +set -euo pipefail +here=$(cd "$(dirname "$0")" && pwd) +out=${1:?output path} +cc=${2:-aarch64-linux-gnu-gcc} +gen=$(mktemp -d) +trap 'rm -rf "$gen"' EXIT +mkdir -p "$gen/gosys" +bash "$here/build-gosys.sh" "$gen/gosys/gosys_blob.h" +"$cc" -std=gnu11 -shared -fPIC -O2 -fno-delete-null-pointer-checks -Wall -Wno-nonnull-compare \ + -Wno-format-truncation -Wno-stringop-truncation -Wl,-z,max-page-size=16384 -I "$gen" \ + -o "$out" "$here/ddnatv-hook.c" -ldl +"${cc%gcc}strip" --strip-unneeded "$out" diff --git a/tools/nativeguest/ddnatv-hook.c b/tools/nativeguest/ddnatv-hook.c new file mode 100644 index 0000000000..954991c261 --- /dev/null +++ b/tools/nativeguest/ddnatv-hook.c @@ -0,0 +1,87 @@ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifndef RENAME_EXCHANGE +#define RENAME_EXCHANGE (1 << 1) +#endif + +// The code that is added to static Go programs (gosys/gosys.c), built by scripts/build-gosys.sh. A +// build without it works as before, only without Go support. It is AArch64 code: the 32-bit hook +// has none (a 32-bit phone cannot run 64-bit programs). +#if defined(__has_include) && defined(__LP64__) +#if __has_include("gosys/gosys_blob.h") +#include "gosys/gosys_abi.h" +#include "gosys/gosys_blob.h" +#define DDNATV_HAVE_GOSYS 1 +#endif +#endif + +// The hook is one translation unit split into parts by topic, in this order (later parts use +// the static functions of earlier ones). +#include "hook/syscalls.c" +#include "hook/paths.c" +#include "hook/hardlinks.c" +#include "hook/signals.c" +#include "hook/setup.c" +#include "hook/files.c" +#include "hook/fileops.c" +#include "hook/identity.c" +#include "hook/privileged.c" +#include "hook/env.c" +#include "hook/exec_probe.c" +#include "hook/static_programs.c" +#include "hook/go_programs.c" +#include "hook/exec.c" +#include "hook/net.c" +#include "hook/sockets.c" +#include "hook/sysv_shm.c" +#include "hook/posix_shm.c" diff --git a/tools/nativeguest/gosys/gosys.c b/tools/nativeguest/gosys/gosys.c new file mode 100644 index 0000000000..fb8230ce4a --- /dev/null +++ b/tools/nativeguest/gosys/gosys.c @@ -0,0 +1,510 @@ +// The code The native userland adds to a static Go program so that it sees the rootfs (see gosys_abi.h). +// +// Built freestanding (scripts/build-gosys.sh) as a flat, position-independent binary with no +// writable data, no libc and no relocations: the hook copies it into the program's file. It is +// entered from the one place where Go's system calls are made, in place of the "svc #0" of +// internal/runtime/syscall.Syscall6, with the registers of that call: arguments in x0 to x5, +// the number in x8 (passed again as the eighth argument). It returns what the kernel would, and it +// may use every register but x28 (the current goroutine), x29, x30 and sp, which it keeps. +// +// It does for Go what libddnatv-hook.so does for C programs: maps absolute Ubuntu paths into the +// rootfs (the same rules as hook/paths.c), shortens the Unix socket paths that do not fit, shows +// the program the paths it expects from getcwd and /proc/self/exe, and starts Ubuntu programs +// through the rootfs's dynamic linker and env, which then go through the hook. + +#include "gosys_abi.h" + +typedef int64_t i64; +typedef uint64_t u64; +typedef uint32_t u32; +typedef uint16_t u16; + +#define AT_FDCWD (-100) +#define SLOT_PATH_A 0u +#define SLOT_PATH_B 4096u +#define SLOT_ARGV 8192u +#define SLOT_ENV 16384u +#define SLOT_PATH_CAP 4096u +#define ARGV_CAP 1000u // entries of the argv array that starts a program +#define ENV_CAP 2000u + +// The numbers of AArch64 Linux (the same for every kernel) +enum { + NR_SETXATTR = 5, NR_LSETXATTR = 6, NR_GETXATTR = 8, NR_LGETXATTR = 9, NR_LISTXATTR = 11, + NR_LLISTXATTR = 12, NR_REMOVEXATTR = 14, NR_LREMOVEXATTR = 15, NR_GETCWD = 17, + NR_INOTIFY_ADD_WATCH = 27, NR_MKNODAT = 33, NR_MKDIRAT = 34, NR_UNLINKAT = 35, NR_SYMLINKAT = 36, + NR_LINKAT = 37, NR_RENAMEAT = 38, NR_STATFS = 43, NR_TRUNCATE = 45, NR_FACCESSAT = 48, + NR_CHDIR = 49, NR_CHROOT = 51, NR_FCHMODAT = 53, NR_FCHOWNAT = 54, NR_OPENAT = 56, + NR_READLINKAT = 78, NR_NEWFSTATAT = 79, NR_UTIMENSAT = 88, NR_BIND = 200, NR_CONNECT = 203, + NR_EXECVE = 221, NR_NAME_TO_HANDLE_AT = 264, NR_RENAMEAT2 = 276, NR_STATX = 291, + NR_OPENAT2 = 437, NR_FACCESSAT2 = 439, NR_FCHMODAT2 = 452, +}; + +__attribute__((section(".hdr"), used)) +static const volatile struct gosys_hdr g_hdr = { .magic = "CXGOSYS1", .version = GOSYS_VERSION }; + +// Filled by the patcher; the instructions that connect the call site to cxgo_entry +__attribute__((section(".thunks"), used)) +static const volatile u32 g_thunks[GOSYS_THUNK_BYTES / 4]; + +__attribute__((section(".trailer"), used)) +static const volatile struct gosys_trailer g_trailer = { .magic = "CXGOEND1", .version = GOSYS_VERSION }; + +static inline const struct gosys_hdr *hdr(void) { + const struct gosys_hdr *h = (const struct gosys_hdr *)&g_hdr; + __asm__("" : "+r"(h)); // the compiler must not assume it knows the contents + return h; +} + +static inline i64 sc(i64 nr, i64 a0, i64 a1, i64 a2, i64 a3, i64 a4, i64 a5) { + register i64 x8 __asm__("x8") = nr; + register i64 x0 __asm__("x0") = a0; + register i64 x1 __asm__("x1") = a1; + register i64 x2 __asm__("x2") = a2; + register i64 x3 __asm__("x3") = a3; + register i64 x4 __asm__("x4") = a4; + register i64 x5 __asm__("x5") = a5; + __asm__ volatile("svc #0" : "+r"(x0) : "r"(x8), "r"(x1), "r"(x2), "r"(x3), "r"(x4), "r"(x5) : "memory", "cc"); + return x0; +} + +static u64 slen(const char *s) { + u64 n = 0; + while (s[n]) n++; + return n; +} + +static void mcopy(char *d, const char *s, u64 n) { + for (u64 i = 0; i < n; i++) d[i] = s[i]; +} + +// Moves n bytes down (d < s) +static void mmove_down(char *d, const char *s, u64 n) { + for (u64 i = 0; i < n; i++) d[i] = s[i]; +} + +static int pre(const char *path, const char *lit, u64 n) { + for (u64 i = 0; i < n; i++) + if (path[i] != lit[i]) return 0; + return path[n] == '/' || path[n] == '\0'; +} + +// Whether path is the rootfs or in it +static int in_root(const struct gosys_hdr *h, const char *p) { + u64 n = h->root_len; + if (n == 0) return 0; + for (u64 i = 0; i < n; i++) + if (p[i] != h->root[i]) return 0; + return p[n] == '/' || p[n] == '\0'; +} + +// Android's own mounts and files, which must never be redirected into the rootfs (hook/paths.c) +static int is_host_path(const char *path) { + switch (path[1]) { + case 'a': return pre(path, "/apex", 5); + case 'c': return pre(path, "/config", 7); + case 'd': return pre(path, "/dev", 4) || pre(path, "/d", 2) || pre(path, "/data", 5); + case 'l': return pre(path, "/linkerconfig", 13); + case 'o': return pre(path, "/odm", 4) || pre(path, "/oem", 4); + case 'p': return pre(path, "/proc", 5) || pre(path, "/product", 8); + case 's': return pre(path, "/sbin/su", 8) || pre(path, "/su", 3) || pre(path, "/sys", 4) || + pre(path, "/system", 7) || pre(path, "/system_ext", 11) || + pre(path, "/sdcard", 7) || pre(path, "/storage", 8); + case 'v': return pre(path, "/vendor", 7); + default: return 0; + } +} + +// An absolute Ubuntu path mapped into the rootfs: `path` itself when it needs no change, else buf +// (hook/paths.c rewrite_path_in_root). A result that would not fit is the path unchanged: the +// kernel then answers as it would for a path that long. +static const char *rw_path(const struct gosys_hdr *h, const char *path, char *buf, u64 cap) { + if (path[0] != '/' || h->root_len == 0) return path; + while (path[0] == '/' && path[1] == '/') path++; + while (path[0] == '/' && path[1] == '.' && (path[2] == '/' || path[2] == '\0')) { + if (path[2] == '/') { + path += 2; + } else { + path = "/"; + break; + } + } + u64 rl = h->root_len; + if (path[1] == '\0') { + mcopy(buf, h->root, rl); + buf[rl] = '\0'; + return buf; + } + if (in_root(h, path)) return path; + if (pre(path, "/dev/shm", 8)) { + u64 rest = slen(path + 8); + if (rl + 8 + rest + 1 > cap) return path; + mcopy(buf, h->root, rl); + mcopy(buf + rl, "/tmp/shm", 8); + mcopy(buf + rl + 8, path + 8, rest + 1); + return buf; + } + if (is_host_path(path)) return path; + u64 pl = slen(path); + if (rl + pl + 1 > cap) return path; + mcopy(buf, h->root, rl); + mcopy(buf + rl, path, pl + 1); + return buf; +} + +// Whether the rootfs is where the code was told it is. In root mode the program runs in a chroot of +// that very rootfs, where the real paths are the Ubuntu ones and the rootfs's own path does not +// exist: then nothing is mapped and the program works as any other. Looked at once for each process +// (the scratch memory starts as zero, and a forked child keeps what its parent found). +static int rewriting(const struct gosys_hdr *h) { + volatile u32 *mode = (volatile u32 *)((char *)h + h->scratch_rel + 4); + u32 m = *mode; + if (m == 0) { + m = sc(NR_FACCESSAT, AT_FDCWD, (i64)h->root, 0, 0, 0, 0) == 0 ? 1u : 2u; + *mode = m; + } + return m == 1; +} + +#define NO_SLOT 0xffffffffu +#define NR_SCHED_YIELD 124 + +// Who is making the call: the goroutine (x28 holds it, and the code is built not to touch x28). A +// goroutine makes one call at a time, and the vfork child that a goroutine makes runs with the same +// x28 as the goroutine does. (TPIDR_EL0 does not tell the threads of a Go program apart: its runtime +// leaves it unset without cgo.) +static inline u64 thread_tag(void) { + u64 t; + __asm__ volatile("mov %0, x28" : "=r"(t)); + return t; +} + +// How many execve calls have not returned (state area, offset 8). One that works never does: it is +// made by a vfork child, and the slot its path was in stays marked as the goroutine's, which goes on +// when the child is gone (the same x28, a call of its own). Without this, goroutines that each +// started a program held a slot each for good, and after the sixty-fourth all the others waited. +static inline u32 *execs_on_their_way(const struct gosys_hdr *h) { + return (u32 *)((char *)h + h->scratch_rel + 8); +} + +static void count_down(u32 *count) { + u32 n = __atomic_load_n(count, __ATOMIC_RELAXED); + while (n != 0 && !__atomic_compare_exchange_n(count, &n, n - 1, 0, __ATOMIC_RELAXED, __ATOMIC_RELAXED)) { + } +} + +// Gives back what this goroutine's vfork child left (see above). Every call looks at one counter +// for it; the list of slots is only looked through while an execve is on its way. +static void reclaim_left_slots(const struct gosys_hdr *h) { + u32 *count = execs_on_their_way(h); + if (__atomic_load_n(count, __ATOMIC_RELAXED) == 0) return; + u64 *owner = (u64 *)((char *)h + h->scratch_rel + 64); + u64 me = thread_tag() | 1; + for (u32 i = 0; i < GOSYS_SLOTS; i++) { + if (__atomic_load_n(&owner[i], __ATOMIC_RELAXED) != me) continue; + __atomic_store_n(&owner[i], 0, __ATOMIC_RELEASE); + count_down(count); + } +} + +// A slot for the call this goroutine is making; *held is its number, to give back when the call is +// done. Each slot has a word of its own that holds who has it (0: nobody), so that taking one is one +// compare-and-swap, and no other goroutine can ever mistake it for its own. +static char *take_slot(const struct gosys_hdr *h, u32 *held) { + char *base = (char *)h + h->scratch_rel; + u64 *owner = (u64 *)(base + 64); + u64 me = thread_tag() | 1; // never 0 + u32 mine = NO_SLOT; + // A slot of this goroutine that is still taken now (cxgo_entry gives back those of its vfork + // children) is its own again + for (u32 i = 0; i < GOSYS_SLOTS; i++) { + if (__atomic_load_n(&owner[i], __ATOMIC_RELAXED) != me) continue; + if (mine == NO_SLOT) + mine = i; + else + __atomic_store_n(&owner[i], 0, __ATOMIC_RELEASE); + } + while (mine == NO_SLOT) { + for (u32 i = 0; i < GOSYS_SLOTS && mine == NO_SLOT; i++) { + u64 none = 0; + if (__atomic_load_n(&owner[i], __ATOMIC_RELAXED) == 0 && + __atomic_compare_exchange_n(&owner[i], &none, me, 0, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED)) + mine = i; + } + if (mine == NO_SLOT) sc(NR_SCHED_YIELD, 0, 0, 0, 0, 0, 0); // all taken: the others finish soon + } + *held = mine; + return base + GOSYS_STATE_SIZE + (u64)mine * GOSYS_SLOT_SIZE; +} + +static void put_slot(const struct gosys_hdr *h, u32 held) { + if (held == NO_SLOT) return; + char *base = (char *)h + h->scratch_rel; + __atomic_store_n((u64 *)(base + 64) + held, 0, __ATOMIC_RELEASE); +} + +// A path as the program expects to see it, from one of the kernel's: the rootfs prefix removed. +// Returns the new length of the n bytes at buf (a path, not NUL-terminated unless `nul`). +static i64 unmap_prefix(const struct gosys_hdr *h, char *buf, i64 n, int nul) { + u64 rl = h->root_len; + if (rl == 0 || n <= 0 || (u64)n < rl + (nul ? 1 : 0)) return n; + for (u64 i = 0; i < rl; i++) + if (buf[i] != h->root[i]) return n; + u64 end = (u64)n - (nul ? 1 : 0); // the path's own length + if (end == rl) { // exactly the rootfs + buf[0] = '/'; + if (nul) { + buf[1] = '\0'; + return 2; + } + return 1; + } + if (buf[rl] != '/') return n; + mmove_down(buf, buf + rl, (u64)n - rl); + return n - (i64)rl; +} + +// ---- Unix sockets ------------------------------------------------------------------------ + +static void put_hex16(char *out, u64 v) { + static const char digits[] = "0123456789abcdef"; + for (int i = 15; i >= 0; i--) { + out[i] = digits[v & 15]; + v >>= 4; + } +} + +// bind() and connect() of a Unix socket with a path: mapped like hook/setup.c does, and hashed +// into /tmp when it does not fit the 108 bytes of sun_path (the same name, so a Go server and a +// program using the C library find each other) +static i64 with_sockaddr(const struct gosys_hdr *h, u32 *held, i64 nr, i64 a0, i64 a1, i64 a2) { + const unsigned char *sa = (const unsigned char *)a1; + if (!sa || a2 < 4 || a2 > 128 || sa[0] != 1 /* AF_UNIX */ || sa[1] != 0 || sa[2] != '/') + return sc(nr, a0, a1, a2, 0, 0, 0); + char *slot = take_slot(h, held); + char *orig = slot + SLOT_PATH_A; + u64 max = (u64)a2 - 2; + if (max > 108) max = 108; + mcopy(orig, (const char *)sa + 2, max); + orig[max] = '\0'; + const char *rw = rw_path(h, orig, slot + SLOT_PATH_B, SLOT_PATH_CAP); + u64 rl = slen(rw); + char *out = slot + SLOT_ARGV; + out[0] = 1; + out[1] = 0; + if (rl < 108) { + mcopy(out + 2, rw, rl + 1); + return sc(nr, a0, (i64)out, (i64)(2 + rl + 1), 0, 0, 0); + } + u64 hash = 14695981039346656037ULL; + for (const char *p = rw; *p; p++) { + hash ^= (unsigned char)*p; + hash *= 1099511628211ULL; + } + u64 n = h->root_len; + if (n + 5 + 11 + 16 + 1 > 108) return sc(nr, a0, a1, a2, 0, 0, 0); + mcopy(out + 2, h->root, n); + mcopy(out + 2 + n, "/tmp/.ctx_sock_", 15); + put_hex16(out + 2 + n + 15, hash); + out[2 + n + 15 + 16] = '\0'; + return sc(nr, a0, (i64)out, (i64)(2 + n + 15 + 16 + 1), 0, 0, 0); +} + +// ---- Starting programs ------------------------------------------------------------------- + +static int starts_with(const char *s, const char *lit) { + for (u64 i = 0; lit[i]; i++) + if (s[i] != lit[i]) return 0; + return 1; +} + +// A program of Android's gets the environment it expects, as from hook/env.c clean_env_for_system(): +// without the hook and the rootfs's libraries, and with Android's directories in its PATH +static i64 exec_host(const struct gosys_hdr *h, char *slot, const char *p, i64 a1, char *const *envp) { + (void)h; + static const char android_path[] = "PATH=/system/bin:/system/xbin:/sbin:/vendor/bin"; + u32 envc = 0; + while (envp && envp[envc]) envc++; + if (envc + 2 > ENV_CAP) return sc(NR_EXECVE, (i64)p, a1, (i64)envp, 0, 0, 0); + char **ne = (char **)(slot + SLOT_ENV); + char *path_str = slot + SLOT_PATH_B; + u32 n = 0; + int has_path = 0; + for (u32 i = 0; i < envc; i++) { + char *e = envp[i]; + if (starts_with(e, "LD_PRELOAD=") || starts_with(e, "LD_LIBRARY_PATH=") || starts_with(e, "GLIBC_TUNABLES=")) + continue; + if (starts_with(e, "PATH=")) { + u64 len = slen(e); + has_path = 1; + if (len + sizeof(android_path) + 1 < SLOT_PATH_CAP) { + mcopy(path_str, android_path, sizeof(android_path) - 1); + path_str[sizeof(android_path) - 1] = ':'; + mcopy(path_str + sizeof(android_path), e + 5, len - 5 + 1); + ne[n++] = path_str; + continue; + } + } + ne[n++] = e; + } + if (!has_path) ne[n++] = (char *)android_path; + ne[n] = 0; + return sc(NR_EXECVE, (i64)p, a1, (i64)ne, 0, 0, 0); +} + +// execve(): a program of the rootfs is started as +// --preload --library-path -c 'exec -a "$0" "$@"' ... +// so that bash, a program of the rootfs with the hook loaded, starts it the way every program of +// The native userland is started (Ubuntu's dynamic programs through ld.so, scripts through their interpreter, +// static programs patched first), with the argv[0] the caller chose. Without bash it is env, +// which cannot set argv[0]. The path is passed as the caller wrote it. +static i64 with_execve(const struct gosys_hdr *h, u32 *held, i64 a0, i64 a1, i64 a2) { + const char *path = (const char *)a0; + char *const *argv = (char *const *)a1; + char *const *envp = (char *const *)a2; + if (!path || path[0] == '\0') return sc(NR_EXECVE, a0, a1, a2, 0, 0, 0); + + char *slot = take_slot(h, held); + const char *p = path[0] == '/' ? rw_path(h, path, slot + SLOT_PATH_A, SLOT_PATH_CAP) : path; + int wrap = h->ldso_len != 0 && h->runner_len != 0 && (path[0] != '/' || in_root(h, p)); + if (!wrap) { + if (path[0] == '/' && !in_root(h, p)) return exec_host(h, slot, p, a1, envp); + return sc(NR_EXECVE, (i64)p, a1, a2, 0, 0, 0); + } + + // A program that is not there is reported to the caller, as execve() does + i64 r = sc(NR_FACCESSAT, AT_FDCWD, (i64)p, 0, 0, 0, 0); + if (r < 0) return r; + + u32 argc = 0; + while (argv && argv[argc]) argc++; + if (argc + 12 > ARGV_CAP) return sc(NR_EXECVE, (i64)p, a1, a2, 0, 0, 0); + + // DDNATV_ROOT must reach env even when the caller built an environment of its own + char *const *env = envp; + u32 envc = 0; + int has_root = 0; + while (envp && envp[envc]) { + const char *e = envp[envc]; + if (e[0] == 'D' && e[1] == 'D' && e[2] == 'N' && e[3] == 'A' && e[4] == 'T' && e[5] == 'V' && + e[6] == '_' && e[7] == 'R' && e[8] == 'O' && e[9] == 'O' && e[10] == 'T' && e[11] == '=') + has_root = 1; + envc++; + } + if (!has_root) { + if (envc + 2 > ENV_CAP) return sc(NR_EXECVE, (i64)p, a1, a2, 0, 0, 0); + char **ne = (char **)(slot + SLOT_ENV); + char *kv = slot + SLOT_PATH_B; + mcopy(kv, "DDNATV_ROOT=", 12); + mcopy(kv + 12, h->root, h->root_len + 1); + for (u32 i = 0; i < envc; i++) ne[i] = envp[i]; + ne[envc] = kv; + ne[envc + 1] = 0; + env = ne; + } + + char **v = (char **)(slot + SLOT_ARGV); + u32 n = 0; + v[n++] = (char *)h->ldso; + if (h->hook_len) { + v[n++] = (char *)"--preload"; + v[n++] = (char *)h->hook; + } + v[n++] = (char *)"--library-path"; + v[n++] = (char *)h->libs; + v[n++] = (char *)h->runner; + if (h->flags & GOSYS_FLAG_RUNNER_IS_BASH) { + v[n++] = (char *)"-c"; + v[n++] = (char *)"exec -a \"$0\" \"$@\""; + v[n++] = (argc > 0 && argv[0]) ? argv[0] : (char *)path; + } else { + v[n++] = (char *)"--"; + } + v[n++] = (char *)path; + for (u32 i = 1; i < argc; i++) v[n++] = argv[i]; + v[n] = 0; + return sc(NR_EXECVE, (i64)h->ldso, (i64)v, (i64)env, 0, 0, 0); +} + +// ---- The entry --------------------------------------------------------------------------- + +// Rewrites the path arguments `i1` and `i2` (an index into a[], -1: none) that are absolute, makes +// the call, and shows the program the paths it expects in what the call returns. +static i64 with_paths(const struct gosys_hdr *h, u32 *held, i64 nr, i64 *a, int i1, int i2) { + char *slot = 0; + int proc_link = 0; + int idx[2] = { i1, i2 }; + for (int k = 0; k < 2; k++) { + int i = idx[k]; + if (i < 0) continue; + const char *p = (const char *)a[i]; + if (!p || p[0] != '/') continue; + if (!slot) slot = take_slot(h, held); + if (nr == NR_READLINKAT && k == 0 && pre(p, "/proc", 5)) proc_link = 1; + a[i] = (i64)rw_path(h, p, slot + (k ? SLOT_PATH_B : SLOT_PATH_A), SLOT_PATH_CAP); + } + i64 r = sc(nr, a[0], a[1], a[2], a[3], a[4], a[5]); + if (proc_link && r > 0) r = unmap_prefix(h, (char *)a[2], r, 0); + return r; +} + +// The call itself; a slot that it takes is given back by cxgo_entry +static i64 handle(const struct gosys_hdr *h, u32 *held, i64 nr, i64 a0, i64 a1, i64 a2, i64 a3, i64 a4, i64 a5) { + i64 a[6] = { a0, a1, a2, a3, a4, a5 }; + switch (nr) { + case NR_OPENAT: case NR_FACCESSAT: case NR_FACCESSAT2: case NR_FCHMODAT: case NR_FCHMODAT2: + case NR_FCHOWNAT: case NR_NEWFSTATAT: case NR_STATX: case NR_READLINKAT: case NR_MKDIRAT: + case NR_MKNODAT: case NR_UNLINKAT: case NR_UTIMENSAT: case NR_OPENAT2: case NR_NAME_TO_HANDLE_AT: + case NR_INOTIFY_ADD_WATCH: case NR_CHDIR: case NR_CHROOT: case NR_STATFS: case NR_TRUNCATE: + case NR_SETXATTR: case NR_LSETXATTR: case NR_GETXATTR: case NR_LGETXATTR: case NR_LISTXATTR: + case NR_LLISTXATTR: case NR_REMOVEXATTR: case NR_LREMOVEXATTR: case NR_RENAMEAT: case NR_RENAMEAT2: + case NR_LINKAT: case NR_SYMLINKAT: case NR_GETCWD: case NR_BIND: case NR_CONNECT: case NR_EXECVE: + // The calls that carry paths: not mapped where the rootfs is not in its own place (above) + if (!rewriting(h)) return sc(nr, a0, a1, a2, a3, a4, a5); + break; + default: + return sc(nr, a0, a1, a2, a3, a4, a5); + } + switch (nr) { + case NR_OPENAT: case NR_FACCESSAT: case NR_FACCESSAT2: case NR_FCHMODAT: case NR_FCHMODAT2: + case NR_FCHOWNAT: case NR_NEWFSTATAT: case NR_STATX: case NR_READLINKAT: case NR_MKDIRAT: + case NR_MKNODAT: case NR_UNLINKAT: case NR_UTIMENSAT: case NR_OPENAT2: case NR_NAME_TO_HANDLE_AT: + case NR_INOTIFY_ADD_WATCH: + return with_paths(h, held, nr, a, 1, -1); + case NR_CHDIR: case NR_CHROOT: case NR_STATFS: case NR_TRUNCATE: case NR_SETXATTR: + case NR_LSETXATTR: case NR_GETXATTR: case NR_LGETXATTR: case NR_LISTXATTR: case NR_LLISTXATTR: + case NR_REMOVEXATTR: case NR_LREMOVEXATTR: + return with_paths(h, held, nr, a, 0, -1); + case NR_RENAMEAT: case NR_RENAMEAT2: case NR_LINKAT: + return with_paths(h, held, nr, a, 1, 3); + case NR_SYMLINKAT: // the target is data, only the link is a path + return with_paths(h, held, nr, a, 2, -1); + case NR_GETCWD: { + i64 r = sc(nr, a0, a1, 0, 0, 0, 0); + return r > 0 ? unmap_prefix(h, (char *)a0, r, 1) : r; + } + case NR_BIND: case NR_CONNECT: + return with_sockaddr(h, held, nr, a0, a1, a2); + case NR_EXECVE: { + // Counted while it is on its way: when it works it does not return (see execs_on_their_way) + u32 *count = execs_on_their_way(h); + __atomic_fetch_add(count, 1, __ATOMIC_RELAXED); + i64 r = with_execve(h, held, a0, a1, a2); + count_down(count); // it did return: it failed, and cxgo_entry gives its slot back + return r; + } + default: + return sc(nr, a0, a1, a2, a3, a4, a5); + } +} + +__attribute__((section(".text.cxgo_entry"), used, noinline)) +i64 cxgo_entry(i64 a0, i64 a1, i64 a2, i64 a3, i64 a4, i64 a5, i64 unused, i64 nr) { + (void)unused; + const struct gosys_hdr *h = hdr(); + u32 held = NO_SLOT; + reclaim_left_slots(h); + i64 r = handle(h, &held, nr, a0, a1, a2, a3, a4, a5); + put_slot(h, held); + return r; +} diff --git a/tools/nativeguest/gosys/gosys.ld b/tools/nativeguest/gosys/gosys.ld new file mode 100644 index 0000000000..ba5a6ba34f --- /dev/null +++ b/tools/nativeguest/gosys/gosys.ld @@ -0,0 +1,14 @@ +/* The layout of gosys.c's flat binary: the header at 0 (the patcher fills it in), the room for the + call site's connecting code, the code with its entry first, read-only data, the trailer last. */ +OUTPUT_FORMAT(elf64-littleaarch64) +ENTRY(cxgo_entry) +SECTIONS +{ + . = 0; + .hdr : { KEEP(*(.hdr)) } + .thunks : { . = ALIGN(16); KEEP(*(.thunks)) } + .text : { . = ALIGN(16); KEEP(*(.text.cxgo_entry)) *(.text .text.*) } + .rodata : { . = ALIGN(16); *(.rodata .rodata.*) } + .trailer : { . = ALIGN(64); KEEP(*(.trailer)) } + /DISCARD/ : { *(.comment) *(.note*) *(.eh_frame*) *(.gnu*) *(.interp) } +} diff --git a/tools/nativeguest/gosys/gosys_abi.h b/tools/nativeguest/gosys/gosys_abi.h new file mode 100644 index 0000000000..e0c4807d34 --- /dev/null +++ b/tools/nativeguest/gosys/gosys_abi.h @@ -0,0 +1,79 @@ +// What the hook and the code it adds to a static Go program (gosys.c) agree on. +// +// A Go program built without cgo makes its system calls itself, so the preloaded hook never sees +// them and Ubuntu's paths (/etc/resolv.conf, /usr/bin/git, ...) mean nothing to it. Before such a +// program starts, the hook adds a small piece of position-independent code to its file (see +// go_programs.c): the program's one system call function, internal/runtime/syscall.Syscall6, jumps +// to it, and it maps paths into the rootfs like the hook does, then makes the call. +// +// This header has no dependencies beyond , so that the freestanding code and the hook, +// which are built with different flags, can both include it. + +#ifndef DDNATV_GOSYS_ABI_H +#define DDNATV_GOSYS_ABI_H + +#include + +#ifndef GOSYS_VERSION +#define GOSYS_VERSION 3u // raised when the code or its header changes: patched programs are patched again +#endif + +// The scratch memory for rewritten paths: a small state area and 64 slots, in zero-filled memory the +// patcher appends to the program's data segment (so nothing is allocated per call). A slot belongs to +// the thread that took it until its call is over: a word in the state area for each slot says +// which goroutine (x28) has it. A slot that a vfork child kept by a successful execve is given back +// at the next call of the goroutine that made the child (the state area counts the execve calls +// that have not returned, [8]). +// (A ring of slots shared by every call was tried first: a thread that was descheduled for a +// moment between taking a slot and the call let sixteen others go round the ring and overwrite its +// path, so a Go program opened another file than it asked for.) +#define GOSYS_SLOTS 64u +#define GOSYS_SLOT_SIZE 32768u // two paths (4K each), an argv array (8K), an environment array (16K) +#define GOSYS_STATE_SIZE 1024u // [4] rewriting mode, [8] execve calls on their way, [64 + 8 * slot] who has the slot (0: nobody) +#define GOSYS_SCRATCH_SIZE (GOSYS_STATE_SIZE + GOSYS_SLOTS * GOSYS_SLOT_SIZE) + +#define GOSYS_THUNK_BYTES 256u // room for the small code that connects a call site to the code + +#define GOSYS_ROOT_MAX 448 +#define GOSYS_PATH_MAX 448 +#define GOSYS_LIBS_MAX 1024 + +struct gosys_hdr { + char magic[8]; // "CXGOSYS1" + uint32_t version; // GOSYS_VERSION + uint32_t flags; + int64_t scratch_rel; // scratch memory, relative to this header (the same in any load address) + uint32_t root_len; // the rootfs, as Android sees it + uint32_t ldso_len; // the rootfs's dynamic linker + uint32_t hook_len; // libddnatv-hook.so; 0: none + uint32_t libs_len; // the rootfs's library directories, ':' separated + uint32_t runner_len; // the rootfs's bash, or env without it; 0: Ubuntu programs cannot be started + uint32_t reserved; + char root[GOSYS_ROOT_MAX]; + char ldso[GOSYS_PATH_MAX]; + char hook[GOSYS_PATH_MAX]; + char libs[GOSYS_LIBS_MAX]; + char runner[GOSYS_PATH_MAX]; +}; + +#define GOSYS_FLAG_RUNNER_IS_BASH 1u // gosys_hdr.flags + +// The last 64 bytes of the added code: where it is, and what the patcher changed to add it. +struct gosys_trailer { + char magic[8]; // "CXGOEND1" + uint32_t version; + uint32_t total_size; // the added code, header to trailer + uint64_t text_orig_filesz; // the segment that holds it: its size before + uint64_t rw_orig_memsz; // the data segment: its size before + uint32_t text_index; // their numbers in the program header table + uint32_t rw_index; + uint32_t text_orig_flags; + uint32_t reserved; + uint64_t site_file_off; // the instruction that was replaced (a "svc #0") + uint64_t reserved2; +}; + +_Static_assert(sizeof(struct gosys_trailer) == 64, "the trailer is 64 bytes"); +_Static_assert(sizeof(struct gosys_hdr) % 16 == 0, "the header keeps the code after it aligned"); + +#endif diff --git a/tools/nativeguest/hook/env.c b/tools/nativeguest/hook/env.c new file mode 100644 index 0000000000..e95083c25e --- /dev/null +++ b/tools/nativeguest/hook/env.c @@ -0,0 +1,309 @@ +// Part of libddnatv-hook.so, included by ddnatv-hook.c (one translation unit, so the +// parts share their static functions and are built with the same command as before). +// +// The environment of started programs: Android programs get it without the hook, rootfs +// programs with it. + +#ifndef DDNATV_HOST_TEST + +static const char *ddnatv_hook_library(char *buf, size_t bufsz); + +static char **clean_env_for_system(char *const envp[]) { + int count = 0; + while (envp && envp[count]) count++; + char **new_env = calloc(count + 3, sizeof(char *)); + int dst = 0; + int has_path = 0; + for (int i = 0; i < count; i++) { + char first = envp[i][0]; + if (!((first == 'L' && (strncmp(envp[i], "LD_PRELOAD=", 11) == 0 || + strncmp(envp[i], "LD_LIBRARY_PATH=", 16) == 0)) || + (first == 'G' && strncmp(envp[i], "GLIBC_TUNABLES=", 15) == 0))) { + if (first == 'P' && strncmp(envp[i], "PATH=", 5) == 0) { + has_path = 1; + size_t plen = strlen(envp[i]); + char *new_path = malloc(plen + 64); + if (new_path) { + snprintf(new_path, plen + 64, "PATH=/system/bin:/system/xbin:/sbin:/vendor/bin:%s", envp[i] + 5); + new_env[dst++] = new_path; + } else { + new_env[dst++] = envp[i]; + } + } else { + new_env[dst++] = envp[i]; + } + } + } + if (!has_path) { + new_env[dst++] = strdup("PATH=/system/bin:/system/xbin:/sbin:/vendor/bin"); + } + new_env[dst] = NULL; + return new_env; +} + +// LD_PRELOAD with every absolute path that is not in the rootfs yet mapped into it. Entries are +// separated by ':' or ' '. free() the result. +static char *map_preload_paths(const char *value) { + if (!value) return NULL; + size_t cap = strlen(value) + (g_ddnatv_root_len + 1) * 16 + 16; + char *out = malloc(cap); + if (!out) return NULL; + size_t used = 0; + out[0] = '\0'; + const char *p = value; + while (*p) { + size_t len = strcspn(p, ": "); + if (len > 0 && len < PATH_MAX) { + char entry[PATH_MAX]; + memcpy(entry, p, len); + entry[len] = '\0'; + char buf[PATH_MAX]; + const char *mapped = entry[0] == '/' ? rewrite_path_in_root(entry, buf, sizeof(buf)) : entry; + size_t mlen = strlen(mapped); + if (used + mlen + 2 >= cap) break; + if (used) out[used++] = ':'; + memcpy(out + used, mapped, mlen); + used += mlen; + out[used] = '\0'; + } + p += len; + if (*p) p++; + } + return out; +} + +static char **prepare_ddnatv_env(char *const envp[], const char *real_exe) { + init_ddnatv_hook(); + int count = 0; + int has_preload = 0; + int has_ld_library_path = 0; + int has_root = 0; + int has_tunables = 0; + int has_path = 0; + int has_tmp = 0; + int has_threads_max = 0; + int has_xz_opt = 0; + int has_xz_defaults = 0; + int has_frontend = 0; + int has_debconf_frontend = 0; + int has_debconf_seen = 0; + + const char *chosen_real_exe = (real_exe && real_exe[0] != '\0') ? real_exe : (g_real_exe[0] != '\0' ? g_real_exe : NULL); + + // Every variable is looked at on every exec, so only the names that start with its first letter + // are compared (this was 13 strncmp() calls for each of the 30 or so variables) + int has_nt = 0; + int has_binds = 0; + int has_hostname = 0; + int has_link_root = 0; + int has_preload_list = 0; + const char *preload_value = NULL; + while (envp && envp[count]) { + const char *e = envp[count]; + switch (e[0]) { + case 'L': + if (strncmp(e, "LD_PRELOAD=", 11) == 0) { has_preload = 1; preload_value = e + 11; } + else if (strncmp(e, "LD_LIBRARY_PATH=", 16) == 0) has_ld_library_path = 1; + break; + case 'G': + if (strncmp(e, "GLIBC_TUNABLES=", 15) == 0) has_tunables = 1; + break; + case 'P': + if (strncmp(e, "PATH=", 5) == 0) has_path = 1; + break; + case 'T': + if (strncmp(e, "TMPDIR=", 7) == 0) has_tmp = 1; + break; + case 'X': + if (strncmp(e, "XZ_OPT=", 7) == 0) has_xz_opt = 1; + else if (strncmp(e, "XZ_DEFAULTS=", 12) == 0) has_xz_defaults = 1; + break; + case 'D': + if (strncmp(e, "DDNATV_ROOT=", 12) == 0) has_root = 1; + else if (strncmp(e, "DDNATV_NT=", 10) == 0) has_nt = 1; + else if (strncmp(e, "DDNATV_BINDS=", 13) == 0) has_binds = 1; + else if (strncmp(e, "DDNATV_PRELOAD=", 15) == 0) has_preload_list = 1; + else if (strncmp(e, "DDNATV_HOSTNAME=", 16) == 0) has_hostname = 1; + else if (strncmp(e, "DDNATV_LINK_ROOT=", 17) == 0) has_link_root = 1; + else if (strncmp(e, "DPKG_DEB_THREADS_MAX=", 21) == 0) has_threads_max = 1; + else if (strncmp(e, "DEBIAN_FRONTEND=", 16) == 0) has_frontend = 1; + else if (strncmp(e, "DEBCONF_FRONTEND=", 17) == 0) has_debconf_frontend = 1; + else if (strncmp(e, "DEBCONF_NONINTERACTIVE_SEEN=", 28) == 0) has_debconf_seen = 1; + break; + default: + break; + } + count++; + } + + char **new_env = calloc(count + 24, sizeof(char *)); + int dst = 0; + for (int i = 0; i < count; i++) { + if (has_preload && envp[i][0] == 'L' && strncmp(envp[i], "LD_PRELOAD=", 11) == 0) { + // ld.so opens these itself: the guest's paths are mapped to the rootfs, and the libraries every + // program loads (DDNATV_PRELOAD) come first + char *mapped = map_preload_paths(preload_value); + if (mapped) { + size_t chain = g_preload_host ? strlen(g_preload_host) : 0; + int has_chain = chain > 0 && strstr(mapped, g_preload_host) != NULL; + size_t len = 11 + chain + 1 + strlen(mapped) + 1; + char *str = malloc(len); + if (str) { + if (chain > 0 && !has_chain) snprintf(str, len, "LD_PRELOAD=%s:%s", g_preload_host, mapped); + else snprintf(str, len, "LD_PRELOAD=%s", mapped); + new_env[dst++] = str; + free(mapped); + continue; + } + free(mapped); + } + } + // If we have an explicit real executable, drop the inherited entry to replace with the fresh target + if (chosen_real_exe && envp[i][0] == 'D' && strncmp(envp[i], "DDNATV_REAL_EXE=", 16) == 0) { + continue; + } + new_env[dst++] = envp[i]; + } + + // Exact-size strings: this runs before every exec, often in a freshly forked child where each + // newly touched page is a copy-on-write fault. + if (chosen_real_exe) { + size_t len = strlen(chosen_real_exe); + char *str = malloc(16 + len + 1); + if (str) { + memcpy(str, "DDNATV_REAL_EXE=", 16); + memcpy(str + 16, chosen_real_exe, len + 1); + new_env[dst++] = str; + } + } + + char hook_buf[PATH_MAX]; + const char *hook = (!has_preload && g_ddnatv_root[0] != '\0') ? ddnatv_hook_library(hook_buf, sizeof(hook_buf)) : NULL; + if (hook) { + size_t hook_len = strlen(hook); + size_t host_len = g_preload_host ? strlen(g_preload_host) + 1 : 0; + size_t size = 11 + host_len + hook_len + 1; + char *str = malloc(size); + if (str) { + size_t n = ddnatv_append(str, size, 0, "LD_PRELOAD=", 11); + if (host_len) { + n = ddnatv_append(str, size, n, g_preload_host, host_len - 1); + n = ddnatv_append(str, size, n, ":", 1); + } + ddnatv_append(str, size, n, hook, hook_len); + new_env[dst++] = str; + } + } + if (!has_preload_list && g_preload_guest) { + size_t len = strlen(g_preload_guest); + char *str = malloc(15 + len + 1); + if (str) { + memcpy(str, "DDNATV_PRELOAD=", 15); + memcpy(str + 15, g_preload_guest, len + 1); + new_env[dst++] = str; + } + } + if (!has_ld_library_path && g_ddnatv_root[0] != '\0') { + size_t size = 16 + 1; + for (size_t i = 0; i < sizeof(k_rootfs_lib_dirs) / sizeof(k_rootfs_lib_dirs[0]); i++) { + char dir[PATH_MAX]; + size += 1 + strlen(rewrite_path_in_root(k_rootfs_lib_dirs[i], dir, sizeof(dir))); + } + char *str = malloc(size); + if (str) { + size_t n = ddnatv_append(str, size, 0, "LD_LIBRARY_PATH=", 16); + append_rootfs_lib_dirs(str, size, n, 0); + new_env[dst++] = str; + } + } + if (!has_root && g_ddnatv_root[0] != '\0') { + char *str = malloc(PATH_MAX + 16); + if (str) { + snprintf(str, PATH_MAX + 16, "DDNATV_ROOT=%s", g_ddnatv_root); + new_env[dst++] = str; + } + } + if (!has_link_root && g_link_root_len) { + char *str = malloc(17 + g_link_root_len + 1); + if (str) { + memcpy(str, "DDNATV_LINK_ROOT=", 17); + memcpy(str + 17, g_link_root, g_link_root_len + 1); + new_env[dst++] = str; + } + } + if (!has_hostname && g_hostname[0]) { + char *str = malloc(16 + sizeof(g_hostname)); + if (str) { + snprintf(str, 16 + sizeof(g_hostname), "DDNATV_HOSTNAME=%s", g_hostname); + new_env[dst++] = str; + } + } + if (!has_binds && g_binds_raw) { + size_t len = strlen(g_binds_raw); + char *str = malloc(13 + len + 1); + if (str) { + memcpy(str, "DDNATV_BINDS=", 13); + memcpy(str + 13, g_binds_raw, len + 1); + new_env[dst++] = str; + } + } + // This process looked for the ARM translator's files and found none: the programs it starts + // need not look again (it takes a look at /proc/self/fd in each of them, see privileged.c) + if (!has_nt && g_no_translator) { + new_env[dst++] = strdup("DDNATV_NT=0"); + } + if (!has_tunables) { + new_env[dst++] = strdup("GLIBC_TUNABLES=glibc.pthread.rseq=0"); + } + if (!has_path) { + new_env[dst++] = strdup("PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"); + } + if (!has_tmp && g_ddnatv_root[0] != '\0') { + char *str = malloc(PATH_MAX + 16); + if (str) { + snprintf(str, PATH_MAX + 16, "TMPDIR=%s/tmp", g_ddnatv_root); + new_env[dst++] = str; + } + } + if (!has_threads_max) { + new_env[dst++] = strdup("DPKG_DEB_THREADS_MAX=1"); + } + if (!has_xz_opt) { + new_env[dst++] = strdup("XZ_OPT=-T1"); + } + if (!has_xz_defaults) { + new_env[dst++] = strdup("XZ_DEFAULTS=-T1"); + } + if (!has_frontend) { + new_env[dst++] = strdup("DEBIAN_FRONTEND=noninteractive"); + } + if (!has_debconf_frontend) { + new_env[dst++] = strdup("DEBCONF_FRONTEND=noninteractive"); + } + if (!has_debconf_seen) { + new_env[dst++] = strdup("DEBCONF_NONINTERACTIVE_SEEN=true"); + } + new_env[dst] = NULL; + return new_env; +} + +static void free_modified_env(char **modified_env, char *const orig_env[]) { + if (!modified_env) return; + for (int i = 0; modified_env[i] != NULL; i++) { + int is_orig = 0; + if (orig_env) { + for (int j = 0; orig_env[j] != NULL; j++) { + if (modified_env[i] == orig_env[j]) { + is_orig = 1; + break; + } + } + } + if (!is_orig) { + free(modified_env[i]); + } + } + free(modified_env); +} + +#endif /* !DDNATV_HOST_TEST */ diff --git a/tools/nativeguest/hook/exec.c b/tools/nativeguest/hook/exec.c new file mode 100644 index 0000000000..90df96f8fe --- /dev/null +++ b/tools/nativeguest/hook/exec.c @@ -0,0 +1,1127 @@ +// Part of libddnatv-hook.so, included by ddnatv-hook.c (one translation unit, so the +// parts share their static functions and are built with the same command as before). +// +// execve() and posix_spawn(): rootfs programs through ld.so with the hook, scripts through their +// interpreter, Android programs with Android's environment. + +#ifndef DDNATV_HOST_TEST + +// Runs before every exec, mostly in a child a shell has just forked: the copies write only the +// string (ddnatv_copy_cstr), not the whole PATH_MAX buffer, since each page written there is a +// copy-on-write fault. +static void resolve_symlinks(const char *path, char *out, size_t out_size, int depth) { + if (!path || !out || out_size == 0) return; + ddnatv_copy_cstr(out, out_size, path); + if (depth > 8) return; + + char link_target[PATH_MAX]; + ssize_t len = readlink(path, link_target, sizeof(link_target) - 1); + if (len < 0) return; + link_target[len] = '\0'; + + char next_path[PATH_MAX]; + if (link_target[0] == '/') { + char rw_buf[PATH_MAX]; + const char *rewritten = rewrite_path(link_target, rw_buf, sizeof(rw_buf)); + ddnatv_copy_cstr(next_path, sizeof(next_path), rewritten); + } else { + const char *slash = strrchr(path, '/'); + if (slash) { + size_t dir_len = (size_t)(slash - path); + if (dir_len > sizeof(next_path) - 1) dir_len = sizeof(next_path) - 1; + memcpy(next_path, path, dir_len); + next_path[dir_len] = '\0'; + size_t n = ddnatv_append(next_path, sizeof(next_path), dir_len, "/", 1); + ddnatv_append(next_path, sizeof(next_path), n, link_target, (size_t)len); + } else { + ddnatv_copy_cstr(next_path, sizeof(next_path), link_target); + } + } + // proot's link2symlink leaves hard links as ".l2s.*" files that programs must not see by name + const char *next_base = strrchr(next_path, '/'); + next_base = next_base ? next_base + 1 : next_path; + if (strncmp(next_base, ".l2s.", 5) == 0) return; + resolve_symlinks(next_path, out, out_size, depth + 1); +} + +static char **ensure_hook_in_envp(char *const envp[], const char *real_exe) { + return prepare_ddnatv_env(envp, real_exe); +} + +// The file whose header decides how to start `target`: the symlink-resolved ELF (so ld.so and +// /proc/self/exe see the real program, as with the kernel), else `target` itself (scripts, +// links to ld.so). `probe` describes the returned file. +static const char *select_exec_target(const char *target, char *resolved, size_t resolved_size, struct exec_probe *probe) { + resolve_symlinks(target, resolved, resolved_size, 0); + probe_exec_file(resolved, probe); + if (probe->is_elf && !is_ld_linux(resolved)) return resolved; + if (strcmp(resolved, target) != 0) probe_exec_file(target, probe); + return target; +} + +// The library children get with --preload: the one this process runs from, so that a program +// always starts with the same build as its parent, else the first of usr/lib and lib that exists; +// NULL if neither does. +// The path dladdr() finds is kept (it does not change): set once, read by every later exec, and +// set before a fork by prepare_exec_in_parent() so that the children only read it. +static char g_hook_library[PATH_MAX]; +static int g_hook_library_known = 0; + +static const char *ddnatv_hook_library(char *buf, size_t bufsz) { + static const char usr_lib[] = "/usr/lib/libddnatv-hook.so"; + static const char lib[] = "/lib/libddnatv-hook.so"; + if (__atomic_load_n(&g_hook_library_known, __ATOMIC_ACQUIRE)) return g_hook_library; + Dl_info info; + if (dladdr((void *)ddnatv_hook_library, &info) && info.dli_fname && info.dli_fname[0] == '/' && + strlen(info.dli_fname) < sizeof(g_hook_library)) { + // loaded from there, so it exists + ddnatv_copy_cstr(g_hook_library, sizeof(g_hook_library), info.dli_fname); + __atomic_store_n(&g_hook_library_known, 1, __ATOMIC_RELEASE); + return g_hook_library; + } + size_t n = ddnatv_append(buf, bufsz, 0, g_ddnatv_root, g_ddnatv_root_len); + ddnatv_append(buf, bufsz, n, usr_lib, sizeof(usr_lib) - 1); + if (access(buf, F_OK) == 0) return buf; + n = ddnatv_append(buf, bufsz, 0, g_ddnatv_root, g_ddnatv_root_len); + ddnatv_append(buf, bufsz, n, lib, sizeof(lib) - 1); + return access(buf, F_OK) == 0 ? buf : NULL; +} + +// ld.so does not need the exec bit on the program it loads, but other tools look at it: set +// it as the old access(X_OK)/chmod() pair did, using the mode the probe already has. +static void ensure_exec_bit(const char *path, mode_t mode) { + if (S_ISDIR(mode) || (mode & 0111) != 0) return; + if (strstr(path, "/bin/") || strstr(path, "/sbin/")) return; + chmod(path, 0755); +} + +// Rootfs path of a "#!" interpreter; a missing /bin/sh falls back to Android's shell. +static const char *shebang_interpreter(const char *interp, char *buf, size_t bufsz) { + const char *rewritten = rewrite_path(interp, buf, bufsz); + if (strcmp(interp, "/bin/sh") == 0 && access(rewritten, F_OK) != 0 && access("/system/bin/sh", X_OK) == 0) { + return "/system/bin/sh"; + } + return rewritten; +} + +static const char *exec_prog_name(const char *target) { + const char *slash = strrchr(target, '/'); + return slash ? slash + 1 : target; +} + +// Hook execve +typedef int (*orig_execve_f_type)(const char *filename, char *const argv[], char *const envp[]); + +// glibc's execve(), looked up once (also by prepare_exec_in_parent() before a fork) +static orig_execve_f_type g_orig_execve = NULL; + +static orig_execve_f_type real_execve(void) { + orig_execve_f_type f = __atomic_load_n(&g_orig_execve, __ATOMIC_RELAXED); + if (!f) { + f = (orig_execve_f_type)dlsym(RTLD_NEXT, "execve"); + __atomic_store_n(&g_orig_execve, f, __ATOMIC_RELAXED); + } + return f; +} + +// prepare_exec_in_parent() rehearses an exec in the parent: everything up to the execve() itself, +// which this then skips. Only in the thread that rehearses: another thread's exec at that moment +// is a real one. +static int g_exec_rehearsal = 0; +static pthread_t g_exec_rehearsal_thread; + +static int in_exec_rehearsal(void) { + return __atomic_load_n(&g_exec_rehearsal, __ATOMIC_ACQUIRE) && + pthread_equal(pthread_self(), g_exec_rehearsal_thread); +} + +static int run_execve(orig_execve_f_type f, const char *path, char *const argv[], char *const envp[]) { + if (in_exec_rehearsal()) { + errno = 0; + return 0; + } + return f(path, argv, envp); +} + +// ldconfig does nothing in The native userland (its cache is for paths that ld.so opens without the hook), but the +// exec code reads /etc/ld.so.conf(.d) again when /etc/ld.so.cache changes: "sudo ldconfig" after an +// edit of a .conf file marks it changed, as the real ldconfig would by writing it. A cache asked +// for with -C is made (empty): Flatpak mounts the one it generates over /etc/ld.so.cache. +static void ldconfig_mark_cache(char *const argv[]) { + if (g_ddnatv_root[0] == '\0') return; + char path[PATH_MAX]; + const char *cache = NULL; + for (int i = 1; argv && argv[i]; i++) { + if (strcmp(argv[i], "-C") == 0 && argv[i + 1]) cache = argv[++i]; + else if (strncmp(argv[i], "-C", 2) == 0 && argv[i][2] != '\0') cache = argv[i] + 2; + } + const char *target = path; + if (cache) { + target = rewrite_path_in_root(cache, path, sizeof(path)); + } else { + size_t n = ddnatv_append(path, sizeof(path), 0, g_ddnatv_root, g_ddnatv_root_len); + ddnatv_append(path, sizeof(path), n, "/etc/ld.so.cache", 16); + } + int fd = (int)syscall(SYS_openat, AT_FDCWD, target, O_WRONLY | O_CREAT | O_CLOEXEC, 0644); + if (fd < 0) return; + syscall(SYS_utimensat, fd, NULL, NULL, 0); + syscall(SYS_close, fd); +} + +static int is_ldconfig_name(const char *name) { + return strcmp(name, "ldconfig") == 0 || strcmp(name, "ldconfig.real") == 0; +} + +static int ddnatv_execve(const char *filename, char *const argv[], char *const envp[], int depth) { + orig_execve_f_type orig_execve = real_execve(); + if (!orig_execve) { + errno = ENOSYS; + return -1; + } + + // Scripts get the path exactly as the caller wrote it ("./x.sh" stays "./x.sh" in $0), + // like the kernel would pass it; routing below uses the absolute form. + const char *caller_path = filename; + char abs_buf[PATH_MAX]; + filename = absolutize_exec_path(filename, abs_buf, sizeof(abs_buf)); + + char buf[PATH_MAX]; + const char *target = rewrite_path(filename, buf, sizeof(buf)); + + init_ddnatv_hook(); + + const char *f_base = (filename != NULL) ? strrchr(filename, '/') : NULL; + f_base = (f_base != NULL) ? f_base + 1 : (filename != NULL ? filename : ""); + + const char *prog_name = strrchr(target, '/'); + prog_name = (prog_name != NULL) ? prog_name + 1 : target; + + // Wine's preloader is a static program that opens the program and the dynamic linker it loads with + // system calls of its own, which no hook can map, to reserve address ranges before the linker runs. + // The program is started without it: its libc and ntdll reserve what Wine needs themselves. + if ((strcmp(prog_name, "wine-preloader") == 0 || strcmp(prog_name, "wine64-preloader") == 0) && + argv && argv[0] && argv[1] && depth < DDNATV_MAX_SHEBANG_DEPTH) { + return ddnatv_execve(argv[1], argv + 1, envp, depth + 1); + } + + // Intercept utilities that fail or cause issues in unprivileged Android environment + if (strcmp(f_base, "ldconfig") == 0 || + strcmp(f_base, "ldconfig.real") == 0 || + strcmp(f_base, "systemd-machine-id-setup") == 0 || + strcmp(f_base, "systemd-sysusers") == 0 || + strcmp(f_base, "systemd-tmpfiles") == 0 || + strcmp(f_base, "start-stop-daemon") == 0 || + strcmp(f_base, "mandb") == 0 || + strcmp(f_base, "mandb.real") == 0 || + strcmp(f_base, "update-mime-database") == 0 || + strcmp(f_base, "update-desktop-database") == 0 || + strcmp(f_base, "install-info") == 0 || + strcmp(f_base, "install-sgmlcatalog") == 0 || + strcmp(prog_name, "ldconfig") == 0 || + strcmp(prog_name, "ldconfig.real") == 0 || + strcmp(prog_name, "systemd-machine-id-setup") == 0 || + strcmp(prog_name, "systemd-sysusers") == 0 || + strcmp(prog_name, "systemd-tmpfiles") == 0 || + strcmp(prog_name, "start-stop-daemon") == 0 || + strcmp(prog_name, "mandb") == 0 || + strcmp(prog_name, "mandb.real") == 0 || + strcmp(prog_name, "update-mime-database") == 0 || + strcmp(prog_name, "update-desktop-database") == 0 || + strcmp(prog_name, "install-info") == 0 || + strcmp(prog_name, "install-sgmlcatalog") == 0) { + if (is_ldconfig_name(f_base) || is_ldconfig_name(prog_name)) ldconfig_mark_cache(argv); + _exit(0); + } + if (strcmp(f_base, "mountpoint") == 0 || strcmp(prog_name, "mountpoint") == 0) { + int is_virtual_fs = 0; + for (int i = 1; argv && argv[i]; i++) { + if (strcmp(argv[i], "/proc") == 0 || strcmp(argv[i], "/proc/") == 0 || + strcmp(argv[i], "/sys") == 0 || strcmp(argv[i], "/sys/") == 0 || + strcmp(argv[i], "/dev") == 0 || strcmp(argv[i], "/dev/") == 0 || + strcmp(argv[i], "/dev/pts") == 0) { + is_virtual_fs = 1; + break; + } + } + if (is_virtual_fs) { + _exit(0); + } + } + if (strcmp(f_base, "policy-rc.d") == 0 || strcmp(prog_name, "policy-rc.d") == 0) { + _exit(101); + } + + // A static program inside the rootfs, to be made safe to start (prepare_static_program) + const char *static_elf = NULL; + char resolved_target[PATH_MAX]; + + // Only intercept binaries/scripts within DDNATV_ROOT + if ((g_ddnatv_root[0] != '\0' && strncmp(target, g_ddnatv_root, g_ddnatv_root_len) == 0) || is_mapped_guest_path(filename)) { + struct exec_probe probe; + const char *elf_target = select_exec_target(target, resolved_target, sizeof(resolved_target), &probe); + if (probe.is_elf && !probe.is_dynamic) static_elf = elf_target; + // A Go program built for Android asks for Android's linker, so it is started like the dynamic + // ones, but it makes its own system calls: it gets the code of the static Go programs (#42) + if (probe.is_dynamic && probe.android_interp) prepare_static_program_ex(elf_target, 1); + + // 1. Transparently route dynamically linked glibc ELF binaries through ld.so + // Note: Statically linked binaries (PT_INTERP absent, e.g. Meta Muse Code) must execute directly! + if (probe.is_dynamic) { + char ld_so[PATH_MAX]; + ld_so[0] = '\0'; + if (find_dynamic_linker_for_machine(g_ddnatv_root, probe.e_machine, ld_so, sizeof(ld_so)) && strcmp(elf_target, ld_so) != 0) { + ensure_exec_bit(elf_target, probe.mode); + + if (!in_exec_rehearsal()) ddnatv_copy_cstr(g_real_exe, sizeof(g_real_exe), elf_target); + + char hook_buf[PATH_MAX]; + const char *hook_so = ddnatv_hook_library(hook_buf, sizeof(hook_buf)); + char ld_lib_path[PATH_MAX * 6]; + build_ld_library_path(envp, probe.runpath, probe.runpath_is_rpath, elf_target, ld_lib_path, sizeof(ld_lib_path)); + char **new_argv = build_ldso_argv(ld_so, hook_so, ld_lib_path, elf_target, exec_prog_name(target), argv); + if (!new_argv) { + errno = ENOMEM; + return -1; + } + char **new_env = ensure_hook_in_envp(envp, elf_target); + int ret = run_execve(orig_execve, ld_so, new_argv, new_env); + if (errno == EACCES && chmod(ld_so, 0755) == 0) { + // ld.so without its exec bit (an unusual restore): fix it once and retry + ret = run_execve(orig_execve, ld_so, new_argv, new_env); + } + int saved_errno = errno; + free(new_argv); + free_modified_env(new_env, envp); + errno = saved_errno; + return ret; + } + } + + // ld.so started directly (ldd, "ld.so --list PROGRAM", "ld.so PROGRAM"): the program it loads + // gets the --library-path a start of its own gets, or ldd reports its libraries as not found, + // and its path mapped into the rootfs, since ld.so opens it without the hook + if (probe.is_elf && !probe.is_dynamic && is_ld_linux(elf_target)) { + int caller_lib_path = 0; + int pi = ldso_program_index(argv, &caller_lib_path); + if (pi > 0 && caller_lib_path) { + // the caller's own --library-path stays as it is; only the program is mapped (when it is + // in the rootfs: a caller can also name a file by its real path) + char prog_buf[PATH_MAX]; + const char *prog = argv[pi]; + if (prog[0] == '/') { + const char *mapped = rewrite_path(prog, prog_buf, sizeof(prog_buf)); + int fd = mapped != prog ? (int)syscall(SYS_openat, AT_FDCWD, mapped, O_PATH | O_CLOEXEC) : -1; + if (fd >= 0) { + syscall(SYS_close, fd); + prog = mapped; + } + } + int argc = 0; + while (argv[argc]) argc++; + char **new_argv = (char **)malloc(sizeof(char *) * (size_t)(argc + 1)); + if (!new_argv) { + errno = ENOMEM; + return -1; + } + for (int i = 0; i <= argc; i++) new_argv[i] = argv[i]; + new_argv[pi] = (char *)prog; + prepare_static_program(static_elf); // ld.so is a static program itself + char **ddnatv_env = prepare_ddnatv_env(envp, target); + int ret = run_execve(orig_execve, target, new_argv, ddnatv_env); + int saved_errno = errno; + free_modified_env(ddnatv_env, envp); + free(new_argv); + errno = saved_errno; + return ret; + } + if (pi > 0) { + char prog_buf[PATH_MAX]; + const char *prog = argv[pi][0] == '/' ? rewrite_path(argv[pi], prog_buf, sizeof(prog_buf)) : argv[pi]; + struct exec_probe prog_probe; + probe_exec_file(prog, &prog_probe); + char ld_lib_path[PATH_MAX * 6]; + build_ld_library_path(envp, prog_probe.runpath, prog_probe.runpath_is_rpath, + prog[0] == '/' ? prog : NULL, ld_lib_path, sizeof(ld_lib_path)); + int argc = 0; + while (argv[argc]) argc++; + char **new_argv = (char **)malloc(sizeof(char *) * (size_t)(argc + 3)); + if (!new_argv) { + errno = ENOMEM; + return -1; + } + new_argv[0] = argv[0]; + new_argv[1] = (char *)"--library-path"; + new_argv[2] = ld_lib_path; + for (int i = 1; i <= argc; i++) new_argv[i + 2] = argv[i]; + new_argv[pi + 2] = (char *)prog; // ld.so opens it itself: "ldd /usr/bin/adb" as well + prepare_static_program(static_elf); // ld.so is a static program itself + char **ddnatv_env = prepare_ddnatv_env(envp, target); + int ret = run_execve(orig_execve, target, new_argv, ddnatv_env); + int saved_errno = errno; + free_modified_env(ddnatv_env, envp); + free(new_argv); + errno = saved_errno; + return ret; + } + } + + // 2. Handle scripts with shebang lines (e.g., #!/bin/sh, #!/usr/bin/perl) + if (!probe.is_elf && probe.n >= 2 && probe.hdr[0] == '#' && probe.hdr[1] == '!') { + char *interp; + char *interp_arg; + parse_shebang(probe.hdr, &interp, &interp_arg); + char interp_buf[PATH_MAX]; + const char *rewritten_interp = shebang_interpreter(interp, interp_buf, sizeof(interp_buf)); + const char *script = (caller_path && caller_path[0] != '/') ? caller_path : target; + char **new_argv = build_script_argv(rewritten_interp, interp_arg, script, argv); + if (!new_argv) { + errno = ENOMEM; + return -1; + } + int ret; + if (is_host_interpreter(rewritten_interp)) { + char **sys_env = clean_env_for_system(envp); + ret = run_execve(orig_execve, rewritten_interp, new_argv, sys_env); + int saved_errno = errno; + free_modified_env(sys_env, envp); + errno = saved_errno; + } else if (depth >= DDNATV_MAX_SHEBANG_DEPTH) { + errno = ELOOP; + ret = -1; + } else { + char **ddnatv_env = prepare_ddnatv_env(envp, NULL); + ret = ddnatv_execve(rewritten_interp, new_argv, ddnatv_env, depth + 1); + int saved_errno = errno; + free_modified_env(ddnatv_env, envp); + errno = saved_errno; + } + free(new_argv); + return ret; + } + } else { + // Any binary outside DDNATV_ROOT is an Android host binary (e.g. /system/bin/su, /sbin/su, /data/adb/...) + char **sys_env = clean_env_for_system(envp); + int ret = run_execve(orig_execve, target, argv, sys_env); + int saved_errno = errno; + free_modified_env(sys_env, envp); + errno = saved_errno; + return ret; + } + + if (static_elf) prepare_static_program(static_elf); + char **ddnatv_env = prepare_ddnatv_env(envp, target); + int ret = run_execve(orig_execve, target, argv, ddnatv_env); + int saved_errno = errno; + free_modified_env(ddnatv_env, envp); + errno = saved_errno; + return ret; +} + +int execve(const char *filename, char *const argv[], char *const envp[]) { + return ddnatv_execve(filename, argv, envp, 0); +} + +// ---- fork() --------------------------------------------------------------------------------- +// A shell (and make, dpkg, perl, ...) starts a program with fork() and then execve() in the child. +// What the exec code above needs for the first time in a process (glibc's execve, fstat and readlink +// behind the hooks, the rootfs' ld.so, the path of this library) it found in the child, where every +// page it writes is a copy of the parent's: some 40 copy-on-write faults before each program even +// started, where a shell's child without the hook has fewer than ten. Now the parent finds them at +// its first fork(), and the children inherit them and only read them. At its eighth fork() the +// parent also rehearses an exec (everything but the execve() itself), which binds the calls the +// exec code makes and touches the code it runs. That costs about as much as it saves in four +// children, so only a process that keeps forking (a shell running a loop or a long script, make, +// dpkg) does it; one that forks a few times (sh -c with a pipe) does not pay for it. +#define DDNATV_FORKS_BEFORE_REHEARSAL 8 +static int g_exec_prepared = 0; // 1 after the first fork(), 2 after the rehearsal +static pthread_mutex_t g_exec_prepare_lock = PTHREAD_MUTEX_INITIALIZER; + +static void rehearse_exec(void) { + static const char rehearsal_target[] = "/usr/bin/true"; + char target_buf[PATH_MAX]; + const char *target = rewrite_path(rehearsal_target, target_buf, sizeof(target_buf)); + struct exec_probe probe; + // A dynamic program only: it takes the one way through ddnatv_execve() that ends in run_execve() + probe_exec_file(target, &probe); + if (!probe.is_elf || !probe.is_dynamic) return; + extern char **environ; + char *rehearsal_argv[] = { (char *)"true", NULL }; + g_exec_rehearsal_thread = pthread_self(); + __atomic_store_n(&g_exec_rehearsal, 1, __ATOMIC_RELEASE); + ddnatv_execve(rehearsal_target, rehearsal_argv, environ, 0); + __atomic_store_n(&g_exec_rehearsal, 0, __ATOMIC_RELEASE); +} + +static void prepare_exec_in_parent(int level) { + // One thread prepares (two rehearsing at once would take each other for the other's thread). + // Not waiting for the lock: a signal handler that forks while its thread is preparing would wait + // for itself; a busy lock leaves the preparation to a later fork(). + if (pthread_mutex_trylock(&g_exec_prepare_lock) != 0) return; + int done = __atomic_load_n(&g_exec_prepared, __ATOMIC_ACQUIRE); + if (done >= level) { + pthread_mutex_unlock(&g_exec_prepare_lock); + return; + } + int saved_errno = errno; + init_ddnatv_hook(); + real_execve(); + if (g_ddnatv_root[0] != '\0') { + if (done < 1) { + char ld_so[PATH_MAX]; +#if defined(__aarch64__) + uint16_t machine = 183; +#elif defined(__arm__) + uint16_t machine = 40; +#elif defined(__x86_64__) + uint16_t machine = 62; +#else + uint16_t machine = 0; +#endif + if (machine && find_dynamic_linker_for_machine(g_ddnatv_root, machine, ld_so, sizeof(ld_so))) { + // readlink(), open(), read() and close() behind the hooks, as the probe calls them + char resolved[PATH_MAX]; + struct exec_probe probe; + resolve_symlinks(ld_so, resolved, sizeof(resolved), 0); + probe_exec_file(ld_so, &probe); + } + char hook_buf[PATH_MAX]; + ddnatv_hook_library(hook_buf, sizeof(hook_buf)); + struct stat st; + probe_fstat(-1, &st); // looks fstat() up; -1 only fails with EBADF + char conf[PATH_MAX * 2]; + ldconf_dirs(conf, sizeof(conf)); // ld.so.conf read here, so the children only compare times + } + if (level >= 2) rehearse_exec(); + } + __atomic_store_n(&g_exec_prepared, level, __ATOMIC_RELEASE); + pthread_mutex_unlock(&g_exec_prepare_lock); + errno = saved_errno; +} + +pid_t fork(void) { + static pid_t (*orig_fork)(void) = NULL; + if (!orig_fork) orig_fork = (pid_t (*)(void))dlsym(RTLD_NEXT, "fork"); + if (!orig_fork) { + errno = ENOSYS; + return -1; + } + int done = __atomic_load_n(&g_exec_prepared, __ATOMIC_ACQUIRE); + if (done < 2) { + static int forks = 0; + int n = __atomic_add_fetch(&forks, 1, __ATOMIC_RELAXED); + if (n == 1 || n >= DDNATV_FORKS_BEFORE_REHEARSAL) prepare_exec_in_parent(n >= DDNATV_FORKS_BEFORE_REHEARSAL ? 2 : 1); + } + pid_t pid = orig_fork(); + // A child forked from a signal handler that interrupted a rehearsal is not rehearsing: its + // execve() is a real one (only read in the usual case, so no page is copied for it) + if (pid == 0 && __atomic_load_n(&g_exec_rehearsal, __ATOMIC_RELAXED)) __atomic_store_n(&g_exec_rehearsal, 0, __ATOMIC_RELAXED); + return pid; +} + +extern char **environ; + +int execv(const char *path, char *const argv[]) { + return execve(path, argv, environ); +} + +int execl(const char *path, const char *arg0, ...) { + va_list args; + va_start(args, arg0); + int count = (arg0 != NULL) ? 1 : 0; + if (arg0 != NULL) { + while (va_arg(args, const char *) != NULL) { + count++; + } + } + va_end(args); + + char **argv = (char **)calloc(count + 1, sizeof(char *)); + if (!argv) { + errno = ENOMEM; + return -1; + } + if (count > 0) { + argv[0] = (char *)arg0; + va_start(args, arg0); + for (int i = 1; i < count; i++) { + argv[i] = va_arg(args, char *); + } + va_end(args); + } + argv[count] = NULL; + int ret = execv(path, argv); + free(argv); + return ret; +} + +int execlp(const char *file, const char *arg0, ...) { + va_list args; + va_start(args, arg0); + int count = (arg0 != NULL) ? 1 : 0; + if (arg0 != NULL) { + while (va_arg(args, const char *) != NULL) { + count++; + } + } + va_end(args); + + char **argv = (char **)calloc(count + 1, sizeof(char *)); + if (!argv) { + errno = ENOMEM; + return -1; + } + if (count > 0) { + argv[0] = (char *)arg0; + va_start(args, arg0); + for (int i = 1; i < count; i++) { + argv[i] = va_arg(args, char *); + } + va_end(args); + } + argv[count] = NULL; + int ret = execvp(file, argv); + free(argv); + return ret; +} + +int execle(const char *path, const char *arg0, ...) { + va_list args; + va_start(args, arg0); + int count = (arg0 != NULL) ? 1 : 0; + if (arg0 != NULL) { + while (va_arg(args, const char *) != NULL) { + count++; + } + } + char *const *envp = va_arg(args, char *const *); + va_end(args); + + char **argv = (char **)calloc(count + 1, sizeof(char *)); + if (!argv) { + errno = ENOMEM; + return -1; + } + if (count > 0) { + argv[0] = (char *)arg0; + va_start(args, arg0); + for (int i = 1; i < count; i++) { + argv[i] = va_arg(args, char *); + } + va_end(args); + } + argv[count] = NULL; + int ret = execve(path, argv, (char *const *)envp); + free(argv); + return ret; +} + +int execvp(const char *file, char *const argv[]) { + if (!file || !*file) { + errno = ENOENT; + return -1; + } + if (strchr(file, '/')) { + return execve(file, argv, environ); + } + const char *path_env = getenv("PATH"); + if (!path_env) path_env = "/usr/bin:/bin"; + char path_copy[4096]; + strncpy(path_copy, path_env, sizeof(path_copy) - 1); + path_copy[sizeof(path_copy) - 1] = '\0'; + char *saveptr = NULL; + char *token = strtok_r(path_copy, ":", &saveptr); + while (token) { + char candidate[PATH_MAX]; + snprintf(candidate, sizeof(candidate), "%s/%s", token, file); + if (access(candidate, F_OK) == 0) { + return execve(candidate, argv, environ); + } + token = strtok_r(NULL, ":", &saveptr); + } + const char *standard_paths[] = {"/usr/bin", "/bin", "/usr/sbin", "/sbin", "/usr/local/bin", NULL}; + for (int i = 0; standard_paths[i] != NULL; i++) { + char candidate[PATH_MAX]; + snprintf(candidate, sizeof(candidate), "%s/%s", standard_paths[i], file); + if (access(candidate, F_OK) == 0) { + return execve(candidate, argv, environ); + } + } + return execve(file, argv, environ); +} + +int execvpe(const char *file, char *const argv[], char *const envp[]) { + if (!file || !*file) { + errno = ENOENT; + return -1; + } + if (strchr(file, '/')) { + return execve(file, argv, envp); + } + const char *path_env = NULL; + if (envp) { + for (char *const *ep = envp; *ep; ep++) { + if (strncmp(*ep, "PATH=", 5) == 0) { + path_env = *ep + 5; + break; + } + } + } + if (!path_env) path_env = getenv("PATH"); + if (!path_env) path_env = "/usr/bin:/bin"; + char path_copy[4096]; + strncpy(path_copy, path_env, sizeof(path_copy) - 1); + path_copy[sizeof(path_copy) - 1] = '\0'; + char *saveptr = NULL; + char *token = strtok_r(path_copy, ":", &saveptr); + while (token) { + char candidate[PATH_MAX]; + snprintf(candidate, sizeof(candidate), "%s/%s", token, file); + if (access(candidate, F_OK) == 0) { + return execve(candidate, argv, envp); + } + token = strtok_r(NULL, ":", &saveptr); + } + const char *standard_paths[] = {"/usr/bin", "/bin", "/usr/sbin", "/sbin", "/usr/local/bin", NULL}; + for (int i = 0; standard_paths[i] != NULL; i++) { + char candidate[PATH_MAX]; + snprintf(candidate, sizeof(candidate), "%s/%s", standard_paths[i], file); + if (access(candidate, F_OK) == 0) { + return execve(candidate, argv, envp); + } + } + return execve(file, argv, envp); +} + +typedef int (*orig_posix_spawn_f_type)(pid_t *, const char *, const posix_spawn_file_actions_t *, + const posix_spawnattr_t *, char *const [], char *const []); + +static orig_posix_spawn_f_type real_posix_spawn(void) { + static orig_posix_spawn_f_type orig_posix_spawn = NULL; + if (!orig_posix_spawn) orig_posix_spawn = (orig_posix_spawn_f_type)dlsym(RTLD_NEXT, "posix_spawn"); + return orig_posix_spawn; +} + +static int ddnatv_posix_spawn(pid_t *pid, const char *path, + const posix_spawn_file_actions_t *file_actions, + const posix_spawnattr_t *attrp, + char *const argv[], char *const envp[], int depth); + +// A rootfs script: start its interpreter with the same file actions and attributes. Handing the +// script to the kernel resolves "#!/bin/sh" to Android's own shell, which crashed on the glibc +// hook in LD_PRELOAD (and "#!/usr/bin/env" etc. used to fall back to fork() + execve(), which +// dropped the file actions). +static int spawn_script(pid_t *pid, const char *script, struct exec_probe *probe, + const posix_spawn_file_actions_t *file_actions, const posix_spawnattr_t *attrp, + char *const argv[], char *const envp[], int depth) { + char *interp; + char *interp_arg; + parse_shebang(probe->hdr, &interp, &interp_arg); + char interp_buf[PATH_MAX]; + const char *rewritten_interp = shebang_interpreter(interp, interp_buf, sizeof(interp_buf)); + char **new_argv = build_script_argv(rewritten_interp, interp_arg, script, argv); + if (!new_argv) return ENOMEM; + int ret; + if (is_host_interpreter(rewritten_interp)) { + orig_posix_spawn_f_type orig_posix_spawn = real_posix_spawn(); + char **sys_env = clean_env_for_system(envp); + ret = orig_posix_spawn ? orig_posix_spawn(pid, rewritten_interp, file_actions, attrp, new_argv, sys_env) : ENOSYS; + free_modified_env(sys_env, envp); + } else { + ret = ddnatv_posix_spawn(pid, rewritten_interp, file_actions, attrp, new_argv, envp, depth + 1); + } + free(new_argv); + return ret; +} + +static int ddnatv_posix_spawn(pid_t *pid, const char *path, + const posix_spawn_file_actions_t *file_actions, + const posix_spawnattr_t *attrp, + char *const argv[], char *const envp[], int depth) { + orig_posix_spawn_f_type orig_posix_spawn = real_posix_spawn(); + + const char *caller_path = path; + char abs_buf[PATH_MAX]; + path = absolutize_exec_path(path, abs_buf, sizeof(abs_buf)); + + char buf[PATH_MAX]; + const char *target = rewrite_path(path, buf, sizeof(buf)); + + init_ddnatv_hook(); + + const char *f_base = (path != NULL) ? strrchr(path, '/') : NULL; + f_base = (f_base != NULL) ? f_base + 1 : (path != NULL ? path : ""); + + const char *prog_name = exec_prog_name(target); + + if (strcmp(f_base, "ldconfig") == 0 || + strcmp(f_base, "ldconfig.real") == 0 || + strcmp(f_base, "systemd-machine-id-setup") == 0 || + strcmp(f_base, "systemd-sysusers") == 0 || + strcmp(f_base, "systemd-tmpfiles") == 0 || + strcmp(f_base, "start-stop-daemon") == 0 || + strcmp(prog_name, "ldconfig") == 0 || + strcmp(prog_name, "ldconfig.real") == 0 || + strcmp(prog_name, "systemd-machine-id-setup") == 0 || + strcmp(prog_name, "systemd-sysusers") == 0 || + strcmp(prog_name, "systemd-tmpfiles") == 0 || + strcmp(prog_name, "start-stop-daemon") == 0) { + pid_t child = fork(); + if (child == 0) { + if (is_ldconfig_name(f_base) || is_ldconfig_name(prog_name)) ldconfig_mark_cache(argv); + _exit(0); + } else if (child > 0) { + if (pid) *pid = child; + return 0; + } + return errno; + } + if (strcmp(f_base, "mountpoint") == 0 || strcmp(prog_name, "mountpoint") == 0) { + int is_virtual_fs = 0; + for (int i = 1; argv && argv[i]; i++) { + if (strcmp(argv[i], "/proc") == 0 || strcmp(argv[i], "/proc/") == 0 || + strcmp(argv[i], "/sys") == 0 || strcmp(argv[i], "/sys/") == 0 || + strcmp(argv[i], "/dev") == 0 || strcmp(argv[i], "/dev/") == 0 || + strcmp(argv[i], "/dev/pts") == 0) { + is_virtual_fs = 1; + break; + } + } + if (is_virtual_fs) { + pid_t child = fork(); + if (child == 0) { + _exit(0); + } else if (child > 0) { + if (pid) *pid = child; + return 0; + } + return errno; + } + } + if (strcmp(f_base, "policy-rc.d") == 0 || strcmp(prog_name, "policy-rc.d") == 0) { + pid_t child = fork(); + if (child == 0) { + _exit(101); + } else if (child > 0) { + if (pid) *pid = child; + return 0; + } + return errno; + } + + char *const *orig_ep = envp ? envp : environ; + int in_root = (g_ddnatv_root[0] != '\0' && strncmp(target, g_ddnatv_root, g_ddnatv_root_len) == 0) || is_mapped_guest_path(path); + + struct exec_probe probe; + char resolved_target[PATH_MAX]; + const char *elf_target = target; + if (in_root) { + elf_target = select_exec_target(target, resolved_target, sizeof(resolved_target), &probe); + if (!probe.is_elf && probe.n >= 2 && probe.hdr[0] == '#' && probe.hdr[1] == '!') { + // Like the kernel, give up after a few nested "#!" interpreters. + if (depth >= DDNATV_MAX_SHEBANG_DEPTH) return ELOOP; + const char *script = (caller_path && caller_path[0] != '/') ? caller_path : target; + return spawn_script(pid, script, &probe, file_actions, attrp, argv, orig_ep, depth); + } + } else { + probe.hdr[0] = '\0'; + probe.n = -1; + probe.is_elf = 0; + probe.is_dynamic = 0; + probe.android_interp = 0; + probe.e_machine = 0; + probe.mode = 0; + probe.runpath[0] = '\0'; + probe.runpath_is_rpath = 0; + } + + char ld_so[PATH_MAX]; + ld_so[0] = '\0'; + int has_ld_so = probe.is_dynamic ? find_dynamic_linker_for_machine(g_ddnatv_root, probe.e_machine, ld_so, sizeof(ld_so)) : 0; + + // Android programs get Android's environment, as execve() gives them: the glibc hook in + // LD_PRELOAD crashes them. + char **new_envp = in_root ? ensure_hook_in_envp(orig_ep, elf_target) : clean_env_for_system(orig_ep); + + int ret = -1; + if (has_ld_so && strcmp(elf_target, ld_so) != 0) { + ensure_exec_bit(elf_target, probe.mode); + if (probe.android_interp) prepare_static_program_ex(elf_target, 1); // a Go program built for Android (#42) + + char hook_buf[PATH_MAX]; + const char *hook_so = ddnatv_hook_library(hook_buf, sizeof(hook_buf)); + char ld_lib_path[PATH_MAX * 6]; + build_ld_library_path(orig_ep, probe.runpath, probe.runpath_is_rpath, elf_target, ld_lib_path, sizeof(ld_lib_path)); + char **new_argv = build_ldso_argv(ld_so, hook_so, ld_lib_path, elf_target, prog_name, argv); + if (!new_argv) { + ret = ENOMEM; + } else { + if (orig_posix_spawn) { + ret = orig_posix_spawn(pid, ld_so, file_actions, attrp, new_argv, new_envp); + if (ret == EACCES && chmod(ld_so, 0755) == 0) { + // ld.so without its exec bit (an unusual restore): fix it once and retry + ret = orig_posix_spawn(pid, ld_so, file_actions, attrp, new_argv, new_envp); + } + } + free(new_argv); + } + } else { + if (in_root && probe.is_elf && !probe.is_dynamic) prepare_static_program(elf_target); + if (orig_posix_spawn) { + ret = orig_posix_spawn(pid, target, file_actions, attrp, argv, new_envp); + } + } + + if (!orig_posix_spawn) { + pid_t child = fork(); + if (child < 0) { + free_modified_env(new_envp, orig_ep); + return errno; + } else if (child == 0) { + execve(target, argv, new_envp); + _exit(127); + } else { + if (pid) *pid = child; + free_modified_env(new_envp, orig_ep); + return 0; + } + } + free_modified_env(new_envp, orig_ep); + return ret; +} + +static int apply_closefrom(posix_spawn_file_actions_t *actions); + +int posix_spawn(pid_t *pid, const char *path, + const posix_spawn_file_actions_t *file_actions, + const posix_spawnattr_t *attrp, + char *const argv[], char *const envp[]) { + if (file_actions) { + int bad = apply_closefrom((posix_spawn_file_actions_t *)file_actions); + if (bad) return bad; + } + posix_spawnattr_t sanitized; + return ddnatv_posix_spawn(pid, path, file_actions, spawn_attr_for_android(attrp, &sanitized), argv, envp, 0); +} + +int posix_spawnp(pid_t *pid, const char *file, + const posix_spawn_file_actions_t *file_actions, + const posix_spawnattr_t *attrp, + char *const argv[], char *const envp[]) { + if (!file || !*file) { + return ENOENT; + } + if (strchr(file, '/')) { + return posix_spawn(pid, file, file_actions, attrp, argv, envp); + } + const char *path_env = NULL; + if (envp) { + for (char *const *ep = envp; *ep; ep++) { + if (strncmp(*ep, "PATH=", 5) == 0) { + path_env = *ep + 5; + break; + } + } + } + if (!path_env) path_env = getenv("PATH"); + if (!path_env) path_env = "/usr/bin:/bin"; + char path_copy[4096]; + strncpy(path_copy, path_env, sizeof(path_copy) - 1); + path_copy[sizeof(path_copy) - 1] = '\0'; + char *saveptr = NULL; + char *token = strtok_r(path_copy, ":", &saveptr); + while (token) { + char candidate[PATH_MAX]; + snprintf(candidate, sizeof(candidate), "%s/%s", token, file); + if (access(candidate, F_OK) == 0) { + return posix_spawn(pid, candidate, file_actions, attrp, argv, envp); + } + token = strtok_r(NULL, ":", &saveptr); + } + const char *standard_paths[] = {"/usr/bin", "/bin", "/usr/sbin", "/sbin", "/usr/local/bin", NULL}; + for (int i = 0; standard_paths[i] != NULL; i++) { + char candidate[PATH_MAX]; + snprintf(candidate, sizeof(candidate), "%s/%s", standard_paths[i], file); + if (access(candidate, F_OK) == 0) { + return posix_spawn(pid, candidate, file_actions, attrp, argv, envp); + } + } + // If not found in PATH or standard paths, return ENOENT without spawning zombie + return ENOENT; +} + +// ---- system(), popen() and pclose() --------------------------------------------------------- +// glibc starts the shell of system() and popen() from inside the C library (its own posix_spawn and +// execve), where the hook's execve() is not called: the kernel ran the machine's /bin/sh, which on +// Android is the system's own shell, and that one failed to link against Ubuntu's libraries +// ("CANNOT LINK EXECUTABLE sh: ...libc.so has bad ELF magic", #49). They start Ubuntu's /bin/sh here. + +extern char **environ; + +static pid_t start_shell(const char *command, int child_fd, int target_fd, const sigset_t *mask, + const struct sigaction *intr, const struct sigaction *quit, int close_popen_streams); + +// While system() waits, SIGINT and SIGQUIT are ignored; with several threads in system() at once, the +// first one in saves the dispositions and the last one out puts them back (as glibc does) +static pthread_mutex_t g_system_lock = PTHREAD_MUTEX_INITIALIZER; +static int g_system_waiting; +static struct sigaction g_system_intr, g_system_quit; + +// system(3) as POSIX describes it: SIGINT and SIGQUIT ignored and SIGCHLD blocked while it waits +int system(const char *command) { + if (!command) return access("/bin/sh", X_OK) == 0; // whether there is a shell (Ubuntu's) + pthread_mutex_lock(&g_system_lock); + if (g_system_waiting++ == 0) { + struct sigaction ignore; + memset(&ignore, 0, sizeof(ignore)); + ignore.sa_handler = SIG_IGN; + sigemptyset(&ignore.sa_mask); + sigaction(SIGINT, &ignore, &g_system_intr); + sigaction(SIGQUIT, &ignore, &g_system_quit); + } + struct sigaction intr = g_system_intr, quit = g_system_quit; + pthread_mutex_unlock(&g_system_lock); + sigset_t block, old; + sigemptyset(&block); + sigaddset(&block, SIGCHLD); + sigprocmask(SIG_BLOCK, &block, &old); + + int status = -1; + pid_t pid = start_shell(command, -1, -1, &old, &intr, &quit, 0); + if (pid > 0) { + while (waitpid(pid, &status, 0) < 0) { + if (errno != EINTR) { + status = -1; + break; + } + } + } + int saved = errno; + pthread_mutex_lock(&g_system_lock); + if (--g_system_waiting == 0) { + sigaction(SIGINT, &g_system_intr, NULL); + sigaction(SIGQUIT, &g_system_quit, NULL); + } + pthread_mutex_unlock(&g_system_lock); + sigprocmask(SIG_SETMASK, &old, NULL); + errno = saved; + return status; +} + +// The streams popen() made, with their shells: pclose() waits for the one of its stream +struct ddnatv_popen { + FILE *fp; + pid_t pid; + struct ddnatv_popen *next; +}; +static struct ddnatv_popen *g_popen_streams; +static pthread_mutex_t g_popen_lock = PTHREAD_MUTEX_INITIALIZER; + +static pid_t start_shell(const char *command, int child_fd, int target_fd, const sigset_t *mask, + const struct sigaction *intr, const struct sigaction *quit, int close_popen_streams) { + pid_t pid = fork(); + if (pid != 0) return pid; + if (intr) sigaction(SIGINT, intr, NULL); + if (quit) sigaction(SIGQUIT, quit, NULL); + if (mask) sigprocmask(SIG_SETMASK, mask, NULL); + if (child_fd >= 0) { + if (child_fd == target_fd) { + fcntl(target_fd, F_SETFD, 0); + } else if (dup2(child_fd, target_fd) < 0) { + _exit(127); + } + } + // POSIX: the streams of earlier popen() calls are not open in the new shell + if (close_popen_streams) { + for (struct ddnatv_popen *p = g_popen_streams; p; p = p->next) { + int fd = fileno(p->fp); + if (fd >= 0 && fd != target_fd) close(fd); + } + } + char *const argv[] = { (char *)"sh", (char *)"-c", (char *)command, NULL }; + ddnatv_execve("/bin/sh", argv, environ, 0); + _exit(127); +} + +FILE *popen(const char *command, const char *type) { + if (!command || !type || (type[0] != 'r' && type[0] != 'w')) { + errno = EINVAL; + return NULL; + } + int reading = type[0] == 'r'; + int keep_on_exec = strchr(type, 'e') == NULL; + int fds[2]; + if (pipe2(fds, O_CLOEXEC) != 0) return NULL; + int parent_end = reading ? fds[0] : fds[1]; + int child_end = reading ? fds[1] : fds[0]; + struct ddnatv_popen *entry = (struct ddnatv_popen *)malloc(sizeof(*entry)); + if (!entry) { + close(fds[0]); + close(fds[1]); + errno = ENOMEM; + return NULL; + } + pthread_mutex_lock(&g_popen_lock); + pid_t pid = start_shell(command, child_end, reading ? STDOUT_FILENO : STDIN_FILENO, NULL, NULL, NULL, 1); + int saved = errno; + close(child_end); + FILE *fp = NULL; + if (pid > 0) { + if (keep_on_exec) fcntl(parent_end, F_SETFD, 0); + fp = fdopen(parent_end, reading ? "r" : "w"); + saved = errno; + } + if (!fp) { + close(parent_end); + pthread_mutex_unlock(&g_popen_lock); + free(entry); + if (pid > 0) { + // A shell nobody will read or write: it is not left to run (or waited for while it does) + kill(pid, SIGKILL); + while (waitpid(pid, NULL, 0) < 0 && errno == EINTR) {} + } + errno = saved; + return NULL; + } + entry->fp = fp; + entry->pid = pid; + entry->next = g_popen_streams; + g_popen_streams = entry; + pthread_mutex_unlock(&g_popen_lock); + return fp; +} + +int pclose(FILE *stream) { + struct ddnatv_popen *found = NULL; + pthread_mutex_lock(&g_popen_lock); + for (struct ddnatv_popen **p = &g_popen_streams; *p; p = &(*p)->next) { + if ((*p)->fp == stream) { + found = *p; + *p = found->next; + break; + } + } + // Closed before the lock is let go: a popen() in between would fork a shell that keeps this pipe open, + // and the shell of this stream would wait for its end of it + if (found) fclose(stream); + pthread_mutex_unlock(&g_popen_lock); + if (!found) { + // Not one of ours: glibc's own + static int (*orig_pclose)(FILE *) = NULL; + if (!orig_pclose) orig_pclose = (int (*)(FILE *))dlsym(RTLD_NEXT, "pclose"); + if (!orig_pclose) { + errno = ECHILD; + return -1; + } + return orig_pclose(stream); + } + pid_t pid = found->pid; + free(found); + int status = -1; + pid_t r; + do { + r = waitpid(pid, &status, 0); + } while (r < 0 && errno == EINTR); + return r < 0 ? -1 : status; +} + +#endif /* !DDNATV_HOST_TEST */ diff --git a/tools/nativeguest/hook/exec_probe.c b/tools/nativeguest/hook/exec_probe.c new file mode 100644 index 0000000000..f877175e58 --- /dev/null +++ b/tools/nativeguest/hook/exec_probe.c @@ -0,0 +1,1027 @@ +// Part of libddnatv-hook.so, included by ddnatv-hook.c (one translation unit, so the +// parts share their static functions and are built with the same command as before). +// +// What execve() and posix_spawn() need to know about a program: its ld.so, whether it is a +// dynamic ELF, a script and its interpreter, and the argument lists built from that. + +// Relative exec paths ("./configure", "bin/tool", "../x.sh") must go through the same +// DDNATV_ROOT routing as absolute ones. Without this they were mistaken for Android host +// binaries and handed to the kernel unchanged, which then resolved "#!/bin/bash" (or a glibc +// PT_INTERP) literally and failed with "bad interpreter: No such file or directory". +// getcwd() is not virtualised, so cwd + path is a real path that starts with DDNATV_ROOT. +static const char *absolutize_exec_path(const char *path, char *buffer, size_t bufsize) { + if (!path || path[0] == '\0' || path[0] == '/') return path; + char cwd[PATH_MAX]; + static char *(*real_getcwd)(char *, size_t) = NULL; + if (!real_getcwd) real_getcwd = (char *(*)(char *, size_t))dlsym(RTLD_NEXT, "getcwd"); + if (!real_getcwd || !real_getcwd(cwd, sizeof(cwd))) return path; + + while (path[0] == '.' && path[1] == '/') { + path += 2; + while (path[0] == '/') path++; + } + if (path[0] == '\0' || strcmp(path, ".") == 0) return path; + + size_t cwd_len = strlen(cwd); + while (cwd_len > 1 && cwd[cwd_len - 1] == '/') cwd[--cwd_len] = '\0'; + int n = snprintf(buffer, bufsize, "%s%s%s", cwd, (cwd_len == 1 && cwd[0] == '/') ? "" : "/", path); + if (n < 0 || (size_t)n >= bufsize) return path; + return buffer; +} + +static char s_cached_ld_so[PATH_MAX] = {0}; +static char s_cached_ld_root[PATH_MAX] = {0}; +static uint16_t s_cached_e_machine = 0; + +// ld.so inside `ddnatv_root` for an ELF with machine type `e_machine` (0 when unknown). +static int find_dynamic_linker_for_machine(const char *ddnatv_root, uint16_t e_machine, char *out_ld_so, size_t max_len) { + if (!ddnatv_root || ddnatv_root[0] == '\0' || !out_ld_so || max_len == 0) return 0; + + if (s_cached_ld_so[0] != '\0' && strcmp(s_cached_ld_root, ddnatv_root) == 0 && + e_machine != 0 && e_machine == s_cached_e_machine) { + ddnatv_copy_cstr(out_ld_so, max_len, s_cached_ld_so); + return 1; + } + + const char *aarch64_cands[] = { + "/usr/lib/aarch64-linux-gnu/ld-linux-aarch64.so.1", + "/usr/lib/aarch64-linux-gnu/ld-2.39.so", + "/lib/ld-linux-aarch64.so.1", + "/lib/aarch64-linux-gnu/ld-linux-aarch64.so.1", + "/usr/lib/ld-linux-aarch64.so.1", + "/usr/lib64/ld-linux-aarch64.so.1", + "/lib64/ld-linux-aarch64.so.1", + "/lib/aarch64-linux-gnu/ld-2.39.so", + NULL + }; + + const char *armhf_cands[] = { + "/usr/lib/arm-linux-gnueabihf/ld-linux-armhf.so.3", + "/usr/lib/arm-linux-gnueabihf/ld-2.39.so", + "/lib/ld-linux-armhf.so.3", + "/lib/arm-linux-gnueabihf/ld-linux-armhf.so.3", + "/usr/lib/ld-linux-armhf.so.3", + "/lib/arm-linux-gnueabihf/ld-2.39.so", + "/usr/lib/arm-linux-gnueabi/ld-linux.so.3", + "/lib/arm-linux-gnueabi/ld-linux.so.3", + NULL + }; + + const char *x86_64_cands[] = { + "/lib64/ld-linux-x86-64.so.2", + "/usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2", + "/usr/lib/x86_64-linux-gnu/ld-2.39.so", + "/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2", + NULL + }; + + const char **primary = NULL; + const char **secondary = NULL; + + if (e_machine == 183) { // EM_AARCH64 + primary = aarch64_cands; + secondary = NULL; + } else if (e_machine == 40) { // EM_ARM + primary = armhf_cands; + secondary = NULL; + } else if (e_machine == 62) { // EM_X86_64 + primary = x86_64_cands; + secondary = NULL; + } else if (e_machine == 3) { // EM_386 + primary = NULL; + secondary = NULL; + } else { + #if defined(__aarch64__) + primary = aarch64_cands; + secondary = armhf_cands; + #elif defined(__arm__) + primary = armhf_cands; + secondary = aarch64_cands; + #else + primary = x86_64_cands; + #endif + } + + if (primary) { + for (int i = 0; primary[i] != NULL; i++) { + snprintf(out_ld_so, max_len, "%s%s", ddnatv_root, primary[i]); + if (access(out_ld_so, F_OK) == 0) { + if (e_machine != 0) { + ddnatv_copy_cstr(s_cached_ld_so, sizeof(s_cached_ld_so), out_ld_so); + ddnatv_copy_cstr(s_cached_ld_root, sizeof(s_cached_ld_root), ddnatv_root); + s_cached_e_machine = e_machine; + } + return 1; + } + } + } + if (secondary) { + for (int i = 0; secondary[i] != NULL; i++) { + snprintf(out_ld_so, max_len, "%s%s", ddnatv_root, secondary[i]); + if (access(out_ld_so, F_OK) == 0) { + if (e_machine != 0) { + ddnatv_copy_cstr(s_cached_ld_so, sizeof(s_cached_ld_so), out_ld_so); + ddnatv_copy_cstr(s_cached_ld_root, sizeof(s_cached_ld_root), ddnatv_root); + s_cached_e_machine = e_machine; + } + return 1; + } + } + } + + return 0; +} + +#ifdef DDNATV_HOST_TEST +// Test entry point taking a program instead of its machine type (ddnatv-pty.c, which the tests +// include too, has a find_dynamic_linker of its own). +static int hook_find_dynamic_linker(const char *ddnatv_root, const char *cmd, char *out_ld_so, size_t max_len) { + if (!ddnatv_root || ddnatv_root[0] == '\0' || !out_ld_so || max_len == 0) return 0; + + uint16_t e_machine = 0; + if (cmd && cmd[0] != '\0') { + int fd = open(cmd, O_RDONLY); + if (fd >= 0) { + unsigned char ehdr[20]; + ssize_t n = read(fd, ehdr, sizeof(ehdr)); + close(fd); + if (n >= 20 && ehdr[0] == 0x7f && ehdr[1] == 'E' && ehdr[2] == 'L' && ehdr[3] == 'F') { + e_machine = (uint16_t)(ehdr[18] | (ehdr[19] << 8)); + } + } + } + return find_dynamic_linker_for_machine(ddnatv_root, e_machine, out_ld_so, max_len); +} +#endif + +static int check_elf_dynamic(int fd, const unsigned char *ehdr, ssize_t n) { + if (n < 52 || ehdr[0] != 0x7f || ehdr[1] != 'E' || ehdr[2] != 'L' || ehdr[3] != 'F') { + return 0; + } + if ((ehdr[4] != 1 && ehdr[4] != 2) || ehdr[5] != 1 /* ELFDATA2LSB */) { + return 0; + } + int is_64 = (ehdr[4] == 2); + uint64_t phoff = 0; + uint16_t phentsize = 0; + uint16_t phnum = 0; + if (is_64) { + if (n < 64) return 0; + memcpy(&phoff, ehdr + 32, sizeof(uint64_t)); + memcpy(&phentsize, ehdr + 54, sizeof(uint16_t)); + memcpy(&phnum, ehdr + 56, sizeof(uint16_t)); + } else { + uint32_t phoff32 = 0; + memcpy(&phoff32, ehdr + 28, sizeof(uint32_t)); + phoff = phoff32; + memcpy(&phentsize, ehdr + 42, sizeof(uint16_t)); + memcpy(&phnum, ehdr + 44, sizeof(uint16_t)); + } + uint16_t min_phentsize = is_64 ? 56 : 32; + if (phoff == 0 || phentsize < min_phentsize || phentsize > 4096 || phnum == 0) { + return 0; + } + if ((off_t)phoff < 0 || (uint64_t)(off_t)phoff != phoff) { + return 0; + } + if (lseek(fd, (off_t)phoff, SEEK_SET) < 0) { + return 0; + } + for (int i = 0; i < phnum && i < 128; i++) { + uint32_t p_type = 0; + if (read(fd, &p_type, sizeof(p_type)) != sizeof(p_type)) break; + if (p_type == 3 /* PT_INTERP */) { + return 1; + } + if (lseek(fd, (off_t)(phentsize - sizeof(p_type)), SEEK_CUR) < 0) break; + } + return 0; +} + +// check_elf_dynamic() on the bytes already read: 1 or 0, or -1 when a program header lies past +// the buffer and the file has to be read after all. Program headers normally follow the ELF +// header directly, so this saves the lseek()/read() pairs. +static int elf_interp_in_buffer(const unsigned char *ehdr, ssize_t n) { + if (n < 52 || ehdr[0] != 0x7f || ehdr[1] != 'E' || ehdr[2] != 'L' || ehdr[3] != 'F') { + return 0; + } + if ((ehdr[4] != 1 && ehdr[4] != 2) || ehdr[5] != 1 /* ELFDATA2LSB */) { + return 0; + } + int is_64 = (ehdr[4] == 2); + uint64_t phoff = 0; + uint16_t phentsize = 0; + uint16_t phnum = 0; + if (is_64) { + if (n < 64) return 0; + memcpy(&phoff, ehdr + 32, sizeof(uint64_t)); + memcpy(&phentsize, ehdr + 54, sizeof(uint16_t)); + memcpy(&phnum, ehdr + 56, sizeof(uint16_t)); + } else { + uint32_t phoff32 = 0; + memcpy(&phoff32, ehdr + 28, sizeof(uint32_t)); + phoff = phoff32; + memcpy(&phentsize, ehdr + 42, sizeof(uint16_t)); + memcpy(&phnum, ehdr + 44, sizeof(uint16_t)); + } + uint16_t min_phentsize = is_64 ? 56 : 32; + if (phoff == 0 || phentsize < min_phentsize || phentsize > 4096 || phnum == 0) { + return 0; + } + if ((off_t)phoff < 0 || (uint64_t)(off_t)phoff != phoff) { + return 0; + } + for (int i = 0; i < phnum && i < 128; i++) { + uint64_t off = phoff + (uint64_t)i * phentsize; + if (off > (uint64_t)n || (uint64_t)n - off < sizeof(uint32_t)) return -1; + uint32_t p_type = 0; + memcpy(&p_type, ehdr + off, sizeof(p_type)); + if (p_type == 3 /* PT_INTERP */) { + return 1; + } + } + return 0; +} + +// Whether the interpreter of a 64-bit ELF is Android's own linker (/system/bin/linker64). A Go +// program built with GOOS=android is a PIE that asks for it, so it has a PT_INTERP and looks like a +// dynamic program, but it has no C library and makes its own system calls like every Go program +// (#42). `h` holds the `n` first bytes of the file; the program headers usually are among them. +static int elf_interp_is_android(int fd, const unsigned char *h, ssize_t n) { + if (n < 64 || h[4] != 2) return 0; + uint64_t phoff = 0; + uint16_t phentsize = 0, phnum = 0; + memcpy(&phoff, h + 32, sizeof(phoff)); + memcpy(&phentsize, h + 54, sizeof(phentsize)); + memcpy(&phnum, h + 56, sizeof(phnum)); + if (phoff == 0 || phoff > (1ULL << 40) || phentsize < 56 || phentsize > 4096 || phnum == 0) return 0; + for (int i = 0; i < phnum && i < 128; i++) { + unsigned char e[56]; + uint64_t off = phoff + (uint64_t)i * phentsize; + if (off + sizeof(e) <= (uint64_t)n) { + memcpy(e, h + off, sizeof(e)); + } else if (pread(fd, e, sizeof(e), (off_t)off) != (ssize_t)sizeof(e)) { + return 0; + } + uint32_t p_type = 0; + memcpy(&p_type, e, sizeof(p_type)); + if (p_type != 3 /* PT_INTERP */) continue; + uint64_t p_offset = 0, p_filesz = 0; + memcpy(&p_offset, e + 8, sizeof(p_offset)); + memcpy(&p_filesz, e + 32, sizeof(p_filesz)); + char interp[48]; + size_t want = p_filesz < sizeof(interp) - 1 ? (size_t)p_filesz : sizeof(interp) - 1; + if (want == 0 || p_offset > (1ULL << 40)) return 0; + ssize_t got = pread(fd, interp, want, (off_t)p_offset); + if (got <= 0) return 0; + interp[got] = '\0'; + return strncmp(interp, "/system/bin/linker", 18) == 0; + } + return 0; +} + +// The kernel follows at most four nested "#!" interpreters, then fails with ELOOP. +#define DDNATV_MAX_SHEBANG_DEPTH 4 + +// What execve()/posix_spawn() need to know about the file they are about to start, from a +// single open()/read() (they used to open it up to three times). +struct exec_probe { + char hdr[256]; // first bytes of the file, NUL-terminated for "#!" parsing + ssize_t n; // bytes read; -1 when the file could not be opened + int is_elf; + int is_dynamic; // ELF with PT_INTERP: has to be started through ld.so + int android_interp; // AArch64 ELF whose PT_INTERP is Android's linker (a Go program for GOOS=android) + uint16_t e_machine; + mode_t mode; // st_mode, filled in for dynamic ELF files only + int runpath_is_rpath; // runpath holds a DT_RPATH (searched before LD_LIBRARY_PATH), not a DT_RUNPATH + char runpath[1024]; // DT_RUNPATH (else DT_RPATH) of a dynamic ELF as written in it, or "" +}; + +// The DT_RUNPATH of a dynamic ELF (its DT_RPATH when it has none, as ld.so does) into `out`, as +// written in the file. ld.so looks for the program's libraries there, but it opens those +// directories itself, without the hook: an absolute one such as /usr/lib/aarch64-linux-gnu/android +// (Ubuntu's adb and fastboot keep their own libbase, liblog, libssl... there) is looked for in +// Android's tree and the program does not start. The exec adds the rootfs copies to the +// --library-path it gives ld.so (build_ld_library_path). Returns 1 for a DT_RUNPATH, 2 for a +// DT_RPATH, 0 when there is neither. The headers are read in pieces, so that their number does +// not matter; a list longer than `out` keeps its first whole entries (ld.so still has the rest). +static int elf_read_dynamic(int fd, const unsigned char *h, ssize_t n, char *out, size_t outsz, + char *needed, size_t needed_size, int *needed_count); + +static int elf_read_runpath(int fd, const unsigned char *h, ssize_t n, char *out, size_t outsz) { + return elf_read_dynamic(fd, h, n, out, outsz, NULL, 0, NULL); +} + +// elf_read_runpath(), and with `needed` the DT_NEEDED names too: one after the other, each ended by a +// NUL, their number in *needed_count (as many as fit). +#define ELF_MAX_NEEDED 128 +static int elf_read_dynamic(int fd, const unsigned char *h, ssize_t n, char *out, size_t outsz, + char *needed, size_t needed_size, int *needed_count) { + if (needed_count) *needed_count = 0; + if (!out || outsz < 2) return 0; + out[0] = '\0'; + if (n < 52 || h[0] != 0x7f || h[1] != 'E' || h[2] != 'L' || h[3] != 'F' || h[5] != 1 /* LSB */) return 0; + int is_64 = (h[4] == 2); + if (!is_64 && h[4] != 1) return 0; + if (is_64 && n < 64) return 0; + uint64_t phoff = 0; + uint16_t phentsize = 0, phnum = 0; + if (is_64) { + memcpy(&phoff, h + 32, 8); + memcpy(&phentsize, h + 54, 2); + memcpy(&phnum, h + 56, 2); + } else { + uint32_t off32 = 0; + memcpy(&off32, h + 28, 4); + phoff = off32; + memcpy(&phentsize, h + 42, 2); + memcpy(&phnum, h + 44, 2); + } + size_t ph_size = is_64 ? 56 : 32; + if (phoff == 0 || phoff > (1ULL << 40) || phentsize < ph_size || phentsize > 1024 || phnum == 0) return 0; + + // PT_DYNAMIC, and the PT_LOAD segments that map an address of the file to its offset + unsigned char buf[2048]; + uint64_t dyn_off = 0, dyn_size = 0; + uint64_t load_vaddr[16], load_off[16], load_size[16]; + int loads = 0; + size_t per_read = sizeof(buf) / phentsize; + for (size_t first = 0; first < phnum; first += per_read) { + size_t count = phnum - first < per_read ? phnum - first : per_read; + size_t bytes = count * phentsize; + if (pread(fd, buf, bytes, (off_t)(phoff + first * phentsize)) != (ssize_t)bytes) return 0; + for (size_t i = 0; i < count; i++) { + const unsigned char *e = buf + i * phentsize; + uint32_t type = 0; + memcpy(&type, e, 4); + uint64_t off = 0, vaddr = 0, filesz = 0; + if (is_64) { + memcpy(&off, e + 8, 8); + memcpy(&vaddr, e + 16, 8); + memcpy(&filesz, e + 32, 8); + } else { + uint32_t v = 0; + memcpy(&v, e + 4, 4); off = v; + memcpy(&v, e + 8, 4); vaddr = v; + memcpy(&v, e + 16, 4); filesz = v; + } + if (type == 2 /* PT_DYNAMIC */) { + dyn_off = off; + dyn_size = filesz; + } else if (type == 1 /* PT_LOAD */ && loads < 16) { + load_vaddr[loads] = vaddr; + load_off[loads] = off; + load_size[loads] = filesz; + loads++; + } + } + } + if (dyn_size == 0 || dyn_off > (1ULL << 40)) return 0; + if (dyn_size > (1u << 20)) dyn_size = 1u << 20; + + size_t ent = is_64 ? 16 : 8; + uint64_t strtab = 0, runpath = 0, rpath = 0; + int has_strtab = 0, has_runpath = 0, has_rpath = 0, done = 0; + uint64_t needed_at[ELF_MAX_NEEDED]; + int nneeded = 0; + for (uint64_t at = 0; at + ent <= dyn_size && !done;) { + size_t want = dyn_size - at < sizeof(buf) ? (size_t)(dyn_size - at) : sizeof(buf); + want -= want % ent; + ssize_t got = pread(fd, buf, want, (off_t)(dyn_off + at)); + if (got < (ssize_t)ent) break; + for (size_t i = 0; i + ent <= (size_t)got; i += ent) { + int64_t tag = 0; + uint64_t val = 0; + if (is_64) { + memcpy(&tag, buf + i, 8); + memcpy(&val, buf + i + 8, 8); + } else { + int32_t tv = 0; + uint32_t vv = 0; + memcpy(&tv, buf + i, 4); + memcpy(&vv, buf + i + 4, 4); + tag = tv; + val = vv; + } + if (tag == 0 /* DT_NULL */) { done = 1; break; } + if (tag == 5 /* DT_STRTAB */) { strtab = val; has_strtab = 1; } + else if (tag == 29 /* DT_RUNPATH */) { runpath = val; has_runpath = 1; } + else if (tag == 15 /* DT_RPATH */) { rpath = val; has_rpath = 1; } + else if (tag == 1 /* DT_NEEDED */ && needed && nneeded < ELF_MAX_NEEDED) needed_at[nneeded++] = val; + } + at += (uint64_t)got - (uint64_t)got % ent; + } + if (!has_strtab || (!has_runpath && !has_rpath)) return 0; + uint64_t str_index = has_runpath ? runpath : rpath; + + // DT_STRTAB is an address: the file offset is that of the PT_LOAD segment holding it + uint64_t strtab_off = 0; + int mapped = 0; + for (int i = 0; i < loads; i++) { + if (strtab >= load_vaddr[i] && strtab - load_vaddr[i] < load_size[i]) { + strtab_off = load_off[i] + (strtab - load_vaddr[i]); + mapped = 1; + break; + } + } + if (!mapped || str_index > (1ULL << 32)) return 0; + if (needed && needed_count && needed_size > 0) { + size_t used = 0; + for (int i = 0; i < nneeded; i++) { + char name[256]; + ssize_t got = pread(fd, name, sizeof(name) - 1, (off_t)(strtab_off + needed_at[i])); + if (got <= 0) continue; + name[got] = '\0'; + size_t len = strnlen(name, (size_t)got); + if (len == 0 || len == (size_t)got || used + len + 1 > needed_size) continue; + memcpy(needed + used, name, len + 1); + used += len + 1; + (*needed_count)++; + } + } + ssize_t len = pread(fd, out, outsz - 1, (off_t)(strtab_off + str_index)); + if (len <= 0) { + out[0] = '\0'; + return 0; + } + out[len] = '\0'; + if (strnlen(out, (size_t)len) == (size_t)len) { + // Longer than `out`: its whole entries up to there + char *last = strrchr(out, ':'); + if (!last) { + out[0] = '\0'; + return 0; + } + *last = '\0'; + } + if (out[0] == '\0') return 0; + return has_runpath ? 1 : 2; +} + +// Appends dir[0..len) to the ':'-separated out_buf[0..used) unless it is there already. +static size_t append_lib_dir(char *out_buf, size_t out_size, size_t used, const char *dir, size_t len) { + if (len == 0 || lib_dir_list_has(out_buf, dir, len)) return used; + size_t need = (used > 0 ? 1 : 0) + len; + if (used + need >= out_size) return used; + if (used > 0) out_buf[used++] = ':'; + memcpy(out_buf + used, dir, len); + used += len; + out_buf[used] = '\0'; + return used; +} + +// The directories of `runpath` (a DT_RUNPATH or DT_RPATH), in their order, appended to +// out_buf[0..used): the absolute ones that lie in the rootfs mapped into it, and those that start +// with $ORIGIN with it replaced by the directory of `exe` (the program's real path, in the rootfs +// already), so that both keep their places. ld.so opens them itself, without the hook, so +// /usr/lib/aarch64-linux-gnu/android would be looked for in Android's tree. Others ($LIB, +// $PLATFORM, directories outside the rootfs) are left to ld.so. Returns the new length. +static size_t append_runpath_dirs(char *out_buf, size_t out_size, size_t used, const char *runpath, const char *exe) { + size_t origin_len = 0; + if (exe) { + const char *slash = strrchr(exe, '/'); + if (slash) origin_len = slash > exe ? (size_t)(slash - exe) : 1; + } + for (const char *p = runpath; p && *p;) { + const char *end = strchr(p, ':'); + size_t len = end ? (size_t)(end - p) : strlen(p); + if (len > 0 && len < PATH_MAX) { + char tok[PATH_MAX]; + memcpy(tok, p, len); + tok[len] = '\0'; + size_t skip = 0; + if (strncmp(tok, "$ORIGIN", 7) == 0) skip = 7; + else if (strncmp(tok, "${ORIGIN}", 9) == 0) skip = 9; + if (skip && (tok[skip] == '\0' || tok[skip] == '/') && origin_len > 0 && !strchr(tok + skip, '$')) { + char full[PATH_MAX]; + size_t rest = len - skip; + if (origin_len + rest < sizeof(full)) { + memcpy(full, exe, origin_len); + memcpy(full + origin_len, tok + skip, rest); + full[origin_len + rest] = '\0'; + used = append_lib_dir(out_buf, out_size, used, full, origin_len + rest); + } + } else if (tok[0] == '/' && len > 1 && !strchr(tok, '$')) { + char rw_buf[PATH_MAX]; + const char *rw = rewrite_path_in_root(tok, rw_buf, sizeof(rw_buf)); + if (strcmp(rw, tok) != 0) used = append_lib_dir(out_buf, out_size, used, rw, strlen(rw)); + } + } + if (!end) break; + p = end + 1; + } + return used; +} + +// Whether dir[0..len) is one of the rootfs library directories The native userland adds itself (also what its +// shells export in LD_LIBRARY_PATH). +static int is_rootfs_default_lib_dir(const char *dir, size_t len) { + if (len <= g_ddnatv_root_len || memcmp(dir, g_ddnatv_root, g_ddnatv_root_len) != 0) return 0; + const char *rel = dir + g_ddnatv_root_len; + size_t rel_len = len - g_ddnatv_root_len; + for (size_t i = 0; i < sizeof(k_rootfs_lib_dirs) / sizeof(k_rootfs_lib_dirs[0]); i++) { + if (strlen(k_rootfs_lib_dirs[i]) == rel_len && memcmp(k_rootfs_lib_dirs[i], rel, rel_len) == 0) return 1; + } + return 0; +} + +// Where the program is among the arguments of ld.so started directly (ldd runs "ld.so PROGRAM" with +// LD_TRACE_LOADED_OBJECTS set; "ld.so --list PROGRAM" and "ld.so --verify PROGRAM" are the same): its +// index, or -1 when there is none; *has_library_path says whether the caller gives --library-path itself. +static int ldso_program_index(char *const argv[], int *has_library_path) { + if (has_library_path) *has_library_path = 0; + if (!argv || !argv[0]) return -1; + static const char *const with_value[] = { "--library-path", "--preload", "--argv0", "--inhibit-rpath", "--audit", + "--glibc-hwcaps-prepend", "--glibc-hwcaps-mask" }; + for (int i = 1; argv[i]; i++) { + const char *a = argv[i]; + if (strcmp(a, "--library-path") == 0 && has_library_path) *has_library_path = 1; + if (a[0] != '-' || a[1] != '-') return i; + if (a[2] == '\0') return argv[i + 1] ? i + 1 : -1; + int takes_value = 0; + for (size_t k = 0; k < sizeof(with_value) / sizeof(with_value[0]); k++) { + if (strcmp(a, with_value[k]) == 0) takes_value = 1; + } + if (takes_value) { + if (!argv[i + 1]) return -1; + i++; + } + } + return -1; +} + +// ---- dlopen() of a library with a RUNPATH --------------------------------------------------- +// A library that a program loads with dlopen() looks for its own dependencies in its RUNPATH, which ld.so +// opens without the hook: PulseAudio's modules (RUNPATH /usr/lib/aarch64-linux-gnu/pulseaudio) and many +// plugins did not load. Before the real dlopen(), the dependencies found in the rootfs copies of those +// directories are loaded by their full path, depth first; ld.so then finds them loaded, by their name. +#define DLOPEN_PRELOAD_DEPTH 6 + +static int dlopen_preload_deps(const char *path, void *(*orig_dlopen)(const char *, int), int depth) { + if (depth > DLOPEN_PRELOAD_DEPTH || !path || path[0] != '/') return 0; + int fd = (int)syscall(SYS_openat, AT_FDCWD, path, O_RDONLY | O_CLOEXEC); + if (fd < 0) return 0; + unsigned char hdr[64]; + ssize_t n = pread(fd, hdr, sizeof(hdr), 0); + char runpath[1024]; + char needed[4096]; + int count = 0; + int kind = n > 0 ? elf_read_dynamic(fd, hdr, n, runpath, sizeof(runpath), needed, sizeof(needed), &count) : 0; + syscall(SYS_close, fd); + if (!kind || count == 0) return 0; + // the RUNPATH's directories, mapped into the rootfs ($ORIGIN from this library's own place) + char dirs[PATH_MAX * 2]; + dirs[0] = '\0'; + size_t used = append_runpath_dirs(dirs, sizeof(dirs), 0, runpath, path); + if (used == 0) return 0; + int loaded = 0; + const char *name = needed; + for (int i = 0; i < count; i++, name += strlen(name) + 1) { + if (strchr(name, '/')) continue; + void *already = orig_dlopen(name, RTLD_LAZY | RTLD_NOLOAD); + if (already) { + dlclose(already); + continue; + } + for (const char *p = dirs; *p;) { + const char *end = strchr(p, ':'); + size_t len = end ? (size_t)(end - p) : strlen(p); + char candidate[PATH_MAX]; + if (len + 1 + strlen(name) < sizeof(candidate)) { + memcpy(candidate, p, len); + candidate[len] = '/'; + strcpy(candidate + len + 1, name); + int probe_fd = (int)syscall(SYS_openat, AT_FDCWD, candidate, O_PATH | O_CLOEXEC); + if (probe_fd >= 0) { + syscall(SYS_close, probe_fd); + dlopen_preload_deps(candidate, orig_dlopen, depth + 1); + if (orig_dlopen(candidate, RTLD_LAZY | RTLD_LOCAL)) loaded++; + break; + } + } + if (!end) break; + p = end + 1; + } + } + return loaded; +} + +// ---- /etc/ld.so.conf ------------------------------------------------------------------------- +// The library directories of /etc/ld.so.conf and the files it includes (/etc/ld.so.conf.d/*.conf), +// mapped into the rootfs. On Linux, ldconfig turns them into /etc/ld.so.cache, which ld.so reads. +// Here ld.so would read Android's /etc/ld.so.cache, and the paths in the cache are not mapped into +// the rootfs either, so the exec hands the directories to ld.so in --library-path instead. They are +// read again only when /etc/ld.so.conf, /etc/ld.so.conf.d or /etc/ld.so.cache changes: a .conf file +// added or removed counts at once, an edit of one after ldconfig, as on Linux. +#define LDCONF_MAX_DEPTH 4 +#define LDCONF_MAX_INCLUDES 64 + +struct ldconf_sig { + int64_t t[6]; // mtimes (seconds, nanoseconds) of ld.so.conf, ld.so.conf.d and ld.so.cache; 0 if missing +}; + +static struct { + int valid; + struct ldconf_sig sig; + char root[PATH_MAX]; + char dirs[PATH_MAX * 2]; +} g_ldconf; +static pthread_mutex_t g_ldconf_lock = PTHREAD_MUTEX_INITIALIZER; + +// fstatat() behind the hook's stat emulation (which would map the hard link table in a child that is +// about to exec): only the times are wanted. +static int ldconf_stat_time(const char *real, int64_t *sec, int64_t *nsec) { + static int (*f)(int, const char *, struct stat64 *, int) = NULL; + int (*fn)(int, const char *, struct stat64 *, int) = __atomic_load_n(&f, __ATOMIC_RELAXED); + if (!fn) { + fn = (int (*)(int, const char *, struct stat64 *, int))dlsym(RTLD_NEXT, "fstatat64"); + if (!fn) return -1; + __atomic_store_n(&f, fn, __ATOMIC_RELAXED); + } + struct stat64 st; + if (fn(AT_FDCWD, real, &st, 0) != 0) return -1; + *sec = (int64_t)st.st_mtim.tv_sec; + *nsec = (int64_t)st.st_mtim.tv_nsec; + return 0; +} + +static void ldconf_signature(struct ldconf_sig *sig) { + static const char *const files[3] = { "/etc/ld.so.conf", "/etc/ld.so.conf.d", "/etc/ld.so.cache" }; + for (int i = 0; i < 3; i++) { + char path[PATH_MAX]; + size_t n = ddnatv_append(path, sizeof(path), 0, g_ddnatv_root, g_ddnatv_root_len); + ddnatv_append(path, sizeof(path), n, files[i], strlen(files[i])); + if (ldconf_stat_time(path, &sig->t[i * 2], &sig->t[i * 2 + 1]) != 0) { + sig->t[i * 2] = 0; + sig->t[i * 2 + 1] = 0; + } + } +} + +// The whole of the rootfs file `logical` (an absolute path inside Ubuntu) into buf, NUL-terminated. +static ssize_t ldconf_read(const char *logical, char *buf, size_t bufsz) { + char rw_buf[PATH_MAX]; + const char *real = rewrite_path_in_root(logical, rw_buf, sizeof(rw_buf)); + int fd = (int)syscall(SYS_openat, AT_FDCWD, real, O_RDONLY | O_CLOEXEC); + if (fd < 0) return -1; + size_t got = 0; + while (got + 1 < bufsz) { + ssize_t n = (ssize_t)syscall(SYS_read, fd, buf + got, bufsz - 1 - got); + if (n <= 0) break; + got += (size_t)n; + } + syscall(SYS_close, fd); + buf[got] = '\0'; + return (ssize_t)got; +} + +static int ldconf_name_cmp(const void *a, const void *b) { + return strcmp(*(const char *const *)a, *(const char *const *)b); +} + +static void ldconf_parse_file(const char *logical, char *out, size_t outsz, size_t *used, int depth, int *includes); + +// include PATTERN: the files of one directory that match a shell pattern, in name order (as glob()). +// Patterns in the directory part are not supported (Ubuntu uses /etc/ld.so.conf.d/*.conf). +static void ldconf_include(const char *pattern, char *out, size_t outsz, size_t *used, int depth, int *includes) { + const char *slash = strrchr(pattern, '/'); + if (!slash || slash == pattern) return; + size_t dir_len = (size_t)(slash - pattern); + if (dir_len >= PATH_MAX || memchr(pattern, '*', dir_len) || memchr(pattern, '?', dir_len) || memchr(pattern, '[', dir_len)) return; + char dir[PATH_MAX]; + memcpy(dir, pattern, dir_len); + dir[dir_len] = '\0'; + const char *name_pattern = slash + 1; + if (!strpbrk(name_pattern, "*?[")) { + ldconf_parse_file(pattern, out, outsz, used, depth + 1, includes); + return; + } + char rw_buf[PATH_MAX]; + const char *real = rewrite_path_in_root(dir, rw_buf, sizeof(rw_buf)); + int fd = (int)syscall(SYS_openat, AT_FDCWD, real, O_RDONLY | O_DIRECTORY | O_CLOEXEC); + if (fd < 0) return; + char names_buf[4096]; + char *names[LDCONF_MAX_INCLUDES]; + int count = 0; + size_t names_used = 0; + char dents[4096]; + for (;;) { + long n = syscall(SYS_getdents64, fd, dents, sizeof(dents)); + if (n <= 0) break; + for (long at = 0; at < n;) { + unsigned short reclen; + memcpy(&reclen, dents + at + 16, sizeof(reclen)); + const char *name = dents + at + 19; + if (reclen == 0) break; + size_t len = strnlen(name, (size_t)reclen - 19); + if (count < LDCONF_MAX_INCLUDES && names_used + len + 1 <= sizeof(names_buf) && + fnmatch(name_pattern, name, FNM_PERIOD) == 0) { + memcpy(names_buf + names_used, name, len + 1); + names[count++] = names_buf + names_used; + names_used += len + 1; + } + at += reclen; + } + } + syscall(SYS_close, fd); + qsort(names, (size_t)count, sizeof(names[0]), ldconf_name_cmp); + for (int i = 0; i < count; i++) { + char file[PATH_MAX]; + if (dir_len + 1 + strlen(names[i]) >= sizeof(file)) continue; + snprintf(file, sizeof(file), "%s/%s", dir, names[i]); + ldconf_parse_file(file, out, outsz, used, depth + 1, includes); + } +} + +// One ld.so.conf-style file: a directory per line, "include PATTERN..." (relative to the file's own +// directory), "#" comments, and the old "hwcap" lines, which are skipped as glibc does. +static void ldconf_parse_file(const char *logical, char *out, size_t outsz, size_t *used, int depth, int *includes) { + if (depth > LDCONF_MAX_DEPTH || ++*includes > LDCONF_MAX_INCLUDES) return; + char text[8192]; + if (ldconf_read(logical, text, sizeof(text)) < 0) return; + for (char *line = text; line && *line;) { + char *next = strchr(line, '\n'); + if (next) *next++ = '\0'; + char *hash = strchr(line, '#'); + if (hash) *hash = '\0'; + while (*line == ' ' || *line == '\t' || *line == '\r') line++; + size_t len = strlen(line); + while (len > 0 && (line[len - 1] == ' ' || line[len - 1] == '\t' || line[len - 1] == '\r')) line[--len] = '\0'; + if (len == 0) { + line = next; + continue; + } + if (strncmp(line, "include", 7) == 0 && (line[7] == ' ' || line[7] == '\t')) { + for (char *p = line + 8; *p;) { + while (*p == ' ' || *p == '\t') p++; + if (!*p) break; + char *end = p; + while (*end && *end != ' ' && *end != '\t') end++; + char saved = *end; + *end = '\0'; + char pattern[PATH_MAX]; + if (p[0] == '/') { + snprintf(pattern, sizeof(pattern), "%s", p); + } else { + const char *slash = strrchr(logical, '/'); + int base = slash ? (int)(slash - logical) : 0; + snprintf(pattern, sizeof(pattern), "%.*s/%s", base, logical, p); + } + ldconf_include(pattern, out, outsz, used, depth, includes); + *end = saved; + p = end; + } + } else if (strncmp(line, "hwcap", 5) == 0 && (line[5] == ' ' || line[5] == '\t')) { + // ignored by ldconfig since glibc 2.37 + } else if (line[0] == '/') { + // "DIR" or the old "DIR=TYPE" / "DIR TYPE" + size_t dlen = strcspn(line, "= \t"); + while (dlen > 1 && line[dlen - 1] == '/') dlen--; + if (dlen > 0 && dlen < PATH_MAX) { + char dir[PATH_MAX]; + memcpy(dir, line, dlen); + dir[dlen] = '\0'; + char rw_buf[PATH_MAX]; + const char *rw = rewrite_path_in_root(dir, rw_buf, sizeof(rw_buf)); + if (strcmp(rw, dir) != 0) *used = append_lib_dir(out, outsz, *used, rw, strlen(rw)); + } + } + line = next; + } +} + +static void ldconf_parse(char *out, size_t outsz) { + out[0] = '\0'; + size_t used = 0; + int includes = 0; + ldconf_parse_file("/etc/ld.so.conf", out, outsz, &used, 0, &includes); +} + +// The ld.so.conf directories into out (':'-separated, "" when there are none). Uses the copy of an +// earlier call while the files have not changed: the parent finds it when it first forks, and its +// children only compare the three times. Never waits for the lock: a child forked while another +// thread held it reads the files itself. +static void ldconf_dirs(char *out, size_t outsz) { + if (!out || outsz == 0) return; + out[0] = '\0'; + if (g_ddnatv_root[0] == '\0') return; + struct ldconf_sig sig; + ldconf_signature(&sig); + if (sig.t[0] == 0 && sig.t[1] == 0) return; // neither ld.so.conf nor ld.so.conf.d + if (pthread_mutex_trylock(&g_ldconf_lock) != 0) { + ldconf_parse(out, outsz); + return; + } + if (!g_ldconf.valid || memcmp(&g_ldconf.sig, &sig, sizeof(sig)) != 0 || strcmp(g_ldconf.root, g_ddnatv_root) != 0) { + ldconf_parse(g_ldconf.dirs, sizeof(g_ldconf.dirs)); + g_ldconf.sig = sig; + snprintf(g_ldconf.root, sizeof(g_ldconf.root), "%s", g_ddnatv_root); + g_ldconf.valid = 1; + } + snprintf(out, outsz, "%s", g_ldconf.dirs); + pthread_mutex_unlock(&g_ldconf_lock); +} + +// The --library-path ld.so is given for the rootfs program `exe`, in the order ld.so searches on +// Linux: the program's RPATH, the caller's LD_LIBRARY_PATH (the rootfs paths in it mapped), its +// RUNPATH, the directories of /etc/ld.so.conf (ld.so's cache), and the rootfs defaults last. The native userland's +// shells export the defaults in LD_LIBRARY_PATH too, where Linux has none; when there is anything to +// put before them they move to the end, so that a library the program keeps for itself wins over +// one of the same name in /usr/lib. Runs after the hook's setup, so the +// rewrites are those of rewrite_path(). +static void build_ld_library_path(char *const envp[], const char *runpath, int is_rpath, const char *exe, + char *out_buf, size_t out_size) { + if (!out_buf || out_size == 0) return; + out_buf[0] = '\0'; + + const char *caller_ld = NULL; + if (envp) { + for (int i = 0; envp[i] != NULL; i++) { + if (envp[i][0] == 'L' && strncmp(envp[i], "LD_LIBRARY_PATH=", 16) == 0) { + caller_ld = envp[i] + 16; + break; + } + } + } + if (!caller_ld) { + caller_ld = getenv("LD_LIBRARY_PATH"); + } + int has_runpath = runpath && runpath[0]; + char conf[PATH_MAX * 2]; + ldconf_dirs(conf, sizeof(conf)); + // The rootfs defaults that The native userland's shells export in LD_LIBRARY_PATH go to the end, after the + // program's and ld.so.conf's own directories, as on Linux (where LD_LIBRARY_PATH has none of them) + int move_defaults = has_runpath || conf[0] != '\0'; + + size_t used = 0; + if (has_runpath && is_rpath) used = append_runpath_dirs(out_buf, out_size, used, runpath, exe); + + // The caller's directories (those inside the rootfs rewritten), written straight into out_buf: + // this runs in nearly every child a shell forks, where two 8 KB copies of the list on the stack + // were pages more to copy before the exec. + size_t cap = used + PATH_MAX * 2 < out_size ? used + PATH_MAX * 2 : out_size; // the caller's part, as before + for (const char *p = caller_ld; p && *p;) { + const char *end = strchr(p, ':'); + size_t len = end ? (size_t)(end - p) : strlen(p); + if (len > 0 && len < PATH_MAX) { + char tok[PATH_MAX]; + memcpy(tok, p, len); + tok[len] = '\0'; + char rw_buf[PATH_MAX]; + const char *rw = (tok[0] == '/') ? rewrite_path_in_root(tok, rw_buf, sizeof(rw_buf)) : tok; + size_t rw_len = strlen(rw); + size_t need = (used > 0 ? 1 : 0) + rw_len; + if (move_defaults && is_rootfs_default_lib_dir(rw, rw_len)) { + // after the program's own directories, with the other defaults + } else if (used + need < cap) { + if (used > 0) out_buf[used++] = ':'; + memcpy(out_buf + used, rw, rw_len); + used += rw_len; + out_buf[used] = '\0'; + } + } + if (!end) break; + p = end + 1; + } + if (has_runpath && !is_rpath) used = append_runpath_dirs(out_buf, out_size, used, runpath, exe); + // ld.so.conf, as ld.so's cache: after LD_LIBRARY_PATH and RUNPATH, before the default directories + for (const char *p = conf; *p;) { + const char *end = strchr(p, ':'); + size_t len = end ? (size_t)(end - p) : strlen(p); + used = append_lib_dir(out_buf, out_size, used, p, len); + if (!end) break; + p = end + 1; + } + append_rootfs_lib_dirs_missing_from(out_buf, out_size, used, used > 0, out_buf); +} + +// fstat() without the hook's stat emulation: the probe wants the mode bits only, and the +// emulation would map the hard link table and look up the app's ids in a child that is about to +// exec (new pages there, each a copy-on-write fault). Resolved by prepare_exec_in_parent() when the +// process forks, so that its children find it set. +static int (*g_probe_fstat)(int, struct stat *) = NULL; + +static int probe_fstat(int fd, struct stat *st) { + int (*f)(int, struct stat *) = __atomic_load_n(&g_probe_fstat, __ATOMIC_RELAXED); + if (!f) { + f = (int (*)(int, struct stat *))dlsym(RTLD_NEXT, "fstat"); + if (!f) return fstat(fd, st); + __atomic_store_n(&g_probe_fstat, f, __ATOMIC_RELAXED); + } + return f(fd, st); +} + +static void probe_exec_file(const char *path, struct exec_probe *p) { + p->hdr[0] = '\0'; + p->n = -1; + p->is_elf = 0; + p->is_dynamic = 0; + p->android_interp = 0; + p->e_machine = 0; + p->mode = 0; + p->runpath[0] = '\0'; + p->runpath_is_rpath = 0; + if (!path || path[0] == '\0') return; + int fd = open(path, O_RDONLY | O_CLOEXEC); + if (fd < 0) return; + ssize_t n = read(fd, p->hdr, sizeof(p->hdr) - 1); + p->n = n; + p->hdr[n > 0 ? n : 0] = '\0'; + const unsigned char *h = (const unsigned char *)p->hdr; + if (n >= 4 && h[0] == 0x7f && h[1] == 'E' && h[2] == 'L' && h[3] == 'F') { + p->is_elf = 1; + if (n >= 20) p->e_machine = (uint16_t)(h[18] | (h[19] << 8)); + int dynamic = elf_interp_in_buffer(h, n); + if (dynamic < 0) dynamic = check_elf_dynamic(fd, h, n); + p->is_dynamic = dynamic; + if (dynamic && p->e_machine == 183 /* EM_AARCH64 */) p->android_interp = elf_interp_is_android(fd, h, n); + if (dynamic && !p->android_interp) p->runpath_is_rpath = elf_read_runpath(fd, h, n, p->runpath, sizeof(p->runpath)) == 2; + struct stat st; + if (dynamic && probe_fstat(fd, &st) == 0) p->mode = st.st_mode; + } + close(fd); +} + +static int is_ld_linux(const char *path) { + if (!path) return 0; + const char *base = strrchr(path, '/'); + base = base ? base + 1 : path; + return (strncmp(base, "ld-linux", 8) == 0 || strncmp(base, "ld-2.", 5) == 0); +} + +// Splits a "#!" line (NUL-terminated `hdr`, modified in place) into the interpreter and its +// optional argument, the way execve() always has. +static void parse_shebang(char *hdr, char **interp, char **interp_arg) { + char *newline = strchr(hdr, '\n'); + if (newline) *newline = '\0'; + char *p = hdr + 2; + while (*p == ' ' || *p == '\t') p++; + char *arg = strchr(p, ' '); + if (arg) { + *arg = '\0'; + arg++; + while (*arg == ' ' || *arg == '\t') arg++; + } + *interp = p; + *interp_arg = arg; +} + +// Interpreters outside the rootfs (Android's /system/bin/sh) run with Android's environment. +static int is_host_interpreter(const char *interp) { + return strncmp(interp, "/system", 7) == 0 || + (g_ddnatv_root[0] != '\0' && strncmp(interp, g_ddnatv_root, g_ddnatv_root_len) != 0); +} + +// argv for running `script` with its interpreter: interp [arg] script args... (the script's own +// argv[0] is dropped, as the kernel does). free() the result. +static char **build_script_argv(const char *interp, const char *interp_arg, const char *script, char *const argv[]) { + int argc = 0; + while (argv && argv[argc]) argc++; + int has_arg = (interp_arg && *interp_arg) ? 1 : 0; + char **v = (char **)calloc(argc + has_arg + 3, sizeof(char *)); + if (!v) return NULL; + int n = 0; + v[n++] = (char *)interp; + if (has_arg) v[n++] = (char *)interp_arg; + v[n++] = (char *)script; + for (int i = 1; i < argc; i++) v[n++] = argv[i]; + v[n] = NULL; + return v; +} + +// argv for "ld.so [--preload hook] --library-path dirs --argv0 name program args...". +// free() the result; the strings stay owned by the caller. +static char **build_ldso_argv(const char *ld_so, const char *hook, const char *lib_path, + const char *program, const char *prog_name, char *const argv[]) { + int argc = 0; + while (argv && argv[argc]) argc++; + char **v = (char **)calloc(argc + 16, sizeof(char *)); + if (!v) return NULL; + int n = 0; + v[n++] = (char *)ld_so; + if (hook) { + v[n++] = (char *)"--preload"; + v[n++] = (char *)hook; + } + v[n++] = (char *)"--library-path"; + v[n++] = (char *)lib_path; + v[n++] = (char *)"--argv0"; + v[n++] = (char *)((argc > 0 && argv[0]) ? argv[0] : prog_name); + v[n++] = (char *)program; + for (int i = 1; i < argc; i++) v[n++] = argv[i]; + v[n] = NULL; + return v; +} + +// POSIX_SPAWN_RESETIDS makes glibc's spawn child call setresuid() and setresgid() after it has reset +// every signal handler to its default, which includes the SIGSYS handler that emulates the calls +// Android's seccomp filter blocks, so the child died of SIGSYS (GNU make asks for the flag on every +// command it starts). The native userland shows one fixed user id and never runs set-user-id programs: there is +// no id to reset, so the flag is left out and everything else is passed on. +static const posix_spawnattr_t *spawn_attr_for_android(const posix_spawnattr_t *attrp, posix_spawnattr_t *copy) { + short flags = 0; + if (!attrp || posix_spawnattr_getflags(attrp, &flags) != 0 || !(flags & POSIX_SPAWN_RESETIDS)) return attrp; + memcpy(copy, attrp, sizeof(*copy)); + posix_spawnattr_setflags(copy, (short)(flags & ~POSIX_SPAWN_RESETIDS)); + return copy; +} diff --git a/tools/nativeguest/hook/fileops.c b/tools/nativeguest/hook/fileops.c new file mode 100644 index 0000000000..9d0ff7cf9d --- /dev/null +++ b/tools/nativeguest/hook/fileops.c @@ -0,0 +1,805 @@ +// Part of libddnatv-hook.so, included by ddnatv-hook.c (one translation unit, so the +// parts share their static functions and are built with the same command as before). +// +// Other file operations: access, chmod, unlink, mkdir, rename, readlink, extended attributes, +// links, times, truncate, statfs, the working directory, realpath and scandir. + +#ifndef DDNATV_HOST_TEST + +// Hook faccessat +int faccessat(int dirfd, const char *pathname, int mode, int flags) { + init_ddnatv_hook(); + if (!pathname) { + errno = EFAULT; + return -1; + } + DDNATV_AT_REMAP(dirfd, pathname); + + struct stat st; + int stat_flags = (flags & AT_SYMLINK_NOFOLLOW) ? AT_SYMLINK_NOFOLLOW : 0; + if (fstatat(dirfd, pathname, &st, stat_flags) != 0) { + return -1; + } + + if (mode == F_OK) { + return 0; + } + + if (mode & X_OK) { + if (S_ISDIR(st.st_mode)) return 0; + if ((st.st_mode & 0111) != 0) return 0; + if (strstr(pathname, "/bin/") || strstr(pathname, "/sbin/")) return 0; + errno = EACCES; + return -1; + } + + return 0; +} + +int faccessat2(int dirfd, const char *pathname, int mode, int flags) { + return faccessat(dirfd, pathname, mode, flags); +} + +// Hook access +int access(const char *pathname, int mode) { + return faccessat(AT_FDCWD, pathname, mode, 0); +} + +// Hook euidaccess and eaccess +int euidaccess(const char *pathname, int mode) { + return faccessat(AT_FDCWD, pathname, mode, AT_EACCESS); +} + +int eaccess(const char *pathname, int mode) { + return faccessat(AT_FDCWD, pathname, mode, AT_EACCESS); +} + +// Hook chmod +int chmod(const char *pathname, mode_t mode) { + static int (*orig_chmod)(const char *, mode_t) = NULL; + if (!orig_chmod) orig_chmod = (int (*)(const char *, mode_t))dlsym(RTLD_NEXT, "chmod"); + if (!orig_chmod) return -1; + char buf[PATH_MAX]; + const char *target = rewrite_path_follow(pathname, buf, sizeof(buf)); + int ret = orig_chmod(target, mode); + if (ret != 0 && ((mode & 06000) != 0 || errno == EPERM || errno == EACCES || errno == ENOSYS)) { + ret = orig_chmod(target, mode & 01777); + } + if (ret != 0 && (errno == EPERM || errno == EACCES)) { + errno = 0; + return 0; + } + return ret; +} + +// Hook fchmod +int fchmod(int fd, mode_t mode) { + static int (*orig_fchmod)(int, mode_t) = NULL; + if (!orig_fchmod) orig_fchmod = (int (*)(int, mode_t))dlsym(RTLD_NEXT, "fchmod"); + if (!orig_fchmod) return -1; + int ret = orig_fchmod(fd, mode); + if (ret != 0 && ((mode & 06000) != 0 || errno == EPERM || errno == EACCES || errno == ENOSYS)) { + ret = orig_fchmod(fd, mode & 01777); + } + if (ret != 0 && (errno == EPERM || errno == EACCES)) { + errno = 0; + return 0; + } + return ret; +} + +// Hook fchmodat +int fchmodat(int dirfd, const char *pathname, mode_t mode, int flags) { + static int (*orig_fchmodat)(int, const char *, mode_t, int) = NULL; + if (!orig_fchmodat) orig_fchmodat = (int (*)(int, const char *, mode_t, int))dlsym(RTLD_NEXT, "fchmodat"); + if (!orig_fchmodat) return -1; + DDNATV_AT_REMAP(dirfd, pathname); + char buf[PATH_MAX]; + const char *target = (pathname && pathname[0] == '/') ? rewrite_path(pathname, buf, sizeof(buf)) : pathname; + int ret = orig_fchmodat(dirfd, target, mode, flags); + if (ret != 0 && ((mode & 06000) != 0 || errno == EPERM || errno == EACCES || errno == ENOSYS)) { + ret = orig_fchmodat(dirfd, target, mode & 01777, flags); + } + if (ret != 0 && (errno == EPERM || errno == EACCES)) { + errno = 0; + return 0; + } + return ret; +} + +// Hook unlink +int unlink(const char *pathname) { + static int (*orig_unlink)(const char *) = NULL; + if (!orig_unlink) orig_unlink = (int (*)(const char *))dlsym(RTLD_NEXT, "unlink"); + char buf[PATH_MAX]; + const char *target = rewrite_path(pathname, buf, sizeof(buf)); + unlink_mapped_unix_socket(target); + dev_t hl_dev = 0; + ino_t hl_ino = 0; + int hardlinked = lookup_emulated_hardlink_at(AT_FDCWD, target, &hl_dev, &hl_ino); + int ret = orig_unlink(target); + if (ret != 0 && refused_in_repaired_dir(AT_FDCWD, target)) { + // The name could not be looked up while its directory was closed + hardlinked = lookup_emulated_hardlink_at(AT_FDCWD, target, &hl_dev, &hl_ino); + ret = orig_unlink(target); + } + if (ret == 0 && hardlinked) unregister_emulated_hardlink(hl_dev, hl_ino); + return ret; +} + +// Hook unlinkat +int unlinkat(int dirfd, const char *pathname, int flags) { + static int (*orig_unlinkat)(int, const char *, int) = NULL; + if (!orig_unlinkat) orig_unlinkat = (int (*)(int, const char *, int))dlsym(RTLD_NEXT, "unlinkat"); + DDNATV_AT_REMAP(dirfd, pathname); + char buf[PATH_MAX]; + const char *target = (pathname && pathname[0] == '/') ? rewrite_path(pathname, buf, sizeof(buf)) : pathname; + dev_t hl_dev = 0; + ino_t hl_ino = 0; + int hardlinked = 0; + if (target) { + unlink_mapped_unix_socket(target); + hardlinked = lookup_emulated_hardlink_at(dirfd, target, &hl_dev, &hl_ino); + } + int ret = orig_unlinkat ? orig_unlinkat(dirfd, target, flags) : -1; + if (ret != 0 && orig_unlinkat && refused_in_repaired_dir(dirfd, target)) { + hardlinked = target ? lookup_emulated_hardlink_at(dirfd, target, &hl_dev, &hl_ino) : 0; + ret = orig_unlinkat(dirfd, target, flags); + } + if (ret == 0 && hardlinked) unregister_emulated_hardlink(hl_dev, hl_ino); + return ret; +} + +// Hook rmdir +int rmdir(const char *pathname) { + static int (*orig_rmdir)(const char *) = NULL; + if (!orig_rmdir) orig_rmdir = (int (*)(const char *))dlsym(RTLD_NEXT, "rmdir"); + char buf[PATH_MAX]; + const char *target = rewrite_path(pathname, buf, sizeof(buf)); + int ret = orig_rmdir(target); + if (ret != 0 && refused_in_repaired_dir(AT_FDCWD, target)) ret = orig_rmdir(target); + return ret; +} + +// glibc's remove() calls its internal unlinkat, which no preload can see: GLib's g_file_delete() +// got ENOENT for every guest path, and Flatpak could not replace an app's "current" link +int remove(const char *pathname) { + if (unlink(pathname) == 0) return 0; + if (errno != EISDIR) return -1; + return rmdir(pathname); +} + +// glob() and nftw() walk directories with glibc's internal calls too. glob() is given the hook's +// own through GLOB_ALTDIRFUNC; nftw() walks the mapped directory and its callback is given guest paths. +static int glob_alt_stat(const char *path, void *st) { return stat(path, (struct stat *)st); } +static int glob_alt_lstat(const char *path, void *st) { return lstat(path, (struct stat *)st); } +static void *glob_alt_opendir(const char *path) { return opendir(path); } +static void *glob_alt_readdir(void *dir) { return readdir((DIR *)dir); } +static void glob_alt_closedir(void *dir) { closedir((DIR *)dir); } + +int glob(const char *pattern, int flags, int (*errfunc)(const char *, int), glob_t *pglob) { + static int (*orig_glob)(const char *, int, int (*)(const char *, int), glob_t *) = NULL; + if (!orig_glob) orig_glob = (int (*)(const char *, int, int (*)(const char *, int), glob_t *))dlsym(RTLD_NEXT, "glob"); + if (pglob && !(flags & GLOB_ALTDIRFUNC)) { + init_ddnatv_hook(); + pglob->gl_closedir = glob_alt_closedir; + pglob->gl_readdir = (struct dirent *(*)(void *))glob_alt_readdir; + pglob->gl_opendir = glob_alt_opendir; + pglob->gl_lstat = (int (*)(const char *, struct stat *))glob_alt_lstat; + pglob->gl_stat = (int (*)(const char *, struct stat *))glob_alt_stat; + flags |= GLOB_ALTDIRFUNC; + } + return orig_glob ? orig_glob(pattern, flags, errfunc, pglob) : GLOB_NOSPACE; +} + +int glob64(const char *pattern, int flags, int (*errfunc)(const char *, int), glob64_t *pglob) { + return glob(pattern, flags, errfunc, (glob_t *)pglob); +} + +static __thread struct { + const char *host, *guest; + size_t host_len, guest_len; + int (*nftw_fn)(const char *, const struct stat *, int, struct FTW *); + int (*ftw_fn)(const char *, const struct stat *, int); +} t_walk; + +static const char *walk_guest_path(const char *path, char *buf, size_t size, int *base) { + if (!t_walk.host || strncmp(path, t_walk.host, t_walk.host_len) != 0) return path; + const char *rest = path + t_walk.host_len; + if (*rest != '\0' && *rest != '/') return path; + // a walk of "/" gives "/etc", not "//etc" + size_t guest_len = (t_walk.guest_len == 1 && *rest == '/') ? 0 : t_walk.guest_len; + size_t n = ddnatv_append(buf, size, 0, t_walk.guest, guest_len); + ddnatv_append(buf, size, n, rest, strlen(rest)); + if (base) *base = *base - (int)t_walk.host_len + (int)guest_len; + return buf; +} + +static int walk_nftw_cb(const char *path, const struct stat *st, int type, struct FTW *ftwbuf) { + char buf[PATH_MAX]; + struct FTW guest_ftw = *ftwbuf; + return t_walk.nftw_fn(walk_guest_path(path, buf, sizeof(buf), &guest_ftw.base), st, type, &guest_ftw); +} + +static int walk_ftw_cb(const char *path, const struct stat *st, int type) { + char buf[PATH_MAX]; + return t_walk.ftw_fn(walk_guest_path(path, buf, sizeof(buf), NULL), st, type); +} + +int nftw(const char *dir, int (*fn)(const char *, const struct stat *, int, struct FTW *), int fds, int flags) { + static int (*orig_nftw)(const char *, int (*)(const char *, const struct stat *, int, struct FTW *), int, int) = NULL; + if (!orig_nftw) orig_nftw = (int (*)(const char *, int (*)(const char *, const struct stat *, int, struct FTW *), int, int))dlsym(RTLD_NEXT, "nftw"); + if (!orig_nftw) { errno = ENOSYS; return -1; } + char buf[PATH_MAX]; + const char *target = rewrite_path(dir, buf, sizeof(buf)); + if (target == dir || !fn) return orig_nftw(dir, fn, fds, flags); + __typeof__(t_walk) saved = t_walk; + t_walk.host = target; t_walk.host_len = strlen(target); + while (t_walk.host_len > 1 && target[t_walk.host_len - 1] == '/') t_walk.host_len--; + t_walk.guest = dir; t_walk.guest_len = strlen(dir); + while (t_walk.guest_len > 1 && dir[t_walk.guest_len - 1] == '/') t_walk.guest_len--; + t_walk.nftw_fn = fn; + int ret = orig_nftw(target, walk_nftw_cb, fds, flags); + t_walk = saved; + return ret; +} + +int nftw64(const char *dir, int (*fn)(const char *, const struct stat64 *, int, struct FTW *), int fds, int flags) { + return nftw(dir, (int (*)(const char *, const struct stat *, int, struct FTW *))fn, fds, flags); +} + +int ftw(const char *dir, int (*fn)(const char *, const struct stat *, int), int fds) { + static int (*orig_ftw)(const char *, int (*)(const char *, const struct stat *, int), int) = NULL; + if (!orig_ftw) orig_ftw = (int (*)(const char *, int (*)(const char *, const struct stat *, int), int))dlsym(RTLD_NEXT, "ftw"); + if (!orig_ftw) { errno = ENOSYS; return -1; } + char buf[PATH_MAX]; + const char *target = rewrite_path(dir, buf, sizeof(buf)); + if (target == dir || !fn) return orig_ftw(dir, fn, fds); + __typeof__(t_walk) saved = t_walk; + t_walk.host = target; t_walk.host_len = strlen(target); + while (t_walk.host_len > 1 && target[t_walk.host_len - 1] == '/') t_walk.host_len--; + t_walk.guest = dir; t_walk.guest_len = strlen(dir); + while (t_walk.guest_len > 1 && dir[t_walk.guest_len - 1] == '/') t_walk.guest_len--; + t_walk.ftw_fn = fn; + int ret = orig_ftw(target, walk_ftw_cb, fds); + t_walk = saved; + return ret; +} + +int ftw64(const char *dir, int (*fn)(const char *, const struct stat64 *, int), int fds) { + return ftw(dir, (int (*)(const char *, const struct stat *, int))fn, fds); +} + +// The app is root for the programs it runs, and root enters and writes every directory whatever its +// mode; the app does not. dpkg makes each new directory with mode 0 and sets the mode a moment later, +// and a chmod that fails is reported as done (see chmod), which left directories nobody could put a +// file in: "Permission denied" in /usr/share/applications and others (#44). So a directory always +// keeps its owner's read, write and search rights. +#define NEW_DIR_OWNER_RIGHTS S_IRWXU + +// Hook mkdir +int mkdir(const char *pathname, mode_t mode) { + static int (*orig_mkdir)(const char *, mode_t) = NULL; + if (!orig_mkdir) orig_mkdir = (int (*)(const char *, mode_t))dlsym(RTLD_NEXT, "mkdir"); + char buf[PATH_MAX]; + const char *target = rewrite_path(pathname, buf, sizeof(buf)); + int ret = orig_mkdir(target, mode | NEW_DIR_OWNER_RIGHTS); + if (ret != 0 && refused_in_repaired_dir(AT_FDCWD, target)) ret = orig_mkdir(target, mode | NEW_DIR_OWNER_RIGHTS); + return ret; +} + +// Hook mkdirat +int mkdirat(int dirfd, const char *pathname, mode_t mode) { + static int (*orig_mkdirat)(int, const char *, mode_t) = NULL; + if (!orig_mkdirat) orig_mkdirat = (int (*)(int, const char *, mode_t))dlsym(RTLD_NEXT, "mkdirat"); + DDNATV_AT_REMAP(dirfd, pathname); + char buf[PATH_MAX]; + const char *target = (pathname && pathname[0] == '/') ? rewrite_path(pathname, buf, sizeof(buf)) : pathname; + int ret = orig_mkdirat(dirfd, target, mode | NEW_DIR_OWNER_RIGHTS); + if (ret != 0 && refused_in_repaired_dir(dirfd, target)) ret = orig_mkdirat(dirfd, target, mode | NEW_DIR_OWNER_RIGHTS); + return ret; +} + +static void ensure_dir_exists(const char *path) { + if (!path || path[0] == '\0') return; + char tmp[PATH_MAX]; + strncpy(tmp, path, sizeof(tmp) - 1); + tmp[sizeof(tmp) - 1] = '\0'; + for (char *p = tmp + 1; *p; p++) { + if (*p == '/') { + *p = '\0'; + mkdir(tmp, 0755); + *p = '/'; + } + } + mkdir(tmp, 0755); +} + +// Hook inotify_add_watch +int inotify_add_watch(int fd, const char *pathname, uint32_t mask) { + static int (*orig_inotify_add_watch)(int, const char *, uint32_t) = NULL; + if (!orig_inotify_add_watch) orig_inotify_add_watch = (int (*)(int, const char *, uint32_t))dlsym(RTLD_NEXT, "inotify_add_watch"); + if (!pathname) return -1; + char buf[PATH_MAX]; + const char *target = rewrite_path(pathname, buf, sizeof(buf)); + if (!orig_inotify_add_watch) return -1; + int wd = orig_inotify_add_watch(fd, target, mask); + if (wd < 0 && errno == ENOENT && (mask & IN_ONLYDIR)) { + ensure_dir_exists(target); + wd = orig_inotify_add_watch(fd, target, mask); + } + return wd; +} + +// Hook rename +int rename(const char *oldpath, const char *newpath) { + static int (*orig_rename)(const char *, const char *) = NULL; + if (!orig_rename) orig_rename = (int (*)(const char *, const char *))dlsym(RTLD_NEXT, "rename"); + char buf1[PATH_MAX]; + char buf2[PATH_MAX]; + const char *target_old = rewrite_path(oldpath, buf1, sizeof(buf1)); + const char *target_new = rewrite_path(newpath, buf2, sizeof(buf2)); + dev_t hl_dev = 0; + ino_t hl_ino = 0; + int hardlinked = lookup_emulated_hardlink_at(AT_FDCWD, target_new, &hl_dev, &hl_ino); + int ret = orig_rename(target_old, target_new); + if (ret != 0 && errno == EACCES) { + // Both directories: the old name is taken out of one, the new put in the other + int refused = errno; + int fixed_old = refused_in_repaired_dir(AT_FDCWD, target_old); + errno = refused; + int fixed_new = refused_in_repaired_dir(AT_FDCWD, target_new); + if (fixed_old || fixed_new) { + hardlinked = lookup_emulated_hardlink_at(AT_FDCWD, target_new, &hl_dev, &hl_ino); + ret = orig_rename(target_old, target_new); + } + } + if (ret == 0 && hardlinked) unregister_emulated_hardlink(hl_dev, hl_ino); + return ret; +} + +// Hook renameat +int renameat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath) { + static int (*orig_renameat)(int, const char *, int, const char *) = NULL; + if (!orig_renameat) orig_renameat = (int (*)(int, const char *, int, const char *))dlsym(RTLD_NEXT, "renameat"); + DDNATV_AT_REMAP_NAMED(olddirfd, oldpath, ddnatv_at_old); + DDNATV_AT_REMAP_NAMED(newdirfd, newpath, ddnatv_at_new); + char buf1[PATH_MAX]; + char buf2[PATH_MAX]; + const char *target_old = (oldpath && oldpath[0] == '/') ? rewrite_path(oldpath, buf1, sizeof(buf1)) : oldpath; + const char *target_new = (newpath && newpath[0] == '/') ? rewrite_path(newpath, buf2, sizeof(buf2)) : newpath; + dev_t hl_dev = 0; + ino_t hl_ino = 0; + int hardlinked = lookup_emulated_hardlink_at(newdirfd, target_new, &hl_dev, &hl_ino); + int ret = orig_renameat(olddirfd, target_old, newdirfd, target_new); + if (ret != 0 && errno == EACCES) { + int refused = errno; + int fixed_old = refused_in_repaired_dir(olddirfd, target_old); + errno = refused; + int fixed_new = refused_in_repaired_dir(newdirfd, target_new); + if (fixed_old || fixed_new) { + hardlinked = lookup_emulated_hardlink_at(newdirfd, target_new, &hl_dev, &hl_ino); + ret = orig_renameat(olddirfd, target_old, newdirfd, target_new); + } + } + if (ret == 0 && hardlinked) unregister_emulated_hardlink(hl_dev, hl_ino); + return ret; +} + +// Hook renameat2 +int renameat2(int olddirfd, const char *oldpath, int newdirfd, const char *newpath, unsigned int flags) { + static int (*orig_renameat2)(int, const char *, int, const char *, unsigned int) = NULL; + if (!orig_renameat2) { + orig_renameat2 = (int (*)(int, const char *, int, const char *, unsigned int))dlsym(RTLD_NEXT, "renameat2"); + if (!orig_renameat2) return renameat(olddirfd, oldpath, newdirfd, newpath); + } + DDNATV_AT_REMAP_NAMED(olddirfd, oldpath, ddnatv_at_old); + DDNATV_AT_REMAP_NAMED(newdirfd, newpath, ddnatv_at_new); + char buf1[PATH_MAX]; + char buf2[PATH_MAX]; + const char *target_old = (oldpath && oldpath[0] == '/') ? rewrite_path(oldpath, buf1, sizeof(buf1)) : oldpath; + const char *target_new = (newpath && newpath[0] == '/') ? rewrite_path(newpath, buf2, sizeof(buf2)) : newpath; + dev_t hl_dev = 0; + ino_t hl_ino = 0; + // RENAME_EXCHANGE keeps both files, so nothing is replaced. + int hardlinked = !(flags & RENAME_EXCHANGE) && lookup_emulated_hardlink_at(newdirfd, target_new, &hl_dev, &hl_ino); + int ret = orig_renameat2(olddirfd, target_old, newdirfd, target_new, flags); + if (ret != 0 && errno == EACCES) { + int refused = errno; + int fixed_old = refused_in_repaired_dir(olddirfd, target_old); + errno = refused; + int fixed_new = refused_in_repaired_dir(newdirfd, target_new); + if (fixed_old || fixed_new) { + hardlinked = !(flags & RENAME_EXCHANGE) && lookup_emulated_hardlink_at(newdirfd, target_new, &hl_dev, &hl_ino); + ret = orig_renameat2(olddirfd, target_old, newdirfd, target_new, flags); + } + } + if (ret == 0 && hardlinked) unregister_emulated_hardlink(hl_dev, hl_ino); + return ret; +} + +// Hook readlink +ssize_t readlink(const char *pathname, char *buf, size_t bufsiz) { + if (is_proc_self_exe(pathname)) { + ssize_t exe_len = handle_proc_self_exe(buf, bufsiz); + if (exe_len >= 0) return exe_len; + } + static ssize_t (*orig_readlink)(const char *, char *, size_t) = NULL; + if (!orig_readlink) orig_readlink = (ssize_t (*)(const char *, char *, size_t))dlsym(RTLD_NEXT, "readlink"); + char pbuf[PATH_MAX]; + const char *target = rewrite_path(pathname, pbuf, sizeof(pbuf)); + ssize_t ret = orig_readlink ? orig_readlink(target, buf, bufsiz) : -1; + if (ret < 0 && errno == ENOENT) { + char alt[PATH_MAX]; + const char *retry = usrmerge_retry_path(target, alt, sizeof(alt)); + if (retry) ret = orig_readlink(retry, buf, bufsiz); + } + return postprocess_readlink(pathname, buf, ret, bufsiz); +} + +// Hook readlinkat +ssize_t readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz) { + static ssize_t (*orig_readlinkat)(int, const char *, char *, size_t) = NULL; + if (!orig_readlinkat) orig_readlinkat = (ssize_t (*)(int, const char *, char *, size_t))dlsym(RTLD_NEXT, "readlinkat"); + DDNATV_AT_REMAP(dirfd, pathname); + // after the remap: "proc/self/exe" relative to a descriptor for "/" is /proc/self/exe as well + if (is_proc_self_exe(pathname)) { + ssize_t exe_len = handle_proc_self_exe(buf, bufsiz); + if (exe_len >= 0) return exe_len; + } + char pbuf[PATH_MAX]; + const char *target = (pathname && pathname[0] == '/') ? rewrite_path(pathname, pbuf, sizeof(pbuf)) : pathname; + ssize_t ret = orig_readlinkat ? orig_readlinkat(dirfd, target, buf, bufsiz) : -1; + if (ret < 0 && errno == ENOENT && pathname && pathname[0] == '/') { + char alt[PATH_MAX]; + const char *retry = usrmerge_retry_path(target, alt, sizeof(alt)); + if (retry) ret = orig_readlinkat(dirfd, retry, buf, bufsiz); + } + return postprocess_readlink(pathname, buf, ret, bufsiz); +} + +// Fortified readlink variants used by dpkg / update-alternatives +ssize_t __readlink_chk(const char *pathname, char *buf, size_t bufsiz, size_t buflen) { + return readlink(pathname, buf, bufsiz); +} + +ssize_t __readlinkat_chk(int dirfd, const char *pathname, char *buf, size_t bufsiz, size_t buflen) { + return readlinkat(dirfd, pathname, buf, bufsiz); +} + +// Ubuntu's programs find Android's SELinux (enforcing) through libselinux and then ask its policy +// questions it has no answers for. The one that mattered: PAM's pam_rootok, which lets root +// change its own entries, asks "may this user be root" and was refused, so chfn, chsh and +// `adduser --gecos` (the installation of PostgreSQL) failed with "PAM: Authentication failure". +// Ubuntu does not use SELinux, so for its programs it is off, as its labels are hidden below. +int is_selinux_enabled(void) { return 0; } + +// Extended attributes take the same paths as stat(). Without these hooks coreutils read +// security.selinux with the Ubuntu path itself: `ls -l` printed an error line for every file +// Android lacks (/etc/os-release) and showed Android's own label for the ones it has +// (/etc/passwd). Ubuntu does not use SELinux, so its label is hidden altogether: `ls -l` looks as +// on Ubuntu and cp/tar/rsync do not try to copy Android's labels. +ssize_t getxattr(const char *path, const char *name, void *value, size_t size) { + static ssize_t (*orig_getxattr)(const char *, const char *, void *, size_t) = NULL; + if (!orig_getxattr) orig_getxattr = (ssize_t (*)(const char *, const char *, void *, size_t))dlsym(RTLD_NEXT, "getxattr"); + if (is_selinux_xattr(name)) { + errno = ENODATA; + return -1; + } + char pbuf[PATH_MAX]; + return orig_getxattr ? orig_getxattr(rewrite_path_follow(path, pbuf, sizeof(pbuf)), name, value, size) : -1; +} + +ssize_t lgetxattr(const char *path, const char *name, void *value, size_t size) { + static ssize_t (*orig_lgetxattr)(const char *, const char *, void *, size_t) = NULL; + if (!orig_lgetxattr) orig_lgetxattr = (ssize_t (*)(const char *, const char *, void *, size_t))dlsym(RTLD_NEXT, "lgetxattr"); + if (is_selinux_xattr(name)) { + errno = ENODATA; + return -1; + } + char pbuf[PATH_MAX]; + return orig_lgetxattr ? orig_lgetxattr(rewrite_path(path, pbuf, sizeof(pbuf)), name, value, size) : -1; +} + +ssize_t listxattr(const char *path, char *list, size_t size) { + static ssize_t (*orig_listxattr)(const char *, char *, size_t) = NULL; + if (!orig_listxattr) orig_listxattr = (ssize_t (*)(const char *, char *, size_t))dlsym(RTLD_NEXT, "listxattr"); + char pbuf[PATH_MAX]; + ssize_t ret = orig_listxattr ? orig_listxattr(rewrite_path_follow(path, pbuf, sizeof(pbuf)), list, size) : -1; + return size > 0 ? drop_selinux_xattr(list, ret) : ret; +} + +ssize_t llistxattr(const char *path, char *list, size_t size) { + static ssize_t (*orig_llistxattr)(const char *, char *, size_t) = NULL; + if (!orig_llistxattr) orig_llistxattr = (ssize_t (*)(const char *, char *, size_t))dlsym(RTLD_NEXT, "llistxattr"); + char pbuf[PATH_MAX]; + ssize_t ret = orig_llistxattr ? orig_llistxattr(rewrite_path(path, pbuf, sizeof(pbuf)), list, size) : -1; + return size > 0 ? drop_selinux_xattr(list, ret) : ret; +} + +ssize_t fgetxattr(int fd, const char *name, void *value, size_t size) { + static ssize_t (*orig_fgetxattr)(int, const char *, void *, size_t) = NULL; + if (!orig_fgetxattr) orig_fgetxattr = (ssize_t (*)(int, const char *, void *, size_t))dlsym(RTLD_NEXT, "fgetxattr"); + if (is_selinux_xattr(name)) { + errno = ENODATA; + return -1; + } + return orig_fgetxattr ? orig_fgetxattr(fd, name, value, size) : -1; +} + +ssize_t flistxattr(int fd, char *list, size_t size) { + static ssize_t (*orig_flistxattr)(int, char *, size_t) = NULL; + if (!orig_flistxattr) orig_flistxattr = (ssize_t (*)(int, char *, size_t))dlsym(RTLD_NEXT, "flistxattr"); + ssize_t ret = orig_flistxattr ? orig_flistxattr(fd, list, size) : -1; + return size > 0 ? drop_selinux_xattr(list, ret) : ret; +} + +int setxattr(const char *path, const char *name, const void *value, size_t size, int flags) { + static int (*orig_setxattr)(const char *, const char *, const void *, size_t, int) = NULL; + if (!orig_setxattr) orig_setxattr = (int (*)(const char *, const char *, const void *, size_t, int))dlsym(RTLD_NEXT, "setxattr"); + char pbuf[PATH_MAX]; + return orig_setxattr ? orig_setxattr(rewrite_path_follow(path, pbuf, sizeof(pbuf)), name, value, size, flags) : -1; +} + +int lsetxattr(const char *path, const char *name, const void *value, size_t size, int flags) { + static int (*orig_lsetxattr)(const char *, const char *, const void *, size_t, int) = NULL; + if (!orig_lsetxattr) orig_lsetxattr = (int (*)(const char *, const char *, const void *, size_t, int))dlsym(RTLD_NEXT, "lsetxattr"); + char pbuf[PATH_MAX]; + return orig_lsetxattr ? orig_lsetxattr(rewrite_path(path, pbuf, sizeof(pbuf)), name, value, size, flags) : -1; +} + +int removexattr(const char *path, const char *name) { + static int (*orig_removexattr)(const char *, const char *) = NULL; + if (!orig_removexattr) orig_removexattr = (int (*)(const char *, const char *))dlsym(RTLD_NEXT, "removexattr"); + char pbuf[PATH_MAX]; + return orig_removexattr ? orig_removexattr(rewrite_path(path, pbuf, sizeof(pbuf)), name) : -1; +} + +int lremovexattr(const char *path, const char *name) { + static int (*orig_lremovexattr)(const char *, const char *) = NULL; + if (!orig_lremovexattr) orig_lremovexattr = (int (*)(const char *, const char *))dlsym(RTLD_NEXT, "lremovexattr"); + char pbuf[PATH_MAX]; + return orig_lremovexattr ? orig_lremovexattr(rewrite_path(path, pbuf, sizeof(pbuf)), name) : -1; +} + +// Hook symlink +int symlink(const char *target, const char *linkpath) { + static int (*orig_symlink)(const char *, const char *) = NULL; + if (!orig_symlink) orig_symlink = (int (*)(const char *, const char *))dlsym(RTLD_NEXT, "symlink"); + char pbuf[PATH_MAX], tbuf[PATH_MAX]; + const char *newlink = rewrite_path(linkpath, pbuf, sizeof(pbuf)); + const char *newtarget = (target && target[0] == '/') ? rewrite_path(target, tbuf, sizeof(tbuf)) : target; + int ret = orig_symlink ? orig_symlink(newtarget, newlink) : -1; + if (ret != 0 && orig_symlink && refused_in_repaired_dir(AT_FDCWD, newlink)) ret = orig_symlink(newtarget, newlink); + return ret; +} + +// Hook symlinkat +int symlinkat(const char *target, int newdirfd, const char *linkpath) { + static int (*orig_symlinkat)(const char *, int, const char *) = NULL; + if (!orig_symlinkat) orig_symlinkat = (int (*)(const char *, int, const char *))dlsym(RTLD_NEXT, "symlinkat"); + DDNATV_AT_REMAP(newdirfd, linkpath); + char pbuf[PATH_MAX], tbuf[PATH_MAX]; + const char *newlink = (linkpath && linkpath[0] == '/') ? rewrite_path(linkpath, pbuf, sizeof(pbuf)) : linkpath; + const char *newtarget = (target && target[0] == '/') ? rewrite_path(target, tbuf, sizeof(tbuf)) : target; + int ret = orig_symlinkat ? orig_symlinkat(newtarget, newdirfd, newlink) : -1; + if (ret != 0 && orig_symlinkat && refused_in_repaired_dir(newdirfd, newlink)) ret = orig_symlinkat(newtarget, newdirfd, newlink); + return ret; +} + +// Hook linkat +int linkat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath, int flags) { + static int (*orig_linkat)(int, const char *, int, const char *, int) = NULL; + if (!orig_linkat) orig_linkat = (int (*)(int, const char *, int, const char *, int))dlsym(RTLD_NEXT, "linkat"); + DDNATV_AT_REMAP_NAMED(olddirfd, oldpath, ddnatv_at_old); + DDNATV_AT_REMAP_NAMED(newdirfd, newpath, ddnatv_at_new); + char obuf[PATH_MAX], nbuf[PATH_MAX]; + const char *rold = (oldpath && oldpath[0] == '/') ? rewrite_path(oldpath, obuf, sizeof(obuf)) : oldpath; + const char *rnew = (newpath && newpath[0] == '/') ? rewrite_path(newpath, nbuf, sizeof(nbuf)) : newpath; + int ret = orig_linkat ? orig_linkat(olddirfd, rold, newdirfd, rnew, flags) : -1; + if (ret != 0 && orig_linkat && refused_in_repaired_dir(newdirfd, rnew)) ret = orig_linkat(olddirfd, rold, newdirfd, rnew, flags); + if (ret != 0 && (errno == EXDEV || errno == EPERM || errno == EACCES || errno == ENOTSUP || errno == ENOSYS)) { + ret = ddnatv_emulate_hardlinkat(olddirfd, rold, newdirfd, rnew); + } + return ret; +} + +// Hook link +int link(const char *oldpath, const char *newpath) { + return linkat(AT_FDCWD, oldpath, AT_FDCWD, newpath, 0); +} + +// Hook utime / utimes / lutimes / futimesat / utimensat +int utime(const char *filename, const struct utimbuf *times) { + static int (*orig_utime)(const char *, const struct utimbuf *) = NULL; + if (!orig_utime) orig_utime = (int (*)(const char *, const struct utimbuf *))dlsym(RTLD_NEXT, "utime"); + char buf[PATH_MAX]; + const char *target = rewrite_path_follow(filename, buf, sizeof(buf)); + return orig_utime ? orig_utime(target, times) : -1; +} + +int utimes(const char *filename, const struct timeval times[2]) { + static int (*orig_utimes)(const char *, const struct timeval[2]) = NULL; + if (!orig_utimes) orig_utimes = (int (*)(const char *, const struct timeval[2]))dlsym(RTLD_NEXT, "utimes"); + char buf[PATH_MAX]; + const char *target = rewrite_path_follow(filename, buf, sizeof(buf)); + return orig_utimes ? orig_utimes(target, times) : -1; +} + +int lutimes(const char *filename, const struct timeval times[2]) { + static int (*orig_lutimes)(const char *, const struct timeval[2]) = NULL; + if (!orig_lutimes) orig_lutimes = (int (*)(const char *, const struct timeval[2]))dlsym(RTLD_NEXT, "lutimes"); + char buf[PATH_MAX]; + const char *target = rewrite_path(filename, buf, sizeof(buf)); + return orig_lutimes ? orig_lutimes(target, times) : -1; +} + +int futimesat(int dirfd, const char *pathname, const struct timeval times[2]) { + static int (*orig_futimesat)(int, const char *, const struct timeval[2]) = NULL; + if (!orig_futimesat) orig_futimesat = (int (*)(int, const char *, const struct timeval[2]))dlsym(RTLD_NEXT, "futimesat"); + char buf[PATH_MAX]; + const char *target = (pathname && pathname[0] == '/') ? rewrite_path(pathname, buf, sizeof(buf)) : pathname; + return orig_futimesat ? orig_futimesat(dirfd, target, times) : -1; +} + +int utimensat(int dirfd, const char *pathname, const struct timespec times[2], int flags) { + static int (*orig_utimensat)(int, const char *, const struct timespec[2], int) = NULL; + if (!orig_utimensat) orig_utimensat = (int (*)(int, const char *, const struct timespec[2], int))dlsym(RTLD_NEXT, "utimensat"); + DDNATV_AT_REMAP(dirfd, pathname); + char buf[PATH_MAX]; + const char *target = (pathname && pathname[0] == '/') ? rewrite_path(pathname, buf, sizeof(buf)) : pathname; + return orig_utimensat ? orig_utimensat(dirfd, target, times, flags) : -1; +} + +// Hook truncate +int truncate(const char *path, off_t length) { + static int (*orig_truncate)(const char *, off_t) = NULL; + if (!orig_truncate) orig_truncate = (int (*)(const char *, off_t))dlsym(RTLD_NEXT, "truncate"); + char buf[PATH_MAX]; + const char *target = rewrite_path_follow(path, buf, sizeof(buf)); + return orig_truncate(target, length); +} + +// Hook statfs / statfs64 / statvfs / statvfs64 +int statfs(const char *path, struct statfs *buf) { + static int (*orig_statfs)(const char *, struct statfs *) = NULL; + if (!orig_statfs) orig_statfs = (int (*)(const char *, struct statfs *))dlsym(RTLD_NEXT, "statfs"); + char pbuf[PATH_MAX]; + const char *target = rewrite_path_follow(path, pbuf, sizeof(pbuf)); + return orig_statfs ? orig_statfs(target, buf) : -1; +} + +int statvfs(const char *path, struct statvfs *buf) { + static int (*orig_statvfs)(const char *, struct statvfs *) = NULL; + if (!orig_statvfs) orig_statvfs = (int (*)(const char *, struct statvfs *))dlsym(RTLD_NEXT, "statvfs"); + char pbuf[PATH_MAX]; + const char *target = rewrite_path_follow(path, pbuf, sizeof(pbuf)); + return orig_statvfs ? orig_statvfs(target, buf) : -1; +} + +// Hook chdir +int chdir(const char *path) { + static int (*orig_chdir)(const char *) = NULL; + if (!orig_chdir) orig_chdir = (int (*)(const char *))dlsym(RTLD_NEXT, "chdir"); + char buf[PATH_MAX]; + const char *target = rewrite_path_follow(path, buf, sizeof(buf)); + return orig_chdir ? orig_chdir(target) : -1; +} + +// Hook fchdir +int fchdir(int fd) { + static int (*orig_fchdir)(int) = NULL; + if (!orig_fchdir) orig_fchdir = (int (*)(int))dlsym(RTLD_NEXT, "fchdir"); + return orig_fchdir ? orig_fchdir(fd) : -1; +} + +// Hook getcwd +char *getcwd(char *buf, size_t size) { + static char *(*orig_getcwd)(char *, size_t) = NULL; + if (!orig_getcwd) orig_getcwd = (char *(*)(char *, size_t))dlsym(RTLD_NEXT, "getcwd"); + char *res = orig_getcwd ? orig_getcwd(buf, size) : NULL; + if (res) { + size_t len = strlen(res); + guest_path_of_mount_source(res, len, buf ? size : len + 1); + } + return res; +} + +extern void __chk_fail(void) __attribute__((__noreturn__)); +char *__getcwd_chk(char *buf, size_t size, size_t buflen) { + if (size > buflen) __chk_fail(); + return getcwd(buf, size); +} + +// Hook realpath and canonicalize_file_name +char *realpath(const char *path, char *resolved_path) { + static char *(*orig_realpath)(const char *, char *) = NULL; + if (!orig_realpath) orig_realpath = (char *(*)(const char *, char *))dlsym(RTLD_NEXT, "realpath"); + if (!path) { + errno = EINVAL; + return NULL; + } + init_ddnatv_hook(); + if (is_proc_self_exe(path)) { + char tmp[PATH_MAX]; + ssize_t n = handle_proc_self_exe(tmp, sizeof(tmp) - 1); + if (n > 0) { + tmp[n] = '\0'; + if (resolved_path) { + strncpy(resolved_path, tmp, PATH_MAX - 1); + resolved_path[PATH_MAX - 1] = '\0'; + return resolved_path; + } else { + return strdup(tmp); + } + } + } + char pbuf[PATH_MAX]; + const char *target = rewrite_path(path, pbuf, sizeof(pbuf)); + char *res = orig_realpath ? orig_realpath(target, resolved_path) : NULL; + if (res && g_ddnatv_root[0] != '\0') { + size_t rlen = g_ddnatv_root_len; + if (strncmp(res, g_ddnatv_root, rlen) == 0 && (res[rlen] == '/' || res[rlen] == '\0')) { + size_t rem_len = strlen(res + rlen); + if (rem_len == 0) { + res[0] = '/'; + res[1] = '\0'; + } else { + memmove(res, res + rlen, rem_len + 1); + } + } + } + if (res) guest_path_of_mount_source(res, strlen(res), resolved_path ? PATH_MAX : strlen(res) + 1); + return res; +} + +char *__realpath_chk(const char *path, char *resolved_path, size_t resolved_len) { + return realpath(path, resolved_path); +} + +char *canonicalize_file_name(const char *path) { + return realpath(path, NULL); +} + +// Hook scandir and scandir64 +int scandir(const char *dirp, struct dirent ***namelist, + int (*filter)(const struct dirent *), + int (*compar)(const struct dirent **, const struct dirent **)) { + static int (*orig_scandir)(const char *, struct dirent ***, + int (*)(const struct dirent *), + int (*)(const struct dirent **, const struct dirent **)) = NULL; + if (!orig_scandir) orig_scandir = (int (*)(const char *, struct dirent ***, + int (*)(const struct dirent *), + int (*)(const struct dirent **, const struct dirent **)))dlsym(RTLD_NEXT, "scandir"); + char buf[PATH_MAX]; + const char *target = rewrite_path(dirp, buf, sizeof(buf)); + return orig_scandir ? orig_scandir(target, namelist, filter, compar) : -1; +} + +#if defined(__LP64__) +// Export 64-bit Large File Support (LFS) symbol aliases on 64-bit platforms so that +// calls from libraries compiled with LFS (such as libstdc++ calling fopen64) are intercepted. +__asm__( + ".globl open64\n" ".set open64, open\n" + ".globl openat64\n" ".set openat64, openat\n" + ".globl __open64_2\n" ".set __open64_2, __open_2\n" + ".globl __openat64_2\n" ".set __openat64_2, __openat_2\n" + ".globl creat64\n" ".set creat64, creat\n" + ".globl fopen64\n" ".set fopen64, fopen\n" + ".globl freopen64\n" ".set freopen64, freopen\n" + ".globl stat64\n" ".set stat64, stat\n" + ".globl lstat64\n" ".set lstat64, lstat\n" + ".globl fstatat64\n" ".set fstatat64, fstatat\n" + ".globl truncate64\n" ".set truncate64, truncate\n" + ".globl statfs64\n" ".set statfs64, statfs\n" + ".globl statvfs64\n" ".set statvfs64, statvfs\n" + ".globl scandir64\n" ".set scandir64, scandir\n" + ".globl mkstemp64\n" ".set mkstemp64, mkstemp\n" + ".globl mkostemp64\n" ".set mkostemp64, mkostemp\n" + ".globl mkstemps64\n" ".set mkstemps64, mkstemps\n" + ".globl mkostemps64\n" ".set mkostemps64, mkostemps\n" + ".globl tmpfile64\n" ".set tmpfile64, tmpfile\n" +); +#endif + +#endif /* !DDNATV_HOST_TEST */ diff --git a/tools/nativeguest/hook/files.c b/tools/nativeguest/hook/files.c new file mode 100644 index 0000000000..6c52f70adf --- /dev/null +++ b/tools/nativeguest/hook/files.c @@ -0,0 +1,813 @@ +// Part of libddnatv-hook.so, included by ddnatv-hook.c (one translation unit, so the +// parts share their static functions and are built with the same command as before). +// +// Opening, creating and inspecting files: open, fopen, temporary files, directories, the stat family. + +#ifndef DDNATV_HOST_TEST + +#ifndef O_TMPFILE +#define O_TMPFILE (020000000 | 00200000) +#endif + +static inline int open_needs_mode(int flags) { +#ifdef O_TMPFILE + return ((flags & O_CREAT) != 0) || ((flags & O_TMPFILE) == O_TMPFILE); +#else + return (flags & O_CREAT) != 0; +#endif +} + +// The app is root for the programs it runs, and root puts names in every directory and takes them out +// whatever the mode of the directory; the app does not. A directory that is the app's own can have lost +// its owner's rights: an earlier version left the directories dpkg makes (with mode 0) at mode 0 when the +// chmod that follows did not take effect, and a package can ship a read-only one (#44). When a call that +// makes or removes a name is refused in such a directory, the directory is given its owner's rights and the +// call is made once more. Returns 1 when the directory was repaired (the call can be made again). +static int give_dir_owner_rights(int dirfd, const char *name) { + static int (*orig_fchmodat)(int, const char *, mode_t, int) = NULL; + if (!name || !*name) return 0; + char parent[PATH_MAX]; + size_t len = strlen(name); + if (len >= sizeof(parent)) return 0; + memcpy(parent, name, len + 1); + while (len > 1 && parent[len - 1] == '/') parent[--len] = '\0'; + char *slash = strrchr(parent, '/'); + if (!slash) { + strcpy(parent, "."); + } else if (slash == parent) { + parent[1] = '\0'; + } else { + *slash = '\0'; + } + struct stat st; + if (real_fstatat(dirfd, parent, &st, 0) != 0 || !S_ISDIR(st.st_mode)) return 0; + if (__atomic_load_n(&g_app_uid, __ATOMIC_RELAXED) == (uid_t)-1) load_app_ids(); + if (st.st_uid != __atomic_load_n(&g_app_uid, __ATOMIC_RELAXED)) return 0; + if ((st.st_mode & 0700) == 0700) return 0; + if (!orig_fchmodat) orig_fchmodat = (int (*)(int, const char *, mode_t, int))dlsym(RTLD_NEXT, "fchmodat"); + return orig_fchmodat && orig_fchmodat(dirfd, parent, (st.st_mode | 0700) & 07777, 0) == 0; +} + +// For the calls that were refused (EACCES): whether the directory of `name` was repaired, so that the call +// can be made again. errno stays what the call left when it was not. +static int refused_in_repaired_dir(int dirfd, const char *name) { + if (errno != EACCES) return 0; + int saved = errno; + int fixed = give_dir_owner_rights(dirfd, name); + if (!fixed) errno = saved; + return fixed; +} + +static int dlopen_preload_deps(const char *path, void *(*orig_dlopen)(const char *, int), int depth); + +// Hook dlopen and dlmopen +void *dlopen(const char *filename, int flags) { + static void *(*orig_dlopen)(const char *, int) = NULL; + if (!orig_dlopen) orig_dlopen = (void *(*)(const char *, int))dlsym(RTLD_NEXT, "dlopen"); + if (!filename) return orig_dlopen ? orig_dlopen(NULL, flags) : NULL; + if (!orig_dlopen) return NULL; + + char buf[PATH_MAX]; + const char *target = rewrite_path_follow(filename, buf, sizeof(buf)); + void *handle = orig_dlopen(target, flags); + if (!handle && target[0] == '/' && !(flags & RTLD_NOLOAD)) { + // Its dependencies may be in its RUNPATH, which ld.so looked for outside the rootfs + int saved = errno; + if (dlopen_preload_deps(target, orig_dlopen, 0) > 0) handle = orig_dlopen(target, flags); + if (!handle) errno = saved; + } + return handle; +} + +void *dlmopen(Lmid_t lmid, const char *filename, int flags) { + static void *(*orig_dlmopen)(Lmid_t, const char *, int) = NULL; + if (!orig_dlmopen) orig_dlmopen = (void *(*)(Lmid_t, const char *, int))dlsym(RTLD_NEXT, "dlmopen"); + if (!filename) return orig_dlmopen ? orig_dlmopen(lmid, NULL, flags) : NULL; + + char buf[PATH_MAX]; + const char *target = rewrite_path(filename, buf, sizeof(buf)); + return orig_dlmopen ? orig_dlmopen(lmid, target, flags) : NULL; +} + +// /proc//status (and a thread's) give the app's own ids, which every other call presents as +// root: process monitors comparing them with getuid() showed none of the session's processes. Only +// such tools are given root's there: Chromium (Steam's web helper) checks the ids of the processes it +// sandboxes in those files, and with them all 0 every helper exited and Steam never drew a frame. +static int reader_compares_process_ids(void) { + static const char *const k_readers[] = { + "ps", "top", "htop", "btop", "atop", "pgrep", "pkill", "pidof", "pstree", "w", "glances", + "ksystemstats", "plasma-systemmonitor", "ksysguard", "ksysguardd", "kinfocenter", "gnome-system-monitor", + }; + const char *me = program_invocation_short_name; + if (!me) return 0; + for (size_t i = 0; i < sizeof(k_readers) / sizeof(k_readers[0]); i++) { + if (strcmp(me, k_readers[i]) == 0) return 1; + } + return 0; +} + +static int is_proc_status_path(const char *p) { + if (!p || strncmp(p, "/proc/", 6) != 0) return 0; + p += 6; + if (strncmp(p, "self/", 5) == 0) p += 5; + else if (strncmp(p, "thread-self/", 12) == 0) p += 12; + else { + if (*p < '0' || *p > '9') return 0; + while (*p >= '0' && *p <= '9') p++; + if (*p++ != '/') return 0; + if (strncmp(p, "task/", 5) == 0) { + p += 5; + if (*p < '0' || *p > '9') return 0; + while (*p >= '0' && *p <= '9') p++; + if (*p++ != '/') return 0; + } + } + return strcmp(p, "status") == 0; +} + +static void proc_status_root_ids(char *text, size_t len, const char *field, unsigned long id) { + size_t flen = strlen(field); + for (size_t at = 0; at + flen <= len; at++) { + if ((at != 0 && text[at - 1] != '\n') || memcmp(text + at, field, flen) != 0) continue; + char *p = text + at + flen; + char *end = memchr(p, '\n', len - (size_t)(p - text)); + if (!end) end = text + len; + char out[128]; + size_t used = 0; + while (p < end && used + 24 < sizeof(out)) { + while (p < end && (*p == '\t' || *p == ' ')) out[used++] = *p++; + char *num = p; + while (p < end && *p >= '0' && *p <= '9') p++; + if (p == num) break; + unsigned long v = strtoul(num, NULL, 10); + used += (size_t)snprintf(out + used, sizeof(out) - used, "%lu", v == id ? 0UL : v); + } + if (p != end) return; + // the line shrinks or stays (an id becomes 0), so it is rewritten in place + size_t old_len = (size_t)(end - (text + at + flen)); + if (used > old_len) return; + memcpy(text + at + flen, out, used); + memmove(text + at + flen + used, end, len - (size_t)(end - text) + 1); + return; + } +} + +// The status file with the app's ids as root's, in a memory file; -2 when `path` is no such file +// or is opened for writing. +static int open_proc_status(const char *path, int flags) { + if ((flags & O_ACCMODE) != O_RDONLY || !is_proc_status_path(path) || !reader_compares_process_ids()) return -2; + int fd = (int)syscall(SYS_openat, AT_FDCWD, path, O_RDONLY | O_CLOEXEC); + if (fd < 0) return -1; + char text[16384]; + size_t len = 0; + for (;;) { + ssize_t n = (ssize_t)syscall(SYS_read, fd, text + len, sizeof(text) - 1 - len); + if (n <= 0) break; + len += (size_t)n; + if (len >= sizeof(text) - 1) break; + } + syscall(SYS_close, fd); + text[len] = '\0'; + if (__atomic_load_n(&g_app_uid, __ATOMIC_RELAXED) == (uid_t)-1) load_app_ids(); + proc_status_root_ids(text, len, "Uid:", (unsigned long)__atomic_load_n(&g_app_uid, __ATOMIC_RELAXED)); + len = strlen(text); + proc_status_root_ids(text, len, "Gid:", (unsigned long)__atomic_load_n(&g_app_gid, __ATOMIC_RELAXED)); + len = strlen(text); + int mfd = (int)syscall(SYS_memfd_create, "status", (flags & O_CLOEXEC) ? MFD_CLOEXEC : 0); + if (mfd < 0) return (int)syscall(SYS_openat, AT_FDCWD, path, flags, 0); + size_t written = 0; + while (written < len) { + ssize_t n = (ssize_t)syscall(SYS_write, mfd, text + written, len - written); + if (n <= 0) break; + written += (size_t)n; + } + syscall(SYS_lseek, mfd, 0, SEEK_SET); + return mfd; +} + +// openat(dirfd, "status") with dirfd on /proc/ or a thread of it +static int open_proc_status_at(int dirfd, const char *name, int flags) { + if (dirfd < 0 || !name || strcmp(name, "status") != 0 || (flags & O_ACCMODE) != O_RDONLY) return -2; + char link[48], dir[PATH_MAX]; + snprintf(link, sizeof(link), "/proc/self/fd/%d", dirfd); + long n = syscall(SYS_readlinkat, AT_FDCWD, link, dir, sizeof(dir) - 8); + if (n <= 0) return -2; + dir[n] = '\0'; + memcpy(dir + n, "/status", 8); + return open_proc_status(dir, flags); +} + +// Hook open +int open(const char *pathname, int flags, ...) { + static int (*orig_open)(const char *, int, ...) = NULL; + if (!orig_open) orig_open = (int (*)(const char *, int, ...))dlsym(RTLD_NEXT, "open"); + init_ddnatv_hook(); + char exe_buf[PATH_MAX]; + if (is_proc_self_exe(pathname)) { + if (g_real_exe[0] != '\0') { + pathname = g_real_exe; + } else if (handle_proc_self_exe(exe_buf, sizeof(exe_buf)) > 0) { + pathname = exe_buf; + } + } + int status_fd = open_proc_status(pathname, flags); + if (status_fd != -2) return status_fd; + char buf[PATH_MAX]; + const char *target = ((flags & O_NOFOLLOW) ? rewrite_path : rewrite_path_follow)(pathname, buf, sizeof(buf)); + if (open_needs_mode(flags)) { + va_list args; + va_start(args, flags); + mode_t mode = va_arg(args, mode_t); + va_end(args); + int fd = orig_open(target, flags, mode); + if (fd < 0 && (flags & O_CREAT) && refused_in_repaired_dir(AT_FDCWD, target)) fd = orig_open(target, flags, mode); + return fd; + } + return orig_open(target, flags); +} + +// Hook openat +int openat(int dirfd, const char *pathname, int flags, ...) { + static int (*orig_openat)(int, const char *, int, ...) = NULL; + if (!orig_openat) orig_openat = (int (*)(int, const char *, int, ...))dlsym(RTLD_NEXT, "openat"); + init_ddnatv_hook(); + DDNATV_AT_REMAP(dirfd, pathname); + char exe_buf[PATH_MAX]; + if (is_proc_self_exe(pathname)) { + if (g_real_exe[0] != '\0') { + pathname = g_real_exe; + } else if (handle_proc_self_exe(exe_buf, sizeof(exe_buf)) > 0) { + pathname = exe_buf; + } + } + int status_fd = (pathname && pathname[0] == '/') ? open_proc_status(pathname, flags) : open_proc_status_at(dirfd, pathname, flags); + if (status_fd != -2) return status_fd; + char buf[PATH_MAX]; + const char *target = (pathname && pathname[0] == '/') ? ((flags & O_NOFOLLOW) ? rewrite_path : rewrite_path_follow)(pathname, buf, sizeof(buf)) : pathname; + if (open_needs_mode(flags)) { + va_list args; + va_start(args, flags); + mode_t mode = va_arg(args, mode_t); + va_end(args); + int fd = orig_openat(dirfd, target, flags, mode); + if (fd < 0 && (flags & O_CREAT) && refused_in_repaired_dir(dirfd, target)) fd = orig_openat(dirfd, target, flags, mode); + return fd; + } + return orig_openat(dirfd, target, flags); +} + +// Hook creat +int creat(const char *pathname, mode_t mode) { + return open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode); +} + +// Fortified open variants used by GNU tar and other coreutils compiled with _FORTIFY_SOURCE=2/3 +int __open_2(const char *pathname, int flags) { + return open(pathname, flags); +} + +int __openat_2(int dirfd, const char *pathname, int flags) { + return openat(dirfd, pathname, flags); +} + +// openat2/open_how local definition: avoids depending on , +// which old NDK/cross headers may lack. Layout matches UAPI ver0 +// (flags, mode, resolve = 3 x __u64); extra fields are never read here. +struct ddnatv_open_how { + uint64_t flags; + uint64_t mode; + uint64_t resolve; +}; +#ifndef RESOLVE_BENEATH +#define RESOLVE_BENEATH 0x08 +#endif +#ifndef __NR_openat2 +#define __NR_openat2 437 +#endif +#ifndef __NR_fchmodat2 +#define __NR_fchmodat2 452 +#endif + +static long call_orig_syscall(long number, unsigned long a1, unsigned long a2, unsigned long a3, unsigned long a4, unsigned long a5, unsigned long a6) { + static long (*orig_syscall)(long, unsigned long, unsigned long, unsigned long, unsigned long, unsigned long, unsigned long) = NULL; + if (!orig_syscall) { + orig_syscall = (long (*)(long, unsigned long, unsigned long, unsigned long, unsigned long, unsigned long, unsigned long))dlsym(RTLD_NEXT, "syscall"); + } + return orig_syscall ? orig_syscall(number, a1, a2, a3, a4, a5, a6) : syscall(number, a1, a2, a3, a4, a5, a6); +} + +// Hook openat2 (used by GNU tar 1.35+/gnulib for RESOLVE_BENEATH traversal). +// Rewrites absolute paths like openat, then delegates; on ENOSYS (kernels +// without openat2, e.g. Android < 5.6) emulates with plain openat instead of +// trusting every caller to implement the fallback itself. +int openat2(int dirfd, const char *pathname, struct ddnatv_open_how *how, size_t usize) { + static int (*orig_openat2)(int, const char *, struct ddnatv_open_how *, size_t) = NULL; + if (!orig_openat2) orig_openat2 = (int (*)(int, const char *, struct ddnatv_open_how *, size_t))dlsym(RTLD_NEXT, "openat2"); + if (!pathname) { + errno = EFAULT; + return -1; + } + const char *given = pathname; + DDNATV_AT_REMAP(dirfd, pathname); + // a remapped path is absolute, which RESOLVE_BENEATH and the like refuse: the caller walks it itself + if (pathname != given && how && how->resolve != 0) { + errno = ENOSYS; + return -1; + } + char exe_buf[PATH_MAX]; + if (is_proc_self_exe(pathname)) { + if (g_real_exe[0] != '\0') { + pathname = g_real_exe; + } else if (handle_proc_self_exe(exe_buf, sizeof(exe_buf)) > 0) { + pathname = exe_buf; + } + } + char buf[PATH_MAX]; + const char *target = (pathname[0] == '/') ? rewrite_path(pathname, buf, sizeof(buf)) : pathname; + if (orig_openat2) { + int ret = orig_openat2(dirfd, target, how, usize); + if (ret >= 0 || errno != ENOSYS) return ret; + } +#ifdef __NR_openat2 + long sret = call_orig_syscall(__NR_openat2, (unsigned long)dirfd, (unsigned long)target, (unsigned long)how, (unsigned long)usize, 0, 0); + if (sret >= 0 || errno != ENOSYS) { + return (int)sret; + } +#endif + if (!how || usize < 16) { + errno = EINVAL; + return -1; + } + int flags = (int)(how->flags & 0xffffffffu); + mode_t mode = (mode_t)(how->mode & 07777u); + + // RESOLVE_BENEATH, RESOLVE_NO_SYMLINKS and the other resolve flags restrict every component of + // the path, which a plain openat() cannot do (a symlink in the middle would still be followed). + // Say so: callers such as gnulib's openat2 (GNU tar) then walk the path safely themselves. + if (how->resolve != 0) { + errno = ENOSYS; + return -1; + } + + if (open_needs_mode(flags)) { + return openat(dirfd, target, flags, mode); + } + return openat(dirfd, target, flags); +} + +// Hook fchmodat2 (used by GNU tar for lchmod-style symlink chmod). +// Delegates when the libc symbol exists, otherwise emulates: symlinks cannot +// be chmod'ed without kernel support, so skip them (return 0) like gnulib, +// and route everything else through fchmodat. +int fchmodat2(int dirfd, const char *pathname, mode_t mode, int flags) { + static int (*orig_fchmodat2)(int, const char *, mode_t, int) = NULL; + if (!orig_fchmodat2) orig_fchmodat2 = (int (*)(int, const char *, mode_t, int))dlsym(RTLD_NEXT, "fchmodat2"); + if (!pathname) { + errno = EFAULT; + return -1; + } + char buf[PATH_MAX]; + const char *target = (pathname[0] == '/') ? rewrite_path(pathname, buf, sizeof(buf)) : pathname; + if (orig_fchmodat2) { + int ret = orig_fchmodat2(dirfd, target, mode, flags); + if (ret == 0 || errno != ENOSYS) return ret; + } + if ((flags & AT_SYMLINK_NOFOLLOW) != 0) { + struct stat st; + if (fstatat(dirfd, target, &st, AT_SYMLINK_NOFOLLOW) == 0 && S_ISLNK(st.st_mode)) { + return 0; + } + } + return fchmodat(dirfd, target, mode, 0); +} + + +// Hook fopen +FILE *fopen(const char *pathname, const char *mode) { + static FILE *(*orig_fopen)(const char *, const char *) = NULL; + if (!orig_fopen) orig_fopen = (FILE *(*)(const char *, const char *))dlsym(RTLD_NEXT, "fopen"); + if (mode && mode[0] == 'r' && !strchr(mode, '+')) { + int status_fd = open_proc_status(pathname, O_RDONLY | (strchr(mode, 'e') ? O_CLOEXEC : 0)); + if (status_fd >= 0) return fdopen(status_fd, mode); + if (status_fd == -1) return NULL; + } + char buf[PATH_MAX]; + const char *target = rewrite_path_follow(pathname, buf, sizeof(buf)); + return orig_fopen(target, mode); +} + +// Hook freopen +FILE *freopen(const char *pathname, const char *mode, FILE *stream) { + static FILE *(*orig_freopen)(const char *, const char *, FILE *) = NULL; + if (!orig_freopen) orig_freopen = (FILE *(*)(const char *, const char *, FILE *))dlsym(RTLD_NEXT, "freopen"); + char buf[PATH_MAX]; + const char *target = rewrite_path_follow(pathname, buf, sizeof(buf)); + return orig_freopen(target, mode, stream); +} + +// Hook write +ssize_t write(int fd, const void *buf, size_t count) { + static ssize_t (*orig_write)(int, const void *, size_t) = NULL; + if (!orig_write) orig_write = (ssize_t (*)(int, const void *, size_t))dlsym(RTLD_NEXT, "write"); + return orig_write ? orig_write(fd, buf, count) : -1; +} + +// Hook mkstemp +int mkstemp(char *template) { + static int (*orig_mkstemp)(char *) = NULL; + if (!orig_mkstemp) orig_mkstemp = (int (*)(char *))dlsym(RTLD_NEXT, "mkstemp"); + if (!template) { errno = EINVAL; return -1; } + + char buf[PATH_MAX]; + const char *target = rewrite_path(template, buf, sizeof(buf)); + if (target == template) { + return orig_mkstemp ? orig_mkstemp(template) : -1; + } + + char tmp_buf[PATH_MAX]; + strncpy(tmp_buf, target, sizeof(tmp_buf) - 1); + tmp_buf[sizeof(tmp_buf) - 1] = '\0'; + + int fd = orig_mkstemp ? orig_mkstemp(tmp_buf) : -1; + if (fd >= 0) { + size_t orig_len = strlen(template); + size_t tmp_len = strlen(tmp_buf); + if (orig_len >= 6 && tmp_len >= 6) { + memcpy(template + orig_len - 6, tmp_buf + tmp_len - 6, 6); + } + } + return fd; +} + +// Hook mkostemp +int mkostemp(char *template, int flags) { + static int (*orig_mkostemp)(char *, int) = NULL; + if (!orig_mkostemp) orig_mkostemp = (int (*)(char *, int))dlsym(RTLD_NEXT, "mkostemp"); + if (!template) { errno = EINVAL; return -1; } + + char buf[PATH_MAX]; + const char *target = rewrite_path(template, buf, sizeof(buf)); + if (target == template) { + return orig_mkostemp ? orig_mkostemp(template, flags) : -1; + } + + char tmp_buf[PATH_MAX]; + strncpy(tmp_buf, target, sizeof(tmp_buf) - 1); + tmp_buf[sizeof(tmp_buf) - 1] = '\0'; + + int fd = orig_mkostemp ? orig_mkostemp(tmp_buf, flags) : -1; + if (fd >= 0) { + size_t orig_len = strlen(template); + size_t tmp_len = strlen(tmp_buf); + if (orig_len >= 6 && tmp_len >= 6) { + memcpy(template + orig_len - 6, tmp_buf + tmp_len - 6, 6); + } + } + return fd; +} + +// Hook mkstemps +int mkstemps(char *template, int suffixlen) { + static int (*orig_mkstemps)(char *, int) = NULL; + if (!orig_mkstemps) orig_mkstemps = (int (*)(char *, int))dlsym(RTLD_NEXT, "mkstemps"); + if (!template) { errno = EINVAL; return -1; } + + char buf[PATH_MAX]; + const char *target = rewrite_path(template, buf, sizeof(buf)); + if (target == template) { + return orig_mkstemps ? orig_mkstemps(template, suffixlen) : -1; + } + + char tmp_buf[PATH_MAX]; + strncpy(tmp_buf, target, sizeof(tmp_buf) - 1); + tmp_buf[sizeof(tmp_buf) - 1] = '\0'; + + int fd = orig_mkstemps ? orig_mkstemps(tmp_buf, suffixlen) : -1; + if (fd >= 0) { + size_t orig_len = strlen(template); + size_t tmp_len = strlen(tmp_buf); + if (orig_len >= (size_t)(suffixlen + 6) && tmp_len >= (size_t)(suffixlen + 6)) { + memcpy(template + orig_len - suffixlen - 6, tmp_buf + tmp_len - suffixlen - 6, 6); + } + } + return fd; +} + +// Hook mkostemps +int mkostemps(char *template, int suffixlen, int flags) { + static int (*orig_mkostemps)(char *, int, int) = NULL; + if (!orig_mkostemps) orig_mkostemps = (int (*)(char *, int, int))dlsym(RTLD_NEXT, "mkostemps"); + if (!template) { errno = EINVAL; return -1; } + + char buf[PATH_MAX]; + const char *target = rewrite_path(template, buf, sizeof(buf)); + if (target == template) { + return orig_mkostemps ? orig_mkostemps(template, suffixlen, flags) : -1; + } + + char tmp_buf[PATH_MAX]; + strncpy(tmp_buf, target, sizeof(tmp_buf) - 1); + tmp_buf[sizeof(tmp_buf) - 1] = '\0'; + + int fd = orig_mkostemps ? orig_mkostemps(tmp_buf, suffixlen, flags) : -1; + if (fd >= 0) { + size_t orig_len = strlen(template); + size_t tmp_len = strlen(tmp_buf); + if (orig_len >= (size_t)(suffixlen + 6) && tmp_len >= (size_t)(suffixlen + 6)) { + memcpy(template + orig_len - suffixlen - 6, tmp_buf + tmp_len - suffixlen - 6, 6); + } + } + return fd; +} + +// Hook mkdtemp +char *mkdtemp(char *template) { + static char *(*orig_mkdtemp)(char *) = NULL; + if (!orig_mkdtemp) orig_mkdtemp = (char *(*)(char *))dlsym(RTLD_NEXT, "mkdtemp"); + if (!template) { errno = EINVAL; return NULL; } + + char buf[PATH_MAX]; + const char *target = rewrite_path(template, buf, sizeof(buf)); + if (target == template) { + return orig_mkdtemp ? orig_mkdtemp(template) : NULL; + } + + char tmp_buf[PATH_MAX]; + strncpy(tmp_buf, target, sizeof(tmp_buf) - 1); + tmp_buf[sizeof(tmp_buf) - 1] = '\0'; + + char *res = orig_mkdtemp ? orig_mkdtemp(tmp_buf) : NULL; + if (res) { + size_t orig_len = strlen(template); + size_t tmp_len = strlen(tmp_buf); + if (orig_len >= 6 && tmp_len >= 6) { + memcpy(template + orig_len - 6, tmp_buf + tmp_len - 6, 6); + } + return template; + } + return NULL; +} + +// Hook tmpfile +FILE *tmpfile(void) { + init_ddnatv_hook(); + char template_buf[PATH_MAX]; + if (g_ddnatv_root[0] != '\0') { + snprintf(template_buf, sizeof(template_buf), "%s/tmp/tmpfile.XXXXXX", g_ddnatv_root); + } else { + snprintf(template_buf, sizeof(template_buf), "/tmp/tmpfile.XXXXXX"); + } + int fd = mkstemp(template_buf); + if (fd < 0) return NULL; + unlink(template_buf); + FILE *f = fdopen(fd, "w+b"); + if (!f) { + int saved_errno = errno; + close(fd); + errno = saved_errno; + } + return f; +} + +#ifndef __USE_FILE_OFFSET64 +// Programs built with _LARGEFILE64_SOURCE call this name. With _FILE_OFFSET_BITS=64 (the 32-bit ARM +// build) the headers already export the definition above as tmpfile64. +FILE *tmpfile64(void) { + return tmpfile(); +} +#endif + +// Hook opendir +DIR *opendir(const char *name) { + static DIR *(*orig_opendir)(const char *) = NULL; + if (!orig_opendir) orig_opendir = (DIR *(*)(const char *))dlsym(RTLD_NEXT, "opendir"); + char buf[PATH_MAX]; + const char *target = rewrite_path_follow(name, buf, sizeof(buf)); + return orig_opendir(target); +} + +// Hook stat +int stat(const char *pathname, struct stat *statbuf) { + static int (*orig_stat)(const char *, struct stat *) = NULL; + if (!orig_stat) orig_stat = (int (*)(const char *, struct stat *))dlsym(RTLD_NEXT, "stat"); + char buf[PATH_MAX]; + const char *target = rewrite_path_follow(pathname, buf, sizeof(buf)); + int ret = orig_stat ? orig_stat(target, statbuf) : -1; + if (ret != 0 && errno == ENOENT) { + char alt[PATH_MAX]; + const char *retry = usrmerge_retry_path(target, alt, sizeof(alt)); + if (retry) ret = orig_stat(retry, statbuf); + } + if (ret == 0) apply_stat_emulation(statbuf); + return ret; +} + +// Hook lstat +int lstat(const char *pathname, struct stat *statbuf) { + static int (*orig_lstat)(const char *, struct stat *) = NULL; + if (!orig_lstat) orig_lstat = (int (*)(const char *, struct stat *))dlsym(RTLD_NEXT, "lstat"); + char buf[PATH_MAX]; + const char *target = rewrite_path(pathname, buf, sizeof(buf)); + int ret = orig_lstat ? orig_lstat(target, statbuf) : -1; + char alt[PATH_MAX]; + if (ret != 0 && errno == ENOENT) { + const char *retry = usrmerge_retry_path(target, alt, sizeof(alt)); + if (retry) { + ret = orig_lstat(retry, statbuf); + if (ret == 0) target = retry; + } + } + if (ret == 0 && statbuf && S_ISLNK(statbuf->st_mode) && g_ddnatv_root[0] != '\0' && target && + readlink_result_may_change(pathname, (long long)statbuf->st_size)) { + char link_buf[PATH_MAX]; + static ssize_t (*orig_readlink)(const char *, char *, size_t) = NULL; + if (!orig_readlink) orig_readlink = (ssize_t (*)(const char *, char *, size_t))dlsym(RTLD_NEXT, "readlink"); + ssize_t llen = orig_readlink ? orig_readlink(target, link_buf, sizeof(link_buf) - 1) : -1; + if (llen > 0) { + ssize_t post_len = postprocess_readlink(pathname, link_buf, llen, sizeof(link_buf)); + if (post_len > 0) { + statbuf->st_size = post_len; + } + } + } + if (ret == 0) apply_stat_emulation(statbuf); + return ret; +} + +// Hook fstatat +int fstatat(int dirfd, const char *pathname, struct stat *statbuf, int flags) { + static int (*orig_fstatat)(int, const char *, struct stat *, int) = NULL; + if (!orig_fstatat) orig_fstatat = (int (*)(int, const char *, struct stat *, int))dlsym(RTLD_NEXT, "fstatat"); + DDNATV_AT_REMAP(dirfd, pathname); + char buf[PATH_MAX]; + const char *target = (pathname && pathname[0] == '/') ? ((flags & AT_SYMLINK_NOFOLLOW) ? rewrite_path : rewrite_path_follow)(pathname, buf, sizeof(buf)) : pathname; + int ret = orig_fstatat ? orig_fstatat(dirfd, target, statbuf, flags) : -1; + char alt[PATH_MAX]; + if (ret != 0 && errno == ENOENT && pathname && pathname[0] == '/') { + const char *retry = usrmerge_retry_path(target, alt, sizeof(alt)); + if (retry) { + ret = orig_fstatat(dirfd, retry, statbuf, flags); + if (ret == 0) target = retry; + } + } + if (ret == 0 && statbuf && (flags & AT_SYMLINK_NOFOLLOW) && S_ISLNK(statbuf->st_mode) && g_ddnatv_root[0] != '\0' && target && + readlink_result_may_change(pathname, (long long)statbuf->st_size)) { + char link_buf[PATH_MAX]; + static ssize_t (*orig_readlinkat)(int, const char *, char *, size_t) = NULL; + if (!orig_readlinkat) orig_readlinkat = (ssize_t (*)(int, const char *, char *, size_t))dlsym(RTLD_NEXT, "readlinkat"); + ssize_t llen = orig_readlinkat ? orig_readlinkat(dirfd, target, link_buf, sizeof(link_buf) - 1) : -1; + if (llen > 0) { + ssize_t post_len = postprocess_readlink(pathname, link_buf, llen, sizeof(link_buf)); + if (post_len > 0) { + statbuf->st_size = post_len; + } + } + } + if (ret == 0) apply_stat_emulation(statbuf); + return ret; +} + +// Hook fstat +int fstat(int fd, struct stat *statbuf) { + static int (*orig_fstat)(int, struct stat *) = NULL; + if (!orig_fstat) orig_fstat = (int (*)(int, struct stat *))dlsym(RTLD_NEXT, "fstat"); + int ret = orig_fstat ? orig_fstat(fd, statbuf) : -1; + if (ret == 0) apply_stat_emulation(statbuf); + return ret; +} + +// Glibc __xstat compatibility hooks, what programs built against glibc before 2.33 call (Firefox): +// they present owners like stat() does, or Firefox saw $HOME owned by the app's uid while geteuid() +// said 0 and refused to start ("as root in a regular user's session"). +int __xstat(int ver, const char *pathname, struct stat *statbuf) { + static int (*orig___xstat)(int, const char *, struct stat *) = NULL; + if (!orig___xstat) orig___xstat = (int (*)(int, const char *, struct stat *))dlsym(RTLD_NEXT, "__xstat"); + if (!orig___xstat) return stat(pathname, statbuf); + char buf[PATH_MAX]; + int ret = orig___xstat(ver, rewrite_path_follow(pathname, buf, sizeof(buf)), statbuf); + if (ret == 0) apply_stat_emulation(statbuf); + return ret; +} + +int __xstat64(int ver, const char *pathname, struct stat64 *statbuf) { + static int (*orig___xstat64)(int, const char *, struct stat64 *) = NULL; + if (!orig___xstat64) orig___xstat64 = (int (*)(int, const char *, struct stat64 *))dlsym(RTLD_NEXT, "__xstat64"); + if (!orig___xstat64) return stat(pathname, (struct stat *)statbuf); + char buf[PATH_MAX]; + int ret = orig___xstat64(ver, rewrite_path_follow(pathname, buf, sizeof(buf)), statbuf); + if (ret == 0) apply_stat_emulation((struct stat *)statbuf); + return ret; +} + +int __lxstat(int ver, const char *pathname, struct stat *statbuf) { + static int (*orig___lxstat)(int, const char *, struct stat *) = NULL; + if (!orig___lxstat) orig___lxstat = (int (*)(int, const char *, struct stat *))dlsym(RTLD_NEXT, "__lxstat"); + if (!orig___lxstat) return lstat(pathname, statbuf); + char buf[PATH_MAX]; + int ret = orig___lxstat(ver, rewrite_path(pathname, buf, sizeof(buf)), statbuf); + if (ret == 0) apply_stat_emulation(statbuf); + return ret; +} + +int __lxstat64(int ver, const char *pathname, struct stat64 *statbuf) { + static int (*orig___lxstat64)(int, const char *, struct stat64 *) = NULL; + if (!orig___lxstat64) orig___lxstat64 = (int (*)(int, const char *, struct stat64 *))dlsym(RTLD_NEXT, "__lxstat64"); + if (!orig___lxstat64) return lstat(pathname, (struct stat *)statbuf); + char buf[PATH_MAX]; + int ret = orig___lxstat64(ver, rewrite_path(pathname, buf, sizeof(buf)), statbuf); + if (ret == 0) apply_stat_emulation((struct stat *)statbuf); + return ret; +} + +int __fxstat(int ver, int fd, struct stat *statbuf) { + static int (*orig___fxstat)(int, int, struct stat *) = NULL; + if (!orig___fxstat) orig___fxstat = (int (*)(int, int, struct stat *))dlsym(RTLD_NEXT, "__fxstat"); + if (!orig___fxstat) return fstat(fd, statbuf); + int ret = orig___fxstat(ver, fd, statbuf); + if (ret == 0) apply_stat_emulation(statbuf); + return ret; +} + +int __fxstat64(int ver, int fd, struct stat64 *statbuf) { + static int (*orig___fxstat64)(int, int, struct stat64 *) = NULL; + if (!orig___fxstat64) orig___fxstat64 = (int (*)(int, int, struct stat64 *))dlsym(RTLD_NEXT, "__fxstat64"); + if (!orig___fxstat64) return fstat(fd, (struct stat *)statbuf); + int ret = orig___fxstat64(ver, fd, statbuf); + if (ret == 0) apply_stat_emulation((struct stat *)statbuf); + return ret; +} + +int __fxstatat(int ver, int dirfd, const char *pathname, struct stat *statbuf, int flags) { + static int (*orig___fxstatat)(int, int, const char *, struct stat *, int) = NULL; + if (!orig___fxstatat) orig___fxstatat = (int (*)(int, int, const char *, struct stat *, int))dlsym(RTLD_NEXT, "__fxstatat"); + if (!orig___fxstatat) return fstatat(dirfd, pathname, statbuf, flags); + DDNATV_AT_REMAP(dirfd, pathname); + char buf[PATH_MAX]; + const char *target = (pathname && pathname[0] == '/') ? ((flags & AT_SYMLINK_NOFOLLOW) ? rewrite_path : rewrite_path_follow)(pathname, buf, sizeof(buf)) : pathname; + int ret = orig___fxstatat(ver, dirfd, target, statbuf, flags); + if (ret == 0) apply_stat_emulation(statbuf); + return ret; +} + +int __fxstatat64(int ver, int dirfd, const char *pathname, struct stat64 *statbuf, int flags) { + static int (*orig___fxstatat64)(int, int, const char *, struct stat64 *, int) = NULL; + if (!orig___fxstatat64) orig___fxstatat64 = (int (*)(int, int, const char *, struct stat64 *, int))dlsym(RTLD_NEXT, "__fxstatat64"); + if (!orig___fxstatat64) return fstatat(dirfd, pathname, (struct stat *)statbuf, flags); + DDNATV_AT_REMAP(dirfd, pathname); + char buf[PATH_MAX]; + const char *target = (pathname && pathname[0] == '/') ? ((flags & AT_SYMLINK_NOFOLLOW) ? rewrite_path : rewrite_path_follow)(pathname, buf, sizeof(buf)) : pathname; + int ret = orig___fxstatat64(ver, dirfd, target, statbuf, flags); + if (ret == 0) apply_stat_emulation((struct stat *)statbuf); + return ret; +} + +// Hook statx +int statx(int dirfd, const char *pathname, int flags, unsigned int mask, struct statx *statxbuf) { + // Rust's standard library asks whether statx exists by calling it with a NULL path and buffer + // (EFAULT means it does). glibc's header declares both as nonnull, so the compiler would drop + // the NULL check below and read the path: every Rust program (rg, rustc, cargo...) crashed. + __asm__ volatile("" : "+r"(pathname), "+r"(statxbuf)); + static int (*orig_statx)(int, const char *, int, unsigned int, struct statx *) = NULL; + if (!orig_statx) orig_statx = (int (*)(int, const char *, int, unsigned int, struct statx *))dlsym(RTLD_NEXT, "statx"); + DDNATV_AT_REMAP(dirfd, pathname); + char buf[PATH_MAX]; + const char *target = (pathname && pathname[0] == '/') ? ((flags & AT_SYMLINK_NOFOLLOW) ? rewrite_path : rewrite_path_follow)(pathname, buf, sizeof(buf)) : pathname; + int ret = orig_statx ? orig_statx(dirfd, target, flags, mask, statxbuf) : -1; + if (ret == 0 && statxbuf && hardlinks_maybe_any()) { + const unsigned int needed = STATX_INO | STATX_NLINK | STATX_MTIME; + struct statx full; + const struct statx *sx = statxbuf; + if ((statxbuf->stx_mask & needed) != needed) { + // The caller asked for less ("stat -c %h" for the link count only): what an emulated + // link is recognized by + int saved_errno = errno; + sx = orig_statx(dirfd, target, flags, needed, &full) == 0 && (full.stx_mask & needed) == needed ? &full : NULL; + errno = saved_errno; + } + if (sx) { + struct stat st; + memset(&st, 0, sizeof(st)); + st.st_dev = makedev(sx->stx_dev_major, sx->stx_dev_minor); + st.st_ino = sx->stx_ino; + st.st_nlink = sx->stx_nlink; + st.st_mtim.tv_sec = sx->stx_mtime.tv_sec; + st.st_mtim.tv_nsec = sx->stx_mtime.tv_nsec; + apply_emulated_hardlink(&st); + if (st.st_ino != sx->stx_ino || st.st_nlink != sx->stx_nlink) { + statxbuf->stx_ino = st.st_ino; + statxbuf->stx_nlink = st.st_nlink; + statxbuf->stx_mask |= STATX_INO | STATX_NLINK; + } + } + } + if (ret == 0 && statxbuf) { + uid_t uid = (uid_t)statxbuf->stx_uid; + gid_t gid = (gid_t)statxbuf->stx_gid; + present_owner_as_root((statxbuf->stx_mask & STATX_UID) ? &uid : NULL, (statxbuf->stx_mask & STATX_GID) ? &gid : NULL); + statxbuf->stx_uid = (uint32_t)uid; + statxbuf->stx_gid = (uint32_t)gid; + } + return ret; +} + +#endif /* !DDNATV_HOST_TEST */ diff --git a/tools/nativeguest/hook/go_programs.c b/tools/nativeguest/hook/go_programs.c new file mode 100644 index 0000000000..987d0132ee --- /dev/null +++ b/tools/nativeguest/hook/go_programs.c @@ -0,0 +1,465 @@ +// Part of libddnatv-hook.so, included by ddnatv-hook.c (one translation unit, so the +// parts share their static functions and are built with the same command as before). +// +// Static Go programs: they make their system calls themselves, so Ubuntu's paths mean nothing to +// them. The code of gosys/gosys.c is added to their file. + +// ---- Static Go programs ------------------------------------------------------------------- +// A Go program built without cgo (all of Ubuntu's, and the single files downloaded from a project's +// releases) has no C library: the preloaded hook is never loaded into it, so "/etc/resolv.conf" is +// a file of Android's that does not exist, and so is every program it starts. Go makes all of its +// system calls but a few of its runtime's in one assembly function, internal/runtime/syscall. +// Syscall6, which loads the number and the arguments from the stack and executes "svc #0". +// +// Before such a program (AArch64, Go 1.19 or newer) starts, The native userland changes its file in place, once: +// - the code of gosys/gosys.c is put after the program's code, in the zero padding that the Go +// linker leaves between its segments (the segment is made longer, and executable if it only +// was readable), +// - zero-filled memory for it is added to the end of the program's data segment, +// - the "svc #0" of Syscall6 becomes a branch to a few instructions in that code, which call it +// and go back to the instruction after the "svc". +// The code maps the paths of the calls into the rootfs, as the hook does for C programs, and starts +// the programs of the rootfs through the hook. The file keeps its size, its times and its mode; +// only the program headers, the padding and one instruction differ. The file is changed back and +// patched again when the code of a newer The native userland differs (the trailer says what to undo). + +#ifdef DDNATV_HAVE_GOSYS + +#define GO_MAX_PH 32 +#define GO_PF_X 1u +#define GO_PF_W 2u +#define GO_ET_EXEC 2 +#define GO_ET_DYN 3 + +struct go_ph { + uint32_t type, flags; + uint64_t offset, vaddr, filesz, memsz; + uint64_t pos; // where the entry is in the file +}; + +static uint64_t go_rd64(const unsigned char *p) { return (uint64_t)rd32(p) | ((uint64_t)rd32(p + 4) << 32); } +static void go_wr64(unsigned char *p, uint64_t v) { wr32(p, (uint32_t)v); wr32(p + 4, (uint32_t)(v >> 32)); } + +// The program headers of a 64-bit little-endian ELF; their number, or -1 +static int go_read_phdrs(int fd, struct go_ph *ph, uint16_t *etype, uint16_t *machine) { + unsigned char eh[64]; + if (pread(fd, eh, sizeof(eh), 0) != (ssize_t)sizeof(eh) || memcmp(eh, "\x7f" "ELF", 4) != 0 || eh[4] != 2 || eh[5] != 1) + return -1; + *etype = rd16(eh + 16); + *machine = rd16(eh + 18); + uint64_t phoff = go_rd64(eh + 32); + uint16_t phentsize = rd16(eh + 54), phnum = rd16(eh + 56); + if (phentsize != 56 || phnum == 0 || phnum > GO_MAX_PH) return -1; + for (int i = 0; i < phnum; i++) { + unsigned char e[56]; + uint64_t pos = phoff + (uint64_t)i * 56; + if (pread(fd, e, sizeof(e), (off_t)pos) != (ssize_t)sizeof(e)) return -1; + ph[i].type = rd32(e); + ph[i].flags = rd32(e + 4); + ph[i].offset = go_rd64(e + 8); + ph[i].vaddr = go_rd64(e + 16); + ph[i].filesz = go_rd64(e + 32); + ph[i].memsz = go_rd64(e + 40); + ph[i].pos = pos; + } + return phnum; +} + +// The instructions of Syscall6 up to its system call: x8 and x0 to x5 loaded from the stack +// (ldr x8, [sp, #8]; ldr x0, [sp, #16] ... ldr x5, [sp, #56]), then "svc #0" and "cmn x0, #4095" +static const uint32_t k_go_syscall6[7] = { + 0xf94007e8u, 0xf9400be0u, 0xf9400fe1u, 0xf94013e2u, 0xf94017e3u, 0xf9401be4u, 0xf9401fe5u, +}; +#define GO_SVC 0xd4000001u +#define GO_CMN_X0_4095 0xb13ffc1fu + +// Where Syscall6 makes its call: the file offset of the instruction after the seven loads (the +// "svc #0", or the branch that replaced it). *found counts the places; the first is returned. +static int64_t go_find_syscall6(int fd, const struct go_ph *ph, int n, int *found) { + int64_t first = -1; + *found = 0; + enum { CHUNK = 1 << 20, KEEP = 36 }; + unsigned char *buf = (unsigned char *)malloc(CHUNK + KEEP); + if (!buf) return -1; + for (int i = 0; i < n; i++) { + if (ph[i].type != 1 /* PT_LOAD */ || !(ph[i].flags & GO_PF_X) || ph[i].filesz == 0) continue; + uint64_t pos = 0; + while (pos < ph[i].filesz) { + uint64_t want = ph[i].filesz - pos; + if (want > CHUNK + KEEP) want = CHUNK + KEEP; + ssize_t got = pread(fd, buf, (size_t)want, (off_t)(ph[i].offset + pos)); + if (got <= KEEP) break; + for (ssize_t o = 0; o + 36 <= got; o += 4) { + if (rd32(buf + o) != k_go_syscall6[0]) continue; + int same = 1; + for (int k = 1; k < 7 && same; k++) same = rd32(buf + o + 4 * k) == k_go_syscall6[k]; + if (!same || rd32(buf + o + 32) != GO_CMN_X0_4095) continue; + if (*found == 0) first = (int64_t)(ph[i].offset + pos + (uint64_t)o + 28); + (*found)++; + } + if (pos + (uint64_t)got >= ph[i].filesz) break; + pos += (uint64_t)got - (KEEP - 4); // the next chunk starts where the last place of this could begin, no place is split or counted twice + } + } + free(buf); + return first; +} + +// "b " / "bl " for a distance in bytes, or 0 when it does not fit +static uint32_t go_branch(uint32_t base, int64_t delta) { + if ((delta & 3) != 0 || delta >= (1LL << 27) || delta < -(1LL << 27)) return 0; + return base | (uint32_t)((delta >> 2) & 0x3ffffff); +} + +static uint64_t go_align_up(uint64_t v, uint64_t a) { return (v + a - 1) & ~(a - 1); } + +// The strings the added code needs, as they are now +static int go_make_header(struct gosys_hdr *h) { + if (g_ddnatv_root_len == 0 || g_ddnatv_root_len >= GOSYS_ROOT_MAX) return -1; + memcpy(h->root, g_ddnatv_root, g_ddnatv_root_len); + h->root_len = (uint32_t)g_ddnatv_root_len; + + char buf[PATH_MAX]; + if (find_dynamic_linker_for_machine(g_ddnatv_root, EM_AARCH64, buf, sizeof(buf)) && strlen(buf) < GOSYS_PATH_MAX) { + h->ldso_len = (uint32_t)strlen(buf); + memcpy(h->ldso, buf, h->ldso_len); + } + static const char *const hooks[] = { "/usr/lib/libddnatv-hook.so", "/lib/libddnatv-hook.so" }; + for (size_t i = 0; i < sizeof(hooks) / sizeof(hooks[0]); i++) { + size_t n = ddnatv_append(buf, sizeof(buf), 0, g_ddnatv_root, g_ddnatv_root_len); + n = ddnatv_append(buf, sizeof(buf), n, hooks[i], strlen(hooks[i])); + if (n < GOSYS_PATH_MAX && access(buf, F_OK) == 0) { + h->hook_len = (uint32_t)n; + memcpy(h->hook, buf, n); + break; + } + } + // The program that starts the others: bash, which can give them the argv[0] they are asked for + static const char *const runners[] = { "/usr/bin/bash", "/bin/bash", "/usr/bin/env", "/bin/env" }; + for (size_t i = 0; i < sizeof(runners) / sizeof(runners[0]); i++) { + size_t n = ddnatv_append(buf, sizeof(buf), 0, g_ddnatv_root, g_ddnatv_root_len); + n = ddnatv_append(buf, sizeof(buf), n, runners[i], strlen(runners[i])); + if (n < GOSYS_PATH_MAX && access(buf, F_OK) == 0) { + h->runner_len = (uint32_t)n; + memcpy(h->runner, buf, n); + if (i < 2) h->flags |= GOSYS_FLAG_RUNNER_IS_BASH; + break; + } + } + // The directories the programs of an AArch64 rootfs load their libraries from + static const int dirs[] = { 0, 1, 4, 5, 6 }; + size_t used = 0; + for (size_t i = 0; i < sizeof(dirs) / sizeof(dirs[0]); i++) { + const char *d = k_rootfs_lib_dirs[dirs[i]]; + if (i > 0) used = ddnatv_append(buf, sizeof(buf), used, ":", 1); + used = ddnatv_append(buf, sizeof(buf), used, g_ddnatv_root, g_ddnatv_root_len); + used = ddnatv_append(buf, sizeof(buf), used, d, strlen(d)); + } + if (used < GOSYS_LIBS_MAX) { + h->libs_len = (uint32_t)used; + memcpy(h->libs, buf, used); + } + if (h->ldso_len == 0 || h->runner_len == 0) h->runner_len = 0; // Ubuntu programs cannot be started + return 0; +} + +static int go_pwrite_all(int fd, const void *buf, size_t len, uint64_t off) { + return pwrite(fd, buf, len, (off_t)off) == (ssize_t)len ? 0 : -1; +} + +static int go_open_for_write(int fd, const struct stat *st, const char *path, int *made_writable) { + int wfd = open(path, O_WRONLY | O_CLOEXEC); + if (wfd < 0 && errno == EACCES && fchmod(fd, (st->st_mode | S_IWUSR) & 07777) == 0) { + *made_writable = 1; + wfd = open(path, O_WRONLY | O_CLOEXEC); + } + return wfd; // ETXTBSY: the program is running right now, patched at its next start +} + +// The bytes of the added code for a given place: the code with its header (the strings, and where +// the scratch memory is), the instructions that connect the call site to it, and the trailer that +// says what was changed to put it there. +static void go_build_image(unsigned char *img, const struct gosys_hdr *strings, uint64_t cave_v, uint64_t scratch_v, + uint32_t to_entry, uint32_t back, const struct gosys_trailer *info) { + memcpy(img, gosys_blob, GOSYS_BLOB_SIZE); + struct gosys_hdr h = *strings; + h.scratch_rel = (int64_t)scratch_v - (int64_t)cave_v; + memcpy(img, &h, sizeof(h)); + unsigned char *th = img + GOSYS_THUNKS_OFF; + wr32(th + 0, 0xd10043ffu); // sub sp, sp, #16 + wr32(th + 4, 0xf90003feu); // str x30, [sp] + wr32(th + 8, 0xaa0803e7u); // mov x7, x8 (the number is the eighth argument) + wr32(th + 12, to_entry); // bl entry + wr32(th + 16, 0xf94003feu); // ldr x30, [sp] + wr32(th + 20, 0x910043ffu); // add sp, sp, #16 + wr32(th + 24, back); // b + struct gosys_trailer t; + memcpy(&t, img + GOSYS_BLOB_SIZE - sizeof(t), sizeof(t)); + t.version = GOSYS_VERSION; + t.total_size = GOSYS_BLOB_SIZE; + t.text_orig_filesz = info->text_orig_filesz; + t.text_orig_flags = info->text_orig_flags; + t.rw_orig_memsz = info->rw_orig_memsz; + t.text_index = info->text_index; + t.rw_index = info->rw_index; + t.site_file_off = info->site_file_off; + memcpy(img + GOSYS_BLOB_SIZE - sizeof(t), &t, sizeof(t)); +} + +// A short fingerprint of the added code, so that a program patched by an older The native userland is looked at +// again (the marker of a prepared program has it) +static uint32_t go_blob_fingerprint(void) { + static uint32_t fp; + if (fp == 0) { + uint32_t h = 2166136261u; + for (size_t i = 0; i < GOSYS_BLOB_SIZE; i++) h = (h ^ gosys_blob[i]) * 16777619u; + fp = h ? h : 1; + } + return fp; +} + +// Gives the program its old segments and call back: the added code is zeroed, the data segment +// is shortened, the instruction is a "svc #0" again. +static int go_undo(int wfd, struct go_ph *ph, int cave_seg, const struct gosys_trailer *tr, int64_t site_off) { + uint64_t cave_off = ph[cave_seg].offset + ph[cave_seg].filesz - tr->total_size; + unsigned char word[4]; + wr32(word, GO_SVC); + if (go_pwrite_all(wfd, word, 4, (uint64_t)site_off) != 0) return -1; + unsigned char sz[16]; + go_wr64(sz, tr->text_orig_filesz); + go_wr64(sz + 8, tr->text_orig_filesz); + if (go_pwrite_all(wfd, sz, 16, ph[cave_seg].pos + 32) != 0) return -1; + wr32(word, tr->text_orig_flags); + if (go_pwrite_all(wfd, word, 4, ph[cave_seg].pos + 4) != 0) return -1; + if (tr->rw_index < GO_MAX_PH) { + unsigned char mem[8]; + go_wr64(mem, tr->rw_orig_memsz); + if (go_pwrite_all(wfd, mem, 8, ph[tr->rw_index].pos + 40) != 0) return -1; + } + unsigned char *zero = (unsigned char *)calloc(1, tr->total_size); + if (!zero) return -1; + int rc = go_pwrite_all(wfd, zero, tr->total_size, cave_off); + free(zero); + return rc; +} + +// DDNATV_NO_GOSYS=1 (any value but empty or 0): no code is added to Go programs, and the code that is +// in one is taken out again, so that a program can be run as it was built, to see whether a fault +// is the program's or the code's. The marker of a prepared program says it (static_marker). +static int go_disabled(void) { + const char *v = getenv("DDNATV_NO_GOSYS"); + return v && v[0] != '\0' && strcmp(v, "0") != 0; +} + +// The three branches that connect the call site and the code: site -> thunk, thunk -> entry (bl), +// thunk -> the instruction after the site. 0 when one of them is too far. +static int go_branches(uint64_t cave_v, uint64_t site_v, uint32_t *to_thunk, uint32_t *to_entry, uint32_t *back) { + uint64_t thunk_v = cave_v + GOSYS_THUNKS_OFF; + *to_thunk = go_branch(0x14000000u, (int64_t)thunk_v - (int64_t)site_v); + *to_entry = go_branch(0x94000000u, (int64_t)(cave_v + GOSYS_ENTRY_OFF) - (int64_t)(thunk_v + 12)); + *back = go_branch(0x14000000u, (int64_t)(site_v + 4) - (int64_t)(thunk_v + 24)); + return *to_thunk && *to_entry && *back; +} + +// Adds the code to the program, or brings it up to date. Returns 1 when it changed the file, 0 when +// there was nothing to do (not a Go program of a kind the code can serve, or already done), -1 when +// the file could not be changed (it is started as it is). +static int prepare_go_program(int fd, const char *path, const struct stat *st) { + struct go_ph ph[GO_MAX_PH]; + uint16_t etype = 0, machine = 0; + int n = go_read_phdrs(fd, ph, &etype, &machine); + if (n < 0) return -1; + if (machine != EM_AARCH64 || (etype != GO_ET_EXEC && etype != GO_ET_DYN)) return 0; + + int found = 0; + int64_t site_off = go_find_syscall6(fd, ph, n, &found); + if (found != 1) return 0; // not a Go of 1.19 or newer (or its function is not there once) + + // The loadable segments, by address + int order[GO_MAX_PH], nl = 0; + for (int i = 0; i < n; i++) { + if (ph[i].type != 1 /* PT_LOAD */) continue; + int k = nl++; + while (k > 0 && ph[order[k - 1]].vaddr > ph[i].vaddr) { + order[k] = order[k - 1]; + k--; + } + order[k] = i; + } + if (nl < 2) return 0; + + // The call site must be in a segment that is mapped as code + uint64_t site_v = 0; + for (int i = 0; i < nl; i++) { + const struct go_ph *s = &ph[order[i]]; + if ((s->flags & GO_PF_X) && (uint64_t)site_off >= s->offset && (uint64_t)site_off < s->offset + s->filesz) + site_v = s->vaddr + ((uint64_t)site_off - s->offset); + } + if (site_v == 0) return 0; + + struct gosys_hdr strings; + memset(&strings, 0, sizeof(strings)); + memcpy(strings.magic, "CXGOSYS1", 8); + strings.version = GOSYS_VERSION; + if (go_make_header(&strings) != 0) return 0; + + unsigned char *img = (unsigned char *)malloc(GOSYS_BLOB_SIZE); + unsigned char *have = (unsigned char *)malloc(GOSYS_BLOB_SIZE); + if (!img || !have) { + free(img); + free(have); + return -1; + } + int wfd = -1, made_writable = 0, changed = 0, rc = 0; + unsigned char word[4]; + + // An earlier run's code: it ends the segment it is in, and says what it changed + int cave_seg = -1; + struct gosys_trailer tr; + memset(&tr, 0, sizeof(tr)); + for (int i = 0; i < nl && cave_seg < 0; i++) { + const struct go_ph *s = &ph[order[i]]; + if (s->filesz < sizeof(tr) + sizeof(struct gosys_hdr)) continue; + struct gosys_trailer t; + if (pread(fd, &t, sizeof(t), (off_t)(s->offset + s->filesz - sizeof(t))) != (ssize_t)sizeof(t)) continue; + if (memcmp(t.magic, "CXGOEND1", 8) != 0 || t.total_size > s->filesz || t.total_size < sizeof(t) + sizeof(struct gosys_hdr)) continue; + tr = t; + cave_seg = order[i]; + } + + if (go_disabled()) { + if (cave_seg >= 0) { + wfd = go_open_for_write(fd, st, path, &made_writable); + if (wfd < 0) { + rc = -1; + } else { + rc = go_undo(wfd, ph, cave_seg, &tr, site_off); + changed = rc == 0; + } + } + goto done; + } + + if (cave_seg >= 0) { + uint64_t cave_off = ph[cave_seg].offset + ph[cave_seg].filesz - tr.total_size; + uint64_t cave_v = ph[cave_seg].vaddr + (cave_off - ph[cave_seg].offset); + int same = 0, site_ok = 0; + uint32_t to_thunk = 0, to_entry = 0, back = 0; + // Is it what would be put there now? (the code of an older The native userland differs) + if (tr.total_size == GOSYS_BLOB_SIZE && tr.rw_index < (uint32_t)n && ph[tr.rw_index].type == 1 && + go_branches(cave_v, site_v, &to_thunk, &to_entry, &back)) { + uint64_t scratch_v = go_align_up(ph[tr.rw_index].vaddr + tr.rw_orig_memsz, 64); + go_build_image(img, &strings, cave_v, scratch_v, to_entry, back, &tr); + same = pread(fd, have, GOSYS_BLOB_SIZE, (off_t)cave_off) == (ssize_t)GOSYS_BLOB_SIZE && + memcmp(have, img, GOSYS_BLOB_SIZE) == 0 && + ph[tr.rw_index].memsz == scratch_v - ph[tr.rw_index].vaddr + GOSYS_SCRATCH_SIZE; + } + unsigned char cur[4]; + site_ok = same && pread(fd, cur, 4, (off_t)site_off) == 4 && rd32(cur) == to_thunk; + if (same && site_ok) goto done; + + wfd = go_open_for_write(fd, st, path, &made_writable); + if (wfd < 0) { + rc = -1; + goto done; + } + if (same) { + // The code is there, only the branch is not (a patch that was cut short) + wr32(word, to_thunk); + rc = go_pwrite_all(wfd, word, 4, (uint64_t)site_off); + changed = rc == 0; + goto done; + } + // An older code, or other strings: back to the plain program, then patch again + if (go_undo(wfd, ph, cave_seg, &tr, site_off) != 0) { + rc = -1; + goto done; + } + close(wfd); + wfd = -1; + if (made_writable) fchmod(fd, st->st_mode & 07777); + free(img); + free(have); + return prepare_go_program(fd, path, st); + } + + // A place for the code: the end of a segment that is followed by padding up to the next one + uint64_t total = GOSYS_BLOB_SIZE; + int text = -1; + uint64_t cave_v = 0, cave_off = 0; + for (int pass = 0; pass < 2 && text < 0; pass++) { + for (int k = 0; k + 1 < nl && text < 0; k++) { + const struct go_ph *s = &ph[order[k]], *nx = &ph[order[k + 1]]; + if (pass == 0 ? !(s->flags & GO_PF_X) : (s->flags & GO_PF_W)) continue; + if (s->memsz != s->filesz || s->filesz == 0) continue; + uint64_t v = go_align_up(s->vaddr + s->filesz, 64); + uint64_t off = s->offset + (v - s->vaddr); + if (v + total > nx->vaddr || (nx->vaddr & 0x3fff) != 0) continue; + if (off + total > nx->offset || off + total > (uint64_t)st->st_size) continue; + // Only padding may be written over + uint64_t pad_from = s->offset + s->filesz, pad_len = off + total - pad_from; + unsigned char *pad = (unsigned char *)malloc(pad_len); + if (!pad) continue; + int zero = pread(fd, pad, pad_len, (off_t)pad_from) == (ssize_t)pad_len; + for (uint64_t i = 0; zero && i < pad_len; i++) zero = pad[i] == 0; + free(pad); + if (!zero) continue; + text = order[k]; + cave_v = v; + cave_off = off; + } + } + int rw = order[nl - 1]; + uint32_t to_thunk = 0, to_entry = 0, back = 0; + if (text < 0 || !(ph[rw].flags & GO_PF_W) || rw == text || !go_branches(cave_v, site_v, &to_thunk, &to_entry, &back)) goto done; + + uint64_t scratch_v = go_align_up(ph[rw].vaddr + ph[rw].memsz, 64); + struct gosys_trailer info; + memset(&info, 0, sizeof(info)); + info.text_orig_filesz = ph[text].filesz; + info.text_orig_flags = ph[text].flags; + info.rw_orig_memsz = ph[rw].memsz; + info.text_index = (uint32_t)text; + info.rw_index = (uint32_t)rw; + info.site_file_off = (uint64_t)site_off; + go_build_image(img, &strings, cave_v, scratch_v, to_entry, back, &info); + + wfd = go_open_for_write(fd, st, path, &made_writable); + if (wfd < 0) { + rc = -1; + goto done; + } + // The code first and the call site last: whatever is cut short leaves a program that runs + unsigned char sz[16]; + unsigned char mem[8]; + go_wr64(mem, scratch_v - ph[rw].vaddr + GOSYS_SCRATCH_SIZE); + go_wr64(sz, cave_v + GOSYS_BLOB_SIZE - ph[text].vaddr); + go_wr64(sz + 8, cave_v + GOSYS_BLOB_SIZE - ph[text].vaddr); + rc = go_pwrite_all(wfd, img, GOSYS_BLOB_SIZE, cave_off); + if (rc == 0) rc = go_pwrite_all(wfd, mem, 8, ph[rw].pos + 40); + if (rc == 0) rc = go_pwrite_all(wfd, sz, 16, ph[text].pos + 32); + if (rc == 0 && !(ph[text].flags & GO_PF_X)) { + wr32(word, ph[text].flags | GO_PF_X); + rc = go_pwrite_all(wfd, word, 4, ph[text].pos + 4); + } + if (rc == 0) { + wr32(word, to_thunk); + rc = go_pwrite_all(wfd, word, 4, (uint64_t)site_off); + } + changed = rc == 0; + +done: + free(img); + free(have); + if (wfd >= 0) { + // Keep the times: make and friends must not see a rebuilt program. + const struct timespec times[2] = { st->st_atim, st->st_mtim }; + futimens(wfd, times); + close(wfd); + } + if (made_writable) fchmod(fd, st->st_mode & 07777); + if (rc != 0) return -1; + return changed ? 1 : 0; +} + +#endif /* DDNATV_HAVE_GOSYS */ diff --git a/tools/nativeguest/hook/hardlinks.c b/tools/nativeguest/hook/hardlinks.c new file mode 100644 index 0000000000..ed8a32f7b2 --- /dev/null +++ b/tools/nativeguest/hook/hardlinks.c @@ -0,0 +1,628 @@ +// Part of libddnatv-hook.so, included by ddnatv-hook.c (one translation unit, so the +// parts share their static functions and are built with the same command as before). +// +// Hard links: link() falls back to a copy that reports the link count of a real link, in every +// process of the rootfs. + +// Emulated hard links. Android does not let apps create hard links, so link() falls back to a copy +// (ddnatv_emulate_hardlinkat). Programs that check the result through the link count, such as the +// lock files of shadow (link(file.PID, file.lock), then stat(file.PID).st_nlink == 2), and those +// that look for files linked to each other (tar, rsync -H, du, find -samefile), are shown what a +// real link would give: every copy of one emulated link reports the inode number of the original +// and the number of copies as its link count. +// +// The copies are recorded in a table that every process of the rootfs maps +// ($DDNATV_ROOT/var/lib/ddnatv/hardlinks), so "ln a b" in one process and "stat a" in the next +// agree, also after the app restarts. Copies are recorded by device and inode, so a relative name, +// an absolute name and an open descriptor of the same file all match, and with their modification +// time: a copy written to since (it no longer shares the original's content) or a new file that got +// the inode number of a removed copy (removed outside the hook, e.g. in root mode) do not match. +// +// Readers (every stat) take no lock: a sequence number that writers make odd while they change the +// table tells them to read again. Writers (link, unlink, rename) take an fcntl lock on the file. +// Without a rootfs (or when the file cannot be made) the table is private to the process and its +// children, as it was before. + +#define DDNATV_HL_MAGIC 0x6b6e6c43u // "Clnk" +#define DDNATV_HL_VERSION 1u +#define DDNATV_HL_SLOTS 8192u // a power of two +#define DDNATV_HL_MAX_USED 6144u // at most 3/4 full, so that lookups stay short +#define DDNATV_HL_EMPTY 0u +#define DDNATV_HL_USED 1u +#define DDNATV_HL_DELETED 2u + +typedef struct { + uint64_t dev, ino; // this copy + int64_t mtime_sec, mtime_nsec; // its modification time when it was linked (compared in whole + // seconds: stat() and statx() need not agree on the fraction) + uint64_t group_dev, group_ino; // the original, whose inode number every copy reports + uint32_t state; + uint32_t nlink; // copies in the group + uint32_t pad[2]; +} ddnatv_hl_slot_t; + +typedef struct { + uint32_t magic, version; + uint32_t seq; // odd while a writer changes the table + uint32_t used, deleted; + uint32_t pad[11]; + ddnatv_hl_slot_t slots[DDNATV_HL_SLOTS]; +} ddnatv_hl_table_t; + +// The file and the mapping, in whole 64 KB: a size that is a multiple of every page size +#define DDNATV_HL_MAP_SIZE ((sizeof(ddnatv_hl_table_t) + 0xffff) & ~(size_t)0xffff) + +static ddnatv_hl_table_t *g_hl_table = NULL; // set once, then never changed (until a test resets it) +static int g_hl_shared = 0; // mapped from the rootfs' file +static long g_hl_next_try = 0; // when to look for the file again (monotonic seconds) +static pthread_mutex_t g_hardlinks_lock = PTHREAD_MUTEX_INITIALIZER; + +#ifndef DDNATV_HOST_TEST +static void init_ddnatv_hook(void); +#endif + +static size_t hl_table_path(char *buf, size_t size) { +#ifndef DDNATV_HOST_TEST + // The rootfs is known once a path was rewritten; "ln a b" or "stat a" name only relative ones + init_ddnatv_hook(); +#endif + if (g_ddnatv_root_len == 0) return 0; + size_t n = ddnatv_append(buf, size, 0, g_ddnatv_root, g_ddnatv_root_len); + static const char rel[] = "/var/lib/ddnatv/hardlinks"; + n = ddnatv_append(buf, size, n, rel, sizeof(rel) - 1); + return n == g_ddnatv_root_len + sizeof(rel) - 1 ? n : 0; +} + +static long hl_now_seconds(void) { + struct timespec ts; +#ifdef CLOCK_MONOTONIC_COARSE + if (clock_gettime(CLOCK_MONOTONIC_COARSE, &ts) == 0) return (long)ts.tv_sec; +#endif + if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) return (long)ts.tv_sec; + return 0; +} + +static int hl_open_file(const char *path, int create) { + return (int)syscall(SYS_openat, AT_FDCWD, path, O_RDWR | O_CLOEXEC | (create ? O_CREAT : 0), 0600); +} + +// Takes the writers' lock on the table's file (blocking); -1 when there is no file to lock. +static int hl_lock_file(void) { + if (!g_hl_shared) return -1; + char path[PATH_MAX]; + if (!hl_table_path(path, sizeof(path))) return -1; + int fd = hl_open_file(path, 0); + if (fd < 0) return -1; + struct flock fl; + memset(&fl, 0, sizeof(fl)); + fl.l_type = F_WRLCK; + fl.l_whence = SEEK_SET; + while (fcntl(fd, F_SETLKW, &fl) != 0 && errno == EINTR) { + } + return fd; +} + +static void hl_unlock_file(int fd) { + if (fd >= 0) syscall(SYS_close, fd); // closing the file drops the lock +} + +static void hl_init_header(ddnatv_hl_table_t *t) { + memset(t, 0, sizeof(*t)); + t->version = DDNATV_HL_VERSION; + __atomic_store_n(&t->magic, DDNATV_HL_MAGIC, __ATOMIC_RELEASE); +} + +// The size of an open file. Not fstat(): this library's own fstat() looks into the table, which +// would wait for the lock that the caller holds. +static off_t hl_file_size(int fd) { + return lseek(fd, 0, SEEK_END); +} + +// Maps the table. Readers (create = 0) get NULL while no process has made the file yet, and look +// for it again a second later; writers make it, or fall back to a table of this process. Readers +// never wait for the lock (a stat() while it is held, or in a child forked while another thread +// held it, would wait forever): they find no table then. +static ddnatv_hl_table_t *hl_table(int create) { + ddnatv_hl_table_t *t = __atomic_load_n(&g_hl_table, __ATOMIC_ACQUIRE); + if (t) return t; + if (!create && __atomic_load_n(&g_hl_next_try, __ATOMIC_RELAXED) > hl_now_seconds()) return NULL; + int saved_errno = errno; + if (create) { + pthread_mutex_lock(&g_hardlinks_lock); + } else if (pthread_mutex_trylock(&g_hardlinks_lock) != 0) { + errno = saved_errno; + return NULL; + } + t = g_hl_table; + if (!t) { + char path[PATH_MAX]; + path[0] = '\0'; + int have_path = hl_table_path(path, sizeof(path)) != 0; + int fd = have_path ? hl_open_file(path, 0) : -1; + if (fd < 0 && create && path[0]) { + // var/lib/ddnatv may not exist yet + char dir[PATH_MAX]; + size_t n = ddnatv_append(dir, sizeof(dir), 0, path, strlen(path) - strlen("/hardlinks")); + if (n > 0) syscall(SYS_mkdirat, AT_FDCWD, dir, 0700); + fd = hl_open_file(path, 1); + } + if (fd >= 0) { + off_t size = hl_file_size(fd); + int ok = size >= 0; + if (ok && (size_t)size < DDNATV_HL_MAP_SIZE) { + // New (or an older, shorter format): made by a writer, under the lock + ok = 0; + if (create) { + struct flock fl; + memset(&fl, 0, sizeof(fl)); + fl.l_type = F_WRLCK; + fl.l_whence = SEEK_SET; + while (fcntl(fd, F_SETLKW, &fl) != 0 && errno == EINTR) { + } + size = hl_file_size(fd); + ok = size >= 0 && + ((size_t)size >= DDNATV_HL_MAP_SIZE || ftruncate(fd, DDNATV_HL_MAP_SIZE) == 0); + } + } + if (ok) { + void *m = mmap(NULL, DDNATV_HL_MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if (m != MAP_FAILED) { + t = (ddnatv_hl_table_t *)m; + if (t->magic != DDNATV_HL_MAGIC || t->version != DDNATV_HL_VERSION) { + if (create) { + // Still under the lock taken above when the file was new; otherwise take it + struct flock fl; + memset(&fl, 0, sizeof(fl)); + fl.l_type = F_WRLCK; + fl.l_whence = SEEK_SET; + while (fcntl(fd, F_SETLKW, &fl) != 0 && errno == EINTR) { + } + if (t->magic != DDNATV_HL_MAGIC || t->version != DDNATV_HL_VERSION) hl_init_header(t); + } else { + munmap(m, DDNATV_HL_MAP_SIZE); + t = NULL; + } + } + if (t) g_hl_shared = 1; + } + } + syscall(SYS_close, fd); // the mapping stays; also drops the lock + } + if (!t && create) { + // No rootfs file: this process and its children share a table + void *m = mmap(NULL, DDNATV_HL_MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); + if (m != MAP_FAILED) { + t = (ddnatv_hl_table_t *)m; + hl_init_header(t); + g_hl_shared = 0; + } + } + if (t) { + __atomic_store_n(&g_hl_table, t, __ATOMIC_RELEASE); + } else if (have_path) { + // No table yet: looked for again a second later (without a rootfs there is none to wait for) + __atomic_store_n(&g_hl_next_try, hl_now_seconds() + 1, __ATOMIC_RELAXED); + } + } + pthread_mutex_unlock(&g_hardlinks_lock); + errno = saved_errno; + return t; +} + +// Whether any emulated link may exist; what the stat() hooks check first. +static inline int hardlinks_maybe_any(void) { + ddnatv_hl_table_t *t = hl_table(0); + return t && __atomic_load_n(&t->used, __ATOMIC_RELAXED) > 0; +} + +static inline uint32_t hl_hash(uint64_t dev, uint64_t ino) { + uint64_t h = ino * 0x9E3779B97F4A7C15ull ^ (dev + 0x632BE59BD9B4E019ull) * 0xC2B2AE3D27D4EB4Full; + return (uint32_t)(h >> 40) & (DDNATV_HL_SLOTS - 1); +} + +// The slot of a copy, or -1. Writers only (under the locks). +static long hl_find_locked(ddnatv_hl_table_t *t, uint64_t dev, uint64_t ino) { + uint32_t i = hl_hash(dev, ino); + for (uint32_t n = 0; n < DDNATV_HL_SLOTS; n++, i = (i + 1) & (DDNATV_HL_SLOTS - 1)) { + ddnatv_hl_slot_t *s = &t->slots[i]; + if (s->state == DDNATV_HL_EMPTY) return -1; + if (s->state == DDNATV_HL_USED && s->ino == ino && s->dev == dev) return (long)i; + } + return -1; +} + +// Writers: the process' mutex, the file's lock, then the sequence number made odd. +static int hl_write_begin(ddnatv_hl_table_t *t) { + pthread_mutex_lock(&g_hardlinks_lock); + int fd = hl_lock_file(); + uint32_t seq = __atomic_load_n(&t->seq, __ATOMIC_RELAXED); + // An odd number left by a writer that died: the lock was given up with it + if (!(seq & 1)) seq++; + __atomic_store_n(&t->seq, seq, __ATOMIC_RELAXED); + __atomic_thread_fence(__ATOMIC_RELEASE); + return fd; +} + +static void hl_write_end(ddnatv_hl_table_t *t, int fd) { + __atomic_store_n(&t->seq, __atomic_load_n(&t->seq, __ATOMIC_RELAXED) + 1, __ATOMIC_RELEASE); + hl_unlock_file(fd); + pthread_mutex_unlock(&g_hardlinks_lock); +} + +// Rebuilds the table without the slots of removed copies, which make every lookup longer. +static void hl_compact_locked(ddnatv_hl_table_t *t) { + ddnatv_hl_slot_t *keep = (ddnatv_hl_slot_t *)malloc(sizeof(ddnatv_hl_slot_t) * DDNATV_HL_MAX_USED); + if (!keep) return; + uint32_t n = 0; + for (uint32_t i = 0; i < DDNATV_HL_SLOTS && n < DDNATV_HL_MAX_USED; i++) { + if (t->slots[i].state == DDNATV_HL_USED) keep[n++] = t->slots[i]; + } + memset(t->slots, 0, sizeof(t->slots)); + for (uint32_t k = 0; k < n; k++) { + uint32_t i = hl_hash(keep[k].dev, keep[k].ino); + while (t->slots[i].state != DDNATV_HL_EMPTY) i = (i + 1) & (DDNATV_HL_SLOTS - 1); + t->slots[i] = keep[k]; + } + t->used = n; + t->deleted = 0; + free(keep); +} + +static int hl_put_locked(ddnatv_hl_table_t *t, const struct stat *st, uint64_t group_dev, uint64_t group_ino) { + long i = hl_find_locked(t, st->st_dev, st->st_ino); + if (i < 0) { + if (t->used >= DDNATV_HL_MAX_USED) return -1; + if (t->used + t->deleted >= DDNATV_HL_SLOTS - DDNATV_HL_SLOTS / 8) hl_compact_locked(t); + uint32_t j = hl_hash(st->st_dev, st->st_ino); + while (t->slots[j].state == DDNATV_HL_USED) j = (j + 1) & (DDNATV_HL_SLOTS - 1); + if (t->slots[j].state == DDNATV_HL_DELETED) t->deleted--; + i = (long)j; + t->used++; + } + ddnatv_hl_slot_t *s = &t->slots[i]; + s->dev = st->st_dev; + s->ino = st->st_ino; + s->mtime_sec = st->st_mtim.tv_sec; + s->mtime_nsec = st->st_mtim.tv_nsec; + s->group_dev = group_dev; + s->group_ino = group_ino; + s->nlink = 1; + s->state = DDNATV_HL_USED; + return 0; +} + +static void hl_remove_locked(ddnatv_hl_table_t *t, long i) { + t->slots[i].state = DDNATV_HL_DELETED; + t->used--; + t->deleted++; +} + +// Gives every copy of a group its size; a group of one is a plain file again. +static void hl_update_group_locked(ddnatv_hl_table_t *t, uint64_t group_dev, uint64_t group_ino) { + uint32_t n = 0; + long last = -1; + for (uint32_t i = 0; i < DDNATV_HL_SLOTS; i++) { + ddnatv_hl_slot_t *s = &t->slots[i]; + if (s->state == DDNATV_HL_USED && s->group_ino == group_ino && s->group_dev == group_dev) { + n++; + last = (long)i; + } + } + if (n == 1) { + hl_remove_locked(t, last); + return; + } + for (uint32_t i = 0; i < DDNATV_HL_SLOTS && n > 1; i++) { + ddnatv_hl_slot_t *s = &t->slots[i]; + if (s->state == DDNATV_HL_USED && s->group_ino == group_ino && s->group_dev == group_dev) s->nlink = n; + } +} + +// Removes the copy in slot i. A group reports the inode number of one of its copies (the original); +// when that copy goes, the group reports another one's: the number of a removed file can be given to +// a new file, which must not show up as linked to the copies that are left. +static void hl_remove_copy_locked(ddnatv_hl_table_t *t, long i) { + uint64_t group_dev = t->slots[i].group_dev, group_ino = t->slots[i].group_ino; + int reported = t->slots[i].dev == group_dev && t->slots[i].ino == group_ino; + hl_remove_locked(t, i); + if (reported) { + uint64_t new_dev = 0, new_ino = 0; + int found = 0; + for (uint32_t j = 0; j < DDNATV_HL_SLOTS; j++) { + ddnatv_hl_slot_t *s = &t->slots[j]; + if (s->state != DDNATV_HL_USED || s->group_ino != group_ino || s->group_dev != group_dev) continue; + if (!found) { + new_dev = s->dev; + new_ino = s->ino; + found = 1; + } + s->group_dev = new_dev; + s->group_ino = new_ino; + } + if (found) { + group_dev = new_dev; + group_ino = new_ino; + } + } + hl_update_group_locked(t, group_dev, group_ino); +} + +// Records that the file [copy] is an emulated hard link of [old]. +static void register_emulated_hardlink(const struct stat *old, const struct stat *copy) { + ddnatv_hl_table_t *t = hl_table(1); + if (!t) return; + int saved_errno = errno; + int fd = hl_write_begin(t); + uint64_t group_dev = old->st_dev, group_ino = old->st_ino; + long i = hl_find_locked(t, old->st_dev, old->st_ino); + if (i >= 0 && t->slots[i].mtime_sec == old->st_mtim.tv_sec) { + // Linking a copy again: the new copy joins the same group + group_dev = t->slots[i].group_dev; + group_ino = t->slots[i].group_ino; + } else { + // A stale slot of an earlier file with this inode number + if (i >= 0) hl_remove_copy_locked(t, i); + hl_put_locked(t, old, group_dev, group_ino); + } + if (hl_put_locked(t, copy, group_dev, group_ino) == 0 || hl_find_locked(t, old->st_dev, old->st_ino) >= 0) { + hl_update_group_locked(t, group_dev, group_ino); + } + hl_write_end(t, fd); + errno = saved_errno; +} + +// Forgets a copy whose name was removed; the last copy of a group goes back to a plain file. +static void unregister_emulated_hardlink(dev_t dev, ino_t ino) { + ddnatv_hl_table_t *t = hl_table(0); + if (!t || __atomic_load_n(&t->used, __ATOMIC_RELAXED) == 0) return; + int saved_errno = errno; + int fd = hl_write_begin(t); + long i = hl_find_locked(t, dev, ino); + if (i >= 0) hl_remove_copy_locked(t, i); + hl_write_end(t, fd); + errno = saved_errno; +} + +// Reads the slot of a copy without a lock: 1 and [out] when there is one. +static int hl_read(ddnatv_hl_table_t *t, uint64_t dev, uint64_t ino, ddnatv_hl_slot_t *out) { + for (int tries = 0; tries < 64; tries++) { + uint32_t seq = __atomic_load_n(&t->seq, __ATOMIC_ACQUIRE); + if (seq & 1) { + sched_yield(); + continue; + } + int found = 0; + uint32_t i = hl_hash(dev, ino); + for (uint32_t n = 0; n < DDNATV_HL_SLOTS; n++, i = (i + 1) & (DDNATV_HL_SLOTS - 1)) { + const volatile ddnatv_hl_slot_t *s = &t->slots[i]; + uint32_t state = s->state; + if (state == DDNATV_HL_EMPTY) break; + if (state == DDNATV_HL_USED && s->ino == ino && s->dev == dev) { + memcpy(out, (const void *)s, sizeof(*out)); + found = 1; + break; + } + } + __atomic_thread_fence(__ATOMIC_ACQUIRE); + if (__atomic_load_n(&t->seq, __ATOMIC_RELAXED) == seq) return found; + } + return 0; // a writer that does not finish: as if there were no links +} + +// Makes a stat() result of an emulated hard link look like a real one: the inode number of the +// original and the number of copies as the link count. +static void apply_emulated_hardlink(struct stat *st) { + if (!st || !hardlinks_maybe_any()) return; + ddnatv_hl_slot_t s; + if (!hl_read(g_hl_table, st->st_dev, st->st_ino, &s)) return; + if (s.mtime_sec != (int64_t)st->st_mtim.tv_sec) return; + if (s.nlink > (uint32_t)st->st_nlink) st->st_nlink = (nlink_t)s.nlink; + st->st_ino = (ino_t)s.group_ino; +} + +// The app's own user and group, which the hook presents as root: getuid() and getpwuid() say so, +// and stat() does as well, so that a program that compares a file's owner with getuid() sees the +// files it made as its own (git's "dubious ownership", sshd's /run/sshd, tmux's socket directory, +// ssh's ~/.ssh/config). -1 until known; a test sets them. +static uid_t g_app_uid = (uid_t)-1; +static gid_t g_app_gid = (gid_t)-1; + +static void load_app_ids(void) { +#ifdef DDNATV_HOST_TEST + uid_t uid = getuid(); + gid_t gid = getgid(); +#else + // The real ones (getuid() and syscall() are the hook's own, which answer 0) + static uid_t (*orig_getuid)(void) = NULL; + static gid_t (*orig_getgid)(void) = NULL; + if (!orig_getuid) orig_getuid = (uid_t (*)(void))dlsym(RTLD_NEXT, "getuid"); + if (!orig_getgid) orig_getgid = (gid_t (*)(void))dlsym(RTLD_NEXT, "getgid"); + if (!orig_getuid || !orig_getgid) return; + uid_t uid = orig_getuid(); + gid_t gid = orig_getgid(); +#endif + __atomic_store_n(&g_app_gid, gid, __ATOMIC_RELAXED); + __atomic_store_n(&g_app_uid, uid, __ATOMIC_RELAXED); +} + +static void present_owner_as_root(uid_t *uid, gid_t *gid) { + if (__atomic_load_n(&g_app_uid, __ATOMIC_RELAXED) == (uid_t)-1) load_app_ids(); + uid_t app_uid = __atomic_load_n(&g_app_uid, __ATOMIC_RELAXED); + gid_t app_gid = __atomic_load_n(&g_app_gid, __ATOMIC_RELAXED); + if (app_uid == (uid_t)-1) return; + if (uid && *uid == app_uid) *uid = 0; + if (gid && *gid == app_gid) *gid = 0; +} + +// What the stat() family returns: emulated hard links as real ones, the app's files as root's. +static void apply_stat_emulation(struct stat *st) { + if (!st) return; + apply_emulated_hardlink(st); + present_owner_as_root(&st->st_uid, &st->st_gid); +} + +static int real_fstatat(int dirfd, const char *path, struct stat *st, int flags) { +#ifdef DDNATV_HOST_TEST + return fstatat(dirfd, path, st, flags); +#else + static int (*orig_fstatat)(int, const char *, struct stat *, int) = NULL; + if (!orig_fstatat) orig_fstatat = (int (*)(int, const char *, struct stat *, int))dlsym(RTLD_NEXT, "fstatat"); + return orig_fstatat ? orig_fstatat(dirfd, path, st, flags) : fstatat(dirfd, path, st, flags); +#endif +} + +// Before a name is removed or replaced (unlink, rename over it): whether it is an emulated hard +// link, and which. Costs nothing until the process has made one. +static int lookup_emulated_hardlink_at(int dirfd, const char *path, dev_t *dev, ino_t *ino) { + if (!path || !hardlinks_maybe_any()) return 0; + int saved_errno = errno; + struct stat st; + int found = 0; + if (real_fstatat(dirfd, path, &st, AT_SYMLINK_NOFOLLOW) == 0) { + ddnatv_hl_slot_t s; + found = hl_read(g_hl_table, st.st_dev, st.st_ino, &s); + *dev = st.st_dev; + *ino = st.st_ino; + } + errno = saved_errno; + return found; +} + +static int ddnatv_emulate_hardlinkat(int olddirfd, const char *rold, int newdirfd, const char *rnew) { + if (!rold || !rnew) { + errno = EINVAL; + return -1; + } + struct stat st; +#ifdef DDNATV_HOST_TEST + if (fstatat(olddirfd, rold, &st, 0) != 0) { + return -1; + } +#else + static int (*orig_fstatat)(int, const char *, struct stat *, int) = NULL; + if (!orig_fstatat) orig_fstatat = (int (*)(int, const char *, struct stat *, int))dlsym(RTLD_NEXT, "fstatat"); + if (orig_fstatat) { + if (orig_fstatat(olddirfd, rold, &st, 0) != 0) return -1; + } else { + if (fstatat(olddirfd, rold, &st, 0) != 0) return -1; + } +#endif + if (S_ISDIR(st.st_mode)) { + errno = EPERM; + return -1; + } + + // Clean up stale dangling symlink or stale dead-process lock file at rnew +#ifndef DDNATV_HOST_TEST + struct stat lst; + int have_lst = 0; + if (orig_fstatat) { + if (orig_fstatat(newdirfd, rnew, &lst, AT_SYMLINK_NOFOLLOW) == 0) have_lst = 1; + } else { + if (fstatat(newdirfd, rnew, &lst, AT_SYMLINK_NOFOLLOW) == 0) have_lst = 1; + } + if (have_lst) { + if (S_ISLNK(lst.st_mode)) { + unlinkat(newdirfd, rnew, 0); + } else if (strstr(rnew, ".lock")) { + static int (*orig_openat)(int, const char *, int, ...) = NULL; + if (!orig_openat) orig_openat = (int (*)(int, const char *, int, ...))dlsym(RTLD_NEXT, "openat"); + int rfd = orig_openat ? orig_openat(newdirfd, rnew, O_RDONLY | O_CLOEXEC) + : openat(newdirfd, rnew, O_RDONLY | O_CLOEXEC); + if (rfd >= 0) { + char pbuf[32] = {0}; + ssize_t pr = read(rfd, pbuf, sizeof(pbuf) - 1); + close(rfd); + if (pr > 0) { + pid_t lpid = (pid_t)atoi(pbuf); + if (lpid > 0 && kill(lpid, 0) == -1 && errno == ESRCH) { + unlinkat(newdirfd, rnew, 0); + } + } else { + unlinkat(newdirfd, rnew, 0); + } + } + } + } +#endif + +#ifdef DDNATV_HOST_TEST + int fd_new = openat(newdirfd, rnew, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, st.st_mode & 0777); + if (fd_new < 0) return -1; + int fd_old = openat(olddirfd, rold, O_RDONLY | O_CLOEXEC); +#else + static int (*orig_openat)(int, const char *, int, ...) = NULL; + if (!orig_openat) orig_openat = (int (*)(int, const char *, int, ...))dlsym(RTLD_NEXT, "openat"); + int fd_new = orig_openat ? orig_openat(newdirfd, rnew, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, st.st_mode & 0777) + : openat(newdirfd, rnew, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, st.st_mode & 0777); + if (fd_new < 0) return -1; + int fd_old = orig_openat ? orig_openat(olddirfd, rold, O_RDONLY | O_CLOEXEC) + : openat(olddirfd, rold, O_RDONLY | O_CLOEXEC); +#endif + if (fd_old < 0) { + int err = errno; + close(fd_new); + unlinkat(newdirfd, rnew, 0); + errno = err; + return -1; + } + + ssize_t sent = 0; + int copy_failed = 0; + while (sent < st.st_size) { + ssize_t s = sendfile(fd_new, fd_old, NULL, (size_t)(st.st_size - sent)); + if (s > 0) { + sent += s; + } else { + break; + } + } + if (sent < st.st_size) { + char cbuf[16384]; + ssize_t nread; + while ((nread = read(fd_old, cbuf, sizeof(cbuf))) > 0) { + ssize_t nwritten = 0; + while (nwritten < nread) { + ssize_t w = write(fd_new, cbuf + nwritten, nread - nwritten); + if (w < 0) { + copy_failed = 1; + break; + } + nwritten += w; + } + if (copy_failed) break; + } + if (nread < 0) copy_failed = 1; + } + + fchmod(fd_new, st.st_mode & 0777); + // A hard link shares the times of the original (tar, dpkg and make compare them). + const struct timespec times[2] = { st.st_atim, st.st_mtim }; + futimens(fd_new, times); + struct stat st_new; + memset(&st_new, 0, sizeof(st_new)); +#ifndef DDNATV_HOST_TEST + static int (*orig_fstat)(int, struct stat *) = NULL; + if (!orig_fstat) orig_fstat = (int (*)(int, struct stat *))dlsym(RTLD_NEXT, "fstat"); + if (orig_fstat) orig_fstat(fd_new, &st_new); + else fstat(fd_new, &st_new); +#else + fstat(fd_new, &st_new); +#endif + + close(fd_old); + close(fd_new); + + if (copy_failed) { + unlinkat(newdirfd, rnew, 0); + errno = EIO; + return -1; + } + + register_emulated_hardlink(&st, &st_new); + return 0; +} + +static inline int ddnatv_emulate_hardlink(const char *rold, const char *rnew) { + return ddnatv_emulate_hardlinkat(AT_FDCWD, rold, AT_FDCWD, rnew); +} diff --git a/tools/nativeguest/hook/identity.c b/tools/nativeguest/hook/identity.c new file mode 100644 index 0000000000..9599d7e0b2 --- /dev/null +++ b/tools/nativeguest/hook/identity.c @@ -0,0 +1,838 @@ +// Part of libddnatv-hook.so, included by ddnatv-hook.c (one translation unit, so the +// parts share their static functions and are built with the same command as before). +// +// Fake root: user and group ids, the shadow lock, and passwd/group lookups from the rootfs. + +#ifndef DDNATV_HOST_TEST + +// Fakeroot identity hooks for APT and DPKG to operate without superuser restrictions +uid_t getuid(void) { return 0; } +uid_t geteuid(void) { return 0; } +gid_t getgid(void) { return 0; } +gid_t getegid(void) { return 0; } + +int setuid(uid_t uid) { (void)uid; return 0; } +int seteuid(uid_t uid) { (void)uid; return 0; } +int setgid(gid_t gid) { (void)gid; return 0; } +int setegid(gid_t gid) { (void)gid; return 0; } +int setreuid(uid_t ruid, uid_t euid) { (void)ruid; (void)euid; return 0; } +int setregid(gid_t rgid, gid_t egid) { (void)rgid; (void)egid; return 0; } +int setresuid(uid_t ruid, uid_t euid, uid_t suid) { (void)ruid; (void)euid; (void)suid; return 0; } +int setresgid(gid_t rgid, gid_t egid, gid_t sgid) { (void)rgid; (void)egid; (void)sgid; return 0; } + +int getgroups(int size, gid_t list[]) { + if (size > 0 && list != NULL) list[0] = 0; + return 1; +} +// What programs built with _FORTIFY_SOURCE call when the buffer's size is known (the check is +// glibc's): without it the count came from the hook (1) and the list from the kernel (the app's +// groups, more than one), and sshd ended the login on "getgroups: Invalid argument" +extern void __chk_fail(void) __attribute__((__noreturn__)); +int __getgroups_chk(int size, gid_t list[], size_t listlen) { + if (size > 0 && (size_t)size * sizeof(gid_t) > listlen) __chk_fail(); + return getgroups(size, list); +} +int setgroups(size_t size, const gid_t *list) { (void)size; (void)list; return 0; } +int initgroups(const char *user, gid_t group) { (void)user; (void)group; return 0; } + +int getgrouplist(const char *user, gid_t group, gid_t *groups, int *ngroups) { + (void)user; + if (!ngroups) { + errno = EINVAL; + return -1; + } + if (*ngroups > 0 && groups) { + groups[0] = group; + *ngroups = 1; + return 1; + } + // Too small (a caller asking for the size first): -1 and the number needed, as glibc does. + *ngroups = 1; + return -1; +} + +// The session's host name, as proot's --kernel-release gave it: Steam names the device after it +int uname(struct utsname *buf) { + static int (*orig_uname)(struct utsname *) = NULL; + if (!orig_uname) orig_uname = (int (*)(struct utsname *))dlsym(RTLD_NEXT, "uname"); + int ret = orig_uname ? orig_uname(buf) : -1; + if (ret == 0 && buf && g_hostname[0]) { + ddnatv_copy_cstr(buf->nodename, sizeof(buf->nodename), g_hostname); + ddnatv_copy_cstr(buf->domainname, sizeof(buf->domainname), "localdomain"); + } + return ret; +} + +int gethostname(char *name, size_t len) { + static int (*orig_gethostname)(char *, size_t) = NULL; + if (!orig_gethostname) orig_gethostname = (int (*)(char *, size_t))dlsym(RTLD_NEXT, "gethostname"); + if (!g_hostname[0]) return orig_gethostname ? orig_gethostname(name, len) : -1; + size_t n = strlen(g_hostname) + 1; + if (!name || n > len) { + if (name && len) memcpy(name, g_hostname, len); + errno = ENAMETOOLONG; + return -1; + } + memcpy(name, g_hostname, n); + return 0; +} + +#endif /* !DDNATV_HOST_TEST */ + +static uid_t real_uid(void) { +#ifdef DDNATV_HOST_TEST + return 10000; +#else + static uid_t (*orig_getuid)(void) = NULL; + if (!orig_getuid) orig_getuid = (uid_t (*)(void))dlsym(RTLD_NEXT, "getuid"); + return orig_getuid ? orig_getuid() : 0; +#endif +} + +static gid_t real_gid(void) { +#ifdef DDNATV_HOST_TEST + return 10000; +#else + static gid_t (*orig_getgid)(void) = NULL; + if (!orig_getgid) orig_getgid = (gid_t (*)(void))dlsym(RTLD_NEXT, "getgid"); + return orig_getgid ? orig_getgid() : 0; +#endif +} + +#ifdef DDNATV_HOST_TEST +#define DDNATV_NSS_SYM(name) hook_##name +#define DDNATV_SHADOW_SYM(name) hook_##name +static int g_test_orig_nss_r_ret = 0; +static int g_test_orig_nss_r_found = 0; + +int hook_link(const char *oldpath, const char *newpath) { + int ret = link(oldpath, newpath); + if (ret != 0 && (errno == EXDEV || errno == EPERM || errno == EACCES || errno == ENOTSUP || errno == ENOSYS)) { + ret = ddnatv_emulate_hardlink(oldpath, newpath); + } + return ret; +} + +int hook_linkat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath, int flags) { + int ret = linkat(olddirfd, oldpath, newdirfd, newpath, flags); + if (ret != 0 && (errno == EXDEV || errno == EPERM || errno == EACCES || errno == ENOTSUP || errno == ENOSYS)) { + ret = ddnatv_emulate_hardlinkat(olddirfd, oldpath, newdirfd, newpath); + } + return ret; +} + +int hook_stat(const char *path, struct stat *st) { + int ret = stat(path, st); + if (ret == 0) apply_stat_emulation(st); + return ret; +} + +int hook_unlink(const char *path) { + dev_t hl_dev = 0; + ino_t hl_ino = 0; + int hardlinked = lookup_emulated_hardlink_at(AT_FDCWD, path, &hl_dev, &hl_ino); + int ret = unlink(path); + if (ret == 0 && hardlinked) unregister_emulated_hardlink(hl_dev, hl_ino); + return ret; +} +#else +#define DDNATV_NSS_SYM(name) name +#define DDNATV_SHADOW_SYM(name) name +#endif + +// Hook lckpwdf and ulckpwdf (shadow password database locking) +// In glibc, lckpwdf() attempts to open /etc/.pwd.lock directly using internal syscalls +// which fails with EROFS on Android because host /etc is mounted read-only. +static int g_pwd_lock_fd = -1; +static pthread_mutex_t g_pwd_lock_mutex = PTHREAD_MUTEX_INITIALIZER; + +int DDNATV_SHADOW_SYM(lckpwdf)(void) { +#ifndef DDNATV_HOST_TEST + init_ddnatv_hook(); + pthread_mutex_lock(&g_pwd_lock_mutex); + if (g_pwd_lock_fd != -1) { + pthread_mutex_unlock(&g_pwd_lock_mutex); + return 0; // Already locked + } + char buf[PATH_MAX]; + const char *target = rewrite_path("/etc/.pwd.lock", buf, sizeof(buf)); + static int (*orig_open)(const char *, int, ...) = NULL; + if (!orig_open) orig_open = (int (*)(const char *, int, ...))dlsym(RTLD_NEXT, "open"); + if (orig_open && target) { + g_pwd_lock_fd = orig_open(target, O_WRONLY | O_CREAT | O_CLOEXEC, 0600); + if (g_pwd_lock_fd >= 0) { + struct flock fl; + memset(&fl, 0, sizeof(fl)); + fl.l_type = F_WRLCK; + fl.l_whence = SEEK_SET; + if (fcntl(g_pwd_lock_fd, F_SETLK, &fl) != 0) { + int saved = errno; + close(g_pwd_lock_fd); + g_pwd_lock_fd = -1; + pthread_mutex_unlock(&g_pwd_lock_mutex); + errno = saved; + return -1; + } + } + } + if (g_pwd_lock_fd < 0) { + g_pwd_lock_fd = -2; // In-memory lock held + } + pthread_mutex_unlock(&g_pwd_lock_mutex); +#endif + return 0; +} + +int DDNATV_SHADOW_SYM(ulckpwdf)(void) { +#ifndef DDNATV_HOST_TEST + pthread_mutex_lock(&g_pwd_lock_mutex); + if (g_pwd_lock_fd >= 0) { + close(g_pwd_lock_fd); + } + g_pwd_lock_fd = -1; + pthread_mutex_unlock(&g_pwd_lock_mutex); +#endif + return 0; +} + + +// Group lookup helper: reads $DDNATV_ROOT/etc/group or falls back to known entries +static int ddnatv_lookup_group(const char *search_name, gid_t search_gid, int by_name, + struct group *grp, char *buf, size_t buflen) { + if (!grp || !buf || buflen < 64) { + return ERANGE; + } + + int found = 0; + char found_name[64] = {0}; + char found_pass[32] = {0}; + gid_t found_gid = 0; + char found_members[256] = {0}; + + // 1. Try reading /etc/group from The native userland rootfs +#ifndef DDNATV_HOST_TEST + char path_buf[PATH_MAX]; + const char *grp_path = rewrite_path("/etc/group", path_buf, sizeof(path_buf)); + static int (*orig_open)(const char *, int, ...) = NULL; + if (!orig_open) orig_open = (int (*)(const char *, int, ...))dlsym(RTLD_NEXT, "open"); + int fd = (orig_open && grp_path) ? orig_open(grp_path, O_RDONLY | O_CLOEXEC) : -1; + if (fd >= 0) { + char file_buf[8192]; + ssize_t n = read(fd, file_buf, sizeof(file_buf) - 1); + close(fd); + if (n > 0) { + file_buf[n] = '\0'; + char *line = file_buf; + while (*line && !found) { + char *next_line = strchr(line, '\n'); + if (next_line) { + *next_line = '\0'; + } + size_t llen = strlen(line); + if (llen > 0 && line[llen - 1] == '\r') line[llen - 1] = '\0'; + + // format: name:pass:gid:members + char *p1 = strchr(line, ':'); + if (p1) { + *p1 = '\0'; + char *p2 = strchr(p1 + 1, ':'); + if (p2) { + *p2 = '\0'; + char *p3 = strchr(p2 + 1, ':'); + if (p3) { + *p3 = '\0'; + const char *gname = line; + const char *gpass = p1 + 1; + gid_t ggid = (gid_t)strtoul(p2 + 1, NULL, 10); + const char *gmem = p3 + 1; + + if (by_name && search_name && strcmp(gname, search_name) == 0) { + snprintf(found_name, sizeof(found_name), "%s", gname); + snprintf(found_pass, sizeof(found_pass), "%s", gpass); + found_gid = ggid; + snprintf(found_members, sizeof(found_members), "%s", gmem); + found = 1; + } else if (!by_name && ggid == search_gid) { + snprintf(found_name, sizeof(found_name), "%s", gname); + snprintf(found_pass, sizeof(found_pass), "%s", gpass); + found_gid = ggid; + snprintf(found_members, sizeof(found_members), "%s", gmem); + found = 1; + } + } + } + } + if (!next_line) break; + line = next_line + 1; + } + } + } +#endif + + // 2. Fallbacks for well-known groups if absent from file + if (!found) { + if (by_name) { + if (!search_name) return ENOENT; + if (strcmp(search_name, "root") == 0 || strcmp(search_name, "ddnatv") == 0) { + snprintf(found_name, sizeof(found_name), "%s", search_name); + snprintf(found_pass, sizeof(found_pass), "x"); + found_gid = 0; + snprintf(found_members, sizeof(found_members), "%s", search_name); + found = 1; + } else if (strcmp(search_name, "_ssh") == 0) { + snprintf(found_name, sizeof(found_name), "_ssh"); + snprintf(found_pass, sizeof(found_pass), "x"); + found_gid = 100; + found = 1; + } else if (strcmp(search_name, "ssh") == 0) { + snprintf(found_name, sizeof(found_name), "ssh"); + snprintf(found_pass, sizeof(found_pass), "x"); + found_gid = 101; + found = 1; + } else if (strcmp(search_name, "nogroup") == 0 || strcmp(search_name, "nobody") == 0) { + snprintf(found_name, sizeof(found_name), "%s", search_name); + snprintf(found_pass, sizeof(found_pass), "x"); + found_gid = 65534; + found = 1; + } + } else { + if (search_gid == 0 || search_gid == real_gid()) { + snprintf(found_name, sizeof(found_name), "root"); + snprintf(found_pass, sizeof(found_pass), "x"); + found_gid = search_gid; + snprintf(found_members, sizeof(found_members), "root"); + found = 1; + } + } + } + + if (!found) { + return ENOENT; + } + + // 3. Assemble struct group into caller's buf + size_t mem_ptr_count = 1; + char *m_copy = found_members; + while (*m_copy) { + if (*m_copy == ',') mem_ptr_count++; + m_copy++; + } + if (found_members[0] != '\0') mem_ptr_count++; + + size_t mem_array_bytes = (mem_ptr_count + 1) * sizeof(char *); + mem_array_bytes = (mem_array_bytes + sizeof(void *) - 1) & ~(sizeof(void *) - 1); + + size_t name_len = strlen(found_name) + 1; + size_t pass_len = strlen(found_pass) + 1; + size_t mem_str_len = strlen(found_members) + 1; + + if (mem_array_bytes + name_len + pass_len + mem_str_len > buflen) { + return ERANGE; + } + + char **mem_array = (char **)buf; + char *str_cursor = buf + mem_array_bytes; + + grp->gr_name = str_cursor; + memcpy(str_cursor, found_name, name_len); + str_cursor += name_len; + + grp->gr_passwd = str_cursor; + memcpy(str_cursor, found_pass, pass_len); + str_cursor += pass_len; + + grp->gr_gid = found_gid; + + size_t m_idx = 0; + if (found_members[0] != '\0') { + char *m_tok = str_cursor; + memcpy(str_cursor, found_members, mem_str_len); + str_cursor += mem_str_len; + + char *curr = m_tok; + while (curr && *curr) { + char *comma = strchr(curr, ','); + if (comma) { + *comma = '\0'; + } + if (*curr) { + mem_array[m_idx++] = curr; + } + curr = comma ? comma + 1 : NULL; + } + } + mem_array[m_idx] = NULL; + grp->gr_mem = mem_array; + + return 0; +} + +// Passwd lookup helper: reads $DDNATV_ROOT/etc/passwd or falls back to known entries +static int ddnatv_lookup_passwd(const char *search_name, uid_t search_uid, int by_name, + struct passwd *pwd, char *buf, size_t buflen) { + if (!pwd || !buf || buflen < 128) { + return ERANGE; + } + + int found = 0; + char found_name[64] = {0}; + char found_pass[32] = {0}; + uid_t found_uid = 0; + gid_t found_gid = 0; + char found_gecos[64] = {0}; + char found_dir[128] = {0}; + char found_shell[64] = {0}; + + // 1. Try reading /etc/passwd from The native userland rootfs +#ifndef DDNATV_HOST_TEST + char path_buf[PATH_MAX]; + const char *pwd_path = rewrite_path("/etc/passwd", path_buf, sizeof(path_buf)); + static int (*orig_open)(const char *, int, ...) = NULL; + if (!orig_open) orig_open = (int (*)(const char *, int, ...))dlsym(RTLD_NEXT, "open"); + int fd = (orig_open && pwd_path) ? orig_open(pwd_path, O_RDONLY | O_CLOEXEC) : -1; + if (fd >= 0) { + char file_buf[8192]; + ssize_t n = read(fd, file_buf, sizeof(file_buf) - 1); + close(fd); + if (n > 0) { + file_buf[n] = '\0'; + char *line = file_buf; + while (*line && !found) { + char *next_line = strchr(line, '\n'); + if (next_line) { + *next_line = '\0'; + } + size_t llen = strlen(line); + if (llen > 0 && line[llen - 1] == '\r') line[llen - 1] = '\0'; + + // format: name:pass:uid:gid:gecos:dir:shell + char *f[7] = {0}; + char *curr = line; + int f_idx = 0; + while (curr && f_idx < 7) { + f[f_idx++] = curr; + char *col = strchr(curr, ':'); + if (col && f_idx < 7) { + *col = '\0'; + curr = col + 1; + } else { + break; + } + } + if (f_idx >= 7) { + const char *uname = f[0]; + const char *upass = f[1]; + uid_t uuid = (uid_t)strtoul(f[2], NULL, 10); + gid_t ugid = (gid_t)strtoul(f[3], NULL, 10); + const char *ugecos = f[4]; + const char *udir = f[5]; + const char *ushell = f[6]; + + if (by_name && search_name && strcmp(uname, search_name) == 0) { + snprintf(found_name, sizeof(found_name), "%s", uname); + snprintf(found_pass, sizeof(found_pass), "%s", upass); + found_uid = uuid; + found_gid = ugid; + snprintf(found_gecos, sizeof(found_gecos), "%s", ugecos); + snprintf(found_dir, sizeof(found_dir), "%s", udir); + snprintf(found_shell, sizeof(found_shell), "%s", ushell); + found = 1; + } else if (!by_name && uuid == search_uid) { + snprintf(found_name, sizeof(found_name), "%s", uname); + snprintf(found_pass, sizeof(found_pass), "%s", upass); + found_uid = uuid; + found_gid = ugid; + snprintf(found_gecos, sizeof(found_gecos), "%s", ugecos); + snprintf(found_dir, sizeof(found_dir), "%s", udir); + snprintf(found_shell, sizeof(found_shell), "%s", ushell); + found = 1; + } + } + if (!next_line) break; + line = next_line + 1; + } + } + } +#endif + + // 2. Fallbacks for well-known users if absent from file + if (!found) { + if (by_name) { + if (!search_name) return ENOENT; + if (strcmp(search_name, "root") == 0) { + snprintf(found_name, sizeof(found_name), "root"); + snprintf(found_pass, sizeof(found_pass), "x"); + found_uid = 0; + found_gid = 0; + snprintf(found_gecos, sizeof(found_gecos), "root"); + snprintf(found_dir, sizeof(found_dir), "/root"); + snprintf(found_shell, sizeof(found_shell), "/bin/bash"); + found = 1; + } else if (strcmp(search_name, "ddnatv") == 0) { + snprintf(found_name, sizeof(found_name), "ddnatv"); + snprintf(found_pass, sizeof(found_pass), "x"); + found_uid = 0; + found_gid = 0; + snprintf(found_gecos, sizeof(found_gecos), "The native userland"); + snprintf(found_dir, sizeof(found_dir), "/home/ddnatv"); + snprintf(found_shell, sizeof(found_shell), "/bin/bash"); + found = 1; + } else if (strcmp(search_name, "_ssh") == 0) { + snprintf(found_name, sizeof(found_name), "_ssh"); + snprintf(found_pass, sizeof(found_pass), "x"); + found_uid = 100; + found_gid = 100; + snprintf(found_gecos, sizeof(found_gecos), "OpenSSH daemon"); + snprintf(found_dir, sizeof(found_dir), "/run/sshd"); + snprintf(found_shell, sizeof(found_shell), "/usr/sbin/nologin"); + found = 1; + } else if (strcmp(search_name, "sshd") == 0) { + snprintf(found_name, sizeof(found_name), "sshd"); + snprintf(found_pass, sizeof(found_pass), "x"); + found_uid = 101; + found_gid = 101; + snprintf(found_gecos, sizeof(found_gecos), "Privilege-separated SSH"); + snprintf(found_dir, sizeof(found_dir), "/run/sshd"); + snprintf(found_shell, sizeof(found_shell), "/usr/sbin/nologin"); + found = 1; + } else if (strcmp(search_name, "nobody") == 0) { + snprintf(found_name, sizeof(found_name), "nobody"); + snprintf(found_pass, sizeof(found_pass), "x"); + found_uid = 65534; + found_gid = 65534; + snprintf(found_gecos, sizeof(found_gecos), "nobody"); + snprintf(found_dir, sizeof(found_dir), "/nonexistent"); + snprintf(found_shell, sizeof(found_shell), "/usr/sbin/nologin"); + found = 1; + } + } else { + if (search_uid == 0 || search_uid == real_uid()) { + snprintf(found_name, sizeof(found_name), "root"); + snprintf(found_pass, sizeof(found_pass), "x"); + found_uid = search_uid; + found_gid = 0; + snprintf(found_gecos, sizeof(found_gecos), "root"); + snprintf(found_dir, sizeof(found_dir), "/home"); + snprintf(found_shell, sizeof(found_shell), "/bin/bash"); + found = 1; + } + } + } + + if (!found) { + return ENOENT; + } + + // 3. Assemble struct passwd into caller's buf + size_t name_len = strlen(found_name) + 1; + size_t pass_len = strlen(found_pass) + 1; + size_t gecos_len = strlen(found_gecos) + 1; + size_t dir_len = strlen(found_dir) + 1; + size_t shell_len = strlen(found_shell) + 1; + + size_t total_str_bytes = name_len + pass_len + gecos_len + dir_len + shell_len; + if (total_str_bytes > buflen) { + return ERANGE; + } + + char *str_cursor = buf; + + pwd->pw_name = str_cursor; + memcpy(str_cursor, found_name, name_len); + str_cursor += name_len; + + pwd->pw_passwd = str_cursor; + memcpy(str_cursor, found_pass, pass_len); + str_cursor += pass_len; + + pwd->pw_uid = found_uid; + pwd->pw_gid = found_gid; + + pwd->pw_gecos = str_cursor; + memcpy(str_cursor, found_gecos, gecos_len); + str_cursor += gecos_len; + + pwd->pw_dir = str_cursor; + memcpy(str_cursor, found_dir, dir_len); + str_cursor += dir_len; + + pwd->pw_shell = str_cursor; + memcpy(str_cursor, found_shell, shell_len); + str_cursor += shell_len; + + return 0; +} + +#ifndef DDNATV_HOST_TEST +// The shadow database (/etc/shadow) of the rootfs. libc reads the real /etc/shadow of the machine, +// which Android does not have: pam_unix's account check for root found no entry and answered +// "Authentication failure", so chfn, chsh and `adduser --gecos` (the installation of PostgreSQL) +// failed. The fields are "name:hash:lastchange:min:max:warn:inactive:expire:flag", numbers empty +// when unset (libc stores -1 then). +static int ddnatv_lookup_shadow(const char *name, struct spwd *sp, char *buf, size_t buflen) { + if (!name || !sp || !buf) return EINVAL; + char path_buf[PATH_MAX]; + const char *path = rewrite_path("/etc/shadow", path_buf, sizeof(path_buf)); + static int (*orig_open)(const char *, int, ...) = NULL; + if (!orig_open) orig_open = (int (*)(const char *, int, ...))dlsym(RTLD_NEXT, "open"); + int fd = (orig_open && path) ? orig_open(path, O_RDONLY | O_CLOEXEC) : -1; + if (fd < 0) return ENOENT; + FILE *f = fdopen(fd, "r"); + if (!f) { + close(fd); + return ENOENT; + } + size_t name_len = strlen(name); + char line[1024]; + int err = ENOENT; + while (fgets(line, sizeof(line), f)) { + if (strncmp(line, name, name_len) != 0 || line[name_len] != ':') continue; + line[strcspn(line, "\r\n")] = '\0'; + char *fields[9] = {0}; + int n = 0; + char *cursor = line; + while (n < 9) { + fields[n++] = cursor; + char *colon = strchr(cursor, ':'); + if (!colon) break; + *colon = '\0'; + cursor = colon + 1; + } + size_t need = strlen(fields[0]) + 1 + strlen(fields[1] ? fields[1] : "") + 1; + if (n < 2 || need > buflen) { + err = n < 2 ? ENOENT : ERANGE; + break; + } + char *out = buf; + sp->sp_namp = out; + memcpy(out, fields[0], strlen(fields[0]) + 1); + out += strlen(fields[0]) + 1; + sp->sp_pwdp = out; + memcpy(out, fields[1], strlen(fields[1]) + 1); + long number[6]; + for (int i = 0; i < 6; i++) { + const char *v = (2 + i < n) ? fields[2 + i] : NULL; + number[i] = (v && v[0] != '\0') ? strtol(v, NULL, 10) : -1; + } + sp->sp_lstchg = number[0]; + sp->sp_min = number[1]; + sp->sp_max = number[2]; + sp->sp_warn = number[3]; + sp->sp_inact = number[4]; + sp->sp_expire = number[5]; + sp->sp_flag = (8 < n && fields[8][0] != '\0') ? strtoul(fields[8], NULL, 10) : (unsigned long)-1; + err = 0; + break; + } + fclose(f); + return err; +} + +int getspnam_r(const char *name, struct spwd *spbuf, char *buf, size_t buflen, struct spwd **result) { + static int (*orig_getspnam_r)(const char *, struct spwd *, char *, size_t, struct spwd **) = NULL; + if (!orig_getspnam_r) orig_getspnam_r = (int (*)(const char *, struct spwd *, char *, size_t, struct spwd **))dlsym(RTLD_NEXT, "getspnam_r"); + int ret = ENOENT; + if (orig_getspnam_r) { + ret = orig_getspnam_r(name, spbuf, buf, buflen, result); + if ((ret == 0 && result && *result) || ret == ERANGE) return ret; + } + int err = ddnatv_lookup_shadow(name, spbuf, buf, buflen); + if (err == 0) { + if (result) *result = spbuf; + return 0; + } + if (result) *result = NULL; + return err == ENOENT ? (ret != 0 ? ret : ENOENT) : err; +} + +struct spwd *getspnam(const char *name) { + static struct spwd entry; + static char entry_buf[1024]; + struct spwd *found = NULL; + int err = getspnam_r(name, &entry, entry_buf, sizeof(entry_buf), &found); + if (err != 0) { + errno = err; + return NULL; + } + return found; +} +#endif /* !DDNATV_HOST_TEST */ + +static struct group s_grp_storage; +static char s_grp_buf[2048]; +static struct passwd s_pwd_storage; +static char s_pwd_buf[2048]; + +struct group *DDNATV_NSS_SYM(getgrgid)(gid_t gid) { +#ifndef DDNATV_HOST_TEST + static struct group *(*orig_getgrgid)(gid_t) = NULL; + if (!orig_getgrgid) orig_getgrgid = (struct group *(*)(gid_t))dlsym(RTLD_NEXT, "getgrgid"); + struct group *res = orig_getgrgid ? orig_getgrgid(gid) : NULL; + if (res) return res; +#endif + + int err = ddnatv_lookup_group(NULL, gid, 0, &s_grp_storage, s_grp_buf, sizeof(s_grp_buf)); + if (err == 0) return &s_grp_storage; + return NULL; +} + +struct group *DDNATV_NSS_SYM(getgrnam)(const char *name) { +#ifndef DDNATV_HOST_TEST + static struct group *(*orig_getgrnam)(const char *) = NULL; + if (!orig_getgrnam) orig_getgrnam = (struct group *(*)(const char *))dlsym(RTLD_NEXT, "getgrnam"); + struct group *res = orig_getgrnam ? orig_getgrnam(name) : NULL; + if (res) return res; +#endif + + int err = ddnatv_lookup_group(name, 0, 1, &s_grp_storage, s_grp_buf, sizeof(s_grp_buf)); + if (err == 0) return &s_grp_storage; + return NULL; +} + +int DDNATV_NSS_SYM(getgrgid_r)(gid_t gid, struct group *grp, char *buf, size_t buflen, struct group **result) { +#ifndef DDNATV_HOST_TEST + static int (*orig_getgrgid_r)(gid_t, struct group *, char *, size_t, struct group **) = NULL; + if (!orig_getgrgid_r) orig_getgrgid_r = (int (*)(gid_t, struct group *, char *, size_t, struct group **))dlsym(RTLD_NEXT, "getgrgid_r"); + int ret = orig_getgrgid_r ? orig_getgrgid_r(gid, grp, buf, buflen, result) : ENOENT; +#else + int ret = g_test_orig_nss_r_ret; + if (result) *result = (ret == 0 && g_test_orig_nss_r_found) ? grp : NULL; +#endif + if (ret == 0 && result && *result != NULL) return 0; + if (ret != 0 && ret != ENOENT) { + if (result) *result = NULL; + return ret; + } + + int err = ddnatv_lookup_group(NULL, gid, 0, grp, buf, buflen); + if (err == 0) { + if (result) *result = grp; + return 0; + } + if (result) *result = NULL; + return (err == ERANGE) ? ERANGE : 0; +} + +int DDNATV_NSS_SYM(getgrnam_r)(const char *name, struct group *grp, char *buf, size_t buflen, struct group **result) { +#ifndef DDNATV_HOST_TEST + static int (*orig_getgrnam_r)(const char *, struct group *, char *, size_t, struct group **) = NULL; + if (!orig_getgrnam_r) orig_getgrnam_r = (int (*)(const char *, struct group *, char *, size_t, struct group **))dlsym(RTLD_NEXT, "getgrnam_r"); + int ret = orig_getgrnam_r ? orig_getgrnam_r(name, grp, buf, buflen, result) : ENOENT; +#else + int ret = g_test_orig_nss_r_ret; + if (result) *result = (ret == 0 && g_test_orig_nss_r_found) ? grp : NULL; +#endif + if (ret == 0 && result && *result != NULL) return 0; + if (ret != 0 && ret != ENOENT) { + if (result) *result = NULL; + return ret; + } + + int err = ddnatv_lookup_group(name, 0, 1, grp, buf, buflen); + if (err == 0) { + if (result) *result = grp; + return 0; + } + if (result) *result = NULL; + return (err == ERANGE) ? ERANGE : 0; +} + +// sshd's privilege-separated user is root's ids: The native userland cannot drop privileges (setuid() and +// setgid() only pretend to), and sshd's unprivileged part checks after dropping them that it cannot +// take them back, which ended it ("permanently_set_uid: was able to restore old [e]gid"). Like apt's +// sandbox user (APT::Sandbox::User "root"), it then runs as the rest does. +// +// The app's own ids are root's as well: the rootfs lists root with them for proot, and a server +// that allows "localuser:root" compares getpwnam("root") with a peer's credentials, which the hook +// reports as 0. Xwayland refused gamescope's own connection when the two disagreed. +static struct passwd *present_privsep_user_as_root(struct passwd *pwd) { + if (pwd && pwd->pw_name && strcmp(pwd->pw_name, "sshd") == 0) { + pwd->pw_uid = 0; + pwd->pw_gid = 0; + } + if (pwd && pwd->pw_uid == real_uid()) pwd->pw_uid = 0; + if (pwd && pwd->pw_gid == real_gid()) pwd->pw_gid = 0; + return pwd; +} + +struct passwd *DDNATV_NSS_SYM(getpwuid)(uid_t uid) { +#ifndef DDNATV_HOST_TEST + static struct passwd *(*orig_getpwuid)(uid_t) = NULL; + if (!orig_getpwuid) orig_getpwuid = (struct passwd *(*)(uid_t))dlsym(RTLD_NEXT, "getpwuid"); + struct passwd *res = orig_getpwuid ? orig_getpwuid(uid) : NULL; + if (res) return present_privsep_user_as_root(res); +#endif + + int err = ddnatv_lookup_passwd(NULL, uid, 0, &s_pwd_storage, s_pwd_buf, sizeof(s_pwd_buf)); + if (err == 0) return present_privsep_user_as_root(&s_pwd_storage); + return NULL; +} + +struct passwd *DDNATV_NSS_SYM(getpwnam)(const char *name) { +#ifndef DDNATV_HOST_TEST + static struct passwd *(*orig_getpwnam)(const char *) = NULL; + if (!orig_getpwnam) orig_getpwnam = (struct passwd *(*)(const char *))dlsym(RTLD_NEXT, "getpwnam"); + struct passwd *res = orig_getpwnam ? orig_getpwnam(name) : NULL; + if (res) return present_privsep_user_as_root(res); +#endif + + int err = ddnatv_lookup_passwd(name, 0, 1, &s_pwd_storage, s_pwd_buf, sizeof(s_pwd_buf)); + if (err == 0) return present_privsep_user_as_root(&s_pwd_storage); + return NULL; +} + +int DDNATV_NSS_SYM(getpwuid_r)(uid_t uid, struct passwd *pwd, char *buf, size_t buflen, struct passwd **result) { +#ifndef DDNATV_HOST_TEST + static int (*orig_getpwuid_r)(uid_t, struct passwd *, char *, size_t, struct passwd **) = NULL; + if (!orig_getpwuid_r) orig_getpwuid_r = (int (*)(uid_t, struct passwd *, char *, size_t, struct passwd **))dlsym(RTLD_NEXT, "getpwuid_r"); + int ret = orig_getpwuid_r ? orig_getpwuid_r(uid, pwd, buf, buflen, result) : ENOENT; +#else + int ret = g_test_orig_nss_r_ret; + if (result) *result = (ret == 0 && g_test_orig_nss_r_found) ? pwd : NULL; +#endif + if (ret == 0 && result && *result != NULL) { + present_privsep_user_as_root(*result); + return 0; + } + if (ret != 0 && ret != ENOENT) { + if (result) *result = NULL; + return ret; + } + + int err = ddnatv_lookup_passwd(NULL, uid, 0, pwd, buf, buflen); + if (err == 0) { + present_privsep_user_as_root(pwd); + if (result) *result = pwd; + return 0; + } + if (result) *result = NULL; + return (err == ERANGE) ? ERANGE : 0; +} + +int DDNATV_NSS_SYM(getpwnam_r)(const char *name, struct passwd *pwd, char *buf, size_t buflen, struct passwd **result) { +#ifndef DDNATV_HOST_TEST + static int (*orig_getpwnam_r)(const char *, struct passwd *, char *, size_t, struct passwd **) = NULL; + if (!orig_getpwnam_r) orig_getpwnam_r = (int (*)(const char *, struct passwd *, char *, size_t, struct passwd **))dlsym(RTLD_NEXT, "getpwnam_r"); + int ret = orig_getpwnam_r ? orig_getpwnam_r(name, pwd, buf, buflen, result) : ENOENT; +#else + int ret = g_test_orig_nss_r_ret; + if (result) *result = (ret == 0 && g_test_orig_nss_r_found) ? pwd : NULL; +#endif + if (ret == 0 && result && *result != NULL) { + present_privsep_user_as_root(*result); + return 0; + } + if (ret != 0 && ret != ENOENT) { + if (result) *result = NULL; + return ret; + } + + int err = ddnatv_lookup_passwd(name, 0, 1, pwd, buf, buflen); + if (err == 0) { + present_privsep_user_as_root(pwd); + if (result) *result = pwd; + return 0; + } + if (result) *result = NULL; + return (err == ERANGE) ? ERANGE : 0; +} diff --git a/tools/nativeguest/hook/net.c b/tools/nativeguest/hook/net.c new file mode 100644 index 0000000000..19424c5796 --- /dev/null +++ b/tools/nativeguest/hook/net.c @@ -0,0 +1,1054 @@ +// Part of libddnatv-hook.so, included by ddnatv-hook.c (one translation unit, so the +// parts share their static functions and are built with the same command as before). +// +// Name resolution: DNS packets, the network's DNS servers, the fallback resolver, getaddrinfo() +// and the resolver state, and DNS queries to loopback redirected to a real server. + +// ---- DNS packets and servers ------------------------------------------------------------- +// Pure functions (no syscalls apart from if_nametoindex), so tests/native can check them directly. + +#define DDNATV_DNS_TYPE_A 1 +#define DDNATV_DNS_TYPE_AAAA 28 +#define DDNATV_DNS_MAX_SERVERS 8 + +typedef struct { + struct sockaddr_storage addr; + socklen_t len; +} ddnatv_dns_server_t; + +// Fills `server` with the DNS server `text` (an IPv4 or IPv6 address, IPv6 optionally with a +// "%interface" scope) on port 53. Returns 0 for loopback and unspecified addresses (nothing answers +// there on Android), for a link-local IPv6 address without a scope, and for anything invalid. +static int dns_server_from_string(const char *text, ddnatv_dns_server_t *server) { + memset(server, 0, sizeof(*server)); + if (!text) return 0; + struct in_addr a4; + if (inet_pton(AF_INET, text, &a4) == 1) { + if ((ntohl(a4.s_addr) >> 24) == 127 || a4.s_addr == 0) return 0; + struct sockaddr_in *sin = (struct sockaddr_in *)&server->addr; + sin->sin_family = AF_INET; + sin->sin_port = htons(53); + sin->sin_addr = a4; + server->len = sizeof(*sin); + return 1; + } + char host[INET6_ADDRSTRLEN + IF_NAMESIZE + 2]; + if (strlen(text) >= sizeof(host)) return 0; + strcpy(host, text); + unsigned int scope = 0; + char *pct = strchr(host, '%'); + if (pct) { + *pct++ = '\0'; + if (*pct >= '0' && *pct <= '9') { + scope = (unsigned int)strtoul(pct, NULL, 10); + } else { + scope = if_nametoindex(pct); + } + } + struct in6_addr a6; + if (inet_pton(AF_INET6, host, &a6) != 1) return 0; + if (IN6_IS_ADDR_LOOPBACK(&a6) || IN6_IS_ADDR_UNSPECIFIED(&a6)) return 0; + if (IN6_IS_ADDR_V4MAPPED(&a6) && (a6.s6_addr[12] == 127 || (a6.s6_addr[12] | a6.s6_addr[13] | a6.s6_addr[14] | a6.s6_addr[15]) == 0)) return 0; + if (IN6_IS_ADDR_LINKLOCAL(&a6) && scope == 0) return 0; + struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&server->addr; + sin6->sin6_family = AF_INET6; + sin6->sin6_port = htons(53); + sin6->sin6_addr = a6; + sin6->sin6_scope_id = scope; + server->len = sizeof(*sin6); + return 1; +} + +// The "nameserver" lines of a resolv.conf, in order: up to `max` usable servers, IPv4 and IPv6. +static int parse_nameservers(const char *text, ddnatv_dns_server_t *out, int max) { + int count = 0; + const char *line = text; + while (line && *line && count < max) { + const char *next = strchr(line, '\n'); + size_t len = next ? (size_t)(next - line) : strlen(line); + char buf[256]; + if (len >= sizeof(buf)) len = sizeof(buf) - 1; + memcpy(buf, line, len); + buf[len] = '\0'; + char *p = buf; + while (*p == ' ' || *p == '\t') p++; + if (strncmp(p, "nameserver", 10) == 0 && (p[10] == ' ' || p[10] == '\t')) { + p += 10; + while (*p == ' ' || *p == '\t') p++; + char *end = p; + while (*end && *end != ' ' && *end != '\t' && *end != '\r' && *end != '#' && *end != ';') end++; + *end = '\0'; + if (dns_server_from_string(p, &out[count])) count++; + } + line = next ? next + 1 : NULL; + } + return count; +} + +static int dns_server_equal(const ddnatv_dns_server_t *a, const ddnatv_dns_server_t *b) { + return a->len == b->len && memcmp(&a->addr, &b->addr, a->len) == 0; +} + +// Builds a recursive query for one name and record type; returns its length, or -1 when the name +// is not a valid DNS name (an empty label, a label over 63 bytes, over 253 bytes in all). +static int dns_build_query(const char *name, int qtype, uint16_t txid, unsigned char *packet, size_t size) { + if (!name || !packet || size < 12) return -1; + size_t name_len = strlen(name); + if (name_len > 0 && name[name_len - 1] == '.') name_len--; // "example.com." is absolute + if (name_len == 0 || name_len > 253) return -1; + memset(packet, 0, 12); + packet[0] = (unsigned char)(txid >> 8); + packet[1] = (unsigned char)(txid & 0xff); + packet[2] = 0x01; // recursion desired + packet[5] = 0x01; // one question + size_t pos = 12; + size_t start = 0; + for (;;) { + size_t end = start; + while (end < name_len && name[end] != '.') end++; + size_t len = end - start; + if (len == 0 || len > 63 || pos + 1 + len + 1 + 4 > size) return -1; + packet[pos++] = (unsigned char)len; + memcpy(packet + pos, name + start, len); + pos += len; + if (end == name_len) break; + start = end + 1; + } + packet[pos++] = 0; + packet[pos++] = (unsigned char)(qtype >> 8); + packet[pos++] = (unsigned char)(qtype & 0xff); + packet[pos++] = 0x00; + packet[pos++] = 0x01; // class IN + return (int)pos; +} + +// Offset after the (possibly compressed) name at `off`, or -1 when it is malformed. +static int dns_skip_name(const unsigned char *msg, int n, int off) { + for (int labels = 0; labels < 128; labels++) { + if (off >= n) return -1; + unsigned char len = msg[off]; + if (len == 0) return off + 1; + if ((len & 0xc0) == 0xc0) return off + 2 <= n ? off + 2 : -1; + if (len & 0xc0) return -1; + off += 1 + len; + } + return -1; +} + +// Copies the first address of type `qtype` (4 bytes for A, 16 for AAAA) from the reply `msg` to the +// query `txid` and returns 0. Returns 1 when the reply says there is no such address (the name does +// not exist, or has no record of that type): asking other servers would only send the name to more +// of them. Returns -1 for anything that is worth asking another server about: a failure (SERVFAIL, +// REFUSED), a truncated, malformed or foreign reply. +static int dns_parse_answer(const unsigned char *msg, int n, uint16_t txid, int qtype, unsigned char *out) { + if (!msg || n < 12) return -1; + if (((msg[0] << 8) | msg[1]) != txid) return -1; + if (!(msg[2] & 0x80)) return -1; // not a reply + int rcode = msg[3] & 0x0f; + if (rcode == 3) return 1; // NXDOMAIN + if (rcode != 0) return -1; + int qdcount = (msg[4] << 8) | msg[5]; + int ancount = (msg[6] << 8) | msg[7]; + int off = 12; + for (int q = 0; q < qdcount; q++) { + off = dns_skip_name(msg, n, off); + if (off < 0 || off + 4 > n) return -1; + off += 4; + } + int want = qtype == DDNATV_DNS_TYPE_AAAA ? 16 : 4; + for (int a = 0; a < ancount; a++) { + off = dns_skip_name(msg, n, off); + if (off < 0 || off + 10 > n) return -1; + int rtype = (msg[off] << 8) | msg[off + 1]; + int rclass = (msg[off + 2] << 8) | msg[off + 3]; + int rdlen = (msg[off + 8] << 8) | msg[off + 9]; + off += 10; + if (off + rdlen > n) return -1; + if (rtype == qtype && rclass == 1 && rdlen == want) { + memcpy(out, msg + off, (size_t)want); + return 0; + } + off += rdlen; + } + return (msg[2] & 0x02) ? -1 : 1; // no such record, unless the reply was truncated +} + +// ---- Sockets bound to a port the program chose ---------------------------------------------- +// Which descriptors are TCP/UDP sockets that bind() gave a port the program asked for (not 0, +// where the kernel picks one). listen() reports only those to the app, which connects to a +// reported port to see whether it serves web pages (sockets.c). Descriptors past the bitmap +// are taken as not chosen: they are not reported. +#define DDNATV_CHOSEN_PORT_FDS 65536 +static unsigned char g_chosen_port_fds[DDNATV_CHOSEN_PORT_FDS / 8]; + +static void set_chosen_port_fd(int fd, int chosen) { + if (fd < 0 || fd >= DDNATV_CHOSEN_PORT_FDS) return; + unsigned char bit = (unsigned char)(1u << (fd & 7)); + // Written only when it changes: a page of the bitmap is copied for a forked child only then + if (chosen) { + if (!(__atomic_load_n(&g_chosen_port_fds[fd >> 3], __ATOMIC_RELAXED) & bit)) + __atomic_fetch_or(&g_chosen_port_fds[fd >> 3], bit, __ATOMIC_RELAXED); + } else if (__atomic_load_n(&g_chosen_port_fds[fd >> 3], __ATOMIC_RELAXED) & bit) { + __atomic_fetch_and(&g_chosen_port_fds[fd >> 3], (unsigned char)~bit, __ATOMIC_RELAXED); + } +} + +static int is_chosen_port_fd(int fd) { + if (fd < 0 || fd >= DDNATV_CHOSEN_PORT_FDS) return 0; + return (__atomic_load_n(&g_chosen_port_fds[fd >> 3], __ATOMIC_RELAXED) >> (fd & 7)) & 1; +} + +#ifndef DDNATV_HOST_TEST + +// DNS resolution hooking and localhost DNS redirect +#define DNS_REDIRECT_MAX 64 +static struct { + int fd; + struct sockaddr_storage orig; + socklen_t orig_len; + uint32_t active; +} g_dns_redirects[DNS_REDIRECT_MAX]; +static pthread_mutex_t g_dns_redirect_mutex = PTHREAD_MUTEX_INITIALIZER; + +static void record_dns_redirect(int fd, const struct sockaddr *orig, socklen_t orig_len) { + if (fd < 0 || !orig) return; + if (orig_len > sizeof(struct sockaddr_storage)) orig_len = sizeof(struct sockaddr_storage); + pthread_mutex_lock(&g_dns_redirect_mutex); + int target_idx = (fd >= 0 ? fd : -fd) % DNS_REDIRECT_MAX; + g_dns_redirects[target_idx].fd = fd; + memcpy(&g_dns_redirects[target_idx].orig, orig, orig_len); + g_dns_redirects[target_idx].orig_len = orig_len; + g_dns_redirects[target_idx].active = 1; + pthread_mutex_unlock(&g_dns_redirect_mutex); +} + +static void clear_dns_redirect(int fd) { + if (fd < 0) return; + int target_idx = (fd >= 0 ? fd : -fd) % DNS_REDIRECT_MAX; + if (!g_dns_redirects[target_idx].active || g_dns_redirects[target_idx].fd != fd) { + return; + } + pthread_mutex_lock(&g_dns_redirect_mutex); + if (g_dns_redirects[target_idx].active && g_dns_redirects[target_idx].fd == fd) { + g_dns_redirects[target_idx].active = 0; + g_dns_redirects[target_idx].fd = -1; + g_dns_redirects[target_idx].orig_len = 0; + } + pthread_mutex_unlock(&g_dns_redirect_mutex); +} + +int close(int fd) { + static int (*orig_close)(int) = NULL; + if (!orig_close) orig_close = (int (*)(int))dlsym(RTLD_NEXT, "close"); + // The ARM translator's memory file (privileged.c): left open, as if it were closed + if (g_kept_fd_count > 0 && is_kept_fd(fd)) return 0; + clear_dns_redirect(fd); + set_chosen_port_fd(fd, 0); + return orig_close ? orig_close(fd) : -1; +} + +int dup2(int oldfd, int newfd) { + static int (*orig_dup2)(int, int) = NULL; + if (!orig_dup2) orig_dup2 = (int (*)(int, int))dlsym(RTLD_NEXT, "dup2"); + if (oldfd != newfd) { + clear_dns_redirect(newfd); + set_chosen_port_fd(newfd, is_chosen_port_fd(oldfd)); + } + return orig_dup2 ? orig_dup2(oldfd, newfd) : -1; +} + +int dup3(int oldfd, int newfd, int flags) { + static int (*orig_dup3)(int, int, int) = NULL; + if (!orig_dup3) orig_dup3 = (int (*)(int, int, int))dlsym(RTLD_NEXT, "dup3"); + if (oldfd != newfd) { + clear_dns_redirect(newfd); + set_chosen_port_fd(newfd, is_chosen_port_fd(oldfd)); + } + return orig_dup3 ? orig_dup3(oldfd, newfd, flags) : -1; +} + +static int get_dns_redirect(int fd, struct sockaddr_storage *orig, socklen_t *orig_len) { + if (fd < 0) return 0; + int found = 0; + pthread_mutex_lock(&g_dns_redirect_mutex); + int target_idx = (fd >= 0 ? fd : -fd) % DNS_REDIRECT_MAX; + if (g_dns_redirects[target_idx].active && g_dns_redirects[target_idx].fd == fd) { + if (orig) *orig = g_dns_redirects[target_idx].orig; + if (orig_len) *orig_len = g_dns_redirects[target_idx].orig_len; + found = 1; + } + pthread_mutex_unlock(&g_dns_redirect_mutex); + return found; +} + +// A DNS query to 127.0.0.0/8 or ::1 (or ::ffff:127.x), where nothing listens on Android. +static inline int is_loopback_dns(const struct sockaddr *addr, socklen_t addrlen) { + if (!addr || addrlen < sizeof(sa_family_t)) return 0; + if (addr->sa_family == AF_INET && addrlen >= sizeof(struct sockaddr_in)) { + const struct sockaddr_in *sin = (const struct sockaddr_in *)addr; + return (sin->sin_port == htons(53) && ((ntohl(sin->sin_addr.s_addr) >> 24) == 127)); + } + if (addr->sa_family == AF_INET6 && addrlen >= sizeof(struct sockaddr_in6)) { + const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)addr; + if (sin6->sin6_port != htons(53)) return 0; + return IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr) || + (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) && sin6->sin6_addr.s6_addr[12] == 127); + } + return 0; +} + +static in_addr_t get_primary_dns(void) { + static in_addr_t cached_dns = 0; + static time_t last_check = 0; + static pthread_mutex_t dns_mutex = PTHREAD_MUTEX_INITIALIZER; + + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + time_t now = ts.tv_sec; + + // Fast path: cached value valid within 5s TTL + if (cached_dns != 0 && (now - last_check < 5)) { + return cached_dns; + } + + pthread_mutex_lock(&dns_mutex); + if (cached_dns != 0 && (now - last_check < 5)) { + in_addr_t val = cached_dns; + pthread_mutex_unlock(&dns_mutex); + return val; + } + last_check = now; + + in_addr_t new_dns = 0; + init_ddnatv_hook(); + + // 1. Check Android system property net.dns1 via Bionic libc + static int (*orig_sys_prop_get)(const char *, char *) = NULL; + static int prop_lookup_done = 0; + if (!prop_lookup_done) { + orig_sys_prop_get = (int (*)(const char *, char *))dlsym(RTLD_DEFAULT, "__system_property_get"); + prop_lookup_done = 1; + } + if (orig_sys_prop_get) { + char prop_val[256]; + if (orig_sys_prop_get("net.dns1", prop_val) > 0 && prop_val[0] != '\0') { + struct in_addr a; + if (inet_aton(prop_val, &a)) { + if ((ntohl(a.s_addr) >> 24) != 127 && a.s_addr != 0) { + new_dns = a.s_addr; + } + } + } + } + + // 2. Parse /etc/resolv.conf in The native userland rootfs + char resolv_path[PATH_MAX]; + if (g_ddnatv_root[0] != '\0') { + snprintf(resolv_path, sizeof(resolv_path), "%s/etc/resolv.conf", g_ddnatv_root); + } else { + snprintf(resolv_path, sizeof(resolv_path), "/etc/resolv.conf"); + } + + struct stat st; + if (stat(resolv_path, &st) == 0) { + if (new_dns == 0) { + FILE *f = fopen(resolv_path, "r"); + if (f) { + char line[256]; + while (fgets(line, sizeof(line), f)) { + char *p = line; + while (*p == ' ' || *p == '\t') p++; + if (strncmp(p, "nameserver", 10) == 0) { + p += 10; + while (*p == ' ' || *p == '\t') p++; + char *end = p; + while (*end && *end != ' ' && *end != '\t' && *end != '\r' && *end != '\n') end++; + *end = '\0'; + struct in_addr a; + if (inet_aton(p, &a)) { + // Only accept valid, non-loopback DNS servers + if ((ntohl(a.s_addr) >> 24) != 127 && a.s_addr != 0) { + new_dns = a.s_addr; + break; + } + } + } + } + fclose(f); + } + } + } + + if (new_dns == 0) { + new_dns = inet_addr("8.8.8.8"); + } + + cached_dns = new_dns; + pthread_mutex_unlock(&dns_mutex); + return cached_dns; +} + + +// The DNS servers in the rootfs resolv.conf (which the app writes from Android's network settings), +// read again at most every 5 seconds. +static int get_dns_servers(ddnatv_dns_server_t *out, int max) { + static ddnatv_dns_server_t cached[DDNATV_DNS_MAX_SERVERS]; + static int cached_count = 0; + static time_t last_check = 0; + static int checked = 0; + static pthread_mutex_t servers_mutex = PTHREAD_MUTEX_INITIALIZER; + + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + pthread_mutex_lock(&servers_mutex); + if (!checked || ts.tv_sec - last_check >= 5) { + checked = 1; + last_check = ts.tv_sec; + init_ddnatv_hook(); + char path[PATH_MAX]; + snprintf(path, sizeof(path), "%s/etc/resolv.conf", g_ddnatv_root[0] ? g_ddnatv_root : ""); + char text[4096]; + ssize_t n = -1; + int fd = open(path, O_RDONLY | O_CLOEXEC); + if (fd >= 0) { + n = read(fd, text, sizeof(text) - 1); + close(fd); + } + text[n > 0 ? n : 0] = '\0'; + cached_count = parse_nameservers(text, cached, DDNATV_DNS_MAX_SERVERS); + } + int count = cached_count < max ? cached_count : max; + memcpy(out, cached, (size_t)count * sizeof(cached[0])); + pthread_mutex_unlock(&servers_mutex); + return count; +} + +// Where a DNS query to a loopback address goes instead: the network's DNS server, in the family of +// the socket (an IPv6 socket gets the first IPv6 server, else the IPv4 one as ::ffff:a.b.c.d). +// Returns the length of the address written to `out`. +static socklen_t dns_redirect_target(const struct sockaddr *orig, struct sockaddr_storage *out) { + memset(out, 0, sizeof(*out)); + if (orig->sa_family == AF_INET) { + memcpy(out, orig, sizeof(struct sockaddr_in)); + ((struct sockaddr_in *)out)->sin_addr.s_addr = get_primary_dns(); + return sizeof(struct sockaddr_in); + } + struct sockaddr_in6 *dst = (struct sockaddr_in6 *)out; + memcpy(dst, orig, sizeof(*dst)); + ddnatv_dns_server_t servers[DDNATV_DNS_MAX_SERVERS]; + int n = get_dns_servers(servers, DDNATV_DNS_MAX_SERVERS); + for (int i = 0; i < n; i++) { + if (servers[i].addr.ss_family == AF_INET6) { + const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *)&servers[i].addr; + dst->sin6_addr = s6->sin6_addr; + dst->sin6_scope_id = s6->sin6_scope_id; + return sizeof(*dst); + } + } + in_addr_t v4 = get_primary_dns(); + memset(&dst->sin6_addr, 0, sizeof(dst->sin6_addr)); + dst->sin6_addr.s6_addr[10] = 0xff; + dst->sin6_addr.s6_addr[11] = 0xff; + memcpy(&dst->sin6_addr.s6_addr[12], &v4, 4); + dst->sin6_scope_id = 0; + return sizeof(*dst); +} + +// Puts the loopback address that a DNS socket was redirected from back into the source address of a +// reply (`cap` is the size of the caller's buffer), so resolvers that check the sender accept it. +static void restore_dns_source(int fd, struct sockaddr *addr, socklen_t cap, socklen_t *len) { + if (!addr || cap < sizeof(sa_family_t)) return; + in_port_t port; + if (addr->sa_family == AF_INET && cap >= sizeof(struct sockaddr_in)) { + port = ((const struct sockaddr_in *)addr)->sin_port; + } else if (addr->sa_family == AF_INET6 && cap >= sizeof(struct sockaddr_in6)) { + port = ((const struct sockaddr_in6 *)addr)->sin6_port; + } else { + return; + } + if (port != htons(53)) return; + struct sockaddr_storage orig; + socklen_t orig_len = 0; + if (!get_dns_redirect(fd, &orig, &orig_len) || orig.ss_family != addr->sa_family) return; + memcpy(addr, &orig, orig_len < cap ? orig_len : cap); + if (len) *len = orig_len; +} + +#define DDNATV_ADDRINFO_MAGIC 0x434f5254 + +// One addrinfo for an IPv4 (4 bytes) or IPv6 (16 bytes) address; freed by the freeaddrinfo hook. +static struct addrinfo *alloc_addrinfo(const char *node, int family, const void *addr, int port, int socktype, int protocol) { + struct addrinfo *ai = (struct addrinfo *)calloc(1, sizeof(struct addrinfo)); + if (!ai) return NULL; + socklen_t len = family == AF_INET6 ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in); + void *sa = calloc(1, len); + if (!sa) { + free(ai); + return NULL; + } + if (family == AF_INET6) { + struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa; + sin6->sin6_family = AF_INET6; + sin6->sin6_port = htons((uint16_t)port); + memcpy(&sin6->sin6_addr, addr, 16); + } else { + struct sockaddr_in *sin = (struct sockaddr_in *)sa; + sin->sin_family = AF_INET; + sin->sin_port = htons((uint16_t)port); + memcpy(&sin->sin_addr, addr, 4); + } + + ai->ai_flags = DDNATV_ADDRINFO_MAGIC; + ai->ai_family = family; + ai->ai_socktype = socktype ? socktype : SOCK_STREAM; + ai->ai_protocol = protocol; + ai->ai_addrlen = len; + ai->ai_addr = (struct sockaddr *)sa; + ai->ai_canonname = node ? strdup(node) : NULL; + ai->ai_next = NULL; + return ai; +} + +static uint16_t generate_dns_txid(void) { + uint16_t txid = 0; + static ssize_t (*libc_getrandom)(void *, size_t, unsigned int) = NULL; + static int getrandom_checked = 0; + if (!getrandom_checked) { + libc_getrandom = (ssize_t (*)(void *, size_t, unsigned int))dlsym(RTLD_DEFAULT, "getrandom"); + getrandom_checked = 1; + } + if (libc_getrandom) { + if (libc_getrandom(&txid, sizeof(txid), 1 /* GRND_NONBLOCK */) == sizeof(txid) && txid != 0) { + return txid; + } + } +#if defined(SYS_getrandom) || defined(__NR_getrandom) +#ifndef __NR_getrandom +#define __NR_getrandom SYS_getrandom +#endif + long r = syscall(__NR_getrandom, &txid, sizeof(txid), 1 /* GRND_NONBLOCK */); + if (r == (long)sizeof(txid) && txid != 0) { + return txid; + } +#endif + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + uint32_t val = (uint32_t)(ts.tv_nsec ^ (ts.tv_sec << 16) ^ (uintptr_t)&txid); + txid = (uint16_t)((val & 0xffff) ^ (val >> 16)); + if (txid == 0) txid = 0x5a6b; + return txid; +} + +static void add_dns_server(ddnatv_dns_server_t *list, int *count, int max, const ddnatv_dns_server_t *server) { + if (*count >= max) return; + for (int i = 0; i < *count; i++) { + if (dns_server_equal(&list[i], server)) return; + } + list[(*count)++] = *server; +} + +// Looks one address of `name` up itself, for when glibc found none: asks the network's DNS servers, +// then public ones (IPv4 first, then IPv6, for IPv6-only networks without IPv4 translation), and +// stops at the first that answers. `qtype` is DDNATV_DNS_TYPE_A (4 bytes into `out`) or +// DDNATV_DNS_TYPE_AAAA (16 bytes). `*server` is -1 to ask them in turn, or the index of the only +// server to ask; on return it holds the server that answered (-1 when none did). +static int dns_lookup(const char *name, int qtype, unsigned char *out, int *server) { + if (!name || !out) return -1; + int family = qtype == DDNATV_DNS_TYPE_AAAA ? AF_INET6 : AF_INET; + if (inet_pton(family, name, out) == 1) return 0; + + static const char *const k_public_dns[] = { + "8.8.8.8", "1.1.1.1", "9.9.9.9", "2001:4860:4860::8888", "2606:4700:4700::1111", + }; + ddnatv_dns_server_t servers[DDNATV_DNS_MAX_SERVERS + 6]; + int max = (int)(sizeof(servers) / sizeof(servers[0])); + int count = 0; + ddnatv_dns_server_t s; + // the network's own servers in resolv.conf's order first, its IPv6 ones too: the first IPv4 one + // (get_primary_dns) can be the app's public fallback, which must not see private names first + ddnatv_dns_server_t network[DDNATV_DNS_MAX_SERVERS]; + int network_count = get_dns_servers(network, DDNATV_DNS_MAX_SERVERS); + for (int i = 0; i < network_count; i++) add_dns_server(servers, &count, max, &network[i]); + char primary[INET_ADDRSTRLEN]; + in_addr_t p_dns = get_primary_dns(); // also covers net.dns1 of older Android versions + if (inet_ntop(AF_INET, &p_dns, primary, sizeof(primary)) && dns_server_from_string(primary, &s)) { + add_dns_server(servers, &count, max, &s); + } + for (size_t i = 0; i < sizeof(k_public_dns) / sizeof(k_public_dns[0]); i++) { + if (dns_server_from_string(k_public_dns[i], &s)) add_dns_server(servers, &count, max, &s); + } + + unsigned char packet[512]; + uint16_t txid = generate_dns_txid(); + int packet_len = dns_build_query(name, qtype, txid, packet, sizeof(packet)); + if (packet_len < 0) return -1; + + int only = *server; + *server = -1; + for (int i = (only >= 0 ? only : 0); i < count && (only < 0 || i == only); i++) { + int sock = socket(servers[i].addr.ss_family, SOCK_DGRAM | SOCK_CLOEXEC, 0); + if (sock < 0) continue; + + struct timeval tv; + tv.tv_sec = 0; + tv.tv_usec = 600000; // 600ms fast failover + setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); + + // A connected socket only receives datagrams from that server. + if (connect(sock, (const struct sockaddr *)&servers[i].addr, servers[i].len) != 0 || + send(sock, packet, (size_t)packet_len, 0) != packet_len) { + close(sock); + continue; + } + unsigned char resp[1500]; + ssize_t n = recv(sock, resp, sizeof(resp), 0); + close(sock); + int result = n > 0 ? dns_parse_answer(resp, (int)n, txid, qtype, out) : -1; + if (result >= 0) { + *server = i; + return result == 0 ? 0 : -1; + } + } + return -1; +} + +// Whether the network's DNS servers are all IPv6 ones, as on an IPv6-only mobile network. +static int network_dns_is_ipv6_only(void) { + ddnatv_dns_server_t servers[DDNATV_DNS_MAX_SERVERS]; + int n = get_dns_servers(servers, DDNATV_DNS_MAX_SERVERS); + for (int i = 0; i < n; i++) { + if (servers[i].addr.ss_family != AF_INET6) return 0; + } + return n > 0; +} + +// glibc reads /etc/services with an open() of its own, which the path hooks never see, so a service +// name was looked up where Android has no such file: getaddrinfo(host, "https") failed with +// EAI_SERVICE. The rootfs's file is read here when glibc finds nothing. 0, ENOENT or ERANGE. +static int ddnatv_lookup_service(const char *name, int port, const char *proto, + struct servent *out, char *buf, size_t buflen) { + char path_buf[PATH_MAX]; + const char *path = rewrite_path("/etc/services", path_buf, sizeof(path_buf)); + FILE *f = path ? fopen(path, "re") : NULL; + if (!f) return ENOENT; + char line[512]; + int result = ENOENT; + while (result == ENOENT && fgets(line, sizeof(line), f)) { + char *hash = strchr(line, '#'); + if (hash) *hash = '\0'; + char *save = NULL; + char *svc = strtok_r(line, " \t\r\n", &save); + char *pp = svc ? strtok_r(NULL, " \t\r\n", &save) : NULL; + char *slash = pp ? strchr(pp, '/') : NULL; + if (!slash) continue; + *slash = '\0'; + const char *svc_proto = slash + 1; + int svc_port = atoi(pp); + if (proto && strcmp(proto, svc_proto) != 0) continue; + char *aliases[16]; + int naliases = 0; + int matched = name ? strcmp(svc, name) == 0 : svc_port == port; + for (char *a = strtok_r(NULL, " \t\r\n", &save); a && naliases < 15; a = strtok_r(NULL, " \t\r\n", &save)) { + aliases[naliases++] = a; + if (name && strcmp(a, name) == 0) matched = 1; + } + if (!matched) continue; + // buf holds the alias pointers, then the strings + size_t need = sizeof(char *) * (size_t)(naliases + 1) + strlen(svc) + 1 + strlen(svc_proto) + 1; + for (int i = 0; i < naliases; i++) need += strlen(aliases[i]) + 1; + if (need > buflen) { result = ERANGE; break; } + char **list = (char **)(void *)buf; + char *p = buf + sizeof(char *) * (size_t)(naliases + 1); + out->s_name = strcpy(p, svc); p += strlen(svc) + 1; + out->s_proto = strcpy(p, svc_proto); p += strlen(svc_proto) + 1; + for (int i = 0; i < naliases; i++) { list[i] = strcpy(p, aliases[i]); p += strlen(aliases[i]) + 1; } + list[naliases] = NULL; + out->s_aliases = list; + out->s_port = (int)htons((uint16_t)svc_port); + result = 0; + } + fclose(f); + return result; +} + +int getservbyname_r(const char *name, const char *proto, struct servent *result_buf, char *buf, size_t buflen, + struct servent **result) { + static int (*orig)(const char *, const char *, struct servent *, char *, size_t, struct servent **) = NULL; + if (!orig) orig = (int (*)(const char *, const char *, struct servent *, char *, size_t, struct servent **))dlsym(RTLD_NEXT, "getservbyname_r"); + int ret = orig ? orig(name, proto, result_buf, buf, buflen, result) : ENOENT; + if (ret == 0 && result && *result) return 0; + if (!name || !result_buf || !buf) return ret; + int err = ddnatv_lookup_service(name, 0, proto, result_buf, buf, buflen); + if (result) *result = err == 0 ? result_buf : NULL; + return err == ERANGE ? ERANGE : 0; +} + +int getservbyport_r(int port, const char *proto, struct servent *result_buf, char *buf, size_t buflen, + struct servent **result) { + static int (*orig)(int, const char *, struct servent *, char *, size_t, struct servent **) = NULL; + if (!orig) orig = (int (*)(int, const char *, struct servent *, char *, size_t, struct servent **))dlsym(RTLD_NEXT, "getservbyport_r"); + int ret = orig ? orig(port, proto, result_buf, buf, buflen, result) : ENOENT; + if (ret == 0 && result && *result) return 0; + if (!result_buf || !buf) return ret; + int err = ddnatv_lookup_service(NULL, ntohs((uint16_t)port), proto, result_buf, buf, buflen); + if (result) *result = err == 0 ? result_buf : NULL; + return err == ERANGE ? ERANGE : 0; +} + +struct servent *getservbyname(const char *name, const char *proto) { + static __thread struct servent entry; + static __thread char buf[1024]; + struct servent *found = NULL; + return getservbyname_r(name, proto, &entry, buf, sizeof(buf), &found) == 0 ? found : NULL; +} + +struct servent *getservbyport(int port, const char *proto) { + static __thread struct servent entry; + static __thread char buf[1024]; + struct servent *found = NULL; + return getservbyport_r(port, proto, &entry, buf, sizeof(buf), &found) == 0 ? found : NULL; +} + +static int synthesize_fallback_addrinfo(const char *node, const char *service, + const struct addrinfo *hints, + struct addrinfo **res) { + if (!node || !res) return EAI_NONAME; + + int socktype = (hints && hints->ai_socktype) ? hints->ai_socktype : SOCK_STREAM; + int protocol = (hints && hints->ai_protocol) ? hints->ai_protocol + : socktype == SOCK_DGRAM ? IPPROTO_UDP : socktype == SOCK_STREAM ? IPPROTO_TCP : 0; + int family = hints ? hints->ai_family : AF_UNSPEC; + if (family != AF_UNSPEC && family != AF_INET && family != AF_INET6) return EAI_FAMILY; + + // As glibc: no service is port 0, a number is that port, a name comes from /etc/services + int port = 0; + if (service && *service) { + char *end = NULL; + errno = 0; + long p = strtol(service, &end, 10); + if (end && *end == '\0' && errno == 0) { + if (p < 0 || p > 65535) return EAI_SERVICE; + port = (int)p; + } else { + struct servent se, *found = NULL; + char se_buf[1024]; + if (getservbyname_r(service, socktype == SOCK_DGRAM ? "udp" : "tcp", &se, se_buf, sizeof(se_buf), &found) != 0 || !found) + return EAI_SERVICE; + port = ntohs((uint16_t)found->s_port); + } + } + + // The address families the caller asked for: an IPv6-only caller must not get an IPv4 address, + // and AF_UNSPEC gets both, IPv6 first on a network whose DNS servers are all IPv6 ones. The second + // lookup only asks the server that answered the first, and is skipped when none answered. + int want4 = family == AF_UNSPEC || family == AF_INET; + int want6 = family == AF_UNSPEC || family == AF_INET6; + int v6_first = want6 && (!want4 || network_dns_is_ipv6_only()); + unsigned char resolved[16]; + int server = -1; + struct addrinfo *first = NULL; + struct addrinfo *second = NULL; + if (dns_lookup(node, v6_first ? DDNATV_DNS_TYPE_AAAA : DDNATV_DNS_TYPE_A, resolved, &server) == 0) { + first = alloc_addrinfo(node, v6_first ? AF_INET6 : AF_INET, resolved, port, socktype, protocol); + } + if (want4 && want6 && server >= 0 && + dns_lookup(node, v6_first ? DDNATV_DNS_TYPE_A : DDNATV_DNS_TYPE_AAAA, resolved, &server) == 0) { + second = alloc_addrinfo(node, v6_first ? AF_INET : AF_INET6, resolved, port, socktype, protocol); + } + if (first && second) { + first->ai_next = second; + } + struct addrinfo *ai = first ? first : second; + if (ai) { + *res = ai; + return 0; + } + + return EAI_NONAME; +} + +int getaddrinfo(const char *node, const char *service, + const struct addrinfo *hints, + struct addrinfo **res) { + static int (*orig_getaddrinfo)(const char *, const char *, const struct addrinfo *, struct addrinfo **) = NULL; + if (!orig_getaddrinfo) orig_getaddrinfo = (int (*)(const char *, const char *, const struct addrinfo *, struct addrinfo **))dlsym(RTLD_NEXT, "getaddrinfo"); + + struct addrinfo mod_hints; + if (hints) { + mod_hints = *hints; + // On Android, unprivileged apps are blocked by SELinux from querying netlink routing interfaces. + // Glibc's AI_ADDRCONFIG flag causes getaddrinfo to fail or return no addresses when netlink fails. + // Clearing AI_ADDRCONFIG ensures normal, robust DNS resolution. + mod_hints.ai_flags &= ~AI_ADDRCONFIG; + } else { + memset(&mod_hints, 0, sizeof(mod_hints)); + mod_hints.ai_family = AF_UNSPEC; + mod_hints.ai_flags = 0; + } + + int ret = orig_getaddrinfo ? orig_getaddrinfo(node, service, &mod_hints, res) : EAI_FAIL; + // A numeric-only lookup must never turn into a DNS query. + if ((ret != 0 || !res || !*res) && node && !(hints && (hints->ai_flags & AI_NUMERICHOST))) { + int fallback = synthesize_fallback_addrinfo(node, service, hints, res); + // When the fallback finds nothing either, glibc's own error (EAI_AGAIN: try again later, + // EAI_NONAME: no such name) is the more precise one. + // A request the fallback rejects outright (no such service, an unsupported family) is that + // error whatever glibc's was + if (fallback == 0 || ret == 0 || fallback == EAI_SERVICE || fallback == EAI_FAMILY) ret = fallback; + } + return ret; +} + +void freeaddrinfo(struct addrinfo *res) { + static void (*orig_freeaddrinfo)(struct addrinfo *) = NULL; + if (!orig_freeaddrinfo) orig_freeaddrinfo = (void (*)(struct addrinfo *))dlsym(RTLD_NEXT, "freeaddrinfo"); + + if (!res) return; + if (res->ai_flags == DDNATV_ADDRINFO_MAGIC) { + struct addrinfo *curr = res; + while (curr) { + struct addrinfo *next = curr->ai_next; + if (curr->ai_canonname) free(curr->ai_canonname); + if (curr->ai_addr) free(curr->ai_addr); + free(curr); + curr = next; + } + return; + } + if (orig_freeaddrinfo) orig_freeaddrinfo(res); +} + +// gethostbyname() and gethostbyname2() read /etc/resolv.conf with glibc's own open(), on Android's +// /etc, so they asked no DNS server and only /etc/hosts answered. When glibc finds nothing, the +// name is looked up with getaddrinfo() above, which knows the network's servers; a name DNS does +// not have still gets no address (HOST_NOT_FOUND). +static int hostent_from_getaddrinfo(const char *name, int af, struct hostent *ret, char *buf, size_t buflen, + struct hostent **result, int *herr) { + *result = NULL; + if (!name || (af != AF_INET && af != AF_INET6)) { *herr = NO_RECOVERY; return EINVAL; } + struct addrinfo hints; + memset(&hints, 0, sizeof(hints)); + hints.ai_family = af; + hints.ai_socktype = SOCK_STREAM; + hints.ai_flags = AI_CANONNAME; + struct addrinfo *res = NULL; + int rc = getaddrinfo(name, NULL, &hints, &res); + if (rc != 0 || !res) { + *herr = (rc == EAI_NONAME || rc == EAI_NODATA || rc == EAI_ADDRFAMILY) ? HOST_NOT_FOUND + : rc == EAI_AGAIN ? TRY_AGAIN : NO_RECOVERY; + if (res) freeaddrinfo(res); + return rc == EAI_AGAIN ? EAGAIN : ENOENT; + } + size_t addr_len = af == AF_INET ? sizeof(struct in_addr) : sizeof(struct in6_addr); + int count = 0; + for (struct addrinfo *a = res; a && count < 16; a = a->ai_next) if (a->ai_family == af) count++; + const char *canon = res->ai_canonname ? res->ai_canonname : name; + size_t canon_len = strlen(canon) + 1; + size_t need = sizeof(char *) * (size_t)(count + 1) + sizeof(char *) + addr_len * (size_t)count + canon_len + sizeof(void *); + if (count == 0 || need > buflen) { + freeaddrinfo(res); + if (count == 0) { *herr = NO_DATA; return ENOENT; } + *herr = NETDB_INTERNAL; + return ERANGE; + } + // pointers first, aligned, then the addresses and the name + char *p = buf + ((sizeof(void *) - ((uintptr_t)buf % sizeof(void *))) % sizeof(void *)); + char **addr_list = (char **)p; + p += sizeof(char *) * (size_t)(count + 1); + char **aliases = (char **)p; + p += sizeof(char *); + aliases[0] = NULL; + int i = 0; + for (struct addrinfo *a = res; a && i < count; a = a->ai_next) { + if (a->ai_family != af) continue; + const void *src = af == AF_INET ? (const void *)&((struct sockaddr_in *)a->ai_addr)->sin_addr + : (const void *)&((struct sockaddr_in6 *)a->ai_addr)->sin6_addr; + memcpy(p, src, addr_len); + addr_list[i++] = p; + p += addr_len; + } + addr_list[i] = NULL; + memcpy(p, canon, canon_len); + ret->h_name = p; + ret->h_aliases = aliases; + ret->h_addrtype = af; + ret->h_length = (int)addr_len; + ret->h_addr_list = addr_list; + freeaddrinfo(res); + *result = ret; + *herr = 0; + return 0; +} + +int gethostbyname2_r(const char *name, int af, struct hostent *ret, char *buf, size_t buflen, + struct hostent **result, int *h_errnop) { + static int (*orig)(const char *, int, struct hostent *, char *, size_t, struct hostent **, int *) = NULL; + if (!orig) orig = (int (*)(const char *, int, struct hostent *, char *, size_t, struct hostent **, int *))dlsym(RTLD_NEXT, "gethostbyname2_r"); + int herr = 0; + int rc = orig ? orig(name, af, ret, buf, buflen, result, &herr) : ENOSYS; + if (rc == 0 && result && *result) { if (h_errnop) *h_errnop = 0; return 0; } + if (rc == ERANGE) { if (h_errnop) *h_errnop = herr; return rc; } + int fallback_herr = 0; + struct hostent *found = NULL; + rc = hostent_from_getaddrinfo(name, af, ret, buf, buflen, &found, &fallback_herr); + if (result) *result = found; + if (h_errnop) *h_errnop = fallback_herr; + return rc; +} + +int gethostbyname_r(const char *name, struct hostent *ret, char *buf, size_t buflen, + struct hostent **result, int *h_errnop) { + return gethostbyname2_r(name, AF_INET, ret, buf, buflen, result, h_errnop); +} + +static __thread struct hostent t_hostent; +static __thread char t_hostent_buf[4096]; + +struct hostent *gethostbyname2(const char *name, int af) { + struct hostent *result = NULL; + int herr = 0; + gethostbyname2_r(name, af, &t_hostent, t_hostent_buf, sizeof(t_hostent_buf), &result, &herr); + h_errno = result ? 0 : herr; + return result; +} + +struct hostent *gethostbyname(const char *name) { + return gethostbyname2(name, AF_INET); +} + +// Whether a resolver state names a server other than loopback. glibc keeps IPv6 servers in the +// extension (their nsaddr_list entry has family 0), so an IPv6-only resolv.conf counts too. +// The network's own servers, IPv6 ones too (an IPv6-only network has no IPv4 server to ask), for a +// state glibc left with none it can use: it read no /etc/resolv.conf. glibc frees the IPv6 entries. +static void fill_fallback_servers(res_state statp) { + ddnatv_dns_server_t servers[DDNATV_DNS_MAX_SERVERS]; + int n = get_dns_servers(servers, DDNATV_DNS_MAX_SERVERS); + for (int i = 0; i < MAXNS; i++) { + free(statp->_u._ext.nsaddrs[i]); + statp->_u._ext.nsaddrs[i] = NULL; + } + int count = 0; + for (int i = 0; i < n && count < MAXNS; i++) { + if (servers[i].addr.ss_family == AF_INET) { + memcpy(&statp->nsaddr_list[count], &servers[i].addr, sizeof(struct sockaddr_in)); + statp->nsaddr_list[count].sin_port = htons(53); + statp->_u._ext.nsaddrs[count] = NULL; + count++; + } else if (servers[i].addr.ss_family == AF_INET6) { + struct sockaddr_in6 *six = malloc(sizeof(*six)); + if (!six) continue; + memcpy(six, &servers[i].addr, sizeof(*six)); + six->sin6_port = htons(53); + memset(&statp->nsaddr_list[count], 0, sizeof(statp->nsaddr_list[count])); + statp->_u._ext.nsaddrs[count] = six; + count++; + } + } + if (count == 0) { + statp->nsaddr_list[0].sin_family = AF_INET; + statp->nsaddr_list[0].sin_port = htons(53); + statp->nsaddr_list[0].sin_addr.s_addr = get_primary_dns(); + statp->nsaddr_list[1].sin_family = AF_INET; + statp->nsaddr_list[1].sin_port = htons(53); + inet_pton(AF_INET, "8.8.8.8", &statp->nsaddr_list[1].sin_addr); + count = 2; + } + statp->nscount = count; +} + +static int res_state_has_usable_server(res_state statp) { + for (int i = 0; i < statp->nscount && i < MAXNS; i++) { + if (statp->nsaddr_list[i].sin_family == AF_INET) { + in_addr_t a = statp->nsaddr_list[i].sin_addr.s_addr; + if ((ntohl(a) >> 24) != 127 && a != 0) return 1; + } else if (statp->_u._ext.nsaddrs[i] != NULL) { + const struct in6_addr *a6 = &statp->_u._ext.nsaddrs[i]->sin6_addr; + if (!IN6_IS_ADDR_LOOPBACK(a6) && !IN6_IS_ADDR_UNSPECIFIED(a6)) return 1; + } + } + return 0; +} + +int res_init(void) { + static int (*orig_res_init)(void) = NULL; + // glibc's symbols are __res_init and __res_ninit (resolv.h makes the names macros): looked up as + // res_init and res_ninit they were never found, the state stayed without RES_INIT, and Chromium + // (Steam's store) failed to read a DNS configuration + if (!orig_res_init) orig_res_init = (int (*)(void))dlsym(RTLD_NEXT, "__res_init"); + if (!orig_res_init) orig_res_init = (int (*)(void))dlsym(RTLD_NEXT, "res_init"); + int ret = orig_res_init ? orig_res_init() : 0; + res_state statp = __res_state(); + if (statp) { + if (!res_state_has_usable_server(statp)) fill_fallback_servers(statp); + statp->retrans = 1; + statp->retry = 2; + } + return ret; +} + +int res_ninit(res_state statp) { + static int (*orig_res_ninit)(res_state) = NULL; + if (!orig_res_ninit) orig_res_ninit = (int (*)(res_state))dlsym(RTLD_NEXT, "__res_ninit"); + if (!orig_res_ninit) orig_res_ninit = (int (*)(res_state))dlsym(RTLD_NEXT, "res_ninit"); + if (statp) { + memset(statp, 0, sizeof(*statp)); + statp->_vcsock = -1; + for (int i = 0; i < MAXNS; i++) { + statp->_u._ext.nssocks[i] = -1; + } + } + int ret = orig_res_ninit ? orig_res_ninit(statp) : 0; + if (statp) { + if (!res_state_has_usable_server(statp)) fill_fallback_servers(statp); + statp->retrans = 1; + statp->retry = 2; + } + return ret; +} + +int res_nquery(res_state statp, const char *dname, int class, int type, + unsigned char *answer, int anslen) { + static int (*orig_res_nquery)(res_state, const char *, int, int, unsigned char *, int) = NULL; + if (!orig_res_nquery) orig_res_nquery = (int (*)(res_state, const char *, int, int, unsigned char *, int))dlsym(RTLD_NEXT, "res_nquery"); + // DNS SRV queries are optional for APT mirrors and frequently cause delays or issues + if (type == 33 /* T_SRV */) { + h_errno = NO_DATA; + return -1; + } + return orig_res_nquery ? orig_res_nquery(statp, dname, class, type, answer, anslen) : -1; +} + +int res_query(const char *dname, int class, int type, + unsigned char *answer, int anslen) { + static int (*orig_res_query)(const char *, int, int, unsigned char *, int) = NULL; + if (!orig_res_query) orig_res_query = (int (*)(const char *, int, int, unsigned char *, int))dlsym(RTLD_NEXT, "res_query"); + if (type == 33 /* T_SRV */) { + h_errno = NO_DATA; + return -1; + } + return orig_res_query ? orig_res_query(dname, class, type, answer, anslen) : -1; +} + +int res_nsearch(res_state statp, const char *dname, int class, int type, + unsigned char *answer, int anslen) { + static int (*orig_res_nsearch)(res_state, const char *, int, int, unsigned char *, int) = NULL; + if (!orig_res_nsearch) orig_res_nsearch = (int (*)(res_state, const char *, int, int, unsigned char *, int))dlsym(RTLD_NEXT, "res_nsearch"); + if (type == 33 /* T_SRV */) { + h_errno = NO_DATA; + return -1; + } + return orig_res_nsearch ? orig_res_nsearch(statp, dname, class, type, answer, anslen) : -1; +} + +int res_search(const char *dname, int class, int type, + unsigned char *answer, int anslen) { + static int (*orig_res_search)(const char *, int, int, unsigned char *, int) = NULL; + if (!orig_res_search) orig_res_search = (int (*)(const char *, int, int, unsigned char *, int))dlsym(RTLD_NEXT, "res_search"); + if (type == 33 /* T_SRV */) { + h_errno = NO_DATA; + return -1; + } + return orig_res_search ? orig_res_search(dname, class, type, answer, anslen) : -1; +} + +#endif /* !DDNATV_HOST_TEST */ diff --git a/tools/nativeguest/hook/paths.c b/tools/nativeguest/hook/paths.c new file mode 100644 index 0000000000..4c1340b339 --- /dev/null +++ b/tools/nativeguest/hook/paths.c @@ -0,0 +1,697 @@ +// Part of libddnatv-hook.so, included by ddnatv-hook.c (one translation unit, so the +// parts share their static functions and are built with the same command as before). +// +// Path translation: Ubuntu paths (/usr, /etc, ...) mapped into the rootfs, Android's own mounts +// left alone, and the library directories of the rootfs. + +// ---- Path translation ------------------------------------------------------------------- +// Pure string functions (no syscalls), so tests/native can check them directly. They run on +// every file-system call, so they avoid snprintf and compute the rootfs prefix length once. + +static char g_ddnatv_root[PATH_MAX] = {0}; +static size_t g_ddnatv_root_len = 0; + +static void ddnatv_set_root(const char *root, size_t len) { + if (!root) len = 0; + if (len > sizeof(g_ddnatv_root) - 1) len = sizeof(g_ddnatv_root) - 1; + if (len > 0) memcpy(g_ddnatv_root, root, len); + g_ddnatv_root[len] = '\0'; + g_ddnatv_root_len = len; +} + +// Mounts that proot used to provide: "src:dst" pairs from DDNATV_BINDS ('|' separated), longest dst first. +// A path under dst is read from src; src equal to dst keeps a host path that would otherwise be +// looked up in the rootfs. +struct ddnatv_bind { + char *src, *dst; + size_t src_len, dst_len; + int identity; + int order; // position in DDNATV_BINDS: the earlier of two mounts of one folder is its guest path +}; +// as many as the session has: a fixed table dropped what came after its 128th entry +static struct ddnatv_bind *g_binds = NULL; +static int g_nbinds = 0; +static char *g_binds_raw = NULL; +// The session's host name (DDNATV_HOSTNAME), what proot put in uname(): Android's kernel says "localhost" +static char g_hostname[65]; +// A Flatpak sandbox's: the session's rootfs (DDNATV_LINK_ROOT), whose hook stored absolute guest +// link targets as paths in it, as the kernel follows them there; in the sandbox they are guest paths +static char g_link_root[PATH_MAX]; +static size_t g_link_root_len = 0; + +static int ddnatv_bind_longer_first(const void *a, const void *b) { + const struct ddnatv_bind *x = a, *y = b; + return (y->dst_len > x->dst_len) - (y->dst_len < x->dst_len); +} + +static void ddnatv_load_binds(const char *spec) { + if (!spec || !spec[0]) return; + char *raw = strdup(spec); + char *copy = strdup(spec); + if (!raw || !copy) { free(raw); free(copy); return; } + char *save = NULL; + size_t count = 1; + for (const char *c = spec; *c; c++) count += *c == '|'; + g_binds = calloc(count, sizeof(*g_binds)); + if (!g_binds) { free(raw); free(copy); return; } + for (char *entry = strtok_r(copy, "|", &save); entry && (size_t)g_nbinds < count; entry = strtok_r(NULL, "|", &save)) { + char *colon = strchr(entry, ':'); + const char *src = entry; + const char *dst = colon ? colon + 1 : entry; + if (colon) *colon = '\0'; + if (src[0] != '/' || dst[0] != '/' || strcmp(dst, "/") == 0) continue; + struct ddnatv_bind *b = &g_binds[g_nbinds++]; + b->src = strdup(src); + b->dst = strdup(dst); + b->src_len = strlen(src); + b->dst_len = strlen(dst); + while (b->dst_len > 1 && b->dst[b->dst_len - 1] == '/') b->dst[--b->dst_len] = '\0'; + while (b->src_len > 1 && b->src[b->src_len - 1] == '/') b->src[--b->src_len] = '\0'; + b->identity = strcmp(b->src, b->dst) == 0; + b->order = g_nbinds - 1; + } + free(copy); + qsort(g_binds, (size_t)g_nbinds, sizeof(g_binds[0]), ddnatv_bind_longer_first); + g_binds_raw = raw; +} + +// Appends s[0..len) to out[0..used) and NUL-terminates, truncating like snprintf. +// Returns the new length. +static size_t ddnatv_append(char *out, size_t outsz, size_t used, const char *s, size_t len) { + if (outsz == 0) return 0; + size_t room = used < outsz - 1 ? outsz - 1 - used : 0; + if (len > room) len = room; + if (len > 0) memcpy(out + used, s, len); + out[used + len] = '\0'; + return used + len; +} + +// Copies the string src into out[0..outsz), truncated and NUL-terminated as strncpy() followed by +// out[outsz - 1] = '\0' leaves it, but writes only the bytes of the string: strncpy() fills the rest +// of a PATH_MAX buffer with zeros, 4 KB of writes for a path of 30 characters, and in a child that a +// shell has just forked every page written is a copy-on-write fault. +static size_t ddnatv_copy_cstr(char *out, size_t outsz, const char *src) { + if (outsz == 0) return 0; + size_t len = strnlen(src, outsz - 1); + memcpy(out, src, len); + out[len] = '\0'; + return len; +} + +static inline int is_path_prefix(const char *path, const char *prefix, size_t prefix_len) { + if (strncmp(path, prefix, prefix_len) != 0) return 0; + return path[prefix_len] == '/' || path[prefix_len] == '\0'; +} + +static inline const char *get_rootfs_subpath(const char *target) { + if (!target || g_ddnatv_root_len == 0) return NULL; + if (strncmp(target, g_ddnatv_root, g_ddnatv_root_len) == 0 && + (target[g_ddnatv_root_len] == '/' || target[g_ddnatv_root_len] == '\0')) { + return target + g_ddnatv_root_len; + } + return NULL; +} + +// A guest path that a session mount puts outside the rootfs (/run/droiddeck, a game library): a +// program or script there is the guest's, and a script's "#!/bin/sh" is the rootfs shell. The +// kernel ran them with Android's /system/bin/sh, which found no /usr/bin/kwin_wayland. +static int is_mapped_guest_path(const char *path) { + if (!path || path[0] != '/') return 0; + for (int i = 0; i < g_nbinds; i++) { + const struct ddnatv_bind *b = &g_binds[i]; + if (b->identity) continue; + if (is_path_prefix(path, b->dst, b->dst_len)) return 1; + // its real place too: a relative program is made absolute from the real working directory + if (is_path_prefix(path, b->src, b->src_len) && get_rootfs_subpath(b->src) == NULL) return 1; + } + return 0; +} + +// Android's own mounts and files, which must never be redirected into the rootfs. +// `path` starts with '/'; a prefix can only match when its second character does. +static int is_android_host_path(const char *path) { + switch (path[1]) { + case 'a': return is_path_prefix(path, "/apex", 5); + case 'c': return is_path_prefix(path, "/config", 7); + case 'd': return is_path_prefix(path, "/dev", 4) || is_path_prefix(path, "/d", 2) || + is_path_prefix(path, "/data", 5); + case 'l': return is_path_prefix(path, "/linkerconfig", 13); + case 'o': return is_path_prefix(path, "/odm", 4) || is_path_prefix(path, "/oem", 4); + case 'p': return is_path_prefix(path, "/proc", 5) || is_path_prefix(path, "/product", 8); + case 's': return strcmp(path, "/sbin/su") == 0 || strcmp(path, "/su") == 0 || + is_path_prefix(path, "/sys", 4) || is_path_prefix(path, "/system", 7) || + is_path_prefix(path, "/system_ext", 11) || is_path_prefix(path, "/sdcard", 7) || + is_path_prefix(path, "/storage", 8); + case 'v': return is_path_prefix(path, "/vendor", 7); + default: return 0; + } +} + +static const char *rewrite_path_in_root(const char *path, char *buffer, size_t bufsize); + +#if defined(__aarch64__) +static long raw_readlinkat(int dirfd, const char *path, char *buf, size_t size) { + register long x8 __asm__("x8") = 78; // readlinkat, made directly: the hook's own readlink would map the path again + register long x0 __asm__("x0") = dirfd; + register long x1 __asm__("x1") = (long)path; + register long x2 __asm__("x2") = (long)buf; + register long x3 __asm__("x3") = (long)size; + __asm__ volatile("svc #0" : "+r"(x0) : "r"(x8), "r"(x1), "r"(x2), "r"(x3) : "memory"); + return x0; +} +#endif + +static __thread int t_bind_link_depth = 0; + +// A mount stand-in can hold symbolic links with absolute guest targets (/sys/class/hidraw/hidraw16 -> +// /sys/devices/droiddeck/...): proot follows them inside the guest, the kernel on Android's own /sys. +// A link met on the way to a deeper path is replaced by its target, mapped again; the last component is +// left as it is (lstat() and readlink() must see the link). +static const char *resolve_bind_links(const char *host, size_t src_len, char *buffer, size_t bufsize) { +#if defined(__aarch64__) + size_t len = strlen(host); + if (len >= PATH_MAX || t_bind_link_depth >= 8) return host; + for (size_t i = src_len + 1; i < len; i++) { + if (host[i] != '/') continue; + char prefix[PATH_MAX]; + memcpy(prefix, host, i); + prefix[i] = '\0'; + char target[PATH_MAX]; + long n = raw_readlinkat(AT_FDCWD, prefix, target, sizeof(target) - 1); + if (n <= 0) continue; + target[n] = '\0'; + if (target[0] != '/') continue; + char guest[PATH_MAX]; + size_t used = ddnatv_append(guest, sizeof(guest), 0, target, (size_t)n); + ddnatv_append(guest, sizeof(guest), used, host + i, len - i); + t_bind_link_depth++; + const char *mapped = rewrite_path_in_root(guest, buffer, bufsize); + if (mapped != buffer) ddnatv_copy_cstr(buffer, bufsize, mapped); + t_bind_link_depth--; + return buffer; + } +#else + (void)src_len; (void)bufsize; +#endif + return host == buffer ? buffer : host; +} + +// Wine opens every file through its prefix's drive link (dosdevices/z: -> /), so the link must be +// followed in the guest's view even where the prefix is plain rootfs: the kernel would look for the +// rest of the path on Android's own tree. Only that component is checked, not each one of every path. +static const char *resolve_drive_links(const char *host, char *buffer, size_t bufsize) { + const char *devices = strstr(host, "/dosdevices/"); + if (!devices) return host; + return resolve_bind_links(host, (size_t)(devices - host) + 11, buffer, bufsize); +} + +// Maps an absolute Ubuntu path into the rootfs. Returns `path` (possibly advanced past +// redundant leading "/" and "/." components) when it needs no change, else `buffer`. +static const char *rewrite_path_in_root(const char *path, char *buffer, size_t bufsize) { + if (!path) return NULL; + if (path[0] != '/' || g_ddnatv_root_len == 0) return path; + + // Strip multiple redundant leading slashes: //foo -> /foo + while (path[0] == '/' && path[1] == '/') { + path++; + } + + // Strip leading /./: /./boot -> /boot + while (path[0] == '/' && path[1] == '.' && (path[2] == '/' || path[2] == '\0')) { + if (path[2] == '/') { + path += 2; + } else { + path = "/"; + break; + } + } + + // Root directory + if (path[1] == '\0') { + ddnatv_append(buffer, bufsize, 0, g_ddnatv_root, g_ddnatv_root_len); + return buffer; + } + + for (int i = 0; i < g_nbinds; i++) { + const struct ddnatv_bind *b = &g_binds[i]; + if (path[1] == b->dst[1] && is_path_prefix(path, b->dst, b->dst_len)) { + if (b->identity) return path; + size_t n = ddnatv_append(buffer, bufsize, 0, b->src, b->src_len); + ddnatv_append(buffer, bufsize, n, path + b->dst_len, strlen(path + b->dst_len)); + return resolve_bind_links(buffer, b->src_len, buffer, bufsize); + } + } + + // Already inside The native userland rootfs + if (get_rootfs_subpath(path) != NULL) { + return resolve_drive_links(path, buffer, bufsize); + } + + // The real place of a mounted folder, which getcwd() and /proc/self/fd links answer for it: Steam + // made a game's path from one on the SD card library and found no sekiro.exe in the rootfs + for (int i = 0; i < g_nbinds; i++) { + const struct ddnatv_bind *b = &g_binds[i]; + if (!b->identity && path[1] == b->src[1] && is_path_prefix(path, b->src, b->src_len)) + return resolve_drive_links(path, buffer, bufsize); + } + + // POSIX shared memory (/dev/shm) redirection to The native userland rootfs tmp/shm + if (strncmp(path, "/dev/shm", 8) == 0 && (path[8] == '/' || path[8] == '\0')) { + size_t n = ddnatv_append(buffer, bufsize, 0, g_ddnatv_root, g_ddnatv_root_len); + n = ddnatv_append(buffer, bufsize, n, "/tmp/shm", 8); + ddnatv_append(buffer, bufsize, n, path + 8, strlen(path + 8)); + return buffer; + } + + // Real host Android kernel & system mounts + if (is_android_host_path(path)) { + return path; + } + + // Any other absolute path belongs to The native userland rootfs (/boot, /media, /snap, /etc, /usr, ...) + size_t n = ddnatv_append(buffer, bufsize, 0, g_ddnatv_root, g_ddnatv_root_len); + ddnatv_append(buffer, bufsize, n, path, strlen(path)); + return resolve_drive_links(buffer, buffer, bufsize); +} + +// A link target the session's hook stored as a path in its rootfs, given back as the guest path it +// stands for (see g_link_root). Rewrites target[0..len) in place; returns the new length. +static size_t link_root_guest_target(char *target, size_t len) { + if (!g_link_root_len || len < g_link_root_len || memcmp(target, g_link_root, g_link_root_len) != 0) return len; + if (len == g_link_root_len) { target[0] = '/'; target[1] = '\0'; return 1; } + if (target[g_link_root_len] != '/') return len; + memmove(target, target + g_link_root_len, len - g_link_root_len + 1); + return len - g_link_root_len; +} + +// The guest path a symbolic link at absolute guest path `path` leads to, followed in the guest's +// view: the kernel resolves a relative target next to the link's real place, and in a mount that is +// elsewhere (Flatpak's /run/user/0/wayland-0 -> ../../flatpak/wayland-0, where /run/flatpak/wayland-0 +// is another mount). Returns `path` when it is no link. +static const char *follow_guest_link(const char *path, char *out, size_t outsz) { +#if defined(__aarch64__) + const char *cur = path; + for (int depth = 0; depth < 8; depth++) { + char host_buf[PATH_MAX]; + const char *host = rewrite_path_in_root(cur, host_buf, sizeof(host_buf)); + // /dev/stdin and /dev/fd are mounts of /proc/self/fd, whose links ("pipe:[123]") only the kernel opens + if (strncmp(host, "/proc/", 6) == 0) return cur; + char target[PATH_MAX]; + long n = raw_readlinkat(AT_FDCWD, host, target, sizeof(target) - 1); + if (n <= 0) return cur; + target[n] = '\0'; + n = (long)link_root_guest_target(target, (size_t)n); + char joined[PATH_MAX]; + if (target[0] == '/') { + ddnatv_copy_cstr(joined, sizeof(joined), target); + } else { + const char *slash = strrchr(cur, '/'); + size_t dir_len = slash ? (size_t)(slash - cur) : 0; + size_t used = ddnatv_append(joined, sizeof(joined), 0, cur, dir_len); + used = ddnatv_append(joined, sizeof(joined), used, "/", 1); + ddnatv_append(joined, sizeof(joined), used, target, strlen(target)); + } + // lexically, as the guest sees it + size_t len = 0; + out[0] = '\0'; + for (const char *p = joined; *p;) { + while (*p == '/') p++; + const char *end = strchr(p, '/'); + size_t part = end ? (size_t)(end - p) : strlen(p); + if (part == 0) break; + if (part == 1 && p[0] == '.') { + } else if (part == 2 && p[0] == '.' && p[1] == '.') { + while (len > 0 && out[len - 1] != '/') len--; + if (len > 0) len--; + out[len] = '\0'; + } else if (len + 1 + part < outsz) { + out[len++] = '/'; + memcpy(out + len, p, part); + len += part; + out[len] = '\0'; + } + p += part; + } + if (len == 0) { out[0] = '/'; out[1] = '\0'; } + cur = out; + } + return cur; +#else + (void)out; (void)outsz; + return path; +#endif +} + +// On ENOENT, stat()/lstat()/readlink() retry /usr/bin <-> /bin and /usr/sbin <-> /sbin, for +// rootfs trees where bin and sbin are real directories. Returns 1 for the bin pair, 2 for the +// sbin pair (with the other spelling in `alt`), 0 when `sub` is in neither. +#define DDNATV_USRMERGE_BIN 1 +#define DDNATV_USRMERGE_SBIN 2 +static int usrmerge_alt_path(const char *sub, char *alt, size_t altsz) { + const char *dir; + const char *rest; + int pair; + if (strncmp(sub, "/usr/bin/", 9) == 0) { + dir = "/bin/"; rest = sub + 9; pair = DDNATV_USRMERGE_BIN; + } else if (strncmp(sub, "/bin/", 5) == 0) { + dir = "/usr/bin/"; rest = sub + 5; pair = DDNATV_USRMERGE_BIN; + } else if (strncmp(sub, "/usr/sbin/", 10) == 0) { + dir = "/sbin/"; rest = sub + 10; pair = DDNATV_USRMERGE_SBIN; + } else if (strncmp(sub, "/sbin/", 6) == 0) { + dir = "/usr/sbin/"; rest = sub + 6; pair = DDNATV_USRMERGE_SBIN; + } else { + return 0; + } + size_t n = ddnatv_append(alt, altsz, 0, g_ddnatv_root, g_ddnatv_root_len); + n = ddnatv_append(alt, altsz, n, dir, strlen(dir)); + ddnatv_append(alt, altsz, n, rest, strlen(rest)); + return pair; +} + +// Whether postprocess_readlink() below can change a link's target of length target_len, read +// through linkpath: only a target that starts with the rootfs path (at least that long) or a relative +// one into /etc/alternatives (at least "../alternatives", 15 characters, from a directory next to it; +// any "../" under /etc/alternatives itself). lstat() of a link reads it only then, to report its +// rewritten length. +static int readlink_result_may_change(const char *linkpath, long long target_len) { + if (g_ddnatv_root_len > 0 && target_len >= (long long)g_ddnatv_root_len) return 1; + if (!linkpath || linkpath[0] != '/') return 0; + if (strncmp(linkpath, "/etc/alternatives", 17) == 0) return target_len >= 2; + return target_len >= 15; +} + +// Helper for path normalization +static void normalize_path(char *path) { + if (!path || !*path) return; + int is_abs = (path[0] == '/'); + char *stack[128]; + int top = 0; + + char tmp[4096]; + strncpy(tmp, path, sizeof(tmp) - 1); + tmp[sizeof(tmp) - 1] = '\0'; + + char *saveptr = NULL; + char *tok = strtok_r(tmp, "/", &saveptr); + while (tok) { + if (strcmp(tok, ".") == 0) { + // ignore + } else if (strcmp(tok, "..") == 0) { + if (top > 0) top--; + } else { + if (top < 128) stack[top++] = tok; + } + tok = strtok_r(NULL, "/", &saveptr); + } + + char *dst = path; + if (is_abs) *dst++ = '/'; + for (int i = 0; i < top; i++) { + size_t len = strlen(stack[i]); + memcpy(dst, stack[i], len); + dst += len; + if (i + 1 < top) *dst++ = '/'; + } + if (dst == path && is_abs) *dst++ = '/'; + *dst = '\0'; +} + +// /proc/self/fd/N, /proc/thread-self/fd/N and /proc/PID/fd/N: the real path of an open file. A program +// that finds a path this way (Bun and Zig programs do: their realpath() is an open and a readlink of +// this) goes on with system calls of its own, which the hook does not see, so a path inside the rootfs +// has to keep its real beginning there: "/home" for $DDNATV_ROOT/home is a path that does not exist +// for them ("ENOENT, realpath '/home'" in Kilo). It is what getcwd() answers, too. +static int is_proc_fd_link(const char *path) { + if (!path || strncmp(path, "/proc/", 6) != 0) return 0; + const char *p = path + 6; + if (strncmp(p, "self/", 5) == 0) { + p += 5; + } else if (strncmp(p, "thread-self/", 12) == 0) { + p += 12; + } else { + if (*p < '0' || *p > '9') return 0; + while (*p >= '0' && *p <= '9') p++; + if (*p != '/') return 0; + p++; + } + // fd/N and nothing after it: a path beneath the link goes through it to somewhere else + if (strncmp(p, "fd/", 3) != 0) return 0; + p += 3; + if (*p < '0' || *p > '9') return 0; + while (*p >= '0' && *p <= '9') p++; + return *p == '\0'; +} + +// The guest path of a folder that a session mount puts outside the rootfs, for what the kernel +// reports as its real place (getcwd(), /proc/self/cwd and fd links). Wine found no DOS drive for +// Sekiro's folder on the SD card library ("starting in the Windows directory"), and the game quit. +// Rewrites buf[0..len) in place when the guest path fits in cap; returns the new length. +static size_t guest_path_of_mount_source(char *buf, size_t len, size_t cap) { + const struct ddnatv_bind *best = NULL; + for (int i = 0; i < g_nbinds; i++) { + const struct ddnatv_bind *b = &g_binds[i]; + if (b->identity || get_rootfs_subpath(b->src) != NULL || len < b->src_len) continue; + if (memcmp(buf, b->src, b->src_len) != 0 || (len > b->src_len && buf[b->src_len] != '/')) continue; + if (!best || b->src_len > best->src_len || (b->src_len == best->src_len && b->order < best->order)) best = b; + } + if (!best) return len; + size_t rest = len - best->src_len; + if (best->dst_len + rest + 1 > cap) return len; + memmove(buf + best->dst_len, buf + best->src_len, rest); + memcpy(buf, best->dst, best->dst_len); + buf[best->dst_len + rest] = '\0'; + return best->dst_len + rest; +} + +static ssize_t postprocess_readlink(const char *linkpath, char *buf, ssize_t ret, size_t bufsiz) { + if (ret <= 0) return ret; + if ((size_t)ret < bufsiz) { + buf[ret] = '\0'; + ret = (ssize_t)link_root_guest_target(buf, (size_t)ret); + } + ret = (ssize_t)guest_path_of_mount_source(buf, (size_t)ret, bufsiz); + + // 1. Strip g_ddnatv_root if target starts with it + if (g_ddnatv_root_len > 0 && !is_proc_fd_link(linkpath)) { + size_t rlen = g_ddnatv_root_len; + if ((size_t)ret >= rlen && memcmp(buf, g_ddnatv_root, rlen) == 0 && + ((size_t)ret == rlen || buf[rlen] == '/')) { + size_t new_len = ret - rlen; + if (new_len == 0) { + buf[0] = '/'; + return 1; + } else { + memmove(buf, buf + rlen, new_len); + return (ssize_t)new_len; + } + } + } + + // 2. Normalize relative alternatives links. The result can only start with /etc/alternatives + // (or the link be in it) when "alternatives" is in the target or in the link's own path: the + // normalized path is made of their components. Most relative links have neither (the ones of + // /usr/lib and /usr/share/doc), and these skip the copies and the normalization, which were a + // tenth of what "cp -a" of a tree of them cost. + if (linkpath && linkpath[0] == '/' && (size_t)ret < PATH_MAX && ret >= 2 && buf[0] == '.' && buf[1] == '.' && + (memmem(buf, (size_t)ret, "alternatives", 12) != NULL || strstr(linkpath, "alternatives") != NULL)) { + char link_copy[PATH_MAX]; + memcpy(link_copy, buf, ret); + link_copy[ret] = '\0'; + + if (strncmp(link_copy, "..", 2) == 0) { + char parent[PATH_MAX]; + snprintf(parent, sizeof(parent), "%s", linkpath); + char *last_slash = strrchr(parent, '/'); + if (last_slash) { + if (last_slash == parent) { + *(last_slash + 1) = '\0'; + } else { + *last_slash = '\0'; + } + } + char combined[PATH_MAX * 2]; + snprintf(combined, sizeof(combined), "%s/%s", parent, link_copy); + normalize_path(combined); + + if (strncmp(combined, "/etc/alternatives", 17) == 0 || + strncmp(linkpath, "/etc/alternatives", 17) == 0) { + size_t clen = strlen(combined); + if (clen <= bufsiz) { + memcpy(buf, combined, clen); + return (ssize_t)clen; + } + } + } + } + + return ret; +} + +// Android's SELinux label, which the xattr hooks keep away from Ubuntu programs. +static const char k_selinux_xattr[] = "security.selinux"; + +static int is_selinux_xattr(const char *name) { + return name != NULL && strcmp(name, k_selinux_xattr) == 0; +} + +// Removes "security.selinux" from a listxattr() result (NUL-separated names); returns the new length. +static ssize_t drop_selinux_xattr(char *list, ssize_t len) { + if (!list || len <= 0) return len; + ssize_t out = 0; + ssize_t i = 0; + while (i < len) { + size_t name_len = strnlen(list + i, (size_t)(len - i)); + ssize_t entry = (ssize_t)name_len + 1; + if (entry > len - i) entry = len - i; + if (name_len != sizeof(k_selinux_xattr) - 1 || memcmp(list + i, k_selinux_xattr, name_len) != 0) { + memmove(list + out, list + i, (size_t)entry); + out += entry; + } + i += entry; + } + return out; +} + +// Library directories of the rootfs, in the order ld.so searches them. ld.so tries every +// directory for every library, so the multiarch ones that hold nearly all libraries come first +// (as in glibc's own default order). Keep in sync with Environment.kt, 00-env.sh and su.sh. +static const char *const k_rootfs_lib_dirs[] = { + "/lib/aarch64-linux-gnu", "/usr/lib/aarch64-linux-gnu", + "/lib/arm-linux-gnueabihf", "/usr/lib/arm-linux-gnueabihf", + "/lib", "/usr/lib", "/usr/local/lib", + "/usr/lib/systemd", "/lib/systemd", + "/usr/lib/aarch64-linux-gnu/systemd", "/lib/aarch64-linux-gnu/systemd", + "/usr/lib/arm-linux-gnueabihf/systemd", "/lib/arm-linux-gnueabihf/systemd", +}; + +// Whether the ':'-separated list holds the directory dir[0..dir_len) as one of its entries. +static int lib_dir_list_has(const char *list, const char *dir, size_t dir_len) { + if (!list) return 0; + for (const char *p = list; *p;) { + const char *end = strchr(p, ':'); + size_t len = end ? (size_t)(end - p) : strlen(p); + if (len == dir_len && memcmp(p, dir, len) == 0) return 1; + if (!end) break; + p = end + 1; + } + return 0; +} + +// Appends the rootfs library directories as a ':'-separated list (with a leading ':' when +// `separate` is set), leaving out those that `have` (a list too, or NULL) names already: the +// caller's LD_LIBRARY_PATH is usually this very list, and ld.so spends time on every directory of +// the list it is given, in every program that is started, and tries it again for each library that +// is not found. Returns the new length. +static size_t append_rootfs_lib_dirs_missing_from(char *out, size_t outsz, size_t used, int separate, const char *have) { + for (size_t i = 0; i < sizeof(k_rootfs_lib_dirs) / sizeof(k_rootfs_lib_dirs[0]); i++) { + // a Flatpak sandbox mounts /usr from its runtime + char buf[PATH_MAX]; + const char *full = rewrite_path_in_root(k_rootfs_lib_dirs[i], buf, sizeof(buf)); + size_t n = strlen(full); + if (have && have[0] != '\0' && lib_dir_list_has(have, full, n)) continue; + // whole directories only: a cut-off one would be searched in the wrong place + if (used + (separate ? 1 : 0) + n >= outsz) continue; + if (separate) used = ddnatv_append(out, outsz, used, ":", 1); + separate = 1; + used = ddnatv_append(out, outsz, used, full, n); + } + return used; +} + +// All of them. +static size_t append_rootfs_lib_dirs(char *out, size_t outsz, size_t used, int separate) { + return append_rootfs_lib_dirs_missing_from(out, outsz, used, separate, NULL); +} + +#ifndef DDNATV_HOST_TEST +// Relative *at() calls. A program that walks a path one component at a time (systemd's chase(), used +// by libudev; fts) opens "/" and then "sys", "class", ... relative to the last descriptor, and those +// names never pass through rewrite_path(): "/" is the rootfs, whose own sys/ is empty, so the stand-ins +// mounted at /sys/class/hidraw and the host's /sys were out of reach. The directories on the way to a +// mount (every proper ancestor of a mount's guest path) are recorded by device and inode; a name given +// relative to one of them is joined to its guest path and mapped like an absolute one. +struct ddnatv_at_dir { + unsigned long dev, ino; + char *guest; +}; +static struct ddnatv_at_dir *g_at_dirs = NULL; +static int g_n_at_dirs = 0; +static pthread_once_t g_at_once = PTHREAD_ONCE_INIT; + +#if defined(__aarch64__) +static long raw_fstatat(int dirfd, const char *path, struct stat *st, int flags) { + register long x8 __asm__("x8") = 79; // newfstatat + register long x0 __asm__("x0") = dirfd; + register long x1 __asm__("x1") = (long)path; + register long x2 __asm__("x2") = (long)st; + register long x3 __asm__("x3") = flags; + __asm__ volatile("svc #0" : "+r"(x0) : "r"(x8), "r"(x1), "r"(x2), "r"(x3) : "memory"); + return x0; +} +#endif + +static void ddnatv_add_at_dir(const char *guest) { +#if defined(__aarch64__) + for (int i = 0; i < g_n_at_dirs; i++) if (strcmp(g_at_dirs[i].guest, guest) == 0) return; + char buf[PATH_MAX]; + const char *host = rewrite_path_in_root(guest, buf, sizeof(buf)); + struct stat st; + if (raw_fstatat(AT_FDCWD, host, &st, 0) != 0 || !S_ISDIR(st.st_mode)) return; + struct ddnatv_at_dir *grown = realloc(g_at_dirs, sizeof(*grown) * (size_t)(g_n_at_dirs + 1)); + if (!grown) return; + g_at_dirs = grown; + g_at_dirs[g_n_at_dirs].dev = (unsigned long)st.st_dev; + g_at_dirs[g_n_at_dirs].ino = (unsigned long)st.st_ino; + g_at_dirs[g_n_at_dirs].guest = strdup(guest); + if (g_at_dirs[g_n_at_dirs].guest) g_n_at_dirs++; +#else + (void)guest; +#endif +} + +static void ddnatv_build_at_dirs(void) { + if (g_nbinds == 0) return; + ddnatv_add_at_dir("/"); + for (int i = 0; i < g_nbinds; i++) { + char guest[PATH_MAX]; + ddnatv_copy_cstr(guest, sizeof(guest), g_binds[i].dst); + for (char *slash = strrchr(guest, '/'); slash && slash > guest; slash = strrchr(guest, '/')) { + *slash = '\0'; + ddnatv_add_at_dir(guest); + } + } +} + +// The absolute host path for `rel` given relative to `dirfd`, or NULL when the call needs no change. +static const char *map_at(int dirfd, const char *rel, char *buf, size_t size) { +#if defined(__aarch64__) + if (dirfd == AT_FDCWD || !rel || rel[0] == '\0' || rel[0] == '/' || g_nbinds == 0) return NULL; + pthread_once(&g_at_once, ddnatv_build_at_dirs); + if (g_n_at_dirs == 0) return NULL; + struct stat st; + if (raw_fstatat(dirfd, "", &st, AT_EMPTY_PATH) != 0) return NULL; + for (int i = 0; i < g_n_at_dirs; i++) { + if (g_at_dirs[i].ino != (unsigned long)st.st_ino || g_at_dirs[i].dev != (unsigned long)st.st_dev) continue; + char guest[PATH_MAX]; + const char *base = g_at_dirs[i].guest; + size_t used = ddnatv_append(guest, sizeof(guest), 0, base, strlen(base)); + if (used > 1) used = ddnatv_append(guest, sizeof(guest), used, "/", 1); + ddnatv_append(guest, sizeof(guest), used, rel, strlen(rel)); + const char *mapped = rewrite_path_in_root(guest, buf, size); + if (mapped != buf) ddnatv_copy_cstr(buf, size, mapped); + return buf; + } +#else + (void)dirfd; (void)rel; (void)buf; (void)size; +#endif + return NULL; +} + +#define DDNATV_AT_REMAP_NAMED(dirfd, pathname, buf) \ + char buf[PATH_MAX]; \ + if (map_at((dirfd), (pathname), buf, sizeof(buf))) { \ + (dirfd) = AT_FDCWD; \ + (pathname) = buf; \ + } +#define DDNATV_AT_REMAP(dirfd, pathname) DDNATV_AT_REMAP_NAMED(dirfd, pathname, ddnatv_at_buf) +#endif /* !DDNATV_HOST_TEST */ diff --git a/tools/nativeguest/hook/posix_shm.c b/tools/nativeguest/hook/posix_shm.c new file mode 100644 index 0000000000..afd8a6f655 --- /dev/null +++ b/tools/nativeguest/hook/posix_shm.c @@ -0,0 +1,174 @@ +// Part of libddnatv-hook.so, included by ddnatv-hook.c. +// +// shm_open() and shm_unlink() of glibc open /dev/shm/NAME with a system call of their own, which +// the path hooks never see and Android has no /dev/shm for. They go through open() and unlink() +// here, which map /dev/shm. + +#ifndef DDNATV_HOST_TEST + +static int posix_shm_path(const char *name, char *path, size_t size) { + if (!name) { errno = EINVAL; return -1; } + while (*name == '/') name++; + size_t len = strlen(name); + if (len == 0 || len > NAME_MAX || strchr(name, '/') || strcmp(name, ".") == 0 || strcmp(name, "..") == 0) { + errno = len > NAME_MAX ? ENAMETOOLONG : EINVAL; + return -1; + } + if (snprintf(path, size, "/dev/shm/%s", name) >= (int)size) { errno = ENAMETOOLONG; return -1; } + return 0; +} + +int shm_open(const char *name, int oflag, mode_t mode) { + char path[PATH_MAX]; + if (posix_shm_path(name, path, sizeof(path)) < 0) return -1; + return open(path, (oflag & (O_ACCMODE | O_CREAT | O_EXCL | O_TRUNC)) | O_NOFOLLOW | O_CLOEXEC, mode); +} + +int shm_unlink(const char *name) { + char path[PATH_MAX]; + if (posix_shm_path(name, path, sizeof(path)) < 0) return -1; + return unlink(path); +} + +#endif /* !DDNATV_HOST_TEST */ + +#ifndef DDNATV_HOST_TEST +// glibc's posix_spawn child resets every signal handler to its default, the hook's SIGSYS handler +// included, before it runs the file actions, and "close everything from N" is close_range(), which +// Android's seccomp filter answers with SIGSYS: the child died of it (Python's subprocess with +// close_fds does this). It becomes one close per descriptor, added when the program is spawned so +// that descriptors opened in between and the ones earlier actions make are closed too; the targets +// of actions added after it stay open, as they would. A close of a descriptor that is not open is +// no error to glibc. +#define CLOSEFROM_SLOTS 64 +#define CLOSEFROM_KEEP 32 +static struct closefrom_entry { + posix_spawn_file_actions_t *actions; + int from; // -1: no close-from yet, only targets of actions seen + int highest; // the highest descriptor an action before close-from makes + int nkeep; + int keep[CLOSEFROM_KEEP]; + int closed_to; // the descriptors up to this one are closed by actions already added + int bad_source; // a dup2 after close-from duplicates a descriptor it closed: glibc's EBADF +} g_closefrom[CLOSEFROM_SLOTS]; +static pthread_mutex_t g_closefrom_lock = PTHREAD_MUTEX_INITIALIZER; + +static struct closefrom_entry *closefrom_entry(posix_spawn_file_actions_t *actions, int create) { + struct closefrom_entry *free_slot = NULL; + for (int i = 0; i < CLOSEFROM_SLOTS; i++) { + if (g_closefrom[i].actions == actions) return &g_closefrom[i]; + if (!g_closefrom[i].actions && !free_slot) free_slot = &g_closefrom[i]; + } + if (!create || !free_slot) return NULL; + memset(free_slot, 0, sizeof(*free_slot)); + free_slot->actions = actions; + free_slot->from = -1; + free_slot->highest = -1; + free_slot->closed_to = -1; + return free_slot; +} + +static void closefrom_note_target(posix_spawn_file_actions_t *actions, int fd) { + pthread_mutex_lock(&g_closefrom_lock); + struct closefrom_entry *e = closefrom_entry(actions, 1); + if (e) { + if (e->from < 0) { + if (fd > e->highest) e->highest = fd; + } else if (e->nkeep < CLOSEFROM_KEEP) { + e->keep[e->nkeep++] = fd; + } + } + pthread_mutex_unlock(&g_closefrom_lock); +} + +int posix_spawn_file_actions_addclosefrom_np(posix_spawn_file_actions_t *actions, int from) { + if (!actions || from < 0) return EINVAL; + pthread_mutex_lock(&g_closefrom_lock); + struct closefrom_entry *e = closefrom_entry(actions, 1); + if (e && (e->from < 0 || from < e->from)) e->from = from; + pthread_mutex_unlock(&g_closefrom_lock); + return e ? 0 : ENOMEM; +} + +int posix_spawn_file_actions_addopen(posix_spawn_file_actions_t *actions, int fd, const char *path, int oflag, mode_t mode) { + static int (*orig)(posix_spawn_file_actions_t *, int, const char *, int, mode_t) = NULL; + if (!orig) orig = (int (*)(posix_spawn_file_actions_t *, int, const char *, int, mode_t))dlsym(RTLD_NEXT, "posix_spawn_file_actions_addopen"); + int ret = orig ? orig(actions, fd, path, oflag, mode) : ENOSYS; + if (ret == 0) closefrom_note_target(actions, fd); + return ret; +} + +int posix_spawn_file_actions_adddup2(posix_spawn_file_actions_t *actions, int fd, int newfd) { + static int (*orig)(posix_spawn_file_actions_t *, int, int) = NULL; + if (!orig) orig = (int (*)(posix_spawn_file_actions_t *, int, int))dlsym(RTLD_NEXT, "posix_spawn_file_actions_adddup2"); + int ret = orig ? orig(actions, fd, newfd) : ENOSYS; + if (ret == 0) { + pthread_mutex_lock(&g_closefrom_lock); + struct closefrom_entry *e = closefrom_entry(actions, 0); + if (e && e->from >= 0 && fd >= e->from) { + int reopened = 0; + for (int k = 0; k < e->nkeep; k++) reopened |= e->keep[k] == fd; + if (!reopened) e->bad_source = 1; + } + pthread_mutex_unlock(&g_closefrom_lock); + closefrom_note_target(actions, newfd); + } + return ret; +} + +int posix_spawn_file_actions_init(posix_spawn_file_actions_t *actions) { + static int (*orig)(posix_spawn_file_actions_t *) = NULL; + if (!orig) orig = (int (*)(posix_spawn_file_actions_t *))dlsym(RTLD_NEXT, "posix_spawn_file_actions_init"); + pthread_mutex_lock(&g_closefrom_lock); + struct closefrom_entry *e = closefrom_entry(actions, 0); + if (e) e->actions = NULL; + pthread_mutex_unlock(&g_closefrom_lock); + return orig ? orig(actions) : ENOSYS; +} + +int posix_spawn_file_actions_destroy(posix_spawn_file_actions_t *actions) { + static int (*orig)(posix_spawn_file_actions_t *) = NULL; + if (!orig) orig = (int (*)(posix_spawn_file_actions_t *))dlsym(RTLD_NEXT, "posix_spawn_file_actions_destroy"); + pthread_mutex_lock(&g_closefrom_lock); + struct closefrom_entry *e = closefrom_entry(actions, 0); + if (e) e->actions = NULL; + pthread_mutex_unlock(&g_closefrom_lock); + return orig ? orig(actions) : ENOSYS; +} + +static int apply_closefrom(posix_spawn_file_actions_t *actions) { + pthread_mutex_lock(&g_closefrom_lock); + struct closefrom_entry *e = closefrom_entry(actions, 0); + if (!e || e->from < 0) { + pthread_mutex_unlock(&g_closefrom_lock); + return 0; + } + if (e->bad_source) { + pthread_mutex_unlock(&g_closefrom_lock); + return EBADF; + } + int highest = e->highest; + DIR *dir = opendir("/proc/self/fd"); + if (dir) { + int self = dirfd(dir); + struct dirent *entry; + while ((entry = readdir(dir)) != NULL) { + if (entry->d_name[0] < '0' || entry->d_name[0] > '9') continue; + int fd = atoi(entry->d_name); + if (fd != self && fd > highest) highest = fd; + } + closedir(dir); + } + int start = e->from > e->closed_to + 1 ? e->from : e->closed_to + 1; + for (int fd = start; fd <= highest; fd++) { + int kept = 0; + for (int k = 0; k < e->nkeep; k++) kept |= e->keep[k] == fd; + if (!kept && posix_spawn_file_actions_addclose(actions, fd) != 0) break; + e->closed_to = fd; + } + pthread_mutex_unlock(&g_closefrom_lock); + return 0; +} +#else +static int apply_closefrom(posix_spawn_file_actions_t *actions) { (void)actions; return 0; } +#endif diff --git a/tools/nativeguest/hook/privileged.c b/tools/nativeguest/hook/privileged.c new file mode 100644 index 0000000000..6b527ae4f3 --- /dev/null +++ b/tools/nativeguest/hook/privileged.c @@ -0,0 +1,815 @@ +// Part of libddnatv-hook.so, included by ddnatv-hook.c (one translation unit, so the +// parts share their static functions and are built with the same command as before). +// +// Calls an app may not make (chown, mknod, capabilities, prctl, landlock, seccomp, namespaces, +// close_range) and the generic syscall() entry point. + +// The ARM translator of x86 devices and emulators (ndk_translation, houdini) keeps memory files of +// its own open in the process (ndk_translation at descriptor 3) and needs them whenever it +// translates more code: a program that closes every descriptor it did not open (sshd at start, +// closefrom(3)) made it abort later ("CHECK failed" in mmap_posix.cc). Its memory files open when +// the hook starts survive close(), close_range() and closefrom(); phones that run ARM code +// themselves have none. +#define DDNATV_MAX_KEPT_FDS 4 +static int g_kept_fds[DDNATV_MAX_KEPT_FDS]; // ascending +static int g_kept_fd_count = 0; + +static int is_kept_fd(int fd) { + for (int i = 0; i < g_kept_fd_count; i++) { + if (g_kept_fds[i] == fd) return 1; + } + return 0; +} + +// Calls fn for each part of [first, last] that leaves out the descriptors in kept (ascending); +// the first nonzero answer of fn is returned, else 0. +static int for_each_range_without(unsigned int first, unsigned int last, const int *kept, int nkept, + int (*fn)(unsigned int, unsigned int, void *), void *arg) { + unsigned int from = first; + for (int i = 0; i < nkept && from <= last; i++) { + if (kept[i] < 0 || (unsigned int)kept[i] < from || (unsigned int)kept[i] > last) continue; + unsigned int k = (unsigned int)kept[i]; + if (k > from) { + int r = fn(from, k - 1, arg); + if (r != 0) return r; + } + if (k == last) return 0; + from = k + 1; + } + return from <= last ? fn(from, last, arg) : 0; +} + +#ifndef DDNATV_HOST_TEST + +// ndk_translation's file is "/memfd:child"; any memory file counts in a process whose executable is +// a translator's runner (houdini). ndk_translation shows a program its own path as /proc/self/exe, +// so the name is what tells there. +// +// This runs at the start of every program, so it costs three system calls on a phone that runs ARM +// code itself: the descriptors that are open (/proc/self/fd, read at once) are all that are asked +// about, where asking about each of the numbers from 3 to 63 was 61 readlink() calls in every +// process that was started. +// Set once this process knows that it has none (it looked, or was told by DDNATV_NT=0 in its +// environment, which prepare_ddnatv_env hands on to the programs it starts) +static int g_no_translator = 0; + +__attribute__((constructor(102))) static void find_translator_files(void) { + // The first program of a session looks; the ones it starts are told (a translator is there for + // all the programs of a machine or for none, and where it is not known they look as before) + const char *known = getenv("DDNATV_NT"); + if (known && known[0] == '0' && known[1] == '\0') { + g_no_translator = 1; + return; + } + ssize_t (*real_readlink)(const char *, char *, size_t) = (ssize_t (*)(const char *, char *, size_t))dlsym(RTLD_NEXT, "readlink"); + int (*real_open)(const char *, int, ...) = (int (*)(const char *, int, ...))dlsym(RTLD_NEXT, "open"); + int (*real_close)(int) = (int (*)(int))dlsym(RTLD_NEXT, "close"); + ssize_t (*real_getdents64)(int, void *, size_t) = (ssize_t (*)(int, void *, size_t))dlsym(RTLD_NEXT, "getdents64"); + if (!real_readlink || !real_open || !real_close) return; + int dir = real_getdents64 ? real_open("/proc/self/fd", O_RDONLY | O_DIRECTORY | O_CLOEXEC) : -1; + if (dir < 0) return; + // The numbers of the descriptors open now, those from 3 to 63 (the directory's own left out) + char entries[2048]; + unsigned char open_fds[64]; + int nopen = 0; + for (;;) { + long n = real_getdents64(dir, entries, sizeof(entries)); + if (n <= 0) break; + for (long off = 0; off < n;) { + // struct linux_dirent64: d_ino (8), d_off (8), d_reclen (2), d_type (1), then d_name + unsigned short reclen; + memcpy(&reclen, entries + off + 16, sizeof(reclen)); + const char *name = entries + off + 19; + off += reclen ? reclen : n; // a record without a length ends the buffer + if (name[0] < '0' || name[0] > '9') continue; // "." and ".." + int fd = atoi(name); + if (fd >= 3 && fd < 64 && fd != dir) open_fds[nopen++] = (unsigned char)fd; + } + // A short answer is the whole directory; asking again would only be told "no more" + if ((size_t)n + 64 <= sizeof(entries)) break; + } + real_close(dir); + if (nopen == 0) { + g_no_translator = 1; + return; + } + + char path[PATH_MAX]; + int runner = 0; + ssize_t n = real_readlink("/proc/self/exe", path, sizeof(path) - 1); + if (n > 0) { + path[n] = '\0'; + const char *base = strrchr(path, '/'); + base = base ? base + 1 : path; + runner = strncmp(base, "ndk_translation", 15) == 0 || strncmp(base, "houdini", 7) == 0; + } + // In ascending order, as the list is kept + for (int i = 0; i < nopen - 1; i++) { + for (int j = i + 1; j < nopen; j++) { + if (open_fds[j] < open_fds[i]) { + unsigned char t = open_fds[i]; + open_fds[i] = open_fds[j]; + open_fds[j] = t; + } + } + } + for (int i = 0; i < nopen && g_kept_fd_count < DDNATV_MAX_KEPT_FDS; i++) { + char link[32], target[64]; + snprintf(link, sizeof(link), "/proc/self/fd/%d", open_fds[i]); + ssize_t m = real_readlink(link, target, sizeof(target) - 1); + if (m <= 0) continue; + target[m] = '\0'; + if (strncmp(target, "/memfd:child", 12) == 0 || (runner && strncmp(target, "/memfd:", 7) == 0)) { + g_kept_fds[g_kept_fd_count++] = open_fds[i]; + } + } + if (g_kept_fd_count == 0) g_no_translator = 1; +} + +int chown(const char *pathname, uid_t owner, gid_t group) { (void)pathname; (void)owner; (void)group; return 0; } +int fchown(int fd, uid_t owner, gid_t group) { (void)fd; (void)owner; (void)group; return 0; } +int lchown(const char *pathname, uid_t owner, gid_t group) { (void)pathname; (void)owner; (void)group; return 0; } +int fchownat(int dirfd, const char *pathname, uid_t owner, gid_t group, int flags) { + DDNATV_AT_REMAP(dirfd, pathname); + (void)dirfd; (void)pathname; (void)owner; (void)group; (void)flags; return 0; +} +int chroot(const char *path) { (void)path; return 0; } + +// Hook mkfifo / mkfifoat / mknod / mknodat +int mkfifo(const char *pathname, mode_t mode); +int mkfifoat(int dirfd, const char *pathname, mode_t mode); + +int mknodat(int dirfd, const char *pathname, mode_t mode, dev_t dev) { + DDNATV_AT_REMAP(dirfd, pathname); + (void)dev; + if (!pathname) { + errno = EFAULT; + return -1; + } + char buf[PATH_MAX]; + const char *target = (pathname[0] == '/') ? rewrite_path(pathname, buf, sizeof(buf)) : pathname; + // A file type of 0 means a regular file, as in mknod(2), which also fails with EEXIST instead + // of touching an existing file. + if (S_ISREG(mode) || (mode & S_IFMT) == 0) { + // mknod(2) returns 0 on success, never a file descriptor: close the + // transient fd instead of leaking one per call (tar/dpkg extract + // thousands of files; leaked fds exhaust the table -> EBADF noise). + int fd = openat(dirfd, target, O_CREAT | O_EXCL | O_WRONLY | O_CLOEXEC, mode & 07777); + if (fd >= 0) { + close(fd); + return 0; + } + return -1; + } + if (S_ISFIFO(mode)) { + return mkfifoat(dirfd, target, mode); + } + return 0; +} + +int mknod(const char *pathname, mode_t mode, dev_t dev) { + return mknodat(AT_FDCWD, pathname, mode, dev); +} + +int mkfifoat(int dirfd, const char *pathname, mode_t mode) { + static int (*orig_mkfifoat)(int, const char *, mode_t) = NULL; + if (!orig_mkfifoat) orig_mkfifoat = (int (*)(int, const char *, mode_t))dlsym(RTLD_NEXT, "mkfifoat"); + if (!pathname) { + errno = EFAULT; + return -1; + } + char buf[PATH_MAX]; + const char *target = (pathname[0] == '/') ? rewrite_path(pathname, buf, sizeof(buf)) : pathname; + if (orig_mkfifoat) { + return orig_mkfifoat(dirfd, target, mode); + } + static int (*orig_mkfifo)(const char *, mode_t) = NULL; + if (!orig_mkfifo) orig_mkfifo = (int (*)(const char *, mode_t))dlsym(RTLD_NEXT, "mkfifo"); + if (orig_mkfifo && dirfd == AT_FDCWD) { + return orig_mkfifo(target, mode); + } + return -1; +} + +int mkfifo(const char *pathname, mode_t mode) { + return mkfifoat(AT_FDCWD, pathname, mode); +} + +// Hook sync / syncfs to prevent Android flash storage stalls during dpkg operations +void sync(void) { + // No-op +} + +int syncfs(int fd) { + (void)fd; + return 0; +} + +int capget(void *hdrp, void *datap) { + static int (*orig_capget)(void *, void *) = NULL; + if (!orig_capget) orig_capget = (int (*)(void *, void *))dlsym(RTLD_NEXT, "capget"); + if (orig_capget && orig_capget(hdrp, datap) == 0) { + return 0; + } + if (datap) { + uint32_t ver = hdrp ? *(const uint32_t *)hdrp : 0; + size_t nbytes = (ver == 0x20071026U || ver == 0x20080522U) ? 24 : 12; + memset(datap, 0, nbytes); + } + errno = 0; + return 0; +} +int capset(void *hdrp, const void *datap) { (void)hdrp; (void)datap; return 0; } + +int prctl(int option, ...) { + va_list ap; + va_start(ap, option); + unsigned long arg2 = va_arg(ap, unsigned long); + unsigned long arg3 = va_arg(ap, unsigned long); + unsigned long arg4 = va_arg(ap, unsigned long); + unsigned long arg5 = va_arg(ap, unsigned long); + va_end(ap); + + // PR_SET_NO_NEW_PRIVS = 38, PR_GET_NO_NEW_PRIVS = 39 +#ifdef PR_SET_NO_NEW_PRIVS + if (option == PR_SET_NO_NEW_PRIVS) return 0; +#endif + if (option == 38) return 0; +#ifdef PR_GET_NO_NEW_PRIVS + if (option == PR_GET_NO_NEW_PRIVS) return 1; +#endif + if (option == 39) return 1; + + // PR_SET_SECCOMP = 22, PR_GET_SECCOMP = 21 +#ifdef PR_SET_SECCOMP + if (option == PR_SET_SECCOMP) { errno = EINVAL; return -1; } +#endif + if (option == 22) { errno = EINVAL; return -1; } +#ifdef PR_GET_SECCOMP + if (option == PR_GET_SECCOMP) return 0; +#endif + if (option == 21) return 0; + + // PR_SET_SPECULATION_CTRL = 47 + if (option == 47) return 0; + + static int (*orig_prctl)(int, unsigned long, unsigned long, unsigned long, unsigned long) = NULL; + if (!orig_prctl) orig_prctl = (int (*)(int, unsigned long, unsigned long, unsigned long, unsigned long))dlsym(RTLD_NEXT, "prctl"); + return orig_prctl ? orig_prctl(option, arg2, arg3, arg4, arg5) : 0; +} + +// Limits that leave a process no new descriptors (RLIMIT_NOFILE below 16) or no file writes +// (RLIMIT_FSIZE 0) are not applied. Sandboxes give them to their unprivileged part (sshd's before +// login sets 1 and 0, with the seccomp filter above), and Android's ARM translator (emulators, x86 +// devices) needs new descriptors and memory files in the process: sshd's died in it ("CHECK failed" +// in its mmap) and the client's connection was reset. Other limits are set as asked. +static int sandbox_only_limit(int resource, const struct rlimit *rlim) { + if (!rlim) return 0; + if (resource == RLIMIT_NOFILE) return rlim->rlim_cur < 16; + if (resource == RLIMIT_FSIZE) return rlim->rlim_cur == 0; + return 0; +} + +int setrlimit(__rlimit_resource_t resource, const struct rlimit *rlim) { + if (sandbox_only_limit((int)resource, rlim)) return 0; + static int (*orig_setrlimit)(__rlimit_resource_t, const struct rlimit *) = NULL; +#if defined(__USE_FILE_OFFSET64) && !defined(__LP64__) + // With 64-bit file offsets on a 32-bit system this function is setrlimit64 + if (!orig_setrlimit) orig_setrlimit = (int (*)(__rlimit_resource_t, const struct rlimit *))dlsym(RTLD_NEXT, "setrlimit64"); +#else + if (!orig_setrlimit) orig_setrlimit = (int (*)(__rlimit_resource_t, const struct rlimit *))dlsym(RTLD_NEXT, "setrlimit"); +#endif + if (!orig_setrlimit) { + errno = ENOSYS; + return -1; + } + return orig_setrlimit(resource, rlim); +} +#if defined(__LP64__) +__asm__(".globl setrlimit64\n" ".set setrlimit64, setrlimit\n"); +#endif + +#ifndef LANDLOCK_CREATE_RULESET_VERSION +#define LANDLOCK_CREATE_RULESET_VERSION (1U << 0) +#endif + +#ifndef __NR_landlock_create_ruleset +#define __NR_landlock_create_ruleset 444 +#endif +#ifndef __NR_landlock_add_rule +#define __NR_landlock_add_rule 445 +#endif +#ifndef __NR_landlock_restrict_self +#define __NR_landlock_restrict_self 446 +#endif + +#ifndef __NR_seccomp +#if defined(__aarch64__) +#define __NR_seccomp 277 +#elif defined(__x86_64__) +#define __NR_seccomp 317 +#elif defined(__arm__) +#define __NR_seccomp 383 +#else +#define __NR_seccomp 277 +#endif +#endif + +#ifndef __NR_unshare +#if defined(__aarch64__) +#define __NR_unshare 97 +#elif defined(__x86_64__) +#define __NR_unshare 272 +#elif defined(__arm__) +#define __NR_unshare 337 +#else +#define __NR_unshare 97 +#endif +#endif + +#ifndef __NR_setns +#if defined(__aarch64__) +#define __NR_setns 268 +#elif defined(__x86_64__) +#define __NR_setns 308 +#elif defined(__arm__) +#define __NR_setns 375 +#else +#define __NR_setns 268 +#endif +#endif + +#ifndef __NR_clone3 +#define __NR_clone3 435 +#endif +#ifndef __NR_pidfd_open +#define __NR_pidfd_open 434 +#endif +#ifndef __NR_pidfd_send_signal +#define __NR_pidfd_send_signal 424 +#endif +#ifndef __NR_pidfd_getfd +#define __NR_pidfd_getfd 438 +#endif +#ifndef __NR_close_range +#define __NR_close_range 436 +#endif +#ifndef __NR_faccessat +#if defined(__aarch64__) +#define __NR_faccessat 48 +#elif defined(__arm__) +#define __NR_faccessat 334 +#elif defined(__x86_64__) +#define __NR_faccessat 269 +#endif +#endif +#ifndef __NR_faccessat2 +#define __NR_faccessat2 439 +#endif +#ifndef __NR_statx +#if defined(__aarch64__) || defined(__arm__) +#define __NR_statx 291 +#elif defined(__x86_64__) +#define __NR_statx 332 +#endif +#endif + +#ifndef SECCOMP_SET_MODE_STRICT +#define SECCOMP_SET_MODE_STRICT 0 +#endif +#ifndef SECCOMP_SET_MODE_FILTER +#define SECCOMP_SET_MODE_FILTER 1 +#endif +#ifndef SECCOMP_GET_ACTION_AVAIL +#define SECCOMP_GET_ACTION_AVAIL 2 +#endif + +int landlock_create_ruleset(const void *attr, size_t size, uint32_t flags) { + (void)attr; + (void)size; + (void)flags; + errno = ENOSYS; + return -1; +} + +int landlock_add_rule(int ruleset_fd, int rule_type, const void *rule_attr, uint32_t flags) { + (void)ruleset_fd; + (void)rule_type; + (void)rule_attr; + (void)flags; + errno = ENOSYS; + return -1; +} + +int landlock_restrict_self(int ruleset_fd, uint32_t flags) { + (void)ruleset_fd; + (void)flags; + errno = ENOSYS; + return -1; +} + +int seccomp(unsigned int operation, unsigned int flags, void *args) { + (void)flags; + (void)args; + if (operation != SECCOMP_SET_MODE_STRICT && + operation != SECCOMP_SET_MODE_FILTER && + operation != SECCOMP_GET_ACTION_AVAIL && + operation != 3 /* SECCOMP_GET_NOTIF_SIZES */) { + errno = EINVAL; + return -1; + } + errno = ENOSYS; + return -1; +} + +int unshare(int flags) { + (void)flags; + errno = ENOSYS; + return -1; +} + +int setns(int fd, int nstype) { + (void)fd; + (void)nstype; + errno = ENOSYS; + return -1; +} + +#ifndef CLOSE_RANGE_CLOEXEC +#define CLOSE_RANGE_CLOEXEC (1U << 2) +#endif + +static int close_range_all(unsigned int first, unsigned int last, int flags); + +static int close_range_part(unsigned int first, unsigned int last, void *flags) { + return close_range_all(first, last, *(int *)flags); +} + +int close_range(unsigned int first, unsigned int last, int flags) { + // Close-on-exec only (CLOSE_RANGE_CLOEXEC) leaves the translator's files open + if (g_kept_fd_count > 0 && !(flags & CLOSE_RANGE_CLOEXEC)) { + return for_each_range_without(first, last, g_kept_fds, g_kept_fd_count, close_range_part, &flags); + } + return close_range_all(first, last, flags); +} + +// glibc's closefrom() makes the close_range system call itself, past the function above +void closefrom(int lowfd) { + close_range(lowfd < 0 ? 0U : (unsigned int)lowfd, ~0U, 0); +} + +static int close_range_all(unsigned int first, unsigned int last, int flags) { + static int (*orig_close_range)(unsigned int, unsigned int, int) = NULL; + if (!orig_close_range) { + orig_close_range = (int (*)(unsigned int, unsigned int, int))dlsym(RTLD_NEXT, "close_range"); + } +#ifdef __NR_close_range + static long (*orig_raw_syscall)(long, ...) = NULL; + if (!orig_raw_syscall) orig_raw_syscall = (long (*)(long, ...))dlsym(RTLD_NEXT, "syscall"); + if (orig_raw_syscall) { + long sret = orig_raw_syscall(__NR_close_range, first, last, flags); + if (sret == 0) return 0; + if (sret < 0 && errno != ENOSYS) return -1; + } +#endif + if (orig_close_range) { + int cret = orig_close_range(first, last, flags); + if (cret == 0) return 0; + if (cret < 0 && errno != ENOSYS) return -1; + } + + int set_cloexec = (flags & CLOSE_RANGE_CLOEXEC) != 0; + + DIR *d = opendir("/proc/self/fd"); + if (d) { + int dfd = dirfd(d); + struct dirent *de; + while ((de = readdir(d)) != NULL) { + if (de->d_name[0] == '.') continue; + int fd = atoi(de->d_name); + if (fd >= (int)first && (last == ~0U || (unsigned int)fd <= last) && fd != dfd) { + if (set_cloexec) { + int fl = fcntl(fd, F_GETFD); + if (fl >= 0) { + fcntl(fd, F_SETFD, fl | FD_CLOEXEC); + } + } else { + close(fd); + } + } + } + closedir(d); + return 0; + } + int max_fd = (int)sysconf(_SC_OPEN_MAX); + if (max_fd <= 0 || max_fd > 1024) max_fd = 1024; + int end = (last < (unsigned int)max_fd) ? (int)last : max_fd; + for (int fd = (int)first; fd <= end; fd++) { + if (set_cloexec) { + int fl = fcntl(fd, F_GETFD); + if (fl >= 0) { + fcntl(fd, F_SETFD, fl | FD_CLOEXEC); + } + } else { + close(fd); + } + } + return 0; +} + +static __thread int t_syscall_redirect = 0; + +long syscall(long number, ...) { + va_list ap; + va_start(ap, number); + unsigned long arg1 = va_arg(ap, unsigned long); + unsigned long arg2 = va_arg(ap, unsigned long); + unsigned long arg3 = va_arg(ap, unsigned long); + unsigned long arg4 = va_arg(ap, unsigned long); + unsigned long arg5 = va_arg(ap, unsigned long); + unsigned long arg6 = va_arg(ap, unsigned long); + va_end(ap); + +#if defined(__NR_readlinkat) + if (number == __NR_readlinkat) { + return (long)readlinkat((int)arg1, (const char *)arg2, (char *)arg3, (size_t)arg4); + } +#endif +#if defined(__NR_readlink) + if (number == __NR_readlink) { + return (long)readlink((const char *)arg1, (char *)arg2, (size_t)arg3); + } +#endif +#if defined(__NR_linkat) + if (number == __NR_linkat) { + return (long)linkat((int)arg1, (const char *)arg2, (int)arg3, (const char *)arg4, (int)arg5); + } +#endif +#if defined(__NR_link) + if (number == __NR_link) { + return (long)link((const char *)arg1, (const char *)arg2); + } +#endif + +#ifdef __NR_landlock_create_ruleset + if (number == __NR_landlock_create_ruleset) { + return (long)landlock_create_ruleset((const void *)arg1, (size_t)arg2, (uint32_t)arg3); + } + if (number == __NR_landlock_add_rule) { + return (long)landlock_add_rule((int)arg1, (int)arg2, (const void *)arg3, (uint32_t)arg4); + } + if (number == __NR_landlock_restrict_self) { + return (long)landlock_restrict_self((int)arg1, (uint32_t)arg2); + } +#endif + +#ifdef __NR_seccomp + if (number == __NR_seccomp) { + return (long)seccomp((unsigned int)arg1, (unsigned int)arg2, (void *)arg3); + } +#endif + +#ifdef __NR_unshare + if (number == __NR_unshare) { + return (long)unshare((int)arg1); + } +#endif + +#ifdef __NR_setns + if (number == __NR_setns) { + return (long)setns((int)arg1, (int)arg2); + } +#endif + +#ifdef __NR_clone3 + if (number == __NR_clone3) { + errno = ENOSYS; + return -1; + } +#endif + +#ifdef __NR_pidfd_open + if (number == __NR_pidfd_open) { + errno = ENOSYS; + return -1; + } +#endif +#ifdef __NR_pidfd_send_signal + if (number == __NR_pidfd_send_signal) { + errno = ENOSYS; + return -1; + } +#endif +#ifdef __NR_pidfd_getfd + if (number == __NR_pidfd_getfd) { + errno = ENOSYS; + return -1; + } +#endif + +#ifdef __NR_close_range + if (number == __NR_close_range) { + // libblsession's close_range() calls syscall() again: the nested call goes straight to the kernel + if (t_syscall_redirect) return call_orig_syscall(number, arg1, arg2, arg3, 0, 0, 0); + t_syscall_redirect++; + long ret = (long)close_range((unsigned int)arg1, (unsigned int)arg2, (int)arg3); + t_syscall_redirect--; + return ret; + } +#endif + +#ifdef __NR_openat2 + if (number == __NR_openat2) { + // libblsession's openat2() calls syscall() again (an endless loop until the stack ran out): the nested + // call maps the path and goes straight to the kernel + if (t_syscall_redirect) { + char nested_buf[PATH_MAX]; + const char *path = (const char *)arg2; + if (path && path[0] == '/') path = rewrite_path(path, nested_buf, sizeof(nested_buf)); + return call_orig_syscall(number, arg1, (unsigned long)path, arg3, arg4, 0, 0); + } + t_syscall_redirect++; + long ret = (long)openat2((int)arg1, (const char *)arg2, (struct ddnatv_open_how *)arg3, (size_t)arg4); + t_syscall_redirect--; + return ret; + } +#endif + +#ifdef __NR_fchmodat2 + if (number == __NR_fchmodat2) { + return (long)fchmodat2((int)arg1, (const char *)arg2, (mode_t)arg3, (int)arg4); + } +#endif + +#if defined(__NR_statx) + if (number == __NR_statx) { + return (long)statx((int)arg1, (const char *)arg2, (int)arg3, (unsigned int)arg4, (struct statx *)arg5); + } +#endif + +#if defined(__NR_faccessat) + if (number == __NR_faccessat) { + if (t_syscall_redirect) { + char nested_buf[PATH_MAX]; + const char *path = (const char *)arg2; + if (path && path[0] == '/') path = rewrite_path(path, nested_buf, sizeof(nested_buf)); + return call_orig_syscall(number, arg1, (unsigned long)path, arg3, 0, 0, 0); + } + t_syscall_redirect++; + long ret = (long)faccessat((int)arg1, (const char *)arg2, (int)arg3, 0); + t_syscall_redirect--; + return ret; + } +#endif + +#if defined(__NR_faccessat2) + if (number == __NR_faccessat2) { + return (long)faccessat2((int)arg1, (const char *)arg2, (int)arg3, (int)arg4); + } +#endif + +#if defined(__NR_mknodat) + if (number == __NR_mknodat) { + return (long)mknodat((int)arg1, (const char *)arg2, (mode_t)arg3, (dev_t)arg4); + } +#endif + +#if defined(__NR_mknod) + if (number == __NR_mknod) { + return (long)mknod((const char *)arg1, (mode_t)arg2, (dev_t)arg3); + } +#endif + +#if defined(__NR_mkfifoat) + if (number == __NR_mkfifoat) { + return (long)mkfifoat((int)arg1, (const char *)arg2, (mode_t)arg3); + } +#endif + +#if defined(__NR_setresuid) + if (number == __NR_setresuid) return 0; +#endif +#if defined(__NR_setresuid32) + if (number == __NR_setresuid32) return 0; +#endif +#if defined(__NR_setresgid) + if (number == __NR_setresgid) return 0; +#endif +#if defined(__NR_setresgid32) + if (number == __NR_setresgid32) return 0; +#endif +#if defined(__NR_setuid) + if (number == __NR_setuid) return 0; +#endif +#if defined(__NR_setuid32) + if (number == __NR_setuid32) return 0; +#endif +#if defined(__NR_setgid) + if (number == __NR_setgid) return 0; +#endif +#if defined(__NR_setgid32) + if (number == __NR_setgid32) return 0; +#endif +#if defined(__NR_setreuid) + if (number == __NR_setreuid) return 0; +#endif +#if defined(__NR_setreuid32) + if (number == __NR_setreuid32) return 0; +#endif +#if defined(__NR_setregid) + if (number == __NR_setregid) return 0; +#endif +#if defined(__NR_setregid32) + if (number == __NR_setregid32) return 0; +#endif +#if defined(__NR_setgroups) + if (number == __NR_setgroups) return 0; +#endif +#if defined(__NR_setgroups32) + if (number == __NR_setgroups32) return 0; +#endif +#if defined(__NR_setfsuid) + if (number == __NR_setfsuid) return 0; +#endif +#if defined(__NR_setfsuid32) + if (number == __NR_setfsuid32) return 0; +#endif +#if defined(__NR_setfsgid) + if (number == __NR_setfsgid) return 0; +#endif +#if defined(__NR_setfsgid32) + if (number == __NR_setfsgid32) return 0; +#endif +#if defined(__NR_getuid) + if (number == __NR_getuid) return 0; +#endif +#if defined(__NR_getuid32) + if (number == __NR_getuid32) return 0; +#endif +#if defined(__NR_geteuid) + if (number == __NR_geteuid) return 0; +#endif +#if defined(__NR_geteuid32) + if (number == __NR_geteuid32) return 0; +#endif +#if defined(__NR_getgid) + if (number == __NR_getgid) return 0; +#endif +#if defined(__NR_getgid32) + if (number == __NR_getgid32) return 0; +#endif +#if defined(__NR_getegid) + if (number == __NR_getegid) return 0; +#endif +#if defined(__NR_getegid32) + if (number == __NR_getegid32) return 0; +#endif +#if defined(__NR_getresuid) + if (number == __NR_getresuid) { + uid_t *ruid = (uid_t *)arg1; + uid_t *euid = (uid_t *)arg2; + uid_t *suid = (uid_t *)arg3; + if (ruid) *ruid = 0; + if (euid) *euid = 0; + if (suid) *suid = 0; + return 0; + } +#endif +#if defined(__NR_getresuid32) + if (number == __NR_getresuid32) { + uid_t *ruid = (uid_t *)arg1; + uid_t *euid = (uid_t *)arg2; + uid_t *suid = (uid_t *)arg3; + if (ruid) *ruid = 0; + if (euid) *euid = 0; + if (suid) *suid = 0; + return 0; + } +#endif +#if defined(__NR_getresgid) + if (number == __NR_getresgid) { + gid_t *rgid = (gid_t *)arg1; + gid_t *egid = (gid_t *)arg2; + gid_t *sgid = (gid_t *)arg3; + if (rgid) *rgid = 0; + if (egid) *egid = 0; + if (sgid) *sgid = 0; + return 0; + } +#endif +#if defined(__NR_getresgid32) + if (number == __NR_getresgid32) { + gid_t *rgid = (gid_t *)arg1; + gid_t *egid = (gid_t *)arg2; + gid_t *sgid = (gid_t *)arg3; + if (rgid) *rgid = 0; + if (egid) *egid = 0; + if (sgid) *sgid = 0; + return 0; + } +#endif + + return call_orig_syscall(number, arg1, arg2, arg3, arg4, arg5, arg6); +} + +#endif /* !DDNATV_HOST_TEST */ diff --git a/tools/nativeguest/hook/setup.c b/tools/nativeguest/hook/setup.c new file mode 100644 index 0000000000..90c5d87afe --- /dev/null +++ b/tools/nativeguest/hook/setup.c @@ -0,0 +1,384 @@ +// Part of libddnatv-hook.so, included by ddnatv-hook.c (one translation unit, so the +// parts share their static functions and are built with the same command as before). +// +// Start-up (DDNATV_ROOT, certificate and time zone defaults), /proc/self/exe, rewrite_path() and +// the short names of Unix sockets. + +#ifndef DDNATV_HOST_TEST + +static char g_real_exe[PATH_MAX] = {0}; +// DDNATV_PRELOAD: guest paths of libraries every program loads before the hook (what +// /etc/ld.so.preload does for programs run by the rootfs's own ld.so), and the same as host paths. +static char *g_preload_guest = NULL; +static char *g_preload_host = NULL; +static int g_initialized = 0; + +static void init_ddnatv_hook(void) { + if (g_initialized) return; + const char *root = getenv("DDNATV_ROOT"); + if (root && strlen(root) > 0) { + size_t len = strlen(root); + if (len > sizeof(g_ddnatv_root) - 1) len = sizeof(g_ddnatv_root) - 1; + if (root[len - 1] == '/') len--; + ddnatv_set_root(root, len); + } else { + Dl_info info; + if (dladdr((void *)init_ddnatv_hook, &info) && info.dli_fname) { + const char *p = strstr(info.dli_fname, "/usr/lib/libddnatv-hook"); + if (p && p > info.dli_fname) { + size_t len = p - info.dli_fname; + if (len < sizeof(g_ddnatv_root)) { + ddnatv_set_root(info.dli_fname, len); + } + } + } + } + ddnatv_load_binds(getenv("DDNATV_BINDS")); + const char *hostname = getenv("DDNATV_HOSTNAME"); + if (hostname && hostname[0] && strlen(hostname) < sizeof(g_hostname)) ddnatv_copy_cstr(g_hostname, sizeof(g_hostname), hostname); + const char *link_root = getenv("DDNATV_LINK_ROOT"); + if (link_root && link_root[0] == '/' && strlen(link_root) < sizeof(g_link_root)) { + ddnatv_copy_cstr(g_link_root, sizeof(g_link_root), link_root); + g_link_root_len = strlen(g_link_root); + while (g_link_root_len > 1 && g_link_root[g_link_root_len - 1] == '/') g_link_root[--g_link_root_len] = 0; + } + const char *preload = getenv("DDNATV_PRELOAD"); + if (preload && preload[0] && g_ddnatv_root_len > 0) { + size_t libs = 1; + for (const char *p = preload; *p; p++) libs += *p == ':'; + size_t cap = libs * (PATH_MAX + 1); + char *host = malloc(cap); + if (host) { + size_t used = 0; + host[0] = '\0'; + char *copy = strdup(preload); + char *save = NULL; + for (char *lib = copy ? strtok_r(copy, ":", &save) : NULL; lib; lib = strtok_r(NULL, ":", &save)) { + if (lib[0] != '/') continue; + char mapped_buf[PATH_MAX]; + const char *mapped = rewrite_path_in_root(lib, mapped_buf, sizeof(mapped_buf)); + size_t mapped_len = strlen(mapped); + if (used + mapped_len + 2 > cap) break; + if (used) host[used++] = ':'; + used = ddnatv_append(host, cap, used, mapped, mapped_len); + } + free(copy); + g_preload_host = host; + g_preload_guest = strdup(preload); + } + } + const char *curr_real_exe = getenv("DDNATV_REAL_EXE"); + if (curr_real_exe && curr_real_exe[0] != '\0') { + ddnatv_copy_cstr(g_real_exe, sizeof(g_real_exe), curr_real_exe); + } + if (g_ddnatv_root[0] != '\0') { + const char *curr_tzdir = getenv("TZDIR"); + if (!curr_tzdir || curr_tzdir[0] == '\0') { + char tzdir_buf[PATH_MAX]; + snprintf(tzdir_buf, sizeof(tzdir_buf), "%s/usr/share/zoneinfo", g_ddnatv_root); + setenv("TZDIR", tzdir_buf, 0); + } + + const char *curr_cert = getenv("SSL_CERT_FILE"); + if (!curr_cert || curr_cert[0] == '\0' || strcmp(curr_cert, "/etc/ssl/certs/ca-certificates.crt") == 0) { + char cert_buf[PATH_MAX]; + snprintf(cert_buf, sizeof(cert_buf), "%s/etc/ssl/certs/ca-certificates.crt", g_ddnatv_root); + setenv("SSL_CERT_FILE", cert_buf, 1); + } + + const char *curr_cert_dir = getenv("SSL_CERT_DIR"); + if (!curr_cert_dir || curr_cert_dir[0] == '\0' || strcmp(curr_cert_dir, "/etc/ssl/certs") == 0) { + char cert_dir_buf[PATH_MAX]; + snprintf(cert_dir_buf, sizeof(cert_dir_buf), "%s/etc/ssl/certs:/system/etc/security/cacerts", g_ddnatv_root); + setenv("SSL_CERT_DIR", cert_dir_buf, 1); + } + + const char *curr_curl = getenv("CURL_CA_BUNDLE"); + if (!curr_curl || curr_curl[0] == '\0' || strcmp(curr_curl, "/etc/ssl/certs/ca-certificates.crt") == 0) { + char curl_buf[PATH_MAX]; + snprintf(curl_buf, sizeof(curl_buf), "%s/etc/ssl/certs/ca-certificates.crt", g_ddnatv_root); + setenv("CURL_CA_BUNDLE", curl_buf, 1); + } + + const char *curr_godebug = getenv("GODEBUG"); + if (!curr_godebug || curr_godebug[0] == '\0') { + setenv("GODEBUG", "netdns=cgo", 0); + } else if (strstr(curr_godebug, "netdns") == NULL) { + char godebug_buf[512]; + snprintf(godebug_buf, sizeof(godebug_buf), "%s,netdns=cgo", curr_godebug); + setenv("GODEBUG", godebug_buf, 1); + } + } + g_initialized = 1; +} + +// Before the program runs: init_ddnatv_hook() sets variables, and setenv() may move the block the +// environment is kept in. A program that had changed its environment (putenv, setenv) and passed it +// to execv() before its first call into the hook handed the exec code a block that was freed by the +// time it was read. +__attribute__((constructor(103))) static void init_ddnatv_hook_early(void) { + init_ddnatv_hook(); + // The kernel names the process after what it executed, the rootfs ld.so: process monitors, and + // the app's own checks of /proc//comm (steam, wineserver), saw only "ld-linux-aarch64.so.1" + const char *name = program_invocation_short_name; + if (g_ddnatv_root[0] != '\0' && name && name[0] && strncmp(name, "ld-linux", 8) != 0) { + prctl(PR_SET_NAME, (unsigned long)name, 0, 0, 0); + } +} + +void tzset(void) { + static void (*orig_tzset)(void) = NULL; + if (!orig_tzset) orig_tzset = (void (*)(void))dlsym(RTLD_NEXT, "tzset"); + init_ddnatv_hook(); + if (g_ddnatv_root[0] != '\0') { + const char *curr_tzdir = getenv("TZDIR"); + if (!curr_tzdir || curr_tzdir[0] == '\0') { + char tzdir_buf[PATH_MAX]; + snprintf(tzdir_buf, sizeof(tzdir_buf), "%s/usr/share/zoneinfo", g_ddnatv_root); + setenv("TZDIR", tzdir_buf, 0); + } + } + if (orig_tzset) orig_tzset(); +} + +static int is_proc_self_exe(const char *path) { + if (!path) return 0; + if (strcmp(path, "/proc/self/exe") == 0 || + strcmp(path, "/proc/thread-self/exe") == 0) { + return 1; + } + if (strncmp(path, "/proc/", 6) == 0) { + const char *p = path + 6; + while (*p >= '0' && *p <= '9') p++; + if (strcmp(p, "/exe") == 0) { + pid_t pid = (pid_t)atoi(path + 6); + if (pid == getpid()) { + return 1; + } + } + } + return 0; +} + +static ssize_t handle_proc_self_exe(char *buf, size_t bufsiz) { + if (!buf || bufsiz == 0) { + errno = EINVAL; + return -1; + } + init_ddnatv_hook(); + if (g_real_exe[0] == '\0') { + int fd = open("/proc/self/cmdline", O_RDONLY); + if (fd >= 0) { + char cmdline[8192]; + ssize_t n = read(fd, cmdline, sizeof(cmdline) - 1); + close(fd); + if (n > 0) { + cmdline[n] = '\0'; + char *args[64] = {0}; + int ac = 0; + char *p = cmdline; + while (p < cmdline + n && ac < 64) { + args[ac++] = p; + p += strlen(p) + 1; + } + for (int i = 0; i < ac; i++) { + if (strcmp(args[i], "--argv0") == 0 && i + 2 < ac) { + strncpy(g_real_exe, args[i + 2], sizeof(g_real_exe) - 1); + g_real_exe[sizeof(g_real_exe) - 1] = '\0'; + break; + } + } + if (g_real_exe[0] == '\0' && ac > 0 && args[0] && args[0][0] != '\0') { + strncpy(g_real_exe, args[0], sizeof(g_real_exe) - 1); + g_real_exe[sizeof(g_real_exe) - 1] = '\0'; + } + } + } + } + + if (g_real_exe[0] != '\0') { + char stripped[PATH_MAX]; + const char *out_path = g_real_exe; + if (g_ddnatv_root[0] != '\0') { + size_t rlen = g_ddnatv_root_len; + if (strncmp(g_real_exe, g_ddnatv_root, rlen) == 0 && + (g_real_exe[rlen] == '/' || g_real_exe[rlen] == '\0')) { + out_path = g_real_exe + rlen; + if (out_path[0] == '\0') out_path = "/"; + } + } + strncpy(stripped, out_path, sizeof(stripped) - 1); + stripped[sizeof(stripped) - 1] = '\0'; + + size_t len = strlen(stripped); + size_t copy_len = (len < bufsiz) ? len : bufsiz; + memcpy(buf, stripped, copy_len); + return (ssize_t)copy_len; + } + return -1; +} + +static const char *rewrite_path(const char *path, char *buffer, size_t bufsize) { + if (!path) return NULL; + // Relative paths must NEVER be rewritten to rootfs root + if (path[0] != '/') return path; + init_ddnatv_hook(); + return rewrite_path_in_root(path, buffer, bufsize); +} + +// rewrite_path() for calls that follow a symbolic link in the last component. In a mount whose +// source is elsewhere the kernel would follow the link's absolute guest target on Android's own +// tree (a Flatpak app's /app/bin/PINBALL.DAT -> /app/extra/Pinball/PINBALL.DAT), so the link is +// followed in the guest's view first. The rootfs's own links were made relative already. +static const char *rewrite_path_follow(const char *path, char *buffer, size_t bufsize) { + if (!path) return NULL; + if (path[0] != '/') return path; + init_ddnatv_hook(); + int moved = 0; + for (int i = 0; i < g_nbinds; i++) { + const struct ddnatv_bind *b = &g_binds[i]; + if (path[1] == b->dst[1] && is_path_prefix(path, b->dst, b->dst_len)) { moved = !b->identity; break; } + } + char from_source[PATH_MAX]; + if (!moved) { + // or a mount's real place, as /proc/self/exe and getcwd() give it (a Flatpak app's checkout) + const struct ddnatv_bind *best = NULL; + for (int i = 0; i < g_nbinds; i++) { + const struct ddnatv_bind *b = &g_binds[i]; + if (b->identity || !is_path_prefix(path, b->src, b->src_len)) continue; + if (!best || b->src_len > best->src_len) best = b; + } + if (!best) { + const char *host = rewrite_path_in_root(path, buffer, bufsize); +#if defined(__aarch64__) + // A link in the rootfs with an absolute guest target that the hook did not write (one made + // under proot, or copied from Proton's template prefix, as Wine's "z:" -> "/") is followed + // by the kernel on Android's own tree: Wine found no drive for any path and gave games + // \\?\unix\ names, which an NW.js game could not load its DLLs from. + if (is_android_host_path(path)) return host; + char target[PATH_MAX]; + long n = raw_readlinkat(AT_FDCWD, host, target, sizeof(target) - 1); + if (n <= 0 || target[0] != '/') return host; + target[n] = '\0'; + if (is_android_host_path(target) || + (g_ddnatv_root_len && strncmp(target, g_ddnatv_root, g_ddnatv_root_len) == 0)) return host; + char guest_path[PATH_MAX]; + ddnatv_copy_cstr(guest_path, sizeof(guest_path), path); + char guest_target[PATH_MAX]; + const char *followed = follow_guest_link(guest_path, guest_target, sizeof(guest_target)); + char mapped[PATH_MAX]; + ddnatv_copy_cstr(buffer, bufsize, rewrite_path_in_root(followed, mapped, sizeof(mapped))); + return buffer; +#else + return host; +#endif + } + size_t n = ddnatv_append(from_source, sizeof(from_source), 0, best->dst, best->dst_len); + ddnatv_append(from_source, sizeof(from_source), n, path + best->src_len, strlen(path + best->src_len)); + path = from_source; + } + char guest[PATH_MAX]; + const char *followed = follow_guest_link(path, guest, sizeof(guest)); + char mapped[PATH_MAX]; + const char *host = rewrite_path_in_root(followed, mapped, sizeof(mapped)); + // the result may be `followed` or `path` itself, which can live here + ddnatv_copy_cstr(buffer, bufsize, host); + return buffer; +} + +// Whether the rootfs keeps bin (pair DDNATV_USRMERGE_BIN) or sbin as a symlink into /usr, as +// Ubuntu 24.04 does. Checked once per process with one readlink(). +static int rootfs_pair_is_merged(int pair) { + static int known[3] = { 0, -1, -1 }; + if (known[pair] >= 0) return known[pair]; + static ssize_t (*orig_readlink)(const char *, char *, size_t) = NULL; + if (!orig_readlink) orig_readlink = (ssize_t (*)(const char *, char *, size_t))dlsym(RTLD_NEXT, "readlink"); + const char *dir = (pair == DDNATV_USRMERGE_BIN) ? "/bin" : "/sbin"; + const char *usr_dir = (pair == DDNATV_USRMERGE_BIN) ? "usr/bin" : "usr/sbin"; + char path[PATH_MAX]; + char link[16]; + size_t n = ddnatv_append(path, sizeof(path), 0, g_ddnatv_root, g_ddnatv_root_len); + ddnatv_append(path, sizeof(path), n, dir, strlen(dir)); + int saved_errno = errno; + ssize_t len = orig_readlink ? orig_readlink(path, link, sizeof(link) - 1) : -1; + errno = saved_errno; + int merged = 0; + if (len > 0) { + link[len] = '\0'; + size_t ulen = strlen(usr_dir); + merged = strncmp(link, usr_dir, ulen) == 0 && (link[ulen] == '\0' || (link[ulen] == '/' && link[ulen + 1] == '\0')); + } + known[pair] = merged; + return merged; +} + +// The other spelling of a missing rootfs path under bin/sbin, or NULL when there is none or both +// spellings reach the same directory (the retry could only fail again). +static const char *usrmerge_retry_path(const char *target, char *alt, size_t altsz) { + const char *sub = get_rootfs_subpath(target); + if (!sub) return NULL; + int pair = usrmerge_alt_path(sub, alt, altsz); + if (pair == 0 || rootfs_pair_is_merged(pair)) return NULL; + return alt; +} + +static const char *rewrite_unix_socket_path(const char *sun_path, char *out_buf, size_t out_size) { + if (!sun_path || out_size == 0) return sun_path; + // Abstract sockets start with null byte (\0); leave untouched + if (sun_path[0] == '\0') { + return sun_path; + } + + char full[PATH_MAX]; + const char *rw = (sun_path[0] == '/') ? rewrite_path(sun_path, full, sizeof(full)) : sun_path; + size_t rw_len = strlen(rw); + + // If rewritten path fits safely in sockaddr_un.sun_path (usually 108 bytes) + if (rw_len < out_size) { + strncpy(out_buf, rw, out_size - 1); + out_buf[out_size - 1] = '\0'; + return out_buf; + } + + // Path exceeds sockaddr_un 108-byte limit! + // Deterministically hash the path into /tmp to guarantee it fits safely (< 80 bytes) + // while remaining 100% deterministic between server and client. + unsigned long long hash = 14695981039346656037ULL; + for (const char *p = rw; *p; p++) { + hash ^= (unsigned char)(*p); + hash *= 1099511628211ULL; + } + + init_ddnatv_hook(); + if (g_ddnatv_root[0] != '\0') { + snprintf(out_buf, out_size, "%s/tmp/.ctx_sock_%016llx", g_ddnatv_root, hash); + } else { + snprintf(out_buf, out_size, "/tmp/.ctx_sock_%016llx", hash); + } + out_buf[out_size - 1] = '\0'; + return out_buf; +} + +static void unlink_mapped_unix_socket(const char *target) { + if (!target) return; + size_t t_len = strlen(target); + if (t_len >= 107) { + unsigned long long hash = 14695981039346656037ULL; + for (const char *p = target; *p; p++) { + hash ^= (unsigned char)(*p); + hash *= 1099511628211ULL; + } + char short_path[PATH_MAX]; + init_ddnatv_hook(); + if (g_ddnatv_root[0] != '\0') { + snprintf(short_path, sizeof(short_path), "%s/tmp/.ctx_sock_%016llx", g_ddnatv_root, hash); + } else { + snprintf(short_path, sizeof(short_path), "/tmp/.ctx_sock_%016llx", hash); + } + static int (*orig_unlink)(const char *) = NULL; + if (!orig_unlink) orig_unlink = (int (*)(const char *))dlsym(RTLD_NEXT, "unlink"); + if (orig_unlink) orig_unlink(short_path); + } +} + +#endif /* !DDNATV_HOST_TEST */ diff --git a/tools/nativeguest/hook/signals.c b/tools/nativeguest/hook/signals.c new file mode 100644 index 0000000000..0b5765070d --- /dev/null +++ b/tools/nativeguest/hook/signals.c @@ -0,0 +1,233 @@ +// Part of libddnatv-hook.so, included by ddnatv-hook.c (one translation unit, so the +// parts share their static functions and are built with the same command as before). +// +// The SIGSYS handler for system calls Android's seccomp filter blocks, and the signal hooks that +// keep programs from replacing or blocking it. + +#ifndef DDNATV_HOST_TEST + +#ifndef SYS_SECCOMP +#define SYS_SECCOMP 1 +#endif + +static int (*get_real_sigaction(void))(int, const struct sigaction *, struct sigaction *); + +// A program's own SIGSYS handler, for the traps of a seccomp filter it installed itself (Firefox's +// and Chromium's sandboxes emulate the calls they trap there). Android's app filter traps with data +// 0; such a sandbox's traps carry their own nonzero data, which the kernel gives as si_errno. +static struct sigaction g_app_sigsys; +static int g_app_sigsys_set = 0; + +static void remember_app_sigsys(const struct sigaction *act, struct sigaction *oldact) { + if (oldact) { + if (g_app_sigsys_set) { + *oldact = g_app_sigsys; + } else { + memset(oldact, 0, sizeof(*oldact)); + oldact->sa_handler = SIG_DFL; + } + } + if (act) { + g_app_sigsys = *act; + g_app_sigsys_set = act->sa_handler != SIG_DFL && act->sa_handler != SIG_IGN; + } +} + +// Intercept SECCOMP blocked syscalls (SIGSYS), resolve sandbox/landlock gracefully, and advance PC +static void ddnatv_sigsys_handler(int sig, siginfo_t *info, void *ctx) { + if (!ctx || !info) return; + if (info->si_code != SYS_SECCOMP) { + int (*real_sig)(int, const struct sigaction *, struct sigaction *) = get_real_sigaction(); + if (real_sig) { + struct sigaction sa_dfl; + memset(&sa_dfl, 0, sizeof(sa_dfl)); + sa_dfl.sa_handler = SIG_DFL; + sigemptyset(&sa_dfl.sa_mask); + real_sig(sig, &sa_dfl, NULL); + } + raise(sig); + return; + } + if (info->si_errno != 0 && g_app_sigsys_set) { + if (g_app_sigsys.sa_flags & SA_SIGINFO) g_app_sigsys.sa_sigaction(sig, info, ctx); + else g_app_sigsys.sa_handler(sig); + return; + } + ucontext_t *uctx = (ucontext_t *)ctx; + + int sys_nr = info->si_syscall; + long ret_val = is_sigsys_synthetic_success_syscall(sys_nr) ? 0 : -ENOSYS; + +#if defined(__aarch64__) + uctx->uc_mcontext.regs[0] = (uint64_t)ret_val; + if (uctx->uc_mcontext.pc >= 4) { + uint32_t insn_at_pc = 0; + uint32_t insn_before_pc = 0; + memcpy(&insn_at_pc, (const void *)uctx->uc_mcontext.pc, sizeof(insn_at_pc)); + memcpy(&insn_before_pc, (const void *)(uctx->uc_mcontext.pc - 4), sizeof(insn_before_pc)); + // On SECCOMP_RET_TRAP, the kernel already advanced PC past svc #0 (*(pc - 4) is svc #0). + // Only advance PC if PC itself points to svc #0 and *(pc - 4) does not. + int at_is_svc = ((insn_at_pc & 0xffe0001fU) == 0xd4000001U); + int prev_is_svc = ((insn_before_pc & 0xffe0001fU) == 0xd4000001U); + if (at_is_svc && !prev_is_svc) { + uctx->uc_mcontext.pc += 4; + } + } +#elif defined(__arm__) + uctx->uc_mcontext.arm_r0 = (unsigned long)ret_val; + if (uctx->uc_mcontext.arm_pc >= 4) { + if (uctx->uc_mcontext.arm_cpsr & 0x20) { + uint16_t insn_at_pc = 0; + uint16_t insn_before_pc = 0; + memcpy(&insn_at_pc, (const void *)uctx->uc_mcontext.arm_pc, sizeof(insn_at_pc)); + memcpy(&insn_before_pc, (const void *)(uctx->uc_mcontext.arm_pc - 2), sizeof(insn_before_pc)); + int at_is_svc = ((insn_at_pc & 0xff00U) == 0xdf00U); + int prev_is_svc = ((insn_before_pc & 0xff00U) == 0xdf00U); + if (at_is_svc && !prev_is_svc) { + uctx->uc_mcontext.arm_pc += 2; + } + } else { + uint32_t insn_at_pc = 0; + uint32_t insn_before_pc = 0; + memcpy(&insn_at_pc, (const void *)uctx->uc_mcontext.arm_pc, sizeof(insn_at_pc)); + memcpy(&insn_before_pc, (const void *)(uctx->uc_mcontext.arm_pc - 4), sizeof(insn_before_pc)); + int at_is_svc = ((insn_at_pc & 0x0f000000U) == 0x0f000000U); + int prev_is_svc = ((insn_before_pc & 0x0f000000U) == 0x0f000000U); + if (at_is_svc && !prev_is_svc) { + uctx->uc_mcontext.arm_pc += 4; + } + } + } +#elif defined(__x86_64__) && defined(REG_RAX) + uctx->uc_mcontext.gregs[REG_RAX] = (greg_t)ret_val; + if (uctx->uc_mcontext.gregs[REG_RIP] >= 2) { + unsigned char insn_at[2] = {0}; + unsigned char insn_prev[2] = {0}; + memcpy(insn_at, (const void *)uctx->uc_mcontext.gregs[REG_RIP], sizeof(insn_at)); + memcpy(insn_prev, (const void *)(uctx->uc_mcontext.gregs[REG_RIP] - 2), sizeof(insn_prev)); + int at_is_sys = (insn_at[0] == 0x0f && insn_at[1] == 0x05); + int prev_is_sys = (insn_prev[0] == 0x0f && insn_prev[1] == 0x05); + if (at_is_sys && !prev_is_sys) { + uctx->uc_mcontext.gregs[REG_RIP] += 2; + } + } +#elif defined(__i386__) && defined(REG_EAX) + uctx->uc_mcontext.gregs[REG_EAX] = (greg_t)ret_val; + if (uctx->uc_mcontext.gregs[REG_EIP] >= 2) { + unsigned char insn_at[2] = {0}; + unsigned char insn_prev[2] = {0}; + memcpy(insn_at, (const void *)uctx->uc_mcontext.gregs[REG_EIP], sizeof(insn_at)); + memcpy(insn_prev, (const void *)(uctx->uc_mcontext.gregs[REG_EIP] - 2), sizeof(insn_prev)); + int at_is_sys = (insn_at[0] == 0xcd && insn_at[1] == 0x80); + int prev_is_sys = (insn_prev[0] == 0xcd && insn_prev[1] == 0x80); + if (at_is_sys && !prev_is_sys) { + uctx->uc_mcontext.gregs[REG_EIP] += 2; + } + } +#endif +} + +static int (*get_real_sigaction(void))(int, const struct sigaction *, struct sigaction *) { + static int (*real_sigaction)(int, const struct sigaction *, struct sigaction *) = NULL; + if (!real_sigaction) { + real_sigaction = (int (*)(int, const struct sigaction *, struct sigaction *))dlsym(RTLD_NEXT, "sigaction"); + if (!real_sigaction) { + real_sigaction = (int (*)(int, const struct sigaction *, struct sigaction *))dlsym(RTLD_DEFAULT, "sigaction"); + } + } + return real_sigaction; +} + +__attribute__((constructor(101))) static void install_sigsys_handler(void) { + int (*real_sig)(int, const struct sigaction *, struct sigaction *) = get_real_sigaction(); + if (!real_sig) return; + + struct sigaction sa; + memset(&sa, 0, sizeof(sa)); + sa.sa_sigaction = ddnatv_sigsys_handler; + sa.sa_flags = SA_SIGINFO | SA_RESTART; + sigemptyset(&sa.sa_mask); + real_sig(SIGSYS, &sa, NULL); +} + +int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact) { + int (*real_sig)(int, const struct sigaction *, struct sigaction *) = get_real_sigaction(); + if (!real_sig) return -1; + + if (signum == SIGSYS) { + if (act && act->sa_sigaction == ddnatv_sigsys_handler) { + return real_sig(signum, act, oldact); + } + remember_app_sigsys(act, oldact); + return 0; + } + return real_sig(signum, act, oldact); +} + +int rt_sigaction(int signum, const struct sigaction *act, struct sigaction *oldact, size_t sigsetsize) { + static int (*orig_rt_sigaction)(int, const struct sigaction *, struct sigaction *, size_t) = NULL; + if (!orig_rt_sigaction) orig_rt_sigaction = (int (*)(int, const struct sigaction *, struct sigaction *, size_t))dlsym(RTLD_NEXT, "rt_sigaction"); + + if (signum == SIGSYS) { + if (act && act->sa_sigaction == ddnatv_sigsys_handler) { + return orig_rt_sigaction ? orig_rt_sigaction(signum, act, oldact, sigsetsize) : 0; + } + remember_app_sigsys(act, oldact); + return 0; + } + return orig_rt_sigaction ? orig_rt_sigaction(signum, act, oldact, sigsetsize) : -1; +} + +int __libc_sigaction(int signum, const struct sigaction *act, struct sigaction *oldact) { + return sigaction(signum, act, oldact); +} + +typedef void (*sighandler_t)(int); +sighandler_t signal(int signum, sighandler_t handler) { + static sighandler_t (*orig_signal)(int, sighandler_t) = NULL; + if (!orig_signal) orig_signal = (sighandler_t (*)(int, sighandler_t))dlsym(RTLD_NEXT, "signal"); + if (signum == SIGSYS) { + struct sigaction act, old; + memset(&act, 0, sizeof(act)); + act.sa_handler = handler; + remember_app_sigsys(&act, &old); + return old.sa_handler; + } + return orig_signal ? orig_signal(signum, handler) : NULL; +} + +int sigprocmask(int how, const sigset_t *set, sigset_t *oldset) { + static int (*orig_sigprocmask)(int, const sigset_t *, sigset_t *) = NULL; + if (!orig_sigprocmask) orig_sigprocmask = (int (*)(int, const sigset_t *, sigset_t *))dlsym(RTLD_NEXT, "sigprocmask"); + if (set && (how == SIG_BLOCK || how == SIG_SETMASK)) { + sigset_t mod_set = *set; + sigdelset(&mod_set, SIGSYS); + return orig_sigprocmask ? orig_sigprocmask(how, &mod_set, oldset) : 0; + } + return orig_sigprocmask ? orig_sigprocmask(how, set, oldset) : 0; +} + +int pthread_sigmask(int how, const sigset_t *set, sigset_t *oldset) { + static int (*orig_pthread_sigmask)(int, const sigset_t *, sigset_t *) = NULL; + if (!orig_pthread_sigmask) orig_pthread_sigmask = (int (*)(int, const sigset_t *, sigset_t *))dlsym(RTLD_NEXT, "pthread_sigmask"); + if (set && (how == SIG_BLOCK || how == SIG_SETMASK)) { + sigset_t mod_set = *set; + sigdelset(&mod_set, SIGSYS); + return orig_pthread_sigmask ? orig_pthread_sigmask(how, &mod_set, oldset) : 0; + } + return orig_pthread_sigmask ? orig_pthread_sigmask(how, set, oldset) : 0; +} + +int rt_sigprocmask(int how, const sigset_t *set, sigset_t *oldset, size_t sigsetsize) { + static int (*orig_rt_sigprocmask)(int, const sigset_t *, sigset_t *, size_t) = NULL; + if (!orig_rt_sigprocmask) orig_rt_sigprocmask = (int (*)(int, const sigset_t *, sigset_t *, size_t))dlsym(RTLD_NEXT, "rt_sigprocmask"); + if (set && (how == SIG_BLOCK || how == SIG_SETMASK)) { + sigset_t mod_set; + memcpy(&mod_set, set, sizeof(mod_set)); + sigdelset(&mod_set, SIGSYS); + return orig_rt_sigprocmask ? orig_rt_sigprocmask(how, &mod_set, oldset, sigsetsize) : 0; + } + return orig_rt_sigprocmask ? orig_rt_sigprocmask(how, set, oldset, sigsetsize) : 0; +} + +#endif /* !DDNATV_HOST_TEST */ diff --git a/tools/nativeguest/hook/sockets.c b/tools/nativeguest/hook/sockets.c new file mode 100644 index 0000000000..2903505f9b --- /dev/null +++ b/tools/nativeguest/hook/sockets.c @@ -0,0 +1,351 @@ +// Part of libddnatv-hook.so, included by ddnatv-hook.c (one translation unit, so the +// parts share their static functions and are built with the same command as before). +// +// Sockets: Unix socket paths in the rootfs, the redirected DNS queries of loopback DNS, and the +// TCP servers programs start (the app offers to open them in the browser). + +// The TCP port a listening socket was bound to, 0 for other sockets. +static int listening_tcp_port(int fd) { + struct sockaddr_storage ss; + socklen_t len = sizeof(ss); + memset(&ss, 0, sizeof(ss)); + if (syscall(SYS_getsockname, fd, (struct sockaddr *)&ss, &len) != 0) return 0; + if (ss.ss_family == AF_INET && len >= (socklen_t)sizeof(struct sockaddr_in)) + return ntohs(((struct sockaddr_in *)&ss)->sin_port); + if (ss.ss_family == AF_INET6 && len >= (socklen_t)sizeof(struct sockaddr_in6)) + return ntohs(((struct sockaddr_in6 *)&ss)->sin6_port); + return 0; +} + +// Tells the app that this program listens on [port]: $DDNATV_ROOT/run/ddnatv/listening/PORT with +// "PID NAME" (ListeningPorts). Raw system calls: nothing of this library runs again. +static void report_listening_port(const char *root, size_t root_len, int port, const char *name) { + if (root_len == 0 || port <= 0 || port > 65535) return; + char path[PATH_MAX]; + int n = snprintf(path, sizeof(path), "%.*s/run/ddnatv/listening/%d", (int)root_len, root, port); + if (n <= 0 || n >= (int)sizeof(path)) return; + int out = (int)syscall(SYS_openat, AT_FDCWD, path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0600); + if (out < 0) return; // no app watching (the directory is the app's) + char line[160]; + int m = snprintf(line, sizeof(line), "%d %.100s\n", (int)getpid(), name && name[0] ? name : "?"); + if (m > 0) syscall(SYS_write, out, line, (size_t)(m < (int)sizeof(line) ? m : (int)sizeof(line) - 1)); + syscall(SYS_close, out); +} + +// The first port of the kernel's range for ports it picks itself, from the text of +// /proc/sys/net/ipv4/ip_local_port_range ("32768\t60999"); 32768, Linux's default, when there is none. +static int ephemeral_port_low(const char *range) { + if (!range) return 32768; + while (*range == ' ' || *range == '\t') range++; + int low = 0, digits = 0; + while (*range >= '0' && *range <= '9' && digits < 6) low = low * 10 + (*range++ - '0'), digits++; + return (digits > 0 && low > 0 && low <= 65535) ? low : 32768; +} + +// Whether listen() tells the app about a server on `port`, which it then connects to and sends an +// HTTP request (LocalServers). Only for a port the program chose (`chosen`) below the kernel's own +// range: a server whose port the kernel picked, or one in that range, is how programs talk to +// each other (Gradle's daemon, the Kotlin daemon, a JVM's debugger, test runners). Such a server +// often accepts one connection, so the app's own took the place of the program it waited for: +// that one read EOF ("Did not receive connection preamble" from Gradle, #51). Web servers people +// open (npm run dev, vite, python3 -m http.server, jupyter) are on ports such as 3000, 5173, 8000. +static int listening_port_is_reported(int chosen, int port, int ephemeral_low) { + return chosen && port > 0 && port < ephemeral_low; +} + +#ifndef DDNATV_HOST_TEST + +// The kernel's range, read once (raw system calls: nothing of this library runs again) +static int kernel_ephemeral_port_low(void) { + static int low = 0; + int v = __atomic_load_n(&low, __ATOMIC_RELAXED); + if (v) return v; + char text[64]; + ssize_t n = -1; + int fd = (int)syscall(SYS_openat, AT_FDCWD, "/proc/sys/net/ipv4/ip_local_port_range", O_RDONLY | O_CLOEXEC); + if (fd >= 0) { + n = syscall(SYS_read, fd, text, sizeof(text) - 1); + syscall(SYS_close, fd); + } + if (n > 0) text[n] = '\0'; + v = ephemeral_port_low(n > 0 ? text : NULL); + __atomic_store_n(&low, v, __ATOMIC_RELAXED); + return v; +} + +int listen(int sockfd, int backlog) { + static int (*orig_listen)(int, int) = NULL; + if (!orig_listen) orig_listen = (int (*)(int, int))dlsym(RTLD_NEXT, "listen"); + if (!orig_listen) { + errno = ENOSYS; + return -1; + } + int ret = orig_listen(sockfd, backlog); + if (ret == 0) { + int saved_errno = errno; + int port = is_chosen_port_fd(sockfd) ? listening_tcp_port(sockfd) : 0; + if (listening_port_is_reported(port > 0, port, kernel_ephemeral_port_low())) { + init_ddnatv_hook(); + extern char *program_invocation_short_name; + report_listening_port(g_ddnatv_root, g_ddnatv_root_len, port, program_invocation_short_name); + } + errno = saved_errno; + } + return ret; +} + +int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen) { + static int (*orig_bind)(int, const struct sockaddr *, socklen_t) = NULL; + if (!orig_bind) orig_bind = (int (*)(int, const struct sockaddr *, socklen_t))dlsym(RTLD_NEXT, "bind"); + + if (addr && addrlen > offsetof(struct sockaddr_un, sun_path) && addr->sa_family == AF_UNIX) { + const struct sockaddr_un *sun = (const struct sockaddr_un *)addr; + if (sun->sun_path[0] != '\0') { + struct sockaddr_un mod_sun; + memset(&mod_sun, 0, sizeof(mod_sun)); + mod_sun.sun_family = AF_UNIX; + rewrite_unix_socket_path(sun->sun_path, mod_sun.sun_path, sizeof(mod_sun.sun_path)); + return orig_bind ? orig_bind(sockfd, (struct sockaddr *)&mod_sun, sizeof(mod_sun)) : -1; + } + } + int ret = orig_bind ? orig_bind(sockfd, addr, addrlen) : -1; + if (ret == 0 && addr) { + int saved_errno = errno; + int chosen = 0; + if (addr->sa_family == AF_INET && addrlen >= (socklen_t)sizeof(struct sockaddr_in)) + chosen = ((const struct sockaddr_in *)addr)->sin_port != 0; + else if (addr->sa_family == AF_INET6 && addrlen >= (socklen_t)sizeof(struct sockaddr_in6)) + chosen = ((const struct sockaddr_in6 *)addr)->sin6_port != 0; + set_chosen_port_fd(sockfd, chosen); + errno = saved_errno; + } + return ret; +} + +int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) { + static int (*orig_connect)(int, const struct sockaddr *, socklen_t) = NULL; + if (!orig_connect) orig_connect = (int (*)(int, const struct sockaddr *, socklen_t))dlsym(RTLD_NEXT, "connect"); + + if (addr && addrlen > offsetof(struct sockaddr_un, sun_path) && addr->sa_family == AF_UNIX) { + const struct sockaddr_un *sun = (const struct sockaddr_un *)addr; + if (sun->sun_path[0] != '\0') { + struct sockaddr_un mod_sun; + memset(&mod_sun, 0, sizeof(mod_sun)); + mod_sun.sun_family = AF_UNIX; + char guest[sizeof(sun->sun_path) + 1]; + size_t max = addrlen - offsetof(struct sockaddr_un, sun_path); + if (max > sizeof(sun->sun_path)) max = sizeof(sun->sun_path); + memcpy(guest, sun->sun_path, max); + guest[max] = '\0'; + char followed[PATH_MAX]; + const char *path = guest[0] == '/' ? follow_guest_link(guest, followed, sizeof(followed)) : guest; + rewrite_unix_socket_path(path, mod_sun.sun_path, sizeof(mod_sun.sun_path)); + return orig_connect ? orig_connect(sockfd, (struct sockaddr *)&mod_sun, sizeof(mod_sun)) : -1; + } + } + + if (addr && is_loopback_dns(addr, addrlen)) { + struct sockaddr_storage redirected; + socklen_t redirected_len = dns_redirect_target(addr, &redirected); + int ret = orig_connect ? orig_connect(sockfd, (struct sockaddr *)&redirected, redirected_len) : -1; + if (ret == 0 || errno == EINPROGRESS) { + int saved_errno = errno; + record_dns_redirect(sockfd, addr, addrlen); + errno = saved_errno; + } + return ret; + } + return orig_connect ? orig_connect(sockfd, addr, addrlen) : -1; +} + +ssize_t sendto(int sockfd, const void *buf, size_t len, int flags, + const struct sockaddr *dest_addr, socklen_t addrlen) { + static ssize_t (*orig_sendto)(int, const void *, size_t, int, const struct sockaddr *, socklen_t) = NULL; + if (!orig_sendto) orig_sendto = (ssize_t (*)(int, const void *, size_t, int, const struct sockaddr *, socklen_t))dlsym(RTLD_NEXT, "sendto"); + + if (dest_addr && is_loopback_dns(dest_addr, addrlen)) { + struct sockaddr_storage redirected; + socklen_t redirected_len = dns_redirect_target(dest_addr, &redirected); + record_dns_redirect(sockfd, dest_addr, addrlen); + return orig_sendto ? orig_sendto(sockfd, buf, len, flags, (struct sockaddr *)&redirected, redirected_len) : -1; + } + return orig_sendto ? orig_sendto(sockfd, buf, len, flags, dest_addr, addrlen) : -1; +} + +#if defined(__NR_getuid32) +#define DDNATV_NR_REAL_GETUID __NR_getuid32 +#define DDNATV_NR_REAL_GETGID __NR_getgid32 +#else +#define DDNATV_NR_REAL_GETUID __NR_getuid +#define DDNATV_NR_REAL_GETGID __NR_getgid +#endif + +// Credentials sent over a Unix socket (SCM_CREDENTIALS) have to be the process's own: the kernel +// refuses others with EPERM, and programs fill them in with getuid(), which is root's 0 here. +// PulseAudio's clients send them on every connection, which then failed ("Connection terminated"). +// Returns 1 with the message copied into mod (its control data into ctrl) and the ids made real. +static int real_scm_credentials(const struct msghdr *msg, struct msghdr *mod, char *ctrl, size_t ctrl_size) { + if (!msg || !msg->msg_control || msg->msg_controllen < sizeof(struct cmsghdr) || msg->msg_controllen > ctrl_size) return 0; + int found = 0; + for (struct cmsghdr *c = CMSG_FIRSTHDR(msg); c; c = CMSG_NXTHDR((struct msghdr *)msg, c)) { + if (c->cmsg_level == SOL_SOCKET && c->cmsg_type == SCM_CREDENTIALS) found = 1; + } + if (!found) return 0; + memcpy(mod, msg, sizeof(*mod)); + memcpy(ctrl, msg->msg_control, msg->msg_controllen); + mod->msg_control = ctrl; + for (struct cmsghdr *c = CMSG_FIRSTHDR(mod); c; c = CMSG_NXTHDR(mod, c)) { + if (c->cmsg_level != SOL_SOCKET || c->cmsg_type != SCM_CREDENTIALS || c->cmsg_len < CMSG_LEN(sizeof(struct ucred))) continue; + // a record that claims more than the buffer holds is the kernel's to refuse, not ours to read + if (c->cmsg_len > mod->msg_controllen - (size_t)((char *)c - ctrl)) break; + struct ucred cred; + memcpy(&cred, CMSG_DATA(c), sizeof(cred)); + cred.pid = getpid(); + cred.uid = (uid_t)call_orig_syscall(DDNATV_NR_REAL_GETUID, 0, 0, 0, 0, 0, 0); + cred.gid = (gid_t)call_orig_syscall(DDNATV_NR_REAL_GETGID, 0, 0, 0, 0, 0, 0); + memcpy(CMSG_DATA(c), &cred, sizeof(cred)); + } + return 1; +} + +ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags) { + static ssize_t (*orig_sendmsg)(int, const struct msghdr *, int) = NULL; + if (!orig_sendmsg) orig_sendmsg = (ssize_t (*)(int, const struct msghdr *, int))dlsym(RTLD_NEXT, "sendmsg"); + struct msghdr cred_msg; + char cred_ctrl[512] __attribute__((aligned(8))); + if (real_scm_credentials(msg, &cred_msg, cred_ctrl, sizeof(cred_ctrl))) msg = &cred_msg; + + if (msg && msg->msg_name && is_loopback_dns((const struct sockaddr *)msg->msg_name, msg->msg_namelen)) { + struct sockaddr_storage redirected; + socklen_t redirected_len = dns_redirect_target((const struct sockaddr *)msg->msg_name, &redirected); + struct msghdr mod_msg; + memcpy(&mod_msg, msg, sizeof(mod_msg)); + mod_msg.msg_name = &redirected; + mod_msg.msg_namelen = redirected_len; + record_dns_redirect(sockfd, (const struct sockaddr *)msg->msg_name, msg->msg_namelen); + return orig_sendmsg ? orig_sendmsg(sockfd, &mod_msg, flags) : -1; + } + return orig_sendmsg ? orig_sendmsg(sockfd, msg, flags) : -1; +} + +static void present_peer_as_root(struct ucred *cred) { + if (cred->uid == (uid_t)call_orig_syscall(DDNATV_NR_REAL_GETUID, 0, 0, 0, 0, 0, 0)) cred->uid = 0; + if (cred->gid == (gid_t)call_orig_syscall(DDNATV_NR_REAL_GETGID, 0, 0, 0, 0, 0, 0)) cred->gid = 0; +} + +// A server compares a peer's credentials with the user the peer claims, which is getuid()'s 0 +// here, while the kernel reports the app's ids: dbus-daemon refused every EXTERNAL login ("Did not +// receive a reply"), so the Plasma desktop never started. +int getsockopt(int sockfd, int level, int optname, void *optval, socklen_t *optlen) { + static int (*orig_getsockopt)(int, int, int, void *, socklen_t *) = NULL; + if (!orig_getsockopt) orig_getsockopt = (int (*)(int, int, int, void *, socklen_t *))dlsym(RTLD_NEXT, "getsockopt"); + if (!orig_getsockopt) { + errno = ENOSYS; + return -1; + } + int ret = orig_getsockopt(sockfd, level, optname, optval, optlen); + if (ret == 0 && level == SOL_SOCKET && optname == SO_PEERCRED && optval && optlen && *optlen >= sizeof(struct ucred)) { + struct ucred cred; + memcpy(&cred, optval, sizeof(cred)); + present_peer_as_root(&cred); + memcpy(optval, &cred, sizeof(cred)); + } + return ret; +} + +static void received_credentials_as_root(struct msghdr *msg) { + if (!msg || !msg->msg_control || msg->msg_controllen < sizeof(struct cmsghdr)) return; + for (struct cmsghdr *c = CMSG_FIRSTHDR(msg); c; c = CMSG_NXTHDR(msg, c)) { + if (c->cmsg_level != SOL_SOCKET || c->cmsg_type != SCM_CREDENTIALS || c->cmsg_len < CMSG_LEN(sizeof(struct ucred))) continue; + struct ucred cred; + memcpy(&cred, CMSG_DATA(c), sizeof(cred)); + present_peer_as_root(&cred); + memcpy(CMSG_DATA(c), &cred, sizeof(cred)); + } +} + +ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags, + struct sockaddr *src_addr, socklen_t *addrlen) { + static ssize_t (*orig_recvfrom)(int, void *, size_t, int, struct sockaddr *, socklen_t *) = NULL; + if (!orig_recvfrom) orig_recvfrom = (ssize_t (*)(int, void *, size_t, int, struct sockaddr *, socklen_t *))dlsym(RTLD_NEXT, "recvfrom"); + + socklen_t cap = addrlen ? *addrlen : 0; + ssize_t ret = orig_recvfrom ? orig_recvfrom(sockfd, buf, len, flags, src_addr, addrlen) : -1; + if (ret > 0 && src_addr && addrlen) { + int saved_errno = errno; + restore_dns_source(sockfd, src_addr, cap, addrlen); + errno = saved_errno; + } + return ret; +} + +// What programs built with _FORTIFY_SOURCE call when the buffer's size is known; the check is +// glibc's +extern void __chk_fail(void) __attribute__((__noreturn__)); +ssize_t __recvfrom_chk(int sockfd, void *buf, size_t len, size_t buflen, int flags, + struct sockaddr *src_addr, socklen_t *addrlen) { + if (len > buflen) __chk_fail(); + return recvfrom(sockfd, buf, len, flags, src_addr, addrlen); +} + +ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags) { + static ssize_t (*orig_recvmsg)(int, struct msghdr *, int) = NULL; + if (!orig_recvmsg) orig_recvmsg = (ssize_t (*)(int, struct msghdr *, int))dlsym(RTLD_NEXT, "recvmsg"); + + socklen_t cap = msg ? msg->msg_namelen : 0; + ssize_t ret = orig_recvmsg ? orig_recvmsg(sockfd, msg, flags) : -1; + if (ret >= 0) received_credentials_as_root(msg); + if (ret > 0 && msg && msg->msg_name) { + int saved_errno = errno; + restore_dns_source(sockfd, (struct sockaddr *)msg->msg_name, cap, &msg->msg_namelen); + errno = saved_errno; + } + return ret; +} + +int getpeername(int sockfd, struct sockaddr *addr, socklen_t *addrlen) { + static int (*orig_getpeername)(int, struct sockaddr *, socklen_t *) = NULL; + if (!orig_getpeername) orig_getpeername = (int (*)(int, struct sockaddr *, socklen_t *))dlsym(RTLD_NEXT, "getpeername"); + + socklen_t cap = addrlen ? *addrlen : 0; + int ret = orig_getpeername ? orig_getpeername(sockfd, addr, addrlen) : -1; + if (ret == 0 && addr && addrlen) restore_dns_source(sockfd, addr, cap, addrlen); + return ret; +} + +// accept() as accept4() without flags, the call bionic makes for both: Android's seccomp filter +// for apps is made from what bionic calls. On the x86_64 emulator a translated accept() answered +// with its system call number (43) as the new descriptor, and sshd failed every connection with +// "fcntl(43, F_GETFL): Bad file descriptor" (Perl, Python and nginx use accept4 themselves). +int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen) { + static int (*orig_accept4)(int, struct sockaddr *, socklen_t *, int) = NULL; + if (!orig_accept4) orig_accept4 = (int (*)(int, struct sockaddr *, socklen_t *, int))dlsym(RTLD_NEXT, "accept4"); + if (!orig_accept4) { + errno = ENOSYS; + return -1; + } + return orig_accept4(sockfd, addr, addrlen, 0); +} + +#ifndef AF_NETLINK +#define AF_NETLINK 16 +#endif +#ifndef NETLINK_AUDIT +#define NETLINK_AUDIT 9 +#endif + +int socket(int domain, int type, int protocol) { + static int (*orig_socket)(int, int, int) = NULL; + if (!orig_socket) orig_socket = (int (*)(int, int, int))dlsym(RTLD_NEXT, "socket"); + if (domain == AF_NETLINK && protocol == NETLINK_AUDIT) { + errno = EPROTONOSUPPORT; + return -1; + } + int fd = orig_socket ? orig_socket(domain, type, protocol) : -1; + // A new socket has not chosen a port, whatever the descriptor's last one had (close_range() and + // raw close system calls do not pass the hook's close()) + if (fd >= 0) set_chosen_port_fd(fd, 0); + return fd; +} + +#endif /* !DDNATV_HOST_TEST */ diff --git a/tools/nativeguest/hook/static_programs.c b/tools/nativeguest/hook/static_programs.c new file mode 100644 index 0000000000..406fca0da9 --- /dev/null +++ b/tools/nativeguest/hook/static_programs.c @@ -0,0 +1,345 @@ +// Part of libddnatv-hook.so, included by ddnatv-hook.c (one translation unit, so the +// parts share their static functions and are built with the same command as before). +// +// Statically linked programs: their seccomp-blocked system calls are patched before they run. + +// ---- Static programs ---------------------------------------------------------------------- +// A statically linked program (gcc -static) carries its own copy of glibc, whose system calls the +// preloaded hook never sees. The ones Android's seccomp filter answers with SIGSYS therefore kill it: +// set_robust_list at start-up, clone3 for every thread, rseq, and the others the hook's SIGSYS +// handler catches for dynamic programs. Before such a program runs, The native userland patches those call sites +// in its file to what that handler answers (0 for set_robust_list and the set*id family, -ENOSYS, +// which glibc falls back from, for the rest), as scripts/patch-bootstrap-ld.py does to the rootfs +// ld.so and libc at build time. The scanning is a pure function over a buffer, so tests/native can +// check it against the script. + +#define DDNATV_STATIC_WINDOW 16 // instructions between "mov x8, #nr" and its "svc #0" + +// AArch64: what the SIGSYS handler answers for syscall `nr`: 1 for 0, 2 for -ENOSYS, 0 to leave it. +// set_robust_list keeps the script's no-op (x0 then holds its non-error first argument). +static int a64_blocked_syscall(unsigned int nr) { + switch (nr) { + case 99: // set_robust_list + return 3; + case 100: // get_robust_list + case 143: case 144: case 145: case 146: case 147: case 149: // setregid setgid setreuid setuid setresuid setresgid + case 151: case 152: case 159: // setfsuid setfsgid setgroups + return 1; + case 97: case 217: case 218: case 219: // unshare add_key request_key keyctl + case 268: case 277: case 280: case 293: // setns seccomp bpf rseq + return 2; + default: + return (nr >= 424 && nr < 512) ? 2 : 0; // newer than Android's filter (clone3, close_range, ...) + } +} + +static uint32_t rd32(const unsigned char *p) { return (uint32_t)p[0] | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24); } +static uint16_t rd16(const unsigned char *p) { return (uint16_t)(p[0] | (p[1] << 8)); } +static void wr32(unsigned char *p, uint32_t v) { p[0] = v & 0xff; p[1] = (v >> 8) & 0xff; p[2] = (v >> 16) & 0xff; p[3] = (v >> 24) & 0xff; } +static void wr16(unsigned char *p, uint16_t v) { p[0] = v & 0xff; p[1] = (v >> 8) & 0xff; } + +// An AArch64 instruction that ends the search for the "svc #0" of a "mov x8, #nr": a branch, another +// svc, or anything with x8 in its first register field (another write to x8, most likely). +static int a64_ends_syscall_window(uint32_t insn) { + if ((insn & 0x1f) == 8) return 1; + if ((insn & 0xffe0001f) == 0xd4000001u) return 1; // svc #imm + if ((insn & 0x7c000000u) == 0x14000000u) return 1; // b, bl + if ((insn & 0xff000010u) == 0x54000000u) return 1; // b.cond + if ((insn & 0x7e000000u) == 0x34000000u) return 1; // cbz, cbnz + if ((insn & 0x7e000000u) == 0x36000000u) return 1; // tbz, tbnz + if ((insn & 0xfe000000u) == 0xd6000000u) return 1; // br, blr, ret + return 0; +} + +static size_t a64_patch_seccomp_syscalls(unsigned char *data, size_t start, size_t end) { + size_t patched = 0; + start = (start + 3) & ~(size_t)3; + for (size_t pos = start; pos + 4 <= end; pos += 4) { + uint32_t insn = rd32(data + pos); + // movz x8, #nr / movz w8, #nr + if ((insn & 0x7fe0001fu) != 0x52800008u) continue; + int result = a64_blocked_syscall((insn >> 5) & 0xffff); + if (!result) continue; + for (size_t s = pos + 4, i = 0; s + 4 <= end && i < DDNATV_STATIC_WINDOW; s += 4, i++) { + uint32_t next = rd32(data + s); + if (next == 0xd4000001u) { // svc #0 + wr32(data + s, result == 3 ? 0xd503201fu /* nop */ + : result == 1 ? 0xd2800000u /* mov x0, #0 */ + : 0x928004a0u); /* mov x0, #-38 */ + patched++; + break; + } + if (a64_ends_syscall_window(next)) break; + } + } + return patched; +} + +// 32-bit ARM: the three calls glibc makes on its own, with the exact patterns of the script +// (ARM mode, Thumb-2 with r7, and Thumb-2 through __libc_do_syscall with r12). +static long thumb_bl_target(const unsigned char *data, size_t size, size_t off) { + if (off + 4 > size) return -1; + uint16_t hw1 = rd16(data + off), hw2 = rd16(data + off + 2); + if ((hw1 & 0xf800) != 0xf000 || (hw2 & 0xd000) != 0xd000) return -1; + long s = (hw1 >> 10) & 1; + long imm10 = hw1 & 0x3ff; + long j1 = (hw2 >> 13) & 1, j2 = (hw2 >> 11) & 1; + long imm11 = hw2 & 0x7ff; + long i1 = (j1 ^ s) ^ 1, i2 = (j2 ^ s) ^ 1; + long imm25 = (s << 24) | (i1 << 23) | (i2 << 22) | (imm10 << 12) | (imm11 << 1); + if (s) imm25 -= (1L << 25); + return (long)off + 4 + imm25; +} + +static size_t arm_patch_seccomp_syscalls(unsigned char *data, size_t start, size_t end) { + static const struct { uint32_t movw_r7; uint32_t repl; } arm[] = { + { 0xe3007152u, 0xe320f000u }, // movw r7, #338 (set_robust_list) -> nop + { 0xe30071b3u, 0xe3e00025u }, // movw r7, #435 (clone3) -> mvn r0, #37 + { 0xe300718eu, 0xe3e00025u }, // movw r7, #398 (rseq) -> mvn r0, #37 + }; + static const struct { uint16_t hw1, hw2; int nop_only; } thumb_r7[] = { + { 0xf240, 0x1752, 1 }, // movw r7, #338 + { 0xf240, 0x17b3, 0 }, // movw r7, #435 + { 0xf240, 0x178e, 0 }, // movw r7, #398 + }; + static const struct { uint16_t hw1, hw2; uint16_t repl1, repl2; } thumb_r12[] = { + { 0xf44f, 0x7ca9, 0xf04f, 0x0000 }, // mov.w r12, #338 -> mov.w r0, #0 + { 0xf44f, 0x7cc7, 0xf06f, 0x0025 }, // mov.w r12, #398 -> mvn.w r0, #37 + { 0xf240, 0x1cb3, 0xf06f, 0x0025 }, // movw r12, #435 -> mvn.w r0, #37 + }; + static const unsigned char do_syscall_stub[6] = { 0x80, 0xb5, 0x67, 0x46, 0x00, 0xdf }; + size_t patched = 0; + + size_t a4 = (start + 3) & ~(size_t)3, e4 = end & ~(size_t)3; + for (size_t pos = a4; pos + 4 < e4; pos += 4) { + uint32_t insn = rd32(data + pos); + for (size_t k = 0; k < sizeof(arm) / sizeof(arm[0]); k++) { + if (insn != arm[k].movw_r7) continue; + for (size_t s = pos + 4; s < e4 && s < pos + 68; s += 4) { + if (rd32(data + s) == 0xef000000u) { // svc #0 + wr32(data + s, arm[k].repl); + patched++; + break; + } + } + } + } + + size_t a2 = (start + 1) & ~(size_t)1, e2 = end & ~(size_t)1; + for (size_t t = a2; t + 4 < e2; t += 2) { + uint16_t hw1 = rd16(data + t), hw2 = rd16(data + t + 2); + int matched_r7 = 0; + for (size_t k = 0; k < sizeof(thumb_r7) / sizeof(thumb_r7[0]); k++) { + if (hw1 != thumb_r7[k].hw1 || hw2 != thumb_r7[k].hw2) continue; + matched_r7 = 1; + size_t limit = (e2 - 2 < t + 64 ? e2 - 2 : t + 64) + 2; + for (size_t s = t + 4; s < limit; s += 2) { + if (rd16(data + s) == 0xdf00) { // svc #0 + if (!thumb_r7[k].nop_only) { + wr16(data + t, 0xf06f); // mvn.w r0, #37 in place of the movw + wr16(data + t + 2, 0x0025); + } + wr16(data + s, 0xbf00); // nop + patched++; + break; + } + } + break; + } + if (matched_r7) continue; + for (size_t k = 0; k < sizeof(thumb_r12) / sizeof(thumb_r12[0]); k++) { + if (hw1 != thumb_r12[k].hw1 || hw2 != thumb_r12[k].hw2) continue; + size_t limit = (e2 - 4 < t + 48 ? e2 - 4 : t + 48) + 2; + for (size_t s = t + 4; s < limit; s += 2) { + long target = thumb_bl_target(data, end, s); + if (target >= (long)start && target + 6 <= (long)end && memcmp(data + target, do_syscall_stub, 6) == 0) { + wr16(data + s, thumb_r12[k].repl1); + wr16(data + s + 2, thumb_r12[k].repl2); + patched++; + break; + } + } + break; + } + } + return patched; +} + +#ifndef EM_ARM +#define EM_ARM 40 +#endif +#ifndef EM_AARCH64 +#define EM_AARCH64 183 +#endif + +// Patches the blocked syscalls in data[start, end), code of an ELF for `e_machine`; returns how many. +static size_t patch_seccomp_syscalls(unsigned char *data, size_t start, size_t end, uint16_t e_machine) { + if (!data || end <= start) return 0; + if (e_machine == EM_AARCH64) return a64_patch_seccomp_syscalls(data, start, end); + if (e_machine == EM_ARM) return arm_patch_seccomp_syscalls(data, start, end); + return 0; +} + +// Reads the program headers of an ELF file into `ph` (as 64-bit fields); returns their number, or -1. +struct ddnatv_phdr { uint32_t type, flags; uint64_t offset, filesz; }; +static int read_elf_phdrs(int fd, uint16_t *machine, struct ddnatv_phdr *ph, int max) { + unsigned char eh[64]; + if (pread(fd, eh, sizeof(eh), 0) < 52 || memcmp(eh, "\x7f" "ELF", 4) != 0 || eh[5] != 1) return -1; + int is64 = eh[4] == 2; + uint64_t phoff = is64 ? ((uint64_t)rd32(eh + 32) | ((uint64_t)rd32(eh + 36) << 32)) : rd32(eh + 28); + uint16_t phentsize = rd16(eh + (is64 ? 54 : 42)); + uint16_t phnum = rd16(eh + (is64 ? 56 : 44)); + *machine = rd16(eh + 18); + if (phentsize < (is64 ? 56 : 32) || phnum == 0 || phnum > max) return -1; + for (int i = 0; i < phnum; i++) { + unsigned char e[64]; + if (pread(fd, e, phentsize < sizeof(e) ? phentsize : sizeof(e), (off_t)(phoff + (uint64_t)i * phentsize)) < (is64 ? 56 : 32)) return -1; + ph[i].type = rd32(e); + if (is64) { + ph[i].flags = rd32(e + 4); + ph[i].offset = (uint64_t)rd32(e + 8) | ((uint64_t)rd32(e + 12) << 32); + ph[i].filesz = (uint64_t)rd32(e + 32) | ((uint64_t)rd32(e + 36) << 32); + } else { + ph[i].offset = rd32(e + 4); + ph[i].filesz = rd32(e + 16); + ph[i].flags = rd32(e + 24); + } + } + return phnum; +} + +// Whether the ELF has a Go build-ID note: Go programs make their own system calls and never these. +static int elf_is_go(int fd, const struct ddnatv_phdr *ph, int n) { + for (int i = 0; i < n; i++) { + if (ph[i].type != 4 /* PT_NOTE */ || ph[i].filesz < 16) continue; + unsigned char note[16]; + if (pread(fd, note, sizeof(note), (off_t)ph[i].offset) != sizeof(note)) continue; + if (rd32(note) == 4 && memcmp(note + 12, "Go\0\0", 4) == 0) return 1; + } + return 0; +} + +#define DDNATV_STATIC_XATTR "user.ddnatv.seccomp" +#define DDNATV_STATIC_MAX_CODE (256u * 1024 * 1024) + +#ifdef DDNATV_HAVE_GOSYS +static int prepare_go_program(int fd, const char *path, const struct stat *st); +static uint32_t go_blob_fingerprint(void); +static int go_disabled(void); +#endif + +// What was done to a file is recorded with its size and time, with the rootfs (a Go program carries +// the paths of it) and with the code Go programs are given (go_programs.c): a newer The native userland looks at +// the programs again. +static void static_marker(const struct stat *st, char *buf, size_t size) { + unsigned fingerprint = 0; + const char *off = ""; +#ifdef DDNATV_HAVE_GOSYS + fingerprint = go_blob_fingerprint(); + if (go_disabled()) off = " off"; +#endif + snprintf(buf, size, "3 %lld %lld.%09ld %08x %s%s", (long long)st->st_size, (long long)st->st_mtim.tv_sec, + (long)st->st_mtim.tv_nsec, fingerprint, g_ddnatv_root, off); +} + +// Makes a static program safe to start (see above). Runs before every start of one, so a program +// already handled (marked by an extended attribute that holds its size and modification time) costs +// one fstat and one getxattr; without extended attributes it is scanned again each time. Returns the +// number of patched calls, 0 when there was nothing to do, -1 when the file could not be handled +// (it is then started as it is). +// go_only: leave the file alone unless it is an AArch64 Go program. That is for a program that asks for +// Android's linker (GOOS=android): it is started like the dynamic ones, but it makes its own system +// calls, so it gets the code of the static Go programs (#42). +static int prepare_static_program_ex(const char *path, int go_only) { + int fd = open(path, O_RDONLY | O_CLOEXEC); + if (fd < 0) return -1; + struct stat st; + char marker[PATH_MAX + 96], seen[PATH_MAX + 96]; + if (fstat(fd, &st) != 0 || !S_ISREG(st.st_mode)) { + close(fd); + return -1; + } + static_marker(&st, marker, sizeof(marker)); + ssize_t seen_len = fgetxattr(fd, DDNATV_STATIC_XATTR, seen, sizeof(seen) - 1); + if (seen_len > 0) { + seen[seen_len] = '\0'; + if (strcmp(seen, marker) == 0) { + close(fd); + return 0; + } + } + + struct ddnatv_phdr ph[64]; + uint16_t machine = 0; + int n = read_elf_phdrs(fd, &machine, ph, 64); + if (go_only && !(n >= 0 && machine == EM_AARCH64 && elf_is_go(fd, ph, n))) { + close(fd); + return 0; + } + if (n < 0 || (machine != EM_AARCH64 && machine != EM_ARM) || elf_is_go(fd, ph, n)) { + // A Go program has no C library for the hook to be loaded into: it gets code of its own + int go_rc = 0; +#ifdef DDNATV_HAVE_GOSYS + if (n >= 0 && machine == EM_AARCH64 && elf_is_go(fd, ph, n)) go_rc = prepare_go_program(fd, path, &st); +#endif + if (n >= 0 && go_rc >= 0) { + struct stat after; + if (fstat(fd, &after) == 0) static_marker(&after, marker, sizeof(marker)); + fsetxattr(fd, DDNATV_STATIC_XATTR, marker, strlen(marker), 0); + } + close(fd); + return n < 0 || go_rc < 0 ? -1 : 0; + } + + int total = 0; + int wfd = -1; + int made_writable = 0; + for (int i = 0; i < n; i++) { + if (ph[i].type != 1 /* PT_LOAD */ || !(ph[i].flags & 1 /* PF_X */) || ph[i].filesz == 0) continue; + if (ph[i].filesz > DDNATV_STATIC_MAX_CODE || ph[i].offset > (uint64_t)st.st_size || + ph[i].filesz > (uint64_t)st.st_size - ph[i].offset) continue; + size_t len = (size_t)ph[i].filesz; + unsigned char *code = (unsigned char *)malloc(len); + if (!code) continue; + if (pread(fd, code, len, (off_t)ph[i].offset) != (ssize_t)len) { + free(code); + continue; + } + size_t patched = patch_seccomp_syscalls(code, 0, len, machine); + if (patched > 0) { + if (wfd < 0) { + wfd = open(path, O_WRONLY | O_CLOEXEC); + if (wfd < 0 && errno == EACCES && fchmod(fd, (st.st_mode | S_IWUSR) & 07777) == 0) { + made_writable = 1; + wfd = open(path, O_WRONLY | O_CLOEXEC); + } + } + // ETXTBSY: the program is running right now, so it was handled when it started. + if (wfd < 0 || pwrite(wfd, code, len, (off_t)ph[i].offset) != (ssize_t)len) { + free(code); + total = -1; + break; + } + total += (int)patched; + } + free(code); + } + if (wfd >= 0) { + // Keep the times: make and friends must not see a rebuilt program. + const struct timespec times[2] = { st.st_atim, st.st_mtim }; + futimens(wfd, times); + close(wfd); + } + if (made_writable) fchmod(fd, st.st_mode & 07777); + if (total >= 0) { + struct stat after; + if (fstat(fd, &after) == 0) { + static_marker(&after, marker, sizeof(marker)); + fsetxattr(fd, DDNATV_STATIC_XATTR, marker, strlen(marker), 0); + } + } + close(fd); + return total; +} + +static int prepare_static_program(const char *path) { return prepare_static_program_ex(path, 0); } diff --git a/tools/nativeguest/hook/syscalls.c b/tools/nativeguest/hook/syscalls.c new file mode 100644 index 0000000000..06cacd8646 --- /dev/null +++ b/tools/nativeguest/hook/syscalls.c @@ -0,0 +1,128 @@ +// Part of libddnatv-hook.so, included by ddnatv-hook.c (one translation unit, so the +// parts share their static functions and are built with the same command as before). +// +// The system calls that Android's seccomp filter answers with SIGSYS, and which of them the +// SIGSYS handler (signals.c) turns into a fake success. + +static int is_sigsys_synthetic_success_syscall(int sys_nr) { +#if defined(__NR_setuid) + if (sys_nr == __NR_setuid) return 1; +#endif +#if defined(__NR_setuid32) + if (sys_nr == __NR_setuid32) return 1; +#endif +#if defined(__NR_setgid) + if (sys_nr == __NR_setgid) return 1; +#endif +#if defined(__NR_setgid32) + if (sys_nr == __NR_setgid32) return 1; +#endif +#if defined(__NR_setreuid) + if (sys_nr == __NR_setreuid) return 1; +#endif +#if defined(__NR_setreuid32) + if (sys_nr == __NR_setreuid32) return 1; +#endif +#if defined(__NR_setregid) + if (sys_nr == __NR_setregid) return 1; +#endif +#if defined(__NR_setregid32) + if (sys_nr == __NR_setregid32) return 1; +#endif +#if defined(__NR_setresuid) + if (sys_nr == __NR_setresuid) return 1; +#endif +#if defined(__NR_setresuid32) + if (sys_nr == __NR_setresuid32) return 1; +#endif +#if defined(__NR_setresgid) + if (sys_nr == __NR_setresgid) return 1; +#endif +#if defined(__NR_setresgid32) + if (sys_nr == __NR_setresgid32) return 1; +#endif +#if defined(__NR_setfsuid) + if (sys_nr == __NR_setfsuid) return 1; +#endif +#if defined(__NR_setfsuid32) + if (sys_nr == __NR_setfsuid32) return 1; +#endif +#if defined(__NR_setfsgid) + if (sys_nr == __NR_setfsgid) return 1; +#endif +#if defined(__NR_setfsgid32) + if (sys_nr == __NR_setfsgid32) return 1; +#endif +#if defined(__NR_setgroups) + if (sys_nr == __NR_setgroups) return 1; +#endif +#if defined(__NR_setgroups32) + if (sys_nr == __NR_setgroups32) return 1; +#endif +#if defined(__NR_set_robust_list) + if (sys_nr == __NR_set_robust_list) return 1; +#endif +#if defined(__NR_get_robust_list) + if (sys_nr == __NR_get_robust_list) return 1; +#endif + return 0; +} + +static int is_handled_sigsys_syscall(int sys_nr) { + if (is_sigsys_synthetic_success_syscall(sys_nr)) { + return 1; + } + + // Unified Linux 5.1+ syscall numbers across architectures + if (sys_nr >= 424 && sys_nr < 512) { + return 1; + } + + // Architecture-specific syscall numbers guarded per target ABI +#if defined(__aarch64__) + if (sys_nr == 97 /* __NR_unshare */ || + sys_nr == 217 /* __NR_add_key */ || + sys_nr == 218 /* __NR_request_key */ || + sys_nr == 219 /* __NR_keyctl */ || + sys_nr == 268 /* __NR_setns */ || + sys_nr == 277 /* __NR_seccomp */ || + sys_nr == 280 /* __NR_bpf */ || + sys_nr == 293 /* __NR_rseq */) { + return 1; + } +#elif defined(__arm__) + if (sys_nr == 309 /* __NR_add_key */ || + sys_nr == 310 /* __NR_request_key */ || + sys_nr == 311 /* __NR_keyctl */ || + sys_nr == 337 /* __NR_unshare */ || + sys_nr == 375 /* __NR_setns */ || + sys_nr == 383 /* __NR_seccomp */ || + sys_nr == 386 /* __NR_bpf */ || + sys_nr == 398 /* __NR_rseq */) { + return 1; + } +#elif defined(__x86_64__) + if (sys_nr == 248 /* __NR_add_key */ || + sys_nr == 249 /* __NR_request_key */ || + sys_nr == 250 /* __NR_keyctl */ || + sys_nr == 272 /* __NR_unshare */ || + sys_nr == 308 /* __NR_setns */ || + sys_nr == 317 /* __NR_seccomp */ || + sys_nr == 321 /* __NR_bpf */ || + sys_nr == 334 /* __NR_rseq */) { + return 1; + } +#elif defined(__i386__) + if (sys_nr == 286 /* __NR_add_key */ || + sys_nr == 287 /* __NR_request_key */ || + sys_nr == 288 /* __NR_keyctl */ || + sys_nr == 310 /* __NR_unshare */ || + sys_nr == 346 /* __NR_setns */ || + sys_nr == 354 /* __NR_seccomp */ || + sys_nr == 357 /* __NR_bpf */ || + sys_nr == 386 /* __NR_rseq */) { + return 1; + } +#endif + return 0; +} diff --git a/tools/nativeguest/hook/sysv_shm.c b/tools/nativeguest/hook/sysv_shm.c new file mode 100644 index 0000000000..0b43e1fe2c --- /dev/null +++ b/tools/nativeguest/hook/sysv_shm.c @@ -0,0 +1,561 @@ +// Part of libddnatv-hook.so, included by ddnatv-hook.c (one translation unit, so the +// parts share their static functions and are built with the same command as before). +// +// System V shared memory (shmget, shmat, shmdt, shmctl). Android's seccomp filter blocks these system +// calls for apps (the SIGSYS handler turns them into ENOSYS), but X uses them: a program draws an image +// into a segment and the X server reads it from there (the MIT-SHM extension), and Qt, GTK and SDL +// programs expect it. The segments are files of their own in /tmp/.ddnatv-sysv-shm of the rootfs, +// which every process of the app can open and map: +// +// id-N the segment's description: "PID KEY MODE SIZE" (who made it, its key, its rights, its size) +// seg-N its memory (a file of SIZE bytes, mapped MAP_SHARED) +// key-K for a segment with a key: the N it belongs to (K in hex) +// att-N-P process P has the segment attached (made by shmat(), removed by its last shmdt()) +// +// N is chosen at random and claimed with O_EXCL, so processes never pick the same one. IPC_RMID +// removes the files at once: programs that attached the segment keep their mapping (as on Linux), +// later attaches fail, which is what X programs expect (they remove a segment as soon as the X +// server has attached it). A segment nobody removed stays until the session's directory is cleared. + +#ifndef DDNATV_HOST_TEST + +#ifndef MAP_FIXED_NOREPLACE +#define MAP_FIXED_NOREPLACE 0x100000 +#endif +#ifndef SHM_REMAP +#define SHM_REMAP 040000 +#endif + +#define SHM_DIR "/tmp/.ddnatv-sysv-shm" +#define SHM_MAX_ATTACH 512 + +struct shm_meta { + int pid; + long key; + unsigned mode; + size_t size; +}; + +struct shm_attach { + void *addr; + size_t size; + int id; +}; + +static struct shm_attach g_shm_attach[SHM_MAX_ATTACH]; +static pthread_mutex_t g_shm_lock = PTHREAD_MUTEX_INITIALIZER; + +// root + /tmp/.ddnatv-sysv-shm[/name] +static int shm_path(char *out, size_t outsz, const char *name) { + int n = snprintf(out, outsz, "%s%s%s%s", g_ddnatv_root, SHM_DIR, name ? "/" : "", name ? name : ""); + return n > 0 && (size_t)n < outsz ? 0 : -1; +} + +static int shm_open_file(const char *name, int flags, unsigned mode) { + char path[PATH_MAX]; + if (shm_path(path, sizeof(path), name) != 0) return -1; + return (int)syscall(SYS_openat, AT_FDCWD, path, flags | O_CLOEXEC, mode); +} + +static void shm_unlink_file(const char *name) { + char path[PATH_MAX]; + if (shm_path(path, sizeof(path), name) == 0) syscall(SYS_unlinkat, AT_FDCWD, path, 0); +} + +static int shm_ensure_dir(void) { + char path[PATH_MAX]; + if (shm_path(path, sizeof(path), NULL) != 0) return -1; + if (syscall(SYS_mkdirat, AT_FDCWD, path, 0700) == 0 || errno == EEXIST) return 0; + // /tmp of the rootfs missing too (a rootfs being set up) + char tmp[PATH_MAX]; + snprintf(tmp, sizeof(tmp), "%s/tmp", g_ddnatv_root); + syscall(SYS_mkdirat, AT_FDCWD, tmp, 01777); + return (syscall(SYS_mkdirat, AT_FDCWD, path, 0700) == 0 || errno == EEXIST) ? 0 : -1; +} + +static int shm_read_small(const char *name, char *buf, size_t bufsz) { + int fd = shm_open_file(name, O_RDONLY, 0); + if (fd < 0) return -1; + ssize_t n = (ssize_t)syscall(SYS_read, fd, buf, bufsz - 1); + syscall(SYS_close, fd); + if (n < 0) return -1; + buf[n] = '\0'; + return (int)n; +} + +// The description of segment `id`; waits a moment for one being written. 0, or -1 (errno EINVAL). +static int shm_read_meta(int id, struct shm_meta *meta) { + char name[32], text[128]; + snprintf(name, sizeof(name), "id-%d", id); + for (int attempt = 0; attempt < 50; attempt++) { + int n = shm_read_small(name, text, sizeof(text)); + if (n < 0) break; + unsigned long size = 0; + if (n > 0 && sscanf(text, "%d %lx %o %lu", &meta->pid, &meta->key, &meta->mode, &size) == 4) { + meta->size = (size_t)size; + return 0; + } + struct timespec pause = { 0, 1000000 }; + nanosleep(&pause, NULL); + } + errno = EINVAL; + return -1; +} + +static int shm_process_alive(int pid) { + return pid > 0 && (kill(pid, 0) == 0 || errno == EPERM); +} + +static void shm_remove(int id, long key) { + char name[32]; + if (key != IPC_PRIVATE) { + // only when the key still names this segment + char text[32]; + snprintf(name, sizeof(name), "key-%lx", key); + if (shm_read_small(name, text, sizeof(text)) > 0 && atoi(text) == id) shm_unlink_file(name); + } + snprintf(name, sizeof(name), "seg-%d", id); + shm_unlink_file(name); + snprintf(name, sizeof(name), "id-%d", id); + shm_unlink_file(name); +} + +static int shm_mark_attached(int id, int attached) { + char name[48]; + snprintf(name, sizeof(name), "att-%d-%d", id, (int)getpid()); + if (!attached) { + shm_unlink_file(name); + return 0; + } + int fd = shm_open_file(name, O_WRONLY | O_CREAT, 0600); + if (fd < 0) return -1; + syscall(SYS_close, fd); + return 0; +} + +// The collector holds this exclusively while it scans and removes, shmat() shared while it checks a +// segment, maps it and marks itself, so that no segment is collected while it is being attached. +static int shm_lock_dir(int op) { + int fd = shm_open_file(".lock", O_RDWR | O_CREAT, 0600); + if (fd < 0) return -1; + long rc; + do { + rc = syscall(SYS_flock, fd, op); + } while (rc != 0 && errno == EINTR); + if (rc != 0) { + syscall(SYS_close, fd); + return -1; + } + return fd; +} + +static void shm_unlock_dir(int fd) { + if (fd >= 0) syscall(SYS_close, fd); +} + +// Deletes the marks of attached processes that have ended. Every segment stays until IPC_RMID, as on +// Linux: a private one's maker can hand its id to a process that attaches after the maker has ended. +static void shm_collect(void) { + char path[PATH_MAX]; + if (shm_path(path, sizeof(path), NULL) != 0) return; + int lock = shm_lock_dir(LOCK_EX); + if (lock < 0) return; + int dfd = (int)syscall(SYS_openat, AT_FDCWD, path, O_RDONLY | O_DIRECTORY | O_CLOEXEC); + if (dfd < 0) { + shm_unlock_dir(lock); + return; + } + char dents[4096]; + for (;;) { + long n = syscall(SYS_getdents64, dfd, dents, sizeof(dents)); + if (n <= 0) break; + for (long at = 0; at < n;) { + unsigned short reclen; + memcpy(&reclen, dents + at + 16, sizeof(reclen)); + if (reclen == 0) break; + const char *entry = dents + at + 19; + int id = 0, pid = 0; + if (strncmp(entry, "att-", 4) == 0 && sscanf(entry + 4, "%d-%d", &id, &pid) == 2 && !shm_process_alive(pid)) + shm_unlink_file(entry); + at += reclen; + } + } + syscall(SYS_close, dfd); + shm_unlock_dir(lock); +} + +static int shm_create(long key, size_t size, unsigned mode) { + // a different sequence of ids for every call, also within one second and one process + static unsigned calls = 0; + struct timespec now; + clock_gettime(CLOCK_MONOTONIC, &now); + unsigned seed = (unsigned)getpid() * 2654435761u ^ (unsigned)now.tv_nsec ^ (unsigned)now.tv_sec * 40503u ^ + __atomic_add_fetch(&calls, 1, __ATOMIC_RELAXED) * 0x9e3779b9u; + for (int attempt = 0; attempt < 64; attempt++) { + seed = seed * 1103515245u + 12345u; + int id = (int)((seed >> 1) & 0x3fffffff); + if (id == 0) continue; + char name[32]; + snprintf(name, sizeof(name), "id-%d", id); + int meta_fd = shm_open_file(name, O_WRONLY | O_CREAT | O_EXCL, 0600); + if (meta_fd < 0) { + if (errno == EEXIST) continue; + return -1; + } + snprintf(name, sizeof(name), "seg-%d", id); + int seg_fd = shm_open_file(name, O_RDWR | O_CREAT | O_TRUNC, 0600); + long page = sysconf(_SC_PAGESIZE); + if (page <= 0) page = 4096; + if (size > (size_t)INTPTR_MAX - (size_t)page) { + // no file (and no mapping) can be that large + if (seg_fd >= 0) syscall(SYS_close, seg_fd); + syscall(SYS_close, meta_fd); + shm_remove(id, IPC_PRIVATE); + errno = EINVAL; + return -1; + } + size_t rounded = (size + (size_t)page - 1) / (size_t)page * (size_t)page; + if (seg_fd < 0 || ftruncate(seg_fd, (off_t)rounded) != 0) { + int saved = errno; + if (seg_fd >= 0) syscall(SYS_close, seg_fd); + syscall(SYS_close, meta_fd); + shm_remove(id, IPC_PRIVATE); + errno = saved == EFBIG ? EINVAL : (saved ? saved : ENOMEM); + return -1; + } + syscall(SYS_close, seg_fd); + char text[128]; + int len = snprintf(text, sizeof(text), "%d %lx %o %lu\n", getpid(), key, mode & 0777, (unsigned long)size); + ssize_t written = (ssize_t)syscall(SYS_write, meta_fd, text, (size_t)len); + syscall(SYS_close, meta_fd); + if (written != len) { + shm_remove(id, IPC_PRIVATE); + errno = ENOSPC; + return -1; + } + return id; + } + errno = ENOSPC; + return -1; +} + +// Writes `text` to the file `name` at once: into "new-ID" first, then renamed over it (`exclusive`: only +// when `name` does not exist yet, else -1 with EEXIST). Readers see the old file or the whole new one. +static int shm_publish(const char *name, int id, const char *text, size_t len, int exclusive) { + char tmp_name[32], tmp[PATH_MAX], path[PATH_MAX]; + snprintf(tmp_name, sizeof(tmp_name), "new-%d", id); + if (shm_path(tmp, sizeof(tmp), tmp_name) != 0 || shm_path(path, sizeof(path), name) != 0) { + errno = ENAMETOOLONG; + return -1; + } + int fd = (int)syscall(SYS_openat, AT_FDCWD, tmp, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0600); + if (fd < 0) return -1; + ssize_t written = (ssize_t)syscall(SYS_write, fd, text, len); + syscall(SYS_close, fd); + if (written != (ssize_t)len) { + syscall(SYS_unlinkat, AT_FDCWD, tmp, 0); + errno = ENOSPC; + return -1; + } + long rc; + if (exclusive) { + rc = syscall(SYS_renameat2, AT_FDCWD, tmp, AT_FDCWD, path, 1 /* RENAME_NOREPLACE */); + if (rc != 0 && (errno == ENOSYS || errno == EINVAL)) { + // a kernel or file system without RENAME_NOREPLACE: claim the name, then write it + int kfd = (int)syscall(SYS_openat, AT_FDCWD, path, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, 0600); + syscall(SYS_unlinkat, AT_FDCWD, tmp, 0); + if (kfd < 0) return -1; + written = (ssize_t)syscall(SYS_write, kfd, text, len); + syscall(SYS_close, kfd); + if (written == (ssize_t)len) return 0; + syscall(SYS_unlinkat, AT_FDCWD, path, 0); + errno = ENOSPC; + return -1; + } + } else { + rc = syscall(SYS_renameat2, AT_FDCWD, tmp, AT_FDCWD, path, 0); +#ifdef SYS_renameat + if (rc != 0 && errno == ENOSYS) rc = syscall(SYS_renameat, AT_FDCWD, tmp, AT_FDCWD, path); +#endif + } + if (rc != 0) { + int saved = errno; + syscall(SYS_unlinkat, AT_FDCWD, tmp, 0); + errno = saved; + return -1; + } + return 0; +} + +int shmget(key_t key, size_t size, int shmflg) { + init_ddnatv_hook(); + if (g_ddnatv_root[0] == '\0') { + errno = ENOSYS; + return -1; + } + if (shm_ensure_dir() != 0) return -1; + if ((shmflg & IPC_CREAT) || key == IPC_PRIVATE) shm_collect(); + if (key == IPC_PRIVATE) { + if (size == 0) { + errno = EINVAL; + return -1; + } + return shm_create(IPC_PRIVATE, size, (unsigned)shmflg); + } + char key_name[32]; + snprintf(key_name, sizeof(key_name), "key-%lx", (long)key); + for (int attempt = 0; attempt < 8; attempt++) { + char text[32]; + if (shm_read_small(key_name, text, sizeof(text)) > 0) { + int id = atoi(text); + struct shm_meta meta; + if (id > 0 && shm_read_meta(id, &meta) == 0) { + if ((shmflg & IPC_CREAT) && (shmflg & IPC_EXCL)) { + errno = EEXIST; + return -1; + } + if (size > meta.size) { + errno = EINVAL; + return -1; + } + return id; + } + shm_unlink_file(key_name); // names a segment that is gone + } + if (!(shmflg & IPC_CREAT)) { + errno = ENOENT; + return -1; + } + if (size == 0) { + errno = EINVAL; + return -1; + } + int id = shm_create((long)key, size, (unsigned)shmflg); + if (id < 0) return -1; + // The key file appears with its id in it, never empty: written under a name of its own, then + // renamed to the key's name unless another process made the key's segment meanwhile + int len = snprintf(text, sizeof(text), "%d\n", id); + int published = shm_publish(key_name, id, text, (size_t)len, 1); + if (published == 0) return id; + int saved = errno; + shm_remove(id, IPC_PRIVATE); + if (saved != EEXIST) { + errno = saved; + return -1; + } + // another process made the key's segment at the same time: use that one + } + errno = EAGAIN; + return -1; +} + +static void *shmat_locked(int shmid, const void *shmaddr, int shmflg); + +void *shmat(int shmid, const void *shmaddr, int shmflg) { + init_ddnatv_hook(); + // without the lock the collector could remove the segment mid-attach + int lock = g_ddnatv_root[0] != '\0' ? shm_lock_dir(LOCK_SH) : -1; + if (lock < 0) { + errno = EINVAL; + return (void *)-1; + } + void *addr = shmat_locked(shmid, shmaddr, shmflg); + int saved = errno; + shm_unlock_dir(lock); + errno = saved; + return addr; +} + +static int shm_attached_here_locked(int id); + +static void *shmat_locked(int shmid, const void *shmaddr, int shmflg) { + struct shm_meta meta; + if (g_ddnatv_root[0] == '\0' || shm_read_meta(shmid, &meta) != 0) { + errno = EINVAL; + return (void *)-1; + } + int readonly = (shmflg & SHM_RDONLY) != 0; + char name[32]; + snprintf(name, sizeof(name), "seg-%d", shmid); + int fd = shm_open_file(name, readonly ? O_RDONLY : O_RDWR, 0); + if (fd < 0) { + errno = EINVAL; + return (void *)-1; + } + void *want = (void *)shmaddr; + int flags = MAP_SHARED; + if (want) { + if (shmflg & SHM_RND) want = (void *)((uintptr_t)want & ~((uintptr_t)SHMLBA - 1)); + else if ((uintptr_t)want & ((uintptr_t)sysconf(_SC_PAGESIZE) - 1)) { + syscall(SYS_close, fd); + errno = EINVAL; + return (void *)-1; + } + // an address in use is replaced only with SHM_REMAP, as on Linux (MAP_FIXED_NOREPLACE: a kernel + // older than 4.17 takes it as a hint, which the check below catches) + flags |= (shmflg & SHM_REMAP) ? MAP_FIXED : MAP_FIXED_NOREPLACE; + } + // a place in the table first, so that every attach can be detached + int slot = -1; + pthread_mutex_lock(&g_shm_lock); + for (int i = 0; i < SHM_MAX_ATTACH; i++) { + if (!g_shm_attach[i].addr && !g_shm_attach[i].size) { + g_shm_attach[i].size = 1; // reserved + slot = i; + break; + } + } + pthread_mutex_unlock(&g_shm_lock); + if (slot < 0) { + syscall(SYS_close, fd); + errno = EMFILE; + return (void *)-1; + } + void *addr = mmap(want, meta.size, readonly ? PROT_READ : (PROT_READ | PROT_WRITE), flags, fd, 0); + int saved = errno; + syscall(SYS_close, fd); + if (addr != MAP_FAILED && want && addr != want) { + munmap(addr, meta.size); + addr = MAP_FAILED; + saved = EINVAL; + } + pthread_mutex_lock(&g_shm_lock); + // the mark keeps the segment from being collected; without one the attach is undone + if (addr != MAP_FAILED && shm_mark_attached(shmid, 1) != 0) { + saved = errno; + if (shm_attached_here_locked(shmid) == 0) shm_mark_attached(shmid, 0); + munmap(addr, meta.size); + addr = MAP_FAILED; + if (saved != ENOMEM) saved = EINVAL; + } + if (addr == MAP_FAILED) { + g_shm_attach[slot].size = 0; + } else { + g_shm_attach[slot].addr = addr; + g_shm_attach[slot].size = meta.size; + g_shm_attach[slot].id = shmid; + } + pthread_mutex_unlock(&g_shm_lock); + if (addr == MAP_FAILED) { + errno = saved == ENOMEM ? ENOMEM : EINVAL; + return (void *)-1; + } + return addr; +} + +int shmdt(const void *shmaddr) { + pthread_mutex_lock(&g_shm_lock); + for (int i = 0; i < SHM_MAX_ATTACH; i++) { + if (g_shm_attach[i].addr && g_shm_attach[i].addr == shmaddr) { + void *addr = g_shm_attach[i].addr; + size_t size = g_shm_attach[i].size; + int id = g_shm_attach[i].id; + g_shm_attach[i].addr = NULL; + g_shm_attach[i].size = 0; + // counted and unmarked under the lock, so that an attach of this process meanwhile keeps its mark + if (shm_attached_here_locked(id) == 0) shm_mark_attached(id, 0); + pthread_mutex_unlock(&g_shm_lock); + munmap(addr, size); + return 0; + } + } + pthread_mutex_unlock(&g_shm_lock); + errno = EINVAL; + return -1; +} + +static int shm_attached_here_locked(int id) { + int count = 0; + for (int i = 0; i < SHM_MAX_ATTACH; i++) { + if (g_shm_attach[i].addr && g_shm_attach[i].id == id) count++; + } + return count; +} + +static int shm_attached_here(int id) { + int count = 0; + pthread_mutex_lock(&g_shm_lock); + for (int i = 0; i < SHM_MAX_ATTACH; i++) { + if (g_shm_attach[i].addr && g_shm_attach[i].id == id) count++; + } + pthread_mutex_unlock(&g_shm_lock); + return count; +} + +int shmctl(int shmid, int cmd, struct shmid_ds *buf) { + init_ddnatv_hook(); + if (g_ddnatv_root[0] == '\0') { + errno = ENOSYS; + return -1; + } + switch (cmd) { + case IPC_INFO: + case SHM_INFO: + if (buf) { + // the caller's buffer is the structure of the command (struct shminfo or struct shm_info) + memset(buf, 0, cmd == IPC_INFO ? sizeof(struct shminfo) : sizeof(struct shm_info)); + if (cmd == IPC_INFO) { + struct shminfo *info = (struct shminfo *)buf; + info->shmmax = sizeof(unsigned long) > 4 ? (unsigned long)1 << 40 : 0x7fffffffUL; + info->shmmin = 1; + info->shmmni = 4096; + info->shmseg = 4096; + info->shmall = (unsigned long)1 << 30; + } + } + return 0; + case SHM_LOCK: + case SHM_UNLOCK: + return 0; + default: + break; + } + struct shm_meta meta; + if (shm_read_meta(shmid, &meta) != 0) { + errno = EINVAL; + return -1; + } + switch (cmd) { + case IPC_STAT: + case SHM_STAT: + case SHM_STAT_ANY: + if (!buf) { + errno = EFAULT; + return -1; + } + memset(buf, 0, sizeof(*buf)); + buf->shm_perm.__key = (key_t)meta.key; + // The owner's rights for everyone of the app: X compares the uid of its client with the owner's, + // and the two can differ in The native userland (root emulated for one, not the other) + unsigned owner = meta.mode & 0600; + buf->shm_perm.mode = (unsigned short)(owner | (owner >> 3) | (owner >> 6)); + buf->shm_perm.uid = buf->shm_perm.cuid = geteuid(); + buf->shm_perm.gid = buf->shm_perm.cgid = getegid(); + buf->shm_segsz = meta.size; + buf->shm_cpid = meta.pid; + buf->shm_lpid = getpid(); + buf->shm_nattch = (shmatt_t)(shm_attached_here(shmid) + (meta.pid != getpid() ? 1 : 0)); + buf->shm_ctime = time(NULL); + return cmd == IPC_STAT ? 0 : shmid; + case IPC_SET: { + if (!buf) { + errno = EFAULT; + return -1; + } + // the new rights are kept (owner and group stay the app's: there is no other user to give it to) + char name[32], text[128]; + snprintf(name, sizeof(name), "id-%d", shmid); + int len = snprintf(text, sizeof(text), "%d %lx %o %lu\n", meta.pid, meta.key, buf->shm_perm.mode & 0777u, + (unsigned long)meta.size); + return shm_publish(name, shmid, text, (size_t)len, 0) == 0 ? 0 : -1; + } + case IPC_RMID: + shm_remove(shmid, meta.key); + return 0; + default: + errno = EINVAL; + return -1; + } +} + +#endif /* !DDNATV_HOST_TEST */ diff --git a/tools/nativeguest/seccomp-sim.c b/tools/nativeguest/seccomp-sim.c new file mode 100644 index 0000000000..e1b50a09c6 --- /dev/null +++ b/tools/nativeguest/seccomp-sim.c @@ -0,0 +1,92 @@ +// Runs a program under a seccomp filter that answers the system calls Android's app policy blocks +// with SIGSYS, the way a stock app process does, for testing on builds that have no filter: +// seccomp-sim [-a NR,NR...] PROGRAM [ARGS...] +// seccomp-sim -f FILTER PROGRAM [ARGS...] +// -a leaves the listed system calls allowed (to find which one kills a program). -f installs a +// filter as it is (raw struct sock_filter entries), such as the app filter a device's zygote makes. +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static const int blocked[] = {99, 100, 97, 217, 218, 219, 268, 277, 280, 293, 143, 144, 145, 146, 147, 149, 151, 152, 159}; + +#define MAX_ALLOWED 64 +#define NBLOCKED (sizeof(blocked) / sizeof(blocked[0])) + +static int run_filter_file(const char *path, char **argv) { + static struct sock_filter filter[BPF_MAXINSNS]; + FILE *f = fopen(path, "rb"); + if (!f) { + perror("seccomp-sim: -f"); + return 2; + } + size_t n = fread(filter, sizeof(filter[0]), BPF_MAXINSNS, f); + int extra = fgetc(f) != EOF; + fclose(f); + if (n == 0 || extra) { + fprintf(stderr, "seccomp-sim: %s holds no filter of at most %d instructions\n", path, BPF_MAXINSNS); + return 2; + } + struct sock_fprog prog = { .len = (unsigned short)n, .filter = filter }; + if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) || prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog)) { + perror("seccomp-sim"); + return 1; + } + execv(argv[0], argv); + perror("seccomp-sim: exec"); + return 127; +} + +int main(int argc, char **argv) { + if (argc > 3 && strcmp(argv[1], "-f") == 0) return run_filter_file(argv[2], argv + 3); + int skip[MAX_ALLOWED], nskip = 0, arg = 1; + if (argc > 3 && strcmp(argv[1], "-a") == 0) { + for (char *p = argv[2];; p++) { + char *end = NULL; + long nr = strtol(p, &end, 10); + if (end == p || (*end != '\0' && *end != ',') || *p < '0' || *p > '9' || + nr < 0 || nr > 1023 || nskip == MAX_ALLOWED) { + fprintf(stderr, "seccomp-sim: -a takes up to %d comma-separated system call numbers, not \"%s\"\n", MAX_ALLOWED, argv[2]); + return 2; + } + skip[nskip++] = (int)nr; + if (*end == '\0') break; + p = end; + } + arg = 3; + } + if (arg >= argc) return 2; + struct sock_filter filter[4 + 2 * MAX_ALLOWED + 2 + 2 * NBLOCKED + 1]; + int n = 0; + filter[n++] = (struct sock_filter)BPF_STMT(BPF_LD | BPF_W | BPF_ABS, offsetof(struct seccomp_data, arch)); + filter[n++] = (struct sock_filter)BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, AUDIT_ARCH_AARCH64, 1, 0); + filter[n++] = (struct sock_filter)BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW); + filter[n++] = (struct sock_filter)BPF_STMT(BPF_LD | BPF_W | BPF_ABS, offsetof(struct seccomp_data, nr)); + for (int i = 0; i < nskip; i++) { + filter[n++] = (struct sock_filter)BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, skip[i], 0, 1); + filter[n++] = (struct sock_filter)BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW); + } + filter[n++] = (struct sock_filter)BPF_JUMP(BPF_JMP | BPF_JGE | BPF_K, 424, 0, 1); + filter[n++] = (struct sock_filter)BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_TRAP); + for (size_t i = 0; i < NBLOCKED; i++) { + filter[n++] = (struct sock_filter)BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, blocked[i], 0, 1); + filter[n++] = (struct sock_filter)BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_TRAP); + } + filter[n++] = (struct sock_filter)BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW); + struct sock_fprog prog = { .len = (unsigned short)n, .filter = filter }; + if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) || prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog)) { + perror("seccomp-sim"); + return 1; + } + execv(argv[arg], argv + arg); + perror("seccomp-sim: exec"); + return 127; +} diff --git a/tools/packs/build.sh b/tools/packs/build.sh index 7b7e7e76fd..4ea6a41932 100755 --- a/tools/packs/build.sh +++ b/tools/packs/build.sh @@ -77,6 +77,7 @@ pack tracking "$work/tracking" hand_landmarker.task hand_landmarker_lite.task po pack xbox "$linuxfs/droiddeck-xbox" version xodus-cli xodus-service pack comet "$linuxfs/droiddeck-comet" version comet pack powervr "$repo/app/src/main/assets/hybris" runtime.tzst version.txt +[ ! -f "$linuxfs/droiddeck-ubisoft/droiddeck-upc.exe" ] || pack ubisoft "$linuxfs/droiddeck-ubisoft" droiddeck-upc.exe mkdir "$work/butler" fetch "https://broth.itch.zone/butler/linux-arm64/${BUTLER_VERSION}/archive/default" \ diff --git a/tools/proot/PATCHES.md b/tools/proot/PATCHES.md index dbf1b959b5..c4173a48c7 100644 --- a/tools/proot/PATCHES.md +++ b/tools/proot/PATCHES.md @@ -132,6 +132,36 @@ Prototype (inert unless `PROOT_FASTPATH` is set; see `docs/development/proot-per path, and PRoot refused the call unless the descriptor was `AT_FDCWD`. FEX starts every child of an emulated program with `execveat(fd, "/proc/self/exe")`, so x86 programs in a Flatpak sandbox, which does not carry the session's preloads, could not start other programs. +- `0025-fastpath-trampoline-sigsys-enosys.patch` - Android 10 and older refuse `statx` with + `SIGSYS`. A `statx` from the fast path's trampoline page already carries the host path, but + proot's `SIGSYS` emulation translated it again as a guest path (`//...`) and + returned `ENOENT`, so every existing file looked missing to Rust's `metadata` and GNU `stat`: + uutils `mkdir -p` on an existing directory failed with "Already exists" and Steam setup stopped + (Pico 4, Android 10). A refused syscall from the trampoline now gets `ENOSYS` without + translation or extension handling, and the fast path falls back as it already does for + `statx` (`newfstatat`) and `memfd_create`. Syscalls outside the trampoline and sessions + without `PROOT_FASTPATH` are unchanged. `bench/statx-trap.c` traps `statx` and checks an + existing directory still reads as one. +- `0026-arm64-kernel-program-layout.patch` - on ARM64, PRoot put its loader at 128 GiB, a PIE + program at 192 GiB and its interpreter at 252 GiB. Android kernels give a process a 39-bit + (512 GiB) address space, so those three split the low half into holes of 64 GiB or less, and a + program that reserves one large contiguous range there could not get it. The Linux kernel loads + a PIE program at two thirds of the address space (`ELF_ET_DYN_BASE`, 0x5555555000 with 39 bits) + and keeps the range below it free; the loader, program and interpreter now sit there too, which + leaves 64 GiB to 336 GiB free as on a native kernel. Switch emulators that run guest code + natively reserve 256 GiB above 64 GiB for guest memory: under PRoot the reservation failed and + the first guest instruction faulted forever. 48-bit address spaces and 32-bit programs are + unchanged in practice. +- `0027-guest-seccomp-sigsys.patch` - PRoot treated every seccomp `SIGSYS` as Android's app policy + refusing a call: it emulated the few it knows and answered the rest with `ENOSYS`, so a filter + the guest installs itself never reached its own handler. Firefox's content sandbox traps + `openat` and opens files through a broker in the parent; every font open came back `ENOSYS`, + and each tab crashed in `gfxFontGroup::GetDefaultFont`. Android's policy traps with + `SECCOMP_RET_DATA` 0, while Firefox's and Chromium's filters tag their traps, so a tagged + `SIGSYS` is now delivered to the guest. A trap from the fast path's trampoline still gets + `ENOSYS`, because its arguments are host paths; the fast path then stops for that process and + repeats the call through a plain `svc`, which the guest's filter traps with the guest path. + `bench/guest-trap.c` installs a tagged `openat` trap and checks its handler answers the call. Old kernel fixes: diff --git a/tools/proot/bench/chown.py b/tools/proot/bench/chown.py new file mode 100644 index 0000000000..b774e79af3 --- /dev/null +++ b/tools/proot/bench/chown.py @@ -0,0 +1,98 @@ +import errno +import os +import shutil +import sys +import time + +me = (os.getuid(), os.getgid()) + + +def call(fn): + try: + return repr(fn()) + except OSError as e: + return "E" + errno.errorcode.get(e.errno, str(e.errno)) + + +def run(base, links): + shutil.rmtree(base, ignore_errors=True) + os.makedirs(base + "/d") + open(base + "/f", "w").close() + open(base + "/d/g", "w").close() + if links: + os.symlink("f", base + "/lf") + os.symlink("nowhere", base + "/dl") + os.symlink("d", base + "/ld") + os.symlink("loop2", base + "/loop1") + os.symlink("loop1", base + "/loop2") + uid = lambda p, follow=True: os.stat(p, follow_symlinks=follow).st_uid + cases = [ + ("path", lambda: os.chown(base + "/f", *me)), + ("keep", lambda: os.chown(base + "/f", -1, -1)), + ("uid only", lambda: os.chown(base + "/f", me[0], -1)), + ("gid only", lambda: os.chown(base + "/f", -1, me[1])), + ("foreign uid", lambda: os.chown(base + "/f", me[0] + 1, -1)), + ("foreign gid", lambda: os.chown(base + "/f", -1, me[1] + 12345)), + ("dir", lambda: os.chown(base + "/d", *me)), + ("dir trailing slash", lambda: os.chown(base + "/d/", *me)), + ("file trailing slash", lambda: os.chown(base + "/f/", *me)), + ("missing", lambda: os.chown(base + "/nope", *me)), + ("missing parent", lambda: os.chown(base + "/nope/x", *me)), + ("under file", lambda: os.chown(base + "/f/x", *me)), + ("empty", lambda: os.chown("", *me)), + ("dotdot", lambda: os.chown(base + "/d/../f", *me)), + ("fd", lambda: (lambda fd: (os.chown(fd, *me), os.close(fd))[1])(os.open(base + "/f", os.O_RDONLY))), + ("dirfd rel", lambda: (lambda fd: (os.chown("g", *me, dir_fd=fd), os.close(fd))[1])(os.open(base + "/d", os.O_RDONLY))), + ("dirfd abs", lambda: (lambda fd: (os.chown(base + "/f", *me, dir_fd=fd), os.close(fd))[1])(os.open(base + "/d", os.O_RDONLY))), + ("bad dirfd", lambda: os.chown("g", *me, dir_fd=9999)), + ("file dirfd", lambda: (lambda fd: (os.chown("g", *me, dir_fd=fd), os.close(fd))[1])(os.open(base + "/f", os.O_RDONLY))), + ("cwd rel", lambda: (os.chdir(base + "/d"), os.chown("g", *me))[1]), + ("cwd dotdot", lambda: (os.chdir(base + "/d"), os.chown("../f", *me))[1]), + ] + if links: + cases += [ + ("link follow", lambda: os.chown(base + "/lf", *me)), + ("link nofollow", lambda: os.chown(base + "/lf", *me, follow_symlinks=False)), + ("dangling follow", lambda: os.chown(base + "/dl", *me)), + ("dangling nofollow", lambda: os.chown(base + "/dl", *me, follow_symlinks=False)), + ("dirlink", lambda: os.chown(base + "/ld/g", *me)), + ("dirlink slash", lambda: os.chown(base + "/ld/", *me)), + ("loop", lambda: os.chown(base + "/loop1", *me)), + ("loop nofollow", lambda: os.chown(base + "/loop1", *me, follow_symlinks=False)), + ("link foreign", lambda: os.chown(base + "/lf", me[0] + 1, -1)), + ] + out = [] + for label, fn in cases: + os.chdir("/") + out.append(f" {label}: {call(fn)}") + os.chdir("/") + out.append(" owner after: " + call(lambda: (uid(base + "/f"), os.stat(base + "/f").st_gid))) + shutil.rmtree(base, ignore_errors=True) + return out + + +def bench(base, count): + shutil.rmtree(base, ignore_errors=True) + os.makedirs(base) + names = [f"{base}/f{i}" for i in range(count)] + for n in names: + open(n, "w").close() + start = time.perf_counter() + for n in names: + try: + os.chown(n, *me) + except OSError: + pass + elapsed = time.perf_counter() - start + shutil.rmtree(base, ignore_errors=True) + return elapsed + + +for base, links in (("/root/chown-eq", True), ("/tmp/chown-eq", True), ("/mnt/droiddeck-sd/chown-eq", False)): + print("==", base) + print("\n".join(run(base, links))) + +if len(sys.argv) > 1: + for base in ("/root/chown-bench", "/mnt/droiddeck-sd/chown-bench"): + t = bench(base, int(sys.argv[1])) + print(f"BENCH {base} {int(sys.argv[1])} calls {t:.3f}s {t / int(sys.argv[1]) * 1e6:.0f}us each", file=sys.stderr) diff --git a/tools/proot/bench/guest-trap.c b/tools/proot/bench/guest-trap.c new file mode 100644 index 0000000000..ae5a304287 --- /dev/null +++ b/tools/proot/bench/guest-trap.c @@ -0,0 +1,49 @@ +// Run inside a session, with and without the fast path: a seccomp filter the guest installs +// itself must reach its own SIGSYS handler, which answers the trapped openat. +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define ANSWER 4242 +static volatile int traps, data; + +static void on_sigsys(int sig, siginfo_t *info, void *context) { + (void)sig; + ucontext_t *uc = context; + traps++; + data = info->si_errno; + uc->uc_mcontext.regs[0] = ANSWER; +} + +int main(void) { + struct sigaction sa; + memset(&sa, 0, sizeof sa); + sa.sa_sigaction = on_sigsys; + sa.sa_flags = SA_SIGINFO; + sigaction(SIGSYS, &sa, NULL); + struct sock_filter code[] = { + BPF_STMT(BPF_LD | BPF_W | BPF_ABS, offsetof(struct seccomp_data, nr)), + BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, SYS_openat, 0, 1), + BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_TRAP | 7), + BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW), + }; + struct sock_fprog filter = {.len = sizeof code / sizeof *code, .filter = code}; + if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) || prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &filter, 0, 0)) { + perror("seccomp"); + return 77; + } + int fd = open("/etc/hostname", O_RDONLY); + int err = errno; + printf("open=%d errno=%d traps=%d data=%d\n", fd, fd < 0 ? err : 0, traps, data); + return fd == ANSWER && traps == 1 && data == 7 ? 0 : 1; +} diff --git a/tools/proot/bench/statx-trap.c b/tools/proot/bench/statx-trap.c new file mode 100644 index 0000000000..fab7bd5ee7 --- /dev/null +++ b/tools/proot/bench/statx-trap.c @@ -0,0 +1,32 @@ +#include "../fastpath/fastpath.c" +#include "checks.h" +#include +#include +#include + +static void expect_dir(const char *path) { + struct statx stx; + memset(&stx, 0, sizeof stx); + long rs = fp_statx(AT_FDCWD, path, 0, STATX_BASIC_STATS, &stx); + if (rs != 0) fprintf(stderr, "statx %s: %s\n", path, strerror((int)-rs)); + assert(rs == 0 && S_ISDIR(stx.stx_mode)); +} + +int main(int argc, char **argv) { + assert(argc == 2); + if (!fp_ready()) return 77; + struct sock_filter code[] = { + BPF_STMT(BPF_LD | BPF_W | BPF_ABS, offsetof(struct seccomp_data, nr)), + BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, SYS_statx, 0, 1), + BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_TRAP), + BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW), + }; + struct sock_fprog filter = {.len = sizeof code / sizeof *code, .filter = code}; + if (sc(SYS_prctl, PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) || + sc(SYS_prctl, PR_SET_SECCOMP, SECCOMP_MODE_FILTER, (long)&filter, 0, 0)) return 77; + expect_dir("/"); + expect_dir(argv[1]); + expect_dir(argv[1]); + puts("PASS: statx refused by the host policy still answers an existing guest directory"); + return 0; +} diff --git a/tools/proot/bench/thermal.py b/tools/proot/bench/thermal.py new file mode 100644 index 0000000000..d7a50503ac --- /dev/null +++ b/tools/proot/bench/thermal.py @@ -0,0 +1,12 @@ +import sys +import time + +count = int(sys.argv[1]) +paths = ["/sys/class/thermal/thermal_zone%d/temp" % n for n in (22, 26, 28)] +start = time.perf_counter() +for _ in range(count): + for p in paths: + with open(p) as f: + f.read() +elapsed = time.perf_counter() - start +print(f"{count * len(paths)} opens {elapsed:.3f}s {elapsed / (count * len(paths)) * 1e6:.0f}us each") diff --git a/tools/proot/build.sh b/tools/proot/build.sh index 64a51a660a..6285d18788 100755 --- a/tools/proot/build.sh +++ b/tools/proot/build.sh @@ -52,6 +52,13 @@ cp "$HERE/talloc-config.h" "$WORK/talloc/config/config.h" "$TOOLCHAIN/llvm-ar" rcs "$WORK/libtalloc.a" "$WORK/talloc.o" mkdir -p "$WORK/bin" ln -s "$TOOLCHAIN/llvm-readelf" "$WORK/bin/readelf" +# loader-info.awk needs strtonum, which mawk (the default awk on some runners) lacks. +if command -v gawk >/dev/null; then + ln -s "$(command -v gawk)" "$WORK/bin/awk" +elif ! awk 'BEGIN { exit !(strtonum("0x10") == 16) }' 2>/dev/null; then + echo "gawk, or an awk with strtonum, is required to build the proot loader" >&2 + exit 1 +fi PATH="$WORK/bin:$PATH" make -C "$WORK/proot/src" -j"$(getconf _NPROCESSORS_ONLN)" \ CC="$CC" STRIP="$TOOLCHAIN/llvm-strip" OBJCOPY="$TOOLCHAIN/llvm-objcopy" OBJDUMP="$TOOLCHAIN/llvm-objdump" \ CPPFLAGS="-D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -I. -I$WORK/talloc -DARG_MAX=131072 -Wno-error=implicit-function-declaration" \ diff --git a/tools/proot/fastpath/fastpath.c b/tools/proot/fastpath/fastpath.c index 0a9d3f22b5..8d645eddd2 100644 --- a/tools/proot/fastpath/fastpath.c +++ b/tools/proot/fastpath/fastpath.c @@ -49,7 +49,30 @@ static size_t rootlen; static long long ttl_ns = 2000000000LL; static unsigned long hits, misses; -static long sc(long nr, long a, long b, long c, long d, long e) { return fp_sys(nr, a, b, c, d, e, 0); } +// A seccomp filter the guest installs itself (Firefox's content sandbox) can trap a trampoline +// syscall that the host allows; proot answers those with ENOSYS. The call is then repeated +// through libc, where the guest's own SIGSYS handler sees the guest path, and the process stops +// using the fast path. +static __thread int fp_refused; +static int optional_syscall(long nr) { + return nr == SYS_statx || nr == SYS_memfd_create || nr == SYS_faccessat2 || nr == SYS_close_range || + nr == SYS_openat2 || nr == SYS_renameat2; +} +static long sc(long nr, long a, long b, long c, long d, long e) { + if (fp_refused) return -ENOSYS; + long r = fp_sys(nr, a, b, c, d, e, 0); + if (r == -ENOSYS && !optional_syscall(nr)) { + __atomic_store_n(&fp_on, 0, __ATOMIC_RELAXED); + fp_refused = 1; + } + return r; +} +static inline int refused(void) { + if (!fp_refused) return 0; + fp_refused = 0; + return 1; +} +static inline int answered(long r) { return !refused() && r != FP_SLOW; } static void fp_init(void); static pthread_once_t init_once = PTHREAD_ONCE_INIT; static inline int fp_ready(void) { @@ -672,6 +695,18 @@ static int base_of(int dirfd, char *h) { return gone(h, n) ? -1 : 0; } +static long bound_literal(const char *literal, struct res *r) { + struct bind *bd; + if (reserved(literal) || bind_of(literal, &bd) < 0 || !bd || bd->glen != strlen(literal) || bd->glen < 2) return FP_SLOW; + if (snprintf(r->host, PATH_MAX, "%s", bd->host) >= PATH_MAX) return FP_SLOW; + if (sc(SYS_newfstatat, AT_FDCWD, (long)r->host, (long)&r->st, AT_SYMLINK_NOFOLLOW, 0) < 0 || S_ISLNK(r->st.st_mode)) return FP_SLOW; + snprintf(r->guest, PATH_MAX, "%s", literal); + r->exists = 1; + r->moving = 0; + r->magic = 0; + return 0; +} + static long resolve_in(const char *base, const char *path, int follow, struct res *r) { char norm[PATH_MAX]; size_t o = 0; @@ -707,6 +742,10 @@ static long resolve_in(const char *base, const char *path, int follow, struct re unsigned h = hash_of(norm); if (!cached(norm, h, r->guest)) { w = walk(r, norm, 1, 0); + if (w == FP_SLOW && !trailing) { + *slash = '/'; + return bound_literal(norm, r); + } if (w < 0) return w; if (w != GLUE && !S_ISDIR(r->st.st_mode)) return -ENOTDIR; if (!r->moving) remember(norm, h, r->guest); @@ -772,7 +811,7 @@ static int ret(long r) { if (!real_##name) real_##name = (type)dlsym(RTLD_NEXT, #name) #define ANSWER(r, dirfd, path, real_call) \ - do { long r_ = (r); if (r_ != FP_SLOW) { hits++; return ret(r_); } missed(__func__, dirfd, path); return real_call; } while (0) + do { long r_ = (r); if (answered(r_)) { hits++; return ret(r_); } missed(__func__, dirfd, path); return real_call; } while (0) static long fp_open(int dirfd, const char *path, int flags, mode_t mode) { @@ -789,7 +828,7 @@ static long fp_open(int dirfd, const char *path, int flags, mode_t mode) { static int do_openat(int dirfd, const char *path, int flags, mode_t mode, int (*real)(int, const char *, int, ...)) { long r = fp_open(dirfd, path, flags, mode); - if (r != FP_SLOW) { hits++; return ret(r); } + if (answered(r)) { hits++; return ret(r); } missed(__func__, dirfd, path); return real(dirfd, path, flags, mode); } @@ -854,8 +893,10 @@ int lstat64(const char *p, struct stat64 *st) { return lstat(p, (struct stat *)s int fstat(int fd, struct stat *st) { REAL(fstat, int (*)(int, struct stat *)); if (!fp_ready()) return real_fstat(fd, st); + long r = sc(SYS_fstat, fd, (long)st, 0, 0, 0); + if (!answered(r)) return real_fstat(fd, st); hits++; - return ret(sc(SYS_fstat, fd, (long)st, 0, 0, 0)); + return ret(r); } int fstat64(int fd, struct stat64 *st) { return fstat(fd, (struct stat *)st); } @@ -973,7 +1014,7 @@ static long fp_access(int dirfd, const char *path, int mode, int flags) { static int access_common(int dirfd, const char *path, int mode, int flags) { REAL(faccessat, int (*)(int, const char *, int, int)); long rs = fp_access(dirfd, path, mode, flags); - if (rs != FP_SLOW) { hits++; return ret(rs); } + if (answered(rs)) { hits++; return ret(rs); } missed(__func__, dirfd, path); if (flags == 0) return (int)syscall(SYS_faccessat, dirfd, path, mode); return real_faccessat(dirfd, path, mode, flags); @@ -1071,7 +1112,7 @@ char *canonicalize_file_name(const char *path) { return realpath(path, NULL); } char *getcwd(char *buf, size_t size) { REAL(getcwd, char *(*)(char *, size_t)); char g[PATH_MAX]; - if (!fp_ready() || own_cwd(g) == FP_SLOW) { missed(__func__, AT_FDCWD, "."); return real_getcwd(buf, size); } + if (!fp_ready() || own_cwd(g) == FP_SLOW || refused()) { missed(__func__, AT_FDCWD, "."); return real_getcwd(buf, size); } hits++; size_t need = strlen(g) + 1; if (buf && size == 0) { errno = EINVAL; return NULL; } @@ -1165,7 +1206,7 @@ static long fp_rename(int olddirfd, const char *old, int newdirfd, const char *n static int rename_common(int olddirfd, const char *old, int newdirfd, const char *new, unsigned flags) { REAL(renameat2, int (*)(int, const char *, int, const char *, unsigned)); long rs = fp_rename(olddirfd, old, newdirfd, new, flags); - if (rs != FP_SLOW) { hits++; return ret(rs); } + if (answered(rs)) { hits++; return ret(rs); } missed(__func__, olddirfd, old); if (!flags) return (int)syscall(SYS_renameat, olddirfd, old, newdirfd, new); return real_renameat2(olddirfd, old, newdirfd, new, flags); @@ -1204,6 +1245,20 @@ int fchmodat(int dirfd, const char *path, mode_t mode, int flags) { } int chmod(const char *path, mode_t mode) { return fchmodat(AT_FDCWD, path, mode, 0); } +static long fp_chown(int dirfd, const char *path, uid_t uid, gid_t gid, int flags) { + struct res r; + long rs = flags & ~AT_SYMLINK_NOFOLLOW ? FP_SLOW : resolve(dirfd, path, (flags & AT_SYMLINK_NOFOLLOW ? 0 : 1) | MAY_BE_UNSEEN, &r); + if (rs == 1) rs = -ENOENT; + if (rs == 0 || rs == UNSEEN) rs = sc(SYS_fchownat, AT_FDCWD, (long)r.host, (long)uid, (long)gid, flags); + return rs; +} +int fchownat(int dirfd, const char *path, uid_t uid, gid_t gid, int flags) { + REAL(fchownat, int (*)(int, const char *, uid_t, gid_t, int)); + ANSWER(fp_chown(dirfd, path, uid, gid, flags), dirfd, path, real_fchownat(dirfd, path, uid, gid, flags)); +} +int chown(const char *path, uid_t uid, gid_t gid) { return fchownat(AT_FDCWD, path, uid, gid, 0); } +int lchown(const char *path, uid_t uid, gid_t gid) { return fchownat(AT_FDCWD, path, uid, gid, AT_SYMLINK_NOFOLLOW); } + static long fp_utimens(int dirfd, const char *path, const struct timespec times[2], int flags) { struct res r; long rs; @@ -1325,7 +1380,7 @@ static long fp_getxattr(const char *path, const char *name, void *value, size_t static ssize_t getxattr_common(const char *path, const char *name, void *value, size_t size, int follow, ssize_t (*real)(const char *, const char *, void *, size_t)) { long rs = fp_getxattr(path, name, value, size, follow); - if (rs != FP_SLOW) { hits++; return rs < 0 ? ret(rs) : rs; } + if (answered(rs)) { hits++; return rs < 0 ? ret(rs) : rs; } missed(__func__, AT_FDCWD, path); return real(path, name, value, size); } @@ -1348,7 +1403,7 @@ static long fp_listxattr(const char *path, char *list, size_t size, int follow) static ssize_t listxattr_common(const char *path, char *list, size_t size, int follow, ssize_t (*real)(const char *, char *, size_t)) { long rs = fp_listxattr(path, list, size, follow); - if (rs != FP_SLOW) { hits++; return rs < 0 ? ret(rs) : rs; } + if (answered(rs)) { hits++; return rs < 0 ? ret(rs) : rs; } missed(__func__, AT_FDCWD, path); return real(path, list, size); } @@ -1551,6 +1606,7 @@ static int accept_common(int fd, struct sockaddr *addr, socklen_t *len, int flag struct sockaddr_storage got; socklen_t got_len = sizeof got; long r = sc(SYS_accept4, fd, addr ? (long)&got : 0, addr ? (long)&got_len : 0, flags, 0); + if (refused()) return real_accept4(fd, addr, len, flags); if (r >= 0) inherit_long_name(fd, (int)r); if (!addr) return ret(r); if (r >= 0) report_address((int)r, LONG_PEER, &got, got_len, addr, len); @@ -1565,6 +1621,7 @@ static int name_common(long nr, int fd, struct sockaddr *addr, socklen_t *len, i struct sockaddr_storage got; socklen_t got_len = sizeof got; long r = sc(nr, fd, (long)&got, (long)&got_len, 0, 0); + if (refused()) return real(fd, addr, len); if (r == 0) report_address(fd, nr == SYS_getsockname ? LONG_SELF : LONG_PEER, &got, got_len, addr, len); return ret(r); } @@ -1657,7 +1714,7 @@ static int online_cpus(void) { int get_nprocs(void) { REAL(get_nprocs, int (*)(void)); int n = fp_ready() ? online_cpus() : -1; - if (n > 0) { hits++; return n; } + if (n > 0 && !refused()) { hits++; return n; } missed(__func__, AT_FDCWD, "(nprocs)"); return real_get_nprocs(); } @@ -1713,6 +1770,7 @@ int isatty(int fd) { if (!fp_ready()) return real_isatty(fd); hits++; long r = sc(SYS_ioctl, fd, TCGETS, (long)&k, 0, 0); + if (refused()) return real_isatty(fd); if (r < 0) { errno = (int)-r; return 0; } return r == 0; } diff --git a/tools/proot/fastpath/svc-cache.h b/tools/proot/fastpath/svc-cache.h index 57c1be4bab..c346c3d21b 100644 --- a/tools/proot/fastpath/svc-cache.h +++ b/tools/proot/fastpath/svc-cache.h @@ -39,7 +39,7 @@ static int svc_cache_name(const struct dl_phdr_info *info, char *path, size_t si id[2 * j + 1] = hex[b & 15]; } id[2 * h.n_descsz] = 0; - int n = snprintf(path, size, "/tmp/.droiddeck-svc-v2-%d-%s", svc_android_api, id); + int n = snprintf(path, size, "/tmp/.droiddeck-svc-v3-%d-%s", svc_android_api, id); return n > 0 && (size_t)n < size; } at += name + desc; diff --git a/tools/proot/fastpath/svc-dispatch.h b/tools/proot/fastpath/svc-dispatch.h index b8e87475fd..a5e88cdb66 100644 --- a/tools/proot/fastpath/svc-dispatch.h +++ b/tools/proot/fastpath/svc-dispatch.h @@ -29,7 +29,7 @@ static int svc_supported(int nr) { switch (nr) { case SYS_openat: case SYS_newfstatat: case SYS_statx: case SYS_readlinkat: case SYS_faccessat: case SYS_mkdirat: case SYS_unlinkat: case SYS_renameat: - case SYS_renameat2: case SYS_symlinkat: case SYS_fchmodat: case SYS_utimensat: + case SYS_renameat2: case SYS_symlinkat: case SYS_fchmodat: case SYS_fchownat: case SYS_utimensat: case SYS_truncate: case SYS_statfs: case SYS_getxattr: case SYS_lgetxattr: case SYS_listxattr: case SYS_llistxattr: case SYS_inotify_add_watch: case SYS_getcwd: return 1; @@ -112,6 +112,7 @@ static long svc_path_call(const long *r) { return fp_rename((int)r[0], path, (int)r[2], other, r[8] == SYS_renameat2 ? (unsigned)r[4] : 0); case SYS_symlinkat: return fp_symlink(path, (int)r[1], other); case SYS_fchmodat: return fp_chmod((int)r[0], path, (mode_t)r[2], 0); + case SYS_fchownat: return fp_chown((int)r[0], path, (uid_t)r[2], (gid_t)r[3], (int)r[4]); case SYS_utimensat: return fp_utimens((int)r[0], path, when, (int)r[3]); case SYS_truncate: return fp_truncate(path, (off_t)r[1]); case SYS_statfs: { diff --git a/tools/proot/fastpath/svc-patch.h b/tools/proot/fastpath/svc-patch.h index 6d19da3156..09cb014089 100644 --- a/tools/proot/fastpath/svc-patch.h +++ b/tools/proot/fastpath/svc-patch.h @@ -74,11 +74,14 @@ long fp_svc_dispatch(const long *r) { int saved_errno = errno; long result = svc_blocked(r); uintptr_t frame = (uintptr_t)r; - if (result == FP_SLOW && (!svc_busy || frame >= svc_busy)) { + if (result == FP_SLOW && __atomic_load_n(&fp_on, __ATOMIC_RELAXED) && (!svc_busy || frame >= svc_busy)) { uintptr_t previous = svc_busy; svc_busy = frame; - if (svc_stack_room(frame)) { result = svc_path_call(r); svc_busy = 0; } - else svc_busy = previous; + if (svc_stack_room(frame)) { + result = svc_path_call(r); + if (refused()) result = FP_SLOW; + svc_busy = 0; + } else svc_busy = previous; } if (result == FP_SLOW) { __atomic_fetch_add(&svc_slow, 1, __ATOMIC_RELAXED); diff --git a/tools/proot/patches/0025-fastpath-trampoline-sigsys-enosys.patch b/tools/proot/patches/0025-fastpath-trampoline-sigsys-enosys.patch new file mode 100644 index 0000000000..d9d02f9283 --- /dev/null +++ b/tools/proot/patches/0025-fastpath-trampoline-sigsys-enosys.patch @@ -0,0 +1,57 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: maxjivi05 +Date: Sat, 7 Nov 2026 12:00:00 -0500 +Subject: [PATCH] fastpath: answer a host-refused trampoline syscall with ENOSYS + instead of translating its host path again + +--- +--- a/src/syscall/seccomp.c ++++ b/src/syscall/seccomp.c +@@ -812,6 +812,16 @@ + return 0; + } + ++bool from_fastpath_stub(const Tracee *tracee) ++{ ++ word_t pc; ++ ++ if (!fastpath_enabled()) ++ return false; ++ pc = peek_reg(tracee, CURRENT, INSTR_POINTER); ++ return pc >= FASTPATH_STUB_ADDR && pc < FASTPATH_STUB_ADDR + FASTPATH_STUB_SIZE; ++} ++ + word_t filtered_sysnum_flags(const Tracee *tracee, Sysnum sysnum) + { + static word_t flags[PR_NB_SYSNUM]; +--- a/src/syscall/seccomp.h ++++ b/src/syscall/seccomp.h +@@ -45,5 +45,6 @@ + + extern int enable_syscall_filtering(const Tracee *tracee); + extern word_t filtered_sysnum_flags(const Tracee *tracee, Sysnum sysnum); ++extern bool from_fastpath_stub(const Tracee *tracee); + + #endif /* SECCOMP_H */ +--- a/src/tracee/seccomp.c ++++ b/src/tracee/seccomp.c +@@ -12,6 +12,7 @@ + #include "cli/note.h" + #include "syscall/chain.h" + #include "syscall/syscall.h" ++#include "syscall/seccomp.h" + #include "tracee/seccomp.h" + #include "tracee/mem.h" + #include "tracee/statx.h" +@@ -123,6 +124,11 @@ + + sysnum = get_sysnum(tracee, CURRENT); + ++ if (from_fastpath_stub(tracee)) { ++ set_result_after_seccomp(tracee, -ENOSYS); ++ return 0; ++ } ++ + status = notify_extensions(tracee, SIGSYS_OCC, 0, 0); + if (status < 0) { + VERBOSE(tracee, 4, "SIGSYS errored out when being handled by an extension"); diff --git a/tools/proot/patches/0026-arm64-kernel-program-layout.patch b/tools/proot/patches/0026-arm64-kernel-program-layout.patch new file mode 100644 index 0000000000..57e05e452d --- /dev/null +++ b/tools/proot/patches/0026-arm64-kernel-program-layout.patch @@ -0,0 +1,17 @@ +diff --git a/src/arch.h b/src/arch.h +index 06b7f1b..db129a9 100644 +--- a/src/arch.h ++++ b/src/arch.h +@@ -135,9 +135,9 @@ typedef unsigned char byte_t; + #define OFFSETOF_STAT_UID_32 24 + #define OFFSETOF_STAT_GID_32 28 + +- #define LOADER_ADDRESS 0x2000000000 +- #define EXEC_PIC_ADDRESS 0x3000000000 +- #define INTERP_PIC_ADDRESS 0x3f00000000 ++ #define LOADER_ADDRESS 0x5400000000 ++ #define EXEC_PIC_ADDRESS 0x5555000000 ++ #define INTERP_PIC_ADDRESS 0x5f00000000 + #define HAS_POKEDATA_WORKAROUND true + + #define HAS_LOADER_32BIT true diff --git a/tools/proot/patches/0027-guest-seccomp-sigsys.patch b/tools/proot/patches/0027-guest-seccomp-sigsys.patch new file mode 100644 index 0000000000..73e7c3ce14 --- /dev/null +++ b/tools/proot/patches/0027-guest-seccomp-sigsys.patch @@ -0,0 +1,59 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Kurt Himebauch +Date: Mon, 9 Nov 2026 15:30:00 -0500 +Subject: [PATCH] seccomp: hand a guest filter's own SIGSYS back to the guest + +--- +--- a/src/tracee/seccomp.h ++++ b/src/tracee/seccomp.h +@@ -1,3 +1,4 @@ ++#include + #include "tracee/tracee.h" + #include "sys/vfs.h" + +@@ -18,5 +19,5 @@ + + void restart_syscall_after_seccomp(Tracee* tracee); + void set_result_after_seccomp(Tracee *tracee, word_t result); +-int handle_seccomp_event(Tracee* tracee); ++int handle_seccomp_event(Tracee* tracee, const siginfo_t *siginfo); + void fix_and_restart_enosys_syscall(Tracee* tracee); +--- a/src/tracee/seccomp.c ++++ b/src/tracee/seccomp.c +@@ -68,7 +68,7 @@ + * + * Return 0 to swallow signal or SIGSYS to deliver it to process. + */ +-int handle_seccomp_event(Tracee* tracee) ++int handle_seccomp_event(Tracee* tracee, const siginfo_t *siginfo) + { + int ret; + +@@ -100,6 +100,16 @@ + + print_current_regs(tracee, 3, "seccomp SIGSYS"); + ++ /* Android's app policy traps with SECCOMP_RET_DATA 0. A filter the guest ++ * installed itself (Firefox's and Chromium's sandboxes) tags its traps, and ++ * its own SIGSYS handler answers them, usually through a broker process. ++ * A trap from the fast path's trampoline still gets ENOSYS: its arguments ++ * are host paths, and the fast path repeats the call through libc. */ ++ if (siginfo->si_errno != 0 && !from_fastpath_stub(tracee)) { ++ VERBOSE(tracee, 4, "SIGSYS from the guest's own seccomp filter (data %d)", siginfo->si_errno); ++ return SIGSYS; ++ } ++ + return handle_seccomp_event_common(tracee); + } + +--- a/src/tracee/event.c ++++ b/src/tracee/event.c +@@ -676,7 +676,7 @@ + tracee->skip_next_seccomp_signal = false; + signal = 0; + } else { +- signal = handle_seccomp_event(tracee); ++ signal = handle_seccomp_event(tracee, &siginfo); + } + } else { + VERBOSE(tracee, 1, "non-seccomp SIGSYS"); diff --git a/tools/quest/build-driver.py b/tools/quest/build-driver.py index 43c7c5a3be..b11ec27d6d 100644 --- a/tools/quest/build-driver.py +++ b/tools/quest/build-driver.py @@ -7,6 +7,9 @@ import sys import tarfile import tempfile import hashlib +import http.client +import time +import urllib.error import urllib.request import zipfile @@ -32,11 +35,23 @@ def pack(entries, output): print(output) +def download(url, attempts=4): + for attempt in range(1, attempts + 1): + try: + with urllib.request.urlopen(url, timeout=120) as response: + return response.read() + except (OSError, http.client.HTTPException) as error: + if attempt == attempts or isinstance(error, urllib.error.HTTPError) and error.code < 500 and error.code != 429: + raise + print(f'Download failed ({error}); retrying in {attempt * 15} s') + time.sleep(attempt * 15) + + def build_a6xx(ndk, output_dir=OUTPUT.parent, archive_path=None): cached = archive_path or ROOT / 'app/build/quest-inputs' / f'Turnip-{A6XX_TAG}.zip' if not cached.exists(): cached.parent.mkdir(parents=True, exist_ok=True) - data = urllib.request.urlopen(A6XX_URL, timeout=120).read() + data = download(A6XX_URL) if hashlib.sha256(data).hexdigest() != A6XX_SHA256: raise ValueError('A6xx archive checksum mismatch') cached.write_bytes(data) diff --git a/tools/release/pr_discord.py b/tools/release/pr_discord.py index b577df247b..77a576208e 100644 --- a/tools/release/pr_discord.py +++ b/tools/release/pr_discord.py @@ -101,23 +101,31 @@ def description_excerpt(body): return "" -def build_content(repo, number, title, sha, apk, release, description=None): +def commit_subject(message): + subject = (message or "").splitlines()[0].strip() if message else "" + return plain_title(subject[:300]) + + +def build_content(repo, number, title, sha, apk, release, description=None, commit_message=None): excerpt = description_excerpt(description) + subject = commit_subject(commit_message) return (f"**Signed test build** `pr-{number}` · `{sha[:7]}`\n" f"[#{number}](https://github.com/{repo}/pull/{number}): {plain_title(title)}\n" + + (f"Latest commit: {subject}\n" if subject else "") + (f"\n{excerpt}\n\n" if excerpt else "") + "_Not merged — for testing._\n" f"In the app: **Updates → Test builds → #{number}**\n" f"Download: [standard APK](<{apk}>) · [all variants](<{release}>)") -def retired_content(content, merged): - label = "Merged" if merged else "Closed" - if content.startswith(("**Merged** —", "**Closed** —")): +def retired_content(content, merged=False, superseded_by=None): + label = f"Superseded by `{superseded_by[:7]}`" if superseded_by else ("Merged" if merged else "Closed") + if content.startswith(("**Merged** —", "**Closed** —", "**Superseded by `")): content = "\n".join(line[2:-2] if line.startswith("~~") and line.endswith("~~") else line for line in content.splitlines()[2:]) struck = "\n".join(f"~~{line}~~" if line else "" for line in content.splitlines()) - return f"**{label}** — this test build is no longer available.\n\n{struck}" + note = "This test build has been replaced." if superseded_by else "This test build is no longer available." + return f"**{label}** — {note}\n\n{struck}" def sync(mode, repo, number, discord, sha="", apk="", release=""): @@ -136,6 +144,11 @@ def sync(mode, repo, number, discord, sha="", apk="", release=""): return raise RuntimeError("Signed build report comment is missing") message_id = saved_message(comment["body"], discord.id) + state_match = STATE.search(comment["body"]) + try: + old_state = json.loads(state_match.group(1)) if state_match else {} + except ValueError: + old_state = {} if mode == "retire": if message_id: message = discord.request("GET", f"/messages/{message_id}") @@ -144,16 +157,41 @@ def sync(mode, repo, number, discord, sha="", apk="", release=""): {"content": retired_content(message["content"], pull.get("merged", False)), "allowed_mentions": {"parse": []}, "flags": 4}) return - content = build_content(repo, number, pull["title"], sha, apk, release, pull.get("body")) + try: + commit = github(f"repos/{repo}/commits/{sha}") or {} + except RuntimeError: + commit = {} + commit_message = (commit.get("commit") or {}).get("message", "") + content = build_content(repo, number, pull["title"], sha, apk, release, pull.get("body"), commit_message) if len(content) > 1800: raise RuntimeError("Discord announcement is too long") payload = {"content": content, "allowed_mentions": {"parse": []}, "flags": 4} - message = discord.request("PATCH", f"/messages/{message_id}", payload) if message_id else None + old_message = None + previous_sha = old_state.get("sha") + if message_id and not previous_sha: + old_message = discord.request("GET", f"/messages/{message_id}") + if old_message: + match = re.search(r"`([0-9a-f]{7})`", old_message.get("content", "")) + previous_sha = match.group(1) if match else None + if message_id and (previous_sha == sha or previous_sha == sha[:7]): + message = discord.request("PATCH", f"/messages/{message_id}", payload) + else: + if message_id and old_state.get("sha"): + previous = discord.request("GET", f"/messages/{message_id}") + if previous: + discord.request("PATCH", f"/messages/{message_id}", + {"content": retired_content(previous["content"], superseded_by=sha), + "allowed_mentions": {"parse": []}, "flags": 4}) + elif message_id and old_message: + discord.request("PATCH", f"/messages/{message_id}", + {"content": retired_content(old_message["content"], superseded_by=sha), + "allowed_mentions": {"parse": []}, "flags": 4}) + message = None if message is None: message = discord.request("POST", "?wait=true", payload) if message is None: raise RuntimeError("Discord webhook no longer exists") - state = {"webhook_id": discord.id, "message_id": message["id"]} + state = {"webhook_id": discord.id, "message_id": message["id"], "sha": sha} latest = github(f"repos/{repo}/issues/comments/{comment['id']}") body = STATE.sub("", latest["body"]).rstrip() + f"\n" github(f"repos/{repo}/issues/comments/{comment['id']}", "PATCH", {"body": body}) diff --git a/tools/release/runtime_inputs.py b/tools/release/runtime_inputs.py index 806c49f79e..57fe792465 100644 --- a/tools/release/runtime_inputs.py +++ b/tools/release/runtime_inputs.py @@ -14,15 +14,16 @@ ROOT = Path(__file__).resolve().parents[2] MANIFEST = Path('app/build/runtime-inputs.json') PACKS = Path('app/build/packs') PACKED = {'assets/linuxfs/usr/local/lib/droiddeck-xbox/': 'xbox', 'assets/linuxfs/usr/local/lib/droiddeck-comet/': 'comet', + 'assets/linuxfs/usr/local/lib/droiddeck-ubisoft/': 'ubisoft', 'assets/directaudio/': 'directaudio', 'assets/hybris/': 'powervr'} PACKED_FILES = {'lib/arm64-v8a/libproot.so': ('proot', 'libproot.so'), 'lib/arm64-v8a/libproot-loader.so': ('proot', 'libproot-loader.so')} PACKED_LIBRARIES = ['lib/arm64-v8a/libopencv_java4.so', 'lib/arm64-v8a/libmediapipe_tasks_jni.so'] -PACK_NAMES = ['proot', 'directaudio', 'tracking', 'xbox', 'comet', 'butler', 'powervr'] +PACK_NAMES = ['proot', 'directaudio', 'tracking', 'xbox', 'comet', 'butler', 'powervr', 'ubisoft'] SOURCE_PATHS = ['app/build.gradle', 'app/src/main/cpp/fakeinput_steam.cpp', 'app/src/main/assets/pulseaudio.tzst', 'tools/linuxfs', 'tools/proot', 'tools/aaudio-sink', 'tools/directaudio', 'tools/packs', 'tools/gamescope/release.env', 'tools/droiddeck-esync/release.env', - 'tools/mangoapp', 'tools/msitools', 'tools/pipewire', 'tools/comet', 'tools/xbox', 'tools/dirt-ws2', 'tools/components', 'tools/ci', 'tools/build_local.sh', + 'tools/mangoapp', 'tools/msitools', 'tools/pipewire', 'tools/comet', 'tools/xbox', 'tools/dirt-ws2', 'tools/ubisoft-launcher', 'tools/components', 'tools/ci', 'tools/build_local.sh', 'tools/powervr', 'tools/hybris', 'tools/dxvk', 'tools/vulkan-wsi', 'tools/release/runtime_inputs.py', 'app/src/main/assets/desktop-removal', 'tools/quest', 'tools/vr/src', 'tools/vr/driver_droiddeck', 'tools/vr/layer', 'tools/vr/build.sh', @@ -76,7 +77,7 @@ def required(): paths += [f'assets/graphics_driver/adrenotools-{name}.tzst' for name in ['turnip26.3.0-a6xx', 'turnip26.3.0-quest2']] paths += ['assets/linuxfs/' + name for name in ['libfakeinput.so', 'libblsession.so', - 'libblfastpath.so', 'libssbs.so', 'usr/local/bin/droiddeck-clipboard', + 'libblfastpath.so', 'libddnatv-hook.so', 'libssbs.so', 'usr/local/bin/droiddeck-clipboard', 'usr/local/bin/droiddeck-gog-maintenance', 'usr/local/bin/gamescope', 'usr/local/lib/droiddeck/uruntime', 'usr/local/lib/droiddeck-pipewire.tzst']] @@ -211,7 +212,8 @@ def pack_digests(root, name): def check_apk(apk, root=ROOT): data = verify(root) - packs = {name: pack_digests(root, name) for name in PACK_NAMES} + packs = {name: pack_digests(root, name) for name in PACK_NAMES + if name != 'ubisoft' or (root / PACKS / 'ubisoft.tar.zst').is_file()} with ZipFile(apk) as package: names = set(package.namelist()) for library in PACKED_LIBRARIES: @@ -231,7 +233,7 @@ def check_apk(apk, root=ROOT): if prefix: if name in names: raise ValueError('APK still carries ' + name + ', which the ' + PACKED[prefix] + ' pack provides') - if packs[PACKED[prefix]].get(name[len(prefix):]) != output['sha256']: + if packs.get(PACKED[prefix], {}).get(name[len(prefix):]) != output['sha256']: raise ValueError('Add-on pack ' + PACKED[prefix] + ' is missing or has a stale ' + name) continue try: diff --git a/tools/release/validate_apk.py b/tools/release/validate_apk.py index 3ff90c2e27..1e7e31f960 100644 --- a/tools/release/validate_apk.py +++ b/tools/release/validate_apk.py @@ -63,6 +63,10 @@ def main(): expected = run["head_sha"] if run["event"] == "pull_request": prs = api(f"repos/{repo}/commits/{expected}/pulls") + owner = (run.get("head_repository") or {}).get("owner", {}).get("login") + if not prs and owner and run.get("head_branch"): + query = urlencode({"state": "open", "head": f"{owner}:{run['head_branch']}"}) + prs = api(f"repos/{repo}/pulls?{query}") pr_number = next((pr["number"] for pr in prs if pr["state"] == "open" and pr["head"]["sha"] == expected), None) if pr_number is None: diff --git a/tools/release/virustotal-scan.sh b/tools/release/virustotal-scan.sh new file mode 100755 index 0000000000..8138a428ae --- /dev/null +++ b/tools/release/virustotal-scan.sh @@ -0,0 +1,89 @@ +#!/usr/bin/env bash +# Usage: virustotal-scan.sh ... +# Scans each file on VirusTotal and writes the verdicts to $GITHUB_STEP_SUMMARY. +# Exits 1 when an engine flags a file and FAIL_ON_DETECTION=1. +set -euo pipefail + +API=${VT_API:-https://www.virustotal.com/api/v3} +: "${VT_API_KEY:?VT_API_KEY is not set}" +POLL_SECONDS=${POLL_SECONDS:-30} +TIMEOUT_SECONDS=${TIMEOUT_SECONDS:-1200} +SUMMARY=${GITHUB_STEP_SUMMARY:-/dev/stdout} + +vt() { + curl -sS --fail-with-body --retry 5 --retry-delay 20 --retry-all-errors \ + -H "x-apikey: $VT_API_KEY" "$@" +} + +# Workflow commands end at a newline and decode %XX sequences. +warn() { + local message=$1 + message=${message//'%'/'%25'} + message=${message//$'\r'/'%0D'} + message=${message//$'\n'/'%0A'} + printf '::warning title=VirusTotal::%s\n' "$message" +} + +# The free API allows 4 requests a minute; poll slowly. +wait_for_analysis() { + local id=$1 waited=0 status + while :; do + status=$(vt "$API/analyses/$id" | jq -r .data.attributes.status) + [ "$status" = completed ] && return 0 + [ "$waited" -ge "$TIMEOUT_SECONDS" ] && return 1 + sleep "$POLL_SECONDS" + waited=$((waited + POLL_SECONDS)) + done +} + +flagged_total=0 +{ + echo "### VirusTotal" + echo + echo "| File | Flagged | Engines | Detections |" + echo "| --- | --- | --- | --- |" +} >> "$SUMMARY" + +for file in "$@"; do + name=$(basename "$file") + sha=$(sha256sum "$file" | cut -d' ' -f1) + link="https://www.virustotal.com/gui/file/$sha" + + known=$(curl -sS --retry 5 --retry-delay 20 -o /dev/null -w '%{http_code}' \ + -H "x-apikey: $VT_API_KEY" "$API/files/$sha") + if [ "$known" != 200 ] && [ "$known" != 404 ]; then + echo "::error title=VirusTotal::hash lookup for $name returned HTTP $known" + exit 1 + fi + if [ "$known" = 404 ]; then + # Files over 32 MB need a one-time upload URL. + url=$(vt "$API/files/upload_url" | jq -r .data) + id=$(vt -X POST -F "file=@$file" "$url" | jq -r .data.id) + echo "uploaded $name ($sha), analysis $id" + if ! wait_for_analysis "$id"; then + warn "$name was still being analysed after ${TIMEOUT_SECONDS}s: $link" + echo "| [$name]($link) | pending | | |" >> "$SUMMARY" + continue + fi + fi + + report=$(vt "$API/files/$sha") + malicious=$(jq -r '.data.attributes.last_analysis_stats.malicious // 0' <<<"$report") + suspicious=$(jq -r '.data.attributes.last_analysis_stats.suspicious // 0' <<<"$report") + engines=$(jq -r '[(.data.attributes.last_analysis_stats // {})[]] | add // 0' <<<"$report") + detections=$(jq -r '[(.data.attributes.last_analysis_results // {})[] + | select(.category == "malicious" or .category == "suspicious") + | "\(.engine_name): \(.result)"] | join("
")' <<<"$report") + flagged=$((malicious + suspicious)) + flagged_total=$((flagged_total + flagged)) + echo "$name: $flagged/$engines flagged $link" + echo "| [$name]($link) | $flagged | $engines | ${detections:-none} |" >> "$SUMMARY" + if [ "$flagged" -gt 0 ]; then + warn "$name flagged by $flagged/$engines engines: ${detections//
/, } $link" + fi +done + +if [ "$flagged_total" -gt 0 ] && [ "${FAIL_ON_DETECTION:-0}" = 1 ]; then + echo "::error::VirusTotal flagged the release" + exit 1 +fi diff --git a/tools/tests/test_appimage_run.py b/tools/tests/test_appimage_run.py index 5e0c5dd658..14ac8ad676 100644 --- a/tools/tests/test_appimage_run.py +++ b/tools/tests/test_appimage_run.py @@ -6,7 +6,7 @@ import unittest from pathlib import Path RUN = Path(__file__).resolve().parents[1] / "linuxfs/overlay/usr/local/bin/droiddeck-appimage-run" -KEYS = ("QT_QPA_PLATFORM", "WAYLAND_DISPLAY", "SDL_VIDEODRIVER", "XDG_SESSION_TYPE", "QT_WAYLAND_DISABLE_WINDOWDECORATION", "DISABLE_GAMESCOPE_WSI") +KEYS = ("TU_DEBUG", "DROIDDECK_GAME_TU_DEBUG", "QT_QPA_PLATFORM", "WAYLAND_DISPLAY", "SDL_VIDEODRIVER", "XDG_SESSION_TYPE", "QT_WAYLAND_DISABLE_WINDOWDECORATION", "DISABLE_GAMESCOPE_WSI") QUICK = ("usr/lib/libQt6Quick.so.6", "usr/lib/libvulkan.so.1", "usr/plugins/platforms/libqwayland-generic.so") RUN_MODE = {"GAMESCOPE_WAYLAND_DISPLAY": "gamescope-0", "WAYLAND_DISPLAY": "gamescope-0", "DISPLAY": ":0", "QT_QPA_PLATFORM": "xcb", "SDL_VIDEODRIVER": "x11", "XDG_SESSION_TYPE": "x11"} @@ -128,5 +128,24 @@ class AppImageRunTest(unittest.TestCase): self.assertEqual("wayland;xcb", env["QT_QPA_PLATFORM"]) + def test_a_program_drops_the_sessions_sysmem_like_a_steam_game(self): + env, _ = self.launch(dict(RUN_MODE, TU_DEBUG="sysmem", DROIDDECK_GAME_TU_DEBUG="")) + self.assertEqual("unset", env["TU_DEBUG"]) + self.assertEqual("unset", env["DROIDDECK_GAME_TU_DEBUG"]) + + def test_a_program_gets_the_game_driver_setting(self): + env, _ = self.launch(dict(RUN_MODE, TU_DEBUG="sysmem", DROIDDECK_GAME_TU_DEBUG="gmem")) + self.assertEqual("gmem", env["TU_DEBUG"]) + + def test_a_chromium_app_keeps_the_session_setting(self): + self.bundle("chrome_100_percent.pak") + env, _ = self.launch(dict(RUN_MODE, TU_DEBUG="sysmem", DROIDDECK_GAME_TU_DEBUG="")) + self.assertEqual("sysmem", env["TU_DEBUG"]) + + def test_without_the_game_setting_the_session_value_stays(self): + env, _ = self.launch(dict(RUN_MODE, TU_DEBUG="sysmem")) + self.assertEqual("sysmem", env["TU_DEBUG"]) + + if __name__ == "__main__": unittest.main() diff --git a/tools/tests/test_binfmt_preload.py b/tools/tests/test_binfmt_preload.py index 063345edb2..9966b28505 100644 --- a/tools/tests/test_binfmt_preload.py +++ b/tools/tests/test_binfmt_preload.py @@ -22,22 +22,29 @@ extern char **environ; int main(int argc, char **argv) { if (argc < 3) return 2; const char *how = argv[1], *file = argv[2]; + char *node_env[] = {"ELECTRON_RUN_AS_NODE=1", NULL}; + char *const *child_env = !strcmp(how, "execve-node") || !strcmp(how, "execvpe-node") || + !strcmp(how, "spawn-node") || !strcmp(how, "spawnp-node") ? node_env : environ; char **args = argv + 2; if (!strcmp(how, "access")) { int result = access(file, X_OK); printf("access %d %d\n", result, result ? errno : 0); return 0; } - if (!strcmp(how, "spawn") || !strcmp(how, "spawnp")) { + if (!strcmp(how, "spawn") || !strcmp(how, "spawnp") || !strcmp(how, "spawn-node") || + !strcmp(how, "spawnp-node")) { pid_t pid; - int error = how[5] ? posix_spawnp(&pid, file, NULL, NULL, args, environ) : posix_spawn(&pid, file, NULL, NULL, args, environ); + int spawnp = !strcmp(how, "spawnp") || !strcmp(how, "spawnp-node"); + int error = spawnp ? posix_spawnp(&pid, file, NULL, NULL, args, child_env) + : posix_spawn(&pid, file, NULL, NULL, args, child_env); if (error) { printf("errno %d\n", error); return 3; } int status; waitpid(pid, &status, 0); printf("exit %d\n", WEXITSTATUS(status)); return 0; } - if (!strcmp(how, "execve")) execve(file, args, environ); + if (!strcmp(how, "execve") || !strcmp(how, "execve-node")) execve(file, args, (char *const *)child_env); + else if (!strcmp(how, "execvpe-node")) execvpe(file, args, (char *const *)child_env); else if (!strcmp(how, "execv")) execv(file, args); else if (!strcmp(how, "execvp")) execvp(file, args); else if (!strcmp(how, "execvpe")) execvpe(file, args, environ); @@ -88,14 +95,29 @@ class BinfmtPreloadTest(unittest.TestCase): path.chmod(mode) return path - def run_program(self, how, target, *args, desktop=True): + def chromium_script(self): + folder = self.dir / "vscode" + folder.mkdir() + executable = folder / "code" + executable.write_text("#!/bin/sh\nprintf '%s\\n' \"$@\"\n") + executable.chmod(0o755) + (folder / "chrome-sandbox").touch() + return executable + + def environment(self, desktop=True, **extra): env = {"PATH": "%s:/usr/bin:/bin" % self.dir, "LD_PRELOAD": str(self.library), "HANDLER_LOG": str(self.log)} if desktop: env["BL_DESKTOP"] = "1" - done = subprocess.run([str(self.program), how, str(target)] + list(args), env=env, + env.update(extra) + return env + + def handed(self): + return self.log.read_text().splitlines() if self.log.exists() else None + + def run_program(self, how, target, *args, desktop=True, **extra): + done = subprocess.run([str(self.program), how, str(target)] + list(args), env=self.environment(desktop, **extra), stdout=subprocess.PIPE, text=True, timeout=30) - handed = self.log.read_text().splitlines() if self.log.exists() else None - return done.returncode, done.stdout.strip(), handed + return done.returncode, done.stdout.strip(), self.handed() def test_an_x86_program_goes_to_the_handler_before_it_is_executed(self): tool = self.x86() @@ -155,6 +177,87 @@ class BinfmtPreloadTest(unittest.TestCase): self.file("script.sh", b"#!/bin/sh\nexit 5\n") self.assertEqual(self.run_program("spawnp", "script.sh"), (0, "exit 5", None)) + def test_explicit_node_child_environment_skips_injection_for_execve_and_spawn(self): + code = self.chromium_script() + self.assertEqual(self.run_program("execve-node", code, "--version"), (0, "--version", None)) + self.assertEqual(self.run_program("execvpe-node", code, "--version"), (0, "--version", None)) + self.assertEqual(self.run_program("spawn-node", code, "--version"), (0, "--version\nexit 0", None)) + self.assertEqual(self.run_program("spawnp-node", code, "--version"), (0, "--version\nexit 0", None)) + + def test_chromium_gui_launch_without_node_mode_still_receives_no_sandbox(self): + code = self.chromium_script() + self.assertEqual(self.run_program("execve", code, "--version"), (0, "--no-sandbox\n--version", None)) + + def test_the_hand_off_switch_works_without_the_desktop(self): + tool = self.x86() + for how in ("execve", "execvp"): + self.log.unlink(missing_ok=True) + self.assertEqual(self.run_program(how, tool, "a", desktop=False, BL_BINFMT="1"), (7, "", ["--exec", str(tool), "a"])) + self.log.unlink() + self.assertEqual(self.run_program("spawnp", "tool", "b", desktop=False, BL_BINFMT="1"), (0, "exit 7", ["--exec", str(tool), "b"])) + + def test_only_the_value_one_switches_the_hand_off_on(self): + for value in ("0", "", "11", "yes"): + with self.subTest(value=value): + self.assertEqual(self.run_program("execve", self.x86(), desktop=False, BL_BINFMT=value), (0, "", None)) + + def test_the_desktop_hands_off_whatever_the_switch_says(self): + tool = self.x86() + for value in ("0", ""): + with self.subTest(value=value): + self.log.unlink(missing_ok=True) + self.assertEqual(self.run_program("execve", tool, BL_BINFMT=value), (7, "", ["--exec", str(tool)])) + + def script_child(self, *lines, **extra): + script = self.file("start.sh", ("#!/bin/bash\ncd \"${0%%/*}\"\n%s\n" % "\n".join(lines)).encode()) + done = subprocess.run([str(script)], env=self.environment(**extra), stdout=subprocess.PIPE, text=True, timeout=30) + return done.returncode, done.stdout.strip(), self.handed() + + @unittest.skipUnless(os.path.exists("/bin/bash"), "needs bash") + def test_an_x86_program_a_native_script_runs_goes_to_the_handler(self): + tool = self.x86() + self.assertEqual(self.script_child("./tool one", "echo after $?", desktop=False, BL_BINFMT="1"), + (0, "after 7", ["--exec", "./tool", "one"])) + self.log.unlink() + self.assertEqual(self.script_child("exec ./tool two", desktop=False, BL_BINFMT="1"), (7, "", ["--exec", str(tool), "two"])) + + @unittest.skipUnless(os.path.exists("/bin/bash"), "needs bash") + def test_native_children_of_a_script_run_as_themselves(self): + self.file("child.sh", b"#!/bin/sh\nexit 5\n") + self.assertEqual(self.script_child("./child.sh", "echo $?", desktop=False, BL_BINFMT="1"), (0, "5", None)) + + @unittest.skipUnless(os.path.exists("/bin/bash"), "needs bash") + def test_outside_the_desktop_and_script_runs_a_scripts_children_are_untouched(self): + self.x86() + self.assertEqual(self.script_child("./tool", "echo $?", desktop=False), (0, "0", None)) + + +@unittest.skipUnless(os.path.exists("/bin/bash") and not os.path.exists("/usr/local/bin/droiddeck-fex"), + "needs bash and no droiddeck-fex on this host") +class ScriptRunTest(unittest.TestCase): + def run_app(self, env, fex=None): + tmp = Path(tempfile.mkdtemp()) + self.addCleanup(shutil.rmtree, tmp, True) + (tmp / "app/files").mkdir(parents=True) + (tmp / "app/entry").write_text("go.sh") + if fex: + (tmp / "app/fex").write_text(fex) + (tmp / "app/files/go.sh").write_text("#!/bin/bash\necho \"${BL_BINFMT-unset} ${BL_DESKTOP-unset}\"\n") + runner = Path(__file__).resolve().parents[1] / "linuxfs/overlay/usr/local/bin/droiddeck-script-run" + base = {"PATH": "/usr/bin:/bin", "DBUS_SESSION_BUS_ADDRESS": "unix:path=/nonexistent"} + done = subprocess.run(["/bin/bash", str(runner), str(tmp / "app")], stdout=subprocess.PIPE, text=True, timeout=60, + env=dict(base, **env)) + return done.returncode, done.stdout.strip().splitlines()[-1] + + def test_a_script_run_switches_the_hand_off_on_for_its_children_only(self): + self.assertEqual(self.run_app({}), (0, "1 unset")) + self.assertEqual(self.run_app({"BL_BINFMT": "0", "BL_DESKTOP": "1"}), (0, "1 1")) + self.assertEqual(self.run_app({}, fex="on"), (0, "1 unset")) + + def test_fex_off_keeps_x86_children_away_from_the_hand_off(self): + self.assertEqual(self.run_app({}, fex="off"), (0, "unset unset")) + self.assertEqual(self.run_app({"BL_BINFMT": "1"}, fex="off"), (0, "unset unset")) + if __name__ == "__main__": unittest.main() diff --git a/tools/tests/test_ci_checks.py b/tools/tests/test_ci_checks.py index 52c1607aad..125dba0398 100644 --- a/tools/tests/test_ci_checks.py +++ b/tools/tests/test_ci_checks.py @@ -48,35 +48,69 @@ class CiChecksTest(unittest.TestCase): def test_independent_jobs_wait_only_for_the_pinned_source(self): jobs = yaml.safe_load((ROOT / '.github/workflows/build.yml').read_text())['jobs'] - for name in ('xbox', 'components', 'python-tests'): + for name in ('xbox', 'python-tests', 'components-runner'): self.assertEqual(jobs[name]['needs'], 'source') - self.assertEqual(set(jobs['build']['needs']), {'source', 'components', 'xbox'}) + self.assertEqual(set(jobs['components']['needs']), {'source', 'components-runner'}) + self.assertEqual(jobs['build-runner']['needs'], ['components']) + self.assertEqual(set(jobs['build']['needs']), {'source', 'components', 'xbox', 'build-runner'}) def test_dependency_update_errors_block_installation(self): with tempfile.TemporaryDirectory() as directory: root = Path(directory) sudo = root / 'sudo' - sudo.write_text('#!/bin/bash\nprintf "%s\\n" "$*" >> "$APT_LOG"\nexit "${APT_RESULT:-0}"\n') + sudo.write_text('#!/bin/bash\nprintf "%s\\n" "$*" >> "$APT_LOG"\n' + 'for arg; do case "$arg" in Dir::Etc::sourcelist=*) ' + 'grep -h "^URIs:" "${arg#*=}" >> "$APT_LOG.mirrors";; esac; done\n' + 'exit "${APT_RESULT:-0}"\n') sudo.chmod(0o755) query = root / 'dpkg-query' query.write_text('#!/bin/sh\nexit 1\n') query.chmod(0o755) + dpkg = root / 'dpkg' + dpkg.write_text('#!/bin/sh\necho "${DPKG_ARCH:-amd64}"\n') + dpkg.chmod(0o755) log = root / 'apt.log' - env = dict(os.environ, PATH=str(root) + os.pathsep + os.environ['PATH'], APT_LOG=str(log)) + sources = root / 'ubuntu.sources' + sources.write_text('URIs: http://mirror.invalid/ubuntu\n') + env = dict(os.environ, PATH=str(root) + os.pathsep + os.environ['PATH'], APT_LOG=str(log), + UBUNTU_SOURCES_FILE=str(sources)) installer = ROOT / 'tools/ci/install-ubuntu-packages.sh' result = subprocess.run(['bash', str(installer), 'qemu-user'], env=dict(env, APT_RESULT='17')) self.assertEqual(result.returncode, 17) - self.assertEqual(len(log.read_text().splitlines()), 2) + self.assertEqual(len(log.read_text().splitlines()), 3) + mirrors = Path(str(log) + '.mirrors') + self.assertEqual(mirrors.read_text().splitlines(), [ + 'URIs: http://mirror.invalid/ubuntu', + 'URIs: https://archive.ubuntu.com/ubuntu', + 'URIs: https://mirrors.edge.kernel.org/ubuntu', + ]) log.unlink() + mirrors.unlink() + result = subprocess.run(['bash', str(installer), 'qemu-user'], env=dict(env, APT_RESULT='17', DPKG_ARCH='arm64')) + self.assertEqual(result.returncode, 17) + self.assertEqual(mirrors.read_text().splitlines()[1:], [ + 'URIs: https://ports.ubuntu.com/ubuntu-ports', + 'URIs: https://mirrors.ocf.berkeley.edu/ubuntu-ports', + ]) + log.unlink() + mirrors.unlink() subprocess.run(['bash', str(installer), 'qemu-user'], env=env, check=True) update, install = log.read_text().splitlines() for command in (update, install): self.assertIn('Dir::Etc::sourceparts=-', command) + self.assertIn(f'Dir::Etc::sourcelist={sources}', command) self.assertIn('Acquire::https::Timeout=15', command) self.assertIn('Acquire::Retries=1', command) self.assertIn('APT::Update::Error-Mode=any', update) self.assertIn('install -y -q --no-install-recommends qemu-user', install) + log.unlink() + absent = dict(env, UBUNTU_SOURCES_FILE=str(root / 'missing.sources'), APT_RESULT='17') + result = subprocess.run(['bash', str(installer), 'qemu-user'], env=absent) + self.assertEqual(result.returncode, 1) + self.assertEqual(len(log.read_text().splitlines()), 1) + self.assertNotIn('Dir::Etc::sourceparts', log.read_text()) + def test_required_status_rejects_failed_cancelled_or_skipped_test_jobs(self): jobs = yaml.safe_load((ROOT / '.github/workflows/build.yml').read_text())['jobs'] gate = jobs['checks'] diff --git a/tools/tests/test_desktop_open.py b/tools/tests/test_desktop_open.py index 39eee38cf9..c32baf71a6 100644 --- a/tools/tests/test_desktop_open.py +++ b/tools/tests/test_desktop_open.py @@ -315,6 +315,68 @@ class GameFolderTest(Temp): self.assertRegex(requests[0], "^id=[0-9a-f]{16}\nkind=folder\npath=%s\n$" % script) +class ExecTest(Temp): + def setUp(self): + super().setUp() + self.storage = self.tmp / "storage" + self.write("storage/Download/Game/start.sh", b"#!/bin/sh\n./game.x86_64\n", 0o755) + self.write("storage/Download/Game/game.x86_64", elf(62), 0o755) + self.write("storage/Download/Game/helper.bin", elf(183), 0o755) + self.write("storage/Download/Game/libhelper.so", elf(183)) + self.copied = [] + env = {k: v for k, v in os.environ.items() if k not in ("BL_DESKTOP", "BL_BINFMT", "GAMESCOPE_WAYLAND_DISPLAY")} + for patcher in (mock.patch.dict(OPEN, {"noexec": lambda path: True, "storage_root": lambda folder: str(self.storage), + "copied_in": lambda path: self.copied.append(path) or "/opt/droiddeck-apps/game", + "steam_title": lambda path: None, "notify": lambda *a: None, + "CACHE": str(self.tmp / "cache")}), + mock.patch.dict(os.environ, env, clear=True), + mock.patch.object(OPEN["os"], "chdir"), + mock.patch.object(OPEN["os"], "execv", side_effect=self.execv), + mock.patch.object(OPEN["subprocess"], "call", side_effect=self.call)): + patcher.start() + self.addCleanup(patcher.stop) + self.ran = [] + + def execv(self, program, cmd): + self.ran.append(cmd) + raise SystemExit(0) + + def call(self, cmd, *args, **kwargs): + self.ran.append(cmd) + return 0 + + def open(self, *argv, env=None): + os.environ.update(env or {}) + with contextlib.suppress(SystemExit): + OPEN["main"](list(argv)) + return self.ran[-1] + + def test_an_x86_program_a_running_program_starts_runs_under_fex_where_it_lies(self): + program = str(self.storage / "Download/Game/game.x86_64") + for env in ({}, {"BL_BINFMT": "1"}): + with self.subTest(env=env): + self.assertEqual(self.open("--exec", program, "-x", env=env), [OPEN["FEX"], "run", "--mode", "auto", "--", program, "-x"]) + self.assertEqual(self.copied, []) + + def test_an_in_place_hand_off_keeps_the_callers_working_folder(self): + program = str(self.storage / "Download/Game/game.x86_64") + with mock.patch("os.chdir") as chdir: + self.open("--exec", program, env={"BL_BINFMT": "1"}) + chdir.assert_not_called() + self.open("--exec", str(self.storage / "Download/Game/helper.bin")) + chdir.assert_called_once() + + def test_a_native_program_a_running_program_starts_still_gets_its_folder_copied_in(self): + program = str(self.storage / "Download/Game/helper.bin") + self.assertEqual(self.open("--exec", program, "-y"), [OPEN["SCRIPT_RUN"], "/opt/droiddeck-apps/game", "-y"]) + self.assertEqual(self.copied, [program]) + + def test_opening_an_x86_program_from_the_desktop_still_copies_its_folder_in(self): + program = str(self.storage / "Download/Game/game.x86_64") + self.assertEqual(self.open(program, env={"BL_DESKTOP": "1"}), [OPEN["SCRIPT_RUN"], "/opt/droiddeck-apps/game"]) + self.assertEqual(self.copied, [program]) + + def appinfo(apps): strings = [] @@ -420,6 +482,83 @@ class ProtonRunTest(Temp): self.assertEqual(found["droiddeck-proton-arm64"], (str(self.root / "compatibilitytools.d/droiddeck-proton-arm64"), "droiddeck-proton")) + def test_a_valve_depot_is_registered_through_the_droiddeck_wrapper(self): + name = "Proton Experimental (ARM64)" + folder = "steamapps/common/%s" % name + shutil.rmtree(self.root / "compatibilitytools.d/droiddeck-proton-arm64") + self.write("Steam/steamapps/appmanifest_4427310.acf", '"AppState" { "appid" "4427310" }') + self.write("Steam/%s/toolmanifest.vdf" % folder, '"manifest"\n{\n "commandline" "/proton %verb%"\n}\n') + self.write("Steam/%s/proton" % folder, b"#!/bin/sh\n", 0o755) + self.write("Steam/%s/files/bin-arm64/proton" % folder, b"#!/bin/sh\n", 0o755) + found = RUN["tools"](str(self.root)) + wrapper = self.root / "compatibilitytools.d/droiddeck-proton-arm64" + self.assertEqual(found["droiddeck-proton-arm64"], (str(wrapper), "droiddeck-proton")) + self.assertNotIn(name, found) + + def test_an_incomplete_valve_depot_is_not_registered(self): + name = "Proton Experimental (ARM64)" + folder = "steamapps/common/%s" % name + shutil.rmtree(self.root / "compatibilitytools.d/droiddeck-proton-arm64") + self.write("Steam/steamapps/appmanifest_4427310.acf", '"AppState" { "appid" "4427310" }') + self.write("Steam/%s/toolmanifest.vdf" % folder, '"manifest"\n{\n "commandline" "/proton %verb%"\n}\n') + self.write("Steam/%s/files/bin-arm64/proton" % folder, b"#!/bin/sh\n", 0o755) + found = RUN["tools"](str(self.root)) + self.assertNotIn("droiddeck-proton-arm64", found) + self.assertFalse((self.root / "compatibilitytools.d/droiddeck-proton-arm64").exists()) + + def test_a_failed_wrapper_build_does_not_hide_existing_tools(self): + shutil.rmtree(self.root / "compatibilitytools.d/droiddeck-proton-arm64") + self.write("Steam/steamapps/appmanifest_4427310.acf", '"AppState" { "appid" "4427310" }') + folder = "steamapps/common/Proton Experimental (ARM64)" + self.write("Steam/%s/proton" % folder, b"#!/bin/sh\n", 0o755) + self.write("Steam/%s/files/bin-arm64/proton" % folder, b"#!/bin/sh\n", 0o755) + build = mock.Mock(side_effect=OSError("read-only")) + with mock.patch.dict(RUN["compat"](), {"build_tool": build}): + found = RUN["tools"](str(self.root)) + build.assert_called_once() + self.assertIn("droiddeck-proton-11-arm64", found) + + def test_a_runnable_depot_is_chosen_over_a_newer_incomplete_one(self): + shutil.rmtree(self.root / "compatibilitytools.d/droiddeck-proton-arm64") + self.write("Steam/steamapps/appmanifest_4427310.acf", '"AppState" { "appid" "4427310" }') + good = "Steam/steamapps/common/Proton Experimental (ARM64)" + self.write("%s/proton" % good, b"#!/bin/sh\n", 0o755) + self.write("%s/files/bin-arm64/proton" % good, b"#!/bin/sh\n", 0o755) + broken = "Library/steamapps/common/Proton Experimental (ARM64)" + self.write("Library/steamapps/libraryfolders.vdf", b"") + self.write("%s/version" % broken, b"2\n") + self.write("%s/files/bin-arm64/proton" % broken, b"#!/bin/sh\n", 0o755) + self.write("Steam/steamapps/libraryfolders.vdf", '"path" "%s"' % (self.tmp / "Library")) + compat = RUN["compat"]() + source = compat["find_source"] + self.assertEqual(source(str(self.root), runnable=True), str(self.tmp / good)) + + def test_a_registered_wrapper_without_its_launcher_is_rebuilt(self): + wrapper = self.root / "compatibilitytools.d/droiddeck-proton-arm64" + self.write("Steam/steamapps/appmanifest_4427310.acf", '"AppState" { "appid" "4427310" }') + folder = "Steam/steamapps/common/Proton Experimental (ARM64)" + self.write("%s/proton" % folder, b"#!/bin/sh\n", 0o755) + self.write("%s/files/bin-arm64/proton" % folder, b"#!/bin/sh\n", 0o755) + launcher = wrapper / RUN["compat"]()["LAUNCHER"] + if launcher.exists(): + launcher.unlink() + found = RUN["tools"](str(self.root)) + self.assertTrue(os.access(launcher, os.X_OK)) + self.assertIn("droiddeck-proton-arm64", found) + + def test_a_registered_wrapper_without_its_manifest_is_rebuilt(self): + wrapper = self.root / "compatibilitytools.d/droiddeck-proton-arm64" + self.write("Steam/steamapps/appmanifest_4427310.acf", '"AppState" { "appid" "4427310" }') + folder = "Steam/steamapps/common/Proton Experimental (ARM64)" + self.write("%s/proton" % folder, b"#!/bin/sh\n", 0o755) + self.write("%s/files/bin-arm64/proton" % folder, b"#!/bin/sh\n", 0o755) + manifest = wrapper / "toolmanifest.vdf" + if manifest.exists(): + manifest.unlink() + found = RUN["tools"](str(self.root)) + self.assertTrue(manifest.is_file()) + self.assertIn("droiddeck-proton-arm64", found) + def test_steams_choice_for_the_title_then_its_default(self): available = RUN["tools"](str(self.root)) current = RUN["mapping"](str(self.root)) @@ -428,6 +567,23 @@ class ProtonRunTest(Temp): self.assertEqual(RUN["choose_tool"]("70", {"0": "GE-Proton"}, available), ("droiddeck-proton-arm64", "the default ARM64 Proton")) self.assertEqual(RUN["choose_tool"]("70", {}, {}), (None, None)) + def test_a_removed_droiddeck_proton_build_is_replaced_by_the_newest_one(self): + for version in ("11-12.2", "11-12.3"): + folder = "compatibilitytools.d/DroidDeck-Proton %s" % version + self.write("Steam/%s/compatibilitytool.vdf" % folder, TOOL_VDF % ("DroidDeck-Proton-" + version, version, "windows")) + self.write("Steam/%s/toolmanifest.vdf" % folder, '"manifest"\n{\n "commandline" "/proton %verb%"\n}\n') + self.write("Steam/%s/proton" % folder, b"#!/bin/sh\n", 0o755) + available = RUN["tools"](str(self.root)) + newest = "DroidDeck-Proton-11-12.3" + self.assertEqual(RUN["choose_tool"]("70", {"0": "DroidDeck-Proton-11-12.1"}, available)[0], newest) + self.assertEqual(RUN["choose_tool"]("70", {"70": "DroidDeck-Proton-11-12.1", "0": "droiddeck-proton-arm64"}, available)[0], newest) + self.assertEqual(RUN["choose_tool"]("70", {"0": "DroidDeck-Proton-11-12.2"}, available), ("DroidDeck-Proton-11-12.2", "Steam's default")) + self.assertEqual(RUN["choose_tool"]("70", {"0": "GE-Proton"}, available), ("droiddeck-proton-arm64", "the default ARM64 Proton")) + (self.tmp / "state.json").write_text('{"default": "DroidDeck-Proton-11-12.1"}') + self.assertEqual(RUN["choose_tool"]("70", {}, available)[0], newest) + (self.tmp / "state.json").write_text('{"default": "droiddeck-proton-11-arm64"}') + self.assertEqual(RUN["choose_tool"]("70", {}, available), ("droiddeck-proton-11-arm64", "the default ARM64 Proton")) + def test_a_steam_games_program_uses_its_prefix(self): program = self.game("413150", "Stardew Valley", "Stardew Valley.exe") target = RUN["locate"](str(program), str(self.root)) diff --git a/tools/tests/test_dircache_preload.py b/tools/tests/test_dircache_preload.py index 50ad24d5f7..42db6817ce 100644 --- a/tools/tests/test_dircache_preload.py +++ b/tools/tests/test_dircache_preload.py @@ -3,6 +3,7 @@ from pathlib import Path import shutil import subprocess import tempfile +import threading import unittest PRELOAD = Path(__file__).resolve().parents[1] / "linuxfs/preload" @@ -10,6 +11,7 @@ PRELOAD = Path(__file__).resolve().parents[1] / "linuxfs/preload" FAKE = r""" #define _GNU_SOURCE #include +#include #include #include #include @@ -17,9 +19,11 @@ FAKE = r""" #include #include #include +#include #include int real_opendirs; +int real_accesses; static void coarse(const char *path, struct stat *st) { if (getenv("COARSE") && strstr(path, "/fuse/") && S_ISDIR(st->st_mode)) { @@ -32,6 +36,17 @@ static void coarse(const char *path, struct stat *st) { int stat(const char *path, struct stat *st) { static int (*next)(const char *, struct stat *); if (!next) next = dlsym(RTLD_NEXT, "stat"); + char folded[4096]; + const char *fold = getenv("FOLD"); + const char *upper = fold ? strstr(path, "/STEAMAPPS") : NULL; + if (!upper && fold && !strcmp(fold, "all")) upper = strcasestr(path, "/steamapps/"); + if (upper && strlen(path) < sizeof(folded)) { + strcpy(folded, path); + memcpy(folded + (upper - path) + 1, "steamapps", 9); + if (!strcmp(fold, "all")) + for (char *c = folded + (upper - path); *c; c++) if (*c >= 'A' && *c <= 'Z') *c += 'a' - 'A'; + path = folded; + } int r = next(path, st); if (r == 0) coarse(path, st); return r; @@ -61,6 +76,16 @@ int fstatfs(int fd, struct statfs *out) { return r; } +int inotify_add_watch(int fd, const char *path, uint32_t mask) { + static int (*next)(int, const char *, uint32_t); + if (!next) next = dlsym(RTLD_NEXT, "inotify_add_watch"); + if (getenv("NOFOLLOW_FAIL") && (mask & IN_DONT_FOLLOW)) { + errno = ENOSPC; + return -1; + } + return next(fd, path, mask); +} + int statfs(const char *path, struct statfs *out) { static int (*next)(const char *, struct statfs *); if (!next) next = dlsym(RTLD_NEXT, "statfs"); @@ -82,6 +107,13 @@ DIR *opendir(const char *path) { return next(path); } +int faccessat(int dirfd, const char *path, int mode, int flags) { + static int (*next)(int, const char *, int, int); + if (!next) next = dlsym(RTLD_NEXT, "faccessat"); + if (strstr(path, "/fuse/")) real_accesses++; + return next(dirfd, path, mode, flags); +} + DIR *fdopendir(int fd) { static DIR *(*next)(int); if (!next) next = dlsym(RTLD_NEXT, "fdopendir"); @@ -94,6 +126,7 @@ PROGRAM = r""" #define _GNU_SOURCE #include #include +#include #include #include #include @@ -171,6 +204,7 @@ static void stress(const char *dir) { int main(int argc, char **argv) { int *opens = dlsym(RTLD_DEFAULT, "real_opendirs"); + int *accesses = dlsym(RTLD_DEFAULT, "real_accesses"); for (int i = 1; i < argc; i++) { const char *op = argv[i], *a = argv[i + 1]; if (!strcmp(op, "list")) { list(a); i++; } @@ -178,6 +212,14 @@ int main(int argc, char **argv) { else if (!strcmp(op, "create")) { close(open(a, O_WRONLY | O_CREAT, 0644)); i++; } else if (!strcmp(op, "sneak")) { close((int)syscall(SYS_openat, AT_FDCWD, a, O_WRONLY | O_CREAT, 0644)); i++; } else if (!strcmp(op, "sneak-unlink")) { syscall(SYS_unlinkat, AT_FDCWD, a, 0); i++; } + else if (!strcmp(op, "sneak-rename")) { syscall(SYS_renameat, AT_FDCWD, a, AT_FDCWD, argv[i + 2]); i += 2; } + else if (!strcmp(op, "sneak-mkdir")) { syscall(SYS_mkdirat, AT_FDCWD, a, 0755); i++; } + else if (!strcmp(op, "pause")) { + char line[16]; + printf("ready\n"); + fflush(stdout); + if (!fgets(line, sizeof(line), stdin)) return 1; + } else if (!strcmp(op, "unlink")) { unlink(a); i++; } else if (!strcmp(op, "remove")) { remove(a); i++; } else if (!strcmp(op, "mkdir")) { mkdir(a, 0755); i++; } @@ -246,6 +288,12 @@ int main(int argc, char **argv) { waitpid(child, NULL, 0); i += 2; } + else if (!strcmp(op, "access")) { + int r = access(a, F_OK); + printf("%d %d\n", r, r ? errno : 0); + i++; + } + else if (!strcmp(op, "accesses")) printf("%d\n", accesses ? *accesses : -1); else if (!strcmp(op, "opens")) printf("%d\n", opens ? *opens : -1); } return 0; @@ -284,13 +332,39 @@ class DirCachePreloadTest(unittest.TestCase): for name in ("a.bundle", "b.bundle"): (self.staging / name).touch() - def run_program(self, *args, program=None, env=None): + def environment(self, env): environment = dict(os.environ, LD_PRELOAD="%s:%s" % (self.cache, self.fake)) environment.update(env or {}) - result = subprocess.run([str(program or self.steam), *map(str, args)], env=environment, + return environment + + def run_program(self, *args, program=None, env=None): + result = subprocess.run([str(program or self.steam), *map(str, args)], env=self.environment(env), capture_output=True, text=True, check=True, timeout=60) return result.stdout.splitlines() + def run_paused(self, *args, between, env=None): + probe = subprocess.Popen([str(self.steam), *map(str, args)], env=self.environment(env), text=True, + stdin=subprocess.PIPE, stdout=subprocess.PIPE) + timer = threading.Timer(60, probe.kill) + timer.start() + try: + before = [] + for line in probe.stdout: + if line == "ready\n": + break + before.append(line.rstrip("\n")) + else: + self.fail("the probe stopped before it was ready") + between() + after, _ = probe.communicate("go\n", timeout=60) + finally: + timer.cancel() + if probe.poll() is None: + probe.kill() + probe.communicate() + self.assertEqual(0, probe.returncode) + return before + after.splitlines() + def test_repeated_listings_read_the_directory_once(self): out = self.run_program("list", self.staging, "list", self.staging, "list", self.staging, "opens") self.assertEqual(["a.bundle,b.bundle"] * 3 + ["1"], out) @@ -399,6 +473,208 @@ class DirCachePreloadTest(unittest.TestCase): outside.mkdir() self.assertEqual("2", self.run_program("list", outside, "list", outside, "opens")[-1]) + def test_missing_names_are_answered_from_the_listing(self): + s = self.staging + out = self.run_program("list", s, "access", s / "x.bundle", "access", s / "y/z.bundle", + "access", s / "c.bundle", "accesses", "access", s / "a.bundle", "accesses", "opens") + self.assertEqual(["-1 2", "-1 2", "-1 2", "1", "0 0", "2", "1"], out[1:]) + + def test_a_name_that_differs_only_in_case_is_asked_of_the_filesystem(self): + s = self.staging + out = self.run_program("list", s, "access", s / "A.BUNDLE", "accesses") + self.assertEqual(["-1 2", "1"], out[1:]) + + def test_misses_before_the_client_lists_a_directory_reach_the_filesystem(self): + s = self.staging + out = self.run_program("access", s / "x.bundle", "access", s / "y.bundle", "accesses", "opens") + self.assertEqual(["-1 2", "-1 2", "2", "0"], out) + + def test_names_created_after_a_miss_are_found(self): + s = self.staging + out = self.run_program("list", s, "access", s / "x.bundle", "create", s / "x.bundle", "access", s / "x.bundle", + "access", s / "y.bundle", "sneak", s / "y.bundle", "access", s / "y.bundle", + "rename", s / "y.bundle", s / "w.bundle", "access", s / "w.bundle", + "mkdir", s / "sub", "access", s / "sub", + env={"COARSE": "1"}) + self.assertEqual(["-1 2", "0 0", "-1 2", "0 0", "0 0", "0 0"], out[1:]) + + def test_a_differently_cased_directory_reaches_the_filesystem_when_case_matters(self): + s = self.staging + upper = Path(str(s).replace("downloading", "DOWNLOADING")) + out = self.run_program("list", s, "access", upper / "x.bundle", "accesses") + self.assertEqual(["-1 2", "1"], out[1:]) + + def test_a_differently_cased_directory_uses_the_listing_when_the_filesystem_folds_case(self): + s = self.staging + upper = Path(str(s).replace("downloading", "DOWNLOADING")) + out = self.run_program("list", s, "access", upper / "x.bundle", "access", upper / "A.BUNDLE", "accesses", + env={"FOLD": "all"}) + self.assertEqual(["-1 2", "-1 2", "1"], out[1:]) + + def test_a_case_sensitive_folder_below_a_folding_steamapps_reaches_the_filesystem(self): + s = self.staging + upper = self.lib / "DOWNLOADING" / s.name + upper.mkdir(parents=True) + (upper / "x.bundle").write_bytes(b"") + out = self.run_program("list", s, "access", upper / "x.bundle", "access", upper / "y.bundle", "accesses", + env={"FOLD": "1"}) + self.assertEqual(["0 0", "-1 2", "2"], out[1:]) + + def test_a_differently_cased_folder_above_steamapps_reaches_the_filesystem(self): + s = self.staging + top = self.lib.parent.parent + other = top.parent / top.name.swapcase() / s.relative_to(top) + self.assertNotEqual(top.name, top.name.swapcase()) + other.mkdir(parents=True) + (other / "x.bundle").write_bytes(b"") + out = self.run_program("list", s, "access", other / "x.bundle", "accesses", env={"FOLD": "all"}) + self.assertEqual(["0 0", "1"], out[1:]) + + def test_a_differently_cased_sibling_of_a_folded_listing_reaches_the_filesystem(self): + s = self.staging + (self.lib.parent / "STEAMAPPS").symlink_to("steamapps") + (self.lib / "DOWNLOADING").symlink_to("downloading") + other = self.lib / "Downloading" / s.name + other.mkdir(parents=True) + (other / "x.bundle").touch() + out = self.run_program("list", s, "access", other / "x.bundle", "accesses") + self.assertEqual("0 0", out[1]) + + def test_a_listing_is_dropped_when_a_folder_above_it_is_renamed(self): + s = self.staging + parent = s.parent + moved = parent.parent / "downloading.old" + out = self.run_program("list", s, "rename", parent, moved, "mkdir", parent, "mkdir", s, + "sneak", s / "x.bundle", "access", s / "x.bundle", "accesses") + self.assertEqual(["0 0", "1"], out[1:]) + + def test_a_listing_is_dropped_when_another_process_renames_a_folder_above_it(self): + s = self.staging + parent = s.parent + + def swap(): + parent.rename(parent.parent / "downloading.old") + s.mkdir(parents=True) + (s / "x.bundle").touch() + + out = self.run_paused("list", s, "pause", "access", s / "x.bundle", "accesses", between=swap) + self.assertEqual(["0 0", "1"], out[1:]) + + def test_a_listing_is_dropped_when_another_process_renames_the_library_above_steamapps(self): + s = self.staging + root = self.lib.parent + + def swap(): + root.rename(root.parent / "fuse.old") + s.mkdir(parents=True) + (s / "x.bundle").touch() + + out = self.run_paused("list", s, "pause", "access", s / "x.bundle", "accesses", between=swap) + self.assertEqual(["0 0", "1"], out[1:]) + + def test_a_repointed_symlink_above_a_listing_does_not_hide_files(self): + s = self.staging + real = self.lib / "dl-a" + s.parent.rename(real) + s.parent.symlink_to(real.name) + other = self.lib / "dl-b" / s.name + other.mkdir(parents=True) + (other / "x.bundle").touch() + + def repoint(): + link = self.lib / "downloading.new" + link.symlink_to("dl-b") + link.replace(s.parent) + + out = self.run_paused("list", s, "pause", "access", s / "x.bundle", between=repoint) + self.assertEqual("0 0", out[1]) + + def test_a_symlink_check_that_cannot_be_watched_does_not_hide_files(self): + s = self.staging + real = self.lib / "dl-a" + s.parent.rename(real) + s.parent.symlink_to(real.name) + other = self.lib / "dl-b" / s.name + other.mkdir(parents=True) + (other / "x.bundle").touch() + + def repoint(): + link = self.lib / "downloading.new" + link.symlink_to("dl-b") + link.replace(s.parent) + + out = self.run_paused("list", s, "pause", "access", s / "x.bundle", between=repoint, env={"NOFOLLOW_FAIL": "1"}) + self.assertEqual("0 0", out[1]) + + def test_an_unreadable_folder_above_steamapps_does_not_hide_files(self): + s = self.staging + root = self.lib.parent + moved = root.parent / "fuse.old" + root.chmod(0o311) + self.addCleanup(lambda: moved.chmod(0o755) if moved.exists() else root.chmod(0o755)) + if os.access(root, os.R_OK): + self.skipTest("folder permissions are not enforced here") + + def swap(): + root.rename(moved) + s.mkdir(parents=True) + (s / "x.bundle").touch() + + out = self.run_paused("list", s, "pause", "access", s / "x.bundle", between=swap) + self.assertEqual("0 0", out[1]) + + def test_a_listing_is_dropped_when_another_process_removes_a_folder_above_it(self): + deep = self.staging / "deep" + deep.mkdir() + parent = self.staging.parent + + def swap(): + self.staging.rename(self.lib / "moved") + parent.rmdir() + deep.mkdir(parents=True) + (deep / "x.bundle").touch() + + out = self.run_paused("list", deep, "pause", "access", deep / "x.bundle", "accesses", between=swap) + self.assertEqual(["", "0 0", "1"], out) + + def test_a_listing_is_dropped_when_a_folder_above_it_is_renamed_around_the_cache(self): + s = self.staging + parent = s.parent + out = self.run_program("list", s, "sneak-rename", parent, parent.parent / "downloading.old", + "sneak-mkdir", parent, "sneak-mkdir", s, "sneak", s / "x.bundle", + "access", s / "x.bundle", "accesses") + self.assertEqual(["0 0", "1"], out[1:]) + + def test_changes_in_a_cached_folder_above_keep_the_listing_below(self): + s = self.staging + parent = s.parent + + def churn(): + (parent / "z").touch() + (parent / "z").unlink() + (parent / "sibling").mkdir() + (parent / "sibling").rmdir() + + out = self.run_paused("list", parent, "list", s, "pause", "list", parent, "list", s, + "access", s / "x.bundle", "accesses", "opens", between=churn) + self.assertEqual([s.name, "a.bundle,b.bundle", s.name, "a.bundle,b.bundle", "-1 2", "0", "3"], out) + + def test_misses_outside_the_client_or_fuse_steamapps_reach_the_filesystem(self): + s = self.staging + other = self.run_program("list", s, "access", s / "x.bundle", "access", s / "y.bundle", "accesses", + program=self.other) + self.assertEqual(["-1 2", "-1 2", "2"], other[1:]) + outside = self.lib.parent / "elsewhere" + outside.mkdir() + out = self.run_program("list", outside, "access", outside / "x", "access", outside / "y", "accesses") + self.assertEqual(["-1 2", "-1 2", "2"], out[1:]) + + def test_misses_reach_the_filesystem_when_turned_off(self): + s = self.staging + out = self.run_program("list", s, "access", s / "x.bundle", "access", s / "y.bundle", "accesses", + env={"BL_NO_DIR_CACHE": "1"}) + self.assertEqual(["-1 2", "-1 2", "2"], out[1:]) + def test_can_be_turned_off(self): out = self.run_program("list", self.staging, "list", self.staging, "opens", env={"BL_NO_DIR_CACHE": "1"}) self.assertEqual("2", out[-1]) diff --git a/tools/tests/test_droiddeck_agent.py b/tools/tests/test_droiddeck_agent.py index 02e53f9607..a1adca90c6 100644 --- a/tools/tests/test_droiddeck_agent.py +++ b/tools/tests/test_droiddeck_agent.py @@ -457,6 +457,26 @@ class AgentTest(unittest.TestCase): self.assertEqual(1, agent.main(["--steam-shortcut-state", game_id])) self.assertEqual(2, agent.main(["--steam-shortcut-state"])) + def test_steam_ready_uses_steams_initialized_store(self): + with patch.object(agent, "cdp_eval", return_value={"value": True}) as evaluate: + self.assertTrue(agent.steam_ready(self.dir)) + self.assertIn("m_bIsInitialized", evaluate.call_args.args[1]) + with patch.object(agent, "cdp_eval", return_value={"value": False}): + self.assertFalse(agent.steam_ready(self.dir)) + + def test_pending_interstitial_is_continued_by_steam(self): + with patch.object(agent, "cdp_eval", return_value={"value": True}) as evaluate: + self.assertTrue(agent.steam_clear_pending_interstitial(self.dir, "3551182975")) + expression = evaluate.call_args.args[1] + self.assertIn("ContinueGameAction", expression) + self.assertIn("ShowInterstitials", expression) + self.assertIn('"3551182975"', expression) + self.assertIn("await apps.GetActiveGameActions()", expression) + with patch.object(agent, "cdp_eval", return_value={"value": False}): + self.assertFalse(agent.steam_clear_pending_interstitial(self.dir, "3551182975")) + with self.assertRaises(agent.AgentError): + agent.steam_clear_pending_interstitial(self.dir, "0") + def test_asleep_command_reports_steams_own_sleep_state(self): with patch.dict(os.environ, {"BL_LAUNCH_DIR": str(self.dir.parent)}): for value, code in (("yes", 0), ("no", 5), ("unknown", 1), (None, 1)): diff --git a/tools/tests/test_droiddeck_proton_default.py b/tools/tests/test_droiddeck_proton_default.py index 6c0665d571..2173a223a1 100644 --- a/tools/tests/test_droiddeck_proton_default.py +++ b/tools/tests/test_droiddeck_proton_default.py @@ -3,14 +3,18 @@ import runpy import tempfile import unittest from pathlib import Path +from unittest import mock BIN = Path(__file__).resolve().parents[1] / "linuxfs/overlay/usr/local/bin" COMPAT = runpy.run_path(str(BIN / "steam-compatibility")) TOOL = COMPAT["TOOL"] OLD = {"name": "DroidDeck-Proton-11-11.2", "display": "DroidDeck-Proton 11-11.2", "dir": "DroidDeck-Proton 11-11.2", "valve": False} NEW = {"name": "DroidDeck-Proton-11-11.4", "display": "DroidDeck-Proton 11-11.4", "dir": "DroidDeck-Proton 11-11.4", "valve": False} -VALVE = {"name": TOOL, "display": "Proton Experimental ARM64", "dir": "Proton - Experimental (ARM64)", "valve": True} +VALVE = {"name": TOOL, "display": "Proton Experimental ARM64", "dir": COMPAT["SOURCES"][0], "valve": True} +VALVE_11 = {"name": COMPAT["TOOL_11"], "display": "Proton 11.0 ARM64", "dir": COMPAT["SOURCES"][1], "valve": True} GE = {"name": "GE-Proton11-7", "display": "GE-Proton11-7", "dir": "GE-Proton11-7", "valve": False} +GE10 = {"name": "GE-Proton10-3", "display": "GE-Proton10-3", "dir": "GE-Proton10-3", "valve": False} +NEWER = {"name": "DroidDeck-Proton-11-12.1", "display": "DroidDeck-Proton 11-12.1", "dir": "DroidDeck-Proton 11-12.1", "valve": False} def config_text(entries): @@ -19,15 +23,19 @@ def config_text(entries): class DroidDeckProtonDefaultTest(unittest.TestCase): - def run_default(self, state, mapping, catalog, apps=(), request=None, settle=True): + def run_default(self, state, mapping, catalog, apps=(), request=None, settle=True, owned=None, games=None): with tempfile.TemporaryDirectory() as tmp: directory = Path(tmp) (directory / "state.json").write_text(json.dumps(state)) if request is not None: (directory / "request.json").write_text(json.dumps(request)) + if games is not None: + (directory / "games.json").write_text(json.dumps({"version": 1, "games": games})) config = directory / "config.vdf" config.write_text(config_text(mapping)) - default = COMPAT["register_default"](str(config), list(apps), catalog=catalog, directory=str(directory), settle=settle) + with mock.patch.dict(COMPAT["register_default"].__globals__, steam_running=lambda: False): + default = COMPAT["register_default"](str(config), list(apps), catalog=catalog, directory=str(directory), + settle=settle, owned=owned) return default, COMPAT["read_config_mapping"](str(config)), json.loads((directory / "state.json").read_text()) def test_a_replacement_seen_while_steam_runs_still_moves_the_default_later(self): @@ -50,8 +58,7 @@ class DroidDeckProtonDefaultTest(unittest.TestCase): {"default": TOOL, "auto": {"42": TOOL}}, [("0", TOOL), ("42", TOOL), ("43", TOOL)], [VALVE, NEW], ["42", "43"]) self.assertEqual(default, NEW["name"]) self.assertEqual(mapping, {"0": NEW["name"], "42": NEW["name"], "43": TOOL}) - self.assertEqual(state["default"], NEW["name"]) - self.assertTrue(state["own_proton"]) + self.assertEqual((state["default"], state["source"], state["written"]), (NEW["name"], "update", NEW["name"])) def test_a_default_picked_after_the_switch_is_kept(self): default, mapping, state = self.run_default( @@ -59,8 +66,7 @@ class DroidDeckProtonDefaultTest(unittest.TestCase): [("0", NEW["name"])], [VALVE, NEW], request={"seq": 2, "valve": True, "dir": VALVE["dir"]}) self.assertEqual(default, TOOL) self.assertEqual(mapping, {"0": TOOL}) - default, mapping, state = self.run_default( - {"default": TOOL, "own_proton": True, "tools": [VALVE, NEW]}, [("0", TOOL)], [VALVE, NEW]) + default, mapping, state = self.run_default(state, [("0", TOOL)], [VALVE, NEW]) self.assertEqual(default, TOOL) self.assertEqual(mapping, {"0": TOOL}) @@ -75,7 +81,7 @@ class DroidDeckProtonDefaultTest(unittest.TestCase): def test_a_replaced_build_does_not_override_a_valve_default(self): default, mapping, _ = self.run_default( - {"default": TOOL, "own_proton": True, "tools": [VALVE, OLD]}, [("0", TOOL), ("43", OLD["name"])], [VALVE, NEW], ["43"]) + {"default": TOOL, "source": "app", "written": TOOL, "tools": [VALVE, OLD]}, [("0", TOOL), ("43", OLD["name"])], [VALVE, NEW], ["43"]) self.assertEqual(default, TOOL) self.assertEqual(mapping, {"0": TOOL, "43": NEW["name"]}) @@ -85,6 +91,114 @@ class DroidDeckProtonDefaultTest(unittest.TestCase): self.assertEqual(mapping, {"0": TOOL}) self.assertNotIn("own_proton", state) + def test_a_fresh_state_with_steam_on_proton_experimental_moves_to_the_newest_build(self): + default, mapping, state = self.run_default({}, [("0", TOOL)], [VALVE, OLD, NEW]) + self.assertEqual((default, mapping), (NEW["name"], {"0": NEW["name"]})) + default, mapping, state = self.run_default(state, [("0", NEW["name"])], [VALVE, OLD, NEW]) + self.assertEqual((default, state["source"]), (NEW["name"], "update")) + + def test_a_default_adopted_from_steam_by_an_earlier_build_recovers(self): + default, mapping, state = self.run_default( + {"default": TOOL, "source": "steam", "seq": 5, "own_proton": True, "tools": [VALVE, NEW]}, [("0", TOOL)], [VALVE, NEW]) + self.assertEqual((default, mapping, state["source"]), (NEW["name"], {"0": NEW["name"]}, "update")) + self.assertNotIn("own_proton", state) + + def test_a_request_applied_without_a_change_still_moves_to_the_newest_build(self): + for request in ({"seq": 5, "valve": True, "dir": VALVE["dir"]}, {"seq": 5, "valve": False, "dir": GE["dir"]}): + default, mapping, state = self.run_default({"default": TOOL, "applied": 1}, [("0", TOOL)], [VALVE, NEW], request=request) + self.assertEqual((default, mapping, state["applied"]), (NEW["name"], {"0": NEW["name"]}, 5)) + + def test_a_default_that_is_not_installed_falls_back_without_counting_as_a_pick(self): + default, mapping, state = self.run_default( + {"default": GE["name"], "source": "app", "seq": 9, "written": GE["name"]}, [("0", GE["name"])], [VALVE]) + self.assertEqual((default, mapping), (TOOL, {"0": TOOL})) + self.assertEqual((state["source"], state["seq"]), ("fallback", 9)) + default, _, state = self.run_default(state, [("0", TOOL)], [VALVE, NEW]) + self.assertEqual((default, state["source"]), (NEW["name"], "update")) + + def test_proton_experimental_asked_for_in_the_app_stays(self): + default, mapping, state = self.run_default( + {"default": NEW["name"], "written": NEW["name"], "applied": 1}, [("0", NEW["name"])], [VALVE, NEW], + request={"seq": 2, "valve": True, "dir": VALVE["dir"]}) + self.assertEqual((default, state["source"]), (TOOL, "app")) + for catalog in ([VALVE, NEW], [VALVE, NEW, NEWER], [VALVE, NEWER]): + default, mapping, state = self.run_default(state, [("0", TOOL)], catalog) + self.assertEqual((default, mapping, state["source"]), (TOOL, {"0": TOOL}, "app")) + + def test_proton_experimental_picked_in_steam_stays(self): + default, mapping, state = self.run_default( + {"default": NEW["name"], "written": NEW["name"], "source": "update"}, [("0", TOOL)], [VALVE, NEW]) + self.assertEqual((default, state["source"], state["written"]), (TOOL, "steam", TOOL)) + for catalog in ([VALVE, NEW], [VALVE, NEW, NEWER]): + default, mapping, state = self.run_default(state, [("0", TOOL)], catalog) + self.assertEqual((default, mapping, state["source"]), (TOOL, {"0": TOOL}, "steam")) + + def test_steam_echoing_the_written_default_is_not_a_pick(self): + default, _, state = self.run_default( + {"default": NEW["name"], "written": TOOL, "source": "update"}, [("0", TOOL)], [VALVE, NEW]) + self.assertEqual((default, state["source"]), (NEW["name"], "update")) + + def test_a_default_the_running_client_does_not_list_is_kept(self): + state = {"default": NEW["name"], "written": NEW["name"], "seq": 1, "source": "update"} + default, state = COMPAT["choose_default"](TOOL, state, {}, [VALVE, NEW], listed=[TOOL]) + self.assertEqual((default, state["default"], state["source"], state["seq"]), (TOOL, NEW["name"], "update", 1)) + default, state = COMPAT["choose_default"](GE["name"], dict(state, written=TOOL), {}, [VALVE, NEW, GE]) + self.assertEqual((default, state["source"]), (GE["name"], "steam")) + + def test_owned_shortcuts_follow_the_default_and_explicit_picks_stay(self): + owned = ["3000000001", "3000000002", "3000000003", "3000000004"] + catalog = [VALVE, NEW, GE, GE10] + default, mapping, state = self.run_default( + {"default": NEW["name"], "written": NEW["name"], "source": "update", "auto": {}}, + [("0", NEW["name"]), ("3000000001", NEW["name"]), ("3000000002", TOOL), ("3000000003", GE["name"]), + ("3000000004", NEW["name"]), ("42", GE["name"]), ("44", NEW["name"])], + catalog, ["42", "44"] + owned, settle=False, owned=owned, games={"44": {"valve": False, "dir": GE["dir"]}}) + self.assertEqual(mapping, {"0": NEW["name"], "3000000001": NEW["name"], "3000000002": NEW["name"], + "3000000003": GE["name"], "3000000004": NEW["name"], "42": GE["name"], "44": GE["name"]}) + mapping["3000000001"] = TOOL + default, mapping, state = self.run_default(state, sorted(mapping.items()), catalog, ["42", "44"] + owned, + settle=False, owned=owned) + self.assertEqual(mapping["3000000001"], TOOL) + default, mapping, state = self.run_default(state, sorted(mapping.items()), catalog, ["42", "44"] + owned, + request={"seq": 1, "valve": False, "dir": GE10["dir"]}) + self.assertEqual(default, GE10["name"]) + self.assertEqual(mapping, {"0": GE10["name"], "3000000001": TOOL, "3000000002": GE10["name"], + "3000000003": GE["name"], "3000000004": GE10["name"], "42": GE["name"], "44": GE["name"]}) + + def test_proton_experimental_picked_for_a_game_in_steam_stays(self): + default, mapping, state = self.run_default( + {"default": NEW["name"], "written": NEW["name"], "source": "update", + "auto": {"42": NEW["name"], "43": NEW["name"], "45": NEW["name"]}}, + [("0", NEW["name"]), ("42", "proton_experimental"), ("43", NEW["name"]), ("45", TOOL)], [VALVE, NEW], + ["42", "43", "44", "45"]) + self.assertEqual(default, NEW["name"]) + self.assertEqual(mapping, {"0": NEW["name"], "42": TOOL, "43": NEW["name"], "44": NEW["name"], "45": TOOL}) + self.assertEqual(state["auto"], {"43": NEW["name"], "44": NEW["name"]}) + self.assertTrue({"42", "45"} <= set(state["picked"])) + default, mapping, state = self.run_default(state, sorted(mapping.items()), [VALVE, NEWER], ["42", "43", "44", "45"]) + self.assertEqual(default, NEWER["name"]) + self.assertEqual(mapping, {"0": NEWER["name"], "42": TOOL, "43": NEWER["name"], "44": NEWER["name"], "45": TOOL}) + + def test_a_steam_pick_of_a_valve_proton_moves_to_the_build_made_from_it(self): + catalog = [VALVE, VALVE_11, NEW] + default, mapping, state = self.run_default( + {"default": NEW["name"], "written": NEW["name"], "source": "update", "auto": {"42": TOOL, "43": NEW["name"]}, + "picked": ["44"]}, + [("0", NEW["name"]), ("42", "proton-experimental-arm64"), ("43", "proton_11"), ("44", "proton_11-arm64"), + ("45", "proton_9")], catalog, ["42", "43", "44", "45"]) + self.assertEqual(mapping, {"0": NEW["name"], "42": TOOL, "43": VALVE_11["name"], "44": VALVE_11["name"], "45": NEW["name"]}) + self.assertEqual(state["auto"], {"45": NEW["name"]}) + _, mapping, _ = self.run_default(state, sorted(mapping.items()), catalog, ["42", "43", "44", "45"], + request={"seq": 1, "valve": False, "dir": GE["dir"]}) + self.assertEqual(mapping, {"0": NEW["name"], "42": TOOL, "43": VALVE_11["name"], "44": VALVE_11["name"], "45": NEW["name"]}) + + def test_proton_experimental_picked_as_steams_default_stays(self): + default, mapping, state = self.run_default( + {"default": NEW["name"], "written": NEW["name"], "source": "update"}, [("0", "proton_experimental")], [VALVE, NEW]) + self.assertEqual((default, mapping, state["source"]), (TOOL, {"0": TOOL}, "steam")) + default, mapping, state = self.run_default(state, [("0", TOOL)], [VALVE, NEW, NEWER]) + self.assertEqual((default, mapping), (TOOL, {"0": TOOL})) + def test_the_newest_build_is_found_by_version(self): newest, gone = COMPAT["own_proton_moves"]({"tools": [OLD]}, [dict(NEW, name="a", dir="DroidDeck-Proton 11-9.10"), dict(NEW, name="b", dir="DroidDeck-Proton 11-11.1")]) self.assertEqual(newest, "b") diff --git a/tools/tests/test_droiddeckctl.py b/tools/tests/test_droiddeckctl.py index 731e3aa38c..ab929de931 100644 --- a/tools/tests/test_droiddeckctl.py +++ b/tools/tests/test_droiddeckctl.py @@ -213,6 +213,18 @@ class DroiddeckctlTest(FakeAdbTest): started = [e["args"] for e in self.calls() if e["args"][:3] == ["shell", "am", "start"]] self.assertIn("com.antutu.benchmark.full/com.droiddeck.launcher.agent.AgentStartActivity", started[0]) + def test_a_start_delivered_to_an_existing_task_is_a_failure(self): + output = (b"Starting: Intent { cmp=com.droiddeck.launcher/.agent.AgentStartActivity (has extras) }\n" + b"Warning: Activity not started, intent has been delivered to currently running top-most instance.\n" + b"Status: ok\nActivity: com.droiddeck.launcher/.FrontEndActivity\nComplete\n") + + def adb(_adb, _serial, *args, timeout=30): + return ctl.subprocess.CompletedProcess(args, 0, output, b"") + + with unittest.mock.patch.object(ctl, "run_adb", adb), self.assertRaises(ctl.ControlError) as caught: + ctl.start_agent_activity("adb", "fake-1", {"mode": "steam"}) + self.assertEqual("ACTIVITY_START_FAILED", caught.exception.code) + def test_a_malformed_package_is_refused_before_adb(self): with unittest.mock.patch.dict(os.environ, {"DROIDDECK_PACKAGE": "com.example;reboot"}): variant = load("droiddeckctl") @@ -328,6 +340,15 @@ class DroiddeckctlTest(FakeAdbTest): self.assertEqual("222", out["displays"][1]["physicalId"]) self.assertEqual([1080, 1240], out["displays"][1]["size"]) + def test_displays_named_with_their_unique_id(self): + text = DUMPSYS_DISPLAY.replace("mPrimaryDisplayDevice=Screen-2", + "mPrimaryDisplayDevice=Screen-2(local:222)") + self.configure({}, dumpsys_display=text) + code, out = self.run_ctl("screenshot", str(self.dir / "shot.png"), "--display", "4") + self.assertEqual(0, code) + shot = [c for c in self.calls() if c["args"][:2] == ["exec-out", "screencap"]][0] + self.assertEqual(["exec-out", "screencap", "-p", "-d", "222"], shot["args"]) + def test_unknown_display_is_an_error(self): self.configure({}, dumpsys_display=DUMPSYS_DISPLAY) code, out = self.run_ctl("screenshot", str(self.dir / "x.png"), "--display", "9") @@ -344,6 +365,11 @@ class DroiddeckctlTest(FakeAdbTest): self.assertEqual(248, summary["offCadence"]) self.assertEqual(13.8, summary["offCadencePercent"]) + def test_pacing_reads_a_dump_with_windows_line_ends(self): + summary = ctl.pacing_summary(TIMESTATS_SPLIT.replace("\n", "\r\n"), "com.droiddeck.launcher") + self.assertEqual(ctl.pacing_summary(TIMESTATS_SPLIT, "com.droiddeck.launcher"), summary) + self.assertEqual(1800, summary["frames"]) + def test_pacing_falls_back_to_the_sessions_surfaceview(self): summary = ctl.pacing_summary(TIMESTATS_SURFACEVIEW, "com.droiddeck.launcher") self.assertEqual(1, len(summary["layers"])) @@ -393,6 +419,30 @@ class DroiddeckctlTest(FakeAdbTest): self.assertEqual(4, code) self.assertEqual("NO_FRAMES", out["error"]["code"]) + def test_provider_call_falls_back_to_a_broadcast_without_content(self): + calls = [] + answers = iter([b"", b'{"ok": true, "command": "state"}']) + + def adb(_adb, _serial, *args, timeout=30): + calls.append(args) + if args[:2] == ("shell", "content"): + return ctl.subprocess.CompletedProcess(args, 127, b"", b"/system/bin/sh: content: inaccessible or not found") + if args[:2] == ("shell", "am"): + return ctl.subprocess.CompletedProcess(args, 0, b"Broadcast completed: result=0", b"") + if args[:2] == ("shell", "pidof " + ctl.PACKAGE): + return ctl.subprocess.CompletedProcess(args, 0, b"4940\n", b"") + return ctl.subprocess.CompletedProcess(args, 0, next(answers), b"") + + with unittest.mock.patch.object(ctl, "run_adb", adb), unittest.mock.patch.object(ctl.time, "sleep"): + answer = ctl.provider_call("adb", "fake-1", "state", arg="a b", request={"x": 1}) + self.assertEqual({"ok": True, "command": "state"}, answer) + broadcast = next(c for c in calls if c[:2] == ("shell", "am")) + self.assertEqual(("shell", "am", "broadcast", "-a", ctl.PACKAGE + ".AGENT_CALL", "-p", ctl.PACKAGE), broadcast[:7]) + self.assertIn("'a b'", broadcast) + self.assertEqual(ctl.encode_request({"x": 1}), broadcast[broadcast.index("request") + 1]) + call_id = broadcast[broadcast.index("id") + 1] + self.assertIn("agent-calls/%s.json" % call_id, calls[-1][1]) + def test_pacing_turns_timestats_off_when_the_dump_fails(self): calls = [] @@ -787,5 +837,48 @@ class ScenarioTest(FakeAdbTest): self.assertEqual([{"appId": 7, "timeout": 15.0}] * 2, [c["request"] for c in self.calls("quit")]) +class ProviderFallbackTest(unittest.TestCase): + MISSING = "/system/bin/sh: content: inaccessible or not found\n" + + def adb(self, answers): + calls = [] + + def run(adb, serial, *args, timeout=None): + calls.append(args) + for prefix, (code, out, err) in answers: + if " ".join(args).startswith(" ".join(prefix)): + return unittest.mock.Mock(returncode=code, stdout=out.encode(), stderr=err.encode()) + raise AssertionError(f"unexpected adb call {args}") + return calls, unittest.mock.patch.object(ctl, "run_adb", run) + + def test_a_command_that_prints_the_shell_error_is_not_run_again(self): + result = 'Result: Bundle[{json={"ok":true,"stdout":{"text":"' + self.MISSING.strip() + '"}}}]' + calls, patch = self.adb([(["shell", "content"], (0, result, ""))]) + with patch: + self.assertTrue(ctl.provider_call("adb", "s", "native", request={"argv": ["x"]})["ok"]) + self.assertEqual(1, len(calls)) + + def test_a_shell_without_content_uses_the_broadcast(self): + calls, patch = self.adb([ + (["shell", "content"], (127, "", self.MISSING)), + (["shell", "pidof"], (0, "4940\n", "")), + (["shell", "am", "broadcast"], (0, "Broadcast completed", "")), + (["shell"], (0, '{"ok": true}', "")), + ]) + with patch: + self.assertTrue(ctl.provider_call("adb", "s", "state")["ok"]) + self.assertEqual(["shell", "am", "broadcast"], list(calls[2][:3])) + + def test_the_broadcast_needs_the_app_running(self): + calls, patch = self.adb([ + (["shell", "content"], (127, "", self.MISSING)), + (["shell", "pidof"], (1, "", "")), + ]) + with patch, self.assertRaises(ctl.ControlError) as raised: + ctl.provider_call("adb", "s", "state") + self.assertEqual("APP_NOT_RUNNING", raised.exception.code) + self.assertFalse(any(c[:3] == ("shell", "am", "broadcast") for c in calls)) + + if __name__ == "__main__": unittest.main() diff --git a/tools/tests/test_fakeinput_open.py b/tools/tests/test_fakeinput_open.py new file mode 100644 index 0000000000..f4853e30e7 --- /dev/null +++ b/tools/tests/test_fakeinput_open.py @@ -0,0 +1,103 @@ +import os +from pathlib import Path +import shutil +import subprocess +import sys +import tempfile +import unittest + +SOURCE = Path(__file__).resolve().parents[2] / "app/src/main/cpp/fakeinput_steam.cpp" + +PROGRAM = r""" +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char **argv) { + assert(argc == 4); + void *symbol = dlsym(RTLD_DEFAULT, argv[1]); + assert(symbol); + int (*plain)(const char *, int, ...) = symbol; + int (*at)(int, const char *, int, ...) = symbol; + int relative = strstr(argv[1], "at") != NULL; + int temporary = !strcmp(argv[2], "temporary"); + int directory = !strcmp(argv[2], "directory"); + int dirfd = open(argv[3], O_RDONLY | O_DIRECTORY); + assert(dirfd >= 0); + assert(fchdir(dirfd) == 0); + umask(0); + const char *path = temporary || directory ? "." : "created"; + int flags = directory ? O_RDONLY | O_DIRECTORY : O_RDWR | (temporary ? O_TMPFILE : O_CREAT | O_EXCL); + int fd; + if (directory) fd = relative ? at(dirfd, path, flags) : plain(path, flags); + else fd = relative ? at(dirfd, path, flags, 0640) : plain(path, flags, 0640); + if (temporary && fd < 0 && (errno == EOPNOTSUPP || errno == EINVAL)) return 77; + assert(fd >= 0); + struct stat st; + assert(fstat(fd, &st) == 0); + if (directory) assert(S_ISDIR(st.st_mode)); + else { + if ((st.st_mode & 0777) != 0640) { + fprintf(stderr, "%s %s: expected mode 640, got %o\n", argv[1], argv[2], st.st_mode & 0777); + return 1; + } + assert(write(fd, "ok", 2) == 2); + assert(lseek(fd, 0, SEEK_SET) == 0); + char bytes[2]; + assert(read(fd, bytes, 2) == 2 && !memcmp(bytes, "ok", 2)); + } + close(fd); + close(dirfd); + return 0; +} +""" + + +@unittest.skipUnless(sys.platform == "linux" and shutil.which("gcc") and shutil.which("g++"), + "needs Linux, gcc and g++") +class FakeinputOpenTest(unittest.TestCase): + @classmethod + def setUpClass(cls): + cls.tmp = tempfile.TemporaryDirectory() + cls.addClassCleanup(cls.tmp.cleanup) + cls.path = Path(cls.tmp.name) + source = cls.path / "program.c" + source.write_text(PROGRAM) + subprocess.run(["gcc", "-o", str(cls.path / "program"), str(source), "-ldl"], check=True) + subprocess.run(["g++", "-shared", "-fPIC", "-O2", "-Wno-attributes", "-pthread", + "-std=c++17", "-static-libstdc++", "-static-libgcc", + "-Wl,--exclude-libs,ALL", "-o", str(cls.path / "libfakeinput.so"), + str(SOURCE), "-ldl"], check=True) + + def check_open(self, kind): + for function in ("open", "openat", "open64", "openat64"): + with self.subTest(function=function), tempfile.TemporaryDirectory() as directory: + args = [str(self.path / "program"), function, kind, directory] + baseline = subprocess.run(args, capture_output=True, text=True) + if baseline.returncode == 77: + self.skipTest("filesystem does not support O_TMPFILE") + self.assertEqual(baseline.returncode, 0, baseline.stderr) + if kind == "named": + (Path(directory) / "created").unlink() + result = subprocess.run(args, env=dict(os.environ, LD_PRELOAD=str(self.path / "libfakeinput.so")), + capture_output=True, text=True, timeout=10) + self.assertEqual(result.returncode, 0, result.stderr) + + def test_unnamed_temporary_files_keep_requested_permissions(self): + self.check_open("temporary") + + def test_named_files_keep_requested_permissions(self): + self.check_open("named") + + def test_directory_opens_do_not_need_a_mode_argument(self): + self.check_open("directory") + + +if __name__ == "__main__": + unittest.main() diff --git a/tools/tests/test_fakeinput_reentry.py b/tools/tests/test_fakeinput_reentry.py new file mode 100644 index 0000000000..7abd49caed --- /dev/null +++ b/tools/tests/test_fakeinput_reentry.py @@ -0,0 +1,58 @@ +from pathlib import Path +import os +import shutil +import subprocess +import sys +import tempfile +import unittest + +SOURCE = Path(__file__).resolve().parents[2] / "app/src/main/cpp/fakeinput_steam.cpp" + +# Stands in for an allocator like mimalloc that calls access() while it starts up (Claude Code +# probes /sys/devices/system/node/node1 on its first allocation, which comes from fakeinput's +# constructor when it is preloaded). +PROGRAM = r""" +#define _GNU_SOURCE +#include +#include +#include + +extern void *__libc_malloc(size_t); +static int started; + +void *malloc(size_t size) { + if (!started) { + started = 1; + access("/sys/devices/system/node/node1", R_OK); + } + return __libc_malloc(size); +} + +int main(void) { + puts("ok"); + return 0; +} +""" + + +@unittest.skipUnless(sys.platform == "linux" and shutil.which("gcc") and shutil.which("g++"), + "needs Linux, gcc and g++") +class FakeinputReentryTest(unittest.TestCase): + def test_allocator_calling_hooks_during_first_allocation_survives_preload(self): + with tempfile.TemporaryDirectory() as tmp: + path = Path(tmp) + (path / "program.c").write_text(PROGRAM) + subprocess.run(["gcc", "-rdynamic", "-o", str(path / "program"), str(path / "program.c")], check=True) + subprocess.run(["g++", "-shared", "-fPIC", "-O2", "-Wno-attributes", "-pthread", + "-std=c++17", "-static-libstdc++", "-static-libgcc", + "-Wl,--exclude-libs,ALL", "-o", str(path / "libfakeinput.so"), + str(SOURCE), "-ldl"], check=True) + result = subprocess.run([str(path / "program")], + env=dict(os.environ, LD_PRELOAD=str(path / "libfakeinput.so")), + capture_output=True, text=True, timeout=10) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertEqual(result.stdout, "ok\n") + + +if __name__ == "__main__": + unittest.main() diff --git a/tools/tests/test_fastpath_bind_names.py b/tools/tests/test_fastpath_bind_names.py index 2e8a8b11f2..08f73dab90 100644 --- a/tools/tests/test_fastpath_bind_names.py +++ b/tools/tests/test_fastpath_bind_names.py @@ -43,6 +43,24 @@ int main(int argc, char **argv) { if (detranslated(text, argv[i + 1]) == FP_SLOW) strcpy(text, "SLOW"); i += 2; puts(text); + } else if (!strcmp(argv[i], "read")) { + long fd = fp_open(AT_FDCWD, argv[++i], O_RDONLY, 0); + if (fd == FP_SLOW) puts("SLOW"); + else if (fd < 0) printf("E%ld\n", -fd); + else { + char data[64] = ""; + ssize_t n = read((int)fd, data, sizeof data - 1); + close((int)fd); + data[n > 0 ? n : 0] = 0; + puts(data); + } + } else if (!strcmp(argv[i], "chown")) { + int flags = !strcmp(argv[i + 1], "nofollow") ? AT_SYMLINK_NOFOLLOW : 0; + long r = fp_chown(AT_FDCWD, argv[i + 2], getuid(), getgid(), flags); + i += 2; + if (r == FP_SLOW) puts("SLOW"); + else if (r < 0) printf("E%ld\n", -r); + else puts("ok"); } else if (!strcmp(argv[i], "lstat")) { struct stat st; long r = fstatat(AT_FDCWD, argv[++i], &st, AT_SYMLINK_NOFOLLOW); @@ -65,13 +83,17 @@ class FastPathBindNamesTest(unittest.TestCase): cls.program = cls.work / "names" subprocess.run(["gcc", "-O1", "-w", "-pthread", "-o", str(cls.program), str(source), "-ldl"], check=True) t = cls.work / "tree" - for d in ["root/etc", "root/mnt", "root/opt", "root/d/real", "lib/common/game", "fonts", "a", "b"]: + for d in ["root/etc", "root/mnt", "root/opt", "root/d/real", "lib/common/game", "fonts", "a", "b", + "root/sys/class/thermal", "root/sys/devices/zone28", "bound"]: (t / d).mkdir(parents=True, exist_ok=True) for f in ["root/etc/x", "lib/f", "lib/common/game/f", "fonts/f", "a/f", "b/f"]: (t / f).touch() os.symlink(t / "lib/f", t / "lib/l2") os.symlink(t / "lib/common", t / "lib/l") os.symlink("real", t / "root/d/link") + os.symlink("../../devices/zone28", t / "root/sys/class/thermal/zone28") + (t / "bound/temp").write_text("bound") + (t / "root/sys/devices/zone28/other").write_text("other") cls.tree = t cls.binds = [ (t / "lib", "/mnt/droiddeck-sd"), @@ -82,6 +104,8 @@ class FastPathBindNamesTest(unittest.TestCase): (t / "a", "/opt/g"), (t / "b", "/opt/g"), (t / "fonts", "/mnt/x"), + (t / "bound/temp", "/sys/class/thermal/zone28/temp"), + (t / "bound/temp", "/dev/stdin/x"), ] @classmethod @@ -134,6 +158,31 @@ class FastPathBindNamesTest(unittest.TestCase): ("lstat", "/d/link/"), ), ["link", "dir", "dir", "dir"]) + def test_a_bound_file_under_a_link_is_found_by_its_spelled_name(self): + self.assertEqual(self.ask( + ("read", "/sys/class/thermal/zone28/temp"), + ("read", "//sys/class/thermal/./zone28//temp"), + ("read", "/sys/class/thermal/zone28/other"), + ("read", "/sys/class/thermal/zone28/temp/"), + ("read", "/sys/class/thermal/zone28/../zone28/temp"), + ("read", "/sys/class/thermal/zone28/missing"), + ("read", "/dev/stdin/x"), + ), ["bound", "bound", "SLOW", "SLOW", "SLOW", "SLOW", "SLOW"]) + + def test_chown_answers_like_the_kernel(self): + t = self.tree + self.assertEqual(self.ask( + ("chown", "follow", "/etc/x"), + ("chown", "follow", "/d/link"), + ("chown", "nofollow", "/d/link"), + ("chown", "follow", "/nope"), + ("chown", "follow", "/etc/x/y"), + ("chown", "follow", "/mnt/droiddeck-sd/f"), + ("chown", "follow", "/sys/class/thermal/zone28/temp"), + ("chown", "nofollow", "/sys/class/thermal/zone28/temp"), + ("chown", "follow", "/sys/class/thermal/zone28/other"), + ), ["ok", "ok", "ok", "E2", "E20", "ok", "ok", "ok", "SLOW"]) + if __name__ == "__main__": unittest.main() diff --git a/tools/tests/test_flatpak_uninstall.py b/tools/tests/test_flatpak_uninstall.py new file mode 100644 index 0000000000..fb78182053 --- /dev/null +++ b/tools/tests/test_flatpak_uninstall.py @@ -0,0 +1,123 @@ +import importlib.machinery +import importlib.util +import io +import json +import os +from pathlib import Path +import shutil +import sys +import tempfile +import types +import unittest +from contextlib import redirect_stdout + +SCRIPT = Path(__file__).resolve().parents[1] / "linuxfs/overlay/usr/local/bin/droiddeck-flatpak" + + +class FakeInstallation: + def __init__(self, root): + self.root = root + + def get_path(self): + return types.SimpleNamespace(get_path=lambda: self.root) + + +def load(root, ref, transactions): + repository = types.ModuleType("gi.repository") + repository.Flatpak = types.SimpleNamespace() + repository.Gio = types.SimpleNamespace() + repository.GLib = types.SimpleNamespace(Error=RuntimeError) + gi = types.ModuleType("gi") + gi.require_version = lambda *args: None + gi.repository = repository + saved = {name: sys.modules.get(name) for name in ("gi", "gi.repository")} + sys.modules["gi"] = gi + sys.modules["gi.repository"] = repository + try: + loader = importlib.machinery.SourceFileLoader("droiddeck_flatpak_uninstall", str(SCRIPT)) + module = importlib.util.module_from_spec(importlib.util.spec_from_loader(loader.name, loader)) + loader.exec_module(module) + finally: + for name, value in saved.items(): + if value is None: + sys.modules.pop(name, None) + else: + sys.modules[name] = value + module.installation = lambda: FakeInstallation(root) + module.app_ref = lambda inst, app_id: ref + + def transaction(inst): + transactions.append(inst) + raise AssertionError("an uninstalled app reached a transaction") + + module.transaction = transaction + return module + + +class FlatpakUninstallTest(unittest.TestCase): + def setUp(self): + self.root = tempfile.mkdtemp() + self.addCleanup(shutil.rmtree, self.root, True) + self.apps = os.path.join(self.root, "app") + os.makedirs(self.apps) + + def uninstall(self, app_id, ref=None): + transactions = [] + module = load(self.root, ref, transactions) + out = io.StringIO() + with redirect_stdout(out): + rc = module.uninstall(app_id) + return rc, [json.loads(line) for line in out.getvalue().splitlines()], transactions + + def test_an_app_folder_flatpak_no_longer_knows_is_removed(self): + stale = os.path.join(self.apps, "org.videolan.VLC") + os.makedirs(os.path.join(stale, "aarch64", "stable")) + os.symlink("missing-deploy", os.path.join(stale, "aarch64", "stable", "active")) + rc, events, transactions = self.uninstall("org.videolan.VLC") + self.assertEqual(rc, 0) + self.assertFalse(os.path.lexists(stale)) + self.assertEqual(events, [{"e": "done", "note": "not installed"}]) + self.assertEqual(transactions, []) + + def test_other_apps_and_link_targets_are_left_alone(self): + outside = os.path.join(self.root, "outside") + os.makedirs(outside) + keep = os.path.join(outside, "keep") + open(keep, "w").close() + stale = os.path.join(self.apps, "com.google.Chrome") + os.makedirs(stale) + os.symlink(outside, os.path.join(stale, "linked")) + other = os.path.join(self.apps, "org.videolan.VLC") + os.makedirs(other) + rc, _, _ = self.uninstall("com.google.Chrome") + self.assertEqual(rc, 0) + self.assertFalse(os.path.lexists(stale)) + self.assertTrue(os.path.isfile(keep)) + self.assertTrue(os.path.isdir(other)) + + def test_a_linked_app_folder_is_not_followed(self): + target = os.path.join(self.root, "elsewhere") + os.makedirs(target) + open(os.path.join(target, "keep"), "w").close() + os.symlink(target, os.path.join(self.apps, "com.example.Linked")) + rc, _, _ = self.uninstall("com.example.Linked") + self.assertEqual(rc, 0) + self.assertTrue(os.path.isfile(os.path.join(target, "keep"))) + + def test_ids_that_leave_the_app_folder_are_refused(self): + for app_id in ("..", ".", "", "../app", "a/b"): + rc, events, _ = self.uninstall(app_id) + self.assertEqual(rc, 0) + self.assertEqual(events, [{"e": "done", "note": "not installed"}]) + self.assertTrue(os.path.isdir(self.apps)) + + def test_an_installed_app_still_goes_through_flatpak(self): + os.makedirs(os.path.join(self.apps, "org.videolan.VLC")) + ref = types.SimpleNamespace(format_ref=lambda: "app/org.videolan.VLC/aarch64/stable") + with self.assertRaises(AssertionError): + self.uninstall("org.videolan.VLC", ref) + self.assertTrue(os.path.isdir(os.path.join(self.apps, "org.videolan.VLC"))) + + +if __name__ == "__main__": + unittest.main() diff --git a/tools/tests/test_game_environment.py b/tools/tests/test_game_environment.py index dfc65de1c7..d6cb49582e 100644 --- a/tools/tests/test_game_environment.py +++ b/tools/tests/test_game_environment.py @@ -708,6 +708,54 @@ class ReadConfigMappingTest(unittest.TestCase): class GameEnvironmentTest(unittest.TestCase): + def test_component_filter_and_prefix_repair_use_per_game_proton_architecture(self): + with tempfile.TemporaryDirectory() as tmp: + home = Path(tmp) + config = home / ".config/droiddeck/game-environment.json" + config.parent.mkdir(parents=True) + config.write_text(json.dumps({"version": 1, "shared": {"PROTON_USE_ARM64": "1"}, + "games": {"42": {"PROTON_USE_ARM64": "0"}}})) + seen_components, seen_repair, seen_start = [], [], [] + globals = MODULE["main"].__globals__ + stubs = { + "game_gpu": lambda env: env, + "mark_install_scripts": lambda command, env: [], + "install_scripts_done": lambda scripts, env: False, + "scripts_digest": lambda scripts: None, + "win_components": lambda *args: seen_components.append(args[3]) or "", + "repair_foreign_system_dlls": lambda *args: seen_repair.append(args[2]) or [], + "gl_vsync": lambda env: False, + "game_images": lambda env: None, + "skip_red_launcher": lambda command: command, + "ubisoft_overlay_off": lambda env: None, + "project_zomboid_jvm": lambda command: None, + "engine_fixes": lambda *args: ({}, [], []), + "dirt_ws2_wanted": lambda *args: None, + "apply_game_debug_logs": lambda env, config, debug_dir: env, + "offline_without_anticheat": lambda command, env: command, + "game_logs": lambda command, env, app_id: env, + "directaudio_settings": lambda env: env, + "vram_cap": lambda env: env, + "render_path_defaults": lambda *args: None, + "bridge_feature_level": lambda env: None, + "force_ssbs": lambda command, env: env, + "game_cores": lambda env: None, + "cpu_topology": lambda env, command: None, + "ego_core_plan": lambda *args: None, + "runpy": mock.Mock(run_path=lambda *args: {"launch": lambda command, env: None}), + "load_sync": lambda: {}, + "select_sync": lambda command, env, sync: (command, env), + "start": lambda command, env, *args: seen_start.append(env) or 7, + } + environment = {"HOME": tmp, "STEAM_COMPAT_DATA_PATH": "/compatdata/42", "PROTON_USE_ARM64": "1"} + command = ["/stock/proton", "waitforexitandrun", "/game/game.exe"] + with patch.dict(os.environ, environment, clear=True), patch.object(sys, "argv", ["game-env", *command]), \ + patch.dict(globals, stubs): + self.assertEqual(MODULE["main"](), 7) + self.assertEqual(["0"], [env["PROTON_USE_ARM64"] for env in seen_components]) + self.assertEqual(["0"], [env["PROTON_USE_ARM64"] for env in seen_repair]) + self.assertEqual(["0"], [env["PROTON_USE_ARM64"] for env in seen_start]) + def test_optional_xbox_loader_failure_keeps_stock_launch(self): from unittest.mock import patch for error in (FileNotFoundError('missing'), RuntimeError('module initialization failed')): @@ -902,6 +950,46 @@ class GameEnvironmentTest(unittest.TestCase): self.assertEqual(MODULE["apply_config"](env, config, "42")["TU_DEBUG"], "sysmem") self.assertNotIn("TU_DEBUG", MODULE["apply_config"](env, config, "43")) + def test_a_chromium_app_keeps_the_sessions_turnip_debug(self): + gpu = MODULE["game_gpu"] + self.assertEqual(gpu({"TU_DEBUG": "sysmem", "DROIDDECK_GAME_TU_DEBUG": "", "KEEP": "1"}, True), + {"TU_DEBUG": "sysmem", "KEEP": "1"}) + self.assertEqual(gpu({"TU_DEBUG": "sysmem", "DROIDDECK_GAME_TU_DEBUG": "nolrz"}, True), {"TU_DEBUG": "sysmem"}) + self.assertEqual(gpu({"TU_DEBUG": "sysmem"}, True), {"TU_DEBUG": "sysmem"}) + + def test_chromium_files_are_found_four_levels_deep_and_no_further(self): + chromium = MODULE["chromium_app"] + with tempfile.TemporaryDirectory() as tmp: + root = Path(tmp) / "app" + (root / "usr/lib/tool").mkdir(parents=True) + (root / "usr/lib/tool/libtool.so").write_bytes(b"") + self.assertFalse(chromium(str(root))) + for name in ("chrome-sandbox", "chrome_100_percent.pak", "v8_context_snapshot.bin"): + found = root / "a/b/c" / name + found.parent.mkdir(parents=True, exist_ok=True) + found.write_bytes(b"") + self.assertTrue(chromium(str(root)), name) + found.unlink() + deep = root / "a/b/c/d/chrome-sandbox" + deep.parent.mkdir(parents=True) + deep.write_bytes(b"") + self.assertFalse(chromium(str(root))) + self.assertFalse(chromium(str(Path(tmp) / "missing"))) + + def test_chromium_detection_does_not_follow_links_inside_the_app(self): + chromium = MODULE["chromium_app"] + with tempfile.TemporaryDirectory() as tmp: + other = Path(tmp) / "other" + other.mkdir() + (other / "chrome-sandbox").write_bytes(b"") + root = Path(tmp) / "app" + root.mkdir() + (root / "linked").symlink_to(other, target_is_directory=True) + self.assertFalse(chromium(str(root))) + link = Path(tmp) / "app-link" + link.symlink_to(other, target_is_directory=True) + self.assertTrue(chromium(str(link))) + def test_the_vram_cap_comes_before_the_games_own_dxvk_settings(self): cap = MODULE["vram_cap"] self.assertEqual(cap({"BL_VRAM_MB": "4096"})["DXVK_CONFIG"], "dxgi.maxDeviceMemory = 4096") @@ -1036,6 +1124,23 @@ class GameEnvironmentTest(unittest.TestCase): scope = MODULE["game_id"](env) expected = "untouched" if scope is None else scope if scope in {"42", "4294967295", "2147483648"} else "*" self.assertEqual(target.read_text(), expected) + for app, expected in (("42", "42"), ("4294967295", "4294967295"), ("43", "*"), ("", "*"), ("x", "*")): + with self.subTest(shared=app): + env = dict(os.environ, HOME=str(root), STEAM_COMPAT_DATA_PATH="/steam/compatdata/2929927072", SteamAppId=app) + target.write_text("untouched") + subprocess.run(["bash", "-c", script, "test", str(base)], env=env, check=True, capture_output=True) + self.assertEqual(MODULE["game_id"](env), app if app in {"42", "4294967295", "43"} else "2929927072") + self.assertEqual(target.read_text(), expected) + for game, expected in ( + (str(42 << 32 | 0x02000000), "42"), (str(4294967295 << 32 | 0x02000000), "4294967295"), + (str(42 << 32 | 0x02000001), "*"), (str(42 << 32 | 0x02000000 | 1 << 64), "*"), ("33554432", "*"), ("x", "*"), + ): + with self.subTest(direct=game): + env = dict(os.environ, HOME=str(root), STEAM_COMPAT_DATA_PATH="/steam/compatdata/2929927072", SteamAppId="0", SteamGameId=game) + target.write_text("untouched") + subprocess.run(["bash", "-c", script, "test", str(base)], env=env, check=True, capture_output=True) + self.assertEqual(MODULE["game_id"](env), expected if expected != "*" else "2929927072") + self.assertEqual(target.read_text(), expected) def test_engine_fixes_follow_the_games_files(self): with tempfile.TemporaryDirectory() as tmp: @@ -1540,6 +1645,10 @@ class GameEnvironmentTest(unittest.TestCase): self.assertIsNone(MODULE["game_id"]({"STEAM_COMPAT_DATA_PATH": prefix})) self.assertEqual(MODULE["game_id"]({"STEAM_COMPAT_DATA_PATH": "/a/compatdata/42/"}), "42") self.assertEqual(MODULE["game_id"]({"STEAM_COMPAT_DATA_PATH": "/a/compatdata/-1"}), "4294967295") + shared = {"STEAM_COMPAT_DATA_PATH": "/a/compatdata/2929927072", "SteamAppId": "2424921676"} + self.assertEqual(MODULE["game_id"](shared), "2424921676") + self.assertEqual(MODULE["game_id"]({"STEAM_COMPAT_DATA_PATH": "/a/compatdata/2929927072"}), "2929927072") + self.assertEqual(MODULE["game_id"]({"STEAM_COMPAT_DATA_PATH": "/a/compatdata/42", "SteamAppId": "7"}), "42") def test_launch_reads_updates_preserves_argv_and_does_not_execute_values(self): with tempfile.TemporaryDirectory() as tmp: @@ -2024,7 +2133,7 @@ class ProtonDefaultTest(unittest.TestCase): self.assertEqual(mapping_of(config.read_text()), {"0": self.TOOL, "427520": self.TOOL}) saved = json.loads((Path(tmp) / "state.json").read_text()) self.assertEqual(saved["native_choices"], []) - self.assertEqual(saved["auto"], {"427520": self.TOOL}) + self.assertEqual(saved["auto"], {}) finally: globals_["appinfo_platforms"] = original_platforms @@ -2545,6 +2654,178 @@ class UbisoftCompositionTest(unittest.TestCase): self.assertEqual(list(prefix.glob(".ubisoft-*.reg")), []) + +def pe_image(machine): + head = bytearray(64) + head[:2] = b"MZ" + head[60:64] = (64).to_bytes(4, "little") + return bytes(head) + b"PE\0\0" + machine.to_bytes(2, "little") + b"\0" * 32 + + +def arm64ec_pe_image(): + image = bytearray(0x400) + image[:2] = b"MZ" + image[60:64] = (64).to_bytes(4, "little") + image[64:68] = b"PE\0\0" + coff = 68 + image[coff:coff + 2] = (0x8664).to_bytes(2, "little") + image[coff + 2:coff + 4] = (1).to_bytes(2, "little") + image[coff + 16:coff + 18] = (240).to_bytes(2, "little") + optional = coff + 20 + image[optional:optional + 2] = (0x20B).to_bytes(2, "little") + image[optional + 108:optional + 112] = (16).to_bytes(4, "little") + directory = optional + 112 + 10 * 8 + image[directory:directory + 4] = (0x1000).to_bytes(4, "little") + image[directory + 4:directory + 8] = (0xD0).to_bytes(4, "little") + section = optional + 240 + image[section + 8:section + 12] = (0x200).to_bytes(4, "little") + image[section + 12:section + 16] = (0x1000).to_bytes(4, "little") + image[section + 16:section + 20] = (0x200).to_bytes(4, "little") + image[section + 20:section + 24] = (0x200).to_bytes(4, "little") + image[0x200:0x204] = (0xD0).to_bytes(4, "little") + image[0x2C8:0x2D0] = (0x10000000).to_bytes(8, "little") + return bytes(image) + + +class ForeignSystemDllTest(unittest.TestCase): + def prefix(self, tmp, arm=True): + proton = Path(tmp) / "Proton" + dist = proton / "files/lib/wine" + if arm: + (dist / "aarch64-unix").mkdir(parents=True) + else: + (dist / "x86_64-unix").mkdir(parents=True) + for libdir, machine in (("aarch64-windows", 0xAA64), ("i386-windows", 0x14C)): + (dist / libdir).mkdir(parents=True) + for name in ("ucrtbase.dll", "rpcrt4.dll"): + (dist / libdir / name).write_bytes(pe_image(machine)) + windows = Path(tmp) / "compat/pfx/drive_c/windows" + for folder in ("system32", "syswow64"): + (windows / folder).mkdir(parents=True) + return proton / "proton", Path(tmp) / "compat", windows + + def run_repair(self, compat, proton): + return MODULE["repair_foreign_system_dlls"](str(compat), str(proton)) + + def test_x64_dll_in_arm64_system32_is_replaced_by_the_builtin(self): + with tempfile.TemporaryDirectory() as tmp: + proton, compat, windows = self.prefix(tmp) + (windows / "system32/UCRTBASE.DLL").write_bytes(pe_image(0x8664)) + (windows / "system32/rpcrt4.dll").write_bytes(pe_image(0xAA64)) + fixed = self.run_repair(compat, proton) + self.assertEqual(["system32/UCRTBASE.DLL (machine 0x8664)"], fixed) + self.assertTrue((windows / "system32/UCRTBASE.DLL").is_symlink()) + self.assertEqual(0xAA64, MODULE["pe_machine"](windows / "system32/UCRTBASE.DLL")) + self.assertEqual(0x8664, MODULE["pe_machine"](windows / "system32/UCRTBASE.DLL.foreign-arch")) + self.assertFalse((windows / "system32/rpcrt4.dll").is_symlink()) + self.assertEqual([], self.run_repair(compat, proton)) + + def test_64_bit_dll_in_syswow64_is_replaced(self): + with tempfile.TemporaryDirectory() as tmp: + proton, compat, windows = self.prefix(tmp) + (windows / "syswow64/ucrtbase.dll").write_bytes(pe_image(0xAA64)) + self.assertEqual(["syswow64/ucrtbase.dll (machine 0xaa64)"], self.run_repair(compat, proton)) + self.assertEqual(0x14C, MODULE["pe_machine"](windows / "syswow64/ucrtbase.dll")) + + def test_matching_ec_symlinked_unknown_and_non_pe_files_are_left_alone(self): + with tempfile.TemporaryDirectory() as tmp: + proton, compat, windows = self.prefix(tmp) + system32 = windows / "system32" + (system32 / "ucrtbase.dll").write_bytes(pe_image(0xA641)) + (system32 / "userlib.dll").write_bytes(pe_image(0x8664)) + (system32 / "rpcrt4.dll").write_bytes(b"not a pe file") + self.assertEqual([], self.run_repair(compat, proton)) + self.assertFalse((system32 / "ucrtbase.dll").is_symlink()) + self.assertEqual(0x8664, MODULE["pe_machine"](system32 / "userlib.dll")) + + def test_x64_visual_cpp_runtime_in_arm64_system32_is_kept(self): + with tempfile.TemporaryDirectory() as tmp: + proton, compat, windows = self.prefix(tmp) + builtins = proton.parent / "files/lib/wine/aarch64-windows" + for name in ("msvcp140.dll", "VCRUNTIME140.dll", "msvcp_win.dll"): + (builtins / name.lower()).write_bytes(pe_image(0xAA64)) + (windows / "system32" / name).write_bytes(pe_image(0x8664)) + self.assertEqual(["system32/msvcp_win.dll (machine 0x8664)"], self.run_repair(compat, proton)) + self.assertFalse((windows / "system32/msvcp140.dll").is_symlink()) + self.assertFalse((windows / "system32/VCRUNTIME140.dll").is_symlink()) + + def test_final_arm64ec_image_is_not_repaired_as_a_foreign_x64_dll(self): + with tempfile.TemporaryDirectory() as tmp: + proton, compat, windows = self.prefix(tmp) + dll = windows / "system32/ucrtbase.dll" + dll.write_bytes(arm64ec_pe_image()) + self.assertEqual(0x8664, MODULE["pe_machine"](dll)) + self.assertTrue(MODULE["PE"]["machine_compatible"](dll, frozenset((0xAA64, 0xA641)))) + self.assertFalse(MODULE["PE"]["machine_compatible"](dll, frozenset((0x8664,)))) + self.assertEqual([], self.run_repair(compat, proton)) + self.assertFalse(dll.is_symlink()) + + def test_component_owned_and_native_first_foreign_dlls_are_still_replaced(self): + with tempfile.TemporaryDirectory() as tmp: + proton, compat, windows = self.prefix(tmp) + (windows / "system32/ucrtbase.dll").write_bytes(pe_image(0x8664)) + (windows / "system32/rpcrt4.dll").write_bytes(pe_image(0x8664)) + state = {"files": {"drive_c/windows/system32/ucrtbase.dll": {}}} + (compat / ".droiddeck-wincomponents.json").write_text(json.dumps(state)) + reg = 'WINE REGISTRY Version 2\n\n[Software\\\\Wine\\\\DllOverrides] 1\n"RPCRT4.DLL"="native,builtin"\n' + (compat / "pfx/user.reg").write_text(reg) + env = {"WINEDLLOVERRIDES": "ucrtbase=n,b"} + self.assertEqual(["system32/rpcrt4.dll (machine 0x8664)", "system32/ucrtbase.dll (machine 0x8664)"], + sorted(MODULE["repair_foreign_system_dlls"](str(compat), str(proton), env))) + self.assertEqual(0xAA64, MODULE["pe_machine"](windows / "system32/ucrtbase.dll")) + self.assertEqual(0xAA64, MODULE["pe_machine"](windows / "system32/rpcrt4.dll")) + + def test_newest_foreign_copy_is_the_only_backup_and_failed_final_replace_restores_it(self): + with tempfile.TemporaryDirectory() as tmp: + proton, compat, windows = self.prefix(tmp) + dll = windows / "system32/ucrtbase.dll" + dll.write_bytes(pe_image(0x8664)) + backup = windows / "system32/ucrtbase.dll.foreign-arch" + backup.write_bytes(pe_image(0x1234)) + self.assertEqual(["system32/ucrtbase.dll (machine 0x8664)"], self.run_repair(compat, proton)) + self.assertEqual(0x1234, MODULE["pe_machine"](backup)) + self.assertEqual(0x8664, MODULE["pe_machine"](backup.with_name(backup.name + ".1"))) + self.assertEqual(0xAA64, MODULE["pe_machine"](dll)) + with tempfile.TemporaryDirectory() as tmp: + proton, compat, windows = self.prefix(tmp) + dll = windows / "system32/ucrtbase.dll" + dll.write_bytes(pe_image(0x8664)) + backup = windows / "system32/ucrtbase.dll.foreign-arch" + backup.write_bytes(pe_image(0x1234)) + real = os.replace + def flaky_with_backup(src, dst): + if str(src).endswith(".bl-new"): + raise OSError("boom") + return real(src, dst) + with mock.patch("os.replace", side_effect=flaky_with_backup): + self.assertEqual([], self.run_repair(compat, proton)) + self.assertEqual(0x8664, MODULE["pe_machine"](dll)) + self.assertFalse((windows / "system32/ucrtbase.dll.foreign-arch.1").exists()) + with tempfile.TemporaryDirectory() as tmp: + proton, compat, windows = self.prefix(tmp) + dll = windows / "system32/ucrtbase.dll" + dll.write_bytes(pe_image(0x8664)) + real = os.replace + def flaky(src, dst): + if str(src).endswith(".bl-new"): + raise OSError("boom") + return real(src, dst) + with mock.patch("os.replace", side_effect=flaky): + self.assertEqual([], self.run_repair(compat, proton)) + self.assertEqual(0x8664, MODULE["pe_machine"](dll)) + self.assertFalse(dll.is_symlink()) + self.assertFalse((windows / "system32/ucrtbase.dll.foreign-arch").exists()) + self.assertFalse((windows / "system32/ucrtbase.dll.bl-new").is_symlink()) + + def test_non_arm64_proton_and_missing_prefix_are_untouched(self): + with tempfile.TemporaryDirectory() as tmp: + proton, compat, windows = self.prefix(tmp, arm=False) + (windows / "system32/ucrtbase.dll").write_bytes(pe_image(0x8664)) + self.assertEqual([], self.run_repair(compat, proton)) + self.assertEqual(0x8664, MODULE["pe_machine"](windows / "system32/ucrtbase.dll")) + self.assertEqual([], self.run_repair(Path(tmp) / "missing", proton)) + + if __name__ == "__main__": unittest.main() diff --git a/tools/tests/test_game_gpu_launchers.py b/tools/tests/test_game_gpu_launchers.py new file mode 100644 index 0000000000..09b3aef85a --- /dev/null +++ b/tools/tests/test_game_gpu_launchers.py @@ -0,0 +1,108 @@ +import os +import runpy +import shutil +import subprocess +import sys +import tempfile +import unittest +from pathlib import Path + +BIN = Path(__file__).resolve().parents[1] / "linuxfs/overlay/usr/local/bin" +KEYS = ("TU_DEBUG", "DROIDDECK_GAME_TU_DEBUG", "MESA_LOADER_DRIVER_OVERRIDE", "GALLIUM_DRIVER", + "LIBGL_KOPPER_DRI2", "VK_ICD_FILENAMES", "VK_DRIVER_FILES") +SESSION = {"TU_DEBUG": "sysmem", "DROIDDECK_GAME_TU_DEBUG": ""} +GPU_SESSION = {"MESA_LOADER_DRIVER_OVERRIDE": "zink", "GALLIUM_DRIVER": "zink", + "LIBGL_KOPPER_DRI2": "true", "VK_ICD_FILENAMES": "/runtime/turnip.json", + "VK_DRIVER_FILES": "/runtime/turnip.json"} + + +class GameGpuLaunchersTest(unittest.TestCase): + def setUp(self): + self.tmp = Path(tempfile.mkdtemp()) + self.addCleanup(shutil.rmtree, self.tmp, True) + self.out = self.tmp / "env" + self.tools = self.tmp / "tools" + self.tools.mkdir() + self.app = self.tmp / "deploy" + (self.app / "files/bin").mkdir(parents=True) + self.report = self.tools / "report" + self.report.write_text("#!/bin/sh\nfor k in %s; do eval \"echo $k=\\${$k-unset}\"; done > %s\n" + % (" ".join(KEYS), self.out)) + self.report.chmod(0o755) + flatpak = self.tools / "flatpak" + flatpak.write_text("#!/bin/sh\n" + "if [ \"$1\" = info ]; then [ \"$3\" = org.example.App ] && echo %s; exit 0; fi\n" + "exec %s\n" % (self.app, self.report)) + flatpak.chmod(0o755) + + def chromium(self): + (self.app / "files/lib/browser").mkdir(parents=True) + (self.app / "files/lib/browser/chrome_100_percent.pak").write_bytes(b"") + + def run_env(self, command, session): + env = {k: v for k, v in os.environ.items() if k not in KEYS + ("DBUS_SESSION_BUS_ADDRESS", "GAMESCOPE_WAYLAND_DISPLAY")} + env.update(session, PATH="%s:%s" % (self.tools, env.get("PATH", "/usr/bin:/bin")), + DBUS_SESSION_BUS_ADDRESS="unix:path=/dev/null") + result = subprocess.run(command, env=env, capture_output=True, text=True, timeout=30) + self.assertEqual(0, result.returncode, result.stderr) + return dict(line.split("=", 1) for line in self.out.read_text().splitlines()) + + def wrapper(self, *args, session=SESSION): + return self.run_env([sys.executable, str(BIN / "droiddeck-game-gpu"), *args, "--", str(self.report)], session) + + def flatpak(self, session=SESSION): + return self.run_env(["bash", str(BIN / "droiddeck-flatpak-run"), "--command=app", "org.example.App"], session) + + def test_the_wrapper_gives_a_program_the_game_setting(self): + self.assertEqual({key: "unset" for key in KEYS}, self.wrapper()) + self.assertEqual("gmem", self.wrapper(session={"TU_DEBUG": "sysmem", "DROIDDECK_GAME_TU_DEBUG": "gmem"})["TU_DEBUG"]) + + def test_non_flatpak_programs_keep_the_session_gpu_path(self): + values = self.wrapper(session=GPU_SESSION) + self.assertEqual(GPU_SESSION, {key: values[key] for key in GPU_SESSION}) + + def test_the_wrapper_keeps_the_session_setting_for_chromium(self): + values = self.wrapper("--chromium") + self.assertEqual(("sysmem", "unset"), (values["TU_DEBUG"], values["DROIDDECK_GAME_TU_DEBUG"])) + self.assertEqual("unset", self.wrapper("--for", str(self.app / "files"))["TU_DEBUG"]) + self.chromium() + self.assertEqual("sysmem", self.wrapper("--for", str(self.app / "files"))["TU_DEBUG"]) + + def test_without_the_game_setting_the_wrapper_changes_nothing(self): + self.assertEqual("sysmem", self.wrapper(session={"TU_DEBUG": "sysmem"})["TU_DEBUG"]) + self.assertEqual("unset", self.wrapper(session={})["TU_DEBUG"]) + + def test_the_wrapper_reports_a_missing_program(self): + result = subprocess.run([sys.executable, str(BIN / "droiddeck-game-gpu"), "--", str(self.tmp / "missing")], + capture_output=True, text=True, timeout=30) + self.assertEqual(127, result.returncode) + self.assertEqual(2, subprocess.run([sys.executable, str(BIN / "droiddeck-game-gpu")], capture_output=True, + timeout=30).returncode) + + def test_a_flatpak_app_gets_the_game_setting(self): + self.assertEqual({key: "unset" for key in KEYS}, self.flatpak(session=dict(SESSION, **GPU_SESSION))) + self.assertEqual("nolrz", self.flatpak({"TU_DEBUG": "sysmem", "DROIDDECK_GAME_TU_DEBUG": "nolrz"})["TU_DEBUG"]) + + def test_a_chromium_flatpak_keeps_the_session_setting(self): + self.chromium() + values = self.flatpak() + self.assertEqual(("sysmem", "unset"), (values["TU_DEBUG"], values["DROIDDECK_GAME_TU_DEBUG"])) + + def test_a_flatpak_without_the_game_setting_is_unchanged(self): + self.assertEqual("sysmem", self.flatpak({"TU_DEBUG": "sysmem"})["TU_DEBUG"]) + + def test_a_snap_gets_the_game_setting_unless_it_is_chromium(self): + try: + snap_run = runpy.run_path(str(BIN / "droiddeck-snap-run")) + except ImportError: + self.skipTest("PyYAML is not installed") + gpu = snap_run["game_gpu"] + snap = self.app / "files" + self.assertEqual({"KEEP": "1"}, gpu(dict(SESSION, KEEP="1"), str(snap))) + self.assertEqual({"TU_DEBUG": "nolrz"}, gpu({"TU_DEBUG": "sysmem", "DROIDDECK_GAME_TU_DEBUG": "nolrz"}, str(snap))) + self.chromium() + self.assertEqual({"TU_DEBUG": "sysmem"}, gpu(dict(SESSION), str(snap))) + + +if __name__ == "__main__": + unittest.main() diff --git a/tools/tests/test_gamescope_pointer_hold.py b/tools/tests/test_gamescope_pointer_hold.py new file mode 100644 index 0000000000..6c8d93ee4c --- /dev/null +++ b/tools/tests/test_gamescope_pointer_hold.py @@ -0,0 +1,147 @@ +from pathlib import Path +import shutil +import subprocess +import tempfile +import unittest + + +ROOT = Path(__file__).resolve().parents[2] +PATCH = ROOT / "tools/gamescope/patches/0133-constraint-relative-when-held.patch" + + +def added_source(): + return "\n".join(line[1:] for line in PATCH.read_text().splitlines() if line.startswith("+") and not line.startswith("+++")) + + +def block(text, start): + begin = text.index(start) + return text[begin:text.index("\n}", begin) + 3] + + +def rules(): + text = added_source() + return "\n".join([ + block(text, "struct PointerHold"), + block(text, "static bool pointer_hold_step"), + ]) + + +PREAMBLE = r''' +#include +#include +#include +#include +#include +''' + +MAIN = r''' +static const uint64_t FRAME = 16'666'667ul; + +static bool run(PointerHold &state, int frames, double seatDX, double seatDY, double pointerDX, double pointerDY, uint64_t &now, bool constrained = true) { + bool held = state.bHeld; + for (int i = 0; i < frames; i++) { + now += FRAME; + held = pointer_hold_step(state, constrained, seatDX, seatDY, pointerDX, pointerDY, now); + } + return held; +} + +int main() { + uint64_t now = 1'000'000'000ul; + + PointerHold cursorGame; + assert(!run(cursorGame, 600, 12.0, -9.0, 12.0, -9.0, now)); + assert(!run(cursorGame, 600, 40.0, 3.0, 39.0, 3.0, now)); + assert(!run(cursorGame, 1, 30.0, 0.0, 0.0, 0.0, now)); + assert(!run(cursorGame, 1, 30.0, 0.0, 30.0, 0.0, now)); + assert(!run(cursorGame, 1, 30.0, 0.0, 0.0, 0.0, now)); + assert(!run(cursorGame, 120, 2.0, 1.0, 0.0, 0.0, now)); + + PointerHold offset; + assert(!run(offset, 600, 25.0, 25.0, 25.0, 25.0, now)); + + PointerHold recentring; + assert(!run(recentring, 2, -40.0, 12.0, 0.0, 0.0, now)); + assert(run(recentring, 1, -40.0, 12.0, 1.0, 0.0, now)); + assert(run(recentring, 600, 0.0, 0.0, 0.0, 0.0, now)); + assert(run(recentring, 29, 10.0, 4.0, 10.0, 4.0, now)); + assert(run(recentring, 1, 20.0, 0.0, 0.0, 0.0, now)); + assert(run(recentring, 29, 10.0, 4.0, 10.0, 4.0, now)); + assert(!run(recentring, 1, 10.0, 4.0, 10.0, 4.0, now)); + + PointerHold clipped; + assert(run(clipped, 3, 0.0, 30.0, 0.0, 2.0, now)); + + PointerHold spaced; + for (int i = 0; i < 10; i++) { + assert(!run(spaced, 1, 50.0, 0.0, 0.0, 0.0, now)); + now += 600'000'000ul; + } + + PointerHold hinted; + hinted.bHeld = true; + assert(run(hinted, 29, 0.0, 15.0, 0.0, 15.0, now)); + assert(!run(hinted, 1, 0.0, 15.0, 0.0, 15.0, now)); + + PointerHold hintedCamera; + hintedCamera.bHeld = true; + assert(run(hintedCamera, 600, 30.0, -8.0, 0.0, 0.0, now)); + + PointerHold chained; + for (int i = 0; i < 6; i++) { + assert(!run(chained, 1, 50.0, 0.0, 0.0, 0.0, now)); + now += 400'000'000ul; + } + + PointerHold released; + assert(run(released, 3, 0.0, -25.0, 0.0, 0.0, now)); + assert(!run(released, 1, 0.0, -25.0, 0.0, 0.0, now, false)); + assert(!run(released, 2, 0.0, -25.0, 0.0, 0.0, now)); + + puts("ok"); + return 0; +} +''' + + +@unittest.skipUnless(shutil.which("c++"), "needs a C++ compiler") +class PointerHoldTest(unittest.TestCase): + def test_only_a_game_that_holds_the_x_pointer_goes_relative(self): + with tempfile.TemporaryDirectory() as tmp: + source = Path(tmp) / "hold.cpp" + binary = Path(tmp) / "hold" + source.write_text(PREAMBLE + rules() + MAIN) + subprocess.run(["c++", "-std=c++17", "-Wall", "-Wextra", "-Werror", str(source), "-o", str(binary)], check=True) + self.assertEqual(subprocess.run([str(binary)], capture_output=True, text=True, check=True).stdout.strip(), "ok") + + def test_a_hinted_constraint_starts_relative_and_others_wait_for_the_hold(self): + added = [line.strip() for line in added_source().splitlines()] + self.assertIn("update_pointer_hold( pPaintFocus, bHasPointerConstraint && bImageEmpty && nRelativeMouse != 2 && !bExcludedAppId,", added) + self.assertIn("pPaintFocus->cursor && pPaintFocus->cursor->IsConstrained() );", added) + self.assertIn("s_Hold.bHeld = bHinted;", added) + self.assertIn("if ( s_bTracking && bHinted && !s_bHinted )", added) + self.assertIn("const bool bGameHoldsPointer = bHasPointerConstraint && s_bPointerHeld;", added) + self.assertIn(": ( bForcedRelativeMouse || s_bFollowClientPointer || ( bImageEmpty && bGameHoldsPointer && !bExcludedAppId ) );", added) + + def test_a_new_window_or_constraint_starts_its_own_hold(self): + tracker = block(added_source(), "static void update_pointer_hold") + reset = tracker.index("if ( pWindow != s_pWindow || pConstraint != s_pConstraint )") + self.assertIn("const steamcompmgr_win_t *pWindow = pFocus->inputFocusWindow;", tracker) + constraint = tracker.index("wlserver.GetCursorConstraint();") + self.assertLess(tracker.index("wlserver_lock();"), constraint) + self.assertLess(constraint, tracker.index("wlserver_unlock();")) + self.assertLess(reset, tracker.index("if ( s_bTracking && bHinted && !s_bHinted )")) + self.assertIn("s_bTracking = false;", tracker[reset:reset + 120]) + + def test_the_motion_sent_to_clients_is_counted(self): + added = [line.strip() for line in added_source().splitlines()] + self.assertIn("wlserver.flSentDX += unaccel_dx;", added) + self.assertIn("wlserver.flSentDY += unaccel_dy;", added) + + def test_the_forced_and_follow_rules_are_unchanged(self): + removed = [line[1:].strip() for line in PATCH.read_text().splitlines() if line.startswith("-") and not line.startswith("---")] + self.assertEqual(removed, [": ( bForcedRelativeMouse || s_bFollowClientPointer || ( bImageEmpty && bHasPointerConstraint && !bExcludedAppId ) );"]) + + +if __name__ == "__main__": + unittest.main() diff --git a/tools/tests/test_memory_pressure.py b/tools/tests/test_memory_pressure.py index 8aa2d1f2c5..13f9e2f8d3 100644 --- a/tools/tests/test_memory_pressure.py +++ b/tools/tests/test_memory_pressure.py @@ -32,7 +32,7 @@ step() { :; } bl_zram_usage() { _bl_rss=$rss _bl_swap=$client_swap; } bl_zram_send() { echo "send:$1:$3"; rss=$((rss - reclaimed)); _bl_pageout_complete=${pageout_complete:-1}; } ''' + functions + '\nbl_memory_read\n' + body - result = run_bash(harness) + result = run_bash('NOW=0\n' + harness.replace('SECONDS', 'NOW')) self.assertEqual(0, result.returncode, result.stderr) self.assertEqual('', result.stderr) return result.stdout.splitlines() @@ -168,7 +168,7 @@ bl_session_others() { _bl_session=(321 654); } bl_zram_usage() { _bl_rss=$rss _bl_swap=$swap; } bl_zram_send() { echo "send:$1:$3"; rss=$((rss - pushed)); swap=$((swap + pushed)); _bl_pageout_complete=$complete; } ''' + functions + '\nbl_memory_read\n' + body - result = run_bash(harness) + result = run_bash('NOW=0\n' + harness.replace('SECONDS', 'NOW')) self.assertEqual(0, result.returncode, result.stderr) self.assertEqual('', result.stderr) return result.stdout.splitlines() @@ -229,9 +229,34 @@ echo "backoff:$_bl_tier_backoff" self.assertEqual(['send:1:game', 'send:1:game'], self.run_policy(''' _bl_tier_backoff=300 SECONDS=0; bl_session_pressure -swap=0 SECONDS=20; bl_session_pressure SECONDS=30; bl_session_pressure +''')) + + def test_nearly_out_of_memory_still_backs_off_when_the_game_reads_its_pageout_back(self): + self.memory(available=512) + self.assertEqual(['send:1:game', 'backoff:60', 'send:1:game'], self.run_policy(''' +SECONDS=0; bl_session_pressure +swap=0 +SECONDS=30; bl_session_pressure +echo "backoff:$_bl_tier_backoff" +SECONDS=59; bl_session_pressure +SECONDS=60; bl_session_pressure +SECONDS=90; bl_session_pressure +''')) + + def test_nearly_out_of_memory_retries_a_skipped_pageout_after_30_seconds(self): + self.memory(available=512) + self.assertEqual(['send:1:game', 'send:1:game'], self.run_policy(''' +SECONDS=0; bl_session_pressure +swap=0 +SECONDS=30; bl_session_pressure +_bl_tier_backoff=300 +saved=$rss; rss=$((128 * 1024)) +SECONDS=330; bl_session_pressure +rss=$saved +SECONDS=359; bl_session_pressure +SECONDS=360; bl_session_pressure ''')) def test_small_swap_headroom_skips_the_pageout(self): diff --git a/tools/tests/test_netlink_preload.py b/tools/tests/test_netlink_preload.py index 64c3b9c97a..aedbdbd733 100644 --- a/tools/tests/test_netlink_preload.py +++ b/tools/tests/test_netlink_preload.py @@ -118,6 +118,7 @@ PROGRAM = r""" #include #include #include +#include #include #include @@ -317,6 +318,8 @@ static void *ordinary_traffic(void *argument) { probe_end(counts); require(counts[0] == 0, "ordinary setup avoids netlink lock"); + int pipes[2]; + require(pipe(pipes) == 0, "ordinary pipe setup"); probe_begin(0); const int rounds = 32; for (int round = 0; round < rounds; round++) { @@ -338,6 +341,15 @@ static void *ordinary_traffic(void *argument) { message.msg_name = &source; message.msg_namelen = sizeof(source); require(recvmsg(server, &message, 0) == 1 && buffer == 'z', "ordinary recvmsg"); + require(write(client, "w", 1) == 1, "ordinary write"); + require(read(server, &buffer, 1) == 1 && buffer == 'w', "ordinary read"); + char pair[2]; + struct iovec pieces[2] = {{.iov_base = "v", .iov_len = 1}, {.iov_base = "u", .iov_len = 1}}; + require(writev(client, pieces, 2) == 2, "ordinary writev"); + require(recv(server, pair, 2, MSG_WAITALL) == 2 && pair[0] == 'v' && pair[1] == 'u', + "ordinary writev data"); + require(write(pipes[1], "p", 1) == 1 && read(pipes[0], &buffer, 1) == 1 && buffer == 'p', + "ordinary pipe"); length = sizeof(source); require(getpeername(client, (struct sockaddr *)&source, &length) == 0, "ordinary peer"); const int options[] = {SO_TYPE, SO_DOMAIN, SO_PROTOCOL}; @@ -368,6 +380,8 @@ static void *ordinary_traffic(void *argument) { require(counts[2] == (unsigned)rounds, "ordinary traffic has only requested getpeername"); require(counts[3] == 0, "ordinary traffic avoids restoration fstat"); require(counts[4] == (unsigned)rounds * 3, "ordinary socket options use one syscall each"); + close(pipes[0]); + close(pipes[1]); close(client); close(server); if (server != listener) close(listener); @@ -798,6 +812,52 @@ static void connect_test(void) { close(descriptor); } +static ssize_t link_reply(int descriptor, const char *path, unsigned char *buffer, size_t capacity) { + struct { + struct nlmsghdr header; + struct ifinfomsg info; + } query = { + .header = {.nlmsg_len = sizeof(query), .nlmsg_type = RTM_GETLINK, + .nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK, .nlmsg_seq = 42}, + .info = {.ifi_index = 1} + }; + struct iovec parts[2] = {{.iov_base = &query.header, .iov_len = sizeof(query.header)}, + {.iov_base = &query.info, .iov_len = sizeof(query.info)}}; + struct msghdr message = {.msg_iov = parts, .msg_iovlen = 2}; + ssize_t sent; + if (!strcmp(path, "send")) sent = send(descriptor, &query, sizeof(query), 0); + else if (!strcmp(path, "sendmsg")) sent = sendmsg(descriptor, &message, 0); + else if (!strcmp(path, "write")) sent = write(descriptor, &query, sizeof(query)); + else sent = writev(descriptor, parts, 2); + require(sent == sizeof(query), path); + ssize_t received = read(descriptor, buffer, capacity); + require(received > 0, "read reply"); + return received; +} + +static void write_test(void) { + const char *paths[] = {"send", "sendmsg", "write", "writev"}; + unsigned char expected[4096]; + int descriptor = route_socket(); + ssize_t expected_length = link_reply(descriptor, paths[0], expected, sizeof(expected)); + struct nlmsghdr *header = (struct nlmsghdr *)expected; + require(header->nlmsg_type == RTM_NEWLINK && header->nlmsg_seq == 42 + && header->nlmsg_pid == port_of(descriptor), "send reply"); + require(expected_length > (ssize_t)NLMSG_ALIGN(header->nlmsg_len), "acknowledged reply"); + struct nlmsghdr *acknowledgement = (struct nlmsghdr *)(expected + NLMSG_ALIGN(header->nlmsg_len)); + require(acknowledgement->nlmsg_type == NLMSG_ERROR + && ((struct nlmsgerr *)NLMSG_DATA(acknowledgement))->error == 0, "send acknowledgement"); + for (size_t index = 1; index < sizeof(paths) / sizeof(paths[0]); index++) { + unsigned char buffer[4096]; + ssize_t length = link_reply(descriptor, paths[index], buffer, sizeof(buffer)); + require(length == expected_length, "reply length"); + require(!memcmp(buffer, expected, length), "reply bytes"); + } + require(recv(descriptor, expected, sizeof(expected), MSG_DONTWAIT) == -1 && errno == EAGAIN, + "no extra replies"); + close(descriptor); +} + static void receive_test(void) { int descriptor = route_socket(); send_link(descriptor, 1, 0); @@ -985,6 +1045,7 @@ int main(int argc, char **argv) { else if (!strcmp(argv[1], "nonblocking")) send_timeout_test(1); else if (!strcmp(argv[1], "connect")) connect_test(); else if (!strcmp(argv[1], "receive")) receive_test(); + else if (!strcmp(argv[1], "write")) write_test(); else if (!strcmp(argv[1], "replace")) replacement_test(0); else if (!strcmp(argv[1], "raw_replace")) replacement_test(1); else if (!strcmp(argv[1], "shared")) shared_test(0); @@ -1160,6 +1221,9 @@ class NetlinkPreloadTest(unittest.TestCase): def test_kernel_connect_and_disconnect(self): self.run_program("connect") + def test_write_and_writev_requests_get_the_same_replies_as_send(self): + self.run_program("write") + def test_receive_sources_and_truncation(self): self.run_program("receive") diff --git a/tools/tests/test_pr_discord.py b/tools/tests/test_pr_discord.py index 0533e7094c..a80efdff75 100644 --- a/tools/tests/test_pr_discord.py +++ b/tools/tests/test_pr_discord.py @@ -14,17 +14,30 @@ class PrDiscordTest(unittest.TestCase): self.discord.request.return_value = {"id": "1557547293375332465"} self.github = patch.object(module, "github").start() self.addCleanup(patch.stopall) - self.github.side_effect = lambda path, method="GET", body=None: self.pull if "/pulls/" in path else self.comment + def github_request(path, method="GET", body=None): + if "/pulls/" in path: + return self.pull + if "/commits/" in path: + return {"commit": {"message": "Fix reconnect behavior\n\nMore details"}} + if path.endswith("/issues/comments/10"): + if method == "PATCH": + self.comment["body"] = body["body"] + return self.comment + return self.comment + self.github.side_effect = github_request patch.object(module, "report_comment", return_value=self.comment).start() - def publish(self): - module.sync("publish", "Droid-Deck/DroidDeck", "410", self.discord, "a" * 40, + def publish(self, sha=None): + if sha is not None: + self.pull["head"]["sha"] = sha + sha = sha or "a" * 40 + module.sync("publish", "Droid-Deck/DroidDeck", "410", self.discord, sha, "https://github.com/Droid-Deck/DroidDeck-CI/releases/download/pr-410/build.apk", "https://github.com/Droid-Deck/DroidDeck-CI/releases/tag/pr-410") def save_state(self): self.comment["body"] += '\n' + "webhook_id": self.discord.id, "message_id": "1557547293375332465", "sha": "a" * 40}) + ' -->' def test_create_confirmed_message_and_persist_reference(self): self.publish() @@ -36,12 +49,76 @@ class PrDiscordTest(unittest.TestCase): self.assertEqual("1557547293375332465", module.saved_message(saved, self.discord.id)) self.assertIn("Build download", saved) - def test_new_build_updates_existing_message(self): + def test_same_build_updates_existing_message_without_duplicate(self): self.save_state() self.publish() self.assertEqual("PATCH", self.discord.request.call_args.args[0]) saved = self.github.call_args.args[2]["body"] self.assertEqual(1, len(module.STATE.findall(saved))) + self.assertIn('"sha": "' + "a" * 40 + '"', saved) + + def test_new_commit_retires_old_message_and_posts_replacement(self): + self.save_state() + old_content = module.build_content("Droid-Deck/DroidDeck", "410", self.pull["title"], "a" * 40, + "apk", "release") + self.discord.request.side_effect = [ + {"content": old_content}, {"id": "1557547293375332465"}, {"id": "1557547293375332466"}] + self.publish("b" * 40) + calls = self.discord.request.call_args_list + self.assertEqual(["GET", "PATCH", "POST"], [call.args[0] for call in calls]) + self.assertIn("**Superseded by `bbbbbbb`**", calls[1].args[2]["content"]) + self.assertIn("~~Download:", calls[1].args[2]["content"]) + self.assertIn("Latest commit: Fix reconnect behavior", calls[2].args[2]["content"]) + saved = self.github.call_args.args[2]["body"] + self.assertEqual("1557547293375332466", module.saved_message(saved, self.discord.id)) + self.assertIn('"sha": "' + "b" * 40 + '"', saved) + + def test_publish_supersede_rerun_and_close_lifecycle(self): + messages = {} + created = [] + next_id = 1 + + def discord_request(method, suffix="", body=None): + nonlocal next_id + if method == "POST": + message_id = str(1557547293375332465 + next_id) + next_id += 1 + messages[message_id] = {"id": message_id, "content": body["content"]} + created.append(message_id) + return messages[message_id] + message_id = suffix.split("/")[-1] + if method == "GET": + return messages.get(message_id) + if method == "PATCH": + if message_id not in messages: + return None + messages[message_id].update(body) + return messages[message_id] + self.fail(f"Unexpected Discord method: {method}") + + self.discord.request.side_effect = discord_request + self.publish("a" * 40) + first_id = created[-1] + self.publish("b" * 40) + second_id = created[-1] + self.publish("b" * 40) + self.assertEqual(2, len(created)) + self.assertIn("**Superseded by `bbbbbbb`**", messages[first_id]["content"]) + self.assertIn("`bbbbbbb`", messages[second_id]["content"]) + + self.pull.update(state="closed", merged=True) + module.sync("retire", "Droid-Deck/DroidDeck", "410", self.discord) + self.assertTrue(messages[second_id]["content"].startswith("**Merged**")) + self.assertIn("~~Latest commit:", messages[second_id]["content"]) + + def test_legacy_state_uses_embedded_short_sha_to_supersede(self): + self.save_state() + self.comment["body"] = self.comment["body"].replace(', "sha": "' + "a" * 40 + '"', '') + old_content = "**Signed test build** `pr-410` · `aaaaaaa`\nold build" + self.discord.request.side_effect = [ + {"content": old_content}, {"id": "1557547293375332465"}, {"id": "1557547293375332466"}] + self.publish("b" * 40) + self.assertEqual(["GET", "PATCH", "POST"], [call.args[0] for call in self.discord.request.call_args_list]) def test_deleted_message_is_recreated(self): self.save_state() @@ -78,6 +155,10 @@ class PrDiscordTest(unittest.TestCase): module.sync("retire", "Droid-Deck/DroidDeck", "410", self.discord) self.discord.request.assert_not_called() + def test_superseded_content_is_idempotent(self): + once = module.retired_content("first\n\nlast", superseded_by="a" * 40) + self.assertEqual(once, module.retired_content(once, superseded_by="a" * 40)) + def test_old_build_without_saved_id_is_not_guessed(self): self.pull["state"] = "closed" module.sync("retire", "Droid-Deck/DroidDeck", "410", self.discord) @@ -153,6 +234,29 @@ class PrDiscordTest(unittest.TestCase): self.assertLessEqual(len(content), 1800) self.assertLessEqual(len(module.retired_content(content, True)), 2000) + def test_commit_subject_is_sanitized_and_uses_only_first_line(self): + content = module.build_content("Droid-Deck/DroidDeck", "410", "title", "a" * 40, + "apk", "release", commit_message="Fix *input* @everyone\nDetails") + self.assertIn(r"Latest commit: Fix \*input\* @everyone", content) + self.assertNotIn("Details", content) + + def test_commit_lookup_failure_does_not_block_announcement(self): + def unavailable_commit(path, method="GET", body=None): + if "/commits/" in path: + raise RuntimeError("GitHub request failed") + return self.pull if "/pulls/" in path else self.comment + self.github.side_effect = unavailable_commit + self.publish() + self.assertEqual("POST", self.discord.request.call_args.args[0]) + self.assertNotIn("Latest commit:", self.discord.request.call_args.args[2]["content"]) + + def test_empty_commit_response_does_not_block_announcement(self): + self.github.side_effect = lambda path, method="GET", body=None: ( + self.pull if "/pulls/" in path else None if "/commits/" in path else self.comment) + self.publish() + self.assertEqual("POST", self.discord.request.call_args.args[0]) + self.assertNotIn("Latest commit:", self.discord.request.call_args.args[2]["content"]) + original_report = module.report_comment diff --git a/tools/tests/test_preload_snapshots.py b/tools/tests/test_preload_snapshots.py index 6629d1ab22..2d0ee01642 100644 --- a/tools/tests/test_preload_snapshots.py +++ b/tools/tests/test_preload_snapshots.py @@ -150,6 +150,30 @@ class PreloadSnapshotTest(unittest.TestCase): with self.assertRaisesRegex(ValueError, 'successful main'): snapshots.artifact_bytes(self.data['origin'], self.root / 'cache') + def test_origin_apk_without_a_newer_output_reads_the_rest(self): + from zipfile import ZipFile + newest = 'libddnatv-hook.so' + apk = io.BytesIO() + with ZipFile(apk, 'w') as z: + for name, value in self.binaries.items(): + if name != newest: + z.writestr('assets/linuxfs/' + name, value) + outer = io.BytesIO() + with ZipFile(outer, 'w') as z: + z.writestr('app-release.apk', apk.getvalue()) + digest = snapshots.sha(outer.getvalue()) + origin = dict(self.data['origin'], artifact_sha256=digest) + build = dict(event='push', head_branch='main', conclusion='success', head_sha=origin['commit'], + path='.github/workflows/build.yml', repository=dict(full_name=snapshots.REPO)) + artifact = dict(name='droiddeck-apk', workflow_run=dict(id=origin['run']), digest='sha256:' + digest) + cache = self.root / 'cache' + cache.mkdir() + (cache / (digest + '.zip')).write_bytes(outer.getvalue()) + with patch.object(snapshots, 'api', side_effect=[build, artifact]): + content = snapshots.artifact_bytes(origin, cache) + self.assertNotIn(newest, content) + self.assertEqual(set(content), snapshots.FILES - {newest}) + def test_corrupt_archive_is_rejected(self): with self.assertRaisesRegex(ValueError, 'checksum'): snapshots.unpack(self.content + b'tampered', self.data) diff --git a/tools/tests/test_quest_driver_download.py b/tools/tests/test_quest_driver_download.py new file mode 100644 index 0000000000..aed4f47c1b --- /dev/null +++ b/tools/tests/test_quest_driver_download.py @@ -0,0 +1,56 @@ +import importlib.util +import io +import unittest +import urllib.error +from pathlib import Path +from unittest import mock + +SCRIPT = Path(__file__).resolve().parents[1] / "quest/build-driver.py" +spec = importlib.util.spec_from_file_location("build_driver", SCRIPT) +driver = importlib.util.module_from_spec(spec) +spec.loader.exec_module(driver) + + +def http_error(code): + return urllib.error.HTTPError("https://example.invalid/a.zip", code, "error", {}, io.BytesIO()) + + +class Response(io.BytesIO): + def __enter__(self): + return self + + def __exit__(self, *args): + self.close() + + +class DriverDownloadTest(unittest.TestCase): + def fetch(self, *outcomes): + calls = [] + + def urlopen(url, timeout): + calls.append(url) + outcome = outcomes[len(calls) - 1] + if isinstance(outcome, Exception): + raise outcome + return Response(outcome) + + with mock.patch.object(driver.urllib.request, "urlopen", urlopen), mock.patch.object(driver.time, "sleep") as sleep: + try: + return driver.download("https://example.invalid/a.zip"), len(calls), sleep.call_count + except Exception as error: + return error, len(calls), sleep.call_count + + def test_a_timeout_or_server_error_is_retried(self): + self.assertEqual((b"zip", 3, 2), self.fetch(TimeoutError("read timed out"), http_error(502), b"zip")) + + def test_a_missing_file_fails_at_once(self): + error, calls, sleeps = self.fetch(http_error(404)) + self.assertEqual((404, 1, 0), (error.code, calls, sleeps)) + + def test_it_gives_up_after_the_last_attempt(self): + error, calls, sleeps = self.fetch(*[http_error(504)] * 4) + self.assertEqual((504, 4, 3), (error.code, calls, sleeps)) + + +if __name__ == "__main__": + unittest.main() diff --git a/tools/tests/test_runtime_inputs.py b/tools/tests/test_runtime_inputs.py index 60c0fc9920..906bef4c2f 100644 --- a/tools/tests/test_runtime_inputs.py +++ b/tools/tests/test_runtime_inputs.py @@ -33,6 +33,9 @@ class RuntimeInputsTest(unittest.TestCase): self.source = self.root / 'tools/linuxfs/preload/test.c' self.source.parent.mkdir(parents=True) self.source.write_text('current source') + service = self.root / 'app/src/main/assets/linuxfs/usr/local/lib/droiddeck-ubisoft/droiddeck-upc.exe' + service.parent.mkdir(parents=True) + service.write_bytes(b'MZ service') self.audio(['pactl', 'modules/arm64/module-aaudio-sink.so', 'modules/arm64/module-directaudio-native-sink.so']) self.packs() @@ -179,6 +182,13 @@ class RuntimeInputsTest(unittest.TestCase): with self.assertRaisesRegex(ValueError, 'xbox pack provides'): runtime.check_apk(apk, self.root) + def test_an_apk_still_carrying_the_ubisoft_service_is_rejected(self): + name = 'assets/linuxfs/usr/local/lib/droiddeck-ubisoft/droiddeck-upc.exe' + self.assertIn(name, runtime.outputs(self.root)) + apk = self.current_apk(self.root / 'bundled.apk', {name: b'MZ service'}) + with self.assertRaisesRegex(ValueError, 'ubisoft pack provides'): + runtime.check_apk(apk, self.root) + def test_an_apk_still_carrying_the_powervr_runtime_is_rejected(self): apk = self.current_apk(self.root / 'bundled.apk', {'assets/hybris/runtime.tzst': b'runtime'}) with self.assertRaisesRegex(ValueError, 'powervr pack provides'): diff --git a/tools/tests/test_session_appimage.py b/tools/tests/test_session_appimage.py index f1ba2d11af..3fc5352427 100644 --- a/tools/tests/test_session_appimage.py +++ b/tools/tests/test_session_appimage.py @@ -6,8 +6,12 @@ import tempfile import unittest from pathlib import Path -SESSION = Path(__file__).resolve().parents[1] / "linuxfs/overlay/usr/local/bin/droiddeck-session" +BIN = Path(__file__).resolve().parents[1] / "linuxfs/overlay/usr/local/bin" +SESSION = BIN / "droiddeck-session" +EXEC = BIN / "droiddeck-appimage-exec" URUNTIME = "/usr/local/lib/droiddeck/uruntime" +FEX = "/usr/local/bin/droiddeck-fex" +REPORT = "#!/bin/sh\nprintf '%%s\\n' \"TARGET=${TARGET_APPIMAGE:-} EXTRACT=${APPIMAGE_EXTRACT_AND_RUN:-}\" %s \"$@\"\n" def target_case(): @@ -15,34 +19,54 @@ def target_case(): return match.group(0) -class SessionAppImageTest(unittest.TestCase): +class AppImageExecTest(unittest.TestCase): def setUp(self): self.dir = Path(tempfile.mkdtemp()) self.addCleanup(shutil.rmtree, self.dir, True) self.image = self.dir / "rpcs3.AppImage" - self.image.write_bytes(b"\x7fELF") - self.uruntime = self.dir / "uruntime" - self.uruntime.write_text("#!/bin/sh\n") - self.uruntime.chmod(0o755) + self.machine(183) + self.uruntime = self.tool("uruntime", "uruntime") + self.fex = self.tool("droiddeck-fex", "fex") - def launch(self, fex=()): - script = ("set -u\n" - "run_program() { printf '%%s\\n' \"TARGET=${TARGET_APPIMAGE:-} EXTRACT=${APPIMAGE_EXTRACT_AND_RUN:-}\" \"$@\"; exit 0; }\n" - "target=%s\nfex=(%s)\nset -- --no-gui\n" % (self.image, " ".join(fex)) - + target_case().replace(URUNTIME, str(self.uruntime))) + def machine(self, number): + self.image.write_bytes(b"\x7fELF" + bytes(14) + number.to_bytes(2, "little") + bytes(44)) + + def tool(self, name, label): + path = self.dir / name + path.write_text(REPORT % label) + path.chmod(0o755) + return path + + def launch(self): + script = self.dir / "exec" + script.write_text(EXEC.read_text().replace(URUNTIME, str(self.uruntime)).replace(FEX, str(self.fex))) env = {k: v for k, v in os.environ.items() if k not in ("TARGET_APPIMAGE", "APPIMAGE_EXTRACT_AND_RUN")} - return subprocess.run(["bash", "-c", script], env=env, capture_output=True, text=True, timeout=30).stdout.splitlines() + return subprocess.run(["bash", str(script), str(self.image), "--no-gui"], env=env, + capture_output=True, text=True, timeout=30).stdout.splitlines() def test_an_arm64_image_runs_through_the_shipped_runtime(self): - self.assertEqual(["TARGET=%s EXTRACT=1" % self.image, str(self.uruntime), "--no-gui"], self.launch()) + self.assertEqual(["TARGET=%s EXTRACT=1" % self.image, "uruntime", "--no-gui"], self.launch()) def test_without_the_shipped_runtime_the_image_runs_itself(self): self.uruntime.unlink() - self.assertEqual(["TARGET= EXTRACT=1", str(self.image), "--no-gui"], self.launch()) + self.image.write_text(REPORT % "own") + self.assertEqual(["TARGET= EXTRACT=1", "own", "--no-gui"], self.launch()) def test_an_x86_image_still_starts_under_fex(self): - fex = ("/usr/local/bin/droiddeck-fex", "run", "--mode", "on", "--") - self.assertEqual(["TARGET= EXTRACT=1", *fex, str(self.image), "--no-gui"], self.launch(fex)) + self.machine(62) + self.assertEqual(["TARGET= EXTRACT=1", "fex", "run", "--mode", "on", "--", str(self.image), "--no-gui"], self.launch()) + + def test_an_x86_image_without_fex_uses_the_shipped_runtime(self): + self.machine(62) + self.fex.unlink() + self.assertEqual(["TARGET=%s EXTRACT=1" % self.image, "uruntime", "--no-gui"], self.launch()) + + def test_the_session_starts_images_through_the_shared_launcher(self): + script = ("set -u\n" + "run_program() { printf '%%s\\n' \"$@\"; exit 0; }\n" + "target=%s\nfex=()\nset -- --no-gui\n" % self.image + target_case()) + out = subprocess.run(["bash", "-c", script], capture_output=True, text=True, timeout=30).stdout.splitlines() + self.assertEqual(["/usr/local/bin/droiddeck-appimage-exec", str(self.image), "--no-gui"], out) if __name__ == "__main__": diff --git a/tools/tests/test_session_bus.py b/tools/tests/test_session_bus.py new file mode 100644 index 0000000000..3528500f1d --- /dev/null +++ b/tools/tests/test_session_bus.py @@ -0,0 +1,55 @@ +import re +import shutil +import subprocess +import tempfile +import unittest +from pathlib import Path + +SESSION = Path(__file__).resolve().parents[1] / "linuxfs/overlay/usr/local/bin/droiddeck-session" + +FAKE_DAEMON = """#!/usr/bin/env python3 +import socket, sys, time +address = next(a for a in sys.argv if a.startswith("--address=")) +path = address.split("unix:path=", 1)[1] +open(sys.argv[0] + ".args", "w").write(" ".join(sys.argv[1:])) +s = socket.socket(socket.AF_UNIX) +s.bind(path) +time.sleep(5) +""" + + +class SessionBusTest(unittest.TestCase): + def setUp(self): + self.root = Path(tempfile.mkdtemp()) + self.addCleanup(shutil.rmtree, self.root, True) + self.bin = self.root / "bin" + self.bin.mkdir() + self.runtime = self.root / "run" + self.runtime.mkdir() + daemon = self.bin / "dbus-daemon" + daemon.write_text(FAKE_DAEMON) + daemon.chmod(0o755) + + def run_block(self, env_address=""): + match = re.search(r'^ *if \[ -z "\$\{DBUS_SESSION_BUS_ADDRESS:-\}" \]; then\n.*?^ *fi\n *fi\n', + SESSION.read_text(), re.M | re.S) + self.assertIsNotNone(match) + script = "nap() { sleep \"$1\"; }\n" + match.group(0) + 'echo "ADDRESS=$DBUS_SESSION_BUS_ADDRESS"\nkill %1 2>/dev/null\n' + env = {"PATH": "%s:/usr/bin:/bin" % self.bin, "XDG_RUNTIME_DIR": str(self.runtime)} + if env_address: + env["DBUS_SESSION_BUS_ADDRESS"] = env_address + return subprocess.run(["bash", "-c", script], env=env, capture_output=True, text=True, timeout=30).stdout + + def test_steam_mode_exports_its_own_session_bus(self): + out = self.run_block() + self.assertIn("ADDRESS=unix:path=%s/bus" % self.runtime, out) + self.assertIn("--session", (self.bin / "dbus-daemon.args").read_text()) + + def test_an_existing_session_bus_is_kept(self): + out = self.run_block("unix:path=/somewhere/else") + self.assertIn("ADDRESS=unix:path=/somewhere/else", out) + self.assertFalse((self.bin / "dbus-daemon.args").exists()) + + +if __name__ == "__main__": + unittest.main() diff --git a/tools/tests/test_session_machine_id.py b/tools/tests/test_session_machine_id.py new file mode 100644 index 0000000000..20fb3c4b1e --- /dev/null +++ b/tools/tests/test_session_machine_id.py @@ -0,0 +1,61 @@ +import re +import shutil +import subprocess +import tempfile +import unittest +from pathlib import Path + +SESSION = Path(__file__).resolve().parents[1] / "linuxfs/overlay/usr/local/bin/droiddeck-session" +FRESH = "0123456789abcdef0123456789abcdef" + + +class SessionMachineIdTest(unittest.TestCase): + def setUp(self): + self.root = Path(tempfile.mkdtemp()) + self.addCleanup(shutil.rmtree, self.root, True) + self.bin = self.root / "bin" + self.bin.mkdir() + self.file = self.root / "machine-id" + + def generator(self, output, status=0): + tool = self.bin / "dbus-uuidgen" + tool.write_text("#!/bin/sh\necho %s\nexit %d\n" % (output, status)) + tool.chmod(0o755) + + def ensure(self): + match = re.search(r"^ensure_machine_id\(\) \{\n.*?^\} 2>/dev/null\n", SESSION.read_text(), re.M | re.S) + script = match.group(0).replace("/etc/machine-id", str(self.file)) + "ensure_machine_id\n" + subprocess.run(["bash", "-c", script], env={"PATH": "%s:/usr/bin:/bin" % self.bin}, timeout=30, check=True) + return self.file.read_text() if self.file.exists() else None + + def test_the_empty_id_shipped_in_the_runtime_gets_a_fresh_one(self): + self.file.write_text("") + self.generator(FRESH) + self.assertEqual(FRESH + "\n", self.ensure()) + self.assertEqual([self.file.name], [p.name for p in self.root.iterdir() if p.is_file()]) + + def test_a_missing_or_malformed_id_is_replaced(self): + self.generator(FRESH) + self.assertEqual(FRESH + "\n", self.ensure()) + self.file.write_text("uninitialized\n") + self.assertEqual(FRESH + "\n", self.ensure()) + self.file.write_text("fedcba9876543210fedcba9876543210\nextra\n") + self.assertEqual(FRESH + "\n", self.ensure()) + self.file.write_text("0" * 32 + "\n") + self.assertEqual(FRESH + "\n", self.ensure()) + + def test_a_valid_id_is_never_changed(self): + self.file.write_text("fedcba9876543210fedcba9876543210\n") + self.generator(FRESH) + self.assertEqual("fedcba9876543210fedcba9876543210\n", self.ensure()) + + def test_without_a_usable_generator_the_file_is_left_alone(self): + self.file.write_text("") + self.generator(FRESH, status=1) + self.assertEqual("", self.ensure()) + self.generator("not-an-id") + self.assertEqual("", self.ensure()) + + +if __name__ == "__main__": + unittest.main() diff --git a/tools/tests/test_steam_cold_launch.py b/tools/tests/test_steam_cold_launch.py index 1695ad0e16..0fe085ff9e 100644 --- a/tools/tests/test_steam_cold_launch.py +++ b/tools/tests/test_steam_cold_launch.py @@ -41,6 +41,7 @@ class SteamColdLaunchTest(unittest.TestCase): "#!/bin/sh\n" "printf '%s\n' \"$@\" > \"$AGENT_ARGS\"\n" "n=$(cat \"$AGENT_COUNT\" 2>/dev/null || echo 0); n=$((n + 1)); echo $n > \"$AGENT_COUNT\"\n" + "[ \"$1\" = --steam-ready ] && exit \"${READY_STATUS:-0}\"\n" "exit \"$AGENT_STATUS\"\n") agent.chmod(0o755) sleep = self.bin / "sleep" @@ -55,12 +56,13 @@ class SteamColdLaunchTest(unittest.TestCase): pgrep.chmod(0o755) self.block = LAUNCH_BLOCK.replace("/usr/local/bin/droiddeck-agent", str(agent)) - def run_launch(self, target, focus="769", helper_status=0, running_after=999): + def run_launch(self, target, focus="769", helper_status=0, running_after=999, ready_status=0): env = dict(os.environ, HOME=str(self.root), BL_LAUNCH_DIR=str(self.launch_dir), PATH=str(self.bin) + os.pathsep + os.environ["PATH"], AGENT_ARGS=str(self.agent_args), SLEEP_ARGS=str(self.sleep_args), AGENT_COUNT=str(self.agent_count), AGENT_STATUS=str(helper_status), - FOCUS=focus, FOCUS_COUNT=str(self.focus_count), RUNNING_AFTER=str(running_after)) + FOCUS=focus, FOCUS_COUNT=str(self.focus_count), RUNNING_AFTER=str(running_after), + READY_STATUS=str(ready_status)) script = self.block + "\nprintf 'ARG:%s\n' \"$@\"\nwait\n" url = target if target.startswith("steam://") else "steam://rungameid/" + target result = subprocess.run(["bash", "-c", script, "cold-launch", url], @@ -72,28 +74,40 @@ class SteamColdLaunchTest(unittest.TestCase): return [l for l in result.stdout.splitlines() if "launching the requested game" in l] def test_a_shortcut_that_takes_focus_is_handed_over_once_without_waiting_for_the_overview(self): - result = self.run_launch(SHORTCUT, focus="769 769 4242") + result = self.run_launch(SHORTCUT, focus="769 4242") self.assertEqual(SHORTCUT, (self.launch_dir / "steam-game").read_text().strip()) self.assertEqual(1, len(self.hand_offs(result)), result.stdout) - self.assertFalse(self.agent_args.exists(), result.stdout) - self.assertEqual(["1"], self.sleep_args.read_text().split()) + self.assertEqual(["--steam-ready"], self.agent_args.read_text().splitlines()) + self.assertFalse(self.sleep_args.exists()) def test_a_shortcut_that_never_takes_focus_is_handed_over_again_after_the_overview_wait(self): result = self.run_launch(SHORTCUT, focus="769", helper_status=0) - self.assertEqual(2, len(self.hand_offs(result)), result.stdout) - self.assertIn("launching the requested game again", result.stdout) - self.assertEqual(["--wait-steam-shortcut-overview", SHORTCUT, "45"], self.agent_args.read_text().splitlines()) + self.assertEqual(1, len(self.hand_offs(result)), result.stdout) + self.assertIn("clearing the pending launch interstitial", result.stdout) + self.assertEqual(["--steam-clear-pending-interstitial", "3551182975"], self.agent_args.read_text().splitlines()) self.assertEqual(["1"] * 20, self.sleep_args.read_text().split()) def test_a_timed_out_overview_still_gets_one_more_hand_off(self): result = self.run_launch(SHORTCUT, focus="769", helper_status=1) self.assertEqual(2, len(self.hand_offs(result)), result.stdout) self.assertIn("overview status 1", result.stdout) + self.assertIn("launching the requested game again", result.stdout) + + def test_a_steam_that_never_reports_ready_still_gets_the_launch(self): + result = self.run_launch("413150", ready_status=1) + self.assertEqual("413150", (self.launch_dir / "steam-game").read_text().strip()) + self.assertEqual(1, len(self.hand_offs(result)), result.stdout) + self.assertEqual(["1"] * 30, self.sleep_args.read_text().split()) + + def test_a_focus_change_does_not_end_the_readiness_wait(self): + result = self.run_launch("413150", focus="769 4242", ready_status=1) + self.assertEqual(1, len(self.hand_offs(result)), result.stdout) + self.assertEqual(["1"] * 30, self.sleep_args.read_text().split()) def test_a_game_still_starting_without_a_window_is_not_launched_twice(self): result = self.run_launch(SHORTCUT, running_after=1) self.assertEqual(1, len(self.hand_offs(result)), result.stdout) - self.assertFalse(self.agent_args.exists()) + self.assertIn("--steam-ready", self.agent_args.read_text()) self.assertEqual(["-f", "reaper SteamLaunch AppId=3551182975( |$)"], (self.root / "pgrep-args").read_text().splitlines()) self.assertIn("already starting", result.stdout) @@ -108,15 +122,15 @@ class SteamColdLaunchTest(unittest.TestCase): focus = " ".join(["769"] * 23) + " 4242" result = self.run_launch(SHORTCUT, focus=focus, helper_status=0) self.assertEqual(1, len(self.hand_offs(result)), result.stdout) - self.assertIn("took focus while waiting", result.stdout) + self.assertNotIn("clearing the pending launch interstitial", result.stdout) self.assertNotIn("launching the requested game again", result.stdout) def test_native_app_id_keeps_the_short_delay_and_is_handed_over_once(self): result = self.run_launch("413150") self.assertEqual("413150", (self.launch_dir / "steam-game").read_text().strip()) self.assertEqual(1, len(self.hand_offs(result)), result.stdout) - self.assertEqual("4", self.sleep_args.read_text().strip()) - self.assertFalse(self.agent_args.exists()) + self.assertFalse(self.sleep_args.exists()) + self.assertIn("--steam-ready", self.agent_args.read_text()) def test_a_restart_for_a_game_uses_the_same_hand_off(self): loop = SOURCE[SOURCE.index(' if [ -s "$HOME/.bl-restart-game" ]; then'):SOURCE.index(' if [ -f "$HOME/.bl-restart-once" ]; then')] diff --git a/tools/tests/test_steam_compat_live.py b/tools/tests/test_steam_compat_live.py index af2d8cfb62..15ae63bb89 100644 --- a/tools/tests/test_steam_compat_live.py +++ b/tools/tests/test_steam_compat_live.py @@ -749,6 +749,54 @@ class ReconcileTest(unittest.TestCase): self.assertIn(entry % ("10", COMPAT["FEX_TOOL"]), " ".join(config.read_text().split())) self.assertEqual(self.games(), {}) + def test_our_default_the_client_does_not_list_survives_to_the_next_start(self): + build = "DroidDeck-Proton 11-11.4" + (self.steam / "compatibilitytools.d" / build).mkdir(parents=True) + (self.steam / "compatibilitytools.d" / build / COMPAT["EXTRA_WRAPPER"]).write_text("") + (self.state / "state.json").write_text(json.dumps({"default": build, "written": build, "source": "update", "auto": {"10": build}})) + page = FakePage({"0": TOOL, "10": build}, [TOOL, TOOL_11]) + state = self.reconcile(page) + self.assertEqual((state["default"], state["source"], state["written"]), (build, "update", TOOL)) + self.assertEqual(page.sets, [("10", TOOL), ("11", TOOL)]) + config = self.steam / "config/config.vdf" + config.parent.mkdir(parents=True) + vdf = '"InstallConfigStore" { "Software" { "Valve" { "Steam" { "CompatToolMapping" { %s } } } } }' + entry = '"%s" { "name" "%s" "config" "" "priority" "250" }' + config.write_text(vdf % "".join(entry % (app, page.mapping[app]) for app in ("0", "10", "11"))) + with mock.patch.dict(COMPAT["register_default"].__globals__, steam_running=lambda: False): + default = COMPAT["register_default"](str(config), ["10", "11"], catalog=COMPAT["tool_catalog"](str(self.steam)), + directory=str(self.state), settle=True) + self.assertEqual(default, build) + self.assertEqual(COMPAT["read_config_mapping"](str(config)), {"0": build, "10": build, "11": build}) + + def test_proton_experimental_picked_for_a_game_in_steam_is_kept_and_mirrored(self): + build = "DroidDeck-Proton 11-12.3" + (self.steam / "compatibilitytools.d" / build).mkdir(parents=True) + (self.steam / "compatibilitytools.d" / build / COMPAT["EXTRA_WRAPPER"]).write_text("") + (self.state / "state.json").write_text(json.dumps( + {"default": build, "written": build, "source": "update", "auto": {"11": build}, "picked": ["10", "11"], "tools": [], + "games_applied": {"10": {"valve": False, "dir": build}}})) + self.write_games({"10": {"valve": False, "dir": build}}) + page = FakePage({"0": build, "10": "proton_experimental", "11": build}, [TOOL, TOOL_11, build, "proton_experimental"]) + state = self.reconcile(page) + self.assertEqual(page.sets, [("10", TOOL)]) + self.assertEqual(state["auto"], {"11": build}) + self.assertEqual(self.games(), {"10": {"valve": True, "dir": COMPAT["SOURCES"][0]}}) + config = self.steam / "config/config.vdf" + config.parent.mkdir(parents=True) + vdf = '"InstallConfigStore" { "Software" { "Valve" { "Steam" { "CompatToolMapping" { %s } } } } }' + entry = '"%s" { "name" "%s" "config" "" "priority" "250" }' + config.write_text(vdf % "".join(entry % (app, page.mapping[app]) for app in ("0", "10", "11"))) + with mock.patch.dict(COMPAT["register_default"].__globals__, steam_running=lambda: False): + default = COMPAT["register_default"](str(config), ["10", "11"], catalog=COMPAT["tool_catalog"](str(self.steam)), + directory=str(self.state), settle=True) + self.assertEqual(default, build) + self.assertEqual(COMPAT["read_config_mapping"](str(config)), {"0": build, "10": TOOL, "11": build}) + page.sets.clear() + self.write_games({"10": {"valve": True, "dir": COMPAT["SOURCES"][1]}}) + self.reconcile(page) + self.assertEqual(page.sets, [("10", TOOL_11)]) + def test_file_mode_defers_to_the_live_helper(self): (self.state / "live.pid").write_text("%d\n" % os.getpid()) self.assertTrue(COMPAT["live_helper_running"]()) diff --git a/tools/tests/test_steam_tool_platforms.py b/tools/tests/test_steam_tool_platforms.py new file mode 100644 index 0000000000..899b0b07e9 --- /dev/null +++ b/tools/tests/test_steam_tool_platforms.py @@ -0,0 +1,215 @@ +import json +import os +from pathlib import Path +import select +import tempfile +import unittest +from unittest import mock + +from tools.tests import test_droiddeck_fex as fixtures +from tools.tests import test_steam_compat_live as live +from tools.tests.test_steam_compat_mapping import config_text + + +COMPAT = live.COMPAT +TOOL = COMPAT["TOOL"] + + +class ToolPlatformsTest(unittest.TestCase): + def setUp(self): + temporary = tempfile.TemporaryDirectory() + self.addCleanup(temporary.cleanup) + self.root = Path(temporary.name) + self.steam = self.root / "Steam" + self.state = self.root / "state" + self.config = self.steam / "config/config.vdf" + self.config.parent.mkdir(parents=True) + self.state.mkdir() + (self.steam / "appcache").mkdir() + (self.steam / "steamapps").mkdir() + self.entries = { + 976730: ("windows", "64", "game"), + 1826330: ("linux", "", "tool"), + 1161040: ("linux", "", "tool"), + 900001: ("windows", "64", "tool"), + 900002: ("linux", "arm64", "tool"), + 900003: ("osx", "64", "tool"), + 900004: ("", "", "tool"), + 900005: ("linux,windows", "64", "tool"), + } + self.write_appinfo() + self.catalog = [{"name": TOOL, "display": "Proton", "dir": "Proton", "valve": True}] + self.config.write_text(config_text('"0" { "name" "%s" }' % TOOL)) + patcher = mock.patch.dict(COMPAT["register_default"].__globals__, COMPAT_DIR=str(self.state)) + patcher.start() + self.addCleanup(patcher.stop) + + def write_appinfo(self, magic=0x07564429): + (self.steam / "appcache/appinfo.vdf").write_bytes(fixtures.AppInfoTest.appinfo(self.entries, magic)) + + def platforms(self): + return COMPAT["tool_platforms"](str(self.steam)) + + def plan(self, current, auto=None, **kwargs): + return COMPAT["plan_mapping"](current, ["976730"], [TOOL], TOOL, auto or {}, + tools=self.platforms(), **kwargs) + + def test_metadata_selects_tools_across_cache_formats(self): + for magic in (0x07564427, 0x07564428, 0x07564429): + with self.subTest(magic=magic): + self.write_appinfo(magic) + self.assertEqual(set(self.platforms()), {"1826330", "1161040", "900001", "900002", "900005"}) + + def test_tools_are_not_installed_games(self): + for app in self.entries: + (self.steam / ("steamapps/appmanifest_%s.acf" % app)).write_text("") + self.assertEqual(COMPAT["installed_apps"](str(self.steam)), ["976730"]) + + def test_linux_tools_clear_proton_and_windows_tools_get_default_before_install(self): + current = {"0": TOOL, "976730": TOOL, "1826330": TOOL, "1161040": "proton_experimental"} + changes, auto = self.plan(current, {"1826330": TOOL, "976730": TOOL}) + self.assertEqual(changes, {"1826330": None, "1161040": None, "900001": TOOL}) + self.assertNotIn("1826330", auto) + self.assertEqual(auto["900001"], TOOL) + self.assertEqual(auto["976730"], TOOL) + + def test_removed_ge_proton_is_cleared_with_or_without_saved_ownership(self): + for saved in ({}, {"1826330": "GE-Proton11-7"}): + with self.subTest(saved=saved): + changes, auto = self.plan({"0": TOOL, "1826330": "GE-Proton11-7"}, saved) + self.assertIsNone(changes["1826330"]) + self.assertNotIn("1826330", auto) + + def test_removed_ge_proton_game_selection_survives(self): + changes, auto = self.plan({"0": TOOL, "976730": "GE-Proton11-7"}) + self.assertNotIn("976730", changes) + self.assertNotIn("976730", auto) + + def test_saved_native_selections_survive(self): + for name in (COMPAT["FEX_TOOL"], "custom-native"): + with self.subTest(name=name): + changes, auto = self.plan({"0": TOOL, "1826330": name}, {"1826330": name}) + self.assertNotIn("1826330", changes) + self.assertNotIn("1826330", auto) + + def test_main_watches_cache_updates_and_replacements(self): + class StopWatching(BaseException): + pass + + watchers = [] + + def capture(files): + watcher = live.LIVE["Watcher"](files) + watchers.append(watcher) + raise StopWatching() + + namespace = live.LIVE["main"].__globals__ + with mock.patch.dict(namespace, Watcher=capture), mock.patch.dict(os.environ, BL_CDP_PORT="1"), \ + mock.patch.object(live.LIVE["sys"], "argv", ["compat", str(self.steam)]): + with self.assertRaises(StopWatching): + live.LIVE["main"]() + watcher = watchers[0] + if watcher.fd >= 0: + self.addCleanup(os.close, watcher.fd) + self.assertFalse(watcher.changed()) + self.entries[900006] = ("linux", "", "tool") + self.write_appinfo() + self.assertTrue(watcher.changed()) + self.assertIn("900006", self.platforms()) + cache = self.steam / "appcache/appinfo.vdf" + replacement = cache.with_suffix(".tmp") + replacement.write_bytes(cache.read_bytes()) + replacement.replace(cache) + self.assertTrue(watcher.changed()) + + def test_watcher_follows_an_appcache_created_after_startup(self): + root = self.root / "late" + (root / "state").mkdir(parents=True) + watcher = live.LIVE["Watcher"]([str(root / "state/request.json"), str(root / "appcache/appinfo.vdf")]) + self.assertGreaterEqual(watcher.fd, 0) + self.addCleanup(os.close, watcher.fd) + self.assertFalse(watcher.changed()) + (root / "appcache").mkdir() + self.assertTrue(watcher.changed()) + (root / "appcache/appinfo.vdf").write_bytes(b"x") + self.assertTrue(watcher.changed()) + + def test_watcher_follows_each_missing_directory_level(self): + root = self.root / "levels" + root.mkdir() + target = root / "sd/steamapps/appmanifest_1.acf" + watcher = live.LIVE["Watcher"]([str(target)]) + self.assertGreaterEqual(watcher.fd, 0) + self.addCleanup(os.close, watcher.fd) + (root / "sd").mkdir() + self.assertTrue(select.select([watcher.fd], [], [], 1)[0]) + self.assertFalse(watcher.changed()) + (root / "sd/steamapps").mkdir() + self.assertTrue(select.select([watcher.fd], [], [], 1)[0]) + self.assertTrue(watcher.changed()) + target.write_bytes(b"x") + self.assertTrue(watcher.changed()) + + def test_linux_tool_requests_cannot_restore_windows_platform(self): + changes, auto = self.plan({"0": TOOL, "1826330": TOOL}, overrides={"1826330": TOOL}) + self.assertIsNone(changes["1826330"]) + self.assertNotIn("1826330", auto) + + def test_linux_execution_tools_and_custom_selections_survive(self): + current = {"0": TOOL, "1826330": COMPAT["FEX_TOOL"], "900002": "custom-native", + "900001": "custom-windows", "900005": TOOL} + changes, auto = self.plan(current) + self.assertEqual(changes, {"976730": TOOL}) + self.assertNotIn("1826330", auto) + + def test_missing_or_malformed_cache_does_not_invent_platforms(self): + cache = self.steam / "appcache/appinfo.vdf" + cache.unlink() + self.assertEqual(self.platforms(), {}) + cache.write_bytes(b"invalid") + self.assertEqual(self.platforms(), {}) + + def test_offline_repair_is_idempotent_and_preserves_game_and_neighbor_fields(self): + self.config.write_text(config_text( + '"0" { "name" "%s" } "976730" { "name" "custom-game" } ' + '"1826330" { "name" "%s" }' % (TOOL, TOOL))) + for _ in range(2): + COMPAT["register_default"](str(self.config), ["976730"], catalog=self.catalog, + directory=str(self.state), default=TOOL) + self.assertEqual(COMPAT["read_config_mapping"](str(self.config)), + {"0": TOOL, "976730": "custom-game", "900001": TOOL}) + self.assertIn('"outside"', self.config.read_text()) + state = json.loads((self.state / "state.json").read_text()) + self.assertNotIn("1826330", state["auto"]) + + def reconcile(self, page): + helper = live.LIVE["Helper"](str(self.steam), 1) + helper.page = page + with mock.patch.dict(COMPAT, tool_catalog=lambda root: self.catalog): + helper.reconcile() + return json.loads((self.state / "state.json").read_text()) + + def test_live_repair_covers_uninstalled_dependencies_and_stays_idempotent(self): + page = live.FakePage({"0": TOOL, "1826330": TOOL, "976730": "custom-game"}, [TOOL]) + state = self.reconcile(page) + self.assertEqual(page.sets, [("900001", TOOL), ("1826330", "")]) + self.assertNotIn("1826330", state["auto"]) + page.sets.clear() + self.reconcile(page) + self.assertEqual(page.sets, []) + self.assertEqual(page.mapping["976730"], "custom-game") + + def test_live_removed_ge_proton_is_cleared_and_stays_cleared(self): + page = live.FakePage({"0": TOOL, "1826330": "GE-Proton11-7"}, [TOOL]) + (self.state / "state.json").write_text(json.dumps({"auto": {"1826330": "GE-Proton11-7"}})) + state = self.reconcile(page) + self.assertIn(("1826330", ""), page.sets) + self.assertNotIn("1826330", state["auto"]) + page.sets.clear() + self.reconcile(page) + self.assertEqual(page.sets, []) + + def test_live_refused_tools_are_not_written(self): + page = live.FakePage({"0": TOOL, "1826330": TOOL}, [TOOL], refused={"1826330", "900001"}) + self.reconcile(page) + self.assertEqual(page.sets, []) diff --git a/tools/tests/test_store_launch.py b/tools/tests/test_store_launch.py index 7ea5a1ae79..36a1b7c9a1 100644 --- a/tools/tests/test_store_launch.py +++ b/tools/tests/test_store_launch.py @@ -387,6 +387,193 @@ def set_registry(**arguments): return {'install': {'action': 'setRegistry', 'arguments': arguments}, 'name': 'setRegistry'} +def support_data(**arguments): + return {'install': {'action': 'supportData', 'arguments': arguments}, 'name': 'supportData'} + + +class GogSupportData(unittest.TestCase): + def prefix(self, root): + user = Path(root) / 'compat/pfx/drive_c/users/steamuser' + user.mkdir(parents=True) + return user + + def test_support_files_reach_the_prefix_once(self): + with tempfile.TemporaryDirectory() as root: + folder = game(root, store_id='gog', ident='9') + user = self.prefix(root) + support = folder / 'support/9' + (support / 'Docs').mkdir(parents=True) + (support / 'Docs/Fallout4.ini').write_text('[Archive]\n') + (support / 'Plugins.txt').write_text('*LondonWorldSpace.esm\n') + (folder / '__AppData/Saves').mkdir(parents=True) + (folder / '__AppData/Saves/a.txt').write_text('a') + gog_script(folder, '9', [ + set_registry(root='HKLM', subkey='Software\\Game', valueName='a', valueData='b', valueType='string'), + support_data(type='folder', target='{userdocs}/My Games/Fallout4'), + support_data(type='file', overwrite=True, source='{supportDir}/docs/Fallout4.ini', + target='{userdocs}/My Games/Fallout4/Fallout4.ini'), + support_data(type='file', overwrite=True, source='{supportDir}/Plugins.txt', + target='{localappdata}/Fallout4/Plugins.txt'), + support_data(type='folder', overwrite=True, source='{app}/__AppData', target='{localappdata}/Fallout4'), + ]) + with patch.object(store, 'create_prefix') as create: + self.assertEqual('copied=3', store.gog_support_data(folder, root + '/compat', '/proton')) + create.assert_not_called() + self.assertTrue((user / 'Documents/My Games/Fallout4').is_dir()) + self.assertEqual('[Archive]\n', (user / 'Documents/My Games/Fallout4/Fallout4.ini').read_text()) + self.assertEqual('*LondonWorldSpace.esm\n', (user / 'AppData/Local/Fallout4/Plugins.txt').read_text()) + self.assertEqual('a', (user / 'AppData/Local/Fallout4/Saves/a.txt').read_text()) + (user / 'AppData/Local/Fallout4/Plugins.txt').write_text('player order') + self.assertEqual('ready', store.gog_support_data(folder, root + '/compat', '/proton')) + self.assertEqual('player order', (user / 'AppData/Local/Fallout4/Plugins.txt').read_text()) + + def test_a_source_the_build_lacks_does_not_recopy_the_rest_every_launch(self): + with tempfile.TemporaryDirectory() as root: + folder = game(root, store_id='gog', ident='9') + user = self.prefix(root) + (folder / 'support/9').mkdir(parents=True) + (folder / 'support/9/Plugins.txt').write_text('x') + gog_script(folder, '9', [ + support_data(type='file', overwrite=True, source='{supportDir}/Plugins.txt', target='{localappdata}/G/Plugins.txt'), + support_data(type='folder', overwrite=True, source='{app}/__AppData', target='{localappdata}/G'), + ]) + self.assertEqual('copied=1 missing=1', store.gog_support_data(folder, root + '/compat', '/proton')) + (user / 'AppData/Local/G/Plugins.txt').write_text('player order') + self.assertEqual('ready', store.gog_support_data(folder, root + '/compat', '/proton')) + self.assertEqual('player order', (user / 'AppData/Local/G/Plugins.txt').read_text()) + + def test_a_link_never_carries_a_copy_outside(self): + with tempfile.TemporaryDirectory() as root: + folder = game(root, store_id='gog', ident='9') + user = self.prefix(root) + outside = Path(root) / 'outside' + outside.mkdir() + (outside / 'secret.ini').write_text('s') + (folder / '__AppData').mkdir() + (folder / '__AppData/linked').symlink_to(outside) + (folder / 'support/9').mkdir(parents=True) + (folder / 'support/9/a.ini').write_text('a') + gog_script(folder, '9', [ + support_data(type='folder', overwrite=True, source='{app}/__AppData', target='{localappdata}/G'), + support_data(type='file', overwrite=True, source='{supportDir}/a.ini', target='{localappdata}/G/linked/a.ini'), + support_data(type='file', overwrite=True, source='{app}/__AppData/linked/secret.ini', target='{userdocs}/secret.ini'), + ]) + self.assertEqual('copied=1 skipped=2', store.gog_support_data(folder, root + '/compat', '/proton')) + self.assertTrue((user / 'AppData/Local/G/linked').is_symlink()) + self.assertEqual(['secret.ini'], sorted(p.name for p in outside.iterdir())) + self.assertFalse((user / 'Documents/secret.ini').exists()) + + def test_a_linked_source_folder_is_copied_as_a_link(self): + with tempfile.TemporaryDirectory() as root: + folder = game(root, store_id='gog', ident='9') + user = self.prefix(root) + outside = Path(root) / 'outside' + outside.mkdir() + (outside / 'secret.ini').write_text('s') + (folder / 'linked').symlink_to(outside) + gog_script(folder, '9', [support_data(type='folder', overwrite=True, source='{app}/linked', target='{userdocs}/G')]) + store.gog_support_data(folder, root + '/compat', '/proton') + self.assertTrue((user / 'Documents/G').is_symlink()) + self.assertEqual(outside, Path(os.readlink(user / 'Documents/G'))) + + def test_a_players_own_links_stay_without_overwrite(self): + with tempfile.TemporaryDirectory() as root: + folder = game(root, store_id='gog', ident='9') + user = self.prefix(root) + (folder / 'support/9/Saves').mkdir(parents=True) + (folder / 'support/9/a.ini').write_text('a') + (folder / 'support/9/Saves/b.ini').write_text('b') + (user / 'Documents/G').mkdir(parents=True) + (user / 'Documents/a.ini').symlink_to(user / 'mine.ini') + (user / 'Documents/G/b.ini').symlink_to(user / 'gone.ini') + gog_script(folder, '9', [ + support_data(type='file', source='{supportDir}/a.ini', target='{userdocs}/a.ini'), + support_data(type='folder', source='{supportDir}/Saves', target='{userdocs}/G'), + ]) + self.assertEqual('copied=0', store.gog_support_data(folder, root + '/compat', '/proton')) + self.assertEqual(user / 'mine.ini', Path(os.readlink(user / 'Documents/a.ini'))) + self.assertEqual(user / 'gone.ini', Path(os.readlink(user / 'Documents/G/b.ini'))) + + def test_a_changed_script_applies_again(self): + with tempfile.TemporaryDirectory() as root: + folder = game(root, store_id='gog', ident='9') + user = self.prefix(root) + (folder / 'support/9').mkdir(parents=True) + (folder / 'support/9/a.ini').write_text('1') + action = support_data(type='file', overwrite=True, source='{supportDir}/a.ini', target='{userdocs}/a.ini') + gog_script(folder, '9', [action]) + self.assertEqual('copied=1', store.gog_support_data(folder, root + '/compat', '/proton')) + (folder / 'support/9/b.ini').write_text('2') + gog_script(folder, '9', [action, support_data(type='file', overwrite=True, source='{supportDir}/b.ini', + target='{userdocs}/b.ini')]) + self.assertEqual('copied=2', store.gog_support_data(folder, root + '/compat', '/proton')) + self.assertEqual('2', (user / 'Documents/b.ini').read_text()) + + def test_a_players_file_is_kept_unless_the_script_overwrites_it(self): + with tempfile.TemporaryDirectory() as root: + folder = game(root, store_id='gog', ident='9') + user = self.prefix(root) + (folder / 'support/9').mkdir(parents=True) + (folder / 'support/9/a.ini').write_text('default') + (user / 'Documents').mkdir() + (user / 'Documents/a.ini').write_text('mine') + gog_script(folder, '9', [support_data(type='file', source='{supportDir}/a.ini', target='{userdocs}/a.ini')]) + self.assertEqual('copied=0', store.gog_support_data(folder, root + '/compat', '/proton')) + self.assertEqual('mine', (user / 'Documents/a.ini').read_text()) + + def test_paths_outside_the_known_folders_are_refused(self): + with tempfile.TemporaryDirectory() as root: + folder = game(root, store_id='gog', ident='9') + self.prefix(root) + (folder / 'support/9').mkdir(parents=True) + (folder / 'support/9/a.ini').write_text('x') + gog_script(folder, '9', [ + support_data(type='file', overwrite=True, source='{supportDir}/a.ini', target='{userdocs}/../../../../escape.ini'), + support_data(type='file', overwrite=True, source='{supportDir}/a.ini', target='C:/escape.ini'), + support_data(type='folder', target='{windir}/x'), + ]) + self.assertEqual('copied=0 skipped=3', store.gog_support_data(folder, root + '/compat', '/proton')) + self.assertEqual([], list(Path(root).rglob('escape.ini'))) + self.assertFalse((Path(root) / 'compat/pfx' / store.GOG_SUPPORT_MARK).exists()) + + def test_links_in_a_support_folder_are_copied_as_links(self): + with tempfile.TemporaryDirectory() as root: + folder = game(root, store_id='gog', ident='9') + user = self.prefix(root) + outside = Path(root) / 'outside' + outside.mkdir() + (outside / 'secret.txt').write_text('s') + (folder / '__AppData').mkdir() + (folder / '__AppData/linked').symlink_to(outside, target_is_directory=True) + gog_script(folder, '9', [support_data(type='folder', overwrite=True, source='{app}/__AppData', target='{localappdata}/G')]) + self.assertEqual('copied=1', store.gog_support_data(folder, root + '/compat', '/proton')) + self.assertTrue((user / 'AppData/Local/G/linked').is_symlink()) + self.assertEqual('s', (outside / 'secret.txt').read_text()) + + def test_without_support_data_no_prefix_is_made(self): + with tempfile.TemporaryDirectory() as root: + folder = game(root, store_id='gog', ident='9') + gog_script(folder, '9', [set_registry(root='HKLM', subkey='Software\\Game', valueName='a', valueData='b', valueType='string')]) + with patch.object(store, 'create_prefix') as create: + self.assertEqual('none', store.gog_support_data(folder, root + '/compat', '/proton')) + create.assert_not_called() + + def test_a_missing_prefix_is_made_first(self): + with tempfile.TemporaryDirectory() as root: + folder = game(root, store_id='gog', ident='9') + gog_script(folder, '9', [support_data(type='folder', target='{userdocs}/My Games/G')]) + with patch.object(store, 'create_prefix', return_value='failed') as create: + self.assertEqual('no-prefix', store.gog_support_data(folder, root + '/compat', '/proton')) + create.assert_called_once_with(root + '/compat', '/proton') + + def made(compat_data, proton): + self.prefix(root) + return 'created' + with patch.object(store, 'create_prefix', side_effect=made): + self.assertEqual('copied=0', store.gog_support_data(folder, root + '/compat', '/proton')) + self.assertTrue((Path(root) / 'compat/pfx/drive_c/users/steamuser/Documents/My Games/G').is_dir()) + + class GogRegistry(unittest.TestCase): def test_patch_registration_uses_authentic_build_and_the_prefix_registry_executable(self): with tempfile.TemporaryDirectory() as root: @@ -605,6 +792,107 @@ class GogRegistry(unittest.TestCase): folder = game(root, store_id='gog') with patch.dict(os.environ, {'BL_LAUNCH_DIR': root, 'STEAM_COMPAT_DATA_PATH': root + '/compat/', 'BL_STORE_PROTON': '/chosen/proton'}), \ patch.object(store, 'prepare_gog', return_value=False), patch.object(store, 'ask', return_value=None), \ - patch.object(store, 'apply_gog_registry', return_value='ready') as apply: + patch.object(store, 'apply_gog_registry', return_value='ready') as apply, \ + patch.object(store, 'gog_support_data', return_value='ready') as support: store.main([str(folder / store.LAUNCHER)]) apply.assert_called_once_with(folder, root + '/compat', '/chosen/proton') + support.assert_called_once_with(folder, root + '/compat', '/chosen/proton') + + +class Ubisoft(unittest.TestCase): + def setUp(self): + self.tmp = tempfile.TemporaryDirectory() + self.root = Path(self.tmp.name) + self.compat_data = self.root / 'compatdata/2929927072' + (self.compat_data / 'pfx').mkdir(parents=True) + (self.compat_data / 'pfx/system.reg').write_text('WINE REGISTRY Version 2\n') + (self.compat_data / 'pfx/user.reg').write_text('WINE REGISTRY Version 2\n') + self.folder = self.root / 'Games/Stores/Ubisoft/Splinter Cell' + (self.folder / 'system').mkdir(parents=True) + self.sidecar = {'store': 'ubisoft', 'id': 'space-1', 'exe': 'system/SplinterCell.exe', 'extra': {'productId': '109'}} + service = self.root / 'droiddeck-upc.exe' + service.write_bytes(b'MZ') + for patcher in (patch.object(store, 'UBISOFT_SERVICE', service), patch.object(store, 'UBISOFT_CACHE', self.root / 'cache'), + patch.object(store, 'wait_for_server', return_value=True)): + patcher.start() + self.addCleanup(patcher.stop) + + def tearDown(self): + self.tmp.cleanup() + + def runtime(self): + folder = store.ubisoft_runtime_dir(self.compat_data) + folder.mkdir(parents=True, exist_ok=True) + for name in store.UBISOFT_RUNTIME: + (folder / name).write_bytes(b'MZ') + + def answer(self, *_args, **_kwargs): + return {'ok': True, 'ticket': 'PC', 'owned': [82, 109], 'appId': 'app', + 'account': {'userId': 'u1', 'username': 'name', 'nameOnPlatform': 'name', 'email': 'e@x', 'country': 'US'}} + + def prepare(self, ask_app): + return store.prepare_ubisoft(self.folder, self.sidecar, str(self.compat_data), str(self.root / 'launch'), '/proton', ask_app=ask_app) + + def test_an_extracted_runtime_is_not_extracted_again(self): + self.runtime() + with patch.object(store.subprocess, 'run') as run: + self.assertEqual('present', store.install_runtime(str(self.compat_data))) + run.assert_not_called() + + def test_the_runtime_comes_from_the_installer_through_7z(self): + installer = self.root / 'UbisoftConnectInstaller.exe' + + def run(command, **_kwargs): + self.assertEqual(['7z', 'e', '-y'], command[:3]) + self.assertEqual(str(installer), command[4]) + out = Path(command[3][2:]) + for name in command[5:]: + (out / name).write_bytes(b'MZ') + + (self.root / 'cache').mkdir() + with patch.object(store, 'fetch_installer', return_value=installer), patch.object(store.subprocess, 'run', side_effect=run): + self.assertEqual('installed', store.install_runtime(str(self.compat_data))) + self.assertTrue(store.ubisoft_runtime_ready(str(self.compat_data))) + + def test_a_launch_registers_the_game_and_writes_the_session_for_the_service(self): + self.runtime() + result = self.prepare(self.answer) + self.assertEqual('runtime present, registered 109, 0 manifest value(s), session ready', result) + reg = (self.compat_data / 'pfx/system.reg').read_text() + for view in ('[Software\\\\Ubisoft\\\\Launcher]', '[Software\\\\Wow6432Node\\\\Ubisoft\\\\Launcher]'): + self.assertIn(view, reg) + self.assertIn('[Software\\\\Wow6432Node\\\\Ubisoft\\\\Launcher\\\\Installs\\\\109]', reg) + self.assertIn('"InstallDir"="C:\\\\Program Files (x86)\\\\Ubisoft\\\\Ubisoft Game Launcher\\\\"', reg) + launch = json.loads((self.folder / store.UBISOFT_LAUNCH).read_text()) + self.assertEqual(109, launch['productId']) + self.assertEqual('PC', launch['ticket']) + self.assertEqual([82, 109], launch['owned']) + self.assertEqual(store.windows_path(str(self.folder / 'system/SplinterCell.exe')), launch['exe']) + self.assertEqual('C:/users/steamuser/Saved Games/Ubisoft/u1/109/', launch['saves']) + self.assertTrue((self.compat_data / 'pfx/drive_c/users/steamuser/Saved Games/Ubisoft/u1/109').is_dir()) + + def test_without_a_session_the_launch_file_is_removed_and_the_game_starts_as_is(self): + self.runtime() + (self.folder / store.UBISOFT_LAUNCH).write_text('{}') + result = self.prepare(lambda *_a, **_k: {'ok': False, 'reason': 'signed-out'}) + self.assertEqual('runtime present, registered 109, session signed-out', result) + self.assertFalse((self.folder / store.UBISOFT_LAUNCH).exists()) + + def test_a_game_without_a_product_is_left_alone(self): + self.sidecar['extra'] = {} + self.assertEqual('no-product', self.prepare(self.answer)) + + def test_the_manifest_registry_lands_in_both_views_with_the_install_folder_and_language(self): + self.runtime() + registry = [{'key': r'HKEY_LOCAL_MACHINE\SOFTWARE\Ubisoft\Launcher\Installs\109\Language', + 'values': [{'name': 'French', 'value': 'fr-FR'}, {'name': 'English', 'value': 'en-US'}]}, + {'key': r'HKEY_LOCAL_MACHINE\SOFTWARE\Ubisoft\Splinter Cell\GamePath', 'values': [{'name': '$INSTALL_DIR', 'value': ''}]}] + answer = dict(self.answer(), registry=registry, language='en-US') + result = self.prepare(lambda *_a, **_k: answer) + self.assertEqual('runtime present, registered 109, 4 manifest value(s), session ready', result) + reg = (self.compat_data / 'pfx/system.reg').read_text() + self.assertIn(r'[SOFTWARE\\Wow6432Node\\Ubisoft\\Launcher\\Installs\\109]', reg) + self.assertIn(r'[SOFTWARE\\Ubisoft\\Launcher\\Installs\\109]', reg) + self.assertIn('"Language"="en-US"', reg) + folder = store.windows_path(str(self.folder)).replace('\\', '\\\\') + self.assertIn('"GamePath"="%s\\\\"' % folder, reg) diff --git a/tools/tests/test_store_launch_ea.py b/tools/tests/test_store_launch_ea.py new file mode 100644 index 0000000000..2dd72b324b --- /dev/null +++ b/tools/tests/test_store_launch_ea.py @@ -0,0 +1,129 @@ +import base64 +import importlib.machinery +import importlib.util +import json +from pathlib import Path +import tempfile +import unittest +from unittest.mock import patch + +BIN = Path(__file__).resolve().parents[1] / 'linuxfs/overlay/usr/local/bin' +loader = importlib.machinery.SourceFileLoader('droiddeck_store_launch_ea', str(BIN / 'droiddeck-store-launch')) +spec = importlib.util.spec_from_loader(loader.name, loader) +store = importlib.util.module_from_spec(spec) +loader.exec_module(store) + + +class EaHook(unittest.TestCase): + def test_wmi_output_reads_the_value_under_the_header(self): + text = 'Manufacturer \r\r\nThe Wine Project \r\r\n\r\r\n' + self.assertEqual(store.wmi_value(text), 'The Wine Project') + self.assertEqual(store.wmi_value(store.decode_output('Name\r\nFEX CPU\r\n'.encode('utf-16'))), 'FEX CPU') + self.assertEqual(store.wmi_value(''), '') + + def test_registry_points_the_game_keys_at_the_install_folder(self): + sidecar = {'id': 'Origin.OFR.50.0001', 'title': 'Mass Effect 3', 'extra': {'registry': json.dumps([ + [r'HKEY_LOCAL_MACHINE\SOFTWARE\EA Games\Game', 'Install Dir'], + [r'HKLM\SOFTWARE\Respawn\Titanfall2', 'Install Dir'], + [r'HKEY_CLASSES_ROOT\x', 'y'], + ])}} + entries = store.ea_registry_entries(Path('/root/Games/Stores/EA/Game'), sidecar) + self.assertIn((r'HKEY_LOCAL_MACHINE\SOFTWARE\Origin', 'ClientPath', store.EA_CLIENT), entries) + game = [e for e in entries if e[1] == 'Install Dir'] + self.assertEqual([e[0] for e in game], [r'HKEY_LOCAL_MACHINE\SOFTWARE\EA Games\Game', r'HKEY_LOCAL_MACHINE\SOFTWARE\Respawn\Titanfall2']) + self.assertEqual(game[0][2], 'Z:' + '\\'.join(['', 'root', 'Games', 'Stores', 'EA', 'Game', ''])) + self.assertIn((r'HKEY_LOCAL_MACHINE\SOFTWARE\EA Games\Game', 'DisplayName', 'Mass Effect 3'), entries) + self.assertIn((r'HKEY_LOCAL_MACHINE\SOFTWARE\Respawn\Titanfall2', 'DisplayName', 'Mass Effect 3'), entries) + + def test_legacy_games_are_registered_by_content_id(self): + sidecar = {'id': 'Origin.OFR.50.0005237', 'title': 'Mass Effect 3 (2012)', + 'extra': {'contentIds': '71073,71183, bad id', 'displayName': 'Mass Effect 3', 'locale': 'fr_FR'}} + entries = store.ea_registry_entries(Path('/root/Games/ME3'), sidecar) + key = r'HKEY_LOCAL_MACHINE\SOFTWARE\Origin Games' + '\\' + self.assertIn((key + '71073', 'DisplayName', 'Mass Effect 3'), entries) + self.assertIn((key + '71183', 'Locale', 'fr_FR'), entries) + self.assertFalse(any('bad id' in e[0] for e in entries)) + + def test_cpuid_probe_is_a_32_bit_console_program(self): + import struct + exe = store.cpuid_exe() + self.assertEqual(exe[:2], b'MZ') + self.assertEqual(exe[0x40:0x44], b'PE\0\0') + self.assertEqual(struct.unpack_from(' -char *const *bl_webhelper_argv(const char *path, char *const argv[]); +#include +char *const *bl_webhelper_argv(const char *path, char *const argv[], char *const envp[]); int main(int argc, char **argv) { - char *const *out = bl_webhelper_argv(argv[1], argv + 1); + const char *mode = getenv("TEST_NODE_MODE_CHILD"); + static char node[64]; + snprintf(node, sizeof node, "ELECTRON_RUN_AS_NODE=%s", mode ? mode : ""); + char *node_env[] = {node, NULL}; + char *plain_env[] = {"PATH=/usr/bin", NULL}; + char *const *envp = mode ? node_env : plain_env; + char *const *out = bl_webhelper_argv(argv[1], argv + 1, (char *const *)envp); for (int i = 0; out[i]; i++) printf("%s\n", out[i]); return 0; } @@ -35,13 +42,28 @@ class WebhelperPreloadTest(unittest.TestCase): def tearDownClass(cls): shutil.rmtree(cls.tmp, ignore_errors=True) - def argv(self, *args, spec=None): + def argv(self, *args, spec=None, node_child=False, parent_node=False): env = {k: v for k, v in os.environ.items() if k != "BL_WEBHELPER_ARGS"} + env.pop("ELECTRON_RUN_AS_NODE", None) if spec is not None: env["BL_WEBHELPER_ARGS"] = spec + if node_child is not False: + env["TEST_NODE_MODE_CHILD"] = "1" if node_child is True else node_child + else: + env.pop("TEST_NODE_MODE_CHILD", None) + if parent_node: + env["ELECTRON_RUN_AS_NODE"] = "1" result = subprocess.run([str(self.program), *args], env=env, capture_output=True, text=True, check=True) return result.stdout.splitlines() + def chromium(self, directory, name): + root = self.tmp / directory + root.mkdir(exist_ok=True) + executable = root / name + executable.touch() + (root / "chrome-sandbox").touch() + return str(executable) + def test_the_browser_process_keeps_its_helpers_in_process(self): out = self.argv(WEBHELPER, "-lang=en_US", "--enable-features=A", "--no-sandbox", "--disable-features=B,C") self.assertEqual(out, [WEBHELPER, "-lang=en_US", "--no-sandbox", "--no-zygote", "--in-process-gpu", @@ -57,6 +79,31 @@ class WebhelperPreloadTest(unittest.TestCase): other = ["/usr/bin/steam", "--enable-features=A"] self.assertEqual(self.argv(*other), other) + def test_node_mode_from_the_child_environment_skips_sandbox_injection(self): + code = self.chromium("vscode", "code") + self.assertEqual(self.argv(code, node_child=True), [code]) + + def test_any_nonempty_node_mode_value_skips_sandbox_injection(self): + code = self.chromium("vscode-true", "code") + for value in ("true", "0", "yes"): + self.assertEqual(self.argv(code, node_child=value), [code]) + + def test_empty_node_mode_value_still_gets_sandbox_flag(self): + chrome = self.chromium("chromium-empty-mode", "chrome") + self.assertEqual(self.argv(chrome, node_child=""), [chrome, "--no-sandbox"]) + + def test_non_browser_helper_beside_chrome_sandbox_skips_injection_in_node_mode(self): + helper = self.chromium("vscode-helper", "remote-cli") + self.assertEqual(self.argv(helper, node_child=True), [helper]) + + def test_chromium_gui_launch_still_gets_sandbox_flag_from_explicit_environment(self): + chrome = self.chromium("chromium", "chrome") + self.assertEqual(self.argv(chrome), [chrome, "--no-sandbox"]) + + def test_parent_node_mode_does_not_override_the_explicit_child_environment(self): + chrome = self.chromium("chromium-parent-mode", "chrome") + self.assertEqual(self.argv(chrome, parent_node=True), [chrome, "--no-sandbox"]) + def test_stock_turns_it_off(self): args = [WEBHELPER, "--enable-features=A"] self.assertEqual(self.argv(*args, spec="stock"), args) diff --git a/tools/tests/test_wincomponents.py b/tools/tests/test_wincomponents.py index 51c555b9bb..7423fb2bbf 100644 --- a/tools/tests/test_wincomponents.py +++ b/tools/tests/test_wincomponents.py @@ -35,8 +35,146 @@ class WinComponentsTest(unittest.TestCase): def pick(self, *ids): self.selection.write_text(json.dumps({"version": 1, "games": {"2571976725": list(ids)}})) - def apply(self): - return MODULE["apply"](self.compat, "2571976725", self.selection, self.store) + def apply(self, proton=None, env=None): + return MODULE["apply"](self.compat, "2571976725", self.selection, self.store, proton=proton, env=env) + + def proton(self, arm=True): + root = Path(self.tmp.name) / ("arm-proton" if arm else "x64-proton") + (root / "files/lib/wine" / ("aarch64-unix" if arm else "x86_64-unix")).mkdir(parents=True) + return root / "proton" + + def pe_image(self, machine): + head = bytearray(64) + head[:2] = b"MZ" + head[60:64] = (64).to_bytes(4, "little") + return bytes(head) + b"PE\0\0" + machine.to_bytes(2, "little") + b"\0" * 32 + + def arm64ec_pe_image(self): + image = bytearray(0x400) + image[:2] = b"MZ" + image[60:64] = (64).to_bytes(4, "little") + image[64:68] = b"PE\0\0" + coff = 68 + image[coff:coff + 2] = (0x8664).to_bytes(2, "little") + image[coff + 2:coff + 4] = (1).to_bytes(2, "little") + image[coff + 16:coff + 18] = (240).to_bytes(2, "little") + optional = coff + 20 + image[optional:optional + 2] = (0x20B).to_bytes(2, "little") + image[optional + 108:optional + 112] = (16).to_bytes(4, "little") + directory = optional + 112 + 10 * 8 + image[directory:directory + 4] = (0x1000).to_bytes(4, "little") + image[directory + 4:directory + 8] = (0xD0).to_bytes(4, "little") + section = optional + 240 + image[section + 8:section + 12] = (0x200).to_bytes(4, "little") + image[section + 12:section + 16] = (0x1000).to_bytes(4, "little") + image[section + 16:section + 20] = (0x200).to_bytes(4, "little") + image[section + 20:section + 24] = (0x200).to_bytes(4, "little") + image[0x200:0x204] = (0xD0).to_bytes(4, "little") + image[0x2C8:0x2D0] = (0x10000000).to_bytes(8, "little") + return bytes(image) + + def test_foreign_component_dll_is_skipped_and_override_dropped(self): + proton = self.proton() + self.component("vc", ["ucrtbase"], {"system32/ucrtbase.dll": self.pe_image(0x8664)}) + self.pick("vc") + names, _ = self.apply(proton) + self.assertEqual(names, []) + self.assertFalse((self.pfx / "system32/ucrtbase.dll").exists()) + names, notes = self.apply(proton) + self.assertEqual(names, []) + self.assertEqual(notes, []) + + def test_component_final_arm64ec_dll_is_placed_and_keeps_its_override(self): + proton = self.proton() + self.component("vc", ["ucrtbase"], {"system32/ucrtbase.dll": self.arm64ec_pe_image()}) + self.pick("vc") + names, _ = self.apply(proton) + self.assertEqual(names, ["ucrtbase"]) + self.assertEqual(MODULE["pe_machine"](self.pfx / "system32/ucrtbase.dll"), 0x8664) + + def test_x64_visual_cpp_runtime_is_placed_in_arm64_system32(self): + proton = self.proton() + self.component("vc", ["msvcp140", "vcruntime140_1"], { + "system32/msvcp140.dll": self.pe_image(0x8664), + "system32/vcruntime140_1.dll": self.pe_image(0x8664), + }) + self.pick("vc") + names, _ = self.apply(proton) + self.assertEqual(names, ["msvcp140", "vcruntime140_1"]) + self.assertEqual(MODULE["pe_machine"](self.pfx / "system32/msvcp140.dll"), 0x8664) + self.assertEqual(MODULE["pe_machine"](self.pfx / "system32/vcruntime140_1.dll"), 0x8664) + + def test_compatible_copy_keeps_override_when_other_architecture_copy_is_skipped(self): + proton = self.proton() + self.component("vc", ["ucrtbase"], { + "system32/ucrtbase.dll": self.pe_image(0x8664), + "syswow64/ucrtbase.dll": self.pe_image(0x14C), + }) + self.pick("vc") + names, _ = self.apply(proton) + self.assertEqual(names, ["ucrtbase"]) + self.assertFalse((self.pfx / "system32/ucrtbase.dll").exists()) + self.assertEqual(MODULE["pe_machine"](self.pfx / "syswow64/ucrtbase.dll"), 0x14C) + + def test_existing_foreign_component_dll_is_removed_for_repair(self): + proton = self.proton() + self.component("vc", ["ucrtbase"], {"system32/ucrtbase.dll": self.pe_image(0x8664)}) + self.pick("vc") + self.pfx.joinpath("system32").mkdir(parents=True) + self.pfx.joinpath("system32/ucrtbase.dll").write_bytes(self.pe_image(0x8664)) + self.compat.joinpath(".droiddeck-wincomponents.json").write_text(json.dumps({ + "files": {"drive_c/windows/system32/ucrtbase.dll": "vc"}, "components": ["vc"]})) + self.apply(proton) + self.assertFalse((self.pfx / "system32/ucrtbase.dll").exists()) + self.assertEqual([], GAME_ENV["repair_foreign_system_dlls"](str(self.compat), str(proton))) + self.assertEqual([], GAME_ENV["repair_foreign_system_dlls"](str(self.compat), str(proton))) + + def test_foreign_component_restore_does_not_override_repair_with_native_override(self): + proton = self.proton() + builtin = Path(self.tmp.name) / "arm-proton/files/lib/wine/aarch64-windows/ucrtbase.dll" + builtin.parent.mkdir(parents=True, exist_ok=True) + builtin.write_bytes(self.pe_image(0xAA64)) + system32 = self.pfx / "system32" + system32.mkdir(parents=True) + dll = system32 / "ucrtbase.dll" + dll.write_bytes(self.pe_image(0x8664)) + backup = self.compat / ".droiddeck-wincomponents-backup/drive_c/windows/system32/ucrtbase.dll" + backup.parent.mkdir(parents=True) + backup.write_bytes(self.pe_image(0x8664)) + self.compat.joinpath(".droiddeck-wincomponents.json").write_text(json.dumps({ + "files": {"drive_c/windows/system32/ucrtbase.dll": "vc"}, "components": ["vc"]})) + self.compat.joinpath("pfx/user.reg").parent.mkdir(parents=True, exist_ok=True) + self.compat.joinpath("pfx/user.reg").write_text( + 'WINE REGISTRY Version 2\n\n[Software\\\\Wine\\\\DllOverrides] 1\n"ucrtbase"="native,builtin"\n') + fixed = GAME_ENV["repair_foreign_system_dlls"](str(self.compat), str(proton)) + self.assertEqual(["system32/ucrtbase.dll (machine 0x8664)"], fixed) + self.apply(proton) + self.assertTrue(dll.is_symlink()) + self.assertEqual(0xAA64, MODULE["pe_machine"](dll)) + + def test_matching_component_dll_is_placed(self): + proton = self.proton() + self.component("vc", ["ucrtbase"], {"system32/ucrtbase.dll": self.pe_image(0xAA64)}) + self.pick("vc") + names, _ = self.apply(proton) + self.assertEqual(names, ["ucrtbase"]) + self.assertEqual(MODULE["pe_machine"](self.pfx / "system32/ucrtbase.dll"), 0xAA64) + + def test_x64_proton_places_x64_component_dll(self): + proton = self.proton(arm=False) + self.component("vc", ["ucrtbase"], {"system32/ucrtbase.dll": self.pe_image(0x8664)}) + self.pick("vc") + names, _ = self.apply(proton) + self.assertEqual(names, ["ucrtbase"]) + self.assertEqual(MODULE["pe_machine"](self.pfx / "system32/ucrtbase.dll"), 0x8664) + + def test_arm_proton_with_x64_selection_places_x64_component_dll(self): + proton = self.proton() + self.component("vc", ["ucrtbase"], {"system32/ucrtbase.dll": self.pe_image(0x8664)}) + self.pick("vc") + names, _ = self.apply(proton, {"PROTON_USE_ARM64": "0"}) + self.assertEqual(names, ["ucrtbase"]) + self.assertEqual(MODULE["pe_machine"](self.pfx / "system32/ucrtbase.dll"), 0x8664) def test_places_files_before_the_prefix_exists_and_returns_overrides(self): self.component("openal", ["openal32"], {"syswow64/OpenAL32.dll": b"x86", "system32/OpenAL32.dll": b"x64"}) @@ -235,7 +373,7 @@ class WinComponentsTest(unittest.TestCase): self.pick("openal") globals_ = GAME_ENV["win_components"].__globals__ real = runpy.run_path(str(BIN / "droiddeck-wincomponents")) - real["apply"].__defaults__ = (self.selection, self.store, self.selection.parent / "no-pending.json", None) + real["apply"].__defaults__ = (self.selection, self.store, self.selection.parent / "no-pending.json", None, None, None) with mock.patch.dict(globals_, {"runpy": mock.Mock(run_path=lambda _path: real)}): self.assertEqual(GAME_ENV["win_components"](str(self.compat) + "/", "2571976725"), "openal32=n,b") self.assertEqual(GAME_ENV["win_components"]("", "2571976725"), "") diff --git a/tools/ubisoft-launcher/.gitignore b/tools/ubisoft-launcher/.gitignore new file mode 100644 index 0000000000..90de6fc989 --- /dev/null +++ b/tools/ubisoft-launcher/.gitignore @@ -0,0 +1,2 @@ +target/ +out/ diff --git a/tools/ubisoft-launcher/Cargo.lock b/tools/ubisoft-launcher/Cargo.lock new file mode 100644 index 0000000000..a2bc2229ea --- /dev/null +++ b/tools/ubisoft-launcher/Cargo.lock @@ -0,0 +1,105 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "droiddeck-ubisoft-launcher" +version = "0.1.0" +dependencies = [ + "serde_json", +] + +[[package]] +name = "itoa" +version = "1.0.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3bd1574e617a9a6c1f8f8181bd7e841e1f20e4277607ef28945765188d6284d4" + +[[package]] +name = "memchr" +version = "2.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98" + +[[package]] +name = "proc-macro2" +version = "1.0.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.47" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "serde" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4148590afebada386688f18773da617792bf2ef03ffc1e4cbd2b1d45b023e0ba" +dependencies = [ + "serde_core", +] + +[[package]] +name = "serde_core" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67dca2c9c51e58a4791a4b1ed58308b39c64224d349a935ab5039aa360942a48" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7a5d71263a5a7d47b41f6b3f06ba276f10cc18b0931f1799f710578e2309348" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.154" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7e9cc8b1b85264074fbcc02a88680c4096b1e47df8f739dceb03bf482f04bd6" +dependencies = [ + "itoa", + "memchr", + "serde", + "serde_core", + "zmij", +] + +[[package]] +name = "syn" +version = "3.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d78c8dee4c7bf0e14673097256fed6142ce9d3b85a408189d07482442145823b" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563" + +[[package]] +name = "zmij" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29666d0abbfad1e3dc4dcf6144730dd3a3ab225bbbdac83319345b1b44ccfc1b" diff --git a/tools/ubisoft-launcher/Cargo.toml b/tools/ubisoft-launcher/Cargo.toml new file mode 100644 index 0000000000..e90819761e --- /dev/null +++ b/tools/ubisoft-launcher/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "droiddeck-ubisoft-launcher" +version = "0.1.0" +edition = "2021" +license = "GPL-3.0-only" +publish = false + +[[bin]] +name = "droiddeck-upc" +path = "src/main.rs" + +[dependencies] +serde_json = "1" + +[profile.release] +opt-level = "s" +lto = true +panic = "abort" +strip = true diff --git a/tools/ubisoft-launcher/build.sh b/tools/ubisoft-launcher/build.sh new file mode 100755 index 0000000000..437d2c2df0 --- /dev/null +++ b/tools/ubisoft-launcher/build.sh @@ -0,0 +1,10 @@ +#!/usr/bin/env bash +# Builds droiddeck-upc.exe for Wine and stages it with the guest scripts. +set -euo pipefail +here=$(cd "$(dirname "$0")" && pwd) +out=${1:-"$here/../../app/src/main/assets/linuxfs/usr/local/lib/droiddeck-ubisoft"} +target=x86_64-pc-windows-gnu +rustup target add "$target" >/dev/null 2>&1 || true +cargo build --manifest-path "$here/Cargo.toml" --release --locked --target "$target" +mkdir -p "$out" +install -m644 "${CARGO_TARGET_DIR:-$here/target}/$target/release/droiddeck-upc.exe" "$out/droiddeck-upc.exe" diff --git a/tools/ubisoft-launcher/src/launcher.rs b/tools/ubisoft-launcher/src/launcher.rs new file mode 100644 index 0000000000..343708dba0 --- /dev/null +++ b/tools/ubisoft-launcher/src/launcher.rs @@ -0,0 +1,513 @@ +//! What Ubisoft Connect answers on its local pipes, built from the launch configuration. +//! +//! Field numbers follow `mg.protocol.uplaydll` and `game_starter` as embedded in Ubisoft's own +//! `uplay_r1.dll`; see docs/development/ubisoft-launch.md. + +use crate::pb::{self, Msg, Value}; +use serde_json::Value as Json; + +pub const GAME_START: &str = "game_start_ipc_pipe_"; +pub const API_PROCESS: &str = "uplay_api_process_ipc_pipe_"; +pub const SESSION: &str = "uplay_ipc_pipe_"; +pub const AUX: &str = "uplay_aux_ipc_pipe_"; +pub const SERVICE: &str = "uplay_service_ipc_pipe_"; +pub const ORBIT: &str = "orbit_ipc_pipe_"; +/// Pipes Connect creates that the game may open; they are held open without answers. +pub const QUIET: &[&str] = &[ + "upc_api_process_ipc_pipe_standard", + "uplay_protocol_ipc_pipe_", + "uplay_overlay_ipc_pipe_", + "uplay_crash_reporter_ipc_pipe_", +]; +/// `GetOsiRsp.Status.Failed`: no online-services pipe is offered. +const OSI_FAILED: u64 = 1; +/// The add-on list Connect returns from the aux init; the game echoes it back. +const AUX_ADDONS: [u64; 3] = [3367, 3368, 3962]; +/// `OverlayInjectionMethod_None`. +const OVERLAY_NONE: u64 = 1; +const HWSCORE: &str = "C:/ProgramData/Ubisoft/Ubisoft Game Launcher/cache//statistics/hwscore"; + +pub struct Config { + pub product: u32, + pub exe: String, + pub args: String, + pub workdir: String, + pub user_id: String, + pub username: String, + pub name_on_platform: String, + pub email: String, + pub country: String, + pub ticket: String, + pub owned: Vec, + pub app_id: String, + pub saves: String, + pub language: String, + pub bridge: String, + pub log: String, + /// Installed chunks of the game's manifest: id and tags. + pub chunks: Vec<(u32, String)>, + /// Owned products and their ownership type (0 game, 1 add-on). + pub products: Vec<(u32, u32)>, +} + +fn s(j: &Json, k: &str) -> String { + j.get(k).and_then(|v| v.as_str()).unwrap_or_default().to_string() +} + +impl Config { + pub fn parse(text: &str) -> Result { + let j: Json = serde_json::from_str(text).map_err(|e| format!("config: {e}"))?; + let product = j.get("productId").and_then(|v| v.as_u64()).ok_or("config: no productId")? as u32; + let account = j.get("account").cloned().unwrap_or(Json::Null); + let mut owned: Vec = j.get("owned").and_then(|v| v.as_array()).map(|a| a.iter().filter_map(|v| v.as_u64()).map(|v| v as u32).collect()).unwrap_or_default(); + if !owned.contains(&product) { + owned.push(product); + } + let mut saves = s(&j, "saves").replace('\\', "/"); + if !saves.ends_with('/') { + saves.push('/'); + } + let c = Config { + product, + exe: s(&j, "exe"), + args: s(&j, "args"), + workdir: s(&j, "workdir"), + user_id: s(&account, "userId"), + username: s(&account, "username"), + name_on_platform: s(&account, "nameOnPlatform"), + email: s(&account, "email"), + country: s(&account, "country"), + ticket: s(&j, "ticket"), + owned, + app_id: s(&j, "appId"), + saves, + language: Some(s(&j, "language")).filter(|l| !l.is_empty()).unwrap_or_else(|| "en-US".into()), + bridge: s(&j, "bridge"), + log: s(&j, "log"), + chunks: j.get("chunks").and_then(|v| v.as_array()).map(|a| a.iter().filter_map(|c| { + Some((c.get("id")?.as_u64()? as u32, c.get("tags").and_then(|t| t.as_str()).unwrap_or("").to_string())) + }).collect()).unwrap_or_default(), + products: j.get("products").and_then(|v| v.as_array()).map(|a| a.iter().filter_map(|p| { + Some((p.get("id")?.as_u64()? as u32, p.get("type").and_then(|t| t.as_u64()).unwrap_or(0) as u32)) + }).collect()).unwrap_or_default(), + }; + if c.exe.is_empty() || c.ticket.is_empty() { + return Err("config: exe and ticket are required".into()); + } + Ok(c) + } + + fn root(&self) -> String { + let mut r = self.workdir.replace('\\', "/"); + if !r.ends_with('/') { + r.push('/'); + } + r + } + + /// The executable the way Connect hands it back: the install root, a slash, then the path under it. + pub fn reply_exe(&self, requested: &str) -> String { + let root = self.root(); + let base = root.trim_end_matches('/'); + let exe = requested.replace('\\', "/"); + let rel = if !base.is_empty() && exe.to_lowercase().starts_with(&(base.to_lowercase() + "/")) { &exe[base.len() + 1..] } else { &exe[..] }; + format!("{root}/{rel}") + } + + /// `game_starter.Downstream` granting the start the starter asked for. + pub fn game_start(&self, request: &[u8]) -> Vec { + let requested = pb::find(request, 10, 4).map(|b| String::from_utf8_lossy(b).into_owned()).unwrap_or_else(|| self.exe.clone()); + let progress = Msg::new() + .msg(1, Msg::new().uint(1, 1).uint(2, 113).uint(3, 0).uint(4, 1).uint(5, 0)) + .msg(1, Msg::new().uint(1, 3).uint(2, 123).uint(3, 0).uint(4, 0).uint(5, 0)); + let granted = Msg::new() + .uint(1, 0) + .uint(2, 0) + .text(3, &self.reply_exe(&requested)) + .text(4, &self.root()) + .uint(8, 3) + .msg(9, progress); + Msg::new().msg(2, Msg::new().msg(1, granted)).0 + } + + /// `Chunks`: every installed chunk, marked downloaded. + fn chunks(&self) -> Msg { + let mut m = Msg::new(); + for (id, tags) in &self.chunks { + m = m.msg(1, Msg::new().uint(1, *id as u64).uint(2, 1).text(3, tags)); + } + m + } + + /// `Rsp.initProcessRsp` for the game announcing its process. The overlay is reported off + /// everywhere: with it on, `uplay_r1.dll` loads `overlay.dll` and fails its startup without it. + pub fn init_process(&self) -> Vec { + Msg::new().msg(13, Msg::new().uint(1, 1).uint(3, 0).uint(4, OVERLAY_NONE).uint(6, 0)).0 + } + + /// `Rsp.initRsp`: the account, its products and the PC ticket. `uplay_r2.dll` copies the + /// install and storage state into objects it only creates for the subsystems the game asked + /// for, without checking them, so those parts follow the request when it names subsystems. + pub fn session(&self, wants: Option) -> Vec { + let account = Msg::new() + .text(1, &self.user_id) + .text(2, &self.username) + .text(3, &self.name_on_platform) + .text(4, &self.email) + .text(5, "") + .text(6, &self.country); + let mut body = Msg::new().uint(1, 1).msg(2, account).uint(3, 1).uint(4, self.product as u64); + for id in &self.owned { + body = body.uint(5, *id as u64); + } + body = body + .msg(6, Msg::new().uint(1, self.product as u64).text(2, "")) + .uint(7, 0) + .text(8, &self.ticket); + if wants.map_or(true, |w| w.install) { + body = body.msg(14, Msg::new().uint(1, 1).msg(2, self.chunks()).text(3, &self.language)); + } + body = body + .msg(11, Msg::new().uint(1, 0)) + .uint(12, 0) + .msg(15, Msg::new().uint(1, 0).uint(2, OVERLAY_NONE)) + .msg(16, Msg::new().uint(1, 6211).uint(2, 22395).float(3, 1.0)); + if wants.map_or(true, |w| w.storage) { + body = body.msg(17, Msg::new().text(1, &self.saves)); + } + body = body + .bytes(18, b"") + .msg(19, Msg::new().uint(1, 1).uint(2, 1)) + .msg(21, Msg::new().uint(2, 1)); + if wants.is_some() { + body = body.bytes(20, b""); + } + if !self.app_id.is_empty() { + body = body.msg(22, Msg::new().text(1, &self.app_id)); + } + Msg::new().msg(1, body.bytes(23, b"")).0 + } + + /// `Rsp.userRsp.getCredentialsRsp`: the account without its password, which DroidDeck never holds. + pub fn credentials(&self, request_id: u64) -> Vec { + let rsp = Msg::new().uint(1, request_id).text(2, &self.username).text(3, "").text(4, &self.user_id).text(5, &self.email); + Msg::new().msg(8, Msg::new().msg(1, rsp)).0 + } + + /// `Rsp.installerRsp.initInstallerRsp`. + pub fn installer(&self) -> Vec { + Msg::new().msg(10, Msg::new().msg(1, Msg::new().uint(1, 1).msg(2, self.chunks()).text(3, &self.language))).0 + } + + /// `Rsp.productRsp.getProductListRsp`: every owned product as owned, playable and purchased. + pub fn product_list(&self, request_id: u64) -> Vec { + let mut list = Msg::new().uint(1, request_id).uint(2, 1); + let products: Vec<(u32, u32)> = if self.products.is_empty() { self.owned.iter().map(|id| (*id, 0)).collect() } else { self.products.clone() }; + for (id, kind) in products { + list = list.msg(3, Msg::new().uint(1, id as u64).uint(2, if kind == 1 { 2 } else { 1 }).uint(3, 1).uint(4, 3).uint(6, 1)); + } + Msg::new().msg(15, Msg::new().msg(1, list)).0 + } + + pub fn service_hello(&self) -> Vec { + Msg::new().msg(3, Msg::new().uint(1, 1).msg(3, Msg::new().text(1, HWSCORE))).0 + } + + /// `mg.protocol.orbitdll.Rsp`. The DLL keeps its own `*.save` files in the folder `init` returns. + pub fn orbit(&self, body: &[u8]) -> Option<(Vec, String)> { + let f = pb::fields(body)?; + let id = pb::num(&f, 1)?; + let rsp = Msg::new().uint(1, id); + if let Some(init) = pb::sub(&f, 12) { + let product = pb::fields(init).and_then(|i| pb::num(&i, 1)).unwrap_or(0); + return Some((rsp.msg(12, Msg::new().text(1, &self.saves).uint(2, 1)).0, format!("init {id} for {product} answered"))); + } + if pb::has(&f, 2) { + let login = Msg::new().text(1, &self.username).text(2, "").text(3, ""); + return Some((rsp.msg(2, login).0, format!("login details {id} answered"))); + } + if let Some(osi) = pb::sub(&f, 3) { + let sub = pb::fields(osi).and_then(|o| pb::num(&o, 1)).unwrap_or(0); + return Some((rsp.msg(3, Msg::new().uint(2, OSI_FAILED)).0, format!("osi {id} for {sub} declined"))); + } + None + } +} + +/// Requests answered with only their id and a result: (`Req` field, its request) to (`Rsp` field, its +/// response, `OperationResult`). 1 is ok and 4 not found. +const ACKNOWLEDGED: &[(u32, u32, u32, u32, u64)] = &[ + (8, 1, 5, 1, 1), // achievements: list (none) + (8, 2, 5, 2, 4), // achievements: image + (8, 3, 5, 3, 1), // achievements: earn + (9, 1, 6, 1, 1), // overlay: show + (9, 2, 6, 2, 1), // overlay: shop url + (9, 4, 6, 3, 1), // overlay: browser url + (9, 5, 6, 4, 1), // overlay: notification + (9, 6, 6, 5, 1), // overlay: friend invitation + (9, 7, 6, 6, 1), // overlay: friend selection + (9, 8, 6, 8, 1), // overlay: micro app + (10, 1, 7, 1, 4), // avatar + (20, 1, 14, 1, 1), // store products (none) + (23, 3, 15, 5, 1), // track add-on + (25, 1, 17, 1, 4), // user lookup + (25, 3, 17, 3, 1), // rich presence + (25, 4, 17, 4, 1), // played with +]; + +/// `Rsp.streamingRsp` (20) for `Req.streamingReq` (27): the game is not streamed, so its streaming +/// type is `StreamingType_None` and every other streaming query is not found. +fn streaming(req: &[(u32, Value<'_>)]) -> (Vec, String) { + let id = pb::num(req, 1).unwrap_or(0); + let rsp = if pb::has(req, 3) { + Msg::new().uint(1, id).uint(2, 1).msg(4, Msg::new().uint(1, 0)) + } else { + Msg::new().uint(1, id).uint(2, 4) + }; + (Msg::new().msg(20, rsp).0, format!("streaming id {id} -> not streamed")) +} + +/// The id-and-result answer to a request in [ACKNOWLEDGED] or a streaming query, with a name for the log. +pub fn acknowledge(body: &[u8]) -> Option<(Vec, String)> { + let f = pb::fields(body)?; + if let Some(req) = pb::sub(&f, 27).and_then(pb::fields) { + return Some(streaming(&req)); + } + for &(req, sub, rsp, rsp_sub, result) in ACKNOWLEDGED { + let Some(inner) = pb::sub(&f, req).and_then(pb::fields) else { continue }; + let Some(leaf) = pb::sub(&inner, sub).and_then(pb::fields) else { continue }; + let id = pb::num(&leaf, 1).unwrap_or(0); + let answer = Msg::new().msg(rsp, Msg::new().msg(rsp_sub, Msg::new().uint(1, id).uint(2, result))).0; + return Some((answer, format!("{req}.{sub} id {id} -> {result}"))); + } + None +} + +/// The subsystems an `InitReq` asks for (fields 4 to 11). +#[derive(Clone, Copy, Debug, Default, PartialEq)] +pub struct Subsystems { + pub install: bool, + pub storage: bool, +} + +impl Subsystems { + /// `None` when the request names no subsystem at all. + pub fn of(init: &[u8]) -> Option { + let f = pb::fields(init)?; + if !(4..=11).any(|n| pb::has(&f, n)) { + return None; + } + let on = |n| pb::num(&f, n).unwrap_or(0) != 0; + Some(Subsystems { storage: on(7), install: on(8) }) + } +} + +pub enum SessionReq { + Init(Option), + Installer, + ProductList(u64), + Credentials(u64), + Other, +} + +pub fn session_request(body: &[u8]) -> SessionReq { + let Some(f) = pb::fields(body) else { return SessionReq::Other }; + let leaf = |req, sub| pb::sub(&f, req).and_then(pb::fields).and_then(|r| pb::sub(&r, sub).and_then(pb::fields)); + if pb::sub(&f, 13).and_then(pb::fields).is_some_and(|i| pb::has(&i, 1)) { + SessionReq::Installer + } else if pb::has(&f, 1) { + SessionReq::Init(pb::sub(&f, 1).and_then(Subsystems::of)) + } else if let Some(get) = leaf(11, 1) { + SessionReq::Credentials(pb::num(&get, 1).unwrap_or(0)) + } else if let Some(list) = leaf(23, 1) { + SessionReq::ProductList(pb::num(&list, 1).unwrap_or(0)) + } else { + SessionReq::Other + } +} + +/// The product and game process announced on the api-process pipe, each when the game names it +/// (Far Cry 3 Blood Dragon sends only its process). +pub fn announced(body: &[u8]) -> Option<(Option, Option)> { + let f = pb::fields(body)?; + let r = pb::fields(pb::sub(&f, 2)?)?; + let named = |n| pb::num(&r, n).map(|v| v as u32).filter(|v| *v != 0); + Some((named(1), named(2))) +} + +pub enum AuxReq<'a> { + Init(u64), + Activate(u64, &'a [u8]), + Other(u64), +} + +/// `UplayAuxDll.Req`: an init, or an activation carrying the game's Denuvo request token. +pub fn aux_request(body: &[u8]) -> AuxReq<'_> { + let Some(f) = pb::fields(body) else { return AuxReq::Other(0) }; + let rid = pb::num(&f, 1).unwrap_or(0); + let Some(activate) = pb::sub(&f, 3) else { + return if pb::has(&f, 2) { AuxReq::Init(rid) } else { AuxReq::Other(rid) }; + }; + let token = pb::fields(activate) + .and_then(|a| pb::sub(&a, 2).and_then(pb::fields)) + .and_then(|t| t.iter().find_map(|(n, v)| match v { Value::Bytes(b) if *n == 2 => Some(*b), _ => None })); + match token { + Some(t) => AuxReq::Activate(rid, t), + None => AuxReq::Other(rid), + } +} + +pub fn aux_init(rid: u64, pid: u32) -> Vec { + let mut init = Msg::new().uint(1, 1).uint(2, pid as u64); + for a in AUX_ADDONS { + init = init.uint(3, a); + } + Msg::new().uint(1, rid).msg(2, init).0 +} + +pub fn aux_token(rid: u64, game_token: &[u8]) -> Vec { + let answer = Msg::new().uint(2, 1).uint(3, 0).bytes(4, game_token).bytes(5, b""); + Msg::new().uint(1, rid).msg(3, Msg::new().msg(2, answer)).0 +} + +#[cfg(test)] +mod tests { + use super::*; + + fn config() -> Config { + Config::parse(r#"{"productId":109,"exe":"Z:/games/sc/system/splintercell.exe","workdir":"Z:/games/sc","ticket":"T", + "account":{"userId":"u1","username":"name","nameOnPlatform":"name","email":"e@x","country":"US"}, + "owned":[82],"appId":"a-b","saves":"Z:/saves/u1/109","chunks":[{"id":0,"tags":""},{"id":64,"tags":"custo05"}]}"#).unwrap() + } + + #[test] + fn the_owned_list_always_holds_the_game_and_saves_end_with_a_slash() { + let c = config(); + assert_eq!(c.owned, vec![82, 109]); + assert_eq!(c.saves, "Z:/saves/u1/109/"); + } + + #[test] + fn the_start_reply_names_the_exe_under_the_root_with_connects_double_slash() { + let c = config(); + assert_eq!(c.reply_exe("Z:\\games\\sc\\system\\splintercell.exe"), "Z:/games/sc//system/splintercell.exe"); + let req = Msg::new().msg(1, Msg::new().msg(1, Msg::new().uint(1, 13389).uint(2, 109).text(10, "Z:/games/sc/system/splintercell.exe"))); + let rsp = c.game_start(&req.0); + let granted = pb::fields(pb::sub(&pb::fields(pb::sub(&pb::fields(&rsp).unwrap(), 2).unwrap()).unwrap(), 1).unwrap()).unwrap(); + assert_eq!(pb::num(&granted, 1), Some(0)); + assert_eq!(pb::sub(&granted, 3), Some(&b"Z:/games/sc//system/splintercell.exe"[..])); + assert_eq!(pb::sub(&granted, 4), Some(&b"Z:/games/sc/"[..])); + assert_eq!(pb::num(&granted, 8), Some(3)); + } + + #[test] + fn the_session_carries_the_ticket_products_and_save_root() { + let rsp = config().session(None); + let init = pb::fields(pb::sub(&pb::fields(&rsp).unwrap(), 1).unwrap()).unwrap(); + assert_eq!(pb::num(&init, 1), Some(1)); + assert_eq!(pb::num(&init, 4), Some(109)); + let owned: Vec = init.iter().filter_map(|(n, v)| match v { Value::Int(x) if *n == 5 => Some(*x), _ => None }).collect(); + assert_eq!(owned, vec![82, 109]); + assert_eq!(pb::sub(&init, 8), Some(&b"T"[..])); + let saves = pb::fields(pb::sub(&init, 17).unwrap()).unwrap(); + assert_eq!(pb::sub(&saves, 1), Some(&b"Z:/saves/u1/109/"[..])); + let account = pb::fields(pb::sub(&init, 2).unwrap()).unwrap(); + assert_eq!(pb::sub(&account, 1), Some(&b"u1"[..])); + assert_eq!(pb::sub(&account, 5), Some(&b""[..])); + let install = pb::fields(pb::sub(&init, 14).unwrap()).unwrap(); + assert_eq!(pb::num(&install, 1), Some(1)); + let chunks = pb::fields(pb::sub(&install, 2).unwrap()).unwrap(); + let second = pb::fields(match &chunks[1].1 { Value::Bytes(b) => b, _ => panic!() }).unwrap(); + assert_eq!((pb::num(&second, 1), pb::num(&second, 2), pb::sub(&second, 3)), (Some(64), Some(1), Some(&b"custo05"[..]))); + } + + fn session_fields(rsp: &[u8]) -> Vec { + let init = pb::fields(pb::sub(&pb::fields(rsp).unwrap(), 1).unwrap()).unwrap(); + init.iter().map(|(n, _)| *n).collect() + } + + #[test] + fn an_r2_session_leaves_out_the_install_and_storage_the_game_did_not_ask_for() { + let req = Msg::new().msg(1, Msg::new().uint(1, 6145).uint(2, 512).uint(3, 2).uint(4, 1).uint(5, 1).uint(7, 1).uint(8, 0)); + let SessionReq::Init(wants) = session_request(&req.0) else { panic!() }; + assert_eq!(wants, Some(Subsystems { install: false, storage: true })); + let fields = session_fields(&config().session(wants)); + assert!(!fields.contains(&14)); + assert!(fields.contains(&17)); + assert!(fields.contains(&20)); + let all = session_fields(&config().session(Some(Subsystems { install: true, storage: true }))); + assert!(all.contains(&14) && all.contains(&17)); + } + + #[test] + fn streaming_queries_say_the_game_is_not_streamed() { + let (answer, _) = acknowledge(&[0xda, 0x01, 0x06, 0x08, 0x80, 0x80, 0x08, 0x1a, 0x00]).unwrap(); + let rsp = pb::fields(pb::sub(&pb::fields(&answer).unwrap(), 20).unwrap()).unwrap(); + assert_eq!((pb::num(&rsp, 1), pb::num(&rsp, 2)), (Some(0x20000), Some(1))); + assert_eq!(pb::num(&pb::fields(pb::sub(&rsp, 4).unwrap()).unwrap(), 1), Some(0)); + let (answer, _) = acknowledge(&Msg::new().msg(27, Msg::new().uint(1, 7).bytes(2, b"")).0).unwrap(); + let rsp = pb::fields(pb::sub(&pb::fields(&answer).unwrap(), 20).unwrap()).unwrap(); + assert_eq!((pb::num(&rsp, 1), pb::num(&rsp, 2)), (Some(7), Some(4))); + } + + #[test] + fn an_r1_session_without_subsystems_keeps_install_and_storage() { + let SessionReq::Init(wants) = session_request(&Msg::new().msg(1, Msg::new().uint(1, 109).uint(2, 4242)).0) else { panic!() }; + assert_eq!(wants, None); + let fields = session_fields(&config().session(wants)); + assert!(fields.contains(&14) && fields.contains(&17)); + assert!(!fields.contains(&20)); + } + + #[test] + fn requests_are_told_apart() { + assert!(matches!(session_request(&Msg::new().msg(1, Msg::new().uint(1, 109)).0), SessionReq::Init(None))); + assert!(matches!(session_request(&Msg::new().msg(13, Msg::new().bytes(1, b"")).0), SessionReq::Installer)); + assert!(matches!(session_request(&[0xba, 0x01, 0x06, 0x0a, 0x04, 0x08, 0x80, 0x80, 0x04]), SessionReq::ProductList(65536))); + let (answer, _) = acknowledge(&[0x42, 0x08, 0x0a, 0x06, 0x08, 0x80, 0x80, 0x08, 0x10, 0x00]).unwrap(); + let rsp = pb::fields(pb::sub(&pb::fields(pb::sub(&pb::fields(&answer).unwrap(), 5).unwrap()).unwrap(), 1).unwrap()).unwrap(); + assert_eq!((pb::num(&rsp, 1), pb::num(&rsp, 2)), (Some(0x20000), Some(1))); + assert!(acknowledge(&Msg::new().msg(1, Msg::new().uint(1, 1)).0).is_none()); + assert_eq!(announced(&Msg::new().msg(2, Msg::new().uint(1, 109).uint(2, 4242).uint(5, 1)).0), Some((Some(109), Some(4242)))); + assert_eq!(announced(&[0x12, 0x05, 0x10, 0x88, 0x04, 0x28, 0x01]), Some((None, Some(520)))); + assert!(matches!(session_request(&[0x5a, 0x04, 0x0a, 0x02, 0x08, 0x00]), SessionReq::Credentials(0))); + let activate = Msg::new().uint(1, 2).msg(3, Msg::new().msg(2, Msg::new().bytes(2, b"challenge").uint(3, 0))); + assert!(matches!(aux_request(&activate.0), AuxReq::Activate(2, b"challenge"))); + assert!(matches!(aux_request(&Msg::new().uint(1, 1).msg(2, Msg::new().uint(1, 109)).0), AuxReq::Init(1))); + } + + #[test] + fn orbit_init_answers_with_the_save_folder() { + let (rsp, what) = config().orbit(&[0x08, 0x01, 0x62, 0x03, 0x08, 0xcd, 0x01]).unwrap(); + assert_eq!(what, "init 1 for 205 answered"); + let f = pb::fields(&rsp).unwrap(); + assert_eq!(pb::num(&f, 1), Some(1)); + let init = pb::fields(pb::sub(&f, 12).unwrap()).unwrap(); + assert_eq!(pb::sub(&init, 1), Some(&b"Z:/saves/u1/109/"[..])); + assert_eq!(pb::num(&init, 2), Some(1)); + } + + #[test] + fn orbit_login_details_carry_no_password_and_osi_is_declined() { + let c = config(); + let (rsp, _) = c.orbit(&Msg::new().uint(1, 2).bytes(2, b"").0).unwrap(); + let f = pb::fields(&rsp).unwrap(); + let login = pb::fields(pb::sub(&f, 2).unwrap()).unwrap(); + assert_eq!((pb::sub(&login, 1), pb::sub(&login, 2), pb::sub(&login, 3)), (Some(&b"name"[..]), Some(&b""[..]), Some(&b""[..]))); + let (rsp, what) = c.orbit(&Msg::new().uint(1, 3).msg(3, Msg::new().uint(1, 7)).0).unwrap(); + assert_eq!(what, "osi 3 for 7 declined"); + let osi = pb::fields(pb::sub(&pb::fields(&rsp).unwrap(), 3).unwrap()).unwrap(); + assert_eq!((pb::num(&osi, 2), pb::has(&osi, 1)), (Some(OSI_FAILED), false)); + assert!(c.orbit(&Msg::new().uint(1, 4).0).is_none()); + } + + #[test] + fn credentials_name_the_account_without_a_password() { + let rsp = config().credentials(7); + let user = pb::fields(pb::sub(&pb::fields(&rsp).unwrap(), 8).unwrap()).unwrap(); + let get = pb::fields(pb::sub(&user, 1).unwrap()).unwrap(); + assert_eq!(pb::num(&get, 1), Some(7)); + assert_eq!((pb::sub(&get, 2), pb::sub(&get, 3), pb::sub(&get, 4), pb::sub(&get, 5)), + (Some(&b"name"[..]), Some(&b""[..]), Some(&b"u1"[..]), Some(&b"e@x"[..]))); + } +} diff --git a/tools/ubisoft-launcher/src/main.rs b/tools/ubisoft-launcher/src/main.rs new file mode 100644 index 0000000000..0b7126a919 --- /dev/null +++ b/tools/ubisoft-launcher/src/main.rs @@ -0,0 +1,355 @@ +//! Stands in for Ubisoft Connect on the local pipes a Ubisoft game talks to, and starts the game. +//! +//! Usage: droiddeck-upc.exe + +#![cfg_attr(windows, windows_subsystem = "windows")] + +mod launcher; +mod pb; +#[cfg(windows)] +mod win; + +#[cfg(not(windows))] +fn main() { + eprintln!("droiddeck-upc runs inside Wine"); + std::process::exit(2); +} + +#[cfg(windows)] +fn main() { + std::process::exit(service::run()); +} + +#[cfg(windows)] +mod service { + use crate::launcher::{self, AuxReq, Config, SessionReq}; + use crate::win::{self, Handle}; + use std::fs::OpenOptions; + use std::io::Write; + use std::path::PathBuf; + use std::sync::atomic::{AtomicU32, Ordering}; + use std::sync::{Arc, Mutex}; + use std::thread; + use std::time::{Duration, Instant}; + + const MAX_FRAME: usize = 16 << 20; + const ANNOUNCE_WAIT: Duration = Duration::from_secs(120); + const RESTART_WAIT: Duration = Duration::from_secs(15); + const DENUVO_WAIT: Duration = Duration::from_secs(90); + + struct State { + cfg: Config, + prefix: String, + log: Mutex>, + event: Handle, + game: AtomicU32, + started: Instant, + } + + impl State { + fn say(&self, line: &str) { + let text = format!("[{:7.2}] {line}\n", self.started.elapsed().as_secs_f32()); + eprint!("droiddeck-upc: {text}"); + if let Some(f) = self.log.lock().unwrap().as_mut() { + let _ = f.write_all(text.as_bytes()); + } + } + } + + struct Frames<'a> { + pipe: &'a Handle, + buf: Vec, + } + + impl<'a> Frames<'a> { + fn new(pipe: &'a Handle) -> Self { + Frames { pipe, buf: Vec::new() } + } + + fn fill(&mut self) -> bool { + let mut chunk = vec![0u8; 65536]; + match self.pipe.read(&mut chunk) { + Some(n) => { + self.buf.extend_from_slice(&chunk[..n]); + true + } + None => false, + } + } + + /// The next message without its 4-byte big-endian length. + fn next(&mut self) -> Option> { + while self.buf.len() < 4 { + if !self.fill() { + return None; + } + } + let n = u32::from_be_bytes(self.buf[..4].try_into().unwrap()) as usize; + if n > MAX_FRAME { + return None; + } + while self.buf.len() < 4 + n { + if !self.fill() { + return None; + } + } + let body = self.buf[4..4 + n].to_vec(); + self.buf.drain(..4 + n); + Some(body) + } + } + + fn send(pipe: &Handle, body: &[u8]) -> bool { + let mut framed = (body.len() as u32).to_be_bytes().to_vec(); + framed.extend_from_slice(body); + pipe.write_all(&framed) + } + + fn hold(state: &State, name: &str, frames: &mut Frames) { + while let Some(body) = frames.next() { + state.say(&format!("{name}: {} B left unanswered", body.len())); + } + } + + fn game_start(state: &State, pipe: Handle) { + let mut frames = Frames::new(&pipe); + let Some(req) = frames.next() else { return }; + send(&pipe, &state.cfg.game_start(&req)); + state.say("game_start: start granted"); + hold(state, "game_start", &mut frames); + } + + fn api_process(state: &State, pipe: Handle) { + let mut frames = Frames::new(&pipe); + let Some(req) = frames.next() else { return }; + match launcher::announced(&req) { + Some((product, named)) => { + let product = product.unwrap_or(state.cfg.product); + match named.or_else(|| pipe.client_pid()) { + Some(pid) => { + state.say(&format!("api_process: product {product} is process {pid}")); + state.game.store(pid, Ordering::SeqCst); + } + None => state.say(&format!("api_process: product {product} named no process")), + } + } + None => state.say(&format!("api_process: {} B announcement not understood {}", req.len(), hex(&req, 32))), + } + send(&pipe, &state.cfg.init_process()); + state.event.set(); + hold(state, "api_process", &mut frames); + } + + fn session(state: &State, pipe: Handle) { + let mut frames = Frames::new(&pipe); + let mut pushes = 0; + while let Some(req) = frames.next() { + match launcher::session_request(&req) { + SessionReq::Init(wants) => { + pushes += 1; + send(&pipe, &state.cfg.session(wants)); + let asked = wants.map_or("no subsystems named".to_string(), |w| format!("install {}, storage {}", w.install, w.storage)); + state.say(&format!("session: init answered ({pushes}; {asked}; {})", hex(&req, 48))); + } + SessionReq::Installer => { + send(&pipe, &state.cfg.installer()); + state.say("session: installer init answered"); + } + SessionReq::Credentials(id) => { + send(&pipe, &state.cfg.credentials(id)); + state.say(&format!("session: credentials {id} answered")); + } + SessionReq::ProductList(id) => { + send(&pipe, &state.cfg.product_list(id)); + state.say(&format!("session: product list {id} answered")); + } + SessionReq::Other => match launcher::acknowledge(&req) { + Some((answer, what)) => { + send(&pipe, &answer); + state.say(&format!("session: {what}")); + } + None => state.say(&format!("session: {} B request {} not handled", req.len(), hex(&req, 24))), + }, + } + } + state.say("session: closed"); + } + + fn denuvo(state: &State, rid: u64, challenge: &[u8]) -> Option> { + if state.cfg.bridge.is_empty() { + return None; + } + let dir = PathBuf::from(&state.cfg.bridge); + let _ = std::fs::create_dir_all(&dir); + let rsp = dir.join(format!("denuvo-{rid}.rsp")); + let _ = std::fs::remove_file(&rsp); + let part = dir.join(format!("denuvo-{rid}.req.part")); + std::fs::write(&part, challenge).ok()?; + std::fs::rename(&part, dir.join(format!("denuvo-{rid}.req"))).ok()?; + let deadline = Instant::now() + DENUVO_WAIT; + while Instant::now() < deadline { + if let Ok(token) = std::fs::read(&rsp) { + let _ = std::fs::remove_file(&rsp); + return (!token.is_empty()).then_some(token); + } + thread::sleep(Duration::from_millis(200)); + } + None + } + + fn aux(state: &State, pipe: Handle) { + let mut frames = Frames::new(&pipe); + while let Some(req) = frames.next() { + match launcher::aux_request(&req) { + AuxReq::Init(rid) => { + send(&pipe, &launcher::aux_init(rid, win::pid())); + state.say("aux: init answered"); + } + AuxReq::Activate(rid, challenge) => { + state.say(&format!("aux: activation {rid} with a {} B challenge", challenge.len())); + match denuvo(state, rid, challenge) { + Some(token) => { + send(&pipe, &launcher::aux_token(rid, &token)); + state.say(&format!("aux: activation {rid} answered ({} B)", token.len())); + } + None => state.say(&format!("aux: no game token for activation {rid}")), + } + } + AuxReq::Other(rid) => state.say(&format!("aux: request {rid} not handled ({} B)", req.len())), + } + } + } + + fn service(state: &State, pipe: Handle) { + send(&pipe, &state.cfg.service_hello()); + let mut chunk = vec![0u8; 65536]; + while pipe.read(&mut chunk).is_some() {} + } + + fn orbit(state: &State, pipe: Handle) { + let mut frames = Frames::new(&pipe); + while let Some(req) = frames.next() { + match state.cfg.orbit(&req) { + Some((answer, what)) => { + send(&pipe, &answer); + state.say(&format!("orbit: {what}")); + } + None => state.say(&format!("orbit: {} B request {} not handled", req.len(), hex(&req, 24))), + } + } + state.say("orbit: closed"); + } + + fn quiet(state: &State, name: &str, pipe: Handle) { + state.say(&format!("{name}: connected")); + let mut chunk = vec![0u8; 65536]; + while let Some(n) = pipe.read(&mut chunk) { + state.say(&format!("{name}: {n} B unanswered {}", hex(&chunk[..n], 64))); + } + } + + fn serve(state: Arc, name: &'static str) { + thread::spawn(move || { + let full = format!("{}{name}", state.prefix); + loop { + let Some(pipe) = win::accept(&full) else { + state.say(&format!("{name}: cannot serve the pipe (error {})", win::last_error())); + return; + }; + let s = state.clone(); + thread::spawn(move || match name { + launcher::GAME_START => game_start(&s, pipe), + launcher::API_PROCESS => api_process(&s, pipe), + launcher::SESSION => session(&s, pipe), + launcher::AUX => aux(&s, pipe), + launcher::SERVICE => service(&s, pipe), + launcher::ORBIT => orbit(&s, pipe), + _ => quiet(&s, name, pipe), + }); + } + }); + } + + fn hex(b: &[u8], n: usize) -> String { + b.iter().take(n).map(|x| format!("{x:02x}")).collect() + } + + fn command_line(cfg: &Config) -> String { + let mut c = format!("\"{}\"", cfg.exe); + if !cfg.args.trim().is_empty() { + c.push(' '); + c.push_str(cfg.args.trim()); + } + c + } + + pub fn run() -> i32 { + let Some(path) = std::env::args().nth(1) else { + eprintln!("usage: droiddeck-upc "); + return 2; + }; + let parsed = std::fs::read_to_string(&path).map_err(|e| e.to_string()).and_then(|t| Config::parse(&t)); + let _ = std::fs::remove_file(&path); + let cfg = match parsed { + Ok(c) => c, + Err(e) => { + eprintln!("droiddeck-upc: {e}"); + return 2; + } + }; + let log = (!cfg.log.is_empty()).then(|| OpenOptions::new().create(true).append(true).open(&cfg.log).ok()).flatten(); + let state = Arc::new(State { + prefix: format!(r"\\.\pipe\terminal_{}_", win::session_id()), + cfg, + log: Mutex::new(log), + event: win::inheritable_event(), + game: AtomicU32::new(0), + started: Instant::now(), + }); + state.say(&format!("product {} on {}*", state.cfg.product, state.prefix)); + for name in [launcher::GAME_START, launcher::API_PROCESS, launcher::SESSION, launcher::AUX, launcher::SERVICE, launcher::ORBIT] + .into_iter() + .chain(launcher::QUIET.iter().copied()) + { + serve(state.clone(), name); + } + thread::sleep(Duration::from_millis(300)); + + let (child, pid) = match win::spawn(&command_line(&state.cfg), &state.cfg.workdir) { + Ok(c) => c, + Err(e) => { + state.say(&format!("cannot start {} (error {e})", state.cfg.exe)); + return 1; + } + }; + state.say(&format!("started {} as process {pid}", state.cfg.exe)); + while !child.wait(1000) {} + let mut code = child.exit_code(); + state.say(&format!("process {pid} exited with 0x{code:08x}")); + + let mut waiting = Instant::now(); + let mut limit = ANNOUNCE_WAIT; + loop { + let game = state.game.swap(0, Ordering::SeqCst); + if game != 0 { + match win::open_process(game) { + Some(h) => { + while !h.wait(1000) {} + code = h.exit_code(); + state.say(&format!("game process {game} exited with 0x{code:08x}")); + } + None => state.say(&format!("game process {game} is already gone")), + } + waiting = Instant::now(); + limit = RESTART_WAIT; + continue; + } + if waiting.elapsed() >= limit { + break; + } + thread::sleep(Duration::from_millis(500)); + } + state.say("done"); + code as i32 + } +} diff --git a/tools/ubisoft-launcher/src/pb.rs b/tools/ubisoft-launcher/src/pb.rs new file mode 100644 index 0000000000..d0ae27d160 --- /dev/null +++ b/tools/ubisoft-launcher/src/pb.rs @@ -0,0 +1,168 @@ +//! The protobuf wire format, enough to build and read the launcher messages by field number. + +pub fn varint(mut n: u64, out: &mut Vec) { + loop { + let b = (n & 0x7f) as u8; + n >>= 7; + if n == 0 { + out.push(b); + return; + } + out.push(b | 0x80); + } +} + +#[derive(Default, Clone)] +pub struct Msg(pub Vec); + +impl Msg { + pub fn new() -> Self { + Msg(Vec::new()) + } + + fn key(mut self, field: u32, wire: u8) -> Self { + varint(((field as u64) << 3) | wire as u64, &mut self.0); + self + } + + pub fn uint(self, field: u32, value: u64) -> Self { + let mut m = self.key(field, 0); + varint(value, &mut m.0); + m + } + + pub fn bytes(self, field: u32, value: &[u8]) -> Self { + let mut m = self.key(field, 2); + varint(value.len() as u64, &mut m.0); + m.0.extend_from_slice(value); + m + } + + pub fn text(self, field: u32, value: &str) -> Self { + self.bytes(field, value.as_bytes()) + } + + pub fn msg(self, field: u32, value: Msg) -> Self { + self.bytes(field, &value.0) + } + + pub fn float(self, field: u32, value: f32) -> Self { + let mut m = self.key(field, 5); + m.0.extend_from_slice(&value.to_le_bytes()); + m + } +} + +#[derive(Debug, Clone, PartialEq)] +pub enum Value<'a> { + Int(u64), + Bytes(&'a [u8]), +} + +fn read_varint(b: &[u8], i: &mut usize) -> Option { + let mut r = 0u64; + let mut shift = 0; + loop { + let c = *b.get(*i)?; + *i += 1; + if shift >= 64 { + return None; + } + r |= ((c & 0x7f) as u64) << shift; + if c & 0x80 == 0 { + return Some(r); + } + shift += 7; + } +} + +/// The top-level fields of a message, or `None` when it does not parse. +pub fn fields(b: &[u8]) -> Option)>> { + let mut out = Vec::new(); + let mut i = 0; + while i < b.len() { + let key = read_varint(b, &mut i)?; + let field = (key >> 3) as u32; + match key & 7 { + 0 => out.push((field, Value::Int(read_varint(b, &mut i)?))), + 1 => { + let v = b.get(i..i + 8)?; + i += 8; + out.push((field, Value::Int(u64::from_le_bytes(v.try_into().ok()?)))); + } + 2 => { + let n = read_varint(b, &mut i)? as usize; + let v = b.get(i..i.checked_add(n)?)?; + i += n; + out.push((field, Value::Bytes(v))); + } + 5 => { + let v = b.get(i..i + 4)?; + i += 4; + out.push((field, Value::Int(u32::from_le_bytes(v.try_into().ok()?) as u64))); + } + _ => return None, + } + } + Some(out) +} + +pub fn sub<'a>(f: &[(u32, Value<'a>)], field: u32) -> Option<&'a [u8]> { + f.iter().find_map(|(n, v)| match v { + Value::Bytes(b) if *n == field => Some(*b), + _ => None, + }) +} + +pub fn num(f: &[(u32, Value<'_>)], field: u32) -> Option { + f.iter().find_map(|(n, v)| match v { + Value::Int(x) if *n == field => Some(*x), + _ => None, + }) +} + +pub fn has(f: &[(u32, Value<'_>)], field: u32) -> bool { + f.iter().any(|(n, _)| *n == field) +} + +/// The first length-delimited `field` at any depth up to `depth`, searching breadth first. +pub fn find(b: &[u8], field: u32, depth: u32) -> Option<&[u8]> { + let f = fields(b)?; + if let Some(v) = sub(&f, field) { + return Some(v); + } + if depth == 0 { + return None; + } + f.iter().find_map(|(_, v)| match v { + Value::Bytes(inner) => find(inner, field, depth - 1), + _ => None, + }) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn round_trip() { + let m = Msg::new().uint(1, 300).text(2, "hi").msg(3, Msg::new().uint(4, 1)).float(5, 1.0); + let f = fields(&m.0).unwrap(); + assert_eq!(num(&f, 1), Some(300)); + assert_eq!(sub(&f, 2), Some(&b"hi"[..])); + assert_eq!(num(&fields(sub(&f, 3).unwrap()).unwrap(), 4), Some(1)); + assert_eq!(num(&f, 5), Some(0x3f80_0000)); + } + + #[test] + fn finds_nested_fields() { + let m = Msg::new().msg(1, Msg::new().msg(1, Msg::new().uint(2, 109).text(10, "C:/game.exe"))); + assert_eq!(find(&m.0, 10, 4), Some(&b"C:/game.exe"[..])); + assert_eq!(find(&m.0, 11, 4), None); + } + + #[test] + fn rejects_truncated_input() { + assert!(fields(&[0x0a, 0x05, 1, 2]).is_none()); + } +} diff --git a/tools/ubisoft-launcher/src/win.rs b/tools/ubisoft-launcher/src/win.rs new file mode 100644 index 0000000000..e6953f46d1 --- /dev/null +++ b/tools/ubisoft-launcher/src/win.rs @@ -0,0 +1,189 @@ +//! The handful of kernel32 calls the launcher needs. + +#![allow(non_snake_case, clippy::upper_case_acronyms)] + +use std::ffi::c_void; +use std::os::windows::ffi::OsStrExt; + +pub type HANDLE = *mut c_void; +pub const INVALID: HANDLE = -1isize as HANDLE; +const PIPE_ACCESS_DUPLEX: u32 = 3; +const PIPE_TYPE_BYTE: u32 = 0; +const PIPE_READMODE_BYTE: u32 = 0; +const PIPE_WAIT: u32 = 0; +const PIPE_UNLIMITED_INSTANCES: u32 = 255; +const ERROR_PIPE_CONNECTED: u32 = 535; +const HANDLE_FLAG_INHERIT: u32 = 1; +const SYNCHRONIZE: u32 = 0x0010_0000; +const PROCESS_QUERY_LIMITED_INFORMATION: u32 = 0x1000; +pub const WAIT_OBJECT_0: u32 = 0; +const BUFFER: u32 = 65536; + +#[repr(C)] +struct STARTUPINFOW { + cb: u32, + lpReserved: *mut u16, + lpDesktop: *mut u16, + lpTitle: *mut u16, + dwX: u32, + dwY: u32, + dwXSize: u32, + dwYSize: u32, + dwXCountChars: u32, + dwYCountChars: u32, + dwFillAttribute: u32, + dwFlags: u32, + wShowWindow: u16, + cbReserved2: u16, + lpReserved2: *mut u8, + hStdInput: HANDLE, + hStdOutput: HANDLE, + hStdError: HANDLE, +} + +#[repr(C)] +struct PROCESS_INFORMATION { + hProcess: HANDLE, + hThread: HANDLE, + dwProcessId: u32, + dwThreadId: u32, +} + +#[link(name = "kernel32")] +extern "system" { + fn CreateNamedPipeW(name: *const u16, open: u32, mode: u32, max: u32, out: u32, inp: u32, timeout: u32, sa: *mut c_void) -> HANDLE; + fn ConnectNamedPipe(h: HANDLE, ov: *mut c_void) -> i32; + fn ReadFile(h: HANDLE, buf: *mut u8, n: u32, read: *mut u32, ov: *mut c_void) -> i32; + fn WriteFile(h: HANDLE, buf: *const u8, n: u32, written: *mut u32, ov: *mut c_void) -> i32; + fn FlushFileBuffers(h: HANDLE) -> i32; + fn CloseHandle(h: HANDLE) -> i32; + fn GetLastError() -> u32; + fn CreateEventW(sa: *mut c_void, manual: i32, initial: i32, name: *const u16) -> HANDLE; + fn SetEvent(h: HANDLE) -> i32; + fn SetHandleInformation(h: HANDLE, mask: u32, flags: u32) -> i32; + fn CreateProcessW(app: *const u16, cmd: *mut u16, pa: *mut c_void, ta: *mut c_void, inherit: i32, flags: u32, + env: *mut c_void, cwd: *const u16, si: *mut STARTUPINFOW, pi: *mut PROCESS_INFORMATION) -> i32; + fn WaitForSingleObject(h: HANDLE, ms: u32) -> u32; + fn GetExitCodeProcess(h: HANDLE, code: *mut u32) -> i32; + fn OpenProcess(access: u32, inherit: i32, pid: u32) -> HANDLE; + fn ProcessIdToSessionId(pid: u32, session: *mut u32) -> i32; + fn GetNamedPipeClientProcessId(pipe: HANDLE, pid: *mut u32) -> i32; + fn GetCurrentProcessId() -> u32; +} + +pub fn wide(s: &str) -> Vec { + std::ffi::OsStr::new(s).encode_wide().chain(Some(0)).collect() +} + +pub fn last_error() -> u32 { + unsafe { GetLastError() } +} + +pub fn pid() -> u32 { + unsafe { GetCurrentProcessId() } +} + +pub fn session_id() -> u32 { + let mut s = 0; + if unsafe { ProcessIdToSessionId(GetCurrentProcessId(), &mut s) } == 0 { + return 1; + } + s +} + +pub struct Handle(pub HANDLE); +unsafe impl Send for Handle {} +unsafe impl Sync for Handle {} + +impl Drop for Handle { + fn drop(&mut self) { + if !self.0.is_null() && self.0 != INVALID { + unsafe { CloseHandle(self.0) }; + } + } +} + +impl Handle { + pub fn read(&self, buf: &mut [u8]) -> Option { + let mut n = 0u32; + let ok = unsafe { ReadFile(self.0, buf.as_mut_ptr(), buf.len() as u32, &mut n, std::ptr::null_mut()) }; + (ok != 0 && n > 0).then_some(n as usize) + } + + pub fn write_all(&self, mut data: &[u8]) -> bool { + while !data.is_empty() { + let mut n = 0u32; + if unsafe { WriteFile(self.0, data.as_ptr(), data.len() as u32, &mut n, std::ptr::null_mut()) } == 0 || n == 0 { + return false; + } + data = &data[n as usize..]; + } + unsafe { FlushFileBuffers(self.0) }; + true + } + + pub fn wait(&self, ms: u32) -> bool { + unsafe { WaitForSingleObject(self.0, ms) == WAIT_OBJECT_0 } + } + + pub fn exit_code(&self) -> u32 { + let mut c = 0; + unsafe { GetExitCodeProcess(self.0, &mut c) }; + c + } + + pub fn set(&self) { + unsafe { SetEvent(self.0) }; + } + + pub fn client_pid(&self) -> Option { + let mut pid = 0; + (unsafe { GetNamedPipeClientProcessId(self.0, &mut pid) } != 0 && pid != 0).then_some(pid) + } +} + +/// A new instance of the named pipe, waiting for its client; `None` when it cannot be created. +pub fn accept(name: &str) -> Option { + let w = wide(name); + let h = unsafe { + CreateNamedPipeW(w.as_ptr(), PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT, + PIPE_UNLIMITED_INSTANCES, BUFFER, BUFFER, 0, std::ptr::null_mut()) + }; + if h == INVALID { + return None; + } + let pipe = Handle(h); + if unsafe { ConnectNamedPipe(h, std::ptr::null_mut()) } == 0 && last_error() != ERROR_PIPE_CONNECTED { + return None; + } + Some(pipe) +} + +/// An auto-reset event the started process inherits. +pub fn inheritable_event() -> Handle { + let h = unsafe { CreateEventW(std::ptr::null_mut(), 0, 0, std::ptr::null()) }; + unsafe { SetHandleInformation(h, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT) }; + Handle(h) +} + +pub fn spawn(command_line: &str, cwd: &str) -> Result<(Handle, u32), u32> { + let mut cmd = wide(command_line); + let dir = wide(cwd); + let mut si: STARTUPINFOW = unsafe { std::mem::zeroed() }; + si.cb = std::mem::size_of::() as u32; + let mut pi: PROCESS_INFORMATION = unsafe { std::mem::zeroed() }; + let ok = unsafe { + CreateProcessW(std::ptr::null(), cmd.as_mut_ptr(), std::ptr::null_mut(), std::ptr::null_mut(), 1, 0, + std::ptr::null_mut(), if cwd.is_empty() { std::ptr::null() } else { dir.as_ptr() }, &mut si, &mut pi) + }; + if ok == 0 { + return Err(last_error()); + } + drop(Handle(pi.hThread)); + Ok((Handle(pi.hProcess), pi.dwProcessId)) +} + +pub fn open_process(pid: u32) -> Option { + let h = unsafe { OpenProcess(SYNCHRONIZE | PROCESS_QUERY_LIMITED_INFORMATION, 0, pid) }; + (!h.is_null()).then(|| Handle(h)) +} diff --git a/tools/ubuntu/README.md b/tools/ubuntu/README.md index 85001fe67a..eefff4c6c7 100644 --- a/tools/ubuntu/README.md +++ b/tools/ubuntu/README.md @@ -9,7 +9,8 @@ Steam on the desktop). The app installs it into `files/linuxfs` and runs it unde `/dev/kgsl-3d0`, and KWin with `patches/kwin` (nested desktop scale, an explicit render node, scroll source/discrete forwarding). Both get a `+droiddeck` version. `patches/mesa` adds KGSL UBWC 5/6 (backported from upstream Mesa) and the KGSL device numbers for WSI (from - [Droid-Deck/Drivers](https://github.com/Droid-Deck/Drivers)). + [Droid-Deck/Drivers](https://github.com/Droid-Deck/Drivers)). It also carries Icenowy Zheng's upstream + Zink fix that keeps the Imagination depth/stencil swizzle off colour views (Mesa d55418dae7). - The runtime's built-in Vulkan driver is DroidDeck's DD-Turnip Linux build (`DD_TURNIP_URL` and `DD_TURNIP_SHA256` in `base.env`): Ubuntu's Turnip is diverted to `libvulkan_freedreno.so.distrib` because it lacks the Adreno 8xx fixes. Zink, EGL and llvmpipe stay Ubuntu's. diff --git a/tools/ubuntu/patches/mesa/0005-zink-check-zs-aspect-before-creating-zs-view.patch b/tools/ubuntu/patches/mesa/0005-zink-check-zs-aspect-before-creating-zs-view.patch new file mode 100644 index 0000000000..3a2aa0bd96 --- /dev/null +++ b/tools/ubuntu/patches/mesa/0005-zink-check-zs-aspect-before-creating-zs-view.patch @@ -0,0 +1,41 @@ +From d55418dae753c8daacf66ab81a7481094808576a Mon Sep 17 00:00:00 2001 +From: Icenowy Zheng +Date: Sun, 31 May 2026 21:56:32 +0800 +Subject: [PATCH] zink: check Z/S aspect before creating Z/S image view + +When moving the code of creating image views to the init_sampler_view() +function, the check for Z/S aspect bits was forgotten to be added +(because it was a big if gating a lot of code originally). + +When the driver doesn't have needs_zs_shader_swizzle set, this is not +problematic, because the condition for creating Z/S view is to have only +Z aspect; however the needs_zs_shader_swizzle case now fails because Z/S +views are now created for color images. + +Fix the issue by re-adding the Z/S aspect check before checking +needs_zs_shader_swizzle flag. + +Fixes: cafa22142bf1 ("zink: create views for samplers lazily") +Signed-off-by: Icenowy Zheng +Part-of: +--- + src/gallium/drivers/zink/zink_context.c | 3 ++- + 1 file changed, 2 insertions(+), 1 deletion(-) + +diff --git a/src/gallium/drivers/zink/zink_context.c b/src/gallium/drivers/zink/zink_context.c +index e264e57b2703..41bc1b6932a7 100644 +--- a/src/gallium/drivers/zink/zink_context.c ++++ b/src/gallium/drivers/zink/zink_context.c +@@ -1173,7 +1173,8 @@ init_sampler_view(struct zink_context *ctx, struct zink_sampler_view *sv) + + bool red_depth_sampler_view = false; + if (sv->ivci.subresourceRange.aspectMask == VK_IMAGE_ASPECT_DEPTH_BIT || +- screen->driver_compiler_workarounds.needs_zs_shader_swizzle) { ++ ((sv->ivci.subresourceRange.aspectMask & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) && ++ screen->driver_compiler_workarounds.needs_zs_shader_swizzle)) { + VkComponentSwizzle *swizzle = (VkComponentSwizzle*)&sv->ivci.components; + for (unsigned i = 0; i < 4; i++) { + if (swizzle[i] == VK_COMPONENT_SWIZZLE_ONE || +-- +GitLab +