Skip to content

Commit

Permalink
Use the udev "uniq" attribute when generating the device GUID
Browse files Browse the repository at this point in the history
It helps to avoid duplicate GUIDs when connecting two similar joysticks.
  • Loading branch information
zvova7890 committed Jul 14, 2024
1 parent 6e51b1a commit 9106d15
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/core/jstick.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/core/jstick.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
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 9106d15

Please sign in to comment.