logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

community_modules.py (16221B)


  1. import contextlib
  2. from argcomplete.completers import FilesCompleter
  3. from pathlib import Path
  4. from milc import cli
  5. import qmk.path
  6. from qmk.info import get_modules
  7. from qmk.keyboard import keyboard_completer, keyboard_folder
  8. from qmk.commands import dump_lines
  9. from qmk.constants import GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE, GPL2_HEADER_SH_LIKE, GENERATED_HEADER_SH_LIKE
  10. from qmk.community_modules import module_api_list, load_module_jsons, find_module_path
  11. @contextlib.contextmanager
  12. def _render_api_guard(lines, api):
  13. if api.guard:
  14. lines.append(f'#if {api.guard}')
  15. yield
  16. if api.guard:
  17. lines.append(f'#endif // {api.guard}')
  18. def _render_api_header(api):
  19. lines = []
  20. if api.header:
  21. lines.append('')
  22. with _render_api_guard(lines, api):
  23. lines.append(f'#include <{api.header}>')
  24. return lines
  25. def _render_keycodes(module_jsons):
  26. lines = []
  27. lines.append('')
  28. lines.append('enum {')
  29. first = True
  30. for module_json in module_jsons:
  31. module_name = Path(module_json['module']).name
  32. keycodes = module_json.get('keycodes', [])
  33. if len(keycodes) > 0:
  34. lines.append(f' // From module: {module_name}')
  35. for keycode in keycodes:
  36. key = keycode.get('key', None)
  37. if first:
  38. lines.append(f' {key} = QK_COMMUNITY_MODULE,')
  39. first = False
  40. else:
  41. lines.append(f' {key},')
  42. for alias in keycode.get('aliases', []):
  43. lines.append(f' {alias} = {key},')
  44. lines.append('')
  45. lines.append(' LAST_COMMUNITY_MODULE_KEY')
  46. lines.append('};')
  47. lines.append('STATIC_ASSERT((int)LAST_COMMUNITY_MODULE_KEY <= (int)(QK_COMMUNITY_MODULE_MAX+1), "Too many community module keycodes");')
  48. return lines
  49. def _render_api_declarations(api, module, user_kb=True):
  50. lines = []
  51. lines.append('')
  52. with _render_api_guard(lines, api):
  53. if user_kb:
  54. lines.append(f'{api.ret_type} {api.name}_{module}_user({api.args});')
  55. lines.append(f'{api.ret_type} {api.name}_{module}_kb({api.args});')
  56. lines.append(f'{api.ret_type} {api.name}_{module}({api.args});')
  57. return lines
  58. def _render_api_implementations(api, module):
  59. module_name = Path(module).name
  60. lines = []
  61. lines.append('')
  62. with _render_api_guard(lines, api):
  63. # _user
  64. lines.append(f'__attribute__((weak)) {api.ret_type} {api.name}_{module_name}_user({api.args}) {{')
  65. if api.ret_type == 'bool':
  66. lines.append(' return true;')
  67. elif api.ret_type in ['layer_state_t', 'report_mouse_t']:
  68. lines.append(f' return {api.call_params};')
  69. else:
  70. pass
  71. lines.append('}')
  72. lines.append('')
  73. # _kb
  74. lines.append(f'__attribute__((weak)) {api.ret_type} {api.name}_{module_name}_kb({api.args}) {{')
  75. if api.ret_type == 'bool':
  76. lines.append(f' if(!{api.name}_{module_name}_user({api.call_params})) {{ return false; }}')
  77. lines.append(' return true;')
  78. elif api.ret_type in ['layer_state_t', 'report_mouse_t']:
  79. lines.append(f' return {api.name}_{module_name}_user({api.call_params});')
  80. else:
  81. lines.append(f' {api.name}_{module_name}_user({api.call_params});')
  82. lines.append('}')
  83. lines.append('')
  84. # module (non-suffixed)
  85. lines.append(f'__attribute__((weak)) {api.ret_type} {api.name}_{module_name}({api.args}) {{')
  86. if api.ret_type == 'bool':
  87. lines.append(f' if(!{api.name}_{module_name}_kb({api.call_params})) {{ return false; }}')
  88. lines.append(' return true;')
  89. elif api.ret_type in ['layer_state_t', 'report_mouse_t']:
  90. lines.append(f' return {api.name}_{module_name}_kb({api.call_params});')
  91. else:
  92. lines.append(f' {api.name}_{module_name}_kb({api.call_params});')
  93. lines.append('}')
  94. return lines
  95. def _render_core_implementation(api, modules):
  96. lines = []
  97. lines.append('')
  98. with _render_api_guard(lines, api):
  99. lines.append(f'{api.ret_type} {api.name}_modules({api.args}) {{')
  100. if api.ret_type == 'bool':
  101. lines.append(' return true')
  102. for module in modules:
  103. module_name = Path(module).name
  104. if api.ret_type == 'bool':
  105. lines.append(f' && {api.name}_{module_name}({api.call_params})')
  106. elif api.ret_type in ['layer_state_t', 'report_mouse_t']:
  107. lines.append(f' {api.call_params} = {api.name}_{module_name}({api.call_params});')
  108. else:
  109. lines.append(f' {api.name}_{module_name}({api.call_params});')
  110. if api.ret_type == 'bool':
  111. lines.append(' ;')
  112. elif api.ret_type in ['layer_state_t', 'report_mouse_t']:
  113. lines.append(f' return {api.call_params};')
  114. lines.append('}')
  115. return lines
  116. def _generate_features_rules(features_dict):
  117. lines = []
  118. for feature, enabled in features_dict.items():
  119. feature = feature.upper()
  120. enabled = 'yes' if enabled else 'no'
  121. lines.append(f'{feature}_ENABLE={enabled}')
  122. return lines
  123. def _generate_modules_rules(keyboard, filename):
  124. lines = []
  125. modules = get_modules(keyboard, filename)
  126. if len(modules) > 0:
  127. lines.append('')
  128. lines.append('OPT_DEFS += -DCOMMUNITY_MODULES_ENABLE=TRUE')
  129. for module in modules:
  130. module_path = qmk.path.unix_style_path(find_module_path(module))
  131. if not module_path:
  132. raise FileNotFoundError(f"Module '{module}' not found.")
  133. lines.append('')
  134. lines.append(f'COMMUNITY_MODULES += {module_path.name}') # use module_path here instead of module as it may be a subdirectory
  135. lines.append(f'OPT_DEFS += -DCOMMUNITY_MODULE_{module_path.name.upper()}_ENABLE=TRUE')
  136. lines.append(f'COMMUNITY_MODULE_PATHS += {module_path}')
  137. lines.append(f'VPATH += {module_path}')
  138. lines.append(f'SRC += $(wildcard {module_path}/{module_path.name}.c)')
  139. lines.append(f'MODULE_NAME_{module_path.name.upper()} := {module_path.name}')
  140. lines.append(f'MODULE_PATH_{module_path.name.upper()} := {module_path}')
  141. lines.append(f'-include {module_path}/rules.mk')
  142. module_jsons = load_module_jsons(modules)
  143. for module_json in module_jsons:
  144. if 'features' in module_json:
  145. lines.append('')
  146. lines.append(f'# Module: {module_json["module_name"]}')
  147. lines.extend(_generate_features_rules(module_json['features']))
  148. return lines
  149. @cli.argument('-o', '--output', arg_only=True, type=qmk.path.normpath, help='File to write to')
  150. @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
  151. @cli.argument('-e', '--escape', arg_only=True, action='store_true', help="Escape spaces in quiet mode")
  152. @cli.argument('-kb', '--keyboard', arg_only=True, type=keyboard_folder, completer=keyboard_completer, help='Keyboard to generate rules.mk for.')
  153. @cli.argument('filename', nargs='?', arg_only=True, type=qmk.path.FileType('r'), completer=FilesCompleter('.json'), help='A configurator export JSON to be compiled and flashed or a pre-compiled binary firmware file (bin/hex) to be flashed.')
  154. @cli.subcommand('Creates a community_modules_rules_mk from a keymap.json file.')
  155. def generate_community_modules_rules_mk(cli):
  156. rules_mk_lines = [GPL2_HEADER_SH_LIKE, GENERATED_HEADER_SH_LIKE]
  157. rules_mk_lines.extend(_generate_modules_rules(cli.args.keyboard, cli.args.filename))
  158. # Show the results
  159. dump_lines(cli.args.output, rules_mk_lines)
  160. if cli.args.output:
  161. if cli.args.quiet:
  162. if cli.args.escape:
  163. print(cli.args.output.as_posix().replace(' ', '\\ '))
  164. else:
  165. print(cli.args.output)
  166. else:
  167. cli.log.info('Wrote rules.mk to %s.', cli.args.output)
  168. @cli.argument('-o', '--output', arg_only=True, type=qmk.path.normpath, help='File to write to')
  169. @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
  170. @cli.argument('-kb', '--keyboard', arg_only=True, type=keyboard_folder, completer=keyboard_completer, help='Keyboard to generate community_modules.h for.')
  171. @cli.argument('filename', nargs='?', type=qmk.path.FileType('r'), arg_only=True, completer=FilesCompleter('.json'), help='Configurator JSON file')
  172. @cli.subcommand('Creates a community_modules.h from a keymap.json file.')
  173. def generate_community_modules_h(cli):
  174. """Creates a community_modules.h from a keymap.json file
  175. """
  176. if cli.args.output and cli.args.output.name == '-':
  177. cli.args.output = None
  178. api_list, api_version, ver_major, ver_minor, ver_patch = module_api_list()
  179. lines = [
  180. GPL2_HEADER_C_LIKE,
  181. GENERATED_HEADER_C_LIKE,
  182. '#pragma once',
  183. '#include <stdint.h>',
  184. '#include <stdbool.h>',
  185. '#include <keycodes.h>',
  186. '',
  187. '#include "compiler_support.h"',
  188. '',
  189. '#define COMMUNITY_MODULES_API_VERSION_BUILDER(ver_major,ver_minor,ver_patch) (((((uint32_t)(ver_major))&0xFF) << 24) | ((((uint32_t)(ver_minor))&0xFF) << 16) | (((uint32_t)(ver_patch))&0xFF))',
  190. f'#define COMMUNITY_MODULES_API_VERSION COMMUNITY_MODULES_API_VERSION_BUILDER({ver_major},{ver_minor},{ver_patch})',
  191. f'#define ASSERT_COMMUNITY_MODULES_MIN_API_VERSION(ver_major,ver_minor,ver_patch) STATIC_ASSERT(COMMUNITY_MODULES_API_VERSION_BUILDER(ver_major,ver_minor,ver_patch) <= COMMUNITY_MODULES_API_VERSION, "Community module requires a newer version of QMK modules API -- needs: " #ver_major "." #ver_minor "." #ver_patch ", current: {api_version}.")',
  192. '',
  193. 'typedef struct keyrecord_t keyrecord_t; // forward declaration so we don\'t need to include quantum.h',
  194. '',
  195. ]
  196. modules = get_modules(cli.args.keyboard, cli.args.filename)
  197. module_jsons = load_module_jsons(modules)
  198. if len(modules) > 0:
  199. lines.extend(_render_keycodes(module_jsons))
  200. for api in api_list:
  201. lines.extend(_render_api_header(api))
  202. for module in modules:
  203. lines.append('')
  204. lines.append(f'// From module: {module}')
  205. for api in api_list:
  206. lines.extend(_render_api_declarations(api, Path(module).name))
  207. lines.append('')
  208. lines.append('// Core wrapper')
  209. for api in api_list:
  210. lines.extend(_render_api_declarations(api, 'modules', user_kb=False))
  211. dump_lines(cli.args.output, lines, cli.args.quiet, remove_repeated_newlines=True)
  212. @cli.argument('-o', '--output', arg_only=True, type=qmk.path.normpath, help='File to write to')
  213. @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
  214. @cli.argument('-kb', '--keyboard', arg_only=True, type=keyboard_folder, completer=keyboard_completer, help='Keyboard to generate community_modules.c for.')
  215. @cli.argument('filename', nargs='?', type=qmk.path.FileType('r'), arg_only=True, completer=FilesCompleter('.json'), help='Configurator JSON file')
  216. @cli.subcommand('Creates a community_modules.c from a keymap.json file.')
  217. def generate_community_modules_c(cli):
  218. """Creates a community_modules.c from a keymap.json file
  219. """
  220. if cli.args.output and cli.args.output.name == '-':
  221. cli.args.output = None
  222. api_list, _, _, _, _ = module_api_list()
  223. lines = [
  224. GPL2_HEADER_C_LIKE,
  225. GENERATED_HEADER_C_LIKE,
  226. '',
  227. '#include "community_modules.h"',
  228. ]
  229. modules = get_modules(cli.args.keyboard, cli.args.filename)
  230. if len(modules) > 0:
  231. for module in modules:
  232. for api in api_list:
  233. lines.extend(_render_api_implementations(api, Path(module).name))
  234. for api in api_list:
  235. lines.extend(_render_core_implementation(api, modules))
  236. dump_lines(cli.args.output, lines, cli.args.quiet, remove_repeated_newlines=True)
  237. def _generate_include_per_module(cli, include_file_name):
  238. """Generates C code to include "<module_path>/include_file_name" for each module."""
  239. if cli.args.output and cli.args.output.name == '-':
  240. cli.args.output = None
  241. lines = [GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE]
  242. for module in get_modules(cli.args.keyboard, cli.args.filename):
  243. full_path = f'{find_module_path(module)}/{include_file_name}'
  244. lines.append('')
  245. lines.append(f'#if __has_include("{full_path}")')
  246. lines.append(f'#include "{full_path}"')
  247. lines.append(f'#endif // __has_include("{full_path}")')
  248. dump_lines(cli.args.output, lines, cli.args.quiet, remove_repeated_newlines=True)
  249. @cli.argument('-o', '--output', arg_only=True, type=qmk.path.normpath, help='File to write to')
  250. @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
  251. @cli.argument('-kb', '--keyboard', arg_only=True, type=keyboard_folder, completer=keyboard_completer, help='Keyboard to generate community_modules_introspection.h for.')
  252. @cli.argument('filename', nargs='?', type=qmk.path.FileType('r'), arg_only=True, completer=FilesCompleter('.json'), help='Configurator JSON file')
  253. @cli.subcommand('Creates a community_modules_introspection.h from a keymap.json file.')
  254. def generate_community_modules_introspection_h(cli):
  255. """Creates a community_modules_introspection.h from a keymap.json file
  256. """
  257. _generate_include_per_module(cli, 'introspection.h')
  258. @cli.argument('-o', '--output', arg_only=True, type=qmk.path.normpath, help='File to write to')
  259. @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
  260. @cli.argument('-kb', '--keyboard', arg_only=True, type=keyboard_folder, completer=keyboard_completer, help='Keyboard to generate community_modules.c for.')
  261. @cli.argument('filename', nargs='?', type=qmk.path.FileType('r'), arg_only=True, completer=FilesCompleter('.json'), help='Configurator JSON file')
  262. @cli.subcommand('Creates a community_modules_introspection.c from a keymap.json file.')
  263. def generate_community_modules_introspection_c(cli):
  264. """Creates a community_modules_introspection.c from a keymap.json file
  265. """
  266. _generate_include_per_module(cli, 'introspection.c')
  267. @cli.argument('-o', '--output', arg_only=True, type=qmk.path.normpath, help='File to write to')
  268. @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
  269. @cli.argument('-kb', '--keyboard', arg_only=True, type=keyboard_folder, completer=keyboard_completer, help='Keyboard to generate led_matrix_community_modules.inc for.')
  270. @cli.argument('filename', nargs='?', type=qmk.path.FileType('r'), arg_only=True, completer=FilesCompleter('.json'), help='Configurator JSON file')
  271. @cli.subcommand('Creates an led_matrix_community_modules.inc from a keymap.json file.')
  272. def generate_led_matrix_community_modules_inc(cli):
  273. """Creates an led_matrix_community_modules.inc from a keymap.json file
  274. """
  275. _generate_include_per_module(cli, 'led_matrix_module.inc')
  276. @cli.argument('-o', '--output', arg_only=True, type=qmk.path.normpath, help='File to write to')
  277. @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
  278. @cli.argument('-kb', '--keyboard', arg_only=True, type=keyboard_folder, completer=keyboard_completer, help='Keyboard to generate rgb_matrix_community_modules.inc for.')
  279. @cli.argument('filename', nargs='?', type=qmk.path.FileType('r'), arg_only=True, completer=FilesCompleter('.json'), help='Configurator JSON file')
  280. @cli.subcommand('Creates an rgb_matrix_community_modules.inc from a keymap.json file.')
  281. def generate_rgb_matrix_community_modules_inc(cli):
  282. """Creates an rgb_matrix_community_modules.inc from a keymap.json file
  283. """
  284. _generate_include_per_module(cli, 'rgb_matrix_module.inc')