diff --git a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt index c334972670..fb344c1943 100644 --- a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt @@ -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>() + private var bottomRecoveries = 0 + private var gameOnBottom by mutableStateOf(SessionState.gameOnBottom) + private var bottomSwapAvailable by mutableStateOf(false) private var pausedFrame by mutableStateOf(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? { 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 { + private fun outputSize(): Pair = + 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? { 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 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 923ee29996..36b9c3dfdc 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/BottomDesktop.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/BottomDesktop.kt @@ -61,7 +61,9 @@ object BottomDesktop { /** The proot to freeze with the session when it pauses. */ val suspendPid: Int get() = guestPid private var guestGeneration = 0 - private var outputSize = 0 to 0 + var outputSize = 0 to 0 + private set + private var attachedSurface: Surface? = null private var cursorShown = false /** The session generation whose launch the running compositor and guest belong to; -1 when none. */ private var owner = -1 @@ -97,16 +99,8 @@ object BottomDesktop { fun available(): Boolean = launch?.installed == true && SessionState.running && SessionState.mode != SessionService.MODE_DESKTOP - /** - * The desktop renders below the panel's resolution: about 720 pixels on the short side keeps - * Plasma's text big enough to touch and its software rendering cheap. A running desktop keeps its size. - */ - fun outputSizeFor(width: Int, height: Int): Pair { - if ((state == State.STARTING || state == State.RUNNING) && outputSize.first > 0) return outputSize - if (width <= 0 || height <= 0) return 0 to 0 - val scale = maxOf(1f, Math.round(minOf(width, height) / TARGET_SHORT_SIDE * 2f) / 2f) - return ((width / scale).toInt() and 1.inv()) to ((height / scale).toInt() and 1.inv()) - } + fun outputSizeFor(width: Int, height: Int, fullResolution: Boolean = false): Pair = + desktopOutputSize(width, height, fullResolution, TARGET_SHORT_SIDE) fun addListener(listener: () -> Unit) { listeners.add(listener) } @@ -148,6 +142,7 @@ object BottomDesktop { appContext = context.applicationContext owner = current.generation outputSize = width to height + attachedSurface = surface val dir = File(current.sessionDir, "bottom-desktop").apply { mkdirs() } val msg = Message.obtain(null, Comp.MSG_ATTACH).apply { arg1 = owner @@ -169,7 +164,19 @@ object BottomDesktop { } } - fun detach() { + fun resize(surface: Surface, width: Int, height: Int) { + if (surface !== attachedSurface || width <= 0 || height <= 0 || outputSize == (width to height)) return + outputSize = width to height + pendingAttach?.data?.apply { + putInt(Comp.KEY_WIDTH, width) + putInt(Comp.KEY_HEIGHT, height) + } + service?.let { send(it, Message.obtain(null, Comp.MSG_RESIZE, width, height)) } + } + + fun detach(surface: Surface? = null) { + if (surface != null && attachedSurface != null && surface !== attachedSurface) return + attachedSurface = null pendingAttach = null service?.let { send(it, Message.obtain(null, Comp.MSG_DETACH)) } } @@ -351,3 +358,9 @@ object BottomDesktop { }, "bottom-desktop-start").start() } } + +internal fun desktopOutputSize(width: Int, height: Int, fullResolution: Boolean, targetShortSide: Float): Pair { + if (width <= 0 || height <= 0) return 0 to 0 + val scale = if (fullResolution) 1f else maxOf(1f, Math.round(minOf(width, height) / targetShortSide * 2f) / 2f) + return ((width / scale).toInt() and 1.inv()) to ((height / scale).toInt() and 1.inv()) +} 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 8e63d05c67..2e66d2b80f 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionState.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionState.kt @@ -93,6 +93,7 @@ object SessionState { @Volatile var secondScreenDisplay = -1 @Volatile var secondScreenAutoOpened = false @Volatile var secondScreenProvisional = false + @Volatile var gameOnBottom = false @Volatile var steamLibraryMounts: Map = emptyMap() diff --git a/app/src/main/java/com/droiddeck/launcher/ui/BottomDashboard.kt b/app/src/main/java/com/droiddeck/launcher/ui/BottomDashboard.kt index d5991b5c09..f6be827cf5 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/BottomDashboard.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/BottomDashboard.kt @@ -20,6 +20,7 @@ import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material.icons.Icons import androidx.compose.material.icons.outlined.Close import androidx.compose.material.icons.outlined.Menu +import androidx.compose.material.icons.outlined.SwapVert import androidx.compose.material3.Icon import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Text @@ -84,10 +85,11 @@ fun BottomDashboard( pages: List, remembered: String?, onPage: (String) -> Unit, - onClose: () -> Unit, + onClose: (() -> Unit)?, onMenu: (() -> Unit)?, gameStarts: Int = 0, strip: (@Composable (String?) -> Unit)? = null, + onSwap: (() -> Unit)? = null, ) { val colors = MaterialTheme.colorScheme var current by remember { mutableStateOf(pickBottomPage(remembered, pages.map { it.id })) } @@ -100,7 +102,7 @@ fun BottomDashboard( val labels = bottomTabsWithLabels(maxWidth.value, pages.size) Column(Modifier.fillMaxSize().padding(horizontal = 12.dp, vertical = 10.dp), verticalArrangement = Arrangement.spacedBy(10.dp)) { Row(Modifier.fillMaxWidth().height(48.dp), verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(8.dp)) { - BarButton(Icons.Outlined.Close, stringResource(R.string.deck_close), "bottom-close", onClose) + if (onClose != null) BarButton(Icons.Outlined.Close, stringResource(R.string.deck_close), "bottom-close", onClose) Row(Modifier.weight(1f).horizontalScroll(rememberScrollState()), horizontalArrangement = Arrangement.spacedBy(6.dp)) { pages.forEach { p -> PageTab(p, p.id == page?.id, labels) { @@ -109,6 +111,7 @@ fun BottomDashboard( } } } + if (onSwap != null) BarButton(Icons.Outlined.SwapVert, stringResource(R.string.swap_screens), "bottom-swap", onSwap) if (onMenu != null) BarButton(Icons.Outlined.Menu, stringResource(R.string.touch_droiddeck_menu), "bottom-menu", onMenu) } strip?.invoke(page?.id) diff --git a/app/src/main/java/com/droiddeck/launcher/ui/BottomDesktopPanel.kt b/app/src/main/java/com/droiddeck/launcher/ui/BottomDesktopPanel.kt index d6b80ac813..0b838b8f1a 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/BottomDesktopPanel.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/BottomDesktopPanel.kt @@ -4,6 +4,7 @@ import android.annotation.SuppressLint import android.content.Context import android.content.res.Configuration import android.graphics.Color +import android.os.Build import android.os.SystemClock import android.graphics.Typeface import android.view.Gravity @@ -13,6 +14,7 @@ import android.view.SurfaceHolder import android.view.SurfaceView import android.view.View import android.view.ViewConfiguration +import android.view.WindowInsets import android.graphics.RectF import androidx.compose.runtime.mutableStateOf import androidx.compose.ui.platform.ComposeView @@ -33,9 +35,11 @@ import com.droiddeck.launcher.session.BottomDesktop @SuppressLint("ViewConstructor") class BottomDesktopPanel( context: Context, - onClose: () -> Unit, + onClose: (() -> Unit)?, onMenu: (() -> Unit)?, steamButtons: Pair<() -> Unit, () -> Unit>?, + onSwap: (() -> Unit)? = null, + private val fullResolution: Boolean = false, ) : LinearLayout(context), SurfaceHolder.Callback { private lateinit var surfaceView: SurfaceView private val area = FrameLayout(context).apply { clipChildren = true } @@ -66,7 +70,7 @@ class BottomDesktopPanel( setBackgroundColor(Color.BLACK) isFocusable = true isFocusableInTouchMode = true - addView(bar(onClose, onMenu, steamButtons), LayoutParams(-1, -2)) + addView(bar(onClose, onMenu, steamButtons, onSwap), LayoutParams(-1, -2)) area.addView(keyboardHost) addSurface() area.addView(status, FrameLayout.LayoutParams(-1, -1)) @@ -83,7 +87,7 @@ class BottomDesktopPanel( area.addView(surfaceView, 1, FrameLayout.LayoutParams(fullWidth.takeIf { it > 0 } ?: -1, fullHeight.takeIf { it > 0 } ?: -1)) } - private fun bar(onClose: () -> Unit, onMenu: (() -> Unit)?, steamButtons: Pair<() -> Unit, () -> Unit>?): View { + private fun bar(onClose: (() -> Unit)?, onMenu: (() -> Unit)?, steamButtons: Pair<() -> Unit, () -> Unit>?, onSwap: (() -> Unit)?): View { val bar = LinearLayout(context).apply { orientation = HORIZONTAL gravity = Gravity.CENTER_VERTICAL @@ -91,7 +95,7 @@ class BottomDesktopPanel( setBackgroundColor(Color.BLACK) } fun gap() = View(context).apply { layoutParams = LayoutParams(dp(6), 1) } - bar.addView(DeckControlsPanel.roundButton(context, "×", context.getString(R.string.deck_close), onClose)) + onClose?.let { bar.addView(DeckControlsPanel.roundButton(context, "×", context.getString(R.string.deck_close), it)) } title = TextView(context).apply { textSize = 14f typeface = Typeface.create(Typeface.DEFAULT, Typeface.BOLD) @@ -106,6 +110,10 @@ class BottomDesktopPanel( bar.addView(gap()) bar.addView(DeckControlsPanel.iconButton(context, R.drawable.ic_bottom_keyboard, context.getString(R.string.second_show_keyboard)) { keyboardHost.toggle() }) + onSwap?.let { + bar.addView(gap()) + bar.addView(DeckControlsPanel.iconButton(context, R.drawable.ic_bottom_swap, context.getString(R.string.swap_screens), it)) + } onMenu?.let { bar.addView(gap()) bar.addView(DeckControlsPanel.menuButton(context, it)) @@ -187,7 +195,9 @@ class BottomDesktopPanel( addSurface() return } - if (attached) attach(surfaceView.holder) + if (!attached) return + attached = false + syncSurface(surfaceView.holder) } private fun showState() { @@ -208,13 +218,28 @@ class BottomDesktopPanel( if (w != fullWidth || h > fullHeight || !keyboardHost.shown) { fullWidth = w fullHeight = h - surfaceView.layoutParams = FrameLayout.LayoutParams(w, h) + val (desktopW, desktopH) = BottomDesktop.outputSizeFor(w, h, fullResolution) + val (fitW, fitH) = if (w - desktopW in 0..1 && h - desktopH in 0..1) desktopW to desktopH + else fitDesktop(w, h, desktopW, desktopH) + surfaceView.layoutParams = if (fitW == w && fitH == h) FrameLayout.LayoutParams(w, h) + else FrameLayout.LayoutParams(fitW, fitH, Gravity.CENTER_HORIZONTAL).apply { topMargin = (h - fitH) / 2 } + surfaceView.post { syncSurface(surfaceView.holder) } } pan() } + private fun imeOverlap(): Int { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) return 0 + if (area.height < fullHeight) return 0 + val ime = rootWindowInsets?.getInsets(WindowInsets.Type.ime())?.bottom ?: return 0 + if (ime <= 0) return 0 + val at = IntArray(2) + area.getLocationInWindow(at) + return (at[1] + area.height - (rootView.height - ime)).coerceIn(0, area.height) + } + private fun pan() { - val visible = area.height + val visible = area.height - imeOverlap() val hidden = (fullHeight - visible).coerceAtLeast(0) val anchor = if (directTouch) lastTouchY else trackpad.y surfaceView.translationY = if (hidden == 0 || !keyboardHost.shown) 0f @@ -223,24 +248,27 @@ class BottomDesktopPanel( override fun surfaceCreated(holder: SurfaceHolder) = Unit - override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) { - if (!attached) attach(holder) - } + override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) = syncSurface(holder) override fun surfaceDestroyed(holder: SurfaceHolder) { if (holder !== surfaceView.holder) return attached = false surfaceUsed = false - BottomDesktop.detach() + BottomDesktop.detach(holder.surface) } - private fun attach(holder: SurfaceHolder) { - val frame = holder.surfaceFrame - val (width, height) = BottomDesktop.outputSizeFor(fullWidth.takeIf { it > 0 } ?: frame.width(), - fullHeight.takeIf { it > 0 } ?: frame.height()) + private fun syncSurface(holder: SurfaceHolder) { + if (holder !== surfaceView.holder || fullWidth <= 0 || fullHeight <= 0 || !holder.surface.isValid) return + val (width, height) = BottomDesktop.outputSizeFor(fullWidth, fullHeight, fullResolution) if (width <= 0 || height <= 0) return + val frame = holder.surfaceFrame if (frame.width() != width || frame.height() != height) { - holder.setFixedSize(width, height) + val params = surfaceView.layoutParams + if (params.width == width && params.height == height) holder.setSizeFromLayout() else holder.setFixedSize(width, height) + return + } + if (attached) { + BottomDesktop.resize(holder.surface, width, height) return } attached = true @@ -299,7 +327,7 @@ class BottomDesktopPanel( override fun onWheel(steps: Int) = BottomDesktop.sendScene(SCENE_WHEEL, steps) override fun onScroll(dx: Float, dy: Float) { - val (outW, outH) = BottomDesktop.outputSizeFor(surfaceView.width, surfaceView.height) + val (outW, outH) = BottomDesktop.outputSize scrollRestX += dx * outW / surfaceView.width.coerceAtLeast(1) * AXIS_UNITS_PER_PIXEL scrollRestY += dy * outH / surfaceView.height.coerceAtLeast(1) * AXIS_UNITS_PER_PIXEL val h = scrollRestX.toInt() @@ -343,3 +371,11 @@ class BottomDesktopPanel( const val TEXT_FIELD_TAP_MS = 1_000L } } + +internal fun fitDesktop(areaW: Int, areaH: Int, desktopW: Int, desktopH: Int): Pair { + if (areaW <= 0 || areaH <= 0 || desktopW <= 0 || desktopH <= 0) return areaW to areaH + val widthLimited = areaW.toLong() * desktopH <= areaH.toLong() * desktopW + val w = if (widthLimited) areaW else (desktopW.toLong() * areaH / desktopH).toInt() + val h = if (widthLimited) (desktopH.toLong() * areaW / desktopW).toInt() else areaH + return if (areaW - w <= 4 && areaH - h <= 4) areaW to areaH else w to h +} diff --git a/app/src/main/java/com/droiddeck/launcher/ui/BottomGameScreen.kt b/app/src/main/java/com/droiddeck/launcher/ui/BottomGameScreen.kt new file mode 100644 index 0000000000..ba5b5af126 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/ui/BottomGameScreen.kt @@ -0,0 +1,66 @@ +package com.droiddeck.launcher.ui + +import android.app.Activity +import android.app.Presentation +import android.graphics.Color +import android.os.Bundle +import android.view.Display +import android.view.KeyEvent +import android.view.MotionEvent +import android.view.SurfaceHolder +import android.view.SurfaceView +import android.view.View +import android.view.ViewGroup +import android.view.WindowManager +import android.widget.FrameLayout + +class BottomGameScreen( + private val activity: Activity, + display: Display, + private val callback: SurfaceHolder.Callback, + private val onTouch: (MotionEvent) -> Boolean, +) : Presentation(activity, display) { + val surfaceView = SurfaceView(getContext()) + val holder: SurfaceHolder get() = surfaceView.holder + + val hz: Float get() = display.mode.refreshRate + + init { + setCancelable(false) + setCanceledOnTouchOutside(false) + } + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + val root = FrameLayout(context).apply { setBackgroundColor(Color.BLACK) } + root.addView(surfaceView, FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)) + setContentView(root) + window?.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON or WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) + @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) + surfaceView.setOnTouchListener { _, event -> onTouch(event) } + surfaceView.holder.addCallback(callback) + } + + override fun dispatchKeyEvent(event: KeyEvent): Boolean = + activity.dispatchKeyEvent(event) || super.dispatchKeyEvent(event) + + override fun dispatchGenericMotionEvent(event: MotionEvent): Boolean = + activity.dispatchGenericMotionEvent(event) || super.dispatchGenericMotionEvent(event) +} + +object GameScreenSwap { + fun wanted( + requested: Boolean, + activityStarted: Boolean, + sessionEnded: Boolean, + inPip: Boolean, + glassesOutput: Boolean, + bottomScreenFree: Boolean, + ): Boolean = requested && activityStarted && !sessionEnded && !inPip && !glassesOutput && bottomScreenFree +} 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 df4182e0de..26251ddada 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/SecondScreenPresentation.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/SecondScreenPresentation.kt @@ -58,6 +58,7 @@ class SecondScreenPresentation( private val onMenu: (() -> Unit)? = null, private val menu: (@androidx.compose.runtime.Composable () -> Unit)? = null, private val dashboard: (@androidx.compose.runtime.Composable () -> Unit)? = null, + private val onSwap: (() -> Unit)? = null, ) : Presentation(sessionActivity, display) { private val frame = FrameLayout(context) private var menuView: androidx.compose.ui.platform.ComposeView? = null @@ -246,7 +247,7 @@ class SecondScreenPresentation( SecondScreenMode.DESKTOP -> { ownComposeViews() root.addView(BottomDesktopPanel(context, onClose, onMenu, - (onSteamMenu to onQam).takeIf { SessionState.mode == SessionService.MODE_STEAM }), weightParams()) + (onSteamMenu to onQam).takeIf { SessionState.mode == SessionService.MODE_STEAM }, onSwap), weightParams()) } SecondScreenMode.NONE -> Unit } 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 8bcf13c7de..4ecf45256f 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/SessionOverlay.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/SessionOverlay.kt @@ -245,6 +245,9 @@ class DrawerActions( val textureAnisotropy: Int? = 0, val textureLodBias: String? = TextureFiltering.LOD_BIAS_OFF, val secondScreenMode: SecondScreenMode, + val gameOnBottom: Boolean = false, + val gameSwapAvailable: Boolean = false, + val onGameOnBottom: (Boolean) -> Unit = {}, val dashboardAvailable: Boolean = false, val desktopAvailable: Boolean = false, val secondScreenDisplays: List, @@ -1176,6 +1179,11 @@ private fun SecondScreenGroup(host: MenuHost, a: DrawerActions, track: (String) a.secondScreenDisplays.map { it.id to it.label }, a.selectedSecondScreenDisplay, chipModifier = track("second-screen-display"), onPick = a.onSecondScreenDisplay) } + 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)), + a.gameOnBottom, chipModifier = track("game-screen"), onPick = a.onGameOnBottom) + } } @Composable diff --git a/app/src/main/java/com/droiddeck/launcher/wayland/BottomDesktopCompositorService.kt b/app/src/main/java/com/droiddeck/launcher/wayland/BottomDesktopCompositorService.kt index 59c182dd35..26faded94c 100644 --- a/app/src/main/java/com/droiddeck/launcher/wayland/BottomDesktopCompositorService.kt +++ b/app/src/main/java/com/droiddeck/launcher/wayland/BottomDesktopCompositorService.kt @@ -20,12 +20,15 @@ class BottomDesktopCompositorService : Service() { private var client: Messenger? = null /** Echoed in every reply, so the session process can tell replies to an earlier attach apart. */ @Volatile private var attachTag = 0 + private var outputWidth = 0 + private var outputHeight = 0 private val handler = object : Handler(Looper.getMainLooper()) { override fun handleMessage(msg: Message) { when (msg.what) { MSG_ATTACH -> attach(msg) MSG_DETACH -> CompositorHost.detach(null) + MSG_RESIZE -> resize(msg.arg1, msg.arg2) MSG_TOUCH -> WaylandCompositor.nativeSendTouch(msg.arg1 and 0xff, msg.arg1 shr 8, msg.arg2 ushr 16, msg.arg2 and 0xffff) MSG_KEY -> WaylandCompositor.nativeSendKey(msg.arg1, msg.arg2) @@ -75,15 +78,26 @@ class BottomDesktopCompositorService : Service() { } val turnip = com.droiddeck.launcher.gpu.TurnipDriver(this) val driverId = if (CompositorHost.isStarted) null else turnip.install() + val resized = CompositorHost.isStarted && (width != outputWidth || height != outputHeight) val started = CompositorHost.startOrAttach( this, surface, runtimeDir.path, driverId?.let { turnip.driverPath(it) }, driverId?.let { turnip.libraryName(it) }, applicationInfo.nativeLibraryDir, width, height, data.getFloat(KEY_REFRESH, 60f), 0, ) + outputWidth = width + outputHeight = height + if (resized) WaylandCompositor.nativeResizeOutput(width, height) Log.i(TAG, (if (started) "compositor started" else "compositor attached") + " at ${width}x$height in ${runtimeDir.path}") reply(MSG_ATTACHED) } + private fun resize(width: Int, height: Int) { + if (width <= 0 || height <= 0 || !CompositorHost.isStarted || (width == outputWidth && height == outputHeight)) return + outputWidth = width + outputHeight = height + WaylandCompositor.nativeResizeOutput(width, height) + } + private fun reply(what: Int) { val target = client ?: return runCatching { target.send(Message.obtain(null, what, attachTag, 0)) } @@ -102,6 +116,7 @@ class BottomDesktopCompositorService : Service() { const val MSG_POINTER = 8 const val MSG_SCENE = 9 const val MSG_CURSOR = 10 + const val MSG_RESIZE = 11 const val MSG_ATTACHED = 101 const val MSG_FIRST_FRAME = 102 const val KEY_RUNTIME_DIR = "runtimeDir" diff --git a/app/src/main/res/drawable/ic_bottom_swap.xml b/app/src/main/res/drawable/ic_bottom_swap.xml new file mode 100644 index 0000000000..e8ef8120df --- /dev/null +++ b/app/src/main/res/drawable/ic_bottom_swap.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 6f88608575..556d23d95a 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1903,6 +1903,12 @@ The desktop couldn\'t start. Its logs are in this session\'s bottom-desktop folder. The desktop is closed. Nothing to show here yet. + Swap screens + The bottom screen is not free right now + Game screen + Show the game on + Top screen + Bottom screen Downloads No downloads running. Steam diff --git a/app/src/test/java/com/droiddeck/launcher/session/BottomDesktopSizeTest.kt b/app/src/test/java/com/droiddeck/launcher/session/BottomDesktopSizeTest.kt new file mode 100644 index 0000000000..bafa252400 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/session/BottomDesktopSizeTest.kt @@ -0,0 +1,25 @@ +package com.droiddeck.launcher.session + +import org.junit.Assert.assertEquals +import org.junit.Test + +class BottomDesktopSizeTest { + @Test fun theDuosBottomPanelKeepsItsPixels() { + assertEquals(1280 to 848, desktopOutputSize(1280, 848, false, 720f)) + } + + @Test fun aLargerPanelRendersBelowItsResolution() { + assertEquals(826 to 646, desktopOutputSize(1240, 970, false, 720f)) + assertEquals(1280 to 644, desktopOutputSize(1920, 968, false, 720f)) + } + + @Test fun theTopScreenAfterASwapGetsItsOwnPixelsAndTheWideShape() { + assertEquals(1920 to 968, desktopOutputSize(1920, 968, true, 720f)) + assertEquals(1920 to 968, desktopOutputSize(1920, 969, true, 720f)) + } + + @Test fun noPanelNoSize() { + assertEquals(0 to 0, desktopOutputSize(0, 848, true, 720f)) + assertEquals(0 to 0, desktopOutputSize(1280, 0, false, 720f)) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/ui/BottomDesktopFitTest.kt b/app/src/test/java/com/droiddeck/launcher/ui/BottomDesktopFitTest.kt new file mode 100644 index 0000000000..42922163a8 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/ui/BottomDesktopFitTest.kt @@ -0,0 +1,21 @@ +package com.droiddeck.launcher.ui + +import org.junit.Assert.assertEquals +import org.junit.Test + +class BottomDesktopFitTest { + @Test fun aDesktopOfThePanelsShapeFillsThePanel() { + assertEquals(1280 to 870, fitDesktop(1280, 870, 1280, 870)) + assertEquals(1280 to 870, fitDesktop(1280, 870, 1278, 868)) + } + + @Test fun aDesktopOfAnotherShapeIsFittedNotStretched() { + val (w, h) = fitDesktop(1920, 1016, 1280, 870) + assertEquals(1016, h) + assertEquals(1494, w) + } + + @Test fun nothingToFitLeavesThePanelSize() { + assertEquals(1920 to 1016, fitDesktop(1920, 1016, 0, 0)) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/ui/GameScreenSwapTest.kt b/app/src/test/java/com/droiddeck/launcher/ui/GameScreenSwapTest.kt new file mode 100644 index 0000000000..04ff32d59c --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/ui/GameScreenSwapTest.kt @@ -0,0 +1,35 @@ +package com.droiddeck.launcher.ui + +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Test + +class GameScreenSwapTest { + private fun wanted( + requested: Boolean = true, + activityStarted: Boolean = true, + sessionEnded: Boolean = false, + inPip: Boolean = false, + glassesOutput: Boolean = false, + bottomScreenFree: Boolean = true, + ) = GameScreenSwap.wanted(requested, activityStarted, sessionEnded, inPip, glassesOutput, bottomScreenFree) + + @Test fun thePickedSwapPutsTheGameOnTheBottomScreen() { + assertTrue(wanted()) + } + + @Test fun withoutAPickTheGameStaysOnTop() { + assertFalse(wanted(requested = false)) + } + + @Test fun theGameComesBackToTheTopWhenTheSessionIsHiddenOrEnds() { + assertFalse(wanted(activityStarted = false)) + assertFalse(wanted(sessionEnded = true)) + assertFalse(wanted(inPip = true)) + } + + @Test fun glassesAndAHeldBottomScreenWin() { + assertFalse(wanted(glassesOutput = true)) + assertFalse(wanted(bottomScreenFree = false)) + } +}