feat: add Steam second-screen controls

This commit is contained in:
xXJsonDeruloXx committed 2026-09-23 13:23:49 -04:00
1 parent c597dee0ae
commit 96d3132431
9 files changed
+680 -11

No files matched your search

@@ -1,6 +1,7 @@
package com.steamdeck.launcher
import android.hardware.input.InputManager
import android.hardware.display.DisplayManager
import android.os.Build
import android.os.Bundle
import android.os.Environment
@@ -9,6 +10,7 @@ import android.os.Looper
import android.util.Log
import android.view.KeyEvent
import android.view.MotionEvent
import android.view.Display
import android.view.SurfaceHolder
import android.view.SurfaceView
import android.view.View
@@ -30,6 +32,8 @@ import com.steamdeck.launcher.input.KeyboardHost
import com.steamdeck.launcher.input.OnScreenControls
import com.steamdeck.launcher.input.PadBridge
import com.steamdeck.launcher.input.PointerGestures
import com.steamdeck.launcher.input.SecondScreenDisplay
import com.steamdeck.launcher.input.SecondScreenMode
import com.steamdeck.launcher.input.TouchpadGestures
import com.steamdeck.launcher.runtime.LinuxRuntime
import com.steamdeck.launcher.session.LoadingState
@@ -50,6 +54,7 @@ import com.steamdeck.launcher.ui.ProtonDialog
import com.steamdeck.launcher.ui.ProtonRow
import com.steamdeck.launcher.ui.SessionDrawer
import com.steamdeck.launcher.ui.SessionPausedOverlay
import com.steamdeck.launcher.ui.SecondScreenPresentation
import com.steamdeck.launcher.ui.SteamDeckTheme
import com.steamdeck.launcher.wayland.CompositorHost
import com.steamdeck.launcher.wayland.WaylandCompositor
@@ -93,6 +98,16 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
private var fexPreset by mutableStateOf("")
private var oscMode by mutableStateOf(SessionPrefs.OSC_AUTO)
private var shapeMode by mutableStateOf(SessionPrefs.SHAPE_AUTO)
private var secondScreenMode by mutableStateOf(SecondScreenMode.NONE)
private var secondScreenDisplays by mutableStateOf<List<SecondScreenDisplay>>(emptyList())
private var selectedSecondScreenDisplay by mutableStateOf(-1)
private lateinit var displayManager: DisplayManager
private var secondScreenPresentation: SecondScreenPresentation? = null
private val displayListener = object : DisplayManager.DisplayListener {
override fun onDisplayAdded(displayId: Int) = refreshSecondScreenDisplays()
override fun onDisplayRemoved(displayId: Int) = refreshSecondScreenDisplays()
override fun onDisplayChanged(displayId: Int) = refreshSecondScreenDisplays()
}
/**
* Shows the on-screen pad when nothing is plugged in and takes it away the moment something
@@ -115,6 +130,8 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
// The device's volume keys change the stream the session plays on (the relay and
// PulseAudio are media playback); they are never forwarded to the guest.
volumeControlStream = android.media.AudioManager.STREAM_MUSIC
displayManager = getSystemService(DISPLAY_SERVICE) as DisplayManager
refreshSecondScreenDisplays()
if (!LinuxRuntime.isInstalled(this)) {
Log.e(TAG, "the Linux runtime is not installed")
@@ -173,6 +190,9 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
oscMode = oscMode, touchMode = touchMode,
touchAuto = if (usingTouchpad()) "touchpad" else "direct",
shapeMode = shapeMode, fexPreset = fexPreset,
secondScreenMode = secondScreenMode,
secondScreenDisplays = secondScreenDisplays,
selectedSecondScreenDisplay = selectedSecondScreenDisplay,
onHud = { on -> SessionPrefs.setHudEnabled(this@SessionActivity, on); hudOn = on; hud.refresh() },
onFrameGenPick = { engine, multiplier ->
FrameGen.set(this@SessionActivity, engine, multiplier)
@@ -199,6 +219,8 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
onTouch = { v -> SessionPrefs.setTouchMode(this@SessionActivity, v); readPrefs() },
onShape = { v -> SessionPrefs.setShapeMode(this@SessionActivity, v); readPrefs() },
onFexPreset = { v -> SessionPrefs.setFexPreset(this@SessionActivity, v); readPrefs() },
onSecondScreenMode = ::selectSecondScreenMode,
onSecondScreenDisplay = ::selectSecondScreenDisplay,
onBackground = { drawerOpen = false; moveTaskToBack(true) },
onStop = { drawerOpen = false; SessionService.stop(this@SessionActivity); finish() },
onClose = { drawerOpen = false },
@@ -496,6 +518,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
}
private fun onSessionEnded(status: Int) {
closeSecondScreen(reset = true)
if (status == 0) {
runOnUiThread {
if (isFinishing || isDestroyed) return@runOnUiThread
@@ -663,6 +686,106 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
if (SessionState.mode == SessionService.MODE_STEAM && SessionState.steamUi != "desktop") uiHandler.postDelayed(cursorHide, 2500)
}
private fun refreshSecondScreenDisplays() {
if (!::displayManager.isInitialized) return
val primaryId = display?.displayId ?: windowManager.defaultDisplay.displayId
val candidates = displayManager.getDisplays(DisplayManager.DISPLAY_CATEGORY_PRESENTATION)
.asSequence()
.filter { it.isValid && it.displayId != primaryId && (it.flags and Display.FLAG_PRESENTATION) != 0 }
.map { target ->
val mode = target.mode
SecondScreenDisplay(target.displayId, "${target.name} · ${mode.physicalWidth}×${mode.physicalHeight}")
}
.toList()
val oldIds = secondScreenDisplays.map { it.id }.toSet()
secondScreenDisplays = candidates
val newIds = candidates.map { it.id }.toSet()
if (secondScreenMode != SecondScreenMode.NONE && oldIds.isNotEmpty() && selectedSecondScreenDisplay !in newIds) {
closeSecondScreen(reset = true)
return
}
if (selectedSecondScreenDisplay !in newIds) selectedSecondScreenDisplay = candidates.firstOrNull()?.id ?: -1
if (secondScreenMode != SecondScreenMode.NONE && candidates.isEmpty()) closeSecondScreen(reset = true)
}
private fun selectSecondScreenDisplay(displayId: Int) {
if (secondScreenDisplays.none { it.id == displayId }) return
selectedSecondScreenDisplay = displayId
if (secondScreenMode != SecondScreenMode.NONE) showSecondScreen(secondScreenMode)
}
private fun selectSecondScreenMode(mode: SecondScreenMode) {
if (mode == SecondScreenMode.NONE) {
closeSecondScreen(reset = true)
return
}
if (secondScreenDisplays.isEmpty()) {
secondScreenMode = SecondScreenMode.NONE
return
}
if (selectedSecondScreenDisplay !in secondScreenDisplays.map { it.id }) {
selectedSecondScreenDisplay = secondScreenDisplays.first().id
}
showSecondScreen(mode)
}
private fun showSecondScreen(mode: SecondScreenMode) {
val target = displayManager.getDisplay(selectedSecondScreenDisplay)
if (target == null || !target.isValid || (target.flags and Display.FLAG_PRESENTATION) == 0) {
closeSecondScreen(reset = true)
refreshSecondScreenDisplays()
return
}
var presentation = secondScreenPresentation
if (presentation?.display?.displayId != target.displayId) {
presentation?.closeControls()
presentation = SecondScreenPresentation(
this, target,
sendPointer = ::moveSecondScreenPointer,
sendButton = { button, pressed ->
WaylandCompositor.nativeSendSceneInput(3, button, if (pressed) 1 else 0)
},
sendWheel = { steps -> WaylandCompositor.nativeSendSceneInput(4, steps, 0) },
onSteamMenu = {
if (SessionState.mode == SessionService.MODE_STEAM) {
padBridge?.applyTouch { st -> st.press(com.steamdeck.launcher.input.PadState.GUIDE, true) }
Handler(Looper.getMainLooper()).postDelayed({
padBridge?.applyTouch { st -> st.press(com.steamdeck.launcher.input.PadState.GUIDE, false) }
}, 90)
}
},
onQam = {
if (SessionState.mode == SessionService.MODE_STEAM) padBridge?.triggerQam()
},
)
secondScreenPresentation = presentation
}
try {
if (!presentation.isShowing) presentation.show()
presentation.showMode(mode)
secondScreenMode = mode
} catch (e: Exception) {
Log.w(TAG, "could not show second-screen controls on ${target.name}", e)
closeSecondScreen(reset = true)
}
}
private fun moveSecondScreenPointer(x: Float, y: Float, width: Float, height: Float) {
if (width <= 0f || height <= 0f) return
val nx = (x / width).coerceIn(0f, 1f)
val ny = (y / height).coerceIn(0f, 1f)
WaylandCompositor.nativeSendPointer(1, (nx * 1919f).toInt(), (ny * 1079f).toInt())
drawnRect()?.let { rect ->
showCursor(rect.left + nx * rect.width(), rect.top + ny * rect.height())
}
}
private fun closeSecondScreen(reset: Boolean) {
secondScreenPresentation?.closeControls()
secondScreenPresentation = null
if (reset) secondScreenMode = SecondScreenMode.NONE
}
/** Touchpad on the desktop, direct in Steam, unless the drawer says otherwise. */
private fun usingTouchpad(): Boolean = when (SessionPrefs.touchMode(this)) {
SessionPrefs.TOUCH_PAD -> true
@@ -751,10 +874,15 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
override fun onStart() {
super.onStart()
displayManager.registerDisplayListener(displayListener, Handler(Looper.getMainLooper()))
refreshSecondScreenDisplays()
if (secondScreenMode != SecondScreenMode.NONE) showSecondScreen(secondScreenMode)
SessionService.setActivityVisible(this, true)
}
override fun onStop() {
displayManager.unregisterDisplayListener(displayListener)
closeSecondScreen(reset = true)
SessionService.setActivityVisible(this, false)
super.onStop()
}
@@ -817,6 +945,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
// 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
closeSecondScreen(reset = true)
if (::hud.isInitialized) hud.stop()
padBridge?.stop()
if (SessionState.endListener === endListener) SessionState.endListener = null
@@ -22,8 +22,13 @@ import android.view.inputmethod.InputMethodManager
* Invisible and unfocusable until asked for: a focused View would take D-pad and stick motion
* away from the pad.
*/
class KeyboardHost(private val activity: Activity) : View(activity) {
private val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
class KeyboardHost(
context: Context,
private val sendKeyEvent: (KeyEvent) -> Boolean,
) : View(context) {
constructor(activity: Activity) : this(activity, { activity.dispatchKeyEvent(it) })
private val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
private val map: KeyCharacterMap = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD)
var shown = false
private set
@@ -75,14 +80,14 @@ class KeyboardHost(private val activity: Activity) : View(activity) {
// key event carrying the character, which the activity's key path works back from.
val events = map.getEvents(text.toString().toCharArray())
if (events != null) {
for (event in events) activity.dispatchKeyEvent(event)
for (event in events) sendKeyEvent(event)
} else {
for (ch in text) {
val t = System.currentTimeMillis()
val down = KeyEvent(t, t, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_UNKNOWN, 0, 0,
KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0, 0)
activity.dispatchKeyEvent(CharKeyEvent(down, ch))
activity.dispatchKeyEvent(CharKeyEvent(KeyEvent.changeAction(down, KeyEvent.ACTION_UP), ch))
sendKeyEvent(CharKeyEvent(down, ch))
sendKeyEvent(CharKeyEvent(KeyEvent.changeAction(down, KeyEvent.ACTION_UP), ch))
}
}
return true
@@ -97,7 +102,7 @@ class KeyboardHost(private val activity: Activity) : View(activity) {
return true
}
override fun sendKeyEvent(event: KeyEvent): Boolean = activity.dispatchKeyEvent(event)
override fun sendKeyEvent(event: KeyEvent): Boolean = this@KeyboardHost.sendKeyEvent(event)
override fun performEditorAction(actionCode: Int): Boolean {
tap(KeyEvent.KEYCODE_ENTER)
@@ -106,8 +111,8 @@ class KeyboardHost(private val activity: Activity) : View(activity) {
private fun tap(keyCode: Int) {
val t = System.currentTimeMillis()
activity.dispatchKeyEvent(KeyEvent(t, t, KeyEvent.ACTION_DOWN, keyCode, 0))
activity.dispatchKeyEvent(KeyEvent(t, t, KeyEvent.ACTION_UP, keyCode, 0))
sendKeyEvent(KeyEvent(t, t, KeyEvent.ACTION_DOWN, keyCode, 0))
sendKeyEvent(KeyEvent(t, t, KeyEvent.ACTION_UP, keyCode, 0))
}
override fun getTextBeforeCursor(n: Int, flags: Int): CharSequence = ""
@@ -0,0 +1,10 @@
package com.steamdeck.launcher.input
/** The control surface shown on a secondary Android display during a Linux session. */
enum class SecondScreenMode(val id: String, val label: String) {
NONE("none", "None"),
KEYBOARD_TRACKPAD("keyboard-trackpad", "Keyboard + trackpad"),
TERMINAL("terminal", "Terminal"),
}
data class SecondScreenDisplay(val id: Int, val label: String)
@@ -60,6 +60,9 @@ class SessionService : Service() {
private var wakeLock: PowerManager.WakeLock? = null
private var wifiLock: WifiManager.WifiLock? = null
private var sessionPid = -1
/** Auxiliary PTY-backed proot shells launched from a secondary-display terminal. */
private val auxiliaryProcesses = HashMap<Int, Long>()
private val auxiliaryProcessesLock = Any()
private val mainHandler = Handler(Looper.getMainLooper())
private var suspendController: SessionSuspendController? = null
private var suspendPolicy = SessionPrefs.SUSPEND_MANUAL
@@ -96,6 +99,34 @@ class SessionService : Service() {
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
when (intent?.action) {
ACTION_TRACK_AUXILIARY -> {
val pid = intent.getIntExtra(EXTRA_AUXILIARY_PID, -1)
val started = if (pid > 1) readStat(pid)?.second else null
if (started != null) {
val tracked = synchronized(auxiliaryProcessesLock) {
if (SessionState.running) {
auxiliaryProcesses[pid] = started
true
} else false
}
if (!tracked) Thread({ teardown(pid, started) }, "auxiliary-stop-$pid").start()
}
return START_NOT_STICKY
}
ACTION_STOP_AUXILIARY -> {
val pid = intent.getIntExtra(EXTRA_AUXILIARY_PID, -1)
if (pid > 1) {
val started = synchronized(auxiliaryProcessesLock) { auxiliaryProcesses.remove(pid) } ?: readStat(pid)?.second
if (started != null) Thread({ teardown(pid, started) }, "auxiliary-stop-$pid").start()
}
return START_NOT_STICKY
}
ACTION_AUXILIARY_EXITED -> {
synchronized(auxiliaryProcessesLock) {
auxiliaryProcesses.remove(intent.getIntExtra(EXTRA_AUXILIARY_PID, -1))
}
return START_NOT_STICKY
}
ACTION_STOP -> {
Log.i(TAG, "stop requested from the notification")
stopSession(0)
@@ -198,6 +229,7 @@ class SessionService : Service() {
// ── The session ─────────────────────────────────────────────────────────────────────────
private fun runSession(gen: Int) {
SessionTerminal.clear()
try {
LinuxRuntime.writeAccounts(this)
} catch (e: Exception) {
@@ -399,6 +431,13 @@ class SessionService : Service() {
// The second library's name, for bannerlator-steam-library; the bind itself is made below.
GameStorage.effective(this)?.let { guest.add("BL_LIBRARY_LABEL=" + it.label.replace('"', ' ')) }
if (SessionState.mode == MODE_STEAM && SessionState.steamUi == "desktop") guest.add("BL_STEAM_UI=desktop")
val shellGuest = ArrayList(guest).apply {
add("SHELL=/bin/bash")
add("TERM=xterm-256color")
add("/bin/bash")
add("-c")
add("cd \"\$HOME\" && exec /bin/bash -i")
}
guest.add(LinuxRuntime.SESSION_SCRIPT)
guest.add(SessionState.mode)
if (SessionState.mode == MODE_STEAM) SessionState.steamUrl?.takeIf { it.startsWith("steam://") }?.let {
@@ -502,6 +541,18 @@ class SessionService : Service() {
val prootLibs = LinuxRuntime.prootLibraryPath(this)
if (prootLibs.isNotEmpty()) hostEnv["LD_LIBRARY_PATH"] = prootLibs
val terminalCommand = LinuxRuntime.command(
this, sessionRoot, runtimeDir, Environment.getExternalStorageDirectory(), binds, shellGuest,
)
val terminalHostEnvironment = LinkedHashMap(System.getenv())
hostEnv.asArray().forEach { entry ->
val separator = entry.indexOf('=')
if (separator > 0) terminalHostEnvironment[entry.substring(0, separator)] = entry.substring(separator + 1)
}
if (gen == sessionGen && SessionState.running) {
SessionTerminal.prepare(terminalCommand, terminalHostEnvironment.map { (key, value) -> "$key=$value" }.toTypedArray(), root, gen)
}
// Whether the client signs in to Valve or starts offline: read once, while it starts, and
// rewritten by the client when it exits, so it is set again here at every session start.
if (SessionState.mode == MODE_STEAM) OfflineMode.apply(this, root)
@@ -542,10 +593,14 @@ class SessionService : Service() {
}
}
private fun teardown(prootPid: Int) {
private fun teardown(prootPid: Int, expectedStartTime: Long? = null) {
fun stillSameProcess(): Boolean = expectedStartTime == null || readStat(prootPid)?.second == expectedStartTime
if (!stillSameProcess()) return
val tree = descendants(prootPid)
if (!stillSameProcess()) return
android.os.Process.sendSignal(prootPid, 15) // SIGTERM
if (!waitForExit(prootPid, GRACE_MS)) {
if (!stillSameProcess()) return
Log.w(TAG, "proot $prootPid did not exit on SIGTERM; killing it")
android.os.Process.killProcess(prootPid)
}
@@ -738,6 +793,10 @@ class SessionService : Service() {
// reused by a new process is never touched.
val prootPid = sessionPid
sessionPid = -1
val auxiliary = synchronized(auxiliaryProcessesLock) {
auxiliaryProcesses.toMap().also { auxiliaryProcesses.clear() }
}
SessionTerminal.clear(sessionGen)
val controller = suspendController
suspendController = null
// On its own thread, never here: stopSession runs on the main thread (the notification's
@@ -757,9 +816,10 @@ class SessionService : Service() {
components.clear()
FakeInputWriter.releaseAllRingSlots()
val finishAfterTeardown: () -> Unit = {
if (prootPid > 1) {
if (prootPid > 1 || auxiliary.isNotEmpty()) {
Thread({
teardown(prootPid)
auxiliary.forEach { (pid, started) -> teardown(pid, started) }
if (prootPid > 1) teardown(prootPid)
finishSessionStop(status)
}, "session-teardown").start()
} else {
@@ -903,6 +963,10 @@ class SessionService : Service() {
const val ACTION_RESUME = "com.steamdeck.launcher.RESUME_SESSION"
private const val ACTION_ACTIVITY_VISIBLE = "com.steamdeck.launcher.ACTIVITY_VISIBLE"
private const val ACTION_ACTIVITY_HIDDEN = "com.steamdeck.launcher.ACTIVITY_HIDDEN"
private const val ACTION_TRACK_AUXILIARY = "com.steamdeck.launcher.TRACK_AUXILIARY"
private const val ACTION_STOP_AUXILIARY = "com.steamdeck.launcher.STOP_AUXILIARY"
private const val ACTION_AUXILIARY_EXITED = "com.steamdeck.launcher.AUXILIARY_EXITED"
private const val EXTRA_AUXILIARY_PID = "auxiliaryPid"
/** Command lines that can only belong to a session of ours. */
private val STRAGGLERS = listOf("bannerlator-session", "gamescope", "Xwayland", "steamrtarm64",
"steamwebhelper", "linuxfs/opt/android-host/proot", "pulseaudio/libpulseaudio.so")
@@ -950,5 +1014,30 @@ class SessionService : Service() {
if (!SessionState.running) return
context.startService(Intent(context, SessionService::class.java).setAction(ACTION_RESUME))
}
/** Let the session service clean up the PTY's proot tree if the control screen closes. */
fun stopTerminalProcess(context: Context, pid: Int) {
if (pid > 1) context.startService(
Intent(context, SessionService::class.java)
.setAction(ACTION_STOP_AUXILIARY)
.putExtra(EXTRA_AUXILIARY_PID, pid),
)
}
fun registerTerminalProcess(context: Context, pid: Int) {
if (pid > 1) context.startService(
Intent(context, SessionService::class.java)
.setAction(ACTION_TRACK_AUXILIARY)
.putExtra(EXTRA_AUXILIARY_PID, pid),
)
}
fun terminalProcessExited(context: Context, pid: Int) {
if (pid > 1) context.startService(
Intent(context, SessionService::class.java)
.setAction(ACTION_AUXILIARY_EXITED)
.putExtra(EXTRA_AUXILIARY_PID, pid),
)
}
}
}
@@ -0,0 +1,40 @@
package com.steamdeck.launcher.session
import com.termux.terminal.TerminalSession
import com.termux.terminal.TerminalSessionClient
import java.io.File
/** Immutable launch context for the optional PTY shell attached to the active Linux runtime. */
object SessionTerminal {
private data class Launch(
val command: Array<String>,
val hostEnvironment: Array<String>,
val workingDirectory: String,
val generation: Int,
)
@Volatile private var launch: Launch? = null
fun prepare(
command: List<String>,
hostEnvironment: Array<String>,
workingDirectory: File,
generation: Int,
) {
launch = Launch(command.toTypedArray(), hostEnvironment.copyOf(), workingDirectory.path, generation)
}
fun create(client: TerminalSessionClient): TerminalSession? {
val current = launch ?: return null
if (!SessionState.running) return null
return TerminalSession(
current.command.first(), current.workingDirectory, current.command,
current.hostEnvironment, 3000, client,
).also { it.mSessionName = "SteamDeck Linux shell" }
}
fun clear(generation: Int? = null) {
val current = launch ?: return
if (generation == null || current.generation == generation) launch = null
}
}
@@ -0,0 +1,372 @@
package com.steamdeck.launcher.ui
import android.app.Presentation
import android.content.ClipboardManager
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.os.Bundle
import android.util.Log
import android.view.Display
import android.view.Gravity
import android.view.KeyEvent
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.view.ViewConfiguration
import android.view.WindowManager
import android.view.inputmethod.InputMethodManager
import android.widget.Button
import android.widget.FrameLayout
import android.widget.HorizontalScrollView
import android.widget.LinearLayout
import android.widget.TextView
import com.steamdeck.launcher.input.KeyboardHost
import com.steamdeck.launcher.input.PointerGestures
import com.steamdeck.launcher.input.SecondScreenMode
import com.steamdeck.launcher.input.TouchpadGestures
import com.steamdeck.launcher.SessionActivity
import com.steamdeck.launcher.session.SessionService
import com.steamdeck.launcher.session.SessionState
import com.steamdeck.launcher.session.SessionTerminal
import com.termux.terminal.TerminalEmulator
import com.termux.terminal.TerminalSession
import com.termux.terminal.TerminalSessionClient
import com.termux.view.TerminalView
import com.termux.view.TerminalViewClient
/** Touch controls drawn on a second Android display while the compositor remains on the main one. */
class SecondScreenPresentation(
private val sessionActivity: SessionActivity,
display: Display,
private val sendPointer: (x: Float, y: Float, width: Float, height: Float) -> Unit,
private val sendButton: (button: Int, pressed: Boolean) -> Unit,
private val sendWheel: (steps: Int) -> Unit,
private val onSteamMenu: () -> Unit,
private val onQam: () -> Unit,
) : Presentation(sessionActivity, display) {
private val root = LinearLayout(context).apply {
orientation = LinearLayout.VERTICAL
setBackgroundColor(Color.rgb(15, 18, 22))
isFocusableInTouchMode = true
}
private var currentMode = SecondScreenMode.NONE
private var keyboardHost: KeyboardHost? = null
private var terminalSession: TerminalSession? = null
private var terminalView: TerminalView? = null
private var terminalPid = -1
private var ctrlActive = false
private var altActive = false
private var created = false
init {
setCancelable(false)
setCanceledOnTouchOutside(false)
setOnDismissListener { closeTerminal() }
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
created = true
window?.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
setContentView(root)
applyImmersiveMode()
renderMode()
}
fun showMode(mode: SecondScreenMode) {
if (currentMode == mode && isShowing) return
currentMode = mode
if (created) renderMode()
}
/** Releases the auxiliary Linux shell before the window is dismissed. */
fun closeControls() {
closeTerminal()
keyboardHost?.hide()
keyboardHost = null
dismiss()
}
override fun dispatchKeyEvent(event: KeyEvent): Boolean {
if (currentMode == SecondScreenMode.KEYBOARD_TRACKPAD) {
if (sessionActivity.dispatchKeyEvent(event)) return true
}
return super.dispatchKeyEvent(event)
}
override fun onWindowFocusChanged(hasFocus: Boolean) {
super.onWindowFocusChanged(hasFocus)
if (hasFocus) applyImmersiveMode()
}
private fun renderMode() {
keyboardHost?.hide()
keyboardHost = null
closeTerminal()
root.removeAllViews()
if (currentMode == SecondScreenMode.NONE) return
when (currentMode) {
SecondScreenMode.KEYBOARD_TRACKPAD -> renderKeyboardAndTrackpad()
SecondScreenMode.TERMINAL -> renderTerminal()
SecondScreenMode.NONE -> Unit
}
}
private fun renderKeyboardAndTrackpad() {
val panel = FrameLayout(context)
panel.addView(trackpadView(), FrameLayout.LayoutParams(-1, -1))
val controls = LinearLayout(context).apply {
gravity = Gravity.CENTER_VERTICAL
orientation = LinearLayout.HORIZONTAL
setPadding(dp(12), dp(8), dp(12), dp(8))
setBackgroundColor(0xE6191E24.toInt())
}
controls.addView(TextView(context).apply {
text = "Trackpad"
textSize = 14f
setTextColor(Color.WHITE)
}, LinearLayout.LayoutParams(0, -2, 1f))
val keyboardButton = Button(context).apply {
text = "Show keyboard"
setOnClickListener {
val host = keyboardHost ?: return@setOnClickListener
if (host.shown) {
host.hide()
text = "Show keyboard"
} else {
host.show()
text = "Hide keyboard"
}
}
}
controls.addView(keyboardButton)
controls.addView(Button(context).apply {
text = "STEAM"
contentDescription = "Open Steam menu"
setOnClickListener { onSteamMenu() }
})
controls.addView(Button(context).apply {
text = "…"
contentDescription = "Open Quick Access Menu"
setOnClickListener { onQam() }
})
panel.addView(controls, FrameLayout.LayoutParams(-1, -2, Gravity.TOP))
root.addView(panel, weightParams())
val host = KeyboardHost(context) { sessionActivity.dispatchKeyEvent(it) }
keyboardHost = host
root.addView(host, ViewGroup.LayoutParams(1, 1))
}
private fun trackpadView(): View = TrackpadView(context)
private fun renderTerminal() {
val bar = LinearLayout(context).apply {
orientation = LinearLayout.HORIZONTAL
gravity = Gravity.CENTER_VERTICAL
setPadding(dp(8), dp(4), dp(8), dp(4))
setBackgroundColor(Color.rgb(27, 32, 38))
}
bar.addView(TextView(context).apply {
text = "Linux terminal"
textSize = 14f
setTextColor(Color.WHITE)
}, LinearLayout.LayoutParams(0, -2, 1f))
bar.addView(Button(context).apply {
text = "Keyboard"
setOnClickListener { showTerminalKeyboard() }
})
root.addView(bar, LinearLayout.LayoutParams(-1, -2))
val terminalClient = TerminalClient()
val view = TerminalView(context, null).apply {
setTerminalViewClient(terminalClient)
setTextSize(dp(14))
isFocusable = true
isFocusableInTouchMode = true
}
terminalView = view
val session = SessionTerminal.create(terminalClient)
if (session == null) {
root.addView(TextView(context).apply {
text = "The Linux session is not ready for a terminal yet."
setTextColor(Color.WHITE)
gravity = Gravity.CENTER
}, LinearLayout.LayoutParams(-1, 0, 1f))
return
}
terminalSession = session
val keyStrip = HorizontalScrollView(context).apply { isHorizontalScrollBarEnabled = false }
val keys = LinearLayout(context).apply { orientation = LinearLayout.HORIZONTAL; setPadding(dp(4), dp(2), dp(4), dp(2)) }
fun key(label: String, input: String, onClick: ((Button) -> Unit)? = null) {
val button = Button(context).apply {
text = label
setOnClickListener { if (onClick != null) onClick(this) else session.write(input) }
}
keys.addView(button)
}
key("Esc", "\u001b")
key("Tab", "\t")
key("Ctrl", "") { button ->
ctrlActive = !ctrlActive
button.text = if (ctrlActive) "Ctrl ✓" else "Ctrl"
}
key("Alt", "") { button ->
altActive = !altActive
button.text = if (altActive) "Alt ✓" else "Alt"
}
key("↑", "\u001b[A")
key("↓", "\u001b[B")
key("←", "\u001b[D")
key("→", "\u001b[C")
key("Ctrl+C", "\u0003")
key("Ctrl+D", "\u0004")
keyStrip.addView(keys)
root.addView(keyStrip, LinearLayout.LayoutParams(-1, -2))
root.addView(view, LinearLayout.LayoutParams(-1, 0, 1f))
view.attachSession(session)
view.post {
registerTerminalPid()
view.requestFocus()
showTerminalKeyboard()
}
}
private fun showTerminalKeyboard() {
terminalView?.let { view ->
view.requestFocus()
(context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager)
.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT)
}
}
private fun registerTerminalPid() {
val pid = terminalSession?.pid ?: -1
if (pid > 1 && pid != terminalPid) {
terminalPid = pid
SessionService.registerTerminalProcess(context, pid)
}
}
private fun closeTerminal() {
terminalView?.let { view ->
(context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager)
.hideSoftInputFromWindow(view.windowToken, 0)
}
val pid = terminalPid.takeIf { it > 1 } ?: (terminalSession?.pid ?: -1)
if (pid > 1) SessionService.stopTerminalProcess(context, pid)
terminalPid = -1
terminalSession = null
terminalView = null
ctrlActive = false
altActive = false
}
private fun dp(value: Int): Int = (value * resources.displayMetrics.density + 0.5f).toInt()
private fun weightParams() = LinearLayout.LayoutParams(-1, 0, 1f)
private fun applyImmersiveMode() {
@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)
}
private inner class TrackpadView(context: Context) : View(context) {
private val gestures = TouchpadGestures(ViewConfiguration.get(context).scaledTouchSlop.toFloat(),
object : PointerGestures.Listener {
override fun onMove(x: Float, y: Float) {
sendPointer(x, y, width.toFloat(), height.toFloat())
invalidate()
}
override fun onButton(button: Int, pressed: Boolean, x: Float, y: Float) {
sendPointer(x, y, width.toFloat(), height.toFloat())
sendButton(button, pressed)
}
override fun onWheel(steps: Int) = sendWheel(steps)
override fun onLongPress() { performHapticFeedback(android.view.HapticFeedbackConstants.LONG_PRESS) }
})
init { isFocusable = true }
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
gestures.bounds = android.graphics.RectF(0f, 0f, w.toFloat(), h.toFloat())
if (oldw == 0 || oldh == 0) gestures.place(w / 2f, h / 2f)
}
override fun onDraw(canvas: Canvas) {
canvas.drawColor(Color.BLACK)
}
override fun onTouchEvent(event: MotionEvent): Boolean = gestures.onTouch(event)
}
private inner class TerminalClient : TerminalSessionClient, TerminalViewClient {
override fun onTextChanged(changedSession: TerminalSession) {
terminalView?.onScreenUpdated()
}
override fun onTitleChanged(changedSession: TerminalSession) = Unit
override fun onSessionFinished(finishedSession: TerminalSession) {
val pid = terminalPid.takeIf { it > 1 } ?: finishedSession.pid
if (pid > 1) SessionService.terminalProcessExited(context, pid)
terminalPid = -1
terminalView?.onScreenUpdated()
}
override fun onCopyTextToClipboard(session: TerminalSession, text: String) {
(context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager)
.setPrimaryClip(android.content.ClipData.newPlainText("Terminal", text))
}
override fun onPasteTextFromClipboard(session: TerminalSession) {
val clip = (context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager).primaryClip
if (clip != null && clip.itemCount > 0) session.write(clip.getItemAt(0).coerceToText(context).toString())
}
override fun onBell(session: TerminalSession) {
terminalView?.performHapticFeedback(android.view.HapticFeedbackConstants.KEYBOARD_TAP)
}
override fun onColorsChanged(changedSession: TerminalSession) {
terminalView?.invalidate()
}
override fun onTerminalCursorStateChange(state: Boolean) {
terminalView?.setTerminalCursorBlinkerRate(if (state) 600 else 0)
}
override fun getTerminalCursorStyle(): Int = TerminalEmulator.TERMINAL_CURSOR_STYLE_BLOCK
override fun logError(tag: String, message: String) { Log.e(tag, message) }
override fun logWarn(tag: String, message: String) { Log.w(tag, message) }
override fun logInfo(tag: String, message: String) { Log.i(tag, message) }
override fun logDebug(tag: String, message: String) { Log.d(tag, message) }
override fun logVerbose(tag: String, message: String) { Log.v(tag, message) }
override fun logStackTraceWithMessage(tag: String, message: String, e: Exception) { Log.e(tag, message, e) }
override fun logStackTrace(tag: String, e: Exception) { Log.e(tag, e.message, e) }
override fun onScale(scale: Float): Float = 1f
override fun onSingleTapUp(e: MotionEvent) = showTerminalKeyboard()
override fun shouldBackButtonBeMappedToEscape(): Boolean = true
override fun shouldEnforceCharBasedInput(): Boolean = false
override fun shouldUseCtrlSpaceWorkaround(): Boolean = false
override fun isTerminalViewSelected(): Boolean = true
override fun copyModeChanged(copyMode: Boolean) = Unit
override fun onKeyDown(keyCode: Int, e: KeyEvent, session: TerminalSession): Boolean = false
override fun onKeyUp(keyCode: Int, e: KeyEvent): Boolean = false
override fun onLongPress(event: MotionEvent): Boolean = false
override fun readControlKey(): Boolean = ctrlActive
override fun readAltKey(): Boolean = altActive
override fun readShiftKey(): Boolean = false
override fun readFnKey(): Boolean = false
override fun onCodePoint(codePoint: Int, ctrlDown: Boolean, session: TerminalSession): Boolean = false
override fun onEmulatorSet() {
terminalView?.onScreenUpdated()
registerTerminalPid()
}
}
}
@@ -64,6 +64,8 @@ import androidx.compose.ui.text.font.FontWeight
import com.steamdeck.launcher.core.FexPreset
import com.steamdeck.launcher.gpu.FrameGen
import com.steamdeck.launcher.session.SessionPrefs
import com.steamdeck.launcher.input.SecondScreenDisplay
import com.steamdeck.launcher.input.SecondScreenMode
import kotlinx.coroutines.flow.collect
@Composable
@@ -143,6 +145,9 @@ class DrawerActions(
val touchAuto: String,
val shapeMode: String,
val fexPreset: String,
val secondScreenMode: SecondScreenMode,
val secondScreenDisplays: List<SecondScreenDisplay>,
val selectedSecondScreenDisplay: Int,
val onHud: (Boolean) -> Unit,
val onFrameGenPick: (engine: String, multiplier: Int) -> Unit,
val onKeyboard: () -> Unit,
@@ -153,6 +158,8 @@ class DrawerActions(
val onTouch: (String) -> Unit,
val onShape: (String) -> Unit,
val onFexPreset: (String) -> Unit,
val onSecondScreenMode: (SecondScreenMode) -> Unit,
val onSecondScreenDisplay: (Int) -> Unit,
val onBackground: () -> Unit,
val onStop: () -> Unit,
val onClose: () -> Unit,
@@ -258,6 +265,20 @@ fun SessionDrawer(open: Boolean, a: DrawerActions) {
ActionRow("Keyboard", null, "Show") { host.open = null; a.onKeyboard() }
}
if (a.steam && a.secondScreenDisplays.isNotEmpty()) SettingsGroup("Second screen") {
ChoiceRow(
host, "second-screen-mode", "Controls", null,
listOf(SecondScreenMode.NONE, SecondScreenMode.KEYBOARD_TRACKPAD, SecondScreenMode.TERMINAL)
.map { it to it.label }, a.secondScreenMode,
onPick = a.onSecondScreenMode,
)
if (a.secondScreenDisplays.size > 1) ChoiceRow(
host, "second-screen-display", "Display", null,
a.secondScreenDisplays.map { it.id to it.label }, a.selectedSecondScreenDisplay,
onPick = a.onSecondScreenDisplay,
)
}
SettingsGroup("Next session") {
ChoiceRow(
host, "shape", "Display shape", null,