info.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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. from milc import cli
  7. from qmk.constants import CHIBIOS_PROCESSORS, LUFA_PROCESSORS, VUSB_PROCESSORS, LED_INDICATORS
  8. from qmk.c_parse import find_layouts
  9. from qmk.keyboard import config_h, rules_mk
  10. from qmk.keymap import list_keymaps
  11. from qmk.makefile import parse_rules_mk_file
  12. from qmk.math import compute
  13. rgblight_properties = {
  14. 'led_count': 'RGBLED_NUM',
  15. 'pin': 'RGB_DI_PIN',
  16. 'split_count': 'RGBLED_SPLIT',
  17. 'max_brightness': 'RGBLIGHT_LIMIT_VAL',
  18. 'hue_steps': 'RGBLIGHT_HUE_STEP',
  19. 'saturation_steps': 'RGBLIGHT_SAT_STEP',
  20. 'brightness_steps': 'RGBLIGHT_VAL_STEP'
  21. }
  22. rgblight_toggles = {
  23. 'sleep': 'RGBLIGHT_SLEEP',
  24. 'split': 'RGBLIGHT_SPLIT',
  25. }
  26. rgblight_animations = {
  27. 'all': 'RGBLIGHT_ANIMATIONS',
  28. 'alternating': 'RGBLIGHT_EFFECT_ALTERNATING',
  29. 'breathing': 'RGBLIGHT_EFFECT_BREATHING',
  30. 'christmas': 'RGBLIGHT_EFFECT_CHRISTMAS',
  31. 'knight': 'RGBLIGHT_EFFECT_KNIGHT',
  32. 'rainbow_mood': 'RGBLIGHT_EFFECT_RAINBOW_MOOD',
  33. 'rainbow_swirl': 'RGBLIGHT_EFFECT_RAINBOW_SWIRL',
  34. 'rgb_test': 'RGBLIGHT_EFFECT_RGB_TEST',
  35. 'snake': 'RGBLIGHT_EFFECT_SNAKE',
  36. 'static_gradient': 'RGBLIGHT_EFFECT_STATIC_GRADIENT',
  37. 'twinkle': 'RGBLIGHT_EFFECT_TWINKLE'
  38. }
  39. true_values = ['1', 'on', 'yes']
  40. false_values = ['0', 'off', 'no']
  41. def info_json(keyboard):
  42. """Generate the info.json data for a specific keyboard.
  43. """
  44. cur_dir = Path('keyboards')
  45. rules = parse_rules_mk_file(cur_dir / keyboard / 'rules.mk')
  46. if 'DEFAULT_FOLDER' in rules:
  47. keyboard = rules['DEFAULT_FOLDER']
  48. rules = parse_rules_mk_file(cur_dir / keyboard / 'rules.mk', rules)
  49. info_data = {
  50. 'keyboard_name': str(keyboard),
  51. 'keyboard_folder': str(keyboard),
  52. 'keymaps': {},
  53. 'layouts': {},
  54. 'parse_errors': [],
  55. 'parse_warnings': [],
  56. 'maintainer': 'qmk',
  57. }
  58. # Populate the list of JSON keymaps
  59. for keymap in list_keymaps(keyboard, c=False, fullpath=True):
  60. info_data['keymaps'][keymap.name] = {'url': f'https://raw.githubusercontent.com/qmk/qmk_firmware/master/{keymap}/keymap.json'}
  61. # Populate layout data
  62. for layout_name, layout_json in _find_all_layouts(info_data, keyboard).items():
  63. if not layout_name.startswith('LAYOUT_kc'):
  64. layout_json['c_macro'] = True
  65. info_data['layouts'][layout_name] = layout_json
  66. # Merge in the data from info.json, config.h, and rules.mk
  67. info_data = merge_info_jsons(keyboard, info_data)
  68. info_data = _extract_config_h(info_data)
  69. info_data = _extract_rules_mk(info_data)
  70. # Make sure we have at least one layout
  71. if not info_data.get('layouts'):
  72. _log_error(info_data, 'No LAYOUTs defined! Need at least one layout defined in the keyboard.h or info.json.')
  73. # Make sure we supply layout macros for the community layouts we claim to support
  74. # FIXME(skullydazed): This should be populated into info.json and read from there instead
  75. if 'LAYOUTS' in rules and info_data.get('layouts'):
  76. # Match these up against the supplied layouts
  77. supported_layouts = rules['LAYOUTS'].strip().split()
  78. for layout_name in sorted(info_data['layouts']):
  79. layout_name = layout_name[7:]
  80. if layout_name in supported_layouts:
  81. supported_layouts.remove(layout_name)
  82. if supported_layouts:
  83. for supported_layout in supported_layouts:
  84. _log_error(info_data, 'Claims to support community layout %s but no LAYOUT_%s() macro found' % (supported_layout, supported_layout))
  85. return info_data
  86. def _extract_debounce(info_data, config_c):
  87. """Handle debounce.
  88. """
  89. if 'debounce' in info_data and 'DEBOUNCE' in config_c:
  90. _log_warning(info_data, 'Debounce is specified in both info.json and config.h, the config.h value wins.')
  91. if 'DEBOUNCE' in config_c:
  92. info_data['debounce'] = config_c.get('DEBOUNCE')
  93. return info_data
  94. def _extract_diode_direction(info_data, config_c):
  95. """Handle the diode direction.
  96. """
  97. if 'diode_direction' in info_data and 'DIODE_DIRECTION' in config_c:
  98. _log_warning(info_data, 'Diode direction is specified in both info.json and config.h, the config.h value wins.')
  99. if 'DIODE_DIRECTION' in config_c:
  100. info_data['diode_direction'] = config_c.get('DIODE_DIRECTION')
  101. return info_data
  102. def _extract_indicators(info_data, config_c):
  103. """Find the LED indicator information.
  104. """
  105. for json_key, config_key in LED_INDICATORS.items():
  106. if json_key in info_data.get('indicators', []) and config_key in config_c:
  107. _log_warning(info_data, f'Indicator {json_key} is specified in both info.json and config.h, the config.h value wins.')
  108. if config_key in config_c:
  109. info_data['indicators'][json_key] = config_c.get(config_key)
  110. return info_data
  111. def _extract_community_layouts(info_data, rules):
  112. """Find the community layouts in rules.mk.
  113. """
  114. community_layouts = rules['LAYOUTS'].split() if 'LAYOUTS' in rules else []
  115. if 'community_layouts' in info_data:
  116. for layout in community_layouts:
  117. if layout not in info_data['community_layouts']:
  118. community_layouts.append(layout)
  119. else:
  120. info_data['community_layouts'] = community_layouts
  121. return info_data
  122. def _extract_features(info_data, rules):
  123. """Find all the features enabled in rules.mk.
  124. """
  125. for key, value in rules.items():
  126. if key.endswith('_ENABLE'):
  127. key = '_'.join(key.split('_')[:-1]).lower()
  128. value = True if value in true_values else False if value in false_values else value
  129. if 'config_h_features' not in info_data:
  130. info_data['config_h_features'] = {}
  131. if 'features' not in info_data:
  132. info_data['features'] = {}
  133. if key in info_data['features']:
  134. _log_warning(info_data, 'Feature %s is specified in both info.json and rules.mk, the rules.mk value wins.' % (key,))
  135. info_data['features'][key] = value
  136. info_data['config_h_features'][key] = value
  137. return info_data
  138. def _extract_rgblight(info_data, config_c):
  139. """Handle the rgblight configuration
  140. """
  141. rgblight = info_data.get('rgblight', {})
  142. animations = rgblight.get('animations', {})
  143. for json_key, config_key in rgblight_properties.items():
  144. if config_key in config_c:
  145. if json_key in rgblight:
  146. _log_warning(info_data, 'RGB Light: %s is specified in both info.json and config.h, the config.h value wins.' % (json_key,))
  147. rgblight[json_key] = config_c[config_key]
  148. for json_key, config_key in rgblight_toggles.items():
  149. if config_key in config_c:
  150. if json_key in rgblight:
  151. _log_warning(info_data, 'RGB Light: %s is specified in both info.json and config.h, the config.h value wins.', json_key)
  152. rgblight[json_key] = config_c[config_key]
  153. for json_key, config_key in rgblight_animations.items():
  154. if config_key in config_c:
  155. if json_key in animations:
  156. _log_warning(info_data, 'RGB Light: animations: %s is specified in both info.json and config.h, the config.h value wins.' % (json_key,))
  157. animations[json_key] = config_c[config_key]
  158. if animations:
  159. rgblight['animations'] = animations
  160. if rgblight:
  161. info_data['rgblight'] = rgblight
  162. return info_data
  163. def _extract_matrix_info(info_data, config_c):
  164. """Populate the matrix information.
  165. """
  166. row_pins = config_c.get('MATRIX_ROW_PINS', '').replace('{', '').replace('}', '').strip()
  167. col_pins = config_c.get('MATRIX_COL_PINS', '').replace('{', '').replace('}', '').strip()
  168. direct_pins = config_c.get('DIRECT_PINS', '').replace(' ', '')[1:-1]
  169. if 'MATRIX_ROWS' in config_c and 'MATRIX_COLS' in config_c:
  170. if 'matrix_size' in info_data:
  171. _log_warning(info_data, 'Matrix size is specified in both info.json and config.h, the config.h values win.')
  172. info_data['matrix_size'] = {
  173. 'rows': compute(config_c.get('MATRIX_ROWS', '0')),
  174. 'cols': compute(config_c.get('MATRIX_COLS', '0')),
  175. }
  176. if row_pins and col_pins:
  177. if 'matrix_pins' in info_data:
  178. _log_warning(info_data, 'Matrix pins are specified in both info.json and config.h, the config.h values win.')
  179. info_data['matrix_pins'] = {}
  180. if row_pins:
  181. info_data['matrix_pins']['rows'] = row_pins.split(',')
  182. if col_pins:
  183. info_data['matrix_pins']['cols'] = col_pins.split(',')
  184. if direct_pins:
  185. if 'matrix_pins' in info_data:
  186. _log_warning(info_data, 'Direct pins are specified in both info.json and config.h, the config.h values win.')
  187. info_data['matrix_pins'] = {}
  188. direct_pin_array = []
  189. for row in direct_pins.split('},{'):
  190. if row.startswith('{'):
  191. row = row[1:]
  192. if row.endswith('}'):
  193. row = row[:-1]
  194. direct_pin_array.append([])
  195. for pin in row.split(','):
  196. if pin == 'NO_PIN':
  197. pin = None
  198. direct_pin_array[-1].append(pin)
  199. info_data['matrix_pins']['direct'] = direct_pin_array
  200. return info_data
  201. def _extract_usb_info(info_data, config_c):
  202. """Populate the USB information.
  203. """
  204. usb_properties = {'vid': 'VENDOR_ID', 'pid': 'PRODUCT_ID', 'device_ver': 'DEVICE_VER'}
  205. if 'usb' not in info_data:
  206. info_data['usb'] = {}
  207. for info_name, config_name in usb_properties.items():
  208. if config_name in config_c:
  209. if info_name in info_data['usb']:
  210. _log_warning(info_data, '%s in config.h is overwriting usb.%s in info.json' % (config_name, info_name))
  211. info_data['usb'][info_name] = config_c[config_name]
  212. elif info_name not in info_data['usb']:
  213. _log_error(info_data, '%s not specified in config.h, and %s not specified in info.json. One is required.' % (config_name, info_name))
  214. return info_data
  215. def _extract_config_h(info_data):
  216. """Pull some keyboard information from existing config.h files
  217. """
  218. config_c = config_h(info_data['keyboard_folder'])
  219. _extract_debounce(info_data, config_c)
  220. _extract_diode_direction(info_data, config_c)
  221. _extract_indicators(info_data, config_c)
  222. _extract_matrix_info(info_data, config_c)
  223. _extract_usb_info(info_data, config_c)
  224. _extract_rgblight(info_data, config_c)
  225. return info_data
  226. def _extract_rules_mk(info_data):
  227. """Pull some keyboard information from existing rules.mk files
  228. """
  229. rules = rules_mk(info_data['keyboard_folder'])
  230. mcu = rules.get('MCU')
  231. if mcu in CHIBIOS_PROCESSORS:
  232. arm_processor_rules(info_data, rules)
  233. elif mcu in LUFA_PROCESSORS + VUSB_PROCESSORS:
  234. avr_processor_rules(info_data, rules)
  235. else:
  236. cli.log.warning("%s: Unknown MCU: %s" % (info_data['keyboard_folder'], mcu))
  237. unknown_processor_rules(info_data, rules)
  238. _extract_community_layouts(info_data, rules)
  239. _extract_features(info_data, rules)
  240. return info_data
  241. def _merge_layouts(info_data, new_info_data):
  242. """Merge new_info_data into info_data in an intelligent way.
  243. """
  244. for layout_name, layout_json in new_info_data['layouts'].items():
  245. if layout_name in info_data['layouts']:
  246. # Pull in layouts we have a macro for
  247. if len(info_data['layouts'][layout_name]['layout']) != len(layout_json['layout']):
  248. msg = '%s: %s: Number of elements in info.json does not match! info.json:%s != %s:%s'
  249. _log_error(info_data, msg % (info_data['keyboard_folder'], layout_name, len(layout_json['layout']), layout_name, len(info_data['layouts'][layout_name]['layout'])))
  250. else:
  251. for i, key in enumerate(info_data['layouts'][layout_name]['layout']):
  252. key.update(layout_json['layout'][i])
  253. else:
  254. # Pull in layouts that have matrix data
  255. missing_matrix = False
  256. for key in layout_json['layout']:
  257. if 'matrix' not in key:
  258. missing_matrix = True
  259. if not missing_matrix:
  260. if layout_name in info_data['layouts']:
  261. # Update an existing layout with new data
  262. for i, key in enumerate(info_data['layouts'][layout_name]['layout']):
  263. key.update(layout_json['layout'][i])
  264. else:
  265. # Copy in the new layout wholesale
  266. layout_json['c_macro'] = False
  267. info_data['layouts'][layout_name] = layout_json
  268. return info_data
  269. def _search_keyboard_h(path):
  270. current_path = Path('keyboards/')
  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. layouts.update(find_layouts(keyboard_h_path))
  278. return layouts
  279. def _find_all_layouts(info_data, keyboard):
  280. """Looks for layout macros associated with this keyboard.
  281. """
  282. layouts = _search_keyboard_h(Path(keyboard))
  283. if not layouts:
  284. # 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.
  285. info_data['parse_warnings'].append('%s: Falling back to searching for KEYMAP/LAYOUT macros.' % (keyboard))
  286. for file in glob('keyboards/%s/*.h' % keyboard):
  287. if file.endswith('.h'):
  288. these_layouts = find_layouts(file)
  289. if these_layouts:
  290. layouts.update(these_layouts)
  291. return layouts
  292. def _log_error(info_data, message):
  293. """Send an error message to both JSON and the log.
  294. """
  295. info_data['parse_errors'].append(message)
  296. cli.log.error('%s: %s', info_data.get('keyboard_folder', 'Unknown Keyboard!'), message)
  297. def _log_warning(info_data, message):
  298. """Send a warning message to both JSON and the log.
  299. """
  300. info_data['parse_warnings'].append(message)
  301. cli.log.warning('%s: %s', info_data.get('keyboard_folder', 'Unknown Keyboard!'), message)
  302. def arm_processor_rules(info_data, rules):
  303. """Setup the default info for an ARM board.
  304. """
  305. info_data['processor_type'] = 'arm'
  306. info_data['bootloader'] = rules['BOOTLOADER'] if 'BOOTLOADER' in rules else 'unknown'
  307. info_data['processor'] = rules['MCU'] if 'MCU' in rules else 'unknown'
  308. info_data['protocol'] = 'ChibiOS'
  309. if info_data['bootloader'] == 'unknown':
  310. if 'STM32' in info_data['processor']:
  311. info_data['bootloader'] = 'stm32-dfu'
  312. if 'STM32' in info_data['processor']:
  313. info_data['platform'] = 'STM32'
  314. elif 'MCU_SERIES' in rules:
  315. info_data['platform'] = rules['MCU_SERIES']
  316. elif 'ARM_ATSAM' in rules:
  317. info_data['platform'] = 'ARM_ATSAM'
  318. return info_data
  319. def avr_processor_rules(info_data, rules):
  320. """Setup the default info for an AVR board.
  321. """
  322. info_data['processor_type'] = 'avr'
  323. info_data['bootloader'] = rules['BOOTLOADER'] if 'BOOTLOADER' in rules else 'atmel-dfu'
  324. info_data['platform'] = rules['ARCH'] if 'ARCH' in rules else 'unknown'
  325. info_data['processor'] = rules['MCU'] if 'MCU' in rules else 'unknown'
  326. info_data['protocol'] = 'V-USB' if rules.get('MCU') in VUSB_PROCESSORS else 'LUFA'
  327. # FIXME(fauxpark/anyone): Eventually we should detect the protocol by looking at PROTOCOL inherited from mcu_selection.mk:
  328. # info_data['protocol'] = 'V-USB' if rules.get('PROTOCOL') == 'VUSB' else 'LUFA'
  329. return info_data
  330. def unknown_processor_rules(info_data, rules):
  331. """Setup the default keyboard info for unknown boards.
  332. """
  333. info_data['bootloader'] = 'unknown'
  334. info_data['platform'] = 'unknown'
  335. info_data['processor'] = 'unknown'
  336. info_data['processor_type'] = 'unknown'
  337. info_data['protocol'] = 'unknown'
  338. return info_data
  339. def merge_info_jsons(keyboard, info_data):
  340. """Return a merged copy of all the info.json files for a keyboard.
  341. """
  342. for info_file in find_info_json(keyboard):
  343. # Load and validate the JSON data
  344. try:
  345. new_info_data = json.load(info_file.open('r'))
  346. except Exception as e:
  347. _log_error(info_data, "Invalid JSON in file %s: %s: %s" % (str(info_file), e.__class__.__name__, e))
  348. new_info_data = {}
  349. if not isinstance(new_info_data, dict):
  350. _log_error(info_data, "Invalid file %s, root object should be a dictionary." % (str(info_file),))
  351. continue
  352. # Copy whitelisted keys into `info_data`
  353. for key in ('debounce', 'diode_direction', 'indicators', 'keyboard_name', 'manufacturer', 'identifier', 'url', 'maintainer', 'processor', 'bootloader', 'width', 'height'):
  354. if key in new_info_data:
  355. info_data[key] = new_info_data[key]
  356. # Deep merge certain keys
  357. # FIXME(skullydazed/anyone): this should be generalized more so that we can inteligently merge more than one level deep. It would be nice if we could filter on valid keys too. That may have to wait for a future where we use openapi or something.
  358. for key in ('features', 'layout_aliases', 'matrix_pins', 'rgblight', 'usb'):
  359. if key in new_info_data:
  360. if key not in info_data:
  361. info_data[key] = {}
  362. info_data[key].update(new_info_data[key])
  363. # Merge the layouts
  364. if 'community_layouts' in new_info_data:
  365. if 'community_layouts' in info_data:
  366. for layout in new_info_data['community_layouts']:
  367. if layout not in info_data['community_layouts']:
  368. info_data['community_layouts'].append(layout)
  369. else:
  370. info_data['community_layouts'] = new_info_data['community_layouts']
  371. if 'layouts' in new_info_data:
  372. _merge_layouts(info_data, new_info_data)
  373. return info_data
  374. def find_info_json(keyboard):
  375. """Finds all the info.json files associated with a keyboard.
  376. """
  377. # Find the most specific first
  378. base_path = Path('keyboards')
  379. keyboard_path = base_path / keyboard
  380. keyboard_parent = keyboard_path.parent
  381. info_jsons = [keyboard_path / 'info.json']
  382. # Add DEFAULT_FOLDER before parents, if present
  383. rules = rules_mk(keyboard)
  384. if 'DEFAULT_FOLDER' in rules:
  385. info_jsons.append(Path(rules['DEFAULT_FOLDER']) / 'info.json')
  386. # Add in parent folders for least specific
  387. for _ in range(5):
  388. info_jsons.append(keyboard_parent / 'info.json')
  389. if keyboard_parent.parent == base_path:
  390. break
  391. keyboard_parent = keyboard_parent.parent
  392. # Return a list of the info.json files that actually exist
  393. return [info_json for info_json in info_jsons if info_json.exists()]