From cac33f493aeeac7a7e5e235d283e37a6a6682baf Mon Sep 17 00:00:00 2001 From: Sophie Tyalie Date: Thu, 17 Nov 2022 21:04:37 +0100 Subject: [PATCH] Behavior: Lock: Make persistence configurable Persistence of a custom lock is disabled by default, but it can now be enabled in the dts keyboard config similar to this: ``` deadlock: deadlock { compatible = "zmk,behavior-custom-lock"; label = "dead-key-toggle"; #binding-cells = <0>; persistence = "enabled"; } ``` Currently the options are `disabled`, `enabled` and `per-profile`, where the latter doesn't do anything different to `enabled` as of right now. Signed-off-by: Sophie Tyalie --- .../behaviors/zmk,behavior-custom-lock.yaml | 10 +++++++ app/src/behaviors/behavior_custom_lock.c | 27 ++++++++++++++++--- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/app/dts/bindings/behaviors/zmk,behavior-custom-lock.yaml b/app/dts/bindings/behaviors/zmk,behavior-custom-lock.yaml index 4cf79c5d..19fa0225 100644 --- a/app/dts/bindings/behaviors/zmk,behavior-custom-lock.yaml +++ b/app/dts/bindings/behaviors/zmk,behavior-custom-lock.yaml @@ -7,6 +7,16 @@ compatible: "zmk,behavior-custom-lock" include: zero_param.yaml +properties: + persistence: + type: string + default: "disabled" + required: false + enum: + - "disabled" + - "enabled" + - "per-profile" + child-binding: description: Key definitions for lock variable diff --git a/app/src/behaviors/behavior_custom_lock.c b/app/src/behaviors/behavior_custom_lock.c index 175cb129..31c83ac4 100644 --- a/app/src/behaviors/behavior_custom_lock.c +++ b/app/src/behaviors/behavior_custom_lock.c @@ -26,6 +26,10 @@ struct behavior_custom_lock_key_config { char *locked_behavior_dev; }; +struct behavior_custom_lock_var_config { + uint8_t persistence; +}; + struct behavior_custom_lock_var_data { bool active; }; @@ -88,8 +92,15 @@ static int lock_settings_load(const char *name, size_t len, settings_read_cb rea } struct behavior_custom_lock_var_data* data = dev->data; + const struct behavior_custom_lock_var_config *config = dev->config; int rc; + if (config->persistence == 0) { + LOG_WRN("key %s is not marked as persistent (%d), deleting", name, config->persistence); + lock_delete_state(name); + return 0; + } + if (len != sizeof(bool)) { LOG_DBG("something is of with size %d", len); return -EINVAL; @@ -182,9 +193,13 @@ static int on_keymap_var_binding_pressed(struct zmk_behavior_binding *binding, struct zmk_behavior_binding_event event) { const struct device *dev = device_get_binding(binding->behavior_dev); struct behavior_custom_lock_var_data* data = dev->data; + const struct behavior_custom_lock_var_config* config = dev->config; data->active = !data->active; - lock_save_state(dev->name, data->active); + + if (config->persistence != 0) { + lock_save_state(dev->name, data->active); + } return ZMK_BEHAVIOR_OPAQUE; } @@ -217,9 +232,13 @@ static const struct behavior_driver_api behavior_lock_var_driver_api = { #define CL_INST(id) \ DT_INST_FOREACH_CHILD(id, CL_CHILD) \ static struct behavior_custom_lock_var_data behavior_lock_var_data_##id = { .active = false }; \ - \ - DEVICE_DT_INST_DEFINE(id, &behavior_lock_var_init, NULL, &behavior_lock_var_data_##id, \ - NULL, APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &behavior_lock_var_driver_api); + static struct behavior_custom_lock_var_config behavior_lock_var_config_##id = { \ + .persistence = DT_INST_ENUM_IDX(id, persistence), \ + };\ + \ + DEVICE_DT_INST_DEFINE(id, &behavior_lock_var_init, NULL, \ + &behavior_lock_var_data_##id, &behavior_lock_var_config_##id, \ + APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &behavior_lock_var_driver_api); DT_INST_FOREACH_STATUS_OKAY(CL_INST)