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"
39 #ifdef FAUXCLICKY_ENABLE
40 #include <fauxclicky.h>
43 void action_exec(keyevent_t event)
45 if (!IS_NOEVENT(event)) {
46 dprint("\n---- action_exec: start -----\n");
47 dprint("EVENT: "); debug_event(event); dprintln();
50 #ifdef FAUXCLICKY_ENABLE
51 if (IS_PRESSED(event)) {
52 FAUXCLICKY_ACTION_PRESS;
54 if (IS_RELEASED(event)) {
55 FAUXCLICKY_ACTION_RELEASE;
61 if (!IS_NOEVENT(event)) {
62 process_hand_swap(&event);
66 keyrecord_t record = { .event = event };
68 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
69 if (has_oneshot_layer_timed_out()) {
70 dprintf("Oneshot layer: timeout\n");
71 clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
75 #ifndef NO_ACTION_TAPPING
76 action_tapping_process(record);
78 process_record(&record);
79 if (!IS_NOEVENT(record.event)) {
80 dprint("processed: "); debug_record(record); dprintln();
86 bool swap_hands = false;
88 void process_hand_swap(keyevent_t *event) {
89 static swap_state_row_t swap_state[MATRIX_ROWS];
91 keypos_t pos = event->key;
92 swap_state_row_t col_bit = (swap_state_row_t)1<<pos.col;
93 bool do_swap = event->pressed ? swap_hands :
94 swap_state[pos.row] & (col_bit);
97 event->key = hand_swap_config[pos.row][pos.col];
98 swap_state[pos.row] |= col_bit;
100 swap_state[pos.row] &= ~(col_bit);
105 #if !defined(NO_ACTION_LAYER) && defined(PREVENT_STUCK_MODIFIERS)
106 bool disable_action_cache = false;
108 void process_record_nocache(keyrecord_t *record)
110 disable_action_cache = true;
111 process_record(record);
112 disable_action_cache = false;
115 void process_record_nocache(keyrecord_t *record)
117 process_record(record);
121 __attribute__ ((weak))
122 bool process_record_quantum(keyrecord_t *record) {
126 void process_record(keyrecord_t *record)
128 if (IS_NOEVENT(record->event)) { return; }
130 if(!process_record_quantum(record))
133 action_t action = store_or_get_action(record->event.pressed, record->event.key);
134 dprint("ACTION: "); debug_action(action);
135 #ifndef NO_ACTION_LAYER
136 dprint(" layer_state: "); layer_debug();
137 dprint(" default_layer_state: "); default_layer_debug();
141 process_action(record, action);
144 void process_action(keyrecord_t *record, action_t action)
146 keyevent_t event = record->event;
147 #ifndef NO_ACTION_TAPPING
148 uint8_t tap_count = record->tap.count;
152 // clear the potential weak mods left by previously pressed keys
156 #ifndef NO_ACTION_ONESHOT
157 bool do_release_oneshot = false;
158 // notice we only clear the one shot layer if the pressed key is not a modifier.
159 if (is_oneshot_layer_active() && event.pressed && !IS_MOD(action.key.code)) {
160 clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
161 do_release_oneshot = !is_oneshot_layer_active();
165 switch (action.kind.id) {
170 uint8_t mods = (action.kind.id == ACT_LMODS) ? action.key.mods :
174 if (IS_MOD(action.key.code) || action.key.code == KC_NO) {
175 // e.g. LSFT(KC_LGUI): we don't want the LSFT to be weak as it would make it useless.
176 // This also makes LSFT(KC_LGUI) behave exactly the same as LGUI(KC_LSFT).
177 // Same applies for some keys like KC_MEH which are declared as MEH(KC_NO).
182 send_keyboard_report();
184 register_code(action.key.code);
186 unregister_code(action.key.code);
188 if (IS_MOD(action.key.code) || action.key.code == KC_NO) {
193 send_keyboard_report();
198 #ifndef NO_ACTION_TAPPING
202 uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods :
204 switch (action.layer_tap.code) {
205 #ifndef NO_ACTION_ONESHOT
209 if (tap_count == 0) {
210 dprint("MODS_TAP: Oneshot: 0\n");
212 } else if (tap_count == 1) {
213 dprint("MODS_TAP: Oneshot: start\n");
214 set_oneshot_mods(mods);
215 #if defined(ONESHOT_TAP_TOGGLE) && ONESHOT_TAP_TOGGLE > 1
216 } else if (tap_count == ONESHOT_TAP_TOGGLE) {
217 dprint("MODS_TAP: Toggling oneshot");
218 clear_oneshot_mods();
219 set_oneshot_locked_mods(mods);
226 if (tap_count == 0) {
227 clear_oneshot_mods();
228 unregister_mods(mods);
229 } else if (tap_count == 1) {
230 // Retain Oneshot mods
231 #if defined(ONESHOT_TAP_TOGGLE) && ONESHOT_TAP_TOGGLE > 1
232 if (mods & get_mods()) {
233 clear_oneshot_locked_mods();
234 clear_oneshot_mods();
235 unregister_mods(mods);
237 } else if (tap_count == ONESHOT_TAP_TOGGLE) {
238 // Toggle Oneshot Layer
241 clear_oneshot_mods();
242 unregister_mods(mods);
247 case MODS_TAP_TOGGLE:
249 if (tap_count <= TAPPING_TOGGLE) {
253 if (tap_count < TAPPING_TOGGLE) {
254 unregister_mods(mods);
261 #ifndef IGNORE_MOD_TAP_INTERRUPT
262 if (record->tap.interrupted) {
263 dprint("mods_tap: tap: cancel: add_mods\n");
264 // ad hoc: set 0 to cancel tap
265 record->tap.count = 0;
270 dprint("MODS_TAP: Tap: register_code\n");
271 register_code(action.key.code);
274 dprint("MODS_TAP: No tap: add_mods\n");
279 dprint("MODS_TAP: Tap: unregister_code\n");
280 unregister_code(action.key.code);
282 dprint("MODS_TAP: No tap: add_mods\n");
283 unregister_mods(mods);
291 #ifdef EXTRAKEY_ENABLE
292 /* other HID usage */
294 switch (action.usage.page) {
297 host_system_send(action.usage.code);
304 host_consumer_send(action.usage.code);
306 host_consumer_send(0);
312 #ifdef MOUSEKEY_ENABLE
316 switch (action.key.code) {
318 tp_buttons |= (1<<0);
321 tp_buttons |= (1<<1);
324 tp_buttons |= (1<<2);
329 mousekey_on(action.key.code);
332 switch (action.key.code) {
334 tp_buttons &= ~(1<<0);
337 tp_buttons &= ~(1<<1);
340 tp_buttons &= ~(1<<2);
345 mousekey_off(action.key.code);
350 #ifndef NO_ACTION_LAYER
352 if (action.layer_bitop.on == 0) {
353 /* Default Layer Bitwise Operation */
354 if (!event.pressed) {
355 uint8_t shift = action.layer_bitop.part*4;
356 uint32_t bits = ((uint32_t)action.layer_bitop.bits)<<shift;
357 uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
358 switch (action.layer_bitop.op) {
359 case OP_BIT_AND: default_layer_and(bits | mask); break;
360 case OP_BIT_OR: default_layer_or(bits | mask); break;
361 case OP_BIT_XOR: default_layer_xor(bits | mask); break;
362 case OP_BIT_SET: default_layer_and(mask); default_layer_or(bits); break;
366 /* Layer Bitwise Operation */
367 if (event.pressed ? (action.layer_bitop.on & ON_PRESS) :
368 (action.layer_bitop.on & ON_RELEASE)) {
369 uint8_t shift = action.layer_bitop.part*4;
370 uint32_t bits = ((uint32_t)action.layer_bitop.bits)<<shift;
371 uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
372 switch (action.layer_bitop.op) {
373 case OP_BIT_AND: layer_and(bits | mask); break;
374 case OP_BIT_OR: layer_or(bits | mask); break;
375 case OP_BIT_XOR: layer_xor(bits | mask); break;
376 case OP_BIT_SET: layer_and(mask); layer_or(bits); break;
381 #ifndef NO_ACTION_TAPPING
383 case ACT_LAYER_TAP_EXT:
384 switch (action.layer_tap.code) {
386 /* layer On/Off with modifiers(left only) */
388 layer_on(action.layer_tap.val);
389 register_mods(action.layer_tap.code & 0x0f);
391 layer_off(action.layer_tap.val);
392 unregister_mods(action.layer_tap.code & 0x0f);
398 if (tap_count < TAPPING_TOGGLE) {
399 layer_invert(action.layer_tap.val);
402 if (tap_count <= TAPPING_TOGGLE) {
403 layer_invert(action.layer_tap.val);
408 event.pressed ? layer_on(action.layer_tap.val) :
409 layer_off(action.layer_tap.val);
412 event.pressed ? layer_off(action.layer_tap.val) :
413 layer_on(action.layer_tap.val);
416 event.pressed ? layer_move(action.layer_tap.val) :
419 #ifndef NO_ACTION_ONESHOT
422 #if defined(ONESHOT_TAP_TOGGLE) && ONESHOT_TAP_TOGGLE > 1
423 do_release_oneshot = false;
425 del_mods(get_oneshot_locked_mods());
426 if (get_oneshot_layer_state() == ONESHOT_TOGGLED) {
427 reset_oneshot_layer();
428 layer_off(action.layer_tap.val);
430 } else if (tap_count < ONESHOT_TAP_TOGGLE) {
431 layer_on(action.layer_tap.val);
432 set_oneshot_layer(action.layer_tap.val, ONESHOT_START);
435 add_mods(get_oneshot_locked_mods());
436 if (tap_count >= ONESHOT_TAP_TOGGLE) {
437 reset_oneshot_layer();
438 clear_oneshot_locked_mods();
439 set_oneshot_layer(action.layer_tap.val, ONESHOT_TOGGLED);
441 clear_oneshot_layer_state(ONESHOT_PRESSED);
446 layer_on(action.layer_tap.val);
447 set_oneshot_layer(action.layer_tap.val, ONESHOT_START);
449 clear_oneshot_layer_state(ONESHOT_PRESSED);
451 clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
461 dprint("KEYMAP_TAP_KEY: Tap: register_code\n");
462 register_code(action.layer_tap.code);
464 dprint("KEYMAP_TAP_KEY: No tap: On on press\n");
465 layer_on(action.layer_tap.val);
469 dprint("KEYMAP_TAP_KEY: Tap: unregister_code\n");
470 if (action.layer_tap.code == KC_CAPS) {
473 unregister_code(action.layer_tap.code);
475 dprint("KEYMAP_TAP_KEY: No tap: Off on release\n");
476 layer_off(action.layer_tap.val);
485 #ifndef NO_ACTION_MACRO
487 action_macro_play(action_get_macro(record, action.func.id, action.func.opt));
490 #ifdef BACKLIGHT_ENABLE
492 if (!event.pressed) {
493 switch (action.backlight.opt) {
494 case BACKLIGHT_INCREASE:
495 backlight_increase();
497 case BACKLIGHT_DECREASE:
498 backlight_decrease();
500 case BACKLIGHT_TOGGLE:
506 case BACKLIGHT_LEVEL:
507 backlight_level(action.backlight.level);
515 #ifdef ONEHAND_ENABLE
517 switch (action.swap.code) {
520 swap_hands = !swap_hands;
524 swap_hands = event.pressed;
527 swap_hands = !event.pressed;
530 if (!event.pressed) {
535 if (!event.pressed) {
539 #ifndef NO_ACTION_TAPPING
540 case OP_SH_TAP_TOGGLE:
543 if (!event.pressed) {
544 swap_hands = !swap_hands;
547 swap_hands = event.pressed;
553 register_code(action.swap.code);
555 unregister_code(action.swap.code);
558 swap_hands = event.pressed;
563 #ifndef NO_ACTION_FUNCTION
565 action_function(record, action.func.id, action.func.opt);
572 #ifndef NO_ACTION_LAYER
573 // if this event is a layer action, update the leds
574 switch (action.kind.id) {
576 #ifndef NO_ACTION_TAPPING
578 case ACT_LAYER_TAP_EXT:
580 led_set(host_keyboard_leds());
587 #ifndef NO_ACTION_ONESHOT
588 /* Because we switch layers after a oneshot event, we need to release the
589 * key before we leave the layer or no key up event will be generated.
591 if (do_release_oneshot && !(get_oneshot_layer_state() & ONESHOT_PRESSED ) ) {
592 record->event.pressed = false;
593 layer_on(get_oneshot_layer());
594 process_record(record);
595 layer_off(get_oneshot_layer());
604 * Utilities for actions.
606 void register_code(uint8_t code)
612 #ifdef LOCKING_SUPPORT_ENABLE
613 else if (KC_LOCKING_CAPS == code) {
614 #ifdef LOCKING_RESYNC_ENABLE
615 // Resync: ignore if caps lock already is on
616 if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) return;
618 add_key(KC_CAPSLOCK);
619 send_keyboard_report();
620 del_key(KC_CAPSLOCK);
621 send_keyboard_report();
624 else if (KC_LOCKING_NUM == code) {
625 #ifdef LOCKING_RESYNC_ENABLE
626 if (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK)) return;
629 send_keyboard_report();
631 send_keyboard_report();
634 else if (KC_LOCKING_SCROLL == code) {
635 #ifdef LOCKING_RESYNC_ENABLE
636 if (host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK)) return;
638 add_key(KC_SCROLLLOCK);
639 send_keyboard_report();
640 del_key(KC_SCROLLLOCK);
641 send_keyboard_report();
645 else if IS_KEY(code) {
646 // TODO: should push command_proc out of this block?
647 if (command_proc(code)) return;
649 #ifndef NO_ACTION_ONESHOT
651 if (oneshot_state.mods && !oneshot_state.disabled) {
652 uint8_t tmp_mods = get_mods();
653 add_mods(oneshot_state.mods);
656 send_keyboard_report();
659 send_keyboard_report();
666 send_keyboard_report();
669 else if IS_MOD(code) {
670 add_mods(MOD_BIT(code));
671 send_keyboard_report();
673 else if IS_SYSTEM(code) {
674 host_system_send(KEYCODE2SYSTEM(code));
676 else if IS_CONSUMER(code) {
677 host_consumer_send(KEYCODE2CONSUMER(code));
681 void unregister_code(uint8_t code)
687 #ifdef LOCKING_SUPPORT_ENABLE
688 else if (KC_LOCKING_CAPS == code) {
689 #ifdef LOCKING_RESYNC_ENABLE
690 // Resync: ignore if caps lock already is off
691 if (!(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK))) return;
693 add_key(KC_CAPSLOCK);
694 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();
706 send_keyboard_report();
709 else if (KC_LOCKING_SCROLL == code) {
710 #ifdef LOCKING_RESYNC_ENABLE
711 if (!(host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK))) return;
713 add_key(KC_SCROLLLOCK);
714 send_keyboard_report();
715 del_key(KC_SCROLLLOCK);
716 send_keyboard_report();
720 else if IS_KEY(code) {
722 send_keyboard_report();
724 else if IS_MOD(code) {
725 del_mods(MOD_BIT(code));
726 send_keyboard_report();
728 else if IS_SYSTEM(code) {
731 else if IS_CONSUMER(code) {
732 host_consumer_send(0);
736 void register_mods(uint8_t mods)
740 send_keyboard_report();
744 void unregister_mods(uint8_t mods)
748 send_keyboard_report();
752 void clear_keyboard(void)
755 clear_keyboard_but_mods();
758 void clear_keyboard_but_mods(void)
763 send_keyboard_report();
764 #ifdef MOUSEKEY_ENABLE
768 #ifdef EXTRAKEY_ENABLE
770 host_consumer_send(0);
774 bool is_tap_key(keypos_t key)
776 action_t action = layer_switch_get_action(key);
778 switch (action.kind.id) {
782 case ACT_LAYER_TAP_EXT:
783 switch (action.layer_tap.code) {
791 switch (action.swap.code) {
793 case OP_SH_TAP_TOGGLE:
799 if (action.func.opt & FUNC_TAP) { return true; }
809 void debug_event(keyevent_t event)
811 dprintf("%04X%c(%u)", (event.key.row<<8 | event.key.col), (event.pressed ? 'd' : 'u'), event.time);
814 void debug_record(keyrecord_t record)
816 debug_event(record.event);
817 #ifndef NO_ACTION_TAPPING
818 dprintf(":%u%c", record.tap.count, (record.tap.interrupted ? '-' : ' '));
822 void debug_action(action_t action)
824 switch (action.kind.id) {
825 case ACT_LMODS: dprint("ACT_LMODS"); break;
826 case ACT_RMODS: dprint("ACT_RMODS"); break;
827 case ACT_LMODS_TAP: dprint("ACT_LMODS_TAP"); break;
828 case ACT_RMODS_TAP: dprint("ACT_RMODS_TAP"); break;
829 case ACT_USAGE: dprint("ACT_USAGE"); break;
830 case ACT_MOUSEKEY: dprint("ACT_MOUSEKEY"); break;
831 case ACT_LAYER: dprint("ACT_LAYER"); break;
832 case ACT_LAYER_TAP: dprint("ACT_LAYER_TAP"); break;
833 case ACT_LAYER_TAP_EXT: dprint("ACT_LAYER_TAP_EXT"); break;
834 case ACT_MACRO: dprint("ACT_MACRO"); break;
835 case ACT_COMMAND: dprint("ACT_COMMAND"); break;
836 case ACT_FUNCTION: dprint("ACT_FUNCTION"); break;
837 case ACT_SWAP_HANDS: dprint("ACT_SWAP_HANDS"); break;
838 default: dprint("UNKNOWN"); break;
840 dprintf("[%X:%02X]", action.kind.param>>8, action.kind.param&0xff);