mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-19 12:56:06 +00:00
fix: preserve input lifecycle and system actions
Keep unused pad rings closed during profile and enable changes. Route Steam menu and VR stick-switch pulses independently of primary bindings, preserving physical holds and cancelling stale releases. Seed replacement routes with retained source state and require held controls to return to neutral. Add nine regressions and document validation: 2,239 JVM tests and the debug APK build pass; device validation remains pending.
This commit is contained in:
1 parent
d202233beb
commit
1dae4bf8ee
9 files changed
+279
-19
No files matched your search
@@ -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() {
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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 }
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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<Boolean>(bridge, "open"))
|
||||
bridge.applyExternal({ it.press(PadState.X, true) }, true)
|
||||
assertTrue(ReflectionHelpers.getField<Boolean>(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<String>()
|
||||
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 {
|
||||
|
||||
@@ -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<Unit>(activity, name)
|
||||
private fun advance(ms: Long) {
|
||||
shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(ms))
|
||||
shadowOf(ReflectionHelpers.getStaticField<Handler>(PadBridge::class.java, "chordHandler").looper).idle()
|
||||
}
|
||||
|
||||
private class RecordingOutput : InputOutput {
|
||||
val events = mutableListOf<String>()
|
||||
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" }
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user