2 #include "action_tapping.h"
4 static uint16_t last_td;
5 static int8_t highest_td = -1;
7 void qk_tap_dance_pair_finished (qk_tap_dance_state_t *state, void *user_data) {
8 qk_tap_dance_pair_t *pair = (qk_tap_dance_pair_t *)user_data;
10 if (state->count == 1) {
11 register_code16 (pair->kc1);
12 } else if (state->count == 2) {
13 register_code16 (pair->kc2);
17 void qk_tap_dance_pair_reset (qk_tap_dance_state_t *state, void *user_data) {
18 qk_tap_dance_pair_t *pair = (qk_tap_dance_pair_t *)user_data;
20 if (state->count == 1) {
21 unregister_code16 (pair->kc1);
22 } else if (state->count == 2) {
23 unregister_code16 (pair->kc2);
27 static inline void _process_tap_dance_action_fn (qk_tap_dance_state_t *state,
29 qk_tap_dance_user_fn_t fn)
36 static inline void process_tap_dance_action_on_each_tap (qk_tap_dance_action_t *action)
38 _process_tap_dance_action_fn (&action->state, action->user_data, action->fn.on_each_tap);
41 static inline void process_tap_dance_action_on_dance_finished (qk_tap_dance_action_t *action)
43 if (action->state.finished)
45 action->state.finished = true;
46 add_mods(action->state.oneshot_mods);
47 send_keyboard_report();
48 _process_tap_dance_action_fn (&action->state, action->user_data, action->fn.on_dance_finished);
51 static inline void process_tap_dance_action_on_reset (qk_tap_dance_action_t *action)
53 _process_tap_dance_action_fn (&action->state, action->user_data, action->fn.on_reset);
54 del_mods(action->state.oneshot_mods);
55 send_keyboard_report();
58 bool process_tap_dance(uint16_t keycode, keyrecord_t *record) {
59 uint16_t idx = keycode - QK_TAP_DANCE;
60 qk_tap_dance_action_t *action;
62 if (last_td && last_td != keycode) {
63 (&tap_dance_actions[last_td - QK_TAP_DANCE])->state.interrupted = true;
67 case QK_TAP_DANCE ... QK_TAP_DANCE_MAX:
68 if ((int16_t)idx > highest_td)
70 action = &tap_dance_actions[idx];
72 action->state.pressed = record->event.pressed;
73 if (record->event.pressed) {
74 action->state.keycode = keycode;
75 action->state.count++;
76 action->state.timer = timer_read();
77 action->state.oneshot_mods = get_oneshot_mods();
78 process_tap_dance_action_on_each_tap (action);
80 if (last_td && last_td != keycode) {
81 qk_tap_dance_action_t *paction = &tap_dance_actions[last_td - QK_TAP_DANCE];
82 paction->state.interrupted = true;
83 process_tap_dance_action_on_dance_finished (paction);
84 reset_tap_dance (&paction->state);
93 if (!record->event.pressed)
99 for (int i = 0; i <= highest_td; i++) {
100 action = &tap_dance_actions[i];
101 if (action->state.count == 0)
103 action->state.interrupted = true;
104 process_tap_dance_action_on_dance_finished (action);
105 reset_tap_dance (&action->state);
113 void matrix_scan_tap_dance () {
114 if (highest_td == -1)
117 for (int i = 0; i <= highest_td; i++) {
118 qk_tap_dance_action_t *action = &tap_dance_actions[i];
120 if (action->state.count && timer_elapsed (action->state.timer) > TAPPING_TERM) {
121 process_tap_dance_action_on_dance_finished (action);
122 reset_tap_dance (&action->state);
127 void reset_tap_dance (qk_tap_dance_state_t *state) {
128 qk_tap_dance_action_t *action;
133 action = &tap_dance_actions[state->keycode - QK_TAP_DANCE];
135 process_tap_dance_action_on_reset (action);
138 state->interrupted = false;
139 state->finished = false;