forked from kofal.net/zmk
feat(behaviors): lazy sticky keys
This commit is contained in:
committed by
Pete Johanson
parent
8929355ac0
commit
341534aa15
@@ -33,6 +33,7 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
||||
struct behavior_sticky_key_config {
|
||||
uint32_t release_after_ms;
|
||||
bool quick_release;
|
||||
bool lazy;
|
||||
bool ignore_modifiers;
|
||||
struct zmk_behavior_binding behavior;
|
||||
};
|
||||
@@ -146,8 +147,11 @@ static int on_sticky_key_binding_pressed(struct zmk_behavior_binding *binding,
|
||||
return ZMK_BEHAVIOR_OPAQUE;
|
||||
}
|
||||
|
||||
press_sticky_key_behavior(sticky_key, event.timestamp);
|
||||
LOG_DBG("%d new sticky_key", event.position);
|
||||
if (!sticky_key->config->lazy) {
|
||||
// press the key now if it's not lazy
|
||||
press_sticky_key_behavior(sticky_key, event.timestamp);
|
||||
}
|
||||
return ZMK_BEHAVIOR_OPAQUE;
|
||||
}
|
||||
|
||||
@@ -191,14 +195,21 @@ static int sticky_key_keycode_state_changed_listener(const zmk_event_t *eh) {
|
||||
return ZMK_EV_EVENT_BUBBLE;
|
||||
}
|
||||
|
||||
// keep track whether the event has been reraised, so we only reraise it once
|
||||
bool event_reraised = false;
|
||||
// we want to make sure every sticky key is given a chance to lazy press their behavior before
|
||||
// the event gets reraised, and release their behavior after the event is reraised, so we keep
|
||||
// track of them. this allows us to ensure the sticky key is pressed and released "around" the
|
||||
// other key, especially in the case of lazy keys.
|
||||
struct active_sticky_key *sticky_keys_to_press_before_reraise[ZMK_BHV_STICKY_KEY_MAX_HELD];
|
||||
struct active_sticky_key *sticky_keys_to_release_after_reraise[ZMK_BHV_STICKY_KEY_MAX_HELD];
|
||||
|
||||
// reraising the event frees it, so make a copy of any event data we might
|
||||
// need after it's been freed.
|
||||
const struct zmk_keycode_state_changed ev_copy = *ev;
|
||||
|
||||
for (int i = 0; i < ZMK_BHV_STICKY_KEY_MAX_HELD; i++) {
|
||||
sticky_keys_to_press_before_reraise[i] = NULL;
|
||||
sticky_keys_to_release_after_reraise[i] = NULL;
|
||||
|
||||
struct active_sticky_key *sticky_key = &active_sticky_keys[i];
|
||||
if (sticky_key->position == ZMK_BHV_STICKY_KEY_POSITION_FREE) {
|
||||
continue;
|
||||
@@ -212,14 +223,6 @@ static int sticky_key_keycode_state_changed_listener(const zmk_event_t *eh) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// If this event was queued, the timer may be triggered late or not at all.
|
||||
// Release the sticky key if the timer should've run out in the meantime.
|
||||
if (sticky_key->release_at != 0 && ev_copy.timestamp > sticky_key->release_at) {
|
||||
stop_timer(sticky_key);
|
||||
release_sticky_key_behavior(sticky_key, sticky_key->release_at);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ev_copy.state) { // key down
|
||||
if (sticky_key->config->ignore_modifiers &&
|
||||
is_mod(ev_copy.usage_page, ev_copy.keycode)) {
|
||||
@@ -231,17 +234,30 @@ static int sticky_key_keycode_state_changed_listener(const zmk_event_t *eh) {
|
||||
// this sticky key is already in use for a keycode
|
||||
continue;
|
||||
}
|
||||
|
||||
// we don't want the timer to release the sticky key before the other key is released
|
||||
stop_timer(sticky_key);
|
||||
|
||||
// If this event was queued, the timer may be triggered late or not at all.
|
||||
// Release the sticky key if the timer should've run out in the meantime.
|
||||
if (sticky_key->release_at != 0 && ev_copy.timestamp > sticky_key->release_at) {
|
||||
// If the key is lazy, a release is not needed on timeout
|
||||
if (sticky_key->config->lazy) {
|
||||
clear_sticky_key(sticky_key);
|
||||
} else {
|
||||
release_sticky_key_behavior(sticky_key, sticky_key->release_at);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (sticky_key->config->lazy) {
|
||||
// if the sticky key is lazy, we need to press it before the event is reraised
|
||||
sticky_keys_to_press_before_reraise[i] = sticky_key;
|
||||
}
|
||||
if (sticky_key->timer_started) {
|
||||
stop_timer(sticky_key);
|
||||
if (sticky_key->config->quick_release) {
|
||||
// immediately release the sticky key after the key press is handled.
|
||||
if (!event_reraised) {
|
||||
struct zmk_keycode_state_changed_event dupe_ev =
|
||||
copy_raised_zmk_keycode_state_changed(ev);
|
||||
ZMK_EVENT_RAISE_AFTER(dupe_ev, behavior_sticky_key);
|
||||
event_reraised = true;
|
||||
}
|
||||
release_sticky_key_behavior(sticky_key, ev_copy.timestamp);
|
||||
sticky_keys_to_release_after_reraise[i] = sticky_key;
|
||||
}
|
||||
}
|
||||
sticky_key->modified_key_usage_page = ev_copy.usage_page;
|
||||
@@ -251,14 +267,35 @@ static int sticky_key_keycode_state_changed_listener(const zmk_event_t *eh) {
|
||||
sticky_key->modified_key_usage_page == ev_copy.usage_page &&
|
||||
sticky_key->modified_key_keycode == ev_copy.keycode) {
|
||||
stop_timer(sticky_key);
|
||||
release_sticky_key_behavior(sticky_key, ev_copy.timestamp);
|
||||
sticky_keys_to_release_after_reraise[i] = sticky_key;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (event_reraised) {
|
||||
return ZMK_EV_EVENT_CAPTURED;
|
||||
|
||||
// give each sticky key a chance to press their behavior before the event is reraised
|
||||
for (int i = 0; i < ZMK_BHV_STICKY_KEY_MAX_HELD; i++) {
|
||||
struct active_sticky_key *sticky_key = sticky_keys_to_press_before_reraise[i];
|
||||
if (sticky_key) {
|
||||
press_sticky_key_behavior(sticky_key, ev_copy.timestamp);
|
||||
}
|
||||
}
|
||||
return ZMK_EV_EVENT_BUBBLE;
|
||||
// give each sticky key a chance to release their behavior after the event is reraised, lazily
|
||||
// reraising. keep track whether the event has been reraised so we only reraise it once
|
||||
bool event_reraised = false;
|
||||
for (int i = 0; i < ZMK_BHV_STICKY_KEY_MAX_HELD; i++) {
|
||||
struct active_sticky_key *sticky_key = sticky_keys_to_release_after_reraise[i];
|
||||
if (sticky_key) {
|
||||
if (!event_reraised) {
|
||||
struct zmk_keycode_state_changed_event dupe_ev =
|
||||
copy_raised_zmk_keycode_state_changed(ev);
|
||||
ZMK_EVENT_RAISE_AFTER(dupe_ev, behavior_sticky_key);
|
||||
event_reraised = true;
|
||||
}
|
||||
release_sticky_key_behavior(sticky_key, ev_copy.timestamp);
|
||||
}
|
||||
}
|
||||
|
||||
return event_reraised ? ZMK_EV_EVENT_CAPTURED : ZMK_EV_EVENT_BUBBLE;
|
||||
}
|
||||
|
||||
void behavior_sticky_key_timer_handler(struct k_work *item) {
|
||||
@@ -271,7 +308,12 @@ void behavior_sticky_key_timer_handler(struct k_work *item) {
|
||||
if (sticky_key->timer_cancelled) {
|
||||
sticky_key->timer_cancelled = false;
|
||||
} else {
|
||||
release_sticky_key_behavior(sticky_key, sticky_key->release_at);
|
||||
// If the key is lazy, a release is not needed on timeout
|
||||
if (sticky_key->config->lazy) {
|
||||
clear_sticky_key(sticky_key);
|
||||
} else {
|
||||
release_sticky_key_behavior(sticky_key, sticky_key->release_at);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -295,8 +337,9 @@ static struct behavior_sticky_key_data behavior_sticky_key_data;
|
||||
static struct behavior_sticky_key_config behavior_sticky_key_config_##n = { \
|
||||
.behavior = ZMK_KEYMAP_EXTRACT_BINDING(0, DT_DRV_INST(n)), \
|
||||
.release_after_ms = DT_INST_PROP(n, release_after_ms), \
|
||||
.ignore_modifiers = DT_INST_PROP(n, ignore_modifiers), \
|
||||
.quick_release = DT_INST_PROP(n, quick_release), \
|
||||
.lazy = DT_INST_PROP(n, lazy), \
|
||||
.ignore_modifiers = DT_INST_PROP(n, ignore_modifiers), \
|
||||
}; \
|
||||
BEHAVIOR_DT_INST_DEFINE(n, behavior_sticky_key_init, NULL, &behavior_sticky_key_data, \
|
||||
&behavior_sticky_key_config_##n, POST_KERNEL, \
|
||||
|
||||
Reference in New Issue
Block a user