Community modules (#24848)
This commit is contained in:
parent
63b095212b
commit
1efc82403b
37 changed files with 987 additions and 84 deletions
|
@ -49,6 +49,7 @@ subcommands = [
|
|||
'qmk.cli.generate.api',
|
||||
'qmk.cli.generate.autocorrect_data',
|
||||
'qmk.cli.generate.compilation_database',
|
||||
'qmk.cli.generate.community_modules',
|
||||
'qmk.cli.generate.config_h',
|
||||
'qmk.cli.generate.develop_pr_list',
|
||||
'qmk.cli.generate.dfu_header',
|
||||
|
|
|
@ -10,7 +10,7 @@ from qmk.path import normpath
|
|||
from qmk.c_parse import c_source_files
|
||||
|
||||
c_file_suffixes = ('c', 'h', 'cpp', 'hpp')
|
||||
core_dirs = ('drivers', 'quantum', 'tests', 'tmk_core', 'platforms')
|
||||
core_dirs = ('drivers', 'quantum', 'tests', 'tmk_core', 'platforms', 'modules')
|
||||
ignored = ('tmk_core/protocol/usb_hid', 'platforms/chibios/boards')
|
||||
|
||||
|
||||
|
|
|
@ -9,7 +9,7 @@ from milc import cli
|
|||
|
||||
from qmk.info import info_json
|
||||
from qmk.json_schema import json_load, validate
|
||||
from qmk.json_encoders import InfoJSONEncoder, KeymapJSONEncoder, UserspaceJSONEncoder
|
||||
from qmk.json_encoders import InfoJSONEncoder, KeymapJSONEncoder, UserspaceJSONEncoder, CommunityModuleJSONEncoder
|
||||
from qmk.path import normpath
|
||||
|
||||
|
||||
|
@ -30,6 +30,13 @@ def _detect_json_format(file, json_data):
|
|||
except ValidationError:
|
||||
pass
|
||||
|
||||
if json_encoder is None:
|
||||
try:
|
||||
validate(json_data, 'qmk.community_module.v1')
|
||||
json_encoder = CommunityModuleJSONEncoder
|
||||
except ValidationError:
|
||||
pass
|
||||
|
||||
if json_encoder is None:
|
||||
try:
|
||||
validate(json_data, 'qmk.keyboard.v1')
|
||||
|
@ -54,6 +61,8 @@ def _get_json_encoder(file, json_data):
|
|||
json_encoder = KeymapJSONEncoder
|
||||
elif cli.args.format == 'userspace':
|
||||
json_encoder = UserspaceJSONEncoder
|
||||
elif cli.args.format == 'community_module':
|
||||
json_encoder = CommunityModuleJSONEncoder
|
||||
else:
|
||||
# This should be impossible
|
||||
cli.log.error('Unknown format: %s', cli.args.format)
|
||||
|
@ -61,7 +70,7 @@ def _get_json_encoder(file, json_data):
|
|||
|
||||
|
||||
@cli.argument('json_file', arg_only=True, type=normpath, help='JSON file to format')
|
||||
@cli.argument('-f', '--format', choices=['auto', 'keyboard', 'keymap', 'userspace'], default='auto', arg_only=True, help='JSON formatter to use (Default: autodetect)')
|
||||
@cli.argument('-f', '--format', choices=['auto', 'keyboard', 'keymap', 'userspace', 'community_module'], default='auto', arg_only=True, help='JSON formatter to use (Default: autodetect)')
|
||||
@cli.argument('-i', '--inplace', action='store_true', arg_only=True, help='If set, will operate in-place on the input file')
|
||||
@cli.argument('-p', '--print', action='store_true', arg_only=True, help='If set, will print the formatted json to stdout ')
|
||||
@cli.subcommand('Generate an info.json file for a keyboard.', hidden=False if cli.config.user.developer else True)
|
||||
|
|
263
lib/python/qmk/cli/generate/community_modules.py
Normal file
263
lib/python/qmk/cli/generate/community_modules.py
Normal file
|
@ -0,0 +1,263 @@
|
|||
import contextlib
|
||||
from argcomplete.completers import FilesCompleter
|
||||
from pathlib import Path
|
||||
|
||||
from milc import cli
|
||||
|
||||
import qmk.path
|
||||
from qmk.info import get_modules
|
||||
from qmk.keyboard import keyboard_completer, keyboard_folder
|
||||
from qmk.commands import dump_lines
|
||||
from qmk.constants import GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE
|
||||
from qmk.community_modules import module_api_list, load_module_jsons, find_module_path
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def _render_api_guard(lines, api):
|
||||
if api.guard:
|
||||
lines.append(f'#if {api.guard}')
|
||||
yield
|
||||
if api.guard:
|
||||
lines.append(f'#endif // {api.guard}')
|
||||
|
||||
|
||||
def _render_api_header(api):
|
||||
lines = []
|
||||
if api.header:
|
||||
lines.append('')
|
||||
with _render_api_guard(lines, api):
|
||||
lines.append(f'#include <{api.header}>')
|
||||
return lines
|
||||
|
||||
|
||||
def _render_keycodes(module_jsons):
|
||||
lines = []
|
||||
lines.append('')
|
||||
lines.append('enum {')
|
||||
first = True
|
||||
for module_json in module_jsons:
|
||||
module_name = Path(module_json['module']).name
|
||||
keycodes = module_json.get('keycodes', [])
|
||||
if len(keycodes) > 0:
|
||||
lines.append(f' // From module: {module_name}')
|
||||
for keycode in keycodes:
|
||||
key = keycode.get('key', None)
|
||||
if first:
|
||||
lines.append(f' {key} = QK_COMMUNITY_MODULE,')
|
||||
first = False
|
||||
else:
|
||||
lines.append(f' {key},')
|
||||
for alias in keycode.get('aliases', []):
|
||||
lines.append(f' {alias} = {key},')
|
||||
lines.append('')
|
||||
lines.append(' LAST_COMMUNITY_MODULE_KEY')
|
||||
lines.append('};')
|
||||
lines.append('_Static_assert((int)LAST_COMMUNITY_MODULE_KEY <= (int)(QK_COMMUNITY_MODULE_MAX+1), "Too many community module keycodes");')
|
||||
return lines
|
||||
|
||||
|
||||
def _render_api_declarations(api, module, user_kb=True):
|
||||
lines = []
|
||||
lines.append('')
|
||||
with _render_api_guard(lines, api):
|
||||
if user_kb:
|
||||
lines.append(f'{api.ret_type} {api.name}_{module}_user({api.args});')
|
||||
lines.append(f'{api.ret_type} {api.name}_{module}_kb({api.args});')
|
||||
lines.append(f'{api.ret_type} {api.name}_{module}({api.args});')
|
||||
return lines
|
||||
|
||||
|
||||
def _render_api_implementations(api, module):
|
||||
module_name = Path(module).name
|
||||
lines = []
|
||||
lines.append('')
|
||||
with _render_api_guard(lines, api):
|
||||
# _user
|
||||
lines.append(f'__attribute__((weak)) {api.ret_type} {api.name}_{module_name}_user({api.args}) {{')
|
||||
if api.ret_type == 'bool':
|
||||
lines.append(' return true;')
|
||||
else:
|
||||
pass
|
||||
lines.append('}')
|
||||
lines.append('')
|
||||
|
||||
# _kb
|
||||
lines.append(f'__attribute__((weak)) {api.ret_type} {api.name}_{module_name}_kb({api.args}) {{')
|
||||
if api.ret_type == 'bool':
|
||||
lines.append(f' if(!{api.name}_{module_name}_user({api.call_params})) {{ return false; }}')
|
||||
lines.append(' return true;')
|
||||
else:
|
||||
lines.append(f' {api.name}_{module_name}_user({api.call_params});')
|
||||
lines.append('}')
|
||||
lines.append('')
|
||||
|
||||
# module (non-suffixed)
|
||||
lines.append(f'__attribute__((weak)) {api.ret_type} {api.name}_{module_name}({api.args}) {{')
|
||||
if api.ret_type == 'bool':
|
||||
lines.append(f' if(!{api.name}_{module_name}_kb({api.call_params})) {{ return false; }}')
|
||||
lines.append(' return true;')
|
||||
else:
|
||||
lines.append(f' {api.name}_{module_name}_kb({api.call_params});')
|
||||
lines.append('}')
|
||||
return lines
|
||||
|
||||
|
||||
def _render_core_implementation(api, modules):
|
||||
lines = []
|
||||
lines.append('')
|
||||
with _render_api_guard(lines, api):
|
||||
lines.append(f'{api.ret_type} {api.name}_modules({api.args}) {{')
|
||||
if api.ret_type == 'bool':
|
||||
lines.append(' return true')
|
||||
for module in modules:
|
||||
module_name = Path(module).name
|
||||
if api.ret_type == 'bool':
|
||||
lines.append(f' && {api.name}_{module_name}({api.call_params})')
|
||||
else:
|
||||
lines.append(f' {api.name}_{module_name}({api.call_params});')
|
||||
if api.ret_type == 'bool':
|
||||
lines.append(' ;')
|
||||
lines.append('}')
|
||||
return lines
|
||||
|
||||
|
||||
@cli.argument('-o', '--output', arg_only=True, type=qmk.path.normpath, help='File to write to')
|
||||
@cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
|
||||
@cli.argument('-kb', '--keyboard', arg_only=True, type=keyboard_folder, completer=keyboard_completer, help='Keyboard to generate community_modules.h for.')
|
||||
@cli.argument('filename', nargs='?', type=qmk.path.FileType('r'), arg_only=True, completer=FilesCompleter('.json'), help='Configurator JSON file')
|
||||
@cli.subcommand('Creates a community_modules.h from a keymap.json file.')
|
||||
def generate_community_modules_h(cli):
|
||||
"""Creates a community_modules.h from a keymap.json file
|
||||
"""
|
||||
if cli.args.output and cli.args.output.name == '-':
|
||||
cli.args.output = None
|
||||
|
||||
api_list, api_version, ver_major, ver_minor, ver_patch = module_api_list()
|
||||
|
||||
lines = [
|
||||
GPL2_HEADER_C_LIKE,
|
||||
GENERATED_HEADER_C_LIKE,
|
||||
'#pragma once',
|
||||
'#include <stdint.h>',
|
||||
'#include <stdbool.h>',
|
||||
'#include <keycodes.h>',
|
||||
'',
|
||||
'#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))',
|
||||
f'#define COMMUNITY_MODULES_API_VERSION COMMUNITY_MODULES_API_VERSION_BUILDER({ver_major},{ver_minor},{ver_patch})',
|
||||
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}.")',
|
||||
'',
|
||||
'typedef struct keyrecord_t keyrecord_t; // forward declaration so we don\'t need to include quantum.h',
|
||||
'',
|
||||
]
|
||||
|
||||
modules = get_modules(cli.args.keyboard, cli.args.filename)
|
||||
module_jsons = load_module_jsons(modules)
|
||||
if len(modules) > 0:
|
||||
lines.extend(_render_keycodes(module_jsons))
|
||||
|
||||
for api in api_list:
|
||||
lines.extend(_render_api_header(api))
|
||||
|
||||
for module in modules:
|
||||
lines.append('')
|
||||
lines.append(f'// From module: {module}')
|
||||
for api in api_list:
|
||||
lines.extend(_render_api_declarations(api, Path(module).name))
|
||||
lines.append('')
|
||||
|
||||
lines.append('// Core wrapper')
|
||||
for api in api_list:
|
||||
lines.extend(_render_api_declarations(api, 'modules', user_kb=False))
|
||||
|
||||
dump_lines(cli.args.output, lines, cli.args.quiet, remove_repeated_newlines=True)
|
||||
|
||||
|
||||
@cli.argument('-o', '--output', arg_only=True, type=qmk.path.normpath, help='File to write to')
|
||||
@cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
|
||||
@cli.argument('-kb', '--keyboard', arg_only=True, type=keyboard_folder, completer=keyboard_completer, help='Keyboard to generate community_modules.c for.')
|
||||
@cli.argument('filename', nargs='?', type=qmk.path.FileType('r'), arg_only=True, completer=FilesCompleter('.json'), help='Configurator JSON file')
|
||||
@cli.subcommand('Creates a community_modules.c from a keymap.json file.')
|
||||
def generate_community_modules_c(cli):
|
||||
"""Creates a community_modules.c from a keymap.json file
|
||||
"""
|
||||
if cli.args.output and cli.args.output.name == '-':
|
||||
cli.args.output = None
|
||||
|
||||
api_list, _, _, _, _ = module_api_list()
|
||||
|
||||
lines = [
|
||||
GPL2_HEADER_C_LIKE,
|
||||
GENERATED_HEADER_C_LIKE,
|
||||
'',
|
||||
'#include "community_modules.h"',
|
||||
]
|
||||
|
||||
modules = get_modules(cli.args.keyboard, cli.args.filename)
|
||||
if len(modules) > 0:
|
||||
|
||||
for module in modules:
|
||||
for api in api_list:
|
||||
lines.extend(_render_api_implementations(api, Path(module).name))
|
||||
|
||||
for api in api_list:
|
||||
lines.extend(_render_core_implementation(api, modules))
|
||||
|
||||
dump_lines(cli.args.output, lines, cli.args.quiet, remove_repeated_newlines=True)
|
||||
|
||||
|
||||
@cli.argument('-o', '--output', arg_only=True, type=qmk.path.normpath, help='File to write to')
|
||||
@cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
|
||||
@cli.argument('-kb', '--keyboard', arg_only=True, type=keyboard_folder, completer=keyboard_completer, help='Keyboard to generate community_modules.c for.')
|
||||
@cli.argument('filename', nargs='?', type=qmk.path.FileType('r'), arg_only=True, completer=FilesCompleter('.json'), help='Configurator JSON file')
|
||||
@cli.subcommand('Creates a community_modules_introspection.h from a keymap.json file.')
|
||||
def generate_community_modules_introspection_h(cli):
|
||||
"""Creates a community_modules_introspection.h from a keymap.json file
|
||||
"""
|
||||
if cli.args.output and cli.args.output.name == '-':
|
||||
cli.args.output = None
|
||||
|
||||
lines = [
|
||||
GPL2_HEADER_C_LIKE,
|
||||
GENERATED_HEADER_C_LIKE,
|
||||
'',
|
||||
]
|
||||
|
||||
modules = get_modules(cli.args.keyboard, cli.args.filename)
|
||||
if len(modules) > 0:
|
||||
for module in modules:
|
||||
module_path = find_module_path(module)
|
||||
lines.append(f'#if __has_include("{module_path}/introspection.h")')
|
||||
lines.append(f'#include "{module_path}/introspection.h"')
|
||||
lines.append(f'#endif // __has_include("{module_path}/introspection.h")')
|
||||
lines.append('')
|
||||
|
||||
dump_lines(cli.args.output, lines, cli.args.quiet, remove_repeated_newlines=True)
|
||||
|
||||
|
||||
@cli.argument('-o', '--output', arg_only=True, type=qmk.path.normpath, help='File to write to')
|
||||
@cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
|
||||
@cli.argument('-kb', '--keyboard', arg_only=True, type=keyboard_folder, completer=keyboard_completer, help='Keyboard to generate community_modules.c for.')
|
||||
@cli.argument('filename', nargs='?', type=qmk.path.FileType('r'), arg_only=True, completer=FilesCompleter('.json'), help='Configurator JSON file')
|
||||
@cli.subcommand('Creates a community_modules_introspection.c from a keymap.json file.')
|
||||
def generate_community_modules_introspection_c(cli):
|
||||
"""Creates a community_modules_introspection.c from a keymap.json file
|
||||
"""
|
||||
if cli.args.output and cli.args.output.name == '-':
|
||||
cli.args.output = None
|
||||
|
||||
lines = [
|
||||
GPL2_HEADER_C_LIKE,
|
||||
GENERATED_HEADER_C_LIKE,
|
||||
'',
|
||||
]
|
||||
|
||||
modules = get_modules(cli.args.keyboard, cli.args.filename)
|
||||
if len(modules) > 0:
|
||||
for module in modules:
|
||||
module_path = find_module_path(module)
|
||||
lines.append(f'#if __has_include("{module_path}/introspection.c")')
|
||||
lines.append(f'#include "{module_path}/introspection.c"')
|
||||
lines.append(f'#endif // __has_include("{module_path}/introspection.c")')
|
||||
lines.append('')
|
||||
|
||||
dump_lines(cli.args.output, lines, cli.args.quiet, remove_repeated_newlines=True)
|
|
@ -6,12 +6,13 @@ from dotty_dict import dotty
|
|||
from argcomplete.completers import FilesCompleter
|
||||
from milc import cli
|
||||
|
||||
from qmk.info import info_json
|
||||
from qmk.info import info_json, get_modules
|
||||
from qmk.json_schema import json_load
|
||||
from qmk.keyboard import keyboard_completer, keyboard_folder
|
||||
from qmk.commands import dump_lines, parse_configurator_json
|
||||
from qmk.path import normpath, FileType
|
||||
from qmk.constants import GPL2_HEADER_SH_LIKE, GENERATED_HEADER_SH_LIKE
|
||||
from qmk.community_modules import find_module_path, load_module_jsons
|
||||
|
||||
|
||||
def generate_rule(rules_key, rules_value):
|
||||
|
@ -46,6 +47,42 @@ def process_mapping_rule(kb_info_json, rules_key, info_dict):
|
|||
return generate_rule(rules_key, rules_value)
|
||||
|
||||
|
||||
def generate_features_rules(features_dict):
|
||||
lines = []
|
||||
for feature, enabled in features_dict.items():
|
||||
feature = feature.upper()
|
||||
enabled = 'yes' if enabled else 'no'
|
||||
lines.append(generate_rule(f'{feature}_ENABLE', enabled))
|
||||
return lines
|
||||
|
||||
|
||||
def generate_modules_rules(keyboard, filename):
|
||||
lines = []
|
||||
modules = get_modules(keyboard, filename)
|
||||
if len(modules) > 0:
|
||||
lines.append('')
|
||||
lines.append('OPT_DEFS += -DCOMMUNITY_MODULES_ENABLE=TRUE')
|
||||
for module in modules:
|
||||
module_path = find_module_path(module)
|
||||
if not module_path:
|
||||
raise FileNotFoundError(f"Module '{module}' not found.")
|
||||
lines.append('')
|
||||
lines.append(f'COMMUNITY_MODULES += {module_path.name}') # use module_path here instead of module as it may be a subdirectory
|
||||
lines.append(f'OPT_DEFS += -DCOMMUNITY_MODULE_{module_path.name.upper()}_ENABLE=TRUE')
|
||||
lines.append(f'COMMUNITY_MODULE_PATHS += {module_path}')
|
||||
lines.append(f'VPATH += {module_path}')
|
||||
lines.append(f'SRC += $(wildcard {module_path}/{module_path.name}.c)')
|
||||
lines.append(f'-include {module_path}/rules.mk')
|
||||
|
||||
module_jsons = load_module_jsons(modules)
|
||||
for module_json in module_jsons:
|
||||
if 'features' in module_json:
|
||||
lines.append('')
|
||||
lines.append(f'# Module: {module_json["module_name"]}')
|
||||
lines.extend(generate_features_rules(module_json['features']))
|
||||
return lines
|
||||
|
||||
|
||||
@cli.argument('filename', nargs='?', arg_only=True, type=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.')
|
||||
@cli.argument('-o', '--output', arg_only=True, type=normpath, help='File to write to')
|
||||
@cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
|
||||
|
@ -80,10 +117,7 @@ def generate_rules_mk(cli):
|
|||
|
||||
# Iterate through features to enable/disable them
|
||||
if 'features' in kb_info_json:
|
||||
for feature, enabled in kb_info_json['features'].items():
|
||||
feature = feature.upper()
|
||||
enabled = 'yes' if enabled else 'no'
|
||||
rules_mk_lines.append(generate_rule(f'{feature}_ENABLE', enabled))
|
||||
rules_mk_lines.extend(generate_features_rules(kb_info_json['features']))
|
||||
|
||||
# Set SPLIT_TRANSPORT, if needed
|
||||
if kb_info_json.get('split', {}).get('transport', {}).get('protocol') == 'custom':
|
||||
|
@ -99,6 +133,8 @@ def generate_rules_mk(cli):
|
|||
if converter:
|
||||
rules_mk_lines.append(generate_rule('CONVERT_TO', converter))
|
||||
|
||||
rules_mk_lines.extend(generate_modules_rules(cli.args.keyboard, cli.args.filename))
|
||||
|
||||
# Show the results
|
||||
dump_lines(cli.args.output, rules_mk_lines)
|
||||
|
||||
|
|
|
@ -52,6 +52,11 @@ def show_keymap(kb_info_json, title_caps=True):
|
|||
|
||||
if keymap_path and keymap_path.suffix == '.json':
|
||||
keymap_data = json.load(keymap_path.open(encoding='utf-8'))
|
||||
|
||||
# cater for layout-less keymap.json
|
||||
if 'layout' not in keymap_data:
|
||||
return
|
||||
|
||||
layout_name = keymap_data['layout']
|
||||
layout_name = kb_info_json.get('layout_aliases', {}).get(layout_name, layout_name) # Resolve alias names
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue