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