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"
30 static void process_action(keyrecord_t *record);
31 static bool process_tapping(keyrecord_t *record);
32 static void waiting_buffer_scan_tap(void);
34 static void debug_event(keyevent_t event);
35 static void debug_record(keyrecord_t record);
36 static void debug_action(action_t action);
37 static void debug_tapping_key(void);
38 static void debug_waiting_buffer(void);
44 /* period of tapping(ms) */
46 #define TAPPING_TERM 200
49 /* tap count needed for toggling a feature */
50 #ifndef TAPPING_TOGGLE
51 #define TAPPING_TOGGLE 5
54 /* stores a key event of current tap. */
55 static keyrecord_t tapping_key = {};
57 #define IS_TAPPING() !IS_NOEVENT(tapping_key.event)
58 #define IS_TAPPING_PRESSED() (IS_TAPPING() && tapping_key.event.pressed)
59 #define IS_TAPPING_RELEASED() (IS_TAPPING() && !tapping_key.event.pressed)
60 #define IS_TAPPING_KEY(k) (IS_TAPPING() && KEYEQ(tapping_key.event.key, (k)))
61 #define WITHIN_TAPPING_TERM(e) (TIMER_DIFF_16(e.time, tapping_key.event.time) < TAPPING_TERM)
67 * stores key events waiting for settling current tap.
69 #define WAITING_BUFFER_SIZE 8
70 static keyrecord_t waiting_buffer[WAITING_BUFFER_SIZE] = {};
72 /* point to empty cell to enq */
73 static uint8_t waiting_buffer_head = 0;
75 /* point to the oldest data cell to deq */
76 static uint8_t waiting_buffer_tail = 0;
78 static bool waiting_buffer_enq(keyrecord_t record)
80 if (IS_NOEVENT(record.event)) {
84 if ((waiting_buffer_head + 1) % WAITING_BUFFER_SIZE == waiting_buffer_tail) {
85 debug("waiting_buffer_enq: Over flow.\n");
89 waiting_buffer[waiting_buffer_head] = record;
90 waiting_buffer_head = (waiting_buffer_head + 1) % WAITING_BUFFER_SIZE;
92 debug("waiting_buffer_enq: "); debug_waiting_buffer();
96 static void waiting_buffer_clear(void)
98 waiting_buffer_head = 0;
99 waiting_buffer_tail = 0;
102 #if TAPPING_TERM >= 500
103 static bool waiting_buffer_typed(keyevent_t event)
105 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
106 if (KEYEQ(event.key, waiting_buffer[i].event.key) && event.pressed != waiting_buffer[i].event.pressed) {
114 bool waiting_buffer_has_anykey_pressed(void)
116 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
117 if (waiting_buffer[i].event.pressed) return true;
125 * Problem: Want to capitalize like 'The' but the result tends to be 'THe'.
126 * Solution: Oneshot modifier have its effect on only one key coming next.
127 * Tap Shift, then type 't', 'h' and 'e'. Not need to hold Shift key.
129 * Hold: works as normal modifier.
130 * Tap: one shot modifier.
131 * 2 Tap: cancel one shot modifier.
132 * 5-Tap: toggles enable/disable oneshot feature.
141 static void oneshot_start(uint8_t mods, uint16_t time)
143 oneshot_state.mods = mods;
144 oneshot_state.time = time;
145 oneshot_state.ready = true;
148 static void oneshot_cancel(void)
150 oneshot_state.mods = 0;
151 oneshot_state.time = 0;
152 oneshot_state.ready = false;
155 static void oneshot_toggle(void)
157 oneshot_state.disabled = !oneshot_state.disabled;
162 void action_exec(keyevent_t event)
164 if (!IS_NOEVENT(event)) {
165 debug("\n---- action_exec: start -----\n");
166 debug("EVENT: "); debug_event(event); debug("\n");
169 keyrecord_t record = { .event = event };
171 // pre-process on tapping
172 if (process_tapping(&record)) {
173 if (!IS_NOEVENT(record.event)) {
174 debug("processed: "); debug_record(record); debug("\n");
178 if (!waiting_buffer_enq(record)) {
179 // clear all in case of overflow.
180 debug("OVERFLOW: CLEAR ALL STATES\n");
182 waiting_buffer_clear();
183 tapping_key = (keyrecord_t){};
187 // process waiting_buffer
188 if (!IS_NOEVENT(event) && waiting_buffer_head != waiting_buffer_tail) {
189 debug("---- action_exec: process waiting_buffer -----\n");
192 for (; waiting_buffer_tail != waiting_buffer_head; waiting_buffer_tail = (waiting_buffer_tail + 1) % WAITING_BUFFER_SIZE) {
193 if (process_tapping(&waiting_buffer[waiting_buffer_tail])) {
194 debug("processed: waiting_buffer["); debug_dec(waiting_buffer_tail); debug("] = ");
195 debug_record(waiting_buffer[waiting_buffer_tail]); debug("\n\n");
200 if (!IS_NOEVENT(event)) {
205 static void process_action(keyrecord_t *record)
207 keyevent_t event = record->event;
208 uint8_t tap_count = record->tap_count;
210 if (IS_NOEVENT(event)) { return; }
212 action_t action = layer_switch_get_action(event.key);
213 debug("ACTION: "); debug_action(action);
214 debug(" overlays: "); overlay_debug();
215 debug(" keymaps: "); keymap_debug();
216 debug(" default_layer: "); debug_dec(default_layer); debug("\n");
218 switch (action.kind.id) {
223 uint8_t mods = (action.kind.id == ACT_LMODS) ? action.key.mods :
226 uint8_t tmp_mods = host_get_mods();
229 host_send_keyboard_report();
231 register_code(action.key.code);
232 if (mods && action.key.code) {
233 host_set_mods(tmp_mods);
234 host_send_keyboard_report();
237 if (mods && !action.key.code) {
239 host_send_keyboard_report();
241 unregister_code(action.key.code);
248 uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods :
250 switch (action.layer.code) {
254 if (tap_count == 0) {
255 debug("MODS_TAP: Oneshot: add_mods\n");
258 else if (tap_count == 1) {
259 debug("MODS_TAP: Oneshot: start\n");
260 oneshot_start(mods, event.time);
262 else if (tap_count == TAPPING_TOGGLE) {
263 debug("MODS_TAP: Oneshot: toggle\n");
267 debug("MODS_TAP: Oneshot: cancel&add_mods\n");
268 // double tap cancels oneshot and works as normal modifier.
273 if (tap_count == 0) {
274 debug("MODS_TAP: Oneshot: cancel/del_mods\n");
275 // cancel oneshot on hold
279 else if (tap_count == 1) {
280 debug("MODS_TAP: Oneshot: del_mods\n");
285 debug("MODS_TAP: Oneshot: del_mods\n");
294 if (waiting_buffer_has_anykey_pressed()) {
295 debug("MODS_TAP: Tap: Cancel: add_mods\n");
296 // ad hoc: set 0 to cancel tap
297 record->tap_count = 0;
300 debug("MODS_TAP: Tap: register_code\n");
301 register_code(action.key.code);
304 debug("MODS_TAP: No tap: add_mods\n");
309 debug("MODS_TAP: Tap: unregister_code\n");
310 unregister_code(action.key.code);
312 debug("MODS_TAP: No tap: add_mods\n");
321 /* other HID usage */
323 #ifdef EXTRAKEY_ENABLE
324 switch (action.usage.page) {
327 host_system_send(action.usage.code);
334 host_consumer_send(action.usage.code);
336 host_consumer_send(0);
345 #ifdef MOUSEKEY_ENABLE
347 mousekey_on(action.key.code);
350 mousekey_off(action.key.code);
357 switch (action.layer.code) {
360 default_layer_set(action.layer.val);
362 /* Keymap Reset default layer */
363 case (OP_RESET | ON_PRESS):
365 default_layer_set(action.layer.val);
369 case (OP_RESET | ON_RELEASE):
370 if (!event.pressed) {
371 default_layer_set(action.layer.val);
375 case (OP_RESET | ON_BOTH):
376 default_layer_set(action.layer.val);
380 /* Keymap Bit invert */
382 /* with tap toggle */
384 if (tap_count < TAPPING_TOGGLE) {
385 debug("KEYMAP_INV: tap toggle(press).\n");
386 keymap_invert(action.layer.val);
389 if (tap_count <= TAPPING_TOGGLE) {
390 debug("KEYMAP_INV: tap toggle(release).\n");
391 keymap_invert(action.layer.val);
395 case (OP_INV | ON_PRESS):
397 keymap_invert(action.layer.val);
400 case (OP_INV | ON_RELEASE):
401 if (!event.pressed) {
402 keymap_invert(action.layer.val);
405 case (OP_INV | ON_BOTH):
406 keymap_invert(action.layer.val);
412 keymap_on(action.layer.val);
414 keymap_off(action.layer.val);
417 case (OP_ON | ON_PRESS):
419 keymap_on(action.layer.val);
422 case (OP_ON | ON_RELEASE):
423 if (!event.pressed) {
424 keymap_on(action.layer.val);
427 case (OP_ON | ON_BOTH):
428 keymap_on(action.layer.val);
434 keymap_off(action.layer.val);
436 keymap_on(action.layer.val);
439 case (OP_OFF | ON_PRESS):
441 keymap_off(action.layer.val);
444 case (OP_OFF | ON_RELEASE):
445 if (!event.pressed) {
446 keymap_off(action.layer.val);
449 case (OP_OFF | ON_BOTH):
450 keymap_off(action.layer.val);
456 keymap_set(action.layer.val);
461 case (OP_SET | ON_PRESS):
463 keymap_set(action.layer.val);
466 case (OP_SET | ON_RELEASE):
467 if (!event.pressed) {
468 keymap_set(action.layer.val);
471 case (OP_SET | ON_BOTH):
472 keymap_set(action.layer.val);
475 /* Keymap Bit invert with tap key */
478 if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
479 debug("KEYMAP_TAP_KEY: Tap: register_code\n");
480 register_code(action.layer.code);
482 debug("KEYMAP_TAP_KEY: No tap: invert on press\n");
483 keymap_invert(action.layer.val);
486 if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
487 debug("KEYMAP_TAP_KEY: Tap: unregister_code\n");
488 unregister_code(action.layer.code);
490 debug("KEYMAP_TAP_KEY: No tap: invert on release\n");
491 keymap_invert(action.layer.val);
499 switch (action.layer.code) {
500 // Overlay Invert bit4
502 if (action.layer.val == 0) {
505 overlay_set(overlay_stat ^ action.layer.val);
509 if (action.layer.val == 0) {
510 if (event.pressed) overlay_clear();
512 overlay_set(overlay_stat ^ action.layer.val<<4);
516 if (action.layer.val == 0) {
517 if (!event.pressed) overlay_clear();
519 overlay_set(overlay_stat ^ action.layer.val<<8);
523 if (action.layer.val == 0) {
526 overlay_set(overlay_stat ^ action.layer.val<<12);
530 /* Overlay Bit invert */
532 /* with tap toggle */
534 if (tap_count < TAPPING_TOGGLE) {
535 debug("OVERLAY_INV: tap toggle(press).\n");
536 overlay_invert(action.layer.val);
539 if (tap_count <= TAPPING_TOGGLE) {
540 debug("OVERLAY_INV: tap toggle(release).\n");
541 overlay_invert(action.layer.val);
545 case (OP_INV | ON_PRESS):
547 overlay_invert(action.layer.val);
550 case (OP_INV | ON_RELEASE):
551 if (!event.pressed) {
552 overlay_invert(action.layer.val);
555 case (OP_INV | ON_BOTH):
556 overlay_invert(action.layer.val);
562 overlay_on(action.layer.val);
564 overlay_off(action.layer.val);
567 case (OP_ON | ON_PRESS):
569 overlay_on(action.layer.val);
572 case (OP_ON | ON_RELEASE):
573 if (!event.pressed) {
574 overlay_on(action.layer.val);
577 case (OP_ON | ON_BOTH):
578 overlay_on(action.layer.val);
581 /* Overlay Bit off */
584 overlay_off(action.layer.val);
586 overlay_on(action.layer.val);
589 case (OP_OFF | ON_PRESS):
591 overlay_off(action.layer.val);
594 case (OP_OFF | ON_RELEASE):
595 if (!event.pressed) {
596 overlay_off(action.layer.val);
599 case (OP_OFF | ON_BOTH):
600 overlay_off(action.layer.val);
603 /* Overlay Bit set */
606 overlay_move(action.layer.val);
611 case (OP_SET | ON_PRESS):
613 overlay_move(action.layer.val);
616 case (OP_SET | ON_RELEASE):
617 if (!event.pressed) {
618 overlay_move(action.layer.val);
621 case (OP_SET | ON_BOTH):
622 overlay_move(action.layer.val);
625 /* Overlay Bit invert with tap key */
628 if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
629 debug("OVERLAY_TAP_KEY: Tap: register_code\n");
630 register_code(action.layer.code);
632 debug("OVERLAY_TAP_KEY: No tap: invert on press\n");
633 overlay_invert(action.layer.val);
636 if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
637 debug("OVERLAY_TAP_KEY: Tap: unregister_code\n");
638 unregister_code(action.layer.code);
640 debug("OVERLAY_TAP_KEY: No tap: invert on release\n");
641 overlay_invert(action.layer.val);
655 action_function(record, action.func.id, action.func.opt);
664 * Rule: Tap key is typed(pressed and released) within TAPPING_TERM.
665 * (without interfering by typing other key)
667 /* return true when key event is processed or consumed. */
668 static bool process_tapping(keyrecord_t *keyp)
670 keyevent_t event = keyp->event;
673 if (IS_TAPPING_PRESSED()) {
674 if (WITHIN_TAPPING_TERM(event)) {
675 if (tapping_key.tap_count == 0) {
676 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
678 debug("Tapping: First tap(0->1).\n");
679 tapping_key.tap_count = 1;
681 process_action(&tapping_key);
684 keyp->tap_count = tapping_key.tap_count;
687 #if TAPPING_TERM >= 500
688 /* This can prevent from typing some tap keys in a row at a time. */
689 else if (!event.pressed && waiting_buffer_typed(event)) {
690 // other key typed. not tap.
691 debug("Tapping: End. No tap. Interfered by typing key\n");
692 process_action(&tapping_key);
693 tapping_key = (keyrecord_t){};
701 // other key events shall be enq'd till tapping state settles.
707 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
708 debug("Tapping: Tap release("); debug_dec(tapping_key.tap_count); debug(")\n");
709 keyp->tap_count = tapping_key.tap_count;
710 process_action(keyp);
715 else if (is_tap_key(keyp->event.key) && event.pressed) {
716 if (tapping_key.tap_count > 1) {
717 debug("Tapping: Start new tap with releasing last tap(>1).\n");
719 process_action(&(keyrecord_t){
720 .tap_count = tapping_key.tap_count,
721 .event.key = tapping_key.event.key,
722 .event.time = event.time,
723 .event.pressed = false
726 debug("Tapping: Start while last tap(1).\n");
729 waiting_buffer_scan_tap();
734 if (!IS_NOEVENT(keyp->event)) {
735 debug("Tapping: key event while last tap(>0).\n");
737 process_action(keyp);
742 // after TAPPING_TERM
744 if (tapping_key.tap_count == 0) {
745 debug("Tapping: End. Timeout. Not tap(0): ");
746 debug_event(event); debug("\n");
747 process_action(&tapping_key);
748 tapping_key = (keyrecord_t){};
752 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
753 debug("Tapping: End. last timeout tap release(>0).");
754 keyp->tap_count = tapping_key.tap_count;
755 process_action(keyp);
756 tapping_key = (keyrecord_t){};
759 else if (is_tap_key(keyp->event.key) && event.pressed) {
760 if (tapping_key.tap_count > 1) {
761 debug("Tapping: Start new tap with releasing last timeout tap(>1).\n");
763 process_action(&(keyrecord_t){
764 .tap_count = tapping_key.tap_count,
765 .event.key = tapping_key.event.key,
766 .event.time = event.time,
767 .event.pressed = false
770 debug("Tapping: Start while last timeout tap(1).\n");
773 waiting_buffer_scan_tap();
778 if (!IS_NOEVENT(keyp->event)) {
779 debug("Tapping: key event while last timeout tap(>0).\n");
781 process_action(keyp);
786 } else if (IS_TAPPING_RELEASED()) {
787 if (WITHIN_TAPPING_TERM(event)) {
788 if (tapping_key.tap_count > 0 && IS_TAPPING_KEY(event.key) && event.pressed) {
790 keyp->tap_count = tapping_key.tap_count + 1;
791 debug("Tapping: Tap press("); debug_dec(keyp->tap_count); debug(")\n");
792 process_action(keyp);
796 } else if (event.pressed && is_tap_key(event.key)) {
797 // Sequential tap can be interfered with other tap key.
798 debug("Tapping: Start with interfering other tap.\n");
800 waiting_buffer_scan_tap();
804 if (!IS_NOEVENT(keyp->event)) debug("Tapping: other key just after tap.\n");
805 process_action(keyp);
809 // timeout. no sequential tap.
810 debug("Tapping: End(Timeout after releasing last tap): ");
811 debug_event(event); debug("\n");
812 tapping_key = (keyrecord_t){};
817 // not tapping satate
819 if (event.pressed && is_tap_key(event.key)) {
820 debug("Tapping: Start(Press tap key).\n");
822 waiting_buffer_scan_tap();
826 process_action(keyp);
832 /* scan buffer for tapping */
833 static void waiting_buffer_scan_tap(void)
835 // tapping already is settled
836 if (tapping_key.tap_count > 0) return;
837 // invalid state: tapping_key released && tap_count == 0
838 if (!tapping_key.event.pressed) return;
840 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
841 if (IS_TAPPING_KEY(waiting_buffer[i].event.key) &&
842 !waiting_buffer[i].event.pressed &&
843 WITHIN_TAPPING_TERM(waiting_buffer[i].event)) {
844 tapping_key.tap_count = 1;
845 waiting_buffer[i].tap_count = 1;
846 process_action(&tapping_key);
848 debug("waiting_buffer_scan_tap: found at ["); debug_dec(i); debug("]\n");
849 debug_waiting_buffer();
858 * Utilities for actions.
860 void register_code(uint8_t code)
865 else if IS_KEY(code) {
866 // TODO: should push command_proc out of this block?
867 if (command_proc(code)) return;
869 if (oneshot_state.mods && oneshot_state.ready && !oneshot_state.disabled) {
870 uint8_t tmp_mods = host_get_mods();
871 host_add_mods(oneshot_state.mods);
873 host_send_keyboard_report();
875 host_set_mods(tmp_mods);
876 oneshot_state.ready = false;
879 host_send_keyboard_report();
882 else if IS_MOD(code) {
883 host_add_mods(MOD_BIT(code));
884 host_send_keyboard_report();
888 void unregister_code(uint8_t code)
892 host_send_keyboard_report();
894 else if IS_MOD(code) {
895 host_del_mods(MOD_BIT(code));
896 host_send_keyboard_report();
900 void add_mods(uint8_t mods)
904 host_send_keyboard_report();
908 void del_mods(uint8_t mods)
912 host_send_keyboard_report();
916 void set_mods(uint8_t mods)
919 host_send_keyboard_report();
922 void clear_keyboard(void)
925 clear_keyboard_but_mods();
928 void clear_keyboard_but_mods(void)
931 host_send_keyboard_report();
932 #ifdef MOUSEKEY_ENABLE
936 #ifdef EXTRAKEY_ENABLE
938 host_consumer_send(0);
942 bool sending_anykey(void)
944 return (host_has_anykey() || host_mouse_in_use() ||
945 host_last_sysytem_report() || host_last_consumer_report());
948 bool is_tap_key(key_t key)
950 action_t action = layer_switch_get_action(key);
952 switch (action.kind.id) {
958 switch (action.layer.code) {
959 case 0x04 ... 0xEF: /* tap key */
966 if (action.func.opt & FUNC_TAP) { return true; }
976 static void debug_event(keyevent_t event)
978 debug_hex16((event.key.row<<8) | event.key.col);
979 if (event.pressed) debug("d("); else debug("u(");
980 debug_dec(event.time); debug(")");
982 static void debug_record(keyrecord_t record)
984 debug_event(record.event); debug(":"); debug_dec(record.tap_count);
986 static void debug_action(action_t action)
988 switch (action.kind.id) {
989 case ACT_LMODS: debug("ACT_LMODS"); break;
990 case ACT_RMODS: debug("ACT_RMODS"); break;
991 case ACT_LMODS_TAP: debug("ACT_LMODS_TAP"); break;
992 case ACT_RMODS_TAP: debug("ACT_RMODS_TAP"); break;
993 case ACT_USAGE: debug("ACT_USAGE"); break;
994 case ACT_MOUSEKEY: debug("ACT_MOUSEKEY"); break;
995 case ACT_KEYMAP: debug("ACT_KEYMAP"); break;
996 case ACT_OVERLAY: debug("ACT_OVERLAY"); break;
997 case ACT_MACRO: debug("ACT_MACRO"); break;
998 case ACT_COMMAND: debug("ACT_COMMAND"); break;
999 case ACT_FUNCTION: debug("ACT_FUNCTION"); break;
1000 default: debug("UNKNOWN"); break;
1003 debug_hex4(action.kind.param>>8);
1005 debug_hex8(action.kind.param & 0xff);
1008 static void debug_tapping_key(void)
1010 debug("TAPPING_KEY="); debug_record(tapping_key); debug("\n");
1012 static void debug_waiting_buffer(void)
1015 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
1016 debug("["); debug_dec(i); debug("]="); debug_record(waiting_buffer[i]); debug(" ");