From 71ac82337f803e8ec0c081b3221ac0ccf61035b0 Mon Sep 17 00:00:00 2001 From: tmk Date: Tue, 9 Oct 2012 16:50:14 +0900 Subject: [PATCH] Clean host.h interface. --- common/command.c | 7 --- common/host.c | 85 +++++++++---------------------------- common/host.h | 21 ++++----- common/keyboard.c | 84 ++++++++++++++++++++---------------- keyboard/hhkb/config_vusb.h | 2 +- protocol/vusb/vusb.c | 4 +- 6 files changed, 81 insertions(+), 122 deletions(-) diff --git a/common/command.c b/common/command.c index f9cdaf57..16c6cfb8 100644 --- a/common/command.c +++ b/common/command.c @@ -78,8 +78,6 @@ static uint8_t command_common(void) help(); break; case KC_B: - host_clear_keyboard_report(); - host_send_keyboard_report(); print("jump to bootloader... "); _delay_ms(1000); bootloader_jump(); // not return @@ -157,9 +155,6 @@ static uint8_t command_common(void) break; #ifdef NKRO_ENABLE case KC_N: - // send empty report before change - host_clear_keyboard_report(); - host_send_keyboard_report(); keyboard_nkro = !keyboard_nkro; if (keyboard_nkro) print("NKRO: enabled\n"); @@ -169,8 +164,6 @@ static uint8_t command_common(void) #endif #ifdef EXTRAKEY_ENABLE case KC_ESC: - host_clear_keyboard_report(); - host_send_keyboard_report(); #ifdef HOST_PJRC if (suspend && remote_wakeup) { usb_remote_wakeup(); diff --git a/common/host.c b/common/host.c index 37f707d0..a671c97d 100644 --- a/common/host.c +++ b/common/host.c @@ -1,5 +1,5 @@ /* -Copyright 2011 Jun Wako +Copyright 2011,2012 Jun Wako This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -28,10 +28,7 @@ bool keyboard_nkro = false; #endif static host_driver_t *driver; -static report_keyboard_t report0; -static report_keyboard_t report1; -report_keyboard_t *keyboard_report = &report0; -report_keyboard_t *keyboard_report_prev = &report1; +report_keyboard_t *keyboard_report = &(report_keyboard_t){}; static inline void add_key_byte(uint8_t code); @@ -56,27 +53,6 @@ uint8_t host_keyboard_leds(void) return (*driver->keyboard_leds)(); } -/* new interface */ -void host_register_key(uint8_t key) -{ - host_add_key(key); - host_send_keyboard_report(); -} - -void host_unregister_key(uint8_t key) -{ - host_del_key(key); - host_send_keyboard_report(); -} - -void host_clear_all_keys_but_mods(void) -{ - for (int8_t i = 0; i < REPORT_KEYS; i++) { - keyboard_report->keys[i] = 0; - } - host_send_keyboard_report(); -} - /* keyboard report operations */ void host_add_key(uint8_t key) { @@ -100,6 +76,13 @@ void host_del_key(uint8_t key) del_key_byte(key); } +void host_clear_keys(void) +{ + for (int8_t i = 0; i < REPORT_KEYS; i++) { + keyboard_report->keys[i] = 0; + } +} + void host_add_mod_bit(uint8_t mod) { keyboard_report->mods |= mod; @@ -115,40 +98,9 @@ void host_set_mods(uint8_t mods) keyboard_report->mods = mods; } -void host_add_code(uint8_t code) -{ - if (IS_MOD(code)) { - host_add_mod_bit(MOD_BIT(code)); - } else { - host_add_key(code); - } -} - -void host_del_code(uint8_t code) -{ - if (IS_MOD(code)) { - host_del_mod_bit(MOD_BIT(code)); - } else { - host_del_key(code); - } -} - -void host_swap_keyboard_report(void) -{ - uint8_t sreg = SREG; - cli(); - report_keyboard_t *tmp = keyboard_report_prev; - keyboard_report_prev = keyboard_report; - keyboard_report = tmp; - SREG = sreg; -} - -void host_clear_keyboard_report(void) +void host_clear_mods(void) { keyboard_report->mods = 0; - for (int8_t i = 0; i < REPORT_KEYS; i++) { - keyboard_report->keys[i] = 0; - } } uint8_t host_has_anykey(void) @@ -174,7 +126,6 @@ uint8_t host_get_first_key(void) return keyboard_report->keys[0]; } - void host_send_keyboard_report(void) { if (!driver) return; @@ -189,6 +140,14 @@ void host_send_keyboard_report(void) } } + +/* send report */ +void host_keyboard_send(report_keyboard_t *report) +{ + if (!driver) return; + (*driver->send_keyboard)(report); +} + void host_mouse_send(report_mouse_t *report) { if (!driver) return; @@ -218,17 +177,13 @@ void host_consumer_send(uint16_t data) static inline void add_key_byte(uint8_t code) { - // TODO: fix ugly code int8_t i = 0; int8_t empty = -1; for (; i < REPORT_KEYS; i++) { - if (keyboard_report_prev->keys[i] == code) { - keyboard_report->keys[i] = code; + if (keyboard_report->keys[i] == code) { break; } - if (empty == -1 && - keyboard_report_prev->keys[i] == 0 && - keyboard_report->keys[i] == 0) { + if (empty == -1 && keyboard_report->keys[i] == 0) { empty = i; } } diff --git a/common/host.h b/common/host.h index a6dff8de..a0a661af 100644 --- a/common/host.h +++ b/common/host.h @@ -35,30 +35,31 @@ extern report_keyboard_t *keyboard_report; extern report_keyboard_t *keyboard_report_prev; +/* host driver */ void host_set_driver(host_driver_t *driver); host_driver_t *host_get_driver(void); + uint8_t host_keyboard_leds(void); -/* new interface */ -void host_register_key(uint8_t key); -void host_unregister_key(uint8_t key); -void host_clear_all_keys_but_mods(void); /* keyboard report operations */ +/* key */ 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_add_code(uint8_t code); -void host_del_code(uint8_t code); -void host_swap_keyboard_report(void); -void host_clear_keyboard_report(void); +void host_clear_mods(void); +/* query */ uint8_t host_has_anykey(void); uint8_t host_get_first_key(void); - - +/* send report */ void host_send_keyboard_report(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); diff --git a/common/keyboard.c b/common/keyboard.c index 6adad888..37d3b06b 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<. /* key combination for command */ -#define IS_COMMAND() (keyboard_report->mods == (MOD_BIT(KB_LSHIFT) | MOD_BIT(KB_RSHIFT))) +#define IS_COMMAND() (keyboard_report->mods == (MOD_BIT(KC_LSHIFT) | MOD_BIT(KC_RSHIFT))) /* mouse keys */ #ifdef MOUSEKEY_ENABLE diff --git a/protocol/vusb/vusb.c b/protocol/vusb/vusb.c index 4e11836e..59834e69 100644 --- a/protocol/vusb/vusb.c +++ b/protocol/vusb/vusb.c @@ -164,8 +164,8 @@ usbRequest_t *rq = (void *)data; if(rq->bRequest == USBRQ_HID_GET_REPORT){ debug("GET_REPORT:"); /* we only have one report type, so don't look at wValue */ - usbMsgPtr = (void *)keyboard_report_prev; - return sizeof(*keyboard_report_prev); + usbMsgPtr = (void *)keyboard_report; + return sizeof(*keyboard_report); }else if(rq->bRequest == USBRQ_HID_GET_IDLE){ debug("GET_IDLE: "); //debug_hex(vusb_idle_rate);