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"
36 #ifdef FAUXCLICKY_ENABLE
37 #include <fauxclicky.h>
40 void action_exec(keyevent_t event)
42 if (!IS_NOEVENT(event)) {
43 dprint("\n---- action_exec: start -----\n");
44 dprint("EVENT: "); debug_event(event); dprintln();
47 #ifdef FAUXCLICKY_ENABLE
48 if (IS_PRESSED(event)) {
49 FAUXCLICKY_ACTION_PRESS;
51 if (IS_RELEASED(event)) {
52 FAUXCLICKY_ACTION_RELEASE;
58 if (!IS_NOEVENT(event)) {
59 process_hand_swap(&event);
63 keyrecord_t record = { .event = event };
65 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
66 if (has_oneshot_layer_timed_out()) {
67 dprintf("Oneshot layer: timeout\n");
68 clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
72 #ifndef NO_ACTION_TAPPING
73 action_tapping_process(record);
75 process_record(&record);
76 if (!IS_NOEVENT(record.event)) {
77 dprint("processed: "); debug_record(record); dprintln();
83 bool swap_hands = false;
85 void process_hand_swap(keyevent_t *event) {
86 static swap_state_row_t swap_state[MATRIX_ROWS];
88 keypos_t pos = event->key;
89 swap_state_row_t col_bit = (swap_state_row_t)1<<pos.col;
90 bool do_swap = event->pressed ? swap_hands :
91 swap_state[pos.row] & (col_bit);
94 event->key = hand_swap_config[pos.row][pos.col];
95 swap_state[pos.row] |= col_bit;
97 swap_state[pos.row] &= ~(col_bit);
102 #if !defined(NO_ACTION_LAYER) && defined(PREVENT_STUCK_MODIFIERS)
103 bool disable_action_cache = false;
105 void process_record_nocache(keyrecord_t *record)
107 disable_action_cache = true;
108 process_record(record);
109 disable_action_cache = false;
112 void process_record_nocache(keyrecord_t *record)
114 process_record(record);
118 __attribute__ ((weak))
119 bool process_record_quantum(keyrecord_t *record) {
123 void process_record(keyrecord_t *record)
125 if (IS_NOEVENT(record->event)) { return; }
127 if(!process_record_quantum(record))
130 action_t action = store_or_get_action(record->event.pressed, record->event.key);
131 dprint("ACTION: "); debug_action(action);
132 #ifndef NO_ACTION_LAYER
133 dprint(" layer_state: "); layer_debug();
134 dprint(" default_layer_state: "); default_layer_debug();
138 process_action(record, action);
141 void process_action(keyrecord_t *record, action_t action)
143 keyevent_t event = record->event;
144 #ifndef NO_ACTION_TAPPING
145 uint8_t tap_count = record->tap.count;
149 // clear the potential weak mods left by previously pressed keys
153 #ifndef NO_ACTION_ONESHOT
154 bool do_release_oneshot = false;
155 // notice we only clear the one shot layer if the pressed key is not a modifier.
156 if (is_oneshot_layer_active() && event.pressed && !IS_MOD(action.key.code)) {
157 clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
158 do_release_oneshot = !is_oneshot_layer_active();
162 switch (action.kind.id) {
167 uint8_t mods = (action.kind.id == ACT_LMODS) ? action.key.mods :
171 if (IS_MOD(action.key.code) || action.key.code == KC_NO) {
172 // e.g. LSFT(KC_LGUI): we don't want the LSFT to be weak as it would make it useless.
173 // This also makes LSFT(KC_LGUI) behave exactly the same as LGUI(KC_LSFT).
174 // Same applies for some keys like KC_MEH which are declared as MEH(KC_NO).
179 send_keyboard_report();
181 register_code(action.key.code);
183 unregister_code(action.key.code);
185 if (IS_MOD(action.key.code) || action.key.code == KC_NO) {
190 send_keyboard_report();
195 #ifndef NO_ACTION_TAPPING
199 uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods :
201 switch (action.layer_tap.code) {
202 #ifndef NO_ACTION_ONESHOT
206 if (tap_count == 0) {
207 dprint("MODS_TAP: Oneshot: 0\n");
209 } else if (tap_count == 1) {
210 dprint("MODS_TAP: Oneshot: start\n");
211 set_oneshot_mods(mods);
212 #if defined(ONESHOT_TAP_TOGGLE) && ONESHOT_TAP_TOGGLE > 1
213 } else if (tap_count == ONESHOT_TAP_TOGGLE) {
214 dprint("MODS_TAP: Toggling oneshot");
215 clear_oneshot_mods();
216 set_oneshot_locked_mods(mods);
223 if (tap_count == 0) {
224 clear_oneshot_mods();
225 unregister_mods(mods);
226 } else if (tap_count == 1) {
227 // Retain Oneshot mods
228 #if defined(ONESHOT_TAP_TOGGLE) && ONESHOT_TAP_TOGGLE > 1
229 if (mods & get_mods()) {
230 clear_oneshot_locked_mods();
231 clear_oneshot_mods();
232 unregister_mods(mods);
234 } else if (tap_count == ONESHOT_TAP_TOGGLE) {
235 // Toggle Oneshot Layer
238 clear_oneshot_mods();
239 unregister_mods(mods);
244 case MODS_TAP_TOGGLE:
246 if (tap_count <= TAPPING_TOGGLE) {
250 if (tap_count < TAPPING_TOGGLE) {
251 unregister_mods(mods);
258 #ifndef IGNORE_MOD_TAP_INTERRUPT
259 if (record->tap.interrupted) {
260 dprint("mods_tap: tap: cancel: add_mods\n");
261 // ad hoc: set 0 to cancel tap
262 record->tap.count = 0;
267 dprint("MODS_TAP: Tap: register_code\n");
268 register_code(action.key.code);
271 dprint("MODS_TAP: No tap: add_mods\n");
276 dprint("MODS_TAP: Tap: unregister_code\n");
277 unregister_code(action.key.code);
279 dprint("MODS_TAP: No tap: add_mods\n");
280 unregister_mods(mods);
288 #ifdef EXTRAKEY_ENABLE
289 /* other HID usage */
291 switch (action.usage.page) {
294 host_system_send(action.usage.code);
301 host_consumer_send(action.usage.code);
303 host_consumer_send(0);
309 #ifdef MOUSEKEY_ENABLE
313 mousekey_on(action.key.code);
316 mousekey_off(action.key.code);
321 #ifndef NO_ACTION_LAYER
323 if (action.layer_bitop.on == 0) {
324 /* Default Layer Bitwise Operation */
325 if (!event.pressed) {
326 uint8_t shift = action.layer_bitop.part*4;
327 uint32_t bits = ((uint32_t)action.layer_bitop.bits)<<shift;
328 uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
329 switch (action.layer_bitop.op) {
330 case OP_BIT_AND: default_layer_and(bits | mask); break;
331 case OP_BIT_OR: default_layer_or(bits | mask); break;
332 case OP_BIT_XOR: default_layer_xor(bits | mask); break;
333 case OP_BIT_SET: default_layer_and(mask); default_layer_or(bits); break;
337 /* Layer Bitwise Operation */
338 if (event.pressed ? (action.layer_bitop.on & ON_PRESS) :
339 (action.layer_bitop.on & ON_RELEASE)) {
340 uint8_t shift = action.layer_bitop.part*4;
341 uint32_t bits = ((uint32_t)action.layer_bitop.bits)<<shift;
342 uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
343 switch (action.layer_bitop.op) {
344 case OP_BIT_AND: layer_and(bits | mask); break;
345 case OP_BIT_OR: layer_or(bits | mask); break;
346 case OP_BIT_XOR: layer_xor(bits | mask); break;
347 case OP_BIT_SET: layer_and(mask); layer_or(bits); break;
352 #ifndef NO_ACTION_TAPPING
354 case ACT_LAYER_TAP_EXT:
355 switch (action.layer_tap.code) {
357 /* layer On/Off with modifiers(left only) */
359 layer_on(action.layer_tap.val);
360 register_mods(action.layer_tap.code & 0x0f);
362 layer_off(action.layer_tap.val);
363 unregister_mods(action.layer_tap.code & 0x0f);
369 if (tap_count < TAPPING_TOGGLE) {
370 layer_invert(action.layer_tap.val);
373 if (tap_count <= TAPPING_TOGGLE) {
374 layer_invert(action.layer_tap.val);
379 event.pressed ? layer_on(action.layer_tap.val) :
380 layer_off(action.layer_tap.val);
383 event.pressed ? layer_off(action.layer_tap.val) :
384 layer_on(action.layer_tap.val);
387 event.pressed ? layer_move(action.layer_tap.val) :
390 #ifndef NO_ACTION_ONESHOT
393 #if defined(ONESHOT_TAP_TOGGLE) && ONESHOT_TAP_TOGGLE > 1
394 do_release_oneshot = false;
396 del_mods(get_oneshot_locked_mods());
397 if (get_oneshot_layer_state() == ONESHOT_TOGGLED) {
398 reset_oneshot_layer();
399 layer_off(action.layer_tap.val);
401 } else if (tap_count < ONESHOT_TAP_TOGGLE) {
402 layer_on(action.layer_tap.val);
403 set_oneshot_layer(action.layer_tap.val, ONESHOT_START);
406 add_mods(get_oneshot_locked_mods());
407 if (tap_count >= ONESHOT_TAP_TOGGLE) {
408 reset_oneshot_layer();
409 clear_oneshot_locked_mods();
410 set_oneshot_layer(action.layer_tap.val, ONESHOT_TOGGLED);
412 clear_oneshot_layer_state(ONESHOT_PRESSED);
417 layer_on(action.layer_tap.val);
418 set_oneshot_layer(action.layer_tap.val, ONESHOT_START);
420 clear_oneshot_layer_state(ONESHOT_PRESSED);
422 clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
432 dprint("KEYMAP_TAP_KEY: Tap: register_code\n");
433 register_code(action.layer_tap.code);
435 dprint("KEYMAP_TAP_KEY: No tap: On on press\n");
436 layer_on(action.layer_tap.val);
440 dprint("KEYMAP_TAP_KEY: Tap: unregister_code\n");
441 unregister_code(action.layer_tap.code);
443 dprint("KEYMAP_TAP_KEY: No tap: Off on release\n");
444 layer_off(action.layer_tap.val);
453 #ifndef NO_ACTION_MACRO
455 action_macro_play(action_get_macro(record, action.func.id, action.func.opt));
458 #ifdef BACKLIGHT_ENABLE
460 if (!event.pressed) {
461 switch (action.backlight.opt) {
462 case BACKLIGHT_INCREASE:
463 backlight_increase();
465 case BACKLIGHT_DECREASE:
466 backlight_decrease();
468 case BACKLIGHT_TOGGLE:
474 case BACKLIGHT_LEVEL:
475 backlight_level(action.backlight.level);
483 #ifdef ONEHAND_ENABLE
485 switch (action.swap.code) {
488 swap_hands = !swap_hands;
492 swap_hands = event.pressed;
495 swap_hands = !event.pressed;
498 if (!event.pressed) {
503 if (!event.pressed) {
507 #ifndef NO_ACTION_TAPPING
508 case OP_SH_TAP_TOGGLE:
511 if (!event.pressed) {
512 swap_hands = !swap_hands;
515 swap_hands = event.pressed;
521 register_code(action.swap.code);
523 unregister_code(action.swap.code);
526 swap_hands = event.pressed;
531 #ifndef NO_ACTION_FUNCTION
533 action_function(record, action.func.id, action.func.opt);
540 #ifndef NO_ACTION_LAYER
541 // if this event is a layer action, update the leds
542 switch (action.kind.id) {
544 #ifndef NO_ACTION_TAPPING
546 case ACT_LAYER_TAP_EXT:
548 led_set(host_keyboard_leds());
555 #ifndef NO_ACTION_ONESHOT
556 /* Because we switch layers after a oneshot event, we need to release the
557 * key before we leave the layer or no key up event will be generated.
559 if (do_release_oneshot && !(get_oneshot_layer_state() & ONESHOT_PRESSED ) ) {
560 record->event.pressed = false;
561 layer_on(get_oneshot_layer());
562 process_record(record);
563 layer_off(get_oneshot_layer());
572 * Utilities for actions.
574 void register_code(uint8_t code)
580 #ifdef LOCKING_SUPPORT_ENABLE
581 else if (KC_LOCKING_CAPS == code) {
582 #ifdef LOCKING_RESYNC_ENABLE
583 // Resync: ignore if caps lock already is on
584 if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) return;
586 add_key(KC_CAPSLOCK);
587 send_keyboard_report();
588 del_key(KC_CAPSLOCK);
589 send_keyboard_report();
592 else if (KC_LOCKING_NUM == code) {
593 #ifdef LOCKING_RESYNC_ENABLE
594 if (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK)) return;
597 send_keyboard_report();
599 send_keyboard_report();
602 else if (KC_LOCKING_SCROLL == code) {
603 #ifdef LOCKING_RESYNC_ENABLE
604 if (host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK)) return;
606 add_key(KC_SCROLLLOCK);
607 send_keyboard_report();
608 del_key(KC_SCROLLLOCK);
609 send_keyboard_report();
613 else if IS_KEY(code) {
614 // TODO: should push command_proc out of this block?
615 if (command_proc(code)) return;
617 #ifndef NO_ACTION_ONESHOT
619 if (oneshot_state.mods && !oneshot_state.disabled) {
620 uint8_t tmp_mods = get_mods();
621 add_mods(oneshot_state.mods);
624 send_keyboard_report();
627 send_keyboard_report();
634 send_keyboard_report();
637 else if IS_MOD(code) {
638 add_mods(MOD_BIT(code));
639 send_keyboard_report();
641 else if IS_SYSTEM(code) {
642 host_system_send(KEYCODE2SYSTEM(code));
644 else if IS_CONSUMER(code) {
645 host_consumer_send(KEYCODE2CONSUMER(code));
649 void unregister_code(uint8_t code)
655 #ifdef LOCKING_SUPPORT_ENABLE
656 else if (KC_LOCKING_CAPS == code) {
657 #ifdef LOCKING_RESYNC_ENABLE
658 // Resync: ignore if caps lock already is off
659 if (!(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK))) return;
661 add_key(KC_CAPSLOCK);
662 send_keyboard_report();
663 del_key(KC_CAPSLOCK);
664 send_keyboard_report();
667 else if (KC_LOCKING_NUM == code) {
668 #ifdef LOCKING_RESYNC_ENABLE
669 if (!(host_keyboard_leds() & (1<<USB_LED_NUM_LOCK))) return;
672 send_keyboard_report();
674 send_keyboard_report();
677 else if (KC_LOCKING_SCROLL == code) {
678 #ifdef LOCKING_RESYNC_ENABLE
679 if (!(host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK))) return;
681 add_key(KC_SCROLLLOCK);
682 send_keyboard_report();
683 del_key(KC_SCROLLLOCK);
684 send_keyboard_report();
688 else if IS_KEY(code) {
690 send_keyboard_report();
692 else if IS_MOD(code) {
693 del_mods(MOD_BIT(code));
694 send_keyboard_report();
696 else if IS_SYSTEM(code) {
699 else if IS_CONSUMER(code) {
700 host_consumer_send(0);
704 void register_mods(uint8_t mods)
708 send_keyboard_report();
712 void unregister_mods(uint8_t mods)
716 send_keyboard_report();
720 void clear_keyboard(void)
723 clear_keyboard_but_mods();
726 void clear_keyboard_but_mods(void)
731 send_keyboard_report();
732 #ifdef MOUSEKEY_ENABLE
736 #ifdef EXTRAKEY_ENABLE
738 host_consumer_send(0);
742 bool is_tap_key(keypos_t key)
744 action_t action = layer_switch_get_action(key);
746 switch (action.kind.id) {
750 case ACT_LAYER_TAP_EXT:
751 switch (action.layer_tap.code) {
759 switch (action.swap.code) {
761 case OP_SH_TAP_TOGGLE:
767 if (action.func.opt & FUNC_TAP) { return true; }
777 void debug_event(keyevent_t event)
779 dprintf("%04X%c(%u)", (event.key.row<<8 | event.key.col), (event.pressed ? 'd' : 'u'), event.time);
782 void debug_record(keyrecord_t record)
784 debug_event(record.event);
785 #ifndef NO_ACTION_TAPPING
786 dprintf(":%u%c", record.tap.count, (record.tap.interrupted ? '-' : ' '));
790 void debug_action(action_t action)
792 switch (action.kind.id) {
793 case ACT_LMODS: dprint("ACT_LMODS"); break;
794 case ACT_RMODS: dprint("ACT_RMODS"); break;
795 case ACT_LMODS_TAP: dprint("ACT_LMODS_TAP"); break;
796 case ACT_RMODS_TAP: dprint("ACT_RMODS_TAP"); break;
797 case ACT_USAGE: dprint("ACT_USAGE"); break;
798 case ACT_MOUSEKEY: dprint("ACT_MOUSEKEY"); break;
799 case ACT_LAYER: dprint("ACT_LAYER"); break;
800 case ACT_LAYER_TAP: dprint("ACT_LAYER_TAP"); break;
801 case ACT_LAYER_TAP_EXT: dprint("ACT_LAYER_TAP_EXT"); break;
802 case ACT_MACRO: dprint("ACT_MACRO"); break;
803 case ACT_COMMAND: dprint("ACT_COMMAND"); break;
804 case ACT_FUNCTION: dprint("ACT_FUNCTION"); break;
805 case ACT_SWAP_HANDS: dprint("ACT_SWAP_HANDS"); break;
806 default: dprint("UNKNOWN"); break;
808 dprintf("[%X:%02X]", action.kind.param>>8, action.kind.param&0xff);