info.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. """Functions that help us generate and use info.json files.
  2. """
  3. from glob import glob
  4. from pathlib import Path
  5. import jsonschema
  6. from dotty_dict import dotty
  7. from milc import cli
  8. from qmk.constants import CHIBIOS_PROCESSORS, LUFA_PROCESSORS, VUSB_PROCESSORS
  9. from qmk.c_parse import find_layouts
  10. from qmk.json_schema import deep_update, json_load, keyboard_validate, keyboard_api_validate
  11. from qmk.keyboard import config_h, rules_mk
  12. from qmk.keymap import list_keymaps
  13. from qmk.makefile import parse_rules_mk_file
  14. from qmk.math import compute
  15. true_values = ['1', 'on', 'yes']
  16. false_values = ['0', 'off', 'no']
  17. def info_json(keyboard):
  18. """Generate the info.json data for a specific keyboard.
  19. """
  20. cur_dir = Path('keyboards')
  21. rules = parse_rules_mk_file(cur_dir / keyboard / 'rules.mk')
  22. if 'DEFAULT_FOLDER' in rules:
  23. keyboard = rules['DEFAULT_FOLDER']
  24. rules = parse_rules_mk_file(cur_dir / keyboard / 'rules.mk', rules)
  25. info_data = {
  26. 'keyboard_name': str(keyboard),
  27. 'keyboard_folder': str(keyboard),
  28. 'keymaps': {},
  29. 'layouts': {},
  30. 'parse_errors': [],
  31. 'parse_warnings': [],
  32. 'maintainer': 'qmk',
  33. }
  34. # Populate the list of JSON keymaps
  35. for keymap in list_keymaps(keyboard, c=False, fullpath=True):
  36. info_data['keymaps'][keymap.name] = {'url': f'https://raw.githubusercontent.com/qmk/qmk_firmware/master/{keymap}/keymap.json'}
  37. # Populate layout data
  38. layouts, aliases = _find_all_layouts(info_data, keyboard)
  39. if aliases:
  40. info_data['layout_aliases'] = aliases
  41. for layout_name, layout_json in layouts.items():
  42. if not layout_name.startswith('LAYOUT_kc'):
  43. layout_json['c_macro'] = True
  44. info_data['layouts'][layout_name] = layout_json
  45. # Merge in the data from info.json, config.h, and rules.mk
  46. info_data = merge_info_jsons(keyboard, info_data)
  47. info_data = _extract_config_h(info_data)
  48. info_data = _extract_rules_mk(info_data)
  49. # Validate against the jsonschema
  50. try:
  51. keyboard_api_validate(info_data)
  52. except jsonschema.ValidationError as e:
  53. json_path = '.'.join([str(p) for p in e.absolute_path])
  54. cli.log.error('Invalid API data: %s: %s: %s', keyboard, json_path, e.message)
  55. exit()
  56. # Make sure we have at least one layout
  57. if not info_data.get('layouts'):
  58. _log_error(info_data, 'No LAYOUTs defined! Need at least one layout defined in the keyboard.h or info.json.')
  59. # Make sure we supply layout macros for the community layouts we claim to support
  60. for layout in info_data.get('community_layouts', []):
  61. layout_name = 'LAYOUT_' + layout
  62. if layout_name not in info_data.get('layouts', {}) and layout_name not in info_data.get('layout_aliases', {}):
  63. _log_error(info_data, 'Claims to support community layout %s but no %s() macro found' % (layout, layout_name))
  64. return info_data
  65. def _extract_features(info_data, rules):
  66. """Find all the features enabled in rules.mk.
  67. """
  68. # Special handling for bootmagic which also supports a "lite" mode.
  69. if rules.get('BOOTMAGIC_ENABLE') == 'lite':
  70. rules['BOOTMAGIC_LITE_ENABLE'] = 'on'
  71. del rules['BOOTMAGIC_ENABLE']
  72. if rules.get('BOOTMAGIC_ENABLE') == 'full':
  73. rules['BOOTMAGIC_ENABLE'] = 'on'
  74. # Skip non-boolean features we haven't implemented special handling for
  75. for feature in 'HAPTIC_ENABLE', 'QWIIC_ENABLE':
  76. if rules.get(feature):
  77. del rules[feature]
  78. # Process the rest of the rules as booleans
  79. for key, value in rules.items():
  80. if key.endswith('_ENABLE'):
  81. key = '_'.join(key.split('_')[:-1]).lower()
  82. value = True if value.lower() in true_values else False if value.lower() in false_values else value
  83. if 'config_h_features' not in info_data:
  84. info_data['config_h_features'] = {}
  85. if 'features' not in info_data:
  86. info_data['features'] = {}
  87. if key in info_data['features']:
  88. _log_warning(info_data, 'Feature %s is specified in both info.json and rules.mk, the rules.mk value wins.' % (key,))
  89. info_data['features'][key] = value
  90. info_data['config_h_features'][key] = value
  91. return info_data
  92. def _pin_name(pin):
  93. """Returns the proper representation for a pin.
  94. """
  95. pin = pin.strip()
  96. if not pin:
  97. return None
  98. elif pin.isdigit():
  99. return int(pin)
  100. elif pin == 'NO_PIN':
  101. return None
  102. elif pin[0] in 'ABCDEFGHIJK' and pin[1].isdigit():
  103. return pin
  104. raise ValueError(f'Invalid pin: {pin}')
  105. def _extract_pins(pins):
  106. """Returns a list of pins from a comma separated string of pins.
  107. """
  108. return [_pin_name(pin) for pin in pins.split(',')]
  109. def _extract_direct_matrix(info_data, direct_pins):
  110. """
  111. """
  112. info_data['matrix_pins'] = {}
  113. direct_pin_array = []
  114. while direct_pins[-1] != '}':
  115. direct_pins = direct_pins[:-1]
  116. for row in direct_pins.split('},{'):
  117. if row.startswith('{'):
  118. row = row[1:]
  119. if row.endswith('}'):
  120. row = row[:-1]
  121. direct_pin_array.append([])
  122. for pin in row.split(','):
  123. if pin == 'NO_PIN':
  124. pin = None
  125. direct_pin_array[-1].append(pin)
  126. return direct_pin_array
  127. def _extract_matrix_info(info_data, config_c):
  128. """Populate the matrix information.
  129. """
  130. row_pins = config_c.get('MATRIX_ROW_PINS', '').replace('{', '').replace('}', '').strip()
  131. col_pins = config_c.get('MATRIX_COL_PINS', '').replace('{', '').replace('}', '').strip()
  132. direct_pins = config_c.get('DIRECT_PINS', '').replace(' ', '')[1:-1]
  133. if 'MATRIX_ROWS' in config_c and 'MATRIX_COLS' in config_c:
  134. if 'matrix_size' in info_data:
  135. _log_warning(info_data, 'Matrix size is specified in both info.json and config.h, the config.h values win.')
  136. info_data['matrix_size'] = {
  137. 'cols': compute(config_c.get('MATRIX_COLS', '0')),
  138. 'rows': compute(config_c.get('MATRIX_ROWS', '0')),
  139. }
  140. if row_pins and col_pins:
  141. if 'matrix_pins' in info_data:
  142. _log_warning(info_data, 'Matrix pins are specified in both info.json and config.h, the config.h values win.')
  143. info_data['matrix_pins'] = {
  144. 'cols': _extract_pins(col_pins),
  145. 'rows': _extract_pins(row_pins),
  146. }
  147. if direct_pins:
  148. if 'matrix_pins' in info_data:
  149. _log_warning(info_data, 'Direct pins are specified in both info.json and config.h, the config.h values win.')
  150. info_data['matrix_pins']['direct'] = _extract_direct_matrix(info_data, direct_pins)
  151. return info_data
  152. def _extract_config_h(info_data):
  153. """Pull some keyboard information from existing config.h files
  154. """
  155. config_c = config_h(info_data['keyboard_folder'])
  156. # Pull in data from the json map
  157. dotty_info = dotty(info_data)
  158. info_config_map = json_load(Path('data/mappings/info_config.json'))
  159. for config_key, info_dict in info_config_map.items():
  160. info_key = info_dict['info_key']
  161. key_type = info_dict.get('value_type', 'str')
  162. try:
  163. if config_key in config_c and info_dict.get('to_json', True):
  164. if dotty_info.get(info_key) and info_dict.get('warn_duplicate', True):
  165. _log_warning(info_data, '%s in config.h is overwriting %s in info.json' % (config_key, info_key))
  166. if key_type.startswith('array'):
  167. if '.' in key_type:
  168. key_type, array_type = key_type.split('.', 1)
  169. else:
  170. array_type = None
  171. config_value = config_c[config_key].replace('{', '').replace('}', '').strip()
  172. if array_type == 'int':
  173. dotty_info[info_key] = list(map(int, config_value.split(',')))
  174. else:
  175. dotty_info[info_key] = config_value.split(',')
  176. elif key_type == 'bool':
  177. dotty_info[info_key] = config_c[config_key] in true_values
  178. elif key_type == 'hex':
  179. dotty_info[info_key] = '0x' + config_c[config_key][2:].upper()
  180. elif key_type == 'list':
  181. dotty_info[info_key] = config_c[config_key].split()
  182. elif key_type == 'int':
  183. dotty_info[info_key] = int(config_c[config_key])
  184. else:
  185. dotty_info[info_key] = config_c[config_key]
  186. except Exception as e:
  187. _log_warning(info_data, f'{config_key}->{info_key}: {e}')
  188. info_data.update(dotty_info)
  189. # Pull data that easily can't be mapped in json
  190. _extract_matrix_info(info_data, config_c)
  191. return info_data
  192. def _extract_rules_mk(info_data):
  193. """Pull some keyboard information from existing rules.mk files
  194. """
  195. rules = rules_mk(info_data['keyboard_folder'])
  196. info_data['processor'] = rules.get('MCU', info_data.get('processor', 'atmega32u4'))
  197. if info_data['processor'] in CHIBIOS_PROCESSORS:
  198. arm_processor_rules(info_data, rules)
  199. elif info_data['processor'] in LUFA_PROCESSORS + VUSB_PROCESSORS:
  200. avr_processor_rules(info_data, rules)
  201. else:
  202. cli.log.warning("%s: Unknown MCU: %s" % (info_data['keyboard_folder'], info_data['processor']))
  203. unknown_processor_rules(info_data, rules)
  204. # Pull in data from the json map
  205. dotty_info = dotty(info_data)
  206. info_rules_map = json_load(Path('data/mappings/info_rules.json'))
  207. for rules_key, info_dict in info_rules_map.items():
  208. info_key = info_dict['info_key']
  209. key_type = info_dict.get('value_type', 'str')
  210. try:
  211. if rules_key in rules and info_dict.get('to_json', True):
  212. if dotty_info.get(info_key) and info_dict.get('warn_duplicate', True):
  213. _log_warning(info_data, '%s in rules.mk is overwriting %s in info.json' % (rules_key, info_key))
  214. if key_type.startswith('array'):
  215. if '.' in key_type:
  216. key_type, array_type = key_type.split('.', 1)
  217. else:
  218. array_type = None
  219. rules_value = rules[rules_key].replace('{', '').replace('}', '').strip()
  220. if array_type == 'int':
  221. dotty_info[info_key] = list(map(int, rules_value.split(',')))
  222. else:
  223. dotty_info[info_key] = rules_value.split(',')
  224. elif key_type == 'list':
  225. dotty_info[info_key] = rules[rules_key].split()
  226. elif key_type == 'bool':
  227. dotty_info[info_key] = rules[rules_key] in true_values
  228. elif key_type == 'hex':
  229. dotty_info[info_key] = '0x' + rules[rules_key][2:].upper()
  230. elif key_type == 'int':
  231. dotty_info[info_key] = int(rules[rules_key])
  232. else:
  233. dotty_info[info_key] = rules[rules_key]
  234. except Exception as e:
  235. _log_warning(info_data, f'{rules_key}->{info_key}: {e}')
  236. info_data.update(dotty_info)
  237. # Merge in config values that can't be easily mapped
  238. _extract_features(info_data, rules)
  239. return info_data
  240. def _merge_layouts(info_data, new_info_data):
  241. """Merge new_info_data into info_data in an intelligent way.
  242. """
  243. for layout_name, layout_json in new_info_data['layouts'].items():
  244. if layout_name in info_data['layouts']:
  245. # Pull in layouts we have a macro for
  246. if len(info_data['layouts'][layout_name]['layout']) != len(layout_json['layout']):
  247. msg = '%s: %s: Number of elements in info.json does not match! info.json:%s != %s:%s'
  248. _log_error(info_data, msg % (info_data['keyboard_folder'], layout_name, len(layout_json['layout']), layout_name, len(info_data['layouts'][layout_name]['layout'])))
  249. else:
  250. for i, key in enumerate(info_data['layouts'][layout_name]['layout']):
  251. key.update(layout_json['layout'][i])
  252. else:
  253. # Pull in layouts that have matrix data
  254. missing_matrix = False
  255. for key in layout_json.get('layout', {}):
  256. if 'matrix' not in key:
  257. missing_matrix = True
  258. if not missing_matrix:
  259. if layout_name in info_data['layouts']:
  260. # Update an existing layout with new data
  261. for i, key in enumerate(info_data['layouts'][layout_name]['layout']):
  262. key.update(layout_json['layout'][i])
  263. else:
  264. # Copy in the new layout wholesale
  265. layout_json['c_macro'] = False
  266. info_data['layouts'][layout_name] = layout_json
  267. return info_data
  268. def _search_keyboard_h(path):
  269. current_path = Path('keyboards/')
  270. aliases = {}
  271. layouts = {}
  272. for directory in path.parts:
  273. current_path = current_path / directory
  274. keyboard_h = '%s.h' % (directory,)
  275. keyboard_h_path = current_path / keyboard_h
  276. if keyboard_h_path.exists():
  277. new_layouts, new_aliases = find_layouts(keyboard_h_path)
  278. layouts.update(new_layouts)
  279. for alias, alias_text in new_aliases.items():
  280. if alias_text in layouts:
  281. aliases[alias] = alias_text
  282. return layouts, aliases
  283. def _find_all_layouts(info_data, keyboard):
  284. """Looks for layout macros associated with this keyboard.
  285. """
  286. layouts, aliases = _search_keyboard_h(Path(keyboard))
  287. if not layouts:
  288. # 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.
  289. info_data['parse_warnings'].append('%s: Falling back to searching for KEYMAP/LAYOUT macros.' % (keyboard))
  290. for file in glob('keyboards/%s/*.h' % keyboard):
  291. if file.endswith('.h'):
  292. these_layouts, these_aliases = find_layouts(file)
  293. if these_layouts:
  294. layouts.update(these_layouts)
  295. for alias, alias_text in these_aliases.items():
  296. if alias_text in layouts:
  297. aliases[alias] = alias_text
  298. return layouts, aliases
  299. def _log_error(info_data, message):
  300. """Send an error message to both JSON and the log.
  301. """
  302. info_data['parse_errors'].append(message)
  303. cli.log.error('%s: %s', info_data.get('keyboard_folder', 'Unknown Keyboard!'), message)
  304. def _log_warning(info_data, message):
  305. """Send a warning message to both JSON and the log.
  306. """
  307. info_data['parse_warnings'].append(message)
  308. cli.log.warning('%s: %s', info_data.get('keyboard_folder', 'Unknown Keyboard!'), message)
  309. def arm_processor_rules(info_data, rules):
  310. """Setup the default info for an ARM board.
  311. """
  312. info_data['processor_type'] = 'arm'
  313. info_data['protocol'] = 'ChibiOS'
  314. if 'bootloader' not in info_data:
  315. if 'STM32' in info_data['processor']:
  316. info_data['bootloader'] = 'stm32-dfu'
  317. else:
  318. info_data['bootloader'] = 'unknown'
  319. if 'STM32' in info_data['processor']:
  320. info_data['platform'] = 'STM32'
  321. elif 'MCU_SERIES' in rules:
  322. info_data['platform'] = rules['MCU_SERIES']
  323. elif 'ARM_ATSAM' in rules:
  324. info_data['platform'] = 'ARM_ATSAM'
  325. return info_data
  326. def avr_processor_rules(info_data, rules):
  327. """Setup the default info for an AVR board.
  328. """
  329. info_data['processor_type'] = 'avr'
  330. info_data['platform'] = rules['ARCH'] if 'ARCH' in rules else 'unknown'
  331. info_data['protocol'] = 'V-USB' if rules.get('MCU') in VUSB_PROCESSORS else 'LUFA'
  332. if 'bootloader' not in info_data:
  333. info_data['bootloader'] = 'atmel-dfu'
  334. # FIXME(fauxpark/anyone): Eventually we should detect the protocol by looking at PROTOCOL inherited from mcu_selection.mk:
  335. # info_data['protocol'] = 'V-USB' if rules.get('PROTOCOL') == 'VUSB' else 'LUFA'
  336. return info_data
  337. def unknown_processor_rules(info_data, rules):
  338. """Setup the default keyboard info for unknown boards.
  339. """
  340. info_data['bootloader'] = 'unknown'
  341. info_data['platform'] = 'unknown'
  342. info_data['processor'] = 'unknown'
  343. info_data['processor_type'] = 'unknown'
  344. info_data['protocol'] = 'unknown'
  345. return info_data
  346. def merge_info_jsons(keyboard, info_data):
  347. """Return a merged copy of all the info.json files for a keyboard.
  348. """
  349. for info_file in find_info_json(keyboard):
  350. # Load and validate the JSON data
  351. new_info_data = json_load(info_file)
  352. if not isinstance(new_info_data, dict):
  353. _log_error(info_data, "Invalid file %s, root object should be a dictionary." % (str(info_file),))
  354. continue
  355. try:
  356. keyboard_validate(new_info_data)
  357. except jsonschema.ValidationError as e:
  358. json_path = '.'.join([str(p) for p in e.absolute_path])
  359. cli.log.error('Not including data from file: %s', info_file)
  360. cli.log.error('\t%s: %s', json_path, e.message)
  361. continue
  362. # Merge layout data in
  363. if 'layout_aliases' in new_info_data:
  364. info_data['layout_aliases'] = {**info_data.get('layout_aliases', {}), **new_info_data['layout_aliases']}
  365. del new_info_data['layout_aliases']
  366. for layout_name, layout in new_info_data.get('layouts', {}).items():
  367. if layout_name in info_data.get('layout_aliases', {}):
  368. _log_warning(info_data, f"info.json uses alias name {layout_name} instead of {info_data['layout_aliases'][layout_name]}")
  369. layout_name = info_data['layout_aliases'][layout_name]
  370. if layout_name in info_data['layouts']:
  371. for new_key, existing_key in zip(layout['layout'], info_data['layouts'][layout_name]['layout']):
  372. existing_key.update(new_key)
  373. else:
  374. layout['c_macro'] = False
  375. info_data['layouts'][layout_name] = layout
  376. # Update info_data with the new data
  377. if 'layouts' in new_info_data:
  378. del new_info_data['layouts']
  379. deep_update(info_data, new_info_data)
  380. return info_data
  381. def find_info_json(keyboard):
  382. """Finds all the info.json files associated with a keyboard.
  383. """
  384. # Find the most specific first
  385. base_path = Path('keyboards')
  386. keyboard_path = base_path / keyboard
  387. keyboard_parent = keyboard_path.parent
  388. info_jsons = [keyboard_path / 'info.json']
  389. # Add DEFAULT_FOLDER before parents, if present
  390. rules = rules_mk(keyboard)
  391. if 'DEFAULT_FOLDER' in rules:
  392. info_jsons.append(Path(rules['DEFAULT_FOLDER']) / 'info.json')
  393. # Add in parent folders for least specific
  394. for _ in range(5):
  395. info_jsons.append(keyboard_parent / 'info.json')
  396. if keyboard_parent.parent == base_path:
  397. break
  398. keyboard_parent = keyboard_parent.parent
  399. # Return a list of the info.json files that actually exist
  400. return [info_json for info_json in info_jsons if info_json.exists()]