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