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 Aug 12, 2024
1 parent 2c6409f commit bdee456
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 20 deletions.
5 changes: 4 additions & 1 deletion builddefs/build_keyboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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"
]
}
}
2 changes: 1 addition & 1 deletion lib/python/qmk/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
28 changes: 16 additions & 12 deletions lib/python/qmk/keymap.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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 = ''
Expand Down

0 comments on commit bdee456

Please sign in to comment.