From 9106d15fdf3e7dc71e8758b57a56b27f36c569a4 Mon Sep 17 00:00:00 2001 From: Volodymyr Zolotopupov Date: Wed, 10 Jul 2024 03:00:08 +0300 Subject: [PATCH 1/4] Use the udev "uniq" attribute when generating the device GUID It helps to avoid duplicate GUIDs when connecting two similar joysticks. --- src/core/jstick.c | 9 +++++++++ src/core/jstick.h | 1 + src/gui/linux/os_jstick.c | 3 +++ 3 files changed, 13 insertions(+) diff --git a/src/core/jstick.c b/src/core/jstick.c index 9457c778a..587d56a86 100644 --- a/src/core/jstick.c +++ b/src/core/jstick.c @@ -154,6 +154,15 @@ void js_guid_create(_js_device *jdev) { (*(word + 5)) = jdev->usb.product_id - 300; (*(word + 6)) = jdev->usb.version; (*(word + 7)) = jdev->usb.version - 400; + + BYTE *byte = (BYTE *)&word[2]; + int idx = 0; + for (const char *s = jdev->uniq; *s; ++s) { + byte[idx++] ^= *s; + if (idx > 11) { + idx = 0; + } + } } else { word += 2; memcpy((char *)word, (char *)jdev->desc, sizeof(jdev->guid.data) - 4); diff --git a/src/core/jstick.h b/src/core/jstick.h index 5e070ea25..61f2b4b0f 100644 --- a/src/core/jstick.h +++ b/src/core/jstick.h @@ -165,6 +165,7 @@ typedef struct _js_device { // comuni enum _js_gamepad_type type; uTCHAR desc[128]; + uTCHAR uniq[64]; _js_info info; _js_data data; _input_guid guid; diff --git a/src/gui/linux/os_jstick.c b/src/gui/linux/os_jstick.c index d3d834e4a..4b140c995 100644 --- a/src/gui/linux/os_jstick.c +++ b/src/gui/linux/os_jstick.c @@ -73,12 +73,15 @@ void js_os_jdev_open(_js_device *jdev, void *arg) { const char *vid = udev_device_get_sysattr_value(udevd, "id/vendor"); const char *ver = udev_device_get_sysattr_value(udevd, "id/version"); const char *name = udev_device_get_sysattr_value(udevd, "name"); + const char *uniq = udev_device_get_sysattr_value(udevd, "uniq"); jdev->usb.bustype = strtoul(btype, NULL, 16); jdev->usb.vendor_id = strtoul(vid, NULL, 16); jdev->usb.product_id = strtoul(pid, NULL, 16); jdev->usb.version = strtoul(ver, NULL, 16); + ustrncpy(jdev->desc, name, usizeof(jdev->desc) - 1); + ustrncpy(jdev->uniq, uniq, usizeof(jdev->uniq) - 1); } else { return; } From 6b66a879c41e5a0f24e17d1b9889e01b441d1186 Mon Sep 17 00:00:00 2001 From: Fabio Cavallo Date: Thu, 1 Aug 2024 12:03:24 +0200 Subject: [PATCH 2/4] Check that the environment is Linux. --- src/core/jstick.c | 18 +++++++++++------- src/core/jstick.h | 4 +++- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/core/jstick.c b/src/core/jstick.c index 587d56a86..f0b20a126 100644 --- a/src/core/jstick.c +++ b/src/core/jstick.c @@ -154,15 +154,19 @@ void js_guid_create(_js_device *jdev) { (*(word + 5)) = jdev->usb.product_id - 300; (*(word + 6)) = jdev->usb.version; (*(word + 7)) = jdev->usb.version - 400; - - BYTE *byte = (BYTE *)&word[2]; - int idx = 0; - for (const char *s = jdev->uniq; *s; ++s) { - byte[idx++] ^= *s; - if (idx > 11) { - idx = 0; +#if defined (_linux__) + if (ustrlen(jdev->uniq)) { + BYTE *byte = (BYTE *)&word[2]; + int idx = 0; + + for (const char *s = jdev->uniq; *s; ++s) { + byte[idx++] ^= *s; + if (idx > 11) { + idx = 0; + } } } +#endif } else { word += 2; memcpy((char *)word, (char *)jdev->desc, sizeof(jdev->guid.data) - 4); diff --git a/src/core/jstick.h b/src/core/jstick.h index 61f2b4b0f..6c4eb06ff 100644 --- a/src/core/jstick.h +++ b/src/core/jstick.h @@ -161,11 +161,13 @@ typedef struct _js_device { int size; } report; #endif +#endif +#if defined (__linux__) + uTCHAR uniq[64]; #endif // comuni enum _js_gamepad_type type; uTCHAR desc[128]; - uTCHAR uniq[64]; _js_info info; _js_data data; _input_guid guid; From e8ab38fd7927b05cb74225045f9a97b1f3456b04 Mon Sep 17 00:00:00 2001 From: "Fabio Cavallo (FHorse)" Date: Thu, 1 Aug 2024 19:33:50 +0200 Subject: [PATCH 3/4] Update src/core/jstick.c Co-authored-by: zvova7890 --- src/core/jstick.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/jstick.c b/src/core/jstick.c index f0b20a126..358c84a5b 100644 --- a/src/core/jstick.c +++ b/src/core/jstick.c @@ -154,7 +154,7 @@ void js_guid_create(_js_device *jdev) { (*(word + 5)) = jdev->usb.product_id - 300; (*(word + 6)) = jdev->usb.version; (*(word + 7)) = jdev->usb.version - 400; -#if defined (_linux__) +#if defined (__linux__) if (ustrlen(jdev->uniq)) { BYTE *byte = (BYTE *)&word[2]; int idx = 0; From cd5df745453bd8794b33a0d249577b56eedb1b91 Mon Sep 17 00:00:00 2001 From: Fabio Cavallo Date: Fri, 2 Aug 2024 08:54:38 +0200 Subject: [PATCH 4/4] Deleted a redundant check added in a prior commit. --- src/core/jstick.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/core/jstick.c b/src/core/jstick.c index 358c84a5b..8d2bf2487 100644 --- a/src/core/jstick.c +++ b/src/core/jstick.c @@ -155,15 +155,13 @@ void js_guid_create(_js_device *jdev) { (*(word + 6)) = jdev->usb.version; (*(word + 7)) = jdev->usb.version - 400; #if defined (__linux__) - if (ustrlen(jdev->uniq)) { - BYTE *byte = (BYTE *)&word[2]; - int idx = 0; - - for (const char *s = jdev->uniq; *s; ++s) { - byte[idx++] ^= *s; - if (idx > 11) { - idx = 0; - } + BYTE *byte = (BYTE *)&word[2]; + int idx = 0; + + for (const char *s = jdev->uniq; *s; ++s) { + byte[idx++] ^= *s; + if (idx > 11) { + idx = 0; } } #endif