Invoke called behavior after layer change.

* If you press a key with a layer active, then
  deactivate the layer (e.g. releasing a `&mo`,
  then release the key, we currently may send
  the wrong key release event.
* Fixes #67.
This commit is contained in:
Pete Johanson 2020-08-09 12:13:39 -04:00
parent e2848c66c3
commit 3127192720

View file

@ -34,9 +34,6 @@ static u8_t zmk_keymap_layer_default = 0;
#define TRANSFORMED_LAYER(node) \
{ UTIL_LISTIFY(DT_PROP_LEN(node, bindings), _TRANSFORM_ENTRY, node) },
static struct zmk_behavior_binding zmk_keymap[ZMK_KEYMAP_LAYERS_LEN][ZMK_KEYMAP_LEN] = {
DT_INST_FOREACH_CHILD(0, TRANSFORMED_LAYER)
};
#if ZMK_KEYMAP_HAS_SENSORS
#define _TRANSFORM_SENSOR_ENTRY(idx, layer) \
@ -50,6 +47,21 @@ static struct zmk_behavior_binding zmk_keymap[ZMK_KEYMAP_LAYERS_LEN][ZMK_KEYMAP_
({ UTIL_LISTIFY(DT_PROP_LEN(node, sensor_bindings), _TRANSFORM_SENSOR_ENTRY, node) }), \
({})),
#endif /* ZMK_KEYMAP_HAS_SENSORS */
// State
// When a behavior handles a key position "down" event, we record that layer
// 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 struct zmk_behavior_binding zmk_keymap[ZMK_KEYMAP_LAYERS_LEN][ZMK_KEYMAP_LEN] = {
DT_INST_FOREACH_CHILD(0, TRANSFORMED_LAYER)
};
#if ZMK_KEYMAP_HAS_SENSORS
static struct zmk_behavior_binding zmk_sensor_keymap[ZMK_KEYMAP_LAYERS_LEN][ZMK_KEYMAP_SENSORS_LEN] = {
DT_INST_FOREACH_CHILD(0, SENSOR_LAYER)
};
@ -74,11 +86,18 @@ int zmk_keymap_layer_deactivate(u8_t layer)
SET_LAYER_STATE(layer, false);
};
bool is_active_position(u32_t position, u8_t layer)
{
return (zmk_keymap_layer_state & BIT(layer)) == BIT(layer)
|| layer == zmk_keymap_layer_default
|| zmk_keymap_active_behavior_layer[position] == layer;
}
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 ((zmk_keymap_layer_state & BIT(layer)) == BIT(layer) || layer == zmk_keymap_layer_default)
if (is_active_position(position, layer))
{
struct zmk_behavior_binding *binding = &zmk_keymap[layer][position];
struct device *behavior;
@ -104,8 +123,10 @@ int zmk_keymap_position_state_changed(u32_t position, bool pressed)
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;
}
}