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