Skip to content

Commit

Permalink
fix: Properly calculate highest active layer for display.
Browse files Browse the repository at this point in the history
  • Loading branch information
petejohanson committed Sep 19, 2024
1 parent 1baf18d commit aafd1f7
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 16 deletions.
9 changes: 5 additions & 4 deletions app/boards/arm/corneish_zen/widgets/layer_status.c
Original file line number Diff line number Diff line change
Expand Up @@ -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] = {};
Expand All @@ -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,
Expand Down
7 changes: 4 additions & 3 deletions app/boards/shields/nice_view/widgets/status.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ struct output_status_state {
};

struct layer_status_state {
uint8_t index;
zmk_keymap_layer_index_t index;
const char *label;
};

Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion app/include/zmk/keymap.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 4 additions & 3 deletions app/src/display/widgets/layer_status.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand All @@ -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,
Expand Down
17 changes: 12 additions & 5 deletions app/src/keymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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); };
Expand Down

0 comments on commit aafd1f7

Please sign in to comment.