Skip to content

Commit

Permalink
[Linux] Use the udev "uniq" attribute when generating the device GUID (
Browse files Browse the repository at this point in the history
…#407) (thanks zvova7890).

It helps to avoid duplicate GUIDs when connecting two similar joysticks.
  • Loading branch information
zvova7890 authored Aug 2, 2024
1 parent c6b0dce commit 7ee4b5a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/core/jstick.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,17 @@ 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__)
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);
Expand Down
3 changes: 3 additions & 0 deletions src/core/jstick.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ typedef struct _js_device {
int size;
} report;
#endif
#endif
#if defined (__linux__)
uTCHAR uniq[64];
#endif
// comuni
enum _js_gamepad_type type;
Expand Down
3 changes: 3 additions & 0 deletions src/gui/linux/os_jstick.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 7ee4b5a

Please sign in to comment.