2020-12-31 00:27:37 +06:00
|
|
|
"""Used by the make system to generate layouts.h from info.json.
|
|
|
|
"""
|
|
|
|
from milc import cli
|
|
|
|
|
|
|
|
from qmk.constants import COL_LETTERS, ROW_LETTERS
|
|
|
|
from qmk.decorators import automagic_keyboard, automagic_keymap
|
|
|
|
from qmk.info import info_json
|
2021-04-15 08:00:22 +06:00
|
|
|
from qmk.keyboard import keyboard_completer, keyboard_folder
|
2020-12-31 00:27:37 +06:00
|
|
|
from qmk.path import is_keyboard, normpath
|
|
|
|
|
|
|
|
usb_properties = {
|
|
|
|
'vid': 'VENDOR_ID',
|
|
|
|
'pid': 'PRODUCT_ID',
|
|
|
|
'device_ver': 'DEVICE_VER',
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@cli.argument('-o', '--output', arg_only=True, type=normpath, help='File to write to')
|
|
|
|
@cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
|
2021-04-15 08:00:22 +06:00
|
|
|
@cli.argument('-kb', '--keyboard', type=keyboard_folder, completer=keyboard_completer, help='Keyboard to generate config.h for.')
|
2020-12-31 00:27:37 +06:00
|
|
|
@cli.subcommand('Used by the make system to generate layouts.h from info.json', hidden=True)
|
|
|
|
@automagic_keyboard
|
|
|
|
@automagic_keymap
|
|
|
|
def generate_layouts(cli):
|
|
|
|
"""Generates the layouts.h file.
|
|
|
|
"""
|
|
|
|
# Determine our keyboard(s)
|
|
|
|
if not cli.config.generate_layouts.keyboard:
|
2021-03-01 02:19:07 +06:00
|
|
|
cli.log.error('Missing parameter: --keyboard')
|
2020-12-31 00:27:37 +06:00
|
|
|
cli.subcommands['info'].print_help()
|
|
|
|
return False
|
|
|
|
|
|
|
|
if not is_keyboard(cli.config.generate_layouts.keyboard):
|
|
|
|
cli.log.error('Invalid keyboard: "%s"', cli.config.generate_layouts.keyboard)
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Build the info.json file
|
|
|
|
kb_info_json = info_json(cli.config.generate_layouts.keyboard)
|
|
|
|
|
|
|
|
# Build the layouts.h file.
|
|
|
|
layouts_h_lines = ['/* This file was generated by `qmk generate-layouts`. Do not edit or copy.' ' */', '', '#pragma once']
|
|
|
|
|
2021-01-03 08:08:17 +06:00
|
|
|
if 'matrix_pins' in kb_info_json:
|
|
|
|
if 'direct' in kb_info_json['matrix_pins']:
|
|
|
|
col_num = len(kb_info_json['matrix_pins']['direct'][0])
|
|
|
|
row_num = len(kb_info_json['matrix_pins']['direct'])
|
|
|
|
elif 'cols' in kb_info_json['matrix_pins'] and 'rows' in kb_info_json['matrix_pins']:
|
|
|
|
col_num = len(kb_info_json['matrix_pins']['cols'])
|
|
|
|
row_num = len(kb_info_json['matrix_pins']['rows'])
|
|
|
|
else:
|
|
|
|
cli.log.error('%s: Invalid matrix config.', cli.config.generate_layouts.keyboard)
|
|
|
|
return False
|
2020-12-31 00:27:37 +06:00
|
|
|
|
|
|
|
for layout_name in kb_info_json['layouts']:
|
|
|
|
if kb_info_json['layouts'][layout_name]['c_macro']:
|
|
|
|
continue
|
|
|
|
|
2021-01-10 03:34:14 +06:00
|
|
|
if 'matrix' not in kb_info_json['layouts'][layout_name]['layout'][0]:
|
|
|
|
cli.log.debug('%s/%s: No matrix data!', cli.config.generate_layouts.keyboard, layout_name)
|
|
|
|
continue
|
|
|
|
|
2020-12-31 00:27:37 +06:00
|
|
|
layout_keys = []
|
|
|
|
layout_matrix = [['KC_NO' for i in range(col_num)] for i in range(row_num)]
|
|
|
|
|
|
|
|
for i, key in enumerate(kb_info_json['layouts'][layout_name]['layout']):
|
|
|
|
row = key['matrix'][0]
|
|
|
|
col = key['matrix'][1]
|
|
|
|
identifier = 'k%s%s' % (ROW_LETTERS[row], COL_LETTERS[col])
|
|
|
|
|
|
|
|
try:
|
|
|
|
layout_matrix[row][col] = identifier
|
|
|
|
layout_keys.append(identifier)
|
|
|
|
except IndexError:
|
|
|
|
key_name = key.get('label', identifier)
|
|
|
|
cli.log.error('Matrix data out of bounds for layout %s at index %s (%s): %s, %s', layout_name, i, key_name, row, col)
|
|
|
|
return False
|
|
|
|
|
|
|
|
layouts_h_lines.append('')
|
|
|
|
layouts_h_lines.append('#define %s(%s) {\\' % (layout_name, ', '.join(layout_keys)))
|
|
|
|
|
|
|
|
rows = ', \\\n'.join(['\t {' + ', '.join(row) + '}' for row in layout_matrix])
|
|
|
|
rows += ' \\'
|
|
|
|
layouts_h_lines.append(rows)
|
|
|
|
layouts_h_lines.append('}')
|
|
|
|
|
2021-02-28 02:00:50 +06:00
|
|
|
for alias, target in kb_info_json.get('layout_aliases', {}).items():
|
|
|
|
layouts_h_lines.append('')
|
2021-05-22 05:59:10 +06:00
|
|
|
layouts_h_lines.append(f'#ifndef {alias}')
|
|
|
|
layouts_h_lines.append(f'# define {alias} {target}')
|
|
|
|
layouts_h_lines.append('#endif')
|
2021-02-28 02:00:50 +06:00
|
|
|
|
2020-12-31 00:27:37 +06:00
|
|
|
# Show the results
|
|
|
|
layouts_h = '\n'.join(layouts_h_lines) + '\n'
|
|
|
|
|
|
|
|
if cli.args.output:
|
|
|
|
cli.args.output.parent.mkdir(parents=True, exist_ok=True)
|
|
|
|
if cli.args.output.exists():
|
2021-03-01 02:19:07 +06:00
|
|
|
cli.args.output.replace(cli.args.output.parent / (cli.args.output.name + '.bak'))
|
2020-12-31 00:27:37 +06:00
|
|
|
cli.args.output.write_text(layouts_h)
|
|
|
|
|
|
|
|
if not cli.args.quiet:
|
|
|
|
cli.log.info('Wrote info_config.h to %s.', cli.args.output)
|
|
|
|
|
|
|
|
else:
|
|
|
|
print(layouts_h)
|