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 #ifdef KEYMAP_DEFAULT_LAYER
50 uint8_t default_layer
= KEYMAP_DEFAULT_LAYER
;
51 uint8_t current_layer
= KEYMAP_DEFAULT_LAYER
;
53 uint8_t default_layer
= 0;
54 uint8_t current_layer
= 0;
57 /* keyboard internal states */
58 static kbdstate_t kbdstate
= IDLE
;
59 static uint8_t fn_state_bits
= 0;
60 static keyrecord_t delayed_fn
;
61 static keyrecord_t waiting_key
;
64 static const char *state_str(kbdstate_t state
)
66 if (state
== IDLE
) return PSTR("IDLE");
67 if (state
== DELAYING
) return PSTR("DELAYING");
68 if (state
== WAITING
) return PSTR("WAITING");
69 if (state
== PRESSING
) return PSTR("PRESSING");
70 return PSTR("UNKNOWN");
73 static inline keykind_t
get_keykind(uint8_t code
, bool pressed
)
75 if IS_KEY(code
) return (pressed
? KEY_DOWN
: KEY_UP
);
76 if IS_MOD(code
) return (pressed
? MOD_DOWN
: MOD_UP
);
78 if (keymap_fn_keycode(FN_INDEX(code
)))
79 return (pressed
? FNK_DOWN
: FNK_UP
);
81 return (pressed
? FN_DOWN
: FN_UP
);
83 if IS_MOUSEKEY(code
) return (pressed
? KEY_DOWN
: KEY_UP
);
84 if IS_SYSTEM(code
) return (pressed
? KEY_DOWN
: KEY_UP
);
85 if IS_CONSUMER(code
) return (pressed
? KEY_DOWN
: KEY_UP
);
89 static void clear_keyboard(void)
93 host_send_keyboard_report();
96 host_consumer_send(0);
98 #ifdef MOUSEKEY_ENABLE
104 static void clear_keyboard_but_mods(void)
107 host_send_keyboard_report();
110 host_consumer_send(0);
112 #ifdef MOUSEKEY_ENABLE
118 static bool anykey_sent_to_host(void)
120 return (host_has_anykey() || host_mouse_in_use() ||
121 host_last_sysytem_report() || host_last_consumer_report());
124 static void layer_switch_on(uint8_t code
)
126 if (!IS_FN(code
)) return;
127 fn_state_bits
|= FN_BIT(code
);
128 uint8_t new_layer
= (fn_state_bits
? keymap_fn_layer(biton(fn_state_bits
)) : default_layer
);
129 if (current_layer
!= new_layer
) {
130 debug("Layer Switch(on): "); debug_hex(current_layer
);
131 debug(" -> "); debug_hex(new_layer
); debug("\n");
133 clear_keyboard_but_mods();
134 current_layer
= new_layer
;
138 static bool layer_switch_off(uint8_t code
)
140 if (!IS_FN(code
)) return false;
141 fn_state_bits
&= ~FN_BIT(code
);
142 uint8_t new_layer
= (fn_state_bits
? keymap_fn_layer(biton(fn_state_bits
)) : default_layer
);
143 if (current_layer
!= new_layer
) {
144 debug("Layer Switch(off): "); debug_hex(current_layer
);
145 debug(" -> "); debug_hex(new_layer
); debug("\n");
147 clear_keyboard_but_mods();
148 current_layer
= new_layer
;
154 static void register_code(uint8_t code
)
158 host_send_keyboard_report();
160 else if IS_MOD(code
) {
161 host_add_mod_bit(MOD_BIT(code
));
162 host_send_keyboard_report();
164 else if IS_FN(code
) {
165 host_add_key(keymap_fn_keycode(FN_INDEX(code
)));
166 host_send_keyboard_report();
168 else if IS_MOUSEKEY(code
) {
169 #ifdef MOUSEKEY_ENABLE
174 else if IS_CONSUMER(code
) {
180 case KC_AUDIO_VOL_UP
:
181 usage
= AUDIO_VOL_UP
;
183 case KC_AUDIO_VOL_DOWN
:
184 usage
= AUDIO_VOL_DOWN
;
186 case KC_MEDIA_NEXT_TRACK
:
187 usage
= TRANSPORT_NEXT_TRACK
;
189 case KC_MEDIA_PREV_TRACK
:
190 usage
= TRANSPORT_PREV_TRACK
;
193 usage
= TRANSPORT_STOP
;
195 case KC_MEDIA_PLAY_PAUSE
:
196 usage
= TRANSPORT_PLAY_PAUSE
;
198 case KC_MEDIA_SELECT
:
199 usage
= AL_CC_CONFIG
;
205 usage
= AL_CALCULATOR
;
208 usage
= AL_LOCAL_BROWSER
;
228 case KC_WWW_FAVORITES
:
229 usage
= AC_BOOKMARKS
;
232 host_consumer_send(usage
);
234 else if IS_SYSTEM(code
) {
237 case KC_SYSTEM_POWER
:
238 usage
= SYSTEM_POWER_DOWN
;
240 case KC_SYSTEM_SLEEP
:
241 usage
= SYSTEM_SLEEP
;
244 usage
= SYSTEM_WAKE_UP
;
247 host_system_send(usage
);
252 static void unregister_code(uint8_t code
)
256 host_send_keyboard_report();
258 else if IS_MOD(code
) {
259 host_del_mod_bit(MOD_BIT(code
));
260 host_send_keyboard_report();
262 else if IS_FN(code
) {
263 host_del_key(keymap_fn_keycode(FN_INDEX(code
)));
264 host_send_keyboard_report();
266 else if IS_MOUSEKEY(code
) {
267 #ifdef MOUSEKEY_ENABLE
272 else if IS_CONSUMER(code
) {
273 host_consumer_send(0x0000);
275 else if IS_SYSTEM(code
) {
276 host_system_send(0x0000);
282 * Event/State|IDLE PRESSING DELAYING[f] WAITING[f,k]
283 * -----------+------------------------------------------------------------------
284 * Fn Down |(L+) -*1 WAITING(Sk) IDLE(Rf,Ps)*7
285 * Up |(L-) IDLE(L-)*8 IDLE(L-)*8 IDLE(L-)*8
286 * Fnk Down |DELAYING(Sf)* (Rf) WAITING(Sk) IDLE(Rf,Ps,Rf)
287 * Up |(L-) IDLE(L-/Uf)*8 IDLE(Rf,Uf/L-)*3 IDLE(Rf,Ps,Uf/L-)*3
288 * Key Down |PRESSING(Rk) (Rk) WAITING(Sk) IDLE(Rf,Ps,Rk)
289 * Up |(Uk) IDLE(Uk)*4 (Uk) IDLE(L+,Ps,Pk)/(Uk)*a
291 * Delay |- - IDLE(L+) IDLE(L+,Ps)
292 * Magic Key |COMMAND*5
294 * *1: ignore Fn if other key is down.
295 * *2: register Fnk if any key is pressing
296 * *3: register/unregister delayed Fnk and move to IDLE if code == delayed Fnk, else *8
297 * *4: if no keys registered to host
298 * *5: unregister all keys
299 * *6: only if no keys down
300 * *7: ignore Fn because Fnk key and stored key are down.
301 * *8: move to IDLE if layer switch(off) occurs, else stay at current state
302 * *9: repeat key if pressing Fnk twice quickly(move to PRESSING)
303 * *a: layer switch and process waiting key and code if code == wainting key, else unregister key
306 * IDLE: No key is down except modifiers
307 * DELAYING: delay layer switch after pressing Fn with alt keycode
308 * WAITING: key is pressed during DELAYING
311 * Fn: Fn key without alternative keycode
312 * Fnk: Fn key with alternative keycode
314 * Delay: layer switch delay term is elapsed
319 * Rf: register Fn(alt keycode)
320 * Uf: unregister Fn(alt keycode)
321 * Rs: register stored key
322 * Us: unregister stored key
323 * Sk: Store key(waiting Key)
324 * Sf: Store Fn(delayed Fn)
325 * Ps: Process stored key
327 * Is: Interpret stored keys in current layer
328 * L+: Switch to new layer(*unregister* all keys but modifiers)
329 * L-: Switch back to last layer(*unregister* all keys but modifiers)
330 * Ld: Switch back to default layer(*unregister* all keys but modifiers)
332 #define NEXT(state) do { \
333 debug("NEXT: "); print_P(state_str(kbdstate)); \
335 debug(" -> "); print_P(state_str(kbdstate)); debug("\n"); \
338 static inline void process_key(keyevent_t event
)
340 uint8_t code
= keymap_get_keycode(current_layer
, event
.key
.row
, event
.key
.col
);
341 keykind_t kind
= get_keykind(code
, event
.pressed
);
345 debug("state: "); print_P(state_str(kbdstate
));
346 debug(" kind: "); debug_hex(kind
);
347 debug(" code: "); debug_hex(code
);
348 if (event
.pressed
) { debug("d"); } else { debug("u"); }
355 layer_switch_on(code
);
358 layer_switch_off(code
);
361 // repeat Fn alt key when press Fn key down, up then down again quickly
362 if (KEYEQ(delayed_fn
.event
.key
, event
.key
) &&
363 timer_elapsed(delayed_fn
.time
) < LAYER_DELAY
) {
367 delayed_fn
= (keyrecord_t
) {
370 .mods
= keyboard_report
->mods
,
377 layer_switch_off(code
);
388 unregister_code(code
);
397 // ignored when any key is pressed
400 if (layer_switch_off(code
))
407 if (layer_switch_off(code
)) {
410 unregister_code(code
);
411 if (!anykey_sent_to_host())
421 unregister_code(code
);
422 if (!anykey_sent_to_host())
434 waiting_key
= (keyrecord_t
) {
437 .mods
= keyboard_report
->mods
,
446 if (layer_switch_off(code
))
450 if (code
== delayed_fn
.code
) {
451 // type Fn with alt keycode
452 // restore the mod status at the time of pressing Fn key
453 tmp_mods
= keyboard_report
->mods
;
454 host_set_mods(delayed_fn
.mods
);
455 register_code(delayed_fn
.code
);
456 unregister_code(delayed_fn
.code
);
457 host_set_mods(tmp_mods
);
460 if (layer_switch_off(code
))
466 unregister_code(code
);
477 tmp_mods
= keyboard_report
->mods
;
478 host_set_mods(delayed_fn
.mods
);
479 register_code(delayed_fn
.code
);
480 host_set_mods(waiting_key
.mods
);
481 register_code(waiting_key
.code
);
482 host_set_mods(tmp_mods
);
483 if (kind
== FN_DOWN
) {
485 } else if (kind
== FNK_DOWN
) {
487 } else if (kind
== KEY_DOWN
) {
496 if (layer_switch_off(code
))
500 if (code
== delayed_fn
.code
) {
501 // alt down, key down, alt up
502 tmp_mods
= keyboard_report
->mods
;
503 host_set_mods(delayed_fn
.mods
);
504 register_code(delayed_fn
.code
);
505 host_set_mods(waiting_key
.mods
);
506 register_code(waiting_key
.code
);
507 unregister_code(delayed_fn
.code
);
508 host_set_mods(tmp_mods
);
511 if (layer_switch_off(code
))
516 if (code
== waiting_key
.code
) {
517 layer_switch_on(delayed_fn
.code
);
519 // process waiting_key
520 tmp_mods
= keyboard_report
->mods
;
521 host_set_mods(waiting_key
.mods
);
522 process_key(waiting_key
.event
);
523 host_set_mods(tmp_mods
);
526 unregister_code(code
);
530 unregister_code(code
);
539 void keyboard_init(void)
541 debug_keyboard
= true;
545 #ifdef PS2_MOUSE_ENABLE
550 void keyboard_task(void)
552 static matrix_row_t matrix_prev
[MATRIX_ROWS
];
553 matrix_row_t matrix_row
= 0;
554 matrix_row_t matrix_change
= 0;
557 if (command_proc()) {
559 // TODO: COMMAND state?
564 for (int r
= 0; r
< MATRIX_ROWS
; r
++) {
565 matrix_row
= matrix_get_row(r
);
566 matrix_change
= matrix_row
^ matrix_prev
[r
];
568 if (debug_matrix
) matrix_print();
570 for (int c
= 0; c
< MATRIX_COLS
; c
++) {
571 if (matrix_change
& (1<<c
)) {
572 process_key((keyevent_t
){
573 .key
= (key_t
){ .row
= r
, .col
= c
},
574 .pressed
= (matrix_row
& (1<<c
))
576 // record a processed key
577 matrix_prev
[r
] ^= (1<<c
);
578 // process a key per task call
579 goto MATRIX_LOOP_END
;
586 // layer switch when delay term elapses
587 if (kbdstate
== DELAYING
|| kbdstate
== WAITING
) {
588 if (timer_elapsed(delayed_fn
.time
) > LAYER_DELAY
) {
589 if (kbdstate
== DELAYING
) {
590 layer_switch_on(delayed_fn
.code
);
593 if (kbdstate
== WAITING
) {
594 layer_switch_on(delayed_fn
.code
);
596 uint8_t tmp_mods
= keyboard_report
->mods
;
597 host_set_mods(waiting_key
.mods
);
598 process_key(waiting_key
.event
);
599 host_set_mods(tmp_mods
);
604 #ifdef MOUSEKEY_ENABLE
605 // mousekey repeat & acceleration
609 // FAIL SAFE: clear all key if no key down
611 matrix_row_t is_matrix_on
= 0;
612 for (int r
= 0; r
< MATRIX_ROWS
; r
++) {
613 is_matrix_on
|= matrix_get_row(r
);
616 debug("FAIL SAFE: clear all keys(default layer).\n");
618 current_layer
= default_layer
;
625 void keyboard_set_leds(uint8_t leds
)