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