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/>.
26 #include "layer_switch.h"
27 #include "action_macro.h"
31 static void process_action(keyrecord_t *record);
32 static bool process_tapping(keyrecord_t *record);
33 static void waiting_buffer_scan_tap(void);
35 static void debug_event(keyevent_t event);
36 static void debug_record(keyrecord_t record);
37 static void debug_action(action_t action);
38 static void debug_tapping_key(void);
39 static void debug_waiting_buffer(void);
45 /* period of tapping(ms) */
47 #define TAPPING_TERM 200
50 /* tap count needed for toggling a feature */
51 #ifndef TAPPING_TOGGLE
52 #define TAPPING_TOGGLE 5
55 /* stores a key event of current tap. */
56 static keyrecord_t tapping_key = {};
58 #define IS_TAPPING() !IS_NOEVENT(tapping_key.event)
59 #define IS_TAPPING_PRESSED() (IS_TAPPING() && tapping_key.event.pressed)
60 #define IS_TAPPING_RELEASED() (IS_TAPPING() && !tapping_key.event.pressed)
61 #define IS_TAPPING_KEY(k) (IS_TAPPING() && KEYEQ(tapping_key.event.key, (k)))
62 #define WITHIN_TAPPING_TERM(e) (TIMER_DIFF_16(e.time, tapping_key.event.time) < TAPPING_TERM)
68 * stores key events waiting for settling current tap.
70 #define WAITING_BUFFER_SIZE 8
71 static keyrecord_t waiting_buffer[WAITING_BUFFER_SIZE] = {};
73 /* point to empty cell to enq */
74 static uint8_t waiting_buffer_head = 0;
76 /* point to the oldest data cell to deq */
77 static uint8_t waiting_buffer_tail = 0;
79 static bool waiting_buffer_enq(keyrecord_t record)
81 if (IS_NOEVENT(record.event)) {
85 if ((waiting_buffer_head + 1) % WAITING_BUFFER_SIZE == waiting_buffer_tail) {
86 debug("waiting_buffer_enq: Over flow.\n");
90 waiting_buffer[waiting_buffer_head] = record;
91 waiting_buffer_head = (waiting_buffer_head + 1) % WAITING_BUFFER_SIZE;
93 debug("waiting_buffer_enq: "); debug_waiting_buffer();
97 static void waiting_buffer_clear(void)
99 waiting_buffer_head = 0;
100 waiting_buffer_tail = 0;
103 #if TAPPING_TERM >= 500
104 static bool waiting_buffer_typed(keyevent_t event)
106 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
107 if (KEYEQ(event.key, waiting_buffer[i].event.key) && event.pressed != waiting_buffer[i].event.pressed) {
115 bool waiting_buffer_has_anykey_pressed(void)
117 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
118 if (waiting_buffer[i].event.pressed) return true;
126 * Problem: Want to capitalize like 'The' but the result tends to be 'THe'.
127 * Solution: Oneshot modifier have its effect on only one key coming next.
128 * Tap Shift, then type 't', 'h' and 'e'. Not need to hold Shift key.
130 * Hold: works as normal modifier.
131 * Tap: one shot modifier.
132 * 2 Tap: cancel one shot modifier.
133 * 5-Tap: toggles enable/disable oneshot feature.
142 static void oneshot_start(uint8_t mods, uint16_t time)
144 oneshot_state.mods = mods;
145 oneshot_state.time = time;
146 oneshot_state.ready = true;
149 static void oneshot_cancel(void)
151 oneshot_state.mods = 0;
152 oneshot_state.time = 0;
153 oneshot_state.ready = false;
156 static void oneshot_toggle(void)
158 oneshot_state.disabled = !oneshot_state.disabled;
163 void action_exec(keyevent_t event)
165 if (!IS_NOEVENT(event)) {
166 debug("\n---- action_exec: start -----\n");
167 debug("EVENT: "); debug_event(event); debug("\n");
170 keyrecord_t record = { .event = event };
172 // pre-process on tapping
173 if (process_tapping(&record)) {
174 if (!IS_NOEVENT(record.event)) {
175 debug("processed: "); debug_record(record); debug("\n");
179 if (!waiting_buffer_enq(record)) {
180 // clear all in case of overflow.
181 debug("OVERFLOW: CLEAR ALL STATES\n");
183 waiting_buffer_clear();
184 tapping_key = (keyrecord_t){};
188 // process waiting_buffer
189 if (!IS_NOEVENT(event) && waiting_buffer_head != waiting_buffer_tail) {
190 debug("---- action_exec: process waiting_buffer -----\n");
193 for (; waiting_buffer_tail != waiting_buffer_head; waiting_buffer_tail = (waiting_buffer_tail + 1) % WAITING_BUFFER_SIZE) {
194 if (process_tapping(&waiting_buffer[waiting_buffer_tail])) {
195 debug("processed: waiting_buffer["); debug_dec(waiting_buffer_tail); debug("] = ");
196 debug_record(waiting_buffer[waiting_buffer_tail]); debug("\n\n");
201 if (!IS_NOEVENT(event)) {
206 static void process_action(keyrecord_t *record)
208 keyevent_t event = record->event;
209 uint8_t tap_count = record->tap.count;
211 if (IS_NOEVENT(event)) { return; }
213 action_t action = layer_switch_get_action(event.key);
214 debug("ACTION: "); debug_action(action);
215 debug(" overlays: "); overlay_debug();
216 debug(" keymaps: "); keymap_debug();
217 debug(" default_layer: "); debug_dec(default_layer); debug("\n");
219 switch (action.kind.id) {
224 uint8_t mods = (action.kind.id == ACT_LMODS) ? action.key.mods :
227 uint8_t tmp_mods = host_get_mods();
230 host_send_keyboard_report();
232 register_code(action.key.code);
233 if (mods && action.key.code) {
234 host_set_mods(tmp_mods);
235 host_send_keyboard_report();
238 if (mods && !action.key.code) {
240 host_send_keyboard_report();
242 unregister_code(action.key.code);
249 uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods :
251 switch (action.layer.code) {
255 if (tap_count == 0) {
256 debug("MODS_TAP: Oneshot: add_mods\n");
259 else if (tap_count == 1) {
260 debug("MODS_TAP: Oneshot: start\n");
261 oneshot_start(mods, event.time);
263 else if (tap_count == TAPPING_TOGGLE) {
264 debug("MODS_TAP: Oneshot: toggle\n");
268 debug("MODS_TAP: Oneshot: cancel&add_mods\n");
269 // double tap cancels oneshot and works as normal modifier.
274 if (tap_count == 0) {
275 debug("MODS_TAP: Oneshot: cancel/del_mods\n");
276 // cancel oneshot on hold
280 else if (tap_count == 1) {
281 debug("MODS_TAP: Oneshot: del_mods\n");
286 debug("MODS_TAP: Oneshot: del_mods\n");
295 if (waiting_buffer_has_anykey_pressed()) {
296 debug("MODS_TAP: Tap: Cancel: add_mods\n");
297 // ad hoc: set 0 to cancel tap
298 record->tap.count = 0;
301 debug("MODS_TAP: Tap: register_code\n");
302 register_code(action.key.code);
305 debug("MODS_TAP: No tap: add_mods\n");
310 debug("MODS_TAP: Tap: unregister_code\n");
311 unregister_code(action.key.code);
313 debug("MODS_TAP: No tap: add_mods\n");
322 /* other HID usage */
324 #ifdef EXTRAKEY_ENABLE
325 switch (action.usage.page) {
328 host_system_send(action.usage.code);
335 host_consumer_send(action.usage.code);
337 host_consumer_send(0);
346 #ifdef MOUSEKEY_ENABLE
348 mousekey_on(action.key.code);
351 mousekey_off(action.key.code);
358 switch (action.layer.code) {
361 switch (action.layer.val & 0x03) {
374 if (!event.pressed) {
383 /* NOTE: 4-7 rserved */
386 /* Keymap Reset default layer */
387 case (OP_RESET | ON_PRESS):
389 default_layer_set(action.layer.val);
392 case (OP_RESET | ON_RELEASE):
393 if (!event.pressed) {
394 default_layer_set(action.layer.val);
397 case (OP_RESET | ON_BOTH):
398 default_layer_set(action.layer.val);
401 /* Keymap Bit invert */
403 /* with tap toggle */
405 if (tap_count < TAPPING_TOGGLE) {
406 debug("KEYMAP_INV: tap toggle(press).\n");
407 keymap_invert(action.layer.val);
410 if (tap_count <= TAPPING_TOGGLE) {
411 debug("KEYMAP_INV: tap toggle(release).\n");
412 keymap_invert(action.layer.val);
416 case (OP_INV | ON_PRESS):
418 keymap_invert(action.layer.val);
421 case (OP_INV | ON_RELEASE):
422 if (!event.pressed) {
423 keymap_invert(action.layer.val);
426 case (OP_INV | ON_BOTH):
427 keymap_invert(action.layer.val);
433 keymap_on(action.layer.val);
435 keymap_off(action.layer.val);
438 case (OP_ON | ON_PRESS):
440 keymap_on(action.layer.val);
443 case (OP_ON | ON_RELEASE):
444 if (!event.pressed) {
445 keymap_on(action.layer.val);
448 case (OP_ON | ON_BOTH):
449 keymap_on(action.layer.val);
455 keymap_off(action.layer.val);
457 keymap_on(action.layer.val);
460 case (OP_OFF | ON_PRESS):
462 keymap_off(action.layer.val);
465 case (OP_OFF | ON_RELEASE):
466 if (!event.pressed) {
467 keymap_off(action.layer.val);
470 case (OP_OFF | ON_BOTH):
471 keymap_off(action.layer.val);
477 keymap_set(action.layer.val);
482 case (OP_SET | ON_PRESS):
484 keymap_set(action.layer.val);
487 case (OP_SET | ON_RELEASE):
488 if (!event.pressed) {
489 keymap_set(action.layer.val);
492 case (OP_SET | ON_BOTH):
493 keymap_set(action.layer.val);
496 /* Keymap Bit invert with tap key */
500 debug("KEYMAP_TAP_KEY: Tap: register_code\n");
501 register_code(action.layer.code);
503 debug("KEYMAP_TAP_KEY: No tap: On on press\n");
504 keymap_on(action.layer.val);
508 debug("KEYMAP_TAP_KEY: Tap: unregister_code\n");
509 unregister_code(action.layer.code);
511 debug("KEYMAP_TAP_KEY: No tap: Off on release\n");
512 keymap_off(action.layer.val);
520 switch (action.layer.code) {
521 // Overlay Invert bit4
523 if (action.layer.val == 0) {
524 // NOTE: reserved for future use
527 overlay_set(overlay_stat ^ action.layer.val);
531 if (action.layer.val == 0) {
533 if (event.pressed) overlay_clear();
535 overlay_set(overlay_stat ^ action.layer.val<<4);
539 if (action.layer.val == 0) {
541 if (!event.pressed) overlay_clear();
543 overlay_set(overlay_stat ^ action.layer.val<<8);
547 if (action.layer.val == 0) {
551 overlay_set(overlay_stat ^ action.layer.val<<12);
555 /* Overlay Bit invert */
557 /* with tap toggle */
559 if (tap_count < TAPPING_TOGGLE) {
560 debug("OVERLAY_INV: tap toggle(press).\n");
561 overlay_invert(action.layer.val);
564 if (tap_count <= TAPPING_TOGGLE) {
565 debug("OVERLAY_INV: tap toggle(release).\n");
566 overlay_invert(action.layer.val);
570 case (OP_INV | ON_PRESS):
572 overlay_invert(action.layer.val);
575 case (OP_INV | ON_RELEASE):
576 if (!event.pressed) {
577 overlay_invert(action.layer.val);
580 case (OP_INV | ON_BOTH):
581 overlay_invert(action.layer.val);
587 overlay_on(action.layer.val);
589 overlay_off(action.layer.val);
592 case (OP_ON | ON_PRESS):
594 overlay_on(action.layer.val);
597 case (OP_ON | ON_RELEASE):
598 if (!event.pressed) {
599 overlay_on(action.layer.val);
602 case (OP_ON | ON_BOTH):
603 overlay_on(action.layer.val);
606 /* Overlay Bit off */
609 overlay_off(action.layer.val);
611 overlay_on(action.layer.val);
614 case (OP_OFF | ON_PRESS):
616 overlay_off(action.layer.val);
619 case (OP_OFF | ON_RELEASE):
620 if (!event.pressed) {
621 overlay_off(action.layer.val);
624 case (OP_OFF | ON_BOTH):
625 overlay_off(action.layer.val);
628 /* Overlay Bit set */
631 overlay_move(action.layer.val);
636 case (OP_SET | ON_PRESS):
638 overlay_move(action.layer.val);
641 case (OP_SET | ON_RELEASE):
642 if (!event.pressed) {
643 overlay_move(action.layer.val);
646 case (OP_SET | ON_BOTH):
647 overlay_move(action.layer.val);
650 /* Overlay Bit invert with tap key */
654 debug("OVERLAY_TAP_KEY: Tap: register_code\n");
655 register_code(action.layer.code);
657 debug("OVERLAY_TAP_KEY: No tap: On on press\n");
658 overlay_on(action.layer.val);
662 debug("OVERLAY_TAP_KEY: Tap: unregister_code\n");
663 unregister_code(action.layer.code);
665 debug("OVERLAY_TAP_KEY: No tap: Off on release\n");
666 overlay_off(action.layer.val);
675 action_macro_play(action_get_macro(record, action.func.id, action.func.opt));
680 action_function(record, action.func.id, action.func.opt);
689 * Rule: Tap key is typed(pressed and released) within TAPPING_TERM.
690 * (without interfering by typing other key)
692 /* return true when key event is processed or consumed. */
693 static bool process_tapping(keyrecord_t *keyp)
695 keyevent_t event = keyp->event;
698 if (IS_TAPPING_PRESSED()) {
699 if (WITHIN_TAPPING_TERM(event)) {
700 if (tapping_key.tap.count == 0) {
701 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
703 debug("Tapping: First tap(0->1).\n");
704 tapping_key.tap.count = 1;
705 tapping_key.tap.interrupted = (waiting_buffer_has_anykey_pressed() ? true : false);
707 process_action(&tapping_key);
710 keyp->tap = tapping_key.tap;
713 #if TAPPING_TERM >= 500
714 /* This can prevent from typing some tap keys in a row at a time. */
715 else if (!event.pressed && waiting_buffer_typed(event)) {
716 // other key typed. not tap.
717 debug("Tapping: End. No tap. Interfered by typing key\n");
718 process_action(&tapping_key);
719 tapping_key = (keyrecord_t){};
727 // other key events shall be enq'd till tapping state settles.
733 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
734 debug("Tapping: Tap release("); debug_dec(tapping_key.tap.count); debug(")\n");
735 keyp->tap = tapping_key.tap;
736 process_action(keyp);
741 else if (is_tap_key(keyp->event.key) && event.pressed) {
742 if (tapping_key.tap.count > 1) {
743 debug("Tapping: Start new tap with releasing last tap(>1).\n");
745 process_action(&(keyrecord_t){
746 .tap = tapping_key.tap,
747 .event.key = tapping_key.event.key,
748 .event.time = event.time,
749 .event.pressed = false
752 debug("Tapping: Start while last tap(1).\n");
755 waiting_buffer_scan_tap();
760 if (!IS_NOEVENT(keyp->event)) {
761 debug("Tapping: key event while last tap(>0).\n");
763 process_action(keyp);
768 // after TAPPING_TERM
770 if (tapping_key.tap.count == 0) {
771 debug("Tapping: End. Timeout. Not tap(0): ");
772 debug_event(event); debug("\n");
773 process_action(&tapping_key);
774 tapping_key = (keyrecord_t){};
778 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
779 debug("Tapping: End. last timeout tap release(>0).");
780 keyp->tap = tapping_key.tap;
781 process_action(keyp);
782 tapping_key = (keyrecord_t){};
785 else if (is_tap_key(keyp->event.key) && event.pressed) {
786 if (tapping_key.tap.count > 1) {
787 debug("Tapping: Start new tap with releasing last timeout tap(>1).\n");
789 process_action(&(keyrecord_t){
790 .tap = tapping_key.tap,
791 .event.key = tapping_key.event.key,
792 .event.time = event.time,
793 .event.pressed = false
796 debug("Tapping: Start while last timeout tap(1).\n");
799 waiting_buffer_scan_tap();
804 if (!IS_NOEVENT(keyp->event)) {
805 debug("Tapping: key event while last timeout tap(>0).\n");
807 process_action(keyp);
812 } else if (IS_TAPPING_RELEASED()) {
813 if (WITHIN_TAPPING_TERM(event)) {
814 if (tapping_key.tap.count > 0 && IS_TAPPING_KEY(event.key) && event.pressed) {
816 keyp->tap = tapping_key.tap;
817 keyp->tap.count += 1;
818 debug("Tapping: Tap press("); debug_dec(keyp->tap.count); debug(")\n");
819 process_action(keyp);
823 } else if (event.pressed && is_tap_key(event.key)) {
824 // Sequential tap can be interfered with other tap key.
825 debug("Tapping: Start with interfering other tap.\n");
827 waiting_buffer_scan_tap();
831 if (!IS_NOEVENT(keyp->event)) debug("Tapping: other key just after tap.\n");
832 process_action(keyp);
836 // timeout. no sequential tap.
837 debug("Tapping: End(Timeout after releasing last tap): ");
838 debug_event(event); debug("\n");
839 tapping_key = (keyrecord_t){};
844 // not tapping satate
846 if (event.pressed && is_tap_key(event.key)) {
847 debug("Tapping: Start(Press tap key).\n");
849 waiting_buffer_scan_tap();
853 process_action(keyp);
859 /* scan buffer for tapping */
860 static void waiting_buffer_scan_tap(void)
862 // tapping already is settled
863 if (tapping_key.tap.count > 0) return;
864 // invalid state: tapping_key released && tap.count == 0
865 if (!tapping_key.event.pressed) return;
867 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
868 if (IS_TAPPING_KEY(waiting_buffer[i].event.key) &&
869 !waiting_buffer[i].event.pressed &&
870 WITHIN_TAPPING_TERM(waiting_buffer[i].event)) {
871 tapping_key.tap.count = 1;
872 waiting_buffer[i].tap.count = 1;
873 process_action(&tapping_key);
875 debug("waiting_buffer_scan_tap: found at ["); debug_dec(i); debug("]\n");
876 debug_waiting_buffer();
885 * Utilities for actions.
887 void register_code(uint8_t code)
892 else if IS_KEY(code) {
893 // TODO: should push command_proc out of this block?
894 if (command_proc(code)) return;
896 if (oneshot_state.mods && oneshot_state.ready && !oneshot_state.disabled) {
897 uint8_t tmp_mods = host_get_mods();
898 host_add_mods(oneshot_state.mods);
900 host_send_keyboard_report();
902 host_set_mods(tmp_mods);
903 oneshot_state.ready = false;
906 host_send_keyboard_report();
909 else if IS_MOD(code) {
910 host_add_mods(MOD_BIT(code));
911 host_send_keyboard_report();
915 void unregister_code(uint8_t code)
919 host_send_keyboard_report();
921 else if IS_MOD(code) {
922 host_del_mods(MOD_BIT(code));
923 host_send_keyboard_report();
927 void add_mods(uint8_t mods)
931 host_send_keyboard_report();
935 void del_mods(uint8_t mods)
939 host_send_keyboard_report();
943 void set_mods(uint8_t mods)
946 host_send_keyboard_report();
949 void clear_keyboard(void)
952 clear_keyboard_but_mods();
955 void clear_keyboard_but_mods(void)
958 host_send_keyboard_report();
959 #ifdef MOUSEKEY_ENABLE
963 #ifdef EXTRAKEY_ENABLE
965 host_consumer_send(0);
969 bool sending_anykey(void)
971 return (host_has_anykey() || host_mouse_in_use() ||
972 host_last_sysytem_report() || host_last_consumer_report());
975 bool is_tap_key(key_t key)
977 action_t action = layer_switch_get_action(key);
979 switch (action.kind.id) {
985 switch (action.layer.code) {
986 case 0x04 ... 0xEF: /* tap key */
994 if (action.func.opt & FUNC_TAP) { return true; }
1004 static void debug_event(keyevent_t event)
1006 debug_hex16((event.key.row<<8) | event.key.col);
1007 if (event.pressed) debug("d("); else debug("u(");
1008 debug_dec(event.time); debug(")");
1010 static void debug_record(keyrecord_t record)
1012 debug_event(record.event); debug(":"); debug_dec(record.tap.count);
1013 if (record.tap.interrupted) debug("-");
1015 static void debug_action(action_t action)
1017 switch (action.kind.id) {
1018 case ACT_LMODS: debug("ACT_LMODS"); break;
1019 case ACT_RMODS: debug("ACT_RMODS"); break;
1020 case ACT_LMODS_TAP: debug("ACT_LMODS_TAP"); break;
1021 case ACT_RMODS_TAP: debug("ACT_RMODS_TAP"); break;
1022 case ACT_USAGE: debug("ACT_USAGE"); break;
1023 case ACT_MOUSEKEY: debug("ACT_MOUSEKEY"); break;
1024 case ACT_KEYMAP: debug("ACT_KEYMAP"); break;
1025 case ACT_OVERLAY: debug("ACT_OVERLAY"); break;
1026 case ACT_MACRO: debug("ACT_MACRO"); break;
1027 case ACT_COMMAND: debug("ACT_COMMAND"); break;
1028 case ACT_FUNCTION: debug("ACT_FUNCTION"); break;
1029 default: debug("UNKNOWN"); break;
1032 debug_hex4(action.kind.param>>8);
1034 debug_hex8(action.kind.param & 0xff);
1037 static void debug_tapping_key(void)
1039 debug("TAPPING_KEY="); debug_record(tapping_key); debug("\n");
1041 static void debug_waiting_buffer(void)
1044 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
1045 debug("["); debug_dec(i); debug("]="); debug_record(waiting_buffer[i]); debug(" ");