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 /* 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_switch_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_SWITCH:
535 switch (action.layer.code) {
536 case LAYER_MOMENTARY: /* momentary */
538 layer_switch_on(action.layer.val);
540 layer_switch_off(action.layer.val);
545 layer_switch_inv(action.layer.val);
548 case LAYER_ON_RELEASE:
549 if (!event.pressed) {
550 layer_switch_inv(action.layer.val);
554 layer_switch_inv(action.layer.val);
556 case LAYER_TAP_TOGGLE: /* switch on hold and toggle on several taps */
558 if (tap_count < TAPPING_TOGGLE) {
559 debug("LAYER_SWITCH: tap toggle(press).\n");
560 layer_switch_inv(action.layer.val);
563 if (tap_count <= TAPPING_TOGGLE) {
564 debug("LAYER_SWITCH: tap toggle(release).\n");
565 layer_switch_inv(action.layer.val);
572 if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
573 debug("LAYER_SWITCH: Tap: register_code\n");
574 register_code(action.layer.code);
576 debug("LAYER_SWITCH: No tap: layer_switch(on press)\n");
577 layer_switch_inv(action.layer.val);
580 if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
581 debug("LAYER_SWITCH: Tap: unregister_code\n");
582 unregister_code(action.layer.code);
584 debug("LAYER_SWITCH: No tap: layer_switch(on release)\n");
585 layer_switch_inv(action.layer.val);
599 action_function(record, action.func.id, action.func.opt);
608 * Rule: Tap key is typed(pressed and released) within TAPPING_TERM.
609 * (without interfering by typing other key)
611 /* return true when key event is processed or consumed. */
612 static bool process_tapping(keyrecord_t *keyp)
614 keyevent_t event = keyp->event;
617 if (IS_TAPPING_PRESSED()) {
618 if (WITHIN_TAPPING_TERM(event)) {
619 if (tapping_key.tap_count == 0) {
620 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
622 debug("Tapping: First tap(0->1).\n");
623 tapping_key.tap_count = 1;
625 process_action(&tapping_key);
628 keyp->tap_count = tapping_key.tap_count;
631 #if TAPPING_TERM >= 500
632 /* This can prevent from typing some tap keys in a row at a time. */
633 else if (!event.pressed && waiting_buffer_typed(event)) {
634 // other key typed. not tap.
635 debug("Tapping: End. No tap. Interfered by typing key\n");
636 process_action(&tapping_key);
637 tapping_key = (keyrecord_t){};
645 // other key events shall be enq'd till tapping state settles.
651 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
652 debug("Tapping: Tap release("); debug_dec(tapping_key.tap_count); debug(")\n");
653 keyp->tap_count = tapping_key.tap_count;
654 process_action(keyp);
659 else if (is_tap_key(keyp->event.key) && event.pressed) {
660 if (tapping_key.tap_count > 1) {
661 debug("Tapping: Start new tap with releasing last tap(>1).\n");
663 process_action(&(keyrecord_t){
664 .tap_count = tapping_key.tap_count,
665 .event.key = tapping_key.event.key,
666 .event.time = event.time,
667 .event.pressed = false
670 debug("Tapping: Start while last tap(1).\n");
673 waiting_buffer_scan_tap();
678 if (!IS_NOEVENT(keyp->event)) {
679 debug("Tapping: key event while last tap(>0).\n");
681 process_action(keyp);
686 // after TAPPING_TERM
688 if (tapping_key.tap_count == 0) {
689 debug("Tapping: End. Timeout. Not tap(0): ");
690 debug_event(event); debug("\n");
691 process_action(&tapping_key);
692 tapping_key = (keyrecord_t){};
696 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
697 debug("Tapping: End. last timeout tap release(>0).");
698 keyp->tap_count = tapping_key.tap_count;
699 process_action(keyp);
700 tapping_key = (keyrecord_t){};
703 else if (is_tap_key(keyp->event.key) && event.pressed) {
704 if (tapping_key.tap_count > 1) {
705 debug("Tapping: Start new tap with releasing last timeout tap(>1).\n");
707 process_action(&(keyrecord_t){
708 .tap_count = tapping_key.tap_count,
709 .event.key = tapping_key.event.key,
710 .event.time = event.time,
711 .event.pressed = false
714 debug("Tapping: Start while last timeout tap(1).\n");
717 waiting_buffer_scan_tap();
722 if (!IS_NOEVENT(keyp->event)) {
723 debug("Tapping: key event while last timeout tap(>0).\n");
725 process_action(keyp);
730 } else if (IS_TAPPING_RELEASED()) {
731 if (WITHIN_TAPPING_TERM(event)) {
732 if (tapping_key.tap_count > 0 && IS_TAPPING_KEY(event.key) && event.pressed) {
734 keyp->tap_count = tapping_key.tap_count + 1;
735 debug("Tapping: Tap press("); debug_dec(keyp->tap_count); debug(")\n");
736 process_action(keyp);
740 } else if (event.pressed && is_tap_key(event.key)) {
741 // Sequential tap can be interfered with other tap key.
742 debug("Tapping: Start with interfering other tap.\n");
744 waiting_buffer_scan_tap();
748 if (!IS_NOEVENT(keyp->event)) debug("Tapping: other key just after tap.\n");
749 process_action(keyp);
753 // timeout. no sequential tap.
754 debug("Tapping: End(Timeout after releasing last tap): ");
755 debug_event(event); debug("\n");
756 tapping_key = (keyrecord_t){};
761 // not tapping satate
763 if (event.pressed && is_tap_key(event.key)) {
764 debug("Tapping: Start(Press tap key).\n");
766 waiting_buffer_scan_tap();
770 process_action(keyp);
776 /* scan buffer for tapping */
777 static void waiting_buffer_scan_tap(void)
779 // tapping already is settled
780 if (tapping_key.tap_count > 0) return;
781 // invalid state: tapping_key released && tap_count == 0
782 if (!tapping_key.event.pressed) return;
784 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
785 if (IS_TAPPING_KEY(waiting_buffer[i].event.key) &&
786 !waiting_buffer[i].event.pressed &&
787 WITHIN_TAPPING_TERM(waiting_buffer[i].event)) {
788 tapping_key.tap_count = 1;
789 waiting_buffer[i].tap_count = 1;
790 process_action(&tapping_key);
792 debug("waiting_buffer_scan_tap: found at ["); debug_dec(i); debug("]\n");
793 debug_waiting_buffer();
802 * Utilities for actions.
804 void register_code(uint8_t code)
809 else if IS_KEY(code) {
810 // TODO: should push command_proc out of this block?
811 if (command_proc(code)) return;
813 if (oneshot_state.mods && oneshot_state.ready && !oneshot_state.disabled) {
814 uint8_t tmp_mods = host_get_mods();
815 host_add_mods(oneshot_state.mods);
817 host_send_keyboard_report();
819 host_set_mods(tmp_mods);
820 oneshot_state.ready = false;
823 host_send_keyboard_report();
826 else if IS_MOD(code) {
827 host_add_mods(MOD_BIT(code));
828 host_send_keyboard_report();
832 void unregister_code(uint8_t code)
836 host_send_keyboard_report();
838 else if IS_MOD(code) {
839 host_del_mods(MOD_BIT(code));
840 host_send_keyboard_report();
844 void add_mods(uint8_t mods)
848 host_send_keyboard_report();
852 void del_mods(uint8_t mods)
856 host_send_keyboard_report();
860 void set_mods(uint8_t mods)
863 host_send_keyboard_report();
866 void clear_keyboard(void)
869 clear_keyboard_but_mods();
872 void clear_keyboard_but_mods(void)
875 host_send_keyboard_report();
876 #ifdef MOUSEKEY_ENABLE
880 #ifdef EXTRAKEY_ENABLE
882 host_consumer_send(0);
886 bool sending_anykey(void)
888 return (host_has_anykey() || host_mouse_in_use() ||
889 host_last_sysytem_report() || host_last_consumer_report());
892 // TODO: rename or reinpl with new layer_switch.c
893 void layer_switch(uint8_t new_layer)
895 if (current_layer != new_layer) {
896 debug("Layer Switch: "); debug_hex(current_layer);
897 debug(" -> "); debug_hex(new_layer); debug("\n");
899 current_layer = new_layer;
900 clear_keyboard_but_mods(); // To avoid stuck keys
901 // NOTE: update mods with full scan of matrix? if modifier changes between layers
905 bool is_tap_key(key_t key)
907 action_t action = get_action(key);
909 switch (action.kind.id) {
915 switch (action.layer.code) {
916 case LAYER_MOMENTARY:
918 case LAYER_ON_RELEASE:
920 case LAYER_SET_DEFAULT_ON_PRESS:
921 case LAYER_SET_DEFAULT_ON_RELEASE:
922 case LAYER_SET_DEFAULT_ON_BOTH:
924 case LAYER_TAP_TOGGLE:
925 default: /* tap key */
930 if (action.func.opt & FUNC_TAP) { return true; }
940 static void debug_event(keyevent_t event)
942 debug_hex16((event.key.row<<8) | event.key.col);
943 if (event.pressed) debug("d("); else debug("u(");
944 debug_dec(event.time); debug(")");
946 static void debug_record(keyrecord_t record)
948 debug_event(record.event); debug(":"); debug_dec(record.tap_count);
950 static void debug_action(action_t action)
952 switch (action.kind.id) {
953 case ACT_LMODS: debug("ACT_LMODS"); break;
954 case ACT_RMODS: debug("ACT_RMODS"); break;
955 case ACT_LMODS_TAP: debug("ACT_LMODS_TAP"); break;
956 case ACT_RMODS_TAP: debug("ACT_RMODS_TAP"); break;
957 case ACT_USAGE: debug("ACT_USAGE"); break;
958 case ACT_MOUSEKEY: debug("ACT_MOUSEKEY"); break;
959 case ACT_LAYER: debug("ACT_LAYER"); break;
960 case ACT_LAYER_BIT: debug("ACT_LAYER_BIT"); break;
961 case ACT_LAYER_SWITCH: debug("ACT_LAYER_SWITCH"); break;
962 case ACT_MACRO: debug("ACT_MACRO"); break;
963 case ACT_COMMAND: debug("ACT_COMMAND"); break;
964 case ACT_FUNCTION: debug("ACT_FUNCTION"); break;
965 default: debug("UNKNOWN"); break;
968 debug_hex4(action.kind.param>>8);
970 debug_hex8(action.kind.param & 0xff);
973 static void debug_tapping_key(void)
975 debug("TAPPING_KEY="); debug_record(tapping_key); debug("\n");
977 static void debug_waiting_buffer(void)
980 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
981 debug("["); debug_dec(i); debug("]="); debug_record(waiting_buffer[i]); debug(" ");