]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/common/action.c
document how to define LEFT_LEDS and how that hack is done
[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         wait_ms(100);        
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         wait_ms(100);        
634         del_key(KC_NUMLOCK);
635         send_keyboard_report();
636     }
637
638     else if (KC_LOCKING_SCROLL == code) {
639 #ifdef LOCKING_RESYNC_ENABLE
640         if (host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK)) return;
641 #endif
642         add_key(KC_SCROLLLOCK);
643         send_keyboard_report();
644         wait_ms(100);        
645         del_key(KC_SCROLLLOCK);
646         send_keyboard_report();
647     }
648 #endif
649
650     else if IS_KEY(code) {
651         // TODO: should push command_proc out of this block?
652         if (command_proc(code)) return;
653
654 #ifndef NO_ACTION_ONESHOT
655 /* TODO: remove
656         if (oneshot_state.mods && !oneshot_state.disabled) {
657             uint8_t tmp_mods = get_mods();
658             add_mods(oneshot_state.mods);
659
660             add_key(code);
661             send_keyboard_report();
662
663             set_mods(tmp_mods);
664             send_keyboard_report();
665             oneshot_cancel();
666         } else
667 */
668 #endif
669         {
670             add_key(code);
671             send_keyboard_report();
672         }
673     }
674     else if IS_MOD(code) {
675         add_mods(MOD_BIT(code));
676         send_keyboard_report();
677     }
678     else if IS_SYSTEM(code) {
679         host_system_send(KEYCODE2SYSTEM(code));
680     }
681     else if IS_CONSUMER(code) {
682         host_consumer_send(KEYCODE2CONSUMER(code));
683     }
684 }
685
686 void unregister_code(uint8_t code)
687 {
688     if (code == KC_NO) {
689         return;
690     }
691
692 #ifdef LOCKING_SUPPORT_ENABLE
693     else if (KC_LOCKING_CAPS == code) {
694 #ifdef LOCKING_RESYNC_ENABLE
695         // Resync: ignore if caps lock already is off
696         if (!(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK))) return;
697 #endif
698         add_key(KC_CAPSLOCK);
699         send_keyboard_report();
700         del_key(KC_CAPSLOCK);
701         send_keyboard_report();
702     }
703
704     else if (KC_LOCKING_NUM == code) {
705 #ifdef LOCKING_RESYNC_ENABLE
706         if (!(host_keyboard_leds() & (1<<USB_LED_NUM_LOCK))) return;
707 #endif
708         add_key(KC_NUMLOCK);
709         send_keyboard_report();
710         del_key(KC_NUMLOCK);
711         send_keyboard_report();
712     }
713
714     else if (KC_LOCKING_SCROLL == code) {
715 #ifdef LOCKING_RESYNC_ENABLE
716         if (!(host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK))) return;
717 #endif
718         add_key(KC_SCROLLLOCK);
719         send_keyboard_report();
720         del_key(KC_SCROLLLOCK);
721         send_keyboard_report();
722     }
723 #endif
724
725     else if IS_KEY(code) {
726         del_key(code);
727         send_keyboard_report();
728     }
729     else if IS_MOD(code) {
730         del_mods(MOD_BIT(code));
731         send_keyboard_report();
732     }
733     else if IS_SYSTEM(code) {
734         host_system_send(0);
735     }
736     else if IS_CONSUMER(code) {
737         host_consumer_send(0);
738     }
739 }
740
741 void register_mods(uint8_t mods)
742 {
743     if (mods) {
744         add_mods(mods);
745         send_keyboard_report();
746     }
747 }
748
749 void unregister_mods(uint8_t mods)
750 {
751     if (mods) {
752         del_mods(mods);
753         send_keyboard_report();
754     }
755 }
756
757 void clear_keyboard(void)
758 {
759     clear_mods();
760     clear_keyboard_but_mods();
761 }
762
763 void clear_keyboard_but_mods(void)
764 {
765     clear_weak_mods();
766     clear_macro_mods();
767     clear_keys();
768     send_keyboard_report();
769 #ifdef MOUSEKEY_ENABLE
770     mousekey_clear();
771     mousekey_send();
772 #endif
773 #ifdef EXTRAKEY_ENABLE
774     host_system_send(0);
775     host_consumer_send(0);
776 #endif
777 }
778
779 bool is_tap_key(keypos_t key)
780 {
781     action_t action = layer_switch_get_action(key);
782
783     switch (action.kind.id) {
784         case ACT_LMODS_TAP:
785         case ACT_RMODS_TAP:
786         case ACT_LAYER_TAP:
787         case ACT_LAYER_TAP_EXT:
788             switch (action.layer_tap.code) {
789                 case 0x00 ... 0xdf:
790                 case OP_TAP_TOGGLE:
791                 case OP_ONESHOT:
792                     return true;
793             }
794             return false;
795         case ACT_SWAP_HANDS:
796             switch (action.swap.code) {
797                 case 0x00 ... 0xdf:
798                 case OP_SH_TAP_TOGGLE:
799                     return true;
800             }
801             return false;
802         case ACT_MACRO:
803         case ACT_FUNCTION:
804             if (action.func.opt & FUNC_TAP) { return true; }
805             return false;
806     }
807     return false;
808 }
809
810
811 /*
812  * debug print
813  */
814 void debug_event(keyevent_t event)
815 {
816     dprintf("%04X%c(%u)", (event.key.row<<8 | event.key.col), (event.pressed ? 'd' : 'u'), event.time);
817 }
818
819 void debug_record(keyrecord_t record)
820 {
821     debug_event(record.event);
822 #ifndef NO_ACTION_TAPPING
823     dprintf(":%u%c", record.tap.count, (record.tap.interrupted ? '-' : ' '));
824 #endif
825 }
826
827 void debug_action(action_t action)
828 {
829     switch (action.kind.id) {
830         case ACT_LMODS:             dprint("ACT_LMODS");             break;
831         case ACT_RMODS:             dprint("ACT_RMODS");             break;
832         case ACT_LMODS_TAP:         dprint("ACT_LMODS_TAP");         break;
833         case ACT_RMODS_TAP:         dprint("ACT_RMODS_TAP");         break;
834         case ACT_USAGE:             dprint("ACT_USAGE");             break;
835         case ACT_MOUSEKEY:          dprint("ACT_MOUSEKEY");          break;
836         case ACT_LAYER:             dprint("ACT_LAYER");             break;
837         case ACT_LAYER_TAP:         dprint("ACT_LAYER_TAP");         break;
838         case ACT_LAYER_TAP_EXT:     dprint("ACT_LAYER_TAP_EXT");     break;
839         case ACT_MACRO:             dprint("ACT_MACRO");             break;
840         case ACT_COMMAND:           dprint("ACT_COMMAND");           break;
841         case ACT_FUNCTION:          dprint("ACT_FUNCTION");          break;
842         case ACT_SWAP_HANDS:        dprint("ACT_SWAP_HANDS");        break;
843         default:                    dprint("UNKNOWN");               break;
844     }
845     dprintf("[%X:%02X]", action.kind.param>>8, action.kind.param&0xff);
846 }