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:
516 backlight_level(BACKLIGHT_LEVELS);
527 #ifdef ONEHAND_ENABLE
529 switch (action.swap.code) {
532 swap_hands = !swap_hands;
536 swap_hands = event.pressed;
539 swap_hands = !event.pressed;
542 if (!event.pressed) {
547 if (!event.pressed) {
551 #ifndef NO_ACTION_TAPPING
552 case OP_SH_TAP_TOGGLE:
555 if (!event.pressed) {
556 swap_hands = !swap_hands;
559 swap_hands = event.pressed;
565 register_code(action.swap.code);
567 unregister_code(action.swap.code);
570 swap_hands = event.pressed;
575 #ifndef NO_ACTION_FUNCTION
577 action_function(record, action.func.id, action.func.opt);
584 #ifndef NO_ACTION_LAYER
585 // if this event is a layer action, update the leds
586 switch (action.kind.id) {
588 #ifndef NO_ACTION_TAPPING
590 case ACT_LAYER_TAP_EXT:
592 led_set(host_keyboard_leds());
599 #ifndef NO_ACTION_TAPPING
601 if (!is_tap_key(record->event.key)) {
602 retro_tapping_counter = 0;
606 retro_tapping_counter = 0;
612 retro_tapping_counter = 0;
614 if (retro_tapping_counter == 2) {
615 register_code(action.layer_tap.code);
616 unregister_code(action.layer_tap.code);
618 retro_tapping_counter = 0;
625 #ifndef NO_ACTION_ONESHOT
626 /* Because we switch layers after a oneshot event, we need to release the
627 * key before we leave the layer or no key up event will be generated.
629 if (do_release_oneshot && !(get_oneshot_layer_state() & ONESHOT_PRESSED ) ) {
630 record->event.pressed = false;
631 layer_on(get_oneshot_layer());
632 process_record(record);
633 layer_off(get_oneshot_layer());
642 * Utilities for actions.
644 void register_code(uint8_t code)
650 #ifdef LOCKING_SUPPORT_ENABLE
651 else if (KC_LOCKING_CAPS == code) {
652 #ifdef LOCKING_RESYNC_ENABLE
653 // Resync: ignore if caps lock already is on
654 if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) return;
656 add_key(KC_CAPSLOCK);
657 send_keyboard_report();
659 del_key(KC_CAPSLOCK);
660 send_keyboard_report();
663 else if (KC_LOCKING_NUM == code) {
664 #ifdef LOCKING_RESYNC_ENABLE
665 if (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK)) return;
668 send_keyboard_report();
671 send_keyboard_report();
674 else if (KC_LOCKING_SCROLL == code) {
675 #ifdef LOCKING_RESYNC_ENABLE
676 if (host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK)) return;
678 add_key(KC_SCROLLLOCK);
679 send_keyboard_report();
681 del_key(KC_SCROLLLOCK);
682 send_keyboard_report();
686 else if IS_KEY(code) {
687 // TODO: should push command_proc out of this block?
688 if (command_proc(code)) return;
690 #ifndef NO_ACTION_ONESHOT
692 if (oneshot_state.mods && !oneshot_state.disabled) {
693 uint8_t tmp_mods = get_mods();
694 add_mods(oneshot_state.mods);
697 send_keyboard_report();
700 send_keyboard_report();
707 send_keyboard_report();
710 else if IS_MOD(code) {
711 add_mods(MOD_BIT(code));
712 send_keyboard_report();
714 else if IS_SYSTEM(code) {
715 host_system_send(KEYCODE2SYSTEM(code));
717 else if IS_CONSUMER(code) {
718 host_consumer_send(KEYCODE2CONSUMER(code));
722 void unregister_code(uint8_t code)
728 #ifdef LOCKING_SUPPORT_ENABLE
729 else if (KC_LOCKING_CAPS == code) {
730 #ifdef LOCKING_RESYNC_ENABLE
731 // Resync: ignore if caps lock already is off
732 if (!(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK))) return;
734 add_key(KC_CAPSLOCK);
735 send_keyboard_report();
736 del_key(KC_CAPSLOCK);
737 send_keyboard_report();
740 else if (KC_LOCKING_NUM == code) {
741 #ifdef LOCKING_RESYNC_ENABLE
742 if (!(host_keyboard_leds() & (1<<USB_LED_NUM_LOCK))) return;
745 send_keyboard_report();
747 send_keyboard_report();
750 else if (KC_LOCKING_SCROLL == code) {
751 #ifdef LOCKING_RESYNC_ENABLE
752 if (!(host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK))) return;
754 add_key(KC_SCROLLLOCK);
755 send_keyboard_report();
756 del_key(KC_SCROLLLOCK);
757 send_keyboard_report();
761 else if IS_KEY(code) {
763 send_keyboard_report();
765 else if IS_MOD(code) {
766 del_mods(MOD_BIT(code));
767 send_keyboard_report();
769 else if IS_SYSTEM(code) {
772 else if IS_CONSUMER(code) {
773 host_consumer_send(0);
777 void register_mods(uint8_t mods)
781 send_keyboard_report();
785 void unregister_mods(uint8_t mods)
789 send_keyboard_report();
793 void clear_keyboard(void)
796 clear_keyboard_but_mods();
799 void clear_keyboard_but_mods(void)
804 send_keyboard_report();
805 #ifdef MOUSEKEY_ENABLE
809 #ifdef EXTRAKEY_ENABLE
811 host_consumer_send(0);
815 bool is_tap_key(keypos_t key)
817 action_t action = layer_switch_get_action(key);
819 switch (action.kind.id) {
823 case ACT_LAYER_TAP_EXT:
824 switch (action.layer_tap.code) {
832 switch (action.swap.code) {
834 case OP_SH_TAP_TOGGLE:
840 if (action.func.opt & FUNC_TAP) { return true; }
850 void debug_event(keyevent_t event)
852 dprintf("%04X%c(%u)", (event.key.row<<8 | event.key.col), (event.pressed ? 'd' : 'u'), event.time);
855 void debug_record(keyrecord_t record)
857 debug_event(record.event);
858 #ifndef NO_ACTION_TAPPING
859 dprintf(":%u%c", record.tap.count, (record.tap.interrupted ? '-' : ' '));
863 void debug_action(action_t action)
865 switch (action.kind.id) {
866 case ACT_LMODS: dprint("ACT_LMODS"); break;
867 case ACT_RMODS: dprint("ACT_RMODS"); break;
868 case ACT_LMODS_TAP: dprint("ACT_LMODS_TAP"); break;
869 case ACT_RMODS_TAP: dprint("ACT_RMODS_TAP"); break;
870 case ACT_USAGE: dprint("ACT_USAGE"); break;
871 case ACT_MOUSEKEY: dprint("ACT_MOUSEKEY"); break;
872 case ACT_LAYER: dprint("ACT_LAYER"); break;
873 case ACT_LAYER_TAP: dprint("ACT_LAYER_TAP"); break;
874 case ACT_LAYER_TAP_EXT: dprint("ACT_LAYER_TAP_EXT"); break;
875 case ACT_MACRO: dprint("ACT_MACRO"); break;
876 case ACT_COMMAND: dprint("ACT_COMMAND"); break;
877 case ACT_FUNCTION: dprint("ACT_FUNCTION"); break;
878 case ACT_SWAP_HANDS: dprint("ACT_SWAP_HANDS"); break;
879 default: dprint("UNKNOWN"); break;
881 dprintf("[%X:%02X]", action.kind.param>>8, action.kind.param&0xff);