Skip to content

Commit

Permalink
feat(keymap): API for retrieving label for a layer
Browse files Browse the repository at this point in the history
  • Loading branch information
petejohanson committed Jan 4, 2021
1 parent 74b397a commit a55b139
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/include/zmk/keymap.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ int zmk_keymap_layer_activate(uint8_t layer);
int zmk_keymap_layer_deactivate(uint8_t layer);
int zmk_keymap_layer_toggle(uint8_t layer);
int zmk_keymap_layer_to(uint8_t layer);
const char *zmk_keymap_layer_label(uint8_t layer);

int zmk_keymap_position_state_changed(uint32_t position, bool pressed, int64_t timestamp);
17 changes: 17 additions & 0 deletions app/src/keymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ static uint8_t _zmk_keymap_layer_default = 0;

#endif /* ZMK_KEYMAP_HAS_SENSORS */

#define LAYER_LABEL(node) COND_CODE_0(DT_NODE_HAS_PROP(node, label), (NULL), (DT_LABEL(node))),

// State

// When a behavior handles a key position "down" event, we record the layer state
Expand All @@ -69,6 +71,9 @@ static uint32_t zmk_keymap_active_behavior_layer[ZMK_KEYMAP_LEN];
static struct zmk_behavior_binding zmk_keymap[ZMK_KEYMAP_LAYERS_LEN][ZMK_KEYMAP_LEN] = {
DT_INST_FOREACH_CHILD(0, TRANSFORMED_LAYER)};

static const char *zmk_keymap_layer_names[ZMK_KEYMAP_LAYERS_LEN] = {
DT_INST_FOREACH_CHILD(0, LAYER_LABEL)};

#if ZMK_KEYMAP_HAS_SENSORS

static struct zmk_behavior_binding zmk_sensor_keymap[ZMK_KEYMAP_LAYERS_LEN]
Expand Down Expand Up @@ -143,6 +148,18 @@ int zmk_keymap_layer_to(uint8_t layer) {
return 0;
}

bool is_active_layer(uint8_t layer, zmk_keymap_layers_state_t layer_state) {
return (layer_state & BIT(layer)) == BIT(layer) || layer == _zmk_keymap_layer_default;
}

const char *zmk_keymap_layer_label(uint8_t layer) {
if (layer >= ZMK_KEYMAP_LAYERS_LEN) {
return NULL;
}

return zmk_keymap_layer_names[layer];
}

int zmk_keymap_apply_position_state(int layer, uint32_t position, bool pressed, int64_t timestamp) {
struct zmk_behavior_binding *binding = &zmk_keymap[layer][position];
const struct device *behavior;
Expand Down

0 comments on commit a55b139

Please sign in to comment.