2 Copyright 2012,2013 Jun Wako <wakojun@gmail.com>
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.
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.
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/>.
23 #include "backlight.h"
24 #include "action_layer.h"
25 #include "action_tapping.h"
26 #include "action_macro.h"
27 #include "action_util.h"
40 int retro_tapping_counter = 0;
43 #ifdef FAUXCLICKY_ENABLE
44 #include <fauxclicky.h>
47 #ifndef TAP_CODE_DELAY
48 # define TAP_CODE_DELAY 0
50 #ifndef TAP_HOLD_CAPS_DELAY
51 # define TAP_HOLD_CAPS_DELAY 80
53 /** \brief Called to execute an action.
55 * FIXME: Needs documentation.
57 void action_exec(keyevent_t event)
59 if (!IS_NOEVENT(event)) {
60 dprint("\n---- action_exec: start -----\n");
61 dprint("EVENT: "); debug_event(event); dprintln();
63 retro_tapping_counter++;
67 #ifdef FAUXCLICKY_ENABLE
68 if (IS_PRESSED(event)) {
69 FAUXCLICKY_ACTION_PRESS;
71 if (IS_RELEASED(event)) {
72 FAUXCLICKY_ACTION_RELEASE;
77 #ifdef SWAP_HANDS_ENABLE
78 if (!IS_NOEVENT(event)) {
79 process_hand_swap(&event);
83 keyrecord_t record = { .event = event };
85 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
86 if (has_oneshot_layer_timed_out()) {
87 clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
89 if (has_oneshot_mods_timed_out()) {
94 #ifndef NO_ACTION_TAPPING
95 action_tapping_process(record);
97 process_record(&record);
98 if (!IS_NOEVENT(record.event)) {
99 dprint("processed: "); debug_record(record); dprintln();
104 #ifdef SWAP_HANDS_ENABLE
105 bool swap_hands = false;
106 bool swap_held = false;
108 /** \brief Process Hand Swap
110 * FIXME: Needs documentation.
112 void process_hand_swap(keyevent_t *event) {
113 static swap_state_row_t swap_state[MATRIX_ROWS];
115 keypos_t pos = event->key;
116 swap_state_row_t col_bit = (swap_state_row_t)1<<pos.col;
117 bool do_swap = event->pressed ? swap_hands :
118 swap_state[pos.row] & (col_bit);
121 event->key = hand_swap_config[pos.row][pos.col];
122 swap_state[pos.row] |= col_bit;
124 swap_state[pos.row] &= ~(col_bit);
129 #if !defined(NO_ACTION_LAYER) && !defined(STRICT_LAYER_RELEASE)
130 bool disable_action_cache = false;
132 void process_record_nocache(keyrecord_t *record)
134 disable_action_cache = true;
135 process_record(record);
136 disable_action_cache = false;
139 void process_record_nocache(keyrecord_t *record)
141 process_record(record);
145 __attribute__ ((weak))
146 bool process_record_quantum(keyrecord_t *record) {
150 #ifndef NO_ACTION_TAPPING
151 /** \brief Allows for handling tap-hold actions immediately instead of waiting for TAPPING_TERM or another keypress.
153 * FIXME: Needs documentation.
155 void process_record_tap_hint(keyrecord_t *record)
157 action_t action = layer_switch_get_action(record->event.key);
159 switch (action.kind.id) {
160 #ifdef SWAP_HANDS_ENABLE
162 switch (action.swap.code) {
163 case OP_SH_TAP_TOGGLE:
165 swap_hands = !swap_hands;
174 /** \brief Take a key event (key press or key release) and processes it.
176 * FIXME: Needs documentation.
178 void process_record(keyrecord_t *record)
180 if (IS_NOEVENT(record->event)) { return; }
182 if(!process_record_quantum(record))
185 action_t action = store_or_get_action(record->event.pressed, record->event.key);
186 dprint("ACTION: "); debug_action(action);
187 #ifndef NO_ACTION_LAYER
188 dprint(" layer_state: "); layer_debug();
189 dprint(" default_layer_state: "); default_layer_debug();
193 process_action(record, action);
196 /** \brief Take an action and processes it.
198 * FIXME: Needs documentation.
200 void process_action(keyrecord_t *record, action_t action)
202 keyevent_t event = record->event;
203 #ifndef NO_ACTION_TAPPING
204 uint8_t tap_count = record->tap.count;
208 // clear the potential weak mods left by previously pressed keys
212 #ifndef NO_ACTION_ONESHOT
213 bool do_release_oneshot = false;
214 // notice we only clear the one shot layer if the pressed key is not a modifier.
215 if (is_oneshot_layer_active() && event.pressed && !IS_MOD(action.key.code)) {
216 clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
217 do_release_oneshot = !is_oneshot_layer_active();
221 switch (action.kind.id) {
226 uint8_t mods = (action.kind.id == ACT_LMODS) ? action.key.mods :
230 if (IS_MOD(action.key.code) || action.key.code == KC_NO) {
231 // e.g. LSFT(KC_LGUI): we don't want the LSFT to be weak as it would make it useless.
232 // This also makes LSFT(KC_LGUI) behave exactly the same as LGUI(KC_LSFT).
233 // Same applies for some keys like KC_MEH which are declared as MEH(KC_NO).
238 send_keyboard_report();
240 register_code(action.key.code);
242 unregister_code(action.key.code);
244 if (IS_MOD(action.key.code) || action.key.code == KC_NO) {
249 send_keyboard_report();
254 #ifndef NO_ACTION_TAPPING
258 uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods :
260 switch (action.layer_tap.code) {
261 #ifndef NO_ACTION_ONESHOT
265 if (tap_count == 0) {
266 dprint("MODS_TAP: Oneshot: 0\n");
267 register_mods(mods | get_oneshot_mods());
268 } else if (tap_count == 1) {
269 dprint("MODS_TAP: Oneshot: start\n");
270 set_oneshot_mods(mods | get_oneshot_mods());
271 #if defined(ONESHOT_TAP_TOGGLE) && ONESHOT_TAP_TOGGLE > 1
272 } else if (tap_count == ONESHOT_TAP_TOGGLE) {
273 dprint("MODS_TAP: Toggling oneshot");
274 clear_oneshot_mods();
275 set_oneshot_locked_mods(mods);
279 register_mods(mods | get_oneshot_mods());
282 if (tap_count == 0) {
283 clear_oneshot_mods();
284 unregister_mods(mods);
285 } else if (tap_count == 1) {
286 // Retain Oneshot mods
287 #if defined(ONESHOT_TAP_TOGGLE) && ONESHOT_TAP_TOGGLE > 1
288 if (mods & get_mods()) {
289 clear_oneshot_locked_mods();
290 clear_oneshot_mods();
291 unregister_mods(mods);
293 } else if (tap_count == ONESHOT_TAP_TOGGLE) {
294 // Toggle Oneshot Layer
297 clear_oneshot_mods();
298 unregister_mods(mods);
303 case MODS_TAP_TOGGLE:
305 if (tap_count <= TAPPING_TOGGLE) {
309 if (tap_count < TAPPING_TOGGLE) {
310 unregister_mods(mods);
317 #ifndef IGNORE_MOD_TAP_INTERRUPT
318 if (record->tap.interrupted) {
319 dprint("mods_tap: tap: cancel: add_mods\n");
320 // ad hoc: set 0 to cancel tap
321 record->tap.count = 0;
326 dprint("MODS_TAP: Tap: register_code\n");
327 register_code(action.key.code);
330 dprint("MODS_TAP: No tap: add_mods\n");
335 dprint("MODS_TAP: Tap: unregister_code\n");
336 if (action.layer_tap.code == KC_CAPS) {
337 wait_ms(TAP_HOLD_CAPS_DELAY);
339 unregister_code(action.key.code);
341 dprint("MODS_TAP: No tap: add_mods\n");
342 unregister_mods(mods);
350 #ifdef EXTRAKEY_ENABLE
351 /* other HID usage */
353 switch (action.usage.page) {
356 host_system_send(action.usage.code);
363 host_consumer_send(action.usage.code);
365 host_consumer_send(0);
371 #ifdef MOUSEKEY_ENABLE
375 switch (action.key.code) {
377 tp_buttons |= (1<<0);
380 tp_buttons |= (1<<1);
383 tp_buttons |= (1<<2);
388 mousekey_on(action.key.code);
391 switch (action.key.code) {
393 tp_buttons &= ~(1<<0);
396 tp_buttons &= ~(1<<1);
399 tp_buttons &= ~(1<<2);
404 mousekey_off(action.key.code);
409 #ifndef NO_ACTION_LAYER
411 if (action.layer_bitop.on == 0) {
412 /* Default Layer Bitwise Operation */
413 if (!event.pressed) {
414 uint8_t shift = action.layer_bitop.part*4;
415 layer_state_t bits = ((layer_state_t)action.layer_bitop.bits)<<shift;
416 layer_state_t mask = (action.layer_bitop.xbit) ? ~(((layer_state_t)0xf)<<shift) : 0;
417 switch (action.layer_bitop.op) {
418 case OP_BIT_AND: default_layer_and(bits | mask); break;
419 case OP_BIT_OR: default_layer_or(bits | mask); break;
420 case OP_BIT_XOR: default_layer_xor(bits | mask); break;
421 case OP_BIT_SET: default_layer_set(bits | mask); break;
425 /* Layer Bitwise Operation */
426 if (event.pressed ? (action.layer_bitop.on & ON_PRESS) :
427 (action.layer_bitop.on & ON_RELEASE)) {
428 uint8_t shift = action.layer_bitop.part*4;
429 layer_state_t bits = ((layer_state_t)action.layer_bitop.bits)<<shift;
430 layer_state_t mask = (action.layer_bitop.xbit) ? ~(((layer_state_t)0xf)<<shift) : 0;
431 switch (action.layer_bitop.op) {
432 case OP_BIT_AND: layer_and(bits | mask); break;
433 case OP_BIT_OR: layer_or(bits | mask); break;
434 case OP_BIT_XOR: layer_xor(bits | mask); break;
435 case OP_BIT_SET: layer_state_set(bits | mask); break;
440 #ifndef NO_ACTION_TAPPING
442 case ACT_LAYER_TAP_EXT:
443 switch (action.layer_tap.code) {
445 /* layer On/Off with modifiers(left only) */
447 layer_on(action.layer_tap.val);
448 register_mods(action.layer_tap.code & 0x0f);
450 layer_off(action.layer_tap.val);
451 unregister_mods(action.layer_tap.code & 0x0f);
457 if (tap_count < TAPPING_TOGGLE) {
458 layer_invert(action.layer_tap.val);
461 if (tap_count <= TAPPING_TOGGLE) {
462 layer_invert(action.layer_tap.val);
467 event.pressed ? layer_on(action.layer_tap.val) :
468 layer_off(action.layer_tap.val);
471 event.pressed ? layer_off(action.layer_tap.val) :
472 layer_on(action.layer_tap.val);
475 event.pressed ? layer_move(action.layer_tap.val) :
478 #ifndef NO_ACTION_ONESHOT
481 #if defined(ONESHOT_TAP_TOGGLE) && ONESHOT_TAP_TOGGLE > 1
482 do_release_oneshot = false;
484 del_mods(get_oneshot_locked_mods());
485 if (get_oneshot_layer_state() == ONESHOT_TOGGLED) {
486 reset_oneshot_layer();
487 layer_off(action.layer_tap.val);
489 } else if (tap_count < ONESHOT_TAP_TOGGLE) {
490 layer_on(action.layer_tap.val);
491 set_oneshot_layer(action.layer_tap.val, ONESHOT_START);
494 add_mods(get_oneshot_locked_mods());
495 if (tap_count >= ONESHOT_TAP_TOGGLE) {
496 reset_oneshot_layer();
497 clear_oneshot_locked_mods();
498 set_oneshot_layer(action.layer_tap.val, ONESHOT_TOGGLED);
500 clear_oneshot_layer_state(ONESHOT_PRESSED);
505 layer_on(action.layer_tap.val);
506 set_oneshot_layer(action.layer_tap.val, ONESHOT_START);
508 clear_oneshot_layer_state(ONESHOT_PRESSED);
510 clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
520 dprint("KEYMAP_TAP_KEY: Tap: register_code\n");
521 register_code(action.layer_tap.code);
523 dprint("KEYMAP_TAP_KEY: No tap: On on press\n");
524 layer_on(action.layer_tap.val);
528 dprint("KEYMAP_TAP_KEY: Tap: unregister_code\n");
529 if (action.layer_tap.code == KC_CAPS) {
530 wait_ms(TAP_HOLD_CAPS_DELAY);
532 wait_ms(TAP_CODE_DELAY);
534 unregister_code(action.layer_tap.code);
536 dprint("KEYMAP_TAP_KEY: No tap: Off on release\n");
537 layer_off(action.layer_tap.val);
546 #ifndef NO_ACTION_MACRO
548 action_macro_play(action_get_macro(record, action.func.id, action.func.opt));
551 #if defined(BACKLIGHT_ENABLE) | defined(LED_MATRIX_ENABLE)
553 if (!event.pressed) {
554 switch (action.backlight.opt) {
555 case BACKLIGHT_INCREASE:
556 backlight_increase();
558 case BACKLIGHT_DECREASE:
559 backlight_decrease();
561 case BACKLIGHT_TOGGLE:
568 backlight_level(BACKLIGHT_LEVELS);
579 #ifdef SWAP_HANDS_ENABLE
581 switch (action.swap.code) {
584 swap_hands = !swap_hands;
588 swap_hands = event.pressed;
591 swap_hands = !event.pressed;
594 if (!event.pressed) {
599 if (!event.pressed) {
603 #ifndef NO_ACTION_TAPPING
604 case OP_SH_TAP_TOGGLE:
611 swap_hands = !swap_hands;
614 if (tap_count < TAPPING_TOGGLE) {
615 swap_hands = !swap_hands;
623 swap_hands = !swap_hands; // undo hold set up in _tap_hint
627 register_code(action.swap.code);
629 wait_ms(TAP_CODE_DELAY);
630 unregister_code(action.swap.code);
631 *record = (keyrecord_t){}; // hack: reset tap mode
634 if (swap_held && !event.pressed) {
635 swap_hands = !swap_hands; // undo hold set up in _tap_hint
642 #ifndef NO_ACTION_FUNCTION
644 action_function(record, action.func.id, action.func.opt);
651 #ifndef NO_ACTION_LAYER
652 // if this event is a layer action, update the leds
653 switch (action.kind.id) {
655 #ifndef NO_ACTION_TAPPING
657 case ACT_LAYER_TAP_EXT:
659 led_set(host_keyboard_leds());
666 #ifndef NO_ACTION_TAPPING
668 if (!is_tap_action(action)) {
669 retro_tapping_counter = 0;
673 retro_tapping_counter = 0;
679 retro_tapping_counter = 0;
681 if (retro_tapping_counter == 2) {
682 tap_code(action.layer_tap.code);
684 retro_tapping_counter = 0;
691 #ifndef NO_ACTION_ONESHOT
692 /* Because we switch layers after a oneshot event, we need to release the
693 * key before we leave the layer or no key up event will be generated.
695 if (do_release_oneshot && !(get_oneshot_layer_state() & ONESHOT_PRESSED ) ) {
696 record->event.pressed = false;
697 layer_on(get_oneshot_layer());
698 process_record(record);
699 layer_off(get_oneshot_layer());
707 /** \brief Utilities for actions. (FIXME: Needs better description)
709 * FIXME: Needs documentation.
711 void register_code(uint8_t code)
717 #ifdef LOCKING_SUPPORT_ENABLE
718 else if (KC_LOCKING_CAPS == code) {
719 #ifdef LOCKING_RESYNC_ENABLE
720 // Resync: ignore if caps lock already is on
721 if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) return;
723 add_key(KC_CAPSLOCK);
724 send_keyboard_report();
726 del_key(KC_CAPSLOCK);
727 send_keyboard_report();
730 else if (KC_LOCKING_NUM == code) {
731 #ifdef LOCKING_RESYNC_ENABLE
732 if (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK)) return;
735 send_keyboard_report();
738 send_keyboard_report();
741 else if (KC_LOCKING_SCROLL == code) {
742 #ifdef LOCKING_RESYNC_ENABLE
743 if (host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK)) return;
745 add_key(KC_SCROLLLOCK);
746 send_keyboard_report();
748 del_key(KC_SCROLLLOCK);
749 send_keyboard_report();
753 else if IS_KEY(code) {
754 // TODO: should push command_proc out of this block?
755 if (command_proc(code)) return;
757 #ifndef NO_ACTION_ONESHOT
759 if (oneshot_state.mods && !oneshot_state.disabled) {
760 uint8_t tmp_mods = get_mods();
761 add_mods(oneshot_state.mods);
764 send_keyboard_report();
767 send_keyboard_report();
774 send_keyboard_report();
777 else if IS_MOD(code) {
778 add_mods(MOD_BIT(code));
779 send_keyboard_report();
781 else if IS_SYSTEM(code) {
782 host_system_send(KEYCODE2SYSTEM(code));
784 else if IS_CONSUMER(code) {
785 host_consumer_send(KEYCODE2CONSUMER(code));
788 #ifdef MOUSEKEY_ENABLE
789 else if IS_MOUSEKEY(code) {
796 /** \brief Utilities for actions. (FIXME: Needs better description)
798 * FIXME: Needs documentation.
800 void unregister_code(uint8_t code)
806 #ifdef LOCKING_SUPPORT_ENABLE
807 else if (KC_LOCKING_CAPS == code) {
808 #ifdef LOCKING_RESYNC_ENABLE
809 // Resync: ignore if caps lock already is off
810 if (!(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK))) return;
812 add_key(KC_CAPSLOCK);
813 send_keyboard_report();
814 del_key(KC_CAPSLOCK);
815 send_keyboard_report();
818 else if (KC_LOCKING_NUM == code) {
819 #ifdef LOCKING_RESYNC_ENABLE
820 if (!(host_keyboard_leds() & (1<<USB_LED_NUM_LOCK))) return;
823 send_keyboard_report();
825 send_keyboard_report();
828 else if (KC_LOCKING_SCROLL == code) {
829 #ifdef LOCKING_RESYNC_ENABLE
830 if (!(host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK))) return;
832 add_key(KC_SCROLLLOCK);
833 send_keyboard_report();
834 del_key(KC_SCROLLLOCK);
835 send_keyboard_report();
839 else if IS_KEY(code) {
841 send_keyboard_report();
843 else if IS_MOD(code) {
844 del_mods(MOD_BIT(code));
845 send_keyboard_report();
847 else if IS_SYSTEM(code) {
850 else if IS_CONSUMER(code) {
851 host_consumer_send(0);
853 #ifdef MOUSEKEY_ENABLE
854 else if IS_MOUSEKEY(code) {
861 /** \brief Utilities for actions. (FIXME: Needs better description)
863 * FIXME: Needs documentation.
865 void tap_code(uint8_t code) {
867 if (code == KC_CAPS) {
868 wait_ms(TAP_HOLD_CAPS_DELAY);
870 wait_ms(TAP_CODE_DELAY);
872 unregister_code(code);
875 /** \brief Utilities for actions. (FIXME: Needs better description)
877 * FIXME: Needs documentation.
879 void register_mods(uint8_t mods)
883 send_keyboard_report();
887 /** \brief Utilities for actions. (FIXME: Needs better description)
889 * FIXME: Needs documentation.
891 void unregister_mods(uint8_t mods)
895 send_keyboard_report();
899 /** \brief Utilities for actions. (FIXME: Needs better description)
901 * FIXME: Needs documentation.
903 void clear_keyboard(void)
906 clear_keyboard_but_mods();
909 /** \brief Utilities for actions. (FIXME: Needs better description)
911 * FIXME: Needs documentation.
913 void clear_keyboard_but_mods(void)
916 clear_keyboard_but_mods_and_keys();
919 /** \brief Utilities for actions. (FIXME: Needs better description)
921 * FIXME: Needs documentation.
923 void clear_keyboard_but_mods_and_keys()
927 send_keyboard_report();
928 #ifdef MOUSEKEY_ENABLE
932 #ifdef EXTRAKEY_ENABLE
934 host_consumer_send(0);
938 /** \brief Utilities for actions. (FIXME: Needs better description)
940 * FIXME: Needs documentation.
942 bool is_tap_key(keypos_t key)
944 action_t action = layer_switch_get_action(key);
945 return is_tap_action(action);
948 /** \brief Utilities for actions. (FIXME: Needs better description)
950 * FIXME: Needs documentation.
952 bool is_tap_action(action_t action)
954 switch (action.kind.id) {
958 case ACT_LAYER_TAP_EXT:
959 switch (action.layer_tap.code) {
967 switch (action.swap.code) {
969 case OP_SH_TAP_TOGGLE:
975 if (action.func.opt & FUNC_TAP) { return true; }
982 /** \brief Debug print (FIXME: Needs better description)
984 * FIXME: Needs documentation.
986 void debug_event(keyevent_t event)
988 dprintf("%04X%c(%u)", (event.key.row<<8 | event.key.col), (event.pressed ? 'd' : 'u'), event.time);
991 /** \brief Debug print (FIXME: Needs better description)
993 * FIXME: Needs documentation.
995 void debug_record(keyrecord_t record)
997 debug_event(record.event);
998 #ifndef NO_ACTION_TAPPING
999 dprintf(":%u%c", record.tap.count, (record.tap.interrupted ? '-' : ' '));
1003 /** \brief Debug print (FIXME: Needs better description)
1005 * FIXME: Needs documentation.
1007 void debug_action(action_t action)
1009 switch (action.kind.id) {
1010 case ACT_LMODS: dprint("ACT_LMODS"); break;
1011 case ACT_RMODS: dprint("ACT_RMODS"); break;
1012 case ACT_LMODS_TAP: dprint("ACT_LMODS_TAP"); break;
1013 case ACT_RMODS_TAP: dprint("ACT_RMODS_TAP"); break;
1014 case ACT_USAGE: dprint("ACT_USAGE"); break;
1015 case ACT_MOUSEKEY: dprint("ACT_MOUSEKEY"); break;
1016 case ACT_LAYER: dprint("ACT_LAYER"); break;
1017 case ACT_LAYER_TAP: dprint("ACT_LAYER_TAP"); break;
1018 case ACT_LAYER_TAP_EXT: dprint("ACT_LAYER_TAP_EXT"); break;
1019 case ACT_MACRO: dprint("ACT_MACRO"); break;
1020 case ACT_COMMAND: dprint("ACT_COMMAND"); break;
1021 case ACT_FUNCTION: dprint("ACT_FUNCTION"); break;
1022 case ACT_SWAP_HANDS: dprint("ACT_SWAP_HANDS"); break;
1023 default: dprint("UNKNOWN"); break;
1025 dprintf("[%X:%02X]", action.kind.param>>8, action.kind.param&0xff);