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/>.
24 #include "layer_switch.h"
25 #include "action_tapping.h"
26 #include "action_oneshot.h"
27 #include "action_macro.h"
31 void action_exec(keyevent_t event)
33 if (!IS_NOEVENT(event)) {
34 debug("\n---- action_exec: start -----\n");
35 debug("EVENT: "); debug_event(event); debug("\n");
38 keyrecord_t record = { .event = event };
40 #ifndef NO_ACTION_TAPPING
41 action_tapping_process(record);
43 process_action(&record);
44 if (!IS_NOEVENT(record.event)) {
45 debug("processed: "); debug_record(record); debug("\n");
50 void process_action(keyrecord_t *record)
52 keyevent_t event = record->event;
53 uint8_t tap_count = record->tap.count;
55 if (IS_NOEVENT(event)) { return; }
57 action_t action = layer_switch_get_action(event.key);
58 debug("ACTION: "); debug_action(action);
59 debug(" overlays: "); overlay_debug();
60 debug(" keymaps: "); keymap_debug();
61 debug(" default_layer: "); debug_dec(default_layer); debug("\n");
63 switch (action.kind.id) {
68 uint8_t mods = (action.kind.id == ACT_LMODS) ? action.key.mods :
71 uint8_t tmp_mods = host_get_mods();
74 host_send_keyboard_report();
76 register_code(action.key.code);
77 if (mods && action.key.code) {
78 host_set_mods(tmp_mods);
79 host_send_keyboard_report();
82 if (mods && !action.key.code) {
84 host_send_keyboard_report();
86 unregister_code(action.key.code);
90 #ifndef NO_ACTION_TAPPING
94 uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods :
96 switch (action.layer.code) {
97 #ifndef NO_ACTION_ONESHOT
101 if (tap_count == 0) {
102 debug("MODS_TAP: Oneshot: add_mods\n");
105 else if (tap_count == 1) {
106 debug("MODS_TAP: Oneshot: start\n");
109 else if (tap_count == TAPPING_TOGGLE) {
110 debug("MODS_TAP: Oneshot: toggle\n");
114 debug("MODS_TAP: Oneshot: cancel&add_mods\n");
115 // double tap cancels oneshot and works as normal modifier.
120 if (tap_count == 0) {
121 debug("MODS_TAP: Oneshot: cancel/del_mods\n");
122 // cancel oneshot on hold
126 else if (tap_count == 1) {
127 debug("MODS_TAP: Oneshot: del_mods\n");
132 debug("MODS_TAP: Oneshot: del_mods\n");
142 if (record->tap.interrupted) {
143 debug("MODS_TAP: Tap: Cancel: add_mods\n");
144 // ad hoc: set 0 to cancel tap
145 record->tap.count = 0;
148 debug("MODS_TAP: Tap: register_code\n");
149 register_code(action.key.code);
152 debug("MODS_TAP: No tap: add_mods\n");
157 debug("MODS_TAP: Tap: unregister_code\n");
158 unregister_code(action.key.code);
160 debug("MODS_TAP: No tap: add_mods\n");
169 #ifdef EXTRAKEY_ENABLE
170 /* other HID usage */
172 switch (action.usage.page) {
175 host_system_send(action.usage.code);
182 host_consumer_send(action.usage.code);
184 host_consumer_send(0);
190 #ifdef MOUSEKEY_ENABLE
194 mousekey_on(action.key.code);
197 mousekey_off(action.key.code);
202 #ifndef NO_ACTION_KEYMAP
204 switch (action.layer.code) {
207 switch (action.layer.val & 0x03) {
220 if (!event.pressed) {
229 /* NOTE: 4-7 rserved */
232 /* Keymap Reset default layer */
233 case (OP_RESET | ON_PRESS):
235 default_layer_set(action.layer.val);
238 case (OP_RESET | ON_RELEASE):
239 if (!event.pressed) {
240 default_layer_set(action.layer.val);
243 case (OP_RESET | ON_BOTH):
244 default_layer_set(action.layer.val);
247 /* Keymap Bit invert */
249 /* with tap toggle */
251 if (tap_count < TAPPING_TOGGLE) {
252 debug("KEYMAP_INV: tap toggle(press).\n");
253 keymap_invert(action.layer.val);
256 if (tap_count <= TAPPING_TOGGLE) {
257 debug("KEYMAP_INV: tap toggle(release).\n");
258 keymap_invert(action.layer.val);
262 case (OP_INV | ON_PRESS):
264 keymap_invert(action.layer.val);
267 case (OP_INV | ON_RELEASE):
268 if (!event.pressed) {
269 keymap_invert(action.layer.val);
272 case (OP_INV | ON_BOTH):
273 keymap_invert(action.layer.val);
279 keymap_on(action.layer.val);
281 keymap_off(action.layer.val);
284 case (OP_ON | ON_PRESS):
286 keymap_on(action.layer.val);
289 case (OP_ON | ON_RELEASE):
290 if (!event.pressed) {
291 keymap_on(action.layer.val);
294 case (OP_ON | ON_BOTH):
295 keymap_on(action.layer.val);
301 keymap_off(action.layer.val);
303 keymap_on(action.layer.val);
306 case (OP_OFF | ON_PRESS):
308 keymap_off(action.layer.val);
311 case (OP_OFF | ON_RELEASE):
312 if (!event.pressed) {
313 keymap_off(action.layer.val);
316 case (OP_OFF | ON_BOTH):
317 keymap_off(action.layer.val);
323 keymap_set(action.layer.val);
328 case (OP_SET | ON_PRESS):
330 keymap_set(action.layer.val);
333 case (OP_SET | ON_RELEASE):
334 if (!event.pressed) {
335 keymap_set(action.layer.val);
338 case (OP_SET | ON_BOTH):
339 keymap_set(action.layer.val);
342 /* Keymap Bit invert with tap key */
346 debug("KEYMAP_TAP_KEY: Tap: register_code\n");
347 register_code(action.layer.code);
349 debug("KEYMAP_TAP_KEY: No tap: On on press\n");
350 keymap_on(action.layer.val);
354 debug("KEYMAP_TAP_KEY: Tap: unregister_code\n");
355 unregister_code(action.layer.code);
357 debug("KEYMAP_TAP_KEY: No tap: Off on release\n");
358 keymap_off(action.layer.val);
365 #ifndef NO_ACTION_OVERLAY
367 switch (action.layer.code) {
368 // Overlay Invert bit4
370 if (action.layer.val == 0) {
371 // NOTE: reserved for future use
374 overlay_set(overlay_stat ^ action.layer.val);
378 if (action.layer.val == 0) {
380 if (event.pressed) overlay_clear();
382 overlay_set(overlay_stat ^ action.layer.val<<4);
386 if (action.layer.val == 0) {
388 if (!event.pressed) overlay_clear();
390 overlay_set(overlay_stat ^ action.layer.val<<8);
394 if (action.layer.val == 0) {
398 overlay_set(overlay_stat ^ action.layer.val<<12);
402 /* Overlay Bit invert */
404 /* with tap toggle */
406 if (tap_count < TAPPING_TOGGLE) {
407 debug("OVERLAY_INV: tap toggle(press).\n");
408 overlay_invert(action.layer.val);
411 if (tap_count <= TAPPING_TOGGLE) {
412 debug("OVERLAY_INV: tap toggle(release).\n");
413 overlay_invert(action.layer.val);
417 case (OP_INV | ON_PRESS):
419 overlay_invert(action.layer.val);
422 case (OP_INV | ON_RELEASE):
423 if (!event.pressed) {
424 overlay_invert(action.layer.val);
427 case (OP_INV | ON_BOTH):
428 overlay_invert(action.layer.val);
434 overlay_on(action.layer.val);
436 overlay_off(action.layer.val);
439 case (OP_ON | ON_PRESS):
441 overlay_on(action.layer.val);
444 case (OP_ON | ON_RELEASE):
445 if (!event.pressed) {
446 overlay_on(action.layer.val);
449 case (OP_ON | ON_BOTH):
450 overlay_on(action.layer.val);
453 /* Overlay Bit off */
456 overlay_off(action.layer.val);
458 overlay_on(action.layer.val);
461 case (OP_OFF | ON_PRESS):
463 overlay_off(action.layer.val);
466 case (OP_OFF | ON_RELEASE):
467 if (!event.pressed) {
468 overlay_off(action.layer.val);
471 case (OP_OFF | ON_BOTH):
472 overlay_off(action.layer.val);
475 /* Overlay Bit set */
478 overlay_move(action.layer.val);
483 case (OP_SET | ON_PRESS):
485 overlay_move(action.layer.val);
488 case (OP_SET | ON_RELEASE):
489 if (!event.pressed) {
490 overlay_move(action.layer.val);
493 case (OP_SET | ON_BOTH):
494 overlay_move(action.layer.val);
497 /* Overlay Bit invert with tap key */
501 debug("OVERLAY_TAP_KEY: Tap: register_code\n");
502 register_code(action.layer.code);
504 debug("OVERLAY_TAP_KEY: No tap: On on press\n");
505 overlay_on(action.layer.val);
509 debug("OVERLAY_TAP_KEY: Tap: unregister_code\n");
510 unregister_code(action.layer.code);
512 debug("OVERLAY_TAP_KEY: No tap: Off on release\n");
513 overlay_off(action.layer.val);
521 #ifndef NO_ACTION_MACRO
523 action_macro_play(action_get_macro(record, action.func.id, action.func.opt));
528 #ifndef NO_ACTION_FUNCTION
530 action_function(record, action.func.id, action.func.opt);
542 * Utilities for actions.
544 void register_code(uint8_t code)
549 #ifdef CAPSLOCK_LOCKING_ENABLE
550 else if (KC_LOCKING_CAPS == code) {
551 #ifdef CAPSLOCK_LOCKING_RESYNC_ENABLE
552 // Resync: ignore if caps lock already is on
553 if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) return;
555 host_add_key(KC_CAPSLOCK);
556 host_send_keyboard_report();
557 host_del_key(KC_CAPSLOCK);
558 host_send_keyboard_report();
561 else if IS_KEY(code) {
562 // TODO: should push command_proc out of this block?
563 if (command_proc(code)) return;
565 #ifndef NO_ACTION_ONESHOT
566 if (oneshot_state.mods && !oneshot_state.disabled) {
567 uint8_t tmp_mods = host_get_mods();
568 host_add_mods(oneshot_state.mods);
571 host_send_keyboard_report();
573 host_set_mods(tmp_mods);
579 host_send_keyboard_report();
582 else if IS_MOD(code) {
583 host_add_mods(MOD_BIT(code));
584 host_send_keyboard_report();
588 void unregister_code(uint8_t code)
593 #ifdef CAPSLOCK_LOCKING_ENABLE
594 else if (KC_LOCKING_CAPS == code) {
595 #ifdef CAPSLOCK_LOCKING_RESYNC_ENABLE
596 // Resync: ignore if caps lock already is off
597 if (!(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK))) return;
599 host_add_key(KC_CAPSLOCK);
600 host_send_keyboard_report();
601 host_del_key(KC_CAPSLOCK);
602 host_send_keyboard_report();
605 else if IS_KEY(code) {
607 host_send_keyboard_report();
609 else if IS_MOD(code) {
610 host_del_mods(MOD_BIT(code));
611 host_send_keyboard_report();
615 void add_mods(uint8_t mods)
619 host_send_keyboard_report();
623 void del_mods(uint8_t mods)
627 host_send_keyboard_report();
631 void set_mods(uint8_t mods)
634 host_send_keyboard_report();
637 void clear_keyboard(void)
640 clear_keyboard_but_mods();
643 void clear_keyboard_but_mods(void)
646 host_send_keyboard_report();
647 #ifdef MOUSEKEY_ENABLE
651 #ifdef EXTRAKEY_ENABLE
653 host_consumer_send(0);
657 bool sending_anykey(void)
659 return (host_has_anykey() || host_mouse_in_use() ||
660 host_last_sysytem_report() || host_last_consumer_report());
663 bool is_tap_key(key_t key)
665 action_t action = layer_switch_get_action(key);
667 switch (action.kind.id) {
673 switch (action.layer.code) {
674 case 0x04 ... 0xEF: /* tap key */
682 if (action.func.opt & FUNC_TAP) { return true; }
692 void debug_event(keyevent_t event)
694 debug_hex16((event.key.row<<8) | event.key.col);
695 if (event.pressed) debug("d("); else debug("u(");
696 debug_dec(event.time); debug(")");
699 void debug_record(keyrecord_t record)
701 debug_event(record.event);
702 #ifndef NO_ACTION_TAPPING
703 debug(":"); debug_dec(record.tap.count);
704 if (record.tap.interrupted) debug("-");
708 void debug_action(action_t action)
710 switch (action.kind.id) {
711 case ACT_LMODS: debug("ACT_LMODS"); break;
712 case ACT_RMODS: debug("ACT_RMODS"); break;
713 case ACT_LMODS_TAP: debug("ACT_LMODS_TAP"); break;
714 case ACT_RMODS_TAP: debug("ACT_RMODS_TAP"); break;
715 case ACT_USAGE: debug("ACT_USAGE"); break;
716 case ACT_MOUSEKEY: debug("ACT_MOUSEKEY"); break;
717 case ACT_KEYMAP: debug("ACT_KEYMAP"); break;
718 case ACT_OVERLAY: debug("ACT_OVERLAY"); break;
719 case ACT_MACRO: debug("ACT_MACRO"); break;
720 case ACT_COMMAND: debug("ACT_COMMAND"); break;
721 case ACT_FUNCTION: debug("ACT_FUNCTION"); break;
722 default: debug("UNKNOWN"); break;
725 debug_hex4(action.kind.param>>8);
727 debug_hex8(action.kind.param & 0xff);