feat(dual-screen): swap screens - game on the bottom screen, panel on top (#2006)

Swap screens for dual-screen handhelds: the game moves to the built-in
bottom screen while the top screen shows the Dashboard or the KDE desktop.
Swap button in the Dashboard and Desktop bars and a Show the game on
row in the session menu. The desktop resizes to the screen it is on,
Follow screen and Match screen sessions take the bottom screen's shape,
the keyboard opens on the top screen, and the game swaps back on session
end, picture-in-picture, glasses, or when the bottom screen is taken.
Review follow-ups cover desktop restart, refresh pacing, key and pointer
ownership, touch cancellation and bottom surface recovery.
This commit is contained in:
The412Banner authored and GitHub committed 2026-11-02 21:43:22 +00:00
1 parent 3fbd86c89d
commit 8d3616eccd
14 files changed
+515 -71

No files matched your search

@@ -37,6 +37,7 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.runtime.withFrameNanos
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.unit.dp
@@ -294,9 +295,9 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
private fun alignCursorToGuest(): Boolean {
if (pointerLocked || !CompositorHost.isStarted) return false
if (!WaylandCompositor.nativeCursorInputPos(cursorPosBuf)) return false
if (surfaceView.width <= 0 || surfaceView.height <= 0) return false
val x = cursorPosBuf[0] / InputSpace.WIDTH.toFloat() * surfaceView.width
val y = cursorPosBuf[1] / InputSpace.HEIGHT.toFloat() * surfaceView.height
if (outputView().width <= 0 || outputView().height <= 0) return false
val x = cursorPosBuf[0] / InputSpace.WIDTH.toFloat() * outputView().width
val y = cursorPosBuf[1] / InputSpace.HEIGHT.toFloat() * outputView().height
if (kotlin.math.abs(x - cursorPos.x) < 0.75f && kotlin.math.abs(y - cursorPos.y) < 0.75f) return false
cursorPos = androidx.compose.ui.geometry.Offset(x, y)
return true
@@ -421,6 +422,12 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
private var glassesSettings by mutableStateOf(Glasses.settings.value)
private var glassesScreen: GlassesScreen? = null
private var glassesOutputActive by mutableStateOf(false)
private var bottomGame: com.droiddeck.launcher.ui.BottomGameScreen? = null
private var bottomOutputActive by mutableStateOf(false)
private val topDesktopKeys = mutableSetOf<Pair<Int, Int>>()
private var bottomRecoveries = 0
private var gameOnBottom by mutableStateOf(SessionState.gameOnBottom)
private var bottomSwapAvailable by mutableStateOf(false)
private var pausedFrame by mutableStateOf<com.droiddeck.launcher.ui.PausedFrame?>(null)
private var pausedFrameWanted = false
private var pauseAnimated by mutableStateOf(false)
@@ -477,6 +484,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
private var secondScreenPresentation: SecondScreenPresentation? = null
private val bottomScreenListener: () -> Unit = {
if (secondScreenPresentation?.display?.displayId?.let(com.droiddeck.launcher.input.DualScreen::taken) == true) closeSecondScreen(reset = false)
updateBottomGame()
refreshSecondScreenDisplays()
}
@@ -494,6 +502,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
override fun onDisplayChanged(displayId: Int) {
notifyActivityDisplayStateChanged(displayId)
followPanelRefresh(displayId)
updateBottomGame()
refreshSecondScreenDisplays()
}
}
@@ -525,7 +534,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
private fun followPanelRefresh(displayId: Int) {
if (!CompositorHost.isStarted) return
val output = glassesScreen?.display
val output = bottomGame?.takeIf { bottomOutputActive }?.display ?: glassesScreen?.display
?: (if (Build.VERSION.SDK_INT >= 30) display else windowManager.defaultDisplay) ?: return
if (output.displayId != displayId) return
applyOutputPacing()
@@ -585,6 +594,8 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
SessionEvents.begin(this, mode, SessionPaths.label(mode, intent.getStringExtra(SessionService.EXTRA_PROGRAM)))
SessionState.secondScreenAutoOpened = false
SessionState.secondScreenProvisional = false
SessionState.gameOnBottom = false
gameOnBottom = false
}
if (!SessionState.running && SessionState.phase in setOf(SessionPhase.IDLE, SessionPhase.FAILED)) {
SessionState.program = intent.getStringExtra(SessionService.EXTRA_PROGRAM)
@@ -844,9 +855,10 @@ 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) { updatePointerCapture() }
androidx.compose.runtime.LaunchedEffect(drawerOpen, pcKeyboardOpen, achievementsOpen, pipUi, bottomOutputActive) { updatePointerCapture() }
androidx.compose.runtime.LaunchedEffect(bottomOutputActive) { hud.setHidden(pipUi || bottomOutputActive) }
androidx.compose.runtime.LaunchedEffect(pipUi) {
hud.setHidden(pipUi)
hud.setHidden(pipUi || bottomOutputActive)
applyOutputPacing()
if (pipUi != frameGenPip) {
frameGenPip = pipUi
@@ -882,10 +894,11 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
}
} else {
com.droiddeck.launcher.ui.FocusGlideHost(androidx.compose.ui.Modifier.fillMaxSize()) {
CursorOverlay({ cursorPos }, cursorVisible && !uiPointer,
CursorOverlay({ cursorPos }, cursorVisible && !uiPointer && !bottomOutputActive,
resources.displayMetrics.density,
cursorImage, cursorHotX, cursorHotY, cursorImageScale)
if (memoryHud.text.isNotEmpty()) HudText(memoryHud.text, hudColor, hudOpacity)
if (bottomOutputActive && !loading.visible) SwappedTopPanel()
val phonePanel = glassesOutputActive && !loading.visible && !drawerOpen && !pcKeyboardOpen
if (phonePanel) GlassesPhonePanel(glasses, steamVrStatus == SteamVr.Status.RUNNING, headTracking)
if (trackingRecovery != null && !drawerOpen && !pcKeyboardOpen && !loading.visible) {
@@ -1192,6 +1205,9 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
touchAuto = getString(if (usingTouchpad()) R.string.session_touch_auto_touchpad else R.string.session_touch_auto_direct),
fexPreset = fexPreset,
secondScreenMode = secondScreenMode,
gameOnBottom = bottomOutputActive,
gameSwapAvailable = bottomSwapAvailable,
onGameOnBottom = { on -> drawerOpen = false; putGameOnBottom(on) },
dashboardAvailable = bottomPages().isNotEmpty(),
desktopAvailable = com.droiddeck.launcher.session.BottomDesktop.available(),
secondScreenDisplays = secondScreenDisplays,
@@ -2073,7 +2089,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
val live = windowOutputSize(holder.surfaceFrame.width(), holder.surfaceFrame.height())
val size = live ?: if (SessionState.running) previous else glassesOutputSize() ?: outputSize()
SessionState.outputSize = size
if (!SessionState.running) SessionState.refreshHz = refreshHz()
if (!SessionState.running) SessionState.refreshHz = refreshHz(holder)
val panel = panelBounds()
SessionState.upscaleRatio = maxOf(panel.width(), panel.height()).toFloat() / maxOf(size.first, size.second).coerceAtLeast(1)
Thread({
@@ -2123,7 +2139,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
CompositorHost.startOrAttach(
this, holder.surface, runtimeDir.path,
driverId?.let { turnip.driverPath(it) }, driverId?.let { turnip.libraryName(it) },
applicationInfo.nativeLibraryDir, size.first, size.second, refreshHz(), outputFpsLimit(),
applicationInfo.nativeLibraryDir, size.first, size.second, refreshHz(holder), outputFpsLimit(),
)
onOutputAttached(holder)
if (SessionState.running && size != previous) {
@@ -2165,6 +2181,13 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
private fun windowOutputSize(width: Int, height: Int): Pair<Int, Int>? {
if (isInPictureInPictureMode || width <= 0 || height <= 0) return null
if (bottomGame != null) return when {
SessionState.followScreen ->
com.droiddeck.launcher.session.SessionDisplay.followSize(width, height, false, SessionState.followScale)
resolutionChoice() == com.droiddeck.launcher.session.SessionDisplay.MATCH_SCREEN ->
(width and 1.inv()) to (height and 1.inv())
else -> null
}
return if (SessionState.followScreen)
com.droiddeck.launcher.session.SessionDisplay.followSize(width, height, controlsBelowWanted(), SessionState.followScale)
else if (glassesScreen == null) outputSize() else null
@@ -2211,12 +2234,14 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
override fun surfaceDestroyed(holder: SurfaceHolder) {
if (holder !== outputHolder()) return
cancelTouchGestures()
cancelOutputTouches()
surfaceW = 0
surfaceH = 0
glassesOutputActive = false
bottomOutputActive = false
if (CompositorHost.isStarted) WaylandCompositor.nativeSetCursorComposite(false)
CompositorHost.detach(holder.surface)
bottomGame?.takeIf { it.holder === holder }?.let { screen -> uiHandler.post { recoverBottomOutput(screen) } }
}
private fun cancelTouchGestures() {
@@ -2239,19 +2264,24 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
private fun displayMode(): String = SessionPrefs.displayMode(this,
intent.getStringExtra(SessionService.EXTRA_MODE) ?: SessionService.MODE_STEAM, intent.getStringExtra(SessionService.EXTRA_PROGRAM))
private fun outputSize(): Pair<Int, Int> {
private fun outputSize(): Pair<Int, Int> =
com.droiddeck.launcher.session.SessionDisplay.resolveChoice(com.droiddeck.launcher.session.SessionDisplay.panelSize(this),
resolutionChoice(), SessionPrefs.portrait(this) && !controlsBelowWanted())
private fun resolutionChoice(): String {
val mode = displayMode()
val panel = com.droiddeck.launcher.session.SessionDisplay.panelSize(this)
val program = intent.getStringExtra(SessionService.EXTRA_PROGRAM)
val choice = if (intent.getStringExtra(SessionService.EXTRA_MODE) == SessionService.MODE_RUN &&
return if (intent.getStringExtra(SessionService.EXTRA_MODE) == SessionService.MODE_RUN &&
Library.drawsAtPanel(program) && !SessionPrefs.resolutionChosen(this, mode))
com.droiddeck.launcher.session.SessionDisplay.MATCH_SCREEN
else SessionPrefs.resolutionChoice(this, mode, panel)
return com.droiddeck.launcher.session.SessionDisplay.resolveChoice(panel, choice,
SessionPrefs.portrait(this) && !controlsBelowWanted())
else SessionPrefs.resolutionChoice(this, mode, com.droiddeck.launcher.session.SessionDisplay.panelSize(this))
}
private fun refreshHz(): Float {
private fun refreshHz(): Float = refreshHz(null)
private fun refreshHz(holder: SurfaceHolder?): Float {
bottomGame?.takeIf { if (holder != null) holder === it.holder else bottomOutputActive }
?.hz?.takeIf { it > 1f }?.let { return minOf(it, PerfMode.peakRefreshCap(this)) }
glassesScreen?.hz?.takeIf { it > 1f }?.let { return it }
val display = if (Build.VERSION.SDK_INT >= 30) display else windowManager.defaultDisplay
val wanted = window.attributes.preferredDisplayModeId
@@ -2600,6 +2630,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
return true
}
if ((pcKeyboardOpen || achievementsOpen) && 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
if (event.keyCode != KeyEvent.KEYCODE_BACK && padBridge?.onKeyEvent(event) == true) return true
@@ -2611,6 +2642,24 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
return super.dispatchKeyEvent(event)
}
private fun sendToTopDesktop(event: KeyEvent): Boolean {
val key = event.deviceId to event.keyCode
val owned = if (event.action == KeyEvent.ACTION_UP) topDesktopKeys.remove(key) else topDesktopFocused()
if (!owned || !super.dispatchKeyEvent(event)) return false
if (event.action == KeyEvent.ACTION_DOWN) topDesktopKeys += key
return true
}
private fun topDesktopFocused(): Boolean {
if (!bottomOutputActive) return false
var view: View? = currentFocus
while (view != null) {
if (view is com.droiddeck.launcher.ui.BottomDesktopPanel) return true
view = view.parent as? View
}
return false
}
override fun dispatchGenericMotionEvent(event: MotionEvent): Boolean {
if (pipUi) return true
notePress(event)
@@ -2750,7 +2799,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
private fun focusDrawer(): Boolean = dockedMenu()?.focusMenu() ?: sessionOverlay.requestFocus()
private fun dockDrawer(): Boolean {
if (pipUi || !started || glassesScreen != null || !::displayManager.isInitialized) return false
if (pipUi || !started || glassesScreen != null || bottomGame != null || !::displayManager.isInitialized) return false
val target = com.droiddeck.launcher.input.DualScreen.sessionDisplay(this, displayManager, shownOnDisplayId) ?: return false
if (secondScreenDisplays.none { it.id == target.displayId }) return false
val shown = secondScreenPresentation?.display?.displayId
@@ -2865,7 +2914,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
val rect = drawnRect() ?: return
val cx = x.coerceIn(rect.left, rect.right)
val cy = y.coerceIn(rect.top, rect.bottom)
WaylandCompositor.nativeSendPointer(1, InputSpace.x(cx, surfaceView.width), InputSpace.y(cy, surfaceView.height))
WaylandCompositor.nativeSendPointer(1, InputSpace.x(cx, outputView().width), InputSpace.y(cy, outputView().height))
cursorVisible = false
}
override fun onButton(button: Int, pressed: Boolean) {
@@ -2893,7 +2942,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
scrollRestY = 0f
val rect = drawnRect() ?: return
val x = rect.left + rect.width() * PAD_SCROLL_POINTER_X
WaylandCompositor.nativeSendPointer(1, InputSpace.x(x, surfaceView.width), InputSpace.y(rect.centerY(), surfaceView.height))
WaylandCompositor.nativeSendPointer(1, InputSpace.x(x, outputView().width), InputSpace.y(rect.centerY(), outputView().height))
cursorVisible = false
}
override fun onScroll(dx: Float, dy: Float) = scrollListener.onScroll(dx, dy)
@@ -2941,10 +2990,10 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
}
private fun placeTouchpadAtGuest(rect: android.graphics.RectF) {
if (pointerLocked || !CompositorHost.isStarted || surfaceView.width <= 0 || surfaceView.height <= 0) return
if (pointerLocked || !CompositorHost.isStarted || outputView().width <= 0 || outputView().height <= 0) return
if (!WaylandCompositor.nativeCursorInputPos(cursorPosBuf)) return
val x = cursorPosBuf[0] / InputSpace.WIDTH.toFloat() * surfaceView.width
val y = cursorPosBuf[1] / InputSpace.HEIGHT.toFloat() * surfaceView.height
val x = cursorPosBuf[0] / InputSpace.WIDTH.toFloat() * outputView().width
val y = cursorPosBuf[1] / InputSpace.HEIGHT.toFloat() * outputView().height
if (rect.contains(x, y)) touchpad.place(x, y)
}
@@ -2960,7 +3009,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
val rect = drawnRect() ?: return
val cx = x.coerceIn(rect.left, rect.right)
val cy = y.coerceIn(rect.top, rect.bottom)
WaylandCompositor.nativeSendPointer(1, InputSpace.x(cx, surfaceView.width), InputSpace.y(cy, surfaceView.height))
WaylandCompositor.nativeSendPointer(1, InputSpace.x(cx, outputView().width), InputSpace.y(cy, outputView().height))
showCursor(cx, cy)
}
@@ -3025,6 +3074,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
.filter { glassesScreen == null || it.id != glasses.displayId }
val oldIds = secondScreenDisplays.map { it.id }.toSet()
secondScreenDisplays = candidates
bottomSwapAvailable = bottomGame != null || bottomGameDisplay() != null
val newIds = candidates.map { it.id }.toSet()
if (secondScreenMode != SecondScreenMode.NONE && oldIds.isNotEmpty() && selectedSecondScreenDisplay !in newIds) {
closeSecondScreen(reset = true)
@@ -3091,7 +3141,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
closeSecondScreen(reset = false)
return
}
if (com.droiddeck.launcher.input.DualScreen.taken(target.displayId)) {
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
@@ -3133,6 +3183,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
menu = {
DroidDeckTheme { SessionMenu(docked = true) }
},
onSwap = if (com.droiddeck.launcher.input.DualScreen.builtIn(target)) ({ putGameOnBottom(true) }) else null,
dashboard = if (bottomPages().isEmpty()) null else ({
DroidDeckTheme {
BottomDashboard(
@@ -3141,6 +3192,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
onPage = { com.droiddeck.launcher.ui.SessionBottomTab.pick(SessionState.sessionId, it) },
onClose = { selectSecondScreenMode(SecondScreenMode.NONE) },
onMenu = if (dockDrawer()) ({ drawerOpen = true }) else null,
onSwap = if (bottomSwapAvailable && com.droiddeck.launcher.input.DualScreen.builtIn(target)) ({ putGameOnBottom(true) }) else null,
strip = { pageId ->
com.droiddeck.launcher.ui.SessionBottomStrip(pageId) { if (::hud.isInitialized) hud.liveFps else null }
},
@@ -3157,7 +3209,16 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
))
private fun openSettingsBelow(intent: android.content.Intent, done: () -> Boolean) {
val presentation = secondScreenPresentation ?: return
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()) }
@@ -3188,7 +3249,8 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
}
private fun captureScreenshot(onDone: (Boolean) -> Unit) {
val source = if (glassesOutputActive) glassesScreen?.surfaceView else if (::surfaceView.isInitialized) surfaceView else null
val source = if (glassesOutputActive) glassesScreen?.surfaceView else if (bottomOutputActive) bottomGame?.surfaceView
else if (::surfaceView.isInitialized) surfaceView else null
val width = source?.width ?: 0
val height = source?.height ?: 0
if (source == null || width <= 0 || height <= 0 || source.holder.surface?.isValid != true) {
@@ -3252,7 +3314,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
}
private fun controlsBelowWanted(): Boolean {
if (!::sessionRoot.isInitialized || glassesScreen != null || cardboard.enabled) return false
if (!::sessionRoot.isInitialized || glassesScreen != null || bottomGame != null || cardboard.enabled) return false
return SessionPrefs.controlsBelowWanted(this, SessionState.mode, sessionRoot.width, sessionRoot.height,
pipUi || isInPictureInPictureMode)
}
@@ -3298,7 +3360,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
val rect = drawnRect() ?: return
val px = rect.left + nx * rect.width()
val py = rect.top + ny * rect.height()
WaylandCompositor.nativeSendPointer(1, InputSpace.x(px, surfaceView.width), InputSpace.y(py, surfaceView.height))
WaylandCompositor.nativeSendPointer(1, InputSpace.x(px, outputView().width), InputSpace.y(py, outputView().height))
showCursor(px, py)
}
@@ -3463,8 +3525,8 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
}
private fun drawnRect(): android.graphics.RectF? {
val width = surfaceView.width.takeIf { it > 0 } ?: return null
val height = surfaceView.height.takeIf { it > 0 } ?: return null
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])
@@ -3560,7 +3622,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
val cx = x.coerceIn(rect.left, rect.right)
val cy = y.coerceIn(rect.top, rect.bottom)
WaylandCompositor.nativeSendTouch(action, pointerId,
InputSpace.x(cx, surfaceView.width), InputSpace.y(cy, surfaceView.height))
InputSpace.x(cx, outputView().width), InputSpace.y(cy, outputView().height))
showCursor(cx, cy)
}
@@ -3624,7 +3686,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
private fun updatePointerCapture() {
if (!::surfaceView.isInitialized) return
val view = window.decorView
val want = pointerLocked && resumed && hasWindowFocus() && !drawerOpen && !pcKeyboardOpen && !achievementsOpen && !pipUi && mouseAttached()
val want = pointerLocked && resumed && hasWindowFocus() && !drawerOpen && !pcKeyboardOpen && !achievementsOpen && !pipUi && !bottomOutputActive && mouseAttached()
if (want == view.hasPointerCapture()) return
if (want) view.requestPointerCapture() else view.releasePointerCapture()
}
@@ -3632,7 +3694,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
private var platformPointerShown: Boolean? = null
private fun appUiOwnsPointer(): Boolean =
drawerOpen || pcKeyboardOpen || achievementsOpen || pipUi || glassesOutputActive
drawerOpen || pcKeyboardOpen || achievementsOpen || pipUi || glassesOutputActive || bottomOutputActive
private fun applyPlatformPointer(shown: Boolean) {
if (!::sessionRoot.isInitialized || !::surfaceView.isInitialized) return
@@ -3855,7 +3917,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
}
private fun capturePausedFrame(manual: Boolean = false) {
if (!SessionState.running || pipUi || isInPictureInPictureMode || glassesOutputActive) return
if (!SessionState.running || pipUi || isInPictureInPictureMode || glassesOutputActive || bottomOutputActive) return
if (SessionState.suspended && pausedFrame != null) return
if ((suspendPolicy == SessionPrefs.SUSPEND_NEVER && !manual && !SessionState.manualPause) || !::surfaceView.isInitialized) return
if (surfaceView.holder.surface?.isValid != true) return
@@ -3970,6 +4032,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
if (!inPip && surfaceW > 0) resizeOutputForWindow(surfaceW, surfaceH)
applyUnfoldedControls()
updateGlassesScreen()
updateBottomGame()
}
override fun startActivityForResult(intent: Intent, requestCode: Int, options: Bundle?) {
@@ -3999,6 +4062,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
SessionService.setActivityVisible(this, true, sessionActivityDisplayId)
Glasses.rescan()
updateGlassesScreen()
updateBottomGame()
updateHandCamera()
}
@@ -4013,6 +4077,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
closeSecondScreen(reset = false)
startedScreens = (startedScreens - 1).coerceAtLeast(0)
updateGlassesScreen()
updateBottomGame()
if (startedScreens == 0) SessionService.setActivityVisible(this, false, sessionActivityDisplayId)
super.onStop()
}
@@ -4322,15 +4387,20 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
glassesScreen?.trackingRecovery?.update(trackingRecovery, glasses.sbs)
}
private fun outputHolder(): SurfaceHolder = glassesScreen?.holder ?: surfaceView.holder
private fun outputHolder(): SurfaceHolder = glassesScreen?.holder ?: bottomGame?.holder ?: surfaceView.holder
private fun outputView(): SurfaceView = bottomGame?.takeIf { bottomOutputActive }?.surfaceView ?: surfaceView
private fun onOutputAttached(holder: SurfaceHolder) {
if (holder !== outputHolder()) return
glassesOutputActive = glassesScreen?.holder === holder
if (CompositorHost.isStarted) WaylandCompositor.nativeSetCursorComposite(glassesOutputActive)
surfaceView.visibility = if (glassesOutputActive) View.INVISIBLE else View.VISIBLE
bottomOutputActive = bottomGame?.holder === holder
if (bottomOutputActive) bottomRecoveries = 0
if (CompositorHost.isStarted) WaylandCompositor.nativeSetCursorComposite(glassesOutputActive || bottomOutputActive)
surfaceView.visibility = if (glassesOutputActive || bottomOutputActive) View.INVISIBLE else View.VISIBLE
Log.i(TAG, "compositor output attached: " +
if (glassesOutputActive) "glasses display ${glassesScreen?.display?.displayId}" else "phone")
if (glassesOutputActive) "glasses display ${glassesScreen?.display?.displayId}"
else if (bottomOutputActive) "bottom screen display ${bottomGame?.display?.displayId}" else "phone")
if (pausedFrameWanted) uiHandler.postDelayed({
if (pausedFrameWanted && pausedFrame == null && resumed) capturePausedFrame()
}, PAUSED_FRAME_REDRAW_MS)
@@ -4348,6 +4418,140 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
if (surfaceView.holder.surface?.isValid == true) surfaceCreated(surfaceView.holder)
}
private fun bottomGameDisplay(): Display? {
if (!::displayManager.isInitialized) return null
return com.droiddeck.launcher.input.DualScreen.sessionDisplay(this, displayManager, shownOnDisplayId)
?.takeIf { it.isValid && com.droiddeck.launcher.input.DualScreen.builtIn(it) }
}
private fun putGameOnBottom(on: Boolean) {
if (on && bottomGame == null && bottomGameDisplay() == null) {
android.widget.Toast.makeText(this, R.string.swap_screens_unavailable, android.widget.Toast.LENGTH_SHORT).show()
return
}
SessionState.gameOnBottom = on
gameOnBottom = on
Log.i(TAG, "game screen: " + if (on) "bottom" else "top")
updateBottomGame()
}
private fun updateBottomGame() {
if (!::surfaceView.isInitialized) return
val target = bottomGameDisplay()
val want = com.droiddeck.launcher.ui.GameScreenSwap.wanted(
requested = gameOnBottom,
activityStarted = started && !runtimeRemovalBlocked,
sessionEnded = ended,
inPip = pipUi || isInPictureInPictureMode,
glassesOutput = glassesScreen != null,
bottomScreenFree = target != null,
)
val shown = if (want) target else null
val current = bottomGame
if (current != null && current.display.displayId == shown?.displayId) return
if (current != null || shown != null) cancelOutputTouches()
if (current != null) {
restoreTopOutput(current)
runCatching { current.dismiss() }
}
if (shown != null) {
closeSecondScreen(reset = false)
val screen = com.droiddeck.launcher.ui.BottomGameScreen(this, shown, this, ::onBottomGameTouch)
bottomGame = screen
surfaceW = 0
surfaceH = 0
screen.setOnDismissListener {
if (bottomGame === screen) {
restoreTopOutput(screen)
refreshSecondScreenDisplays()
}
}
try {
screen.show()
} catch (e: Exception) {
restoreTopOutput(screen)
runCatching { screen.dismiss() }
Log.w(TAG, "could not show the game on ${shown.name}", e)
}
if (bottomGame === screen) Log.i(TAG, "requested game output on the bottom screen (display ${shown.displayId})")
}
refreshSecondScreenDisplays()
}
private fun cancelOutputTouches() {
cancelTouchGestures()
if (::directTouchHold.isInitialized) directTouchHold.cancel()
if (::twoFingerGestures.isInitialized) twoFingerGestures.cancel()
if (CompositorHost.isStarted && deliversTouches(routedTouchMode)) WaylandCompositor.nativeSendTouch(3, -1, 0, 0)
}
private fun recoverBottomOutput(screen: com.droiddeck.launcher.ui.BottomGameScreen) {
if (bottomGame !== screen || bottomOutputActive || !started || screen.holder.surface?.isValid == true) return
Log.i(TAG, "bottom screen surface gone - game back on the top screen")
restoreTopOutput(screen)
runCatching { screen.dismiss() }
refreshSecondScreenDisplays()
if (++bottomRecoveries <= BOTTOM_RECOVERY_TRIES) uiHandler.postDelayed({ updateBottomGame() }, BOTTOM_RECOVERY_MS)
}
private fun restoreTopOutput(screen: com.droiddeck.launcher.ui.BottomGameScreen) {
if (bottomGame !== screen) return
bottomGame = null
bottomOutputActive = false
surfaceW = 0
surfaceH = 0
CompositorHost.detach(screen.holder.surface)
if (glassesScreen != null) return
surfaceView.visibility = View.VISIBLE
applyOutputPacing()
if (surfaceView.holder.surface?.isValid == true) surfaceCreated(surfaceView.holder)
}
private fun onBottomGameTouch(event: MotionEvent): Boolean {
if (pipUi) return true
if (!event.isFromSource(InputDevice.SOURCE_TOUCHSCREEN)) return false
notePress(event)
if (event.actionMasked == MotionEvent.ACTION_DOWN) {
drawerControllerActive = false
gestureTouchStreamGate.reset()
}
if (!gestureTouchStreamGate.accepts(GestureTouchStreamGate.Source.SURFACE, event)) return false
return onTouchEvent(event)
}
@androidx.compose.runtime.Composable
private fun SwappedTopPanel() {
val pages = bottomPages()
val swapBack = { putGameOnBottom(false) }
androidx.compose.foundation.layout.Box(
Modifier.fillMaxSize().background(androidx.compose.material3.MaterialTheme.colorScheme.background)
.pointerInput(Unit) { awaitPointerEventScope { while (true) awaitPointerEvent() } },
) {
if (secondScreenMode == SecondScreenMode.DESKTOP && com.droiddeck.launcher.session.BottomDesktop.available()) {
androidx.compose.ui.viewinterop.AndroidView(
factory = { context ->
com.droiddeck.launcher.ui.BottomDesktopPanel(context, null, { drawerOpen = true },
(({ tapSteamButton() }) to ({ tapQam() })).takeIf { SessionState.mode == SessionService.MODE_STEAM }, swapBack,
fullResolution = true)
},
modifier = Modifier.fillMaxSize(),
)
} else if (pages.isNotEmpty()) BottomDashboard(
pages, com.droiddeck.launcher.ui.SessionBottomTab.picked(SessionState.sessionId),
gameStarts = gameStarts,
onPage = { com.droiddeck.launcher.ui.SessionBottomTab.pick(SessionState.sessionId, it) },
onClose = null,
onMenu = { drawerOpen = true },
onSwap = swapBack,
strip = { pageId ->
com.droiddeck.launcher.ui.SessionBottomStrip(pageId) { if (::hud.isInitialized) hud.liveFps else null }
},
) else androidx.compose.material3.Button(onClick = swapBack, modifier = Modifier.align(androidx.compose.ui.Alignment.Center)) {
androidx.compose.material3.Text(getString(R.string.swap_screens))
}
}
}
private fun glassesOutputSize(): Pair<Int, Int>? {
if (glassesScreen == null || glasses.width <= 0 || glasses.height <= 0) return null
val panel = (if (glasses.sbs) glasses.width / 2 else glasses.width) to glasses.height
@@ -4405,6 +4609,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
runCatching { current.dismiss() }
}
if (target != null) {
bottomGame?.let { restoreTopOutput(it); runCatching { it.dismiss() } }
val screen = GlassesScreen(this, target, Glasses.outputHz(), this, Glasses.outputResolution())
glassesScreen = screen
applyOutputPacing()
@@ -4430,6 +4635,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
applyOutputPacing()
refreshSecondScreenDisplays()
applyCardboard(cardboard)
if (target == null) updateBottomGame()
}
private fun onGlassesSteamVr(now: SteamVr.Status) {
@@ -4541,6 +4747,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
closeSecondScreen(reset = false)
glassesScope.cancel()
glassesScreen?.let { screen -> glassesScreen = null; runCatching { screen.dismiss() } }
bottomGame?.let { screen -> bottomGame = null; runCatching { screen.dismiss() } }
headTracker?.stop()
if (::hud.isInitialized) hud.stop()
if (::memoryHud.isInitialized) memoryHud.stop()
@@ -4626,6 +4833,8 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
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"
private var startedScreens = 0
@Volatile private var activeInstallWorker: Any? = null