From 08c556b78b9dc672a2aa2388bbd3fdf408e3ce98 Mon Sep 17 00:00:00 2001 From: Nick Brassel Date: Sun, 5 Jun 2022 10:26:02 +1000 Subject: [PATCH] Add keymap wrappers for introspection into the keymap. (#17229) * Introspection handlers for keymaps. * Renaming. --- builddefs/build_keyboard.mk | 4 +++- quantum/keymap.h | 2 ++ quantum/keymap_introspection.c | 25 +++++++++++++++++++++++++ quantum/keymap_introspection.h | 15 +++++++++++++++ 4 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 quantum/keymap_introspection.c create mode 100644 quantum/keymap_introspection.h diff --git a/builddefs/build_keyboard.mk b/builddefs/build_keyboard.mk index dc86b232df..a258f33216 100644 --- a/builddefs/build_keyboard.mk +++ b/builddefs/build_keyboard.mk @@ -390,10 +390,12 @@ ifneq ("$(KEYMAP_H)","") CONFIG_H += $(KEYMAP_H) endif +OPT_DEFS += -DKEYMAP_C=\"$(KEYMAP_C)\" + # project specific files SRC += \ $(KEYBOARD_SRC) \ - $(KEYMAP_C) \ + $(QUANTUM_DIR)/keymap_introspection.c \ $(QUANTUM_SRC) \ $(QUANTUM_DIR)/main.c \ diff --git a/quantum/keymap.h b/quantum/keymap.h index d64b271efb..081bc54ebe 100644 --- a/quantum/keymap.h +++ b/quantum/keymap.h @@ -55,3 +55,5 @@ extern const uint16_t keymaps[][MATRIX_ROWS][MATRIX_COLS]; // Ensure we have a forward declaration for the encoder map # include "encoder.h" #endif + +#include "keymap_introspection.h" diff --git a/quantum/keymap_introspection.c b/quantum/keymap_introspection.c new file mode 100644 index 0000000000..9628b41eef --- /dev/null +++ b/quantum/keymap_introspection.c @@ -0,0 +1,25 @@ +// Copyright 2022 Nick Brassel (@tzarc) +// SPDX-License-Identifier: GPL-2.0-or-later + +// Pull the actual keymap code so that we can inspect stuff from it +#include KEYMAP_C + +#include "keymap_introspection.h" + +#define NUM_KEYMAP_LAYERS ((uint8_t)(sizeof(keymaps) / ((MATRIX_ROWS) * (MATRIX_COLS) * sizeof(uint16_t)))) + +uint8_t keymap_layer_count(void) { + return NUM_KEYMAP_LAYERS; +} + +#if defined(ENCODER_ENABLE) && defined(ENCODER_MAP_ENABLE) + +# define NUM_ENCODERMAP_LAYERS ((uint8_t)(sizeof(encoder_map) / ((NUM_ENCODERS) * (2) * sizeof(uint16_t)))) + +uint8_t encodermap_layer_count(void) { + return NUM_ENCODERMAP_LAYERS; +} + +_Static_assert(NUM_KEYMAP_LAYERS == NUM_ENCODERMAP_LAYERS, "Number of encoder_map layers doesn't match the number of keymap layers"); + +#endif // defined(ENCODER_ENABLE) && defined(ENCODER_MAP_ENABLE) diff --git a/quantum/keymap_introspection.h b/quantum/keymap_introspection.h new file mode 100644 index 0000000000..23f6f2016f --- /dev/null +++ b/quantum/keymap_introspection.h @@ -0,0 +1,15 @@ +// Copyright 2022 Nick Brassel (@tzarc) +// SPDX-License-Identifier: GPL-2.0-or-later +#pragma once + +#include + +// Get the number of layers defined in the keymap +uint8_t keymap_layer_count(void); + +#if defined(ENCODER_ENABLE) && defined(ENCODER_MAP_ENABLE) + +// Get the number of layers defined in the encoder map +uint8_t encodermap_layer_count(void); + +#endif // defined(ENCODER_ENABLE) && defined(ENCODER_MAP_ENABLE)