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) {
373 if (!event.pressed) {
384 /* Keymap Reset default layer */
385 case (OP_RESET | ON_PRESS):
387 default_layer_set(action.layer.val);
390 case (OP_RESET | ON_RELEASE):
391 if (!event.pressed) {
392 default_layer_set(action.layer.val);
395 case (OP_RESET | ON_BOTH):
396 default_layer_set(action.layer.val);
399 /* Keymap Bit invert */
401 /* with tap toggle */
403 if (tap_count < TAPPING_TOGGLE) {
404 debug("KEYMAP_INV: tap toggle(press).\n");
405 keymap_invert(action.layer.val);
408 if (tap_count <= TAPPING_TOGGLE) {
409 debug("KEYMAP_INV: tap toggle(release).\n");
410 keymap_invert(action.layer.val);
414 case (OP_INV | ON_PRESS):
416 keymap_invert(action.layer.val);
419 case (OP_INV | ON_RELEASE):
420 if (!event.pressed) {
421 keymap_invert(action.layer.val);
424 case (OP_INV | ON_BOTH):
425 keymap_invert(action.layer.val);
431 keymap_on(action.layer.val);
433 keymap_off(action.layer.val);
436 case (OP_ON | ON_PRESS):
438 keymap_on(action.layer.val);
441 case (OP_ON | ON_RELEASE):
442 if (!event.pressed) {
443 keymap_on(action.layer.val);
446 case (OP_ON | ON_BOTH):
447 keymap_on(action.layer.val);
453 keymap_off(action.layer.val);
455 keymap_on(action.layer.val);
458 case (OP_OFF | ON_PRESS):
460 keymap_off(action.layer.val);
463 case (OP_OFF | ON_RELEASE):
464 if (!event.pressed) {
465 keymap_off(action.layer.val);
468 case (OP_OFF | ON_BOTH):
469 keymap_off(action.layer.val);
475 keymap_set(action.layer.val);
480 case (OP_SET | ON_PRESS):
482 keymap_set(action.layer.val);
485 case (OP_SET | ON_RELEASE):
486 if (!event.pressed) {
487 keymap_set(action.layer.val);
490 case (OP_SET | ON_BOTH):
491 keymap_set(action.layer.val);
494 /* Keymap Bit invert with tap key */
498 debug("KEYMAP_TAP_KEY: Tap: register_code\n");
499 register_code(action.layer.code);
501 debug("KEYMAP_TAP_KEY: No tap: On on press\n");
502 keymap_on(action.layer.val);
506 debug("KEYMAP_TAP_KEY: Tap: unregister_code\n");
507 unregister_code(action.layer.code);
509 debug("KEYMAP_TAP_KEY: No tap: Off on release\n");
510 keymap_off(action.layer.val);
518 switch (action.layer.code) {
519 // Overlay Invert bit4
521 if (action.layer.val == 0) {
524 overlay_set(overlay_stat ^ action.layer.val);
528 if (action.layer.val == 0) {
529 if (event.pressed) overlay_clear();
531 overlay_set(overlay_stat ^ action.layer.val<<4);
535 if (action.layer.val == 0) {
536 if (!event.pressed) overlay_clear();
538 overlay_set(overlay_stat ^ action.layer.val<<8);
542 if (action.layer.val == 0) {
545 overlay_set(overlay_stat ^ action.layer.val<<12);
549 /* Overlay Bit invert */
551 /* with tap toggle */
553 if (tap_count < TAPPING_TOGGLE) {
554 debug("OVERLAY_INV: tap toggle(press).\n");
555 overlay_invert(action.layer.val);
558 if (tap_count <= TAPPING_TOGGLE) {
559 debug("OVERLAY_INV: tap toggle(release).\n");
560 overlay_invert(action.layer.val);
564 case (OP_INV | ON_PRESS):
566 overlay_invert(action.layer.val);
569 case (OP_INV | ON_RELEASE):
570 if (!event.pressed) {
571 overlay_invert(action.layer.val);
574 case (OP_INV | ON_BOTH):
575 overlay_invert(action.layer.val);
581 overlay_on(action.layer.val);
583 overlay_off(action.layer.val);
586 case (OP_ON | ON_PRESS):
588 overlay_on(action.layer.val);
591 case (OP_ON | ON_RELEASE):
592 if (!event.pressed) {
593 overlay_on(action.layer.val);
596 case (OP_ON | ON_BOTH):
597 overlay_on(action.layer.val);
600 /* Overlay Bit off */
603 overlay_off(action.layer.val);
605 overlay_on(action.layer.val);
608 case (OP_OFF | ON_PRESS):
610 overlay_off(action.layer.val);
613 case (OP_OFF | ON_RELEASE):
614 if (!event.pressed) {
615 overlay_off(action.layer.val);
618 case (OP_OFF | ON_BOTH):
619 overlay_off(action.layer.val);
622 /* Overlay Bit set */
625 overlay_move(action.layer.val);
630 case (OP_SET | ON_PRESS):
632 overlay_move(action.layer.val);
635 case (OP_SET | ON_RELEASE):
636 if (!event.pressed) {
637 overlay_move(action.layer.val);
640 case (OP_SET | ON_BOTH):
641 overlay_move(action.layer.val);
644 /* Overlay Bit invert with tap key */
648 debug("OVERLAY_TAP_KEY: Tap: register_code\n");
649 register_code(action.layer.code);
651 debug("OVERLAY_TAP_KEY: No tap: On on press\n");
652 overlay_on(action.layer.val);
656 debug("OVERLAY_TAP_KEY: Tap: unregister_code\n");
657 unregister_code(action.layer.code);
659 debug("OVERLAY_TAP_KEY: No tap: Off on release\n");
660 overlay_off(action.layer.val);
669 action_macro_play(action_get_macro(record, action.func.id, action.func.opt));
674 action_function(record, action.func.id, action.func.opt);
683 * Rule: Tap key is typed(pressed and released) within TAPPING_TERM.
684 * (without interfering by typing other key)
686 /* return true when key event is processed or consumed. */
687 static bool process_tapping(keyrecord_t *keyp)
689 keyevent_t event = keyp->event;
692 if (IS_TAPPING_PRESSED()) {
693 if (WITHIN_TAPPING_TERM(event)) {
694 if (tapping_key.tap.count == 0) {
695 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
697 debug("Tapping: First tap(0->1).\n");
698 tapping_key.tap.count = 1;
699 tapping_key.tap.interrupted = (waiting_buffer_has_anykey_pressed() ? true : false);
701 process_action(&tapping_key);
704 keyp->tap = tapping_key.tap;
707 #if TAPPING_TERM >= 500
708 /* This can prevent from typing some tap keys in a row at a time. */
709 else if (!event.pressed && waiting_buffer_typed(event)) {
710 // other key typed. not tap.
711 debug("Tapping: End. No tap. Interfered by typing key\n");
712 process_action(&tapping_key);
713 tapping_key = (keyrecord_t){};
721 // other key events shall be enq'd till tapping state settles.
727 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
728 debug("Tapping: Tap release("); debug_dec(tapping_key.tap.count); debug(")\n");
729 keyp->tap = tapping_key.tap;
730 process_action(keyp);
735 else if (is_tap_key(keyp->event.key) && event.pressed) {
736 if (tapping_key.tap.count > 1) {
737 debug("Tapping: Start new tap with releasing last tap(>1).\n");
739 process_action(&(keyrecord_t){
740 .tap = tapping_key.tap,
741 .event.key = tapping_key.event.key,
742 .event.time = event.time,
743 .event.pressed = false
746 debug("Tapping: Start while last tap(1).\n");
749 waiting_buffer_scan_tap();
754 if (!IS_NOEVENT(keyp->event)) {
755 debug("Tapping: key event while last tap(>0).\n");
757 process_action(keyp);
762 // after TAPPING_TERM
764 if (tapping_key.tap.count == 0) {
765 debug("Tapping: End. Timeout. Not tap(0): ");
766 debug_event(event); debug("\n");
767 process_action(&tapping_key);
768 tapping_key = (keyrecord_t){};
772 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
773 debug("Tapping: End. last timeout tap release(>0).");
774 keyp->tap = tapping_key.tap;
775 process_action(keyp);
776 tapping_key = (keyrecord_t){};
779 else if (is_tap_key(keyp->event.key) && event.pressed) {
780 if (tapping_key.tap.count > 1) {
781 debug("Tapping: Start new tap with releasing last timeout tap(>1).\n");
783 process_action(&(keyrecord_t){
784 .tap = tapping_key.tap,
785 .event.key = tapping_key.event.key,
786 .event.time = event.time,
787 .event.pressed = false
790 debug("Tapping: Start while last timeout tap(1).\n");
793 waiting_buffer_scan_tap();
798 if (!IS_NOEVENT(keyp->event)) {
799 debug("Tapping: key event while last timeout tap(>0).\n");
801 process_action(keyp);
806 } else if (IS_TAPPING_RELEASED()) {
807 if (WITHIN_TAPPING_TERM(event)) {
808 if (tapping_key.tap.count > 0 && IS_TAPPING_KEY(event.key) && event.pressed) {
810 keyp->tap = tapping_key.tap;
811 keyp->tap.count += 1;
812 debug("Tapping: Tap press("); debug_dec(keyp->tap.count); debug(")\n");
813 process_action(keyp);
817 } else if (event.pressed && is_tap_key(event.key)) {
818 // Sequential tap can be interfered with other tap key.
819 debug("Tapping: Start with interfering other tap.\n");
821 waiting_buffer_scan_tap();
825 if (!IS_NOEVENT(keyp->event)) debug("Tapping: other key just after tap.\n");
826 process_action(keyp);
830 // timeout. no sequential tap.
831 debug("Tapping: End(Timeout after releasing last tap): ");
832 debug_event(event); debug("\n");
833 tapping_key = (keyrecord_t){};
838 // not tapping satate
840 if (event.pressed && is_tap_key(event.key)) {
841 debug("Tapping: Start(Press tap key).\n");
843 waiting_buffer_scan_tap();
847 process_action(keyp);
853 /* scan buffer for tapping */
854 static void waiting_buffer_scan_tap(void)
856 // tapping already is settled
857 if (tapping_key.tap.count > 0) return;
858 // invalid state: tapping_key released && tap.count == 0
859 if (!tapping_key.event.pressed) return;
861 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
862 if (IS_TAPPING_KEY(waiting_buffer[i].event.key) &&
863 !waiting_buffer[i].event.pressed &&
864 WITHIN_TAPPING_TERM(waiting_buffer[i].event)) {
865 tapping_key.tap.count = 1;
866 waiting_buffer[i].tap.count = 1;
867 process_action(&tapping_key);
869 debug("waiting_buffer_scan_tap: found at ["); debug_dec(i); debug("]\n");
870 debug_waiting_buffer();
879 * Utilities for actions.
881 void register_code(uint8_t code)
886 else if IS_KEY(code) {
887 // TODO: should push command_proc out of this block?
888 if (command_proc(code)) return;
890 if (oneshot_state.mods && oneshot_state.ready && !oneshot_state.disabled) {
891 uint8_t tmp_mods = host_get_mods();
892 host_add_mods(oneshot_state.mods);
894 host_send_keyboard_report();
896 host_set_mods(tmp_mods);
897 oneshot_state.ready = false;
900 host_send_keyboard_report();
903 else if IS_MOD(code) {
904 host_add_mods(MOD_BIT(code));
905 host_send_keyboard_report();
909 void unregister_code(uint8_t code)
913 host_send_keyboard_report();
915 else if IS_MOD(code) {
916 host_del_mods(MOD_BIT(code));
917 host_send_keyboard_report();
921 void add_mods(uint8_t mods)
925 host_send_keyboard_report();
929 void del_mods(uint8_t mods)
933 host_send_keyboard_report();
937 void set_mods(uint8_t mods)
940 host_send_keyboard_report();
943 void clear_keyboard(void)
946 clear_keyboard_but_mods();
949 void clear_keyboard_but_mods(void)
952 host_send_keyboard_report();
953 #ifdef MOUSEKEY_ENABLE
957 #ifdef EXTRAKEY_ENABLE
959 host_consumer_send(0);
963 bool sending_anykey(void)
965 return (host_has_anykey() || host_mouse_in_use() ||
966 host_last_sysytem_report() || host_last_consumer_report());
969 bool is_tap_key(key_t key)
971 action_t action = layer_switch_get_action(key);
973 switch (action.kind.id) {
979 switch (action.layer.code) {
980 case 0x04 ... 0xEF: /* tap key */
988 if (action.func.opt & FUNC_TAP) { return true; }
998 static void debug_event(keyevent_t event)
1000 debug_hex16((event.key.row<<8) | event.key.col);
1001 if (event.pressed) debug("d("); else debug("u(");
1002 debug_dec(event.time); debug(")");
1004 static void debug_record(keyrecord_t record)
1006 debug_event(record.event); debug(":"); debug_dec(record.tap.count);
1007 if (record.tap.interrupted) debug("-");
1009 static void debug_action(action_t action)
1011 switch (action.kind.id) {
1012 case ACT_LMODS: debug("ACT_LMODS"); break;
1013 case ACT_RMODS: debug("ACT_RMODS"); break;
1014 case ACT_LMODS_TAP: debug("ACT_LMODS_TAP"); break;
1015 case ACT_RMODS_TAP: debug("ACT_RMODS_TAP"); break;
1016 case ACT_USAGE: debug("ACT_USAGE"); break;
1017 case ACT_MOUSEKEY: debug("ACT_MOUSEKEY"); break;
1018 case ACT_KEYMAP: debug("ACT_KEYMAP"); break;
1019 case ACT_OVERLAY: debug("ACT_OVERLAY"); break;
1020 case ACT_MACRO: debug("ACT_MACRO"); break;
1021 case ACT_COMMAND: debug("ACT_COMMAND"); break;
1022 case ACT_FUNCTION: debug("ACT_FUNCTION"); break;
1023 default: debug("UNKNOWN"); break;
1026 debug_hex4(action.kind.param>>8);
1028 debug_hex8(action.kind.param & 0xff);
1031 static void debug_tapping_key(void)
1033 debug("TAPPING_KEY="); debug_record(tapping_key); debug("\n");
1035 static void debug_waiting_buffer(void)
1038 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
1039 debug("["); debug_dec(i); debug("]="); debug_record(waiting_buffer[i]); debug(" ");