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 void action_exec(keyevent_t event)
39 if (!IS_NOEVENT(event)) {
40 dprint("\n---- action_exec: start -----\n");
41 dprint("EVENT: "); debug_event(event); dprintln();
45 if (!IS_NOEVENT(event)) {
46 process_hand_swap(&event);
50 keyrecord_t record = { .event = event };
52 #ifndef NO_ACTION_TAPPING
53 action_tapping_process(record);
55 process_record(&record);
56 if (!IS_NOEVENT(record.event)) {
57 dprint("processed: "); debug_record(record); dprintln();
63 bool swap_hands = false;
65 void process_hand_swap(keyevent_t *event) {
66 static swap_state_row_t swap_state[MATRIX_ROWS];
68 keypos_t pos = event->key;
69 swap_state_row_t col_bit = (swap_state_row_t)1<<pos.col;
70 bool do_swap = event->pressed ? swap_hands :
71 swap_state[pos.row] & (col_bit);
74 event->key = hand_swap_config[pos.row][pos.col];
75 swap_state[pos.row] |= col_bit;
77 swap_state[pos.row] &= ~(col_bit);
82 #if !defined(NO_ACTION_LAYER) && defined(PREVENT_STUCK_MODIFIERS)
83 bool disable_action_cache = false;
85 void process_record_nocache(keyrecord_t *record)
87 disable_action_cache = true;
88 process_record(record);
89 disable_action_cache = false;
92 void process_record_nocache(keyrecord_t *record)
94 process_record(record);
98 __attribute__ ((weak))
99 bool process_record_quantum(keyrecord_t *record) {
103 void process_record(keyrecord_t *record)
105 if (IS_NOEVENT(record->event)) { return; }
107 if(!process_record_quantum(record))
110 action_t action = store_or_get_action(record->event.pressed, record->event.key);
111 dprint("ACTION: "); debug_action(action);
112 #ifndef NO_ACTION_LAYER
113 dprint(" layer_state: "); layer_debug();
114 dprint(" default_layer_state: "); default_layer_debug();
118 process_action(record, action);
121 void process_action(keyrecord_t *record, action_t action)
123 bool do_release_oneshot = false;
124 keyevent_t event = record->event;
125 #ifndef NO_ACTION_TAPPING
126 uint8_t tap_count = record->tap.count;
129 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
130 if (has_oneshot_layer_timed_out()) {
131 dprintf("Oneshot layer: timeout\n");
132 clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
137 // clear the potential weak mods left by previously pressed keys
141 #ifndef NO_ACTION_ONESHOT
142 // notice we only clear the one shot layer if the pressed key is not a modifier.
143 if (is_oneshot_layer_active() && event.pressed && !IS_MOD(action.key.code)) {
144 clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
145 do_release_oneshot = !is_oneshot_layer_active();
149 switch (action.kind.id) {
154 uint8_t mods = (action.kind.id == ACT_LMODS) ? action.key.mods :
158 if (IS_MOD(action.key.code) || action.key.code == KC_NO) {
159 // e.g. LSFT(KC_LGUI): we don't want the LSFT to be weak as it would make it useless.
160 // This also makes LSFT(KC_LGUI) behave exactly the same as LGUI(KC_LSFT).
161 // Same applies for some keys like KC_MEH which are declared as MEH(KC_NO).
166 send_keyboard_report();
168 register_code(action.key.code);
170 unregister_code(action.key.code);
172 if (IS_MOD(action.key.code) || action.key.code == KC_NO) {
177 send_keyboard_report();
182 #ifndef NO_ACTION_TAPPING
186 uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods :
188 switch (action.layer_tap.code) {
189 #ifndef NO_ACTION_ONESHOT
193 if (tap_count == 0) {
194 dprint("MODS_TAP: Oneshot: 0\n");
196 } else if (tap_count == 1) {
197 dprint("MODS_TAP: Oneshot: start\n");
198 set_oneshot_mods(mods);
199 #if defined(ONESHOT_TAP_TOGGLE) && ONESHOT_TAP_TOGGLE > 1
200 } else if (tap_count == ONESHOT_TAP_TOGGLE) {
201 dprint("MODS_TAP: Toggling oneshot");
202 clear_oneshot_mods();
203 set_oneshot_locked_mods(mods);
210 if (tap_count == 0) {
211 clear_oneshot_mods();
212 unregister_mods(mods);
213 } else if (tap_count == 1) {
214 // Retain Oneshot mods
215 #if defined(ONESHOT_TAP_TOGGLE) && ONESHOT_TAP_TOGGLE > 1
216 if (mods & get_mods()) {
217 clear_oneshot_locked_mods();
218 clear_oneshot_mods();
219 unregister_mods(mods);
221 } else if (tap_count == ONESHOT_TAP_TOGGLE) {
222 // Toggle Oneshot Layer
225 clear_oneshot_mods();
226 unregister_mods(mods);
231 case MODS_TAP_TOGGLE:
233 if (tap_count <= TAPPING_TOGGLE) {
237 if (tap_count < TAPPING_TOGGLE) {
238 unregister_mods(mods);
245 #ifndef IGNORE_MOD_TAP_INTERRUPT
246 if (record->tap.interrupted) {
247 dprint("mods_tap: tap: cancel: add_mods\n");
248 // ad hoc: set 0 to cancel tap
249 record->tap.count = 0;
254 dprint("MODS_TAP: Tap: register_code\n");
255 register_code(action.key.code);
258 dprint("MODS_TAP: No tap: add_mods\n");
263 dprint("MODS_TAP: Tap: unregister_code\n");
264 unregister_code(action.key.code);
266 dprint("MODS_TAP: No tap: add_mods\n");
267 unregister_mods(mods);
275 #ifdef EXTRAKEY_ENABLE
276 /* other HID usage */
278 switch (action.usage.page) {
281 host_system_send(action.usage.code);
288 host_consumer_send(action.usage.code);
290 host_consumer_send(0);
296 #ifdef MOUSEKEY_ENABLE
300 mousekey_on(action.key.code);
303 mousekey_off(action.key.code);
308 #ifndef NO_ACTION_LAYER
310 if (action.layer_bitop.on == 0) {
311 /* Default Layer Bitwise Operation */
312 if (!event.pressed) {
313 uint8_t shift = action.layer_bitop.part*4;
314 uint32_t bits = ((uint32_t)action.layer_bitop.bits)<<shift;
315 uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
316 switch (action.layer_bitop.op) {
317 case OP_BIT_AND: default_layer_and(bits | mask); break;
318 case OP_BIT_OR: default_layer_or(bits | mask); break;
319 case OP_BIT_XOR: default_layer_xor(bits | mask); break;
320 case OP_BIT_SET: default_layer_and(mask); default_layer_or(bits); break;
324 /* Layer Bitwise Operation */
325 if (event.pressed ? (action.layer_bitop.on & ON_PRESS) :
326 (action.layer_bitop.on & ON_RELEASE)) {
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: layer_and(bits | mask); break;
332 case OP_BIT_OR: layer_or(bits | mask); break;
333 case OP_BIT_XOR: layer_xor(bits | mask); break;
334 case OP_BIT_SET: layer_and(mask); layer_or(bits); break;
339 #ifndef NO_ACTION_TAPPING
341 case ACT_LAYER_TAP_EXT:
342 switch (action.layer_tap.code) {
344 /* layer On/Off with modifiers(left only) */
346 layer_on(action.layer_tap.val);
347 register_mods(action.layer_tap.code & 0x0f);
349 layer_off(action.layer_tap.val);
350 unregister_mods(action.layer_tap.code & 0x0f);
356 if (tap_count < TAPPING_TOGGLE) {
357 layer_invert(action.layer_tap.val);
360 if (tap_count <= TAPPING_TOGGLE) {
361 layer_invert(action.layer_tap.val);
366 event.pressed ? layer_on(action.layer_tap.val) :
367 layer_off(action.layer_tap.val);
370 event.pressed ? layer_off(action.layer_tap.val) :
371 layer_on(action.layer_tap.val);
374 event.pressed ? layer_move(action.layer_tap.val) :
377 #ifndef NO_ACTION_ONESHOT
380 #if defined(ONESHOT_TAP_TOGGLE) && ONESHOT_TAP_TOGGLE > 1
381 do_release_oneshot = false;
383 del_mods(get_oneshot_locked_mods());
384 if (get_oneshot_layer_state() == ONESHOT_TOGGLED) {
385 reset_oneshot_layer();
386 layer_off(action.layer_tap.val);
388 } else if (tap_count < ONESHOT_TAP_TOGGLE) {
389 layer_on(action.layer_tap.val);
390 set_oneshot_layer(action.layer_tap.val, ONESHOT_START);
393 add_mods(get_oneshot_locked_mods());
394 if (tap_count >= ONESHOT_TAP_TOGGLE) {
395 reset_oneshot_layer();
396 clear_oneshot_locked_mods();
397 set_oneshot_layer(action.layer_tap.val, ONESHOT_TOGGLED);
399 clear_oneshot_layer_state(ONESHOT_PRESSED);
404 layer_on(action.layer_tap.val);
405 set_oneshot_layer(action.layer_tap.val, ONESHOT_START);
407 clear_oneshot_layer_state(ONESHOT_PRESSED);
409 clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
419 dprint("KEYMAP_TAP_KEY: Tap: register_code\n");
420 register_code(action.layer_tap.code);
422 dprint("KEYMAP_TAP_KEY: No tap: On on press\n");
423 layer_on(action.layer_tap.val);
427 dprint("KEYMAP_TAP_KEY: Tap: unregister_code\n");
428 unregister_code(action.layer_tap.code);
430 dprint("KEYMAP_TAP_KEY: No tap: Off on release\n");
431 layer_off(action.layer_tap.val);
440 #ifndef NO_ACTION_MACRO
442 action_macro_play(action_get_macro(record, action.func.id, action.func.opt));
445 #ifdef BACKLIGHT_ENABLE
447 if (!event.pressed) {
448 switch (action.backlight.opt) {
449 case BACKLIGHT_INCREASE:
450 backlight_increase();
452 case BACKLIGHT_DECREASE:
453 backlight_decrease();
455 case BACKLIGHT_TOGGLE:
461 case BACKLIGHT_LEVEL:
462 backlight_level(action.backlight.level);
470 #ifdef ONEHAND_ENABLE
472 switch (action.swap.code) {
475 swap_hands = !swap_hands;
479 swap_hands = event.pressed;
482 swap_hands = !event.pressed;
485 if (!event.pressed) {
490 if (!event.pressed) {
494 #ifndef NO_ACTION_TAPPING
495 case OP_SH_TAP_TOGGLE:
498 if (!event.pressed) {
499 swap_hands = !swap_hands;
502 swap_hands = event.pressed;
508 register_code(action.swap.code);
510 unregister_code(action.swap.code);
513 swap_hands = event.pressed;
518 #ifndef NO_ACTION_FUNCTION
520 action_function(record, action.func.id, action.func.opt);
527 #ifndef NO_ACTION_ONESHOT
528 /* Because we switch layers after a oneshot event, we need to release the
529 * key before we leave the layer or no key up event will be generated.
531 if (do_release_oneshot && !(get_oneshot_layer_state() & ONESHOT_PRESSED ) ) {
532 record->event.pressed = false;
533 layer_on(get_oneshot_layer());
534 process_record(record);
535 layer_off(get_oneshot_layer());
544 * Utilities for actions.
546 void register_code(uint8_t code)
552 #ifdef LOCKING_SUPPORT_ENABLE
553 else if (KC_LOCKING_CAPS == code) {
554 #ifdef LOCKING_RESYNC_ENABLE
555 // Resync: ignore if caps lock already is on
556 if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) return;
558 add_key(KC_CAPSLOCK);
559 send_keyboard_report();
560 del_key(KC_CAPSLOCK);
561 send_keyboard_report();
564 else if (KC_LOCKING_NUM == code) {
565 #ifdef LOCKING_RESYNC_ENABLE
566 if (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK)) return;
569 send_keyboard_report();
571 send_keyboard_report();
574 else if (KC_LOCKING_SCROLL == code) {
575 #ifdef LOCKING_RESYNC_ENABLE
576 if (host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK)) return;
578 add_key(KC_SCROLLLOCK);
579 send_keyboard_report();
580 del_key(KC_SCROLLLOCK);
581 send_keyboard_report();
585 else if IS_KEY(code) {
586 // TODO: should push command_proc out of this block?
587 if (command_proc(code)) return;
589 #ifndef NO_ACTION_ONESHOT
591 if (oneshot_state.mods && !oneshot_state.disabled) {
592 uint8_t tmp_mods = get_mods();
593 add_mods(oneshot_state.mods);
596 send_keyboard_report();
599 send_keyboard_report();
606 send_keyboard_report();
609 else if IS_MOD(code) {
610 add_mods(MOD_BIT(code));
611 send_keyboard_report();
613 else if IS_SYSTEM(code) {
614 host_system_send(KEYCODE2SYSTEM(code));
616 else if IS_CONSUMER(code) {
617 host_consumer_send(KEYCODE2CONSUMER(code));
621 void unregister_code(uint8_t code)
627 #ifdef LOCKING_SUPPORT_ENABLE
628 else if (KC_LOCKING_CAPS == code) {
629 #ifdef LOCKING_RESYNC_ENABLE
630 // Resync: ignore if caps lock already is off
631 if (!(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK))) return;
633 add_key(KC_CAPSLOCK);
634 send_keyboard_report();
635 del_key(KC_CAPSLOCK);
636 send_keyboard_report();
639 else if (KC_LOCKING_NUM == code) {
640 #ifdef LOCKING_RESYNC_ENABLE
641 if (!(host_keyboard_leds() & (1<<USB_LED_NUM_LOCK))) return;
644 send_keyboard_report();
646 send_keyboard_report();
649 else if (KC_LOCKING_SCROLL == code) {
650 #ifdef LOCKING_RESYNC_ENABLE
651 if (!(host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK))) return;
653 add_key(KC_SCROLLLOCK);
654 send_keyboard_report();
655 del_key(KC_SCROLLLOCK);
656 send_keyboard_report();
660 else if IS_KEY(code) {
662 send_keyboard_report();
664 else if IS_MOD(code) {
665 del_mods(MOD_BIT(code));
666 send_keyboard_report();
668 else if IS_SYSTEM(code) {
671 else if IS_CONSUMER(code) {
672 host_consumer_send(0);
676 void register_mods(uint8_t mods)
680 send_keyboard_report();
684 void unregister_mods(uint8_t mods)
688 send_keyboard_report();
692 void clear_keyboard(void)
695 clear_keyboard_but_mods();
698 void clear_keyboard_but_mods(void)
703 send_keyboard_report();
704 #ifdef MOUSEKEY_ENABLE
708 #ifdef EXTRAKEY_ENABLE
710 host_consumer_send(0);
714 bool is_tap_key(keypos_t key)
716 action_t action = layer_switch_get_action(key);
718 switch (action.kind.id) {
722 case ACT_LAYER_TAP_EXT:
723 switch (action.layer_tap.code) {
731 switch (action.swap.code) {
733 case OP_SH_TAP_TOGGLE:
739 if (action.func.opt & FUNC_TAP) { return true; }
749 void debug_event(keyevent_t event)
751 dprintf("%04X%c(%u)", (event.key.row<<8 | event.key.col), (event.pressed ? 'd' : 'u'), event.time);
754 void debug_record(keyrecord_t record)
756 debug_event(record.event);
757 #ifndef NO_ACTION_TAPPING
758 dprintf(":%u%c", record.tap.count, (record.tap.interrupted ? '-' : ' '));
762 void debug_action(action_t action)
764 switch (action.kind.id) {
765 case ACT_LMODS: dprint("ACT_LMODS"); break;
766 case ACT_RMODS: dprint("ACT_RMODS"); break;
767 case ACT_LMODS_TAP: dprint("ACT_LMODS_TAP"); break;
768 case ACT_RMODS_TAP: dprint("ACT_RMODS_TAP"); break;
769 case ACT_USAGE: dprint("ACT_USAGE"); break;
770 case ACT_MOUSEKEY: dprint("ACT_MOUSEKEY"); break;
771 case ACT_LAYER: dprint("ACT_LAYER"); break;
772 case ACT_LAYER_TAP: dprint("ACT_LAYER_TAP"); break;
773 case ACT_LAYER_TAP_EXT: dprint("ACT_LAYER_TAP_EXT"); break;
774 case ACT_MACRO: dprint("ACT_MACRO"); break;
775 case ACT_COMMAND: dprint("ACT_COMMAND"); break;
776 case ACT_FUNCTION: dprint("ACT_FUNCTION"); break;
777 case ACT_SWAP_HANDS: dprint("ACT_SWAP_HANDS"); break;
778 default: dprint("UNKNOWN"); break;
780 dprintf("[%X:%02X]", action.kind.param>>8, action.kind.param&0xff);