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