keymap_introspection.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright 2022 Nick Brassel (@tzarc)
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. // Pull the actual keymap code so that we can inspect stuff from it
  4. #include KEYMAP_C
  5. // Allow for keymap or userspace rules.mk to specify an alternate location for the keymap array
  6. #ifdef INTROSPECTION_KEYMAP_C
  7. # include INTROSPECTION_KEYMAP_C
  8. #endif // INTROSPECTION_KEYMAP_C
  9. #include "keymap_introspection.h"
  10. #define NUM_KEYMAP_LAYERS ((uint8_t)(sizeof(keymaps) / ((MATRIX_ROWS) * (MATRIX_COLS) * sizeof(uint16_t))))
  11. uint8_t keymap_layer_count(void) {
  12. return NUM_KEYMAP_LAYERS;
  13. }
  14. _Static_assert(NUM_KEYMAP_LAYERS <= MAX_LAYER, "Number of keymap layers exceeds maximum set by LAYER_STATE_(8|16|32)BIT");
  15. uint16_t keycode_at_keymap_location_raw(uint8_t layer_num, uint8_t row, uint8_t column) {
  16. if (layer_num < NUM_KEYMAP_LAYERS && row < MATRIX_ROWS && column < MATRIX_COLS) {
  17. return pgm_read_word(&keymaps[layer_num][row][column]);
  18. }
  19. return KC_NO;
  20. }
  21. __attribute__((weak)) uint16_t keycode_at_keymap_location(uint8_t layer_num, uint8_t row, uint8_t column) {
  22. return keycode_at_keymap_location_raw(layer_num, row, column);
  23. }
  24. #if defined(ENCODER_ENABLE) && defined(ENCODER_MAP_ENABLE)
  25. # define NUM_ENCODERMAP_LAYERS ((uint8_t)(sizeof(encoder_map) / ((NUM_ENCODERS) * (2) * sizeof(uint16_t))))
  26. uint8_t encodermap_layer_count(void) {
  27. return NUM_ENCODERMAP_LAYERS;
  28. }
  29. _Static_assert(NUM_KEYMAP_LAYERS == NUM_ENCODERMAP_LAYERS, "Number of encoder_map layers doesn't match the number of keymap layers");
  30. uint16_t keycode_at_encodermap_location_raw(uint8_t layer_num, uint8_t encoder_idx, bool clockwise) {
  31. if (layer_num < NUM_ENCODERMAP_LAYERS && encoder_idx < NUM_ENCODERS) {
  32. return pgm_read_word(&encoder_map[layer_num][encoder_idx][clockwise ? 0 : 1]);
  33. }
  34. return KC_NO;
  35. }
  36. __attribute__((weak)) uint16_t keycode_at_encodermap_location(uint8_t layer_num, uint8_t encoder_idx, bool clockwise) {
  37. return keycode_at_encodermap_location_raw(layer_num, encoder_idx, clockwise);
  38. }
  39. #endif // defined(ENCODER_ENABLE) && defined(ENCODER_MAP_ENABLE)