Peter Hutterer
2016-07-13 00:14:14 UTC
The existing libwacom_get_button_led_group() only works for mode toggle
buttons but we have a need to generally group buttons together. libwacom is
where this information should be stored so add the call here rather than
relying on the caller to know that identical physical positioning is the same
as being in the same led group.
Signed-off-by: Peter Hutterer <***@who-t.net>
---
I'm not happy about the function name, but short of
libwacom_get_button_led_group2() I couldn't think of anything else.
Suggestions welcome.
libwacom/libwacom.c | 33 +++++++++++++++++++++++++++++++++
libwacom/libwacom.h | 21 +++++++++++++++++++++
test/load.c | 43 +++++++++++++++++++++++++++++++++++++++++++
test/tablet-validity.c | 14 ++++++++++++++
4 files changed, 111 insertions(+)
diff --git a/libwacom/libwacom.c b/libwacom/libwacom.c
index f96bbab..4aa14d0 100644
--- a/libwacom/libwacom.c
+++ b/libwacom/libwacom.c
@@ -1006,6 +1006,39 @@ int libwacom_get_button_led_group (const WacomDevice *device, char button)
return -1;
}
+int libwacom_get_button_matching_led_group(const WacomDevice *device,
+ char button)
+{
+ int toggle_btn, button_index;
+ WacomButtonFlags toggle_btn_flags, button_flags;
+ int led_group;
+
+ g_return_val_if_fail (device->num_buttons > 0, -1);
+ g_return_val_if_fail (button >= 'A', -1);
+ g_return_val_if_fail (button < 'A' + device->num_buttons, -1);
+
+ led_group = libwacom_get_button_led_group(device, button);
+ if (led_group != -1)
+ return led_group;
+
+ button_index = button - 'A';
+ button_flags = device->buttons[button_index];
+
+ for (toggle_btn = 0; toggle_btn < device->num_buttons; toggle_btn++) {
+ toggle_btn_flags = device->buttons[toggle_btn];
+ if ((toggle_btn_flags & WACOM_BUTTON_MODESWITCH) == 0)
+ continue;
+
+ if ((toggle_btn_flags & WACOM_BUTTON_DIRECTION) !=
+ (button_flags & WACOM_BUTTON_DIRECTION))
+ continue;
+
+ return libwacom_get_button_led_group(device, 'A' + toggle_btn);
+ }
+
+ return -1;
+}
+
int libwacom_is_builtin(const WacomDevice *device)
{
return !!(libwacom_get_integration_flags (device) & WACOM_DEVICE_INTEGRATED_DISPLAY);
diff --git a/libwacom/libwacom.h b/libwacom/libwacom.h
index d4c94ef..b8a525c 100644
--- a/libwacom/libwacom.h
+++ b/libwacom/libwacom.h
@@ -506,6 +506,10 @@ int libwacom_get_strips_num_modes(const WacomDevice *device);
const WacomStatusLEDs *libwacom_get_status_leds(const WacomDevice *device, int *num_leds);
/**
+ * Return the LED group this button belongs to, provided this button is a
+ * mode switch button (i.e. @ref WACOM_BUTTON_MODESWITCH is set in the
+ * button flags). For all other buttons, this function returns -1.
+ *
* @param device The tablet to query
* @param button The ID of the button to check for, between 'A' and 'Z'
* @return the status LED group id to use
@@ -515,6 +519,23 @@ int libwacom_get_button_led_group (const WacomDevice *device,
char button);
/**
+ * This function is equivalent to libwacom_get_button_led_group() but works
+ * for any button that libwacom considers to be in the same LED group.
+ *
+ * For example, if button 'A' is a mode switch button for led group 1 and
+ * button 'A' has the same physical positioning as button 'B' and 'C' (and
+ * these buttons are not mode-switch buttons themselves) then this function
+ * returns 1 for all three buttons 'A', 'B' and 'C'.
+ *
+ * @param device The tablet to query
+ * @param button The ID of the button to check for, between 'A' and 'Z'
+ * @return the status LED group id to use
+ * or -1 if no LED is available for the given tablet / button
+ */
+int libwacom_get_button_matching_led_group(const WacomDevice *device,
+ char button);
+
+/**
* @param device The tablet to query
* @return non-zero if the device is built into the screen (ie a screen tablet)
* or zero if the device is an external tablet
diff --git a/test/load.c b/test/load.c
index 583cd06..b1cec94 100644
--- a/test/load.c
+++ b/test/load.c
@@ -62,6 +62,7 @@ int main(int argc, char **argv)
WacomDevice *device;
const WacomMatch *match;
const char *str;
+ int btn;
db = libwacom_database_new_for_path(TOPSRCDIR"/data");
if (!db)
@@ -92,6 +93,19 @@ int main(int argc, char **argv)
assert(libwacom_get_width(device) == 8);
assert(libwacom_get_height(device) == 5);
+ btn = libwacom_get_num_buttons(device);
+ while (btn--) {
+ int group;
+ group = libwacom_get_button_matching_led_group(device, 'A' + btn);
+ assert(group == 0);
+
+ group = libwacom_get_button_led_group(device, 'A' + btn);
+ if (btn > 0)
+ assert(group == -1);
+ else
+ assert(group == 0);
+ }
+
/* I4 WL has two matches */
check_multiple_match(device);
@@ -140,6 +154,35 @@ int main(int argc, char **argv)
assert(libwacom_match_get_product_id(match) == 0xf6);
assert(libwacom_match_get_bustype(match) == WBUSTYPE_USB);
+ btn = libwacom_get_num_buttons(device);
+ while (btn--) {
+ int group;
+ group = libwacom_get_button_matching_led_group(device, 'A' + btn);
+ if (btn > 7)
+ assert(group == 0);
+ else
+ assert(group == 1);
+
+ group = libwacom_get_button_led_group(device, 'A' + btn);
+ printf("group for %d: %d\n", btn, group);
+
+ switch(btn) {
+ case 0:
+ case 1:
+ case 2:
+ assert(group == 1);
+ break;
+ case 8:
+ case 9:
+ case 10:
+ assert(group == 0);
+ break;
+ default:
+ assert(group == -1);
+ break;
+ }
+ }
+
libwacom_database_destroy (db);
return 0;
}
diff --git a/test/tablet-validity.c b/test/tablet-validity.c
index 1146f58..c07929f 100644
--- a/test/tablet-validity.c
+++ b/test/tablet-validity.c
@@ -171,6 +171,20 @@ static int verify_led_groups(WacomDevice *device)
assert(group_flags == (1 << nleds) - 1);
+ /* Check we have at least one non-toggle button in each group */
+ group_flags = 0;
+ for (btn_idx = 0; btn_idx < nbuttons; btn_idx++) {
+ WacomButtonFlags flags = libwacom_get_button_flag(device, 'A' + btn_idx);
+ int group = libwacom_get_button_matching_led_group(device, 'A' + btn_idx);
+
+ if (flags & WACOM_BUTTON_MODESWITCH)
+ continue;
+
+ assert(group != -1);
+ group_flags |= 1 << group;
+ }
+ assert(group_flags == (1 << nleds) - 1);
+
return 1;
}
buttons but we have a need to generally group buttons together. libwacom is
where this information should be stored so add the call here rather than
relying on the caller to know that identical physical positioning is the same
as being in the same led group.
Signed-off-by: Peter Hutterer <***@who-t.net>
---
I'm not happy about the function name, but short of
libwacom_get_button_led_group2() I couldn't think of anything else.
Suggestions welcome.
libwacom/libwacom.c | 33 +++++++++++++++++++++++++++++++++
libwacom/libwacom.h | 21 +++++++++++++++++++++
test/load.c | 43 +++++++++++++++++++++++++++++++++++++++++++
test/tablet-validity.c | 14 ++++++++++++++
4 files changed, 111 insertions(+)
diff --git a/libwacom/libwacom.c b/libwacom/libwacom.c
index f96bbab..4aa14d0 100644
--- a/libwacom/libwacom.c
+++ b/libwacom/libwacom.c
@@ -1006,6 +1006,39 @@ int libwacom_get_button_led_group (const WacomDevice *device, char button)
return -1;
}
+int libwacom_get_button_matching_led_group(const WacomDevice *device,
+ char button)
+{
+ int toggle_btn, button_index;
+ WacomButtonFlags toggle_btn_flags, button_flags;
+ int led_group;
+
+ g_return_val_if_fail (device->num_buttons > 0, -1);
+ g_return_val_if_fail (button >= 'A', -1);
+ g_return_val_if_fail (button < 'A' + device->num_buttons, -1);
+
+ led_group = libwacom_get_button_led_group(device, button);
+ if (led_group != -1)
+ return led_group;
+
+ button_index = button - 'A';
+ button_flags = device->buttons[button_index];
+
+ for (toggle_btn = 0; toggle_btn < device->num_buttons; toggle_btn++) {
+ toggle_btn_flags = device->buttons[toggle_btn];
+ if ((toggle_btn_flags & WACOM_BUTTON_MODESWITCH) == 0)
+ continue;
+
+ if ((toggle_btn_flags & WACOM_BUTTON_DIRECTION) !=
+ (button_flags & WACOM_BUTTON_DIRECTION))
+ continue;
+
+ return libwacom_get_button_led_group(device, 'A' + toggle_btn);
+ }
+
+ return -1;
+}
+
int libwacom_is_builtin(const WacomDevice *device)
{
return !!(libwacom_get_integration_flags (device) & WACOM_DEVICE_INTEGRATED_DISPLAY);
diff --git a/libwacom/libwacom.h b/libwacom/libwacom.h
index d4c94ef..b8a525c 100644
--- a/libwacom/libwacom.h
+++ b/libwacom/libwacom.h
@@ -506,6 +506,10 @@ int libwacom_get_strips_num_modes(const WacomDevice *device);
const WacomStatusLEDs *libwacom_get_status_leds(const WacomDevice *device, int *num_leds);
/**
+ * Return the LED group this button belongs to, provided this button is a
+ * mode switch button (i.e. @ref WACOM_BUTTON_MODESWITCH is set in the
+ * button flags). For all other buttons, this function returns -1.
+ *
* @param device The tablet to query
* @param button The ID of the button to check for, between 'A' and 'Z'
* @return the status LED group id to use
@@ -515,6 +519,23 @@ int libwacom_get_button_led_group (const WacomDevice *device,
char button);
/**
+ * This function is equivalent to libwacom_get_button_led_group() but works
+ * for any button that libwacom considers to be in the same LED group.
+ *
+ * For example, if button 'A' is a mode switch button for led group 1 and
+ * button 'A' has the same physical positioning as button 'B' and 'C' (and
+ * these buttons are not mode-switch buttons themselves) then this function
+ * returns 1 for all three buttons 'A', 'B' and 'C'.
+ *
+ * @param device The tablet to query
+ * @param button The ID of the button to check for, between 'A' and 'Z'
+ * @return the status LED group id to use
+ * or -1 if no LED is available for the given tablet / button
+ */
+int libwacom_get_button_matching_led_group(const WacomDevice *device,
+ char button);
+
+/**
* @param device The tablet to query
* @return non-zero if the device is built into the screen (ie a screen tablet)
* or zero if the device is an external tablet
diff --git a/test/load.c b/test/load.c
index 583cd06..b1cec94 100644
--- a/test/load.c
+++ b/test/load.c
@@ -62,6 +62,7 @@ int main(int argc, char **argv)
WacomDevice *device;
const WacomMatch *match;
const char *str;
+ int btn;
db = libwacom_database_new_for_path(TOPSRCDIR"/data");
if (!db)
@@ -92,6 +93,19 @@ int main(int argc, char **argv)
assert(libwacom_get_width(device) == 8);
assert(libwacom_get_height(device) == 5);
+ btn = libwacom_get_num_buttons(device);
+ while (btn--) {
+ int group;
+ group = libwacom_get_button_matching_led_group(device, 'A' + btn);
+ assert(group == 0);
+
+ group = libwacom_get_button_led_group(device, 'A' + btn);
+ if (btn > 0)
+ assert(group == -1);
+ else
+ assert(group == 0);
+ }
+
/* I4 WL has two matches */
check_multiple_match(device);
@@ -140,6 +154,35 @@ int main(int argc, char **argv)
assert(libwacom_match_get_product_id(match) == 0xf6);
assert(libwacom_match_get_bustype(match) == WBUSTYPE_USB);
+ btn = libwacom_get_num_buttons(device);
+ while (btn--) {
+ int group;
+ group = libwacom_get_button_matching_led_group(device, 'A' + btn);
+ if (btn > 7)
+ assert(group == 0);
+ else
+ assert(group == 1);
+
+ group = libwacom_get_button_led_group(device, 'A' + btn);
+ printf("group for %d: %d\n", btn, group);
+
+ switch(btn) {
+ case 0:
+ case 1:
+ case 2:
+ assert(group == 1);
+ break;
+ case 8:
+ case 9:
+ case 10:
+ assert(group == 0);
+ break;
+ default:
+ assert(group == -1);
+ break;
+ }
+ }
+
libwacom_database_destroy (db);
return 0;
}
diff --git a/test/tablet-validity.c b/test/tablet-validity.c
index 1146f58..c07929f 100644
--- a/test/tablet-validity.c
+++ b/test/tablet-validity.c
@@ -171,6 +171,20 @@ static int verify_led_groups(WacomDevice *device)
assert(group_flags == (1 << nleds) - 1);
+ /* Check we have at least one non-toggle button in each group */
+ group_flags = 0;
+ for (btn_idx = 0; btn_idx < nbuttons; btn_idx++) {
+ WacomButtonFlags flags = libwacom_get_button_flag(device, 'A' + btn_idx);
+ int group = libwacom_get_button_matching_led_group(device, 'A' + btn_idx);
+
+ if (flags & WACOM_BUTTON_MODESWITCH)
+ continue;
+
+ assert(group != -1);
+ group_flags |= 1 << group;
+ }
+ assert(group_flags == (1 << nleds) - 1);
+
return 1;
}
--
2.7.4
2.7.4