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 /** \brief Called to execute an action.
49 * FIXME: Needs documentation.
51 void action_exec(keyevent_t event)
53 if (!IS_NOEVENT(event)) {
54 dprint("\n---- action_exec: start -----\n");
55 dprint("EVENT: "); debug_event(event); dprintln();
57 retro_tapping_counter++;
61 #ifdef FAUXCLICKY_ENABLE
62 if (IS_PRESSED(event)) {
63 FAUXCLICKY_ACTION_PRESS;
65 if (IS_RELEASED(event)) {
66 FAUXCLICKY_ACTION_RELEASE;
71 #ifdef SWAP_HANDS_ENABLE
72 if (!IS_NOEVENT(event)) {
73 process_hand_swap(&event);
77 keyrecord_t record = { .event = event };
79 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
80 if (has_oneshot_layer_timed_out()) {
81 clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
83 if (has_oneshot_mods_timed_out()) {
88 #ifndef NO_ACTION_TAPPING
89 action_tapping_process(record);
91 process_record(&record);
92 if (!IS_NOEVENT(record.event)) {
93 dprint("processed: "); debug_record(record); dprintln();
98 #ifdef SWAP_HANDS_ENABLE
99 bool swap_hands = false;
100 bool swap_held = false;
102 /** \brief Process Hand Swap
104 * FIXME: Needs documentation.
106 void process_hand_swap(keyevent_t *event) {
107 static swap_state_row_t swap_state[MATRIX_ROWS];
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);
115 event->key = hand_swap_config[pos.row][pos.col];
116 swap_state[pos.row] |= col_bit;
118 swap_state[pos.row] &= ~(col_bit);
123 #if !defined(NO_ACTION_LAYER) && !defined(STRICT_LAYER_RELEASE)
124 bool disable_action_cache = false;
126 void process_record_nocache(keyrecord_t *record)
128 disable_action_cache = true;
129 process_record(record);
130 disable_action_cache = false;
133 void process_record_nocache(keyrecord_t *record)
135 process_record(record);
139 __attribute__ ((weak))
140 bool process_record_quantum(keyrecord_t *record) {
144 #ifndef NO_ACTION_TAPPING
145 /** \brief Allows for handling tap-hold actions immediately instead of waiting for TAPPING_TERM or another keypress.
147 * FIXME: Needs documentation.
149 void process_record_tap_hint(keyrecord_t *record)
151 action_t action = layer_switch_get_action(record->event.key);
153 switch (action.kind.id) {
154 #ifdef SWAP_HANDS_ENABLE
156 switch (action.swap.code) {
157 case OP_SH_TAP_TOGGLE:
159 swap_hands = !swap_hands;
168 /** \brief Take a key event (key press or key release) and processes it.
170 * FIXME: Needs documentation.
172 void process_record(keyrecord_t *record)
174 if (IS_NOEVENT(record->event)) { return; }
176 if(!process_record_quantum(record))
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();
187 process_action(record, action);
190 /** \brief Take an action and processes it.
192 * FIXME: Needs documentation.
194 void process_action(keyrecord_t *record, action_t action)
196 keyevent_t event = record->event;
197 #ifndef NO_ACTION_TAPPING
198 uint8_t tap_count = record->tap.count;
202 // clear the potential weak mods left by previously pressed keys
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();
215 switch (action.kind.id) {
220 uint8_t mods = (action.kind.id == ACT_LMODS) ? action.key.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).
232 send_keyboard_report();
234 register_code(action.key.code);
236 unregister_code(action.key.code);
238 if (IS_MOD(action.key.code) || action.key.code == KC_NO) {
243 send_keyboard_report();
248 #ifndef NO_ACTION_TAPPING
252 uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods :
254 switch (action.layer_tap.code) {
255 #ifndef NO_ACTION_ONESHOT
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);
273 register_mods(mods | get_oneshot_mods());
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);
287 } else if (tap_count == ONESHOT_TAP_TOGGLE) {
288 // Toggle Oneshot Layer
291 clear_oneshot_mods();
292 unregister_mods(mods);
297 case MODS_TAP_TOGGLE:
299 if (tap_count <= TAPPING_TOGGLE) {
303 if (tap_count < TAPPING_TOGGLE) {
304 unregister_mods(mods);
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;
320 dprint("MODS_TAP: Tap: register_code\n");
321 register_code(action.key.code);
324 dprint("MODS_TAP: No tap: add_mods\n");
329 dprint("MODS_TAP: Tap: unregister_code\n");
330 unregister_code(action.key.code);
332 dprint("MODS_TAP: No tap: add_mods\n");
333 unregister_mods(mods);
341 #ifdef EXTRAKEY_ENABLE
342 /* other HID usage */
344 switch (action.usage.page) {
347 host_system_send(action.usage.code);
354 host_consumer_send(action.usage.code);
356 host_consumer_send(0);
362 #ifdef MOUSEKEY_ENABLE
366 switch (action.key.code) {
368 tp_buttons |= (1<<0);
371 tp_buttons |= (1<<1);
374 tp_buttons |= (1<<2);
379 mousekey_on(action.key.code);
382 switch (action.key.code) {
384 tp_buttons &= ~(1<<0);
387 tp_buttons &= ~(1<<1);
390 tp_buttons &= ~(1<<2);
395 mousekey_off(action.key.code);
400 #ifndef NO_ACTION_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;
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;
431 #ifndef NO_ACTION_TAPPING
433 case ACT_LAYER_TAP_EXT:
434 switch (action.layer_tap.code) {
436 /* layer On/Off with modifiers(left only) */
438 layer_on(action.layer_tap.val);
439 register_mods(action.layer_tap.code & 0x0f);
441 layer_off(action.layer_tap.val);
442 unregister_mods(action.layer_tap.code & 0x0f);
448 if (tap_count < TAPPING_TOGGLE) {
449 layer_invert(action.layer_tap.val);
452 if (tap_count <= TAPPING_TOGGLE) {
453 layer_invert(action.layer_tap.val);
458 event.pressed ? layer_on(action.layer_tap.val) :
459 layer_off(action.layer_tap.val);
462 event.pressed ? layer_off(action.layer_tap.val) :
463 layer_on(action.layer_tap.val);
466 event.pressed ? layer_move(action.layer_tap.val) :
469 #ifndef NO_ACTION_ONESHOT
472 #if defined(ONESHOT_TAP_TOGGLE) && ONESHOT_TAP_TOGGLE > 1
473 do_release_oneshot = false;
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);
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);
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);
491 clear_oneshot_layer_state(ONESHOT_PRESSED);
496 layer_on(action.layer_tap.val);
497 set_oneshot_layer(action.layer_tap.val, ONESHOT_START);
499 clear_oneshot_layer_state(ONESHOT_PRESSED);
501 clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
511 dprint("KEYMAP_TAP_KEY: Tap: register_code\n");
512 register_code(action.layer_tap.code);
514 dprint("KEYMAP_TAP_KEY: No tap: On on press\n");
515 layer_on(action.layer_tap.val);
519 dprint("KEYMAP_TAP_KEY: Tap: unregister_code\n");
520 if (action.layer_tap.code == KC_CAPS) {
523 unregister_code(action.layer_tap.code);
525 dprint("KEYMAP_TAP_KEY: No tap: Off on release\n");
526 layer_off(action.layer_tap.val);
535 #ifndef NO_ACTION_MACRO
537 action_macro_play(action_get_macro(record, action.func.id, action.func.opt));
540 #ifdef BACKLIGHT_ENABLE
542 if (!event.pressed) {
543 switch (action.backlight.opt) {
544 case BACKLIGHT_INCREASE:
545 backlight_increase();
547 case BACKLIGHT_DECREASE:
548 backlight_decrease();
550 case BACKLIGHT_TOGGLE:
557 backlight_level(BACKLIGHT_LEVELS);
568 #ifdef SWAP_HANDS_ENABLE
570 switch (action.swap.code) {
573 swap_hands = !swap_hands;
577 swap_hands = event.pressed;
580 swap_hands = !event.pressed;
583 if (!event.pressed) {
588 if (!event.pressed) {
592 #ifndef NO_ACTION_TAPPING
593 case OP_SH_TAP_TOGGLE:
600 swap_hands = !swap_hands;
603 if (tap_count < TAPPING_TOGGLE) {
604 swap_hands = !swap_hands;
612 swap_hands = !swap_hands; // undo hold set up in _tap_hint
616 register_code(action.swap.code);
618 unregister_code(action.swap.code);
619 *record = (keyrecord_t){}; // hack: reset tap mode
622 if (swap_held && !event.pressed) {
623 swap_hands = !swap_hands; // undo hold set up in _tap_hint
630 #ifndef NO_ACTION_FUNCTION
632 action_function(record, action.func.id, action.func.opt);
639 #ifndef NO_ACTION_LAYER
640 // if this event is a layer action, update the leds
641 switch (action.kind.id) {
643 #ifndef NO_ACTION_TAPPING
645 case ACT_LAYER_TAP_EXT:
647 led_set(host_keyboard_leds());
654 #ifndef NO_ACTION_TAPPING
656 if (!is_tap_key(record->event.key)) {
657 retro_tapping_counter = 0;
661 retro_tapping_counter = 0;
667 retro_tapping_counter = 0;
669 if (retro_tapping_counter == 2) {
670 register_code(action.layer_tap.code);
671 unregister_code(action.layer_tap.code);
673 retro_tapping_counter = 0;
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.
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());
696 /** \brief Utilities for actions. (FIXME: Needs better description)
698 * FIXME: Needs documentation.
700 void register_code(uint8_t code)
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;
712 add_key(KC_CAPSLOCK);
713 send_keyboard_report();
715 del_key(KC_CAPSLOCK);
716 send_keyboard_report();
719 else if (KC_LOCKING_NUM == code) {
720 #ifdef LOCKING_RESYNC_ENABLE
721 if (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK)) return;
724 send_keyboard_report();
727 send_keyboard_report();
730 else if (KC_LOCKING_SCROLL == code) {
731 #ifdef LOCKING_RESYNC_ENABLE
732 if (host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK)) return;
734 add_key(KC_SCROLLLOCK);
735 send_keyboard_report();
737 del_key(KC_SCROLLLOCK);
738 send_keyboard_report();
742 else if IS_KEY(code) {
743 // TODO: should push command_proc out of this block?
744 if (command_proc(code)) return;
746 #ifndef NO_ACTION_ONESHOT
748 if (oneshot_state.mods && !oneshot_state.disabled) {
749 uint8_t tmp_mods = get_mods();
750 add_mods(oneshot_state.mods);
753 send_keyboard_report();
756 send_keyboard_report();
763 send_keyboard_report();
766 else if IS_MOD(code) {
767 add_mods(MOD_BIT(code));
768 send_keyboard_report();
770 else if IS_SYSTEM(code) {
771 host_system_send(KEYCODE2SYSTEM(code));
773 else if IS_CONSUMER(code) {
774 host_consumer_send(KEYCODE2CONSUMER(code));
777 #ifdef MOUSEKEY_ENABLE
778 else if IS_MOUSEKEY(code) {
785 /** \brief Utilities for actions. (FIXME: Needs better description)
787 * FIXME: Needs documentation.
789 void unregister_code(uint8_t code)
795 #ifdef LOCKING_SUPPORT_ENABLE
796 else if (KC_LOCKING_CAPS == code) {
797 #ifdef LOCKING_RESYNC_ENABLE
798 // Resync: ignore if caps lock already is off
799 if (!(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK))) return;
801 add_key(KC_CAPSLOCK);
802 send_keyboard_report();
803 del_key(KC_CAPSLOCK);
804 send_keyboard_report();
807 else if (KC_LOCKING_NUM == code) {
808 #ifdef LOCKING_RESYNC_ENABLE
809 if (!(host_keyboard_leds() & (1<<USB_LED_NUM_LOCK))) return;
812 send_keyboard_report();
814 send_keyboard_report();
817 else if (KC_LOCKING_SCROLL == code) {
818 #ifdef LOCKING_RESYNC_ENABLE
819 if (!(host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK))) return;
821 add_key(KC_SCROLLLOCK);
822 send_keyboard_report();
823 del_key(KC_SCROLLLOCK);
824 send_keyboard_report();
828 else if IS_KEY(code) {
830 send_keyboard_report();
832 else if IS_MOD(code) {
833 del_mods(MOD_BIT(code));
834 send_keyboard_report();
836 else if IS_SYSTEM(code) {
839 else if IS_CONSUMER(code) {
840 host_consumer_send(0);
842 #ifdef MOUSEKEY_ENABLE
843 else if IS_MOUSEKEY(code) {
850 /** \brief Utilities for actions. (FIXME: Needs better description)
852 * FIXME: Needs documentation.
854 void tap_code(uint8_t code) {
856 #if TAP_CODE_DELAY > 0
857 wait_ms(TAP_CODE_DELAY);
859 unregister_code(code);
862 /** \brief Utilities for actions. (FIXME: Needs better description)
864 * FIXME: Needs documentation.
866 void register_mods(uint8_t mods)
870 send_keyboard_report();
874 /** \brief Utilities for actions. (FIXME: Needs better description)
876 * FIXME: Needs documentation.
878 void unregister_mods(uint8_t mods)
882 send_keyboard_report();
886 /** \brief Utilities for actions. (FIXME: Needs better description)
888 * FIXME: Needs documentation.
890 void clear_keyboard(void)
893 clear_keyboard_but_mods();
896 /** \brief Utilities for actions. (FIXME: Needs better description)
898 * FIXME: Needs documentation.
900 void clear_keyboard_but_mods(void)
903 clear_keyboard_but_mods_and_keys();
906 /** \brief Utilities for actions. (FIXME: Needs better description)
908 * FIXME: Needs documentation.
910 void clear_keyboard_but_mods_and_keys()
914 send_keyboard_report();
915 #ifdef MOUSEKEY_ENABLE
919 #ifdef EXTRAKEY_ENABLE
921 host_consumer_send(0);
925 /** \brief Utilities for actions. (FIXME: Needs better description)
927 * FIXME: Needs documentation.
929 bool is_tap_key(keypos_t key)
931 action_t action = layer_switch_get_action(key);
933 switch (action.kind.id) {
937 case ACT_LAYER_TAP_EXT:
938 switch (action.layer_tap.code) {
946 switch (action.swap.code) {
948 case OP_SH_TAP_TOGGLE:
954 if (action.func.opt & FUNC_TAP) { return true; }
961 /** \brief Debug print (FIXME: Needs better description)
963 * FIXME: Needs documentation.
965 void debug_event(keyevent_t event)
967 dprintf("%04X%c(%u)", (event.key.row<<8 | event.key.col), (event.pressed ? 'd' : 'u'), event.time);
970 /** \brief Debug print (FIXME: Needs better description)
972 * FIXME: Needs documentation.
974 void debug_record(keyrecord_t record)
976 debug_event(record.event);
977 #ifndef NO_ACTION_TAPPING
978 dprintf(":%u%c", record.tap.count, (record.tap.interrupted ? '-' : ' '));
982 /** \brief Debug print (FIXME: Needs better description)
984 * FIXME: Needs documentation.
986 void debug_action(action_t action)
988 switch (action.kind.id) {
989 case ACT_LMODS: dprint("ACT_LMODS"); break;
990 case ACT_RMODS: dprint("ACT_RMODS"); break;
991 case ACT_LMODS_TAP: dprint("ACT_LMODS_TAP"); break;
992 case ACT_RMODS_TAP: dprint("ACT_RMODS_TAP"); break;
993 case ACT_USAGE: dprint("ACT_USAGE"); break;
994 case ACT_MOUSEKEY: dprint("ACT_MOUSEKEY"); break;
995 case ACT_LAYER: dprint("ACT_LAYER"); break;
996 case ACT_LAYER_TAP: dprint("ACT_LAYER_TAP"); break;
997 case ACT_LAYER_TAP_EXT: dprint("ACT_LAYER_TAP_EXT"); break;
998 case ACT_MACRO: dprint("ACT_MACRO"); break;
999 case ACT_COMMAND: dprint("ACT_COMMAND"); break;
1000 case ACT_FUNCTION: dprint("ACT_FUNCTION"); break;
1001 case ACT_SWAP_HANDS: dprint("ACT_SWAP_HANDS"); break;
1002 default: dprint("UNKNOWN"); break;
1004 dprintf("[%X:%02X]", action.kind.param>>8, action.kind.param&0xff);