Skip to content

Commit

Permalink
refactor(keymap): Reduce flash usage for studio builds (#2771)
Browse files Browse the repository at this point in the history
When building for ZMK Studio, we can use the constant stock keymap
for init of the in-memory keymap, to avoid duplicate flash usage
for the keymap from the devicetree.
  • Loading branch information
petejohanson authored Jan 15, 2025
1 parent c367d8f commit e4f1454
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
5 changes: 4 additions & 1 deletion app/include/zmk/keymap.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@

#define ZMK_LAYER_CHILD_LEN_PLUS_ONE(node) 1 +
#define ZMK_KEYMAP_LAYERS_LEN \
(DT_FOREACH_CHILD(DT_INST(0, zmk_keymap), ZMK_LAYER_CHILD_LEN_PLUS_ONE) 0)
(COND_CODE_1( \
IS_ENABLED(CONFIG_ZMK_STUDIO), \
(DT_FOREACH_CHILD(DT_INST(0, zmk_keymap), ZMK_LAYER_CHILD_LEN_PLUS_ONE)), \
(DT_FOREACH_CHILD_STATUS_OKAY(DT_INST(0, zmk_keymap), ZMK_LAYER_CHILD_LEN_PLUS_ONE))) 0)

/**
* @brief A layer ID is a stable identifier to refer to a layer, regardless of ordering.
Expand Down
19 changes: 13 additions & 6 deletions app/src/keymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,21 @@ static uint8_t keymap_layer_orders[ZMK_KEYMAP_LAYERS_LEN];

#endif // IS_ENABLED(CONFIG_ZMK_KEYMAP_LAYER_REORDERING)

#define KEYMAP_VAR(_name, _opts) \
#define KEYMAP_VAR(_name, _opts, no_init) \
static _opts struct zmk_behavior_binding _name[ZMK_KEYMAP_LAYERS_LEN][ZMK_KEYMAP_LEN] = { \
COND_CODE_1(IS_ENABLED(CONFIG_ZMK_STUDIO), \
(DT_INST_FOREACH_CHILD_SEP(0, TRANSFORMED_LAYER, (, ))), \
(DT_INST_FOREACH_CHILD_STATUS_OKAY_SEP(0, TRANSFORMED_LAYER, (, ))))};
COND_CODE_0( \
no_init, \
(COND_CODE_1(IS_ENABLED(CONFIG_ZMK_STUDIO), \
(DT_INST_FOREACH_CHILD_SEP(0, TRANSFORMED_LAYER, (, ))), \
(DT_INST_FOREACH_CHILD_STATUS_OKAY_SEP(0, TRANSFORMED_LAYER, (, ))))), \
(0))};

KEYMAP_VAR(zmk_keymap, COND_CODE_1(IS_ENABLED(CONFIG_ZMK_KEYMAP_SETTINGS_STORAGE), (), (const)))
KEYMAP_VAR(zmk_keymap, COND_CODE_1(IS_ENABLED(CONFIG_ZMK_KEYMAP_SETTINGS_STORAGE), (), (const)),
IS_ENABLED(CONFIG_ZMK_STUDIO))

#if IS_ENABLED(CONFIG_ZMK_KEYMAP_SETTINGS_STORAGE)

KEYMAP_VAR(zmk_stock_keymap, const)
KEYMAP_VAR(zmk_stock_keymap, const, 0)

static char zmk_keymap_layer_names[ZMK_KEYMAP_LAYERS_LEN][CONFIG_ZMK_KEYMAP_LAYER_NAME_MAX_LEN] = {
DT_INST_FOREACH_CHILD_SEP(0, LAYER_NAME, (, ))};
Expand Down Expand Up @@ -955,6 +959,9 @@ int keymap_init(void) {
#if IS_ENABLED(CONFIG_ZMK_KEYMAP_LAYER_REORDERING)
load_stock_keymap_layer_ordering();
#endif
#if IS_ENABLED(CONFIG_ZMK_STUDIO)
reload_from_stock_keymap();
#endif

return 0;
}
Expand Down

0 comments on commit e4f1454

Please sign in to comment.