From 4ae979f6ef8dbf9e1d1f35be15322ad6d02e2958 Mon Sep 17 00:00:00 2001 From: tmk Date: Sat, 6 Oct 2012 02:23:12 +0900 Subject: Initial version of new code for layer switch is added. --- common/keyboard.c | 543 +++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 413 insertions(+), 130 deletions(-) mode change 100644 => 100755 common/keyboard.c (limited to 'common/keyboard.c') diff --git a/common/keyboard.c b/common/keyboard.c old mode 100644 new mode 100755 index 25f32eb02..9f0c27670 --- a/common/keyboard.c +++ b/common/keyboard.c @@ -15,15 +15,16 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ #include "keyboard.h" -#include "host.h" -#include "layer.h" #include "matrix.h" +#include "keymap.h" +#include "host.h" #include "led.h" #include "usb_keycodes.h" #include "timer.h" #include "print.h" #include "debug.h" #include "command.h" +#include "util.h" #ifdef MOUSEKEY_ENABLE #include "mousekey.h" #endif @@ -32,162 +33,444 @@ along with this program. If not, see . #endif -static uint8_t last_leds = 0; +#define LAYER_DELAY 250 +typedef enum keykind { + NONE, + FN_DOWN, FN_UP, + FNK_DOWN, FNK_UP, + KEY_DOWN, KEY_UP, + MOD_DOWN, MOD_UP, + MOUSEKEY_DOWN, MOUSEKEY_UP, + DELAY +} keykind_t; -void keyboard_init(void) +typedef enum { IDLE, DELAYING, WAITING, PRESSING } kbdstate_t; + + +uint8_t current_layer = 0; +uint8_t default_layer = 0; + +/* keyboard internal states */ +static kbdstate_t kbdstate = IDLE; +static uint8_t fn_state_bits = 0; +static keyrecord_t delayed_fn; +static keyrecord_t waiting_key; + + +static const char *state_str(kbdstate_t state) { - timer_init(); - matrix_init(); -#ifdef PS2_MOUSE_ENABLE - ps2_mouse_init(); -#endif + if (state == IDLE) return PSTR("IDLE"); + if (state == DELAYING) return PSTR("DELAYING"); + if (state == WAITING) return PSTR("WAITING"); + if (state == PRESSING) return PSTR("PRESSING"); + return PSTR("UNKNOWN"); } -void keyboard_proc(void) +static inline keykind_t get_keykind(uint8_t code, bool pressed) { - uint8_t fn_bits = 0; -#ifdef EXTRAKEY_ENABLE - uint16_t consumer_code = 0; - uint16_t system_code = 0; -#endif - - matrix_scan(); + if IS_KEY(code) return (pressed ? KEY_DOWN : KEY_UP); + if IS_MOD(code) return (pressed ? MOD_DOWN : MOD_UP); + if IS_FN(code) { + if (keymap_fn_keycode(FN_INDEX(code))) + return (pressed ? FNK_DOWN : FNK_UP); + else + return (pressed ? FN_DOWN : FN_UP); + } + if IS_MOUSEKEY(code) return (pressed ? MOUSEKEY_DOWN : MOUSEKEY_UP); + return NONE; +} - if (matrix_is_modified()) { - if (debug_matrix) matrix_print(); -#ifdef DEBUG_LED - // LED flash for debug - DEBUG_LED_CONFIG; - DEBUG_LED_ON; -#endif +static void layer_switch_on(uint8_t code) +{ + if (!IS_FN(code)) return; + fn_state_bits |= FN_BIT(code); + if (current_layer != keymap_fn_layer(FN_INDEX(code))) { + //TODO: clear all key execpt Mod key + debug("Layer Switch(on): "); debug_hex(current_layer); + current_layer = keymap_fn_layer(FN_INDEX(code)); + debug(" -> "); debug_hex(current_layer); debug("\n"); } +} - if (matrix_has_ghost()) { - // should send error? - debug("matrix has ghost!!\n"); - return; +static void layer_switch_off(uint8_t code) +{ + if (!IS_FN(code)) return; + fn_state_bits &= ~FN_BIT(code); + if (current_layer != keymap_fn_layer(biton(fn_state_bits))) { + //TODO: clear all key execpt Mod key + debug("Layer Switch(off): "); debug_hex(current_layer); + current_layer = keymap_fn_layer(biton(fn_state_bits)); + debug(" -> "); debug_hex(current_layer); debug("\n"); } +} - host_swap_keyboard_report(); - host_clear_keyboard_report(); - for (int row = 0; row < matrix_rows(); row++) { - for (int col = 0; col < matrix_cols(); col++) { - if (!matrix_is_on(row, col)) continue; - - uint8_t code = layer_get_keycode(row, col); - if (code == KB_NO) { - // do nothing - } else if (IS_MOD(code)) { - host_add_mod_bit(MOD_BIT(code)); - } else if (IS_FN(code)) { - fn_bits |= FN_BIT(code); - } -// TODO: use table or something -#ifdef EXTRAKEY_ENABLE - // System Control - else if (code == KB_SYSTEM_POWER) { -#ifdef HOST_PJRC - if (suspend && remote_wakeup) { - usb_remote_wakeup(); +static inline uint8_t get_keycode(key_t key) +{ + return keymap_get_keycode(current_layer, key.row, key.col); +} + +// whether any key except modifier is down or not +static inline bool is_anykey_down(void) +{ + for (int r = 0; r < MATRIX_ROWS; r++) { + matrix_row_t matrix_row = matrix_get_row(r); + for (int c = 0; c < MATRIX_COLS; c++) { + if (matrix_row && (1< "); print_P(state_str(kbdstate)); debug("\n"); \ +} while (0) + +static inline void process_key(keyevent_t event) +{ + + /* TODO: ring buffer + static keyrecord_t waiting_keys[5]; + static uint8_t waiting_keys_head = 0; + static uint8_t waiting_keys_tail = 0; + */ + + uint8_t code = get_keycode(event.key); + keykind_t kind = get_keykind(code, event.pressed); + + uint8_t tmp_mods; + + //debug("kbdstate: "); debug_hex(kbdstate); + debug("state: "); print_P(state_str(kbdstate)); + debug(" kind: "); debug_hex(kind); + debug(" code: "); debug_hex(code); + if (event.pressed) { debug("d"); } else { debug("u"); } + debug("\n"); + switch (kbdstate) { + case IDLE: + switch (kind) { + case FN_DOWN: + layer_switch_on(code); + break; + case FN_UP: + layer_switch_off(code); + break; + case FNK_DOWN: + // store event + delayed_fn = (keyrecord_t) { .event = event, .code = code, .mods = keyboard_report->mods, .time = timer_read() }; + NEXT(DELAYING); + break; + case FNK_UP: + layer_switch_off(code); + break; + case KEY_DOWN: + case MOUSEKEY_DOWN: + register_code(code); + NEXT(PRESSING); + break; + case MOD_DOWN: + register_code(code); + break; + case KEY_UP: + case MOUSEKEY_UP: + case MOD_UP: + unregister_code(code); + break; + default: + break; } -#endif - else if (IS_KEY(code)) { - host_add_key(code); + break; + case PRESSING: + switch (kind) { + case FN_DOWN: + // ignored when any key is pressed + break; + case FN_UP: + layer_switch_off(code); + NEXT(IDLE); + break; + case FNK_DOWN: + register_code(keymap_fn_keycode(FN_INDEX(code))); + break; + case FNK_UP: + unregister_code(keymap_fn_keycode(FN_INDEX(code))); + break; + case KEY_DOWN: + case MOD_DOWN: + case MOUSEKEY_DOWN: + register_code(code); + break; + case KEY_UP: + case MOD_UP: + case MOUSEKEY_UP: + unregister_code(code); + // no key registered? mousekey, mediakey, systemkey + if (!host_has_anykey()) + NEXT(IDLE); + break; + default: + break; } -#ifdef MOUSEKEY_ENABLE - else if (IS_MOUSEKEY(code)) { - mousekey_decode(code); + break; + case DELAYING: + switch (kind) { + case FN_DOWN: + case FNK_DOWN: + case KEY_DOWN: + case MOUSEKEY_DOWN: + waiting_key = (keyrecord_t) { .event = event, .code = code, .mods = keyboard_report->mods, .time = timer_read() }; + NEXT(WAITING); + break; + case MOD_DOWN: + register_code(code); + break; + case FN_UP: + layer_switch_off(code); + NEXT(IDLE); + break; + case FNK_UP: + if (code == delayed_fn.code) { + // type Fn with alt keycode + // restore the mod status at the time of pressing Fn key + tmp_mods = keyboard_report->mods; + host_set_mods(delayed_fn.mods); + register_code(keymap_fn_keycode(FN_INDEX(delayed_fn.code))); + unregister_code(keymap_fn_keycode(FN_INDEX(delayed_fn.code))); + host_set_mods(tmp_mods); + NEXT(IDLE); + } else { + layer_switch_off(code); + NEXT(IDLE); + } + break; + case KEY_UP: + case MOUSEKEY_UP: + unregister_code(code); + NEXT(IDLE); + break; + case MOD_UP: + unregister_code(code); + break; + default: + break; } -#endif - else { - debug("ignore keycode: "); debug_hex(code); debug("\n"); + break; + case WAITING: + switch (kind) { + case FN_DOWN: + case FNK_DOWN: + case KEY_DOWN: + case MOUSEKEY_DOWN: + tmp_mods = keyboard_report->mods; + host_set_mods(delayed_fn.mods); + register_code(keymap_fn_keycode(FN_INDEX(delayed_fn.code))); + host_set_mods(waiting_key.mods); + register_code(waiting_key.code); + host_set_mods(tmp_mods); + register_code(code); + NEXT(IDLE); + break; + case MOD_DOWN: + register_code(code); + break; + case FN_UP: + layer_switch_off(code); + NEXT(IDLE); + break; + case FNK_UP: + if (code == delayed_fn.code) { + // alt down, key down, alt up + tmp_mods = keyboard_report->mods; + host_set_mods(delayed_fn.mods); + register_code(keymap_fn_keycode(FN_INDEX(delayed_fn.code))); + host_set_mods(waiting_key.mods); + register_code(waiting_key.code); + unregister_code(keymap_fn_keycode(FN_INDEX(delayed_fn.code))); + host_set_mods(tmp_mods); + NEXT(IDLE); + } else { + layer_switch_off(code); + NEXT(IDLE); + } + break; + case KEY_UP: + case MOUSEKEY_UP: + if (code == waiting_key.code) { + layer_switch_on(delayed_fn.code); + NEXT(IDLE); + // process waiting_key + tmp_mods = keyboard_report->mods; + host_set_mods(waiting_key.mods); + process_key(waiting_key.event); + host_set_mods(tmp_mods); + process_key(event); + } else { + unregister_code(code); + } + break; + case MOD_UP: + unregister_code(code); + break; + default: + break; } - } + break; } - layer_switching(fn_bits); + // TODO: FAIL SAFE: unregister all keys when no key down +} + +void keyboard_init(void) +{ + debug_keyboard = true; + + timer_init(); + matrix_init(); +#ifdef PS2_MOUSE_ENABLE + ps2_mouse_init(); +#endif +} + +void keyboard_task(void) +{ + static matrix_row_t matrix_prev[MATRIX_ROWS]; + matrix_row_t matrix_row = 0; + matrix_row_t matrix_change = 0; + matrix_scan(); if (command_proc()) { + debug("COMMAND\n"); + // TODO: clear all keys + host_clear_keyboard_report(); + host_send_keyboard_report(); return; } + for (int r = 0; r < MATRIX_ROWS; r++) { + matrix_row = matrix_get_row(r); + matrix_change = matrix_row ^ matrix_prev[r]; + if (matrix_change) { + // TODO: print once per scan + if (debug_matrix) matrix_print(); - // TODO: should send only when changed from last report - if (matrix_is_modified()) { - host_send_keyboard_report(); -#ifdef EXTRAKEY_ENABLE - host_consumer_send(consumer_code); - host_system_send(system_code); -#endif -#ifdef DEBUG_LED - // LED flash for debug - DEBUG_LED_CONFIG; - DEBUG_LED_OFF; -#endif + for (int c = 0; c < MATRIX_COLS; c++) { + if (matrix_change & (1< LAYER_DELAY) { + if (kbdstate == DELAYING) { + layer_switch_on(delayed_fn.code); + NEXT(IDLE); + } + if (kbdstate == WAITING) { + layer_switch_on(delayed_fn.code); + NEXT(IDLE); + uint8_t tmp_mods = keyboard_report->mods; + host_set_mods(waiting_key.mods); + process_key(waiting_key.event); + host_set_mods(tmp_mods); + } + } + } -#ifdef PS2_MOUSE_ENABLE - // TODO: should comform new API - if (ps2_mouse_read() == 0) - ps2_mouse_usb_send(); -#endif + // mousekey repeat & acceleration + mousekey_task(); - if (last_leds != host_keyboard_leds()) { - keyboard_set_leds(host_keyboard_leds()); - last_leds = host_keyboard_leds(); - } + return; } void keyboard_set_leds(uint8_t leds) -- cgit v1.2.3 From e9af482690152f1beedbbb915791eccd2d5c22d1 Mon Sep 17 00:00:00 2001 From: tmk Date: Sun, 7 Oct 2012 12:25:18 +0900 Subject: Add repeating of Fn key with alt keycode. --- common/keyboard.c | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) mode change 100755 => 100644 common/keyboard.c (limited to 'common/keyboard.c') diff --git a/common/keyboard.c b/common/keyboard.c old mode 100755 new mode 100644 index 9f0c27670..b7063a264 --- a/common/keyboard.c +++ b/common/keyboard.c @@ -163,7 +163,7 @@ static void unregister_code(uint8_t code) * Event/State|IDLE DELAYING[f] WAITING[f,k] PRESSING * -----------+------------------------------------------------------------------ * Fn Down |IDLE(L+) WAITING(Sk) WAITING(Sk) - - * Up |IDLE(L-) IDLE(L-) IDLE(L-) IDLE(L-) + * Up |IDLE(L-) IDLE(L-) IDLE(L-) IDLE(L-) * Fnk Down |DELAYING(Sf) WAITING(Sk) WAINTING(Sk) PRESSING(Rf) * Up |IDLE(L-) IDLE(Rf,Uf) IDLE(Rf,Ps,Uf)*3 PRESSING(Uf) * Key Down |PRESSING(Rk) WAITING(Sk) WAITING(Sk) PRESSING(Rk) @@ -208,7 +208,6 @@ static void unregister_code(uint8_t code) static inline void process_key(keyevent_t event) { - /* TODO: ring buffer static keyrecord_t waiting_keys[5]; static uint8_t waiting_keys_head = 0; @@ -220,12 +219,12 @@ static inline void process_key(keyevent_t event) uint8_t tmp_mods; - //debug("kbdstate: "); debug_hex(kbdstate); debug("state: "); print_P(state_str(kbdstate)); debug(" kind: "); debug_hex(kind); debug(" code: "); debug_hex(code); if (event.pressed) { debug("d"); } else { debug("u"); } debug("\n"); + switch (kbdstate) { case IDLE: switch (kind) { @@ -236,9 +235,20 @@ static inline void process_key(keyevent_t event) layer_switch_off(code); break; case FNK_DOWN: - // store event - delayed_fn = (keyrecord_t) { .event = event, .code = code, .mods = keyboard_report->mods, .time = timer_read() }; - NEXT(DELAYING); + // repeat Fn alt key when press Fn key down, up then down again quickly + if (KEYEQ(delayed_fn.event.key, event.key) && + timer_elapsed(delayed_fn.time) < LAYER_DELAY) { + register_code(keymap_fn_keycode(FN_INDEX(code))); + NEXT(PRESSING); + } else { + delayed_fn = (keyrecord_t) { + .event = event, + .code = code, + .mods = keyboard_report->mods, + .time = timer_read() + }; + NEXT(DELAYING); + } break; case FNK_UP: layer_switch_off(code); @@ -298,7 +308,12 @@ static inline void process_key(keyevent_t event) case FNK_DOWN: case KEY_DOWN: case MOUSEKEY_DOWN: - waiting_key = (keyrecord_t) { .event = event, .code = code, .mods = keyboard_report->mods, .time = timer_read() }; + waiting_key = (keyrecord_t) { + .event = event, + .code = code, + .mods = keyboard_report->mods, + .time = timer_read() + }; NEXT(WAITING); break; case MOD_DOWN: -- cgit v1.2.3 From 16ba9bda5601ebef6e4db04a5ad079af32370815 Mon Sep 17 00:00:00 2001 From: tmk Date: Tue, 9 Oct 2012 13:48:39 +0900 Subject: Add consumer/system usage support. --- common/keyboard.c | 122 +++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 106 insertions(+), 16 deletions(-) (limited to 'common/keyboard.c') diff --git a/common/keyboard.c b/common/keyboard.c index b7063a264..328941df3 100644 --- a/common/keyboard.c +++ b/common/keyboard.c @@ -41,8 +41,6 @@ typedef enum keykind { FNK_DOWN, FNK_UP, KEY_DOWN, KEY_UP, MOD_DOWN, MOD_UP, - MOUSEKEY_DOWN, MOUSEKEY_UP, - DELAY } keykind_t; typedef enum { IDLE, DELAYING, WAITING, PRESSING } kbdstate_t; @@ -69,15 +67,17 @@ static const char *state_str(kbdstate_t state) static inline keykind_t get_keykind(uint8_t code, bool pressed) { - if IS_KEY(code) return (pressed ? KEY_DOWN : KEY_UP); - if IS_MOD(code) return (pressed ? MOD_DOWN : MOD_UP); + if IS_KEY(code) return (pressed ? KEY_DOWN : KEY_UP); + if IS_MOD(code) return (pressed ? MOD_DOWN : MOD_UP); if IS_FN(code) { if (keymap_fn_keycode(FN_INDEX(code))) return (pressed ? FNK_DOWN : FNK_UP); else return (pressed ? FN_DOWN : FN_UP); } - if IS_MOUSEKEY(code) return (pressed ? MOUSEKEY_DOWN : MOUSEKEY_UP); + if IS_MOUSEKEY(code) return (pressed ? KEY_DOWN : KEY_UP); + if IS_SYSTEM(code) return (pressed ? KEY_DOWN : KEY_UP); + if IS_CONSUMER(code) return (pressed ? KEY_DOWN : KEY_UP); return NONE; } @@ -86,7 +86,13 @@ static void layer_switch_on(uint8_t code) if (!IS_FN(code)) return; fn_state_bits |= FN_BIT(code); if (current_layer != keymap_fn_layer(FN_INDEX(code))) { - //TODO: clear all key execpt Mod key + // clear all key execpt Mod key + host_clear_all_keys_but_mods(); + host_system_send(0); + host_consumer_send(0); + mousekey_clear(); + mousekey_send(); + debug("Layer Switch(on): "); debug_hex(current_layer); current_layer = keymap_fn_layer(FN_INDEX(code)); debug(" -> "); debug_hex(current_layer); debug("\n"); @@ -98,7 +104,13 @@ static void layer_switch_off(uint8_t code) if (!IS_FN(code)) return; fn_state_bits &= ~FN_BIT(code); if (current_layer != keymap_fn_layer(biton(fn_state_bits))) { - //TODO: clear all key execpt Mod key + // clear all key execpt Mod key + host_clear_all_keys_but_mods(); + host_system_send(0); + host_consumer_send(0); + mousekey_clear(); + mousekey_send(); + debug("Layer Switch(off): "); debug_hex(current_layer); current_layer = keymap_fn_layer(biton(fn_state_bits)); debug(" -> "); debug_hex(current_layer); debug("\n"); @@ -128,6 +140,7 @@ static inline bool is_anykey_down(void) static void register_code(uint8_t code) { +debug("register_code\n"); if IS_KEY(code) { host_add_key(code); host_send_keyboard_report(); @@ -140,6 +153,84 @@ static void register_code(uint8_t code) mousekey_on(code); mousekey_send(); } + else if IS_CONSUMER(code) { +debug("consumer\n"); + uint16_t usage = 0; + switch (code) { + case KB_AUDIO_MUTE: + usage = AUDIO_MUTE; + break; + case KB_AUDIO_VOL_UP: + usage = AUDIO_VOL_UP; + break; + case KB_AUDIO_VOL_DOWN: + usage = AUDIO_VOL_DOWN; + break; + case KB_MEDIA_NEXT_TRACK: + usage = TRANSPORT_NEXT_TRACK; + break; + case KB_MEDIA_PREV_TRACK: + usage = TRANSPORT_PREV_TRACK; + break; + case KB_MEDIA_STOP: + usage = TRANSPORT_STOP; + break; + case KB_MEDIA_PLAY_PAUSE: + usage = TRANSPORT_PLAY_PAUSE; + break; + case KB_MEDIA_SELECT: + usage = AL_CC_CONFIG; + break; + case KB_MAIL: + usage = AL_EMAIL; + break; + case KB_CALCULATOR: + usage = AL_CALCULATOR; + break; + case KB_MY_COMPUTER: + usage = AL_LOCAL_BROWSER; + break; + case KB_WWW_SEARCH: + usage = AC_SEARCH; + break; + case KB_WWW_HOME: + usage = AC_HOME; + break; + case KB_WWW_BACK: + usage = AC_BACK; + break; + case KB_WWW_FORWARD: + usage = AC_FORWARD; + break; + case KB_WWW_STOP: + usage = AC_STOP; + break; + case KB_WWW_REFRESH: + usage = AC_REFRESH; + break; + case KB_WWW_FAVORITES: + usage = AC_BOOKMARKS; + break; + } +debug("usage: "); phex16(usage); debug("\n"); + host_consumer_send(usage); + } + else if IS_SYSTEM(code) { + uint16_t usage = 0; + switch (code) { + case KB_SYSTEM_POWER: + usage = SYSTEM_POWER_DOWN; + break; + case KB_SYSTEM_SLEEP: + usage = SYSTEM_SLEEP; + break; + case KB_SYSTEM_WAKE: + usage = SYSTEM_WAKE_UP; + break; + } + host_system_send(usage); + } + } static void unregister_code(uint8_t code) @@ -156,6 +247,12 @@ static void unregister_code(uint8_t code) mousekey_off(code); mousekey_send(); } + else if IS_CONSUMER(code) { + host_consumer_send(0x0000); + } + else if IS_SYSTEM(code) { + host_system_send(0x0000); + } } /* @@ -254,7 +351,6 @@ static inline void process_key(keyevent_t event) layer_switch_off(code); break; case KEY_DOWN: - case MOUSEKEY_DOWN: register_code(code); NEXT(PRESSING); break; @@ -262,7 +358,6 @@ static inline void process_key(keyevent_t event) register_code(code); break; case KEY_UP: - case MOUSEKEY_UP: case MOD_UP: unregister_code(code); break; @@ -283,16 +378,16 @@ static inline void process_key(keyevent_t event) register_code(keymap_fn_keycode(FN_INDEX(code))); break; case FNK_UP: + // can't know whether layer switched or not + layer_switch_off(code); unregister_code(keymap_fn_keycode(FN_INDEX(code))); break; case KEY_DOWN: case MOD_DOWN: - case MOUSEKEY_DOWN: register_code(code); break; case KEY_UP: case MOD_UP: - case MOUSEKEY_UP: unregister_code(code); // no key registered? mousekey, mediakey, systemkey if (!host_has_anykey()) @@ -307,7 +402,6 @@ static inline void process_key(keyevent_t event) case FN_DOWN: case FNK_DOWN: case KEY_DOWN: - case MOUSEKEY_DOWN: waiting_key = (keyrecord_t) { .event = event, .code = code, @@ -339,7 +433,6 @@ static inline void process_key(keyevent_t event) } break; case KEY_UP: - case MOUSEKEY_UP: unregister_code(code); NEXT(IDLE); break; @@ -355,7 +448,6 @@ static inline void process_key(keyevent_t event) case FN_DOWN: case FNK_DOWN: case KEY_DOWN: - case MOUSEKEY_DOWN: tmp_mods = keyboard_report->mods; host_set_mods(delayed_fn.mods); register_code(keymap_fn_keycode(FN_INDEX(delayed_fn.code))); @@ -389,7 +481,6 @@ static inline void process_key(keyevent_t event) } break; case KEY_UP: - case MOUSEKEY_UP: if (code == waiting_key.code) { layer_switch_on(delayed_fn.code); NEXT(IDLE); @@ -444,7 +535,6 @@ void keyboard_task(void) matrix_row = matrix_get_row(r); matrix_change = matrix_row ^ matrix_prev[r]; if (matrix_change) { - // TODO: print once per scan if (debug_matrix) matrix_print(); for (int c = 0; c < MATRIX_COLS; c++) { -- cgit v1.2.3 From 373ab0e7192811944786c095facb80938c33f1d5 Mon Sep 17 00:00:00 2001 From: tmk Date: Tue, 9 Oct 2012 14:36:13 +0900 Subject: Add keycode.h and remove usb_keycodes.h. --- common/keyboard.c | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) (limited to 'common/keyboard.c') diff --git a/common/keyboard.c b/common/keyboard.c index 328941df3..6adad8882 100644 --- a/common/keyboard.c +++ b/common/keyboard.c @@ -19,7 +19,7 @@ along with this program. If not, see . #include "keymap.h" #include "host.h" #include "led.h" -#include "usb_keycodes.h" +#include "keycode.h" #include "timer.h" #include "print.h" #include "debug.h" @@ -157,58 +157,58 @@ debug("register_code\n"); debug("consumer\n"); uint16_t usage = 0; switch (code) { - case KB_AUDIO_MUTE: + case KC_AUDIO_MUTE: usage = AUDIO_MUTE; break; - case KB_AUDIO_VOL_UP: + case KC_AUDIO_VOL_UP: usage = AUDIO_VOL_UP; break; - case KB_AUDIO_VOL_DOWN: + case KC_AUDIO_VOL_DOWN: usage = AUDIO_VOL_DOWN; break; - case KB_MEDIA_NEXT_TRACK: + case KC_MEDIA_NEXT_TRACK: usage = TRANSPORT_NEXT_TRACK; break; - case KB_MEDIA_PREV_TRACK: + case KC_MEDIA_PREV_TRACK: usage = TRANSPORT_PREV_TRACK; break; - case KB_MEDIA_STOP: + case KC_MEDIA_STOP: usage = TRANSPORT_STOP; break; - case KB_MEDIA_PLAY_PAUSE: + case KC_MEDIA_PLAY_PAUSE: usage = TRANSPORT_PLAY_PAUSE; break; - case KB_MEDIA_SELECT: + case KC_MEDIA_SELECT: usage = AL_CC_CONFIG; break; - case KB_MAIL: + case KC_MAIL: usage = AL_EMAIL; break; - case KB_CALCULATOR: + case KC_CALCULATOR: usage = AL_CALCULATOR; break; - case KB_MY_COMPUTER: + case KC_MY_COMPUTER: usage = AL_LOCAL_BROWSER; break; - case KB_WWW_SEARCH: + case KC_WWW_SEARCH: usage = AC_SEARCH; break; - case KB_WWW_HOME: + case KC_WWW_HOME: usage = AC_HOME; break; - case KB_WWW_BACK: + case KC_WWW_BACK: usage = AC_BACK; break; - case KB_WWW_FORWARD: + case KC_WWW_FORWARD: usage = AC_FORWARD; break; - case KB_WWW_STOP: + case KC_WWW_STOP: usage = AC_STOP; break; - case KB_WWW_REFRESH: + case KC_WWW_REFRESH: usage = AC_REFRESH; break; - case KB_WWW_FAVORITES: + case KC_WWW_FAVORITES: usage = AC_BOOKMARKS; break; } @@ -218,13 +218,13 @@ debug("usage: "); phex16(usage); debug("\n"); else if IS_SYSTEM(code) { uint16_t usage = 0; switch (code) { - case KB_SYSTEM_POWER: + case KC_SYSTEM_POWER: usage = SYSTEM_POWER_DOWN; break; - case KB_SYSTEM_SLEEP: + case KC_SYSTEM_SLEEP: usage = SYSTEM_SLEEP; break; - case KB_SYSTEM_WAKE: + case KC_SYSTEM_WAKE: usage = SYSTEM_WAKE_UP; break; } -- cgit v1.2.3 From 71ac82337f803e8ec0c081b3221ac0ccf61035b0 Mon Sep 17 00:00:00 2001 From: tmk Date: Tue, 9 Oct 2012 16:50:14 +0900 Subject: Clean host.h interface. --- common/keyboard.c | 84 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 47 insertions(+), 37 deletions(-) (limited to 'common/keyboard.c') 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) return NONE; } +static void clear_keyboard(void) +{ + host_clear_keys(); + host_clear_mods(); + host_send_keyboard_report(); + + host_system_send(0); + host_consumer_send(0); + + mousekey_clear(); + mousekey_send(); +} + +static void clear_keyboard_but_mods(void) +{ + host_clear_keys(); + host_send_keyboard_report(); + + host_system_send(0); + host_consumer_send(0); + + mousekey_clear(); + mousekey_send(); +} + static void layer_switch_on(uint8_t code) { if (!IS_FN(code)) return; fn_state_bits |= FN_BIT(code); if (current_layer != keymap_fn_layer(FN_INDEX(code))) { - // clear all key execpt Mod key - host_clear_all_keys_but_mods(); - host_system_send(0); - host_consumer_send(0); - mousekey_clear(); - mousekey_send(); + clear_keyboard_but_mods(); debug("Layer Switch(on): "); debug_hex(current_layer); current_layer = keymap_fn_layer(FN_INDEX(code)); @@ -104,12 +124,7 @@ static void layer_switch_off(uint8_t code) if (!IS_FN(code)) return; fn_state_bits &= ~FN_BIT(code); if (current_layer != keymap_fn_layer(biton(fn_state_bits))) { - // clear all key execpt Mod key - host_clear_all_keys_but_mods(); - host_system_send(0); - host_consumer_send(0); - mousekey_clear(); - mousekey_send(); + clear_keyboard_but_mods(); debug("Layer Switch(off): "); debug_hex(current_layer); current_layer = keymap_fn_layer(biton(fn_state_bits)); @@ -117,11 +132,6 @@ static void layer_switch_off(uint8_t code) } } -static inline uint8_t get_keycode(key_t key) -{ - return keymap_get_keycode(current_layer, key.row, key.col); -} - // whether any key except modifier is down or not static inline bool is_anykey_down(void) { @@ -129,7 +139,7 @@ static inline bool is_anykey_down(void) matrix_row_t matrix_row = matrix_get_row(r); for (int c = 0; c < MATRIX_COLS; c++) { if (matrix_row && (1< Date: Wed, 10 Oct 2012 11:06:47 +0900 Subject: Fix build option MOUSEKEY_ENABLE. --- common/keyboard.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'common/keyboard.c') diff --git a/common/keyboard.c b/common/keyboard.c index 37d3b06ba..43abf4236 100644 --- a/common/keyboard.c +++ b/common/keyboard.c @@ -90,8 +90,10 @@ static void clear_keyboard(void) host_system_send(0); host_consumer_send(0); +#ifdef MOUSEKEY_ENABLE mousekey_clear(); mousekey_send(); +#endif } static void clear_keyboard_but_mods(void) @@ -102,8 +104,10 @@ static void clear_keyboard_but_mods(void) host_system_send(0); host_consumer_send(0); +#ifdef MOUSEKEY_ENABLE mousekey_clear(); mousekey_send(); +#endif } static void layer_switch_on(uint8_t code) @@ -159,8 +163,10 @@ static void register_code(uint8_t code) host_send_keyboard_report(); } else if IS_MOUSEKEY(code) { +#ifdef MOUSEKEY_ENABLE mousekey_on(code); mousekey_send(); +#endif } else if IS_CONSUMER(code) { uint16_t usage = 0; @@ -251,8 +257,10 @@ static void unregister_code(uint8_t code) host_send_keyboard_report(); } else if IS_MOUSEKEY(code) { +#ifdef MOUSEKEY_ENABLE mousekey_off(code); mousekey_send(); +#endif } else if IS_CONSUMER(code) { host_consumer_send(0x0000); @@ -390,7 +398,7 @@ static inline void process_key(keyevent_t event) case KEY_UP: case MOD_UP: unregister_code(code); - // no key registered? mousekey, mediakey, systemkey + // TODO: no key registered? mousekey, mediakey, systemkey if (!host_has_anykey()) NEXT(IDLE); break; @@ -570,8 +578,10 @@ void keyboard_task(void) } } +#ifdef MOUSEKEY_ENABLE // mousekey repeat & acceleration mousekey_task(); +#endif // FAIL SAFE: clear all key if no key down if (matrix_change) { -- cgit v1.2.3 From 1677b021d7bff6af7763532b038612363b61dada Mon Sep 17 00:00:00 2001 From: tmk Date: Fri, 12 Oct 2012 04:46:37 +0900 Subject: Fix layer switching and host API. --- common/keyboard.c | 136 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 76 insertions(+), 60 deletions(-) (limited to 'common/keyboard.c') diff --git a/common/keyboard.c b/common/keyboard.c index 43abf4236..7a17a9e38 100644 --- a/common/keyboard.c +++ b/common/keyboard.c @@ -110,6 +110,12 @@ static void clear_keyboard_but_mods(void) #endif } +static bool anykey_sent_to_host(void) +{ + return (host_has_anykey() || host_mouse_in_use() || + host_last_sysytem_report() || host_last_consumer_report()); +} + static void layer_switch_on(uint8_t code) { if (!IS_FN(code)) return; @@ -123,9 +129,9 @@ static void layer_switch_on(uint8_t code) } } -static void layer_switch_off(uint8_t code) +static bool layer_switch_off(uint8_t code) { - if (!IS_FN(code)) return; + if (!IS_FN(code)) return false; fn_state_bits &= ~FN_BIT(code); if (current_layer != keymap_fn_layer(biton(fn_state_bits))) { clear_keyboard_but_mods(); @@ -133,21 +139,7 @@ static void layer_switch_off(uint8_t code) debug("Layer Switch(off): "); debug_hex(current_layer); current_layer = keymap_fn_layer(biton(fn_state_bits)); debug(" -> "); debug_hex(current_layer); debug("\n"); - } -} - -// whether any key except modifier is down or not -static inline bool is_anykey_down(void) -{ - for (int r = 0; r < MATRIX_ROWS; r++) { - matrix_row_t matrix_row = matrix_get_row(r); - for (int c = 0; c < MATRIX_COLS; c++) { - if (matrix_row && (1<mods; host_set_mods(delayed_fn.mods); - register_code(keymap_fn_keycode(FN_INDEX(delayed_fn.code))); - unregister_code(keymap_fn_keycode(FN_INDEX(delayed_fn.code))); + register_code(delayed_fn.code); + unregister_code(delayed_fn.code); host_set_mods(tmp_mods); NEXT(IDLE); } else { - layer_switch_off(code); - NEXT(IDLE); + if (layer_switch_off(code)) + NEXT(IDLE); } break; case KEY_UP: - unregister_code(code); - NEXT(IDLE); - break; case MOD_UP: unregister_code(code); break; @@ -459,34 +469,40 @@ static inline void process_key(keyevent_t event) case KEY_DOWN: tmp_mods = keyboard_report->mods; host_set_mods(delayed_fn.mods); - register_code(keymap_fn_keycode(FN_INDEX(delayed_fn.code))); + register_code(delayed_fn.code); host_set_mods(waiting_key.mods); register_code(waiting_key.code); host_set_mods(tmp_mods); - register_code(code); + if (kind == FN_DOWN) { + // ignore Fn + } else if (kind == FNK_DOWN) { + register_code(code); + } else if (kind == KEY_DOWN) { + register_code(code); + } NEXT(IDLE); break; case MOD_DOWN: register_code(code); break; case FN_UP: - layer_switch_off(code); - NEXT(IDLE); + if (layer_switch_off(code)) + NEXT(IDLE); break; case FNK_UP: if (code == delayed_fn.code) { // alt down, key down, alt up tmp_mods = keyboard_report->mods; host_set_mods(delayed_fn.mods); - register_code(keymap_fn_keycode(FN_INDEX(delayed_fn.code))); + register_code(delayed_fn.code); host_set_mods(waiting_key.mods); register_code(waiting_key.code); - unregister_code(keymap_fn_keycode(FN_INDEX(delayed_fn.code))); + unregister_code(delayed_fn.code); host_set_mods(tmp_mods); NEXT(IDLE); } else { - layer_switch_off(code); - NEXT(IDLE); + if (layer_switch_off(code)) + NEXT(IDLE); } break; case KEY_UP: -- cgit v1.2.3 From f7a445e537243f886d9cb022bede18859c9bfd12 Mon Sep 17 00:00:00 2001 From: tmk Date: Fri, 12 Oct 2012 11:00:52 +0900 Subject: Fix use of default_layer --- common/keyboard.c | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) (limited to 'common/keyboard.c') diff --git a/common/keyboard.c b/common/keyboard.c index 7a17a9e38..be01e5540 100644 --- a/common/keyboard.c +++ b/common/keyboard.c @@ -46,8 +46,13 @@ typedef enum keykind { typedef enum { IDLE, DELAYING, WAITING, PRESSING } kbdstate_t; -uint8_t current_layer = 0; +#ifdef KEYMAP_DEFAULT_LAYER +uint8_t default_layer = KEYMAP_DEFAULT_LAYER; +uint8_t current_layer = KEYMAP_DEFAULT_LAYER; +#else uint8_t default_layer = 0; +uint8_t current_layer = 0; +#endif /* keyboard internal states */ static kbdstate_t kbdstate = IDLE; @@ -120,12 +125,13 @@ static void layer_switch_on(uint8_t code) { if (!IS_FN(code)) return; fn_state_bits |= FN_BIT(code); - if (current_layer != keymap_fn_layer(FN_INDEX(code))) { - clear_keyboard_but_mods(); - + uint8_t new_layer = (fn_state_bits ? keymap_fn_layer(biton(fn_state_bits)) : default_layer); + if (current_layer != new_layer) { debug("Layer Switch(on): "); debug_hex(current_layer); - current_layer = keymap_fn_layer(FN_INDEX(code)); - debug(" -> "); debug_hex(current_layer); debug("\n"); + debug(" -> "); debug_hex(new_layer); debug("\n"); + + clear_keyboard_but_mods(); + current_layer = new_layer; } } @@ -133,12 +139,13 @@ static bool layer_switch_off(uint8_t code) { if (!IS_FN(code)) return false; fn_state_bits &= ~FN_BIT(code); - if (current_layer != keymap_fn_layer(biton(fn_state_bits))) { - clear_keyboard_but_mods(); - + uint8_t new_layer = (fn_state_bits ? keymap_fn_layer(biton(fn_state_bits)) : default_layer); + if (current_layer != new_layer) { debug("Layer Switch(off): "); debug_hex(current_layer); - current_layer = keymap_fn_layer(biton(fn_state_bits)); - debug(" -> "); debug_hex(current_layer); debug("\n"); + debug(" -> "); debug_hex(new_layer); debug("\n"); + + clear_keyboard_but_mods(); + current_layer = new_layer; return true; } return false; @@ -606,8 +613,9 @@ void keyboard_task(void) is_matrix_on |= matrix_get_row(r); } if (!is_matrix_on) { - debug("FAIL SAFE: clear all keys.\n"); + debug("FAIL SAFE: clear all keys(default layer).\n"); clear_keyboard(); + current_layer = default_layer; } } -- cgit v1.2.3 From e451c059296a4c9af7a476577fee64afb9965bca Mon Sep 17 00:00:00 2001 From: tmk Date: Tue, 16 Oct 2012 11:20:49 +0900 Subject: Fix commands --- common/keyboard.c | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) (limited to 'common/keyboard.c') diff --git a/common/keyboard.c b/common/keyboard.c index be01e5540..c7ea2b840 100644 --- a/common/keyboard.c +++ b/common/keyboard.c @@ -28,9 +28,6 @@ along with this program. If not, see . #ifdef MOUSEKEY_ENABLE #include "mousekey.h" #endif -#ifdef EXTRAKEY_ENABLE -#include -#endif #define LAYER_DELAY 250 @@ -154,8 +151,12 @@ static bool layer_switch_off(uint8_t code) static void register_code(uint8_t code) { if IS_KEY(code) { - host_add_key(code); - host_send_keyboard_report(); + if (command_proc(code)) { + //clear_keyboard(); + } else { + host_add_key(code); + host_send_keyboard_report(); + } } else if IS_MOD(code) { host_add_mod_bit(MOD_BIT(code)); @@ -330,9 +331,9 @@ static void unregister_code(uint8_t code) * Ld: Switch back to default layer(*unregister* all keys but modifiers) */ #define NEXT(state) do { \ - debug("NEXT: "); print_P(state_str(kbdstate)); \ + debug("NEXT: "); debug_P(state_str(kbdstate)); \ kbdstate = state; \ - debug(" -> "); print_P(state_str(kbdstate)); debug("\n"); \ + debug(" -> "); debug_P(state_str(kbdstate)); debug("\n"); \ } while (0) static inline void process_key(keyevent_t event) @@ -342,7 +343,7 @@ static inline void process_key(keyevent_t event) uint8_t tmp_mods; - debug("state: "); print_P(state_str(kbdstate)); + debug("state: "); debug_P(state_str(kbdstate)); debug(" kind: "); debug_hex(kind); debug(" code: "); debug_hex(code); if (event.pressed) { debug("d"); } else { debug("u"); } @@ -554,18 +555,11 @@ void keyboard_task(void) matrix_row_t matrix_change = 0; matrix_scan(); - if (command_proc()) { - debug("COMMAND\n"); - // TODO: COMMAND state? - clear_keyboard(); - return; - } - for (int r = 0; r < MATRIX_ROWS; r++) { matrix_row = matrix_get_row(r); matrix_change = matrix_row ^ matrix_prev[r]; if (matrix_change) { - if (debug_matrix) matrix_print(); + matrix_debug(); for (int c = 0; c < MATRIX_COLS; c++) { if (matrix_change & (1< Date: Wed, 17 Oct 2012 03:44:01 +0900 Subject: Add command console and mouseky parameters tweak. --- common/keyboard.c | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) (limited to 'common/keyboard.c') diff --git a/common/keyboard.c b/common/keyboard.c index c7ea2b840..d7ced430e 100644 --- a/common/keyboard.c +++ b/common/keyboard.c @@ -30,6 +30,10 @@ along with this program. If not, see . #endif +#define Kdebug(s) do { if (debug_keyboard) debug(s); } while(0) +#define Kdebug_P(s) do { if (debug_keyboard) debug_P(s); } while(0) +#define Kdebug_hex(s) do { if (debug_keyboard) debug_hex(s); } while(0) + #define LAYER_DELAY 250 typedef enum keykind { @@ -124,8 +128,8 @@ static void layer_switch_on(uint8_t code) fn_state_bits |= FN_BIT(code); uint8_t new_layer = (fn_state_bits ? keymap_fn_layer(biton(fn_state_bits)) : default_layer); if (current_layer != new_layer) { - debug("Layer Switch(on): "); debug_hex(current_layer); - debug(" -> "); debug_hex(new_layer); debug("\n"); + Kdebug("Layer Switch(on): "); Kdebug_hex(current_layer); + Kdebug(" -> "); Kdebug_hex(new_layer); Kdebug("\n"); clear_keyboard_but_mods(); current_layer = new_layer; @@ -138,8 +142,8 @@ static bool layer_switch_off(uint8_t code) fn_state_bits &= ~FN_BIT(code); uint8_t new_layer = (fn_state_bits ? keymap_fn_layer(biton(fn_state_bits)) : default_layer); if (current_layer != new_layer) { - debug("Layer Switch(off): "); debug_hex(current_layer); - debug(" -> "); debug_hex(new_layer); debug("\n"); + Kdebug("Layer Switch(off): "); Kdebug_hex(current_layer); + Kdebug(" -> "); Kdebug_hex(new_layer); Kdebug("\n"); clear_keyboard_but_mods(); current_layer = new_layer; @@ -151,9 +155,7 @@ static bool layer_switch_off(uint8_t code) static void register_code(uint8_t code) { if IS_KEY(code) { - if (command_proc(code)) { - //clear_keyboard(); - } else { + if (!command_proc(code)) { host_add_key(code); host_send_keyboard_report(); } @@ -163,8 +165,10 @@ static void register_code(uint8_t code) host_send_keyboard_report(); } else if IS_FN(code) { - host_add_key(keymap_fn_keycode(FN_INDEX(code))); - host_send_keyboard_report(); + if (!command_proc(keymap_fn_keycode(FN_INDEX(code)))) { + host_add_key(keymap_fn_keycode(FN_INDEX(code))); + host_send_keyboard_report(); + } } else if IS_MOUSEKEY(code) { #ifdef MOUSEKEY_ENABLE @@ -331,9 +335,9 @@ static void unregister_code(uint8_t code) * Ld: Switch back to default layer(*unregister* all keys but modifiers) */ #define NEXT(state) do { \ - debug("NEXT: "); debug_P(state_str(kbdstate)); \ + Kdebug("NEXT: "); Kdebug_P(state_str(kbdstate)); \ kbdstate = state; \ - debug(" -> "); debug_P(state_str(kbdstate)); debug("\n"); \ + Kdebug(" -> "); Kdebug_P(state_str(kbdstate)); Kdebug("\n"); \ } while (0) static inline void process_key(keyevent_t event) @@ -343,11 +347,11 @@ static inline void process_key(keyevent_t event) uint8_t tmp_mods; - debug("state: "); debug_P(state_str(kbdstate)); - debug(" kind: "); debug_hex(kind); - debug(" code: "); debug_hex(code); - if (event.pressed) { debug("d"); } else { debug("u"); } - debug("\n"); + Kdebug("state: "); Kdebug_P(state_str(kbdstate)); + Kdebug(" kind: "); Kdebug_hex(kind); + Kdebug(" code: "); Kdebug_hex(code); + if (event.pressed) { Kdebug("d"); } else { Kdebug("u"); } + Kdebug("\n"); switch (kbdstate) { case IDLE: @@ -607,7 +611,7 @@ void keyboard_task(void) is_matrix_on |= matrix_get_row(r); } if (!is_matrix_on) { - debug("FAIL SAFE: clear all keys(default layer).\n"); + Kdebug("FAIL SAFE: clear all keys(default layer).\n"); clear_keyboard(); current_layer = default_layer; } -- cgit v1.2.3 From fffc375b45ad795c4a8d3cde94783dac195a2613 Mon Sep 17 00:00:00 2001 From: tmk Date: Thu, 18 Oct 2012 00:10:20 +0900 Subject: Add child proof keyboard locking feature! Fix: add wait for Power down command Add ifdef of MOUSEKEY_ENABLE --- common/keyboard.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'common/keyboard.c') diff --git a/common/keyboard.c b/common/keyboard.c index d7ced430e..e973c46d5 100644 --- a/common/keyboard.c +++ b/common/keyboard.c @@ -563,7 +563,7 @@ void keyboard_task(void) matrix_row = matrix_get_row(r); matrix_change = matrix_row ^ matrix_prev[r]; if (matrix_change) { - matrix_debug(); + if (debug_matrix) matrix_print(); for (int c = 0; c < MATRIX_COLS; c++) { if (matrix_change & (1<