Compare commits

...
Author SHA1 Message Date
maxjivi05 cd2b93782f fix(input): each pad is listed once in Windows games so both triggers read together
A pad that Wine's SDL backend also reads was described to Wine's udev
backend as a Steam Input virtual pad, so Wine built two devices for one
pad. SDL RawInput games could not match the two and read held triggers as
released. The pad now keeps the identity the SDL backend sees unless that
backend is told to skip it.

Rumble that Wine starts through SDL is now held until the next update,
matching the udev path, instead of stopping after one second.
2026-11-29 15:18:52 -05:00
3 changed files with 179 additions and 14 deletions

No files matched your search

+66 -8
View File
@@ -34,6 +34,8 @@
"NAME=\"Xbox 360 Controller (%d)\"\n" \
"PHYS=\"usb-fakeinput/input%d\"\n"
#define RING_SDL_ID "0x045e/0x028e"
#define PARENT_NAME "droiddeck-input"
#define PARENT_SYSPATH_FORMAT "/sys/devices/droiddeck%d/" PARENT_NAME "%d"
#define EVENT_SYSPATH_FORMAT PARENT_SYSPATH_FORMAT "/event%d"
@@ -49,6 +51,8 @@ struct pad {
int live;
};
static const char *ring_uevent_format(void);
static struct pad pads[PAD_MAX];
static struct pad parents[PAD_MAX];
static int pad_count;
@@ -89,7 +93,7 @@ static void discover_pads(void) {
snprintf(pad->syspath, sizeof(pad->syspath), EVENT_SYSPATH_FORMAT, (int)slot, (int)slot,
(int)slot);
snprintf(pad->devnode, sizeof(pad->devnode), "/dev/input/event%ld", slot);
snprintf(pad->uevent, sizeof(pad->uevent), PAD_UEVENT_FORMAT, (int)slot, (int)slot);
snprintf(pad->uevent, sizeof(pad->uevent), ring_uevent_format(), (int)slot, (int)slot);
pad->devnum = makedev(13, EVENT_MINOR_BASE + (unsigned int)slot);
pad->node = (int)slot;
pad->live = 1;
@@ -137,6 +141,21 @@ static int any_virtual_pad(void) {
return 0;
}
static int sdl_bus_skips_ring_pad(void) {
const char *steam_virtual = getenv("FAKE_EVDEV_STEAM_VIRTUAL");
const char *except = getenv("SDL_GAMECONTROLLER_IGNORE_DEVICES_EXCEPT");
const char *ignore = getenv("SDL_GAMECONTROLLER_IGNORE_DEVICES");
if (steam_virtual != NULL && atoi(steam_virtual) != 0) return 1;
if (except != NULL) return strcasestr(except, RING_SDL_ID) == NULL;
return ignore != NULL && strcasestr(ignore, RING_SDL_ID) != NULL;
}
static const char *ring_uevent_format(void) {
if (uinput_enabled() && any_virtual_pad()) return PAD_PHYSICAL_UEVENT_FORMAT;
return sdl_bus_skips_ring_pad() ? PAD_UEVENT_FORMAT : PAD_PHYSICAL_UEVENT_FORMAT;
}
static void refresh_locked(int node) {
struct pad *pad = &pads[node];
@@ -167,9 +186,7 @@ static void refresh_locked(int node) {
} else {
snprintf(pad->devnode, sizeof(pad->devnode), "/dev/input/event%d", node);
pad->live = 1;
if (uinput_enabled())
snprintf(pad->uevent, sizeof(pad->uevent),
any_virtual_pad() ? PAD_PHYSICAL_UEVENT_FORMAT : PAD_UEVENT_FORMAT, node, node);
snprintf(pad->uevent, sizeof(pad->uevent), ring_uevent_format(), node, node);
}
}
sync_parent(node);
@@ -441,11 +458,46 @@ static void *next_symbol(const char *name) {
char *dd_cached_realpath(const char *path, char *resolved, char *(*real)(const char *, char *))
__attribute__((visibility("hidden")));
static int caller_is_winebus(void *caller) {
static int caller_in_library(void *caller, const char *library) {
Dl_info info;
if (dladdr(caller, &info) == 0 || info.dli_fname == NULL) return 0;
return strstr(info.dli_fname, "winebus.so") != NULL;
return strstr(info.dli_fname, library) != NULL;
}
static int caller_is_winebus(void *caller) { return caller_in_library(caller, "winebus.so"); }
typedef int (*sdl_rumble_fn)(void *, uint16_t, uint16_t, uint32_t);
static _Atomic(sdl_rumble_fn) real_sdl_rumble;
static atomic_int winebus_drives_sdl;
static int winebus_sdl_rumble(void *joystick, uint16_t low, uint16_t high, uint32_t duration) {
sdl_rumble_fn real = atomic_load_explicit(&real_sdl_rumble, memory_order_acquire);
if (caller_is_winebus(__builtin_return_address(0))) {
atomic_store_explicit(&winebus_drives_sdl, 1, memory_order_release);
if (low != 0 || high != 0) duration = 0;
}
return real(joystick, low, high, duration);
}
static void *winebus_sdl_rumble_dlsym(void *handle, const char *name) {
(void)handle;
(void)name;
return (void *)winebus_sdl_rumble;
}
static void *sdl_rumble_target(void *handle, void *(*forward)(void *, const char *)) {
sdl_rumble_fn expected = NULL;
sdl_rumble_fn found;
if (handle == RTLD_NEXT || handle == RTLD_DEFAULT) return NULL;
found = (sdl_rumble_fn)forward(handle, "SDL_JoystickRumble");
if (found == NULL) return NULL;
atomic_compare_exchange_strong_explicit(&real_sdl_rumble, &expected, found, memory_order_acq_rel,
memory_order_acquire);
if (atomic_load_explicit(&real_sdl_rumble, memory_order_acquire) != found) return NULL;
return (void *)winebus_sdl_rumble_dlsym;
}
__attribute__((visibility("hidden"))) int dd_winebus_rumble_ioctl(int (*real)(int, unsigned long, void *), int fd,
@@ -454,7 +506,10 @@ __attribute__((visibility("hidden"))) int dd_winebus_rumble_ioctl(int (*real)(in
struct ff_effect *effect = arg;
__u16 length;
if (request != EVIOCSFF || effect == NULL || effect->type != FF_RUMBLE || !caller_is_winebus(caller)) return 0;
if (request != EVIOCSFF || effect == NULL || effect->type != FF_RUMBLE) return 0;
if (!caller_is_winebus(caller) && !(atomic_load_explicit(&winebus_drives_sdl, memory_order_acquire) &&
caller_in_library(caller, "libSDL")))
return 0;
length = effect->replay.length;
effect->replay.length = 0;
*rc = real(fd, request, arg);
@@ -1195,7 +1250,10 @@ static void *own_dlsym(void *handle, const char *name) {
__attribute__((visibility("hidden"), used)) void *inputudev_dlsym_target(void *handle, const char *name) {
void *forward = real_dlsym();
(void)handle;
if (forward != NULL && name != NULL && strcmp(name, "SDL_JoystickRumble") == 0) {
void *rumble = sdl_rumble_target(handle, forward);
if (rumble != NULL) return rumble;
}
if (name != NULL && strncmp(name, "udev_", 5) == 0 && own_udev_symbol(name) != NULL &&
!steam_client_process())
return (void *)own_dlsym;
+46 -2
View File
@@ -260,6 +260,7 @@ static void *(*next)(void *);
static void *(*by_name)(void *, const char *);
static const char *(*name)(void *);
static const char *(*value)(void *);
static const char *(*sysattr)(void *, const char *);
static void *sym(const char *symbol) { return dlsym(lib, symbol); }
@@ -463,6 +464,20 @@ static void run_every(int count, char **entries) {
printf("pad %d real calls %d\n", pad != NULL, real_calls != NULL ? *real_calls : -1);
}
static void print_product(const char *label, void *device) {
const char *uevent = device != NULL ? sysattr(device, "uevent") : NULL;
printf("%s %.*s\n", label, uevent != NULL ? (int)strcspn(uevent, "\n") : 4, uevent != NULL ? uevent : "none");
}
static void run_identity(void) {
int udev;
void *device = new_device(&udev, PAD);
print_product("pad", device);
print_product("parent", device != NULL ? get_parent(device) : NULL);
}
static void run_probe(void) {
int udev;
@@ -539,7 +554,9 @@ int main(int argc, char **argv) {
by_name = sym("udev_list_entry_get_by_name");
name = sym("udev_list_entry_get_name");
value = sym("udev_list_entry_get_value");
sysattr = sym("udev_device_get_sysattr_value");
if (strcmp(argv[1], "probe") == 0) run_probe();
else if (strcmp(argv[1], "identity") == 0) run_identity();
else if (strcmp(argv[1], "every") == 0) run_every(argc - 3, argv + 3);
else if (strcmp(argv[1], "stable") == 0) run_stable();
else if (strcmp(argv[1], "enumerate") == 0) run_enumerate();
@@ -597,10 +614,13 @@ class InputUdevPreloadTest(unittest.TestCase):
(pads / node).write_text("")
return pads
def run_program(self, mode, *preloads, pads=None, slots="0=/pad", program=None, args=()):
def run_program(self, mode, *preloads, pads=None, slots="0=/pad", program=None, args=(), extra=None):
env = dict(os.environ, LD_PRELOAD=":".join(str(self.dir / name) for name in ("libddsession.so", *preloads)),
FAKE_EVDEV_MEMFD_PATHS=slots, FAKE_EVDEV_DIR=str(pads or self.pads))
env.pop("FAKE_EVDEV_UINPUT", None)
for key in ("FAKE_EVDEV_UINPUT", "FAKE_EVDEV_STEAM_VIRTUAL", "SDL_GAMECONTROLLER_IGNORE_DEVICES",
"SDL_GAMECONTROLLER_IGNORE_DEVICES_EXCEPT"):
env.pop(key, None)
env.update(extra or {})
result = subprocess.run([str(program or self.program), mode, str(self.udev), *args], env=env, capture_output=True, text=True,
timeout=20)
self.assertEqual(result.returncode, 0, result.stderr)
@@ -684,6 +704,30 @@ class InputUdevPreloadTest(unittest.TestCase):
"nothing added scan -1",
])
def identity(self, pads=None, **extra):
return self.run_program("identity", pads=pads, extra=extra)
def test_a_pad_wine_also_reads_through_sdl_is_described_as_the_pad_sdl_sees(self):
xbox = ["pad PRODUCT=3/45e/28e/110", "parent PRODUCT=3/45e/28e/110"]
for extra in ({}, {"FAKE_EVDEV_UINPUT": "1"}, {"SDL_GAMECONTROLLER_IGNORE_DEVICES": "0x28de/0x1142"},
{"SDL_GAMECONTROLLER_IGNORE_DEVICES_EXCEPT": "0x045e/0x028e"}, {"FAKE_EVDEV_STEAM_VIRTUAL": "0"}):
with self.subTest(**extra):
self.assertEqual(self.identity(**extra), xbox)
def test_a_pad_sdl_skips_keeps_the_steam_input_identity(self):
steam = ["pad PRODUCT=6/28de/11ff/110", "parent PRODUCT=6/28de/11ff/110"]
for extra in ({"FAKE_EVDEV_STEAM_VIRTUAL": "1"}, {"SDL_GAMECONTROLLER_IGNORE_DEVICES": "0x28de/0x1142,0x045E/0x028E"},
{"SDL_GAMECONTROLLER_IGNORE_DEVICES_EXCEPT": "0x28de/0x11ff"},
{"FAKE_EVDEV_UINPUT": "1", "SDL_GAMECONTROLLER_IGNORE_DEVICES": "0x045e/0x028e"}):
with self.subTest(**extra):
self.assertEqual(self.identity(**extra), steam)
def test_a_pad_under_steam_inputs_own_virtual_pad_stays_an_xbox_pad(self):
pads = self.pad_dir("under-virtual", "event0", "event16")
(pads / ".uinput-event16.uevent").write_text('PRODUCT=6/28de/11ff/1\nNAME="Microsoft X-Box 360 pad 0"\n')
self.assertEqual(self.identity(pads, FAKE_EVDEV_UINPUT="1", SDL_GAMECONTROLLER_IGNORE_DEVICES="0x045e/0x028e"),
["pad PRODUCT=3/45e/28e/110", "parent PRODUCT=3/45e/28e/110"])
def test_a_program_that_closes_and_reopens_libudev_keeps_working(self):
self.assertEqual(self.run_program("reload"), ["reload real0 real1"])
+67 -4
View File
@@ -40,6 +40,39 @@ void upload(unsigned short type, unsigned short length) {
}
"""
SDL = r"""
#include <linux/input.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
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 <dlfcn.h>
#include <stdint.h>
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 <dlfcn.h>
#include <linux/input.h>
@@ -47,8 +80,21 @@ PROGRAM = r"""
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);
@@ -65,7 +111,8 @@ class WinebusRumblePreloadTest(unittest.TestCase):
cls.dir = Path(tempfile.mkdtemp())
build = cls.dir / "build"
build.mkdir()
for name, text in (("shim.c", SHIM), ("upload.c", UPLOAD), ("program.c", PROGRAM)):
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),
@@ -73,10 +120,13 @@ class WinebusRumblePreloadTest(unittest.TestCase):
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")], check=True)
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"), "-ldl"],
check=True)
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):
@@ -88,6 +138,19 @@ class WinebusRumblePreloadTest(unittest.TestCase):
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])
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"])