]> git.donarmstrong.com Git - tmk_firmware.git/blob - common/action_tapping.c
Merge branch 'action_tapping'
[tmk_firmware.git] / common / action_tapping.c
1 #include <stdint.h>
2 #include <stdbool.h>
3 #include "action.h"
4 #include "action_tapping.h"
5 #include "timer.h"
6 #include "debug.h"
7
8
9 #ifndef NO_ACTION_TAPPING
10
11 #define IS_TAPPING()            !IS_NOEVENT(tapping_key.event)
12 #define IS_TAPPING_PRESSED()    (IS_TAPPING() && tapping_key.event.pressed)
13 #define IS_TAPPING_RELEASED()   (IS_TAPPING() && !tapping_key.event.pressed)
14 #define IS_TAPPING_KEY(k)       (IS_TAPPING() && KEYEQ(tapping_key.event.key, (k)))
15 #define WITHIN_TAPPING_TERM(e)  (TIMER_DIFF_16(e.time, tapping_key.event.time) < TAPPING_TERM)
16
17
18 static keyrecord_t tapping_key = {};
19 static keyrecord_t waiting_buffer[WAITING_BUFFER_SIZE] = {};
20 static uint8_t waiting_buffer_head = 0;
21 static uint8_t waiting_buffer_tail = 0;
22
23 static bool process_tapping(keyrecord_t *record);
24 static bool waiting_buffer_enq(keyrecord_t record);
25 static void waiting_buffer_clear(void);
26 #if TAPPING_TERM >= 500
27 static bool waiting_buffer_typed(keyevent_t event);
28 #endif
29 static bool waiting_buffer_has_anykey_pressed(void);
30 static void waiting_buffer_scan_tap(void);
31 static void debug_tapping_key(void);
32 static void debug_waiting_buffer(void);
33
34
35 void action_tapping_process(keyrecord_t record)
36 {
37     if (process_tapping(&record)) {
38         if (!IS_NOEVENT(record.event)) {
39             debug("processed: "); debug_record(record); debug("\n");
40         }
41     } else {
42         if (!waiting_buffer_enq(record)) {
43             // clear all in case of overflow.
44             debug("OVERFLOW: CLEAR ALL STATES\n");
45             clear_keyboard();
46             waiting_buffer_clear();
47             tapping_key = (keyrecord_t){};
48         }
49     }
50
51     // process waiting_buffer
52     if (!IS_NOEVENT(record.event) && waiting_buffer_head != waiting_buffer_tail) {
53         debug("---- action_exec: process waiting_buffer -----\n");
54     }
55     for (; waiting_buffer_tail != waiting_buffer_head; waiting_buffer_tail = (waiting_buffer_tail + 1) % WAITING_BUFFER_SIZE) {
56         if (process_tapping(&waiting_buffer[waiting_buffer_tail])) {
57             debug("processed: waiting_buffer["); debug_dec(waiting_buffer_tail); debug("] = ");
58             debug_record(waiting_buffer[waiting_buffer_tail]); debug("\n\n");
59         } else {
60             break;
61         }
62     }
63     if (!IS_NOEVENT(record.event)) {
64         debug("\n");
65     }
66 }
67
68
69 /* Tapping
70  *
71  * Rule: Tap key is typed(pressed and released) within TAPPING_TERM.
72  *       (without interfering by typing other key)
73  */
74 /* return true when key event is processed or consumed. */
75 bool process_tapping(keyrecord_t *keyp)
76 {
77     keyevent_t event = keyp->event;
78
79     // if tapping
80     if (IS_TAPPING_PRESSED()) {
81         if (WITHIN_TAPPING_TERM(event)) {
82             if (tapping_key.tap.count == 0) {
83                 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
84                     // first tap!
85                     debug("Tapping: First tap(0->1).\n");
86                     tapping_key.tap.count = 1;
87                     tapping_key.tap.interrupted  = (waiting_buffer_has_anykey_pressed() ? true : false);
88                     debug_tapping_key();
89                     process_action(&tapping_key);
90
91                     // enqueue
92                     keyp->tap = tapping_key.tap;
93                     return false;
94                 }
95 #if TAPPING_TERM >= 500
96                 /* This can prevent from typing some tap keys in a row at a time. */
97                 else if (!event.pressed && waiting_buffer_typed(event)) {
98                     // other key typed. not tap.
99                     debug("Tapping: End. No tap. Interfered by typing key\n");
100                     process_action(&tapping_key);
101                     tapping_key = (keyrecord_t){};
102                     debug_tapping_key();
103
104                     // enqueue
105                     return false;
106                 }
107 #endif
108                 else {
109                     // other key events shall be enq'd till tapping state settles.
110                     return false;
111                 }
112             }
113             // tap_count > 0
114             else {
115                 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
116                     debug("Tapping: Tap release("); debug_dec(tapping_key.tap.count); debug(")\n");
117                     keyp->tap = tapping_key.tap;
118                     process_action(keyp);
119                     tapping_key = *keyp;
120                     debug_tapping_key();
121                     return true;
122                 }
123                 else if (is_tap_key(keyp->event.key) && event.pressed) {
124                     if (tapping_key.tap.count > 1) {
125                         debug("Tapping: Start new tap with releasing last tap(>1).\n");
126                         // unregister key
127                         process_action(&(keyrecord_t){
128                                 .tap = tapping_key.tap,
129                                 .event.key = tapping_key.event.key,
130                                 .event.time = event.time,
131                                 .event.pressed = false
132                         });
133                     } else {
134                         debug("Tapping: Start while last tap(1).\n");
135                     }
136                     tapping_key = *keyp;
137                     waiting_buffer_scan_tap();
138                     debug_tapping_key();
139                     return true;
140                 }
141                 else {
142                     if (!IS_NOEVENT(keyp->event)) {
143                         debug("Tapping: key event while last tap(>0).\n");
144                     }
145                     process_action(keyp);
146                     return true;
147                 }
148             }
149         }
150         // after TAPPING_TERM
151         else {
152             if (tapping_key.tap.count == 0) {
153                 debug("Tapping: End. Timeout. Not tap(0): ");
154                 debug_event(event); debug("\n");
155                 process_action(&tapping_key);
156                 tapping_key = (keyrecord_t){};
157                 debug_tapping_key();
158                 return false;
159             }  else {
160                 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
161                     debug("Tapping: End. last timeout tap release(>0).");
162                     keyp->tap = tapping_key.tap;
163                     process_action(keyp);
164                     tapping_key = (keyrecord_t){};
165                     return true;
166                 }
167                 else if (is_tap_key(keyp->event.key) && event.pressed) {
168                     if (tapping_key.tap.count > 1) {
169                         debug("Tapping: Start new tap with releasing last timeout tap(>1).\n");
170                         // unregister key
171                         process_action(&(keyrecord_t){
172                                 .tap = tapping_key.tap,
173                                 .event.key = tapping_key.event.key,
174                                 .event.time = event.time,
175                                 .event.pressed = false
176                         });
177                     } else {
178                         debug("Tapping: Start while last timeout tap(1).\n");
179                     }
180                     tapping_key = *keyp;
181                     waiting_buffer_scan_tap();
182                     debug_tapping_key();
183                     return true;
184                 }
185                 else {
186                     if (!IS_NOEVENT(keyp->event)) {
187                         debug("Tapping: key event while last timeout tap(>0).\n");
188                     }
189                     process_action(keyp);
190                     return true;
191                 }
192             }
193         }
194     } else if (IS_TAPPING_RELEASED()) {
195         if (WITHIN_TAPPING_TERM(event)) {
196             if (tapping_key.tap.count > 0 && IS_TAPPING_KEY(event.key) && event.pressed) {
197                 // sequential tap.
198                 keyp->tap = tapping_key.tap;
199                 keyp->tap.count += 1;
200                 debug("Tapping: Tap press("); debug_dec(keyp->tap.count); debug(")\n");
201                 process_action(keyp);
202                 tapping_key = *keyp;
203                 debug_tapping_key();
204                 return true;
205             } else if (event.pressed && is_tap_key(event.key)) {
206                 // Sequential tap can be interfered with other tap key.
207                 debug("Tapping: Start with interfering other tap.\n");
208                 tapping_key = *keyp;
209                 waiting_buffer_scan_tap();
210                 debug_tapping_key();
211                 return true;
212             } else {
213                 if (!IS_NOEVENT(keyp->event)) debug("Tapping: other key just after tap.\n");
214                 process_action(keyp);
215                 return true;
216             }
217         } else {
218             // timeout. no sequential tap.
219             debug("Tapping: End(Timeout after releasing last tap): ");
220             debug_event(event); debug("\n");
221             tapping_key = (keyrecord_t){};
222             debug_tapping_key();
223             return false;
224         }
225     }
226     // not tapping satate
227     else {
228         if (event.pressed && is_tap_key(event.key)) {
229             debug("Tapping: Start(Press tap key).\n");
230             tapping_key = *keyp;
231             waiting_buffer_scan_tap();
232             debug_tapping_key();
233             return true;
234         } else {
235             process_action(keyp);
236             return true;
237         }
238     }
239 }
240
241
242 /*
243  * Waiting buffer
244  */
245 bool waiting_buffer_enq(keyrecord_t record)
246 {
247     if (IS_NOEVENT(record.event)) {
248         return true;
249     }
250
251     if ((waiting_buffer_head + 1) % WAITING_BUFFER_SIZE == waiting_buffer_tail) {
252         debug("waiting_buffer_enq: Over flow.\n");
253         return false;
254     }
255
256     waiting_buffer[waiting_buffer_head] = record;
257     waiting_buffer_head = (waiting_buffer_head + 1) % WAITING_BUFFER_SIZE;
258
259     debug("waiting_buffer_enq: "); debug_waiting_buffer();
260     return true;
261 }
262
263 void waiting_buffer_clear(void)
264 {
265     waiting_buffer_head = 0;
266     waiting_buffer_tail = 0;
267 }
268
269 #if TAPPING_TERM >= 500
270 bool waiting_buffer_typed(keyevent_t event)
271 {
272     for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
273         if (KEYEQ(event.key, waiting_buffer[i].event.key) && event.pressed !=  waiting_buffer[i].event.pressed) {
274             return true;
275         }
276     }
277     return false;
278 }
279 #endif
280
281 bool waiting_buffer_has_anykey_pressed(void)
282 {
283     for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
284         if (waiting_buffer[i].event.pressed) return true;
285     }
286     return false;
287 }
288
289 /* scan buffer for tapping */
290 void waiting_buffer_scan_tap(void)
291 {
292     // tapping already is settled
293     if (tapping_key.tap.count > 0) return;
294     // invalid state: tapping_key released && tap.count == 0
295     if (!tapping_key.event.pressed) return;
296
297     for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
298         if (IS_TAPPING_KEY(waiting_buffer[i].event.key) &&
299                 !waiting_buffer[i].event.pressed &&
300                 WITHIN_TAPPING_TERM(waiting_buffer[i].event)) {
301             tapping_key.tap.count = 1;
302             waiting_buffer[i].tap.count = 1;
303             process_action(&tapping_key);
304
305             debug("waiting_buffer_scan_tap: found at ["); debug_dec(i); debug("]\n");
306             debug_waiting_buffer();
307             return;
308         }
309     }
310 }
311
312
313 /*
314  * debug print
315  */
316 static void debug_tapping_key(void)
317 {
318     debug("TAPPING_KEY="); debug_record(tapping_key); debug("\n");
319 }
320
321 static void debug_waiting_buffer(void)
322 {
323     debug("{ ");
324     for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
325         debug("["); debug_dec(i); debug("]="); debug_record(waiting_buffer[i]); debug(" ");
326     }
327     debug("}\n");
328 }
329
330 #endif