process_tap_dance.c
6724 bytes
1/* Copyright 2016 Jack Humbert
2 *
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.
7 *
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.
12 *
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/>.
15 */
16
17#include "process_tap_dance.h"
18#include "quantum.h"
19#include "action_layer.h"
20#include "action_tapping.h"
21#include "action_util.h"
22#include "timer.h"
23#include "wait.h"
24#include "keymap_introspection.h"
25
26static uint16_t active_td;
27static uint16_t last_tap_time;
28
29void tap_dance_pair_on_each_tap(tap_dance_state_t *state, void *user_data) {
30 tap_dance_pair_t *pair = (tap_dance_pair_t *)user_data;
31
32 if (state->count == 2) {
33 register_code16(pair->kc2);
34 state->finished = true;
35 }
36}
37
38void tap_dance_pair_finished(tap_dance_state_t *state, void *user_data) {
39 tap_dance_pair_t *pair = (tap_dance_pair_t *)user_data;
40
41 register_code16(pair->kc1);
42}
43
44void tap_dance_pair_reset(tap_dance_state_t *state, void *user_data) {
45 tap_dance_pair_t *pair = (tap_dance_pair_t *)user_data;
46
47 if (state->count == 1) {
48 wait_ms(TAP_CODE_DELAY);
49 unregister_code16(pair->kc1);
50 } else if (state->count == 2) {
51 unregister_code16(pair->kc2);
52 }
53}
54
55void tap_dance_dual_role_on_each_tap(tap_dance_state_t *state, void *user_data) {
56 tap_dance_dual_role_t *pair = (tap_dance_dual_role_t *)user_data;
57
58 if (state->count == 2) {
59 layer_move(pair->layer);
60 state->finished = true;
61 }
62}
63
64void tap_dance_dual_role_finished(tap_dance_state_t *state, void *user_data) {
65 tap_dance_dual_role_t *pair = (tap_dance_dual_role_t *)user_data;
66
67 if (state->count == 1) {
68 register_code16(pair->kc);
69 } else if (state->count == 2) {
70 pair->layer_function(pair->layer);
71 }
72}
73
74void tap_dance_dual_role_reset(tap_dance_state_t *state, void *user_data) {
75 tap_dance_dual_role_t *pair = (tap_dance_dual_role_t *)user_data;
76
77 if (state->count == 1) {
78 wait_ms(TAP_CODE_DELAY);
79 unregister_code16(pair->kc);
80 }
81}
82
83static inline void _process_tap_dance_action_fn(tap_dance_state_t *state, void *user_data, tap_dance_user_fn_t fn) {
84 if (fn) {
85 fn(state, user_data);
86 }
87}
88
89static inline void process_tap_dance_action_on_each_tap(tap_dance_action_t *action) {
90 action->state.count++;
91 action->state.weak_mods = get_mods();
92 action->state.weak_mods |= get_weak_mods();
93#ifndef NO_ACTION_ONESHOT
94 action->state.oneshot_mods = get_oneshot_mods();
95#endif
96 _process_tap_dance_action_fn(&action->state, action->user_data, action->fn.on_each_tap);
97}
98
99static inline void process_tap_dance_action_on_each_release(tap_dance_action_t *action) {
100 _process_tap_dance_action_fn(&action->state, action->user_data, action->fn.on_each_release);
101}
102
103static inline void process_tap_dance_action_on_reset(tap_dance_action_t *action) {
104 _process_tap_dance_action_fn(&action->state, action->user_data, action->fn.on_reset);
105 del_weak_mods(action->state.weak_mods);
106#ifndef NO_ACTION_ONESHOT
107 del_mods(action->state.oneshot_mods);
108#endif
109 send_keyboard_report();
110 action->state = (const tap_dance_state_t){0};
111}
112
113static inline void process_tap_dance_action_on_dance_finished(tap_dance_action_t *action) {
114 if (!action->state.finished) {
115 action->state.finished = true;
116 add_weak_mods(action->state.weak_mods);
117#ifndef NO_ACTION_ONESHOT
118 add_mods(action->state.oneshot_mods);
119#endif
120 send_keyboard_report();
121 _process_tap_dance_action_fn(&action->state, action->user_data, action->fn.on_dance_finished);
122 }
123 active_td = 0;
124 if (!action->state.pressed) {
125 // There will not be a key release event, so reset now.
126 process_tap_dance_action_on_reset(action);
127 }
128}
129
130bool preprocess_tap_dance(uint16_t keycode, keyrecord_t *record) {
131 tap_dance_action_t *action;
132
133 if (!record->event.pressed) return false;
134
135 if (!active_td || keycode == active_td) return false;
136
137 action = tap_dance_get(QK_TAP_DANCE_GET_INDEX(active_td));
138 action->state.interrupted = true;
139 action->state.interrupting_keycode = keycode;
140 process_tap_dance_action_on_dance_finished(action);
141
142 // Tap dance actions can leave some weak mods active (e.g., if the tap dance is mapped to a keycode with
143 // modifiers), but these weak mods should not affect the keypress which interrupted the tap dance.
144 clear_weak_mods();
145
146 // Signal that a tap dance has been finished due to being interrupted,
147 // therefore the keymap lookup for the currently processed event needs to
148 // be repeated with the current layer state that might have been updated by
149 // the finished tap dance.
150 return true;
151}
152
153bool process_tap_dance(uint16_t keycode, keyrecord_t *record) {
154 int td_index;
155 tap_dance_action_t *action;
156
157 switch (keycode) {
158 case QK_TAP_DANCE ... QK_TAP_DANCE_MAX:
159 td_index = QK_TAP_DANCE_GET_INDEX(keycode);
160 if (td_index >= tap_dance_count()) {
161 return false;
162 }
163 action = tap_dance_get(td_index);
164
165 action->state.pressed = record->event.pressed;
166 if (record->event.pressed) {
167 last_tap_time = timer_read();
168 process_tap_dance_action_on_each_tap(action);
169 active_td = action->state.finished ? 0 : keycode;
170 } else {
171 process_tap_dance_action_on_each_release(action);
172 if (action->state.finished) {
173 process_tap_dance_action_on_reset(action);
174 if (active_td == keycode) {
175 active_td = 0;
176 }
177 }
178 }
179
180 break;
181 }
182
183 return true;
184}
185
186void tap_dance_task(void) {
187 tap_dance_action_t *action;
188
189 if (!active_td || timer_elapsed(last_tap_time) <= GET_TAPPING_TERM(active_td, &(keyrecord_t){})) return;
190
191 action = tap_dance_get(QK_TAP_DANCE_GET_INDEX(active_td));
192 if (!action->state.interrupted) {
193 process_tap_dance_action_on_dance_finished(action);
194 }
195}
196
197void reset_tap_dance(tap_dance_state_t *state) {
198 active_td = 0;
199 process_tap_dance_action_on_reset((tap_dance_action_t *)state);
200}