diff options
Diffstat (limited to 'lib/python/qmk/cli/generate/layouts.py')
-rwxr-xr-x | lib/python/qmk/cli/generate/layouts.py | 93 |
1 files changed, 93 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..809f0ef7e --- /dev/null +++ b/lib/python/qmk/cli/generate/layouts.py | |||
@@ -0,0 +1,93 @@ | |||
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 'direct' in kb_info_json['matrix_pins']: | ||
43 | col_num = len(kb_info_json['matrix_pins']['direct'][0]) | ||
44 | row_num = len(kb_info_json['matrix_pins']['direct']) | ||
45 | elif 'cols' in kb_info_json['matrix_pins'] and 'rows' in kb_info_json['matrix_pins']: | ||
46 | col_num = len(kb_info_json['matrix_pins']['cols']) | ||
47 | row_num = len(kb_info_json['matrix_pins']['rows']) | ||
48 | else: | ||
49 | cli.log.error('%s: Invalid matrix config.', cli.config.generate_layouts.keyboard) | ||
50 | return False | ||
51 | |||
52 | for layout_name in kb_info_json['layouts']: | ||
53 | if kb_info_json['layouts'][layout_name]['c_macro']: | ||
54 | continue | ||
55 | |||
56 | layout_keys = [] | ||
57 | layout_matrix = [['KC_NO' for i in range(col_num)] for i in range(row_num)] | ||
58 | |||
59 | for i, key in enumerate(kb_info_json['layouts'][layout_name]['layout']): | ||
60 | row = key['matrix'][0] | ||
61 | col = key['matrix'][1] | ||
62 | identifier = 'k%s%s' % (ROW_LETTERS[row], COL_LETTERS[col]) | ||
63 | |||
64 | try: | ||
65 | layout_matrix[row][col] = identifier | ||
66 | layout_keys.append(identifier) | ||
67 | except IndexError: | ||
68 | key_name = key.get('label', identifier) | ||
69 | cli.log.error('Matrix data out of bounds for layout %s at index %s (%s): %s, %s', layout_name, i, key_name, row, col) | ||
70 | return False | ||
71 | |||
72 | layouts_h_lines.append('') | ||
73 | layouts_h_lines.append('#define %s(%s) {\\' % (layout_name, ', '.join(layout_keys))) | ||
74 | |||
75 | rows = ', \\\n'.join(['\t {' + ', '.join(row) + '}' for row in layout_matrix]) | ||
76 | rows += ' \\' | ||
77 | layouts_h_lines.append(rows) | ||
78 | layouts_h_lines.append('}') | ||
79 | |||
80 | # Show the results | ||
81 | layouts_h = '\n'.join(layouts_h_lines) + '\n' | ||
82 | |||
83 | if cli.args.output: | ||
84 | cli.args.output.parent.mkdir(parents=True, exist_ok=True) | ||
85 | if cli.args.output.exists(): | ||
86 | cli.args.output.replace(cli.args.output.name + '.bak') | ||
87 | cli.args.output.write_text(layouts_h) | ||
88 | |||
89 | if not cli.args.quiet: | ||
90 | cli.log.info('Wrote info_config.h to %s.', cli.args.output) | ||
91 | |||
92 | else: | ||
93 | print(layouts_h) | ||