From bdee456a0d2ca505b5db18f84ae705326d2cacc5 Mon Sep 17 00:00:00 2001 From: Nick Brassel Date: Sat, 2 Sep 2023 20:20:50 +1000 Subject: [PATCH] Allow codegen of keymap.json => keymap.c without requiring a keymap. --- builddefs/build_keyboard.mk | 5 ++++- data/schemas/keymap.jsonschema | 7 +------ lib/python/qmk/commands.py | 2 +- lib/python/qmk/keymap.py | 28 ++++++++++++++++------------ 4 files changed, 22 insertions(+), 20 deletions(-) diff --git a/builddefs/build_keyboard.mk b/builddefs/build_keyboard.mk index ad81b85a27a7..08c132f14ad6 100644 --- a/builddefs/build_keyboard.mk +++ b/builddefs/build_keyboard.mk @@ -189,7 +189,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) diff --git a/data/schemas/keymap.jsonschema b/data/schemas/keymap.jsonschema index 7233e896e940..e967e45c53e4 100644 --- a/data/schemas/keymap.jsonschema +++ b/data/schemas/keymap.jsonschema @@ -72,10 +72,5 @@ "notes": { "type": "string" } - }, - "required": [ - "keyboard", - "layout", - "layers" - ] + } } diff --git a/lib/python/qmk/commands.py b/lib/python/qmk/commands.py index 97d9c5032c8f..a05b3641b5ca 100644 --- a/lib/python/qmk/commands.py +++ b/lib/python/qmk/commands.py @@ -55,7 +55,7 @@ def parse_configurator_json(configurator_file): cli.log.error(f'Invalid JSON keymap: {configurator_file} : {e.message}') maybe_exit(1) - keyboard = user_keymap['keyboard'] + keyboard = user_keymap.get('keyboard', None) aliases = keyboard_alias_definitions() while keyboard in aliases: diff --git a/lib/python/qmk/keymap.py b/lib/python/qmk/keymap.py index 97c358788a5e..b33fcc31d852 100644 --- a/lib/python/qmk/keymap.py +++ b/lib/python/qmk/keymap.py @@ -29,38 +29,39 @@ * This file was generated by qmk json2c. You may or may not want to * edit it directly. */ -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] = { +__KEYMAP_GOES_HERE__ __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(' [%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(' [%s] = {%s}' % (layer_num, encoder_keycode_txt)) + lines.extend(['};', '#endif // defined(ENCODER_ENABLE) && defined(ENCODER_MAP_ENABLE)']) return lines @@ -271,8 +272,11 @@ def generate_c(keymap_json): A sequence of strings containing macros to implement for this keyboard. """ new_keymap = DEFAULT_KEYMAP_C - 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 = ''