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