diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index a187dc15b0..5f010b48d3 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -195,7 +195,6 @@ - - SessionPrefs.setOscMode(this, o); oscMode = o }, - onTint = { t -> ControllerPrefs.setTint(this, t); refreshController() }, - onOpacity = { o -> ControllerPrefs.setOpacity(this, o); refreshController() }, - onSize = { v -> ControllerPrefs.setSize(this, v); refreshController() }, - onStickClick = { on -> ControllerPrefs.setStickClick(this, on); refreshController() }, - onAdaptiveSticks = { on -> ControllerPrefs.setAdaptiveSticks(this, on); refreshController() }, - onRumble = { on -> ControllerPrefs.setRumble(this, on); refreshController() }, - onDeadZone = { v -> ControllerPrefs.setDeadZone(this, v); refreshController() }, - onSteamButton = { on -> ControllerPrefs.setSteamButton(this, on); refreshController() }, - onQamButton = { on -> ControllerPrefs.setQamButton(this, on); refreshController() }, - onKeyboardButton = { on -> ControllerPrefs.setKeyboardButton(this, on); refreshController() }, - onSelectSteam = { on -> ControllerPrefs.setSelectSteam(this, on); refreshController() }, - onJoyConVr = { on -> ControllerPrefs.setJoyConVr(this, on); refreshController() }, - onEditLayout = { startActivity(Intent(this, ControllerEditorActivity::class.java)) }, - onResetLayout = { ControllerPrefs.resetAllLayouts(this); refreshController() }, onMapping = { showPerformance = false; showComponents = false; showMapping = true }, - onResetAll = { ControllerPrefs.resetAll(this); refreshController() }, ), ), page = page, @@ -2136,13 +2118,7 @@ class FrontEndActivity : ComponentActivity() { @Composable private fun MappingHost() { - val settings = controllerSettings ?: return - ControllerMappingPage( - mapping = settings.mapping, - onPick = { id, target -> ControllerPrefs.setTarget(this, id, target); refreshController() }, - onReset = { ControllerPrefs.resetMapping(this); refreshController() }, - onBack = { showMapping = false }, - ) + com.droiddeck.launcher.ui.InputSettingsPage(onBack = { showMapping = false }) } private fun openComponents(focusContent: Boolean = true, tab: String? = null, gameKey: String? = null) { @@ -2438,6 +2414,7 @@ class FrontEndActivity : ComponentActivity() { SessionPrefs.setUnfoldedControlsWithController(this, on); unfoldedControlsWithController = on }, onOsc = { o -> SessionPrefs.setOscMode(this, o); oscMode = o }, + onInputSettings = { showPerformance = false; showComponents = false; showMapping = true }, onBackHold = { on -> SessionPrefs.setBackHold(this, on); backHold = on }, onBackActionsInverted = { inverted -> SessionPrefs.setBackActionsInverted(this, inverted) diff --git a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt index 99fe2528ff..7df65cd605 100644 --- a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt @@ -76,12 +76,22 @@ import com.droiddeck.launcher.input.KeyboardHost import com.droiddeck.launcher.input.ControllerPrefs import com.droiddeck.launcher.input.SessionClipboard import com.droiddeck.launcher.input.OnScreenControls +import com.droiddeck.launcher.input.InputRouter +import com.droiddeck.launcher.input.InputOutput +import com.droiddeck.launcher.input.InputCommand +import com.droiddeck.launcher.input.InputProfiles +import com.droiddeck.launcher.input.InputProfile +import com.droiddeck.launcher.input.InputAction +import com.droiddeck.launcher.input.InputRole +import com.droiddeck.launcher.input.InputControl +import com.droiddeck.launcher.input.InputMotion +import com.droiddeck.launcher.input.HeldInput +import com.droiddeck.launcher.input.DeckControls import com.droiddeck.launcher.input.PadBridge import com.droiddeck.launcher.input.PhysicalShortcuts import com.droiddeck.launcher.input.PhysicalShortcutRouter import com.droiddeck.launcher.input.PadMouse import com.droiddeck.launcher.input.PadState -import com.droiddeck.launcher.input.ScreenKeys import com.droiddeck.launcher.input.SteamControllerBle import com.droiddeck.launcher.input.DirectTouchHold import com.droiddeck.launcher.input.GestureTouchStreamGate @@ -162,11 +172,44 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { } WaylandCompositor.nativeSetScaleMode(SCALE_FIT, alignment) } - private var unfoldedPanel: com.droiddeck.launcher.ui.DeckControlsPanel? = null + private var unfoldedPanel: com.droiddeck.launcher.ui.InputSurfaceView? = null private lateinit var sessionRoot: FrameLayout + private var inputRouter: InputRouter? = null + private var physicalInputBindings: com.droiddeck.launcher.input.PhysicalInputBindings? = null + private var inputSettingsOpen by mutableStateOf(false) + @Volatile private var inputUsesLocalActions = false + private var inputUsesPointer = false + private var inputProfile = InputProfile.defaults(InputProfile.GAME) + private val inputMotion = InputMotion { inputRouter } + private val heldKeys = HeldInput { code, down -> WaylandCompositor.nativeSendKey(code, if (down) 1 else 0) } + private val heldButtons = HeldInput(::sendSceneButton) + private val inputOutput = object : InputOutput { + override fun key(code: Int, down: Boolean) { heldKeys.set(4, code, down) } + override fun button(code: Int, down: Boolean) { heldButtons.set(4, code, down) } + override fun pointer(dx: Float, dy: Float) { + if (Looper.myLooper() == Looper.getMainLooper()) moveInputPointer(dx, dy) + else uiHandler.post { + if (resumed && !SessionState.suspended && !pipUi && !drawerOpen && !inputSettingsOpen) + moveInputPointer(dx, dy) + } + } + override fun scroll(steps: Int) { sendSceneWheel(steps) } + override fun scrollHorizontal(steps: Int) { sendSceneWheel(steps, horizontal = true) } + override fun requestMotion() { inputMotion.request() } + override fun command(command: InputCommand) { + uiHandler.post { + if (!resumed || isDestroyed || pipUi || SessionState.suspended) return@post + when (command) { + InputCommand.MENU -> { releasePads(); drawerOpen = true } + InputCommand.KEYBOARD -> if (secondScreenPresentation?.showKeyboard() != true) togglePcKeyboard() + InputCommand.STEAM -> tapSteamButton() + InputCommand.QUICK_ACCESS -> tapQam() + } + } + } + } private val guestKeysDown = mutableSetOf() private val guestButtonsDown = mutableMapOf() - private val trackpadButtonsDown = mutableSetOf() private lateinit var surfaceView: SurfaceView private lateinit var sessionOverlay: ComposeView private lateinit var loading: LoadingState @@ -177,7 +220,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { private var joyConVr: com.droiddeck.launcher.input.JoyConVr? = null private var handCamera: com.droiddeck.launcher.handtracking.HandCamera? = null private var padMotion: com.droiddeck.launcher.input.PadMotion? = null - private var resumed = false + private var resumed by mutableStateOf(false) private val deckPadListener: () -> Unit = { uiHandler.post { updatePadMotion(); openDeckRing("deck pad changed"); applyUnfoldedControls() } } private fun openDeckRing(why: String) { @@ -196,6 +239,9 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { } private var steamController: SteamControllerBle? = null private var onScreenControls: OnScreenControls? = null + private var bottomInputControls: OnScreenControls? = null + private var primaryInputLayout: com.droiddeck.launcher.input.SavedInputLayout? = null + private var secondaryInputLayout by mutableStateOf(null) private var steamTouchControls: com.droiddeck.launcher.input.SteamTouchControls? = null private var steamTouchAvailable by mutableStateOf(false) private val steamTouchRetry = Runnable { updateOnScreenControls() } @@ -230,19 +276,10 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { private var directHoldBehavior by mutableStateOf(com.droiddeck.launcher.input.TouchGestureConfig.MOUSE_BEHAVIOR_CLICK) private var relativeMouseMode by mutableIntStateOf(0) @Volatile private var appliedRelativeMouseMode: Int? = null - private var screenKeyPickerOpen by mutableStateOf(false) - private var screenKeyBind: List? = null - private var screenKeyDir: Int? = null - private var screenKeyTextFocus = false - private val appTextFocus get() = screenKeyTextFocus || wowChat.request != null + private val appTextFocus get() = wowChat.request != null private val wowChat by lazy { com.droiddeck.launcher.ui.WowChat(this, ::sendWowChat, ::cancelWowChat, ::readGameField) } private var wowChatBottom: SecondScreenPresentation? = null private var wowChatOnTop by mutableStateOf(false) - private var screenKeyEdit by mutableStateOf(null) - private var screenLayoutsOpen by mutableStateOf(false) - private var screenKeysShown by mutableStateOf(true) - private var screenLayoutNames by mutableStateOf(listOf()) - private var screenKeys: ScreenKeys? = null private var recognizedGesture by mutableStateOf(null) private var gesturePointerX = 0f private var gesturePointerY = 0f @@ -339,7 +376,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { holds = { backHold }, ) private val mouseBackInput = com.droiddeck.launcher.input.MouseBackInput( - sendRight = { down -> sendSceneButton(PointerGestures.BTN_RIGHT, down) }, + sendRight = { down -> heldButtons.set(32, PointerGestures.BTN_RIGHT, down) }, mouseAttached = ::mouseAttached, ) private var drawerDirectionKey = KeyEvent.KEYCODE_UNKNOWN @@ -598,6 +635,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { backQamInput.onDeviceRemoved(deviceId) mouseBackInput.onDeviceRemoved(deviceId) joyConVr?.refresh() + physicalInputBindings?.disconnected(deviceId) padBridge?.onDeviceRemoved(deviceId) if (::padMouse.isInitialized) padMouse.reset() refreshControls() @@ -692,9 +730,10 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { val bridge = PadBridge(File(LinuxRuntime.sessionRoot(this), "dev/input")) bridge.setStickDeadZone(ControllerPrefs.deadZone(this)) - bridge.setSelectSteam(ControllerPrefs.read(this).selectSteam) bridge.setBuiltinPad(SessionState.mode != SessionService.MODE_STEAM || SessionPrefs.builtinPad(this)) padBridge = bridge + inputRouter = InputRouter(inputOutput).also { bridge.setInputRouter(it); DeckControls.setRouter(it); physicalInputBindings = com.droiddeck.launcher.input.PhysicalInputBindings(it) } + refreshInputProfile() bridge.setOnChordInput(backQamInput::onChordInput) SessionState.padBridge = bridge if (Build.VERSION.SDK_INT >= 31) { @@ -708,20 +747,16 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { joyConVr = vr } SteamControllerBle.listener = { uiHandler.post { updatePadMotion(); refreshControls() } } - steamController = SteamControllerBle(this).also { it.start() } + steamController = SteamControllerBle(this, inputRouter).also { it.start() } padMotion = com.droiddeck.launcher.input.PadMotion(this) { @Suppress("DEPRECATION") (if (Build.VERSION.SDK_INT >= 30) display else windowManager.defaultDisplay)?.rotation ?: android.view.Surface.ROTATION_0 } SessionState.deckPadListener = deckPadListener openDeckRing("activity created") - bridge.setOnPlayerInput { - lastPadInputMs = android.os.SystemClock.uptimeMillis() - uiHandler.removeCallbacks(cursorHide) - mouseOwnsCursor = false - cursorVisible = false - } - onScreenControls = OnScreenControls(this, bridge, onKeyboard = ::togglePcKeyboard).also { controls -> + bridge.setOnPlayerInput(::onPadInput) + onScreenControls = OnScreenControls(this, bridge).also { controls -> + controls.updateLayout(primaryInputLayout) controls.onTouchMiss = miss@{ event -> if (!gestureTouchStreamGate.accepts(GestureTouchStreamGate.Source.ON_SCREEN_CONTROLS, event)) return@miss event.offsetLocation(controls.left - surfaceView.left.toFloat(), controls.top - surfaceView.top.toFloat()) @@ -729,20 +764,6 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { } root.addView(controls) } - screenKeysShown = com.droiddeck.launcher.input.ControllerPrefs.screenKeysShown(this) - screenKeys = ScreenKeys(this) { code, down -> if (CompositorHost.isStarted) sendGuestKey(code, down) }.also { keys -> - keys.onTouchMiss = miss@{ event -> - if (!gestureTouchStreamGate.accepts(GestureTouchStreamGate.Source.SCREEN_KEYS, event)) return@miss - event.offsetLocation(keys.left - surfaceView.left.toFloat(), keys.top - surfaceView.top.toFloat()) - onTouchEvent(event) - } - keys.onAdd = { screenKeyPickerOpen = true } - keys.onSelect = { codes -> screenKeyEdit = screenKeys?.stateOf(codes) } - keys.onEditingChanged = { touchEditorChanged(); if (!it) screenKeyEdit = null } - keys.steamStyle = { steamTouchControls?.style } - keys.visibility = if (screenKeysShown) View.VISIBLE else View.GONE - root.addView(keys) - } keyboard = KeyboardHost(this).also { host -> host.onImeChanged = { updateDesktopKeyboardButton() } root.addView(host) @@ -882,6 +903,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { if (pipUi) return when { ::loading.isInitialized && loading.ended -> finish() + inputSettingsOpen -> inputSettingsOpen = false drawerOpen -> drawerOpen = false achievementsOpen -> closeAchievements() pcKeyboardOpen -> pcKeyboardOpen = false @@ -906,7 +928,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { androidx.compose.runtime.DisposableEffect(Unit) { onDispose { com.droiddeck.launcher.glasses.GlassesMenuMirror.setSessionMenuVisible(this@SessionActivity, false) } } - androidx.compose.runtime.LaunchedEffect(drawerOpen, pcKeyboardOpen, achievementsOpen, pipUi, bottomOutputActive) { updatePointerCapture() } + androidx.compose.runtime.LaunchedEffect(drawerOpen, pcKeyboardOpen, achievementsOpen, inputSettingsOpen, pipUi, bottomOutputActive) { updatePointerCapture() } androidx.compose.runtime.LaunchedEffect(bottomOutputActive) { hud.setHidden(pipUi || bottomOutputActive) } androidx.compose.runtime.LaunchedEffect(pipUi) { hud.setHidden(pipUi || bottomOutputActive) @@ -1011,15 +1033,28 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { runCatching { com.droiddeck.launcher.session.TelemetryOutput.applyAutomatic(this@SessionActivity, nextGame) } } } - LaunchedEffect(drawerOpen, pcKeyboardOpen, achievementsOpen, gestureSettingsOpen, pipUi, SessionState.suspended) { - if (drawerOpen || pcKeyboardOpen || achievementsOpen || gestureSettingsOpen || pipUi || SessionState.suspended) { + LaunchedEffect(drawerOpen, pcKeyboardOpen, achievementsOpen, gestureSettingsOpen, inputSettingsOpen, pipUi, SessionState.suspended, resumed, appTextFocus) { + updateOnScreenControls() + padBridge?.setInputEnabled(resumed && !drawerOpen && !pcKeyboardOpen && !achievementsOpen && !gestureSettingsOpen && !inputSettingsOpen && !pipUi && !appTextFocus && !SessionState.suspended) + if (drawerOpen || pcKeyboardOpen || achievementsOpen || gestureSettingsOpen || inputSettingsOpen || pipUi || appTextFocus || SessionState.suspended) { backQamInput.cancel() com.droiddeck.launcher.input.HomeButtonShortcuts.cancel(this@SessionActivity) cancelTouchGestures() - screenKeys?.stopEditing() } } com.droiddeck.launcher.ui.GestureFeedback(recognizedGesture) + if (inputSettingsOpen) com.droiddeck.launcher.ui.InputSettingsDialog( + onBack = { inputSettingsOpen = false; secondScreenPresentation?.refreshControls() }, + onChanged = { + controllerSettings = ControllerPrefs.read(this@SessionActivity) + oscMode = SessionPrefs.oscMode(this@SessionActivity) + padBridge?.setStickDeadZone(ControllerPrefs.deadZone(this@SessionActivity)) + joyConVr?.refresh() + onScreenControls?.reload() + refreshInputProfile() + refreshControls() + }, + ) LaunchedEffect(Unit) { val root = LinuxRuntime.rootDir(this@SessionActivity) while (true) { @@ -1052,84 +1087,6 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { onDismiss = { gestureSettingsOpen = false }, onSaved = { gestureSettingsOpen = false; refreshGestureConfig() }, ) - if (screenKeyPickerOpen) { - val dir = screenKeyDir - com.droiddeck.launcher.ui.ScreenKeyPicker( - initial = if (dir != null) { - listOfNotNull((screenKeyEdit?.codes?.getOrNull(dir) ?: 0).takeIf { it != 0 }) - } else { - screenKeyBind ?: emptyList() - }, - onDismiss = { screenKeyPickerOpen = false; screenKeyBind = null; screenKeyDir = null }, - onAdd = { codes -> - val bind = screenKeyBind - if (dir != null && bind != null) { - screenKeys?.setDirection(bind, dir, codes.lastOrNull() ?: 0)?.let { screenKeyEdit = it } - } else if (bind != null) { - screenKeys?.setCodes(bind, codes)?.let { screenKeyEdit = it } - } else { - screenKeys?.add(codes) - screenKeyEdit = screenKeys?.stateOf(codes) - } - screenKeyPickerOpen = false - screenKeyBind = null - screenKeyDir = null - }, - ) - } - screenKeyEdit?.let { key -> - com.droiddeck.launcher.ui.ScreenKeyEditor( - codes = key.codes, keyY = key.y, - size = key.size, opacity = key.opacity, shape = key.shape, toggle = key.toggle, - labels = key.labels, - text = key.text, hasImage = key.image.isNotEmpty(), - onResize = { screenKeys?.resize(key.codes, it); screenKeyEdit = screenKeys?.stateOf(key.codes) }, - onOpacity = { screenKeys?.setOpacity(key.codes, it); screenKeyEdit = screenKeys?.stateOf(key.codes) }, - onShape = { screenKeys?.setShape(key.codes, it); screenKeyEdit = screenKeys?.stateOf(key.codes) }, - onToggle = { on -> screenKeys?.setToggle(key.codes, on); screenKeyEdit = screenKeys?.stateOf(key.codes) }, - onLabels = { on -> screenKeys?.setLabels(key.codes, on); screenKeyEdit = screenKeys?.stateOf(key.codes) }, - onDirection = { index -> screenKeyBind = key.codes; screenKeyDir = index; screenKeyPickerOpen = true }, - onText = { screenKeys?.setText(key.codes, it); screenKeyEdit = screenKeys?.stateOf(key.codes) }, - onImage = { screenKeys?.setImage(key.codes, it); screenKeyEdit = screenKeys?.stateOf(key.codes) }, - onClearImage = { screenKeys?.setImage(key.codes, ""); screenKeyEdit = screenKeys?.stateOf(key.codes) }, - onBind = { screenKeyBind = key.codes; screenKeyDir = null; screenKeyPickerOpen = true }, - onTextFocus = { screenKeyTextFocus = true; keyboard?.hide() }, - onTextBlur = { screenKeyTextFocus = false }, - onRemove = { screenKeys?.remove(key.codes); screenKeyEdit = null }, - onDismiss = { screenKeyEdit = null; screenKeyTextFocus = false; screenKeys?.deselect() }, - ) - } - if (screenLayoutsOpen) com.droiddeck.launcher.ui.ScreenLayoutPanel( - layouts = screenLayoutNames, - onSave = { name -> - screenKeys?.let { keys -> - com.droiddeck.launcher.input.ControllerPrefs.saveScreenLayout(this@SessionActivity, name, keys.export()) - } - screenLayoutNames = layoutNames() - }, - onLoad = { name -> - com.droiddeck.launcher.input.ControllerPrefs.screenLayouts(this@SessionActivity)[name]?.let { screenKeys?.load(it) } - screenLayoutsOpen = false - }, - onDelete = { name -> - com.droiddeck.launcher.input.ControllerPrefs.deleteScreenLayout(this@SessionActivity, name) - screenLayoutNames = layoutNames() - }, - onImport = { text -> - val width = (screenKeys?.width ?: 0).takeIf { it > 0 } ?: resources.displayMetrics.widthPixels - val keys = com.droiddeck.launcher.input.importScreenLayout(text, width, resources.displayMetrics.density) - if (keys != null) { - screenKeys?.load(com.droiddeck.launcher.input.ControllerPrefs.encodeScreenKeys(keys)) - screenLayoutsOpen = false - } - }, - exportText = { - val width = (screenKeys?.width ?: 0).takeIf { it > 0 } ?: resources.displayMetrics.widthPixels - val keys = com.droiddeck.launcher.input.ControllerPrefs.decodeScreenKeys(screenKeys?.export().orEmpty()) - com.droiddeck.launcher.input.exportScreenLayout(keys, width, resources.displayMetrics.density) - }, - onDismiss = { screenLayoutsOpen = false }, - ) androidx.compose.runtime.LaunchedEffect(drawerOpen) { if (drawerOpen) { syncLossless() @@ -1170,7 +1127,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { if (pcKeyboardOpen || drawerOpen || achievementsOpen) padMouse.reset() } if (pcKeyboardOpen) com.droiddeck.launcher.ui.PcKeyboard( - sendKey = { code, down -> if (CompositorHost.isStarted) sendGuestKey(code, down) }, + sendKey = { code, down -> if (CompositorHost.isStarted) sendGuestKey(code, down, 32) }, onAndroidKeyboard = { pcKeyboardOpen = false; keyboard?.toggle() }, onClose = { pcKeyboardOpen = false }, ) @@ -1228,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(), @@ -1329,9 +1287,6 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { onWowChat = wowChat::choose, controller = controllerSettings, onRumble = { on -> updateControllerPrefs { ControllerPrefs.setRumble(this@SessionActivity, on) } }, - onSteamButton = { on -> updateControllerPrefs { ControllerPrefs.setSteamButton(this@SessionActivity, on) } }, - onQamButton = { on -> updateControllerPrefs { ControllerPrefs.setQamButton(this@SessionActivity, on) } }, - onKeyboardButton = { on -> updateControllerPrefs { ControllerPrefs.setKeyboardButton(this@SessionActivity, on) } }, onSteamMenu = if (SessionState.mode == SessionService.MODE_STEAM) ({ sendSteamGuide() }) else null, onQam = if (SessionState.mode == SessionService.MODE_STEAM) ({ triggerSteamQam() }) else null, onAchievements = achievementsInFront?.let { { drawerOpen = false; achievementsOpen = true } }, @@ -1399,6 +1354,8 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { SessionService.suspendPolicyChanged(this@SessionActivity) }, onTouch = { v -> SessionPrefs.setTouchMode(this@SessionActivity, v); readPrefs() }, + onInputSettings = { releasePads(); drawerOpen = false; inputSettingsOpen = true }, + onInputSelection = { releasePads(); refreshInputProfile() }, onGestureSettings = { drawerOpen = false; gestureSettingsOpen = true }, touchActive = drawerTouchMode(), directHoldBehavior = directHoldBehavior, @@ -1414,19 +1371,6 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { relativeMouseMode = mode setRelativeMouse(mode) }, - onScreenKeys = { if (screenKeysShown) { drawerOpen = false; screenKeys?.startEditing() } }, - screenKeysShown = screenKeysShown, - onScreenKeysShown = { shown -> - screenKeysShown = shown - com.droiddeck.launcher.input.ControllerPrefs.setScreenKeysShown(this@SessionActivity, shown) - screenKeys?.stopEditing() - screenKeys?.visibility = if (shown) View.VISIBLE else View.GONE - }, - onScreenLayouts = { - drawerOpen = false - screenLayoutNames = layoutNames() - screenLayoutsOpen = true - }, onFexPreset = { v -> SessionPrefs.setFexPreset(this@SessionActivity, v); readPrefs() }, onSecondScreenMode = { mode -> if (docked) drawerOpen = false @@ -1475,7 +1419,6 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { } }, onClose = { drawerOpen = false }, - onDismissScreen = if (docked) ({ selectSecondScreenMode(SecondScreenMode.NONE) }) else null, components = drawerComponents, onComponentsRefresh = { refreshDrawerComponents() @@ -1519,10 +1462,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { private fun sendSteamGuide() { drawerOpen = false - padBridge?.applyTouch { state -> state.press(com.droiddeck.launcher.input.PadState.GUIDE, true) } - uiHandler.postDelayed({ - padBridge?.applyTouch { state -> state.press(com.droiddeck.launcher.input.PadState.GUIDE, false) } - }, 90) + padBridge?.triggerSteamMenu() } @androidx.compose.runtime.Composable @@ -1564,12 +1504,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { } private fun switchVrSticks() { - val chord = { down: Boolean -> padBridge?.applyTouch { state -> - state.press(com.droiddeck.launcher.input.PadState.L3, down) - state.press(com.droiddeck.launcher.input.PadState.R3, down) - } } - chord(true) - uiHandler.postDelayed({ chord(false) }, 120) + padBridge?.triggerVrStickSwitch() } private fun triggerSteamQam() { @@ -1602,6 +1537,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { drawerOpen = false keyboard?.hide() onScreenControls?.releaseAll() + bottomInputControls?.releaseAll() pcKeyboardOpen = !pcKeyboardOpen } @@ -1986,7 +1922,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { if (CompositorHost.isStarted) { WaylandCompositor.nativeSendTouch(3, -1, 0, 0) listOf(PointerGestures.BTN_LEFT, PointerGestures.BTN_RIGHT, PointerGestures.BTN_MIDDLE).forEach { - WaylandCompositor.nativeSendSceneInput(3, it, 0) + heldButtons.set(1, it, false) } } if (nextTouchMode == SessionPrefs.TOUCH_OFF || !usingTouchpad()) cursorVisible = false @@ -2532,6 +2468,8 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { } private fun releasePads() { + inputMotion.stop() + physicalInputBindings?.release() com.droiddeck.launcher.input.HomeButtonShortcuts.cancel(this) backQamInput.cancel() padBridge?.releaseAll() @@ -2657,7 +2595,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { SessionState.mode == SessionService.MODE_STEAM && SessionState.running && SessionState.firstFrameSeen && !SessionState.suspended && !ended && !pipUi && !drawerOpen && !pcKeyboardOpen && !achievementsOpen && - !gestureSettingsOpen && !appTextFocus && window.decorView.hasWindowFocus() + !gestureSettingsOpen && !inputSettingsOpen && !appTextFocus && window.decorView.hasWindowFocus() if (steamVolume) { val audio = getSystemService(android.media.AudioManager::class.java) if (com.droiddeck.launcher.input.SteamVolumeKeys.dispatch(event, !audio.isVolumeFixed) { direction -> @@ -2667,14 +2605,40 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { return super.dispatchKeyEvent(event) } } + if (appTextFocus) return super.dispatchKeyEvent(event) + if (!pipUi) { + if (physicalInputBindings?.onKey(event, resumed && !drawerOpen && !pcKeyboardOpen && !achievementsOpen && + !inputSettingsOpen && !gestureSettingsOpen && !SessionState.suspended && !loading.visible) == true) return true + if (physicalShortcuts.onKey( + event, + enabled = resumed && SessionState.running && !SessionState.suspended && + !(drawerOpen && event.keyCode == KeyEvent.KEYCODE_BUTTON_A) && + !(drawerOpen && PhysicalShortcuts.isDrawerNavigationKey(event)) && + (event.deviceId to event.keyCode) !in resumeKeysDown && + !(::loading.isInitialized && (loading.visible || loading.ended)) && + !pcKeyboardOpen && !achievementsOpen && !gestureSettingsOpen && !inputSettingsOpen, + steam = SessionState.mode == SessionService.MODE_STEAM, + ) { action -> + pendingBackAction?.let(uiHandler::removeCallbacks) + pendingBackAction = null + drawerControllerActive = true + Log.i(TAG, "physical shortcut ${action.name} device=${event.deviceId} key=${event.keyCode} scan=${event.scanCode}") + when (action) { + PhysicalShortcuts.Action.STEAM -> sendSteamGuide() + PhysicalShortcuts.Action.QAM -> triggerSteamQam() + PhysicalShortcuts.Action.DROIDDECK -> drawerOpen = !drawerOpen + } + }) return true + } if (event.keyCode == KeyEvent.KEYCODE_BACK && event.action == KeyEvent.ACTION_DOWN && event.repeatCount == 0) { Log.i(TAG, String.format(java.util.Locale.ROOT, "back key device=%d (%s) source=0x%x flags=0x%x", event.deviceId, event.device?.name, event.source, event.flags)) } - val mouseToGuest = !pipUi && !appTextFocus && !drawerOpen && !pcKeyboardOpen && !achievementsOpen && !gestureSettingsOpen && + val mouseToGuest = !pipUi && !appTextFocus && !drawerOpen && !pcKeyboardOpen && !achievementsOpen && !gestureSettingsOpen && !inputSettingsOpen && !SessionState.suspended && !(::loading.isInitialized && (loading.visible || loading.ended)) if (mouseBackInput.onKeyEvent(event, mouseToGuest)) return true val backToQam = !pipUi && !appTextFocus && !drawerOpen && !pcKeyboardOpen && !achievementsOpen && + !inputSettingsOpen && !gestureSettingsOpen && SessionState.mode == SessionService.MODE_STEAM && SessionState.running && SessionState.firstFrameSeen && !SessionState.suspended && padBridge?.canHoldQam() == true if (backQamInput.onKeyEvent(event, backToQam)) { @@ -2683,30 +2647,13 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { return true } if (pipUi) return true - if (appTextFocus) return super.dispatchKeyEvent(event) - if (physicalShortcuts.onKey( - event, - enabled = SessionState.running && !SessionState.suspended && - !(drawerOpen && event.keyCode == KeyEvent.KEYCODE_BUTTON_A) && - !(drawerOpen && PhysicalShortcuts.isDrawerNavigationKey(event)) && - (event.deviceId to event.keyCode) !in resumeKeysDown && - !(::loading.isInitialized && (loading.visible || loading.ended)) && - !pcKeyboardOpen && !achievementsOpen && !gestureSettingsOpen, - steam = SessionState.mode == SessionService.MODE_STEAM, - ) { action -> - pendingBackAction?.let(uiHandler::removeCallbacks) - pendingBackAction = null - drawerControllerActive = true - Log.i(TAG, "physical shortcut ${action.name} device=${event.deviceId} key=${event.keyCode} scan=${event.scanCode}") - when (action) { - PhysicalShortcuts.Action.STEAM -> sendSteamGuide() - PhysicalShortcuts.Action.QAM -> triggerSteamQam() - PhysicalShortcuts.Action.DROIDDECK -> drawerOpen = !drawerOpen - } - }) return true val pad = com.droiddeck.launcher.input.JoyCon.normalize(event) if (pad !== event) return dispatchKeyEvent(pad) val fromController = PadBridge.isControllerKeyEvent(event) + if (fromController && (drawerOpen || pcKeyboardOpen || achievementsOpen || inputSettingsOpen || gestureSettingsOpen || SessionState.suspended)) { + padBridge?.setInputEnabled(false) + padBridge?.onKeyEvent(event) + } if (fromController && event.action == KeyEvent.ACTION_DOWN) { if (drawerOpen && !drawerControllerActive) focusDrawer() drawerControllerActive = true @@ -2772,7 +2719,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { } return true } - if ((pcKeyboardOpen || achievementsOpen) && fromController) return super.dispatchKeyEvent(event) + if ((pcKeyboardOpen || achievementsOpen || inputSettingsOpen) && fromController) return super.dispatchKeyEvent(event) if (!fromController && event.keyCode != KeyEvent.KEYCODE_BACK && sendToTopDesktop(event)) return true if (joyConVr?.onKeyEvent(event) == true) return true if (fromController && event.keyCode != KeyEvent.KEYCODE_BACK && padMouseMode() && padMouse.onKey(event)) return true @@ -2782,7 +2729,8 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { if (CompositorHost.isStarted && keyboardKey && !fromController) { val down = event.action == KeyEvent.ACTION_DOWN if (down && event.repeatCount > 0) return true - if (com.droiddeck.launcher.input.GuestKeys.send(event, ::sendGuestKey)) return true + val owner = if (event.deviceId <= 0) 16 else 8 + if (com.droiddeck.launcher.input.GuestKeys.send(event) { code, pressed -> sendGuestKey(code, pressed, owner) }) return true } return super.dispatchKeyEvent(event) } @@ -2808,6 +2756,10 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { override fun dispatchGenericMotionEvent(event: MotionEvent): Boolean { if (pipUi) return true notePress(event) + if (PadBridge.isControllerMotionEvent(event) && (drawerOpen || pcKeyboardOpen || achievementsOpen || inputSettingsOpen || gestureSettingsOpen || SessionState.suspended)) { + padBridge?.setInputEnabled(false) + padBridge?.onMotionEvent(event) + } if (drawerOpen && PadBridge.isControllerMotionEvent(event)) { if (!drawerControllerActive) focusDrawer() drawerControllerActive = true @@ -2819,7 +2771,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { return true } if (drawerDirectionKey != KeyEvent.KEYCODE_UNKNOWN) releaseDrawerDirection() - if ((pcKeyboardOpen || achievementsOpen) && PadBridge.isControllerMotionEvent(event)) return super.dispatchGenericMotionEvent(event) + if ((pcKeyboardOpen || achievementsOpen || inputSettingsOpen) && PadBridge.isControllerMotionEvent(event)) return super.dispatchGenericMotionEvent(event) if (joyConVr?.onMotionEvent(event) == true) return true if (PadBridge.isControllerMotionEvent(event) && padMouseMode() && padMouse.onMotion(event)) return true if (padBridge?.onMotionEvent(event) == true) return true @@ -2849,7 +2801,6 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { } return onScreenControls?.let { takes(it, it::takesTouchAt) } == true || steamTouchControls?.let { takes(it, it::takesTouchAt) } == true || - screenKeys?.let { takes(it, it::takesTouchAt) } == true || unfoldedPanel?.let { takes(it, it::takesTouchAt) } == true } @@ -2977,8 +2928,6 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { if (secondScreenMode == SecondScreenMode.NONE) closeSecondScreen(reset = false) else updateKeyboardRoute() } - private fun layoutNames(): List = - com.droiddeck.launcher.input.ControllerPrefs.screenLayouts(this).keys.sorted() private val relativeMouseRequests = java.util.concurrent.Executors.newSingleThreadExecutor { runnable -> Thread(runnable, "relative-mouse").apply { isDaemon = true } @@ -3048,7 +2997,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { } else moveTo(gesturePointerX + dx, gesturePointerY + dy) } override fun button(code: Int, down: Boolean) { - sendSceneButton(code, down) + heldButtons.set(16, code, down) } override fun prepareRelativePan() { if (pointerLocked) return @@ -3057,7 +3006,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { } override fun key(code: Int, down: Boolean) { if (down) gestureKeysDown.add(code) else gestureKeysDown.remove(code) - if (code !in guestKeysDown) WaylandCompositor.nativeSendKey(code, if (down) 1 else 0) + heldKeys.set(2, code, down) } override fun wheel(steps: Int) { sendSceneWheel(steps) } override fun showKeyboard() { uiHandler.post { touchscreenGestures.cancel(); togglePcKeyboard() } } @@ -3074,7 +3023,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { } override fun onButton(button: Int, pressed: Boolean, x: Float, y: Float) { if (!pointerLocked) movePointer(x, y) - sendSceneButton(button, pressed) + heldButtons.set(1, button, pressed) } override fun onWheel(steps: Int) { sendSceneWheel(steps) } override fun onScroll(dx: Float, dy: Float) = fingerScroll(dx, dy) @@ -3084,9 +3033,9 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { return true } override fun onZoom(steps: Int) { - WaylandCompositor.nativeSendKey(KEY_LEFTCTRL, 1) + heldKeys.set(128, KEY_LEFTCTRL, true) sendSceneWheel(steps) - WaylandCompositor.nativeSendKey(KEY_LEFTCTRL, 0) + heldKeys.set(128, KEY_LEFTCTRL, false) } override fun onLongPress() { @Suppress("DEPRECATION") @@ -3103,7 +3052,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { cursorVisible = false } override fun onButton(button: Int, pressed: Boolean) { - WaylandCompositor.nativeSendSceneInput(3, button, if (pressed) 1 else 0) + heldButtons.set(1, button, pressed) } override fun onScroll(dx: Float, dy: Float) { val rect = pointerSurfaceRect() ?: return @@ -3143,6 +3092,11 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { private fun padMouseMode(): Boolean { val now = android.os.SystemClock.uptimeMillis() + if (inputUsesLocalActions) { + if (padMouseOn) padMouse.reset() + padMouseOn = false + return false + } if (now - padMouseCheckedAt < PAD_MOUSE_CHECK_MS) return padMouseOn padMouseCheckedAt = now val on = !pointerLocked && EpicClient.launcherFocused(this) && !EpicClient.playing(this) @@ -3198,6 +3152,30 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { showCursor(cx, cy, fromMouse) } + private fun moveInputPointer(dx: Float, dy: Float) { + if (pointerLocked) { + sendSceneDelta(dx, dy) + cursorVisible = false + return + } + val rect = drawnRect() ?: return + val position = cursorPos + val x = if (position.x >= 0f) position.x.coerceIn(rect.left, rect.right) else rect.centerX() + val y = if (position.y >= 0f) position.y.coerceIn(rect.top, rect.bottom) else rect.centerY() + val out = SessionState.outputSize + movePointer(x + dx * rect.width() / out.first, y + dy * rect.height() / out.second) + // Make frame-callback updates visible to drawing before this frame's traversal. + androidx.compose.runtime.snapshots.Snapshot.sendApplyNotifications() + } + + private fun onPadInput() { + if (inputUsesPointer && !pointerLocked) return + lastPadInputMs = android.os.SystemClock.uptimeMillis() + uiHandler.removeCallbacks(cursorHide) + mouseOwnsCursor = false + cursorVisible = false + } + private fun moveCapturedMouse(dx: Float, dy: Float) { val rect = drawnRect() ?: return val scale = sceneScale(rect) @@ -3217,8 +3195,8 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { cursorResyncIdle = 0 if (fromMouse || !touchDrivesPointer()) uiHandler.postDelayed(cursorResync, 60) if (clientHidesCursor) { cursorVisible = false; return } - if (!fromMouse && directTouchCursorHidden()) { cursorVisible = false; return } - if (android.os.SystemClock.uptimeMillis() - lastPadInputMs < CURSOR_PAD_HOLD_MS) return + if (!inputUsesPointer && !fromMouse && directTouchCursorHidden()) { cursorVisible = false; return } + if (!inputUsesPointer && android.os.SystemClock.uptimeMillis() - lastPadInputMs < CURSOR_PAD_HOLD_MS) return cursorVisible = true uiHandler.removeCallbacks(cursorHide) if (!fromMouse && SessionState.mode == SessionService.MODE_STEAM && SessionState.steamUi != "desktop") uiHandler.postDelayed(cursorHide, 2500) @@ -3335,8 +3313,29 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { showSecondScreen(mode) } - private fun showSecondScreen(mode: SecondScreenMode) { + private fun showSecondScreen(requested: SecondScreenMode) { if (pipUi || !started) return + val mode = if (requested == SecondScreenMode.DECK_CONTROLS || requested == SecondScreenMode.KEYBOARD_TRACKPAD) { + val remembered = SessionPrefs.secondScreenPanel(this) + if (remembered == requested.id) { + val desktop = SessionState.mode == SessionService.MODE_DESKTOP + val key = if (desktop) InputProfile.DESKTOP else InputProfile.GAME + val preset = if (requested == SecondScreenMode.DECK_CONTROLS) com.droiddeck.launcher.input.InputStockLayouts.DECK else com.droiddeck.launcher.input.InputStockLayouts.TRACKPAD + runCatching { + com.droiddeck.launcher.input.InputLibraries.update(this) { library -> + val selection = library.selections[key] + if (selection?.profileSelected == true || selection?.parts?.containsKey(InputRole.SECONDARY) == true) library + else library.selectPart(desktop, InputRole.SECONDARY, preset) + } + }.onSuccess { + refreshInputProfile() + SessionPrefs.setSecondScreenPanel(this, SecondScreenMode.CUSTOM_CONTROLS.id) + }.onFailure { + Log.w(TAG, "could not migrate the saved second-screen selection", it) + } + } + SecondScreenMode.CUSTOM_CONTROLS + } else requested val target = displayManager.getDisplay(selectedSecondScreenDisplay) if (target == null || !target.isValid || (target.flags and Display.FLAG_PRESENTATION) == 0) { closeSecondScreen(reset = false) @@ -3370,9 +3369,6 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { private fun secondScreenPresentation(target: Display) = SecondScreenPresentation( this, target, - sendPointer = ::moveSecondScreenPointer, - sendButton = ::sendTrackpadButton, - sendWheel = { steps -> WaylandCompositor.nativeSendSceneInput(4, steps, 0) }, onSteamMenu = ::tapSteamButton, onQam = ::tapQam, onClose = ::closeBottomPanel, @@ -3486,12 +3482,39 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { } private fun tapSteamButton() { - if (SessionState.mode == SessionService.MODE_STEAM) { - padBridge?.applyTouch { st -> st.press(com.droiddeck.launcher.input.PadState.GUIDE, true) } - Handler(Looper.getMainLooper()).postDelayed({ - padBridge?.applyTouch { st -> st.press(com.droiddeck.launcher.input.PadState.GUIDE, false) } - }, 90) - } + if (SessionState.mode == SessionService.MODE_STEAM) padBridge?.triggerSteamMenu() + } + + fun inputEditorDisplay(role: InputRole): Display? { + val main = window.decorView.display + if (role == InputRole.PRIMARY) return bottomGame?.display ?: main + if (bottomGame != null) return main + return displayManager.getDisplay(selectedSecondScreenDisplay) + ?: secondScreenDisplays.firstOrNull()?.let { displayManager.getDisplay(it.id) } + } + + private fun refreshInputProfile() { + val library = com.droiddeck.launcher.input.InputLibraries.read(this) + val desktop = loadingMode() == SessionService.MODE_DESKTOP + inputProfile = library.compileActive(desktop) + primaryInputLayout = library.activeLayout(desktop, InputRole.PRIMARY) + secondaryInputLayout = library.activeLayout(desktop, InputRole.SECONDARY) + inputUsesLocalActions = InputControl.padControls.any { inputProfile.action(InputRole.PHYSICAL, it) !is InputAction.Native } + inputUsesPointer = inputProfile.bindings.values.any { bindings -> + bindings.values.any { it is InputAction.Pointer || it is InputAction.MouseButton || it is InputAction.Scroll } + } || inputProfile.elements.values.any { bindings -> + bindings.values.any { it is InputAction.Pointer || it is InputAction.MouseButton || it is InputAction.Scroll } + } || inputProfile.clicks.values.any { bindings -> bindings.values.any { it is InputAction.MouseButton } } + || inputProfile.keys.any { it.action is InputAction.MouseButton } + physicalInputBindings?.configure(inputProfile.keys) + padBridge?.setInputProfile(inputProfile) + DeckControls.refresh() + onScreenControls?.updateLayout(primaryInputLayout) + bottomInputControls?.updateLayout(primaryInputLayout) + unfoldedPanel?.updateCustomLayout(primaryInputLayout?.elements.orEmpty()) + unfoldedPanel?.updateStyle(primaryInputLayout?.style ?: com.droiddeck.launcher.input.InputSurfaceStyle()) + updateOnScreenPad() + secondScreenPresentation?.refreshControls() } private fun tapQam() { @@ -3513,7 +3536,6 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { private fun refreshControls() { applyUnfoldedControls() updateOnScreenControls() - screenKeys?.refreshAppearance() } private fun controlsBelowWanted(): Boolean { @@ -3536,19 +3558,13 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { else "unfolded controls: game full screen") } var panel = unfoldedPanel - if (panel != null && panel.deckControls != SessionState.deckPad) { - sessionRoot.removeView(panel) - unfoldedPanel = null - panel = null - } if (!wanted) { if (panel != null) { sessionRoot.removeView(panel); unfoldedPanel = null; updateOnScreenControls() } return } if (panel == null) { - panel = com.droiddeck.launcher.ui.DeckControlsPanel(this, onClose = { chooseUnfoldedControls(false) }, - onSteamMenu = ::tapSteamButton, onQam = ::tapQam, pad = padBridge, - portraitDock = !com.droiddeck.launcher.session.SessionDisplay.foldable(this), onMenu = { drawerOpen = true }) + panel = com.droiddeck.launcher.ui.InputSurfaceView(this, padBridge, primaryInputLayout?.elements.orEmpty(), InputRole.PRIMARY, + primaryInputLayout?.style ?: com.droiddeck.launcher.input.InputSurfaceStyle()) sessionRoot.addView(panel, sessionRoot.indexOfChild(surfaceView) + 1) unfoldedPanel = panel updateOnScreenControls() @@ -3556,17 +3572,6 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { panel.layoutParams = FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, height - gameHeight, Gravity.BOTTOM) } - 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) - val rect = pointerSurfaceRect() ?: return - val px = rect.left + nx * rect.width() - val py = rect.top + ny * rect.height() - WaylandCompositor.nativeSendPointer(1, InputSpace.x(px, outputView().width), InputSpace.y(py, outputView().height)) - showCursor(px, py) - } - private fun bottomKeyboardPresentation(): SecondScreenPresentation? { if (!started) return null if (secondScreenPresentation?.showsKeyboard != true && dockDrawer()) { @@ -3574,7 +3579,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { if (drawerOpen || drawerPinned) drawerOpen = false typingReturnMode = secondScreenMode selectedSecondScreenDisplay = target.displayId - selectSecondScreenMode(SecondScreenMode.KEYBOARD_TRACKPAD) + selectSecondScreenMode(SecondScreenMode.CUSTOM_CONTROLS) } return secondScreenPresentation?.takeIf { it.showsKeyboard && it.display != null } } @@ -3645,13 +3650,13 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { } private fun selectGameField(vararg then: Int) { - val held = KEY_LEFTCTRL in guestKeysDown || KEY_LEFTCTRL in gestureKeysDown - if (!held) WaylandCompositor.nativeSendKey(KEY_LEFTCTRL, 1) - for (key in intArrayOf(KEY_A) + then) { - WaylandCompositor.nativeSendKey(key, 1) - WaylandCompositor.nativeSendKey(key, 0) - } - if (!held) WaylandCompositor.nativeSendKey(KEY_LEFTCTRL, 0) + heldKeys.set(256, KEY_LEFTCTRL, true) + try { + for (key in intArrayOf(KEY_A) + then) { + heldKeys.set(256, key, true) + heldKeys.set(256, key, false) + } + } finally { heldKeys.release(256) } } private fun readGameField(done: (String?) -> Unit) { @@ -3672,7 +3677,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { com.droiddeck.launcher.ui.SessionTyping.returnFocus(this, taskId) val mode = typingReturnMode ?: return typingReturnMode = null - uiHandler.post { if (secondScreenMode == SecondScreenMode.KEYBOARD_TRACKPAD) selectSecondScreenMode(mode) } + uiHandler.post { if (secondScreenMode == SecondScreenMode.CUSTOM_CONTROLS) selectSecondScreenMode(mode) } } private fun updateKeyboardRoute() { @@ -3756,40 +3761,44 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { private fun sceneScale(rect: android.graphics.RectF): Float = SessionState.outputSize.first / rect.width() - private fun openEvdevMouse() { + // Clearing button owners emits releases, so backend transitions share their lock with all mouse writes. + private fun openEvdevMouse(): Unit = synchronized(heldButtons) { if (evdevMouseActive || !evdevMouseOn) return val writer = mouseWriter ?: com.droiddeck.launcher.input.FakeInputWriter( File(LinuxRuntime.sessionRoot(this), "dev/input").absolutePath, com.droiddeck.launcher.input.FakeInputWriter.MOUSE_SLOT, ).also { mouseWriter = it } if (!writer.open()) return + heldButtons.clear() evdevMouseActive = true lastMouseX = Float.NaN lastMouseY = Float.NaN Log.i(TAG, "raw mouse channel active (pointer locked)") } - private fun closeEvdevMouse() { + private fun closeEvdevMouse(): Unit = synchronized(heldButtons) { if (!evdevMouseActive) return + heldButtons.clear() mouseWriter?.softRelease() evdevMouseActive = false Log.i(TAG, "raw mouse channel idle") } - private fun releaseEvdevMouse() { + private fun releaseEvdevMouse(): Unit = synchronized(heldButtons) { + if (evdevMouseActive) heldButtons.clear() evdevMouseActive = false mouseWriter?.destroy() mouseWriter = null Log.i(TAG, "raw mouse channel removed") } - private fun sendSceneDelta(dx: Float, dy: Float) { + private fun sendSceneDelta(dx: Float, dy: Float): Unit = synchronized(heldButtons) { val writer = if (evdevMouseActive) mouseWriter else null if (writer != null) writer.writeMouseDelta(dx, dy) else WaylandCompositor.sendPointerDelta(dx, dy) } - private fun sendSceneButton(code: Int, down: Boolean) { + private fun sendSceneButton(code: Int, down: Boolean): Unit = synchronized(heldButtons) { val raw = if (down) evdevMouseActive.also { guestButtonsDown[code] = it } else guestButtonsDown.remove(code) ?: evdevMouseActive val writer = if (raw) mouseWriter else null @@ -3797,14 +3806,10 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { else WaylandCompositor.nativeSendSceneInput(3, code, if (down) 1 else 0) } - private fun sendTrackpadButton(button: Int, pressed: Boolean) { - if (pressed) trackpadButtonsDown.add(button) else trackpadButtonsDown.remove(button) - WaylandCompositor.nativeSendSceneInput(3, button, if (pressed) 1 else 0) - } - - private fun sendSceneWheel(steps: Int) { + private fun sendSceneWheel(steps: Int, horizontal: Boolean = false): Unit = synchronized(heldButtons) { val writer = if (evdevMouseActive) mouseWriter else null - if (writer != null) writer.writeMouseWheel(-steps, 0, 120) + if (writer != null) writer.writeMouseWheel(if (horizontal) 0 else -steps, if (horizontal) steps else 0, 120) + else if (horizontal) WaylandCompositor.nativeSendSceneInput(11, 0, steps * 120) else WaylandCompositor.nativeSendSceneInput(4, steps, 0) } @@ -3866,7 +3871,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { if (event.actionMasked == MotionEvent.ACTION_UP || event.actionMasked == MotionEvent.ACTION_CANCEL) dismissTouch = false return true } - val uiBlocking = drawerOpen || pcKeyboardOpen || achievementsOpen || gestureSettingsOpen || pipUi || SessionState.suspended + val uiBlocking = drawerOpen || pcKeyboardOpen || achievementsOpen || gestureSettingsOpen || inputSettingsOpen || pipUi || SessionState.suspended if (event.actionMasked == MotionEvent.ACTION_DOWN && !uiBlocking && routedTouchMode != SessionPrefs.TOUCH_GESTURES) { scrollTouchGesture = scrollTouchMode() scrollRestX = 0f @@ -3981,7 +3986,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { if (!pointerLocked) showCursor(event.x.coerceIn(rect.left, rect.right), event.y.coerceIn(rect.top, rect.bottom)) if (WaylandCompositor.nativeFocusTakesTouch()) { listOf(PointerGestures.BTN_LEFT, PointerGestures.BTN_RIGHT, PointerGestures.BTN_MIDDLE).forEach { - WaylandCompositor.nativeSendSceneInput(3, it, 0) + heldButtons.set(1, it, false) } } } @@ -3995,7 +4000,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { if (evdevMouseActive) sendMouseDelta(event.x, event.y) else movePointer(event.x, event.y, fromMouse = true) MotionEvent.ACTION_BUTTON_PRESS, MotionEvent.ACTION_BUTTON_RELEASE -> { if (evdevMouseActive) sendMouseDelta(event.x, event.y) else movePointer(event.x, event.y, fromMouse = true) - guestMouseButton(event.actionButton)?.let { sendSceneButton(it, event.actionMasked == MotionEvent.ACTION_BUTTON_PRESS) } + guestMouseButton(event.actionButton)?.let { heldButtons.set(8, it, event.actionMasked == MotionEvent.ACTION_BUTTON_PRESS) } } MotionEvent.ACTION_SCROLL -> { val v = event.getAxisValue(MotionEvent.AXIS_VSCROLL) @@ -4031,7 +4036,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { private fun updatePointerCapture() { if (!::surfaceView.isInitialized) return val view = window.decorView - val want = (pointerLocked || relativeMouseAttached()) && resumed && hasWindowFocus() && !drawerOpen && !pcKeyboardOpen && !achievementsOpen && !pipUi && !bottomOutputActive && !touchEditorOpen && mouseAttached() + val want = (pointerLocked || relativeMouseAttached()) && resumed && hasWindowFocus() && !drawerOpen && !pcKeyboardOpen && !achievementsOpen && !inputSettingsOpen && !pipUi && !bottomOutputActive && !touchEditorOpen && mouseAttached() releaseDesktopMouseGrab() if (want == view.hasPointerCapture()) return if (want) view.requestPointerCapture() else view.releasePointerCapture() @@ -4084,13 +4089,13 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { private var platformPointerShown: Boolean? = null private fun touchEditorChanged() { - touchEditorOpen = screenKeys?.isEditing == true || steamTouchControls?.isEditing == true + touchEditorOpen = steamTouchControls?.isEditing == true updatePointerCapture() applyPlatformPointer(appUiOwnsPointer()) } private fun appUiOwnsPointer(): Boolean = - drawerOpen || pcKeyboardOpen || achievementsOpen || pipUi || glassesOutputActive || bottomOutputActive || touchEditorOpen || wowChatOnTop + drawerOpen || pcKeyboardOpen || achievementsOpen || inputSettingsOpen || pipUi || glassesOutputActive || bottomOutputActive || touchEditorOpen || wowChatOnTop private fun applyPlatformPointer(shown: Boolean) { if (!::sessionRoot.isInitialized || !::surfaceView.isInitialized) return @@ -4120,7 +4125,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { } } MotionEvent.ACTION_BUTTON_PRESS, MotionEvent.ACTION_BUTTON_RELEASE -> - guestMouseButton(event.actionButton)?.let { sendSceneButton(it, event.actionMasked == MotionEvent.ACTION_BUTTON_PRESS) } + guestMouseButton(event.actionButton)?.let { heldButtons.set(8, it, event.actionMasked == MotionEvent.ACTION_BUTTON_PRESS) } MotionEvent.ACTION_SCROLL -> { val v = event.getAxisValue(MotionEvent.AXIS_VSCROLL) if (v != 0f) sendSceneWheel(-Math.round(v)) @@ -4164,7 +4169,6 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { device.plugged = false uiHandler.postDelayed({ updateOnScreenControls() }, 700) } - view.onStyle = { screenKeys?.refreshAppearance() } view.onTouchMiss = miss@{ event -> if (!gestureTouchStreamGate.accepts(GestureTouchStreamGate.Source.ON_SCREEN_CONTROLS, event)) return@miss event.offsetLocation(view.left - surfaceView.left.toFloat(), view.top - surfaceView.top.toFloat()) @@ -4188,19 +4192,20 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { } private fun updateOnScreenPad() { + fun hideBottomControls() { bottomInputControls?.takeIf { it.visibility == View.VISIBLE }?.let { it.releaseAll(); it.visibility = View.GONE } } val forced = File(Environment.getExternalStorageDirectory(), "Download/droiddeck-osc") .takeIf { it.isFile } ?.let { FileUtils.readString(it)?.trim()?.lowercase() } ?: SessionPrefs.oscMode(this) val controls = onScreenControls ?: return - if (pipUi || unfoldedPanel != null || pcKeyboardOpen || + if (pipUi || unfoldedPanel != null || pcKeyboardOpen || inputSettingsOpen || (SessionState.mode == SessionService.MODE_STEAM && !SessionPrefs.builtinPad(this))) { onScreenButtonsVisible = false + hideBottomControls() controls.releaseAll() controls.visibility = View.GONE steamTouchControls?.let { it.cancelEditing(); it.releaseAll(); it.visibility = View.GONE } com.droiddeck.launcher.input.SteamTouchDevice.current?.plugged = false - screenKeys?.followSteam = false applyTouchRouting() return } @@ -4229,7 +4234,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { onScreenButtonsVisible = show val drawn = show && !keyboardCoversPad() if (steam != null) { - screenKeys?.followSteam = drawn + hideBottomControls() if (controls.visibility == View.VISIBLE) { controls.releaseAll(); controls.visibility = View.GONE } if (!drawn) { steam.cancelEditing(); steam.releaseAll() } steam.visibility = if (drawn) View.VISIBLE else View.GONE @@ -4238,7 +4243,24 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { Log.i(TAG, "on-screen controls: Steam's touch controller " + if (drawn) "shown" else "hidden") return } - screenKeys?.followSteam = false + val bottomRoot = bottomGame?.surfaceView?.parent as? android.view.ViewGroup + if (drawn && bottomRoot != null) { + controls.releaseAll(); controls.visibility = View.GONE + var bottom = bottomInputControls + if (bottom?.parent !== bottomRoot) { + (bottom?.parent as? android.view.ViewGroup)?.removeView(bottom) + bottom = OnScreenControls(bottomRoot.context, padBridge).also { view -> + view.onTouchMiss = { event -> onBottomGameTouch(event) } + bottomRoot.addView(view, android.view.ViewGroup.LayoutParams(-1, -1)) + } + bottomInputControls = bottom + } + bottom.updateLayout(primaryInputLayout) + bottom.setButtonsOnly(buttonsOnly) + bottom.visibility = View.VISIBLE + return + } + hideBottomControls() if (drawn == (controls.visibility == View.VISIBLE)) return if (!drawn) controls.releaseAll() controls.visibility = if (drawn) View.VISIBLE else View.GONE @@ -4307,17 +4329,17 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { SessionState.mode == SessionService.MODE_STEAM && AgentGuest.steamKeyboard private fun releaseHeldGuestInput() { - guestButtonsDown.keys.toList().forEach { sendSceneButton(it, false) } - trackpadButtonsDown.toList().forEach { WaylandCompositor.nativeSendSceneInput(3, it, 0) } - trackpadButtonsDown.clear() + mouseBackInput.cancel() + heldButtons.release(1 or 2 or 8 or 16) guestKeysDown.toList().forEach { sendGuestKey(it, false) } + heldKeys.release(8 or 16 or 32) } - private fun sendGuestKey(key: Int, down: Boolean) { + private fun sendGuestKey(key: Int, down: Boolean, owner: Int = 1) { if (down && pipUi) return val code = if (key == KEY_RIGHTCTRL && SessionState.mode == SessionService.MODE_DESKTOP) KEY_LEFTCTRL else key - if (down) guestKeysDown.add(code) else guestKeysDown.remove(code) - if (down || code !in gestureKeysDown) WaylandCompositor.nativeSendKey(code, if (down) 1 else 0) + if (owner == 1) { if (down) guestKeysDown.add(code) else guestKeysDown.remove(code) } + heldKeys.set(owner, code, down) } private fun capturePausedFrame(manual: Boolean = false) { @@ -4393,13 +4415,15 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { pendingBackAction = null releaseDrawerDirection() onScreenControls?.releaseAll() - screenKeys?.releaseAll() + bottomInputControls?.releaseAll() releasePads() touchpad.cancel() scrollTouch.cancel() directTouchHold.cancel() twoFingerGestures.cancel() - releaseHeldGuestInput() + guestKeysDown.toList().forEach { sendGuestKey(it, false) } + heldKeys.clear() + heldButtons.clear() keyboard?.hide() sessionClipboard.stop() padMotion?.stop() @@ -4410,7 +4434,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { if (CompositorHost.isStarted) { WaylandCompositor.nativeSendTouch(3, -1, 0, 0) listOf(PointerGestures.BTN_LEFT, PointerGestures.BTN_RIGHT, PointerGestures.BTN_MIDDLE).forEach { - WaylandCompositor.nativeSendSceneInput(3, it, 0) + heldButtons.set(1, it, false) } WaylandCompositor.nativeSetScaleMode(SCALE_FIT, ALIGN_CENTER) } @@ -4516,7 +4540,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { override fun onResume() { super.onResume() com.droiddeck.launcher.input.SystemKeyCapture.attach(this, { hasWindowFocus() && !appUiOwnsPointer() && CompositorHost.isStarted }) { event -> - com.droiddeck.launcher.input.GuestKeys.send(event, ::sendGuestKey) + com.droiddeck.launcher.input.GuestKeys.send(event) { code, down -> sendGuestKey(code, down, 8) } } if (refreshActivityDisplayId() && started) { SessionService.setActivityDisplay(this, sessionActivityDisplayId) @@ -4544,7 +4568,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { com.droiddeck.launcher.input.HomeButtonShortcuts.attach(this, enabled = { resumed && window.decorView.hasWindowFocus() && !ended && !drawerOpen && !pcKeyboardOpen && - !achievementsOpen && !gestureSettingsOpen && !pipUi && SessionState.running && !SessionState.suspended && + !achievementsOpen && !gestureSettingsOpen && !inputSettingsOpen && !pipUi && SessionState.running && !SessionState.suspended && SessionState.mode == SessionService.MODE_STEAM && padBridge != null }, setPressed = { padBridge?.setHomeGuidePressed(it) }, @@ -4555,6 +4579,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { updatePadMotion() ControllerPrefs.prefs(this).registerOnSharedPreferenceChangeListener(shortcutPrefListener) readPrefs() + refreshInputProfile() refreshControls() if (::pip.isInitialized) pip.resumed() if (CompositorHost.isStarted) applyFrameGen() @@ -4569,6 +4594,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { backQamInput.cancel() mouseBackInput.cancel() capturePausedFrame() + padBridge?.setInputEnabled(false) sessionClipboard.stop() if (::touchscreenGestures.isInitialized) touchscreenGestures.cancel() if (::touchpad.isInitialized) touchpad.cancel() @@ -4577,12 +4603,17 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { uiHandler.removeCallbacks(clearRecognizedGesture) uiHandler.removeCallbacks(cursorResync) gestureSettingsOpen = false + inputSettingsOpen = false + releasePads() + heldKeys.clear() + heldButtons.clear() drawerOpen = false pcKeyboardOpen = false achievementsOpen = false (getSystemService(INPUT_SERVICE) as? InputManager)?.unregisterInputDeviceListener(deviceListener) releaseDrawerDirection() onScreenControls?.releaseAll() + bottomInputControls?.releaseAll() if (::scrollTouch.isInitialized) scrollTouch.cancel() if (::padMouse.isInitialized) padMouse.reset() resumed = false @@ -4806,6 +4837,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { if (holder !== outputHolder()) return glassesOutputActive = glassesScreen?.holder === holder bottomOutputActive = bottomGame?.holder === holder + updateOnScreenPad() updatePointerCapture() if (bottomOutputActive) bottomRecoveries = 0 if (CompositorHost.isStarted) WaylandCompositor.nativeSetCursorComposite(glassesOutputActive || bottomOutputActive) @@ -4910,6 +4942,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { if (bottomGame !== screen) return bottomGame = null bottomOutputActive = false + updateOnScreenPad() surfaceW = 0 surfaceH = 0 CompositorHost.detach(screen.holder.surface) @@ -4941,9 +4974,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { title, openMenu, openMenu, if (steam) ({ tapSteamButton() }) else null, if (steam) ({ tapQam() }) else null, extras + com.droiddeck.launcher.ui.BarExtra(getString(R.string.swap_screens), icon = R.drawable.ic_bottom_swap, onClick = swapBack), ) - val keyboardExtra = com.droiddeck.launcher.ui.BarExtra(getString(R.string.second_show_keyboard), icon = R.drawable.ic_bottom_keyboard) { - keyboard?.toggle() - } + val profileLayout = secondaryInputLayout androidx.compose.foundation.layout.Box( Modifier.fillMaxSize().background(androidx.compose.ui.graphics.Color.Black) .pointerInput(Unit) { awaitPointerEventScope { while (true) awaitPointerEvent() } }, @@ -4957,36 +4988,16 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { }, modifier = Modifier.fillMaxSize(), ) - secondScreenMode == SecondScreenMode.DECK_CONTROLS && SessionState.deckPad -> - androidx.compose.ui.viewinterop.AndroidView( - factory = { context -> - com.droiddeck.launcher.ui.DeckControlsPanel(context, onClose = openMenu, onSteamMenu = ::tapSteamButton, - onQam = ::tapQam, pad = padBridge, onMenu = openMenu, onSwap = swapBack) - }, - modifier = Modifier.fillMaxSize(), - ) - secondScreenMode == SecondScreenMode.KEYBOARD_TRACKPAD -> androidx.compose.foundation.layout.Column(Modifier.fillMaxSize()) { - com.droiddeck.launcher.ui.BottomScreenBar(bar(getString(SecondScreenMode.KEYBOARD_TRACKPAD.label), listOf(keyboardExtra))) - androidx.compose.ui.viewinterop.AndroidView( - factory = { context -> - com.droiddeck.launcher.ui.SessionTrackpadView(context, ::moveSecondScreenPointer, - ::sendTrackpadButton, - { steps -> WaylandCompositor.nativeSendSceneInput(4, steps, 0) }) - }, - modifier = Modifier.fillMaxWidth().weight(1f), - ) + secondScreenMode == SecondScreenMode.CUSTOM_CONTROLS -> { + androidx.compose.runtime.key(profileLayout) { + androidx.compose.ui.viewinterop.AndroidView( + factory = { context -> com.droiddeck.launcher.ui.InputSurfaceView(context, padBridge, profileLayout?.elements.orEmpty(), + surfaceStyle = profileLayout?.style ?: com.droiddeck.launcher.input.InputSurfaceStyle()) }, + modifier = Modifier.fillMaxSize(), + ) + } } - secondScreenMode == SecondScreenMode.TERMINAL -> androidx.compose.foundation.layout.Column(Modifier.fillMaxSize()) { - var terminal by androidx.compose.runtime.remember { androidx.compose.runtime.mutableStateOf(null) } - com.droiddeck.launcher.ui.BottomScreenBar(bar(getString(SecondScreenMode.TERMINAL.label), listOf( - com.droiddeck.launcher.ui.BarExtra(getString(R.string.drawer_keyboard), icon = R.drawable.ic_bottom_keyboard) { terminal?.showKeyboard() }, - ))) - androidx.compose.ui.viewinterop.AndroidView( - factory = { context -> com.droiddeck.launcher.ui.SessionTerminalPanel(context).also { terminal = it } }, - modifier = Modifier.fillMaxWidth().weight(1f), - ) - } - pages.isEmpty() -> com.droiddeck.launcher.ui.BottomScreenBar(bar(null)) + else -> BottomDashboard( pages, com.droiddeck.launcher.ui.SessionBottomTab.picked(SessionState.sessionId), gameStarts = gameStarts, @@ -5199,6 +5210,8 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { closeSecondScreen(reset = false) glassesScope.cancel() glassesScreen?.let { screen -> glassesScreen = null; runCatching { screen.dismiss() } } + bottomInputControls?.let { (it.parent as? android.view.ViewGroup)?.removeView(it) } + bottomInputControls = null bottomGame?.let { screen -> bottomGame = null; runCatching { screen.dismiss() } } headTracker?.stop() if (::hud.isInitialized) hud.stop() @@ -5207,7 +5220,14 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { joyConVr = null handCamera?.stop() handCamera = null + inputMotion.stop() + padBridge?.setInputEnabled(false) + physicalInputBindings?.close() padBridge?.stop() + DeckControls.clearRouter(inputRouter) + inputRouter = null + heldKeys.clear() + heldButtons.clear() steamController?.stop() if (SessionState.padBridge === padBridge) SessionState.padBridge = null padMotion?.stop() diff --git a/app/src/main/java/com/droiddeck/launcher/input/ControllerEditorActivity.kt b/app/src/main/java/com/droiddeck/launcher/input/ControllerEditorActivity.kt deleted file mode 100644 index cb544d5a4d..0000000000 --- a/app/src/main/java/com/droiddeck/launcher/input/ControllerEditorActivity.kt +++ /dev/null @@ -1,136 +0,0 @@ -package com.droiddeck.launcher.input - -import com.droiddeck.launcher.ui.focusGlide -import android.os.Build -import android.os.Bundle -import android.view.Gravity -import android.view.View -import android.view.WindowManager -import android.widget.FrameLayout -import androidx.activity.ComponentActivity -import androidx.compose.foundation.background -import androidx.compose.foundation.clickable -import androidx.compose.foundation.layout.Arrangement -import androidx.compose.foundation.layout.Column -import androidx.compose.foundation.layout.Row -import androidx.compose.foundation.layout.padding -import androidx.compose.foundation.selection.toggleable -import androidx.compose.foundation.shape.RoundedCornerShape -import androidx.compose.material3.MaterialTheme -import androidx.compose.material3.Text -import androidx.compose.ui.Alignment -import androidx.compose.runtime.Composable -import androidx.compose.runtime.getValue -import androidx.compose.runtime.mutableStateOf -import androidx.compose.runtime.remember -import androidx.compose.runtime.setValue -import androidx.compose.ui.Modifier -import androidx.compose.ui.draw.clip -import androidx.compose.ui.graphics.Color -import androidx.compose.ui.semantics.Role -import androidx.compose.ui.text.font.FontWeight -import androidx.compose.ui.platform.ComposeView -import androidx.compose.ui.unit.dp -import androidx.compose.ui.unit.sp -import androidx.compose.ui.res.stringResource -import com.droiddeck.launcher.R -import com.droiddeck.launcher.ui.DroidDeckTheme -import com.droiddeck.launcher.ui.onSignal -import com.droiddeck.launcher.ui.LocalPalette - -class ControllerEditorActivity : ComponentActivity() { - override fun attachBaseContext(newBase: android.content.Context) { - super.attachBaseContext(newBase) - com.droiddeck.launcher.core.AppLanguage.applyTo(this, newBase) - } - - private lateinit var controls: OnScreenControls - - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - requestedOrientation = com.droiddeck.launcher.session.SessionPrefs.orientation(this) - immersive() - val root = FrameLayout(this) - root.setBackgroundColor(0xFF0B0F14.toInt()) - controls = OnScreenControls(this, null, editing = true) - root.addView(controls, FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT)) - val bar = ComposeView(this).apply { - setContent { - DroidDeckTheme { - var snap by remember { mutableStateOf(ControllerPrefs.read(context).snap) } - Column( - horizontalAlignment = Alignment.CenterHorizontally, - verticalArrangement = Arrangement.spacedBy(5.dp), - modifier = Modifier - .background(MaterialTheme.colorScheme.surface.copy(alpha = 0.9f), RoundedCornerShape(20.dp)) - .padding(start = 5.dp, end = 5.dp, top = 8.dp, bottom = 5.dp), - ) { - Text(stringResource(R.string.ctrl_editor_drag), fontSize = 12.sp, color = MaterialTheme.colorScheme.onSurfaceVariant) - Row(horizontalArrangement = Arrangement.spacedBy(6.dp)) { - EditorButton(stringResource(R.string.ctrl_editor_snap), false, selected = snap) { - snap = !snap - ControllerPrefs.setSnap(context, snap) - controls.setSnap(snap) - } - EditorButton(stringResource(R.string.ctrl_reset), false) { controls.resetLayout() } - EditorButton(stringResource(R.string.common_cancel), false) { finish() } - EditorButton(stringResource(R.string.ctrl_editor_save), true) { - controls.saveLayout() - finish() - } - } - } - } - } - } - root.addView(bar, FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.CENTER)) - setContentView(root) - } - - @Composable - private fun EditorButton(label: String, primary: Boolean, selected: Boolean? = null, onClick: () -> Unit) { - val pal = LocalPalette.current - Text( - label, fontSize = 12.sp, fontWeight = FontWeight.SemiBold, - color = when { - primary -> pal.onSignal - selected == true -> pal.signal - else -> MaterialTheme.colorScheme.onBackground - }, - modifier = Modifier.clip(RoundedCornerShape(50)) - .background( - when { - primary -> pal.signal - selected == true -> pal.signal.copy(alpha = 0.18f) - else -> Color.White.copy(alpha = 0.06f) - }, - ) - .focusGlide(RoundedCornerShape(50)) - .then( - if (selected == null) Modifier.clickable(onClick = onClick) - else Modifier.toggleable(value = selected, role = Role.Switch, onValueChange = { onClick() }), - ) - .padding(horizontal = 12.dp, vertical = 6.dp), - ) - } - - override fun onWindowFocusChanged(hasFocus: Boolean) { - super.onWindowFocusChanged(hasFocus) - if (hasFocus) immersive() - } - - private fun immersive() { - @Suppress("DEPRECATION") - window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY - or View.SYSTEM_UI_FLAG_FULLSCREEN - or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION - or View.SYSTEM_UI_FLAG_LAYOUT_STABLE - or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN - or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION) - if (Build.VERSION.SDK_INT >= 28) { - window.attributes = window.attributes.apply { - layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES - } - } - } -} diff --git a/app/src/main/java/com/droiddeck/launcher/input/ControllerPrefs.kt b/app/src/main/java/com/droiddeck/launcher/input/ControllerPrefs.kt index bb1705ba68..1edc2395ce 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/ControllerPrefs.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/ControllerPrefs.kt @@ -169,56 +169,6 @@ object ControllerPrefs { prefs(context).edit().remove("layout.${width}x$height").apply() } - fun screenKeysShown(context: Context): Boolean = prefs(context).getBoolean("screenKeysShown", true) - - fun setScreenKeysShown(context: Context, shown: Boolean) { - prefs(context).edit().putBoolean("screenKeysShown", shown).apply() - } - - fun screenKeys(context: Context): List = decodeScreenKeys(prefs(context).getString("screenKeys", null).orEmpty()) - - fun setScreenKeys(context: Context, keys: List) { - prefs(context).edit().putString("screenKeys", encodeScreenKeys(keys)).apply() - } - - fun encodeScreenKeys(keys: List): String = keys.joinToString(";") { - "${it.codes.joinToString("+")}:${it.x},${it.y},${it.size}:${escape(it.glyph)}:${escape(it.text)}:${escape(it.image)}:${it.opacity}:${it.shape}:${if (it.toggle) 1 else 0}:${if (it.labels) 1 else 0}" - } - - fun decodeScreenKeys(raw: String): List = raw.split(';').mapNotNull { entry -> - val parts = entry.split(':') - if (parts.size < 2) return@mapNotNull null - val keys = parts[0].split('+').mapNotNull { it.toIntOrNull() }.takeIf { it.isNotEmpty() } ?: return@mapNotNull null - val nums = parts[1].split(',').mapNotNull { it.toFloatOrNull() }.takeIf { it.size == 3 } ?: return@mapNotNull null - ScreenKey( - keys, nums[0], nums[1], nums[2].toInt(), - parts.getOrNull(2)?.let(::unescape).orEmpty(), - parts.getOrNull(3)?.let(::unescape).orEmpty(), - parts.getOrNull(4)?.let(::unescape).orEmpty(), - parts.getOrNull(5)?.toFloatOrNull()?.coerceIn(0.2f, 1f) ?: 1f, - parts.getOrNull(6)?.toIntOrNull()?.coerceIn(SCREEN_KEY_PILL, SCREEN_KEY_STICK) ?: SCREEN_KEY_PILL, - parts.getOrNull(7) == "1", - parts.getOrNull(8) != "0", - ) - } - - fun screenLayouts(context: Context): Map = - prefs(context).all.mapNotNull { (key, value) -> - if (!key.startsWith("screenLayout.") || value !is String) null else key.removePrefix("screenLayout.") to value - }.toMap() - - fun saveScreenLayout(context: Context, name: String, raw: String) { - prefs(context).edit().putString("screenLayout.$name", raw).apply() - } - - fun deleteScreenLayout(context: Context, name: String) { - prefs(context).edit().remove("screenLayout.$name").apply() - } - - private fun escape(value: String) = value.replace("%", "%25").replace(";", "%3B").replace(":", "%3A").replace(",", "%2C") - - private fun unescape(value: String) = value.replace("%2C", ",").replace("%3A", ":").replace("%3B", ";").replace("%25", "%") - fun resetAll(context: Context) { prefs(context).edit().clear().putBoolean("rumble", true).apply() } diff --git a/app/src/main/java/com/droiddeck/launcher/input/DeckControls.kt b/app/src/main/java/com/droiddeck/launcher/input/DeckControls.kt index a8c499793b..41b7240362 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/DeckControls.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/DeckControls.kt @@ -5,53 +5,165 @@ object DeckControls { const val R4 = 2 const val L5 = 4 const val R5 = 8 - private const val LEFT_TOUCH = 16 - private const val RIGHT_TOUCH = 32 - private const val LEFT_CLICK = 64 - private const val RIGHT_CLICK = 128 - - private const val SLOT = 0 - - private var controls = 0 + private val controls = arrayOf(InputControl.L4, InputControl.R4, InputControl.L5, InputControl.R5, + InputControl.LEFT_PAD, InputControl.RIGHT_PAD, InputControl.LEFT_CLICK, InputControl.RIGHT_CLICK) + private val sources = ArrayList() + private val physical = Source(InputRole.PHYSICAL) + private var router: InputRouter? = null private val pads = ShortArray(4) private val pressure = ShortArray(2) + private val touchOwners = arrayOfNulls(2) @Synchronized - fun setGrip(bit: Int, down: Boolean) { - set(bit, down) + fun setRouter(next: InputRouter?) { + if (router === next) return + for (source in sources) source.closeRoutes() + router = next + for (source in sources) source.configure() publish() } @Synchronized - fun setPad(right: Boolean, touching: Boolean, x: Float = 0f, y: Float = 0f) { - val at = if (right) 2 else 0 - set(if (right) RIGHT_TOUCH else LEFT_TOUCH, touching) - pads[at] = if (touching) axis(x) else 0 - pads[at + 1] = if (touching) axis(y) else 0 - publish() + fun clearRouter(owner: InputRouter?) { + if (owner != null && router === owner) setRouter(null) } @Synchronized - fun setClick(right: Boolean, down: Boolean) { - set(if (right) RIGHT_CLICK else LEFT_CLICK, down) - pressure[if (right) 1 else 0] = if (down) Short.MAX_VALUE else 0 - publish() - } + @JvmOverloads + fun source(role: InputRole, routed: Boolean = false) = Source(role, routed) + + @Synchronized + fun setGrip(bit: Int, down: Boolean) = physical.setGrip(bit, down) + + @Synchronized + fun setPad(right: Boolean, touching: Boolean, x: Float = 0f, y: Float = 0f) = physical.setPad(right, touching, x, y) + + @Synchronized + fun setClick(right: Boolean, down: Boolean) = physical.setClick(right, down) @Synchronized fun releaseAll() { - if (controls == 0 && pads.all { it.toInt() == 0 } && pressure.all { it.toInt() == 0 }) return - controls = 0 - pads.fill(0) - pressure.fill(0) + for (source in sources) source.clear() publish() } - private fun set(bit: Int, on: Boolean) { - controls = if (on) controls or bit else controls and bit.inv() + @Synchronized + fun suspend() { + for (source in sources) source.suspend() + publish() } - private fun axis(value: Float): Short = (value.coerceIn(-1f, 1f) * Short.MAX_VALUE).toInt().toShort() + @Synchronized + fun refresh() { + for (source in sources) source.route() + publish() + } - private fun publish() = FakeInputWriter.writeDeckControls(SLOT, controls, pads, pressure) + class Source internal constructor(private val role: InputRole, private val routed: Boolean = false) { + private val x = FloatArray(8) + private val y = FloatArray(8) + private val touched = BooleanArray(8) + private var routes: Array? = null + internal var mask = 0 + internal val axes = ShortArray(4) + + internal fun configure() { + routes = router?.takeUnless { routed }?.let { r -> Array(8) { i -> + r.channel(role, controls[i]).also { it.awaitNeutral(x[i], y[i], touched[i]) } + } } + mask = 0 + axes.fill(0) + } + + private fun attach() { + if (this !in sources) { sources.add(this); configure() } + } + + fun setGrip(bit: Int, down: Boolean) = synchronized(DeckControls) { + val at = when (bit) { L4 -> 0; R4 -> 1; L5 -> 2; R5 -> 3; else -> return@synchronized } + update(at, if (down) 1f else 0f) + } + + fun setPad(right: Boolean, touching: Boolean, x: Float = 0f, y: Float = 0f) = synchronized(DeckControls) { + update(if (right) 5 else 4, if (touching) x else 0f, if (touching) y else 0f, touching) + } + + fun setClick(right: Boolean, down: Boolean) = synchronized(DeckControls) { + update(if (right) 7 else 6, if (down) 1f else 0f) + } + + private fun update(at: Int, value: Float, second: Float = 0f, contact: Boolean = false) { + val timing = InputTiming.begin() + attach() + x[at] = value; y[at] = second; touched[at] = contact + route() + publish() + InputTiming.end(timing) + } + + internal fun native(control: InputControl?, value: Float, second: Float, contact: Boolean) = synchronized(DeckControls) { + require(routed) + attach() + x.fill(0f); y.fill(0f); touched.fill(false) + val at = controls.indexOf(control) + if (at >= 0) { x[at] = value; y[at] = second; touched[at] = contact } + route(); publish() + } + + internal fun route() { + mask = 0 + axes.fill(0) + for (i in controls.indices) { + val target = if (routes == null) controls[i] else routes!![i].update(x[i], y[i], touched[i]) ?: continue + val at = controls.indexOf(target) + if (at < 0) continue + if (at == 4 || at == 5) { + if (touched[i]) { + mask = mask or (1 shl at) + axes[(at - 4) * 2] = axis(x[i]); axes[(at - 4) * 2 + 1] = axis(y[i]) + } + } else if (x[i] > 0.5f) mask = mask or (1 shl at) + } + } + + internal fun clear() { + x.fill(0f); y.fill(0f); touched.fill(false); mask = 0; axes.fill(0) + routes?.forEach { it.release() } + } + + internal fun suspend() { + mask = 0; axes.fill(0) + routes?.forEach { it.suspend() } + } + + fun release() = synchronized(DeckControls) { clear(); publish() } + + internal fun closeRoutes() { routes?.forEach { it.close() }; routes = null } + + fun close() = synchronized(DeckControls) { + clear(); closeRoutes(); sources.remove(this); publish() + } + } + + private fun axis(value: Float) = (value.coerceIn(-1f, 1f) * Short.MAX_VALUE).toInt().toShort() + + private fun publish() { + var mask = 0 + pads.fill(0) + for (source in sources) { + mask = mask or source.mask + } + for (side in 0..1) { + val contact = 16 shl side + val owner = touchOwners[side]?.takeIf { it.mask and contact != 0 } + ?: sources.firstOrNull { it.mask and contact != 0 } + touchOwners[side] = owner + if (owner == null) continue + pads[side * 2] = owner.axes[side * 2] + pads[side * 2 + 1] = owner.axes[side * 2 + 1] + } + pressure[0] = if (mask and 64 != 0) Short.MAX_VALUE else 0 + pressure[1] = if (mask and 128 != 0) Short.MAX_VALUE else 0 + FakeInputWriter.writeDeckControls(0, mask, pads, pressure) + } } diff --git a/app/src/main/java/com/droiddeck/launcher/ui/GamepadGeometry.kt b/app/src/main/java/com/droiddeck/launcher/input/GamepadGeometry.kt similarity index 99% rename from app/src/main/java/com/droiddeck/launcher/ui/GamepadGeometry.kt rename to app/src/main/java/com/droiddeck/launcher/input/GamepadGeometry.kt index 2b369c20aa..988ab5f764 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/GamepadGeometry.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/GamepadGeometry.kt @@ -1,4 +1,4 @@ -package com.droiddeck.launcher.ui +package com.droiddeck.launcher.input import kotlin.math.min diff --git a/app/src/main/java/com/droiddeck/launcher/input/GestureTouchStreamGate.kt b/app/src/main/java/com/droiddeck/launcher/input/GestureTouchStreamGate.kt index 4d69f68c5b..9ba24e3381 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/GestureTouchStreamGate.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/GestureTouchStreamGate.kt @@ -3,7 +3,7 @@ package com.droiddeck.launcher.input import android.view.MotionEvent internal class GestureTouchStreamGate { - enum class Source { SURFACE, SCREEN_KEYS, ON_SCREEN_CONTROLS } + enum class Source { SURFACE, ON_SCREEN_CONTROLS } private var owner: Source? = null diff --git a/app/src/main/java/com/droiddeck/launcher/input/HeldInput.kt b/app/src/main/java/com/droiddeck/launcher/input/HeldInput.kt new file mode 100644 index 0000000000..1fdb6121fc --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/input/HeldInput.kt @@ -0,0 +1,27 @@ +package com.droiddeck.launcher.input + +class HeldInput(private val emit: (Int, Boolean) -> Unit) { + private val owners = IntArray(768) + + @Synchronized + fun set(source: Int, code: Int, down: Boolean) { + if (code !in owners.indices) return + val before = owners[code] + val after = if (down) before or source else before and source.inv() + owners[code] = after + if ((before == 0) != (after == 0)) emit(code, after != 0) + } + + @Synchronized + fun release(source: Int) { + for (code in owners.indices) if (owners[code] and source != 0) set(source, code, false) + } + + @Synchronized + fun clear() { + for (code in owners.indices) if (owners[code] != 0) { + owners[code] = 0 + emit(code, false) + } + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/input/ScreenKeyIcons.kt b/app/src/main/java/com/droiddeck/launcher/input/InputButtonIcons.kt similarity index 77% rename from app/src/main/java/com/droiddeck/launcher/input/ScreenKeyIcons.kt rename to app/src/main/java/com/droiddeck/launcher/input/InputButtonIcons.kt index b1ea677b99..8f87c05cbf 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/ScreenKeyIcons.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/InputButtonIcons.kt @@ -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 ScreenKeyIcons { - fun has(code: Int): Boolean = code in SET +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 - private val SET = setOf(28, 14, 15, 57, 42, 54, 29, 97, 125, 103, 108, 105, 106) + fun has(code: Int): Boolean = code in codes - fun draw(canvas: Canvas, code: Int, cx: Float, cy: Float, box: Float, paint: Paint): Boolean { - if (!has(code)) return false + 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 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() diff --git a/app/src/main/java/com/droiddeck/launcher/input/InputLibraries.kt b/app/src/main/java/com/droiddeck/launcher/input/InputLibraries.kt new file mode 100644 index 0000000000..28e36f1806 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/input/InputLibraries.kt @@ -0,0 +1,294 @@ +package com.droiddeck.launcher.input + +import android.content.Context +import android.hardware.display.DisplayManager +import android.util.DisplayMetrics +import android.view.Display +import org.json.JSONArray +import org.json.JSONObject +import java.util.UUID + +object InputLibraries { + private const val VERSION = 8 + private const val KEY = "library" + private const val RECOVERY = "library-recovery." + const val OVERLAY = "builtin.overlay" + const val DECK = "builtin.deck" + const val TRACKPAD = "builtin.trackpad" + + private fun prefs(context: Context) = context.getSharedPreferences("input-profiles", Context.MODE_PRIVATE) + + @Synchronized + fun read(context: Context): InputLibrary { + val p = prefs(context) + val raw = p.getString(KEY, null) + val decoded = raw?.let(::decode) + if (raw != null && decoded == null) return scopeLayouts(upgradeStockLayouts(upgradeOverlay(context, migrate(context)))) + var library = decoded ?: scopeLayouts(upgradeStockLayouts(upgradeOverlay(context, migrate(context)))) + val version = raw?.let { JSONObject(it).getInt("version") } ?: 0 + if (version < 5) library = scopeLayouts(upgradeStockLayouts(upgradeOverlay(context, library))) + if (version < 6 || !p.contains(LegacyScreenKeyMigration.BACKUP)) { + val legacy = LegacyScreenKeyMigration.snapshot(context) + if (legacy != null) { + val upgraded = runCatching { LegacyScreenKeyMigration.migrate(context, library, legacy) }.getOrElse { + android.util.Log.e("InputLibraries", "Could not migrate screen keys; keeping the original settings", it) + return library + } + val encoded = encode(upgraded) + require(encoded.length <= 2_000_000) + if (!p.edit().putString(KEY, encoded).putString(LegacyScreenKeyMigration.BACKUP, legacy).commit()) return library + LegacyScreenKeyMigration.clean(context) + return upgraded + } + } + if (raw == null || version < VERSION) write(context, library) + if (p.contains(LegacyScreenKeyMigration.BACKUP)) LegacyScreenKeyMigration.clean(context) + return library + } + + fun hasPendingScreenKeyMigration(context: Context) = !prefs(context).contains(LegacyScreenKeyMigration.BACKUP) && LegacyScreenKeyMigration.snapshot(context) != null + + fun hasLibraryRecovery(context: Context) = prefs(context).all.keys.any { it.startsWith(RECOVERY) } + + @Synchronized + fun update(context: Context, change: (InputLibrary) -> InputLibrary): InputLibrary { + val p = prefs(context) + val stored = p.getString(KEY, null) + val invalid = stored != null && decode(stored) == null + require(!invalid || runCatching { JSONObject(stored!!).getInt("version") }.getOrNull()?.let { it <= VERSION } != false) { + "The saved input library cannot be edited by this version." + } + val updated = change(read(context)).validate() + if (invalid) { + val encoded = encode(updated) + require(encoded.length <= 2_000_000) { "The input library is too large to save." } + check(p.edit().putString(RECOVERY + UUID.nameUUIDFromBytes(stored!!.toByteArray(Charsets.UTF_8)), stored) + .putString(KEY, encoded).commit()) { "Could not save the input library and its recovery copy." } + } else write(context, updated) + return updated + } + + private fun write(context: Context, library: InputLibrary) { + val encoded = encode(library.validate()) + require(encoded.length <= 2_000_000) { "The input library is too large to save." } + prefs(context).edit().putString(KEY, encoded).apply() + } + + private fun migrate(context: Context): InputLibrary { + val profiles = mutableListOf() + 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 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()) + bindings += preset + InputPart(preset.id, when (role) { InputRole.PHYSICAL -> null; InputRole.PRIMARY -> OVERLAY; InputRole.SECONDARY -> layout.id }) + } + profiles += SavedInputProfile(id, name, id, parts) + } + val selected = prefs(context).getString("selected", InputProfiles.AUTO) + ?.takeIf { it == InputProfiles.AUTO || profiles.any { p -> p.id == it } } ?: InputProfiles.AUTO + return InputLibrary(profiles, layouts, bindings, selected).validate() + } + + @Suppress("DEPRECATION") + private fun upgradeOverlay(context: Context, library: InputLibrary): InputLibrary { + if (library.layouts.none { it.template == InputLayoutTemplate.OVERLAY }) return library + val display = context.getSystemService(DisplayManager::class.java)?.getDisplay(Display.DEFAULT_DISPLAY) + val surfaceContext = display?.let(context::createDisplayContext) ?: context + val metrics = DisplayMetrics().also { if (display != null) display.getRealMetrics(it) else it.setTo(context.resources.displayMetrics) } + val settings = ControllerPrefs.read(context) + val elements = LegacyGamepadLayout(surfaceContext, metrics.widthPixels.coerceAtLeast(1), metrics.heightPixels.coerceAtLeast(1)).elements() + val style = InputSurfaceStyle(settings.tint, settings.opacity) + val replacements = library.layouts.filter { it.template == InputLayoutTemplate.OVERLAY }.associate { old -> + old.id to old.copy(name = "Gamepad", elements = elements, template = InputLayoutTemplate.CUSTOM, style = style) + } + var result = library.copy(layouts = library.layouts.map { replacements[it.id] ?: it }) + val migrated = mutableMapOf, String>() + for (profile in library.profiles) { + val parts = profile.parts.mapValues { (role, part) -> + val layout = replacements[part.layout] ?: return@mapValues part + val key = part.bindings to layout.id + val id = migrated.getOrPut(key) { + val old = library.preset(part.bindings) + val newId = migrationBindingId(result, old.id, layout.id) + val keyboard = elements.filter { it.id == "builtin:keyboard" }.associate { it.id to InputAction.Command(InputCommand.KEYBOARD) } + val clicks = if (settings.stickClick) old.clicks else old.clicks + elements.filter { it.hasClick }.associate { it.id to InputAction.Disabled } + result = result.withPreset(old.copy(id = newId, layoutId = layout.id, elements = old.elements + keyboard, clicks = clicks)) + newId + } + part.copy(bindings = id) + } + result = result.withProfile(profile.copy(parts = parts)) + } + val oldBindings = migrated.keys.map { it.first }.toSet() + result = result.copy(bindings = result.bindings.filter { it.id !in oldBindings || result.usersOfBindings(it.id).isNotEmpty() }) + // Older editor layouts were resolution-specific. Keep each as a selectable saved layout. + for (key in ControllerPrefs.prefs(context).all.keys.filter { it.startsWith("layout.") }.sorted()) { + val size = key.removePrefix("layout.").split('x').mapNotNull { it.toIntOrNull() } + if (size.size != 2 || size.any { it <= 0 } || result.layouts.size >= 128) continue + if (size[0] == metrics.widthPixels && size[1] == metrics.heightPixels) continue + val id = "migrated.$key" + if (result.layouts.none { it.id == id }) result = result.withLayout(SavedInputLayout(id, "Gamepad ${size[0]} × ${size[1]}", + LegacyGamepadLayout(surfaceContext, size[0], size[1]).elements(), style = style)) + } + return result.validate() + } + + private fun upgradeStockLayouts(library: InputLibrary): InputLibrary { + val stock = InputStockLayouts.layouts() + val replacements = library.layouts.filter { it.template == InputLayoutTemplate.DECK || it.template == InputLayoutTemplate.TRACKPAD } + .associate { old -> old.id to stock.first { it.id == if (old.template == InputLayoutTemplate.DECK) DECK else TRACKPAD }.copy(id = old.id, name = old.name) } + var result = library.copy(layouts = library.layouts.map { replacements[it.id] ?: it }) + val migrated = mutableMapOf, String>() + for (profile in library.profiles) { + val parts = profile.parts.mapValues { (_, part) -> + val layout = replacements[part.layout] ?: return@mapValues part + val id = migrated.getOrPut(part.bindings to layout.id) { + val old = library.preset(part.bindings) + val id = migrationBindingId(result, old.id, layout.id) + val original = library.layouts.first { it.id == layout.id } + val stockLayout = layout.copy(id = if (original.template == InputLayoutTemplate.TRACKPAD) TRACKPAD else DECK) + result = result.withPreset(old.copy(id = id, layoutId = layout.id, + elements = InputStockLayouts.actions(stockLayout) + old.elements, clicks = InputStockLayouts.clicks(stockLayout) + old.clicks)) + id + } + part.copy(bindings = id) + } + result = result.withProfile(profile.copy(parts = parts)) + } + val oldBindings = migrated.keys.map { it.first }.toSet() + result = result.copy(bindings = result.bindings.filter { it.id !in oldBindings || result.usersOfBindings(it.id).isNotEmpty() }) + for (layout in stock) if (result.layouts.none { it.id == layout.id }) result = result.withLayout(layout) + for (preset in InputStockLayouts.presets(stock)) if (result.bindings.none { it.id == preset.id }) result = result.withPreset(preset) + return result.validate() + } + + private fun migrationBindingId(library: InputLibrary, binding: String, layout: String): String { + val seed = JSONArray(listOf(binding, layout)).toString().toByteArray(Charsets.UTF_8) + val base = "migrated.bindings." + UUID.nameUUIDFromBytes(seed) + var id = base + var suffix = 0 + while (library.bindings.any { it.id == id }) id = "$base.${++suffix}" + return id + } + + private fun scopeLayouts(library: InputLibrary): InputLibrary = library.copy(layouts = library.layouts.map { layout -> + val roles = library.profiles.flatMap { p -> p.parts.filterValues { it.layout == layout.id }.keys } + + library.selections.values.flatMap { s -> s.parts.filterValues { library.preset(it).layoutId == layout.id }.keys } + val screen = when { + InputRole.PRIMARY in roles && InputRole.SECONDARY in roles -> InputLayoutScreen.BOTH + InputRole.PRIMARY in roles -> InputLayoutScreen.MAIN + InputRole.SECONDARY in roles -> InputLayoutScreen.SECONDARY + layout.id == OVERLAY || layout.id.startsWith("migrated.layout.") -> InputLayoutScreen.MAIN + else -> InputLayoutScreen.SECONDARY + } + layout.copy(screen = screen) + }) + + internal fun encode(library: InputLibrary): String { + val layouts = JSONArray() + for (layout in library.layouts) { + val elements = JSONArray() + for (e in layout.elements) elements.put(JSONObject().put("id", e.id).put("kind", e.kind.name) + .put("control", e.control.id).put("name", e.name).put("x", e.x.toDouble()).put("y", e.y.toDouble()) + .put("width", e.width.toDouble()).put("height", e.height.toDouble()).put("floating", e.floating).put("hideWhenIdle", e.hideWhenIdle).put("mouseGestures", e.mouseGestures).put("toggle", e.toggle).put("icon", e.icon).put("image", e.image) + .put("elementOpacity", e.opacity.toDouble()).put("buttonShape", e.buttonShape.name).put("showLabel", e.showLabel)) + layouts.put(JSONObject().put("id", layout.id).put("name", layout.name).put("template", layout.template.name).put("elements", elements) + .put("tint", layout.style.tint).put("opacity", layout.style.opacity).put("screen", layout.screen.name)) + } + val bindings = JSONArray() + for (b in library.bindings) { + val controls = JSONObject(); b.controls.forEach { (c, a) -> controls.put(c.id, InputProfiles.encode(a)) } + val elements = JSONObject(); b.elements.forEach { (id, a) -> elements.put(id, InputProfiles.encode(a)) } + val clicks = JSONObject(); b.clicks.forEach { (id, a) -> clicks.put(id, InputProfiles.encode(a)) } + val keys = JSONArray() + b.keys.forEach { binding -> keys.put(JSONObject().put("device", binding.key.device).put("name", binding.key.name) + .put("keyCode", binding.key.keyCode).put("scanCode", binding.key.scanCode).put("action", InputProfiles.encode(binding.action))) } + bindings.put(JSONObject().put("id", b.id).put("name", b.name).put("physical", b.physical) + .put("layout", b.layoutId).put("controls", controls).put("elements", elements).put("keys", keys).put("clicks", clicks)) + } + val profiles = JSONArray() + for (p in library.profiles) { + val parts = JSONObject() + p.parts.forEach { (role, part) -> parts.put(role.name, JSONObject().put("bindings", part.bindings).put("layout", part.layout)) } + profiles.put(JSONObject().put("id", p.id).put("name", p.name).put("base", p.base).put("parts", parts)) + } + val selections = JSONObject() + for ((mode, selection) in library.selections) { + val parts = JSONObject() + selection.parts.forEach { (role, id) -> parts.put(role.name, id) } + selections.put(mode, JSONObject().put("profile", selection.profile).put("parts", parts).put("profileSelected", selection.profileSelected)) + } + return JSONObject().put("selections", selections).put("version", VERSION).put("selected", library.selected).put("steam", library.steam).put("desktop", library.desktop) + .put("profiles", profiles).put("layouts", layouts).put("bindings", bindings).toString() + } + + internal fun decode(raw: String): InputLibrary? = runCatching { + require(raw.length <= 2_000_000) + val doc = JSONObject(raw); require(doc.getInt("version") in 2..VERSION) + val layouts = doc.getJSONArray("layouts").objects(256).map { layout -> + val elements = layout.getJSONArray("elements").objects(64).map { e -> + InputSurfaceElement(requireNotNull(InputControl.fromId(e.getString("control"))), e.getDouble("x").toFloat(), + e.getDouble("y").toFloat(), e.getDouble("width").toFloat(), e.getDouble("height").toFloat(), + e.getString("id"), InputElementKind.valueOf(e.getString("kind")), e.getString("name"), + e.optBoolean("floating", true), e.optBoolean("hideWhenIdle", false), e.optBoolean("mouseGestures", false), e.optBoolean("toggle", false), e.optInt("icon", 0), e.optString("image", ""), + e.optDouble("elementOpacity", 1.0).toFloat(), InputButtonShape.valueOf(e.optString("buttonShape", InputButtonShape.ROUNDED.name)), e.optBoolean("showLabel", true)) + } + SavedInputLayout(layout.getString("id"), layout.getString("name"), elements, InputLayoutTemplate.valueOf(layout.getString("template")), + InputSurfaceStyle(layout.optInt("tint", ControllerPrefs.STEAM_BLUE), layout.optInt("opacity", 100)), + InputLayoutScreen.valueOf(layout.optString("screen", InputLayoutScreen.BOTH.name))) + } + val bindings = doc.getJSONArray("bindings").objects(512).map { b -> + val controls = b.getJSONObject("controls").actions(28).mapKeys { requireNotNull(InputControl.fromId(it.key)) } + val keys = b.getJSONArray("keys").objects(64).map { k -> PhysicalInputBinding( + PhysicalInputKey(k.getString("device"), k.getString("name"), k.getInt("keyCode"), k.getInt("scanCode")), + requireNotNull(InputProfiles.decode(k.getJSONObject("action")))) } + SavedInputBindings(b.getString("id"), b.getString("name"), b.getBoolean("physical"), b.optionalString("layout"), + controls, b.getJSONObject("elements").actions(64), keys, b.optJSONObject("clicks")?.actions(64).orEmpty()) + } + val profiles = doc.getJSONArray("profiles").objects(64).map { p -> + val parts = p.getJSONObject("parts") + SavedInputProfile(p.getString("id"), p.getString("name"), p.getString("base"), InputRole.entries.associateWith { role -> + val part = parts.getJSONObject(role.name); InputPart(part.getString("bindings"), part.optionalString("layout")) + }) + } + val selections = doc.optJSONObject("selections")?.let { saved -> + require(saved.length() <= 2) + saved.keys().asSequence().associateWith { mode -> + val value = saved.getJSONObject(mode) + val parts = value.getJSONObject("parts") + require(parts.length() <= InputRole.entries.size) + val profile = value.getString("profile") + InputSelection(profile, parts.keys().asSequence().associate { role -> InputRole.valueOf(role) to parts.getString(role) }, + value.optBoolean("profileSelected", profile != InputProfiles.AUTO || parts.length() == 0)) + } + }.orEmpty() + InputLibrary(profiles, layouts, bindings, doc.getString("selected"), doc.getString("steam"), doc.getString("desktop"), selections).validate() + }.getOrNull() + + private fun JSONArray.objects(limit: Int): List { + require(length() <= limit) + return (0 until length()).map { getJSONObject(it) } + } + + private fun JSONObject.actions(limit: Int): Map { + require(length() <= limit) + return keys().asSequence().associateWith { requireNotNull(InputProfiles.decode(getJSONObject(it))) } + } + + private fun JSONObject.optionalString(key: String): String? = if (isNull(key)) null else getString(key) +} diff --git a/app/src/main/java/com/droiddeck/launcher/input/InputLibrary.kt b/app/src/main/java/com/droiddeck/launcher/input/InputLibrary.kt new file mode 100644 index 0000000000..217e42216a --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/input/InputLibrary.kt @@ -0,0 +1,286 @@ +package com.droiddeck.launcher.input + +import java.util.UUID + +enum class InputLayoutTemplate { CUSTOM, OVERLAY, DECK, TRACKPAD } + +enum class InputLayoutScreen { MAIN, SECONDARY, BOTH; + fun supports(role: InputRole) = role != InputRole.PHYSICAL && (this == BOTH || + if (role == InputRole.PRIMARY) this == MAIN else this == SECONDARY) +} + +data class InputSurfaceStyle(val tint: Int = ControllerPrefs.STEAM_BLUE, val opacity: Int = 100) + +data class SavedInputLayout( + val id: String, val name: String, val elements: List = emptyList(), + val template: InputLayoutTemplate = InputLayoutTemplate.CUSTOM, + val style: InputSurfaceStyle = InputSurfaceStyle(), + val screen: InputLayoutScreen = InputLayoutScreen.BOTH, +) { + val editable get() = template == InputLayoutTemplate.CUSTOM + fun supports(role: InputRole) = screen.supports(role) && when (template) { + InputLayoutTemplate.OVERLAY -> role == InputRole.PRIMARY + InputLayoutTemplate.DECK, InputLayoutTemplate.TRACKPAD -> role == InputRole.SECONDARY + InputLayoutTemplate.CUSTOM -> true + } +} + +data class SavedInputBindings( + val id: String, val name: String, val physical: Boolean, + val layoutId: String? = null, + val controls: Map = emptyMap(), + val elements: Map = emptyMap(), + val keys: List = emptyList(), + val clicks: Map = emptyMap(), +) { + fun compatible(layout: SavedInputLayout?) = if (physical) layout == null + else layout != null && if (layout.editable) layoutId == layout.id else layoutId == null + + fun action(element: InputSurfaceElement) = elements[element.id] + ?: if (element.id == "legacy:${element.control.id}") controls[element.control] ?: InputAction.Native() + else InputAction.Disabled + + fun clickAction(element: InputSurfaceElement) = clicks[element.id] + ?: defaultElementClick(element, action(element)) { controls[it] ?: InputAction.Native() } +} + +data class InputSelection( + val profile: String = InputProfiles.AUTO, val parts: Map = emptyMap(), val profileSelected: Boolean = false, +) + +data class InputPart(val bindings: String, val layout: String? = null) + +data class SavedInputProfile(val id: String, val name: String, val base: String, val parts: Map) { + fun binding(role: InputRole) = parts.getValue(role).bindings +} + +data class InputLibrary( + val profiles: List, val layouts: List, val bindings: List, + val selected: String = InputProfiles.AUTO, + val steam: String = InputProfile.GAME, val desktop: String = InputProfile.DESKTOP, + val selections: Map = emptyMap(), +) { + fun profile(id: String) = profiles.first { it.id == id } + fun preset(id: String) = bindings.first { it.id == id } + fun layout(profile: String, role: InputRole) = profiles.firstOrNull { it.id == profile }?.parts?.get(role)?.layout + ?.let { id -> layouts.firstOrNull { it.id == id } } + private fun mode(isDesktop: Boolean) = if (isDesktop) InputProfile.DESKTOP else InputProfile.GAME + fun selection(isDesktop: Boolean) = selections[mode(isDesktop)] ?: InputSelection(selected) + fun activeId(isDesktop: Boolean): String { + val id = selection(isDesktop).profile + return if (id == InputProfiles.AUTO) { if (isDesktop) desktop else steam } else id + } + fun selectProfile(isDesktop: Boolean, id: String) = copy(selections = selections + (mode(isDesktop) to InputSelection(id, profileSelected = true))) + fun selectPart(isDesktop: Boolean, role: InputRole, bindingsId: String): InputLibrary { + val previous = selection(isDesktop) + return copy(selections = selections + (mode(isDesktop) to previous.copy(parts = previous.parts + (role to bindingsId)))) + } + fun activePart(isDesktop: Boolean, role: InputRole): InputPart { + val binding = selection(isDesktop).parts[role]?.let(::preset) + return if (binding != null) InputPart(binding.id, binding.layoutId) else profile(activeId(isDesktop)).parts.getValue(role) + } + fun activeLayout(isDesktop: Boolean, role: InputRole) = activePart(isDesktop, role).layout?.let { id -> layouts.first { it.id == id } } + fun compileActive(isDesktop: Boolean): InputProfile = compileParts(activeId(isDesktop), InputRole.entries.associateWith { preset(activePart(isDesktop, it).bindings) }) + + fun saveSelection(isDesktop: Boolean, name: String, id: String = newInputId()): InputLibrary { + val current = profile(activeId(isDesktop)) + var result = withProfile(SavedInputProfile(id, inputName(name), current.base, InputRole.entries.associateWith { activePart(isDesktop, it) })) + for (role in InputRole.entries) result = result.usePart(id, role, id, shared = false) + return result.selectProfile(isDesktop, id) + } + + + fun compile(id: String): InputProfile { + val profile = profile(id) + val parts = InputRole.entries.associateWith { preset(profile.binding(it)) } + return compileParts(id, parts) + } + + private fun compileParts(id: String, parts: Map): InputProfile { + return InputProfile(id, parts.mapValues { it.value.controls }.filterValues { it.isNotEmpty() }, + parts.getValue(InputRole.PHYSICAL).keys, + parts.mapValues { it.value.elements }.filterValues { it.isNotEmpty() }, + parts.mapValues { it.value.clicks }.filterValues { it.isNotEmpty() }) + } + + fun usersOfBindings(id: String) = profiles.filter { p -> p.parts.values.any { it.bindings == id } } + fun selectedBindings(id: String) = selections.values.any { id in it.parts.values } + fun usersOfLayout(id: String) = profiles.filter { p -> p.parts.values.any { it.layout == id } } + fun withProfile(value: SavedInputProfile) = copy(profiles = profiles.filterNot { it.id == value.id } + value) + fun withPreset(value: SavedInputBindings) = copy(bindings = bindings.filterNot { it.id == value.id } + value) + fun withLayout(value: SavedInputLayout) = copy(layouts = layouts.filterNot { it.id == value.id } + value, + bindings = bindings.map { preset -> if (preset.layoutId != value.id) preset else preset.copy( + elements = preset.elements.filter { (id, action) -> value.elements.any { it.id == id && action.validFor(it) } }, + clicks = preset.clicks.filter { (id, action) -> value.elements.any { it.id == id && it.hasClick && action.validForClick() } }) }) + + fun duplicateProfile(id: String, name: String, newId: String = newInputId(), sharePhysical: Boolean = false): InputLibrary { + require(profiles.size < 64) + var result = withProfile(profile(id).copy(id = newId, name = inputName(name))) + for (role in InputRole.entries) result = result.usePart(newId, role, id, shared = sharePhysical && role == InputRole.PHYSICAL) + return result + } + + fun usePart(profileId: String, role: InputRole, sourceId: String, shared: Boolean): InputLibrary { + val target = profile(profileId) + val source = profile(sourceId).parts.getValue(role) + if (shared) return withProfile(target.copy(parts = target.parts + (role to source))) + val sourceLayout = layout(sourceId, role) + val name = (target.name + " / " + role.name.lowercase()).take(80) + val copiedLayout = sourceLayout?.takeIf { it.editable }?.copy(id = newInputId(), name = name) + val copiedBindings = preset(source.bindings).copy(id = newInputId(), name = name, + layoutId = copiedLayout?.id ?: preset(source.bindings).layoutId) + return (if (copiedLayout == null) this else withLayout(copiedLayout)).withPreset(copiedBindings) + .withProfile(target.copy(parts = target.parts + (role to InputPart(copiedBindings.id, copiedLayout?.id ?: source.layout)))) + } + + fun useLayout(profileId: String, role: InputRole, layoutId: String, shared: Boolean): InputLibrary { + val source = layouts.first { it.id == layoutId } + require(source.supports(role)) + val target = profile(profileId) + val layout = if (shared || !source.editable) source else source.copy(id = newInputId(), name = (target.name + " / " + source.name).take(80)) + val previous = preset(target.binding(role)) + val actions = if (previous.compatible(source)) previous else bindings.firstOrNull { it.compatible(source) } ?: SavedInputBindings(newInputId(), target.name, false, + controls = if (source.editable) emptyMap() else InputProfile.defaults(target.base).bindings[role].orEmpty(), + elements = if (source.editable) source.elements.associate { it.id to InputAction.Disabled } else emptyMap()) + val copied = actions.copy(id = newInputId(), name = (target.name + " / " + role.name.lowercase()).take(80), + layoutId = layout.id.takeIf { layout.editable }) + return withLayout(layout).withPreset(copied) + .withProfile(target.copy(parts = target.parts + (role to InputPart(copied.id, layout.id)))) + } + + fun copyBindings(id: String, name: String, profileId: String? = null, role: InputRole? = null, newId: String = newInputId()): InputLibrary { + require(bindings.size < 512) + var result = withPreset(preset(id).copy(id = newId, name = inputName(name))) + if (profileId != null && role != null) result = result.chooseBindings(profileId, role, newId) + return result + } + + fun chooseBindings(profileId: String, role: InputRole, id: String): InputLibrary { + val profile = profile(profileId) + require(preset(id).compatible(layout(profileId, role))) + return withProfile(profile.copy(parts = profile.parts + (role to profile.parts.getValue(role).copy(bindings = id)))) + } + + fun chooseLayout(profileId: String, role: InputRole, id: String): InputLibrary { + val layout = layouts.first { it.id == id } + require(layout.supports(role)) + val profile = profile(profileId) + val previous = preset(profile.binding(role)) + val compatible = bindings.firstOrNull { it.id == previous.id && it.compatible(layout) } + ?: bindings.firstOrNull { it.compatible(layout) } + ?: SavedInputBindings(newInputId(), layout.name, false, if (layout.editable) layout.id else null) + return withPreset(compatible).withProfile(profile.copy(parts = profile.parts + (role to InputPart(compatible.id, id)))) + } + + fun copyLayout(id: String, name: String, newId: String = newInputId()): InputLibrary { + val source = layouts.first { it.id == id } + require(source.editable) + return withLayout(source.copy(id = newId, name = inputName(name))) + } + + fun removeProfile(id: String): InputLibrary { + require(id != InputProfile.GAME && id != InputProfile.DESKTOP) + return copy(profiles = profiles.filterNot { it.id == id }, selected = selected.takeUnless { it == id } ?: InputProfiles.AUTO, + steam = steam.takeUnless { it == id } ?: InputProfile.GAME, desktop = desktop.takeUnless { it == id } ?: InputProfile.DESKTOP, + selections = selections.mapValues { (_, selection) -> if (selection.profile == id) selection.copy(profile = InputProfiles.AUTO) else selection }) + } + + fun removeLayout(id: String): InputLibrary { + require(usersOfLayout(id).isEmpty() && bindings.none { it.layoutId == id && selectedBindings(it.id) } && layouts.first { it.id == id }.editable) + return copy(layouts = layouts.filterNot { it.id == id }, bindings = bindings.filterNot { it.layoutId == id }) + } + + fun removeBindings(id: String): InputLibrary { + require(usersOfBindings(id).isEmpty() && !selectedBindings(id)) + return copy(bindings = bindings.filterNot { it.id == id }) + } + + fun resetProfile(id: String): InputLibrary { + val p = profile(id) + var result = this + val defaults = InputProfile.defaults(p.base) + val parts = p.parts.mapValues { (role, part) -> + val previous = preset(part.bindings) + val isPrivate = usersOfBindings(part.bindings).all { it.id == id } + 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)?.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) + } + return result.withProfile(p.copy(parts = parts)) + } + + fun validate(): InputLibrary { + require(profiles.size in 2..64 && layouts.size in 3..256 && bindings.size in 1..512) + fun unique(ids: List) { require(ids.all { it.isNotBlank() && it.length <= 128 } && ids.distinct().size == ids.size) } + unique(profiles.map { it.id }); unique(layouts.map { it.id }); unique(bindings.map { it.id }) + require(profiles.any { it.id == InputProfile.GAME } && profiles.any { it.id == InputProfile.DESKTOP }) + require(selected == InputProfiles.AUTO || profiles.any { it.id == selected }) + require(profiles.any { it.id == steam } && profiles.any { it.id == desktop }) + for (layout in layouts) { + require(layout.style.opacity in 1..100) + inputName(layout.name); require(layout.elements.size <= 64); unique(layout.elements.map { it.id }) + for (e in layout.elements) { + inputName(e.name) + require(e.icon == 0 || InputButtonIcons.has(e.icon)) + require(e.image.isEmpty() || e.image.matches(Regex("[A-Za-z0-9_.-]{1,128}")) && e.image != "." && e.image != "..") + require(e.opacity.isFinite() && e.opacity in 0.01f..1f) + require(!e.toggle || e.kind == InputElementKind.BUTTON || e.kind == InputElementKind.TRIGGER) + require(listOf(e.x, e.y, e.width, e.height).all { it.isFinite() }) + require(e == e.bounded()) + require(if (e.id.startsWith("legacy:")) e.id == "legacy:${e.control.id}" && e.kind == InputElementKind.from(e.control) + else e.control == e.kind.control) + } + } + for (preset in bindings) { + inputName(preset.name) + require(preset.controls.all { (c, a) -> a.validFor(c) }) + require(preset.elements.size <= 64 && preset.keys.size <= 64 && preset.clicks.size <= 64) + require(preset.keys.map { Triple(it.key.device, it.key.keyCode, it.key.scanCode) }.distinct().size == preset.keys.size) + require(preset.physical || preset.keys.isEmpty()) + val layout = preset.layoutId?.let { id -> layouts.first { it.id == id } } + require(!preset.physical || layout == null && preset.elements.isEmpty() && preset.clicks.isEmpty()) + require(preset.elements.all { (id, action) -> layout?.elements?.firstOrNull { it.id == id }?.let { action.validFor(it) } == true }) + require(preset.clicks.all { (id, action) -> layout?.elements?.any { it.id == id && it.hasClick } == true && action.validForClick() }) + require(preset.keys.all { it.key.device.isNotBlank() && it.key.keyCode >= 0 && it.key.scanCode >= 0 && + it.action.validFor(InputControl.A) && (it.action !is InputAction.Native || it.action.target == null) }) + } + require(selections.keys.all { it == InputProfile.GAME || it == InputProfile.DESKTOP }) + for (selection in selections.values) { + require(selection.profile == InputProfiles.AUTO || profiles.any { it.id == selection.profile }) + for ((role, id) in selection.parts) { + val binding = preset(id) + require(binding.physical == (role == InputRole.PHYSICAL)) + val layout = binding.layoutId?.let { lid -> layouts.first { it.id == lid } } + require(binding.compatible(layout) && (layout == null || layout.supports(role))) + } + } + for (p in profiles) { + inputName(p.name); require(p.base == InputProfile.GAME || p.base == InputProfile.DESKTOP) + require(p.parts.keys == InputRole.entries.toSet()) + for ((role, part) in p.parts) { + val layout = part.layout?.let { id -> layouts.first { it.id == id } } + require(if (role == InputRole.PHYSICAL) layout == null else layout?.supports(role) == true) + require(preset(part.bindings).compatible(layout)) + } + } + return this + } +} + +internal fun inputName(value: String): String = value.trim().also { require(it.isNotEmpty() && it.length <= 80) } +fun newInputId(): String = UUID.randomUUID().toString() + +internal fun InputAction.validFor(element: InputSurfaceElement): Boolean = when { + this !is InputAction.Native -> validFor(element.control) + element.id.startsWith("legacy:") -> validFor(element.control) + element.kind == InputElementKind.DPAD -> target == null + target == null -> false + element.kind == InputElementKind.TRACKPAD -> target.isTrackpad + element.kind == InputElementKind.STICK -> target == InputControl.LS || target == InputControl.RS + element.kind == InputElementKind.TRIGGER -> target == InputControl.LT || target == InputControl.RT + else -> target.dimensions == 1 && target != InputControl.LT && target != InputControl.RT +} diff --git a/app/src/main/java/com/droiddeck/launcher/input/InputMotion.kt b/app/src/main/java/com/droiddeck/launcher/input/InputMotion.kt new file mode 100644 index 0000000000..9d24ca8d54 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/input/InputMotion.kt @@ -0,0 +1,35 @@ +package com.droiddeck.launcher.input + +import android.os.Handler +import android.os.Looper +import android.view.Choreographer + +class InputMotion(private val router: () -> InputRouter?) : Choreographer.FrameCallback { + private val handler = Handler(Looper.getMainLooper()) + private var scheduled = false + private var lastFrame = 0L + private val start = Runnable { + if (!scheduled) { + scheduled = true + lastFrame = System.nanoTime() + Choreographer.getInstance().postFrameCallback(this) + } + } + + fun request() { + if (Looper.myLooper() == Looper.getMainLooper()) start.run() else handler.post(start) + } + + override fun doFrame(frameTimeNanos: Long) { + val dt = (frameTimeNanos - lastFrame).coerceAtLeast(0) / 1_000_000_000f + lastFrame = frameTimeNanos + if (router()?.tick(dt) == true) Choreographer.getInstance().postFrameCallback(this) + else scheduled = false + } + + fun stop() { + handler.removeCallbacks(start) + Choreographer.getInstance().removeFrameCallback(this) + scheduled = false + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/input/InputProfile.kt b/app/src/main/java/com/droiddeck/launcher/input/InputProfile.kt new file mode 100644 index 0000000000..2375cf34e8 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/input/InputProfile.kt @@ -0,0 +1,107 @@ +package com.droiddeck.launcher.input + +enum class InputRole { PHYSICAL, PRIMARY, SECONDARY } + +enum class InputControl(val id: String, val label: String, val dimensions: Int = 1) { + A("a", "A"), B("b", "B"), X("x", "X"), Y("y", "Y"), + LB("lb", "L1"), RB("rb", "R1"), SELECT("select", "View / Select"), START("start", "Menu / Start"), + L3("l3", "L3"), R3("r3", "R3"), GUIDE("guide", "Steam / Guide"), QAM("qam", "Quick access"), + UP("up", "D-pad up"), RIGHT("right", "D-pad right"), DOWN("down", "D-pad down"), LEFT("left", "D-pad left"), + LT("lt", "L2"), RT("rt", "R2"), LS("ls", "Left stick", 2), RS("rs", "Right stick", 2), + L4("l4", "L4"), R4("r4", "R4"), L5("l5", "L5"), R5("r5", "R5"), + LEFT_PAD("left-pad", "Left trackpad", 2), RIGHT_PAD("right-pad", "Right trackpad", 2), + LEFT_CLICK("left-click", "Left trackpad click"), RIGHT_CLICK("right-click", "Right trackpad click"); + + val isPad: Boolean get() = ordinal <= RS.ordinal + val isTrackpad: Boolean get() = this == LEFT_PAD || this == RIGHT_PAD + + companion object { + val padControls = entries.filter { it.isPad }.toTypedArray() + fun fromId(id: String) = entries.firstOrNull { it.id == id } + } +} + +enum class InputCommand { MENU, KEYBOARD, STEAM, QUICK_ACCESS } + +sealed interface InputAction { + data class Native(val target: InputControl? = null) : InputAction + data object Disabled : InputAction + data class Keys(val codes: List) : InputAction + data class MouseButton(val code: Int) : InputAction + data class Command(val command: InputCommand) : InputAction + data class Directions(val up: Int, val right: Int, val down: Int, val left: Int, val diagonals: List? = null) : InputAction + data class Pointer(val speed: Float = 900f) : InputAction + data class Scroll(val speed: Float = 10f, val horizontal: Boolean = false) : InputAction +} + +data class InputProfile( + val id: String, + val bindings: Map> = emptyMap(), + val keys: List = emptyList(), + val elements: Map> = emptyMap(), + val clicks: Map> = emptyMap(), +) { + fun action(role: InputRole, control: InputControl): InputAction = + bindings[role]?.get(control) ?: InputAction.Native() + + fun withBinding(role: InputRole, control: InputControl, action: InputAction) = + copy(bindings = bindings + (role to (bindings[role].orEmpty() + (control to action)))) + + fun elementAction(role: InputRole, id: String, control: InputControl): InputAction = + elements[role]?.get(id) ?: if (id == "legacy:${control.id}") action(role, control) else InputAction.Disabled + + fun elementClickAction(role: InputRole, element: InputSurfaceElement): InputAction = clicks[role]?.get(element.id) + ?: defaultElementClick(element, elementAction(role, element.id, element.control)) { action(role, it) } + + companion object { + const val GAME = "game" + const val DESKTOP = "desktop" + + fun defaults(id: String): InputProfile { + if (id != DESKTOP) return InputProfile(GAME) + val desktop = mapOf( + InputControl.LS to InputAction.Pointer(), InputControl.RS to InputAction.Scroll(), + InputControl.A to InputAction.MouseButton(272), InputControl.B to InputAction.MouseButton(273), + InputControl.X to InputAction.Keys(listOf(28)), InputControl.Y to InputAction.Keys(listOf(1)), + InputControl.UP to InputAction.Keys(listOf(103)), InputControl.RIGHT to InputAction.Keys(listOf(106)), + InputControl.DOWN to InputAction.Keys(listOf(108)), InputControl.LEFT to InputAction.Keys(listOf(105)), + InputControl.LT to InputAction.MouseButton(273), InputControl.RT to InputAction.MouseButton(272), + InputControl.LB to InputAction.Keys(listOf(29)), InputControl.RB to InputAction.Keys(listOf(56)), + InputControl.SELECT to InputAction.Command(InputCommand.KEYBOARD), + InputControl.START to InputAction.Command(InputCommand.MENU), + InputControl.LEFT_PAD to InputAction.Scroll(), InputControl.RIGHT_PAD to InputAction.Pointer(), + InputControl.LEFT_CLICK to InputAction.MouseButton(273), InputControl.RIGHT_CLICK to InputAction.MouseButton(272), + ) + return InputProfile(DESKTOP, InputRole.entries.associateWith { desktop }) + } + } +} + +internal fun defaultElementClick(element: InputSurfaceElement, action: InputAction, legacy: (InputControl) -> InputAction): InputAction { + fun click(control: InputControl) = when (control) { + InputControl.LS -> InputControl.L3; InputControl.RS -> InputControl.R3 + InputControl.LEFT_PAD -> InputControl.LEFT_CLICK; InputControl.RIGHT_PAD -> InputControl.RIGHT_CLICK + else -> null + } + if (!element.hasClick) return InputAction.Disabled + if (element.id == "legacy:${element.control.id}") { + val target = click(element.control) ?: return InputAction.Disabled + return legacy(target).let { if (it is InputAction.Native && it.target == null) InputAction.Native(target) else it } + } + return (action as? InputAction.Native)?.let { click(it.target ?: element.control) }?.let { InputAction.Native(it) } ?: InputAction.Disabled +} + +internal fun InputAction.validForClick(): Boolean = if (this is InputAction.Native) + target != null && target.dimensions == 1 && target != InputControl.LT && target != InputControl.RT +else validFor(InputControl.A) + +internal fun InputAction.validFor(control: InputControl): Boolean = when (this) { + is InputAction.Native -> target == null || (control.isPad == target.isPad && control.dimensions == target.dimensions && + (!control.isTrackpad && !target.isTrackpad || control.isTrackpad && target.isTrackpad)) + is InputAction.Keys -> codes.isNotEmpty() && codes.size <= 8 && codes.distinct().size == codes.size && codes.all { it in 1..255 } + is InputAction.MouseButton -> code in 272..276 + is InputAction.Directions -> control.dimensions == 2 && listOf(up, right, down, left).all { it in 0..255 } && (diagonals == null || diagonals.size == 4 && diagonals.all { it in 0..255 }) + is InputAction.Pointer -> control.dimensions == 2 && speed.isFinite() && speed in 100f..3000f + is InputAction.Scroll -> control.dimensions == 2 && speed.isFinite() && speed in 1f..60f + else -> true +} diff --git a/app/src/main/java/com/droiddeck/launcher/input/InputProfiles.kt b/app/src/main/java/com/droiddeck/launcher/input/InputProfiles.kt new file mode 100644 index 0000000000..16d41bfd0b --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/input/InputProfiles.kt @@ -0,0 +1,139 @@ +package com.droiddeck.launcher.input + +import android.content.Context +import org.json.JSONArray +import org.json.JSONObject + +object InputProfiles { + const val AUTO = "auto" + private const val VERSION = 1 + + private fun prefs(context: Context) = context.getSharedPreferences("input-profiles", Context.MODE_PRIVATE) + + fun selection(context: Context): String = InputLibraries.read(context).selected + + fun select(context: Context, id: String) { + InputLibraries.update(context) { library -> + require(id == AUTO || library.profiles.any { it.id == id }) + library.copy(selected = id, selections = emptyMap()) + } + } + + fun active(context: Context, desktop: Boolean): InputProfile { + val library = InputLibraries.read(context) + return library.compileActive(desktop) + } + + fun read(context: Context, id: String): InputProfile { + return InputLibraries.read(context).compile(id) + } + + internal fun readLegacy(context: Context, id: String): InputProfile { + migrate(context) + var result = InputProfile.defaults(id) + val stored = prefs(context).getString("profile.$id", null) ?: return result + val document = runCatching { JSONObject(stored) }.getOrNull() ?: return result + if (document.optInt("version") != VERSION) return result + val bindings = document.optJSONArray("bindings") ?: return result + for (i in 0 until minOf(bindings.length(), 256)) { + val entry = bindings.optJSONObject(i) ?: continue + val role = runCatching { InputRole.valueOf(entry.getString("role")) }.getOrNull() ?: continue + val control = InputControl.fromId(entry.optString("control")) ?: continue + val action = decode(entry.optJSONObject("action") ?: continue) ?: continue + if (action.validFor(control)) result = result.withBinding(role, control, action) + } + val keys = document.optJSONArray("keys") + if (keys != null) result = result.copy(keys = (0 until minOf(keys.length(), 64)).mapNotNull { i -> + runCatching { + val item = keys.getJSONObject(i) + val action = decode(item.getJSONObject("action")) ?: return@mapNotNull null + if (!action.validFor(InputControl.A) || action is InputAction.Native && action.target != null) return@mapNotNull null + val key = PhysicalInputKey(item.getString("device"), item.getString("name"), item.getInt("keyCode"), item.getInt("scanCode")) + if (key.device.isBlank() || key.keyCode < 0 || key.scanCode < 0) return@mapNotNull null + PhysicalInputBinding(key, action) + }.getOrNull() + }.distinctBy { Triple(it.key.device, it.key.keyCode, it.key.scanCode) }) + return result + } + + fun save(context: Context, profile: InputProfile) { + InputLibraries.update(context) { library -> + val definition = library.profile(profile.id) + var updated = library + for (role in InputRole.entries) { + val preset = updated.preset(definition.binding(role)) + updated = updated.withPreset(preset.copy(controls = profile.bindings[role].orEmpty(), + elements = profile.elements[role].orEmpty(), keys = if (role == InputRole.PHYSICAL) profile.keys else emptyList(), + clicks = profile.clicks[role].orEmpty())) + } + updated + } + } + + private fun saveLegacy(context: Context, profile: InputProfile) { + val bindings = JSONArray() + for ((role, controls) in profile.bindings) for ((control, action) in controls) { + require(action.validFor(control)) + bindings.put(JSONObject().put("role", role.name).put("control", control.id).put("action", encode(action))) + } + val keys = JSONArray() + for (binding in profile.keys) keys.put(JSONObject().put("device", binding.key.device).put("name", binding.key.name) + .put("keyCode", binding.key.keyCode).put("scanCode", binding.key.scanCode).put("action", encode(binding.action))) + prefs(context).edit().putString("profile.${profile.id}", JSONObject() + .put("version", VERSION).put("id", profile.id).put("bindings", bindings).put("keys", keys).toString()).apply() + } + + fun reset(context: Context, id: String) = InputLibraries.update(context) { it.resetProfile(id) }.let { } + + fun resetAll(context: Context) { + prefs(context).edit().clear().putBoolean("migrated", true).apply() + InputSurfaceLayout.resetAll(context) + } + + private fun migrate(context: Context) { + val p = prefs(context) + if (p.getBoolean("migrated", false)) return + var game = InputProfile.defaults(InputProfile.GAME) + val old = ControllerPrefs.read(context) + for ((id, target) in old.mapping) { + val source = InputControl.fromId(id) ?: continue + val action = if (target == ControllerPrefs.OFF) InputAction.Disabled + else InputAction.Native(InputControl.fromId(target) ?: continue) + if (target != id) game = game.withBinding(InputRole.PRIMARY, source, action) + } + if (old.selectSteam) game = game.withBinding(InputRole.PHYSICAL, InputControl.SELECT, InputAction.Native(InputControl.GUIDE)) + if (!p.contains("profile.${InputProfile.GAME}")) saveLegacy(context, game) + if (old.selectSteam && !p.contains("profile.${InputProfile.DESKTOP}")) { + saveLegacy(context, InputProfile.defaults(InputProfile.DESKTOP) + .withBinding(InputRole.PHYSICAL, InputControl.SELECT, InputAction.Native(InputControl.GUIDE))) + } + p.edit().putBoolean("migrated", true).apply() + } + + internal fun encode(action: InputAction): JSONObject = JSONObject().apply { + when (action) { + is InputAction.Native -> { put("type", "native"); action.target?.let { put("target", it.id) } } + InputAction.Disabled -> put("type", "disabled") + is InputAction.Keys -> { put("type", "keys"); put("codes", JSONArray(action.codes)) } + is InputAction.MouseButton -> { put("type", "button"); put("code", action.code) } + is InputAction.Command -> { put("type", "command"); put("command", action.command.name) } + is InputAction.Directions -> { put("type", "directions"); put("codes", JSONArray(listOf(action.up, action.right, action.down, action.left))); action.diagonals?.let { put("diagonals", JSONArray(it)) } } + is InputAction.Pointer -> { put("type", "pointer"); put("speed", action.speed.toDouble()) } + is InputAction.Scroll -> { put("type", "scroll"); put("speed", action.speed.toDouble()); put("horizontal", action.horizontal) } + } + } + + internal fun decode(json: JSONObject): InputAction? = runCatching { + when (json.getString("type")) { + "native" -> InputAction.Native(if (json.has("target")) InputControl.fromId(json.getString("target")) ?: return null else null) + "disabled" -> InputAction.Disabled + "keys" -> InputAction.Keys(json.getJSONArray("codes").let { a -> (0 until a.length()).map { a.getInt(it) } }) + "button" -> InputAction.MouseButton(json.getInt("code")) + "command" -> InputAction.Command(InputCommand.valueOf(json.getString("command"))) + "directions" -> json.getJSONArray("codes").let { InputAction.Directions(it.getInt(0), it.getInt(1), it.getInt(2), it.getInt(3), json.optJSONArray("diagonals")?.let { d -> (0 until d.length()).map { d.getInt(it) } }) } + "pointer" -> InputAction.Pointer(json.getDouble("speed").toFloat()) + "scroll" -> InputAction.Scroll(json.getDouble("speed").toFloat(), json.optBoolean("horizontal", false)) + else -> null + } + }.getOrNull() +} diff --git a/app/src/main/java/com/droiddeck/launcher/input/InputRouter.kt b/app/src/main/java/com/droiddeck/launcher/input/InputRouter.kt new file mode 100644 index 0000000000..24b45ed1ef --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/input/InputRouter.kt @@ -0,0 +1,360 @@ +package com.droiddeck.launcher.input + +import kotlin.math.abs + +interface InputOutput { + fun key(code: Int, down: Boolean) + fun button(code: Int, down: Boolean) + fun pointer(dx: Float, dy: Float) + fun scroll(steps: Int) + fun scrollHorizontal(steps: Int) {} + fun command(command: InputCommand) + fun requestMotion() +} + +class InputRouter(private val output: InputOutput) { + private val keys = IntArray(256) + private val buttons = IntArray(5) + private val channels = ArrayList() + private val pads = ArrayList() + private var profile = InputProfile.defaults(InputProfile.GAME) + private var moving = false + private var wheel = 0f + private var horizontalWheel = 0f + private var enabled = true + private val snapshots = ArrayList<() -> Unit>() + + @Synchronized + fun beforeProfileChange(snapshot: () -> Unit) { snapshots.add(snapshot) } + + @Synchronized + fun removeSnapshot(snapshot: () -> Unit) { snapshots.remove(snapshot) } + + @Synchronized + fun setEnabled(on: Boolean): Boolean { + if (enabled == on) return false + if (!on) releaseAll() + enabled = on + for (pad in pads) pad.refreshIdentity() + return true + } + + @Synchronized + fun setProfile(next: InputProfile) { + if (profile == next) return + for (snapshot in snapshots) snapshot() + for (pad in pads) pad.snapshot() + profile = next + for (channel in channels) channel.configure(channel.actionFrom(next)) + for (pad in pads) pad.refreshIdentity() + wheel = 0f + horizontalWheel = 0f + moving = false + } + + @JvmOverloads + @Synchronized + fun pad(role: InputRole, retained: PadState? = null): PadSource = PadSource(role, retained).also { pads.add(it) } + + @Synchronized + fun channel(role: InputRole, control: InputControl, fixedAction: InputAction? = null, element: InputSurfaceElement? = null, elementClick: Boolean = false): Channel = Channel(role, control, fixedAction, element, elementClick).also { + channels.add(it) + it.configure(it.actionFrom(profile)) + } + + @Synchronized + fun releaseAll() { + for (snapshot in snapshots) snapshot() + for (pad in pads) pad.snapshot() + for (channel in channels) channel.suspend() + for (pad in pads) pad.refreshIdentity() + wheel = 0f + horizontalWheel = 0f + moving = false + } + + @Synchronized + fun tick(seconds: Float): Boolean { + if (!enabled) return false + var x = 0f + var y = 0f + var scroll = 0f + var horizontalScroll = 0f + for (channel in channels) { + if (channel.blocked || channel.control.isTrackpad) continue + val cx = if (abs(channel.x) > 0.12f) channel.x else 0f + val cy = if (abs(channel.y) > 0.12f) channel.y else 0f + when (val action = channel.action) { + is InputAction.Pointer -> { x += cx * action.speed; y += cy * action.speed } + is InputAction.Scroll -> if (action.horizontal) horizontalScroll += cx * action.speed else scroll += cy * action.speed + else -> Unit + } + } + val dt = seconds.coerceIn(0f, 0.05f) + if (x != 0f || y != 0f) output.pointer(x * dt, y * dt) + wheel += scroll * dt + horizontalWheel += horizontalScroll * dt + flushWheel() + moving = x != 0f || y != 0f || scroll != 0f || horizontalScroll != 0f + return moving + } + + private fun flushWheel() { + val steps = wheel.toInt() + if (steps != 0) { wheel -= steps; output.scroll(steps) } + val horizontalSteps = horizontalWheel.toInt() + if (horizontalSteps != 0) { horizontalWheel -= horizontalSteps; output.scrollHorizontal(horizontalSteps) } + } + + private fun key(code: Int, down: Boolean) { + val was = keys[code] + keys[code] = (was + if (down) 1 else -1).coerceAtLeast(0) + if ((was == 0) != (keys[code] == 0)) output.key(code, down) + } + + private fun button(code: Int, down: Boolean) { + val at = code - 272 + val was = buttons[at] + buttons[at] = (was + if (down) 1 else -1).coerceAtLeast(0) + if ((was == 0) != (buttons[at] == 0)) output.button(code, down) + } + + inner class Channel internal constructor(val role: InputRole, val control: InputControl, private val fixedAction: InputAction?, private val element: InputSurfaceElement?, private val elementClick: Boolean) { + var action: InputAction = InputAction.Native() + private set + internal var x = 0f + internal var y = 0f + internal var contact = false + internal var blocked = false + private var held = false + private val directions = BooleanArray(4) + private var radialKeys = emptyList() + + internal fun actionFrom(profile: InputProfile) = element?.let { if (elementClick) profile.elementClickAction(role, it) else profile.elementAction(role, it.id, control) } + ?: profile.action(role, control) + + internal fun configure(next: InputAction) { + suspend() + action = (fixedAction ?: next).takeIf { if (elementClick) it.validForClick() else if (element != null) it.validFor(element) else it.validFor(control) } + ?: if (element != null) InputAction.Disabled else InputAction.Native() + } + + internal fun observe(value: Float, second: Float = 0f, touching: Boolean = false) { + x = value; y = second; contact = touching + } + + internal fun awaitNeutral(value: Float, second: Float, touching: Boolean) = synchronized(this@InputRouter) { + observe(value, second, touching) + suspend() + } + + internal fun nativeIdentity(): Boolean = enabled && !blocked && (action as? InputAction.Native)?.target == null && action is InputAction.Native + + internal fun suspend() { + releaseOutputs() + blocked = contact || abs(x) > 0.12f || abs(y) > 0.12f + } + + fun release() = synchronized(this@InputRouter) { + releaseOutputs() + x = 0f; y = 0f; contact = false; blocked = false + } + + fun close() = synchronized(this@InputRouter) { + release() + channels.remove(this) + } + + fun acceptsPointerGesture() = synchronized(this@InputRouter) { enabled && !blocked && contact && action is InputAction.Pointer } + + fun pointerDelta(dx: Float, dy: Float) = synchronized(this@InputRouter) { + val pointer = action as? InputAction.Pointer + if (enabled && !blocked && contact && pointer != null && dx.isFinite() && dy.isFinite()) output.pointer(dx * pointer.speed, dy * pointer.speed) + } + + fun wheelSteps(steps: Int) = synchronized(this@InputRouter) { + val scroll = action as? InputAction.Scroll + if (enabled && !blocked && scroll != null && steps != 0) { + if (scroll.horizontal) output.scrollHorizontal(steps) else output.scroll(steps) + } + } + + fun update(value: Float, second: Float = 0f, touching: Boolean = false): InputControl? = synchronized(this@InputRouter) { + val oldX = x + val oldY = y + val oldContact = contact + x = value.coerceIn(-1f, 1f) + y = second.coerceIn(-1f, 1f) + contact = touching + if (!enabled) { + blocked = contact || abs(x) > 0.12f || abs(y) > 0.12f + return@synchronized null + } + if (blocked) { + if (!contact && abs(x) <= 0.12f && abs(y) <= 0.12f) blocked = false + return@synchronized null + } + when (val a = action) { + is InputAction.Native -> return@synchronized a.target ?: control + is InputAction.Disabled -> Unit + is InputAction.Pointer -> { + if (control.isTrackpad) { + if (contact && oldContact) output.pointer((x - oldX) * a.speed, -(y - oldY) * a.speed) + } else requestMotion() + } + is InputAction.Scroll -> { + if (control.isTrackpad) { + if (contact && oldContact) { + if (a.horizontal) horizontalWheel -= (x - oldX) * a.speed else wheel += (y - oldY) * a.speed + flushWheel() + } + } else requestMotion() + } + is InputAction.Directions -> { + val vertical = if (control.isTrackpad) -y else y + if (a.diagonals == null) { + direction(0, -vertical, a.up); direction(1, x, a.right) + direction(2, vertical, a.down); direction(3, -x, a.left) + } else { + val cardinal = listOf(a.up, a.right, a.down, a.left) + val sector = ((kotlin.math.round(Math.toDegrees(kotlin.math.atan2(vertical, x).toDouble()) / 45).toInt() + 2) % 8 + 8) % 8 + val next = if (kotlin.math.hypot(x, vertical) < .24f) emptyList() else if (sector % 2 == 0) listOf(cardinal[sector / 2]) + else a.diagonals[sector / 2].let { if (it != 0) listOf(it) else listOf(cardinal[sector / 2], cardinal[(sector / 2 + 1) % 4]) } + val codes = next.filter { it != 0 }.distinct() + radialKeys.filterNot { it in codes }.forEach { key(it, false) } + codes.filterNot { it in radialKeys }.forEach { key(it, true) } + radialKeys = codes + } + } + else -> { + val down = if (control.isTrackpad) contact else maxOf(abs(x), abs(y)) > if (held) 0.35f else 0.55f + if (held != down) { + held = down + when (a) { + is InputAction.Keys -> if (down) a.codes.forEach { key(it, true) } else a.codes.asReversed().forEach { key(it, false) } + is InputAction.MouseButton -> button(a.code, down) + is InputAction.Command -> if (down) output.command(a.command) + else -> Unit + } + } + } + } + null + } + + private fun requestMotion() { + if (!moving && (abs(x) > 0.12f || abs(y) > 0.12f)) { moving = true; output.requestMotion() } + } + + private fun direction(at: Int, value: Float, code: Int) { + val down = value > if (directions[at]) 0.35f else 0.55f + if (directions[at] != down) { directions[at] = down; if (code != 0) key(code, down) } + } + + private fun releaseOutputs() { + when (val a = action) { + is InputAction.Keys -> if (held) a.codes.asReversed().forEach { key(it, false) } + is InputAction.MouseButton -> if (held) button(a.code, false) + is InputAction.Directions -> { + val codes = intArrayOf(a.up, a.right, a.down, a.left) + for (i in directions.indices) if (directions[i] && codes[i] != 0) key(codes[i], false) + } + else -> Unit + } + radialKeys.forEach { key(it, false) }; radialKeys = emptyList() + held = false + directions.fill(false) + } + } + + inner class PadSource internal constructor(role: InputRole, retained: PadState?) { + private val routes = Array(InputControl.padControls.size) { channel(role, InputControl.padControls[it]) } + private val result = PadState() + private var last: PadState? = null + private var identity = true + private var nativeCandidate = true + + init { + last = retained + if (retained != null) for (route in routes) { + route.awaitNeutral(retained.value(route.control), retained.secondValue(route.control), false) + } + refreshIdentity() + } + + internal fun refreshIdentity() { + nativeCandidate = routes.all { (it.action as? InputAction.Native)?.target == null && it.action is InputAction.Native } + identity = enabled && nativeCandidate && routes.none { it.blocked } + } + + internal fun snapshot() { + val raw = last ?: return + for (route in routes) { + route.x = raw.value(route.control) + route.y = raw.secondValue(route.control) + } + } + + fun route(raw: PadState): PadState = synchronized(this@InputRouter) { + last = raw + if (identity) return@synchronized raw + result.clear() + for (route in routes) { + val x = raw.value(route.control) + val y = raw.secondValue(route.control) + val target = route.update(x, y) ?: continue + result.add(target, x, y) + } + if (nativeCandidate && enabled && routes.none { it.blocked }) identity = true + result + } + + fun release() = synchronized(this@InputRouter) { + for (route in routes) route.release() + last = null + result.clear() + refreshIdentity() + } + + fun close() = synchronized(this@InputRouter) { + release() + for (route in routes) route.close() + pads.remove(this) + } + } +} + +private val padButtons = intArrayOf(PadState.A, PadState.B, PadState.X, PadState.Y, PadState.LB, PadState.RB, + PadState.SELECT, PadState.START, PadState.L3, PadState.R3, PadState.GUIDE, PadState.QAM) + +internal fun PadState.value(control: InputControl): Float = when (control) { + InputControl.UP -> if (up) 1f else 0f + InputControl.RIGHT -> if (right) 1f else 0f + InputControl.DOWN -> if (down) 1f else 0f + InputControl.LEFT -> if (left) 1f else 0f + InputControl.LT -> leftTrigger + InputControl.RT -> rightTrigger + InputControl.LS -> leftX + InputControl.RS -> rightX + else -> if (control.ordinal < padButtons.size && isDown(padButtons[control.ordinal])) 1f else 0f +} + +internal fun PadState.secondValue(control: InputControl): Float = when (control) { + InputControl.LS -> leftY + InputControl.RS -> rightY + else -> 0f +} + +internal fun PadState.add(control: InputControl, x: Float, y: Float = 0f) { + when (control) { + InputControl.UP -> up = up || x > 0.5f + InputControl.RIGHT -> right = right || x > 0.5f + InputControl.DOWN -> down = down || x > 0.5f + InputControl.LEFT -> left = left || x > 0.5f + InputControl.LT -> leftTrigger = maxOf(leftTrigger, x) + InputControl.RT -> rightTrigger = maxOf(rightTrigger, x) + InputControl.LS -> if (x * x + y * y > leftX * leftX + leftY * leftY) { leftX = x; leftY = y } + InputControl.RS -> if (x * x + y * y > rightX * rightX + rightY * rightY) { rightX = x; rightY = y } + else -> if (control.ordinal < padButtons.size && x > 0.5f) press(padButtons[control.ordinal], true) + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/input/InputStockLayouts.kt b/app/src/main/java/com/droiddeck/launcher/input/InputStockLayouts.kt new file mode 100644 index 0000000000..3ca108c533 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/input/InputStockLayouts.kt @@ -0,0 +1,41 @@ +package com.droiddeck.launcher.input + +internal object InputStockLayouts { + const val DECK = "preset.deck" + const val TRACKPAD = "preset.trackpad" + private fun commands(indices: List): List { + 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 = listOf(InputCommand.MENU, InputCommand.KEYBOARD, InputCommand.STEAM, InputCommand.QUICK_ACCESS) + .mapIndexed { i, command -> "command:$i" to InputAction.Command(command) }.toMap() + private fun grips() = listOf(InputControl.L4, InputControl.L5, InputControl.R5, InputControl.R4).mapIndexed { i, c -> + InputSurfaceElement(c, .02f + i * .245f, .78f, .225f, .20f) + } + private fun pads() = listOf(InputSurfaceElement(InputControl.LEFT_PAD, .02f, .14f, .47f, .62f), + InputSurfaceElement(InputControl.RIGHT_PAD, .51f, .14f, .47f, .62f)) + fun layouts() = listOf( + 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), + ) + + fun actions(layout: SavedInputLayout): Map = (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 emptyMap()).filterKeys { id -> layout.elements.any { it.id == id } } + + fun clicks(layout: SavedInputLayout): Map = if (layout.id == InputLibraries.TRACKPAD) + mapOf("legacy:right-pad" to InputAction.MouseButton(272)) else emptyMap() + + fun presets(layouts: List) = 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)) + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/input/InputSurfaceLayout.kt b/app/src/main/java/com/droiddeck/launcher/input/InputSurfaceLayout.kt new file mode 100644 index 0000000000..b20acf3741 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/input/InputSurfaceLayout.kt @@ -0,0 +1,95 @@ +package com.droiddeck.launcher.input + +import android.content.Context +import org.json.JSONArray +import org.json.JSONObject + +enum class InputElementKind(val control: InputControl) { + BUTTON(InputControl.A), STICK(InputControl.LS), TRACKPAD(InputControl.LEFT_PAD), + DPAD(InputControl.LS), TRIGGER(InputControl.LT); + + companion object { + fun from(control: InputControl) = when { + control.isTrackpad -> TRACKPAD + control.dimensions == 2 -> STICK + control == InputControl.LT || control == InputControl.RT -> TRIGGER + else -> BUTTON + } + } +} + +enum class InputButtonShape { ROUNDED, PILL, CIRCLE, RECTANGLE } + +data class InputSurfaceElement( + val control: InputControl, val x: Float, val y: Float, val width: Float, val height: Float, + val id: String = "legacy:${control.id}", + val kind: InputElementKind = InputElementKind.from(control), + val name: String = control.label, + val floating: Boolean = true, + val hideWhenIdle: Boolean = false, + val mouseGestures: Boolean = false, + val toggle: Boolean = false, + val icon: Int = 0, + val image: String = "", + val opacity: Float = 1f, + val buttonShape: InputButtonShape = InputButtonShape.ROUNDED, + val showLabel: Boolean = true, +) { + val hasClick get() = kind == InputElementKind.STICK || kind == InputElementKind.TRACKPAD + + fun bounded() = copy(x = x.coerceIn(0f, 0.97f), y = y.coerceIn(0f, 0.97f), + width = width.coerceIn(0.03f, 1f - x.coerceIn(0f, 0.97f)), height = height.coerceIn(0.03f, 1f - y.coerceIn(0f, 0.97f))) +} + +object InputSurfaceLayout { + private fun prefs(context: Context) = context.getSharedPreferences("input-surfaces", Context.MODE_PRIVATE) + + fun resetAll(context: Context) { prefs(context).edit().clear().apply() } + + fun defaults() = listOf( + InputSurfaceElement(InputControl.LEFT_PAD, 0.02f, 0.02f, 0.46f, 0.65f), + InputSurfaceElement(InputControl.RIGHT_PAD, 0.52f, 0.02f, 0.46f, 0.65f), + InputSurfaceElement(InputControl.A, 0.02f, 0.72f, 0.20f, 0.25f), + InputSurfaceElement(InputControl.B, 0.26f, 0.72f, 0.20f, 0.25f), + InputSurfaceElement(InputControl.SELECT, 0.52f, 0.72f, 0.20f, 0.25f), + InputSurfaceElement(InputControl.START, 0.76f, 0.72f, 0.22f, 0.25f), + ) + + fun read(context: Context, profile: String, role: InputRole = InputRole.SECONDARY): List = + 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 { + val text = prefs(context).getString(profile, null) ?: return defaults() + return runCatching { + val doc = JSONObject(text) + require(doc.getInt("version") == 1) + val elements = doc.getJSONArray("elements") + (0 until minOf(elements.length(), 28)).mapNotNull { i -> + val item = elements.optJSONObject(i) ?: return@mapNotNull null + val control = InputControl.fromId(item.optString("control")) ?: return@mapNotNull null + val values = listOf("x", "y", "w", "h").map { item.optDouble(it, Double.NaN).toFloat() } + if (values.any { !it.isFinite() }) return@mapNotNull null + InputSurfaceElement(control, values[0], values[1], values[2], values[3]).bounded() + }.distinctBy { it.control } + }.getOrElse { defaults() } + } + + fun save(context: Context, profile: String, layout: List) { + InputLibraries.update(context) { library -> + val saved = library.layout(profile, InputRole.SECONDARY) ?: return@update library + library.withLayout(saved.copy(elements = layout.map { it.bounded() })) + } + } + + internal fun saveLegacy(context: Context, profile: String, layout: List) { + val array = JSONArray() + for (element in layout.distinctBy { it.control }.take(28)) { + val e = element.bounded() + array.put(JSONObject().put("control", e.control.id).put("x", e.x.toDouble()).put("y", e.y.toDouble()) + .put("w", e.width.toDouble()).put("h", e.height.toDouble())) + } + prefs(context).edit().putString(profile, JSONObject().put("version", 1).put("elements", array).toString()).apply() + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/input/InputSurfaceSnap.kt b/app/src/main/java/com/droiddeck/launcher/input/InputSurfaceSnap.kt new file mode 100644 index 0000000000..011a478888 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/input/InputSurfaceSnap.kt @@ -0,0 +1,24 @@ +package com.droiddeck.launcher.input + +import kotlin.math.abs + +internal data class SurfaceSnapResult(val element: InputSurfaceElement, val x: Float? = null, val y: Float? = null) + +internal fun snapSurfaceElement(element: InputSurfaceElement, others: List, thresholdX: Float, thresholdY: Float, + corner: Int = 0): SurfaceSnapResult { + fun axis(start: Float, size: Float, targets: List, threshold: Float, low: Boolean, high: Boolean): Triple { + val anchors = when { low -> listOf(0f); high -> listOf(size); else -> listOf(0f, size / 2f, size) } + val candidate = anchors.flatMap { anchor -> targets.map { target -> Triple(target - start - anchor, target, anchor) } } + .filter { abs(it.first) <= threshold }.minByOrNull { abs(it.first) } ?: return Triple(start, size, null) + val offset = candidate.first + val nextStart = if (high) start else start + offset + val nextSize = when { low -> size - offset; high -> size + offset; else -> size } + if (nextStart < 0f || nextSize < .03f || nextStart + nextSize > 1.00001f) return Triple(start, size, null) + return Triple(nextStart.coerceAtLeast(0f), nextSize, candidate.second) + } + val xTargets = listOf(0f, .5f, 1f) + others.flatMap { listOf(it.x, it.x + it.width / 2f, it.x + it.width) } + val yTargets = listOf(0f, .5f, 1f) + others.flatMap { listOf(it.y, it.y + it.height / 2f, it.y + it.height) } + val x = axis(element.x, element.width, xTargets, thresholdX, corner and 1 != 0, corner and 2 != 0) + val y = axis(element.y, element.height, yTargets, thresholdY, corner and 4 != 0, corner and 8 != 0) + return SurfaceSnapResult(element.copy(x = x.first, y = y.first, width = x.second, height = y.second).bounded(), x.third, y.third) +} diff --git a/app/src/main/java/com/droiddeck/launcher/input/InputSurfaceSource.kt b/app/src/main/java/com/droiddeck/launcher/input/InputSurfaceSource.kt new file mode 100644 index 0000000000..80c869a641 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/input/InputSurfaceSource.kt @@ -0,0 +1,117 @@ +package com.droiddeck.launcher.input + +class InputSurfaceSource(private val pad: PadBridge, private val role: InputRole, val element: InputSurfaceElement) { + private val router = pad.inputRouter + private val route = router?.channel(role, element.control, element = element) + private val deck = DeckControls.source(role, routed = true) + private val clickDeck = DeckControls.source(role, routed = true) + private val clickOwner = Any() + private val clickRoute = if (element.hasClick) router?.channel(role, InputControl.A, element = element, elementClick = true) else null + private val gestureWheel = if (element.mouseGestures) router?.channel(role, InputControl.RIGHT_PAD, fixedAction = InputAction.Scroll()) else null + private val gestureRight = if (element.mouseGestures) router?.channel(role, InputControl.A, fixedAction = InputAction.MouseButton(273)) else null + private val gestureMiddle = if (element.mouseGestures) router?.channel(role, InputControl.A, fixedAction = InputAction.MouseButton(274)) else null + val mouseGestures get() = element.mouseGestures && route?.action is InputAction.Pointer + + fun gestureContact(touching: Boolean) { if (!closed) route?.update(0f, 0f, touching) } + fun pointerDelta(dx: Float, dy: Float) { + if (closed) return + val timing = InputTiming.begin() + route?.pointerDelta(dx, dy) + InputTiming.end(timing) + } + fun gestureWheel(steps: Int) { if (!closed && route?.acceptsPointerGesture() == true) gestureWheel?.wheelSteps(steps) } + fun gestureButton(code: Int, down: Boolean) { + if (closed || down && route?.acceptsPointerGesture() != true) return + when (code) { + 272 -> click(down) + 273 -> gestureRight?.update(if (down) 1f else 0f) + 274 -> gestureMiddle?.update(if (down) 1f else 0f) + } + } + + private var nativePad = false + private var nativeDeck = false + private var clickPad = false + private var clickDeckActive = false + private var closed = false + private var fingerDown = false + private var latchBlocked = false + var latched = false + private set + var onStateChanged: () -> Unit = {} + private fun resetLatch() { latched = false; latchBlocked = fingerDown; onStateChanged() } + private val snapshot: () -> Unit = { releaseNative(); resetLatch(); if (element.toggle) route?.release() } + + init { router?.beforeProfileChange(snapshot) } + + fun update(value: Float, y: Float = 0f, touching: Boolean = false) { + if (closed) return + val timing = InputTiming.begin() + val x = if (element.toggle && route?.action !is InputAction.Command && route?.action !is InputAction.Disabled) { + val down = value > .5f + if (!down) latchBlocked = false + if (down && !fingerDown && !latchBlocked) latched = !latched + fingerDown = down + if (latched) 1f else 0f + } else value + val target = if (router == null) element.control else route?.update(x, y, touching) + val toPad = target?.isPad == true + val toDeck = target != null && !target.isPad + if (toPad || nativePad) writePad(this, target) { state -> + state.clear() + if (toPad) { + if (element.kind == InputElementKind.DPAD) { + state.up = y < -.5f; state.down = y > .5f; state.left = x < -.5f; state.right = x > .5f + } else state.add(target, x, y) + } + } + if (toDeck || nativeDeck) deck.native(if (toDeck) target else null, x, y, touching) + nativePad = toPad; nativeDeck = toDeck + if (element.toggle) onStateChanged() + InputTiming.end(timing) + } + + fun click(down: Boolean) { + if (closed) return + val timing = InputTiming.begin() + val target = if (router == null) when (element.control) { InputControl.LS -> InputControl.L3; InputControl.RS -> InputControl.R3; else -> null } else clickRoute?.update(if (down) 1f else 0f) + val toPad = target?.isPad == true + val toDeck = target != null && !target.isPad + if (toPad || clickPad) writePad(clickOwner, target) { state -> + state.clear() + if (toPad && down) state.add(target, 1f) + } + if (toDeck || clickDeckActive) clickDeck.native(if (toDeck) target else null, if (down) 1f else 0f, 0f, false) + clickPad = toPad; clickDeckActive = toDeck + InputTiming.end(timing) + } + + private fun writePad(owner: Any, target: InputControl?, mutation: (PadState) -> Unit) { + if (router != null) pad.applyRoutedTouch(owner, role, mutation) + else pad.applyTouch(owner, role, when (target) { InputControl.LS -> PadBridge.LEFT_STICK; InputControl.RS -> PadBridge.RIGHT_STICK; else -> 0 }, mutation) + } + + private fun releaseNative() { + if (nativePad) pad.releaseTouch(this) + if (nativeDeck) deck.release() + if (clickPad) pad.releaseTouch(clickOwner) + if (clickDeckActive) clickDeck.release() + clickPad = false; clickDeckActive = false + nativePad = false; nativeDeck = false + } + + fun close() { + if (closed) return + closed = true + router?.removeSnapshot(snapshot) + releaseNative(); resetLatch() + route?.close(); clickRoute?.close(); gestureWheel?.close(); gestureRight?.close(); gestureMiddle?.close(); deck.close(); clickDeck.close() + } + + fun release() { + if (closed) return + releaseNative(); resetLatch() + route?.release(); clickRoute?.release(); gestureWheel?.release(); gestureRight?.release(); gestureMiddle?.release() + } + +} diff --git a/app/src/main/java/com/droiddeck/launcher/input/InputTiming.kt b/app/src/main/java/com/droiddeck/launcher/input/InputTiming.kt new file mode 100644 index 0000000000..0d2589c42f --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/input/InputTiming.kt @@ -0,0 +1,33 @@ +package com.droiddeck.launcher.input + +import java.util.Locale + +object InputTiming { + @JvmField @Volatile var enabled = false + private val samples = LongArray(4096) + private var count = 0L + private var next = 0 + + @JvmStatic fun begin(): Long = if (enabled) System.nanoTime() else 0L + + @JvmStatic fun end(start: Long) { + if (start == 0L) return + val elapsed = System.nanoTime() - start + synchronized(this) { + samples[next] = elapsed + next = (next + 1) % samples.size + count++ + } + } + + @Synchronized fun reset() { count = 0; next = 0; samples.fill(0) } + + @Synchronized fun report(): String { + val size = minOf(count, samples.size.toLong()).toInt() + if (size == 0) return "No input samples" + val values = samples.copyOf(size).sortedArray() + fun percentile(p: Double) = values[((size - 1) * p).toInt()] / 1000.0 + return String.format(Locale.ROOT, "Dispatch to publication (µs): p50 %.1f, p95 %.1f, p99 %.1f; %d samples, last %d retained", + percentile(.5), percentile(.95), percentile(.99), count, size) + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/input/LegacyGamepadLayout.kt b/app/src/main/java/com/droiddeck/launcher/input/LegacyGamepadLayout.kt new file mode 100644 index 0000000000..4525fa9d9c --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/input/LegacyGamepadLayout.kt @@ -0,0 +1,207 @@ +package com.droiddeck.launcher.input + +import android.content.Context +import android.graphics.Rect +import kotlin.math.* + +internal class LegacyGamepadLayout(private val context: Context, private val width: Int, private val height: Int) { + private val resources get() = context.resources + private val settings = ControllerPrefs.read(context) + private val safe = Rect() + private var fit = 1f + private class Control(val id: String, val group: String, val stick: Int, val radiusDp: Float, val offsetXDp: Float, val offsetYDp: Float) { + var radius = 0f + var cx = 0f + var cy = 0f + val halfW get() = if (id in shoulderIds) radius * SHOULDER_WIDTH else radius + val halfH get() = if (id in shoulderIds) radius * SHOULDER_HEIGHT else radius + } + private val controls = listOf( + Control("up", "dpad", -1, 26f, 0f, -52f), + Control("right", "dpad", -1, 26f, 52f, 0f), + Control("down", "dpad", -1, 26f, 0f, 52f), + Control("left", "dpad", -1, 26f, -52f, 0f), + Control("ls", "ls", 0, 48f, 0f, 0f), + Control("rs", "rs", 1, 48f, 0f, 0f), + Control("a", "face", -1, 30f, 0f, 50f), + Control("b", "face", -1, 30f, 50f, 0f), + Control("x", "face", -1, 30f, -50f, 0f), + Control("y", "face", -1, 30f, 0f, -50f), + Control("lb", "lb", -1, 24f, 0f, 0f), + Control("rb", "rb", -1, 24f, 0f, 0f), + Control("lt", "lt", -1, 24f, 0f, 0f), + Control("rt", "rt", -1, 24f, 0f, 0f), + Control("select", "select", -1, 20f, 0f, 0f), + Control("start", "start", -1, 20f, 0f, 0f), + Control("guide", "guide", -1, 22f, 0f, 0f), + Control("qam", "qam", -1, 22f, 0f, 0f), + ) + + private val groups = controls.map { it.group }.distinct() + + private fun dp(value: Float) = value * resources.displayMetrics.density + + private fun scaled(value: Float) = dp(value) * settings.size / 100f * fit + + private fun fitScale(): Float { + val metrics = resources.displayMetrics + val heightMm = (height - safe.top - safe.bottom) / pxPerMm(metrics.ydpi) + val faceMm = dp(60f) / pxPerMm(metrics.xdpi) + val floor = (MIN_FACE_MM / faceMm).coerceAtMost(1f) + return (heightMm / FULL_SIZE_HEIGHT_MM).coerceIn(floor, 1f) + } + + private fun pxPerMm(dpi: Float): Float { + val nominal = resources.displayMetrics.densityDpi.toFloat() + return (if (dpi > nominal * 0.6f && dpi < nominal * 1.6f) dpi else nominal) / 25.4f + } + + private fun placeAuto(w: Float, h: Float) { + val metrics = resources.displayMetrics + val mmX = pxPerMm(metrics.xdpi) + val mmY = pxPerMm(metrics.ydpi) + val usableW = w - safe.left - safe.right + val usableH = h - safe.top - safe.bottom + val inboard = (0.17f * usableW / mmX).coerceIn(24f, 40f) * mmX + val lift = (0.285f * usableH / mmY).coerceIn(22f, 45f) * mmY + val gap = dp(12f) + val floor = BOTTOM_MARGIN_MM * mmY + val drop = max(drop("dpad", "ls", gap), drop("rs", "face", gap)) + val restY = min(h - safe.bottom - lift, h - safe.bottom - floor - drop) + put("ls", safe.left + inboard, restY) + put("face", w - safe.right - inboard, restY) + besideBelow("dpad", "ls", 1f, gap, floor) + besideBelow("rs", "face", -1f, gap, floor) + val phone = usableH / mmY < PHONE_MAX_HEIGHT_MM + val shoulderY = if (phone) safe.top + floor + halfH("lb") + else min(centre("ls").second - extent("ls"), centre("face").second - extent("face")) - gap - halfH("lb") + shoulders("ls", "lb", "lt", 1f, gap, shoulderY) + shoulders("face", "rb", "rt", -1f, gap, shoulderY) + val bottom = h - safe.bottom - floor - extent("guide") + val spread = extent("guide") + gap / 2f + put("guide", w / 2f - spread, bottom) + put("qam", w / 2f + spread, bottom) + val inner = max(centre("dpad").first + extent("dpad"), w - centre("rs").first + extent("rs")) + gap + extent("select") + put("select", inner, restY) + put("start", w - inner, restY) + if (phone) { + val top = max(centre("lb").first + extent("lb"), w - centre("rb").first + extent("rb")) + gap * 2.5f + extent("select") + put("select", top, shoulderY) + put("start", w - top, shoulderY) + } + if (crowded("select", gap) || crowded("start", gap)) { + above("select", "dpad", gap) + val (sx, sy) = centre("select") + put("start", w - sx, sy) + if (crowded("start", gap)) above("start", "rs", gap) + } + if (crowded("guide", gap, gap, "qam") || crowded("qam", gap, gap, "guide")) { + val row = centre("select").second + put("guide", w / 2f - spread, row) + put("qam", w / 2f + spread, row) + if (crowded("guide", gap, gap, "qam") || crowded("qam", gap, gap, "guide")) { + val top = safe.top + dp(8f) + extent("guide") + put("guide", w / 2f - spread, top) + put("qam", w / 2f + spread, top) + } + } + } + + private fun drop(group: String, anchor: String, gap: Float): Float = + (extent(anchor) + extent(group) + gap) * sin(Math.toRadians(DROP_DEGREES)).toFloat() + extent(group) + + private fun besideBelow(group: String, anchor: String, inward: Float, gap: Float, margin: Float) { + val (ax, ay) = centre(anchor) + val reach = extent(anchor) + extent(group) + gap + val floor = height - safe.bottom - margin - extent(group) + val dy = min(reach * sin(Math.toRadians(DROP_DEGREES)).toFloat(), floor - ay) + val dx = sqrt(max(0f, reach * reach - dy * dy)) + put(group, ax + inward * dx, ay + dy) + } + + private fun shoulders(anchor: String, bumper: String, trigger: String, inward: Float, gap: Float, y: Float) { + val (ax, _) = centre(anchor) + val spread = extent(bumper) + gap / 2f + put(bumper, ax + inward * spread, y) + put(trigger, ax - inward * spread, y) + } + + private fun above(group: String, anchor: String, gap: Float) { + val (ax, ay) = centre(anchor) + put(group, ax, ay - extent(anchor) - gap - extent(group)) + } + + private fun crowded(group: String, gap: Float, space: Float = gap / 2f, except: String? = null): Boolean { + val (x, y) = centre(group) + return groups.any { other -> + if (other == group || other == except) return@any false + val (ox, oy) = centre(other) + val dx = x - ox + val dy = y - oy + sqrt(dx * dx + dy * dy) < extent(group) + extent(other) + space + } + } + + private fun members(group: String) = controls.filter { it.group == group } + + private fun centre(group: String): Pair { + val first = members(group).first() + return (first.cx - scaled(first.offsetXDp)) to (first.cy - scaled(first.offsetYDp)) + } + + private fun extent(group: String): Float = members(group).maxOf { control -> + sqrt(scaled(control.offsetXDp).let { it * it } + scaled(control.offsetYDp).let { it * it }) + control.halfW + } + + private fun halfH(group: String): Float = members(group).maxOf { abs(scaled(it.offsetYDp)) + it.halfH } + + private fun put(group: String, x: Float, y: Float) { + for (control in members(group)) { + control.cx = x + scaled(control.offsetXDp) + control.cy = y + scaled(control.offsetYDp) + } + } + + private fun clamp(group: String) { + val list = members(group) + val margin = dp(4f) + val minX = list.minOf { it.cx - it.halfW } - safe.left - margin + val maxX = list.maxOf { it.cx + it.halfW } - (width - safe.right - margin) + val minY = list.minOf { it.cy - it.halfH } - safe.top - margin + val maxY = list.maxOf { it.cy + it.halfH } - (height - safe.bottom - margin) + val dx = if (minX < 0) -minX else if (maxX > 0) -maxX else 0f + val dy = if (minY < 0) -minY else if (maxY > 0) -maxY else 0f + if (dx == 0f && dy == 0f) return + for (control in list) { control.cx += dx; control.cy += dy } + } + + + fun elements(): List { + fit = fitScale() + controls.forEach { it.radius = scaled(it.radiusDp) } + placeAuto(width.toFloat(), height.toFloat()) + for ((group, position) in ControllerPrefs.layout(context, width, height)) { + if (group in groups && position.first.isFinite() && position.second.isFinite()) put(group, position.first * width, position.second * height) + } + groups.forEach(::clamp) + val result = controls.filter { + settings.mapping[it.id] != ControllerPrefs.OFF && (it.id != "guide" || settings.steamButton) && (it.id != "qam" || settings.qamButton) + }.map { c -> + InputSurfaceElement(requireNotNull(InputControl.fromId(c.id)), (c.cx - c.halfW) / width, (c.cy - c.halfH) / height, + 2 * c.halfW / width, 2 * c.halfH / height, floating = settings.adaptiveSticks, hideWhenIdle = c.stick >= 0 && settings.adaptiveSticks).bounded() + }.toMutableList() + if (settings.keyboardButton) result += InputSurfaceElement(InputControl.A, .02f, .9f, .12f, .08f, + "builtin:keyboard", name = "Keyboard") + return result + } + private companion object { + const val MIN_FACE_MM = 9.5f + const val FULL_SIZE_HEIGHT_MM = 85f + const val BOTTOM_MARGIN_MM = 5f + const val DROP_DEGREES = 35.0 + const val PHONE_MAX_HEIGHT_MM = 90f + const val SHOULDER_WIDTH = 1.45f + const val SHOULDER_HEIGHT = 0.8f + val shoulderIds = setOf("lb", "rb", "lt", "rt") + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/input/LegacyScreenKeyMigration.kt b/app/src/main/java/com/droiddeck/launcher/input/LegacyScreenKeyMigration.kt new file mode 100644 index 0000000000..fed7b35fe9 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/input/LegacyScreenKeyMigration.kt @@ -0,0 +1,82 @@ +package com.droiddeck.launcher.input + +import android.content.Context +import android.hardware.display.DisplayManager +import android.util.DisplayMetrics +import android.view.Display +import org.json.JSONObject + +internal object LegacyScreenKeyMigration { + const val BACKUP = "legacy-screen-keys-backup" + + fun snapshot(context: Context): String? { + val values = ControllerPrefs.prefs(context).all.filterKeys { it == "screenKeys" || it == "screenKeysShown" || it.startsWith("screenLayout.") } + return values.takeIf { it.isNotEmpty() }?.let { JSONObject(it).toString() } + } + + fun clean(context: Context) { + val prefs = ControllerPrefs.prefs(context) + prefs.edit().apply { prefs.all.keys.filter { it == "screenKeys" || it == "screenKeysShown" || it.startsWith("screenLayout.") }.forEach(::remove) }.apply() + } + + @Suppress("DEPRECATION") + fun migrate(context: Context, library: InputLibrary, raw: String): InputLibrary { + val old = JSONObject(raw) + val display = context.getSystemService(DisplayManager::class.java)?.getDisplay(Display.DEFAULT_DISPLAY) + val metrics = DisplayMetrics().also { if (display != null) display.getRealMetrics(it) else it.setTo(context.resources.displayMetrics) } + val settings = ControllerPrefs.read(context) + val style = InputSurfaceStyle(settings.tint, settings.opacity) + val sets = buildList { + old.optString("screenKeys").takeIf { it.isNotBlank() }?.let { add("Screen keys (imported)" to it) } + old.keys().asSequence().filter { it.startsWith("screenLayout.") }.sorted().forEach { key -> + add((key.removePrefix("screenLayout.").ifBlank { "Screen keys" } + " (imported)").take(80) to old.getString(key)) + } + } + var result = library + for ((index, set) in sets.withIndex()) { + val id = "migrated.screenkeys.$index" + if (result.profiles.any { it.id == id }) continue + val converted = convert(context, set.second, metrics.widthPixels, metrics.heightPixels, metrics.density) + if (converted.isEmpty()) continue + val layout = SavedInputLayout("$id.layout", set.first, converted.map { it.first }, style = style, screen = InputLayoutScreen.MAIN) + val bindings = SavedInputBindings("$id.bindings", set.first, false, layout.id, elements = converted.associate { it.first.id to it.second }) + val base = library.profile(library.steam) + val profile = SavedInputProfile(id, set.first, base.base, base.parts + (InputRole.PRIMARY to InputPart(bindings.id, layout.id))) + result = result.withLayout(layout).withPreset(bindings).withProfile(profile) + } + return result.validate() + } + + internal fun convert(context: Context, raw: String, width: Int, height: Int, density: Float): List> = + raw.split(';').filter { it.isNotBlank() }.mapIndexed { index, entry -> + val parts = entry.split(':') + require(parts.size >= 2) { "Invalid legacy screen key" } + val codes = parts[0].split('+').map { it.toInt() } + require(codes.all { it in 0..255 }) + val position = parts[1].split(',').map { it.toFloat() } + require(position.size == 3 && position.all { it.isFinite() }) + fun text(at: Int) = parts.getOrNull(at).orEmpty().replace("%2C", ",").replace("%3A", ":").replace("%3B", ";").replace("%25", "%") + val shape = parts.getOrNull(6)?.toIntOrNull() ?: 0 + val kind = when (shape) { 5 -> InputElementKind.DPAD; 6 -> InputElementKind.STICK; 4 -> InputElementKind.TRIGGER; else -> InputElementKind.BUTTON } + val label = text(3).ifEmpty { text(2) }.ifEmpty { codes.filter { it != 0 }.joinToString("+") { pcKeyLabel(context, it) } }.ifBlank { "Button" }.take(80) + val image = text(4) + val icon = codes.singleOrNull()?.takeIf { InputButtonIcons.has(it) && text(3).isEmpty() && text(2).isEmpty() } ?: 0 + val heightPx = position[2].coerceIn(24f, 140f) * density + val ratio = when { shape == 2 -> 1f; shape == 4 -> 1.7f; shape == 5 || shape == 6 || image.isNotEmpty() || icon != 0 -> 1.15f; else -> (.55f * label.length + 1.05f).coerceIn(1.7f, 4.6f) } + val w = (heightPx * ratio / width.coerceAtLeast(1)).coerceIn(.03f, 1f) + val h = (heightPx / height.coerceAtLeast(1)).coerceIn(.03f, 1f) + val element = InputSurfaceElement(kind.control, (position[0] - w / 2).coerceIn(0f, 1f - w), (position[1] - h / 2).coerceIn(0f, 1f - h), w, h, + id = "imported:$index", kind = kind, name = label, floating = false, + toggle = parts.getOrNull(7) == "1" && kind != InputElementKind.DPAD && kind != InputElementKind.STICK, + icon = icon, image = image, opacity = (parts.getOrNull(5)?.toFloatOrNull() ?: 1f).coerceIn(.2f, 1f), + buttonShape = when (shape) { 0 -> InputButtonShape.PILL; 2 -> InputButtonShape.CIRCLE; 3 -> InputButtonShape.RECTANGLE; else -> InputButtonShape.ROUNDED }, + showLabel = parts.getOrNull(8) != "0").bounded() + val action = when (kind) { + InputElementKind.DPAD -> InputAction.Directions(codes.getOrElse(0) { 0 }, codes.getOrElse(1) { 0 }, codes.getOrElse(2) { 0 }, codes.getOrElse(3) { 0 }) + InputElementKind.STICK -> InputAction.Directions(codes.getOrElse(0) { 0 }, codes.getOrElse(2) { 0 }, codes.getOrElse(4) { 0 }, codes.getOrElse(6) { 0 }, listOf(1, 3, 5, 7).map { codes.getOrElse(it) { 0 } }) + else -> codes.filter { it != 0 }.distinct().let { if (it.isEmpty()) InputAction.Disabled else InputAction.Keys(it) } + } + require(action.validFor(element)) + element to action + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/input/OnScreenControls.kt b/app/src/main/java/com/droiddeck/launcher/input/OnScreenControls.kt index dc26a75f55..053bc695ed 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/OnScreenControls.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/OnScreenControls.kt @@ -1,833 +1,44 @@ package com.droiddeck.launcher.input -import android.annotation.SuppressLint import android.content.Context -import android.graphics.Canvas -import android.graphics.Color -import android.graphics.Paint -import android.graphics.Path -import android.graphics.Rect -import android.graphics.RectF -import android.os.Build -import android.view.HapticFeedbackConstants import android.view.MotionEvent -import android.view.View -import android.view.WindowInsets -import com.droiddeck.launcher.session.SessionPrefs -import kotlin.math.abs -import kotlin.math.hypot -import kotlin.math.max -import kotlin.math.min -import kotlin.math.sin -import kotlin.math.sqrt +import android.widget.FrameLayout +import com.droiddeck.launcher.ui.InputSurfaceView -@SuppressLint("ViewConstructor") -class OnScreenControls( - context: Context, - private val pad: PadBridge?, - private val editing: Boolean = false, - private val onKeyboard: (() -> Unit)? = null, -) : View(context) { +class OnScreenControls(context: Context, pad: PadBridge?) : FrameLayout(context) { + private val surface = InputSurfaceView(context, pad, inputRole = InputRole.PRIMARY) + private var layout: SavedInputLayout? = null + var isButtonsOnly = false + private set + var onTouchMiss: ((MotionEvent) -> Unit)? + get() = surface.onTouchMiss + set(value) { surface.onTouchMiss = value } - private class Control( - val id: String, - val group: String, - val stick: Int, - val radiusDp: Float, - val offsetXDp: Float, - val offsetYDp: Float, - ) { - var target = id - var radius = 0f - var cx = 0f - var cy = 0f - var pressedBy = -1 - var kx = 0f - var ky = 0f - var ax = 0f - var ay = 0f - var dirty = false - var clicked = false - var lastUp = 0L - var downAt = 0L - var downX = 0f - var downY = 0f - var travel = 0f - val wide = id in shoulderIds - val halfW get() = if (wide) radius * SHOULDER_WIDTH else radius - val halfH get() = if (wide) radius * SHOULDER_HEIGHT else radius + init { addView(surface, LayoutParams(-1, -1)) } - fun contains(x: Float, y: Float, r: Float, padding: Float = 0f): Boolean { - val dx = x - cx - val dy = y - cy - if (wide) return abs(dx) <= halfW + r * 0.25f + padding && abs(dy) <= halfH + r * 0.25f + padding - val hit = r * 1.25f + padding - return dx * dx + dy * dy <= hit * hit - } - - fun drag(x: Float, y: Float): Boolean { - travel = max(travel, hypot(x - downX, y - downY)) - var dx = x - ax - var dy = y - ay - val d = sqrt(dx * dx + dy * dy) - val atLimit = d > radius - if (atLimit) { dx = dx / d * radius; dy = dy / d * radius } - if (!atLimit && abs(dx - kx) < MIN_STICK_MOVE_PX && abs(dy - ky) < MIN_STICK_MOVE_PX) return false - kx = dx; ky = dy; dirty = true - return true - } - } - - private val stickRedraw = ThrottledRedraw(this, STICK_REDRAW_MS) - private val tapSlop = android.view.ViewConfiguration.get(context).scaledTouchSlop.toFloat() - - private fun stickTap(control: Control, event: MotionEvent): Boolean { - val i = event.actionIndex - val travel = max(control.travel, hypot(event.getX(i) - control.downX, event.getY(i) - control.downY)) - return stickTap(control.downAt, event.eventTime, travel, tapSlop, control.clicked, - event.actionMasked == MotionEvent.ACTION_CANCEL || (event.flags and MotionEvent.FLAG_CANCELED) != 0) - } - - private val controls = listOf( - Control("up", "dpad", -1, 26f, 0f, -52f), - Control("right", "dpad", -1, 26f, 52f, 0f), - Control("down", "dpad", -1, 26f, 0f, 52f), - Control("left", "dpad", -1, 26f, -52f, 0f), - Control("ls", "ls", 0, 48f, 0f, 0f), - Control("rs", "rs", 1, 48f, 0f, 0f), - Control("a", "face", -1, 30f, 0f, 50f), - Control("b", "face", -1, 30f, 50f, 0f), - Control("x", "face", -1, 30f, -50f, 0f), - Control("y", "face", -1, 30f, 0f, -50f), - Control("lb", "lb", -1, 24f, 0f, 0f), - Control("rb", "rb", -1, 24f, 0f, 0f), - Control("lt", "lt", -1, 24f, 0f, 0f), - Control("rt", "rt", -1, 24f, 0f, 0f), - Control("select", "select", -1, 20f, 0f, 0f), - Control("start", "start", -1, 20f, 0f, 0f), - Control("guide", "guide", -1, 22f, 0f, 0f), - Control("qam", "qam", -1, 22f, 0f, 0f), - ) - - private val groups = controls.map { it.group }.distinct() - - private var buttonsOnly = false - val isButtonsOnly: Boolean get() = buttonsOnly - private var quickHidden = false - private var quickPressedBy = -1 - private var keyboardPressedBy = -1 - private var forwardedPointerBits = 0 - var onTouchMiss: ((MotionEvent) -> Unit)? = null - private var settings = ControllerPrefs.read(context) - private var snap = settings.snap - private var safe = Rect() - private var selected: String? = null - private var snapX: Float? = null - private var snapY: Float? = null - private var grabX = 0f - private var grabY = 0f - private var ignoreSaved = false - private var fit = 1f - - private var idleFill = 0 - private var heldFill = 0 - private var idleStroke = 0 - private var heldStroke = 0 - private var idleText = 0 - private var stickFill = 0 - private var knobFill = 0 - - private val fill = Paint(Paint.ANTI_ALIAS_FLAG).apply { style = Paint.Style.FILL } - private val stroke = Paint(Paint.ANTI_ALIAS_FLAG).apply { style = Paint.Style.STROKE } - private val arrow = Path() - private val box = RectF() - private val text = Paint(Paint.ANTI_ALIAS_FLAG).apply { - textAlign = Paint.Align.CENTER - typeface = android.graphics.Typeface.DEFAULT_BOLD - } - - init { - applySettings() - } - - private fun dp(value: Float) = value * resources.displayMetrics.density - - private fun scaled(value: Float) = dp(value) * settings.size / 100f * fit - - private fun fitScale(): Float { - val metrics = resources.displayMetrics - val heightMm = (height - safe.top - safe.bottom) / pxPerMm(metrics.ydpi) - val faceMm = dp(60f) / pxPerMm(metrics.xdpi) - val floor = (MIN_FACE_MM / faceMm).coerceAtMost(1f) - return (heightMm / FULL_SIZE_HEIGHT_MM).coerceIn(floor, 1f) - } - - fun reload() { + fun updateLayout(value: SavedInputLayout?) { + if (layout == value) return releaseAll() - settings = ControllerPrefs.read(context) - setSnap(settings.snap) - applySettings() - relayout() - } - - private fun clearSnapGuides() { - if (snapX == null && snapY == null) return - snapX = null - snapY = null - invalidate() - } - - private fun applySettings() { - for (control in controls) control.target = settings.mapping[control.id] ?: control.id - val tint = settings.tint - val alpha = settings.opacity / 100f - fun shade(a: Int, f: Float) = Color.argb((a * alpha).toInt(), (Color.red(tint) * f).toInt(), (Color.green(tint) * f).toInt(), (Color.blue(tint) * f).toInt()) - fun light(a: Int, f: Float) = Color.argb( - (a * alpha).toInt(), - (Color.red(tint) + (255 - Color.red(tint)) * f).toInt(), - (Color.green(tint) + (255 - Color.green(tint)) * f).toInt(), - (Color.blue(tint) + (255 - Color.blue(tint)) * f).toInt(), - ) - idleFill = shade(80, 0.12f) - heldFill = shade(160, 1f) - idleStroke = light(150, 0.25f) - heldStroke = light(230, 0.7f) - idleText = light(210, 0.75f) - stickFill = shade(60, 0.12f) - knobFill = shade(120, 0.35f) - stroke.strokeWidth = dp(1.5f) + layout = value + render() } fun setButtonsOnly(enabled: Boolean) { - if (buttonsOnly == enabled) return + if (isButtonsOnly == enabled) return releaseAll() - buttonsOnly = enabled - quickHidden = false - relayout() + isButtonsOnly = enabled + render() } - fun setQuickHidden(hidden: Boolean) { - if (buttonsOnly || editing || quickHidden == hidden) return - if (hidden) releaseAll() - quickHidden = hidden - invalidate() + private fun render() { + val elements = layout?.elements.orEmpty() + surface.updateCustomLayout(if (isButtonsOnly) elements.filter { it.control == InputControl.GUIDE || it.control == InputControl.QAM || it.id == "builtin:keyboard" } else elements) + surface.updateStyle(layout?.style ?: InputSurfaceStyle()) } - fun setSnap(on: Boolean) { - snap = on - if (!on) clearSnapGuides() - } + fun takesTouchAt(x: Float, y: Float) = surface.takesTouchAt(x, y) + fun releaseAll() = surface.releaseInputs() + fun reload() { releaseAll(); render() } - override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) { - super.onSizeChanged(w, h, oldw, oldh) - relayout() - } - - override fun onApplyWindowInsets(insets: WindowInsets): WindowInsets { - val cutout = if (Build.VERSION.SDK_INT >= 28) insets.displayCutout else null - val fresh = Rect(cutout?.safeInsetLeft ?: 0, cutout?.safeInsetTop ?: 0, cutout?.safeInsetRight ?: 0, cutout?.safeInsetBottom ?: 0) - if (fresh != safe) { safe = fresh; relayout() } - return super.onApplyWindowInsets(insets) - } - - override fun onDetachedFromWindow() { - stickRedraw.cancel() - releaseAll() - super.onDetachedFromWindow() - } - - private fun relayout() { - if (width <= 0 || height <= 0) return - fit = if (buttonsOnly && !editing) 1f else fitScale() - for (control in controls) control.radius = if (buttonsOnly && !editing && (control.id == "guide" || control.id == "qam")) dp(30f) else scaled(control.radiusDp) - if (buttonsOnly && !editing) { - val inset = dp(44f) - put("guide", safe.left + inset, height - safe.bottom - inset) - put("qam", width - safe.right - inset, height - safe.bottom - inset) - } else { - placeAuto(width.toFloat(), height.toFloat()) - if (!ignoreSaved) for ((group, pos) in ControllerPrefs.layout(context, width, height)) { - if (group in groups) put(group, pos.first * width, pos.second * height) - } - } - groups.forEach { clamp(it) } - invalidate() - } - - private fun pxPerMm(dpi: Float): Float { - val nominal = resources.displayMetrics.densityDpi.toFloat() - return (if (dpi > nominal * 0.6f && dpi < nominal * 1.6f) dpi else nominal) / 25.4f - } - - private fun placeAuto(w: Float, h: Float) { - val metrics = resources.displayMetrics - val mmX = pxPerMm(metrics.xdpi) - val mmY = pxPerMm(metrics.ydpi) - val usableW = w - safe.left - safe.right - val usableH = h - safe.top - safe.bottom - val inboard = (0.17f * usableW / mmX).coerceIn(24f, 40f) * mmX - val lift = (0.285f * usableH / mmY).coerceIn(22f, 45f) * mmY - val gap = dp(12f) - val floor = BOTTOM_MARGIN_MM * mmY - val drop = max(drop("dpad", "ls", gap), drop("rs", "face", gap)) - val restY = min(h - safe.bottom - lift, h - safe.bottom - floor - drop) - put("ls", safe.left + inboard, restY) - put("face", w - safe.right - inboard, restY) - besideBelow("dpad", "ls", 1f, gap, floor) - besideBelow("rs", "face", -1f, gap, floor) - val phone = usableH / mmY < PHONE_MAX_HEIGHT_MM - val shoulderY = if (phone) safe.top + floor + halfH("lb") - else min(centre("ls").second - extent("ls"), centre("face").second - extent("face")) - gap - halfH("lb") - shoulders("ls", "lb", "lt", 1f, gap, shoulderY) - shoulders("face", "rb", "rt", -1f, gap, shoulderY) - val bottom = h - safe.bottom - floor - extent("guide") - val spread = extent("guide") + gap / 2f - put("guide", w / 2f - spread, bottom) - put("qam", w / 2f + spread, bottom) - val inner = max(centre("dpad").first + extent("dpad"), w - centre("rs").first + extent("rs")) + gap + extent("select") - put("select", inner, restY) - put("start", w - inner, restY) - if (phone) { - val top = max(centre("lb").first + extent("lb"), w - centre("rb").first + extent("rb")) + gap * 2.5f + extent("select") - put("select", top, shoulderY) - put("start", w - top, shoulderY) - } - if (crowded("select", gap) || crowded("start", gap)) { - above("select", "dpad", gap) - val (sx, sy) = centre("select") - put("start", w - sx, sy) - if (crowded("start", gap)) above("start", "rs", gap) - } - if (crowded("guide", gap, gap, "qam") || crowded("qam", gap, gap, "guide")) { - val row = centre("select").second - put("guide", w / 2f - spread, row) - put("qam", w / 2f + spread, row) - if (crowded("guide", gap, gap, "qam") || crowded("qam", gap, gap, "guide")) { - val top = safe.top + dp(8f) + extent("guide") - put("guide", w / 2f - spread, top) - put("qam", w / 2f + spread, top) - } - } - } - - private fun drop(group: String, anchor: String, gap: Float): Float = - (extent(anchor) + extent(group) + gap) * sin(Math.toRadians(DROP_DEGREES)).toFloat() + extent(group) - - private fun besideBelow(group: String, anchor: String, inward: Float, gap: Float, margin: Float) { - val (ax, ay) = centre(anchor) - val reach = extent(anchor) + extent(group) + gap - val floor = height - safe.bottom - margin - extent(group) - val dy = min(reach * sin(Math.toRadians(DROP_DEGREES)).toFloat(), floor - ay) - val dx = sqrt(max(0f, reach * reach - dy * dy)) - put(group, ax + inward * dx, ay + dy) - } - - private fun shoulders(anchor: String, bumper: String, trigger: String, inward: Float, gap: Float, y: Float) { - val (ax, _) = centre(anchor) - val spread = extent(bumper) + gap / 2f - put(bumper, ax + inward * spread, y) - put(trigger, ax - inward * spread, y) - } - - private fun above(group: String, anchor: String, gap: Float) { - val (ax, ay) = centre(anchor) - put(group, ax, ay - extent(anchor) - gap - extent(group)) - } - - private fun crowded(group: String, gap: Float, space: Float = gap / 2f, except: String? = null): Boolean { - val (x, y) = centre(group) - return groups.any { other -> - if (other == group || other == except) return@any false - val (ox, oy) = centre(other) - val dx = x - ox - val dy = y - oy - sqrt(dx * dx + dy * dy) < extent(group) + extent(other) + space - } - } - - private fun members(group: String) = controls.filter { it.group == group } - - private fun centre(group: String): Pair { - val first = members(group).first() - return (first.cx - scaled(first.offsetXDp)) to (first.cy - scaled(first.offsetYDp)) - } - - private fun extent(group: String): Float = members(group).maxOf { control -> - sqrt(scaled(control.offsetXDp).let { it * it } + scaled(control.offsetYDp).let { it * it }) + control.halfW - } - - private fun halfH(group: String): Float = members(group).maxOf { abs(scaled(it.offsetYDp)) + it.halfH } - - private fun put(group: String, x: Float, y: Float) { - for (control in members(group)) { - control.cx = x + scaled(control.offsetXDp) - control.cy = y + scaled(control.offsetYDp) - } - } - - private fun clamp(group: String) { - val list = members(group) - val margin = dp(4f) - val minX = list.minOf { it.cx - it.halfW } - safe.left - margin - val maxX = list.maxOf { it.cx + it.halfW } - (width - safe.right - margin) - val minY = list.minOf { it.cy - it.halfH } - safe.top - margin - val maxY = list.maxOf { it.cy + it.halfH } - (height - safe.bottom - margin) - val dx = if (minX < 0) -minX else if (maxX > 0) -maxX else 0f - val dy = if (minY < 0) -minY else if (maxY > 0) -maxY else 0f - if (dx == 0f && dy == 0f) return - for (control in list) { control.cx += dx; control.cy += dy } - } - - fun saveLayout() { - if (width <= 0 || height <= 0) return - if (ignoreSaved) { ControllerPrefs.resetLayout(context, width, height); return } - ControllerPrefs.setLayout(context, width, height, groups.associateWith { group -> - val (x, y) = centre(group) - x / width to y / height - }) - } - - fun resetLayout() { - ControllerPrefs.resetLayout(context, width, height) - ignoreSaved = true - selected = null - relayout() - } - - private fun label(control: Control): String = when (control.target) { - "ls" -> "L" - "rs" -> "R" - "select" -> "⧉" - "start" -> "☰" - "guide" -> if (buttonsOnly && !editing) "Steam" else "◉" - "qam" -> "⋯" - else -> control.target.uppercase() - } - - override fun onDraw(canvas: Canvas) { - if (!quickHidden) for (control in controls) { - if (!isVisible(control)) continue - if (control.stick >= 0 && settings.adaptiveSticks && !editing && control.pressedBy == -1) continue - val held = control.pressedBy != -1 - val radius = control.radius - if (control.stick >= 0) { - val bx = if (held) control.ax else control.cx - val by = if (held) control.ay else control.cy - fill.color = if (held) heldFill else stickFill - canvas.drawCircle(bx, by, radius, fill) - stroke.color = if (held) heldStroke else idleStroke - canvas.drawCircle(bx, by, radius, stroke) - val knob = radius * 0.46f - fill.color = if (held || control.clicked) heldFill else knobFill - canvas.drawCircle(bx + control.kx, by + control.ky, knob, fill) - stroke.color = if (held) heldStroke else idleStroke - canvas.drawCircle(bx + control.kx, by + control.ky, knob, stroke) - text.color = if (held) Color.WHITE else idleText - text.textSize = knob * 0.8f - canvas.drawText(label(control), bx + control.kx, by + control.ky + text.textSize * 0.35f, text) - } else if (control.wide) { - box.set(control.cx - control.halfW, control.cy - control.halfH, control.cx + control.halfW, control.cy + control.halfH) - val corner = control.halfH * 0.55f - fill.color = if (held) heldFill else idleFill - canvas.drawRoundRect(box, corner, corner, fill) - stroke.color = if (held) heldStroke else idleStroke - canvas.drawRoundRect(box, corner, corner, stroke) - text.color = if (held) Color.WHITE else idleText - text.textSize = control.halfH * 0.8f - canvas.drawText(label(control), control.cx, control.cy + text.textSize * 0.35f, text) - } else { - fill.color = if (held) heldFill else idleFill - canvas.drawCircle(control.cx, control.cy, radius, fill) - stroke.color = if (held) heldStroke else idleStroke - canvas.drawCircle(control.cx, control.cy, radius, stroke) - text.color = if (held) Color.WHITE else idleText - val direction = directions.indexOf(control.target) - if (direction >= 0) { - drawArrow(canvas, control.cx, control.cy, radius * 0.34f, direction) - continue - } - val name = label(control) - text.textSize = if (name.length > 2) dp(11f) else radius * if (name.length == 2) 0.62f else 0.85f - canvas.drawText(name, control.cx, control.cy + text.textSize * 0.35f, text) - } - } - if (editing) { - stroke.color = heldStroke - snapX?.let { canvas.drawLine(it, safe.top.toFloat(), it, (height - safe.bottom).toFloat(), stroke) } - snapY?.let { canvas.drawLine(safe.left.toFloat(), it, (width - safe.right).toFloat(), it, stroke) } - selected?.let { group -> - val (x, y) = centre(group) - val single = members(group).singleOrNull() - if (single?.wide == true) { - val pad = dp(6f) - box.set(x - single.halfW - pad, y - single.halfH - pad, x + single.halfW + pad, y + single.halfH + pad) - canvas.drawRoundRect(box, single.halfH, single.halfH, stroke) - } else canvas.drawCircle(x, y, extent(group) + dp(6f), stroke) - } - } else if (!buttonsOnly) { - val (x, y) = quickCenter() - val radius = dp(18f) - fill.color = Color.argb(190, Color.red(idleFill), Color.green(idleFill), Color.blue(idleFill)) - canvas.drawCircle(x, y, radius, fill) - stroke.color = idleStroke - canvas.drawCircle(x, y, radius, stroke) - text.color = idleText - text.textSize = dp(22f) - canvas.drawText(if (quickHidden) "+" else "−", x, y + text.textSize * 0.35f, text) - } - if (!editing && settings.keyboardButton && onKeyboard != null) { - val (x, y) = keyboardCenter() - fill.color = Color.argb(190, Color.red(idleFill), Color.green(idleFill), Color.blue(idleFill)) - canvas.drawCircle(x, y, dp(18f), fill) - stroke.color = if (keyboardPressedBy == -1) idleStroke else heldStroke - canvas.drawCircle(x, y, dp(18f), stroke) - box.set(x - dp(10f), y - dp(7f), x + dp(10f), y + dp(7f)) - canvas.drawRoundRect(box, dp(2f), dp(2f), stroke) - for (row in 0..1) for (col in 0..2) { - val kx = x + dp(-6f + col * 5f) - val ky = y + dp(-3f + row * 4f) - canvas.drawLine(kx, ky, kx + dp(2f), ky, stroke) - } - canvas.drawLine(x - dp(5f), y + dp(4f), x + dp(5f), y + dp(4f), stroke) - } - } - - private fun quickCenter() = (safe.left + dp(34f)) to (height - safe.bottom - dp(34f)) - - private fun keyboardCenter() = if (buttonsOnly) - width / 2f to (height - safe.bottom - dp(44f)) - else (safe.left + dp(90f)) to (height - safe.bottom - dp(34f)) - - private fun keyboardContains(x: Float, y: Float): Boolean { - if (!settings.keyboardButton || onKeyboard == null) return false - val (cx, cy) = keyboardCenter() - val dx = x - cx - val dy = y - cy - return dx * dx + dy * dy <= dp(26f) * dp(26f) - } - - private fun quickContains(x: Float, y: Float): Boolean { - val (cx, cy) = quickCenter() - val radius = dp(26f) - val dx = x - cx - val dy = y - cy - return dx * dx + dy * dy <= radius * radius - } - - private fun drawArrow(canvas: Canvas, x: Float, y: Float, size: Float, direction: Int) { - val (fx, fy) = when (direction) { - 0 -> 0f to -1f - 1 -> 1f to 0f - 2 -> 0f to 1f - else -> -1f to 0f - } - arrow.reset() - arrow.moveTo(x + fx * size, y + fy * size) - arrow.lineTo(x - fx * size * 0.7f - fy * size, y - fy * size * 0.7f + fx * size) - arrow.lineTo(x - fx * size * 0.7f + fy * size, y - fy * size * 0.7f - fx * size) - arrow.close() - fill.color = text.color - canvas.drawPath(arrow, fill) - } - - @SuppressLint("ClickableViewAccessibility") - override fun onTouchEvent(event: MotionEvent): Boolean { - if (editing) return onEditTouch(event) - if (event.getToolType(event.actionIndex) == MotionEvent.TOOL_TYPE_MOUSE) return false - when (event.actionMasked) { - MotionEvent.ACTION_DOWN, MotionEvent.ACTION_POINTER_DOWN -> { - val index = event.actionIndex - val x = event.getX(index) - val y = event.getY(index) - if (event.actionMasked == MotionEvent.ACTION_DOWN) forwardedPointerBits = 0 - if (event.actionMasked == MotionEvent.ACTION_POINTER_DOWN && forwardMiss(event, x, y)) return true - if (forwardedPointerBits != 0) forwardPointers(event, forwardedPointerBits) - if (event.actionMasked == MotionEvent.ACTION_DOWN && keyboardContains(x, y)) { - keyboardPressedBy = event.getPointerId(index) - invalidate() - return true - } - if (keyboardPressedBy != -1) return true - if (!buttonsOnly && event.actionMasked == MotionEvent.ACTION_DOWN && quickContains(x, y)) { - quickPressedBy = event.getPointerId(index) - return true - } - if (quickPressedBy != -1) return true - if (quickHidden) return false - val control = controlAt(x, y) ?: adaptiveStickAt(x, y) ?: return false - if (control.pressedBy != -1) return true - control.pressedBy = event.getPointerId(index) - if (control.stick >= 0) { - control.clicked = settings.stickClick && control.lastUp > 0L && event.eventTime - control.lastUp < DOUBLE_TAP_MS - control.downAt = event.eventTime - control.downX = event.getX(index) - control.downY = event.getY(index) - control.travel = 0f - val adaptive = settings.adaptiveSticks - control.ax = if (adaptive) event.getX(index) else control.cx - control.ay = if (adaptive) event.getY(index) else control.cy - control.drag(event.getX(index), event.getY(index)) - control.dirty = true - } - if (SessionPrefs.lowLatency(context)) requestUnbufferedDispatch(event) - apply() - return true - } - MotionEvent.ACTION_MOVE -> { - if (forwardedPointerBits != 0) forwardPointers(event, forwardedPointerBits) - if (keyboardPressedBy != -1) return true - if (quickPressedBy != -1) return true - var changed = false - var stickMoved = false - for (index in 0 until event.pointerCount) { - val pointer = event.getPointerId(index) - if (forwardedPointerBits and (1 shl pointer) != 0) continue - val x = event.getX(index) - val y = event.getY(index) - val stick = controls.firstOrNull { it.stick >= 0 && it.pressedBy == pointer } - if (stick != null) { stickMoved = stick.drag(x, y) || stickMoved; continue } - val over = controlAt(x, y)?.takeIf { it.stick < 0 } - for (control in controls) { - if (control.stick < 0 && control.pressedBy == pointer && control !== over) { - control.pressedBy = -1 - changed = true - } - } - if (over != null && over.pressedBy == -1) { - over.pressedBy = pointer - changed = true - } - } - if (changed) apply() else if (stickMoved) apply(stickOnly = true) - return true - } - MotionEvent.ACTION_UP, MotionEvent.ACTION_POINTER_UP, MotionEvent.ACTION_CANCEL -> { - val pointer = event.getPointerId(event.actionIndex) - if (forwardedPointerBits != 0) forwardPointers(event, forwardedPointerBits) - if (event.actionMasked == MotionEvent.ACTION_CANCEL) { - forwardedPointerBits = 0 - releaseAll() - return true - } - if (forwardedPointerBits and (1 shl pointer) != 0) { - forwardedPointerBits = forwardedPointerBits and (1 shl pointer).inv() - return true - } - if (keyboardPressedBy == pointer) { - val tapped = event.actionMasked == MotionEvent.ACTION_UP && keyboardContains(event.x, event.y) - keyboardPressedBy = -1 - invalidate() - if (tapped) { releaseAll(); onKeyboard?.invoke() } - return true - } - if (quickPressedBy == pointer) { - val toggled = event.actionMasked == MotionEvent.ACTION_UP && quickContains(event.x, event.y) - quickPressedBy = -1 - if (toggled) setQuickHidden(!quickHidden) - return true - } - if (quickHidden) return false - var changed = false - for (control in controls) { - if (control.pressedBy == pointer || event.actionMasked == MotionEvent.ACTION_CANCEL) { - if (control.pressedBy != -1) changed = true - if (control.stick >= 0 && control.pressedBy != -1) control.lastUp = if (stickTap(control, event)) event.eventTime else 0L - control.pressedBy = -1 - control.clicked = false - if (control.stick >= 0 && (control.kx != 0f || control.ky != 0f)) control.dirty = true - control.kx = 0f; control.ky = 0f - } - } - if (changed) apply() - return true - } - } - return false - } - - private fun forwardMiss(event: MotionEvent, x: Float, y: Float): Boolean { - if (onTouchMiss == null || keyboardPressedBy != -1 || quickPressedBy != -1) return false - if (!quickHidden && (controlAt(x, y) ?: adaptiveStickAt(x, y)) != null) return false - forwardedPointerBits = forwardedPointerBits or (1 shl event.getPointerId(event.actionIndex)) - forwardPointers(event, forwardedPointerBits) - return true - } - - private fun forwardPointers(event: MotionEvent, pointerIdBits: Int) { - val split = event.pointerSubset(pointerIdBits) ?: return - try { - onTouchMiss?.invoke(split) - } finally { - split.recycle() - } - } - - private fun onEditTouch(event: MotionEvent): Boolean { - when (event.actionMasked) { - MotionEvent.ACTION_DOWN -> { - clearSnapGuides() - val control = controlAt(event.x, event.y) - selected = control?.group - val group = selected ?: run { invalidate(); return false } - val (x, y) = centre(group) - grabX = x - event.x - grabY = y - event.y - invalidate() - } - MotionEvent.ACTION_MOVE -> { - val group = selected ?: return true - ignoreSaved = false - val rawX = event.x + grabX - val rawY = event.y + grabY - val others = groups.filter { it != group && members(it).any(::isVisible) }.map { centre(it) } - val nextX = if (snap) closestSnap(rawX, others.map { it.first } + width / 2f) else null - val nextY = if (snap) closestSnap(rawY, others.map { it.second }) else null - put(group, nextX ?: rawX, nextY ?: rawY) - clamp(group) - val (x, y) = centre(group) - val visibleX = nextX?.takeIf { abs(x - it) <= SNAP_TOLERANCE_PX } - val visibleY = nextY?.takeIf { abs(y - it) <= SNAP_TOLERANCE_PX } - if ((visibleX != null && visibleX != snapX) || (visibleY != null && visibleY != snapY)) performHapticFeedback(HapticFeedbackConstants.CLOCK_TICK) - snapX = visibleX - snapY = visibleY - invalidate() - } - MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> clearSnapGuides() - } - return true - } - - private fun closestSnap(position: Float, targets: List): Float? = - targets.minByOrNull { abs(position - it) }?.takeIf { abs(position - it) <= dp(SNAP_DP) } - - fun takesTouchAt(x: Float, y: Float): Boolean = - editing || keyboardContains(x, y) || (!buttonsOnly && quickContains(x, y)) || - (!quickHidden && (controlAt(x, y) ?: adaptiveStickAt(x, y)) != null) - - private fun controlAt(x: Float, y: Float): Control? = - controls.firstOrNull { - !quickHidden && isVisible(it) && (editing || !settings.adaptiveSticks || it.stick < 0) && it.contains(x, y, it.radius) - } - - private fun adaptiveStickAt(x: Float, y: Float): Control? { - if (!settings.adaptiveSticks || editing || buttonsOnly || quickHidden) return null - if (x < safe.left || x >= width - safe.right || y < safe.top || y >= height - safe.bottom) return null - if (controls.any { control -> - control.stick < 0 && isVisible(control) && control.contains(x, y, control.radius, 5f) - }) return null - return controls.filter { control -> - val dx = if (control.stick == 0) max(0f, x - control.cx) else x - control.cx - val dy = y - control.cy - val reach = control.radius * 1.4f * sqrt(1.5f) - control.stick >= 0 && isVisible(control) && control.pressedBy == -1 && - dx * dx + dy * dy <= reach * reach - }.minByOrNull { control -> - val dx = x - control.cx - val dy = y - control.cy - dx * dx + dy * dy - } - } - - private fun isVisible(control: Control): Boolean = when { - !editing && control.id == "guide" && !settings.steamButton -> false - !editing && control.id == "qam" && !settings.qamButton -> false - buttonsOnly && !editing -> control.id == "guide" || control.id == "qam" - else -> control.target != ControllerPrefs.OFF - } - - private fun apply(stickOnly: Boolean = false) { - val bridge = pad - if (bridge == null || editing) { invalidate(); return } - val qamHeld = controls.any { it.id == "qam" && it.pressedBy != -1 } - if (buttonsOnly) { - bridge.setSystemButtons(controls.any { it.id == "guide" && it.pressedBy != -1 }, qamHeld) - } else { - bridge.setSystemButtons(false, qamHeld) - val stickWrites = controls.filter { it.stick >= 0 && it.dirty }.fold(0) { mask, control -> - mask or if (control.stick == 0) PadBridge.LEFT_STICK else PadBridge.RIGHT_STICK - } - bridge.applyTouch(stickWrites) { state -> - val used = HashSet() - val held = HashSet() - for (control in controls) { - when { - control.stick >= 0 -> { - val click = if (control.stick == 0) "l3" else "r3" - if (settings.stickClick) used.add(click) - if (control.clicked) held.add(click) - if (!control.dirty) continue - if (control.stick == 0) { state.leftX = control.kx / control.radius; state.leftY = control.ky / control.radius } - else { state.rightX = control.kx / control.radius; state.rightY = control.ky / control.radius } - control.dirty = false - } - control.id == "qam" || control.target == ControllerPrefs.OFF -> {} - else -> { - used.add(control.target) - if (control.pressedBy != -1) held.add(control.target) - } - } - } - for (target in used) write(state, target, target in held) - } - } - if (stickOnly) stickRedraw.request() else invalidate() - } - - private fun write(state: PadState, target: String, down: Boolean) { - when (target) { - "a" -> state.press(PadState.A, down) - "b" -> state.press(PadState.B, down) - "x" -> state.press(PadState.X, down) - "y" -> state.press(PadState.Y, down) - "lb" -> state.press(PadState.LB, down) - "rb" -> state.press(PadState.RB, down) - "select" -> state.press(PadState.SELECT, down) - "start" -> state.press(PadState.START, down) - "l3" -> state.press(PadState.L3, down) - "r3" -> state.press(PadState.R3, down) - "guide" -> state.press(PadState.GUIDE, down) - "lt" -> state.leftTrigger = if (down) 1f else 0f - "rt" -> state.rightTrigger = if (down) 1f else 0f - "up" -> state.up = down - "right" -> state.right = down - "down" -> state.down = down - "left" -> state.left = down - } - } - - fun releaseAll() { - quickPressedBy = -1 - keyboardPressedBy = -1 - invalidate() - if (controls.none { it.pressedBy != -1 || it.clicked } && !buttonsOnly) return - controls.forEach { - it.pressedBy = -1 - it.clicked = false - it.lastUp = 0L - if (it.stick >= 0 && (it.kx != 0f || it.ky != 0f)) it.dirty = true - it.kx = 0f; it.ky = 0f - } - apply() - } - - private companion object { - const val DOUBLE_TAP_MS = 300L - const val STICK_TAP_MS = 200L - - internal fun stickTap(downAt: Long, upAt: Long, travel: Float, slop: Float, clicked: Boolean, cancelled: Boolean): Boolean = - !clicked && !cancelled && upAt - downAt <= STICK_TAP_MS && travel <= slop - const val STICK_REDRAW_MS = 33L - const val MIN_STICK_MOVE_PX = 1f - const val MIN_FACE_MM = 9.5f - const val FULL_SIZE_HEIGHT_MM = 85f - const val BOTTOM_MARGIN_MM = 5f - const val DROP_DEGREES = 35.0 - const val PHONE_MAX_HEIGHT_MM = 90f - const val SHOULDER_WIDTH = 1.45f - const val SHOULDER_HEIGHT = 0.8f - const val SNAP_DP = 9f - const val SNAP_TOLERANCE_PX = 1f - val shoulderIds = setOf("lb", "rb", "lt", "rt") - val directions = listOf("up", "right", "down", "left") - } + override fun onDetachedFromWindow() { releaseAll(); super.onDetachedFromWindow() } } diff --git a/app/src/main/java/com/droiddeck/launcher/input/PadBridge.java b/app/src/main/java/com/droiddeck/launcher/input/PadBridge.java index 4bf45e9fee..0d0dbc2f71 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/PadBridge.java +++ b/app/src/main/java/com/droiddeck/launcher/input/PadBridge.java @@ -59,11 +59,25 @@ public final class PadBridge { private final int[] retainedSticks = new int[SLOTS]; private final PadState effectiveState = new PadState(); private final PadState homeGuideReport = new PadState(); + private final PadState combinedState = new PadState(); + private InputRouter inputRouter; + private final InputRouter.PadSource[] physicalRoutes = new InputRouter.PadSource[SLOTS]; + private final java.util.IdentityHashMap touchSources = new java.util.IdentityHashMap<>(); + private final Object defaultTouchSource = new Object(); + + private static final class TouchSource { + final PadState raw = new PadState(); + final PadState passthrough = new PadState(); + final InputRole role; + boolean routed; + InputRouter.PadSource route; + TouchSource(InputRole role) { this.role = role; } + } private final Handler mainHandler = new Handler(Looper.getMainLooper()); private boolean open; - private volatile boolean selectSteam; private volatile boolean builtinPad = true; private TouchRoute touchRoute; + private boolean touchMouseActive; private final PadState routedTouch = new PadState(); private boolean systemGuidePressed; private boolean systemQamPressed; @@ -72,6 +86,7 @@ public final class PadBridge { private int homeGuideSlot = -1; private final PadState lastBackQamReport = new PadState(); private final PadState lastWrittenReport = new PadState(); + private final PadState liveReport = new PadState(); private boolean backQamAxesBlocked; private final java.util.Set backQamWrites = new java.util.HashSet<>(); private long backQamDelayMs, backQamWriteAt; @@ -79,6 +94,11 @@ public final class PadBridge { private int chordButton = -1; private boolean chordButtonPressed; private boolean qamTapPressed; + private boolean guideTapPressed; + private int guideTapGeneration; + private boolean vrStickTapPressed; + private int vrStickTapGeneration; + private boolean routedQamPressed; private int qamChordGeneration; private volatile Runnable onPlayerInput; private Runnable onChordInput; @@ -119,8 +139,36 @@ public final class PadBridge { for (int slot = 1; slot < SLOTS; slot++) playerStates[slot] = new PadState(); } - public void setSelectSteam(boolean on) { - selectSteam = on; + public synchronized void setInputRouter(InputRouter router) { + if (inputRouter == router) return; + for (InputRouter.PadSource route : physicalRoutes) if (route != null) route.close(); + for (TouchSource source : touchSources.values()) if (source.route != null) source.route.close(); + inputRouter = router; + for (int slot = 0; slot < SLOTS; slot++) physicalRoutes[slot] = router == null ? null : router.pad(InputRole.PHYSICAL, stateFor(slot)); + for (TouchSource source : touchSources.values()) source.route = router == null || source.routed ? null : router.pad(source.role, source.raw); + clearBackQamWrites(); + if (open) publish(); + for (int slot = 1; slot < SLOTS; slot++) if (playerWriters[slot] != null) publish(slot); + } + + public synchronized boolean hasInputRouter() { return inputRouter != null; } + + public synchronized InputRouter getInputRouter() { return inputRouter; } + + public synchronized void setInputProfile(InputProfile profile) { + if (inputRouter == null) return; + inputRouter.setProfile(profile); + clearBackQamWrites(); + if (open) publish(); + for (int slot = 1; slot < SLOTS; slot++) if (playerWriters[slot] != null) publish(slot); + } + + public synchronized void setInputEnabled(boolean enabled) { + if (inputRouter == null || !inputRouter.setEnabled(enabled)) return; + if (!enabled) clearBackQamWrites(); + if (open) publish(); + for (int slot = 1; slot < SLOTS; slot++) if (playerWriters[slot] != null) publish(slot); + DeckControls.INSTANCE.refresh(); } public void setExcluded(java.util.function.IntPredicate devices) { @@ -147,10 +195,18 @@ public final class PadBridge { homeGuidePressed = false; clearBackQamWrites(); routedTouch.clear(); + for (TouchSource source : touchSources.values()) { + source.raw.clear(); + if (source.route != null) source.route.release(); + } systemGuidePressed = false; systemQamPressed = false; backQamPressed = false; qamTapPressed = false; + guideTapPressed = false; + guideTapGeneration++; + vrStickTapPressed = false; + vrStickTapGeneration++; qamChordActive = false; chordButtonPressed = false; qamChordGeneration++; @@ -168,6 +224,16 @@ public final class PadBridge { } public synchronized void stop() { + guideTapPressed = false; + guideTapGeneration++; + vrStickTapPressed = false; + vrStickTapGeneration++; + if (inputRouter != null) inputRouter.releaseAll(); + for (TouchSource source : touchSources.values()) if (source.route != null) source.route.close(); + touchSources.clear(); + for (InputRouter.PadSource route : physicalRoutes) if (route != null) route.close(); + Arrays.fill(physicalRoutes, null); + inputRouter = null; homeGuidePressed = false; homeGuideSlot = -1; clearBackQamWrites(); @@ -314,6 +380,7 @@ public final class PadBridge { if (slot < 0) return false; int side = JoyCon.side(event.getDevice()); if (side >= 0 && partners[slot] != NO_CONTROLLER && JoyCon.isRail(side, event.getScanCode())) return true; + long timing = InputTiming.begin(); PadState pad = stateFor(slot); boolean pressed = event.getAction() == KeyEvent.ACTION_DOWN; switch (event.getKeyCode()) { @@ -324,9 +391,6 @@ public final class PadBridge { case KeyEvent.KEYCODE_BUTTON_L1: pad.press(4, pressed); break; case KeyEvent.KEYCODE_BUTTON_R1: pad.press(5, pressed); break; case KeyEvent.KEYCODE_BUTTON_SELECT: - if (selectSteam) pad.press(PadState.GUIDE, pressed); - else pad.press(6, pressed); - break; case KeyEvent.KEYCODE_BACK: pad.press(6, pressed); break; case KeyEvent.KEYCODE_BUTTON_START: case KeyEvent.KEYCODE_MENU: pad.press(7, pressed); break; @@ -355,6 +419,7 @@ public final class PadBridge { statButtons++; scheduleStats(); publish(slot); + InputTiming.end(timing); return true; } @@ -422,6 +487,7 @@ public final class PadBridge { if (event.getAction() != MotionEvent.ACTION_MOVE) return false; int slot = slotFor(event.getDevice()); if (slot < 0) return false; + long timing = InputTiming.begin(); PadState pad = stateFor(slot); noteDevice(event.getDevice(), slot); statAxes++; @@ -438,6 +504,7 @@ public final class PadBridge { if (chordStickInput) noteChordInput(slot); if (Math.max(Math.abs(x), Math.abs(y)) > 0.5f) notePlayerInput(); publish(slot); + InputTiming.end(timing); return true; } float lx = event.getAxisValue(MotionEvent.AXIS_X); @@ -476,6 +543,7 @@ public final class PadBridge { notePlayerInput(); } publish(slot); + InputTiming.end(timing); return true; } @@ -493,41 +561,70 @@ public final class PadBridge { public synchronized void setTouchRoute(TouchRoute route) { touchRoute = route; + touchMouseActive = false; routedTouch.clear(); } public synchronized void applyTouch(java.util.function.Consumer mutation) { - applyTouch(0, mutation); + applyTouch(defaultTouchSource, InputRole.PRIMARY, 0, mutation); } public synchronized void applyTouch(int stickWrites, java.util.function.Consumer mutation) { + applyTouch(defaultTouchSource, InputRole.PRIMARY, stickWrites, mutation); + } + + public synchronized void applyTouch(Object owner, InputRole role, java.util.function.Consumer mutation) { + applyTouch(owner, role, 0, mutation); + } + + public synchronized void applyTouch(Object owner, InputRole role, int stickWrites, java.util.function.Consumer mutation) { if (!builtinPad) return; - if (touchRoute != null && touchRoute.active()) { - mutation.accept(routedTouch); - touchRoute.accept(routedTouch); - boolean changed = false; - for (int button : ROUTED_PASS_THROUGH) { - boolean down = routedTouch.isDown(button); - if (state.isDown(button) == down) continue; - state.press(button, down); - changed = true; + long timing = InputTiming.begin(); + TouchSource source = touchSources.get(owner); + if (source == null) { + source = new TouchSource(role); + if (inputRouter != null) source.route = inputRouter.pad(role); + touchSources.put(owner, source); + } + float lx = source.raw.leftX, ly = source.raw.leftY, rx = source.raw.rightX, ry = source.raw.rightY; + mutation.accept(source.raw); + if (inputRouter == null && (touchRoute == null || !touchRoute.active())) { + if ((stickWrites & LEFT_STICK) != 0 || source.raw.leftX != lx || source.raw.leftY != ly) { + retainedSticks[0] &= ~LEFT_STICK; + state.leftX = state.leftY = 0f; } - if (changed) { - activeControllerId = NO_CONTROLLER; - slotControllers.set(0, NO_CONTROLLER); - publish(); + if ((stickWrites & RIGHT_STICK) != 0 || source.raw.rightX != rx || source.raw.rightY != ry) { + retainedSticks[0] &= ~RIGHT_STICK; + state.rightX = state.rightY = 0f; } - return; } - routedTouch.clear(); - retainedSticks[0] &= ~stickWrites; - mutateState(mutation); activeControllerId = NO_CONTROLLER; slotControllers.set(0, NO_CONTROLLER); notePlayerInput(); statOnScreen++; scheduleStats(); publish(); + InputTiming.end(timing); + } + + public synchronized void releaseTouch(Object owner) { + TouchSource source = touchSources.remove(owner); + if (source == null) return; + if (source.route != null) source.route.close(); + publish(); + } + + public synchronized void applyRoutedTouch(Object owner, InputRole role, java.util.function.Consumer mutation) { + if (!builtinPad) return; + TouchSource source = touchSources.get(owner); + if (source == null) { + source = new TouchSource(role); + source.routed = true; + touchSources.put(owner, source); + } + mutation.accept(source.raw); + notePlayerInput(); + publish(); } public synchronized void applyExternal(java.util.function.Consumer mutation, boolean changed) { @@ -552,16 +649,30 @@ public final class PadBridge { } public synchronized void releaseAll() { + guideTapGeneration++; + vrStickTapPressed = false; + vrStickTapGeneration++; + routedQamPressed = false; + systemGuidePressed = false; + systemQamPressed = false; + guideTapPressed = false; + qamTapPressed = false; + qamChordActive = false; + chordButtonPressed = false; + qamChordGeneration++; + if (inputRouter != null) inputRouter.releaseAll(); + if (inputRouter == null) for (TouchSource source : touchSources.values()) source.raw.clear(); + routedTouch.clear(); + DeckControls.INSTANCE.suspend(); + if (inputRouter == null) clearPlayerState(0); homeGuidePressed = false; homeGuideSlot = -1; clearBackQamWrites(); - routedTouch.clear(); backQamPressed = false; - releaseBackPaddles(); - clearPlayerState(0); + if (inputRouter == null) releaseBackPaddles(); publish(); for (int slot = 1; slot < SLOTS; slot++) { - clearPlayerState(slot); + if (inputRouter == null) clearPlayerState(slot); if (playerWriters[slot] == null) continue; publish(slot); } @@ -602,7 +713,11 @@ public final class PadBridge { if (!homeGuidePressed) return; clearBackQamWrites(); homeGuidePressed = false; - clearPlayerState(0); + if (inputRouter == null) clearPlayerState(0); + else { + inputRouter.releaseAll(); + DeckControls.INSTANCE.suspend(); + } publish(); } @@ -641,7 +756,11 @@ public final class PadBridge { if (!backQamPressed && backQamWrites.isEmpty()) return; clearBackQamWrites(); backQamPressed = false; - clearPlayerState(0); + if (inputRouter == null) clearPlayerState(0); + else { + inputRouter.releaseAll(); + DeckControls.INSTANCE.suspend(); + } publish(); } @@ -677,6 +796,7 @@ public final class PadBridge { } private void writePad(PadState report) { + liveReport.copyFrom(report); if (backQamAxesBlocked && !hasQamAxes()) backQamAxesBlocked = false; long at = android.os.SystemClock.uptimeMillis(); if (backQamPressed || homeGuidePressed || !backQamWrites.isEmpty()) at += backQamDelayMs; @@ -684,11 +804,11 @@ public final class PadBridge { } private boolean hasQamAxes() { - boolean left = (retainedSticks[0] & 1) != 0 - ? hasChordStickInput(rawSticks[0][0], rawSticks[0][1]) : state.leftX != 0f || state.leftY != 0f; - boolean right = (retainedSticks[0] & 2) != 0 - ? hasChordStickInput(rawSticks[0][2], rawSticks[0][3]) : state.rightX != 0f || state.rightY != 0f; - return left || right || state.leftTrigger != 0f || state.rightTrigger != 0f; + boolean left = (retainedSticks[0] & 1) != 0 && liveReport.leftX == state.leftX && liveReport.leftY == state.leftY + ? hasChordStickInput(rawSticks[0][0], rawSticks[0][1]) : liveReport.leftX != 0f || liveReport.leftY != 0f; + boolean right = (retainedSticks[0] & 2) != 0 && liveReport.rightX == state.rightX && liveReport.rightY == state.rightY + ? hasChordStickInput(rawSticks[0][2], rawSticks[0][3]) : liveReport.rightX != 0f || liveReport.rightY != 0f; + return left || right || liveReport.leftTrigger != 0f || liveReport.rightTrigger != 0f; } private static void clearAxes(PadState pad) { @@ -709,9 +829,9 @@ public final class PadBridge { PadState live = new PadState(); live.copyFrom(report); if (queued) { - live.leftX = state.leftX; live.leftY = state.leftY; - live.rightX = state.rightX; live.rightY = state.rightY; - live.leftTrigger = state.leftTrigger; live.rightTrigger = state.rightTrigger; + live.leftX = liveReport.leftX; live.leftY = liveReport.leftY; + live.rightX = liveReport.rightX; live.rightY = liveReport.rightY; + live.leftTrigger = liveReport.leftTrigger; live.rightTrigger = liveReport.rightTrigger; } boolean held = (backQamPressed && report.isDown(PadState.QAM)) || (homeGuidePressed && report.isDown(PadState.GUIDE)); if ((queued && !held) || backQamAxesBlocked) clearAxes(live); @@ -732,6 +852,34 @@ public final class PadBridge { chordHandler.postDelayed(() -> releaseQamTap(generation), QAM_A_HOLD_MS); } + public synchronized void triggerSteamMenu() { + if (!builtinPad || guideTapPressed) return; + guideTapPressed = true; + int generation = ++guideTapGeneration; + publish(); + chordHandler.postDelayed(() -> { + synchronized (PadBridge.this) { + if (generation != guideTapGeneration) return; + guideTapPressed = false; + publish(); + } + }, 90); + } + + public synchronized void triggerVrStickSwitch() { + if (!builtinPad || vrStickTapPressed) return; + vrStickTapPressed = true; + int generation = ++vrStickTapGeneration; + publish(); + chordHandler.postDelayed(() -> { + synchronized (PadBridge.this) { + if (generation != vrStickTapGeneration) return; + vrStickTapPressed = false; + publish(); + } + }, 120); + } + private synchronized void releaseQamTap(int generation) { if (generation != qamChordGeneration || !qamTapPressed) return; qamTapPressed = false; @@ -856,6 +1004,7 @@ public final class PadBridge { } private void free(int slot) { + if (physicalRoutes[slot] != null) physicalRoutes[slot].release(); Log.i(TAG, "player " + (slot + 1) + " (id " + owners[slot] + ") left"); owners[slot] = NO_CONTROLLER; partners[slot] = NO_CONTROLLER; @@ -909,6 +1058,7 @@ public final class PadBridge { publish(); return; } + PadState output = physicalRoutes[slot] == null ? playerStates[slot] : physicalRoutes[slot].route(playerStates[slot]); FakeInputWriter player = playerWriters[slot]; if (player == null) { player = new FakeInputWriter(fakeInputDir.getAbsolutePath(), slot); @@ -918,31 +1068,60 @@ public final class PadBridge { } playerWriters[slot] = player; } - PadState report = playerStates[slot]; if (slot == homeGuideSlot) { - homeGuideReport.copyFrom(report); + homeGuideReport.copyFrom(output); homeGuideReport.press(PadState.GUIDE, true); - report = homeGuideReport; + output = homeGuideReport; } - player.writePad(report); + player.writePad(output); } private void publish() { if (!builtinPad && !open && owners[0] == NO_CONTROLLER && partners[0] == NO_CONTROLLER) return; + PadState output = physicalRoutes[0] == null ? state : physicalRoutes[0].route(state); + boolean mouseRoute = (!touchSources.isEmpty() || touchMouseActive) && touchRoute != null && touchRoute.active(); + touchMouseActive = mouseRoute && !touchSources.isEmpty(); + if (mouseRoute) routedTouch.clear(); + if (!touchSources.isEmpty()) { + combinedState.copyFrom(output); + for (TouchSource source : touchSources.values()) { + PadState touch = source.route == null ? source.raw : source.route.route(source.raw); + if (mouseRoute) { + routedTouch.merge(touch); + source.passthrough.clear(); + for (int button : ROUTED_PASS_THROUGH) source.passthrough.press(button, touch.isDown(button)); + combinedState.merge(source.passthrough); + } else combinedState.merge(touch); + } + output = combinedState; + } + if (mouseRoute) touchRoute.accept(routedTouch); + boolean guide = systemGuidePressed || qamChordActive || guideTapPressed || homeGuidePressed; + boolean routedQam = output.isDown(PadState.QAM); + boolean routedQamStarted = routedQam && !routedQamPressed; + routedQamPressed = routedQam; + if (routedQamStarted && !nativeQam() && !qamChordActive) { + startChord(PadState.A); + return; + } + boolean qam = nativeQam() && (systemQamPressed || qamTapPressed || backQamPressed); if (!open && !start()) { statDropped++; return; } - boolean guide = systemGuidePressed || qamChordActive || homeGuidePressed; - boolean qam = nativeQam() && (systemQamPressed || qamTapPressed || backQamPressed); - if (!guide && !qam) { - writePad(state); + if (!guide && !qam && !vrStickTapPressed && (!routedQam || nativeQam())) { + writePad(output); return; } - effectiveState.copyFrom(state); + effectiveState.copyFrom(output); + if (!nativeQam()) effectiveState.press(PadState.QAM, false); if (guide) effectiveState.press(PadState.GUIDE, true); if (qam) effectiveState.press(PadState.QAM, true); if (chordButtonPressed) effectiveState.press(chordButton, true); + if (vrStickTapPressed) { + effectiveState.press(PadState.L3, true); + effectiveState.press(PadState.R3, true); + } writePad(effectiveState); } diff --git a/app/src/main/java/com/droiddeck/launcher/input/PadState.kt b/app/src/main/java/com/droiddeck/launcher/input/PadState.kt index d337a2a320..a1261dcd8b 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/PadState.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/PadState.kt @@ -36,6 +36,19 @@ class PadState { buttons = 0 } + fun merge(other: PadState) { + buttons = buttons or other.buttons + up = up || other.up; right = right || other.right; down = down || other.down; left = left || other.left + leftTrigger = maxOf(leftTrigger, other.leftTrigger) + rightTrigger = maxOf(rightTrigger, other.rightTrigger) + if (other.leftX * other.leftX + other.leftY * other.leftY > leftX * leftX + leftY * leftY) { + leftX = other.leftX; leftY = other.leftY + } + if (other.rightX * other.rightX + other.rightY * other.rightY > rightX * rightX + rightY * rightY) { + rightX = other.rightX; rightY = other.rightY + } + } + companion object { const val A = 0 const val B = 1 diff --git a/app/src/main/java/com/droiddeck/launcher/input/PhysicalInputBindings.kt b/app/src/main/java/com/droiddeck/launcher/input/PhysicalInputBindings.kt new file mode 100644 index 0000000000..fcb883a997 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/input/PhysicalInputBindings.kt @@ -0,0 +1,89 @@ +package com.droiddeck.launcher.input + +import android.view.InputDevice +import android.view.KeyEvent + +data class PhysicalInputKey(val device: String, val name: String, val keyCode: Int, val scanCode: Int) { + val label: String get() = "$name · ${KeyEvent.keyCodeToString(keyCode).removePrefix("KEYCODE_")} ($scanCode)" + + companion object { + fun identity(device: InputDevice) = "${device.vendorId}:${device.productId}:${device.descriptor.ifBlank { device.name }}" + + fun capture(event: KeyEvent): PhysicalInputKey? { + val device = event.device ?: return null + if (device.isVirtual || PadBridge.isSystemVirtualInputName(device.name)) return null + when (event.keyCode) { + KeyEvent.KEYCODE_VOLUME_UP, KeyEvent.KEYCODE_VOLUME_DOWN, KeyEvent.KEYCODE_VOLUME_MUTE, + KeyEvent.KEYCODE_HOME, KeyEvent.KEYCODE_POWER, KeyEvent.KEYCODE_SLEEP, + KeyEvent.KEYCODE_WAKEUP, KeyEvent.KEYCODE_APP_SWITCH -> return null + } + if (event.keyCode == KeyEvent.KEYCODE_UNKNOWN && event.scanCode <= 0) return null + return PhysicalInputKey(identity(device), device.name, event.keyCode, event.scanCode) + } + } +} + +data class PhysicalInputBinding(val key: PhysicalInputKey, val action: InputAction) + +class PhysicalInputBindings(private val router: InputRouter) { + private class Entry(val binding: PhysicalInputBinding, val channel: InputRouter.Channel) { + var heldDevice = -1 + var suppressed = false + } + private var entries = emptyList() + private val released = ArrayList() + private val devices = HashMap() + + fun configure(bindings: List) { + if (entries.map { it.binding } == bindings) return + for (entry in entries) { + entry.channel.close() + if (entry.heldDevice != -1) released.add(entry) + } + entries = bindings.map { Entry(it, router.channel(InputRole.PHYSICAL, InputControl.A, it.action)) } + } + + fun onKey(event: KeyEvent, enabled: Boolean): Boolean { + if (event.action != KeyEvent.ACTION_DOWN && event.action != KeyEvent.ACTION_UP) return false + for (i in released.indices.reversed()) { + val old = released[i] + if (old.heldDevice == event.deviceId && old.binding.key.keyCode == event.keyCode && old.binding.key.scanCode == event.scanCode) { + if (event.action == KeyEvent.ACTION_UP) released.removeAt(i) + return true + } + } + for (entry in entries) { + val key = entry.binding.key + if (key.keyCode != event.keyCode || key.scanCode != event.scanCode) continue + if (entry.heldDevice != -1 && entry.heldDevice == event.deviceId) { + if (event.action == KeyEvent.ACTION_UP) { + entry.channel.update(0f); entry.heldDevice = -1; entry.suppressed = false + } + return true + } + if (!enabled || event.action != KeyEvent.ACTION_DOWN || entry.binding.action is InputAction.Native) continue + val identity = devices[event.deviceId] ?: event.device?.let { PhysicalInputKey.identity(it) }?.also { devices[event.deviceId] = it } ?: continue + if (identity != key.device) continue + entry.heldDevice = event.deviceId + entry.suppressed = event.repeatCount != 0 + if (!entry.suppressed) entry.channel.update(1f) + return true + } + return false + } + + fun release() { + for (entry in entries) { + entry.channel.release() + if (entry.heldDevice != -1) entry.suppressed = true + } + } + + fun disconnected(id: Int) { + devices.remove(id) + released.removeAll { it.heldDevice == id } + for (entry in entries) if (entry.heldDevice == id) { entry.channel.release(); entry.heldDevice = -1 } + } + + fun close() { entries.forEach { it.channel.close() }; entries = emptyList(); released.clear(); devices.clear() } +} diff --git a/app/src/main/java/com/droiddeck/launcher/input/PhysicalShortcuts.kt b/app/src/main/java/com/droiddeck/launcher/input/PhysicalShortcuts.kt index a79fa8c706..11e76cb4ad 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/PhysicalShortcuts.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/PhysicalShortcuts.kt @@ -105,6 +105,10 @@ object PhysicalShortcuts { write(context, read(context).filterNot { it == binding }) } + fun restoreDefault(context: Context, action: Action) { + write(context, read(context).filterNot { it.action == action }) + } + private fun write(context: Context, bindings: List) { val array = JSONArray() bindings.forEach { diff --git a/app/src/main/java/com/droiddeck/launcher/input/ScreenKey.kt b/app/src/main/java/com/droiddeck/launcher/input/ScreenKey.kt deleted file mode 100644 index 4223754320..0000000000 --- a/app/src/main/java/com/droiddeck/launcher/input/ScreenKey.kt +++ /dev/null @@ -1,52 +0,0 @@ -package com.droiddeck.launcher.input - -import android.content.Context - -fun screenKeyLabel(context: Context, codes: List): String = - codes.filter { it != 0 }.joinToString("+") { short(context, it) } - -private fun short(context: Context, code: Int): String = when (code) { - 28 -> "⏎" - 14 -> "⌫" - 15 -> "Tab" - 57 -> "Space" - 58 -> "Caps" - 42, 54 -> "Shift" - 29, 97 -> "Ctrl" - 56 -> "Alt" - 100 -> "AltGr" - 125 -> "Super" - else -> pcKeyLabel(context, code) -} - -const val SCREEN_KEY_PILL = 0 -const val SCREEN_KEY_ROUND = 1 -const val SCREEN_KEY_CIRCLE = 2 -const val SCREEN_KEY_RECT = 3 -const val SCREEN_KEY_TRIGGER = 4 -const val SCREEN_KEY_DPAD = 5 -const val SCREEN_KEY_STICK = 6 - -val SCREEN_KEY_DPAD_DIRS = listOf("↑", "→", "↓", "←") -val SCREEN_KEY_STICK_DIRS = listOf("↑", "↗", "→", "↘", "↓", "↙", "←", "↖") - -fun screenKeyDirections(shape: Int): List = when (shape) { - SCREEN_KEY_DPAD -> SCREEN_KEY_DPAD_DIRS - SCREEN_KEY_STICK -> SCREEN_KEY_STICK_DIRS - else -> emptyList() -} - -data class ScreenKey( - val codes: List, - val x: Float, - val y: Float, - val size: Int, - val glyph: String = "", - val text: String = "", - val image: String = "", - val opacity: Float = 1f, - val shape: Int = SCREEN_KEY_PILL, - val toggle: Boolean = false, - val labels: Boolean = true, -) { -} diff --git a/app/src/main/java/com/droiddeck/launcher/input/ScreenKeys.kt b/app/src/main/java/com/droiddeck/launcher/input/ScreenKeys.kt deleted file mode 100644 index 2e27eecbe6..0000000000 --- a/app/src/main/java/com/droiddeck/launcher/input/ScreenKeys.kt +++ /dev/null @@ -1,1098 +0,0 @@ -package com.droiddeck.launcher.input - -import android.content.Context -import android.graphics.Bitmap -import android.graphics.BitmapFactory -import android.graphics.Canvas -import android.graphics.Color -import android.graphics.Paint -import android.graphics.PointF -import android.graphics.Rect -import android.graphics.RectF -import android.graphics.Typeface -import android.os.Build -import android.view.MotionEvent -import android.view.View -import android.view.WindowInsets -import java.io.File -import kotlin.math.abs -import kotlin.math.hypot -import kotlin.math.round - -class ScreenKeys(context: Context, private val sendKey: (Int, Boolean) -> Unit) : View(context) { - private class Entry(val codes: List) { - var glyph = "" - var text = "" - var image = "" - var bitmap: Bitmap? = null - var bitmapFor = "" - var x = 0.5f - var y = 0.5f - var size = DEFAULT_SIZE - var opacity = 1f - var shape = SCREEN_KEY_PILL - var toggle = false - var labels = true - var armed = -1 - var cx = 0f - var cy = 0f - var halfW = 0f - var halfH = 0f - } - - private val entries = mutableListOf() - private val down = HashMap() - private val latched = mutableListOf() - private val dpadDown = HashMap() - private val stickEntries = HashMap() - private val stickCodes = HashMap>() - private val held = HashMap() - private var forwardedPointerBits = 0 - private var addX = -1f - private var addY = -1f - private var pressX = 0f - private var pressY = 0f - private var guideX = -1f - private var guideY = -1f - private val addPress = Runnable { addX = pressX; addY = pressY; onAdd?.invoke() } - private var editing = false - val isEditing get() = editing - private var selected: Entry? = null - private var dragX = 0f - private var dragY = 0f - private var dragging = false - private var moved = false - private var toolbarDrag = false - private var safe = Rect() - private var tint = ControllerPrefs.STEAM_BLUE - - init { - ControllerPrefs.screenKeys(context).forEach { key -> entries += entryOf(key) } - } - - var followSteam = false - set(value) { - if (field == value) return - field = value - invalidate() - } - - var steamStyle: () -> Pair? = { null } - - var onAdd: (() -> Unit)? = null - var onSelect: ((List) -> Unit)? = null - var onEditingChanged: ((Boolean) -> Unit)? = null - var onTouchMiss: ((MotionEvent) -> Unit)? = null - - private val fillPaint = Paint(Paint.ANTI_ALIAS_FLAG) - private val strokePaint = Paint(Paint.ANTI_ALIAS_FLAG).apply { style = Paint.Style.STROKE } - private val ringPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply { style = Paint.Style.STROKE } - private val textPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply { - textAlign = Paint.Align.CENTER - typeface = Typeface.DEFAULT_BOLD - } - private val gridPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply { style = Paint.Style.STROKE } - private val rect = RectF() - private val bodyPathRect = RectF() - private val bodyPath = android.graphics.Path() - private val clipPath = android.graphics.Path() - private val imageRect = RectF() - private val bitmapRect = RectF() - - private val capsuleRadius = dp(24f) - private val capsuleGap = dp(62f) - private val toolbar = PointF() - private val addCenter = PointF() - private val doneCenter = PointF() - - fun startEditing() { - if (editing) return - editing = true - releaseAll() - selected = null - placeToolbar() - invalidate() - onEditingChanged?.invoke(true) - } - - fun stopEditing() { - if (!editing) return - releaseAll() - editing = false - selected = null - save() - invalidate() - onEditingChanged?.invoke(false) - } - - fun add(codes: List) { - if (codes.isEmpty()) return - entries.firstOrNull { it.codes == codes }?.let { - selected = it - invalidate() - return - } - val entry = Entry(codes) - if (addX >= 0f && width > 0) { - entry.x = (addX / width).coerceIn(0.05f, 0.95f) - entry.y = (addY / height).coerceIn(0.05f, 0.95f) - } else { - entry.x = (0.30f + 0.08f * (entries.size % 5)).coerceAtMost(0.70f) - entry.y = 0.62f - } - addX = -1f - addY = -1f - entries += entry - if (width > 0) measure(entry) - selected = entry - save() - invalidate() - } - - fun remove(codes: List) { - val entry = entries.firstOrNull { it.codes == codes } ?: return - entries.remove(entry) - if (selected === entry) selected = null - save() - invalidate() - } - - fun setCodes(from: List, to: List): ScreenKey? { - if (to.isEmpty()) return null - val index = entries.indexOfFirst { it.codes == from } - if (index < 0) return null - val old = entries[index] - if (to != from && entries.any { it !== old && it.codes == to }) return null - val fresh = Entry(to).also { - it.glyph = old.glyph - it.text = old.text - it.image = old.image - it.bitmap = old.bitmap - it.bitmapFor = old.bitmapFor - it.x = old.x - it.y = old.y - it.size = old.size - it.opacity = old.opacity - it.shape = old.shape - it.toggle = old.toggle - it.labels = old.labels - } - entries[index] = fresh - measure(fresh) - if (selected === old) selected = fresh - save() - invalidate() - return stateOf(fresh.codes) - } - - fun setText(codes: List, text: String) { - val entry = entries.firstOrNull { it.codes == codes } ?: return - entry.text = text - measure(entry) - save() - invalidate() - } - - fun setImage(codes: List, image: String) { - val entry = entries.firstOrNull { it.codes == codes } ?: return - val previous = entry.image - entry.image = image - entry.bitmap = null - entry.bitmapFor = "" - if (previous.isNotEmpty() && previous != image) File(File(context.filesDir, IMAGE_DIR), previous).delete() - measure(entry) - save() - invalidate() - } - - fun resize(codes: List, size: Int) { - val entry = entries.firstOrNull { it.codes == codes } ?: return - entry.size = size.coerceIn(MIN_SIZE, MAX_SIZE) - measure(entry) - save() - invalidate() - } - - fun setOpacity(codes: List, opacity: Float) { - val entry = entries.firstOrNull { it.codes == codes } ?: return - entry.opacity = opacity.coerceIn(0.2f, 1f) - save() - invalidate() - } - - fun setShape(codes: List, shape: Int) { - val entry = entries.firstOrNull { it.codes == codes } ?: return - entry.shape = shape.coerceIn(SCREEN_KEY_PILL, SCREEN_KEY_STICK) - measure(entry) - save() - invalidate() - } - - fun setToggle(codes: List, toggle: Boolean) { - val entry = entries.firstOrNull { it.codes == codes } ?: return - entry.toggle = toggle - if (!toggle && latched.remove(entry)) release(entry) - save() - invalidate() - } - - fun setLabels(codes: List, labels: Boolean) { - val entry = entries.firstOrNull { it.codes == codes } ?: return - entry.labels = labels - save() - invalidate() - } - - fun setDirection(codes: List, index: Int, code: Int): ScreenKey? { - val entry = entries.firstOrNull { it.codes == codes } ?: entries.firstOrNull { it.codes == trimmed(codes) } ?: return null - val next = entry.codes.toMutableList() - while (next.size <= index) next += 0 - next[index] = code - val nextCodes = trimmed(next) - if (entries.any { it !== entry && it.codes == nextCodes }) return null - val updated = Entry(nextCodes).also { - it.glyph = entry.glyph - it.text = entry.text - it.image = entry.image - it.bitmap = entry.bitmap - it.bitmapFor = entry.bitmapFor - it.x = entry.x - it.y = entry.y - it.size = entry.size - it.opacity = entry.opacity - it.shape = entry.shape - it.toggle = entry.toggle - it.labels = entry.labels - } - entries[entries.indexOf(entry)] = updated - measure(updated) - if (selected === entry) selected = updated - save() - invalidate() - return stateOf(updated.codes) - } - - private fun trimmed(codes: List): List = codes.dropLastWhile { it == 0 }.ifEmpty { listOf(0) } - - fun stateOf(codes: List): ScreenKey? = entries.firstOrNull { it.codes == codes }?.let { - ScreenKey(it.codes, it.x, it.y, it.size, it.glyph, it.text, it.image, it.opacity, it.shape, it.toggle, it.labels) - } - - fun deselect() { - if (selected == null) return - selected = null - invalidate() - } - - override fun onAttachedToWindow() { - super.onAttachedToWindow() - refreshAppearance() - } - - fun refreshAppearance() { - tint = ControllerPrefs.read(context).tint - invalidate() - } - - override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) { - super.onSizeChanged(w, h, oldw, oldh) - entries.forEach { measure(it) } - placeToolbar() - } - - override fun onApplyWindowInsets(insets: WindowInsets): WindowInsets { - val cutout = if (Build.VERSION.SDK_INT >= 28) insets.displayCutout else null - val fresh = Rect( - cutout?.safeInsetLeft ?: 0, cutout?.safeInsetTop ?: 0, - cutout?.safeInsetRight ?: 0, cutout?.safeInsetBottom ?: 0, - ) - if (fresh != safe) { - safe = fresh - placeToolbar() - invalidate() - } - return super.onApplyWindowInsets(insets) - } - - override fun onDetachedFromWindow() { - releaseAll() - super.onDetachedFromWindow() - } - - override fun onVisibilityChanged(changedView: View, visibility: Int) { - super.onVisibilityChanged(changedView, visibility) - if (visibility != VISIBLE) releaseAll() - } - - override fun setVisibility(visibility: Int) { - super.setVisibility(visibility) - if (visibility != VISIBLE) releaseAll() - } - - fun releaseAll() { - dpadDown.values.forEach { releaseKey(it) } - dpadDown.clear() - entries.forEach { it.armed = -1 } - stickCodes.values.flatten().forEach { if (it != 0) releaseKey(it) } - stickCodes.clear() - stickEntries.clear() - down.values.forEach { release(it) } - down.clear() - latched.toList().forEach { release(it) } - latched.clear() - invalidate() - } - - private fun snap(value: Float) = if (width > 0) round(value / (width / 100f)) * (width / 100f) else value - - private fun placeToolbar() { - if (width == 0 || height == 0) return - if (toolbar.x == 0f && toolbar.y == 0f) { - toolbar.set(width - safe.right - capsuleGap - capsuleRadius, height - safe.bottom - capsuleRadius - dp(20f)) - } - addCenter.set(toolbar.x - capsuleGap / 2f, toolbar.y) - doneCenter.set(toolbar.x + capsuleGap / 2f, toolbar.y) - } - - private fun entryOf(key: ScreenKey): Entry = Entry(key.codes).also { - it.glyph = key.glyph - it.text = key.text - it.image = key.image - it.x = key.x - it.y = key.y - it.size = key.size - it.opacity = key.opacity - it.shape = key.shape - it.toggle = key.toggle - it.labels = key.labels - } - - private fun label(entry: Entry) = when { - entry.text.isNotEmpty() -> entry.text - entry.glyph.isNotEmpty() -> entry.glyph - else -> screenKeyLabel(context, entry.codes) - } - - private fun bitmapFor(entry: Entry): Bitmap? { - if (entry.image.isEmpty()) return null - if (entry.bitmapFor == entry.image) return entry.bitmap - val file = File(File(context.filesDir, IMAGE_DIR), entry.image) - val bitmap = if (file.isFile) BitmapFactory.decodeFile(file.absolutePath) else null - entry.bitmap = bitmap - entry.bitmapFor = entry.image - return bitmap - } - - private fun measure(entry: Entry) { - val h = dp(entry.size.toFloat()) - entry.halfH = h / 2f - entry.halfW = when { - entry.shape == SCREEN_KEY_CIRCLE -> entry.halfH - entry.shape == SCREEN_KEY_TRIGGER -> entry.halfH * 1.7f - entry.shape == SCREEN_KEY_DPAD || entry.shape == SCREEN_KEY_STICK -> entry.halfH * 1.15f - entry.image.isNotEmpty() -> entry.halfH * 1.15f - hasIcon(entry) -> entry.halfH * 1.15f - else -> { - val chars = label(entry).length - entry.halfH * (0.55f * chars + 1.05f).coerceIn(1.7f, 4.6f) - } - } - entry.cx = entry.x * width - entry.cy = entry.y * height - } - - private fun hasIcon(entry: Entry): Boolean = - entry.text.isEmpty() && entry.glyph.isEmpty() && entry.codes.size == 1 && ScreenKeyIcons.has(entry.codes[0]) - - private fun unit() = minOf(width / 1280f, height / 720f) - - private fun accent(): Pair { - val style = if (followSteam) steamStyle() else null - return (style?.first ?: tint) to (style?.second ?: 1f) - } - - private fun tinted(color: Int, alpha: Float): Int { - val scale = accent().second - return Color.argb( - (alpha * scale * 255f).toInt().coerceIn(0, 255), - Color.red(color), Color.green(color), Color.blue(color), - ) - } - - private fun triggerPath(entry: Entry, path: android.graphics.Path, inset: Float = 0f) { - val lean = (entry.halfH - inset) * 0.6f - val left = entry.cx - entry.halfW + inset - val right = entry.cx + entry.halfW - inset - val top = entry.cy - entry.halfH + inset - val bottom = entry.cy + entry.halfH - inset - path.reset() - path.moveTo(left + lean, top) - path.lineTo(right, top) - path.lineTo(right - lean, bottom) - path.lineTo(left, bottom) - path.close() - } - - private fun body(canvas: Canvas, entry: Entry, radius: Float, paint: Paint, inset: Float = 0f) { - if (entry.shape != SCREEN_KEY_TRIGGER) { - canvas.drawRoundRect(rect, radius, radius, paint) - return - } - triggerPath(entry, bodyPath, inset) - canvas.drawPath(bodyPath, paint) - } - - private fun drawDpad(canvas: Canvas, entry: Entry) { - val color = accent().first - val alpha = entry.opacity - val r = minOf(entry.halfW, entry.halfH) - val t = r * 0.42f - val hairline = maxOf(dp(1.2f), entry.halfH * 0.032f) - val engaged = entry.armed >= 0 - crossPath(entry, bodyPath, r, t) - fillPaint.style = Paint.Style.FILL - fillPaint.color = tinted(color, if (entry.armed >= 0) 0.26f * alpha else 0.10f * alpha) - canvas.drawPath(bodyPath, fillPaint) - if (entry.armed >= 0) { - armRect(entry, bodyPathRect, entry.armed, r, t) - fillPaint.color = tinted(color, 0.55f * alpha) - canvas.drawRoundRect(bodyPathRect, t * 0.6f, t * 0.6f, fillPaint) - } - strokePaint.strokeJoin = Paint.Join.ROUND - strokePaint.strokeWidth = if (engaged) hairline * 1.6f else hairline - strokePaint.color = tinted(color, (if (editing) 0.90f else 0.80f) * alpha) - canvas.drawPath(bodyPath, strokePaint) - textPaint.setShadowLayer(0f, 0f, 0f, 0) - fillPaint.color = tinted(color, if (editing) 0.90f else 0.80f) - DPAD_DIRS.forEachIndexed { index, dir -> - val on = entry.armed == index - val code = entry.codes.getOrNull(index) ?: 0 - val name = if (code == 0) "" else screenKeyLabel(context, listOf(code)) - if (entry.labels && name.isNotEmpty()) { - textPaint.color = if (on) Color.WHITE else tinted(color, alpha) - textPaint.textSize = dp(48f) - val fit = 48f * (t * 1.9f) / textPaint.measureText(name).coerceAtLeast(1f) - textPaint.textSize = minOf(fit, t * 1.7f).coerceIn(dp(8f), dp(60f)) - val tx = entry.cx + dir.first * (r - t * 0.6f) - val ty = entry.cy + dir.second * (r - t * 0.6f) - canvas.drawText(name, tx, ty - (textPaint.ascent() + textPaint.descent()) / 2f, textPaint) - } else { - fillPaint.color = if (on) Color.WHITE else tinted(color, if (editing) 0.90f else 0.80f) - arrow(canvas, fillPaint, entry.cx, entry.cy, r - t * 0.55f, dir.first, dir.second, t * 0.8f) - } - } - if (editing && selected === entry) { - ringPaint.strokeWidth = hairline * 0.8f - ringPaint.color = tinted(color, 0.9f) - ringPaint.style = Paint.Style.STROKE - canvas.drawCircle(entry.cx, entry.cy, r + hairline * 3f, ringPaint) - } - } - - private fun crossPath(entry: Entry, path: android.graphics.Path, r: Float, t: Float) { - val cx = entry.cx - val cy = entry.cy - path.reset() - path.moveTo(cx - t, cy - r) - path.lineTo(cx + t, cy - r) - path.lineTo(cx + t, cy - t) - path.lineTo(cx + r, cy - t) - path.lineTo(cx + r, cy + t) - path.lineTo(cx + t, cy + t) - path.lineTo(cx + t, cy + r) - path.lineTo(cx - t, cy + r) - path.lineTo(cx - t, cy + t) - path.lineTo(cx - r, cy + t) - path.lineTo(cx - r, cy - t) - path.lineTo(cx - t, cy - t) - path.close() - } - - private fun armRect(entry: Entry, rect: RectF, index: Int, r: Float, t: Float) { - when (index) { - 0 -> rect.set(entry.cx - t, entry.cy - r, entry.cx + t, entry.cy - t) - 1 -> rect.set(entry.cx + t, entry.cy - t, entry.cx + r, entry.cy + t) - 2 -> rect.set(entry.cx - t, entry.cy + t, entry.cx + t, entry.cy + r) - else -> rect.set(entry.cx - r, entry.cy - t, entry.cx - t, entry.cy + t) - } - } - - private fun arrow(canvas: Canvas, paint: Paint, cx: Float, cy: Float, dist: Float, dx: Float, dy: Float, size: Float) { - val tipX = cx + dx * dist - val tipY = cy + dy * dist - val baseX = cx + dx * (dist - size) - val baseY = cy + dy * (dist - size) - val nx = -dy * size * 0.62f - val ny = dx * size * 0.62f - bodyPath.reset() - bodyPath.moveTo(tipX, tipY) - bodyPath.lineTo(baseX + nx, baseY + ny) - bodyPath.lineTo(baseX - nx, baseY - ny) - bodyPath.close() - canvas.drawPath(bodyPath, paint) - } - - private fun armOf(entry: Entry, x: Float, y: Float): Int { - val dx = x - entry.cx - val dy = y - entry.cy - return if (abs(dx) > abs(dy)) (if (dx > 0f) 1 else 3) else (if (dy > 0f) 2 else 0) - } - - private fun drawStick(canvas: Canvas, entry: Entry) { - val color = accent().first - val alpha = entry.opacity - val engaged = entry.armed >= 0 - val radius = minOf(entry.halfW, entry.halfH) - val hairline = maxOf(dp(1.2f), entry.halfH * 0.032f) - fillPaint.style = Paint.Style.FILL - fillPaint.color = tinted(color, 0.14f * alpha) - canvas.drawCircle(entry.cx, entry.cy, radius - hairline, fillPaint) - strokePaint.strokeWidth = if (engaged) hairline * 1.6f else hairline - strokePaint.color = tinted(color, (if (editing) 0.90f else 0.80f) * alpha) - canvas.drawCircle(entry.cx, entry.cy, radius - hairline / 2f, strokePaint) - val dir = if (engaged) STICK_DIRS[entry.armed.coerceIn(0, 7)] else 0f to 0f - val knob = radius * 0.36f - fillPaint.color = tinted(color, if (engaged) 0.55f * alpha else 0.24f * alpha) - canvas.drawCircle(entry.cx + dir.first * radius * 0.40f, entry.cy + dir.second * radius * 0.40f, knob, fillPaint) - strokePaint.strokeWidth = hairline - strokePaint.color = tinted(color, (if (editing) 0.90f else 0.80f) * alpha) - canvas.drawCircle(entry.cx + dir.first * radius * 0.40f, entry.cy + dir.second * radius * 0.40f, knob, strokePaint) - textPaint.setShadowLayer(0f, 0f, 0f, 0) - STICK_DIRS.forEachIndexed { index, unit -> - val on = entry.armed == index - val code = entry.codes.getOrNull(index) ?: 0 - val name = if (code == 0) "" else screenKeyLabel(context, listOf(code)) - if (entry.labels && name.isNotEmpty()) { - textPaint.color = if (on) Color.WHITE else tinted(color, 0.85f * alpha) - textPaint.textSize = dp(48f) - val fit = 48f * (radius * 0.34f) / textPaint.measureText(name).coerceAtLeast(1f) - textPaint.textSize = minOf(fit, radius * 0.22f).coerceAtLeast(dp(6f)) - val tx = entry.cx + unit.first * radius * 0.66f - val ty = entry.cy + unit.second * radius * 0.66f - canvas.drawText(name, tx, ty - (textPaint.ascent() + textPaint.descent()) / 2f, textPaint) - } else { - fillPaint.color = if (on) Color.WHITE else tinted(color, if (editing) 0.88f else 0.72f) - arrow(canvas, fillPaint, entry.cx, entry.cy, radius * 0.88f, unit.first, unit.second, radius * 0.16f) - } - } - if (editing && selected === entry) { - ringPaint.strokeWidth = hairline * 0.8f - ringPaint.color = tinted(color, 0.9f) - ringPaint.style = Paint.Style.STROKE - canvas.drawCircle(entry.cx, entry.cy, radius + hairline * 3f, ringPaint) - } - } - - private fun directionCodes(entry: Entry, index: Int): List { - if (index < 0) return emptyList() - val code = entry.codes.getOrNull(index) ?: 0 - if (code != 0) return listOf(code) - if (entry.shape != SCREEN_KEY_STICK || index % 2 == 0) return emptyList() - return listOfNotNull( - entry.codes.getOrNull((index + 7) % 8)?.takeIf { it != 0 }, - entry.codes.getOrNull((index + 1) % 8)?.takeIf { it != 0 }, - ) - } - - private fun stickSector(entry: Entry, x: Float, y: Float): Int { - val dx = x - entry.cx - val dy = y - entry.cy - if (hypot(dx, dy) < minOf(entry.halfW, entry.halfH) * 0.24f) return -1 - val degrees = Math.toDegrees(kotlin.math.atan2(dy, dx).toDouble()).toFloat() - val index = (round(degrees / 45f).toInt() + 2) % 8 - return (index + 8) % 8 - } - - private fun radiusOf(entry: Entry): Float = when (entry.shape) { - SCREEN_KEY_ROUND -> entry.halfH * 0.30f - SCREEN_KEY_RECT -> entry.halfH * 0.12f - SCREEN_KEY_TRIGGER -> entry.halfH * 0.45f - else -> entry.halfH - } - - fun takesTouchAt(x: Float, y: Float): Boolean = editing || entryAt(x, y) != null - - private fun entryAt(x: Float, y: Float): Entry? = entries.lastOrNull { entry -> - x >= entry.cx - entry.halfW && x <= entry.cx + entry.halfW && y >= entry.cy - entry.halfH && y <= entry.cy + entry.halfH - } - - private fun inCircle(center: PointF, x: Float, y: Float, radius: Float) = hypot(x - center.x, y - center.y) <= radius - - private fun inToolbar(x: Float, y: Float): Boolean = - editing && x >= toolbar.x - capsuleGap / 2f - capsuleRadius && x <= toolbar.x + capsuleGap / 2f + capsuleRadius && - y >= toolbar.y - capsuleRadius && y <= toolbar.y + capsuleRadius - - override fun onDraw(canvas: Canvas) { - if (editing) drawGrid(canvas) - entries.forEach { draw(canvas, it) } - if (editing) { - drawGuides(canvas) - drawToolbar(canvas) - } - } - - private fun drawGuides(canvas: Canvas) { - if (guideX < 0f && guideY < 0f) return - gridPaint.strokeWidth = dp(1f) - gridPaint.color = tinted(accent().first, 0.9f) - if (guideX >= 0f) canvas.drawLine(guideX, 0f, guideX, height.toFloat(), gridPaint) - if (guideY >= 0f) canvas.drawLine(0f, guideY, width.toFloat(), guideY, gridPaint) - } - - private fun drawGrid(canvas: Canvas) { - if (width == 0 || height == 0) return - val step = width / 100f - gridPaint.strokeWidth = dp(1f) - gridPaint.color = Color.argb(38, 255, 255, 255) - var x = step - while (x < width) { - canvas.drawLine(x, 0f, x, height.toFloat(), gridPaint) - x += step - } - var y = step - while (y < height) { - canvas.drawLine(0f, y, width.toFloat(), y, gridPaint) - y += step - } - gridPaint.color = Color.argb(96, 255, 255, 255) - gridPaint.strokeWidth = dp(1f) - canvas.drawLine(width / 2f, 0f, width / 2f, height.toFloat(), gridPaint) - canvas.drawLine(0f, height / 2f, width.toFloat(), height / 2f, gridPaint) - } - - private fun draw(canvas: Canvas, entry: Entry) { - if (entry.shape == SCREEN_KEY_DPAD) { - drawDpad(canvas, entry) - return - } - if (entry.shape == SCREEN_KEY_STICK) { - drawStick(canvas, entry) - return - } - val style = if (followSteam) steamStyle() else null - val engaged = down.values.any { it === entry } || latched.contains(entry) - val bitmap = bitmapFor(entry) - val icon = bitmap == null && hasIcon(entry) - val frameless = entry.text.isBlank() && (bitmap != null || icon) - val alpha = entry.opacity - val h2 = entry.halfH * 2f - val radius = radiusOf(entry) - val selectedNow = editing && selected === entry - val hairline = maxOf(dp(1.2f), h2 * 0.032f) - rect.set(entry.cx - entry.halfW, entry.cy - entry.halfH, entry.cx + entry.halfW, entry.cy + entry.halfH) - val inkColor: Int - if (style != null) { - val inkAlpha = (255f * style.second * alpha).toInt().coerceIn(30, 255) - inkColor = Color.argb(inkAlpha, Color.red(style.first), Color.green(style.first), Color.blue(style.first)) - fillPaint.style = Paint.Style.FILL - if (engaged) { - fillPaint.color = Color.argb((inkAlpha * 0.85f).toInt(), 230, 233, 236) - body(canvas, entry, radius, fillPaint) - } - if (frameless) { - if (engaged) { - fillPaint.color = Color.argb((inkAlpha * 0.45f).toInt(), 230, 233, 236) - body(canvas, entry, radius, fillPaint) - } - } else { - strokePaint.color = if (selectedNow) Color.rgb(26, 159, 255) else inkColor - strokePaint.strokeWidth = if (engaged) hairline * 1.6f else hairline - body(canvas, entry, radius, strokePaint) - } - } else { - fillPaint.style = Paint.Style.FILL - if (!frameless) { - fillPaint.color = tinted(BODY, 0.60f * alpha) - canvas.drawRoundRect(rect, radius, radius, fillPaint) - } - if (engaged) { - fillPaint.color = tinted(accent().first, if (frameless) 0.32f * alpha else 0.42f * alpha) - canvas.drawRoundRect(rect, radius, radius, fillPaint) - } - if (!frameless) { - strokePaint.strokeWidth = if (engaged) hairline * 1.6f else hairline - strokePaint.color = tinted( - accent().first, - (if (engaged) 0.96f else if (editing) 0.80f else 0.68f) * alpha, - ) - body(canvas, entry, radius, strokePaint) - } - inkColor = if (engaged) Color.WHITE else tinted(accent().first, alpha) - } - if (editing && selected === entry) { - ringPaint.strokeWidth = hairline * 0.8f - ringPaint.color = tinted(accent().first, 0.9f) - val gap = hairline * 2.4f - rect.set( - entry.cx - entry.halfW - gap, entry.cy - entry.halfH - gap, - entry.cx + entry.halfW + gap, entry.cy + entry.halfH + gap, - ) - if (entry.shape == SCREEN_KEY_TRIGGER) { - body(canvas, entry, radius + gap, ringPaint, gap) - } else { - canvas.drawRoundRect(rect, radius + gap, radius + gap, ringPaint) - } - rect.set(entry.cx - entry.halfW, entry.cy - entry.halfH, entry.cx + entry.halfW, entry.cy + entry.halfH) - } - - if (bitmap != null) { - imageRect.set(rect) - canvas.save() - clipPath.reset() - when (entry.shape) { - SCREEN_KEY_CIRCLE -> clipPath.addCircle( - entry.cx, entry.cy, minOf(entry.halfW, entry.halfH), android.graphics.Path.Direction.CW, - ) - SCREEN_KEY_TRIGGER -> triggerPath(entry, clipPath, 0f) - else -> clipPath.addRoundRect(rect, radius, radius, android.graphics.Path.Direction.CW) - } - canvas.clipPath(clipPath) - val scale = maxOf(imageRect.width() / bitmap.width, imageRect.height() / bitmap.height) - val w = bitmap.width * scale - val h = bitmap.height * scale - bitmapRect.set( - imageRect.centerX() - w / 2f, imageRect.centerY() - h / 2f, - imageRect.centerX() + w / 2f, imageRect.centerY() + h / 2f, - ) - canvas.drawBitmap(bitmap, null, bitmapRect, fillPaint) - canvas.restore() - } - - val name = label(entry) - textPaint.color = inkColor - if (bitmap != null && name.isEmpty()) return - var iconDrawn = false - if (icon) { - val box = minOf(entry.halfW, entry.halfH) * 1.35f - iconDrawn = ScreenKeyIcons.draw(canvas, entry.codes[0], entry.cx, entry.cy, box, textPaint) - textPaint.color = inkColor - } - if (iconDrawn || name.isEmpty()) return - val overImage = bitmap != null - if (overImage) textPaint.color = Color.WHITE - textPaint.setShadowLayer(if (overImage) dp(3f) else 0f, 0f, 0f, Color.argb(200, 0, 0, 0)) - textPaint.textSize = dp(48f) - val available = rect.width() - hairline * 4f - val fit = 48f * available / textPaint.measureText(name).coerceAtLeast(1f) - textPaint.textSize = minOf(fit, entry.halfH * 1.15f).coerceIn(dp(8f), dp(120f)) - canvas.drawText(name, entry.cx, entry.cy - (textPaint.ascent() + textPaint.descent()) / 2f, textPaint) - textPaint.clearShadowLayer() - } - - private fun drawToolbar(canvas: Canvas) { - val (color, _) = accent() - val capsule = RectF( - toolbar.x - capsuleGap / 2f - capsuleRadius, toolbar.y - capsuleRadius, - toolbar.x + capsuleGap / 2f + capsuleRadius, toolbar.y + capsuleRadius, - ) - fillPaint.style = Paint.Style.FILL - fillPaint.color = tinted(color, 0.30f) - canvas.drawRoundRect(capsule, capsuleRadius, capsuleRadius, fillPaint) - strokePaint.strokeWidth = dp(1.5f) - strokePaint.color = tinted(color, 0.85f) - canvas.drawRoundRect(capsule, capsuleRadius, capsuleRadius, strokePaint) - - textPaint.color = Color.WHITE - textPaint.setShadowLayer(0f, 0f, 0f, 0) - textPaint.textSize = capsuleRadius * 1.15f - val plus = "+" - canvas.drawText(plus, addCenter.x, addCenter.y - (textPaint.ascent() + textPaint.descent()) / 2f, textPaint) - val path = android.graphics.Path() - val check = capsuleRadius * 0.42f - path.moveTo(doneCenter.x - check, doneCenter.y) - path.lineTo(doneCenter.x - check * 0.25f, doneCenter.y + check * 0.7f) - path.lineTo(doneCenter.x + check, doneCenter.y - check * 0.7f) - ringPaint.strokeWidth = dp(3f) - ringPaint.strokeCap = Paint.Cap.ROUND - ringPaint.color = Color.WHITE - canvas.drawPath(path, ringPaint) - ringPaint.strokeCap = Paint.Cap.BUTT - } - - override fun onTouchEvent(event: MotionEvent): Boolean = if (editing) editTouch(event) else playTouch(event) - - private fun playTouch(event: MotionEvent): Boolean { - if (event.getToolType(event.actionIndex) == MotionEvent.TOOL_TYPE_MOUSE) return false - when (event.actionMasked) { - MotionEvent.ACTION_DOWN, MotionEvent.ACTION_POINTER_DOWN -> { - val index = event.actionIndex - val entry = entryAt(event.getX(index), event.getY(index)) - if (entry == null) { - if (event.actionMasked == MotionEvent.ACTION_DOWN) return false - forwardedPointerBits = forwardedPointerBits or (1 shl event.getPointerId(index)) - forwardPointers(event, forwardedPointerBits) - return true - } - if (forwardedPointerBits != 0) forwardPointers(event, forwardedPointerBits) - if (entry.shape == SCREEN_KEY_DPAD) { - val arm = armOf(entry, event.getX(index), event.getY(index)) - val code = entry.codes.getOrNull(arm) ?: 0 - if (code == 0) return true - val pid = event.getPointerId(index) - dpadDown.remove(pid)?.let { releaseKey(it) } - dpadDown[pid] = code - entry.armed = arm - pressKey(code) - invalidate() - return true - } - if (entry.shape == SCREEN_KEY_STICK) { - val sector = stickSector(entry, event.getX(index), event.getY(index)) - val pid = event.getPointerId(index) - stickCodes.remove(pid)?.forEach { if (it != 0) releaseKey(it) } - val codes = directionCodes(entry, sector) - stickEntries[pid] = entry - stickCodes[pid] = codes - entry.armed = if (codes.isEmpty()) -1 else sector - codes.forEach { pressKey(it) } - invalidate() - return true - } - if (entry.toggle) { - if (latched.remove(entry)) release(entry) else { - latched += entry - entry.codes.forEach { pressKey(it) } - } - invalidate() - return true - } - if (down.values.any { it === entry }) return true - down[event.getPointerId(index)] = entry - entry.codes.forEach { pressKey(it) } - invalidate() - return true - } - - MotionEvent.ACTION_MOVE -> { - if (forwardedPointerBits != 0) forwardPointers(event, forwardedPointerBits) - if (stickEntries.isEmpty()) return true - repeat(event.pointerCount) { index -> - val pid = event.getPointerId(index) - val entry = stickEntries[pid] ?: return@repeat - val sector = stickSector(entry, event.getX(index), event.getY(index)) - val codes = directionCodes(entry, sector) - val previous = stickCodes[pid].orEmpty() - if (codes != previous) { - previous.filterNot { it in codes }.forEach { releaseKey(it) } - codes.filterNot { it in previous }.forEach { pressKey(it) } - stickCodes[pid] = codes - entry.armed = if (codes.isEmpty()) -1 else sector - invalidate() - } - } - return true - } - - MotionEvent.ACTION_UP, MotionEvent.ACTION_POINTER_UP -> { - val pid = event.getPointerId(event.actionIndex) - if (forwardedPointerBits and (1 shl pid) != 0) { - forwardPointers(event, forwardedPointerBits) - forwardedPointerBits = forwardedPointerBits and (1 shl pid).inv() - if (stickCodes.containsKey(pid) || dpadDown.containsKey(pid)) return true - down.remove(pid)?.let(::release) - return true - } - if (forwardedPointerBits != 0) forwardPointers(event, forwardedPointerBits) - if (stickCodes.containsKey(pid)) { - stickCodes.remove(pid)?.forEach { if (it != 0) releaseKey(it) } - stickEntries.remove(pid)?.armed = -1 - invalidate() - return true - } - val armedCode = dpadDown.remove(pid) - if (armedCode != null) { - releaseKey(armedCode) - entries.firstOrNull { it.armed >= 0 && it.codes.contains(armedCode) }?.armed = -1 - invalidate() - return true - } - val entry = down.remove(pid) - if (entry != null) release(entry) - return true - } - - MotionEvent.ACTION_CANCEL -> { - if (forwardedPointerBits != 0) forwardPointers(event, forwardedPointerBits) - forwardedPointerBits = 0 - stickCodes.values.flatten().forEach { if (it != 0) releaseKey(it) } - stickCodes.clear() - stickEntries.clear() - entries.forEach { it.armed = -1 } - dpadDown.values.forEach { releaseKey(it) } - dpadDown.clear() - entries.forEach { it.armed = -1 } - down.values.forEach { release(it) } - down.clear() - return true - } - } - return true - } - - private fun forwardPointers(event: MotionEvent, pointerIdBits: Int) { - val split = event.pointerSubset(pointerIdBits) ?: return - try { - onTouchMiss?.invoke(split) - } finally { - split.recycle() - } - } - - private fun editTouch(event: MotionEvent): Boolean { - when (event.actionMasked) { - MotionEvent.ACTION_DOWN -> { - val x = event.x - val y = event.y - if (inCircle(addCenter, x, y, capsuleRadius)) { - onAdd?.invoke() - return true - } - if (inCircle(doneCenter, x, y, capsuleRadius)) { - stopEditing() - return true - } - if (inToolbar(x, y)) { - toolbarDrag = true - dragX = toolbar.x - x - dragY = toolbar.y - y - return true - } - val entry = entryAt(x, y) - if (entry == null) { - pressX = x - pressY = y - postDelayed(addPress, 520) - selected = null - invalidate() - return true - } - selected = entry - dragging = true - moved = false - dragX = entry.cx - x - dragY = entry.cy - y - invalidate() - return true - } - - MotionEvent.ACTION_MOVE -> { - if (toolbarDrag) { - val maxX = width - safe.right - capsuleGap / 2f - capsuleRadius - val maxY = height - safe.bottom - capsuleRadius - toolbar.x = (event.x + dragX).coerceIn(safe.left + capsuleGap / 2f + capsuleRadius, maxOf(safe.left + capsuleGap / 2f + capsuleRadius, maxX)) - toolbar.y = (event.y + dragY).coerceIn(safe.top + capsuleRadius, maxOf(safe.top + capsuleRadius, maxY)) - addCenter.set(toolbar.x - capsuleGap / 2f, toolbar.y) - doneCenter.set(toolbar.x + capsuleGap / 2f, toolbar.y) - invalidate() - return true - } - val entry = selected - if (entry == null) { - if (hypot(event.x - pressX, event.y - pressY) > dp(10f)) removeCallbacks(addPress) - return true - } - if (!dragging) return true - if (hypot(event.x - (entry.cx - dragX), event.y - (entry.cy - dragY)) > dp(10f)) removeCallbacks(addPress) - var nextX = snap((event.x + dragX).coerceIn(entry.halfW + safe.left, width - entry.halfW - safe.right)) - var nextY = snap((event.y + dragY).coerceIn(entry.halfH + safe.top, height - entry.halfH - safe.bottom)) - guideX = -1f - guideY = -1f - entries.forEach { other -> - if (other === entry) return@forEach - if (abs(nextX - other.cx) < dp(8f)) { nextX = other.cx; guideX = other.cx } - if (abs(nextY - other.cy) < dp(8f)) { nextY = other.cy; guideY = other.cy } - } - if (abs(nextX - width / 2f) < dp(8f)) { nextX = width / 2f; guideX = nextX } - if (abs(nextY - height / 2f) < dp(8f)) { nextY = height / 2f; guideY = nextY } - if (hypot(nextX - entry.cx, nextY - entry.cy) > dp(4f)) moved = true - entry.cx = nextX - entry.cy = nextY - entry.x = entry.cx / width - entry.y = entry.cy / height - invalidate() - return true - } - - MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> { - removeCallbacks(addPress) - guideX = -1f - guideY = -1f - if (toolbarDrag) { - toolbarDrag = false - return true - } - val entry = selected - if (entry != null && dragging) { - dragging = false - if (moved) { - save() - invalidate() - } else { - onSelect?.invoke(entry.codes) - } - } - return true - } - } - return true - } - - private fun pressKey(code: Int) { - val owners = held[code] ?: 0 - held[code] = owners + 1 - if (owners == 0) sendKey(code, true) - } - - private fun releaseKey(code: Int) { - val owners = held[code] ?: return - if (owners > 1) { - held[code] = owners - 1 - return - } - held.remove(code) - sendKey(code, false) - } - - private fun release(entry: Entry) { - entry.codes.asReversed().forEach { releaseKey(it) } - invalidate() - } - - fun export(): String = ControllerPrefs.encodeScreenKeys( - entries.map { ScreenKey(it.codes, it.x, it.y, it.size, it.glyph, it.text, it.image, it.opacity, it.shape, it.toggle, it.labels) }, - ) - - fun load(raw: String) { - releaseAll() - entries.clear() - ControllerPrefs.decodeScreenKeys(raw).forEach { entries += entryOf(it) } - entries.forEach { measure(it) } - selected = null - save() - invalidate() - } - - private fun save() { - ControllerPrefs.setScreenKeys( - context, - entries.map { ScreenKey(it.codes, it.x, it.y, it.size, it.glyph, it.text, it.image, it.opacity, it.shape, it.toggle, it.labels) }, - ) - } - - private fun dp(value: Float) = value * resources.displayMetrics.density - - companion object { - private val DPAD_DIRS = listOf( - 0f to -1f, 1f to 0f, 0f to 1f, -1f to 0f, - ) - private val STICK_DIRS = listOf( - 0f to -1f, 0.7071f to -0.7071f, 1f to 0f, 0.7071f to 0.7071f, - 0f to 1f, -0.7071f to 0.7071f, -1f to 0f, -0.7071f to -0.7071f, - ) - const val DEFAULT_SIZE = 44 - const val MIN_SIZE = 24 - const val MAX_SIZE = 140 - const val IMAGE_DIR = "screenkeys" - private const val BODY = 0xFF14181F.toInt() - } -} diff --git a/app/src/main/java/com/droiddeck/launcher/input/ScreenLayoutFile.kt b/app/src/main/java/com/droiddeck/launcher/input/ScreenLayoutFile.kt deleted file mode 100644 index fc25940719..0000000000 --- a/app/src/main/java/com/droiddeck/launcher/input/ScreenLayoutFile.kt +++ /dev/null @@ -1,140 +0,0 @@ -package com.droiddeck.launcher.input - -import org.json.JSONArray -import org.json.JSONObject -import kotlin.math.roundToInt - -private val ICP_NAMES: Map = mapOf( - 103 to "KEY_UP", 106 to "KEY_RIGHT", 108 to "KEY_DOWN", 105 to "KEY_LEFT", - 28 to "KEY_ENTER", 1 to "KEY_ESC", 14 to "KEY_BKSP", 111 to "KEY_DEL", 15 to "KEY_TAB", - 57 to "KEY_SPACE", 29 to "KEY_CTRL_L", 97 to "KEY_CTRL_R", 110 to "KEY_INSERT", - 42 to "KEY_SHIFT_L", 54 to "KEY_SHIFT_R", 56 to "KEY_ALT_L", 100 to "KEY_ALT_R", - 102 to "KEY_HOME", 107 to "KEY_END", 99 to "KEY_PRTSCN", 104 to "KEY_PG_UP", 109 to "KEY_PG_DOWN", - 58 to "KEY_CAPS_LOCK", 69 to "KEY_NUM_LOCK", 41 to "KEY_GRAVE", 12 to "KEY_MINUS", - 11 to "KEY_0", 2 to "KEY_1", 3 to "KEY_2", 4 to "KEY_3", 5 to "KEY_4", 6 to "KEY_5", - 7 to "KEY_6", 8 to "KEY_7", 9 to "KEY_8", 10 to "KEY_9", - 30 to "KEY_A", 48 to "KEY_B", 46 to "KEY_C", 32 to "KEY_D", 18 to "KEY_E", 33 to "KEY_F", - 34 to "KEY_G", 35 to "KEY_H", 23 to "KEY_I", 36 to "KEY_J", 37 to "KEY_K", 38 to "KEY_L", - 50 to "KEY_M", 49 to "KEY_N", 24 to "KEY_O", 25 to "KEY_P", 16 to "KEY_Q", 19 to "KEY_R", - 31 to "KEY_S", 20 to "KEY_T", 22 to "KEY_U", 47 to "KEY_V", 17 to "KEY_W", 45 to "KEY_X", - 21 to "KEY_Y", 44 to "KEY_Z", - 26 to "KEY_BRACKET_LEFT", 27 to "KEY_BRACKET_RIGHT", 43 to "KEY_BACKSLASH", 53 to "KEY_SLASH", - 39 to "KEY_SEMICOLON", 51 to "KEY_COMMA", 52 to "KEY_PERIOD", 40 to "KEY_APOSTROPHE", - 59 to "KEY_F1", 60 to "KEY_F2", 61 to "KEY_F3", 62 to "KEY_F4", 63 to "KEY_F5", 64 to "KEY_F6", - 65 to "KEY_F7", 66 to "KEY_F8", 67 to "KEY_F9", 68 to "KEY_F10", 87 to "KEY_F11", 88 to "KEY_F12", - 79 to "KEY_KP_1", 80 to "KEY_KP_2", 81 to "KEY_KP_3", 75 to "KEY_KP_4", 76 to "KEY_KP_5", - 77 to "KEY_KP_6", 71 to "KEY_KP_7", 72 to "KEY_KP_8", 73 to "KEY_KP_9", 82 to "KEY_KP_0", -) - -private val ICP_CODES: Map = ICP_NAMES.entries.associate { (code, name) -> name to code } - -private fun icpShape(shape: Int): String = when (shape) { - SCREEN_KEY_CIRCLE -> "CIRCLE" - SCREEN_KEY_RECT -> "RECT" - SCREEN_KEY_ROUND -> "ROUND_RECT" - SCREEN_KEY_DPAD -> "ROUND_RECT" - SCREEN_KEY_STICK -> "ROUND_RECT" - SCREEN_KEY_TRIGGER -> "ROUND_RECT" - else -> "ROUND_RECT" -} - -private fun screenKeyShape(shape: String): Int = when (shape) { - "CIRCLE" -> SCREEN_KEY_CIRCLE - "RECT" -> SCREEN_KEY_RECT - "SQUARE" -> SCREEN_KEY_ROUND - else -> SCREEN_KEY_PILL -} - -fun exportScreenLayout(keys: List, widthPx: Int, density: Float): String { - val snap = widthPx / 100f - val elements = JSONArray() - keys.forEach { key -> - val binds = JSONArray() - val directional = key.shape == SCREEN_KEY_DPAD || key.shape == SCREEN_KEY_STICK - key.codes.forEach { code -> - val name = ICP_NAMES[code] - if (name != null) binds.put(name) else if (directional) binds.put("") - } - if (binds.length() == 0) return@forEach - val shape = icpShape(key.shape) - val sizePx = key.size * density - val scale = when { - key.shape == SCREEN_KEY_DPAD -> sizePx * 2.3f / (14f * snap) - key.shape == SCREEN_KEY_STICK -> sizePx * 2.3f / (14f * snap) - shape == "CIRCLE" -> sizePx / (6f * snap) - else -> sizePx / (4f * snap) - } - elements.put( - JSONObject() - .put( - "type", - when (key.shape) { - SCREEN_KEY_DPAD -> "D_PAD" - SCREEN_KEY_STICK -> "STICK" - else -> "BUTTON" - }, - ) - .put("shape", shape) - .put("customColor", 0) - .put("bindings", binds) - .put("scale", scale) - .put("x", key.x) - .put("y", key.y) - .put("toggleSwitch", key.toggle) - .put("swipeable", false) - .put("text", key.text) - .put("iconId", 0) - .apply { if (key.opacity < 1f) put("opacity", key.opacity) }, - ) - } - return JSONObject() - .put("id", 0) - .put("name", "DroidDeck") - .put("cursorSpeed", 1.5f) - .put("elements", elements) - .toString() -} - -fun importScreenLayout(raw: String, widthPx: Int, density: Float): List? { - val root = runCatching { JSONObject(raw) }.getOrNull() ?: return null - val elements = root.optJSONArray("elements") ?: return null - val snap = widthPx / 100f - val keys = mutableListOf() - for (i in 0 until elements.length()) { - val element = elements.optJSONObject(i) ?: continue - val type = element.optString("type", "BUTTON") - if (type != "BUTTON" && type != "D_PAD" && type != "STICK") continue - val binds = element.optJSONArray("bindings") ?: continue - val raw = (0 until binds.length()).map { ICP_CODES[binds.optString(it)] ?: 0 } - val codes = if (type == "D_PAD" || type == "STICK") { - raw.dropLastWhile { it == 0 }.ifEmpty { listOf(0) } - } else { - raw.filter { it != 0 } - } - if (codes.isEmpty()) continue - val dpad = type == "D_PAD" - val stick = type == "STICK" - val shape = when { - dpad -> SCREEN_KEY_DPAD - stick -> SCREEN_KEY_STICK - else -> screenKeyShape(element.optString("shape")) - } - val scale = element.optDouble("scale", 1.0).toFloat().takeIf { it > 0f } ?: 1f - val sizePx = when { - dpad || stick -> 14f * snap * scale / 2.3f - shape == SCREEN_KEY_CIRCLE -> 6f * snap * scale - else -> 4f * snap * scale - } - keys += ScreenKey( - codes = codes, - x = element.optDouble("x", 0.5).toFloat().coerceIn(0f, 1f), - y = element.optDouble("y", 0.5).toFloat().coerceIn(0f, 1f), - size = (sizePx / density).roundToInt().coerceIn(ScreenKeys.MIN_SIZE, ScreenKeys.MAX_SIZE), - text = element.optString("text").take(24), - opacity = element.optDouble("opacity", 1.0).toFloat().coerceIn(0.2f, 1f), - shape = shape, - toggle = element.optBoolean("toggleSwitch"), - ) - } - return keys -} diff --git a/app/src/main/java/com/droiddeck/launcher/input/SecondScreenMode.kt b/app/src/main/java/com/droiddeck/launcher/input/SecondScreenMode.kt index 35400d9553..3cbc12a227 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/SecondScreenMode.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/SecondScreenMode.kt @@ -10,6 +10,7 @@ enum class SecondScreenMode(val id: String, @StringRes val label: Int) { KEYBOARD_TRACKPAD("keyboard-trackpad", R.string.second_mode_keyboard_trackpad), TERMINAL("terminal", R.string.second_mode_terminal), DECK_CONTROLS("deck-controls", R.string.second_mode_deck_controls), + CUSTOM_CONTROLS("custom-controls", R.string.input_profile_controls_mode), DASHBOARD("dashboard", R.string.second_mode_dashboard), DESKTOP("desktop", R.string.second_mode_desktop); diff --git a/app/src/main/java/com/droiddeck/launcher/input/SteamControllerBle.kt b/app/src/main/java/com/droiddeck/launcher/input/SteamControllerBle.kt index e77b322f2b..5b0b69c8f0 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/SteamControllerBle.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/SteamControllerBle.kt @@ -20,7 +20,11 @@ import java.util.ArrayDeque import java.util.UUID @SuppressLint("MissingPermission") -class SteamControllerBle(private val context: Context) { +class SteamControllerBle(private val context: Context, router: InputRouter? = null) { + private val inputLock = router ?: Any() + private var routedInput = router?.let(::TritonInputRouter) + @Volatile private var stopped = false + private var started = false private val thread = HandlerThread("steam-controller-ble").apply { start() } private val handler = Handler(thread.looper) private val outputReports = mutableMapOf() @@ -36,22 +40,54 @@ class SteamControllerBle(private val context: Context) { private var outputWritePending = false private var featureRead = 0L - fun start() = handler.post { - if (running) return@post - running = true - Log.i(TAG, "looking for a paired Steam Controller") - connect() + fun start(): Unit = synchronized(inputLock) { + if (stopped || started) return@synchronized + started = true + synchronized(slotLock) { + slotOwner = this + connected = false + FakeInputWriter.writeTritonState(SLOT, 0x45, ByteArray(STATE_BYTES)) + FakeInputWriter.setTritonConnected(SLOT, false) + } + handler.post { + if (stopped || slotOwner !== this || running) return@post + running = true + Log.i(TAG, "looking for a paired Steam Controller") + connect() + } } - fun stop() = handler.post { - running = false - handler.removeCallbacksAndMessages(null) - disconnect("session ended") - thread.quitSafely() + private inline fun withSlot(block: () -> Unit) = synchronized(inputLock) { + synchronized(slotLock) { + if (!stopped && slotOwner === this) block() + } + } + + fun stop() { + synchronized(inputLock) { + if (stopped) return + stopped = true + routedInput?.close() + routedInput = null + synchronized(slotLock) { + if (slotOwner === this) { + FakeInputWriter.writeTritonState(SLOT, if (reportId == 0x47) 0x47 else 0x45, ByteArray(STATE_BYTES)) + connected = false + FakeInputWriter.setTritonConnected(SLOT, false) + slotOwner = null + } + } + } + handler.post { + running = false + handler.removeCallbacksAndMessages(null) + disconnect("session ended") + thread.quitSafely() + } } private fun connect() { - if (!running || gatt != null) return + if (stopped || slotOwner !== this || !running || gatt != null) return if (SessionState.suspended) { handler.postDelayed(::connect, RETRY_MS) return @@ -84,11 +120,17 @@ class SteamControllerBle(private val context: Context) { featureRead = 0L queuedWrites.clear() outputReports.clear() - connected = false - FakeInputWriter.setTritonConnected(SLOT, false) + synchronized(inputLock) { + if (!stopped) { + release() + withSlot { + connected = false + FakeInputWriter.setTritonConnected(SLOT, false) + } + } + } g.disconnect() g.close() - release() Log.i(TAG, "disconnected: $why") listener?.invoke() } @@ -119,10 +161,12 @@ class SteamControllerBle(private val context: Context) { } override fun onDescriptorWrite(g: BluetoothGatt, descriptor: BluetoothGattDescriptor, status: Int) { - if (g !== gatt || descriptor.characteristic.uuid != input) return - Log.i(TAG, "subscribed to report 0x%02x (status %d)".format(reportId, status)) - connected = status == BluetoothGatt.GATT_SUCCESS - FakeInputWriter.setTritonConnected(SLOT, connected) + withSlot { + if (g !== gatt || descriptor.characteristic.uuid != input) return + Log.i(TAG, "subscribed to report 0x%02x (status %d)".format(reportId, status)) + connected = status == BluetoothGatt.GATT_SUCCESS + FakeInputWriter.setTritonConnected(SLOT, connected) + } listener?.invoke() if (connected) { refreshLizardMode() @@ -164,6 +208,7 @@ class SteamControllerBle(private val context: Context) { } private fun subscribe(g: BluetoothGatt) { + if (stopped || slotOwner !== this) return val service = g.getService(SERVICE) if (service == null) { Log.w(TAG, "no Valve service on \"${g.device.name}\" (attempt ${discoveries + 1}); services: " + @@ -207,25 +252,31 @@ class SteamControllerBle(private val context: Context) { .onFailure { Log.w(TAG, "could not refresh the service cache", it) } } - private fun refreshLizardMode() { - if (!connected) return + private fun refreshLizardMode() = withSlot { + if (!connected) return@withSlot val msg = ByteArray(FEATURE_BYTES) msg[0] = 0x87.toByte(); msg[1] = 3; msg[2] = 9 enqueueOutput(REPORT, msg) handler.postDelayed(::refreshLizardMode, LIZARD_REFRESH_MS) } - private fun onState(data: ByteArray) { - if (data.size < STATE_BYTES) return - FakeInputWriter.writeTritonState(SLOT, reportId, data) + private fun onState(data: ByteArray) = withSlot { + if (data.size < STATE_BYTES) return@withSlot + val timing = InputTiming.begin() + FakeInputWriter.writeTritonState(SLOT, reportId, routedInput?.route(data) ?: data) + InputTiming.end(timing) } - private fun release() { - FakeInputWriter.writeTritonState(SLOT, if (reportId == 0x47) 0x47 else 0x45, ByteArray(STATE_BYTES)) + private fun release() = synchronized(inputLock) { + if (stopped) return@synchronized + routedInput?.release() + withSlot { + FakeInputWriter.writeTritonState(SLOT, if (reportId == 0x47) 0x47 else 0x45, ByteArray(STATE_BYTES)) + } } - private fun pollOutput() { - if (!running || !connected || gatt == null) return + private fun pollOutput() = withSlot { + if (!running || !connected || gatt == null) return@withSlot FakeInputWriter.readTritonOutput(SLOT, outputSequence)?.let { output -> outputSequence = ByteBuffer.wrap(output, 0, 8).order(ByteOrder.LITTLE_ENDIAN).long sendOutputReport(output[8] != 0.toByte(), output.copyOfRange(9, output.size)) @@ -239,7 +290,7 @@ class SteamControllerBle(private val context: Context) { handler.postDelayed(::pollOutput, OUTPUT_POLL_MS) } - private fun onFeatureRead(value: ByteArray?) { + private fun onFeatureRead(value: ByteArray?) = withSlot { outputWritePending = false if (featureRead != 0L) { if (value == null) Log.w(TAG, "feature read failed") @@ -267,14 +318,14 @@ class SteamControllerBle(private val context: Context) { writeNextOutput() } - private fun writeNextOutput() { - if (outputWritePending) return - val g = gatt ?: return - val (uuid, value) = queuedWrites.pollFirst() ?: return + private fun writeNextOutput() = withSlot { + if (outputWritePending) return@withSlot + val g = gatt ?: return@withSlot + val (uuid, value) = queuedWrites.pollFirst() ?: return@withSlot val characteristic = g.getService(SERVICE)?.getCharacteristic(uuid) if (characteristic == null) { Log.w(TAG, "BLE output characteristic $uuid disappeared") - return + return@withSlot } if (value == null) { if (g.readCharacteristic(characteristic)) { @@ -283,7 +334,7 @@ class SteamControllerBle(private val context: Context) { queuedWrites.addFirst(uuid to null) handler.postDelayed(::writeNextOutput, OUTPUT_RETRY_MS) } - return + return@withSlot } @Suppress("DEPRECATION") characteristic.writeType = BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT @@ -299,6 +350,8 @@ class SteamControllerBle(private val context: Context) { } companion object { + private val slotLock = Any() + @Volatile private var slotOwner: SteamControllerBle? = null private const val TAG = "SteamControllerBle" private const val SLOT = 0 private const val RETRY_MS = 5_000L diff --git a/app/src/main/java/com/droiddeck/launcher/input/TritonInputRouter.kt b/app/src/main/java/com/droiddeck/launcher/input/TritonInputRouter.kt new file mode 100644 index 0000000000..c85b4ffa94 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/input/TritonInputRouter.kt @@ -0,0 +1,93 @@ +package com.droiddeck.launcher.input + +import kotlin.math.roundToInt + +class TritonInputRouter(private val router: InputRouter) { + private val channels = InputControl.entries.map { router.channel(InputRole.PHYSICAL, it) }.toTypedArray() + private var last: ByteArray? = null + private var buffer = ByteArray(45) + private val snapshot: () -> Unit = { last?.let { read(it, false) } } + + init { router.beforeProfileChange(snapshot) } + + fun route(data: ByteArray): ByteArray = synchronized(router) { + if (data.size < 45) return@synchronized data + last = data + if (channels.all { it.nativeIdentity() }) return@synchronized data + if (buffer.size != data.size) buffer = ByteArray(data.size) + data.copyInto(buffer) + putBits(buffer, bits(data) and CONTROL_BITS.inv()) + for (offset in 5..27 step 2) putShort(buffer, offset, 0) + read(data, true) + buffer + } + + private fun read(data: ByteArray, emit: Boolean) { + val bits = bits(data) + for (control in InputControl.entries) { + val channel = channels[control.ordinal] + val offset = offset(control) + val pair = control.dimensions == 2 + val x = if (offset != 0) axis(data, offset) else if (bits and bit(control) != 0) 1f else 0f + val y = if (pair) axis(data, offset + 2) * if (control.isTrackpad) 1f else -1f else 0f + val contact = control.isTrackpad && bits and bit(control) != 0 + if (!emit) { channel.observe(x, y, contact); continue } + val target = channel.update(x, y, contact) ?: continue + val targetOffset = offset(target) + if (targetOffset != 0) { + val value = shortValue(x) + if (target.dimensions == 2) { + val second = shortValue(y * if (target.isTrackpad) 1f else -1f) + val oldX = short(buffer, targetOffset).toLong() + val oldY = short(buffer, targetOffset + 2).toLong() + if (value.toLong() * value + second.toLong() * second >= oldX * oldX + oldY * oldY) { + putShort(buffer, targetOffset, value) + putShort(buffer, targetOffset + 2, second) + } + val touch = if (control.isTrackpad) contact else bits and bit(control) != 0 + if (touch) putBits(buffer, bits(buffer) or bit(target)) + } else { + if (value >= short(buffer, targetOffset)) putShort(buffer, targetOffset, value) + if (bits and bit(control) != 0) putBits(buffer, bits(buffer) or bit(target)) + } + } else if (x > 0.5f) putBits(buffer, bits(buffer) or bit(target)) + if (target == InputControl.LEFT_CLICK || target == InputControl.RIGHT_CLICK) { + val pressureOffset = if (target == InputControl.LEFT_CLICK) 21 else 27 + val from = if (control == InputControl.LEFT_CLICK) 21 else if (control == InputControl.RIGHT_CLICK) 27 else 0 + if (from != 0) putShort(buffer, pressureOffset, short(data, from)) + else if (x > 0.5f) putShort(buffer, pressureOffset, 32767) + } + } + } + + fun release() = synchronized(router) { channels.forEach { it.release() }; last = null } + fun close() = synchronized(router) { release(); channels.forEach { it.close() }; router.removeSnapshot(snapshot) } + + private fun bits(data: ByteArray): Int = (data[1].toInt() and 255) or ((data[2].toInt() and 255) shl 8) or + ((data[3].toInt() and 255) shl 16) or ((data[4].toInt() and 255) shl 24) + private fun putBits(data: ByteArray, value: Int) { for (i in 0..3) data[i + 1] = (value ushr (i * 8)).toByte() } + private fun short(data: ByteArray, offset: Int) = ((data[offset].toInt() and 255) or (data[offset + 1].toInt() shl 8)).toShort().toInt() + private fun axis(data: ByteArray, offset: Int): Float = short(data, offset).let { it / if (it < 0) 32768f else 32767f } + private fun shortValue(value: Float) = (value * if (value < 0f) 32768f else 32767f).roundToInt().coerceIn(-32768, 32767) + private fun putShort(data: ByteArray, offset: Int, value: Int) { data[offset] = value.toByte(); data[offset + 1] = (value shr 8).toByte() } + + companion object { + private const val CONTROL_BITS = 0x0fffffff + private fun bit(c: InputControl): Int = when (c) { + InputControl.A -> 0x1; InputControl.B -> 0x2; InputControl.X -> 0x4; InputControl.Y -> 0x8 + InputControl.QAM -> 0x10; InputControl.R3 -> 0x20; InputControl.START -> 0x40 + InputControl.R4 -> 0x80; InputControl.R5 -> 0x100; InputControl.RB -> 0x200 + InputControl.DOWN -> 0x400; InputControl.RIGHT -> 0x800; InputControl.LEFT -> 0x1000; InputControl.UP -> 0x2000 + InputControl.SELECT -> 0x4000; InputControl.L3 -> 0x8000; InputControl.GUIDE -> 0x10000 + InputControl.L4 -> 0x20000; InputControl.L5 -> 0x40000; InputControl.LB -> 0x80000 + InputControl.RS -> 0x100000; InputControl.RIGHT_PAD -> 0x200000; InputControl.RIGHT_CLICK -> 0x400000 + InputControl.RT -> 0x800000; InputControl.LS -> 0x1000000; InputControl.LEFT_PAD -> 0x2000000 + InputControl.LEFT_CLICK -> 0x4000000; InputControl.LT -> 0x8000000 + } + private fun offset(c: InputControl): Int = when (c) { + InputControl.LT -> 5; InputControl.RT -> 7; InputControl.LS -> 9; InputControl.RS -> 13 + InputControl.LEFT_PAD -> 17; InputControl.RIGHT_PAD -> 23 + else -> 0 + } + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/session/SettingsReset.kt b/app/src/main/java/com/droiddeck/launcher/session/SettingsReset.kt index df74d80544..48a1d57be2 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SettingsReset.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SettingsReset.kt @@ -5,6 +5,7 @@ import android.util.Log import com.droiddeck.launcher.core.DownloadSource import com.droiddeck.launcher.core.GameEnvironment import com.droiddeck.launcher.input.ControllerPrefs +import com.droiddeck.launcher.input.InputProfiles import com.droiddeck.launcher.runtime.LinuxRuntime object SettingsReset { @@ -43,6 +44,7 @@ object SettingsReset { val session = context.getSharedPreferences("session", Context.MODE_PRIVATE) session.edit().apply { session.all.keys.filter { isSessionSetting(it) }.forEach { remove(it) } }.commit() ControllerPrefs.resetAll(context) + InputProfiles.resetAll(context) context.getSharedPreferences("app-ui", Context.MODE_PRIVATE).edit().clear().commit() context.getSharedPreferences("frame_gen", Context.MODE_PRIVATE).edit().clear().commit() context.getSharedPreferences("steam_touch", Context.MODE_PRIVATE).edit() diff --git a/app/src/main/java/com/droiddeck/launcher/ui/ControllerSettings.kt b/app/src/main/java/com/droiddeck/launcher/ui/ControllerSettings.kt index 6c5f7c230e..2afc537c97 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/ControllerSettings.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/ControllerSettings.kt @@ -1,111 +1,20 @@ package com.droiddeck.launcher.ui -import com.droiddeck.launcher.R -import androidx.compose.ui.res.stringResource -import androidx.compose.foundation.background -import androidx.compose.foundation.border -import androidx.compose.foundation.layout.Arrangement -import androidx.compose.foundation.layout.Box -import androidx.compose.foundation.layout.Row -import androidx.compose.foundation.layout.size -import androidx.compose.foundation.shape.CircleShape -import androidx.compose.foundation.layout.ColumnScope -import androidx.compose.runtime.Composable -import androidx.compose.ui.Modifier -import androidx.compose.ui.graphics.Color -import androidx.compose.ui.unit.dp +import androidx.compose.runtime.* +import androidx.compose.ui.platform.LocalContext import com.droiddeck.launcher.input.ControllerPrefs -import com.droiddeck.launcher.session.SessionPrefs -class ControllerActions( - val onOsc: (String) -> Unit, - val onTint: (Int) -> Unit, - val onOpacity: (Int) -> Unit, - val onSize: (Int) -> Unit, - val onStickClick: (Boolean) -> Unit, - val onAdaptiveSticks: (Boolean) -> Unit, - val onRumble: (Boolean) -> Unit, - val onDeadZone: (Int) -> Unit, - val onSteamButton: (Boolean) -> Unit, - val onQamButton: (Boolean) -> Unit, - val onKeyboardButton: (Boolean) -> Unit, - val onSelectSteam: (Boolean) -> Unit, - val onJoyConVr: (Boolean) -> Unit, - val onEditLayout: () -> Unit, - val onResetLayout: () -> Unit, - val onMapping: () -> Unit, - val onResetAll: () -> Unit, -) +class ControllerActions(val onMapping: () -> Unit) @Composable -private fun Swatch(color: Int) { - Box(Modifier.size(14.dp).background(Color(color), CircleShape).border(1.dp, Color.White.copy(alpha = 0.35f), CircleShape)) -} - -@Composable -fun ColumnScope.ControllerRows(host: MenuHost, oscMode: String, c: ControllerPrefs.Settings, a: ControllerActions) { - ChoiceRow( - host, "controller-osc", stringResource(R.string.mode_osc), stringResource(R.string.ctrl_osc_hint), - listOf(SessionPrefs.OSC_AUTO to stringResource(R.string.common_auto), SessionPrefs.OSC_ALWAYS to stringResource(R.string.common_always), SessionPrefs.OSC_STEAM_TOUCH to stringResource(R.string.osc_steam_touch), SessionPrefs.OSC_STEAM_QAM to stringResource(R.string.mode_osc_qam), SessionPrefs.OSC_NEVER to stringResource(R.string.common_never)), - oscMode, note = stringResource(R.string.mode_osc_note), onPick = a.onOsc, - ) - val tintOpen = host.open == "controller-tint" - SettingsRow(stringResource(R.string.ctrl_color), stringResource(R.string.ctrl_color_hint), highlighted = tintOpen) { - Box { - ValueChip(ControllerPrefs.tints.firstOrNull { it.first == c.tint }?.second?.let { stringResource(it) } ?: stringResource(R.string.ctrl_custom), tintOpen) { - host.open = if (tintOpen) null else "controller-tint" - } - AnchoredMenu(tintOpen, onDismiss = { if (host.open == "controller-tint") host.open = null }, title = stringResource(R.string.ctrl_color)) { firstItemFocus -> - ControllerPrefs.tints.forEachIndexed { index, (color, name) -> - MenuItem(stringResource(name), checked = c.tint == color, leading = { Swatch(color) }, focusRequester = if (index == 0) firstItemFocus else null) { - a.onTint(color) - host.open = null - } - } - } - } - } - ChoiceRow(host, "controller-opacity", stringResource(R.string.ctrl_opacity), null, ControllerPrefs.opacities.map { it to stringResource(R.string.ctrl_percent, it) }, c.opacity, onPick = a.onOpacity) - ChoiceRow(host, "controller-size", stringResource(R.string.ctrl_size), stringResource(R.string.ctrl_size_hint), ControllerPrefs.sizes.map { it to stringResource(R.string.ctrl_percent, it) }, c.size, onPick = a.onSize) - ToggleRow(host, "controller-stick-click", stringResource(R.string.ctrl_stick_click), stringResource(R.string.ctrl_stick_click_hint), c.stickClick, onChange = a.onStickClick) - ToggleRow(host, "controller-adaptive", stringResource(R.string.ctrl_adaptive), stringResource(R.string.ctrl_adaptive_hint), c.adaptiveSticks, onChange = a.onAdaptiveSticks) - ToggleRow(host, "controller-rumble", stringResource(R.string.ctrl_rumble), null, c.rumble, onChange = a.onRumble) - ChoiceRow(host, "controller-dead-zone", stringResource(R.string.ctrl_dead_zone), null, ControllerPrefs.deadZones.map { it to stringResource(R.string.ctrl_percent, it) }, c.deadZone, onPick = a.onDeadZone) - ToggleRow(host, "controller-steam", stringResource(R.string.ctrl_steam_button), null, c.steamButton, onChange = a.onSteamButton) - ToggleRow(host, "controller-qam", stringResource(R.string.ctrl_qam_button), null, c.qamButton, onChange = a.onQamButton) - ToggleRow(host, "controller-keyboard", stringResource(R.string.ctrl_keyboard_button), null, c.keyboardButton, onChange = a.onKeyboardButton) - ToggleRow(host, "controller-select-steam", stringResource(R.string.ctrl_select_steam), stringResource(R.string.ctrl_select_steam_hint), c.selectSteam, onChange = a.onSelectSteam) - ToggleRow(host, "controller-joycon-vr", stringResource(R.string.ctrl_joycon_vr), stringResource(R.string.ctrl_joycon_vr_hint), c.joyConVr, onChange = a.onJoyConVr) - SettingsRow(stringResource(R.string.ctrl_layout), if (c.customLayout) stringResource(R.string.ctrl_layout_custom) else stringResource(R.string.ctrl_layout_default)) { - Row(horizontalArrangement = Arrangement.spacedBy(6.dp)) { - SecondaryButton(stringResource(R.string.ctrl_edit)) { a.onEditLayout() } - SecondaryButton(stringResource(R.string.ctrl_reset), enabled = c.customLayout) { a.onResetLayout() } - } - } - val remapped = c.mapping.count { (id, target) -> id != target } - ActionRow(stringResource(R.string.ctrl_mapping), if (remapped == 0) stringResource(R.string.ctrl_mapping_default) else stringResource(R.string.ctrl_mapping_count, remapped, c.mapping.size), stringResource(R.string.ctrl_configure), a.onMapping) - ActionRow(stringResource(R.string.ctrl_reset_all), stringResource(R.string.ctrl_reset_all_hint), stringResource(R.string.ctrl_reset), a.onResetAll) -} - -@Composable -fun ControllerMappingPage(mapping: Map, onPick: (String, String) -> Unit, onReset: () -> Unit, onBack: () -> Unit) { - val host = rememberMenuHost() - SettingsPage( - host, title = stringResource(R.string.ctrl_mapping), eyebrow = stringResource(R.string.setup_controller), - lede = stringResource(R.string.ctrl_mapping_lede), - onBack = onBack, - ) { - SettingsGroup(stringResource(R.string.shortcuts_title)) { - PhysicalShortcutRows() - } - SettingsGroup(stringResource(R.string.ctrl_onscreen_buttons)) { - val targets = ControllerPrefs.targets.map { (id, label) -> id to stringResource(label) } - for ((id, name) in ControllerPrefs.mappable) { - ChoiceRow(host, "map-$id", stringResource(name), null, targets, mapping[id] ?: id) { onPick(id, it) } - } - } - SettingsGroup(stringResource(R.string.ctrl_defaults)) { - ActionRow(stringResource(R.string.ctrl_reset_mapping), stringResource(R.string.ctrl_mapping_default), stringResource(R.string.ctrl_reset), onReset) - } +internal fun rememberControllerSettings(): ControllerPrefs.Settings { + val context = LocalContext.current + var settings by remember { mutableStateOf(ControllerPrefs.read(context)) } + DisposableEffect(context) { + val prefs = ControllerPrefs.prefs(context) + val listener = android.content.SharedPreferences.OnSharedPreferenceChangeListener { _, _ -> settings = ControllerPrefs.read(context) } + prefs.registerOnSharedPreferenceChangeListener(listener) + onDispose { prefs.unregisterOnSharedPreferenceChangeListener(listener) } } + return settings } diff --git a/app/src/main/java/com/droiddeck/launcher/ui/DeckControlsPanel.kt b/app/src/main/java/com/droiddeck/launcher/ui/DeckControlsPanel.kt deleted file mode 100644 index 0193ce3201..0000000000 --- a/app/src/main/java/com/droiddeck/launcher/ui/DeckControlsPanel.kt +++ /dev/null @@ -1,720 +0,0 @@ -package com.droiddeck.launcher.ui - -import com.droiddeck.launcher.R -import android.annotation.SuppressLint -import android.content.Context -import android.graphics.Canvas -import android.graphics.Color -import android.graphics.Paint -import android.graphics.RadialGradient -import android.graphics.RectF -import android.graphics.Shader -import android.graphics.Typeface -import android.graphics.drawable.GradientDrawable -import android.view.Gravity -import android.view.HapticFeedbackConstants -import android.view.MotionEvent -import android.view.View -import android.view.ViewGroup -import android.widget.FrameLayout -import android.widget.HorizontalScrollView -import android.widget.LinearLayout -import android.widget.TextView -import com.droiddeck.launcher.input.DeckControls -import com.droiddeck.launcher.input.PadBridge -import com.droiddeck.launcher.input.PadState -import com.droiddeck.launcher.session.SessionService -import com.droiddeck.launcher.session.SessionPrefs -import com.droiddeck.launcher.session.SessionState -import kotlin.math.atan2 -import kotlin.math.hypot -import kotlin.math.min - -@SuppressLint("ViewConstructor") -class DeckControlsPanel( - context: Context, - private val onClose: () -> Unit, - private val onSteamMenu: () -> Unit, - private val onQam: () -> Unit, - private val pad: PadBridge? = null, - private val portraitDock: Boolean = false, - private val onMenu: (() -> Unit)? = null, - private val onSwap: (() -> Unit)? = null, -) : LinearLayout(context) { - private enum class Tab(val label: Int) { GAMEPAD(R.string.deck_tab_gamepad), GRIPS(R.string.deck_tab_grips), LEFT(R.string.deck_tab_left), RIGHT(R.string.deck_tab_right), BOTH(R.string.deck_tab_both) } - - private val content = FrameLayout(context) - val deckControls = pad == null || SessionState.deckPad - private val tabViews = HashMap() - private val topBar: View by lazy { tabsRow() } - - init { - orientation = VERTICAL - setBackgroundColor(Color.BLACK) - val title = context.getString(if (deckControls) R.string.second_mode_deck_controls else R.string.deck_tab_gamepad) - val extras = listOfNotNull(onSwap?.let { BarExtra(context.getString(R.string.swap_screens), icon = R.drawable.ic_bottom_swap, onClick = it) }) - addView(bottomScreenBarView(context) { sessionBarModel(title, onClose, onMenu, onSteamMenu, onQam, extras) }, - LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)) - addView(topBar, LayoutParams(LayoutParams.MATCH_PARENT, dp(52))) - addView(content, LayoutParams(LayoutParams.MATCH_PARENT, 0, 1f)) - select(if (pad != null && !deckControls) Tab.GAMEPAD else if (pad != null) selectedWithPad else selected) - } - - override fun onDetachedFromWindow() { - DeckControls.releaseAll() - super.onDetachedFromWindow() - } - - private fun tabsRow(): View = LinearLayout(context).apply { - orientation = HORIZONTAL - gravity = Gravity.CENTER - setPadding(dp(12), 0, dp(12), dp(8)) - val tabs = LinearLayout(context).apply { - orientation = HORIZONTAL - background = outline(CORNER_PILL, SURFACE) - setPadding(dp(4), dp(4), dp(4), dp(4)) - for (tab in Tab.values()) { - if (!deckControls && tab != Tab.GAMEPAD) continue - if (tab == Tab.GAMEPAD && pad == null) continue - val view = TextView(context).apply { - text = context.getString(tab.label) - textSize = 14f - typeface = Typeface.create(Typeface.DEFAULT, Typeface.BOLD) - gravity = Gravity.CENTER - isSingleLine = true - setPadding(dp(14), 0, dp(14), 0) - setOnClickListener { select(tab) } - } - tabViews[tab] = view - addView(view, LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT)) - } - } - val centre = HorizontalScrollView(context).apply { - isHorizontalScrollBarEnabled = false - overScrollMode = View.OVER_SCROLL_NEVER - addView(tabs, FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, dp(44))) - } - addView(centre, LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT)) - } - - private fun select(tab: Tab) { - if (pad != null) selectedWithPad = tab else selected = tab - DeckControls.releaseAll() - for ((each, view) in tabViews) { - val on = each == tab - view.background = if (on) filled(CORNER_PILL, ACCENT) else null - view.setTextColor(if (on) ON_ACCENT else TEXT_DIM) - } - tabViews[tab]?.let { selectedView -> - (selectedView.parent?.parent as? HorizontalScrollView)?.post { - val left = selectedView.left - val right = selectedView.right - val scroll = selectedView.parent?.parent as? HorizontalScrollView ?: return@post - when { - left < scroll.scrollX -> scroll.smoothScrollTo(left, 0) - right > scroll.scrollX + scroll.width -> scroll.smoothScrollTo(right - scroll.width, 0) - } - } - } - content.removeAllViews() - topBar.visibility = if (tab == Tab.GAMEPAD || tabViews.size < 2) View.GONE else View.VISIBLE - content.addView( - when (tab) { - Tab.GRIPS -> gripsGrid() - Tab.LEFT -> singlePad(right = false) - Tab.RIGHT -> singlePad(right = true) - Tab.BOTH -> bothPads() - Tab.GAMEPAD -> gamepad(pad ?: return) - }, - FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT), - ) - } - - private fun gripsGrid(): View = LinearLayout(context).apply { - orientation = HORIZONTAL - setPadding(dp(12), dp(6), dp(12), dp(12)) - fun column(upper: View, lower: View) = LinearLayout(context).apply { - orientation = VERTICAL - addView(upper, LayoutParams(LayoutParams.MATCH_PARENT, 0, 1f).apply { bottomMargin = dp(6) }) - addView(lower, LayoutParams(LayoutParams.MATCH_PARENT, 0, 1f).apply { topMargin = dp(6) }) - } - addView(column(GripButton(context, "L4", context.getString(R.string.deck_left_upper), DeckControls.L4), GripButton(context, "L5", context.getString(R.string.deck_left_lower), DeckControls.L5)), - LayoutParams(0, LayoutParams.MATCH_PARENT, 1f).apply { rightMargin = dp(6) }) - addView(column(GripButton(context, "R4", context.getString(R.string.deck_right_upper), DeckControls.R4), GripButton(context, "R5", context.getString(R.string.deck_right_lower), DeckControls.R5)), - LayoutParams(0, LayoutParams.MATCH_PARENT, 1f).apply { leftMargin = dp(6) }) - } - - private fun singlePad(right: Boolean): View = LinearLayout(context).apply { - orientation = VERTICAL - setPadding(dp(12), dp(6), dp(12), dp(12)) - addView(TrackpadView(context, right), LayoutParams(LayoutParams.MATCH_PARENT, 0, 1f).apply { bottomMargin = dp(12) }) - addView(ClickBar(context, right), LayoutParams(LayoutParams.MATCH_PARENT, dp(76))) - } - - private fun bothPads(): View = LinearLayout(context).apply { - orientation = VERTICAL - setPadding(dp(12), dp(6), dp(12), dp(12)) - val pads = LinearLayout(context).apply { - orientation = HORIZONTAL - addView(TrackpadView(context, right = false), LayoutParams(0, LayoutParams.MATCH_PARENT, 1f).apply { rightMargin = dp(6) }) - addView(TrackpadView(context, right = true), LayoutParams(0, LayoutParams.MATCH_PARENT, 1f).apply { leftMargin = dp(6) }) - } - addView(pads, LayoutParams(LayoutParams.MATCH_PARENT, 0, 1f).apply { bottomMargin = dp(12) }) - val grips = LinearLayout(context).apply { - orientation = HORIZONTAL - val row = listOf("L4" to DeckControls.L4, "L5" to DeckControls.L5, "R5" to DeckControls.R5, "R4" to DeckControls.R4) - row.forEachIndexed { index, (label, bit) -> - addView(GripButton(context, label, null, bit), LayoutParams(0, LayoutParams.MATCH_PARENT, 1f).apply { - if (index > 0) leftMargin = dp(6) - if (index < row.size - 1) rightMargin = dp(6) - }) - } - } - addView(grips, LayoutParams(LayoutParams.MATCH_PARENT, dp(84))) - } - - private fun gamepad(pad: PadBridge): View = GamepadLayout(context, portraitDock).apply { - fun pill(label: String, write: (PadState, Boolean) -> Unit) = PadButton(context, pad, label, write) - place(surfaceSlot(pad, right = false), GamepadGeometry.Slot.LEFT_MAIN) - place(DirectionPad(context, pad), GamepadGeometry.Slot.LEFT_SECOND) - place(surfaceSlot(pad, right = true), GamepadGeometry.Slot.RIGHT_MAIN) - place(FaceButtons(context, pad), GamepadGeometry.Slot.RIGHT_SECOND) - place(pill("LT") { st, d -> st.leftTrigger = if (d) 1f else 0f }, GamepadGeometry.Slot.LEFT_OUTER_SHOULDER) - place(pill("LB") { st, d -> st.press(PadState.LB, d) }, GamepadGeometry.Slot.LEFT_INNER_SHOULDER) - place(pill("RB") { st, d -> st.press(PadState.RB, d) }, GamepadGeometry.Slot.RIGHT_INNER_SHOULDER) - place(pill("RT") { st, d -> st.rightTrigger = if (d) 1f else 0f }, GamepadGeometry.Slot.RIGHT_OUTER_SHOULDER) - place(pill("VIEW") { st, d -> st.press(PadState.SELECT, d) }, GamepadGeometry.Slot.VIEW) - place(pill("MENU") { st, d -> st.press(PadState.START, d) }, GamepadGeometry.Slot.MENU) - if (deckControls) place(centreTap("\u25a6 " + context.getString(R.string.deck_panels), context.getString(R.string.deck_more_controls)) { select(Tab.BOTH) }, - GamepadGeometry.Slot.PANELS) - if (portraitDock && deckControls) { - place(GripButton(context, "L4", null, DeckControls.L4), GamepadGeometry.Slot.L4) - place(GripButton(context, "L5", null, DeckControls.L5), GamepadGeometry.Slot.L5) - place(GripButton(context, "R5", null, DeckControls.R5), GamepadGeometry.Slot.R5) - place(GripButton(context, "R4", null, DeckControls.R4), GamepadGeometry.Slot.R4) - } - } - - private fun centreTap(label: String, description: String, onClick: () -> Unit) = TextView(context).apply { - text = label - contentDescription = description - textSize = 13f - maxLines = 1 - setAutoSizeTextTypeUniformWithConfiguration(9, 13, 1, android.util.TypedValue.COMPLEX_UNIT_SP) - typeface = Typeface.create(Typeface.DEFAULT, Typeface.BOLD) - letterSpacing = 0.08f - gravity = Gravity.CENTER - setTextColor(TEXT) - background = outline(CORNER_PILL, SURFACE) - setOnClickListener { - performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY) - onClick() - } - } - - private fun surfaceSlot(pad: PadBridge, right: Boolean): View = FrameLayout(context).apply { - if (!deckControls) { - addView(StickView(context, pad, right), - FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT)) - return@apply - } - val slot = this - fun show() { - slot.removeAllViews() - val stick = SessionPrefs.gamepadSurface(context, right) == SessionPrefs.SURFACE_STICK - slot.addView(if (stick) StickView(context, pad, right) else TrackpadView(context, right, captioned = false), - FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT)) - val chip = TextView(context).apply { - text = "\u21c4" - contentDescription = context.getString(R.string.deck_surface_switch) - textSize = 14f - typeface = Typeface.create(Typeface.DEFAULT, Typeface.BOLD) - gravity = Gravity.CENTER - setTextColor(TEXT_DIM) - background = outline(CORNER_PILL, SURFACE) - setOnClickListener { - performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY) - SessionPrefs.setGamepadSurface(context, right, - if (stick) SessionPrefs.SURFACE_TRACKPAD else SessionPrefs.SURFACE_STICK) - show() - } - } - slot.addView(chip, FrameLayout.LayoutParams(dp(30), dp(30), - (if (right) Gravity.START else Gravity.END) or Gravity.TOP).apply { setMargins(dp(6), dp(6), dp(6), dp(6)) }) - } - show() - } - - fun takesTouchAt(x: Float, y: Float): Boolean = controlUnder(this, x, y) - - private fun controlUnder(view: View, x: Float, y: Float): Boolean { - if (view.visibility != VISIBLE || x < 0f || y < 0f || x >= view.width || y >= view.height) return false - if (view.isClickable || view is HorizontalScrollView || view is Surface || view is DirectionPad || view is FaceButtons || - view is androidx.compose.ui.platform.ComposeView) return true - val group = view as? ViewGroup ?: return false - return (group.childCount - 1 downTo 0).any { i -> - val child = group.getChildAt(i) - controlUnder(child, x + group.scrollX - child.x, y + group.scrollY - child.y) - } - } - - private fun outline(radius: Float, fill: Int) = outline(context, radius, fill) - - private fun filled(radius: Float, fill: Int) = GradientDrawable().apply { - cornerRadius = radius * resources.displayMetrics.density - setColor(fill) - } - - private fun dp(value: Int): Int = dp(context, value) - - - private abstract class Surface(context: Context) : View(context) { - protected val density = resources.displayMetrics.density - protected val rect = RectF() - private val fill = Paint(Paint.ANTI_ALIAS_FLAG) - private val stroke = Paint(Paint.ANTI_ALIAS_FLAG).apply { style = Paint.Style.STROKE } - protected val labelPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply { - textAlign = Paint.Align.CENTER - typeface = Typeface.create(Typeface.DEFAULT, Typeface.BOLD) - } - protected val captionPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply { - textAlign = Paint.Align.CENTER - color = TEXT_DIM - } - - protected fun drawSurface(canvas: Canvas, lit: Boolean) { - val inset = density - rect.set(inset, inset, width - inset, height - inset) - val radius = CORNER * density - fill.color = if (lit) ACCENT_WASH else SURFACE - canvas.drawRoundRect(rect, radius, radius, fill) - stroke.color = if (lit) ACCENT else LINE - stroke.strokeWidth = (if (lit) 2f else 1f) * density - canvas.drawRoundRect(rect, radius, radius, stroke) - } - } - - private open class HoldButton( - context: Context, - private val label: String, - private val caption: String?, - private val onChange: (Boolean) -> Unit, - ) : Surface(context) { - private var down = false - - override fun onDraw(canvas: Canvas) { - drawSurface(canvas, down) - val cx = width / 2f - labelPaint.textSize = min(height * 0.3f, 30 * density) - labelPaint.color = if (down) ACCENT else TEXT - val labelY = height / 2f + labelPaint.textSize * (if (caption == null) 0.35f else 0.1f) - canvas.drawText(label, cx, labelY, labelPaint) - if (caption != null) { - captionPaint.textSize = 12 * density - canvas.drawText(caption, cx, labelY + 22 * density, captionPaint) - } - } - - @SuppressLint("ClickableViewAccessibility") - override fun onTouchEvent(event: MotionEvent): Boolean { - when (event.actionMasked) { - MotionEvent.ACTION_DOWN -> set(true) - MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> set(false) - } - return true - } - - private fun set(pressed: Boolean) { - if (down == pressed) return - down = pressed - if (pressed) performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY) - onChange(pressed) - invalidate() - } - } - - private class GripButton(context: Context, label: String, caption: String?, bit: Int) : - HoldButton(context, label, caption, { DeckControls.setGrip(bit, it) }) - - private class ClickBar(context: Context, right: Boolean) : - HoldButton(context, context.getString(R.string.deck_click), if (right) context.getString(R.string.deck_right_trackpad) else context.getString(R.string.deck_left_trackpad), { DeckControls.setClick(right, it) }) - - private class TrackpadView(context: Context, private val right: Boolean, private val captioned: Boolean = true) : Surface(context) { - private var touching = false - private var clicked = false - private var fingerX = 0f - private var fingerY = 0f - private val guide = Paint(Paint.ANTI_ALIAS_FLAG).apply { style = Paint.Style.STROKE; color = GUIDE } - private val dot = Paint(Paint.ANTI_ALIAS_FLAG).apply { color = ACCENT } - private val glow = Paint(Paint.ANTI_ALIAS_FLAG) - - override fun onDraw(canvas: Canvas) { - drawSurface(canvas, clicked) - val cx = width / 2f - val cy = height / 2f - guide.strokeWidth = density - val reach = min(width, height) / 2f - 24 * density - canvas.drawCircle(cx, cy, reach, guide) - canvas.drawCircle(cx, cy, reach / 2f, guide) - canvas.drawLine(cx - reach, cy, cx + reach, cy, guide) - canvas.drawLine(cx, cy - reach, cx, cy + reach, guide) - captionPaint.textSize = 12 * density - captionPaint.color = if (touching) ACCENT else TEXT_DIM - if (captioned) canvas.drawText((if (right) context.getString(R.string.deck_right_trackpad) else context.getString(R.string.deck_left_trackpad)).uppercase(), cx, 28 * density, captionPaint) - if (touching) { - val radius = 56 * density - glow.shader = RadialGradient(fingerX, fingerY, radius, ACCENT_GLOW, Color.TRANSPARENT, Shader.TileMode.CLAMP) - canvas.drawCircle(fingerX, fingerY, radius, glow) - canvas.drawCircle(fingerX, fingerY, (if (clicked) 16 else 12) * density, dot) - } - } - - @SuppressLint("ClickableViewAccessibility") - override fun onTouchEvent(event: MotionEvent): Boolean { - when (event.actionMasked) { - MotionEvent.ACTION_DOWN, MotionEvent.ACTION_MOVE -> { - touch(event.getX(0), event.getY(0)) - click(event.pointerCount > 1) - } - MotionEvent.ACTION_POINTER_DOWN -> click(true) - MotionEvent.ACTION_POINTER_UP -> { - val stays = if (event.actionIndex == 0) 1 else 0 - touch(event.getX(stays), event.getY(stays)) - click(event.pointerCount - 1 > 1) - } - MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> { - click(false) - touching = false - DeckControls.setPad(right, touching = false) - invalidate() - } - } - return true - } - - private fun touch(x: Float, y: Float) { - fingerX = x.coerceIn(0f, width.toFloat()) - fingerY = y.coerceIn(0f, height.toFloat()) - touching = true - DeckControls.setPad(right, true, (fingerX - width / 2f) / (width / 2f), -(fingerY - height / 2f) / (height / 2f)) - invalidate() - } - - private fun click(down: Boolean) { - if (clicked == down) return - clicked = down - if (down) performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY) - DeckControls.setClick(right, down) - invalidate() - } - } - - private class PadButton( - context: Context, - private val pad: PadBridge, - label: String, - private val write: (PadState, Boolean) -> Unit, - ) : HoldButton(context, label, null, { down -> pad.applyTouch { write(it, down) } }) { - override fun onDetachedFromWindow() { - pad.applyTouch { write(it, false) } - super.onDetachedFromWindow() - } - } - - private class StickView(context: Context, private val pad: PadBridge, private val right: Boolean) : Surface(context) { - private var touching = false - private var clicking = false - private var lastUp = 0L - private var originX = 0f - private var originY = 0f - private var knobX = 0f - private var knobY = 0f - private val ring = Paint(Paint.ANTI_ALIAS_FLAG).apply { style = Paint.Style.STROKE } - private val knob = Paint(Paint.ANTI_ALIAS_FLAG) - - private fun reach() = min(width, height) * 0.30f - - override fun onDraw(canvas: Canvas) { - drawSurface(canvas, clicking) - val cx = if (touching) originX else width / 2f - val cy = if (touching) originY else height / 2f - ring.strokeWidth = 2 * density - ring.color = if (touching) ACCENT else LINE - canvas.drawCircle(cx, cy, reach(), ring) - knob.color = if (touching) ACCENT else KNOB - canvas.drawCircle(cx + knobX, cy + knobY, reach() * 0.45f, knob) - } - - @SuppressLint("ClickableViewAccessibility") - override fun onTouchEvent(event: MotionEvent): Boolean { - val r = reach().coerceAtLeast(1f) - when (event.actionMasked) { - MotionEvent.ACTION_DOWN -> { - originX = event.x.coerceIn(r, (width - r).coerceAtLeast(r)) - originY = event.y.coerceIn(r, (height - r).coerceAtLeast(r)) - touching = true - performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY) - if (event.eventTime - lastUp < CLICK_MS) setClick(true) - follow(event.x, event.y, r) - } - MotionEvent.ACTION_MOVE -> follow(event.getX(0), event.getY(0), r) - MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> { - touching = false - lastUp = event.eventTime - setClick(false) - move(0f, 0f, 1f) - } - } - return true - } - - private fun follow(x: Float, y: Float, r: Float) { - var dx = x - originX - var dy = y - originY - val length = hypot(dx, dy) - if (length > r) { dx *= r / length; dy *= r / length } - move(dx, dy, r) - } - - private fun move(dx: Float, dy: Float, r: Float) { - knobX = dx - knobY = dy - pad.applyTouch(if (right) PadBridge.RIGHT_STICK else PadBridge.LEFT_STICK) { s -> - if (right) { s.rightX = dx / r; s.rightY = dy / r } else { s.leftX = dx / r; s.leftY = dy / r } - } - invalidate() - } - - private fun setClick(down: Boolean) { - if (clicking == down) return - clicking = down - if (down) performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY) - pad.applyTouch { s -> s.press(if (right) PadState.R3 else PadState.L3, down) } - } - - override fun onDetachedFromWindow() { - setClick(false) - if (knobX != 0f || knobY != 0f) move(0f, 0f, 1f) - super.onDetachedFromWindow() - } - } - - private class DirectionPad(context: Context, private val pad: PadBridge) : View(context) { - private val density = resources.displayMetrics.density - private var held = 0 - private val arm = Paint(Paint.ANTI_ALIAS_FLAG) - private val rect = RectF() - - override fun onDraw(canvas: Canvas) { - val size = min(width, height) * 0.86f - val cx = width / 2f - val cy = height / 2f - val half = size / 2f - val thick = size / 3f / 2f - fun armRect(bit: Int, l: Float, t: Float, r: Float, b: Float) { - arm.color = if (held and bit != 0) ACCENT else KNOB - rect.set(l, t, r, b) - canvas.drawRoundRect(rect, 8 * density, 8 * density, arm) - } - armRect(1, cx - thick, cy - half, cx + thick, cy - thick) - armRect(4, cx - thick, cy + thick, cx + thick, cy + half) - armRect(8, cx - half, cy - thick, cx - thick, cy + thick) - armRect(2, cx + thick, cy - thick, cx + half, cy + thick) - arm.color = KNOB - canvas.drawRect(cx - thick, cy - thick, cx + thick, cy + thick, arm) - } - - @SuppressLint("ClickableViewAccessibility") - override fun onTouchEvent(event: MotionEvent): Boolean { - val bits = when (event.actionMasked) { - MotionEvent.ACTION_DOWN, MotionEvent.ACTION_MOVE -> { - val dx = event.getX(0) - width / 2f - val dy = event.getY(0) - height / 2f - if (hypot(dx, dy) < min(width, height) * 0.08f) 0 else { - val sector = ((Math.toDegrees(atan2(dy.toDouble(), dx.toDouble())) + 360 + 22.5) % 360 / 45).toInt() - intArrayOf(2, 2 or 4, 4, 4 or 8, 8, 8 or 1, 1, 1 or 2)[sector] - } - } - else -> 0 - } - set(bits) - return true - } - - private fun set(bits: Int) { - if (bits == held) return - if (bits and held.inv() != 0) performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY) - held = bits - pad.applyTouch { s -> - s.up = bits and 1 != 0; s.right = bits and 2 != 0; s.down = bits and 4 != 0; s.left = bits and 8 != 0 - } - invalidate() - } - - override fun onDetachedFromWindow() { - set(0) - super.onDetachedFromWindow() - } - } - - private class FaceButtons(context: Context, private val pad: PadBridge) : View(context) { - private val buttons = listOf(PadState.Y to "Y", PadState.B to "B", PadState.A to "A", PadState.X to "X") - private var held = emptySet() - private val fill = Paint(Paint.ANTI_ALIAS_FLAG) - private val text = Paint(Paint.ANTI_ALIAS_FLAG).apply { - textAlign = Paint.Align.CENTER - typeface = Typeface.create(Typeface.DEFAULT, Typeface.BOLD) - } - - private fun layout(): Pair>, Float> { - val cx = width / 2f - val cy = height / 2f - val spread = min(width, height) * 0.30f - val radius = min(width, height) * 0.16f - return listOf(cx to cy - spread, cx + spread to cy, cx to cy + spread, cx - spread to cy) to radius - } - - override fun onDraw(canvas: Canvas) { - val (centres, radius) = layout() - text.textSize = radius * 0.9f - buttons.forEachIndexed { i, (button, label) -> - val (x, y) = centres[i] - val down = button in held - fill.color = if (down) ACCENT else KNOB - canvas.drawCircle(x, y, radius, fill) - text.color = if (down) ON_ACCENT else TEXT - canvas.drawText(label, x, y + text.textSize * 0.35f, text) - } - } - - @SuppressLint("ClickableViewAccessibility") - override fun onTouchEvent(event: MotionEvent): Boolean { - val (centres, radius) = layout() - val now = HashSet() - val ending = event.actionMasked == MotionEvent.ACTION_UP || event.actionMasked == MotionEvent.ACTION_CANCEL - if (!ending) for (p in 0 until event.pointerCount) { - if (event.actionMasked == MotionEvent.ACTION_POINTER_UP && p == event.actionIndex) continue - val x = event.getX(p) - val y = event.getY(p) - val nearest = centres.indices.minByOrNull { hypot(centres[it].first - x, centres[it].second - y) } ?: continue - if (hypot(centres[nearest].first - x, centres[nearest].second - y) <= radius * 1.6f) now.add(buttons[nearest].first) - } - set(now) - return true - } - - private fun set(now: Set) { - if (now == held) return - if ((now - held).isNotEmpty()) performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY) - val before = held - held = now - pad.applyTouch { s -> for (b in before + now) s.press(b, b in now) } - invalidate() - } - - override fun onDetachedFromWindow() { - set(emptySet()) - super.onDetachedFromWindow() - } - } - - private class GamepadLayout(context: Context, private val portraitDock: Boolean) : ViewGroup(context) { - private val slots = LinkedHashMap() - - fun place(view: View, slot: GamepadGeometry.Slot) { - slots[view] = slot - addView(view) - } - - private fun boxes(w: Int, h: Int) = GamepadGeometry.layout(w.toFloat(), h.toFloat(), resources.displayMetrics.density, portraitDock) - - override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { - val w = MeasureSpec.getSize(widthMeasureSpec) - val h = MeasureSpec.getSize(heightMeasureSpec) - setMeasuredDimension(w, h) - val boxes = boxes(w, h) - for ((view, slot) in slots) { - val box = boxes[slot] - view.visibility = if (box == null) View.GONE else View.VISIBLE - if (box == null) continue - view.measure(MeasureSpec.makeMeasureSpec(box.width.toInt().coerceAtLeast(1), MeasureSpec.EXACTLY), - MeasureSpec.makeMeasureSpec(box.height.toInt().coerceAtLeast(1), MeasureSpec.EXACTLY)) - } - } - - override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) { - val boxes = boxes(r - l, b - t) - for ((view, slot) in slots) { - val box = boxes[slot] ?: continue - val left = box.left.toInt() - val top = box.top.toInt() - view.layout(left, top, left + box.width.toInt().coerceAtLeast(1), top + box.height.toInt().coerceAtLeast(1)) - } - } - } - - companion object { - private const val CLICK_MS = 300L - - private var selected = Tab.BOTH - private var selectedWithPad = Tab.BOTH - - private const val CORNER = 20f - private const val CORNER_PILL = 22f - private val ACCENT = Color.rgb(0x1A, 0x9F, 0xFF) - private val ACCENT_WASH = Color.argb(0x2E, 0x1A, 0x9F, 0xFF) - private val ACCENT_GLOW = Color.argb(0x66, 0x1A, 0x9F, 0xFF) - private val ON_ACCENT = Color.rgb(0x03, 0x11, 0x1F) - private val SURFACE = Color.rgb(0x0A, 0x0B, 0x0D) - private val LINE = Color.rgb(0x26, 0x29, 0x2E) - private val GUIDE = Color.rgb(0x16, 0x18, 0x1B) - private val TEXT = Color.rgb(0xF2, 0xF4, 0xF7) - private val TEXT_DIM = Color.rgb(0x7A, 0x80, 0x8A) - private val KNOB = Color.rgb(0x2A, 0x2D, 0x33) - - internal fun dp(context: Context, value: Int): Int = (value * context.resources.displayMetrics.density + 0.5f).toInt() - - internal fun outline(context: Context, radius: Float, fill: Int) = GradientDrawable().apply { - cornerRadius = radius * context.resources.displayMetrics.density - setColor(fill) - setStroke(dp(context, 1), LINE) - } - - internal fun roundButton(context: Context, label: String, description: String, onClick: () -> Unit, wide: Boolean = false) = - TextView(context).apply { - text = label - contentDescription = description - textSize = if (wide) 13f else 20f - typeface = Typeface.create(Typeface.DEFAULT, Typeface.BOLD) - letterSpacing = if (wide) 0.08f else 0f - gravity = Gravity.CENTER - setTextColor(TEXT) - background = outline(context, CORNER_PILL, SURFACE) - setPadding(if (wide) dp(context, 16) else 0, 0, if (wide) dp(context, 16) else 0, 0) - minWidth = dp(context, 44) - layoutParams = LinearLayout.LayoutParams(if (wide) LinearLayout.LayoutParams.WRAP_CONTENT else dp(context, 44), dp(context, 44)) - setOnClickListener { - performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY) - onClick() - } - } - - internal fun menuButton(context: Context, onClick: () -> Unit) = - iconButton(context, R.drawable.ic_stat_session, context.getString(R.string.touch_droiddeck_menu), onClick) - - internal fun iconButton(context: Context, icon: Int, description: String, onClick: () -> Unit) = android.widget.ImageView(context).apply { - setImageResource(icon) - setColorFilter(TEXT) - contentDescription = description - scaleType = android.widget.ImageView.ScaleType.CENTER_INSIDE - val pad = dp(context, 11) - setPadding(pad, pad, pad, pad) - background = outline(context, CORNER_PILL, SURFACE) - layoutParams = LinearLayout.LayoutParams(dp(context, 44), dp(context, 44)) - setOnClickListener { - performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY) - onClick() - } - } - } -} diff --git a/app/src/main/java/com/droiddeck/launcher/ui/FrontEndSetup.kt b/app/src/main/java/com/droiddeck/launcher/ui/FrontEndSetup.kt index 66dd4e0e1d..ac844177df 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/FrontEndSetup.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/FrontEndSetup.kt @@ -245,7 +245,7 @@ internal fun SetupPanel( PageHeader(stringResource(R.string.setup_title)) { Box(Modifier.weight(1f)) { SheetChips(tabs, tab, chipFocus, onPick = pick) } } - Column(modifier = Modifier.weight(1f).fillMaxWidth().verticalScroll(rememberScrollState()).padding(bottom = 24.dp)) { + Column(modifier = Modifier.weight(1f).fillMaxWidth().then(if (tab == 1) Modifier else Modifier.verticalScroll(rememberScrollState())).padding(bottom = if (tab == 1) 0.dp else 24.dp)) { when (tab) { 0 -> { // Setup's "Safe mode" row, under the GPU row while the bridge is on and can be used here. @@ -413,9 +413,7 @@ internal fun SetupPanel( } 1 -> { val controller = s.controller - if (controller != null && a.controller != null) SettingsGroup(stringResource(R.string.setup_controller)) { - ControllerRows(host, s.oscMode, controller, a.controller) - } + if (controller != null && a.controller != null) InputSettingsPage(onBack = { pick(0) }, embedded = true) if (s.controller == null || a.controller == null) Note(stringResource(R.string.setup_controller_unavailable)) } 2 -> { diff --git a/app/src/main/java/com/droiddeck/launcher/ui/InputActionEditor.kt b/app/src/main/java/com/droiddeck/launcher/ui/InputActionEditor.kt new file mode 100644 index 0000000000..a53de480bb --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/ui/InputActionEditor.kt @@ -0,0 +1,136 @@ +package com.droiddeck.launcher.ui + +import androidx.activity.compose.BackHandler +import androidx.compose.foundation.layout.* +import androidx.compose.material3.* +import androidx.compose.runtime.* +import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.platform.testTag +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.unit.dp +import com.droiddeck.launcher.R +import com.droiddeck.launcher.input.* + +@Composable +internal fun InputActionEditor( + control: InputControl, current: InputAction, title: String, onBack: () -> Unit, onSave: (InputAction) -> Unit, + element: InputSurfaceElement? = null, nativeTargets: Boolean = true, detail: String? = null, + properties: (@Composable ColumnScope.() -> Unit)? = null, + click: Boolean = false, +) { + val context = LocalContext.current + val host = rememberMenuHost() + var draft by remember(current, title) { mutableStateOf(current) } + val drafts = remember(current, title) { mutableMapOf() } + fun typeOf(action: InputAction): String = when (action) { + is InputAction.Native -> "native"; InputAction.Disabled -> "disabled" + is InputAction.MouseButton -> "mouse"; is InputAction.Command -> "command" + is InputAction.Pointer -> "pointer"; is InputAction.Scroll -> "scroll" + is InputAction.Directions -> "directions"; is InputAction.Keys -> "keys" + } + var localType by remember(current, title) { mutableStateOf(typeOf(current).takeUnless { it == "native" || it == "disabled" } ?: "keys") } + fun switch(type: String, initial: () -> InputAction) { + drafts[typeOf(draft)] = draft + draft = drafts[type] ?: initial() + if (type != "native" && type != "disabled") localType = type + } + var keys by remember { mutableStateOf(false) } + var direction by remember { mutableStateOf(null) } + fun valid(action: InputAction) = if (click) action.validForClick() else if (element != null) action.validFor(element) else action.validFor(control) + val nativeOptions = remember(control, element, nativeTargets, click) { + (listOf(InputAction.Native()) + if (nativeTargets) InputControl.entries.map { InputAction.Native(it) } else emptyList()).filter(::valid) + } + val route = when (draft) { is InputAction.Native -> 0; InputAction.Disabled -> 2; else -> 1 } + BackHandler(onBack = onBack) + SettingsPage(host, title, onBack, eyebrow = stringResource(R.string.input_properties), lede = detail, + action = { PrimaryButton(stringResource(R.string.gesture_save_changes), enabled = valid(draft), + modifier = Modifier.testTag("input-action-save")) { onSave(draft) } }) { + properties?.invoke(this) + Spacer(Modifier.height(12.dp)) + SheetChips(listOf(stringResource(R.string.input_route_controller), stringResource(R.string.input_route_local), stringResource(R.string.input_disabled)), route) { + when (it) { + 0 -> switch("native") { nativeOptions.firstOrNull() ?: InputAction.Disabled } + 2 -> switch("disabled") { InputAction.Disabled } + else -> switch(localType) { InputAction.Keys(listOf(28)) } + } + } + Spacer(Modifier.height(16.dp)) + when (route) { + 0 -> SettingsGroup(stringResource(R.string.input_route_controller)) { + ChoiceRow(host, "native-target", stringResource(R.string.input_remap_native), stringResource(R.string.input_controller_route_hint), + nativeOptions.map { it to (it.target?.label ?: stringResource(if (nativeTargets) R.string.input_native else R.string.input_normal_binding)) }, + draft as InputAction.Native) { draft = it } + } + 2 -> Text(stringResource(R.string.input_disabled_hint), Modifier.padding(16.dp)) + else -> { + val types = listOf("keys" to stringResource(R.string.input_keys), "mouse" to stringResource(R.string.input_mouse_click), + "command" to stringResource(R.string.input_command)) + if (control.dimensions == 2) listOf( + "pointer" to stringResource(R.string.input_pointer), "scroll" to stringResource(R.string.input_scroll), + "directions" to stringResource(R.string.input_direction_keys)) else emptyList() + val type = when (draft) { is InputAction.MouseButton -> "mouse"; is InputAction.Command -> "command"; + is InputAction.Pointer -> "pointer"; is InputAction.Scroll -> "scroll"; is InputAction.Directions -> "directions"; else -> "keys" } + SettingsGroup(stringResource(R.string.input_action)) { + ChoiceRow(host, "action-type", stringResource(R.string.input_action), null, types, type) { + switch(it) { when (it) { "mouse" -> InputAction.MouseButton(272); "command" -> InputAction.Command(InputCommand.MENU); + "pointer" -> InputAction.Pointer(); "scroll" -> InputAction.Scroll(); + "directions" -> InputAction.Directions(103, 106, 108, 105); else -> InputAction.Keys(listOf(28)) } } + } + if (!click && draft !is InputAction.Pointer && draft !is InputAction.Scroll && draft !is InputAction.Directions) { + val hint = when { + control.isTrackpad -> R.string.input_contact_action_hint + control.dimensions == 2 -> R.string.input_axis_action_hint + control == InputControl.LT || control == InputControl.RT -> R.string.input_trigger_action_hint + else -> null + } + hint?.let { Text(stringResource(it), Modifier.padding(horizontal = 16.dp, vertical = 8.dp), + style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant) } + } + when (val action = draft) { + is InputAction.Keys -> ActionRow(stringResource(R.string.input_keys), actionLabel(action), stringResource(R.string.ctrl_edit), { keys = true }) + is InputAction.MouseButton -> ChoiceRow(host, "mouse-button", stringResource(R.string.input_mouse_click), null, + (272..274).map { it to actionLabel(InputAction.MouseButton(it)) }, action.code) { draft = InputAction.MouseButton(it) } + is InputAction.Command -> ChoiceRow(host, "command", stringResource(R.string.input_command), null, + InputCommand.entries.map { it to commandLabel(it) }, action.command) { draft = InputAction.Command(it) } + is InputAction.Pointer -> { + Text(stringResource(R.string.input_pointer_speed, action.speed.toInt()), Modifier.padding(horizontal = 16.dp, vertical = 8.dp), color = MaterialTheme.colorScheme.onSurface) + Slider(action.speed, { draft = action.copy(speed = it) }, valueRange = 100f..3000f, modifier = Modifier.padding(horizontal = 16.dp)) + } + is InputAction.Scroll -> { + ChoiceRow(host, "scroll-axis", stringResource(R.string.input_scroll_axis), null, + listOf(false to stringResource(R.string.input_vertical), true to stringResource(R.string.input_horizontal)), action.horizontal) { + draft = action.copy(horizontal = it) + } + Text(stringResource(R.string.input_scroll_speed, action.speed.toInt()), Modifier.padding(horizontal = 16.dp, vertical = 8.dp), color = MaterialTheme.colorScheme.onSurface) + Slider(action.speed, { draft = action.copy(speed = it) }, valueRange = 1f..60f, modifier = Modifier.padding(horizontal = 16.dp)) + } + is InputAction.Directions -> { + ToggleRow(host, "direction-diagonals", stringResource(R.string.input_diagonal_keys), stringResource(R.string.input_diagonal_keys_hint), action.diagonals != null) { + draft = action.copy(diagonals = if (it) action.diagonals ?: listOf(0, 0, 0, 0) else null) + } + val codes = listOf(action.up, action.right, action.down, action.left) + action.diagonals.orEmpty() + val arrows = listOf("↑", "→", "↓", "←", "↗", "↘", "↙", "↖") + codes.forEachIndexed { i, code -> + ActionRow(arrows[i], if (code == 0) stringResource(if (i < 4) R.string.input_disabled else R.string.input_diagonal_neighbors) else pcKeyLabel(context, code), stringResource(R.string.ctrl_edit), { direction = i }) + } + } + else -> Unit + } + } + } + } + } + if (keys) KeyCombinationPicker(onDismiss = { keys = false }, initial = (draft as? InputAction.Keys)?.codes.orEmpty(), + sequence = true, sequenceLimit = 8, title = stringResource(R.string.input_keys), confirm = stringResource(R.string.gesture_save_changes), onAdd = { + draft = InputAction.Keys(it.sortedBy { code -> if (code in PC_MODIFIERS) 0 else 1 }); keys = false + }) + direction?.let { at -> + val action = draft as? InputAction.Directions ?: return@let + val codes = listOf(action.up, action.right, action.down, action.left) + action.diagonals.orEmpty() + KeyCombinationPicker(onDismiss = { direction = null }, initial = listOf(codes[at]).filter { it != 0 }, sequence = true, sequenceLimit = 1, allowEmpty = true, + title = stringResource(R.string.input_direction_keys), confirm = stringResource(R.string.gesture_save_changes), onAdd = { + val next = codes.toMutableList().also { values -> values[at] = it.firstOrNull() ?: 0 } + draft = InputAction.Directions(next[0], next[1], next[2], next[3], if (action.diagonals != null) next.drop(4) else null); direction = null + }) + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/ui/InputBindingWidgets.kt b/app/src/main/java/com/droiddeck/launcher/ui/InputBindingWidgets.kt new file mode 100644 index 0000000000..a8eb95b347 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/ui/InputBindingWidgets.kt @@ -0,0 +1,79 @@ +package com.droiddeck.launcher.ui + +import androidx.compose.foundation.layout.Column +import androidx.compose.ui.focus.FocusRequester +import androidx.compose.ui.focus.focusRequester +import androidx.compose.material3.* +import androidx.compose.runtime.* +import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.window.DialogProperties +import com.droiddeck.launcher.R +import com.droiddeck.launcher.input.* + +@Composable +internal fun actionLabel(action: InputAction): String { + val context = LocalContext.current + return when (action) { + is InputAction.Native -> action.target?.let { stringResource(R.string.input_native_target, it.label) } ?: stringResource(R.string.input_native) + InputAction.Disabled -> stringResource(R.string.input_disabled) + is InputAction.Keys -> action.codes.joinToString(" + ") { pcKeyLabel(context, it) } + is InputAction.MouseButton -> stringResource(when (action.code) { 272 -> R.string.input_mouse_left; 273 -> R.string.input_mouse_right; else -> R.string.input_mouse_middle }) + is InputAction.Command -> commandLabel(action.command) + is InputAction.Directions -> listOf(action.up, action.right, action.down, action.left).joinToString(" / ") { pcKeyLabel(context, it) } + is InputAction.Pointer -> stringResource(R.string.input_pointer) + is InputAction.Scroll -> stringResource(if (action.horizontal) R.string.input_scroll_horizontal else R.string.input_scroll_vertical) + } +} + +@Composable +internal fun commandLabel(command: InputCommand) = stringResource(when (command) { + InputCommand.MENU -> R.string.input_command_menu + InputCommand.KEYBOARD -> R.string.input_command_keyboard + InputCommand.STEAM -> R.string.input_command_steam + InputCommand.QUICK_ACCESS -> R.string.input_command_qam +}) + +@Composable +internal fun InputCaptureDialog(onDismiss: () -> Unit, onPick: (PhysicalInputKey) -> Unit) { + val confirmFocus = remember { FocusRequester() } + var captured by remember { mutableStateOf(null) } + var capturedDevice by remember { mutableIntStateOf(-1) } + var ready by remember { mutableStateOf(false) } + AlertDialog(onDismissRequest = onDismiss, title = { Text(stringResource(R.string.input_capture)) }, + properties = DialogProperties(dismissOnBackPress = ready), + text = { + CaptureShortcutKeys { event -> + if (ready) { + if (event.keyCode == android.view.KeyEvent.KEYCODE_BACK || event.keyCode == android.view.KeyEvent.KEYCODE_BUTTON_B) { + if (event.action == android.view.KeyEvent.ACTION_DOWN && event.repeatCount == 0) onDismiss() + true + } else false + } else { + val key = PhysicalInputKey.capture(event) + if (key == null) false else { + if (captured == null && event.action == android.view.KeyEvent.ACTION_DOWN && event.repeatCount == 0) { + captured = key + capturedDevice = event.deviceId + } else if (captured == key && capturedDevice == event.deviceId && event.action == android.view.KeyEvent.ACTION_UP) ready = true + true + } + } + } + Column { + Text(captured?.label ?: stringResource(R.string.input_capture_hint)) + if (captured != null) Text(stringResource(R.string.input_capture_confirm)) + } + }, + confirmButton = { + TextButton(enabled = ready, modifier = Modifier.focusRequester(confirmFocus) + .controllerConfirm(ready) { captured?.let(onPick) }, onClick = { captured?.let(onPick) }) { Text(stringResource(R.string.ctrl_edit)) } + LaunchedEffect(ready) { if (ready) confirmFocus.requestFocus() } + }, + dismissButton = { + TextButton(modifier = Modifier.controllerConfirm { onDismiss() }, onClick = onDismiss) { Text(stringResource(R.string.gesture_cancel)) } + if (ready) TextButton(modifier = Modifier.controllerConfirm { captured = null; ready = false }, + onClick = { captured = null; ready = false }) { Text(stringResource(R.string.input_capture_retry)) } + }) +} diff --git a/app/src/main/java/com/droiddeck/launcher/ui/InputDisplayContent.kt b/app/src/main/java/com/droiddeck/launcher/ui/InputDisplayContent.kt new file mode 100644 index 0000000000..65bcd52be6 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/ui/InputDisplayContent.kt @@ -0,0 +1,67 @@ +package com.droiddeck.launcher.ui + +import android.app.Presentation +import android.graphics.Color +import android.graphics.drawable.ColorDrawable +import android.os.Bundle +import android.view.Display +import android.view.WindowManager +import androidx.activity.ComponentActivity +import androidx.activity.setViewTreeOnBackPressedDispatcherOwner +import androidx.compose.runtime.* +import androidx.compose.ui.platform.ComposeView +import androidx.lifecycle.Lifecycle +import androidx.lifecycle.LifecycleEventObserver +import androidx.lifecycle.setViewTreeLifecycleOwner +import androidx.lifecycle.setViewTreeViewModelStoreOwner +import androidx.savedstate.setViewTreeSavedStateRegistryOwner + +internal val LocalInputActivity = staticCompositionLocalOf { null } + +@Composable +internal fun InputDisplayContent(host: ComponentActivity, display: Display, onDismiss: () -> Unit, content: @Composable () -> Unit) { + val parent = rememberCompositionContext() + val currentContent by rememberUpdatedState(content) + val dismiss by rememberUpdatedState(onDismiss) + DisposableEffect(host, display.displayId) { + val presentation = object : Presentation(host, display) { + var compose: ComposeView? = null + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + window?.apply { + setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT)) + clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND) + prepareInputEditor() + decorView.setViewTreeLifecycleOwner(host) + decorView.setViewTreeViewModelStoreOwner(host) + decorView.setViewTreeSavedStateRegistryOwner(host) + decorView.setViewTreeOnBackPressedDispatcherOwner(host) + } + // Presentation contexts cannot host TYPE_APPLICATION dialogs on Android 12+. + compose = ComposeView(host).also { + it.setParentCompositionContext(parent) + it.setContent { CompositionLocalProvider(LocalInputActivity provides host) { currentContent() } } + setContentView(it) + } + } + override fun dismiss() { compose?.disposeComposition(); compose = null; super.dismiss() } + } + presentation.setOnCancelListener { dismiss() } + fun showPresentation() { + if (!display.isValid) { dismiss(); return } + try { + presentation.show() + } catch (_: WindowManager.InvalidDisplayException) { + presentation.dismiss() + dismiss() + } + } + showPresentation() + val observer = LifecycleEventObserver { _, event -> + if (event == Lifecycle.Event.ON_STOP) presentation.hide() + if (event == Lifecycle.Event.ON_START) showPresentation() + } + host.lifecycle.addObserver(observer) + onDispose { host.lifecycle.removeObserver(observer); presentation.dismiss() } + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/ui/InputElementProperties.kt b/app/src/main/java/com/droiddeck/launcher/ui/InputElementProperties.kt new file mode 100644 index 0000000000..6236ac7af4 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/ui/InputElementProperties.kt @@ -0,0 +1,54 @@ +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 +import androidx.compose.ui.graphics.nativeCanvas +import com.droiddeck.launcher.input.* +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.unit.dp +import com.droiddeck.launcher.R + +@Composable +internal fun InputElementProperties(element: InputSurfaceElement, onChange: (InputSurfaceElement) -> Unit) { + OutlinedTextField(element.name, { onChange(element.copy(name = it.take(80))) }, label = { Text(stringResource(R.string.input_name)) }, + singleLine = true, modifier = Modifier.fillMaxWidth().padding(bottom = 16.dp)) + if (element.kind == InputElementKind.BUTTON || element.kind == InputElementKind.TRIGGER) { + val host = rememberMenuHost() + val context = LocalContext.current + ToggleRow(host, "element-toggle", stringResource(R.string.input_toggle_button), stringResource(R.string.input_toggle_button_hint), element.toggle) { onChange(element.copy(toggle = it)) } + ChoiceRow(host, "element-shape", stringResource(R.string.input_button_shape), null, + InputButtonShape.entries.map { it to stringResource(when (it) { InputButtonShape.ROUNDED -> R.string.input_shape_rounded; InputButtonShape.PILL -> R.string.input_shape_pill; InputButtonShape.CIRCLE -> R.string.input_shape_circle; InputButtonShape.RECTANGLE -> R.string.input_shape_rectangle }) }, + element.buttonShape) { onChange(element.copy(buttonShape = it)) } + 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 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(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)) { + 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 } + } + } + } + } + if (element.kind == InputElementKind.TRACKPAD) { + ToggleRow(rememberMenuHost(), "element-mouse-gestures", stringResource(R.string.input_mouse_gestures), stringResource(R.string.input_mouse_gestures_hint), element.mouseGestures) { onChange(element.copy(mouseGestures = it)) } + } + if (element.kind == InputElementKind.STICK) { + val host = rememberMenuHost() + ToggleRow(host, "element-floating", stringResource(R.string.input_floating_stick), null, element.floating) { onChange(element.copy(floating = it)) } + ToggleRow(host, "element-hide-idle", stringResource(R.string.input_hide_idle), null, element.hideWhenIdle) { onChange(element.copy(hideWhenIdle = it)) } + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/ui/InputLayoutWidgets.kt b/app/src/main/java/com/droiddeck/launcher/ui/InputLayoutWidgets.kt new file mode 100644 index 0000000000..c4c2047d0a --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/ui/InputLayoutWidgets.kt @@ -0,0 +1,88 @@ +package com.droiddeck.launcher.ui + +import android.content.Context +import android.graphics.Canvas +import android.view.View +import androidx.compose.foundation.background +import androidx.compose.foundation.border +import androidx.compose.foundation.clickable +import androidx.compose.foundation.layout.* +import androidx.compose.foundation.shape.CircleShape +import androidx.compose.material3.* +import androidx.compose.runtime.* +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.platform.testTag +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.unit.dp +import androidx.compose.ui.viewinterop.AndroidView +import com.droiddeck.launcher.R +import com.droiddeck.launcher.input.* + +@Composable +internal fun InputLayoutRow(layout: SavedInputLayout, role: InputRole, detail: String?, onManage: () -> Unit, onPick: () -> Unit) { + Row(Modifier.fillMaxWidth().clickable(onClick = onPick).padding(12.dp), + verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(12.dp)) { + InputLayoutThumbnail(layout, role) + Column(Modifier.weight(1f), verticalArrangement = Arrangement.spacedBy(4.dp)) { + Text(layout.name, style = MaterialTheme.typography.titleMedium) + Text(detail ?: stringResource(R.string.input_layout_count, layout.elements.size), + style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant) + } + TextButton(onClick = onManage, modifier = Modifier.testTag("layout-manage-${layout.id}")) { Text("⋮", style = MaterialTheme.typography.titleLarge) } + } +} + +@Composable +internal fun InputLayoutThumbnail(layout: SavedInputLayout, role: InputRole) { + AndroidView(factory = { InputLayoutPreviewView(it) }, modifier = Modifier.width(112.dp).height(76.dp).testTag("layout-preview-${layout.id}"), + update = { it.update(layout, role) }) +} + +internal class InputLayoutPreviewView(context: Context) : View(context) { + private val renderer = InputSurfaceView(context) + private var sourceHeight = 563 + fun update(layout: SavedInputLayout, role: InputRole) { + sourceHeight = if (role == InputRole.PRIMARY) 563 else 870 + renderer.updateCustomLayout(layout.elements) + renderer.updateStyle(layout.style.copy(opacity = 100)) + renderer.measure(MeasureSpec.makeMeasureSpec(1000, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(sourceHeight, MeasureSpec.EXACTLY)) + renderer.layout(0, 0, 1000, sourceHeight) + contentDescription = layout.name + invalidate() + } + override fun onDraw(canvas: Canvas) { + canvas.save() + canvas.clipRect(0, 0, width, height) + canvas.drawColor(android.graphics.Color.rgb(10, 11, 13)) + val scale = minOf(width / 1000f, height / sourceHeight.toFloat()) + canvas.translate((width - 1000 * scale) / 2, (height - sourceHeight * scale) / 2) + canvas.scale(scale, scale) + renderer.draw(canvas) + canvas.restore() + } +} + +@Composable +internal fun InputColorRow(host: MenuHost, value: Int, onPick: (Int) -> Unit) { + val open = host.open == "layout-color" + SettingsRow(stringResource(R.string.ctrl_color), null, highlighted = open) { + Box { + ValueChip(ControllerPrefs.tints.firstOrNull { it.first == value }?.second?.let { stringResource(it) } + ?: stringResource(R.string.ctrl_custom), open, modifier = Modifier.testTag("setting-layout-color")) { + host.open = if (open) null else "layout-color" + } + AnchoredMenu(open, onDismiss = { if (host.open == "layout-color") host.open = null }, title = stringResource(R.string.ctrl_color)) { firstItemFocus -> + ControllerPrefs.tints.forEachIndexed { index, (color, name) -> + MenuItem(stringResource(name), checked = value == color, leading = { + Box(Modifier.size(14.dp).background(Color(color), CircleShape).border(1.dp, Color.White.copy(alpha = .35f), CircleShape)) + }, focusRequester = if (index == 0) firstItemFocus else null) { + onPick(color) + host.open = null + } + } + } + } + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/ui/InputQuickSettings.kt b/app/src/main/java/com/droiddeck/launcher/ui/InputQuickSettings.kt new file mode 100644 index 0000000000..ab7dcee975 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/ui/InputQuickSettings.kt @@ -0,0 +1,82 @@ +package com.droiddeck.launcher.ui + +import android.content.Context +import android.content.SharedPreferences +import androidx.compose.foundation.layout.* +import androidx.compose.foundation.clickable +import androidx.compose.foundation.BorderStroke +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.ui.Alignment +import androidx.compose.ui.unit.dp +import androidx.compose.ui.platform.testTag +import androidx.compose.material3.* +import androidx.compose.runtime.* +import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.res.stringResource +import com.droiddeck.launcher.R +import com.droiddeck.launcher.input.* + +@Composable +internal fun InputQuickSettings(host: MenuHost, desktop: Boolean, onChanged: (InputRole?) -> Unit = {}, onEdit: () -> Unit = {}, track: (String) -> Modifier = { Modifier }) { + val context = LocalContext.current + var library by remember { mutableStateOf(InputLibraries.read(context)) } + var expanded by remember { mutableStateOf(false) } + var naming by remember { mutableStateOf(false) } + var failure by remember { mutableStateOf(null) } + DisposableEffect(context) { + val preferences = context.getSharedPreferences("input-profiles", Context.MODE_PRIVATE) + val listener = SharedPreferences.OnSharedPreferenceChangeListener { _, key -> if (key == "library") library = InputLibraries.read(context) } + preferences.registerOnSharedPreferenceChangeListener(listener) + onDispose { preferences.unregisterOnSharedPreferenceChangeListener(listener) } + } + fun update(role: InputRole?, change: (InputLibrary) -> InputLibrary) { + runCatching { InputLibraries.update(context, change) }.onSuccess { library = it; onChanged(role) } + .onFailure { failure = it.message ?: context.getString(R.string.input_save_failed) } + } + val selection = library.selection(desktop) + val mixed = selection.parts.isNotEmpty() + val active = library.profile(library.activeId(desktop)) + val profiles = listOf(InputProfiles.AUTO to stringResource(R.string.input_quick_auto, if (desktop) library.profile(library.desktop).name else library.profile(library.steam).name)) + + library.profiles.map { it.id to it.name } + if (mixed) listOf("mixed" to stringResource(R.string.input_quick_mixed, active.name)) else emptyList() + Row(Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) { + Row(Modifier.weight(1f).clickable { expanded = !expanded }.padding(14.dp).testTag("input-profile-expand"), + verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(10.dp)) { + Text(if (expanded) "▾" else "▸", color = MaterialTheme.colorScheme.onSurface) + Text(stringResource(R.string.input_profile), style = MaterialTheme.typography.titleMedium, color = MaterialTheme.colorScheme.onSurface) + } + Box(Modifier.padding(end = 14.dp).widthIn(max = 280.dp)) { + ChoiceChip(host, "input-profile", stringResource(R.string.input_profile), profiles, if (mixed) "mixed" else selection.profile, + modifier = track("input-profile")) { id -> if (id != "mixed") update(null) { it.selectProfile(desktop, id) } } + } + } + if (expanded) Surface( + modifier = Modifier.fillMaxWidth().padding(start = 18.dp, end = 12.dp, bottom = 14.dp).testTag("input-profile-parts"), + shape = RoundedCornerShape(12.dp), + color = MaterialTheme.colorScheme.primary.copy(alpha = .08f), + border = BorderStroke(1.dp, MaterialTheme.colorScheme.primary.copy(alpha = .35f)), + ) { + Column(Modifier.padding(vertical = 6.dp)) { + for (role in InputRole.entries) { + val options = library.bindings.filter { binding -> + if (role == InputRole.PHYSICAL) binding.physical + else !binding.physical && library.layouts.any { it.id == binding.layoutId && it.supports(role) } + }.map { it.id to it.name } + ChoiceRow(host, "input-part-${role.name}", roleLabel(role), null, options, library.activePart(desktop, role).bindings, + chipModifier = track("input-part-${role.name}")) { id -> update(role) { it.selectPart(desktop, role, id) } } + } + if (mixed) SecondaryButton(stringResource(R.string.input_save_combination)) { naming = true } + ActionRow(stringResource(R.string.input_settings), null, stringResource(R.string.ctrl_edit), onEdit, + modifier = track("input-settings").testTag("input-profile-edit")) + } + } + if (naming) { + var name by remember { mutableStateOf("") } + AlertDialog(onDismissRequest = { naming = false }, title = { Text(stringResource(R.string.input_save_combination)) }, + text = { OutlinedTextField(name, { name = it.take(80) }, singleLine = true, label = { Text(stringResource(R.string.input_name)) }) }, + confirmButton = { TextButton(enabled = name.isNotBlank(), onClick = { update(null) { it.saveSelection(desktop, name) }; naming = false }) { Text(stringResource(R.string.gesture_save_changes)) } }, + dismissButton = { TextButton(onClick = { naming = false }) { Text(stringResource(R.string.common_cancel)) } }) + } + failure?.let { message -> AlertDialog(onDismissRequest = { failure = null }, title = { Text(stringResource(R.string.input_save_failed)) }, + text = { Text(message) }, confirmButton = { TextButton(onClick = { failure = null }) { Text(stringResource(R.string.common_ok)) } }) } +} diff --git a/app/src/main/java/com/droiddeck/launcher/ui/InputSettingsPage.kt b/app/src/main/java/com/droiddeck/launcher/ui/InputSettingsPage.kt new file mode 100644 index 0000000000..53c777be35 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/ui/InputSettingsPage.kt @@ -0,0 +1,411 @@ +package com.droiddeck.launcher.ui + +import androidx.activity.compose.BackHandler +import androidx.compose.foundation.clickable +import androidx.compose.foundation.layout.* +import androidx.compose.foundation.rememberScrollState +import androidx.compose.foundation.verticalScroll +import androidx.compose.material3.* +import androidx.compose.runtime.* +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.platform.testTag +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.semantics.Role +import androidx.compose.ui.unit.dp +import androidx.compose.ui.window.Dialog +import androidx.compose.ui.window.DialogProperties +import com.droiddeck.launcher.R +import com.droiddeck.launcher.input.* +import com.droiddeck.launcher.session.SessionPrefs + +@Composable +fun InputSettingsDialog(onBack: () -> Unit, onChanged: () -> Unit) { + val context = LocalContext.current + val activity = context.findActivity() as? com.droiddeck.launcher.SessionActivity + val target = activity?.inputEditorDisplay(InputRole.SECONDARY) + if (activity != null && target != null && target.displayId != activity.window.decorView.display?.displayId) { + InputDisplayContent(activity, target, onBack) { InputSettingsPane(onBack, onChanged) } + } else InputSettingsWindow(onBack, onChanged) +} + +@Composable +private fun InputSettingsWindow(onBack: () -> Unit, onChanged: () -> Unit) { + Dialog(onDismissRequest = onBack, properties = DialogProperties(usePlatformDefaultWidth = false)) { + InputSettingsPane(onBack, onChanged) + } +} + +@Composable +private fun InputSettingsPane(onBack: () -> Unit, onChanged: () -> Unit) { + var editingSurface by remember { mutableStateOf(false) } + val window = (androidx.compose.ui.platform.LocalView.current.parent as? androidx.compose.ui.window.DialogWindowProvider)?.window + DisposableEffect(window, editingSurface) { + window?.setDimAmount(if (editingSurface) 0f else .32f) + onDispose { } + } + BoxWithConstraints(Modifier.fillMaxSize().systemBarsPadding().padding(8.dp)) { + CompositionLocalProvider(LocalNarrowPane provides (maxWidth < NarrowPaneWidth)) { + Surface(Modifier.fillMaxSize(), color = if (editingSurface) androidx.compose.ui.graphics.Color.Transparent else MaterialTheme.colorScheme.surface) { + InputSettingsPage(onBack, onChanged, onSurfaceEditing = { editingSurface = it }) + } + } + } +} + +private enum class InputPage { HOME, PROFILE, SOURCE, LAYOUTS, REUSE } +private data class InputBindingEdit(val preset: String, val control: InputControl, val element: InputSurfaceElement? = null, val key: PhysicalInputKey? = null, val click: Boolean = false) +private data class InputNameRequest(val title: Int, val initial: String, val sources: List> = emptyList(), + val source: String = "", val offerSharing: Boolean = false, val save: (String, String, Boolean) -> Unit) +private data class InputConfirmation(val title: String, val detail: String, val action: () -> Unit) +private data class InputReuseRequest(val name: String, val layoutOnly: Boolean, val apply: (Boolean) -> Unit) + +@Composable +fun InputSettingsPage(onBack: () -> Unit, onChanged: () -> Unit = {}, embedded: Boolean = false, onSurfaceEditing: (Boolean) -> Unit = {}) { + CompositionLocalProvider(LocalContentColor provides MaterialTheme.colorScheme.onBackground) { + InputSettingsContent(onBack, onChanged, embedded, onSurfaceEditing) + } +} + +@Composable +private fun InputSettingsContent(onBack: () -> Unit, onChanged: () -> Unit, embedded: Boolean, onSurfaceEditing: (Boolean) -> Unit) { + val context = LocalContext.current + val host = rememberMenuHost() + var library by remember { mutableStateOf(InputLibraries.read(context)) } + var page by remember { mutableStateOf(InputPage.HOME) } + var bindingsTab by remember { mutableIntStateOf(0) } + var profileId by remember { mutableStateOf(InputProfile.GAME) } + var role by remember { mutableStateOf(InputRole.PHYSICAL) } + var edit by remember { mutableStateOf(null) } + var naming by remember { mutableStateOf(null) } + var confirmation by remember { mutableStateOf(null) } + var reuse by remember { mutableStateOf(null) } + var capture by remember { mutableStateOf(false) } + var surface by remember { mutableStateOf(false) } + LaunchedEffect(surface) { onSurfaceEditing(surface) } + DisposableEffect(Unit) { onDispose { onSurfaceEditing(false) } } + var managingLayout by remember { mutableStateOf(null) } + var error by remember { mutableStateOf(null) } + val controller = rememberControllerSettings() + var oscMode by remember { mutableStateOf(SessionPrefs.oscMode(context)) } + fun update(change: (InputLibrary) -> InputLibrary): Boolean = runCatching { InputLibraries.update(context, change) } + .onSuccess { library = it; onChanged() }.onFailure { error = it.message ?: context.getString(R.string.input_save_failed) }.isSuccess + fun go(next: InputPage) { page = next; host.open = null } + fun back() { + when { + edit != null -> edit = null + page == InputPage.HOME -> onBack() + page == InputPage.PROFILE -> go(InputPage.HOME) + page == InputPage.SOURCE -> go(InputPage.PROFILE) + else -> go(InputPage.SOURCE) + } + } + BackHandler(enabled = !embedded || page != InputPage.HOME || edit != null, onBack = ::back) + val profile = library.profile(profileId) + val preset = library.preset(profile.binding(role)) + val layout = library.layout(profileId, role) + fun rename(title: Int, name: String, save: (String) -> Unit) { + naming = InputNameRequest(title, name) { value, _, _ -> save(value) } + } + fun newProfile(copy: Boolean) { + naming = InputNameRequest(if (copy) R.string.input_copy_profile else R.string.input_new_profile, + if (copy) profile.name else "", library.profiles.map { it.id to it.name }, profileId, offerSharing = true) { name, source, shared -> + val id = newInputId() + if (update { it.duplicateProfile(source, name, id, sharePhysical = shared) }) { profileId = id; go(InputPage.PROFILE) } + } + } + fun editPart(nextRole: InputRole) { role = nextRole; bindingsTab = 0; go(InputPage.SOURCE) } + val editing = edit + if (editing != null) { + val b = library.preset(editing.preset) + var elementProperties by remember(editing) { mutableStateOf(editing.element) } + val current = editing.element?.let { if (editing.click) b.clickAction(it) else b.action(it) } + ?: editing.key?.let { k -> b.keys.firstOrNull { it.key == k }?.action ?: InputAction.Disabled } + ?: b.controls[editing.control] ?: InputAction.Native() + InputActionEditor(editing.control, current, if (editing.click) stringResource(R.string.input_click_name, editing.element!!.name) + else editing.element?.name ?: editing.key?.label ?: editing.control.label, + properties = { if (!editing.click) elementProperties?.let { e -> InputElementProperties(e) { elementProperties = it } } }, + onBack = { edit = null }, element = editing.element, nativeTargets = editing.key == null, click = editing.click, + detail = if (library.usersOfBindings(b.id).size > 1) stringResource(R.string.input_shared_detail, b.name, library.usersOfBindings(b.id).size) + else "${profile.name} · ${roleLabel(role)}", onSave = { action -> + if (update { l -> + val geometry = if (!editing.click && elementProperties != null && layout != null) l.withLayout(layout.copy(elements = layout.elements.map { + if (it.id == elementProperties!!.id) elementProperties!!.copy(name = elementProperties!!.name.trim().ifBlank { it.name }) else it + })) else l + geometry.withPreset(when { + editing.element != null && editing.click -> b.copy(clicks = b.clicks + (editing.element.id to action)) + editing.element != null -> b.copy(elements = b.elements + (editing.element.id to action)) + editing.key != null -> b.copy(keys = b.keys.filterNot { it.key == editing.key } + PhysicalInputBinding(editing.key, action)) + else -> b.copy(controls = b.controls + (editing.control to action)) + }) }) edit = null + }) + } else if (!surface) key(page, profileId, role) { + SettingsPage(host, title = when (page) { + InputPage.HOME -> if (embedded) null else stringResource(R.string.setup_controller) + InputPage.PROFILE -> profile.name + InputPage.SOURCE -> roleLabel(role) + InputPage.LAYOUTS -> stringResource(R.string.input_saved_layouts) + InputPage.REUSE -> stringResource(R.string.input_use_settings_from) + }, eyebrow = when (page) { + InputPage.SOURCE -> profile.name + InputPage.LAYOUTS, InputPage.REUSE -> "${profile.name} · ${roleLabel(role)}" + else -> null + }, + onBack = ::back, showBack = !(embedded && page == InputPage.HOME), compactLayout = true) { + when (page) { + InputPage.HOME -> { + SettingsGroup(stringResource(R.string.input_default_profiles)) { + ChoiceRow(host, "auto-desktop", stringResource(R.string.input_desktop_mode), null, + library.profiles.map { it.id to it.name }, library.desktop) { id -> update { it.copy(desktop = id) } } + ChoiceRow(host, "auto-game", stringResource(R.string.input_game_mode), null, + library.profiles.map { it.id to it.name }, library.steam) { id -> update { it.copy(steam = id) } } + for (desktop in listOf(true, false)) { + val selection = library.selection(desktop) + if (selection.profile != InputProfiles.AUTO || selection.parts.isNotEmpty()) InputHint(stringResource(R.string.input_quick_override_hint, + stringResource(if (desktop) R.string.input_desktop_mode else R.string.input_game_mode))) + } + } + if (InputLibraries.hasPendingScreenKeyMigration(context)) InputHint(stringResource(R.string.input_import_failed)) + if (InputLibraries.hasLibraryRecovery(context)) InputHint(stringResource(R.string.input_library_recovered)) + if (library.profiles.any { it.id.startsWith("migrated.screenkeys.") }) Text(stringResource(R.string.input_imported_keys_hint), + Modifier.padding(16.dp), style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant) + SettingsGroup(stringResource(R.string.input_profiles)) { + for (p in library.profiles) InputNavigationRow(p.name, when { + p.id == library.steam && p.id == library.desktop -> stringResource(R.string.input_default_both) + p.id == library.steam -> stringResource(R.string.input_default_game) + p.id == library.desktop -> stringResource(R.string.input_default_desktop) + else -> null + }, Modifier.testTag("input-profile-${p.id}")) { profileId = p.id; go(InputPage.PROFILE) } + TextButton(onClick = { newProfile(false) }, modifier = Modifier.padding(8.dp)) { Text(stringResource(R.string.input_new_profile)) } + } + SettingsGroup(stringResource(R.string.input_device_settings)) { + ToggleRow(host, "controller-rumble", stringResource(R.string.ctrl_rumble), null, controller.rumble) { ControllerPrefs.setRumble(context, it); onChanged() } + ChoiceRow(host, "controller-dead-zone", stringResource(R.string.ctrl_dead_zone), null, ControllerPrefs.deadZones.map { it to stringResource(R.string.ctrl_percent, it) }, controller.deadZone) { ControllerPrefs.setDeadZone(context, it); onChanged() } + ToggleRow(host, "controller-joycon-vr", stringResource(R.string.ctrl_joycon_vr), stringResource(R.string.ctrl_joycon_vr_hint), controller.joyConVr) { ControllerPrefs.setJoyConVr(context, it); onChanged() } + } + SettingsGroup(stringResource(R.string.shortcuts_title)) { PhysicalShortcutRows() } + SettingsGroup(stringResource(R.string.input_latency)) { + var measuring by remember { mutableStateOf(InputTiming.enabled) } + var timing by remember { mutableStateOf(InputTiming.report()) } + InputHint(stringResource(R.string.input_latency_hint)) + ToggleRow(host, "input-measure", stringResource(R.string.input_measure), null, measuring) { measuring = it; InputTiming.enabled = it; if (it) InputTiming.reset() } + var reading by remember { mutableStateOf(false) } + ActionRow(stringResource(R.string.input_measurements), stringResource(R.string.input_measure_instructions), + stringResource(R.string.input_read_timing), { timing = InputTiming.report(); reading = true }) + if (reading) AlertDialog(onDismissRequest = { reading = false }, title = { Text(stringResource(R.string.input_measurements)) }, + text = { Column(verticalArrangement = Arrangement.spacedBy(12.dp)) { + Text(timing) + Text(stringResource(R.string.input_measure_instructions), style = MaterialTheme.typography.bodySmall) + } }, confirmButton = { TextButton(onClick = { reading = false }) { Text(stringResource(R.string.common_close)) } }) + } + } + InputPage.PROFILE -> { + SettingsGroup(stringResource(R.string.input_profile_controls)) { + for (r in InputRole.entries) InputNavigationRow(roleLabel(r), + if (r == InputRole.PHYSICAL) stringResource(R.string.input_physical_hint) else library.layout(profileId, r)?.name, + Modifier.testTag("input-part-${r.name.lowercase()}"), leading = { + library.layout(profileId, r)?.let { InputLayoutThumbnail(it, r) } + }) { editPart(r) } + } + Spacer(Modifier.height(16.dp)) + Actions { + SecondaryButton(stringResource(R.string.input_rename)) { rename(R.string.input_rename, profile.name) { name -> update { it.withProfile(profile.copy(name = name)) } } } + SecondaryButton(stringResource(R.string.input_copy_profile)) { newProfile(true) } + SecondaryButton(stringResource(R.string.input_reset_actions)) { + confirmation = InputConfirmation(context.getString(R.string.input_reset_actions), context.getString(R.string.input_reset_profile_detail, profile.name)) { update { it.resetProfile(profileId) } } + } + if (profileId !in listOf(InputProfile.GAME, InputProfile.DESKTOP)) SecondaryButton(stringResource(R.string.input_delete)) { + confirmation = InputConfirmation(context.getString(R.string.input_delete), context.getString(R.string.input_delete_profile_detail, profile.name)) { + if (update { it.removeProfile(profileId) }) { profileId = InputProfile.GAME; go(InputPage.HOME) } + } + } + } + } + InputPage.SOURCE -> { + if (role == InputRole.PRIMARY) SettingsGroup(stringResource(R.string.input_screen_visibility)) { + ChoiceRow(host, "controller-osc", stringResource(R.string.mode_osc), stringResource(R.string.input_visibility_shared), + listOf(SessionPrefs.OSC_AUTO to stringResource(R.string.common_auto), SessionPrefs.OSC_ALWAYS to stringResource(R.string.common_always), + SessionPrefs.OSC_STEAM_TOUCH to stringResource(R.string.osc_steam_touch), SessionPrefs.OSC_STEAM_QAM to stringResource(R.string.mode_osc_qam), SessionPrefs.OSC_NEVER to stringResource(R.string.common_never)), + oscMode) { SessionPrefs.setOscMode(context, it); oscMode = it; onChanged() } + } + if (layout != null) SettingsGroup(stringResource(R.string.ctrl_layout)) { + if (layout.editable) InputNavigationRow(stringResource(R.string.input_edit_layout), layout.name, Modifier.testTag("input-surface-${role.name.lowercase()}")) { surface = true } + else InputHint(layout.name) + InputNavigationRow(stringResource(R.string.input_saved_layouts), stringResource(R.string.input_saved_layouts_hint)) { go(InputPage.LAYOUTS) } + if (layout.editable) { + InputColorRow(host, layout.style.tint) { color -> update { it.withLayout(layout.copy(style = layout.style.copy(tint = color))) } } + SliderRow(stringResource(R.string.ctrl_opacity), null, layout.style.opacity, 1..100, step = 1, + format = { "$it%" }) { opacity -> update { it.withLayout(layout.copy(style = layout.style.copy(opacity = opacity))) } } + } + if (role == InputRole.SECONDARY) InputHint(stringResource(R.string.input_second_screen_enable)) + } + SettingsGroup(stringResource(R.string.input_assign_actions)) { + val users = library.usersOfBindings(preset.id) + if (users.size > 1) InputHint(stringResource(R.string.input_shared_with, users.filterNot { it.id == profileId }.joinToString { it.name })) + val elements = layout?.takeIf { it.editable }?.elements + if (elements != null) { + if (elements.isEmpty()) InputHint(stringResource(R.string.input_empty_elements)) + for (e in elements) { + InputNavigationRow(e.name, actionLabel(preset.action(e)), Modifier.testTag("input-element-${e.id}")) { edit = InputBindingEdit(preset.id, e.control, e) } + if (e.hasClick) InputNavigationRow(stringResource(R.string.input_click_name, e.name), actionLabel(preset.clickAction(e)), Modifier.testTag("input-click-${e.id}")) { edit = InputBindingEdit(preset.id, InputControl.A, e, click = true) } + } + } else { + Box(Modifier.padding(horizontal = 12.dp, vertical = 12.dp)) { + SheetChips(listOf(stringResource(R.string.input_buttons), stringResource(R.string.input_axes)) + + if (preset.physical) listOf(stringResource(R.string.input_extra_keys)) else emptyList(), bindingsTab) { bindingsTab = it } + } + val controls = InputControl.padControls.asList() + if (bindingsTab != 2) InputControlGrid(preset, controls, bindingsTab == 1) { edit = InputBindingEdit(preset.id, it) } + else { + for (b in preset.keys) { + InputNavigationRow(b.key.label, actionLabel(b.action)) { edit = InputBindingEdit(preset.id, InputControl.A, key = b.key) } + TextButton(onClick = { update { it.withPreset(preset.copy(keys = preset.keys - b)) } }) { Text(stringResource(R.string.input_remove_binding)) } + } + InputNavigationRow(stringResource(R.string.input_capture), stringResource(R.string.input_capture_hint)) { capture = true } + } + } + } + SettingsGroup(stringResource(R.string.input_reuse)) { + InputNavigationRow(stringResource(R.string.input_use_settings_from), stringResource(R.string.input_reuse_hint)) { go(InputPage.REUSE) } + if (library.usersOfBindings(preset.id).size > 1 || layout?.let { it.editable && library.usersOfLayout(it.id).size > 1 } == true) + InputNavigationRow(stringResource(R.string.input_make_independent), stringResource(R.string.input_independent_hint), Modifier.testTag("input-make-independent")) { + update { it.usePart(profileId, role, profileId, shared = false) } + } + } + } + InputPage.LAYOUTS -> { + InputHint(stringResource(R.string.input_layout_geometry_hint)) + SettingsGroup(stringResource(R.string.input_saved_layouts)) { + var showOther by remember { mutableStateOf(false) } + val available = library.layouts.filter { it.supports(role) } + val other = available.filter { saved -> !saved.id.startsWith("builtin.") && saved.id != layout?.id && + library.usersOfLayout(saved.id).isEmpty() && library.bindings.none { it.layoutId == saved.id && library.selectedBindings(it.id) } } + for (saved in available.filter { showOther || it !in other }) { + InputLayoutRow(saved, role, if (saved.id == layout?.id) stringResource(R.string.input_current_layout) else null, + onManage = { managingLayout = saved }) { + reuse = InputReuseRequest(saved.name, layoutOnly = true) { shared -> if (update { it.useLayout(profileId, role, saved.id, shared) }) go(InputPage.SOURCE) } + } + } + if (other.isNotEmpty()) TextButton(onClick = { showOther = !showOther }, modifier = Modifier.padding(8.dp)) { + Text(stringResource(if (showOther) R.string.input_hide_other_layouts else R.string.input_other_layouts, other.size)) + } + TextButton(onClick = { naming = InputNameRequest(R.string.input_new_layout, "") { name, _, _ -> + val created = SavedInputLayout(newInputId(), name, screen = if (role == InputRole.PRIMARY) InputLayoutScreen.MAIN else InputLayoutScreen.SECONDARY) + if (update { it.withLayout(created).useLayout(profileId, role, created.id, shared = true) }) { go(InputPage.SOURCE); surface = true } + } }, modifier = Modifier.padding(8.dp)) { Text(stringResource(R.string.input_new_layout)) } + } + + } + InputPage.REUSE -> { + InputHint(stringResource(R.string.input_reuse_explanation, roleLabel(role))) + SettingsGroup(stringResource(R.string.input_profiles)) { + for (p in library.profiles.filterNot { it.id == profileId }) InputNavigationRow(p.name, library.layout(p.id, role)?.name) { + reuse = InputReuseRequest(p.name, layoutOnly = false) { shared -> if (update { it.usePart(profileId, role, p.id, shared) }) go(InputPage.SOURCE) } + } + } + } + + } + } + } + managingLayout?.let { saved -> AlertDialog(onDismissRequest = { managingLayout = null }, title = { Text(saved.name) }, + text = { Column(verticalArrangement = Arrangement.spacedBy(12.dp)) { + Text(stringResource(R.string.input_layout_count, saved.elements.size)) + SecondaryButton(stringResource(R.string.input_rename)) { + managingLayout = null + rename(R.string.input_rename_layout, saved.name) { name -> update { it.withLayout(saved.copy(name = name)) } } + } + val used = library.usersOfLayout(saved.id).isNotEmpty() || library.bindings.any { it.layoutId == saved.id && library.selectedBindings(it.id) } + if (!used && saved.editable) SecondaryButton(stringResource(R.string.input_delete)) { + managingLayout = null + confirmation = InputConfirmation(context.getString(R.string.input_delete), context.getString(R.string.input_delete_layout_detail, saved.name)) { update { it.removeLayout(saved.id) } } + } + } }, confirmButton = { TextButton(onClick = { managingLayout = null }) { Text(stringResource(R.string.common_close)) } }) } + if (capture) InputCaptureDialog(onDismiss = { capture = false }) { capture = false; edit = InputBindingEdit(preset.id, InputControl.A, key = it) } + if (surface) InputSurfaceDialog(profileId, onDismiss = { surface = false; library = InputLibraries.read(context) }, onChanged = { + library = InputLibraries.read(context); onChanged() + }, role = role, layoutId = layout?.id, bindingsId = preset.id) + naming?.let { request -> key(request) { InputNameDialog(request) { naming = null } } } + reuse?.let { request -> key(request) { InputReuseDialog(request) { reuse = null } } } + confirmation?.let { request -> AlertDialog(onDismissRequest = { confirmation = null }, title = { Text(request.title) }, + text = { Text(request.detail) }, confirmButton = { TextButton(onClick = { confirmation = null; request.action() }) { Text(stringResource(R.string.common_ok)) } }, + dismissButton = { TextButton(onClick = { confirmation = null }) { Text(stringResource(R.string.common_cancel)) } }) } + error?.let { message -> AlertDialog(onDismissRequest = { error = null }, title = { Text(stringResource(R.string.input_save_failed)) }, + text = { Text(message) }, confirmButton = { TextButton(onClick = { error = null }) { Text(stringResource(R.string.common_ok)) } }) } +} + +@Composable +private fun InputNameDialog(request: InputNameRequest, onDismiss: () -> Unit) { + var name by remember { mutableStateOf(request.initial) } + var source by remember { mutableStateOf(request.source) } + var shared by remember { mutableStateOf(false) } + val host = rememberMenuHost() + AlertDialog(onDismissRequest = onDismiss, title = { Text(stringResource(request.title)) }, text = { Column(Modifier.verticalScroll(rememberScrollState())) { + OutlinedTextField(name, { name = it.take(80) }, label = { Text(stringResource(R.string.input_name)) }, singleLine = true, modifier = Modifier.testTag("input-name")) + if (request.sources.isNotEmpty()) ChoiceRow(host, "input-copy-source", stringResource(R.string.input_start_from), null, request.sources, source) { source = it } + if (request.offerSharing) { + Row(Modifier.fillMaxWidth().clickable { shared = !shared }, verticalAlignment = Alignment.CenterVertically) { + Checkbox(shared, { shared = it }, Modifier.testTag("input-share-physical")) + Text(stringResource(R.string.input_share_physical)) + } + Text(stringResource(R.string.input_new_independent_hint), style = MaterialTheme.typography.bodySmall) + } + } }, confirmButton = { TextButton(enabled = name.isNotBlank(), onClick = { request.save(name.trim(), source, shared); onDismiss() }) { Text(stringResource(R.string.gesture_save_changes)) } }, + dismissButton = { TextButton(onClick = onDismiss) { Text(stringResource(R.string.common_cancel)) } }) +} + +@Composable +private fun InputReuseDialog(request: InputReuseRequest, onDismiss: () -> Unit) { + var shared by remember { mutableStateOf(false) } + AlertDialog(onDismissRequest = onDismiss, title = { Text(request.name) }, text = { Column(Modifier.verticalScroll(rememberScrollState())) { + Text(stringResource(if (request.layoutOnly) R.string.input_reuse_layout_detail else R.string.input_reuse_part_detail)) + for (link in listOf(false, true)) Row(Modifier.fillMaxWidth().clickable { shared = link }, verticalAlignment = Alignment.CenterVertically) { + RadioButton(shared == link, { shared = link }) + Text(stringResource(if (link) R.string.input_keep_shared else R.string.input_independent_copy)) + } + } }, confirmButton = { TextButton(onClick = { request.apply(shared); onDismiss() }) { Text(stringResource(R.string.common_ok)) } }, + dismissButton = { TextButton(onClick = onDismiss) { Text(stringResource(R.string.common_cancel)) } }) +} + +@Composable +private fun InputControlGrid(preset: SavedInputBindings, available: List, axes: Boolean, onPick: (InputControl) -> Unit) { + val controls = available.filter { (it.dimensions == 2 || it == InputControl.LT || it == InputControl.RT) == axes } + BoxWithConstraints(Modifier.fillMaxWidth().padding(12.dp)) { + val columns = if (maxWidth < 480.dp) 2 else 4 + Column(verticalArrangement = Arrangement.spacedBy(8.dp)) { + for (row in controls.chunked(columns)) Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) { + for (control in row) OutlinedCard(onClick = { onPick(control) }, modifier = Modifier.weight(1f).testTag("input-binding-${control.id}")) { + Column(Modifier.padding(12.dp).heightIn(min = 48.dp)) { + Text(control.label, style = MaterialTheme.typography.titleMedium) + Text(actionLabel(preset.controls[control] ?: InputAction.Native()), style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant) + } + } + repeat(columns - row.size) { Spacer(Modifier.weight(1f)) } + } + } + } +} + +@Composable +private fun InputNavigationRow(title: String, detail: String?, modifier: Modifier = Modifier, leading: (@Composable () -> Unit)? = null, onClick: () -> Unit) { + Row(modifier.fillMaxWidth().clickable(role = Role.Button, onClick = onClick).heightIn(min = 64.dp).padding(14.dp), + verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(12.dp)) { + leading?.invoke() + Column(Modifier.weight(1f)) { + Text(title, style = MaterialTheme.typography.titleMedium) + if (detail != null) Text(detail, style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant) + } + Text("›", style = MaterialTheme.typography.headlineSmall, color = MaterialTheme.colorScheme.onSurfaceVariant) + } +} + +@Composable +internal fun roleLabel(role: InputRole) = stringResource(when (role) { + InputRole.PHYSICAL -> R.string.input_physical; InputRole.PRIMARY -> R.string.input_primary; InputRole.SECONDARY -> R.string.input_secondary +}) + +@Composable +private fun InputHint(text: String) { + Text(text, Modifier.padding(14.dp), style = MaterialTheme.typography.bodyMedium, color = MaterialTheme.colorScheme.onSurfaceVariant) +} diff --git a/app/src/main/java/com/droiddeck/launcher/ui/InputSurfaceDialog.kt b/app/src/main/java/com/droiddeck/launcher/ui/InputSurfaceDialog.kt new file mode 100644 index 0000000000..002e05b6fb --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/ui/InputSurfaceDialog.kt @@ -0,0 +1,330 @@ +package com.droiddeck.launcher.ui + +import android.app.Presentation +import android.hardware.display.DisplayManager +import android.os.Bundle +import android.os.Handler +import android.os.Looper +import android.view.Display +import android.view.View +import android.view.Window +import android.view.WindowManager +import androidx.activity.ComponentActivity +import androidx.activity.compose.BackHandler +import androidx.activity.setViewTreeOnBackPressedDispatcherOwner +import androidx.compose.animation.core.animateDpAsState +import androidx.compose.ui.draw.clipToBounds +import androidx.compose.animation.AnimatedVisibility +import androidx.compose.animation.expandVertically +import androidx.compose.animation.shrinkVertically +import androidx.compose.animation.fadeIn +import androidx.compose.animation.fadeOut +import androidx.compose.foundation.clickable +import androidx.compose.foundation.gestures.detectDragGestures +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.foundation.verticalScroll +import androidx.compose.foundation.background +import androidx.compose.foundation.horizontalScroll +import androidx.compose.foundation.layout.* +import androidx.compose.foundation.rememberScrollState +import androidx.compose.material3.* +import androidx.compose.runtime.* +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.geometry.Offset +import androidx.compose.ui.input.pointer.pointerInput +import androidx.compose.ui.layout.onSizeChanged +import androidx.compose.ui.platform.LocalDensity +import androidx.compose.ui.unit.IntOffset +import androidx.compose.ui.unit.IntSize +import kotlin.math.roundToInt +import androidx.compose.ui.platform.ComposeView +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.platform.LocalView +import androidx.compose.ui.platform.testTag +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.unit.dp +import androidx.compose.ui.viewinterop.AndroidView +import androidx.compose.ui.window.Dialog +import androidx.compose.ui.window.DialogProperties +import androidx.compose.ui.window.DialogWindowProvider +import androidx.lifecycle.Lifecycle +import androidx.lifecycle.LifecycleEventObserver +import androidx.lifecycle.setViewTreeLifecycleOwner +import androidx.lifecycle.setViewTreeViewModelStoreOwner +import androidx.savedstate.setViewTreeSavedStateRegistryOwner +import com.droiddeck.launcher.R +import com.droiddeck.launcher.SessionActivity +import com.droiddeck.launcher.input.* +import com.droiddeck.launcher.session.SessionState + +private class SurfaceDraft(val layout: SavedInputLayout, val preset: SavedInputBindings) { + var elements by mutableStateOf(layout.elements) + var actions by mutableStateOf(preset.elements) + var clicks by mutableStateOf(preset.clicks) + var selected by mutableStateOf(null) + var toolsExpanded by mutableStateOf(false) + var toolsX by mutableFloatStateOf(.02f) + var toolsY by mutableFloatStateOf(.02f) + var snap by mutableStateOf(true) + var adding by mutableStateOf(false) + var properties by mutableStateOf(false) + var clickProperties by mutableStateOf(false) + var history by mutableStateOf, Map, Map>>>(emptyList()) + val element get() = elements.firstOrNull { it.id == selected } + val dirty get() = elements != layout.elements || actions != preset.elements || clicks != preset.clicks + fun checkpoint() { history = (history + Triple(elements, actions, clicks)).takeLast(40) } + fun undo() { history.lastOrNull()?.let { elements = it.first; actions = it.second; clicks = it.third; history = history.dropLast(1) } } + fun action(e: InputSurfaceElement) = preset.copy(elements = actions).action(e) + fun clickAction(e: InputSurfaceElement) = preset.copy(elements = actions, clicks = clicks).clickAction(e) +} + +@Composable +fun InputSurfaceDialog(profile: String, onDismiss: () -> Unit, onChanged: () -> Unit, + role: InputRole = InputRole.SECONDARY, layoutId: String? = null, bindingsId: String? = null) { + val context = LocalContext.current + val host = LocalInputActivity.current ?: context.findActivity() as? ComponentActivity + val library = remember { InputLibraries.read(context) } + val savedLayout = remember { library.layouts.first { it.id == (layoutId ?: library.profile(profile).parts.getValue(role).layout) } } + val savedBindings = remember { library.preset(bindingsId ?: library.profile(profile).binding(role)) } + val layoutUsers = library.usersOfLayout(savedLayout.id).size + val bindingUsers = library.usersOfBindings(savedBindings.id).size + val draft = remember { SurfaceDraft(savedLayout, savedBindings) } + val manager = remember { context.getSystemService(DisplayManager::class.java) } + var revision by remember { mutableIntStateOf(0) } + var discard by remember { mutableStateOf(false) } + var failure by remember { mutableStateOf(null) } + val target = remember(revision, role) { + if (host is SessionActivity) host.inputEditorDisplay(role) + else if (role == InputRole.PRIMARY) host?.window?.decorView?.display + else { + val displays = SecondScreenDisplays.available(manager, host?.window?.decorView?.display?.displayId ?: Display.DEFAULT_DISPLAY) + manager.getDisplay(displays.firstOrNull { it.id == SessionState.secondScreenDisplay }?.id ?: displays.firstOrNull()?.id ?: -1) + } + } + val remote = target != null && target.displayId != host?.window?.decorView?.display?.displayId + fun leave() { if (draft.dirty) discard = true else onDismiss() } + fun save() { + runCatching { + InputLibraries.update(context) { latest -> + val l = latest.layouts.first { it.id == savedLayout.id } + val p = latest.preset(savedBindings.id) + latest.withLayout(l.copy(elements = draft.elements)).withPreset(p.copy(elements = draft.actions.filterKeys { id -> draft.elements.any { it.id == id } }, + clicks = draft.clicks.filterKeys { id -> draft.elements.any { it.id == id && it.hasClick } })) + } + }.onSuccess { onChanged(); onDismiss() }.onFailure { failure = it.message ?: context.getString(R.string.input_save_failed) } + } + DisposableEffect(manager) { + val listener = object : DisplayManager.DisplayListener { + override fun onDisplayAdded(id: Int) { revision++ } + override fun onDisplayRemoved(id: Int) { revision++ } + override fun onDisplayChanged(id: Int) { revision++ } + } + manager.registerDisplayListener(listener, Handler(Looper.getMainLooper())) + onDispose { manager.unregisterDisplayListener(listener) } + } + if (remote && host != null) DisposableEffect(target!!.displayId, host) { + val presentation = SurfaceEditorPresentation(host, target, transparent = host is SessionActivity && role == InputRole.PRIMARY) { + SurfaceCanvas(draft, transparent = host is SessionActivity && role == InputRole.PRIMARY) + } + runCatching { presentation.show() }.onFailure { failure = it.message } + val observer = LifecycleEventObserver { _, event -> + if (event == Lifecycle.Event.ON_STOP) presentation.hide() + if (event == Lifecycle.Event.ON_START) runCatching { presentation.show() } + } + host.lifecycle.addObserver(observer) + onDispose { host.lifecycle.removeObserver(observer); presentation.dismiss() } + } + // Compose 1.6 custom dialog sizing subtracts system bars even when they are hidden. + Dialog(onDismissRequest = ::leave, properties = DialogProperties(usePlatformDefaultWidth = true, decorFitsSystemWindows = false)) { + val editorWindow = (LocalView.current.parent as? DialogWindowProvider)?.window + DisposableEffect(editorWindow) { + editorWindow?.prepareInputEditor() + if (host is SessionActivity && role == InputRole.PRIMARY) editorWindow?.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND) + onDispose { } + } + Surface(Modifier.fillMaxSize(), color = if (host is SessionActivity && role == InputRole.PRIMARY && !draft.properties && !remote) + androidx.compose.ui.graphics.Color.Transparent else MaterialTheme.colorScheme.surface) { + val element = draft.element + BackHandler { if (draft.properties) draft.properties = false else leave() } + when { + draft.properties && element != null -> { + var properties by remember(element.id) { mutableStateOf(element) } + InputActionEditor(if (draft.clickProperties) InputControl.A else element.control, + if (draft.clickProperties) draft.clickAction(element) else draft.action(element), + if (draft.clickProperties) stringResource(R.string.input_click_name, element.name) else element.name, + onBack = { draft.properties = false }, element = element, click = draft.clickProperties, + properties = { + if (!draft.clickProperties) InputElementProperties(properties) { properties = it } + if (layoutUsers > 1) Text(stringResource(R.string.input_editor_shared, layoutUsers), + style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant, + modifier = Modifier.padding(bottom = 12.dp)) + }, detail = if (bindingUsers > 1) stringResource(R.string.input_shared_detail, savedBindings.name, bindingUsers) + else "${library.profile(profile).name} · ${roleLabel(role)}", onSave = { action -> + draft.checkpoint() + if (draft.clickProperties) draft.clicks = draft.clicks + (element.id to action) else draft.actions = draft.actions + (element.id to action) + draft.elements = draft.elements.map { if (it.id == element.id) properties.copy(name = properties.name.trim().ifBlank { element.name }) else it } + draft.properties = false + }) + } + target == null -> SettingsPage(rememberMenuHost(), savedLayout.name, ::leave) { + Text(stringResource(R.string.input_display_unavailable)) + PrimaryButton(stringResource(R.string.input_editor_done)) { save() } + } + remote -> SettingsPage(rememberMenuHost(), stringResource(R.string.input_editor_remote), ::leave, + lede = if (layoutUsers > 1) stringResource(R.string.input_editor_shared, layoutUsers) else savedLayout.name) { + SurfaceToolbar(draft, ::save, ::leave) + SurfaceSelection(draft) + } + else -> Box(Modifier.fillMaxSize()) { + SurfaceCanvas(draft, transparent = host is SessionActivity && role == InputRole.PRIMARY) + FloatingSurfaceTools(draft, ::save, ::leave) + } + } + } + } + if (draft.adding) AlertDialog(onDismissRequest = { draft.adding = false }, title = { Text(stringResource(R.string.input_add_element)) }, confirmButton = {}, text = { Column { + for (kind in InputElementKind.entries) TextButton(onClick = { draft.checkpoint() + val w = if (kind.control.dimensions == 2) .4f else .2f + val h = .3f + val name = context.getString(kind.label()) + " " + (draft.elements.count { it.kind == kind } + 1) + val element = InputSurfaceElement(kind.control, (1f - w) / 2, (1f - h) / 2, w, h, newInputId(), kind, name) + draft.elements = draft.elements + element; draft.selected = element.id + draft.adding = false; draft.toolsExpanded = false }) { Text(stringResource(kind.label())) } + } }) + if (discard) AlertDialog(onDismissRequest = { discard = false }, title = { Text(stringResource(R.string.input_editor_discard)) }, + confirmButton = { TextButton(onClick = onDismiss) { Text(stringResource(R.string.common_ok)) } }, + dismissButton = { TextButton(onClick = { discard = false }) { Text(stringResource(R.string.common_cancel)) } }) + failure?.let { AlertDialog(onDismissRequest = { failure = null }, title = { Text(stringResource(R.string.input_save_failed)) }, text = { Text(it) }, + confirmButton = { TextButton(onClick = { failure = null }) { Text(stringResource(R.string.common_ok)) } }) } +} + +@Composable +private fun SurfaceCanvas(draft: SurfaceDraft, transparent: Boolean = false) { + AndroidView(factory = { InputSurfaceEditorView(it) }, modifier = Modifier.fillMaxSize().testTag("input-layout-canvas"), + update = { view -> + view.setBackgroundColor(if (transparent) android.graphics.Color.TRANSPARENT else android.graphics.Color.rgb(10, 11, 13)) + view.style = draft.layout.style; view.elements = draft.elements; view.selected = draft.selected; view.snap = draft.snap + view.isEnabled = !draft.properties && !draft.adding + view.onSelect = { if (!draft.properties) draft.selected = it } + view.onBeginEdit = draft::checkpoint + view.onChange = { e -> if (!draft.properties) draft.elements = draft.elements.map { if (it.id == e.id) e else it } } + + }) +} + +@Composable +private fun FloatingSurfaceTools(draft: SurfaceDraft, onSave: () -> Unit, onLeave: () -> Unit) { + BoxWithConstraints(Modifier.fillMaxSize()) { + val density = LocalDensity.current + val screenWidth = with(density) { maxWidth.toPx() } + val screenHeight = with(density) { maxHeight.toPx() } + val margin = with(density) { 8.dp.toPx() } + var panelSize by remember { mutableStateOf(IntSize.Zero) } + val x = (draft.toolsX * screenWidth).coerceIn(margin, (screenWidth - panelSize.width - margin).coerceAtLeast(margin)) + val y = (draft.toolsY * screenHeight).coerceIn(margin, (screenHeight - panelSize.height - margin).coerceAtLeast(margin)) + val position by rememberUpdatedState(Offset(x, y)) + val expandedWidth = minOf(420.dp, maxWidth - 16.dp) + val panelWidth by animateDpAsState(if (draft.toolsExpanded) expandedWidth else 152.dp, label = "input-tools-width") + Surface(Modifier.offset { IntOffset(x.roundToInt(), y.roundToInt()) } + .width(panelWidth).clipToBounds() + .heightIn(max = maxHeight - 16.dp).onSizeChanged { panelSize = it }, + shape = RoundedCornerShape(18.dp), color = MaterialTheme.colorScheme.surface, + tonalElevation = 6.dp, shadowElevation = 10.dp) { + Column { + Row(Modifier.fillMaxWidth().heightIn(min = 52.dp).testTag("input-editor-tools") + .pointerInput(screenWidth, screenHeight, panelSize) { + var dragPosition = Offset.Zero + detectDragGestures(onDragStart = { dragPosition = position }) { change, delta -> + change.consume() + dragPosition = Offset((dragPosition.x + delta.x).coerceIn(margin, (screenWidth - panelSize.width - margin).coerceAtLeast(margin)), + (dragPosition.y + delta.y).coerceIn(margin, (screenHeight - panelSize.height - margin).coerceAtLeast(margin))) + draft.toolsX = dragPosition.x / screenWidth + draft.toolsY = dragPosition.y / screenHeight + } + } + .clickable { draft.toolsExpanded = !draft.toolsExpanded }.padding(horizontal = 16.dp), + verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(12.dp)) { + Text("⠿", color = MaterialTheme.colorScheme.primary) + Text(stringResource(if (draft.toolsExpanded) R.string.input_hide_tools else R.string.input_tools), style = MaterialTheme.typography.titleSmall, maxLines = 1) + } + AnimatedVisibility(draft.toolsExpanded, enter = expandVertically(expandFrom = Alignment.Top) + fadeIn(), + exit = shrinkVertically(shrinkTowards = Alignment.Top) + fadeOut()) { + Column(Modifier.requiredWidth(expandedWidth).verticalScroll(rememberScrollState())) { + HorizontalDivider() + SurfaceToolbar(draft, onSave, onLeave) + HorizontalDivider() + SurfaceSelection(draft) + } + } + } + } + } +} + +@OptIn(ExperimentalLayoutApi::class) +@Composable +private fun SurfaceToolbar(draft: SurfaceDraft, onSave: () -> Unit, onLeave: () -> Unit) { + FlowRow(Modifier.fillMaxWidth().padding(8.dp), horizontalArrangement = Arrangement.spacedBy(8.dp), verticalArrangement = Arrangement.spacedBy(4.dp)) { + SecondaryButton(stringResource(R.string.common_cancel), compact = true, onClick = onLeave) + SecondaryButton(stringResource(R.string.input_editor_add), enabled = draft.elements.size < 64, compact = true) { draft.adding = true } + SecondaryButton(stringResource(R.string.input_editor_undo), enabled = draft.history.isNotEmpty(), compact = true) { draft.undo() } + Row(verticalAlignment = Alignment.CenterVertically) { + Checkbox(draft.snap, { draft.snap = it }) + Text(stringResource(R.string.ctrl_editor_snap)) + } + PrimaryButton(stringResource(R.string.input_editor_done), compact = true, onClick = onSave) + } +} + +@OptIn(ExperimentalLayoutApi::class) +@Composable +private fun SurfaceSelection(draft: SurfaceDraft) { + Column(Modifier.padding(12.dp), verticalArrangement = Arrangement.spacedBy(8.dp)) { + draft.element?.let { e -> + Text(e.name, style = MaterialTheme.typography.titleMedium) + FlowRow(horizontalArrangement = Arrangement.spacedBy(8.dp), verticalArrangement = Arrangement.spacedBy(4.dp)) { + PrimaryButton(stringResource(R.string.input_properties), compact = true) { draft.clickProperties = false; draft.properties = true } + if (e.hasClick) SecondaryButton(stringResource(R.string.input_click_properties), compact = true) { draft.clickProperties = true; draft.properties = true } + SecondaryButton(stringResource(R.string.input_remove_element), compact = true) { + draft.checkpoint(); draft.elements = draft.elements.filterNot { it.id == e.id }; draft.actions = draft.actions - e.id; draft.clicks = draft.clicks - e.id; draft.selected = null + } + } + } ?: Text(stringResource(R.string.input_editor_hint)) + } +} + +private fun InputElementKind.label() = when (this) { + InputElementKind.BUTTON -> R.string.input_element_button + InputElementKind.STICK -> R.string.input_element_stick + InputElementKind.TRACKPAD -> R.string.input_element_trackpad + InputElementKind.DPAD -> R.string.input_element_dpad + InputElementKind.TRIGGER -> R.string.input_element_trigger +} + +private class SurfaceEditorPresentation(private val host: ComponentActivity, display: Display, private val transparent: Boolean, private val content: @Composable () -> Unit) : Presentation(host, display) { + private var view: ComposeView? = null + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setCancelable(false) + window?.addFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) + window?.prepareInputEditor() + window?.decorView?.let { + it.setViewTreeLifecycleOwner(host); it.setViewTreeViewModelStoreOwner(host) + it.setViewTreeSavedStateRegistryOwner(host); it.setViewTreeOnBackPressedDispatcherOwner(host) + } + window?.setBackgroundDrawable(android.graphics.drawable.ColorDrawable(android.graphics.Color.TRANSPARENT)) + view = ComposeView(context).also { it.setContent { DroidDeckTheme { + Surface(Modifier.fillMaxSize(), color = if (transparent) androidx.compose.ui.graphics.Color.Transparent else MaterialTheme.colorScheme.surface) { content() } + } }; setContentView(it) } + } + override fun dismiss() { view?.disposeComposition(); view = null; super.dismiss() } +} + +@Suppress("DEPRECATION") +internal fun Window.prepareInputEditor() { + addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON or WindowManager.LayoutParams.FLAG_FULLSCREEN) + setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT) + 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 +} diff --git a/app/src/main/java/com/droiddeck/launcher/ui/InputSurfaceEditorView.kt b/app/src/main/java/com/droiddeck/launcher/ui/InputSurfaceEditorView.kt new file mode 100644 index 0000000000..d03eaa6e9f --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/ui/InputSurfaceEditorView.kt @@ -0,0 +1,107 @@ +package com.droiddeck.launcher.ui + +import android.annotation.SuppressLint +import android.content.Context +import android.graphics.Canvas +import android.graphics.Color +import android.graphics.Paint +import android.view.HapticFeedbackConstants +import android.view.MotionEvent +import android.view.ViewConfiguration +import android.widget.FrameLayout +import com.droiddeck.launcher.input.InputSurfaceElement +import com.droiddeck.launcher.input.InputSurfaceStyle +import com.droiddeck.launcher.input.snapSurfaceElement +import kotlin.math.abs + +internal class InputSurfaceEditorView(context: Context) : FrameLayout(context) { + var elements: List = emptyList() + set(value) { field = value; preview.updateCustomLayout(value); invalidate() } + var style = InputSurfaceStyle() + set(value) { field = value; preview.updateStyle(value) } + var snap = true + set(value) { field = value; if (!value) clearGuides() } + private var guideX: Float? = null + private var guideY: Float? = null + var selected: String? = null + set(value) { field = value; invalidate() } + var onSelect: (String?) -> Unit = {} + var onBeginEdit: () -> Unit = {} + var onChange: (InputSurfaceElement) -> Unit = {} + private val preview = InputSurfaceView(context) + private val paint = Paint(Paint.ANTI_ALIAS_FLAG).apply { color = Color.rgb(26, 159, 255); strokeWidth = 2 * resources.displayMetrics.density } + private val radius = 14 * resources.displayMetrics.density + private var original: InputSurfaceElement? = null + private var downX = 0f + private var downY = 0f + private var corner = 0 + private var dragging = false + + init { + setBackgroundColor(Color.rgb(10, 11, 13)) + addView(preview, LayoutParams(-1, -1)) + } + + override fun dispatchDraw(canvas: Canvas) { + super.dispatchDraw(canvas) + paint.style = Paint.Style.STROKE + guideX?.let { canvas.drawLine(width * it, 0f, width * it, height.toFloat(), paint) } + guideY?.let { canvas.drawLine(0f, height * it, width.toFloat(), height * it, paint) } + val e = elements.firstOrNull { it.id == selected } ?: return + val l = width * e.x; val t = height * e.y; val r = l + width * e.width; val b = t + height * e.height + paint.style = Paint.Style.STROKE; canvas.drawRect(l, t, r, b, paint) + paint.style = Paint.Style.FILL + for (x in listOf(l, r)) for (y in listOf(t, b)) canvas.drawCircle(x, y, radius / 3, paint) + } + + private fun clearGuides() { guideX = null; guideY = null; invalidate() } + + override fun onInterceptTouchEvent(event: MotionEvent) = true + + @SuppressLint("ClickableViewAccessibility") + override fun onTouchEvent(event: MotionEvent): Boolean { + if (!isEnabled || width == 0 || height == 0) return true + when (event.actionMasked) { + MotionEvent.ACTION_DOWN -> { + clearGuides() + downX = event.x; downY = event.y; dragging = false; corner = 0 + val current = elements.firstOrNull { it.id == selected } + if (current != null) { + val left = width * current.x; val top = height * current.y + val right = left + width * current.width; val bottom = top + height * current.height + val horizontal = if (abs(downX - left) < radius) 1 else if (abs(downX - right) < radius) 2 else 0 + val vertical = if (abs(downY - top) < radius) 4 else if (abs(downY - bottom) < radius) 8 else 0 + if (horizontal != 0 && vertical != 0) corner = horizontal or vertical + } + original = if (corner != 0) current else elements.asReversed().firstOrNull { + downX / width in it.x..(it.x + it.width) && downY / height in it.y..(it.y + it.height) + } + selected = original?.id; onSelect(selected) + } + MotionEvent.ACTION_MOVE -> { + val e = original ?: return true + if (!dragging && abs(event.x - downX) + abs(event.y - downY) > ViewConfiguration.get(context).scaledTouchSlop) { + dragging = true; onBeginEdit() + } + if (!dragging) return true + val dx = (event.x - downX) / width; val dy = (event.y - downY) / height + val next = if (corner == 0) e.copy(x = (e.x + dx).coerceIn(0f, 1f - e.width), y = (e.y + dy).coerceIn(0f, 1f - e.height)) + else { + val left = if (corner and 1 != 0) (e.x + dx).coerceIn(0f, e.x + e.width - .03f) else e.x + val top = if (corner and 4 != 0) (e.y + dy).coerceIn(0f, e.y + e.height - .03f) else e.y + val right = if (corner and 2 != 0) (e.x + e.width + dx).coerceIn(left + .03f, 1f) else e.x + e.width + val bottom = if (corner and 8 != 0) (e.y + e.height + dy).coerceIn(top + .03f, 1f) else e.y + e.height + e.copy(x = left, y = top, width = right - left, height = bottom - top).bounded() + } + val aligned = if (snap) snapSurfaceElement(next, elements.filter { it.id != e.id }, 9 * resources.displayMetrics.density / width, + 9 * resources.displayMetrics.density / height, corner) else null + if (aligned != null && (aligned.x != null && aligned.x != guideX || aligned.y != null && aligned.y != guideY)) performHapticFeedback(HapticFeedbackConstants.CLOCK_TICK) + guideX = aligned?.x; guideY = aligned?.y + onChange(aligned?.element ?: next) + invalidate() + } + MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> { original = null; dragging = false; clearGuides() } + } + return true + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/ui/InputSurfaceView.kt b/app/src/main/java/com/droiddeck/launcher/ui/InputSurfaceView.kt new file mode 100644 index 0000000000..c6ff3fc901 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/ui/InputSurfaceView.kt @@ -0,0 +1,546 @@ +package com.droiddeck.launcher.ui + +import com.droiddeck.launcher.R + +import android.annotation.SuppressLint +import android.content.Context +import android.graphics.BitmapFactory +import android.graphics.Canvas +import android.graphics.Color +import android.graphics.Paint +import android.graphics.RadialGradient +import android.graphics.RectF +import android.graphics.Shader +import android.graphics.Typeface +import android.graphics.drawable.GradientDrawable +import android.view.Gravity +import android.view.HapticFeedbackConstants +import android.view.ViewConfiguration +import android.view.MotionEvent +import com.droiddeck.launcher.input.TouchpadGestures +import com.droiddeck.launcher.input.PointerGestures +import android.view.View +import android.view.ViewGroup +import android.widget.FrameLayout +import android.widget.HorizontalScrollView +import android.widget.LinearLayout +import android.widget.TextView +import com.droiddeck.launcher.input.InputRole +import com.droiddeck.launcher.input.InputControl +import com.droiddeck.launcher.input.InputSurfaceElement +import com.droiddeck.launcher.input.InputSurfaceStyle +import com.droiddeck.launcher.input.pointerSubset +import com.droiddeck.launcher.input.InputSurfaceSource +import com.droiddeck.launcher.input.InputElementKind +import com.droiddeck.launcher.input.InputButtonIcons +import com.droiddeck.launcher.input.InputButtonShape +import java.io.File +import com.droiddeck.launcher.input.PadBridge +import com.droiddeck.launcher.session.SessionPrefs +import kotlin.math.atan2 +import kotlin.math.hypot +import kotlin.math.min + +@SuppressLint("ViewConstructor") +class InputSurfaceView( + context: Context, + private val pad: PadBridge? = null, + private var customLayout: List = emptyList(), + private val inputRole: InputRole = InputRole.SECONDARY, + private var surfaceStyle: InputSurfaceStyle = InputSurfaceStyle(), +) : FrameLayout(context) { + private val content = FrameLayout(context) + private val elementSources = ArrayList() + private var customCanvas: ViewGroup? = null + init { addView(content, LayoutParams(-1, -1)); renderCustom(pad) } + + var onTouchMiss: ((MotionEvent) -> Unit)? = null + private var forwardedPointers = 0 + + override fun dispatchTouchEvent(event: MotionEvent): Boolean { + if (event.actionMasked == MotionEvent.ACTION_DOWN) forwardedPointers = 0 + if (onTouchMiss == null) return super.dispatchTouchEvent(event) + if (event.actionMasked == MotionEvent.ACTION_DOWN || event.actionMasked == MotionEvent.ACTION_POINTER_DOWN) { + val index = event.actionIndex + if (!takesTouchAt(event.getX(index), event.getY(index))) forwardedPointers = forwardedPointers or (1 shl event.getPointerId(index)) + else if (SessionPrefs.lowLatency(context)) requestUnbufferedDispatch(event) + } + if (forwardedPointers == 0) return super.dispatchTouchEvent(event) + var all = 0 + for (index in 0 until event.pointerCount) all = all or (1 shl event.getPointerId(index)) + event.pointerSubset(all and forwardedPointers.inv())?.let { split -> + try { super.dispatchTouchEvent(split) } finally { split.recycle() } + } + event.pointerSubset(all and forwardedPointers)?.let { split -> + try { onTouchMiss?.invoke(split) } finally { split.recycle() } + } + if (event.actionMasked == MotionEvent.ACTION_POINTER_UP) forwardedPointers = forwardedPointers and (1 shl event.getPointerId(event.actionIndex)).inv() + if (event.actionMasked == MotionEvent.ACTION_UP || event.actionMasked == MotionEvent.ACTION_CANCEL) forwardedPointers = 0 + return true + } + + fun updateStyle(style: InputSurfaceStyle) { + if (surfaceStyle == style) return + surfaceStyle = style + customCanvas?.let { canvas -> for (i in 0 until canvas.childCount) styleElement(canvas.getChildAt(i)) } + } + + private fun styleElement(view: View) { + view.alpha = surfaceStyle.opacity / 100f * ((view.tag as? InputSurfaceElement)?.opacity ?: 1f) + (view as? Surface)?.tint = surfaceStyle.tint + view.invalidate() + } + + fun updateCustomLayout(layout: List) { + val sameElements = customLayout.map { it.copy(x = 0f, y = 0f, width = 1f, height = 1f) } == layout.map { it.copy(x = 0f, y = 0f, width = 1f, height = 1f) } + customLayout = layout + if (sameElements) customCanvas?.requestLayout() else { + elementSources.forEach { it.close() }; elementSources.clear() + content.removeAllViews(); renderCustom(pad) + } + } + + fun releaseInputs() { + val now = android.os.SystemClock.uptimeMillis() + val cancel = MotionEvent.obtain(now, now, MotionEvent.ACTION_CANCEL, 0f, 0f, 0) + try { super.dispatchTouchEvent(cancel) } finally { cancel.recycle() } + forwardedPointers = 0 + elementSources.forEach { it.release() } + } + + private fun renderCustom(pad: PadBridge?) { + val canvas = object : ViewGroup(context) { + override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { + val layout = customLayout + val w = MeasureSpec.getSize(widthMeasureSpec) + val h = MeasureSpec.getSize(heightMeasureSpec) + setMeasuredDimension(w, h) + for (i in layout.indices) getChildAt(i).measure(MeasureSpec.makeMeasureSpec((w * layout[i].width).toInt(), MeasureSpec.EXACTLY), + MeasureSpec.makeMeasureSpec((h * layout[i].height).toInt(), MeasureSpec.EXACTLY)) + } + override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) { + val layout = customLayout + for (i in layout.indices) { + val e = layout[i] + val child = getChildAt(i) + val x = (width * e.x).toInt(); val y = (height * e.y).toInt() + child.layout(x, y, x + child.measuredWidth, y + child.measuredHeight) + } + } + } + for (element in customLayout) { + val source = pad?.let { InputSurfaceSource(it, inputRole, element).also { s -> elementSources += s } } + canvas.addView(when (element.kind) { + InputElementKind.STICK -> StickView(context, source, element.floating, element.hideWhenIdle && pad != null) + InputElementKind.TRACKPAD -> TrackpadView(context, element.control == InputControl.RIGHT_PAD, source = source) + InputElementKind.DPAD -> DirectionPad(context, source) + else -> HoldButton(context, element, source) + }.apply { tag = element; contentDescription = element.name; styleElement(this); source?.onStateChanged = { postInvalidate() } }) + } + content.addView(canvas, FrameLayout.LayoutParams(-1, -1)) + customCanvas = canvas + } + + override fun onAttachedToWindow() { + super.onAttachedToWindow() + if (pad != null && elementSources.isEmpty()) { + content.removeAllViews() + renderCustom(pad) + } + } + + override fun onDetachedFromWindow() { + elementSources.forEach { it.close() } + elementSources.clear() + super.onDetachedFromWindow() + } + + fun takesTouchAt(x: Float, y: Float): Boolean = controlUnder(this, x, y) + + private fun controlUnder(view: View, x: Float, y: Float): Boolean { + if (view.visibility != VISIBLE || x < 0f || y < 0f || x >= view.width || y >= view.height) return false + if (view.isClickable || view is HorizontalScrollView || view is Surface || view is DirectionPad || + view is androidx.compose.ui.platform.ComposeView) return true + val group = view as? ViewGroup ?: return false + return (group.childCount - 1 downTo 0).any { i -> + val child = group.getChildAt(i) + controlUnder(child, x + group.scrollX - child.x, y + group.scrollY - child.y) + } + } + + private abstract class Surface(context: Context) : View(context) { + var tint: Int? = null + protected val density = resources.displayMetrics.density + protected val rect = RectF() + private val fill = Paint(Paint.ANTI_ALIAS_FLAG) + private val stroke = Paint(Paint.ANTI_ALIAS_FLAG).apply { style = Paint.Style.STROKE } + protected val labelPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply { + textAlign = Paint.Align.CENTER + typeface = Typeface.create(Typeface.DEFAULT, Typeface.BOLD) + } + protected val captionPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply { + textAlign = Paint.Align.CENTER + color = TEXT_DIM + } + + protected fun drawSurface(canvas: Canvas, lit: Boolean, shape: InputButtonShape = InputButtonShape.ROUNDED) { + val inset = density + rect.set(inset, inset, width - inset, height - inset) + val radius = when (shape) { InputButtonShape.CIRCLE, InputButtonShape.PILL -> min(width, height) / 2f; InputButtonShape.RECTANGLE -> 2 * density; else -> CORNER * density } + fill.color = tint?.let { color -> (color and 0x00ffffff) or ((if (lit) 140 else 48) shl 24) } ?: if (lit) ACCENT_WASH else SURFACE + canvas.drawRoundRect(rect, radius, radius, fill) + stroke.color = tint?.let { color -> (color and 0x00ffffff) or ((if (lit) 255 else 160) shl 24) } ?: if (lit) ACCENT else LINE + stroke.strokeWidth = (if (lit) 2f else 1f) * density + canvas.drawRoundRect(rect, radius, radius, stroke) + } + } + + 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 } + BitmapFactory.decodeFile(file.path, size) + val options = BitmapFactory.Options().apply { + while (size.outWidth / inSampleSize.coerceAtLeast(1) > 512 || size.outHeight / inSampleSize.coerceAtLeast(1) > 512) + inSampleSize = inSampleSize.coerceAtLeast(1) * 2 + } + if (file.isFile) BitmapFactory.decodeFile(file.path, options) else null + } + private val imageRect = RectF() + + override fun onDraw(canvas: Canvas) { + val lit = if (element.toggle) source?.latched == true else down + drawSurface(canvas, lit, element.buttonShape) + val cx = width / 2f + labelPaint.color = if (lit) ACCENT else TEXT + bitmap?.let { + val scale = min(width * .84f / it.width, height * .84f / it.height) + imageRect.set(cx - it.width * scale / 2, (height - it.height * scale) / 2, + cx + it.width * scale / 2, (height + it.height * scale) / 2) + canvas.drawBitmap(it, null, imageRect, 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) + if (labelWidth > width * .84f) labelPaint.textSize *= width * .84f / labelWidth + canvas.drawText(element.name, cx, height / 2f + labelPaint.textSize * .35f, labelPaint) + } + + @SuppressLint("ClickableViewAccessibility") + override fun onTouchEvent(event: MotionEvent): Boolean { + when (event.actionMasked) { + MotionEvent.ACTION_DOWN -> set(true) + MotionEvent.ACTION_UP -> set(false) + MotionEvent.ACTION_CANCEL -> { set(false); source?.release() } + } + return true + } + + override fun onDetachedFromWindow() { set(false); source?.release(); super.onDetachedFromWindow() } + + private fun set(pressed: Boolean) { + if (down == pressed) return + down = pressed + if (pressed) performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY) + source?.update(if (pressed) 1f else 0f) + invalidate() + } + } + + private inner class TrackpadView(context: Context, private val right: Boolean, private val captioned: Boolean = true, + private val source: InputSurfaceSource? = null) : Surface(context) { + private var gestureX = 0f + private var gestureY = 0f + private val mouse = TouchpadGestures(ViewConfiguration.get(context).scaledTouchSlop.toFloat(), object : PointerGestures.Listener { + override fun onMove(x: Float, y: Float) { + source?.pointerDelta(2 * (x - gestureX) / width.coerceAtLeast(1), 2 * (y - gestureY) / height.coerceAtLeast(1)) + gestureX = x; gestureY = y + } + override fun onButton(button: Int, pressed: Boolean, x: Float, y: Float) { source?.gestureButton(button, pressed) } + override fun onWheel(steps: Int) { source?.gestureWheel(steps) } + override fun onLongPress() { performHapticFeedback(HapticFeedbackConstants.LONG_PRESS) } + }).apply { bounds = RectF(-1e9f, -1e9f, 1e9f, 1e9f) } + + private var touching = false + private var clicked = false + private var fingerX = 0f + private var fingerY = 0f + private val guide = Paint(Paint.ANTI_ALIAS_FLAG).apply { style = Paint.Style.STROKE; color = GUIDE } + private val dot = Paint(Paint.ANTI_ALIAS_FLAG).apply { color = ACCENT } + private val glow = Paint(Paint.ANTI_ALIAS_FLAG) + + override fun onDraw(canvas: Canvas) { + drawSurface(canvas, clicked) + val cx = width / 2f + val cy = height / 2f + guide.strokeWidth = density + val reach = min(width, height) / 2f - 24 * density + canvas.drawCircle(cx, cy, reach, guide) + canvas.drawCircle(cx, cy, reach / 2f, guide) + canvas.drawLine(cx - reach, cy, cx + reach, cy, guide) + canvas.drawLine(cx, cy - reach, cx, cy + reach, guide) + captionPaint.textSize = 12 * density + captionPaint.color = if (touching) ACCENT else TEXT_DIM + if (captioned) canvas.drawText((contentDescription?.toString() ?: if (right) context.getString(R.string.deck_right_trackpad) else context.getString(R.string.deck_left_trackpad)).uppercase(), cx, 28 * density, captionPaint) + if (touching) { + val radius = 56 * density + glow.shader = RadialGradient(fingerX, fingerY, radius, ACCENT_GLOW, Color.TRANSPARENT, Shader.TileMode.CLAMP) + canvas.drawCircle(fingerX, fingerY, radius, glow) + canvas.drawCircle(fingerX, fingerY, (if (clicked) 16 else 12) * density, dot) + } + } + + @SuppressLint("ClickableViewAccessibility") + override fun onTouchEvent(event: MotionEvent): Boolean { + if (source?.mouseGestures == true) { + if (event.actionMasked == MotionEvent.ACTION_DOWN) { + gestureX = 0f; gestureY = 0f; mouse.place(0f, 0f) + source.gestureContact(true) + } + touching = event.actionMasked != MotionEvent.ACTION_UP && event.actionMasked != MotionEvent.ACTION_CANCEL + fingerX = event.x; fingerY = event.y + mouse.onTouch(event) + if (!touching) source.gestureContact(false) + invalidate() + return true + } + when (event.actionMasked) { + MotionEvent.ACTION_DOWN, MotionEvent.ACTION_MOVE -> { + touch(event.getX(0), event.getY(0)) + click(event.pointerCount > 1) + } + MotionEvent.ACTION_POINTER_DOWN -> click(true) + MotionEvent.ACTION_POINTER_UP -> { + val stays = if (event.actionIndex == 0) 1 else 0 + touch(event.getX(stays), event.getY(stays)) + click(event.pointerCount - 1 > 1) + } + MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> { + click(false) + touching = false + source?.update(0f, 0f, false) + invalidate() + } + } + return true + } + + override fun onDetachedFromWindow() { mouse.cancel(); source?.release(); super.onDetachedFromWindow() } + + private fun touch(x: Float, y: Float) { + fingerX = x.coerceIn(0f, width.toFloat()) + fingerY = y.coerceIn(0f, height.toFloat()) + touching = true + val xAxis = (fingerX - width / 2f) / (width / 2f).coerceAtLeast(1f) + val yAxis = -(fingerY - height / 2f) / (height / 2f).coerceAtLeast(1f) + source?.update(xAxis, yAxis, true) + invalidate() + } + + private fun click(down: Boolean) { + if (clicked == down) return + clicked = down + if (down) performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY) + source?.click(down) + invalidate() + } + } + + private inner class StickView(context: Context, private val source: InputSurfaceSource? = null, private val floating: Boolean = true, private val hideWhenIdle: Boolean = false) : Surface(context) { + private var touching = false + private var clicking = false + private var lastUp = 0L + private var downAt = 0L + private var downX = 0f + private var downY = 0f + private var travel = 0f + private val tapSlop = ViewConfiguration.get(context).scaledTouchSlop.toFloat() + private var originX = 0f + private var originY = 0f + private var knobX = 0f + private var knobY = 0f + private val ring = Paint(Paint.ANTI_ALIAS_FLAG).apply { style = Paint.Style.STROKE } + private val knob = Paint(Paint.ANTI_ALIAS_FLAG) + + private fun reach() = min(width, height) * 0.30f + + override fun onDraw(canvas: Canvas) { + if (hideWhenIdle && !touching) return + drawSurface(canvas, clicking) + val cx = if (touching) originX else width / 2f + val cy = if (touching) originY else height / 2f + ring.strokeWidth = 2 * density + ring.color = if (touching) ACCENT else LINE + canvas.drawCircle(cx, cy, reach(), ring) + knob.color = if (touching) ACCENT else KNOB + canvas.drawCircle(cx + knobX, cy + knobY, reach() * 0.45f, knob) + } + + @SuppressLint("ClickableViewAccessibility") + override fun onTouchEvent(event: MotionEvent): Boolean { + val r = reach().coerceAtLeast(1f) + when (event.actionMasked) { + MotionEvent.ACTION_DOWN -> { + downAt = event.eventTime + downX = event.x + downY = event.y + travel = 0f + originX = if (floating) event.x else width / 2f + originY = if (floating) event.y else height / 2f + touching = true + performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY) + if (lastUp != 0L && event.eventTime - lastUp < CLICK_MS) setClick(true) + follow(event.x, event.y, r, force = true) + } + MotionEvent.ACTION_MOVE -> follow(event.getX(0), event.getY(0), r) + MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> { + touching = false + val cancelled = event.actionMasked == MotionEvent.ACTION_CANCEL || event.flags and MotionEvent.FLAG_CANCELED != 0 + travel = maxOf(travel, hypot(event.x - downX, event.y - downY)) + lastUp = if (!clicking && !cancelled && event.eventTime - downAt <= STICK_TAP_MS && travel <= tapSlop) event.eventTime else 0L + setClick(false) + move(0f, 0f, 1f) + } + } + return true + } + + private fun follow(x: Float, y: Float, r: Float, force: Boolean = false) { + travel = maxOf(travel, hypot(x - downX, y - downY)) + var dx = x - originX + var dy = y - originY + val length = hypot(dx, dy) + if (length > r) { dx *= r / length; dy *= r / length } + if (force || dx != knobX || dy != knobY) move(dx, dy, r) + } + + private fun move(dx: Float, dy: Float, r: Float) { + knobX = dx + knobY = dy + source?.update(dx / r, dy / r) + invalidate() + } + + private fun setClick(down: Boolean) { + if (clicking == down) return + clicking = down + if (down) performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY) + source?.click(down) + } + + override fun onDetachedFromWindow() { + setClick(false) + if (knobX != 0f || knobY != 0f) move(0f, 0f, 1f) + super.onDetachedFromWindow() + } + } + + private inner class DirectionPad(context: Context, private val source: InputSurfaceSource? = null) : View(context) { + private val density = resources.displayMetrics.density + private var held = 0 + private val arm = Paint(Paint.ANTI_ALIAS_FLAG) + private val rect = RectF() + + override fun onDraw(canvas: Canvas) { + val size = min(width, height) * 0.86f + val cx = width / 2f + val cy = height / 2f + val half = size / 2f + val thick = size / 3f / 2f + fun armRect(bit: Int, l: Float, t: Float, r: Float, b: Float) { + arm.color = if (held and bit != 0) ACCENT else KNOB + rect.set(l, t, r, b) + canvas.drawRoundRect(rect, 8 * density, 8 * density, arm) + } + armRect(1, cx - thick, cy - half, cx + thick, cy - thick) + armRect(4, cx - thick, cy + thick, cx + thick, cy + half) + armRect(8, cx - half, cy - thick, cx - thick, cy + thick) + armRect(2, cx + thick, cy - thick, cx + half, cy + thick) + arm.color = KNOB + canvas.drawRect(cx - thick, cy - thick, cx + thick, cy + thick, arm) + } + + @SuppressLint("ClickableViewAccessibility") + override fun onTouchEvent(event: MotionEvent): Boolean { + val bits = when (event.actionMasked) { + MotionEvent.ACTION_DOWN, MotionEvent.ACTION_MOVE -> { + val dx = event.getX(0) - width / 2f + val dy = event.getY(0) - height / 2f + if (hypot(dx, dy) < min(width, height) * 0.08f) 0 else { + val sector = ((Math.toDegrees(atan2(dy.toDouble(), dx.toDouble())) + 360 + 22.5) % 360 / 45).toInt() + intArrayOf(2, 2 or 4, 4, 4 or 8, 8, 8 or 1, 1, 1 or 2)[sector] + } + } + else -> 0 + } + set(bits) + return true + } + + private fun set(bits: Int) { + if (bits == held) return + if (bits and held.inv() != 0) performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY) + held = bits + source?.update((if (bits and 2 != 0) 1f else 0f) - (if (bits and 8 != 0) 1f else 0f), + (if (bits and 4 != 0) 1f else 0f) - (if (bits and 1 != 0) 1f else 0f)) + + invalidate() + } + + override fun onDetachedFromWindow() { + set(0) + super.onDetachedFromWindow() + } + } + + companion object { + private const val CLICK_MS = 300L + private const val STICK_TAP_MS = 200L + + + private const val CORNER = 20f + private const val CORNER_PILL = 22f + private val ACCENT = Color.rgb(0x1A, 0x9F, 0xFF) + private val ACCENT_WASH = Color.argb(0x2E, 0x1A, 0x9F, 0xFF) + private val ACCENT_GLOW = Color.argb(0x66, 0x1A, 0x9F, 0xFF) + private val ON_ACCENT = Color.rgb(0x03, 0x11, 0x1F) + private val SURFACE = Color.rgb(0x0A, 0x0B, 0x0D) + private val LINE = Color.rgb(0x26, 0x29, 0x2E) + private val GUIDE = Color.rgb(0x16, 0x18, 0x1B) + private val TEXT = Color.rgb(0xF2, 0xF4, 0xF7) + private val TEXT_DIM = Color.rgb(0x7A, 0x80, 0x8A) + private val KNOB = Color.rgb(0x2A, 0x2D, 0x33) + + internal fun dp(context: Context, value: Int): Int = (value * context.resources.displayMetrics.density + 0.5f).toInt() + + internal fun outline(context: Context, radius: Float, fill: Int) = GradientDrawable().apply { + cornerRadius = radius * context.resources.displayMetrics.density + setColor(fill) + setStroke(dp(context, 1), LINE) + } + + internal fun roundButton(context: Context, label: String, description: String, onClick: () -> Unit, wide: Boolean = false) = + TextView(context).apply { + text = label + contentDescription = description + textSize = if (wide) 13f else 20f + typeface = Typeface.create(Typeface.DEFAULT, Typeface.BOLD) + letterSpacing = if (wide) 0.08f else 0f + gravity = Gravity.CENTER + setTextColor(TEXT) + background = outline(context, CORNER_PILL, SURFACE) + setPadding(if (wide) dp(context, 16) else 0, 0, if (wide) dp(context, 16) else 0, 0) + minWidth = dp(context, 44) + layoutParams = LinearLayout.LayoutParams(if (wide) LinearLayout.LayoutParams.WRAP_CONTENT else dp(context, 44), dp(context, 44)) + setOnClickListener { + performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY) + onClick() + } + } + + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/ui/ScreenKeyPicker.kt b/app/src/main/java/com/droiddeck/launcher/ui/KeyCombinationPicker.kt similarity index 95% rename from app/src/main/java/com/droiddeck/launcher/ui/ScreenKeyPicker.kt rename to app/src/main/java/com/droiddeck/launcher/ui/KeyCombinationPicker.kt index e70249eef2..3a06fbb3bc 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/ScreenKeyPicker.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/KeyCombinationPicker.kt @@ -52,13 +52,16 @@ import com.droiddeck.launcher.input.PcKey import com.droiddeck.launcher.input.pcKeyLabel @Composable -fun ScreenKeyPicker( +fun KeyCombinationPicker( onDismiss: () -> Unit, onAdd: (List) -> Unit, initial: List = emptyList(), sequence: Boolean = false, sequenceLimit: Int = 4, + allowEmpty: Boolean = false, slots: List = emptyList(), + title: String = stringResource(R.string.input_keys), + confirm: String = stringResource(R.string.gesture_save_changes), ) { var picked by remember { mutableStateOf(initial) } val p = LocalPalette.current @@ -86,7 +89,7 @@ fun ScreenKeyPicker( ) { Column { Row(Modifier.fillMaxWidth().padding(start = 16.dp, end = 6.dp), verticalAlignment = Alignment.CenterVertically) { - Text(stringResource(R.string.screen_keys_add), Modifier.weight(1f), style = MaterialTheme.typography.titleMedium, fontWeight = FontWeight.SemiBold) + Text(title, Modifier.weight(1f), style = MaterialTheme.typography.titleMedium, fontWeight = FontWeight.SemiBold) IconButton(onClick = onDismiss, modifier = Modifier.focusGlide(CircleShape)) { Icon(Icons.Default.Close, null) } } Row( @@ -136,10 +139,10 @@ fun ScreenKeyPicker( Row(Modifier.fillMaxWidth().padding(horizontal = 16.dp, vertical = 8.dp), horizontalArrangement = Arrangement.spacedBy(8.dp)) { Button( onClick = { onAdd(picked) }, - enabled = picked.isNotEmpty(), + enabled = allowEmpty || picked.isNotEmpty(), modifier = Modifier.weight(1f).focusGlide(CircleShape), ) { - Text(stringResource(R.string.screen_keys_add)) + Text(confirm) } } } diff --git a/app/src/main/java/com/droiddeck/launcher/ui/ModeSettingsDialog.kt b/app/src/main/java/com/droiddeck/launcher/ui/ModeSettingsDialog.kt index dfaf8b6f71..8489f5cceb 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/ModeSettingsDialog.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/ModeSettingsDialog.kt @@ -120,6 +120,7 @@ class ModeSettingsActions( val onSteamDownloadsInBackground: (Boolean) -> Unit = {}, val onPipAutoEnter: (Boolean) -> Unit = {}, val onOsc: (String) -> Unit, + val onInputSettings: () -> Unit, val onBackHold: (Boolean) -> Unit = {}, val onBackActionsInverted: (Boolean) -> Unit = {}, val onDirectAudio: (Boolean) -> Unit, @@ -259,6 +260,7 @@ fun ModeSheetRows(s: ModeSettings, a: ModeSettingsActions, tab: String, extras: extras.drivers(s.gpuDrivers) } if (tab == SheetTab.CONTROLS) { + InputQuickSettings(host, desktop = s.mode == SessionService.MODE_DESKTOP, onEdit = a.onInputSettings) SettingsGroup(if (steam) stringResource(R.string.mode_touch_controls) else stringResource(R.string.mode_touch)) { ChoiceRow( host, "touch", stringResource(R.string.mode_touch), null, diff --git a/app/src/main/java/com/droiddeck/launcher/ui/PhysicalShortcutsDialog.kt b/app/src/main/java/com/droiddeck/launcher/ui/PhysicalShortcutsDialog.kt index 7331d49fb0..243b1a0bc8 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/PhysicalShortcutsDialog.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/PhysicalShortcutsDialog.kt @@ -36,6 +36,7 @@ import androidx.compose.ui.window.DialogProperties import androidx.compose.ui.window.DialogWindowProvider import com.droiddeck.launcher.R import com.droiddeck.launcher.input.PhysicalShortcuts +import com.droiddeck.launcher.session.SessionPrefs @Composable internal fun PhysicalShortcutRows( @@ -48,14 +49,22 @@ internal fun PhysicalShortcutRows( Column { for (action in PhysicalShortcuts.Action.entries) { val assigned = bindings.filter { it.action == action } + val inverted = SessionPrefs.backActionsInverted(context) + val default = stringResource(when (action) { + PhysicalShortcuts.Action.STEAM -> R.string.input_shortcut_guide + PhysicalShortcuts.Action.QAM -> if (inverted) R.string.input_shortcut_back_once else R.string.input_shortcut_back_twice + PhysicalShortcuts.Action.DROIDDECK -> if (inverted) R.string.input_shortcut_back_modes else R.string.input_shortcut_back_once + }) SettingsRow( stringResource(action.label), - assigned.joinToString { "${it.button.label} · ${it.button.name}" }.ifEmpty { null }, + assigned.joinToString { "${it.button.label} · ${it.button.name}" }.ifEmpty { + if (action == PhysicalShortcuts.Action.QAM && SessionPrefs.backHold(context)) context.getString(R.string.input_shortcut_hold_hint) else null + }, ) { SecondaryButton( - stringResource(if (assigned.isEmpty()) R.string.game_settings_default else R.string.ctrl_custom), + if (assigned.isEmpty()) default else assigned.map { it.button.label }.distinct().joinToString(" / "), compact = true, - modifier = modifierFor(action).width(LocalChipMinWidth.current) + modifier = modifierFor(action).widthIn(min = LocalChipMinWidth.current, max = 240.dp) .testTag("shortcuts-assign-${action.name}"), ) { editing = action } } @@ -165,6 +174,12 @@ internal fun PhysicalShortcutsDialog( modifier = Modifier.testTag("shortcuts-remove"), ) } + SecondaryButton(stringResource(R.string.input_restore_shortcut), enabled = bindings.any { it.action == action }, + modifier = Modifier.testTag("shortcuts-restore-default")) { + PhysicalShortcuts.restoreDefault(context, action) + onChanged() + onDismiss() + } SecondaryButton(stringResource(R.string.common_close), modifier = Modifier.testTag("shortcuts-cancel"), onClick = onDismiss) } @@ -174,7 +189,7 @@ internal fun PhysicalShortcutsDialog( } @Composable -private fun CaptureShortcutKeys(onKey: (KeyEvent) -> Boolean) { +internal fun CaptureShortcutKeys(onKey: (KeyEvent) -> Boolean) { val currentOnKey by rememberUpdatedState(onKey) val window = (LocalView.current.parent as DialogWindowProvider).window DisposableEffect(window) { diff --git a/app/src/main/java/com/droiddeck/launcher/ui/ScreenKeyEditor.kt b/app/src/main/java/com/droiddeck/launcher/ui/ScreenKeyEditor.kt deleted file mode 100644 index 94e1a5ff27..0000000000 --- a/app/src/main/java/com/droiddeck/launcher/ui/ScreenKeyEditor.kt +++ /dev/null @@ -1,368 +0,0 @@ -package com.droiddeck.launcher.ui - -import android.content.Context -import android.graphics.Bitmap -import android.graphics.BitmapFactory -import android.net.Uri -import androidx.activity.compose.rememberLauncherForActivityResult -import androidx.activity.result.contract.ActivityResultContracts -import androidx.compose.foundation.BorderStroke -import androidx.compose.foundation.background -import androidx.compose.foundation.clickable -import androidx.compose.foundation.gestures.detectDragGestures -import androidx.compose.foundation.layout.Arrangement -import androidx.compose.foundation.layout.Box -import androidx.compose.foundation.layout.Column -import androidx.compose.foundation.layout.Row -import androidx.compose.foundation.layout.fillMaxSize -import androidx.compose.foundation.layout.fillMaxWidth -import androidx.compose.foundation.layout.height -import androidx.compose.foundation.layout.offset -import androidx.compose.foundation.layout.padding -import androidx.compose.foundation.layout.systemBarsPadding -import androidx.compose.foundation.layout.width -import androidx.compose.foundation.layout.widthIn -import androidx.compose.foundation.shape.CircleShape -import androidx.compose.foundation.shape.RoundedCornerShape -import androidx.compose.foundation.text.BasicTextField -import androidx.compose.foundation.text.KeyboardOptions -import androidx.compose.material.icons.Icons -import androidx.compose.material.icons.filled.Close -import androidx.compose.material.icons.filled.Delete -import androidx.compose.material.icons.filled.DragIndicator -import androidx.compose.material.icons.filled.Image -import androidx.compose.material.icons.filled.Keyboard -import androidx.compose.material3.Icon -import androidx.compose.material3.IconButton -import androidx.compose.material3.MaterialTheme -import androidx.compose.material3.Slider -import androidx.compose.material3.Surface -import androidx.compose.material3.Switch -import androidx.compose.material3.Text -import androidx.compose.runtime.Composable -import androidx.compose.runtime.getValue -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.geometry.Offset -import androidx.compose.ui.focus.onFocusChanged -import androidx.compose.ui.input.pointer.pointerInput -import androidx.compose.ui.platform.LocalConfiguration -import androidx.compose.ui.platform.LocalContext -import androidx.compose.ui.platform.LocalDensity -import androidx.compose.ui.res.stringResource -import androidx.compose.ui.text.TextStyle -import androidx.compose.ui.text.font.FontWeight -import androidx.compose.ui.text.input.ImeAction -import androidx.compose.ui.text.style.TextOverflow -import androidx.compose.ui.unit.IntOffset -import androidx.compose.ui.unit.dp -import androidx.compose.ui.unit.sp -import com.droiddeck.launcher.R -import com.droiddeck.launcher.input.SCREEN_KEY_CIRCLE -import com.droiddeck.launcher.input.SCREEN_KEY_DPAD -import com.droiddeck.launcher.input.SCREEN_KEY_PILL -import com.droiddeck.launcher.input.SCREEN_KEY_RECT -import com.droiddeck.launcher.input.SCREEN_KEY_ROUND -import com.droiddeck.launcher.input.SCREEN_KEY_STICK -import com.droiddeck.launcher.input.SCREEN_KEY_TRIGGER -import com.droiddeck.launcher.input.ScreenKeys -import com.droiddeck.launcher.input.pcKeyLabel -import com.droiddeck.launcher.input.screenKeyDirections -import java.io.File -import kotlin.math.roundToInt - -private val PANEL_WIDTH = 600.dp -private val CHIP_PADDING = androidx.compose.foundation.layout.PaddingValues(horizontal = 6.dp, vertical = 2.dp) - -@Composable -fun ScreenKeyEditor( - codes: List, - keyY: Float, - size: Int, - opacity: Float, - shape: Int, - toggle: Boolean, - labels: Boolean, - text: String, - hasImage: Boolean, - onResize: (Int) -> Unit, - onOpacity: (Float) -> Unit, - onShape: (Int) -> Unit, - onToggle: (Boolean) -> Unit, - onLabels: (Boolean) -> Unit, - onText: (String) -> Unit, - onImage: (String) -> Unit, - onClearImage: () -> Unit, - onBind: () -> Unit, - onTextFocus: () -> Unit, - onTextBlur: () -> Unit, - onDirection: (Int) -> Unit, - onRemove: () -> Unit, - onDismiss: () -> Unit, -) { - val p = LocalPalette.current - val context = LocalContext.current - var offset by remember(codes) { mutableStateOf(Offset.Zero) } - val density = LocalDensity.current - val screen = LocalConfiguration.current - val panelWidthPx = with(density) { PANEL_WIDTH.toPx() } - val screenWidthPx = screen.screenWidthDp * density.density - val screenHeightPx = screen.screenHeightDp * density.density - val atTop = keyY > 0.45f - val button = shape <= SCREEN_KEY_RECT - val directions = screenKeyDirections(shape) - val pickImage = rememberLauncherForActivityResult(ActivityResultContracts.GetContent()) { uri -> - if (uri != null) importImage(context, uri, codes)?.let(onImage) - } - Box( - Modifier.fillMaxSize().systemBarsPadding().padding(horizontal = 10.dp, vertical = 6.dp), - contentAlignment = if (atTop) Alignment.TopCenter else Alignment.BottomCenter, - ) { - Surface( - Modifier.widthIn(max = PANEL_WIDTH).fillMaxWidth() - .offset { IntOffset(offset.x.roundToInt(), offset.y.roundToInt()) }, - shape = RoundedCornerShape(18.dp), - color = p.background, contentColor = p.onBackground, border = BorderStroke(1.dp, p.line2), - ) { - Column(Modifier.padding(start = 10.dp, end = 6.dp, top = 2.dp, bottom = 6.dp)) { - Row(Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) { - Icon( - Icons.Default.DragIndicator, null, - tint = p.onBackground.copy(alpha = 0.45f), - modifier = Modifier.pointerInput(panelWidthPx, screenWidthPx, screenHeightPx) { - detectDragGestures { change, drag -> - change.consume() - offset = Offset( - (offset.x + drag.x).coerceIn( - -(screenWidthPx - panelWidthPx) / 2f + 8f, - (screenWidthPx - panelWidthPx) / 2f - 8f, - ), - (offset.y + drag.y).coerceIn( - -screenHeightPx * if (atTop) 0f else 0.75f, - screenHeightPx * if (atTop) 0.75f else 0f, - ), - ) - } - }, - ) - Box( - Modifier.weight(1f).height(36.dp) - .background(p.line2.copy(alpha = 0.24f), RoundedCornerShape(9.dp)) - .padding(horizontal = 10.dp), - contentAlignment = Alignment.CenterStart, - ) { - BasicTextField( - value = text, - onValueChange = { onText(it.take(24)) }, - singleLine = true, - textStyle = TextStyle( - color = p.onBackground, fontSize = 13.sp, - fontWeight = FontWeight.Medium, - ), - cursorBrush = androidx.compose.ui.graphics.SolidColor(p.primary), - keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done), - modifier = Modifier.fillMaxWidth().onFocusChanged { - if (it.isFocused) onTextFocus() else onTextBlur() - }.focusGlide(), - ) - if (text.isEmpty()) { - Text( - stringResource(R.string.screen_key_text), - color = p.onBackground.copy(alpha = 0.4f), - style = MaterialTheme.typography.bodySmall, - ) - } - } - if (button) { - IconButton(onClick = onBind, modifier = Modifier.width(38.dp).focusGlide(CircleShape)) { - Icon( - Icons.Default.Keyboard, stringResource(R.string.screen_key_bind), - tint = p.onBackground.copy(alpha = 0.8f), - ) - } - } - IconButton(onClick = { pickImage.launch("image/*") }, modifier = Modifier.width(38.dp).focusGlide(CircleShape)) { - Icon( - Icons.Default.Image, stringResource(R.string.screen_key_image), - tint = p.onBackground.copy(alpha = 0.8f), - ) - } - if (hasImage) IconButton(onClick = onClearImage, modifier = Modifier.width(34.dp).focusGlide(CircleShape)) { - Icon(Icons.Default.Close, stringResource(R.string.screen_key_image_clear), tint = p.error) - } - IconButton(onClick = onRemove, modifier = Modifier.width(38.dp).focusGlide(CircleShape)) { - Icon(Icons.Default.Delete, stringResource(R.string.common_delete), tint = p.error) - } - IconButton(onClick = onDismiss, modifier = Modifier.width(34.dp).focusGlide(CircleShape)) { - Icon(Icons.Default.Close, null, tint = p.onBackground) - } - } - Row( - Modifier.fillMaxWidth().padding(vertical = 2.dp), - horizontalArrangement = Arrangement.spacedBy(4.dp), - verticalAlignment = Alignment.CenterVertically, - ) { - listOf>( - (if (button) SCREEN_KEY_PILL else null) to R.string.screen_key_type_button, - SCREEN_KEY_TRIGGER to R.string.screen_key_shape_trigger, - SCREEN_KEY_DPAD to R.string.touch_dpad, - SCREEN_KEY_STICK to R.string.deck_surface_stick, - ).forEach { (value, label) -> - val selected = if (value == null) button else shape == value - Chip(stringResource(label), selected, Modifier.weight(1f)) { - onShape(if (value == null) (if (button) shape else SCREEN_KEY_PILL) else value) - } - } - Box(Modifier.width(1.dp).height(20.dp).background(p.line2)) - listOf( - SCREEN_KEY_PILL to R.string.screen_key_shape_pill, - SCREEN_KEY_ROUND to R.string.screen_key_shape_round, - SCREEN_KEY_CIRCLE to R.string.screen_key_shape_circle, - SCREEN_KEY_RECT to R.string.screen_key_shape_rect, - ).forEach { (value, label) -> - Chip( - stringResource(label), button && shape == value, Modifier.weight(1f), enabled = button, - ) { onShape(value) } - } - Box(Modifier.width(1.dp).height(20.dp).background(p.line2)) - Text( - stringResource(R.string.screen_key_toggle), Modifier.padding(start = 6.dp, end = 2.dp), - style = MaterialTheme.typography.bodySmall, maxLines = 1, - ) - Switch(checked = toggle, onCheckedChange = onToggle, modifier = Modifier.height(30.dp).focusGlide()) - } - if (directions.isNotEmpty()) { - Row( - Modifier.fillMaxWidth().padding(vertical = 2.dp), - horizontalArrangement = Arrangement.spacedBy(4.dp), - verticalAlignment = Alignment.CenterVertically, - ) { - directions.forEachIndexed { index, arrow -> - val code = codes.getOrNull(index) ?: 0 - Chip( - if (code == 0) arrow else "$arrow ${pcKeyLabel(context, code)}", - code != 0, Modifier.weight(1f), - ) { onDirection(index) } - } - Text( - stringResource(R.string.screen_key_text), Modifier.padding(start = 6.dp, end = 2.dp), - style = MaterialTheme.typography.bodySmall, maxLines = 1, - ) - Switch(checked = labels, onCheckedChange = onLabels, modifier = Modifier.height(30.dp).focusGlide()) - } - } - Row( - Modifier.fillMaxWidth().padding(top = 2.dp), - horizontalArrangement = Arrangement.spacedBy(6.dp), - verticalAlignment = Alignment.CenterVertically, - ) { - KeySlider( - label = stringResource(R.string.screen_key_size), - value = "$size", - current = size.toFloat(), - min = ScreenKeys.MIN_SIZE.toFloat(), - max = ScreenKeys.MAX_SIZE.toFloat(), - modifier = Modifier.weight(1f), - ) { onResize(it.roundToInt()) } - KeySlider( - label = stringResource(R.string.screen_key_opacity), - value = "${(opacity * 100).roundToInt()}%", - current = opacity, - min = 0.2f, - max = 1f, - modifier = Modifier.weight(1f), - onChange = onOpacity, - ) - } - } - } - } -} - -@Composable -private fun KeySlider( - label: String, - value: String, - current: Float, - min: Float, - max: Float, - modifier: Modifier, - onChange: (Float) -> Unit, -) { - Row(modifier, verticalAlignment = Alignment.CenterVertically) { - Text(label, style = MaterialTheme.typography.bodySmall, maxLines = 1) - Slider(current, onChange, Modifier.weight(1f).height(28.dp).focusGlide(), valueRange = min..max) - Text( - value, Modifier.width(32.dp), style = MaterialTheme.typography.bodySmall, - fontWeight = FontWeight.SemiBold, maxLines = 1, - ) - } -} - -@Composable -private fun Chip( - label: String, - selected: Boolean, - modifier: Modifier = Modifier, - enabled: Boolean = true, - onClick: () -> Unit, -) { - val p = LocalPalette.current - Box( - modifier - .background( - if (selected) p.primary.copy(alpha = 0.18f) else p.line2.copy(alpha = 0.28f), - CircleShape, - ) - .focusGlide(RoundedCornerShape(8.dp)).clickable(enabled = enabled, onClick = onClick) - .padding(vertical = 6.dp), - contentAlignment = Alignment.Center, - ) { - Text( - label, style = MaterialTheme.typography.labelSmall, maxLines = 1, overflow = TextOverflow.Ellipsis, - color = when { - !enabled -> p.onBackground.copy(alpha = 0.28f) - selected -> p.primary - else -> p.onBackground.copy(alpha = 0.7f) - }, - ) - } -} - -private fun importImage(context: Context, uri: Uri, codes: List): String? { - val dir = File(context.filesDir, ScreenKeys.IMAGE_DIR).apply { mkdirs() } - val name = "key_" + codes.joinToString("_") + "_" + System.currentTimeMillis() + ".png" - val longest = (ScreenKeys.MAX_SIZE * context.resources.displayMetrics.density).toInt() - return runCatching { - val bounds = BitmapFactory.Options().apply { inJustDecodeBounds = true } - context.contentResolver.openInputStream(uri)?.use { BitmapFactory.decodeStream(it, null, bounds) } - val source = maxOf(bounds.outWidth, bounds.outHeight) - if (source <= 0) return@runCatching null - var sample = 1 - while (source / (sample * 2) >= longest) sample *= 2 - val bitmap = context.contentResolver.openInputStream(uri)?.use { - BitmapFactory.decodeStream(it, null, BitmapFactory.Options().apply { inSampleSize = sample }) - } ?: return@runCatching null - val scale = longest.toFloat() / maxOf(bitmap.width, bitmap.height) - val scaled = if (scale < 1f) { - Bitmap.createScaledBitmap( - bitmap, - (bitmap.width * scale).toInt().coerceAtLeast(1), - (bitmap.height * scale).toInt().coerceAtLeast(1), - true, - ) - } else { - bitmap - } - try { - val saved = File(dir, name).outputStream().use { scaled.compress(Bitmap.CompressFormat.PNG, 100, it) } - if (saved) name else null - } finally { - if (scaled !== bitmap) bitmap.recycle() - scaled.recycle() - } - }.getOrNull().also { if (it == null) File(dir, name).delete() } -} diff --git a/app/src/main/java/com/droiddeck/launcher/ui/ScreenLayoutPanel.kt b/app/src/main/java/com/droiddeck/launcher/ui/ScreenLayoutPanel.kt deleted file mode 100644 index 7bc78f7105..0000000000 --- a/app/src/main/java/com/droiddeck/launcher/ui/ScreenLayoutPanel.kt +++ /dev/null @@ -1,139 +0,0 @@ -package com.droiddeck.launcher.ui - -import androidx.compose.foundation.shape.CircleShape -import androidx.activity.compose.rememberLauncherForActivityResult -import androidx.activity.result.contract.ActivityResultContracts -import androidx.compose.foundation.BorderStroke -import androidx.compose.foundation.clickable -import androidx.compose.foundation.layout.Arrangement -import androidx.compose.foundation.layout.Box -import androidx.compose.foundation.layout.Column -import androidx.compose.foundation.layout.Row -import androidx.compose.foundation.layout.fillMaxSize -import androidx.compose.foundation.layout.fillMaxWidth -import androidx.compose.foundation.layout.heightIn -import androidx.compose.foundation.layout.padding -import androidx.compose.foundation.layout.systemBarsPadding -import androidx.compose.foundation.layout.widthIn -import androidx.compose.foundation.rememberScrollState -import androidx.compose.foundation.shape.RoundedCornerShape -import androidx.compose.foundation.text.KeyboardOptions -import androidx.compose.foundation.verticalScroll -import androidx.compose.material.icons.Icons -import androidx.compose.material.icons.filled.Close -import androidx.compose.material3.Button -import androidx.compose.material3.Icon -import androidx.compose.material3.IconButton -import androidx.compose.material3.MaterialTheme -import androidx.compose.material3.OutlinedTextField -import androidx.compose.material3.Surface -import androidx.compose.material3.Text -import androidx.compose.material3.TextButton -import androidx.compose.runtime.Composable -import androidx.compose.runtime.getValue -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.platform.LocalContext -import androidx.compose.ui.res.stringResource -import androidx.compose.ui.text.font.FontWeight -import androidx.compose.ui.text.input.ImeAction -import androidx.compose.ui.unit.dp -import com.droiddeck.launcher.ui.GlideDialog as Dialog -import androidx.compose.ui.window.DialogProperties -import com.droiddeck.launcher.R - -@Composable -fun ScreenLayoutPanel( - layouts: List, - onSave: (String) -> Unit, - onLoad: (String) -> Unit, - onDelete: (String) -> Unit, - onImport: (String) -> Unit, - exportText: () -> String, - onDismiss: () -> Unit, -) { - val p = LocalPalette.current - val context = LocalContext.current - var naming by remember { mutableStateOf(false) } - var name by remember { mutableStateOf("") } - - val importer = rememberLauncherForActivityResult(ActivityResultContracts.OpenDocument()) { uri -> - if (uri == null) return@rememberLauncherForActivityResult - val text = runCatching { - context.contentResolver.openInputStream(uri)?.use { it.readBytes().toString(Charsets.UTF_8) } - }.getOrNull() - if (text != null) onImport(text) - } - val exporter = rememberLauncherForActivityResult(ActivityResultContracts.CreateDocument("application/json")) { uri -> - if (uri == null) return@rememberLauncherForActivityResult - runCatching { context.contentResolver.openOutputStream(uri)?.use { it.write(exportText().toByteArray()) } } - } - - Dialog(onDismissRequest = onDismiss, properties = DialogProperties(usePlatformDefaultWidth = false)) { - Box(Modifier.fillMaxSize().systemBarsPadding().padding(16.dp), contentAlignment = Alignment.Center) { - Surface( - Modifier.widthIn(max = 520.dp).fillMaxWidth(), shape = RoundedCornerShape(22.dp), - color = p.background, contentColor = p.onBackground, border = BorderStroke(1.dp, p.line2), - ) { - Column(Modifier.padding(start = 20.dp, end = 8.dp, top = 6.dp, bottom = 12.dp)) { - Row(verticalAlignment = Alignment.CenterVertically) { - Text( - stringResource(R.string.screen_key_layout), Modifier.weight(1f), - style = MaterialTheme.typography.titleMedium, fontWeight = FontWeight.SemiBold, - ) - IconButton(onClick = onDismiss, modifier = Modifier.focusGlide(CircleShape)) { Icon(Icons.Default.Close, null) } - } - Row(Modifier.fillMaxWidth().padding(end = 12.dp), horizontalArrangement = Arrangement.spacedBy(8.dp)) { - TextButton(onClick = { naming = true }, modifier = Modifier.focusGlide(CircleShape)) { Text(stringResource(R.string.screen_key_layout_save)) } - TextButton(onClick = { importer.launch(arrayOf("application/json", "text/plain", "*/*")) }, modifier = Modifier.focusGlide(CircleShape)) { - Text(stringResource(R.string.boot_movie_import)) - } - TextButton(onClick = { exporter.launch("droiddeck-layout.icp") }, modifier = Modifier.focusGlide(CircleShape)) { - Text(stringResource(R.string.screen_key_layout_export)) - } - } - Column(Modifier.padding(end = 12.dp).heightIn(max = 320.dp).verticalScroll(rememberScrollState())) { - layouts.forEach { layout -> - Row(Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) { - Text( - layout, Modifier.weight(1f).clickable { onLoad(layout) }.padding(vertical = 14.dp), - style = MaterialTheme.typography.bodyLarge, - ) - TextButton(onClick = { onDelete(layout) }, modifier = Modifier.focusGlide(CircleShape)) { - Text(stringResource(R.string.common_delete), color = p.error) - } - } - } - } - } - } - } - } - - if (naming) { - Dialog(onDismissRequest = { naming = false }, properties = DialogProperties(usePlatformDefaultWidth = false)) { - Box(Modifier.fillMaxSize().systemBarsPadding().padding(16.dp), contentAlignment = Alignment.Center) { - Surface( - Modifier.widthIn(max = 520.dp).fillMaxWidth(), shape = RoundedCornerShape(22.dp), - color = p.background, contentColor = p.onBackground, border = BorderStroke(1.dp, p.line2), - ) { - Column(Modifier.padding(20.dp), verticalArrangement = Arrangement.spacedBy(12.dp)) { - OutlinedTextField( - name, { name = it.take(24) }, Modifier.fillMaxWidth().focusGlide(), singleLine = true, - keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done), - ) - Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.End) { - TextButton(onClick = { naming = false }, modifier = Modifier.focusGlide(CircleShape)) { Text(stringResource(R.string.common_cancel)) } - Button(onClick = { onSave(name.trim()); naming = false }, enabled = name.isNotBlank(), modifier = Modifier.focusGlide(CircleShape)) { - Text(stringResource(R.string.screen_key_layout_save)) - } - } - } - } - } - } - } -} diff --git a/app/src/main/java/com/droiddeck/launcher/ui/SecondScreenPresentation.kt b/app/src/main/java/com/droiddeck/launcher/ui/SecondScreenPresentation.kt index bd89179663..5e9e004e07 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/SecondScreenPresentation.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/SecondScreenPresentation.kt @@ -29,9 +29,6 @@ import com.droiddeck.launcher.session.SessionState 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, private val onClose: () -> Unit, @@ -62,6 +59,7 @@ class SecondScreenPresentation( private var imeSeen = false private var fieldKeyboard = false private var terminalPanel: SessionTerminalPanel? = null + private var controlsView: InputSurfaceView? = null private var created = false init { @@ -121,8 +119,9 @@ class SecondScreenPresentation( } fun showMode(mode: SecondScreenMode) { - if (currentMode == mode && isShowing) return - currentMode = mode + val next = if (mode == SecondScreenMode.KEYBOARD_TRACKPAD || mode == SecondScreenMode.DECK_CONTROLS) SecondScreenMode.CUSTOM_CONTROLS else mode + if (currentMode == next && isShowing) return + currentMode = next if (created) renderMode() } @@ -161,7 +160,7 @@ class SecondScreenPresentation( view.disposeComposition() } - val showsKeyboard: Boolean get() = isShowing && !menuShown && currentMode == SecondScreenMode.KEYBOARD_TRACKPAD + val showsKeyboard: Boolean get() = isShowing && !menuShown && currentMode == SecondScreenMode.CUSTOM_CONTROLS val showsControls: Boolean get() = isShowing && currentMode != SecondScreenMode.NONE @@ -214,6 +213,7 @@ class SecondScreenPresentation( dropChat() keyboardHost?.hide() keyboardHost = null + controlsView = null dismiss() } @@ -236,6 +236,22 @@ class SecondScreenPresentation( return super.dispatchGenericMotionEvent(event) } + fun refreshControls() { + if (!created || currentMode != SecondScreenMode.CUSTOM_CONTROLS) return + val controls = controlsView ?: return + val layout = com.droiddeck.launcher.input.InputLibraries.read(context) + .activeLayout(SessionState.mode == SessionService.MODE_DESKTOP, com.droiddeck.launcher.input.InputRole.SECONDARY) + controls.updateCustomLayout(layout?.elements.orEmpty()) + controls.updateStyle(layout?.style ?: com.droiddeck.launcher.input.InputSurfaceStyle()) + } + + fun showKeyboard(): Boolean { + val host = keyboardHost ?: return false + host.toggle() + keyboardShown.value = host.shown + return true + } + override fun onWindowFocusChanged(hasFocus: Boolean) { super.onWindowFocusChanged(hasFocus) if (hasFocus) { @@ -256,19 +272,28 @@ class SecondScreenPresentation( closeTerminal() closeDashboard() root.removeAllViews() + controlsView = null ownComposeViews() + val library = com.droiddeck.launcher.input.InputLibraries.read(context) + val profileLayout = library.activeLayout(SessionState.mode == SessionService.MODE_DESKTOP, com.droiddeck.launcher.input.InputRole.SECONDARY) + val requested = currentMode val mode = when { - currentMode == SecondScreenMode.DECK_CONTROLS && !SessionState.deckPad -> SecondScreenMode.NONE - currentMode == SecondScreenMode.DASHBOARD && dashboard == null -> SecondScreenMode.NONE - currentMode == SecondScreenMode.DESKTOP && !BottomDesktop.available() -> SecondScreenMode.NONE - else -> currentMode + requested == SecondScreenMode.DASHBOARD && dashboard == null -> SecondScreenMode.NONE + requested == SecondScreenMode.DESKTOP && !BottomDesktop.available() -> SecondScreenMode.NONE + else -> requested } if (mode == SecondScreenMode.NONE) return when (mode) { - SecondScreenMode.KEYBOARD_TRACKPAD -> renderKeyboardAndTrackpad() SecondScreenMode.TERMINAL -> renderTerminal() - SecondScreenMode.DECK_CONTROLS -> root.addView(DeckControlsPanel(context, onClose, onSteamMenu, onQam, pad, onMenu = onMenu, onSwap = onSwap), weightParams()) + SecondScreenMode.CUSTOM_CONTROLS -> { + val controls = InputSurfaceView(context, pad, profileLayout?.elements.orEmpty(), + surfaceStyle = profileLayout?.style ?: com.droiddeck.launcher.input.InputSurfaceStyle()) + controlsView = controls + root.addView(controls, weightParams()) + attachKeyboard() + } + SecondScreenMode.KEYBOARD_TRACKPAD, SecondScreenMode.DECK_CONTROLS -> Unit SecondScreenMode.DASHBOARD -> renderDashboard() SecondScreenMode.DESKTOP -> { root.addView(BottomDesktopPanel(context, onClose, onMenu, @@ -299,17 +324,7 @@ class SecondScreenPresentation( dashboardView = null } - private fun renderKeyboardAndTrackpad() { - bar(SecondScreenMode.KEYBOARD_TRACKPAD) { - listOf(BarExtra(context.getString(if (keyboardShown.value) R.string.second_hide_keyboard else R.string.second_show_keyboard), - icon = R.drawable.ic_bottom_keyboard, active = keyboardShown.value, tag = "bottom-keyboard") { - keyboardHost?.let { host -> - host.toggle() - keyboardShown.value = host.shown - } - }) - } - root.addView(trackpadView(), weightParams()) + private fun attachKeyboard() { val host = KeyboardHost(context) { event -> val handled = sessionActivity.dispatchKeyEvent(event) if (fieldKeyboard && event.action == KeyEvent.ACTION_UP && @@ -321,8 +336,6 @@ class SecondScreenPresentation( root.addView(host, ViewGroup.LayoutParams(1, 1)) } - private fun trackpadView(): View = SessionTrackpadView(context, sendPointer, sendButton, sendWheel) - private fun renderTerminal() { bar(SecondScreenMode.TERMINAL) { listOf(BarExtra(context.getString(R.string.drawer_keyboard), icon = R.drawable.ic_bottom_keyboard, tag = "bottom-keyboard") { diff --git a/app/src/main/java/com/droiddeck/launcher/ui/SessionDrawerPage.kt b/app/src/main/java/com/droiddeck/launcher/ui/SessionDrawerPage.kt index ec13f79997..07289bdf29 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/SessionDrawerPage.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/SessionDrawerPage.kt @@ -1,9 +1,9 @@ package com.droiddeck.launcher.ui internal enum class SessionDrawerPage(val firstControl: String) { - CONTROLLER("touch"), + CONTROLLER("input-profile"), GLASSES("glasses-game"), - SECOND_SCREEN("second-screen:keyboard-trackpad"), + SECOND_SCREEN("second-screen:custom-controls"), DISPLAY("fg"), HUD("hud"), EFFECTS("look"), diff --git a/app/src/main/java/com/droiddeck/launcher/ui/SessionOverlay.kt b/app/src/main/java/com/droiddeck/launcher/ui/SessionOverlay.kt index 17409c85bc..ac34b0e92b 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/SessionOverlay.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/SessionOverlay.kt @@ -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, @@ -275,9 +276,6 @@ class DrawerActions( val onEditSteamTouch: (() -> Unit)? = null, val controller: com.droiddeck.launcher.input.ControllerPrefs.Settings? = null, val onRumble: (Boolean) -> Unit = {}, - val onSteamButton: (Boolean) -> Unit = {}, - val onQamButton: (Boolean) -> Unit = {}, - val onKeyboardButton: (Boolean) -> Unit = {}, val onSuspendPolicy: (String) -> Unit, val onSteamDownloadsInBackground: (Boolean) -> Unit = {}, val onBackActionsInverted: (Boolean) -> Unit, @@ -290,14 +288,12 @@ class DrawerActions( val directCursor: Boolean = false, val onDirectCursor: (Boolean) -> Unit = {}, val onTouch: (String) -> Unit, + val onInputSettings: () -> Unit = {}, + val onInputSelection: (com.droiddeck.launcher.input.InputRole?) -> Unit = {}, val onGestureSettings: () -> Unit = {}, val touchActive: String = "", val directHoldBehavior: String = MOUSE_BEHAVIOR_CLICK, val onDirectHoldBehavior: (String) -> Unit = {}, - val onScreenKeys: (() -> Unit)? = null, - val onScreenLayouts: (() -> Unit)? = null, - val screenKeysShown: Boolean = true, - val onScreenKeysShown: (Boolean) -> Unit = {}, val onFexPreset: (String) -> Unit, val onUpscaler: (Int) -> Unit = {}, val onUpscaleSharpness: (Int) -> Unit = {}, @@ -325,7 +321,6 @@ class DrawerActions( val onStop: () -> Unit, val onStopFrom: ((androidx.compose.ui.geometry.Rect?) -> Unit)? = null, val onClose: () -> Unit, - val onDismissScreen: (() -> Unit)? = null, val components: ComponentsManager.Snapshot? = null, val systemPowerVr: Boolean = false, val onComponentsRefresh: () -> Unit = {}, @@ -431,7 +426,7 @@ internal fun SessionDrawer(open: Boolean, requestedPage: SessionDrawerPage, cont page == SessionDrawerPage.GLASSES -> "glasses-tab-$glassesTab" page == SessionDrawerPage.SESSION && a.isHomeApp -> "apps" page == SessionDrawerPage.SECOND_SCREEN -> "second-screen:" + - (if (a.secondScreenMode == SecondScreenMode.NONE && !a.secondScreenNone) SecondScreenMode.KEYBOARD_TRACKPAD else a.secondScreenMode).id + (if (a.secondScreenMode == SecondScreenMode.NONE && !a.secondScreenNone) SecondScreenMode.CUSTOM_CONTROLS else a.secondScreenMode).id else -> page.firstControl } if (turned) focus.forget(page) @@ -489,7 +484,7 @@ internal fun SessionDrawer(open: Boolean, requestedPage: SessionDrawerPage, cont ) { SteamActionPill( stringResource(R.string.common_close), Icons.Outlined.Close, focus.track(page, "close"), - ) { host.open = null; (a.onDismissScreen ?: a.onClose)() } + ) { host.open = null; a.onClose() } if (roomyHeader) Spacer(Modifier.weight(1f)) SteamActionPill( stringResource(R.string.stores_dl_pause), Icons.Outlined.Pause, focus.track(page, "pause"), @@ -595,8 +590,11 @@ internal fun SessionDrawer(open: Boolean, requestedPage: SessionDrawerPage, cont } SessionDrawerPage.CONTROLLER -> { SettingsGroup(stringResource(R.string.drawer_page_controller)) { + 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 -> - ChoiceRow(host, "pad-type", stringResource(R.string.mode_controller), null, + ChoiceRow(host, "pad-type", stringResource(R.string.input_virtual_controller), stringResource(R.string.input_virtual_controller_hint), listOf(SessionPrefs.CONTROLLER_DECK to stringResource(R.string.mode_controller_deck), SessionPrefs.CONTROLLER_STEAM to stringResource(R.string.mode_controller_steam), SessionPrefs.CONTROLLER_DS4 to stringResource(R.string.mode_controller_ds4), @@ -604,43 +602,35 @@ internal fun SessionDrawer(open: Boolean, requestedPage: SessionDrawerPage, cont SessionPrefs.CONTROLLER_XBOX360 to stringResource(R.string.mode_controller_x360)), type, chipModifier = focus.track(page, "pad-type"), onPick = a.onPadType) } - ChoiceRow(host, "touch", stringResource(R.string.mode_touch), null, + a.controller?.let { c -> + ToggleRow(host, "rumble", stringResource(R.string.ctrl_rumble), null, c.rumble, + chipModifier = focus.track(page, "rumble"), onChange = a.onRumble) + + } + } + SettingsGroup(stringResource(R.string.input_touchscreen)) { + ChoiceRow(host, "touch", stringResource(R.string.input_touchscreen), stringResource(R.string.input_touchscreen_hint), listOf(SessionPrefs.TOUCH_AUTO to stringResource(R.string.drawer_touch_auto, a.touchAuto), SessionPrefs.TOUCH_PAD to stringResource(R.string.mode_touch_touchpad), SessionPrefs.TOUCH_DIRECT to stringResource(R.string.mode_touch_direct), SessionPrefs.TOUCH_GESTURES to stringResource(R.string.mode_touch_gestures), SessionPrefs.TOUCH_OFF to stringResource(R.string.widgets_off)), a.touchMode, chipModifier = focus.track(page, "touch"), onPick = a.onTouch) if (a.touchActive == SessionPrefs.TOUCH_PAD) RelativeMouseRow(host, a, focus.track(page, "relative-mouse")) if (a.touchActive == SessionPrefs.TOUCH_DIRECT) - ToggleRow(host, "direct-cursor", stringResource(R.string.drawer_direct_cursor), - null, a.directCursor, + ToggleRow(host, "direct-cursor", stringResource(R.string.input_touch_cursor), + stringResource(R.string.input_touch_cursor_hint), a.directCursor, chipModifier = focus.track(page, "direct-cursor"), onChange = a.onDirectCursor) when (a.touchActive) { SessionPrefs.TOUCH_GESTURES -> ActionRow(stringResource(R.string.gesture_settings), null, stringResource(R.string.ctrl_edit), a.onGestureSettings, modifier = focus.track(page, "gesture-settings")) - SessionPrefs.TOUCH_DIRECT -> ChoiceRow(host, "long-press", stringResource(R.string.gesture_long_press), null, + SessionPrefs.TOUCH_DIRECT -> ChoiceRow(host, "long-press", stringResource(R.string.input_touch_hold), stringResource(R.string.input_touch_hold_hint), listOf(MOUSE_BEHAVIOR_NONE to stringResource(R.string.gesture_hold_left), MOUSE_BEHAVIOR_CLICK to stringResource(R.string.gesture_right_click), MOUSE_BEHAVIOR_HOLD to stringResource(R.string.gesture_hold_right)), a.directHoldBehavior, chipModifier = focus.track(page, "long-press"), onPick = a.onDirectHoldBehavior) } - if (a.onScreenKeys != null) { - SettingsRow(stringResource(R.string.screen_keys), null, stacked = narrow) { - Row( - verticalAlignment = Alignment.CenterVertically, - horizontalArrangement = Arrangement.spacedBy(10.dp), - ) { - SecondaryButton(stringResource(R.string.ctrl_edit), enabled = a.screenKeysShown, - modifier = focus.track(page, "screen-keys"), onClick = a.onScreenKeys) - a.onScreenLayouts?.let { open -> - SecondaryButton(stringResource(R.string.screen_key_layout), - modifier = focus.track(page, "screen-key-layouts"), onClick = open) - } - ToggleSwitch(a.screenKeysShown, true, - modifier = focus.track(page, "screen-keys-shown"), onChange = a.onScreenKeysShown) - } - } - } + } + SettingsGroup(stringResource(R.string.input_screen_visibility)) { ChoiceRow(host, "osc", stringResource(R.string.mode_osc), null, if (a.steam) listOf(SessionPrefs.OSC_AUTO to stringResource(R.string.common_auto), SessionPrefs.OSC_ALWAYS to stringResource(R.string.common_always), SessionPrefs.OSC_STEAM_TOUCH to stringResource(R.string.osc_steam_touch), SessionPrefs.OSC_STEAM_QAM to stringResource(R.string.mode_osc_qam), SessionPrefs.OSC_NEVER to stringResource(R.string.common_never)) else listOf(SessionPrefs.OSC_AUTO to stringResource(R.string.common_auto), SessionPrefs.OSC_ALWAYS to stringResource(R.string.common_always), SessionPrefs.OSC_NEVER to stringResource(R.string.common_never)), @@ -649,30 +639,6 @@ internal fun SessionDrawer(open: Boolean, requestedPage: SessionDrawerPage, cont a.onEditSteamTouch?.let { edit -> ActionRow(stringResource(R.string.osc_steam_layout), stringResource(R.string.osc_steam_layout_hint), stringResource(R.string.ctrl_edit), edit) } - a.controller?.let { c -> - ToggleRow(host, "rumble", stringResource(R.string.ctrl_rumble), null, c.rumble, - chipModifier = focus.track(page, "rumble"), onChange = a.onRumble) - if (a.onScreenButtonsVisible) { - if (a.steam) { - ToggleRow(host, "steam-button", stringResource(R.string.ctrl_steam_button), null, c.steamButton, - chipModifier = focus.track(page, "steam-button"), onChange = a.onSteamButton) - ToggleRow(host, "qam-button", stringResource(R.string.ctrl_qam_button), null, c.qamButton, - chipModifier = focus.track(page, "qam-button"), onChange = a.onQamButton) - } - ToggleRow(host, "keyboard-button", stringResource(R.string.ctrl_keyboard_button), null, c.keyboardButton, - chipModifier = focus.track(page, "keyboard-button"), onChange = a.onKeyboardButton) - } - } - if (a.steam) ToggleRow(host, "back-hold", stringResource(R.string.back_hold), - stringResource(R.string.back_hold_hint), a.backHold, - chipModifier = focus.track(page, "back-hold"), onChange = a.onBackHold) - if (a.steam) ChoiceRow(host, "back-actions", stringResource(R.string.mode_back), null, - listOf(false to stringResource(SessionPrefs.BACK_MENU_THEN_QAM), true to stringResource(SessionPrefs.BACK_QAM_THEN_MENU)), - a.backActionsInverted, chipModifier = focus.track(page, "back-actions"), onPick = a.onBackActionsInverted) - if (a.steam) ChoiceRow(host, "back-double-tap", stringResource(R.string.back_double_tap_window), null, - SessionPrefs.BACK_DOUBLE_TAP_MS_OPTIONS.map { it to stringResource(R.string.back_tap_ms, it) }, - a.backDoubleTapMs, chipModifier = focus.track(page, "back-double-tap"), onPick = a.onBackDoubleTapMs) - PhysicalShortcutRows(modifierFor = { focus.track(page, "physical-shortcut-${it.name}") }) if (a.steam && a.unfoldedControls != null) ToggleRow(host, "unfolded-controls", stringResource(R.string.unfolded_controls), stringResource(R.string.unfolded_controls_hint), a.unfoldedControls, chipModifier = focus.track(page, "unfolded-controls"), onChange = a.onUnfoldedControls) @@ -682,6 +648,18 @@ internal fun SessionDrawer(open: Boolean, requestedPage: SessionDrawerPage, cont chipModifier = focus.track(page, "unfolded-controls-with-controller"), onChange = a.onUnfoldedControlsWithController) } + SettingsGroup(stringResource(R.string.shortcuts_title)) { + if (a.steam) ToggleRow(host, "back-hold", stringResource(R.string.back_hold), + stringResource(R.string.back_hold_hint), a.backHold, + chipModifier = focus.track(page, "back-hold"), onChange = a.onBackHold) + if (a.steam) ChoiceRow(host, "back-actions", stringResource(R.string.input_back_taps), stringResource(R.string.input_back_taps_hint), + listOf(false to stringResource(SessionPrefs.BACK_MENU_THEN_QAM), true to stringResource(SessionPrefs.BACK_QAM_THEN_MENU)), + a.backActionsInverted, chipModifier = focus.track(page, "back-actions"), onPick = a.onBackActionsInverted) + if (a.steam) ChoiceRow(host, "back-double-tap", stringResource(R.string.back_double_tap_window), null, + SessionPrefs.BACK_DOUBLE_TAP_MS_OPTIONS.map { it to stringResource(R.string.back_tap_ms, it) }, + a.backDoubleTapMs, chipModifier = focus.track(page, "back-double-tap"), onPick = a.onBackDoubleTapMs) + PhysicalShortcutRows(modifierFor = { focus.track(page, "physical-shortcut-${it.name}") }) + } SettingsGroup(stringResource(R.string.drawer_latency)) { ToggleRow(host, "low-latency", stringResource(R.string.drawer_low_latency), stringResource(R.string.drawer_low_latency_hint), a.lowLatency, @@ -1174,8 +1152,7 @@ private fun SteamVrRow(a: DrawerActions) { } internal fun secondScreenModes(none: Boolean, deckPad: Boolean, dashboard: Boolean, desktop: Boolean): List = - (if (none) listOf(SecondScreenMode.NONE) else emptyList()) + listOf(SecondScreenMode.KEYBOARD_TRACKPAD, SecondScreenMode.TERMINAL) + - (if (deckPad) listOf(SecondScreenMode.DECK_CONTROLS) else emptyList()) + + (if (none) listOf(SecondScreenMode.NONE) else emptyList()) + listOf(SecondScreenMode.CUSTOM_CONTROLS, SecondScreenMode.TERMINAL) + (if (dashboard) listOf(SecondScreenMode.DASHBOARD) else emptyList()) + (if (desktop) listOf(SecondScreenMode.DESKTOP) else emptyList()) @@ -1185,6 +1162,8 @@ private fun SecondScreenGroup(host: MenuHost, a: DrawerActions, track: (String) val pal = LocalPalette.current val modes = secondScreenModes(a.secondScreenNone, com.droiddeck.launcher.session.SessionState.deckPad, a.dashboardAvailable, a.desktopAvailable) SettingsGroup(stringResource(R.string.drawer_second_screen_mode)) { + Text(stringResource(R.string.input_second_mode_hint), Modifier.padding(14.dp), + style = MaterialTheme.typography.bodySmall, color = colors.onSurfaceVariant) Column(Modifier.fillMaxWidth().selectableGroup()) { modes.forEach { mode -> val selected = a.secondScreenMode == mode diff --git a/app/src/main/java/com/droiddeck/launcher/ui/SessionPanels.kt b/app/src/main/java/com/droiddeck/launcher/ui/SessionPanels.kt index c1e629251b..35d5538bfb 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/SessionPanels.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/SessionPanels.kt @@ -2,21 +2,17 @@ package com.droiddeck.launcher.ui import android.content.ClipboardManager import android.content.Context -import android.graphics.Canvas import android.graphics.Color import android.util.Log import android.view.Gravity import android.view.KeyEvent import android.view.MotionEvent import android.view.View -import android.view.ViewConfiguration import android.view.inputmethod.InputMethodManager import android.widget.HorizontalScrollView import android.widget.LinearLayout import android.widget.TextView import com.droiddeck.launcher.R -import com.droiddeck.launcher.input.PointerGestures -import com.droiddeck.launcher.input.TouchpadGestures import com.droiddeck.launcher.session.SessionService import com.droiddeck.launcher.session.SessionTerminal import com.termux.terminal.TerminalEmulator @@ -25,65 +21,6 @@ import com.termux.terminal.TerminalSessionClient import com.termux.view.TerminalView import com.termux.view.TerminalViewClient -class SessionTrackpadView( - context: Context, - 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, -) : View(context) { - private fun dp(value: Int): Int = (value * resources.displayMetrics.density + 0.5f).toInt() - - 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 - setBackgroundColor(Color.BLACK) - } - - 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) - } - - private val surface = android.graphics.RectF() - private val fill = android.graphics.Paint(android.graphics.Paint.ANTI_ALIAS_FLAG).apply { color = Color.rgb(0x0A, 0x0B, 0x0D) } - private val edge = android.graphics.Paint(android.graphics.Paint.ANTI_ALIAS_FLAG).apply { - style = android.graphics.Paint.Style.STROKE - color = Color.rgb(0x26, 0x29, 0x2E) - strokeWidth = resources.displayMetrics.density - } - - override fun onDraw(canvas: Canvas) { - val inset = dp(12).toFloat() - surface.set(inset, 0f, width - inset, height - inset) - if (surface.height() <= inset) return - val radius = dp(20).toFloat() - canvas.drawRoundRect(surface, radius, radius, fill) - canvas.drawRoundRect(surface, radius, radius, edge) - } - - override fun onTouchEvent(event: MotionEvent): Boolean = gestures.onTouch(event) - - override fun onDetachedFromWindow() { - gestures.cancel() - super.onDetachedFromWindow() - } -} - class SessionTerminalPanel(context: Context) : LinearLayout(context) { private var terminalSession: TerminalSession? = null private var terminalView: TerminalView? = null @@ -123,7 +60,7 @@ class SessionTerminalPanel(context: Context) : LinearLayout(context) { val keys = LinearLayout(context).apply { orientation = HORIZONTAL; setPadding(dp(12), 0, dp(12), dp(8)) } fun key(label: String, input: String, onClick: ((TextView) -> Unit)? = null) { lateinit var button: TextView - button = DeckControlsPanel.roundButton(context, label, label, { if (onClick != null) onClick(button) else session.write(input) }, wide = true) + button = InputSurfaceView.roundButton(context, label, label, { if (onClick != null) onClick(button) else session.write(input) }, wide = true) if (keys.childCount > 0) keys.addView(View(context), LinearLayout.LayoutParams(dp(8), 1)) keys.addView(button) } diff --git a/app/src/main/java/com/droiddeck/launcher/ui/SettingsWidgets.kt b/app/src/main/java/com/droiddeck/launcher/ui/SettingsWidgets.kt index 2189b26ba1..b5595a52b7 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/SettingsWidgets.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/SettingsWidgets.kt @@ -509,16 +509,23 @@ fun ChoiceRow( stacked: Boolean = false, onPick: (T) -> Unit, ) { + SettingsRow(label, hint, highlighted = host.open == key, hintLines = hintLines, stacked = stacked) { + ChoiceChip(host, key, label, options, selected, enabled, note, chipModifier, onPick) + } +} + +@Composable +internal fun ChoiceChip(host: MenuHost, key: String, label: String, options: List>, selected: T, + enabled: Boolean = true, note: String? = null, modifier: Modifier = Modifier, onPick: (T) -> Unit) { val open = host.open == key - SettingsRow(label, hint, highlighted = open, hintLines = hintLines, stacked = stacked) { - Box { - ValueChip(options.firstOrNull { it.first == selected }?.second ?: "-", open, enabled, modifier = chipModifier.testTag("setting-$key")) { host.open = if (open) null else key } - AnchoredMenu(open, onDismiss = { if (host.open == key) host.open = null }, title = label, note = note) { firstItemFocus -> - options.forEachIndexed { index, (value, text) -> - MenuItem(text, checked = value == selected, focusRequester = if (index == 0) firstItemFocus else null) { - onPick(value) - host.open = null - } + Box { + ValueChip(options.firstOrNull { it.first == selected }?.second ?: "-", open, enabled, + modifier = modifier.testTag("setting-$key")) { host.open = if (open) null else key } + AnchoredMenu(open, onDismiss = { if (host.open == key) host.open = null }, title = label, note = note) { firstItemFocus -> + options.forEachIndexed { index, (value, text) -> + MenuItem(text, checked = value == selected, focusRequester = if (index == 0) firstItemFocus else null) { + onPick(value) + host.open = null } } } @@ -714,6 +721,7 @@ fun SettingsPage( scroll: androidx.compose.foundation.ScrollState? = null, scrollContent: Boolean = true, compactLayout: Boolean = false, + showBack: Boolean = true, header: (@Composable () -> Unit)? = null, modifier: Modifier = Modifier, content: @Composable ColumnScope.() -> Unit, @@ -736,7 +744,7 @@ fun SettingsPage( horizontalArrangement = Arrangement.spacedBy(12.dp), modifier = Modifier.fillMaxWidth(), ) { - BackLink(stringResource(R.string.mode_back), compact = true, onClick = onBack) + if (showBack) BackLink(stringResource(R.string.mode_back), compact = true, onClick = onBack) if (header != null) Box(Modifier.weight(1f)) { header() } } } @@ -746,7 +754,7 @@ fun SettingsPage( verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(12.dp), modifier = Modifier.padding(bottom = 6.dp), ) { - BackLink(stringResource(R.string.mode_back), compact = true, onClick = onBack) + if (showBack) BackLink(stringResource(R.string.mode_back), compact = true, onClick = onBack) Text( title, fontSize = 20.sp, fontWeight = FontWeight.Bold, color = colors.onBackground, maxLines = 1, overflow = TextOverflow.Ellipsis, modifier = Modifier.weight(1f), @@ -758,7 +766,7 @@ fun SettingsPage( } else { Rise(0) { Row(verticalAlignment = Alignment.CenterVertically) { - BackLink(stringResource(R.string.mode_back), compact = compactLayout, onClick = onBack) + if (showBack) BackLink(stringResource(R.string.mode_back), compact = compactLayout, onClick = onBack) if (eyebrow != null) { Spacer(Modifier.width(10.dp)) Eyebrow(eyebrow) diff --git a/app/src/main/res/drawable/ic_input_quick_access.xml b/app/src/main/res/drawable/ic_input_quick_access.xml new file mode 100644 index 0000000000..1a1a6943e1 --- /dev/null +++ b/app/src/main/res/drawable/ic_input_quick_access.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/drawable/ic_input_steam.xml b/app/src/main/res/drawable/ic_input_steam.xml new file mode 100644 index 0000000000..4a423ce320 --- /dev/null +++ b/app/src/main/res/drawable/ic_input_steam.xml @@ -0,0 +1,8 @@ + + + + + + + diff --git a/app/src/main/res/values/input_library.xml b/app/src/main/res/values/input_library.xml new file mode 100644 index 0000000000..d463478069 --- /dev/null +++ b/app/src/main/res/values/input_library.xml @@ -0,0 +1,159 @@ + + + Unreadable input settings were preserved in a recovery copy. You can continue editing the restored profiles. + This built-in layout keeps its existing controls. Create a custom layout to add, move or resize elements. + Properties + Click properties + %1$s · Click + Buttons + Sticks, pads and triggers + Hide tools + Show tools + Controller + Send controller input to Steam Input or the game. + This element sends no controller, mouse or keyboard input. + Mouse click + DroidDeck action + Action + Scroll speed: %1$d steps/s + Auto selection + Shortcuts and diagnostics + Layouts + Opening a profile for editing does not activate it. + Saved profiles + + New profile + + New layout + + New bindings + Saved elements, positions and sizes. Actions are stored in bindings. + Used by %1$d profiles + %1$s · Used by %2$d profiles. Changes apply to every profile using these bindings. + Empty layout + Saved parts + Choose shared bindings and a layout for each touch screen. + Arrange elements directly on the target display. Properties open on the main display. + Use this profile + Rename + Make a copy + Delete + Add elements to the layout before assigning their actions. + Edit on + Confirm change + Continue with this reset or deletion? + Could not save input settings + Name + Start from + Button + Stick + Trackpad + D-pad + Trigger + Done + Undo + + Add + Drag to move. Drag a corner to resize. Tap Properties to assign an action. + Tap to place the new element. + Editing on your second screen + The selected display is not available. Connect it to edit this layout at its actual size. + Discard unsaved layout changes? + This layout is used by %1$d profiles. Saved changes apply to all of them. + Unassigned + Default profiles + Desktop mode + Game mode + The session is pinned to %1$s. Choose Auto in the session menu to use these defaults. + Default for Desktop and Game + Default for Game mode + Default for Desktop mode + Device settings + Vibration, stick dead zone, shortcuts and diagnostics + Controls + Buttons, sticks, triggers and extra keys + Copy profile + Reset actions + Reset the actions in %1$s to its base profile defaults? Layouts and other profiles will stay unchanged. + Delete %1$s? Modes using it will return to their built-in default profile. Saved layouts and shared settings will remain available. + Visibility + Applies to all profiles. Auto shows the main-screen controls when no gamepad is connected in Game mode. + Edit on screen + Saved layouts… + Choose a layout or create your own + Built-in gamepad options + Appearance, buttons and positions + These options apply to the built-in main-screen gamepad in every profile. Custom layouts are edited directly on screen. + Choose Profile controls in the second-screen session menu. The Profile and Second screen selectors in the Controller menu choose the controls to show. + Button actions + Shared with %1$s. Edits apply to those profiles too. + Reuse settings + Use settings from… + Copy from another profile or keep settings shared + Make independent + Keep the current setup and stop sharing changes + Layouts store elements and their positions. Each profile can assign its own actions. Edit at full size on the target screen. + Current layout + Delete the unused layout %1$s and its saved actions? + Rename current layout + Reuse %1$s from another profile. The other control sources will stay unchanged. + Keep physical controls shared + By default, all controls and custom layouts are independent copies. Sharing only links the physical controls. + Use these elements and positions. Sharing keeps future geometry edits in sync; your button actions remain independent. A different layout starts with unassigned actions. + Copy these controls and actions, or link them so future edits apply to both profiles. + Keep shared + Independent copy + Built-in gamepad · positions shared across profiles + Start stick at touch position + Hide stick when not in use + Tools + Auto (%1$s) + Custom (%1$s) + Save as new profile + Profile controls + Profile + Mouse touchpad gestures + When assigned to the mouse pointer: tap to click, hold to drag, and scroll with two fingers. + %1$s has a quick-menu override. Select Auto there to use its default profile and all three saved parts. + DroidDeck Input + Held while the stick is moved in any direction: press above 55%, release below 35%. Use Direction keys to assign each direction separately. DroidDeck commands run once per deflection. The stick click is configured separately. + Held while touching the trackpad; released when you lift your finger. DroidDeck commands run once per touch. The trackpad click is configured separately. + Held when the trigger passes 55%; released below 35%. DroidDeck commands run once per press. + Scroll direction + Vertical + Horizontal + Scroll horizontally + Measurements + Enable recording, use controls in a running session, then return and read the results. Measures DroidDeck dispatch time only, not display latency. Recording ends when the app process closes. + %1$d controls + Other saved layouts (%1$d) ▾ + Hide other saved layouts (%1$d) ▴ + Home / Guide + Back · single tap + Back · double tap + Back · twice in Steam, once in Desktop + In Steam mode. Holding Back also opens quick access. + Restore to default + Your panel choice is remembered. Desktop opens a second workspace; it does not switch the session or its input profile. + Virtual controller + The controller type seen by Steam or the game. + Touchscreen + How touches on the game screen are handled. Profile buttons keep their assigned actions. + Touch cursor + Show a pointer for direct touches on the game screen. + Touch and hold + Mouse action when holding a finger on the game screen. + Back button taps + First action: single tap. Second action: double tap. + Toggle button + Tap to hold, tap again to release. Opening a menu or switching profiles releases the button. + Shape + Rounded + Pill + Circle + Rectangle + Button label + Text + Imported image + Separate diagonal keys + Assign a key to each diagonal. An empty diagonal presses its two neighboring directions. + Neighboring directions + Your old screen keys are saved as imported profiles below. Select one to use or edit it. Your current profile selections have been kept. + The old screen keys could not be imported automatically. The original settings have been kept. + diff --git a/app/src/main/res/values/input_strings.xml b/app/src/main/res/values/input_strings.xml new file mode 100644 index 0000000000..765191d9e2 --- /dev/null +++ b/app/src/main/res/values/input_strings.xml @@ -0,0 +1,53 @@ + + Input profiles + Choose what each control sends. Native controls keep their gamepad or Steam Input behavior; local actions send keys, mouse input or DroidDeck commands. + Profiles + Game + Desktop + Active profile + Auto uses Desktop in desktop sessions and Game in other sessions. Select Game manually when playing a game from the desktop. + Edit profile + Control source + Physical controller + Main screen + Second screen + Bindings + Native / Steam Input + Native: %1$s + Send another controller control + Disabled + Key or key combination + Directional keys + Move pointer + Pointer speed: %1$d + Scroll + Scroll vertically + Left mouse button + Right mouse button + Middle mouse button + Open DroidDeck menu + Show keyboard + Open Steam menu + Open Steam quick access + Screen layout + Custom second-screen layout + Arrange controls here, then select Custom controls in the second-screen panel. Their actions come from this profile’s Second screen bindings. Native trackpads require a Steam Deck controller; Steam Mobile Touch layouts remain configured in Steam. + Horizontal position + Vertical position + Width + Height + Remove control + Add control + Custom controls + Device-specific buttons + Assign a physical button + Release the button, then choose Edit, Retry, or Cancel. B or Back cancels after release. + Retry + Press a button, including a back button reported as a keyboard key. Volume and Android system keys are reserved. The assignment applies only to this device. + Remove assignment + Input diagnostics + Record input dispatch time + Read measurements + Measures Android dispatch through publication to the existing input transport. This excludes Bluetooth arrival, guest polling, rendering and display latency. Recording is off by default. + Use normal input binding + diff --git a/app/src/test/java/com/droiddeck/launcher/input/InputButtonIconsTest.kt b/app/src/test/java/com/droiddeck/launcher/input/InputButtonIconsTest.kt new file mode 100644 index 0000000000..2969933db5 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/input/InputButtonIconsTest.kt @@ -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() } + } + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/input/InputLibraryTest.kt b/app/src/test/java/com/droiddeck/launcher/input/InputLibraryTest.kt new file mode 100644 index 0000000000..b96515a009 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/input/InputLibraryTest.kt @@ -0,0 +1,394 @@ +package com.droiddeck.launcher.input + +import android.content.Context +import org.junit.Assert.* +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.annotation.Config + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [34], manifest = Config.NONE) +class InputLibraryTest { + private val context: Context get() = RuntimeEnvironment.getApplication() + private val prefs get() = context.getSharedPreferences("input-profiles", Context.MODE_PRIVATE) + @Before fun reset() { + 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() + InputSurfaceLayout.saveLegacy(context, "desktop", listOf(InputSurfaceElement(InputControl.B, .3f, .2f, .4f, .4f))) + val legacy = InputProfiles.readLegacy(context, "desktop") + assertEquals(InputAction.Keys(listOf(29, 33)), legacy.action(InputRole.PHYSICAL, InputControl.A)) + val library = InputLibraries.read(context) + assertEquals(legacy.bindings, library.compile("desktop").bindings) + assertEquals(legacy.keys, library.compile("desktop").keys) + assertEquals("desktop", library.selected) + assertEquals(.3f, library.layout("desktop", InputRole.SECONDARY)!!.elements.single().x) + assertEquals(library, InputLibraries.decode(InputLibraries.encode(library))) + } + + @Test fun profilesSharePhysicalBindingsWhileUsingDifferentTouchActions() { + val original = InputLibraries.read(context) + val shared = original.duplicateProfile("desktop", "Editor", "editor", sharePhysical = true) + val p = shared.preset(shared.profile("desktop").binding(InputRole.PHYSICAL)) + val updated = shared.withPreset(p.copy(controls = p.controls + (InputControl.R3 to InputAction.Keys(listOf(29, 33))))) + .copyBindings(shared.profile("editor").binding(InputRole.SECONDARY), "Editor touch", "editor", InputRole.SECONDARY, "editor.touch") + val touch = updated.preset("editor.touch") + val final = updated.withPreset(touch.copy(controls = touch.controls + (InputControl.A to InputAction.Keys(listOf(28))))).validate() + assertEquals(InputAction.Keys(listOf(29, 33)), final.compile("editor").action(InputRole.PHYSICAL, InputControl.R3)) + assertEquals(InputAction.Keys(listOf(29, 33)), final.compile("desktop").action(InputRole.PHYSICAL, InputControl.R3)) + assertEquals(InputAction.MouseButton(272), final.compile("desktop").action(InputRole.SECONDARY, InputControl.A)) + assertEquals(InputAction.Keys(listOf(28)), final.compile("editor").action(InputRole.SECONDARY, InputControl.A)) + assertEquals(final.layout("desktop", InputRole.SECONDARY)!!.elements, final.layout("editor", InputRole.SECONDARY)!!.elements) + assertNotEquals(final.layout("desktop", InputRole.SECONDARY)!!.id, final.layout("editor", InputRole.SECONDARY)!!.id) + } + + @Test fun copyingPhysicalBindingsOnlyRelinksTheSelectedProfile() { + val library = InputLibraries.read(context).duplicateProfile("desktop", "Editor", "editor") + val id = library.profile("desktop").binding(InputRole.PHYSICAL) + val copied = library.copyBindings(id, "Editor controller", "editor", InputRole.PHYSICAL, "private") + assertEquals(id, copied.profile("desktop").binding(InputRole.PHYSICAL)) + assertEquals("private", copied.profile("editor").binding(InputRole.PHYSICAL)) + assertEquals(library.preset(id).controls, copied.preset("private").controls) + assertEquals(library.profile("editor").parts, library.copyBindings(id, "Library copy").profile("editor").parts) + } + + @Test fun copyingProfileIsIndependentAndPreservesElementActions() { + val original = InputLibraries.read(context) + val library = original.duplicateProfile("desktop", "Editor", "editor").validate() + for (role in InputRole.entries) { + assertNotEquals(library.profile("desktop").binding(role), library.profile("editor").binding(role)) + assertEquals(library.compile("desktop").bindings[role], library.compile("editor").bindings[role]) + if (library.layout("desktop", role)?.editable == true) { + assertNotEquals(library.layout("desktop", role)!!.id, library.layout("editor", role)!!.id) + assertEquals(library.layout("desktop", role)!!.elements, library.layout("editor", role)!!.elements) + } + } + assertEquals(original.profiles, library.profiles.filterNot { it.id == "editor" }) + assertEquals(original.selected, library.selected) + } + + @Test fun sharingGeometryKeepsActionsIndependentAndDetachingCopiesBoth() { + var library = InputLibraries.read(context).duplicateProfile("desktop", "Editor", "editor") + val source = library.layout("desktop", InputRole.SECONDARY)!! + library = library.useLayout("editor", InputRole.SECONDARY, source.id, shared = true).validate() + assertEquals(source.id, library.layout("editor", InputRole.SECONDARY)!!.id) + assertNotEquals(library.profile("desktop").binding(InputRole.SECONDARY), library.profile("editor").binding(InputRole.SECONDARY)) + library = library.usePart("editor", InputRole.SECONDARY, "desktop", shared = true).validate() + assertEquals(library.profile("desktop").parts[InputRole.SECONDARY], library.profile("editor").parts[InputRole.SECONDARY]) + val detached = library.usePart("editor", InputRole.SECONDARY, "editor", shared = false).validate() + assertNotEquals(source.id, detached.layout("editor", InputRole.SECONDARY)!!.id) + assertNotEquals(detached.profile("desktop").binding(InputRole.SECONDARY), detached.profile("editor").binding(InputRole.SECONDARY)) + assertEquals(library.compile("editor"), detached.compile("editor")) + } + + @Test fun elementIdsSurviveGeometryEditsAndDeletionPrunesEveryLinkedPreset() { + val library = InputLibraries.read(context) + val layout = library.layout("game", InputRole.SECONDARY)!! + val e = InputSurfaceElement(InputControl.A, .1f, .1f, .2f, .2f, "button-1") + val b = library.preset(library.profile("game").binding(InputRole.SECONDARY)) + val saved = library.withLayout(layout.copy(elements = listOf(e))) + .withPreset(b.copy(elements = mapOf(e.id to InputAction.Keys(listOf(57))))) + .copyBindings(b.id, "Alternative", newId = "alternative").validate() + val moved = saved.withLayout(layout.copy(elements = listOf(e.copy(x = .6f)))).validate() + assertEquals(InputAction.Keys(listOf(57)), moved.preset(b.id).action(e)) + val deleted = moved.withLayout(layout.copy(elements = emptyList())).validate() + assertTrue(deleted.preset(b.id).elements.isEmpty()) + assertTrue(deleted.preset("alternative").elements.isEmpty()) + } + + @Test fun missingAndIncompatibleReferencesAreRejected() { + val library = InputLibraries.read(context) + assertThrows(IllegalArgumentException::class.java) { library.removeBindings(library.profile("game").binding(InputRole.PHYSICAL)) } + assertThrows(IllegalArgumentException::class.java) { library.removeLayout(library.layout("game", InputRole.SECONDARY)!!.id) } + assertThrows(IllegalArgumentException::class.java) { library.chooseBindings("game", InputRole.PHYSICAL, library.profile("game").binding(InputRole.SECONDARY)) } + assertThrows(IllegalArgumentException::class.java) { library.chooseLayout("game", InputRole.PHYSICAL, InputLibraries.DECK) } + } + + @Test fun deletingActiveProfileFallsBackWithoutDeletingSharedParts() { + val library = InputLibraries.read(context).duplicateProfile("desktop", "Editor", "editor") + val removed = library.copy(selected = "editor", desktop = "editor").removeProfile("editor").validate() + assertEquals(InputProfiles.AUTO, removed.selected) + assertEquals("desktop", removed.desktop) + assertEquals(library.layouts, removed.layouts) + assertEquals(library.bindings, removed.bindings) + } + + @Test fun futureLibrariesAreNotOverwritten() { + val future = org.json.JSONObject(InputLibraries.encode(InputLibraries.read(context))).put("version", 999).toString() + prefs.edit().putString("library", future).commit() + InputLibraries.read(context) + assertThrows(IllegalArgumentException::class.java) { InputLibraries.update(context) { it.copy(selected = "desktop") } } + assertEquals(future, prefs.getString("library", null)) + } + + @Test fun unreadableLibrariesKeepStableEditorReferencesUntilAnEditPreservesRecoveryData() { + val valid = InputLibraries.encode(InputLibraries.read(context)) + val invalidValues = listOf("{broken", org.json.JSONObject(valid).put("selected", "missing").toString()) + for (raw in invalidValues) { + prefs.edit().putString("library", raw).commit() + val first = InputLibraries.read(context) + val second = InputLibraries.read(context) + assertEquals(first, second) + val id = first.profile("game").binding(InputRole.PRIMARY) + assertEquals(first.preset(id), second.preset(id)) + assertEquals(raw, prefs.getString("library", null)) + val updated = InputLibraries.update(context) { it.copy(selected = "desktop") } + assertEquals("desktop", InputLibraries.read(context).selected) + assertEquals(updated, InputLibraries.read(context)) + assertTrue(prefs.all.filterKeys { it.startsWith("library-recovery.") }.containsValue(raw)) + } + assertTrue(InputLibraries.hasLibraryRecovery(context)) + assertEquals(invalidValues.toSet(), prefs.all.filterKeys { it.startsWith("library-recovery.") }.values.toSet()) + } + + @Test fun resettingAProfileDoesNotResetSharedBindingsOrAccumulatePrivateCopies() { + var library = InputLibraries.read(context).duplicateProfile("desktop", "Editor", "editor", sharePhysical = true) + val common = library.preset(library.profile("desktop").binding(InputRole.PHYSICAL)) + library = library.withPreset(common.copy(controls = common.controls + (InputControl.A to InputAction.Keys(listOf(57))))) + .resetProfile("editor").validate() + val count = library.bindings.size + repeat(100) { library = library.resetProfile("editor").validate() } + assertEquals(count, library.bindings.size) + assertEquals(InputAction.Keys(listOf(57)), library.compile("desktop").action(InputRole.PHYSICAL, InputControl.A)) + assertEquals(InputAction.MouseButton(272), library.compile("editor").action(InputRole.PHYSICAL, InputControl.A)) + } + + @Test fun clickActionsRoundTripAndArePrunedWithTheirElement() { + val library = InputLibraries.read(context) + val layout = library.layout("desktop", InputRole.SECONDARY)!! + val pad = layout.elements.first { it.hasClick } + val preset = library.preset(library.profile("desktop").binding(InputRole.SECONDARY)) + val updated = library.withPreset(preset.copy(clicks = mapOf(pad.id to InputAction.Keys(listOf(29, 33))))).validate() + val roundTrip = InputLibraries.decode(InputLibraries.encode(updated))!! + assertEquals(InputAction.Keys(listOf(29, 33)), roundTrip.compile("desktop").elementClickAction(InputRole.SECONDARY, pad)) + assertEquals(preset.action(pad), roundTrip.preset(preset.id).action(pad)) + assertTrue(roundTrip.withLayout(layout.copy(elements = emptyList())).preset(preset.id).clicks.isEmpty()) + } + @Test fun versionTwoFixedGamepadMigratesOnceWithoutChangingOtherProfiles() { + val original = InputLibraries.read(context) + val fixed = original.copy(layouts = original.layouts.map { if (it.id == InputLibraries.OVERLAY) it.copy(elements = emptyList(), template = InputLayoutTemplate.OVERLAY) else it }, + bindings = original.bindings.map { if (it.layoutId == InputLibraries.OVERLAY) it.copy(layoutId = null, elements = emptyMap(), clicks = emptyMap()) else it }) + val raw = org.json.JSONObject(InputLibraries.encode(fixed)).put("version", 2).toString() + prefs.edit().putString("library", raw).commit() + ControllerPrefs.setStickClick(context, false) + ControllerPrefs.setKeyboardButton(context, true) + val size = context.resources.displayMetrics + ControllerPrefs.setLayout(context, size.widthPixels, size.heightPixels, mapOf("a" to (.5f to .5f), "ls" to (.3f to .4f))) + ControllerPrefs.setLayout(context, 1600, 900, mapOf("ls" to (.3f to .4f))) + val migrated = InputLibraries.read(context) + val saved = prefs.getString("library", null) + assertEquals(migrated, InputLibraries.read(context)) + assertEquals(saved, prefs.getString("library", null)) + assertFalse(migrated.layouts.any { it.template == InputLayoutTemplate.OVERLAY }) + assertTrue(migrated.layouts.any { it.id == "migrated.layout.1600x900" }) + assertEquals(original.layout("game", InputRole.SECONDARY), migrated.layout("game", InputRole.SECONDARY)) + assertEquals(original.preset(original.profile("game").binding(InputRole.PHYSICAL)), migrated.preset(migrated.profile("game").binding(InputRole.PHYSICAL))) + val layout = migrated.layout("game", InputRole.PRIMARY)!! + val preset = migrated.preset(migrated.profile("game").binding(InputRole.PRIMARY)) + assertTrue(layout.editable) + assertEquals(InputAction.Command(InputCommand.KEYBOARD), preset.elements["builtin:keyboard"]) + assertEquals(InputAction.Disabled, preset.clickAction(layout.elements.first { it.control == InputControl.LS })) + } + + @Test fun oldGamepadPlacementAndVisibilityBecomeEditableElements() { + ControllerPrefs.setLayout(context, 1200, 800, mapOf("ls" to (.4f to .5f))) + ControllerPrefs.setSteamButton(context, false) + ControllerPrefs.setTarget(context, "b", ControllerPrefs.OFF) + val elements = LegacyGamepadLayout(context, 1200, 800).elements() + val stick = elements.first { it.control == InputControl.LS } + assertEquals(.4f, stick.x + stick.width / 2f, .0001f) + assertEquals(.5f, stick.y + stick.height / 2f, .0001f) + assertFalse(elements.any { it.control == InputControl.GUIDE || it.control == InputControl.B }) + assertTrue(elements.all { it == it.bounded() }) + } + + @Test fun quickPartsAreModeSpecificAndDoNotEditSavedProfiles() { + val original = InputLibraries.read(context) + val mixed = original.selectPart(true, InputRole.SECONDARY, InputStockLayouts.TRACKPAD).validate() + assertEquals(original.profiles, mixed.profiles) + assertEquals(original.compileActive(false), mixed.compileActive(false)) + assertEquals(InputLibraries.TRACKPAD, mixed.activeLayout(true, InputRole.SECONDARY)?.id) + assertEquals(InputAction.Pointer(), mixed.compileActive(true).elementAction(InputRole.SECONDARY, "legacy:right-pad", InputControl.RIGHT_PAD)) + assertEquals(mixed, InputLibraries.decode(InputLibraries.encode(mixed))) + val restored = mixed.selectProfile(true, InputProfiles.AUTO) + assertTrue(restored.selection(true).parts.isEmpty()) + assertEquals(original.compileActive(true), restored.compileActive(true)) + } + + @Test fun explicitAutoSelectionStaysDistinctFromAnInheritedProfileAfterSaving() { + val original = InputLibraries.read(context) + val binding = original.profile("game").binding(InputRole.PHYSICAL) + val inherited = original.selectPart(false, InputRole.PHYSICAL, binding) + val explicit = original.selectProfile(false, InputProfiles.AUTO).selectPart(false, InputRole.PHYSICAL, binding) + assertFalse(InputLibraries.decode(InputLibraries.encode(inherited))!!.selection(false).profileSelected) + assertTrue(InputLibraries.decode(InputLibraries.encode(explicit))!!.selection(false).profileSelected) + assertEquals(inherited.compileActive(false), explicit.compileActive(false)) + } + + @Test fun versionSevenSelectionsRetainKnownProfileAndPartChoices() { + val original = InputLibraries.read(context) + val binding = original.profile("game").binding(InputRole.PHYSICAL) + val choices = listOf( + original.selectPart(false, InputRole.PHYSICAL, binding) to false, + original.selectProfile(false, InputProfiles.AUTO) to true, + original.selectProfile(false, "game").selectPart(false, InputRole.PHYSICAL, binding) to true, + original.selectPart(false, InputRole.SECONDARY, InputStockLayouts.TRACKPAD) to false, + ) + for ((library, selected) in choices) { + val old = org.json.JSONObject(InputLibraries.encode(library)).put("version", 7) + old.getJSONObject("selections").getJSONObject("game").remove("profileSelected") + prefs.edit().putString("library", old.toString()).commit() + val restored = InputLibraries.read(context) + assertEquals(selected, restored.selection(false).profileSelected) + assertEquals(library, restored) + assertEquals(restored, InputLibraries.read(context)) + } + } + + @Test fun savedQuickCombinationOwnsIndependentCopiesOfAllThreeParts() { + val original = InputLibraries.read(context) + val mixed = original.selectPart(true, InputRole.SECONDARY, InputStockLayouts.TRACKPAD) + val saved = mixed.saveSelection(true, "Writing", "writing").validate() + assertEquals("writing", saved.activeId(true)) + assertTrue(saved.selection(true).parts.isEmpty()) + for (role in InputRole.entries) { + val source = mixed.activePart(true, role) + val copy = saved.activePart(true, role) + assertNotEquals(source.bindings, copy.bindings) + if (role != InputRole.PHYSICAL) assertNotEquals(source.layout, copy.layout) + assertEquals(mixed.preset(source.bindings).elements, saved.preset(copy.bindings).elements) + } + assertEquals(original.profile("desktop"), saved.profile("desktop")) + } + + @Test fun activeQuickPartsCannotBeDeletedAndInvalidCombinationsAreRejected() { + 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() } + } + + @Test fun oldFixedLowerPanelsMigrateIntoEditableLayoutsWithWorkingCommands() { + val current = InputLibraries.read(context) + val profile = current.profile("game") + val old = current.copy(layouts = current.layouts.map { if (it.id == InputLibraries.DECK) it.copy(elements = emptyList(), template = InputLayoutTemplate.DECK) else it }, + bindings = current.bindings.filterNot { it.layoutId == InputLibraries.DECK }) + .withPreset(SavedInputBindings("old-deck", "Old Deck", false, controls = mapOf(InputControl.L4 to InputAction.Keys(listOf(57))))) + .withProfile(profile.copy(parts = profile.parts + (InputRole.SECONDARY to InputPart("old-deck", InputLibraries.DECK)))).validate() + val raw = org.json.JSONObject(InputLibraries.encode(old)).put("version", 3).toString() + context.getSharedPreferences("input-profiles", Context.MODE_PRIVATE).edit().putString("library", raw).commit() + val upgraded = InputLibraries.read(context) + assertTrue(upgraded.layout("game", InputRole.SECONDARY)!!.editable) + val output = upgraded.compile("game") + assertEquals(InputAction.Keys(listOf(57)), output.elementAction(InputRole.SECONDARY, "legacy:l4", InputControl.L4)) + assertEquals(InputAction.Command(InputCommand.MENU), output.elementAction(InputRole.SECONDARY, "command:0", InputControl.A)) + assertEquals(upgraded, InputLibraries.read(context)) + } + + @Test fun stockPresetsAreScopedToTheSecondScreenAndCopiedWithTheirActions() { + val library = InputLibraries.read(context) + for (layout in InputStockLayouts.layouts()) { + val role = InputRole.SECONDARY + assertFalse(library.layouts.first { it.id == layout.id }.supports(InputRole.PRIMARY)) + val changed = library.useLayout("game", role, layout.id, shared = false).validate() + val copy = changed.layout("game", role)!! + assertNotEquals(layout.id, copy.id) + assertEquals(layout.elements, copy.elements) + assertEquals(InputAction.Command(InputCommand.MENU), changed.compile("game").elementAction(role, "command:0", InputControl.A)) + } + } + + @Test fun versionFourScopingPreservesLayoutsAlreadySharedAcrossScreens() { + val original = InputLibraries.read(context) + val deck = original.layouts.first { it.id == InputLibraries.DECK }.copy(screen = InputLayoutScreen.BOTH) + val shared = original.withLayout(deck).useLayout("game", InputRole.PRIMARY, deck.id, shared = true) + .useLayout("game", InputRole.SECONDARY, deck.id, shared = true) + val json = org.json.JSONObject(InputLibraries.encode(shared)).put("version", 4) + val layouts = json.getJSONArray("layouts") + for (i in 0 until layouts.length()) layouts.getJSONObject(i).remove("screen") + context.getSharedPreferences("input-profiles", Context.MODE_PRIVATE).edit().putString("library", json.toString()).commit() + val migrated = InputLibraries.read(context) + assertEquals(InputLayoutScreen.BOTH, migrated.layouts.first { it.id == deck.id }.screen) + assertEquals(shared.profiles, migrated.profiles) + assertEquals(shared.bindings, migrated.bindings) + assertEquals(migrated, InputLibraries.read(context)) + } + + @Test fun horizontalScrollAndLayoutTargetSurviveSerialization() { + val library = InputLibraries.read(context) + val preset = library.preset(library.profile("desktop").binding(InputRole.PHYSICAL)) + val changed = library.withPreset(preset.copy(controls = preset.controls + (InputControl.RS to InputAction.Scroll(17f, true)))) + assertEquals(changed, InputLibraries.decode(InputLibraries.encode(changed))) + } + +} diff --git a/app/src/test/java/com/droiddeck/launcher/input/InputProfilesTest.kt b/app/src/test/java/com/droiddeck/launcher/input/InputProfilesTest.kt new file mode 100644 index 0000000000..f94a04569f --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/input/InputProfilesTest.kt @@ -0,0 +1,85 @@ +package com.droiddeck.launcher.input + +import android.content.Context +import org.junit.Assert.* +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.annotation.Config + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [34], manifest = Config.NONE) +class InputProfilesTest { + private val context: Context get() = RuntimeEnvironment.getApplication() + @Before fun reset() { + context.getSharedPreferences("input-profiles", Context.MODE_PRIVATE).edit().clear().commit() + context.getSharedPreferences("controller", Context.MODE_PRIVATE).edit().clear().commit() + context.getSharedPreferences("input-surfaces", Context.MODE_PRIVATE).edit().clear().commit() + } + + @Test fun migrationKeepsLegacyTouchMappingSeparateFromPhysicalControls() { + ControllerPrefs.setTarget(context, "a", "b") + ControllerPrefs.setTarget(context, "x", "off") + ControllerPrefs.setSelectSteam(context, true) + val game = InputProfiles.read(context, InputProfile.GAME) + assertEquals(InputAction.Native(InputControl.B), game.action(InputRole.PRIMARY, InputControl.A)) + assertEquals(InputAction.Disabled, game.action(InputRole.PRIMARY, InputControl.X)) + assertEquals(InputAction.Native(), game.action(InputRole.PHYSICAL, InputControl.A)) + assertEquals(InputAction.Native(InputControl.GUIDE), game.action(InputRole.PHYSICAL, InputControl.SELECT)) + assertEquals(InputAction.Native(InputControl.GUIDE), InputProfiles.read(context, InputProfile.DESKTOP) + .action(InputRole.PHYSICAL, InputControl.SELECT)) + InputProfiles.reset(context, InputProfile.GAME) + assertEquals(InputAction.Native(), InputProfiles.read(context, InputProfile.GAME).action(InputRole.PRIMARY, InputControl.A)) + } + + @Test fun migrationPreservesSavedDesktopSelectBinding() { + ControllerPrefs.setSelectSteam(context, true) + context.getSharedPreferences("input-profiles", Context.MODE_PRIVATE).edit().putString("profile.desktop", + """{"version":1,"bindings":[{"role":"PHYSICAL","control":"select","action":{"type":"disabled"}}]}""").commit() + assertEquals(InputAction.Disabled, InputProfiles.read(context, InputProfile.DESKTOP) + .action(InputRole.PHYSICAL, InputControl.SELECT)) + } + + @Test fun migrationKeepsDesktopKeyboardWhenSelectToSteamIsOff() { + assertEquals(InputAction.Command(InputCommand.KEYBOARD), InputProfiles.read(context, InputProfile.DESKTOP) + .action(InputRole.PHYSICAL, InputControl.SELECT)) + } + + @Test fun bindingsAndDeviceKeysRoundTripWithoutAffectingOtherProfile() { + val game = InputProfile.defaults(InputProfile.GAME) + .withBinding(InputRole.SECONDARY, InputControl.A, InputAction.Keys(listOf(29, 30))) + .copy(keys = listOf(PhysicalInputBinding(PhysicalInputKey("1:2:descriptor", "Thor", 131, 59), InputAction.Command(InputCommand.MENU)))) + InputProfiles.save(context, game) + assertEquals(game, InputProfiles.read(context, InputProfile.GAME)) + assertEquals(InputAction.Pointer(), InputProfiles.read(context, InputProfile.DESKTOP).action(InputRole.PHYSICAL, InputControl.LS)) + } + + @Test fun profileSelectionIsExplicitAndNeverDependsOnDisplayPresence() { + assertEquals(InputProfile.DESKTOP, InputProfiles.active(context, true).id) + assertEquals(InputProfile.GAME, InputProfiles.active(context, false).id) + InputProfiles.select(context, InputProfile.GAME) + assertEquals(InputProfile.GAME, InputProfiles.active(context, true).id) + } + + @Test fun unknownVersionDoesNotExecuteBindings() { + context.getSharedPreferences("input-profiles", Context.MODE_PRIVATE).edit() + .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)), + 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() { + InputSurfaceLayout.save(context, InputProfile.GAME, listOf(InputSurfaceElement(InputControl.A, .95f, -.1f, 2f, 2f))) + val element = InputSurfaceLayout.read(context, InputProfile.GAME).single() + assertEquals(.95f, element.x); assertEquals(0f, element.y) + assertTrue(element.x + element.width <= 1f) + assertEquals(InputAction.Native(), InputProfiles.read(context, InputProfile.GAME).action(InputRole.SECONDARY, InputControl.A)) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/input/InputRouterTest.kt b/app/src/test/java/com/droiddeck/launcher/input/InputRouterTest.kt new file mode 100644 index 0000000000..3313995778 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/input/InputRouterTest.kt @@ -0,0 +1,241 @@ +package com.droiddeck.launcher.input + +import org.junit.Assert.* +import org.junit.Test + +class InputRouterTest { + private class Output : InputOutput { + val events = mutableListOf() + override fun key(code: Int, down: Boolean) { events += "key $code $down" } + override fun button(code: Int, down: Boolean) { events += "button $code $down" } + override fun pointer(dx: Float, dy: Float) { events += "pointer $dx $dy" } + override fun scroll(steps: Int) { events += "scroll $steps" } + override fun scrollHorizontal(steps: Int) { events += "horizontal $steps" } + override fun command(command: InputCommand) { events += command.name } + override fun requestMotion() { events += "motion" } + } + private val output = Output() + private val router = InputRouter(output) + private fun bind(control: InputControl, action: InputAction, role: InputRole = InputRole.PHYSICAL) = + InputProfile.defaults(InputProfile.GAME).withBinding(role, control, action) + + @Test fun radialKeysKeepCustomDiagonalsAndReleaseSharedNeighborKeys() { + router.setProfile(bind(InputControl.LS, InputAction.Directions(103, 106, 0, 105, listOf(30, 0, 0, 0)))) + val stick = router.channel(InputRole.PHYSICAL, InputControl.LS) + stick.update(.7f, -.7f) + assertEquals(listOf("key 30 true"), output.events) + stick.update(-.7f, -.7f) + assertEquals(listOf("key 30 true", "key 30 false", "key 105 true", "key 103 true"), output.events) + output.events.clear() + stick.update(0f, -1f) + assertEquals(listOf("key 105 false"), output.events) + router.releaseAll() + assertEquals(listOf("key 105 false", "key 103 false"), output.events) + } + + @Test fun horizontalStickUsesXAndKeepsTheVerticalWheelIndependent() { + router.setProfile(bind(InputControl.RS, InputAction.Scroll(20f, true)).withBinding(InputRole.PHYSICAL, InputControl.LS, InputAction.Scroll(20f))) + val horizontal = router.channel(InputRole.PHYSICAL, InputControl.RS) + val vertical = router.channel(InputRole.PHYSICAL, InputControl.LS) + horizontal.update(1f, 0f); vertical.update(0f, -1f) + router.tick(.05f) + assertTrue(output.events.contains("horizontal 1")) + assertTrue(output.events.contains("scroll -1")) + output.events.clear() + horizontal.update(0f, 1f); vertical.update(1f, 0f) + assertFalse(router.tick(.05f)) + assertTrue(output.events.isEmpty()) + } + + @Test fun horizontalTouchScrollFollowsContactAndDropsRemainderOnSuspend() { + val channel = router.channel(InputRole.SECONDARY, InputControl.RIGHT_PAD, InputAction.Scroll(10f, true)) + channel.update(0f, 0f, true) + channel.update(.25f, .8f, true) + assertEquals(listOf("horizontal -2"), output.events) + router.releaseAll() + output.events.clear() + channel.update(0f, 0f, false) + channel.update(0f, 0f, true) + channel.update(.06f, 0f, true) + assertTrue(output.events.isEmpty()) + } + + @Test fun nativeProfileReturnsOriginalStateWithoutScheduling() { + val source = router.pad(InputRole.PHYSICAL) + val state = PadState().apply { leftX = .7f; press(PadState.A, true) } + repeat(1000) { assertSame(state, source.route(state)) } + assertTrue(output.events.isEmpty()) + } + + @Test fun retainedPadReturnsToNativeFastPathAfterHeldControlsReachNeutral() { + val raw = PadState().apply { press(PadState.A, true); leftX = .8f } + val source = router.pad(InputRole.PHYSICAL, raw) + raw.press(PadState.B, true) + val blocked = source.route(raw) + assertFalse(blocked.isDown(PadState.A)) + assertEquals(0f, blocked.leftX) + assertTrue(blocked.isDown(PadState.B)) + raw.clear() + source.route(raw) + raw.press(PadState.A, true) + repeat(1000) { assertSame(raw, source.route(raw)) } + assertTrue(output.events.isEmpty()) + } + + @Test fun localBindingConsumesOnlyItsOwnNativeControlAndReleasesModifiersLast() { + router.setProfile(bind(InputControl.A, InputAction.Keys(listOf(29, 30)))) + val source = router.pad(InputRole.PHYSICAL) + val state = PadState().apply { press(PadState.A, true); press(PadState.B, true); leftX = .4f } + val routed = source.route(state) + assertFalse(routed.isDown(PadState.A)); assertTrue(routed.isDown(PadState.B)) + assertEquals(.4f, routed.leftX) + source.route(state) + state.press(PadState.A, false); source.route(state) + assertEquals(listOf("key 29 true", "key 30 true", "key 30 false", "key 29 false"), output.events) + } + + @Test fun twoSourcesHoldingOneKeyDoNotReleaseEachOther() { + val p = bind(InputControl.A, InputAction.Keys(listOf(57))) + .withBinding(InputRole.SECONDARY, InputControl.B, InputAction.Keys(listOf(57))) + router.setProfile(p) + val physical = router.pad(InputRole.PHYSICAL) + val touch = router.pad(InputRole.SECONDARY) + physical.route(PadState().apply { press(PadState.A, true) }) + touch.route(PadState().apply { press(PadState.B, true) }) + touch.close() + assertEquals(listOf("key 57 true"), output.events) + physical.release() + assertEquals(listOf("key 57 true", "key 57 false"), output.events) + } + + @Test fun remapCombinesHeldTargetsRatherThanLastButtonWinning() { + router.setProfile(bind(InputControl.A, InputAction.Native(InputControl.B))) + val source = router.pad(InputRole.PHYSICAL) + val raw = PadState().apply { press(PadState.A, true) } + assertTrue(source.route(raw).isDown(PadState.B)) + raw.press(PadState.B, true); raw.press(PadState.A, false) + assertTrue(source.route(raw).isDown(PadState.B)) + raw.press(PadState.B, false) + assertFalse(source.route(raw).isDown(PadState.B)) + } + + @Test fun profileChangeBlocksHeldNativeInputUntilNeutral() { + val source = router.pad(InputRole.PHYSICAL) + val raw = PadState().apply { press(PadState.A, true) } + source.route(raw) + router.setProfile(bind(InputControl.A, InputAction.Keys(listOf(1)))) + source.route(raw) + assertTrue(output.events.isEmpty()) + raw.press(PadState.A, false); source.route(raw) + raw.press(PadState.A, true); source.route(raw) + assertEquals(listOf("key 1 true"), output.events) + } + + @Test fun changingLocalBindingReleasesOldKeyWithoutPressingNewOne() { + router.setProfile(bind(InputControl.A, InputAction.Keys(listOf(1)))) + val source = router.pad(InputRole.PHYSICAL) + val raw = PadState().apply { press(PadState.A, true) } + source.route(raw) + router.setProfile(bind(InputControl.A, InputAction.Keys(listOf(28)))) + source.route(raw) + assertEquals(listOf("key 1 true", "key 1 false"), output.events) + } + + @Test fun triggersUseHysteresisAndCommandsFireOnce() { + router.setProfile(bind(InputControl.LT, InputAction.Command(InputCommand.MENU))) + val source = router.pad(InputRole.PHYSICAL) + val raw = PadState() + for (value in listOf(.6f, .5f, .4f, .6f, .3f, .6f)) { raw.leftTrigger = value; source.route(raw) } + assertEquals(listOf("MENU", "MENU"), output.events) + } + + @Test fun pointerTicksOnlyWhileMovingAndOrdinaryKeysDoNotWaitForTick() { + router.setProfile(InputProfile.defaults(InputProfile.DESKTOP)) + val source = router.pad(InputRole.PHYSICAL) + val raw = PadState().apply { leftX = 1f; press(PadState.X, true) } + source.route(raw) + assertTrue(output.events.contains("key 28 true")) + assertFalse(output.events.any { it.startsWith("pointer") }) + assertTrue(router.tick(.01f)); assertTrue(output.events.contains("pointer 9.0 0.0")) + raw.clear(); source.route(raw) + assertFalse(router.tick(.01f)) + } + + @Test fun trackpadUsesContactRelativeDeltasWithoutFirstTouchJump() { + router.setProfile(bind(InputControl.RIGHT_PAD, InputAction.Pointer(1000f), InputRole.SECONDARY)) + val channel = router.channel(InputRole.SECONDARY, InputControl.RIGHT_PAD) + assertNull(channel.update(.5f, .5f, true)) + assertTrue(output.events.isEmpty()) + channel.update(.6f, .6f, true) + assertEquals(1, output.events.size) + assertTrue(output.events[0].startsWith("pointer 100.")) + channel.update(0f, 0f, false) + channel.update(-.9f, -.9f, true) + assertEquals(1, output.events.size) + } + + @Test fun stickScrollFollowsUpAndDownForEveryInputRoleAndStopsOnRelease() { + for (role in InputRole.entries) { + router.setProfile(bind(InputControl.RS, InputAction.Scroll(10f), role)) + val channel = router.channel(role, InputControl.RS) + output.events.clear() + channel.update(0f, -1f) + repeat(2) { assertTrue(router.tick(.05f)) } + assertEquals(listOf("motion", "scroll -1"), output.events) + channel.update(0f, 1f) + repeat(2) { assertTrue(router.tick(.05f)) } + assertEquals("scroll 1", output.events.last()) + channel.release() + val count = output.events.size + assertFalse(router.tick(.05f)) + assertEquals(count, output.events.size) + channel.close() + } + } + + @Test fun trackpadScrollKeepsItsContactDirectionAndDoesNotScheduleStickMotion() { + router.setProfile(bind(InputControl.LEFT_PAD, InputAction.Scroll(10f), InputRole.SECONDARY)) + val channel = router.channel(InputRole.SECONDARY, InputControl.LEFT_PAD) + channel.update(0f, 0f, true) + channel.update(0f, .5f, true) + channel.update(0f, 0f, true) + assertEquals(listOf("scroll 5", "scroll -5"), output.events) + assertFalse(router.tick(.05f)) + } + + @Test fun disablingRoutingReleasesKeysAndBlocksHeldInputOnResume() { + router.setProfile(bind(InputControl.A, InputAction.Keys(listOf(1)))) + val source = router.pad(InputRole.PHYSICAL) + val raw = PadState().apply { press(PadState.A, true) } + source.route(raw); router.setEnabled(false); source.route(raw); router.setEnabled(true); source.route(raw) + assertEquals(listOf("key 1 true", "key 1 false"), output.events) + raw.clear(); source.route(raw); raw.press(PadState.A, true); source.route(raw) + assertEquals("key 1 true", output.events.last()) + } + + @Test fun disabledSourcesDoNotLeakNativeButtons() { + val source = router.pad(InputRole.PHYSICAL) + router.setEnabled(false) + assertFalse(source.route(PadState().apply { press(PadState.A, true) }).isDown(PadState.A)) + } + + @Test fun lifecycleReleaseAlsoBlocksNativeIdentityRoute() { + val source = router.pad(InputRole.PHYSICAL) + val raw = PadState().apply { press(PadState.A, true) } + source.route(raw); router.releaseAll() + assertFalse(source.route(raw).isDown(PadState.A)) + } + + @Test fun mergedPadKeepsPhysicalButtonAndWholeStickVector() { + val physical = PadState().apply { press(PadState.A, true); leftX = .8f; leftY = -.6f } + physical.merge(PadState().apply { leftX = -.2f; leftY = .1f }) + assertTrue(physical.isDown(PadState.A)); assertEquals(.8f, physical.leftX); assertEquals(-.6f, physical.leftY) + } + + @Test fun outputOwnershipKeepsKeyboardAndMappingHoldsIndependent() { + val held = HeldInput(output::key) + held.set(1, 29, true); held.set(4, 29, true); held.release(4) + assertEquals(listOf("key 29 true"), output.events) + held.clear(); assertEquals("key 29 false", output.events.last()) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/input/InputSourceOwnershipTest.kt b/app/src/test/java/com/droiddeck/launcher/input/InputSourceOwnershipTest.kt new file mode 100644 index 0000000000..b200a195d7 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/input/InputSourceOwnershipTest.kt @@ -0,0 +1,291 @@ +package com.droiddeck.launcher.input + +import java.io.File +import org.junit.Assert.* +import org.junit.After +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.util.ReflectionHelpers + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [34], manifest = Config.NONE, shadows = [JoyConPadTest.RecordingWriter::class]) +class InputSourceOwnershipTest { + private val bridge = PadBridge(File(RuntimeEnvironment.getApplication().cacheDir, "input-ownership")) + private fun state() = JoyConPadTest.RecordingWriter.pads[0]!! + @After fun close() { bridge.stop(); DeckControls.releaseAll(); DeckControls.setRouter(null) } + + @Test fun profileAndEnableChangesLeaveRingClosedUntilInputArrives() { + bridge.setInputRouter(InputRouter(RecordingOutput())) + bridge.setInputProfile(InputProfile.defaults(InputProfile.DESKTOP)) + bridge.setInputEnabled(false) + bridge.setInputEnabled(true) + assertFalse(ReflectionHelpers.getField(bridge, "open")) + bridge.applyExternal({ it.press(PadState.X, true) }, true) + assertTrue(ReflectionHelpers.getField(bridge, "open")) + } + + @Test fun disablingRoutingPublishesReleaseToAnAlreadyOpenRing() { + bridge.setInputRouter(InputRouter(RecordingOutput())) + bridge.applyExternal({ it.press(PadState.A, true) }, true) + assertTrue(state().isDown(PadState.A)) + bridge.setInputEnabled(false) + assertFalse(state().isDown(PadState.A)) + bridge.setInputEnabled(true) + assertFalse(state().isDown(PadState.A)) + } + + @Test fun repeatedEnableStateDoesNotRepublishPadOrDeckReports() { + val router = InputRouter(RecordingOutput()) + bridge.setInputRouter(router) + DeckControls.setRouter(router) + bridge.applyExternal({ it.press(PadState.A, true) }, true) + DeckControls.setGrip(DeckControls.L4, true) + for (enabled in listOf(true, false, true)) { + bridge.setInputEnabled(enabled) + val padWrites = JoyConPadTest.RecordingWriter.padWrites + val deckWrites = JoyConPadTest.RecordingWriter.deckWrites + repeat(10) { bridge.setInputEnabled(enabled) } + assertEquals(padWrites, JoyConPadTest.RecordingWriter.padWrites) + assertEquals(deckWrites, JoyConPadTest.RecordingWriter.deckWrites) + } + assertFalse(state().isDown(PadState.A)) + assertEquals(0, JoyConPadTest.RecordingWriter.grips) + bridge.applyExternal({ it.press(PadState.A, false) }, true) + DeckControls.setGrip(DeckControls.L4, false) + bridge.applyExternal({ it.press(PadState.A, true) }, true) + DeckControls.setGrip(DeckControls.L4, true) + assertTrue(state().isDown(PadState.A)) + assertEquals(DeckControls.L4, JoyConPadTest.RecordingWriter.grips) + } + + @Test fun replacingADisabledRouterAllowsTheNewRouterToBeDisabled() { + bridge.setInputRouter(InputRouter(RecordingOutput())) + bridge.setInputEnabled(false) + bridge.setInputRouter(InputRouter(RecordingOutput())) + bridge.applyExternal({ it.press(PadState.A, true) }, true) + assertTrue(state().isDown(PadState.A)) + bridge.setInputEnabled(false) + assertFalse(state().isDown(PadState.A)) + } + + @Test fun attachingADisabledRouterAllowsItToBeEnabled() { + bridge.setInputRouter(InputRouter(RecordingOutput()).also { it.setEnabled(false) }) + bridge.setInputEnabled(true) + bridge.applyExternal({ it.press(PadState.A, true) }, true) + assertTrue(state().isDown(PadState.A)) + } + + @Test fun replacingRouterReleasesRetainedPhysicalAndTouchHoldsUntilNeutral() { + val before = RecordingOutput() + val after = RecordingOutput() + val profile = InputProfile.defaults(InputProfile.GAME) + .withBinding(InputRole.PHYSICAL, InputControl.A, InputAction.Keys(listOf(29))) + .withBinding(InputRole.SECONDARY, InputControl.B, InputAction.Keys(listOf(29))) + bridge.setInputRouter(InputRouter(before).also { it.setProfile(profile) }) + bridge.applyExternal({ it.press(PadState.A, true) }, true) + val touch = Any() + bridge.applyTouch(touch, InputRole.SECONDARY) { it.press(PadState.B, true) } + bridge.setInputRouter(InputRouter(after).also { it.setProfile(profile) }) + bridge.applyExternal({ it.press(PadState.X, true) }, true) + assertEquals(listOf("key 29 true", "key 29 false"), before.events) + assertTrue(after.events.isEmpty()) + bridge.applyExternal({ it.press(PadState.A, false) }, true) + bridge.applyExternal({ it.press(PadState.A, true) }, true) + assertEquals(listOf("key 29 true"), after.events) + bridge.applyExternal({ it.press(PadState.A, false) }, true) + assertEquals(listOf("key 29 true", "key 29 false"), after.events) + bridge.applyTouch(touch, InputRole.SECONDARY) { it.press(PadState.B, false) } + bridge.applyTouch(touch, InputRole.SECONDARY) { it.press(PadState.B, true) } + assertEquals("key 29 true", after.events.last()) + } + + @Test fun replacingRouterReleasesRetainedNativeButtonsAndStick() { + bridge.setInputRouter(InputRouter(RecordingOutput())) + bridge.applyExternal({ it.press(PadState.A, true); it.leftX = .8f }, true) + bridge.setInputRouter(InputRouter(RecordingOutput())) + assertFalse(state().isDown(PadState.A)) + assertEquals(0f, state().leftX) + bridge.applyExternal({ it.press(PadState.B, true) }, true) + assertTrue(state().isDown(PadState.B)) + assertFalse(state().isDown(PadState.A)) + assertEquals(0f, state().leftX) + bridge.applyExternal({ it.press(PadState.A, false); it.leftX = 0f }, true) + bridge.applyExternal({ it.press(PadState.A, true); it.leftX = .8f }, true) + assertTrue(state().isDown(PadState.A)) + assertEquals(.8f, state().leftX) + } + + @Test fun replacingDeckRouterBlocksExistingGripAndContactUntilRelease() { + DeckControls.setRouter(InputRouter(RecordingOutput())) + val surface = DeckControls.source(InputRole.SECONDARY) + try { + surface.setGrip(DeckControls.L4, true) + surface.setPad(false, true, .3f, .4f) + DeckControls.setRouter(InputRouter(RecordingOutput())) + assertEquals(0, JoyConPadTest.RecordingWriter.grips) + surface.setPad(false, true, .5f, .6f) + assertEquals(0, JoyConPadTest.RecordingWriter.grips) + surface.setGrip(DeckControls.L4, false) + surface.setPad(false, false) + surface.setGrip(DeckControls.L4, true) + surface.setPad(false, true, .2f, .3f) + assertEquals(DeckControls.L4 or 16, JoyConPadTest.RecordingWriter.grips) + } finally { surface.close() } + } + + @Test fun clearingOldDeckOwnerPreservesReplacementBindingsAndHeldKeys() { + val previous = InputRouter(RecordingOutput()) + val output = RecordingOutput() + val replacement = InputRouter(output).also { + it.setProfile(InputProfile.defaults(InputProfile.GAME) + .withBinding(InputRole.PHYSICAL, InputControl.L4, InputAction.Keys(listOf(57)))) + } + DeckControls.setRouter(previous) + DeckControls.setRouter(replacement) + DeckControls.setGrip(DeckControls.L4, true) + DeckControls.clearRouter(previous) + DeckControls.clearRouter(null) + assertEquals(listOf("key 57 true"), output.events) + DeckControls.setGrip(DeckControls.L4, false) + DeckControls.setGrip(DeckControls.L4, true) + assertEquals(listOf("key 57 true", "key 57 false", "key 57 true"), output.events) + assertEquals(0, JoyConPadTest.RecordingWriter.grips) + } + + @Test fun clearingCurrentDeckOwnerReleasesItsKeysAndRemovesBindings() { + val output = RecordingOutput() + val router = InputRouter(output).also { + it.setProfile(InputProfile.defaults(InputProfile.GAME) + .withBinding(InputRole.PHYSICAL, InputControl.L4, InputAction.Keys(listOf(57)))) + } + DeckControls.setRouter(router) + DeckControls.setGrip(DeckControls.L4, true) + DeckControls.clearRouter(router) + DeckControls.clearRouter(router) + DeckControls.setGrip(DeckControls.L4, false) + DeckControls.setGrip(DeckControls.L4, true) + assertEquals(listOf("key 57 true", "key 57 false"), output.events) + assertEquals(DeckControls.L4, JoyConPadTest.RecordingWriter.grips) + } + + @Test fun releasingAllKeepsPhysicalGripsBlockedUntilNeutral() { + val router = InputRouter(RecordingOutput()) + bridge.setInputRouter(router) + DeckControls.setRouter(router) + DeckControls.setGrip(DeckControls.L4, true) + assertEquals(DeckControls.L4, JoyConPadTest.RecordingWriter.grips) + bridge.releaseAll() + DeckControls.setGrip(DeckControls.L4, true) + assertEquals(0, JoyConPadTest.RecordingWriter.grips) + DeckControls.setGrip(DeckControls.L4, false) + DeckControls.setGrip(DeckControls.L4, true) + assertEquals(DeckControls.L4, JoyConPadTest.RecordingWriter.grips) + } + + @Test fun cancellingHomeReleasesDeckOutputAndKeepsGripsBlockedUntilNeutral() { + val router = InputRouter(RecordingOutput()) + bridge.setInputRouter(router) + DeckControls.setRouter(router) + bridge.setHomeGuidePressed(true) + DeckControls.setGrip(DeckControls.L4, true) + assertEquals(DeckControls.L4, JoyConPadTest.RecordingWriter.grips) + bridge.cancelHomeGuide() + assertEquals(0, JoyConPadTest.RecordingWriter.grips) + DeckControls.setGrip(DeckControls.L4, true) + assertEquals(0, JoyConPadTest.RecordingWriter.grips) + DeckControls.setGrip(DeckControls.L4, false) + DeckControls.setGrip(DeckControls.L4, true) + assertEquals(DeckControls.L4, JoyConPadTest.RecordingWriter.grips) + } + + private class RecordingOutput : InputOutput { + val events = mutableListOf() + override fun key(code: Int, down: Boolean) { events += "key $code $down" } + override fun button(code: Int, down: Boolean) { events += "button $code $down" } + override fun pointer(dx: Float, dy: Float) { events += "pointer" } + override fun scroll(steps: Int) { events += "scroll" } + override fun command(command: InputCommand) { events += command.name } + override fun requestMotion() { events += "motion" } + } + + @Test fun removingLastTouchSurfaceReleasesLegacyMouseRoute() { + val routed = PadState() + bridge.setTouchRoute(object : PadBridge.TouchRoute { + override fun active() = true + override fun accept(state: PadState) { routed.copyFrom(state) } + }) + val first = Any(); val second = Any() + bridge.applyTouch(first, InputRole.PRIMARY) { it.press(PadState.A, true) } + bridge.applyTouch(second, InputRole.SECONDARY) { it.press(PadState.A, true) } + bridge.releaseTouch(first) + assertTrue(routed.isDown(PadState.A)) + bridge.releaseTouch(second) + assertFalse(routed.isDown(PadState.A)) + } + + @Test fun nativeTrackpadKeepsItsOwnerUntilThatContactEnds() { + val first = DeckControls.source(InputRole.PRIMARY) + val second = DeckControls.source(InputRole.SECONDARY) + try { + first.setPad(false, true, .25f, .5f) + val firstPosition = JoyConPadTest.RecordingWriter.deckPads.copyOf() + second.setPad(false, true, -.75f, -.5f) + assertArrayEquals(firstPosition, JoyConPadTest.RecordingWriter.deckPads) + first.setPad(false, false) + assertTrue(JoyConPadTest.RecordingWriter.deckPads[0] < 0) + second.close() + assertEquals(0, JoyConPadTest.RecordingWriter.grips and 16) + } finally { first.close(); second.close() } + } + + @Test fun closingOneSurfaceDoesNotReleaseAnotherSurfaceOrPhysicalPad() { + val a = Any(); val b = Any() + bridge.applyExternal({ it.press(PadState.A, true) }, true) + bridge.applyTouch(a, InputRole.PRIMARY) { it.press(PadState.A, true); it.press(PadState.B, true) } + bridge.applyTouch(b, InputRole.SECONDARY) { it.press(PadState.X, true) } + bridge.releaseTouch(a) + assertTrue(state().isDown(PadState.A)); assertTrue(state().isDown(PadState.X)); assertFalse(state().isDown(PadState.B)) + bridge.releaseTouch(b) + assertTrue(state().isDown(PadState.A)); assertFalse(state().isDown(PadState.X)) + } + + @Test fun disconnectingSecondaryDeckSurfaceKeepsPhysicalGripHeld() { + val surface = DeckControls.source(InputRole.SECONDARY) + DeckControls.setGrip(DeckControls.L4, true) + surface.setGrip(DeckControls.L4, true) + surface.close() + assertEquals(DeckControls.L4, JoyConPadTest.RecordingWriter.grips) + DeckControls.setGrip(DeckControls.L4, false) + assertEquals(0, JoyConPadTest.RecordingWriter.grips) + } + + @Test fun nativePadAndTouchMappingsUseDifferentRoles() { + val output = object : InputOutput { + override fun key(code: Int, down: Boolean) {} + override fun button(code: Int, down: Boolean) {} + override fun pointer(dx: Float, dy: Float) {} + override fun scroll(steps: Int) {} + override fun command(command: InputCommand) {} + override fun requestMotion() {} + } + val router = InputRouter(output) + bridge.setInputRouter(router) + bridge.setInputProfile(InputProfile.defaults(InputProfile.GAME).withBinding(InputRole.SECONDARY, InputControl.A, InputAction.Disabled)) + val touch = Any() + bridge.applyTouch(touch, InputRole.SECONDARY) { it.press(PadState.A, true) } + assertFalse(state().isDown(PadState.A)) + bridge.applyExternal({ it.press(PadState.A, true) }, true) + assertTrue(state().isDown(PadState.A)) + bridge.releaseAll() + assertFalse(state().isDown(PadState.A)) + bridge.applyExternal({ it.press(PadState.B, true) }, true) + assertFalse(state().isDown(PadState.A)) + assertTrue(state().isDown(PadState.B)) + bridge.applyExternal({ it.press(PadState.A, false) }, true) + bridge.applyExternal({ it.press(PadState.A, true) }, true) + assertTrue(state().isDown(PadState.A)) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/input/InputSurfaceSnapTest.kt b/app/src/test/java/com/droiddeck/launcher/input/InputSurfaceSnapTest.kt new file mode 100644 index 0000000000..be726ff63c --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/input/InputSurfaceSnapTest.kt @@ -0,0 +1,33 @@ +package com.droiddeck.launcher.input + +import org.junit.Assert.* +import org.junit.Test + +class InputSurfaceSnapTest { + private val element = InputSurfaceElement(InputControl.A, .395f, .3f, .2f, .2f) + @Test fun snapsToCanvasCenterWithoutChangingSize() { + val result = snapSurfaceElement(element, emptyList(), .01f, .01f) + assertEquals(.4f, result.element.x, .0001f) + assertEquals(.5f, result.x!!, 0f) + assertEquals(element.width, result.element.width, 0f) + } + @Test fun alignsEdgesWithOtherElementsAndLeavesDistantPositionsFree() { + val other = element.copy(x = .1f, y = .65f) + val aligned = snapSurfaceElement(element.copy(x = .105f), listOf(other), .01f, .01f) + assertEquals(.1f, aligned.element.x, .0001f) + val free = snapSurfaceElement(element.copy(x = .36f, y = .34f), listOf(other), .01f, .01f) + assertNull(free.x); assertNull(free.y) + } + @Test fun resizingSnapsTheDraggedEdgeAndKeepsTheOppositeEdge() { + val result = snapSurfaceElement(element.copy(x = .2f, width = .295f), emptyList(), .01f, .01f, 2 or 8) + assertEquals(.2f, result.element.x, 0f) + assertEquals(.3f, result.element.width, .0001f) + } + @Test fun boundsPreventFalseGuidesAndNegativeSize() { + val e = element.copy(x = .01f, width = .98f) + val result = snapSurfaceElement(e, listOf(element.copy(x = .995f)), .02f, .01f, 1 or 4) + assertTrue(result.element.x >= 0f) + assertTrue(result.element.x + result.element.width <= 1f) + assertTrue(result.element.width >= .03f) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/input/InputSurfaceSourceTest.kt b/app/src/test/java/com/droiddeck/launcher/input/InputSurfaceSourceTest.kt new file mode 100644 index 0000000000..593584875f --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/input/InputSurfaceSourceTest.kt @@ -0,0 +1,161 @@ +package com.droiddeck.launcher.input + +import java.io.File +import org.junit.After +import org.junit.Assert.* +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.annotation.Config + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [34], manifest = Config.NONE, shadows = [JoyConPadTest.RecordingWriter::class]) +class InputSurfaceSourceTest { + private val events = mutableListOf() + private val router = InputRouter(object : InputOutput { + override fun key(code: Int, down: Boolean) { events += "key:$code:$down" } + override fun button(code: Int, down: Boolean) { events += "button:$code:$down" } + override fun pointer(dx: Float, dy: Float) { events += "pointer" } + override fun scroll(steps: Int) { events += "scroll" } + override fun command(command: InputCommand) { events += command.name } + override fun requestMotion() {} + }) + private val bridge = PadBridge(File(RuntimeEnvironment.getApplication().cacheDir, "element-input")) + private val sources = mutableListOf() + @Before fun setup() { bridge.setInputRouter(router); DeckControls.setRouter(router) } + @After fun close() { sources.forEach { it.close() }; bridge.stop(); DeckControls.setRouter(null); DeckControls.releaseAll() } + private fun button(id: String) = InputSurfaceElement(InputControl.A, .1f, .1f, .2f, .2f, id) + private fun source(e: InputSurfaceElement) = InputSurfaceSource(bridge, InputRole.SECONDARY, e).also { sources += it } + private fun bind(vararg actions: Pair) { + bridge.setInputProfile(InputProfile("test", elements = mapOf(InputRole.SECONDARY to mapOf(*actions)))) + } + private fun state() = JoyConPadTest.RecordingWriter.pads[0] ?: PadState() + + @Test fun toggleUsesSharedOwnershipAndReleasesOnMenuAndProfileChange() { + bind("first" to InputAction.Keys(listOf(29)), "second" to InputAction.Keys(listOf(29))) + val a = source(button("first").copy(toggle = true)); val b = source(button("second")) + a.update(1f); a.update(0f) + assertTrue(a.latched) + assertEquals(listOf("key:29:true"), events) + b.update(1f) + a.update(1f); a.update(0f) + assertFalse(a.latched) + assertEquals(1, events.size) + b.update(0f) + assertEquals("key:29:false", events.last()) + a.update(1f); a.update(0f) + bridge.setInputEnabled(false) + assertFalse(a.latched) + assertEquals("key:29:false", events.last()) + bridge.setInputEnabled(true) + a.update(1f); a.update(0f) + assertEquals("key:29:true", events.last()) + bind("first" to InputAction.Keys(listOf(57))) + assertFalse(a.latched) + assertEquals("key:29:false", events.last()) + a.update(1f); a.update(0f) + assertEquals("key:57:true", events.last()) + a.release() + assertEquals("key:57:false", events.last()) + } + + @Test fun nativeToggleReleasesWithoutRepressingAcrossProfileChange() { + bind("first" to InputAction.Native(InputControl.B)) + val a = source(button("first").copy(toggle = true)) + a.update(1f); a.update(0f) + assertTrue(state().isDown(PadState.B)) + bind("first" to InputAction.Native(InputControl.X)) + assertFalse(state().isDown(PadState.B)) + a.update(1f); a.update(0f) + assertTrue(state().isDown(PadState.X)) + a.close() + assertFalse(state().isDown(PadState.X)) + } + + @Test fun twoButtonsWithTheSameShapeHaveIndependentActions() { + bind("first" to InputAction.Keys(listOf(29)), "second" to InputAction.Keys(listOf(57))) + val a = source(button("first")); val b = source(button("second")) + a.update(1f); b.update(1f); a.update(0f); b.update(0f) + assertEquals(listOf("key:29:true", "key:57:true", "key:29:false", "key:57:false"), events) + } + + @Test fun sharedLocalButtonRemainsHeldUntilBothElementsRelease() { + bind("first" to InputAction.MouseButton(272), "second" to InputAction.MouseButton(272)) + val a = source(button("first")); val b = source(button("second")) + a.update(1f); b.update(1f); a.close() + assertEquals(listOf("button:272:true"), events) + b.update(0f) + assertEquals(listOf("button:272:true", "button:272:false"), events) + a.update(1f) + assertEquals(2, events.size) + } + + @Test fun nativeTouchOutputIsNotRemappedTwiceAndKeepsOtherOwners() { + bridge.setInputProfile(InputProfile("test", bindings = mapOf(InputRole.SECONDARY to mapOf(InputControl.B to InputAction.Disabled)), + elements = mapOf(InputRole.SECONDARY to mapOf("first" to InputAction.Native(InputControl.B), "second" to InputAction.Native(InputControl.B))))) + val a = source(button("first")); val b = source(button("second")) + a.update(1f); b.update(1f) + assertTrue(state().isDown(PadState.B)) + a.close() + assertTrue(state().isDown(PadState.B)) + b.update(0f) + assertFalse(state().isDown(PadState.B)) + } + + @Test fun profileChangeReleasesOldOutputAndRequiresNeutralBeforeNewAction() { + bind("first" to InputAction.Keys(listOf(29))) + val a = source(button("first")); a.update(1f) + bind("first" to InputAction.Keys(listOf(57))) + a.update(1f) + assertEquals(listOf("key:29:true", "key:29:false"), events) + a.update(0f); a.update(1f) + assertEquals("key:57:true", events.last()) + } + + @Test fun unmappedNewButtonsDoNotInheritLegacyBindings() { + bridge.setInputProfile(InputProfile.defaults(InputProfile.DESKTOP)) + source(button("new")).update(1f) + assertTrue(events.isEmpty()) + } + + @Test fun stickClicksAreSuspendedWithTheRestOfInput() { + val e = InputSurfaceElement(InputControl.LS, .1f, .1f, .3f, .3f, "stick", InputElementKind.STICK) + bind(e.id to InputAction.Native(InputControl.RS)) + val stick = source(e) + stick.click(true) + assertTrue(state().isDown(PadState.R3)) + bridge.setInputEnabled(false) + assertFalse(state().isDown(PadState.R3)) + bridge.setInputEnabled(true); stick.click(true) + assertFalse(state().isDown(PadState.R3)) + stick.click(false); stick.click(true) + assertTrue(state().isDown(PadState.R3)) + stick.close(); stick.click(true) + assertFalse(state().isDown(PadState.R3)) + } + + @Test fun customTrackpadMovementAndClickHaveIndependentActions() { + val e = InputSurfaceElement(InputControl.LEFT_PAD, .1f, .1f, .4f, .4f, "mouse", InputElementKind.TRACKPAD) + bridge.setInputProfile(InputProfile("test", elements = mapOf(InputRole.SECONDARY to mapOf(e.id to InputAction.Pointer())), + clicks = mapOf(InputRole.SECONDARY to mapOf(e.id to InputAction.MouseButton(272))))) + val surface = source(e) + surface.update(0f, 0f, true); surface.update(.2f, .2f, true); surface.click(true) + assertEquals(listOf("pointer", "button:272:true"), events) + bridge.setInputEnabled(false) + assertEquals("button:272:false", events.last()) + bridge.setInputEnabled(true); surface.click(true) + assertEquals(3, events.size) + surface.click(false); surface.click(true); surface.close() + assertEquals(listOf("pointer", "button:272:true", "button:272:false", "button:272:true", "button:272:false"), events) + } + + @Test fun migratedTrackpadClickStillUsesTheExistingLogicalBinding() { + bridge.setInputProfile(InputProfile.defaults(InputProfile.DESKTOP)) + val e = InputSurfaceElement(InputControl.RIGHT_PAD, .1f, .1f, .4f, .4f) + val surface = source(e) + surface.click(true); surface.click(false) + assertEquals(listOf("button:272:true", "button:272:false"), events) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/input/JoyConPadTest.kt b/app/src/test/java/com/droiddeck/launcher/input/JoyConPadTest.kt index a325632132..0b8c930ed0 100644 --- a/app/src/test/java/com/droiddeck/launcher/input/JoyConPadTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/input/JoyConPadTest.kt @@ -240,16 +240,26 @@ class JoyConPadTest { @Implementation fun open(): Boolean = true @Implementation fun close() = Unit @Implementation fun destroy() = Unit - @Implementation fun writePad(state: PadState) { pads[slot] = PadState().apply { copyFrom(state) } } + @Implementation fun writePad(state: PadState) { + pads[slot] = PadState().apply { copyFrom(state) } + padWrites++ + } companion object { val pads = HashMap() + var padWrites = 0 + var deckWrites = 0 var grips = 0 + var deckPads = ShortArray(4) @JvmStatic @Implementation fun __staticInitializer__() = Unit @JvmStatic @Implementation - fun writeDeckControls(slot: Int, controls: Int, pads: ShortArray, pressure: ShortArray) { grips = controls } + fun writeDeckControls(slot: Int, controls: Int, pads: ShortArray, pressure: ShortArray) { + grips = controls + deckPads = pads.copyOf() + deckWrites++ + } } } } diff --git a/app/src/test/java/com/droiddeck/launcher/input/LegacyScreenKeyMigrationTest.kt b/app/src/test/java/com/droiddeck/launcher/input/LegacyScreenKeyMigrationTest.kt new file mode 100644 index 0000000000..6d72190ce4 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/input/LegacyScreenKeyMigrationTest.kt @@ -0,0 +1,95 @@ +package com.droiddeck.launcher.input + +import android.content.Context +import org.json.JSONObject +import org.junit.Assert.* +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.annotation.Config + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [34], manifest = Config.NONE) +class LegacyScreenKeyMigrationTest { + private val context: Context get() = RuntimeEnvironment.getApplication() + private val prefs get() = context.getSharedPreferences("input-profiles", Context.MODE_PRIVATE) + @Before fun reset() { + for (name in listOf("input-profiles", "input-surfaces", "controller")) context.getSharedPreferences(name, Context.MODE_PRIVATE).edit().clear().commit() + } + + @Test fun upgradeImportsEverySavedSetWithoutChangingExistingProfilesAndIsIdempotent() { + val original = InputLibraries.read(context).selectProfile(false, "desktop") + prefs.edit().putString("library", JSONObject(InputLibraries.encode(original)).put("version", 5).toString()).commit() + ControllerPrefs.prefs(context).edit().putString("screenKeys", "29+33:0.3,0.7,44::Find::0.6:1:1:1") + .putBoolean("screenKeysShown", false).putString("screenLayout.Editor", "28:0.8,0.6,50::::1.0:2:0:1").commit() + val migrated = InputLibraries.read(context) + assertEquals(original.profiles, migrated.profiles.take(original.profiles.size)) + assertEquals(original.selections, migrated.selections) + assertEquals(2, migrated.profiles.count { it.id.startsWith("migrated.screenkeys.") }) + val imported = migrated.profiles.first { it.name == "Screen keys (imported)" } + val layout = migrated.layout(imported.id, InputRole.PRIMARY)!! + assertEquals(InputLayoutScreen.MAIN, layout.screen) + assertTrue(layout.elements.single().toggle) + assertEquals(InputAction.Keys(listOf(29, 33)), migrated.compile(imported.id).elements[InputRole.PRIMARY]!!.values.single()) + assertFalse(ControllerPrefs.prefs(context).contains("screenKeys")) + assertTrue(prefs.contains(LegacyScreenKeyMigration.BACKUP)) + assertEquals(migrated, InputLibraries.read(context)) + assertEquals(migrated, InputLibraries.decode(InputLibraries.encode(migrated))) + } + + @Test fun conversionRetainsCoordinatesImagesOpacitySymbolsAndIndependentDiagonalKeys() { + val values = LegacyScreenKeyMigration.convert(context, + "28:0.25,0.6,50::::0.8:2:0:1;29:0.7,0.8,40::Ctrl:custom.png:0.4:3:1:1;103+30+106+0+108+0+105+0:0.5,0.5,80::::1:6:0:1", 1000, 500, 2f) + val enter = values[0].first + assertEquals(.25f, enter.x + enter.width / 2, .0001f) + assertEquals(.6f, enter.y + enter.height / 2, .0001f) + assertEquals(.1f, enter.width, .0001f) + assertEquals(.2f, enter.height, .0001f) + assertEquals(28, enter.icon) + assertEquals(InputButtonShape.CIRCLE, enter.buttonShape) + assertEquals("custom.png", values[1].first.image) + assertEquals(.4f, values[1].first.opacity) + assertTrue(values[1].first.toggle) + assertEquals(InputAction.Directions(103, 106, 108, 105, listOf(30, 0, 0, 0)), values[2].second) + assertFalse(values[2].first.floating) + } + + @Test fun invalidMigrationDoesNotEraseTheOriginalSavedLibraryOrKeys() { + val original = JSONObject(InputLibraries.encode(InputLibraries.read(context))).put("version", 5).toString() + prefs.edit().putString("library", original).commit() + ControllerPrefs.prefs(context).edit().putString("screenKeys", "28:NaN,0.5,44").commit() + InputLibraries.read(context) + assertEquals(original, prefs.getString("library", null)) + assertEquals("28:NaN,0.5,44", ControllerPrefs.prefs(context).getString("screenKeys", null)) + assertFalse(prefs.contains(LegacyScreenKeyMigration.BACKUP)) + } + + @Test fun failedScreenKeyMigrationKeepsUpgradedBindingIdsStableForEditorLookup() { + val original = InputLibraries.read(context).chooseLayout("game", InputRole.SECONDARY, InputLibraries.DECK) + val fixed = original.copy(layouts = original.layouts.map { + when (it.id) { + InputLibraries.OVERLAY -> it.copy(elements = emptyList(), template = InputLayoutTemplate.OVERLAY) + InputLibraries.DECK -> it.copy(elements = emptyList(), template = InputLayoutTemplate.DECK) + else -> it + } + }, bindings = original.bindings.map { + if (it.layoutId in setOf(InputLibraries.OVERLAY, InputLibraries.DECK)) it.copy(elements = emptyMap(), clicks = emptyMap()) else it + }) + val raw = JSONObject(InputLibraries.encode(fixed)).put("version", 2).toString() + prefs.edit().putString("library", raw).commit() + ControllerPrefs.prefs(context).edit().putString("screenKeys", "28:NaN,0.5,44").commit() + val first = InputLibraries.read(context) + val second = InputLibraries.read(context) + assertEquals(first, second) + for (role in listOf(InputRole.PRIMARY, InputRole.SECONDARY)) { + val id = first.profile("game").binding(role) + assertEquals(first.preset(id), second.preset(id)) + assertTrue(second.layout("game", role)!!.editable) + } + assertEquals(raw, prefs.getString("library", null)) + assertTrue(InputLibraries.hasPendingScreenKeyMigration(context)) + assertEquals("28:NaN,0.5,44", ControllerPrefs.prefs(context).getString("screenKeys", null)) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/input/OnScreenControlsTest.kt b/app/src/test/java/com/droiddeck/launcher/input/OnScreenControlsTest.kt index b071b04906..5f7b48f1e3 100644 --- a/app/src/test/java/com/droiddeck/launcher/input/OnScreenControlsTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/input/OnScreenControlsTest.kt @@ -1,16 +1,11 @@ package com.droiddeck.launcher.input -import kotlin.math.abs -import kotlin.math.sqrt import android.content.Context -import android.graphics.Bitmap -import android.graphics.Canvas -import android.graphics.Rect -import android.view.DisplayCutout import android.view.MotionEvent import android.view.View -import android.view.WindowInsets import android.widget.FrameLayout +import java.io.File +import org.junit.After import org.junit.Assert.* import org.junit.Before import org.junit.Test @@ -18,506 +13,172 @@ 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 = [28], manifest = Config.NONE, qualifiers = "mdpi") -@GraphicsMode(GraphicsMode.Mode.NATIVE) +@Config(sdk = [34], manifest = Config.NONE, shadows = [JoyConPadTest.RecordingWriter::class]) class OnScreenControlsTest { - private lateinit var context: Context + private val context: Context get() = RuntimeEnvironment.getApplication() + private val bridge = PadBridge(File(context.cacheDir, "surface-overlay")) private lateinit var view: OnScreenControls + private val left = InputSurfaceElement(InputControl.LS, .1f, .3f, .2f, .4f) + private val button = InputSurfaceElement(InputControl.A, .7f, .3f, .2f, .3f) private var time = 1000L - - @Before fun setUp() { - context = RuntimeEnvironment.getApplication() - context.getSharedPreferences("controller", Context.MODE_PRIVATE).edit().clear().commit() - ControllerPrefs.setLayout(context, 1200, 800, mapOf("ls" to (0.45f to 0.5f), "rs" to (0.55f to 0.5f))) - view = OnScreenControls(context, null) - view.layout(0, 0, 1200, 800) + @Before fun setup() { + view = OnScreenControls(context, bridge) + view.updateLayout(SavedInputLayout("test", "Test", listOf(left, button))) + measure(view) + } + @After fun close() { view.releaseAll(); bridge.stop() } + private fun state() = JoyConPadTest.RecordingWriter.pads[0] ?: PadState() + private fun measure(target: View) { + target.measure(View.MeasureSpec.makeMeasureSpec(1000, View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(600, View.MeasureSpec.EXACTLY)) + target.layout(0, 0, 1000, 600) } - @Test fun editorSnapsToOtherGroupCentreLines() { - editor() - val button = control("lb") - val target = control("rb") - val x = value(button, "cx") - val y = value(button, "cy") - touch(MotionEvent.ACTION_DOWN, 1 to (x to y)) - touch(MotionEvent.ACTION_MOVE, 1 to (x to y + 5f)) - assertEquals(value(target, "cy"), value(button, "cy"), 0f) - assertEquals(value(target, "cy"), nullableFloat(view, "snapY")!!, 0f) - touch(MotionEvent.ACTION_UP, 1 to (x to y + 5f)) - assertNull(nullableFloat(view, "snapY")) + @Test fun independentFingersHoldStickAndButtonAndCancelReleasesBoth() { + touch(MotionEvent.ACTION_DOWN, 3 to (200f to 300f)) + touch(MotionEvent.ACTION_MOVE, 3 to (245f to 300f)) + touch(MotionEvent.ACTION_POINTER_DOWN or (1 shl 8), 3 to (245f to 300f), 8 to (800f to 250f)) + assertTrue(state().leftX > .5f) + assertTrue(state().isDown(PadState.A)) + touch(MotionEvent.ACTION_POINTER_UP or (1 shl 8), 3 to (245f to 300f), 8 to (800f to 250f)) + assertFalse(state().isDown(PadState.A)) + assertTrue(state().leftX > .5f) + touch(MotionEvent.ACTION_CANCEL, 3 to (245f to 300f)) + assertEquals(0f, state().leftX, 0f) } - @Test fun editorSnapsToClusterCentresNotTheirOuterButtons() { - editor() - val button = control("lb") - val x = value(button, "cx") - val row = value(control("y"), "cy") + 3f - touch(MotionEvent.ACTION_DOWN, 1 to (x to value(button, "cy"))) - touch(MotionEvent.ACTION_MOVE, 1 to (x to row)) - assertEquals(row, value(button, "cy"), 0f) - assertNull(nullableFloat(view, "snapY")) - touch(MotionEvent.ACTION_MOVE, 1 to (x to value(control("x"), "cy") + 3f)) - assertEquals(value(control("x"), "cy"), value(button, "cy"), 0f) - touch(MotionEvent.ACTION_UP, 1 to (x to row)) - } - - @Test fun editorDoesNotSnapBeyondDistanceAndCanDragFreeFromGuide() { - editor() - val button = control("lb") - val x = value(button, "cx") - val y = value(button, "cy") - val otherYs = (field(view, "controls") as List<*>) - .filter { field(it!!, "group") != "lb" } - .map { value(it!!, "cy") } - val freeY = (100..700 step 31).map(Int::toFloat).first { - otherYs.minOf { targetY -> abs(it - targetY) } > 12f - } - touch(MotionEvent.ACTION_DOWN, 1 to (x to y)) - touch(MotionEvent.ACTION_MOVE, 1 to (x to freeY)) - assertEquals(freeY, value(button, "cy"), 0f) - assertNull(nullableFloat(view, "snapY")) - val target = value(control("rb"), "cy") - touch(MotionEvent.ACTION_MOVE, 1 to (x to target + 5f)) - assertEquals(target, value(button, "cy"), 0f) - touch(MotionEvent.ACTION_MOVE, 1 to (x to freeY)) - assertEquals(freeY, value(button, "cy"), 0f) - assertNull(nullableFloat(view, "snapY")) - touch(MotionEvent.ACTION_UP, 1 to (x to freeY)) - } - - @Test fun editorSnapsToScreenCentreAndCanDisableSnapping() { - editor() - val button = control("lb") - val y = value(button, "cy") - touch(MotionEvent.ACTION_DOWN, 1 to (value(button, "cx") to y)) - touch(MotionEvent.ACTION_MOVE, 1 to (605f to y)) - assertEquals(600f, value(button, "cx"), 0f) - assertEquals(600f, nullableFloat(view, "snapX")!!, 0f) - touch(MotionEvent.ACTION_UP, 1 to (605f to y)) - - ControllerPrefs.setSnap(context, false) - editor() - val unsnapped = control("lb") - val x = value(unsnapped, "cx") - val startY = value(unsnapped, "cy") - touch(MotionEvent.ACTION_DOWN, 1 to (x to startY)) - touch(MotionEvent.ACTION_MOVE, 1 to (605f to startY)) - assertEquals(605f, value(unsnapped, "cx"), 0f) - assertNull(nullableFloat(view, "snapX")) - touch(MotionEvent.ACTION_UP, 1 to (605f to startY)) - } - - @Test fun editorSnapPreferenceDefaultsOnAndCanBeChanged() { - assertTrue(ControllerPrefs.read(context).snap) - ControllerPrefs.setSnap(context, false) - assertFalse(ControllerPrefs.read(context).snap) - ControllerPrefs.resetAll(context) - assertTrue(ControllerPrefs.read(context).snap) - } - - @Test fun adaptiveStickHidesUntilTouchedAndReturnsToNeutralOnRelease() { - val left = control("ls") - assertEquals(0, pixel(value(left, "cx"), value(left, "cy"))) - assertTrue(touch(MotionEvent.ACTION_DOWN, 7 to (540f to 400f))) - assertEquals(7, field(left, "pressedBy")) - assertEquals(540f, value(left, "ax"), 0f) - assertEquals(400f, value(left, "ay"), 0f) - assertNotEquals(0, pixel(540f, 400f)) - touch(MotionEvent.ACTION_MOVE, 7 to (580f to 420f)) - assertTrue(value(left, "kx") > 0f) - assertTrue(value(left, "ky") > 0f) - touch(MotionEvent.ACTION_UP, 7 to (580f to 420f)) - assertEquals(-1, field(left, "pressedBy")) - assertEquals(0f, value(left, "kx"), 0f) - assertEquals(0f, value(left, "ky"), 0f) - assertEquals(0, pixel(540f, 400f)) - } - - @Test fun simultaneousSticksKeepTheirPointersAndReleaseIndependently() { - val left = control("ls") - val right = control("rs") - touch(MotionEvent.ACTION_DOWN, 7 to (540f to 400f)) - touch(MotionEvent.ACTION_POINTER_DOWN or (1 shl 8), 7 to (540f to 400f), 9 to (660f to 400f)) - assertEquals(7, field(left, "pressedBy")) - assertEquals(9, field(right, "pressedBy")) - touch(MotionEvent.ACTION_POINTER_DOWN or (2 shl 8), 7 to (540f to 400f), 9 to (660f to 400f), 11 to (500f to 400f)) - assertEquals(7, field(left, "pressedBy")) - touch(MotionEvent.ACTION_POINTER_UP or (2 shl 8), 7 to (540f to 400f), 9 to (660f to 400f), 11 to (500f to 400f)) - touch(MotionEvent.ACTION_MOVE, 7 to (700f to 400f), 9 to (500f to 400f)) - assertTrue(value(left, "kx") > 0f) - assertTrue(value(right, "kx") < 0f) - touch(MotionEvent.ACTION_POINTER_UP, 7 to (700f to 400f), 9 to (500f to 400f)) - assertEquals(-1, field(left, "pressedBy")) - assertEquals(9, field(right, "pressedBy")) - touch(MotionEvent.ACTION_CANCEL, 9 to (500f to 400f)) - for (stick in listOf(left, right)) { - assertEquals(-1, field(stick, "pressedBy")) - assertEquals(0f, value(stick, "kx"), 0f) - } - } - - @Test fun cancelledTouchDoesNotBecomeAStickClick() { - touch(MotionEvent.ACTION_DOWN, 1 to (540f to 400f)) - touch(MotionEvent.ACTION_CANCEL, 1 to (540f to 400f)) - touch(MotionEvent.ACTION_DOWN, 1 to (540f to 400f)) - assertEquals(false, field(control("ls"), "clicked")) - touch(MotionEvent.ACTION_UP, 1 to (540f to 400f)) - touch(MotionEvent.ACTION_DOWN, 1 to (540f to 400f)) - assertEquals(true, field(control("ls"), "clicked")) - } - - @Test fun aCancelledSecondFingerDoesNotArmAStickClick() { - touch(MotionEvent.ACTION_DOWN, 7 to (660f to 400f)) - touch(MotionEvent.ACTION_POINTER_DOWN or (1 shl 8), 7 to (660f to 400f), 9 to (540f to 400f)) - touch(MotionEvent.ACTION_POINTER_UP or (1 shl 8), 7 to (660f to 400f), 9 to (540f to 400f), flags = MotionEvent.FLAG_CANCELED) - touch(MotionEvent.ACTION_POINTER_DOWN or (1 shl 8), 7 to (660f to 400f), 9 to (540f to 400f)) - assertEquals(false, field(control("ls"), "clicked")) - touch(MotionEvent.ACTION_POINTER_UP or (1 shl 8), 7 to (660f to 400f), 9 to (540f to 400f)) - touch(MotionEvent.ACTION_POINTER_DOWN or (1 shl 8), 7 to (660f to 400f), 9 to (540f to 400f)) - assertEquals(true, field(control("ls"), "clicked")) - } - - @Test fun movingTheStickAndTouchingItAgainDoesNotClickIt() { - touch(MotionEvent.ACTION_DOWN, 1 to (540f to 400f)) - touch(MotionEvent.ACTION_MOVE, 1 to (600f to 400f)) - touch(MotionEvent.ACTION_UP, 1 to (600f to 400f)) - touch(MotionEvent.ACTION_DOWN, 1 to (540f to 400f)) - assertEquals(false, field(control("ls"), "clicked")) - } - - @Test fun liftingFarFromTheTouchPointDoesNotArmAStickClick() { - touch(MotionEvent.ACTION_DOWN, 1 to (540f to 400f)) - touch(MotionEvent.ACTION_UP, 1 to (600f to 400f)) - touch(MotionEvent.ACTION_DOWN, 1 to (540f to 400f)) - assertEquals(false, field(control("ls"), "clicked")) - } - - @Test fun aLongHoldThenAQuickTouchDoesNotClickTheStick() { - touch(MotionEvent.ACTION_DOWN, 1 to (540f to 400f)) - time += 600 - touch(MotionEvent.ACTION_UP, 1 to (540f to 400f)) - touch(MotionEvent.ACTION_DOWN, 1 to (540f to 400f)) - assertEquals(false, field(control("ls"), "clicked")) - } - - @Test fun aFastDoubleTapClicksTheStickAndCanBeHeldWhileMoving() { - touch(MotionEvent.ACTION_DOWN, 1 to (540f to 400f)) - touch(MotionEvent.ACTION_UP, 1 to (540f to 400f)) - touch(MotionEvent.ACTION_DOWN, 1 to (540f to 400f)) - touch(MotionEvent.ACTION_MOVE, 1 to (600f to 400f)) - assertEquals(true, field(control("ls"), "clicked")) - touch(MotionEvent.ACTION_UP, 1 to (600f to 400f)) - touch(MotionEvent.ACTION_DOWN, 1 to (540f to 400f)) - assertEquals(false, field(control("ls"), "clicked")) - } - - @Test fun aSlowSecondTapDoesNotClickTheStick() { - touch(MotionEvent.ACTION_DOWN, 1 to (540f to 400f)) - touch(MotionEvent.ACTION_UP, 1 to (540f to 400f)) - time += 400 - touch(MotionEvent.ACTION_DOWN, 1 to (540f to 400f)) - assertEquals(false, field(control("ls"), "clicked")) - } - - @Test fun buttonsTakePriorityOverAdaptiveRegions() { - val button = control("a") - touch(MotionEvent.ACTION_DOWN, 3 to (value(button, "cx") to value(button, "cy"))) - assertEquals(3, field(button, "pressedBy")) - assertEquals(-1, field(control("rs"), "pressedBy")) - } - - @Test fun takesTouchAtMatchesWhatATouchDoes() { - var checked = 0 - for (x in 0 until 1200 step 40) for (y in 0 until 800 step 40) { - val expected = view.takesTouchAt(x.toFloat(), y.toFloat()) - assertEquals("($x, $y)", expected, touch(MotionEvent.ACTION_DOWN, 0 to (x.toFloat() to y.toFloat()))) - touch(MotionEvent.ACTION_UP, 0 to (x.toFloat() to y.toFloat())) - if (expected) checked++ - } - assertTrue("some points land on controls", checked > 0) - } - - @Test fun fixedSticksAndEditorRemainVisibleAtSavedPositions() { - ControllerPrefs.setAdaptiveSticks(context, false) + @Test fun reloadCancelsHeldViewsSoTheNextPressWorks() { + touch(MotionEvent.ACTION_DOWN, 0 to (800f to 250f)) + assertTrue(state().isDown(PadState.A)) view.reload() - val left = control("ls") - val x = value(left, "cx") - val y = value(left, "cy") - assertNotEquals(0, pixel(x, y)) - assertFalse(touch(MotionEvent.ACTION_DOWN, 1 to (0f to y))) - assertFalse(touch(MotionEvent.ACTION_DOWN, 1 to (600f to 200f))) - touch(MotionEvent.ACTION_DOWN, 1 to (x + 10f to y)) - assertEquals(x, value(left, "ax"), 0f) - assertEquals(10f, value(left, "kx"), 0f) - ControllerPrefs.setAdaptiveSticks(context, true) - view = OnScreenControls(context, null, editing = true) - view.layout(0, 0, 1200, 800) - assertNotEquals(0, pixel(value(control("ls"), "cx"), value(control("ls"), "cy"))) + assertFalse(state().isDown(PadState.A)) + touch(MotionEvent.ACTION_DOWN, 0 to (800f to 250f)) + assertTrue(state().isDown(PadState.A)) } - @Test fun buttonsOnlyAndReloadReleaseAdaptiveSticks() { - touch(MotionEvent.ACTION_DOWN, 1 to (540f to 400f)) - touch(MotionEvent.ACTION_MOVE, 1 to (570f to 400f)) - view.reload() - assertEquals(-1, field(control("ls"), "pressedBy")) - assertEquals(0f, value(control("ls"), "kx"), 0f) - view.setButtonsOnly(true) - assertFalse(touch(MotionEvent.ACTION_DOWN, 1 to (600f to 200f))) + @Test fun replacingTheLayoutReleasesOldSourcesAndHitRegions() { + touch(MotionEvent.ACTION_DOWN, 0 to (800f to 250f)) + view.updateLayout(SavedInputLayout("empty", "Empty")) + measure(view) + assertFalse(state().isDown(PadState.A)) + assertFalse(view.takesTouchAt(800f, 250f)) } - @Test fun adaptiveActivationKeepsItsVerticalAndInwardLimits() { - val left = control("ls") - val x = value(left, "cx") - val y = value(left, "cy") - val reach = activationReach(left) - assertFalse(touch(MotionEvent.ACTION_DOWN, 1 to (x to y - reach - 1f))) - assertTrue(touch(MotionEvent.ACTION_DOWN, 1 to (x to y - reach + 1f))) - assertEquals(y - reach + 1f, value(left, "ay"), 0.001f) - touch(MotionEvent.ACTION_UP, 1 to (x to y - reach + 1f)) - assertFalse(touch(MotionEvent.ACTION_DOWN, 1 to (x + reach to y - reach))) - assertFalse(touch(MotionEvent.ACTION_DOWN, 1 to (600f to 200f))) - } - - @Test fun leftAdaptiveStickCanStartAtTheLeftEdgeAndReleaseNormally() { - val left = control("ls") - val y = value(left, "cy") - val reach = activationReach(left) - assertFalse(touch(MotionEvent.ACTION_DOWN, 1 to (0f to y - reach - 1f))) - assertTrue(touch(MotionEvent.ACTION_DOWN, 1 to (0f to y))) - assertEquals(1, field(left, "pressedBy")) - assertEquals(0f, value(left, "ax"), 0f) - assertEquals(0f, value(left, "kx"), 0f) - touch(MotionEvent.ACTION_MOVE, 1 to (30f to y)) - assertEquals(30f, value(left, "kx"), 0f) - touch(MotionEvent.ACTION_CANCEL, 1 to (30f to y)) - assertEquals(-1, field(left, "pressedBy")) - assertEquals(0f, value(left, "kx"), 0f) - assertEquals(-1, field(control("rs"), "pressedBy")) - } - - @Test fun rightAdaptiveStickKeepsItsSavedActivationCircle() { - val right = control("rs") - val x = value(right, "cx") - val y = value(right, "cy") - val reach = activationReach(right) - assertFalse(touch(MotionEvent.ACTION_DOWN, 1 to (1199f to y))) - assertFalse(touch(MotionEvent.ACTION_DOWN, 1 to (x + reach + 1f to y))) - assertTrue(touch(MotionEvent.ACTION_DOWN, 1 to (x + reach - 1f to y))) - assertEquals(1, field(right, "pressedBy")) - } - - @Test - @Config(sdk = [30]) - @Suppress("DEPRECATION") - fun leftActivationStopsAtTheSafeCutoutEdge() { - val insets = WindowInsets.Builder() - .setDisplayCutout(DisplayCutout(Rect(24, 0, 0, 0), listOf(Rect(0, 380, 24, 420)))) - .build() - view.dispatchApplyWindowInsets(insets) - val left = control("ls") - val y = value(left, "cy") - assertFalse(touch(MotionEvent.ACTION_DOWN, 1 to (23f to y))) - assertTrue(touch(MotionEvent.ACTION_DOWN, 1 to (24f to y))) - assertEquals(1, field(left, "pressedBy")) - } - - @Test fun touchesOutsideActivationAreasStayWithTrackpad() { + @Test fun emptySpaceDoesNotConsumeTheUnderlyingTrackpadStream() { val received = mutableListOf() - val trackpad = View(context).apply { - setOnTouchListener { _, event -> received.add(event.actionMasked); true } - } val root = FrameLayout(context).apply { - addView(trackpad, FrameLayout.LayoutParams(1200, 800)) - addView(view, FrameLayout.LayoutParams(1200, 800)) - measure(View.MeasureSpec.makeMeasureSpec(1200, View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(800, View.MeasureSpec.EXACTLY)) - layout(0, 0, 1200, 800) + addView(View(context).apply { setOnTouchListener { _, e -> received += e.actionMasked; true } }, FrameLayout.LayoutParams(-1, -1)) + addView(view, FrameLayout.LayoutParams(-1, -1)) } - for ((action, position) in listOf( - MotionEvent.ACTION_DOWN to (600f to 200f), - MotionEvent.ACTION_MOVE to (540f to 400f), - MotionEvent.ACTION_UP to (540f to 400f), - )) { - val event = MotionEvent.obtain(1000L, time++, action, position.first, position.second, 0) + measure(root) + for (action in listOf(MotionEvent.ACTION_DOWN, MotionEvent.ACTION_MOVE, MotionEvent.ACTION_UP)) { + val event = MotionEvent.obtain(1000, time++, action, 500f, 100f, 0) try { assertTrue(root.dispatchTouchEvent(event)) } finally { event.recycle() } } assertEquals(listOf(MotionEvent.ACTION_DOWN, MotionEvent.ACTION_MOVE, MotionEvent.ACTION_UP), received) - assertEquals(-1, field(control("ls"), "pressedBy")) } - @Test fun trackpadFingerKeepsItsOwnStreamWhileAStickIsHeld() { - val forwarded = mutableListOf>() - view.onTouchMiss = { forwarded += Triple(it.actionMasked, it.getPointerId(0), it.getX(0)) } - touch(MotionEvent.ACTION_DOWN, 7 to (540f to 400f)) - assertTrue(touch(MotionEvent.ACTION_POINTER_DOWN or (1 shl 8), 7 to (540f to 400f), 9 to (600f to 200f))) - touch(MotionEvent.ACTION_MOVE, 7 to (500f to 400f), 9 to (650f to 200f)) - touch(MotionEvent.ACTION_POINTER_UP or (1 shl 8), 7 to (500f to 400f), 9 to (650f to 200f)) - assertEquals(listOf( - Triple(MotionEvent.ACTION_DOWN, 9, 600f), - Triple(MotionEvent.ACTION_MOVE, 9, 650f), - Triple(MotionEvent.ACTION_UP, 9, 650f), - ), forwarded) - assertEquals(7, field(control("ls"), "pressedBy")) - assertTrue(value(control("ls"), "kx") < 0f) + @Test fun backgroundFingerIsForwardedWhileAStickIsHeld() { + val received = mutableListOf>() + view.onTouchMiss = { received += it.actionMasked to it.getPointerId(0) } + touch(MotionEvent.ACTION_DOWN, 3 to (200f to 300f)) + touch(MotionEvent.ACTION_POINTER_DOWN or (1 shl 8), 3 to (200f to 300f), 8 to (500f to 100f)) + touch(MotionEvent.ACTION_MOVE, 3 to (245f to 300f), 8 to (520f to 100f)) + touch(MotionEvent.ACTION_POINTER_UP or (1 shl 8), 3 to (245f to 300f), 8 to (520f to 100f)) + assertEquals(listOf(MotionEvent.ACTION_DOWN to 8, MotionEvent.ACTION_MOVE to 8, MotionEvent.ACTION_UP to 8), received) + assertTrue(state().leftX > .5f) } - @Test fun stickPressedAfterTrackpadFingerReachesTheControls() { - val received = mutableListOf>() - val trackpad = View(context).apply { - setOnTouchListener { _, event -> received += Triple(event.actionMasked, event.pointerCount, event.getPointerId(0)); true } - } - val root = FrameLayout(context).apply { - isMotionEventSplittingEnabled = true - addView(trackpad, FrameLayout.LayoutParams(1200, 800)) - addView(view, FrameLayout.LayoutParams(1200, 800)) - measure(View.MeasureSpec.makeMeasureSpec(1200, View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(800, View.MeasureSpec.EXACTLY)) - layout(0, 0, 1200, 800) - } - fun dispatch(action: Int, vararg pointers: Pair>) { - val properties = pointers.map { (id, _) -> MotionEvent.PointerProperties().apply { this.id = id; toolType = MotionEvent.TOOL_TYPE_FINGER } }.toTypedArray() - val coords = pointers.map { (_, position) -> MotionEvent.PointerCoords().apply { x = position.first; y = position.second; pressure = 1f; size = 1f } }.toTypedArray() - val event = MotionEvent.obtain(1000L, time++, action, pointers.size, properties, coords, 0, 0, 1f, 1f, 0, 0, 0, 0) - try { assertTrue(root.dispatchTouchEvent(event)) } finally { event.recycle() } - } - dispatch(MotionEvent.ACTION_DOWN, 0 to (600f to 200f)) - dispatch(MotionEvent.ACTION_POINTER_DOWN or (1 shl 8), 0 to (600f to 200f), 1 to (540f to 400f)) - dispatch(MotionEvent.ACTION_MOVE, 0 to (640f to 220f), 1 to (500f to 400f)) - assertEquals(1, field(control("ls"), "pressedBy")) - assertTrue(value(control("ls"), "kx") < 0f) - dispatch(MotionEvent.ACTION_POINTER_UP or (1 shl 8), 0 to (640f to 220f), 1 to (500f to 400f)) - dispatch(MotionEvent.ACTION_UP, 0 to (640f to 220f)) - assertEquals(-1, field(control("ls"), "pressedBy")) - assertEquals(Triple(MotionEvent.ACTION_DOWN, 1, 0), received.first()) - assertEquals(Triple(MotionEvent.ACTION_UP, 1, 0), received.last()) - assertTrue(received.all { it.second == 1 && it.third == 0 }) + @Test fun aBackgroundStreamDoesNotTurnIntoAButtonWhenItCrossesOne() { + val received = mutableListOf() + view.onTouchMiss = { received += it.actionMasked } + touch(MotionEvent.ACTION_DOWN, 3 to (500f to 100f)) + touch(MotionEvent.ACTION_MOVE, 3 to (800f to 250f)) + touch(MotionEvent.ACTION_UP, 3 to (800f to 250f)) + assertFalse(state().isDown(PadState.A)) + assertEquals(listOf(MotionEvent.ACTION_DOWN, MotionEvent.ACTION_MOVE, MotionEvent.ACTION_UP), received) } - @Test fun adaptiveActivationLeavesFivePixelsAroundButtonHitTargets() { - for (id in listOf("a", "rb")) { - val button = control(id) - val radius = value(button, "radius") - val edge = value(button, "cy") - radius * if (id == "rb") 1.05f else 1.25f - val x = value(button, "cx") - val left = control("ls") - moveControl(left, x, edge - 10f) - assertFalse(touch(MotionEvent.ACTION_DOWN, 1 to (x to edge - 4f))) - assertEquals(-1, field(left, "pressedBy")) - assertTrue(touch(MotionEvent.ACTION_DOWN, 1 to (x to edge - 6f))) - assertEquals(1, field(left, "pressedBy")) - touch(MotionEvent.ACTION_UP, 1 to (x to edge - 6f)) + @Test fun floatingAndFixedSticksUseTheirConfiguredOrigin() { + for (floating in listOf(true, false)) { + view.updateLayout(SavedInputLayout("test", "Test", listOf(left.copy(floating = floating)))) + measure(view) + touch(MotionEvent.ACTION_DOWN, 3 to (230f to 300f)) + assertEquals(if (floating) 0f else .5f, state().leftX, .001f) + touch(MotionEvent.ACTION_UP, 3 to (230f to 300f)) } } - @Test fun leftExtensionKeepsButtonPriorityAndClearance() { - val button = control("left") - val x = value(button, "cx") - val y = value(button, "cy") - val edge = y - value(button, "radius") * 1.25f - val left = control("ls") - moveControl(left, x + 200f, y) - assertTrue(touch(MotionEvent.ACTION_DOWN, 1 to (x to y))) - assertEquals(1, field(button, "pressedBy")) - assertEquals(-1, field(left, "pressedBy")) - touch(MotionEvent.ACTION_UP, 1 to (x to y)) - assertFalse(touch(MotionEvent.ACTION_DOWN, 1 to (x to edge - 4f))) - assertTrue(touch(MotionEvent.ACTION_DOWN, 1 to (x to edge - 6f))) - assertEquals(1, field(left, "pressedBy")) - } - - private fun control(id: String): Any = (field(view, "controls") as List<*>).first { field(it!!, "id") == id }!! - private fun editor() { - view = OnScreenControls(context, null, editing = true) - view.layout(0, 0, 1200, 800) - } - - @Test fun keyboardShortcutWorksWithHiddenPadAndButtonsOnly() { - var opened = 0 - view = OnScreenControls(context, null, onKeyboard = { opened++ }) - view.layout(0, 0, 1200, 800) - view.setQuickHidden(true) - assertNotEquals(0, pixel(90f, 766f)) - touch(MotionEvent.ACTION_DOWN, 1 to (90f to 766f)) - assertEquals(0, opened) - touch(MotionEvent.ACTION_UP, 1 to (90f to 766f)) - assertEquals(1, opened) - assertFalse(touch(MotionEvent.ACTION_DOWN, 1 to (600f to 400f))) + @Test fun systemOnlyVisibilityUsesTheSameLayoutAndReleasesOtherButtons() { + val guide = InputSurfaceElement(InputControl.GUIDE, .4f, .7f, .1f, .2f) + view.updateLayout(SavedInputLayout("test", "Test", listOf(button, guide))) + measure(view) + touch(MotionEvent.ACTION_DOWN, 0 to (800f to 250f)) view.setButtonsOnly(true) - touch(MotionEvent.ACTION_DOWN, 1 to (600f to 756f)) - touch(MotionEvent.ACTION_UP, 1 to (600f to 756f)) - assertEquals(2, opened) - assertEquals(-1, field(control("guide"), "pressedBy")) + measure(view) + assertFalse(state().isDown(PadState.A)) + assertFalse(view.takesTouchAt(800f, 250f)) + assertTrue(view.takesTouchAt(450f, 480f)) } - @Test fun cancelledDraggedOrDisabledKeyboardShortcutDoesNotOpen() { - var opened = 0 - view = OnScreenControls(context, null, onKeyboard = { opened++ }) - view.layout(0, 0, 1200, 800) - touch(MotionEvent.ACTION_DOWN, 1 to (90f to 766f)) - touch(MotionEvent.ACTION_CANCEL, 1 to (90f to 766f)) - touch(MotionEvent.ACTION_UP, 1 to (90f to 766f)) - assertEquals(0, opened) - touch(MotionEvent.ACTION_DOWN, 1 to (90f to 766f)) - touch(MotionEvent.ACTION_MOVE, 1 to (180f to 766f)) - touch(MotionEvent.ACTION_UP, 1 to (180f to 766f)) - assertEquals(0, opened) - ControllerPrefs.setKeyboardButton(context, false) - view.reload() - view.setQuickHidden(true) - assertEquals(0, pixel(90f, 766f)) - assertFalse(touch(MotionEvent.ACTION_DOWN, 1 to (90f to 766f))) + @Test fun aCancelledSecondFingerDoesNotArmAStickClick() { + touch(MotionEvent.ACTION_DOWN, 7 to (800f to 250f)) + touch(MotionEvent.ACTION_POINTER_DOWN or (1 shl 8), 7 to (800f to 250f), 9 to (200f to 300f)) + touch(MotionEvent.ACTION_POINTER_UP or (1 shl 8), 7 to (800f to 250f), 9 to (200f to 300f), flags = MotionEvent.FLAG_CANCELED) + touch(MotionEvent.ACTION_POINTER_DOWN or (1 shl 8), 7 to (800f to 250f), 9 to (200f to 300f)) + assertFalse(state().isDown(PadState.L3)) + touch(MotionEvent.ACTION_POINTER_UP or (1 shl 8), 7 to (800f to 250f), 9 to (200f to 300f)) + touch(MotionEvent.ACTION_POINTER_DOWN or (1 shl 8), 7 to (800f to 250f), 9 to (200f to 300f)) + assertTrue(state().isDown(PadState.L3)) } - @Test fun hidingSystemButtonsRemovesHitTargetsInBothOverlayModes() { - for (buttonsOnly in listOf(false, true)) { - ControllerPrefs.setSteamButton(context, true) - ControllerPrefs.setQamButton(context, true) - view.reload() - view.setButtonsOnly(buttonsOnly) - val guide = control("guide") - val qam = control("qam") - val gx = value(guide, "cx"); val gy = value(guide, "cy") - val qx = value(qam, "cx"); val qy = value(qam, "cy") - assertNotEquals(0, pixel(gx, gy)) - assertNotEquals(0, pixel(qx, qy)) - touch(MotionEvent.ACTION_DOWN, 1 to (gx to gy)) - ControllerPrefs.setSteamButton(context, false) - view.reload() - assertEquals(-1, field(guide, "pressedBy")) - assertEquals(0, pixel(gx, gy)) - assertFalse(touch(MotionEvent.ACTION_DOWN, 1 to (gx to gy))) - assertTrue(touch(MotionEvent.ACTION_DOWN, 1 to (qx to qy))) - touch(MotionEvent.ACTION_UP, 1 to (qx to qy)) - ControllerPrefs.setQamButton(context, false) - view.reload() - assertEquals(0, pixel(qx, qy)) - assertFalse(touch(MotionEvent.ACTION_DOWN, 1 to (qx to qy))) - } - } - private fun field(owner: Any, name: String): Any = owner.javaClass.getDeclaredField(name).apply { isAccessible = true }.get(owner)!! - private fun nullableFloat(owner: Any, name: String) = owner.javaClass.getDeclaredField(name).apply { isAccessible = true }.get(owner) as Float? - private fun value(owner: Any, name: String) = field(owner, name) as Float - - private fun activationReach(control: Any) = value(control, "radius") * 1.4f * sqrt(1.5f) - - private fun moveControl(control: Any, x: Float, y: Float) { - control.javaClass.getDeclaredField("cx").apply { isAccessible = true }.setFloat(control, x) - control.javaClass.getDeclaredField("cy").apply { isAccessible = true }.setFloat(control, y) + @Test fun movingTheStickAndTouchingItAgainDoesNotClickIt() { + touch(MotionEvent.ACTION_DOWN, 1 to (200f to 300f)) + touch(MotionEvent.ACTION_MOVE, 1 to (250f to 300f)) + touch(MotionEvent.ACTION_UP, 1 to (250f to 300f)) + touch(MotionEvent.ACTION_DOWN, 1 to (200f to 300f)) + assertFalse(state().isDown(PadState.L3)) } - private fun pixel(x: Float, y: Float): Int { - val bitmap = Bitmap.createBitmap(view.width, view.height, Bitmap.Config.ARGB_8888) - view.draw(Canvas(bitmap)) - val color = bitmap.getPixel(x.toInt(), y.toInt()) - bitmap.recycle() - return color + @Test fun liftingFarFromTheTouchPointDoesNotArmAStickClick() { + touch(MotionEvent.ACTION_DOWN, 1 to (200f to 300f)) + touch(MotionEvent.ACTION_UP, 1 to (250f to 300f)) + touch(MotionEvent.ACTION_DOWN, 1 to (200f to 300f)) + assertFalse(state().isDown(PadState.L3)) + } + + @Test fun aLongHoldThenAQuickTouchDoesNotClickTheStick() { + touch(MotionEvent.ACTION_DOWN, 1 to (200f to 300f)) + time += 600 + touch(MotionEvent.ACTION_UP, 1 to (200f to 300f)) + touch(MotionEvent.ACTION_DOWN, 1 to (200f to 300f)) + assertFalse(state().isDown(PadState.L3)) + } + + @Test fun aFastDoubleTapClicksTheStickAndCanBeHeldWhileMoving() { + touch(MotionEvent.ACTION_DOWN, 1 to (200f to 300f)) + touch(MotionEvent.ACTION_UP, 1 to (200f to 300f)) + touch(MotionEvent.ACTION_DOWN, 1 to (200f to 300f)) + touch(MotionEvent.ACTION_MOVE, 1 to (250f to 300f)) + assertTrue(state().isDown(PadState.L3)) + touch(MotionEvent.ACTION_UP, 1 to (250f to 300f)) + touch(MotionEvent.ACTION_DOWN, 1 to (200f to 300f)) + assertFalse(state().isDown(PadState.L3)) + } + + @Test fun aSlowSecondTapDoesNotClickTheStick() { + touch(MotionEvent.ACTION_DOWN, 1 to (200f to 300f)) + touch(MotionEvent.ACTION_UP, 1 to (200f to 300f)) + time += 400 + touch(MotionEvent.ACTION_DOWN, 1 to (200f to 300f)) + assertFalse(state().isDown(PadState.L3)) } private fun touch(action: Int, vararg pointers: Pair>, flags: Int = 0): Boolean { val properties = pointers.map { (id, _) -> MotionEvent.PointerProperties().apply { this.id = id; toolType = MotionEvent.TOOL_TYPE_FINGER } }.toTypedArray() val coords = pointers.map { (_, position) -> MotionEvent.PointerCoords().apply { x = position.first; y = position.second; pressure = 1f; size = 1f } }.toTypedArray() val event = MotionEvent.obtain(1000L, time++, action, pointers.size, properties, coords, 0, 0, 1f, 1f, 0, 0, 0, flags) - return try { view.onTouchEvent(event) } finally { event.recycle() } + return try { view.dispatchTouchEvent(event) } finally { event.recycle() } } } diff --git a/app/src/test/java/com/droiddeck/launcher/input/PadBridgeQamTest.kt b/app/src/test/java/com/droiddeck/launcher/input/PadBridgeQamTest.kt index 3795c25e0a..a0c65427f5 100644 --- a/app/src/test/java/com/droiddeck/launcher/input/PadBridgeQamTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/input/PadBridgeQamTest.kt @@ -375,6 +375,37 @@ class PadBridgeQamTest { } } + @Test fun playerSlotHomeGuidePreservesRemappedInputAndTheEnableGate() { + PadBridge.setFirstSlot(1) + try { + bridge.setBuiltinPad(false) + routed(InputProfile.defaults(InputProfile.GAME) + .withBinding(InputRole.PHYSICAL, InputControl.A, InputAction.Native(InputControl.B)) + .withBinding(InputRole.PHYSICAL, InputControl.LS, InputAction.Native(InputControl.RS))) + val device = controller() + bridge.onKeyEvent(key(device, KeyEvent.KEYCODE_BUTTON_A)) + motion(device, MotionEvent.AXIS_X, .6f) + bridge.setHomeGuidePressed(true) + val held = requireNotNull(JoyConPadTest.RecordingWriter.pads[1]) + assertTrue(held.isDown(PadState.GUIDE)) + assertTrue(held.isDown(PadState.B)) + assertFalse(held.isDown(PadState.A)) + assertEquals(0f, held.leftX) + assertEquals(.6f, held.rightX) + bridge.setInputEnabled(false) + val disabled = requireNotNull(JoyConPadTest.RecordingWriter.pads[1]) + assertTrue(disabled.isDown(PadState.GUIDE)) + assertFalse(disabled.isDown(PadState.A)) + assertFalse(disabled.isDown(PadState.B)) + assertEquals(0f, disabled.leftX) + assertEquals(0f, disabled.rightX) + bridge.cancelHomeGuide() + assertFalse(requireNotNull(JoyConPadTest.RecordingWriter.pads[1]).isDown(PadState.GUIDE)) + } finally { + PadBridge.setFirstSlot(0) + } + } + @Test fun removingThePrimaryPlayerForgetsItsHomeGuide() { PadBridge.setFirstSlot(1) try { @@ -440,6 +471,117 @@ class PadBridgeQamTest { assertFalse(pad().isDown(PadState.GUIDE)) } + @Test fun ordinaryRoutedInputDoesNotWaitForTheChordQueue() { + routed(InputProfile.defaults(InputProfile.GAME)) + bridge.applyExternal({ it.press(PadState.A, true); it.leftX = .6f }, true) + assertTrue(pad().isDown(PadState.A)) + assertEquals(.6f, pad().leftX) + bridge.applyExternal({ it.press(PadState.A, false); it.leftX = 0f }, true) + assertFalse(pad().isDown(PadState.A)) + assertEquals(0f, pad().leftX) + } + + @Test fun routedQamDuringAnActiveChordPublishesOtherInputImmediately() { + ReflectionHelpers.setStaticField(FakeInputWriter::class.java, "padType", FakeInputWriter.PAD_TYPE_XBOX) + routed(InputProfile.defaults(InputProfile.GAME) + .withBinding(InputRole.PHYSICAL, InputControl.X, InputAction.Native(InputControl.QAM))) + bridge.triggerQam() + assertTrue(pad().isDown(PadState.GUIDE)) + bridge.applyExternal({ it.press(PadState.X, true); it.press(PadState.B, true); it.leftX = .6f }, true) + assertTrue(pad().isDown(PadState.GUIDE)) + assertTrue(pad().isDown(PadState.B)) + assertEquals(.6f, pad().leftX) + assertFalse(pad().isDown(PadState.QAM)) + bridge.applyExternal({ it.press(PadState.X, false); it.press(PadState.B, false); it.leftX = 0f }, true) + assertFalse(pad().isDown(PadState.B)) + assertEquals(0f, pad().leftX) + advanceTime() + assertFalse(pad().isDown(PadState.GUIDE)) + assertFalse(pad().isDown(PadState.A)) + } + + @Test fun queuedChordsKeepRoutedAndTouchAxesInsteadOfRawPhysicalAxes() { + routed(InputProfile.defaults(InputProfile.GAME) + .withBinding(InputRole.PHYSICAL, InputControl.LS, InputAction.Native(InputControl.RS))) + bridge.setBackQamPressed(true) + bridge.applyExternal({ it.leftY = -.8f }, true) + val touch = Any() + bridge.applyTouch(touch, InputRole.SECONDARY) { it.leftX = .5f } + advance(20) + bridge.applyExternal({ it.leftY = -.25f }, true) + advance(60) + assertEquals(.5f, pad().leftX) + assertEquals(0f, pad().leftY) + assertEquals(-.25f, pad().rightY) + } + + @Test fun queuedChordsDoNotLeakDesktopAxesToSteam() { + routed(InputProfile.defaults(InputProfile.DESKTOP)) + bridge.setHomeGuidePressed(true) + bridge.applyExternal({ it.leftY = -1f; it.rightY = .8f }, true) + advance(80) + assertEquals(0f, pad().leftY) + assertEquals(0f, pad().rightY) + } + + @Test fun disablingRoutingDiscardsQueuedNativeButtons() { + routed(InputProfile.defaults(InputProfile.GAME)) + bridge.setBackQamPressed(true) + bridge.applyExternal({ it.press(PadState.A, true) }, true) + advance(20) + bridge.setInputEnabled(false) + advance(60) + assertFalse(pad().isDown(PadState.A)) + bridge.setInputEnabled(true) + assertFalse(pad().isDown(PadState.A)) + } + + @Test fun changingProfileDiscardsQueuedNativeButtons() { + routed(InputProfile.defaults(InputProfile.GAME)) + bridge.setHomeGuidePressed(true) + bridge.applyExternal({ it.press(PadState.A, true) }, true) + advance(20) + bridge.setInputProfile(InputProfile.defaults(InputProfile.DESKTOP)) + advance(60) + assertFalse(pad().isDown(PadState.A)) + } + + @Test fun cancellingHomeKeepsHeldInputBlockedUntilNeutral() { + routed(InputProfile.defaults(InputProfile.GAME)) + bridge.setHomeGuidePressed(true) + bridge.applyExternal({ it.leftY = -1f }, true) + bridge.cancelHomeGuide() + bridge.applyExternal({ it.leftY = -.9f }, true) + assertEquals(0f, pad().leftY) + bridge.applyExternal({ it.leftY = 0f }, true) + bridge.applyExternal({ it.leftY = -.9f }, true) + assertEquals(-.9f, pad().leftY) + } + + @Test fun cancellingBackKeepsHeldInputBlockedUntilNeutral() { + routed(InputProfile.defaults(InputProfile.GAME)) + bridge.setBackQamPressed(true) + bridge.applyExternal({ it.leftY = -1f }, true) + bridge.cancelBackQam() + bridge.applyExternal({ it.leftY = -.9f }, true) + assertEquals(0f, pad().leftY) + bridge.applyExternal({ it.leftY = 0f }, true) + bridge.applyExternal({ it.leftY = -.9f }, true) + assertEquals(-.9f, pad().leftY) + } + + private fun routed(profile: InputProfile) { + bridge.setInputRouter(InputRouter(object : InputOutput { + override fun key(code: Int, down: Boolean) {} + override fun button(code: Int, down: Boolean) {} + override fun pointer(dx: Float, dy: Float) {} + override fun scroll(steps: Int) {} + override fun command(command: InputCommand) {} + override fun requestMotion() {} + })) + bridge.setInputProfile(profile) + } + @Test fun zeroDeadZoneDriftDoesNotKeepAxesBlockedAfterBackRelease() { val device = controller() bridge.setStickDeadZone(0) diff --git a/app/src/test/java/com/droiddeck/launcher/input/PhysicalInputBindingsTest.kt b/app/src/test/java/com/droiddeck/launcher/input/PhysicalInputBindingsTest.kt new file mode 100644 index 0000000000..71a0985787 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/input/PhysicalInputBindingsTest.kt @@ -0,0 +1,70 @@ +package com.droiddeck.launcher.input + +import android.hardware.input.InputManager +import android.view.InputDevice +import android.view.KeyEvent +import org.junit.Assert.* +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.Shadows.shadowOf +import org.robolectric.annotation.Config +import org.robolectric.shadows.InputDeviceBuilder + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [34], manifest = Config.NONE, shadows = [PadBridgeRoutingTest.RegisteredKeyEvent::class]) +class PhysicalInputBindingsTest { + private val events = ArrayList() + private val router = InputRouter(object : InputOutput { + override fun key(code: Int, down: Boolean) { events += "$code $down" } + override fun button(code: Int, down: Boolean) {} + override fun pointer(dx: Float, dy: Float) {} + override fun scroll(steps: Int) {} + override fun command(command: InputCommand) { events += command.name } + override fun requestMotion() {} + }) + private val bindings = PhysicalInputBindings(router) + + private fun device(id: Int): InputDevice { + val device = InputDeviceBuilder.newBuilder().setId(id).setName("Thor extra keys").setDescriptor("keys-$id") + .setSources(InputDevice.SOURCE_KEYBOARD).setKeyboardType(InputDevice.KEYBOARD_TYPE_NON_ALPHABETIC).build() + val manager = RuntimeEnvironment.getApplication().getSystemService(InputManager::class.java) + shadowOf(manager).addInputDevice(device) + return device + } + private fun event(id: Int, down: Boolean, code: Int = KeyEvent.KEYCODE_F1, repeat: Int = 0) = + KeyEvent(0L, 0L, if (down) KeyEvent.ACTION_DOWN else KeyEvent.ACTION_UP, code, repeat, 0, id, 59, 0, InputDevice.SOURCE_KEYBOARD) + + @Test fun captureReservesSystemKeysButAcceptsKeyboardBackButtons() { + val device = device(40) + assertNotNull(PhysicalInputKey.capture(event(device.id, true))) + assertNull(PhysicalInputKey.capture(event(device.id, true, KeyEvent.KEYCODE_VOLUME_UP))) + assertNull(PhysicalInputKey.capture(event(device.id, true, KeyEvent.KEYCODE_HOME))) + } + + @Test fun bindingMatchesOnlyItsDeviceAndReleasesAfterEditingProfile() { + val device = device(40); val other = device(41) + val key = PhysicalInputKey.capture(event(device.id, true))!! + bindings.configure(listOf(PhysicalInputBinding(key, InputAction.Keys(listOf(29, 30))))) + assertFalse(bindings.onKey(event(other.id, true), true)) + assertTrue(bindings.onKey(event(device.id, true), true)) + bindings.configure(listOf(PhysicalInputBinding(key, InputAction.Keys(listOf(28))))) + assertTrue(bindings.onKey(event(device.id, true, repeat = 1), true)) + assertTrue(bindings.onKey(event(device.id, false), true)) + assertEquals(listOf("29 true", "30 true", "30 false", "29 false"), events) + bindings.onKey(event(device.id, true), true) + assertEquals("28 true", events.last()) + } + + @Test fun releasesRemainConsumedAfterOpeningMenuAndVirtualKeysAreUntouched() { + val device = device(40) + val key = PhysicalInputKey.capture(event(device.id, true))!! + bindings.configure(listOf(PhysicalInputBinding(key, InputAction.Keys(listOf(57))))) + assertFalse(bindings.onKey(event(-1, true), true)) + bindings.onKey(event(device.id, true), true) + bindings.release() + assertTrue(bindings.onKey(event(device.id, false), false)) + assertEquals(listOf("57 true", "57 false"), events) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/input/ScreenKeysTest.kt b/app/src/test/java/com/droiddeck/launcher/input/ScreenKeysTest.kt deleted file mode 100644 index 14905a5964..0000000000 --- a/app/src/test/java/com/droiddeck/launcher/input/ScreenKeysTest.kt +++ /dev/null @@ -1,140 +0,0 @@ -package com.droiddeck.launcher.input - -import android.view.InputDevice -import android.view.KeyEvent -import android.view.MotionEvent -import android.view.View -import org.junit.Assert.assertEquals -import org.junit.Assert.assertFalse -import org.junit.Assert.assertTrue -import org.junit.Before -import org.junit.Test -import org.junit.runner.RunWith -import org.robolectric.RobolectricTestRunner -import org.robolectric.RuntimeEnvironment -import org.robolectric.annotation.Config - -@RunWith(RobolectricTestRunner::class) -@Config(sdk = [28], manifest = Config.NONE) -class ScreenKeysTest { - private val context = RuntimeEnvironment.getApplication() - - @Before fun clearKeys() { - ControllerPrefs.setScreenKeys(context, emptyList()) - } - - @Test fun missesAreForwardedAsTheirOwnTouchStreamWhileAKeyIsHeld() { - val sent = mutableListOf>() - val keys = ScreenKeys(context) { code, down -> sent += code to down } - keys.layout(0, 0, 1000, 600) - keys.add(listOf(KeyEvent.KEYCODE_W)) - val forwarded = mutableListOf>() - keys.onTouchMiss = { forwarded += it.actionMasked to it.getPointerId(0) } - - send(keys, MotionEvent.ACTION_DOWN, listOf(0), listOf(300f), listOf(372f)) - send(keys, MotionEvent.ACTION_POINTER_DOWN or (1 shl MotionEvent.ACTION_POINTER_INDEX_SHIFT), listOf(0, 1), listOf(300f, 900f), listOf(372f, 550f)) - send(keys, MotionEvent.ACTION_MOVE, listOf(0, 1), listOf(310f, 880f), listOf(372f, 530f)) - send(keys, MotionEvent.ACTION_POINTER_UP or (1 shl MotionEvent.ACTION_POINTER_INDEX_SHIFT), listOf(0, 1), listOf(310f, 880f), listOf(372f, 530f)) - send(keys, MotionEvent.ACTION_UP, listOf(0), listOf(310f), listOf(372f)) - - assertEquals( - listOf( - MotionEvent.ACTION_DOWN to 1, - MotionEvent.ACTION_MOVE to 1, - MotionEvent.ACTION_UP to 1, - ), - forwarded, - ) - assertEquals(listOf(KeyEvent.KEYCODE_W to true, KeyEvent.KEYCODE_W to false), sent) - } - - @Test fun gestureRecognizerKeepsOneSplitTargetOwnerUntilItsStreamEnds() { - val gate = GestureTouchStreamGate() - fun accepts(source: GestureTouchStreamGate.Source, action: Int): Boolean { - val event = MotionEvent.obtain(0L, 1L, action, 1f, 1f, 0) - return try { gate.accepts(source, event) } finally { event.recycle() } - } - - assertTrue(accepts(GestureTouchStreamGate.Source.SURFACE, MotionEvent.ACTION_DOWN)) - assertFalse(accepts(GestureTouchStreamGate.Source.SCREEN_KEYS, MotionEvent.ACTION_DOWN)) - assertTrue(accepts(GestureTouchStreamGate.Source.SURFACE, MotionEvent.ACTION_MOVE)) - assertTrue(accepts(GestureTouchStreamGate.Source.SURFACE, MotionEvent.ACTION_UP)) - assertTrue(accepts(GestureTouchStreamGate.Source.SCREEN_KEYS, MotionEvent.ACTION_DOWN)) - assertFalse(accepts(GestureTouchStreamGate.Source.SURFACE, MotionEvent.ACTION_DOWN)) - assertTrue(accepts(GestureTouchStreamGate.Source.SCREEN_KEYS, MotionEvent.ACTION_CANCEL)) - assertTrue(accepts(GestureTouchStreamGate.Source.SURFACE, MotionEvent.ACTION_DOWN)) - assertFalse(accepts(GestureTouchStreamGate.Source.SCREEN_KEYS, MotionEvent.ACTION_DOWN)) - gate.reset() - assertTrue(accepts(GestureTouchStreamGate.Source.SURFACE, MotionEvent.ACTION_DOWN)) - } - - @Test fun takesTouchAtMatchesWhatATouchDoes() { - val keys = ScreenKeys(context) { _, _ -> } - keys.layout(0, 0, 1000, 600) - keys.add(listOf(KeyEvent.KEYCODE_W)) - var checked = 0 - for (x in 0 until 1000 step 25) for (y in 0 until 600 step 25) { - val expected = keys.takesTouchAt(x.toFloat(), y.toFloat()) - val down = MotionEvent.obtain(0L, 0L, MotionEvent.ACTION_DOWN, x.toFloat(), y.toFloat(), 0) - assertEquals("($x, $y)", expected, keys.onTouchEvent(down)) - down.recycle() - MotionEvent.obtain(0L, 1L, MotionEvent.ACTION_UP, x.toFloat(), y.toFloat(), 0).also { keys.onTouchEvent(it); it.recycle() } - if (expected) checked++ - } - assertTrue("some points land on the key", checked > 0) - } - - @Test fun loadingLayoutReleasesLatchedKeysBeforeReplacingEntries() { - val sent = mutableListOf>() - val keys = ScreenKeys(context) { code, down -> sent += code to down } - keys.layout(0, 0, 1000, 600) - keys.add(listOf(KeyEvent.KEYCODE_SHIFT_LEFT)) - keys.setToggle(listOf(KeyEvent.KEYCODE_SHIFT_LEFT), true) - send(keys, MotionEvent.ACTION_DOWN, listOf(0), listOf(300f), listOf(372f)) - send(keys, MotionEvent.ACTION_UP, listOf(0), listOf(300f), listOf(372f)) - - keys.load("") - - assertEquals(listOf(KeyEvent.KEYCODE_SHIFT_LEFT to true, KeyEvent.KEYCODE_SHIFT_LEFT to false), sent) - } - - @Test fun hidingTheViewReleasesLatchedKeys() { - val sent = mutableListOf>() - val keys = ScreenKeys(context) { code, down -> sent += code to down } - keys.layout(0, 0, 1000, 600) - keys.add(listOf(KeyEvent.KEYCODE_SHIFT_LEFT)) - keys.setToggle(listOf(KeyEvent.KEYCODE_SHIFT_LEFT), true) - send(keys, MotionEvent.ACTION_DOWN, listOf(0), listOf(300f), listOf(372f)) - send(keys, MotionEvent.ACTION_UP, listOf(0), listOf(300f), listOf(372f)) - - keys.visibility = View.GONE - - assertEquals(listOf(KeyEvent.KEYCODE_SHIFT_LEFT to true, KeyEvent.KEYCODE_SHIFT_LEFT to false), sent) - } - - private fun send(view: View, action: Int, ids: List, xs: List, ys: List) { - val properties = Array(ids.size) { index -> - MotionEvent.PointerProperties().apply { - id = ids[index] - toolType = MotionEvent.TOOL_TYPE_FINGER - } - } - val coordinates = Array(ids.size) { index -> - MotionEvent.PointerCoords().apply { - x = xs[index] - y = ys[index] - pressure = 1f - size = 1f - } - } - val event = MotionEvent.obtain( - 0L, 1L, action, ids.size, properties, coordinates, 0, 0, - 1f, 1f, 0, 0, InputDevice.SOURCE_TOUCHSCREEN, 0, - ) - try { - view.onTouchEvent(event) - } finally { - event.recycle() - } - } -} diff --git a/app/src/test/java/com/droiddeck/launcher/input/SecondScreenModeTest.kt b/app/src/test/java/com/droiddeck/launcher/input/SecondScreenModeTest.kt index f0df9309de..0888984c4a 100644 --- a/app/src/test/java/com/droiddeck/launcher/input/SecondScreenModeTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/input/SecondScreenModeTest.kt @@ -26,6 +26,12 @@ class SecondScreenModeTest { assertEquals(SecondScreenMode.KEYBOARD_TRACKPAD, SecondScreenMode.opening("deck-controls", deckPad = false, dashboard = true)) } + @Test fun customControlsReopenWithEitherControllerType() { + for (deckPad in listOf(false, true)) { + assertEquals(SecondScreenMode.CUSTOM_CONTROLS, SecondScreenMode.opening("custom-controls", deckPad)) + } + } + @Test fun anUnknownOrClosedChoiceFallsBackToTheDefault() { assertEquals(SecondScreenMode.DECK_CONTROLS, SecondScreenMode.opening("none", deckPad = true, dashboard = true)) assertEquals(SecondScreenMode.DASHBOARD, SecondScreenMode.opening("gone", deckPad = false, dashboard = true)) diff --git a/app/src/test/java/com/droiddeck/launcher/input/SecondScreenProfileMigrationTest.kt b/app/src/test/java/com/droiddeck/launcher/input/SecondScreenProfileMigrationTest.kt new file mode 100644 index 0000000000..a14dcbbc6b --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/input/SecondScreenProfileMigrationTest.kt @@ -0,0 +1,168 @@ +package com.droiddeck.launcher.input + +import android.content.Context +import android.hardware.display.DisplayManager +import android.view.Display +import androidx.compose.runtime.MutableState +import com.droiddeck.launcher.SessionActivity +import com.droiddeck.launcher.session.SessionPrefs +import com.droiddeck.launcher.session.SessionService +import com.droiddeck.launcher.session.SessionState +import org.json.JSONObject +import org.junit.After +import org.junit.Assert.* +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.Robolectric +import org.robolectric.RobolectricTestRunner +import org.robolectric.Shadows.shadowOf +import org.robolectric.annotation.Config +import org.robolectric.util.ReflectionHelpers + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [28], shadows = [SessionInputPointerTest.WriterShadow::class]) +class SecondScreenProfileMigrationTest { + private lateinit var activity: SessionActivity + private val previousMode = SessionState.mode + private val previousPanel = SessionState.secondScreenMode + private val previousDisplay = SessionState.secondScreenDisplay + + @Before fun setUp() { + SessionState.secondScreenMode = SecondScreenMode.NONE + activity = Robolectric.buildActivity(SessionActivity::class.java).get() + val displays = activity.getSystemService(DisplayManager::class.java) + shadowOf(displays.getDisplay(Display.DEFAULT_DISPLAY)).setFlags(Display.FLAG_PRESENTATION) + ReflectionHelpers.setField(activity, "displayManager", displays) + ReflectionHelpers.setField(activity, "started", true) + val display: MutableState = ReflectionHelpers.getField(activity, "selectedSecondScreenDisplay\$delegate") + display.value = Display.DEFAULT_DISPLAY + } + + @After fun tearDown() { + ReflectionHelpers.callInstanceMethod(activity, "closeSecondScreen", ReflectionHelpers.ClassParameter.from(Boolean::class.javaPrimitiveType, true)) + SessionState.mode = previousMode + SessionState.secondScreenMode = previousPanel + SessionState.secondScreenDisplay = previousDisplay + } + + @Test fun automaticOpeningKeepsCustomLayoutsAndBindingsInBothSessionModes() { + for (desktop in listOf(false, true)) for (deckPad in listOf(false, true)) { + val original = customLibrary(desktop) + open(null, deckPad) + assertEquals(original, InputLibraries.read(activity)) + assertNull(SessionPrefs.secondScreenPanel(activity)) + assertEquals(SecondScreenMode.CUSTOM_CONTROLS, SessionState.secondScreenMode) + } + } + + @Test fun unavailableOrUnknownPanelsDoNotReplaceCustomControls() { + for (remembered in listOf("none", "gone", "dashboard", "desktop")) for (deckPad in listOf(false, true)) { + val original = customLibrary(desktop = false) + SessionPrefs.setSecondScreenPanel(activity, remembered) + open(remembered, deckPad) + assertEquals(original, InputLibraries.read(activity)) + assertEquals(remembered, SessionPrefs.secondScreenPanel(activity)) + assertEquals(SecondScreenMode.CUSTOM_CONTROLS, SessionState.secondScreenMode) + } + } + + @Test fun explicitlySavedLegacyPanelsStillMigrateOnlyTheCurrentMode() { + for (desktop in listOf(false, true)) for (legacy in listOf(SecondScreenMode.DECK_CONTROLS, SecondScreenMode.KEYBOARD_TRACKPAD)) { + val original = customLibrary(desktop) + SessionPrefs.setSecondScreenPanel(activity, legacy.id) + open(legacy.id, deckPad = true) + val preset = if (legacy == SecondScreenMode.DECK_CONTROLS) InputStockLayouts.DECK else InputStockLayouts.TRACKPAD + val expected = original.selectPart(desktop, InputRole.SECONDARY, preset) + assertEquals(expected, InputLibraries.read(activity)) + assertEquals(SecondScreenMode.CUSTOM_CONTROLS.id, SessionPrefs.secondScreenPanel(activity)) + open(SessionPrefs.secondScreenPanel(activity), deckPad = true) + assertEquals(expected, InputLibraries.read(activity)) + } + } + + @Test fun legacyPanelsDoNotReplaceAnExistingQuickSelection() { + customLibrary(desktop = true) + val original = InputLibraries.update(activity) { it.selectPart(true, InputRole.SECONDARY, "custom.bindings") } + SessionPrefs.setSecondScreenPanel(activity, SecondScreenMode.DECK_CONTROLS.id) + open(SecondScreenMode.DECK_CONTROLS.id, deckPad = true) + assertEquals(original, InputLibraries.read(activity)) + assertEquals(SecondScreenMode.CUSTOM_CONTROLS.id, SessionPrefs.secondScreenPanel(activity)) + } + + @Test fun unrelatedPartSelectionsDoNotSkipTheSavedPanelMigration() { + for (desktop in listOf(false, true)) for (role in listOf(InputRole.PHYSICAL, InputRole.PRIMARY)) { + customLibrary(desktop) + val original = InputLibraries.update(activity) { it.selectPart(desktop, role, it.profile("game").binding(role)) } + SessionPrefs.setSecondScreenPanel(activity, SecondScreenMode.KEYBOARD_TRACKPAD.id) + open(SecondScreenMode.KEYBOARD_TRACKPAD.id, deckPad = true) + assertEquals(original.selectPart(desktop, InputRole.SECONDARY, InputStockLayouts.TRACKPAD), InputLibraries.read(activity)) + assertEquals(SecondScreenMode.CUSTOM_CONTROLS.id, SessionPrefs.secondScreenPanel(activity)) + } + } + + @Test fun explicitProfilesIncludingAutoKeepTheirSecondaryLayoutAfterOtherPartChanges() { + for (desktop in listOf(false, true)) for (profile in listOf(InputProfiles.AUTO, InputProfile.GAME, InputProfile.DESKTOP)) { + for (role in listOf(null, InputRole.PHYSICAL, InputRole.PRIMARY)) { + customLibrary(desktop) + val original = InputLibraries.update(activity) { library -> + val selected = library.selectProfile(desktop, profile) + if (role == null) selected else selected.selectPart(desktop, role, selected.profile("game").binding(role)) + } + SessionPrefs.setSecondScreenPanel(activity, SecondScreenMode.DECK_CONTROLS.id) + open(SecondScreenMode.DECK_CONTROLS.id, deckPad = true) + assertEquals(original, InputLibraries.read(activity)) + assertEquals(SecondScreenMode.CUSTOM_CONTROLS.id, SessionPrefs.secondScreenPanel(activity)) + } + } + } + + @Test fun unavailableDeckPanelKeepsItsSavedChoiceUntilItCanMigrate() { + for (desktop in listOf(false, true)) { + val original = customLibrary(desktop) + SessionPrefs.setSecondScreenPanel(activity, SecondScreenMode.DECK_CONTROLS.id) + open(SecondScreenMode.DECK_CONTROLS.id, deckPad = false) + assertEquals(original, InputLibraries.read(activity)) + assertEquals(SecondScreenMode.DECK_CONTROLS.id, SessionPrefs.secondScreenPanel(activity)) + open(SessionPrefs.secondScreenPanel(activity), deckPad = true) + assertEquals(original.selectPart(desktop, InputRole.SECONDARY, InputStockLayouts.DECK), InputLibraries.read(activity)) + assertEquals(SecondScreenMode.CUSTOM_CONTROLS.id, SessionPrefs.secondScreenPanel(activity)) + } + } + + @Test fun failedMigrationKeepsTheSavedPanelAndCanBeRetried() { + val original = customLibrary(desktop = false) + val prefs = activity.getSharedPreferences("input-profiles", Context.MODE_PRIVATE) + val valid = prefs.getString("library", null)!! + val unsupported = JSONObject(valid).put("version", 999).toString() + prefs.edit().putString("library", unsupported).commit() + SessionPrefs.setSecondScreenPanel(activity, SecondScreenMode.KEYBOARD_TRACKPAD.id) + open(SecondScreenMode.KEYBOARD_TRACKPAD.id, deckPad = true) + assertEquals(unsupported, prefs.getString("library", null)) + assertEquals(SecondScreenMode.KEYBOARD_TRACKPAD.id, SessionPrefs.secondScreenPanel(activity)) + prefs.edit().putString("library", valid).commit() + open(SessionPrefs.secondScreenPanel(activity), deckPad = true) + assertEquals(original.selectPart(false, InputRole.SECONDARY, InputStockLayouts.TRACKPAD), InputLibraries.read(activity)) + assertEquals(SecondScreenMode.CUSTOM_CONTROLS.id, SessionPrefs.secondScreenPanel(activity)) + } + + private fun customLibrary(desktop: Boolean): InputLibrary { + SessionState.mode = if (desktop) SessionService.MODE_DESKTOP else SessionService.MODE_STEAM + return InputLibraries.update(activity) { library -> + val id = if (desktop) InputProfile.DESKTOP else InputProfile.GAME + val element = InputSurfaceElement(InputControl.A, .3f, .2f, .2f, .2f, "custom.button") + val layout = SavedInputLayout("custom.layout", "Custom controls", listOf(element), screen = InputLayoutScreen.SECONDARY) + val bindings = SavedInputBindings("custom.bindings", "Custom actions", false, layout.id, + elements = mapOf(element.id to InputAction.Keys(listOf(57)))) + val profile = library.profile(id) + library.withLayout(layout).withPreset(bindings) + .withProfile(profile.copy(parts = profile.parts + (InputRole.SECONDARY to InputPart(bindings.id, layout.id)))) + .copy(selections = emptyMap()) + } + } + + private fun open(remembered: String?, deckPad: Boolean) { + val mode = SecondScreenMode.opening(remembered, deckPad) + ReflectionHelpers.callInstanceMethod(activity, "showSecondScreen", ReflectionHelpers.ClassParameter.from(SecondScreenMode::class.java, mode)) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/input/SelectProfileRoutingTest.kt b/app/src/test/java/com/droiddeck/launcher/input/SelectProfileRoutingTest.kt new file mode 100644 index 0000000000..3cbdaacc4d --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/input/SelectProfileRoutingTest.kt @@ -0,0 +1,110 @@ +package com.droiddeck.launcher.input + +import android.hardware.input.InputManager +import android.view.InputDevice +import android.view.KeyEvent +import java.io.File +import org.junit.After +import org.junit.Assert.* +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.Shadows.shadowOf +import org.robolectric.annotation.Config +import org.robolectric.shadows.InputDeviceBuilder + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [34], manifest = Config.NONE, shadows = [JoyConPadTest.RecordingWriter::class, + PadBridgeRoutingTest.RegisteredKeyEvent::class]) +class SelectProfileRoutingTest { + private val context get() = RuntimeEnvironment.getApplication() + private val bridge = PadBridge(File(context.cacheDir, "select-profile")) + private val events = mutableListOf() + private val output = object : InputOutput { + override fun key(code: Int, down: Boolean) { events += "key $code $down" } + override fun button(code: Int, down: Boolean) = Unit + override fun pointer(dx: Float, dy: Float) = Unit + override fun scroll(steps: Int) = Unit + override fun command(command: InputCommand) { events += command.name } + override fun requestMotion() = Unit + } + private val device = InputDeviceBuilder.newBuilder().setId(42).setName("Select test pad") + .setDescriptor("select-test").setSources(InputDevice.SOURCE_GAMEPAD).build() + + @Before fun setUp() { + context.getSharedPreferences("input-profiles", 0).edit().clear().commit() + context.getSharedPreferences("controller", 0).edit().clear().commit() + ControllerPrefs.setSelectSteam(context, true) + shadowOf(context.getSystemService(InputManager::class.java)).addInputDevice(device) + bridge.setInputRouter(InputRouter(output)) + bridge.setInputProfile(InputProfiles.active(context, desktop = false)) + } + + @After fun close() { bridge.stop() } + + @Test fun migratedSteamSelectReachesGuideAndReleasesWithoutReleasingPhysicalGuide() { + key(KeyEvent.KEYCODE_BUTTON_SELECT, true) + assertTrue(state().isDown(PadState.GUIDE)) + assertFalse(state().isDown(PadState.SELECT)) + key(KeyEvent.KEYCODE_BUTTON_MODE, true) + key(KeyEvent.KEYCODE_BUTTON_SELECT, false) + assertTrue(state().isDown(PadState.GUIDE)) + key(KeyEvent.KEYCODE_BUTTON_MODE, false) + assertFalse(state().isDown(PadState.GUIDE)) + } + + @Test fun profileResetAndExplicitNativeBindingOverrideTheMigratedSetting() { + val migrated = InputProfiles.active(context, desktop = false) + InputProfiles.save(context, migrated.withBinding(InputRole.PHYSICAL, InputControl.SELECT, InputAction.Native())) + bridge.setInputProfile(InputProfiles.active(context, desktop = false)) + assertNativeSelect() + InputProfiles.reset(context, InputProfile.GAME) + bridge.setInputProfile(InputProfiles.active(context, desktop = false)) + assertNativeSelect() + assertTrue(ControllerPrefs.read(context).selectSteam) + } + + @Test fun disabledAndLocalSelectBindingsDoNotLeakGuide() { + for (action in listOf(InputAction.Disabled, InputAction.Keys(listOf(28)), InputAction.Command(InputCommand.KEYBOARD))) { + bridge.setInputProfile(InputProfile.defaults(InputProfile.GAME).withBinding(InputRole.PHYSICAL, InputControl.SELECT, action)) + key(KeyEvent.KEYCODE_BUTTON_SELECT, true) + assertFalse(state().isDown(PadState.GUIDE)) + assertFalse(state().isDown(PadState.SELECT)) + key(KeyEvent.KEYCODE_BUTTON_SELECT, false) + } + assertEquals(listOf("key 28 true", "key 28 false", "KEYBOARD"), events) + } + + @Test fun replacingOrRemovingRouterWhileSelectIsHeldNeverLeavesGuideStuck() { + key(KeyEvent.KEYCODE_BUTTON_SELECT, true) + bridge.setInputRouter(null) + assertFalse(state().isDown(PadState.GUIDE)) + key(KeyEvent.KEYCODE_BUTTON_SELECT, false) + assertFalse(state().isDown(PadState.SELECT)) + key(KeyEvent.KEYCODE_BUTTON_SELECT, true) + bridge.setInputRouter(InputRouter(output).also { it.setProfile(InputProfiles.active(context, desktop = false)) }) + assertFalse(state().isDown(PadState.GUIDE)) + key(KeyEvent.KEYCODE_BUTTON_SELECT, false) + assertFalse(state().isDown(PadState.GUIDE)) + key(KeyEvent.KEYCODE_BUTTON_SELECT, true) + assertTrue(state().isDown(PadState.GUIDE)) + key(KeyEvent.KEYCODE_BUTTON_SELECT, false) + assertFalse(state().isDown(PadState.GUIDE)) + } + + private fun assertNativeSelect() { + key(KeyEvent.KEYCODE_BUTTON_SELECT, true) + assertTrue(state().isDown(PadState.SELECT)) + assertFalse(state().isDown(PadState.GUIDE)) + key(KeyEvent.KEYCODE_BUTTON_SELECT, false) + } + + private fun key(code: Int, down: Boolean) { + assertTrue(bridge.onKeyEvent(KeyEvent(0, 0, if (down) KeyEvent.ACTION_DOWN else KeyEvent.ACTION_UP, + code, 0, 0, device.id, 0, 0, InputDevice.SOURCE_GAMEPAD))) + } + + private fun state() = JoyConPadTest.RecordingWriter.pads[0]!! +} diff --git a/app/src/test/java/com/droiddeck/launcher/input/SessionBackBindingTest.kt b/app/src/test/java/com/droiddeck/launcher/input/SessionBackBindingTest.kt new file mode 100644 index 0000000000..4e6669d928 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/input/SessionBackBindingTest.kt @@ -0,0 +1,141 @@ +package com.droiddeck.launcher.input + +import android.hardware.input.InputManager +import android.os.Looper +import android.view.InputDevice +import android.view.KeyEvent +import androidx.compose.runtime.MutableState +import com.droiddeck.launcher.SessionActivity +import com.droiddeck.launcher.session.LoadingState +import com.droiddeck.launcher.session.SessionService +import com.droiddeck.launcher.session.SessionState +import java.io.File +import java.time.Duration +import org.junit.After +import org.junit.Assert.* +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.Robolectric +import org.robolectric.RobolectricTestRunner +import org.robolectric.Shadows.shadowOf +import org.robolectric.annotation.Config +import org.robolectric.shadows.InputDeviceBuilder +import org.robolectric.util.ReflectionHelpers + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [34], manifest = Config.NONE, shadows = [JoyConPadTest.RecordingWriter::class, + PadBridgeRoutingTest.RegisteredKeyEvent::class]) +class SessionBackBindingTest { + private val activity = Robolectric.buildActivity(SessionActivity::class.java).get() + private val bridge = PadBridge(File(activity.cacheDir, "back-binding")) + private val events = mutableListOf>() + private val router = InputRouter(object : InputOutput { + override fun key(code: Int, down: Boolean) { events += code to down } + override fun button(code: Int, down: Boolean) {} + override fun pointer(dx: Float, dy: Float) {} + override fun scroll(steps: Int) {} + override fun command(command: InputCommand) {} + override fun requestMotion() {} + }) + private val bindings = PhysicalInputBindings(router) + private val previousMode = SessionState.mode + private val previousRunning = SessionState.running + private val previousFrame = SessionState.firstFrameSeen + private val previousDeck = SessionState.deckPad + private val previousSuspended = SessionState.suspended + + @Before fun setUp() { + SessionState.mode = SessionService.MODE_STEAM + SessionState.running = true + SessionState.firstFrameSeen = true + SessionState.deckPad = true + SessionState.suspended = false + ReflectionHelpers.setStaticField(FakeInputWriter::class.java, "padType", FakeInputWriter.PAD_TYPE_DECK) + ReflectionHelpers.setField(activity, "padBridge", bridge) + ReflectionHelpers.setField(activity, "physicalInputBindings", bindings) + ReflectionHelpers.setField(activity, "loading", LoadingState(activity).apply { visible = false }) + ReflectionHelpers.getField>(activity, "resumed\$delegate").value = true + val device = InputDeviceBuilder.newBuilder().setId(42).setName("Back key").setDescriptor("back-key") + .setSources(InputDevice.SOURCE_KEYBOARD).setKeyboardType(InputDevice.KEYBOARD_TYPE_NON_ALPHABETIC).build() + shadowOf(activity.getSystemService(InputManager::class.java)).addInputDevice(device) + bridge.setInputRouter(router) + bridge.start() + } + + @After fun close() { + ReflectionHelpers.getField(activity, "backQamInput").cancel() + bindings.close() + bridge.stop() + SessionState.mode = previousMode + SessionState.running = previousRunning + SessionState.firstFrameSeen = previousFrame + SessionState.deckPad = previousDeck + SessionState.suspended = previousSuspended + } + + @Test fun capturedBackBindingTakesPrecedenceOverTheDefaultQamShortcut() { + bindings.configure(listOf(PhysicalInputBinding(PhysicalInputKey.capture(event(true))!!, InputAction.Keys(listOf(28))))) + assertTrue(activity.dispatchKeyEvent(event(true))) + assertEquals(listOf(28 to true), events) + advance(700) + assertFalse(JoyConPadTest.RecordingWriter.pads[0]?.isDown(PadState.QAM) == true) + assertTrue(activity.dispatchKeyEvent(event(false))) + assertEquals(listOf(28 to true, 28 to false), events) + } + + @Test fun anUnmappedBackKeyStillHoldsAndReleasesQam() { + assertTrue(activity.dispatchKeyEvent(event(true))) + advance(700) + assertTrue(JoyConPadTest.RecordingWriter.pads[0]!!.isDown(PadState.QAM)) + assertTrue(activity.dispatchKeyEvent(event(false))) + advance(1000) + assertFalse(JoyConPadTest.RecordingWriter.pads[0]!!.isDown(PadState.QAM)) + assertTrue(events.isEmpty()) + } + + @Test fun capturedProfileBindingTakesPrecedenceOverGlobalShortcut() { + assignMenuShortcut() + bindings.configure(listOf(PhysicalInputBinding(PhysicalInputKey.capture(event(true))!!, InputAction.Keys(listOf(28))))) + assertTrue(activity.dispatchKeyEvent(event(true))) + assertTrue(activity.dispatchKeyEvent(event(false))) + assertEquals(listOf(28 to true, 28 to false), events) + assertFalse(drawerOpen()) + } + + @Test fun globalShortcutOverridesDefaultBackQamAndConsumesReleaseAcrossMenuChange() { + assignMenuShortcut() + assertTrue(activity.dispatchKeyEvent(event(true))) + assertTrue(drawerOpen()) + advance(700) + assertFalse(JoyConPadTest.RecordingWriter.pads[0]?.isDown(PadState.QAM) == true) + assertTrue(activity.dispatchKeyEvent(event(false))) + assertTrue(drawerOpen()) + assertTrue(activity.dispatchKeyEvent(event(true))) + assertFalse(drawerOpen()) + assertTrue(activity.dispatchKeyEvent(event(false))) + assertTrue(events.isEmpty()) + } + + @Test fun inputSettingsDoNotExecuteGlobalShortcuts() { + assignMenuShortcut() + ReflectionHelpers.getField>(activity, "inputSettingsOpen\$delegate").value = true + activity.dispatchKeyEvent(event(true)) + activity.dispatchKeyEvent(event(false)) + assertFalse(drawerOpen()) + assertTrue(events.isEmpty()) + } + + private fun assignMenuShortcut() { + ReflectionHelpers.getField(activity, "physicalShortcuts").bindings = + listOf(PhysicalShortcuts.Binding(PhysicalShortcuts.button(event(true))!!, PhysicalShortcuts.Action.DROIDDECK)) + } + + private fun drawerOpen(): Boolean = + ReflectionHelpers.getField>(activity, "drawerOpenState\$delegate").value + + private fun event(down: Boolean) = KeyEvent(0L, 0L, if (down) KeyEvent.ACTION_DOWN else KeyEvent.ACTION_UP, + KeyEvent.KEYCODE_BACK, 0, 0, 42, 158, 0, InputDevice.SOURCE_KEYBOARD) + + private fun advance(ms: Long) = shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(ms)) +} diff --git a/app/src/test/java/com/droiddeck/launcher/input/SessionInputPointerTest.kt b/app/src/test/java/com/droiddeck/launcher/input/SessionInputPointerTest.kt new file mode 100644 index 0000000000..835eca2e9b --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/input/SessionInputPointerTest.kt @@ -0,0 +1,302 @@ +package com.droiddeck.launcher.input + +import android.os.Looper +import android.view.SurfaceView +import androidx.compose.runtime.MutableState +import androidx.compose.runtime.snapshots.Snapshot +import androidx.compose.ui.geometry.Offset +import com.droiddeck.launcher.SessionActivity +import com.droiddeck.launcher.session.SessionService +import com.droiddeck.launcher.session.SessionState +import com.droiddeck.launcher.wayland.WaylandCompositor +import org.junit.After +import org.junit.Assert.* +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.Robolectric +import org.robolectric.RobolectricTestRunner +import org.robolectric.Shadows.shadowOf +import org.robolectric.annotation.Config +import org.robolectric.annotation.Implementation +import org.robolectric.annotation.Implements +import org.robolectric.util.ReflectionHelpers +import java.util.Collections +import java.util.concurrent.CountDownLatch +import java.util.concurrent.Executors +import java.util.concurrent.TimeUnit + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [28], manifest = Config.NONE, shadows = [SessionInputPointerTest.CompositorShadow::class, SessionInputPointerTest.WriterShadow::class]) +class SessionInputPointerTest { + private lateinit var activity: SessionActivity + private lateinit var output: InputOutput + private var previousSize = SessionState.outputSize + private var previousMode = SessionState.mode + + @Before fun setUp() { + activity = Robolectric.buildActivity(SessionActivity::class.java).get() + output = ReflectionHelpers.getField(activity, "inputOutput") + ReflectionHelpers.setField(activity, "surfaceView", SurfaceView(activity).apply { layout(0, 0, 1000, 600) }) + SessionState.outputSize = 1600 to 900 + SessionState.mode = SessionService.MODE_DESKTOP + InputProfiles.resetAll(activity) + InputProfiles.select(activity, InputProfile.DESKTOP) + refresh() + CompositorShadow.pointers.clear() + CompositorShadow.events.clear() + CompositorShadow.keys.clear() + WriterShadow.mouse.clear() + WriterShadow.beforeWheel = null + } + + @After fun tearDown() { + shadowOf(Looper.getMainLooper()).idle() + SessionState.outputSize = previousSize + SessionState.mode = previousMode + } + + @Test fun desktopMouseButtonsKeepTheVisibleCursorAndDeliverImmediately() { + state("cursorPos").value = Offset(500f, 300f) + state("cursorVisible").value = true + output.button(272, true) + invoke("onPadInput") + output.button(272, false) + assertTrue(state("cursorVisible").value) + assertEquals(listOf(Triple(3, 272, 1), Triple(3, 272, 0)), CompositorShadow.events) + } + + @Test fun nativeGameInputStillHidesCursor() { + InputProfiles.select(activity, InputProfile.GAME) + refresh() + state("cursorVisible").value = true + invoke("onPadInput") + assertFalse(state("cursorVisible").value) + } + + @Test fun lowerScreenPointerBindingAlsoKeepsCursorVisible() { + InputProfiles.save(activity, InputProfile(InputProfile.GAME).withBinding(InputRole.SECONDARY, InputControl.RIGHT_PAD, InputAction.Pointer())) + InputProfiles.select(activity, InputProfile.GAME) + refresh() + state("cursorVisible").value = true + invoke("onPadInput") + assertTrue(state("cursorVisible").value) + } + + @Test fun stickContinuesFromTrackpadPositionAndUpdatesVisibleAndGuestCoordinates() { + output.pointer(-400f, -225f) + output.pointer(160f, 90f) + invoke("onPadInput") + assertEquals(Offset(350f, 215.625f), state("cursorPos").value) + assertTrue(state("cursorVisible").value) + assertEquals(Triple(1, InputSpace.x(350f, 1000), InputSpace.y(215.625f, 600)), CompositorShadow.pointers.last()) + } + + @Test fun firstStickMovementStartsAtCenterAndClampsToPictureBounds() { + output.pointer(160f, 90f) + assertEquals(Offset(600f, 356.25f), state("cursorPos").value) + output.pointer(10000f, -10000f) + assertEquals(Offset(1000f, 18.75f), state("cursorPos").value) + } + + @Test fun stickFrameNotifiesCursorDrawingBeforeReturningToTheLooper() { + val position = state("cursorPos") + Snapshot.sendApplyNotifications() + var changed = false + val observer = Snapshot.registerApplyObserver { objects, _ -> + if (position in objects) changed = true + } + try { + output.pointer(20f, 10f) + assertTrue(changed) + } finally { observer.dispose() } + } + + @Test fun pointerLockKeepsRelativeMotionAndHiddenCursor() { + ReflectionHelpers.setField(activity, "pointerLocked", true) + state("cursorVisible").value = true + output.pointer(2.5f, -1.25f) + assertEquals(listOf(Triple(6, 640, -320)), CompositorShadow.events) + assertTrue(CompositorShadow.pointers.isEmpty()) + assertFalse(state("cursorVisible").value) + } + + @Test fun clientRequestedHiddenCursorStaysHidden() { + ReflectionHelpers.setField(activity, "clientHidesCursor", true) + output.pointer(1f, 1f) + assertFalse(state("cursorVisible").value) + } + + @Test fun mappedPointerStaysVisibleWithDirectTouchSelected() { + ReflectionHelpers.setField(activity, "routedTouchMode", com.droiddeck.launcher.session.SessionPrefs.TOUCH_DIRECT) + state("directCursorOn").value = false + output.pointer(20f, 10f) + assertTrue(state("cursorVisible").value) + } + + @Test fun touchpadZoomDoesNotReleaseCtrlHeldByTheProfileOrScreenKeys() { + val held = ReflectionHelpers.getField(activity, "heldKeys") + output.key(29, true) + held.set(64, 29, true) + ReflectionHelpers.getField(activity, "pointerListener").onZoom(1) + assertEquals(listOf(29 to 1), CompositorShadow.keys) + output.key(29, false) + assertEquals(listOf(29 to 1), CompositorShadow.keys) + held.set(64, 29, false) + assertEquals(listOf(29 to 1, 29 to 0), CompositorShadow.keys) + assertEquals(listOf(Triple(4, 1, 0)), CompositorShadow.events) + } + + @Test fun desktopRightCtrlUsesUpstreamAliasWithoutReleasingTheProfileHold() { + fun hardwareCtrl(down: Boolean) = ReflectionHelpers.callInstanceMethod(activity, "sendGuestKey", + ReflectionHelpers.ClassParameter.from(Int::class.javaPrimitiveType, 97), + ReflectionHelpers.ClassParameter.from(Boolean::class.javaPrimitiveType, down), + ReflectionHelpers.ClassParameter.from(Int::class.javaPrimitiveType, 8)) + output.key(29, true) + hardwareCtrl(true) + hardwareCtrl(false) + assertEquals(listOf(29 to 1), CompositorShadow.keys) + output.key(29, false) + assertEquals(listOf(29 to 1, 29 to 0), CompositorShadow.keys) + } + + @Test fun routedMouseUsesRawBackendAndKeepsSharedButtonOwnership() { + ReflectionHelpers.setField(activity, "mouseWriter", FakeInputWriter(activity.cacheDir.path, FakeInputWriter.MOUSE_SLOT)) + ReflectionHelpers.setField(activity, "evdevMouseActive", true) + ReflectionHelpers.setField(activity, "pointerLocked", true) + val held = ReflectionHelpers.getField(activity, "heldButtons") + output.button(272, true) + held.set(2, 272, true) + output.button(272, false) + assertEquals(listOf("button:272:true"), WriterShadow.mouse) + held.set(2, 272, false) + output.pointer(2.5f, -1.25f) + output.scroll(2) + assertEquals(listOf("button:272:true", "button:272:false", "delta:2.5:-1.25", "wheel:-2:0:120"), WriterShadow.mouse) + assertTrue(CompositorShadow.events.isEmpty()) + } + + @Test fun focusCleanupReleasesHardwareAndKeyboardKeysWithoutReleasingAnotherOwner() { + val held = ReflectionHelpers.getField(activity, "heldKeys") + output.key(29, true) + for ((owner, code) in listOf(8 to 30, 16 to 31, 32 to 32, 16 to 29)) held.set(owner, code, true) + ReflectionHelpers.callInstanceMethod(activity, "releaseHeldGuestInput") + assertEquals(setOf(30 to 0, 31 to 0, 32 to 0), CompositorShadow.keys.filter { it.second == 0 }.toSet()) + output.key(29, false) + assertEquals(29 to 0, CompositorShadow.keys.last()) + } + + @Test fun horizontalWheelUsesTheSameDirectionInBothMouseBackends() { + output.scrollHorizontal(2) + assertEquals(listOf(Triple(11, 0, 240)), CompositorShadow.events) + CompositorShadow.events.clear() + ReflectionHelpers.setField(activity, "mouseWriter", FakeInputWriter(activity.cacheDir.path, FakeInputWriter.MOUSE_SLOT)) + ReflectionHelpers.setField(activity, "evdevMouseActive", true) + output.scrollHorizontal(2) + assertEquals(listOf("wheel:0:2:120"), WriterShadow.mouse) + assertTrue(CompositorShadow.events.isEmpty()) + } + + @Test fun leavingRawMouseReleasesItsHoldBeforeReturningToTheWaylandSeat() { + ReflectionHelpers.setField(activity, "mouseWriter", FakeInputWriter(activity.cacheDir.path, FakeInputWriter.MOUSE_SLOT)) + ReflectionHelpers.setField(activity, "evdevMouseActive", true) + output.button(272, true) + invoke("closeEvdevMouse") + output.button(272, false) + assertEquals(listOf("button:272:true", "button:272:false", "release"), WriterShadow.mouse) + assertTrue(CompositorShadow.events.isEmpty()) + output.button(272, true) + output.button(272, false) + assertEquals(listOf(Triple(3, 272, 1), Triple(3, 272, 0)), CompositorShadow.events) + } + + @Test fun wowFieldSelectionPreservesCtrlHeldByTheInputProfile() { + output.key(29, true) + ReflectionHelpers.callInstanceMethod(activity, "selectGameField", + ReflectionHelpers.ClassParameter.from(IntArray::class.java, intArrayOf(46))) + assertEquals(listOf(29 to 1, 30 to 1, 30 to 0, 46 to 1, 46 to 0), CompositorShadow.keys) + output.key(29, false) + assertEquals(29 to 0, CompositorShadow.keys.last()) + } + + @Test fun horizontalWheelFinishesBeforeRawMouseDestruction() { + checkWheelTransition(horizontal = true, transition = "releaseEvdevMouse", marker = "destroy") + } + + @Test fun verticalWheelFinishesBeforeRawMouseSoftRelease() { + checkWheelTransition(horizontal = false, transition = "closeEvdevMouse", marker = "release") + } + + private fun checkWheelTransition(horizontal: Boolean, transition: String, marker: String) { + ReflectionHelpers.setField(activity, "mouseWriter", FakeInputWriter(activity.cacheDir.path, FakeInputWriter.MOUSE_SLOT)) + ReflectionHelpers.setField(activity, "evdevMouseActive", true) + val entered = CountDownLatch(1) + val continueWrite = CountDownLatch(1) + val transitioning = CountDownLatch(1) + val finished = CountDownLatch(1) + WriterShadow.beforeWheel = { + entered.countDown() + check(continueWrite.await(5, TimeUnit.SECONDS)) + } + val executor = Executors.newFixedThreadPool(2) + fun scroll() { if (horizontal) output.scrollHorizontal(2) else output.scroll(2) } + try { + val writing = executor.submit { scroll() } + assertTrue(entered.await(5, TimeUnit.SECONDS)) + val closing = executor.submit { + transitioning.countDown() + invoke(transition) + finished.countDown() + } + assertTrue(transitioning.await(5, TimeUnit.SECONDS)) + assertFalse(finished.await(100, TimeUnit.MILLISECONDS)) + continueWrite.countDown() + writing.get(5, TimeUnit.SECONDS) + closing.get(5, TimeUnit.SECONDS) + assertEquals(listOf(if (horizontal) "wheel:0:2:120" else "wheel:-2:0:120", marker), WriterShadow.mouse) + scroll() + assertEquals(2, WriterShadow.mouse.size) + assertEquals(listOf(if (horizontal) Triple(11, 0, 240) else Triple(4, 2, 0)), CompositorShadow.events) + } finally { + continueWrite.countDown() + executor.shutdownNow() + executor.awaitTermination(5, TimeUnit.SECONDS) + WriterShadow.beforeWheel = null + } + } + + private fun refresh() = invoke("refreshInputProfile") + private fun invoke(name: String) = ReflectionHelpers.callInstanceMethod(activity, name) + private fun state(name: String): MutableState = ReflectionHelpers.getField(activity, "$name\$delegate") + + @Implements(value = WaylandCompositor::class, isInAndroidSdk = false) + class CompositorShadow { + companion object { + val pointers = mutableListOf>() + val events = mutableListOf>() + val keys = mutableListOf>() + @JvmStatic @Implementation fun __staticInitializer__() {} + @JvmStatic @Implementation fun nativeSendPointer(action: Int, x: Int, y: Int) { pointers += Triple(action, x, y) } + @JvmStatic @Implementation fun nativeSendSceneInput(type: Int, code: Int, value: Int) { events += Triple(type, code, value) } + @JvmStatic @Implementation fun nativeSendKey(code: Int, value: Int) { keys += code to value } + } + } + + @Implements(value = FakeInputWriter::class, isInAndroidSdk = false) + class WriterShadow { + @Implementation fun writeMouseButton(code: Short, down: Boolean) { mouse += "button:$code:$down" } + @Implementation fun writeMouseDelta(dx: Float, dy: Float) { mouse += "delta:$dx:$dy" } + @Implementation fun writeMouseWheel(vertical: Int, horizontal: Int, value120: Int) { + beforeWheel?.invoke() + mouse += "wheel:$vertical:$horizontal:$value120" + } + @Implementation fun softRelease() { mouse += "release" } + @Implementation fun destroy() { mouse += "destroy" } + companion object { + val mouse: MutableList = Collections.synchronizedList(mutableListOf()) + @Volatile var beforeWheel: (() -> Unit)? = null + @JvmStatic @Implementation fun __staticInitializer__() {} + @JvmStatic @Implementation fun writeDeckControls(index: Int, mask: Int, pads: ShortArray, pressure: ShortArray) {} + } + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/input/SessionSystemInputTest.kt b/app/src/test/java/com/droiddeck/launcher/input/SessionSystemInputTest.kt new file mode 100644 index 0000000000..167f486816 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/input/SessionSystemInputTest.kt @@ -0,0 +1,90 @@ +package com.droiddeck.launcher.input + +import android.os.Handler +import android.os.Looper +import com.droiddeck.launcher.SessionActivity +import java.io.File +import java.time.Duration +import org.junit.After +import org.junit.Assert.* +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.Robolectric +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.Shadows.shadowOf +import org.robolectric.annotation.Config +import org.robolectric.annotation.LooperMode +import org.robolectric.util.ReflectionHelpers + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [34], manifest = Config.NONE, shadows = [JoyConPadTest.RecordingWriter::class]) +@LooperMode(LooperMode.Mode.PAUSED) +class SessionSystemInputTest { + private val bridge = PadBridge(File(RuntimeEnvironment.getApplication().cacheDir, "system-input")) + private val activity = Robolectric.buildActivity(SessionActivity::class.java).get().also { + ReflectionHelpers.setField(it, "padBridge", bridge) + } + private val output = RecordingOutput() + private val router = InputRouter(output).also { + bridge.setInputRouter(it) + bridge.setInputProfile(InputProfile.defaults(InputProfile.GAME) + .withBinding(InputRole.PRIMARY, InputControl.GUIDE, InputAction.Disabled) + .withBinding(InputRole.PRIMARY, InputControl.L3, InputAction.Keys(listOf(29))) + .withBinding(InputRole.PRIMARY, InputControl.R3, InputAction.Command(InputCommand.MENU))) + } + + @After fun close() { bridge.stop(); advance(500) } + + @Test fun steamMenuBypassesPrimaryBindingsAndDisabledRouting() { + bridge.setInputEnabled(false) + invoke("sendSteamGuide") + assertTrue(state().isDown(PadState.GUIDE)) + advance(90) + assertFalse(state().isDown(PadState.GUIDE)) + assertTrue(output.events.isEmpty()) + } + + @Test fun vrStickSwitchBypassesPrimaryBindingsAndPreservesPhysicalHold() { + bridge.applyExternal({ it.press(PadState.L3, true) }, true) + invoke("switchVrSticks") + assertTrue(state().isDown(PadState.L3)) + assertTrue(state().isDown(PadState.R3)) + advance(120) + assertTrue(state().isDown(PadState.L3)) + assertFalse(state().isDown(PadState.R3)) + assertTrue(output.events.isEmpty()) + } + + @Test fun releasingSystemActionsCancelsOldVrReleaseWithoutEndingNewTap() { + invoke("switchVrSticks") + advance(60) + bridge.releaseAll() + assertFalse(state().isDown(PadState.L3)) + assertFalse(state().isDown(PadState.R3)) + invoke("switchVrSticks") + advance(60) + assertTrue(state().isDown(PadState.L3)) + assertTrue(state().isDown(PadState.R3)) + advance(60) + assertFalse(state().isDown(PadState.L3)) + assertFalse(state().isDown(PadState.R3)) + } + + private fun state() = JoyConPadTest.RecordingWriter.pads[0]!! + private fun invoke(name: String) = ReflectionHelpers.callInstanceMethod(activity, name) + private fun advance(ms: Long) { + shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(ms)) + shadowOf(ReflectionHelpers.getStaticField(PadBridge::class.java, "chordHandler").looper).idle() + } + + private class RecordingOutput : InputOutput { + val events = mutableListOf() + override fun key(code: Int, down: Boolean) { events += "key $code $down" } + override fun button(code: Int, down: Boolean) { events += "button $code $down" } + override fun pointer(dx: Float, dy: Float) { events += "pointer" } + override fun scroll(steps: Int) { events += "scroll" } + override fun command(command: InputCommand) { events += command.name } + override fun requestMotion() { events += "motion" } + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/input/SteamControllerBleTest.kt b/app/src/test/java/com/droiddeck/launcher/input/SteamControllerBleTest.kt new file mode 100644 index 0000000000..9d05efea50 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/input/SteamControllerBleTest.kt @@ -0,0 +1,177 @@ +package com.droiddeck.launcher.input + +import android.bluetooth.BluetoothAdapter +import android.bluetooth.BluetoothGatt +import android.bluetooth.BluetoothGattCallback +import android.bluetooth.BluetoothGattCharacteristic +import android.bluetooth.BluetoothGattDescriptor +import android.os.Handler +import org.junit.After +import org.junit.Assert.* +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.Shadows.shadowOf +import org.robolectric.annotation.Config +import org.robolectric.annotation.Implementation +import org.robolectric.annotation.Implements +import org.robolectric.util.ReflectionHelpers +import org.robolectric.shadows.ShadowBluetoothGatt +import java.util.UUID + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [34], manifest = Config.NONE, shadows = [SteamControllerBleTest.Writer::class]) +class SteamControllerBleTest { + private val keys = ArrayList>() + private val router = InputRouter(object : InputOutput { + override fun key(code: Int, down: Boolean) { keys += code to down } + override fun button(code: Int, down: Boolean) {} + override fun pointer(dx: Float, dy: Float) {} + override fun scroll(steps: Int) {} + override fun command(command: InputCommand) {} + override fun requestMotion() {} + }) + private lateinit var controller: SteamControllerBle + private lateinit var handler: Handler + + @Before fun setUp() { + Writer.reports.clear() + Writer.connections.clear() + Writer.features = 0 + router.setProfile(InputProfile.defaults(InputProfile.GAME) + .withBinding(InputRole.PHYSICAL, InputControl.A, InputAction.Keys(listOf(57)))) + controller = SteamControllerBle(RuntimeEnvironment.getApplication(), router) + handler = ReflectionHelpers.getField(controller, "handler") + shadowOf(handler.looper).pause() + controller.start() + } + + @After fun close() { + controller.stop() + handler.looper.quit() + } + + @Test fun stopReleasesAndDetachesInputBeforeQueuedCallbacksCanPublish() { + state(ByteArray(45).apply { this[1] = 1 }) + assertEquals(listOf(57 to true), keys) + handler.post { state(ByteArray(45).apply { this[1] = 1 }) } + controller.stop() + assertEquals(listOf(57 to true, 57 to false), keys) + assertNull(ReflectionHelpers.getField(controller, "routedInput")) + assertTrue(Writer.reports.last().all { it == 0.toByte() }) + val reports = Writer.reports.size + val connections = Writer.connections.toList() + shadowOf(handler.looper).idle() + state(ByteArray(45)) + state(ByteArray(45).apply { this[1] = 1 }) + ReflectionHelpers.callInstanceMethod(controller, "release") + ReflectionHelpers.setField(controller, "featureRead", 7L) + ReflectionHelpers.callInstanceMethod(controller, "onFeatureRead", ReflectionHelpers.ClassParameter.from(ByteArray::class.java, ByteArray(2))) + controller.stop() + assertEquals(reports, Writer.reports.size) + assertEquals(connections, Writer.connections) + assertEquals(0, Writer.features) + assertEquals(listOf(57 to true, 57 to false), keys) + } + + @Test fun stoppingBeforeQueuedStartPreservesAnotherSourcesHeldKey() { + val other = router.channel(InputRole.PRIMARY, InputControl.B, InputAction.Keys(listOf(57))) + other.update(1f) + state(ByteArray(45).apply { this[1] = 1 }) + controller.start() + controller.stop() + shadowOf(handler.looper).idle() + assertFalse(ReflectionHelpers.getField(controller, "running")) + assertEquals(listOf(57 to true), keys) + other.close() + assertEquals(listOf(57 to true, 57 to false), keys) + } + + @Test fun staleControllerCannotClearOrPublishIntoItsReplacementsSlot() { + state(ByteArray(45).apply { this[1] = 1 }) + assertEquals(listOf(57 to true), keys) + val replacement = SteamControllerBle(RuntimeEnvironment.getApplication()) + val replacementHandler = ReflectionHelpers.getField(replacement, "handler") + shadowOf(replacementHandler.looper).pause() + try { + replacement.start() + subscribe(replacement) + val report = ByteArray(45).apply { this[1] = 2 } + state(report, replacement) + val reports = Writer.reports.size + val connections = Writer.connections.toList() + controller.start() + shadowOf(handler.looper).idle() + subscribe(controller) + state(ByteArray(45)) + ReflectionHelpers.setField(controller, "featureRead", 7L) + ReflectionHelpers.callInstanceMethod(controller, "onFeatureRead", + ReflectionHelpers.ClassParameter.from(ByteArray::class.java, ByteArray(2))) + ReflectionHelpers.callInstanceMethod(controller, "disconnect", + ReflectionHelpers.ClassParameter.from(String::class.java, "old link lost")) + controller.stop() + shadowOf(handler.looper).idle() + assertTrue(SteamControllerBle.connected) + assertEquals(connections, Writer.connections) + assertEquals(reports, Writer.reports.size) + assertArrayEquals(report, Writer.reports.last()) + assertEquals(0, Writer.features) + assertEquals(listOf(57 to true, 57 to false), keys) + assertNull(ReflectionHelpers.getField(controller, "routedInput")) + replacement.stop() + assertFalse(SteamControllerBle.connected) + assertFalse(Writer.connections.last()) + assertTrue(Writer.reports.last().all { it == 0.toByte() }) + } finally { + replacement.stop() + replacementHandler.looper.quit() + } + } + + @Test fun stoppingAnUnstartedControllerPreservesTheActiveSlot() { + subscribe(controller) + val unused = SteamControllerBle(RuntimeEnvironment.getApplication()) + try { + val reports = Writer.reports.size + val connections = Writer.connections.toList() + unused.stop() + assertTrue(SteamControllerBle.connected) + assertEquals(connections, Writer.connections) + assertEquals(reports, Writer.reports.size) + } finally { + unused.stop() + ReflectionHelpers.getField(unused, "handler").looper.quit() + } + } + + private fun subscribe(target: SteamControllerBle) { + val device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice("00:11:22:33:44:55") + val gatt = ShadowBluetoothGatt.newInstance(device) + val input = UUID.fromString("100F6C7A-1735-4313-B402-38567131E5F3") + val characteristic = BluetoothGattCharacteristic(input, BluetoothGattCharacteristic.PROPERTY_NOTIFY, 0) + val descriptor = BluetoothGattDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"), 0) + characteristic.addDescriptor(descriptor) + ReflectionHelpers.setField(target, "gatt", gatt) + ReflectionHelpers.setField(target, "input", input) + ReflectionHelpers.setField(target, "reportId", 0x45) + ReflectionHelpers.getField(target, "callback") + .onDescriptorWrite(gatt, descriptor, BluetoothGatt.GATT_SUCCESS) + } + + private fun state(bytes: ByteArray, target: SteamControllerBle = controller) = ReflectionHelpers.callInstanceMethod(target, "onState", + ReflectionHelpers.ClassParameter.from(ByteArray::class.java, bytes)) + + @Implements(FakeInputWriter::class) + class Writer { + companion object { + val reports = ArrayList() + val connections = ArrayList() + var features = 0 + @JvmStatic @Implementation fun writeTritonState(slot: Int, reportId: Int, report: ByteArray) { reports += report.copyOf() } + @JvmStatic @Implementation fun setTritonConnected(slot: Int, connected: Boolean) { connections += connected } + @JvmStatic @Implementation fun writeTritonFeatureReply(slot: Int, request: Long, report: ByteArray?) { features++ } + } + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/input/StickDeadZoneTest.kt b/app/src/test/java/com/droiddeck/launcher/input/StickDeadZoneTest.kt index 38ebeefca9..bcb071e883 100644 --- a/app/src/test/java/com/droiddeck/launcher/input/StickDeadZoneTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/input/StickDeadZoneTest.kt @@ -400,22 +400,23 @@ class StickDeadZoneTest { for (id in listOf("ls", "rs")) { motion(device, MotionEvent.AXIS_X to 0.6f, MotionEvent.AXIS_Z to 0.7f) val view = OnScreenControls(context, bridge) + val elements = LegacyGamepadLayout(context, 1200, 800).elements() + view.updateLayout(SavedInputLayout("test", "Test", elements)) + view.measure(android.view.View.MeasureSpec.makeMeasureSpec(1200, android.view.View.MeasureSpec.EXACTLY), android.view.View.MeasureSpec.makeMeasureSpec(800, android.view.View.MeasureSpec.EXACTLY)) view.layout(0, 0, 1200, 800) - val controls = ReflectionHelpers.getField>(view, "controls") - val control = controls.first { ReflectionHelpers.getField(it, "id") == id } - val x = ReflectionHelpers.getField(control, "cx") - val y = ReflectionHelpers.getField(control, "cy") + val control = elements.first { it.control.id == id } + val x = (control.x + control.width / 2) * 1200 + val y = (control.y + control.height / 2) * 800 val down = MotionEvent.obtain(1000, 1000, MotionEvent.ACTION_DOWN, x, y, 0) - try { assertTrue(view.onTouchEvent(down)) } finally { down.recycle() } - assertFalse(ReflectionHelpers.getField(control, "dirty")) + try { assertTrue(view.dispatchTouchEvent(down)) } finally { down.recycle() } assertEquals(0f, if (id == "ls") pad().leftX else pad().rightX, 0f) assertTrue((if (id == "ls") pad().rightX else pad().leftX) > 0f) bridge.setStickDeadZone(25) assertEquals(0f, if (id == "ls") pad().leftX else pad().rightX, 0f) motion(device, MotionEvent.AXIS_X to 0.6f, MotionEvent.AXIS_Z to 0.7f) - val button = controls.first { ReflectionHelpers.getField(it, "id") == "a" } - val bx = ReflectionHelpers.getField(button, "cx") - val by = ReflectionHelpers.getField(button, "cy") + val button = elements.first { it.control == InputControl.A } + val bx = (button.x + button.width / 2) * 1200 + val by = (button.y + button.height / 2) * 800 fun buttonTouch(action: Int, time: Long) { val event = MotionEvent.obtain(1000, time, action or (1 shl MotionEvent.ACTION_POINTER_INDEX_SHIFT), 2, arrayOf(MotionEvent.PointerProperties().apply { this.id = 0; toolType = MotionEvent.TOOL_TYPE_FINGER }, @@ -423,7 +424,7 @@ class StickDeadZoneTest { arrayOf(MotionEvent.PointerCoords().apply { this.x = x; this.y = y }, MotionEvent.PointerCoords().apply { this.x = bx; this.y = by }), 0, 0, 1f, 1f, 0, 0, InputDevice.SOURCE_TOUCHSCREEN, 0) - try { assertTrue(view.onTouchEvent(event)) } finally { event.recycle() } + try { assertTrue(view.dispatchTouchEvent(event)) } finally { event.recycle() } } buttonTouch(MotionEvent.ACTION_POINTER_DOWN, 1030) assertTrue(pad().isDown(PadState.A)) @@ -435,7 +436,7 @@ class StickDeadZoneTest { assertEquals(0.6f, pad().leftX, 0f) assertEquals(0.7f, pad().rightX, 0f) val up = MotionEvent.obtain(1000, 1100, MotionEvent.ACTION_UP, x, y, 0) - try { assertTrue(view.onTouchEvent(up)) } finally { up.recycle() } + try { assertTrue(view.dispatchTouchEvent(up)) } finally { up.recycle() } } } } diff --git a/app/src/test/java/com/droiddeck/launcher/input/TritonInputRouterTest.kt b/app/src/test/java/com/droiddeck/launcher/input/TritonInputRouterTest.kt new file mode 100644 index 0000000000..a267d6d8ce --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/input/TritonInputRouterTest.kt @@ -0,0 +1,47 @@ +package com.droiddeck.launcher.input + +import org.junit.Assert.* +import org.junit.Test + +class TritonInputRouterTest { + private val events = ArrayList() + private val router = InputRouter(object : InputOutput { + override fun key(code: Int, down: Boolean) { events += "$code $down" } + override fun button(code: Int, down: Boolean) {} + override fun pointer(dx: Float, dy: Float) {} + override fun scroll(steps: Int) {} + override fun command(command: InputCommand) {} + override fun requestMotion() {} + }) + + @Test fun nativeReportIsPassedThroughWithoutCopyingOrReencoding() { + val adapter = TritonInputRouter(router) + val bytes = ByteArray(64) { (it * 11).toByte() } + assertSame(bytes, adapter.route(bytes)) + assertTrue(events.isEmpty()) + adapter.close() + } + + @Test fun consumedButtonDoesNotChangeOtherBitsAxesPressureMotionOrExtension() { + router.setProfile(InputProfile.defaults(InputProfile.GAME).withBinding(InputRole.PHYSICAL, InputControl.A, InputAction.Keys(listOf(57)))) + val adapter = TritonInputRouter(router) + val bytes = ByteArray(64) { (it * 7).toByte() } + bytes[1] = (bytes[1].toInt() or 1).toByte() + val expected = bytes.copyOf().apply { this[1] = (this[1].toInt() and 1.inv()).toByte() } + assertArrayEquals(expected, adapter.route(bytes)) + assertEquals(listOf("57 true"), events) + adapter.release() + assertEquals(listOf("57 true", "57 false"), events) + } + + @Test fun switchingProfileWhileNativeButtonHeldRequiresRelease() { + val adapter = TritonInputRouter(router) + val held = ByteArray(45).apply { this[1] = 1 } + adapter.route(held) + router.setProfile(InputProfile.defaults(InputProfile.GAME).withBinding(InputRole.PHYSICAL, InputControl.A, InputAction.Keys(listOf(57)))) + adapter.route(held) + assertTrue(events.isEmpty()) + adapter.route(ByteArray(45)); adapter.route(held) + assertEquals(listOf("57 true"), events) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/session/SettingsResetTest.kt b/app/src/test/java/com/droiddeck/launcher/session/SettingsResetTest.kt index e95a49608a..f25207a697 100644 --- a/app/src/test/java/com/droiddeck/launcher/session/SettingsResetTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/session/SettingsResetTest.kt @@ -4,6 +4,12 @@ import android.content.Context import com.droiddeck.launcher.core.AppUiPrefs import com.droiddeck.launcher.gpu.FrameGen import com.droiddeck.launcher.input.ControllerPrefs +import com.droiddeck.launcher.input.InputAction +import com.droiddeck.launcher.input.InputControl +import com.droiddeck.launcher.input.InputProfile +import com.droiddeck.launcher.input.InputProfiles +import com.droiddeck.launcher.input.InputRole +import com.droiddeck.launcher.input.InputSurfaceLayout import org.junit.Assert.* import org.junit.Before import org.junit.Test @@ -20,7 +26,7 @@ class SettingsResetTest { private val session get() = prefs("session") @Before fun clear() { - listOf("session", "controller", "app-ui", "frame_gen", "steam_touch", "agent").forEach { + listOf("session", "controller", "input-profiles", "input-surfaces", "app-ui", "frame_gen", "steam_touch", "agent").forEach { prefs(it).edit().clear().commit() } } @@ -119,6 +125,24 @@ class SettingsResetTest { assertEquals("a:1,2", prefs("steam_touch").getString("copiedLayout", null)) } + @Test fun inputProfilesAndSurfacesResetWithoutRestoringLegacyMappings() { + val defaultGame = InputProfiles.read(app, InputProfile.GAME) + val defaultDesktop = InputProfiles.read(app, InputProfile.DESKTOP) + val defaultSurface = InputSurfaceLayout.read(app, InputProfile.DESKTOP) + ControllerPrefs.setTarget(app, "a", "b") + InputProfiles.select(app, InputProfile.DESKTOP) + InputProfiles.save(app, InputProfile.defaults(InputProfile.GAME) + .withBinding(InputRole.PHYSICAL, InputControl.A, InputAction.Disabled)) + InputSurfaceLayout.save(app, InputProfile.DESKTOP, emptyList()) + + assertTrue(SettingsReset.resetAll(app, gameProfiles = false)) + + assertEquals(InputProfiles.AUTO, InputProfiles.selection(app)) + assertEquals(defaultGame, InputProfiles.read(app, InputProfile.GAME)) + assertEquals(defaultDesktop, InputProfiles.read(app, InputProfile.DESKTOP)) + assertEquals(defaultSurface, InputSurfaceLayout.read(app, InputProfile.DESKTOP)) + } + @Test fun gameTextureFilteringIsKeptUnlessGameProfilesAreReset() { session.edit().putString("gameTextureFiltering", """{"440":{"anisotropy":16}}""").commit() SettingsReset.resetAll(app, gameProfiles = false) diff --git a/app/src/test/java/com/droiddeck/launcher/ui/GamepadGeometryTest.kt b/app/src/test/java/com/droiddeck/launcher/ui/GamepadGeometryTest.kt index 06f4dedfd7..dbb153bce9 100644 --- a/app/src/test/java/com/droiddeck/launcher/ui/GamepadGeometryTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/ui/GamepadGeometryTest.kt @@ -1,7 +1,8 @@ package com.droiddeck.launcher.ui import com.droiddeck.launcher.session.SessionDisplay -import com.droiddeck.launcher.ui.GamepadGeometry.Slot +import com.droiddeck.launcher.input.GamepadGeometry +import com.droiddeck.launcher.input.GamepadGeometry.Slot import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse import org.junit.Assert.assertTrue diff --git a/app/src/test/java/com/droiddeck/launcher/ui/InputCaptureDialogTest.kt b/app/src/test/java/com/droiddeck/launcher/ui/InputCaptureDialogTest.kt new file mode 100644 index 0000000000..3afeba08bf --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/ui/InputCaptureDialogTest.kt @@ -0,0 +1,84 @@ +package com.droiddeck.launcher.ui + +import android.hardware.input.InputManager +import android.view.InputDevice +import android.view.KeyCharacterMap +import android.view.KeyEvent +import androidx.compose.runtime.mutableStateOf +import androidx.compose.ui.test.* +import com.droiddeck.launcher.R +import com.droiddeck.launcher.input.PadBridgeRoutingTest +import com.droiddeck.launcher.input.PhysicalInputKey +import org.junit.Assert.* +import org.junit.Before +import org.junit.Rule +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.Shadows.shadowOf +import org.robolectric.annotation.Config +import org.robolectric.shadows.InputDeviceBuilder +import org.robolectric.shadows.ShadowDialog + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [34], qualifiers = "w960dp-h640dp-land", shadows = [PadBridgeRoutingTest.RegisteredKeyEvent::class]) +class InputCaptureDialogTest { + @get:Rule val compose = createRobolectricComposeRule() + private val context get() = RuntimeEnvironment.getApplication() + private val open = mutableStateOf(true) + private var selected: PhysicalInputKey? = null + private lateinit var device: InputDevice + + @Before fun setUp() { + device = InputDeviceBuilder.newBuilder().setId(10).setName("Thor buttons") + .setDescriptor("thor-extra").setSources(InputDevice.SOURCE_KEYBOARD) + .setKeyboardType(InputDevice.KEYBOARD_TYPE_ALPHABETIC) + .setKeyCharacterMap(KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD)).build() + shadowOf(context.getSystemService(InputManager::class.java)).addInputDevice(device) + compose.setContent { DroidDeckTheme { + if (open.value) InputCaptureDialog({ open.value = false }) { selected = it; open.value = false } + } } + } + + @Test fun physicalBackCanBeCapturedAndConfirmedWithControllerA() { + send(KeyEvent.KEYCODE_BACK, KeyEvent.ACTION_DOWN) + compose.onNodeWithText(context.getString(R.string.ctrl_edit)).assertIsNotEnabled() + assertTrue(open.value) + send(KeyEvent.KEYCODE_BACK, KeyEvent.ACTION_UP) + compose.onNodeWithText(context.getString(R.string.ctrl_edit)).assertIsEnabled() + assertNull(selected) + send(KeyEvent.KEYCODE_BUTTON_A, KeyEvent.ACTION_DOWN) + assertEquals(KeyEvent.KEYCODE_BACK, selected?.keyCode) + assertFalse(open.value) + } + + @Test fun controllerBCanBeCapturedAndAnotherPressCancelsWithoutSaving() { + send(KeyEvent.KEYCODE_BUTTON_B, KeyEvent.ACTION_DOWN) + send(KeyEvent.KEYCODE_BUTTON_B, KeyEvent.ACTION_UP) + assertTrue(open.value) + send(KeyEvent.KEYCODE_BUTTON_B, KeyEvent.ACTION_DOWN) + assertFalse(open.value) + assertNull(selected) + } + + @Test fun retryAllowsAnotherButtonAndConsumesItsReleaseBeforeConfirmation() { + send(KeyEvent.KEYCODE_F1, KeyEvent.ACTION_DOWN) + send(KeyEvent.KEYCODE_F1, KeyEvent.ACTION_UP) + compose.onNodeWithText(context.getString(R.string.input_capture_retry)).performClick() + send(KeyEvent.KEYCODE_BUTTON_A, KeyEvent.ACTION_DOWN) + send(KeyEvent.KEYCODE_BUTTON_A, KeyEvent.ACTION_UP) + assertNull(selected) + assertTrue(open.value) + send(KeyEvent.KEYCODE_BUTTON_A, KeyEvent.ACTION_DOWN) + assertEquals(KeyEvent.KEYCODE_BUTTON_A, selected?.keyCode) + } + + private fun send(code: Int, action: Int) { + compose.runOnIdle { + assertTrue(ShadowDialog.getLatestDialog().window!!.callback.dispatchKeyEvent( + KeyEvent(100L, 100L, action, code, 0, 0, device.id, 700, 0, InputDevice.SOURCE_KEYBOARD))) + } + compose.waitForIdle() + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/ui/InputSettingsTest.kt b/app/src/test/java/com/droiddeck/launcher/ui/InputSettingsTest.kt new file mode 100644 index 0000000000..530f18c5a8 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/ui/InputSettingsTest.kt @@ -0,0 +1,382 @@ +package com.droiddeck.launcher.ui + +import android.content.Context +import android.app.Presentation +import android.hardware.display.DisplayManager +import android.view.Display +import androidx.compose.runtime.mutableStateOf +import androidx.compose.ui.test.* +import com.droiddeck.launcher.input.* +import com.droiddeck.launcher.R +import com.droiddeck.launcher.session.SessionState +import com.droiddeck.launcher.session.SessionService +import org.junit.Assert.* +import org.junit.Before +import org.junit.Rule +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.Shadows.shadowOf +import org.robolectric.shadows.ShadowDialog +import org.robolectric.shadows.ShadowDisplayManager +import org.robolectric.util.ReflectionHelpers +import org.robolectric.annotation.Config +import org.robolectric.annotation.GraphicsMode + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [28], qualifiers = "w1000dp-h900dp") +@GraphicsMode(GraphicsMode.Mode.NATIVE) +class InputSettingsTest { + @get:Rule val compose = createRobolectricComposeRule() + private val context get() = RuntimeEnvironment.getApplication() + @Before fun reset() { + context.getSharedPreferences("input-profiles", Context.MODE_PRIVATE).edit().clear().commit() + context.getSharedPreferences("input-surfaces", Context.MODE_PRIVATE).edit().clear().commit() + 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 + compose.setContent { DroidDeckTheme { + ModeSheetRows(ModeSettings(mode = mode.value, resolution = "auto", hdr = false, hdrReason = null, + touchMode = "auto", suspendPolicy = "auto", oscMode = "auto", directAudio = null, mic = null), + ModeSettingsActions(onResolution = {}, onHdr = {}, onTouch = {}, onSuspendPolicy = {}, onOsc = {}, + onInputSettings = { opened++ }, onDirectAudio = {}, onMic = {}, onDismiss = {}), SheetTab.CONTROLS) + } } + compose.onNodeWithTag("input-profile-expand").performClick() + compose.onNodeWithText(context.getString(R.string.ctrl_edit)).performClick() + compose.runOnIdle { assertEquals(1, opened); mode.value = SessionService.MODE_DESKTOP } + compose.onNodeWithText(context.getString(R.string.ctrl_edit)).performClick() + compose.runOnIdle { assertEquals(2, opened) } + } + + @Test fun quickMenuCombinesPartsWithoutChangingTheSavedProfile() { + val original = InputLibraries.read(context) + compose.setContent { DroidDeckTheme { + androidx.compose.foundation.layout.Column { InputQuickSettings(rememberMenuHost(), desktop = true) } + } } + compose.onNodeWithTag("setting-input-profile").assertExists() + compose.onNodeWithTag("setting-input-part-PHYSICAL").assertDoesNotExist() + compose.onNodeWithTag("input-profile-expand").performClick() + compose.onNodeWithTag("setting-input-part-PHYSICAL").assertExists() + compose.onNodeWithTag("setting-input-part-PRIMARY").assertExists() + compose.onNodeWithTag("setting-input-part-SECONDARY").performClick() + compose.onNodeWithText("Deck grips + trackpads").performScrollTo().performClick() + compose.runOnIdle { + val changed = InputLibraries.read(context) + assertEquals(InputStockLayouts.DECK, changed.activePart(true, InputRole.SECONDARY).bindings) + assertEquals(original.profiles, changed.profiles) + assertEquals(original.compileActive(false), changed.compileActive(false)) + } + compose.onNodeWithText(context.getString(R.string.input_save_combination)).performClick() + compose.onNode(hasSetTextAction()).performTextInput("Reading") + compose.onNodeWithText(context.getString(R.string.gesture_save_changes)).performClick() + compose.runOnIdle { + val saved = InputLibraries.read(context) + assertEquals("Reading", saved.profile(saved.activeId(true)).name) + assertTrue(saved.selection(true).parts.isEmpty()) + assertEquals(original.profile("desktop"), saved.profile("desktop")) + } + } + + @Test fun actionDraftSurvivesRouteAndTypeChanges() { + var saved: InputAction? = null + compose.setContent { DroidDeckTheme { InputActionEditor(InputControl.RS, InputAction.Scroll(27f, true), "Right stick", {}, { saved = it }) } } + compose.onNodeWithText(context.getString(R.string.input_route_controller), substring = false).performClick() + compose.onNodeWithText(context.getString(R.string.input_disabled), substring = false).performClick() + compose.onNodeWithText(context.getString(R.string.input_route_local), substring = false).performClick() + compose.onNodeWithText(context.getString(R.string.input_scroll_speed, 27)).assertExists() + compose.onNodeWithTag("setting-action-type").performClick() + compose.onNodeWithText(context.getString(R.string.input_mouse_click)).performClick() + compose.onNodeWithTag("setting-action-type").performClick() + compose.onNodeWithText(context.getString(R.string.input_scroll)).performClick() + compose.onNodeWithTag("input-action-save").performClick() + compose.runOnIdle { assertEquals(InputAction.Scroll(27f, true), saved) } + } + + @Test fun bindingEditorPersistsDisabledWithoutChangingTouchControls() { + var changed = false + compose.setContent { DroidDeckTheme { InputSettingsPage({}, { changed = true }) } } + compose.onNodeWithTag("input-profile-game").performScrollTo().performClick() + compose.onNodeWithTag("input-part-physical").performScrollTo().performClick() + compose.onNodeWithTag("input-binding-a").performScrollTo().performClick() + compose.onNodeWithText("Disabled", substring = false).performScrollTo().performClick() + compose.runOnIdle { assertFalse(changed) } + compose.onNodeWithTag("input-action-save").performClick() + compose.runOnIdle { + assertTrue(changed) + val profile = InputProfiles.read(context, InputProfile.GAME) + assertEquals(InputAction.Disabled, profile.action(InputRole.PHYSICAL, InputControl.A)) + assertEquals(InputAction.Native(), profile.action(InputRole.SECONDARY, InputControl.A)) + } + } + + @Test fun addingCreatesAnUnassignedElementAtTheCanvasCenterWithoutAnotherTouch() { + var changed = false + val library = InputLibraries.read(context) + val layout = library.layout(InputProfile.GAME, InputRole.SECONDARY)!! + compose.setContent { DroidDeckTheme { InputSurfaceDialog(InputProfile.GAME, {}, { changed = true }, + role = InputRole.PRIMARY, layoutId = layout.id, bindingsId = library.profile(InputProfile.GAME).binding(InputRole.SECONDARY)) } } + compose.onNodeWithTag("input-editor-tools").performClick() + compose.onNodeWithText(context.getString(R.string.input_editor_add)).performClick() + compose.onNodeWithText(context.getString(R.string.input_element_button), substring = false).performClick() + compose.onNodeWithTag("input-editor-tools").performClick() + compose.onNodeWithText(context.getString(R.string.input_properties)).assertExists() + compose.onNodeWithText(context.getString(R.string.input_editor_done)).performClick() + compose.runOnIdle { + assertTrue(changed) + val updated = InputLibraries.read(context) + val added = updated.layout(InputProfile.GAME, InputRole.SECONDARY)!!.elements.single { e -> layout.elements.none { it.id == e.id } } + assertEquals(InputAction.Disabled, updated.compile(InputProfile.GAME).elementAction(InputRole.SECONDARY, added.id, added.control)) + assertEquals(.5f, added.x + added.width / 2, .01f) + } + } + + @Test fun aNewProfileIsIndependentAndDoesNotBecomeActive() { + compose.setContent { DroidDeckTheme { InputSettingsPage({}) } } + compose.onNodeWithText(context.getString(R.string.input_new_profile)).performScrollTo().performClick() + compose.onNodeWithTag("input-name").performTextInput("Minecraft") + compose.onNodeWithText(context.getString(R.string.gesture_save_changes)).performClick() + compose.runOnIdle { + val library = InputLibraries.read(context) + val created = library.profiles.single { it.name == "Minecraft" } + for (role in InputRole.entries) assertNotEquals(library.profile(InputProfile.GAME).binding(role), created.binding(role)) + assertNotEquals(library.layout(InputProfile.GAME, InputRole.SECONDARY)!!.id, library.layout(created.id, InputRole.SECONDARY)!!.id) + assertEquals(InputProfiles.AUTO, library.selected) + } + } + + @Test fun openingAProfileDoesNotActivateItAndBackKeepsItsContext() { + compose.setContent { DroidDeckTheme { InputSettingsPage({}) } } + compose.onNodeWithTag("input-profile-desktop").performScrollTo().performClick() + compose.onNodeWithTag("input-part-physical").performScrollTo().performClick() + compose.onNodeWithTag("input-binding-a").performScrollTo().performClick() + compose.onNodeWithTag("input-action-save").performClick() + compose.onNodeWithTag("input-binding-a").assertExists() + compose.onNodeWithText(context.getString(R.string.mode_back)).performClick() + compose.onNodeWithTag("input-part-secondary").assertExists() + compose.runOnIdle { assertEquals(InputProfiles.AUTO, InputLibraries.read(context).selected) } + } + + @Test fun profileCreationCanExplicitlyShareOnlyPhysicalControls() { + compose.setContent { DroidDeckTheme { InputSettingsPage({}) } } + compose.onNodeWithText(context.getString(R.string.input_new_profile)).performScrollTo().performClick() + compose.onNodeWithTag("input-name").performTextInput("Shared controls") + compose.onNodeWithTag("input-share-physical").performClick() + compose.onNodeWithText(context.getString(R.string.gesture_save_changes)).performClick() + compose.runOnIdle { + val library = InputLibraries.read(context) + val created = library.profiles.single { it.name == "Shared controls" } + assertEquals(library.profile("game").binding(InputRole.PHYSICAL), created.binding(InputRole.PHYSICAL)) + assertNotEquals(library.profile("game").binding(InputRole.SECONDARY), created.binding(InputRole.SECONDARY)) + } + } + + @Test fun hidingEditorToolsPreservesPlacedElements() { + val library = InputLibraries.read(context) + val layout = library.layout("game", InputRole.SECONDARY)!! + compose.setContent { DroidDeckTheme { InputSurfaceDialog("game", {}, {}, role = InputRole.PRIMARY, + layoutId = layout.id, bindingsId = library.profile("game").binding(InputRole.SECONDARY)) } } + compose.onNodeWithTag("input-editor-tools").performClick() + compose.onNodeWithText(context.getString(R.string.input_editor_add)).performClick() + compose.onNodeWithText(context.getString(R.string.input_element_button)).performClick() + compose.onNodeWithTag("input-layout-canvas").performTouchInput { click(center) } + compose.onNodeWithTag("input-editor-tools").performClick() + compose.onNodeWithText(context.getString(R.string.input_hide_tools)).performClick() + compose.onNodeWithText(context.getString(R.string.input_editor_add)).assertDoesNotExist() + compose.onNodeWithTag("input-editor-tools").performClick() + compose.onNodeWithText(context.getString(R.string.input_editor_done)).performClick() + compose.runOnIdle { assertEquals(layout.elements.size + 1, InputLibraries.read(context).layout("game", InputRole.SECONDARY)!!.elements.size) } + } + + @Test fun localToolsCanMoveAndExpandWithoutMovingTheCanvas() { + val library = InputLibraries.read(context) + compose.setContent { DroidDeckTheme { InputSurfaceDialog("game", {}, {}, role = InputRole.PRIMARY, + layoutId = library.layout("game", InputRole.SECONDARY)!!.id, bindingsId = library.profile("game").binding(InputRole.SECONDARY)) } } + compose.runOnIdle { + val window = ShadowDialog.getLatestDialog().window!! + assertEquals(android.view.WindowManager.LayoutParams.MATCH_PARENT, window.attributes.width) + assertEquals(android.view.WindowManager.LayoutParams.MATCH_PARENT, window.attributes.height) + } + val canvas = compose.onNodeWithTag("input-layout-canvas").fetchSemanticsNode().boundsInRoot + val before = compose.onNodeWithTag("input-editor-tools").fetchSemanticsNode().boundsInRoot + compose.onNodeWithTag("input-editor-tools").performTouchInput { swipe(center, center + androidx.compose.ui.geometry.Offset(250f, 150f), 500) } + val after = compose.onNodeWithTag("input-editor-tools").fetchSemanticsNode().boundsInRoot + assertTrue(after.left > before.left + 100f) + compose.onNodeWithTag("input-editor-tools").performClick() + compose.onNodeWithText(context.getString(R.string.input_editor_add)).assertIsDisplayed() + assertEquals(canvas, compose.onNodeWithTag("input-layout-canvas").fetchSemanticsNode().boundsInRoot) + compose.onNodeWithTag("input-editor-tools").performClick() + compose.onNodeWithText(context.getString(R.string.input_editor_add)).assertDoesNotExist() + } + + @Test fun deckPresetOffersItsRealElementsAndTheCommonEditor() { + InputLibraries.update(context) { it.useLayout("game", InputRole.SECONDARY, InputLibraries.DECK, shared = true) } + compose.setContent { DroidDeckTheme { InputSettingsPage({}) } } + compose.onNodeWithTag("input-profile-game").performScrollTo().performClick() + compose.onNodeWithTag("input-part-secondary").performScrollTo().performClick() + compose.onNodeWithTag("input-element-legacy:l4").performScrollTo().assertIsDisplayed() + compose.onNodeWithTag("input-binding-a").assertDoesNotExist() + } + + @Test fun mouseTrackpadPresetIsEditableLikeAnyOtherLayout() { + InputLibraries.update(context) { it.useLayout("game", InputRole.SECONDARY, InputLibraries.TRACKPAD, shared = true) } + compose.setContent { DroidDeckTheme { InputSettingsPage({}) } } + compose.onNodeWithTag("input-profile-game").performScrollTo().performClick() + compose.onNodeWithTag("input-part-secondary").performScrollTo().performClick() + compose.onNodeWithTag("input-element-legacy:right-pad").performScrollTo().assertIsDisplayed() + compose.onNodeWithTag("input-surface-secondary").performScrollTo().assertIsDisplayed() + compose.onNodeWithTag("input-binding-a").assertDoesNotExist() + } + + @Test fun primaryScreenOffersItsActualElementsAndTheCommonEditor() { + compose.setContent { DroidDeckTheme { InputSettingsPage({}) } } + compose.onNodeWithTag("input-profile-game").performScrollTo().performClick() + compose.onNodeWithTag("input-part-primary").performScrollTo().performClick() + compose.onNodeWithTag("input-element-legacy:a").performScrollTo().assertIsDisplayed() + compose.onNodeWithTag("input-binding-a").assertDoesNotExist() + compose.onNodeWithTag("input-surface-primary").performScrollTo().performClick() + compose.onNodeWithTag("input-editor-tools").performClick() + compose.onNodeWithText(context.getString(R.string.input_editor_add)).assertIsDisplayed() + compose.onNodeWithTag("input-layout-canvas").assertExists() + } + + @Test fun deviceSettingsAreInlineBelowProfiles() { + compose.setContent { DroidDeckTheme { InputSettingsPage({}) } } + compose.onNodeWithTag("input-more").assertDoesNotExist() + compose.onNodeWithText(context.getString(R.string.ctrl_dead_zone)).performScrollTo().assertIsDisplayed() + compose.onNodeWithTag("input-profile-game").performScrollTo().assertIsDisplayed() + } + + @Test fun migratedPadClicksRemainEditableWithoutChangingMovement() { + compose.setContent { DroidDeckTheme { InputSettingsPage({}) } } + compose.onNodeWithTag("input-profile-desktop").performScrollTo().performClick() + compose.onNodeWithTag("input-part-secondary").performScrollTo().performClick() + compose.onNodeWithTag("input-click-legacy:right-pad").performScrollTo().performClick() + compose.onNodeWithText("Disabled", substring = false).performScrollTo().performClick() + compose.onNodeWithTag("input-action-save").performClick() + compose.runOnIdle { + val library = InputLibraries.read(context) + val pad = library.layout("desktop", InputRole.SECONDARY)!!.elements.first { it.control == InputControl.RIGHT_PAD } + val profile = library.compile("desktop") + assertEquals(InputAction.Disabled, profile.elementClickAction(InputRole.SECONDARY, pad)) + assertEquals(InputAction.Pointer(), profile.elementAction(InputRole.SECONDARY, pad.id, pad.control)) + } + } + + @Test @Config(sdk = [34]) fun secondarySettingsCanOpenAnActivityDialogAndCloseCleanly() { + val displayId = ShadowDisplayManager.addDisplay("w640dp-h480dp", ReflectionHelpers.getStaticField(Display::class.java, "TYPE_EXTERNAL")) + val display = context.getSystemService(DisplayManager::class.java).getDisplay(displayId) + shadowOf(display).setFlags(Display.FLAG_PRESENTATION) + val open = mutableStateOf(true) + try { + compose.setContent { DroidDeckTheme { + val host = androidx.compose.ui.platform.LocalContext.current.findActivity() as androidx.activity.ComponentActivity + if (open.value) InputDisplayContent(host, display, { open.value = false }) { InputSettingsPage({ open.value = false }) } + } } + compose.onNodeWithText(context.getString(R.string.input_new_profile)).performScrollTo().performClick() + compose.onNodeWithTag("input-name").assertExists() + compose.runOnIdle { + assertTrue(ShadowDialog.getShownDialogs().filterIsInstance().any { it.isShowing && it.display.displayId == displayId }) + val dialog = ShadowDialog.getShownDialogs().last { it !is Presentation && it.isShowing } + assertEquals(Display.DEFAULT_DISPLAY, dialog.window?.decorView?.display?.displayId) + open.value = false + } + compose.runOnIdle { assertTrue(ShadowDialog.getShownDialogs().none { it.isShowing }) } + } finally { ShadowDisplayManager.removeDisplay(displayId) } + } + + @Test @Config(sdk = [34]) fun unavailableDisplayDismissesSettingsWithoutCrashing() { + val displayId = ShadowDisplayManager.addDisplay("w640dp-h480dp", ReflectionHelpers.getStaticField(Display::class.java, "TYPE_EXTERNAL")) + val display = context.getSystemService(DisplayManager::class.java).getDisplay(displayId) + shadowOf(display).setFlags(Display.FLAG_PRESENTATION) + ShadowDisplayManager.removeDisplay(displayId) + val open = mutableStateOf(true) + compose.setContent { DroidDeckTheme { + val host = androidx.compose.ui.platform.LocalContext.current.findActivity() as androidx.activity.ComponentActivity + if (open.value) InputDisplayContent(host, display, { open.value = false }) { InputSettingsPage({}) } + } } + compose.runOnIdle { + assertFalse(open.value) + assertTrue(ShadowDialog.getShownDialogs().none { it.isShowing }) + } + } + + @Test @Config(sdk = [34]) fun mainEditorOpenedFromSecondarySettingsUsesAnActivityWindow() { + val displayId = ShadowDisplayManager.addDisplay("w640dp-h480dp", ReflectionHelpers.getStaticField(Display::class.java, "TYPE_EXTERNAL")) + val display = context.getSystemService(DisplayManager::class.java).getDisplay(displayId) + shadowOf(display).setFlags(Display.FLAG_PRESENTATION) + val open = mutableStateOf(true) + try { + compose.setContent { DroidDeckTheme { + val host = androidx.compose.ui.platform.LocalContext.current.findActivity() as androidx.activity.ComponentActivity + if (open.value) InputDisplayContent(host, display, { open.value = false }) { + InputSurfaceDialog("game", { open.value = false }, {}, role = InputRole.PRIMARY) + } + } } + compose.onNodeWithTag("input-layout-canvas").assertExists() + compose.runOnIdle { + assertTrue(ShadowDialog.getShownDialogs().none { it is Presentation && it.display.displayId == Display.DEFAULT_DISPLAY }) + val editor = ShadowDialog.getShownDialogs().last { it !is Presentation && it.isShowing } + assertEquals(Display.DEFAULT_DISPLAY, editor.window?.decorView?.display?.displayId) + open.value = false + } + compose.runOnIdle { assertTrue(ShadowDialog.getShownDialogs().none { it.isShowing }) } + } finally { ShadowDisplayManager.removeDisplay(displayId) } + } + + @Test @Config(sdk = [34]) fun auxiliaryEditorUsesThatDisplayAndSurvivesItsRemoval() { + val displayId = ShadowDisplayManager.addDisplay("w640dp-h480dp", ReflectionHelpers.getStaticField(Display::class.java, "TYPE_EXTERNAL")) + val display = context.getSystemService(DisplayManager::class.java).getDisplay(displayId) + shadowOf(display).setFlags(Display.FLAG_PRESENTATION) + assertTrue(SecondScreenDisplays.available(context.getSystemService(DisplayManager::class.java)).any { it.id == displayId }) + val previous = SessionState.secondScreenDisplay + SessionState.secondScreenDisplay = displayId + val open = mutableStateOf(true) + try { + compose.setContent { DroidDeckTheme { if (open.value) InputSurfaceDialog("game", { open.value = false }, {}) } } + compose.onNodeWithText(context.getString(R.string.input_editor_remote)).assertExists() + compose.onAllNodesWithText(context.getString(R.string.input_editor_add)).assertCountEquals(1) + compose.onNodeWithText(context.getString(R.string.input_hide_tools)).assertDoesNotExist() + assertEquals(0f, compose.onNodeWithTag("input-layout-canvas").fetchSemanticsNode().boundsInRoot.top, 1f) + compose.runOnIdle { + assertTrue(ShadowDialog.getShownDialogs().filterIsInstance().any { it.isShowing && it.display.displayId == displayId }) + } + compose.onNodeWithTag("input-layout-canvas").performTouchInput { click(percentOffset(.25f, .25f)) } + compose.onAllNodesWithText(context.getString(R.string.input_properties)).onFirst().performClick() + compose.onNodeWithTag("input-action-save").assertIsDisplayed() + compose.onNodeWithText("Disabled", substring = false).performScrollTo().performClick() + compose.runOnIdle { + assertTrue(ShadowDialog.getShownDialogs().any { it !is Presentation && it.isShowing && it.window?.decorView?.display?.displayId == Display.DEFAULT_DISPLAY }) + ShadowDisplayManager.removeDisplay(displayId) + } + compose.onNodeWithTag("input-action-save").assertIsDisplayed() + compose.onNodeWithTag("input-action-save").performClick() + compose.onNodeWithText(context.getString(R.string.input_display_unavailable)).assertExists() + compose.onNodeWithText(context.getString(R.string.input_editor_done)).performScrollTo().performClick() + compose.runOnIdle { + assertFalse(open.value) + assertEquals(InputAction.Disabled, InputProfiles.read(context, "game").elementAction(InputRole.SECONDARY, "legacy:left-pad", InputControl.LEFT_PAD)) + } + } finally { + SessionState.secondScreenDisplay = previous + if (display.isValid) ShadowDisplayManager.removeDisplay(displayId) + } + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/ui/InputSurfaceGesturesTest.kt b/app/src/test/java/com/droiddeck/launcher/ui/InputSurfaceGesturesTest.kt new file mode 100644 index 0000000000..78cdd27115 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/ui/InputSurfaceGesturesTest.kt @@ -0,0 +1,120 @@ +package com.droiddeck.launcher.ui + +import android.os.Looper +import android.view.MotionEvent +import android.view.View +import android.view.ViewGroup +import androidx.activity.ComponentActivity +import com.droiddeck.launcher.input.* +import org.junit.After +import org.junit.Assert.* +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.Robolectric +import org.robolectric.RobolectricTestRunner +import org.robolectric.Shadows.shadowOf +import org.robolectric.annotation.Config +import java.time.Duration + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [34], shadows = [JoyConPadTest.RecordingWriter::class]) +class InputSurfaceGesturesTest { + private val controller = Robolectric.buildActivity(ComponentActivity::class.java).setup() + private val activity = controller.get() + private val events = mutableListOf() + private val moves = mutableListOf>() + private val bridge = PadBridge(activity.cacheDir) + private val router = InputRouter(object : InputOutput { + override fun key(code: Int, down: Boolean) { events += "key:$code:$down" } + override fun button(code: Int, down: Boolean) { events += "button:$code:$down" } + override fun pointer(dx: Float, dy: Float) { moves += dx to dy } + override fun scroll(steps: Int) { events += "scroll:$steps" } + override fun command(command: InputCommand) {} + override fun requestMotion() { error("Touchpad must not schedule stick frames") } + }) + private lateinit var panel: InputSurfaceView + private lateinit var pad: View + private var time = 1000L + private val element = InputSurfaceElement(InputControl.RIGHT_PAD, 0f, 0f, 1f, 1f, mouseGestures = true) + private fun profile(action: InputAction = InputAction.Pointer()) = InputProfile("test", + elements = mapOf(InputRole.SECONDARY to mapOf(element.id to action)), + clicks = mapOf(InputRole.SECONDARY to mapOf(element.id to InputAction.MouseButton(272)))) + + @Before fun setup() { + bridge.setInputRouter(router) + bridge.setInputProfile(profile()) + panel = InputSurfaceView(activity, bridge, listOf(element)) + activity.setContentView(panel) + panel.measure(View.MeasureSpec.makeMeasureSpec(1000, View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(600, View.MeasureSpec.EXACTLY)) + panel.layout(0, 0, 1000, 600) + pad = descendants(panel).first { it.javaClass.simpleName == "TrackpadView" } + } + @After fun close() { panel.releaseInputs(); bridge.stop(); controller.pause().stop().destroy() } + + @Test fun tapClicksWithoutRepositioningCursorFromAnotherSource() { + touch(MotionEvent.ACTION_DOWN); touch(MotionEvent.ACTION_UP) + shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(100)) + assertEquals(listOf("button:272:true", "button:272:false"), events) + assertTrue(moves.isEmpty()) + val other = router.channel(InputRole.PHYSICAL, InputControl.LS, fixedAction = InputAction.Pointer()) + other.update(0f, 0f, true); other.pointerDelta(.1f, .2f); other.update(0f) + val positionChanges = moves.toList() + time += 1000 + touch(MotionEvent.ACTION_DOWN); touch(MotionEvent.ACTION_UP) + assertEquals(positionChanges, moves) + other.close() + } + + @Test fun tapUsesTheConfiguredElementClick() { + bridge.setInputProfile(profile().copy(clicks = mapOf(InputRole.SECONDARY to mapOf(element.id to InputAction.Keys(listOf(57)))))) + touch(MotionEvent.ACTION_DOWN); touch(MotionEvent.ACTION_UP) + shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(100)) + assertEquals(listOf("key:57:true", "key:57:false"), events) + } + + @Test fun motionDeliversImmediatelyAndTwoFingerScrollDoesNotMoveThePointer() { + touch(MotionEvent.ACTION_DOWN) + touch(MotionEvent.ACTION_MOVE, y = 230f) + assertTrue(moves.isNotEmpty()) + touch(MotionEvent.ACTION_UP) + time += 1000 + touch(MotionEvent.ACTION_DOWN) + touch(MotionEvent.ACTION_POINTER_DOWN or (1 shl MotionEvent.ACTION_POINTER_INDEX_SHIFT), fingers = 2) + val before = moves.size + touch(MotionEvent.ACTION_MOVE, y = 270f, fingers = 2) + assertEquals(before, moves.size) + assertTrue(events.any { it.startsWith("scroll:") }) + } + + @Test fun profileChangeAndMenuSuspensionSuppressPendingGesturePresses() { + touch(MotionEvent.ACTION_DOWN) + bridge.setInputEnabled(false) + bridge.setInputEnabled(true) + shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(1000)) + assertTrue(events.isEmpty()) + touch(MotionEvent.ACTION_CANCEL) + time += 1000 + touch(MotionEvent.ACTION_DOWN) + bridge.setInputProfile(profile(InputAction.Disabled)) + shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(1000)) + assertTrue(events.isEmpty()) + panel.releaseInputs() + bridge.setInputProfile(profile()) + time += 1000 + touch(MotionEvent.ACTION_DOWN); touch(MotionEvent.ACTION_UP) + shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(100)) + assertEquals(listOf("button:272:true", "button:272:false"), events) + } + + private fun touch(action: Int, y: Float = 200f, fingers: Int = 1) { + val properties = Array(fingers) { i -> MotionEvent.PointerProperties().apply { id = i; toolType = MotionEvent.TOOL_TYPE_FINGER } } + val coords = Array(fingers) { i -> MotionEvent.PointerCoords().apply { x = 200f + 100 * i; this.y = y; pressure = 1f; size = 1f } } + val event = MotionEvent.obtain(time - 20, time, action, fingers, properties, coords, 0, 0, 1f, 1f, 0, 0, 0, 0) + try { assertTrue(pad.onTouchEvent(event)) } finally { event.recycle(); time += 10 } + } + private fun descendants(view: View): Sequence = sequence { + yield(view) + if (view is ViewGroup) for (i in 0 until view.childCount) yieldAll(descendants(view.getChildAt(i))) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/ui/InputSurfaceOwnershipTest.kt b/app/src/test/java/com/droiddeck/launcher/ui/InputSurfaceOwnershipTest.kt new file mode 100644 index 0000000000..566c93d772 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/ui/InputSurfaceOwnershipTest.kt @@ -0,0 +1,148 @@ +package com.droiddeck.launcher.ui + +import androidx.activity.ComponentActivity +import android.view.MotionEvent +import android.view.View +import android.view.ViewGroup +import android.widget.FrameLayout +import android.widget.TextView +import com.droiddeck.launcher.R +import com.droiddeck.launcher.input.* +import com.droiddeck.launcher.input.JoyConPadTest +import com.droiddeck.launcher.input.PadBridge +import com.droiddeck.launcher.input.PadState +import com.droiddeck.launcher.session.SessionPrefs +import com.droiddeck.launcher.session.SessionState +import java.io.File +import org.junit.After +import org.junit.Assert.* +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.Robolectric +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [34], shadows = [JoyConPadTest.RecordingWriter::class]) +class InputSurfaceOwnershipTest { + private val controller = Robolectric.buildActivity(ComponentActivity::class.java).setup() + private val activity = controller.get() + private val root = FrameLayout(activity) + private val bridge = PadBridge(File(activity.cacheDir, "panel-ownership")) + private lateinit var panel: InputSurfaceView + private val previousDeckPad = SessionState.deckPad + + @Before fun setUp() { + SessionState.deckPad = true + DeckControls.releaseAll() + bridge.setInputRouter(InputRouter(object : InputOutput { + override fun key(code: Int, down: Boolean) {} + override fun button(code: Int, down: Boolean) {} + override fun pointer(dx: Float, dy: Float) {} + override fun scroll(steps: Int) {} + override fun command(command: InputCommand) {} + override fun requestMotion() {} + })) + val elements = listOf(InputControl.RIGHT_PAD, InputControl.LS, InputControl.A).mapIndexed { i, c -> + InputSurfaceElement(c, .02f + i * .3f, .1f, .28f, .4f) + } + InputSurfaceElement(InputControl.LS, .02f, .6f, .28f, .3f, "dpad", InputElementKind.DPAD) + bridge.setInputProfile(InputProfile("native", elements = mapOf(InputRole.SECONDARY to mapOf("dpad" to InputAction.Native())))) + activity.setContentView(root) + panel = InputSurfaceView(activity, bridge, elements) + root.addView(panel) + val trackpad = child("TrackpadView") + trackpad.layout(0, 0, 300, 300) + touch(trackpad, MotionEvent.ACTION_DOWN, 200f, 100f) + touch(trackpad, MotionEvent.ACTION_POINTER_DOWN or (1 shl MotionEvent.ACTION_POINTER_INDEX_SHIFT), 200f, 100f, fingers = 2) + assertEquals(32 or 128, JoyConPadTest.RecordingWriter.grips) + } + + @After fun close() { + root.removeAllViews() + bridge.stop() + DeckControls.releaseAll() + controller.pause().stop().destroy() + SessionState.deckPad = previousDeckPad + } + + @Test fun removingTheStickKeepsTrackpadContactAndClickHeld() { + val stick = child("StickView") + stick.layout(0, 0, 300, 300) + touch(stick, MotionEvent.ACTION_DOWN, 150f, 150f) + touch(stick, MotionEvent.ACTION_MOVE, 240f, 150f) + assertTrue(state().leftX > 0f) + val position = JoyConPadTest.RecordingWriter.deckPads.copyOf() + (stick.parent as ViewGroup).removeView(stick) + assertEquals(0f, state().leftX) + assertTrackpadStillHeld(position) + root.removeView(panel) + assertEquals(0, JoyConPadTest.RecordingWriter.grips) + } + + @Test fun removingDirectionPadReleasesOnlyItsOwnInput() { + val dpad = child("DirectionPad") + dpad.layout(0, 0, 300, 300) + touch(dpad, MotionEvent.ACTION_DOWN, 150f, 30f) + assertTrue(state().up) + val position = JoyConPadTest.RecordingWriter.deckPads.copyOf() + (dpad.parent as ViewGroup).removeView(dpad) + assertFalse(state().up) + assertTrackpadStillHeld(position) + } + + @Test fun removingAButtonReleasesOnlyItsOwnInput() { + val buttons = child("HoldButton") + buttons.layout(0, 0, 300, 300) + touch(buttons, MotionEvent.ACTION_DOWN, 150f, 240f) + assertTrue(state().isDown(PadState.A)) + val position = JoyConPadTest.RecordingWriter.deckPads.copyOf() + (buttons.parent as ViewGroup).removeView(buttons) + assertFalse(state().isDown(PadState.A)) + assertTrackpadStillHeld(position) + } + + @Test fun replacingTheLayoutReleasesEveryOldControl() { + holdFaceButton() + assertTrue(state().isDown(PadState.A)) + panel.updateCustomLayout(emptyList()) + assertFalse(state().isDown(PadState.A)) + assertEquals(0, JoyConPadTest.RecordingWriter.grips) + bridge.applyExternal({ it.press(PadState.B, true) }, true) + assertTrue(state().isDown(PadState.B)) + assertFalse(state().isDown(PadState.A)) + } + + @Test fun removingThePanelLeavesNoTouchSourceForLaterPhysicalInput() { + holdFaceButton() + root.removeView(panel) + assertFalse(state().isDown(PadState.A)) + bridge.applyExternal({ it.press(PadState.B, true) }, true) + assertTrue(state().isDown(PadState.B)) + } + + private fun holdFaceButton() { + val buttons = child("HoldButton") + buttons.layout(0, 0, 300, 300) + touch(buttons, MotionEvent.ACTION_DOWN, 150f, 240f) + } + + private fun assertTrackpadStillHeld(position: ShortArray) { + assertEquals(32 or 128, JoyConPadTest.RecordingWriter.grips) + assertArrayEquals(position, JoyConPadTest.RecordingWriter.deckPads) + } + + private fun state() = JoyConPadTest.RecordingWriter.pads[0]!! + private fun child(name: String) = descendants(panel).first { it.javaClass.simpleName == name } + private fun descendants(view: View): Sequence = sequence { + yield(view) + if (view is ViewGroup) for (i in 0 until view.childCount) yieldAll(descendants(view.getChildAt(i))) + } + + private fun touch(view: View, action: Int, x: Float, y: Float, fingers: Int = 1) { + val properties = Array(fingers) { i -> MotionEvent.PointerProperties().apply { id = i; toolType = MotionEvent.TOOL_TYPE_FINGER } } + val coords = Array(fingers) { MotionEvent.PointerCoords().apply { this.x = x; this.y = y; pressure = 1f; size = 1f } } + val event = MotionEvent.obtain(1000, 1010, action, fingers, properties, coords, 0, 0, 1f, 1f, 0, 0, 0, 0) + try { assertTrue(view.onTouchEvent(event)) } finally { event.recycle() } + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/ui/DeckControlsPanelTest.kt b/app/src/test/java/com/droiddeck/launcher/ui/InputSurfaceViewTest.kt similarity index 54% rename from app/src/test/java/com/droiddeck/launcher/ui/DeckControlsPanelTest.kt rename to app/src/test/java/com/droiddeck/launcher/ui/InputSurfaceViewTest.kt index 45f6566977..48616c03a5 100644 --- a/app/src/test/java/com/droiddeck/launcher/ui/DeckControlsPanelTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/ui/InputSurfaceViewTest.kt @@ -4,6 +4,8 @@ import android.content.ComponentName import android.view.MotionEvent import androidx.activity.ComponentActivity import android.view.View +import android.widget.FrameLayout +import com.droiddeck.launcher.input.* import com.droiddeck.launcher.input.JoyConPadTest import com.droiddeck.launcher.input.PadBridge import com.droiddeck.launcher.session.SessionState @@ -20,8 +22,42 @@ import java.nio.file.Files @RunWith(RobolectricTestRunner::class) @Config(sdk = [34], manifest = Config.NONE, instrumentedPackages = ["com.droiddeck.launcher.input.FakeInputWriter"], - shadows = [JoyConPadTest.RecordingWriter::class]) -class DeckControlsPanelTest { + shadows = [JoyConPadTest.RecordingWriter::class, SessionInputPointerTest.CompositorShadow::class]) +class InputSurfaceViewTest { + @Test fun customControlsReleaseOnDetachAndWorkWhenTheSameViewReturns() { + val controller = Robolectric.buildActivity(ComponentActivity::class.java).setup() + val activity = controller.get() + val events = mutableListOf() + val bridge = PadBridge(activity.cacheDir) + val router = InputRouter(object : InputOutput { + override fun key(code: Int, down: Boolean) { events += down } + override fun button(code: Int, down: Boolean) {} + override fun pointer(dx: Float, dy: Float) {} + override fun scroll(steps: Int) {} + override fun command(command: InputCommand) {} + override fun requestMotion() {} + }) + bridge.setInputRouter(router) + bridge.setInputProfile(InputProfile("test", elements = mapOf(InputRole.SECONDARY to mapOf("one" to InputAction.Keys(listOf(57)))))) + val root = FrameLayout(activity) + activity.setContentView(root) + val panel = InputSurfaceView(activity, bridge, + listOf(InputSurfaceElement(InputControl.A, .2f, .2f, .2f, .2f, "one"))) + try { + repeat(2) { + root.addView(panel) + panel.measure(View.MeasureSpec.makeMeasureSpec(600, View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(600, View.MeasureSpec.EXACTLY)) + panel.layout(0, 0, 600, 600) + val down = MotionEvent.obtain(0, 1, MotionEvent.ACTION_DOWN, 150f, 150f, 0) + try { assertTrue(panel.dispatchTouchEvent(down)) } finally { down.recycle() } + assertEquals(true, events.last()) + root.removeView(panel) + assertEquals(false, events.last()) + } + assertEquals(listOf(true, false, true, false), events) + } finally { root.removeAllViews(); bridge.stop(); controller.pause().stop().destroy() } + } + @Test fun takesTouchAtMatchesWhatATouchDoes() { val directory = Files.createTempDirectory("deck-panel").toFile() try { @@ -29,12 +65,10 @@ class DeckControlsPanelTest { val app = RuntimeEnvironment.getApplication() shadowOf(app.packageManager).addActivityIfNotPresent(ComponentName(app, ComponentActivity::class.java)) val activity = Robolectric.buildActivity(ComponentActivity::class.java).setup().get() - for (pad in listOf(null, PadBridge(directory))) for (portraitDock in listOf(false, true)) { + for (pad in listOf(null, PadBridge(directory))) for (portraitDock in listOf(false, true)) for (layout in InputStockLayouts.layouts()) { val name = "pad=${pad != null} portraitDock=$portraitDock" - val panel = DeckControlsPanel(activity, onClose = {}, onSteamMenu = {}, onQam = {}, - pad = pad, portraitDock = portraitDock) + val panel = InputSurfaceView(activity, pad, layout.elements) activity.setContentView(panel) - assertEquals(name, pad == null, panel.deckControls) val (w, h) = if (portraitDock) 720 to 1000 else 1200 to 480 panel.measure(View.MeasureSpec.makeMeasureSpec(w, View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(h, View.MeasureSpec.EXACTLY)) diff --git a/app/src/test/java/com/droiddeck/launcher/ui/InputUiLayoutTest.kt b/app/src/test/java/com/droiddeck/launcher/ui/InputUiLayoutTest.kt new file mode 100644 index 0000000000..2715af7116 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/ui/InputUiLayoutTest.kt @@ -0,0 +1,131 @@ +package com.droiddeck.launcher.ui + +import android.content.Context +import android.graphics.Bitmap +import android.graphics.Canvas +import android.view.View +import androidx.compose.foundation.layout.BoxWithConstraints +import androidx.compose.material3.Surface +import androidx.compose.runtime.CompositionLocalProvider +import androidx.compose.ui.platform.LocalView +import androidx.compose.ui.semantics.SemanticsActions +import androidx.compose.ui.test.* +import androidx.compose.ui.unit.dp +import com.droiddeck.launcher.R +import com.droiddeck.launcher.input.ControllerPrefs +import com.droiddeck.launcher.input.InputLibraries +import com.droiddeck.launcher.input.InputProfiles +import org.junit.Assert.assertEquals +import com.droiddeck.launcher.input.InputAction +import com.droiddeck.launcher.input.InputControl +import org.junit.Before +import org.junit.Rule +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 +import java.io.File + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [28]) +@GraphicsMode(GraphicsMode.Mode.NATIVE) +class InputUiLayoutTest { + @get:Rule val compose = createRobolectricComposeRule() + private lateinit var root: View + @Before fun reset() { + val context = RuntimeEnvironment.getApplication() + for (name in listOf("input-profiles", "input-surfaces", "controller")) context.getSharedPreferences(name, Context.MODE_PRIVATE).edit().clear().commit() + } + + @Test fun layoutThumbnailDoesNotPaintOverItsRowOrAdjacentControls() { + val context = RuntimeEnvironment.getApplication() + val library = InputLibraries.read(context) + val preview = InputLayoutPreviewView(context) + preview.update(library.layout("game", com.droiddeck.launcher.input.InputRole.SECONDARY)!!, com.droiddeck.launcher.input.InputRole.SECONDARY) + preview.measure(View.MeasureSpec.makeMeasureSpec(112, View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(76, View.MeasureSpec.EXACTLY)) + preview.layout(0, 0, 112, 76) + val bitmap = Bitmap.createBitmap(240, 160, Bitmap.Config.ARGB_8888) + bitmap.eraseColor(android.graphics.Color.MAGENTA) + preview.draw(Canvas(bitmap)) + assertEquals(android.graphics.Color.MAGENTA, bitmap.getPixel(150, 30)) + assertEquals(android.graphics.Color.MAGENTA, bitmap.getPixel(30, 120)) + bitmap.recycle() + } + + @Test @Config(qualifiers = "w360dp-h800dp") + fun phoneKeepsLibraryNavigationAndProfileEditingReachable() { + compose.setContent { root = LocalView.current; DroidDeckTheme { Surface { BoxWithConstraints { + CompositionLocalProvider(LocalNarrowPane provides (maxWidth < 600.dp)) { InputSettingsPage({}) } + } } } } + compose.onNodeWithTag("input-profile-desktop").performScrollTo().assertIsDisplayed().performClick() + compose.onNodeWithTag("input-part-physical").performScrollTo().assertIsDisplayed().performClick() + compose.onNodeWithTag("input-binding-a").performScrollTo().assertIsDisplayed().performClick() + compose.onNodeWithTag("input-action-save").assertIsDisplayed() + screenshot("phone-properties") + } + + @Test @Config(qualifiers = "w640dp-h360dp") + fun handheldPropertiesLeaveSaveVisibleAboveScrollableContent() { + compose.setContent { root = LocalView.current; DroidDeckTheme { Surface { InputActionEditor(InputControl.LS, InputAction.Pointer(), "Left stick", {}, {}) } } } + compose.onNodeWithTag("input-action-save").assertIsDisplayed() + compose.onNode(SemanticsMatcher.keyIsDefined(SemanticsActions.SetProgress)).performScrollTo().assertIsDisplayed() + screenshot("handheld-properties") + } + + @Test @Config(qualifiers = "w960dp-h600dp") + fun largeScreenShowsTheProfileLibraryInTheExistingTheme() { + compose.setContent { root = LocalView.current; DroidDeckTheme { Surface { InputSettingsPage({}) } } } + compose.onNodeWithTag("input-profile-game").assertIsDisplayed() + screenshot("profiles") + } + + @Test @Config(qualifiers = "w960dp-h600dp") + fun controllerTabKeepsSetupNavigationWithoutDuplicateEditors() { + val context = RuntimeEnvironment.getApplication() + compose.setContent { + root = LocalView.current + DroidDeckTheme { + CompositionLocalProvider(LocalFrontFocus provides FrontFocus(), LocalNarrowPane provides false) { + FocusGlideHost { + SetupPanel(FrontEndState( + installed = null, ready = false, available = null, busy = false, stage = "", percent = -1, + desktopInstalled = false, offlineAccount = null, offline = false, frameGenLabel = "", + romsDir = null, logsEnabled = false, steamGames = emptyList(), emulators = emptyList(), running = null, + controller = ControllerPrefs.read(context), + ), FrontEndActions( + onPlay = {}, onPlayDesktopUi = {}, onSteamGame = {}, onDesktop = {}, onEmulator = {}, + onRom = {}, onResume = {}, onSteamSettings = {}, onDesktopSettings = {}, + onInstallPackage = {}, onRemovePackage = {}, onRuntime = {}, onFrameGenPick = {}, + onImportLossless = {}, onProtons = {}, onComponents = {}, onPerformance = {}, + onRoms = {}, onFiles = {}, onLogs = {}, onOffline = {}, + onWirelessAdbPair = { _, _, _, _ -> }, onWirelessAdbApply = { _, _, _, _ -> }, + onSetPhantomProcessLimit = { _, _ -> }, controller = ControllerActions({}), + ), onOpenDeveloperOptions = {}, onRequestWirelessAdb = {}, onPairWirelessDebugging = {}) + } + } + } + } + compose.onNodeWithText(context.getString(R.string.setup_tab_controller)).performClick() + compose.onNodeWithTag("input-profile-game").assertIsDisplayed() + compose.onNodeWithText(context.getString(R.string.ctrl_layout)).assertDoesNotExist() + compose.onNodeWithText(context.getString(R.string.ctrl_configure)).assertDoesNotExist() + screenshot("controller-tab") + compose.onNodeWithTag("input-profile-game").performClick() + compose.onNodeWithTag("input-part-physical").performScrollTo().performClick() + compose.onNodeWithText(context.getString(R.string.setup_tab_launcher)).assertIsDisplayed() + screenshot("controller-physical") + compose.runOnIdle { assertEquals(InputProfiles.AUTO, InputLibraries.read(context).selected) } + } + + private fun screenshot(name: String) { + compose.runOnIdle { + val image = Bitmap.createBitmap(root.width, root.height, Bitmap.Config.ARGB_8888) + root.draw(Canvas(image)) + val file = File("build/reports/input-ui/$name.png").apply { parentFile?.mkdirs() } + file.outputStream().use { image.compress(Bitmap.CompressFormat.PNG, 100, it) } + image.recycle() + } + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/ui/PhysicalShortcutsDialogTest.kt b/app/src/test/java/com/droiddeck/launcher/ui/PhysicalShortcutsDialogTest.kt index a9afe50287..864eb0ef64 100644 --- a/app/src/test/java/com/droiddeck/launcher/ui/PhysicalShortcutsDialogTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/ui/PhysicalShortcutsDialogTest.kt @@ -66,10 +66,11 @@ class PhysicalShortcutsDialogTest { } } - @Test fun buttonMappingPageOpensPhysicalShortcutAssignments() { + @Test fun inputSettingsPageOpensPhysicalShortcutAssignments() { compose.setContent { - DroidDeckTheme { ControllerMappingPage(emptyMap(), { _, _ -> }, {}, {}) } + DroidDeckTheme { InputSettingsPage(onBack = {}) } } + compose.onNodeWithTag("shortcuts-assign-STEAM").performScrollTo() compose.onNodeWithTag("shortcuts-assign-STEAM").assertExists() compose.onNodeWithTag("shortcuts-assign-QAM").assertExists() compose.onNodeWithTag("shortcuts-assign-DROIDDECK").assertExists() @@ -134,12 +135,12 @@ class PhysicalShortcutsDialogTest { compose.setContent { DroidDeckTheme { PhysicalShortcutRows() } } assign(PhysicalShortcuts.Action.STEAM) send(KeyEvent.KEYCODE_F3, KeyEvent.ACTION_DOWN) - compose.onNodeWithTag("shortcuts-remove").performScrollTo().performClick() + compose.onNodeWithTag("shortcuts-restore-default").performScrollTo().performClick() compose.runOnIdle { assertEquals(PhysicalShortcuts.Action.QAM, PhysicalShortcuts.read(context).single().action) assertTrue(ControllerPrefs.read(context).selectSteam) } - compose.onNodeWithTag("shortcuts-assign-STEAM").assertTextContains(context.getString(R.string.game_settings_default)) + compose.onNodeWithTag("shortcuts-assign-STEAM").assertTextContains(context.getString(R.string.input_shortcut_guide)) } @Test fun retryRecoversFromAMissingReleaseWithoutSavingTheOldButton() { @@ -172,7 +173,7 @@ class PhysicalShortcutsDialogTest { val label = compose.onNodeWithText(context.getString(R.string.drawer_steam_menu)).getUnclippedBoundsInRoot() assertTrue(control.right - control.left < 200.dp) assertTrue("Menu label must not wrap vertically: $label", label.bottom - label.top <= 48.dp) - compose.onNodeWithTag("shortcuts-assign-STEAM").assertTextContains(context.getString(R.string.ctrl_custom)) + compose.onNodeWithTag("shortcuts-assign-STEAM").assertTextContains(button.label) compose.onNodeWithText("${button.label} · ${button.name}").assertExists() } diff --git a/app/src/test/java/com/droiddeck/launcher/ui/SessionSecondScreenTest.kt b/app/src/test/java/com/droiddeck/launcher/ui/SessionSecondScreenTest.kt index 9891ac1b0d..7349b9a57d 100644 --- a/app/src/test/java/com/droiddeck/launcher/ui/SessionSecondScreenTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/ui/SessionSecondScreenTest.kt @@ -5,12 +5,14 @@ import android.hardware.input.InputManager import android.view.Display import android.view.InputDevice import android.view.KeyEvent +import android.view.MotionEvent import android.view.View import android.view.ViewGroup import android.widget.TextView import androidx.compose.ui.test.* import androidx.compose.ui.semantics.SemanticsProperties.StateDescription import androidx.compose.runtime.MutableState +import androidx.compose.ui.geometry.Offset import com.droiddeck.launcher.R import com.droiddeck.launcher.SessionActivity import com.droiddeck.launcher.gpu.FrameGen @@ -21,6 +23,14 @@ import com.droiddeck.launcher.input.PadBridgeRoutingTest import com.droiddeck.launcher.session.SessionService 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 +import org.junit.Assert.assertSame +import org.junit.Assert.assertNotNull import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse import org.junit.Assert.assertTrue @@ -61,6 +71,28 @@ 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 + var selected = SecondScreenMode.CUSTOM_CONTROLS + compose.setContent { + DroidDeckTheme { + SessionDrawer(true, SessionDrawerPage.SECOND_SCREEN, false, {}, + actions(true, listOf(SecondScreenDisplay(1, "Bottom screen")), + onClose = { closes++ }, select = { selected = it }), docked = true) + } + } + compose.onNodeWithContentDescription(context.getString(R.string.common_close)).performClick() + assertEquals(1, closes) + assertEquals(SecondScreenMode.CUSTOM_CONTROLS, selected) + } + @Test fun desktopWithoutASecondDisplayFallsBackToController() { SessionState.mode = SessionService.MODE_DESKTOP var page = SessionDrawerPage.SECOND_SCREEN @@ -104,7 +136,7 @@ class SessionSecondScreenTest { SessionState.mode = SessionService.MODE_STEAM 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 panel = SecondScreenPresentation(activity, display, {}, {}, {}) try { panel.show() panel.showMode(SecondScreenMode.KEYBOARD_TRACKPAD) @@ -123,6 +155,74 @@ class SessionSecondScreenTest { } } + @Test fun profileRefreshLeavesTerminalAndDashboardIntact() { + val activity = Robolectric.buildActivity(SessionActivity::class.java).get() + val display = activity.getSystemService(DisplayManager::class.java).getDisplay(Display.DEFAULT_DISPLAY) + val panel = SecondScreenPresentation(activity, display, {}, {}, {}, dashboard = { androidx.compose.material3.Text("Dashboard") }) + try { + panel.show() + for (mode in listOf(SecondScreenMode.TERMINAL, SecondScreenMode.DASHBOARD)) { + panel.showMode(mode) + val root = panel.window!!.decorView + val before = descendants(root).toList() + val terminal: Any? = ReflectionHelpers.getField(panel, "terminalPanel") + panel.refreshControls() + assertEquals(before, descendants(root).toList()) + assertSame(terminal, ReflectionHelpers.getField(panel, "terminalPanel")) + } + } finally { panel.closeControls() } + } + + @Test fun profileRefreshUpdatesControlsWithoutClosingTheFieldKeyboard() { + SessionState.mode = SessionService.MODE_STEAM + val activity = Robolectric.buildActivity(SessionActivity::class.java).get() + val display = activity.getSystemService(DisplayManager::class.java).getDisplay(Display.DEFAULT_DISPLAY) + var closed = 0 + val panel = SecondScreenPresentation(activity, display, {}, {}, {}, onKeyboardClosed = { closed++ }) + try { + panel.show() + panel.showMode(SecondScreenMode.CUSTOM_CONTROLS) + panel.requestKeyboard(forField = true) + val host: Any = ReflectionHelpers.getField(panel, "keyboardHost") + val shown: MutableState = ReflectionHelpers.getField(panel, "keyboardShown") + shown.value = true + ReflectionHelpers.setField(panel, "imeSeen", true) + val controls = descendants(panel.window!!.decorView).filterIsInstance().single() + val element = InputSurfaceElement(InputControl.A, .3f, .2f, .2f, .2f, "updated") + InputLibraries.update(activity) { library -> + val layout = library.activeLayout(false, InputRole.SECONDARY)!! + library.withLayout(layout.copy(elements = listOf(element), style = InputSurfaceStyle(opacity = 35))) + } + panel.refreshControls() + assertSame(controls, descendants(panel.window!!.decorView).filterIsInstance().single()) + val child = descendants(controls).single { it.tag == element } + assertEquals(.35f, child.alpha, .001f) + assertSame(host, ReflectionHelpers.getField(panel, "keyboardHost")) + assertTrue(panel.fieldKeyboardOpen) + assertTrue(shown.value) + assertTrue(ReflectionHelpers.getField(panel, "imeSeen")) + assertEquals(0, closed) + } finally { panel.closeControls() } + } + + @Test fun profileRefreshPreservesTheLowerScreenChatPanel() { + val activity = Robolectric.buildActivity(SessionActivity::class.java).get() + val display = activity.getSystemService(DisplayManager::class.java).getDisplay(Display.DEFAULT_DISPLAY) + var lost = 0 + val panel = SecondScreenPresentation(activity, display, {}, {}, {}) + try { + panel.show() + panel.showMode(SecondScreenMode.CUSTOM_CONTROLS) + panel.showChat(onLost = { lost++ }) { androidx.compose.material3.Text("Chat draft") } + val chat: View? = ReflectionHelpers.getField(panel, "chatView") + assertNotNull(chat) + panel.refreshControls() + assertSame(chat, ReflectionHelpers.getField(panel, "chatView")) + assertTrue(descendants(panel.window!!.decorView).any { it === chat }) + assertEquals(0, lost) + } finally { panel.closeControls() } + } + @Test @Config(sdk = [34], shadows = [PadBridgeRoutingTest.RegisteredKeyEvent::class]) fun controllerBumpersReachTheSecondScreenInBothSessionModes() { @@ -208,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().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() @@ -217,11 +354,12 @@ class SessionSecondScreenTest { SessionDrawer(true, SessionDrawerPage.SECOND_SCREEN, false, {}, actions(steam, displays) { selected += it }) } } - for (mode in listOf(SecondScreenMode.KEYBOARD_TRACKPAD, SecondScreenMode.TERMINAL)) { + val modes = listOf(SecondScreenMode.CUSTOM_CONTROLS, SecondScreenMode.TERMINAL) + for (mode in modes) { compose.onNodeWithText(context.getString(mode.label)).performScrollTo().assertIsDisplayed().performClick() } compose.onNodeWithText(context.getString(SecondScreenMode.DECK_CONTROLS.label)).assertDoesNotExist() - assertEquals(listOf(SecondScreenMode.KEYBOARD_TRACKPAD, SecondScreenMode.TERMINAL), selected) + assertEquals(modes, selected) } private fun descendants(view: View): Sequence = sequence { @@ -231,8 +369,11 @@ class SessionSecondScreenTest { private fun actions(steam: Boolean, displays: List, 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, @@ -241,5 +382,5 @@ class SessionSecondScreenTest { onHud = {}, onFrameGenPick = {}, onImportLossless = {}, onKeyboard = {}, onHardwareKeyboard = {}, onSteamMenu = null, onQam = null, onOsc = {}, onSuspendPolicy = {}, onBackActionsInverted = {}, onTouch = {}, onFexPreset = {}, onSecondScreenMode = select, onSecondScreenDisplay = {}, - onLaunchAndroidApp = { _, _ -> }, onBackground = {}, onShareLogs = {}, onStop = {}, onClose = {}) + onLaunchAndroidApp = { _, _ -> }, onBackground = {}, onShareLogs = {}, onStop = {}, onClose = onClose) } diff --git a/docs/development/unified-input-proposal.md b/docs/development/unified-input-proposal.md new file mode 100644 index 0000000000..1d2100b2d9 --- /dev/null +++ b/docs/development/unified-input-proposal.md @@ -0,0 +1,393 @@ +# Shared input profiles and configurable control surfaces + +This change implements a shared routing model for physical controllers and local +screen controls. It is intended for review of the architectural direction, with +a functional configuration UI and hardware validation on AYN Thor. The related +maintainer discussions motivate this work; they are not approval of this +implementation. + +The configuration UI keeps the existing Setup tabs. Controller contains mode +defaults, one profile list and device settings. Reusable layouts and actions are +stored separately but are managed within each profile source. Direct editing on +the target display has been exercised on AYN Thor, including editing above a live +Steam session and keeping the secondary canvas separate from the main-screen +tools. Revision-specific checks and remaining limits are recorded in the +validation report. + +## Why a shared system + +Desktop sessions need mouse, keyboard and application actions from a controller. +A dual-screen device may already have a comfortable lower-screen mouse, leaving +its physical sticks available for other actions. The same logical button or pad +can appear on the main screen, lower screen or a physical device; its location +should not dictate which actions it can perform. + +The model separates three concerns: + +- **Source role:** physical controller, game overlay (primary) or auxiliary panel + (secondary). Swapping the game and auxiliary panel between displays preserves + the panel's role and bindings. +- **Profile:** references to a physical binding preset and a layout/binding pair + for each touch role. Game and Desktop are editable bases; named profiles can + share parts. Auto has separate Steam and Desktop assignments. Opening a profile + for editing does not activate it. +- **Surface layout:** stable element IDs, types, names, positions and sizes. + Named custom layouts can be used on either touch role. Actions belong to a + separate binding preset associated with that layout. Two desktop profiles can + share their physical bindings and layout while using different touch actions. + +A binding selects native controller output, a DroidDeck action, or disabled. +Native output continues through the existing game/Steam path, optionally with a +compatible gamepad remap. A local action consumes that control so, for example, +R3 assigned to Ctrl+F does not also press the guest's R3 button. + +```mermaid +flowchart TD + Physical[Android controller events] --> PadBridge + Primary[Primary on-screen controls] --> PadBridge + Secondary[Secondary custom controls] --> PadBridge + Deck[Deck pads and grips] --> DeckControls + BLE[Steam Controller BLE reports] --> TritonInputRouter + Library[Saved profiles, layouts and binding presets] --> Profiles[Resolved active profile by source role] + Profiles --> Router[InputRouter] + PadBridge --> Router + DeckControls --> Router + TritonInputRouter --> Router + Router --> Native[Existing pad / Deck report publication] + Router --> Held[Shared key and mouse ownership] + Router --> Commands[Existing menu and keyboard hosts] + Router --> Motion[InputMotion: active stick pointer / scroll only] + Motion --> Held + Held --> Guest[Existing guest keyboard and mouse output] + Native --> Steam[Steam Input or guest controller consumer] + Layout[Independent surface geometry and element IDs] --> Secondary + Layout --> Primary +``` + +These are routing relationships, not additional asynchronous queues. Native +publication is performed by the existing source adapters and writers. + +## Implementation + +| Component | Responsibility | +| --- | --- | +| `InputProfile` | Logical controls, source roles and supported actions | +| `InputLibraries` / `InputLibrary` | Versioned reusable parts, validated references, selection and migration | +| `InputProfiles` | Compatibility API for the compiled active profile and legacy migration | +| `InputSurfaceLayout` | Bounded normalized touch geometry and legacy layout migration | +| `InputSurfaceSource` | Independently owned per-element routing through existing pad/Deck publishers | +| `InputRouter` | Resolve channels on profile changes, route events, track held outputs | +| `PadBridge` | Existing player slots plus independently owned local touch sources | +| `DeckControls` | Source ownership for Deck grips, contacts, coordinates and clicks | +| `TritonInputRouter` | Apply routing before Steam Controller BLE report publication | +| `PhysicalInputBindings` | Capture additional Android keys with device identity | +| `HeldInput` | Coordinate router outputs with existing keyboard and gesture outputs | +| `InputMotion` | Frame callback for active synthesized stick pointer/scroll movement | +| `InputTiming` | Optional bounded dispatch-to-publication diagnostics | + +Supported actions are native output, compatible native remapping, disabled, +keyboard keys/combinations, mouse buttons, pointer movement, scrolling, four +keyboard directions, and existing menu/keyboard/Steam/quick-access commands. +Analog controls used as buttons have separate activation and release thresholds. +Key combinations press modifiers first and release in reverse order. + +Standard controller assignments are shared by the physical role. Additional +captured keys match a device identity plus Android keycode/scancode, rather than +persisting a transient Android device ID. System-reserved keys remain with +Android. This is not a per-device calibration system. + +### Controller navigation + +Setup keeps its existing top-level tabs. Controller starts with Desktop and Game +mode defaults, followed by one profile list and inline device settings. Opening +or editing a profile does not activate it; the session menu owns the active +profile override. Quick-menu overrides are called out beside the default selectors. + +Each profile opens Physical controller, Main screen or Second screen. Control +cards and touch element rows open the same large properties editor. Layout +editing uses the full-size canvas and one draggable, expandable Tools palette. Back +returns to the source and profile being edited, rather than the library root. +Saved layouts and reuse commands live inside the relevant source editor; there +are no parallel top-level layout, binding and profile catalogs. + +New profiles copy actions and custom geometry independently by default. Users +can explicitly keep physical controls shared at creation, or copy/link a source +from another profile later. Reusing a saved layout can link geometry without +linking actions. Make independent copies both geometry and actions for that +source. Reset actions affects only the selected profile, preserving its layouts +and detaching shared actions before resetting them. + +Main-screen visibility remains a global device setting. Color and opacity belong +to each saved touch layout. The standard gamepad is an ordinary editable layout: +its legacy positions, appearance and stick options are migrated into elements. +Other saved resolutions remain selectable layouts. The previous fixed gamepad +renderer and its separate editor are removed; only its placement calculation is +retained for migration. Vibration, radial stick dead zone, Joy-Con VR, global +shortcuts and timing diagnostics are inline below the profile list. This adds no +settings reads or queues to input dispatch. + +### Session profile composition + +Both the session drawer and launcher controls sheet expose Profile, Physical +controller, Main screen and Second screen. Choosing a profile loads its three +saved parts; choosing one part creates a mode-specific override without editing +the source profile. Game and Desktop remember their choices separately. Auto +resolves the corresponding default profile. Selecting a complete profile clears +part overrides. Save as new profile creates independent copies of all three +parts, including geometry, appearance and actions. + +Fixed lower-screen tabs, side selectors and click bars are removed. Deck pads +and grips, one or two Deck pads, grips alone, the mouse trackpad with keyboard, +and a compact gamepad are ordinary editable layout/binding presets. Their menu, +keyboard and click buttons are ordinary elements too. The compact gamepad reuses +the previous panel geometry calculation. Existing fixed-panel selections migrate +to profile controls on first use. Schema 4 also preserves version-3 custom +layouts and converts legacy fixed layout references without changing physical +mappings. Active quick-menu parts cannot be deleted. + +Mouse touchpad gestures reuse `TouchpadGestures` for relative movement, tap, +hold-drag, two-finger scrolling and secondary clicks. A tap uses the element's +configured click action; it does not reposition the pointer. Gesture outputs use +owned router channels and are suppressed while input is suspended or until a +held contact returns to neutral after a profile change. Native Deck pad mode +retains contact/position reporting and click behavior for Steam Input. + +### Reuse and migration + +Existing `FakeInputWriter` transports, player assignment, guest keyboard/mouse +output, `KeyboardHost` and second-screen presentation are reused. The existing +button, stick, d-pad and trackpad views now live in `InputSurfaceView`; there is +no parallel fixed controller panel. The custom editor arranges the existing button, stick, d-pad and trackpad +views; it does not create another emulated controller. Trigger elements provide +press/release input. A new element is unassigned until an action is explicitly +saved. Native sticks and pads retain their existing click gestures. +Movement/contact and click are separate assignments for sticks and trackpads. +Both use the same properties editor and router ownership rules; migrated click +bindings remain editable, and a custom mouse pad can assign its click separately. + +The editor uses the target display at actual size. On a dual-screen device, +Add, Undo, Snap, Save and element properties stay on the activity display; the +auxiliary display is a full-size canvas with no toolbar inset. On one display, +a draggable Tools bubble expands into those same tools, including element +properties, and collapses to leave the canvas accessible. Properties replace the +canvas and return to the same draft. Snapping aligns centers and edges with +other elements and the display, including during resize. Done persists geometry and actions together; Cancel discards the +draft after confirmation. The preview has no input sources and cannot send +input to the running session. + +Primary custom layouts follow the existing on-screen-controls visibility setting; +Steam's touch-controller mode retains priority. Secondary layouts use Profile controls mode. Terminal, desktop and dashboard +remain separate panel modes. Steam-managed touch controls keep their own editor; a new editable layout +can start empty or copy a saved custom layout. + +Legacy Game/Desktop mappings, key captures, active selection and lower-screen +geometry migrate to the library together. Legacy element IDs preserve their +existing logical bindings. New elements use stable independent IDs, so two +buttons can have different actions or hold the same output without releasing +one another. Referenced parts cannot be deleted, and editing an unsupported +library version cannot overwrite it. Shared edits show their scope; copying a +binding preset from a profile relinks only that profile's selected role. + +Library decoding, validation and preset resolution happen during configuration +or profile activation. Dispatch uses preconfigured router channels, with no +preference reads, profile lookups or new asynchronous queues. The physical native +identity path and frame-based stick motion remain in place. These properties do +not substitute for a fresh latency comparison on hardware. + +The old touch-only mapping and Select-as-Steam settings migrate once into Game's +primary-screen and physical bindings respectively. Their redundant UI is removed. +`PadBridge` retains raw SELECT state; only the profile maps it to GUIDE. The old +flag is read for migration, not applied again during event handling, so explicit +native/disabled/local bindings and profile resets remain authoritative. Attaching +or removing a router cannot change which raw button a SELECT release clears. +Invalid stored entries fall back to defaults. The global settings reset clears +both profile and surface settings without reimporting the old mappings. + +The shared PC-key definitions are adapted from kingokksa's +[#1305](https://github.com/Droid-Deck/DroidDeck/pull/1305) at `76fc9b31`. +Device capture and identity handling build on the approach in jmarti326's +[#1202](https://github.com/Droid-Deck/DroidDeck/pull/1202) at `42db788f`. +The initial implementation adapted these parts rather than merging either PR +wholesale. Upstream subsequently merged #1202; the October 30 integration retains +its global physical shortcuts, saved assignments and configuration UI alongside +the profile bindings. + +The November 4 integration reuses upstream's common bottom-screen bar, extracted +trackpad/terminal panels and screen-swap presentation. Custom controls coexist +with the dashboard and KDE desktop panels. Routed on-screen stick updates retain +upstream's throttled visual redraws without deferring pad publication. Upstream +hardware-key translation and mouse capture use the existing output ownership and +release capture while the input-profile editor is open. + +Explicit captured bindings in the active profile take precedence over a global +physical shortcut for the same key. Otherwise the global shortcut precedes the +default Back/QAM handling and logical controller routing. A consumed shortcut +does not also reach the guest, and its release remains consumed across menu +changes. Shortcut capture remains available from Input profiles, the session +menu and mode settings. Profile editing gates execution of these shortcuts. + +### Ownership and lifecycle + +Each source retains its own contribution. Releasing a physical mouse button does +not release an identical button still held on the lower screen. Closing a surface +releases that surface, including the final release on the existing Epic launcher +mouse route. Existing gesture and keyboard outputs participate in shared key and +mouse-button ownership. + +Native pad merging uses OR for buttons, the maximum trigger value, and the +strongest complete stick vector. A native Deck trackpad retains one source's +coordinates until that contact ends, then accepts a waiting contact. Concurrent +surfaces do not overwrite its coordinates during the same contact. + +Profile changes, suspension and menu entry release old local outputs. Held +controls must return to neutral before activating their new bindings. Device +removal and surface disposal remove their owned state. The existing menu +navigation path remains available independently of gameplay assignments. + +When an adapter attaches retained input state to a replacement router, it seeds +the new channels without emitting presses and requires held controls to return +to neutral. Already-open pad rings receive the released state immediately; +attaching a router, changing a profile or enabling input does not open an unused +ring. Steam-menu and VR stick-switch requests use separate system-owned button +pulses, so primary-surface bindings cannot suppress or remap those commands. + +The menu and keyboard commands currently open their respective UI; they are not +toggles. Back/B closes the menu. This can be reconsidered with the UX work. + +### Hot path and scheduling + +Profile JSON, preferences and layout geometry are processed outside event +routing. Channels resolve their actions when configuration changes. The current +router uses mutable per-source state under one synchronized monitor; it is not an +immutable compiled plan. This keeps shared ownership consistent, but contention +with simultaneous BLE input has not yet been measured on hardware. + +For an unchanged Game binding, `PadSource` returns the original `PadState`. +The BLE identity route returns its original report array. Modified reports use +reusable buffers and preserve unrelated timestamp, motion, pressure and extension +fields. Normal controller events do not wait for a frame, coroutine or timer. + +Only synthesized stick-to-pointer/scroll motion uses `Choreographer`, because a +held stick must keep moving without new device events. Trackpad deltas and button +changes publish synchronously on the normal UI input path. Pointer output arriving +from an off-main-thread adapter is marshalled to the existing UI cursor path; +application commands also use the main handler. Neither schedules native forwarding. + +The mapped cursor reuses the existing compositor coordinates and Android cursor +layer. Trackpad contact starts from the current pointer position, preventing +jumps when switching sources. Publishing pending Compose snapshot notifications +before drawing avoids the observed every-other-frame cursor update without +changing the stick response curve or adding smoothing. + +See [validation](unified-input-validation.md) for measurements and their limits. +The evidence supports no clear typical latency regression in the tested native +path, not zero overhead or a complete input-to-display latency guarantee. + +## Steam ownership and boundaries + +Steam remains responsible for game mappings, action sets, layers, radial menus +and its cloud profiles. Native/Steam-managed here means forwarding the controller +through the existing path; it is not a new integration with Steam's profile API. +Steam Mobile Touch retains its own Steam-managed layout and protocol. Its widgets +do not pass through this local remapper. The VR-specific input path also remains +separate. Existing gestures retain their recognizers and share output ownership. + +The local UI revision includes named profiles, reusable binding presets and +custom layouts edited on either display. Import/export, automatic per-app +selection, macros, configurable tap/hold behavior and multi-button activation +chords remain deferred. One button emitting Ctrl+F is supported; pressing +multiple buttons to activate a new binding is not implemented. Built-in Steam +touch layouts retain their separate Steam-managed editor. + +Physical native remaps stay within compatible report families. Generic custom +buttons choose a supported button output explicitly. Additional hardware +adapters must join before publishing consumed controls; an Activity-only +interceptor cannot cover every raw device. Future adapters should use this +contract without replacing device-specific calibration or protocol handling. + +## Related upstream work + +Reviewed on 2026-10-24. Several related proposals remain open; this is not a claim +that no other controller work exists or that this implementation supersedes it. + +- [#1202](https://github.com/Droid-Deck/DroidDeck/pull/1202): physical shortcuts and + capture. The maintainer's + [centralization feedback](https://github.com/Droid-Deck/DroidDeck/pull/1202#issuecomment-6299647180) + motivates one shared assignment model. Questions there about overlapping + actions inform consumption/ownership; multi-button activation remains deferred. +- [#1305](https://github.com/Droid-Deck/DroidDeck/pull/1305): screen key mappings, + combinations and layout management. This change shares the action model across + physical and screen sources, but does not implement that PR's full layout UX. +- [#1414](https://github.com/Droid-Deck/DroidDeck/pull/1414): external-controller + button and touchpad-press capture, including devices exposing a touchpad press + as pointer input. Our key capture does not yet implement that pointer-contact + adapter. It is a concrete area for coordination. +- [#860](https://github.com/Droid-Deck/DroidDeck/issues/860) and + [#1356](https://github.com/Droid-Deck/DroidDeck/issues/1356): desktop controller + input. This implements controller-driven pointer/keyboard actions, not every + gesture or automatic keyboard behavior requested in #1356. +- [#581](https://github.com/Droid-Deck/DroidDeck/pull/581): hardware calibration and + device profiles remain a separate responsibility from action profiles. +- [#1226](https://github.com/Droid-Deck/DroidDeck/pull/1226): keyboard host selection; + the local action reuses the existing host rather than implementing another IME. +- [#1150](https://github.com/Droid-Deck/DroidDeck/pull/1150): raw DualSense input. + That proposed adapter will need integration before publication; it is not + covered merely by the current Android key capture. + +## Review focus + +The main questions are whether source roles and profile boundaries match the +project's direction, whether adapters join at the right point, and whether the +shared ownership/lifecycle contract is suitable for future input sources. The +validation document distinguishes device-tested behavior, host regressions and +remaining coverage. UX/UI refinement can follow agreement on those decisions. + +### Settings polish and display targeting + +The session Profile row expands to show the three saved parts and the editor. +Choosing the profile remains a separate selector. Session editing opens its +settings on the spare display, when available; element properties and editor +commands stay on the activity display. Main-screen placement over a running +session uses a transparent canvas so controls can be arranged around game UI. +Existing visible controls are suspended while editing. + +Schema 5 adds an explicit main/secondary/both target to saved layouts. Migration +infers the target from existing profile and quick-menu references, preserving +layouts already used on both screens. Layout rows render a miniature with the +same `InputSurfaceView` renderer; unused legacy copies are retained under Other +saved layouts. Newly created layouts belong to the source being edited. + +Action editing retains a draft for each route and action type. Sticks assigned +to a digital action press above 55% on either axis and release below 35%; a +trackpad holds a digital action during contact. These semantics are explained +next to the action. Horizontal scroll uses the same router and existing evdev +REL_HWHEEL / Wayland axis transport, with independent fractional accumulation. +No additional event thread or queue is introduced. + +### Screen-key consolidation + +Schema 6 imports the retired screen-key overlay into normal saved profiles, +layouts and element bindings. The active old set and each named saved set become +separate imported profiles. Current Game/Desktop defaults and quick selections +remain unchanged; the settings page explains where to find the imported profiles. +The old editor, overlay View, guest-key dispatch path and session-menu entries +are removed. Only a one-time reader of the old preferences remains. + +Migration converts normalized centers and density-based sizes into the shared +layout geometry. Key chords, four-way directions, separate eight-way stick +bindings, toggle buttons, built-in symbols, custom image references, shape and +opacity are retained. Blank stick diagonals keep their previous behavior of +pressing neighboring cardinal directions. Imported images are decoded once per +view, sampled to at most 512 pixels, and read only from the app's existing image +directory. New buttons can choose text or an existing built-in key symbol. + +A converted library and the original raw preferences are committed together +before old preference entries are removed. Invalid data or capacity failures do +not erase the old library or keys, and settings show a pending-import notice. +The backup is recovery data, not a second runtime input configuration. + +Toggle buttons use `InputSurfaceSource` and the same router ownership as every +other element. Releasing one owner cannot release another owner's held key. +Menu suspension, profile changes, cancellation and detach release latched output. +Physical native input continues through its existing fast path; no queue, timer +or additional event thread is introduced for these features. diff --git a/docs/development/unified-input-validation.md b/docs/development/unified-input-validation.md new file mode 100644 index 0000000000..f5dfdbfa80 --- /dev/null +++ b/docs/development/unified-input-validation.md @@ -0,0 +1,703 @@ +# Unified input validation + +Recorded on 2026-10-24. These results distinguish the device-tested candidate from +the later upstream rebase and host regression fixes. They do not establish zero +latency or complete coverage of every controller and session mode. + +## UI publication candidate, 2026-11-06 + +The implementation was updated through upstream `a1bc8467f` (merge `aa3757eff`). +The generic action selector now says **Scroll**; binding summaries still state +the configured direction. Closing the docked session drawer now invokes its +ordinary close action instead of disabling the second-screen presentation. +The global-reset regression compares against a pristine compiled library, which +includes the editable gamepad's keyboard element. + +- All **944 input/UI tests in 125 suites passed**. The final full JVM run completed + **3,598 tests in 493 suites: 3,573 passed, 25 failed, no errors or skips**. + The failures are the same unchanged upstream log-export tests (24) and + case-sensitive WoW add-on import test (1) described below. Input/UI and global + settings reset passed in this full run. +- Initial full runs exhausted this Mac's free disk and produced secondary native + runtime/idle failures. Obsolete local test APKs were removed. The completed run + used a local Gradle init script with one worker and a fresh test JVM every 25 + classes to bound temporary-file use. No repository test configuration was + weakened and no tests were excluded from the completed run. +- The APK passed runtime-input, session-asset, native dependency, alignment and + v1/v2/v3 signature checks. SHA-256: + `611814f11cbac4bab7f97dac5c0b0657d85d8dfbd6b40b741bda1fc13efa6f83`. + It was installed over the separate Input Test app with its environment intact; + as with earlier local candidates, Rust store engines were omitted. +- On AYN Thor, closing the lower-screen menu with its X restored the existing + controls instead of revealing Android. The live Steam editor, separate lower + canvas and upper tools/properties, profile previews, saved layouts, and action + pages were exercised. A single Trigger selection added a centered, selected + element; the reported occasional extra tap was not reproduced. The temporary + editor change was discarded. +- The user also verified native controls, profile switching, trackpad/pointer + and scrolling behavior on the immediately preceding UI candidate. This is + distinct from automated UI navigation and the historical latency measurements. + +Actual display captures and test reports are retained locally under +`app/build/reports/input-publish/`. Screenshot publication is subject to the +user's review. No new latency measurement was made for this UI revision. + +The follow-up router-ownership fix passed **47 tests** in `InputSourceOwnershipTest`, +`InputSurfaceSourceTest`, and `InputRouterTest`, including two new regressions. +An obsolete activity (or one without a router) cannot detach the replacement +activity's Deck routes; the current owner still releases its held keys on cleanup. +GitHub's complete Build APK workflow passed for `7b7722bd2`. + +### Review follow-up + +- Upgrade binding IDs are deterministic across unsuccessful reads. Editor lookup + from an earlier read remains valid when legacy screen-key import fails. +- An edit of malformed supported-version data commits both the restored library + and a recovery copy of the original text. Reads alone preserve the original; + newer unsupported versions remain protected against overwrite. Settings show + that recovery data was retained. +- Focus/capture loss releases hardware, Android keyboard and PC-keyboard owners + without releasing another source's hold. Secondary settings handle an invalid + presentation display at initial show and resume. +- Physical-button capture waits for release before confirmation/navigation. + Back and B remain assignable; another Back/B press cancels, A confirms, and + Retry starts a new capture. The existing dialog-window key hook is reused. +- Steam Controller BLE teardown synchronously closes its routed source and + gates state/feature publication. Queued callbacks and starts cannot reactivate + it after stop returns; disconnect work remains on the existing BLE thread. + +The input/UI/global-reset run passed **959 tests in 127 suites**. After the +additional BLE finding, **5 BLE/routing tests passed**, including two regressions +for queued callbacks, synchronous release, source detachment and shared holds. +The final APK passed packaging, runtime/native dependency and signature checks: +`a5ec9120be120b829748f8489782a0c7158360d7f8deb61777fe75d601708d20`. +It was installed over Input Test; saved profile preferences remained byte-for-byte +identical and the existing environment was retained. +The button-capture dialog opened and cancelled on Thor without a crash. No +physical BLE-controller check or new latency measurement was made for these +follow-ups. + +### Replacement-controller and mouse-backend follow-up + +The Triton slot now has a single owner across activity instances. Late reports, +subscription callbacks, feature replies and teardown from a replaced controller +cannot overwrite the current controller's report or connection flag. Every +instance still closes its own routed source. Claiming the slot happens once at +start; a queued or repeated old start cannot take it back. + +Legacy Select-to-Steam also migrates to the Desktop physical profile when there +is no saved Desktop profile. An existing Desktop assignment is preserved. +Mouse backend selection and publication share the button-owner lock with raw +channel open, soft release and destruction. Horizontal and vertical scrolling +use the same path and fall back to Wayland after leaving raw mode. + +The complete input/UI/global-reset run passed **967 tests in 128 suites**. +The focused run passed **28 tests**, including replacement subscription before +old teardown, unstarted-instance teardown, saved/default Desktop migration, and +concurrent scroll versus raw-channel release/destruction. These changes add no +input queue or timer. Physical BLE hardware and latency remain unmeasured for +this follow-up. + +### Upstream integration, 2026-11-07 + +Merged upstream through `542bbf89c`. The shared stick renderer retains the new +fast-double-tap rule from #2173, including cancelled, dragged, long-held and slow +second taps; the six upstream regressions were adapted to the profile-backed +renderer. WoW chat uses the existing custom lower-screen presentation and shared +key ownership, preserving Ctrl held by another input source. The new platform +pointer, PiP, component-update and store-launch changes are retained. + +The merged candidate passed **1017 tests in 133 suites**, with no failures, +errors or skips: all input/UI tests, global reset, component updates, PiP policy, +runtime auto-update and Proton menu auto-selection. The gamescope p22 archive +was downloaded from the newly pinned component repository and verified against +upstream's SHA-256 before refreshing the local runtime inputs. + +### Second-screen refresh follow-up, 2026-11-07 + +Refreshing a profile now updates the existing custom control surface in place. +Terminal and dashboard modes are untouched, and the custom surface keeps its +keyboard host, field-keyboard state and WoW chat panel. Actual mode changes +continue to use the existing full teardown/rebuild path. + +All **35 tests in 5 suites passed** across `SessionSecondScreenTest`, the +surface UI suites and `InputSurfaceSourceTest`. Three new regressions cover +terminal/dashboard identity, changed elements/style with an open field keyboard, +and preservation of the lower-screen chat view and its dismissal callback. + +## Input settings polish, 2026-11-06 + +The final input/UI host run passed **941 tests in 124 suites, zero failures**. +New coverage includes retained route/action drafts, centered element creation +without a placement tap, collapsed quick-profile settings, schema-4 migration +with a layout shared across both screens, target/scroll serialization, and +horizontal scrolling through both guest mouse backends. Existing physical native +fast-path and ownership tests remain green. + +The settings use stable DroidDeck Input labels, contextual axis/contact hints, +color swatches, opacity sliders and rendered layout thumbnails. Diagnostics now +open a result dialog and explain how to record a sample and what it measures. +Shortcut rows show the actual assignment and offer Restore to default. + +No new latency comparison was taken for this UI revision. The normal native +controller path is unchanged. New horizontal wheel work happens only for a local +scroll action. The user's environment installation is retained. Device verification is recorded below. + +### Screen-key removal and Thor verification + +The old screen-key overlay, standalone editor, layout panel and separate runtime +key sender are removed. Schema 6 imports the old active set and every named set +as editable profiles. Current defaults and selections are kept. Tests cover +idempotent import, chords, normalized geometry, per-element opacity, shapes, +image references, symbols, custom diagonals, and retaining the original data +when migration cannot complete. A backup and the converted library are committed +together before the old preferences are removed. + +New shared element tests exercise latched keyboard and native controller buttons, +shared ownership, cancellation, menu suspension and profile replacement. Separate +diagonal bindings release previous keys and preserve neighboring-direction +fallbacks. No additional input queue or thread is used. + +Final APK installed over `com.droiddeck.launcher.inputtest` on AYN Thor: +`6618be55c28a52746b6504656a480845d25d8a34114daa1829aad5dbb5b5e729`. +Packaging checked runtime inputs, session assets, native library dependencies and +v1/v2/v3 signatures. As with the earlier test packages, Rust store engines are +omitted; the Steam/Desktop environment is retained. + +On-device checks: + +- The expanded Profile has readable text and one visibly grouped block. +- Profile source rows and saved-layout rows show bounded thumbnails. The previous + black rectangle overflowing the list no longer appears. +- The original anchored color menu shows its color swatches. +- Steam → Controller → Edit opens settings on the spare display. +- Main-screen editing opens on the activity display over live Steam. A second + on-device crash was found and fixed: returning from lower-display settings must + use the existing activity dialog, not a Presentation on the default display. +- Adding a button immediately centers and selects it. Its properties and symbol + picker open on the main display. +- Secondary-screen editing leaves only the canvas below and tools above. Adding + a trackpad centers and selects it without requiring another placement touch. +- Temporary edits were discarded. Comparing the before/after saved libraries, + excluding new default schema fields, found no changes to the user's profiles, + geometry, bindings or selections. Schema changed from 5 to 6. +- Steam remained READY after closing the editors; the final device log contained + no new fatal exception. + +The test device had no old screen-key sets to import. Legacy content migration +and toggle ownership were verified by host regression tests, not by a new +physical-input latency or end-to-end game test. Evidence is under +`app/build/reports/input-polish/`; the final installable copy is +`app/build/input-ui/DroidDeck-Input-Test-unified-screenkeys-2026-11-06.apk`. + +## Unified editor and session profile composition, 2026-11-06 + +The fixed gamepad and its separate editor are replaced with a saved editable +layout rendered by `InputSurfaceView` on either display. The old fixed lower +panel, tab switches and separate mouse trackpad view are removed. Deck pads, +grips, single pads, the mouse trackpad and a compact gamepad are editable presets; +their menu, keyboard and click buttons are normal elements. The compact preset +reuses the previous panel geometry calculation. + +Schema 4 migrates version-2 overlay references, mappings, saved positions, +appearance and stick-click preferences, and version-3 fixed lower layouts. +Other resolution-specific positions remain saved layouts. Future or invalid +libraries are not overwritten. Physical mappings retain their identities and +assignments. Existing custom layouts remain intact. + +On two displays, the activity hosts Add, Undo, Snap, Done and full-size properties; +the other display contains only the canvas, without a reserved toolbar strip. +On the main display a draggable Tools bubble expands into the same tools, +including Properties. The editor snaps centers and edges while moving and +resizing. Both source pages list their actual elements. Device settings are +inline below the profiles. + +The quick menu selects a whole profile or overrides Physical controller, Main +screen and Second screen independently. Game and Desktop selections are separate. +Changes do not mutate the source profiles; saving a combination creates independent +copies. Returning to Auto clears the overrides. Tests cover serialization, +reference validation, deletion protection and preservation of source profiles. + +The complete input/UI host run passed **931 tests in 124 suites, zero failures**. +Coverage includes full-size shared editing, floating tools, display removal, +geometry migration, source ownership during detach/layout replacement, quick-menu +composition and saving, native Deck clicks and mouse gestures. Tap and hold use +the configured element click; ordinary taps do not reposition the cursor. Profile +changes and menu suspension suppress delayed gesture presses until neutral. +The neutral-stick/physical-input regression remains covered. + +No new end-to-end latency measurement was performed for this revision. The +physical controller path remains unchanged. Touch output uses the existing +owned router and guest transports without an additional event queue. Mouse +movement is delivered synchronously; tap/hold recognition reuses the existing +`TouchpadGestures` timings. The local APK omits the Rust store engines and uses +the separate Input Test application ID. + +Device verification on AYN Thor confirmed the four quick selectors and saved +second-screen presets, draggable/expandable tools, full-screen properties and a +canvas-only secondary display. WindowManager reports the main editor at +`[0,0][1920,1080]`; the secondary canvas uses the full `1240×1080` display. +The installed library migrated to schema 4 with existing profile references, +bindings and custom layouts preserved. A previous test migration's unchanged +main overlay geometry was corrected from its window-size copy to its saved +1920×1080 version. No user-edited geometry was replaced. + +Final APK SHA-256: `c13326d11f5e8de3b878d83b296ba2f77495baf5c35d1d00dbb0327333c0277d`. +Runtime assets, native library dependencies and v1/v2/v3 APK signatures passed +verification. Focused editor/input checks passed again after fixing Compose 1.6 +fullscreen dialog sizing. Live game/desktop input and perceived latency with this +final build still need user testing; the device checks above cover UI and migration. + +## Controller integration and upstream update, 2026-11-06 + +Merged upstream `bf693d7f7` into the working branch as `ba5876bf6` before the UI +integration. This includes 90 upstream commits, including radial stick dead zones +and mouse-button handling. The merge keeps routed source ownership and the new +upstream raw-stick behavior; the targeted input merge checks passed. + +Controller now contains two default-profile selectors, one profile list and a +Device settings entry. Source-specific action editing and saved layouts are +inside each profile. New profiles are independent by default, with an explicit +option to share physical controls. Direct editing and Hide tools / Show tools +are retained. No input-event queue or preference lookup was added by this UI +reorganization. + +The first full host run executed 3,580 tests in 490 suites: 3,555 passed and 25 +failed, with no skips. All 926 input and UI tests in that run passed. The failures +are in unchanged upstream log-export tests (24) and WoW add-on import tests (1). +The log fixtures require `SecureDirectoryStream`, unavailable in this macOS JDK; +the WoW test assumes `Foo` and `foo` refer to different paths, while this host +filesystem is case insensitive. A direct Java filesystem probe confirmed both +conditions. The log-export failures also reproduce when run separately. The +affected production and test sources match `origin/main`. These host limitations +have not been hidden by disabling tests or changing production code. + +The final input/UI-only run passed all 927 tests in 122 suites. + +Additional final checks cover integration in the existing Setup tabs, the lack +of duplicate Layout/Configure entries, back navigation, independent creation, +explicit physical sharing, and preserving elements while hiding editor tools. +Rendered UI checks cover 360×800, 640×360 and 960×600 dp. Earlier latency results +below are not a fresh measurement of this revision. + +The final 26 profile/library/editor checks passed again after the source-specific +control lists and sharing labels were finalized. The APK passed session-asset +matching, runtime-input attestation, native dependency/export checks, alignment +and signing validation. It uses the same local HTTPS/direct-release download +adaptations as the previous build and still omits the store Rust engines. + +Installed the update over `com.droiddeck.launcher.inputtest` on AYN Thor without +clearing data. Verified Setup → Controller → Desktop → Second screen → Edit on +screen; the canvas opened on the lower display, Hide tools / Show tools worked, +and selecting the right trackpad opened large properties on the main display. +Returned without saving; the stored input-profile preferences matched the backup +exactly. Device settings had no persisted overrides before or after this check. +No fresh gameplay or latency measurement was performed in this UI pass. + +Final APK SHA-256: +`f1d16a503661055a0abe8cc954d3fc94f6b9a1c1c626ac38b6c88dba478810d1`. + +## Local UI revision, 2026-11-05 + +This revision adds the Profiles / Layouts / Bindings libraries, migration of the +previous settings, independently identified touch elements, and a direct editor +on either display with properties on the activity display. + +- The complete JVM/Compose run passed 3,306 tests across 467 suites, with no + failures, errors or skipped tests. The native identity fast-path checks remain + in that suite. After the final editor-window adjustment, all nine editor and + adaptive-layout UI tests passed again. +- New coverage includes legacy mapping/geometry/selection migration, shared + physical presets with separate touch actions, explicit private copies, safe + deletion/reset, unsupported schema preservation, independent element actions, + overlapping held outputs, suppression across profile/menu changes, and inert + editor previews, independent stick/trackpad click bindings, same-view + detach/reattach, and saving a draft after the auxiliary display disappears. +- Both Steam and Desktop quick settings expose the same input library entry. +- Compose navigation was exercised at 360×800, 640×360 and 960×600 dp. Rendered + images are generated under `app/build/reports/input-ui/`. The properties Save + control stays outside the scrollable content on the short landscape screen. +- Updated the existing `com.droiddeck.launcher.inputtest` installation on AYN + Thor from 0.6.0 to the local 0.6.9 build without clearing its data. The official + package was not replaced. A local backup of the two input preference files was + taken before migration; all 61 saved Desktop bindings matched after migration. +- On Thor, exercised the quick-settings entry, library navigation, full-size + editing on both displays, selection on the lower display with properties on + the main display, moving an element, Undo and Done. After undoing the move and + saving, the complete stored library matched the pre-edit data. +- The earlier Thor latency measurements below apply to the earlier candidate, + not to this library/editor revision. No new end-to-end or dispatch latency + claim is made. + +The complete local runtime build passed session-asset matching, runtime-input +attestation, native export/dependency validation, alignment and signature checks. +The local build-script copy used HTTPS for the Debian archive and direct GitHub +release downloads when the unauthenticated API was rate limited; checksum and +revocation checks remained enabled. No build-script changes are included in the +source diff. As before, the test APK excludes the Epic/GOG/Amazon Rust engines. +The installed APK SHA-256 is +`3e3f41589d45b70294f2d59404f591ae76f2ceba8488657e8821aab1bca224b3`. + +Before publishing, also test on Thor: create two Desktop profiles sharing physical +bindings; give them different touch actions; edit and resize on the actual lower +display; open properties on the top display; save/cancel; swap screens; return +from Android Home; disconnect/reconnect the auxiliary display; and repeat the +paired physical-button latency measurement. Verify primary custom overlays with +on-screen controls enabled and secondary layouts in Custom controls mode. + +## Revisions and environment + +The Thor candidate was `5021e7ac` on base `ac0909ea`, with the local temporary-file, +cursor visibility/handoff/frame cadence, scroll direction and button label fixes +subsequently committed on this branch. Its final APK SHA-256 was +`bb211095d6443e9f85ed1f5d8c5674f527df2937ac6b0efe7d6fc4adcb9dcf62`. + +The branch was then rebased onto upstream `906e0d1f`. Review added regressions and +fixes for final touch-source release, concurrent native trackpad contact ownership, +and resetting the new preferences. Those final changes and the rebase have host +test coverage; the installed Thor APK was not silently replaced and those exact +changes have not yet been revalidated on the device. + +Local Android builds use Java 17 and NDK `28.2.13676358`. The Thor test APK used a +local Gradle init script to install alongside the official app; no application ID +or app-name changes are part of this PR. Local builds use `-PskipRust=true`, so +Steam/Desktop checks do not establish the behavior of the Epic/GOG/Amazon store +engines in a complete release build. + +## Automated checks + +On the rebased implementation, the complete `:app:testDebugUnitTest` run passed: +**2,230 tests across 333 suites, zero failures/errors/skips**. + +The supported `tools/build_local.sh` debug build also passed, including session +asset matching, runtime-input attestation, native export/dependency validation, +ZIP alignment and signature verification. The APK SHA-256 was +`a7d440b4029f9ad67ca6f44c92ccdd6b9eba9e9a758b28ab0e432256697d49b9`. +The only local environment repair was moving a Finder `.DS_Store` file out of the +NDK's host-toolchain directory; the upstream script counted it as another host. +No build checks were disabled to get past the stale-runtime check after rebasing. + +Commands, with Java/SDK paths configured for the local machine: + +```sh +./gradlew :app:testDebugUnitTest -PskipRust=true -PndkVersion=28.2.13676358 +DROIDDECK_BUILD_VARIANT=debug DROIDDECK_SKIP_RUST=1 \ + DROIDDECK_NDK_VERSION=28.2.13676358 bash tools/build_local.sh +# Linux with gcc and g++ available: +python3 -m unittest tools.tests.test_fakeinput_open -v +``` + +Coverage includes profile persistence and migration, invalid bindings, shared +holds, key order, trigger/direction hysteresis, neutral gating on profile changes, +source disposal, raw BLE report preservation, physical key capture, global reset, +existing controller/gesture/keyboard paths, pointer handoff, and Compose settings +and layout editor behavior. + +Two additional ownership regressions were demonstrated failing before their +fixes, then passed in the full suite: + +- Removing the last local touch surface must send the final release to the legacy + Epic mouse route, while removing only one of two holds must preserve the other. +- Concurrent native Deck pad surfaces must retain the first contact's coordinates + until it ends, then transfer ownership and release correctly. + +The native preload regression probe compiles the actual library and compares +`open`, `openat`, `open64` and `openat64` with libc. It covers ordinary file +creation, `O_TMPFILE` permissions, and opening a directory without a mode argument +(12 combinations). The temporary-file fix addresses the KDE cache permissions +problem encountered during the Desktop test; it does not change controller +transport or publication ordering. + +All three test methods passed on Linux x86-64 after rebasing. The same 12 +combinations also passed in the earlier i386 probe. + +Earlier native testing also exposed three i386 ring-test failures, reproduced on +the unmodified pre-feature baseline. These are not presented as passing or fixed +by this PR. Physical BLE coverage is likewise not implied by synthetic tests. + +### PR review follow-up + +After the first CodeRabbit review of PR #1439, nine regression tests were added +for deferred pad-ring opening, releases to an already-open ring, system-owned +Steam/VR actions, and retained input when replacing a router. Seven of the new +scenarios reproduced failures before the fixes. The retained-input tests cover +physical and touch button ownership, native sticks, Deck grips and trackpad +contacts; a separate regression verifies return to the native identity fast path +without scheduling output after held controls reach neutral. + +The full `:app:testDebugUnitTest` run now passes **2,239 tests across 334 suites, +zero failures/errors/skips**. `:app:assembleDebug` also passes with the existing +verified native inputs and the same Java/SDK/NDK and `-PskipRust=true` settings. +The resulting APK passes `check_session_assets.py` and `runtime_inputs.py apk`; +its SHA-256 is +`1c77c85d5d08c4a054db0528ade33d60eaf8755c09a56a598f342207b27c85d4`. + +These follow-up changes have not been installed on Thor or benchmarked on the +device. Retained-state initialization runs when sources attach; ordinary input +keeps its existing synchronous routing and native identity path. The VR command +uses the existing chord handler for its release, retaining the 120 ms pulse. + +### SELECT and panel ownership follow-up + +The next review covered SELECT-to-GUIDE routing and child-view ownership. The +existing one-time Select-as-Steam migration is exercised through real controller +key events, including simultaneous physical GUIDE ownership. Explicit native, +disabled and local SELECT assignments, and resetting the Game profile, remain +authoritative. Removing the obsolete bridge flag keeps raw SELECT state stable +when a router is attached or removed while the button is held. + +Panel regressions use an attached Android view hierarchy and a two-finger +trackpad contact. Switching the other stick to a trackpad, or removing the D-pad +or face buttons, must release only that child's controls and preserve the other +trackpad's contact, coordinates and click. Whole-panel removal still releases +the shared source. These three panel scenarios and the retained SELECT scenario +failed before the fixes; all seven new tests pass after them. + +The complete JVM run passes **2,246 tests across 336 suites, zero failures/errors/ +skips**. The debug APK build, session-asset check and runtime-input verification +pass with the same local build settings. APK SHA-256: +`0d85b4003cd6b31084e50d603e8b00f239775c9c97f3ec17327264db5dd50612`. +These changes have host coverage only; Thor validation and new latency +measurements remain pending. + +### Panel tab teardown follow-up + +Tab changes now remove the old child views before releasing the panel's touch +source. Child detach callbacks can publish button releases, so source cleanup +must run after those callbacks to avoid retaining a neutral source. + +The new tab-switch regression fails before this fix and passes after it. It +checks held-button release, removal of the old source before later physical +input, and touch input after returning to the Gamepad tab. A second regression +confirms that whole-panel removal already releases the source correctly. +All **22 targeted tests** in the panel ownership, input ownership and second-screen +suites pass. The debug APK build, session-asset check and runtime-input +verification also pass. This follow-up has not been tested on Thor or benchmarked +on the device. + +### Upstream integration (2026-10-27) + +Merged upstream `43a0ce93` while preserving the published PR history. The merge +retains upstream screen keys, portrait controls and Home/Back shortcuts alongside +the input profiles and custom second-screen layouts. Screen keys have their own +held-key owner, and explicit physical Back bindings take precedence over the +default QAM shortcut. + +Queued Home/Back chord reports now use current routed axes, including touch +sources, rather than raw physical axes. Router/profile changes and disabled +routing discard queued reports. Cancelling a chord or releasing all input keeps +held controls blocked until neutral, including Deck grips. Ordinary routed input +still publishes synchronously without waiting for the chord queue. + +Eleven integration regressions cover these interactions. Four routed-axis and +cancellation scenarios failed before their fixes. The existing panel ownership +fixture now explicitly selects a Deck controller, matching upstream's restriction +of native trackpad controls to that controller type. + +The complete `:app:testDebugUnitTest` run passes **2,632 tests across 385 suites, +zero failures/errors/skips**, using Java 17, NDK `28.2.13676358` and +`-PskipRust=true`. This compiles the Android Java/Kotlin code and resources; a new +APK build, Thor validation and device latency measurements were not performed +for this merge. + +### QAM and repeated enable-state review (2026-10-27) + +A routed QAM press during an existing emulated chord now publishes simultaneous +button and stick changes immediately. Repeated enable/disable requests no longer +republish unchanged pad or Deck reports; the router reports whether its state +changed, so the bridge does not keep a second copy of that state. + +Both regressions failed before the fixes. Two additional tests cover replacing a +disabled router and enabling a router that was disabled before attachment. The +complete `:app:testDebugUnitTest` run passes **2,636 tests across 385 suites, zero +failures/errors/skips**, with the same Java/SDK/NDK settings and `-PskipRust=true`. +These fixes have not been tested or benchmarked on Thor. + +### Upstream Home/player-slot integration (2026-10-28) + +Merged upstream `213c2da5` without rewriting the published branch. Home/Guide now +follows upstream's primary-player slot selection when the built-in pad is off, +while its report retains routed buttons and axes. The input-settings overlay +continues to gate Home shortcuts and now also gates upstream's Steam volume-key +handling. + +A new regression combines Home on player slot 1 with button/stick remapping and +disabled routing. The complete `:app:testDebugUnitTest` run passes **2,707 tests +across 389 suites, zero failures/errors/skips**, using the same Java/SDK/NDK +settings and `-PskipRust=true`. A new APK build, Thor validation and latency +measurement were not performed for this merge. + +### Upstream shortcuts and dual-screen integration (2026-10-30) + +Merged upstream `6ffe675a` without rewriting the published branch. The merge +preserves upstream's docked second-screen menu, keyboard routing, remembered +panel selection, touch routing and physical shortcuts from #1202, together with +the active input profiles and custom lower-screen layouts. + +Captured profile keys take precedence over global physical shortcuts, which in +turn precede default Back/QAM handling. Both use the original device/key identity +before Joy-Con normalization. Input-profile editing blocks shortcut execution; +consumed releases remain consumed when a shortcut opens or closes the menu. +The upstream shortcut configuration remains accessible from the input settings. + +Mapped mouse output uses upstream's existing Wayland/raw-mouse helpers, retaining +shared button ownership. Changing the raw-mouse backend releases held buttons on +the outgoing backend. Touchpad zoom has an independent Ctrl owner, so it cannot +release Ctrl held by the profile or screen keys. Mapped pointer movement retains +the frame-callback rendering update and cursor visibility behavior. + +Eight new regressions cover these interactions and reopening a custom panel with +either controller type. The upstream shortcut-page UI test now exercises the +replacement Input profiles page. The complete `:app:testDebugUnitTest` run passes +**2,918 tests across 413 suites, zero failures/errors/skips**, using Java 17, +NDK `28.2.13676358` and `-PskipRust=true`. The PR diff against upstream also passes +`git diff --check origin/main`. + +This run compiles Android Java/Kotlin and resources. A new APK build, Thor test +and latency measurement were not performed for this merge; the full release +build remains an upstream CI check. + +### Upstream PC input and bottom-screen integration (2026-11-04 UTC) + +Merged upstream `f0052415` without rewriting the published branch. The merge +retains the common bottom-screen bar, dashboard, KDE desktop panel, game-screen +swap, hardware mouse/keyboard fixes and on-screen stick redraw optimization. +Custom controls use the same bar and preserve their secondary-source bindings +when the game and panel swap displays. Pointer handoff now lives in upstream's +shared `SessionTrackpadView`, used in either display arrangement. + +Physical/IME key translation reuses `GuestKeys`, including keyboard Escape scan +codes and the Desktop right-Ctrl alias, while preserving shared key ownership. +The input-profile editor releases upstream's hardware mouse capture. Routed +on-screen stick movement uses the same redraw throttling as the native overlay; +pad publication remains synchronous, and mapped cursor frame updates keep their +existing cadence mechanism. + +The pointer-handoff regression now exercises the extracted trackpad view. Menu +tests cover the custom panel alongside upstream's panel choices. A new ownership +regression checks that releasing a hardware right-Ctrl alias cannot release Ctrl +still held by the active profile. Panel teardown tests now use `ComponentActivity` +to supply the lifecycle required by upstream's Compose bar. + +The complete `:app:testDebugUnitTest` run passes **3,281 tests across 464 suites, +zero failures/errors/skips**, using Java 17, NDK `28.2.13676358` and +`-PskipRust=true`. Upstream's pinned MaliBridge submodule is initialized and its +required Android server build also succeeds. The PR diff passes +`git diff --check origin/main`. + +No new APK installation, Thor regression pass or latency measurement was performed +for this merge. The earlier device measurements do not validate this exact tree. + +## Manual AYN Thor results + +The user performed the following checks in the installed candidate. The desktop +application was KWrite; native gamepad input was checked in Steam's controller +input test. A loaded game's input-to-display response was not measured. + +| Area | Observed result | +| --- | --- | +| Native Game profile | Physical buttons, D-pad, both sticks/clicks and triggers work without observed missed input or stuck controls | +| Desktop defaults | Left stick moves pointer; A/B click; X/Y provide Enter/Escape; D-pad supplies arrows; right stick scrolls and stops at neutral | +| Scroll direction | Stick down advances to higher line numbers; stick up returns to earlier lines | +| Cursor visibility | Controller buttons and stick movement no longer hide the mapped pointer | +| Pointer handoff | Lower trackpad and stick can alternate without a jump; click position matches cursor | +| Cursor cadence | User confirmed smooth movement after the frame update fix; measurements below | +| Android keyboard | Text entry works on the lower screen, including while moving the pointer; controls continue after hiding the keyboard | +| Configured shortcut | R3 assigned to Ctrl+F through the settings UI repeatedly opens search; Y/Escape closes it; subsequent typing has no stuck Ctrl | +| Combined keyboard input | Physical modifier with Android keyboard supports Ctrl+A, Ctrl+X, Ctrl+V and Ctrl+S | +| Custom lower screen | Right pad moves pointer, left pad scrolls, A/B click, and View/Select opens the keyboard | +| Shared mouse hold | Physical A and screen A can hold together; releasing the physical button preserves selection until the screen button is released | +| Menu/held input | User reported successful menu/Ctrl release and ordinary typing afterward | +| Android Home/resume | Physical stick, lower trackpad, clicks and keyboard shortcuts work after returning | +| Layout persistence | Removing/readding a button through the editor persists across APK update and restart; defaults restored through the editor | +| Label rendering | Long View/Select and Menu/Start labels fit within their buttons | +| Native Deck lower screen | Both trackpads, clicks, L4/L5/R4/R5 and physical controls work in Steam's input test | + +The editor's individual compatible-native and disabled options are host-tested, +not an exhaustive manual hardware matrix. Menu is currently an open command; +Back/B closes it. Multi-button activation chords are not implemented. + +## Relative latency measurement + +### What was measured + +On the same Thor, timestamps from the built-in controller's evdev stream using +`CLOCK_MONOTONIC` were matched to a read-only guest observer of the published +controller ring. The observer polled with a requested 250 microsecond sleep, +roughly 0.31 ms in practice. Each observation bounds receipt between polls; the +table reports the **upper bound**, not an exact physical latency. + +This path includes Android dispatch and publication. It excludes controller +firmware, Steam Input/game processing, rendering and display scanout. USB was +used for control and collection, not as the timed input transport. This is not a +button-to-photon test. + +The public 0.6.0 build (`22eac083`) was sampled first. A locally instrumented +baseline at `ac0909ea` was then compared with the feature candidate; their relevant +baseline Java/native input implementation matched. Both used Steam Deck emulation, +1280×720 at 60 Hz, Steam's main screen, and the same disabled low-latency options. + +The manual samples used physical B presses. Automatic samples injected B edges +into the built-in controller's evdev path, continuing through normal Android +input dispatch. Settled paired runs used a 150-press warmup before 150 measured +presses (300 edges) at approximately 33°C battery temperature. Earlier preliminary +runs were affected by background preparation and are kept separate. + +### Results + +All values below are milliseconds, kernel event to observer upper bound. + +| Run | Matched edges | p50 | p95 | p99 | Max | +| --- | ---: | ---: | ---: | ---: | ---: | +| Public 0.6.0, manual | 84 | 4.379 | 8.155 | 9.334 | 9.729 | +| Local baseline, manual | 82 | 3.876 | 8.322 | 10.667 | 12.447 | +| Baseline automatic, preliminary | 300 | 3.213 | 6.774 | 7.960 | 8.688 | +| Candidate automatic, preliminary | 300 | 2.694 | 5.197 | 6.725 | 8.164 | +| Candidate automatic, settled 1 | 300 | 3.403 | 6.503 | 9.114 | 10.167 | +| Baseline automatic, settled | 300 | 3.061 | 6.269 | 7.160 | 7.850 | +| Candidate automatic, settled 2 | 300 | 2.999 | 5.620 | 8.171 | 13.653 | +| Final installed candidate, extra check | 300 | 3.039 | 5.905 | 7.470 | 11.684 | + +The extra final-APK check matched all 300 edges without an observed overrun or +extra/missing edge. Its p50 interval was 2.692–3.039 ms. It ran at 32°C with internal +timing disabled, without another paired baseline or the explicit 150-press warmup; +it is supporting regression evidence, not a controlled speed comparison. + +A separate internal sample of 600 dispatch-to-publication observations reported: + +| Build | p50 (µs) | p95 (µs) | p99 (µs) | +| --- | ---: | ---: | ---: | +| Baseline | 112.6 | 274.2 | 442.2 | +| Candidate | 116.9 | 315.2 | 561.7 | + +The 4.3 µs median difference is the difference between those runs, **not an +isolated measurement of router cost**. The tested typical native path remained +within baseline variation. Candidate tail values were higher in some runs and +were not isolated to a cause. No claim of a speedup, zero cost, or unchanged worst +case is supported by these samples. + +### Cursor frame cadence + +The initial stick-driven cursor updated at a median 33.333 ms interval, while +the lower trackpad updated at 16.667 ms. Publishing pending Compose snapshot +notifications before the frame's draw traversal changed the stick median to +16.667 ms. Of 119 intervals, 115 were 14–20 ms and none were 30–37 ms. The remaining +four included longer start/stop gaps, so these are not a continuous-load frame-time +distribution. The user confirmed the visible improvement. Speed, sensitivity and +the stick response curve were unchanged. + +This is a rendering cadence check, separate from event propagation latency. + +## Remaining validation + +- Repeat the short Thor regression pass on the rebased final branch. +- Measure simultaneous BLE and touch input, contention and allocations on hardware. +- Compare Desktop key/pointer and native Deck touch latency quantitatively. +- Repeat under a loaded game/rendering workload and, if needed, measure display + response with high-speed video. +- Exercise physical Bluetooth devices and other device-specific adapters. +- Let upstream CI build and test the complete release configuration, including + store engines omitted from the local Steam/Desktop APK. + +These limits should remain visible in the PR while maintainers review the +architecture. Host tests and a successful APK build do not replace those checks. diff --git a/tools/tests/test_fakeinput_open.py b/tools/tests/test_fakeinput_open.py new file mode 100644 index 0000000000..f4853e30e7 --- /dev/null +++ b/tools/tests/test_fakeinput_open.py @@ -0,0 +1,103 @@ +import os +from pathlib import Path +import shutil +import subprocess +import sys +import tempfile +import unittest + +SOURCE = Path(__file__).resolve().parents[2] / "app/src/main/cpp/fakeinput_steam.cpp" + +PROGRAM = r""" +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char **argv) { + assert(argc == 4); + void *symbol = dlsym(RTLD_DEFAULT, argv[1]); + assert(symbol); + int (*plain)(const char *, int, ...) = symbol; + int (*at)(int, const char *, int, ...) = symbol; + int relative = strstr(argv[1], "at") != NULL; + int temporary = !strcmp(argv[2], "temporary"); + int directory = !strcmp(argv[2], "directory"); + int dirfd = open(argv[3], O_RDONLY | O_DIRECTORY); + assert(dirfd >= 0); + assert(fchdir(dirfd) == 0); + umask(0); + const char *path = temporary || directory ? "." : "created"; + int flags = directory ? O_RDONLY | O_DIRECTORY : O_RDWR | (temporary ? O_TMPFILE : O_CREAT | O_EXCL); + int fd; + if (directory) fd = relative ? at(dirfd, path, flags) : plain(path, flags); + else fd = relative ? at(dirfd, path, flags, 0640) : plain(path, flags, 0640); + if (temporary && fd < 0 && (errno == EOPNOTSUPP || errno == EINVAL)) return 77; + assert(fd >= 0); + struct stat st; + assert(fstat(fd, &st) == 0); + if (directory) assert(S_ISDIR(st.st_mode)); + else { + if ((st.st_mode & 0777) != 0640) { + fprintf(stderr, "%s %s: expected mode 640, got %o\n", argv[1], argv[2], st.st_mode & 0777); + return 1; + } + assert(write(fd, "ok", 2) == 2); + assert(lseek(fd, 0, SEEK_SET) == 0); + char bytes[2]; + assert(read(fd, bytes, 2) == 2 && !memcmp(bytes, "ok", 2)); + } + close(fd); + close(dirfd); + return 0; +} +""" + + +@unittest.skipUnless(sys.platform == "linux" and shutil.which("gcc") and shutil.which("g++"), + "needs Linux, gcc and g++") +class FakeinputOpenTest(unittest.TestCase): + @classmethod + def setUpClass(cls): + cls.tmp = tempfile.TemporaryDirectory() + cls.addClassCleanup(cls.tmp.cleanup) + cls.path = Path(cls.tmp.name) + source = cls.path / "program.c" + source.write_text(PROGRAM) + subprocess.run(["gcc", "-o", str(cls.path / "program"), str(source), "-ldl"], check=True) + subprocess.run(["g++", "-shared", "-fPIC", "-O2", "-Wno-attributes", "-pthread", + "-std=c++17", "-static-libstdc++", "-static-libgcc", + "-Wl,--exclude-libs,ALL", "-o", str(cls.path / "libfakeinput.so"), + str(SOURCE), "-ldl"], check=True) + + def check_open(self, kind): + for function in ("open", "openat", "open64", "openat64"): + with self.subTest(function=function), tempfile.TemporaryDirectory() as directory: + args = [str(self.path / "program"), function, kind, directory] + baseline = subprocess.run(args, capture_output=True, text=True) + if baseline.returncode == 77: + self.skipTest("filesystem does not support O_TMPFILE") + self.assertEqual(baseline.returncode, 0, baseline.stderr) + if kind == "named": + (Path(directory) / "created").unlink() + result = subprocess.run(args, env=dict(os.environ, LD_PRELOAD=str(self.path / "libfakeinput.so")), + capture_output=True, text=True, timeout=10) + self.assertEqual(result.returncode, 0, result.stderr) + + def test_unnamed_temporary_files_keep_requested_permissions(self): + self.check_open("temporary") + + def test_named_files_keep_requested_permissions(self): + self.check_open("named") + + def test_directory_opens_do_not_need_a_mode_argument(self): + self.check_open("directory") + + +if __name__ == "__main__": + unittest.main()