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;
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();
95 bool swap_hands = false;
97 void process_hand_swap(keyevent_t *event) {
98 static swap_state_row_t swap_state[MATRIX_ROWS];
100 keypos_t pos = event->key;
101 swap_state_row_t col_bit = (swap_state_row_t)1<<pos.col;
102 bool do_swap = event->pressed ? swap_hands :
103 swap_state[pos.row] & (col_bit);
106 event->key = hand_swap_config[pos.row][pos.col];
107 swap_state[pos.row] |= col_bit;
109 swap_state[pos.row] &= ~(col_bit);
114 #if !defined(NO_ACTION_LAYER) && defined(PREVENT_STUCK_MODIFIERS)
115 bool disable_action_cache = false;
117 void process_record_nocache(keyrecord_t *record)
119 disable_action_cache = true;
120 process_record(record);
121 disable_action_cache = false;
124 void process_record_nocache(keyrecord_t *record)
126 process_record(record);
130 __attribute__ ((weak))
131 bool process_record_quantum(keyrecord_t *record) {
135 void process_record(keyrecord_t *record)
137 if (IS_NOEVENT(record->event)) { return; }
139 if(!process_record_quantum(record))
142 action_t action = store_or_get_action(record->event.pressed, record->event.key);
143 dprint("ACTION: "); debug_action(action);
144 #ifndef NO_ACTION_LAYER
145 dprint(" layer_state: "); layer_debug();
146 dprint(" default_layer_state: "); default_layer_debug();
150 process_action(record, action);
153 void process_action(keyrecord_t *record, action_t action)
155 keyevent_t event = record->event;
156 #ifndef NO_ACTION_TAPPING
157 uint8_t tap_count = record->tap.count;
161 // clear the potential weak mods left by previously pressed keys
165 #ifndef NO_ACTION_ONESHOT
166 bool do_release_oneshot = false;
167 // notice we only clear the one shot layer if the pressed key is not a modifier.
168 if (is_oneshot_layer_active() && event.pressed && !IS_MOD(action.key.code)) {
169 clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
170 do_release_oneshot = !is_oneshot_layer_active();
174 switch (action.kind.id) {
179 uint8_t mods = (action.kind.id == ACT_LMODS) ? action.key.mods :
183 if (IS_MOD(action.key.code) || action.key.code == KC_NO) {
184 // e.g. LSFT(KC_LGUI): we don't want the LSFT to be weak as it would make it useless.
185 // This also makes LSFT(KC_LGUI) behave exactly the same as LGUI(KC_LSFT).
186 // Same applies for some keys like KC_MEH which are declared as MEH(KC_NO).
191 send_keyboard_report();
193 register_code(action.key.code);
195 unregister_code(action.key.code);
197 if (IS_MOD(action.key.code) || action.key.code == KC_NO) {
202 send_keyboard_report();
207 #ifndef NO_ACTION_TAPPING
211 uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods :
213 switch (action.layer_tap.code) {
214 #ifndef NO_ACTION_ONESHOT
218 if (tap_count == 0) {
219 dprint("MODS_TAP: Oneshot: 0\n");
221 } else if (tap_count == 1) {
222 dprint("MODS_TAP: Oneshot: start\n");
223 set_oneshot_mods(mods);
224 #if defined(ONESHOT_TAP_TOGGLE) && ONESHOT_TAP_TOGGLE > 1
225 } else if (tap_count == ONESHOT_TAP_TOGGLE) {
226 dprint("MODS_TAP: Toggling oneshot");
227 clear_oneshot_mods();
228 set_oneshot_locked_mods(mods);
235 if (tap_count == 0) {
236 clear_oneshot_mods();
237 unregister_mods(mods);
238 } else if (tap_count == 1) {
239 // Retain Oneshot mods
240 #if defined(ONESHOT_TAP_TOGGLE) && ONESHOT_TAP_TOGGLE > 1
241 if (mods & get_mods()) {
242 clear_oneshot_locked_mods();
243 clear_oneshot_mods();
244 unregister_mods(mods);
246 } else if (tap_count == ONESHOT_TAP_TOGGLE) {
247 // Toggle Oneshot Layer
250 clear_oneshot_mods();
251 unregister_mods(mods);
256 case MODS_TAP_TOGGLE:
258 if (tap_count <= TAPPING_TOGGLE) {
262 if (tap_count < TAPPING_TOGGLE) {
263 unregister_mods(mods);
270 #ifndef IGNORE_MOD_TAP_INTERRUPT
271 if (record->tap.interrupted) {
272 dprint("mods_tap: tap: cancel: add_mods\n");
273 // ad hoc: set 0 to cancel tap
274 record->tap.count = 0;
279 dprint("MODS_TAP: Tap: register_code\n");
280 register_code(action.key.code);
283 dprint("MODS_TAP: No tap: add_mods\n");
288 dprint("MODS_TAP: Tap: unregister_code\n");
289 unregister_code(action.key.code);
291 dprint("MODS_TAP: No tap: add_mods\n");
292 unregister_mods(mods);
300 #ifdef EXTRAKEY_ENABLE
301 /* other HID usage */
303 switch (action.usage.page) {
306 host_system_send(action.usage.code);
313 host_consumer_send(action.usage.code);
315 host_consumer_send(0);
321 #ifdef MOUSEKEY_ENABLE
325 switch (action.key.code) {
327 tp_buttons |= (1<<0);
330 tp_buttons |= (1<<1);
333 tp_buttons |= (1<<2);
338 mousekey_on(action.key.code);
341 switch (action.key.code) {
343 tp_buttons &= ~(1<<0);
346 tp_buttons &= ~(1<<1);
349 tp_buttons &= ~(1<<2);
354 mousekey_off(action.key.code);
359 #ifndef NO_ACTION_LAYER
361 if (action.layer_bitop.on == 0) {
362 /* Default Layer Bitwise Operation */
363 if (!event.pressed) {
364 uint8_t shift = action.layer_bitop.part*4;
365 uint32_t bits = ((uint32_t)action.layer_bitop.bits)<<shift;
366 uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
367 switch (action.layer_bitop.op) {
368 case OP_BIT_AND: default_layer_and(bits | mask); break;
369 case OP_BIT_OR: default_layer_or(bits | mask); break;
370 case OP_BIT_XOR: default_layer_xor(bits | mask); break;
371 case OP_BIT_SET: default_layer_and(mask); default_layer_or(bits); break;
375 /* Layer Bitwise Operation */
376 if (event.pressed ? (action.layer_bitop.on & ON_PRESS) :
377 (action.layer_bitop.on & ON_RELEASE)) {
378 uint8_t shift = action.layer_bitop.part*4;
379 uint32_t bits = ((uint32_t)action.layer_bitop.bits)<<shift;
380 uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
381 switch (action.layer_bitop.op) {
382 case OP_BIT_AND: layer_and(bits | mask); break;
383 case OP_BIT_OR: layer_or(bits | mask); break;
384 case OP_BIT_XOR: layer_xor(bits | mask); break;
385 case OP_BIT_SET: layer_and(mask); layer_or(bits); break;
390 #ifndef NO_ACTION_TAPPING
392 case ACT_LAYER_TAP_EXT:
393 switch (action.layer_tap.code) {
395 /* layer On/Off with modifiers(left only) */
397 layer_on(action.layer_tap.val);
398 register_mods(action.layer_tap.code & 0x0f);
400 layer_off(action.layer_tap.val);
401 unregister_mods(action.layer_tap.code & 0x0f);
407 if (tap_count < TAPPING_TOGGLE) {
408 layer_invert(action.layer_tap.val);
411 if (tap_count <= TAPPING_TOGGLE) {
412 layer_invert(action.layer_tap.val);
417 event.pressed ? layer_on(action.layer_tap.val) :
418 layer_off(action.layer_tap.val);
421 event.pressed ? layer_off(action.layer_tap.val) :
422 layer_on(action.layer_tap.val);
425 event.pressed ? layer_move(action.layer_tap.val) :
428 #ifndef NO_ACTION_ONESHOT
431 #if defined(ONESHOT_TAP_TOGGLE) && ONESHOT_TAP_TOGGLE > 1
432 do_release_oneshot = false;
434 del_mods(get_oneshot_locked_mods());
435 if (get_oneshot_layer_state() == ONESHOT_TOGGLED) {
436 reset_oneshot_layer();
437 layer_off(action.layer_tap.val);
439 } else if (tap_count < ONESHOT_TAP_TOGGLE) {
440 layer_on(action.layer_tap.val);
441 set_oneshot_layer(action.layer_tap.val, ONESHOT_START);
444 add_mods(get_oneshot_locked_mods());
445 if (tap_count >= ONESHOT_TAP_TOGGLE) {
446 reset_oneshot_layer();
447 clear_oneshot_locked_mods();
448 set_oneshot_layer(action.layer_tap.val, ONESHOT_TOGGLED);
450 clear_oneshot_layer_state(ONESHOT_PRESSED);
455 layer_on(action.layer_tap.val);
456 set_oneshot_layer(action.layer_tap.val, ONESHOT_START);
458 clear_oneshot_layer_state(ONESHOT_PRESSED);
460 clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
470 dprint("KEYMAP_TAP_KEY: Tap: register_code\n");
471 register_code(action.layer_tap.code);
473 dprint("KEYMAP_TAP_KEY: No tap: On on press\n");
474 layer_on(action.layer_tap.val);
478 dprint("KEYMAP_TAP_KEY: Tap: unregister_code\n");
479 if (action.layer_tap.code == KC_CAPS) {
482 unregister_code(action.layer_tap.code);
484 dprint("KEYMAP_TAP_KEY: No tap: Off on release\n");
485 layer_off(action.layer_tap.val);
494 #ifndef NO_ACTION_MACRO
496 action_macro_play(action_get_macro(record, action.func.id, action.func.opt));
499 #ifdef BACKLIGHT_ENABLE
501 if (!event.pressed) {
502 switch (action.backlight.opt) {
503 case BACKLIGHT_INCREASE:
504 backlight_increase();
506 case BACKLIGHT_DECREASE:
507 backlight_decrease();
509 case BACKLIGHT_TOGGLE:
515 case BACKLIGHT_LEVEL:
516 backlight_level(action.backlight.level);
524 #ifdef ONEHAND_ENABLE
526 switch (action.swap.code) {
529 swap_hands = !swap_hands;
533 swap_hands = event.pressed;
536 swap_hands = !event.pressed;
539 if (!event.pressed) {
544 if (!event.pressed) {
548 #ifndef NO_ACTION_TAPPING
549 case OP_SH_TAP_TOGGLE:
552 if (!event.pressed) {
553 swap_hands = !swap_hands;
556 swap_hands = event.pressed;
562 register_code(action.swap.code);
564 unregister_code(action.swap.code);
567 swap_hands = event.pressed;
572 #ifndef NO_ACTION_FUNCTION
574 action_function(record, action.func.id, action.func.opt);
581 #ifndef NO_ACTION_LAYER
582 // if this event is a layer action, update the leds
583 switch (action.kind.id) {
585 #ifndef NO_ACTION_TAPPING
587 case ACT_LAYER_TAP_EXT:
589 led_set(host_keyboard_leds());
596 #ifndef NO_ACTION_TAPPING
598 if (!is_tap_key(record->event.key)) {
599 retro_tapping_counter = 0;
603 retro_tapping_counter = 0;
609 retro_tapping_counter = 0;
611 if (retro_tapping_counter == 2) {
612 register_code(action.layer_tap.code);
613 unregister_code(action.layer_tap.code);
615 retro_tapping_counter = 0;
622 #ifndef NO_ACTION_ONESHOT
623 /* Because we switch layers after a oneshot event, we need to release the
624 * key before we leave the layer or no key up event will be generated.
626 if (do_release_oneshot && !(get_oneshot_layer_state() & ONESHOT_PRESSED ) ) {
627 record->event.pressed = false;
628 layer_on(get_oneshot_layer());
629 process_record(record);
630 layer_off(get_oneshot_layer());
639 * Utilities for actions.
641 void register_code(uint8_t code)
647 #ifdef LOCKING_SUPPORT_ENABLE
648 else if (KC_LOCKING_CAPS == code) {
649 #ifdef LOCKING_RESYNC_ENABLE
650 // Resync: ignore if caps lock already is on
651 if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) return;
653 add_key(KC_CAPSLOCK);
654 send_keyboard_report();
656 del_key(KC_CAPSLOCK);
657 send_keyboard_report();
660 else if (KC_LOCKING_NUM == code) {
661 #ifdef LOCKING_RESYNC_ENABLE
662 if (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK)) return;
665 send_keyboard_report();
668 send_keyboard_report();
671 else if (KC_LOCKING_SCROLL == code) {
672 #ifdef LOCKING_RESYNC_ENABLE
673 if (host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK)) return;
675 add_key(KC_SCROLLLOCK);
676 send_keyboard_report();
678 del_key(KC_SCROLLLOCK);
679 send_keyboard_report();
683 else if IS_KEY(code) {
684 // TODO: should push command_proc out of this block?
685 if (command_proc(code)) return;
687 #ifndef NO_ACTION_ONESHOT
689 if (oneshot_state.mods && !oneshot_state.disabled) {
690 uint8_t tmp_mods = get_mods();
691 add_mods(oneshot_state.mods);
694 send_keyboard_report();
697 send_keyboard_report();
704 send_keyboard_report();
707 else if IS_MOD(code) {
708 add_mods(MOD_BIT(code));
709 send_keyboard_report();
711 else if IS_SYSTEM(code) {
712 host_system_send(KEYCODE2SYSTEM(code));
714 else if IS_CONSUMER(code) {
715 host_consumer_send(KEYCODE2CONSUMER(code));
719 void unregister_code(uint8_t code)
725 #ifdef LOCKING_SUPPORT_ENABLE
726 else if (KC_LOCKING_CAPS == code) {
727 #ifdef LOCKING_RESYNC_ENABLE
728 // Resync: ignore if caps lock already is off
729 if (!(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK))) return;
731 add_key(KC_CAPSLOCK);
732 send_keyboard_report();
733 del_key(KC_CAPSLOCK);
734 send_keyboard_report();
737 else if (KC_LOCKING_NUM == code) {
738 #ifdef LOCKING_RESYNC_ENABLE
739 if (!(host_keyboard_leds() & (1<<USB_LED_NUM_LOCK))) return;
742 send_keyboard_report();
744 send_keyboard_report();
747 else if (KC_LOCKING_SCROLL == code) {
748 #ifdef LOCKING_RESYNC_ENABLE
749 if (!(host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK))) return;
751 add_key(KC_SCROLLLOCK);
752 send_keyboard_report();
753 del_key(KC_SCROLLLOCK);
754 send_keyboard_report();
758 else if IS_KEY(code) {
760 send_keyboard_report();
762 else if IS_MOD(code) {
763 del_mods(MOD_BIT(code));
764 send_keyboard_report();
766 else if IS_SYSTEM(code) {
769 else if IS_CONSUMER(code) {
770 host_consumer_send(0);
774 void register_mods(uint8_t mods)
778 send_keyboard_report();
782 void unregister_mods(uint8_t mods)
786 send_keyboard_report();
790 void clear_keyboard(void)
793 clear_keyboard_but_mods();
796 void clear_keyboard_but_mods(void)
801 send_keyboard_report();
802 #ifdef MOUSEKEY_ENABLE
806 #ifdef EXTRAKEY_ENABLE
808 host_consumer_send(0);
812 bool is_tap_key(keypos_t key)
814 action_t action = layer_switch_get_action(key);
816 switch (action.kind.id) {
820 case ACT_LAYER_TAP_EXT:
821 switch (action.layer_tap.code) {
829 switch (action.swap.code) {
831 case OP_SH_TAP_TOGGLE:
837 if (action.func.opt & FUNC_TAP) { return true; }
847 void debug_event(keyevent_t event)
849 dprintf("%04X%c(%u)", (event.key.row<<8 | event.key.col), (event.pressed ? 'd' : 'u'), event.time);
852 void debug_record(keyrecord_t record)
854 debug_event(record.event);
855 #ifndef NO_ACTION_TAPPING
856 dprintf(":%u%c", record.tap.count, (record.tap.interrupted ? '-' : ' '));
860 void debug_action(action_t action)
862 switch (action.kind.id) {
863 case ACT_LMODS: dprint("ACT_LMODS"); break;
864 case ACT_RMODS: dprint("ACT_RMODS"); break;
865 case ACT_LMODS_TAP: dprint("ACT_LMODS_TAP"); break;
866 case ACT_RMODS_TAP: dprint("ACT_RMODS_TAP"); break;
867 case ACT_USAGE: dprint("ACT_USAGE"); break;
868 case ACT_MOUSEKEY: dprint("ACT_MOUSEKEY"); break;
869 case ACT_LAYER: dprint("ACT_LAYER"); break;
870 case ACT_LAYER_TAP: dprint("ACT_LAYER_TAP"); break;
871 case ACT_LAYER_TAP_EXT: dprint("ACT_LAYER_TAP_EXT"); break;
872 case ACT_MACRO: dprint("ACT_MACRO"); break;
873 case ACT_COMMAND: dprint("ACT_COMMAND"); break;
874 case ACT_FUNCTION: dprint("ACT_FUNCTION"); break;
875 case ACT_SWAP_HANDS: dprint("ACT_SWAP_HANDS"); break;
876 default: dprint("UNKNOWN"); break;
878 dprintf("[%X:%02X]", action.kind.param>>8, action.kind.param&0xff);