]> git.donarmstrong.com Git - tmk_firmware.git/blob - common/action.c
Merge branch 'locking_caps'
[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         case ACT_OVERLAY:
521             switch (action.layer.code) {
522                 // Overlay Invert bit4
523                 case OP_INV4 | 0:
524                     if (action.layer.val == 0) {
525                         // NOTE: reserved for future use
526                         overlay_clear();
527                     } else {
528                         overlay_set(overlay_stat ^ action.layer.val);
529                     }
530                     break;
531                 case OP_INV4 | 1:
532                     if (action.layer.val == 0) {
533                         // on pressed
534                         if (event.pressed) overlay_clear();
535                     } else {
536                         overlay_set(overlay_stat ^ action.layer.val<<4);
537                     }
538                     break;
539                 case OP_INV4 | 2:
540                     if (action.layer.val == 0) {
541                         // on released
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                         // on both
550                         overlay_clear();
551                     } else {
552                         overlay_set(overlay_stat ^ action.layer.val<<12);
553                     }
554                     break;
555
556                 /* Overlay Bit invert */
557                 case OP_INV:
558                     /* with tap toggle */
559                     if (event.pressed) {
560                         if (tap_count < TAPPING_TOGGLE) {
561                             debug("OVERLAY_INV: tap toggle(press).\n");
562                             overlay_invert(action.layer.val);
563                         }
564                     } else {
565                         if (tap_count <= TAPPING_TOGGLE) {
566                             debug("OVERLAY_INV: tap toggle(release).\n");
567                             overlay_invert(action.layer.val);
568                         }
569                     }
570                     break;
571                 case (OP_INV | ON_PRESS):
572                     if (event.pressed) {
573                         overlay_invert(action.layer.val);
574                     }
575                     break;
576                 case (OP_INV | ON_RELEASE):
577                     if (!event.pressed) {
578                         overlay_invert(action.layer.val);
579                     }
580                     break;
581                 case (OP_INV | ON_BOTH):
582                     overlay_invert(action.layer.val);
583                     break;
584
585                 /* Overlay Bit on */
586                 case OP_ON:
587                     if (event.pressed) {
588                         overlay_on(action.layer.val);
589                     } else {
590                         overlay_off(action.layer.val);
591                     }
592                     break;
593                 case (OP_ON | ON_PRESS):
594                     if (event.pressed) {
595                         overlay_on(action.layer.val);
596                     }
597                     break;
598                 case (OP_ON | ON_RELEASE):
599                     if (!event.pressed) {
600                         overlay_on(action.layer.val);
601                     }
602                     break;
603                 case (OP_ON | ON_BOTH):
604                     overlay_on(action.layer.val);
605                     break;
606
607                 /* Overlay Bit off */
608                 case OP_OFF:
609                     if (event.pressed) {
610                         overlay_off(action.layer.val);
611                     } else {
612                         overlay_on(action.layer.val);
613                     }
614                     break;
615                 case (OP_OFF | ON_PRESS):
616                     if (event.pressed) {
617                         overlay_off(action.layer.val);
618                     }
619                     break;
620                 case (OP_OFF | ON_RELEASE):
621                     if (!event.pressed) {
622                         overlay_off(action.layer.val);
623                     }
624                     break;
625                 case (OP_OFF | ON_BOTH):
626                     overlay_off(action.layer.val);
627                     break;
628
629                 /* Overlay Bit set */
630                 case OP_SET:
631                     if (event.pressed) {
632                         overlay_move(action.layer.val);
633                     } else {
634                         overlay_clear();
635                     }
636                     break;
637                 case (OP_SET | ON_PRESS):
638                     if (event.pressed) {
639                         overlay_move(action.layer.val);
640                     }
641                     break;
642                 case (OP_SET | ON_RELEASE):
643                     if (!event.pressed) {
644                         overlay_move(action.layer.val);
645                     }
646                     break;
647                 case (OP_SET | ON_BOTH):
648                     overlay_move(action.layer.val);
649                     break;
650
651                 /* Overlay Bit invert with tap key */
652                 default:
653                     if (event.pressed) {
654                         if (tap_count > 0) {
655                             debug("OVERLAY_TAP_KEY: Tap: register_code\n");
656                             register_code(action.layer.code);
657                         } else {
658                             debug("OVERLAY_TAP_KEY: No tap: On on press\n");
659                             overlay_on(action.layer.val);
660                         }
661                     } else {
662                         if (tap_count > 0) {
663                             debug("OVERLAY_TAP_KEY: Tap: unregister_code\n");
664                             unregister_code(action.layer.code);
665                         } else {
666                             debug("OVERLAY_TAP_KEY: No tap: Off on release\n");
667                             overlay_off(action.layer.val);
668                         }
669                     }
670                     break;
671             }
672             break;
673
674         /* Extentions */
675         case ACT_MACRO:
676             action_macro_play(action_get_macro(record, action.func.id, action.func.opt));
677             break;
678         case ACT_COMMAND:
679             break;
680         case ACT_FUNCTION:
681             action_function(record, action.func.id, action.func.opt);
682             break;
683         default:
684             break;
685     }
686 }
687
688 /* Tapping
689  *
690  * Rule: Tap key is typed(pressed and released) within TAPPING_TERM.
691  *       (without interfering by typing other key)
692  */
693 /* return true when key event is processed or consumed. */
694 static bool process_tapping(keyrecord_t *keyp)
695 {
696     keyevent_t event = keyp->event;
697
698     // if tapping
699     if (IS_TAPPING_PRESSED()) {
700         if (WITHIN_TAPPING_TERM(event)) {
701             if (tapping_key.tap.count == 0) {
702                 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
703                     // first tap!
704                     debug("Tapping: First tap(0->1).\n");
705                     tapping_key.tap.count = 1;
706                     tapping_key.tap.interrupted  = (waiting_buffer_has_anykey_pressed() ? true : false);
707                     debug_tapping_key();
708                     process_action(&tapping_key);
709
710                     // enqueue
711                     keyp->tap = tapping_key.tap;
712                     return false;
713                 }
714 #if TAPPING_TERM >= 500
715                 /* This can prevent from typing some tap keys in a row at a time. */
716                 else if (!event.pressed && waiting_buffer_typed(event)) {
717                     // other key typed. not tap.
718                     debug("Tapping: End. No tap. Interfered by typing key\n");
719                     process_action(&tapping_key);
720                     tapping_key = (keyrecord_t){};
721                     debug_tapping_key();
722
723                     // enqueue
724                     return false;
725                 }
726 #endif
727                 else {
728                     // other key events shall be enq'd till tapping state settles.
729                     return false;
730                 }
731             }
732             // tap_count > 0
733             else {
734                 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
735                     debug("Tapping: Tap release("); debug_dec(tapping_key.tap.count); debug(")\n");
736                     keyp->tap = tapping_key.tap;
737                     process_action(keyp);
738                     tapping_key = *keyp;
739                     debug_tapping_key();
740                     return true;
741                 }
742                 else if (is_tap_key(keyp->event.key) && event.pressed) {
743                     if (tapping_key.tap.count > 1) {
744                         debug("Tapping: Start new tap with releasing last tap(>1).\n");
745                         // unregister key
746                         process_action(&(keyrecord_t){
747                                 .tap = tapping_key.tap,
748                                 .event.key = tapping_key.event.key,
749                                 .event.time = event.time,
750                                 .event.pressed = false
751                         });
752                     } else {
753                         debug("Tapping: Start while last tap(1).\n");
754                     }
755                     tapping_key = *keyp;
756                     waiting_buffer_scan_tap();
757                     debug_tapping_key();
758                     return true;
759                 }
760                 else {
761                     if (!IS_NOEVENT(keyp->event)) {
762                         debug("Tapping: key event while last tap(>0).\n");
763                     }
764                     process_action(keyp);
765                     return true;
766                 }
767             }
768         }
769         // after TAPPING_TERM
770         else {
771             if (tapping_key.tap.count == 0) {
772                 debug("Tapping: End. Timeout. Not tap(0): ");
773                 debug_event(event); debug("\n");
774                 process_action(&tapping_key);
775                 tapping_key = (keyrecord_t){};
776                 debug_tapping_key();
777                 return false;
778             }  else {
779                 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
780                     debug("Tapping: End. last timeout tap release(>0).");
781                     keyp->tap = tapping_key.tap;
782                     process_action(keyp);
783                     tapping_key = (keyrecord_t){};
784                     return true;
785                 }
786                 else if (is_tap_key(keyp->event.key) && event.pressed) {
787                     if (tapping_key.tap.count > 1) {
788                         debug("Tapping: Start new tap with releasing last timeout tap(>1).\n");
789                         // unregister key
790                         process_action(&(keyrecord_t){
791                                 .tap = tapping_key.tap,
792                                 .event.key = tapping_key.event.key,
793                                 .event.time = event.time,
794                                 .event.pressed = false
795                         });
796                     } else {
797                         debug("Tapping: Start while last timeout tap(1).\n");
798                     }
799                     tapping_key = *keyp;
800                     waiting_buffer_scan_tap();
801                     debug_tapping_key();
802                     return true;
803                 }
804                 else {
805                     if (!IS_NOEVENT(keyp->event)) {
806                         debug("Tapping: key event while last timeout tap(>0).\n");
807                     }
808                     process_action(keyp);
809                     return true;
810                 }
811             }
812         }
813     } else if (IS_TAPPING_RELEASED()) {
814         if (WITHIN_TAPPING_TERM(event)) {
815             if (tapping_key.tap.count > 0 && IS_TAPPING_KEY(event.key) && event.pressed) {
816                 // sequential tap.
817                 keyp->tap = tapping_key.tap;
818                 keyp->tap.count += 1;
819                 debug("Tapping: Tap press("); debug_dec(keyp->tap.count); debug(")\n");
820                 process_action(keyp);
821                 tapping_key = *keyp;
822                 debug_tapping_key();
823                 return true;
824             } else if (event.pressed && is_tap_key(event.key)) {
825                 // Sequential tap can be interfered with other tap key.
826                 debug("Tapping: Start with interfering other tap.\n");
827                 tapping_key = *keyp;
828                 waiting_buffer_scan_tap();
829                 debug_tapping_key();
830                 return true;
831             } else {
832                 if (!IS_NOEVENT(keyp->event)) debug("Tapping: other key just after tap.\n");
833                 process_action(keyp);
834                 return true;
835             }
836         } else {
837             // timeout. no sequential tap.
838             debug("Tapping: End(Timeout after releasing last tap): ");
839             debug_event(event); debug("\n");
840             tapping_key = (keyrecord_t){};
841             debug_tapping_key();
842             return false;
843         }
844     }
845     // not tapping satate
846     else {
847         if (event.pressed && is_tap_key(event.key)) {
848             debug("Tapping: Start(Press tap key).\n");
849             tapping_key = *keyp;
850             waiting_buffer_scan_tap();
851             debug_tapping_key();
852             return true;
853         } else {
854             process_action(keyp);
855             return true;
856         }
857     }
858 }
859
860 /* scan buffer for tapping */
861 static void waiting_buffer_scan_tap(void)
862 {
863     // tapping already is settled
864     if (tapping_key.tap.count > 0) return;
865     // invalid state: tapping_key released && tap.count == 0
866     if (!tapping_key.event.pressed) return;
867
868     for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
869         if (IS_TAPPING_KEY(waiting_buffer[i].event.key) &&
870                 !waiting_buffer[i].event.pressed &&
871                 WITHIN_TAPPING_TERM(waiting_buffer[i].event)) {
872             tapping_key.tap.count = 1;
873             waiting_buffer[i].tap.count = 1;
874             process_action(&tapping_key);
875
876             debug("waiting_buffer_scan_tap: found at ["); debug_dec(i); debug("]\n");
877             debug_waiting_buffer();
878             return;
879         }
880     }
881 }
882
883
884
885 /*
886  * Utilities for actions.
887  */
888 void register_code(uint8_t code)
889 {
890     if (code == KC_NO) {
891         return;
892     }
893 #ifdef CAPSLOCK_LOCKING_ENABLE
894     else if (KC_LOCKING_CAPS == code) {
895 #ifdef CAPSLOCK_LOCKING_RESYNC_ENABLE
896         // Resync: ignore if caps lock already is on
897         if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) return;
898 #endif
899         host_add_key(KC_CAPSLOCK);
900         host_send_keyboard_report();
901         host_del_key(KC_CAPSLOCK);
902         host_send_keyboard_report();
903     }
904 #endif
905     else if IS_KEY(code) {
906         // TODO: should push command_proc out of this block?
907         if (command_proc(code)) return;
908
909         if (oneshot_state.mods && oneshot_state.ready && !oneshot_state.disabled) {
910             uint8_t tmp_mods = host_get_mods();
911             host_add_mods(oneshot_state.mods);
912             host_add_key(code);
913             host_send_keyboard_report();
914
915             host_set_mods(tmp_mods);
916             oneshot_state.ready = false;
917         } else {
918             host_add_key(code);
919             host_send_keyboard_report();
920         }
921     }
922     else if IS_MOD(code) {
923         host_add_mods(MOD_BIT(code));
924         host_send_keyboard_report();
925     }
926 }
927
928 void unregister_code(uint8_t code)
929 {
930     if (code == KC_NO) {
931         return;
932     }
933 #ifdef CAPSLOCK_LOCKING_ENABLE
934     else if (KC_LOCKING_CAPS == code) {
935 #ifdef CAPSLOCK_LOCKING_RESYNC_ENABLE
936         // Resync: ignore if caps lock already is off
937         if (!(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK))) return;
938 #endif
939         host_add_key(KC_CAPSLOCK);
940         host_send_keyboard_report();
941         host_del_key(KC_CAPSLOCK);
942         host_send_keyboard_report();
943     }
944 #endif
945     else if IS_KEY(code) {
946         host_del_key(code);
947         host_send_keyboard_report();
948     }
949     else if IS_MOD(code) {
950         host_del_mods(MOD_BIT(code));
951         host_send_keyboard_report();
952     }
953 }
954
955 void add_mods(uint8_t mods)
956 {
957     if (mods) {
958         host_add_mods(mods);
959         host_send_keyboard_report();
960     }
961 }
962
963 void del_mods(uint8_t mods)
964 {
965     if (mods) {
966         host_del_mods(mods);
967         host_send_keyboard_report();
968     }
969 }
970
971 void set_mods(uint8_t mods)
972 {
973     host_set_mods(mods);
974     host_send_keyboard_report();
975 }
976
977 void clear_keyboard(void)
978 {
979     host_clear_mods();
980     clear_keyboard_but_mods();
981 }
982
983 void clear_keyboard_but_mods(void)
984 {
985     host_clear_keys();
986     host_send_keyboard_report();
987 #ifdef MOUSEKEY_ENABLE
988     mousekey_clear();
989     mousekey_send();
990 #endif
991 #ifdef EXTRAKEY_ENABLE
992     host_system_send(0);
993     host_consumer_send(0);
994 #endif
995 }
996
997 bool sending_anykey(void)
998 {
999     return (host_has_anykey() || host_mouse_in_use() ||
1000             host_last_sysytem_report() || host_last_consumer_report());
1001 }
1002
1003 bool is_tap_key(key_t key)
1004 {
1005     action_t action = layer_switch_get_action(key);
1006
1007     switch (action.kind.id) {
1008         case ACT_LMODS_TAP:
1009         case ACT_RMODS_TAP:
1010             return true;
1011         case ACT_KEYMAP:
1012         case ACT_OVERLAY:
1013             switch (action.layer.code) {
1014                 case 0x04 ... 0xEF:    /* tap key */
1015                 case OP_INV:
1016                     return true;
1017                 default:
1018                     return false;
1019             }
1020         case ACT_MACRO:
1021         case ACT_FUNCTION:
1022             if (action.func.opt & FUNC_TAP) { return true; }
1023             return false;
1024     }
1025     return false;
1026 }
1027
1028
1029 /*
1030  * debug print
1031  */
1032 static void debug_event(keyevent_t event)
1033 {
1034     debug_hex16((event.key.row<<8) | event.key.col);
1035     if (event.pressed) debug("d("); else debug("u(");
1036     debug_dec(event.time); debug(")");
1037 }
1038 static void debug_record(keyrecord_t record)
1039 {
1040     debug_event(record.event); debug(":"); debug_dec(record.tap.count);
1041     if (record.tap.interrupted) debug("-");
1042 }
1043 static void debug_action(action_t action)
1044 {
1045     switch (action.kind.id) {
1046         case ACT_LMODS:             debug("ACT_LMODS");             break;
1047         case ACT_RMODS:             debug("ACT_RMODS");             break;
1048         case ACT_LMODS_TAP:         debug("ACT_LMODS_TAP");         break;
1049         case ACT_RMODS_TAP:         debug("ACT_RMODS_TAP");         break;
1050         case ACT_USAGE:             debug("ACT_USAGE");             break;
1051         case ACT_MOUSEKEY:          debug("ACT_MOUSEKEY");          break;
1052         case ACT_KEYMAP:            debug("ACT_KEYMAP");            break;
1053         case ACT_OVERLAY:           debug("ACT_OVERLAY");           break;
1054         case ACT_MACRO:             debug("ACT_MACRO");             break;
1055         case ACT_COMMAND:           debug("ACT_COMMAND");           break;
1056         case ACT_FUNCTION:          debug("ACT_FUNCTION");          break;
1057         default:                    debug("UNKNOWN");               break;
1058     }
1059     debug("[");
1060     debug_hex4(action.kind.param>>8);
1061     debug(":");
1062     debug_hex8(action.kind.param & 0xff);
1063     debug("]");
1064 }
1065 static void debug_tapping_key(void)
1066 {
1067     debug("TAPPING_KEY="); debug_record(tapping_key); debug("\n");
1068 }
1069 static void debug_waiting_buffer(void)
1070 {
1071     debug("{ ");
1072     for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
1073         debug("["); debug_dec(i); debug("]="); debug_record(waiting_buffer[i]); debug(" ");
1074     }
1075     debug("}\n");
1076 }