diff options
| author | tmk <nobody@nowhere> | 2012-10-09 16:50:14 +0900 |
|---|---|---|
| committer | tmk <nobody@nowhere> | 2012-10-17 15:55:37 +0900 |
| commit | 71ac82337f803e8ec0c081b3221ac0ccf61035b0 (patch) | |
| tree | 5692c88c0b16d7f4992321c19a82f5193ebe4dc2 /common/keyboard.c | |
| parent | 373ab0e7192811944786c095facb80938c33f1d5 (diff) | |
| download | qmk_firmware-71ac82337f803e8ec0c081b3221ac0ccf61035b0.tar.gz qmk_firmware-71ac82337f803e8ec0c081b3221ac0ccf61035b0.zip | |
Clean host.h interface.
Diffstat (limited to 'common/keyboard.c')
| -rw-r--r-- | common/keyboard.c | 84 |
1 files changed, 47 insertions, 37 deletions
diff --git a/common/keyboard.c b/common/keyboard.c index 6adad8882..37d3b06ba 100644 --- a/common/keyboard.c +++ b/common/keyboard.c | |||
| @@ -81,17 +81,37 @@ static inline keykind_t get_keykind(uint8_t code, bool pressed) | |||
| 81 | return NONE; | 81 | return NONE; |
| 82 | } | 82 | } |
| 83 | 83 | ||
| 84 | static void clear_keyboard(void) | ||
| 85 | { | ||
| 86 | host_clear_keys(); | ||
| 87 | host_clear_mods(); | ||
| 88 | host_send_keyboard_report(); | ||
| 89 | |||
| 90 | host_system_send(0); | ||
| 91 | host_consumer_send(0); | ||
| 92 | |||
| 93 | mousekey_clear(); | ||
| 94 | mousekey_send(); | ||
| 95 | } | ||
| 96 | |||
| 97 | static void clear_keyboard_but_mods(void) | ||
| 98 | { | ||
| 99 | host_clear_keys(); | ||
| 100 | host_send_keyboard_report(); | ||
| 101 | |||
| 102 | host_system_send(0); | ||
| 103 | host_consumer_send(0); | ||
| 104 | |||
| 105 | mousekey_clear(); | ||
| 106 | mousekey_send(); | ||
| 107 | } | ||
| 108 | |||
| 84 | static void layer_switch_on(uint8_t code) | 109 | static void layer_switch_on(uint8_t code) |
| 85 | { | 110 | { |
| 86 | if (!IS_FN(code)) return; | 111 | if (!IS_FN(code)) return; |
| 87 | fn_state_bits |= FN_BIT(code); | 112 | fn_state_bits |= FN_BIT(code); |
| 88 | if (current_layer != keymap_fn_layer(FN_INDEX(code))) { | 113 | if (current_layer != keymap_fn_layer(FN_INDEX(code))) { |
| 89 | // clear all key execpt Mod key | 114 | clear_keyboard_but_mods(); |
| 90 | host_clear_all_keys_but_mods(); | ||
| 91 | host_system_send(0); | ||
| 92 | host_consumer_send(0); | ||
| 93 | mousekey_clear(); | ||
| 94 | mousekey_send(); | ||
| 95 | 115 | ||
| 96 | debug("Layer Switch(on): "); debug_hex(current_layer); | 116 | debug("Layer Switch(on): "); debug_hex(current_layer); |
| 97 | current_layer = keymap_fn_layer(FN_INDEX(code)); | 117 | current_layer = keymap_fn_layer(FN_INDEX(code)); |
| @@ -104,12 +124,7 @@ static void layer_switch_off(uint8_t code) | |||
| 104 | if (!IS_FN(code)) return; | 124 | if (!IS_FN(code)) return; |
| 105 | fn_state_bits &= ~FN_BIT(code); | 125 | fn_state_bits &= ~FN_BIT(code); |
| 106 | if (current_layer != keymap_fn_layer(biton(fn_state_bits))) { | 126 | if (current_layer != keymap_fn_layer(biton(fn_state_bits))) { |
| 107 | // clear all key execpt Mod key | 127 | clear_keyboard_but_mods(); |
| 108 | host_clear_all_keys_but_mods(); | ||
| 109 | host_system_send(0); | ||
| 110 | host_consumer_send(0); | ||
| 111 | mousekey_clear(); | ||
| 112 | mousekey_send(); | ||
| 113 | 128 | ||
| 114 | debug("Layer Switch(off): "); debug_hex(current_layer); | 129 | debug("Layer Switch(off): "); debug_hex(current_layer); |
| 115 | current_layer = keymap_fn_layer(biton(fn_state_bits)); | 130 | current_layer = keymap_fn_layer(biton(fn_state_bits)); |
| @@ -117,11 +132,6 @@ static void layer_switch_off(uint8_t code) | |||
| 117 | } | 132 | } |
| 118 | } | 133 | } |
| 119 | 134 | ||
| 120 | static inline uint8_t get_keycode(key_t key) | ||
| 121 | { | ||
| 122 | return keymap_get_keycode(current_layer, key.row, key.col); | ||
| 123 | } | ||
| 124 | |||
| 125 | // whether any key except modifier is down or not | 135 | // whether any key except modifier is down or not |
| 126 | static inline bool is_anykey_down(void) | 136 | static inline bool is_anykey_down(void) |
| 127 | { | 137 | { |
| @@ -129,7 +139,7 @@ static inline bool is_anykey_down(void) | |||
| 129 | matrix_row_t matrix_row = matrix_get_row(r); | 139 | matrix_row_t matrix_row = matrix_get_row(r); |
| 130 | for (int c = 0; c < MATRIX_COLS; c++) { | 140 | for (int c = 0; c < MATRIX_COLS; c++) { |
| 131 | if (matrix_row && (1<<c)) { | 141 | if (matrix_row && (1<<c)) { |
| 132 | if (IS_KEY(get_keycode((key_t){ .row = r, .col = c }))) { | 142 | if (IS_KEY(keymap_get_keycode(current_layer, r, c))) { |
| 133 | return true; | 143 | return true; |
| 134 | } | 144 | } |
| 135 | } | 145 | } |
| @@ -140,7 +150,6 @@ static inline bool is_anykey_down(void) | |||
| 140 | 150 | ||
| 141 | static void register_code(uint8_t code) | 151 | static void register_code(uint8_t code) |
| 142 | { | 152 | { |
| 143 | debug("register_code\n"); | ||
| 144 | if IS_KEY(code) { | 153 | if IS_KEY(code) { |
| 145 | host_add_key(code); | 154 | host_add_key(code); |
| 146 | host_send_keyboard_report(); | 155 | host_send_keyboard_report(); |
| @@ -154,7 +163,6 @@ debug("register_code\n"); | |||
| 154 | mousekey_send(); | 163 | mousekey_send(); |
| 155 | } | 164 | } |
| 156 | else if IS_CONSUMER(code) { | 165 | else if IS_CONSUMER(code) { |
| 157 | debug("consumer\n"); | ||
| 158 | uint16_t usage = 0; | 166 | uint16_t usage = 0; |
| 159 | switch (code) { | 167 | switch (code) { |
| 160 | case KC_AUDIO_MUTE: | 168 | case KC_AUDIO_MUTE: |
| @@ -212,7 +220,6 @@ debug("consumer\n"); | |||
| 212 | usage = AC_BOOKMARKS; | 220 | usage = AC_BOOKMARKS; |
| 213 | break; | 221 | break; |
| 214 | } | 222 | } |
| 215 | debug("usage: "); phex16(usage); debug("\n"); | ||
| 216 | host_consumer_send(usage); | 223 | host_consumer_send(usage); |
| 217 | } | 224 | } |
| 218 | else if IS_SYSTEM(code) { | 225 | else if IS_SYSTEM(code) { |
| @@ -293,9 +300,9 @@ static void unregister_code(uint8_t code) | |||
| 293 | * Sk: store key | 300 | * Sk: store key |
| 294 | * Sf: store Fn | 301 | * Sf: store Fn |
| 295 | * Ps: play stored key(Interpret stored key and transit state) | 302 | * Ps: play stored key(Interpret stored key and transit state) |
| 296 | * L+: Switch to new layer(*retain* Modifiers only) | 303 | * L+: Switch to new layer(*unregister* all keys but modifiers) |
| 297 | * L-: Switch back to last layer(*clear* stored key/Fn, *unregister* all Modifier/key) | 304 | * L-: Switch back to last layer(*unregister* all keys but modifiers) |
| 298 | * Ld: Switch back to default layer(*clear* stored key/Fn, *unregister* all Modifier/key) | 305 | * Ld: Switch back to default layer(*unregister* all keys but modifiers) |
| 299 | */ | 306 | */ |
| 300 | #define NEXT(state) do { \ | 307 | #define NEXT(state) do { \ |
| 301 | debug("NEXT: "); print_P(state_str(kbdstate)); \ | 308 | debug("NEXT: "); print_P(state_str(kbdstate)); \ |
| @@ -305,13 +312,7 @@ static void unregister_code(uint8_t code) | |||
| 305 | 312 | ||
| 306 | static inline void process_key(keyevent_t event) | 313 | static inline void process_key(keyevent_t event) |
| 307 | { | 314 | { |
| 308 | /* TODO: ring buffer | 315 | uint8_t code = keymap_get_keycode(current_layer, event.key.row, event.key.col); |
| 309 | static keyrecord_t waiting_keys[5]; | ||
| 310 | static uint8_t waiting_keys_head = 0; | ||
| 311 | static uint8_t waiting_keys_tail = 0; | ||
| 312 | */ | ||
| 313 | |||
| 314 | uint8_t code = get_keycode(event.key); | ||
| 315 | keykind_t kind = get_keykind(code, event.pressed); | 316 | keykind_t kind = get_keykind(code, event.pressed); |
| 316 | 317 | ||
| 317 | uint8_t tmp_mods; | 318 | uint8_t tmp_mods; |
| @@ -502,8 +503,6 @@ static inline void process_key(keyevent_t event) | |||
| 502 | } | 503 | } |
| 503 | break; | 504 | break; |
| 504 | } | 505 | } |
| 505 | |||
| 506 | // TODO: FAIL SAFE: unregister all keys when no key down | ||
| 507 | } | 506 | } |
| 508 | 507 | ||
| 509 | void keyboard_init(void) | 508 | void keyboard_init(void) |
| @@ -526,11 +525,11 @@ void keyboard_task(void) | |||
| 526 | matrix_scan(); | 525 | matrix_scan(); |
| 527 | if (command_proc()) { | 526 | if (command_proc()) { |
| 528 | debug("COMMAND\n"); | 527 | debug("COMMAND\n"); |
| 529 | // TODO: clear all keys | 528 | // TODO: COMMAND state? |
| 530 | host_clear_keyboard_report(); | 529 | clear_keyboard(); |
| 531 | host_send_keyboard_report(); | ||
| 532 | return; | 530 | return; |
| 533 | } | 531 | } |
| 532 | |||
| 534 | for (int r = 0; r < MATRIX_ROWS; r++) { | 533 | for (int r = 0; r < MATRIX_ROWS; r++) { |
| 535 | matrix_row = matrix_get_row(r); | 534 | matrix_row = matrix_get_row(r); |
| 536 | matrix_change = matrix_row ^ matrix_prev[r]; | 535 | matrix_change = matrix_row ^ matrix_prev[r]; |
| @@ -552,7 +551,6 @@ void keyboard_task(void) | |||
| 552 | } | 551 | } |
| 553 | } | 552 | } |
| 554 | MATRIX_LOOP_END: | 553 | MATRIX_LOOP_END: |
| 555 | // TODO: FAIL SAFE: clear all key if no key down | ||
| 556 | 554 | ||
| 557 | // layer switch when delay term elapses | 555 | // layer switch when delay term elapses |
| 558 | if (kbdstate == DELAYING || kbdstate == WAITING) { | 556 | if (kbdstate == DELAYING || kbdstate == WAITING) { |
| @@ -575,6 +573,18 @@ void keyboard_task(void) | |||
| 575 | // mousekey repeat & acceleration | 573 | // mousekey repeat & acceleration |
| 576 | mousekey_task(); | 574 | mousekey_task(); |
| 577 | 575 | ||
| 576 | // FAIL SAFE: clear all key if no key down | ||
| 577 | if (matrix_change) { | ||
| 578 | matrix_row_t is_matrix_on = 0; | ||
| 579 | for (int r = 0; r < MATRIX_ROWS; r++) { | ||
| 580 | is_matrix_on |= matrix_get_row(r); | ||
| 581 | } | ||
| 582 | if (!is_matrix_on) { | ||
| 583 | debug("FAIL SAFE: clear all keys.\n"); | ||
| 584 | clear_keyboard(); | ||
| 585 | } | ||
| 586 | } | ||
| 587 | |||
| 578 | return; | 588 | return; |
| 579 | } | 589 | } |
| 580 | 590 | ||
