mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-18 20:36:04 +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
@@ -36,7 +36,12 @@ class RumbleComponentTest {
|
||||
vibrator = if (Build.VERSION.SDK_INT >= 31) context.getSystemService(VibratorManager::class.java).defaultVibrator
|
||||
else context.getSystemService(Vibrator::class.java)
|
||||
shadowOf(vibrator).setHasVibrator(true)
|
||||
rumble = RumbleComponent().also { it.motorLooper = Looper.getMainLooper(); it.attach(context); it.start() }
|
||||
rumble = RumbleComponent().also {
|
||||
it.motorLooper = Looper.getMainLooper()
|
||||
it.gameRunning = { false }
|
||||
it.attach(context)
|
||||
it.start()
|
||||
}
|
||||
}
|
||||
|
||||
@After fun stopListener() { rumble.stop() }
|
||||
@@ -115,6 +120,143 @@ class RumbleComponentTest {
|
||||
assertFalse(pad.vibrating)
|
||||
}
|
||||
|
||||
@Test fun aHeldEffectStopsWhenTheProcessThatStartedItIsGone() {
|
||||
val pad = FakeMotors()
|
||||
var alive = true
|
||||
rumble.controllerMotors = { pad }
|
||||
rumble.processStart = { pid -> if (pid == SENDER && alive) "1234" else null }
|
||||
rumble.gameRunning = { false }
|
||||
activeController(PAD_ID)
|
||||
effectFrom(SENDER, ms = RumbleComponent.HOLD_MS)
|
||||
idle(RumbleComponent.KEEPALIVE_MS * 2 + 100)
|
||||
assertTrue(pad.vibrating)
|
||||
alive = false
|
||||
idle(RumbleComponent.KEEPALIVE_MS * 2 + 100)
|
||||
assertFalse("a crashed game's rumble kept going", pad.vibrating)
|
||||
val played = pad.played.size
|
||||
idle(RumbleComponent.KEEPALIVE_MS * 4)
|
||||
assertEquals("the keepalive restarted the motors", played, pad.played.size)
|
||||
}
|
||||
|
||||
@Test fun aHeldEffectStopsWhenTheGameItStartedInExits() {
|
||||
val pad = FakeMotors()
|
||||
var game = true
|
||||
rumble.controllerMotors = { pad }
|
||||
rumble.processStart = { "1234" }
|
||||
rumble.gameRunning = { game }
|
||||
activeController(PAD_ID)
|
||||
effectFrom(SENDER, ms = RumbleComponent.HOLD_MS)
|
||||
idle(RumbleComponent.KEEPALIVE_MS * 2 + 100)
|
||||
assertTrue(pad.vibrating)
|
||||
game = false
|
||||
idle(RumbleComponent.KEEPALIVE_MS + 100)
|
||||
assertFalse("rumble outlived the game", pad.vibrating)
|
||||
game = true
|
||||
effectFrom(SENDER, ms = RumbleComponent.HOLD_MS)
|
||||
assertTrue("the next game's rumble was dropped", pad.vibrating)
|
||||
}
|
||||
|
||||
@Test fun aProcessStartTimeIsReadForALiveProcessOnly() {
|
||||
val self = java.io.File("/proc/self").canonicalFile.name.toInt()
|
||||
val start = RumbleComponent.readProcessStart(self)
|
||||
assertNotNull(start)
|
||||
assertNotEquals(RumbleComponent.UNKNOWN_START, start)
|
||||
assertEquals("the start time changed for the same process", start, RumbleComponent.readProcessStart(self))
|
||||
assertNull(RumbleComponent.readProcessStart(Int.MAX_VALUE))
|
||||
}
|
||||
|
||||
@Test fun aGameThatExitsBeforeTheFirstKeepaliveStillStopsItsRumble() {
|
||||
val pad = FakeMotors()
|
||||
var game = true
|
||||
rumble.controllerMotors = { pad }
|
||||
rumble.processStart = { "1234" }
|
||||
rumble.gameRunning = { game }
|
||||
activeController(PAD_ID)
|
||||
effectFrom(SENDER, ms = RumbleComponent.HOLD_MS)
|
||||
game = false
|
||||
idle(RumbleComponent.KEEPALIVE_MS + 100)
|
||||
assertFalse("rumble outlived a game that exited right away", pad.vibrating)
|
||||
}
|
||||
|
||||
@Test fun aGameStartingRightAfterAnotherHoldIsStillWatched() {
|
||||
val pad = FakeMotors()
|
||||
var game = false
|
||||
rumble.controllerMotors = { pad }
|
||||
rumble.processStart = { "1234" }
|
||||
rumble.gameRunning = { game }
|
||||
activeController(PAD_ID)
|
||||
effectFrom(SENDER, ms = RumbleComponent.HOLD_MS)
|
||||
effectFrom(SENDER, strong = 0, weak = 0)
|
||||
game = true
|
||||
idle(RumbleComponent.MIN_GAP_MS)
|
||||
effectFrom(SENDER, ms = RumbleComponent.HOLD_MS)
|
||||
assertTrue(pad.vibrating)
|
||||
game = false
|
||||
idle(RumbleComponent.KEEPALIVE_MS + 100)
|
||||
assertFalse("a stale no-game result left the hold unwatched", pad.vibrating)
|
||||
}
|
||||
|
||||
@Test fun anotherProcessClosingItsOlderRumbleLeavesTheNewerOneOnTheSlot() {
|
||||
val pad = FakeMotors()
|
||||
rumble.controllerMotors = { pad }
|
||||
rumble.processStart = { "1234" }
|
||||
activeController(PAD_ID)
|
||||
effectFrom(SENDER, ms = RumbleComponent.HOLD_MS)
|
||||
idle(RumbleComponent.MIN_GAP_MS)
|
||||
effectFrom(OTHER_SENDER, strong = 30000, ms = RumbleComponent.HOLD_MS)
|
||||
effectFrom(SENDER, strong = 0, weak = 0)
|
||||
assertTrue("an older process's stop cancelled newer rumble", pad.vibrating)
|
||||
effectFrom(OTHER_SENDER, strong = 0, weak = 0)
|
||||
assertFalse(pad.vibrating)
|
||||
}
|
||||
|
||||
@Test fun aSenderOfOurOwnUserThatExitedBeforeItsFirstLookupStopsItsRumble() {
|
||||
val pad = FakeMotors()
|
||||
rumble.controllerMotors = { pad }
|
||||
rumble.processStart = { null }
|
||||
activeController(PAD_ID)
|
||||
effectFrom(SENDER, ms = RumbleComponent.HOLD_MS, ownUser = true)
|
||||
assertTrue(pad.vibrating)
|
||||
idle(RumbleComponent.KEEPALIVE_MS + 100)
|
||||
assertFalse("rumble outlived a sender that was already gone", pad.vibrating)
|
||||
}
|
||||
|
||||
@Test fun anUnseenSenderOfAnotherUserKeepsItsRumble() {
|
||||
val pad = FakeMotors()
|
||||
rumble.controllerMotors = { pad }
|
||||
rumble.processStart = { null }
|
||||
activeController(PAD_ID)
|
||||
effectFrom(SENDER, ms = RumbleComponent.HOLD_MS, ownUser = false)
|
||||
idle(RumbleComponent.KEEPALIVE_MS * 3 + 100)
|
||||
assertTrue("a sender the app can't see lost its rumble", pad.vibrating)
|
||||
}
|
||||
|
||||
@Test fun aSenderFirstReadAsUnknownStillStopsItsRumbleWhenItExits() {
|
||||
val pad = FakeMotors()
|
||||
var start: String? = RumbleComponent.UNKNOWN_START
|
||||
rumble.controllerMotors = { pad }
|
||||
rumble.processStart = { start }
|
||||
activeController(PAD_ID)
|
||||
effectFrom(SENDER, ms = RumbleComponent.HOLD_MS)
|
||||
idle(RumbleComponent.KEEPALIVE_MS * 2 + 100)
|
||||
assertTrue(pad.vibrating)
|
||||
start = null
|
||||
idle(RumbleComponent.KEEPALIVE_MS + 100)
|
||||
assertFalse("rumble outlived its sender", pad.vibrating)
|
||||
}
|
||||
|
||||
@Test fun anUnreadableButLiveProcessKeepsItsRumble() {
|
||||
val pad = FakeMotors()
|
||||
var start: String? = "1234"
|
||||
rumble.controllerMotors = { pad }
|
||||
rumble.processStart = { start }
|
||||
activeController(PAD_ID)
|
||||
effectFrom(SENDER, ms = RumbleComponent.HOLD_MS)
|
||||
start = RumbleComponent.UNKNOWN_START
|
||||
idle(RumbleComponent.KEEPALIVE_MS * 3 + 100)
|
||||
assertTrue("a passing /proc read error cut the rumble", pad.vibrating)
|
||||
}
|
||||
|
||||
@Test fun quietingStopsAHeldEffectAndDropsEffectsUntilLifted() {
|
||||
val pad = FakeMotors()
|
||||
rumble.controllerMotors = { pad }
|
||||
@@ -280,6 +422,12 @@ class RumbleComponentTest {
|
||||
.apply { isAccessible = true }.invoke(rumble, strong, weak, ms, slot)
|
||||
}
|
||||
|
||||
private fun effectFrom(sender: Int, strong: Int = 40000, weak: Int = 20000, slot: Int = 0, ms: Int = 5000, ownUser: Boolean = false) {
|
||||
val int = Int::class.javaPrimitiveType
|
||||
RumbleComponent::class.java.getDeclaredMethod("buzz", int, int, int, int, int, Boolean::class.javaPrimitiveType)
|
||||
.apply { isAccessible = true }.invoke(rumble, strong, weak, ms, slot, sender, ownUser)
|
||||
}
|
||||
|
||||
private fun idle(ms: Long) = shadowOf(Looper.getMainLooper()).idleFor(java.time.Duration.ofMillis(ms))
|
||||
|
||||
private class FakeMotors : RumbleComponent.Motors {
|
||||
@@ -293,6 +441,8 @@ class RumbleComponentTest {
|
||||
private companion object {
|
||||
const val PAD_ID = 7
|
||||
const val SECOND_PAD_ID = 8
|
||||
const val SENDER = 4242
|
||||
const val OTHER_SENDER = 4343
|
||||
}
|
||||
|
||||
@Implements(LocalServerSocket::class)
|
||||
|
||||
Reference in new issue
Block a user