]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/process_keycode/process_tap_dance.c
Usbasploader bootloader option addition (#6304)
[qmk_firmware.git] / quantum / process_keycode / process_tap_dance.c
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 #include "quantum.h"
17 #include "action_tapping.h"
18
19 #ifndef TAPPING_TERM
20 #define TAPPING_TERM 200
21 #endif
22
23 #ifndef NO_ACTION_ONESHOT
24 uint8_t get_oneshot_mods(void);
25 #endif
26
27 static uint16_t last_td;
28 static int8_t highest_td = -1;
29
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;
32
33   if (state->count == 2) {
34     register_code16 (pair->kc2);
35     state->finished = true;
36   }
37 }
38
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;
41
42   if (state->count == 1) {
43     register_code16 (pair->kc1);
44   } else if (state->count == 2) {
45     register_code16 (pair->kc2);
46   }
47 }
48
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;
51
52   if (state->count == 1) {
53     unregister_code16 (pair->kc1);
54   } else if (state->count == 2) {
55     unregister_code16 (pair->kc2);
56   }
57 }
58
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;
61
62   if (state->count == 2) {
63     layer_move (pair->layer);
64     state->finished = true;
65   }
66 }
67
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;
70
71   if (state->count == 1) {
72     register_code16 (pair->kc);
73   } else if (state->count == 2) {
74     layer_move (pair->layer);
75   }
76 }
77
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;
80
81   if (state->count == 1) {
82     unregister_code16 (pair->kc);
83   }
84 }
85
86 static inline void _process_tap_dance_action_fn (qk_tap_dance_state_t *state,
87                                                  void *user_data,
88                                                  qk_tap_dance_user_fn_t fn)
89 {
90   if (fn) {
91     fn(state, user_data);
92   }
93 }
94
95 static inline void process_tap_dance_action_on_each_tap (qk_tap_dance_action_t *action)
96 {
97   _process_tap_dance_action_fn (&action->state, action->user_data, action->fn.on_each_tap);
98 }
99
100 static inline void process_tap_dance_action_on_dance_finished (qk_tap_dance_action_t *action)
101 {
102   if (action->state.finished)
103     return;
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);
109 }
110
111 static inline void process_tap_dance_action_on_reset (qk_tap_dance_action_t *action)
112 {
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();
117 }
118
119 void preprocess_tap_dance(uint16_t keycode, keyrecord_t *record) {
120   qk_tap_dance_action_t *action;
121
122   if (!record->event.pressed)
123     return;
124
125   if (highest_td == -1)
126     return;
127
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)
132         continue;
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);
137     }
138   }
139 }
140
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;
144
145   switch(keycode) {
146   case QK_TAP_DANCE ... QK_TAP_DANCE_MAX:
147     if ((int16_t)idx > highest_td)
148       highest_td = idx;
149     action = &tap_dance_actions[idx];
150
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();
158 #else
159       action->state.oneshot_mods = 0;
160 #endif
161       action->state.weak_mods = get_mods();
162       action->state.weak_mods |= get_weak_mods();
163       process_tap_dance_action_on_each_tap (action);
164
165       last_td = keycode;
166     } else {
167       if (action->state.count && action->state.finished) {
168         reset_tap_dance (&action->state);
169       }
170     }
171
172     break;
173   }
174
175   return true;
176 }
177
178
179
180 void matrix_scan_tap_dance () {
181   if (highest_td == -1)
182     return;
183   uint16_t tap_user_defined;
184
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;
189     }
190     else{
191       tap_user_defined = TAPPING_TERM;
192     }
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);
196     }
197   }
198 }
199
200 void reset_tap_dance (qk_tap_dance_state_t *state) {
201   qk_tap_dance_action_t *action;
202
203   if (state->pressed)
204     return;
205
206   action = &tap_dance_actions[state->keycode - QK_TAP_DANCE];
207
208   process_tap_dance_action_on_reset (action);
209
210   state->count = 0;
211   state->interrupted = false;
212   state->finished = false;
213   state->interrupting_keycode = 0;
214   last_td = 0;
215 }