Merge latest main and fix controller drawer focus

This commit is contained in:
xXJsonDeruloXx committed 2026-09-24 00:54:32 -04:00
commit 46eb874c3d
21 files changed
+1948 -55

No files matched your search

@@ -170,7 +170,7 @@ class MainActivity : ComponentActivity() {
private var suspendPolicy by mutableStateOf(SessionPrefs.SUSPEND_MANUAL)
private var oscMode by mutableStateOf(SessionPrefs.OSC_AUTO)
private var backActionsInverted by mutableStateOf(false)
private var renderer by mutableStateOf("pixman")
private var renderer by mutableStateOf("vulkan")
private var gameStorage by mutableStateOf("")
private var storageOptions by mutableStateOf<List<Pair<String, String>>>(emptyList())
private val pickGameStorage = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { r ->
@@ -8,7 +8,9 @@ import android.os.Bundle
import android.os.Environment
import android.os.Handler
import android.os.Looper
import android.os.SystemClock
import android.util.Log
import android.view.InputDevice
import android.view.KeyEvent
import android.view.MotionEvent
import android.view.Display
@@ -58,6 +60,7 @@ import com.droiddeck.launcher.ui.DroidDeckTheme
import com.droiddeck.launcher.wayland.CompositorHost
import com.droiddeck.launcher.wayland.WaylandCompositor
import java.io.File
import kotlin.math.abs
/**
* The session's screen: our Wayland compositor presenting onto this activity's Surface, and the
@@ -72,6 +75,7 @@ import java.io.File
*/
class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
private lateinit var surfaceView: SurfaceView
private lateinit var sessionOverlay: ComposeView
private lateinit var loading: LoadingState
private lateinit var hud: PerfHud
private var padBridge: PadBridge? = null
@@ -86,10 +90,16 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
private val cursorHide = Runnable { cursorVisible = false }
private val uiHandler = Handler(Looper.getMainLooper())
private var pendingBackAction: Runnable? = null
private var drawerDirectionKey = KeyEvent.KEYCODE_UNKNOWN
private var drawerDirectionDownTime = 0L
private var drawerDirectionLastRepeat = 0L
private var drawerDirectionDeviceId = -1
// Compose reads these; the activity writes them.
private var drawerOpen by mutableStateOf(false)
private var backActionsInverted by mutableStateOf(false)
/** The on-screen PC keyboard (ui/PcKeyboard): real key presses, Esc and F1 included. */
private var pcKeyboardOpen by mutableStateOf(false)
private var hudOn by mutableStateOf(true)
private var frameGenLabel by mutableStateOf("Off")
private var frameGenEngine by mutableStateOf(FrameGen.ENGINE_OFF)
@@ -175,14 +185,31 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
// One Compose layer for every menu and overlay. Touches nothing in it consumes fall
// through to the pad and the game underneath.
root.addView(ComposeView(this).apply {
sessionOverlay = ComposeView(this).apply {
isFocusable = true
isFocusableInTouchMode = true
setContent {
DroidDeckTheme {
CursorOverlay(cursorPos, cursorVisible, resources.displayMetrics.density)
if (hud.text.isNotEmpty()) HudText(hud.text)
if (loading.visible) LoadingOverlay(loading.step, loading.percent, loading.elapsed, loading.hint, loading.ended)
// Opening the drawer takes the controller away from the game: release its pad.
androidx.compose.runtime.LaunchedEffect(drawerOpen) { if (drawerOpen) padBridge?.releaseAll() }
androidx.compose.runtime.LaunchedEffect(drawerOpen) {
if (drawerOpen) {
padBridge?.releaseAll()
androidx.compose.runtime.withFrameNanos { }
sessionOverlay.requestFocus()
} else {
releaseDrawerDirection()
}
}
// The PC keyboard takes it too, for moving over the keys; the drawer opens over it.
androidx.compose.runtime.LaunchedEffect(pcKeyboardOpen) { if (pcKeyboardOpen) padBridge?.releaseAll() }
if (pcKeyboardOpen) com.droiddeck.launcher.ui.PcKeyboard(
sendKey = { code, down -> if (CompositorHost.isStarted) WaylandCompositor.nativeSendKey(code, if (down) 1 else 0) },
onAndroidKeyboard = { pcKeyboardOpen = false; keyboard?.toggle() },
onClose = { pcKeyboardOpen = false },
)
SessionDrawer(drawerOpen, DrawerActions(
steam = SessionState.mode == SessionService.MODE_STEAM,
isHomeApp = isHomeApp,
@@ -202,7 +229,8 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
readPrefs()
applyFrameGen()
},
onKeyboard = { drawerOpen = false; keyboard?.toggle() },
onKeyboard = { drawerOpen = false; pcKeyboardOpen = false; keyboard?.toggle() },
onHardwareKeyboard = { drawerOpen = false; pcKeyboardOpen = !pcKeyboardOpen },
onSteamMenu = if (SessionState.mode == SessionService.MODE_STEAM) ({ sendSteamGuide() }) else null,
onQam = if (SessionState.mode == SessionService.MODE_STEAM) ({ triggerSteamQam() }) else null,
backActionsInverted = backActionsInverted,
@@ -233,7 +261,8 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
}
}
}
})
}
root.addView(sessionOverlay)
setContentView(root)
handleHomeGuideIntent(intent)
@@ -250,7 +279,12 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
// The single action waits out the double-press window so the two actions stay distinct.
onBackPressedDispatcher.addCallback(this, object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
routeBackAction()
// The PC keyboard closes first (B on a controller), then Back is the drawer again.
when {
drawerOpen -> drawerOpen = false
pcKeyboardOpen -> pcKeyboardOpen = false
else -> routeBackAction()
}
}
})
watchSession()
@@ -639,10 +673,17 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
KeyEvent.KEYCODE_VOLUME_UP, KeyEvent.KEYCODE_VOLUME_DOWN, KeyEvent.KEYCODE_VOLUME_MUTE ->
return super.dispatchKeyEvent(event)
}
// With the drawer open the controller drives the drawer, not the game: Android's own
// handling moves focus with the d-pad and stick, and its fallbacks make A select and B Back
// (which closes the drawer) - the same as on the app's main screen.
if (drawerOpen && event.device != null && PadBridge.isFromController(event.device)) return super.dispatchKeyEvent(event)
// Keep keys in the Android/Compose focus tree while the drawer is open. Its Back key and
// controller B always dismiss the drawer; directional and confirm keys stay with the UI.
if (drawerOpen) {
if (event.keyCode == KeyEvent.KEYCODE_BACK || event.keyCode == KeyEvent.KEYCODE_BUTTON_B) {
if (event.action == KeyEvent.ACTION_DOWN && event.repeatCount == 0) drawerOpen = false
releaseDrawerDirection()
return true
}
return super.dispatchKeyEvent(event)
}
if (pcKeyboardOpen && event.device != null && PadBridge.isFromController(event.device)) return super.dispatchKeyEvent(event)
if (event.keyCode != KeyEvent.KEYCODE_BACK && padBridge?.onKeyEvent(event) == true) return true
// A hardware keyboard, forwarded to the compositor's wl_keyboard. Back is left to the
// activity, which opens the drawer.
@@ -684,12 +725,60 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
}
override fun dispatchGenericMotionEvent(event: MotionEvent): Boolean {
if (drawerOpen && event.device != null && PadBridge.isFromController(event.device)) return super.dispatchGenericMotionEvent(event)
if (drawerOpen && event.device != null && PadBridge.isFromController(event.device)) {
dispatchDrawerDirection(event)
return true
}
if (drawerDirectionKey != KeyEvent.KEYCODE_UNKNOWN) releaseDrawerDirection()
if (pcKeyboardOpen && event.device != null && PadBridge.isFromController(event.device)) return super.dispatchGenericMotionEvent(event)
if (padBridge?.onMotionEvent(event) == true) return true
if (event.isFromSource(android.view.InputDevice.SOURCE_MOUSE) && !drawerOpen && onMouse(event)) return true
return super.dispatchGenericMotionEvent(event)
}
/** Convert a held left-stick or d-pad-hat direction into focus navigation. */
private fun dispatchDrawerDirection(event: MotionEvent) {
val hatX = event.getAxisValue(MotionEvent.AXIS_HAT_X)
val hatY = event.getAxisValue(MotionEvent.AXIS_HAT_Y)
val x = if (abs(hatX) > 0.4f) hatX else event.getAxisValue(MotionEvent.AXIS_X)
val y = if (abs(hatY) > 0.4f) hatY else event.getAxisValue(MotionEvent.AXIS_Y)
val keyCode = when {
maxOf(abs(x), abs(y)) < 0.45f -> KeyEvent.KEYCODE_UNKNOWN
abs(x) > abs(y) && x < 0f -> KeyEvent.KEYCODE_DPAD_LEFT
abs(x) > abs(y) -> KeyEvent.KEYCODE_DPAD_RIGHT
y < 0f -> KeyEvent.KEYCODE_DPAD_UP
else -> KeyEvent.KEYCODE_DPAD_DOWN
}
val now = SystemClock.uptimeMillis()
if (keyCode != drawerDirectionKey) {
releaseDrawerDirection()
if (keyCode == KeyEvent.KEYCODE_UNKNOWN) return
drawerDirectionKey = keyCode
drawerDirectionDeviceId = event.deviceId
drawerDirectionDownTime = now
drawerDirectionLastRepeat = now
dispatchDrawerKey(KeyEvent.ACTION_DOWN, 0, now)
} else if (keyCode != KeyEvent.KEYCODE_UNKNOWN && now - drawerDirectionLastRepeat >= 240L) {
drawerDirectionLastRepeat = now
dispatchDrawerKey(KeyEvent.ACTION_DOWN, 1, now)
}
}
private fun releaseDrawerDirection() {
if (drawerDirectionKey == KeyEvent.KEYCODE_UNKNOWN) return
dispatchDrawerKey(KeyEvent.ACTION_UP, 0, SystemClock.uptimeMillis())
drawerDirectionKey = KeyEvent.KEYCODE_UNKNOWN
drawerDirectionDeviceId = -1
}
private fun dispatchDrawerKey(action: Int, repeatCount: Int, eventTime: Long) {
val event = KeyEvent(
drawerDirectionDownTime, eventTime, action, drawerDirectionKey,
repeatCount, 0, drawerDirectionDeviceId, 0, 0, InputDevice.SOURCE_DPAD,
)
super.dispatchKeyEvent(event)
}
/**
* The pointer. Touch goes through [PointerGestures] (tap, hold, drag, two-finger scroll); a
* mouse arrives with real buttons and a wheel and is forwarded as it is. Every position is
@@ -19,7 +19,7 @@ object Library {
class Emulator(val id: String, val name: String, val system: String, val program: String, val installed: Boolean, val games: List<Rom>) {
/** The emulator's own icon, bundled (the runtime keeps them as theme SVGs the app cannot draw). */
val iconRes: Int get() = when (id) {
"rpcs3" -> R.drawable.emu_rpcs3; "pcsx2" -> R.drawable.emu_pcsx2; "dolphin" -> R.drawable.emu_dolphin
"rpcs3" -> R.drawable.emu_rpcs3; "armsx2" -> R.drawable.emu_pcsx2; "dolphin" -> R.drawable.emu_dolphin
"duckstation" -> R.drawable.emu_duckstation; "melonds" -> R.drawable.emu_melonds; "cemu" -> R.drawable.emu_cemu
"ppsspp" -> R.drawable.emu_ppsspp; else -> R.drawable.emu_retroarch
}
@@ -61,7 +61,9 @@ object Library {
private class Spec(val id: String, val name: String, val system: String, val program: String, val folders: List<String>, val exts: Set<String>)
private val specs = listOf(
Spec("rpcs3", "RPCS3", "PS3", "/opt/appimages/rpcs3.AppImage", listOf("ps3"), setOf("iso")),
Spec("pcsx2", "PCSX2", "PS2", "/opt/appimages/pcsx2.AppImage", listOf("ps2"), setOf("iso", "chd", "cso", "gz")),
// PS2: ARMSX2, the PCSX2 fork with ARM64 recompilers. Upstream PCSX2 interprets the PS2's
// CPUs on ARM64 (NFS Underground 2: 17 fps against ARMSX2's full 60), so it is not offered.
Spec("armsx2", "ARMSX2", "PS2", "/opt/appimages/armsx2.AppImage", listOf("ps2"), setOf("iso", "chd", "cso", "gz")),
Spec("dolphin", "Dolphin", "GameCube / Wii", "/opt/appimages/dolphin.AppImage", listOf("gc", "gamecube", "wii"), setOf("iso", "rvz", "gcz", "wbfs", "ciso")),
Spec("duckstation", "DuckStation", "PS1", "/opt/appimages/duckstation.AppImage", listOf("ps1", "psx"), setOf("cue", "chd", "pbp", "iso", "bin", "img", "ecm", "m3u")),
Spec("melonds", "melonDS", "DS", "/opt/appimages/melonds.AppImage", listOf("ds", "nds"), setOf("nds", "dsi")),
@@ -70,7 +72,7 @@ object Library {
Spec("retroarch", "RetroArch", "many systems", "/usr/bin/retroarch", emptyList(), emptySet()),
)
private val installedIds = mapOf(
"rpcs3" to "rpcs3", "pcsx2" to "pcsx2", "dolphin" to "dolphin", "duckstation" to "duckstation",
"rpcs3" to "rpcs3", "armsx2" to "armsx2", "dolphin" to "dolphin", "duckstation" to "duckstation",
"melonds" to "melonds", "cemu" to "cemu", "ppsspp" to "emulators", "retroarch" to "emulators",
)
@@ -38,6 +38,7 @@ object SessionFiles {
"usr/local/bin/bannerlator-desktop-games" to "usr/local/bin/bannerlator-desktop-games",
"usr/local/bin/bannerlator-steam-shim" to "usr/local/bin/bannerlator-steam-shim",
"usr/local/bin/bannerlator-steam-shortcuts" to "usr/local/bin/bannerlator-steam-shortcuts",
"usr/local/bin/bannerlator-pad-defaults" to "usr/local/bin/bannerlator-pad-defaults",
// The SteamOS helpers the client calls in Deck mode: the two Armada found it needs, plus
// the three under /usr/bin, all no-ops that answer "nothing to do" (see each file).
"usr/bin/steamos-update" to "usr/bin/steamos-update",
@@ -56,6 +57,9 @@ object SessionFiles {
// staging them into a runtime without it would make the desktop look present when it is not.
val desktop = arrayOf(
"usr/local/bin/droiddeck-desktop" to "usr/local/bin/droiddeck-desktop",
// Games and emulators from the menu, full screen in a gamescope of their own.
"usr/local/bin/droiddeck-gpu" to "usr/local/bin/droiddeck-gpu",
"usr/local/bin/droiddeck-desktop-gpu" to "usr/local/bin/droiddeck-desktop-gpu",
"etc/xdg/labwc/autostart" to "etc/xdg/labwc/autostart",
"etc/xdg/labwc/rc.xml" to "etc/xdg/labwc/rc.xml",
"etc/xdg/lxqt/panel.conf" to "etc/xdg/lxqt/panel.conf",
@@ -75,9 +79,13 @@ object SessionFiles {
"usr/local/lib/mangoapp/libtraceevent.so.1",
"usr/local/lib/mangoapp/libtracefs.so.1",
).map { it to it }
// The patched wlroots (tools/wlroots) the desktop loads for its vulkan / gles2 renderers.
val wlroots = if (File(root, "usr/bin/labwc").isFile) {
arrayOf("usr/local/lib/droiddeck-wlroots/libwlroots-0.20.so" to "usr/local/lib/droiddeck-wlroots/libwlroots-0.20.so")
} else emptyArray()
val optional = (arrayOf(
"usr/local/bin/gamescope" to "usr/local/bin/gamescope",
) + mangoapp).filter { (asset, _) ->
) + wlroots + mangoapp).filter { (asset, _) ->
val dir = asset.substringBeforeLast('/')
runCatching { context.assets.list("linuxfs/$dir")?.contains(asset.substringAfterLast('/')) == true }.getOrDefault(false)
}
@@ -343,14 +343,18 @@ object SessionPrefs {
}
/**
* What the desktop shell composites with: pixman (software, the default - the Adreno stand-in
* is not a DRM render node, so labwc's gbm allocator cannot use it), or gles2 / vulkan for a
* device that has a real node. `Download/droiddeck-wlr-renderer` still overrides it.
* What the desktop shell composites with: vulkan (the default) or gles2 on the GPU, through the
* app's patched wlroots (tools/wlroots) - the Adreno stand-in is not a DRM device and stock
* wlroots cannot allocate on it - or pixman in software. A GPU renderer a device cannot start
* falls back to pixman by itself (droiddeck-desktop). `Download/droiddeck-wlr-renderer` still
* overrides it.
*/
fun desktopRenderer(context: Context): String = prefs(context).getString("desktopRenderer", "pixman") ?: "pixman"
fun desktopRenderer(context: Context): String = prefs(context).getString("desktopRenderer", "vulkan") ?: "vulkan"
fun setDesktopRenderer(context: Context, renderer: String) {
prefs(context).edit().putString("desktopRenderer", renderer).apply()
// A choice made again is a retry: forget that a renderer failed to start here before.
java.io.File(com.droiddeck.launcher.runtime.LinuxRuntime.rootDir(context), "root/.droiddeck-renderer-failed").delete()
}
/**
@@ -280,7 +280,12 @@ class SessionService : Service() {
// An imported glibc Turnip for this mode, when the user chose one: the session script checks
// the manifest and its library from inside and points the loader at it with VK_DRIVER_FILES,
// so the runtime's own driver above stays untouched and is what a bad import falls back to.
val linuxDriverId = SessionPrefs.linuxDriver(this, SessionPrefs.prefMode(SessionState.mode))
// A program from the rail is one of the desktop's emulators, so it draws with the desktop's
// Linux driver, not the Steam session's: the driver's shader cache is keyed on the driver
// build, and with two drivers every emulator compiled its shaders twice - once per way of
// starting it (RPCS3's 6650 interpreter variants on each first boot).
val driverMode = if (SessionState.mode == MODE_RUN) MODE_DESKTOP else SessionPrefs.prefMode(SessionState.mode)
val linuxDriverId = SessionPrefs.linuxDriver(this, driverMode)
LinuxVulkanDriver.resolveIcdPath(this, linuxDriverId)
?.let { guest.add(LinuxVulkanDriver.ENV + "=" + it) }
// Turnip's own debug switches, for the runtime's driver and everything on it. The file in
@@ -321,13 +326,13 @@ class SessionService : Service() {
// on, even naming every core: it exists to undo the pin Steam applies to its own interface
// renderer, and the scheduler's default is exactly what that pin takes away. A game's is
// sent only when it is a real restriction - a game has no pin of its own to undo.
if (SessionState.mode == MODE_STEAM) {
if (SessionPrefs.clientCpusOverride(this)) {
guest.add("BL_CLIENT_CPUS=" + CpuCores.listOrAll(SessionPrefs.clientCpus(this)))
}
CpuCores.restrictionOrEmpty(SessionPrefs.gameCpus(this))
.takeIf { it.isNotEmpty() }?.let { guest.add("BL_GAME_CPUS=$it") }
if (SessionState.mode == MODE_STEAM && SessionPrefs.clientCpusOverride(this)) {
guest.add("BL_CLIENT_CPUS=" + CpuCores.listOrAll(SessionPrefs.clientCpus(this)))
}
// The game cores also pin the desktop and a program from the rail; without a choice the
// session script picks every core but the slowest cluster for those (program_cores).
CpuCores.restrictionOrEmpty(SessionPrefs.gameCpus(this))
.takeIf { it.isNotEmpty() }?.let { guest.add("BL_GAME_CPUS=$it") }
// PulseAudio always: the client is a native Linux program and has no other way to make a
// sound - its menus, its music and its voice chat all go through here. DirectAudio is not
@@ -398,7 +403,11 @@ class SessionService : Service() {
// identity gets the standard layout without the user configuring the pad by hand.
guest.add("FAKE_EVDEV_IDENTITY=xbox360")
guest.add("FAKE_EVDEV_VIBRATION=1")
guest.add("FAKE_EVDEV_STEAM_VIRTUAL=1")
// Steam Input's virtual-gamepad identity is for games the client starts, which are
// meant to see that pad. Everywhere else (the desktop, a program from the rail) it
// hides the pad: SDL ignores a Steam virtual gamepad unless it runs under Steam, so
// every SDL emulator came up with no controller. There it is a plain Xbox 360 pad.
if (SessionState.mode == MODE_STEAM) guest.add("FAKE_EVDEV_STEAM_VIRTUAL=1")
guest.add("SDL_JOYSTICK_DISABLE_UDEV=1")
guest.add("SDL_HIDAPI_JOYSTICK_DISABLE_UDEV=1")
guest.add("SDL_JOYSTICK_HIDAPI=0")
@@ -407,12 +416,11 @@ class SessionService : Service() {
}
SessionState.fakeInputDir = fakeInputDir
}
// The desktop is wlroots (labwc), and wlroots allocates its buffers through gbm on a real
// DRM render node. Ours is a KGSL stand-in that gbm cannot use - labwc dies at "unable to
// create allocator" - so the desktop shell is composited by pixman (software, a shm
// allocator, no DRM). Accelerated clients on it pay a CPU copy; a 2D emulator does not
// notice, a demanding one does. droiddeck-wlr-renderer in Downloads (pixman/vulkan/gles2)
// overrides it, for trying acceleration on a device that has a real node.
// The desktop is wlroots (labwc). Stock wlroots allocates through gbm on a real DRM render
// node, and ours is a KGSL stand-in - labwc died at "unable to create allocator" - so the
// default is pixman (software, shm), where a Vulkan program cannot draw at all; those run
// through droiddeck-gpu instead. vulkan / gles2 use the app's patched wlroots and fall back
// to pixman by themselves. droiddeck-wlr-renderer in Downloads overrides the choice.
if (SessionState.mode == MODE_DESKTOP) {
val override = File(Environment.getExternalStorageDirectory(), "Download/droiddeck-wlr-renderer")
.takeIf { it.isFile }?.let { FileUtils.readString(it)?.trim() }
@@ -332,8 +332,11 @@ fun ModeSettingsPage(s: ModeSettings, a: ModeSettingsActions) {
if (!steam && s.renderer != null) SettingsGroup("Renderer") {
ChoiceRow(
host, "renderer", "Desktop renderer", "Composites the desktop.",
listOf("pixman" to "pixman - software", "gles2" to "gles2", "vulkan" to "vulkan"), s.renderer,
note = "GLES2 and Vulkan require a DRM render node, unavailable on most devices.", onPick = a.onRenderer,
listOf("vulkan" to "vulkan - GPU", "gles2" to "gles2 - GPU (experimental)", "pixman" to "pixman - software"), s.renderer,
note = "On Vulkan, programs on the desktop draw with the GPU in their own windows; if it cannot start, " +
"the desktop comes up on pixman. On pixman, games and emulators from the menu open full screen " +
"on the GPU instead (right-click one for a desktop window).",
onPick = a.onRenderer,
)
}
}
@@ -0,0 +1,251 @@
package com.droiddeck.launcher.ui
import android.view.KeyEvent
import androidx.compose.animation.animateColorAsState
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.LocalIndication
import androidx.compose.foundation.focusable
import androidx.compose.foundation.gestures.awaitEachGesture
import androidx.compose.foundation.gestures.awaitFirstDown
import androidx.compose.foundation.gestures.waitForUpOrCancellation
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.interaction.collectIsFocusedAsState
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateMapOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.key.nativeKeyCode
import androidx.compose.ui.input.key.onPreviewKeyEvent
import androidx.compose.ui.input.key.type
import androidx.compose.ui.input.key.KeyEventType
import androidx.compose.ui.input.key.key
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
/**
* A PC keyboard on screen: every key is a physical key (a Linux evdev code) pressed and released
* on the session's wl_keyboard, as a USB keyboard would - Esc, the function row, Ctrl, Alt, Super,
* the arrows and the editing block included, none of which the Android keyboard can send.
*
* A key goes down when a finger lands on it and up when it lifts, so holding one repeats it the way
* the program's own key repeat does. Ctrl, Shift, Alt and Super are sticky: one tap holds the
* modifier for the next key, a second tap locks it, a third lets it go. A controller moves over the
* keys with the d-pad and presses with A.
*/
private data class PcKey(val label: String, val code: Int, val weight: Float = 1f, val shifted: String? = null)
private fun k(label: String, code: Int, weight: Float = 1f, shifted: String? = null) = PcKey(label, code, weight, shifted)
private val ROWS: List<List<PcKey>> = listOf(
listOf(
k("Esc", 1), k("F1", 59), k("F2", 60), k("F3", 61), k("F4", 62), k("F5", 63), k("F6", 64),
k("F7", 65), k("F8", 66), k("F9", 67), k("F10", 68), k("F11", 87), k("F12", 88),
k("Ins", 110), k("Del", 111), k("Home", 102), k("End", 107), k("PgUp", 104), k("PgDn", 109),
),
listOf(
k("`", 41, shifted = "~"), k("1", 2, shifted = "!"), k("2", 3, shifted = "@"), k("3", 4, shifted = "#"),
k("4", 5, shifted = "$"), k("5", 6, shifted = "%"), k("6", 7, shifted = "^"), k("7", 8, shifted = "&"),
k("8", 9, shifted = "*"), k("9", 10, shifted = "("), k("0", 11, shifted = ")"), k("-", 12, shifted = "_"),
k("=", 13, shifted = "+"), k("⌫ Backspace", 14, 2f),
),
listOf(
k("Tab ⇥", 15, 1.5f), k("q", 16), k("w", 17), k("e", 18), k("r", 19), k("t", 20), k("y", 21),
k("u", 22), k("i", 23), k("o", 24), k("p", 25), k("[", 26, shifted = "{"), k("]", 27, shifted = "}"),
k("\\", 43, 1.5f, shifted = "|"),
),
listOf(
k("Caps", 58, 1.75f), k("a", 30), k("s", 31), k("d", 32), k("f", 33), k("g", 34), k("h", 35),
k("j", 36), k("k", 37), k("l", 38), k(";", 39, shifted = ":"), k("'", 40, shifted = "\""),
k("Enter ⏎", 28, 2.25f),
),
listOf(
k("⇧ Shift", 42, 2.25f), k("z", 44), k("x", 45), k("c", 46), k("v", 47), k("b", 48), k("n", 49),
k("m", 50), k(",", 51, shifted = "<"), k(".", 52, shifted = ">"), k("/", 53, shifted = "?"),
k("⇧ Shift", 54, 1.75f), k("↑", 103),
),
listOf(
k("Ctrl", 29, 1.5f), k("Super", 125, 1.25f), k("Alt", 56, 1.25f), k("Space", 57, 6.25f),
k("AltGr", 100, 1.25f), k("Ctrl", 97, 1.25f), k("←", 105), k("↓", 108), k("→", 106),
),
)
private val MODIFIERS = setOf(42, 54, 29, 97, 56, 100, 125)
private const val MOD_OFF = 0
private const val MOD_ONCE = 1
private const val MOD_LOCKED = 2
@Composable
fun PcKeyboard(
/** evdev code, pressed. */
sendKey: (Int, Boolean) -> Unit,
onAndroidKeyboard: () -> Unit,
onClose: () -> Unit,
) {
val pal = LocalPalette.current
val colors = MaterialTheme.colorScheme
val mods = remember { mutableStateMapOf<Int, Int>() }
var capsOn by remember { mutableStateOf(false) }
val firstKey = remember { FocusRequester() }
val shifted = (mods[42] ?: MOD_OFF) != MOD_OFF || (mods[54] ?: MOD_OFF) != MOD_OFF
fun releaseOnceModifiers() {
for ((code, state) in mods.toMap()) if (state == MOD_ONCE) { sendKey(code, false); mods[code] = MOD_OFF }
}
fun press(key: PcKey) {
if (key.code in MODIFIERS) {
when (mods[key.code] ?: MOD_OFF) {
MOD_OFF -> { sendKey(key.code, true); mods[key.code] = MOD_ONCE }
MOD_ONCE -> mods[key.code] = MOD_LOCKED
else -> { sendKey(key.code, false); mods[key.code] = MOD_OFF }
}
return
}
sendKey(key.code, true)
}
fun release(key: PcKey) {
if (key.code in MODIFIERS) return
sendKey(key.code, false)
if (key.code == 58) capsOn = !capsOn
releaseOnceModifiers()
}
// Nothing is left held when the keyboard goes away.
androidx.compose.runtime.DisposableEffect(Unit) {
onDispose { for ((code, state) in mods.toMap()) if (state != MOD_OFF) sendKey(code, false) }
}
LaunchedEffect(Unit) { runCatching { firstKey.requestFocus() } }
BoxWithConstraints(Modifier.fillMaxSize(), contentAlignment = Alignment.BottomCenter) {
// Slightly under half the screen, header included, so the program stays in view above it:
// six rows share what is left of 47% after the header, the gaps and the padding.
val rowHeight = ((maxHeight * 0.47f - 24.dp - 3.dp * 6 - 10.dp) / 6).coerceIn(24.dp, 44.dp)
Box(
Modifier.fillMaxWidth()
.background(Color(0xE6101418), RoundedCornerShape(topStart = 14.dp, topEnd = 14.dp))
.border(1.dp, pal.line2, RoundedCornerShape(topStart = 14.dp, topEnd = 14.dp))
// Touches on the keyboard stop here; the program underneath never sees them.
.pointerInput(Unit) { awaitEachGesture { awaitFirstDown().consume(); waitForUpOrCancellation()?.consume() } }
.padding(horizontal = 8.dp, vertical = 5.dp),
) {
Column(verticalArrangement = Arrangement.spacedBy(3.dp)) {
Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier.fillMaxWidth().height(24.dp)) {
Text("PC keyboard", fontSize = 12.sp, fontWeight = FontWeight.SemiBold, color = colors.onSurfaceVariant)
Spacer(Modifier.weight(1f))
HeaderButton("Android keyboard", onAndroidKeyboard)
Spacer(Modifier.width(8.dp))
HeaderButton("Hide ✕", onClose)
}
ROWS.forEachIndexed { r, row ->
Row(horizontalArrangement = Arrangement.spacedBy(3.dp), modifier = Modifier.fillMaxWidth().height(rowHeight)) {
row.forEachIndexed { i, key ->
val state = mods[key.code] ?: MOD_OFF
val lit = state != MOD_OFF || (key.code == 58 && capsOn)
val label = when {
shifted && key.shifted != null -> key.shifted
key.label.length == 1 && key.label[0].isLetter() && (shifted xor capsOn) -> key.label.uppercase()
else -> key.label
}
KeyCap(
label, key.weight, lit, locked = state == MOD_LOCKED,
modifier = if (r == 0 && i == 0) Modifier.focusRequester(firstKey) else Modifier,
onDown = { press(key) }, onUp = { release(key) },
)
}
}
}
}
}
}
}
@Composable
private fun RowScope.KeyCap(
label: String, weight: Float, lit: Boolean, locked: Boolean,
modifier: Modifier, onDown: () -> Unit, onUp: () -> Unit,
) {
val pal = LocalPalette.current
val src = remember { MutableInteractionSource() }
val focused by src.collectIsFocusedAsState()
var down by remember { mutableStateOf(false) }
val fill by animateColorAsState(
when {
down -> pal.signal.copy(alpha = 0.55f)
lit -> pal.signal.copy(alpha = if (locked) 0.45f else 0.28f)
else -> Color.White.copy(alpha = 0.07f)
}, label = "keyFill",
)
val edge = if (focused) pal.signal else if (lit) pal.signal.copy(alpha = 0.7f) else Color.White.copy(alpha = 0.12f)
Box(
contentAlignment = Alignment.Center,
modifier = modifier.weight(weight).fillMaxHeight()
.background(fill, RoundedCornerShape(7.dp))
.border(if (focused) 2.dp else 1.dp, edge, RoundedCornerShape(7.dp))
.pointerInput(Unit) {
awaitEachGesture {
awaitFirstDown().consume()
down = true; onDown()
waitForUpOrCancellation()?.consume()
down = false; onUp()
}
}
// A controller: A (or the d-pad's centre) is the finger.
.onPreviewKeyEvent { e ->
val kc = e.key.nativeKeyCode
if (kc != KeyEvent.KEYCODE_BUTTON_A && kc != KeyEvent.KEYCODE_DPAD_CENTER && kc != KeyEvent.KEYCODE_ENTER) return@onPreviewKeyEvent false
when (e.type) {
KeyEventType.KeyDown -> if (!down) { down = true; onDown() }
KeyEventType.KeyUp -> if (down) { down = false; onUp() }
}
true
}
.focusable(interactionSource = src),
) {
Text(
label, fontSize = if (label.length > 2) 11.sp else 15.sp, fontWeight = FontWeight.SemiBold,
color = Color.White, textAlign = TextAlign.Center, maxLines = 1,
)
}
}
@Composable
private fun HeaderButton(text: String, onClick: () -> Unit) {
val pal = LocalPalette.current
val src = remember { MutableInteractionSource() }
val focused by src.collectIsFocusedAsState()
Box(
contentAlignment = Alignment.Center,
modifier = Modifier.fillMaxHeight()
.background(Color.White.copy(alpha = 0.08f), RoundedCornerShape(7.dp))
.border(if (focused) 2.dp else 1.dp, if (focused) pal.signal else Color.White.copy(alpha = 0.14f), RoundedCornerShape(7.dp))
.clickable(interactionSource = src, indication = LocalIndication.current, onClick = onClick)
.padding(horizontal = 12.dp),
) { Text(text, fontSize = 12.sp, fontWeight = FontWeight.SemiBold, color = Color.White) }
}
@@ -3,6 +3,7 @@ package com.droiddeck.launcher.ui
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.focusGroup
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.ui.focus.focusRequester
import androidx.compose.material3.ButtonDefaults
@@ -30,7 +31,9 @@ import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Alignment
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.onFocusChanged
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.res.painterResource
@@ -64,6 +67,8 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.input.InputMode
import androidx.compose.ui.platform.LocalInputModeManager
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.text.font.FontWeight
@@ -159,7 +164,10 @@ class DrawerActions(
val selectedSecondScreenDisplay: Int,
val onHud: (Boolean) -> Unit,
val onFrameGenPick: (engine: String, multiplier: Int) -> Unit,
/** The Android keyboard (text, turned into key presses). */
val onKeyboard: () -> Unit,
/** The on-screen PC keyboard: real keys, Esc, F1-F12, Ctrl, Alt... */
val onHardwareKeyboard: () -> Unit,
val onSteamMenu: (() -> Unit)?,
val onQam: (() -> Unit)?,
val onOsc: (String) -> Unit,
@@ -175,6 +183,7 @@ class DrawerActions(
val onClose: () -> Unit,
)
@OptIn(ExperimentalComposeUiApi::class)
@Composable
fun SessionDrawer(open: Boolean, a: DrawerActions) {
val colors = MaterialTheme.colorScheme
@@ -185,11 +194,19 @@ fun SessionDrawer(open: Boolean, a: DrawerActions) {
// A controller starts on the drawer's first control, highlighted, as on the app's main screen:
// the STEAM button, or in a desktop session the first setting.
val firstFocus = remember { androidx.compose.ui.focus.FocusRequester() }
var firstControlFocused by remember { mutableStateOf(false) }
val inputModeManager = LocalInputModeManager.current
val steamButtons = a.onSteamMenu != null && a.onQam != null
LaunchedEffect(open) {
if (open) {
androidx.compose.runtime.withFrameNanos { }
runCatching { firstFocus.requestFocus() }
inputModeManager.requestInputMode(InputMode.Keyboard)
var attempts = 0
while (attempts < 24 && !firstControlFocused) {
firstFocus.requestFocus()
androidx.compose.runtime.withFrameNanos { }
attempts++
}
if (!firstControlFocused) android.util.Log.w("SessionDrawer", "first controller focus request did not focus a control")
}
}
if (open || veil > 0.01f) Box(
@@ -208,6 +225,7 @@ fun SessionDrawer(open: Boolean, a: DrawerActions) {
.width(340.dp)
.background(pal.background.copy(alpha = 0.97f))
.clickable(interactionSource = remember { MutableInteractionSource() }, indication = null) {}
.focusGroup()
.verticalScroll(rememberScrollState())
.padding(horizontal = 14.dp, vertical = 16.dp),
) {
@@ -238,7 +256,8 @@ fun SessionDrawer(open: Boolean, a: DrawerActions) {
OutlinedButton(
onClick = { host.open = null; a.onSteamMenu.invoke() },
interactionSource = steamSrc,
modifier = Modifier.weight(1f).height(48.dp).focusRequester(firstFocus),
modifier = Modifier.weight(1f).height(48.dp).focusRequester(firstFocus)
.onFocusChanged { firstControlFocused = it.isFocused },
shape = RoundedCornerShape(12.dp),
border = BorderStroke(if (steamHot) 2.dp else 1.dp, if (steamHot) pal.signal else colors.outline),
colors = ButtonDefaults.outlinedButtonColors(containerColor = if (steamHot) pal.signal.copy(alpha = 0.16f) else Color.Transparent),
@@ -293,7 +312,8 @@ fun SessionDrawer(open: Boolean, a: DrawerActions) {
SettingsGroup("Now") {
ToggleRow(
host, "hud", "Performance HUD", null, a.hudOn,
chipModifier = if (steamButtons) Modifier else Modifier.focusRequester(firstFocus), onChange = a.onHud,
chipModifier = if (steamButtons) Modifier else Modifier.focusRequester(firstFocus)
.onFocusChanged { firstControlFocused = it.isFocused }, onChange = a.onHud,
)
val fgOpen = host.open == "fg"
val fgLabel = when (a.frameGenEngine) {
@@ -329,7 +349,13 @@ fun SessionDrawer(open: Boolean, a: DrawerActions) {
true to SessionPrefs.BACK_QAM_THEN_MENU,
), a.backActionsInverted, onPick = a.onBackActionsInverted,
)
ActionRow("Keyboard", null, "Show") { host.open = null; a.onKeyboard() }
SettingsRow("Keyboard", null) {
Row(verticalAlignment = Alignment.CenterVertically) {
SecondaryButton("Hardware") { host.open = null; a.onHardwareKeyboard() }
Spacer(Modifier.width(8.dp))
SecondaryButton("Android") { host.open = null; a.onKeyboard() }
}
}
}
if (a.steam && a.secondScreenDisplays.isNotEmpty()) SettingsGroup("Second screen") {