Files
DroidDeck/tools/tests/test_ring_pad_sdl_identity.py
MaxsTechReview 72c8b8426d fix(input): Windows games see the gamepad again when the pad is not read through SDL (#3469)
Since each pad was described to Wine's udev backend with the identity its
SDL backend sees, the udev backend left every pad to SDL. Where SDL does
not open the pad, such as a session without proot, Windows games lost both
the gamepad and the on-screen controls.

The udev backend owns the pads again with the Steam Input identity it had
before. SDL inside Wine's bus driver now reads the same identity for those
pads and skips them as it skips any Steam Input pad, so each pad is still
listed once and both triggers keep reading together.
2026-11-30 08:53:12 -05:00

126 lines
4.2 KiB
Python

from pathlib import Path
import os
import shutil
import subprocess
import sys
import tempfile
import unittest
PRELOAD = Path(__file__).resolve().parents[1] / "linuxfs/preload"
SHIM = r"""
#include <linux/input.h>
#include <stdarg.h>
#include <string.h>
int ioctl(int fd, unsigned long request, ...) {
va_list ap;
va_start(ap, request);
struct input_id *id = va_arg(ap, struct input_id *);
va_end(ap);
(void)fd;
if (request != EVIOCGID) return -1;
memset(id, 0, sizeof(*id));
id->bustype = 3;
id->vendor = 0x045e;
id->product = 0x028e;
return 0;
}
"""
QUERY = r"""
#include <fcntl.h>
#include <linux/input.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <unistd.h>
void query(const char *path) {
struct input_id id;
int fd = open(path, O_RDONLY | O_CLOEXEC);
int rc = ioctl(fd, EVIOCGID, &id);
printf("%d %04x:%04x\n", rc, id.vendor, id.product);
close(fd);
}
"""
PROGRAM = r"""
#include <dlfcn.h>
#include <stdio.h>
#include <string.h>
void query(const char *path);
int main(int argc, char **argv) {
(void)argc;
if (strcmp(argv[1], "-") != 0) dlopen(argv[1], RTLD_NOW);
if (strcmp(argv[2], "program") == 0) {
query(argv[3]);
} else {
void *sdl = dlopen(argv[2], RTLD_NOW | RTLD_LOCAL);
void (*sdl_query)(const char *) = (void (*)(const char *))dlsym(sdl, "query");
sdl_query(argv[3]);
}
fflush(stdout);
return 0;
}
"""
@unittest.skipUnless(sys.platform == "linux" and shutil.which("cc"), "needs Linux and cc")
class RingPadSdlIdentityTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.dir = Path(tempfile.mkdtemp())
build = cls.dir / "build"
build.mkdir()
for name, text in (("shim.c", SHIM), ("query.c", QUERY), ("program.c", PROGRAM), ("empty.c", "int bus;\n")):
(build / name).write_text(text)
cls.lib = cls.dir / "libddsession.so"
subprocess.run(["cc", "-shared", "-fPIC", "-O2", "-Wall", "-pthread", "-o", str(cls.lib),
*map(str, sorted(PRELOAD.glob("*.c"))), "-ldl"], check=True)
cls.shim = cls.dir / "libshim.so"
subprocess.run(["cc", "-shared", "-fPIC", "-O2", "-o", str(cls.shim), str(build / "shim.c")], check=True)
cls.winebus = cls.dir / "winebus.so"
subprocess.run(["cc", "-shared", "-fPIC", "-O2", "-o", str(cls.winebus), str(build / "empty.c")], check=True)
cls.sdl_library = cls.dir / "libSDL2-2.0.so.0"
subprocess.run(["cc", "-shared", "-fPIC", "-O2", "-o", str(cls.sdl_library), str(build / "query.c")], check=True)
cls.program = cls.dir / "program"
subprocess.run(["cc", "-O2", "-o", str(cls.program), str(build / "program.c"), str(build / "query.c"), "-ldl"],
check=True)
cls.ring = cls.dir / "ring0"
cls.ring.write_text("")
cls.virtual = cls.dir / "ring16"
cls.virtual.write_text("")
@classmethod
def tearDownClass(cls):
shutil.rmtree(cls.dir, True)
def query(self, winebus=True, caller="sdl", path=None, **extra):
env = dict(os.environ, LD_PRELOAD="%s:%s" % (self.lib, self.shim),
FAKE_EVDEV_MEMFD_PATHS="0=%s;16=%s" % (self.ring, self.virtual))
env.pop("FAKE_EVDEV_STEAM_PAD_AS_X360", None)
env.update(extra)
result = subprocess.run([str(self.program), str(self.winebus) if winebus else "-",
str(self.sdl_library) if caller == "sdl" else "program", str(path or self.ring)],
env=env, capture_output=True, text=True, timeout=10, check=True)
return result.stdout.strip()
def test_sdl_in_a_winebus_process_sees_a_ring_pad_as_the_steam_input_pad(self):
self.assertEqual(self.query(), "0 28de:11ff")
def test_sdl_outside_winebus_keeps_the_pad_identity(self):
self.assertEqual(self.query(winebus=False), "0 045e:028e")
def test_other_callers_and_devices_keep_their_identity(self):
self.assertEqual(self.query(caller="program"), "0 045e:028e")
self.assertEqual(self.query(path=self.virtual), "0 045e:028e")
def test_a_winebus_without_a_udev_bus_keeps_the_pad_for_sdl(self):
self.assertEqual(self.query(FAKE_EVDEV_STEAM_PAD_AS_X360="1"), "0 045e:028e")
if __name__ == "__main__":
unittest.main()