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
@@ -0,0 +1,145 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import socket
|
||||
import struct
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
SOURCE = Path(__file__).resolve().parents[2] / "app/src/main/cpp/fakeinput_steam.cpp"
|
||||
RING_MAGIC = 0x46494252
|
||||
DESCRIPTION_MAGIC = 0x46495544
|
||||
HEADER = 64
|
||||
EVENT = 24
|
||||
CAPACITY = 4096
|
||||
DESCRIPTION = 1824
|
||||
|
||||
GAME = r"""
|
||||
#include <fcntl.h>
|
||||
#include <linux/input.h>
|
||||
#include <string.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static int rumble(int fd, unsigned short strong) {
|
||||
struct ff_effect effect;
|
||||
memset(&effect, 0, sizeof(effect));
|
||||
effect.id = -1;
|
||||
effect.type = FF_RUMBLE;
|
||||
effect.u.rumble.strong_magnitude = strong;
|
||||
effect.u.rumble.weak_magnitude = 20000;
|
||||
if (ioctl(fd, EVIOCSFF, &effect) < 0) return -1;
|
||||
struct input_event play;
|
||||
memset(&play, 0, sizeof(play));
|
||||
play.type = EV_FF;
|
||||
play.code = effect.id;
|
||||
play.value = 1;
|
||||
if (write(fd, &play, sizeof(play)) != sizeof(play)) return -1;
|
||||
return effect.id;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
int fd = open("/dev/input/event16", O_RDWR | O_NONBLOCK);
|
||||
if (fd < 0) return 1;
|
||||
if (argv[1][0] == 'p') {
|
||||
int other = open("/dev/input/event16", O_RDWR | O_NONBLOCK);
|
||||
if (other < 0 || rumble(fd, 40000) < 0 || rumble(other, 30000) < 0) return 2;
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
if (argv[1][0] == 'k') {
|
||||
pid_t child = fork();
|
||||
if (child == 0) {
|
||||
if (rumble(fd, 40000) < 0) _exit(2);
|
||||
close(fd);
|
||||
_exit(0);
|
||||
}
|
||||
int status = 0;
|
||||
waitpid(child, &status, 0);
|
||||
return WEXITSTATUS(status);
|
||||
}
|
||||
struct ff_effect effect;
|
||||
effect.id = rumble(fd, 40000);
|
||||
if (effect.id < 0) return 2;
|
||||
if (argv[1][0] == 'e') ioctl(fd, EVIOCRMFF, effect.id);
|
||||
else if (argv[1][0] == 'c') close(fd);
|
||||
else if (argv[1][0] == 'f') {
|
||||
pid_t child = fork();
|
||||
if (child == 0) {
|
||||
close(fd);
|
||||
_exit(0);
|
||||
}
|
||||
waitpid(child, NULL, 0);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
"""
|
||||
|
||||
|
||||
@unittest.skipUnless(sys.platform == "linux" and shutil.which("g++") and shutil.which("gcc"), "needs Linux, gcc and g++")
|
||||
class FakeinputRumbleStopTest(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.tmp = Path(tempfile.mkdtemp())
|
||||
cls.addClassCleanup(shutil.rmtree, cls.tmp, True)
|
||||
cls.library = cls.tmp / "libfakeinput.so"
|
||||
subprocess.run(["g++", "-shared", "-fPIC", "-O2", "-Wno-attributes", "-pthread", "-std=c++17",
|
||||
"-static-libstdc++", "-static-libgcc", "-Wl,--exclude-libs,ALL", "-o", str(cls.library),
|
||||
str(SOURCE), "-ldl"], check=True)
|
||||
cls.game = cls.tmp / "game"
|
||||
(cls.tmp / "game.c").write_text(GAME)
|
||||
subprocess.run(["gcc", "-o", str(cls.game), str(cls.tmp / "game.c")], check=True)
|
||||
cls.hooks = cls.tmp / "hooks"
|
||||
cls.hooks.mkdir()
|
||||
(cls.hooks / "event16").touch()
|
||||
ring = bytearray(HEADER + CAPACITY * EVENT + DESCRIPTION)
|
||||
struct.pack_into("<IIIIQQQ", ring, 0, RING_MAGIC, 2, EVENT, CAPACITY, 0, 0, 0)
|
||||
struct.pack_into("<IiHHHHI80s", ring, HEADER + CAPACITY * EVENT, DESCRIPTION_MAGIC, os.getpid(),
|
||||
3, 0x28de, 0x11ff, 1, 16, b"Microsoft X-Box 360 pad 0")
|
||||
(cls.hooks / ".uinput-event16").write_bytes(bytes(ring))
|
||||
|
||||
def packets(self, ending):
|
||||
server = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
||||
self.addCleanup(server.close)
|
||||
try:
|
||||
server.bind("\0droiddeck-rumble")
|
||||
except OSError:
|
||||
self.skipTest("the rumble socket name is taken on this machine")
|
||||
server.listen(16)
|
||||
server.settimeout(0.5)
|
||||
env = dict(os.environ, LD_PRELOAD=str(self.library), FAKE_EVDEV_DIR=str(self.hooks), FAKE_EVDEV_VIBRATION="1")
|
||||
run = subprocess.run([str(self.game), ending], env=env, capture_output=True, timeout=10)
|
||||
self.assertEqual(run.returncode, 0, run.stderr)
|
||||
got = []
|
||||
while True:
|
||||
try:
|
||||
client, _ = server.accept()
|
||||
except socket.timeout:
|
||||
return got
|
||||
with client:
|
||||
got.append(struct.unpack("<4H", client.recv(8))[:3])
|
||||
|
||||
def test_closing_the_controller_stops_its_held_rumble(self):
|
||||
self.assertEqual(self.packets("close")[-1], (0, 0, 0))
|
||||
|
||||
def test_erasing_the_playing_effect_stops_it(self):
|
||||
self.assertEqual(self.packets("erase")[-1], (0, 0, 0))
|
||||
|
||||
def test_a_forked_child_closing_its_copy_leaves_the_parents_rumble(self):
|
||||
self.assertEqual(self.packets("fork")[-1], (40000, 20000, 0xffff))
|
||||
|
||||
def test_a_child_that_plays_on_an_inherited_controller_stops_it_on_close(self):
|
||||
self.assertEqual(self.packets("kid")[-1], (0, 0, 0))
|
||||
|
||||
def test_closing_an_older_handle_leaves_the_newer_rumble_on_the_slot(self):
|
||||
self.assertEqual(self.packets("pair")[-1], (30000, 20000, 0xffff))
|
||||
|
||||
def test_a_held_effect_still_plays_until_then(self):
|
||||
self.assertIn((40000, 20000, 0xffff), self.packets("keep"))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user