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