From 4ae0ca5a11c475ee49bbce13d29edbf9bd6fc636 Mon Sep 17 00:00:00 2001 From: Nick Brassel Date: Sat, 6 Jul 2024 09:57:54 +1000 Subject: [PATCH] Tap dance introspection (#24049) --- quantum/keymap_introspection.c | 23 +++++++++++++++++++ quantum/keymap_introspection.h | 25 +++++++++++++++++++-- quantum/process_keycode/process_tap_dance.c | 12 +++++++--- quantum/process_keycode/process_tap_dance.h | 4 +--- tests/tap_dance/examples.c | 3 ++- tests/tap_dance/tap_dance_layers/test.mk | 2 +- tests/tap_dance/test.mk | 3 +-- 7 files changed, 60 insertions(+), 12 deletions(-) diff --git a/quantum/keymap_introspection.c b/quantum/keymap_introspection.c index 71e3b429ea..4e95125335 100644 --- a/quantum/keymap_introspection.c +++ b/quantum/keymap_introspection.c @@ -109,3 +109,26 @@ __attribute__((weak)) combo_t* combo_get(uint16_t combo_idx) { } #endif // defined(COMBO_ENABLE) + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// Tap Dance + +#if defined(TAP_DANCE_ENABLE) + +uint16_t tap_dance_count_raw(void) { + return sizeof(tap_dance_actions) / sizeof(tap_dance_action_t); +} + +uint16_t tap_dance_count(void) { + return tap_dance_count_raw(); +} + +tap_dance_action_t* tap_dance_get_raw(uint16_t tap_dance_idx) { + return &tap_dance_actions[tap_dance_idx]; +} + +tap_dance_action_t* tap_dance_get(uint16_t tap_dance_idx) { + return tap_dance_get_raw(tap_dance_idx); +} + +#endif // defined(TAP_DANCE_ENABLE) diff --git a/quantum/keymap_introspection.h b/quantum/keymap_introspection.h index f7516bf42a..bc4dd93b4c 100644 --- a/quantum/keymap_introspection.h +++ b/quantum/keymap_introspection.h @@ -61,9 +61,30 @@ uint16_t combo_count_raw(void); // Get the number of combos defined in the user's keymap, potentially stored dynamically uint16_t combo_count(void); -// Get the keycode for the encoder mapping location, stored in firmware rather than any other persistent storage +// Get the combo definition, stored in firmware rather than any other persistent storage combo_t* combo_get_raw(uint16_t combo_idx); -// Get the keycode for the encoder mapping location, potentially stored dynamically +// Get the combo definition, potentially stored dynamically combo_t* combo_get(uint16_t combo_idx); #endif // defined(COMBO_ENABLE) + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// Tap Dance + +#if defined(TAP_DANCE_ENABLE) + +// Forward declaration of tap_dance_action_t so we don't need to deal with header reordering +struct tap_dance_action_t; +typedef struct tap_dance_action_t tap_dance_action_t; + +// Get the number of tap dances defined in the user's keymap, stored in firmware rather than any other persistent storage +uint16_t tap_dance_count_raw(void); +// Get the number of tap dances defined in the user's keymap, potentially stored dynamically +uint16_t tap_dance_count(void); + +// Get the tap dance definitions, stored in firmware rather than any other persistent storage +tap_dance_action_t* tap_dance_get_raw(uint16_t tap_dance_idx); +// Get the tap dance definitions, potentially stored dynamically +tap_dance_action_t* tap_dance_get(uint16_t tap_dance_idx); + +#endif // defined(TAP_DANCE_ENABLE) diff --git a/quantum/process_keycode/process_tap_dance.c b/quantum/process_keycode/process_tap_dance.c index ce3b8fc81f..11df62763d 100644 --- a/quantum/process_keycode/process_tap_dance.c +++ b/quantum/process_keycode/process_tap_dance.c @@ -21,6 +21,7 @@ #include "action_util.h" #include "timer.h" #include "wait.h" +#include "keymap_introspection.h" static uint16_t active_td; static uint16_t last_tap_time; @@ -133,7 +134,7 @@ bool preprocess_tap_dance(uint16_t keycode, keyrecord_t *record) { if (!active_td || keycode == active_td) return false; - action = &tap_dance_actions[QK_TAP_DANCE_GET_INDEX(active_td)]; + action = tap_dance_get(QK_TAP_DANCE_GET_INDEX(active_td)); action->state.interrupted = true; action->state.interrupting_keycode = keycode; process_tap_dance_action_on_dance_finished(action); @@ -150,11 +151,16 @@ bool preprocess_tap_dance(uint16_t keycode, keyrecord_t *record) { } bool process_tap_dance(uint16_t keycode, keyrecord_t *record) { + int td_index; tap_dance_action_t *action; switch (keycode) { case QK_TAP_DANCE ... QK_TAP_DANCE_MAX: - action = &tap_dance_actions[QK_TAP_DANCE_GET_INDEX(keycode)]; + td_index = QK_TAP_DANCE_GET_INDEX(keycode); + if (td_index >= tap_dance_count()) { + return false; + } + action = tap_dance_get(td_index); action->state.pressed = record->event.pressed; if (record->event.pressed) { @@ -182,7 +188,7 @@ void tap_dance_task(void) { if (!active_td || timer_elapsed(last_tap_time) <= GET_TAPPING_TERM(active_td, &(keyrecord_t){})) return; - action = &tap_dance_actions[QK_TAP_DANCE_GET_INDEX(active_td)]; + action = tap_dance_get(QK_TAP_DANCE_GET_INDEX(active_td)); if (!action->state.interrupted) { process_tap_dance_action_on_dance_finished(action); } diff --git a/quantum/process_keycode/process_tap_dance.h b/quantum/process_keycode/process_tap_dance.h index c0137c14a3..5cccbdf439 100644 --- a/quantum/process_keycode/process_tap_dance.h +++ b/quantum/process_keycode/process_tap_dance.h @@ -35,7 +35,7 @@ typedef struct { typedef void (*tap_dance_user_fn_t)(tap_dance_state_t *state, void *user_data); -typedef struct { +typedef struct tap_dance_action_t { tap_dance_state_t state; struct { tap_dance_user_fn_t on_each_tap; @@ -78,8 +78,6 @@ typedef struct { #define TD_INDEX(code) QK_TAP_DANCE_GET_INDEX(code) #define TAP_DANCE_KEYCODE(state) TD(((tap_dance_action_t *)state) - tap_dance_actions) -extern tap_dance_action_t tap_dance_actions[]; - void reset_tap_dance(tap_dance_state_t *state); /* To be used internally */ diff --git a/tests/tap_dance/examples.c b/tests/tap_dance/examples.c index 5377b397d3..4b6bdb2090 100644 --- a/tests/tap_dance/examples.c +++ b/tests/tap_dance/examples.c @@ -16,6 +16,7 @@ #include "quantum.h" #include "examples.h" +#include "keymap_introspection.h" // Example code from the tap dance documentation, adapted for testing @@ -83,7 +84,7 @@ bool process_record_user(uint16_t keycode, keyrecord_t *record) { switch (keycode) { case TD(CT_CLN): - action = &tap_dance_actions[QK_TAP_DANCE_GET_INDEX(keycode)]; + action = tap_dance_get(QK_TAP_DANCE_GET_INDEX(keycode)); if (!record->event.pressed && action->state.count && !action->state.finished) { tap_dance_tap_hold_t *tap_hold = (tap_dance_tap_hold_t *)action->user_data; tap_code16(tap_hold->tap); diff --git a/tests/tap_dance/tap_dance_layers/test.mk b/tests/tap_dance/tap_dance_layers/test.mk index b4cdc9b088..a677fda648 100644 --- a/tests/tap_dance/tap_dance_layers/test.mk +++ b/tests/tap_dance/tap_dance_layers/test.mk @@ -7,4 +7,4 @@ TAP_DANCE_ENABLE = yes -SRC += tap_dance_defs.c +INTROSPECTION_KEYMAP_C = tap_dance_defs.c diff --git a/tests/tap_dance/test.mk b/tests/tap_dance/test.mk index 041d9b4dc9..0e727da9e8 100644 --- a/tests/tap_dance/test.mk +++ b/tests/tap_dance/test.mk @@ -18,5 +18,4 @@ # -------------------------------------------------------------------------------- TAP_DANCE_ENABLE = yes - -SRC += examples.c +INTROSPECTION_KEYMAP_C = examples.c