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 static keyevent_t last_event = {};
28 static uint8_t tap_count = 0;
31 uint8_t default_layer = 0;
32 uint8_t current_layer = 0;
33 static keyrecord_t tapping_key = {};
34 // time 0 means no event.
35 #define IS_TAPPING (tapping_key.event.time != 0)
37 #define IS_TAPPING_KEY(key) (tapping_key.event.time != 0 && KEYEQ(tapping_key.event.key, key))
40 /* waiting keys buffer */
41 #define WAITING_KEYS_BUFFER 3
42 static keyrecord_t waiting_keys[WAITING_KEYS_BUFFER] = {};
43 // TODO: double buffer?
44 static keyrecord_t waiting_keys0[WAITING_KEYS_BUFFER] = {};
45 static keyrecord_t waiting_keys1[WAITING_KEYS_BUFFER] = {};
46 static uint8_t waiting_keys_head = 0;
47 static bool waiting_keys_enqueue(keyevent_t event)
49 debug("waiting_keys["); debug_dec(waiting_keys_head); debug("] = ");
50 debug_hex8(event.key.row); debug_hex8(event.key.col); debug("\n"); // TODO event.key.raw
51 if (waiting_keys_head < WAITING_KEYS_BUFFER) {
52 waiting_keys[waiting_keys_head++] = (keyrecord_t){ .event = event,
53 .mods = host_get_mods() };
58 static void waiting_keys_clear(void)
60 waiting_keys_head = 0;
62 static bool waiting_keys_has(keypos_t key)
64 for (uint8_t i = 0; i < waiting_keys_head; i++) {
65 if KEYEQ(key, waiting_keys[i].event.key) return true;
69 static void waiting_keys_process_in_current_layer(void)
71 // TODO: in case of including layer key in waiting keys
72 for (uint8_t i = 0; i < waiting_keys_head; i++) {
73 debug("waiting_keys_process_in_current_layer["); debug_dec(i); debug("]\n");
74 // TODO: no need action in waiting_keys? should get_action() in process()?
75 process(waiting_keys[i].event);
81 void action_exec(keyevent_t event)
84 /* when tap time elapses or waiting key is released */
85 if ((timer_elapsed(tapping_key.event.time) > TAP_TIME) ||
86 (!event.pressed && waiting_keys_has(event.key))) {
88 // TODO process tapping_key: layer swich, modifier, ...
90 debug("notap: process tapping_key.\n");
91 process(tapping_key.event);
93 /* Process waiting keys in new layer */
94 waiting_keys_process_in_current_layer();
96 /* when tapping key is released within tap time */
97 else if (!event.pressed && KEYEQ(event.key, tapping_key.event.key)) {
99 debug("tap("); debug_hex8(tap_count); debug(")[tapping_key](register): "); debug_hex8(tapping_key.action.layer.code); debug("\n");
100 register_code(tapping_key.action.layer.code);
101 tapping_key = (keyrecord_t){};
103 /* process waiting keys */
104 waiting_keys_process_in_current_layer();
108 // not real event. event just to handle time out of tapping key.
109 if (IS_NOEVENT(event)) {
113 /* count up tap when key is up */
118 if (KEYEQ(event.key, last_event.key) && timer_elapsed(last_event.time) <= TAP_TIME) {
119 if (!event.pressed) tap_count++;
124 /* store key events while tapping */
126 // TODO: action is needed?
127 waiting_keys_enqueue(event);
137 static void process(keyevent_t event)
139 action_t action = keymap_get_action(current_layer, event.key.row, event.key.col);
140 debug("action: "); debug_hex16(action.code);
141 if (event.pressed) debug("[down]\n"); else debug("[up]\n");
143 switch (action.kind.id) {
146 // |pressed |released
147 // --------------+---------------------------------+------------
148 // key |down(key) |up(key)
149 // mods |add(mods) |del(mods)
150 // key with mods |add(mods), down(key), unset(mods)|up(key)
152 uint8_t tmp_mods = host_get_mods();
153 if (action.key.mods) {
154 host_add_mods(action.key.mods);
155 host_send_keyboard_report();
157 register_code(action.key.code);
158 if (action.key.mods && action.key.code) {
159 host_set_mods(tmp_mods);
160 host_send_keyboard_report();
163 if (action.key.mods && !action.key.code) {
164 host_del_mods(action.key.mods);
165 host_send_keyboard_report();
167 unregister_code(action.key.code);
171 // |pressed |released
172 // --------------+---------------------------------+------------
173 // key |down(key) |up(key)
174 // mods |add(mods) |del(mods)
175 // key with mods |add(mods), down(key), unset(mods)|up(key)
177 uint8_t tmp_mods = host_get_mods();
178 if (action.key.mods) {
179 host_add_mods(action.key.mods<<4);
180 host_send_keyboard_report();
182 register_code(action.key.code);
183 if (action.key.mods && action.key.code) {
184 host_set_mods(tmp_mods);
185 host_send_keyboard_report();
188 if (action.key.mods && !action.key.code) {
189 host_del_mods(action.key.mods<<4);
190 host_send_keyboard_report();
192 unregister_code(action.key.code);
197 if (tap_count == 0) {
198 if (host_has_anykey()) {
199 // This key is a modifier essentially.
200 // Prioritize mods when key jam or rollover
201 add_mods(action.key.mods);
203 if (IS_TAPPING && KEYEQ(tapping_key.event.key, event.key)) {
205 add_mods(action.key.mods);
206 tapping_key = (keyrecord_t){};
208 debug("tapping lmods("); debug_hex8(action.key.mods); debug(")\n");
209 tapping_key = (keyrecord_t){
212 .mods = host_get_mods()
217 // pressed after tapping
218 debug("tap("); debug_hex(tap_count); debug(")[lmods](register): "); debug_hex8(action.key.code); debug("\n");
219 register_code(action.key.code);
222 if (tap_count == 0) {
223 debug("tap(00)[lmods](del_mods): "); debug_hex8(action.key.mods); debug("\n");
224 del_mods(action.key.mods);
225 } else if (tap_count == 1) {
226 debug("tap(01)[lmods](del_mods/unregister): "); debug_hex8(action.key.mods); debug(" "); debug_hex8(action.key.code); debug("\n");
227 del_mods(action.key.mods);
228 unregister_code(action.key.code);
230 debug("tap("); debug_hex(tap_count); debug(")[lmods](unregister): "); debug_hex8(action.key.code); debug("\n");
231 unregister_code(action.key.code);
237 if (tap_count == 0) {
238 if (host_has_anykey()) {
239 // This key is a modifier essentially.
240 // Prioritize mods when key jam or rollover
241 add_mods(action.key.mods<<4);
243 if (IS_TAPPING && KEYEQ(tapping_key.event.key, event.key)) {
245 add_mods(action.key.mods<<4);
246 tapping_key = (keyrecord_t){};
248 debug("tapping rmods("); debug_hex8(action.key.mods); debug(")\n");
249 tapping_key = (keyrecord_t){
252 .mods = host_get_mods()
257 // pressed after tapping
258 debug("tap("); debug_hex(tap_count); debug(")[rmods](register): "); debug_hex8(action.key.code); debug("\n");
259 register_code(action.key.code);
262 if (tap_count == 0) {
263 debug("tap(00)[rmods](del_mods): "); debug_hex8(action.key.mods); debug("\n");
264 del_mods(action.key.mods<<4);
265 } else if (tap_count == 1) {
266 debug("tap(01)[rmods](del_mods/unregister): "); debug_hex8(action.key.mods); debug(" "); debug_hex8(action.key.code); debug("\n");
267 del_mods(action.key.mods<<4);
268 unregister_code(action.key.code);
270 debug("tap("); debug_hex(tap_count); debug(")[rmods](unregister): "); debug_hex8(action.key.code); debug("\n");
271 unregister_code(action.key.code);
276 /* other HID usage */
278 #ifdef EXTRAKEY_ENABLE
279 switch (action.usage.page) {
280 case ACTION_USAGE_PAGE_SYSTEM:
282 host_system_send(action.usage.code);
287 case ACTION_USAGE_PAGE_CONSUMER:
289 host_consumer_send(action.usage.code);
291 host_consumer_send(0);
300 #ifdef MOUSEKEY_ENABLE
302 mousekey_on(action.key.code);
305 mousekey_off(action.key.code);
312 case ACT_LAYER_PRESSED:
313 // layer action when pressed
314 switch (action.layer.code) {
317 layer_switch(action.layer.opt);
325 default_layer = action.layer.opt;
326 layer_switch(default_layer);
332 if (tap_count == 0) {
333 if (host_has_anykey()) {
334 // This key is a normal key than a leyar key essentially.
335 // Prioritize 'tap key' when key jam or rollover
336 register_code(action.layer.code);
338 if (IS_TAPPING && KEYEQ(tapping_key.event.key, event.key)) {
339 layer_switch(action.layer.opt);
340 tapping_key = (keyrecord_t){};
342 debug("tapping layer("); debug_hex8(action.layer.opt); debug(")\n");
343 tapping_key = (keyrecord_t){
346 .mods = host_get_mods()
350 } else if (tap_count > 0) {
351 // pressed after tapping
352 debug("tap[layer](register): "); debug_hex(tap_count); debug("\n");
353 register_code(action.layer.code);
356 // released after tapping
357 debug("tap[layer](unregister): "); debug_hex(tap_count); debug("\n");
358 unregister_code(action.layer.code);
363 case ACT_LAYER_RELEASED:
364 switch (action.layer.code) {
366 if (!event.pressed) {
367 layer_switch(action.layer.opt);
371 // Ignored. LAYER_RELEASED with tap toggle is invalid action.
374 if (!event.pressed) {
375 default_layer = action.layer.opt;
376 layer_switch(default_layer);
380 // Ignored. LAYER_RELEASED with tap key is invalid action.
385 switch (action.layer.code) {
388 layer_switch(current_layer | action.layer.opt);
390 layer_switch(current_layer & ~action.layer.opt);
397 // change default layer
399 default_layer = current_layer | action.layer.opt;
400 layer_switch(default_layer);
402 default_layer = current_layer & ~action.layer.opt;
403 layer_switch(default_layer);
407 // TODO: see ACT_LAYER_PRESSED code
410 if (tap_count == 0) {
411 if (host_has_anykey()) {
412 register_code(action.layer.code);
414 tapping_key = (keyrecord_t){
417 .mods = keyboard_report->mods
420 } else if (tap_count > 0) {
421 debug("tap[layer_bit](register): "); debug_hex(tap_count); debug("\n");
422 register_code(action.layer.code);
425 if (tap_count == 0) {
427 layer_switch(current_layer & ~action.layer.opt);
428 } else if (tap_count == 1) {
430 register_code(action.layer.code);
432 unregister_code(action.layer.code);
437 switch (action.layer.opt) {
439 // set default layer when pressed
440 switch (action.layer.code) {
443 layer_switch(default_layer);
451 default_layer = current_layer;
452 layer_switch(default_layer);
461 // set default layer when released
462 switch (action.layer.code) {
464 if (!event.pressed) {
465 layer_switch(default_layer);
469 if (!event.pressed) {
470 default_layer = current_layer;
471 layer_switch(default_layer);
477 if (!event.pressed) {
478 layer_switch(default_layer);
495 static void register_code(uint8_t code)
500 else if IS_KEY(code) {
501 // TODO: should push command_proc out of this block?
502 if (!command_proc(code)) {
504 host_send_keyboard_report();
507 else if IS_MOD(code) {
508 host_add_mods(MOD_BIT(code));
509 host_send_keyboard_report();
513 static void unregister_code(uint8_t code)
517 host_send_keyboard_report();
519 else if IS_MOD(code) {
520 host_del_mods(MOD_BIT(code));
521 host_send_keyboard_report();
525 static void add_mods(uint8_t mods)
529 host_send_keyboard_report();
533 static void del_mods(uint8_t mods)
537 host_send_keyboard_report();
541 static void set_mods(uint8_t mods)
544 host_send_keyboard_report();
547 static void clear_keyboard(void)
550 clear_keyboard_but_mods();
553 static void clear_keyboard_but_mods(void)
556 host_send_keyboard_report();
557 #ifdef MOUSEKEY_ENABLE
561 #ifdef EXTRAKEY_ENABLE
563 host_consumer_send(0);
567 static bool sending_anykey(void)
569 return (host_has_anykey() || host_mouse_in_use() ||
570 host_last_sysytem_report() || host_last_consumer_report());
573 static void layer_switch(uint8_t new_layer)
575 if (current_layer != new_layer) {
576 debug("Layer Switch: "); debug_hex(current_layer);
577 debug(" -> "); debug_hex(new_layer); debug("\n");
579 current_layer = new_layer;
580 clear_keyboard_but_mods(); // To avoid stuck keys
581 // TODO: update mods with full scan of matrix? if modifier changes between layers