logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

keymap_introspection.c (6456B)


  1. // Copyright 2022 Nick Brassel (@tzarc)
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #if defined(COMMUNITY_MODULES_ENABLE)
  4. # include "community_modules_introspection.h"
  5. #endif // defined(COMMUNITY_MODULES_ENABLE)
  6. // Pull the actual keymap code so that we can inspect stuff from it
  7. #include KEYMAP_C
  8. // Allow for keymap or userspace rules.mk to specify an alternate location for the keymap array
  9. #ifdef INTROSPECTION_KEYMAP_C
  10. # include INTROSPECTION_KEYMAP_C
  11. #endif // INTROSPECTION_KEYMAP_C
  12. #include "compiler_support.h"
  13. #include "keymap_introspection.h"
  14. #include "util.h"
  15. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  16. // Key mapping
  17. #define NUM_KEYMAP_LAYERS_RAW ((uint8_t)(sizeof(keymaps) / ((MATRIX_ROWS) * (MATRIX_COLS) * sizeof(uint16_t))))
  18. uint8_t keymap_layer_count_raw(void) {
  19. return NUM_KEYMAP_LAYERS_RAW;
  20. }
  21. __attribute__((weak)) uint8_t keymap_layer_count(void) {
  22. return keymap_layer_count_raw();
  23. }
  24. #ifdef DYNAMIC_KEYMAP_ENABLE
  25. STATIC_ASSERT(NUM_KEYMAP_LAYERS_RAW <= MAX_LAYER, "Number of keymap layers exceeds maximum set by DYNAMIC_KEYMAP_LAYER_COUNT");
  26. #else
  27. STATIC_ASSERT(NUM_KEYMAP_LAYERS_RAW <= MAX_LAYER, "Number of keymap layers exceeds maximum set by LAYER_STATE_(8|16|32)BIT");
  28. #endif
  29. uint16_t keycode_at_keymap_location_raw(uint8_t layer_num, uint8_t row, uint8_t column) {
  30. if (layer_num < NUM_KEYMAP_LAYERS_RAW && row < MATRIX_ROWS && column < MATRIX_COLS) {
  31. return pgm_read_word(&keymaps[layer_num][row][column]);
  32. }
  33. return KC_TRNS;
  34. }
  35. __attribute__((weak)) uint16_t keycode_at_keymap_location(uint8_t layer_num, uint8_t row, uint8_t column) {
  36. return keycode_at_keymap_location_raw(layer_num, row, column);
  37. }
  38. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  39. // Encoder mapping
  40. #if defined(ENCODER_ENABLE) && defined(ENCODER_MAP_ENABLE)
  41. # define NUM_ENCODERMAP_LAYERS_RAW ((uint8_t)(sizeof(encoder_map) / ((NUM_ENCODERS) * (NUM_DIRECTIONS) * sizeof(uint16_t))))
  42. uint8_t encodermap_layer_count_raw(void) {
  43. return NUM_ENCODERMAP_LAYERS_RAW;
  44. }
  45. __attribute__((weak)) uint8_t encodermap_layer_count(void) {
  46. return encodermap_layer_count_raw();
  47. }
  48. STATIC_ASSERT(NUM_KEYMAP_LAYERS_RAW == NUM_ENCODERMAP_LAYERS_RAW, "Number of encoder_map layers doesn't match the number of keymap layers");
  49. uint16_t keycode_at_encodermap_location_raw(uint8_t layer_num, uint8_t encoder_idx, bool clockwise) {
  50. if (layer_num < NUM_ENCODERMAP_LAYERS_RAW && encoder_idx < NUM_ENCODERS) {
  51. return pgm_read_word(&encoder_map[layer_num][encoder_idx][clockwise ? 0 : 1]);
  52. }
  53. return KC_TRNS;
  54. }
  55. __attribute__((weak)) uint16_t keycode_at_encodermap_location(uint8_t layer_num, uint8_t encoder_idx, bool clockwise) {
  56. return keycode_at_encodermap_location_raw(layer_num, encoder_idx, clockwise);
  57. }
  58. #endif // defined(ENCODER_ENABLE) && defined(ENCODER_MAP_ENABLE)
  59. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  60. // Dip Switch mapping
  61. #if defined(DIP_SWITCH_ENABLE) && defined(DIP_SWITCH_MAP_ENABLE)
  62. uint16_t keycode_at_dip_switch_map_location_raw(uint8_t switch_idx, bool on) {
  63. if (switch_idx < NUM_DIP_SWITCHES) {
  64. return pgm_read_word(&dip_switch_map[switch_idx][!!on]);
  65. }
  66. return KC_TRNS;
  67. }
  68. __attribute__((weak)) uint16_t keycode_at_dip_switch_map_location(uint8_t switch_idx, bool on) {
  69. return keycode_at_dip_switch_map_location_raw(switch_idx, on);
  70. }
  71. #endif // defined(DIP_SWITCH_ENABLE) && defined(DIP_SWITCH_MAP_ENABLE)
  72. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  73. // Combos
  74. #if defined(COMBO_ENABLE)
  75. uint16_t combo_count_raw(void) {
  76. return ARRAY_SIZE(key_combos);
  77. }
  78. __attribute__((weak)) uint16_t combo_count(void) {
  79. return combo_count_raw();
  80. }
  81. STATIC_ASSERT(ARRAY_SIZE(key_combos) <= (QK_KB), "Number of combos is abnormally high. Are you using SAFE_RANGE in an enum for combos?");
  82. combo_t* combo_get_raw(uint16_t combo_idx) {
  83. if (combo_idx >= combo_count_raw()) {
  84. return NULL;
  85. }
  86. return &key_combos[combo_idx];
  87. }
  88. __attribute__((weak)) combo_t* combo_get(uint16_t combo_idx) {
  89. return combo_get_raw(combo_idx);
  90. }
  91. #endif // defined(COMBO_ENABLE)
  92. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  93. // Tap Dance
  94. #if defined(TAP_DANCE_ENABLE)
  95. uint16_t tap_dance_count_raw(void) {
  96. return ARRAY_SIZE(tap_dance_actions);
  97. }
  98. __attribute__((weak)) uint16_t tap_dance_count(void) {
  99. return tap_dance_count_raw();
  100. }
  101. 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?");
  102. tap_dance_action_t* tap_dance_get_raw(uint16_t tap_dance_idx) {
  103. if (tap_dance_idx >= tap_dance_count_raw()) {
  104. return NULL;
  105. }
  106. return &tap_dance_actions[tap_dance_idx];
  107. }
  108. __attribute__((weak)) tap_dance_action_t* tap_dance_get(uint16_t tap_dance_idx) {
  109. return tap_dance_get_raw(tap_dance_idx);
  110. }
  111. #endif // defined(TAP_DANCE_ENABLE)
  112. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  113. // Key Overrides
  114. #if defined(KEY_OVERRIDE_ENABLE)
  115. uint16_t key_override_count_raw(void) {
  116. return ARRAY_SIZE(key_overrides);
  117. }
  118. __attribute__((weak)) uint16_t key_override_count(void) {
  119. return key_override_count_raw();
  120. }
  121. 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?");
  122. const key_override_t* key_override_get_raw(uint16_t key_override_idx) {
  123. if (key_override_idx >= key_override_count_raw()) {
  124. return NULL;
  125. }
  126. return key_overrides[key_override_idx];
  127. }
  128. __attribute__((weak)) const key_override_t* key_override_get(uint16_t key_override_idx) {
  129. return key_override_get_raw(key_override_idx);
  130. }
  131. #endif // defined(KEY_OVERRIDE_ENABLE)
  132. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  133. // Community modules (must be last in this file!)
  134. #if defined(COMMUNITY_MODULES_ENABLE)
  135. # include "community_modules_introspection.c"
  136. #endif // defined(COMMUNITY_MODULES_ENABLE)