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_HOLD_CAPS_DELAY
48 # define TAP_HOLD_CAPS_DELAY 200
50 /** \brief Called to execute an action.
52 * FIXME: Needs documentation.
54 void action_exec(keyevent_t event)
56 if (!IS_NOEVENT(event)) {
57 dprint("\n---- action_exec: start -----\n");
58 dprint("EVENT: "); debug_event(event); dprintln();
60 retro_tapping_counter++;
64 #ifdef FAUXCLICKY_ENABLE
65 if (IS_PRESSED(event)) {
66 FAUXCLICKY_ACTION_PRESS;
68 if (IS_RELEASED(event)) {
69 FAUXCLICKY_ACTION_RELEASE;
74 #ifdef SWAP_HANDS_ENABLE
75 if (!IS_NOEVENT(event)) {
76 process_hand_swap(&event);
80 keyrecord_t record = { .event = event };
82 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
83 if (has_oneshot_layer_timed_out()) {
84 clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
86 if (has_oneshot_mods_timed_out()) {
91 #ifndef NO_ACTION_TAPPING
92 action_tapping_process(record);
94 process_record(&record);
95 if (!IS_NOEVENT(record.event)) {
96 dprint("processed: "); debug_record(record); dprintln();
101 #ifdef SWAP_HANDS_ENABLE
102 bool swap_hands = false;
103 bool swap_held = false;
105 /** \brief Process Hand Swap
107 * FIXME: Needs documentation.
109 void process_hand_swap(keyevent_t *event) {
110 static swap_state_row_t swap_state[MATRIX_ROWS];
112 keypos_t pos = event->key;
113 swap_state_row_t col_bit = (swap_state_row_t)1<<pos.col;
114 bool do_swap = event->pressed ? swap_hands :
115 swap_state[pos.row] & (col_bit);
118 event->key = hand_swap_config[pos.row][pos.col];
119 swap_state[pos.row] |= col_bit;
121 swap_state[pos.row] &= ~(col_bit);
126 #if !defined(NO_ACTION_LAYER) && !defined(STRICT_LAYER_RELEASE)
127 bool disable_action_cache = false;
129 void process_record_nocache(keyrecord_t *record)
131 disable_action_cache = true;
132 process_record(record);
133 disable_action_cache = false;
136 void process_record_nocache(keyrecord_t *record)
138 process_record(record);
142 __attribute__ ((weak))
143 bool process_record_quantum(keyrecord_t *record) {
147 #ifndef NO_ACTION_TAPPING
148 /** \brief Allows for handling tap-hold actions immediately instead of waiting for TAPPING_TERM or another keypress.
150 * FIXME: Needs documentation.
152 void process_record_tap_hint(keyrecord_t *record)
154 action_t action = layer_switch_get_action(record->event.key);
156 switch (action.kind.id) {
157 #ifdef SWAP_HANDS_ENABLE
159 switch (action.swap.code) {
160 case OP_SH_TAP_TOGGLE:
162 swap_hands = !swap_hands;
171 /** \brief Take a key event (key press or key release) and processes it.
173 * FIXME: Needs documentation.
175 void process_record(keyrecord_t *record)
177 if (IS_NOEVENT(record->event)) { return; }
179 if(!process_record_quantum(record))
182 action_t action = store_or_get_action(record->event.pressed, record->event.key);
183 dprint("ACTION: "); debug_action(action);
184 #ifndef NO_ACTION_LAYER
185 dprint(" layer_state: "); layer_debug();
186 dprint(" default_layer_state: "); default_layer_debug();
190 process_action(record, action);
193 /** \brief Take an action and processes it.
195 * FIXME: Needs documentation.
197 void process_action(keyrecord_t *record, action_t action)
199 keyevent_t event = record->event;
200 #ifndef NO_ACTION_TAPPING
201 uint8_t tap_count = record->tap.count;
205 // clear the potential weak mods left by previously pressed keys
209 #ifndef NO_ACTION_ONESHOT
210 bool do_release_oneshot = false;
211 // notice we only clear the one shot layer if the pressed key is not a modifier.
212 if (is_oneshot_layer_active() && event.pressed && !IS_MOD(action.key.code)) {
213 clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
214 do_release_oneshot = !is_oneshot_layer_active();
218 switch (action.kind.id) {
223 uint8_t mods = (action.kind.id == ACT_LMODS) ? action.key.mods :
227 if (IS_MOD(action.key.code) || action.key.code == KC_NO) {
228 // e.g. LSFT(KC_LGUI): we don't want the LSFT to be weak as it would make it useless.
229 // This also makes LSFT(KC_LGUI) behave exactly the same as LGUI(KC_LSFT).
230 // Same applies for some keys like KC_MEH which are declared as MEH(KC_NO).
235 send_keyboard_report();
237 register_code(action.key.code);
239 unregister_code(action.key.code);
241 if (IS_MOD(action.key.code) || action.key.code == KC_NO) {
246 send_keyboard_report();
251 #ifndef NO_ACTION_TAPPING
255 uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods :
257 switch (action.layer_tap.code) {
258 #ifndef NO_ACTION_ONESHOT
262 if (tap_count == 0) {
263 dprint("MODS_TAP: Oneshot: 0\n");
264 register_mods(mods | get_oneshot_mods());
265 } else if (tap_count == 1) {
266 dprint("MODS_TAP: Oneshot: start\n");
267 set_oneshot_mods(mods | get_oneshot_mods());
268 #if defined(ONESHOT_TAP_TOGGLE) && ONESHOT_TAP_TOGGLE > 1
269 } else if (tap_count == ONESHOT_TAP_TOGGLE) {
270 dprint("MODS_TAP: Toggling oneshot");
271 clear_oneshot_mods();
272 set_oneshot_locked_mods(mods);
276 register_mods(mods | get_oneshot_mods());
279 if (tap_count == 0) {
280 clear_oneshot_mods();
281 unregister_mods(mods);
282 } else if (tap_count == 1) {
283 // Retain Oneshot mods
284 #if defined(ONESHOT_TAP_TOGGLE) && ONESHOT_TAP_TOGGLE > 1
285 if (mods & get_mods()) {
286 clear_oneshot_locked_mods();
287 clear_oneshot_mods();
288 unregister_mods(mods);
290 } else if (tap_count == ONESHOT_TAP_TOGGLE) {
291 // Toggle Oneshot Layer
294 clear_oneshot_mods();
295 unregister_mods(mods);
300 case MODS_TAP_TOGGLE:
302 if (tap_count <= TAPPING_TOGGLE) {
306 if (tap_count < TAPPING_TOGGLE) {
307 unregister_mods(mods);
314 #ifndef IGNORE_MOD_TAP_INTERRUPT
315 if (record->tap.interrupted) {
316 dprint("mods_tap: tap: cancel: add_mods\n");
317 // ad hoc: set 0 to cancel tap
318 record->tap.count = 0;
323 dprint("MODS_TAP: Tap: register_code\n");
324 register_code(action.key.code);
327 dprint("MODS_TAP: No tap: add_mods\n");
332 dprint("MODS_TAP: Tap: unregister_code\n");
333 unregister_code(action.key.code);
335 dprint("MODS_TAP: No tap: add_mods\n");
336 unregister_mods(mods);
344 #ifdef EXTRAKEY_ENABLE
345 /* other HID usage */
347 switch (action.usage.page) {
350 host_system_send(action.usage.code);
357 host_consumer_send(action.usage.code);
359 host_consumer_send(0);
365 #ifdef MOUSEKEY_ENABLE
369 switch (action.key.code) {
371 tp_buttons |= (1<<0);
374 tp_buttons |= (1<<1);
377 tp_buttons |= (1<<2);
382 mousekey_on(action.key.code);
385 switch (action.key.code) {
387 tp_buttons &= ~(1<<0);
390 tp_buttons &= ~(1<<1);
393 tp_buttons &= ~(1<<2);
398 mousekey_off(action.key.code);
403 #ifndef NO_ACTION_LAYER
405 if (action.layer_bitop.on == 0) {
406 /* Default Layer Bitwise Operation */
407 if (!event.pressed) {
408 uint8_t shift = action.layer_bitop.part*4;
409 layer_state_t bits = ((layer_state_t)action.layer_bitop.bits)<<shift;
410 layer_state_t mask = (action.layer_bitop.xbit) ? ~(((layer_state_t)0xf)<<shift) : 0;
411 switch (action.layer_bitop.op) {
412 case OP_BIT_AND: default_layer_and(bits | mask); break;
413 case OP_BIT_OR: default_layer_or(bits | mask); break;
414 case OP_BIT_XOR: default_layer_xor(bits | mask); break;
415 case OP_BIT_SET: default_layer_set(bits | mask); break;
419 /* Layer Bitwise Operation */
420 if (event.pressed ? (action.layer_bitop.on & ON_PRESS) :
421 (action.layer_bitop.on & ON_RELEASE)) {
422 uint8_t shift = action.layer_bitop.part*4;
423 layer_state_t bits = ((layer_state_t)action.layer_bitop.bits)<<shift;
424 layer_state_t mask = (action.layer_bitop.xbit) ? ~(((layer_state_t)0xf)<<shift) : 0;
425 switch (action.layer_bitop.op) {
426 case OP_BIT_AND: layer_and(bits | mask); break;
427 case OP_BIT_OR: layer_or(bits | mask); break;
428 case OP_BIT_XOR: layer_xor(bits | mask); break;
429 case OP_BIT_SET: layer_state_set(bits | mask); break;
434 #ifndef NO_ACTION_TAPPING
436 case ACT_LAYER_TAP_EXT:
437 switch (action.layer_tap.code) {
439 /* layer On/Off with modifiers(left only) */
441 layer_on(action.layer_tap.val);
442 register_mods(action.layer_tap.code & 0x0f);
444 layer_off(action.layer_tap.val);
445 unregister_mods(action.layer_tap.code & 0x0f);
451 if (tap_count < TAPPING_TOGGLE) {
452 layer_invert(action.layer_tap.val);
455 if (tap_count <= TAPPING_TOGGLE) {
456 layer_invert(action.layer_tap.val);
461 event.pressed ? layer_on(action.layer_tap.val) :
462 layer_off(action.layer_tap.val);
465 event.pressed ? layer_off(action.layer_tap.val) :
466 layer_on(action.layer_tap.val);
469 event.pressed ? layer_move(action.layer_tap.val) :
472 #ifndef NO_ACTION_ONESHOT
475 #if defined(ONESHOT_TAP_TOGGLE) && ONESHOT_TAP_TOGGLE > 1
476 do_release_oneshot = false;
478 del_mods(get_oneshot_locked_mods());
479 if (get_oneshot_layer_state() == ONESHOT_TOGGLED) {
480 reset_oneshot_layer();
481 layer_off(action.layer_tap.val);
483 } else if (tap_count < ONESHOT_TAP_TOGGLE) {
484 layer_on(action.layer_tap.val);
485 set_oneshot_layer(action.layer_tap.val, ONESHOT_START);
488 add_mods(get_oneshot_locked_mods());
489 if (tap_count >= ONESHOT_TAP_TOGGLE) {
490 reset_oneshot_layer();
491 clear_oneshot_locked_mods();
492 set_oneshot_layer(action.layer_tap.val, ONESHOT_TOGGLED);
494 clear_oneshot_layer_state(ONESHOT_PRESSED);
499 layer_on(action.layer_tap.val);
500 set_oneshot_layer(action.layer_tap.val, ONESHOT_START);
502 clear_oneshot_layer_state(ONESHOT_PRESSED);
504 clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
514 dprint("KEYMAP_TAP_KEY: Tap: register_code\n");
515 register_code(action.layer_tap.code);
517 dprint("KEYMAP_TAP_KEY: No tap: On on press\n");
518 layer_on(action.layer_tap.val);
522 dprint("KEYMAP_TAP_KEY: Tap: unregister_code\n");
523 if (action.layer_tap.code == KC_CAPS) {
524 wait_ms(TAP_HOLD_CAPS_DELAY);
526 unregister_code(action.layer_tap.code);
528 dprint("KEYMAP_TAP_KEY: No tap: Off on release\n");
529 layer_off(action.layer_tap.val);
538 #ifndef NO_ACTION_MACRO
540 action_macro_play(action_get_macro(record, action.func.id, action.func.opt));
543 #if defined(BACKLIGHT_ENABLE) | defined(LED_MATRIX_ENABLE)
545 if (!event.pressed) {
546 switch (action.backlight.opt) {
547 case BACKLIGHT_INCREASE:
548 backlight_increase();
550 case BACKLIGHT_DECREASE:
551 backlight_decrease();
553 case BACKLIGHT_TOGGLE:
560 backlight_level(BACKLIGHT_LEVELS);
571 #ifdef SWAP_HANDS_ENABLE
573 switch (action.swap.code) {
576 swap_hands = !swap_hands;
580 swap_hands = event.pressed;
583 swap_hands = !event.pressed;
586 if (!event.pressed) {
591 if (!event.pressed) {
595 #ifndef NO_ACTION_TAPPING
596 case OP_SH_TAP_TOGGLE:
603 swap_hands = !swap_hands;
606 if (tap_count < TAPPING_TOGGLE) {
607 swap_hands = !swap_hands;
615 swap_hands = !swap_hands; // undo hold set up in _tap_hint
619 register_code(action.swap.code);
621 unregister_code(action.swap.code);
622 *record = (keyrecord_t){}; // hack: reset tap mode
625 if (swap_held && !event.pressed) {
626 swap_hands = !swap_hands; // undo hold set up in _tap_hint
633 #ifndef NO_ACTION_FUNCTION
635 action_function(record, action.func.id, action.func.opt);
642 #ifndef NO_ACTION_LAYER
643 // if this event is a layer action, update the leds
644 switch (action.kind.id) {
646 #ifndef NO_ACTION_TAPPING
648 case ACT_LAYER_TAP_EXT:
650 led_set(host_keyboard_leds());
657 #ifndef NO_ACTION_TAPPING
659 if (!is_tap_action(action)) {
660 retro_tapping_counter = 0;
664 retro_tapping_counter = 0;
670 retro_tapping_counter = 0;
672 if (retro_tapping_counter == 2) {
673 register_code(action.layer_tap.code);
674 unregister_code(action.layer_tap.code);
676 retro_tapping_counter = 0;
683 #ifndef NO_ACTION_ONESHOT
684 /* Because we switch layers after a oneshot event, we need to release the
685 * key before we leave the layer or no key up event will be generated.
687 if (do_release_oneshot && !(get_oneshot_layer_state() & ONESHOT_PRESSED ) ) {
688 record->event.pressed = false;
689 layer_on(get_oneshot_layer());
690 process_record(record);
691 layer_off(get_oneshot_layer());
699 /** \brief Utilities for actions. (FIXME: Needs better description)
701 * FIXME: Needs documentation.
703 void register_code(uint8_t code)
709 #ifdef LOCKING_SUPPORT_ENABLE
710 else if (KC_LOCKING_CAPS == code) {
711 #ifdef LOCKING_RESYNC_ENABLE
712 // Resync: ignore if caps lock already is on
713 if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) return;
715 add_key(KC_CAPSLOCK);
716 send_keyboard_report();
718 del_key(KC_CAPSLOCK);
719 send_keyboard_report();
722 else if (KC_LOCKING_NUM == code) {
723 #ifdef LOCKING_RESYNC_ENABLE
724 if (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK)) return;
727 send_keyboard_report();
730 send_keyboard_report();
733 else if (KC_LOCKING_SCROLL == code) {
734 #ifdef LOCKING_RESYNC_ENABLE
735 if (host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK)) return;
737 add_key(KC_SCROLLLOCK);
738 send_keyboard_report();
740 del_key(KC_SCROLLLOCK);
741 send_keyboard_report();
745 else if IS_KEY(code) {
746 // TODO: should push command_proc out of this block?
747 if (command_proc(code)) return;
749 #ifndef NO_ACTION_ONESHOT
751 if (oneshot_state.mods && !oneshot_state.disabled) {
752 uint8_t tmp_mods = get_mods();
753 add_mods(oneshot_state.mods);
756 send_keyboard_report();
759 send_keyboard_report();
766 send_keyboard_report();
769 else if IS_MOD(code) {
770 add_mods(MOD_BIT(code));
771 send_keyboard_report();
773 else if IS_SYSTEM(code) {
774 host_system_send(KEYCODE2SYSTEM(code));
776 else if IS_CONSUMER(code) {
777 host_consumer_send(KEYCODE2CONSUMER(code));
780 #ifdef MOUSEKEY_ENABLE
781 else if IS_MOUSEKEY(code) {
788 /** \brief Utilities for actions. (FIXME: Needs better description)
790 * FIXME: Needs documentation.
792 void unregister_code(uint8_t code)
798 #ifdef LOCKING_SUPPORT_ENABLE
799 else if (KC_LOCKING_CAPS == code) {
800 #ifdef LOCKING_RESYNC_ENABLE
801 // Resync: ignore if caps lock already is off
802 if (!(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK))) return;
804 add_key(KC_CAPSLOCK);
805 send_keyboard_report();
806 del_key(KC_CAPSLOCK);
807 send_keyboard_report();
810 else if (KC_LOCKING_NUM == code) {
811 #ifdef LOCKING_RESYNC_ENABLE
812 if (!(host_keyboard_leds() & (1<<USB_LED_NUM_LOCK))) return;
815 send_keyboard_report();
817 send_keyboard_report();
820 else if (KC_LOCKING_SCROLL == code) {
821 #ifdef LOCKING_RESYNC_ENABLE
822 if (!(host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK))) return;
824 add_key(KC_SCROLLLOCK);
825 send_keyboard_report();
826 del_key(KC_SCROLLLOCK);
827 send_keyboard_report();
831 else if IS_KEY(code) {
833 send_keyboard_report();
835 else if IS_MOD(code) {
836 del_mods(MOD_BIT(code));
837 send_keyboard_report();
839 else if IS_SYSTEM(code) {
842 else if IS_CONSUMER(code) {
843 host_consumer_send(0);
845 #ifdef MOUSEKEY_ENABLE
846 else if IS_MOUSEKEY(code) {
853 /** \brief Utilities for actions. (FIXME: Needs better description)
855 * FIXME: Needs documentation.
857 void tap_code(uint8_t code) {
859 if (code == KC_CAPS) {
860 wait_ms(TAP_HOLD_CAPS_DELAY);
862 #if TAP_CODE_DELAY > 0
864 wait_ms(TAP_CODE_DELAY);
867 unregister_code(code);
870 /** \brief Utilities for actions. (FIXME: Needs better description)
872 * FIXME: Needs documentation.
874 void register_mods(uint8_t mods)
878 send_keyboard_report();
882 /** \brief Utilities for actions. (FIXME: Needs better description)
884 * FIXME: Needs documentation.
886 void unregister_mods(uint8_t mods)
890 send_keyboard_report();
894 /** \brief Utilities for actions. (FIXME: Needs better description)
896 * FIXME: Needs documentation.
898 void clear_keyboard(void)
901 clear_keyboard_but_mods();
904 /** \brief Utilities for actions. (FIXME: Needs better description)
906 * FIXME: Needs documentation.
908 void clear_keyboard_but_mods(void)
911 clear_keyboard_but_mods_and_keys();
914 /** \brief Utilities for actions. (FIXME: Needs better description)
916 * FIXME: Needs documentation.
918 void clear_keyboard_but_mods_and_keys()
922 send_keyboard_report();
923 #ifdef MOUSEKEY_ENABLE
927 #ifdef EXTRAKEY_ENABLE
929 host_consumer_send(0);
933 /** \brief Utilities for actions. (FIXME: Needs better description)
935 * FIXME: Needs documentation.
937 bool is_tap_key(keypos_t key)
939 action_t action = layer_switch_get_action(key);
940 return is_tap_action(action);
943 /** \brief Utilities for actions. (FIXME: Needs better description)
945 * FIXME: Needs documentation.
947 bool is_tap_action(action_t action)
949 switch (action.kind.id) {
953 case ACT_LAYER_TAP_EXT:
954 switch (action.layer_tap.code) {
962 switch (action.swap.code) {
964 case OP_SH_TAP_TOGGLE:
970 if (action.func.opt & FUNC_TAP) { return true; }
977 /** \brief Debug print (FIXME: Needs better description)
979 * FIXME: Needs documentation.
981 void debug_event(keyevent_t event)
983 dprintf("%04X%c(%u)", (event.key.row<<8 | event.key.col), (event.pressed ? 'd' : 'u'), event.time);
986 /** \brief Debug print (FIXME: Needs better description)
988 * FIXME: Needs documentation.
990 void debug_record(keyrecord_t record)
992 debug_event(record.event);
993 #ifndef NO_ACTION_TAPPING
994 dprintf(":%u%c", record.tap.count, (record.tap.interrupted ? '-' : ' '));
998 /** \brief Debug print (FIXME: Needs better description)
1000 * FIXME: Needs documentation.
1002 void debug_action(action_t action)
1004 switch (action.kind.id) {
1005 case ACT_LMODS: dprint("ACT_LMODS"); break;
1006 case ACT_RMODS: dprint("ACT_RMODS"); break;
1007 case ACT_LMODS_TAP: dprint("ACT_LMODS_TAP"); break;
1008 case ACT_RMODS_TAP: dprint("ACT_RMODS_TAP"); break;
1009 case ACT_USAGE: dprint("ACT_USAGE"); break;
1010 case ACT_MOUSEKEY: dprint("ACT_MOUSEKEY"); break;
1011 case ACT_LAYER: dprint("ACT_LAYER"); break;
1012 case ACT_LAYER_TAP: dprint("ACT_LAYER_TAP"); break;
1013 case ACT_LAYER_TAP_EXT: dprint("ACT_LAYER_TAP_EXT"); break;
1014 case ACT_MACRO: dprint("ACT_MACRO"); break;
1015 case ACT_COMMAND: dprint("ACT_COMMAND"); break;
1016 case ACT_FUNCTION: dprint("ACT_FUNCTION"); break;
1017 case ACT_SWAP_HANDS: dprint("ACT_SWAP_HANDS"); break;
1018 default: dprint("UNKNOWN"); break;
1020 dprintf("[%X:%02X]", action.kind.param>>8, action.kind.param&0xff);