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_stack.h"
30 /* default layer indicates base layer */
31 uint8_t default_layer = 0;
32 /* current layer indicates active layer at this time */
33 uint8_t current_layer = 0;
36 static void process_action(keyrecord_t *record);
37 static bool process_tapping(keyrecord_t *record);
38 static void waiting_buffer_scan_tap(void);
40 static void debug_event(keyevent_t event);
41 static void debug_record(keyrecord_t record);
42 static void debug_action(action_t action);
43 static void debug_tapping_key(void);
44 static void debug_waiting_buffer(void);
50 /* period of tapping(ms) */
52 #define TAPPING_TERM 200
55 /* tap count needed for toggling a feature */
56 #ifndef TAPPING_TOGGLE
57 #define TAPPING_TOGGLE 5
60 /* stores a key event of current tap. */
61 static keyrecord_t tapping_key = {};
63 #define IS_TAPPING() !IS_NOEVENT(tapping_key.event)
64 #define IS_TAPPING_PRESSED() (IS_TAPPING() && tapping_key.event.pressed)
65 #define IS_TAPPING_RELEASED() (IS_TAPPING() && !tapping_key.event.pressed)
66 #define IS_TAPPING_KEY(k) (IS_TAPPING() && KEYEQ(tapping_key.event.key, (k)))
67 #define WITHIN_TAPPING_TERM(e) (TIMER_DIFF_16(e.time, tapping_key.event.time) < TAPPING_TERM)
73 * stores key events waiting for settling current tap.
75 #define WAITING_BUFFER_SIZE 8
76 static keyrecord_t waiting_buffer[WAITING_BUFFER_SIZE] = {};
78 /* point to empty cell to enq */
79 static uint8_t waiting_buffer_head = 0;
81 /* point to the oldest data cell to deq */
82 static uint8_t waiting_buffer_tail = 0;
84 static bool waiting_buffer_enq(keyrecord_t record)
86 if (IS_NOEVENT(record.event)) {
90 if ((waiting_buffer_head + 1) % WAITING_BUFFER_SIZE == waiting_buffer_tail) {
91 debug("waiting_buffer_enq: Over flow.\n");
95 waiting_buffer[waiting_buffer_head] = record;
96 waiting_buffer_head = (waiting_buffer_head + 1) % WAITING_BUFFER_SIZE;
98 debug("waiting_buffer_enq: "); debug_waiting_buffer();
102 static void waiting_buffer_clear(void)
104 waiting_buffer_head = 0;
105 waiting_buffer_tail = 0;
108 #if TAPPING_TERM >= 500
109 static bool waiting_buffer_typed(keyevent_t event)
111 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
112 if (KEYEQ(event.key, waiting_buffer[i].event.key) && event.pressed != waiting_buffer[i].event.pressed) {
120 bool waiting_buffer_has_anykey_pressed(void)
122 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
123 if (waiting_buffer[i].event.pressed) return true;
131 * Problem: Want to capitalize like 'The' but the result tends to be 'THe'.
132 * Solution: Oneshot modifier have its effect on only one key coming next.
133 * Tap Shift, then type 't', 'h' and 'e'. Not need to hold Shift key.
135 * Hold: works as normal modifier.
136 * Tap: one shot modifier.
137 * 2 Tap: cancel one shot modifier.
138 * 5-Tap: toggles enable/disable oneshot feature.
147 static void oneshot_start(uint8_t mods, uint16_t time)
149 oneshot_state.mods = mods;
150 oneshot_state.time = time;
151 oneshot_state.ready = true;
154 static void oneshot_cancel(void)
156 oneshot_state.mods = 0;
157 oneshot_state.time = 0;
158 oneshot_state.ready = false;
161 static void oneshot_toggle(void)
163 oneshot_state.disabled = !oneshot_state.disabled;
168 void action_exec(keyevent_t event)
170 if (!IS_NOEVENT(event)) {
171 debug("\n---- action_exec: start -----\n");
172 debug("EVENT: "); debug_event(event); debug("\n");
175 keyrecord_t record = { .event = event };
177 // pre-process on tapping
178 if (process_tapping(&record)) {
179 if (!IS_NOEVENT(record.event)) {
180 debug("processed: "); debug_record(record); debug("\n");
184 if (!waiting_buffer_enq(record)) {
185 // clear all in case of overflow.
186 debug("OVERFLOW: CLEAR ALL STATES\n");
188 waiting_buffer_clear();
189 tapping_key = (keyrecord_t){};
193 // process waiting_buffer
194 if (!IS_NOEVENT(event) && waiting_buffer_head != waiting_buffer_tail) {
195 debug("---- action_exec: process waiting_buffer -----\n");
198 for (; waiting_buffer_tail != waiting_buffer_head; waiting_buffer_tail = (waiting_buffer_tail + 1) % WAITING_BUFFER_SIZE) {
199 if (process_tapping(&waiting_buffer[waiting_buffer_tail])) {
200 debug("processed: waiting_buffer["); debug_dec(waiting_buffer_tail); debug("] = ");
201 debug_record(waiting_buffer[waiting_buffer_tail]); debug("\n\n");
206 if (!IS_NOEVENT(event)) {
211 static action_t get_action(key_t key)
214 action.code = ACTION_NO;
217 action = layer_stack_get_action(key);
218 if (action.code != ACTION_TRANSPARENT) {
222 /* current layer: 0 means default layer */
224 action = action_for_key(current_layer, key);
225 if (action.code != ACTION_TRANSPARENT) {
226 debug("current layer: used. "); debug_dec(current_layer); debug("\n");
232 debug("default layer: used. \n");
233 action = action_for_key(default_layer, key);
237 static void process_action(keyrecord_t *record)
239 keyevent_t event = record->event;
240 uint8_t tap_count = record->tap_count;
242 if (IS_NOEVENT(event)) { return; }
244 action_t action = get_action(event.key);
245 debug("ACTION: "); debug_action(action); debug("\n");
247 switch (action.kind.id) {
252 uint8_t mods = (action.kind.id == ACT_LMODS) ? action.key.mods :
255 uint8_t tmp_mods = host_get_mods();
258 host_send_keyboard_report();
260 register_code(action.key.code);
261 if (mods && action.key.code) {
262 host_set_mods(tmp_mods);
263 host_send_keyboard_report();
266 if (mods && !action.key.code) {
268 host_send_keyboard_report();
270 unregister_code(action.key.code);
277 uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods :
279 switch (action.layer.code) {
283 if (tap_count == 0) {
284 debug("MODS_TAP: Oneshot: add_mods\n");
287 else if (tap_count == 1) {
288 debug("MODS_TAP: Oneshot: start\n");
289 oneshot_start(mods, event.time);
291 else if (tap_count == TAPPING_TOGGLE) {
292 debug("MODS_TAP: Oneshot: toggle\n");
296 debug("MODS_TAP: Oneshot: cancel&add_mods\n");
297 // double tap cancels oneshot and works as normal modifier.
302 if (tap_count == 0) {
303 debug("MODS_TAP: Oneshot: cancel/del_mods\n");
304 // cancel oneshot on hold
308 else if (tap_count == 1) {
309 debug("MODS_TAP: Oneshot: del_mods\n");
314 debug("MODS_TAP: Oneshot: del_mods\n");
323 if (waiting_buffer_has_anykey_pressed()) {
324 debug("MODS_TAP: Tap: Cancel: add_mods\n");
325 // ad hoc: set 0 to cancel tap
326 record->tap_count = 0;
329 debug("MODS_TAP: Tap: register_code\n");
330 register_code(action.key.code);
333 debug("MODS_TAP: No tap: add_mods\n");
338 debug("MODS_TAP: Tap: unregister_code\n");
339 unregister_code(action.key.code);
341 debug("MODS_TAP: No tap: add_mods\n");
350 /* other HID usage */
352 #ifdef EXTRAKEY_ENABLE
353 switch (action.usage.page) {
356 host_system_send(action.usage.code);
363 host_consumer_send(action.usage.code);
365 host_consumer_send(0);
374 #ifdef MOUSEKEY_ENABLE
376 mousekey_on(action.key.code);
379 mousekey_off(action.key.code);
387 switch (action.layer.code) {
388 case LAYER_MOMENTARY: /* momentary */
390 layer_switch(action.layer.val);
393 // NOTE: This is needed by legacy keymap support
399 layer_switch(action.layer.val);
402 case LAYER_ON_RELEASE:
403 if (!event.pressed) {
404 layer_switch(action.layer.val);
408 layer_switch(action.layer.val);
410 case LAYER_TAP_TOGGLE: /* switch on hold and toggle on several taps */
412 if (tap_count < TAPPING_TOGGLE) {
413 layer_switch(action.layer.val);
416 if (tap_count >= TAPPING_TOGGLE) {
417 debug("LAYER_PRESSED: tap toggle.\n");
418 layer_switch(action.layer.val);
422 case LAYER_SET_DEFAULT_ON_PRESS:
424 default_layer = action.layer.val;
428 case LAYER_SET_DEFAULT_ON_RELEASE:
429 if (!event.pressed) {
430 default_layer = action.layer.val;
434 case LAYER_SET_DEFAULT_ON_BOTH:
435 default_layer = action.layer.val;
441 if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
442 debug("LAYER_SET: Tap: register_code\n");
443 register_code(action.layer.code);
445 debug("LAYER_SET: No tap: layer_set(on press)\n");
446 layer_switch(action.layer.val);
449 if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
450 debug("LAYER_SET: Tap: unregister_code\n");
451 unregister_code(action.layer.code);
453 // NOTE: This is needed by legacy keymap support
454 debug("LAYER_SET: No tap: return to default layer(on release)\n");
462 switch (action.layer.code) {
463 case LAYER_MOMENTARY: /* momentary */
465 layer_switch(current_layer | action.layer.val);
467 layer_switch(current_layer & ~action.layer.val);
472 layer_switch(current_layer ^ action.layer.val);
475 case LAYER_ON_RELEASE:
476 if (!event.pressed) {
477 layer_switch(current_layer ^ action.layer.val);
481 layer_switch(current_layer ^ action.layer.val);
483 case LAYER_TAP_TOGGLE: /* switch on hold and toggle on several taps */
485 if (tap_count < TAPPING_TOGGLE) {
486 debug("LAYER_BIT: tap toggle(press).\n");
487 layer_switch(current_layer ^ action.layer.val);
490 if (tap_count <= TAPPING_TOGGLE) {
491 debug("LAYER_BIT: tap toggle(release).\n");
492 layer_switch(current_layer ^ action.layer.val);
496 case LAYER_SET_DEFAULT_ON_PRESS:
498 default_layer = default_layer ^ action.layer.val;
502 case LAYER_SET_DEFAULT_ON_RELEASE:
503 if (!event.pressed) {
504 default_layer = default_layer ^ action.layer.val;
508 case LAYER_SET_DEFAULT_ON_BOTH:
509 default_layer = default_layer ^ action.layer.val;
515 if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
516 debug("LAYER_BIT: Tap: register_code\n");
517 register_code(action.layer.code);
519 debug("LAYER_BIT: No tap: layer_bit(on press)\n");
520 layer_switch(current_layer ^ action.layer.val);
523 if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
524 debug("LAYER_BIT: Tap: unregister_code\n");
525 unregister_code(action.layer.code);
527 debug("LAYER_BIT: No tap: layer_bit(on release)\n");
528 layer_switch(current_layer ^ action.layer.val);
534 case ACT_LAYER_STACK:
535 switch (action.layer.code) {
536 case LAYER_MOMENTARY: /* momentary */
538 layer_stack_remove_then_push(action.layer.val);
541 layer_stack_remove(action.layer.val);
547 layer_stack_remove_or_push(action.layer.val);
551 case LAYER_ON_RELEASE:
552 if (!event.pressed) {
553 layer_stack_remove_or_push(action.layer.val);
558 layer_stack_remove_or_push(action.layer.val);
561 case LAYER_TAP_TOGGLE: /* switch on hold and toggle on several taps */
563 if (tap_count < TAPPING_TOGGLE) {
564 debug("LAYER_STACK: tap toggle(press).\n");
565 layer_stack_remove_or_push(action.layer.val);
569 if (tap_count <= TAPPING_TOGGLE) {
570 debug("LAYER_STACK: tap toggle(release).\n");
571 layer_stack_remove_or_push(action.layer.val);
579 if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
580 debug("LAYER_STACK: Tap: register_code\n");
581 register_code(action.layer.code);
583 debug("LAYER_STACK: No tap: layer_stack(on press)\n");
584 layer_stack_remove_or_push(action.layer.val);
588 if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
589 debug("LAYER_STACK: Tap: unregister_code\n");
590 unregister_code(action.layer.code);
592 debug("LAYER_STACK: No tap: layer_stack(on release)\n");
593 layer_stack_remove_or_push(action.layer.val);
608 action_function(record, action.func.id, action.func.opt);
617 * Rule: Tap key is typed(pressed and released) within TAPPING_TERM.
618 * (without interfering by typing other key)
620 /* return true when key event is processed or consumed. */
621 static bool process_tapping(keyrecord_t *keyp)
623 keyevent_t event = keyp->event;
626 if (IS_TAPPING_PRESSED()) {
627 if (WITHIN_TAPPING_TERM(event)) {
628 if (tapping_key.tap_count == 0) {
629 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
631 debug("Tapping: First tap(0->1).\n");
632 tapping_key.tap_count = 1;
634 process_action(&tapping_key);
637 keyp->tap_count = tapping_key.tap_count;
640 #if TAPPING_TERM >= 500
641 /* This can prevent from typing some tap keys in a row at a time. */
642 else if (!event.pressed && waiting_buffer_typed(event)) {
643 // other key typed. not tap.
644 debug("Tapping: End. No tap. Interfered by typing key\n");
645 process_action(&tapping_key);
646 tapping_key = (keyrecord_t){};
654 // other key events shall be enq'd till tapping state settles.
660 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
661 debug("Tapping: Tap release("); debug_dec(tapping_key.tap_count); debug(")\n");
662 keyp->tap_count = tapping_key.tap_count;
663 process_action(keyp);
668 else if (is_tap_key(keyp->event.key) && event.pressed) {
669 if (tapping_key.tap_count > 1) {
670 debug("Tapping: Start new tap with releasing last tap(>1).\n");
672 process_action(&(keyrecord_t){
673 .tap_count = tapping_key.tap_count,
674 .event.key = tapping_key.event.key,
675 .event.time = event.time,
676 .event.pressed = false
679 debug("Tapping: Start while last tap(1).\n");
682 waiting_buffer_scan_tap();
687 if (!IS_NOEVENT(keyp->event)) {
688 debug("Tapping: key event while last tap(>0).\n");
690 process_action(keyp);
695 // after TAPPING_TERM
697 if (tapping_key.tap_count == 0) {
698 debug("Tapping: End. Timeout. Not tap(0): ");
699 debug_event(event); debug("\n");
700 process_action(&tapping_key);
701 tapping_key = (keyrecord_t){};
705 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
706 debug("Tapping: End. last timeout tap release(>0).");
707 keyp->tap_count = tapping_key.tap_count;
708 process_action(keyp);
709 tapping_key = (keyrecord_t){};
712 else if (is_tap_key(keyp->event.key) && event.pressed) {
713 if (tapping_key.tap_count > 1) {
714 debug("Tapping: Start new tap with releasing last timeout tap(>1).\n");
716 process_action(&(keyrecord_t){
717 .tap_count = tapping_key.tap_count,
718 .event.key = tapping_key.event.key,
719 .event.time = event.time,
720 .event.pressed = false
723 debug("Tapping: Start while last timeout tap(1).\n");
726 waiting_buffer_scan_tap();
731 if (!IS_NOEVENT(keyp->event)) {
732 debug("Tapping: key event while last timeout tap(>0).\n");
734 process_action(keyp);
739 } else if (IS_TAPPING_RELEASED()) {
740 if (WITHIN_TAPPING_TERM(event)) {
741 if (tapping_key.tap_count > 0 && IS_TAPPING_KEY(event.key) && event.pressed) {
743 keyp->tap_count = tapping_key.tap_count + 1;
744 debug("Tapping: Tap press("); debug_dec(keyp->tap_count); debug(")\n");
745 process_action(keyp);
749 } else if (event.pressed && is_tap_key(event.key)) {
750 // Sequential tap can be interfered with other tap key.
751 debug("Tapping: Start with interfering other tap.\n");
753 waiting_buffer_scan_tap();
757 if (!IS_NOEVENT(keyp->event)) debug("Tapping: other key just after tap.\n");
758 process_action(keyp);
762 // timeout. no sequential tap.
763 debug("Tapping: End(Timeout after releasing last tap): ");
764 debug_event(event); debug("\n");
765 tapping_key = (keyrecord_t){};
770 // not tapping satate
772 if (event.pressed && is_tap_key(event.key)) {
773 debug("Tapping: Start(Press tap key).\n");
775 waiting_buffer_scan_tap();
779 process_action(keyp);
785 /* scan buffer for tapping */
786 static void waiting_buffer_scan_tap(void)
788 // tapping already is settled
789 if (tapping_key.tap_count > 0) return;
790 // invalid state: tapping_key released && tap_count == 0
791 if (!tapping_key.event.pressed) return;
793 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
794 if (IS_TAPPING_KEY(waiting_buffer[i].event.key) &&
795 !waiting_buffer[i].event.pressed &&
796 WITHIN_TAPPING_TERM(waiting_buffer[i].event)) {
797 tapping_key.tap_count = 1;
798 waiting_buffer[i].tap_count = 1;
799 process_action(&tapping_key);
801 debug("waiting_buffer_scan_tap: found at ["); debug_dec(i); debug("]\n");
802 debug_waiting_buffer();
811 * Utilities for actions.
813 void register_code(uint8_t code)
818 else if IS_KEY(code) {
819 // TODO: should push command_proc out of this block?
820 if (command_proc(code)) return;
822 if (oneshot_state.mods && oneshot_state.ready && !oneshot_state.disabled) {
823 uint8_t tmp_mods = host_get_mods();
824 host_add_mods(oneshot_state.mods);
826 host_send_keyboard_report();
828 host_set_mods(tmp_mods);
829 oneshot_state.ready = false;
832 host_send_keyboard_report();
835 else if IS_MOD(code) {
836 host_add_mods(MOD_BIT(code));
837 host_send_keyboard_report();
841 void unregister_code(uint8_t code)
845 host_send_keyboard_report();
847 else if IS_MOD(code) {
848 host_del_mods(MOD_BIT(code));
849 host_send_keyboard_report();
853 void add_mods(uint8_t mods)
857 host_send_keyboard_report();
861 void del_mods(uint8_t mods)
865 host_send_keyboard_report();
869 void set_mods(uint8_t mods)
872 host_send_keyboard_report();
875 void clear_keyboard(void)
878 clear_keyboard_but_mods();
881 void clear_keyboard_but_mods(void)
884 host_send_keyboard_report();
885 #ifdef MOUSEKEY_ENABLE
889 #ifdef EXTRAKEY_ENABLE
891 host_consumer_send(0);
895 bool sending_anykey(void)
897 return (host_has_anykey() || host_mouse_in_use() ||
898 host_last_sysytem_report() || host_last_consumer_report());
901 void layer_switch(uint8_t new_layer)
903 if (current_layer != new_layer) {
904 debug("Layer Switch: "); debug_hex(current_layer);
905 debug(" -> "); debug_hex(new_layer); debug("\n");
907 current_layer = new_layer;
908 clear_keyboard_but_mods(); // To avoid stuck keys
909 // NOTE: update mods with full scan of matrix? if modifier changes between layers
913 bool is_tap_key(key_t key)
915 action_t action = get_action(key);
917 switch (action.kind.id) {
923 switch (action.layer.code) {
924 case LAYER_MOMENTARY:
926 case LAYER_ON_RELEASE:
928 case LAYER_SET_DEFAULT_ON_PRESS:
929 case LAYER_SET_DEFAULT_ON_RELEASE:
930 case LAYER_SET_DEFAULT_ON_BOTH:
932 case LAYER_TAP_TOGGLE:
933 default: /* tap key */
938 if (action.func.opt & FUNC_TAP) { return true; }
948 static void debug_event(keyevent_t event)
950 debug_hex16((event.key.row<<8) | event.key.col);
951 if (event.pressed) debug("d("); else debug("u(");
952 debug_dec(event.time); debug(")");
954 static void debug_record(keyrecord_t record)
956 debug_event(record.event); debug(":"); debug_dec(record.tap_count);
958 static void debug_action(action_t action)
960 switch (action.kind.id) {
961 case ACT_LMODS: debug("ACT_LMODS"); break;
962 case ACT_RMODS: debug("ACT_RMODS"); break;
963 case ACT_LMODS_TAP: debug("ACT_LMODS_TAP"); break;
964 case ACT_RMODS_TAP: debug("ACT_RMODS_TAP"); break;
965 case ACT_USAGE: debug("ACT_USAGE"); break;
966 case ACT_MOUSEKEY: debug("ACT_MOUSEKEY"); break;
967 case ACT_LAYER: debug("ACT_LAYER"); break;
968 case ACT_LAYER_BIT: debug("ACT_LAYER_BIT"); break;
969 case ACT_LAYER_STACK: debug("ACT_LAYER_STACK"); break;
970 case ACT_MACRO: debug("ACT_MACRO"); break;
971 case ACT_COMMAND: debug("ACT_COMMAND"); break;
972 case ACT_FUNCTION: debug("ACT_FUNCTION"); break;
973 default: debug("UNKNOWN"); break;
976 debug_hex4(action.kind.param>>8);
978 debug_hex8(action.kind.param & 0xff);
981 static void debug_tapping_key(void)
983 debug("TAPPING_KEY="); debug_record(tapping_key); debug("\n");
985 static void debug_waiting_buffer(void)
988 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
989 debug("["); debug_dec(i); debug("]="); debug_record(waiting_buffer[i]); debug(" ");