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