From ea05045923630add09551345359d2238176aa7b2 Mon Sep 17 00:00:00 2001
From: Nick Brassel <nick@tzarc.org>
Date: Tue, 24 Jan 2023 07:10:03 +1100
Subject: [PATCH] Allow overriding of keymap/encodermap layer count. (#19325)

---
 quantum/keymap_introspection.c | 40 +++++++++++++++++++++++-----------
 quantum/keymap_introspection.h | 14 ++++++++++--
 2 files changed, 39 insertions(+), 15 deletions(-)

diff --git a/quantum/keymap_introspection.c b/quantum/keymap_introspection.c
index 1529ab9fe2..2459ad0df5 100644
--- a/quantum/keymap_introspection.c
+++ b/quantum/keymap_introspection.c
@@ -11,44 +11,58 @@
 
 #include "keymap_introspection.h"
 
-#define NUM_KEYMAP_LAYERS ((uint8_t)(sizeof(keymaps) / ((MATRIX_ROWS) * (MATRIX_COLS) * sizeof(uint16_t))))
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+// Key mapping
 
-uint8_t keymap_layer_count(void) {
-    return NUM_KEYMAP_LAYERS;
+#define NUM_KEYMAP_LAYERS_RAW ((uint8_t)(sizeof(keymaps) / ((MATRIX_ROWS) * (MATRIX_COLS) * sizeof(uint16_t))))
+
+uint8_t keymap_layer_count_raw(void) {
+    return NUM_KEYMAP_LAYERS_RAW;
+}
+
+__attribute__((weak)) uint8_t keymap_layer_count(void) {
+    return keymap_layer_count_raw();
 }
 
 #ifdef DYNAMIC_KEYMAP_ENABLE
-_Static_assert(NUM_KEYMAP_LAYERS <= MAX_LAYER, "Number of keymap layers exceeds maximum set by DYNAMIC_KEYMAP_LAYER_COUNT");
+_Static_assert(NUM_KEYMAP_LAYERS_RAW <= MAX_LAYER, "Number of keymap layers exceeds maximum set by DYNAMIC_KEYMAP_LAYER_COUNT");
 #else
-_Static_assert(NUM_KEYMAP_LAYERS <= MAX_LAYER, "Number of keymap layers exceeds maximum set by LAYER_STATE_(8|16|32)BIT");
+_Static_assert(NUM_KEYMAP_LAYERS_RAW <= MAX_LAYER, "Number of keymap layers exceeds maximum set by LAYER_STATE_(8|16|32)BIT");
 #endif
 
 uint16_t keycode_at_keymap_location_raw(uint8_t layer_num, uint8_t row, uint8_t column) {
-    if (layer_num < NUM_KEYMAP_LAYERS && row < MATRIX_ROWS && column < MATRIX_COLS) {
+    if (layer_num < NUM_KEYMAP_LAYERS_RAW && row < MATRIX_ROWS && column < MATRIX_COLS) {
         return pgm_read_word(&keymaps[layer_num][row][column]);
     }
-    return KC_NO;
+    return KC_TRNS;
 }
 
 __attribute__((weak)) uint16_t keycode_at_keymap_location(uint8_t layer_num, uint8_t row, uint8_t column) {
     return keycode_at_keymap_location_raw(layer_num, row, column);
 }
 
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+// Encoder mapping
+
 #if defined(ENCODER_ENABLE) && defined(ENCODER_MAP_ENABLE)
 
-#    define NUM_ENCODERMAP_LAYERS ((uint8_t)(sizeof(encoder_map) / ((NUM_ENCODERS) * (2) * sizeof(uint16_t))))
+#    define NUM_ENCODERMAP_LAYERS_RAW ((uint8_t)(sizeof(encoder_map) / ((NUM_ENCODERS) * (2) * sizeof(uint16_t))))
 
-uint8_t encodermap_layer_count(void) {
-    return NUM_ENCODERMAP_LAYERS;
+uint8_t encodermap_layer_count_raw(void) {
+    return NUM_ENCODERMAP_LAYERS_RAW;
 }
 
-_Static_assert(NUM_KEYMAP_LAYERS == NUM_ENCODERMAP_LAYERS, "Number of encoder_map layers doesn't match the number of keymap layers");
+__attribute__((weak)) uint8_t encodermap_layer_count(void) {
+    return encodermap_layer_count_raw();
+}
+
+_Static_assert(NUM_KEYMAP_LAYERS_RAW == NUM_ENCODERMAP_LAYERS_RAW, "Number of encoder_map layers doesn't match the number of keymap layers");
 
 uint16_t keycode_at_encodermap_location_raw(uint8_t layer_num, uint8_t encoder_idx, bool clockwise) {
-    if (layer_num < NUM_ENCODERMAP_LAYERS && encoder_idx < NUM_ENCODERS) {
+    if (layer_num < NUM_ENCODERMAP_LAYERS_RAW && encoder_idx < NUM_ENCODERS) {
         return pgm_read_word(&encoder_map[layer_num][encoder_idx][clockwise ? 0 : 1]);
     }
-    return KC_NO;
+    return KC_TRNS;
 }
 
 __attribute__((weak)) uint16_t keycode_at_encodermap_location(uint8_t layer_num, uint8_t encoder_idx, bool clockwise) {
diff --git a/quantum/keymap_introspection.h b/quantum/keymap_introspection.h
index 9de706a021..a8df3928a6 100644
--- a/quantum/keymap_introspection.h
+++ b/quantum/keymap_introspection.h
@@ -4,7 +4,12 @@
 
 #include <stdint.h>
 
-// Get the number of layers defined in the keymap
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+// Key mapping
+
+// Get the number of layers defined in the keymap, stored in firmware rather than any other persistent storage
+uint8_t keymap_layer_count_raw(void);
+// Get the number of layers defined in the keymap, potentially stored dynamically
 uint8_t keymap_layer_count(void);
 
 // Get the keycode for the keymap location, stored in firmware rather than any other persistent storage
@@ -12,9 +17,14 @@ uint16_t keycode_at_keymap_location_raw(uint8_t layer_num, uint8_t row, uint8_t
 // Get the keycode for the keymap location, potentially stored dynamically
 uint16_t keycode_at_keymap_location(uint8_t layer_num, uint8_t row, uint8_t column);
 
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+// Encoder mapping
+
 #if defined(ENCODER_ENABLE) && defined(ENCODER_MAP_ENABLE)
 
-// Get the number of layers defined in the encoder map
+// Get the number of layers defined in the encoder map, stored in firmware rather than any other persistent storage
+uint8_t encodermap_layer_count_raw(void);
+// Get the number of layers defined in the encoder map, potentially stored dynamically
 uint8_t encodermap_layer_count(void);
 
 // Get the keycode for the encoder mapping location, stored in firmware rather than any other persistent storage