community_modules.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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
  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. else:
  68. pass
  69. lines.append('}')
  70. lines.append('')
  71. # _kb
  72. lines.append(f'__attribute__((weak)) {api.ret_type} {api.name}_{module_name}_kb({api.args}) {{')
  73. if api.ret_type == 'bool':
  74. lines.append(f' if(!{api.name}_{module_name}_user({api.call_params})) {{ return false; }}')
  75. lines.append(' return true;')
  76. else:
  77. lines.append(f' {api.name}_{module_name}_user({api.call_params});')
  78. lines.append('}')
  79. lines.append('')
  80. # module (non-suffixed)
  81. lines.append(f'__attribute__((weak)) {api.ret_type} {api.name}_{module_name}({api.args}) {{')
  82. if api.ret_type == 'bool':
  83. lines.append(f' if(!{api.name}_{module_name}_kb({api.call_params})) {{ return false; }}')
  84. lines.append(' return true;')
  85. else:
  86. lines.append(f' {api.name}_{module_name}_kb({api.call_params});')
  87. lines.append('}')
  88. return lines
  89. def _render_core_implementation(api, modules):
  90. lines = []
  91. lines.append('')
  92. with _render_api_guard(lines, api):
  93. lines.append(f'{api.ret_type} {api.name}_modules({api.args}) {{')
  94. if api.ret_type == 'bool':
  95. lines.append(' return true')
  96. for module in modules:
  97. module_name = Path(module).name
  98. if api.ret_type == 'bool':
  99. lines.append(f' && {api.name}_{module_name}({api.call_params})')
  100. else:
  101. lines.append(f' {api.name}_{module_name}({api.call_params});')
  102. if api.ret_type == 'bool':
  103. lines.append(' ;')
  104. lines.append('}')
  105. return lines
  106. @cli.argument('-o', '--output', arg_only=True, type=qmk.path.normpath, help='File to write to')
  107. @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
  108. @cli.argument('-kb', '--keyboard', arg_only=True, type=keyboard_folder, completer=keyboard_completer, help='Keyboard to generate community_modules.h for.')
  109. @cli.argument('filename', nargs='?', type=qmk.path.FileType('r'), arg_only=True, completer=FilesCompleter('.json'), help='Configurator JSON file')
  110. @cli.subcommand('Creates a community_modules.h from a keymap.json file.')
  111. def generate_community_modules_h(cli):
  112. """Creates a community_modules.h from a keymap.json file
  113. """
  114. if cli.args.output and cli.args.output.name == '-':
  115. cli.args.output = None
  116. api_list, api_version, ver_major, ver_minor, ver_patch = module_api_list()
  117. lines = [
  118. GPL2_HEADER_C_LIKE,
  119. GENERATED_HEADER_C_LIKE,
  120. '#pragma once',
  121. '#include <stdint.h>',
  122. '#include <stdbool.h>',
  123. '#include <keycodes.h>',
  124. '',
  125. '#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))',
  126. f'#define COMMUNITY_MODULES_API_VERSION COMMUNITY_MODULES_API_VERSION_BUILDER({ver_major},{ver_minor},{ver_patch})',
  127. 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}.")',
  128. '',
  129. 'typedef struct keyrecord_t keyrecord_t; // forward declaration so we don\'t need to include quantum.h',
  130. '',
  131. ]
  132. modules = get_modules(cli.args.keyboard, cli.args.filename)
  133. module_jsons = load_module_jsons(modules)
  134. if len(modules) > 0:
  135. lines.extend(_render_keycodes(module_jsons))
  136. for api in api_list:
  137. lines.extend(_render_api_header(api))
  138. for module in modules:
  139. lines.append('')
  140. lines.append(f'// From module: {module}')
  141. for api in api_list:
  142. lines.extend(_render_api_declarations(api, Path(module).name))
  143. lines.append('')
  144. lines.append('// Core wrapper')
  145. for api in api_list:
  146. lines.extend(_render_api_declarations(api, 'modules', user_kb=False))
  147. dump_lines(cli.args.output, lines, cli.args.quiet, remove_repeated_newlines=True)
  148. @cli.argument('-o', '--output', arg_only=True, type=qmk.path.normpath, help='File to write to')
  149. @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
  150. @cli.argument('-kb', '--keyboard', arg_only=True, type=keyboard_folder, completer=keyboard_completer, help='Keyboard to generate community_modules.c for.')
  151. @cli.argument('filename', nargs='?', type=qmk.path.FileType('r'), arg_only=True, completer=FilesCompleter('.json'), help='Configurator JSON file')
  152. @cli.subcommand('Creates a community_modules.c from a keymap.json file.')
  153. def generate_community_modules_c(cli):
  154. """Creates a community_modules.c from a keymap.json file
  155. """
  156. if cli.args.output and cli.args.output.name == '-':
  157. cli.args.output = None
  158. api_list, _, _, _, _ = module_api_list()
  159. lines = [
  160. GPL2_HEADER_C_LIKE,
  161. GENERATED_HEADER_C_LIKE,
  162. '',
  163. '#include "community_modules.h"',
  164. ]
  165. modules = get_modules(cli.args.keyboard, cli.args.filename)
  166. if len(modules) > 0:
  167. for module in modules:
  168. for api in api_list:
  169. lines.extend(_render_api_implementations(api, Path(module).name))
  170. for api in api_list:
  171. lines.extend(_render_core_implementation(api, modules))
  172. dump_lines(cli.args.output, lines, cli.args.quiet, remove_repeated_newlines=True)
  173. @cli.argument('-o', '--output', arg_only=True, type=qmk.path.normpath, help='File to write to')
  174. @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
  175. @cli.argument('-kb', '--keyboard', arg_only=True, type=keyboard_folder, completer=keyboard_completer, help='Keyboard to generate community_modules.c for.')
  176. @cli.argument('filename', nargs='?', type=qmk.path.FileType('r'), arg_only=True, completer=FilesCompleter('.json'), help='Configurator JSON file')
  177. @cli.subcommand('Creates a community_modules_introspection.h from a keymap.json file.')
  178. def generate_community_modules_introspection_h(cli):
  179. """Creates a community_modules_introspection.h from a keymap.json file
  180. """
  181. if cli.args.output and cli.args.output.name == '-':
  182. cli.args.output = None
  183. lines = [
  184. GPL2_HEADER_C_LIKE,
  185. GENERATED_HEADER_C_LIKE,
  186. '',
  187. ]
  188. modules = get_modules(cli.args.keyboard, cli.args.filename)
  189. if len(modules) > 0:
  190. for module in modules:
  191. module_path = find_module_path(module)
  192. lines.append(f'#if __has_include("{module_path}/introspection.h")')
  193. lines.append(f'#include "{module_path}/introspection.h"')
  194. lines.append(f'#endif // __has_include("{module_path}/introspection.h")')
  195. lines.append('')
  196. dump_lines(cli.args.output, lines, cli.args.quiet, remove_repeated_newlines=True)
  197. @cli.argument('-o', '--output', arg_only=True, type=qmk.path.normpath, help='File to write to')
  198. @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
  199. @cli.argument('-kb', '--keyboard', arg_only=True, type=keyboard_folder, completer=keyboard_completer, help='Keyboard to generate community_modules.c for.')
  200. @cli.argument('filename', nargs='?', type=qmk.path.FileType('r'), arg_only=True, completer=FilesCompleter('.json'), help='Configurator JSON file')
  201. @cli.subcommand('Creates a community_modules_introspection.c from a keymap.json file.')
  202. def generate_community_modules_introspection_c(cli):
  203. """Creates a community_modules_introspection.c from a keymap.json file
  204. """
  205. if cli.args.output and cli.args.output.name == '-':
  206. cli.args.output = None
  207. lines = [
  208. GPL2_HEADER_C_LIKE,
  209. GENERATED_HEADER_C_LIKE,
  210. '',
  211. ]
  212. modules = get_modules(cli.args.keyboard, cli.args.filename)
  213. if len(modules) > 0:
  214. for module in modules:
  215. module_path = find_module_path(module)
  216. lines.append(f'#if __has_include("{module_path}/introspection.c")')
  217. lines.append(f'#include "{module_path}/introspection.c"')
  218. lines.append(f'#endif // __has_include("{module_path}/introspection.c")')
  219. lines.append('')
  220. dump_lines(cli.args.output, lines, cli.args.quiet, remove_repeated_newlines=True)