13 static void process(keyrecord_t
*record
);
17 uint8_t default_layer
= 0;
18 uint8_t current_layer
= 0;
23 /* This counts up when tap occurs */
24 uint8_t tap_count
= 0;
25 keyevent_t tapping_event
= {};
26 keyrecord_t tapping_key
= {};
28 /* TAPPING: This indicates that whether tap or not is not decided yet. */
29 // NOTE: keyevent_t.time 0 means no event.
30 #define IS_TAPPING() (tapping_key.event.time != 0)
31 #define IS_TAPPING_PRESSED() (IS_TAPPING() && tapping_key.event.pressed)
32 #define IS_TAPPING_RELEASED() (IS_TAPPING() && !tapping_key.event.pressed)
33 #define IS_TAPPING_KEY(k) (IS_TAPPING() && KEYEQ(tapping_key.event.key, (k)))
34 #define WITHIN_TAP_TERM(e) (TIMER_DIFF_16(e.time, tapping_key.event.time) < TAP_TERM)
36 /* waiting keys buffer */
37 #define WAITING_BUFFER_SIZE 8
38 static keyrecord_t waiting_buffer
[WAITING_BUFFER_SIZE
] = {};
39 /* point to empty cell to enq */
40 static uint8_t waiting_buffer_head
= 0;
41 /* point to the oldest data cell to deq */
42 static uint8_t waiting_buffer_tail
= 0;
44 static bool waiting_buffer_enq(keyrecord_t record
)
46 if (IS_NOEVENT(record
.event
)) {
50 if ((waiting_buffer_head
+ 1) % WAITING_BUFFER_SIZE
== waiting_buffer_tail
) {
51 debug("waiting_buffer_enq: Over flow.\n");
55 debug("waiting_buffer_enq["); debug_dec(waiting_buffer_head
); debug("] = ");
56 debug_hex16(record
.event
.key
.raw
); debug("\n");
58 waiting_buffer
[waiting_buffer_head
] = record
;
59 waiting_buffer_head
= (waiting_buffer_head
+ 1) % WAITING_BUFFER_SIZE
;
62 static keyrecord_t
waiting_buffer_deq(void)
64 if (waiting_buffer_head
== waiting_buffer_tail
) {
65 return (keyrecord_t
){};
67 uint8_t last_tail
= waiting_buffer_tail
;
68 waiting_buffer_tail
= waiting_buffer_tail
+ 1 % WAITING_BUFFER_SIZE
;
69 return waiting_buffer
[last_tail
];
71 static bool waiting_buffer_is_empty(void)
73 return (waiting_buffer_head
== waiting_buffer_tail
);
75 static void waiting_buffer_clear(void)
77 waiting_buffer_head
= 0;
78 waiting_buffer_tail
= 0;
80 static bool waiting_buffer_typed(keyevent_t event
)
82 for (uint8_t i
= waiting_buffer_tail
; i
!= waiting_buffer_head
; i
= (i
+ 1) % WAITING_BUFFER_SIZE
) {
83 if (KEYEQ(event
.key
, waiting_buffer
[i
].event
.key
) && event
.pressed
!= waiting_buffer
[i
].event
.pressed
) {
89 static bool waiting_buffer_has_anykey_pressed(void)
91 for (uint8_t i
= waiting_buffer_tail
; i
!= waiting_buffer_head
; i
= (i
+ 1) % WAITING_BUFFER_SIZE
) {
92 if (waiting_buffer
[i
].event
.pressed
) return true;
96 static void waiting_buffer_process(void)
102 * Problem: Want to capitalize like 'The' but the result tends to be 'THe'.
103 * Solution: Oneshot modifier have its effect on only one key coming next.
104 * Tap Shift, then type 't', 'h' and 'e'. Not need to hold Shift key.
106 * Hold: works as normal modifier.
107 * Tap: one shot modifier.
108 * 2 Tap: cancel one shot modifier.
109 * 5-Tap: toggles enable/disable oneshot feature.
117 static void oneshot_start(uint8_t mods
, uint16_t time
)
119 oneshot_state
.mods
= mods
;
120 oneshot_state
.time
= time
;
121 oneshot_state
.ready
= true;
123 static void oneshot_cancel(void)
125 oneshot_state
.mods
= 0;
126 oneshot_state
.time
= 0;
127 oneshot_state
.ready
= false;
129 static void oneshot_toggle(void)
131 oneshot_state
.disabled
= !oneshot_state
.disabled
;
137 * Tap key is typed(pressed and released) within TAP_TERM
138 * without interfaring by typing other key.
140 /* return true when key event is processed. */
141 static bool process_tap(keyrecord_t
*keyp
)
143 keyevent_t event
= keyp
->event
;
146 if (IS_TAPPING_PRESSED()) {
147 if (WITHIN_TAP_TERM(event
)) {
148 if (tapping_key
.tap_count
== 0) {
149 if (IS_TAPPING_KEY(event
.key
) && !event
.pressed
) {
151 debug("Tapping: First tap.\n");
152 tapping_key
.tap_count
= 1;
153 process(&tapping_key
);
156 keyp
->tap_count
= tapping_key
.tap_count
;
158 } else if (!event
.pressed
&& waiting_buffer_typed(event
)) {
159 // other key typed. not tap.
160 debug("Tapping: End(No tap. Interfered by typing key).\n");
161 process(&tapping_key
);
162 tapping_key
= (keyrecord_t
){};
167 // other key events shall be stored till tapping state settles.
171 if (IS_TAPPING_KEY(event
.key
) && !event
.pressed
) {
172 keyp
->tap_count
= tapping_key
.tap_count
;
173 debug("Tapping: tap release("); debug_dec(keyp
->tap_count
); debug(")\n");
177 else if (is_tap_key(keyp
->event
.key
) && event
.pressed
) {
178 debug("Tapping: Start with forcing to release last tap.\n");
179 process(&(keyrecord_t
){
180 .tap_count
= tapping_key
.tap_count
,
181 .event
.key
= tapping_key
.event
.key
,
182 .event
.time
= event
.time
,
183 .event
.pressed
= false
189 if (!IS_NOEVENT(keyp
->event
)) debug("Tapping: key event while tap.\n");
195 // not within TAP_TERM
197 if (tapping_key
.tap_count
== 0) {
199 debug("Tapping: End. Not tap(time out).\n");
200 process(&tapping_key
);
201 tapping_key
= (keyrecord_t
){};
204 if (IS_TAPPING_KEY(event
.key
) && !event
.pressed
) {
205 debug("Tapping: End. tap release.");
206 keyp
->tap_count
= tapping_key
.tap_count
;
208 tapping_key
= (keyrecord_t
){};
211 // other key after tap time out.
217 } else if (IS_TAPPING_RELEASED()) {
218 if (WITHIN_TAP_TERM(event
)) {
219 if (tapping_key
.tap_count
> 0 && IS_TAPPING_KEY(event
.key
) && event
.pressed
) {
221 keyp
->tap_count
= tapping_key
.tap_count
+ 1;
222 debug("Tapping: tap press("); debug_dec(keyp
->tap_count
); debug(")\n");
226 } else if (event
.pressed
&& is_tap_key(event
.key
)) {
227 // Sequential tap can be interfered with other tap key.
228 debug("Tapping: Start with interfering other tap.\n");
232 if (!IS_NOEVENT(keyp
->event
)) debug("Tapping: other key just after tap.\n");
237 // timeout. no sequential tap.
238 debug("Tapping: End(Time out after releasing last tap).\n");
239 tapping_key
= (keyrecord_t
){};
244 if (event
.pressed
&& is_tap_key(event
.key
)) {
245 debug("Tapping: Start(Press tap key).\n");
255 void action_exec(keyevent_t event
)
257 if (!IS_NOEVENT(event
)) {
259 debug_hex16(event
.time
); debug(": ");
260 debug_hex16(event
.key
.raw
);
262 if (event
.pressed
) debug("down"); else debug("up");
266 keyrecord_t record
= { .event
= event
};
268 // pre-process on tapping
269 if (process_tap(&record
)) {
270 if (!IS_NOEVENT(record
.event
)) debug("processed.\n");
272 if (!IS_NOEVENT(record
.event
)) debug("enqueued.\n");
273 if (!waiting_buffer_enq(record
)) {
274 // clear all in case of overflow.
276 waiting_buffer_clear();
277 tapping_key
= (keyrecord_t
){};
281 // TODO: need to process every time?
282 // process waiting_buffer
283 for (; waiting_buffer_tail
!= waiting_buffer_head
; waiting_buffer_tail
= (waiting_buffer_tail
+ 1) % WAITING_BUFFER_SIZE
) {
284 if (process_tap(&waiting_buffer
[waiting_buffer_tail
])) {
285 debug("processed: waiting_buffer["); debug_dec(waiting_buffer_tail
); debug("] = ");
286 debug_hex16(waiting_buffer
[waiting_buffer_tail
].event
.key
.raw
); debug("\n");
293 static void process(keyrecord_t
*record
)
296 keyevent_t event
= record
->event
;
297 uint8_t tap_count
= record
->tap_count
;
299 if (IS_NOEVENT(event
)) { return; }
301 action_t action
= keymap_get_action(current_layer
, event
.key
.pos
.row
, event
.key
.pos
.col
);
302 debug("action: "); debug_hex16(action
.code
);
303 if (event
.pressed
) debug("[down]\n"); else debug("[up]\n");
305 switch (action
.kind
.id
) {
310 uint8_t mods
= (action
.kind
.id
== ACT_LMODS
) ? action
.key
.mods
:
313 uint8_t tmp_mods
= host_get_mods();
316 host_send_keyboard_report();
318 register_code(action
.key
.code
);
319 if (mods
&& action
.key
.code
) {
320 host_set_mods(tmp_mods
);
321 host_send_keyboard_report();
324 if (mods
&& !action
.key
.code
) {
326 host_send_keyboard_report();
328 unregister_code(action
.key
.code
);
335 uint8_t mods
= (action
.kind
.id
== ACT_LMODS_TAP
) ? action
.key
.mods
:
337 switch (action
.layer
.code
) {
341 if (tap_count
== 0) {
342 debug("MODS_TAP: Oneshot: add_mods\n");
345 else if (tap_count
== 1) {
346 debug("MODS_TAP: Oneshot: start\n");
347 oneshot_start(mods
, event
.time
);
349 else if (tap_count
== 5) {
350 debug("MODS_TAP: Oneshot: toggle\n");
354 debug("MODS_TAP: Oneshot: cancel&add_mods\n");
355 // double tap cancels oneshot and works as normal modifier.
360 if (tap_count
== 0) {
361 debug("MODS_TAP: Oneshot: cancel/del_mods\n");
362 // cancel oneshot by holding.
366 else if (tap_count
== 1) {
367 debug("MODS_TAP: Oneshot: del_mods\n");
372 debug("MODS_TAP: Oneshot: del_mods\n");
381 if (waiting_buffer_has_anykey_pressed()) {
382 debug("MODS_TAP: Tap: Cancel: add_mods\n");
383 // ad hoc: set 0 to cancel tap
384 record
->tap_count
= 0;
387 debug("MODS_TAP: Tap: register_code\n");
388 register_code(action
.key
.code
);
391 debug("MODS_TAP: No tap: add_mods\n");
396 debug("MODS_TAP: Tap: unregister_code\n");
397 unregister_code(action
.key
.code
);
399 debug("MODS_TAP: No tap: add_mods\n");
408 /* other HID usage */
410 #ifdef EXTRAKEY_ENABLE
411 switch (action
.usage
.page
) {
412 case ACTION_USAGE_PAGE_SYSTEM
:
414 host_system_send(action
.usage
.code
);
419 case ACTION_USAGE_PAGE_CONSUMER
:
421 host_consumer_send(action
.usage
.code
);
423 host_consumer_send(0);
432 #ifdef MOUSEKEY_ENABLE
434 mousekey_on(action
.key
.code
);
437 mousekey_off(action
.key
.code
);
444 case ACT_LAYER_PRESSED
:
445 // layer action when pressed
446 switch (action
.layer
.code
) {
449 layer_switch(action
.layer
.opt
);
457 default_layer
= action
.layer
.opt
;
458 layer_switch(default_layer
);
464 if (IS_TAPPING_KEY(event
.key
)) {
466 debug("LAYER_PRESSED: Tap: register_code\n");
467 register_code(action
.layer
.code
);
469 debug("LAYER_PRESSED: No tap: layer_switch\n");
470 layer_switch(action
.layer
.opt
);
473 // TODO: while other key tapping
474 debug("LAYER_PRESSED: No tap: layer_switch\n");
475 layer_switch(action
.layer
.opt
);
478 if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
479 debug("LAYER_PRESSED: Tap: register_code\n");
480 register_code(action.layer.code);
482 debug("LAYER_PRESSED: No tap: layer_switch\n");
483 layer_switch(action.layer.opt);
487 if (IS_TAPPING_KEY(event
.key
) && tap_count
> 0) {
488 debug("LAYER_PRESSED: Tap: unregister_code\n");
489 unregister_code(action
.layer
.code
);
491 debug("LAYER_PRESSED: No tap: NO ACTION\n");
497 case ACT_LAYER_RELEASED
:
498 switch (action
.layer
.code
) {
500 if (!event
.pressed
) {
501 layer_switch(action
.layer
.opt
);
505 // Ignored. LAYER_RELEASED with tap toggle is invalid action.
508 if (!event
.pressed
) {
509 default_layer
= action
.layer
.opt
;
510 layer_switch(default_layer
);
514 // Ignored. LAYER_RELEASED with tap key is invalid action.
519 switch (action
.layer
.code
) {
522 layer_switch(current_layer
| action
.layer
.opt
);
524 layer_switch(current_layer
& ~action
.layer
.opt
);
531 // change default layer
533 default_layer
= current_layer
| action
.layer
.opt
;
534 layer_switch(default_layer
);
536 default_layer
= current_layer
& ~action
.layer
.opt
;
537 layer_switch(default_layer
);
543 if (IS_TAPPING_KEY(event
.key
) && tap_count
> 0) {
544 debug("LAYER_BIT: Tap: register_code\n");
545 register_code(action
.layer
.code
);
547 debug("LAYER_BIT: No tap: layer_switch(bit on)\n");
548 layer_switch(current_layer
| action
.layer
.opt
);
551 if (IS_TAPPING_KEY(event
.key
) && tap_count
> 0) {
552 debug("LAYER_BIT: Tap: unregister_code\n");
553 unregister_code(action
.layer
.code
);
555 debug("LAYER_BIT: No tap: layer_switch(bit off)\n");
556 layer_switch(current_layer
& ~action
.layer
.opt
);
562 switch (action
.layer
.opt
) {
564 // set default layer when pressed
565 switch (action
.layer
.code
) {
568 layer_switch(default_layer
);
576 default_layer
= current_layer
;
577 layer_switch(default_layer
);
586 // set default layer when released
587 switch (action
.layer
.code
) {
589 if (!event
.pressed
) {
590 layer_switch(default_layer
);
594 if (!event
.pressed
) {
595 default_layer
= current_layer
;
596 layer_switch(default_layer
);
602 if (!event
.pressed
) {
603 layer_switch(default_layer
);
617 action_call_function(event
, action
.func
.id
);
626 * Utilities for actions.
628 void register_code(uint8_t code
)
633 else if IS_KEY(code
) {
634 // TODO: should push command_proc out of this block?
635 if (command_proc(code
)) return;
637 if (oneshot_state
.mods
&& oneshot_state
.ready
&& !oneshot_state
.disabled
) {
638 uint8_t tmp_mods
= host_get_mods();
639 host_add_mods(oneshot_state
.mods
);
641 host_send_keyboard_report();
643 host_set_mods(tmp_mods
);
644 oneshot_state
.ready
= false;
647 host_send_keyboard_report();
650 else if IS_MOD(code
) {
651 host_add_mods(MOD_BIT(code
));
652 host_send_keyboard_report();
656 void unregister_code(uint8_t code
)
660 host_send_keyboard_report();
662 else if IS_MOD(code
) {
663 host_del_mods(MOD_BIT(code
));
664 host_send_keyboard_report();
668 void add_mods(uint8_t mods
)
672 host_send_keyboard_report();
676 void del_mods(uint8_t mods
)
680 host_send_keyboard_report();
684 void set_mods(uint8_t mods
)
687 host_send_keyboard_report();
690 void clear_keyboard(void)
693 clear_keyboard_but_mods();
696 void clear_keyboard_but_mods(void)
699 host_send_keyboard_report();
700 #ifdef MOUSEKEY_ENABLE
704 #ifdef EXTRAKEY_ENABLE
706 host_consumer_send(0);
710 bool sending_anykey(void)
712 return (host_has_anykey() || host_mouse_in_use() ||
713 host_last_sysytem_report() || host_last_consumer_report());
716 void layer_switch(uint8_t new_layer
)
718 if (current_layer
!= new_layer
) {
719 debug("Layer Switch: "); debug_hex(current_layer
);
720 debug(" -> "); debug_hex(new_layer
); debug("\n");
722 current_layer
= new_layer
;
723 clear_keyboard_but_mods(); // To avoid stuck keys
724 // TODO: update mods with full scan of matrix? if modifier changes between layers
728 bool is_tap_key(key_t key
)
730 action_t action
= keymap_get_action(current_layer
, key
.pos
.row
, key
.pos
.col
);
731 switch (action
.kind
.id
) {
735 case ACT_LAYER_PRESSED
:
737 switch (action
.layer
.code
) {
747 if (action
.func
.opt
& 0x1) {