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