]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/process_keycode/process_tap_dance.c
Merge pull request #4453 from drashna/fix_ucis
[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       process_tap_dance_action_on_dance_finished (action);
135       reset_tap_dance (&action->state);
136     }
137   }
138 }
139
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;
143
144   switch(keycode) {
145   case QK_TAP_DANCE ... QK_TAP_DANCE_MAX:
146     if ((int16_t)idx > highest_td)
147       highest_td = idx;
148     action = &tap_dance_actions[idx];
149
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();
157 #else
158       action->state.oneshot_mods = 0;
159 #endif
160       action->state.weak_mods = get_mods();
161       action->state.weak_mods |= get_weak_mods();
162       process_tap_dance_action_on_each_tap (action);
163
164       last_td = keycode;
165     } else {
166       if (action->state.count && action->state.finished) {
167         reset_tap_dance (&action->state);
168       }
169     }
170
171     break;
172   }
173
174   return true;
175 }
176
177
178
179 void matrix_scan_tap_dance () {
180   if (highest_td == -1)
181     return;
182   uint16_t tap_user_defined;
183
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;
188     }
189     else{
190       tap_user_defined = TAPPING_TERM;
191     }
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);
195     }
196   }
197 }
198
199 void reset_tap_dance (qk_tap_dance_state_t *state) {
200   qk_tap_dance_action_t *action;
201
202   if (state->pressed)
203     return;
204
205   action = &tap_dance_actions[state->keycode - QK_TAP_DANCE];
206
207   process_tap_dance_action_on_reset (action);
208
209   state->count = 0;
210   state->interrupted = false;
211   state->finished = false;
212   last_td = 0;
213 }