1
0

Fix mods with tapping.

This commit is contained in:
tmk 2013-01-20 15:03:07 +09:00
parent ee7ce43335
commit f71a5217b7
2 changed files with 189 additions and 191 deletions

View File

@ -23,57 +23,83 @@ static void layer_switch(uint8_t new_layer);
/* tap */ /* tap */
#define TAP_TIME 200 #define TAP_TIME 300
static keyevent_t last_event = {}; /* This counts up when tap occurs */
static uint8_t tap_count = 0; static uint8_t tap_count = 0;
static bool is_tap_key(keyevent_t event)
{
action_t action = keymap_get_action(current_layer, event.key.pos.row, event.key.pos.col);
switch (action.kind.id) {
case ACT_LMODS_TAP:
case ACT_RMODS_TAP:
return true;
case ACT_LAYER_PRESSED:
case ACT_LAYER_BIT:
switch (action.layer.code) {
case 0x00:
case 0xF1 ... 0xFF:
return false;
case 0xF0:
default:
return true;
}
return false;
}
return false;
}
/* layer */ /* layer */
uint8_t default_layer = 0; uint8_t default_layer = 0;
uint8_t current_layer = 0; uint8_t current_layer = 0;
static keyrecord_t tapping_key = {}; static keyevent_t tapping_event = {};
// time 0 means no event.
#define IS_TAPPING (tapping_key.event.time != 0) /* TAPPING: This indicates that whether tap or not is not decided yet. */
/* TODO: // NOTE: keyevent_t.time 0 means no event.
#define IS_TAPPING_KEY(key) (tapping_key.event.time != 0 && KEYEQ(tapping_key.event.key, key)) #define IS_TAPPING(k) (tapping_event.time != 0 && KEYEQ(tapping_event.key, (k)))
*/
/* waiting keys buffer */ /* waiting keys buffer */
#define WAITING_KEYS_BUFFER 3 #define WAITING_KEYS_BUFFER 8
static keyrecord_t waiting_keys[WAITING_KEYS_BUFFER] = {}; static keyevent_t waiting_events[WAITING_KEYS_BUFFER] = {};
// TODO: double buffer? static uint8_t waiting_events_head = 0;
static keyrecord_t waiting_keys0[WAITING_KEYS_BUFFER] = {}; static bool waiting_events_enqueue(keyevent_t event)
static keyrecord_t waiting_keys1[WAITING_KEYS_BUFFER] = {};
static uint8_t waiting_keys_head = 0;
static bool waiting_keys_enqueue(keyevent_t event)
{ {
debug("waiting_keys["); debug_dec(waiting_keys_head); debug("] = "); if (IS_NOEVENT(event)) { return true; }
if (waiting_events_head < WAITING_KEYS_BUFFER) {
debug("waiting_events["); debug_dec(waiting_events_head); debug("] = ");
debug_hex16(event.key.raw); debug("\n"); debug_hex16(event.key.raw); debug("\n");
if (waiting_keys_head < WAITING_KEYS_BUFFER) { waiting_events[waiting_events_head++] = event;
waiting_keys[waiting_keys_head++] = (keyrecord_t){ .event = event,
.mods = host_get_mods() };
} else {
return true; return true;
} }
debug("waiting_events_enqueue: Over flow.\n");
return false;
} }
static void waiting_keys_clear(void) static void waiting_events_clear(void)
{ {
waiting_keys_head = 0; waiting_events_head = 0;
} }
static bool waiting_keys_has(key_t key) static bool waiting_events_has(key_t key)
{ {
for (uint8_t i = 0; i < waiting_keys_head; i++) { for (uint8_t i = 0; i < waiting_events_head; i++) {
if KEYEQ(key, waiting_keys[i].event.key) return true; if KEYEQ(key, waiting_events[i].key) return true;
} }
return false; return false;
} }
static void waiting_keys_process_in_current_layer(void) static void waiting_events_process_in_current_layer(void)
{ {
// TODO: in case of including layer key in waiting keys // TODO: in case of including tap key in waiting keys
for (uint8_t i = 0; i < waiting_keys_head; i++) { for (uint8_t i = 0; i < waiting_events_head; i++) {
debug("waiting_keys_process_in_current_layer["); debug_dec(i); debug("]\n"); debug("waiting_events_process_in_current_layer["); debug_dec(i); debug("]\n");
process(waiting_keys[i].event); process(waiting_events[i]);
} }
waiting_keys_clear(); waiting_events_clear();
}
static bool waiting_events_has_anykey_pressed(void)
{
for (uint8_t i = 0; i < waiting_events_head; i++) {
if (waiting_events[i].pressed) return true;
}
return false;
} }
@ -85,62 +111,106 @@ void action_exec(keyevent_t event)
if (event.pressed) debug("down"); else debug("up"); if (event.pressed) debug("down"); else debug("up");
debug("]\n"); debug("]\n");
} }
if (IS_TAPPING) {
/* when tap time elapses or waiting key is released */
if ((timer_elapsed(tapping_key.event.time) > TAP_TIME) ||
(!event.pressed && waiting_keys_has(event.key))) {
// TODO process tapping_key: layer swich, modifier, ... // In tapping term
// action is needed? if (tapping_event.time && timer_elapsed(tapping_event.time) < TAP_TIME) {
debug("notap: process tapping_key.\n"); if (tapping_event.pressed) {
process(tapping_key.event); if (!event.pressed && KEYEQ(tapping_event.key, event.key)) {
debug("Tapping: Release tap key.\n");
if (tap_count == 0) {
debug("Tapping: First tap.\n");
// count up on release
tap_count++;
/* Process waiting keys in new layer */ process(tapping_event);
waiting_keys_process_in_current_layer(); waiting_events_process_in_current_layer();
} }
/* when tapping key is released within tap time */ tapping_event = event;
else if (!event.pressed && KEYEQ(event.key, tapping_key.event.key)) { process(event);
/* tap key down */ } else if (!event.pressed && waiting_events_has(event.key)) {
debug("tap("); debug_hex8(tap_count); debug(")[tapping_key](register): "); debug_hex8(tapping_key.action.layer.code); debug("\n"); debug("Tapping: End(No tap by typing waiting key).\n");
register_code(tapping_key.action.layer.code);
tapping_key = (keyrecord_t){};
/* process waiting keys */ process(tapping_event);
waiting_keys_process_in_current_layer(); waiting_events_process_in_current_layer();
} process(event);
}
// not real event. event just to handle time out of tapping key. tap_count = 0;
if (IS_NOEVENT(event)) { tapping_event = (keyevent_t){};
} else {
//debug("Tapping: pressing tap key.\n");
if (tap_count == 0) {
// store event
waiting_events_enqueue(event);
return; return;
} }
/* count up tap when key is up */
// key: d u d u d
// tap: 0 1 1 2 2
// key: u d u d u
// tap: 0 0 1 1 2
if (KEYEQ(event.key, last_event.key) && timer_elapsed(last_event.time) <= TAP_TIME) {
if (!event.pressed) tap_count++;
} else {
tap_count = 0;
}
/* store key events while tapping */
if (IS_TAPPING) {
// TODO: action is needed?
waiting_keys_enqueue(event);
} else {
process(event); process(event);
} }
} else {
//debug("Tapping after releasing tap.\n");
// Sequential tap
if (tap_count && event.pressed && KEYEQ(tapping_event.key, event.key)) {
tap_count++;
tapping_event = event;
debug("Tapping: Sequential tap("); debug_hex(tap_count); debug(")\n");
}
process(event);
}
}
// Not in tapping term
else {
if (tapping_event.time) {
if (tapping_event.pressed) {
if (tap_count == 0) {
// Not tap, holding down normal key.
debug("Not tap.\n");
process(tapping_event);
waiting_events_process_in_current_layer();
/* last event */ tap_count = 0;
last_event = event; tapping_event = (keyevent_t){};
process(event);
} else {
// Holding down last tap key.
//debug("Time out with holding last tap.\n");
process(event);
if (!event.pressed && KEYEQ(tapping_event.key, event.key)) {
debug("Tapping: End(Release holding last tap).\n");
// clear after release last tap key
tap_count = 0;
tapping_event = (keyevent_t){};
waiting_events_clear();
}
}
} else {
// time out for sequential tap after complete last tap
debug("Tapping: End(Time out after releasing last tap).\n");
tap_count = 0;
tapping_event = (keyevent_t){};
waiting_events_clear();
process(event);
}
} else {
// Normal state without tapping
if (event.pressed && is_tap_key(event)) {
debug("Tapping: Start(Press tap key).\n");
tapping_event = event;
tap_count = 0;
waiting_events_clear();
} else {
//debug("Normal event(No tapping)\n");
process(event);
}
}
}
} }
static void process(keyevent_t event) static void process(keyevent_t event)
{ {
if (IS_NOEVENT(event)) { return; }
action_t action = keymap_get_action(current_layer, event.key.pos.row, event.key.pos.col); action_t action = keymap_get_action(current_layer, event.key.pos.row, event.key.pos.col);
debug("action: "); debug_hex16(action.code); debug("action: "); debug_hex16(action.code);
if (event.pressed) debug("[down]\n"); else debug("[up]\n"); if (event.pressed) debug("[down]\n"); else debug("[up]\n");
@ -198,82 +268,32 @@ static void process(keyevent_t event)
} }
break; break;
case ACT_LMODS_TAP: case ACT_LMODS_TAP:
if (event.pressed) {
if (tap_count == 0) {
if (host_has_anykey()) {
// This key is a modifier essentially.
// Prioritize mods when key jam or rollover
add_mods(action.key.mods);
} else {
if (IS_TAPPING && KEYEQ(tapping_key.event.key, event.key)) {
// no tapping
add_mods(action.key.mods);
tapping_key = (keyrecord_t){};
} else {
debug("tapping lmods("); debug_hex8(action.key.mods); debug(")\n");
tapping_key = (keyrecord_t){
.event = event,
.action = action,
.mods = host_get_mods()
};
}
}
} else {
// pressed after tapping
debug("tap("); debug_hex(tap_count); debug(")[lmods](register): "); debug_hex8(action.key.code); debug("\n");
register_code(action.key.code);
}
} else {
if (tap_count == 0) {
debug("tap(00)[lmods](del_mods): "); debug_hex8(action.key.mods); debug("\n");
del_mods(action.key.mods);
} else if (tap_count == 1) {
debug("tap(01)[lmods](del_mods/unregister): "); debug_hex8(action.key.mods); debug(" "); debug_hex8(action.key.code); debug("\n");
del_mods(action.key.mods);
unregister_code(action.key.code);
} else {
debug("tap("); debug_hex(tap_count); debug(")[lmods](unregister): "); debug_hex8(action.key.code); debug("\n");
unregister_code(action.key.code);
}
}
break;
case ACT_RMODS_TAP: case ACT_RMODS_TAP:
{
uint8_t tmp_mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods :
action.key.mods<<4;
if (event.pressed) { if (event.pressed) {
if (tap_count == 0) { if (IS_TAPPING(event.key) && tap_count > 0) {
if (host_has_anykey()) { if (waiting_events_has_anykey_pressed()) {
// This key is a modifier essentially. debug("MODS_TAP: Tap: Cancel: add_mods\n");
// Prioritize mods when key jam or rollover tap_count = 0;
add_mods(action.key.mods<<4); add_mods(tmp_mods);
} else { } else {
if (IS_TAPPING && KEYEQ(tapping_key.event.key, event.key)) { debug("MODS_TAP: Tap: register_code\n");
// no tapping
add_mods(action.key.mods<<4);
tapping_key = (keyrecord_t){};
} else {
debug("tapping rmods("); debug_hex8(action.key.mods); debug(")\n");
tapping_key = (keyrecord_t){
.event = event,
.action = action,
.mods = host_get_mods()
};
}
}
} else {
// pressed after tapping
debug("tap("); debug_hex(tap_count); debug(")[rmods](register): "); debug_hex8(action.key.code); debug("\n");
register_code(action.key.code); register_code(action.key.code);
} }
} else { } else {
if (tap_count == 0) { debug("MODS_TAP: No tap: add_mods\n");
debug("tap(00)[rmods](del_mods): "); debug_hex8(action.key.mods); debug("\n"); add_mods(tmp_mods);
del_mods(action.key.mods<<4); }
} else if (tap_count == 1) { } else {
debug("tap(01)[rmods](del_mods/unregister): "); debug_hex8(action.key.mods); debug(" "); debug_hex8(action.key.code); debug("\n"); if (IS_TAPPING(event.key) && tap_count > 0) {
del_mods(action.key.mods<<4); debug("MODS_TAP: Tap: unregister_code\n");
unregister_code(action.key.code); unregister_code(action.key.code);
} else { } else {
debug("tap("); debug_hex(tap_count); debug(")[rmods](unregister): "); debug_hex8(action.key.code); debug("\n"); debug("MODS_TAP: No tap: add_mods\n");
unregister_code(action.key.code); del_mods(tmp_mods);
}
} }
} }
break; break;
@ -334,33 +354,20 @@ static void process(keyevent_t event)
default: default:
// with tap key // with tap key
if (event.pressed) { if (event.pressed) {
if (tap_count == 0) { if (IS_TAPPING(event.key) && tap_count > 0) {
if (host_has_anykey()) { debug("LAYER_PRESSED: Tap: register_code\n");
// This key is a normal key than a leyar key essentially.
// Prioritize 'tap key' when key jam or rollover
register_code(action.layer.code); register_code(action.layer.code);
} else { } else {
if (IS_TAPPING && KEYEQ(tapping_key.event.key, event.key)) { debug("LAYER_PRESSED: No tap: layer_switch\n");
layer_switch(action.layer.opt); layer_switch(action.layer.opt);
tapping_key = (keyrecord_t){};
} else {
debug("tapping layer("); debug_hex8(action.layer.opt); debug(")\n");
tapping_key = (keyrecord_t){
.event = event,
.action = action,
.mods = host_get_mods()
};
}
}
} else if (tap_count > 0) {
// pressed after tapping
debug("tap[layer](register): "); debug_hex(tap_count); debug("\n");
register_code(action.layer.code);
} }
} else { } else {
// released after tapping if (IS_TAPPING(event.key) && tap_count > 0) {
debug("tap[layer](unregister): "); debug_hex(tap_count); debug("\n"); debug("LAYER_PRESSED: Tap: unregister_code\n");
unregister_code(action.layer.code); unregister_code(action.layer.code);
} else {
debug("LAYER_PRESSED: No tap: NO ACTION\n");
}
} }
break; break;
} }
@ -409,32 +416,23 @@ static void process(keyevent_t event)
} }
break; break;
default: default:
// TODO: see ACT_LAYER_PRESSED code
// with tap key // with tap key
if (event.pressed) { if (event.pressed) {
if (tap_count == 0) { if (IS_TAPPING(event.key) && tap_count > 0) {
if (host_has_anykey()) { debug("LAYER_BIT: Tap: register_code\n");
register_code(action.layer.code); register_code(action.layer.code);
} else { } else {
tapping_key = (keyrecord_t){ debug("LAYER_BIT: No tap: layer_switch(bit on)\n");
.event = event, layer_switch(current_layer | action.layer.opt);
.action = action,
.mods = keyboard_report->mods
};
}
} else if (tap_count > 0) {
debug("tap[layer_bit](register): "); debug_hex(tap_count); debug("\n");
register_code(action.layer.code);
} }
} else { } else {
if (tap_count == 0) { if (IS_TAPPING(event.key) && tap_count > 0) {
// no tap debug("LAYER_BIT: Tap: unregister_code\n");
layer_switch(current_layer & ~action.layer.opt);
} else if (tap_count == 1) {
// tap
register_code(action.layer.code);
}
unregister_code(action.layer.code); unregister_code(action.layer.code);
} else {
debug("LAYER_BIT: No tap: layer_switch(bit off)\n");
layer_switch(current_layer & ~action.layer.opt);
}
} }
break; break;
} }