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
= {};
119 void action_exec(keyevent_t event
)
121 /* count tap when key is up */
122 if (KEYEQ(event
.key
, last_event
.key
) && timer_elapsed(last_event_time
) < TAP_TIME
) {
123 if (!event
.pressed
) tap_count
++;
128 /* layer switch after LAYER_DELAY */
129 if (delaying_layer
.action
.code
&& timer_elapsed(delaying_layer
.event
.time
) > LAYER_DELAY
) {
130 switch (delaying_layer
.action
.kind
.id
) {
131 case ACT_LAYER_PRESSED
:
132 layer_switch(delaying_layer
.action
.layer
.opt
);
135 layer_switch(current_layer
| delaying_layer
.action
.layer
.opt
);
138 delaying_layer
= (keyrecord_t
){};
140 action_t action
= keymap_get_action(current_layer
, event
.key
.row
, event
.key
.col
);
142 debug("action: "); debug_hex16(action
.code
); debug("\n");
143 debug("kind.id: "); debug_hex(action
.kind
.id
); debug("\n");
144 debug("kind.param: "); debug_hex16(action
.kind
.param
); debug("\n");
145 debug("key.code: "); debug_hex(action
.key
.code
); debug("\n");
146 debug("key.mods: "); debug_hex(action
.key
.mods
); debug("\n");
148 switch (action
.kind
.id
) {
150 // normal key or key plus mods
152 register_mods(action
.key
.mods
);
153 register_code(action
.key
.code
);
155 unregister_code(action
.key
.code
);
156 unregister_mods(action
.key
.mods
);
161 register_mods(action
.key
.mods
<<4);
162 register_code(action
.key
.code
);
164 unregister_code(action
.key
.code
);
165 unregister_mods(action
.key
.mods
<<4);
173 #ifdef EXTRAKEY_ENABLE
174 switch (action
.usage
.page
) {
175 case ACTION_USAGE_PAGE_SYSTEM
:
177 host_system_send(action
.usage
.code
);
182 case ACTION_USAGE_PAGE_CONSUMER
:
184 host_consumer_send(action
.usage
.code
);
186 host_consumer_send(0);
193 #ifdef MOUSEKEY_ENABLE
195 mousekey_on(action
.key
.code
);
198 mousekey_off(action
.key
.code
);
203 case ACT_LAYER_PRESSED
:
204 // layer action when pressed
205 switch (action
.layer
.code
) {
208 layer_switch(action
.layer
.opt
);
216 default_layer
= action
.layer
.opt
;
217 layer_switch(default_layer
);
222 debug("tap: "); debug_hex(tap_count
); debug("\n");
224 if (tap_count
== 0) {
225 if (host_has_anykey()) {
226 register_code(action
.layer
.code
);
228 delaying_layer
= (keyrecord_t
){
231 .mods
= keyboard_report
->mods
234 } else if (tap_count
> 0) {
235 register_code(action
.layer
.code
);
238 // type key after tap
239 if (tap_count
== 1) {
240 delaying_layer
= (keyrecord_t
){};
241 register_code(action
.layer
.code
);
243 unregister_code(action
.layer
.code
);
248 case ACT_LAYER_RELEASED
:
249 switch (action
.layer
.code
) {
252 layer_switch(action
.layer
.opt
);
256 // Ignored. LAYER_RELEASED with tap toggle is invalid action.
259 if (!event
.pressed
) {
260 default_layer
= action
.layer
.opt
;
261 layer_switch(default_layer
);
265 // Ignored. LAYER_RELEASED with tap key is invalid action.
270 switch (action
.layer
.code
) {
273 layer_switch(current_layer
| action
.layer
.opt
);
275 layer_switch(current_layer
& ~action
.layer
.opt
);
282 // change default layer
284 default_layer
= current_layer
| action
.layer
.opt
;
285 layer_switch(default_layer
);
287 default_layer
= current_layer
& ~action
.layer
.opt
;
288 layer_switch(default_layer
);
293 debug("tap: "); debug_hex(tap_count
); debug("\n");
295 if (tap_count
== 0) {
296 if (host_has_anykey()) {
297 register_code(action
.layer
.code
);
299 delaying_layer
= (keyrecord_t
){
302 .mods
= keyboard_report
->mods
305 } else if (tap_count
> 0) {
306 register_code(action
.layer
.code
);
309 if (tap_count
== 0) {
311 layer_switch(current_layer
& ~action
.layer
.opt
);
312 } else if (tap_count
== 1) {
314 register_code(action
.layer
.code
);
316 unregister_code(action
.layer
.code
);
321 switch (action
.layer
.opt
) {
323 // set default layer when pressed
324 switch (action
.layer
.code
) {
327 layer_switch(default_layer
);
335 default_layer
= current_layer
;
336 layer_switch(default_layer
);
345 // set default layer when released
346 switch (action
.layer
.code
) {
348 if (!event
.pressed
) {
349 layer_switch(default_layer
);
353 if (!event
.pressed
) {
354 default_layer
= current_layer
;
355 layer_switch(default_layer
);
361 if (!event
.pressed
) {
362 layer_switch(default_layer
);
378 last_event_time
= timer_read();
382 static void register_code(uint8_t code
)
387 else if IS_KEY(code
) {
388 // TODO: should push command_proc out of this block?
389 if (!command_proc(code
)) {
391 host_send_keyboard_report();
394 else if IS_MOD(code
) {
395 host_add_mods(MOD_BIT(code
));
396 host_send_keyboard_report();
400 static void unregister_code(uint8_t code
)
404 host_send_keyboard_report();
406 else if IS_MOD(code
) {
407 host_del_mods(MOD_BIT(code
));
408 host_send_keyboard_report();
412 static void register_mods(uint8_t mods
)
416 host_send_keyboard_report();
419 static void unregister_mods(uint8_t mods
)
423 host_send_keyboard_report();
426 static void clear_keyboard(void)
429 clear_keyboard_but_mods();
432 static void clear_keyboard_but_mods(void)
435 host_send_keyboard_report();
436 #ifdef MOUSEKEY_ENABLE
440 #ifdef EXTRAKEY_ENABLE
442 host_consumer_send(0);
446 static void layer_switch(uint8_t new_layer
)
448 if (current_layer
!= new_layer
) {
449 Kdebug("Layer Switch: "); Kdebug_hex(current_layer
);
450 Kdebug(" -> "); Kdebug_hex(new_layer
); Kdebug("\n");
452 current_layer
= new_layer
;
453 clear_keyboard_but_mods(); // To avoid stuck keys