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/>.
29 /* default layer indicates base layer */
30 uint8_t default_layer = 0;
31 /* current layer indicates active layer at this time */
32 uint8_t current_layer = 0;
35 static void process_action(keyrecord_t *record);
36 static bool process_tapping(keyrecord_t *record);
37 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 static void debug_tapping_key(void);
43 static void debug_waiting_buffer(void);
49 /* period of tapping(ms) */
51 #define TAPPING_TERM 200
54 /* tap count needed for toggling a feature */
55 #ifndef TAPPING_TOGGLE
56 #define TAPPING_TOGGLE 5
59 /* stores a key event of current tap. */
60 static keyrecord_t tapping_key = {};
62 #define IS_TAPPING() !IS_NOEVENT(tapping_key.event)
63 #define IS_TAPPING_PRESSED() (IS_TAPPING() && tapping_key.event.pressed)
64 #define IS_TAPPING_RELEASED() (IS_TAPPING() && !tapping_key.event.pressed)
65 #define IS_TAPPING_KEY(k) (IS_TAPPING() && KEYEQ(tapping_key.event.key, (k)))
66 #define WITHIN_TAPPING_TERM(e) (TIMER_DIFF_16(e.time, tapping_key.event.time) < TAPPING_TERM)
72 * stores key events waiting for settling current tap.
74 #define WAITING_BUFFER_SIZE 8
75 static keyrecord_t waiting_buffer[WAITING_BUFFER_SIZE] = {};
77 /* point to empty cell to enq */
78 static uint8_t waiting_buffer_head = 0;
80 /* point to the oldest data cell to deq */
81 static uint8_t waiting_buffer_tail = 0;
83 static bool waiting_buffer_enq(keyrecord_t record)
85 if (IS_NOEVENT(record.event)) {
89 if ((waiting_buffer_head + 1) % WAITING_BUFFER_SIZE == waiting_buffer_tail) {
90 debug("waiting_buffer_enq: Over flow.\n");
94 waiting_buffer[waiting_buffer_head] = record;
95 waiting_buffer_head = (waiting_buffer_head + 1) % WAITING_BUFFER_SIZE;
97 debug("waiting_buffer_enq: "); debug_waiting_buffer();
101 static void waiting_buffer_clear(void)
103 waiting_buffer_head = 0;
104 waiting_buffer_tail = 0;
107 #if TAPPING_TERM >= 500
108 static bool waiting_buffer_typed(keyevent_t event)
110 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
111 if (KEYEQ(event.key, waiting_buffer[i].event.key) && event.pressed != waiting_buffer[i].event.pressed) {
119 bool waiting_buffer_has_anykey_pressed(void)
121 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
122 if (waiting_buffer[i].event.pressed) return true;
130 * Problem: Want to capitalize like 'The' but the result tends to be 'THe'.
131 * Solution: Oneshot modifier have its effect on only one key coming next.
132 * Tap Shift, then type 't', 'h' and 'e'. Not need to hold Shift key.
134 * Hold: works as normal modifier.
135 * Tap: one shot modifier.
136 * 2 Tap: cancel one shot modifier.
137 * 5-Tap: toggles enable/disable oneshot feature.
146 static void oneshot_start(uint8_t mods, uint16_t time)
148 oneshot_state.mods = mods;
149 oneshot_state.time = time;
150 oneshot_state.ready = true;
153 static void oneshot_cancel(void)
155 oneshot_state.mods = 0;
156 oneshot_state.time = 0;
157 oneshot_state.ready = false;
160 static void oneshot_toggle(void)
162 oneshot_state.disabled = !oneshot_state.disabled;
167 void action_exec(keyevent_t event)
169 if (!IS_NOEVENT(event)) {
170 debug("\n---- action_exec: start -----\n");
171 debug("EVENT: "); debug_event(event); debug("\n");
174 keyrecord_t record = { .event = event };
176 // pre-process on tapping
177 if (process_tapping(&record)) {
178 if (!IS_NOEVENT(record.event)) {
179 debug("processed: "); debug_record(record); debug("\n");
183 if (!waiting_buffer_enq(record)) {
184 // clear all in case of overflow.
185 debug("OVERFLOW: CLEAR ALL STATES\n");
187 waiting_buffer_clear();
188 tapping_key = (keyrecord_t){};
192 // process waiting_buffer
193 if (!IS_NOEVENT(event) && waiting_buffer_head != waiting_buffer_tail) {
194 debug("---- action_exec: process waiting_buffer -----\n");
197 for (; waiting_buffer_tail != waiting_buffer_head; waiting_buffer_tail = (waiting_buffer_tail + 1) % WAITING_BUFFER_SIZE) {
198 if (process_tapping(&waiting_buffer[waiting_buffer_tail])) {
199 debug("processed: waiting_buffer["); debug_dec(waiting_buffer_tail); debug("] = ");
200 debug_record(waiting_buffer[waiting_buffer_tail]); debug("\n\n");
205 if (!IS_NOEVENT(event)) {
210 static action_t get_action(key_t key)
212 action_t action = action_for_key(current_layer, key);
214 /* Transparently use default layer */
215 if (action.code == ACTION_TRANSPARENT) {
216 // TODO: layer stacking
217 action = action_for_key(default_layer, key);
218 debug("TRNASPARENT: "); debug_hex16(action.code); debug("\n");
223 static void process_action(keyrecord_t *record)
225 keyevent_t event = record->event;
226 uint8_t tap_count = record->tap_count;
228 if (IS_NOEVENT(event)) { return; }
230 action_t action = get_action(event.key);
231 debug("ACTION: "); debug_action(action); 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 by holding.
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(action.layer.val);
379 // NOTE: This is needed by legacy keymap support
380 layer_switch(default_layer);
385 layer_switch(action.layer.val);
388 case LAYER_ON_RELEASE:
389 if (!event.pressed) {
390 layer_switch(action.layer.val);
393 case LAYER_DEFAULT: /* default layer */
394 switch (action.layer.val) {
395 case DEFAULT_ON_BOTH:
396 layer_switch(default_layer);
398 case DEFAULT_ON_PRESS:
400 layer_switch(default_layer);
403 case DEFAULT_ON_RELEASE:
404 if (!event.pressed) {
405 layer_switch(default_layer);
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_CHANGE_DEFAULT: /* change default layer */
424 default_layer = action.layer.val;
425 layer_switch(default_layer);
428 default: /* switch layer on hold and key on tap*/
431 debug("LAYER_PRESSED: Tap: register_code\n");
432 register_code(action.layer.code);
434 debug("LAYER_PRESSED: No tap: layer_switch\n");
435 layer_switch(action.layer.val);
439 debug("LAYER_PRESSED: Tap: unregister_code\n");
440 unregister_code(action.layer.code);
442 //debug("LAYER_PRESSED: No tap: NO ACTION\n");
443 // NOTE: This is needed by legacy keymap support
444 debug("LAYER_PRESSED: No tap: return to default layer\n");
445 layer_switch(default_layer);
452 switch (action.layer.code) {
453 case LAYER_MOMENTARY: /* momentary */
455 layer_switch(current_layer ^ action.layer.val);
457 layer_switch(current_layer ^ action.layer.val);
462 layer_switch(current_layer ^ action.layer.val);
465 case LAYER_ON_RELEASE:
466 if (!event.pressed) {
467 layer_switch(current_layer ^ action.layer.val);
470 case LAYER_TAP_TOGGLE: /* switch on hold and toggle on several taps */
472 if (tap_count < TAPPING_TOGGLE) {
473 debug("LAYER_BIT: tap toggle(press).\n");
474 layer_switch(current_layer ^ action.layer.val);
477 if (tap_count <= TAPPING_TOGGLE) {
478 debug("LAYER_BIT: tap toggle(release).\n");
479 layer_switch(current_layer ^ action.layer.val);
484 // change default layer
486 default_layer = current_layer ^ action.layer.val;
487 layer_switch(default_layer);
489 default_layer = current_layer ^ action.layer.val;
490 layer_switch(default_layer);
496 if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
497 debug("LAYER_BIT: Tap: register_code\n");
498 register_code(action.layer.code);
500 debug("LAYER_BIT: No tap: layer_switch(bit on)\n");
501 layer_switch(current_layer ^ action.layer.val);
504 if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
505 debug("LAYER_BIT: Tap: unregister_code\n");
506 unregister_code(action.layer.code);
508 debug("LAYER_BIT: No tap: layer_switch(bit off)\n");
509 layer_switch(current_layer ^ action.layer.val);
523 action_function(record, action.func.id, action.func.opt);
532 * Rule: Tap key is typed(pressed and released) within TAPPING_TERM.
533 * (without interfering by typing other key)
535 /* return true when key event is processed or consumed. */
536 static bool process_tapping(keyrecord_t *keyp)
538 keyevent_t event = keyp->event;
541 if (IS_TAPPING_PRESSED()) {
542 if (WITHIN_TAPPING_TERM(event)) {
543 if (tapping_key.tap_count == 0) {
544 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
546 debug("Tapping: First tap(0->1).\n");
547 tapping_key.tap_count = 1;
549 process_action(&tapping_key);
552 keyp->tap_count = tapping_key.tap_count;
555 #if TAPPING_TERM >= 500
556 /* This can prevent from typing some tap keys in a row at a time. */
557 else if (!event.pressed && waiting_buffer_typed(event)) {
558 // other key typed. not tap.
559 debug("Tapping: End. No tap. Interfered by typing key\n");
560 process_action(&tapping_key);
561 tapping_key = (keyrecord_t){};
569 // other key events shall be enq'd till tapping state settles.
575 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
576 debug("Tapping: Tap release("); debug_dec(tapping_key.tap_count); debug(")\n");
577 keyp->tap_count = tapping_key.tap_count;
578 process_action(keyp);
583 else if (is_tap_key(keyp->event.key) && event.pressed) {
584 if (tapping_key.tap_count > 1) {
585 debug("Tapping: Start new tap with releasing last tap(>1).\n");
587 process_action(&(keyrecord_t){
588 .tap_count = tapping_key.tap_count,
589 .event.key = tapping_key.event.key,
590 .event.time = event.time,
591 .event.pressed = false
594 debug("Tapping: Start while last tap(1).\n");
597 waiting_buffer_scan_tap();
602 if (!IS_NOEVENT(keyp->event)) {
603 debug("Tapping: key event while last tap(>0).\n");
605 process_action(keyp);
610 // after TAPPING_TERM
612 if (tapping_key.tap_count == 0) {
613 debug("Tapping: End. Timeout. Not tap(0): ");
614 debug_event(event); debug("\n");
615 process_action(&tapping_key);
616 tapping_key = (keyrecord_t){};
620 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
621 debug("Tapping: End. last timeout tap release(>0).");
622 keyp->tap_count = tapping_key.tap_count;
623 process_action(keyp);
624 tapping_key = (keyrecord_t){};
627 else if (is_tap_key(keyp->event.key) && event.pressed) {
628 if (tapping_key.tap_count > 1) {
629 debug("Tapping: Start new tap with releasing last timeout tap(>1).\n");
631 process_action(&(keyrecord_t){
632 .tap_count = tapping_key.tap_count,
633 .event.key = tapping_key.event.key,
634 .event.time = event.time,
635 .event.pressed = false
638 debug("Tapping: Start while last timeout tap(1).\n");
641 waiting_buffer_scan_tap();
646 if (!IS_NOEVENT(keyp->event)) {
647 debug("Tapping: key event while last timeout tap(>0).\n");
649 process_action(keyp);
654 } else if (IS_TAPPING_RELEASED()) {
655 if (WITHIN_TAPPING_TERM(event)) {
656 if (tapping_key.tap_count > 0 && IS_TAPPING_KEY(event.key) && event.pressed) {
658 keyp->tap_count = tapping_key.tap_count + 1;
659 debug("Tapping: Tap press("); debug_dec(keyp->tap_count); debug(")\n");
660 process_action(keyp);
664 } else if (event.pressed && is_tap_key(event.key)) {
665 // Sequential tap can be interfered with other tap key.
666 debug("Tapping: Start with interfering other tap.\n");
668 waiting_buffer_scan_tap();
672 if (!IS_NOEVENT(keyp->event)) debug("Tapping: other key just after tap.\n");
673 process_action(keyp);
677 // timeout. no sequential tap.
678 debug("Tapping: End(Timeout after releasing last tap): ");
679 debug_event(event); debug("\n");
680 tapping_key = (keyrecord_t){};
685 // not tapping satate
687 if (event.pressed && is_tap_key(event.key)) {
688 debug("Tapping: Start(Press tap key).\n");
690 waiting_buffer_scan_tap();
694 process_action(keyp);
700 /* scan buffer for tapping */
701 static void waiting_buffer_scan_tap(void)
703 // tapping already is settled
704 if (tapping_key.tap_count > 0) return;
705 // invalid state: tapping_key released && tap_count == 0
706 if (!tapping_key.event.pressed) return;
708 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
709 if (IS_TAPPING_KEY(waiting_buffer[i].event.key) &&
710 !waiting_buffer[i].event.pressed &&
711 WITHIN_TAPPING_TERM(waiting_buffer[i].event)) {
712 tapping_key.tap_count = 1;
713 waiting_buffer[i].tap_count = 1;
714 process_action(&tapping_key);
716 debug("waiting_buffer_scan_tap: found at ["); debug_dec(i); debug("]\n");
717 debug_waiting_buffer();
726 * Utilities for actions.
728 void register_code(uint8_t code)
733 else if IS_KEY(code) {
734 // TODO: should push command_proc out of this block?
735 if (command_proc(code)) return;
737 if (oneshot_state.mods && oneshot_state.ready && !oneshot_state.disabled) {
738 uint8_t tmp_mods = host_get_mods();
739 host_add_mods(oneshot_state.mods);
741 host_send_keyboard_report();
743 host_set_mods(tmp_mods);
744 oneshot_state.ready = false;
747 host_send_keyboard_report();
750 else if IS_MOD(code) {
751 host_add_mods(MOD_BIT(code));
752 host_send_keyboard_report();
756 void unregister_code(uint8_t code)
760 host_send_keyboard_report();
762 else if IS_MOD(code) {
763 host_del_mods(MOD_BIT(code));
764 host_send_keyboard_report();
768 void add_mods(uint8_t mods)
772 host_send_keyboard_report();
776 void del_mods(uint8_t mods)
780 host_send_keyboard_report();
784 void set_mods(uint8_t mods)
787 host_send_keyboard_report();
790 void clear_keyboard(void)
793 clear_keyboard_but_mods();
796 void clear_keyboard_but_mods(void)
799 host_send_keyboard_report();
800 #ifdef MOUSEKEY_ENABLE
804 #ifdef EXTRAKEY_ENABLE
806 host_consumer_send(0);
810 bool sending_anykey(void)
812 return (host_has_anykey() || host_mouse_in_use() ||
813 host_last_sysytem_report() || host_last_consumer_report());
816 void layer_switch(uint8_t new_layer)
818 if (current_layer != new_layer) {
819 debug("Layer Switch: "); debug_hex(current_layer);
820 debug(" -> "); debug_hex(new_layer); debug("\n");
822 current_layer = new_layer;
823 clear_keyboard_but_mods(); // To avoid stuck keys
824 // NOTE: update mods with full scan of matrix? if modifier changes between layers
828 bool is_tap_key(key_t key)
830 action_t action = get_action(key);
832 switch (action.kind.id) {
838 switch (action.layer.code) {
839 case LAYER_MOMENTARY:
841 case LAYER_ON_RELEASE:
844 case LAYER_TAP_TOGGLE:
845 default: /* tap key */
850 if (action.func.opt & FUNC_TAP) { return true; }
860 static void debug_event(keyevent_t event)
862 debug_hex16((event.key.row<<8) | event.key.col);
863 if (event.pressed) debug("d("); else debug("u(");
864 debug_dec(event.time); debug(")");
866 static void debug_record(keyrecord_t record)
868 debug_event(record.event); debug(":"); debug_dec(record.tap_count);
870 static void debug_action(action_t action)
872 switch (action.kind.id) {
873 case ACT_LMODS: debug("ACT_LMODS"); break;
874 case ACT_RMODS: debug("ACT_RMODS"); break;
875 case ACT_LMODS_TAP: debug("ACT_LMODS_TAP"); break;
876 case ACT_RMODS_TAP: debug("ACT_RMODS_TAP"); break;
877 case ACT_USAGE: debug("ACT_USAGE"); break;
878 case ACT_MOUSEKEY: debug("ACT_MOUSEKEY"); break;
879 case ACT_LAYER: debug("ACT_LAYER"); break;
880 case ACT_LAYER_BIT: debug("ACT_LAYER_BIT"); break;
881 case ACT_MACRO: debug("ACT_MACRO"); break;
882 case ACT_COMMAND: debug("ACT_COMMAND"); break;
883 case ACT_FUNCTION: debug("ACT_FUNCTION"); break;
884 default: debug("UNKNOWN"); break;
887 debug_hex4(action.kind.param>>8);
889 debug_hex8(action.kind.param & 0xff);
892 static void debug_tapping_key(void)
894 debug("TAPPING_KEY="); debug_record(tapping_key); debug("\n");
896 static void debug_waiting_buffer(void)
899 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
900 debug("["); debug_dec(i); debug("]="); debug_record(waiting_buffer[i]); debug(" ");