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"
37 #ifdef FAUXCLICKY_ENABLE
38 #include <fauxclicky.h>
41 void action_exec(keyevent_t event)
43 if (!IS_NOEVENT(event)) {
44 dprint("\n---- action_exec: start -----\n");
45 dprint("EVENT: "); debug_event(event); dprintln();
48 #ifdef FAUXCLICKY_ENABLE
49 if (IS_PRESSED(event)) {
50 FAUXCLICKY_ACTION_PRESS;
52 if (IS_RELEASED(event)) {
53 FAUXCLICKY_ACTION_RELEASE;
59 if (!IS_NOEVENT(event)) {
60 process_hand_swap(&event);
64 keyrecord_t record = { .event = event };
66 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
67 if (has_oneshot_layer_timed_out()) {
68 dprintf("Oneshot layer: timeout\n");
69 clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
73 #ifndef NO_ACTION_TAPPING
74 action_tapping_process(record);
76 process_record(&record);
77 if (!IS_NOEVENT(record.event)) {
78 dprint("processed: "); debug_record(record); dprintln();
84 bool swap_hands = false;
86 void process_hand_swap(keyevent_t *event) {
87 static swap_state_row_t swap_state[MATRIX_ROWS];
89 keypos_t pos = event->key;
90 swap_state_row_t col_bit = (swap_state_row_t)1<<pos.col;
91 bool do_swap = event->pressed ? swap_hands :
92 swap_state[pos.row] & (col_bit);
95 event->key = hand_swap_config[pos.row][pos.col];
96 swap_state[pos.row] |= col_bit;
98 swap_state[pos.row] &= ~(col_bit);
103 #if !defined(NO_ACTION_LAYER) && defined(PREVENT_STUCK_MODIFIERS)
104 bool disable_action_cache = false;
106 void process_record_nocache(keyrecord_t *record)
108 disable_action_cache = true;
109 process_record(record);
110 disable_action_cache = false;
113 void process_record_nocache(keyrecord_t *record)
115 process_record(record);
119 __attribute__ ((weak))
120 bool process_record_quantum(keyrecord_t *record) {
124 void process_record(keyrecord_t *record)
126 if (IS_NOEVENT(record->event)) { return; }
128 if(!process_record_quantum(record))
131 action_t action = store_or_get_action(record->event.pressed, record->event.key);
132 dprint("ACTION: "); debug_action(action);
133 #ifndef NO_ACTION_LAYER
134 dprint(" layer_state: "); layer_debug();
135 dprint(" default_layer_state: "); default_layer_debug();
139 process_action(record, action);
142 void process_action(keyrecord_t *record, action_t action)
144 keyevent_t event = record->event;
145 #ifndef NO_ACTION_TAPPING
146 uint8_t tap_count = record->tap.count;
150 // clear the potential weak mods left by previously pressed keys
154 #ifndef NO_ACTION_ONESHOT
155 bool do_release_oneshot = false;
156 // notice we only clear the one shot layer if the pressed key is not a modifier.
157 if (is_oneshot_layer_active() && event.pressed && !IS_MOD(action.key.code)) {
158 clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
159 do_release_oneshot = !is_oneshot_layer_active();
163 switch (action.kind.id) {
168 uint8_t mods = (action.kind.id == ACT_LMODS) ? action.key.mods :
172 if (IS_MOD(action.key.code) || action.key.code == KC_NO) {
173 // e.g. LSFT(KC_LGUI): we don't want the LSFT to be weak as it would make it useless.
174 // This also makes LSFT(KC_LGUI) behave exactly the same as LGUI(KC_LSFT).
175 // Same applies for some keys like KC_MEH which are declared as MEH(KC_NO).
180 send_keyboard_report();
182 register_code(action.key.code);
184 unregister_code(action.key.code);
186 if (IS_MOD(action.key.code) || action.key.code == KC_NO) {
191 send_keyboard_report();
196 #ifndef NO_ACTION_TAPPING
200 uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods :
202 switch (action.layer_tap.code) {
203 #ifndef NO_ACTION_ONESHOT
207 if (tap_count == 0) {
208 dprint("MODS_TAP: Oneshot: 0\n");
210 } else if (tap_count == 1) {
211 dprint("MODS_TAP: Oneshot: start\n");
212 set_oneshot_mods(mods);
213 #if defined(ONESHOT_TAP_TOGGLE) && ONESHOT_TAP_TOGGLE > 1
214 } else if (tap_count == ONESHOT_TAP_TOGGLE) {
215 dprint("MODS_TAP: Toggling oneshot");
216 clear_oneshot_mods();
217 set_oneshot_locked_mods(mods);
224 if (tap_count == 0) {
225 clear_oneshot_mods();
226 unregister_mods(mods);
227 } else if (tap_count == 1) {
228 // Retain Oneshot mods
229 #if defined(ONESHOT_TAP_TOGGLE) && ONESHOT_TAP_TOGGLE > 1
230 if (mods & get_mods()) {
231 clear_oneshot_locked_mods();
232 clear_oneshot_mods();
233 unregister_mods(mods);
235 } else if (tap_count == ONESHOT_TAP_TOGGLE) {
236 // Toggle Oneshot Layer
239 clear_oneshot_mods();
240 unregister_mods(mods);
245 case MODS_TAP_TOGGLE:
247 if (tap_count <= TAPPING_TOGGLE) {
251 if (tap_count < TAPPING_TOGGLE) {
252 unregister_mods(mods);
259 #ifndef IGNORE_MOD_TAP_INTERRUPT
260 if (record->tap.interrupted) {
261 dprint("mods_tap: tap: cancel: add_mods\n");
262 // ad hoc: set 0 to cancel tap
263 record->tap.count = 0;
268 dprint("MODS_TAP: Tap: register_code\n");
269 register_code(action.key.code);
272 dprint("MODS_TAP: No tap: add_mods\n");
277 dprint("MODS_TAP: Tap: unregister_code\n");
278 unregister_code(action.key.code);
280 dprint("MODS_TAP: No tap: add_mods\n");
281 unregister_mods(mods);
289 #ifdef EXTRAKEY_ENABLE
290 /* other HID usage */
292 switch (action.usage.page) {
295 host_system_send(action.usage.code);
302 host_consumer_send(action.usage.code);
304 host_consumer_send(0);
310 #ifdef MOUSEKEY_ENABLE
314 mousekey_on(action.key.code);
317 mousekey_off(action.key.code);
322 #ifndef NO_ACTION_LAYER
324 if (action.layer_bitop.on == 0) {
325 /* Default Layer Bitwise Operation */
326 if (!event.pressed) {
327 uint8_t shift = action.layer_bitop.part*4;
328 uint32_t bits = ((uint32_t)action.layer_bitop.bits)<<shift;
329 uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
330 switch (action.layer_bitop.op) {
331 case OP_BIT_AND: default_layer_and(bits | mask); break;
332 case OP_BIT_OR: default_layer_or(bits | mask); break;
333 case OP_BIT_XOR: default_layer_xor(bits | mask); break;
334 case OP_BIT_SET: default_layer_and(mask); default_layer_or(bits); break;
338 /* Layer Bitwise Operation */
339 if (event.pressed ? (action.layer_bitop.on & ON_PRESS) :
340 (action.layer_bitop.on & ON_RELEASE)) {
341 uint8_t shift = action.layer_bitop.part*4;
342 uint32_t bits = ((uint32_t)action.layer_bitop.bits)<<shift;
343 uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
344 switch (action.layer_bitop.op) {
345 case OP_BIT_AND: layer_and(bits | mask); break;
346 case OP_BIT_OR: layer_or(bits | mask); break;
347 case OP_BIT_XOR: layer_xor(bits | mask); break;
348 case OP_BIT_SET: layer_and(mask); layer_or(bits); break;
353 #ifndef NO_ACTION_TAPPING
355 case ACT_LAYER_TAP_EXT:
356 switch (action.layer_tap.code) {
358 /* layer On/Off with modifiers(left only) */
360 layer_on(action.layer_tap.val);
361 register_mods(action.layer_tap.code & 0x0f);
363 layer_off(action.layer_tap.val);
364 unregister_mods(action.layer_tap.code & 0x0f);
370 if (tap_count < TAPPING_TOGGLE) {
371 layer_invert(action.layer_tap.val);
374 if (tap_count <= TAPPING_TOGGLE) {
375 layer_invert(action.layer_tap.val);
380 event.pressed ? layer_on(action.layer_tap.val) :
381 layer_off(action.layer_tap.val);
384 event.pressed ? layer_off(action.layer_tap.val) :
385 layer_on(action.layer_tap.val);
388 event.pressed ? layer_move(action.layer_tap.val) :
391 #ifndef NO_ACTION_ONESHOT
394 #if defined(ONESHOT_TAP_TOGGLE) && ONESHOT_TAP_TOGGLE > 1
395 do_release_oneshot = false;
397 del_mods(get_oneshot_locked_mods());
398 if (get_oneshot_layer_state() == ONESHOT_TOGGLED) {
399 reset_oneshot_layer();
400 layer_off(action.layer_tap.val);
402 } else if (tap_count < ONESHOT_TAP_TOGGLE) {
403 layer_on(action.layer_tap.val);
404 set_oneshot_layer(action.layer_tap.val, ONESHOT_START);
407 add_mods(get_oneshot_locked_mods());
408 if (tap_count >= ONESHOT_TAP_TOGGLE) {
409 reset_oneshot_layer();
410 clear_oneshot_locked_mods();
411 set_oneshot_layer(action.layer_tap.val, ONESHOT_TOGGLED);
413 clear_oneshot_layer_state(ONESHOT_PRESSED);
418 layer_on(action.layer_tap.val);
419 set_oneshot_layer(action.layer_tap.val, ONESHOT_START);
421 clear_oneshot_layer_state(ONESHOT_PRESSED);
423 clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
433 dprint("KEYMAP_TAP_KEY: Tap: register_code\n");
434 register_code(action.layer_tap.code);
436 dprint("KEYMAP_TAP_KEY: No tap: On on press\n");
437 layer_on(action.layer_tap.val);
441 dprint("KEYMAP_TAP_KEY: Tap: unregister_code\n");
442 if (action.layer_tap.code == KC_CAPS) {
445 unregister_code(action.layer_tap.code);
447 dprint("KEYMAP_TAP_KEY: No tap: Off on release\n");
448 layer_off(action.layer_tap.val);
457 #ifndef NO_ACTION_MACRO
459 action_macro_play(action_get_macro(record, action.func.id, action.func.opt));
462 #ifdef BACKLIGHT_ENABLE
464 if (!event.pressed) {
465 switch (action.backlight.opt) {
466 case BACKLIGHT_INCREASE:
467 backlight_increase();
469 case BACKLIGHT_DECREASE:
470 backlight_decrease();
472 case BACKLIGHT_TOGGLE:
478 case BACKLIGHT_LEVEL:
479 backlight_level(action.backlight.level);
487 #ifdef ONEHAND_ENABLE
489 switch (action.swap.code) {
492 swap_hands = !swap_hands;
496 swap_hands = event.pressed;
499 swap_hands = !event.pressed;
502 if (!event.pressed) {
507 if (!event.pressed) {
511 #ifndef NO_ACTION_TAPPING
512 case OP_SH_TAP_TOGGLE:
515 if (!event.pressed) {
516 swap_hands = !swap_hands;
519 swap_hands = event.pressed;
525 register_code(action.swap.code);
527 unregister_code(action.swap.code);
530 swap_hands = event.pressed;
535 #ifndef NO_ACTION_FUNCTION
537 action_function(record, action.func.id, action.func.opt);
544 #ifndef NO_ACTION_LAYER
545 // if this event is a layer action, update the leds
546 switch (action.kind.id) {
548 #ifndef NO_ACTION_TAPPING
550 case ACT_LAYER_TAP_EXT:
552 led_set(host_keyboard_leds());
559 #ifndef NO_ACTION_ONESHOT
560 /* Because we switch layers after a oneshot event, we need to release the
561 * key before we leave the layer or no key up event will be generated.
563 if (do_release_oneshot && !(get_oneshot_layer_state() & ONESHOT_PRESSED ) ) {
564 record->event.pressed = false;
565 layer_on(get_oneshot_layer());
566 process_record(record);
567 layer_off(get_oneshot_layer());
576 * Utilities for actions.
578 void register_code(uint8_t code)
584 #ifdef LOCKING_SUPPORT_ENABLE
585 else if (KC_LOCKING_CAPS == code) {
586 #ifdef LOCKING_RESYNC_ENABLE
587 // Resync: ignore if caps lock already is on
588 if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) return;
590 add_key(KC_CAPSLOCK);
591 send_keyboard_report();
592 del_key(KC_CAPSLOCK);
593 send_keyboard_report();
596 else if (KC_LOCKING_NUM == code) {
597 #ifdef LOCKING_RESYNC_ENABLE
598 if (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK)) return;
601 send_keyboard_report();
603 send_keyboard_report();
606 else if (KC_LOCKING_SCROLL == code) {
607 #ifdef LOCKING_RESYNC_ENABLE
608 if (host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK)) return;
610 add_key(KC_SCROLLLOCK);
611 send_keyboard_report();
612 del_key(KC_SCROLLLOCK);
613 send_keyboard_report();
617 else if IS_KEY(code) {
618 // TODO: should push command_proc out of this block?
619 if (command_proc(code)) return;
621 #ifndef NO_ACTION_ONESHOT
623 if (oneshot_state.mods && !oneshot_state.disabled) {
624 uint8_t tmp_mods = get_mods();
625 add_mods(oneshot_state.mods);
628 send_keyboard_report();
631 send_keyboard_report();
638 send_keyboard_report();
641 else if IS_MOD(code) {
642 add_mods(MOD_BIT(code));
643 send_keyboard_report();
645 else if IS_SYSTEM(code) {
646 host_system_send(KEYCODE2SYSTEM(code));
648 else if IS_CONSUMER(code) {
649 host_consumer_send(KEYCODE2CONSUMER(code));
653 void unregister_code(uint8_t code)
659 #ifdef LOCKING_SUPPORT_ENABLE
660 else if (KC_LOCKING_CAPS == code) {
661 #ifdef LOCKING_RESYNC_ENABLE
662 // Resync: ignore if caps lock already is off
663 if (!(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK))) return;
665 add_key(KC_CAPSLOCK);
666 send_keyboard_report();
667 del_key(KC_CAPSLOCK);
668 send_keyboard_report();
671 else if (KC_LOCKING_NUM == code) {
672 #ifdef LOCKING_RESYNC_ENABLE
673 if (!(host_keyboard_leds() & (1<<USB_LED_NUM_LOCK))) return;
676 send_keyboard_report();
678 send_keyboard_report();
681 else if (KC_LOCKING_SCROLL == code) {
682 #ifdef LOCKING_RESYNC_ENABLE
683 if (!(host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK))) return;
685 add_key(KC_SCROLLLOCK);
686 send_keyboard_report();
687 del_key(KC_SCROLLLOCK);
688 send_keyboard_report();
692 else if IS_KEY(code) {
694 send_keyboard_report();
696 else if IS_MOD(code) {
697 del_mods(MOD_BIT(code));
698 send_keyboard_report();
700 else if IS_SYSTEM(code) {
703 else if IS_CONSUMER(code) {
704 host_consumer_send(0);
708 void register_mods(uint8_t mods)
712 send_keyboard_report();
716 void unregister_mods(uint8_t mods)
720 send_keyboard_report();
724 void clear_keyboard(void)
727 clear_keyboard_but_mods();
730 void clear_keyboard_but_mods(void)
735 send_keyboard_report();
736 #ifdef MOUSEKEY_ENABLE
740 #ifdef EXTRAKEY_ENABLE
742 host_consumer_send(0);
746 bool is_tap_key(keypos_t key)
748 action_t action = layer_switch_get_action(key);
750 switch (action.kind.id) {
754 case ACT_LAYER_TAP_EXT:
755 switch (action.layer_tap.code) {
763 switch (action.swap.code) {
765 case OP_SH_TAP_TOGGLE:
771 if (action.func.opt & FUNC_TAP) { return true; }
781 void debug_event(keyevent_t event)
783 dprintf("%04X%c(%u)", (event.key.row<<8 | event.key.col), (event.pressed ? 'd' : 'u'), event.time);
786 void debug_record(keyrecord_t record)
788 debug_event(record.event);
789 #ifndef NO_ACTION_TAPPING
790 dprintf(":%u%c", record.tap.count, (record.tap.interrupted ? '-' : ' '));
794 void debug_action(action_t action)
796 switch (action.kind.id) {
797 case ACT_LMODS: dprint("ACT_LMODS"); break;
798 case ACT_RMODS: dprint("ACT_RMODS"); break;
799 case ACT_LMODS_TAP: dprint("ACT_LMODS_TAP"); break;
800 case ACT_RMODS_TAP: dprint("ACT_RMODS_TAP"); break;
801 case ACT_USAGE: dprint("ACT_USAGE"); break;
802 case ACT_MOUSEKEY: dprint("ACT_MOUSEKEY"); break;
803 case ACT_LAYER: dprint("ACT_LAYER"); break;
804 case ACT_LAYER_TAP: dprint("ACT_LAYER_TAP"); break;
805 case ACT_LAYER_TAP_EXT: dprint("ACT_LAYER_TAP_EXT"); break;
806 case ACT_MACRO: dprint("ACT_MACRO"); break;
807 case ACT_COMMAND: dprint("ACT_COMMAND"); break;
808 case ACT_FUNCTION: dprint("ACT_FUNCTION"); break;
809 case ACT_SWAP_HANDS: dprint("ACT_SWAP_HANDS"); break;
810 default: dprint("UNKNOWN"); break;
812 dprintf("[%X:%02X]", action.kind.param>>8, action.kind.param&0xff);