From 7f0427b830a5c1506acfaccf21c699cef959e22a Mon Sep 17 00:00:00 2001 From: MaxsTechReview Date: Sat, 7 Nov 2026 15:48:41 -0500 Subject: [PATCH] fix(input): L2/R2 keys stop overriding analog trigger axes (#2205) fix(input): stop L2/R2 keys from overriding analog trigger axes Pads such as the GameSir X2 Pro send KEYCODE_BUTTON_L2/R2 alongside their analog trigger axes. Each key-down and every key repeat set the trigger to full, and the next motion event set it back to the axis value, so a held trigger jumped to full. Android repeats only the most recently pressed key, so with both triggers held one side kept being forced to full while the other tracked its axis. When a device reports an analog axis for a trigger side (LTRIGGER or BRAKE, RTRIGGER or GAS, or the wired Xbox Z/RZ layout), its key no longer drives that trigger. Key repeats never change a trigger value. Pads with digital-only triggers keep the key behaviour unchanged. --- .../droiddeck/launcher/input/PadBridge.java | 33 ++++++++-- .../launcher/input/PadPartialAxesTest.kt | 65 ++++++++++++++++++- 2 files changed, 89 insertions(+), 9 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 0d0dbc2f71..c7dd27a271 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/PadBridge.java +++ b/app/src/main/java/com/droiddeck/launcher/input/PadBridge.java @@ -405,8 +405,14 @@ public final class PadBridge { case KeyEvent.KEYCODE_BUTTON_C: if (isPrimary(event)) DeckControls.INSTANCE.setGrip(isNovaController(event.getDevice()) ? DeckControls.L4 : DeckControls.R4, pressed); break; - case KeyEvent.KEYCODE_BUTTON_L2: pad.leftTrigger = pressed ? 1f : 0f; break; - case KeyEvent.KEYCODE_BUTTON_R2: pad.rightTrigger = pressed ? 1f : 0f; break; + case KeyEvent.KEYCODE_BUTTON_L2: + pad.leftTrigger = keyTrigger(pad.leftTrigger, pressed, event.getRepeatCount(), + analogTrigger(event.getDevice(), MotionEvent.AXIS_LTRIGGER, MotionEvent.AXIS_BRAKE)); + break; + case KeyEvent.KEYCODE_BUTTON_R2: + pad.rightTrigger = keyTrigger(pad.rightTrigger, pressed, event.getRepeatCount(), + analogTrigger(event.getDevice(), MotionEvent.AXIS_RTRIGGER, MotionEvent.AXIS_GAS)); + break; case KeyEvent.KEYCODE_DPAD_UP: pad.up = pressed; break; case KeyEvent.KEYCODE_DPAD_RIGHT: pad.right = pressed; break; case KeyEvent.KEYCODE_DPAD_DOWN: pad.down = pressed; break; @@ -547,6 +553,16 @@ public final class PadBridge { return true; } + static float keyTrigger(float current, boolean pressed, int repeatCount, boolean analog) { + if (analog || repeatCount > 0) return current; + return pressed ? 1f : 0f; + } + + static boolean analogTrigger(InputDevice device, int... axes) { + return wiredXboxLayout(device, InputDevice.SOURCE_JOYSTICK) + || hasAxis(device, InputDevice.SOURCE_JOYSTICK, axes); + } + static boolean hasAxis(InputDevice device, int source, int... axes) { if (device == null) return false; for (int axis : axes) if (device.getMotionRange(axis, source) != null) return true; @@ -1126,14 +1142,17 @@ public final class PadBridge { } public static boolean wiredXboxLayout(MotionEvent event) { - InputDevice device = event.getDevice(); + return wiredXboxLayout(event.getDevice(), event.getSource()); + } + + static boolean wiredXboxLayout(InputDevice device, int source) { return device != null && ((device.getVendorId() == 0x20d6 && device.getProductId() == 0x2063) || (device.getVendorId() == 0x0e6f && device.getProductId() == 0x02f0)) - && device.getMotionRange(MotionEvent.AXIS_RX, event.getSource()) != null - && device.getMotionRange(MotionEvent.AXIS_RY, event.getSource()) != null - && device.getMotionRange(MotionEvent.AXIS_Z, event.getSource()) != null - && device.getMotionRange(MotionEvent.AXIS_RZ, event.getSource()) != null; + && device.getMotionRange(MotionEvent.AXIS_RX, source) != null + && device.getMotionRange(MotionEvent.AXIS_RY, source) != null + && device.getMotionRange(MotionEvent.AXIS_Z, source) != null + && device.getMotionRange(MotionEvent.AXIS_RZ, source) != null; } public static float leftTrigger(MotionEvent event, boolean wired) { diff --git a/app/src/test/java/com/droiddeck/launcher/input/PadPartialAxesTest.kt b/app/src/test/java/com/droiddeck/launcher/input/PadPartialAxesTest.kt index a0c5bc986b..2a2bade1c4 100644 --- a/app/src/test/java/com/droiddeck/launcher/input/PadPartialAxesTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/input/PadPartialAxesTest.kt @@ -94,10 +94,71 @@ class PadPartialAxesTest { assertTrue(pad().down) } + @Test fun analogTriggerAxesWinOverTheirKeys() { + val device = device(MotionEvent.AXIS_LTRIGGER, MotionEvent.AXIS_RTRIGGER) + motion(device, MotionEvent.AXIS_LTRIGGER to 0.3f) + key(device, KeyEvent.KEYCODE_BUTTON_L2) + assertEquals(0.3f, pad().leftTrigger) + key(device, KeyEvent.KEYCODE_BUTTON_L2, repeat = 1) + assertEquals(0.3f, pad().leftTrigger) + motion(device, MotionEvent.AXIS_LTRIGGER to 0.6f) + key(device, KeyEvent.KEYCODE_BUTTON_L2, repeat = 2) + assertEquals(0.6f, pad().leftTrigger) + motion(device, MotionEvent.AXIS_LTRIGGER to 0f) + key(device, KeyEvent.KEYCODE_BUTTON_L2, KeyEvent.ACTION_UP) + assertEquals(0f, pad().leftTrigger) + } + + @Test fun bothAnalogTriggersHeldStayIndependent() { + val device = device(MotionEvent.AXIS_BRAKE, MotionEvent.AXIS_GAS) + motion(device, MotionEvent.AXIS_BRAKE to 0.4f) + key(device, KeyEvent.KEYCODE_BUTTON_L2) + motion(device, MotionEvent.AXIS_BRAKE to 0.4f, MotionEvent.AXIS_GAS to 0.7f) + key(device, KeyEvent.KEYCODE_BUTTON_R2) + key(device, KeyEvent.KEYCODE_BUTTON_R2, repeat = 1) + assertEquals(0.4f, pad().leftTrigger) + assertEquals(0.7f, pad().rightTrigger) + motion(device, MotionEvent.AXIS_BRAKE to 0.9f, MotionEvent.AXIS_GAS to 0.2f) + key(device, KeyEvent.KEYCODE_BUTTON_R2, repeat = 2) + assertEquals(0.9f, pad().leftTrigger) + assertEquals(0.2f, pad().rightTrigger) + } + + @Test fun wiredXboxTriggerKeysDoNotOverrideTheirAxes() { + val device = device(MotionEvent.AXIS_RX, MotionEvent.AXIS_RY, MotionEvent.AXIS_Z, MotionEvent.AXIS_RZ, wired = true) + motion(device, MotionEvent.AXIS_Z to 0f, MotionEvent.AXIS_RZ to -1f) + key(device, KeyEvent.KEYCODE_BUTTON_L2) + key(device, KeyEvent.KEYCODE_BUTTON_R2) + assertEquals(0.5f, pad().leftTrigger) + assertEquals(0f, pad().rightTrigger) + } + + @Test fun digitalTriggerKeysStillPressAndRelease() { + val device = device() + key(device, KeyEvent.KEYCODE_BUTTON_L2) + key(device, KeyEvent.KEYCODE_BUTTON_R2) + key(device, KeyEvent.KEYCODE_BUTTON_L2, repeat = 1) + assertEquals(1f, pad().leftTrigger) + assertEquals(1f, pad().rightTrigger) + key(device, KeyEvent.KEYCODE_BUTTON_L2, KeyEvent.ACTION_UP) + assertEquals(0f, pad().leftTrigger) + assertEquals(1f, pad().rightTrigger) + key(device, KeyEvent.KEYCODE_BUTTON_R2, KeyEvent.ACTION_UP) + assertEquals(0f, pad().rightTrigger) + } + + @Test fun keyTriggerDecision() { + assertEquals(1f, PadBridge.keyTrigger(0f, true, 0, false)) + assertEquals(0f, PadBridge.keyTrigger(1f, false, 0, false)) + assertEquals(0.25f, PadBridge.keyTrigger(0.25f, true, 1, false)) + assertEquals(0.25f, PadBridge.keyTrigger(0.25f, true, 0, true)) + assertEquals(0.25f, PadBridge.keyTrigger(0.25f, false, 0, true)) + } + private fun pad(): PadState = requireNotNull(JoyConPadTest.RecordingWriter.pads[0]) - private fun key(device: InputDevice, code: Int) { - assertTrue(bridge.onKeyEvent(KeyEvent(0L, 0L, KeyEvent.ACTION_DOWN, code, 0, 0, + private fun key(device: InputDevice, code: Int, action: Int = KeyEvent.ACTION_DOWN, repeat: Int = 0) { + assertTrue(bridge.onKeyEvent(KeyEvent(0L, 0L, action, code, repeat, 0, device.id, 0, 0, InputDevice.SOURCE_GAMEPAD))) }