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