Fix typo in uart.c backport and add 32A "support" (#8219)
[jackhill/qmk/firmware.git] / tmk_core / common / action_tapping.c
1 #include <stdint.h>
2 #include <stdbool.h>
3 #include "action.h"
4 #include "action_layer.h"
5 #include "action_tapping.h"
6 #include "keycode.h"
7 #include "timer.h"
8
9 #ifdef DEBUG_ACTION
10 # include "debug.h"
11 #else
12 # include "nodebug.h"
13 #endif
14
15 #ifndef NO_ACTION_TAPPING
16
17 # define IS_TAPPING() !IS_NOEVENT(tapping_key.event)
18 # define IS_TAPPING_PRESSED() (IS_TAPPING() && tapping_key.event.pressed)
19 # define IS_TAPPING_RELEASED() (IS_TAPPING() && !tapping_key.event.pressed)
20 # define IS_TAPPING_KEY(k) (IS_TAPPING() && KEYEQ(tapping_key.event.key, (k)))
21
22 __attribute__((weak)) uint16_t get_tapping_term(uint16_t keycode) { return TAPPING_TERM; }
23
24 # ifdef TAPPING_TERM_PER_KEY
25 # define WITHIN_TAPPING_TERM(e) (TIMER_DIFF_16(e.time, tapping_key.event.time) < get_tapping_term(get_event_keycode(tapping_key.event)))
26 # else
27 # define WITHIN_TAPPING_TERM(e) (TIMER_DIFF_16(e.time, tapping_key.event.time) < TAPPING_TERM)
28 # endif
29
30 # ifdef TAPPING_FORCE_HOLD_PER_KEY
31 __attribute__((weak)) bool get_tapping_force_hold(uint16_t keycode, keyrecord_t *record) { return false; }
32 # endif
33
34 # ifdef PERMISSIVE_HOLD_PER_KEY
35 __attribute__((weak)) bool get_permissive_hold(uint16_t keycode, keyrecord_t *record) { return false; }
36 # endif
37
38 static keyrecord_t tapping_key = {};
39 static keyrecord_t waiting_buffer[WAITING_BUFFER_SIZE] = {};
40 static uint8_t waiting_buffer_head = 0;
41 static uint8_t waiting_buffer_tail = 0;
42
43 static bool process_tapping(keyrecord_t *record);
44 static bool waiting_buffer_enq(keyrecord_t record);
45 static void waiting_buffer_clear(void);
46 static bool waiting_buffer_typed(keyevent_t event);
47 static bool waiting_buffer_has_anykey_pressed(void);
48 static void waiting_buffer_scan_tap(void);
49 static void debug_tapping_key(void);
50 static void debug_waiting_buffer(void);
51
52 /** \brief Action Tapping Process
53 *
54 * FIXME: Needs doc
55 */
56 void action_tapping_process(keyrecord_t record) {
57 if (process_tapping(&record)) {
58 if (!IS_NOEVENT(record.event)) {
59 debug("processed: ");
60 debug_record(record);
61 debug("\n");
62 }
63 } else {
64 if (!waiting_buffer_enq(record)) {
65 // clear all in case of overflow.
66 debug("OVERFLOW: CLEAR ALL STATES\n");
67 clear_keyboard();
68 waiting_buffer_clear();
69 tapping_key = (keyrecord_t){};
70 }
71 }
72
73 // process waiting_buffer
74 if (!IS_NOEVENT(record.event) && waiting_buffer_head != waiting_buffer_tail) {
75 debug("---- action_exec: process waiting_buffer -----\n");
76 }
77 for (; waiting_buffer_tail != waiting_buffer_head; waiting_buffer_tail = (waiting_buffer_tail + 1) % WAITING_BUFFER_SIZE) {
78 if (process_tapping(&waiting_buffer[waiting_buffer_tail])) {
79 debug("processed: waiting_buffer[");
80 debug_dec(waiting_buffer_tail);
81 debug("] = ");
82 debug_record(waiting_buffer[waiting_buffer_tail]);
83 debug("\n\n");
84 } else {
85 break;
86 }
87 }
88 if (!IS_NOEVENT(record.event)) {
89 debug("\n");
90 }
91 }
92
93 /** \brief Tapping
94 *
95 * Rule: Tap key is typed(pressed and released) within TAPPING_TERM.
96 * (without interfering by typing other key)
97 */
98 /* return true when key event is processed or consumed. */
99 bool process_tapping(keyrecord_t *keyp) {
100 keyevent_t event = keyp->event;
101
102 // if tapping
103 if (IS_TAPPING_PRESSED()) {
104 if (WITHIN_TAPPING_TERM(event)) {
105 if (tapping_key.tap.count == 0) {
106 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
107 // first tap!
108 debug("Tapping: First tap(0->1).\n");
109 tapping_key.tap.count = 1;
110 debug_tapping_key();
111 process_record(&tapping_key);
112
113 // copy tapping state
114 keyp->tap = tapping_key.tap;
115 // enqueue
116 return false;
117 }
118 /* Process a key typed within TAPPING_TERM
119 * This can register the key before settlement of tapping,
120 * useful for long TAPPING_TERM but may prevent fast typing.
121 */
122 # if defined(TAPPING_TERM_PER_KEY) || (TAPPING_TERM >= 500) || defined(PERMISSIVE_HOLD) || defined(PERMISSIVE_HOLD_PER_KEY)
123 else if (
124 # ifdef TAPPING_TERM_PER_KEY
125 (get_tapping_term(get_event_keycode(tapping_key.event)) >= 500) &&
126 # endif
127 # ifdef PERMISSIVE_HOLD_PER_KEY
128 !get_permissive_hold(get_event_keycode(tapping_key.event), keyp) &&
129 # endif
130 IS_RELEASED(event) && waiting_buffer_typed(event)) {
131 debug("Tapping: End. No tap. Interfered by typing key\n");
132 process_record(&tapping_key);
133 tapping_key = (keyrecord_t){};
134 debug_tapping_key();
135 // enqueue
136 return false;
137 }
138 # endif
139 /* Process release event of a key pressed before tapping starts
140 * Without this unexpected repeating will occur with having fast repeating setting
141 * https://github.com/tmk/tmk_keyboard/issues/60
142 */
143 else if (IS_RELEASED(event) && !waiting_buffer_typed(event)) {
144 // Modifier should be retained till end of this tapping.
145 action_t action = layer_switch_get_action(event.key);
146 switch (action.kind.id) {
147 case ACT_LMODS:
148 case ACT_RMODS:
149 if (action.key.mods && !action.key.code) return false;
150 if (IS_MOD(action.key.code)) return false;
151 break;
152 case ACT_LMODS_TAP:
153 case ACT_RMODS_TAP:
154 if (action.key.mods && keyp->tap.count == 0) return false;
155 if (IS_MOD(action.key.code)) return false;
156 break;
157 }
158 // Release of key should be process immediately.
159 debug("Tapping: release event of a key pressed before tapping\n");
160 process_record(keyp);
161 return true;
162 } else {
163 // set interrupted flag when other key preesed during tapping
164 if (event.pressed) {
165 tapping_key.tap.interrupted = true;
166 }
167 // enqueue
168 return false;
169 }
170 }
171 // tap_count > 0
172 else {
173 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
174 debug("Tapping: Tap release(");
175 debug_dec(tapping_key.tap.count);
176 debug(")\n");
177 keyp->tap = tapping_key.tap;
178 process_record(keyp);
179 tapping_key = *keyp;
180 debug_tapping_key();
181 return true;
182 } else if (is_tap_key(event.key) && event.pressed) {
183 if (tapping_key.tap.count > 1) {
184 debug("Tapping: Start new tap with releasing last tap(>1).\n");
185 // unregister key
186 process_record(&(keyrecord_t){.tap = tapping_key.tap, .event.key = tapping_key.event.key, .event.time = event.time, .event.pressed = false});
187 } else {
188 debug("Tapping: Start while last tap(1).\n");
189 }
190 tapping_key = *keyp;
191 waiting_buffer_scan_tap();
192 debug_tapping_key();
193 return true;
194 } else {
195 if (!IS_NOEVENT(event)) {
196 debug("Tapping: key event while last tap(>0).\n");
197 }
198 process_record(keyp);
199 return true;
200 }
201 }
202 }
203 // after TAPPING_TERM
204 else {
205 if (tapping_key.tap.count == 0) {
206 debug("Tapping: End. Timeout. Not tap(0): ");
207 debug_event(event);
208 debug("\n");
209 process_record(&tapping_key);
210 tapping_key = (keyrecord_t){};
211 debug_tapping_key();
212 return false;
213 } else {
214 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
215 debug("Tapping: End. last timeout tap release(>0).");
216 keyp->tap = tapping_key.tap;
217 process_record(keyp);
218 tapping_key = (keyrecord_t){};
219 return true;
220 } else if (is_tap_key(event.key) && event.pressed) {
221 if (tapping_key.tap.count > 1) {
222 debug("Tapping: Start new tap with releasing last timeout tap(>1).\n");
223 // unregister key
224 process_record(&(keyrecord_t){.tap = tapping_key.tap, .event.key = tapping_key.event.key, .event.time = event.time, .event.pressed = false});
225 } else {
226 debug("Tapping: Start while last timeout tap(1).\n");
227 }
228 tapping_key = *keyp;
229 waiting_buffer_scan_tap();
230 debug_tapping_key();
231 return true;
232 } else {
233 if (!IS_NOEVENT(event)) {
234 debug("Tapping: key event while last timeout tap(>0).\n");
235 }
236 process_record(keyp);
237 return true;
238 }
239 }
240 }
241 } else if (IS_TAPPING_RELEASED()) {
242 if (WITHIN_TAPPING_TERM(event)) {
243 if (event.pressed) {
244 if (IS_TAPPING_KEY(event.key)) {
245 //# ifndef TAPPING_FORCE_HOLD
246 # if !defined(TAPPING_FORCE_HOLD) || defined(TAPPING_FORCE_HOLD_PER_KEY)
247 if (
248 # ifdef TAPPING_FORCE_HOLD_PER_KEY
249 !get_tapping_force_hold(get_event_keycode(tapping_key.event), keyp) &&
250 # endif
251 !tapping_key.tap.interrupted && tapping_key.tap.count > 0) {
252 // sequential tap.
253 keyp->tap = tapping_key.tap;
254 if (keyp->tap.count < 15) keyp->tap.count += 1;
255 debug("Tapping: Tap press(");
256 debug_dec(keyp->tap.count);
257 debug(")\n");
258 process_record(keyp);
259 tapping_key = *keyp;
260 debug_tapping_key();
261 return true;
262 }
263 # endif
264 // FIX: start new tap again
265 tapping_key = *keyp;
266 return true;
267 } else if (is_tap_key(event.key)) {
268 // Sequential tap can be interfered with other tap key.
269 debug("Tapping: Start with interfering other tap.\n");
270 tapping_key = *keyp;
271 waiting_buffer_scan_tap();
272 debug_tapping_key();
273 return true;
274 } else {
275 // should none in buffer
276 // FIX: interrupted when other key is pressed
277 tapping_key.tap.interrupted = true;
278 process_record(keyp);
279 return true;
280 }
281 } else {
282 if (!IS_NOEVENT(event)) debug("Tapping: other key just after tap.\n");
283 process_record(keyp);
284 return true;
285 }
286 } else {
287 // FIX: process_action here?
288 // timeout. no sequential tap.
289 debug("Tapping: End(Timeout after releasing last tap): ");
290 debug_event(event);
291 debug("\n");
292 tapping_key = (keyrecord_t){};
293 debug_tapping_key();
294 return false;
295 }
296 }
297 // not tapping state
298 else {
299 if (event.pressed && is_tap_key(event.key)) {
300 debug("Tapping: Start(Press tap key).\n");
301 tapping_key = *keyp;
302 process_record_tap_hint(&tapping_key);
303 waiting_buffer_scan_tap();
304 debug_tapping_key();
305 return true;
306 } else {
307 process_record(keyp);
308 return true;
309 }
310 }
311 }
312
313 /** \brief Waiting buffer enq
314 *
315 * FIXME: Needs docs
316 */
317 bool waiting_buffer_enq(keyrecord_t record) {
318 if (IS_NOEVENT(record.event)) {
319 return true;
320 }
321
322 if ((waiting_buffer_head + 1) % WAITING_BUFFER_SIZE == waiting_buffer_tail) {
323 debug("waiting_buffer_enq: Over flow.\n");
324 return false;
325 }
326
327 waiting_buffer[waiting_buffer_head] = record;
328 waiting_buffer_head = (waiting_buffer_head + 1) % WAITING_BUFFER_SIZE;
329
330 debug("waiting_buffer_enq: ");
331 debug_waiting_buffer();
332 return true;
333 }
334
335 /** \brief Waiting buffer clear
336 *
337 * FIXME: Needs docs
338 */
339 void waiting_buffer_clear(void) {
340 waiting_buffer_head = 0;
341 waiting_buffer_tail = 0;
342 }
343
344 /** \brief Waiting buffer typed
345 *
346 * FIXME: Needs docs
347 */
348 bool waiting_buffer_typed(keyevent_t event) {
349 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
350 if (KEYEQ(event.key, waiting_buffer[i].event.key) && event.pressed != waiting_buffer[i].event.pressed) {
351 return true;
352 }
353 }
354 return false;
355 }
356
357 /** \brief Waiting buffer has anykey pressed
358 *
359 * FIXME: Needs docs
360 */
361 __attribute__((unused)) bool waiting_buffer_has_anykey_pressed(void) {
362 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
363 if (waiting_buffer[i].event.pressed) return true;
364 }
365 return false;
366 }
367
368 /** \brief Scan buffer for tapping
369 *
370 * FIXME: Needs docs
371 */
372 void waiting_buffer_scan_tap(void) {
373 // tapping already is settled
374 if (tapping_key.tap.count > 0) return;
375 // invalid state: tapping_key released && tap.count == 0
376 if (!tapping_key.event.pressed) return;
377
378 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
379 if (IS_TAPPING_KEY(waiting_buffer[i].event.key) && !waiting_buffer[i].event.pressed && WITHIN_TAPPING_TERM(waiting_buffer[i].event)) {
380 tapping_key.tap.count = 1;
381 waiting_buffer[i].tap.count = 1;
382 process_record(&tapping_key);
383
384 debug("waiting_buffer_scan_tap: found at [");
385 debug_dec(i);
386 debug("]\n");
387 debug_waiting_buffer();
388 return;
389 }
390 }
391 }
392
393 /** \brief Tapping key debug print
394 *
395 * FIXME: Needs docs
396 */
397 static void debug_tapping_key(void) {
398 debug("TAPPING_KEY=");
399 debug_record(tapping_key);
400 debug("\n");
401 }
402
403 /** \brief Waiting buffer debug print
404 *
405 * FIXME: Needs docs
406 */
407 static void debug_waiting_buffer(void) {
408 debug("{ ");
409 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
410 debug("[");
411 debug_dec(i);
412 debug("]=");
413 debug_record(waiting_buffer[i]);
414 debug(" ");
415 }
416 debug("}\n");
417 }
418
419 #endif