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 #include #include int ioctl(int fd, unsigned long request, ...) { va_list ap; va_start(ap, request); struct ff_effect *effect = va_arg(ap, struct ff_effect *); va_end(ap); if (request == EVIOCSFF) printf("shim type %u length %u\n", effect->type, effect->replay.length); return 0; } """ UPLOAD = r""" #include #include #include #include void upload(unsigned short type, unsigned short length) { struct ff_effect effect; memset(&effect, 0, sizeof(effect)); effect.id = -1; effect.type = type; effect.replay.length = length; ioctl(3, EVIOCSFF, &effect); printf("caller length %u\n", effect.replay.length); } """ SDL = r""" #include #include #include #include #include int SDL_JoystickRumble(void *joystick, uint16_t low, uint16_t high, uint32_t duration) { struct ff_effect effect; (void)joystick; printf("sdl %u %u duration %u\n", low, high, duration); memset(&effect, 0, sizeof(effect)); effect.id = -1; effect.type = FF_RUMBLE; effect.replay.length = 0xffff; effect.u.rumble.strong_magnitude = low; effect.u.rumble.weak_magnitude = high; return ioctl(3, EVIOCSFF, &effect); } """ SDL_CALLER = r""" #include #include int rumble(const char *library, uint16_t low, uint16_t high, uint32_t duration) { void *sdl = dlopen(library, RTLD_NOW | RTLD_LOCAL); int (*call)(void *, uint16_t, uint16_t, uint32_t) = (int (*)(void *, uint16_t, uint16_t, uint32_t))dlsym(sdl, "SDL_JoystickRumble"); call(0, low, high, duration); return 0; } """ PROGRAM = r""" #include #include #include void upload(unsigned short type, unsigned short length); int rumble(const char *library, unsigned short low, unsigned short high, unsigned int duration); int main(int argc, char **argv) { void *winebus = dlopen(argv[1], RTLD_NOW); if (argv[2][0] == 's') { int (*from_winebus)(const char *, unsigned short, unsigned short, unsigned int) = (int (*)(const char *, unsigned short, unsigned short, unsigned int))dlsym(winebus, "rumble"); for (const char *step = argv[3]; *step; step++) { if (*step == 'w') from_winebus(argv[4], 65535, 32768, 1000); if (*step == 'z') from_winebus(argv[4], 0, 0, 0); if (*step == 'p') rumble(argv[4], 65535, 32768, 1000); } fflush(stdout); return 0; } void (*from_winebus)(unsigned short, unsigned short) = (void (*)(unsigned short, unsigned short))dlsym(winebus, "upload"); if (argv[2][0] == 'w') from_winebus(argv[3][0] == 'r' ? FF_RUMBLE : FF_PERIODIC, 1000); else upload(FF_RUMBLE, 1000); fflush(stdout); return 0; } """ @unittest.skipUnless(sys.platform == "linux" and shutil.which("cc"), "needs Linux and cc") class WinebusRumblePreloadTest(unittest.TestCase): @classmethod def setUpClass(cls): cls.dir = Path(tempfile.mkdtemp()) build = cls.dir / "build" build.mkdir() for name, text in (("shim.c", SHIM), ("upload.c", UPLOAD), ("program.c", PROGRAM), ("sdl.c", SDL), ("sdl_caller.c", SDL_CALLER)): (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 / "upload.c"), str(build / "sdl_caller.c"), "-ldl"], 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 / "sdl.c")], check=True) cls.program = cls.dir / "program" subprocess.run(["cc", "-O2", "-o", str(cls.program), str(build / "program.c"), str(build / "upload.c"), str(build / "sdl_caller.c"), "-ldl"], check=True) @classmethod def tearDownClass(cls): shutil.rmtree(cls.dir, True) def upload(self, caller, kind="rumble"): env = dict(os.environ, LD_PRELOAD="%s:%s" % (self.lib, self.shim)) result = subprocess.run([str(self.program), str(self.winebus), caller, kind], env=env, capture_output=True, text=True, timeout=10, check=True) return result.stdout.splitlines() def sdl(self, steps): env = dict(os.environ, LD_PRELOAD="%s:%s" % (self.lib, self.shim)) result = subprocess.run([str(self.program), str(self.winebus), "sdl", steps, str(self.sdl_library)], env=env, capture_output=True, text=True, timeout=10, check=True) return result.stdout.splitlines() def test_rumble_winebus_starts_through_sdl_is_held_until_the_next_update(self): self.assertEqual(self.sdl("wz"), ["sdl 65535 32768 duration 0", "shim type %d length 0" % 0x50, "sdl 0 0 duration 0", "shim type %d length 0" % 0x50]) def test_sdl_rumble_from_other_code_keeps_its_duration_and_length(self): self.assertEqual(self.sdl("p"), ["sdl 65535 32768 duration 1000", "shim type %d length 65535" % 0x50]) self.assertEqual(self.sdl("wp"), ["sdl 65535 32768 duration 0", "shim type %d length 0" % 0x50, "sdl 65535 32768 duration 1000", "shim type %d length 65535" % 0x50]) def test_winebus_rumble_reaches_the_shim_held_and_comes_back_unchanged(self): self.assertEqual(self.upload("winebus"), ["shim type %d length 0" % 0x50, "caller length 1000"]) def test_other_callers_and_effects_keep_their_length(self): self.assertEqual(self.upload("program"), ["shim type %d length 1000" % 0x50, "caller length 1000"]) self.assertEqual(self.upload("winebus", "periodic"), ["shim type %d length 1000" % 0x51, "caller length 1000"]) if __name__ == "__main__": unittest.main()