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)
212 static keyrecord_t waiting_keys[5];
213 static uint8_t waiting_keys_head = 0;
214 static uint8_t waiting_keys_tail = 0;
217 uint8_t code = get_keycode(event.key);
218 keykind_t kind = get_keykind(code, event.pressed);
222 debug("state: "); print_P(state_str(kbdstate));
223 debug(" kind: "); debug_hex(kind);
224 debug(" code: "); debug_hex(code);
225 if (event.pressed) { debug("d"); } else { debug("u"); }
232 layer_switch_on(code);
235 layer_switch_off(code);
238 // repeat Fn alt key when press Fn key down, up then down again quickly
239 if (KEYEQ(delayed_fn.event.key, event.key) &&
240 timer_elapsed(delayed_fn.time) < LAYER_DELAY) {
241 register_code(keymap_fn_keycode(FN_INDEX(code)));
244 delayed_fn = (keyrecord_t) {
247 .mods = keyboard_report->mods,
254 layer_switch_off(code);
267 unregister_code(code);
276 // ignored when any key is pressed
279 layer_switch_off(code);
283 register_code(keymap_fn_keycode(FN_INDEX(code)));
286 unregister_code(keymap_fn_keycode(FN_INDEX(code)));
296 unregister_code(code);
297 // no key registered? mousekey, mediakey, systemkey
298 if (!host_has_anykey())
311 waiting_key = (keyrecord_t) {
314 .mods = keyboard_report->mods,
323 layer_switch_off(code);
327 if (code == delayed_fn.code) {
328 // type Fn with alt keycode
329 // restore the mod status at the time of pressing Fn key
330 tmp_mods = keyboard_report->mods;
331 host_set_mods(delayed_fn.mods);
332 register_code(keymap_fn_keycode(FN_INDEX(delayed_fn.code)));
333 unregister_code(keymap_fn_keycode(FN_INDEX(delayed_fn.code)));
334 host_set_mods(tmp_mods);
337 layer_switch_off(code);
343 unregister_code(code);
347 unregister_code(code);
359 tmp_mods = keyboard_report->mods;
360 host_set_mods(delayed_fn.mods);
361 register_code(keymap_fn_keycode(FN_INDEX(delayed_fn.code)));
362 host_set_mods(waiting_key.mods);
363 register_code(waiting_key.code);
364 host_set_mods(tmp_mods);
372 layer_switch_off(code);
376 if (code == delayed_fn.code) {
377 // alt down, key down, alt up
378 tmp_mods = keyboard_report->mods;
379 host_set_mods(delayed_fn.mods);
380 register_code(keymap_fn_keycode(FN_INDEX(delayed_fn.code)));
381 host_set_mods(waiting_key.mods);
382 register_code(waiting_key.code);
383 unregister_code(keymap_fn_keycode(FN_INDEX(delayed_fn.code)));
384 host_set_mods(tmp_mods);
387 layer_switch_off(code);
393 if (code == waiting_key.code) {
394 layer_switch_on(delayed_fn.code);
396 // process waiting_key
397 tmp_mods = keyboard_report->mods;
398 host_set_mods(waiting_key.mods);
399 process_key(waiting_key.event);
400 host_set_mods(tmp_mods);
403 unregister_code(code);
407 unregister_code(code);
415 // TODO: FAIL SAFE: unregister all keys when no key down
418 void keyboard_init(void)
420 debug_keyboard = true;
424 #ifdef PS2_MOUSE_ENABLE
429 void keyboard_task(void)
431 static matrix_row_t matrix_prev[MATRIX_ROWS];
432 matrix_row_t matrix_row = 0;
433 matrix_row_t matrix_change = 0;
436 if (command_proc()) {
438 // TODO: clear all keys
439 host_clear_keyboard_report();
440 host_send_keyboard_report();
443 for (int r = 0; r < MATRIX_ROWS; r++) {
444 matrix_row = matrix_get_row(r);
445 matrix_change = matrix_row ^ matrix_prev[r];
447 // TODO: print once per scan
448 if (debug_matrix) matrix_print();
450 for (int c = 0; c < MATRIX_COLS; c++) {
451 if (matrix_change & (1<<c)) {
452 process_key((keyevent_t){
453 .key = (key_t){ .row = r, .col = c },
454 .pressed = (matrix_row & (1<<c))
456 // record a processed key
457 matrix_prev[r] ^= (1<<c);
458 // process a key per task call
459 goto MATRIX_LOOP_END;
465 // TODO: FAIL SAFE: clear all key if no key down
467 // layer switch when delay term elapses
468 if (kbdstate == DELAYING || kbdstate == WAITING) {
469 if (timer_elapsed(delayed_fn.time) > LAYER_DELAY) {
470 if (kbdstate == DELAYING) {
471 layer_switch_on(delayed_fn.code);
474 if (kbdstate == WAITING) {
475 layer_switch_on(delayed_fn.code);
477 uint8_t tmp_mods = keyboard_report->mods;
478 host_set_mods(waiting_key.mods);
479 process_key(waiting_key.event);
480 host_set_mods(tmp_mods);
485 // mousekey repeat & acceleration
491 void keyboard_set_leds(uint8_t leds)