From cca637d66e36a85706d1807d44a5fecbc66bc5f7 Mon Sep 17 00:00:00 2001 From: Peter Johanson Date: Thu, 19 Sep 2024 01:34:20 -0600 Subject: [PATCH] fix: Properly calculate highest active layer for display. --- .../arm/corneish_zen/widgets/layer_status.c | 9 +++++---- app/boards/shields/nice_view/widgets/status.c | 7 ++++--- app/include/zmk/keymap.h | 2 +- app/src/display/widgets/layer_status.c | 7 ++++--- app/src/keymap.c | 17 ++++++++++++----- 5 files changed, 26 insertions(+), 16 deletions(-) diff --git a/app/boards/arm/corneish_zen/widgets/layer_status.c b/app/boards/arm/corneish_zen/widgets/layer_status.c index 86418318092..82de72c99fd 100644 --- a/app/boards/arm/corneish_zen/widgets/layer_status.c +++ b/app/boards/arm/corneish_zen/widgets/layer_status.c @@ -19,13 +19,13 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); static sys_slist_t widgets = SYS_SLIST_STATIC_INIT(&widgets); struct layer_status_state { - uint8_t index; + zmk_keymap_layer_index_t index; const char *label; }; static void set_layer_symbol(lv_obj_t *label, struct layer_status_state state) { const char *layer_label = state.label; - uint8_t active_layer_index = state.index; + zmk_keymap_layer_index_t active_layer_index = state.index; if (layer_label == NULL) { char text[6] = {}; @@ -44,8 +44,9 @@ static void layer_status_update_cb(struct layer_status_state state) { } static struct layer_status_state layer_status_get_state(const zmk_event_t *eh) { - uint8_t index = zmk_keymap_highest_layer_active(); - return (struct layer_status_state){.index = index, .label = zmk_keymap_layer_name(index)}; + zmk_keymap_layer_index_t index = zmk_keymap_highest_layer_active(); + return (struct layer_status_state){ + .index = index, .label = zmk_keymap_layer_name(zmk_keymap_layer_index_to_id(index))}; } ZMK_DISPLAY_WIDGET_LISTENER(widget_layer_status, struct layer_status_state, layer_status_update_cb, diff --git a/app/boards/shields/nice_view/widgets/status.c b/app/boards/shields/nice_view/widgets/status.c index f5095cbf1d8..fa0223551c4 100644 --- a/app/boards/shields/nice_view/widgets/status.c +++ b/app/boards/shields/nice_view/widgets/status.c @@ -36,7 +36,7 @@ struct output_status_state { }; struct layer_status_state { - uint8_t index; + zmk_keymap_layer_index_t index; const char *label; }; @@ -277,8 +277,9 @@ static void layer_status_update_cb(struct layer_status_state state) { } static struct layer_status_state layer_status_get_state(const zmk_event_t *eh) { - uint8_t index = zmk_keymap_highest_layer_active(); - return (struct layer_status_state){.index = index, .label = zmk_keymap_layer_name(index)}; + zmk_keymap_layer_index_t index = zmk_keymap_highest_layer_active(); + return (struct layer_status_state){ + .index = index, .label = zmk_keymap_layer_name(zmk_keymap_layer_index_to_id(index))}; } ZMK_DISPLAY_WIDGET_LISTENER(widget_layer_status, struct layer_status_state, layer_status_update_cb, diff --git a/app/include/zmk/keymap.h b/app/include/zmk/keymap.h index 50b358ba92a..9b278a6050f 100644 --- a/app/include/zmk/keymap.h +++ b/app/include/zmk/keymap.h @@ -34,7 +34,7 @@ zmk_keymap_layer_id_t zmk_keymap_layer_index_to_id(zmk_keymap_layer_index_t laye zmk_keymap_layer_id_t zmk_keymap_layer_default(void); zmk_keymap_layers_state_t zmk_keymap_layer_state(void); bool zmk_keymap_layer_active(zmk_keymap_layer_id_t layer); -zmk_keymap_layer_id_t zmk_keymap_highest_layer_active(void); +zmk_keymap_layer_index_t zmk_keymap_highest_layer_active(void); int zmk_keymap_layer_activate(zmk_keymap_layer_id_t layer); int zmk_keymap_layer_deactivate(zmk_keymap_layer_id_t layer); int zmk_keymap_layer_toggle(zmk_keymap_layer_id_t layer); diff --git a/app/src/display/widgets/layer_status.c b/app/src/display/widgets/layer_status.c index 19e25d93312..d341ccd3223 100644 --- a/app/src/display/widgets/layer_status.c +++ b/app/src/display/widgets/layer_status.c @@ -18,7 +18,7 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); static sys_slist_t widgets = SYS_SLIST_STATIC_INIT(&widgets); struct layer_status_state { - uint8_t index; + zmk_keymap_layer_index_t index; const char *label; }; @@ -44,8 +44,9 @@ static void layer_status_update_cb(struct layer_status_state state) { } static struct layer_status_state layer_status_get_state(const zmk_event_t *eh) { - uint8_t index = zmk_keymap_highest_layer_active(); - return (struct layer_status_state){.index = index, .label = zmk_keymap_layer_name(index)}; + zmk_keymap_layer_index_t index = zmk_keymap_highest_layer_active(); + return (struct layer_status_state){ + .index = index, .label = zmk_keymap_layer_name(zmk_keymap_layer_index_to_id(index))}; } ZMK_DISPLAY_WIDGET_LISTENER(widget_layer_status, struct layer_status_state, layer_status_update_cb, diff --git a/app/src/keymap.c b/app/src/keymap.c index 41e57024b77..e49acb579c5 100644 --- a/app/src/keymap.c +++ b/app/src/keymap.c @@ -182,13 +182,20 @@ bool zmk_keymap_layer_active(zmk_keymap_layer_id_t layer) { return zmk_keymap_layer_active_with_state(layer, _zmk_keymap_layer_state); }; -zmk_keymap_layer_id_t zmk_keymap_highest_layer_active(void) { - for (uint8_t layer = ZMK_KEYMAP_LAYERS_LEN - 1; layer > 0; layer--) { - if (zmk_keymap_layer_active(layer)) { - return layer; +zmk_keymap_layer_index_t zmk_keymap_highest_layer_active(void) { + for (int layer_idx = ZMK_KEYMAP_LAYERS_LEN - 1; + layer_idx >= LAYER_ID_TO_INDEX(_zmk_keymap_layer_default); layer_idx--) { + zmk_keymap_layer_id_t layer_id = LAYER_INDEX_TO_ID(layer_idx); + + if (layer_id == ZMK_KEYMAP_LAYER_ID_INVAL) { + continue; + } + if (zmk_keymap_layer_active(layer_id)) { + return LAYER_ID_TO_INDEX(layer_id); } } - return zmk_keymap_layer_default(); + + return LAYER_ID_TO_INDEX(zmk_keymap_layer_default()); } int zmk_keymap_layer_activate(zmk_keymap_layer_id_t layer) { return set_layer_state(layer, true); };