winebus.sys: Simplify parsing udev device info from uevent.

Signed-off-by: Rémi Bernon <rbernon@codeweavers.com>
Signed-off-by: Alexandre Julliard <julliard@winehq.org>
This commit is contained in:
Rémi Bernon 2021-08-16 08:56:23 +02:00 committed by Alexandre Julliard
parent aa40700f9f
commit c2fc919e04
1 changed files with 25 additions and 55 deletions

View File

@ -995,66 +995,38 @@ static int check_device_syspath(DEVICE_OBJECT *device, void* context)
return strcmp(get_device_syspath(private->udev_device), context); return strcmp(get_device_syspath(private->udev_device), context);
} }
static int parse_uevent_info(const char *uevent, DWORD *vendor_id, static void parse_uevent_info(struct udev_device *dev, DWORD *vendor_id, DWORD *product_id,
DWORD *product_id, WORD *input, WCHAR **serial_number) DWORD *input, WCHAR **serial_number)
{ {
DWORD bus_type; const char *ptr, *next, *tmp;
char *tmp; char buffer[256];
char *saveptr = NULL; DWORD bus = 0;
char *line;
char *key;
char *value;
int found_id = 0; if ((next = udev_device_get_sysattr_value(dev, "uevent")))
int found_serial = 0;
tmp = heap_alloc(strlen(uevent) + 1);
strcpy(tmp, uevent);
line = strtok_r(tmp, "\n", &saveptr);
while (line != NULL)
{ {
/* line: "KEY=value" */ while ((ptr = next) && *ptr)
key = line;
value = strchr(line, '=');
if (!value)
{ {
goto next_line; if ((next = strchr(next, '\n'))) next += 1;
} else next = ptr + strlen(ptr);
*value = '\0'; TRACE("uevent %s\n", debugstr_an(ptr, next - ptr - 1));
value++;
if (strcmp(key, "HID_ID") == 0) if (!strncmp(ptr, "HID_UNIQ=", 9))
{
/**
* type vendor product
* HID_ID=0003:000005AC:00008242
**/
int ret = sscanf(value, "%x:%x:%x", &bus_type, vendor_id, product_id);
if (ret == 3)
found_id = 1;
}
else if (strcmp(key, "HID_UNIQ") == 0)
{
/* The caller has to free the serial number */
if (*value)
{ {
*serial_number = strdupAtoW(value); if (sscanf(ptr, "HID_UNIQ=%256s\n", buffer) != 1 || !*buffer) continue;
found_serial = 1; if (!*serial_number) *serial_number = strdupAtoW(buffer);
}
if (!strncmp(ptr, "HID_PHYS=", 9))
{
if (!(tmp = strstr(ptr, "/input")) || tmp >= next) continue;
if (*input == -1) sscanf(tmp, "/input%d\n", input);
}
if (!strncmp(ptr, "HID_ID=", 7))
{
if (bus || *vendor_id || *product_id) continue;
sscanf(ptr, "HID_ID=%x:%x:%x\n", &bus, vendor_id, product_id);
} }
} }
else if (strcmp(key, "HID_PHYS") == 0)
{
const char *input_no = strstr(value, "input");
if (input_no)
*input = atoi(input_no+5 );
}
next_line:
line = strtok_r(NULL, "\n", &saveptr);
} }
heap_free(tmp);
return (found_id && found_serial);
} }
static DWORD a_to_bcd(const char *s) static DWORD a_to_bcd(const char *s)
@ -1072,14 +1044,13 @@ static DWORD a_to_bcd(const char *s)
static void try_add_device(struct udev_device *dev) static void try_add_device(struct udev_device *dev)
{ {
DWORD vid = 0, pid = 0, version = 0; DWORD vid = 0, pid = 0, version = 0, input = -1;
struct udev_device *hiddev = NULL, *walk_device; struct udev_device *hiddev = NULL, *walk_device;
DEVICE_OBJECT *device = NULL; DEVICE_OBJECT *device = NULL;
const char *subsystem; const char *subsystem;
const char *devnode; const char *devnode;
WCHAR *serial = NULL; WCHAR *serial = NULL;
BOOL is_gamepad = FALSE; BOOL is_gamepad = FALSE;
WORD input = -1;
int fd; int fd;
static const CHAR *base_serial = "0000"; static const CHAR *base_serial = "0000";
@ -1109,8 +1080,7 @@ static void try_add_device(struct udev_device *dev)
if (hiddev) if (hiddev)
{ {
const char *bcdDevice = NULL; const char *bcdDevice = NULL;
parse_uevent_info(udev_device_get_sysattr_value(hiddev, "uevent"), parse_uevent_info(hiddev, &vid, &pid, &input, &serial);
&vid, &pid, &input, &serial);
if (serial == NULL) if (serial == NULL)
serial = strdupAtoW(base_serial); serial = strdupAtoW(base_serial);