]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/common/action.c
Add support for TAP_CODE_DELAY to Hold-Tap keys (#5400)
[qmk_firmware.git] / tmk_core / 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 "keycode.h"
19 #include "keyboard.h"
20 #include "mousekey.h"
21 #include "command.h"
22 #include "led.h"
23 #include "backlight.h"
24 #include "action_layer.h"
25 #include "action_tapping.h"
26 #include "action_macro.h"
27 #include "action_util.h"
28 #include "action.h"
29 #include "wait.h"
30
31 #ifdef DEBUG_ACTION
32 #include "debug.h"
33 #else
34 #include "nodebug.h"
35 #endif
36
37 int tp_buttons;
38
39 #ifdef RETRO_TAPPING
40 int retro_tapping_counter = 0;
41 #endif
42
43 #ifdef FAUXCLICKY_ENABLE
44 #include <fauxclicky.h>
45 #endif
46
47 #ifndef TAP_CODE_DELAY
48 #  define TAP_CODE_DELAY 0
49 #endif
50 #ifndef TAP_HOLD_CAPS_DELAY
51 #  define TAP_HOLD_CAPS_DELAY 80
52 #endif
53 /** \brief Called to execute an action.
54  *
55  * FIXME: Needs documentation.
56  */
57 void action_exec(keyevent_t event)
58 {
59     if (!IS_NOEVENT(event)) {
60         dprint("\n---- action_exec: start -----\n");
61         dprint("EVENT: "); debug_event(event); dprintln();
62 #ifdef RETRO_TAPPING
63         retro_tapping_counter++;
64 #endif
65     }
66
67 #ifdef FAUXCLICKY_ENABLE
68     if (IS_PRESSED(event)) {
69         FAUXCLICKY_ACTION_PRESS;
70     }
71     if (IS_RELEASED(event)) {
72         FAUXCLICKY_ACTION_RELEASE;
73     }
74     fauxclicky_check();
75 #endif
76
77 #ifdef SWAP_HANDS_ENABLE
78     if (!IS_NOEVENT(event)) {
79         process_hand_swap(&event);
80     }
81 #endif
82
83     keyrecord_t record = { .event = event };
84
85 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
86     if (has_oneshot_layer_timed_out()) {
87         clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
88     }
89     if (has_oneshot_mods_timed_out()) {
90         clear_oneshot_mods();
91     }
92 #endif
93
94 #ifndef NO_ACTION_TAPPING
95     action_tapping_process(record);
96 #else
97     process_record(&record);
98     if (!IS_NOEVENT(record.event)) {
99         dprint("processed: "); debug_record(record); dprintln();
100     }
101 #endif
102 }
103
104 #ifdef SWAP_HANDS_ENABLE
105 bool swap_hands = false;
106 bool swap_held = false;
107
108 /** \brief Process Hand Swap
109  *
110  * FIXME: Needs documentation.
111  */
112 void process_hand_swap(keyevent_t *event) {
113     static swap_state_row_t swap_state[MATRIX_ROWS];
114
115     keypos_t pos = event->key;
116     swap_state_row_t col_bit = (swap_state_row_t)1<<pos.col;
117     bool do_swap = event->pressed ? swap_hands :
118                                     swap_state[pos.row] & (col_bit);
119
120     if (do_swap) {
121         event->key = hand_swap_config[pos.row][pos.col];
122         swap_state[pos.row] |= col_bit;
123     } else {
124         swap_state[pos.row] &= ~(col_bit);
125     }
126 }
127 #endif
128
129 #if !defined(NO_ACTION_LAYER) && !defined(STRICT_LAYER_RELEASE)
130 bool disable_action_cache = false;
131
132 void process_record_nocache(keyrecord_t *record)
133 {
134     disable_action_cache = true;
135     process_record(record);
136     disable_action_cache = false;
137 }
138 #else
139 void process_record_nocache(keyrecord_t *record)
140 {
141     process_record(record);
142 }
143 #endif
144
145 __attribute__ ((weak))
146 bool process_record_quantum(keyrecord_t *record) {
147     return true;
148 }
149
150 #ifndef NO_ACTION_TAPPING
151 /** \brief Allows for handling tap-hold actions immediately instead of waiting for TAPPING_TERM or another keypress.
152  *
153  * FIXME: Needs documentation.
154  */
155 void process_record_tap_hint(keyrecord_t *record)
156 {
157     action_t action = layer_switch_get_action(record->event.key);
158
159     switch (action.kind.id) {
160 #ifdef SWAP_HANDS_ENABLE
161         case ACT_SWAP_HANDS:
162             switch (action.swap.code) {
163                 case OP_SH_TAP_TOGGLE:
164                 default:
165                     swap_hands = !swap_hands;
166                     swap_held = true;
167             }
168             break;
169 #endif
170     }
171 }
172 #endif
173
174 /** \brief Take a key event (key press or key release) and processes it.
175  *
176  * FIXME: Needs documentation.
177  */
178 void process_record(keyrecord_t *record)
179 {
180     if (IS_NOEVENT(record->event)) { return; }
181
182     if(!process_record_quantum(record))
183         return;
184
185     action_t action = store_or_get_action(record->event.pressed, record->event.key);
186     dprint("ACTION: "); debug_action(action);
187 #ifndef NO_ACTION_LAYER
188     dprint(" layer_state: "); layer_debug();
189     dprint(" default_layer_state: "); default_layer_debug();
190 #endif
191     dprintln();
192
193     process_action(record, action);
194 }
195
196 /** \brief Take an action and processes it.
197  *
198  * FIXME: Needs documentation.
199  */
200 void process_action(keyrecord_t *record, action_t action)
201 {
202     keyevent_t event = record->event;
203 #ifndef NO_ACTION_TAPPING
204     uint8_t tap_count = record->tap.count;
205 #endif
206
207     if (event.pressed) {
208         // clear the potential weak mods left by previously pressed keys
209         clear_weak_mods();
210     }
211
212 #ifndef NO_ACTION_ONESHOT
213     bool do_release_oneshot = false;
214     // notice we only clear the one shot layer if the pressed key is not a modifier.
215     if (is_oneshot_layer_active() && event.pressed && !IS_MOD(action.key.code)) {
216         clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
217         do_release_oneshot = !is_oneshot_layer_active();
218     }
219 #endif
220
221     switch (action.kind.id) {
222         /* Key and Mods */
223         case ACT_LMODS:
224         case ACT_RMODS:
225             {
226                 uint8_t mods = (action.kind.id == ACT_LMODS) ?  action.key.mods :
227                                                                 action.key.mods<<4;
228                 if (event.pressed) {
229                     if (mods) {
230                         if (IS_MOD(action.key.code) || action.key.code == KC_NO) {
231                             // e.g. LSFT(KC_LGUI): we don't want the LSFT to be weak as it would make it useless.
232                             // This also makes LSFT(KC_LGUI) behave exactly the same as LGUI(KC_LSFT).
233                             // Same applies for some keys like KC_MEH which are declared as MEH(KC_NO).
234                             add_mods(mods);
235                         } else {
236                             add_weak_mods(mods);
237                         }
238                         send_keyboard_report();
239                     }
240                     register_code(action.key.code);
241                 } else {
242                     unregister_code(action.key.code);
243                     if (mods) {
244                         if (IS_MOD(action.key.code) || action.key.code == KC_NO) {
245                             del_mods(mods);
246                         } else {
247                             del_weak_mods(mods);
248                         }
249                         send_keyboard_report();
250                     }
251                 }
252             }
253             break;
254 #ifndef NO_ACTION_TAPPING
255         case ACT_LMODS_TAP:
256         case ACT_RMODS_TAP:
257             {
258                 uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ?  action.key.mods :
259                                                                     action.key.mods<<4;
260                 switch (action.layer_tap.code) {
261     #ifndef NO_ACTION_ONESHOT
262                     case MODS_ONESHOT:
263                         // Oneshot modifier
264                         if (event.pressed) {
265                             if (tap_count == 0) {
266                                 dprint("MODS_TAP: Oneshot: 0\n");
267                                 register_mods(mods | get_oneshot_mods());
268                             } else if (tap_count == 1) {
269                                 dprint("MODS_TAP: Oneshot: start\n");
270                                 set_oneshot_mods(mods | get_oneshot_mods());
271                     #if defined(ONESHOT_TAP_TOGGLE) && ONESHOT_TAP_TOGGLE > 1
272                             } else if (tap_count == ONESHOT_TAP_TOGGLE) {
273                                 dprint("MODS_TAP: Toggling oneshot");
274                                 clear_oneshot_mods();
275                                 set_oneshot_locked_mods(mods);
276                                 register_mods(mods);
277                     #endif
278                             } else {
279                                 register_mods(mods | get_oneshot_mods());
280                             }
281                         } else {
282                             if (tap_count == 0) {
283                                 clear_oneshot_mods();
284                                 unregister_mods(mods);
285                             } else if (tap_count == 1) {
286                                 // Retain Oneshot mods
287                     #if defined(ONESHOT_TAP_TOGGLE) && ONESHOT_TAP_TOGGLE > 1
288                                 if (mods & get_mods()) {
289                                     clear_oneshot_locked_mods();
290                                     clear_oneshot_mods();
291                                     unregister_mods(mods);
292                                 }
293                             } else if (tap_count == ONESHOT_TAP_TOGGLE) {
294                                 // Toggle Oneshot Layer
295                     #endif
296                             } else {
297                                 clear_oneshot_mods();
298                                 unregister_mods(mods);
299                             }
300                         }
301                         break;
302     #endif
303                     case MODS_TAP_TOGGLE:
304                         if (event.pressed) {
305                             if (tap_count <= TAPPING_TOGGLE) {
306                                 register_mods(mods);
307                             }
308                         } else {
309                             if (tap_count < TAPPING_TOGGLE) {
310                                 unregister_mods(mods);
311                             }
312                         }
313                         break;
314                     default:
315                         if (event.pressed) {
316                             if (tap_count > 0) {
317 #ifndef IGNORE_MOD_TAP_INTERRUPT
318                                 if (record->tap.interrupted) {
319                                     dprint("mods_tap: tap: cancel: add_mods\n");
320                                     // ad hoc: set 0 to cancel tap
321                                     record->tap.count = 0;
322                                     register_mods(mods);
323                                 } else
324 #endif
325                                 {
326                                     dprint("MODS_TAP: Tap: register_code\n");
327                                     register_code(action.key.code);
328                                 }
329                             } else {
330                                 dprint("MODS_TAP: No tap: add_mods\n");
331                                 register_mods(mods);
332                             }
333                         } else {
334                             if (tap_count > 0) {
335                                 dprint("MODS_TAP: Tap: unregister_code\n");
336                                 if (action.layer_tap.code == KC_CAPS) {
337                                   wait_ms(TAP_HOLD_CAPS_DELAY);
338                                 }
339                                 unregister_code(action.key.code);
340                             } else {
341                                 dprint("MODS_TAP: No tap: add_mods\n");
342                                 unregister_mods(mods);
343                             }
344                         }
345                         break;
346                 }
347             }
348             break;
349 #endif
350 #ifdef EXTRAKEY_ENABLE
351         /* other HID usage */
352         case ACT_USAGE:
353             switch (action.usage.page) {
354                 case PAGE_SYSTEM:
355                     if (event.pressed) {
356                         host_system_send(action.usage.code);
357                     } else {
358                         host_system_send(0);
359                     }
360                     break;
361                 case PAGE_CONSUMER:
362                     if (event.pressed) {
363                         host_consumer_send(action.usage.code);
364                     } else {
365                         host_consumer_send(0);
366                     }
367                     break;
368             }
369             break;
370 #endif
371 #ifdef MOUSEKEY_ENABLE
372         /* Mouse key */
373         case ACT_MOUSEKEY:
374             if (event.pressed) {
375                 switch (action.key.code) {
376                     case KC_MS_BTN1:
377                         tp_buttons |= (1<<0);
378                         break;
379                     case KC_MS_BTN2:
380                         tp_buttons |= (1<<1);
381                         break;
382                     case KC_MS_BTN3:
383                         tp_buttons |= (1<<2);
384                         break;
385                     default:
386                         break;
387                 }
388                 mousekey_on(action.key.code);
389                 mousekey_send();
390             } else {
391                 switch (action.key.code) {
392                     case KC_MS_BTN1:
393                         tp_buttons &= ~(1<<0);
394                         break;
395                     case KC_MS_BTN2:
396                         tp_buttons &= ~(1<<1);
397                         break;
398                     case KC_MS_BTN3:
399                         tp_buttons &= ~(1<<2);
400                         break;
401                     default:
402                         break;
403                 }
404                 mousekey_off(action.key.code);
405                 mousekey_send();
406             }
407             break;
408 #endif
409 #ifndef NO_ACTION_LAYER
410         case ACT_LAYER:
411             if (action.layer_bitop.on == 0) {
412                 /* Default Layer Bitwise Operation */
413                 if (!event.pressed) {
414                     uint8_t shift = action.layer_bitop.part*4;
415                     layer_state_t bits = ((layer_state_t)action.layer_bitop.bits)<<shift;
416                     layer_state_t mask = (action.layer_bitop.xbit) ? ~(((layer_state_t)0xf)<<shift) : 0;
417                     switch (action.layer_bitop.op) {
418                         case OP_BIT_AND: default_layer_and(bits | mask); break;
419                         case OP_BIT_OR:  default_layer_or(bits | mask);  break;
420                         case OP_BIT_XOR: default_layer_xor(bits | mask); break;
421                         case OP_BIT_SET: default_layer_set(bits | mask); break;
422                     }
423                 }
424             } else {
425                 /* Layer Bitwise Operation */
426                 if (event.pressed ? (action.layer_bitop.on & ON_PRESS) :
427                                     (action.layer_bitop.on & ON_RELEASE)) {
428                     uint8_t shift = action.layer_bitop.part*4;
429                     layer_state_t bits = ((layer_state_t)action.layer_bitop.bits)<<shift;
430                     layer_state_t mask = (action.layer_bitop.xbit) ? ~(((layer_state_t)0xf)<<shift) : 0;
431                     switch (action.layer_bitop.op) {
432                         case OP_BIT_AND: layer_and(bits | mask); break;
433                         case OP_BIT_OR:  layer_or(bits | mask);  break;
434                         case OP_BIT_XOR: layer_xor(bits | mask); break;
435                         case OP_BIT_SET: layer_state_set(bits | mask); break;
436                     }
437                 }
438             }
439             break;
440     #ifndef NO_ACTION_TAPPING
441         case ACT_LAYER_TAP:
442         case ACT_LAYER_TAP_EXT:
443             switch (action.layer_tap.code) {
444                 case 0xe0 ... 0xef:
445                     /* layer On/Off with modifiers(left only) */
446                     if (event.pressed) {
447                         layer_on(action.layer_tap.val);
448                         register_mods(action.layer_tap.code & 0x0f);
449                     } else {
450                         layer_off(action.layer_tap.val);
451                         unregister_mods(action.layer_tap.code & 0x0f);
452                     }
453                     break;
454                 case OP_TAP_TOGGLE:
455                     /* tap toggle */
456                     if (event.pressed) {
457                         if (tap_count < TAPPING_TOGGLE) {
458                             layer_invert(action.layer_tap.val);
459                         }
460                     } else {
461                         if (tap_count <= TAPPING_TOGGLE) {
462                             layer_invert(action.layer_tap.val);
463                         }
464                     }
465                     break;
466                 case OP_ON_OFF:
467                     event.pressed ? layer_on(action.layer_tap.val) :
468                                     layer_off(action.layer_tap.val);
469                     break;
470                 case OP_OFF_ON:
471                     event.pressed ? layer_off(action.layer_tap.val) :
472                                     layer_on(action.layer_tap.val);
473                     break;
474                 case OP_SET_CLEAR:
475                     event.pressed ? layer_move(action.layer_tap.val) :
476                                     layer_clear();
477                     break;
478             #ifndef NO_ACTION_ONESHOT
479                 case OP_ONESHOT:
480                     // Oneshot modifier
481                 #if defined(ONESHOT_TAP_TOGGLE) && ONESHOT_TAP_TOGGLE > 1
482                     do_release_oneshot = false;
483                     if (event.pressed) {
484                         del_mods(get_oneshot_locked_mods());
485                         if (get_oneshot_layer_state() == ONESHOT_TOGGLED) {
486                             reset_oneshot_layer();
487                             layer_off(action.layer_tap.val);
488                             break;
489                         } else if (tap_count < ONESHOT_TAP_TOGGLE) {
490                             layer_on(action.layer_tap.val);
491                             set_oneshot_layer(action.layer_tap.val, ONESHOT_START);
492                         }
493                     } else {
494                         add_mods(get_oneshot_locked_mods());
495                         if (tap_count >= ONESHOT_TAP_TOGGLE) {
496                             reset_oneshot_layer();
497                             clear_oneshot_locked_mods();
498                             set_oneshot_layer(action.layer_tap.val, ONESHOT_TOGGLED);
499                         } else {
500                             clear_oneshot_layer_state(ONESHOT_PRESSED);
501                         }
502                     }
503                 #else
504                     if (event.pressed) {
505                         layer_on(action.layer_tap.val);
506                         set_oneshot_layer(action.layer_tap.val, ONESHOT_START);
507                     } else {
508                         clear_oneshot_layer_state(ONESHOT_PRESSED);
509                         if (tap_count > 1) {
510                             clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
511                         }
512                     }
513                 #endif
514                     break;
515             #endif
516                 default:
517                     /* tap key */
518                     if (event.pressed) {
519                         if (tap_count > 0) {
520                             dprint("KEYMAP_TAP_KEY: Tap: register_code\n");
521                             register_code(action.layer_tap.code);
522                         } else {
523                             dprint("KEYMAP_TAP_KEY: No tap: On on press\n");
524                             layer_on(action.layer_tap.val);
525                         }
526                     } else {
527                         if (tap_count > 0) {
528                             dprint("KEYMAP_TAP_KEY: Tap: unregister_code\n");
529                             if (action.layer_tap.code == KC_CAPS) {
530                                 wait_ms(TAP_HOLD_CAPS_DELAY);
531                             } else {
532                                 wait_ms(TAP_CODE_DELAY);
533                               }
534                             unregister_code(action.layer_tap.code);
535                         } else {
536                             dprint("KEYMAP_TAP_KEY: No tap: Off on release\n");
537                             layer_off(action.layer_tap.val);
538                         }
539                     }
540                     break;
541             }
542             break;
543     #endif
544 #endif
545         /* Extentions */
546 #ifndef NO_ACTION_MACRO
547         case ACT_MACRO:
548             action_macro_play(action_get_macro(record, action.func.id, action.func.opt));
549             break;
550 #endif
551 #if defined(BACKLIGHT_ENABLE) | defined(LED_MATRIX_ENABLE)
552         case ACT_BACKLIGHT:
553             if (!event.pressed) {
554                 switch (action.backlight.opt) {
555                     case BACKLIGHT_INCREASE:
556                         backlight_increase();
557                         break;
558                     case BACKLIGHT_DECREASE:
559                         backlight_decrease();
560                         break;
561                     case BACKLIGHT_TOGGLE:
562                         backlight_toggle();
563                         break;
564                     case BACKLIGHT_STEP:
565                         backlight_step();
566                         break;
567                     case BACKLIGHT_ON:
568                         backlight_level(BACKLIGHT_LEVELS);
569                         break;
570                     case BACKLIGHT_OFF:
571                         backlight_level(0);
572                         break;
573                 }
574             }
575             break;
576 #endif
577         case ACT_COMMAND:
578             break;
579 #ifdef SWAP_HANDS_ENABLE
580         case ACT_SWAP_HANDS:
581             switch (action.swap.code) {
582                 case OP_SH_TOGGLE:
583                     if (event.pressed) {
584                         swap_hands = !swap_hands;
585                     }
586                     break;
587                 case OP_SH_ON_OFF:
588                     swap_hands = event.pressed;
589                     break;
590                 case OP_SH_OFF_ON:
591                     swap_hands = !event.pressed;
592                     break;
593                 case OP_SH_ON:
594                     if (!event.pressed) {
595                         swap_hands = true;
596                     }
597                     break;
598                 case OP_SH_OFF:
599                     if (!event.pressed) {
600                         swap_hands = false;
601                     }
602                     break;
603     #ifndef NO_ACTION_TAPPING
604                 case OP_SH_TAP_TOGGLE:
605                     /* tap toggle */
606
607                     if (event.pressed) {
608                         if (swap_held) {
609                             swap_held = false;
610                         } else {
611                             swap_hands = !swap_hands;
612                         }
613                     } else {
614                         if (tap_count < TAPPING_TOGGLE) {
615                             swap_hands = !swap_hands;
616                         }
617                     }
618                     break;
619                 default:
620                     /* tap key */
621                     if (tap_count > 0) {
622                         if (swap_held) {
623                             swap_hands = !swap_hands; // undo hold set up in _tap_hint
624                             swap_held = false;
625                         }
626                         if (event.pressed) {
627                             register_code(action.swap.code);
628                         } else {
629                             wait_ms(TAP_CODE_DELAY);
630                             unregister_code(action.swap.code);
631                             *record = (keyrecord_t){}; // hack: reset tap mode
632                         }
633                     } else {
634                         if (swap_held && !event.pressed) {
635                             swap_hands = !swap_hands; // undo hold set up in _tap_hint
636                             swap_held = false;
637                         }
638                     }
639     #endif
640             }
641 #endif
642 #ifndef NO_ACTION_FUNCTION
643         case ACT_FUNCTION:
644             action_function(record, action.func.id, action.func.opt);
645             break;
646 #endif
647         default:
648             break;
649     }
650
651 #ifndef NO_ACTION_LAYER
652     // if this event is a layer action, update the leds
653     switch (action.kind.id) {
654         case ACT_LAYER:
655         #ifndef NO_ACTION_TAPPING
656         case ACT_LAYER_TAP:
657         case ACT_LAYER_TAP_EXT:
658         #endif
659             led_set(host_keyboard_leds());
660             break;
661         default:
662             break;
663     }
664 #endif
665
666 #ifndef NO_ACTION_TAPPING
667   #ifdef RETRO_TAPPING
668   if (!is_tap_action(action)) {
669     retro_tapping_counter = 0;
670   } else {
671     if (event.pressed) {
672         if (tap_count > 0) {
673           retro_tapping_counter = 0;
674         } else {
675
676         }
677     } else {
678       if (tap_count > 0) {
679         retro_tapping_counter = 0;
680       } else {
681         if (retro_tapping_counter == 2) {
682           tap_code(action.layer_tap.code);
683         }
684         retro_tapping_counter = 0;
685       }
686     }
687   }
688   #endif
689 #endif
690
691 #ifndef NO_ACTION_ONESHOT
692     /* Because we switch layers after a oneshot event, we need to release the
693      * key before we leave the layer or no key up event will be generated.
694      */
695     if (do_release_oneshot && !(get_oneshot_layer_state() & ONESHOT_PRESSED )   ) {
696         record->event.pressed = false;
697         layer_on(get_oneshot_layer());
698         process_record(record);
699         layer_off(get_oneshot_layer());
700     }
701 #endif
702 }
703
704
705
706
707 /** \brief Utilities for actions. (FIXME: Needs better description)
708  *
709  * FIXME: Needs documentation.
710  */
711 void register_code(uint8_t code)
712 {
713     if (code == KC_NO) {
714         return;
715     }
716
717 #ifdef LOCKING_SUPPORT_ENABLE
718     else if (KC_LOCKING_CAPS == code) {
719 #ifdef LOCKING_RESYNC_ENABLE
720         // Resync: ignore if caps lock already is on
721         if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) return;
722 #endif
723         add_key(KC_CAPSLOCK);
724         send_keyboard_report();
725         wait_ms(100);
726         del_key(KC_CAPSLOCK);
727         send_keyboard_report();
728     }
729
730     else if (KC_LOCKING_NUM == code) {
731 #ifdef LOCKING_RESYNC_ENABLE
732         if (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK)) return;
733 #endif
734         add_key(KC_NUMLOCK);
735         send_keyboard_report();
736         wait_ms(100);
737         del_key(KC_NUMLOCK);
738         send_keyboard_report();
739     }
740
741     else if (KC_LOCKING_SCROLL == code) {
742 #ifdef LOCKING_RESYNC_ENABLE
743         if (host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK)) return;
744 #endif
745         add_key(KC_SCROLLLOCK);
746         send_keyboard_report();
747         wait_ms(100);
748         del_key(KC_SCROLLLOCK);
749         send_keyboard_report();
750     }
751 #endif
752
753     else if IS_KEY(code) {
754         // TODO: should push command_proc out of this block?
755         if (command_proc(code)) return;
756
757 #ifndef NO_ACTION_ONESHOT
758 /* TODO: remove
759         if (oneshot_state.mods && !oneshot_state.disabled) {
760             uint8_t tmp_mods = get_mods();
761             add_mods(oneshot_state.mods);
762
763             add_key(code);
764             send_keyboard_report();
765
766             set_mods(tmp_mods);
767             send_keyboard_report();
768             oneshot_cancel();
769         } else
770 */
771 #endif
772         {
773             add_key(code);
774             send_keyboard_report();
775         }
776     }
777     else if IS_MOD(code) {
778         add_mods(MOD_BIT(code));
779         send_keyboard_report();
780     }
781     else if IS_SYSTEM(code) {
782         host_system_send(KEYCODE2SYSTEM(code));
783     }
784     else if IS_CONSUMER(code) {
785         host_consumer_send(KEYCODE2CONSUMER(code));
786     }
787
788     #ifdef MOUSEKEY_ENABLE
789       else if IS_MOUSEKEY(code) {
790         mousekey_on(code);
791         mousekey_send();
792       }
793     #endif
794 }
795
796 /** \brief Utilities for actions. (FIXME: Needs better description)
797  *
798  * FIXME: Needs documentation.
799  */
800 void unregister_code(uint8_t code)
801 {
802     if (code == KC_NO) {
803         return;
804     }
805
806 #ifdef LOCKING_SUPPORT_ENABLE
807     else if (KC_LOCKING_CAPS == code) {
808 #ifdef LOCKING_RESYNC_ENABLE
809         // Resync: ignore if caps lock already is off
810         if (!(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK))) return;
811 #endif
812         add_key(KC_CAPSLOCK);
813         send_keyboard_report();
814         del_key(KC_CAPSLOCK);
815         send_keyboard_report();
816     }
817
818     else if (KC_LOCKING_NUM == code) {
819 #ifdef LOCKING_RESYNC_ENABLE
820         if (!(host_keyboard_leds() & (1<<USB_LED_NUM_LOCK))) return;
821 #endif
822         add_key(KC_NUMLOCK);
823         send_keyboard_report();
824         del_key(KC_NUMLOCK);
825         send_keyboard_report();
826     }
827
828     else if (KC_LOCKING_SCROLL == code) {
829 #ifdef LOCKING_RESYNC_ENABLE
830         if (!(host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK))) return;
831 #endif
832         add_key(KC_SCROLLLOCK);
833         send_keyboard_report();
834         del_key(KC_SCROLLLOCK);
835         send_keyboard_report();
836     }
837 #endif
838
839     else if IS_KEY(code) {
840         del_key(code);
841         send_keyboard_report();
842     }
843     else if IS_MOD(code) {
844         del_mods(MOD_BIT(code));
845         send_keyboard_report();
846     }
847     else if IS_SYSTEM(code) {
848         host_system_send(0);
849     }
850     else if IS_CONSUMER(code) {
851         host_consumer_send(0);
852     }
853     #ifdef MOUSEKEY_ENABLE
854       else if IS_MOUSEKEY(code) {
855         mousekey_off(code);
856         mousekey_send();
857       }
858     #endif
859 }
860
861 /** \brief Utilities for actions. (FIXME: Needs better description)
862  *
863  * FIXME: Needs documentation.
864  */
865 void tap_code(uint8_t code) {
866   register_code(code);
867   if (code == KC_CAPS) {
868     wait_ms(TAP_HOLD_CAPS_DELAY);
869   } else {
870     wait_ms(TAP_CODE_DELAY);
871   }
872   unregister_code(code);
873 }
874
875 /** \brief Utilities for actions. (FIXME: Needs better description)
876  *
877  * FIXME: Needs documentation.
878  */
879 void register_mods(uint8_t mods)
880 {
881     if (mods) {
882         add_mods(mods);
883         send_keyboard_report();
884     }
885 }
886
887 /** \brief Utilities for actions. (FIXME: Needs better description)
888  *
889  * FIXME: Needs documentation.
890  */
891 void unregister_mods(uint8_t mods)
892 {
893     if (mods) {
894         del_mods(mods);
895         send_keyboard_report();
896     }
897 }
898
899 /** \brief Utilities for actions. (FIXME: Needs better description)
900  *
901  * FIXME: Needs documentation.
902  */
903 void clear_keyboard(void)
904 {
905     clear_mods();
906     clear_keyboard_but_mods();
907 }
908
909 /** \brief Utilities for actions. (FIXME: Needs better description)
910  *
911  * FIXME: Needs documentation.
912  */
913 void clear_keyboard_but_mods(void)
914 {
915     clear_keys();
916     clear_keyboard_but_mods_and_keys();
917 }
918
919 /** \brief Utilities for actions. (FIXME: Needs better description)
920  *
921  * FIXME: Needs documentation.
922  */
923 void clear_keyboard_but_mods_and_keys()
924 {
925     clear_weak_mods();
926     clear_macro_mods();
927     send_keyboard_report();
928 #ifdef MOUSEKEY_ENABLE
929     mousekey_clear();
930     mousekey_send();
931 #endif
932 #ifdef EXTRAKEY_ENABLE
933     host_system_send(0);
934     host_consumer_send(0);
935 #endif
936 }
937
938 /** \brief Utilities for actions. (FIXME: Needs better description)
939  *
940  * FIXME: Needs documentation.
941  */
942 bool is_tap_key(keypos_t key)
943 {
944     action_t action = layer_switch_get_action(key);
945     return is_tap_action(action);
946 }
947
948 /** \brief Utilities for actions. (FIXME: Needs better description)
949  *
950  * FIXME: Needs documentation.
951  */
952 bool is_tap_action(action_t action)
953 {
954     switch (action.kind.id) {
955         case ACT_LMODS_TAP:
956         case ACT_RMODS_TAP:
957         case ACT_LAYER_TAP:
958         case ACT_LAYER_TAP_EXT:
959             switch (action.layer_tap.code) {
960                 case 0x00 ... 0xdf:
961                 case OP_TAP_TOGGLE:
962                 case OP_ONESHOT:
963                     return true;
964             }
965             return false;
966         case ACT_SWAP_HANDS:
967             switch (action.swap.code) {
968                 case 0x00 ... 0xdf:
969                 case OP_SH_TAP_TOGGLE:
970                     return true;
971             }
972             return false;
973         case ACT_MACRO:
974         case ACT_FUNCTION:
975             if (action.func.opt & FUNC_TAP) { return true; }
976             return false;
977     }
978     return false;
979 }
980
981
982 /** \brief Debug print (FIXME: Needs better description)
983  *
984  * FIXME: Needs documentation.
985  */
986 void debug_event(keyevent_t event)
987 {
988     dprintf("%04X%c(%u)", (event.key.row<<8 | event.key.col), (event.pressed ? 'd' : 'u'), event.time);
989 }
990
991 /** \brief Debug print (FIXME: Needs better description)
992  *
993  * FIXME: Needs documentation.
994  */
995 void debug_record(keyrecord_t record)
996 {
997     debug_event(record.event);
998 #ifndef NO_ACTION_TAPPING
999     dprintf(":%u%c", record.tap.count, (record.tap.interrupted ? '-' : ' '));
1000 #endif
1001 }
1002
1003 /** \brief Debug print (FIXME: Needs better description)
1004  *
1005  * FIXME: Needs documentation.
1006  */
1007 void debug_action(action_t action)
1008 {
1009     switch (action.kind.id) {
1010         case ACT_LMODS:             dprint("ACT_LMODS");             break;
1011         case ACT_RMODS:             dprint("ACT_RMODS");             break;
1012         case ACT_LMODS_TAP:         dprint("ACT_LMODS_TAP");         break;
1013         case ACT_RMODS_TAP:         dprint("ACT_RMODS_TAP");         break;
1014         case ACT_USAGE:             dprint("ACT_USAGE");             break;
1015         case ACT_MOUSEKEY:          dprint("ACT_MOUSEKEY");          break;
1016         case ACT_LAYER:             dprint("ACT_LAYER");             break;
1017         case ACT_LAYER_TAP:         dprint("ACT_LAYER_TAP");         break;
1018         case ACT_LAYER_TAP_EXT:     dprint("ACT_LAYER_TAP_EXT");     break;
1019         case ACT_MACRO:             dprint("ACT_MACRO");             break;
1020         case ACT_COMMAND:           dprint("ACT_COMMAND");           break;
1021         case ACT_FUNCTION:          dprint("ACT_FUNCTION");          break;
1022         case ACT_SWAP_HANDS:        dprint("ACT_SWAP_HANDS");        break;
1023         default:                    dprint("UNKNOWN");               break;
1024     }
1025     dprintf("[%X:%02X]", action.kind.param>>8, action.kind.param&0xff);
1026 }