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 action_t get_action(key_t key)
208 action.code = ACTION_NO;
211 action = layer_switch_get_action(key);
212 if (action.code != ACTION_TRANSPARENT) {
217 //debug("get_aciton: default layer: "); debug_dec(default_layer); debug("\n");
218 action = action_for_key(default_layer, key);
222 static void process_action(keyrecord_t *record)
224 keyevent_t event = record->event;
225 uint8_t tap_count = record->tap_count;
227 if (IS_NOEVENT(event)) { return; }
229 action_t action = get_action(event.key);
230 debug("ACTION: "); debug_action(action); debug(" ");
231 layer_switch_debug(); debug("["); debug_dec(default_layer); debug("]\n");
233 switch (action.kind.id) {
238 uint8_t mods = (action.kind.id == ACT_LMODS) ? action.key.mods :
241 uint8_t tmp_mods = host_get_mods();
244 host_send_keyboard_report();
246 register_code(action.key.code);
247 if (mods && action.key.code) {
248 host_set_mods(tmp_mods);
249 host_send_keyboard_report();
252 if (mods && !action.key.code) {
254 host_send_keyboard_report();
256 unregister_code(action.key.code);
263 uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods :
265 switch (action.layer.code) {
269 if (tap_count == 0) {
270 debug("MODS_TAP: Oneshot: add_mods\n");
273 else if (tap_count == 1) {
274 debug("MODS_TAP: Oneshot: start\n");
275 oneshot_start(mods, event.time);
277 else if (tap_count == TAPPING_TOGGLE) {
278 debug("MODS_TAP: Oneshot: toggle\n");
282 debug("MODS_TAP: Oneshot: cancel&add_mods\n");
283 // double tap cancels oneshot and works as normal modifier.
288 if (tap_count == 0) {
289 debug("MODS_TAP: Oneshot: cancel/del_mods\n");
290 // cancel oneshot on hold
294 else if (tap_count == 1) {
295 debug("MODS_TAP: Oneshot: del_mods\n");
300 debug("MODS_TAP: Oneshot: del_mods\n");
309 if (waiting_buffer_has_anykey_pressed()) {
310 debug("MODS_TAP: Tap: Cancel: add_mods\n");
311 // ad hoc: set 0 to cancel tap
312 record->tap_count = 0;
315 debug("MODS_TAP: Tap: register_code\n");
316 register_code(action.key.code);
319 debug("MODS_TAP: No tap: add_mods\n");
324 debug("MODS_TAP: Tap: unregister_code\n");
325 unregister_code(action.key.code);
327 debug("MODS_TAP: No tap: add_mods\n");
336 /* other HID usage */
338 #ifdef EXTRAKEY_ENABLE
339 switch (action.usage.page) {
342 host_system_send(action.usage.code);
349 host_consumer_send(action.usage.code);
351 host_consumer_send(0);
360 #ifdef MOUSEKEY_ENABLE
362 mousekey_on(action.key.code);
365 mousekey_off(action.key.code);
373 switch (action.layer.code) {
374 case LAYER_MOMENTARY: /* momentary */
376 layer_switch_move(action.layer.val);
379 // NOTE: This is needed by legacy keymap support
380 layer_switch_move(0);
385 layer_switch_move(action.layer.val);
388 case LAYER_ON_RELEASE:
389 if (!event.pressed) {
390 layer_switch_move(action.layer.val);
394 layer_switch_move(action.layer.val);
396 case LAYER_TAP_TOGGLE: /* switch on hold and toggle on several taps */
398 if (tap_count < TAPPING_TOGGLE) {
399 layer_switch_move(action.layer.val);
402 if (tap_count >= TAPPING_TOGGLE) {
403 debug("LAYER_PRESSED: tap toggle.\n");
404 layer_switch_move(action.layer.val);
408 case LAYER_SET_DEFAULT_ON_PRESS:
410 default_layer = action.layer.val;
411 layer_switch_move(0);
414 case LAYER_SET_DEFAULT_ON_RELEASE:
415 if (!event.pressed) {
416 default_layer = action.layer.val;
417 layer_switch_move(0);
420 case LAYER_SET_DEFAULT_ON_BOTH:
421 default_layer = action.layer.val;
422 layer_switch_move(0);
427 if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
428 debug("LAYER_SET: Tap: register_code\n");
429 register_code(action.layer.code);
431 debug("LAYER_SET: No tap: layer_set(on press)\n");
432 layer_switch_move(action.layer.val);
435 if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
436 debug("LAYER_SET: Tap: unregister_code\n");
437 unregister_code(action.layer.code);
439 // NOTE: This is needed by legacy keymap support
440 debug("LAYER_SET: No tap: return to default layer(on release)\n");
441 layer_switch_move(0);
448 switch (action.layer.code) {
449 case LAYER_MOMENTARY: /* momentary */
451 layer_switch_move(layer_switch_get_layer() | action.layer.val);
453 layer_switch_move(layer_switch_get_layer() & ~action.layer.val);
458 layer_switch_move(layer_switch_get_layer() ^ action.layer.val);
461 case LAYER_ON_RELEASE:
462 if (!event.pressed) {
463 layer_switch_move(layer_switch_get_layer() ^ action.layer.val);
467 layer_switch_move(layer_switch_get_layer() ^ action.layer.val);
469 case LAYER_TAP_TOGGLE: /* switch on hold and toggle on several taps */
471 if (tap_count < TAPPING_TOGGLE) {
472 debug("LAYER_BIT: tap toggle(press).\n");
473 layer_switch_move(layer_switch_get_layer() ^ action.layer.val);
476 if (tap_count <= TAPPING_TOGGLE) {
477 debug("LAYER_BIT: tap toggle(release).\n");
478 layer_switch_move(layer_switch_get_layer() ^ action.layer.val);
482 case LAYER_SET_DEFAULT_ON_PRESS:
484 default_layer = default_layer ^ action.layer.val;
485 layer_switch_move(default_layer);
488 case LAYER_SET_DEFAULT_ON_RELEASE:
489 if (!event.pressed) {
490 default_layer = default_layer ^ action.layer.val;
491 layer_switch_move(default_layer);
494 case LAYER_SET_DEFAULT_ON_BOTH:
495 default_layer = default_layer ^ action.layer.val;
496 layer_switch_move(default_layer);
501 if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
502 debug("LAYER_BIT: Tap: register_code\n");
503 register_code(action.layer.code);
505 debug("LAYER_BIT: No tap: layer_bit(on press)\n");
506 layer_switch_move(layer_switch_get_layer() ^ action.layer.val);
509 if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
510 debug("LAYER_BIT: Tap: unregister_code\n");
511 unregister_code(action.layer.code);
513 debug("LAYER_BIT: No tap: layer_bit(on release)\n");
514 layer_switch_move(layer_switch_get_layer() ^ action.layer.val);
520 case ACT_LAYER_SWITCH:
521 switch (action.layer.code) {
522 case LAYER_MOMENTARY: /* momentary */
524 layer_switch_on(action.layer.val);
526 layer_switch_off(action.layer.val);
531 layer_switch_invert(action.layer.val);
534 case LAYER_ON_RELEASE:
535 if (!event.pressed) {
536 layer_switch_invert(action.layer.val);
540 layer_switch_invert(action.layer.val);
542 case LAYER_TAP_TOGGLE: /* switch on hold and toggle on several taps */
544 if (tap_count < TAPPING_TOGGLE) {
545 debug("LAYER_SWITCH: tap toggle(press).\n");
546 layer_switch_invert(action.layer.val);
549 if (tap_count <= TAPPING_TOGGLE) {
550 debug("LAYER_SWITCH: tap toggle(release).\n");
551 layer_switch_invert(action.layer.val);
558 if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
559 debug("LAYER_SWITCH: Tap: register_code\n");
560 register_code(action.layer.code);
562 debug("LAYER_SWITCH: No tap: layer_switch on press\n");
563 layer_switch_invert(action.layer.val);
566 if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
567 debug("LAYER_SWITCH: Tap: unregister_code\n");
568 unregister_code(action.layer.code);
570 debug("LAYER_SWITCH: No tap: layer_switch on release\n");
571 layer_switch_invert(action.layer.val);
585 action_function(record, action.func.id, action.func.opt);
594 * Rule: Tap key is typed(pressed and released) within TAPPING_TERM.
595 * (without interfering by typing other key)
597 /* return true when key event is processed or consumed. */
598 static bool process_tapping(keyrecord_t *keyp)
600 keyevent_t event = keyp->event;
603 if (IS_TAPPING_PRESSED()) {
604 if (WITHIN_TAPPING_TERM(event)) {
605 if (tapping_key.tap_count == 0) {
606 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
608 debug("Tapping: First tap(0->1).\n");
609 tapping_key.tap_count = 1;
611 process_action(&tapping_key);
614 keyp->tap_count = tapping_key.tap_count;
617 #if TAPPING_TERM >= 500
618 /* This can prevent from typing some tap keys in a row at a time. */
619 else if (!event.pressed && waiting_buffer_typed(event)) {
620 // other key typed. not tap.
621 debug("Tapping: End. No tap. Interfered by typing key\n");
622 process_action(&tapping_key);
623 tapping_key = (keyrecord_t){};
631 // other key events shall be enq'd till tapping state settles.
637 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
638 debug("Tapping: Tap release("); debug_dec(tapping_key.tap_count); debug(")\n");
639 keyp->tap_count = tapping_key.tap_count;
640 process_action(keyp);
645 else if (is_tap_key(keyp->event.key) && event.pressed) {
646 if (tapping_key.tap_count > 1) {
647 debug("Tapping: Start new tap with releasing last tap(>1).\n");
649 process_action(&(keyrecord_t){
650 .tap_count = tapping_key.tap_count,
651 .event.key = tapping_key.event.key,
652 .event.time = event.time,
653 .event.pressed = false
656 debug("Tapping: Start while last tap(1).\n");
659 waiting_buffer_scan_tap();
664 if (!IS_NOEVENT(keyp->event)) {
665 debug("Tapping: key event while last tap(>0).\n");
667 process_action(keyp);
672 // after TAPPING_TERM
674 if (tapping_key.tap_count == 0) {
675 debug("Tapping: End. Timeout. Not tap(0): ");
676 debug_event(event); debug("\n");
677 process_action(&tapping_key);
678 tapping_key = (keyrecord_t){};
682 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
683 debug("Tapping: End. last timeout tap release(>0).");
684 keyp->tap_count = tapping_key.tap_count;
685 process_action(keyp);
686 tapping_key = (keyrecord_t){};
689 else if (is_tap_key(keyp->event.key) && event.pressed) {
690 if (tapping_key.tap_count > 1) {
691 debug("Tapping: Start new tap with releasing last timeout tap(>1).\n");
693 process_action(&(keyrecord_t){
694 .tap_count = tapping_key.tap_count,
695 .event.key = tapping_key.event.key,
696 .event.time = event.time,
697 .event.pressed = false
700 debug("Tapping: Start while last timeout tap(1).\n");
703 waiting_buffer_scan_tap();
708 if (!IS_NOEVENT(keyp->event)) {
709 debug("Tapping: key event while last timeout tap(>0).\n");
711 process_action(keyp);
716 } else if (IS_TAPPING_RELEASED()) {
717 if (WITHIN_TAPPING_TERM(event)) {
718 if (tapping_key.tap_count > 0 && IS_TAPPING_KEY(event.key) && event.pressed) {
720 keyp->tap_count = tapping_key.tap_count + 1;
721 debug("Tapping: Tap press("); debug_dec(keyp->tap_count); debug(")\n");
722 process_action(keyp);
726 } else if (event.pressed && is_tap_key(event.key)) {
727 // Sequential tap can be interfered with other tap key.
728 debug("Tapping: Start with interfering other tap.\n");
730 waiting_buffer_scan_tap();
734 if (!IS_NOEVENT(keyp->event)) debug("Tapping: other key just after tap.\n");
735 process_action(keyp);
739 // timeout. no sequential tap.
740 debug("Tapping: End(Timeout after releasing last tap): ");
741 debug_event(event); debug("\n");
742 tapping_key = (keyrecord_t){};
747 // not tapping satate
749 if (event.pressed && is_tap_key(event.key)) {
750 debug("Tapping: Start(Press tap key).\n");
752 waiting_buffer_scan_tap();
756 process_action(keyp);
762 /* scan buffer for tapping */
763 static void waiting_buffer_scan_tap(void)
765 // tapping already is settled
766 if (tapping_key.tap_count > 0) return;
767 // invalid state: tapping_key released && tap_count == 0
768 if (!tapping_key.event.pressed) return;
770 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
771 if (IS_TAPPING_KEY(waiting_buffer[i].event.key) &&
772 !waiting_buffer[i].event.pressed &&
773 WITHIN_TAPPING_TERM(waiting_buffer[i].event)) {
774 tapping_key.tap_count = 1;
775 waiting_buffer[i].tap_count = 1;
776 process_action(&tapping_key);
778 debug("waiting_buffer_scan_tap: found at ["); debug_dec(i); debug("]\n");
779 debug_waiting_buffer();
788 * Utilities for actions.
790 void register_code(uint8_t code)
795 else if IS_KEY(code) {
796 // TODO: should push command_proc out of this block?
797 if (command_proc(code)) return;
799 if (oneshot_state.mods && oneshot_state.ready && !oneshot_state.disabled) {
800 uint8_t tmp_mods = host_get_mods();
801 host_add_mods(oneshot_state.mods);
803 host_send_keyboard_report();
805 host_set_mods(tmp_mods);
806 oneshot_state.ready = false;
809 host_send_keyboard_report();
812 else if IS_MOD(code) {
813 host_add_mods(MOD_BIT(code));
814 host_send_keyboard_report();
818 void unregister_code(uint8_t code)
822 host_send_keyboard_report();
824 else if IS_MOD(code) {
825 host_del_mods(MOD_BIT(code));
826 host_send_keyboard_report();
830 void add_mods(uint8_t mods)
834 host_send_keyboard_report();
838 void del_mods(uint8_t mods)
842 host_send_keyboard_report();
846 void set_mods(uint8_t mods)
849 host_send_keyboard_report();
852 void clear_keyboard(void)
855 clear_keyboard_but_mods();
858 void clear_keyboard_but_mods(void)
861 host_send_keyboard_report();
862 #ifdef MOUSEKEY_ENABLE
866 #ifdef EXTRAKEY_ENABLE
868 host_consumer_send(0);
872 bool sending_anykey(void)
874 return (host_has_anykey() || host_mouse_in_use() ||
875 host_last_sysytem_report() || host_last_consumer_report());
878 bool is_tap_key(key_t key)
880 action_t action = get_action(key);
882 switch (action.kind.id) {
888 switch (action.layer.code) {
889 case LAYER_MOMENTARY:
891 case LAYER_ON_RELEASE:
893 case LAYER_SET_DEFAULT_ON_PRESS:
894 case LAYER_SET_DEFAULT_ON_RELEASE:
895 case LAYER_SET_DEFAULT_ON_BOTH:
897 case LAYER_TAP_TOGGLE:
898 default: /* tap key */
903 if (action.func.opt & FUNC_TAP) { return true; }
913 static void debug_event(keyevent_t event)
915 debug_hex16((event.key.row<<8) | event.key.col);
916 if (event.pressed) debug("d("); else debug("u(");
917 debug_dec(event.time); debug(")");
919 static void debug_record(keyrecord_t record)
921 debug_event(record.event); debug(":"); debug_dec(record.tap_count);
923 static void debug_action(action_t action)
925 switch (action.kind.id) {
926 case ACT_LMODS: debug("ACT_LMODS"); break;
927 case ACT_RMODS: debug("ACT_RMODS"); break;
928 case ACT_LMODS_TAP: debug("ACT_LMODS_TAP"); break;
929 case ACT_RMODS_TAP: debug("ACT_RMODS_TAP"); break;
930 case ACT_USAGE: debug("ACT_USAGE"); break;
931 case ACT_MOUSEKEY: debug("ACT_MOUSEKEY"); break;
932 case ACT_LAYER_SET: debug("ACT_LAYER_SET"); break;
933 case ACT_LAYER_BIT: debug("ACT_LAYER_BIT"); break;
934 case ACT_LAYER_SWITCH: debug("ACT_LAYER_SWITCH"); break;
935 case ACT_MACRO: debug("ACT_MACRO"); break;
936 case ACT_COMMAND: debug("ACT_COMMAND"); break;
937 case ACT_FUNCTION: debug("ACT_FUNCTION"); break;
938 default: debug("UNKNOWN"); break;
941 debug_hex4(action.kind.param>>8);
943 debug_hex8(action.kind.param & 0xff);
946 static void debug_tapping_key(void)
948 debug("TAPPING_KEY="); debug_record(tapping_key); debug("\n");
950 static void debug_waiting_buffer(void)
953 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
954 debug("["); debug_dec(i); debug("]="); debug_record(waiting_buffer[i]); debug(" ");