You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

767 lines
28 KiB

Add a lot more data to info.json (#13366) * add some split data to info.json * add tags * add half of config_options.md to info.json * add support for designating master split * sort out split transport and primary * fix bad data in UNUSED_PINS * fixup custom transport * wip * allow for setting split right half keyboard matrix * add SPLIT_USB_DETECT * minor cleanup * fix an erroneous message * rework split.usb_detect * adding missing rgblight vars to info.json * add mouse_key to info.json * add all remaining options from docs/config_options.md * fix audio voices * qmk info: Change text output to use dotted notation * tweak layout output * resolve alias names * break out some functions to make flake8 happy * add a field for bootloader instructions * qmk generate-info-json: add a write-to-file argument Adds an argument that instructs qmk generate-info-json to write the output to a file instead of just to the terminal. * -arg_only, +action Because it was never my intention that one would have to specify a value for the argument that enables writing the file. * Bring qmk generate-info-json inline with other generate commands * pytest fixup * fix esca/getawayvan * fix data driven errors for bpiphany converters * features.force_nkro -> usb.force_nkro * split.primary->split.main * fix esca/getawayvan_f042 * fix the bpiphany converters for real * fix bpiphany/tiger_lily * Apply suggestions from code review Co-authored-by: Nick Brassel <nick@tzarc.org> * fix generate-api errors * fix matrix pin extraction for split boards * fix ploopyco/trackball_nano/rev1_001 Co-authored-by: James Young <18669334+noroadsleft@users.noreply.github.com> Co-authored-by: Nick Brassel <nick@tzarc.org>
2 years ago
Add a lot more data to info.json (#13366) * add some split data to info.json * add tags * add half of config_options.md to info.json * add support for designating master split * sort out split transport and primary * fix bad data in UNUSED_PINS * fixup custom transport * wip * allow for setting split right half keyboard matrix * add SPLIT_USB_DETECT * minor cleanup * fix an erroneous message * rework split.usb_detect * adding missing rgblight vars to info.json * add mouse_key to info.json * add all remaining options from docs/config_options.md * fix audio voices * qmk info: Change text output to use dotted notation * tweak layout output * resolve alias names * break out some functions to make flake8 happy * add a field for bootloader instructions * qmk generate-info-json: add a write-to-file argument Adds an argument that instructs qmk generate-info-json to write the output to a file instead of just to the terminal. * -arg_only, +action Because it was never my intention that one would have to specify a value for the argument that enables writing the file. * Bring qmk generate-info-json inline with other generate commands * pytest fixup * fix esca/getawayvan * fix data driven errors for bpiphany converters * features.force_nkro -> usb.force_nkro * split.primary->split.main * fix esca/getawayvan_f042 * fix the bpiphany converters for real * fix bpiphany/tiger_lily * Apply suggestions from code review Co-authored-by: Nick Brassel <nick@tzarc.org> * fix generate-api errors * fix matrix pin extraction for split boards * fix ploopyco/trackball_nano/rev1_001 Co-authored-by: James Young <18669334+noroadsleft@users.noreply.github.com> Co-authored-by: Nick Brassel <nick@tzarc.org>
2 years ago
Add a lot more data to info.json (#13366) * add some split data to info.json * add tags * add half of config_options.md to info.json * add support for designating master split * sort out split transport and primary * fix bad data in UNUSED_PINS * fixup custom transport * wip * allow for setting split right half keyboard matrix * add SPLIT_USB_DETECT * minor cleanup * fix an erroneous message * rework split.usb_detect * adding missing rgblight vars to info.json * add mouse_key to info.json * add all remaining options from docs/config_options.md * fix audio voices * qmk info: Change text output to use dotted notation * tweak layout output * resolve alias names * break out some functions to make flake8 happy * add a field for bootloader instructions * qmk generate-info-json: add a write-to-file argument Adds an argument that instructs qmk generate-info-json to write the output to a file instead of just to the terminal. * -arg_only, +action Because it was never my intention that one would have to specify a value for the argument that enables writing the file. * Bring qmk generate-info-json inline with other generate commands * pytest fixup * fix esca/getawayvan * fix data driven errors for bpiphany converters * features.force_nkro -> usb.force_nkro * split.primary->split.main * fix esca/getawayvan_f042 * fix the bpiphany converters for real * fix bpiphany/tiger_lily * Apply suggestions from code review Co-authored-by: Nick Brassel <nick@tzarc.org> * fix generate-api errors * fix matrix pin extraction for split boards * fix ploopyco/trackball_nano/rev1_001 Co-authored-by: James Young <18669334+noroadsleft@users.noreply.github.com> Co-authored-by: Nick Brassel <nick@tzarc.org>
2 years ago
Add a lot more data to info.json (#13366) * add some split data to info.json * add tags * add half of config_options.md to info.json * add support for designating master split * sort out split transport and primary * fix bad data in UNUSED_PINS * fixup custom transport * wip * allow for setting split right half keyboard matrix * add SPLIT_USB_DETECT * minor cleanup * fix an erroneous message * rework split.usb_detect * adding missing rgblight vars to info.json * add mouse_key to info.json * add all remaining options from docs/config_options.md * fix audio voices * qmk info: Change text output to use dotted notation * tweak layout output * resolve alias names * break out some functions to make flake8 happy * add a field for bootloader instructions * qmk generate-info-json: add a write-to-file argument Adds an argument that instructs qmk generate-info-json to write the output to a file instead of just to the terminal. * -arg_only, +action Because it was never my intention that one would have to specify a value for the argument that enables writing the file. * Bring qmk generate-info-json inline with other generate commands * pytest fixup * fix esca/getawayvan * fix data driven errors for bpiphany converters * features.force_nkro -> usb.force_nkro * split.primary->split.main * fix esca/getawayvan_f042 * fix the bpiphany converters for real * fix bpiphany/tiger_lily * Apply suggestions from code review Co-authored-by: Nick Brassel <nick@tzarc.org> * fix generate-api errors * fix matrix pin extraction for split boards * fix ploopyco/trackball_nano/rev1_001 Co-authored-by: James Young <18669334+noroadsleft@users.noreply.github.com> Co-authored-by: Nick Brassel <nick@tzarc.org>
2 years ago
Add a lot more data to info.json (#13366) * add some split data to info.json * add tags * add half of config_options.md to info.json * add support for designating master split * sort out split transport and primary * fix bad data in UNUSED_PINS * fixup custom transport * wip * allow for setting split right half keyboard matrix * add SPLIT_USB_DETECT * minor cleanup * fix an erroneous message * rework split.usb_detect * adding missing rgblight vars to info.json * add mouse_key to info.json * add all remaining options from docs/config_options.md * fix audio voices * qmk info: Change text output to use dotted notation * tweak layout output * resolve alias names * break out some functions to make flake8 happy * add a field for bootloader instructions * qmk generate-info-json: add a write-to-file argument Adds an argument that instructs qmk generate-info-json to write the output to a file instead of just to the terminal. * -arg_only, +action Because it was never my intention that one would have to specify a value for the argument that enables writing the file. * Bring qmk generate-info-json inline with other generate commands * pytest fixup * fix esca/getawayvan * fix data driven errors for bpiphany converters * features.force_nkro -> usb.force_nkro * split.primary->split.main * fix esca/getawayvan_f042 * fix the bpiphany converters for real * fix bpiphany/tiger_lily * Apply suggestions from code review Co-authored-by: Nick Brassel <nick@tzarc.org> * fix generate-api errors * fix matrix pin extraction for split boards * fix ploopyco/trackball_nano/rev1_001 Co-authored-by: James Young <18669334+noroadsleft@users.noreply.github.com> Co-authored-by: Nick Brassel <nick@tzarc.org>
2 years ago
Add a lot more data to info.json (#13366) * add some split data to info.json * add tags * add half of config_options.md to info.json * add support for designating master split * sort out split transport and primary * fix bad data in UNUSED_PINS * fixup custom transport * wip * allow for setting split right half keyboard matrix * add SPLIT_USB_DETECT * minor cleanup * fix an erroneous message * rework split.usb_detect * adding missing rgblight vars to info.json * add mouse_key to info.json * add all remaining options from docs/config_options.md * fix audio voices * qmk info: Change text output to use dotted notation * tweak layout output * resolve alias names * break out some functions to make flake8 happy * add a field for bootloader instructions * qmk generate-info-json: add a write-to-file argument Adds an argument that instructs qmk generate-info-json to write the output to a file instead of just to the terminal. * -arg_only, +action Because it was never my intention that one would have to specify a value for the argument that enables writing the file. * Bring qmk generate-info-json inline with other generate commands * pytest fixup * fix esca/getawayvan * fix data driven errors for bpiphany converters * features.force_nkro -> usb.force_nkro * split.primary->split.main * fix esca/getawayvan_f042 * fix the bpiphany converters for real * fix bpiphany/tiger_lily * Apply suggestions from code review Co-authored-by: Nick Brassel <nick@tzarc.org> * fix generate-api errors * fix matrix pin extraction for split boards * fix ploopyco/trackball_nano/rev1_001 Co-authored-by: James Young <18669334+noroadsleft@users.noreply.github.com> Co-authored-by: Nick Brassel <nick@tzarc.org>
2 years ago
Add a lot more data to info.json (#13366) * add some split data to info.json * add tags * add half of config_options.md to info.json * add support for designating master split * sort out split transport and primary * fix bad data in UNUSED_PINS * fixup custom transport * wip * allow for setting split right half keyboard matrix * add SPLIT_USB_DETECT * minor cleanup * fix an erroneous message * rework split.usb_detect * adding missing rgblight vars to info.json * add mouse_key to info.json * add all remaining options from docs/config_options.md * fix audio voices * qmk info: Change text output to use dotted notation * tweak layout output * resolve alias names * break out some functions to make flake8 happy * add a field for bootloader instructions * qmk generate-info-json: add a write-to-file argument Adds an argument that instructs qmk generate-info-json to write the output to a file instead of just to the terminal. * -arg_only, +action Because it was never my intention that one would have to specify a value for the argument that enables writing the file. * Bring qmk generate-info-json inline with other generate commands * pytest fixup * fix esca/getawayvan * fix data driven errors for bpiphany converters * features.force_nkro -> usb.force_nkro * split.primary->split.main * fix esca/getawayvan_f042 * fix the bpiphany converters for real * fix bpiphany/tiger_lily * Apply suggestions from code review Co-authored-by: Nick Brassel <nick@tzarc.org> * fix generate-api errors * fix matrix pin extraction for split boards * fix ploopyco/trackball_nano/rev1_001 Co-authored-by: James Young <18669334+noroadsleft@users.noreply.github.com> Co-authored-by: Nick Brassel <nick@tzarc.org>
2 years ago
Add a lot more data to info.json (#13366) * add some split data to info.json * add tags * add half of config_options.md to info.json * add support for designating master split * sort out split transport and primary * fix bad data in UNUSED_PINS * fixup custom transport * wip * allow for setting split right half keyboard matrix * add SPLIT_USB_DETECT * minor cleanup * fix an erroneous message * rework split.usb_detect * adding missing rgblight vars to info.json * add mouse_key to info.json * add all remaining options from docs/config_options.md * fix audio voices * qmk info: Change text output to use dotted notation * tweak layout output * resolve alias names * break out some functions to make flake8 happy * add a field for bootloader instructions * qmk generate-info-json: add a write-to-file argument Adds an argument that instructs qmk generate-info-json to write the output to a file instead of just to the terminal. * -arg_only, +action Because it was never my intention that one would have to specify a value for the argument that enables writing the file. * Bring qmk generate-info-json inline with other generate commands * pytest fixup * fix esca/getawayvan * fix data driven errors for bpiphany converters * features.force_nkro -> usb.force_nkro * split.primary->split.main * fix esca/getawayvan_f042 * fix the bpiphany converters for real * fix bpiphany/tiger_lily * Apply suggestions from code review Co-authored-by: Nick Brassel <nick@tzarc.org> * fix generate-api errors * fix matrix pin extraction for split boards * fix ploopyco/trackball_nano/rev1_001 Co-authored-by: James Young <18669334+noroadsleft@users.noreply.github.com> Co-authored-by: Nick Brassel <nick@tzarc.org>
2 years ago
Add a lot more data to info.json (#13366) * add some split data to info.json * add tags * add half of config_options.md to info.json * add support for designating master split * sort out split transport and primary * fix bad data in UNUSED_PINS * fixup custom transport * wip * allow for setting split right half keyboard matrix * add SPLIT_USB_DETECT * minor cleanup * fix an erroneous message * rework split.usb_detect * adding missing rgblight vars to info.json * add mouse_key to info.json * add all remaining options from docs/config_options.md * fix audio voices * qmk info: Change text output to use dotted notation * tweak layout output * resolve alias names * break out some functions to make flake8 happy * add a field for bootloader instructions * qmk generate-info-json: add a write-to-file argument Adds an argument that instructs qmk generate-info-json to write the output to a file instead of just to the terminal. * -arg_only, +action Because it was never my intention that one would have to specify a value for the argument that enables writing the file. * Bring qmk generate-info-json inline with other generate commands * pytest fixup * fix esca/getawayvan * fix data driven errors for bpiphany converters * features.force_nkro -> usb.force_nkro * split.primary->split.main * fix esca/getawayvan_f042 * fix the bpiphany converters for real * fix bpiphany/tiger_lily * Apply suggestions from code review Co-authored-by: Nick Brassel <nick@tzarc.org> * fix generate-api errors * fix matrix pin extraction for split boards * fix ploopyco/trackball_nano/rev1_001 Co-authored-by: James Young <18669334+noroadsleft@users.noreply.github.com> Co-authored-by: Nick Brassel <nick@tzarc.org>
2 years ago
Add a lot more data to info.json (#13366) * add some split data to info.json * add tags * add half of config_options.md to info.json * add support for designating master split * sort out split transport and primary * fix bad data in UNUSED_PINS * fixup custom transport * wip * allow for setting split right half keyboard matrix * add SPLIT_USB_DETECT * minor cleanup * fix an erroneous message * rework split.usb_detect * adding missing rgblight vars to info.json * add mouse_key to info.json * add all remaining options from docs/config_options.md * fix audio voices * qmk info: Change text output to use dotted notation * tweak layout output * resolve alias names * break out some functions to make flake8 happy * add a field for bootloader instructions * qmk generate-info-json: add a write-to-file argument Adds an argument that instructs qmk generate-info-json to write the output to a file instead of just to the terminal. * -arg_only, +action Because it was never my intention that one would have to specify a value for the argument that enables writing the file. * Bring qmk generate-info-json inline with other generate commands * pytest fixup * fix esca/getawayvan * fix data driven errors for bpiphany converters * features.force_nkro -> usb.force_nkro * split.primary->split.main * fix esca/getawayvan_f042 * fix the bpiphany converters for real * fix bpiphany/tiger_lily * Apply suggestions from code review Co-authored-by: Nick Brassel <nick@tzarc.org> * fix generate-api errors * fix matrix pin extraction for split boards * fix ploopyco/trackball_nano/rev1_001 Co-authored-by: James Young <18669334+noroadsleft@users.noreply.github.com> Co-authored-by: Nick Brassel <nick@tzarc.org>
2 years ago
  1. """Functions that help us generate and use info.json files.
  2. """
  3. import json
  4. from glob import glob
  5. from pathlib import Path
  6. import jsonschema
  7. from dotty_dict import dotty
  8. from milc import cli
  9. from qmk.constants import CHIBIOS_PROCESSORS, KEYBOARD_OUTPUT_PREFIX, LUFA_PROCESSORS, VUSB_PROCESSORS
  10. from qmk.c_parse import find_layouts
  11. from qmk.json_schema import deep_update, json_load, validate
  12. from qmk.keyboard import config_h, rules_mk
  13. from qmk.keymap import list_keymaps
  14. from qmk.makefile import parse_rules_mk_file
  15. from qmk.math import compute
  16. true_values = ['1', 'on', 'yes']
  17. false_values = ['0', 'off', 'no']
  18. def _valid_community_layout(layout):
  19. """Validate that a declared community list exists
  20. """
  21. return (Path('layouts/default') / layout).exists()
  22. def info_json(keyboard, *, overrides=None):
  23. """Generate the info.json data for a specific keyboard.
  24. """
  25. cur_dir = Path('keyboards')
  26. rules = parse_rules_mk_file(cur_dir / keyboard / 'rules.mk')
  27. if 'DEFAULT_FOLDER' in rules:
  28. keyboard = rules['DEFAULT_FOLDER']
  29. rules = parse_rules_mk_file(cur_dir / keyboard / 'rules.mk', rules)
  30. info_data = {
  31. 'keyboard_name': str(keyboard),
  32. 'keyboard_folder': str(keyboard),
  33. 'keymaps': {},
  34. 'layouts': {},
  35. 'parse_errors': [],
  36. 'parse_warnings': [],
  37. 'maintainer': 'qmk',
  38. }
  39. # Populate the list of JSON keymaps
  40. for keymap in list_keymaps(keyboard, c=False, fullpath=True):
  41. info_data['keymaps'][keymap.name] = {'url': f'https://raw.githubusercontent.com/qmk/qmk_firmware/master/{keymap}/keymap.json'}
  42. # Populate layout data
  43. layouts, aliases = _search_keyboard_h(keyboard)
  44. if aliases:
  45. info_data['layout_aliases'] = aliases
  46. for layout_name, layout_json in layouts.items():
  47. if not layout_name.startswith('LAYOUT_kc'):
  48. layout_json['c_macro'] = True
  49. info_data['layouts'][layout_name] = layout_json
  50. # Merge in the data from info.json, config.h, rules.mk, and overrides
  51. info_data = merge_info_jsons(keyboard, info_data)
  52. info_data = _extract_rules_mk(info_data)
  53. info_data = _extract_config_h(info_data)
  54. if overrides:
  55. info_data = merge_info_data(info_data, overrides)
  56. # Ensure that we have matrix row and column counts
  57. info_data = _matrix_size(info_data)
  58. # Validate against the jsonschema
  59. try:
  60. validate(info_data, 'qmk.api.keyboard.v1')
  61. except jsonschema.ValidationError as e:
  62. json_path = '.'.join([str(p) for p in e.absolute_path])
  63. cli.log.error('Invalid API data: %s: %s: %s', keyboard, json_path, e.message)
  64. exit(1)
  65. # Make sure we have at least one layout
  66. if not info_data.get('layouts'):
  67. _find_missing_layouts(info_data, keyboard)
  68. if not info_data.get('layouts'):
  69. _log_error(info_data, 'No LAYOUTs defined! Need at least one layout defined in the keyboard.h or info.json.')
  70. # Filter out any non-existing community layouts
  71. for layout in info_data.get('community_layouts', []):
  72. if not _valid_community_layout(layout):
  73. # Ignore layout from future checks
  74. info_data['community_layouts'].remove(layout)
  75. _log_error(info_data, 'Claims to support a community layout that does not exist: %s' % (layout))
  76. # Make sure we supply layout macros for the community layouts we claim to support
  77. for layout in info_data.get('community_layouts', []):
  78. layout_name = 'LAYOUT_' + layout
  79. if layout_name not in info_data.get('layouts', {}) and layout_name not in info_data.get('layout_aliases', {}):
  80. _log_error(info_data, 'Claims to support community layout %s but no %s() macro found' % (layout, layout_name))
  81. # Check that the reported matrix size is consistent with the actual matrix size
  82. _check_matrix(info_data)
  83. return info_data
  84. def get_keyboard_overrides(keyboard, keymap=None):
  85. """Checks for keyboard_overrides.json in the keyboard build directory and returns them if it exists.
  86. """
  87. if not keymap:
  88. return None
  89. keyboard_filesafe = keyboard.replace('/', '_')
  90. keymap_filesafe = f'{keyboard_filesafe}_{keymap}' if keymap else keyboard_filesafe
  91. keymap_output = Path(f'{KEYBOARD_OUTPUT_PREFIX}{keymap_filesafe}')
  92. keymap_file = keymap_output / 'keymap.json'
  93. if keymap_file.exists():
  94. keymap_json = json.load(keymap_file.open('r', encoding='utf-8'))
  95. if 'keyboard_overrides' in keymap_json:
  96. return keymap_json['keyboard_overrides']
  97. if 'config' in keymap_json:
  98. return keymap_json['config']
  99. def _extract_features(info_data, rules):
  100. """Find all the features enabled in rules.mk.
  101. """
  102. # Special handling for bootmagic which also supports a "lite" mode.
  103. if rules.get('BOOTMAGIC_ENABLE') == 'lite':
  104. rules['BOOTMAGIC_LITE_ENABLE'] = 'on'
  105. del rules['BOOTMAGIC_ENABLE']
  106. if rules.get('BOOTMAGIC_ENABLE') == 'full':
  107. rules['BOOTMAGIC_ENABLE'] = 'on'
  108. # Skip non-boolean features we haven't implemented special handling for
  109. for feature in 'HAPTIC_ENABLE', 'QWIIC_ENABLE':
  110. if rules.get(feature):
  111. del rules[feature]
  112. # Process the rest of the rules as booleans
  113. for key, value in rules.items():
  114. if key.endswith('_ENABLE'):
  115. key = '_'.join(key.split('_')[:-1]).lower()
  116. value = True if value.lower() in true_values else False if value.lower() in false_values else value
  117. if 'config_h_features' not in info_data:
  118. info_data['config_h_features'] = {}
  119. if 'features' not in info_data:
  120. info_data['features'] = {}
  121. if key in info_data['features']:
  122. _log_warning(info_data, 'Feature %s is specified in both info.json and rules.mk, the rules.mk value wins.' % (key,))
  123. info_data['features'][key] = value
  124. info_data['config_h_features'][key] = value
  125. return info_data
  126. def _pin_name(pin):
  127. """Returns the proper representation for a pin.
  128. """
  129. pin = pin.strip()
  130. if not pin:
  131. return None
  132. elif pin.isdigit():
  133. return int(pin)
  134. elif pin == 'NO_PIN':
  135. return None
  136. return pin
  137. def _extract_pins(pins):
  138. """Returns a list of pins from a comma separated string of pins.
  139. """
  140. return [_pin_name(pin) for pin in pins.split(',')]
  141. def _extract_direct_matrix(direct_pins):
  142. """
  143. """
  144. direct_pin_array = []
  145. while direct_pins[-1] != '}':
  146. direct_pins = direct_pins[:-1]
  147. for row in direct_pins.split('},{'):
  148. if row.startswith('{'):
  149. row = row[1:]
  150. if row.endswith('}'):
  151. row = row[:-1]
  152. direct_pin_array.append([])
  153. for pin in row.split(','):
  154. if pin == 'NO_PIN':
  155. pin = None
  156. direct_pin_array[-1].append(pin)
  157. return direct_pin_array
  158. def _extract_audio(info_data, config_c):
  159. """Populate data about the audio configuration
  160. """
  161. audio_pins = []
  162. for pin in 'B5', 'B6', 'B7', 'C4', 'C5', 'C6':
  163. if config_c.get(f'{pin}_AUDIO'):
  164. audio_pins.append(pin)
  165. if audio_pins:
  166. info_data['audio'] = {'pins': audio_pins}
  167. def _extract_split_main(info_data, config_c):
  168. """Populate data about the split configuration
  169. """
  170. # Figure out how the main half is determined
  171. if config_c.get('SPLIT_HAND_PIN') is True:
  172. if 'split' not in info_data:
  173. info_data['split'] = {}
  174. if 'main' in info_data['split']:
  175. _log_warning(info_data, 'Split main hand is specified in both config.h (SPLIT_HAND_PIN) and info.json (split.main) (Value: %s), the config.h value wins.' % info_data['split']['main'])
  176. info_data['split']['main'] = 'pin'
  177. if config_c.get('SPLIT_HAND_MATRIX_GRID'):
  178. if 'split' not in info_data:
  179. info_data['split'] = {}
  180. if 'main' in info_data['split']:
  181. _log_warning(info_data, 'Split main hand is specified in both config.h (SPLIT_HAND_MATRIX_GRID) and info.json (split.main) (Value: %s), the config.h value wins.' % info_data['split']['main'])
  182. info_data['split']['main'] = 'matrix_grid'
  183. info_data['split']['matrix_grid'] = _extract_pins(config_c['SPLIT_HAND_MATRIX_GRID'])
  184. if config_c.get('EE_HANDS') is True:
  185. if 'split' not in info_data:
  186. info_data['split'] = {}
  187. if 'main' in info_data['split']:
  188. _log_warning(info_data, 'Split main hand is specified in both config.h (EE_HANDS) and info.json (split.main) (Value: %s), the config.h value wins.' % info_data['split']['main'])
  189. info_data['split']['main'] = 'eeprom'
  190. if config_c.get('MASTER_RIGHT') is True:
  191. if 'split' not in info_data:
  192. info_data['split'] = {}
  193. if 'main' in info_data['split']:
  194. _log_warning(info_data, 'Split main hand is specified in both config.h (MASTER_RIGHT) and info.json (split.main) (Value: %s), the config.h value wins.' % info_data['split']['main'])
  195. info_data['split']['main'] = 'right'
  196. if config_c.get('MASTER_LEFT') is True:
  197. if 'split' not in info_data:
  198. info_data['split'] = {}
  199. if 'main' in info_data['split']:
  200. _log_warning(info_data, 'Split main hand is specified in both config.h (MASTER_LEFT) and info.json (split.main) (Value: %s), the config.h value wins.' % info_data['split']['main'])
  201. info_data['split']['main'] = 'left'
  202. def _extract_split_transport(info_data, config_c):
  203. # Figure out the transport method
  204. if config_c.get('USE_I2C') is True:
  205. if 'split' not in info_data:
  206. info_data['split'] = {}
  207. if 'transport' not in info_data['split']:
  208. info_data['split']['transport'] = {}
  209. if 'protocol' in info_data['split']['transport']:
  210. _log_warning(info_data, 'Split transport is specified in both config.h (USE_I2C) and info.json (split.transport.protocol) (Value: %s), the config.h value wins.' % info_data['split']['transport'])
  211. info_data['split']['transport']['protocol'] = 'i2c'
  212. elif 'protocol' not in info_data.get('split', {}).get('transport', {}):
  213. if 'split' not in info_data:
  214. info_data['split'] = {}
  215. if 'transport' not in info_data['split']:
  216. info_data['split']['transport'] = {}
  217. info_data['split']['transport']['protocol'] = 'serial'
  218. def _extract_split_right_pins(info_data, config_c):
  219. # Figure out the right half matrix pins
  220. row_pins = config_c.get('MATRIX_ROW_PINS_RIGHT', '').replace('{', '').replace('}', '').strip()
  221. col_pins = config_c.get('MATRIX_COL_PINS_RIGHT', '').replace('{', '').replace('}', '').strip()
  222. unused_pin_text = config_c.get('UNUSED_PINS_RIGHT')
  223. unused_pins = unused_pin_text.replace('{', '').replace('}', '').strip() if isinstance(unused_pin_text, str) else None
  224. direct_pins = config_c.get('DIRECT_PINS_RIGHT', '').replace(' ', '')[1:-1]
  225. if row_pins and col_pins:
  226. if info_data.get('split', {}).get('matrix_pins', {}).get('right') in info_data:
  227. _log_warning(info_data, 'Right hand matrix data is specified in both info.json and config.h, the config.h values win.')
  228. if 'split' not in info_data:
  229. info_data['split'] = {}
  230. if 'matrix_pins' not in info_data['split']:
  231. info_data['split']['matrix_pins'] = {}
  232. if 'right' not in info_data['split']['matrix_pins']:
  233. info_data['split']['matrix_pins']['right'] = {}
  234. info_data['split']['matrix_pins']['right'] = {
  235. 'cols': _extract_pins(col_pins),
  236. 'rows': _extract_pins(row_pins),
  237. }
  238. if direct_pins:
  239. if info_data.get('split', {}).get('matrix_pins', {}).get('right', {}):
  240. _log_warning(info_data, 'Right hand matrix data is specified in both info.json and config.h, the config.h values win.')
  241. if 'split' not in info_data:
  242. info_data['split'] = {}
  243. if 'matrix_pins' not in info_data['split']:
  244. info_data['split']['matrix_pins'] = {}
  245. if 'right' not in info_data['split']['matrix_pins']:
  246. info_data['split']['matrix_pins']['right'] = {}
  247. info_data['split']['matrix_pins']['right']['direct'] = _extract_direct_matrix(direct_pins)
  248. if unused_pins:
  249. if 'split' not in info_data:
  250. info_data['split'] = {}
  251. if 'matrix_pins' not in info_data['split']:
  252. info_data['split']['matrix_pins'] = {}
  253. if 'right' not in info_data['split']['matrix_pins']:
  254. info_data['split']['matrix_pins']['right'] = {}
  255. info_data['split']['matrix_pins']['right']['unused'] = _extract_pins(unused_pins)
  256. def _extract_matrix_info(info_data, config_c):
  257. """Populate the matrix information.
  258. """
  259. row_pins = config_c.get('MATRIX_ROW_PINS', '').replace('{', '').replace('}', '').strip()
  260. col_pins = config_c.get('MATRIX_COL_PINS', '').replace('{', '').replace('}', '').strip()
  261. unused_pin_text = config_c.get('UNUSED_PINS')
  262. unused_pins = unused_pin_text.replace('{', '').replace('}', '').strip() if isinstance(unused_pin_text, str) else None
  263. direct_pins = config_c.get('DIRECT_PINS', '').replace(' ', '')[1:-1]
  264. info_snippet = {}
  265. if 'MATRIX_ROWS' in config_c and 'MATRIX_COLS' in config_c:
  266. if 'matrix_size' in info_data:
  267. _log_warning(info_data, 'Matrix size is specified in both info.json and config.h, the config.h values win.')
  268. info_data['matrix_size'] = {
  269. 'cols': compute(config_c.get('MATRIX_COLS', '0')),
  270. 'rows': compute(config_c.get('MATRIX_ROWS', '0')),
  271. }
  272. if row_pins and col_pins:
  273. if 'matrix_pins' in info_data and 'cols' in info_data['matrix_pins'] and 'rows' in info_data['matrix_pins']:
  274. _log_warning(info_data, 'Matrix pins are specified in both info.json and config.h, the config.h values win.')
  275. info_snippet['cols'] = _extract_pins(col_pins)
  276. info_snippet['rows'] = _extract_pins(row_pins)
  277. if direct_pins:
  278. if 'matrix_pins' in info_data and 'direct' in info_data['matrix_pins']:
  279. _log_warning(info_data, 'Direct pins are specified in both info.json and config.h, the config.h values win.')
  280. info_snippet['direct'] = _extract_direct_matrix(direct_pins)
  281. if unused_pins:
  282. if 'matrix_pins' not in info_data:
  283. info_data['matrix_pins'] = {}
  284. info_snippet['unused'] = _extract_pins(unused_pins)
  285. if config_c.get('CUSTOM_MATRIX', 'no') != 'no':
  286. if 'matrix_pins' in info_data and 'custom' in info_data['matrix_pins']:
  287. _log_warning(info_data, 'Custom Matrix is specified in both info.json and config.h, the config.h values win.')
  288. info_snippet['custom'] = True
  289. if config_c['CUSTOM_MATRIX'] == 'lite':
  290. info_snippet['custom_lite'] = True
  291. if info_snippet:
  292. info_data['matrix_pins'] = info_snippet
  293. return info_data
  294. def _extract_config_h(info_data):
  295. """Pull some keyboard information from existing config.h files
  296. """
  297. config_c = config_h(info_data['keyboard_folder'])
  298. # Pull in data from the json map
  299. dotty_info = dotty(info_data)
  300. info_config_map = json_load(Path('data/mappings/info_config.json'))
  301. for config_key, info_dict in info_config_map.items():
  302. info_key = info_dict['info_key']
  303. key_type = info_dict.get('value_type', 'str')
  304. try:
  305. if config_key in config_c and info_dict.get('to_json', True):
  306. if dotty_info.get(info_key) and info_dict.get('warn_duplicate', True):
  307. _log_warning(info_data, '%s in config.h is overwriting %s in info.json' % (config_key, info_key))
  308. if key_type.startswith('array'):
  309. if '.' in key_type:
  310. key_type, array_type = key_type.split('.', 1)
  311. else:
  312. array_type = None
  313. config_value = config_c[config_key].replace('{', '').replace('}', '').strip()
  314. if array_type == 'int':
  315. dotty_info[info_key] = list(map(int, config_value.split(',')))
  316. else:
  317. dotty_info[info_key] = config_value.split(',')
  318. elif key_type == 'bool':
  319. dotty_info[info_key] = config_c[config_key] in true_values
  320. elif key_type == 'hex':
  321. dotty_info[info_key] = '0x' + config_c[config_key][2:].upper()
  322. elif key_type == 'list':
  323. dotty_info[info_key] = config_c[config_key].split()
  324. elif key_type == 'int':
  325. dotty_info[info_key] = int(config_c[config_key])
  326. else:
  327. dotty_info[info_key] = config_c[config_key]
  328. except Exception as e:
  329. _log_warning(info_data, f'{config_key}->{info_key}: {e}')
  330. info_data.update(dotty_info)
  331. # Pull data that easily can't be mapped in json
  332. _extract_matrix_info(info_data, config_c)
  333. _extract_audio(info_data, config_c)
  334. _extract_split_main(info_data, config_c)
  335. _extract_split_transport(info_data, config_c)
  336. _extract_split_right_pins(info_data, config_c)
  337. return info_data
  338. def _extract_rules_mk(info_data):
  339. """Pull some keyboard information from existing rules.mk files
  340. """
  341. rules = rules_mk(info_data['keyboard_folder'])
  342. info_data['processor'] = rules.get('MCU', info_data.get('processor', 'atmega32u4'))
  343. if info_data['processor'] in CHIBIOS_PROCESSORS:
  344. arm_processor_rules(info_data, rules)
  345. elif info_data['processor'] in LUFA_PROCESSORS + VUSB_PROCESSORS:
  346. avr_processor_rules(info_data, rules)
  347. else:
  348. cli.log.warning("%s: Unknown MCU: %s" % (info_data['keyboard_folder'], info_data['processor']))
  349. unknown_processor_rules(info_data, rules)
  350. # Pull in data from the json map
  351. dotty_info = dotty(info_data)
  352. info_rules_map = json_load(Path('data/mappings/info_rules.json'))
  353. for rules_key, info_dict in info_rules_map.items():
  354. info_key = info_dict['info_key']
  355. key_type = info_dict.get('value_type', 'str')
  356. try:
  357. if rules_key in rules and info_dict.get('to_json', True):
  358. if dotty_info.get(info_key) and info_dict.get('warn_duplicate', True):
  359. _log_warning(info_data, '%s in rules.mk is overwriting %s in info.json' % (rules_key, info_key))
  360. if key_type.startswith('array'):
  361. if '.' in key_type:
  362. key_type, array_type = key_type.split('.', 1)
  363. else:
  364. array_type = None
  365. rules_value = rules[rules_key].replace('{', '').replace('}', '').strip()
  366. if array_type == 'int':
  367. dotty_info[info_key] = list(map(int, rules_value.split(',')))
  368. else:
  369. dotty_info[info_key] = rules_value.split(',')
  370. elif key_type == 'list':
  371. dotty_info[info_key] = rules[rules_key].split()
  372. elif key_type == 'bool':
  373. dotty_info[info_key] = rules[rules_key] in true_values
  374. elif key_type == 'hex':
  375. dotty_info[info_key] = '0x' + rules[rules_key][2:].upper()
  376. elif key_type == 'int':
  377. dotty_info[info_key] = int(rules[rules_key])
  378. else:
  379. dotty_info[info_key] = rules[rules_key]
  380. except Exception as e:
  381. _log_warning(info_data, f'{rules_key}->{info_key}: {e}')
  382. info_data.update(dotty_info)
  383. # Merge in config values that can't be easily mapped
  384. _extract_features(info_data, rules)
  385. return info_data
  386. def _matrix_size(info_data):
  387. """Add info_data['matrix_size'] if it doesn't exist.
  388. """
  389. if 'matrix_size' not in info_data and 'matrix_pins' in info_data:
  390. info_data['matrix_size'] = {}
  391. if 'direct' in info_data['matrix_pins']:
  392. info_data['matrix_size']['cols'] = len(info_data['matrix_pins']['direct'][0])
  393. info_data['matrix_size']['rows'] = len(info_data['matrix_pins']['direct'])
  394. elif 'cols' in info_data['matrix_pins'] and 'rows' in info_data['matrix_pins']:
  395. info_data['matrix_size']['cols'] = len(info_data['matrix_pins']['cols'])
  396. info_data['matrix_size']['rows'] = len(info_data['matrix_pins']['rows'])
  397. return info_data
  398. def _check_matrix(info_data):
  399. """Check the matrix to ensure that row/column count is consistent.
  400. """
  401. if 'matrix_pins' in info_data and 'matrix_size' in info_data:
  402. actual_col_count = info_data['matrix_size'].get('cols', 0)
  403. actual_row_count = info_data['matrix_size'].get('rows', 0)
  404. col_count = row_count = 0
  405. if 'direct' in info_data['matrix_pins']:
  406. col_count = len(info_data['matrix_pins']['direct'][0])
  407. row_count = len(info_data['matrix_pins']['direct'])
  408. elif 'cols' in info_data['matrix_pins'] and 'rows' in info_data['matrix_pins']:
  409. col_count = len(info_data['matrix_pins']['cols'])
  410. row_count = len(info_data['matrix_pins']['rows'])
  411. if col_count != actual_col_count and col_count != (actual_col_count / 2):
  412. # FIXME: once we can we should detect if split is enabled to do the actual_col_count/2 check.
  413. _log_error(info_data, f'MATRIX_COLS is inconsistent with the size of MATRIX_COL_PINS: {col_count} != {actual_col_count}')
  414. if row_count != actual_row_count and row_count != (actual_row_count / 2):
  415. # FIXME: once we can we should detect if split is enabled to do the actual_row_count/2 check.
  416. _log_error(info_data, f'MATRIX_ROWS is inconsistent with the size of MATRIX_ROW_PINS: {row_count} != {actual_row_count}')
  417. def _search_keyboard_h(keyboard):
  418. keyboard = Path(keyboard)
  419. current_path = Path('keyboards/')
  420. aliases = {}
  421. layouts = {}
  422. for directory in keyboard.parts:
  423. current_path = current_path / directory
  424. keyboard_h = '%s.h' % (directory,)
  425. keyboard_h_path = current_path / keyboard_h
  426. if keyboard_h_path.exists():
  427. new_layouts, new_aliases = find_layouts(keyboard_h_path)
  428. layouts.update(new_layouts)
  429. for alias, alias_text in new_aliases.items():
  430. if alias_text in layouts:
  431. aliases[alias] = alias_text
  432. return layouts, aliases
  433. def _find_missing_layouts(info_data, keyboard):
  434. """Looks for layout macros when they aren't found other places.
  435. If we don't find any layouts from info.json or keyboard.h we widen our search. This is error prone which is why we want to encourage people to follow the standard above.
  436. """
  437. _log_warning(info_data, '%s: Falling back to searching for KEYMAP/LAYOUT macros.' % (keyboard))
  438. for file in glob('keyboards/%s/*.h' % keyboard):
  439. these_layouts, these_aliases = find_layouts(file)
  440. if these_layouts:
  441. for layout_name, layout_json in these_layouts.items():
  442. if not layout_name.startswith('LAYOUT_kc'):
  443. layout_json['c_macro'] = True
  444. info_data['layouts'][layout_name] = layout_json
  445. for alias, alias_text in these_aliases.items():
  446. if alias_text in these_layouts:
  447. if 'layout_aliases' not in info_data:
  448. info_data['layout_aliases'] = {}
  449. info_data['layout_aliases'][alias] = alias_text
  450. def _log_error(info_data, message):
  451. """Send an error message to both JSON and the log.
  452. """
  453. info_data['parse_errors'].append(message)
  454. cli.log.error('%s: %s', info_data.get('keyboard_folder', 'Unknown Keyboard!'), message)
  455. def _log_warning(info_data, message):
  456. """Send a warning message to both JSON and the log.
  457. """
  458. info_data['parse_warnings'].append(message)
  459. cli.log.warning('%s: %s', info_data.get('keyboard_folder', 'Unknown Keyboard!'), message)
  460. def arm_processor_rules(info_data, rules):
  461. """Setup the default info for an ARM board.
  462. """
  463. info_data['processor_type'] = 'arm'
  464. info_data['protocol'] = 'ChibiOS'
  465. if 'bootloader' not in info_data:
  466. if 'STM32' in info_data['processor']:
  467. info_data['bootloader'] = 'stm32-dfu'
  468. else:
  469. info_data['bootloader'] = 'unknown'
  470. if 'STM32' in info_data['processor']:
  471. info_data['platform'] = 'STM32'
  472. elif 'MCU_SERIES' in rules:
  473. info_data['platform'] = rules['MCU_SERIES']
  474. elif 'ARM_ATSAM' in rules:
  475. info_data['platform'] = 'ARM_ATSAM'
  476. return info_data
  477. def avr_processor_rules(info_data, rules):
  478. """Setup the default info for an AVR board.
  479. """
  480. info_data['processor_type'] = 'avr'
  481. info_data['platform'] = rules['ARCH'] if 'ARCH' in rules else 'unknown'
  482. info_data['protocol'] = 'V-USB' if rules.get('MCU') in VUSB_PROCESSORS else 'LUFA'
  483. if 'bootloader' not in info_data:
  484. info_data['bootloader'] = 'atmel-dfu'
  485. # FIXME(fauxpark/anyone): Eventually we should detect the protocol by looking at PROTOCOL inherited from mcu_selection.mk:
  486. # info_data['protocol'] = 'V-USB' if rules.get('PROTOCOL') == 'VUSB' else 'LUFA'
  487. return info_data
  488. def unknown_processor_rules(info_data, rules):
  489. """Setup the default keyboard info for unknown boards.
  490. """
  491. info_data['bootloader'] = 'unknown'
  492. info_data['platform'] = 'unknown'
  493. info_data['processor'] = 'unknown'
  494. info_data['processor_type'] = 'unknown'
  495. info_data['protocol'] = 'unknown'
  496. return info_data
  497. def merge_info_data(info_data, new_info_data):
  498. """Return a merged copy of info_data and new_info_data.
  499. """
  500. if 'layout_aliases' in new_info_data:
  501. info_data['layout_aliases'] = {**info_data.get('layout_aliases', {}), **new_info_data['layout_aliases']}
  502. del new_info_data['layout_aliases']
  503. for layout_name, layout in new_info_data.get('layouts', {}).items():
  504. if layout_name in info_data.get('layout_aliases', {}):
  505. _log_warning(info_data, f"info.json uses alias name {layout_name} instead of {info_data['layout_aliases'][layout_name]}")
  506. layout_name = info_data['layout_aliases'][layout_name]
  507. if layout_name in info_data['layouts']:
  508. if len(info_data['layouts'][layout_name]['layout']) != len(layout['layout']):
  509. msg = '%s: %s: Number of elements in info.json does not match! info.json:%s != %s:%s'
  510. _log_error(info_data, msg % (info_data['keyboard_folder'], layout_name, len(layout['layout']), layout_name, len(info_data['layouts'][layout_name]['layout'])))
  511. else:
  512. for new_key, existing_key in zip(layout['layout'], info_data['layouts'][layout_name]['layout']):
  513. existing_key.update(new_key)
  514. else:
  515. layout['c_macro'] = False
  516. info_data['layouts'][layout_name] = layout
  517. # Update info_data with the new data
  518. if 'layouts' in new_info_data:
  519. del new_info_data['layouts']
  520. deep_update(info_data, new_info_data)
  521. return info_data
  522. def merge_info_jsons(keyboard, info_data):
  523. """Return a merged copy of all the info.json files for a keyboard.
  524. """
  525. for info_file in find_info_json(keyboard):
  526. # Load and validate the JSON data
  527. new_info_data = json_load(info_file)
  528. if not isinstance(new_info_data, dict):
  529. _log_error(info_data, "Invalid file %s, root object should be a dictionary." % (str(info_file),))
  530. continue
  531. try:
  532. validate(new_info_data, 'qmk.keyboard.v1')
  533. except jsonschema.ValidationError as e:
  534. json_path = '.'.join([str(p) for p in e.absolute_path])
  535. cli.log.error('Not including data from file: %s', info_file)
  536. cli.log.error('\t%s: %s', json_path, e.message)
  537. continue
  538. # Merge layout data in
  539. merge_info_data(info_data, new_info_data)
  540. return info_data
  541. def find_info_json(keyboard):
  542. """Finds all the info.json files associated with a keyboard.
  543. """
  544. # Find the most specific first
  545. base_path = Path('keyboards')
  546. keyboard_path = base_path / keyboard
  547. keyboard_parent = keyboard_path.parent
  548. info_jsons = [keyboard_path / 'info.json']
  549. # Add DEFAULT_FOLDER before parents, if present
  550. rules = rules_mk(keyboard)
  551. if 'DEFAULT_FOLDER' in rules:
  552. info_jsons.append(Path(rules['DEFAULT_FOLDER']) / 'info.json')
  553. # Add in parent folders for least specific
  554. for _ in range(5):
  555. info_jsons.append(keyboard_parent / 'info.json')
  556. if keyboard_parent.parent == base_path:
  557. break
  558. keyboard_parent = keyboard_parent.parent
  559. # Return a list of the info.json files that actually exist
  560. return [info_json for info_json in info_jsons if info_json.exists()]