-/*
- *
- * Event/State|IDLE DELAYING[f] WAITING[f,k] PRESSING
- * -----------+------------------------------------------------------------------
- * Fn Down |IDLE(L+) WAITING(Sk) WAITING(Sk) -
- * Up |IDLE(L-) IDLE(L-) IDLE(L-) IDLE(L-)
- * Fnk Down |DELAYING(Sf) WAITING(Sk) WAINTING(Sk) PRESSING(Rf)
- * Up |IDLE(L-) IDLE(Rf,Uf) IDLE(Rf,Ps,Uf)*3 PRESSING(Uf)
- * Key Down |PRESSING(Rk) WAITING(Sk) WAITING(Sk) PRESSING(Rk)
- * Up |IDLE(Uk) DELAYING(Uk) IDLE(L+,Ps,Uk) IDLE(Uk)*4
- * Delay |- IDLE(L+) IDLE(L+,Ps) -
- * |
- * No key Down|IDLE(Ld) IDLE(Ld) IDLE(Ld) IDLE(Ld)
- *
- * *2: register Fnk if any key is pressing
- * *3: when Fnk == Stored Fnk, if not ignore.
- * *4: when no registered key any more
- *
- * States:
- * IDLE:
- * DELAYING: delay layer switch after pressing Fn with alt keycode
- * WAITING: key is pressed during DELAYING
- *
- * Events:
- * Fn: Fn key without alternative keycode
- * Fnk: Fn key with alternative keycode
- * -: ignore
- *
- * Actions:
- * Rk: register key
- * Uk: unregister key
- * Rf: register stored Fn(alt keycode)
- * Uf: unregister stored Fn(alt keycode)
- * Rs: register stored key
- * Us: unregister stored key
- * Sk: store key
- * Sf: store Fn
- * Ps: play stored key(Interpret stored key and transit state)
- * L+: Switch to new layer(*unregister* all keys but modifiers)
- * L-: Switch back to last layer(*unregister* all keys but modifiers)
- * Ld: Switch back to default layer(*unregister* all keys but modifiers)
- */
-#define NEXT(state) do { \
- debug("NEXT: "); print_P(state_str(kbdstate)); \
- kbdstate = state; \
- debug(" -> "); print_P(state_str(kbdstate)); debug("\n"); \
-} while (0)
-
-static inline void process_key(keyevent_t event)
-{
- uint8_t code = keymap_get_keycode(current_layer, event.key.row, event.key.col);
- keykind_t kind = get_keykind(code, event.pressed);
-
- uint8_t tmp_mods;
-
- debug("state: "); print_P(state_str(kbdstate));
- debug(" kind: "); debug_hex(kind);
- debug(" code: "); debug_hex(code);
- if (event.pressed) { debug("d"); } else { debug("u"); }
- debug("\n");
-
- switch (kbdstate) {
- case IDLE:
- switch (kind) {
- case FN_DOWN:
- layer_switch_on(code);
- break;
- case FN_UP:
- layer_switch_off(code);
- break;
- case FNK_DOWN:
- // repeat Fn alt key when press Fn key down, up then down again quickly
- if (KEYEQ(delayed_fn.event.key, event.key) &&
- timer_elapsed(delayed_fn.time) < LAYER_DELAY) {
- register_code(keymap_fn_keycode(FN_INDEX(code)));
- NEXT(PRESSING);
- } else {
- delayed_fn = (keyrecord_t) {
- .event = event,
- .code = code,
- .mods = keyboard_report->mods,
- .time = timer_read()
- };
- NEXT(DELAYING);
- }
- break;
- case FNK_UP:
- layer_switch_off(code);
- break;
- case KEY_DOWN:
- register_code(code);
- NEXT(PRESSING);
- break;
- case MOD_DOWN:
- register_code(code);
- break;
- case KEY_UP:
- case MOD_UP:
- unregister_code(code);
- break;
- default:
- break;
- }
- break;
- case PRESSING:
- switch (kind) {
- case FN_DOWN:
- // ignored when any key is pressed
- break;
- case FN_UP:
- layer_switch_off(code);
- NEXT(IDLE);
- break;
- case FNK_DOWN:
- register_code(keymap_fn_keycode(FN_INDEX(code)));
- break;
- case FNK_UP:
- // can't know whether layer switched or not
- layer_switch_off(code);
- unregister_code(keymap_fn_keycode(FN_INDEX(code)));
- break;
- case KEY_DOWN:
- case MOD_DOWN:
- register_code(code);
- break;
- case KEY_UP:
- case MOD_UP:
- unregister_code(code);
- // TODO: no key registered? mousekey, mediakey, systemkey
- if (!host_has_anykey())
- NEXT(IDLE);
- break;
- default:
- break;
- }
- break;
- case DELAYING:
- switch (kind) {
- case FN_DOWN:
- case FNK_DOWN:
- case KEY_DOWN:
- waiting_key = (keyrecord_t) {
- .event = event,
- .code = code,
- .mods = keyboard_report->mods,
- .time = timer_read()
- };
- NEXT(WAITING);
- break;
- case MOD_DOWN:
- register_code(code);
- break;
- case FN_UP:
- layer_switch_off(code);
- NEXT(IDLE);
- break;
- case FNK_UP:
- if (code == delayed_fn.code) {
- // type Fn with alt keycode
- // restore the mod status at the time of pressing Fn key
- tmp_mods = keyboard_report->mods;
- host_set_mods(delayed_fn.mods);
- register_code(keymap_fn_keycode(FN_INDEX(delayed_fn.code)));
- unregister_code(keymap_fn_keycode(FN_INDEX(delayed_fn.code)));
- host_set_mods(tmp_mods);
- NEXT(IDLE);
- } else {
- layer_switch_off(code);
- NEXT(IDLE);
- }
- break;
- case KEY_UP:
- unregister_code(code);
- NEXT(IDLE);
- break;
- case MOD_UP:
- unregister_code(code);
- break;
- default:
- break;
- }
- break;
- case WAITING:
- switch (kind) {
- case FN_DOWN:
- case FNK_DOWN:
- case KEY_DOWN:
- tmp_mods = keyboard_report->mods;
- host_set_mods(delayed_fn.mods);
- register_code(keymap_fn_keycode(FN_INDEX(delayed_fn.code)));
- host_set_mods(waiting_key.mods);
- register_code(waiting_key.code);
- host_set_mods(tmp_mods);
- register_code(code);
- NEXT(IDLE);
- break;
- case MOD_DOWN:
- register_code(code);
- break;
- case FN_UP:
- layer_switch_off(code);
- NEXT(IDLE);
- break;
- case FNK_UP:
- if (code == delayed_fn.code) {
- // alt down, key down, alt up
- tmp_mods = keyboard_report->mods;
- host_set_mods(delayed_fn.mods);
- register_code(keymap_fn_keycode(FN_INDEX(delayed_fn.code)));
- host_set_mods(waiting_key.mods);
- register_code(waiting_key.code);
- unregister_code(keymap_fn_keycode(FN_INDEX(delayed_fn.code)));
- host_set_mods(tmp_mods);
- NEXT(IDLE);
- } else {
- layer_switch_off(code);
- NEXT(IDLE);
- }
- break;
- case KEY_UP:
- if (code == waiting_key.code) {
- layer_switch_on(delayed_fn.code);
- NEXT(IDLE);
- // process waiting_key
- tmp_mods = keyboard_report->mods;
- host_set_mods(waiting_key.mods);
- process_key(waiting_key.event);
- host_set_mods(tmp_mods);
- process_key(event);
- } else {
- unregister_code(code);
- }
- break;
- case MOD_UP:
- unregister_code(code);
- break;
- default:
- break;
- }
- break;
- }
-}