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
33 #define LAYER_DELAY 250
35 typedef enum keykind
{
43 typedef enum { IDLE
, DELAYING
, WAITING
, PRESSING
} kbdstate_t
;
46 #ifdef KEYMAP_DEFAULT_LAYER
47 uint8_t default_layer
= KEYMAP_DEFAULT_LAYER
;
48 uint8_t current_layer
= KEYMAP_DEFAULT_LAYER
;
50 uint8_t default_layer
= 0;
51 uint8_t current_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
? KEY_DOWN
: KEY_UP
);
81 if IS_SYSTEM(code
) return (pressed
? KEY_DOWN
: KEY_UP
);
82 if IS_CONSUMER(code
) return (pressed
? KEY_DOWN
: KEY_UP
);
86 static void clear_keyboard(void)
90 host_send_keyboard_report();
93 host_consumer_send(0);
95 #ifdef MOUSEKEY_ENABLE
101 static void clear_keyboard_but_mods(void)
104 host_send_keyboard_report();
107 host_consumer_send(0);
109 #ifdef MOUSEKEY_ENABLE
115 static bool anykey_sent_to_host(void)
117 return (host_has_anykey() || host_mouse_in_use() ||
118 host_last_sysytem_report() || host_last_consumer_report());
121 static void layer_switch_on(uint8_t code
)
123 if (!IS_FN(code
)) return;
124 fn_state_bits
|= FN_BIT(code
);
125 uint8_t new_layer
= (fn_state_bits
? keymap_fn_layer(biton(fn_state_bits
)) : default_layer
);
126 if (current_layer
!= new_layer
) {
127 debug("Layer Switch(on): "); debug_hex(current_layer
);
128 debug(" -> "); debug_hex(new_layer
); debug("\n");
130 clear_keyboard_but_mods();
131 current_layer
= new_layer
;
135 static bool layer_switch_off(uint8_t code
)
137 if (!IS_FN(code
)) return false;
138 fn_state_bits
&= ~FN_BIT(code
);
139 uint8_t new_layer
= (fn_state_bits
? keymap_fn_layer(biton(fn_state_bits
)) : default_layer
);
140 if (current_layer
!= new_layer
) {
141 debug("Layer Switch(off): "); debug_hex(current_layer
);
142 debug(" -> "); debug_hex(new_layer
); debug("\n");
144 clear_keyboard_but_mods();
145 current_layer
= new_layer
;
151 static void register_code(uint8_t code
)
154 if (command_proc(code
)) {
158 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_FN(code
) {
166 host_add_key(keymap_fn_keycode(FN_INDEX(code
)));
167 host_send_keyboard_report();
169 else if IS_MOUSEKEY(code
) {
170 #ifdef MOUSEKEY_ENABLE
175 else if IS_CONSUMER(code
) {
181 case KC_AUDIO_VOL_UP
:
182 usage
= AUDIO_VOL_UP
;
184 case KC_AUDIO_VOL_DOWN
:
185 usage
= AUDIO_VOL_DOWN
;
187 case KC_MEDIA_NEXT_TRACK
:
188 usage
= TRANSPORT_NEXT_TRACK
;
190 case KC_MEDIA_PREV_TRACK
:
191 usage
= TRANSPORT_PREV_TRACK
;
194 usage
= TRANSPORT_STOP
;
196 case KC_MEDIA_PLAY_PAUSE
:
197 usage
= TRANSPORT_PLAY_PAUSE
;
199 case KC_MEDIA_SELECT
:
200 usage
= AL_CC_CONFIG
;
206 usage
= AL_CALCULATOR
;
209 usage
= AL_LOCAL_BROWSER
;
229 case KC_WWW_FAVORITES
:
230 usage
= AC_BOOKMARKS
;
233 host_consumer_send(usage
);
235 else if IS_SYSTEM(code
) {
238 case KC_SYSTEM_POWER
:
239 usage
= SYSTEM_POWER_DOWN
;
241 case KC_SYSTEM_SLEEP
:
242 usage
= SYSTEM_SLEEP
;
245 usage
= SYSTEM_WAKE_UP
;
248 host_system_send(usage
);
253 static void unregister_code(uint8_t code
)
257 host_send_keyboard_report();
259 else if IS_MOD(code
) {
260 host_del_mod_bit(MOD_BIT(code
));
261 host_send_keyboard_report();
263 else if IS_FN(code
) {
264 host_del_key(keymap_fn_keycode(FN_INDEX(code
)));
265 host_send_keyboard_report();
267 else if IS_MOUSEKEY(code
) {
268 #ifdef MOUSEKEY_ENABLE
273 else if IS_CONSUMER(code
) {
274 host_consumer_send(0x0000);
276 else if IS_SYSTEM(code
) {
277 host_system_send(0x0000);
283 * Event/State|IDLE PRESSING DELAYING[f] WAITING[f,k]
284 * -----------+------------------------------------------------------------------
285 * Fn Down |(L+) -*1 WAITING(Sk) IDLE(Rf,Ps)*7
286 * Up |(L-) IDLE(L-)*8 IDLE(L-)*8 IDLE(L-)*8
287 * Fnk Down |DELAYING(Sf)* (Rf) WAITING(Sk) IDLE(Rf,Ps,Rf)
288 * Up |(L-) IDLE(L-/Uf)*8 IDLE(Rf,Uf/L-)*3 IDLE(Rf,Ps,Uf/L-)*3
289 * Key Down |PRESSING(Rk) (Rk) WAITING(Sk) IDLE(Rf,Ps,Rk)
290 * Up |(Uk) IDLE(Uk)*4 (Uk) IDLE(L+,Ps,Pk)/(Uk)*a
292 * Delay |- - IDLE(L+) IDLE(L+,Ps)
293 * Magic Key |COMMAND*5
295 * *1: ignore Fn if other key is down.
296 * *2: register Fnk if any key is pressing
297 * *3: register/unregister delayed Fnk and move to IDLE if code == delayed Fnk, else *8
298 * *4: if no keys registered to host
299 * *5: unregister all keys
300 * *6: only if no keys down
301 * *7: ignore Fn because Fnk key and stored key are down.
302 * *8: move to IDLE if layer switch(off) occurs, else stay at current state
303 * *9: repeat key if pressing Fnk twice quickly(move to PRESSING)
304 * *a: layer switch and process waiting key and code if code == wainting key, else unregister key
307 * IDLE: No key is down except modifiers
308 * DELAYING: delay layer switch after pressing Fn with alt keycode
309 * WAITING: key is pressed during DELAYING
312 * Fn: Fn key without alternative keycode
313 * Fnk: Fn key with alternative keycode
315 * Delay: layer switch delay term is elapsed
320 * Rf: register Fn(alt keycode)
321 * Uf: unregister Fn(alt keycode)
322 * Rs: register stored key
323 * Us: unregister stored key
324 * Sk: Store key(waiting Key)
325 * Sf: Store Fn(delayed Fn)
326 * Ps: Process stored key
328 * Is: Interpret stored keys in current layer
329 * L+: Switch to new layer(*unregister* all keys but modifiers)
330 * L-: Switch back to last layer(*unregister* all keys but modifiers)
331 * Ld: Switch back to default layer(*unregister* all keys but modifiers)
333 #define NEXT(state) do { \
334 debug("NEXT: "); debug_P(state_str(kbdstate)); \
336 debug(" -> "); debug_P(state_str(kbdstate)); debug("\n"); \
339 static inline void process_key(keyevent_t event
)
341 uint8_t code
= keymap_get_keycode(current_layer
, event
.key
.row
, event
.key
.col
);
342 keykind_t kind
= get_keykind(code
, event
.pressed
);
346 debug("state: "); debug_P(state_str(kbdstate
));
347 debug(" kind: "); debug_hex(kind
);
348 debug(" code: "); debug_hex(code
);
349 if (event
.pressed
) { debug("d"); } else { debug("u"); }
356 layer_switch_on(code
);
359 layer_switch_off(code
);
362 // repeat Fn alt key when press Fn key down, up then down again quickly
363 if (KEYEQ(delayed_fn
.event
.key
, event
.key
) &&
364 timer_elapsed(delayed_fn
.time
) < LAYER_DELAY
) {
368 delayed_fn
= (keyrecord_t
) {
371 .mods
= keyboard_report
->mods
,
378 layer_switch_off(code
);
389 unregister_code(code
);
398 // ignored when any key is pressed
401 if (layer_switch_off(code
))
408 if (layer_switch_off(code
)) {
411 unregister_code(code
);
412 if (!anykey_sent_to_host())
422 unregister_code(code
);
423 if (!anykey_sent_to_host())
435 waiting_key
= (keyrecord_t
) {
438 .mods
= keyboard_report
->mods
,
447 if (layer_switch_off(code
))
451 if (code
== delayed_fn
.code
) {
452 // type Fn with alt keycode
453 // restore the mod status at the time of pressing Fn key
454 tmp_mods
= keyboard_report
->mods
;
455 host_set_mods(delayed_fn
.mods
);
456 register_code(delayed_fn
.code
);
457 unregister_code(delayed_fn
.code
);
458 host_set_mods(tmp_mods
);
461 if (layer_switch_off(code
))
467 unregister_code(code
);
478 tmp_mods
= keyboard_report
->mods
;
479 host_set_mods(delayed_fn
.mods
);
480 register_code(delayed_fn
.code
);
481 host_set_mods(waiting_key
.mods
);
482 register_code(waiting_key
.code
);
483 host_set_mods(tmp_mods
);
484 if (kind
== FN_DOWN
) {
486 } else if (kind
== FNK_DOWN
) {
488 } else if (kind
== KEY_DOWN
) {
497 if (layer_switch_off(code
))
501 if (code
== delayed_fn
.code
) {
502 // alt down, key down, alt up
503 tmp_mods
= keyboard_report
->mods
;
504 host_set_mods(delayed_fn
.mods
);
505 register_code(delayed_fn
.code
);
506 host_set_mods(waiting_key
.mods
);
507 register_code(waiting_key
.code
);
508 unregister_code(delayed_fn
.code
);
509 host_set_mods(tmp_mods
);
512 if (layer_switch_off(code
))
517 if (code
== waiting_key
.code
) {
518 layer_switch_on(delayed_fn
.code
);
520 // process waiting_key
521 tmp_mods
= keyboard_report
->mods
;
522 host_set_mods(waiting_key
.mods
);
523 process_key(waiting_key
.event
);
524 host_set_mods(tmp_mods
);
527 unregister_code(code
);
531 unregister_code(code
);
540 void keyboard_init(void)
542 debug_keyboard
= true;
546 #ifdef PS2_MOUSE_ENABLE
551 void keyboard_task(void)
553 static matrix_row_t matrix_prev
[MATRIX_ROWS
];
554 matrix_row_t matrix_row
= 0;
555 matrix_row_t matrix_change
= 0;
558 for (int r
= 0; r
< MATRIX_ROWS
; r
++) {
559 matrix_row
= matrix_get_row(r
);
560 matrix_change
= matrix_row
^ matrix_prev
[r
];
564 for (int c
= 0; c
< MATRIX_COLS
; c
++) {
565 if (matrix_change
& (1<<c
)) {
566 process_key((keyevent_t
){
567 .key
= (key_t
){ .row
= r
, .col
= c
},
568 .pressed
= (matrix_row
& (1<<c
))
570 // record a processed key
571 matrix_prev
[r
] ^= (1<<c
);
572 // process a key per task call
573 goto MATRIX_LOOP_END
;
580 // layer switch when delay term elapses
581 if (kbdstate
== DELAYING
|| kbdstate
== WAITING
) {
582 if (timer_elapsed(delayed_fn
.time
) > LAYER_DELAY
) {
583 if (kbdstate
== DELAYING
) {
584 layer_switch_on(delayed_fn
.code
);
587 if (kbdstate
== WAITING
) {
588 layer_switch_on(delayed_fn
.code
);
590 uint8_t tmp_mods
= keyboard_report
->mods
;
591 host_set_mods(waiting_key
.mods
);
592 process_key(waiting_key
.event
);
593 host_set_mods(tmp_mods
);
598 #ifdef MOUSEKEY_ENABLE
599 // mousekey repeat & acceleration
603 // FAIL SAFE: clear all key if no key down
605 matrix_row_t is_matrix_on
= 0;
606 for (int r
= 0; r
< MATRIX_ROWS
; r
++) {
607 is_matrix_on
|= matrix_get_row(r
);
610 debug("FAIL SAFE: clear all keys(default layer).\n");
612 current_layer
= default_layer
;
619 void keyboard_set_leds(uint8_t leds
)