Skip to content

Instantly share code, notes, and snippets.

@tdaniel22
Created June 13, 2023 06:50
Show Gist options
  • Save tdaniel22/166301c48a2e50502ba34877f509dcfb to your computer and use it in GitHub Desktop.
Save tdaniel22/166301c48a2e50502ba34877f509dcfb to your computer and use it in GitHub Desktop.
diff --git a/sway/input/input-manager.c b/sway/input/input-manager.c
index 39f4b795..3c114e46 100644
--- a/sway/input/input-manager.c
+++ b/sway/input/input-manager.c
@@ -1,5 +1,7 @@
#define _POSIX_C_SOURCE 200809L
#include <ctype.h>
+#include <libinput.h>
+#include <libudev.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
@@ -63,6 +65,28 @@ struct sway_seat *input_manager_sway_seat_from_wlr_seat(struct wlr_seat *wlr_sea
}
char *input_device_get_identifier(struct wlr_input_device *device) {
+ const char *usb_name = NULL;
+
+ if (wlr_input_device_is_libinput(device)) {
+ struct libinput_device *libinput_device = wlr_libinput_get_device_handle(device);
+ struct udev_device *udev_device = libinput_device_get_udev_device(libinput_device);
+
+ if (udev_device != NULL) {
+ struct udev_device *parent = udev_device;
+
+ do {
+ const char *subsystem = udev_device_get_subsystem(parent);
+ if (subsystem != NULL && strncmp(subsystem, "usb", 3) == 0) {
+ usb_name = udev_device_get_sysname(parent);
+ }
+
+ parent = udev_device_get_parent(parent);
+ } while (usb_name == NULL && parent != NULL);
+
+ udev_device_unref(udev_device);
+ }
+ }
+
int vendor = device->vendor;
int product = device->product;
char *name = strdup(device->name ? device->name : "");
@@ -76,15 +100,21 @@ char *input_device_get_identifier(struct wlr_input_device *device) {
}
}
- const char *fmt = "%d:%d:%s";
- int len = snprintf(NULL, 0, fmt, vendor, product, name) + 1;
+ const char *fmt = usb_name ? "usb:%s" : "%d:%d:%s";
+ int len = usb_name
+ ? snprintf(NULL, 0, fmt, usb_name) + 1
+ : snprintf(NULL, 0, fmt, vendor, product, name) + 1;
char *identifier = malloc(len);
if (!identifier) {
sway_log(SWAY_ERROR, "Unable to allocate unique input device name");
return NULL;
}
- snprintf(identifier, len, fmt, vendor, product, name);
+ if (usb_name) {
+ snprintf(identifier, len, fmt, usb_name);
+ } else {
+ snprintf(identifier, len, fmt, vendor, product, name);
+ }
free(name);
return identifier;
}
@Liriel
Copy link

Liriel commented Jun 15, 2023

@tdaniel22 works for me, thank you so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment