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 static void process_action(keyrecord_t
*record
);
30 static bool process_tapping(keyrecord_t
*record
);
31 static void waiting_buffer_scan_tap(void);
33 static void debug_event(keyevent_t event
);
34 static void debug_record(keyrecord_t record
);
35 static void debug_action(action_t action
);
36 static void debug_tapping_key(void);
37 static void debug_waiting_buffer(void);
43 /* period of tapping(ms) */
45 #define TAPPING_TERM 200
48 /* tap count needed for toggling a feature */
49 #ifndef TAPPING_TOGGLE
50 #define TAPPING_TOGGLE 5
53 /* stores a key event of current tap. */
54 static keyrecord_t tapping_key
= {};
56 #define IS_TAPPING() !IS_NOEVENT(tapping_key.event)
57 #define IS_TAPPING_PRESSED() (IS_TAPPING() && tapping_key.event.pressed)
58 #define IS_TAPPING_RELEASED() (IS_TAPPING() && !tapping_key.event.pressed)
59 #define IS_TAPPING_KEY(k) (IS_TAPPING() && KEYEQ(tapping_key.event.key, (k)))
60 #define WITHIN_TAPPING_TERM(e) (TIMER_DIFF_16(e.time, tapping_key.event.time) < TAPPING_TERM)
66 * stores key events waiting for settling current tap.
68 #define WAITING_BUFFER_SIZE 8
69 static keyrecord_t waiting_buffer
[WAITING_BUFFER_SIZE
] = {};
71 /* point to empty cell to enq */
72 static uint8_t waiting_buffer_head
= 0;
74 /* point to the oldest data cell to deq */
75 static uint8_t waiting_buffer_tail
= 0;
77 static bool waiting_buffer_enq(keyrecord_t record
)
79 if (IS_NOEVENT(record
.event
)) {
83 if ((waiting_buffer_head
+ 1) % WAITING_BUFFER_SIZE
== waiting_buffer_tail
) {
84 debug("waiting_buffer_enq: Over flow.\n");
88 waiting_buffer
[waiting_buffer_head
] = record
;
89 waiting_buffer_head
= (waiting_buffer_head
+ 1) % WAITING_BUFFER_SIZE
;
91 debug("waiting_buffer_enq: "); debug_waiting_buffer();
95 static void waiting_buffer_clear(void)
97 waiting_buffer_head
= 0;
98 waiting_buffer_tail
= 0;
101 #if TAPPING_TERM >= 500
102 static bool waiting_buffer_typed(keyevent_t event
)
104 for (uint8_t i
= waiting_buffer_tail
; i
!= waiting_buffer_head
; i
= (i
+ 1) % WAITING_BUFFER_SIZE
) {
105 if (KEYEQ(event
.key
, waiting_buffer
[i
].event
.key
) && event
.pressed
!= waiting_buffer
[i
].event
.pressed
) {
113 bool waiting_buffer_has_anykey_pressed(void)
115 for (uint8_t i
= waiting_buffer_tail
; i
!= waiting_buffer_head
; i
= (i
+ 1) % WAITING_BUFFER_SIZE
) {
116 if (waiting_buffer
[i
].event
.pressed
) return true;
124 * Problem: Want to capitalize like 'The' but the result tends to be 'THe'.
125 * Solution: Oneshot modifier have its effect on only one key coming next.
126 * Tap Shift, then type 't', 'h' and 'e'. Not need to hold Shift key.
128 * Hold: works as normal modifier.
129 * Tap: one shot modifier.
130 * 2 Tap: cancel one shot modifier.
131 * 5-Tap: toggles enable/disable oneshot feature.
140 static void oneshot_start(uint8_t mods
, uint16_t time
)
142 oneshot_state
.mods
= mods
;
143 oneshot_state
.time
= time
;
144 oneshot_state
.ready
= true;
147 static void oneshot_cancel(void)
149 oneshot_state
.mods
= 0;
150 oneshot_state
.time
= 0;
151 oneshot_state
.ready
= false;
154 static void oneshot_toggle(void)
156 oneshot_state
.disabled
= !oneshot_state
.disabled
;
161 void action_exec(keyevent_t event
)
163 if (!IS_NOEVENT(event
)) {
164 debug("\n---- action_exec: start -----\n");
165 debug("EVENT: "); debug_event(event
); debug("\n");
168 keyrecord_t record
= { .event
= event
};
170 // pre-process on tapping
171 if (process_tapping(&record
)) {
172 if (!IS_NOEVENT(record
.event
)) {
173 debug("processed: "); debug_record(record
); debug("\n");
177 if (!waiting_buffer_enq(record
)) {
178 // clear all in case of overflow.
179 debug("OVERFLOW: CLEAR ALL STATES\n");
181 waiting_buffer_clear();
182 tapping_key
= (keyrecord_t
){};
186 // process waiting_buffer
187 if (!IS_NOEVENT(event
) && waiting_buffer_head
!= waiting_buffer_tail
) {
188 debug("---- action_exec: process waiting_buffer -----\n");
191 for (; waiting_buffer_tail
!= waiting_buffer_head
; waiting_buffer_tail
= (waiting_buffer_tail
+ 1) % WAITING_BUFFER_SIZE
) {
192 if (process_tapping(&waiting_buffer
[waiting_buffer_tail
])) {
193 debug("processed: waiting_buffer["); debug_dec(waiting_buffer_tail
); debug("] = ");
194 debug_record(waiting_buffer
[waiting_buffer_tail
]); debug("\n\n");
199 if (!IS_NOEVENT(event
)) {
204 static void process_action(keyrecord_t
*record
)
206 keyevent_t event
= record
->event
;
207 uint8_t tap_count
= record
->tap_count
;
209 if (IS_NOEVENT(event
)) { return; }
211 action_t action
= keymap_get_action(current_layer
, event
.key
.pos
.row
, event
.key
.pos
.col
);
212 //debug("action: "); debug_hex16(action.code); if (event.pressed) debug("d\n"); else debug("u\n");
213 debug("ACTION: "); debug_action(action
); debug("\n");
215 switch (action
.kind
.id
) {
220 uint8_t mods
= (action
.kind
.id
== ACT_LMODS
) ? action
.key
.mods
:
223 uint8_t tmp_mods
= host_get_mods();
226 host_send_keyboard_report();
228 register_code(action
.key
.code
);
229 if (mods
&& action
.key
.code
) {
230 host_set_mods(tmp_mods
);
231 host_send_keyboard_report();
234 if (mods
&& !action
.key
.code
) {
236 host_send_keyboard_report();
238 unregister_code(action
.key
.code
);
245 uint8_t mods
= (action
.kind
.id
== ACT_LMODS_TAP
) ? action
.key
.mods
:
247 switch (action
.layer
.code
) {
251 if (tap_count
== 0) {
252 debug("MODS_TAP: Oneshot: add_mods\n");
255 else if (tap_count
== 1) {
256 debug("MODS_TAP: Oneshot: start\n");
257 oneshot_start(mods
, event
.time
);
259 else if (tap_count
== TAPPING_TOGGLE
) {
260 debug("MODS_TAP: Oneshot: toggle\n");
264 debug("MODS_TAP: Oneshot: cancel&add_mods\n");
265 // double tap cancels oneshot and works as normal modifier.
270 if (tap_count
== 0) {
271 debug("MODS_TAP: Oneshot: cancel/del_mods\n");
272 // cancel oneshot by holding.
276 else if (tap_count
== 1) {
277 debug("MODS_TAP: Oneshot: del_mods\n");
282 debug("MODS_TAP: Oneshot: del_mods\n");
291 if (waiting_buffer_has_anykey_pressed()) {
292 debug("MODS_TAP: Tap: Cancel: add_mods\n");
293 // ad hoc: set 0 to cancel tap
294 record
->tap_count
= 0;
297 debug("MODS_TAP: Tap: register_code\n");
298 register_code(action
.key
.code
);
301 debug("MODS_TAP: No tap: add_mods\n");
306 debug("MODS_TAP: Tap: unregister_code\n");
307 unregister_code(action
.key
.code
);
309 debug("MODS_TAP: No tap: add_mods\n");
318 /* other HID usage */
320 #ifdef EXTRAKEY_ENABLE
321 switch (action
.usage
.page
) {
324 host_system_send(action
.usage
.code
);
331 host_consumer_send(action
.usage
.code
);
333 host_consumer_send(0);
342 #ifdef MOUSEKEY_ENABLE
344 mousekey_on(action
.key
.code
);
347 mousekey_off(action
.key
.code
);
355 switch (action
.layer
.code
) {
356 case LAYER_MOMENTARY
: /* momentary */
358 layer_switch(action
.layer
.val
);
361 layer_switch(default_layer
);
366 layer_switch(action
.layer
.val
);
369 case LAYER_ON_RELEASE
:
370 if (!event
.pressed
) {
371 layer_switch(action
.layer
.val
);
374 case LAYER_DEFAULT
: /* default layer */
375 switch (action
.layer
.val
) {
376 case DEFAULT_ON_BOTH
:
377 layer_switch(default_layer
);
379 case DEFAULT_ON_PRESS
:
381 layer_switch(default_layer
);
384 case DEFAULT_ON_RELEASE
:
385 if (!event
.pressed
) {
386 layer_switch(default_layer
);
391 case LAYER_TAP_TOGGLE
: /* switch on hold and toggle on several taps */
393 if (tap_count
< TAPPING_TOGGLE
) {
394 layer_switch(action
.layer
.val
);
397 if (tap_count
>= TAPPING_TOGGLE
) {
398 debug("LAYER_PRESSED: tap toggle.\n");
399 layer_switch(action
.layer
.val
);
403 case LAYER_CHANGE_DEFAULT
: /* change default layer */
405 default_layer
= action
.layer
.val
;
406 layer_switch(default_layer
);
409 default: /* switch layer on hold and key on tap*/
412 debug("LAYER_PRESSED: Tap: register_code\n");
413 register_code(action
.layer
.code
);
415 debug("LAYER_PRESSED: No tap: layer_switch\n");
416 layer_switch(action
.layer
.val
);
420 debug("LAYER_PRESSED: Tap: unregister_code\n");
421 unregister_code(action
.layer
.code
);
423 //debug("LAYER_PRESSED: No tap: NO ACTION\n");
425 debug("LAYER_PRESSED: No tap: return to default layer\n");
426 layer_switch(default_layer
);
433 switch (action
.layer
.code
) {
434 case LAYER_MOMENTARY
: /* momentary */
436 layer_switch(current_layer
^ action
.layer
.val
);
438 layer_switch(current_layer
^ action
.layer
.val
);
443 layer_switch(current_layer
^ action
.layer
.val
);
446 case LAYER_ON_RELEASE
:
447 if (!event
.pressed
) {
448 layer_switch(current_layer
^ action
.layer
.val
);
451 case LAYER_TAP_TOGGLE
: /* switch on hold and toggle on several taps */
453 if (tap_count
< TAPPING_TOGGLE
) {
454 debug("LAYER_BIT: tap toggle(press).\n");
455 layer_switch(current_layer
^ action
.layer
.val
);
458 if (tap_count
<= TAPPING_TOGGLE
) {
459 debug("LAYER_BIT: tap toggle(release).\n");
460 layer_switch(current_layer
^ action
.layer
.val
);
465 // change default layer
467 default_layer
= current_layer
^ action
.layer
.val
;
468 layer_switch(default_layer
);
470 default_layer
= current_layer
^ action
.layer
.val
;
471 layer_switch(default_layer
);
477 if (IS_TAPPING_KEY(event
.key
) && tap_count
> 0) {
478 debug("LAYER_BIT: Tap: register_code\n");
479 register_code(action
.layer
.code
);
481 debug("LAYER_BIT: No tap: layer_switch(bit on)\n");
482 layer_switch(current_layer
^ action
.layer
.val
);
485 if (IS_TAPPING_KEY(event
.key
) && tap_count
> 0) {
486 debug("LAYER_BIT: Tap: unregister_code\n");
487 unregister_code(action
.layer
.code
);
489 debug("LAYER_BIT: No tap: layer_switch(bit off)\n");
490 layer_switch(current_layer
^ action
.layer
.val
);
504 keymap_call_function(record
, action
.func
.id
, action
.func
.opt
);
513 * Rule: Tap key is typed(pressed and released) within TAPPING_TERM.
514 * (without interfering by typing other key)
516 /* return true when key event is processed or consumed. */
517 static bool process_tapping(keyrecord_t
*keyp
)
519 keyevent_t event
= keyp
->event
;
522 if (IS_TAPPING_PRESSED()) {
523 if (WITHIN_TAPPING_TERM(event
)) {
524 if (tapping_key
.tap_count
== 0) {
525 if (IS_TAPPING_KEY(event
.key
) && !event
.pressed
) {
527 debug("Tapping: First tap(0->1).\n");
528 tapping_key
.tap_count
= 1;
530 process_action(&tapping_key
);
533 keyp
->tap_count
= tapping_key
.tap_count
;
536 #if TAPPING_TERM >= 500
537 /* This can prevent from typing some tap keys in a row at a time. */
538 else if (!event
.pressed
&& waiting_buffer_typed(event
)) {
539 // other key typed. not tap.
540 debug("Tapping: End. No tap. Interfered by typing key\n");
541 process_action(&tapping_key
);
542 tapping_key
= (keyrecord_t
){};
550 // other key events shall be enq'd till tapping state settles.
556 if (IS_TAPPING_KEY(event
.key
) && !event
.pressed
) {
557 debug("Tapping: Tap release("); debug_dec(tapping_key
.tap_count
); debug(")\n");
558 keyp
->tap_count
= tapping_key
.tap_count
;
559 process_action(keyp
);
564 else if (is_tap_key(keyp
->event
.key
) && event
.pressed
) {
565 if (tapping_key
.tap_count
> 1) {
566 debug("Tapping: Start new tap with releasing last tap(>1).\n");
568 process_action(&(keyrecord_t
){
569 .tap_count
= tapping_key
.tap_count
,
570 .event
.key
= tapping_key
.event
.key
,
571 .event
.time
= event
.time
,
572 .event
.pressed
= false
575 debug("Tapping: Start while last tap(1).\n");
578 waiting_buffer_scan_tap();
583 if (!IS_NOEVENT(keyp
->event
)) {
584 debug("Tapping: key event while last tap(>0).\n");
586 process_action(keyp
);
591 // after TAPPING_TERM
593 if (tapping_key
.tap_count
== 0) {
594 debug("Tapping: End. Timeout. Not tap(0): ");
595 debug_event(event
); debug("\n");
596 process_action(&tapping_key
);
597 tapping_key
= (keyrecord_t
){};
601 if (IS_TAPPING_KEY(event
.key
) && !event
.pressed
) {
602 debug("Tapping: End. last timeout tap release(>0).");
603 keyp
->tap_count
= tapping_key
.tap_count
;
604 process_action(keyp
);
605 tapping_key
= (keyrecord_t
){};
608 else if (is_tap_key(keyp
->event
.key
) && event
.pressed
) {
609 if (tapping_key
.tap_count
> 1) {
610 debug("Tapping: Start new tap with releasing last timeout tap(>1).\n");
612 process_action(&(keyrecord_t
){
613 .tap_count
= tapping_key
.tap_count
,
614 .event
.key
= tapping_key
.event
.key
,
615 .event
.time
= event
.time
,
616 .event
.pressed
= false
619 debug("Tapping: Start while last timeout tap(1).\n");
622 waiting_buffer_scan_tap();
627 if (!IS_NOEVENT(keyp
->event
)) {
628 debug("Tapping: key event while last timeout tap(>0).\n");
630 process_action(keyp
);
635 } else if (IS_TAPPING_RELEASED()) {
636 if (WITHIN_TAPPING_TERM(event
)) {
637 if (tapping_key
.tap_count
> 0 && IS_TAPPING_KEY(event
.key
) && event
.pressed
) {
639 keyp
->tap_count
= tapping_key
.tap_count
+ 1;
640 debug("Tapping: Tap press("); debug_dec(keyp
->tap_count
); debug(")\n");
641 process_action(keyp
);
645 } else if (event
.pressed
&& is_tap_key(event
.key
)) {
646 // Sequential tap can be interfered with other tap key.
647 debug("Tapping: Start with interfering other tap.\n");
649 waiting_buffer_scan_tap();
653 if (!IS_NOEVENT(keyp
->event
)) debug("Tapping: other key just after tap.\n");
654 process_action(keyp
);
658 // timeout. no sequential tap.
659 debug("Tapping: End(Timeout after releasing last tap): ");
660 debug_event(event
); debug("\n");
661 tapping_key
= (keyrecord_t
){};
666 // not tapping satate
668 if (event
.pressed
&& is_tap_key(event
.key
)) {
669 debug("Tapping: Start(Press tap key).\n");
671 waiting_buffer_scan_tap();
675 process_action(keyp
);
681 /* scan buffer for tapping */
682 static void waiting_buffer_scan_tap(void)
684 // tapping already is settled
685 if (tapping_key
.tap_count
> 0) return;
686 // invalid state: tapping_key released && tap_count == 0
687 if (!tapping_key
.event
.pressed
) return;
689 for (uint8_t i
= waiting_buffer_tail
; i
!= waiting_buffer_head
; i
= (i
+ 1) % WAITING_BUFFER_SIZE
) {
690 if (IS_TAPPING_KEY(waiting_buffer
[i
].event
.key
) &&
691 !waiting_buffer
[i
].event
.pressed
&&
692 WITHIN_TAPPING_TERM(waiting_buffer
[i
].event
)) {
693 tapping_key
.tap_count
= 1;
694 waiting_buffer
[i
].tap_count
= 1;
695 process_action(&tapping_key
);
697 debug("waiting_buffer_scan_tap: found at ["); debug_dec(i
); debug("]\n");
698 debug_waiting_buffer();
707 * Utilities for actions.
709 void register_code(uint8_t code
)
714 else if IS_KEY(code
) {
715 // TODO: should push command_proc out of this block?
716 if (command_proc(code
)) return;
718 if (oneshot_state
.mods
&& oneshot_state
.ready
&& !oneshot_state
.disabled
) {
719 uint8_t tmp_mods
= host_get_mods();
720 host_add_mods(oneshot_state
.mods
);
722 host_send_keyboard_report();
724 host_set_mods(tmp_mods
);
725 oneshot_state
.ready
= false;
728 host_send_keyboard_report();
731 else if IS_MOD(code
) {
732 host_add_mods(MOD_BIT(code
));
733 host_send_keyboard_report();
737 void unregister_code(uint8_t code
)
741 host_send_keyboard_report();
743 else if IS_MOD(code
) {
744 host_del_mods(MOD_BIT(code
));
745 host_send_keyboard_report();
749 void add_mods(uint8_t mods
)
753 host_send_keyboard_report();
757 void del_mods(uint8_t mods
)
761 host_send_keyboard_report();
765 void set_mods(uint8_t mods
)
768 host_send_keyboard_report();
771 void clear_keyboard(void)
774 clear_keyboard_but_mods();
777 void clear_keyboard_but_mods(void)
780 host_send_keyboard_report();
781 #ifdef MOUSEKEY_ENABLE
785 #ifdef EXTRAKEY_ENABLE
787 host_consumer_send(0);
791 bool sending_anykey(void)
793 return (host_has_anykey() || host_mouse_in_use() ||
794 host_last_sysytem_report() || host_last_consumer_report());
797 void layer_switch(uint8_t new_layer
)
799 if (current_layer
!= new_layer
) {
800 debug("Layer Switch: "); debug_hex(current_layer
);
801 debug(" -> "); debug_hex(new_layer
); debug("\n");
803 current_layer
= new_layer
;
804 clear_keyboard_but_mods(); // To avoid stuck keys
805 // NOTE: update mods with full scan of matrix? if modifier changes between layers
809 bool is_tap_key(key_t key
)
811 action_t action
= keymap_get_action(current_layer
, key
.pos
.row
, key
.pos
.col
);
812 switch (action
.kind
.id
) {
818 switch (action
.layer
.code
) {
819 case LAYER_MOMENTARY
:
821 case LAYER_ON_RELEASE
:
824 case LAYER_TAP_TOGGLE
:
825 default: /* tap key */
830 if (action
.func
.opt
& FUNC_TAP
) { return true; }
840 static void debug_event(keyevent_t event
)
842 debug_hex16(event
.key
.raw
);
843 if (event
.pressed
) debug("d("); else debug("u(");
844 debug_dec(event
.time
); debug(")");
846 static void debug_record(keyrecord_t record
)
848 debug_event(record
.event
); debug(":"); debug_dec(record
.tap_count
);
850 static void debug_action(action_t action
)
852 switch (action
.kind
.id
) {
853 case ACT_LMODS
: debug("ACT_LMODS"); break;
854 case ACT_RMODS
: debug("ACT_RMODS"); break;
855 case ACT_LMODS_TAP
: debug("ACT_LMODS_TAP"); break;
856 case ACT_RMODS_TAP
: debug("ACT_RMODS_TAP"); break;
857 case ACT_USAGE
: debug("ACT_USAGE"); break;
858 case ACT_MOUSEKEY
: debug("ACT_MOUSEKEY"); break;
859 case ACT_LAYER
: debug("ACT_LAYER"); break;
860 case ACT_LAYER_BIT
: debug("ACT_LAYER_BIT"); break;
861 case ACT_MACRO
: debug("ACT_MACRO"); break;
862 case ACT_COMMAND
: debug("ACT_COMMAND"); break;
863 case ACT_FUNCTION
: debug("ACT_FUNCTION"); break;
864 default: debug("UNKNOWN"); break;
867 debug_hex4(action
.kind
.param
>>8);
869 debug_hex8(action
.kind
.param
& 0xff);
872 static void debug_tapping_key(void)
874 debug("TAPPING_KEY="); debug_record(tapping_key
); debug("\n");
876 static void debug_waiting_buffer(void)
879 for (uint8_t i
= waiting_buffer_tail
; i
!= waiting_buffer_head
; i
= (i
+ 1) % WAITING_BUFFER_SIZE
) {
880 debug("["); debug_dec(i
); debug("]="); debug_record(waiting_buffer
[i
]); debug(" ");