diff --git a/docs/android-apps-in-steam.md b/docs/android-apps-in-steam.md index 54474d43a1..348216c14e 100644 --- a/docs/android-apps-in-steam.md +++ b/docs/android-apps-in-steam.md @@ -4,7 +4,7 @@ On Android 15 or newer, open DroidDeck’s **Apps** tab. If it’s hidden, show The app runs on a separate Android display. Steam shows that display and forwards touch gestures and gamepad input. Android’s system Back stays with the handheld display, so it can open DroidDeck’s menu. Apps with controller support see an Xbox 360 pad. The app plays sound through Android, so use the handheld's media volume. -- Use the DroidDeck menu button for session controls and **Exit Game** in Steam to close the shortcut. +- Exiting the Android app returns you to Steam. You can also use **Exit Game** in Steam to close the shortcut. - To go back inside the Android app with a keyboard, press **Alt+B**. DroidDeck's Back and the session menu are separate controls. - You can run one Android shortcut at a time. Work profile apps and apps that block screen capture or refuse secondary displays may be unavailable. - If the shortcut reports a disconnected device after Wireless debugging has restarted, restart Steam in DroidDeck to refresh the connection. If it asks for an updated Linux runtime, install the runtime update before trying again. diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-android-app b/tools/linuxfs/overlay/usr/local/bin/droiddeck-android-app index 11e85265a1..fd3067b58b 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-android-app +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-android-app @@ -1,5 +1,5 @@ -#!/bin/sh -set -eu +#!/bin/bash +set -euo pipefail fail() { printf 'droiddeck-android-app: %s\n' "$1" >&2 @@ -32,7 +32,12 @@ exec 9>"${XDG_RUNTIME_DIR:-/tmp}/droiddeck-android-app.lock" flock -n 9 || fail "Close the running Android app shortcut first." # The ADB server must not inherit the lock. adb start-server 9>&- || fail "Could not start the Android connection." -trap 'adb kill-server 9>&- >/dev/null 2>&1 || true' EXIT +output= +cleanup() { + adb kill-server 9>&- >/dev/null 2>&1 || true + [ -z "$output" ] || rm -f "$output" +} +trap cleanup EXIT trap 'exit 0' INT TERM HUP connected() { timeout 8 adb connect "$serial" 9>&- >/dev/null 2>&1 || true @@ -41,9 +46,11 @@ connected() { connected || connected || fail "Android is disconnected. Turn on Wireless debugging, then restart Steam." +output=$(mktemp) export SDL_VIDEODRIVER=x11 # Android plays audio on the handheld. Capturing it here would feed it back into Android again. scrcpy --serial "$serial" --new-display=1280x720/240 --start-app="$package" \ --window-title="Android app" --no-vd-system-decorations --no-clipboard-autosync --no-mouse-hover \ - --mouse-bind=----:---- --gamepad=uhid --no-audio --max-fps=60 9>&- || + --mouse-bind=----:---- --gamepad=uhid --no-audio --max-fps=60 9>&- 2>&1 | tee "$output" || + grep -Fq '[server] INFO: DroidDeck Android shortcut ended' "$output" || fail "The Android app window closed unexpectedly. Try launching it again." diff --git a/tools/ubuntu/build-scrcpy-server.sh b/tools/ubuntu/build-scrcpy-server.sh index 7f5e20c3bd..e422080232 100755 --- a/tools/ubuntu/build-scrcpy-server.sh +++ b/tools/ubuntu/build-scrcpy-server.sh @@ -15,6 +15,7 @@ curl -fsSL --retry 3 --proto '=https' -o "$work/source.tar.gz" \ echo "03b72d1f71ca7783cff2995ca7cd8c67b9b75038a3cc35e443a1913e951b49dd $work/source.tar.gz" | sha256sum -c - tar xzf "$work/source.tar.gz" -C "$work" --strip-components=1 git -C "$work" apply "$here/patches/scrcpy-display-focus.patch" +git -C "$work" apply "$here/patches/scrcpy-shortcut-exit.patch" src=$work/server/src/main mkdir -p "$work/generated/com/genymobile/scrcpy" "$work/classes" "$(dirname "$out")" for file in "$src"/aidl/android/*/*.aidl; do diff --git a/tools/ubuntu/patches/scrcpy-shortcut-exit.patch b/tools/ubuntu/patches/scrcpy-shortcut-exit.patch new file mode 100644 index 0000000000..346c1550dc --- /dev/null +++ b/tools/ubuntu/patches/scrcpy-shortcut-exit.patch @@ -0,0 +1,95 @@ +--- a/server/src/main/java/com/genymobile/scrcpy/control/Controller.java ++++ b/server/src/main/java/com/genymobile/scrcpy/control/Controller.java +@@ -691,6 +691,9 @@ + + Ln.i("Starting app \"" + app.getName() + "\" [" + app.getPackageName() + "] on display " + startAppDisplayId + "..."); + Device.startApp(app.getPackageName(), startAppDisplayId, forceStopBeforeStart); ++ if (displayId == Device.DISPLAY_ID_NONE && Build.VERSION.SDK_INT >= AndroidVersions.API_35_ANDROID_15) { ++ AndroidShortcutMonitor.start(startAppDisplayId); ++ } + } + + private int getStartAppDisplayId() { +--- /dev/null ++++ b/server/src/main/java/com/genymobile/scrcpy/control/AndroidShortcutMonitor.java +@@ -0,0 +1,80 @@ ++package com.genymobile.scrcpy.control; ++ ++import com.genymobile.scrcpy.device.Device; ++import com.genymobile.scrcpy.util.Ln; ++ ++import android.os.Handler; ++import android.os.Looper; ++ ++import java.lang.reflect.Field; ++import java.lang.reflect.Method; ++import java.util.List; ++ ++final class AndroidShortcutMonitor implements Runnable { ++ private static final int ACTIVITY_TYPE_HOME = 2; ++ private static final long POLL_MS = 1000; ++ ++ private final Handler handler = new Handler(Looper.getMainLooper()); ++ private final int displayId; ++ private final Object manager; ++ private final Method getTasks; ++ private final Field topActivity; ++ private final Field activityType; ++ private final Field visible; ++ private boolean opened; ++ private int emptyPolls; ++ ++ static void start(int displayId) { ++ try { ++ AndroidShortcutMonitor monitor = new AndroidShortcutMonitor(displayId); ++ monitor.handler.post(monitor); ++ } catch (ReflectiveOperationException e) { ++ Ln.w("Could not watch Android shortcut tasks", e); ++ } ++ } ++ ++ private AndroidShortcutMonitor(int displayId) throws ReflectiveOperationException { ++ this.displayId = displayId; ++ Class taskManager = Class.forName("android.app.ActivityTaskManager"); ++ Class service = Class.forName("android.app.IActivityTaskManager"); ++ Class task = Class.forName("android.app.TaskInfo"); ++ manager = taskManager.getMethod("getService").invoke(null); ++ getTasks = service.getMethod("getAllRootTaskInfosOnDisplay", int.class); ++ topActivity = task.getField("topActivity"); ++ activityType = task.getField("topActivityType"); ++ visible = task.getField("isVisible"); ++ } ++ ++ private boolean hasVisibleApp() throws ReflectiveOperationException { ++ for (Object task : (List) getTasks.invoke(manager, displayId)) { ++ if (visible.getBoolean(task) && topActivity.get(task) != null && activityType.getInt(task) != ACTIVITY_TYPE_HOME) { ++ return true; ++ } ++ } ++ return false; ++ } ++ ++ @Override ++ public void run() { ++ try { ++ if (!Device.isScreenOn(0)) { ++ emptyPolls = 0; ++ } else if (hasVisibleApp()) { ++ opened = true; ++ emptyPolls = 0; ++ } else if (++emptyPolls >= (opened ? 3 : 15)) { ++ if (opened) { ++ Ln.i("DroidDeck Android shortcut ended"); ++ } else { ++ Ln.w("Android shortcut did not open a visible activity"); ++ } ++ handler.getLooper().quitSafely(); ++ return; ++ } ++ } catch (ReflectiveOperationException e) { ++ Ln.w("Could not watch Android shortcut tasks", e); ++ return; ++ } ++ handler.postDelayed(this, POLL_MS); ++ } ++}