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/>.
27 #include "layer_switch.h"
28 #include "action_oneshot.h"
29 #include "action_macro.h"
33 static void process_action(keyrecord_t *record);
34 static void debug_event(keyevent_t event);
35 static void debug_record(keyrecord_t record);
36 static void debug_action(action_t action);
38 #ifndef NO_ACTION_TAPPING
42 /* period of tapping(ms) */
44 #define TAPPING_TERM 200
47 /* tap count needed for toggling a feature */
48 #ifndef TAPPING_TOGGLE
49 #define TAPPING_TOGGLE 5
52 /* stores a key event of current tap. */
53 static keyrecord_t tapping_key = {};
55 #define IS_TAPPING() !IS_NOEVENT(tapping_key.event)
56 #define IS_TAPPING_PRESSED() (IS_TAPPING() && tapping_key.event.pressed)
57 #define IS_TAPPING_RELEASED() (IS_TAPPING() && !tapping_key.event.pressed)
58 #define IS_TAPPING_KEY(k) (IS_TAPPING() && KEYEQ(tapping_key.event.key, (k)))
59 #define WITHIN_TAPPING_TERM(e) (TIMER_DIFF_16(e.time, tapping_key.event.time) < TAPPING_TERM)
65 * stores key events waiting for settling current tap.
67 #define WAITING_BUFFER_SIZE 8
68 static keyrecord_t waiting_buffer[WAITING_BUFFER_SIZE] = {};
69 /* point to empty cell to enq */
70 static uint8_t waiting_buffer_head = 0;
71 /* point to the oldest data cell to deq */
72 static uint8_t waiting_buffer_tail = 0;
75 static bool process_tapping(keyrecord_t *record);
76 static bool waiting_buffer_enq(keyrecord_t record);
77 static void waiting_buffer_clear(void);
78 #if TAPPING_TERM >= 500
79 static bool waiting_buffer_typed(keyevent_t event);
81 static void waiting_buffer_scan_tap(void);
82 static void debug_tapping_key(void);
83 static void debug_waiting_buffer(void);
88 void action_exec(keyevent_t event)
90 if (!IS_NOEVENT(event)) {
91 debug("\n---- action_exec: start -----\n");
92 debug("EVENT: "); debug_event(event); debug("\n");
95 keyrecord_t record = { .event = event };
97 #ifndef NO_ACTION_TAPPING
98 if (process_tapping(&record)) {
99 if (!IS_NOEVENT(record.event)) {
100 debug("processed: "); debug_record(record); debug("\n");
103 if (!waiting_buffer_enq(record)) {
104 // clear all in case of overflow.
105 debug("OVERFLOW: CLEAR ALL STATES\n");
107 waiting_buffer_clear();
108 tapping_key = (keyrecord_t){};
112 // process waiting_buffer
113 if (!IS_NOEVENT(event) && waiting_buffer_head != waiting_buffer_tail) {
114 debug("---- action_exec: process waiting_buffer -----\n");
116 for (; waiting_buffer_tail != waiting_buffer_head; waiting_buffer_tail = (waiting_buffer_tail + 1) % WAITING_BUFFER_SIZE) {
117 if (process_tapping(&waiting_buffer[waiting_buffer_tail])) {
118 debug("processed: waiting_buffer["); debug_dec(waiting_buffer_tail); debug("] = ");
119 debug_record(waiting_buffer[waiting_buffer_tail]); debug("\n\n");
124 if (!IS_NOEVENT(event)) {
128 process_action(&record);
129 if (!IS_NOEVENT(record.event)) {
130 debug("processed: "); debug_record(record); debug("\n");
135 static void process_action(keyrecord_t *record)
137 keyevent_t event = record->event;
138 uint8_t tap_count = record->tap.count;
140 if (IS_NOEVENT(event)) { return; }
142 action_t action = layer_switch_get_action(event.key);
143 debug("ACTION: "); debug_action(action);
144 debug(" overlays: "); overlay_debug();
145 debug(" keymaps: "); keymap_debug();
146 debug(" default_layer: "); debug_dec(default_layer); debug("\n");
148 switch (action.kind.id) {
153 uint8_t mods = (action.kind.id == ACT_LMODS) ? action.key.mods :
156 uint8_t tmp_mods = host_get_mods();
159 host_send_keyboard_report();
161 register_code(action.key.code);
162 if (mods && action.key.code) {
163 host_set_mods(tmp_mods);
164 host_send_keyboard_report();
167 if (mods && !action.key.code) {
169 host_send_keyboard_report();
171 unregister_code(action.key.code);
175 #ifndef NO_ACTION_TAPPING
179 uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods :
181 switch (action.layer.code) {
182 #ifndef NO_ACTION_ONESHOT
186 if (tap_count == 0) {
187 debug("MODS_TAP: Oneshot: add_mods\n");
190 else if (tap_count == 1) {
191 debug("MODS_TAP: Oneshot: start\n");
194 else if (tap_count == TAPPING_TOGGLE) {
195 debug("MODS_TAP: Oneshot: toggle\n");
199 debug("MODS_TAP: Oneshot: cancel&add_mods\n");
200 // double tap cancels oneshot and works as normal modifier.
205 if (tap_count == 0) {
206 debug("MODS_TAP: Oneshot: cancel/del_mods\n");
207 // cancel oneshot on hold
211 else if (tap_count == 1) {
212 debug("MODS_TAP: Oneshot: del_mods\n");
217 debug("MODS_TAP: Oneshot: del_mods\n");
227 if (waiting_buffer_has_anykey_pressed()) {
228 debug("MODS_TAP: Tap: Cancel: add_mods\n");
229 // ad hoc: set 0 to cancel tap
230 record->tap.count = 0;
233 debug("MODS_TAP: Tap: register_code\n");
234 register_code(action.key.code);
237 debug("MODS_TAP: No tap: add_mods\n");
242 debug("MODS_TAP: Tap: unregister_code\n");
243 unregister_code(action.key.code);
245 debug("MODS_TAP: No tap: add_mods\n");
254 #ifdef EXTRAKEY_ENABLE
255 /* other HID usage */
257 switch (action.usage.page) {
260 host_system_send(action.usage.code);
267 host_consumer_send(action.usage.code);
269 host_consumer_send(0);
275 #ifdef MOUSEKEY_ENABLE
279 mousekey_on(action.key.code);
282 mousekey_off(action.key.code);
287 #ifndef NO_ACTION_KEYMAP
289 switch (action.layer.code) {
292 switch (action.layer.val & 0x03) {
305 if (!event.pressed) {
314 /* NOTE: 4-7 rserved */
317 /* Keymap Reset default layer */
318 case (OP_RESET | ON_PRESS):
320 default_layer_set(action.layer.val);
323 case (OP_RESET | ON_RELEASE):
324 if (!event.pressed) {
325 default_layer_set(action.layer.val);
328 case (OP_RESET | ON_BOTH):
329 default_layer_set(action.layer.val);
332 /* Keymap Bit invert */
334 /* with tap toggle */
336 if (tap_count < TAPPING_TOGGLE) {
337 debug("KEYMAP_INV: tap toggle(press).\n");
338 keymap_invert(action.layer.val);
341 if (tap_count <= TAPPING_TOGGLE) {
342 debug("KEYMAP_INV: tap toggle(release).\n");
343 keymap_invert(action.layer.val);
347 case (OP_INV | ON_PRESS):
349 keymap_invert(action.layer.val);
352 case (OP_INV | ON_RELEASE):
353 if (!event.pressed) {
354 keymap_invert(action.layer.val);
357 case (OP_INV | ON_BOTH):
358 keymap_invert(action.layer.val);
364 keymap_on(action.layer.val);
366 keymap_off(action.layer.val);
369 case (OP_ON | ON_PRESS):
371 keymap_on(action.layer.val);
374 case (OP_ON | ON_RELEASE):
375 if (!event.pressed) {
376 keymap_on(action.layer.val);
379 case (OP_ON | ON_BOTH):
380 keymap_on(action.layer.val);
386 keymap_off(action.layer.val);
388 keymap_on(action.layer.val);
391 case (OP_OFF | ON_PRESS):
393 keymap_off(action.layer.val);
396 case (OP_OFF | ON_RELEASE):
397 if (!event.pressed) {
398 keymap_off(action.layer.val);
401 case (OP_OFF | ON_BOTH):
402 keymap_off(action.layer.val);
408 keymap_set(action.layer.val);
413 case (OP_SET | ON_PRESS):
415 keymap_set(action.layer.val);
418 case (OP_SET | ON_RELEASE):
419 if (!event.pressed) {
420 keymap_set(action.layer.val);
423 case (OP_SET | ON_BOTH):
424 keymap_set(action.layer.val);
427 /* Keymap Bit invert with tap key */
431 debug("KEYMAP_TAP_KEY: Tap: register_code\n");
432 register_code(action.layer.code);
434 debug("KEYMAP_TAP_KEY: No tap: On on press\n");
435 keymap_on(action.layer.val);
439 debug("KEYMAP_TAP_KEY: Tap: unregister_code\n");
440 unregister_code(action.layer.code);
442 debug("KEYMAP_TAP_KEY: No tap: Off on release\n");
443 keymap_off(action.layer.val);
450 #ifndef NO_ACTION_OVERLAY
452 switch (action.layer.code) {
453 // Overlay Invert bit4
455 if (action.layer.val == 0) {
456 // NOTE: reserved for future use
459 overlay_set(overlay_stat ^ action.layer.val);
463 if (action.layer.val == 0) {
465 if (event.pressed) overlay_clear();
467 overlay_set(overlay_stat ^ action.layer.val<<4);
471 if (action.layer.val == 0) {
473 if (!event.pressed) overlay_clear();
475 overlay_set(overlay_stat ^ action.layer.val<<8);
479 if (action.layer.val == 0) {
483 overlay_set(overlay_stat ^ action.layer.val<<12);
487 /* Overlay Bit invert */
489 /* with tap toggle */
491 if (tap_count < TAPPING_TOGGLE) {
492 debug("OVERLAY_INV: tap toggle(press).\n");
493 overlay_invert(action.layer.val);
496 if (tap_count <= TAPPING_TOGGLE) {
497 debug("OVERLAY_INV: tap toggle(release).\n");
498 overlay_invert(action.layer.val);
502 case (OP_INV | ON_PRESS):
504 overlay_invert(action.layer.val);
507 case (OP_INV | ON_RELEASE):
508 if (!event.pressed) {
509 overlay_invert(action.layer.val);
512 case (OP_INV | ON_BOTH):
513 overlay_invert(action.layer.val);
519 overlay_on(action.layer.val);
521 overlay_off(action.layer.val);
524 case (OP_ON | ON_PRESS):
526 overlay_on(action.layer.val);
529 case (OP_ON | ON_RELEASE):
530 if (!event.pressed) {
531 overlay_on(action.layer.val);
534 case (OP_ON | ON_BOTH):
535 overlay_on(action.layer.val);
538 /* Overlay Bit off */
541 overlay_off(action.layer.val);
543 overlay_on(action.layer.val);
546 case (OP_OFF | ON_PRESS):
548 overlay_off(action.layer.val);
551 case (OP_OFF | ON_RELEASE):
552 if (!event.pressed) {
553 overlay_off(action.layer.val);
556 case (OP_OFF | ON_BOTH):
557 overlay_off(action.layer.val);
560 /* Overlay Bit set */
563 overlay_move(action.layer.val);
568 case (OP_SET | ON_PRESS):
570 overlay_move(action.layer.val);
573 case (OP_SET | ON_RELEASE):
574 if (!event.pressed) {
575 overlay_move(action.layer.val);
578 case (OP_SET | ON_BOTH):
579 overlay_move(action.layer.val);
582 /* Overlay Bit invert with tap key */
586 debug("OVERLAY_TAP_KEY: Tap: register_code\n");
587 register_code(action.layer.code);
589 debug("OVERLAY_TAP_KEY: No tap: On on press\n");
590 overlay_on(action.layer.val);
594 debug("OVERLAY_TAP_KEY: Tap: unregister_code\n");
595 unregister_code(action.layer.code);
597 debug("OVERLAY_TAP_KEY: No tap: Off on release\n");
598 overlay_off(action.layer.val);
606 #ifndef NO_ACTION_MACRO
608 action_macro_play(action_get_macro(record, action.func.id, action.func.opt));
613 #ifndef NO_ACTION_FUNCTION
615 action_function(record, action.func.id, action.func.opt);
627 * Utilities for actions.
629 void register_code(uint8_t code)
634 #ifdef CAPSLOCK_LOCKING_ENABLE
635 else if (KC_LOCKING_CAPS == code) {
636 #ifdef CAPSLOCK_LOCKING_RESYNC_ENABLE
637 // Resync: ignore if caps lock already is on
638 if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) return;
640 host_add_key(KC_CAPSLOCK);
641 host_send_keyboard_report();
642 host_del_key(KC_CAPSLOCK);
643 host_send_keyboard_report();
646 else if IS_KEY(code) {
647 // TODO: should push command_proc out of this block?
648 if (command_proc(code)) return;
650 #ifndef NO_ACTION_ONESHOT
651 if (oneshot_state.mods && !oneshot_state.disabled) {
652 uint8_t tmp_mods = host_get_mods();
653 host_add_mods(oneshot_state.mods);
656 host_send_keyboard_report();
658 host_set_mods(tmp_mods);
664 host_send_keyboard_report();
667 else if IS_MOD(code) {
668 host_add_mods(MOD_BIT(code));
669 host_send_keyboard_report();
673 void unregister_code(uint8_t code)
678 #ifdef CAPSLOCK_LOCKING_ENABLE
679 else if (KC_LOCKING_CAPS == code) {
680 #ifdef CAPSLOCK_LOCKING_RESYNC_ENABLE
681 // Resync: ignore if caps lock already is off
682 if (!(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK))) return;
684 host_add_key(KC_CAPSLOCK);
685 host_send_keyboard_report();
686 host_del_key(KC_CAPSLOCK);
687 host_send_keyboard_report();
690 else if IS_KEY(code) {
692 host_send_keyboard_report();
694 else if IS_MOD(code) {
695 host_del_mods(MOD_BIT(code));
696 host_send_keyboard_report();
700 void add_mods(uint8_t mods)
704 host_send_keyboard_report();
708 void del_mods(uint8_t mods)
712 host_send_keyboard_report();
716 void set_mods(uint8_t mods)
719 host_send_keyboard_report();
722 void clear_keyboard(void)
725 clear_keyboard_but_mods();
728 void clear_keyboard_but_mods(void)
731 host_send_keyboard_report();
732 #ifdef MOUSEKEY_ENABLE
736 #ifdef EXTRAKEY_ENABLE
738 host_consumer_send(0);
742 bool sending_anykey(void)
744 return (host_has_anykey() || host_mouse_in_use() ||
745 host_last_sysytem_report() || host_last_consumer_report());
748 bool is_tap_key(key_t key)
750 action_t action = layer_switch_get_action(key);
752 switch (action.kind.id) {
758 switch (action.layer.code) {
759 case 0x04 ... 0xEF: /* tap key */
767 if (action.func.opt & FUNC_TAP) { return true; }
777 static void debug_event(keyevent_t event)
779 debug_hex16((event.key.row<<8) | event.key.col);
780 if (event.pressed) debug("d("); else debug("u(");
781 debug_dec(event.time); debug(")");
784 static void debug_record(keyrecord_t record)
786 debug_event(record.event); debug(":"); debug_dec(record.tap.count);
787 if (record.tap.interrupted) debug("-");
790 static void debug_action(action_t action)
792 switch (action.kind.id) {
793 case ACT_LMODS: debug("ACT_LMODS"); break;
794 case ACT_RMODS: debug("ACT_RMODS"); break;
795 case ACT_LMODS_TAP: debug("ACT_LMODS_TAP"); break;
796 case ACT_RMODS_TAP: debug("ACT_RMODS_TAP"); break;
797 case ACT_USAGE: debug("ACT_USAGE"); break;
798 case ACT_MOUSEKEY: debug("ACT_MOUSEKEY"); break;
799 case ACT_KEYMAP: debug("ACT_KEYMAP"); break;
800 case ACT_OVERLAY: debug("ACT_OVERLAY"); break;
801 case ACT_MACRO: debug("ACT_MACRO"); break;
802 case ACT_COMMAND: debug("ACT_COMMAND"); break;
803 case ACT_FUNCTION: debug("ACT_FUNCTION"); break;
804 default: debug("UNKNOWN"); break;
807 debug_hex4(action.kind.param>>8);
809 debug_hex8(action.kind.param & 0xff);
815 #ifndef NO_ACTION_TAPPING
818 * Rule: Tap key is typed(pressed and released) within TAPPING_TERM.
819 * (without interfering by typing other key)
821 /* return true when key event is processed or consumed. */
822 static bool process_tapping(keyrecord_t *keyp)
824 keyevent_t event = keyp->event;
827 if (IS_TAPPING_PRESSED()) {
828 if (WITHIN_TAPPING_TERM(event)) {
829 if (tapping_key.tap.count == 0) {
830 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
832 debug("Tapping: First tap(0->1).\n");
833 tapping_key.tap.count = 1;
834 tapping_key.tap.interrupted = (waiting_buffer_has_anykey_pressed() ? true : false);
836 process_action(&tapping_key);
839 keyp->tap = tapping_key.tap;
842 #if TAPPING_TERM >= 500
843 /* This can prevent from typing some tap keys in a row at a time. */
844 else if (!event.pressed && waiting_buffer_typed(event)) {
845 // other key typed. not tap.
846 debug("Tapping: End. No tap. Interfered by typing key\n");
847 process_action(&tapping_key);
848 tapping_key = (keyrecord_t){};
856 // other key events shall be enq'd till tapping state settles.
862 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
863 debug("Tapping: Tap release("); debug_dec(tapping_key.tap.count); debug(")\n");
864 keyp->tap = tapping_key.tap;
865 process_action(keyp);
870 else if (is_tap_key(keyp->event.key) && event.pressed) {
871 if (tapping_key.tap.count > 1) {
872 debug("Tapping: Start new tap with releasing last tap(>1).\n");
874 process_action(&(keyrecord_t){
875 .tap = tapping_key.tap,
876 .event.key = tapping_key.event.key,
877 .event.time = event.time,
878 .event.pressed = false
881 debug("Tapping: Start while last tap(1).\n");
884 waiting_buffer_scan_tap();
889 if (!IS_NOEVENT(keyp->event)) {
890 debug("Tapping: key event while last tap(>0).\n");
892 process_action(keyp);
897 // after TAPPING_TERM
899 if (tapping_key.tap.count == 0) {
900 debug("Tapping: End. Timeout. Not tap(0): ");
901 debug_event(event); debug("\n");
902 process_action(&tapping_key);
903 tapping_key = (keyrecord_t){};
907 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
908 debug("Tapping: End. last timeout tap release(>0).");
909 keyp->tap = tapping_key.tap;
910 process_action(keyp);
911 tapping_key = (keyrecord_t){};
914 else if (is_tap_key(keyp->event.key) && event.pressed) {
915 if (tapping_key.tap.count > 1) {
916 debug("Tapping: Start new tap with releasing last timeout tap(>1).\n");
918 process_action(&(keyrecord_t){
919 .tap = tapping_key.tap,
920 .event.key = tapping_key.event.key,
921 .event.time = event.time,
922 .event.pressed = false
925 debug("Tapping: Start while last timeout tap(1).\n");
928 waiting_buffer_scan_tap();
933 if (!IS_NOEVENT(keyp->event)) {
934 debug("Tapping: key event while last timeout tap(>0).\n");
936 process_action(keyp);
941 } else if (IS_TAPPING_RELEASED()) {
942 if (WITHIN_TAPPING_TERM(event)) {
943 if (tapping_key.tap.count > 0 && IS_TAPPING_KEY(event.key) && event.pressed) {
945 keyp->tap = tapping_key.tap;
946 keyp->tap.count += 1;
947 debug("Tapping: Tap press("); debug_dec(keyp->tap.count); debug(")\n");
948 process_action(keyp);
952 } else if (event.pressed && is_tap_key(event.key)) {
953 // Sequential tap can be interfered with other tap key.
954 debug("Tapping: Start with interfering other tap.\n");
956 waiting_buffer_scan_tap();
960 if (!IS_NOEVENT(keyp->event)) debug("Tapping: other key just after tap.\n");
961 process_action(keyp);
965 // timeout. no sequential tap.
966 debug("Tapping: End(Timeout after releasing last tap): ");
967 debug_event(event); debug("\n");
968 tapping_key = (keyrecord_t){};
973 // not tapping satate
975 if (event.pressed && is_tap_key(event.key)) {
976 debug("Tapping: Start(Press tap key).\n");
978 waiting_buffer_scan_tap();
982 process_action(keyp);
992 static bool waiting_buffer_enq(keyrecord_t record)
994 if (IS_NOEVENT(record.event)) {
998 if ((waiting_buffer_head + 1) % WAITING_BUFFER_SIZE == waiting_buffer_tail) {
999 debug("waiting_buffer_enq: Over flow.\n");
1003 waiting_buffer[waiting_buffer_head] = record;
1004 waiting_buffer_head = (waiting_buffer_head + 1) % WAITING_BUFFER_SIZE;
1006 debug("waiting_buffer_enq: "); debug_waiting_buffer();
1010 static void waiting_buffer_clear(void)
1012 waiting_buffer_head = 0;
1013 waiting_buffer_tail = 0;
1016 #if TAPPING_TERM >= 500
1017 static bool waiting_buffer_typed(keyevent_t event)
1019 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
1020 if (KEYEQ(event.key, waiting_buffer[i].event.key) && event.pressed != waiting_buffer[i].event.pressed) {
1028 bool waiting_buffer_has_anykey_pressed(void)
1030 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
1031 if (waiting_buffer[i].event.pressed) return true;
1035 /* scan buffer for tapping */
1036 static void waiting_buffer_scan_tap(void)
1038 // tapping already is settled
1039 if (tapping_key.tap.count > 0) return;
1040 // invalid state: tapping_key released && tap.count == 0
1041 if (!tapping_key.event.pressed) return;
1043 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
1044 if (IS_TAPPING_KEY(waiting_buffer[i].event.key) &&
1045 !waiting_buffer[i].event.pressed &&
1046 WITHIN_TAPPING_TERM(waiting_buffer[i].event)) {
1047 tapping_key.tap.count = 1;
1048 waiting_buffer[i].tap.count = 1;
1049 process_action(&tapping_key);
1051 debug("waiting_buffer_scan_tap: found at ["); debug_dec(i); debug("]\n");
1052 debug_waiting_buffer();
1062 static void debug_tapping_key(void)
1064 debug("TAPPING_KEY="); debug_record(tapping_key); debug("\n");
1067 static void debug_waiting_buffer(void)
1070 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
1071 debug("["); debug_dec(i); debug("]="); debug_record(waiting_buffer[i]); debug(" ");