fix(input): detect which axis pair is the right stick (#2333)

* fix(input): detect controller right stick axes

* fix(input): read dedicated triggers when the right stick is on RX/RY

* fix(input): stabilize mixed controller axis detection

* fix(input): normalize ranged signed triggers

* fix(input): reuse the event's device for stick and trigger detection

Pass the InputDevice captured at the start of motion handling into the
right-stick and trigger helpers instead of looking it up again, so a pad
that disappears mid-event keeps its axis layout. Write each pad-mouse
trigger only when that side has an analog source, so a held digital
trigger on the other side is not cleared.
This commit is contained in:
MaxsTechReview authored and GitHub committed 2026-11-17 11:24:33 -05:00
1 parent 812e492302
commit c31f19a9f0
3 files changed
+229 -26

No files matched your search

@@ -12,6 +12,8 @@ import com.droiddeck.launcher.session.SessionState;
import java.io.File;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicIntegerArray;
public final class PadBridge {
@@ -56,6 +58,7 @@ public final class PadBridge {
private final FakeInputWriter[] playerWriters = new FakeInputWriter[SLOTS];
private final PadState[] playerStates = new PadState[SLOTS];
private final float[][] rawSticks = new float[SLOTS][4];
private final Map<Integer, Boolean> rightStickLayouts = new HashMap<>();
private final boolean[][] keyTriggers = new boolean[SLOTS][2];
private final int[] retainedSticks = new int[SLOTS];
private final PadState effectiveState = new PadState();
@@ -240,6 +243,7 @@ public final class PadBridge {
clearBackQamWrites();
releaseBackPaddles();
activeControllerId = NO_CONTROLLER;
rightStickLayouts.clear();
for (int slot = 0; slot < SLOTS; slot++) {
owners[slot] = NO_CONTROLLER;
partners[slot] = NO_CONTROLLER;
@@ -525,20 +529,23 @@ public final class PadBridge {
float lx = event.getAxisValue(MotionEvent.AXIS_X);
float ly = event.getAxisValue(MotionEvent.AXIS_Y);
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);
boolean rxRyStick = rightStickUsesRxRy(event, device, rightStickLayouts);
float rx = event.getAxisValue(rxRyStick ? MotionEvent.AXIS_RX : MotionEvent.AXIS_Z);
float ry = event.getAxisValue(rxRyStick ? MotionEvent.AXIS_RY : MotionEvent.AXIS_RZ);
retainStick(slot, JoyCon.LEFT, lx, ly);
retainStick(slot, JoyCon.RIGHT, rx, ry);
projectSticks(slot);
int source = event.getSource();
float lt = 0f;
float rt = 0f;
if (wired || hasAxis(device, source, MotionEvent.AXIS_LTRIGGER, MotionEvent.AXIS_BRAKE)) {
lt = axisTrigger(leftTrigger(event, wired), keyTriggers[slot], 0);
boolean signedLeft = signedZTriggers(event, device, wired, rxRyStick, true);
boolean signedRight = signedZTriggers(event, device, wired, rxRyStick, false);
if (signedLeft || hasAxis(device, source, MotionEvent.AXIS_LTRIGGER, MotionEvent.AXIS_BRAKE)) {
lt = axisTrigger(triggerValue(event, device, wired, rxRyStick, true), keyTriggers[slot], 0);
pad.leftTrigger = lt;
}
if (wired || hasAxis(device, source, MotionEvent.AXIS_RTRIGGER, MotionEvent.AXIS_GAS)) {
rt = axisTrigger(rightTrigger(event, wired), keyTriggers[slot], 1);
if (signedRight || hasAxis(device, source, MotionEvent.AXIS_RTRIGGER, MotionEvent.AXIS_GAS)) {
rt = axisTrigger(triggerValue(event, device, wired, rxRyStick, false), keyTriggers[slot], 1);
pad.rightTrigger = rt;
}
if (hasAxis(device, source, MotionEvent.AXIS_HAT_X)) {
@@ -596,6 +603,92 @@ public final class PadBridge {
return false;
}
static boolean rightStickUsesRxRy(MotionEvent event, InputDevice device, Map<Integer, Boolean> layouts) {
int source = event.getSource();
if (wiredXboxLayout(device, source)) return true;
boolean hasRxRy = hasAxis(device, source, MotionEvent.AXIS_RX)
&& hasAxis(device, source, MotionEvent.AXIS_RY);
boolean hasZRz = hasAxis(device, source, MotionEvent.AXIS_Z)
&& hasAxis(device, source, MotionEvent.AXIS_RZ);
if (!hasRxRy) return false;
if (!hasZRz) return true;
Boolean cached = layouts.get(event.getDeviceId());
if (cached != null) return cached;
boolean rxRyEvidence = atMinimum(device, source, event, MotionEvent.AXIS_Z, MotionEvent.AXIS_RZ)
&& nearCentre(device, source, event, MotionEvent.AXIS_RX, MotionEvent.AXIS_RY);
boolean zRzEvidence = atMinimum(device, source, event, MotionEvent.AXIS_RX, MotionEvent.AXIS_RY)
&& nearCentre(device, source, event, MotionEvent.AXIS_Z, MotionEvent.AXIS_RZ);
if (rxRyEvidence == zRzEvidence) return false;
boolean useRxRy = rxRyEvidence;
layouts.put(event.getDeviceId(), useRxRy);
return useRxRy;
}
static boolean signedZTriggers(MotionEvent event, InputDevice device, boolean wired, boolean rxRyStick, boolean left) {
if (wired) return true;
if (!rxRyStick) return false;
int source = event.getSource();
int signedAxis = left ? MotionEvent.AXIS_Z : MotionEvent.AXIS_RZ;
int dedicatedAxis = left ? MotionEvent.AXIS_LTRIGGER : MotionEvent.AXIS_RTRIGGER;
int alternateAxis = left ? MotionEvent.AXIS_BRAKE : MotionEvent.AXIS_GAS;
return hasAxis(device, source, signedAxis)
&& !hasAxis(device, source, dedicatedAxis, alternateAxis);
}
static float triggerValue(MotionEvent event, InputDevice device, boolean wired, boolean rxRyStick, boolean left) {
if (signedZTriggers(event, device, wired, rxRyStick, left)) {
int axis = left ? MotionEvent.AXIS_Z : MotionEvent.AXIS_RZ;
float value = event.getAxisValue(axis);
if (!wired && device != null) {
InputDevice.MotionRange range = device.getMotionRange(axis, event.getSource());
if (range != null && range.getMax() > range.getMin()) {
return Math.max(0f, Math.min(1f,
(value - range.getMin()) / (range.getMax() - range.getMin())));
}
}
return Math.max(0f, Math.min(1f, (value + 1f) / 2f));
}
int source = event.getSource();
int axis = left ? MotionEvent.AXIS_LTRIGGER : MotionEvent.AXIS_RTRIGGER;
int alternate = left ? MotionEvent.AXIS_BRAKE : MotionEvent.AXIS_GAS;
if (device == null) {
float value = event.getAxisValue(axis);
return value != 0f ? value : event.getAxisValue(alternate);
}
if (hasAxis(device, source, axis)) {
float value = event.getAxisValue(axis);
return value != 0f ? value : event.getAxisValue(alternate);
}
if (hasAxis(device, source, alternate)) return event.getAxisValue(alternate);
return 0f;
}
private static float restDistance(InputDevice device, int source, MotionEvent event, int xAxis, int yAxis) {
return (restDistance(device.getMotionRange(xAxis, source), event.getAxisValue(xAxis))
+ restDistance(device.getMotionRange(yAxis, source), event.getAxisValue(yAxis))) / 2f;
}
private static float restDistance(InputDevice.MotionRange range, float value) {
float span = range.getMax() - range.getMin();
if (span <= 0f) return 1f;
return Math.abs(value - (range.getMin() + range.getMax()) / 2f) / span;
}
private static boolean atMinimum(InputDevice device, int source, MotionEvent event, int xAxis, int yAxis) {
return atMinimum(device.getMotionRange(xAxis, source), event.getAxisValue(xAxis))
&& atMinimum(device.getMotionRange(yAxis, source), event.getAxisValue(yAxis));
}
private static boolean atMinimum(InputDevice.MotionRange range, float value) {
float span = range.getMax() - range.getMin();
return value - range.getMin() <= span * 0.02f;
}
private static boolean nearCentre(InputDevice device, int source, MotionEvent event, int xAxis, int yAxis) {
return restDistance(device.getMotionRange(xAxis, source), event.getAxisValue(xAxis)) <= 0.1f
&& restDistance(device.getMotionRange(yAxis, source), event.getAxisValue(yAxis)) <= 0.1f;
}
public interface TouchRoute {
boolean active();
@@ -1182,16 +1275,4 @@ public final class PadBridge {
&& device.getMotionRange(MotionEvent.AXIS_RZ, source) != null;
}
public static float leftTrigger(MotionEvent event, boolean wired) {
if (wired) return Math.max(0f, Math.min(1f, (event.getAxisValue(MotionEvent.AXIS_Z) + 1f) / 2f));
float value = event.getAxisValue(MotionEvent.AXIS_LTRIGGER);
return value != 0f ? value : event.getAxisValue(MotionEvent.AXIS_BRAKE);
}
public static float rightTrigger(MotionEvent event, boolean wired) {
if (wired) return Math.max(0f, Math.min(1f, (event.getAxisValue(MotionEvent.AXIS_RZ) + 1f) / 2f));
float value = event.getAxisValue(MotionEvent.AXIS_RTRIGGER);
return value != 0f ? value : event.getAxisValue(MotionEvent.AXIS_GAS);
}
}
@@ -42,6 +42,7 @@ class PadMouse(private val listener: Listener, private val clock: () -> Long = S
private val total = Sticks()
private val triggerAxes = HashMap<Int, Boolean>()
private val wiredLayout = HashMap<Int, Boolean>()
private val rightStickLayouts = HashMap<Int, Boolean>()
private val down = HashMap<Int, MutableSet<Int>>()
private val sent = HashMap<Int, IntArray>()
private val held = LinkedHashMap<Int, Int>()
@@ -87,14 +88,21 @@ class PadMouse(private val listener: Listener, private val clock: () -> Long = S
fun onMotion(event: MotionEvent): Boolean {
if (event.actionMasked != MotionEvent.ACTION_MOVE) return true
val sticks = physical.getOrPut(event.deviceId) { Sticks() }
val wired = wiredLayout.getOrPut(event.deviceId) { PadBridge.wiredXboxLayout(event) }
val device = event.device
val wired = wiredLayout.getOrPut(event.deviceId) { PadBridge.wiredXboxLayout(device, event.source) }
val rxRyStick = PadBridge.rightStickUsesRxRy(event, device, rightStickLayouts)
sticks.leftX = event.getAxisValue(MotionEvent.AXIS_X)
sticks.leftY = event.getAxisValue(MotionEvent.AXIS_Y)
sticks.rightX = scrollAxis(event, if (wired) MotionEvent.AXIS_RX else MotionEvent.AXIS_Z)
sticks.rightY = scrollAxis(event, if (wired) MotionEvent.AXIS_RY else MotionEvent.AXIS_RZ)
if (hasTriggerAxes(event)) {
sticks.leftTrigger = PadBridge.leftTrigger(event, wired)
sticks.rightTrigger = PadBridge.rightTrigger(event, wired)
sticks.rightX = scrollAxis(event, if (rxRyStick) MotionEvent.AXIS_RX else MotionEvent.AXIS_Z)
sticks.rightY = scrollAxis(event, if (rxRyStick) MotionEvent.AXIS_RY else MotionEvent.AXIS_RZ)
val signedLeft = PadBridge.signedZTriggers(event, device, wired, rxRyStick, true)
val signedRight = PadBridge.signedZTriggers(event, device, wired, rxRyStick, false)
val analogTriggers = hasTriggerAxes(event)
if (analogTriggers || signedLeft) {
sticks.leftTrigger = PadBridge.triggerValue(event, device, wired, rxRyStick, true)
}
if (analogTriggers || signedRight) {
sticks.rightTrigger = PadBridge.triggerValue(event, device, wired, rxRyStick, false)
}
val hatX = event.getAxisValue(MotionEvent.AXIS_HAT_X)
val hatY = event.getAxisValue(MotionEvent.AXIS_HAT_Y)
@@ -129,6 +137,7 @@ class PadMouse(private val listener: Listener, private val clock: () -> Long = S
physical.clear()
triggerAxes.clear()
wiredLayout.clear()
rightStickLayouts.clear()
touch.clear()
down.clear()
sent.clear()
@@ -207,6 +216,7 @@ class PadMouse(private val listener: Listener, private val clock: () -> Long = S
private fun hasTriggerAxes(event: android.view.InputEvent): Boolean = triggerAxes.getOrPut(event.deviceId) {
val device = event.device ?: return@getOrPut true
device.getMotionRange(MotionEvent.AXIS_LTRIGGER) != null || device.getMotionRange(MotionEvent.AXIS_BRAKE) != null ||
device.getMotionRange(MotionEvent.AXIS_RTRIGGER) != null || device.getMotionRange(MotionEvent.AXIS_GAS) != null ||
(event is MotionEvent && wiredLayout.getOrPut(event.deviceId) { PadBridge.wiredXboxLayout(event) })
}