diff options
Diffstat (limited to 'lib/python/qmk/cli/generate/layouts.py')
| -rwxr-xr-x | lib/python/qmk/cli/generate/layouts.py | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/lib/python/qmk/cli/generate/layouts.py b/lib/python/qmk/cli/generate/layouts.py new file mode 100755 index 000000000..b7baae065 --- /dev/null +++ b/lib/python/qmk/cli/generate/layouts.py | |||
| @@ -0,0 +1,98 @@ | |||
| 1 | """Used by the make system to generate layouts.h from info.json. | ||
| 2 | """ | ||
| 3 | from milc import cli | ||
| 4 | |||
| 5 | from qmk.constants import COL_LETTERS, ROW_LETTERS | ||
| 6 | from qmk.decorators import automagic_keyboard, automagic_keymap | ||
| 7 | from qmk.info import info_json | ||
| 8 | from qmk.path import is_keyboard, normpath | ||
| 9 | |||
| 10 | usb_properties = { | ||
| 11 | 'vid': 'VENDOR_ID', | ||
| 12 | 'pid': 'PRODUCT_ID', | ||
| 13 | 'device_ver': 'DEVICE_VER', | ||
| 14 | } | ||
| 15 | |||
| 16 | |||
| 17 | @cli.argument('-o', '--output', arg_only=True, type=normpath, help='File to write to') | ||
| 18 | @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages") | ||
| 19 | @cli.argument('-kb', '--keyboard', help='Keyboard to generate config.h for.') | ||
| 20 | @cli.subcommand('Used by the make system to generate layouts.h from info.json', hidden=True) | ||
| 21 | @automagic_keyboard | ||
| 22 | @automagic_keymap | ||
| 23 | def generate_layouts(cli): | ||
| 24 | """Generates the layouts.h file. | ||
| 25 | """ | ||
| 26 | # Determine our keyboard(s) | ||
| 27 | if not cli.config.generate_layouts.keyboard: | ||
| 28 | cli.log.error('Missing paramater: --keyboard') | ||
| 29 | cli.subcommands['info'].print_help() | ||
| 30 | return False | ||
| 31 | |||
| 32 | if not is_keyboard(cli.config.generate_layouts.keyboard): | ||
| 33 | cli.log.error('Invalid keyboard: "%s"', cli.config.generate_layouts.keyboard) | ||
| 34 | return False | ||
| 35 | |||
| 36 | # Build the info.json file | ||
| 37 | kb_info_json = info_json(cli.config.generate_layouts.keyboard) | ||
| 38 | |||
| 39 | # Build the layouts.h file. | ||
| 40 | layouts_h_lines = ['/* This file was generated by `qmk generate-layouts`. Do not edit or copy.' ' */', '', '#pragma once'] | ||
| 41 | |||
| 42 | if 'matrix_pins' in kb_info_json: | ||
| 43 | if 'direct' in kb_info_json['matrix_pins']: | ||
| 44 | col_num = len(kb_info_json['matrix_pins']['direct'][0]) | ||
| 45 | row_num = len(kb_info_json['matrix_pins']['direct']) | ||
| 46 | elif 'cols' in kb_info_json['matrix_pins'] and 'rows' in kb_info_json['matrix_pins']: | ||
| 47 | col_num = len(kb_info_json['matrix_pins']['cols']) | ||
| 48 | row_num = len(kb_info_json['matrix_pins']['rows']) | ||
| 49 | else: | ||
| 50 | cli.log.error('%s: Invalid matrix config.', cli.config.generate_layouts.keyboard) | ||
| 51 | return False | ||
| 52 | |||
| 53 | for layout_name in kb_info_json['layouts']: | ||
| 54 | if kb_info_json['layouts'][layout_name]['c_macro']: | ||
| 55 | continue | ||
| 56 | |||
| 57 | if 'matrix' not in kb_info_json['layouts'][layout_name]['layout'][0]: | ||
| 58 | cli.log.debug('%s/%s: No matrix data!', cli.config.generate_layouts.keyboard, layout_name) | ||
| 59 | continue | ||
| 60 | |||
| 61 | layout_keys = [] | ||
| 62 | layout_matrix = [['KC_NO' for i in range(col_num)] for i in range(row_num)] | ||
| 63 | |||
| 64 | for i, key in enumerate(kb_info_json['layouts'][layout_name]['layout']): | ||
| 65 | row = key['matrix'][0] | ||
| 66 | col = key['matrix'][1] | ||
| 67 | identifier = 'k%s%s' % (ROW_LETTERS[row], COL_LETTERS[col]) | ||
| 68 | |||
| 69 | try: | ||
| 70 | layout_matrix[row][col] = identifier | ||
| 71 | layout_keys.append(identifier) | ||
| 72 | except IndexError: | ||
| 73 | key_name = key.get('label', identifier) | ||
| 74 | cli.log.error('Matrix data out of bounds for layout %s at index %s (%s): %s, %s', layout_name, i, key_name, row, col) | ||
| 75 | return False | ||
| 76 | |||
| 77 | layouts_h_lines.append('') | ||
| 78 | layouts_h_lines.append('#define %s(%s) {\\' % (layout_name, ', '.join(layout_keys))) | ||
| 79 | |||
| 80 | rows = ', \\\n'.join(['\t {' + ', '.join(row) + '}' for row in layout_matrix]) | ||
| 81 | rows += ' \\' | ||
| 82 | layouts_h_lines.append(rows) | ||
| 83 | layouts_h_lines.append('}') | ||
| 84 | |||
| 85 | # Show the results | ||
| 86 | layouts_h = '\n'.join(layouts_h_lines) + '\n' | ||
| 87 | |||
| 88 | if cli.args.output: | ||
| 89 | cli.args.output.parent.mkdir(parents=True, exist_ok=True) | ||
| 90 | if cli.args.output.exists(): | ||
| 91 | cli.args.output.replace(cli.args.output.name + '.bak') | ||
| 92 | cli.args.output.write_text(layouts_h) | ||
| 93 | |||
| 94 | if not cli.args.quiet: | ||
| 95 | cli.log.info('Wrote info_config.h to %s.', cli.args.output) | ||
| 96 | |||
| 97 | else: | ||
| 98 | print(layouts_h) | ||
