diff --git a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt index 3fff669d3c..39a08eb546 100644 --- a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt @@ -1105,10 +1105,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 @@ -1151,12 +1148,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { /** The SteamVR driver switches pad sticks between hands and game on both stick clicks together. */ 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() { 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 56cd48aeb4..88087d9700 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/DeckControls.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/DeckControls.kt @@ -16,6 +16,7 @@ object DeckControls { @Synchronized fun setRouter(next: InputRouter?) { + if (router === next) return for (source in sources) source.closeRoutes() router = next for (source in sources) source.configure() @@ -61,7 +62,11 @@ object DeckControls { internal val axes = ShortArray(4) internal fun configure() { - routes = router?.let { r -> Array(8) { r.channel(role, controls[it]) } } + routes = router?.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() { diff --git a/app/src/main/java/com/droiddeck/launcher/input/InputRouter.kt b/app/src/main/java/com/droiddeck/launcher/input/InputRouter.kt index 52af7ec41b..3c803decfb 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/InputRouter.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/InputRouter.kt @@ -48,8 +48,9 @@ class InputRouter(private val output: InputOutput) { moving = false } + @JvmOverloads @Synchronized - fun pad(role: InputRole): PadSource = PadSource(role).also { pads.add(it) } + 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): Channel = Channel(role, control, fixedAction).also { @@ -128,6 +129,11 @@ class InputRouter(private val output: InputOutput) { 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() { @@ -218,14 +224,20 @@ class InputRouter(private val output: InputOutput) { } } - inner class PadSource internal constructor(role: InputRole) { + 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 { refreshIdentity() } + 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 } 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 49a0d72298..d4bf04d9d0 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/PadBridge.java +++ b/app/src/main/java/com/droiddeck/launcher/input/PadBridge.java @@ -80,6 +80,8 @@ public final class PadBridge { 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; @@ -129,8 +131,10 @@ public final class PadBridge { 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); - for (TouchSource source : touchSources.values()) source.route = router == null ? null : router.pad(source.role); + 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 ? null : router.pad(source.role, source.raw); + if (open) publish(); + for (int slot = 1; slot < SLOTS; slot++) if (playerWriters[slot] != null) publish(slot); } public synchronized boolean hasInputRouter() { return inputRouter != null; } @@ -138,14 +142,14 @@ public final class PadBridge { public synchronized void setInputProfile(InputProfile profile) { if (inputRouter == null) return; inputRouter.setProfile(profile); - publish(); + 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) return; inputRouter.setEnabled(enabled); - publish(); + if (open) publish(); for (int slot = 1; slot < SLOTS; slot++) if (playerWriters[slot] != null) publish(slot); DeckControls.INSTANCE.refresh(); } @@ -181,6 +185,8 @@ public final class PadBridge { qamTapPressed = false; guideTapPressed = false; guideTapGeneration++; + vrStickTapPressed = false; + vrStickTapGeneration++; qamChordActive = false; chordButtonPressed = false; qamChordGeneration++; @@ -200,6 +206,8 @@ 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(); @@ -522,6 +530,8 @@ public final class PadBridge { public synchronized void releaseAll() { guideTapGeneration++; + vrStickTapPressed = false; + vrStickTapGeneration++; routedQamPressed = false; systemGuidePressed = false; systemQamPressed = false; @@ -580,6 +590,20 @@ public final class PadBridge { }, 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; @@ -794,7 +818,7 @@ public final class PadBridge { statDropped++; return; } - if (!guide && !qam && (!routedQam || nativeQam())) { + if (!guide && !qam && !vrStickTapPressed && (!routedQam || nativeQam())) { writer.writePad(output); return; } @@ -803,6 +827,10 @@ public final class PadBridge { 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); + } writer.writePad(effectiveState); } diff --git a/app/src/test/java/com/droiddeck/launcher/input/InputRouterTest.kt b/app/src/test/java/com/droiddeck/launcher/input/InputRouterTest.kt index 2ad592fc4a..4c13732317 100644 --- a/app/src/test/java/com/droiddeck/launcher/input/InputRouterTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/input/InputRouterTest.kt @@ -25,6 +25,21 @@ class InputRouterTest { 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) diff --git a/app/src/test/java/com/droiddeck/launcher/input/InputSourceOwnershipTest.kt b/app/src/test/java/com/droiddeck/launcher/input/InputSourceOwnershipTest.kt index 036c835c94..ae2abc7131 100644 --- a/app/src/test/java/com/droiddeck/launcher/input/InputSourceOwnershipTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/input/InputSourceOwnershipTest.kt @@ -8,6 +8,7 @@ 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]) @@ -16,6 +17,94 @@ class InputSourceOwnershipTest { 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 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() } + } + + 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 { 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/docs/development/unified-input-proposal.md b/docs/development/unified-input-proposal.md index b64056a1e0..2bee4f784a 100644 --- a/docs/development/unified-input-proposal.md +++ b/docs/development/unified-input-proposal.md @@ -123,6 +123,13 @@ 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. diff --git a/docs/development/unified-input-validation.md b/docs/development/unified-input-validation.md index 0398cdaa44..bc3481ed08 100644 --- a/docs/development/unified-input-validation.md +++ b/docs/development/unified-input-validation.md @@ -74,6 +74,28 @@ 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. + ## Manual AYN Thor results The user performed the following checks in the installed candidate. The desktop