13 static void process(keyevent_t event
);
14 static void register_code(uint8_t code
);
15 static void unregister_code(uint8_t code
);
16 static void add_mods(uint8_t mods
);
17 static void del_mods(uint8_t mods
);
18 static void set_mods(uint8_t mods
);
19 static void clear_keyboard(void);
20 static void clear_keyboard_but_mods(void);
21 static bool sending_anykey(void);
22 static void layer_switch(uint8_t new_layer
);
27 /* This counts up when tap occurs */
28 static uint8_t tap_count
= 0;
29 static bool is_tap_key(keyevent_t event
)
31 action_t action
= keymap_get_action(current_layer
, event
.key
.pos
.row
, event
.key
.pos
.col
);
32 switch (action
.kind
.id
) {
36 case ACT_LAYER_PRESSED
:
38 switch (action
.layer
.code
) {
52 uint8_t default_layer
= 0;
53 uint8_t current_layer
= 0;
54 static keyevent_t tapping_event
= {};
56 /* TAPPING: This indicates that whether tap or not is not decided yet. */
57 // NOTE: keyevent_t.time 0 means no event.
58 #define IS_TAPPING(k) (tapping_event.time != 0 && KEYEQ(tapping_event.key, (k)))
60 /* waiting keys buffer */
61 #define WAITING_KEYS_BUFFER 8
62 static keyevent_t waiting_events
[WAITING_KEYS_BUFFER
] = {};
63 static uint8_t waiting_events_head
= 0;
64 static bool waiting_events_enqueue(keyevent_t event
)
66 if (IS_NOEVENT(event
)) { return true; }
68 if (waiting_events_head
< WAITING_KEYS_BUFFER
) {
69 debug("waiting_events["); debug_dec(waiting_events_head
); debug("] = ");
70 debug_hex16(event
.key
.raw
); debug("\n");
71 waiting_events
[waiting_events_head
++] = event
;
74 debug("waiting_events_enqueue: Over flow.\n");
77 static void waiting_events_clear(void)
79 waiting_events_head
= 0;
81 static bool waiting_events_has(key_t key
)
83 for (uint8_t i
= 0; i
< waiting_events_head
; i
++) {
84 if KEYEQ(key
, waiting_events
[i
].key
) return true;
88 static void waiting_events_process_in_current_layer(void)
90 // TODO: in case of including tap key in waiting keys
91 for (uint8_t i
= 0; i
< waiting_events_head
; i
++) {
92 debug("waiting_events_process_in_current_layer["); debug_dec(i
); debug("]\n");
93 process(waiting_events
[i
]);
95 waiting_events_clear();
97 static bool waiting_events_has_anykey_pressed(void)
99 for (uint8_t i
= 0; i
< waiting_events_head
; i
++) {
100 if (waiting_events
[i
].pressed
) return true;
106 void action_exec(keyevent_t event
)
108 if (!IS_NOEVENT(event
)) {
109 debug("event: "); debug_hex16(event
.key
.raw
);
111 if (event
.pressed
) debug("down"); else debug("up");
116 if (tapping_event
.time
&& timer_elapsed(tapping_event
.time
) < TAP_TIME
) {
117 if (tapping_event
.pressed
) {
118 if (!event
.pressed
&& KEYEQ(tapping_event
.key
, event
.key
)) {
119 debug("Tapping: Release tap key.\n");
120 if (tap_count
== 0) {
121 debug("Tapping: First tap.\n");
122 // count up on release
125 process(tapping_event
);
126 waiting_events_process_in_current_layer();
128 tapping_event
= event
;
130 } else if (!event
.pressed
&& waiting_events_has(event
.key
)) {
131 debug("Tapping: End(No tap by typing waiting key).\n");
133 process(tapping_event
);
134 waiting_events_process_in_current_layer();
138 tapping_event
= (keyevent_t
){};
140 //debug("Tapping: pressing tap key.\n");
141 if (tap_count
== 0) {
143 waiting_events_enqueue(event
);
149 //debug("Tapping after releasing tap.\n");
151 if (tap_count
&& event
.pressed
&& KEYEQ(tapping_event
.key
, event
.key
)) {
153 tapping_event
= event
;
154 debug("Tapping: Sequential tap("); debug_hex(tap_count
); debug(")\n");
159 // Not in tapping term
161 if (tapping_event
.time
) {
162 if (tapping_event
.pressed
) {
163 if (tap_count
== 0) {
164 // Not tap, holding down normal key.
166 process(tapping_event
);
167 waiting_events_process_in_current_layer();
170 tapping_event
= (keyevent_t
){};
173 // Holding down last tap key.
174 //debug("Time out with holding last tap.\n");
176 if (!event
.pressed
&& KEYEQ(tapping_event
.key
, event
.key
)) {
177 debug("Tapping: End(Release holding last tap).\n");
178 // clear after release last tap key
180 tapping_event
= (keyevent_t
){};
181 waiting_events_clear();
185 // time out for sequential tap after complete last tap
186 debug("Tapping: End(Time out after releasing last tap).\n");
188 tapping_event
= (keyevent_t
){};
189 waiting_events_clear();
194 // Normal state without tapping
195 if (event
.pressed
&& is_tap_key(event
)) {
196 debug("Tapping: Start(Press tap key).\n");
197 tapping_event
= event
;
199 waiting_events_clear();
201 //debug("Normal event(No tapping)\n");
210 static void process(keyevent_t event
)
212 if (IS_NOEVENT(event
)) { return; }
214 action_t action
= keymap_get_action(current_layer
, event
.key
.pos
.row
, event
.key
.pos
.col
);
215 debug("action: "); debug_hex16(action
.code
);
216 if (event
.pressed
) debug("[down]\n"); else debug("[up]\n");
218 switch (action
.kind
.id
) {
221 // |pressed |released
222 // --------------+---------------------------------+------------
223 // key |down(key) |up(key)
224 // mods |add(mods) |del(mods)
225 // key with mods |add(mods), down(key), unset(mods)|up(key)
227 uint8_t tmp_mods
= host_get_mods();
228 if (action
.key
.mods
) {
229 host_add_mods(action
.key
.mods
);
230 host_send_keyboard_report();
232 register_code(action
.key
.code
);
233 if (action
.key
.mods
&& action
.key
.code
) {
234 host_set_mods(tmp_mods
);
235 host_send_keyboard_report();
238 if (action
.key
.mods
&& !action
.key
.code
) {
239 host_del_mods(action
.key
.mods
);
240 host_send_keyboard_report();
242 unregister_code(action
.key
.code
);
246 // |pressed |released
247 // --------------+---------------------------------+------------
248 // key |down(key) |up(key)
249 // mods |add(mods) |del(mods)
250 // key with mods |add(mods), down(key), unset(mods)|up(key)
252 uint8_t tmp_mods
= host_get_mods();
253 if (action
.key
.mods
) {
254 host_add_mods(action
.key
.mods
<<4);
255 host_send_keyboard_report();
257 register_code(action
.key
.code
);
258 if (action
.key
.mods
&& action
.key
.code
) {
259 host_set_mods(tmp_mods
);
260 host_send_keyboard_report();
263 if (action
.key
.mods
&& !action
.key
.code
) {
264 host_del_mods(action
.key
.mods
<<4);
265 host_send_keyboard_report();
267 unregister_code(action
.key
.code
);
273 uint8_t tmp_mods
= (action
.kind
.id
== ACT_LMODS_TAP
) ? action
.key
.mods
:
276 if (IS_TAPPING(event
.key
) && tap_count
> 0) {
277 if (waiting_events_has_anykey_pressed()) {
278 debug("MODS_TAP: Tap: Cancel: add_mods\n");
282 debug("MODS_TAP: Tap: register_code\n");
283 register_code(action
.key
.code
);
286 debug("MODS_TAP: No tap: add_mods\n");
290 if (IS_TAPPING(event
.key
) && tap_count
> 0) {
291 debug("MODS_TAP: Tap: unregister_code\n");
292 unregister_code(action
.key
.code
);
294 debug("MODS_TAP: No tap: add_mods\n");
301 /* other HID usage */
303 #ifdef EXTRAKEY_ENABLE
304 switch (action
.usage
.page
) {
305 case ACTION_USAGE_PAGE_SYSTEM
:
307 host_system_send(action
.usage
.code
);
312 case ACTION_USAGE_PAGE_CONSUMER
:
314 host_consumer_send(action
.usage
.code
);
316 host_consumer_send(0);
325 #ifdef MOUSEKEY_ENABLE
327 mousekey_on(action
.key
.code
);
330 mousekey_off(action
.key
.code
);
337 case ACT_LAYER_PRESSED
:
338 // layer action when pressed
339 switch (action
.layer
.code
) {
342 layer_switch(action
.layer
.opt
);
350 default_layer
= action
.layer
.opt
;
351 layer_switch(default_layer
);
357 if (IS_TAPPING(event
.key
) && tap_count
> 0) {
358 debug("LAYER_PRESSED: Tap: register_code\n");
359 register_code(action
.layer
.code
);
361 debug("LAYER_PRESSED: No tap: layer_switch\n");
362 layer_switch(action
.layer
.opt
);
365 if (IS_TAPPING(event
.key
) && tap_count
> 0) {
366 debug("LAYER_PRESSED: Tap: unregister_code\n");
367 unregister_code(action
.layer
.code
);
369 debug("LAYER_PRESSED: No tap: NO ACTION\n");
375 case ACT_LAYER_RELEASED
:
376 switch (action
.layer
.code
) {
378 if (!event
.pressed
) {
379 layer_switch(action
.layer
.opt
);
383 // Ignored. LAYER_RELEASED with tap toggle is invalid action.
386 if (!event
.pressed
) {
387 default_layer
= action
.layer
.opt
;
388 layer_switch(default_layer
);
392 // Ignored. LAYER_RELEASED with tap key is invalid action.
397 switch (action
.layer
.code
) {
400 layer_switch(current_layer
| action
.layer
.opt
);
402 layer_switch(current_layer
& ~action
.layer
.opt
);
409 // change default layer
411 default_layer
= current_layer
| action
.layer
.opt
;
412 layer_switch(default_layer
);
414 default_layer
= current_layer
& ~action
.layer
.opt
;
415 layer_switch(default_layer
);
421 if (IS_TAPPING(event
.key
) && tap_count
> 0) {
422 debug("LAYER_BIT: Tap: register_code\n");
423 register_code(action
.layer
.code
);
425 debug("LAYER_BIT: No tap: layer_switch(bit on)\n");
426 layer_switch(current_layer
| action
.layer
.opt
);
429 if (IS_TAPPING(event
.key
) && tap_count
> 0) {
430 debug("LAYER_BIT: Tap: unregister_code\n");
431 unregister_code(action
.layer
.code
);
433 debug("LAYER_BIT: No tap: layer_switch(bit off)\n");
434 layer_switch(current_layer
& ~action
.layer
.opt
);
440 switch (action
.layer
.opt
) {
442 // set default layer when pressed
443 switch (action
.layer
.code
) {
446 layer_switch(default_layer
);
454 default_layer
= current_layer
;
455 layer_switch(default_layer
);
464 // set default layer when released
465 switch (action
.layer
.code
) {
467 if (!event
.pressed
) {
468 layer_switch(default_layer
);
472 if (!event
.pressed
) {
473 default_layer
= current_layer
;
474 layer_switch(default_layer
);
480 if (!event
.pressed
) {
481 layer_switch(default_layer
);
498 static void register_code(uint8_t code
)
503 else if IS_KEY(code
) {
504 // TODO: should push command_proc out of this block?
505 if (!command_proc(code
)) {
507 host_send_keyboard_report();
510 else if IS_MOD(code
) {
511 host_add_mods(MOD_BIT(code
));
512 host_send_keyboard_report();
516 static void unregister_code(uint8_t code
)
520 host_send_keyboard_report();
522 else if IS_MOD(code
) {
523 host_del_mods(MOD_BIT(code
));
524 host_send_keyboard_report();
528 static void add_mods(uint8_t mods
)
532 host_send_keyboard_report();
536 static void del_mods(uint8_t mods
)
540 host_send_keyboard_report();
544 static void set_mods(uint8_t mods
)
547 host_send_keyboard_report();
550 static void clear_keyboard(void)
553 clear_keyboard_but_mods();
556 static void clear_keyboard_but_mods(void)
559 host_send_keyboard_report();
560 #ifdef MOUSEKEY_ENABLE
564 #ifdef EXTRAKEY_ENABLE
566 host_consumer_send(0);
570 static bool sending_anykey(void)
572 return (host_has_anykey() || host_mouse_in_use() ||
573 host_last_sysytem_report() || host_last_consumer_report());
576 static void layer_switch(uint8_t new_layer
)
578 if (current_layer
!= new_layer
) {
579 debug("Layer Switch: "); debug_hex(current_layer
);
580 debug(" -> "); debug_hex(new_layer
); debug("\n");
582 current_layer
= new_layer
;
583 clear_keyboard_but_mods(); // To avoid stuck keys
584 // TODO: update mods with full scan of matrix? if modifier changes between layers