From bc179b1030ccf9dd02818f77ecd9b5b9f14e85b7 Mon Sep 17 00:00:00 2001 From: Peter Johanson Date: Tue, 5 Oct 2021 02:41:56 +0000 Subject: [PATCH] feat(hid): Kconfig for basic/full consumer usages. * Add ZMK_HID_CONSUMER_REPORT_USAGES choice to allow choosing between full consumer usage range, with poor OS compat, or basic consumer usage range, with broader compat. --- app/Kconfig | 19 ++++++++++++++++++- app/include/zmk/hid.h | 25 +++++++++++++++++++++++++ app/src/hid.c | 2 ++ 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/app/Kconfig b/app/Kconfig index 629dfa70..35bf77da 100644 --- a/app/Kconfig +++ b/app/Kconfig @@ -51,13 +51,30 @@ config ZMK_HID_KEYBOARD_REPORT_SIZE int "# Keyboard Keys Reportable" default 6 - endif config ZMK_HID_CONSUMER_REPORT_SIZE int "# Consumer Keys Reportable" default 6 + +choice ZMK_HID_CONSUMER_REPORT_USAGES + prompt "HID Report Type" + +config ZMK_HID_CONSUMER_REPORT_USAGES_FULL + bool "Full Consumer HID Usage Support" + help + Enable full Consumer usage ID values to be sent to hosts. Allows for less + frequently used usages, but has compatibability issues with some host OSes. + +config ZMK_HID_CONSUMER_REPORT_USAGES_BASIC + bool "Basic Consumer HID Usage Support" + help + Enable Consumer usage ID values up to "Playback Speed - Slow" to be sent to + hosts. Allows for broader compatibability with more host OSes. + +endchoice + menu "Output Types" config ZMK_USB diff --git a/app/include/zmk/hid.h b/app/include/zmk/hid.h index be88c175..95b82d46 100644 --- a/app/include/zmk/hid.h +++ b/app/include/zmk/hid.h @@ -138,6 +138,24 @@ static const uint8_t zmk_hid_report_desc[] = { /* USAGE_PAGE (Consumer) */ HID_GI_USAGE_PAGE, HID_USAGE_CONSUMER, + +#if IS_ENABLED(CONFIG_ZMK_HID_CONSUMER_REPORT_USAGES_BASIC) + /* LOGICAL_MINIMUM (0) */ + HID_GI_LOGICAL_MIN(1), + 0x00, + /* LOGICAL_MAXIMUM (0xFFFF) */ + HID_GI_LOGICAL_MAX(1), + 0xFF, + HID_LI_USAGE_MIN(1), + 0x00, + /* USAGE_MAXIMUM (0xFFFF) */ + HID_LI_USAGE_MAX(1), + 0xFF, + /* INPUT (Data,Ary,Abs) */ + /* REPORT_SIZE (8) */ + HID_GI_REPORT_SIZE, + 0x08, +#elif IS_ENABLED(CONFIG_ZMK_HID_CONSUMER_REPORT_USAGES_FULL) /* LOGICAL_MINIMUM (0) */ HID_GI_LOGICAL_MIN(1), 0x00, @@ -155,6 +173,9 @@ static const uint8_t zmk_hid_report_desc[] = { /* REPORT_SIZE (16) */ HID_GI_REPORT_SIZE, 0x10, +#else +#error "A proper consumer HID report usage range must be selected" +#endif /* REPORT_COUNT (CONFIG_ZMK_HID_CONSUMER_REPORT_SIZE) */ HID_GI_REPORT_COUNT, CONFIG_ZMK_HID_CONSUMER_REPORT_SIZE, @@ -187,7 +208,11 @@ struct zmk_hid_keyboard_report { } __packed; struct zmk_hid_consumer_report_body { +#if IS_ENABLED(CONFIG_ZMK_HID_CONSUMER_REPORT_USAGES_BASIC) + uint8_t keys[CONFIG_ZMK_HID_CONSUMER_REPORT_SIZE]; +#elif IS_ENABLED(CONFIG_ZMK_HID_CONSUMER_REPORT_USAGES_FULL) uint16_t keys[CONFIG_ZMK_HID_CONSUMER_REPORT_SIZE]; +#endif } __packed; struct zmk_hid_consumer_report { diff --git a/app/src/hid.c b/app/src/hid.c index 756ed900..b524b09f 100644 --- a/app/src/hid.c +++ b/app/src/hid.c @@ -117,6 +117,8 @@ static inline int deselect_keyboard_usage(zmk_key_t usage) { #endif #define TOGGLE_CONSUMER(match, val) \ + COND_CODE_1(IS_ENABLED(CONFIG_ZMK_HID_CONSUMER_REPORT_USAGES_BASIC), \ + (if (val > 0xFF) { return -ENOTSUP; }), ()) \ for (int idx = 0; idx < CONFIG_ZMK_HID_CONSUMER_REPORT_SIZE; idx++) { \ if (consumer_report.body.keys[idx] != match) { \ continue; \