2 Copyright 2011,2012 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/>.
29 #ifdef MOUSEKEY_ENABLE
34 #define Kdebug(s) do { if (debug_keyboard) debug(s); } while(0)
35 #define Kdebug_P(s) do { if (debug_keyboard) debug_P(s); } while(0)
36 #define Kdebug_hex(s) do { if (debug_keyboard) debug_hex(s); } while(0)
38 #define LAYER_DELAY 250
40 typedef enum keykind
{
48 typedef enum { IDLE
, DELAYING
, WAITING
, PRESSING
} kbdstate_t
;
51 #ifdef KEYMAP_DEFAULT_LAYER
52 uint8_t default_layer
= KEYMAP_DEFAULT_LAYER
;
53 uint8_t current_layer
= KEYMAP_DEFAULT_LAYER
;
55 uint8_t default_layer
= 0;
56 uint8_t current_layer
= 0;
59 /* keyboard internal states */
60 static kbdstate_t kbdstate
= IDLE
;
61 static uint8_t fn_state_bits
= 0;
62 static keyrecord_t delayed_fn
;
63 static keyrecord_t waiting_key
;
66 static const char *state_str(kbdstate_t state
)
68 if (state
== IDLE
) return PSTR("IDLE");
69 if (state
== DELAYING
) return PSTR("DELAYING");
70 if (state
== WAITING
) return PSTR("WAITING");
71 if (state
== PRESSING
) return PSTR("PRESSING");
72 return PSTR("UNKNOWN");
75 static inline keykind_t
get_keykind(uint8_t code
, bool pressed
)
77 if IS_KEY(code
) return (pressed
? KEY_DOWN
: KEY_UP
);
78 if IS_MOD(code
) return (pressed
? MOD_DOWN
: MOD_UP
);
80 if (keymap_fn_keycode(FN_INDEX(code
)))
81 return (pressed
? FNK_DOWN
: FNK_UP
);
83 return (pressed
? FN_DOWN
: FN_UP
);
85 if IS_MOUSEKEY(code
) return (pressed
? KEY_DOWN
: KEY_UP
);
86 if IS_SYSTEM(code
) return (pressed
? KEY_DOWN
: KEY_UP
);
87 if IS_CONSUMER(code
) return (pressed
? KEY_DOWN
: KEY_UP
);
91 static void clear_keyboard(void)
95 host_send_keyboard_report();
98 host_consumer_send(0);
100 #ifdef MOUSEKEY_ENABLE
106 static void clear_keyboard_but_mods(void)
109 host_send_keyboard_report();
112 host_consumer_send(0);
114 #ifdef MOUSEKEY_ENABLE
120 static bool anykey_sent_to_host(void)
122 return (host_has_anykey() || host_mouse_in_use() ||
123 host_last_sysytem_report() || host_last_consumer_report());
126 static void layer_switch_on(uint8_t code
)
128 if (!IS_FN(code
)) return;
129 fn_state_bits
|= FN_BIT(code
);
130 uint8_t new_layer
= (fn_state_bits
? keymap_fn_layer(biton(fn_state_bits
)) : default_layer
);
131 if (current_layer
!= new_layer
) {
132 Kdebug("Layer Switch(on): "); Kdebug_hex(current_layer
);
133 Kdebug(" -> "); Kdebug_hex(new_layer
); Kdebug("\n");
135 clear_keyboard_but_mods();
136 current_layer
= new_layer
;
140 static bool layer_switch_off(uint8_t code
)
142 if (!IS_FN(code
)) return false;
143 fn_state_bits
&= ~FN_BIT(code
);
144 uint8_t new_layer
= (fn_state_bits
? keymap_fn_layer(biton(fn_state_bits
)) : default_layer
);
145 if (current_layer
!= new_layer
) {
146 Kdebug("Layer Switch(off): "); Kdebug_hex(current_layer
);
147 Kdebug(" -> "); Kdebug_hex(new_layer
); Kdebug("\n");
149 clear_keyboard_but_mods();
150 current_layer
= new_layer
;
156 static void register_code(uint8_t code
)
159 if (!command_proc(code
)) {
161 host_send_keyboard_report();
164 else if IS_MOD(code
) {
165 host_add_mod_bit(MOD_BIT(code
));
166 host_send_keyboard_report();
168 else if IS_FN(code
) {
169 if (!command_proc(keymap_fn_keycode(FN_INDEX(code
)))) {
170 host_add_key(keymap_fn_keycode(FN_INDEX(code
)));
171 host_send_keyboard_report();
174 else if IS_MOUSEKEY(code
) {
175 #ifdef MOUSEKEY_ENABLE
180 else if IS_CONSUMER(code
) {
186 case KC_AUDIO_VOL_UP
:
187 usage
= AUDIO_VOL_UP
;
189 case KC_AUDIO_VOL_DOWN
:
190 usage
= AUDIO_VOL_DOWN
;
192 case KC_MEDIA_NEXT_TRACK
:
193 usage
= TRANSPORT_NEXT_TRACK
;
195 case KC_MEDIA_PREV_TRACK
:
196 usage
= TRANSPORT_PREV_TRACK
;
199 usage
= TRANSPORT_STOP
;
201 case KC_MEDIA_PLAY_PAUSE
:
202 usage
= TRANSPORT_PLAY_PAUSE
;
204 case KC_MEDIA_SELECT
:
205 usage
= AL_CC_CONFIG
;
211 usage
= AL_CALCULATOR
;
214 usage
= AL_LOCAL_BROWSER
;
234 case KC_WWW_FAVORITES
:
235 usage
= AC_BOOKMARKS
;
238 host_consumer_send(usage
);
240 else if IS_SYSTEM(code
) {
243 case KC_SYSTEM_POWER
:
244 usage
= SYSTEM_POWER_DOWN
;
246 case KC_SYSTEM_SLEEP
:
247 usage
= SYSTEM_SLEEP
;
250 usage
= SYSTEM_WAKE_UP
;
253 host_system_send(usage
);
258 static void unregister_code(uint8_t code
)
262 host_send_keyboard_report();
264 else if IS_MOD(code
) {
265 host_del_mod_bit(MOD_BIT(code
));
266 host_send_keyboard_report();
268 else if IS_FN(code
) {
269 host_del_key(keymap_fn_keycode(FN_INDEX(code
)));
270 host_send_keyboard_report();
272 else if IS_MOUSEKEY(code
) {
273 #ifdef MOUSEKEY_ENABLE
278 else if IS_CONSUMER(code
) {
279 host_consumer_send(0x0000);
281 else if IS_SYSTEM(code
) {
282 host_system_send(0x0000);
288 * Event/State|IDLE PRESSING DELAYING[f] WAITING[f,k]
289 * -----------+------------------------------------------------------------------
290 * Fn Down |(L+) -*1 WAITING(Sk) IDLE(Rf,Ps)*7
291 * Up |(L-) IDLE(L-)*8 IDLE(L-)*8 IDLE(L-)*8
292 * Fnk Down |DELAYING(Sf)* (Rf) WAITING(Sk) IDLE(Rf,Ps,Rf)
293 * Up |(L-) IDLE(L-/Uf)*8 IDLE(Rf,Uf/L-)*3 IDLE(Rf,Ps,Uf/L-)*3
294 * Key Down |PRESSING(Rk) (Rk) WAITING(Sk) IDLE(Rf,Ps,Rk)
295 * Up |(Uk) IDLE(Uk)*4 (Uk) IDLE(L+,Ps,Pk)/(Uk)*a
297 * Delay |- - IDLE(L+) IDLE(L+,Ps)
298 * Magic Key |COMMAND*5
300 * *1: ignore Fn if other key is down.
301 * *2: register Fnk if any key is pressing
302 * *3: register/unregister delayed Fnk and move to IDLE if code == delayed Fnk, else *8
303 * *4: if no keys registered to host
304 * *5: unregister all keys
305 * *6: only if no keys down
306 * *7: ignore Fn because Fnk key and stored key are down.
307 * *8: move to IDLE if layer switch(off) occurs, else stay at current state
308 * *9: repeat key if pressing Fnk twice quickly(move to PRESSING)
309 * *a: layer switch and process waiting key and code if code == wainting key, else unregister key
312 * IDLE: No key is down except modifiers
313 * DELAYING: delay layer switch after pressing Fn with alt keycode
314 * WAITING: key is pressed during DELAYING
317 * Fn: Fn key without alternative keycode
318 * Fnk: Fn key with alternative keycode
320 * Delay: layer switch delay term is elapsed
325 * Rf: register Fn(alt keycode)
326 * Uf: unregister Fn(alt keycode)
327 * Rs: register stored key
328 * Us: unregister stored key
329 * Sk: Store key(waiting Key)
330 * Sf: Store Fn(delayed Fn)
331 * Ps: Process stored key
333 * Is: Interpret stored keys in current layer
334 * L+: Switch to new layer(*unregister* all keys but modifiers)
335 * L-: Switch back to last layer(*unregister* all keys but modifiers)
336 * Ld: Switch back to default layer(*unregister* all keys but modifiers)
338 #define NEXT(state) do { \
339 Kdebug("NEXT: "); Kdebug_P(state_str(kbdstate)); \
341 Kdebug(" -> "); Kdebug_P(state_str(kbdstate)); Kdebug("\n"); \
344 static inline void process_key(keyevent_t event
)
346 uint8_t code
= keymap_get_keycode(current_layer
, event
.key
.row
, event
.key
.col
);
347 keykind_t kind
= get_keykind(code
, event
.pressed
);
351 Kdebug("state: "); Kdebug_P(state_str(kbdstate
));
352 Kdebug(" kind: "); Kdebug_hex(kind
);
353 Kdebug(" code: "); Kdebug_hex(code
);
354 if (event
.pressed
) { Kdebug("d"); } else { Kdebug("u"); }
361 layer_switch_on(code
);
364 layer_switch_off(code
);
367 // repeat Fn alt key when press Fn key down, up then down again quickly
368 if (KEYEQ(delayed_fn
.event
.key
, event
.key
) &&
369 timer_elapsed(delayed_fn
.time
) < LAYER_DELAY
) {
373 delayed_fn
= (keyrecord_t
) {
376 .mods
= keyboard_report
->mods
,
383 layer_switch_off(code
);
394 unregister_code(code
);
403 // ignored when any key is pressed
406 if (layer_switch_off(code
))
413 if (layer_switch_off(code
)) {
416 unregister_code(code
);
417 if (!anykey_sent_to_host())
427 unregister_code(code
);
428 if (!anykey_sent_to_host())
440 waiting_key
= (keyrecord_t
) {
443 .mods
= keyboard_report
->mods
,
452 if (layer_switch_off(code
))
456 if (code
== delayed_fn
.code
) {
457 // type Fn with alt keycode
458 // restore the mod status at the time of pressing Fn key
459 tmp_mods
= keyboard_report
->mods
;
460 host_set_mods(delayed_fn
.mods
);
461 register_code(delayed_fn
.code
);
462 unregister_code(delayed_fn
.code
);
463 host_set_mods(tmp_mods
);
466 if (layer_switch_off(code
))
472 unregister_code(code
);
483 tmp_mods
= keyboard_report
->mods
;
484 host_set_mods(delayed_fn
.mods
);
485 register_code(delayed_fn
.code
);
486 host_set_mods(waiting_key
.mods
);
487 register_code(waiting_key
.code
);
488 host_set_mods(tmp_mods
);
489 if (kind
== FN_DOWN
) {
491 } else if (kind
== FNK_DOWN
) {
493 } else if (kind
== KEY_DOWN
) {
502 if (layer_switch_off(code
))
506 if (code
== delayed_fn
.code
) {
507 // alt down, key down, alt up
508 tmp_mods
= keyboard_report
->mods
;
509 host_set_mods(delayed_fn
.mods
);
510 register_code(delayed_fn
.code
);
511 host_set_mods(waiting_key
.mods
);
512 register_code(waiting_key
.code
);
513 unregister_code(delayed_fn
.code
);
514 host_set_mods(tmp_mods
);
517 if (layer_switch_off(code
))
522 if (code
== waiting_key
.code
) {
523 layer_switch_on(delayed_fn
.code
);
525 // process waiting_key
526 tmp_mods
= keyboard_report
->mods
;
527 host_set_mods(waiting_key
.mods
);
528 process_key(waiting_key
.event
);
529 host_set_mods(tmp_mods
);
532 unregister_code(code
);
536 unregister_code(code
);
545 void keyboard_init(void)
547 // TODO: to enable debug print magic key bind on boot time
549 // TODO: configuration of sendchar impl
550 print_sendchar_func
= sendchar
;
554 #ifdef PS2_MOUSE_ENABLE
559 void keyboard_task(void)
561 static matrix_row_t matrix_prev
[MATRIX_ROWS
];
562 static uint8_t led_status
= 0;
563 matrix_row_t matrix_row
= 0;
564 matrix_row_t matrix_change
= 0;
567 for (int r
= 0; r
< MATRIX_ROWS
; r
++) {
568 matrix_row
= matrix_get_row(r
);
569 matrix_change
= matrix_row
^ matrix_prev
[r
];
571 if (debug_matrix
) matrix_print();
573 for (int c
= 0; c
< MATRIX_COLS
; c
++) {
574 if (matrix_change
& (1<<c
)) {
575 process_key((keyevent_t
){
576 .key
= (key_t
){ .row
= r
, .col
= c
},
577 .pressed
= (matrix_row
& (1<<c
))
579 // record a processed key
580 matrix_prev
[r
] ^= (1<<c
);
581 // process a key per task call
582 goto MATRIX_LOOP_END
;
589 // layer switch when delay term elapses
590 if (kbdstate
== DELAYING
|| kbdstate
== WAITING
) {
591 if (timer_elapsed(delayed_fn
.time
) > LAYER_DELAY
) {
592 if (kbdstate
== DELAYING
) {
593 layer_switch_on(delayed_fn
.code
);
596 if (kbdstate
== WAITING
) {
597 layer_switch_on(delayed_fn
.code
);
599 uint8_t tmp_mods
= keyboard_report
->mods
;
600 host_set_mods(waiting_key
.mods
);
601 process_key(waiting_key
.event
);
602 host_set_mods(tmp_mods
);
607 #ifdef MOUSEKEY_ENABLE
608 // mousekey repeat & acceleration
612 // FAIL SAFE: clear all key if no key down
614 matrix_row_t is_matrix_on
= 0;
615 for (int r
= 0; r
< MATRIX_ROWS
; r
++) {
616 is_matrix_on
|= matrix_get_row(r
);
619 Kdebug("FAIL SAFE: clear all keys(default layer).\n");
621 current_layer
= default_layer
;
626 if (led_status
!= host_keyboard_leds()) {
627 led_status
= host_keyboard_leds();
628 keyboard_set_leds(led_status
);
634 void keyboard_set_leds(uint8_t leds
)