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