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