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_record(&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_record_nocache(keyrecord_t *record)
61 disable_action_cache = true;
62 process_record(record);
63 disable_action_cache = false;
66 void process_record_nocache(keyrecord_t *record)
68 process_record(record);
72 __attribute__ ((weak))
73 bool process_record_quantum(keyrecord_t *record) {
77 void process_record(keyrecord_t *record)
79 if (IS_NOEVENT(record->event)) { return; }
81 if(!process_record_quantum(record))
84 action_t action = store_or_get_action(record->event.pressed, record->event.key);
85 dprint("ACTION: "); debug_action(action);
86 #ifndef NO_ACTION_LAYER
87 dprint(" layer_state: "); layer_debug();
88 dprint(" default_layer_state: "); default_layer_debug();
92 process_action(record, action);
95 void process_action(keyrecord_t *record, action_t action)
97 bool do_release_oneshot = false;
98 keyevent_t event = record->event;
99 #ifndef NO_ACTION_TAPPING
100 uint8_t tap_count = record->tap.count;
103 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
104 if (has_oneshot_layer_timed_out()) {
105 dprintf("Oneshot layer: timeout\n");
106 clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
111 // clear the potential weak mods left by previously pressed keys
115 #ifndef NO_ACTION_ONESHOT
116 // notice we only clear the one shot layer if the pressed key is not a modifier.
117 if (is_oneshot_layer_active() && event.pressed && !IS_MOD(action.key.code)) {
118 clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
119 do_release_oneshot = !is_oneshot_layer_active();
123 switch (action.kind.id) {
128 uint8_t mods = (action.kind.id == ACT_LMODS) ? action.key.mods :
132 if (IS_MOD(action.key.code)) {
133 // e.g. LSFT(KC_LGUI): we don't want the LSFT to be weak as it would make it useless.
134 // this also makes LSFT(KC_LGUI) behave exactly the same as LGUI(KC_LSFT)
139 send_keyboard_report();
141 register_code(action.key.code);
143 unregister_code(action.key.code);
145 if (IS_MOD(action.key.code)) {
150 send_keyboard_report();
155 #ifndef NO_ACTION_TAPPING
159 uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods :
161 switch (action.layer_tap.code) {
162 #ifndef NO_ACTION_ONESHOT
166 if (tap_count == 0) {
167 dprint("MODS_TAP: Oneshot: 0\n");
169 } else if (tap_count == 1) {
170 dprint("MODS_TAP: Oneshot: start\n");
171 set_oneshot_mods(mods);
172 #if defined(ONESHOT_TAP_TOGGLE) && ONESHOT_TAP_TOGGLE > 1
173 } else if (tap_count == ONESHOT_TAP_TOGGLE) {
174 dprint("MODS_TAP: Toggling oneshot");
175 clear_oneshot_mods();
176 set_oneshot_locked_mods(mods);
183 if (tap_count == 0) {
184 clear_oneshot_mods();
185 unregister_mods(mods);
186 } else if (tap_count == 1) {
187 // Retain Oneshot mods
188 #if defined(ONESHOT_TAP_TOGGLE) && ONESHOT_TAP_TOGGLE > 1
189 if (mods & get_mods()) {
190 clear_oneshot_locked_mods();
191 clear_oneshot_mods();
192 unregister_mods(mods);
194 } else if (tap_count == ONESHOT_TAP_TOGGLE) {
195 // Toggle Oneshot Layer
198 clear_oneshot_mods();
199 unregister_mods(mods);
204 case MODS_TAP_TOGGLE:
206 if (tap_count <= TAPPING_TOGGLE) {
210 if (tap_count < TAPPING_TOGGLE) {
211 unregister_mods(mods);
218 #ifndef IGNORE_MOD_TAP_INTERRUPT
219 if (record->tap.interrupted) {
220 dprint("mods_tap: tap: cancel: add_mods\n");
221 // ad hoc: set 0 to cancel tap
222 record->tap.count = 0;
227 dprint("MODS_TAP: Tap: register_code\n");
228 register_code(action.key.code);
231 dprint("MODS_TAP: No tap: add_mods\n");
236 dprint("MODS_TAP: Tap: unregister_code\n");
237 unregister_code(action.key.code);
239 dprint("MODS_TAP: No tap: add_mods\n");
240 unregister_mods(mods);
248 #ifdef EXTRAKEY_ENABLE
249 /* other HID usage */
251 switch (action.usage.page) {
254 host_system_send(action.usage.code);
261 host_consumer_send(action.usage.code);
263 host_consumer_send(0);
269 #ifdef MOUSEKEY_ENABLE
273 mousekey_on(action.key.code);
276 mousekey_off(action.key.code);
281 #ifndef NO_ACTION_LAYER
283 if (action.layer_bitop.on == 0) {
284 /* Default Layer Bitwise Operation */
285 if (!event.pressed) {
286 uint8_t shift = action.layer_bitop.part*4;
287 uint32_t bits = ((uint32_t)action.layer_bitop.bits)<<shift;
288 uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
289 switch (action.layer_bitop.op) {
290 case OP_BIT_AND: default_layer_and(bits | mask); break;
291 case OP_BIT_OR: default_layer_or(bits | mask); break;
292 case OP_BIT_XOR: default_layer_xor(bits | mask); break;
293 case OP_BIT_SET: default_layer_and(mask); default_layer_or(bits); break;
297 /* Layer Bitwise Operation */
298 if (event.pressed ? (action.layer_bitop.on & ON_PRESS) :
299 (action.layer_bitop.on & ON_RELEASE)) {
300 uint8_t shift = action.layer_bitop.part*4;
301 uint32_t bits = ((uint32_t)action.layer_bitop.bits)<<shift;
302 uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
303 switch (action.layer_bitop.op) {
304 case OP_BIT_AND: layer_and(bits | mask); break;
305 case OP_BIT_OR: layer_or(bits | mask); break;
306 case OP_BIT_XOR: layer_xor(bits | mask); break;
307 case OP_BIT_SET: layer_and(mask); layer_or(bits); break;
312 #ifndef NO_ACTION_TAPPING
314 case ACT_LAYER_TAP_EXT:
315 switch (action.layer_tap.code) {
317 /* layer On/Off with modifiers(left only) */
319 layer_on(action.layer_tap.val);
320 register_mods(action.layer_tap.code & 0x0f);
322 layer_off(action.layer_tap.val);
323 unregister_mods(action.layer_tap.code & 0x0f);
329 if (tap_count < TAPPING_TOGGLE) {
330 layer_invert(action.layer_tap.val);
333 if (tap_count <= TAPPING_TOGGLE) {
334 layer_invert(action.layer_tap.val);
339 event.pressed ? layer_on(action.layer_tap.val) :
340 layer_off(action.layer_tap.val);
343 event.pressed ? layer_off(action.layer_tap.val) :
344 layer_on(action.layer_tap.val);
347 event.pressed ? layer_move(action.layer_tap.val) :
350 #ifndef NO_ACTION_ONESHOT
353 #if defined(ONESHOT_TAP_TOGGLE) && ONESHOT_TAP_TOGGLE > 1
354 do_release_oneshot = false;
356 del_mods(get_oneshot_locked_mods());
357 if (get_oneshot_layer_state() == ONESHOT_TOGGLED) {
358 reset_oneshot_layer();
359 layer_off(action.layer_tap.val);
361 } else if (tap_count < ONESHOT_TAP_TOGGLE) {
362 layer_on(action.layer_tap.val);
363 set_oneshot_layer(action.layer_tap.val, ONESHOT_START);
366 add_mods(get_oneshot_locked_mods());
367 if (tap_count >= ONESHOT_TAP_TOGGLE) {
368 reset_oneshot_layer();
369 clear_oneshot_locked_mods();
370 set_oneshot_layer(action.layer_tap.val, ONESHOT_TOGGLED);
372 clear_oneshot_layer_state(ONESHOT_PRESSED);
377 layer_on(action.layer_tap.val);
378 set_oneshot_layer(action.layer_tap.val, ONESHOT_START);
380 clear_oneshot_layer_state(ONESHOT_PRESSED);
382 clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
392 dprint("KEYMAP_TAP_KEY: Tap: register_code\n");
393 register_code(action.layer_tap.code);
395 dprint("KEYMAP_TAP_KEY: No tap: On on press\n");
396 layer_on(action.layer_tap.val);
400 dprint("KEYMAP_TAP_KEY: Tap: unregister_code\n");
401 unregister_code(action.layer_tap.code);
403 dprint("KEYMAP_TAP_KEY: No tap: Off on release\n");
404 layer_off(action.layer_tap.val);
413 #ifndef NO_ACTION_MACRO
415 action_macro_play(action_get_macro(record, action.func.id, action.func.opt));
418 #ifdef BACKLIGHT_ENABLE
420 if (!event.pressed) {
421 switch (action.backlight.opt) {
422 case BACKLIGHT_INCREASE:
423 backlight_increase();
425 case BACKLIGHT_DECREASE:
426 backlight_decrease();
428 case BACKLIGHT_TOGGLE:
434 case BACKLIGHT_LEVEL:
435 backlight_level(action.backlight.level);
443 #ifndef NO_ACTION_FUNCTION
445 action_function(record, action.func.id, action.func.opt);
452 #ifndef NO_ACTION_ONESHOT
453 /* Because we switch layers after a oneshot event, we need to release the
454 * key before we leave the layer or no key up event will be generated.
456 if (do_release_oneshot && !(get_oneshot_layer_state() & ONESHOT_PRESSED ) ) {
457 record->event.pressed = false;
458 layer_on(get_oneshot_layer());
459 process_record(record);
460 layer_off(get_oneshot_layer());
469 * Utilities for actions.
471 void register_code(uint8_t code)
477 #ifdef LOCKING_SUPPORT_ENABLE
478 else if (KC_LOCKING_CAPS == code) {
479 #ifdef LOCKING_RESYNC_ENABLE
480 // Resync: ignore if caps lock already is on
481 if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) return;
483 add_key(KC_CAPSLOCK);
484 send_keyboard_report();
485 del_key(KC_CAPSLOCK);
486 send_keyboard_report();
489 else if (KC_LOCKING_NUM == code) {
490 #ifdef LOCKING_RESYNC_ENABLE
491 if (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK)) return;
494 send_keyboard_report();
496 send_keyboard_report();
499 else if (KC_LOCKING_SCROLL == code) {
500 #ifdef LOCKING_RESYNC_ENABLE
501 if (host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK)) return;
503 add_key(KC_SCROLLLOCK);
504 send_keyboard_report();
505 del_key(KC_SCROLLLOCK);
506 send_keyboard_report();
510 else if IS_KEY(code) {
511 // TODO: should push command_proc out of this block?
512 if (command_proc(code)) return;
514 #ifndef NO_ACTION_ONESHOT
516 if (oneshot_state.mods && !oneshot_state.disabled) {
517 uint8_t tmp_mods = get_mods();
518 add_mods(oneshot_state.mods);
521 send_keyboard_report();
524 send_keyboard_report();
531 send_keyboard_report();
534 else if IS_MOD(code) {
535 add_mods(MOD_BIT(code));
536 send_keyboard_report();
538 else if IS_SYSTEM(code) {
539 host_system_send(KEYCODE2SYSTEM(code));
541 else if IS_CONSUMER(code) {
542 host_consumer_send(KEYCODE2CONSUMER(code));
546 void unregister_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 off
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) {
587 send_keyboard_report();
589 else if IS_MOD(code) {
590 del_mods(MOD_BIT(code));
591 send_keyboard_report();
593 else if IS_SYSTEM(code) {
596 else if IS_CONSUMER(code) {
597 host_consumer_send(0);
601 void register_mods(uint8_t mods)
605 send_keyboard_report();
609 void unregister_mods(uint8_t mods)
613 send_keyboard_report();
617 void clear_keyboard(void)
620 clear_keyboard_but_mods();
623 void clear_keyboard_but_mods(void)
628 send_keyboard_report();
629 #ifdef MOUSEKEY_ENABLE
633 #ifdef EXTRAKEY_ENABLE
635 host_consumer_send(0);
639 bool is_tap_key(keypos_t key)
641 action_t action = layer_switch_get_action(key);
643 switch (action.kind.id) {
647 case ACT_LAYER_TAP_EXT:
648 switch (action.layer_tap.code) {
657 if (action.func.opt & FUNC_TAP) { return true; }
667 void debug_event(keyevent_t event)
669 dprintf("%04X%c(%u)", (event.key.row<<8 | event.key.col), (event.pressed ? 'd' : 'u'), event.time);
672 void debug_record(keyrecord_t record)
674 debug_event(record.event);
675 #ifndef NO_ACTION_TAPPING
676 dprintf(":%u%c", record.tap.count, (record.tap.interrupted ? '-' : ' '));
680 void debug_action(action_t action)
682 switch (action.kind.id) {
683 case ACT_LMODS: dprint("ACT_LMODS"); break;
684 case ACT_RMODS: dprint("ACT_RMODS"); break;
685 case ACT_LMODS_TAP: dprint("ACT_LMODS_TAP"); break;
686 case ACT_RMODS_TAP: dprint("ACT_RMODS_TAP"); break;
687 case ACT_USAGE: dprint("ACT_USAGE"); break;
688 case ACT_MOUSEKEY: dprint("ACT_MOUSEKEY"); break;
689 case ACT_LAYER: dprint("ACT_LAYER"); break;
690 case ACT_LAYER_TAP: dprint("ACT_LAYER_TAP"); break;
691 case ACT_LAYER_TAP_EXT: dprint("ACT_LAYER_TAP_EXT"); break;
692 case ACT_MACRO: dprint("ACT_MACRO"); break;
693 case ACT_COMMAND: dprint("ACT_COMMAND"); break;
694 case ACT_FUNCTION: dprint("ACT_FUNCTION"); break;
695 default: dprint("UNKNOWN"); break;
697 dprintf("[%X:%02X]", action.kind.param>>8, action.kind.param&0xff);