summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPete Johanson <peter@peterjohanson.com>2020-08-09 12:13:39 -0400
committerPete Johanson <peter@peterjohanson.com>2020-08-09 12:30:47 -0400
commit3127192720c9da848e14625c34ce3f6329eb0a0f (patch)
tree3eaf771e25e624519b8b37e6e587336743461ad8
parente2848c66c305442badd2b21a414c1a8fe89cd4fd (diff)
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.
-rw-r--r--app/src/keymap.c29
1 files changed, 25 insertions, 4 deletions
diff --git a/app/src/keymap.c b/app/src/keymap.c
index aee0577..ff494f7 100644
--- a/app/src/keymap.c
+++ b/app/src/keymap.c
@@ -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;
}
}