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 c75602585c..666a9d1ceb 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/PadBridge.java +++ b/app/src/main/java/com/droiddeck/launcher/input/PadBridge.java @@ -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 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 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); - } - } diff --git a/app/src/main/java/com/droiddeck/launcher/input/PadMouse.kt b/app/src/main/java/com/droiddeck/launcher/input/PadMouse.kt index 2d71451ccf..dccba8a1b6 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/PadMouse.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/PadMouse.kt @@ -42,6 +42,7 @@ class PadMouse(private val listener: Listener, private val clock: () -> Long = S private val total = Sticks() private val triggerAxes = HashMap() private val wiredLayout = HashMap() + private val rightStickLayouts = HashMap() private val down = HashMap>() private val sent = HashMap() private val held = LinkedHashMap() @@ -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) }) } 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 ae46c9d5e3..b79e813a9e 100644 --- a/app/src/test/java/com/droiddeck/launcher/input/PadPartialAxesTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/input/PadPartialAxesTest.kt @@ -83,6 +83,117 @@ class PadPartialAxesTest { assertEquals(-0.6f, pad().rightY) } + @Test fun onlyRxRyAxesProvideTheRightStick() { + val device = device(MotionEvent.AXIS_RX, MotionEvent.AXIS_RY) + motion(device, MotionEvent.AXIS_RX to 0.6f, MotionEvent.AXIS_RY to -0.6f) + assertEquals(0.6f, pad().rightX) + assertEquals(-0.6f, pad().rightY) + } + + @Test fun rxRyStickWithoutZRzKeepsTriggersReleased() { + val device = device(MotionEvent.AXIS_RX, MotionEvent.AXIS_RY) + motion(device, MotionEvent.AXIS_RX to 0f, MotionEvent.AXIS_RY to 0f) + assertEquals(0f, pad().leftTrigger) + assertEquals(0f, pad().rightTrigger) + } + + @Test fun rxRyStickReadsDedicatedTriggerAxes() { + val device = device(MotionEvent.AXIS_RX, MotionEvent.AXIS_RY, MotionEvent.AXIS_LTRIGGER, MotionEvent.AXIS_RTRIGGER) + motion(device, MotionEvent.AXIS_RX to 0.6f, MotionEvent.AXIS_RY to 0f, + MotionEvent.AXIS_LTRIGGER to 0.4f, MotionEvent.AXIS_RTRIGGER to 0f) + assertEquals(0.6f, pad().rightX) + assertEquals(0.4f, pad().leftTrigger) + assertEquals(0f, pad().rightTrigger) + } + + @Test fun leftTriggerFallsBackToBrakeWhenDedicatedAxisIsZero() { + val device = device(MotionEvent.AXIS_LTRIGGER, MotionEvent.AXIS_BRAKE) + motion(device, MotionEvent.AXIS_LTRIGGER to 0f, MotionEvent.AXIS_BRAKE to 0.7f) + assertEquals(0.7f, pad().leftTrigger) + } + + @Test fun rightTriggerFallsBackToGasWhenDedicatedAxisIsZero() { + val device = device(MotionEvent.AXIS_RTRIGGER, MotionEvent.AXIS_GAS) + motion(device, MotionEvent.AXIS_RTRIGGER to 0f, MotionEvent.AXIS_GAS to 0.8f) + assertEquals(0.8f, pad().rightTrigger) + } + + @Test fun onlyZRzAxesProvideTheRightStick() { + val device = device(MotionEvent.AXIS_Z, MotionEvent.AXIS_RZ) + motion(device, MotionEvent.AXIS_Z to 0.6f, MotionEvent.AXIS_RZ to -0.6f) + assertEquals(0.6f, pad().rightX) + assertEquals(-0.6f, pad().rightY) + } + + @Test fun bothPairsUseThePairAtRestNearCentreAsTheStick() { + val device = device(MotionEvent.AXIS_RX, MotionEvent.AXIS_RY, MotionEvent.AXIS_Z, MotionEvent.AXIS_RZ) + motion(device, MotionEvent.AXIS_RX to -1f, MotionEvent.AXIS_RY to -1f, + MotionEvent.AXIS_Z to 0f, MotionEvent.AXIS_RZ to 0f) + assertEquals(0f, pad().rightX) + assertEquals(0f, pad().rightY) + motion(device, MotionEvent.AXIS_RX to 0.2f, MotionEvent.AXIS_RY to -0.3f, + MotionEvent.AXIS_Z to 0.7f, MotionEvent.AXIS_RZ to -0.8f) + assertEquals(0.7f, pad().rightX) + assertEquals(-0.8f, pad().rightY) + } + + @Test fun bothPairsUseRxRyAsStickWhenZRzRestAtMinimum() { + val device = device(MotionEvent.AXIS_RX, MotionEvent.AXIS_RY, MotionEvent.AXIS_Z, MotionEvent.AXIS_RZ) + motion(device, MotionEvent.AXIS_RX to 0f, MotionEvent.AXIS_RY to 0f, + MotionEvent.AXIS_Z to -1f, MotionEvent.AXIS_RZ to -1f) + assertEquals(0f, pad().rightX) + assertEquals(0f, pad().rightY) + assertEquals(0f, pad().leftTrigger) + assertEquals(0f, pad().rightTrigger) + motion(device, MotionEvent.AXIS_RX to 0.6f, MotionEvent.AXIS_RY to -0.6f, + MotionEvent.AXIS_Z to 1f, MotionEvent.AXIS_RZ to 1f) + assertEquals(0.6f, pad().rightX) + assertEquals(-0.6f, pad().rightY) + assertEquals(1f, pad().leftTrigger) + assertEquals(1f, pad().rightTrigger) + } + + @Test fun nonWiredZeroToOneSignedZTriggersNormalizeFromTheirRange() { + val device = device(MotionEvent.AXIS_RX, MotionEvent.AXIS_RY, MotionEvent.AXIS_Z, MotionEvent.AXIS_RZ, + zeroToOneAxes = setOf(MotionEvent.AXIS_Z, MotionEvent.AXIS_RZ)) + motion(device, MotionEvent.AXIS_RX to 0f, MotionEvent.AXIS_RY to 0f, + MotionEvent.AXIS_Z to 0f, MotionEvent.AXIS_RZ to 0f) + assertEquals(0f, pad().leftTrigger) + assertEquals(0f, pad().rightTrigger) + motion(device, MotionEvent.AXIS_RX to 0.6f, MotionEvent.AXIS_RY to -0.6f, + MotionEvent.AXIS_Z to 1f, MotionEvent.AXIS_RZ to 1f) + assertEquals(1f, pad().leftTrigger) + assertEquals(1f, pad().rightTrigger) + } + + @Test fun movingFirstEventDoesNotLockTheWrongStickPair() { + val device = device(MotionEvent.AXIS_RX, MotionEvent.AXIS_RY, MotionEvent.AXIS_Z, MotionEvent.AXIS_RZ) + motion(device, MotionEvent.AXIS_RX to 1f, MotionEvent.AXIS_RY to 0f, + MotionEvent.AXIS_Z to 0f, MotionEvent.AXIS_RZ to 0f) + assertEquals(0f, pad().rightX) + assertEquals(0f, pad().rightY) + motion(device, MotionEvent.AXIS_RX to 0f, MotionEvent.AXIS_RY to 0f, + MotionEvent.AXIS_Z to -1f, MotionEvent.AXIS_RZ to -1f) + motion(device, MotionEvent.AXIS_RX to 0.6f, MotionEvent.AXIS_RY to -0.6f, + MotionEvent.AXIS_Z to -1f, MotionEvent.AXIS_RZ to -1f) + assertEquals(0.6f, pad().rightX) + assertEquals(-0.6f, pad().rightY) + } + + @Test fun dedicatedRightTriggerWinsOverSignedRightTrigger() { + val device = device(MotionEvent.AXIS_RX, MotionEvent.AXIS_RY, MotionEvent.AXIS_Z, + MotionEvent.AXIS_RZ, MotionEvent.AXIS_RTRIGGER) + motion(device, MotionEvent.AXIS_RX to 0f, MotionEvent.AXIS_RY to 0f, + MotionEvent.AXIS_Z to -1f, MotionEvent.AXIS_RZ to -1f) + motion(device, MotionEvent.AXIS_RX to 0.6f, MotionEvent.AXIS_RY to -0.6f, + MotionEvent.AXIS_Z to -1f, MotionEvent.AXIS_RZ to -1f, + MotionEvent.AXIS_RTRIGGER to 0.7f) + assertEquals(0f, pad().leftTrigger) + assertEquals(0.7f, pad().rightTrigger) + assertEquals(0.6f, pad().rightX) + assertEquals(-0.6f, pad().rightY) + } + @Test fun sticksWithoutTriggerOrHatAxesPreserveAllDigitalControls() { val device = device() key(device, KeyEvent.KEYCODE_BUTTON_L2) @@ -208,14 +319,15 @@ class PadPartialAxesTest { try { assertTrue(bridge.onMotionEvent(event)) } finally { event.recycle() } } - private fun device(vararg axes: Int, wired: Boolean = false): InputDevice { + private fun device(vararg axes: Int, wired: Boolean = false, zeroToOneAxes: Set = emptySet()): InputDevice { val builder = InputDeviceBuilder.newBuilder().setId(60).setName("partial pad") .setDescriptor("partial-pad").setExternal(true) .setSources(InputDevice.SOURCE_GAMEPAD or InputDevice.SOURCE_JOYSTICK) .setKeyboardType(InputDevice.KEYBOARD_TYPE_NON_ALPHABETIC) .setVendorId(if (wired) 0x20d6 else 0).setProductId(if (wired) 0x2063 else 0) for (axis in axes.toList() + MotionEvent.AXIS_X) { - builder.addMotionRange(axis, InputDevice.SOURCE_JOYSTICK, -1f, 1f, 0f, 0f, 0f) + val min = if (axis in zeroToOneAxes) 0f else -1f + builder.addMotionRange(axis, InputDevice.SOURCE_JOYSTICK, min, 1f, 0f, 0f, 0f) } return builder.build().also { shadowOf(RuntimeEnvironment.getApplication().getSystemService(InputManager::class.java)).addInputDevice(it)