diff options
Diffstat (limited to 'lib/python/qmk/info.py')
| -rw-r--r-- | lib/python/qmk/info.py | 249 |
1 files changed, 249 insertions, 0 deletions
diff --git a/lib/python/qmk/info.py b/lib/python/qmk/info.py new file mode 100644 index 000000000..e1ace5d51 --- /dev/null +++ b/lib/python/qmk/info.py | |||
| @@ -0,0 +1,249 @@ | |||
| 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 | |||
| 7 | from milc import cli | ||
| 8 | |||
| 9 | from qmk.constants import ARM_PROCESSORS, AVR_PROCESSORS, VUSB_PROCESSORS | ||
| 10 | from qmk.c_parse import find_layouts | ||
| 11 | from qmk.keyboard import config_h, rules_mk | ||
| 12 | from qmk.math import compute | ||
| 13 | |||
| 14 | |||
| 15 | def info_json(keyboard): | ||
| 16 | """Generate the info.json data for a specific keyboard. | ||
| 17 | """ | ||
| 18 | info_data = { | ||
| 19 | 'keyboard_name': str(keyboard), | ||
| 20 | 'keyboard_folder': str(keyboard), | ||
| 21 | 'layouts': {}, | ||
| 22 | 'maintainer': 'qmk', | ||
| 23 | } | ||
| 24 | |||
| 25 | for layout_name, layout_json in _find_all_layouts(keyboard).items(): | ||
| 26 | if not layout_name.startswith('LAYOUT_kc'): | ||
| 27 | info_data['layouts'][layout_name] = layout_json | ||
| 28 | |||
| 29 | info_data = merge_info_jsons(keyboard, info_data) | ||
| 30 | info_data = _extract_config_h(info_data) | ||
| 31 | info_data = _extract_rules_mk(info_data) | ||
| 32 | |||
| 33 | return info_data | ||
| 34 | |||
| 35 | |||
| 36 | def _extract_config_h(info_data): | ||
| 37 | """Pull some keyboard information from existing rules.mk files | ||
| 38 | """ | ||
| 39 | config_c = config_h(info_data['keyboard_folder']) | ||
| 40 | row_pins = config_c.get('MATRIX_ROW_PINS', '').replace('{', '').replace('}', '').strip() | ||
| 41 | col_pins = config_c.get('MATRIX_COL_PINS', '').replace('{', '').replace('}', '').strip() | ||
| 42 | direct_pins = config_c.get('DIRECT_PINS', '').replace(' ', '')[1:-1] | ||
| 43 | |||
| 44 | info_data['diode_direction'] = config_c.get('DIODE_DIRECTION') | ||
| 45 | info_data['matrix_size'] = { | ||
| 46 | 'rows': compute(config_c.get('MATRIX_ROWS', '0')), | ||
| 47 | 'cols': compute(config_c.get('MATRIX_COLS', '0')), | ||
| 48 | } | ||
| 49 | info_data['matrix_pins'] = {} | ||
| 50 | |||
| 51 | if row_pins: | ||
| 52 | info_data['matrix_pins']['rows'] = row_pins.split(',') | ||
| 53 | if col_pins: | ||
| 54 | info_data['matrix_pins']['cols'] = col_pins.split(',') | ||
| 55 | |||
| 56 | if direct_pins: | ||
| 57 | direct_pin_array = [] | ||
| 58 | for row in direct_pins.split('},{'): | ||
| 59 | if row.startswith('{'): | ||
| 60 | row = row[1:] | ||
| 61 | if row.endswith('}'): | ||
| 62 | row = row[:-1] | ||
| 63 | |||
| 64 | direct_pin_array.append([]) | ||
| 65 | |||
| 66 | for pin in row.split(','): | ||
| 67 | if pin == 'NO_PIN': | ||
| 68 | pin = None | ||
| 69 | |||
| 70 | direct_pin_array[-1].append(pin) | ||
| 71 | |||
| 72 | info_data['matrix_pins']['direct'] = direct_pin_array | ||
| 73 | |||
| 74 | info_data['usb'] = { | ||
| 75 | 'vid': config_c.get('VENDOR_ID'), | ||
| 76 | 'pid': config_c.get('PRODUCT_ID'), | ||
| 77 | 'device_ver': config_c.get('DEVICE_VER'), | ||
| 78 | 'manufacturer': config_c.get('MANUFACTURER'), | ||
| 79 | 'product': config_c.get('PRODUCT'), | ||
| 80 | 'description': config_c.get('DESCRIPTION'), | ||
| 81 | } | ||
| 82 | |||
| 83 | return info_data | ||
| 84 | |||
| 85 | |||
| 86 | def _extract_rules_mk(info_data): | ||
| 87 | """Pull some keyboard information from existing rules.mk files | ||
| 88 | """ | ||
| 89 | rules = rules_mk(info_data['keyboard_folder']) | ||
| 90 | mcu = rules.get('MCU') | ||
| 91 | |||
| 92 | if mcu in ARM_PROCESSORS: | ||
| 93 | arm_processor_rules(info_data, rules) | ||
| 94 | elif mcu in AVR_PROCESSORS: | ||
| 95 | avr_processor_rules(info_data, rules) | ||
| 96 | else: | ||
| 97 | cli.log.warning("%s: Unknown MCU: %s" % (info_data['keyboard_folder'], mcu)) | ||
| 98 | unknown_processor_rules(info_data, rules) | ||
| 99 | |||
| 100 | return info_data | ||
| 101 | |||
| 102 | |||
| 103 | def _find_all_layouts(keyboard): | ||
| 104 | """Looks for layout macros associated with this keyboard. | ||
| 105 | """ | ||
| 106 | layouts = {} | ||
| 107 | rules = rules_mk(keyboard) | ||
| 108 | keyboard_path = Path(rules.get('DEFAULT_FOLDER', keyboard)) | ||
| 109 | |||
| 110 | # Pull in all layouts defined in the standard files | ||
| 111 | current_path = Path('keyboards/') | ||
| 112 | for directory in keyboard_path.parts: | ||
| 113 | current_path = current_path / directory | ||
| 114 | keyboard_h = '%s.h' % (directory,) | ||
| 115 | keyboard_h_path = current_path / keyboard_h | ||
| 116 | if keyboard_h_path.exists(): | ||
| 117 | layouts.update(find_layouts(keyboard_h_path)) | ||
| 118 | |||
| 119 | if not layouts: | ||
| 120 | # If we didn't find any layouts above we widen our search. This is error | ||
| 121 | # prone which is why we want to encourage people to follow the standard above. | ||
| 122 | cli.log.warning('%s: Falling back to searching for KEYMAP/LAYOUT macros.' % (keyboard)) | ||
| 123 | for file in glob('keyboards/%s/*.h' % keyboard): | ||
| 124 | if file.endswith('.h'): | ||
| 125 | these_layouts = find_layouts(file) | ||
| 126 | if these_layouts: | ||
| 127 | layouts.update(these_layouts) | ||
| 128 | |||
| 129 | if 'LAYOUTS' in rules: | ||
| 130 | # Match these up against the supplied layouts | ||
| 131 | supported_layouts = rules['LAYOUTS'].strip().split() | ||
| 132 | for layout_name in sorted(layouts): | ||
| 133 | if not layout_name.startswith('LAYOUT_'): | ||
| 134 | continue | ||
| 135 | layout_name = layout_name[7:] | ||
| 136 | if layout_name in supported_layouts: | ||
| 137 | supported_layouts.remove(layout_name) | ||
| 138 | |||
| 139 | if supported_layouts: | ||
| 140 | cli.log.error('%s: Missing LAYOUT() macro for %s' % (keyboard, ', '.join(supported_layouts))) | ||
| 141 | |||
| 142 | return layouts | ||
| 143 | |||
| 144 | |||
| 145 | def arm_processor_rules(info_data, rules): | ||
| 146 | """Setup the default info for an ARM board. | ||
| 147 | """ | ||
| 148 | info_data['processor_type'] = 'arm' | ||
| 149 | info_data['bootloader'] = rules['BOOTLOADER'] if 'BOOTLOADER' in rules else 'unknown' | ||
| 150 | info_data['processor'] = rules['MCU'] if 'MCU' in rules else 'unknown' | ||
| 151 | info_data['protocol'] = 'ChibiOS' | ||
| 152 | |||
| 153 | if info_data['bootloader'] == 'unknown': | ||
| 154 | if 'STM32' in info_data['processor']: | ||
| 155 | info_data['bootloader'] = 'stm32-dfu' | ||
| 156 | elif info_data.get('manufacturer') == 'Input Club': | ||
| 157 | info_data['bootloader'] = 'kiibohd-dfu' | ||
| 158 | |||
| 159 | if 'STM32' in info_data['processor']: | ||
| 160 | info_data['platform'] = 'STM32' | ||
| 161 | elif 'MCU_SERIES' in rules: | ||
| 162 | info_data['platform'] = rules['MCU_SERIES'] | ||
| 163 | elif 'ARM_ATSAM' in rules: | ||
| 164 | info_data['platform'] = 'ARM_ATSAM' | ||
| 165 | |||
| 166 | return info_data | ||
| 167 | |||
| 168 | |||
| 169 | def avr_processor_rules(info_data, rules): | ||
| 170 | """Setup the default info for an AVR board. | ||
| 171 | """ | ||
| 172 | info_data['processor_type'] = 'avr' | ||
| 173 | info_data['bootloader'] = rules['BOOTLOADER'] if 'BOOTLOADER' in rules else 'atmel-dfu' | ||
| 174 | info_data['platform'] = rules['ARCH'] if 'ARCH' in rules else 'unknown' | ||
| 175 | info_data['processor'] = rules['MCU'] if 'MCU' in rules else 'unknown' | ||
| 176 | info_data['protocol'] = 'V-USB' if rules.get('MCU') in VUSB_PROCESSORS else 'LUFA' | ||
| 177 | |||
| 178 | # FIXME(fauxpark/anyone): Eventually we should detect the protocol by looking at PROTOCOL inherited from mcu_selection.mk: | ||
| 179 | # info_data['protocol'] = 'V-USB' if rules.get('PROTOCOL') == 'VUSB' else 'LUFA' | ||
| 180 | |||
| 181 | return info_data | ||
| 182 | |||
| 183 | |||
| 184 | def unknown_processor_rules(info_data, rules): | ||
| 185 | """Setup the default keyboard info for unknown boards. | ||
| 186 | """ | ||
| 187 | info_data['bootloader'] = 'unknown' | ||
| 188 | info_data['platform'] = 'unknown' | ||
| 189 | info_data['processor'] = 'unknown' | ||
| 190 | info_data['processor_type'] = 'unknown' | ||
| 191 | info_data['protocol'] = 'unknown' | ||
| 192 | |||
| 193 | return info_data | ||
| 194 | |||
| 195 | |||
| 196 | def merge_info_jsons(keyboard, info_data): | ||
| 197 | """Return a merged copy of all the info.json files for a keyboard. | ||
| 198 | """ | ||
| 199 | for info_file in find_info_json(keyboard): | ||
| 200 | # Load and validate the JSON data | ||
| 201 | with info_file.open('r') as info_fd: | ||
| 202 | new_info_data = json.load(info_fd) | ||
| 203 | |||
| 204 | if not isinstance(new_info_data, dict): | ||
| 205 | cli.log.error("Invalid file %s, root object should be a dictionary.", str(info_file)) | ||
| 206 | continue | ||
| 207 | |||
| 208 | # Copy whitelisted keys into `info_data` | ||
| 209 | for key in ('keyboard_name', 'manufacturer', 'identifier', 'url', 'maintainer', 'processor', 'bootloader', 'width', 'height'): | ||
| 210 | if key in new_info_data: | ||
| 211 | info_data[key] = new_info_data[key] | ||
| 212 | |||
| 213 | # Merge the layouts in | ||
| 214 | if 'layouts' in new_info_data: | ||
| 215 | for layout_name, json_layout in new_info_data['layouts'].items(): | ||
| 216 | # Only pull in layouts we have a macro for | ||
| 217 | if layout_name in info_data['layouts']: | ||
| 218 | if info_data['layouts'][layout_name]['key_count'] != len(json_layout['layout']): | ||
| 219 | cli.log.error('%s: %s: Number of elements in info.json does not match! info.json:%s != %s:%s', info_data['keyboard_folder'], layout_name, len(json_layout['layout']), layout_name, len(info_data['layouts'][layout_name]['layout'])) | ||
| 220 | else: | ||
| 221 | for i, key in enumerate(info_data['layouts'][layout_name]['layout']): | ||
| 222 | key.update(json_layout['layout'][i]) | ||
| 223 | |||
| 224 | return info_data | ||
| 225 | |||
| 226 | |||
| 227 | def find_info_json(keyboard): | ||
| 228 | """Finds all the info.json files associated with a keyboard. | ||
| 229 | """ | ||
| 230 | # Find the most specific first | ||
| 231 | base_path = Path('keyboards') | ||
| 232 | keyboard_path = base_path / keyboard | ||
| 233 | keyboard_parent = keyboard_path.parent | ||
| 234 | info_jsons = [keyboard_path / 'info.json'] | ||
| 235 | |||
| 236 | # Add DEFAULT_FOLDER before parents, if present | ||
| 237 | rules = rules_mk(keyboard) | ||
| 238 | if 'DEFAULT_FOLDER' in rules: | ||
| 239 | info_jsons.append(Path(rules['DEFAULT_FOLDER']) / 'info.json') | ||
| 240 | |||
| 241 | # Add in parent folders for least specific | ||
| 242 | for _ in range(5): | ||
| 243 | info_jsons.append(keyboard_parent / 'info.json') | ||
| 244 | if keyboard_parent.parent == base_path: | ||
| 245 | break | ||
| 246 | keyboard_parent = keyboard_parent.parent | ||
| 247 | |||
| 248 | # Return a list of the info.json files that actually exist | ||
| 249 | return [info_json for info_json in info_jsons if info_json.exists()] | ||
