Browse Source

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 <mcrosson@kemonine.info>
xmkb
KemoNine 4 years ago committed by GitHub
parent
commit
2427a4ab33
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      app/CMakeLists.txt
  2. 28
      app/include/zmk/events/layer-state-changed.h
  3. 5
      app/include/zmk/keymap.h
  4. 10
      app/src/events/layer_state_changed.c
  5. 50
      app/src/keymap.c

1
app/CMakeLists.txt

@ -30,6 +30,7 @@ target_sources(app PRIVATE src/sensors.c)
target_sources(app PRIVATE src/event_manager.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_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/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/keycode_state_changed.c)
target_sources(app PRIVATE src/events/modifiers_state_changed.c) target_sources(app PRIVATE src/events/modifiers_state_changed.c)
target_sources(app PRIVATE src/events/sensor_event.c) target_sources(app PRIVATE src/events/sensor_event.c)

28
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 <zephyr.h>
#include <zmk/event-manager.h>
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;
}

5
app/include/zmk/keymap.h

@ -6,7 +6,12 @@
#pragma once #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); 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_activate(u8_t layer);
int zmk_keymap_layer_deactivate(u8_t layer); int zmk_keymap_layer_deactivate(u8_t layer);
int zmk_keymap_layer_toggle(u8_t layer); int zmk_keymap_layer_toggle(u8_t layer);

10
app/src/events/layer_state_changed.c

@ -0,0 +1,10 @@
/*
* Copyright (c) 2020 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/
#include <kernel.h>
#include <zmk/events/layer-state-changed.h>
ZMK_EVENT_IMPL(layer_state_changed);

50
app/src/keymap.c

@ -16,10 +16,11 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#include <zmk/event-manager.h> #include <zmk/event-manager.h>
#include <zmk/events/position-state-changed.h> #include <zmk/events/position-state-changed.h>
#include <zmk/events/layer-state-changed.h>
#include <zmk/events/sensor-event.h> #include <zmk/events/sensor-event.h>
static u32_t zmk_keymap_layer_state = 0; static zmk_keymap_layers_state _zmk_keymap_layer_state = 0;
static u8_t zmk_keymap_layer_default = 0; static u8_t _zmk_keymap_layer_default = 0;
#define DT_DRV_COMPAT zmk_keymap #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 */ #endif /* ZMK_KEYMAP_HAS_SENSORS */
#define SET_LAYER_STATE(layer, state) \ static inline int set_layer_state(u8_t layer, bool state) {
if (layer >= 32) { \ if (layer >= 32) {
return -EINVAL; \ return -EINVAL;
} \ }
WRITE_BIT(zmk_keymap_layer_state, layer, state); \ WRITE_BIT(_zmk_keymap_layer_state, layer, state);
ZMK_EVENT_RAISE(create_layer_state_changed(layer, state));
return 0; 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) { 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) { int zmk_keymap_layer_toggle(u8_t layer) {
if (zmk_keymap_layer_active(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); return zmk_keymap_layer_activate(layer);
}; };
bool is_active_layer(u8_t layer, u32_t layer_state) { 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; 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) { 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) { int zmk_keymap_position_state_changed(u32_t position, bool pressed, s64_t timestamp) {
if (pressed) { 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])) { if (is_active_layer(layer, zmk_keymap_active_behavior_layer[position])) {
int ret = zmk_keymap_apply_position_state(layer, position, pressed, timestamp); int ret = zmk_keymap_apply_position_state(layer, position, pressed, timestamp);
if (ret > 0) { 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 #if ZMK_KEYMAP_HAS_SENSORS
int zmk_keymap_sensor_triggered(u8_t sensor_number, struct device *sensor, s64_t timestamp) { 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--) { for (int layer = ZMK_KEYMAP_LAYERS_LEN - 1; layer >= _zmk_keymap_layer_default; layer--) {
if (((zmk_keymap_layer_state & BIT(layer)) == BIT(layer) || if (((_zmk_keymap_layer_state & BIT(layer)) == BIT(layer) ||
layer == zmk_keymap_layer_default) && layer == _zmk_keymap_layer_default) &&
zmk_sensor_keymap[layer] != NULL) { zmk_sensor_keymap[layer] != NULL) {
struct zmk_behavior_binding *binding = &zmk_sensor_keymap[layer][sensor_number]; struct zmk_behavior_binding *binding = &zmk_sensor_keymap[layer][sensor_number];
struct device *behavior; struct device *behavior;

Loading…
Cancel
Save