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