inline_generator.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. """This script generates the XAP protocol generated header to be compiled into QMK.
  2. """
  3. import re
  4. from pathlib import Path
  5. from qmk.casing import to_snake
  6. from qmk.commands import dump_lines
  7. from qmk.constants import GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE
  8. from qmk.xap.common import merge_xap_defs, route_conditions
  9. from qmk.json_schema import json_load
  10. PREFIX_MAP = {
  11. 'rgblight': {
  12. 'ifdef': 'RGBLIGHT_EFFECT',
  13. 'def': 'RGBLIGHT_MODE',
  14. },
  15. 'rgb_matrix': {
  16. 'ifdef': 'ENABLE_RGB_MATRIX',
  17. 'def': 'RGB_MATRIX',
  18. },
  19. 'led_matrix': {
  20. 'ifdef': 'ENABLE_LED_MATRIX',
  21. 'def': 'LED_MATRIX',
  22. },
  23. }
  24. def _get_lighting_spec(xap_defs, feature):
  25. version = xap_defs['uses'][feature]
  26. spec = json_load(Path(f'data/constants/{feature}_{version}.json'))
  27. # preprocess for gross rgblight "mode + n"
  28. for obj in spec.get('effects', {}).values():
  29. define = obj['key']
  30. offset = 0
  31. found = re.match('(.*)_(\\d+)$', define)
  32. if found:
  33. define = found.group(1)
  34. offset = int(found.group(2)) - 1
  35. obj['define'] = define
  36. obj['offset'] = offset
  37. return spec
  38. def _get_c_type(xap_type):
  39. if xap_type == 'bool':
  40. return 'bool'
  41. elif xap_type == 'u8':
  42. return 'uint8_t'
  43. elif xap_type == 'u16':
  44. return 'uint16_t'
  45. elif xap_type == 'u32':
  46. return 'uint32_t'
  47. elif xap_type == 'u64':
  48. return 'uint64_t'
  49. elif xap_type == 'struct':
  50. return 'struct'
  51. elif xap_type == 'string':
  52. return 'const char *'
  53. return 'unknown'
  54. def _get_c_size(xap_type):
  55. if xap_type == 'u8':
  56. return 'sizeof(uint8_t)'
  57. elif xap_type == 'u16':
  58. return 'sizeof(uint16_t)'
  59. elif xap_type == 'u32':
  60. return 'sizeof(uint32_t)'
  61. elif xap_type == 'u64':
  62. return 8
  63. elif xap_type == 'u8[32]':
  64. return 32
  65. return 0
  66. def _get_route_type(container):
  67. if 'routes' in container:
  68. return 'XAP_ROUTE'
  69. elif 'return_execute' in container:
  70. return 'XAP_EXECUTE'
  71. elif 'return_value' in container:
  72. if container['return_type'] == 'u8':
  73. return 'XAP_VALUE'
  74. elif 'return_constant' in container:
  75. if container['return_type'] == 'u8':
  76. return 'XAP_CONST_MEM'
  77. elif container['return_type'] == 'u16':
  78. return 'XAP_CONST_MEM'
  79. elif container['return_type'] == 'u32':
  80. return 'XAP_CONST_MEM'
  81. elif container['return_type'] == 'u64':
  82. return 'XAP_CONST_MEM'
  83. elif container['return_type'] == 'struct':
  84. return 'XAP_CONST_MEM'
  85. elif container['return_type'] == 'string':
  86. return 'XAP_CONST_MEM'
  87. elif 'return_getter' in container:
  88. if container['return_type'] == 'u32':
  89. return 'XAP_GETTER'
  90. return 'UNSUPPORTED'
  91. def _append_routing_table_declaration(lines, container, container_id, route_stack):
  92. route_stack.append(container)
  93. route_name = to_snake('_'.join([r['define'] for r in route_stack]))
  94. if 'routes' in container:
  95. pass
  96. elif 'return_execute' in container:
  97. execute = container['return_execute']
  98. lines.append(f'bool xap_respond_{execute}(xap_token_t token, const uint8_t *data, size_t data_len);')
  99. # elif 'return_value' in container:
  100. # value = container['return_value']
  101. # return_type = container['return_type']
  102. # lines.append('')
  103. # lines.append(f'{_get_c_type(return_type)} {value} = 0;')
  104. elif 'return_constant' in container:
  105. if container['return_type'] == 'u8':
  106. constant = container['return_constant']
  107. lines.append('')
  108. lines.append(f'static const uint8_t {route_name}_data PROGMEM = {constant};')
  109. elif container['return_type'] == 'u16':
  110. constant = container['return_constant']
  111. lines.append('')
  112. lines.append(f'static const uint16_t {route_name}_data PROGMEM = {constant};')
  113. elif container['return_type'] == 'u32':
  114. constant = container['return_constant']
  115. lines.append('')
  116. lines.append(f'static const uint32_t {route_name}_data PROGMEM = {constant};')
  117. elif container['return_type'] == 'u64':
  118. constant = container['return_constant']
  119. lines.append('')
  120. lines.append(f'static const uint64_t {route_name}_data PROGMEM = {constant};')
  121. elif container['return_type'] == 'struct':
  122. lines.append('')
  123. lines.append(f'static const {route_name}_t {route_name}_data PROGMEM = {{')
  124. for constant in container['return_constant']:
  125. lines.append(f' {constant},')
  126. lines.append('};')
  127. elif container['return_type'] == 'string':
  128. constant = container['return_constant']
  129. lines.append('')
  130. lines.append(f'static const char {route_name}_str[] PROGMEM = {constant};')
  131. elif 'return_getter' in container:
  132. if container['return_type'] == 'u32':
  133. lines.append('')
  134. lines.append(f'extern uint32_t {route_name}_getter(void);')
  135. elif container['return_type'] == 'struct':
  136. pass
  137. route_stack.pop()
  138. def _append_routing_table_entry_flags(lines, container, container_id, route_stack):
  139. pem_map = {
  140. None: 'ROUTE_PERMISSIONS_INSECURE',
  141. 'secure': 'ROUTE_PERMISSIONS_SECURE',
  142. }
  143. is_secure = pem_map[container.get('permissions', None)]
  144. lines.append(' .flags = {')
  145. lines.append(f' .type = {_get_route_type(container)},')
  146. lines.append(f' .secure = {is_secure},')
  147. lines.append(' },')
  148. def _append_routing_table_entry_route(lines, container, container_id, route_stack):
  149. route_name = to_snake('_'.join([r['define'] for r in route_stack]))
  150. lines.append(f' .child_routes = {route_name}_table,')
  151. lines.append(f' .child_routes_len = sizeof({route_name}_table)/sizeof(xap_route_t),')
  152. def _append_routing_table_entry_execute(lines, container, container_id, route_stack):
  153. value = container['return_execute']
  154. lines.append(f' .handler = xap_respond_{value},')
  155. def _append_routing_table_entry_value(lines, container, container_id, route_stack):
  156. value = container['return_value']
  157. lines.append(f' .const_data = &{value},')
  158. lines.append(f' .const_data_len = sizeof({value}),')
  159. def _append_routing_table_entry_u32getter(lines, container, container_id, route_stack):
  160. route_name = to_snake('_'.join([r['define'] for r in route_stack]))
  161. lines.append(f' .u32getter = &{route_name}_getter,')
  162. def _append_routing_table_entry_const_data(lines, container, container_id, route_stack):
  163. route_name = to_snake('_'.join([r['define'] for r in route_stack]))
  164. lines.append(f' .const_data = &{route_name}_data,')
  165. lines.append(f' .const_data_len = sizeof({route_name}_data),')
  166. def _append_routing_table_entry_string(lines, container, container_id, route_stack):
  167. route_name = to_snake('_'.join([r['define'] for r in route_stack]))
  168. lines.append(f' .const_data = {route_name}_str,')
  169. lines.append(f' .const_data_len = sizeof({route_name}_str) - 1,')
  170. def _append_routing_table_entry(lines, container, container_id, route_stack):
  171. route_stack.append(container)
  172. route_name = '_'.join([r['define'] for r in route_stack])
  173. condition = route_conditions(route_stack)
  174. if condition:
  175. lines.append(f'#if {condition}')
  176. lines.append(f' [{route_name}] = {{')
  177. _append_routing_table_entry_flags(lines, container, container_id, route_stack)
  178. if 'routes' in container:
  179. _append_routing_table_entry_route(lines, container, container_id, route_stack)
  180. elif 'return_execute' in container:
  181. _append_routing_table_entry_execute(lines, container, container_id, route_stack)
  182. elif 'return_value' in container:
  183. _append_routing_table_entry_value(lines, container, container_id, route_stack)
  184. elif 'return_constant' in container:
  185. if container['return_type'] == 'u8':
  186. _append_routing_table_entry_const_data(lines, container, container_id, route_stack)
  187. elif container['return_type'] == 'u16':
  188. _append_routing_table_entry_const_data(lines, container, container_id, route_stack)
  189. elif container['return_type'] == 'u32':
  190. _append_routing_table_entry_const_data(lines, container, container_id, route_stack)
  191. elif container['return_type'] == 'u64':
  192. _append_routing_table_entry_const_data(lines, container, container_id, route_stack)
  193. elif container['return_type'] == 'struct':
  194. _append_routing_table_entry_const_data(lines, container, container_id, route_stack)
  195. elif container['return_type'] == 'string':
  196. _append_routing_table_entry_string(lines, container, container_id, route_stack)
  197. elif 'return_getter' in container:
  198. if container['return_type'] == 'u32':
  199. _append_routing_table_entry_u32getter(lines, container, container_id, route_stack)
  200. lines.append(' },')
  201. if condition:
  202. lines.append(f'#endif // {condition}')
  203. route_stack.pop()
  204. def _append_routing_tables(lines, container, container_id=None, route_stack=None):
  205. """Handles building the list of the XAP routes, combining parent and child names together, as well as the route number.
  206. """
  207. if route_stack is None:
  208. route_stack = [container]
  209. else:
  210. route_stack.append(container)
  211. route_name = to_snake('_'.join([r['define'] for r in route_stack]))
  212. condition = route_conditions(route_stack)
  213. if 'routes' in container:
  214. for route_id in container['routes']:
  215. route = container['routes'][route_id]
  216. _append_routing_tables(lines, route, route_id, route_stack)
  217. for route_id in container['routes']:
  218. route = container['routes'][route_id]
  219. _append_routing_table_declaration(lines, route, route_id, route_stack)
  220. lines.append('')
  221. if condition:
  222. lines.append(f'#if {condition}')
  223. lines.append(f'static const xap_route_t {route_name}_table[] PROGMEM = {{')
  224. for route_id in container['routes']:
  225. route = container['routes'][route_id]
  226. _append_routing_table_entry(lines, route, route_id, route_stack)
  227. lines.append('};')
  228. if condition:
  229. lines.append(f'#endif // {condition}')
  230. lines.append('')
  231. route_stack.pop()
  232. def _append_broadcast_messages(lines, container):
  233. """TODO:
  234. """
  235. broadcast_messages = container.get('broadcast_messages', {})
  236. broadcast_prefix = broadcast_messages['define_prefix']
  237. for key, value in broadcast_messages['messages'].items():
  238. define = value.get('define')
  239. name = to_snake(f'{broadcast_prefix}_{define}')
  240. if 'return_type' in value:
  241. ret_type = _get_c_type(value['return_type'])
  242. lines.append(f'void {name}({ret_type} value) {{ xap_broadcast({key}, &value, sizeof(value)); }}')
  243. else:
  244. lines.append(f'void {name}(const void *data, size_t length){{ xap_broadcast({key}, data, length); }}')
  245. def _append_lighting_map(lines, feature, spec):
  246. """TODO:
  247. """
  248. groups = spec.get('groups', {})
  249. ifdef_prefix = PREFIX_MAP[feature]['ifdef']
  250. def_prefix = PREFIX_MAP[feature]['def']
  251. lines.append(f'static uint8_t {feature}_effect_map[][2] = {{')
  252. for id, obj in spec.get('effects', {}).items():
  253. define = obj['define']
  254. offset = f' + {obj["offset"]}' if obj['offset'] else ''
  255. line = f'''
  256. #ifdef {ifdef_prefix}_{define}
  257. {{ {id}, {def_prefix}_{define}{offset}}},
  258. #endif'''
  259. group = groups.get(obj.get('group', None), {}).get('define', None)
  260. if group:
  261. line = f'''
  262. #ifdef {group}
  263. {line}
  264. #endif'''
  265. lines.append(line)
  266. lines.append('};')
  267. # add helper funcs
  268. lines.append(
  269. f'''
  270. uint8_t {feature}2xap(uint8_t val) {{
  271. for(uint8_t i = 0; i < ARRAY_SIZE({feature}_effect_map); i++) {{
  272. if ({feature}_effect_map[i][1] == val)
  273. return {feature}_effect_map[i][0];
  274. }}
  275. return 0xFF;
  276. }}
  277. uint8_t xap2{feature}(uint8_t val) {{
  278. for(uint8_t i = 0; i < ARRAY_SIZE({feature}_effect_map); i++) {{
  279. if ({feature}_effect_map[i][0] == val)
  280. return {feature}_effect_map[i][1];
  281. }}
  282. return 0xFF;
  283. }}'''
  284. )
  285. def _append_lighting_bitmask(lines, feature, spec):
  286. """TODO:
  287. """
  288. groups = spec.get('groups', {})
  289. ifdef_prefix = PREFIX_MAP[feature]['ifdef']
  290. lines.append(f'enum {{ ENABLED_{feature.upper()}_EFFECTS = 0')
  291. for id, obj in spec.get('effects', {}).items():
  292. define = obj['define']
  293. line = f'''
  294. #ifdef {ifdef_prefix}_{define}
  295. | (1ULL << {id})
  296. #endif'''
  297. group = groups.get(obj.get('group', None), {}).get('define', None)
  298. if group:
  299. line = f'''
  300. #ifdef {group}
  301. {line}
  302. #endif'''
  303. lines.append(line)
  304. lines.append('};')
  305. def _append_lighting_mapping(lines, xap_defs):
  306. """TODO:
  307. """
  308. # TODO: remove bodge for always enabled effects
  309. lines.append('''
  310. #define RGBLIGHT_EFFECT_STATIC_LIGHT
  311. #define ENABLE_RGB_MATRIX_SOLID_COLOR
  312. #define ENABLE_LED_MATRIX_SOLID
  313. ''')
  314. for feature in PREFIX_MAP.keys():
  315. spec = _get_lighting_spec(xap_defs, feature)
  316. lines.append(f'#ifdef {feature.upper()}_ENABLE')
  317. _append_lighting_map(lines, feature, spec)
  318. lines.append(f'#endif //{feature.upper()}_ENABLE')
  319. # TODO: should be inside ifdef but causes build issues
  320. _append_lighting_bitmask(lines, feature, spec)
  321. def generate_inline(output_file, keyboard, keymap):
  322. """Generates the XAP protocol header file, generated during normal build.
  323. """
  324. xap_defs = merge_xap_defs(keyboard, keymap)
  325. # Preamble
  326. lines = [GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE, '']
  327. # TODO: gen somewhere else?
  328. _append_lighting_mapping(lines, xap_defs)
  329. # Add all the generated code
  330. _append_broadcast_messages(lines, xap_defs)
  331. _append_routing_tables(lines, xap_defs)
  332. dump_lines(output_file, lines)