12 #define Kdebug(s) do { if (debug_keyboard) debug(s); } while(0)
13 #define Kdebug_P(s) do { if (debug_keyboard) debug_P(s); } while(0)
14 #define Kdebug_hex(s) do { if (debug_keyboard) debug_hex(s); } while(0)
19 * Event/State|IDLE PRESSING DELAYING[f] WAITING[f,k]
20 * -----------+------------------------------------------------------------------
21 * Fn Down |(L+) -*1 WAITING(Sk) IDLE(Rf,Ps)*7
22 * Up |(L-) IDLE(L-)*8 IDLE(L-)*8 IDLE(L-)*8
23 * Fnk Down |DELAYING(Sf)* (Rf) WAITING(Sk) IDLE(Rf,Ps,Rf)
24 * Up |(L-) IDLE(L-/Uf)*8 IDLE(Rf,Uf/L-)*3 IDLE(Rf,Ps,Uf/L-)*3
25 * Key Down |PRESSING(Rk) (Rk) WAITING(Sk) IDLE(Rf,Ps,Rk)
26 * Up |(Uk) IDLE(Uk)*4 (Uk) IDLE(L+,Ps,Pk)/(Uk)*a
28 * Delay |- - IDLE(L+) IDLE(L+,Ps)
29 * Magic Key |COMMAND*5
31 * *1: ignore Fn if other key is down.
32 * *2: register Fnk if any key is pressing
33 * *3: register/unregister delayed Fnk and move to IDLE if code == delayed Fnk, else *8
34 * *4: if no keys registered to host
35 * *5: unregister all keys
36 * *6: only if no keys down
37 * *7: ignore Fn because Fnk key and stored key are down.
38 * *8: move to IDLE if layer switch(off) occurs, else stay at current state
39 * *9: repeat key if pressing Fnk twice quickly(move to PRESSING)
40 * *a: layer switch and process waiting key and code if code == wainting key, else unregister key
43 * IDLE: No key is down except modifiers
44 * DELAYING: delay layer switch after pressing Fn with alt keycode
45 * WAITING: key is pressed during DELAYING
48 * Fn: Fn key without alternative keycode
49 * Fnk: Fn key with alternative keycode
51 * Delay: layer switch delay term is elapsed
56 * Rf: register Fn(alt keycode)
57 * Uf: unregister Fn(alt keycode)
58 * Rs: register stored key
59 * Us: unregister stored key
60 * Sk: Store key(waiting Key)
61 * Sf: Store Fn(delayed Fn)
62 * Ps: Process stored key
64 * Is: Interpret stored keys in current layer
65 * L+: Switch to new layer(*unregister* all keys but modifiers)
66 * L-: Switch back to last layer(*unregister* all keys but modifiers)
67 * Ld: Switch back to default layer(*unregister* all keys but modifiers)
71 typedef enum { IDLE, DELAYING, WAITING, PRESSING } kbdstate_t;
72 #define NEXT(state) do { \
73 Kdebug("NEXT: "); Kdebug_P(state_str(kbdstate)); \
75 Kdebug(" -> "); Kdebug_P(state_str(kbdstate)); Kdebug("\n"); \
79 static kbdstate_t kbdstate = IDLE;
80 static uint8_t fn_state_bits = 0;
82 static const char *state_str(kbdstate_t state)
84 if (state == IDLE) return PSTR("IDLE");
85 if (state == DELAYING) return PSTR("DELAYING");
86 if (state == WAITING) return PSTR("WAITING");
87 if (state == PRESSING) return PSTR("PRESSING");
88 return PSTR("UNKNOWN");
90 static bool anykey_sent_to_host(void)
92 return (host_has_anykey() || host_mouse_in_use() ||
93 host_last_sysytem_report() || host_last_consumer_report());
97 static void register_code(uint8_t code);
98 static void unregister_code(uint8_t code);
99 static void register_mods(uint8_t mods);
100 static void unregister_mods(uint8_t mods);
101 static void clear_keyboard(void);
102 static void clear_keyboard_but_mods(void);
103 static void layer_switch(uint8_t new_layer);
108 #define LAYER_DELAY 200
109 static keyevent_t last_event = {};
110 static uint16_t last_event_time = 0;
111 static uint8_t tap_count = 0;
114 uint8_t default_layer = 0;
115 uint8_t current_layer = 0;
116 keyrecord_t delaying_layer = {};
118 keyrecord_t waiting_key = {};
120 // TODO: ring buffer: waiting_keys[]
122 #define WAITING_KEYS_BUFFER 3
123 static keyrecord_t waiting_keys[WAITING_KEYS_BUFFER] = {};
124 static uint8_t waiting_keys_head = 0;
125 static uint8_t waiting_keys_tail = 0;
126 static void waiting_key_queue(keyevent_t event)
129 static void waiting_key_dequeue(keyevent_t event)
134 static void process(keyevent_t event, action_t action)
136 //action_t action = keymap_get_action(current_layer, event.key.row, event.key.col);
138 debug("action: "); debug_hex16(action.code); debug("\n");
139 debug("kind.id: "); debug_hex(action.kind.id); debug("\n");
140 debug("kind.param: "); debug_hex16(action.kind.param); debug("\n");
141 debug("key.code: "); debug_hex(action.key.code); debug("\n");
142 debug("key.mods: "); debug_hex(action.key.mods); debug("\n");
144 switch (action.kind.id) {
147 // normal key or key plus mods
149 uint8_t tmp_mods = host_get_mods();
150 if (action.key.mods) {
151 host_add_mods(action.key.mods);
152 host_send_keyboard_report();
154 register_code(action.key.code);
155 if (action.key.mods && action.key.code) {
156 host_set_mods(tmp_mods);
157 host_send_keyboard_report();
160 if (action.key.mods && !action.key.code) {
161 host_del_mods(action.key.mods);
162 host_send_keyboard_report();
164 unregister_code(action.key.code);
169 uint8_t tmp_mods = host_get_mods();
170 if (action.key.mods) {
171 host_add_mods(action.key.mods<<4);
172 host_send_keyboard_report();
174 register_code(action.key.code);
175 if (action.key.mods && action.key.code) {
176 host_set_mods(tmp_mods);
177 host_send_keyboard_report();
180 if (action.key.mods && !action.key.code) {
181 host_del_mods(action.key.mods<<4);
182 host_send_keyboard_report();
184 unregister_code(action.key.code);
192 /* other HID usage */
194 #ifdef EXTRAKEY_ENABLE
195 switch (action.usage.page) {
196 case ACTION_USAGE_PAGE_SYSTEM:
198 host_system_send(action.usage.code);
203 case ACTION_USAGE_PAGE_CONSUMER:
205 host_consumer_send(action.usage.code);
207 host_consumer_send(0);
216 #ifdef MOUSEKEY_ENABLE
218 mousekey_on(action.key.code);
221 mousekey_off(action.key.code);
228 case ACT_LAYER_PRESSED:
229 // layer action when pressed
230 switch (action.layer.code) {
233 layer_switch(action.layer.opt);
241 default_layer = action.layer.opt;
242 layer_switch(default_layer);
247 debug("tap: "); debug_hex(tap_count); debug("\n");
249 if (tap_count == 0) {
250 if (host_has_anykey()) {
251 register_code(action.layer.code);
253 delaying_layer = (keyrecord_t){
256 .mods = host_get_mods()
259 } else if (tap_count > 0) {
260 register_code(action.layer.code);
264 if (KEYEQ(event.key, delaying_layer.event.key) &&
265 timer_elapsed(delaying_layer.event.time) < TAP_TIME) {
266 uint8_t tmp_mods = host_get_mods();
267 host_set_mods(delaying_layer.mods);
268 register_code(delaying_layer.action.layer.code);
269 host_set_mods(tmp_mods);
270 unregister_code(delaying_layer.action.layer.code);
272 unregister_code(action.layer.code);
274 delaying_layer = (keyrecord_t){};
279 case ACT_LAYER_RELEASED:
280 switch (action.layer.code) {
283 layer_switch(action.layer.opt);
287 // Ignored. LAYER_RELEASED with tap toggle is invalid action.
290 if (!event.pressed) {
291 default_layer = action.layer.opt;
292 layer_switch(default_layer);
296 // Ignored. LAYER_RELEASED with tap key is invalid action.
301 switch (action.layer.code) {
304 layer_switch(current_layer | action.layer.opt);
306 layer_switch(current_layer & ~action.layer.opt);
313 // change default layer
315 default_layer = current_layer | action.layer.opt;
316 layer_switch(default_layer);
318 default_layer = current_layer & ~action.layer.opt;
319 layer_switch(default_layer);
324 debug("tap: "); debug_hex(tap_count); debug("\n");
326 if (tap_count == 0) {
327 if (host_has_anykey()) {
328 register_code(action.layer.code);
330 delaying_layer = (keyrecord_t){
333 .mods = keyboard_report->mods
336 } else if (tap_count > 0) {
337 register_code(action.layer.code);
340 if (tap_count == 0) {
342 layer_switch(current_layer & ~action.layer.opt);
343 } else if (tap_count == 1) {
345 register_code(action.layer.code);
347 unregister_code(action.layer.code);
352 switch (action.layer.opt) {
354 // set default layer when pressed
355 switch (action.layer.code) {
358 layer_switch(default_layer);
366 default_layer = current_layer;
367 layer_switch(default_layer);
376 // set default layer when released
377 switch (action.layer.code) {
379 if (!event.pressed) {
380 layer_switch(default_layer);
384 if (!event.pressed) {
385 default_layer = current_layer;
386 layer_switch(default_layer);
392 if (!event.pressed) {
393 layer_switch(default_layer);
410 void action_exec(keyevent_t event)
412 /* count tap when key is up */
413 if (KEYEQ(event.key, last_event.key) && timer_elapsed(last_event_time) < TAP_TIME) {
414 if (!event.pressed) tap_count++;
419 /* When delaying layer switch */
420 if (delaying_layer.action.code) {
421 /* Layer switch when delay time elapses or waiting key is released */
422 if ((timer_elapsed(delaying_layer.event.time) > LAYER_DELAY) ||
423 (!event.pressed && KEYEQ(event.key, waiting_key.event.key))) {
425 switch (delaying_layer.action.kind.id) {
426 case ACT_LAYER_PRESSED:
427 layer_switch(delaying_layer.action.layer.opt);
430 layer_switch(current_layer | delaying_layer.action.layer.opt);
433 delaying_layer = (keyrecord_t){};
435 /* Process waiting keys in new layer */
436 if (waiting_key.event.time) {
437 uint8_t tmp_mods = host_get_mods();
438 host_set_mods(waiting_key.mods);
439 process(waiting_key.event, keymap_get_action(current_layer,
440 waiting_key.event.key.row,
441 waiting_key.event.key.col));
442 host_set_mods(tmp_mods);
443 waiting_key = (keyrecord_t){};
446 /* when delaying layer key is released within delay term */
447 else if (!event.pressed && KEYEQ(event.key, delaying_layer.event.key)) {
449 uint8_t tmp_mods = host_get_mods();
450 host_set_mods(delaying_layer.mods);
451 register_code(delaying_layer.action.layer.code);
452 delaying_layer = (keyrecord_t){};
454 /* process waiting keys */
455 if (waiting_key.event.time) {
456 host_set_mods(waiting_key.mods);
457 process(waiting_key.event, waiting_key.action);
458 waiting_key = (keyrecord_t){};
460 host_set_mods(tmp_mods);
464 action_t action = keymap_get_action(current_layer, event.key.row, event.key.col);
466 /* postpone key-down events while delaying layer */
467 if (delaying_layer.action.code) {
469 // TODO: waiting_keys[]
470 waiting_key = (keyrecord_t){
473 .mods = host_get_mods()
476 process(event, action);
479 process(event, action);
484 last_event_time = timer_read();
488 static void register_code(uint8_t code)
493 else if IS_KEY(code) {
494 // TODO: should push command_proc out of this block?
495 if (!command_proc(code)) {
497 host_send_keyboard_report();
500 else if IS_MOD(code) {
501 host_add_mods(MOD_BIT(code));
502 host_send_keyboard_report();
506 static void unregister_code(uint8_t code)
510 host_send_keyboard_report();
512 else if IS_MOD(code) {
513 host_del_mods(MOD_BIT(code));
514 host_send_keyboard_report();
518 static void register_mods(uint8_t mods)
522 host_send_keyboard_report();
525 static void unregister_mods(uint8_t mods)
529 host_send_keyboard_report();
532 static void clear_keyboard(void)
535 clear_keyboard_but_mods();
538 static void clear_keyboard_but_mods(void)
541 host_send_keyboard_report();
542 #ifdef MOUSEKEY_ENABLE
546 #ifdef EXTRAKEY_ENABLE
548 host_consumer_send(0);
552 static void layer_switch(uint8_t new_layer)
554 if (current_layer != new_layer) {
555 Kdebug("Layer Switch: "); Kdebug_hex(current_layer);
556 Kdebug(" -> "); Kdebug_hex(new_layer); Kdebug("\n");
558 current_layer = new_layer;
559 clear_keyboard_but_mods(); // To avoid stuck keys
560 // TODO: update mods with full scan of matrix? if modifier changes between layers