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 switch (action
.layer
.val
& 0x03) {
372 if (!event
.pressed
) {
383 /* Keymap Reset default layer */
384 case (OP_RESET
| ON_PRESS
):
388 default_layer_set(action
.layer
.val
);
391 case (OP_RESET
| ON_RELEASE
):
392 if (!event
.pressed
) {
395 default_layer_set(action
.layer
.val
);
398 case (OP_RESET
| ON_BOTH
):
401 default_layer_set(action
.layer
.val
);
404 /* Keymap Bit invert */
406 /* with tap toggle */
408 if (tap_count
< TAPPING_TOGGLE
) {
409 debug("KEYMAP_INV: tap toggle(press).\n");
410 keymap_invert(action
.layer
.val
);
413 if (tap_count
<= TAPPING_TOGGLE
) {
414 debug("KEYMAP_INV: tap toggle(release).\n");
415 keymap_invert(action
.layer
.val
);
419 case (OP_INV
| ON_PRESS
):
421 keymap_invert(action
.layer
.val
);
424 case (OP_INV
| ON_RELEASE
):
425 if (!event
.pressed
) {
426 keymap_invert(action
.layer
.val
);
429 case (OP_INV
| ON_BOTH
):
430 keymap_invert(action
.layer
.val
);
436 keymap_on(action
.layer
.val
);
438 keymap_off(action
.layer
.val
);
441 case (OP_ON
| ON_PRESS
):
443 keymap_on(action
.layer
.val
);
446 case (OP_ON
| ON_RELEASE
):
447 if (!event
.pressed
) {
448 keymap_on(action
.layer
.val
);
451 case (OP_ON
| ON_BOTH
):
452 keymap_on(action
.layer
.val
);
458 keymap_off(action
.layer
.val
);
460 keymap_on(action
.layer
.val
);
463 case (OP_OFF
| ON_PRESS
):
465 keymap_off(action
.layer
.val
);
468 case (OP_OFF
| ON_RELEASE
):
469 if (!event
.pressed
) {
470 keymap_off(action
.layer
.val
);
473 case (OP_OFF
| ON_BOTH
):
474 keymap_off(action
.layer
.val
);
480 keymap_set(action
.layer
.val
);
485 case (OP_SET
| ON_PRESS
):
487 keymap_set(action
.layer
.val
);
490 case (OP_SET
| ON_RELEASE
):
491 if (!event
.pressed
) {
492 keymap_set(action
.layer
.val
);
495 case (OP_SET
| ON_BOTH
):
496 keymap_set(action
.layer
.val
);
499 /* Keymap Bit invert with tap key */
503 debug("KEYMAP_TAP_KEY: Tap: register_code\n");
504 register_code(action
.layer
.code
);
506 debug("KEYMAP_TAP_KEY: No tap: On on press\n");
507 keymap_on(action
.layer
.val
);
511 debug("KEYMAP_TAP_KEY: Tap: unregister_code\n");
512 unregister_code(action
.layer
.code
);
514 debug("KEYMAP_TAP_KEY: No tap: Off on release\n");
515 keymap_off(action
.layer
.val
);
523 switch (action
.layer
.code
) {
524 // Overlay Invert bit4
526 if (action
.layer
.val
== 0) {
529 overlay_set(overlay_stat
^ action
.layer
.val
);
533 if (action
.layer
.val
== 0) {
534 if (event
.pressed
) overlay_clear();
536 overlay_set(overlay_stat
^ action
.layer
.val
<<4);
540 if (action
.layer
.val
== 0) {
541 if (!event
.pressed
) overlay_clear();
543 overlay_set(overlay_stat
^ action
.layer
.val
<<8);
547 if (action
.layer
.val
== 0) {
550 overlay_set(overlay_stat
^ action
.layer
.val
<<12);
554 /* Overlay Bit invert */
556 /* with tap toggle */
558 if (tap_count
< TAPPING_TOGGLE
) {
559 debug("OVERLAY_INV: tap toggle(press).\n");
560 overlay_invert(action
.layer
.val
);
563 if (tap_count
<= TAPPING_TOGGLE
) {
564 debug("OVERLAY_INV: tap toggle(release).\n");
565 overlay_invert(action
.layer
.val
);
569 case (OP_INV
| ON_PRESS
):
571 overlay_invert(action
.layer
.val
);
574 case (OP_INV
| ON_RELEASE
):
575 if (!event
.pressed
) {
576 overlay_invert(action
.layer
.val
);
579 case (OP_INV
| ON_BOTH
):
580 overlay_invert(action
.layer
.val
);
586 overlay_on(action
.layer
.val
);
588 overlay_off(action
.layer
.val
);
591 case (OP_ON
| ON_PRESS
):
593 overlay_on(action
.layer
.val
);
596 case (OP_ON
| ON_RELEASE
):
597 if (!event
.pressed
) {
598 overlay_on(action
.layer
.val
);
601 case (OP_ON
| ON_BOTH
):
602 overlay_on(action
.layer
.val
);
605 /* Overlay Bit off */
608 overlay_off(action
.layer
.val
);
610 overlay_on(action
.layer
.val
);
613 case (OP_OFF
| ON_PRESS
):
615 overlay_off(action
.layer
.val
);
618 case (OP_OFF
| ON_RELEASE
):
619 if (!event
.pressed
) {
620 overlay_off(action
.layer
.val
);
623 case (OP_OFF
| ON_BOTH
):
624 overlay_off(action
.layer
.val
);
627 /* Overlay Bit set */
630 overlay_move(action
.layer
.val
);
635 case (OP_SET
| ON_PRESS
):
637 overlay_move(action
.layer
.val
);
640 case (OP_SET
| ON_RELEASE
):
641 if (!event
.pressed
) {
642 overlay_move(action
.layer
.val
);
645 case (OP_SET
| ON_BOTH
):
646 overlay_move(action
.layer
.val
);
649 /* Overlay Bit invert with tap key */
653 debug("OVERLAY_TAP_KEY: Tap: register_code\n");
654 register_code(action
.layer
.code
);
656 debug("OVERLAY_TAP_KEY: No tap: On on press\n");
657 overlay_on(action
.layer
.val
);
661 debug("OVERLAY_TAP_KEY: Tap: unregister_code\n");
662 unregister_code(action
.layer
.code
);
664 debug("OVERLAY_TAP_KEY: No tap: Off on release\n");
665 overlay_off(action
.layer
.val
);
679 action_function(record
, action
.func
.id
, action
.func
.opt
);
688 * Rule: Tap key is typed(pressed and released) within TAPPING_TERM.
689 * (without interfering by typing other key)
691 /* return true when key event is processed or consumed. */
692 static bool process_tapping(keyrecord_t
*keyp
)
694 keyevent_t event
= keyp
->event
;
697 if (IS_TAPPING_PRESSED()) {
698 if (WITHIN_TAPPING_TERM(event
)) {
699 if (tapping_key
.tap_count
== 0) {
700 if (IS_TAPPING_KEY(event
.key
) && !event
.pressed
) {
702 debug("Tapping: First tap(0->1).\n");
703 tapping_key
.tap_count
= 1;
705 process_action(&tapping_key
);
708 keyp
->tap_count
= tapping_key
.tap_count
;
711 #if TAPPING_TERM >= 500
712 /* This can prevent from typing some tap keys in a row at a time. */
713 else if (!event
.pressed
&& waiting_buffer_typed(event
)) {
714 // other key typed. not tap.
715 debug("Tapping: End. No tap. Interfered by typing key\n");
716 process_action(&tapping_key
);
717 tapping_key
= (keyrecord_t
){};
725 // other key events shall be enq'd till tapping state settles.
731 if (IS_TAPPING_KEY(event
.key
) && !event
.pressed
) {
732 debug("Tapping: Tap release("); debug_dec(tapping_key
.tap_count
); debug(")\n");
733 keyp
->tap_count
= tapping_key
.tap_count
;
734 process_action(keyp
);
739 else if (is_tap_key(keyp
->event
.key
) && event
.pressed
) {
740 if (tapping_key
.tap_count
> 1) {
741 debug("Tapping: Start new tap with releasing last tap(>1).\n");
743 process_action(&(keyrecord_t
){
744 .tap_count
= tapping_key
.tap_count
,
745 .event
.key
= tapping_key
.event
.key
,
746 .event
.time
= event
.time
,
747 .event
.pressed
= false
750 debug("Tapping: Start while last tap(1).\n");
753 waiting_buffer_scan_tap();
758 if (!IS_NOEVENT(keyp
->event
)) {
759 debug("Tapping: key event while last tap(>0).\n");
761 process_action(keyp
);
766 // after TAPPING_TERM
768 if (tapping_key
.tap_count
== 0) {
769 debug("Tapping: End. Timeout. Not tap(0): ");
770 debug_event(event
); debug("\n");
771 process_action(&tapping_key
);
772 tapping_key
= (keyrecord_t
){};
776 if (IS_TAPPING_KEY(event
.key
) && !event
.pressed
) {
777 debug("Tapping: End. last timeout tap release(>0).");
778 keyp
->tap_count
= tapping_key
.tap_count
;
779 process_action(keyp
);
780 tapping_key
= (keyrecord_t
){};
783 else if (is_tap_key(keyp
->event
.key
) && event
.pressed
) {
784 if (tapping_key
.tap_count
> 1) {
785 debug("Tapping: Start new tap with releasing last timeout tap(>1).\n");
787 process_action(&(keyrecord_t
){
788 .tap_count
= tapping_key
.tap_count
,
789 .event
.key
= tapping_key
.event
.key
,
790 .event
.time
= event
.time
,
791 .event
.pressed
= false
794 debug("Tapping: Start while last timeout tap(1).\n");
797 waiting_buffer_scan_tap();
802 if (!IS_NOEVENT(keyp
->event
)) {
803 debug("Tapping: key event while last timeout tap(>0).\n");
805 process_action(keyp
);
810 } else if (IS_TAPPING_RELEASED()) {
811 if (WITHIN_TAPPING_TERM(event
)) {
812 if (tapping_key
.tap_count
> 0 && IS_TAPPING_KEY(event
.key
) && event
.pressed
) {
814 keyp
->tap_count
= tapping_key
.tap_count
+ 1;
815 debug("Tapping: Tap press("); debug_dec(keyp
->tap_count
); debug(")\n");
816 process_action(keyp
);
820 } else if (event
.pressed
&& is_tap_key(event
.key
)) {
821 // Sequential tap can be interfered with other tap key.
822 debug("Tapping: Start with interfering other tap.\n");
824 waiting_buffer_scan_tap();
828 if (!IS_NOEVENT(keyp
->event
)) debug("Tapping: other key just after tap.\n");
829 process_action(keyp
);
833 // timeout. no sequential tap.
834 debug("Tapping: End(Timeout after releasing last tap): ");
835 debug_event(event
); debug("\n");
836 tapping_key
= (keyrecord_t
){};
841 // not tapping satate
843 if (event
.pressed
&& is_tap_key(event
.key
)) {
844 debug("Tapping: Start(Press tap key).\n");
846 waiting_buffer_scan_tap();
850 process_action(keyp
);
856 /* scan buffer for tapping */
857 static void waiting_buffer_scan_tap(void)
859 // tapping already is settled
860 if (tapping_key
.tap_count
> 0) return;
861 // invalid state: tapping_key released && tap_count == 0
862 if (!tapping_key
.event
.pressed
) return;
864 for (uint8_t i
= waiting_buffer_tail
; i
!= waiting_buffer_head
; i
= (i
+ 1) % WAITING_BUFFER_SIZE
) {
865 if (IS_TAPPING_KEY(waiting_buffer
[i
].event
.key
) &&
866 !waiting_buffer
[i
].event
.pressed
&&
867 WITHIN_TAPPING_TERM(waiting_buffer
[i
].event
)) {
868 tapping_key
.tap_count
= 1;
869 waiting_buffer
[i
].tap_count
= 1;
870 process_action(&tapping_key
);
872 debug("waiting_buffer_scan_tap: found at ["); debug_dec(i
); debug("]\n");
873 debug_waiting_buffer();
882 * Utilities for actions.
884 void register_code(uint8_t code
)
889 else if IS_KEY(code
) {
890 // TODO: should push command_proc out of this block?
891 if (command_proc(code
)) return;
893 if (oneshot_state
.mods
&& oneshot_state
.ready
&& !oneshot_state
.disabled
) {
894 uint8_t tmp_mods
= host_get_mods();
895 host_add_mods(oneshot_state
.mods
);
897 host_send_keyboard_report();
899 host_set_mods(tmp_mods
);
900 oneshot_state
.ready
= false;
903 host_send_keyboard_report();
906 else if IS_MOD(code
) {
907 host_add_mods(MOD_BIT(code
));
908 host_send_keyboard_report();
912 void unregister_code(uint8_t code
)
916 host_send_keyboard_report();
918 else if IS_MOD(code
) {
919 host_del_mods(MOD_BIT(code
));
920 host_send_keyboard_report();
924 void add_mods(uint8_t mods
)
928 host_send_keyboard_report();
932 void del_mods(uint8_t mods
)
936 host_send_keyboard_report();
940 void set_mods(uint8_t mods
)
943 host_send_keyboard_report();
946 void clear_keyboard(void)
949 clear_keyboard_but_mods();
952 void clear_keyboard_but_mods(void)
955 host_send_keyboard_report();
956 #ifdef MOUSEKEY_ENABLE
960 #ifdef EXTRAKEY_ENABLE
962 host_consumer_send(0);
966 bool sending_anykey(void)
968 return (host_has_anykey() || host_mouse_in_use() ||
969 host_last_sysytem_report() || host_last_consumer_report());
972 bool is_tap_key(key_t key
)
974 action_t action
= layer_switch_get_action(key
);
976 switch (action
.kind
.id
) {
982 switch (action
.layer
.code
) {
983 case 0x04 ... 0xEF: /* tap key */
990 if (action
.func
.opt
& FUNC_TAP
) { return true; }
1000 static void debug_event(keyevent_t event
)
1002 debug_hex16((event
.key
.row
<<8) | event
.key
.col
);
1003 if (event
.pressed
) debug("d("); else debug("u(");
1004 debug_dec(event
.time
); debug(")");
1006 static void debug_record(keyrecord_t record
)
1008 debug_event(record
.event
); debug(":"); debug_dec(record
.tap_count
);
1010 static void debug_action(action_t action
)
1012 switch (action
.kind
.id
) {
1013 case ACT_LMODS
: debug("ACT_LMODS"); break;
1014 case ACT_RMODS
: debug("ACT_RMODS"); break;
1015 case ACT_LMODS_TAP
: debug("ACT_LMODS_TAP"); break;
1016 case ACT_RMODS_TAP
: debug("ACT_RMODS_TAP"); break;
1017 case ACT_USAGE
: debug("ACT_USAGE"); break;
1018 case ACT_MOUSEKEY
: debug("ACT_MOUSEKEY"); break;
1019 case ACT_KEYMAP
: debug("ACT_KEYMAP"); break;
1020 case ACT_OVERLAY
: debug("ACT_OVERLAY"); break;
1021 case ACT_MACRO
: debug("ACT_MACRO"); break;
1022 case ACT_COMMAND
: debug("ACT_COMMAND"); break;
1023 case ACT_FUNCTION
: debug("ACT_FUNCTION"); break;
1024 default: debug("UNKNOWN"); break;
1027 debug_hex4(action
.kind
.param
>>8);
1029 debug_hex8(action
.kind
.param
& 0xff);
1032 static void debug_tapping_key(void)
1034 debug("TAPPING_KEY="); debug_record(tapping_key
); debug("\n");
1036 static void debug_waiting_buffer(void)
1039 for (uint8_t i
= waiting_buffer_tail
; i
!= waiting_buffer_head
; i
= (i
+ 1) % WAITING_BUFFER_SIZE
) {
1040 debug("["); debug_dec(i
); debug("]="); debug_record(waiting_buffer
[i
]); debug(" ");