From 1677b021d7bff6af7763532b038612363b61dada Mon Sep 17 00:00:00 2001 From: tmk Date: Fri, 12 Oct 2012 04:46:37 +0900 Subject: [PATCH] Fix layer switching and host API. --- common/host.c | 95 +++++++++++++++++++------------- common/host.h | 24 ++++---- common/keyboard.c | 136 ++++++++++++++++++++++++++-------------------- common/mousekey.c | 93 ++++++++++++++++--------------- 4 files changed, 193 insertions(+), 155 deletions(-) diff --git a/common/host.c b/common/host.c index a671c97d..261ec647 100644 --- a/common/host.c +++ b/common/host.c @@ -27,10 +27,14 @@ along with this program. If not, see . bool keyboard_nkro = false; #endif -static host_driver_t *driver; report_keyboard_t *keyboard_report = &(report_keyboard_t){}; +report_mouse_t mouse_report = {}; +static host_driver_t *driver; +static uint16_t last_system_report = 0; +static uint16_t last_consumer_report = 0; + static inline void add_key_byte(uint8_t code); static inline void del_key_byte(uint8_t code); static inline void add_key_bit(uint8_t code); @@ -52,8 +56,48 @@ uint8_t host_keyboard_leds(void) if (!driver) return 0; return (*driver->keyboard_leds)(); } +/* send report */ +void host_keyboard_send(report_keyboard_t *report) +{ + if (!driver) return; + (*driver->send_keyboard)(report); -/* keyboard report operations */ + if (debug_keyboard) { + print("keys: "); + for (int i = 0; i < REPORT_KEYS; i++) { + phex(keyboard_report->keys[i]); print(" "); + } + print(" mods: "); phex(keyboard_report->mods); print("\n"); + } +} + +void host_mouse_send(report_mouse_t *report) +{ + if (!driver) return; + (*driver->send_mouse)(report); +} + +void host_system_send(uint16_t report) +{ + if (report == last_system_report) return; + last_system_report = report; + + if (!driver) return; + (*driver->send_system)(report); +} + +void host_consumer_send(uint16_t report) +{ + if (report == last_consumer_report) return; + last_consumer_report = report; + + if (!driver) return; + (*driver->send_consumer)(report); +} + + + +/* keyboard report utils */ void host_add_key(uint8_t key) { #ifdef NKRO_ENABLE @@ -113,6 +157,11 @@ uint8_t host_has_anykey(void) return cnt; } +uint8_t host_has_anymod(void) +{ + return bitpop(keyboard_report->mods); +} + uint8_t host_get_first_key(void) { #ifdef NKRO_ENABLE @@ -129,52 +178,24 @@ uint8_t host_get_first_key(void) void host_send_keyboard_report(void) { if (!driver) return; - (*driver->send_keyboard)(keyboard_report); - - if (debug_keyboard) { - print("keys: "); - for (int i = 0; i < REPORT_KEYS; i++) { - phex(keyboard_report->keys[i]); print(" "); - } - print(" mods: "); phex(keyboard_report->mods); print("\n"); - } + host_keyboard_send(keyboard_report); } - -/* send report */ -void host_keyboard_send(report_keyboard_t *report) +uint8_t host_mouse_in_use(void) { - if (!driver) return; - (*driver->send_keyboard)(report); + return (mouse_report.buttons | mouse_report.x | mouse_report.y | mouse_report.v | mouse_report.h); } -void host_mouse_send(report_mouse_t *report) +uint16_t host_last_sysytem_report(void) { - if (!driver) return; - (*driver->send_mouse)(report); + return last_system_report; } -void host_system_send(uint16_t data) +uint16_t host_last_consumer_report(void) { - static uint16_t last_data = 0; - if (data == last_data) return; - last_data = data; - - if (!driver) return; - (*driver->send_system)(data); + return last_consumer_report; } -void host_consumer_send(uint16_t data) -{ - static uint16_t last_data = 0; - if (data == last_data) return; - last_data = data; - - if (!driver) return; - (*driver->send_consumer)(data); -} - - static inline void add_key_byte(uint8_t code) { int8_t i = 0; diff --git a/common/host.h b/common/host.h index a0a661af..207b6831 100644 --- a/common/host.h +++ b/common/host.h @@ -31,38 +31,40 @@ extern "C" { extern bool keyboard_nkro; #endif +/* report */ extern report_keyboard_t *keyboard_report; -extern report_keyboard_t *keyboard_report_prev; +extern report_mouse_t mouse_report; /* host driver */ void host_set_driver(host_driver_t *driver); host_driver_t *host_get_driver(void); +/* host driver interface */ uint8_t host_keyboard_leds(void); +void host_keyboard_send(report_keyboard_t *report); +void host_mouse_send(report_mouse_t *report); +void host_system_send(uint16_t data); +void host_consumer_send(uint16_t data); - -/* keyboard report operations */ -/* key */ +/* keyboard report utils */ void host_add_key(uint8_t key); void host_del_key(uint8_t key); void host_clear_keys(void); -/* modifier */ void host_add_mod_bit(uint8_t mod); void host_del_mod_bit(uint8_t mod); void host_set_mods(uint8_t mods); void host_clear_mods(void); -/* query */ uint8_t host_has_anykey(void); +uint8_t host_has_anymod(void); uint8_t host_get_first_key(void); -/* send report */ void host_send_keyboard_report(void); +/* mouse report utils */ +uint8_t host_mouse_in_use(void); -/* send report: mouse, system contorl and consumer page */ -void host_mouse_send(report_mouse_t *report); -void host_system_send(uint16_t data); -void host_consumer_send(uint16_t data); +uint16_t host_last_sysytem_report(void); +uint16_t host_last_consumer_report(void); #ifdef __cplusplus } diff --git a/common/keyboard.c b/common/keyboard.c index 43abf423..7a17a9e3 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: diff --git a/common/mousekey.c b/common/mousekey.c index 4b1fe174..353890a1 100644 --- a/common/mousekey.c +++ b/common/mousekey.c @@ -25,7 +25,6 @@ along with this program. If not, see . #include "mousekey.h" -static report_mouse_t report; static uint8_t mousekey_repeat = 0; @@ -115,89 +114,89 @@ void mousekey_task(void) if (timer_elapsed(last_timer) < (mousekey_repeat ? mk_interval : mk_delay*10)) return; - if (report.x == 0 && report.y == 0 && report.v == 0 && report.h == 0) + if (mouse_report.x == 0 && mouse_report.y == 0 && mouse_report.v == 0 && mouse_report.h == 0) return; if (mousekey_repeat != UINT8_MAX) mousekey_repeat++; - if (report.x > 0) report.x = move_unit(); - if (report.x < 0) report.x = move_unit() * -1; - if (report.y > 0) report.y = move_unit(); - if (report.y < 0) report.y = move_unit() * -1; + if (mouse_report.x > 0) mouse_report.x = move_unit(); + if (mouse_report.x < 0) mouse_report.x = move_unit() * -1; + if (mouse_report.y > 0) mouse_report.y = move_unit(); + if (mouse_report.y < 0) mouse_report.y = move_unit() * -1; - if (report.x && report.y) { - report.x *= 0.7; - report.y *= 0.7; + if (mouse_report.x && mouse_report.y) { + mouse_report.x *= 0.7; + mouse_report.y *= 0.7; } - if (report.v > 0) report.v = wheel_unit(); - if (report.v < 0) report.v = wheel_unit() * -1; - if (report.h > 0) report.h = wheel_unit(); - if (report.h < 0) report.h = wheel_unit() * -1; + if (mouse_report.v > 0) mouse_report.v = wheel_unit(); + if (mouse_report.v < 0) mouse_report.v = wheel_unit() * -1; + if (mouse_report.h > 0) mouse_report.h = wheel_unit(); + if (mouse_report.h < 0) mouse_report.h = wheel_unit() * -1; mousekey_send(); } void mousekey_on(uint8_t code) { - if (code == KC_MS_UP) report.y = MOUSEKEY_MOVE_DELTA * -1; - else if (code == KC_MS_DOWN) report.y = MOUSEKEY_MOVE_DELTA; - else if (code == KC_MS_LEFT) report.x = MOUSEKEY_MOVE_DELTA * -1; - else if (code == KC_MS_RIGHT) report.x = MOUSEKEY_MOVE_DELTA; - else if (code == KC_MS_WH_UP) report.v = MOUSEKEY_WHEEL_DELTA; - else if (code == KC_MS_WH_DOWN) report.v = MOUSEKEY_WHEEL_DELTA * -1; - else if (code == KC_MS_WH_LEFT) report.h = MOUSEKEY_WHEEL_DELTA * -1; - else if (code == KC_MS_WH_RIGHT) report.h = MOUSEKEY_WHEEL_DELTA; - else if (code == KC_MS_BTN1) report.buttons |= MOUSE_BTN1; - else if (code == KC_MS_BTN2) report.buttons |= MOUSE_BTN2; - else if (code == KC_MS_BTN3) report.buttons |= MOUSE_BTN3; - else if (code == KC_MS_BTN4) report.buttons |= MOUSE_BTN4; - else if (code == KC_MS_BTN5) report.buttons |= MOUSE_BTN5; + if (code == KC_MS_UP) mouse_report.y = MOUSEKEY_MOVE_DELTA * -1; + else if (code == KC_MS_DOWN) mouse_report.y = MOUSEKEY_MOVE_DELTA; + else if (code == KC_MS_LEFT) mouse_report.x = MOUSEKEY_MOVE_DELTA * -1; + else if (code == KC_MS_RIGHT) mouse_report.x = MOUSEKEY_MOVE_DELTA; + else if (code == KC_MS_WH_UP) mouse_report.v = MOUSEKEY_WHEEL_DELTA; + else if (code == KC_MS_WH_DOWN) mouse_report.v = MOUSEKEY_WHEEL_DELTA * -1; + else if (code == KC_MS_WH_LEFT) mouse_report.h = MOUSEKEY_WHEEL_DELTA * -1; + else if (code == KC_MS_WH_RIGHT) mouse_report.h = MOUSEKEY_WHEEL_DELTA; + else if (code == KC_MS_BTN1) mouse_report.buttons |= MOUSE_BTN1; + else if (code == KC_MS_BTN2) mouse_report.buttons |= MOUSE_BTN2; + else if (code == KC_MS_BTN3) mouse_report.buttons |= MOUSE_BTN3; + else if (code == KC_MS_BTN4) mouse_report.buttons |= MOUSE_BTN4; + else if (code == KC_MS_BTN5) mouse_report.buttons |= MOUSE_BTN5; } void mousekey_off(uint8_t code) { - if (code == KC_MS_UP && report.y < 0) report.y = 0; - else if (code == KC_MS_DOWN && report.y > 0) report.y = 0; - else if (code == KC_MS_LEFT && report.x < 0) report.x = 0; - else if (code == KC_MS_RIGHT && report.x > 0) report.x = 0; - else if (code == KC_MS_WH_UP && report.v > 0) report.v = 0; - else if (code == KC_MS_WH_DOWN && report.v < 0) report.v = 0; - else if (code == KC_MS_WH_LEFT && report.h < 0) report.h = 0; - else if (code == KC_MS_WH_RIGHT && report.h > 0) report.h = 0; - else if (code == KC_MS_BTN1) report.buttons &= ~MOUSE_BTN1; - else if (code == KC_MS_BTN2) report.buttons &= ~MOUSE_BTN2; - else if (code == KC_MS_BTN3) report.buttons &= ~MOUSE_BTN3; - else if (code == KC_MS_BTN4) report.buttons &= ~MOUSE_BTN4; - else if (code == KC_MS_BTN5) report.buttons &= ~MOUSE_BTN5; + if (code == KC_MS_UP && mouse_report.y < 0) mouse_report.y = 0; + else if (code == KC_MS_DOWN && mouse_report.y > 0) mouse_report.y = 0; + else if (code == KC_MS_LEFT && mouse_report.x < 0) mouse_report.x = 0; + else if (code == KC_MS_RIGHT && mouse_report.x > 0) mouse_report.x = 0; + else if (code == KC_MS_WH_UP && mouse_report.v > 0) mouse_report.v = 0; + else if (code == KC_MS_WH_DOWN && mouse_report.v < 0) mouse_report.v = 0; + else if (code == KC_MS_WH_LEFT && mouse_report.h < 0) mouse_report.h = 0; + else if (code == KC_MS_WH_RIGHT && mouse_report.h > 0) mouse_report.h = 0; + else if (code == KC_MS_BTN1) mouse_report.buttons &= ~MOUSE_BTN1; + else if (code == KC_MS_BTN2) mouse_report.buttons &= ~MOUSE_BTN2; + else if (code == KC_MS_BTN3) mouse_report.buttons &= ~MOUSE_BTN3; + else if (code == KC_MS_BTN4) mouse_report.buttons &= ~MOUSE_BTN4; + else if (code == KC_MS_BTN5) mouse_report.buttons &= ~MOUSE_BTN5; - if (report.x == 0 && report.y == 0 && report.v == 0 && report.h == 0) + if (mouse_report.x == 0 && mouse_report.y == 0 && mouse_report.v == 0 && mouse_report.h == 0) mousekey_repeat = 0; } void mousekey_send(void) { mousekey_debug(); - host_mouse_send(&report); + host_mouse_send(&mouse_report); last_timer = timer_read(); } void mousekey_clear(void) { - report = (report_mouse_t){}; + mouse_report = (report_mouse_t){}; } static void mousekey_debug(void) { if (!debug_mouse) return; print("mousekey [btn|x y v h]rep: ["); - phex(report.buttons); print("|"); - phex(report.x); print(" "); - phex(report.y); print(" "); - phex(report.v); print(" "); - phex(report.h); print("]"); + phex(mouse_report.buttons); print("|"); + phex(mouse_report.x); print(" "); + phex(mouse_report.y); print(" "); + phex(mouse_report.v); print(" "); + phex(mouse_report.h); print("]"); phex(mousekey_repeat); print("\n"); }