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);
73 if (has_oneshot_mods_timed_out()) {
78 #ifndef NO_ACTION_TAPPING
79 action_tapping_process(record);
81 process_record(&record);
82 if (!IS_NOEVENT(record.event)) {
83 dprint("processed: "); debug_record(record); dprintln();
89 bool swap_hands = false;
91 void process_hand_swap(keyevent_t *event) {
92 static swap_state_row_t swap_state[MATRIX_ROWS];
94 keypos_t pos = event->key;
95 swap_state_row_t col_bit = (swap_state_row_t)1<<pos.col;
96 bool do_swap = event->pressed ? swap_hands :
97 swap_state[pos.row] & (col_bit);
100 event->key = hand_swap_config[pos.row][pos.col];
101 swap_state[pos.row] |= col_bit;
103 swap_state[pos.row] &= ~(col_bit);
108 #if !defined(NO_ACTION_LAYER) && defined(PREVENT_STUCK_MODIFIERS)
109 bool disable_action_cache = false;
111 void process_record_nocache(keyrecord_t *record)
113 disable_action_cache = true;
114 process_record(record);
115 disable_action_cache = false;
118 void process_record_nocache(keyrecord_t *record)
120 process_record(record);
124 __attribute__ ((weak))
125 bool process_record_quantum(keyrecord_t *record) {
129 void process_record(keyrecord_t *record)
131 if (IS_NOEVENT(record->event)) { return; }
133 if(!process_record_quantum(record))
136 action_t action = store_or_get_action(record->event.pressed, record->event.key);
137 dprint("ACTION: "); debug_action(action);
138 #ifndef NO_ACTION_LAYER
139 dprint(" layer_state: "); layer_debug();
140 dprint(" default_layer_state: "); default_layer_debug();
144 process_action(record, action);
147 void process_action(keyrecord_t *record, action_t action)
149 keyevent_t event = record->event;
150 #ifndef NO_ACTION_TAPPING
151 uint8_t tap_count = record->tap.count;
155 // clear the potential weak mods left by previously pressed keys
159 #ifndef NO_ACTION_ONESHOT
160 bool do_release_oneshot = false;
161 // notice we only clear the one shot layer if the pressed key is not a modifier.
162 if (is_oneshot_layer_active() && event.pressed && !IS_MOD(action.key.code)) {
163 clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
164 do_release_oneshot = !is_oneshot_layer_active();
168 switch (action.kind.id) {
173 uint8_t mods = (action.kind.id == ACT_LMODS) ? action.key.mods :
177 if (IS_MOD(action.key.code) || action.key.code == KC_NO) {
178 // e.g. LSFT(KC_LGUI): we don't want the LSFT to be weak as it would make it useless.
179 // This also makes LSFT(KC_LGUI) behave exactly the same as LGUI(KC_LSFT).
180 // Same applies for some keys like KC_MEH which are declared as MEH(KC_NO).
185 send_keyboard_report();
187 register_code(action.key.code);
189 unregister_code(action.key.code);
191 if (IS_MOD(action.key.code) || action.key.code == KC_NO) {
196 send_keyboard_report();
201 #ifndef NO_ACTION_TAPPING
205 uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods :
207 switch (action.layer_tap.code) {
208 #ifndef NO_ACTION_ONESHOT
212 if (tap_count == 0) {
213 dprint("MODS_TAP: Oneshot: 0\n");
215 } else if (tap_count == 1) {
216 dprint("MODS_TAP: Oneshot: start\n");
217 set_oneshot_mods(mods);
218 #if defined(ONESHOT_TAP_TOGGLE) && ONESHOT_TAP_TOGGLE > 1
219 } else if (tap_count == ONESHOT_TAP_TOGGLE) {
220 dprint("MODS_TAP: Toggling oneshot");
221 clear_oneshot_mods();
222 set_oneshot_locked_mods(mods);
229 if (tap_count == 0) {
230 clear_oneshot_mods();
231 unregister_mods(mods);
232 } else if (tap_count == 1) {
233 // Retain Oneshot mods
234 #if defined(ONESHOT_TAP_TOGGLE) && ONESHOT_TAP_TOGGLE > 1
235 if (mods & get_mods()) {
236 clear_oneshot_locked_mods();
237 clear_oneshot_mods();
238 unregister_mods(mods);
240 } else if (tap_count == ONESHOT_TAP_TOGGLE) {
241 // Toggle Oneshot Layer
244 clear_oneshot_mods();
245 unregister_mods(mods);
250 case MODS_TAP_TOGGLE:
252 if (tap_count <= TAPPING_TOGGLE) {
256 if (tap_count < TAPPING_TOGGLE) {
257 unregister_mods(mods);
264 #ifndef IGNORE_MOD_TAP_INTERRUPT
265 if (record->tap.interrupted) {
266 dprint("mods_tap: tap: cancel: add_mods\n");
267 // ad hoc: set 0 to cancel tap
268 record->tap.count = 0;
273 dprint("MODS_TAP: Tap: register_code\n");
274 register_code(action.key.code);
277 dprint("MODS_TAP: No tap: add_mods\n");
282 dprint("MODS_TAP: Tap: unregister_code\n");
283 unregister_code(action.key.code);
285 dprint("MODS_TAP: No tap: add_mods\n");
286 unregister_mods(mods);
294 #ifdef EXTRAKEY_ENABLE
295 /* other HID usage */
297 switch (action.usage.page) {
300 host_system_send(action.usage.code);
307 host_consumer_send(action.usage.code);
309 host_consumer_send(0);
315 #ifdef MOUSEKEY_ENABLE
319 switch (action.key.code) {
321 tp_buttons |= (1<<0);
324 tp_buttons |= (1<<1);
327 tp_buttons |= (1<<2);
332 mousekey_on(action.key.code);
335 switch (action.key.code) {
337 tp_buttons &= ~(1<<0);
340 tp_buttons &= ~(1<<1);
343 tp_buttons &= ~(1<<2);
348 mousekey_off(action.key.code);
353 #ifndef NO_ACTION_LAYER
355 if (action.layer_bitop.on == 0) {
356 /* Default Layer Bitwise Operation */
357 if (!event.pressed) {
358 uint8_t shift = action.layer_bitop.part*4;
359 uint32_t bits = ((uint32_t)action.layer_bitop.bits)<<shift;
360 uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
361 switch (action.layer_bitop.op) {
362 case OP_BIT_AND: default_layer_and(bits | mask); break;
363 case OP_BIT_OR: default_layer_or(bits | mask); break;
364 case OP_BIT_XOR: default_layer_xor(bits | mask); break;
365 case OP_BIT_SET: default_layer_and(mask); default_layer_or(bits); break;
369 /* Layer Bitwise Operation */
370 if (event.pressed ? (action.layer_bitop.on & ON_PRESS) :
371 (action.layer_bitop.on & ON_RELEASE)) {
372 uint8_t shift = action.layer_bitop.part*4;
373 uint32_t bits = ((uint32_t)action.layer_bitop.bits)<<shift;
374 uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
375 switch (action.layer_bitop.op) {
376 case OP_BIT_AND: layer_and(bits | mask); break;
377 case OP_BIT_OR: layer_or(bits | mask); break;
378 case OP_BIT_XOR: layer_xor(bits | mask); break;
379 case OP_BIT_SET: layer_and(mask); layer_or(bits); break;
384 #ifndef NO_ACTION_TAPPING
386 case ACT_LAYER_TAP_EXT:
387 switch (action.layer_tap.code) {
389 /* layer On/Off with modifiers(left only) */
391 layer_on(action.layer_tap.val);
392 register_mods(action.layer_tap.code & 0x0f);
394 layer_off(action.layer_tap.val);
395 unregister_mods(action.layer_tap.code & 0x0f);
401 if (tap_count < TAPPING_TOGGLE) {
402 layer_invert(action.layer_tap.val);
405 if (tap_count <= TAPPING_TOGGLE) {
406 layer_invert(action.layer_tap.val);
411 event.pressed ? layer_on(action.layer_tap.val) :
412 layer_off(action.layer_tap.val);
415 event.pressed ? layer_off(action.layer_tap.val) :
416 layer_on(action.layer_tap.val);
419 event.pressed ? layer_move(action.layer_tap.val) :
422 #ifndef NO_ACTION_ONESHOT
425 #if defined(ONESHOT_TAP_TOGGLE) && ONESHOT_TAP_TOGGLE > 1
426 do_release_oneshot = false;
428 del_mods(get_oneshot_locked_mods());
429 if (get_oneshot_layer_state() == ONESHOT_TOGGLED) {
430 reset_oneshot_layer();
431 layer_off(action.layer_tap.val);
433 } else if (tap_count < ONESHOT_TAP_TOGGLE) {
434 layer_on(action.layer_tap.val);
435 set_oneshot_layer(action.layer_tap.val, ONESHOT_START);
438 add_mods(get_oneshot_locked_mods());
439 if (tap_count >= ONESHOT_TAP_TOGGLE) {
440 reset_oneshot_layer();
441 clear_oneshot_locked_mods();
442 set_oneshot_layer(action.layer_tap.val, ONESHOT_TOGGLED);
444 clear_oneshot_layer_state(ONESHOT_PRESSED);
449 layer_on(action.layer_tap.val);
450 set_oneshot_layer(action.layer_tap.val, ONESHOT_START);
452 clear_oneshot_layer_state(ONESHOT_PRESSED);
454 clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
464 dprint("KEYMAP_TAP_KEY: Tap: register_code\n");
465 register_code(action.layer_tap.code);
467 dprint("KEYMAP_TAP_KEY: No tap: On on press\n");
468 layer_on(action.layer_tap.val);
472 dprint("KEYMAP_TAP_KEY: Tap: unregister_code\n");
473 if (action.layer_tap.code == KC_CAPS) {
476 unregister_code(action.layer_tap.code);
478 dprint("KEYMAP_TAP_KEY: No tap: Off on release\n");
479 layer_off(action.layer_tap.val);
488 #ifndef NO_ACTION_MACRO
490 action_macro_play(action_get_macro(record, action.func.id, action.func.opt));
493 #ifdef BACKLIGHT_ENABLE
495 if (!event.pressed) {
496 switch (action.backlight.opt) {
497 case BACKLIGHT_INCREASE:
498 backlight_increase();
500 case BACKLIGHT_DECREASE:
501 backlight_decrease();
503 case BACKLIGHT_TOGGLE:
509 case BACKLIGHT_LEVEL:
510 backlight_level(action.backlight.level);
518 #ifdef ONEHAND_ENABLE
520 switch (action.swap.code) {
523 swap_hands = !swap_hands;
527 swap_hands = event.pressed;
530 swap_hands = !event.pressed;
533 if (!event.pressed) {
538 if (!event.pressed) {
542 #ifndef NO_ACTION_TAPPING
543 case OP_SH_TAP_TOGGLE:
546 if (!event.pressed) {
547 swap_hands = !swap_hands;
550 swap_hands = event.pressed;
556 register_code(action.swap.code);
558 unregister_code(action.swap.code);
561 swap_hands = event.pressed;
566 #ifndef NO_ACTION_FUNCTION
568 action_function(record, action.func.id, action.func.opt);
575 #ifndef NO_ACTION_LAYER
576 // if this event is a layer action, update the leds
577 switch (action.kind.id) {
579 #ifndef NO_ACTION_TAPPING
581 case ACT_LAYER_TAP_EXT:
583 led_set(host_keyboard_leds());
590 #ifndef NO_ACTION_ONESHOT
591 /* Because we switch layers after a oneshot event, we need to release the
592 * key before we leave the layer or no key up event will be generated.
594 if (do_release_oneshot && !(get_oneshot_layer_state() & ONESHOT_PRESSED ) ) {
595 record->event.pressed = false;
596 layer_on(get_oneshot_layer());
597 process_record(record);
598 layer_off(get_oneshot_layer());
607 * Utilities for actions.
609 void register_code(uint8_t code)
615 #ifdef LOCKING_SUPPORT_ENABLE
616 else if (KC_LOCKING_CAPS == code) {
617 #ifdef LOCKING_RESYNC_ENABLE
618 // Resync: ignore if caps lock already is on
619 if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) return;
621 add_key(KC_CAPSLOCK);
622 send_keyboard_report();
623 del_key(KC_CAPSLOCK);
624 send_keyboard_report();
627 else if (KC_LOCKING_NUM == code) {
628 #ifdef LOCKING_RESYNC_ENABLE
629 if (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK)) return;
632 send_keyboard_report();
634 send_keyboard_report();
637 else if (KC_LOCKING_SCROLL == code) {
638 #ifdef LOCKING_RESYNC_ENABLE
639 if (host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK)) return;
641 add_key(KC_SCROLLLOCK);
642 send_keyboard_report();
643 del_key(KC_SCROLLLOCK);
644 send_keyboard_report();
648 else if IS_KEY(code) {
649 // TODO: should push command_proc out of this block?
650 if (command_proc(code)) return;
652 #ifndef NO_ACTION_ONESHOT
654 if (oneshot_state.mods && !oneshot_state.disabled) {
655 uint8_t tmp_mods = get_mods();
656 add_mods(oneshot_state.mods);
659 send_keyboard_report();
662 send_keyboard_report();
669 send_keyboard_report();
672 else if IS_MOD(code) {
673 add_mods(MOD_BIT(code));
674 send_keyboard_report();
676 else if IS_SYSTEM(code) {
677 host_system_send(KEYCODE2SYSTEM(code));
679 else if IS_CONSUMER(code) {
680 host_consumer_send(KEYCODE2CONSUMER(code));
684 void unregister_code(uint8_t code)
690 #ifdef LOCKING_SUPPORT_ENABLE
691 else if (KC_LOCKING_CAPS == code) {
692 #ifdef LOCKING_RESYNC_ENABLE
693 // Resync: ignore if caps lock already is off
694 if (!(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK))) return;
696 add_key(KC_CAPSLOCK);
697 send_keyboard_report();
698 del_key(KC_CAPSLOCK);
699 send_keyboard_report();
702 else if (KC_LOCKING_NUM == code) {
703 #ifdef LOCKING_RESYNC_ENABLE
704 if (!(host_keyboard_leds() & (1<<USB_LED_NUM_LOCK))) return;
707 send_keyboard_report();
709 send_keyboard_report();
712 else if (KC_LOCKING_SCROLL == code) {
713 #ifdef LOCKING_RESYNC_ENABLE
714 if (!(host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK))) return;
716 add_key(KC_SCROLLLOCK);
717 send_keyboard_report();
718 del_key(KC_SCROLLLOCK);
719 send_keyboard_report();
723 else if IS_KEY(code) {
725 send_keyboard_report();
727 else if IS_MOD(code) {
728 del_mods(MOD_BIT(code));
729 send_keyboard_report();
731 else if IS_SYSTEM(code) {
734 else if IS_CONSUMER(code) {
735 host_consumer_send(0);
739 void register_mods(uint8_t mods)
743 send_keyboard_report();
747 void unregister_mods(uint8_t mods)
751 send_keyboard_report();
755 void clear_keyboard(void)
758 clear_keyboard_but_mods();
761 void clear_keyboard_but_mods(void)
766 send_keyboard_report();
767 #ifdef MOUSEKEY_ENABLE
771 #ifdef EXTRAKEY_ENABLE
773 host_consumer_send(0);
777 bool is_tap_key(keypos_t key)
779 action_t action = layer_switch_get_action(key);
781 switch (action.kind.id) {
785 case ACT_LAYER_TAP_EXT:
786 switch (action.layer_tap.code) {
794 switch (action.swap.code) {
796 case OP_SH_TAP_TOGGLE:
802 if (action.func.opt & FUNC_TAP) { return true; }
812 void debug_event(keyevent_t event)
814 dprintf("%04X%c(%u)", (event.key.row<<8 | event.key.col), (event.pressed ? 'd' : 'u'), event.time);
817 void debug_record(keyrecord_t record)
819 debug_event(record.event);
820 #ifndef NO_ACTION_TAPPING
821 dprintf(":%u%c", record.tap.count, (record.tap.interrupted ? '-' : ' '));
825 void debug_action(action_t action)
827 switch (action.kind.id) {
828 case ACT_LMODS: dprint("ACT_LMODS"); break;
829 case ACT_RMODS: dprint("ACT_RMODS"); break;
830 case ACT_LMODS_TAP: dprint("ACT_LMODS_TAP"); break;
831 case ACT_RMODS_TAP: dprint("ACT_RMODS_TAP"); break;
832 case ACT_USAGE: dprint("ACT_USAGE"); break;
833 case ACT_MOUSEKEY: dprint("ACT_MOUSEKEY"); break;
834 case ACT_LAYER: dprint("ACT_LAYER"); break;
835 case ACT_LAYER_TAP: dprint("ACT_LAYER_TAP"); break;
836 case ACT_LAYER_TAP_EXT: dprint("ACT_LAYER_TAP_EXT"); break;
837 case ACT_MACRO: dprint("ACT_MACRO"); break;
838 case ACT_COMMAND: dprint("ACT_COMMAND"); break;
839 case ACT_FUNCTION: dprint("ACT_FUNCTION"); break;
840 case ACT_SWAP_HANDS: dprint("ACT_SWAP_HANDS"); break;
841 default: dprint("UNKNOWN"); break;
843 dprintf("[%X:%02X]", action.kind.param>>8, action.kind.param&0xff);