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/>.
22 #include "usb_keycodes.h"
28 #ifdef MOUSEKEY_ENABLE
31 #ifdef EXTRAKEY_ENABLE
32 #include <util/delay.h>
36 #define LAYER_DELAY 250
38 typedef enum keykind {
44 MOUSEKEY_DOWN, MOUSEKEY_UP,
48 typedef enum { IDLE, DELAYING, WAITING, PRESSING } kbdstate_t;
51 uint8_t current_layer = 0;
52 uint8_t default_layer = 0;
54 /* keyboard internal states */
55 static kbdstate_t kbdstate = IDLE;
56 static uint8_t fn_state_bits = 0;
57 static keyrecord_t delayed_fn;
58 static keyrecord_t waiting_key;
61 static const char *state_str(kbdstate_t state)
63 if (state == IDLE) return PSTR("IDLE");
64 if (state == DELAYING) return PSTR("DELAYING");
65 if (state == WAITING) return PSTR("WAITING");
66 if (state == PRESSING) return PSTR("PRESSING");
67 return PSTR("UNKNOWN");
70 static inline keykind_t get_keykind(uint8_t code, bool pressed)
72 if IS_KEY(code) return (pressed ? KEY_DOWN : KEY_UP);
73 if IS_MOD(code) return (pressed ? MOD_DOWN : MOD_UP);
75 if (keymap_fn_keycode(FN_INDEX(code)))
76 return (pressed ? FNK_DOWN : FNK_UP);
78 return (pressed ? FN_DOWN : FN_UP);
80 if IS_MOUSEKEY(code) return (pressed ? MOUSEKEY_DOWN : MOUSEKEY_UP);
84 static void layer_switch_on(uint8_t code)
86 if (!IS_FN(code)) return;
87 fn_state_bits |= FN_BIT(code);
88 if (current_layer != keymap_fn_layer(FN_INDEX(code))) {
89 //TODO: clear all key execpt Mod key
90 debug("Layer Switch(on): "); debug_hex(current_layer);
91 current_layer = keymap_fn_layer(FN_INDEX(code));
92 debug(" -> "); debug_hex(current_layer); debug("\n");
96 static void layer_switch_off(uint8_t code)
98 if (!IS_FN(code)) return;
99 fn_state_bits &= ~FN_BIT(code);
100 if (current_layer != keymap_fn_layer(biton(fn_state_bits))) {
101 //TODO: clear all key execpt Mod key
102 debug("Layer Switch(off): "); debug_hex(current_layer);
103 current_layer = keymap_fn_layer(biton(fn_state_bits));
104 debug(" -> "); debug_hex(current_layer); debug("\n");
108 static inline uint8_t get_keycode(key_t key)
110 return keymap_get_keycode(current_layer, key.row, key.col);
113 // whether any key except modifier is down or not
114 static inline bool is_anykey_down(void)
116 for (int r = 0; r < MATRIX_ROWS; r++) {
117 matrix_row_t matrix_row = matrix_get_row(r);
118 for (int c = 0; c < MATRIX_COLS; c++) {
119 if (matrix_row && (1<<c)) {
120 if (IS_KEY(get_keycode((key_t){ .row = r, .col = c }))) {
129 static void register_code(uint8_t code)
133 host_send_keyboard_report();
135 else if IS_MOD(code) {
136 host_add_mod_bit(MOD_BIT(code));
137 host_send_keyboard_report();
139 else if IS_MOUSEKEY(code) {
145 static void unregister_code(uint8_t code)
149 host_send_keyboard_report();
151 else if IS_MOD(code) {
152 host_del_mod_bit(MOD_BIT(code));
153 host_send_keyboard_report();
155 else if IS_MOUSEKEY(code) {
163 * Event/State|IDLE DELAYING[f] WAITING[f,k] PRESSING
164 * -----------+------------------------------------------------------------------
165 * Fn Down |IDLE(L+) WAITING(Sk) WAITING(Sk) -
166 * Up |IDLE(L-) IDLE(L-) IDLE(L-) IDLE(L-)
167 * Fnk Down |DELAYING(Sf) WAITING(Sk) WAINTING(Sk) PRESSING(Rf)
168 * Up |IDLE(L-) IDLE(Rf,Uf) IDLE(Rf,Ps,Uf)*3 PRESSING(Uf)
169 * Key Down |PRESSING(Rk) WAITING(Sk) WAITING(Sk) PRESSING(Rk)
170 * Up |IDLE(Uk) DELAYING(Uk) IDLE(L+,Ps,Uk) IDLE(Uk)*4
171 * Delay |- IDLE(L+) IDLE(L+,Ps) -
173 * No key Down|IDLE(Ld) IDLE(Ld) IDLE(Ld) IDLE(Ld)
175 * *2: register Fnk if any key is pressing
176 * *3: when Fnk == Stored Fnk, if not ignore.
177 * *4: when no registered key any more
181 * DELAYING: delay layer switch after pressing Fn with alt keycode
182 * WAITING: key is pressed during DELAYING
185 * Fn: Fn key without alternative keycode
186 * Fnk: Fn key with alternative keycode
192 * Rf: register stored Fn(alt keycode)
193 * Uf: unregister stored Fn(alt keycode)
194 * Rs: register stored key
195 * Us: unregister stored key
198 * Ps: play stored key(Interpret stored key and transit state)
199 * L+: Switch to new layer(*retain* Modifiers only)
200 * L-: Switch back to last layer(*clear* stored key/Fn, *unregister* all Modifier/key)
201 * Ld: Switch back to default layer(*clear* stored key/Fn, *unregister* all Modifier/key)
203 #define NEXT(state) do { \
204 debug("NEXT: "); print_P(state_str(kbdstate)); \
206 debug(" -> "); print_P(state_str(kbdstate)); debug("\n"); \
209 static inline void process_key(keyevent_t event)
213 static keyrecord_t waiting_keys[5];
214 static uint8_t waiting_keys_head = 0;
215 static uint8_t waiting_keys_tail = 0;
218 uint8_t code = get_keycode(event.key);
219 keykind_t kind = get_keykind(code, event.pressed);
223 //debug("kbdstate: "); debug_hex(kbdstate);
224 debug("state: "); print_P(state_str(kbdstate));
225 debug(" kind: "); debug_hex(kind);
226 debug(" code: "); debug_hex(code);
227 if (event.pressed) { debug("d"); } else { debug("u"); }
233 layer_switch_on(code);
236 layer_switch_off(code);
240 delayed_fn = (keyrecord_t) { .event = event, .code = code, .mods = keyboard_report->mods, .time = timer_read() };
244 layer_switch_off(code);
257 unregister_code(code);
266 // ignored when any key is pressed
269 layer_switch_off(code);
273 register_code(keymap_fn_keycode(FN_INDEX(code)));
276 unregister_code(keymap_fn_keycode(FN_INDEX(code)));
286 unregister_code(code);
287 // no key registered? mousekey, mediakey, systemkey
288 if (!host_has_anykey())
301 waiting_key = (keyrecord_t) { .event = event, .code = code, .mods = keyboard_report->mods, .time = timer_read() };
308 layer_switch_off(code);
312 if (code == delayed_fn.code) {
313 // type Fn with alt keycode
314 // restore the mod status at the time of pressing Fn key
315 tmp_mods = keyboard_report->mods;
316 host_set_mods(delayed_fn.mods);
317 register_code(keymap_fn_keycode(FN_INDEX(delayed_fn.code)));
318 unregister_code(keymap_fn_keycode(FN_INDEX(delayed_fn.code)));
319 host_set_mods(tmp_mods);
322 layer_switch_off(code);
328 unregister_code(code);
332 unregister_code(code);
344 tmp_mods = keyboard_report->mods;
345 host_set_mods(delayed_fn.mods);
346 register_code(keymap_fn_keycode(FN_INDEX(delayed_fn.code)));
347 host_set_mods(waiting_key.mods);
348 register_code(waiting_key.code);
349 host_set_mods(tmp_mods);
357 layer_switch_off(code);
361 if (code == delayed_fn.code) {
362 // alt down, key down, alt up
363 tmp_mods = keyboard_report->mods;
364 host_set_mods(delayed_fn.mods);
365 register_code(keymap_fn_keycode(FN_INDEX(delayed_fn.code)));
366 host_set_mods(waiting_key.mods);
367 register_code(waiting_key.code);
368 unregister_code(keymap_fn_keycode(FN_INDEX(delayed_fn.code)));
369 host_set_mods(tmp_mods);
372 layer_switch_off(code);
378 if (code == waiting_key.code) {
379 layer_switch_on(delayed_fn.code);
381 // process waiting_key
382 tmp_mods = keyboard_report->mods;
383 host_set_mods(waiting_key.mods);
384 process_key(waiting_key.event);
385 host_set_mods(tmp_mods);
388 unregister_code(code);
392 unregister_code(code);
400 // TODO: FAIL SAFE: unregister all keys when no key down
403 void keyboard_init(void)
405 debug_keyboard = true;
409 #ifdef PS2_MOUSE_ENABLE
414 void keyboard_task(void)
416 static matrix_row_t matrix_prev[MATRIX_ROWS];
417 matrix_row_t matrix_row = 0;
418 matrix_row_t matrix_change = 0;
421 if (command_proc()) {
423 // TODO: clear all keys
424 host_clear_keyboard_report();
425 host_send_keyboard_report();
428 for (int r = 0; r < MATRIX_ROWS; r++) {
429 matrix_row = matrix_get_row(r);
430 matrix_change = matrix_row ^ matrix_prev[r];
432 // TODO: print once per scan
433 if (debug_matrix) matrix_print();
435 for (int c = 0; c < MATRIX_COLS; c++) {
436 if (matrix_change & (1<<c)) {
437 process_key((keyevent_t){
438 .key = (key_t){ .row = r, .col = c },
439 .pressed = (matrix_row & (1<<c))
441 // record a processed key
442 matrix_prev[r] ^= (1<<c);
443 // process a key per task call
444 goto MATRIX_LOOP_END;
450 // TODO: FAIL SAFE: clear all key if no key down
452 // layer switch when delay term elapses
453 if (kbdstate == DELAYING || kbdstate == WAITING) {
454 if (timer_elapsed(delayed_fn.time) > LAYER_DELAY) {
455 if (kbdstate == DELAYING) {
456 layer_switch_on(delayed_fn.code);
459 if (kbdstate == WAITING) {
460 layer_switch_on(delayed_fn.code);
462 uint8_t tmp_mods = keyboard_report->mods;
463 host_set_mods(waiting_key.mods);
464 process_key(waiting_key.event);
465 host_set_mods(tmp_mods);
470 // mousekey repeat & acceleration
476 void keyboard_set_leds(uint8_t leds)