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);
521 switch (action.layer.code) {
522 // Overlay Invert bit4
524 if (action.layer.val == 0) {
525 // NOTE: reserved for future use
528 overlay_set(overlay_stat ^ action.layer.val);
532 if (action.layer.val == 0) {
534 if (event.pressed) overlay_clear();
536 overlay_set(overlay_stat ^ action.layer.val<<4);
540 if (action.layer.val == 0) {
542 if (!event.pressed) overlay_clear();
544 overlay_set(overlay_stat ^ action.layer.val<<8);
548 if (action.layer.val == 0) {
552 overlay_set(overlay_stat ^ action.layer.val<<12);
556 /* Overlay Bit invert */
558 /* with tap toggle */
560 if (tap_count < TAPPING_TOGGLE) {
561 debug("OVERLAY_INV: tap toggle(press).\n");
562 overlay_invert(action.layer.val);
565 if (tap_count <= TAPPING_TOGGLE) {
566 debug("OVERLAY_INV: tap toggle(release).\n");
567 overlay_invert(action.layer.val);
571 case (OP_INV | ON_PRESS):
573 overlay_invert(action.layer.val);
576 case (OP_INV | ON_RELEASE):
577 if (!event.pressed) {
578 overlay_invert(action.layer.val);
581 case (OP_INV | ON_BOTH):
582 overlay_invert(action.layer.val);
588 overlay_on(action.layer.val);
590 overlay_off(action.layer.val);
593 case (OP_ON | ON_PRESS):
595 overlay_on(action.layer.val);
598 case (OP_ON | ON_RELEASE):
599 if (!event.pressed) {
600 overlay_on(action.layer.val);
603 case (OP_ON | ON_BOTH):
604 overlay_on(action.layer.val);
607 /* Overlay Bit off */
610 overlay_off(action.layer.val);
612 overlay_on(action.layer.val);
615 case (OP_OFF | ON_PRESS):
617 overlay_off(action.layer.val);
620 case (OP_OFF | ON_RELEASE):
621 if (!event.pressed) {
622 overlay_off(action.layer.val);
625 case (OP_OFF | ON_BOTH):
626 overlay_off(action.layer.val);
629 /* Overlay Bit set */
632 overlay_move(action.layer.val);
637 case (OP_SET | ON_PRESS):
639 overlay_move(action.layer.val);
642 case (OP_SET | ON_RELEASE):
643 if (!event.pressed) {
644 overlay_move(action.layer.val);
647 case (OP_SET | ON_BOTH):
648 overlay_move(action.layer.val);
651 /* Overlay Bit invert with tap key */
655 debug("OVERLAY_TAP_KEY: Tap: register_code\n");
656 register_code(action.layer.code);
658 debug("OVERLAY_TAP_KEY: No tap: On on press\n");
659 overlay_on(action.layer.val);
663 debug("OVERLAY_TAP_KEY: Tap: unregister_code\n");
664 unregister_code(action.layer.code);
666 debug("OVERLAY_TAP_KEY: No tap: Off on release\n");
667 overlay_off(action.layer.val);
676 action_macro_play(action_get_macro(record, action.func.id, action.func.opt));
681 action_function(record, action.func.id, action.func.opt);
690 * Rule: Tap key is typed(pressed and released) within TAPPING_TERM.
691 * (without interfering by typing other key)
693 /* return true when key event is processed or consumed. */
694 static bool process_tapping(keyrecord_t *keyp)
696 keyevent_t event = keyp->event;
699 if (IS_TAPPING_PRESSED()) {
700 if (WITHIN_TAPPING_TERM(event)) {
701 if (tapping_key.tap.count == 0) {
702 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
704 debug("Tapping: First tap(0->1).\n");
705 tapping_key.tap.count = 1;
706 tapping_key.tap.interrupted = (waiting_buffer_has_anykey_pressed() ? true : false);
708 process_action(&tapping_key);
711 keyp->tap = tapping_key.tap;
714 #if TAPPING_TERM >= 500
715 /* This can prevent from typing some tap keys in a row at a time. */
716 else if (!event.pressed && waiting_buffer_typed(event)) {
717 // other key typed. not tap.
718 debug("Tapping: End. No tap. Interfered by typing key\n");
719 process_action(&tapping_key);
720 tapping_key = (keyrecord_t){};
728 // other key events shall be enq'd till tapping state settles.
734 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
735 debug("Tapping: Tap release("); debug_dec(tapping_key.tap.count); debug(")\n");
736 keyp->tap = tapping_key.tap;
737 process_action(keyp);
742 else if (is_tap_key(keyp->event.key) && event.pressed) {
743 if (tapping_key.tap.count > 1) {
744 debug("Tapping: Start new tap with releasing last tap(>1).\n");
746 process_action(&(keyrecord_t){
747 .tap = tapping_key.tap,
748 .event.key = tapping_key.event.key,
749 .event.time = event.time,
750 .event.pressed = false
753 debug("Tapping: Start while last tap(1).\n");
756 waiting_buffer_scan_tap();
761 if (!IS_NOEVENT(keyp->event)) {
762 debug("Tapping: key event while last tap(>0).\n");
764 process_action(keyp);
769 // after TAPPING_TERM
771 if (tapping_key.tap.count == 0) {
772 debug("Tapping: End. Timeout. Not tap(0): ");
773 debug_event(event); debug("\n");
774 process_action(&tapping_key);
775 tapping_key = (keyrecord_t){};
779 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
780 debug("Tapping: End. last timeout tap release(>0).");
781 keyp->tap = tapping_key.tap;
782 process_action(keyp);
783 tapping_key = (keyrecord_t){};
786 else if (is_tap_key(keyp->event.key) && event.pressed) {
787 if (tapping_key.tap.count > 1) {
788 debug("Tapping: Start new tap with releasing last timeout tap(>1).\n");
790 process_action(&(keyrecord_t){
791 .tap = tapping_key.tap,
792 .event.key = tapping_key.event.key,
793 .event.time = event.time,
794 .event.pressed = false
797 debug("Tapping: Start while last timeout tap(1).\n");
800 waiting_buffer_scan_tap();
805 if (!IS_NOEVENT(keyp->event)) {
806 debug("Tapping: key event while last timeout tap(>0).\n");
808 process_action(keyp);
813 } else if (IS_TAPPING_RELEASED()) {
814 if (WITHIN_TAPPING_TERM(event)) {
815 if (tapping_key.tap.count > 0 && IS_TAPPING_KEY(event.key) && event.pressed) {
817 keyp->tap = tapping_key.tap;
818 keyp->tap.count += 1;
819 debug("Tapping: Tap press("); debug_dec(keyp->tap.count); debug(")\n");
820 process_action(keyp);
824 } else if (event.pressed && is_tap_key(event.key)) {
825 // Sequential tap can be interfered with other tap key.
826 debug("Tapping: Start with interfering other tap.\n");
828 waiting_buffer_scan_tap();
832 if (!IS_NOEVENT(keyp->event)) debug("Tapping: other key just after tap.\n");
833 process_action(keyp);
837 // timeout. no sequential tap.
838 debug("Tapping: End(Timeout after releasing last tap): ");
839 debug_event(event); debug("\n");
840 tapping_key = (keyrecord_t){};
845 // not tapping satate
847 if (event.pressed && is_tap_key(event.key)) {
848 debug("Tapping: Start(Press tap key).\n");
850 waiting_buffer_scan_tap();
854 process_action(keyp);
860 /* scan buffer for tapping */
861 static void waiting_buffer_scan_tap(void)
863 // tapping already is settled
864 if (tapping_key.tap.count > 0) return;
865 // invalid state: tapping_key released && tap.count == 0
866 if (!tapping_key.event.pressed) return;
868 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
869 if (IS_TAPPING_KEY(waiting_buffer[i].event.key) &&
870 !waiting_buffer[i].event.pressed &&
871 WITHIN_TAPPING_TERM(waiting_buffer[i].event)) {
872 tapping_key.tap.count = 1;
873 waiting_buffer[i].tap.count = 1;
874 process_action(&tapping_key);
876 debug("waiting_buffer_scan_tap: found at ["); debug_dec(i); debug("]\n");
877 debug_waiting_buffer();
886 * Utilities for actions.
888 void register_code(uint8_t code)
893 #ifdef CAPSLOCK_LOCKING_ENABLE
894 else if (KC_LOCKING_CAPS == code) {
895 #ifdef CAPSLOCK_LOCKING_RESYNC_ENABLE
896 // Resync: ignore if caps lock already is on
897 if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) return;
899 host_add_key(KC_CAPSLOCK);
900 host_send_keyboard_report();
901 host_del_key(KC_CAPSLOCK);
902 host_send_keyboard_report();
905 else if IS_KEY(code) {
906 // TODO: should push command_proc out of this block?
907 if (command_proc(code)) return;
909 if (oneshot_state.mods && oneshot_state.ready && !oneshot_state.disabled) {
910 uint8_t tmp_mods = host_get_mods();
911 host_add_mods(oneshot_state.mods);
913 host_send_keyboard_report();
915 host_set_mods(tmp_mods);
916 oneshot_state.ready = false;
919 host_send_keyboard_report();
922 else if IS_MOD(code) {
923 host_add_mods(MOD_BIT(code));
924 host_send_keyboard_report();
928 void unregister_code(uint8_t code)
933 #ifdef CAPSLOCK_LOCKING_ENABLE
934 else if (KC_LOCKING_CAPS == code) {
935 #ifdef CAPSLOCK_LOCKING_RESYNC_ENABLE
936 // Resync: ignore if caps lock already is off
937 if (!(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK))) return;
939 host_add_key(KC_CAPSLOCK);
940 host_send_keyboard_report();
941 host_del_key(KC_CAPSLOCK);
942 host_send_keyboard_report();
945 else if IS_KEY(code) {
947 host_send_keyboard_report();
949 else if IS_MOD(code) {
950 host_del_mods(MOD_BIT(code));
951 host_send_keyboard_report();
955 void add_mods(uint8_t mods)
959 host_send_keyboard_report();
963 void del_mods(uint8_t mods)
967 host_send_keyboard_report();
971 void set_mods(uint8_t mods)
974 host_send_keyboard_report();
977 void clear_keyboard(void)
980 clear_keyboard_but_mods();
983 void clear_keyboard_but_mods(void)
986 host_send_keyboard_report();
987 #ifdef MOUSEKEY_ENABLE
991 #ifdef EXTRAKEY_ENABLE
993 host_consumer_send(0);
997 bool sending_anykey(void)
999 return (host_has_anykey() || host_mouse_in_use() ||
1000 host_last_sysytem_report() || host_last_consumer_report());
1003 bool is_tap_key(key_t key)
1005 action_t action = layer_switch_get_action(key);
1007 switch (action.kind.id) {
1013 switch (action.layer.code) {
1014 case 0x04 ... 0xEF: /* tap key */
1022 if (action.func.opt & FUNC_TAP) { return true; }
1032 static void debug_event(keyevent_t event)
1034 debug_hex16((event.key.row<<8) | event.key.col);
1035 if (event.pressed) debug("d("); else debug("u(");
1036 debug_dec(event.time); debug(")");
1038 static void debug_record(keyrecord_t record)
1040 debug_event(record.event); debug(":"); debug_dec(record.tap.count);
1041 if (record.tap.interrupted) debug("-");
1043 static void debug_action(action_t action)
1045 switch (action.kind.id) {
1046 case ACT_LMODS: debug("ACT_LMODS"); break;
1047 case ACT_RMODS: debug("ACT_RMODS"); break;
1048 case ACT_LMODS_TAP: debug("ACT_LMODS_TAP"); break;
1049 case ACT_RMODS_TAP: debug("ACT_RMODS_TAP"); break;
1050 case ACT_USAGE: debug("ACT_USAGE"); break;
1051 case ACT_MOUSEKEY: debug("ACT_MOUSEKEY"); break;
1052 case ACT_KEYMAP: debug("ACT_KEYMAP"); break;
1053 case ACT_OVERLAY: debug("ACT_OVERLAY"); break;
1054 case ACT_MACRO: debug("ACT_MACRO"); break;
1055 case ACT_COMMAND: debug("ACT_COMMAND"); break;
1056 case ACT_FUNCTION: debug("ACT_FUNCTION"); break;
1057 default: debug("UNKNOWN"); break;
1060 debug_hex4(action.kind.param>>8);
1062 debug_hex8(action.kind.param & 0xff);
1065 static void debug_tapping_key(void)
1067 debug("TAPPING_KEY="); debug_record(tapping_key); debug("\n");
1069 static void debug_waiting_buffer(void)
1072 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
1073 debug("["); debug_dec(i); debug("]="); debug_record(waiting_buffer[i]); debug(" ");