13 static void process(keyrecord_t *record);
15 void test_func(keyevent_t event, uint8_t opt)
18 debug("test_func:pressed: "); debug_hex(opt); debug("\n");
20 debug("test_func:released: "); debug_hex(opt); debug("\n");
25 uint8_t default_layer = 0;
26 uint8_t current_layer = 0;
31 /* This counts up when tap occurs */
32 uint8_t tap_count = 0;
33 keyevent_t tapping_event = {};
34 keyrecord_t tapping_key = {};
36 /* TAPPING: This indicates that whether tap or not is not decided yet. */
37 // NOTE: keyevent_t.time 0 means no event.
38 #define IS_TAPPING() (tapping_key.event.time != 0)
39 #define IS_TAPPING_PRESSED() (IS_TAPPING() && tapping_key.event.pressed)
40 #define IS_TAPPING_RELEASED() (IS_TAPPING() && !tapping_key.event.pressed)
41 #define IS_TAPPING_KEY(k) (IS_TAPPING() && KEYEQ(tapping_key.event.key, (k)))
42 #define WITHIN_TAP_TERM(e) (TIMER_DIFF_16(e.time, tapping_key.event.time) < TAP_TERM)
44 /* waiting keys buffer */
45 #define WAITING_BUFFER_SIZE 8
46 static keyrecord_t waiting_buffer[WAITING_BUFFER_SIZE] = {};
47 /* point to empty cell to enq */
48 static uint8_t waiting_buffer_head = 0;
49 /* point to the oldest data cell to deq */
50 static uint8_t waiting_buffer_tail = 0;
52 static bool waiting_buffer_enq(keyrecord_t record)
54 if (IS_NOEVENT(record.event)) {
58 if ((waiting_buffer_head + 1) % WAITING_BUFFER_SIZE == waiting_buffer_tail) {
59 debug("waiting_buffer_enq: Over flow.\n");
63 debug("waiting_buffer_enq["); debug_dec(waiting_buffer_head); debug("] = ");
64 debug_hex16(record.event.key.raw); debug("\n");
66 waiting_buffer[waiting_buffer_head] = record;
67 waiting_buffer_head = (waiting_buffer_head + 1) % WAITING_BUFFER_SIZE;
70 static keyrecord_t waiting_buffer_deq(void)
72 if (waiting_buffer_head == waiting_buffer_tail) {
73 return (keyrecord_t){};
75 uint8_t last_tail = waiting_buffer_tail;
76 waiting_buffer_tail = waiting_buffer_tail + 1 % WAITING_BUFFER_SIZE;
77 return waiting_buffer[last_tail];
79 static bool waiting_buffer_is_empty(void)
81 return (waiting_buffer_head == waiting_buffer_tail);
83 static void waiting_buffer_clear(void)
85 waiting_buffer_head = 0;
86 waiting_buffer_tail = 0;
88 static bool waiting_buffer_typed(keyevent_t event)
90 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
91 if (KEYEQ(event.key, waiting_buffer[i].event.key) && event.pressed != waiting_buffer[i].event.pressed) {
97 static bool waiting_buffer_has_anykey_pressed(void)
99 for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
100 if (waiting_buffer[i].event.pressed) return true;
104 static void waiting_buffer_process(void)
111 * Tap key is typed(pressed and released) within TAP_TERM
112 * without interfaring by typing other key.
114 /* return true when key event is processed. */
115 static bool process_tap(keyrecord_t *keyp)
117 keyevent_t event = keyp->event;
120 if (IS_TAPPING_PRESSED()) {
121 if (WITHIN_TAP_TERM(event)) {
122 if (tapping_key.tap_count == 0) {
123 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
125 debug("Tapping: First tap.\n");
126 tapping_key.tap_count = 1;
127 process(&tapping_key);
130 keyp->tap_count = tapping_key.tap_count;
132 } else if (!event.pressed && waiting_buffer_typed(event)) {
133 // other key typed. not tap.
134 debug("Tapping: End(No tap. Interfered by typing key).\n");
135 process(&tapping_key);
136 tapping_key = (keyrecord_t){};
141 // other key events shall be stored till tapping state settles.
145 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
146 keyp->tap_count = tapping_key.tap_count;
147 debug("Tapping: tap release("); debug_dec(keyp->tap_count); debug(")\n");
151 else if (is_tap_key(keyp->event.key) && event.pressed) {
152 debug("Tapping: Start with forcing to release last tap.\n");
153 process(&(keyrecord_t){
154 .tap_count = tapping_key.tap_count,
155 .event.key = tapping_key.event.key,
156 .event.time = event.time,
157 .event.pressed = false
163 if (!IS_NOEVENT(keyp->event)) debug("Tapping: key event while tap.\n");
169 // not within TAP_TERM
171 if (tapping_key.tap_count == 0) {
173 debug("Tapping: End. Not tap(time out).\n");
174 process(&tapping_key);
175 tapping_key = (keyrecord_t){};
178 if (IS_TAPPING_KEY(event.key) && !event.pressed) {
179 debug("Tapping: End. tap release.");
180 keyp->tap_count = tapping_key.tap_count;
182 tapping_key = (keyrecord_t){};
185 // other key after tap time out.
191 } else if (IS_TAPPING_RELEASED()) {
192 if (WITHIN_TAP_TERM(event)) {
193 if (tapping_key.tap_count > 0 && IS_TAPPING_KEY(event.key) && event.pressed) {
195 keyp->tap_count = tapping_key.tap_count + 1;
196 debug("Tapping: tap press("); debug_dec(keyp->tap_count); debug(")\n");
200 } else if (event.pressed && is_tap_key(event.key)) {
201 // Sequential tap can be interfered with other tap key.
202 debug("Tapping: Start with interfering other tap.\n");
206 if (!IS_NOEVENT(keyp->event)) debug("Tapping: other key just after tap.\n");
211 // timeout. no sequential tap.
212 debug("Tapping: End(Time out after releasing last tap).\n");
213 tapping_key = (keyrecord_t){};
218 if (event.pressed && is_tap_key(event.key)) {
219 debug("Tapping: Start(Press tap key).\n");
229 void action_exec(keyevent_t event)
231 if (!IS_NOEVENT(event)) {
233 debug_hex16(event.time); debug(": ");
234 debug_hex16(event.key.raw);
236 if (event.pressed) debug("down"); else debug("up");
240 keyrecord_t record = { .event = event };
242 // pre-process on tapping
243 if (process_tap(&record)) {
244 if (!IS_NOEVENT(record.event)) debug("processed.\n");
246 if (!IS_NOEVENT(record.event)) debug("enqueued.\n");
247 if (!waiting_buffer_enq(record)) {
248 // clear all in case of overflow.
250 waiting_buffer_clear();
251 tapping_key = (keyrecord_t){};
255 // TODO: need to process every time?
256 // process waiting_buffer
257 for (; waiting_buffer_tail != waiting_buffer_head; waiting_buffer_tail = (waiting_buffer_tail + 1) % WAITING_BUFFER_SIZE) {
258 if (process_tap(&waiting_buffer[waiting_buffer_tail])) {
259 debug("processed: waiting_buffer["); debug_dec(waiting_buffer_tail); debug("] = ");
260 debug_hex16(waiting_buffer[waiting_buffer_tail].event.key.raw); debug("\n");
267 static void process(keyrecord_t *record)
270 keyevent_t event = record->event;
271 uint8_t tap_count = record->tap_count;
273 if (IS_NOEVENT(event)) { return; }
275 action_t action = keymap_get_action(current_layer, event.key.pos.row, event.key.pos.col);
276 debug("action: "); debug_hex16(action.code);
277 if (event.pressed) debug("[down]\n"); else debug("[up]\n");
279 switch (action.kind.id) {
282 // |pressed |released
283 // --------------+---------------------------------+------------
284 // key |down(key) |up(key)
285 // mods |add(mods) |del(mods)
286 // key with mods |add(mods), down(key), unset(mods)|up(key)
288 uint8_t tmp_mods = host_get_mods();
289 if (action.key.mods) {
290 host_add_mods(action.key.mods);
291 host_send_keyboard_report();
293 register_code(action.key.code);
294 if (action.key.mods && action.key.code) {
295 host_set_mods(tmp_mods);
296 host_send_keyboard_report();
299 if (action.key.mods && !action.key.code) {
300 host_del_mods(action.key.mods);
301 host_send_keyboard_report();
303 unregister_code(action.key.code);
307 // |pressed |released
308 // --------------+---------------------------------+------------
309 // key |down(key) |up(key)
310 // mods |add(mods) |del(mods)
311 // key with mods |add(mods), down(key), unset(mods)|up(key)
313 uint8_t tmp_mods = host_get_mods();
314 if (action.key.mods) {
315 host_add_mods(action.key.mods<<4);
316 host_send_keyboard_report();
318 register_code(action.key.code);
319 if (action.key.mods && action.key.code) {
320 host_set_mods(tmp_mods);
321 host_send_keyboard_report();
324 if (action.key.mods && !action.key.code) {
325 host_del_mods(action.key.mods<<4);
326 host_send_keyboard_report();
328 unregister_code(action.key.code);
334 uint8_t tmp_mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods :
337 if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
338 if (waiting_buffer_has_anykey_pressed()) {
339 debug("MODS_TAP: Tap: Cancel: add_mods\n");
340 // ad hoc: set 0 to cancel tap
341 record->tap_count = 0;
344 debug("MODS_TAP: Tap: register_code\n");
345 register_code(action.key.code);
348 debug("MODS_TAP: No tap: add_mods\n");
352 if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
353 debug("MODS_TAP: Tap: unregister_code\n");
354 unregister_code(action.key.code);
356 debug("MODS_TAP: No tap: add_mods\n");
363 /* other HID usage */
365 #ifdef EXTRAKEY_ENABLE
366 switch (action.usage.page) {
367 case ACTION_USAGE_PAGE_SYSTEM:
369 host_system_send(action.usage.code);
374 case ACTION_USAGE_PAGE_CONSUMER:
376 host_consumer_send(action.usage.code);
378 host_consumer_send(0);
387 #ifdef MOUSEKEY_ENABLE
389 mousekey_on(action.key.code);
392 mousekey_off(action.key.code);
399 case ACT_LAYER_PRESSED:
400 // layer action when pressed
401 switch (action.layer.code) {
404 layer_switch(action.layer.opt);
412 default_layer = action.layer.opt;
413 layer_switch(default_layer);
419 if (IS_TAPPING_KEY(event.key)) {
421 debug("LAYER_PRESSED: Tap: register_code\n");
422 register_code(action.layer.code);
424 debug("LAYER_PRESSED: No tap: layer_switch\n");
425 layer_switch(action.layer.opt);
428 // TODO: while other key tapping
429 debug("LAYER_PRESSED: No tap: layer_switch\n");
430 layer_switch(action.layer.opt);
433 if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
434 debug("LAYER_PRESSED: Tap: register_code\n");
435 register_code(action.layer.code);
437 debug("LAYER_PRESSED: No tap: layer_switch\n");
438 layer_switch(action.layer.opt);
442 if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
443 debug("LAYER_PRESSED: Tap: unregister_code\n");
444 unregister_code(action.layer.code);
446 debug("LAYER_PRESSED: No tap: NO ACTION\n");
452 case ACT_LAYER_RELEASED:
453 switch (action.layer.code) {
455 if (!event.pressed) {
456 layer_switch(action.layer.opt);
460 // Ignored. LAYER_RELEASED with tap toggle is invalid action.
463 if (!event.pressed) {
464 default_layer = action.layer.opt;
465 layer_switch(default_layer);
469 // Ignored. LAYER_RELEASED with tap key is invalid action.
474 switch (action.layer.code) {
477 layer_switch(current_layer | action.layer.opt);
479 layer_switch(current_layer & ~action.layer.opt);
486 // change default layer
488 default_layer = current_layer | action.layer.opt;
489 layer_switch(default_layer);
491 default_layer = current_layer & ~action.layer.opt;
492 layer_switch(default_layer);
498 if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
499 debug("LAYER_BIT: Tap: register_code\n");
500 register_code(action.layer.code);
502 debug("LAYER_BIT: No tap: layer_switch(bit on)\n");
503 layer_switch(current_layer | action.layer.opt);
506 if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
507 debug("LAYER_BIT: Tap: unregister_code\n");
508 unregister_code(action.layer.code);
510 debug("LAYER_BIT: No tap: layer_switch(bit off)\n");
511 layer_switch(current_layer & ~action.layer.opt);
517 switch (action.layer.opt) {
519 // set default layer when pressed
520 switch (action.layer.code) {
523 layer_switch(default_layer);
531 default_layer = current_layer;
532 layer_switch(default_layer);
541 // set default layer when released
542 switch (action.layer.code) {
544 if (!event.pressed) {
545 layer_switch(default_layer);
549 if (!event.pressed) {
550 default_layer = current_layer;
551 layer_switch(default_layer);
557 if (!event.pressed) {
558 layer_switch(default_layer);
572 action_call_function(event, action.func.id);
573 //test_func(event, action.func.opt);
582 * Utilities for actions.
584 void register_code(uint8_t code)
589 else if IS_KEY(code) {
590 // TODO: should push command_proc out of this block?
591 if (!command_proc(code)) {
593 host_send_keyboard_report();
596 else if IS_MOD(code) {
597 host_add_mods(MOD_BIT(code));
598 host_send_keyboard_report();
602 void unregister_code(uint8_t code)
606 host_send_keyboard_report();
608 else if IS_MOD(code) {
609 host_del_mods(MOD_BIT(code));
610 host_send_keyboard_report();
614 void add_mods(uint8_t mods)
618 host_send_keyboard_report();
622 void del_mods(uint8_t mods)
626 host_send_keyboard_report();
630 void set_mods(uint8_t mods)
633 host_send_keyboard_report();
636 void clear_keyboard(void)
639 clear_keyboard_but_mods();
642 void clear_keyboard_but_mods(void)
645 host_send_keyboard_report();
646 #ifdef MOUSEKEY_ENABLE
650 #ifdef EXTRAKEY_ENABLE
652 host_consumer_send(0);
656 bool sending_anykey(void)
658 return (host_has_anykey() || host_mouse_in_use() ||
659 host_last_sysytem_report() || host_last_consumer_report());
662 void layer_switch(uint8_t new_layer)
664 if (current_layer != new_layer) {
665 debug("Layer Switch: "); debug_hex(current_layer);
666 debug(" -> "); debug_hex(new_layer); debug("\n");
668 current_layer = new_layer;
669 clear_keyboard_but_mods(); // To avoid stuck keys
670 // TODO: update mods with full scan of matrix? if modifier changes between layers
674 bool is_tap_key(key_t key)
676 action_t action = keymap_get_action(current_layer, key.pos.row, key.pos.col);
677 switch (action.kind.id) {
681 case ACT_LAYER_PRESSED:
683 switch (action.layer.code) {
693 if (action.func.opt & 0x1) {