From cbbb45c13f3de5ec28f62b6cd4a8b77143026500 Mon Sep 17 00:00:00 2001 From: Nick Brassel Date: Fri, 30 Sep 2022 03:25:55 +1000 Subject: Start moving towards introspection-based data retrieval (#18441) --- quantum/dynamic_keymap.c | 25 +++++++++++++------------ quantum/keyboard.c | 5 ++--- quantum/keymap.h | 2 -- quantum/keymap_common.c | 6 +++--- quantum/keymap_introspection.c | 22 ++++++++++++++++++++++ quantum/keymap_introspection.h | 10 ++++++++++ 6 files changed, 50 insertions(+), 20 deletions(-) (limited to 'quantum') diff --git a/quantum/dynamic_keymap.c b/quantum/dynamic_keymap.c index 01be9806e4..e9529ed14e 100644 --- a/quantum/dynamic_keymap.c +++ b/quantum/dynamic_keymap.c @@ -153,7 +153,7 @@ void dynamic_keymap_reset(void) { for (int row = 0; row < MATRIX_ROWS; row++) { for (int column = 0; column < MATRIX_COLS; column++) { if (layer < keymap_layer_count()) { - dynamic_keymap_set_keycode(layer, row, column, pgm_read_word(&keymaps[layer][row][column])); + dynamic_keymap_set_keycode(layer, row, column, keycode_at_keymap_location_raw(layer, row, column)); } else { dynamic_keymap_set_keycode(layer, row, column, KC_TRANSPARENT); } @@ -162,8 +162,8 @@ void dynamic_keymap_reset(void) { #ifdef ENCODER_MAP_ENABLE for (int encoder = 0; encoder < NUM_ENCODERS; encoder++) { if (layer < encodermap_layer_count()) { - dynamic_keymap_set_encoder(layer, encoder, true, pgm_read_word(&encoder_map[layer][encoder][0])); - dynamic_keymap_set_encoder(layer, encoder, false, pgm_read_word(&encoder_map[layer][encoder][1])); + dynamic_keymap_set_encoder(layer, encoder, true, keycode_at_encodermap_location_raw(layer, encoder, true)); + dynamic_keymap_set_encoder(layer, encoder, false, keycode_at_encodermap_location_raw(layer, encoder, false)); } else { dynamic_keymap_set_encoder(layer, encoder, true, KC_TRANSPARENT); dynamic_keymap_set_encoder(layer, encoder, false, KC_TRANSPARENT); @@ -201,20 +201,21 @@ void dynamic_keymap_set_buffer(uint16_t offset, uint16_t size, uint8_t *data) { } } -// This overrides the one in quantum/keymap_common.c -uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key) { - if (layer < DYNAMIC_KEYMAP_LAYER_COUNT && key.row < MATRIX_ROWS && key.col < MATRIX_COLS) { - return dynamic_keymap_get_keycode(layer, key.row, key.col); +uint16_t keycode_at_keymap_location(uint8_t layer_num, uint8_t row, uint8_t column) { + if (layer_num < DYNAMIC_KEYMAP_LAYER_COUNT && row < MATRIX_ROWS && column < MATRIX_COLS) { + return dynamic_keymap_get_keycode(layer_num, row, column); } + return KC_NO; +} + #ifdef ENCODER_MAP_ENABLE - else if (layer < DYNAMIC_KEYMAP_LAYER_COUNT && key.row == KEYLOC_ENCODER_CW && key.col < NUM_ENCODERS) { - return dynamic_keymap_get_encoder(layer, key.col, true); - } else if (layer < DYNAMIC_KEYMAP_LAYER_COUNT && key.row == KEYLOC_ENCODER_CCW && key.col < NUM_ENCODERS) { - return dynamic_keymap_get_encoder(layer, key.col, false); +uint16_t keycode_at_encodermap_location(uint8_t layer_num, uint8_t encoder_idx, bool clockwise) { + if (layer_num < DYNAMIC_KEYMAP_LAYER_COUNT && encoder_idx < NUM_ENCODERS) { + return dynamic_keymap_get_encoder(layer_num, encoder_idx, clockwise); } -#endif // ENCODER_MAP_ENABLE return KC_NO; } +#endif // ENCODER_MAP_ENABLE uint8_t dynamic_keymap_macro_get_count(void) { return DYNAMIC_KEYMAP_MACRO_COUNT; diff --git a/quantum/keyboard.c b/quantum/keyboard.c index 3b5e9b0200..c8025d059b 100644 --- a/quantum/keyboard.c +++ b/quantum/keyboard.c @@ -175,12 +175,11 @@ uint32_t get_matrix_scan_rate(void) { #endif #ifdef MATRIX_HAS_GHOST -extern const uint16_t keymaps[][MATRIX_ROWS][MATRIX_COLS]; -static matrix_row_t get_real_keys(uint8_t row, matrix_row_t rowdata) { +static matrix_row_t get_real_keys(uint8_t row, matrix_row_t rowdata) { matrix_row_t out = 0; for (uint8_t col = 0; col < MATRIX_COLS; col++) { // read each key in the row data and check if the keymap defines it as a real key - if (pgm_read_byte(&keymaps[0][row][col]) && (rowdata & (1 << col))) { + if (keycode_at_keymap_location(0, row, col) && (rowdata & (1 << col))) { // this creates new row data, if a key is defined in the keymap, it will be set here out |= 1 << col; } diff --git a/quantum/keymap.h b/quantum/keymap.h index 67e35c4e2f..0225f53362 100644 --- a/quantum/keymap.h +++ b/quantum/keymap.h @@ -33,8 +33,6 @@ along with this program. If not, see . // translates key to keycode uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key); -extern const uint16_t keymaps[][MATRIX_ROWS][MATRIX_COLS]; - #ifdef ENCODER_MAP_ENABLE // Ensure we have a forward declaration for the encoder map # include "encoder.h" diff --git a/quantum/keymap_common.c b/quantum/keymap_common.c index 1d5ef9b403..5e8a043470 100644 --- a/quantum/keymap_common.c +++ b/quantum/keymap_common.c @@ -147,13 +147,13 @@ action_t action_for_keycode(uint16_t keycode) { // translates key to keycode __attribute__((weak)) uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key) { if (key.row < MATRIX_ROWS && key.col < MATRIX_COLS) { - return pgm_read_word(&keymaps[layer][key.row][key.col]); + return keycode_at_keymap_location(layer, key.row, key.col); } #ifdef ENCODER_MAP_ENABLE else if (key.row == KEYLOC_ENCODER_CW && key.col < NUM_ENCODERS) { - return pgm_read_word(&encoder_map[layer][key.col][0]); + return keycode_at_encodermap_location(layer, key.col, true); } else if (key.row == KEYLOC_ENCODER_CCW && key.col < NUM_ENCODERS) { - return pgm_read_word(&encoder_map[layer][key.col][1]); + return keycode_at_encodermap_location(layer, key.col, false); } #endif // ENCODER_MAP_ENABLE return KC_NO; diff --git a/quantum/keymap_introspection.c b/quantum/keymap_introspection.c index 179b5eb037..93aab82fcc 100644 --- a/quantum/keymap_introspection.c +++ b/quantum/keymap_introspection.c @@ -19,6 +19,17 @@ uint8_t keymap_layer_count(void) { _Static_assert(NUM_KEYMAP_LAYERS <= MAX_LAYER, "Number of keymap layers exceeds maximum set by LAYER_STATE_(8|16|32)BIT"); +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) { + return pgm_read_word(&keymaps[layer_num][row][column]); + } + return KC_NO; +} + +__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); +} + #if defined(ENCODER_ENABLE) && defined(ENCODER_MAP_ENABLE) # define NUM_ENCODERMAP_LAYERS ((uint8_t)(sizeof(encoder_map) / ((NUM_ENCODERS) * (2) * sizeof(uint16_t)))) @@ -29,4 +40,15 @@ uint8_t encodermap_layer_count(void) { _Static_assert(NUM_KEYMAP_LAYERS == NUM_ENCODERMAP_LAYERS, "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) { + return pgm_read_word(&encoder_map[layer_num][encoder_idx][clockwise ? 0 : 1]); + } + return KC_NO; +} + +__attribute__((weak)) uint16_t keycode_at_encodermap_location(uint8_t layer_num, uint8_t encoder_idx, bool clockwise) { + return keycode_at_encodermap_location_raw(layer_num, encoder_idx, clockwise); +} + #endif // defined(ENCODER_ENABLE) && defined(ENCODER_MAP_ENABLE) diff --git a/quantum/keymap_introspection.h b/quantum/keymap_introspection.h index 23f6f2016f..9de706a021 100644 --- a/quantum/keymap_introspection.h +++ b/quantum/keymap_introspection.h @@ -7,9 +7,19 @@ // Get the number of layers defined in the keymap uint8_t keymap_layer_count(void); +// Get the keycode for the keymap location, stored in firmware rather than any other persistent storage +uint16_t keycode_at_keymap_location_raw(uint8_t layer_num, uint8_t row, uint8_t column); +// 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); + #if defined(ENCODER_ENABLE) && defined(ENCODER_MAP_ENABLE) // Get the number of layers defined in the encoder map uint8_t encodermap_layer_count(void); +// Get the keycode for the encoder mapping location, stored in firmware rather than any other persistent storage +uint16_t keycode_at_encodermap_location_raw(uint8_t layer_num, uint8_t encoder_idx, bool clockwise); +// Get the keycode for the encoder mapping location, potentially stored dynamically +uint16_t keycode_at_encodermap_location(uint8_t layer_num, uint8_t encoder_idx, bool clockwise); + #endif // defined(ENCODER_ENABLE) && defined(ENCODER_MAP_ENABLE) -- cgit v1.2.3