From 02a5be62da4dc1af8f5e18f49dc14c0185126f04 Mon Sep 17 00:00:00 2001 From: MaxsTechReview Date: Wed, 11 Nov 2026 21:57:56 -0500 Subject: [PATCH] fix(input): a controller that disconnects mid-event no longer closes the app (#2369) * fix(input): a controller that disconnects mid-event no longer closes the app KeyEvent.getDevice() looks the device up on every call, so a controller removed while its event was handled made a later lookup return null and crashed in noteDevice. Look the device up once per event and ignore events whose device is gone. * fix(input): retain controller layout during disconnect * fix(input): look up the controller once per key and motion event --- .../droiddeck/launcher/input/PadBridge.java | 39 ++++++----- .../droiddeck/launcher/input/JoyConPadTest.kt | 69 +++++++++++++++++++ .../launcher/input/PadBridgeRoutingTest.kt | 16 ++++- 3 files changed, 106 insertions(+), 18 deletions(-) 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 71744bf866..c75602585c 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/PadBridge.java +++ b/app/src/main/java/com/droiddeck/launcher/input/PadBridge.java @@ -290,7 +290,10 @@ public final class PadBridge { } public static boolean isControllerKeyEvent(KeyEvent event) { - InputDevice device = event.getDevice(); + return isControllerKeyEvent(event, event.getDevice()); + } + + static boolean isControllerKeyEvent(KeyEvent event, InputDevice device) { if (device == null || device.isVirtual() || isSystemVirtualInputName(device.getName())) return false; if (KeyEvent.isGamepadButton(event.getKeyCode())) { return device.supportsSource(InputDevice.SOURCE_GAMEPAD) @@ -315,7 +318,10 @@ public final class PadBridge { } public static boolean isControllerMotionEvent(MotionEvent event) { - InputDevice device = event.getDevice(); + return isControllerMotionEvent(event, event.getDevice()); + } + + static boolean isControllerMotionEvent(MotionEvent event, InputDevice device) { return event.isFromSource(InputDevice.SOURCE_JOYSTICK) && device != null && !device.isVirtual() && !isSystemVirtualInputName(device.getName()) && (device.supportsSource(InputDevice.SOURCE_GAMEPAD) @@ -376,10 +382,11 @@ public final class PadBridge { } public synchronized boolean onKeyEvent(KeyEvent event) { - if (!isControllerKeyEvent(event) || excluded.test(event.getDeviceId())) return false; - int slot = slotFor(event.getDevice()); + InputDevice device = event.getDevice(); + if (!isControllerKeyEvent(event, device) || excluded.test(event.getDeviceId())) return false; + int slot = slotFor(device); if (slot < 0) return false; - int side = JoyCon.side(event.getDevice()); + int side = JoyCon.side(device); if (side >= 0 && partners[slot] != NO_CONTROLLER && JoyCon.isRail(side, event.getScanCode())) return true; long timing = InputTiming.begin(); PadState pad = stateFor(slot); @@ -401,18 +408,18 @@ public final class PadBridge { case KeyEvent.KEYCODE_HOME: case KeyEvent.KEYCODE_MOVE_HOME: pad.press(PadState.GUIDE, pressed); break; case KeyEvent.KEYCODE_BUTTON_Z: - if (isPrimary(event)) DeckControls.INSTANCE.setGrip(isNovaController(event.getDevice()) ? DeckControls.R4 : DeckControls.L4, pressed); + if (isPrimary(event)) DeckControls.INSTANCE.setGrip(isNovaController(device) ? DeckControls.R4 : DeckControls.L4, pressed); break; case KeyEvent.KEYCODE_BUTTON_C: - if (isPrimary(event)) DeckControls.INSTANCE.setGrip(isNovaController(event.getDevice()) ? DeckControls.L4 : DeckControls.R4, pressed); + if (isPrimary(event)) DeckControls.INSTANCE.setGrip(isNovaController(device) ? DeckControls.L4 : DeckControls.R4, pressed); break; case KeyEvent.KEYCODE_BUTTON_L2: pad.leftTrigger = keyTrigger(pad.leftTrigger, pressed, event.getRepeatCount(), - analogTrigger(event.getDevice(), MotionEvent.AXIS_LTRIGGER, MotionEvent.AXIS_BRAKE), keyTriggers[slot], 0); + analogTrigger(device, MotionEvent.AXIS_LTRIGGER, MotionEvent.AXIS_BRAKE), keyTriggers[slot], 0); break; case KeyEvent.KEYCODE_BUTTON_R2: pad.rightTrigger = keyTrigger(pad.rightTrigger, pressed, event.getRepeatCount(), - analogTrigger(event.getDevice(), MotionEvent.AXIS_RTRIGGER, MotionEvent.AXIS_GAS), keyTriggers[slot], 1); + analogTrigger(device, MotionEvent.AXIS_RTRIGGER, MotionEvent.AXIS_GAS), keyTriggers[slot], 1); break; case KeyEvent.KEYCODE_DPAD_UP: pad.up = pressed; break; case KeyEvent.KEYCODE_DPAD_RIGHT: pad.right = pressed; break; @@ -420,7 +427,7 @@ public final class PadBridge { case KeyEvent.KEYCODE_DPAD_LEFT: pad.left = pressed; break; default: return false; } - noteDevice(event.getDevice(), slot); + noteDevice(device, slot); if (pressed && event.getRepeatCount() == 0 && !event.isCanceled()) noteChordInput(slot); if (pressed) notePlayerInput(); statButtons++; @@ -491,16 +498,16 @@ public final class PadBridge { } public synchronized boolean onMotionEvent(MotionEvent event) { - if (!isControllerMotionEvent(event) || excluded.test(event.getDeviceId())) return false; + InputDevice device = event.getDevice(); + if (!isControllerMotionEvent(event, device) || excluded.test(event.getDeviceId())) return false; if (event.getAction() != MotionEvent.ACTION_MOVE) return false; - int slot = slotFor(event.getDevice()); + int slot = slotFor(device); if (slot < 0) return false; long timing = InputTiming.begin(); PadState pad = stateFor(slot); - noteDevice(event.getDevice(), slot); + noteDevice(device, slot); statAxes++; scheduleStats(); - InputDevice device = event.getDevice(); int side = JoyCon.side(device); if (side >= 0) { int[] stick = JoyCon.stickAxes(side, device, event.getSource()); @@ -517,7 +524,7 @@ public final class PadBridge { } float lx = event.getAxisValue(MotionEvent.AXIS_X); float ly = event.getAxisValue(MotionEvent.AXIS_Y); - boolean wired = wiredXboxLayout(event); + boolean wired = wiredXboxLayout(device, event.getSource()); float rx = event.getAxisValue(wired ? MotionEvent.AXIS_RX : MotionEvent.AXIS_Z); float ry = event.getAxisValue(wired ? MotionEvent.AXIS_RY : MotionEvent.AXIS_RZ); retainStick(slot, JoyCon.LEFT, lx, ly); @@ -1007,7 +1014,7 @@ public final class PadBridge { } private boolean isPrimary(KeyEvent event) { - int id = event.getDevice().getId(); + int id = event.getDeviceId(); int first = firstPlayerSlot(); return owners[first] == id || partners[first] == id; } 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 0b8c930ed0..cff58d665e 100644 --- a/app/src/test/java/com/droiddeck/launcher/input/JoyConPadTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/input/JoyConPadTest.kt @@ -147,6 +147,75 @@ class JoyConPadTest { assertNull(RecordingWriter.pads[2]) } + @Test fun aControllerThatDisconnectsWhileItsKeyIsHandledIsIgnored() { + for (lookups in 0..4) { + val event = key(right, KeyEvent.KEYCODE_BUTTON_A, BTN_SOUTH) + PadBridgeRoutingTest.RegisteredKeyEvent.lookupsLeft = lookups + try { + val handled = bridge.onKeyEvent(event) + if (lookups == 0) assertFalse(handled) + if (lookups >= 2) assertTrue(handled) + } finally { PadBridgeRoutingTest.RegisteredKeyEvent.lookupsLeft = -1 } + } + assertTrue(press(right, KeyEvent.KEYCODE_BUTTON_A, BTN_SOUTH)) + } + + @Test fun aControllerThatDisconnectsAfterTheFirstLookupStillDeliversItsEvents() { + val device = controller(0x045e, 0x0b13, MotionEvent.AXIS_X, MotionEvent.AXIS_Y) + val down = key(device, KeyEvent.KEYCODE_BUTTON_A, BTN_SOUTH) + val up = key(device, KeyEvent.KEYCODE_BUTTON_A, BTN_SOUTH, KeyEvent.ACTION_UP) + val move = motion(device, MotionEvent.AXIS_X to 0.5f, MotionEvent.AXIS_Y to -0.25f) + try { + PadBridgeRoutingTest.RegisteredKeyEvent.lookupsLeft = 1 + assertTrue(bridge.onKeyEvent(down)) + assertEquals(0, PadBridgeRoutingTest.RegisteredKeyEvent.lookupsLeft) + assertTrue(pad(0).isDown(PadState.A)) + PadBridgeRoutingTest.RegisteredKeyEvent.lookupsLeft = 1 + assertTrue(bridge.onKeyEvent(up)) + assertFalse(pad(0).isDown(PadState.A)) + PadBridgeRoutingTest.RegisteredMotionEvent.lookupsLeft = 1 + assertTrue(bridge.onMotionEvent(move)) + assertEquals(0, PadBridgeRoutingTest.RegisteredMotionEvent.lookupsLeft) + assertEquals(0.5f, pad(0).leftX) + assertEquals(-0.25f, pad(0).leftY) + } finally { + PadBridgeRoutingTest.RegisteredKeyEvent.lookupsLeft = -1 + PadBridgeRoutingTest.RegisteredMotionEvent.lookupsLeft = -1 + } + } + + @Test fun nonControllerInputStaysUnhandledWithOneLookup() { + val keyboard = InputDeviceBuilder.newBuilder().setId(nextId++).setName("Keyboard").setDescriptor("keyboard") + .setExternal(true).setSources(InputDevice.SOURCE_KEYBOARD) + .setKeyboardType(InputDevice.KEYBOARD_TYPE_ALPHABETIC) + .setKeyCharacterMap(KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD)).build().also(::register) + val typed = KeyEvent(0L, 0L, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_A, 0, 0, keyboard.id, 30, 0, InputDevice.SOURCE_KEYBOARD) + val touch = MotionEvent.obtain(0L, 0L, MotionEvent.ACTION_MOVE, 1, arrayOf(MotionEvent.PointerProperties().apply { id = 0 }), + arrayOf(MotionEvent.PointerCoords().apply { x = 10f; y = 20f }), 0, 0, 1f, 1f, keyboard.id, 0, InputDevice.SOURCE_TOUCHSCREEN, 0) + try { + PadBridgeRoutingTest.RegisteredKeyEvent.lookupsLeft = 1 + assertFalse(bridge.onKeyEvent(typed)) + PadBridgeRoutingTest.RegisteredMotionEvent.lookupsLeft = 1 + assertFalse(bridge.onMotionEvent(touch)) + } finally { + PadBridgeRoutingTest.RegisteredKeyEvent.lookupsLeft = -1 + PadBridgeRoutingTest.RegisteredMotionEvent.lookupsLeft = -1 + } + assertNull(RecordingWriter.pads[0]) + } + + @Test fun wiredMotionKeepsItsAxesWhenDeviceDisappears() { + val device = controller(0x20d6, 0x2063, MotionEvent.AXIS_RX, MotionEvent.AXIS_RY, MotionEvent.AXIS_Z, MotionEvent.AXIS_RZ) + val event = motion(device, MotionEvent.AXIS_RX to 0.75f, MotionEvent.AXIS_RY to -0.5f, + MotionEvent.AXIS_Z to -1f, MotionEvent.AXIS_RZ to 1f) + PadBridgeRoutingTest.RegisteredMotionEvent.lookupsLeft = 2 + try { assertTrue(bridge.onMotionEvent(event)) } finally { PadBridgeRoutingTest.RegisteredMotionEvent.lookupsLeft = -1 } + assertEquals(0.75f, pad(0).rightX) + assertEquals(-0.5f, pad(0).rightY) + assertEquals(0f, pad(0).leftTrigger) + assertEquals(1f, pad(0).rightTrigger) + } + @Test fun joyConsHeldForVrAreLeftAlone() { bridge.setExcluded { it == left.id } assertFalse(bridge.onKeyEvent(key(left, KeyEvent.KEYCODE_BUTTON_L1, BTN_TL))) diff --git a/app/src/test/java/com/droiddeck/launcher/input/PadBridgeRoutingTest.kt b/app/src/test/java/com/droiddeck/launcher/input/PadBridgeRoutingTest.kt index a76da590a3..ea66f32136 100644 --- a/app/src/test/java/com/droiddeck/launcher/input/PadBridgeRoutingTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/input/PadBridgeRoutingTest.kt @@ -191,12 +191,24 @@ class PadBridgeRoutingTest { @Implements(KeyEvent::class) class RegisteredKeyEvent : ShadowKeyEvent() { @RealObject private lateinit var event: KeyEvent - @Implementation override fun getDevice(): InputDevice? = Shadow.directlyOn(event as InputEvent, InputEvent::class.java, "getDevice") + @Implementation override fun getDevice(): InputDevice? { + if (lookupsLeft == 0) return null + if (lookupsLeft > 0) lookupsLeft-- + return Shadow.directlyOn(event as InputEvent, InputEvent::class.java, "getDevice") + } + + companion object { @JvmField var lookupsLeft = -1 } } @Implements(MotionEvent::class) class RegisteredMotionEvent : ShadowMotionEvent() { @RealObject private lateinit var event: MotionEvent - @Implementation override fun getDevice(): InputDevice? = Shadow.directlyOn(event as InputEvent, InputEvent::class.java, "getDevice") + @Implementation override fun getDevice(): InputDevice? { + if (lookupsLeft == 0) return null + if (lookupsLeft > 0) lookupsLeft-- + return Shadow.directlyOn(event as InputEvent, InputEvent::class.java, "getDevice") + } + + companion object { @JvmField var lookupsLeft = -1 } } }