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_hex16(event.key.raw); debug("\n");
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(key_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 process(waiting_keys[i].event);
80 void action_exec(keyevent_t event)
82 if (!IS_NOEVENT(event)) {
83 debug("event: "); debug_hex16(event.key.raw);
85 if (event.pressed) debug("down"); else debug("up");
89 /* when tap time elapses or waiting key is released */
90 if ((timer_elapsed(tapping_key.event.time) > TAP_TIME) ||
91 (!event.pressed && waiting_keys_has(event.key))) {
93 // TODO process tapping_key: layer swich, modifier, ...
95 debug("notap: process tapping_key.\n");
96 process(tapping_key.event);
98 /* Process waiting keys in new layer */
99 waiting_keys_process_in_current_layer();
101 /* when tapping key is released within tap time */
102 else if (!event.pressed && KEYEQ(event.key, tapping_key.event.key)) {
104 debug("tap("); debug_hex8(tap_count); debug(")[tapping_key](register): "); debug_hex8(tapping_key.action.layer.code); debug("\n");
105 register_code(tapping_key.action.layer.code);
106 tapping_key = (keyrecord_t){};
108 /* process waiting keys */
109 waiting_keys_process_in_current_layer();
113 // not real event. event just to handle time out of tapping key.
114 if (IS_NOEVENT(event)) {
118 /* count up tap when key is up */
123 if (KEYEQ(event.key, last_event.key) && timer_elapsed(last_event.time) <= TAP_TIME) {
124 if (!event.pressed) tap_count++;
129 /* store key events while tapping */
131 // TODO: action is needed?
132 waiting_keys_enqueue(event);
142 static void process(keyevent_t event)
144 action_t action = keymap_get_action(current_layer, event.key.pos.row, event.key.pos.col);
145 debug("action: "); debug_hex16(action.code);
146 if (event.pressed) debug("[down]\n"); else debug("[up]\n");
148 switch (action.kind.id) {
151 // |pressed |released
152 // --------------+---------------------------------+------------
153 // key |down(key) |up(key)
154 // mods |add(mods) |del(mods)
155 // key with mods |add(mods), down(key), unset(mods)|up(key)
157 uint8_t tmp_mods = host_get_mods();
158 if (action.key.mods) {
159 host_add_mods(action.key.mods);
160 host_send_keyboard_report();
162 register_code(action.key.code);
163 if (action.key.mods && action.key.code) {
164 host_set_mods(tmp_mods);
165 host_send_keyboard_report();
168 if (action.key.mods && !action.key.code) {
169 host_del_mods(action.key.mods);
170 host_send_keyboard_report();
172 unregister_code(action.key.code);
176 // |pressed |released
177 // --------------+---------------------------------+------------
178 // key |down(key) |up(key)
179 // mods |add(mods) |del(mods)
180 // key with mods |add(mods), down(key), unset(mods)|up(key)
182 uint8_t tmp_mods = host_get_mods();
183 if (action.key.mods) {
184 host_add_mods(action.key.mods<<4);
185 host_send_keyboard_report();
187 register_code(action.key.code);
188 if (action.key.mods && action.key.code) {
189 host_set_mods(tmp_mods);
190 host_send_keyboard_report();
193 if (action.key.mods && !action.key.code) {
194 host_del_mods(action.key.mods<<4);
195 host_send_keyboard_report();
197 unregister_code(action.key.code);
202 if (tap_count == 0) {
203 if (host_has_anykey()) {
204 // This key is a modifier essentially.
205 // Prioritize mods when key jam or rollover
206 add_mods(action.key.mods);
208 if (IS_TAPPING && KEYEQ(tapping_key.event.key, event.key)) {
210 add_mods(action.key.mods);
211 tapping_key = (keyrecord_t){};
213 debug("tapping lmods("); debug_hex8(action.key.mods); debug(")\n");
214 tapping_key = (keyrecord_t){
217 .mods = host_get_mods()
222 // pressed after tapping
223 debug("tap("); debug_hex(tap_count); debug(")[lmods](register): "); debug_hex8(action.key.code); debug("\n");
224 register_code(action.key.code);
227 if (tap_count == 0) {
228 debug("tap(00)[lmods](del_mods): "); debug_hex8(action.key.mods); debug("\n");
229 del_mods(action.key.mods);
230 } else if (tap_count == 1) {
231 debug("tap(01)[lmods](del_mods/unregister): "); debug_hex8(action.key.mods); debug(" "); debug_hex8(action.key.code); debug("\n");
232 del_mods(action.key.mods);
233 unregister_code(action.key.code);
235 debug("tap("); debug_hex(tap_count); debug(")[lmods](unregister): "); debug_hex8(action.key.code); debug("\n");
236 unregister_code(action.key.code);
242 if (tap_count == 0) {
243 if (host_has_anykey()) {
244 // This key is a modifier essentially.
245 // Prioritize mods when key jam or rollover
246 add_mods(action.key.mods<<4);
248 if (IS_TAPPING && KEYEQ(tapping_key.event.key, event.key)) {
250 add_mods(action.key.mods<<4);
251 tapping_key = (keyrecord_t){};
253 debug("tapping rmods("); debug_hex8(action.key.mods); debug(")\n");
254 tapping_key = (keyrecord_t){
257 .mods = host_get_mods()
262 // pressed after tapping
263 debug("tap("); debug_hex(tap_count); debug(")[rmods](register): "); debug_hex8(action.key.code); debug("\n");
264 register_code(action.key.code);
267 if (tap_count == 0) {
268 debug("tap(00)[rmods](del_mods): "); debug_hex8(action.key.mods); debug("\n");
269 del_mods(action.key.mods<<4);
270 } else if (tap_count == 1) {
271 debug("tap(01)[rmods](del_mods/unregister): "); debug_hex8(action.key.mods); debug(" "); debug_hex8(action.key.code); debug("\n");
272 del_mods(action.key.mods<<4);
273 unregister_code(action.key.code);
275 debug("tap("); debug_hex(tap_count); debug(")[rmods](unregister): "); debug_hex8(action.key.code); debug("\n");
276 unregister_code(action.key.code);
281 /* other HID usage */
283 #ifdef EXTRAKEY_ENABLE
284 switch (action.usage.page) {
285 case ACTION_USAGE_PAGE_SYSTEM:
287 host_system_send(action.usage.code);
292 case ACTION_USAGE_PAGE_CONSUMER:
294 host_consumer_send(action.usage.code);
296 host_consumer_send(0);
305 #ifdef MOUSEKEY_ENABLE
307 mousekey_on(action.key.code);
310 mousekey_off(action.key.code);
317 case ACT_LAYER_PRESSED:
318 // layer action when pressed
319 switch (action.layer.code) {
322 layer_switch(action.layer.opt);
330 default_layer = action.layer.opt;
331 layer_switch(default_layer);
337 if (tap_count == 0) {
338 if (host_has_anykey()) {
339 // This key is a normal key than a leyar key essentially.
340 // Prioritize 'tap key' when key jam or rollover
341 register_code(action.layer.code);
343 if (IS_TAPPING && KEYEQ(tapping_key.event.key, event.key)) {
344 layer_switch(action.layer.opt);
345 tapping_key = (keyrecord_t){};
347 debug("tapping layer("); debug_hex8(action.layer.opt); debug(")\n");
348 tapping_key = (keyrecord_t){
351 .mods = host_get_mods()
355 } else if (tap_count > 0) {
356 // pressed after tapping
357 debug("tap[layer](register): "); debug_hex(tap_count); debug("\n");
358 register_code(action.layer.code);
361 // released after tapping
362 debug("tap[layer](unregister): "); debug_hex(tap_count); debug("\n");
363 unregister_code(action.layer.code);
368 case ACT_LAYER_RELEASED:
369 switch (action.layer.code) {
371 if (!event.pressed) {
372 layer_switch(action.layer.opt);
376 // Ignored. LAYER_RELEASED with tap toggle is invalid action.
379 if (!event.pressed) {
380 default_layer = action.layer.opt;
381 layer_switch(default_layer);
385 // Ignored. LAYER_RELEASED with tap key is invalid action.
390 switch (action.layer.code) {
393 layer_switch(current_layer | action.layer.opt);
395 layer_switch(current_layer & ~action.layer.opt);
402 // change default layer
404 default_layer = current_layer | action.layer.opt;
405 layer_switch(default_layer);
407 default_layer = current_layer & ~action.layer.opt;
408 layer_switch(default_layer);
412 // TODO: see ACT_LAYER_PRESSED code
415 if (tap_count == 0) {
416 if (host_has_anykey()) {
417 register_code(action.layer.code);
419 tapping_key = (keyrecord_t){
422 .mods = keyboard_report->mods
425 } else if (tap_count > 0) {
426 debug("tap[layer_bit](register): "); debug_hex(tap_count); debug("\n");
427 register_code(action.layer.code);
430 if (tap_count == 0) {
432 layer_switch(current_layer & ~action.layer.opt);
433 } else if (tap_count == 1) {
435 register_code(action.layer.code);
437 unregister_code(action.layer.code);
442 switch (action.layer.opt) {
444 // set default layer when pressed
445 switch (action.layer.code) {
448 layer_switch(default_layer);
456 default_layer = current_layer;
457 layer_switch(default_layer);
466 // set default layer when released
467 switch (action.layer.code) {
469 if (!event.pressed) {
470 layer_switch(default_layer);
474 if (!event.pressed) {
475 default_layer = current_layer;
476 layer_switch(default_layer);
482 if (!event.pressed) {
483 layer_switch(default_layer);
500 static void register_code(uint8_t code)
505 else if IS_KEY(code) {
506 // TODO: should push command_proc out of this block?
507 if (!command_proc(code)) {
509 host_send_keyboard_report();
512 else if IS_MOD(code) {
513 host_add_mods(MOD_BIT(code));
514 host_send_keyboard_report();
518 static void unregister_code(uint8_t code)
522 host_send_keyboard_report();
524 else if IS_MOD(code) {
525 host_del_mods(MOD_BIT(code));
526 host_send_keyboard_report();
530 static void add_mods(uint8_t mods)
534 host_send_keyboard_report();
538 static void del_mods(uint8_t mods)
542 host_send_keyboard_report();
546 static void set_mods(uint8_t mods)
549 host_send_keyboard_report();
552 static void clear_keyboard(void)
555 clear_keyboard_but_mods();
558 static void clear_keyboard_but_mods(void)
561 host_send_keyboard_report();
562 #ifdef MOUSEKEY_ENABLE
566 #ifdef EXTRAKEY_ENABLE
568 host_consumer_send(0);
572 static bool sending_anykey(void)
574 return (host_has_anykey() || host_mouse_in_use() ||
575 host_last_sysytem_report() || host_last_consumer_report());
578 static void layer_switch(uint8_t new_layer)
580 if (current_layer != new_layer) {
581 debug("Layer Switch: "); debug_hex(current_layer);
582 debug(" -> "); debug_hex(new_layer); debug("\n");
584 current_layer = new_layer;
585 clear_keyboard_but_mods(); // To avoid stuck keys
586 // TODO: update mods with full scan of matrix? if modifier changes between layers