fix: hold XInput rumble until the game changes it, instead of stopping after a second (#938)

This commit is contained in:
TideGear authored and GitHub committed 2026-10-16 17:30:21 +00:00
1 parent 2c37f79701
commit 3f3ed3923b
7 files changed
+283 -43

No files matched your search

@@ -4,6 +4,9 @@ import android.net.LocalServerSocket
import android.content.SharedPreferences
import android.os.Build
import android.os.CombinedVibration
import android.os.Handler
import android.os.HandlerThread
import android.os.Looper
import android.os.SystemClock
import android.os.VibrationEffect
import android.os.Vibrator
@@ -21,15 +24,18 @@ class RumbleComponent : SessionPart() {
@Volatile private var server: LocalServerSocket? = null
private var phone: Motors? = null
private val playing = HashMap<Int, Motors>()
private val sent = HashMap<Int, Sent>()
private val effects = HashMap<Int, Effect>()
private val controllers = HashMap<Int, Motors?>()
private var preferences: SharedPreferences? = null
private var enabled = false
private var motorThread: HandlerThread? = null
private var handler: Handler? = null
private val preferenceListener = SharedPreferences.OnSharedPreferenceChangeListener { _, key ->
if (key == null || key == "rumble") refreshEnabled()
}
internal var controllerMotors: (Int) -> Motors? = ::lookupController
internal var motorLooper: Looper? = null
@Synchronized private fun refreshEnabled() {
enabled = app()?.let { ControllerPrefs.rumbleEnabled(it) } == true
@@ -42,6 +48,7 @@ class RumbleComponent : SessionPart() {
val vibrator = if (Build.VERSION.SDK_INT >= 31) ctx.getSystemService(VibratorManager::class.java)?.defaultVibrator
else @Suppress("DEPRECATION") ctx.getSystemService(Vibrator::class.java)
phone = vibrator?.takeIf { it.hasVibrator() }?.let { PhoneMotors(it) }
handler = Handler(motorLooper ?: HandlerThread("rumble-motors").also { motorThread = it; it.start() }.looper)
preferences = ControllerPrefs.prefs(ctx).also { it.registerOnSharedPreferenceChangeListener(preferenceListener) }
refreshEnabled()
server = s
@@ -66,6 +73,9 @@ class RumbleComponent : SessionPart() {
preferences = null
enabled = false
cancelAll()
handler = null
motorThread?.quitSafely()
motorThread = null
phone = null
controllers.clear()
val s = server
@@ -81,24 +91,39 @@ class RumbleComponent : SessionPart() {
val previous = playing[slot]
if (target !== previous) {
previous?.cancel()
sent.remove(slot)
forget(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(); sent.remove(slot); return }
if ((strong == 0 && weak == 0) || ms == 0) { target.cancel(); forget(slot); return }
val handler = handler ?: return
val effect = effects.getOrPut(slot) { Effect(slot) }
effect.want(strong, weak, ms)
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)
if (effect.playing(now)) return
handler.removeCallbacks(effect)
val wait = effect.at + MIN_GAP_MS - now
if (wait > 0) handler.postDelayed(effect, wait) else play(effect)
}
@Synchronized private fun play(effect: Effect) {
val target = playing[effect.slot]
if (target == null || effects[effect.slot] !== effect) return
val duration = if (effect.hold) HOLD_EFFECT_MS else effect.ms.toLong().coerceIn(1L, 5000L)
try {
target.play(strong, weak, duration)
sent[slot] = Sent(strong, weak, now, now + duration)
target.play(effect.strong, effect.weak, duration)
effect.played(SystemClock.uptimeMillis(), duration)
if (effect.hold) handler?.postDelayed(effect, KEEPALIVE_MS)
} catch (e: Exception) {
Log.w(TAG, "rumble: ${target.name}: $e")
}
}
private fun forget(slot: Int) {
effects.remove(slot)?.let { handler?.removeCallbacks(it) }
}
private fun target(slot: Int): Motors? {
val id = PadBridge.controllerForSlot(slot)
if (id == PadBridge.NO_CONTROLLER) return phone
@@ -109,10 +134,36 @@ class RumbleComponent : SessionPart() {
private fun cancelAll() {
playing.values.toSet().forEach { it.cancel() }
playing.clear()
sent.clear()
effects.values.forEach { handler?.removeCallbacks(it) }
effects.clear()
}
private data class Sent(val strong: Int, val weak: Int, val at: Long, val until: Long)
private inner class Effect(val slot: Int) : Runnable {
var strong = 0
var weak = 0
var ms = 0
var at = -MIN_GAP_MS
private var playedStrong = 0
private var playedWeak = 0
private var playedMs = 0
private var until = 0L
val hold get() = ms == HOLD_MS
fun want(strong: Int, weak: Int, ms: Int) { this.strong = strong; this.weak = weak; this.ms = ms }
fun playing(now: Long) = playedMs != 0 && playedStrong == strong && playedWeak == weak && playedMs == ms &&
(hold || now < until)
fun played(now: Long, duration: Long) {
playedStrong = strong
playedWeak = weak
playedMs = ms
at = now
until = now + duration
}
override fun run() = play(this)
}
internal interface Motors {
val name: String
@@ -137,20 +188,35 @@ class RumbleComponent : SessionPart() {
any = true
}
if (ids.size >= 2) { add(ids[0], strong); add(ids[1], weak) } else add(ids[0], max(strong, weak))
if (any) manager.vibrate(combined.combine()) else manager.cancel()
if (any) manager.vibrate(combined.combine()) else cancel()
}
// Some vendors' input-device vibrator HALs (Samsung's) don't halt a Bluetooth pad's running
// effect on cancel(), but a new effect always replaces it.
override fun cancel() {
val supersede = CombinedVibration.startParallel()
ids.forEach { supersede.addVibrator(it, VibrationEffect.createOneShot(1L, 1)) }
manager.vibrate(supersede.combine())
manager.cancel()
}
override fun cancel() = manager.cancel()
}
private class LegacyPadMotors(override val name: String, private val vibrator: Vibrator) : Motors {
override fun play(strong: Int, weak: Int, ms: Long) = vibrator.vibrate(oneShot(vibrator, max(strong, weak), ms))
override fun cancel() = vibrator.cancel()
override fun cancel() {
vibrator.vibrate(VibrationEffect.createOneShot(1L, 1))
vibrator.cancel()
}
}
companion object {
private const val TAG = "SessionService"
const val NAME = "droiddeck-rumble"
const val MIN_GAP_MS = 50L
// Must match RUMBLE_HOLD_MS in fakeinput_steam.cpp.
const val HOLD_MS = 0xFFFF
const val HOLD_EFFECT_MS = 2000L
const val KEEPALIVE_MS = 1500L
private fun oneShot(vibrator: Vibrator, strength: Int, ms: Long): VibrationEffect {
val amplitude = if (vibrator.hasAmplitudeControl()) (strength * 255L / 65535L).toInt().coerceIn(1, 255)