mirror of
https://github.com/zmkfirmware/zmk.git
synced 2026-04-01 02:45:19 -05:00
feat(behaviors): Add behavior metadata information.
* For upcoming ZMK studio work, make a set of rich metadata available to provide a friendly name for a behavior, and allow super flexible descriptions of the parameters the behaviors take. * Add ability to validate a zmk_behavior_binding against the behavior metadata available.
This commit is contained in:
committed by
Pete Johanson
parent
7cdf1e42ea
commit
03099b04b6
@@ -34,6 +34,10 @@ struct behavior_macro_trigger_state {
|
||||
struct behavior_macro_state {
|
||||
struct behavior_macro_trigger_state release_state;
|
||||
|
||||
#if IS_ENABLED(CONFIG_ZMK_BEHAVIOR_METADATA)
|
||||
struct behavior_parameter_metadata_set set;
|
||||
#endif // IS_ENABLED(CONFIG_ZMK_BEHAVIOR_METADATA)
|
||||
|
||||
uint32_t press_bindings_count;
|
||||
};
|
||||
|
||||
@@ -209,9 +213,100 @@ static int on_macro_binding_released(struct zmk_behavior_binding *binding,
|
||||
return ZMK_BEHAVIOR_OPAQUE;
|
||||
}
|
||||
|
||||
#if IS_ENABLED(CONFIG_ZMK_BEHAVIOR_METADATA)
|
||||
|
||||
static void assign_values_to_set(enum param_source param_source,
|
||||
struct behavior_parameter_metadata_set *set,
|
||||
const struct behavior_parameter_value_metadata *values,
|
||||
size_t values_len) {
|
||||
if (param_source == PARAM_SOURCE_MACRO_1ST) {
|
||||
set->param1_values = values;
|
||||
set->param1_values_len = values_len;
|
||||
} else {
|
||||
set->param2_values = values;
|
||||
set->param2_values_len = values_len;
|
||||
}
|
||||
}
|
||||
|
||||
// This function will dynamically determine the parameter metadata for a particular macro by
|
||||
// inspecting the macro *bindings* to see what behaviors in that list receive the macro parameters,
|
||||
// and then using the metadata from those behaviors for the macro itself.
|
||||
//
|
||||
// Care need be taken, where a behavior in the list takes two parameters, and the macro passes along
|
||||
// a value for the *second* parameter, we need to make sure we find the right metadata set for the
|
||||
// referenced behavior that matches the first parameter.
|
||||
static int get_macro_parameter_metadata(const struct device *macro,
|
||||
struct behavior_parameter_metadata *param_metadata) {
|
||||
const struct behavior_macro_config *cfg = macro->config;
|
||||
struct behavior_macro_state *data = macro->data;
|
||||
struct behavior_macro_trigger_state state = {0};
|
||||
|
||||
for (int i = 0; (i < cfg->count) && (!data->set.param1_values || !data->set.param2_values);
|
||||
i++) {
|
||||
if (handle_control_binding(&state, &cfg->bindings[i]) ||
|
||||
(state.param1_source == PARAM_SOURCE_BINDING &&
|
||||
state.param2_source == PARAM_SOURCE_BINDING)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
LOG_DBG("checking %d for the given state", i);
|
||||
|
||||
struct behavior_parameter_metadata binding_meta;
|
||||
int err = behavior_get_parameter_metadata(
|
||||
zmk_behavior_get_binding(cfg->bindings[i].behavior_dev), &binding_meta);
|
||||
if (err < 0 || binding_meta.sets_len == 0) {
|
||||
LOG_WRN("Failed to fetch macro binding parameter details %d", err);
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
// If both macro parameters get passed to this one entry, use
|
||||
// the metadata for this behavior verbatim.
|
||||
if (state.param1_source != PARAM_SOURCE_BINDING &&
|
||||
state.param2_source != PARAM_SOURCE_BINDING) {
|
||||
param_metadata->sets_len = binding_meta.sets_len;
|
||||
param_metadata->sets = binding_meta.sets;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (state.param1_source != PARAM_SOURCE_BINDING) {
|
||||
assign_values_to_set(state.param1_source, &data->set,
|
||||
binding_meta.sets[0].param1_values,
|
||||
binding_meta.sets[0].param1_values_len);
|
||||
}
|
||||
|
||||
if (state.param2_source != PARAM_SOURCE_BINDING) {
|
||||
// For the param2 metadata, we need to find a set that matches fully bound first
|
||||
// parameter of our macro entry, and use the metadata from that set.
|
||||
for (int s = 0; s < binding_meta.sets_len; s++) {
|
||||
if (zmk_behavior_validate_param_values(binding_meta.sets[s].param1_values,
|
||||
binding_meta.sets[s].param1_values_len,
|
||||
cfg->bindings[i].param1) >= 0) {
|
||||
assign_values_to_set(state.param2_source, &data->set,
|
||||
binding_meta.sets[s].param2_values,
|
||||
binding_meta.sets[s].param2_values_len);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
state.param1_source = PARAM_SOURCE_BINDING;
|
||||
state.param2_source = PARAM_SOURCE_BINDING;
|
||||
}
|
||||
|
||||
param_metadata->sets_len = 1;
|
||||
param_metadata->sets = &data->set;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif // IS_ENABLED(CONFIG_ZMK_BEHAVIOR_METADATA)
|
||||
|
||||
static const struct behavior_driver_api behavior_macro_driver_api = {
|
||||
.binding_pressed = on_macro_binding_pressed,
|
||||
.binding_released = on_macro_binding_released,
|
||||
#if IS_ENABLED(CONFIG_ZMK_BEHAVIOR_METADATA)
|
||||
.get_parameter_metadata = get_macro_parameter_metadata,
|
||||
#endif // IS_ENABLED(CONFIG_ZMK_BEHAVIOR_METADATA)
|
||||
};
|
||||
|
||||
#define TRANSFORMED_BEHAVIORS(n) \
|
||||
|
||||
Reference in New Issue
Block a user