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();
44 keyrecord_t record = { .event = event };
46 #ifndef NO_ACTION_TAPPING
47 action_tapping_process(record);
49 process_action(&record);
50 if (!IS_NOEVENT(record.event)) {
51 dprint("processed: "); debug_record(record); dprintln();
56 #if !defined(NO_ACTION_LAYER) && defined(PREVENT_STUCK_MODIFIERS)
57 bool disable_action_cache = false;
59 void process_action_nocache(keyrecord_t *record)
61 disable_action_cache = true;
62 process_action(record);
63 disable_action_cache = false;
66 void process_action_nocache(keyrecord_t *record)
68 process_action(record);
72 __attribute__ ((weak))
73 void process_action_kb(keyrecord_t *record) {}
75 void process_action(keyrecord_t *record)
77 bool do_release_oneshot = false;
78 keyevent_t event = record->event;
79 #ifndef NO_ACTION_TAPPING
80 uint8_t tap_count = record->tap.count;
83 if (IS_NOEVENT(event)) { return; }
85 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
86 if (has_oneshot_layer_timed_out()) {
87 dprintf("Oneshot layer: timeout\n");
88 clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
92 process_action_kb(record);
94 action_t action = store_or_get_action(event.pressed, event.key);
95 dprint("ACTION: "); debug_action(action);
96 #ifndef NO_ACTION_LAYER
97 dprint(" layer_state: "); layer_debug();
98 dprint(" default_layer_state: "); default_layer_debug();
103 // clear the potential weak mods left by previously pressed keys
107 #ifndef NO_ACTION_ONESHOT
108 // notice we only clear the one shot layer if the pressed key is not a modifier.
109 if (is_oneshot_layer_active() && event.pressed && !IS_MOD(action.key.code)) {
110 clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
111 do_release_oneshot = !is_oneshot_layer_active();
115 switch (action.kind.id) {
120 uint8_t mods = (action.kind.id == ACT_LMODS) ? action.key.mods :
124 if (IS_MOD(action.key.code)) {
125 // e.g. LSFT(KC_LGUI): we don't want the LSFT to be weak as it would make it useless.
126 // this also makes LSFT(KC_LGUI) behave exactly the same as LGUI(KC_LSFT)
131 send_keyboard_report();
133 register_code(action.key.code);
135 unregister_code(action.key.code);
137 if (IS_MOD(action.key.code)) {
142 send_keyboard_report();
147 #ifndef NO_ACTION_TAPPING
151 uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods :
153 switch (action.layer_tap.code) {
154 #ifndef NO_ACTION_ONESHOT
158 if (tap_count == 0) {
159 dprint("MODS_TAP: Oneshot: 0\n");
161 } else if (tap_count == 1) {
162 dprint("MODS_TAP: Oneshot: start\n");
163 set_oneshot_mods(mods);
164 #if defined(ONESHOT_TAP_TOGGLE) && ONESHOT_TAP_TOGGLE > 1
165 } else if (tap_count == ONESHOT_TAP_TOGGLE) {
166 dprint("MODS_TAP: Toggling oneshot");
167 clear_oneshot_mods();
168 set_oneshot_locked_mods(mods);
175 if (tap_count == 0) {
176 clear_oneshot_mods();
177 unregister_mods(mods);
178 } else if (tap_count == 1) {
179 // Retain Oneshot mods
180 #if defined(ONESHOT_TAP_TOGGLE) && ONESHOT_TAP_TOGGLE > 1
181 if (mods & get_mods()) {
182 clear_oneshot_locked_mods();
183 clear_oneshot_mods();
184 unregister_mods(mods);
186 } else if (tap_count == ONESHOT_TAP_TOGGLE) {
187 // Toggle Oneshot Layer
190 clear_oneshot_mods();
191 unregister_mods(mods);
196 case MODS_TAP_TOGGLE:
198 if (tap_count <= TAPPING_TOGGLE) {
202 if (tap_count < TAPPING_TOGGLE) {
203 unregister_mods(mods);
210 #ifndef IGNORE_MOD_TAP_INTERRUPT
211 if (record->tap.interrupted) {
212 dprint("mods_tap: tap: cancel: add_mods\n");
213 // ad hoc: set 0 to cancel tap
214 record->tap.count = 0;
219 dprint("MODS_TAP: Tap: register_code\n");
220 register_code(action.key.code);
223 dprint("MODS_TAP: No tap: add_mods\n");
228 dprint("MODS_TAP: Tap: unregister_code\n");
229 unregister_code(action.key.code);
231 dprint("MODS_TAP: No tap: add_mods\n");
232 unregister_mods(mods);
240 #ifdef EXTRAKEY_ENABLE
241 /* other HID usage */
243 switch (action.usage.page) {
246 host_system_send(action.usage.code);
253 host_consumer_send(action.usage.code);
255 host_consumer_send(0);
261 #ifdef MOUSEKEY_ENABLE
265 mousekey_on(action.key.code);
268 mousekey_off(action.key.code);
273 #ifndef NO_ACTION_LAYER
275 if (action.layer_bitop.on == 0) {
276 /* Default Layer Bitwise Operation */
277 if (!event.pressed) {
278 uint8_t shift = action.layer_bitop.part*4;
279 uint32_t bits = ((uint32_t)action.layer_bitop.bits)<<shift;
280 uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
281 switch (action.layer_bitop.op) {
282 case OP_BIT_AND: default_layer_and(bits | mask); break;
283 case OP_BIT_OR: default_layer_or(bits | mask); break;
284 case OP_BIT_XOR: default_layer_xor(bits | mask); break;
285 case OP_BIT_SET: default_layer_and(mask); default_layer_or(bits); break;
289 /* Layer Bitwise Operation */
290 if (event.pressed ? (action.layer_bitop.on & ON_PRESS) :
291 (action.layer_bitop.on & ON_RELEASE)) {
292 uint8_t shift = action.layer_bitop.part*4;
293 uint32_t bits = ((uint32_t)action.layer_bitop.bits)<<shift;
294 uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
295 switch (action.layer_bitop.op) {
296 case OP_BIT_AND: layer_and(bits | mask); break;
297 case OP_BIT_OR: layer_or(bits | mask); break;
298 case OP_BIT_XOR: layer_xor(bits | mask); break;
299 case OP_BIT_SET: layer_and(mask); layer_or(bits); break;
304 #ifndef NO_ACTION_TAPPING
306 case ACT_LAYER_TAP_EXT:
307 switch (action.layer_tap.code) {
309 /* layer On/Off with modifiers(left only) */
311 layer_on(action.layer_tap.val);
312 register_mods(action.layer_tap.code & 0x0f);
314 layer_off(action.layer_tap.val);
315 unregister_mods(action.layer_tap.code & 0x0f);
321 if (tap_count < TAPPING_TOGGLE) {
322 layer_invert(action.layer_tap.val);
325 if (tap_count <= TAPPING_TOGGLE) {
326 layer_invert(action.layer_tap.val);
331 event.pressed ? layer_on(action.layer_tap.val) :
332 layer_off(action.layer_tap.val);
335 event.pressed ? layer_off(action.layer_tap.val) :
336 layer_on(action.layer_tap.val);
339 event.pressed ? layer_move(action.layer_tap.val) :
342 #ifndef NO_ACTION_ONESHOT
345 #if defined(ONESHOT_TAP_TOGGLE) && ONESHOT_TAP_TOGGLE > 1
346 do_release_oneshot = false;
348 del_mods(get_oneshot_locked_mods());
349 if (get_oneshot_layer_state() == ONESHOT_TOGGLED) {
350 reset_oneshot_layer();
351 layer_off(action.layer_tap.val);
353 } else if (tap_count < ONESHOT_TAP_TOGGLE) {
354 layer_on(action.layer_tap.val);
355 set_oneshot_layer(action.layer_tap.val, ONESHOT_START);
358 add_mods(get_oneshot_locked_mods());
359 if (tap_count >= ONESHOT_TAP_TOGGLE) {
360 reset_oneshot_layer();
361 clear_oneshot_locked_mods();
362 set_oneshot_layer(action.layer_tap.val, ONESHOT_TOGGLED);
364 clear_oneshot_layer_state(ONESHOT_PRESSED);
369 layer_on(action.layer_tap.val);
370 set_oneshot_layer(action.layer_tap.val, ONESHOT_START);
372 clear_oneshot_layer_state(ONESHOT_PRESSED);
374 clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
384 dprint("KEYMAP_TAP_KEY: Tap: register_code\n");
385 register_code(action.layer_tap.code);
387 dprint("KEYMAP_TAP_KEY: No tap: On on press\n");
388 layer_on(action.layer_tap.val);
392 dprint("KEYMAP_TAP_KEY: Tap: unregister_code\n");
393 unregister_code(action.layer_tap.code);
395 dprint("KEYMAP_TAP_KEY: No tap: Off on release\n");
396 layer_off(action.layer_tap.val);
405 #ifndef NO_ACTION_MACRO
407 action_macro_play(action_get_macro(record, action.func.id, action.func.opt));
410 #ifdef BACKLIGHT_ENABLE
412 if (!event.pressed) {
413 switch (action.backlight.opt) {
414 case BACKLIGHT_INCREASE:
415 backlight_increase();
417 case BACKLIGHT_DECREASE:
418 backlight_decrease();
420 case BACKLIGHT_TOGGLE:
426 case BACKLIGHT_LEVEL:
427 backlight_level(action.backlight.level);
435 #ifndef NO_ACTION_FUNCTION
437 action_function(record, action.func.id, action.func.opt);
444 #ifndef NO_ACTION_ONESHOT
445 /* Because we switch layers after a oneshot event, we need to release the
446 * key before we leave the layer or no key up event will be generated.
448 if (do_release_oneshot && !(get_oneshot_layer_state() & ONESHOT_PRESSED ) ) {
449 record->event.pressed = false;
450 layer_on(get_oneshot_layer());
451 process_action(record);
452 layer_off(get_oneshot_layer());
461 * Utilities for actions.
463 void register_code(uint8_t code)
469 #ifdef LOCKING_SUPPORT_ENABLE
470 else if (KC_LOCKING_CAPS == code) {
471 #ifdef LOCKING_RESYNC_ENABLE
472 // Resync: ignore if caps lock already is on
473 if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) return;
475 add_key(KC_CAPSLOCK);
476 send_keyboard_report();
477 del_key(KC_CAPSLOCK);
478 send_keyboard_report();
481 else if (KC_LOCKING_NUM == code) {
482 #ifdef LOCKING_RESYNC_ENABLE
483 if (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK)) return;
486 send_keyboard_report();
488 send_keyboard_report();
491 else if (KC_LOCKING_SCROLL == code) {
492 #ifdef LOCKING_RESYNC_ENABLE
493 if (host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK)) return;
495 add_key(KC_SCROLLLOCK);
496 send_keyboard_report();
497 del_key(KC_SCROLLLOCK);
498 send_keyboard_report();
502 else if IS_KEY(code) {
503 // TODO: should push command_proc out of this block?
504 if (command_proc(code)) return;
506 #ifndef NO_ACTION_ONESHOT
508 if (oneshot_state.mods && !oneshot_state.disabled) {
509 uint8_t tmp_mods = get_mods();
510 add_mods(oneshot_state.mods);
513 send_keyboard_report();
516 send_keyboard_report();
523 send_keyboard_report();
526 else if IS_MOD(code) {
527 add_mods(MOD_BIT(code));
528 send_keyboard_report();
530 else if IS_SYSTEM(code) {
531 host_system_send(KEYCODE2SYSTEM(code));
533 else if IS_CONSUMER(code) {
534 host_consumer_send(KEYCODE2CONSUMER(code));
538 void unregister_code(uint8_t code)
544 #ifdef LOCKING_SUPPORT_ENABLE
545 else if (KC_LOCKING_CAPS == code) {
546 #ifdef LOCKING_RESYNC_ENABLE
547 // Resync: ignore if caps lock already is off
548 if (!(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK))) return;
550 add_key(KC_CAPSLOCK);
551 send_keyboard_report();
552 del_key(KC_CAPSLOCK);
553 send_keyboard_report();
556 else if (KC_LOCKING_NUM == code) {
557 #ifdef LOCKING_RESYNC_ENABLE
558 if (!(host_keyboard_leds() & (1<<USB_LED_NUM_LOCK))) return;
561 send_keyboard_report();
563 send_keyboard_report();
566 else if (KC_LOCKING_SCROLL == code) {
567 #ifdef LOCKING_RESYNC_ENABLE
568 if (!(host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK))) return;
570 add_key(KC_SCROLLLOCK);
571 send_keyboard_report();
572 del_key(KC_SCROLLLOCK);
573 send_keyboard_report();
577 else if IS_KEY(code) {
579 send_keyboard_report();
581 else if IS_MOD(code) {
582 del_mods(MOD_BIT(code));
583 send_keyboard_report();
585 else if IS_SYSTEM(code) {
588 else if IS_CONSUMER(code) {
589 host_consumer_send(0);
593 void register_mods(uint8_t mods)
597 send_keyboard_report();
601 void unregister_mods(uint8_t mods)
605 send_keyboard_report();
609 void clear_keyboard(void)
612 clear_keyboard_but_mods();
615 void clear_keyboard_but_mods(void)
620 send_keyboard_report();
621 #ifdef MOUSEKEY_ENABLE
625 #ifdef EXTRAKEY_ENABLE
627 host_consumer_send(0);
631 bool is_tap_key(keypos_t key)
633 action_t action = layer_switch_get_action(key);
635 switch (action.kind.id) {
639 case ACT_LAYER_TAP_EXT:
640 switch (action.layer_tap.code) {
649 if (action.func.opt & FUNC_TAP) { return true; }
659 void debug_event(keyevent_t event)
661 dprintf("%04X%c(%u)", (event.key.row<<8 | event.key.col), (event.pressed ? 'd' : 'u'), event.time);
664 void debug_record(keyrecord_t record)
666 debug_event(record.event);
667 #ifndef NO_ACTION_TAPPING
668 dprintf(":%u%c", record.tap.count, (record.tap.interrupted ? '-' : ' '));
672 void debug_action(action_t action)
674 switch (action.kind.id) {
675 case ACT_LMODS: dprint("ACT_LMODS"); break;
676 case ACT_RMODS: dprint("ACT_RMODS"); break;
677 case ACT_LMODS_TAP: dprint("ACT_LMODS_TAP"); break;
678 case ACT_RMODS_TAP: dprint("ACT_RMODS_TAP"); break;
679 case ACT_USAGE: dprint("ACT_USAGE"); break;
680 case ACT_MOUSEKEY: dprint("ACT_MOUSEKEY"); break;
681 case ACT_LAYER: dprint("ACT_LAYER"); break;
682 case ACT_LAYER_TAP: dprint("ACT_LAYER_TAP"); break;
683 case ACT_LAYER_TAP_EXT: dprint("ACT_LAYER_TAP_EXT"); break;
684 case ACT_MACRO: dprint("ACT_MACRO"); break;
685 case ACT_COMMAND: dprint("ACT_COMMAND"); break;
686 case ACT_FUNCTION: dprint("ACT_FUNCTION"); break;
687 default: dprint("UNKNOWN"); break;
689 dprintf("[%X:%02X]", action.kind.param>>8, action.kind.param&0xff);