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);
327 mousekey_on(action.key.code);
331 switch (action.key.code) {
333 tp_buttons &= ~(1<<0);
336 tp_buttons &= ~(1<<1);
339 tp_buttons &= ~(1<<2);
342 mousekey_off(action.key.code);
348 #ifndef NO_ACTION_LAYER
350 if (action.layer_bitop.on == 0) {
351 /* Default Layer Bitwise Operation */
352 if (!event.pressed) {
353 uint8_t shift = action.layer_bitop.part*4;
354 uint32_t bits = ((uint32_t)action.layer_bitop.bits)<<shift;
355 uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
356 switch (action.layer_bitop.op) {
357 case OP_BIT_AND: default_layer_and(bits | mask); break;
358 case OP_BIT_OR: default_layer_or(bits | mask); break;
359 case OP_BIT_XOR: default_layer_xor(bits | mask); break;
360 case OP_BIT_SET: default_layer_and(mask); default_layer_or(bits); break;
364 /* Layer Bitwise Operation */
365 if (event.pressed ? (action.layer_bitop.on & ON_PRESS) :
366 (action.layer_bitop.on & ON_RELEASE)) {
367 uint8_t shift = action.layer_bitop.part*4;
368 uint32_t bits = ((uint32_t)action.layer_bitop.bits)<<shift;
369 uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
370 switch (action.layer_bitop.op) {
371 case OP_BIT_AND: layer_and(bits | mask); break;
372 case OP_BIT_OR: layer_or(bits | mask); break;
373 case OP_BIT_XOR: layer_xor(bits | mask); break;
374 case OP_BIT_SET: layer_and(mask); layer_or(bits); break;
379 #ifndef NO_ACTION_TAPPING
381 case ACT_LAYER_TAP_EXT:
382 switch (action.layer_tap.code) {
384 /* layer On/Off with modifiers(left only) */
386 layer_on(action.layer_tap.val);
387 register_mods(action.layer_tap.code & 0x0f);
389 layer_off(action.layer_tap.val);
390 unregister_mods(action.layer_tap.code & 0x0f);
396 if (tap_count < TAPPING_TOGGLE) {
397 layer_invert(action.layer_tap.val);
400 if (tap_count <= TAPPING_TOGGLE) {
401 layer_invert(action.layer_tap.val);
406 event.pressed ? layer_on(action.layer_tap.val) :
407 layer_off(action.layer_tap.val);
410 event.pressed ? layer_off(action.layer_tap.val) :
411 layer_on(action.layer_tap.val);
414 event.pressed ? layer_move(action.layer_tap.val) :
417 #ifndef NO_ACTION_ONESHOT
420 #if defined(ONESHOT_TAP_TOGGLE) && ONESHOT_TAP_TOGGLE > 1
421 do_release_oneshot = false;
423 del_mods(get_oneshot_locked_mods());
424 if (get_oneshot_layer_state() == ONESHOT_TOGGLED) {
425 reset_oneshot_layer();
426 layer_off(action.layer_tap.val);
428 } else if (tap_count < ONESHOT_TAP_TOGGLE) {
429 layer_on(action.layer_tap.val);
430 set_oneshot_layer(action.layer_tap.val, ONESHOT_START);
433 add_mods(get_oneshot_locked_mods());
434 if (tap_count >= ONESHOT_TAP_TOGGLE) {
435 reset_oneshot_layer();
436 clear_oneshot_locked_mods();
437 set_oneshot_layer(action.layer_tap.val, ONESHOT_TOGGLED);
439 clear_oneshot_layer_state(ONESHOT_PRESSED);
444 layer_on(action.layer_tap.val);
445 set_oneshot_layer(action.layer_tap.val, ONESHOT_START);
447 clear_oneshot_layer_state(ONESHOT_PRESSED);
449 clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
459 dprint("KEYMAP_TAP_KEY: Tap: register_code\n");
460 register_code(action.layer_tap.code);
462 dprint("KEYMAP_TAP_KEY: No tap: On on press\n");
463 layer_on(action.layer_tap.val);
467 dprint("KEYMAP_TAP_KEY: Tap: unregister_code\n");
468 if (action.layer_tap.code == KC_CAPS) {
471 unregister_code(action.layer_tap.code);
473 dprint("KEYMAP_TAP_KEY: No tap: Off on release\n");
474 layer_off(action.layer_tap.val);
483 #ifndef NO_ACTION_MACRO
485 action_macro_play(action_get_macro(record, action.func.id, action.func.opt));
488 #ifdef BACKLIGHT_ENABLE
490 if (!event.pressed) {
491 switch (action.backlight.opt) {
492 case BACKLIGHT_INCREASE:
493 backlight_increase();
495 case BACKLIGHT_DECREASE:
496 backlight_decrease();
498 case BACKLIGHT_TOGGLE:
504 case BACKLIGHT_LEVEL:
505 backlight_level(action.backlight.level);
513 #ifdef ONEHAND_ENABLE
515 switch (action.swap.code) {
518 swap_hands = !swap_hands;
522 swap_hands = event.pressed;
525 swap_hands = !event.pressed;
528 if (!event.pressed) {
533 if (!event.pressed) {
537 #ifndef NO_ACTION_TAPPING
538 case OP_SH_TAP_TOGGLE:
541 if (!event.pressed) {
542 swap_hands = !swap_hands;
545 swap_hands = event.pressed;
551 register_code(action.swap.code);
553 unregister_code(action.swap.code);
556 swap_hands = event.pressed;
561 #ifndef NO_ACTION_FUNCTION
563 action_function(record, action.func.id, action.func.opt);
570 #ifndef NO_ACTION_LAYER
571 // if this event is a layer action, update the leds
572 switch (action.kind.id) {
574 #ifndef NO_ACTION_TAPPING
576 case ACT_LAYER_TAP_EXT:
578 led_set(host_keyboard_leds());
585 #ifndef NO_ACTION_ONESHOT
586 /* Because we switch layers after a oneshot event, we need to release the
587 * key before we leave the layer or no key up event will be generated.
589 if (do_release_oneshot && !(get_oneshot_layer_state() & ONESHOT_PRESSED ) ) {
590 record->event.pressed = false;
591 layer_on(get_oneshot_layer());
592 process_record(record);
593 layer_off(get_oneshot_layer());
602 * Utilities for actions.
604 void register_code(uint8_t code)
610 #ifdef LOCKING_SUPPORT_ENABLE
611 else if (KC_LOCKING_CAPS == code) {
612 #ifdef LOCKING_RESYNC_ENABLE
613 // Resync: ignore if caps lock already is on
614 if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) return;
616 add_key(KC_CAPSLOCK);
617 send_keyboard_report();
618 del_key(KC_CAPSLOCK);
619 send_keyboard_report();
622 else if (KC_LOCKING_NUM == code) {
623 #ifdef LOCKING_RESYNC_ENABLE
624 if (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK)) return;
627 send_keyboard_report();
629 send_keyboard_report();
632 else if (KC_LOCKING_SCROLL == code) {
633 #ifdef LOCKING_RESYNC_ENABLE
634 if (host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK)) return;
636 add_key(KC_SCROLLLOCK);
637 send_keyboard_report();
638 del_key(KC_SCROLLLOCK);
639 send_keyboard_report();
643 else if IS_KEY(code) {
644 // TODO: should push command_proc out of this block?
645 if (command_proc(code)) return;
647 #ifndef NO_ACTION_ONESHOT
649 if (oneshot_state.mods && !oneshot_state.disabled) {
650 uint8_t tmp_mods = get_mods();
651 add_mods(oneshot_state.mods);
654 send_keyboard_report();
657 send_keyboard_report();
664 send_keyboard_report();
667 else if IS_MOD(code) {
668 add_mods(MOD_BIT(code));
669 send_keyboard_report();
671 else if IS_SYSTEM(code) {
672 host_system_send(KEYCODE2SYSTEM(code));
674 else if IS_CONSUMER(code) {
675 host_consumer_send(KEYCODE2CONSUMER(code));
679 void unregister_code(uint8_t code)
685 #ifdef LOCKING_SUPPORT_ENABLE
686 else if (KC_LOCKING_CAPS == code) {
687 #ifdef LOCKING_RESYNC_ENABLE
688 // Resync: ignore if caps lock already is off
689 if (!(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK))) return;
691 add_key(KC_CAPSLOCK);
692 send_keyboard_report();
693 del_key(KC_CAPSLOCK);
694 send_keyboard_report();
697 else if (KC_LOCKING_NUM == code) {
698 #ifdef LOCKING_RESYNC_ENABLE
699 if (!(host_keyboard_leds() & (1<<USB_LED_NUM_LOCK))) return;
702 send_keyboard_report();
704 send_keyboard_report();
707 else if (KC_LOCKING_SCROLL == code) {
708 #ifdef LOCKING_RESYNC_ENABLE
709 if (!(host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK))) return;
711 add_key(KC_SCROLLLOCK);
712 send_keyboard_report();
713 del_key(KC_SCROLLLOCK);
714 send_keyboard_report();
718 else if IS_KEY(code) {
720 send_keyboard_report();
722 else if IS_MOD(code) {
723 del_mods(MOD_BIT(code));
724 send_keyboard_report();
726 else if IS_SYSTEM(code) {
729 else if IS_CONSUMER(code) {
730 host_consumer_send(0);
734 void register_mods(uint8_t mods)
738 send_keyboard_report();
742 void unregister_mods(uint8_t mods)
746 send_keyboard_report();
750 void clear_keyboard(void)
753 clear_keyboard_but_mods();
756 void clear_keyboard_but_mods(void)
761 send_keyboard_report();
762 #ifdef MOUSEKEY_ENABLE
766 #ifdef EXTRAKEY_ENABLE
768 host_consumer_send(0);
772 bool is_tap_key(keypos_t key)
774 action_t action = layer_switch_get_action(key);
776 switch (action.kind.id) {
780 case ACT_LAYER_TAP_EXT:
781 switch (action.layer_tap.code) {
789 switch (action.swap.code) {
791 case OP_SH_TAP_TOGGLE:
797 if (action.func.opt & FUNC_TAP) { return true; }
807 void debug_event(keyevent_t event)
809 dprintf("%04X%c(%u)", (event.key.row<<8 | event.key.col), (event.pressed ? 'd' : 'u'), event.time);
812 void debug_record(keyrecord_t record)
814 debug_event(record.event);
815 #ifndef NO_ACTION_TAPPING
816 dprintf(":%u%c", record.tap.count, (record.tap.interrupted ? '-' : ' '));
820 void debug_action(action_t action)
822 switch (action.kind.id) {
823 case ACT_LMODS: dprint("ACT_LMODS"); break;
824 case ACT_RMODS: dprint("ACT_RMODS"); break;
825 case ACT_LMODS_TAP: dprint("ACT_LMODS_TAP"); break;
826 case ACT_RMODS_TAP: dprint("ACT_RMODS_TAP"); break;
827 case ACT_USAGE: dprint("ACT_USAGE"); break;
828 case ACT_MOUSEKEY: dprint("ACT_MOUSEKEY"); break;
829 case ACT_LAYER: dprint("ACT_LAYER"); break;
830 case ACT_LAYER_TAP: dprint("ACT_LAYER_TAP"); break;
831 case ACT_LAYER_TAP_EXT: dprint("ACT_LAYER_TAP_EXT"); break;
832 case ACT_MACRO: dprint("ACT_MACRO"); break;
833 case ACT_COMMAND: dprint("ACT_COMMAND"); break;
834 case ACT_FUNCTION: dprint("ACT_FUNCTION"); break;
835 case ACT_SWAP_HANDS: dprint("ACT_SWAP_HANDS"); break;
836 default: dprint("UNKNOWN"); break;
838 dprintf("[%X:%02X]", action.kind.param>>8, action.kind.param&0xff);