Skip to content

Commit

Permalink
Allow codegen of keymap.json => keymap.c without requiring a keymap.
Browse files Browse the repository at this point in the history
  • Loading branch information
tzarc committed Apr 10, 2024
1 parent 4acdddb commit 05027aa
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 21 deletions.
5 changes: 4 additions & 1 deletion builddefs/build_keyboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,10 @@ endif
# Have we found a keymap.json?
ifneq ("$(wildcard $(KEYMAP_JSON))", "")
ifneq ("$(wildcard $(KEYMAP_C))", "")
$(call WARNING_MESSAGE,Keymap is specified as both keymap.json and keymap.c -- keymap.json file wins.)
# Allow a separately-found keymap.c next to keymap.json -- the keymap.c
# generator will include the other keymap.c in the process, if supplied.
OTHER_KEYMAP_C := $(KEYMAP_C)
OPT_DEFS += -DOTHER_KEYMAP_C=\"$(OTHER_KEYMAP_C)\"
endif

KEYMAP_PATH := $(KEYMAP_JSON_PATH)
Expand Down
7 changes: 1 addition & 6 deletions data/schemas/keymap.jsonschema
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,5 @@
"notes": {
"type": "string"
}
},
"required": [
"keyboard",
"layout",
"layers"
]
}
}
4 changes: 3 additions & 1 deletion lib/python/qmk/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ def parse_configurator_json(configurator_file):
cli.log.error(f'Invalid JSON keymap: {configurator_file} : {e.message}')
exit(1)

keyboard = user_keymap['keyboard']
keyboard = None
if 'keyboard' in user_keymap:
keyboard = user_keymap['keyboard']
aliases = keyboard_alias_definitions()

while keyboard in aliases:
Expand Down
27 changes: 14 additions & 13 deletions lib/python/qmk/keymap.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,40 +27,38 @@
* edit it directly.
*/
__KEYCODE_OUTPUT_GOES_HERE__
const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
__KEYMAP_GOES_HERE__
};
#if defined(ENCODER_ENABLE) && defined(ENCODER_MAP_ENABLE)
const uint16_t PROGMEM encoder_map[][NUM_ENCODERS][NUM_DIRECTIONS] = {
__ENCODER_MAP_GOES_HERE__
};
#endif // defined(ENCODER_ENABLE) && defined(ENCODER_MAP_ENABLE)
__MACRO_OUTPUT_GOES_HERE__
#ifdef OTHER_KEYMAP_C
#if __has_include_next(OTHER_KEYMAP_C)
#include_next OTHER_KEYMAP_C
#endif // __has_include_next(OTHER_KEYMAP_C)
#endif // OTHER_KEYMAP_C
"""


def _generate_keymap_table(keymap_json):
lines = []
lines = ['const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {']
for layer_num, layer in enumerate(keymap_json['layers']):
if layer_num != 0:
lines[-1] = lines[-1] + ','
layer = map(_strip_any, layer)
layer_keys = ', '.join(layer)
lines.append('\t[%s] = %s(%s)' % (layer_num, keymap_json['layout'], layer_keys))
lines.append('};')
return lines


def _generate_encodermap_table(keymap_json):
lines = []
lines = ['#if defined(ENCODER_ENABLE) && defined(ENCODER_MAP_ENABLE)', 'const uint16_t PROGMEM encoder_map[][NUM_ENCODERS][NUM_DIRECTIONS] = {']
for layer_num, layer in enumerate(keymap_json['encoders']):
if layer_num != 0:
lines[-1] = lines[-1] + ','
encoder_keycode_txt = ', '.join([f'ENCODER_CCW_CW({_strip_any(e["ccw"])}, {_strip_any(e["cw"])})' for e in layer])
lines.append('\t[%s] = {%s}' % (layer_num, encoder_keycode_txt))
lines.extend(['};', '#endif // defined(ENCODER_ENABLE) && defined(ENCODER_MAP_ENABLE)'])
return lines


Expand Down Expand Up @@ -329,8 +327,11 @@ def generate_c(keymap_json):
A sequence of strings containing macros to implement for this keyboard.
"""
new_keymap = template_c(keymap_json['keyboard'])
layer_txt = _generate_keymap_table(keymap_json)
keymap = '\n'.join(layer_txt)

keymap = ''
if 'layers' in keymap_json and keymap_json['layers'] is not None:
layer_txt = _generate_keymap_table(keymap_json)
keymap = '\n'.join(layer_txt)
new_keymap = new_keymap.replace('__KEYMAP_GOES_HERE__', keymap)

encodermap = ''
Expand Down

0 comments on commit 05027aa

Please sign in to comment.