info.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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. if 'indicators' not in info_data:
  110. info_data['indicators'] = {}
  111. info_data['indicators'][json_key] = config_c.get(config_key)
  112. return info_data
  113. def _extract_community_layouts(info_data, rules):
  114. """Find the community layouts in rules.mk.
  115. """
  116. community_layouts = rules['LAYOUTS'].split() if 'LAYOUTS' in rules else []
  117. if 'community_layouts' in info_data:
  118. for layout in community_layouts:
  119. if layout not in info_data['community_layouts']:
  120. community_layouts.append(layout)
  121. else:
  122. info_data['community_layouts'] = community_layouts
  123. return info_data
  124. def _extract_features(info_data, rules):
  125. """Find all the features enabled in rules.mk.
  126. """
  127. for key, value in rules.items():
  128. if key.endswith('_ENABLE'):
  129. key = '_'.join(key.split('_')[:-1]).lower()
  130. value = True if value in true_values else False if value in false_values else value
  131. if 'config_h_features' not in info_data:
  132. info_data['config_h_features'] = {}
  133. if 'features' not in info_data:
  134. info_data['features'] = {}
  135. if key in info_data['features']:
  136. _log_warning(info_data, 'Feature %s is specified in both info.json and rules.mk, the rules.mk value wins.' % (key,))
  137. info_data['features'][key] = value
  138. info_data['config_h_features'][key] = value
  139. return info_data
  140. def _extract_rgblight(info_data, config_c):
  141. """Handle the rgblight configuration
  142. """
  143. rgblight = info_data.get('rgblight', {})
  144. animations = rgblight.get('animations', {})
  145. for json_key, config_key in rgblight_properties.items():
  146. if config_key in config_c:
  147. if json_key in rgblight:
  148. _log_warning(info_data, 'RGB Light: %s is specified in both info.json and config.h, the config.h value wins.' % (json_key,))
  149. rgblight[json_key] = config_c[config_key]
  150. for json_key, config_key in rgblight_toggles.items():
  151. if config_key in config_c:
  152. if json_key in rgblight:
  153. _log_warning(info_data, 'RGB Light: %s is specified in both info.json and config.h, the config.h value wins.', json_key)
  154. rgblight[json_key] = config_c[config_key]
  155. for json_key, config_key in rgblight_animations.items():
  156. if config_key in config_c:
  157. if json_key in animations:
  158. _log_warning(info_data, 'RGB Light: animations: %s is specified in both info.json and config.h, the config.h value wins.' % (json_key,))
  159. animations[json_key] = config_c[config_key]
  160. if animations:
  161. rgblight['animations'] = animations
  162. if rgblight:
  163. info_data['rgblight'] = rgblight
  164. return info_data
  165. def _extract_matrix_info(info_data, config_c):
  166. """Populate the matrix information.
  167. """
  168. row_pins = config_c.get('MATRIX_ROW_PINS', '').replace('{', '').replace('}', '').strip()
  169. col_pins = config_c.get('MATRIX_COL_PINS', '').replace('{', '').replace('}', '').strip()
  170. direct_pins = config_c.get('DIRECT_PINS', '').replace(' ', '')[1:-1]
  171. if 'MATRIX_ROWS' in config_c and 'MATRIX_COLS' in config_c:
  172. if 'matrix_size' in info_data:
  173. _log_warning(info_data, 'Matrix size is specified in both info.json and config.h, the config.h values win.')
  174. info_data['matrix_size'] = {
  175. 'rows': compute(config_c.get('MATRIX_ROWS', '0')),
  176. 'cols': compute(config_c.get('MATRIX_COLS', '0')),
  177. }
  178. if row_pins and col_pins:
  179. if 'matrix_pins' in info_data:
  180. _log_warning(info_data, 'Matrix pins are specified in both info.json and config.h, the config.h values win.')
  181. info_data['matrix_pins'] = {}
  182. if row_pins:
  183. info_data['matrix_pins']['rows'] = row_pins.split(',')
  184. if col_pins:
  185. info_data['matrix_pins']['cols'] = col_pins.split(',')
  186. if direct_pins:
  187. if 'matrix_pins' in info_data:
  188. _log_warning(info_data, 'Direct pins are specified in both info.json and config.h, the config.h values win.')
  189. info_data['matrix_pins'] = {}
  190. direct_pin_array = []
  191. for row in direct_pins.split('},{'):
  192. if row.startswith('{'):
  193. row = row[1:]
  194. if row.endswith('}'):
  195. row = row[:-1]
  196. direct_pin_array.append([])
  197. for pin in row.split(','):
  198. if pin == 'NO_PIN':
  199. pin = None
  200. direct_pin_array[-1].append(pin)
  201. info_data['matrix_pins']['direct'] = direct_pin_array
  202. return info_data
  203. def _extract_usb_info(info_data, config_c):
  204. """Populate the USB information.
  205. """
  206. usb_properties = {'vid': 'VENDOR_ID', 'pid': 'PRODUCT_ID', 'device_ver': 'DEVICE_VER'}
  207. if 'usb' not in info_data:
  208. info_data['usb'] = {}
  209. for info_name, config_name in usb_properties.items():
  210. if config_name in config_c:
  211. if info_name in info_data['usb']:
  212. _log_warning(info_data, '%s in config.h is overwriting usb.%s in info.json' % (config_name, info_name))
  213. info_data['usb'][info_name] = config_c[config_name]
  214. elif info_name not in info_data['usb']:
  215. _log_error(info_data, '%s not specified in config.h, and %s not specified in info.json. One is required.' % (config_name, info_name))
  216. return info_data
  217. def _extract_config_h(info_data):
  218. """Pull some keyboard information from existing config.h files
  219. """
  220. config_c = config_h(info_data['keyboard_folder'])
  221. _extract_debounce(info_data, config_c)
  222. _extract_diode_direction(info_data, config_c)
  223. _extract_indicators(info_data, config_c)
  224. _extract_matrix_info(info_data, config_c)
  225. _extract_usb_info(info_data, config_c)
  226. _extract_rgblight(info_data, config_c)
  227. return info_data
  228. def _extract_rules_mk(info_data):
  229. """Pull some keyboard information from existing rules.mk files
  230. """
  231. rules = rules_mk(info_data['keyboard_folder'])
  232. mcu = rules.get('MCU')
  233. if mcu in CHIBIOS_PROCESSORS:
  234. arm_processor_rules(info_data, rules)
  235. elif mcu in LUFA_PROCESSORS + VUSB_PROCESSORS:
  236. avr_processor_rules(info_data, rules)
  237. else:
  238. cli.log.warning("%s: Unknown MCU: %s" % (info_data['keyboard_folder'], mcu))
  239. unknown_processor_rules(info_data, rules)
  240. _extract_community_layouts(info_data, rules)
  241. _extract_features(info_data, rules)
  242. return info_data
  243. def _merge_layouts(info_data, new_info_data):
  244. """Merge new_info_data into info_data in an intelligent way.
  245. """
  246. for layout_name, layout_json in new_info_data['layouts'].items():
  247. if layout_name in info_data['layouts']:
  248. # Pull in layouts we have a macro for
  249. if len(info_data['layouts'][layout_name]['layout']) != len(layout_json['layout']):
  250. msg = '%s: %s: Number of elements in info.json does not match! info.json:%s != %s:%s'
  251. _log_error(info_data, msg % (info_data['keyboard_folder'], layout_name, len(layout_json['layout']), layout_name, len(info_data['layouts'][layout_name]['layout'])))
  252. else:
  253. for i, key in enumerate(info_data['layouts'][layout_name]['layout']):
  254. key.update(layout_json['layout'][i])
  255. else:
  256. # Pull in layouts that have matrix data
  257. missing_matrix = False
  258. for key in layout_json['layout']:
  259. if 'matrix' not in key:
  260. missing_matrix = True
  261. if not missing_matrix:
  262. if layout_name in info_data['layouts']:
  263. # Update an existing layout with new data
  264. for i, key in enumerate(info_data['layouts'][layout_name]['layout']):
  265. key.update(layout_json['layout'][i])
  266. else:
  267. # Copy in the new layout wholesale
  268. layout_json['c_macro'] = False
  269. info_data['layouts'][layout_name] = layout_json
  270. return info_data
  271. def _search_keyboard_h(path):
  272. current_path = Path('keyboards/')
  273. layouts = {}
  274. for directory in path.parts:
  275. current_path = current_path / directory
  276. keyboard_h = '%s.h' % (directory,)
  277. keyboard_h_path = current_path / keyboard_h
  278. if keyboard_h_path.exists():
  279. layouts.update(find_layouts(keyboard_h_path))
  280. return layouts
  281. def _find_all_layouts(info_data, keyboard):
  282. """Looks for layout macros associated with this keyboard.
  283. """
  284. layouts = _search_keyboard_h(Path(keyboard))
  285. if not layouts:
  286. # 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.
  287. info_data['parse_warnings'].append('%s: Falling back to searching for KEYMAP/LAYOUT macros.' % (keyboard))
  288. for file in glob('keyboards/%s/*.h' % keyboard):
  289. if file.endswith('.h'):
  290. these_layouts = find_layouts(file)
  291. if these_layouts:
  292. layouts.update(these_layouts)
  293. return layouts
  294. def _log_error(info_data, message):
  295. """Send an error message to both JSON and the log.
  296. """
  297. info_data['parse_errors'].append(message)
  298. cli.log.error('%s: %s', info_data.get('keyboard_folder', 'Unknown Keyboard!'), message)
  299. def _log_warning(info_data, message):
  300. """Send a warning message to both JSON and the log.
  301. """
  302. info_data['parse_warnings'].append(message)
  303. cli.log.warning('%s: %s', info_data.get('keyboard_folder', 'Unknown Keyboard!'), message)
  304. def arm_processor_rules(info_data, rules):
  305. """Setup the default info for an ARM board.
  306. """
  307. info_data['processor_type'] = 'arm'
  308. info_data['bootloader'] = rules['BOOTLOADER'] if 'BOOTLOADER' in rules else 'unknown'
  309. info_data['processor'] = rules['MCU'] if 'MCU' in rules else 'unknown'
  310. info_data['protocol'] = 'ChibiOS'
  311. if info_data['bootloader'] == 'unknown':
  312. if 'STM32' in info_data['processor']:
  313. info_data['bootloader'] = 'stm32-dfu'
  314. if 'STM32' in info_data['processor']:
  315. info_data['platform'] = 'STM32'
  316. elif 'MCU_SERIES' in rules:
  317. info_data['platform'] = rules['MCU_SERIES']
  318. elif 'ARM_ATSAM' in rules:
  319. info_data['platform'] = 'ARM_ATSAM'
  320. return info_data
  321. def avr_processor_rules(info_data, rules):
  322. """Setup the default info for an AVR board.
  323. """
  324. info_data['processor_type'] = 'avr'
  325. info_data['bootloader'] = rules['BOOTLOADER'] if 'BOOTLOADER' in rules else 'atmel-dfu'
  326. info_data['platform'] = rules['ARCH'] if 'ARCH' in rules else 'unknown'
  327. info_data['processor'] = rules['MCU'] if 'MCU' in rules else 'unknown'
  328. info_data['protocol'] = 'V-USB' if rules.get('MCU') in VUSB_PROCESSORS else 'LUFA'
  329. # FIXME(fauxpark/anyone): Eventually we should detect the protocol by looking at PROTOCOL inherited from mcu_selection.mk:
  330. # info_data['protocol'] = 'V-USB' if rules.get('PROTOCOL') == 'VUSB' else 'LUFA'
  331. return info_data
  332. def unknown_processor_rules(info_data, rules):
  333. """Setup the default keyboard info for unknown boards.
  334. """
  335. info_data['bootloader'] = 'unknown'
  336. info_data['platform'] = 'unknown'
  337. info_data['processor'] = 'unknown'
  338. info_data['processor_type'] = 'unknown'
  339. info_data['protocol'] = 'unknown'
  340. return info_data
  341. def merge_info_jsons(keyboard, info_data):
  342. """Return a merged copy of all the info.json files for a keyboard.
  343. """
  344. for info_file in find_info_json(keyboard):
  345. # Load and validate the JSON data
  346. try:
  347. new_info_data = json.load(info_file.open('r'))
  348. except Exception as e:
  349. _log_error(info_data, "Invalid JSON in file %s: %s: %s" % (str(info_file), e.__class__.__name__, e))
  350. new_info_data = {}
  351. if not isinstance(new_info_data, dict):
  352. _log_error(info_data, "Invalid file %s, root object should be a dictionary." % (str(info_file),))
  353. continue
  354. # Copy whitelisted keys into `info_data`
  355. for key in ('debounce', 'diode_direction', 'indicators', 'keyboard_name', 'manufacturer', 'identifier', 'url', 'maintainer', 'processor', 'bootloader', 'width', 'height'):
  356. if key in new_info_data:
  357. info_data[key] = new_info_data[key]
  358. # Deep merge certain keys
  359. # 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.
  360. for key in ('features', 'layout_aliases', 'matrix_pins', 'rgblight', 'usb'):
  361. if key in new_info_data:
  362. if key not in info_data:
  363. info_data[key] = {}
  364. info_data[key].update(new_info_data[key])
  365. # Merge the layouts
  366. if 'community_layouts' in new_info_data:
  367. if 'community_layouts' in info_data:
  368. for layout in new_info_data['community_layouts']:
  369. if layout not in info_data['community_layouts']:
  370. info_data['community_layouts'].append(layout)
  371. else:
  372. info_data['community_layouts'] = new_info_data['community_layouts']
  373. if 'layouts' in new_info_data:
  374. _merge_layouts(info_data, new_info_data)
  375. return info_data
  376. def find_info_json(keyboard):
  377. """Finds all the info.json files associated with a keyboard.
  378. """
  379. # Find the most specific first
  380. base_path = Path('keyboards')
  381. keyboard_path = base_path / keyboard
  382. keyboard_parent = keyboard_path.parent
  383. info_jsons = [keyboard_path / 'info.json']
  384. # Add DEFAULT_FOLDER before parents, if present
  385. rules = rules_mk(keyboard)
  386. if 'DEFAULT_FOLDER' in rules:
  387. info_jsons.append(Path(rules['DEFAULT_FOLDER']) / 'info.json')
  388. # Add in parent folders for least specific
  389. for _ in range(5):
  390. info_jsons.append(keyboard_parent / 'info.json')
  391. if keyboard_parent.parent == base_path:
  392. break
  393. keyboard_parent = keyboard_parent.parent
  394. # Return a list of the info.json files that actually exist
  395. return [info_json for info_json in info_jsons if info_json.exists()]