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