Skip to content

Commit

Permalink
Tap dance introspection (#24049)
Browse files Browse the repository at this point in the history
  • Loading branch information
tzarc authored Jul 5, 2024
1 parent b9e6734 commit 4ae0ca5
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 12 deletions.
23 changes: 23 additions & 0 deletions quantum/keymap_introspection.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
25 changes: 23 additions & 2 deletions quantum/keymap_introspection.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
12 changes: 9 additions & 3 deletions quantum/process_keycode/process_tap_dance.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand Down
4 changes: 1 addition & 3 deletions quantum/process_keycode/process_tap_dance.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 */
Expand Down
3 changes: 2 additions & 1 deletion tests/tap_dance/examples.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "quantum.h"
#include "examples.h"
#include "keymap_introspection.h"

// Example code from the tap dance documentation, adapted for testing

Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion tests/tap_dance/tap_dance_layers/test.mk
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@

TAP_DANCE_ENABLE = yes

SRC += tap_dance_defs.c
INTROSPECTION_KEYMAP_C = tap_dance_defs.c
3 changes: 1 addition & 2 deletions tests/tap_dance/test.mk
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,4 @@
# --------------------------------------------------------------------------------

TAP_DANCE_ENABLE = yes

SRC += examples.c
INTROSPECTION_KEYMAP_C = examples.c

0 comments on commit 4ae0ca5

Please sign in to comment.