Fix desktop profile selection and simplify default touch layouts

This commit is contained in:
Delete228 committed 2026-11-07 02:10:26 +03:00
1 parent 08526f8283
commit c2d7765619
16 files changed
+265 -88

No files matched your search

@@ -1185,6 +1185,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
SessionDrawer(drawerOpen || (docked && drawerPinned), drawerPage, drawerControllerActive, onPageChange = { drawerPage = it }, docked = docked,
backRequest = drawerBackRequests, onLayerChange = { drawerLayerOpen = it }, a = DrawerActions(
steam = SessionState.mode == SessionService.MODE_STEAM,
desktop = SessionState.mode == SessionService.MODE_DESKTOP,
systemPowerVr = com.droiddeck.launcher.gpu.SystemVulkanDriver.usesDefault(
SessionPrefs.linuxDriver(this@SessionActivity)),
direct = noSteamSession(),
@@ -1,16 +1,49 @@
package com.droiddeck.launcher.input
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.graphics.drawable.Drawable
import com.droiddeck.launcher.R
object InputButtonIcons {
// Persisted symbol IDs must not depend on Android resource IDs or overlap Linux key codes.
const val MENU = 1001
const val KEYBOARD = 1002
const val STEAM = 1003
const val QUICK_ACCESS = 1004
fun has(code: Int): Boolean = code in codes
val codes = listOf(28, 14, 15, 57, 42, 54, 29, 97, 125, 103, 108, 105, 106)
private val keyCodes = listOf(28, 14, 15, 57, 42, 54, 29, 97, 125, 103, 108, 105, 106)
val codes = listOf(MENU, KEYBOARD, STEAM, QUICK_ACCESS) + keyCodes
fun draw(canvas: Canvas, code: Int, cx: Float, cy: Float, box: Float, paint: Paint): Boolean {
if (!has(code)) return false
fun label(context: Context, code: Int): String = when (code) {
MENU -> context.getString(R.string.touch_droiddeck_menu)
KEYBOARD -> context.getString(R.string.drawer_keyboard)
STEAM -> context.getString(R.string.drawer_steam_menu)
QUICK_ACCESS -> context.getString(R.string.drawer_open_qam)
else -> pcKeyLabel(context, code)
}
fun drawable(context: Context, code: Int): Drawable? = when (code) {
MENU -> R.drawable.ic_stat_session
KEYBOARD -> R.drawable.ic_bottom_keyboard
STEAM -> R.drawable.ic_input_steam
QUICK_ACCESS -> R.drawable.ic_input_quick_access
else -> null
}?.let { context.getDrawable(it)?.mutate() }
fun draw(canvas: Canvas, code: Int, cx: Float, cy: Float, box: Float, paint: Paint, drawable: Drawable? = null): Boolean {
if (drawable != null) {
drawable.setTint(paint.color)
val half = box / 2f
drawable.setBounds((cx - half).toInt(), (cy - half).toInt(), (cx + half).toInt(), (cy + half).toInt())
drawable.draw(canvas)
return true
}
if (code !in keyCodes) return false
val h = box / 2f
val stroke = box * 0.085f
val path = Path()
@@ -9,7 +9,7 @@ import org.json.JSONObject
import java.util.UUID
object InputLibraries {
private const val VERSION = 6
private const val VERSION = 7
private const val KEY = "library"
private const val RECOVERY = "library-recovery."
const val OVERLAY = "builtin.overlay"
@@ -76,18 +76,21 @@ object InputLibraries {
private fun migrate(context: Context): InputLibrary {
val profiles = mutableListOf<SavedInputProfile>()
val layouts = mutableListOf(
SavedInputLayout(OVERLAY, "Built-in gamepad", template = InputLayoutTemplate.OVERLAY),
SavedInputLayout(DECK, "Deck grips + trackpads", template = InputLayoutTemplate.DECK),
SavedInputLayout(TRACKPAD, "Trackpad + keyboard", template = InputLayoutTemplate.TRACKPAD),
)
val bindings = mutableListOf<SavedInputBindings>()
val stock = InputStockLayouts.layouts()
val layouts = (listOf(SavedInputLayout(OVERLAY, "Built-in gamepad", template = InputLayoutTemplate.OVERLAY)) + stock).toMutableList()
val bindings = InputStockLayouts.presets(stock).toMutableList()
for (id in listOf(InputProfile.GAME, InputProfile.DESKTOP)) {
val old = InputProfiles.readLegacy(context, id)
val name = if (id == InputProfile.GAME) "Game" else "Desktop"
val layout = SavedInputLayout("$id.layout", "$name touch", InputSurfaceLayout.readLegacy(context, id))
layouts += layout
val legacySurface = InputSurfaceLayout.hasLegacy(context, id) ||
old.bindings[InputRole.SECONDARY] != InputProfile.defaults(id).bindings[InputRole.SECONDARY]
val layout = if (legacySurface) SavedInputLayout("$id.layout", "$name touch", InputSurfaceLayout.readLegacy(context, id)).also { layouts += it }
else stock.first { it.id == if (id == InputProfile.DESKTOP) TRACKPAD else DECK }
val parts = InputRole.entries.associateWith { role ->
if (role == InputRole.SECONDARY && !legacySurface) {
val preset = bindings.first { it.layoutId == layout.id }
return@associateWith InputPart(preset.id, layout.id)
}
val preset = SavedInputBindings("$id.${role.name.lowercase()}", "$name / ${role.name.lowercase()}",
role == InputRole.PHYSICAL, if (role == InputRole.SECONDARY) layout.id else null,
old.bindings[role].orEmpty(), keys = if (role == InputRole.PHYSICAL) old.keys else emptyList())
@@ -203,7 +203,7 @@ data class InputLibrary(
val preset = SavedInputBindings(if (isPrivate) previous.id else newInputId(),
if (isPrivate) previous.name else (p.name + " / " + role.name.lowercase()).take(80), role == InputRole.PHYSICAL,
part.layout?.takeIf { layout(id, role)?.editable == true }, defaults.bindings[role].orEmpty(),
elements = layout(id, role)?.elements.orEmpty().filter { it.id == "builtin:keyboard" }
elements = (layout(id, role)?.let { InputStockLayouts.actions(it) } ?: emptyMap()) + layout(id, role)?.elements.orEmpty().filter { it.id == "builtin:keyboard" }
.associate { it.id to InputAction.Command(InputCommand.KEYBOARD) })
result = result.withPreset(preset)
part.copy(bindings = preset.id)
@@ -3,14 +3,14 @@ package com.droiddeck.launcher.input
internal object InputStockLayouts {
const val DECK = "preset.deck"
const val TRACKPAD = "preset.trackpad"
const val LEFT = "preset.left-pad"
const val RIGHT = "preset.right-pad"
const val BOTH = "preset.both-pads"
const val GRIPS = "preset.grips"
const val GAMEPAD = "preset.gamepad"
private fun commands() = listOf("Menu", "Keyboard", "Steam", "Quick access").mapIndexed { i, name ->
InputSurfaceElement(InputControl.A, .02f + i * .245f, .01f, .225f, .11f, "command:$i", name = name)
private fun commands(indices: List<Int>): List<InputSurfaceElement> {
val names = listOf("DroidDeck menu", "Keyboard", "Steam", "Quick access")
val icons = listOf(InputButtonIcons.MENU, InputButtonIcons.KEYBOARD, InputButtonIcons.STEAM, InputButtonIcons.QUICK_ACCESS)
val step = .98f / indices.size
return indices.mapIndexed { position, i ->
InputSurfaceElement(InputControl.A, .02f + position * step, .01f, step - .02f, .11f,
"command:$i", name = names[i], icon = icons[i])
}
}
private fun commandActions(): Map<String, InputAction> = listOf(InputCommand.MENU, InputCommand.KEYBOARD, InputCommand.STEAM, InputCommand.QUICK_ACCESS)
.mapIndexed { i, command -> "command:$i" to InputAction.Command(command) }.toMap()
@@ -19,66 +19,23 @@ internal object InputStockLayouts {
}
private fun pads() = listOf(InputSurfaceElement(InputControl.LEFT_PAD, .02f, .14f, .47f, .62f),
InputSurfaceElement(InputControl.RIGHT_PAD, .51f, .14f, .47f, .62f))
private fun single(right: Boolean) = listOf(InputSurfaceElement(if (right) InputControl.RIGHT_PAD else InputControl.LEFT_PAD, .02f, .14f, .96f, .62f),
InputSurfaceElement(if (right) InputControl.RIGHT_CLICK else InputControl.LEFT_CLICK, .02f, .78f, .96f, .20f, name = "Click"))
fun layouts() = listOf(
SavedInputLayout(InputLibraries.DECK, "Deck grips + trackpads", commands() + pads() + grips()),
SavedInputLayout(InputLibraries.TRACKPAD, "Trackpad + keyboard", commands().take(2).mapIndexed { i, e -> e.copy(x = .02f + i * .49f, width = .47f) } + listOf(
InputSurfaceElement(InputControl.RIGHT_PAD, .02f, .14f, .96f, .62f, mouseGestures = true),
InputSurfaceElement(InputControl.A, .02f, .78f, .47f, .20f, name = "Left click"),
InputSurfaceElement(InputControl.B, .51f, .78f, .47f, .20f, name = "Right click"))),
SavedInputLayout("builtin.left-pad", "Left Deck trackpad", commands() + single(false)),
SavedInputLayout("builtin.right-pad", "Right Deck trackpad", commands() + single(true)),
SavedInputLayout("builtin.both-pads", "Two Deck trackpads", commands() + pads() + listOf(
InputSurfaceElement(InputControl.LEFT_CLICK, .02f, .78f, .47f, .20f, name = "Left pad click"),
InputSurfaceElement(InputControl.RIGHT_CLICK, .51f, .78f, .47f, .20f, name = "Right pad click"))),
SavedInputLayout("builtin.grips", "Deck grips", commands() + listOf(InputControl.L4, InputControl.R4, InputControl.L5, InputControl.R5).mapIndexed { i, c ->
InputSurfaceElement(c, .02f + (i % 2) * .49f, .14f + (i / 2) * .43f, .47f, .41f)
}),
SavedInputLayout("builtin.gamepad", "Compact gamepad", gamepad()),
SavedInputLayout(InputLibraries.DECK, "Deck grips + trackpads", commands(listOf(2, 3, 0)) + pads() + grips(), screen = InputLayoutScreen.SECONDARY),
SavedInputLayout(InputLibraries.TRACKPAD, "Trackpad + keyboard", commands(listOf(0, 1)) +
InputSurfaceElement(InputControl.RIGHT_PAD, .02f, .14f, .96f, .84f, mouseGestures = true), screen = InputLayoutScreen.SECONDARY),
)
private fun gamepad(): List<InputSurfaceElement> {
val w = 600f; val h = 500f
val boxes = GamepadGeometry.layout(w, h, 1f, portraitDock = true)
val result = mutableListOf<InputSurfaceElement>()
val controls = mapOf(GamepadGeometry.Slot.LEFT_MAIN to InputControl.LS, GamepadGeometry.Slot.RIGHT_MAIN to InputControl.RS,
GamepadGeometry.Slot.LEFT_OUTER_SHOULDER to InputControl.LT, GamepadGeometry.Slot.LEFT_INNER_SHOULDER to InputControl.LB,
GamepadGeometry.Slot.RIGHT_INNER_SHOULDER to InputControl.RB, GamepadGeometry.Slot.RIGHT_OUTER_SHOULDER to InputControl.RT,
GamepadGeometry.Slot.VIEW to InputControl.SELECT, GamepadGeometry.Slot.MENU to InputControl.START,
GamepadGeometry.Slot.STEAM to InputControl.GUIDE, GamepadGeometry.Slot.QAM to InputControl.QAM,
GamepadGeometry.Slot.L4 to InputControl.L4, GamepadGeometry.Slot.L5 to InputControl.L5,
GamepadGeometry.Slot.R4 to InputControl.R4, GamepadGeometry.Slot.R5 to InputControl.R5)
for ((slot, box) in boxes) {
fun add(c: InputControl, x: Float = box.left, y: Float = box.top, width: Float = box.width, height: Float = box.height,
id: String = "legacy:${c.id}", kind: InputElementKind = InputElementKind.from(c), name: String = c.label) {
result += InputSurfaceElement(c, x / w, y / h, width / w, height / h, id, kind, name, floating = false).bounded()
}
when (slot) {
GamepadGeometry.Slot.LEFT_SECOND -> add(InputControl.LS, id = "compact:dpad", kind = InputElementKind.DPAD, name = "D-pad")
GamepadGeometry.Slot.RIGHT_SECOND -> {
val size = minOf(box.width, box.height) * .32f
add(InputControl.Y, box.cx - size / 2, box.top, size, size)
add(InputControl.X, box.left, box.cy - size / 2, size, size)
add(InputControl.B, box.right - size, box.cy - size / 2, size, size)
add(InputControl.A, box.cx - size / 2, box.bottom - size, size, size)
}
GamepadGeometry.Slot.PANELS -> add(InputControl.A, id = "command:0", name = "Menu")
else -> controls[slot]?.let { add(it) }
}
}
return result
}
fun actions(layout: SavedInputLayout): Map<String, InputAction> = commandActions().filterKeys { id -> layout.elements.any { it.id == id } } + if (layout.id == InputLibraries.TRACKPAD) mapOf(
fun actions(layout: SavedInputLayout): Map<String, InputAction> = (commandActions() + if (layout.id == InputLibraries.TRACKPAD) mapOf(
"legacy:right-pad" to InputAction.Pointer(), "legacy:a" to InputAction.MouseButton(272), "legacy:b" to InputAction.MouseButton(273),
) else if (layout.id == "builtin.gamepad") mapOf("compact:dpad" to InputAction.Native()) else emptyMap()
) else emptyMap()).filterKeys { id -> layout.elements.any { it.id == id } }
fun clicks(layout: SavedInputLayout): Map<String, InputAction> = if (layout.id == InputLibraries.TRACKPAD)
mapOf("legacy:right-pad" to InputAction.MouseButton(272)) else emptyMap()
fun presets(layouts: List<SavedInputLayout>) = layouts.zip(listOf(DECK, TRACKPAD, LEFT, RIGHT, BOTH, GRIPS, GAMEPAD)) { layout, id ->
SavedInputBindings(id, layout.name, false, layout.id, elements = actions(layout), clicks = clicks(layout))
fun presets(layouts: List<SavedInputLayout>) = layouts.map { layout ->
val desktop = layout.id == InputLibraries.TRACKPAD
SavedInputBindings(if (desktop) TRACKPAD else DECK, layout.name, false, layout.id,
controls = InputProfile.defaults(if (desktop) InputProfile.DESKTOP else InputProfile.GAME).bindings[InputRole.SECONDARY].orEmpty(),
elements = actions(layout), clicks = clicks(layout))
}
}
@@ -58,6 +58,8 @@ object InputSurfaceLayout {
fun read(context: Context, profile: String, role: InputRole = InputRole.SECONDARY): List<InputSurfaceElement> =
InputLibraries.read(context).layout(profile, role)?.elements.orEmpty()
internal fun hasLegacy(context: Context, profile: String) = prefs(context).contains(profile)
internal fun readLegacy(context: Context, profile: String): List<InputSurfaceElement> {
val text = prefs(context).getString(profile, null) ?: return defaults()
return runCatching {
@@ -3,6 +3,7 @@ package com.droiddeck.launcher.ui
import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.foundation.Canvas
@@ -26,14 +27,16 @@ internal fun InputElementProperties(element: InputSurfaceElement, onChange: (Inp
val open = host.open == "element-icon"
SettingsRow(stringResource(R.string.input_button_symbol), null, highlighted = open) {
Box {
ValueChip(if (element.image.isNotEmpty()) stringResource(R.string.input_imported_image) else if (element.icon == 0) stringResource(R.string.input_button_text) else pcKeyLabel(context, element.icon), open) { host.open = if (open) null else "element-icon" }
ValueChip(if (element.image.isNotEmpty()) stringResource(R.string.input_imported_image) else if (element.icon == 0) stringResource(R.string.input_button_text) else InputButtonIcons.label(context, element.icon), open) { host.open = if (open) null else "element-icon" }
AnchoredMenu(open, onDismiss = { host.open = null }, title = stringResource(R.string.input_button_symbol)) { firstItemFocus ->
MenuItem(stringResource(R.string.input_button_text), element.icon == 0 && element.image.isEmpty(), focusRequester = firstItemFocus) { onChange(element.copy(icon = 0, image = "")); host.open = null }
for (code in InputButtonIcons.codes) MenuItem(pcKeyLabel(context, code), element.icon == code && element.image.isEmpty(), leading = {
for (code in InputButtonIcons.codes) MenuItem(InputButtonIcons.label(context, code), element.icon == code && element.image.isEmpty(), leading = {
val color = MaterialTheme.colorScheme.onSurface
val symbol = remember(context, code) { InputButtonIcons.drawable(context, code) }
val paint = remember { android.graphics.Paint(android.graphics.Paint.ANTI_ALIAS_FLAG) }
Canvas(Modifier.size(22.dp)) {
val paint = android.graphics.Paint(android.graphics.Paint.ANTI_ALIAS_FLAG).apply { this.color = android.graphics.Color.argb((color.alpha * 255).toInt(), (color.red * 255).toInt(), (color.green * 255).toInt(), (color.blue * 255).toInt()) }
InputButtonIcons.draw(drawContext.canvas.nativeCanvas, code, size.width / 2, size.height / 2, size.minDimension, paint)
paint.color = android.graphics.Color.argb((color.alpha * 255).toInt(), (color.red * 255).toInt(), (color.green * 255).toInt(), (color.blue * 255).toInt())
InputButtonIcons.draw(drawContext.canvas.nativeCanvas, code, size.width / 2, size.height / 2, size.minDimension, paint, symbol)
}
}) { onChange(element.copy(icon = code, image = "")); host.open = null }
}
@@ -197,6 +197,7 @@ class InputSurfaceView(
private class HoldButton(context: Context, private val element: InputSurfaceElement, private val source: InputSurfaceSource?) : Surface(context) {
private var down = false
private val symbol = InputButtonIcons.drawable(context, element.icon)
private val bitmap = element.image.takeIf { it.matches(Regex("[A-Za-z0-9_.-]{1,128}")) && it != "." && it != ".." }?.let { name ->
val file = File(File(context.filesDir, "screenkeys"), name)
val size = BitmapFactory.Options().apply { inJustDecodeBounds = true }
@@ -221,7 +222,7 @@ class InputSurfaceView(
canvas.drawBitmap(it, null, imageRect, labelPaint)
return
}
if (element.icon != 0 && InputButtonIcons.draw(canvas, element.icon, cx, height / 2f, min(width, height) * .68f, labelPaint)) return
if (element.icon != 0 && InputButtonIcons.draw(canvas, element.icon, cx, height / 2f, min(width, height) * .68f, labelPaint, symbol)) return
if (!element.showLabel) return
labelPaint.textSize = min(height * .3f, 30 * density)
val labelWidth = labelPaint.measureText(element.name)
@@ -209,6 +209,7 @@ private class DrawerFocus {
class DrawerActions(
val steam: Boolean,
val desktop: Boolean,
val direct: Boolean = false,
val title: String? = null,
val isHomeApp: Boolean,
@@ -589,7 +590,7 @@ internal fun SessionDrawer(open: Boolean, requestedPage: SessionDrawerPage, cont
}
SessionDrawerPage.CONTROLLER -> {
SettingsGroup(stringResource(R.string.drawer_page_controller)) {
InputQuickSettings(host, desktop = !a.steam && !a.direct, onChanged = a.onInputSelection, onEdit = a.onInputSettings) { key -> focus.track(page, key) }
InputQuickSettings(host, desktop = a.desktop, onChanged = a.onInputSelection, onEdit = a.onInputSettings) { key -> focus.track(page, key) }
}
SettingsGroup(stringResource(R.string.input_device_settings)) {
a.padType?.let { type ->
@@ -0,0 +1,5 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp" android:height="24dp" android:viewportWidth="24" android:viewportHeight="24">
<path android:fillColor="#FFFFFFFF"
android:pathData="M6,10a2,2 0,1 0,0 4a2,2 0,1 0,0 -4M12,10a2,2 0,1 0,0 4a2,2 0,1 0,0 -4M18,10a2,2 0,1 0,0 4a2,2 0,1 0,0 -4" />
</vector>
@@ -0,0 +1,8 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp" android:height="24dp" android:viewportWidth="64" android:viewportHeight="64">
<path android:fillColor="#FFFFFFFF" android:pathData="M 30.6,23.6 34.8,33.4 44.7,34.8 29.2,44.6 V 37.6 H 22.2 Z" />
<path android:fillColor="#FFFFFFFF" android:pathData="M 40 15 A 11 11 0 0 0 29 26 A 11 11 0 0 0 40 37 A 11 11 0 0 0 51 26 A 11 11 0 0 0 40 15 z M 40 19 A 7 7 0 0 1 47 26 A 7 7 0 0 1 40 33 A 7 7 0 0 1 33 26 A 7 7 0 0 1 40 19 z" />
<path android:fillColor="#FFFFFFFF" android:pathData="M 23.5 35 A 8.5 8.5 0 0 0 15 43.5 A 8.5 8.5 0 0 0 23.5 52 A 8.5 8.5 0 0 0 32 43.5 A 8.5 8.5 0 0 0 23.5 35 z M 23.5 38 A 5.5 5.5 0 0 1 29 43.5 A 5.5 5.5 0 0 1 23.5 49 A 5.5 5.5 0 0 1 18 43.5 A 5.5 5.5 0 0 1 23.5 38 z" />
<path android:fillColor="#FFFFFFFF" android:pathData="M40,21 a5,5 0,1 0,0 10 a5,5 0,1 0,0 -10" />
<path android:fillColor="#FFFFFFFF" android:pathData="M 4.046875 31.078125 A 28 28 0 0 0 4 32 A 28 28 0 0 0 4.9375 39.123047 L 22.085938 46.701172 A 3.50035 3.50035 0 1 0 24.914062 40.298828 L 4.046875 31.078125 z" />
</vector>
@@ -0,0 +1,36 @@
package com.droiddeck.launcher.input
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import org.junit.Assert.*
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.Config
import org.robolectric.annotation.GraphicsMode
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [34])
@GraphicsMode(GraphicsMode.Mode.NATIVE)
class InputButtonIconsTest {
@Test fun commandSymbolsRenderUsingTheButtonTintAndHaveReadableNames() {
val context = RuntimeEnvironment.getApplication()
for (code in listOf(InputButtonIcons.MENU, InputButtonIcons.KEYBOARD, InputButtonIcons.STEAM, InputButtonIcons.QUICK_ACCESS)) {
val drawable = InputButtonIcons.drawable(context, code)
assertNotNull(drawable)
assertNotEquals(code.toString(), InputButtonIcons.label(context, code))
val bitmap = Bitmap.createBitmap(64, 64, Bitmap.Config.ARGB_8888)
try {
val paint = Paint(Paint.ANTI_ALIAS_FLAG).apply { color = Color.GREEN }
assertTrue(InputButtonIcons.draw(Canvas(bitmap), code, 32f, 32f, 48f, paint, drawable))
val pixels = IntArray(64 * 64)
bitmap.getPixels(pixels, 0, 64, 0, 0, 64, 64)
assertTrue(pixels.count { Color.alpha(it) > 0 } > 30)
assertTrue(pixels.filter { Color.alpha(it) > 0 }.all { Color.green(it) > 0 && Color.red(it) == 0 && Color.blue(it) == 0 })
} finally { bitmap.recycle() }
}
}
}
@@ -18,6 +18,69 @@ class InputLibraryTest {
for (name in listOf("input-profiles", "input-surfaces", "controller")) context.getSharedPreferences(name, Context.MODE_PRIVATE).edit().clear().commit()
}
@Test fun freshLibraryHasOnlyTheMainGamepadAndTwoLowerScreenLayouts() {
val library = InputLibraries.read(context)
assertEquals(listOf("game", "desktop"), library.profiles.map { it.id })
assertEquals(setOf(InputLibraries.OVERLAY, InputLibraries.DECK, InputLibraries.TRACKPAD), library.layouts.map { it.id }.toSet())
assertEquals(6, library.bindings.size)
assertEquals(InputLibraries.DECK, library.activeLayout(false, InputRole.SECONDARY)?.id)
assertEquals(InputLibraries.TRACKPAD, library.activeLayout(true, InputRole.SECONDARY)?.id)
assertEquals(InputAction.Pointer(), library.compileActive(true).action(InputRole.PHYSICAL, InputControl.LS))
assertEquals(InputAction.Native(), library.compileActive(false).action(InputRole.PHYSICAL, InputControl.LS))
val desktop = library.activeLayout(true, InputRole.SECONDARY)!!
val pad = desktop.elements.single { it.kind == InputElementKind.TRACKPAD }
assertTrue(pad.mouseGestures)
assertEquals(3, desktop.elements.size)
assertEquals(InputAction.Pointer(), library.compileActive(true).elementAction(InputRole.SECONDARY, pad.id, pad.control))
assertEquals(InputAction.MouseButton(272), library.compileActive(true).elementClickAction(InputRole.SECONDARY, pad))
val game = library.activeLayout(false, InputRole.SECONDARY)!!
assertEquals(9, game.elements.size)
assertEquals(setOf(InputControl.LEFT_PAD, InputControl.RIGHT_PAD), game.elements.filter { it.kind == InputElementKind.TRACKPAD }.map { it.control }.toSet())
for (control in listOf(InputControl.L4, InputControl.L5, InputControl.R4, InputControl.R5)) {
val element = game.elements.single { it.control == control }
assertEquals(InputAction.Native(), library.compileActive(false).elementAction(InputRole.SECONDARY, element.id, element.control))
}
for ((isDesktop, commands) in listOf(true to listOf(InputCommand.MENU, InputCommand.KEYBOARD),
false to listOf(InputCommand.STEAM, InputCommand.QUICK_ACCESS, InputCommand.MENU))) {
val elements = library.activeLayout(isDesktop, InputRole.SECONDARY)!!.elements.filter { it.id.startsWith("command:") }
assertEquals(commands.map { InputAction.Command(it) }, elements.map { library.compileActive(isDesktop).elementAction(InputRole.SECONDARY, it.id, it.control) })
assertTrue(elements.all { InputButtonIcons.has(it.icon) && it.icon > 255 })
}
assertEquals(library, InputLibraries.decode(InputLibraries.encode(library)))
}
@Test fun legacyLowerScreenBindingsKeepTheirOriginalControlsWithoutSavedGeometry() {
prefs.edit().putBoolean("migrated", true).putString("profile.game",
"""{"version":1,"bindings":[{"role":"SECONDARY","control":"a","action":{"type":"keys","codes":[57]}}]}""").commit()
val library = InputLibraries.read(context)
assertEquals(InputSurfaceLayout.defaults(), library.layout("game", InputRole.SECONDARY)?.elements)
assertEquals(InputAction.Keys(listOf(57)), library.compile("game").elementAction(InputRole.SECONDARY, "legacy:a", InputControl.A))
}
@Test fun existingLibrariesKeepTheirSavedLayoutsBindingsAndSelections() {
var original = InputLibraries.read(context).duplicateProfile("desktop", "Browser", "browser")
val layout = SavedInputLayout("builtin.left-pad", "My left pad", InputSurfaceLayout.defaults(), screen = InputLayoutScreen.SECONDARY)
original = original.withLayout(layout).withPreset(SavedInputBindings("preset.left-pad", "My left pad", false, layout.id,
controls = mapOf(InputControl.LEFT_PAD to InputAction.Pointer(700f))))
.selectProfile(true, "browser").selectPart(true, InputRole.SECONDARY, "preset.left-pad").validate()
val raw = org.json.JSONObject(InputLibraries.encode(original)).put("version", 6).toString()
prefs.edit().putString("library", raw).commit()
assertEquals(original, InputLibraries.read(context))
assertEquals(original, InputLibraries.read(context))
}
@Test fun resettingDefaultActionsKeepsTheMenuAndKeyboardCommands() {
val library = InputLibraries.read(context)
for (id in listOf("game", "desktop")) {
val reset = library.resetProfile(id).validate()
for (element in library.layout(id, InputRole.SECONDARY)!!.elements.filter { it.id.startsWith("command:") }) {
assertEquals(library.compile(id).elementAction(InputRole.SECONDARY, element.id, element.control),
reset.compile(id).elementAction(InputRole.SECONDARY, element.id, element.control))
}
}
}
@Test fun migrationPreservesExistingBindingsGeometryAndActiveSelection() {
prefs.edit().putString("selected", "desktop").putBoolean("migrated", true)
.putString("profile.desktop", """{"version":1,"bindings":[{"role":"PHYSICAL","control":"a","action":{"type":"keys","codes":[29,33]}}]}""").commit()
@@ -238,10 +301,10 @@ class InputLibraryTest {
}
@Test fun activeQuickPartsCannotBeDeletedAndInvalidCombinationsAreRejected() {
val library = InputLibraries.read(context).selectPart(false, InputRole.SECONDARY, InputStockLayouts.RIGHT)
assertThrows(IllegalArgumentException::class.java) { library.removeBindings(InputStockLayouts.RIGHT) }
assertThrows(IllegalArgumentException::class.java) { library.removeLayout("builtin.right-pad") }
assertThrows(IllegalArgumentException::class.java) { library.selectPart(false, InputRole.PHYSICAL, InputStockLayouts.RIGHT).validate() }
val library = InputLibraries.read(context).selectPart(false, InputRole.SECONDARY, InputStockLayouts.TRACKPAD)
assertThrows(IllegalArgumentException::class.java) { library.removeBindings(InputStockLayouts.TRACKPAD) }
assertThrows(IllegalArgumentException::class.java) { library.removeLayout(InputLibraries.TRACKPAD) }
assertThrows(IllegalArgumentException::class.java) { library.selectPart(false, InputRole.PHYSICAL, InputStockLayouts.TRACKPAD).validate() }
assertThrows(IllegalArgumentException::class.java) { library.selectProfile(false, "missing").validate() }
}
@@ -68,7 +68,11 @@ class InputProfilesTest {
.putString("profile.game", """{"version":999,"bindings":[]} """).commit()
val profile = InputProfiles.read(context, InputProfile.GAME)
assertEquals(InputProfile.defaults(InputProfile.GAME).bindings, profile.bindings)
assertEquals(mapOf(InputRole.PRIMARY to mapOf("builtin:keyboard" to InputAction.Command(InputCommand.KEYBOARD))), profile.elements)
assertEquals(mapOf(
InputRole.PRIMARY to mapOf("builtin:keyboard" to InputAction.Command(InputCommand.KEYBOARD)),
InputRole.SECONDARY to mapOf("command:0" to InputAction.Command(InputCommand.MENU),
"command:2" to InputAction.Command(InputCommand.STEAM), "command:3" to InputAction.Command(InputCommand.QUICK_ACCESS)),
), profile.elements)
}
@Test fun layoutsPersistIndependentOfBindingsAndStayInsideScreen() {
@@ -36,6 +36,20 @@ class InputSettingsTest {
context.getSharedPreferences("controller", Context.MODE_PRIVATE).edit().clear().commit()
}
@Test fun commandSymbolsAreSelectableForAnyCustomButton() {
val element = mutableStateOf(InputSurfaceElement(InputControl.A, .1f, .1f, .2f, .2f, name = "Custom button"))
compose.setContent { DroidDeckTheme {
androidx.compose.foundation.layout.Column { InputElementProperties(element.value) { element.value = it } }
} }
var current = context.getString(R.string.input_button_text)
for (code in listOf(InputButtonIcons.MENU, InputButtonIcons.KEYBOARD, InputButtonIcons.STEAM, InputButtonIcons.QUICK_ACCESS)) {
compose.onNodeWithText(current).performClick()
current = InputButtonIcons.label(context, code)
compose.onNodeWithText(current).performScrollTo().performClick()
compose.runOnIdle { assertEquals(code, element.value.icon) }
}
}
@Test fun steamAndDesktopQuickSettingsOpenTheSameInputLibrary() {
val mode = mutableStateOf(SessionService.MODE_STEAM)
var opened = 0
@@ -63,10 +77,10 @@ class InputSettingsTest {
compose.onNodeWithTag("setting-input-part-PHYSICAL").assertExists()
compose.onNodeWithTag("setting-input-part-PRIMARY").assertExists()
compose.onNodeWithTag("setting-input-part-SECONDARY").performClick()
compose.onNodeWithText("Left Deck trackpad").performScrollTo().performClick()
compose.onNodeWithText("Deck grips + trackpads").performScrollTo().performClick()
compose.runOnIdle {
val changed = InputLibraries.read(context)
assertEquals(InputStockLayouts.LEFT, changed.activePart(true, InputRole.SECONDARY).bindings)
assertEquals(InputStockLayouts.DECK, changed.activePart(true, InputRole.SECONDARY).bindings)
assertEquals(original.profiles, changed.profiles)
assertEquals(original.compileActive(false), changed.compileActive(false))
}
@@ -25,6 +25,7 @@ import com.droiddeck.launcher.session.SessionState
import org.junit.After
import com.droiddeck.launcher.input.InputLibraries
import com.droiddeck.launcher.input.InputRole
import com.droiddeck.launcher.input.InputElementKind
import com.droiddeck.launcher.input.InputSurfaceElement
import com.droiddeck.launcher.input.InputSurfaceStyle
import com.droiddeck.launcher.input.InputControl
@@ -70,6 +71,12 @@ class SessionSecondScreenTest {
@Test fun steamMenuKeepsTheExistingTrackpadAndTerminal() = checkModes(steam = true)
@Test fun desktopQuickMenuChangesTheRunningDesktopLayout() = checkQuickSelection(steam = false, desktop = true, direct = true)
@Test fun steamQuickMenuLeavesTheDesktopSelectionAlone() = checkQuickSelection(steam = true, desktop = false, direct = false)
@Test fun directGameQuickMenuLeavesTheDesktopSelectionAlone() = checkQuickSelection(steam = false, desktop = false, direct = true)
@Test fun closingTheDockedDrawerKeepsTheSelectedControlsPanel() {
SessionState.mode = SessionService.MODE_STEAM
var closes = 0
@@ -301,6 +308,43 @@ class SessionSecondScreenTest {
compose.onNodeWithText(context.getString(R.string.mode_mic)).assertDoesNotExist()
}
private fun checkQuickSelection(steam: Boolean, desktop: Boolean, direct: Boolean) {
SessionState.mode = if (desktop) SessionService.MODE_DESKTOP else if (steam) SessionService.MODE_STEAM else SessionService.MODE_RUN
val original = InputLibraries.update(context) { it.chooseLayout("desktop", InputRole.SECONDARY, InputLibraries.DECK) }
val activity = Robolectric.buildActivity(SessionActivity::class.java).get()
val display = activity.getSystemService(DisplayManager::class.java).getDisplay(Display.DEFAULT_DISPLAY)
val panel = SecondScreenPresentation(activity, display, {}, {}, {})
val open = androidx.compose.runtime.mutableStateOf(true)
try {
panel.show()
panel.showMode(SecondScreenMode.CUSTOM_CONTROLS)
compose.setContent {
DroidDeckTheme {
SessionDrawer(open.value, SessionDrawerPage.CONTROLLER, false, {},
actions(steam, emptyList(), desktop = desktop, direct = direct,
onInputSelection = { panel.refreshControls() }, onClose = { open.value = false }))
}
}
compose.onNodeWithTag("setting-input-profile").performScrollTo().performClick()
compose.onNodeWithText("Desktop").performScrollTo().performClick()
compose.onNodeWithTag("input-profile-expand").performScrollTo().performClick()
compose.onNodeWithTag("setting-input-part-SECONDARY").performScrollTo().performClick()
compose.onNodeWithText("Trackpad + keyboard").performScrollTo().performClick()
compose.onNode(hasContentDescription(context.getString(R.string.common_close)) and
SemanticsMatcher.expectValue(androidx.compose.ui.semantics.SemanticsProperties.Role, androidx.compose.ui.semantics.Role.Button)).performClick()
compose.runOnIdle {
val updated = InputLibraries.read(context)
assertEquals("desktop", updated.selection(desktop).profile)
assertEquals(InputLibraries.TRACKPAD, updated.activeLayout(desktop, InputRole.SECONDARY)?.id)
assertEquals(original.selection(!desktop), updated.selection(!desktop))
assertEquals(original.profiles, updated.profiles)
val controls = descendants(panel.window!!.decorView).filterIsInstance<InputSurfaceView>().single()
val pads = descendants(controls).mapNotNull { it.tag as? InputSurfaceElement }.filter { it.kind == InputElementKind.TRACKPAD }.toList()
assertEquals(1, pads.size)
}
} finally { panel.closeControls() }
}
private fun checkModes(steam: Boolean) {
SessionState.mode = if (steam) SessionService.MODE_STEAM else SessionService.MODE_DESKTOP
val selected = mutableListOf<SecondScreenMode>()
@@ -325,9 +369,11 @@ class SessionSecondScreenTest {
private fun actions(steam: Boolean, displays: List<SecondScreenDisplay>,
micAvailable: Boolean = false, micEnabled: Boolean = false, onMic: (Boolean) -> Unit = {},
desktop: Boolean = !steam, direct: Boolean = false, onInputSelection: (InputRole?) -> Unit = {},
onClose: () -> Unit = {},
select: (SecondScreenMode) -> Unit = {}) =
DrawerActions(steam = steam, isHomeApp = false, androidApps = emptyList(), hudOn = false,
DrawerActions(steam = steam, desktop = desktop, direct = direct, onInputSelection = onInputSelection,
isHomeApp = false, androidApps = emptyList(), hudOn = false,
micAvailable = micAvailable, micEnabled = micEnabled, onMicEnabled = onMic,
frameGen = FrameGen.Mode.OFF, lossless = Lossless.State.NONE, oscMode = "never",
onScreenButtonsVisible = false, suspendPolicy = "auto", backActionsInverted = false,