diff --git a/lib/python/qmk/cli/c2json.py b/lib/python/qmk/cli/c2json.py index f7f1f2ffbadc..3ee786cb4fcb 100644 --- a/lib/python/qmk/cli/c2json.py +++ b/lib/python/qmk/cli/c2json.py @@ -16,6 +16,7 @@ @cli.argument('--no-cpp', arg_only=True, action='store_false', help='Do not use \'cpp\' on keymap.c') +@cli.argument('--export-layer-names', arg_only=True, action='store_true', help='Export layer names when they are defined as enum') @cli.argument('-o', '--output', arg_only=True, type=qmk.path.normpath, help='File to write to') @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages") @cli.argument('-kb', '--keyboard', type=keyboard_folder, completer=keyboard_completer, help='The keyboard\'s name') @@ -51,7 +52,7 @@ def c2json(cli): return False try: - keymap_json = c2json_impl(keyboard, keymap, filename, use_cpp=cli.args.no_cpp) + keymap_json = c2json_impl(keyboard, keymap, filename, use_cpp=cli.args.no_cpp, export_layer_names=cli.args.export_layer_names) except CppError as e: if cli.config.general.verbose: cli.log.debug('The C pre-processor ran into a fatal error: %s', e) @@ -60,7 +61,7 @@ def c2json(cli): # Generate the keymap.json try: - keymap_json = generate_json(keymap_json['keymap'], keymap_json['keyboard'], keymap_json['layout'], keymap_json['layers']) + keymap_json = generate_json(keymap_json['keymap'], keymap_json['keyboard'], keymap_json['layout'], keymap_json['layers'], keymap_json['layer_names'] if cli.args.export_layer_names else None) except KeyError: cli.log.error('Something went wrong. Try to use --no-cpp.') return False diff --git a/lib/python/qmk/cli/via2json.py b/lib/python/qmk/cli/via2json.py index 537e1026406d..75d99ae9185b 100755 --- a/lib/python/qmk/cli/via2json.py +++ b/lib/python/qmk/cli/via2json.py @@ -161,7 +161,7 @@ def via2json(cli): keymap_data = _fix_macro_keys(keymap_data) # Generate the keymap.json - keymap_json = generate_json(cli.args.keymap, cli.args.keyboard, keymap_layout, keymap_data, macro_data) + keymap_json = generate_json(cli.args.keymap, cli.args.keyboard, keymap_layout, keymap_data, None, macro_data) keymap_lines = [json.dumps(keymap_json, cls=KeymapJSONEncoder, sort_keys=True)] dump_lines(cli.args.output, keymap_lines, cli.args.quiet) diff --git a/lib/python/qmk/keymap.py b/lib/python/qmk/keymap.py index 9dd043c4a898..08a68d27ee65 100644 --- a/lib/python/qmk/keymap.py +++ b/lib/python/qmk/keymap.py @@ -226,7 +226,7 @@ def is_keymap_dir(keymap, c=True, json=True, additional_files=None): return True -def generate_json(keymap, keyboard, layout, layers, macros=None): +def generate_json(keymap, keyboard, layout, layers, layer_names=None, macros=None): """Returns a `keymap.json` for the specified keyboard, layout, and layers. Args: @@ -242,6 +242,9 @@ def generate_json(keymap, keyboard, layout, layers, macros=None): layers An array of arrays describing the keymap. Each item in the inner array should be a string that is a valid QMK keycode. + layer_names + An array of strings describing the names of each layer. + macros A sequence of strings containing macros to implement for this keyboard. """ @@ -249,6 +252,8 @@ def generate_json(keymap, keyboard, layout, layers, macros=None): new_keymap['keymap'] = keymap new_keymap['layout'] = layout new_keymap['layers'] = layers + if layer_names: + new_keymap['layer_names'] = layer_names if macros: new_keymap['macros'] = macros @@ -327,7 +332,7 @@ def write_json(keyboard, keymap, layout, layers, macros=None): layers An array of arrays describing the keymap. Each item in the inner array should be a string that is a valid QMK keycode. """ - keymap_json = generate_json(keyboard, keymap, layout, layers, macros=None) + keymap_json = generate_json(keyboard, keymap, layout, layers, layer_names=None, macros=None) keymap_content = json.dumps(keymap_json) keymap_file = qmk.path.keymaps(keyboard)[0] / keymap / 'keymap.json' @@ -653,7 +658,7 @@ def parse_keymap_c(keymap_file, use_cpp=True): return keymap -def c2json(keyboard, keymap, keymap_file, use_cpp=True): +def c2json(keyboard, keymap, keymap_file, use_cpp=True, export_layer_names=False): """ Convert keymap.c to keymap.json Args: @@ -673,9 +678,10 @@ def c2json(keyboard, keymap, keymap_file, use_cpp=True): keymap_json = parse_keymap_c(keymap_file, use_cpp) dirty_layers = keymap_json.pop('layers', None) + layer_names = list() keymap_json['layers'] = list() for layer in dirty_layers: - layer.pop('name') + layer_names.append(layer.pop('name')) layout = layer.pop('layout') if not keymap_json.get('layout', False): keymap_json['layout'] = layout @@ -683,4 +689,6 @@ def c2json(keyboard, keymap, keymap_file, use_cpp=True): keymap_json['keyboard'] = keyboard keymap_json['keymap'] = keymap + if export_layer_names: + keymap_json['layer_names'] = layer_names return keymap_json