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)
103 * Tap key is typed(pressed and released) within TAP_TERM
104 * without interfaring by typing other key.
106 /* return true when key event is processed. */
107 static bool process_tap(keyrecord_t *keyp)
109 keyevent_t event = keyp->event;
112 if (IS_TAPPING_PRESSED()) {
113 if (WITHIN_TAP_TERM(event)) {
114 if (tapping_key.tap_count == 0) {
115 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
117 debug("Tapping: First tap.\n");
118 tapping_key.tap_count = 1;
119 process(&tapping_key);
122 keyp->tap_count = tapping_key.tap_count;
124 } else if (!event.pressed && waiting_buffer_typed(event)) {
125 // other key typed. not tap.
126 debug("Tapping: End(No tap. Interfered by typing key).\n");
127 process(&tapping_key);
128 tapping_key = (keyrecord_t){};
133 // other key events shall be stored till tapping state settles.
137 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
138 keyp->tap_count = tapping_key.tap_count;
139 debug("Tapping: tap release("); debug_dec(keyp->tap_count); debug(")\n");
143 else if (is_tap_key(keyp->event.key) && event.pressed) {
144 debug("Tapping: Start with forcing to release last tap.\n");
145 process(&(keyrecord_t){
146 .tap_count = tapping_key.tap_count,
147 .event.key = tapping_key.event.key,
148 .event.time = event.time,
149 .event.pressed = false
155 if (!IS_NOEVENT(keyp->event)) debug("Tapping: key event while tap.\n");
161 // not within TAP_TERM
163 if (tapping_key.tap_count == 0) {
165 debug("Tapping: End. Not tap(time out).\n");
166 process(&tapping_key);
167 tapping_key = (keyrecord_t){};
170 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
171 debug("Tapping: End. tap release.");
172 keyp->tap_count = tapping_key.tap_count;
174 tapping_key = (keyrecord_t){};
177 // other key after tap time out.
183 } else if (IS_TAPPING_RELEASED()) {
184 if (WITHIN_TAP_TERM(event)) {
185 if (tapping_key.tap_count > 0 && IS_TAPPING_KEY(event.key) && event.pressed) {
187 keyp->tap_count = tapping_key.tap_count + 1;
188 debug("Tapping: tap press("); debug_dec(keyp->tap_count); debug(")\n");
192 } else if (event.pressed && is_tap_key(event.key)) {
193 // Sequential tap can be interfered with other tap key.
194 debug("Tapping: Start with interfering other tap.\n");
198 if (!IS_NOEVENT(keyp->event)) debug("Tapping: other key just after tap.\n");
203 // timeout. no sequential tap.
204 debug("Tapping: End(Time out after releasing last tap).\n");
205 tapping_key = (keyrecord_t){};
210 if (event.pressed && is_tap_key(event.key)) {
211 debug("Tapping: Start(Press tap key).\n");
221 void action_exec(keyevent_t event)
223 if (!IS_NOEVENT(event)) {
225 debug_hex16(event.time); debug(": ");
226 debug_hex16(event.key.raw);
228 if (event.pressed) debug("down"); else debug("up");
232 keyrecord_t record = { .event = event };
234 // pre-process on tapping
235 if (process_tap(&record)) {
236 if (!IS_NOEVENT(record.event)) debug("processed.\n");
238 if (!IS_NOEVENT(record.event)) debug("enqueued.\n");
239 if (!waiting_buffer_enq(record)) {
240 // clear all in case of overflow.
242 waiting_buffer_clear();
243 tapping_key = (keyrecord_t){};
247 // TODO: need to process every time?
248 // process waiting_buffer
249 for (; waiting_buffer_tail != waiting_buffer_head; waiting_buffer_tail = (waiting_buffer_tail + 1) % WAITING_BUFFER_SIZE) {
250 if (process_tap(&waiting_buffer[waiting_buffer_tail])) {
251 debug("processed: waiting_buffer["); debug_dec(waiting_buffer_tail); debug("] = ");
252 debug_hex16(waiting_buffer[waiting_buffer_tail].event.key.raw); debug("\n");
259 static void process(keyrecord_t *record)
262 keyevent_t event = record->event;
263 uint8_t tap_count = record->tap_count;
265 if (IS_NOEVENT(event)) { return; }
267 action_t action = keymap_get_action(current_layer, event.key.pos.row, event.key.pos.col);
268 debug("action: "); debug_hex16(action.code);
269 if (event.pressed) debug("[down]\n"); else debug("[up]\n");
271 switch (action.kind.id) {
274 // |pressed |released
275 // --------------+---------------------------------+------------
276 // key |down(key) |up(key)
277 // mods |add(mods) |del(mods)
278 // key with mods |add(mods), down(key), unset(mods)|up(key)
280 uint8_t tmp_mods = host_get_mods();
281 if (action.key.mods) {
282 host_add_mods(action.key.mods);
283 host_send_keyboard_report();
285 register_code(action.key.code);
286 if (action.key.mods && action.key.code) {
287 host_set_mods(tmp_mods);
288 host_send_keyboard_report();
291 if (action.key.mods && !action.key.code) {
292 host_del_mods(action.key.mods);
293 host_send_keyboard_report();
295 unregister_code(action.key.code);
299 // |pressed |released
300 // --------------+---------------------------------+------------
301 // key |down(key) |up(key)
302 // mods |add(mods) |del(mods)
303 // key with mods |add(mods), down(key), unset(mods)|up(key)
305 uint8_t tmp_mods = host_get_mods();
306 if (action.key.mods) {
307 host_add_mods(action.key.mods<<4);
308 host_send_keyboard_report();
310 register_code(action.key.code);
311 if (action.key.mods && action.key.code) {
312 host_set_mods(tmp_mods);
313 host_send_keyboard_report();
316 if (action.key.mods && !action.key.code) {
317 host_del_mods(action.key.mods<<4);
318 host_send_keyboard_report();
320 unregister_code(action.key.code);
326 uint8_t tmp_mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods :
329 if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
330 if (waiting_buffer_has_anykey_pressed()) {
331 debug("MODS_TAP: Tap: Cancel: add_mods\n");
332 // ad hoc: set 0 to cancel tap
333 record->tap_count = 0;
336 debug("MODS_TAP: Tap: register_code\n");
337 register_code(action.key.code);
340 debug("MODS_TAP: No tap: add_mods\n");
344 if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
345 debug("MODS_TAP: Tap: unregister_code\n");
346 unregister_code(action.key.code);
348 debug("MODS_TAP: No tap: add_mods\n");
355 /* other HID usage */
357 #ifdef EXTRAKEY_ENABLE
358 switch (action.usage.page) {
359 case ACTION_USAGE_PAGE_SYSTEM:
361 host_system_send(action.usage.code);
366 case ACTION_USAGE_PAGE_CONSUMER:
368 host_consumer_send(action.usage.code);
370 host_consumer_send(0);
379 #ifdef MOUSEKEY_ENABLE
381 mousekey_on(action.key.code);
384 mousekey_off(action.key.code);
391 case ACT_LAYER_PRESSED:
392 // layer action when pressed
393 switch (action.layer.code) {
396 layer_switch(action.layer.opt);
404 default_layer = action.layer.opt;
405 layer_switch(default_layer);
411 if (IS_TAPPING_KEY(event.key)) {
413 debug("LAYER_PRESSED: Tap: register_code\n");
414 register_code(action.layer.code);
416 debug("LAYER_PRESSED: No tap: layer_switch\n");
417 layer_switch(action.layer.opt);
420 // TODO: while other key tapping
421 debug("LAYER_PRESSED: No tap: layer_switch\n");
422 layer_switch(action.layer.opt);
425 if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
426 debug("LAYER_PRESSED: Tap: register_code\n");
427 register_code(action.layer.code);
429 debug("LAYER_PRESSED: No tap: layer_switch\n");
430 layer_switch(action.layer.opt);
434 if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
435 debug("LAYER_PRESSED: Tap: unregister_code\n");
436 unregister_code(action.layer.code);
438 debug("LAYER_PRESSED: No tap: NO ACTION\n");
444 case ACT_LAYER_RELEASED:
445 switch (action.layer.code) {
447 if (!event.pressed) {
448 layer_switch(action.layer.opt);
452 // Ignored. LAYER_RELEASED with tap toggle is invalid action.
455 if (!event.pressed) {
456 default_layer = action.layer.opt;
457 layer_switch(default_layer);
461 // Ignored. LAYER_RELEASED with tap key is invalid action.
466 switch (action.layer.code) {
469 layer_switch(current_layer | action.layer.opt);
471 layer_switch(current_layer & ~action.layer.opt);
478 // change default layer
480 default_layer = current_layer | action.layer.opt;
481 layer_switch(default_layer);
483 default_layer = current_layer & ~action.layer.opt;
484 layer_switch(default_layer);
490 if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
491 debug("LAYER_BIT: Tap: register_code\n");
492 register_code(action.layer.code);
494 debug("LAYER_BIT: No tap: layer_switch(bit on)\n");
495 layer_switch(current_layer | action.layer.opt);
498 if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
499 debug("LAYER_BIT: Tap: unregister_code\n");
500 unregister_code(action.layer.code);
502 debug("LAYER_BIT: No tap: layer_switch(bit off)\n");
503 layer_switch(current_layer & ~action.layer.opt);
509 switch (action.layer.opt) {
511 // set default layer when pressed
512 switch (action.layer.code) {
515 layer_switch(default_layer);
523 default_layer = current_layer;
524 layer_switch(default_layer);
533 // set default layer when released
534 switch (action.layer.code) {
536 if (!event.pressed) {
537 layer_switch(default_layer);
541 if (!event.pressed) {
542 default_layer = current_layer;
543 layer_switch(default_layer);
549 if (!event.pressed) {
550 layer_switch(default_layer);
564 action_call_function(event, action.func.id);
573 * Utilities for actions.
575 void register_code(uint8_t code)
580 else if IS_KEY(code) {
581 // TODO: should push command_proc out of this block?
582 if (!command_proc(code)) {
584 host_send_keyboard_report();
587 else if IS_MOD(code) {
588 host_add_mods(MOD_BIT(code));
589 host_send_keyboard_report();
593 void unregister_code(uint8_t code)
597 host_send_keyboard_report();
599 else if IS_MOD(code) {
600 host_del_mods(MOD_BIT(code));
601 host_send_keyboard_report();
605 void add_mods(uint8_t mods)
609 host_send_keyboard_report();
613 void del_mods(uint8_t mods)
617 host_send_keyboard_report();
621 void set_mods(uint8_t mods)
624 host_send_keyboard_report();
627 void clear_keyboard(void)
630 clear_keyboard_but_mods();
633 void clear_keyboard_but_mods(void)
636 host_send_keyboard_report();
637 #ifdef MOUSEKEY_ENABLE
641 #ifdef EXTRAKEY_ENABLE
643 host_consumer_send(0);
647 bool sending_anykey(void)
649 return (host_has_anykey() || host_mouse_in_use() ||
650 host_last_sysytem_report() || host_last_consumer_report());
653 void layer_switch(uint8_t new_layer)
655 if (current_layer != new_layer) {
656 debug("Layer Switch: "); debug_hex(current_layer);
657 debug(" -> "); debug_hex(new_layer); debug("\n");
659 current_layer = new_layer;
660 clear_keyboard_but_mods(); // To avoid stuck keys
661 // TODO: update mods with full scan of matrix? if modifier changes between layers
665 bool is_tap_key(key_t key)
667 action_t action = keymap_get_action(current_layer, key.pos.row, key.pos.col);
668 switch (action.kind.id) {
672 case ACT_LAYER_PRESSED:
674 switch (action.layer.code) {
684 if (action.func.opt & 0x1) {