mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-18 12:28:40 +00:00
fix(input): stop controller rumble when the game that started it is gone (#2614)
* fix(input): stop controller rumble when the game that started it is gone A crashed game never sends the stop for a held rumble, so the keepalive re-armed it every 1.5 s until the session ended. A held effect now stops on the next keepalive once the process that sent it or the Steam game it started in has exited. The fake controller also stops its rumble when it's closed or the playing effect is erased, like the kernel does. * test: read the test's own pid from /proc/self * fix(input): track who plays rumble on each slot, check the game when a hold starts The process that plays an effect now owns it, and only the handle currently playing on a slot sends the stop when it closes or erases its effect. A held effect records whether a game is running when it starts, and keeps checking that its sender still exists even when its start time couldn't be read. * fix(input): only cache a running game, not its absence A new hold claimed right after a no-game result reused it, so a game that started within the cache window was never watched. * fix(input): treat a vanished sender of our own user as gone Guest processes run as the app's user, so the app can always see them in /proc. If one is missing at the first lookup it already exited; only senders of another user (root-spawned guests) are left unchecked. * fix(input): a stop only ends rumble the same process started The fake controller's playing-handle tracking is per process, so another process closing or erasing its older effect still sent a slot-wide stop. The app now ignores a stop whose sender isn't the one behind the slot's current effect; stops from an unknown sender work as before. --------- Co-authored-by: Tapuuk <177422350+Tapuuk@users.noreply.github.com>
This commit is contained in:
1 parent
6f7d7f827f
commit
fdf3d98c36
4 files changed
+407
-20
No files matched your search
@@ -19,6 +19,7 @@ import com.droiddeck.launcher.input.ControllerPrefs
|
||||
import com.droiddeck.launcher.input.JoyCon
|
||||
import com.droiddeck.launcher.input.PadBridge
|
||||
import java.io.DataInputStream
|
||||
import java.io.File
|
||||
import kotlin.math.max
|
||||
|
||||
class RumbleComponent : SessionPart() {
|
||||
@@ -39,6 +40,10 @@ class RumbleComponent : SessionPart() {
|
||||
|
||||
internal var controllerMotors: (Int) -> Motors? = ::lookupController
|
||||
internal var motorLooper: Looper? = null
|
||||
internal var processStart: (Int) -> String? = ::readProcessStart
|
||||
internal var gameRunning: () -> Boolean = { SteamGameProcess.running() }
|
||||
private var gameCheckedAt = Long.MIN_VALUE
|
||||
private var gameLastSeen = false
|
||||
|
||||
@Synchronized private fun refreshEnabled() {
|
||||
enabled = app()?.let { ControllerPrefs.rumbleEnabled(it) } == true
|
||||
@@ -66,7 +71,9 @@ class RumbleComponent : SessionPart() {
|
||||
try {
|
||||
val bytes = ByteArray(8)
|
||||
DataInputStream(client.inputStream).readFully(bytes)
|
||||
buzz(u16(bytes, 0), u16(bytes, 2), u16(bytes, 4), u16(bytes, 6))
|
||||
val peer = runCatching { client.peerCredentials }.getOrNull()
|
||||
buzz(u16(bytes, 0), u16(bytes, 2), u16(bytes, 4), u16(bytes, 6), peer?.pid ?: 0,
|
||||
peer?.uid == android.os.Process.myUid())
|
||||
} catch (e: Exception) {
|
||||
} finally {
|
||||
try { client.close() } catch (e: Exception) { }
|
||||
@@ -94,7 +101,9 @@ class RumbleComponent : SessionPart() {
|
||||
|
||||
private fun u16(b: ByteArray, at: Int): Int = (b[at].toInt() and 0xFF) or ((b[at + 1].toInt() and 0xFF) shl 8)
|
||||
|
||||
@Synchronized private fun buzz(strong: Int, weak: Int, ms: Int, slot: Int) {
|
||||
private fun buzz(strong: Int, weak: Int, ms: Int, slot: Int) = buzz(strong, weak, ms, slot, 0, false)
|
||||
|
||||
@Synchronized private fun buzz(strong: Int, weak: Int, ms: Int, slot: Int, sender: Int, ownUser: Boolean) {
|
||||
if (!enabled || quiet) return
|
||||
val target = target(slot)
|
||||
val previous = playing[slot]
|
||||
@@ -105,10 +114,19 @@ class RumbleComponent : SessionPart() {
|
||||
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(); forget(slot); return }
|
||||
if ((strong == 0 && weak == 0) || ms == 0) {
|
||||
val from = effects[slot]?.from ?: 0
|
||||
if (sender > 0 && from > 0 && from != sender) return
|
||||
target.cancel()
|
||||
forget(slot)
|
||||
return
|
||||
}
|
||||
val handler = handler ?: return
|
||||
val effect = effects.getOrPut(slot) { Effect(slot) }
|
||||
val held = effect.hold
|
||||
effect.want(strong, weak, ms)
|
||||
effect.from = sender
|
||||
if (effect.hold && (!held || effect.owner != sender)) effect.claim(sender, ownUser)
|
||||
val now = SystemClock.uptimeMillis()
|
||||
if (effect.playing(now)) return
|
||||
handler.removeCallbacks(effect)
|
||||
@@ -116,19 +134,37 @@ class RumbleComponent : SessionPart() {
|
||||
if (wait > 0) handler.postDelayed(effect, wait) else play(effect)
|
||||
}
|
||||
|
||||
@Synchronized private fun play(effect: Effect) {
|
||||
@Synchronized private fun play(effect: Effect, keepalive: Boolean = false) {
|
||||
val target = playing[effect.slot]
|
||||
if (target == null || effects[effect.slot] !== effect) return
|
||||
if (keepalive && effect.hold && effect.armed) effect.orphaned()?.let { why ->
|
||||
Log.i(TAG, "rumble: slot ${effect.slot} stopped, $why")
|
||||
target.cancel()
|
||||
forget(effect.slot)
|
||||
return
|
||||
}
|
||||
val duration = if (effect.hold) HOLD_EFFECT_MS else effect.ms.toLong().coerceIn(1L, 5000L)
|
||||
try {
|
||||
target.play(effect.strong, effect.weak, duration)
|
||||
effect.played(SystemClock.uptimeMillis(), duration)
|
||||
if (effect.hold) handler?.postDelayed(effect, KEEPALIVE_MS)
|
||||
if (effect.hold) {
|
||||
effect.armed = true
|
||||
handler?.postDelayed(effect, KEEPALIVE_MS)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.w(TAG, "rumble: ${target.name}: $e")
|
||||
}
|
||||
}
|
||||
|
||||
private fun gameRecently(): Boolean {
|
||||
val now = SystemClock.uptimeMillis()
|
||||
if (!gameLastSeen || now - gameCheckedAt >= GAME_CHECK_MS) {
|
||||
gameLastSeen = gameRunning()
|
||||
gameCheckedAt = now
|
||||
}
|
||||
return gameLastSeen
|
||||
}
|
||||
|
||||
private fun forget(slot: Int) {
|
||||
effects.remove(slot)?.let { handler?.removeCallbacks(it) }
|
||||
}
|
||||
@@ -170,8 +206,32 @@ class RumbleComponent : SessionPart() {
|
||||
private var playedWeak = 0
|
||||
private var playedMs = 0
|
||||
private var until = 0L
|
||||
var owner = 0
|
||||
var from = 0
|
||||
private var ownerSeen = false
|
||||
private var ownerStart: String? = null
|
||||
private var duringGame = false
|
||||
var armed = false
|
||||
val hold get() = ms == HOLD_MS
|
||||
|
||||
fun claim(sender: Int, ownUser: Boolean) {
|
||||
owner = sender
|
||||
val start = if (sender > 0) processStart(sender) else null
|
||||
ownerSeen = sender > 0 && (start != null || ownUser)
|
||||
ownerStart = start?.takeIf { it != UNKNOWN_START }
|
||||
duringGame = gameRecently()
|
||||
armed = false
|
||||
}
|
||||
|
||||
fun orphaned(): String? {
|
||||
if (ownerSeen) {
|
||||
val start = processStart(owner)
|
||||
if (start == null || (ownerStart != null && start != UNKNOWN_START && start != ownerStart))
|
||||
return "process $owner that started it is gone"
|
||||
}
|
||||
return if (duringGame && !gameRunning()) "the game it started in has exited" else null
|
||||
}
|
||||
|
||||
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 &&
|
||||
@@ -185,7 +245,7 @@ class RumbleComponent : SessionPart() {
|
||||
until = now + duration
|
||||
}
|
||||
|
||||
override fun run() = play(this)
|
||||
override fun run() = play(this, keepalive = true)
|
||||
}
|
||||
|
||||
internal interface Motors {
|
||||
@@ -256,6 +316,7 @@ class RumbleComponent : SessionPart() {
|
||||
const val HOLD_MS = 0xFFFF
|
||||
const val HOLD_EFFECT_MS = 2000L
|
||||
const val KEEPALIVE_MS = 1500L
|
||||
const val GAME_CHECK_MS = 1000L
|
||||
|
||||
private fun oneShot(vibrator: Vibrator, strength: Int, ms: Long): VibrationEffect {
|
||||
val amplitude = if (vibrator.hasAmplitudeControl()) (strength * 255L / 65535L).toInt().coerceIn(1, 255)
|
||||
@@ -263,6 +324,13 @@ class RumbleComponent : SessionPart() {
|
||||
return VibrationEffect.createOneShot(ms, amplitude)
|
||||
}
|
||||
|
||||
const val UNKNOWN_START = "?"
|
||||
|
||||
internal fun readProcessStart(pid: Int): String? = runCatching {
|
||||
val stat = File("/proc/$pid/stat").readText()
|
||||
stat.substring(stat.lastIndexOf(')') + 2).split(' ')[19]
|
||||
}.getOrElse { if (File("/proc/$pid").exists()) UNKNOWN_START else null }
|
||||
|
||||
private fun lookupController(id: Int): Motors? {
|
||||
val device = InputDevice.getDevice(id) ?: return null
|
||||
val name = "\"${device.name}\""
|
||||
|
||||
Reference in new issue
Block a user