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 #ifndef NO_ACTION_TAPPING
35 static bool process_tapping(keyrecord_t *record);
36 static void waiting_buffer_scan_tap(void);
39 static void debug_event(keyevent_t event);
40 static void debug_record(keyrecord_t record);
41 static void debug_action(action_t action);
42 #ifndef NO_ACTION_TAPPING
43 static void debug_tapping_key(void);
44 static void debug_waiting_buffer(void);
51 /* period of tapping(ms) */
53 #define TAPPING_TERM 200
56 /* tap count needed for toggling a feature */
57 #ifndef TAPPING_TOGGLE
58 #define TAPPING_TOGGLE 5
61 #ifndef NO_ACTION_TAPPING
62 /* stores a key event of current tap. */
63 static keyrecord_t tapping_key = {};
65 #define IS_TAPPING() !IS_NOEVENT(tapping_key.event)
66 #define IS_TAPPING_PRESSED() (IS_TAPPING() && tapping_key.event.pressed)
67 #define IS_TAPPING_RELEASED() (IS_TAPPING() && !tapping_key.event.pressed)
68 #define IS_TAPPING_KEY(k) (IS_TAPPING() && KEYEQ(tapping_key.event.key, (k)))
69 #define WITHIN_TAPPING_TERM(e) (TIMER_DIFF_16(e.time, tapping_key.event.time) < TAPPING_TERM)
75 * stores key events waiting for settling current tap.
77 #define WAITING_BUFFER_SIZE 8
78 static keyrecord_t waiting_buffer[WAITING_BUFFER_SIZE] = {};
80 /* point to empty cell to enq */
81 static uint8_t waiting_buffer_head = 0;
83 /* point to the oldest data cell to deq */
84 static uint8_t waiting_buffer_tail = 0;
86 static bool waiting_buffer_enq(keyrecord_t record)
88 if (IS_NOEVENT(record.event)) {
92 if ((waiting_buffer_head + 1) % WAITING_BUFFER_SIZE == waiting_buffer_tail) {
93 debug("waiting_buffer_enq: Over flow.\n");
97 waiting_buffer[waiting_buffer_head] = record;
98 waiting_buffer_head = (waiting_buffer_head + 1) % WAITING_BUFFER_SIZE;
100 debug("waiting_buffer_enq: "); debug_waiting_buffer();
104 static void waiting_buffer_clear(void)
106 waiting_buffer_head = 0;
107 waiting_buffer_tail = 0;
110 #if TAPPING_TERM >= 500
111 static bool waiting_buffer_typed(keyevent_t event)
113 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
114 if (KEYEQ(event.key, waiting_buffer[i].event.key) && event.pressed != waiting_buffer[i].event.pressed) {
122 bool waiting_buffer_has_anykey_pressed(void)
124 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
125 if (waiting_buffer[i].event.pressed) return true;
132 void action_exec(keyevent_t event)
134 if (!IS_NOEVENT(event)) {
135 debug("\n---- action_exec: start -----\n");
136 debug("EVENT: "); debug_event(event); debug("\n");
139 keyrecord_t record = { .event = event };
141 #ifndef NO_ACTION_TAPPING
142 // pre-process on tapping
143 if (process_tapping(&record)) {
144 if (!IS_NOEVENT(record.event)) {
145 debug("processed: "); debug_record(record); debug("\n");
149 if (!waiting_buffer_enq(record)) {
150 // clear all in case of overflow.
151 debug("OVERFLOW: CLEAR ALL STATES\n");
153 waiting_buffer_clear();
154 tapping_key = (keyrecord_t){};
158 // process waiting_buffer
159 if (!IS_NOEVENT(event) && waiting_buffer_head != waiting_buffer_tail) {
160 debug("---- action_exec: process waiting_buffer -----\n");
163 for (; waiting_buffer_tail != waiting_buffer_head; waiting_buffer_tail = (waiting_buffer_tail + 1) % WAITING_BUFFER_SIZE) {
164 if (process_tapping(&waiting_buffer[waiting_buffer_tail])) {
165 debug("processed: waiting_buffer["); debug_dec(waiting_buffer_tail); debug("] = ");
166 debug_record(waiting_buffer[waiting_buffer_tail]); debug("\n\n");
171 if (!IS_NOEVENT(event)) {
175 process_action(&record);
176 if (!IS_NOEVENT(record.event)) {
177 debug("processed: "); debug_record(record); debug("\n");
182 static void process_action(keyrecord_t *record)
184 keyevent_t event = record->event;
185 uint8_t tap_count = record->tap.count;
187 if (IS_NOEVENT(event)) { return; }
189 action_t action = layer_switch_get_action(event.key);
190 debug("ACTION: "); debug_action(action);
191 debug(" overlays: "); overlay_debug();
192 debug(" keymaps: "); keymap_debug();
193 debug(" default_layer: "); debug_dec(default_layer); debug("\n");
195 switch (action.kind.id) {
200 uint8_t mods = (action.kind.id == ACT_LMODS) ? action.key.mods :
203 uint8_t tmp_mods = host_get_mods();
206 host_send_keyboard_report();
208 register_code(action.key.code);
209 if (mods && action.key.code) {
210 host_set_mods(tmp_mods);
211 host_send_keyboard_report();
214 if (mods && !action.key.code) {
216 host_send_keyboard_report();
218 unregister_code(action.key.code);
222 #ifndef NO_ACTION_TAPPING
226 uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods :
228 switch (action.layer.code) {
229 #ifndef NO_ACTION_ONESHOT
233 if (tap_count == 0) {
234 debug("MODS_TAP: Oneshot: add_mods\n");
237 else if (tap_count == 1) {
238 debug("MODS_TAP: Oneshot: start\n");
241 else if (tap_count == TAPPING_TOGGLE) {
242 debug("MODS_TAP: Oneshot: toggle\n");
246 debug("MODS_TAP: Oneshot: cancel&add_mods\n");
247 // double tap cancels oneshot and works as normal modifier.
252 if (tap_count == 0) {
253 debug("MODS_TAP: Oneshot: cancel/del_mods\n");
254 // cancel oneshot on hold
258 else if (tap_count == 1) {
259 debug("MODS_TAP: Oneshot: del_mods\n");
264 debug("MODS_TAP: Oneshot: del_mods\n");
274 if (waiting_buffer_has_anykey_pressed()) {
275 debug("MODS_TAP: Tap: Cancel: add_mods\n");
276 // ad hoc: set 0 to cancel tap
277 record->tap.count = 0;
280 debug("MODS_TAP: Tap: register_code\n");
281 register_code(action.key.code);
284 debug("MODS_TAP: No tap: add_mods\n");
289 debug("MODS_TAP: Tap: unregister_code\n");
290 unregister_code(action.key.code);
292 debug("MODS_TAP: No tap: add_mods\n");
302 #ifdef EXTRAKEY_ENABLE
303 /* other HID usage */
305 switch (action.usage.page) {
308 host_system_send(action.usage.code);
315 host_consumer_send(action.usage.code);
317 host_consumer_send(0);
324 #ifdef MOUSEKEY_ENABLE
328 mousekey_on(action.key.code);
331 mousekey_off(action.key.code);
337 #ifndef NO_ACTION_KEYMAP
339 switch (action.layer.code) {
342 switch (action.layer.val & 0x03) {
355 if (!event.pressed) {
364 /* NOTE: 4-7 rserved */
367 /* Keymap Reset default layer */
368 case (OP_RESET | ON_PRESS):
370 default_layer_set(action.layer.val);
373 case (OP_RESET | ON_RELEASE):
374 if (!event.pressed) {
375 default_layer_set(action.layer.val);
378 case (OP_RESET | ON_BOTH):
379 default_layer_set(action.layer.val);
382 /* Keymap Bit invert */
384 /* with tap toggle */
386 if (tap_count < TAPPING_TOGGLE) {
387 debug("KEYMAP_INV: tap toggle(press).\n");
388 keymap_invert(action.layer.val);
391 if (tap_count <= TAPPING_TOGGLE) {
392 debug("KEYMAP_INV: tap toggle(release).\n");
393 keymap_invert(action.layer.val);
397 case (OP_INV | ON_PRESS):
399 keymap_invert(action.layer.val);
402 case (OP_INV | ON_RELEASE):
403 if (!event.pressed) {
404 keymap_invert(action.layer.val);
407 case (OP_INV | ON_BOTH):
408 keymap_invert(action.layer.val);
414 keymap_on(action.layer.val);
416 keymap_off(action.layer.val);
419 case (OP_ON | ON_PRESS):
421 keymap_on(action.layer.val);
424 case (OP_ON | ON_RELEASE):
425 if (!event.pressed) {
426 keymap_on(action.layer.val);
429 case (OP_ON | ON_BOTH):
430 keymap_on(action.layer.val);
436 keymap_off(action.layer.val);
438 keymap_on(action.layer.val);
441 case (OP_OFF | ON_PRESS):
443 keymap_off(action.layer.val);
446 case (OP_OFF | ON_RELEASE):
447 if (!event.pressed) {
448 keymap_off(action.layer.val);
451 case (OP_OFF | ON_BOTH):
452 keymap_off(action.layer.val);
458 keymap_set(action.layer.val);
463 case (OP_SET | ON_PRESS):
465 keymap_set(action.layer.val);
468 case (OP_SET | ON_RELEASE):
469 if (!event.pressed) {
470 keymap_set(action.layer.val);
473 case (OP_SET | ON_BOTH):
474 keymap_set(action.layer.val);
477 /* Keymap Bit invert with tap key */
481 debug("KEYMAP_TAP_KEY: Tap: register_code\n");
482 register_code(action.layer.code);
484 debug("KEYMAP_TAP_KEY: No tap: On on press\n");
485 keymap_on(action.layer.val);
489 debug("KEYMAP_TAP_KEY: Tap: unregister_code\n");
490 unregister_code(action.layer.code);
492 debug("KEYMAP_TAP_KEY: No tap: Off on release\n");
493 keymap_off(action.layer.val);
501 #ifndef NO_ACTION_OVERLAY
503 switch (action.layer.code) {
504 // Overlay Invert bit4
506 if (action.layer.val == 0) {
507 // NOTE: reserved for future use
510 overlay_set(overlay_stat ^ action.layer.val);
514 if (action.layer.val == 0) {
516 if (event.pressed) overlay_clear();
518 overlay_set(overlay_stat ^ action.layer.val<<4);
522 if (action.layer.val == 0) {
524 if (!event.pressed) overlay_clear();
526 overlay_set(overlay_stat ^ action.layer.val<<8);
530 if (action.layer.val == 0) {
534 overlay_set(overlay_stat ^ action.layer.val<<12);
538 /* Overlay Bit invert */
540 /* with tap toggle */
542 if (tap_count < TAPPING_TOGGLE) {
543 debug("OVERLAY_INV: tap toggle(press).\n");
544 overlay_invert(action.layer.val);
547 if (tap_count <= TAPPING_TOGGLE) {
548 debug("OVERLAY_INV: tap toggle(release).\n");
549 overlay_invert(action.layer.val);
553 case (OP_INV | ON_PRESS):
555 overlay_invert(action.layer.val);
558 case (OP_INV | ON_RELEASE):
559 if (!event.pressed) {
560 overlay_invert(action.layer.val);
563 case (OP_INV | ON_BOTH):
564 overlay_invert(action.layer.val);
570 overlay_on(action.layer.val);
572 overlay_off(action.layer.val);
575 case (OP_ON | ON_PRESS):
577 overlay_on(action.layer.val);
580 case (OP_ON | ON_RELEASE):
581 if (!event.pressed) {
582 overlay_on(action.layer.val);
585 case (OP_ON | ON_BOTH):
586 overlay_on(action.layer.val);
589 /* Overlay Bit off */
592 overlay_off(action.layer.val);
594 overlay_on(action.layer.val);
597 case (OP_OFF | ON_PRESS):
599 overlay_off(action.layer.val);
602 case (OP_OFF | ON_RELEASE):
603 if (!event.pressed) {
604 overlay_off(action.layer.val);
607 case (OP_OFF | ON_BOTH):
608 overlay_off(action.layer.val);
611 /* Overlay Bit set */
614 overlay_move(action.layer.val);
619 case (OP_SET | ON_PRESS):
621 overlay_move(action.layer.val);
624 case (OP_SET | ON_RELEASE):
625 if (!event.pressed) {
626 overlay_move(action.layer.val);
629 case (OP_SET | ON_BOTH):
630 overlay_move(action.layer.val);
633 /* Overlay Bit invert with tap key */
637 debug("OVERLAY_TAP_KEY: Tap: register_code\n");
638 register_code(action.layer.code);
640 debug("OVERLAY_TAP_KEY: No tap: On on press\n");
641 overlay_on(action.layer.val);
645 debug("OVERLAY_TAP_KEY: Tap: unregister_code\n");
646 unregister_code(action.layer.code);
648 debug("OVERLAY_TAP_KEY: No tap: Off on release\n");
649 overlay_off(action.layer.val);
658 #ifndef NO_ACTION_MACRO
660 action_macro_play(action_get_macro(record, action.func.id, action.func.opt));
665 #ifndef NO_ACTION_FUNCTION
667 action_function(record, action.func.id, action.func.opt);
675 #ifndef NO_ACTION_TAPPING
678 * Rule: Tap key is typed(pressed and released) within TAPPING_TERM.
679 * (without interfering by typing other key)
681 /* return true when key event is processed or consumed. */
682 static bool process_tapping(keyrecord_t *keyp)
684 keyevent_t event = keyp->event;
687 if (IS_TAPPING_PRESSED()) {
688 if (WITHIN_TAPPING_TERM(event)) {
689 if (tapping_key.tap.count == 0) {
690 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
692 debug("Tapping: First tap(0->1).\n");
693 tapping_key.tap.count = 1;
694 tapping_key.tap.interrupted = (waiting_buffer_has_anykey_pressed() ? true : false);
696 process_action(&tapping_key);
699 keyp->tap = tapping_key.tap;
702 #if TAPPING_TERM >= 500
703 /* This can prevent from typing some tap keys in a row at a time. */
704 else if (!event.pressed && waiting_buffer_typed(event)) {
705 // other key typed. not tap.
706 debug("Tapping: End. No tap. Interfered by typing key\n");
707 process_action(&tapping_key);
708 tapping_key = (keyrecord_t){};
716 // other key events shall be enq'd till tapping state settles.
722 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
723 debug("Tapping: Tap release("); debug_dec(tapping_key.tap.count); debug(")\n");
724 keyp->tap = tapping_key.tap;
725 process_action(keyp);
730 else if (is_tap_key(keyp->event.key) && event.pressed) {
731 if (tapping_key.tap.count > 1) {
732 debug("Tapping: Start new tap with releasing last tap(>1).\n");
734 process_action(&(keyrecord_t){
735 .tap = tapping_key.tap,
736 .event.key = tapping_key.event.key,
737 .event.time = event.time,
738 .event.pressed = false
741 debug("Tapping: Start while last tap(1).\n");
744 waiting_buffer_scan_tap();
749 if (!IS_NOEVENT(keyp->event)) {
750 debug("Tapping: key event while last tap(>0).\n");
752 process_action(keyp);
757 // after TAPPING_TERM
759 if (tapping_key.tap.count == 0) {
760 debug("Tapping: End. Timeout. Not tap(0): ");
761 debug_event(event); debug("\n");
762 process_action(&tapping_key);
763 tapping_key = (keyrecord_t){};
767 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
768 debug("Tapping: End. last timeout tap release(>0).");
769 keyp->tap = tapping_key.tap;
770 process_action(keyp);
771 tapping_key = (keyrecord_t){};
774 else if (is_tap_key(keyp->event.key) && event.pressed) {
775 if (tapping_key.tap.count > 1) {
776 debug("Tapping: Start new tap with releasing last timeout tap(>1).\n");
778 process_action(&(keyrecord_t){
779 .tap = tapping_key.tap,
780 .event.key = tapping_key.event.key,
781 .event.time = event.time,
782 .event.pressed = false
785 debug("Tapping: Start while last timeout tap(1).\n");
788 waiting_buffer_scan_tap();
793 if (!IS_NOEVENT(keyp->event)) {
794 debug("Tapping: key event while last timeout tap(>0).\n");
796 process_action(keyp);
801 } else if (IS_TAPPING_RELEASED()) {
802 if (WITHIN_TAPPING_TERM(event)) {
803 if (tapping_key.tap.count > 0 && IS_TAPPING_KEY(event.key) && event.pressed) {
805 keyp->tap = tapping_key.tap;
806 keyp->tap.count += 1;
807 debug("Tapping: Tap press("); debug_dec(keyp->tap.count); debug(")\n");
808 process_action(keyp);
812 } else if (event.pressed && is_tap_key(event.key)) {
813 // Sequential tap can be interfered with other tap key.
814 debug("Tapping: Start with interfering other tap.\n");
816 waiting_buffer_scan_tap();
820 if (!IS_NOEVENT(keyp->event)) debug("Tapping: other key just after tap.\n");
821 process_action(keyp);
825 // timeout. no sequential tap.
826 debug("Tapping: End(Timeout after releasing last tap): ");
827 debug_event(event); debug("\n");
828 tapping_key = (keyrecord_t){};
833 // not tapping satate
835 if (event.pressed && is_tap_key(event.key)) {
836 debug("Tapping: Start(Press tap key).\n");
838 waiting_buffer_scan_tap();
842 process_action(keyp);
848 /* scan buffer for tapping */
849 static void waiting_buffer_scan_tap(void)
851 // tapping already is settled
852 if (tapping_key.tap.count > 0) return;
853 // invalid state: tapping_key released && tap.count == 0
854 if (!tapping_key.event.pressed) return;
856 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
857 if (IS_TAPPING_KEY(waiting_buffer[i].event.key) &&
858 !waiting_buffer[i].event.pressed &&
859 WITHIN_TAPPING_TERM(waiting_buffer[i].event)) {
860 tapping_key.tap.count = 1;
861 waiting_buffer[i].tap.count = 1;
862 process_action(&tapping_key);
864 debug("waiting_buffer_scan_tap: found at ["); debug_dec(i); debug("]\n");
865 debug_waiting_buffer();
875 * Utilities for actions.
877 void register_code(uint8_t code)
882 #ifdef CAPSLOCK_LOCKING_ENABLE
883 else if (KC_LOCKING_CAPS == code) {
884 #ifdef CAPSLOCK_LOCKING_RESYNC_ENABLE
885 // Resync: ignore if caps lock already is on
886 if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) return;
888 host_add_key(KC_CAPSLOCK);
889 host_send_keyboard_report();
890 host_del_key(KC_CAPSLOCK);
891 host_send_keyboard_report();
894 else if IS_KEY(code) {
895 // TODO: should push command_proc out of this block?
896 if (command_proc(code)) return;
898 #ifndef NO_ACTION_ONESHOT
899 if (oneshot_state.mods && !oneshot_state.disabled) {
900 uint8_t tmp_mods = host_get_mods();
901 host_add_mods(oneshot_state.mods);
904 host_send_keyboard_report();
906 host_set_mods(tmp_mods);
912 host_send_keyboard_report();
915 else if IS_MOD(code) {
916 host_add_mods(MOD_BIT(code));
917 host_send_keyboard_report();
921 void unregister_code(uint8_t code)
926 #ifdef CAPSLOCK_LOCKING_ENABLE
927 else if (KC_LOCKING_CAPS == code) {
928 #ifdef CAPSLOCK_LOCKING_RESYNC_ENABLE
929 // Resync: ignore if caps lock already is off
930 if (!(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK))) return;
932 host_add_key(KC_CAPSLOCK);
933 host_send_keyboard_report();
934 host_del_key(KC_CAPSLOCK);
935 host_send_keyboard_report();
938 else if IS_KEY(code) {
940 host_send_keyboard_report();
942 else if IS_MOD(code) {
943 host_del_mods(MOD_BIT(code));
944 host_send_keyboard_report();
948 void add_mods(uint8_t mods)
952 host_send_keyboard_report();
956 void del_mods(uint8_t mods)
960 host_send_keyboard_report();
964 void set_mods(uint8_t mods)
967 host_send_keyboard_report();
970 void clear_keyboard(void)
973 clear_keyboard_but_mods();
976 void clear_keyboard_but_mods(void)
979 host_send_keyboard_report();
980 #ifdef MOUSEKEY_ENABLE
984 #ifdef EXTRAKEY_ENABLE
986 host_consumer_send(0);
990 bool sending_anykey(void)
992 return (host_has_anykey() || host_mouse_in_use() ||
993 host_last_sysytem_report() || host_last_consumer_report());
996 bool is_tap_key(key_t key)
998 action_t action = layer_switch_get_action(key);
1000 switch (action.kind.id) {
1006 switch (action.layer.code) {
1007 case 0x04 ... 0xEF: /* tap key */
1015 if (action.func.opt & FUNC_TAP) { return true; }
1025 static void debug_event(keyevent_t event)
1027 debug_hex16((event.key.row<<8) | event.key.col);
1028 if (event.pressed) debug("d("); else debug("u(");
1029 debug_dec(event.time); debug(")");
1031 static void debug_record(keyrecord_t record)
1033 debug_event(record.event); debug(":"); debug_dec(record.tap.count);
1034 if (record.tap.interrupted) debug("-");
1036 static void debug_action(action_t action)
1038 switch (action.kind.id) {
1039 case ACT_LMODS: debug("ACT_LMODS"); break;
1040 case ACT_RMODS: debug("ACT_RMODS"); break;
1041 case ACT_LMODS_TAP: debug("ACT_LMODS_TAP"); break;
1042 case ACT_RMODS_TAP: debug("ACT_RMODS_TAP"); break;
1043 case ACT_USAGE: debug("ACT_USAGE"); break;
1044 case ACT_MOUSEKEY: debug("ACT_MOUSEKEY"); break;
1045 case ACT_KEYMAP: debug("ACT_KEYMAP"); break;
1046 case ACT_OVERLAY: debug("ACT_OVERLAY"); break;
1047 case ACT_MACRO: debug("ACT_MACRO"); break;
1048 case ACT_COMMAND: debug("ACT_COMMAND"); break;
1049 case ACT_FUNCTION: debug("ACT_FUNCTION"); break;
1050 default: debug("UNKNOWN"); break;
1053 debug_hex4(action.kind.param>>8);
1055 debug_hex8(action.kind.param & 0xff);
1058 #ifndef NO_ACTION_TAPPING
1059 static void debug_tapping_key(void)
1061 debug("TAPPING_KEY="); debug_record(tapping_key); debug("\n");
1063 static void debug_waiting_buffer(void)
1066 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
1067 debug("["); debug_dec(i); debug("]="); debug_record(waiting_buffer[i]); debug(" ");