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
31 #ifdef EXTRAKEY_ENABLE
32 #include <util/delay.h>
36 #define LAYER_DELAY 250
38 typedef enum keykind {
46 typedef enum { IDLE, DELAYING, WAITING, PRESSING } kbdstate_t;
49 uint8_t current_layer = 0;
50 uint8_t default_layer = 0;
52 /* keyboard internal states */
53 static kbdstate_t kbdstate = IDLE;
54 static uint8_t fn_state_bits = 0;
55 static keyrecord_t delayed_fn;
56 static keyrecord_t waiting_key;
59 static const char *state_str(kbdstate_t state)
61 if (state == IDLE) return PSTR("IDLE");
62 if (state == DELAYING) return PSTR("DELAYING");
63 if (state == WAITING) return PSTR("WAITING");
64 if (state == PRESSING) return PSTR("PRESSING");
65 return PSTR("UNKNOWN");
68 static inline keykind_t get_keykind(uint8_t code, bool pressed)
70 if IS_KEY(code) return (pressed ? KEY_DOWN : KEY_UP);
71 if IS_MOD(code) return (pressed ? MOD_DOWN : MOD_UP);
73 if (keymap_fn_keycode(FN_INDEX(code)))
74 return (pressed ? FNK_DOWN : FNK_UP);
76 return (pressed ? FN_DOWN : FN_UP);
78 if IS_MOUSEKEY(code) return (pressed ? KEY_DOWN : KEY_UP);
79 if IS_SYSTEM(code) return (pressed ? KEY_DOWN : KEY_UP);
80 if IS_CONSUMER(code) return (pressed ? KEY_DOWN : KEY_UP);
84 static void clear_keyboard(void)
88 host_send_keyboard_report();
91 host_consumer_send(0);
93 #ifdef MOUSEKEY_ENABLE
99 static void clear_keyboard_but_mods(void)
102 host_send_keyboard_report();
105 host_consumer_send(0);
107 #ifdef MOUSEKEY_ENABLE
113 static bool anykey_sent_to_host(void)
115 return (host_has_anykey() || host_mouse_in_use() ||
116 host_last_sysytem_report() || host_last_consumer_report());
119 static void layer_switch_on(uint8_t code)
121 if (!IS_FN(code)) return;
122 fn_state_bits |= FN_BIT(code);
123 if (current_layer != keymap_fn_layer(FN_INDEX(code))) {
124 clear_keyboard_but_mods();
126 debug("Layer Switch(on): "); debug_hex(current_layer);
127 current_layer = keymap_fn_layer(FN_INDEX(code));
128 debug(" -> "); debug_hex(current_layer); debug("\n");
132 static bool layer_switch_off(uint8_t code)
134 if (!IS_FN(code)) return false;
135 fn_state_bits &= ~FN_BIT(code);
136 if (current_layer != keymap_fn_layer(biton(fn_state_bits))) {
137 clear_keyboard_but_mods();
139 debug("Layer Switch(off): "); debug_hex(current_layer);
140 current_layer = keymap_fn_layer(biton(fn_state_bits));
141 debug(" -> "); debug_hex(current_layer); debug("\n");
147 static void register_code(uint8_t code)
151 host_send_keyboard_report();
153 else if IS_MOD(code) {
154 host_add_mod_bit(MOD_BIT(code));
155 host_send_keyboard_report();
157 else if IS_FN(code) {
158 host_add_key(keymap_fn_keycode(FN_INDEX(code)));
159 host_send_keyboard_report();
161 else if IS_MOUSEKEY(code) {
162 #ifdef MOUSEKEY_ENABLE
167 else if IS_CONSUMER(code) {
173 case KC_AUDIO_VOL_UP:
174 usage = AUDIO_VOL_UP;
176 case KC_AUDIO_VOL_DOWN:
177 usage = AUDIO_VOL_DOWN;
179 case KC_MEDIA_NEXT_TRACK:
180 usage = TRANSPORT_NEXT_TRACK;
182 case KC_MEDIA_PREV_TRACK:
183 usage = TRANSPORT_PREV_TRACK;
186 usage = TRANSPORT_STOP;
188 case KC_MEDIA_PLAY_PAUSE:
189 usage = TRANSPORT_PLAY_PAUSE;
191 case KC_MEDIA_SELECT:
192 usage = AL_CC_CONFIG;
198 usage = AL_CALCULATOR;
201 usage = AL_LOCAL_BROWSER;
221 case KC_WWW_FAVORITES:
222 usage = AC_BOOKMARKS;
225 host_consumer_send(usage);
227 else if IS_SYSTEM(code) {
230 case KC_SYSTEM_POWER:
231 usage = SYSTEM_POWER_DOWN;
233 case KC_SYSTEM_SLEEP:
234 usage = SYSTEM_SLEEP;
237 usage = SYSTEM_WAKE_UP;
240 host_system_send(usage);
245 static void unregister_code(uint8_t code)
249 host_send_keyboard_report();
251 else if IS_MOD(code) {
252 host_del_mod_bit(MOD_BIT(code));
253 host_send_keyboard_report();
255 else if IS_FN(code) {
256 host_del_key(keymap_fn_keycode(FN_INDEX(code)));
257 host_send_keyboard_report();
259 else if IS_MOUSEKEY(code) {
260 #ifdef MOUSEKEY_ENABLE
265 else if IS_CONSUMER(code) {
266 host_consumer_send(0x0000);
268 else if IS_SYSTEM(code) {
269 host_system_send(0x0000);
275 * Event/State|IDLE PRESSING DELAYING[f] WAITING[f,k]
276 * -----------+------------------------------------------------------------------
277 * Fn Down |(L+) -*1 WAITING(Sk) IDLE(Rf,Ps)*7
278 * Up |(L-) IDLE(L-)*8 IDLE(L-)*8 IDLE(L-)*8
279 * Fnk Down |DELAYING(Sf)* (Rf) WAITING(Sk) IDLE(Rf,Ps,Rf)
280 * Up |(L-) IDLE(L-/Uf)*8 IDLE(Rf,Uf/L-)*3 IDLE(Rf,Ps,Uf/L-)*3
281 * Key Down |PRESSING(Rk) (Rk) WAITING(Sk) IDLE(Rf,Ps,Rk)
282 * Up |(Uk) IDLE(Uk)*4 (Uk) IDLE(L+,Ps,Pk)/(Uk)*a
284 * Delay |- - IDLE(L+) IDLE(L+,Ps)
285 * Magic Key |COMMAND*5
287 * *1: ignore Fn if other key is down.
288 * *2: register Fnk if any key is pressing
289 * *3: register/unregister delayed Fnk and move to IDLE if code == delayed Fnk, else *8
290 * *4: if no keys registered to host
291 * *5: unregister all keys
292 * *6: only if no keys down
293 * *7: ignore Fn because Fnk key and stored key are down.
294 * *8: move to IDLE if layer switch(off) occurs, else stay at current state
295 * *9: repeat key if pressing Fnk twice quickly(move to PRESSING)
296 * *a: layer switch and process waiting key and code if code == wainting key, else unregister key
299 * IDLE: No key is down except modifiers
300 * DELAYING: delay layer switch after pressing Fn with alt keycode
301 * WAITING: key is pressed during DELAYING
304 * Fn: Fn key without alternative keycode
305 * Fnk: Fn key with alternative keycode
307 * Delay: layer switch delay term is elapsed
312 * Rf: register Fn(alt keycode)
313 * Uf: unregister Fn(alt keycode)
314 * Rs: register stored key
315 * Us: unregister stored key
316 * Sk: Store key(waiting Key)
317 * Sf: Store Fn(delayed Fn)
318 * Ps: Process stored key
320 * Is: Interpret stored keys in current layer
321 * L+: Switch to new layer(*unregister* all keys but modifiers)
322 * L-: Switch back to last layer(*unregister* all keys but modifiers)
323 * Ld: Switch back to default layer(*unregister* all keys but modifiers)
325 #define NEXT(state) do { \
326 debug("NEXT: "); print_P(state_str(kbdstate)); \
328 debug(" -> "); print_P(state_str(kbdstate)); debug("\n"); \
331 static inline void process_key(keyevent_t event)
333 uint8_t code = keymap_get_keycode(current_layer, event.key.row, event.key.col);
334 keykind_t kind = get_keykind(code, event.pressed);
338 debug("state: "); print_P(state_str(kbdstate));
339 debug(" kind: "); debug_hex(kind);
340 debug(" code: "); debug_hex(code);
341 if (event.pressed) { debug("d"); } else { debug("u"); }
348 layer_switch_on(code);
351 layer_switch_off(code);
354 // repeat Fn alt key when press Fn key down, up then down again quickly
355 if (KEYEQ(delayed_fn.event.key, event.key) &&
356 timer_elapsed(delayed_fn.time) < LAYER_DELAY) {
360 delayed_fn = (keyrecord_t) {
363 .mods = keyboard_report->mods,
370 layer_switch_off(code);
381 unregister_code(code);
390 // ignored when any key is pressed
393 if (layer_switch_off(code))
400 if (layer_switch_off(code)) {
403 unregister_code(code);
404 if (!anykey_sent_to_host())
414 unregister_code(code);
415 if (!anykey_sent_to_host())
427 waiting_key = (keyrecord_t) {
430 .mods = keyboard_report->mods,
439 if (layer_switch_off(code))
443 if (code == delayed_fn.code) {
444 // type Fn with alt keycode
445 // restore the mod status at the time of pressing Fn key
446 tmp_mods = keyboard_report->mods;
447 host_set_mods(delayed_fn.mods);
448 register_code(delayed_fn.code);
449 unregister_code(delayed_fn.code);
450 host_set_mods(tmp_mods);
453 if (layer_switch_off(code))
459 unregister_code(code);
470 tmp_mods = keyboard_report->mods;
471 host_set_mods(delayed_fn.mods);
472 register_code(delayed_fn.code);
473 host_set_mods(waiting_key.mods);
474 register_code(waiting_key.code);
475 host_set_mods(tmp_mods);
476 if (kind == FN_DOWN) {
478 } else if (kind == FNK_DOWN) {
480 } else if (kind == KEY_DOWN) {
489 if (layer_switch_off(code))
493 if (code == delayed_fn.code) {
494 // alt down, key down, alt up
495 tmp_mods = keyboard_report->mods;
496 host_set_mods(delayed_fn.mods);
497 register_code(delayed_fn.code);
498 host_set_mods(waiting_key.mods);
499 register_code(waiting_key.code);
500 unregister_code(delayed_fn.code);
501 host_set_mods(tmp_mods);
504 if (layer_switch_off(code))
509 if (code == waiting_key.code) {
510 layer_switch_on(delayed_fn.code);
512 // process waiting_key
513 tmp_mods = keyboard_report->mods;
514 host_set_mods(waiting_key.mods);
515 process_key(waiting_key.event);
516 host_set_mods(tmp_mods);
519 unregister_code(code);
523 unregister_code(code);
532 void keyboard_init(void)
534 debug_keyboard = true;
538 #ifdef PS2_MOUSE_ENABLE
543 void keyboard_task(void)
545 static matrix_row_t matrix_prev[MATRIX_ROWS];
546 matrix_row_t matrix_row = 0;
547 matrix_row_t matrix_change = 0;
550 if (command_proc()) {
552 // TODO: COMMAND state?
557 for (int r = 0; r < MATRIX_ROWS; r++) {
558 matrix_row = matrix_get_row(r);
559 matrix_change = matrix_row ^ matrix_prev[r];
561 if (debug_matrix) matrix_print();
563 for (int c = 0; c < MATRIX_COLS; c++) {
564 if (matrix_change & (1<<c)) {
565 process_key((keyevent_t){
566 .key = (key_t){ .row = r, .col = c },
567 .pressed = (matrix_row & (1<<c))
569 // record a processed key
570 matrix_prev[r] ^= (1<<c);
571 // process a key per task call
572 goto MATRIX_LOOP_END;
579 // layer switch when delay term elapses
580 if (kbdstate == DELAYING || kbdstate == WAITING) {
581 if (timer_elapsed(delayed_fn.time) > LAYER_DELAY) {
582 if (kbdstate == DELAYING) {
583 layer_switch_on(delayed_fn.code);
586 if (kbdstate == WAITING) {
587 layer_switch_on(delayed_fn.code);
589 uint8_t tmp_mods = keyboard_report->mods;
590 host_set_mods(waiting_key.mods);
591 process_key(waiting_key.event);
592 host_set_mods(tmp_mods);
597 #ifdef MOUSEKEY_ENABLE
598 // mousekey repeat & acceleration
602 // FAIL SAFE: clear all key if no key down
604 matrix_row_t is_matrix_on = 0;
605 for (int r = 0; r < MATRIX_ROWS; r++) {
606 is_matrix_on |= matrix_get_row(r);
609 debug("FAIL SAFE: clear all keys.\n");
617 void keyboard_set_leds(uint8_t leds)