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