From d5a20efd8b53b34dfc52d24ea336e0125b528996 Mon Sep 17 00:00:00 2001 From: Benjamin Tissoires Date: Wed, 7 Oct 2015 20:12:05 -0400 Subject: [PATCH] hidraw: ratbag_hidraw_has_report() can also match the usage and usage page Add a match against the usage page and the usage. If we are not interested in this new match, we can just use 0. Signed-off-by: Benjamin Tissoires --- src/driver-etekcity.c | 2 +- src/driver-hidpp10.c | 2 +- src/driver-hidpp20.c | 2 +- src/driver-roccat.c | 2 +- src/libratbag-hidraw.c | 42 +++++++++++++++++++++++++++++++++++++++--- src/libratbag-hidraw.h | 9 +++++++-- 6 files changed, 50 insertions(+), 9 deletions(-) diff --git a/src/driver-etekcity.c b/src/driver-etekcity.c index a6a70b37..53df8e4f 100644 --- a/src/driver-etekcity.c +++ b/src/driver-etekcity.c @@ -781,7 +781,7 @@ etekcity_probe(struct ratbag_device *device, const struct ratbag_id id) return -ENODEV; } - if (!ratbag_hidraw_has_report(device, ETEKCITY_REPORT_ID_KEY_MAPPING)) { + if (!ratbag_hidraw_has_report(device, ETEKCITY_REPORT_ID_KEY_MAPPING, 0, 0)) { ratbag_close_hidraw(device); return -ENODEV; } diff --git a/src/driver-hidpp10.c b/src/driver-hidpp10.c index 90d1844f..d1558ef0 100644 --- a/src/driver-hidpp10.c +++ b/src/driver-hidpp10.c @@ -194,7 +194,7 @@ hidpp10drv_fill_from_profile(struct ratbag_device *device, struct hidpp10_device static int hidpp10drv_test_hidraw(struct ratbag_device *device) { - return ratbag_hidraw_has_report(device, REPORT_ID_SHORT); + return ratbag_hidraw_has_report(device, REPORT_ID_SHORT, 0, 0); } static int diff --git a/src/driver-hidpp20.c b/src/driver-hidpp20.c index 1cd896e8..1a5cfef8 100644 --- a/src/driver-hidpp20.c +++ b/src/driver-hidpp20.c @@ -434,7 +434,7 @@ hidpp20drv_20_probe(struct ratbag_device *device, const struct ratbag_id id) static int hidpp20drv_test_hidraw(struct ratbag_device *device) { - return ratbag_hidraw_has_report(device, REPORT_ID_LONG); + return ratbag_hidraw_has_report(device, REPORT_ID_LONG, 0, 0); } static int diff --git a/src/driver-roccat.c b/src/driver-roccat.c index d114d3c2..8aa399b3 100644 --- a/src/driver-roccat.c +++ b/src/driver-roccat.c @@ -917,7 +917,7 @@ roccat_probe(struct ratbag_device *device, const struct ratbag_id id) return -ENODEV; } - if (!ratbag_hidraw_has_report(device, ROCCAT_REPORT_ID_KEY_MAPPING)) { + if (!ratbag_hidraw_has_report(device, ROCCAT_REPORT_ID_KEY_MAPPING, 0, 0)) { ratbag_close_hidraw(device); return -ENODEV; } diff --git a/src/libratbag-hidraw.c b/src/libratbag-hidraw.c index dea4b9d5..d4052bc2 100644 --- a/src/libratbag-hidraw.c +++ b/src/libratbag-hidraw.c @@ -275,17 +275,53 @@ ratbag_open_hidraw(struct ratbag_device *device) return __ratbag_find_hidraw(device, hidraw_match_all, false); } +static inline int +ratbag_hidraw_match_report(struct ratbag_hid_report *report, + unsigned int report_id, + unsigned int usage_page, + unsigned int usage) +{ + if (usage && usage != report->usage) + return 0; + + if (usage_page && usage_page != report->usage_page) + return 0; + + return report->report_id == report_id; +} + int -ratbag_hidraw_has_report(struct ratbag_device *device, unsigned int report_id) +ratbag_hidraw_has_report(struct ratbag_device *device, + unsigned int report_id, + unsigned int usage_page, + unsigned int usage) { unsigned i; if (report_id == 0) - return device->hidraw.reports[0].report_id == report_id; + if (ratbag_hidraw_match_report(&device->hidraw.reports[0], + report_id, + usage_page, + usage)) { + log_debug(device->ratbag, + "found matching report ID (0) with 0x%04x | 0x%04x\n", + device->hidraw.reports[0].usage_page, + device->hidraw.reports[0].usage); + return 1; + } for (i = 0; i < device->hidraw.num_reports; i++) { - if (device->hidraw.reports[i].report_id == report_id) + if (ratbag_hidraw_match_report(&device->hidraw.reports[i], + report_id, + usage_page, + usage)) { + log_debug(device->ratbag, + "found matching report ID (%d) with 0x%04x | 0x%04x\n", + report_id, + device->hidraw.reports[i].usage_page, + device->hidraw.reports[i].usage); return 1; + } } return 0; diff --git a/src/libratbag-hidraw.h b/src/libratbag-hidraw.h index e31b9628..9657281b 100644 --- a/src/libratbag-hidraw.h +++ b/src/libratbag-hidraw.h @@ -120,11 +120,16 @@ int ratbag_hidraw_read_input_report(struct ratbag_device *device, uint8_t *buf, * Tells if a given device has the specified report ID. * * @param device the ratbag device which hidraw node is opened - * @param report_id the report ID we inquire about + * @param report_id the report ID we inquire about (0 means no report ID) + * @param usage_page the usage page we want to match with (0 ignores the match) + * @param usage the usage we want to match with (0 ignores the match) * * @return 1 if the device has the given report id, 0 otherwise */ int -ratbag_hidraw_has_report(struct ratbag_device *device, unsigned int report_id); +ratbag_hidraw_has_report(struct ratbag_device *device, + unsigned int report_id, + unsigned int usage_page, + unsigned int usage); #endif /* LIBRATBAG_HIDRAW_H */