2 Copyright 2011 Jun Wako <wakojun@gmail.com>
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 #ifdef MOUSEKEY_ENABLE
33 #define Kdebug(s) do { if (debug_keyboard) debug(s); } while(0)
34 #define Kdebug_P(s) do { if (debug_keyboard) debug_P(s); } while(0)
35 #define Kdebug_hex(s) do { if (debug_keyboard) debug_hex(s); } while(0)
37 #define LAYER_DELAY 250
39 typedef enum keykind {
47 typedef enum { IDLE, DELAYING, WAITING, PRESSING } kbdstate_t;
50 #ifdef KEYMAP_DEFAULT_LAYER
51 uint8_t default_layer = KEYMAP_DEFAULT_LAYER;
52 uint8_t current_layer = KEYMAP_DEFAULT_LAYER;
54 uint8_t default_layer = 0;
55 uint8_t current_layer = 0;
58 /* keyboard internal states */
59 static kbdstate_t kbdstate = IDLE;
60 static uint8_t fn_state_bits = 0;
61 static keyrecord_t delayed_fn;
62 static keyrecord_t waiting_key;
65 static const char *state_str(kbdstate_t state)
67 if (state == IDLE) return PSTR("IDLE");
68 if (state == DELAYING) return PSTR("DELAYING");
69 if (state == WAITING) return PSTR("WAITING");
70 if (state == PRESSING) return PSTR("PRESSING");
71 return PSTR("UNKNOWN");
74 static inline keykind_t get_keykind(uint8_t code, bool pressed)
76 if IS_KEY(code) return (pressed ? KEY_DOWN : KEY_UP);
77 if IS_MOD(code) return (pressed ? MOD_DOWN : MOD_UP);
79 if (keymap_fn_keycode(FN_INDEX(code)))
80 return (pressed ? FNK_DOWN : FNK_UP);
82 return (pressed ? FN_DOWN : FN_UP);
84 if IS_MOUSEKEY(code) return (pressed ? KEY_DOWN : KEY_UP);
85 if IS_SYSTEM(code) return (pressed ? KEY_DOWN : KEY_UP);
86 if IS_CONSUMER(code) return (pressed ? KEY_DOWN : KEY_UP);
90 static void clear_keyboard(void)
94 host_send_keyboard_report();
97 host_consumer_send(0);
99 #ifdef MOUSEKEY_ENABLE
105 static void clear_keyboard_but_mods(void)
108 host_send_keyboard_report();
111 host_consumer_send(0);
113 #ifdef MOUSEKEY_ENABLE
119 static bool anykey_sent_to_host(void)
121 return (host_has_anykey() || host_mouse_in_use() ||
122 host_last_sysytem_report() || host_last_consumer_report());
125 static void layer_switch_on(uint8_t code)
127 if (!IS_FN(code)) return;
128 fn_state_bits |= FN_BIT(code);
129 uint8_t new_layer = (fn_state_bits ? keymap_fn_layer(biton(fn_state_bits)) : default_layer);
130 if (current_layer != new_layer) {
131 Kdebug("Layer Switch(on): "); Kdebug_hex(current_layer);
132 Kdebug(" -> "); Kdebug_hex(new_layer); Kdebug("\n");
134 clear_keyboard_but_mods();
135 current_layer = new_layer;
139 static bool layer_switch_off(uint8_t code)
141 if (!IS_FN(code)) return false;
142 fn_state_bits &= ~FN_BIT(code);
143 uint8_t new_layer = (fn_state_bits ? keymap_fn_layer(biton(fn_state_bits)) : default_layer);
144 if (current_layer != new_layer) {
145 Kdebug("Layer Switch(off): "); Kdebug_hex(current_layer);
146 Kdebug(" -> "); Kdebug_hex(new_layer); Kdebug("\n");
148 clear_keyboard_but_mods();
149 current_layer = new_layer;
155 static void register_code(uint8_t code)
158 if (!command_proc(code)) {
160 host_send_keyboard_report();
163 else if IS_MOD(code) {
164 host_add_mod_bit(MOD_BIT(code));
165 host_send_keyboard_report();
167 else if IS_FN(code) {
168 if (!command_proc(keymap_fn_keycode(FN_INDEX(code)))) {
169 host_add_key(keymap_fn_keycode(FN_INDEX(code)));
170 host_send_keyboard_report();
173 else if IS_MOUSEKEY(code) {
174 #ifdef MOUSEKEY_ENABLE
179 else if IS_CONSUMER(code) {
185 case KC_AUDIO_VOL_UP:
186 usage = AUDIO_VOL_UP;
188 case KC_AUDIO_VOL_DOWN:
189 usage = AUDIO_VOL_DOWN;
191 case KC_MEDIA_NEXT_TRACK:
192 usage = TRANSPORT_NEXT_TRACK;
194 case KC_MEDIA_PREV_TRACK:
195 usage = TRANSPORT_PREV_TRACK;
198 usage = TRANSPORT_STOP;
200 case KC_MEDIA_PLAY_PAUSE:
201 usage = TRANSPORT_PLAY_PAUSE;
203 case KC_MEDIA_SELECT:
204 usage = AL_CC_CONFIG;
210 usage = AL_CALCULATOR;
213 usage = AL_LOCAL_BROWSER;
233 case KC_WWW_FAVORITES:
234 usage = AC_BOOKMARKS;
237 host_consumer_send(usage);
239 else if IS_SYSTEM(code) {
242 case KC_SYSTEM_POWER:
243 usage = SYSTEM_POWER_DOWN;
245 case KC_SYSTEM_SLEEP:
246 usage = SYSTEM_SLEEP;
249 usage = SYSTEM_WAKE_UP;
252 host_system_send(usage);
257 static void unregister_code(uint8_t code)
261 host_send_keyboard_report();
263 else if IS_MOD(code) {
264 host_del_mod_bit(MOD_BIT(code));
265 host_send_keyboard_report();
267 else if IS_FN(code) {
268 host_del_key(keymap_fn_keycode(FN_INDEX(code)));
269 host_send_keyboard_report();
271 else if IS_MOUSEKEY(code) {
272 #ifdef MOUSEKEY_ENABLE
277 else if IS_CONSUMER(code) {
278 host_consumer_send(0x0000);
280 else if IS_SYSTEM(code) {
281 host_system_send(0x0000);
287 * Event/State|IDLE PRESSING DELAYING[f] WAITING[f,k]
288 * -----------+------------------------------------------------------------------
289 * Fn Down |(L+) -*1 WAITING(Sk) IDLE(Rf,Ps)*7
290 * Up |(L-) IDLE(L-)*8 IDLE(L-)*8 IDLE(L-)*8
291 * Fnk Down |DELAYING(Sf)* (Rf) WAITING(Sk) IDLE(Rf,Ps,Rf)
292 * Up |(L-) IDLE(L-/Uf)*8 IDLE(Rf,Uf/L-)*3 IDLE(Rf,Ps,Uf/L-)*3
293 * Key Down |PRESSING(Rk) (Rk) WAITING(Sk) IDLE(Rf,Ps,Rk)
294 * Up |(Uk) IDLE(Uk)*4 (Uk) IDLE(L+,Ps,Pk)/(Uk)*a
296 * Delay |- - IDLE(L+) IDLE(L+,Ps)
297 * Magic Key |COMMAND*5
299 * *1: ignore Fn if other key is down.
300 * *2: register Fnk if any key is pressing
301 * *3: register/unregister delayed Fnk and move to IDLE if code == delayed Fnk, else *8
302 * *4: if no keys registered to host
303 * *5: unregister all keys
304 * *6: only if no keys down
305 * *7: ignore Fn because Fnk key and stored key are down.
306 * *8: move to IDLE if layer switch(off) occurs, else stay at current state
307 * *9: repeat key if pressing Fnk twice quickly(move to PRESSING)
308 * *a: layer switch and process waiting key and code if code == wainting key, else unregister key
311 * IDLE: No key is down except modifiers
312 * DELAYING: delay layer switch after pressing Fn with alt keycode
313 * WAITING: key is pressed during DELAYING
316 * Fn: Fn key without alternative keycode
317 * Fnk: Fn key with alternative keycode
319 * Delay: layer switch delay term is elapsed
324 * Rf: register Fn(alt keycode)
325 * Uf: unregister Fn(alt keycode)
326 * Rs: register stored key
327 * Us: unregister stored key
328 * Sk: Store key(waiting Key)
329 * Sf: Store Fn(delayed Fn)
330 * Ps: Process stored key
332 * Is: Interpret stored keys in current layer
333 * L+: Switch to new layer(*unregister* all keys but modifiers)
334 * L-: Switch back to last layer(*unregister* all keys but modifiers)
335 * Ld: Switch back to default layer(*unregister* all keys but modifiers)
337 #define NEXT(state) do { \
338 Kdebug("NEXT: "); Kdebug_P(state_str(kbdstate)); \
340 Kdebug(" -> "); Kdebug_P(state_str(kbdstate)); Kdebug("\n"); \
343 static inline void process_key(keyevent_t event)
345 uint8_t code = keymap_get_keycode(current_layer, event.key.row, event.key.col);
346 keykind_t kind = get_keykind(code, event.pressed);
350 Kdebug("state: "); Kdebug_P(state_str(kbdstate));
351 Kdebug(" kind: "); Kdebug_hex(kind);
352 Kdebug(" code: "); Kdebug_hex(code);
353 if (event.pressed) { Kdebug("d"); } else { Kdebug("u"); }
360 layer_switch_on(code);
363 layer_switch_off(code);
366 // repeat Fn alt key when press Fn key down, up then down again quickly
367 if (KEYEQ(delayed_fn.event.key, event.key) &&
368 timer_elapsed(delayed_fn.time) < LAYER_DELAY) {
372 delayed_fn = (keyrecord_t) {
375 .mods = keyboard_report->mods,
382 layer_switch_off(code);
393 unregister_code(code);
402 // ignored when any key is pressed
405 if (layer_switch_off(code))
412 if (layer_switch_off(code)) {
415 unregister_code(code);
416 if (!anykey_sent_to_host())
426 unregister_code(code);
427 if (!anykey_sent_to_host())
439 waiting_key = (keyrecord_t) {
442 .mods = keyboard_report->mods,
451 if (layer_switch_off(code))
455 if (code == delayed_fn.code) {
456 // type Fn with alt keycode
457 // restore the mod status at the time of pressing Fn key
458 tmp_mods = keyboard_report->mods;
459 host_set_mods(delayed_fn.mods);
460 register_code(delayed_fn.code);
461 unregister_code(delayed_fn.code);
462 host_set_mods(tmp_mods);
465 if (layer_switch_off(code))
471 unregister_code(code);
482 tmp_mods = keyboard_report->mods;
483 host_set_mods(delayed_fn.mods);
484 register_code(delayed_fn.code);
485 host_set_mods(waiting_key.mods);
486 register_code(waiting_key.code);
487 host_set_mods(tmp_mods);
488 if (kind == FN_DOWN) {
490 } else if (kind == FNK_DOWN) {
492 } else if (kind == KEY_DOWN) {
501 if (layer_switch_off(code))
505 if (code == delayed_fn.code) {
506 // alt down, key down, alt up
507 tmp_mods = keyboard_report->mods;
508 host_set_mods(delayed_fn.mods);
509 register_code(delayed_fn.code);
510 host_set_mods(waiting_key.mods);
511 register_code(waiting_key.code);
512 unregister_code(delayed_fn.code);
513 host_set_mods(tmp_mods);
516 if (layer_switch_off(code))
521 if (code == waiting_key.code) {
522 layer_switch_on(delayed_fn.code);
524 // process waiting_key
525 tmp_mods = keyboard_report->mods;
526 host_set_mods(waiting_key.mods);
527 process_key(waiting_key.event);
528 host_set_mods(tmp_mods);
531 unregister_code(code);
535 unregister_code(code);
544 void keyboard_init(void)
546 debug_keyboard = true;
550 #ifdef PS2_MOUSE_ENABLE
555 void keyboard_task(void)
557 static matrix_row_t matrix_prev[MATRIX_ROWS];
558 static uint8_t led_status = 0;
559 matrix_row_t matrix_row = 0;
560 matrix_row_t matrix_change = 0;
563 for (int r = 0; r < MATRIX_ROWS; r++) {
564 matrix_row = matrix_get_row(r);
565 matrix_change = matrix_row ^ matrix_prev[r];
567 if (debug_matrix) matrix_print();
569 for (int c = 0; c < MATRIX_COLS; c++) {
570 if (matrix_change & (1<<c)) {
571 process_key((keyevent_t){
572 .key = (key_t){ .row = r, .col = c },
573 .pressed = (matrix_row & (1<<c))
575 // record a processed key
576 matrix_prev[r] ^= (1<<c);
577 // process a key per task call
578 goto MATRIX_LOOP_END;
585 // layer switch when delay term elapses
586 if (kbdstate == DELAYING || kbdstate == WAITING) {
587 if (timer_elapsed(delayed_fn.time) > LAYER_DELAY) {
588 if (kbdstate == DELAYING) {
589 layer_switch_on(delayed_fn.code);
592 if (kbdstate == WAITING) {
593 layer_switch_on(delayed_fn.code);
595 uint8_t tmp_mods = keyboard_report->mods;
596 host_set_mods(waiting_key.mods);
597 process_key(waiting_key.event);
598 host_set_mods(tmp_mods);
603 #ifdef MOUSEKEY_ENABLE
604 // mousekey repeat & acceleration
608 // FAIL SAFE: clear all key if no key down
610 matrix_row_t is_matrix_on = 0;
611 for (int r = 0; r < MATRIX_ROWS; r++) {
612 is_matrix_on |= matrix_get_row(r);
615 Kdebug("FAIL SAFE: clear all keys(default layer).\n");
617 current_layer = default_layer;
622 if (led_status != host_keyboard_leds()) {
623 led_status = host_keyboard_leds();
624 keyboard_set_leds(led_status);
630 void keyboard_set_leds(uint8_t leds)