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 process_tap_dance_action_on_dance_finished (action);
135 reset_tap_dance (&action->state);
140 bool process_tap_dance(uint16_t keycode, keyrecord_t *record) {
141 uint16_t idx = keycode - QK_TAP_DANCE;
142 qk_tap_dance_action_t *action;
145 case QK_TAP_DANCE ... QK_TAP_DANCE_MAX:
146 if ((int16_t)idx > highest_td)
148 action = &tap_dance_actions[idx];
150 action->state.pressed = record->event.pressed;
151 if (record->event.pressed) {
152 action->state.keycode = keycode;
153 action->state.count++;
154 action->state.timer = timer_read();
155 #ifndef NO_ACTION_ONESHOT
156 action->state.oneshot_mods = get_oneshot_mods();
158 action->state.oneshot_mods = 0;
160 action->state.weak_mods = get_mods();
161 action->state.weak_mods |= get_weak_mods();
162 process_tap_dance_action_on_each_tap (action);
166 if (action->state.count && action->state.finished) {
167 reset_tap_dance (&action->state);
179 void matrix_scan_tap_dance () {
180 if (highest_td == -1)
182 uint16_t tap_user_defined;
184 for (uint8_t i = 0; i <= highest_td; i++) {
185 qk_tap_dance_action_t *action = &tap_dance_actions[i];
186 if(action->custom_tapping_term > 0 ) {
187 tap_user_defined = action->custom_tapping_term;
190 tap_user_defined = TAPPING_TERM;
192 if (action->state.count && timer_elapsed (action->state.timer) > tap_user_defined) {
193 process_tap_dance_action_on_dance_finished (action);
194 reset_tap_dance (&action->state);
199 void reset_tap_dance (qk_tap_dance_state_t *state) {
200 qk_tap_dance_action_t *action;
205 action = &tap_dance_actions[state->keycode - QK_TAP_DANCE];
207 process_tap_dance_action_on_reset (action);
210 state->interrupted = false;
211 state->finished = false;