fix(dual-screen): allow game swap to external second screens (#2350)

* fix(dual-screen): allow game swap to external second screens

* fix(dual-screen): name external screens primary and secondary in the game screen choice

* Keep the selected game screen in step with display changes and session mode

* Refresh the game output when the screen is picked automatically

* Keep the game screen when controls open automatically

* Keep the external game screen across restarts and show the controls on the primary screen

* fix(dual-screen): preserve selected game display

* fix(dual-screen): retain fallback selection and keyboard access

* fix(input): clear field keyboard ownership on transitions

* fix(dual-screen): route keyboard when selected display is occupied

* fix(dual-screen): enable keyboard fallback routing

* fix(dual-screen): defer keyboard requests until focus returns

* fix(dual-screen): recheck keyboard destination on focus return and ignore stale hide events

* fix(dual-screen): end guest keyboard ownership on real dismissals
This commit is contained in:
MaxsTechReview authored and GitHub committed 2026-11-17 11:22:33 -05:00
1 parent 913cb85c92
commit 2ff890f0ec
8 files changed
+380 -24

No files matched your search

@@ -541,6 +541,9 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
private var lastMouseY = Float.NaN
private var padType by mutableStateOf(SessionPrefs.CONTROLLER_DECK)
private var pcKeyboardOpen by mutableStateOf(false)
private var fieldKeyboardOnPrimary = false
private var pendingFieldKeyboard = false
private var fieldKeyboardImeShown = false
private var achievementsInFront by mutableStateOf<AchievementsInFront?>(null)
private var achievementsOpen by mutableStateOf(false)
private var hudOn by mutableStateOf(true)
@@ -658,11 +661,13 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
override fun onDisplayAdded(displayId: Int) {
notifyActivityDisplayStateChanged(displayId)
refreshSecondScreenDisplays()
updateBottomGame()
}
override fun onDisplayRemoved(displayId: Int) {
if (refreshActivityDisplayId() && started) {
SessionService.setActivityDisplay(this@SessionActivity, sessionActivityDisplayId)
}
updateBottomGame()
refreshSecondScreenDisplays()
}
override fun onDisplayChanged(displayId: Int) {
@@ -874,10 +879,10 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
root.addView(controls)
}
keyboard = KeyboardHost(this).also { host ->
host.onImeChanged = { updateDesktopKeyboardButton() }
host.onImeChanged = ::onPrimaryImeChanged
root.addView(host)
}
desktopKeyboardButton = com.droiddeck.launcher.input.DesktopKeyboardButton(this) { keyboard?.show() }
desktopKeyboardButton = com.droiddeck.launcher.input.DesktopKeyboardButton(this) { showPrimaryKeyboard() }
.also { root.addView(it) }
touchpad = TouchpadGestures(PointerGestures.slop(this), pointerListener)
directTouchHold = DirectTouchHold(PointerGestures.slop(this), pointerListener, directTouchSink)
@@ -1093,6 +1098,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
if (AgentGuest.keyboardDismissals != keyboardDismissalsSeen[0]) {
keyboardDismissalsSeen[0] = AgentGuest.keyboardDismissals
secondScreenPresentation?.closeFieldKeyboard()
dismissFieldKeyboard()
}
}
androidx.compose.runtime.LaunchedEffect(picturePosition, integerScaling, SessionState.running, pipUi, cardboard.enabled) { applyGameAlignment() }
@@ -1315,7 +1321,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
}
if (pcKeyboardOpen) com.droiddeck.launcher.ui.PcKeyboard(
sendKey = { code, down -> if (CompositorHost.isStarted) sendGuestKey(code, down, 32) },
onAndroidKeyboard = { pcKeyboardOpen = false; keyboard?.toggle() },
onAndroidKeyboard = { pcKeyboardOpen = false; togglePrimaryKeyboard() },
onClose = { pcKeyboardOpen = false },
)
if (!drawerDocked) SessionMenu(
@@ -1439,6 +1445,8 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
secondScreenNone = !bottomOutputActive && !dockDrawer(),
gameOnBottom = bottomOutputActive,
gameSwapAvailable = bottomSwapAvailable,
gameSwapBuiltIn = !::displayManager.isInitialized || displayManager.getDisplay(selectedSecondScreenDisplay)
?.let(com.droiddeck.launcher.input.DualScreen::builtIn) ?: true,
onGameOnBottom = { on -> drawerOpen = false; putGameOnBottom(on) },
dashboardAvailable = bottomPages().isNotEmpty(),
desktopAvailable = com.droiddeck.launcher.session.BottomDesktop.available(),
@@ -1632,7 +1640,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
pickLossless.launch(InAppFilePicker.buildIntent(
this@SessionActivity, listOf("dll"), getString(R.string.lsfg_pick_title)))
},
onKeyboard = { drawerOpen = false; pcKeyboardOpen = false; keyboard?.toggle() },
onKeyboard = { drawerOpen = false; pcKeyboardOpen = false; togglePrimaryKeyboard() },
onHardwareKeyboard = ::togglePcKeyboard,
wowChatShown = com.droiddeck.launcher.ui.WowChat.isWow(AgentGuest.gameProgram),
wowChat = wowChat.remembered,
@@ -1863,7 +1871,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
val all = com.droiddeck.launcher.input.VrControllers.Emulate.entries
com.droiddeck.launcher.input.VrControllers.setEmulate(this, all[(pads.emulate.ordinal + 1) % all.size])
}) else null,
onKeyboard = { keyboard?.toggle() },
onKeyboard = { togglePrimaryKeyboard() },
onSessionMenu = { drawerOpen = true },
onStopSteamVr = if (vr) ({ steamVrAction("stop") }) else null,
brightness = glasses.brightness,
@@ -1904,12 +1912,44 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
private fun togglePcKeyboard() {
drawerOpen = false
keyboard?.hide()
hidePrimaryKeyboard()
onScreenControls?.releaseAll()
bottomInputControls?.releaseAll()
pcKeyboardOpen = !pcKeyboardOpen
}
private fun showPrimaryKeyboard() {
pendingFieldKeyboard = false
fieldKeyboardOnPrimary = false
keyboard?.show()
}
private fun togglePrimaryKeyboard() {
pendingFieldKeyboard = false
fieldKeyboardOnPrimary = false
keyboard?.toggle()
}
private fun hidePrimaryKeyboard() {
pendingFieldKeyboard = false
fieldKeyboardOnPrimary = false
keyboard?.hide()
}
private fun onPrimaryImeChanged(visible: Boolean) {
if (visible) fieldKeyboardImeShown = true
else if (fieldKeyboardImeShown) {
fieldKeyboardImeShown = false
fieldKeyboardOnPrimary = false
}
updateDesktopKeyboardButton()
}
private fun dismissFieldKeyboard() {
pendingFieldKeyboard = false
if (fieldKeyboardOnPrimary) hidePrimaryKeyboard()
}
private fun updateControllerPrefs(change: () -> Unit) {
change()
controllerSettings = ControllerPrefs.read(this)
@@ -3660,7 +3700,6 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
val target = com.droiddeck.launcher.input.DualScreen.sessionDisplay(this, displayManager, shownOnDisplayId) ?: return false
return try {
val presentation = secondScreenPresentation ?: secondScreenPresentation(target).also {
selectedSecondScreenDisplay = target.displayId
secondScreenPresentation = it
}
if (!presentation.isShowing) presentation.show()
@@ -4019,16 +4058,23 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
val candidates = SecondScreenDisplays.available(displayManager, primaryId)
.filter { glassesScreen == null || it.id != glasses.displayId }
val oldIds = secondScreenDisplays.map { it.id }.toSet()
val oldSelectedDisplay = selectedSecondScreenDisplay
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) {
bottomSwapAvailable = bottomGame != null
closeSecondScreen(reset = true)
SessionState.secondScreenAutoOpened = false
return
}
if (selectedSecondScreenDisplay !in newIds) selectedSecondScreenDisplay = candidates.firstOrNull()?.id ?: -1
selectedSecondScreenDisplay = SecondScreenDisplays.selection(selectedSecondScreenDisplay, candidates.map { it.id }, gameOnBottom) {
SessionState.secondScreenDisplay = it
}
if (selectedSecondScreenDisplay != oldSelectedDisplay) {
updateBottomGame()
}
bottomSwapAvailable = bottomGame != null || bottomGameDisplay() != null
if (secondScreenMode != SecondScreenMode.NONE && candidates.isEmpty()) {
closeSecondScreen(reset = true)
SessionState.secondScreenAutoOpened = false
@@ -4050,12 +4096,22 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
private fun openDualScreenControls() {
if (SessionState.secondScreenAutoOpened || secondScreenMode != SecondScreenMode.NONE || pipUi) return
if (gameOnBottom && selectedSecondScreenDisplay >= 0 && secondScreenDisplays.none { it.id == selectedSecondScreenDisplay }) return
if (!SessionPrefs.dualScreenSessionControls(this)) return
val target = com.droiddeck.launcher.input.DualScreen.sessionDisplay(this, displayManager, shownOnDisplayId) ?: return
if (secondScreenDisplays.none { it.id == target.displayId }) return
val gameDisplay = bottomGame?.display?.displayId
?: if (SessionState.gameOnBottom) bottomGameDisplay()?.displayId else null
SessionState.secondScreenAutoOpened = true
selectedSecondScreenDisplay = target.displayId
selectSecondScreenMode(openingPanel())
if (gameDisplay != null && gameDisplay != target.displayId && secondScreenDisplays.any { it.id == gameDisplay }) {
panelMode(openingPanel())
} else {
val selectionChanged = selectedSecondScreenDisplay != target.displayId
selectedSecondScreenDisplay = target.displayId
SessionState.secondScreenDisplay = target.displayId
if (selectionChanged) updateBottomGame()
selectSecondScreenMode(openingPanel())
}
automaticPanel = secondScreenMode != SecondScreenMode.NONE
dualControlsProvisional = ::loading.isInitialized && loading.visible
}
@@ -4081,6 +4137,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
if (secondScreenDisplays.none { it.id == displayId }) return
selectedSecondScreenDisplay = displayId
SessionState.secondScreenDisplay = displayId
updateBottomGame()
if (secondScreenMode != SecondScreenMode.NONE) showSecondScreen(secondScreenMode)
}
@@ -4099,6 +4156,10 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
return
}
if (selectedSecondScreenDisplay !in secondScreenDisplays.map { it.id }) {
if (gameOnBottom && selectedSecondScreenDisplay >= 0) {
panelMode(mode)
return
}
selectedSecondScreenDisplay = secondScreenDisplays.first().id
}
showSecondScreen(mode)
@@ -4177,7 +4238,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
)
}
},
onSwap = if (bottomSwapAvailable && com.droiddeck.launcher.input.DualScreen.builtIn(target)) ({ putGameOnBottom(true) }) else null,
onSwap = if (bottomSwapAvailable) ({ putGameOnBottom(true) }) else null,
onDeckPanel = ::selectDeckPanel,
dashboard = if (bottomPages().isEmpty()) null else ({
DroidDeckTheme {
@@ -4421,6 +4482,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
if (!started) return null
if (secondScreenPresentation?.showsKeyboard != true && dockDrawer()) {
val target = com.droiddeck.launcher.input.DualScreen.sessionDisplay(this, displayManager, shownOnDisplayId) ?: return null
if (gameOnBottom && selectedSecondScreenDisplay != target.displayId) return null
if (drawerOpen || drawerPinned) drawerOpen = false
typingReturnMode = secondScreenMode
selectedSecondScreenDisplay = target.displayId
@@ -4430,16 +4492,37 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
}
private fun openBottomKeyboard() {
val presentation = bottomKeyboardPresentation() ?: return
val presentation = bottomKeyboardPresentation()
if (presentation == null) {
if (keyboardBelongsOnPrimary()) {
pendingFieldKeyboard = true
retryFieldKeyboard(hasWindowFocus())
}
return
}
pendingFieldKeyboard = false
if (fieldKeyboardOnPrimary) hidePrimaryKeyboard()
presentation.requestKeyboard(forField = true)
com.droiddeck.launcher.ui.SessionTyping.focusDisplay(this, presentation.display.displayId)
}
private fun keyboardBelongsOnPrimary() = SecondScreenDisplays.keyboardOnPrimary(gameOnBottom, selectedSecondScreenDisplay,
secondScreenDisplays.map { it.id }, taken = com.droiddeck.launcher.input.DualScreen::taken)
private fun retryFieldKeyboard(hasFocus: Boolean) {
if (!pendingFieldKeyboard || !hasFocus || !resumed || pipUi) return
pendingFieldKeyboard = false
if (!keyboardBelongsOnPrimary()) return
fieldKeyboardOnPrimary = true
fieldKeyboardImeShown = keyboard?.hideMeansDismissal == true
keyboard?.show()
}
private fun onGameTextField(focused: Boolean) {
if (!focused || !com.droiddeck.launcher.ui.WowChat.isWow(AgentGuest.gameProgram)) wowChat.chatClosed()
else if (resumed && !pipUi && !pcKeyboardOpen &&
resources.configuration.hardKeyboardHidden != android.content.res.Configuration.HARDKEYBOARDHIDDEN_NO) {
keyboard?.takeIf { it.shown }?.hide()
hidePrimaryKeyboard()
wowChat.chatOpened()
}
}
@@ -4526,9 +4609,13 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
}
private fun updateKeyboardRoute() {
AgentGuest.routeKeyboard(this, SessionState.mode == SessionService.MODE_STEAM &&
(secondScreenPresentation?.showsKeyboard == true || dockDrawer()) &&
SessionPrefs.androidTextFieldKeyboard(this))
AgentGuest.routeKeyboard(this, SecondScreenDisplays.routeKeyboard(
SessionState.mode == SessionService.MODE_STEAM,
SessionPrefs.androidTextFieldKeyboard(this),
secondScreenPresentation?.showsKeyboard == true,
dockDrawer(),
SecondScreenDisplays.keyboardOnPrimary(gameOnBottom, selectedSecondScreenDisplay,
secondScreenDisplays.map { it.id }, resumed && !pipUi, com.droiddeck.launcher.input.DualScreen::taken)))
}
private fun dropDockedDrawer() {
@@ -5427,6 +5514,8 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
private fun showPipUi(show: Boolean) {
pipUi = show
updateKeyboardRoute()
if (!show) retryFieldKeyboard(hasWindowFocus())
if (show) {
if (::hud.isInitialized) hud.setHidden(true)
drawerOpen = false
@@ -5446,7 +5535,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
guestKeysDown.toList().forEach { sendGuestKey(it, false) }
heldKeys.clear()
heldButtons.clear()
keyboard?.hide()
hidePrimaryKeyboard()
sessionClipboard.stop()
padMotion?.stop()
cursorVisible = false
@@ -5589,6 +5678,8 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
onScreenControls?.reload()
controllerSettings = ControllerPrefs.read(this)
resumed = true
updateKeyboardRoute()
retryFieldKeyboard(hasWindowFocus())
updateDesktopKeyboardButton()
com.droiddeck.launcher.input.HomeButtonShortcuts.attach(this,
enabled = {
@@ -5662,9 +5753,12 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
if (::scrollTouch.isInitialized) scrollTouch.cancel()
if (::padMouse.isInitialized) padMouse.reset()
resumed = false
updateKeyboardRoute()
updatePointerCapture()
updatePadMotion()
keyboard?.takeIf { it.shown }?.hide()
val pendingKeyboard = pendingFieldKeyboard
hidePrimaryKeyboard()
pendingFieldKeyboard = pendingKeyboard
SessionPrefs.sessionPrefs(this).unregisterOnSharedPreferenceChangeListener(cardboardPrefListener)
SessionPrefs.sessionPrefs(this).unregisterOnSharedPreferenceChangeListener(screenEffectsProfilePrefListener)
ControllerPrefs.prefs(this).unregisterOnSharedPreferenceChangeListener(shortcutPrefListener)
@@ -5910,9 +6004,10 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
}
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) }
if (!::displayManager.isInitialized || SessionPrefs.dualScreenSessionMode(this) == com.droiddeck.launcher.input.DualScreen.MODE_OFF) return null
return com.droiddeck.launcher.input.DualScreen.selectedSessionDisplay(
this, displayManager, shownOnDisplayId, selectedSecondScreenDisplay,
)
}
private fun putGameOnBottom(on: Boolean) {
@@ -6038,7 +6133,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
tabs = tabs,
)
val keyboardExtra = com.droiddeck.launcher.ui.BarExtra(getString(R.string.second_show_keyboard), icon = R.drawable.ic_bottom_keyboard) {
keyboard?.toggle()
togglePrimaryKeyboard()
}
val profileLayout = secondaryInputLayout
androidx.compose.foundation.layout.Box(
@@ -6235,6 +6330,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
releaseHeldGuestInput()
}
if (hasFocus && !pipUi) {
retryFieldKeyboard(true)
goFullscreen()
refreshClipboard()
}
@@ -82,6 +82,12 @@ object DualScreen {
fun sessionDisplay(context: Context, displayManager: DisplayManager, primaryDisplayId: Int): Display? =
display(context, displayManager, primaryDisplayId, SessionPrefs.dualScreenSessionMode(context))
fun selectedSessionDisplay(context: Context, displayManager: DisplayManager, primaryDisplayId: Int, selectedDisplayId: Int): Display? =
displayManager.getDisplay(selectedDisplayId)?.takeIf {
SessionPrefs.dualScreenSessionMode(context) != MODE_OFF && it.isValid && it.displayId != primaryDisplayId &&
(it.flags and Display.FLAG_PRESENTATION) != 0 && !taken(it.displayId)
}
fun display(context: Context, displayManager: DisplayManager, primaryDisplayId: Int, mode: String = SessionPrefs.dualScreenMode(context)): Display? =
when (mode) {
MODE_OFF -> null
@@ -26,6 +26,8 @@ class KeyboardHost(
var imeShowing = false
private set
var onImeChanged: ((Boolean) -> Unit)? = null
val hideMeansDismissal: Boolean
get() = Build.VERSION.SDK_INT < Build.VERSION_CODES.R || (shown && imeShowing)
init {
isFocusable = false
@@ -40,6 +40,18 @@ enum class SecondScreenMode(val id: String, @StringRes val label: Int, val bindi
data class SecondScreenDisplay(val id: Int, val label: String)
object SecondScreenDisplays {
fun selection(selected: Int, available: List<Int>, gameRequested: Boolean): Int =
if (selected in available || gameRequested && selected >= 0) selected else available.firstOrNull() ?: -1
fun selection(selected: Int, available: List<Int>, gameRequested: Boolean, save: (Int) -> Unit): Int =
selection(selected, available, gameRequested).also { if (it != selected) save(it) }
fun keyboardOnPrimary(gameRequested: Boolean, selected: Int, available: List<Int>, visible: Boolean = true, taken: (Int) -> Boolean): Boolean =
visible && gameRequested && selected >= 0 && (selected !in available || taken(selected))
fun routeKeyboard(steam: Boolean, enabled: Boolean, panel: Boolean, dock: Boolean, primaryFallback: Boolean): Boolean =
steam && enabled && (panel || dock || primaryFallback)
fun available(displayManager: DisplayManager, primaryDisplayId: Int = Display.DEFAULT_DISPLAY): List<SecondScreenDisplay> =
displayManager.getDisplays(DisplayManager.DISPLAY_CATEGORY_PRESENTATION)
.asSequence()
@@ -262,6 +262,7 @@ class DrawerActions(
val secondScreenNone: Boolean = true,
val gameOnBottom: Boolean = false,
val gameSwapAvailable: Boolean = false,
val gameSwapBuiltIn: Boolean = true,
val onGameOnBottom: (Boolean) -> Unit = {},
val dashboardAvailable: Boolean = false,
val desktopAvailable: Boolean = false,
@@ -1303,7 +1304,8 @@ private fun SecondScreenGroup(host: MenuHost, a: DrawerActions, track: (String)
}
if (a.gameSwapAvailable || a.gameOnBottom) SettingsGroup(stringResource(R.string.swap_game_screen_group)) {
ChoiceRow(host, "game-screen", stringResource(R.string.swap_game_screen), null,
listOf(false to stringResource(R.string.swap_game_screen_top), true to stringResource(R.string.swap_game_screen_bottom)),
if (a.gameSwapBuiltIn) listOf(false to stringResource(R.string.swap_game_screen_top), true to stringResource(R.string.swap_game_screen_bottom))
else listOf(false to stringResource(R.string.main_primary), true to stringResource(R.string.main_secondary)),
a.gameOnBottom, chipModifier = track("game-screen"), onPick = a.onGameOnBottom)
}
}