layouts.py 3.8 KB

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