From a785ab9e659db112807ced91662fa4c8bd48ee7b Mon Sep 17 00:00:00 2001 From: Volodymyr Zolotopupov Date: Wed, 10 Jul 2024 03:00:08 +0300 Subject: [PATCH] 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..ad51726f6 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[128]; _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; }