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 clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
72 if (has_oneshot_mods_timed_out()) {
77 #ifndef NO_ACTION_TAPPING
78 action_tapping_process(record);
80 process_record(&record);
81 if (!IS_NOEVENT(record.event)) {
82 dprint("processed: "); debug_record(record); dprintln();
88 bool swap_hands = false;
90 void process_hand_swap(keyevent_t *event) {
91 static swap_state_row_t swap_state[MATRIX_ROWS];
93 keypos_t pos = event->key;
94 swap_state_row_t col_bit = (swap_state_row_t)1<<pos.col;
95 bool do_swap = event->pressed ? swap_hands :
96 swap_state[pos.row] & (col_bit);
99 event->key = hand_swap_config[pos.row][pos.col];
100 swap_state[pos.row] |= col_bit;
102 swap_state[pos.row] &= ~(col_bit);
107 #if !defined(NO_ACTION_LAYER) && defined(PREVENT_STUCK_MODIFIERS)
108 bool disable_action_cache = false;
110 void process_record_nocache(keyrecord_t *record)
112 disable_action_cache = true;
113 process_record(record);
114 disable_action_cache = false;
117 void process_record_nocache(keyrecord_t *record)
119 process_record(record);
123 __attribute__ ((weak))
124 bool process_record_quantum(keyrecord_t *record) {
128 void process_record(keyrecord_t *record)
130 if (IS_NOEVENT(record->event)) { return; }
132 if(!process_record_quantum(record))
135 action_t action = store_or_get_action(record->event.pressed, record->event.key);
136 dprint("ACTION: "); debug_action(action);
137 #ifndef NO_ACTION_LAYER
138 dprint(" layer_state: "); layer_debug();
139 dprint(" default_layer_state: "); default_layer_debug();
143 process_action(record, action);
146 void process_action(keyrecord_t *record, action_t action)
148 keyevent_t event = record->event;
149 #ifndef NO_ACTION_TAPPING
150 uint8_t tap_count = record->tap.count;
154 // clear the potential weak mods left by previously pressed keys
158 #ifndef NO_ACTION_ONESHOT
159 bool do_release_oneshot = false;
160 // notice we only clear the one shot layer if the pressed key is not a modifier.
161 if (is_oneshot_layer_active() && event.pressed && !IS_MOD(action.key.code)) {
162 clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
163 do_release_oneshot = !is_oneshot_layer_active();
167 switch (action.kind.id) {
172 uint8_t mods = (action.kind.id == ACT_LMODS) ? action.key.mods :
176 if (IS_MOD(action.key.code) || action.key.code == KC_NO) {
177 // e.g. LSFT(KC_LGUI): we don't want the LSFT to be weak as it would make it useless.
178 // This also makes LSFT(KC_LGUI) behave exactly the same as LGUI(KC_LSFT).
179 // Same applies for some keys like KC_MEH which are declared as MEH(KC_NO).
184 send_keyboard_report();
186 register_code(action.key.code);
188 unregister_code(action.key.code);
190 if (IS_MOD(action.key.code) || action.key.code == KC_NO) {
195 send_keyboard_report();
200 #ifndef NO_ACTION_TAPPING
204 uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods :
206 switch (action.layer_tap.code) {
207 #ifndef NO_ACTION_ONESHOT
211 if (tap_count == 0) {
212 dprint("MODS_TAP: Oneshot: 0\n");
214 } else if (tap_count == 1) {
215 dprint("MODS_TAP: Oneshot: start\n");
216 set_oneshot_mods(mods);
217 #if defined(ONESHOT_TAP_TOGGLE) && ONESHOT_TAP_TOGGLE > 1
218 } else if (tap_count == ONESHOT_TAP_TOGGLE) {
219 dprint("MODS_TAP: Toggling oneshot");
220 clear_oneshot_mods();
221 set_oneshot_locked_mods(mods);
228 if (tap_count == 0) {
229 clear_oneshot_mods();
230 unregister_mods(mods);
231 } else if (tap_count == 1) {
232 // Retain Oneshot mods
233 #if defined(ONESHOT_TAP_TOGGLE) && ONESHOT_TAP_TOGGLE > 1
234 if (mods & get_mods()) {
235 clear_oneshot_locked_mods();
236 clear_oneshot_mods();
237 unregister_mods(mods);
239 } else if (tap_count == ONESHOT_TAP_TOGGLE) {
240 // Toggle Oneshot Layer
243 clear_oneshot_mods();
244 unregister_mods(mods);
249 case MODS_TAP_TOGGLE:
251 if (tap_count <= TAPPING_TOGGLE) {
255 if (tap_count < TAPPING_TOGGLE) {
256 unregister_mods(mods);
263 #ifndef IGNORE_MOD_TAP_INTERRUPT
264 if (record->tap.interrupted) {
265 dprint("mods_tap: tap: cancel: add_mods\n");
266 // ad hoc: set 0 to cancel tap
267 record->tap.count = 0;
272 dprint("MODS_TAP: Tap: register_code\n");
273 register_code(action.key.code);
276 dprint("MODS_TAP: No tap: add_mods\n");
281 dprint("MODS_TAP: Tap: unregister_code\n");
282 unregister_code(action.key.code);
284 dprint("MODS_TAP: No tap: add_mods\n");
285 unregister_mods(mods);
293 #ifdef EXTRAKEY_ENABLE
294 /* other HID usage */
296 switch (action.usage.page) {
299 host_system_send(action.usage.code);
306 host_consumer_send(action.usage.code);
308 host_consumer_send(0);
314 #ifdef MOUSEKEY_ENABLE
318 switch (action.key.code) {
320 tp_buttons |= (1<<0);
323 tp_buttons |= (1<<1);
326 tp_buttons |= (1<<2);
331 mousekey_on(action.key.code);
334 switch (action.key.code) {
336 tp_buttons &= ~(1<<0);
339 tp_buttons &= ~(1<<1);
342 tp_buttons &= ~(1<<2);
347 mousekey_off(action.key.code);
352 #ifndef NO_ACTION_LAYER
354 if (action.layer_bitop.on == 0) {
355 /* Default Layer Bitwise Operation */
356 if (!event.pressed) {
357 uint8_t shift = action.layer_bitop.part*4;
358 uint32_t bits = ((uint32_t)action.layer_bitop.bits)<<shift;
359 uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
360 switch (action.layer_bitop.op) {
361 case OP_BIT_AND: default_layer_and(bits | mask); break;
362 case OP_BIT_OR: default_layer_or(bits | mask); break;
363 case OP_BIT_XOR: default_layer_xor(bits | mask); break;
364 case OP_BIT_SET: default_layer_and(mask); default_layer_or(bits); break;
368 /* Layer Bitwise Operation */
369 if (event.pressed ? (action.layer_bitop.on & ON_PRESS) :
370 (action.layer_bitop.on & ON_RELEASE)) {
371 uint8_t shift = action.layer_bitop.part*4;
372 uint32_t bits = ((uint32_t)action.layer_bitop.bits)<<shift;
373 uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
374 switch (action.layer_bitop.op) {
375 case OP_BIT_AND: layer_and(bits | mask); break;
376 case OP_BIT_OR: layer_or(bits | mask); break;
377 case OP_BIT_XOR: layer_xor(bits | mask); break;
378 case OP_BIT_SET: layer_and(mask); layer_or(bits); break;
383 #ifndef NO_ACTION_TAPPING
385 case ACT_LAYER_TAP_EXT:
386 switch (action.layer_tap.code) {
388 /* layer On/Off with modifiers(left only) */
390 layer_on(action.layer_tap.val);
391 register_mods(action.layer_tap.code & 0x0f);
393 layer_off(action.layer_tap.val);
394 unregister_mods(action.layer_tap.code & 0x0f);
400 if (tap_count < TAPPING_TOGGLE) {
401 layer_invert(action.layer_tap.val);
404 if (tap_count <= TAPPING_TOGGLE) {
405 layer_invert(action.layer_tap.val);
410 event.pressed ? layer_on(action.layer_tap.val) :
411 layer_off(action.layer_tap.val);
414 event.pressed ? layer_off(action.layer_tap.val) :
415 layer_on(action.layer_tap.val);
418 event.pressed ? layer_move(action.layer_tap.val) :
421 #ifndef NO_ACTION_ONESHOT
424 #if defined(ONESHOT_TAP_TOGGLE) && ONESHOT_TAP_TOGGLE > 1
425 do_release_oneshot = false;
427 del_mods(get_oneshot_locked_mods());
428 if (get_oneshot_layer_state() == ONESHOT_TOGGLED) {
429 reset_oneshot_layer();
430 layer_off(action.layer_tap.val);
432 } else if (tap_count < ONESHOT_TAP_TOGGLE) {
433 layer_on(action.layer_tap.val);
434 set_oneshot_layer(action.layer_tap.val, ONESHOT_START);
437 add_mods(get_oneshot_locked_mods());
438 if (tap_count >= ONESHOT_TAP_TOGGLE) {
439 reset_oneshot_layer();
440 clear_oneshot_locked_mods();
441 set_oneshot_layer(action.layer_tap.val, ONESHOT_TOGGLED);
443 clear_oneshot_layer_state(ONESHOT_PRESSED);
448 layer_on(action.layer_tap.val);
449 set_oneshot_layer(action.layer_tap.val, ONESHOT_START);
451 clear_oneshot_layer_state(ONESHOT_PRESSED);
453 clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
463 dprint("KEYMAP_TAP_KEY: Tap: register_code\n");
464 register_code(action.layer_tap.code);
466 dprint("KEYMAP_TAP_KEY: No tap: On on press\n");
467 layer_on(action.layer_tap.val);
471 dprint("KEYMAP_TAP_KEY: Tap: unregister_code\n");
472 if (action.layer_tap.code == KC_CAPS) {
475 unregister_code(action.layer_tap.code);
477 dprint("KEYMAP_TAP_KEY: No tap: Off on release\n");
478 layer_off(action.layer_tap.val);
487 #ifndef NO_ACTION_MACRO
489 action_macro_play(action_get_macro(record, action.func.id, action.func.opt));
492 #ifdef BACKLIGHT_ENABLE
494 if (!event.pressed) {
495 switch (action.backlight.opt) {
496 case BACKLIGHT_INCREASE:
497 backlight_increase();
499 case BACKLIGHT_DECREASE:
500 backlight_decrease();
502 case BACKLIGHT_TOGGLE:
508 case BACKLIGHT_LEVEL:
509 backlight_level(action.backlight.level);
517 #ifdef ONEHAND_ENABLE
519 switch (action.swap.code) {
522 swap_hands = !swap_hands;
526 swap_hands = event.pressed;
529 swap_hands = !event.pressed;
532 if (!event.pressed) {
537 if (!event.pressed) {
541 #ifndef NO_ACTION_TAPPING
542 case OP_SH_TAP_TOGGLE:
545 if (!event.pressed) {
546 swap_hands = !swap_hands;
549 swap_hands = event.pressed;
555 register_code(action.swap.code);
557 unregister_code(action.swap.code);
560 swap_hands = event.pressed;
565 #ifndef NO_ACTION_FUNCTION
567 action_function(record, action.func.id, action.func.opt);
574 #ifndef NO_ACTION_LAYER
575 // if this event is a layer action, update the leds
576 switch (action.kind.id) {
578 #ifndef NO_ACTION_TAPPING
580 case ACT_LAYER_TAP_EXT:
582 led_set(host_keyboard_leds());
589 #ifndef NO_ACTION_ONESHOT
590 /* Because we switch layers after a oneshot event, we need to release the
591 * key before we leave the layer or no key up event will be generated.
593 if (do_release_oneshot && !(get_oneshot_layer_state() & ONESHOT_PRESSED ) ) {
594 record->event.pressed = false;
595 layer_on(get_oneshot_layer());
596 process_record(record);
597 layer_off(get_oneshot_layer());
606 * Utilities for actions.
608 void register_code(uint8_t code)
614 #ifdef LOCKING_SUPPORT_ENABLE
615 else if (KC_LOCKING_CAPS == code) {
616 #ifdef LOCKING_RESYNC_ENABLE
617 // Resync: ignore if caps lock already is on
618 if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) return;
620 add_key(KC_CAPSLOCK);
621 send_keyboard_report();
622 del_key(KC_CAPSLOCK);
623 send_keyboard_report();
626 else if (KC_LOCKING_NUM == code) {
627 #ifdef LOCKING_RESYNC_ENABLE
628 if (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK)) return;
631 send_keyboard_report();
633 send_keyboard_report();
636 else if (KC_LOCKING_SCROLL == code) {
637 #ifdef LOCKING_RESYNC_ENABLE
638 if (host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK)) return;
640 add_key(KC_SCROLLLOCK);
641 send_keyboard_report();
642 del_key(KC_SCROLLLOCK);
643 send_keyboard_report();
647 else if IS_KEY(code) {
648 // TODO: should push command_proc out of this block?
649 if (command_proc(code)) return;
651 #ifndef NO_ACTION_ONESHOT
653 if (oneshot_state.mods && !oneshot_state.disabled) {
654 uint8_t tmp_mods = get_mods();
655 add_mods(oneshot_state.mods);
658 send_keyboard_report();
661 send_keyboard_report();
668 send_keyboard_report();
671 else if IS_MOD(code) {
672 add_mods(MOD_BIT(code));
673 send_keyboard_report();
675 else if IS_SYSTEM(code) {
676 host_system_send(KEYCODE2SYSTEM(code));
678 else if IS_CONSUMER(code) {
679 host_consumer_send(KEYCODE2CONSUMER(code));
683 void unregister_code(uint8_t code)
689 #ifdef LOCKING_SUPPORT_ENABLE
690 else if (KC_LOCKING_CAPS == code) {
691 #ifdef LOCKING_RESYNC_ENABLE
692 // Resync: ignore if caps lock already is off
693 if (!(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK))) return;
695 add_key(KC_CAPSLOCK);
696 send_keyboard_report();
697 del_key(KC_CAPSLOCK);
698 send_keyboard_report();
701 else if (KC_LOCKING_NUM == code) {
702 #ifdef LOCKING_RESYNC_ENABLE
703 if (!(host_keyboard_leds() & (1<<USB_LED_NUM_LOCK))) return;
706 send_keyboard_report();
708 send_keyboard_report();
711 else if (KC_LOCKING_SCROLL == code) {
712 #ifdef LOCKING_RESYNC_ENABLE
713 if (!(host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK))) return;
715 add_key(KC_SCROLLLOCK);
716 send_keyboard_report();
717 del_key(KC_SCROLLLOCK);
718 send_keyboard_report();
722 else if IS_KEY(code) {
724 send_keyboard_report();
726 else if IS_MOD(code) {
727 del_mods(MOD_BIT(code));
728 send_keyboard_report();
730 else if IS_SYSTEM(code) {
733 else if IS_CONSUMER(code) {
734 host_consumer_send(0);
738 void register_mods(uint8_t mods)
742 send_keyboard_report();
746 void unregister_mods(uint8_t mods)
750 send_keyboard_report();
754 void clear_keyboard(void)
757 clear_keyboard_but_mods();
760 void clear_keyboard_but_mods(void)
765 send_keyboard_report();
766 #ifdef MOUSEKEY_ENABLE
770 #ifdef EXTRAKEY_ENABLE
772 host_consumer_send(0);
776 bool is_tap_key(keypos_t key)
778 action_t action = layer_switch_get_action(key);
780 switch (action.kind.id) {
784 case ACT_LAYER_TAP_EXT:
785 switch (action.layer_tap.code) {
793 switch (action.swap.code) {
795 case OP_SH_TAP_TOGGLE:
801 if (action.func.opt & FUNC_TAP) { return true; }
811 void debug_event(keyevent_t event)
813 dprintf("%04X%c(%u)", (event.key.row<<8 | event.key.col), (event.pressed ? 'd' : 'u'), event.time);
816 void debug_record(keyrecord_t record)
818 debug_event(record.event);
819 #ifndef NO_ACTION_TAPPING
820 dprintf(":%u%c", record.tap.count, (record.tap.interrupted ? '-' : ' '));
824 void debug_action(action_t action)
826 switch (action.kind.id) {
827 case ACT_LMODS: dprint("ACT_LMODS"); break;
828 case ACT_RMODS: dprint("ACT_RMODS"); break;
829 case ACT_LMODS_TAP: dprint("ACT_LMODS_TAP"); break;
830 case ACT_RMODS_TAP: dprint("ACT_RMODS_TAP"); break;
831 case ACT_USAGE: dprint("ACT_USAGE"); break;
832 case ACT_MOUSEKEY: dprint("ACT_MOUSEKEY"); break;
833 case ACT_LAYER: dprint("ACT_LAYER"); break;
834 case ACT_LAYER_TAP: dprint("ACT_LAYER_TAP"); break;
835 case ACT_LAYER_TAP_EXT: dprint("ACT_LAYER_TAP_EXT"); break;
836 case ACT_MACRO: dprint("ACT_MACRO"); break;
837 case ACT_COMMAND: dprint("ACT_COMMAND"); break;
838 case ACT_FUNCTION: dprint("ACT_FUNCTION"); break;
839 case ACT_SWAP_HANDS: dprint("ACT_SWAP_HANDS"); break;
840 default: dprint("UNKNOWN"); break;
842 dprintf("[%X:%02X]", action.kind.param>>8, action.kind.param&0xff);