diff --git a/src/libratbag-hidraw.c b/src/libratbag-hidraw.c index ec31134f..32bf638a 100644 --- a/src/libratbag-hidraw.c +++ b/src/libratbag-hidraw.c @@ -42,9 +42,12 @@ static int ratbag_hidraw_parse_report_descriptor(struct ratbag_device *device) { int rc, desc_size = 0; + struct ratbag_hidraw *hidraw = &device->hidraw; struct hidraw_report_descriptor report_desc = {0}; unsigned int i, j; + hidraw->num_report_ids = 0; + rc = ioctl(device->hidraw.fd, HIDIOCGRDESCSIZE, &desc_size); if (rc < 0) return rc; @@ -54,10 +57,6 @@ ratbag_hidraw_parse_report_descriptor(struct ratbag_device *device) if (rc < 0) return rc; - log_buf_error(device->ratbag, - "Report Descriptor:\n", - report_desc.value, report_desc.size); - i = 0; while (i < report_desc.size) { uint8_t value = report_desc.value[i]; @@ -70,16 +69,19 @@ ratbag_hidraw_parse_report_descriptor(struct ratbag_device *device) if (i + size >= report_desc.size) return -EPROTO; - if (hid == HID_REPORT_ID) - log_error(device->ratbag, "main item: %02x\n", hid); + if (hid == HID_REPORT_ID) { + unsigned report_id = 0; - for (j = 0; j < size; j++) { - value = report_desc.value[i + j + 1]; - if (hid == HID_REPORT_ID) - log_error(device->ratbag, " %02x\n", value); + for (j = 0; j < size; j++) { + report_id |= report_desc.value[i + j + 1] << ((size - j - 1) * 8); + log_debug(device->ratbag, "report ID %02x\n", report_id); + if (hidraw->report_ids) + hidraw->report_ids[hidraw->num_report_ids] = report_id; + hidraw->num_report_ids++; + } } - i += 1 + j; + i += 1 + size; } return 0; @@ -110,6 +112,7 @@ ratbag_open_hidraw(struct ratbag_device *device) device->hidraw.fd = fd; + /* parse first to count the number of reports */ res = ratbag_hidraw_parse_report_descriptor(device); if (res) { log_error(device->ratbag, @@ -120,6 +123,11 @@ ratbag_open_hidraw(struct ratbag_device *device) goto err; } + if (device->hidraw.num_report_ids) { + device->hidraw.report_ids = zalloc(device->hidraw.num_report_ids * + sizeof(uint8_t)); + ratbag_hidraw_parse_report_descriptor(device); + } return 0; @@ -137,6 +145,11 @@ ratbag_close_hidraw(struct ratbag_device *device) ratbag_close_fd(device, device->hidraw.fd); device->hidraw.fd = -1; + + if (device->hidraw.report_ids) { + free(device->hidraw.report_ids); + device->hidraw.report_ids = NULL; + } } int diff --git a/src/libratbag-hidraw.h b/src/libratbag-hidraw.h index b4065207..ac03596e 100644 --- a/src/libratbag-hidraw.h +++ b/src/libratbag-hidraw.h @@ -36,6 +36,8 @@ struct ratbag_hidraw { int fd; + uint8_t *report_ids; + unsigned num_report_ids; }; /**