feat(split): Make locality work nested behavior invocations

Co-authored-by: Tokazio <tokazio@hotmail.com>
This commit is contained in:
Cem Aksoylar
2023-01-17 19:05:04 +01:00
committed by Pete Johanson
parent 11f600d9e5
commit 9e36ebd525
13 changed files with 146 additions and 105 deletions

View File

@@ -18,7 +18,6 @@
#include <zmk/events/position_state_changed.h>
#include <zmk/events/keycode_state_changed.h>
#include <zmk/behavior.h>
#include <zmk/keymap.h>
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
@@ -77,6 +76,7 @@ struct behavior_hold_tap_data {
// this data is specific for each hold-tap
struct active_hold_tap {
int32_t position;
uint8_t source;
uint32_t param_hold;
uint32_t param_tap;
int64_t timestamp;
@@ -250,14 +250,16 @@ static struct active_hold_tap *find_hold_tap(uint32_t position) {
return NULL;
}
static struct active_hold_tap *store_hold_tap(uint32_t position, uint32_t param_hold,
uint32_t param_tap, int64_t timestamp,
static struct active_hold_tap *store_hold_tap(uint32_t position, uint8_t source,
uint32_t param_hold, uint32_t param_tap,
int64_t timestamp,
const struct behavior_hold_tap_config *config) {
for (int i = 0; i < ZMK_BHV_HOLD_TAP_MAX_HELD; i++) {
if (active_hold_taps[i].position != ZMK_BHV_HOLD_TAP_POSITION_NOT_USED) {
continue;
}
active_hold_taps[i].position = position;
active_hold_taps[i].source = source;
active_hold_taps[i].status = STATUS_UNDECIDED;
active_hold_taps[i].config = config;
active_hold_taps[i].param_hold = param_hold;
@@ -400,45 +402,49 @@ static int press_hold_binding(struct active_hold_tap *hold_tap) {
struct zmk_behavior_binding_event event = {
.position = hold_tap->position,
.timestamp = hold_tap->timestamp,
.source = hold_tap->source,
};
struct zmk_behavior_binding binding = {.behavior_dev = hold_tap->config->hold_behavior_dev,
.param1 = hold_tap->param_hold};
return behavior_keymap_binding_pressed(&binding, event);
return zmk_behavior_invoke_binding(&binding, event, true);
}
static int press_tap_binding(struct active_hold_tap *hold_tap) {
struct zmk_behavior_binding_event event = {
.position = hold_tap->position,
.timestamp = hold_tap->timestamp,
.source = hold_tap->source,
};
struct zmk_behavior_binding binding = {.behavior_dev = hold_tap->config->tap_behavior_dev,
.param1 = hold_tap->param_tap};
store_last_hold_tapped(hold_tap);
return behavior_keymap_binding_pressed(&binding, event);
return zmk_behavior_invoke_binding(&binding, event, true);
}
static int release_hold_binding(struct active_hold_tap *hold_tap) {
struct zmk_behavior_binding_event event = {
.position = hold_tap->position,
.timestamp = hold_tap->timestamp,
.source = hold_tap->source,
};
struct zmk_behavior_binding binding = {.behavior_dev = hold_tap->config->hold_behavior_dev,
.param1 = hold_tap->param_hold};
return behavior_keymap_binding_released(&binding, event);
return zmk_behavior_invoke_binding(&binding, event, false);
}
static int release_tap_binding(struct active_hold_tap *hold_tap) {
struct zmk_behavior_binding_event event = {
.position = hold_tap->position,
.timestamp = hold_tap->timestamp,
.source = hold_tap->source,
};
struct zmk_behavior_binding binding = {.behavior_dev = hold_tap->config->tap_behavior_dev,
.param1 = hold_tap->param_tap};
return behavior_keymap_binding_released(&binding, event);
return zmk_behavior_invoke_binding(&binding, event, false);
}
static int press_binding(struct active_hold_tap *hold_tap) {
@@ -597,8 +603,8 @@ static int on_hold_tap_binding_pressed(struct zmk_behavior_binding *binding,
return ZMK_BEHAVIOR_OPAQUE;
}
struct active_hold_tap *hold_tap =
store_hold_tap(event.position, binding->param1, binding->param2, event.timestamp, cfg);
struct active_hold_tap *hold_tap = store_hold_tap(event.position, event.source, binding->param1,
binding->param2, event.timestamp, cfg);
if (hold_tap == NULL) {
LOG_ERR("unable to store hold-tap info, did you press more than %d hold-taps?",
ZMK_BHV_HOLD_TAP_MAX_HELD);

View File

@@ -158,7 +158,8 @@ static void replace_params(struct behavior_macro_trigger_state *state,
state->param2_source = PARAM_SOURCE_BINDING;
}
static void queue_macro(uint32_t position, const struct zmk_behavior_binding bindings[],
static void queue_macro(uint32_t position, uint8_t source,
const struct zmk_behavior_binding bindings[],
struct behavior_macro_trigger_state state,
const struct zmk_behavior_binding *macro_binding) {
LOG_DBG("Iterating macro bindings - starting: %d, count: %d", state.start_index, state.count);
@@ -169,14 +170,14 @@ static void queue_macro(uint32_t position, const struct zmk_behavior_binding bin
switch (state.mode) {
case MACRO_MODE_TAP:
zmk_behavior_queue_add(position, binding, true, state.tap_ms);
zmk_behavior_queue_add(position, binding, false, state.wait_ms);
zmk_behavior_queue_add(position, source, binding, true, state.tap_ms);
zmk_behavior_queue_add(position, source, binding, false, state.wait_ms);
break;
case MACRO_MODE_PRESS:
zmk_behavior_queue_add(position, binding, true, state.wait_ms);
zmk_behavior_queue_add(position, source, binding, true, state.wait_ms);
break;
case MACRO_MODE_RELEASE:
zmk_behavior_queue_add(position, binding, false, state.wait_ms);
zmk_behavior_queue_add(position, source, binding, false, state.wait_ms);
break;
default:
LOG_ERR("Unknown macro mode: %d", state.mode);
@@ -197,7 +198,7 @@ static int on_macro_binding_pressed(struct zmk_behavior_binding *binding,
.start_index = 0,
.count = state->press_bindings_count};
queue_macro(event.position, cfg->bindings, trigger_state, binding);
queue_macro(event.position, event.source, cfg->bindings, trigger_state, binding);
return ZMK_BEHAVIOR_OPAQUE;
}
@@ -208,7 +209,7 @@ static int on_macro_binding_released(struct zmk_behavior_binding *binding,
const struct behavior_macro_config *cfg = dev->config;
struct behavior_macro_state *state = dev->data;
queue_macro(event.position, cfg->bindings, state->release_state, binding);
queue_macro(event.position, event.source, cfg->bindings, state->release_state, binding);
return ZMK_BEHAVIOR_OPAQUE;
}

View File

@@ -51,7 +51,7 @@ static int on_mod_morph_binding_pressed(struct zmk_behavior_binding *binding,
} else {
data->pressed_binding = (struct zmk_behavior_binding *)&cfg->normal_binding;
}
return behavior_keymap_binding_pressed(data->pressed_binding, event);
return zmk_behavior_invoke_binding(data->pressed_binding, event, true);
}
static int on_mod_morph_binding_released(struct zmk_behavior_binding *binding,
@@ -67,7 +67,7 @@ static int on_mod_morph_binding_released(struct zmk_behavior_binding *binding,
struct zmk_behavior_binding *pressed_binding = data->pressed_binding;
data->pressed_binding = NULL;
int err;
err = behavior_keymap_binding_released(pressed_binding, event);
err = zmk_behavior_invoke_binding(pressed_binding, event, false);
zmk_hid_masked_modifiers_clear();
return err;
}

View File

@@ -90,8 +90,8 @@ int zmk_behavior_sensor_rotate_common_process(struct zmk_behavior_binding *bindi
LOG_DBG("Sensor binding: %s", binding->behavior_dev);
for (int i = 0; i < triggers; i++) {
zmk_behavior_queue_add(event.position, triggered_binding, true, cfg->tap_ms);
zmk_behavior_queue_add(event.position, triggered_binding, false, 0);
zmk_behavior_queue_add(event.position, event.source, triggered_binding, true, cfg->tap_ms);
zmk_behavior_queue_add(event.position, event.source, triggered_binding, false, 0);
}
return ZMK_BEHAVIOR_OPAQUE;

View File

@@ -40,6 +40,7 @@ struct behavior_sticky_key_config {
struct active_sticky_key {
uint32_t position;
uint8_t source;
uint32_t param1;
uint32_t param2;
const struct behavior_sticky_key_config *config;
@@ -55,8 +56,8 @@ struct active_sticky_key {
struct active_sticky_key active_sticky_keys[ZMK_BHV_STICKY_KEY_MAX_HELD] = {};
static struct active_sticky_key *store_sticky_key(uint32_t position, uint32_t param1,
uint32_t param2,
static struct active_sticky_key *store_sticky_key(uint32_t position, uint8_t source,
uint32_t param1, uint32_t param2,
const struct behavior_sticky_key_config *config) {
for (int i = 0; i < ZMK_BHV_STICKY_KEY_MAX_HELD; i++) {
struct active_sticky_key *const sticky_key = &active_sticky_keys[i];
@@ -65,6 +66,7 @@ static struct active_sticky_key *store_sticky_key(uint32_t position, uint32_t pa
continue;
}
sticky_key->position = position;
sticky_key->source = source;
sticky_key->param1 = param1;
sticky_key->param2 = param2;
sticky_key->config = config;
@@ -101,8 +103,9 @@ static inline int press_sticky_key_behavior(struct active_sticky_key *sticky_key
struct zmk_behavior_binding_event event = {
.position = sticky_key->position,
.timestamp = timestamp,
.source = sticky_key->source,
};
return behavior_keymap_binding_pressed(&binding, event);
return zmk_behavior_invoke_binding(&binding, event, true);
}
static inline int release_sticky_key_behavior(struct active_sticky_key *sticky_key,
@@ -115,10 +118,11 @@ static inline int release_sticky_key_behavior(struct active_sticky_key *sticky_k
struct zmk_behavior_binding_event event = {
.position = sticky_key->position,
.timestamp = timestamp,
.source = sticky_key->source,
};
clear_sticky_key(sticky_key);
return behavior_keymap_binding_released(&binding, event);
return zmk_behavior_invoke_binding(&binding, event, false);
}
static inline void on_sticky_key_timeout(struct active_sticky_key *sticky_key) {
@@ -149,7 +153,8 @@ static int on_sticky_key_binding_pressed(struct zmk_behavior_binding *binding,
stop_timer(sticky_key);
release_sticky_key_behavior(sticky_key, event.timestamp);
}
sticky_key = store_sticky_key(event.position, binding->param1, binding->param2, cfg);
sticky_key =
store_sticky_key(event.position, event.source, binding->param1, binding->param2, cfg);
if (sticky_key == NULL) {
LOG_ERR("unable to store sticky key, did you press more than %d sticky_key?",
ZMK_BHV_STICKY_KEY_MAX_HELD);

View File

@@ -35,6 +35,7 @@ struct active_tap_dance {
// Tap Dance Data
int counter;
uint32_t position;
uint8_t source;
uint32_t param1;
uint32_t param2;
bool is_pressed;
@@ -59,13 +60,15 @@ static struct active_tap_dance *find_tap_dance(uint32_t position) {
return NULL;
}
static int new_tap_dance(uint32_t position, const struct behavior_tap_dance_config *config,
static int new_tap_dance(uint32_t position, uint8_t source,
const struct behavior_tap_dance_config *config,
struct active_tap_dance **tap_dance) {
for (int i = 0; i < ZMK_BHV_TAP_DANCE_MAX_HELD; i++) {
struct active_tap_dance *const ref_dance = &active_tap_dances[i];
if (ref_dance->position == ZMK_BHV_TAP_DANCE_POSITION_FREE) {
ref_dance->counter = 0;
ref_dance->position = position;
ref_dance->source = source;
ref_dance->config = config;
ref_dance->release_at = 0;
ref_dance->is_pressed = true;
@@ -108,8 +111,9 @@ static inline int press_tap_dance_behavior(struct active_tap_dance *tap_dance, i
struct zmk_behavior_binding_event event = {
.position = tap_dance->position,
.timestamp = timestamp,
.source = tap_dance->source,
};
return behavior_keymap_binding_pressed(&binding, event);
return zmk_behavior_invoke_binding(&binding, event, true);
}
static inline int release_tap_dance_behavior(struct active_tap_dance *tap_dance,
@@ -118,9 +122,10 @@ static inline int release_tap_dance_behavior(struct active_tap_dance *tap_dance,
struct zmk_behavior_binding_event event = {
.position = tap_dance->position,
.timestamp = timestamp,
.source = tap_dance->source,
};
clear_tap_dance(tap_dance);
return behavior_keymap_binding_released(&binding, event);
return zmk_behavior_invoke_binding(&binding, event, false);
}
static int on_tap_dance_binding_pressed(struct zmk_behavior_binding *binding,
@@ -130,7 +135,7 @@ static int on_tap_dance_binding_pressed(struct zmk_behavior_binding *binding,
struct active_tap_dance *tap_dance;
tap_dance = find_tap_dance(event.position);
if (tap_dance == NULL) {
if (new_tap_dance(event.position, cfg, &tap_dance) == -ENOMEM) {
if (new_tap_dance(event.position, event.source, cfg, &tap_dance) == -ENOMEM) {
LOG_ERR("Unable to create new tap dance. Insufficient space in active_tap_dances[].");
return ZMK_BEHAVIOR_OPAQUE;
}
@@ -261,4 +266,4 @@ static int behavior_tap_dance_init(const struct device *dev) {
DT_INST_FOREACH_STATUS_OKAY(KP_INST)
#endif
#endif