From 2427a4ab3308523200f9419316a10a75f237bb6c Mon Sep 17 00:00:00 2001 From: KemoNine Date: Wed, 9 Dec 2020 09:35:57 -0500 Subject: [PATCH] feature(keymaps): Keymap layer state event, highest layer API * Fire an event w/ layer number, state, and timestamp when a layer is activated/deactivated. * Add `zmk_keymap_highest_layer_active()` query. Co-authored-by: KemoNine --- app/CMakeLists.txt | 1 + app/include/zmk/events/layer-state-changed.h | 28 +++++++++++ app/include/zmk/keymap.h | 5 ++ app/src/events/layer_state_changed.c | 10 ++++ app/src/keymap.c | 50 +++++++++++++------- 5 files changed, 77 insertions(+), 17 deletions(-) create mode 100644 app/include/zmk/events/layer-state-changed.h create mode 100644 app/src/events/layer_state_changed.c diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index 8e88e08d..7f374f79 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -30,6 +30,7 @@ target_sources(app PRIVATE src/sensors.c) target_sources(app PRIVATE src/event_manager.c) target_sources_ifdef(CONFIG_ZMK_EXT_POWER app PRIVATE src/ext_power_generic.c) target_sources(app PRIVATE src/events/position_state_changed.c) +target_sources(app PRIVATE src/events/layer_state_changed.c) target_sources(app PRIVATE src/events/keycode_state_changed.c) target_sources(app PRIVATE src/events/modifiers_state_changed.c) target_sources(app PRIVATE src/events/sensor_event.c) diff --git a/app/include/zmk/events/layer-state-changed.h b/app/include/zmk/events/layer-state-changed.h new file mode 100644 index 00000000..dc13ed63 --- /dev/null +++ b/app/include/zmk/events/layer-state-changed.h @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#pragma once + +#include +#include + +struct layer_state_changed { + struct zmk_event_header header; + u8_t layer; + bool state; + s64_t timestamp; +}; + +ZMK_EVENT_DECLARE(layer_state_changed); + +static inline struct layer_state_changed *create_layer_state_changed(u8_t layer, bool state) { + struct layer_state_changed *ev = new_layer_state_changed(); + ev->layer = layer; + ev->state = state; + ev->timestamp = k_uptime_get(); + + return ev; +} diff --git a/app/include/zmk/keymap.h b/app/include/zmk/keymap.h index b8f49694..88dc933d 100644 --- a/app/include/zmk/keymap.h +++ b/app/include/zmk/keymap.h @@ -6,7 +6,12 @@ #pragma once +typedef u32_t zmk_keymap_layers_state; + +u8_t zmk_keymap_layer_default(); +zmk_keymap_layers_state zmk_keymap_layer_state(); bool zmk_keymap_layer_active(u8_t layer); +u8_t zmk_keymap_highest_layer_active(); int zmk_keymap_layer_activate(u8_t layer); int zmk_keymap_layer_deactivate(u8_t layer); int zmk_keymap_layer_toggle(u8_t layer); diff --git a/app/src/events/layer_state_changed.c b/app/src/events/layer_state_changed.c new file mode 100644 index 00000000..e4567115 --- /dev/null +++ b/app/src/events/layer_state_changed.c @@ -0,0 +1,10 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#include +#include + +ZMK_EVENT_IMPL(layer_state_changed); \ No newline at end of file diff --git a/app/src/keymap.c b/app/src/keymap.c index 4174ac1d..08115a1c 100644 --- a/app/src/keymap.c +++ b/app/src/keymap.c @@ -16,10 +16,11 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); #include #include +#include #include -static u32_t zmk_keymap_layer_state = 0; -static u8_t zmk_keymap_layer_default = 0; +static zmk_keymap_layers_state _zmk_keymap_layer_state = 0; +static u8_t _zmk_keymap_layer_default = 0; #define DT_DRV_COMPAT zmk_keymap @@ -76,20 +77,35 @@ static struct zmk_behavior_binding zmk_sensor_keymap[ZMK_KEYMAP_LAYERS_LEN] #endif /* ZMK_KEYMAP_HAS_SENSORS */ -#define SET_LAYER_STATE(layer, state) \ - if (layer >= 32) { \ - return -EINVAL; \ - } \ - WRITE_BIT(zmk_keymap_layer_state, layer, state); \ +static inline int set_layer_state(u8_t layer, bool state) { + if (layer >= 32) { + return -EINVAL; + } + WRITE_BIT(_zmk_keymap_layer_state, layer, state); + ZMK_EVENT_RAISE(create_layer_state_changed(layer, state)); return 0; +} + +u8_t zmk_keymap_layer_default() { return _zmk_keymap_layer_default; } + +zmk_keymap_layers_state zmk_keymap_layer_state() { return _zmk_keymap_layer_state; } bool zmk_keymap_layer_active(u8_t layer) { - return (zmk_keymap_layer_state & (BIT(layer))) == (BIT(layer)); + return (_zmk_keymap_layer_state & (BIT(layer))) == (BIT(layer)); }; -int zmk_keymap_layer_activate(u8_t layer) { SET_LAYER_STATE(layer, true); }; +u8_t zmk_keymap_highest_layer_active() { + for (u8_t layer = 31; layer > 0; layer--) { + if (zmk_keymap_layer_active(layer)) { + return layer; + } + } + return zmk_keymap_layer_default(); +} + +int zmk_keymap_layer_activate(u8_t layer) { return set_layer_state(layer, true); }; -int zmk_keymap_layer_deactivate(u8_t layer) { SET_LAYER_STATE(layer, false); }; +int zmk_keymap_layer_deactivate(u8_t layer) { return set_layer_state(layer, false); }; int zmk_keymap_layer_toggle(u8_t layer) { if (zmk_keymap_layer_active(layer)) { @@ -99,8 +115,8 @@ int zmk_keymap_layer_toggle(u8_t layer) { return zmk_keymap_layer_activate(layer); }; -bool is_active_layer(u8_t layer, u32_t layer_state) { - return (layer_state & BIT(layer)) == BIT(layer) || layer == zmk_keymap_layer_default; +bool is_active_layer(u8_t layer, zmk_keymap_layers_state layer_state) { + return (layer_state & BIT(layer)) == BIT(layer) || layer == _zmk_keymap_layer_default; } int zmk_keymap_apply_position_state(int layer, u32_t position, bool pressed, s64_t timestamp) { @@ -131,9 +147,9 @@ int zmk_keymap_apply_position_state(int layer, u32_t position, bool pressed, s64 int zmk_keymap_position_state_changed(u32_t position, bool pressed, s64_t timestamp) { if (pressed) { - zmk_keymap_active_behavior_layer[position] = zmk_keymap_layer_state; + zmk_keymap_active_behavior_layer[position] = _zmk_keymap_layer_state; } - for (int layer = ZMK_KEYMAP_LAYERS_LEN - 1; layer >= zmk_keymap_layer_default; layer--) { + for (int layer = ZMK_KEYMAP_LAYERS_LEN - 1; layer >= _zmk_keymap_layer_default; layer--) { if (is_active_layer(layer, zmk_keymap_active_behavior_layer[position])) { int ret = zmk_keymap_apply_position_state(layer, position, pressed, timestamp); if (ret > 0) { @@ -153,9 +169,9 @@ int zmk_keymap_position_state_changed(u32_t position, bool pressed, s64_t timest #if ZMK_KEYMAP_HAS_SENSORS int zmk_keymap_sensor_triggered(u8_t sensor_number, struct device *sensor, s64_t timestamp) { - 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) && + 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) && zmk_sensor_keymap[layer] != NULL) { struct zmk_behavior_binding *binding = &zmk_sensor_keymap[layer][sensor_number]; struct device *behavior;