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 void layer_switch_on(uint8_t code
)
115 if (!IS_FN(code
)) return;
116 fn_state_bits
|= FN_BIT(code
);
117 if (current_layer
!= keymap_fn_layer(FN_INDEX(code
))) {
118 clear_keyboard_but_mods();
120 debug("Layer Switch(on): "); debug_hex(current_layer
);
121 current_layer
= keymap_fn_layer(FN_INDEX(code
));
122 debug(" -> "); debug_hex(current_layer
); debug("\n");
126 static void layer_switch_off(uint8_t code
)
128 if (!IS_FN(code
)) return;
129 fn_state_bits
&= ~FN_BIT(code
);
130 if (current_layer
!= keymap_fn_layer(biton(fn_state_bits
))) {
131 clear_keyboard_but_mods();
133 debug("Layer Switch(off): "); debug_hex(current_layer
);
134 current_layer
= keymap_fn_layer(biton(fn_state_bits
));
135 debug(" -> "); debug_hex(current_layer
); debug("\n");
139 // whether any key except modifier is down or not
140 static inline bool is_anykey_down(void)
142 for (int r
= 0; r
< MATRIX_ROWS
; r
++) {
143 matrix_row_t matrix_row
= matrix_get_row(r
);
144 for (int c
= 0; c
< MATRIX_COLS
; c
++) {
145 if (matrix_row
&& (1<<c
)) {
146 if (IS_KEY(keymap_get_keycode(current_layer
, r
, c
))) {
155 static void register_code(uint8_t code
)
159 host_send_keyboard_report();
161 else if IS_MOD(code
) {
162 host_add_mod_bit(MOD_BIT(code
));
163 host_send_keyboard_report();
165 else if IS_MOUSEKEY(code
) {
166 #ifdef MOUSEKEY_ENABLE
171 else if IS_CONSUMER(code
) {
177 case KC_AUDIO_VOL_UP
:
178 usage
= AUDIO_VOL_UP
;
180 case KC_AUDIO_VOL_DOWN
:
181 usage
= AUDIO_VOL_DOWN
;
183 case KC_MEDIA_NEXT_TRACK
:
184 usage
= TRANSPORT_NEXT_TRACK
;
186 case KC_MEDIA_PREV_TRACK
:
187 usage
= TRANSPORT_PREV_TRACK
;
190 usage
= TRANSPORT_STOP
;
192 case KC_MEDIA_PLAY_PAUSE
:
193 usage
= TRANSPORT_PLAY_PAUSE
;
195 case KC_MEDIA_SELECT
:
196 usage
= AL_CC_CONFIG
;
202 usage
= AL_CALCULATOR
;
205 usage
= AL_LOCAL_BROWSER
;
225 case KC_WWW_FAVORITES
:
226 usage
= AC_BOOKMARKS
;
229 host_consumer_send(usage
);
231 else if IS_SYSTEM(code
) {
234 case KC_SYSTEM_POWER
:
235 usage
= SYSTEM_POWER_DOWN
;
237 case KC_SYSTEM_SLEEP
:
238 usage
= SYSTEM_SLEEP
;
241 usage
= SYSTEM_WAKE_UP
;
244 host_system_send(usage
);
249 static void unregister_code(uint8_t code
)
253 host_send_keyboard_report();
255 else if IS_MOD(code
) {
256 host_del_mod_bit(MOD_BIT(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 DELAYING[f] WAITING[f,k] PRESSING
276 * -----------+------------------------------------------------------------------
277 * Fn Down |IDLE(L+) WAITING(Sk) WAITING(Sk) -
278 * Up |IDLE(L-) IDLE(L-) IDLE(L-) IDLE(L-)
279 * Fnk Down |DELAYING(Sf) WAITING(Sk) WAINTING(Sk) PRESSING(Rf)
280 * Up |IDLE(L-) IDLE(Rf,Uf) IDLE(Rf,Ps,Uf)*3 PRESSING(Uf)
281 * Key Down |PRESSING(Rk) WAITING(Sk) WAITING(Sk) PRESSING(Rk)
282 * Up |IDLE(Uk) DELAYING(Uk) IDLE(L+,Ps,Uk) IDLE(Uk)*4
283 * Delay |- IDLE(L+) IDLE(L+,Ps) -
285 * No key Down|IDLE(Ld) IDLE(Ld) IDLE(Ld) IDLE(Ld)
287 * *2: register Fnk if any key is pressing
288 * *3: when Fnk == Stored Fnk, if not ignore.
289 * *4: when no registered key any more
293 * DELAYING: delay layer switch after pressing Fn with alt keycode
294 * WAITING: key is pressed during DELAYING
297 * Fn: Fn key without alternative keycode
298 * Fnk: Fn key with alternative keycode
304 * Rf: register stored Fn(alt keycode)
305 * Uf: unregister stored Fn(alt keycode)
306 * Rs: register stored key
307 * Us: unregister stored key
310 * Ps: play stored key(Interpret stored key and transit state)
311 * L+: Switch to new layer(*unregister* all keys but modifiers)
312 * L-: Switch back to last layer(*unregister* all keys but modifiers)
313 * Ld: Switch back to default layer(*unregister* all keys but modifiers)
315 #define NEXT(state) do { \
316 debug("NEXT: "); print_P(state_str(kbdstate)); \
318 debug(" -> "); print_P(state_str(kbdstate)); debug("\n"); \
321 static inline void process_key(keyevent_t event
)
323 uint8_t code
= keymap_get_keycode(current_layer
, event
.key
.row
, event
.key
.col
);
324 keykind_t kind
= get_keykind(code
, event
.pressed
);
328 debug("state: "); print_P(state_str(kbdstate
));
329 debug(" kind: "); debug_hex(kind
);
330 debug(" code: "); debug_hex(code
);
331 if (event
.pressed
) { debug("d"); } else { debug("u"); }
338 layer_switch_on(code
);
341 layer_switch_off(code
);
344 // repeat Fn alt key when press Fn key down, up then down again quickly
345 if (KEYEQ(delayed_fn
.event
.key
, event
.key
) &&
346 timer_elapsed(delayed_fn
.time
) < LAYER_DELAY
) {
347 register_code(keymap_fn_keycode(FN_INDEX(code
)));
350 delayed_fn
= (keyrecord_t
) {
353 .mods
= keyboard_report
->mods
,
360 layer_switch_off(code
);
371 unregister_code(code
);
380 // ignored when any key is pressed
383 layer_switch_off(code
);
387 register_code(keymap_fn_keycode(FN_INDEX(code
)));
390 // can't know whether layer switched or not
391 layer_switch_off(code
);
392 unregister_code(keymap_fn_keycode(FN_INDEX(code
)));
400 unregister_code(code
);
401 // TODO: no key registered? mousekey, mediakey, systemkey
402 if (!host_has_anykey())
414 waiting_key
= (keyrecord_t
) {
417 .mods
= keyboard_report
->mods
,
426 layer_switch_off(code
);
430 if (code
== delayed_fn
.code
) {
431 // type Fn with alt keycode
432 // restore the mod status at the time of pressing Fn key
433 tmp_mods
= keyboard_report
->mods
;
434 host_set_mods(delayed_fn
.mods
);
435 register_code(keymap_fn_keycode(FN_INDEX(delayed_fn
.code
)));
436 unregister_code(keymap_fn_keycode(FN_INDEX(delayed_fn
.code
)));
437 host_set_mods(tmp_mods
);
440 layer_switch_off(code
);
445 unregister_code(code
);
449 unregister_code(code
);
460 tmp_mods
= keyboard_report
->mods
;
461 host_set_mods(delayed_fn
.mods
);
462 register_code(keymap_fn_keycode(FN_INDEX(delayed_fn
.code
)));
463 host_set_mods(waiting_key
.mods
);
464 register_code(waiting_key
.code
);
465 host_set_mods(tmp_mods
);
473 layer_switch_off(code
);
477 if (code
== delayed_fn
.code
) {
478 // alt down, key down, alt up
479 tmp_mods
= keyboard_report
->mods
;
480 host_set_mods(delayed_fn
.mods
);
481 register_code(keymap_fn_keycode(FN_INDEX(delayed_fn
.code
)));
482 host_set_mods(waiting_key
.mods
);
483 register_code(waiting_key
.code
);
484 unregister_code(keymap_fn_keycode(FN_INDEX(delayed_fn
.code
)));
485 host_set_mods(tmp_mods
);
488 layer_switch_off(code
);
493 if (code
== waiting_key
.code
) {
494 layer_switch_on(delayed_fn
.code
);
496 // process waiting_key
497 tmp_mods
= keyboard_report
->mods
;
498 host_set_mods(waiting_key
.mods
);
499 process_key(waiting_key
.event
);
500 host_set_mods(tmp_mods
);
503 unregister_code(code
);
507 unregister_code(code
);
516 void keyboard_init(void)
518 debug_keyboard
= true;
522 #ifdef PS2_MOUSE_ENABLE
527 void keyboard_task(void)
529 static matrix_row_t matrix_prev
[MATRIX_ROWS
];
530 matrix_row_t matrix_row
= 0;
531 matrix_row_t matrix_change
= 0;
534 if (command_proc()) {
536 // TODO: COMMAND state?
541 for (int r
= 0; r
< MATRIX_ROWS
; r
++) {
542 matrix_row
= matrix_get_row(r
);
543 matrix_change
= matrix_row
^ matrix_prev
[r
];
545 if (debug_matrix
) matrix_print();
547 for (int c
= 0; c
< MATRIX_COLS
; c
++) {
548 if (matrix_change
& (1<<c
)) {
549 process_key((keyevent_t
){
550 .key
= (key_t
){ .row
= r
, .col
= c
},
551 .pressed
= (matrix_row
& (1<<c
))
553 // record a processed key
554 matrix_prev
[r
] ^= (1<<c
);
555 // process a key per task call
556 goto MATRIX_LOOP_END
;
563 // layer switch when delay term elapses
564 if (kbdstate
== DELAYING
|| kbdstate
== WAITING
) {
565 if (timer_elapsed(delayed_fn
.time
) > LAYER_DELAY
) {
566 if (kbdstate
== DELAYING
) {
567 layer_switch_on(delayed_fn
.code
);
570 if (kbdstate
== WAITING
) {
571 layer_switch_on(delayed_fn
.code
);
573 uint8_t tmp_mods
= keyboard_report
->mods
;
574 host_set_mods(waiting_key
.mods
);
575 process_key(waiting_key
.event
);
576 host_set_mods(tmp_mods
);
581 #ifdef MOUSEKEY_ENABLE
582 // mousekey repeat & acceleration
586 // FAIL SAFE: clear all key if no key down
588 matrix_row_t is_matrix_on
= 0;
589 for (int r
= 0; r
< MATRIX_ROWS
; r
++) {
590 is_matrix_on
|= matrix_get_row(r
);
593 debug("FAIL SAFE: clear all keys.\n");
601 void keyboard_set_leds(uint8_t leds
)