package com.steamdeck.launcher import android.app.Activity import android.os.Build import android.os.Bundle import android.os.Handler import android.hardware.input.InputManager import android.os.Environment import android.os.Looper import android.util.Log import android.view.KeyEvent import android.view.MotionEvent import android.view.SurfaceHolder import android.view.SurfaceView import android.view.View import android.view.WindowManager import android.widget.FrameLayout import com.steamdeck.launcher.core.FileUtils import com.steamdeck.launcher.gpu.FrameGen import com.steamdeck.launcher.gpu.TurnipDriver import com.steamdeck.launcher.input.EvdevKeys import com.steamdeck.launcher.input.OnScreenControls import com.steamdeck.launcher.input.PadBridge import com.steamdeck.launcher.runtime.LinuxRuntime import com.steamdeck.launcher.session.LoadingPanel import com.steamdeck.launcher.session.PerfHud import com.steamdeck.launcher.session.SessionDrawer import com.steamdeck.launcher.session.SessionPrefs import com.steamdeck.launcher.session.SessionService import com.steamdeck.launcher.session.SessionState import com.steamdeck.launcher.wayland.CompositorHost import com.steamdeck.launcher.wayland.WaylandCompositor import java.io.File /** * The session's screen: our Wayland compositor presenting onto this activity's Surface, and the * input that reaches it. The session itself — gamescope, the Steam client, audio — belongs to * [SessionService] and keeps running when this activity does not exist, which is what lets the * user leave Big Picture for another app and come back to it still signed in and still * downloading. * * So there are only three jobs here: hold a Surface under the compositor, forward input, and show * the session's milestones until its first frame arrives. */ class SessionActivity : Activity(), SurfaceHolder.Callback { private lateinit var surfaceView: SurfaceView private lateinit var loading: LoadingPanel private lateinit var hud: PerfHud private lateinit var drawer: SessionDrawer private var padBridge: PadBridge? = null private var onScreenControls: OnScreenControls? = null private var watching = true /** * Shows the on-screen pad when nothing is plugged in and takes it away the moment something * is — a user with a controller in their hands should not be looking at buttons they cannot * press, and a user without one must not be left with no way to answer Big Picture. */ private val deviceListener = object : InputManager.InputDeviceListener { override fun onInputDeviceAdded(deviceId: Int) = updateOnScreenControls() override fun onInputDeviceRemoved(deviceId: Int) = updateOnScreenControls() override fun onInputDeviceChanged(deviceId: Int) = updateOnScreenControls() } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) goFullscreen() if (!LinuxRuntime.isInstalled(this)) { Log.e(TAG, "the Linux runtime is not installed") finish() return } val root = FrameLayout(this) surfaceView = SurfaceView(this) surfaceView.holder.addCallback(this) root.addView(surfaceView) // Until the client's first frame arrives there is nothing on screen for a minute or more // on a first run, so the session's own progress is shown instead of a black panel. val panel = layoutInflater.inflate(R.layout.session_loading, root, false) root.addView(panel) loading = LoadingPanel(panel) // The performance line, above everything but the loading panel; it starts with the // client's first frame, since before that there is nothing to count. hud = PerfHud(this) root.addView(hud.view, root.indexOfChild(panel)) // Back opens this; it sits above everything, the loading panel included. drawer = SessionDrawer(this, root, onHudChanged = { hud.refresh() }, onOscChanged = { updateOnScreenControls() }, onFrameGenChanged = { applyFrameGen() }, onBackground = { moveTaskToBack(true) }, onStop = { SessionService.stop(this); finish() }) // A session that has already drawn is past its milestones; do not cover its picture. if (SessionState.running && SessionState.firstFrameSeen) { loading.hide() hud.start() } else { loading.show() } setContentView(root) val bridge = PadBridge(File(LinuxRuntime.sessionRoot(this), "dev/input")) padBridge = bridge onScreenControls = OnScreenControls(this, bridge).also { root.addView(it, 1) } updateOnScreenControls() WaylandCompositor.setFirstFrameListener { SessionState.firstFrameSeen = true runOnUiThread { loading.hide() hud.start() } } SessionState.endListener = { status -> onSessionEnded(status) } watchSessionLog() } // ── Compositor ────────────────────────────────────────────────────────────────────────── override fun surfaceCreated(holder: SurfaceHolder) { val runtimeDir = File(filesDir, ".wayland-rt").apply { mkdirs() } // The compositor hands this keymap to wl_keyboard clients, which is how the guest reads // the evdev codes we inject. FileUtils.copyAsset(this, "wayland/keymap.xkb", File(runtimeDir, "keymap.xkb")) // Turnip, not the system Adreno driver: importing the dma-bufs gamescope commits needs // VK_EXT_image_drm_format_modifier, which the system driver does not implement. val turnip = TurnipDriver(this) val driverId = if (CompositorHost.isStarted) null else turnip.install() val size = outputSize() SessionState.outputSize = size SessionState.refreshHz = refreshHz() // Letterbox, never stretch or crop: the output above can be a different shape from the // panel now, and a game's picture must keep its proportions with bars, not lose its edges. WaylandCompositor.nativeSetScaleMode(SCALE_FIT, ALIGN_CENTER) CompositorHost.startOrAttach( holder.surface, runtimeDir.path, driverId?.let { turnip.driverPath(it) }, driverId?.let { turnip.libraryName(it) }, applicationInfo.nativeLibraryDir, size.first, size.second, refreshHz(), ) // The service owns everything below the compositor. It is started whenever no session is // running — NOT only when the compositor was just started: the compositor lives for the // whole process, so the second Play after a session ended used to re-attach the Surface, // start nothing, and leave the loading panel counting up over a dead session. if (!SessionState.running) { CompositorHost.newSession() SessionService.start(this) } applyFrameGen() } override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {} override fun surfaceDestroyed(holder: SurfaceHolder) { CompositorHost.detach() } /** * The size the session renders at. gamescope is told this and scales its output onto whatever * the panel is, so a 1440p phone can run the client at 1080p without the client knowing. */ private fun outputSize(): Pair { // The panel, not the window: resources.displayMetrics is what is left after the system // bars and the cutout are taken out, which sized the first session 1920x968 on a 1080p // device and had gamescope patch its EDID to match. val bounds = if (Build.VERSION.SDK_INT >= 30) { windowManager.maximumWindowMetrics.bounds } else { val metrics = android.util.DisplayMetrics() @Suppress("DEPRECATION") windowManager.defaultDisplay.getRealMetrics(metrics) android.graphics.Rect(0, 0, metrics.widthPixels, metrics.heightPixels) } val panelW = maxOf(bounds.width(), bounds.height()).toFloat() val panelH = minOf(bounds.width(), bounds.height()).toFloat() // Never narrower than 16:9. A foldable's inner panel is nearly square, and a game handed a // square display draws for the frame it was made for and cuts the sides off itself // (FlatOut on a Fold lost the edges of its own menus). Wider than 16:9 is fine — games and // the client cope with a phone's 20:9 — so the panel's aspect is kept above that and the // compositor letterboxes the 16:9 picture onto a squarer panel. val aspect = maxOf(panelW / panelH, 16f / 9f) // 1080 tall at most: the client's CEF is the heaviest thing in the session, and above // 1080p it costs frames for nothing anyone can see on a handheld panel. val height = minOf(panelH, 1080f).toInt() val width = (height * aspect).toInt() // Odd sizes upset the scaler; both dimensions even is what every mode here would be. return Pair(width and 1.inv(), height and 1.inv()) } private fun refreshHz(): Float { val display = if (Build.VERSION.SDK_INT >= 30) display else windowManager.defaultDisplay val hz = display?.refreshRate ?: 60f return if (hz > 1f) hz else 60f } // ── Session state ─────────────────────────────────────────────────────────────────────── /** Keeps the loading panel current from the session log while it is showing. */ private fun watchSessionLog() { val handler = Handler(Looper.getMainLooper()) val poll = object : Runnable { override fun run() { if (!watching) return if (loading.isVisible) loading.update(SessionState.logFile) handler.postDelayed(this, 500) } } handler.post(poll) } private fun onSessionEnded(status: Int) { runOnUiThread { if (isFinishing || isDestroyed) return@runOnUiThread if (status != 0) { loading.showEnded(getString(R.string.session_ended, status, SessionState.logFile?.path ?: "-")) // A moment on screen, so a failure is readable rather than a flash of black. Handler(Looper.getMainLooper()).postDelayed({ finish() }, 4000) } else { finish() } } } // ── Input ─────────────────────────────────────────────────────────────────────────────── override fun dispatchKeyEvent(event: KeyEvent): Boolean { if (padBridge?.onKeyEvent(event) == true) return true // A hardware keyboard, forwarded to the compositor's wl_keyboard. This is how an account // name and password get typed on the client's first run; the client's own on-screen // keyboard covers a device without one, driven by touch or the pad. if (CompositorHost.isStarted && event.device != null && !PadBridge.isFromController(event.device)) { val down = event.action == KeyEvent.ACTION_DOWN if (down || event.action == KeyEvent.ACTION_UP) { // An unmapped key may still be a real one on a foreign layout: its scan code is // the evdev code the kernel gave Android in the first place. var evdev = EvdevKeys.fromKeyCode(event.keyCode) if (evdev < 0 && event.scanCode > 0) evdev = event.scanCode if (evdev > 0) { WaylandCompositor.nativeSendKey(evdev, if (down) 1 else 0) return true } } } return super.dispatchKeyEvent(event) } override fun dispatchGenericMotionEvent(event: MotionEvent): Boolean { if (padBridge?.onMotionEvent(event) == true) return true return super.dispatchGenericMotionEvent(event) } /** * Touch as a mouse: where you touch is where the pointer goes, and a tap is a left click. * The compositor's pointer space is a fixed 1920x1080 whatever the output size, so the view's * coordinates are scaled into it. */ override fun onTouchEvent(event: MotionEvent): Boolean { val width = surfaceView.width.takeIf { it > 0 } ?: return false val height = surfaceView.height.takeIf { it > 0 } ?: return false val action = when (event.actionMasked) { MotionEvent.ACTION_DOWN -> 0 MotionEvent.ACTION_MOVE -> 1 MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> 2 else -> return false } // The picture is letterboxed inside the view when the panel is a different shape from the // output, so a touch is mapped through the fitted rectangle, not the whole view. val out = SessionState.outputSize val scale = minOf(width / out.first.toFloat(), height / out.second.toFloat()) val drawnW = out.first * scale val drawnH = out.second * scale val left = (width - drawnW) / 2f val top = (height - drawnH) / 2f val x = ((event.x - left) / drawnW * 1920f).toInt().coerceIn(0, 1919) val y = ((event.y - top) / drawnH * 1080f).toInt().coerceIn(0, 1079) WaylandCompositor.nativeSendPointer(action, x, y) return true } // ── Lifecycle ─────────────────────────────────────────────────────────────────────────── /** * Back opens the drawer (and closes it again). Leaving the session running in the background * and ending it are both actions in there, so neither can happen by accident from a button * a game might also be reading. */ @Suppress("DEPRECATION") override fun onBackPressed() { drawer.toggle() } /** * The drawer's mode ("always" / "never") decides outright; on "auto" the controls follow * what is attached. `steamdeck-osc` in Downloads still overrides, for a device we cannot reach. */ private fun updateOnScreenControls() { val forced = File(Environment.getExternalStorageDirectory(), "Download/steamdeck-osc") .takeIf { it.isFile } ?.let { FileUtils.readString(it)?.trim()?.lowercase() } ?: SessionPrefs.oscMode(this) val show = when (forced) { SessionPrefs.OSC_ALWAYS -> true SessionPrefs.OSC_NEVER -> false else -> !PadBridge.anyControllerConnected() } val controls = onScreenControls ?: return if (show == (controls.visibility == View.VISIBLE)) return if (!show) controls.releaseAll() controls.visibility = if (show) View.VISIBLE else View.GONE Log.i(TAG, "on-screen controls " + (if (show) "shown" else "hidden")) } /** * The saved frame-generation setting, pushed to the compositor. Off the main thread: LSFG's * first use translates the shader chain out of Lossless.dll, which takes seconds. */ private fun applyFrameGen() { val hz = refreshHz() Thread({ val problem = FrameGen.apply(this, hz) if (problem != null) runOnUiThread { android.widget.Toast.makeText(this, problem, android.widget.Toast.LENGTH_LONG).show() } }, "frame-gen").start() } override fun onResume() { super.onResume() if (CompositorHost.isStarted) applyFrameGen() (getSystemService(INPUT_SERVICE) as? InputManager) ?.registerInputDeviceListener(deviceListener, Handler(Looper.getMainLooper())) updateOnScreenControls() } override fun onPause() { (getSystemService(INPUT_SERVICE) as? InputManager)?.unregisterInputDeviceListener(deviceListener) // A button held when the app goes away would stay held in the ring for the whole session. onScreenControls?.releaseAll() super.onPause() } override fun onWindowFocusChanged(hasFocus: Boolean) { super.onWindowFocusChanged(hasFocus) if (hasFocus) goFullscreen() } private fun goFullscreen() { @Suppress("DEPRECATION") window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY or View.SYSTEM_UI_FLAG_FULLSCREEN or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION) if (Build.VERSION.SDK_INT >= 28) { window.attributes = window.attributes.apply { layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES } } } override fun onDestroy() { // Deliberately does NOT end the session: this activity can be destroyed while the user is // in another app, and the whole point of the service is that Steam survives that. watching = false hud.stop() padBridge?.stop() SessionState.endListener = null WaylandCompositor.setFirstFrameListener(null) super.onDestroy() } companion object { private const val TAG = "SessionActivity" /** Compositor scale modes (Container.FULLSCREEN_* values): 1 = fit with bars, centred. */ private const val SCALE_FIT = 1 private const val ALIGN_CENTER = 0 } }