13 static void process(keyevent_t event);
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 = {};
35 /* TAPPING: This indicates that whether tap or not is not decided yet. */
36 // NOTE: keyevent_t.time 0 means no event.
37 #define IS_TAPPING(k) (tapping_event.time != 0 && KEYEQ(tapping_event.key, (k)))
39 /* waiting keys buffer */
40 #define WAITING_KEYS_BUFFER 8
41 static keyevent_t waiting_events[WAITING_KEYS_BUFFER] = {};
42 static uint8_t waiting_events_head = 0;
43 static uint8_t waiting_events_tail = 0;
45 static bool waiting_events_enqueue(keyevent_t event)
47 if (IS_NOEVENT(event)) { return true; }
49 if ((waiting_events_head + 1) % WAITING_KEYS_BUFFER == waiting_events_tail) {
50 debug("waiting_events_enqueue: Over flow.\n");
54 debug("waiting_events["); debug_dec(waiting_events_head); debug("] = ");
55 debug_hex16(event.key.raw); debug("\n");
57 waiting_events[waiting_events_head] = event;
58 waiting_events_head = (waiting_events_head + 1)% WAITING_KEYS_BUFFER;
61 static keyevent_t waiting_events_dequeue(void)
63 if (waiting_events_head == waiting_events_tail) {
64 return (keyevent_t){};
66 uint8_t tail = waiting_events_tail;
67 waiting_events_tail = waiting_events_tail + 1 % WAITING_KEYS_BUFFER;
68 return waiting_events[tail];
70 static void waiting_events_clear(void)
72 waiting_events_head = 0;
73 waiting_events_tail = 0;
75 static bool waiting_events_has(key_t key)
77 for (uint8_t i = waiting_events_tail; i != waiting_events_head; i = (i + 1) % WAITING_KEYS_BUFFER) {
78 if KEYEQ(key, waiting_events[i].key) return true;
82 static void waiting_events_process_in_current_layer(void)
84 // TODO: in case of including tap key in waiting keys
85 for (uint8_t i = waiting_events_tail; i != waiting_events_head; i = (i + 1) % WAITING_KEYS_BUFFER) {
86 debug("waiting_events_process_in_current_layer["); debug_dec(i); debug("]\n");
87 process(waiting_events[i]);
89 waiting_events_clear();
91 static bool waiting_events_has_anykey_pressed(void)
93 for (uint8_t i = waiting_events_tail; i != waiting_events_head; i = (i + 1) % WAITING_KEYS_BUFFER) {
94 if (waiting_events[i].pressed) return true;
100 void action_exec(keyevent_t event)
102 if (!IS_NOEVENT(event)) {
103 debug("event: "); debug_hex16(event.key.raw);
105 if (event.pressed) debug("down"); else debug("up");
110 if (tapping_event.time && timer_elapsed(tapping_event.time) < TAP_TIME) {
111 if (tapping_event.pressed) {
112 if (!event.pressed && KEYEQ(tapping_event.key, event.key)) {
113 debug("Tapping: Release tap key.\n");
114 if (tap_count == 0) {
115 debug("Tapping: First tap.\n");
116 // count up on release
119 process(tapping_event);
120 waiting_events_process_in_current_layer();
122 tapping_event = event;
124 } else if (!event.pressed && waiting_events_has(event.key)) {
125 debug("Tapping: End(No tap by typing waiting key).\n");
127 process(tapping_event);
128 waiting_events_process_in_current_layer();
132 tapping_event = (keyevent_t){};
134 if (!IS_NOEVENT(event)) debug("Tapping: other key while tapping.\n");
135 if (tap_count == 0) {
137 waiting_events_enqueue(event);
143 // Waiting for sequential tap
144 if (tap_count && event.pressed && KEYEQ(tapping_event.key, event.key)) {
146 tapping_event = event;
147 debug("Tapping: Sequential tap("); debug_hex(tap_count); debug(")\n");
149 } else if (event.pressed && is_tap_key(event)) {
150 // Sequential tap can be interfered with other tap key.
151 debug("Tapping: Start with interfering other tap.\n");
152 tapping_event = event;
154 waiting_events_clear();
156 if (!IS_NOEVENT(event)) debug("Tapping: other key just after tap.\n");
161 // Not in tapping term
163 if (tapping_event.time) {
164 if (tapping_event.pressed) {
165 if (tap_count == 0) {
166 // Not tap, holding down normal key.
167 debug("Tapping: End. Not tap(time out).\n");
168 process(tapping_event);
169 waiting_events_process_in_current_layer();
172 tapping_event = (keyevent_t){};
175 // Holding down last tap key. waiting for releasing last tap key.
176 if (!event.pressed && KEYEQ(tapping_event.key, event.key)) {
177 debug("Tapping: End. Release holding last tap(time out).\n");
179 // clear after release last tap key
181 tapping_event = (keyevent_t){};
182 waiting_events_clear();
183 } else if (event.pressed && is_tap_key(event)) {
184 debug("Tapping: Start with forcing to release last tap(time out).\n");
185 process((keyevent_t){
186 .key = tapping_event.key,
191 tapping_event = event;
192 waiting_events_clear();
194 if (!IS_NOEVENT(event)) debug("Tapping: other key while waiting for release of last tap(time out).\n");
199 // time out for sequential tap after complete last tap
200 debug("Tapping: End(Time out after releasing last tap).\n");
202 tapping_event = (keyevent_t){};
203 waiting_events_clear();
208 // Normal state without tapping
209 if (event.pressed && is_tap_key(event)) {
210 debug("Tapping: Start(Press tap key).\n");
211 tapping_event = event;
213 waiting_events_clear();
215 //debug("Normal event(No tapping)\n");
223 static void process(keyevent_t event)
225 if (IS_NOEVENT(event)) { return; }
227 action_t action = keymap_get_action(current_layer, event.key.pos.row, event.key.pos.col);
228 debug("action: "); debug_hex16(action.code);
229 if (event.pressed) debug("[down]\n"); else debug("[up]\n");
231 switch (action.kind.id) {
234 // |pressed |released
235 // --------------+---------------------------------+------------
236 // key |down(key) |up(key)
237 // mods |add(mods) |del(mods)
238 // key with mods |add(mods), down(key), unset(mods)|up(key)
240 uint8_t tmp_mods = host_get_mods();
241 if (action.key.mods) {
242 host_add_mods(action.key.mods);
243 host_send_keyboard_report();
245 register_code(action.key.code);
246 if (action.key.mods && action.key.code) {
247 host_set_mods(tmp_mods);
248 host_send_keyboard_report();
251 if (action.key.mods && !action.key.code) {
252 host_del_mods(action.key.mods);
253 host_send_keyboard_report();
255 unregister_code(action.key.code);
259 // |pressed |released
260 // --------------+---------------------------------+------------
261 // key |down(key) |up(key)
262 // mods |add(mods) |del(mods)
263 // key with mods |add(mods), down(key), unset(mods)|up(key)
265 uint8_t tmp_mods = host_get_mods();
266 if (action.key.mods) {
267 host_add_mods(action.key.mods<<4);
268 host_send_keyboard_report();
270 register_code(action.key.code);
271 if (action.key.mods && action.key.code) {
272 host_set_mods(tmp_mods);
273 host_send_keyboard_report();
276 if (action.key.mods && !action.key.code) {
277 host_del_mods(action.key.mods<<4);
278 host_send_keyboard_report();
280 unregister_code(action.key.code);
286 uint8_t tmp_mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods :
289 if (IS_TAPPING(event.key) && tap_count > 0) {
290 if (waiting_events_has_anykey_pressed()) {
291 debug("MODS_TAP: Tap: Cancel: add_mods\n");
295 debug("MODS_TAP: Tap: register_code\n");
296 register_code(action.key.code);
299 debug("MODS_TAP: No tap: add_mods\n");
303 if (IS_TAPPING(event.key) && tap_count > 0) {
304 debug("MODS_TAP: Tap: unregister_code\n");
305 unregister_code(action.key.code);
307 debug("MODS_TAP: No tap: add_mods\n");
314 /* other HID usage */
316 #ifdef EXTRAKEY_ENABLE
317 switch (action.usage.page) {
318 case ACTION_USAGE_PAGE_SYSTEM:
320 host_system_send(action.usage.code);
325 case ACTION_USAGE_PAGE_CONSUMER:
327 host_consumer_send(action.usage.code);
329 host_consumer_send(0);
338 #ifdef MOUSEKEY_ENABLE
340 mousekey_on(action.key.code);
343 mousekey_off(action.key.code);
350 case ACT_LAYER_PRESSED:
351 // layer action when pressed
352 switch (action.layer.code) {
355 layer_switch(action.layer.opt);
363 default_layer = action.layer.opt;
364 layer_switch(default_layer);
370 if (IS_TAPPING(event.key)) {
372 debug("LAYER_PRESSED: Tap: register_code\n");
373 register_code(action.layer.code);
375 debug("LAYER_PRESSED: No tap: layer_switch\n");
376 layer_switch(action.layer.opt);
379 // TODO: while other key tapping
380 debug("LAYER_PRESSED: No tap: layer_switch\n");
381 layer_switch(action.layer.opt);
384 if (IS_TAPPING(event.key) && tap_count > 0) {
385 debug("LAYER_PRESSED: Tap: register_code\n");
386 register_code(action.layer.code);
388 debug("LAYER_PRESSED: No tap: layer_switch\n");
389 layer_switch(action.layer.opt);
393 if (IS_TAPPING(event.key) && tap_count > 0) {
394 debug("LAYER_PRESSED: Tap: unregister_code\n");
395 unregister_code(action.layer.code);
397 debug("LAYER_PRESSED: No tap: NO ACTION\n");
403 case ACT_LAYER_RELEASED:
404 switch (action.layer.code) {
406 if (!event.pressed) {
407 layer_switch(action.layer.opt);
411 // Ignored. LAYER_RELEASED with tap toggle is invalid action.
414 if (!event.pressed) {
415 default_layer = action.layer.opt;
416 layer_switch(default_layer);
420 // Ignored. LAYER_RELEASED with tap key is invalid action.
425 switch (action.layer.code) {
428 layer_switch(current_layer | action.layer.opt);
430 layer_switch(current_layer & ~action.layer.opt);
437 // change default layer
439 default_layer = current_layer | action.layer.opt;
440 layer_switch(default_layer);
442 default_layer = current_layer & ~action.layer.opt;
443 layer_switch(default_layer);
449 if (IS_TAPPING(event.key) && tap_count > 0) {
450 debug("LAYER_BIT: Tap: register_code\n");
451 register_code(action.layer.code);
453 debug("LAYER_BIT: No tap: layer_switch(bit on)\n");
454 layer_switch(current_layer | action.layer.opt);
457 if (IS_TAPPING(event.key) && tap_count > 0) {
458 debug("LAYER_BIT: Tap: unregister_code\n");
459 unregister_code(action.layer.code);
461 debug("LAYER_BIT: No tap: layer_switch(bit off)\n");
462 layer_switch(current_layer & ~action.layer.opt);
468 switch (action.layer.opt) {
470 // set default layer when pressed
471 switch (action.layer.code) {
474 layer_switch(default_layer);
482 default_layer = current_layer;
483 layer_switch(default_layer);
492 // set default layer when released
493 switch (action.layer.code) {
495 if (!event.pressed) {
496 layer_switch(default_layer);
500 if (!event.pressed) {
501 default_layer = current_layer;
502 layer_switch(default_layer);
508 if (!event.pressed) {
509 layer_switch(default_layer);
523 action_call_function(event, action.func.id);
524 //test_func(event, action.func.opt);
533 * Utilities for actions.
535 void register_code(uint8_t code)
540 else if IS_KEY(code) {
541 // TODO: should push command_proc out of this block?
542 if (!command_proc(code)) {
544 host_send_keyboard_report();
547 else if IS_MOD(code) {
548 host_add_mods(MOD_BIT(code));
549 host_send_keyboard_report();
553 void unregister_code(uint8_t code)
557 host_send_keyboard_report();
559 else if IS_MOD(code) {
560 host_del_mods(MOD_BIT(code));
561 host_send_keyboard_report();
565 void add_mods(uint8_t mods)
569 host_send_keyboard_report();
573 void del_mods(uint8_t mods)
577 host_send_keyboard_report();
581 void set_mods(uint8_t mods)
584 host_send_keyboard_report();
587 void clear_keyboard(void)
590 clear_keyboard_but_mods();
593 void clear_keyboard_but_mods(void)
596 host_send_keyboard_report();
597 #ifdef MOUSEKEY_ENABLE
601 #ifdef EXTRAKEY_ENABLE
603 host_consumer_send(0);
607 bool sending_anykey(void)
609 return (host_has_anykey() || host_mouse_in_use() ||
610 host_last_sysytem_report() || host_last_consumer_report());
613 void layer_switch(uint8_t new_layer)
615 if (current_layer != new_layer) {
616 debug("Layer Switch: "); debug_hex(current_layer);
617 debug(" -> "); debug_hex(new_layer); debug("\n");
619 current_layer = new_layer;
620 clear_keyboard_but_mods(); // To avoid stuck keys
621 // TODO: update mods with full scan of matrix? if modifier changes between layers
625 bool is_tap_key(keyevent_t event)
627 action_t action = keymap_get_action(current_layer, event.key.pos.row, event.key.pos.col);
628 switch (action.kind.id) {
632 case ACT_LAYER_PRESSED:
634 switch (action.layer.code) {
644 if (action.func.opt & 0x1) {