13 static void process(keyrecord_t *record);
17 uint8_t default_layer = 0;
18 uint8_t current_layer = 0;
22 /* number of tap which fires toggle feature */
25 /* This counts up when tap occurs */
26 uint8_t tap_count = 0;
27 keyevent_t tapping_event = {};
28 keyrecord_t tapping_key = {};
30 /* TAPPING: This indicates that whether tap or not is not decided yet. */
31 // NOTE: keyevent_t.time 0 means no event.
32 #define IS_TAPPING() (tapping_key.event.time != 0)
33 #define IS_TAPPING_PRESSED() (IS_TAPPING() && tapping_key.event.pressed)
34 #define IS_TAPPING_RELEASED() (IS_TAPPING() && !tapping_key.event.pressed)
35 #define IS_TAPPING_KEY(k) (IS_TAPPING() && KEYEQ(tapping_key.event.key, (k)))
36 #define WITHIN_TAP_TERM(e) (TIMER_DIFF_16(e.time, tapping_key.event.time) < TAP_TERM)
38 /* waiting keys buffer */
39 #define WAITING_BUFFER_SIZE 8
40 static keyrecord_t waiting_buffer[WAITING_BUFFER_SIZE] = {};
41 /* point to empty cell to enq */
42 static uint8_t waiting_buffer_head = 0;
43 /* point to the oldest data cell to deq */
44 static uint8_t waiting_buffer_tail = 0;
46 static bool waiting_buffer_enq(keyrecord_t record)
48 if (IS_NOEVENT(record.event)) {
52 if ((waiting_buffer_head + 1) % WAITING_BUFFER_SIZE == waiting_buffer_tail) {
53 debug("waiting_buffer_enq: Over flow.\n");
57 debug("waiting_buffer_enq["); debug_dec(waiting_buffer_head); debug("] = ");
58 debug_hex16(record.event.key.raw); debug("\n");
60 waiting_buffer[waiting_buffer_head] = record;
61 waiting_buffer_head = (waiting_buffer_head + 1) % WAITING_BUFFER_SIZE;
65 static keyrecord_t waiting_buffer_deq(void)
67 if (waiting_buffer_head == waiting_buffer_tail) {
68 return (keyrecord_t){}; // ???
70 uint8_t last_tail = waiting_buffer_tail;
71 waiting_buffer_tail = waiting_buffer_tail + 1 % WAITING_BUFFER_SIZE;
72 return waiting_buffer[last_tail];
74 static bool waiting_buffer_is_empty(void)
76 return (waiting_buffer_head == waiting_buffer_tail);
79 static void waiting_buffer_clear(void)
81 waiting_buffer_head = 0;
82 waiting_buffer_tail = 0;
84 static bool waiting_buffer_typed(keyevent_t event)
86 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
87 if (KEYEQ(event.key, waiting_buffer[i].event.key) && event.pressed != waiting_buffer[i].event.pressed) {
93 static bool waiting_buffer_has_anykey_pressed(void)
95 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
96 if (waiting_buffer[i].event.pressed) return true;
104 * Problem: Want to capitalize like 'The' but the result tends to be 'THe'.
105 * Solution: Oneshot modifier have its effect on only one key coming next.
106 * Tap Shift, then type 't', 'h' and 'e'. Not need to hold Shift key.
108 * Hold: works as normal modifier.
109 * Tap: one shot modifier.
110 * 2 Tap: cancel one shot modifier.
111 * 5-Tap: toggles enable/disable oneshot feature.
119 static void oneshot_start(uint8_t mods, uint16_t time)
121 oneshot_state.mods = mods;
122 oneshot_state.time = time;
123 oneshot_state.ready = true;
125 static void oneshot_cancel(void)
127 oneshot_state.mods = 0;
128 oneshot_state.time = 0;
129 oneshot_state.ready = false;
131 static void oneshot_toggle(void)
133 oneshot_state.disabled = !oneshot_state.disabled;
139 * Tap key is typed(pressed and released) within TAP_TERM
140 * without interfaring by typing other key.
142 /* return true when key event is processed. */
143 static bool process_tap(keyrecord_t *keyp)
145 keyevent_t event = keyp->event;
148 if (IS_TAPPING_PRESSED()) {
149 if (WITHIN_TAP_TERM(event)) {
150 if (tapping_key.tap_count == 0) {
151 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
153 debug("Tapping: First tap.\n");
154 tapping_key.tap_count = 1;
155 process(&tapping_key);
158 keyp->tap_count = tapping_key.tap_count;
160 } else if (!event.pressed && waiting_buffer_typed(event)) {
161 // other key typed. not tap.
162 debug("Tapping: End(No tap. Interfered by typing key).\n");
163 process(&tapping_key);
164 tapping_key = (keyrecord_t){};
169 // other key events shall be stored till tapping state settles.
173 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
174 keyp->tap_count = tapping_key.tap_count;
175 debug("Tapping: tap release("); debug_dec(keyp->tap_count); debug(")\n");
179 else if (is_tap_key(keyp->event.key) && event.pressed) {
180 debug("Tapping: Start with forcing to release last tap.\n");
181 process(&(keyrecord_t){
182 .tap_count = tapping_key.tap_count,
183 .event.key = tapping_key.event.key,
184 .event.time = event.time,
185 .event.pressed = false
191 if (!IS_NOEVENT(keyp->event)) debug("Tapping: key event while tap.\n");
197 // not within TAP_TERM
199 if (tapping_key.tap_count == 0) {
201 debug("Tapping: End. Not tap(time out).\n");
202 process(&tapping_key);
203 tapping_key = (keyrecord_t){};
206 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
207 debug("Tapping: End. tap release.");
208 keyp->tap_count = tapping_key.tap_count;
210 tapping_key = (keyrecord_t){};
213 // other key after tap time out.
219 } else if (IS_TAPPING_RELEASED()) {
220 if (WITHIN_TAP_TERM(event)) {
221 if (tapping_key.tap_count > 0 && IS_TAPPING_KEY(event.key) && event.pressed) {
223 keyp->tap_count = tapping_key.tap_count + 1;
224 debug("Tapping: tap press("); debug_dec(keyp->tap_count); debug(")\n");
228 } else if (event.pressed && is_tap_key(event.key)) {
229 // Sequential tap can be interfered with other tap key.
230 debug("Tapping: Start with interfering other tap.\n");
234 if (!IS_NOEVENT(keyp->event)) debug("Tapping: other key just after tap.\n");
239 // timeout. no sequential tap.
240 debug("Tapping: End(Time out after releasing last tap).\n");
241 tapping_key = (keyrecord_t){};
246 if (event.pressed && is_tap_key(event.key)) {
247 debug("Tapping: Start(Press tap key).\n");
257 void action_exec(keyevent_t event)
259 if (!IS_NOEVENT(event)) {
261 debug_hex16(event.time); debug(": ");
262 debug_hex16(event.key.raw);
264 if (event.pressed) debug("down"); else debug("up");
268 keyrecord_t record = { .event = event };
270 // pre-process on tapping
271 if (process_tap(&record)) {
272 if (!IS_NOEVENT(record.event)) debug("processed.\n");
274 if (!IS_NOEVENT(record.event)) debug("enqueued.\n");
276 if (!waiting_buffer_enq(record)) {
277 // clear all in case of overflow.
279 waiting_buffer_clear();
280 tapping_key = (keyrecord_t){};
284 // process waiting_buffer
285 for (; waiting_buffer_tail != waiting_buffer_head; waiting_buffer_tail = (waiting_buffer_tail + 1) % WAITING_BUFFER_SIZE) {
286 if (process_tap(&waiting_buffer[waiting_buffer_tail])) {
287 debug("processed: waiting_buffer["); debug_dec(waiting_buffer_tail); debug("] = ");
288 debug_hex16(waiting_buffer[waiting_buffer_tail].event.key.raw); debug("\n");
295 static void process(keyrecord_t *record)
297 keyevent_t event = record->event;
298 uint8_t tap_count = record->tap_count;
300 if (IS_NOEVENT(event)) { return; }
302 action_t action = keymap_get_action(current_layer, event.key.pos.row, event.key.pos.col);
303 debug("action: "); debug_hex16(action.code);
304 if (event.pressed) debug("[down]\n"); else debug("[up]\n");
306 switch (action.kind.id) {
311 uint8_t mods = (action.kind.id == ACT_LMODS) ? action.key.mods :
314 uint8_t tmp_mods = host_get_mods();
317 host_send_keyboard_report();
319 register_code(action.key.code);
320 if (mods && action.key.code) {
321 host_set_mods(tmp_mods);
322 host_send_keyboard_report();
325 if (mods && !action.key.code) {
327 host_send_keyboard_report();
329 unregister_code(action.key.code);
336 uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods :
338 switch (action.layer.code) {
342 if (tap_count == 0) {
343 debug("MODS_TAP: Oneshot: add_mods\n");
346 else if (tap_count == 1) {
347 debug("MODS_TAP: Oneshot: start\n");
348 oneshot_start(mods, event.time);
350 else if (tap_count == 5) {
351 debug("MODS_TAP: Oneshot: toggle\n");
355 debug("MODS_TAP: Oneshot: cancel&add_mods\n");
356 // double tap cancels oneshot and works as normal modifier.
361 if (tap_count == 0) {
362 debug("MODS_TAP: Oneshot: cancel/del_mods\n");
363 // cancel oneshot by holding.
367 else if (tap_count == 1) {
368 debug("MODS_TAP: Oneshot: del_mods\n");
373 debug("MODS_TAP: Oneshot: del_mods\n");
382 if (waiting_buffer_has_anykey_pressed()) {
383 debug("MODS_TAP: Tap: Cancel: add_mods\n");
384 // ad hoc: set 0 to cancel tap
385 record->tap_count = 0;
388 debug("MODS_TAP: Tap: register_code\n");
389 register_code(action.key.code);
392 debug("MODS_TAP: No tap: add_mods\n");
397 debug("MODS_TAP: Tap: unregister_code\n");
398 unregister_code(action.key.code);
400 debug("MODS_TAP: No tap: add_mods\n");
409 /* other HID usage */
411 #ifdef EXTRAKEY_ENABLE
412 switch (action.usage.page) {
413 case ACTION_USAGE_PAGE_SYSTEM:
415 host_system_send(action.usage.code);
420 case ACTION_USAGE_PAGE_CONSUMER:
422 host_consumer_send(action.usage.code);
424 host_consumer_send(0);
433 #ifdef MOUSEKEY_ENABLE
435 mousekey_on(action.key.code);
438 mousekey_off(action.key.code);
445 case ACT_LAYER_PRESSED:
446 // layer action when pressed
447 switch (action.layer.code) {
450 layer_switch(action.layer.opt);
456 if (tap_count < TAP_TOGGLE) {
457 layer_switch(action.layer.opt);
460 if (tap_count >= TAP_TOGGLE) {
461 debug("LAYER_PRESSED: tap toggle.\n");
462 layer_switch(action.layer.opt);
467 // change default layer
469 default_layer = action.layer.opt;
470 layer_switch(default_layer);
477 debug("LAYER_PRESSED: Tap: register_code\n");
478 register_code(action.layer.code);
480 debug("LAYER_PRESSED: No tap: layer_switch\n");
481 layer_switch(action.layer.opt);
485 debug("LAYER_PRESSED: Tap: unregister_code\n");
486 unregister_code(action.layer.code);
488 debug("LAYER_PRESSED: No tap: NO ACTION\n");
494 case ACT_LAYER_RELEASED:
495 switch (action.layer.code) {
497 if (!event.pressed) {
498 layer_switch(action.layer.opt);
504 if (tap_count >= TAP_TOGGLE) {
505 debug("LAYER_RELEASED: tap toggle.\n");
506 layer_switch(action.layer.opt);
509 if (tap_count < TAP_TOGGLE) {
510 layer_switch(action.layer.opt);
515 // change default layer
516 if (!event.pressed) {
517 default_layer = action.layer.opt;
518 layer_switch(default_layer);
525 debug("LAYER_RELEASED: Tap: register_code\n");
526 register_code(action.layer.code);
528 debug("LAYER_RELEASED: No tap: NO ACTION\n");
532 debug("LAYER_RELEASED: Tap: unregister_code\n");
533 unregister_code(action.layer.code);
535 debug("LAYER_RELEASED: No tap: layer_switch\n");
536 layer_switch(action.layer.opt);
543 switch (action.layer.code) {
546 layer_switch(current_layer | action.layer.opt);
548 layer_switch(current_layer & ~action.layer.opt);
554 if (tap_count < TAP_TOGGLE) {
555 debug("LAYER_BIT: tap toggle(press).\n");
556 layer_switch(current_layer | action.layer.opt);
559 if (tap_count < TAP_TOGGLE) {
560 debug("LAYER_BIT: tap toggle(release).\n");
561 layer_switch(current_layer & ~action.layer.opt);
563 debug("LAYER_BIT: tap toggle.\n");
564 layer_switch(current_layer | action.layer.opt);
569 // change default layer
571 default_layer = current_layer | action.layer.opt;
572 layer_switch(default_layer);
574 default_layer = current_layer & ~action.layer.opt;
575 layer_switch(default_layer);
581 if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
582 debug("LAYER_BIT: Tap: register_code\n");
583 register_code(action.layer.code);
585 debug("LAYER_BIT: No tap: layer_switch(bit on)\n");
586 layer_switch(current_layer | action.layer.opt);
589 if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
590 debug("LAYER_BIT: Tap: unregister_code\n");
591 unregister_code(action.layer.code);
593 debug("LAYER_BIT: No tap: layer_switch(bit off)\n");
594 layer_switch(current_layer & ~action.layer.opt);
601 switch (action.layer.opt) {
603 // set default layer when pressed
604 switch (action.layer.code) {
607 layer_switch(default_layer);
613 if (tap_count < TAP_TOGGLE) {
614 layer_switch(default_layer);
617 if (tap_count >= TAP_TOGGLE) {
618 debug("LAYER_EXT_PRESSED: tap toggle.\n");
619 layer_switch(default_layer);
624 // change default layer
626 default_layer = current_layer;
627 layer_switch(default_layer);
634 debug("LAYER_EXT_PRESSED: Tap: register_code\n");
635 register_code(action.layer.code);
637 debug("LAYER_EXT_PRESSED: No tap: layer_switch\n");
638 layer_switch(default_layer);
642 debug("LAYER_EXT_PRESSED: Tap: unregister_code\n");
643 unregister_code(action.layer.code);
645 debug("LAYER_EXT_PRESSED: No tap: NO ACTION\n");
652 // set default layer when released
653 switch (action.layer.code) {
655 if (!event.pressed) {
656 layer_switch(default_layer);
662 if (tap_count >= TAP_TOGGLE) {
663 debug("LAYER_EXT_RELEASED: tap toggle.\n");
664 layer_switch(default_layer);
667 if (tap_count < TAP_TOGGLE) {
668 layer_switch(default_layer);
673 // change default layer
674 if (!event.pressed) {
675 default_layer = current_layer;
676 layer_switch(default_layer);
683 debug("LAYER_EXT_RELEASED: Tap: register_code\n");
684 register_code(action.layer.code);
686 debug("LAYER_EXT_RELEASED: No tap: NO ACTION\n");
690 debug("LAYER_EXT_RELEASED: Tap: unregister_code\n");
691 unregister_code(action.layer.code);
693 debug("LAYER_EXT_RELEASED: No tap: layer_switch\n");
694 layer_switch(default_layer);
709 action_call_function(event, action.func.id);
718 * Utilities for actions.
720 void register_code(uint8_t code)
725 else if IS_KEY(code) {
726 // TODO: should push command_proc out of this block?
727 if (command_proc(code)) return;
729 if (oneshot_state.mods && oneshot_state.ready && !oneshot_state.disabled) {
730 uint8_t tmp_mods = host_get_mods();
731 host_add_mods(oneshot_state.mods);
733 host_send_keyboard_report();
735 host_set_mods(tmp_mods);
736 oneshot_state.ready = false;
739 host_send_keyboard_report();
742 else if IS_MOD(code) {
743 host_add_mods(MOD_BIT(code));
744 host_send_keyboard_report();
748 void unregister_code(uint8_t code)
752 host_send_keyboard_report();
754 else if IS_MOD(code) {
755 host_del_mods(MOD_BIT(code));
756 host_send_keyboard_report();
760 void add_mods(uint8_t mods)
764 host_send_keyboard_report();
768 void del_mods(uint8_t mods)
772 host_send_keyboard_report();
776 void set_mods(uint8_t mods)
779 host_send_keyboard_report();
782 void clear_keyboard(void)
785 clear_keyboard_but_mods();
788 void clear_keyboard_but_mods(void)
791 host_send_keyboard_report();
792 #ifdef MOUSEKEY_ENABLE
796 #ifdef EXTRAKEY_ENABLE
798 host_consumer_send(0);
802 bool sending_anykey(void)
804 return (host_has_anykey() || host_mouse_in_use() ||
805 host_last_sysytem_report() || host_last_consumer_report());
808 void layer_switch(uint8_t new_layer)
810 if (current_layer != new_layer) {
811 debug("Layer Switch: "); debug_hex(current_layer);
812 debug(" -> "); debug_hex(new_layer); debug("\n");
814 current_layer = new_layer;
815 clear_keyboard_but_mods(); // To avoid stuck keys
816 // TODO: update mods with full scan of matrix? if modifier changes between layers
820 bool is_tap_key(key_t key)
822 action_t action = keymap_get_action(current_layer, key.pos.row, key.pos.col);
823 switch (action.kind.id) {
827 case ACT_LAYER_PRESSED:
829 switch (action.layer.code) {
839 if (action.func.opt & 0x1) {