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 <dev@flowerpot.me>
This commit is contained in:
Sophie Tyalie 2022-11-17 21:04:37 +01:00
parent aa1cc023f6
commit cac33f493a
2 changed files with 33 additions and 4 deletions

View file

@ -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

View file

@ -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;
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 }; \
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, \
NULL, APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &behavior_lock_var_driver_api);
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)