keymap_introspection.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. #include "util.h"
  11. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  12. // Key mapping
  13. #define NUM_KEYMAP_LAYERS_RAW ((uint8_t)(sizeof(keymaps) / ((MATRIX_ROWS) * (MATRIX_COLS) * sizeof(uint16_t))))
  14. uint8_t keymap_layer_count_raw(void) {
  15. return NUM_KEYMAP_LAYERS_RAW;
  16. }
  17. __attribute__((weak)) uint8_t keymap_layer_count(void) {
  18. return keymap_layer_count_raw();
  19. }
  20. #ifdef DYNAMIC_KEYMAP_ENABLE
  21. _Static_assert(NUM_KEYMAP_LAYERS_RAW <= MAX_LAYER, "Number of keymap layers exceeds maximum set by DYNAMIC_KEYMAP_LAYER_COUNT");
  22. #else
  23. _Static_assert(NUM_KEYMAP_LAYERS_RAW <= MAX_LAYER, "Number of keymap layers exceeds maximum set by LAYER_STATE_(8|16|32)BIT");
  24. #endif
  25. uint16_t keycode_at_keymap_location_raw(uint8_t layer_num, uint8_t row, uint8_t column) {
  26. if (layer_num < NUM_KEYMAP_LAYERS_RAW && row < MATRIX_ROWS && column < MATRIX_COLS) {
  27. return pgm_read_word(&keymaps[layer_num][row][column]);
  28. }
  29. return KC_TRNS;
  30. }
  31. __attribute__((weak)) uint16_t keycode_at_keymap_location(uint8_t layer_num, uint8_t row, uint8_t column) {
  32. return keycode_at_keymap_location_raw(layer_num, row, column);
  33. }
  34. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  35. // Encoder mapping
  36. #if defined(ENCODER_ENABLE) && defined(ENCODER_MAP_ENABLE)
  37. # define NUM_ENCODERMAP_LAYERS_RAW ((uint8_t)(sizeof(encoder_map) / ((NUM_ENCODERS) * (NUM_DIRECTIONS) * sizeof(uint16_t))))
  38. uint8_t encodermap_layer_count_raw(void) {
  39. return NUM_ENCODERMAP_LAYERS_RAW;
  40. }
  41. __attribute__((weak)) uint8_t encodermap_layer_count(void) {
  42. return encodermap_layer_count_raw();
  43. }
  44. _Static_assert(NUM_KEYMAP_LAYERS_RAW == NUM_ENCODERMAP_LAYERS_RAW, "Number of encoder_map layers doesn't match the number of keymap layers");
  45. uint16_t keycode_at_encodermap_location_raw(uint8_t layer_num, uint8_t encoder_idx, bool clockwise) {
  46. if (layer_num < NUM_ENCODERMAP_LAYERS_RAW && encoder_idx < NUM_ENCODERS) {
  47. return pgm_read_word(&encoder_map[layer_num][encoder_idx][clockwise ? 0 : 1]);
  48. }
  49. return KC_TRNS;
  50. }
  51. __attribute__((weak)) uint16_t keycode_at_encodermap_location(uint8_t layer_num, uint8_t encoder_idx, bool clockwise) {
  52. return keycode_at_encodermap_location_raw(layer_num, encoder_idx, clockwise);
  53. }
  54. #endif // defined(ENCODER_ENABLE) && defined(ENCODER_MAP_ENABLE)
  55. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  56. // Dip Switch mapping
  57. #if defined(DIP_SWITCH_ENABLE) && defined(DIP_SWITCH_MAP_ENABLE)
  58. uint16_t keycode_at_dip_switch_map_location_raw(uint8_t switch_idx, bool on) {
  59. if (switch_idx < NUM_DIP_SWITCHES) {
  60. return pgm_read_word(&dip_switch_map[switch_idx][!!on]);
  61. }
  62. return KC_TRNS;
  63. }
  64. __attribute__((weak)) uint16_t keycode_at_dip_switch_map_location(uint8_t switch_idx, bool on) {
  65. return keycode_at_dip_switch_map_location_raw(switch_idx, on);
  66. }
  67. #endif // defined(DIP_SWITCH_ENABLE) && defined(DIP_SWITCH_MAP_ENABLE)
  68. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  69. // Combos
  70. #if defined(COMBO_ENABLE)
  71. uint16_t combo_count_raw(void) {
  72. return ARRAY_SIZE(key_combos);
  73. }
  74. __attribute__((weak)) uint16_t combo_count(void) {
  75. return combo_count_raw();
  76. }
  77. _Static_assert(ARRAY_SIZE(key_combos) <= (QK_KB), "Number of combos is abnormally high. Are you using SAFE_RANGE in an enum for combos?");
  78. combo_t* combo_get_raw(uint16_t combo_idx) {
  79. if (combo_idx >= combo_count_raw()) {
  80. return NULL;
  81. }
  82. return &key_combos[combo_idx];
  83. }
  84. __attribute__((weak)) combo_t* combo_get(uint16_t combo_idx) {
  85. return combo_get_raw(combo_idx);
  86. }
  87. #endif // defined(COMBO_ENABLE)
  88. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  89. // Tap Dance
  90. #if defined(TAP_DANCE_ENABLE)
  91. uint16_t tap_dance_count_raw(void) {
  92. return ARRAY_SIZE(tap_dance_actions);
  93. }
  94. __attribute__((weak)) uint16_t tap_dance_count(void) {
  95. return tap_dance_count_raw();
  96. }
  97. _Static_assert(ARRAY_SIZE(tap_dance_actions) <= (QK_TAP_DANCE_MAX - QK_TAP_DANCE), "Number of tap dance actions exceeds maximum. Are you using SAFE_RANGE in tap dance enum?");
  98. tap_dance_action_t* tap_dance_get_raw(uint16_t tap_dance_idx) {
  99. if (tap_dance_idx >= tap_dance_count_raw()) {
  100. return NULL;
  101. }
  102. return &tap_dance_actions[tap_dance_idx];
  103. }
  104. __attribute__((weak)) tap_dance_action_t* tap_dance_get(uint16_t tap_dance_idx) {
  105. return tap_dance_get_raw(tap_dance_idx);
  106. }
  107. #endif // defined(TAP_DANCE_ENABLE)
  108. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  109. // Key Overrides
  110. #if defined(KEY_OVERRIDE_ENABLE)
  111. uint16_t key_override_count_raw(void) {
  112. return ARRAY_SIZE(key_overrides);
  113. }
  114. __attribute__((weak)) uint16_t key_override_count(void) {
  115. return key_override_count_raw();
  116. }
  117. _Static_assert(ARRAY_SIZE(key_overrides) <= (QK_KB), "Number of key overrides is abnormally high. Are you using SAFE_RANGE in an enum for key overrides?");
  118. const key_override_t* key_override_get_raw(uint16_t key_override_idx) {
  119. if (key_override_idx >= key_override_count_raw()) {
  120. return NULL;
  121. }
  122. return key_overrides[key_override_idx];
  123. }
  124. __attribute__((weak)) const key_override_t* key_override_get(uint16_t key_override_idx) {
  125. return key_override_get_raw(key_override_idx);
  126. }
  127. #endif // defined(KEY_OVERRIDE_ENABLE)