diff --git a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt index 39a08eb546..24d4e1fe9e 100644 --- a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt @@ -551,7 +551,6 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { } val bridge = PadBridge(File(LinuxRuntime.sessionRoot(this), "dev/input")) - 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) } 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 d4bf04d9d0..90a9a01a1c 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/PadBridge.java +++ b/app/src/main/java/com/droiddeck/launcher/input/PadBridge.java @@ -67,7 +67,6 @@ public final class PadBridge { } 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; @@ -122,10 +121,6 @@ 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(); @@ -363,9 +358,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 && inputRouter == null) 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; diff --git a/app/src/main/java/com/droiddeck/launcher/ui/DeckControlsPanel.kt b/app/src/main/java/com/droiddeck/launcher/ui/DeckControlsPanel.kt index 766eed36b9..6671897af8 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/DeckControlsPanel.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/DeckControlsPanel.kt @@ -551,8 +551,7 @@ class DeckControlsPanel( override fun onDetachedFromWindow() { setClick(false) if (knobX != 0f || knobY != 0f) move(0f, 0f, 1f) - deckSource.close() - super.onDetachedFromWindow() + super.onDetachedFromWindow() } } @@ -610,8 +609,7 @@ class DeckControlsPanel( override fun onDetachedFromWindow() { set(0) - deckSource.close() - super.onDetachedFromWindow() + super.onDetachedFromWindow() } } @@ -672,8 +670,7 @@ class DeckControlsPanel( override fun onDetachedFromWindow() { set(emptySet()) - deckSource.close() - super.onDetachedFromWindow() + super.onDetachedFromWindow() } } 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/ui/DeckControlsPanelOwnershipTest.kt b/app/src/test/java/com/droiddeck/launcher/ui/DeckControlsPanelOwnershipTest.kt new file mode 100644 index 0000000000..b354801ade --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/ui/DeckControlsPanelOwnershipTest.kt @@ -0,0 +1,110 @@ +package com.droiddeck.launcher.ui + +import android.app.Activity +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.DeckControls +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 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 DeckControlsPanelOwnershipTest { + private val controller = Robolectric.buildActivity(Activity::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: DeckControlsPanel + + @Before fun setUp() { + DeckControls.releaseAll() + SessionPrefs.setGamepadSurface(activity, false, SessionPrefs.SURFACE_STICK) + SessionPrefs.setGamepadSurface(activity, true, SessionPrefs.SURFACE_TRACKPAD) + activity.setContentView(root) + panel = DeckControlsPanel(activity, {}, {}, {}, bridge) + root.addView(panel) + descendants(panel).filterIsInstance().first { it.text == activity.getString(R.string.deck_tab_gamepad) }.performClick() + 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() + } + + @Test fun switchingTheOtherStickKeepsTrackpadContactAndClickHeld() { + 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() + descendants(stick.parent as View).first { it.contentDescription == activity.getString(R.string.deck_surface_switch) }.performClick() + 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 removingFaceButtonsReleasesOnlyTheirOwnInput() { + val buttons = child("FaceButtons") + 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) + } + + 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/docs/development/unified-input-proposal.md b/docs/development/unified-input-proposal.md index 2bee4f784a..7343325533 100644 --- a/docs/development/unified-input-proposal.md +++ b/docs/development/unified-input-proposal.md @@ -95,6 +95,10 @@ trackpads and grips; it does not create another emulated controller. 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. diff --git a/docs/development/unified-input-validation.md b/docs/development/unified-input-validation.md index bc3481ed08..c2fcf4fa1a 100644 --- a/docs/development/unified-input-validation.md +++ b/docs/development/unified-input-validation.md @@ -96,6 +96,29 @@ 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. + ## Manual AYN Thor results The user performed the following checks in the installed candidate. The desktop