Fix layer switching and host API.

This commit is contained in:
tmk 2012-10-12 04:46:37 +09:00
parent 0a70be9a97
commit 1677b021d7
4 changed files with 193 additions and 155 deletions

View File

@ -27,10 +27,14 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
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;

View File

@ -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
}

View File

@ -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<<c)) {
if (IS_KEY(keymap_get_keycode(current_layer, r, c))) {
return true;
}
}
}
return true;
}
return false;
}
@ -162,6 +154,10 @@ static void register_code(uint8_t code)
host_add_mod_bit(MOD_BIT(code));
host_send_keyboard_report();
}
else if IS_FN(code) {
host_add_key(keymap_fn_keycode(FN_INDEX(code)));
host_send_keyboard_report();
}
else if IS_MOUSEKEY(code) {
#ifdef MOUSEKEY_ENABLE
mousekey_on(code);
@ -256,6 +252,10 @@ static void unregister_code(uint8_t code)
host_del_mod_bit(MOD_BIT(code));
host_send_keyboard_report();
}
else if IS_FN(code) {
host_del_key(keymap_fn_keycode(FN_INDEX(code)));
host_send_keyboard_report();
}
else if IS_MOUSEKEY(code) {
#ifdef MOUSEKEY_ENABLE
mousekey_off(code);
@ -272,24 +272,31 @@ static void unregister_code(uint8_t code)
/*
*
* Event/State|IDLE DELAYING[f] WAITING[f,k] PRESSING
* Event/State|IDLE PRESSING DELAYING[f] WAITING[f,k]
* -----------+------------------------------------------------------------------
* Fn Down |IDLE(L+) WAITING(Sk) WAITING(Sk) -
* 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)
* Up |IDLE(Uk) DELAYING(Uk) IDLE(L+,Ps,Uk) IDLE(Uk)*4
* Delay |- IDLE(L+) IDLE(L+,Ps) -
* Fn Down |(L+) -*1 WAITING(Sk) IDLE(Rf,Ps)*7
* Up |(L-) IDLE(L-)*8 IDLE(L-)*8 IDLE(L-)*8
* Fnk Down |DELAYING(Sf)* (Rf) WAITING(Sk) IDLE(Rf,Ps,Rf)
* Up |(L-) IDLE(L-/Uf)*8 IDLE(Rf,Uf/L-)*3 IDLE(Rf,Ps,Uf/L-)*3
* Key Down |PRESSING(Rk) (Rk) WAITING(Sk) IDLE(Rf,Ps,Rk)
* Up |(Uk) IDLE(Uk)*4 (Uk) IDLE(L+,Ps,Pk)/(Uk)*a
* |
* No key Down|IDLE(Ld) IDLE(Ld) IDLE(Ld) IDLE(Ld)
* Delay |- - IDLE(L+) IDLE(L+,Ps)
* Magic Key |COMMAND*5
*
* *1: ignore Fn if other key is down.
* *2: register Fnk if any key is pressing
* *3: when Fnk == Stored Fnk, if not ignore.
* *4: when no registered key any more
* *3: register/unregister delayed Fnk and move to IDLE if code == delayed Fnk, else *8
* *4: if no keys registered to host
* *5: unregister all keys
* *6: only if no keys down
* *7: ignore Fn because Fnk key and stored key are down.
* *8: move to IDLE if layer switch(off) occurs, else stay at current state
* *9: repeat key if pressing Fnk twice quickly(move to PRESSING)
* *a: layer switch and process waiting key and code if code == wainting key, else unregister key
*
* States:
* IDLE:
* IDLE: No key is down except modifiers
* DELAYING: delay layer switch after pressing Fn with alt keycode
* WAITING: key is pressed during DELAYING
*
@ -297,17 +304,20 @@ static void unregister_code(uint8_t code)
* Fn: Fn key without alternative keycode
* Fnk: Fn key with alternative keycode
* -: ignore
* Delay: layer switch delay term is elapsed
*
* Actions:
* Rk: register key
* Uk: unregister key
* Rf: register stored Fn(alt keycode)
* Uf: unregister stored Fn(alt keycode)
* Rf: register Fn(alt keycode)
* Uf: unregister Fn(alt keycode)
* Rs: register stored key
* Us: unregister stored key
* Sk: store key
* Sf: store Fn
* Ps: play stored key(Interpret stored key and transit state)
* Sk: Store key(waiting Key)
* Sf: Store Fn(delayed Fn)
* Ps: Process stored key
* Ps: Process key
* Is: Interpret stored keys in current layer
* L+: Switch to new layer(*unregister* all keys but modifiers)
* L-: Switch back to last layer(*unregister* all keys but modifiers)
* Ld: Switch back to default layer(*unregister* all keys but modifiers)
@ -344,7 +354,7 @@ static inline void process_key(keyevent_t event)
// 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)));
register_code(code);
NEXT(PRESSING);
} else {
delayed_fn = (keyrecord_t) {
@ -380,16 +390,20 @@ static inline void process_key(keyevent_t event)
// ignored when any key is pressed
break;
case FN_UP:
layer_switch_off(code);
NEXT(IDLE);
if (layer_switch_off(code))
NEXT(IDLE);
break;
case FNK_DOWN:
register_code(keymap_fn_keycode(FN_INDEX(code)));
register_code(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)));
if (layer_switch_off(code)) {
NEXT(IDLE);
} else {
unregister_code(code);
if (!anykey_sent_to_host())
NEXT(IDLE);
}
break;
case KEY_DOWN:
case MOD_DOWN:
@ -398,8 +412,7 @@ static inline void process_key(keyevent_t event)
case KEY_UP:
case MOD_UP:
unregister_code(code);
// TODO: no key registered? mousekey, mediakey, systemkey
if (!host_has_anykey())
if (!anykey_sent_to_host())
NEXT(IDLE);
break;
default:
@ -423,8 +436,8 @@ static inline void process_key(keyevent_t event)
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) {
@ -432,19 +445,16 @@ static inline void process_key(keyevent_t event)
// 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)));
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:

View File

@ -25,7 +25,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#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");
}