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_macro.h"
32 static void process_action(keyrecord_t *record);
33 static bool process_tapping(keyrecord_t *record);
34 static void waiting_buffer_scan_tap(void);
36 static void debug_event(keyevent_t event);
37 static void debug_record(keyrecord_t record);
38 static void debug_action(action_t action);
39 static void debug_tapping_key(void);
40 static void debug_waiting_buffer(void);
46 /* period of tapping(ms) */
48 #define TAPPING_TERM 200
51 /* tap count needed for toggling a feature */
52 #ifndef TAPPING_TOGGLE
53 #define TAPPING_TOGGLE 5
56 /* stores a key event of current tap. */
57 static keyrecord_t tapping_key = {};
59 #define IS_TAPPING() !IS_NOEVENT(tapping_key.event)
60 #define IS_TAPPING_PRESSED() (IS_TAPPING() && tapping_key.event.pressed)
61 #define IS_TAPPING_RELEASED() (IS_TAPPING() && !tapping_key.event.pressed)
62 #define IS_TAPPING_KEY(k) (IS_TAPPING() && KEYEQ(tapping_key.event.key, (k)))
63 #define WITHIN_TAPPING_TERM(e) (TIMER_DIFF_16(e.time, tapping_key.event.time) < TAPPING_TERM)
69 * stores key events waiting for settling current tap.
71 #define WAITING_BUFFER_SIZE 8
72 static keyrecord_t waiting_buffer[WAITING_BUFFER_SIZE] = {};
74 /* point to empty cell to enq */
75 static uint8_t waiting_buffer_head = 0;
77 /* point to the oldest data cell to deq */
78 static uint8_t waiting_buffer_tail = 0;
80 static bool waiting_buffer_enq(keyrecord_t record)
82 if (IS_NOEVENT(record.event)) {
86 if ((waiting_buffer_head + 1) % WAITING_BUFFER_SIZE == waiting_buffer_tail) {
87 debug("waiting_buffer_enq: Over flow.\n");
91 waiting_buffer[waiting_buffer_head] = record;
92 waiting_buffer_head = (waiting_buffer_head + 1) % WAITING_BUFFER_SIZE;
94 debug("waiting_buffer_enq: "); debug_waiting_buffer();
98 static void waiting_buffer_clear(void)
100 waiting_buffer_head = 0;
101 waiting_buffer_tail = 0;
104 #if TAPPING_TERM >= 500
105 static bool waiting_buffer_typed(keyevent_t event)
107 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
108 if (KEYEQ(event.key, waiting_buffer[i].event.key) && event.pressed != waiting_buffer[i].event.pressed) {
116 bool waiting_buffer_has_anykey_pressed(void)
118 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
119 if (waiting_buffer[i].event.pressed) return true;
127 * Problem: Want to capitalize like 'The' but the result tends to be 'THe'.
128 * Solution: Oneshot modifier have its effect on only one key coming next.
129 * Tap Shift, then type 't', 'h' and 'e'. Not need to hold Shift key.
131 * Hold: works as normal modifier.
132 * Tap: one shot modifier.
133 * 2 Tap: cancel one shot modifier.
134 * 5-Tap: toggles enable/disable oneshot feature.
143 static void oneshot_start(uint8_t mods, uint16_t time)
145 oneshot_state.mods = mods;
146 oneshot_state.time = time;
147 oneshot_state.ready = true;
150 static void oneshot_cancel(void)
152 oneshot_state.mods = 0;
153 oneshot_state.time = 0;
154 oneshot_state.ready = false;
157 static void oneshot_toggle(void)
159 oneshot_state.disabled = !oneshot_state.disabled;
164 void action_exec(keyevent_t event)
166 if (!IS_NOEVENT(event)) {
167 debug("\n---- action_exec: start -----\n");
168 debug("EVENT: "); debug_event(event); debug("\n");
171 keyrecord_t record = { .event = event };
173 // pre-process on tapping
174 if (process_tapping(&record)) {
175 if (!IS_NOEVENT(record.event)) {
176 debug("processed: "); debug_record(record); debug("\n");
180 if (!waiting_buffer_enq(record)) {
181 // clear all in case of overflow.
182 debug("OVERFLOW: CLEAR ALL STATES\n");
184 waiting_buffer_clear();
185 tapping_key = (keyrecord_t){};
189 // process waiting_buffer
190 if (!IS_NOEVENT(event) && waiting_buffer_head != waiting_buffer_tail) {
191 debug("---- action_exec: process waiting_buffer -----\n");
194 for (; waiting_buffer_tail != waiting_buffer_head; waiting_buffer_tail = (waiting_buffer_tail + 1) % WAITING_BUFFER_SIZE) {
195 if (process_tapping(&waiting_buffer[waiting_buffer_tail])) {
196 debug("processed: waiting_buffer["); debug_dec(waiting_buffer_tail); debug("] = ");
197 debug_record(waiting_buffer[waiting_buffer_tail]); debug("\n\n");
202 if (!IS_NOEVENT(event)) {
207 static void process_action(keyrecord_t *record)
209 keyevent_t event = record->event;
210 uint8_t tap_count = record->tap.count;
212 if (IS_NOEVENT(event)) { return; }
214 action_t action = layer_switch_get_action(event.key);
215 debug("ACTION: "); debug_action(action);
216 debug(" overlays: "); overlay_debug();
217 debug(" keymaps: "); keymap_debug();
218 debug(" default_layer: "); debug_dec(default_layer); debug("\n");
220 switch (action.kind.id) {
225 uint8_t mods = (action.kind.id == ACT_LMODS) ? action.key.mods :
228 uint8_t tmp_mods = host_get_mods();
231 host_send_keyboard_report();
233 register_code(action.key.code);
234 if (mods && action.key.code) {
235 host_set_mods(tmp_mods);
236 host_send_keyboard_report();
239 if (mods && !action.key.code) {
241 host_send_keyboard_report();
243 unregister_code(action.key.code);
250 uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods :
252 switch (action.layer.code) {
256 if (tap_count == 0) {
257 debug("MODS_TAP: Oneshot: add_mods\n");
260 else if (tap_count == 1) {
261 debug("MODS_TAP: Oneshot: start\n");
262 oneshot_start(mods, event.time);
264 else if (tap_count == TAPPING_TOGGLE) {
265 debug("MODS_TAP: Oneshot: toggle\n");
269 debug("MODS_TAP: Oneshot: cancel&add_mods\n");
270 // double tap cancels oneshot and works as normal modifier.
275 if (tap_count == 0) {
276 debug("MODS_TAP: Oneshot: cancel/del_mods\n");
277 // cancel oneshot on hold
281 else if (tap_count == 1) {
282 debug("MODS_TAP: Oneshot: del_mods\n");
287 debug("MODS_TAP: Oneshot: del_mods\n");
296 if (waiting_buffer_has_anykey_pressed()) {
297 debug("MODS_TAP: Tap: Cancel: add_mods\n");
298 // ad hoc: set 0 to cancel tap
299 record->tap.count = 0;
302 debug("MODS_TAP: Tap: register_code\n");
303 register_code(action.key.code);
306 debug("MODS_TAP: No tap: add_mods\n");
311 debug("MODS_TAP: Tap: unregister_code\n");
312 unregister_code(action.key.code);
314 debug("MODS_TAP: No tap: add_mods\n");
323 /* other HID usage */
325 #ifdef EXTRAKEY_ENABLE
326 switch (action.usage.page) {
329 host_system_send(action.usage.code);
336 host_consumer_send(action.usage.code);
338 host_consumer_send(0);
347 #ifdef MOUSEKEY_ENABLE
349 mousekey_on(action.key.code);
352 mousekey_off(action.key.code);
359 switch (action.layer.code) {
362 switch (action.layer.val & 0x03) {
375 if (!event.pressed) {
384 /* NOTE: 4-7 rserved */
387 /* Keymap Reset default layer */
388 case (OP_RESET | ON_PRESS):
390 default_layer_set(action.layer.val);
393 case (OP_RESET | ON_RELEASE):
394 if (!event.pressed) {
395 default_layer_set(action.layer.val);
398 case (OP_RESET | ON_BOTH):
399 default_layer_set(action.layer.val);
402 /* Keymap Bit invert */
404 /* with tap toggle */
406 if (tap_count < TAPPING_TOGGLE) {
407 debug("KEYMAP_INV: tap toggle(press).\n");
408 keymap_invert(action.layer.val);
411 if (tap_count <= TAPPING_TOGGLE) {
412 debug("KEYMAP_INV: tap toggle(release).\n");
413 keymap_invert(action.layer.val);
417 case (OP_INV | ON_PRESS):
419 keymap_invert(action.layer.val);
422 case (OP_INV | ON_RELEASE):
423 if (!event.pressed) {
424 keymap_invert(action.layer.val);
427 case (OP_INV | ON_BOTH):
428 keymap_invert(action.layer.val);
434 keymap_on(action.layer.val);
436 keymap_off(action.layer.val);
439 case (OP_ON | ON_PRESS):
441 keymap_on(action.layer.val);
444 case (OP_ON | ON_RELEASE):
445 if (!event.pressed) {
446 keymap_on(action.layer.val);
449 case (OP_ON | ON_BOTH):
450 keymap_on(action.layer.val);
456 keymap_off(action.layer.val);
458 keymap_on(action.layer.val);
461 case (OP_OFF | ON_PRESS):
463 keymap_off(action.layer.val);
466 case (OP_OFF | ON_RELEASE):
467 if (!event.pressed) {
468 keymap_off(action.layer.val);
471 case (OP_OFF | ON_BOTH):
472 keymap_off(action.layer.val);
478 keymap_set(action.layer.val);
483 case (OP_SET | ON_PRESS):
485 keymap_set(action.layer.val);
488 case (OP_SET | ON_RELEASE):
489 if (!event.pressed) {
490 keymap_set(action.layer.val);
493 case (OP_SET | ON_BOTH):
494 keymap_set(action.layer.val);
497 /* Keymap Bit invert with tap key */
501 debug("KEYMAP_TAP_KEY: Tap: register_code\n");
502 register_code(action.layer.code);
504 debug("KEYMAP_TAP_KEY: No tap: On on press\n");
505 keymap_on(action.layer.val);
509 debug("KEYMAP_TAP_KEY: Tap: unregister_code\n");
510 unregister_code(action.layer.code);
512 debug("KEYMAP_TAP_KEY: No tap: Off on release\n");
513 keymap_off(action.layer.val);
520 #ifndef NO_ACTION_OVERLAY
522 switch (action.layer.code) {
523 // Overlay Invert bit4
525 if (action.layer.val == 0) {
526 // NOTE: reserved for future use
529 overlay_set(overlay_stat ^ action.layer.val);
533 if (action.layer.val == 0) {
535 if (event.pressed) overlay_clear();
537 overlay_set(overlay_stat ^ action.layer.val<<4);
541 if (action.layer.val == 0) {
543 if (!event.pressed) overlay_clear();
545 overlay_set(overlay_stat ^ action.layer.val<<8);
549 if (action.layer.val == 0) {
553 overlay_set(overlay_stat ^ action.layer.val<<12);
557 /* Overlay Bit invert */
559 /* with tap toggle */
561 if (tap_count < TAPPING_TOGGLE) {
562 debug("OVERLAY_INV: tap toggle(press).\n");
563 overlay_invert(action.layer.val);
566 if (tap_count <= TAPPING_TOGGLE) {
567 debug("OVERLAY_INV: tap toggle(release).\n");
568 overlay_invert(action.layer.val);
572 case (OP_INV | ON_PRESS):
574 overlay_invert(action.layer.val);
577 case (OP_INV | ON_RELEASE):
578 if (!event.pressed) {
579 overlay_invert(action.layer.val);
582 case (OP_INV | ON_BOTH):
583 overlay_invert(action.layer.val);
589 overlay_on(action.layer.val);
591 overlay_off(action.layer.val);
594 case (OP_ON | ON_PRESS):
596 overlay_on(action.layer.val);
599 case (OP_ON | ON_RELEASE):
600 if (!event.pressed) {
601 overlay_on(action.layer.val);
604 case (OP_ON | ON_BOTH):
605 overlay_on(action.layer.val);
608 /* Overlay Bit off */
611 overlay_off(action.layer.val);
613 overlay_on(action.layer.val);
616 case (OP_OFF | ON_PRESS):
618 overlay_off(action.layer.val);
621 case (OP_OFF | ON_RELEASE):
622 if (!event.pressed) {
623 overlay_off(action.layer.val);
626 case (OP_OFF | ON_BOTH):
627 overlay_off(action.layer.val);
630 /* Overlay Bit set */
633 overlay_move(action.layer.val);
638 case (OP_SET | ON_PRESS):
640 overlay_move(action.layer.val);
643 case (OP_SET | ON_RELEASE):
644 if (!event.pressed) {
645 overlay_move(action.layer.val);
648 case (OP_SET | ON_BOTH):
649 overlay_move(action.layer.val);
652 /* Overlay Bit invert with tap key */
656 debug("OVERLAY_TAP_KEY: Tap: register_code\n");
657 register_code(action.layer.code);
659 debug("OVERLAY_TAP_KEY: No tap: On on press\n");
660 overlay_on(action.layer.val);
664 debug("OVERLAY_TAP_KEY: Tap: unregister_code\n");
665 unregister_code(action.layer.code);
667 debug("OVERLAY_TAP_KEY: No tap: Off on release\n");
668 overlay_off(action.layer.val);
678 action_macro_play(action_get_macro(record, action.func.id, action.func.opt));
683 action_function(record, action.func.id, action.func.opt);
692 * Rule: Tap key is typed(pressed and released) within TAPPING_TERM.
693 * (without interfering by typing other key)
695 /* return true when key event is processed or consumed. */
696 static bool process_tapping(keyrecord_t *keyp)
698 keyevent_t event = keyp->event;
701 if (IS_TAPPING_PRESSED()) {
702 if (WITHIN_TAPPING_TERM(event)) {
703 if (tapping_key.tap.count == 0) {
704 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
706 debug("Tapping: First tap(0->1).\n");
707 tapping_key.tap.count = 1;
708 tapping_key.tap.interrupted = (waiting_buffer_has_anykey_pressed() ? true : false);
710 process_action(&tapping_key);
713 keyp->tap = tapping_key.tap;
716 #if TAPPING_TERM >= 500
717 /* This can prevent from typing some tap keys in a row at a time. */
718 else if (!event.pressed && waiting_buffer_typed(event)) {
719 // other key typed. not tap.
720 debug("Tapping: End. No tap. Interfered by typing key\n");
721 process_action(&tapping_key);
722 tapping_key = (keyrecord_t){};
730 // other key events shall be enq'd till tapping state settles.
736 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
737 debug("Tapping: Tap release("); debug_dec(tapping_key.tap.count); debug(")\n");
738 keyp->tap = tapping_key.tap;
739 process_action(keyp);
744 else if (is_tap_key(keyp->event.key) && event.pressed) {
745 if (tapping_key.tap.count > 1) {
746 debug("Tapping: Start new tap with releasing last tap(>1).\n");
748 process_action(&(keyrecord_t){
749 .tap = tapping_key.tap,
750 .event.key = tapping_key.event.key,
751 .event.time = event.time,
752 .event.pressed = false
755 debug("Tapping: Start while last tap(1).\n");
758 waiting_buffer_scan_tap();
763 if (!IS_NOEVENT(keyp->event)) {
764 debug("Tapping: key event while last tap(>0).\n");
766 process_action(keyp);
771 // after TAPPING_TERM
773 if (tapping_key.tap.count == 0) {
774 debug("Tapping: End. Timeout. Not tap(0): ");
775 debug_event(event); debug("\n");
776 process_action(&tapping_key);
777 tapping_key = (keyrecord_t){};
781 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
782 debug("Tapping: End. last timeout tap release(>0).");
783 keyp->tap = tapping_key.tap;
784 process_action(keyp);
785 tapping_key = (keyrecord_t){};
788 else if (is_tap_key(keyp->event.key) && event.pressed) {
789 if (tapping_key.tap.count > 1) {
790 debug("Tapping: Start new tap with releasing last timeout tap(>1).\n");
792 process_action(&(keyrecord_t){
793 .tap = tapping_key.tap,
794 .event.key = tapping_key.event.key,
795 .event.time = event.time,
796 .event.pressed = false
799 debug("Tapping: Start while last timeout tap(1).\n");
802 waiting_buffer_scan_tap();
807 if (!IS_NOEVENT(keyp->event)) {
808 debug("Tapping: key event while last timeout tap(>0).\n");
810 process_action(keyp);
815 } else if (IS_TAPPING_RELEASED()) {
816 if (WITHIN_TAPPING_TERM(event)) {
817 if (tapping_key.tap.count > 0 && IS_TAPPING_KEY(event.key) && event.pressed) {
819 keyp->tap = tapping_key.tap;
820 keyp->tap.count += 1;
821 debug("Tapping: Tap press("); debug_dec(keyp->tap.count); debug(")\n");
822 process_action(keyp);
826 } else if (event.pressed && is_tap_key(event.key)) {
827 // Sequential tap can be interfered with other tap key.
828 debug("Tapping: Start with interfering other tap.\n");
830 waiting_buffer_scan_tap();
834 if (!IS_NOEVENT(keyp->event)) debug("Tapping: other key just after tap.\n");
835 process_action(keyp);
839 // timeout. no sequential tap.
840 debug("Tapping: End(Timeout after releasing last tap): ");
841 debug_event(event); debug("\n");
842 tapping_key = (keyrecord_t){};
847 // not tapping satate
849 if (event.pressed && is_tap_key(event.key)) {
850 debug("Tapping: Start(Press tap key).\n");
852 waiting_buffer_scan_tap();
856 process_action(keyp);
862 /* scan buffer for tapping */
863 static void waiting_buffer_scan_tap(void)
865 // tapping already is settled
866 if (tapping_key.tap.count > 0) return;
867 // invalid state: tapping_key released && tap.count == 0
868 if (!tapping_key.event.pressed) return;
870 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
871 if (IS_TAPPING_KEY(waiting_buffer[i].event.key) &&
872 !waiting_buffer[i].event.pressed &&
873 WITHIN_TAPPING_TERM(waiting_buffer[i].event)) {
874 tapping_key.tap.count = 1;
875 waiting_buffer[i].tap.count = 1;
876 process_action(&tapping_key);
878 debug("waiting_buffer_scan_tap: found at ["); debug_dec(i); debug("]\n");
879 debug_waiting_buffer();
888 * Utilities for actions.
890 void register_code(uint8_t code)
895 #ifdef CAPSLOCK_LOCKING_ENABLE
896 else if (KC_LOCKING_CAPS == code) {
897 #ifdef CAPSLOCK_LOCKING_RESYNC_ENABLE
898 // Resync: ignore if caps lock already is on
899 if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) return;
901 host_add_key(KC_CAPSLOCK);
902 host_send_keyboard_report();
903 host_del_key(KC_CAPSLOCK);
904 host_send_keyboard_report();
907 else if IS_KEY(code) {
908 // TODO: should push command_proc out of this block?
909 if (command_proc(code)) return;
911 if (oneshot_state.mods && oneshot_state.ready && !oneshot_state.disabled) {
912 uint8_t tmp_mods = host_get_mods();
913 host_add_mods(oneshot_state.mods);
915 host_send_keyboard_report();
917 host_set_mods(tmp_mods);
918 oneshot_state.ready = false;
921 host_send_keyboard_report();
924 else if IS_MOD(code) {
925 host_add_mods(MOD_BIT(code));
926 host_send_keyboard_report();
930 void unregister_code(uint8_t code)
935 #ifdef CAPSLOCK_LOCKING_ENABLE
936 else if (KC_LOCKING_CAPS == code) {
937 #ifdef CAPSLOCK_LOCKING_RESYNC_ENABLE
938 // Resync: ignore if caps lock already is off
939 if (!(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK))) return;
941 host_add_key(KC_CAPSLOCK);
942 host_send_keyboard_report();
943 host_del_key(KC_CAPSLOCK);
944 host_send_keyboard_report();
947 else if IS_KEY(code) {
949 host_send_keyboard_report();
951 else if IS_MOD(code) {
952 host_del_mods(MOD_BIT(code));
953 host_send_keyboard_report();
957 void add_mods(uint8_t mods)
961 host_send_keyboard_report();
965 void del_mods(uint8_t mods)
969 host_send_keyboard_report();
973 void set_mods(uint8_t mods)
976 host_send_keyboard_report();
979 void clear_keyboard(void)
982 clear_keyboard_but_mods();
985 void clear_keyboard_but_mods(void)
988 host_send_keyboard_report();
989 #ifdef MOUSEKEY_ENABLE
993 #ifdef EXTRAKEY_ENABLE
995 host_consumer_send(0);
999 bool sending_anykey(void)
1001 return (host_has_anykey() || host_mouse_in_use() ||
1002 host_last_sysytem_report() || host_last_consumer_report());
1005 bool is_tap_key(key_t key)
1007 action_t action = layer_switch_get_action(key);
1009 switch (action.kind.id) {
1015 switch (action.layer.code) {
1016 case 0x04 ... 0xEF: /* tap key */
1024 if (action.func.opt & FUNC_TAP) { return true; }
1034 static void debug_event(keyevent_t event)
1036 debug_hex16((event.key.row<<8) | event.key.col);
1037 if (event.pressed) debug("d("); else debug("u(");
1038 debug_dec(event.time); debug(")");
1040 static void debug_record(keyrecord_t record)
1042 debug_event(record.event); debug(":"); debug_dec(record.tap.count);
1043 if (record.tap.interrupted) debug("-");
1045 static void debug_action(action_t action)
1047 switch (action.kind.id) {
1048 case ACT_LMODS: debug("ACT_LMODS"); break;
1049 case ACT_RMODS: debug("ACT_RMODS"); break;
1050 case ACT_LMODS_TAP: debug("ACT_LMODS_TAP"); break;
1051 case ACT_RMODS_TAP: debug("ACT_RMODS_TAP"); break;
1052 case ACT_USAGE: debug("ACT_USAGE"); break;
1053 case ACT_MOUSEKEY: debug("ACT_MOUSEKEY"); break;
1054 case ACT_KEYMAP: debug("ACT_KEYMAP"); break;
1055 case ACT_OVERLAY: debug("ACT_OVERLAY"); break;
1056 case ACT_MACRO: debug("ACT_MACRO"); break;
1057 case ACT_COMMAND: debug("ACT_COMMAND"); break;
1058 case ACT_FUNCTION: debug("ACT_FUNCTION"); break;
1059 default: debug("UNKNOWN"); break;
1062 debug_hex4(action.kind.param>>8);
1064 debug_hex8(action.kind.param & 0xff);
1067 static void debug_tapping_key(void)
1069 debug("TAPPING_KEY="); debug_record(tapping_key); debug("\n");
1071 static void debug_waiting_buffer(void)
1074 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
1075 debug("["); debug_dec(i); debug("]="); debug_record(waiting_buffer[i]); debug(" ");