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 void action_exec(keyevent_t event)
49 if (!IS_NOEVENT(event)) {
50 dprint("\n---- action_exec: start -----\n");
51 dprint("EVENT: "); debug_event(event); dprintln();
53 retro_tapping_counter++;
57 #ifdef FAUXCLICKY_ENABLE
58 if (IS_PRESSED(event)) {
59 FAUXCLICKY_ACTION_PRESS;
61 if (IS_RELEASED(event)) {
62 FAUXCLICKY_ACTION_RELEASE;
67 #ifdef SWAP_HANDS_ENABLE
68 if (!IS_NOEVENT(event)) {
69 process_hand_swap(&event);
73 keyrecord_t record = { .event = event };
75 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
76 if (has_oneshot_layer_timed_out()) {
77 clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
79 if (has_oneshot_mods_timed_out()) {
84 #ifndef NO_ACTION_TAPPING
85 action_tapping_process(record);
87 process_record(&record);
88 if (!IS_NOEVENT(record.event)) {
89 dprint("processed: "); debug_record(record); dprintln();
94 #ifdef SWAP_HANDS_ENABLE
95 bool swap_hands = false;
96 bool swap_held = false;
98 void process_hand_swap(keyevent_t *event) {
99 static swap_state_row_t swap_state[MATRIX_ROWS];
101 keypos_t pos = event->key;
102 swap_state_row_t col_bit = (swap_state_row_t)1<<pos.col;
103 bool do_swap = event->pressed ? swap_hands :
104 swap_state[pos.row] & (col_bit);
107 event->key = hand_swap_config[pos.row][pos.col];
108 swap_state[pos.row] |= col_bit;
110 swap_state[pos.row] &= ~(col_bit);
115 #if !defined(NO_ACTION_LAYER) && defined(PREVENT_STUCK_MODIFIERS)
116 bool disable_action_cache = false;
118 void process_record_nocache(keyrecord_t *record)
120 disable_action_cache = true;
121 process_record(record);
122 disable_action_cache = false;
125 void process_record_nocache(keyrecord_t *record)
127 process_record(record);
131 __attribute__ ((weak))
132 bool process_record_quantum(keyrecord_t *record) {
136 #ifndef NO_ACTION_TAPPING
137 // Allows for handling tap-hold actions immediately instead of waiting for TAPPING_TERM or another keypress.
138 void process_record_tap_hint(keyrecord_t *record)
140 action_t action = layer_switch_get_action(record->event.key);
142 switch (action.kind.id) {
143 #ifdef SWAP_HANDS_ENABLE
145 switch (action.swap.code) {
146 case OP_SH_TAP_TOGGLE:
148 swap_hands = !swap_hands;
157 void process_record(keyrecord_t *record)
159 if (IS_NOEVENT(record->event)) { return; }
161 if(!process_record_quantum(record))
164 action_t action = store_or_get_action(record->event.pressed, record->event.key);
165 dprint("ACTION: "); debug_action(action);
166 #ifndef NO_ACTION_LAYER
167 dprint(" layer_state: "); layer_debug();
168 dprint(" default_layer_state: "); default_layer_debug();
172 process_action(record, action);
175 void process_action(keyrecord_t *record, action_t action)
177 keyevent_t event = record->event;
178 #ifndef NO_ACTION_TAPPING
179 uint8_t tap_count = record->tap.count;
183 // clear the potential weak mods left by previously pressed keys
187 #ifndef NO_ACTION_ONESHOT
188 bool do_release_oneshot = false;
189 // notice we only clear the one shot layer if the pressed key is not a modifier.
190 if (is_oneshot_layer_active() && event.pressed && !IS_MOD(action.key.code)) {
191 clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
192 do_release_oneshot = !is_oneshot_layer_active();
196 switch (action.kind.id) {
201 uint8_t mods = (action.kind.id == ACT_LMODS) ? action.key.mods :
205 if (IS_MOD(action.key.code) || action.key.code == KC_NO) {
206 // e.g. LSFT(KC_LGUI): we don't want the LSFT to be weak as it would make it useless.
207 // This also makes LSFT(KC_LGUI) behave exactly the same as LGUI(KC_LSFT).
208 // Same applies for some keys like KC_MEH which are declared as MEH(KC_NO).
213 send_keyboard_report();
215 register_code(action.key.code);
217 unregister_code(action.key.code);
219 if (IS_MOD(action.key.code) || action.key.code == KC_NO) {
224 send_keyboard_report();
229 #ifndef NO_ACTION_TAPPING
233 uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods :
235 switch (action.layer_tap.code) {
236 #ifndef NO_ACTION_ONESHOT
240 if (tap_count == 0) {
241 dprint("MODS_TAP: Oneshot: 0\n");
243 } else if (tap_count == 1) {
244 dprint("MODS_TAP: Oneshot: start\n");
245 set_oneshot_mods(mods);
246 #if defined(ONESHOT_TAP_TOGGLE) && ONESHOT_TAP_TOGGLE > 1
247 } else if (tap_count == ONESHOT_TAP_TOGGLE) {
248 dprint("MODS_TAP: Toggling oneshot");
249 clear_oneshot_mods();
250 set_oneshot_locked_mods(mods);
257 if (tap_count == 0) {
258 clear_oneshot_mods();
259 unregister_mods(mods);
260 } else if (tap_count == 1) {
261 // Retain Oneshot mods
262 #if defined(ONESHOT_TAP_TOGGLE) && ONESHOT_TAP_TOGGLE > 1
263 if (mods & get_mods()) {
264 clear_oneshot_locked_mods();
265 clear_oneshot_mods();
266 unregister_mods(mods);
268 } else if (tap_count == ONESHOT_TAP_TOGGLE) {
269 // Toggle Oneshot Layer
272 clear_oneshot_mods();
273 unregister_mods(mods);
278 case MODS_TAP_TOGGLE:
280 if (tap_count <= TAPPING_TOGGLE) {
284 if (tap_count < TAPPING_TOGGLE) {
285 unregister_mods(mods);
292 #ifndef IGNORE_MOD_TAP_INTERRUPT
293 if (record->tap.interrupted) {
294 dprint("mods_tap: tap: cancel: add_mods\n");
295 // ad hoc: set 0 to cancel tap
296 record->tap.count = 0;
301 dprint("MODS_TAP: Tap: register_code\n");
302 register_code(action.key.code);
305 dprint("MODS_TAP: No tap: add_mods\n");
310 dprint("MODS_TAP: Tap: unregister_code\n");
311 unregister_code(action.key.code);
313 dprint("MODS_TAP: No tap: add_mods\n");
314 unregister_mods(mods);
322 #ifdef EXTRAKEY_ENABLE
323 /* other HID usage */
325 switch (action.usage.page) {
328 host_system_send(action.usage.code);
335 host_consumer_send(action.usage.code);
337 host_consumer_send(0);
343 #ifdef MOUSEKEY_ENABLE
347 switch (action.key.code) {
349 tp_buttons |= (1<<0);
352 tp_buttons |= (1<<1);
355 tp_buttons |= (1<<2);
360 mousekey_on(action.key.code);
363 switch (action.key.code) {
365 tp_buttons &= ~(1<<0);
368 tp_buttons &= ~(1<<1);
371 tp_buttons &= ~(1<<2);
376 mousekey_off(action.key.code);
381 #ifndef NO_ACTION_LAYER
383 if (action.layer_bitop.on == 0) {
384 /* Default Layer Bitwise Operation */
385 if (!event.pressed) {
386 uint8_t shift = action.layer_bitop.part*4;
387 uint32_t bits = ((uint32_t)action.layer_bitop.bits)<<shift;
388 uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
389 switch (action.layer_bitop.op) {
390 case OP_BIT_AND: default_layer_and(bits | mask); break;
391 case OP_BIT_OR: default_layer_or(bits | mask); break;
392 case OP_BIT_XOR: default_layer_xor(bits | mask); break;
393 case OP_BIT_SET: default_layer_and(mask); default_layer_or(bits); break;
397 /* Layer Bitwise Operation */
398 if (event.pressed ? (action.layer_bitop.on & ON_PRESS) :
399 (action.layer_bitop.on & ON_RELEASE)) {
400 uint8_t shift = action.layer_bitop.part*4;
401 uint32_t bits = ((uint32_t)action.layer_bitop.bits)<<shift;
402 uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
403 switch (action.layer_bitop.op) {
404 case OP_BIT_AND: layer_and(bits | mask); break;
405 case OP_BIT_OR: layer_or(bits | mask); break;
406 case OP_BIT_XOR: layer_xor(bits | mask); break;
407 case OP_BIT_SET: layer_and(mask); layer_or(bits); break;
412 #ifndef NO_ACTION_TAPPING
414 case ACT_LAYER_TAP_EXT:
415 switch (action.layer_tap.code) {
417 /* layer On/Off with modifiers(left only) */
419 layer_on(action.layer_tap.val);
420 register_mods(action.layer_tap.code & 0x0f);
422 layer_off(action.layer_tap.val);
423 unregister_mods(action.layer_tap.code & 0x0f);
429 if (tap_count < TAPPING_TOGGLE) {
430 layer_invert(action.layer_tap.val);
433 if (tap_count <= TAPPING_TOGGLE) {
434 layer_invert(action.layer_tap.val);
439 event.pressed ? layer_on(action.layer_tap.val) :
440 layer_off(action.layer_tap.val);
443 event.pressed ? layer_off(action.layer_tap.val) :
444 layer_on(action.layer_tap.val);
447 event.pressed ? layer_move(action.layer_tap.val) :
450 #ifndef NO_ACTION_ONESHOT
453 #if defined(ONESHOT_TAP_TOGGLE) && ONESHOT_TAP_TOGGLE > 1
454 do_release_oneshot = false;
456 del_mods(get_oneshot_locked_mods());
457 if (get_oneshot_layer_state() == ONESHOT_TOGGLED) {
458 reset_oneshot_layer();
459 layer_off(action.layer_tap.val);
461 } else if (tap_count < ONESHOT_TAP_TOGGLE) {
462 layer_on(action.layer_tap.val);
463 set_oneshot_layer(action.layer_tap.val, ONESHOT_START);
466 add_mods(get_oneshot_locked_mods());
467 if (tap_count >= ONESHOT_TAP_TOGGLE) {
468 reset_oneshot_layer();
469 clear_oneshot_locked_mods();
470 set_oneshot_layer(action.layer_tap.val, ONESHOT_TOGGLED);
472 clear_oneshot_layer_state(ONESHOT_PRESSED);
477 layer_on(action.layer_tap.val);
478 set_oneshot_layer(action.layer_tap.val, ONESHOT_START);
480 clear_oneshot_layer_state(ONESHOT_PRESSED);
482 clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
492 dprint("KEYMAP_TAP_KEY: Tap: register_code\n");
493 register_code(action.layer_tap.code);
495 dprint("KEYMAP_TAP_KEY: No tap: On on press\n");
496 layer_on(action.layer_tap.val);
500 dprint("KEYMAP_TAP_KEY: Tap: unregister_code\n");
501 if (action.layer_tap.code == KC_CAPS) {
504 unregister_code(action.layer_tap.code);
506 dprint("KEYMAP_TAP_KEY: No tap: Off on release\n");
507 layer_off(action.layer_tap.val);
516 #ifndef NO_ACTION_MACRO
518 action_macro_play(action_get_macro(record, action.func.id, action.func.opt));
521 #ifdef BACKLIGHT_ENABLE
523 if (!event.pressed) {
524 switch (action.backlight.opt) {
525 case BACKLIGHT_INCREASE:
526 backlight_increase();
528 case BACKLIGHT_DECREASE:
529 backlight_decrease();
531 case BACKLIGHT_TOGGLE:
538 backlight_level(BACKLIGHT_LEVELS);
549 #ifdef SWAP_HANDS_ENABLE
551 switch (action.swap.code) {
554 swap_hands = !swap_hands;
558 swap_hands = event.pressed;
561 swap_hands = !event.pressed;
564 if (!event.pressed) {
569 if (!event.pressed) {
573 #ifndef NO_ACTION_TAPPING
574 case OP_SH_TAP_TOGGLE:
581 swap_hands = !swap_hands;
584 if (tap_count < TAPPING_TOGGLE) {
585 swap_hands = !swap_hands;
593 swap_hands = !swap_hands; // undo hold set up in _tap_hint
597 register_code(action.swap.code);
599 unregister_code(action.swap.code);
600 *record = (keyrecord_t){}; // hack: reset tap mode
603 if (swap_held && !event.pressed) {
604 swap_hands = !swap_hands; // undo hold set up in _tap_hint
611 #ifndef NO_ACTION_FUNCTION
613 action_function(record, action.func.id, action.func.opt);
620 #ifndef NO_ACTION_LAYER
621 // if this event is a layer action, update the leds
622 switch (action.kind.id) {
624 #ifndef NO_ACTION_TAPPING
626 case ACT_LAYER_TAP_EXT:
628 led_set(host_keyboard_leds());
635 #ifndef NO_ACTION_TAPPING
637 if (!is_tap_key(record->event.key)) {
638 retro_tapping_counter = 0;
642 retro_tapping_counter = 0;
648 retro_tapping_counter = 0;
650 if (retro_tapping_counter == 2) {
651 register_code(action.layer_tap.code);
652 unregister_code(action.layer_tap.code);
654 retro_tapping_counter = 0;
661 #ifndef NO_ACTION_ONESHOT
662 /* Because we switch layers after a oneshot event, we need to release the
663 * key before we leave the layer or no key up event will be generated.
665 if (do_release_oneshot && !(get_oneshot_layer_state() & ONESHOT_PRESSED ) ) {
666 record->event.pressed = false;
667 layer_on(get_oneshot_layer());
668 process_record(record);
669 layer_off(get_oneshot_layer());
678 * Utilities for actions.
680 void register_code(uint8_t code)
686 #ifdef LOCKING_SUPPORT_ENABLE
687 else if (KC_LOCKING_CAPS == code) {
688 #ifdef LOCKING_RESYNC_ENABLE
689 // Resync: ignore if caps lock already is on
690 if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) return;
692 add_key(KC_CAPSLOCK);
693 send_keyboard_report();
695 del_key(KC_CAPSLOCK);
696 send_keyboard_report();
699 else if (KC_LOCKING_NUM == code) {
700 #ifdef LOCKING_RESYNC_ENABLE
701 if (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK)) return;
704 send_keyboard_report();
707 send_keyboard_report();
710 else if (KC_LOCKING_SCROLL == code) {
711 #ifdef LOCKING_RESYNC_ENABLE
712 if (host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK)) return;
714 add_key(KC_SCROLLLOCK);
715 send_keyboard_report();
717 del_key(KC_SCROLLLOCK);
718 send_keyboard_report();
722 else if IS_KEY(code) {
723 // TODO: should push command_proc out of this block?
724 if (command_proc(code)) return;
726 #ifndef NO_ACTION_ONESHOT
728 if (oneshot_state.mods && !oneshot_state.disabled) {
729 uint8_t tmp_mods = get_mods();
730 add_mods(oneshot_state.mods);
733 send_keyboard_report();
736 send_keyboard_report();
743 send_keyboard_report();
746 else if IS_MOD(code) {
747 add_mods(MOD_BIT(code));
748 send_keyboard_report();
750 else if IS_SYSTEM(code) {
751 host_system_send(KEYCODE2SYSTEM(code));
753 else if IS_CONSUMER(code) {
754 host_consumer_send(KEYCODE2CONSUMER(code));
758 void unregister_code(uint8_t code)
764 #ifdef LOCKING_SUPPORT_ENABLE
765 else if (KC_LOCKING_CAPS == code) {
766 #ifdef LOCKING_RESYNC_ENABLE
767 // Resync: ignore if caps lock already is off
768 if (!(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK))) return;
770 add_key(KC_CAPSLOCK);
771 send_keyboard_report();
772 del_key(KC_CAPSLOCK);
773 send_keyboard_report();
776 else if (KC_LOCKING_NUM == code) {
777 #ifdef LOCKING_RESYNC_ENABLE
778 if (!(host_keyboard_leds() & (1<<USB_LED_NUM_LOCK))) return;
781 send_keyboard_report();
783 send_keyboard_report();
786 else if (KC_LOCKING_SCROLL == code) {
787 #ifdef LOCKING_RESYNC_ENABLE
788 if (!(host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK))) return;
790 add_key(KC_SCROLLLOCK);
791 send_keyboard_report();
792 del_key(KC_SCROLLLOCK);
793 send_keyboard_report();
797 else if IS_KEY(code) {
799 send_keyboard_report();
801 else if IS_MOD(code) {
802 del_mods(MOD_BIT(code));
803 send_keyboard_report();
805 else if IS_SYSTEM(code) {
808 else if IS_CONSUMER(code) {
809 host_consumer_send(0);
813 void register_mods(uint8_t mods)
817 send_keyboard_report();
821 void unregister_mods(uint8_t mods)
825 send_keyboard_report();
829 void clear_keyboard(void)
832 clear_keyboard_but_mods();
835 void clear_keyboard_but_mods(void)
840 send_keyboard_report();
841 #ifdef MOUSEKEY_ENABLE
845 #ifdef EXTRAKEY_ENABLE
847 host_consumer_send(0);
851 bool is_tap_key(keypos_t key)
853 action_t action = layer_switch_get_action(key);
855 switch (action.kind.id) {
859 case ACT_LAYER_TAP_EXT:
860 switch (action.layer_tap.code) {
868 switch (action.swap.code) {
870 case OP_SH_TAP_TOGGLE:
876 if (action.func.opt & FUNC_TAP) { return true; }
886 void debug_event(keyevent_t event)
888 dprintf("%04X%c(%u)", (event.key.row<<8 | event.key.col), (event.pressed ? 'd' : 'u'), event.time);
891 void debug_record(keyrecord_t record)
893 debug_event(record.event);
894 #ifndef NO_ACTION_TAPPING
895 dprintf(":%u%c", record.tap.count, (record.tap.interrupted ? '-' : ' '));
899 void debug_action(action_t action)
901 switch (action.kind.id) {
902 case ACT_LMODS: dprint("ACT_LMODS"); break;
903 case ACT_RMODS: dprint("ACT_RMODS"); break;
904 case ACT_LMODS_TAP: dprint("ACT_LMODS_TAP"); break;
905 case ACT_RMODS_TAP: dprint("ACT_RMODS_TAP"); break;
906 case ACT_USAGE: dprint("ACT_USAGE"); break;
907 case ACT_MOUSEKEY: dprint("ACT_MOUSEKEY"); break;
908 case ACT_LAYER: dprint("ACT_LAYER"); break;
909 case ACT_LAYER_TAP: dprint("ACT_LAYER_TAP"); break;
910 case ACT_LAYER_TAP_EXT: dprint("ACT_LAYER_TAP_EXT"); break;
911 case ACT_MACRO: dprint("ACT_MACRO"); break;
912 case ACT_COMMAND: dprint("ACT_COMMAND"); break;
913 case ACT_FUNCTION: dprint("ACT_FUNCTION"); break;
914 case ACT_SWAP_HANDS: dprint("ACT_SWAP_HANDS"); break;
915 default: dprint("UNKNOWN"); break;
917 dprintf("[%X:%02X]", action.kind.param>>8, action.kind.param&0xff);