From e8821f4efc2ca7bd9e5e01fd98c22c289e840bc1 Mon Sep 17 00:00:00 2001 From: kmnkv1 <95131920+kmnkv1@users.noreply.github.com> Date: Sun, 11 Oct 2026 00:34:19 +0700 Subject: [PATCH] Rumble: don't restart the vibrator on every force-feedback packet (#514) fix: stop restarting the vibrator on every force-feedback packet --- .../launcher/session/RumbleComponent.kt | 20 +++++++++++++++++-- .../launcher/session/RumbleComponentTest.kt | 18 +++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/droiddeck/launcher/session/RumbleComponent.kt b/app/src/main/java/com/droiddeck/launcher/session/RumbleComponent.kt index 014b67bc33..4adf1c0a56 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/RumbleComponent.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/RumbleComponent.kt @@ -4,6 +4,7 @@ import android.net.LocalServerSocket import android.content.SharedPreferences import android.os.Build import android.os.CombinedVibration +import android.os.SystemClock import android.os.VibrationEffect import android.os.Vibrator import android.os.VibratorManager @@ -33,6 +34,8 @@ class RumbleComponent : SessionPart() { private var phone: Motors? = null /** Per slot, where its last effect went, so the next one (or a stop) can end it there. */ private val playing = HashMap() + /** Per slot, the last effect handed to the motors and when, to drop the repeats games stream. */ + private val sent = HashMap() /** Each controller's motors, looked up once; null when it has none. */ private val controllers = HashMap() private var preferences: SharedPreferences? = null @@ -95,13 +98,21 @@ class RumbleComponent : SessionPart() { val previous = playing[slot] if (target !== previous) { previous?.cancel() + sent.remove(slot) if (target == null) playing.remove(slot) else playing[slot] = target target?.let { Log.i(TAG, "rumble: slot $slot playing on ${it.name}") } } if (target == null) return - if ((strong == 0 && weak == 0) || ms == 0) { target.cancel(); return } + if ((strong == 0 && weak == 0) || ms == 0) { target.cancel(); sent.remove(slot); return } + // Games re-send their effect every frame; restarting the motor that often floods the haptics + // driver. ponytail: a change inside MIN_GAP_MS is dropped, the game's next packet brings it. + val now = SystemClock.uptimeMillis() + val last = sent[slot] + if (last != null && (now - last.at < MIN_GAP_MS || (last.strong == strong && last.weak == weak && now < last.until))) return + val duration = ms.toLong().coerceIn(1L, 5000L) try { - target.play(strong, weak, ms.toLong().coerceIn(1L, 5000L)) + target.play(strong, weak, duration) + sent[slot] = Sent(strong, weak, now, now + duration) } catch (e: Exception) { Log.w(TAG, "rumble: ${target.name}: $e") } @@ -118,8 +129,11 @@ class RumbleComponent : SessionPart() { private fun cancelAll() { playing.values.toSet().forEach { it.cancel() } playing.clear() + sent.clear() } + private data class Sent(val strong: Int, val weak: Int, val at: Long, val until: Long) + /** One place an effect can play. Strengths are the evdev 0..65535 magnitudes. */ internal interface Motors { val name: String @@ -160,6 +174,8 @@ class RumbleComponent : SessionPart() { private const val TAG = "SessionService" /** Must match the fake evdev layer (fakeinput_steam.cpp). */ const val NAME = "droiddeck-rumble" + /** The shortest time between two effects started on one slot's motors. */ + const val MIN_GAP_MS = 50L private fun oneShot(vibrator: Vibrator, strength: Int, ms: Long): VibrationEffect { val amplitude = if (vibrator.hasAmplitudeControl()) (strength * 255L / 65535L).toInt().coerceIn(1, 255) diff --git a/app/src/test/java/com/droiddeck/launcher/session/RumbleComponentTest.kt b/app/src/test/java/com/droiddeck/launcher/session/RumbleComponentTest.kt index 280db6e549..55bb4f0d19 100644 --- a/app/src/test/java/com/droiddeck/launcher/session/RumbleComponentTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/session/RumbleComponentTest.kt @@ -78,6 +78,24 @@ class RumbleComponentTest { assertFalse("the phone buzzed as well", shadowOf(vibrator).isVibrating) } + @Test fun repeatedAndRapidEffectsDoNotRestartTheMotors() { + val pad = FakeMotors() + rumble.controllerMotors = { pad } + activeController(PAD_ID) + repeat(5) { effect(strong = 40000, weak = 1000) } + effect(strong = 20000, weak = 1000) + assertEquals("a stream of packets restarted the motor each time", 1, pad.played.size) + shadowOf(Looper.getMainLooper()).idleFor(java.time.Duration.ofMillis(RumbleComponent.MIN_GAP_MS)) + effect(strong = 40000, weak = 1000) + assertEquals("the same strength, still playing, was started again", 1, pad.played.size) + effect(strong = 20000, weak = 1000) + assertEquals(Triple(20000, 1000, 5000L), pad.played.last()) + effect(strong = 0, weak = 0) + assertFalse(pad.vibrating) + effect(strong = 20000, weak = 1000) + assertTrue("an effect right after a stop was dropped", pad.vibrating) + } + @Test fun aControllerWithoutMotorsFallsBackToThePhone() { rumble.controllerMotors = { null } activeController(PAD_ID)