1 /* Copyright 2016 Jack Humbert
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "action_tapping.h"
20 #define TAPPING_TERM 200
23 #ifndef NO_ACTION_ONESHOT
24 uint8_t get_oneshot_mods(void);
27 static uint16_t last_td;
28 static int8_t highest_td = -1;
30 void qk_tap_dance_pair_on_each_tap (qk_tap_dance_state_t *state, void *user_data) {
31 qk_tap_dance_pair_t *pair = (qk_tap_dance_pair_t *)user_data;
33 if (state->count == 2) {
34 register_code16 (pair->kc2);
35 state->finished = true;
39 void qk_tap_dance_pair_finished (qk_tap_dance_state_t *state, void *user_data) {
40 qk_tap_dance_pair_t *pair = (qk_tap_dance_pair_t *)user_data;
42 if (state->count == 1) {
43 register_code16 (pair->kc1);
44 } else if (state->count == 2) {
45 register_code16 (pair->kc2);
49 void qk_tap_dance_pair_reset (qk_tap_dance_state_t *state, void *user_data) {
50 qk_tap_dance_pair_t *pair = (qk_tap_dance_pair_t *)user_data;
52 if (state->count == 1) {
53 unregister_code16 (pair->kc1);
54 } else if (state->count == 2) {
55 unregister_code16 (pair->kc2);
59 void qk_tap_dance_dual_role_on_each_tap (qk_tap_dance_state_t *state, void *user_data) {
60 qk_tap_dance_dual_role_t *pair = (qk_tap_dance_dual_role_t *)user_data;
62 if (state->count == 2) {
63 layer_move (pair->layer);
64 state->finished = true;
68 void qk_tap_dance_dual_role_finished (qk_tap_dance_state_t *state, void *user_data) {
69 qk_tap_dance_dual_role_t *pair = (qk_tap_dance_dual_role_t *)user_data;
71 if (state->count == 1) {
72 register_code16 (pair->kc);
73 } else if (state->count == 2) {
74 layer_move (pair->layer);
78 void qk_tap_dance_dual_role_reset (qk_tap_dance_state_t *state, void *user_data) {
79 qk_tap_dance_dual_role_t *pair = (qk_tap_dance_dual_role_t *)user_data;
81 if (state->count == 1) {
82 unregister_code16 (pair->kc);
86 static inline void _process_tap_dance_action_fn (qk_tap_dance_state_t *state,
88 qk_tap_dance_user_fn_t fn)
95 static inline void process_tap_dance_action_on_each_tap (qk_tap_dance_action_t *action)
97 _process_tap_dance_action_fn (&action->state, action->user_data, action->fn.on_each_tap);
100 static inline void process_tap_dance_action_on_dance_finished (qk_tap_dance_action_t *action)
102 if (action->state.finished)
104 action->state.finished = true;
105 add_mods(action->state.oneshot_mods);
106 add_weak_mods(action->state.weak_mods);
107 send_keyboard_report();
108 _process_tap_dance_action_fn (&action->state, action->user_data, action->fn.on_dance_finished);
111 static inline void process_tap_dance_action_on_reset (qk_tap_dance_action_t *action)
113 _process_tap_dance_action_fn (&action->state, action->user_data, action->fn.on_reset);
114 del_mods(action->state.oneshot_mods);
115 del_weak_mods(action->state.weak_mods);
116 send_keyboard_report();
119 void preprocess_tap_dance(uint16_t keycode, keyrecord_t *record) {
120 qk_tap_dance_action_t *action;
122 if (!record->event.pressed)
125 if (highest_td == -1)
128 for (int i = 0; i <= highest_td; i++) {
129 action = &tap_dance_actions[i];
130 if (action->state.count) {
131 if (keycode == action->state.keycode && keycode == last_td)
133 action->state.interrupted = true;
134 action->state.interrupting_keycode = keycode;
135 process_tap_dance_action_on_dance_finished (action);
136 reset_tap_dance (&action->state);
141 bool process_tap_dance(uint16_t keycode, keyrecord_t *record) {
142 uint16_t idx = keycode - QK_TAP_DANCE;
143 qk_tap_dance_action_t *action;
146 case QK_TAP_DANCE ... QK_TAP_DANCE_MAX:
147 if ((int16_t)idx > highest_td)
149 action = &tap_dance_actions[idx];
151 action->state.pressed = record->event.pressed;
152 if (record->event.pressed) {
153 action->state.keycode = keycode;
154 action->state.count++;
155 action->state.timer = timer_read();
156 #ifndef NO_ACTION_ONESHOT
157 action->state.oneshot_mods = get_oneshot_mods();
159 action->state.oneshot_mods = 0;
161 action->state.weak_mods = get_mods();
162 action->state.weak_mods |= get_weak_mods();
163 process_tap_dance_action_on_each_tap (action);
167 if (action->state.count && action->state.finished) {
168 reset_tap_dance (&action->state);
180 void matrix_scan_tap_dance () {
181 if (highest_td == -1)
183 uint16_t tap_user_defined;
185 for (uint8_t i = 0; i <= highest_td; i++) {
186 qk_tap_dance_action_t *action = &tap_dance_actions[i];
187 if(action->custom_tapping_term > 0 ) {
188 tap_user_defined = action->custom_tapping_term;
191 tap_user_defined = TAPPING_TERM;
193 if (action->state.count && timer_elapsed (action->state.timer) > tap_user_defined) {
194 process_tap_dance_action_on_dance_finished (action);
195 reset_tap_dance (&action->state);
200 void reset_tap_dance (qk_tap_dance_state_t *state) {
201 qk_tap_dance_action_t *action;
206 action = &tap_dance_actions[state->keycode - QK_TAP_DANCE];
208 process_tap_dance_action_on_reset (action);
211 state->interrupted = false;
212 state->finished = false;
213 state->interrupting_keycode = 0;