]> git.donarmstrong.com Git - tmk_firmware.git/blob - common/action.c
Add layer stack
[tmk_firmware.git] / common / action.c
1 /*
2 Copyright 2012,2013 Jun Wako <wakojun@gmail.com>
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17 #include "host.h"
18 #include "timer.h"
19 #include "keymap.h"
20 #include "keycode.h"
21 #include "keyboard.h"
22 #include "mousekey.h"
23 #include "command.h"
24 #include "util.h"
25 #include "debug.h"
26 #include "action.h"
27
28
29 /* default layer indicates base layer */
30 uint8_t default_layer = 0;
31 /* current layer indicates active layer at this time */
32 uint8_t current_layer = 0;
33
34
35 static void process_action(keyrecord_t *record);
36 static bool process_tapping(keyrecord_t *record);
37 static void waiting_buffer_scan_tap(void);
38
39 static void debug_event(keyevent_t event);
40 static void debug_record(keyrecord_t record);
41 static void debug_action(action_t action);
42 static void debug_tapping_key(void);
43 static void debug_waiting_buffer(void);
44
45
46 /*
47  * Tapping
48  */
49 /* period of tapping(ms) */
50 #ifndef TAPPING_TERM
51 #define TAPPING_TERM    200
52 #endif
53
54 /* tap count needed for toggling a feature */
55 #ifndef TAPPING_TOGGLE
56 #define TAPPING_TOGGLE  5
57 #endif
58
59 /* stores a key event of current tap. */
60 static keyrecord_t tapping_key = {};
61
62 #define IS_TAPPING()            !IS_NOEVENT(tapping_key.event)
63 #define IS_TAPPING_PRESSED()    (IS_TAPPING() && tapping_key.event.pressed)
64 #define IS_TAPPING_RELEASED()   (IS_TAPPING() && !tapping_key.event.pressed)
65 #define IS_TAPPING_KEY(k)       (IS_TAPPING() && KEYEQ(tapping_key.event.key, (k)))
66 #define WITHIN_TAPPING_TERM(e)  (TIMER_DIFF_16(e.time, tapping_key.event.time) < TAPPING_TERM)
67
68
69 /*
70  * Waiting buffer
71  *
72  * stores key events waiting for settling current tap.
73  */
74 #define WAITING_BUFFER_SIZE 8
75 static keyrecord_t waiting_buffer[WAITING_BUFFER_SIZE] = {};
76
77 /* point to empty cell to enq */
78 static uint8_t waiting_buffer_head = 0;
79
80 /* point to the oldest data cell to deq */
81 static uint8_t waiting_buffer_tail = 0;
82
83 static bool waiting_buffer_enq(keyrecord_t record)
84 {
85     if (IS_NOEVENT(record.event)) {
86         return true;
87     }
88
89     if ((waiting_buffer_head + 1) % WAITING_BUFFER_SIZE == waiting_buffer_tail) {
90         debug("waiting_buffer_enq: Over flow.\n");
91         return false;
92     }
93
94     waiting_buffer[waiting_buffer_head] = record;
95     waiting_buffer_head = (waiting_buffer_head + 1) % WAITING_BUFFER_SIZE;
96
97     debug("waiting_buffer_enq: "); debug_waiting_buffer();
98     return true;
99 }
100
101 static void waiting_buffer_clear(void)
102 {
103     waiting_buffer_head = 0;
104     waiting_buffer_tail = 0;
105 }
106
107 #if TAPPING_TERM >= 500
108 static bool waiting_buffer_typed(keyevent_t event)
109 {
110     for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
111         if (KEYEQ(event.key, waiting_buffer[i].event.key) && event.pressed !=  waiting_buffer[i].event.pressed) {
112             return true;
113         }
114     }
115     return false;
116 }
117 #endif
118
119 bool waiting_buffer_has_anykey_pressed(void)
120 {
121     for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
122         if (waiting_buffer[i].event.pressed) return true;
123     }
124     return false;
125 }
126
127
128 /* Oneshot modifier
129  *
130  * Problem: Want to capitalize like 'The' but the result tends to be 'THe'.
131  * Solution: Oneshot modifier have its effect on only one key coming next.
132  *           Tap Shift, then type 't', 'h' and 'e'. Not need to hold Shift key.
133  *
134  *  Hold:       works as normal modifier.
135  *  Tap:        one shot modifier.
136  *  2 Tap:      cancel one shot modifier.
137  *  5-Tap:      toggles enable/disable oneshot feature.
138  */
139 static struct {
140     uint8_t mods;
141     uint8_t time;
142     bool    ready;
143     bool    disabled;
144 }   oneshot_state;
145
146 static void oneshot_start(uint8_t mods, uint16_t time)
147 {
148     oneshot_state.mods = mods;
149     oneshot_state.time = time;
150     oneshot_state.ready = true;
151 }
152
153 static void oneshot_cancel(void)
154 {
155     oneshot_state.mods = 0;
156     oneshot_state.time = 0;
157     oneshot_state.ready = false;
158 }
159
160 static void oneshot_toggle(void)
161 {
162     oneshot_state.disabled = !oneshot_state.disabled;
163 }
164
165
166 /*
167  * Layer stack
168  */
169 #define LAYER_STACK_SIZE 8
170 typedef struct {
171     uint8_t layer:4;
172     uint8_t next:3;
173     bool    used;
174 } layer_item_t;
175
176 static uint8_t top_layer = 0;
177 // [0] is sentinel and not used. [0] is null item.
178 static layer_item_t layer_stack[LAYER_STACK_SIZE] = {};
179
180 static bool layer_push(uint8_t layer)
181 {
182     for (uint8_t i = 1; i < LAYER_STACK_SIZE; i++) {
183         if (!layer_stack[i].used) {
184             layer_stack[i] = (layer_item_t){ .layer = layer,
185                                               .next = top_layer,
186                                               .used = true };
187             top_layer = i;
188             return true;
189         }
190     }
191     return false;
192 }
193 static bool layer_pop(void)
194 {
195     if (layer_stack[top_layer].used) {
196         uint8_t popped = top_layer;
197         top_layer = layer_stack[popped].next;
198         layer_stack[popped] = (layer_item_t){};
199         return true;
200     }
201     return false;
202 }
203 static bool layer_remove(uint8_t layer)
204 {
205     if (layer_stack[top_layer].used && layer_stack[top_layer].layer == layer) {
206         layer_pop();
207         debug("layer_remove: top_layer\n");
208         return true;
209     }
210
211     for (uint8_t i = top_layer; layer_stack[i].used; i = layer_stack[i].next) {
212         debug("layer_remove: ["); debug_dec(i); debug("]");
213         debug_dec(layer_stack[i].layer); debug("\n");
214         uint8_t removed = layer_stack[i].next;
215         if (layer_stack[removed].used && layer_stack[removed].layer == layer) {
216             layer_stack[i].next = layer_stack[removed].next;
217             layer_stack[removed] = (layer_item_t){};
218             debug("layer_remove: removed.\n");
219             return true;
220         }
221     }
222     return false;
223 }
224 static bool layer_remove_then_push(uint8_t layer)
225 {
226     layer_remove(layer);
227     return layer_push(layer);
228 }
229 static bool layer_remove_or_push(uint8_t layer)
230 {
231     return (layer_remove(layer)) || layer_push(layer);
232 }
233 static void debug_layer_stack(void)
234 {
235     debug("layer_stack: ");
236     layer_item_t item = layer_stack[top_layer];
237     while (item.used) {
238         debug_dec(item.layer);
239         debug("["); debug_dec(item.next); debug("]");
240         item = layer_stack[item.next];
241     }
242     debug("\n");
243 }
244
245
246 void action_exec(keyevent_t event)
247 {
248     if (!IS_NOEVENT(event)) {
249         debug("\n---- action_exec: start -----\n");
250         debug("EVENT: "); debug_event(event); debug("\n");
251     }
252
253     keyrecord_t record = { .event = event };
254
255     // pre-process on tapping
256     if (process_tapping(&record)) {
257         if (!IS_NOEVENT(record.event)) {
258             debug("processed: "); debug_record(record); debug("\n");
259         }
260     } else {
261         // enqueue
262         if (!waiting_buffer_enq(record)) {
263             // clear all in case of overflow.
264             debug("OVERFLOW: CLEAR ALL STATES\n");
265             clear_keyboard();
266             waiting_buffer_clear();
267             tapping_key = (keyrecord_t){};
268         }
269     }
270
271     // process waiting_buffer
272     if (!IS_NOEVENT(event) && waiting_buffer_head != waiting_buffer_tail) {
273         debug("---- action_exec: process waiting_buffer -----\n");
274     }
275
276     for (; waiting_buffer_tail != waiting_buffer_head; waiting_buffer_tail = (waiting_buffer_tail + 1) % WAITING_BUFFER_SIZE) {
277         if (process_tapping(&waiting_buffer[waiting_buffer_tail])) {
278             debug("processed: waiting_buffer["); debug_dec(waiting_buffer_tail); debug("] = ");
279             debug_record(waiting_buffer[waiting_buffer_tail]); debug("\n\n");
280         } else {
281             break;
282         }
283     }
284     if (!IS_NOEVENT(event)) {
285         debug("\n");
286     }
287 }
288
289 static action_t get_action(key_t key)
290 {
291     action_t action;
292
293     /* layer stack */
294     for (layer_item_t i = layer_stack[top_layer]; i.used; i = layer_stack[i.next]) {
295         action = action_for_key(i.layer, key);
296         if (action.code != ACTION_TRANSPARENT) {
297             debug_layer_stack();
298             debug("layer_stack: used. "); debug_dec(i.layer); debug("\n");
299             return action;
300         }
301         debug("layer_stack: through. "); debug_dec(i.layer); debug("\n");
302     }
303
304     /* current layer */
305     action = action_for_key(current_layer, key);
306
307     /* default layer */
308     if (action.code == ACTION_TRANSPARENT) {
309         debug("TRNASPARENT: "); debug_hex16(action.code); debug("\n");
310         action = action_for_key(default_layer, key);
311     }
312     return action;
313 }
314
315 static void process_action(keyrecord_t *record)
316 {
317     keyevent_t event = record->event;
318     uint8_t tap_count = record->tap_count;
319
320     if (IS_NOEVENT(event)) { return; }
321
322     action_t action = get_action(event.key);
323     debug("ACTION: "); debug_action(action); debug("\n");
324
325     switch (action.kind.id) {
326         /* Key and Mods */
327         case ACT_LMODS:
328         case ACT_RMODS:
329             {
330                 uint8_t mods = (action.kind.id == ACT_LMODS) ?  action.key.mods :
331                                                                 action.key.mods<<4;
332                 if (event.pressed) {
333                     uint8_t tmp_mods = host_get_mods();
334                     if (mods) {
335                         host_add_mods(mods);
336                         host_send_keyboard_report();
337                     }
338                     register_code(action.key.code);
339                     if (mods && action.key.code) {
340                         host_set_mods(tmp_mods);
341                         host_send_keyboard_report();
342                     }
343                 } else {
344                     if (mods && !action.key.code) {
345                         host_del_mods(mods);
346                         host_send_keyboard_report();
347                     }
348                     unregister_code(action.key.code);
349                 }
350             }
351             break;
352         case ACT_LMODS_TAP:
353         case ACT_RMODS_TAP:
354             {
355                 uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ?  action.key.mods :
356                                                                     action.key.mods<<4;
357                 switch (action.layer.code) {
358                     case 0x00:
359                         // Oneshot modifier
360                         if (event.pressed) {
361                             if (tap_count == 0) {
362                                 debug("MODS_TAP: Oneshot: add_mods\n");
363                                 add_mods(mods);
364                             }
365                             else if (tap_count == 1) {
366                                 debug("MODS_TAP: Oneshot: start\n");
367                                 oneshot_start(mods, event.time);
368                             }
369                             else if (tap_count == TAPPING_TOGGLE) {
370                                 debug("MODS_TAP: Oneshot: toggle\n");
371                                 oneshot_toggle();
372                             }
373                             else {
374                                 debug("MODS_TAP: Oneshot: cancel&add_mods\n");
375                                 // double tap cancels oneshot and works as normal modifier.
376                                 oneshot_cancel();
377                                 add_mods(mods);
378                             }
379                         } else {
380                             if (tap_count == 0) {
381                                 debug("MODS_TAP: Oneshot: cancel/del_mods\n");
382                                 // cancel oneshot on hold
383                                 oneshot_cancel();
384                                 del_mods(mods);
385                             }
386                             else if (tap_count == 1) {
387                                 debug("MODS_TAP: Oneshot: del_mods\n");
388                                 // retain Oneshot
389                                 del_mods(mods);
390                             }
391                             else {
392                                 debug("MODS_TAP: Oneshot: del_mods\n");
393                                 // cancel Mods
394                                 del_mods(mods);
395                             }
396                         }
397                         break;
398                     default:
399                         if (event.pressed) {
400                             if (tap_count > 0) {
401                                 if (waiting_buffer_has_anykey_pressed()) {
402                                     debug("MODS_TAP: Tap: Cancel: add_mods\n");
403                                     // ad hoc: set 0 to cancel tap
404                                     record->tap_count = 0;
405                                     add_mods(mods);
406                                 } else {
407                                     debug("MODS_TAP: Tap: register_code\n");
408                                     register_code(action.key.code);
409                                 }
410                             } else {
411                                 debug("MODS_TAP: No tap: add_mods\n");
412                                 add_mods(mods);
413                             }
414                         } else {
415                             if (tap_count > 0) {
416                                 debug("MODS_TAP: Tap: unregister_code\n");
417                                 unregister_code(action.key.code);
418                             } else {
419                                 debug("MODS_TAP: No tap: add_mods\n");
420                                 del_mods(mods);
421                             }
422                         }
423                         break;
424                 }
425             }
426             break;
427
428         /* other HID usage */
429         case ACT_USAGE:
430 #ifdef EXTRAKEY_ENABLE
431             switch (action.usage.page) {
432                 case PAGE_SYSTEM:
433                     if (event.pressed) {
434                         host_system_send(action.usage.code);
435                     } else {
436                         host_system_send(0);
437                     }
438                     break;
439                 case PAGE_CONSUMER:
440                     if (event.pressed) {
441                         host_consumer_send(action.usage.code);
442                     } else {
443                         host_consumer_send(0);
444                     }
445                     break;
446             }
447 #endif
448             break;
449
450         /* Mouse key */
451         case ACT_MOUSEKEY:
452 #ifdef MOUSEKEY_ENABLE
453             if (event.pressed) {
454                 mousekey_on(action.key.code);
455                 mousekey_send();
456             } else {
457                 mousekey_off(action.key.code);
458                 mousekey_send();
459             }
460 #endif
461             break;
462
463         /* Layer key */
464         case ACT_LAYER:
465             switch (action.layer.code) {
466                 case LAYER_MOMENTARY:  /* momentary */
467                     if (event.pressed) {
468                         layer_switch(action.layer.val);
469                     }
470                     else {
471                         // NOTE: This is needed by legacy keymap support
472                         layer_switch(default_layer);
473                     }
474                     break;
475                 case LAYER_ON_PRESS:
476                     if (event.pressed) {
477                         layer_switch(action.layer.val);
478                     }
479                     break;
480                 case LAYER_ON_RELEASE:
481                     if (!event.pressed) {
482                         layer_switch(action.layer.val);
483                     }
484                     break;
485                 case LAYER_ON_BOTH:
486                     layer_switch(action.layer.val);
487                     break;
488                 case LAYER_TAP_TOGGLE:  /* switch on hold and toggle on several taps */
489                     if (event.pressed) {
490                         if (tap_count < TAPPING_TOGGLE) {
491                             layer_switch(action.layer.val);
492                         }
493                     } else {
494                         if (tap_count >= TAPPING_TOGGLE) {
495                             debug("LAYER_PRESSED: tap toggle.\n");
496                             layer_switch(action.layer.val);
497                         }
498                     }
499                     break;
500                 case LAYER_SET_DEFAULT_ON_PRESS:
501                     if (event.pressed) {
502                         default_layer = action.layer.val;
503                         layer_switch(default_layer);
504                     }
505                     break;
506                 case LAYER_SET_DEFAULT_ON_RELEASE:
507                     if (!event.pressed) {
508                         default_layer = action.layer.val;
509                         layer_switch(default_layer);
510                     }
511                     break;
512                 case LAYER_SET_DEFAULT_ON_BOTH:
513                     default_layer = action.layer.val;
514                     layer_switch(default_layer);
515                     break;
516                 default:
517                     /* tap key */
518                     if (event.pressed) {
519                         if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
520                              debug("LAYER_SET: Tap: register_code\n");
521                              register_code(action.layer.code);
522                         } else {
523                              debug("LAYER_SET: No tap: layer_set(on press)\n");
524                              layer_switch(action.layer.val);
525                         }
526                     } else {
527                         if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
528                             debug("LAYER_SET: Tap: unregister_code\n");
529                             unregister_code(action.layer.code);
530                         } else {
531                             // NOTE: This is needed by legacy keymap support
532                             debug("LAYER_SET: No tap: return to default layer(on release)\n");
533                             layer_switch(default_layer);
534                         }
535                     }
536                     break;
537             }
538             break;
539         case ACT_LAYER_BIT:
540             switch (action.layer.code) {
541                 case LAYER_MOMENTARY:  /* momentary */
542                     if (event.pressed) {
543                         layer_switch(current_layer | action.layer.val);
544                     } else {
545                         layer_switch(current_layer & ~action.layer.val);
546                     }
547                     break;
548                 case LAYER_ON_PRESS:
549                     if (event.pressed) {
550                         layer_switch(current_layer ^ action.layer.val);
551                     }
552                     break;
553                 case LAYER_ON_RELEASE:
554                     if (!event.pressed) {
555                         layer_switch(current_layer ^ action.layer.val);
556                     }
557                     break;
558                 case LAYER_ON_BOTH:
559                     layer_switch(current_layer ^ action.layer.val);
560                     break;
561                 case LAYER_TAP_TOGGLE:  /* switch on hold and toggle on several taps */
562                     if (event.pressed) {
563                         if (tap_count < TAPPING_TOGGLE) {
564                             debug("LAYER_BIT: tap toggle(press).\n");
565                             layer_switch(current_layer ^ action.layer.val);
566                         }
567                     } else {
568                         if (tap_count <= TAPPING_TOGGLE) {
569                             debug("LAYER_BIT: tap toggle(release).\n");
570                             layer_switch(current_layer ^ action.layer.val);
571                         }
572                     }
573                     break;
574                 case LAYER_SET_DEFAULT_ON_PRESS:
575                     if (event.pressed) {
576                         default_layer = current_layer ^ action.layer.val;
577                         layer_switch(default_layer);
578                     }
579                     break;
580                 case LAYER_SET_DEFAULT_ON_RELEASE:
581                     if (!event.pressed) {
582                         default_layer = current_layer ^ action.layer.val;
583                         layer_switch(default_layer);
584                     }
585                     break;
586                 case LAYER_SET_DEFAULT_ON_BOTH:
587                     default_layer = current_layer ^ action.layer.val;
588                     layer_switch(default_layer);
589                     break;
590                 default:
591                     // tap key
592                     if (event.pressed) {
593                         if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
594                             debug("LAYER_BIT: Tap: register_code\n");
595                             register_code(action.layer.code);
596                         } else {
597                             debug("LAYER_BIT: No tap: layer_bit(on press)\n");
598                             layer_switch(current_layer ^ action.layer.val);
599                         }
600                     } else {
601                         if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
602                             debug("LAYER_BIT: Tap: unregister_code\n");
603                             unregister_code(action.layer.code);
604                         } else {
605                             debug("LAYER_BIT: No tap: layer_bit(on release)\n");
606                             layer_switch(current_layer ^ action.layer.val);
607                         }
608                     }
609                     break;
610             }
611             break;
612         case ACT_LAYER_STACK:
613             switch (action.layer.code) {
614                 case LAYER_MOMENTARY:  /* momentary */
615                     if (event.pressed) {
616                         layer_remove_then_push(action.layer.val);
617                         debug_layer_stack();
618                     } else {
619                         layer_remove(action.layer.val);
620                         debug_layer_stack();
621                     }
622                     break;
623                 case LAYER_ON_PRESS:
624                     if (event.pressed) {
625                         layer_remove_or_push(action.layer.val);
626                         debug_layer_stack();
627                     }
628                     break;
629                 case LAYER_ON_RELEASE:
630                     if (!event.pressed) {
631                         layer_remove_or_push(action.layer.val);
632                         debug_layer_stack();
633                     }
634                     break;
635                 case LAYER_ON_BOTH:
636                     layer_remove_or_push(action.layer.val);
637                     debug_layer_stack();
638                     break;
639                 case LAYER_TAP_TOGGLE:  /* switch on hold and toggle on several taps */
640                     if (event.pressed) {
641                         if (tap_count < TAPPING_TOGGLE) {
642                             debug("LAYER_STACK: tap toggle(press).\n");
643                             layer_remove_or_push(action.layer.val);
644                             debug_layer_stack();
645                         }
646                     } else {
647                         if (tap_count <= TAPPING_TOGGLE) {
648                             debug("LAYER_STACK: tap toggle(release).\n");
649                             layer_remove_or_push(action.layer.val);
650                             debug_layer_stack();
651                         }
652                     }
653                     break;
654                 default:
655                     // tap key
656                     if (event.pressed) {
657                         if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
658                             debug("LAYER_STACK: Tap: register_code\n");
659                             register_code(action.layer.code);
660                         } else {
661                             debug("LAYER_STACK: No tap: layer_stack(on press)\n");
662                             layer_remove_or_push(action.layer.val);
663                             debug_layer_stack();
664                         }
665                     } else {
666                         if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
667                             debug("LAYER_STACK: Tap: unregister_code\n");
668                             unregister_code(action.layer.code);
669                         } else {
670                             debug("LAYER_STACK: No tap: layer_stack(on release)\n");
671                             layer_remove_or_push(action.layer.val);
672                             debug_layer_stack();
673                         }
674                     }
675                     break;
676             }
677             break;
678
679         /* Extentions */
680         case ACT_MACRO:
681             // TODO
682             break;
683         case ACT_COMMAND:
684             break;
685         case ACT_FUNCTION:
686             action_function(record, action.func.id, action.func.opt);
687             break;
688         default:
689             break;
690     }
691 }
692
693 /* Tapping
694  *
695  * Rule: Tap key is typed(pressed and released) within TAPPING_TERM.
696  *       (without interfering by typing other key)
697  */
698 /* return true when key event is processed or consumed. */
699 static bool process_tapping(keyrecord_t *keyp)
700 {
701     keyevent_t event = keyp->event;
702
703     // if tapping
704     if (IS_TAPPING_PRESSED()) {
705         if (WITHIN_TAPPING_TERM(event)) {
706             if (tapping_key.tap_count == 0) {
707                 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
708                     // first tap!
709                     debug("Tapping: First tap(0->1).\n");
710                     tapping_key.tap_count = 1;
711                     debug_tapping_key();
712                     process_action(&tapping_key);
713
714                     // enqueue
715                     keyp->tap_count = tapping_key.tap_count;
716                     return false;
717                 }
718 #if TAPPING_TERM >= 500
719                 /* This can prevent from typing some tap keys in a row at a time. */
720                 else if (!event.pressed && waiting_buffer_typed(event)) {
721                     // other key typed. not tap.
722                     debug("Tapping: End. No tap. Interfered by typing key\n");
723                     process_action(&tapping_key);
724                     tapping_key = (keyrecord_t){};
725                     debug_tapping_key();
726
727                     // enqueue
728                     return false;
729                 }
730 #endif
731                 else {
732                     // other key events shall be enq'd till tapping state settles.
733                     return false;
734                 }
735             }
736             // tap_count > 0
737             else {
738                 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
739                     debug("Tapping: Tap release("); debug_dec(tapping_key.tap_count); debug(")\n");
740                     keyp->tap_count = tapping_key.tap_count;
741                     process_action(keyp);
742                     tapping_key = *keyp;
743                     debug_tapping_key();
744                     return true;
745                 }
746                 else if (is_tap_key(keyp->event.key) && event.pressed) {
747                     if (tapping_key.tap_count > 1) {
748                         debug("Tapping: Start new tap with releasing last tap(>1).\n");
749                         // unregister key
750                         process_action(&(keyrecord_t){
751                                 .tap_count = tapping_key.tap_count,
752                                 .event.key = tapping_key.event.key,
753                                 .event.time = event.time,
754                                 .event.pressed = false
755                         });
756                     } else {
757                         debug("Tapping: Start while last tap(1).\n");
758                     }
759                     tapping_key = *keyp;
760                     waiting_buffer_scan_tap();
761                     debug_tapping_key();
762                     return true;
763                 }
764                 else {
765                     if (!IS_NOEVENT(keyp->event)) {
766                         debug("Tapping: key event while last tap(>0).\n");
767                     }
768                     process_action(keyp);
769                     return true;
770                 }
771             }
772         }
773         // after TAPPING_TERM
774         else {
775             if (tapping_key.tap_count == 0) {
776                 debug("Tapping: End. Timeout. Not tap(0): ");
777                 debug_event(event); debug("\n");
778                 process_action(&tapping_key);
779                 tapping_key = (keyrecord_t){};
780                 debug_tapping_key();
781                 return false;
782             }  else {
783                 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
784                     debug("Tapping: End. last timeout tap release(>0).");
785                     keyp->tap_count = tapping_key.tap_count;
786                     process_action(keyp);
787                     tapping_key = (keyrecord_t){};
788                     return true;
789                 }
790                 else if (is_tap_key(keyp->event.key) && event.pressed) {
791                     if (tapping_key.tap_count > 1) {
792                         debug("Tapping: Start new tap with releasing last timeout tap(>1).\n");
793                         // unregister key
794                         process_action(&(keyrecord_t){
795                                 .tap_count = tapping_key.tap_count,
796                                 .event.key = tapping_key.event.key,
797                                 .event.time = event.time,
798                                 .event.pressed = false
799                         });
800                     } else {
801                         debug("Tapping: Start while last timeout tap(1).\n");
802                     }
803                     tapping_key = *keyp;
804                     waiting_buffer_scan_tap();
805                     debug_tapping_key();
806                     return true;
807                 }
808                 else {
809                     if (!IS_NOEVENT(keyp->event)) {
810                         debug("Tapping: key event while last timeout tap(>0).\n");
811                     }
812                     process_action(keyp);
813                     return true;
814                 }
815             }
816         }
817     } else if (IS_TAPPING_RELEASED()) {
818         if (WITHIN_TAPPING_TERM(event)) {
819             if (tapping_key.tap_count > 0 && IS_TAPPING_KEY(event.key) && event.pressed) {
820                 // sequential tap.
821                 keyp->tap_count = tapping_key.tap_count + 1;
822                 debug("Tapping: Tap press("); debug_dec(keyp->tap_count); debug(")\n");
823                 process_action(keyp);
824                 tapping_key = *keyp;
825                 debug_tapping_key();
826                 return true;
827             } else if (event.pressed && is_tap_key(event.key)) {
828                 // Sequential tap can be interfered with other tap key.
829                 debug("Tapping: Start with interfering other tap.\n");
830                 tapping_key = *keyp;
831                 waiting_buffer_scan_tap();
832                 debug_tapping_key();
833                 return true;
834             } else {
835                 if (!IS_NOEVENT(keyp->event)) debug("Tapping: other key just after tap.\n");
836                 process_action(keyp);
837                 return true;
838             }
839         } else {
840             // timeout. no sequential tap.
841             debug("Tapping: End(Timeout after releasing last tap): ");
842             debug_event(event); debug("\n");
843             tapping_key = (keyrecord_t){};
844             debug_tapping_key();
845             return false;
846         }
847     }
848     // not tapping satate
849     else {
850         if (event.pressed && is_tap_key(event.key)) {
851             debug("Tapping: Start(Press tap key).\n");
852             tapping_key = *keyp;
853             waiting_buffer_scan_tap();
854             debug_tapping_key();
855             return true;
856         } else {
857             process_action(keyp);
858             return true;
859         }
860     }
861 }
862
863 /* scan buffer for tapping */
864 static void waiting_buffer_scan_tap(void)
865 {
866     // tapping already is settled
867     if (tapping_key.tap_count > 0) return;
868     // invalid state: tapping_key released && tap_count == 0
869     if (!tapping_key.event.pressed) return;
870
871     for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
872         if (IS_TAPPING_KEY(waiting_buffer[i].event.key) &&
873                 !waiting_buffer[i].event.pressed &&
874                 WITHIN_TAPPING_TERM(waiting_buffer[i].event)) {
875             tapping_key.tap_count = 1;
876             waiting_buffer[i].tap_count = 1;
877             process_action(&tapping_key);
878
879             debug("waiting_buffer_scan_tap: found at ["); debug_dec(i); debug("]\n");
880             debug_waiting_buffer();
881             return;
882         }
883     }
884 }
885
886
887
888 /*
889  * Utilities for actions.
890  */
891 void register_code(uint8_t code)
892 {
893     if (code == KC_NO) {
894         return;
895     }
896     else if IS_KEY(code) {
897         // TODO: should push command_proc out of this block?
898         if (command_proc(code)) return;
899
900         if (oneshot_state.mods && oneshot_state.ready && !oneshot_state.disabled) {
901             uint8_t tmp_mods = host_get_mods();
902             host_add_mods(oneshot_state.mods);
903             host_add_key(code);
904             host_send_keyboard_report();
905
906             host_set_mods(tmp_mods);
907             oneshot_state.ready = false;
908         } else {
909             host_add_key(code);
910             host_send_keyboard_report();
911         }
912     }
913     else if IS_MOD(code) {
914         host_add_mods(MOD_BIT(code));
915         host_send_keyboard_report();
916     }
917 }
918
919 void unregister_code(uint8_t code)
920 {
921     if IS_KEY(code) {
922         host_del_key(code);
923         host_send_keyboard_report();
924     }
925     else if IS_MOD(code) {
926         host_del_mods(MOD_BIT(code));
927         host_send_keyboard_report();
928     }
929 }
930
931 void add_mods(uint8_t mods)
932 {
933     if (mods) {
934         host_add_mods(mods);
935         host_send_keyboard_report();
936     }
937 }
938
939 void del_mods(uint8_t mods)
940 {
941     if (mods) {
942         host_del_mods(mods);
943         host_send_keyboard_report();
944     }
945 }
946
947 void set_mods(uint8_t mods)
948 {
949     host_set_mods(mods);
950     host_send_keyboard_report();
951 }
952
953 void clear_keyboard(void)
954 {
955     host_clear_mods();
956     clear_keyboard_but_mods();
957 }
958
959 void clear_keyboard_but_mods(void)
960 {
961     host_clear_keys();
962     host_send_keyboard_report();
963 #ifdef MOUSEKEY_ENABLE
964     mousekey_clear();
965     mousekey_send();
966 #endif
967 #ifdef EXTRAKEY_ENABLE
968     host_system_send(0);
969     host_consumer_send(0);
970 #endif
971 }
972
973 bool sending_anykey(void)
974 {
975     return (host_has_anykey() || host_mouse_in_use() ||
976             host_last_sysytem_report() || host_last_consumer_report());
977 }
978
979 void layer_switch(uint8_t new_layer)
980 {
981     if (current_layer != new_layer) {
982         debug("Layer Switch: "); debug_hex(current_layer);
983         debug(" -> "); debug_hex(new_layer); debug("\n");
984
985         current_layer = new_layer;
986         clear_keyboard_but_mods(); // To avoid stuck keys
987         // NOTE: update mods with full scan of matrix? if modifier changes between layers
988     }
989 }
990
991 bool is_tap_key(key_t key)
992 {
993     action_t action = get_action(key);
994
995     switch (action.kind.id) {
996         case ACT_LMODS_TAP:
997         case ACT_RMODS_TAP:
998             return true;
999         case ACT_LAYER:
1000         case ACT_LAYER_BIT:
1001             switch (action.layer.code) {
1002                 case LAYER_MOMENTARY:
1003                 case LAYER_ON_PRESS:
1004                 case LAYER_ON_RELEASE:
1005                 case LAYER_ON_BOTH:
1006                 case LAYER_SET_DEFAULT_ON_PRESS:
1007                 case LAYER_SET_DEFAULT_ON_RELEASE:
1008                 case LAYER_SET_DEFAULT_ON_BOTH:
1009                     return false;
1010                 case LAYER_TAP_TOGGLE:
1011                 default:    /* tap key */
1012                     return true;
1013             }
1014             return false;
1015         case ACT_FUNCTION:
1016             if (action.func.opt & FUNC_TAP) { return true; }
1017             return false;
1018     }
1019     return false;
1020 }
1021
1022
1023 /*
1024  * debug print
1025  */
1026 static void debug_event(keyevent_t event)
1027 {
1028     debug_hex16((event.key.row<<8) | event.key.col);
1029     if (event.pressed) debug("d("); else debug("u(");
1030     debug_dec(event.time); debug(")");
1031 }
1032 static void debug_record(keyrecord_t record)
1033 {
1034     debug_event(record.event); debug(":"); debug_dec(record.tap_count);
1035 }
1036 static void debug_action(action_t action)
1037 {
1038     switch (action.kind.id) {
1039         case ACT_LMODS:             debug("ACT_LMODS");             break;
1040         case ACT_RMODS:             debug("ACT_RMODS");             break;
1041         case ACT_LMODS_TAP:         debug("ACT_LMODS_TAP");         break;
1042         case ACT_RMODS_TAP:         debug("ACT_RMODS_TAP");         break;
1043         case ACT_USAGE:             debug("ACT_USAGE");             break;
1044         case ACT_MOUSEKEY:          debug("ACT_MOUSEKEY");          break;
1045         case ACT_LAYER:             debug("ACT_LAYER");             break;
1046         case ACT_LAYER_BIT:         debug("ACT_LAYER_BIT");         break;
1047         case ACT_LAYER_STACK:       debug("ACT_LAYER_STACK");       break;
1048         case ACT_MACRO:             debug("ACT_MACRO");             break;
1049         case ACT_COMMAND:           debug("ACT_COMMAND");           break;
1050         case ACT_FUNCTION:          debug("ACT_FUNCTION");          break;
1051         default:                    debug("UNKNOWN");               break;
1052     }
1053     debug("[");
1054     debug_hex4(action.kind.param>>8);
1055     debug(":");
1056     debug_hex8(action.kind.param & 0xff);
1057     debug("]");
1058 }
1059 static void debug_tapping_key(void)
1060 {
1061     debug("TAPPING_KEY="); debug_record(tapping_key); debug("\n");
1062 }
1063 static void debug_waiting_buffer(void)
1064 {
1065     debug("{ ");
1066     for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
1067         debug("["); debug_dec(i); debug("]="); debug_record(waiting_buffer[i]); debug(" ");
1068     }
1069     debug("}\n");
1070 }