Browse Source

Merge pull request #126 from BrainWart/fix-layer-release

Fix keys not releasing when layers change
xmkb
Pete Johanson 4 years ago committed by GitHub
parent
commit
4202db5a01
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 38
      app/src/keymap.c

38
app/src/keymap.c

@ -51,10 +51,10 @@ static u8_t zmk_keymap_layer_default = 0; @@ -51,10 +51,10 @@ static u8_t zmk_keymap_layer_default = 0;
// State
// When a behavior handles a key position "down" event, we record that layer
// When a behavior handles a key position "down" event, we record the layer state
// here so that even if that layer is deactivated before the "up", event, we
// still send the release event to the behavior in that layer also.
static u8_t zmk_keymap_active_behavior_layer[ZMK_KEYMAP_LEN];
static u32_t zmk_keymap_active_behavior_layer[ZMK_KEYMAP_LEN];
static struct zmk_behavior_binding zmk_keymap[ZMK_KEYMAP_LAYERS_LEN][ZMK_KEYMAP_LEN] = {
DT_INST_FOREACH_CHILD(0, TRANSFORMED_LAYER)
@ -101,22 +101,16 @@ int zmk_keymap_layer_toggle(u8_t layer) @@ -101,22 +101,16 @@ int zmk_keymap_layer_toggle(u8_t layer)
return zmk_keymap_layer_activate(layer);
};
bool is_active_position(u32_t position, u8_t layer)
bool is_active_layer(u8_t layer, u32_t layer_state)
{
return (zmk_keymap_layer_state & BIT(layer)) == BIT(layer)
|| layer == zmk_keymap_layer_default
|| zmk_keymap_active_behavior_layer[position] == layer;
return (layer_state & BIT(layer)) == BIT(layer)
|| layer == zmk_keymap_layer_default;
}
int zmk_keymap_position_state_changed(u32_t position, bool pressed)
{
for (int layer = ZMK_KEYMAP_LAYERS_LEN - 1; layer >= zmk_keymap_layer_default; layer--)
{
if (is_active_position(position, layer))
int zmk_keymap_apply_position_state(int layer, u32_t position, bool pressed)
{
struct zmk_behavior_binding *binding = &zmk_keymap[layer][position];
struct device *behavior;
int ret;
LOG_DBG("layer: %d position: %d, binding name: %s", layer, position, log_strdup(binding->behavior_dev));
@ -124,24 +118,34 @@ int zmk_keymap_position_state_changed(u32_t position, bool pressed) @@ -124,24 +118,34 @@ int zmk_keymap_position_state_changed(u32_t position, bool pressed)
if (!behavior) {
LOG_DBG("No behavior assigned to %d on layer %d", position, layer);
continue;
return 1;
}
if (pressed) {
ret = behavior_keymap_binding_pressed(behavior, position, binding->param1, binding->param2);
return behavior_keymap_binding_pressed(behavior, position, binding->param1, binding->param2);
} else {
ret = behavior_keymap_binding_released(behavior, position, binding->param1, binding->param2);
return behavior_keymap_binding_released(behavior, position, binding->param1, binding->param2);
}
}
int zmk_keymap_position_state_changed(u32_t position, bool pressed)
{
for (int layer = ZMK_KEYMAP_LAYERS_LEN - 1; layer >= zmk_keymap_layer_default; layer--)
{
u32_t layer_state = pressed ? zmk_keymap_layer_state : zmk_keymap_active_behavior_layer[position];
if (is_active_layer(layer, layer_state))
{
int ret = zmk_keymap_apply_position_state(layer, position, pressed);
zmk_keymap_active_behavior_layer[position] = zmk_keymap_layer_state;
if (ret > 0) {
LOG_DBG("behavior processing to continue to next layer");
continue;
} else if (ret < 0) {
LOG_DBG("Behavior returned error: %d", ret);
zmk_keymap_active_behavior_layer[position] = 0;
return ret;
} else {
zmk_keymap_active_behavior_layer[position] = pressed ? layer : 0;
return ret;
}
}

Loading…
Cancel
Save