diff --git a/app/src/main/java/com/droiddeck/launcher/input/AxisPacing.kt b/app/src/main/java/com/droiddeck/launcher/input/AxisPacing.kt new file mode 100644 index 0000000000..2170ab5030 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/input/AxisPacing.kt @@ -0,0 +1,39 @@ +package com.droiddeck.launcher.input + +internal class AxisPacing(private val intervalMs: Long) { + private var lastWriteAt = 0L + private var written = false + private var heldUntil = NOT_HELD + + fun dueAt(now: Long, axesOnly: Boolean): Long = + if (axesOnly && written && now - lastWriteAt < intervalMs) lastWriteAt + intervalMs else now + + fun wrote(at: Long) { + lastWriteAt = at + written = true + } + + fun hold(due: Long): Boolean { + if (heldUntil != NOT_HELD) return false + heldUntil = due + return true + } + + fun release(now: Long): Boolean { + if (heldUntil == NOT_HELD || now < heldUntil) return false + heldUntil = NOT_HELD + return true + } + + fun cancel(): Boolean { + if (heldUntil == NOT_HELD) return false + heldUntil = NOT_HELD + return true + } + + companion object { + private const val NOT_HELD = Long.MIN_VALUE + + @JvmStatic fun keepsMoving(before: Int, after: Int): Boolean = before == after || (before != 0 && after != 0) + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/input/FakeInputWriter.java b/app/src/main/java/com/droiddeck/launcher/input/FakeInputWriter.java index 4675546d61..dc847e90ad 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/FakeInputWriter.java +++ b/app/src/main/java/com/droiddeck/launcher/input/FakeInputWriter.java @@ -1,5 +1,9 @@ package com.droiddeck.launcher.input; +import android.os.Handler; +import android.os.HandlerThread; +import android.os.Process; +import android.os.SystemClock; import android.util.Log; import java.io.File; @@ -128,6 +132,16 @@ public class FakeInputWriter { private static final Object RING_LOCK = new Object(); private static final RingSlot[] RING_SLOTS = new RingSlot[MAX_FAKE_INPUT_SLOTS]; + private static final long AXIS_REPORT_INTERVAL_MS = 4; + + private static final class Pacing { + static final Handler HANDLER; + static { + HandlerThread thread = new HandlerThread("pad-pacing", Process.THREAD_PRIORITY_DISPLAY); + thread.start(); + HANDLER = new Handler(thread.getLooper()); + } + } static { System.loadLibrary("sdnative"); @@ -156,6 +170,9 @@ public class FakeInputWriter { private boolean hasChanges = false; private boolean pendingFullResend = false; private boolean forceResend = false; + private final AxisPacing axisPacing = new AxisPacing(AXIS_REPORT_INTERVAL_MS); + private final PadState pacedPad = new PadState(); + private final Runnable pacedWrite = this::writePacedPad; public FakeInputWriter(String fakeInputPath, int slot) { this.slot = slot; @@ -711,10 +728,12 @@ public class FakeInputWriter { } public synchronized void close() { + cancelPacedWrite(); isOpen = false; } public synchronized void reset() { + cancelPacedWrite(); if (!isOpen && !open()) return; @@ -822,6 +841,53 @@ public class FakeInputWriter { } public synchronized void writePad(PadState state) { + long now = SystemClock.uptimeMillis(); + long due = axisPacing.dueAt(now, !pendingFullResend && onlyAxesMove(state)); + if (due > now) { + pacedPad.copyFrom(state); + if (axisPacing.hold(due)) + Pacing.HANDLER.postAtTime(pacedWrite, due); + return; + } + cancelPacedWrite(); + writePadNow(state); + } + + private synchronized void writePacedPad() { + if (axisPacing.release(SystemClock.uptimeMillis())) + writePadNow(pacedPad); + } + + private void cancelPacedWrite() { + if (axisPacing.cancel()) + Pacing.HANDLER.removeCallbacks(pacedWrite); + } + + private boolean onlyAxesMove(PadState state) { + for (int i = 0; i < 10; i++) { + if (prevButtonStates[i] != state.isDown(i)) + return false; + } + return prevButtonStates[SNAPSHOT_IDX_MODE] == state.isDown(PadState.GUIDE) + && prevQam == state.isDown(PadState.QAM) + && prevHatX == state.hatX() && prevHatY == state.hatY() + && AxisPacing.keepsMoving(prevThumbLX, stickValue(state.leftX)) + && AxisPacing.keepsMoving(prevThumbLY, stickValue(state.leftY)) + && AxisPacing.keepsMoving(prevThumbRX, stickValue(state.rightX)) + && AxisPacing.keepsMoving(prevThumbRY, stickValue(state.rightY)) + && AxisPacing.keepsMoving(prevTriggerL, triggerValue(state.leftTrigger)) + && AxisPacing.keepsMoving(prevTriggerR, triggerValue(state.rightTrigger)); + } + + private static int stickValue(float value) { + return (int) (value * 32767); + } + + private static int triggerValue(float value) { + return (int) (value * 255); + } + + private void writePadNow(PadState state) { if (!isOpen && !open()) return; @@ -845,10 +911,10 @@ public class FakeInputWriter { hasChanges = true; } - int lx = (int) (state.leftX * 32767); - int ly = (int) (state.leftY * 32767); - int rx = (int) (state.rightX * 32767); - int ry = (int) (state.rightY * 32767); + int lx = stickValue(state.leftX); + int ly = stickValue(state.leftY); + int rx = stickValue(state.rightX); + int ry = stickValue(state.rightY); if (forceResend || lx != prevThumbLX) { prevThumbLX = lx; @@ -867,8 +933,8 @@ public class FakeInputWriter { writeEvent(EV_ABS, ABS_RY, ry); } - int tl = (int) (state.leftTrigger * 255); - int tr = (int) (state.rightTrigger * 255); + int tl = triggerValue(state.leftTrigger); + int tr = triggerValue(state.rightTrigger); if (forceResend || tl != prevTriggerL) { prevTriggerL = tl; writeEvent(EV_ABS, ABS_BRAKE, tl); @@ -895,6 +961,8 @@ public class FakeInputWriter { if (!flushBuffer()) { Log.e(TAG, "Gamepad write error: fake input mmap ring unavailable"); pendingFullResend = true; + } else { + axisPacing.wrote(SystemClock.uptimeMillis()); } } forceResend = false; diff --git a/app/src/test/java/com/droiddeck/launcher/input/AxisPacingTest.kt b/app/src/test/java/com/droiddeck/launcher/input/AxisPacingTest.kt new file mode 100644 index 0000000000..d88a9e7964 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/input/AxisPacingTest.kt @@ -0,0 +1,102 @@ +package com.droiddeck.launcher.input + +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Test + +class AxisPacingTest { + @Test fun theFirstReportIsWrittenAtOnce() { + assertEquals(100L, AxisPacing(4).dueAt(100, true)) + } + + @Test fun stickMotionInsideTheIntervalWaitsForItsEnd() { + val pacing = AxisPacing(4) + pacing.wrote(100) + assertEquals(104L, pacing.dueAt(101, true)) + assertEquals(104L, pacing.dueAt(103, true)) + assertEquals(104L, pacing.dueAt(104, true)) + assertEquals(110L, pacing.dueAt(110, true)) + } + + @Test fun buttonsAndRestTransitionsAreNeverHeldBack() { + val pacing = AxisPacing(4) + pacing.wrote(100) + assertEquals(101L, pacing.dueAt(101, false)) + } + + @Test fun onlyMotionThatStaysOffRestIsPaced() { + assertTrue(AxisPacing.keepsMoving(0, 0)) + assertTrue(AxisPacing.keepsMoving(1200, 1300)) + assertTrue(AxisPacing.keepsMoving(-40, 25)) + assertFalse(AxisPacing.keepsMoving(0, 900)) + assertFalse(AxisPacing.keepsMoving(900, 0)) + } + + @Test fun theIntervalRunsFromTheFlushNotFromTheRequest() { + val pacing = AxisPacing(4) + assertEquals(100L, pacing.dueAt(100, true)) + pacing.wrote(110) + assertEquals(114L, pacing.dueAt(111, true)) + } + + @Test fun aHeldReportIsReleasedOnlyAtItsDeadline() { + val pacing = AxisPacing(4) + assertTrue(pacing.hold(104)) + assertFalse(pacing.hold(104)) + assertFalse(pacing.release(103)) + assertTrue(pacing.release(104)) + assertFalse(pacing.release(105)) + } + + @Test fun aCallbackFromACancelledHoldCannotPublishTheNextHoldEarly() { + val pacing = AxisPacing(4) + assertTrue(pacing.hold(104)) + assertTrue(pacing.cancel()) + pacing.wrote(104) + assertTrue(pacing.hold(pacing.dueAt(105, true))) + assertFalse(pacing.release(105)) + assertTrue(pacing.release(108)) + } + + @Test fun cancellingWithNothingHeldRemovesNoCallback() { + val pacing = AxisPacing(4) + assertFalse(pacing.cancel()) + assertTrue(pacing.hold(104)) + assertTrue(pacing.release(104)) + assertFalse(pacing.cancel()) + } + + @Test fun aThousandHertzPadReachesTheGuestAtTheIntervalRate() { + val writes = feed(AxisPacing(4), sampleMs = 1, durationMs = 1000) + assertTrue("$writes writes", writes in 249..251) + } + + @Test fun aPadSlowerThanTheIntervalIsNeverDelayed() { + val pacing = AxisPacing(4) + for (now in 0L until 1000L step 5) { + assertEquals(now, pacing.dueAt(now, true)) + pacing.wrote(now) + } + } + + private fun feed(pacing: AxisPacing, sampleMs: Long, durationMs: Long): Int { + var writes = 0 + var pendingAt = -1L + for (now in 0L until durationMs step sampleMs) { + if (pendingAt in 0..now) { + pacing.wrote(pendingAt) + writes++ + pendingAt = -1 + } + val due = pacing.dueAt(now, true) + if (due <= now) { + pacing.wrote(now) + writes++ + } else if (pendingAt < 0) { + pendingAt = due + } + } + return writes + } +}