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
This commit is contained in:
MaxsTechReview authored and GitHub committed 2026-11-11 21:57:56 -05:00
1 parent 956f17239b
commit 02a5be62da
3 files changed
+106 -18

No files matched your search

@@ -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;
}