diff options
Diffstat (limited to 'lib/python/qmk/cli/generate')
-rw-r--r-- | lib/python/qmk/cli/generate/community_modules.py | 75 | ||||
-rw-r--r-- | lib/python/qmk/cli/generate/keycodes.py | 16 | ||||
-rw-r--r-- | lib/python/qmk/cli/generate/keycodes_tests.py | 39 | ||||
-rwxr-xr-x | lib/python/qmk/cli/generate/rules_mk.py | 34 |
4 files changed, 92 insertions, 72 deletions
diff --git a/lib/python/qmk/cli/generate/community_modules.py b/lib/python/qmk/cli/generate/community_modules.py index 23678a2fb5..61e24077d3 100644 --- a/lib/python/qmk/cli/generate/community_modules.py +++ b/lib/python/qmk/cli/generate/community_modules.py @@ -8,7 +8,7 @@ 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.constants import GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE, GPL2_HEADER_SH_LIKE, GENERATED_HEADER_SH_LIKE from qmk.community_modules import module_api_list, load_module_jsons, find_module_path @@ -76,6 +76,8 @@ def _render_api_implementations(api, module): lines.append(f'__attribute__((weak)) {api.ret_type} {api.name}_{module_name}_user({api.args}) {{') if api.ret_type == 'bool': lines.append(' return true;') + elif api.ret_type in ['layer_state_t', 'report_mouse_t']: + lines.append(f' return {api.call_params};') else: pass lines.append('}') @@ -86,6 +88,8 @@ def _render_api_implementations(api, module): if api.ret_type == 'bool': lines.append(f' if(!{api.name}_{module_name}_user({api.call_params})) {{ return false; }}') lines.append(' return true;') + elif api.ret_type in ['layer_state_t', 'report_mouse_t']: + lines.append(f' return {api.name}_{module_name}_user({api.call_params});') else: lines.append(f' {api.name}_{module_name}_user({api.call_params});') lines.append('}') @@ -96,6 +100,8 @@ def _render_api_implementations(api, module): if api.ret_type == 'bool': lines.append(f' if(!{api.name}_{module_name}_kb({api.call_params})) {{ return false; }}') lines.append(' return true;') + elif api.ret_type in ['layer_state_t', 'report_mouse_t']: + lines.append(f' return {api.name}_{module_name}_kb({api.call_params});') else: lines.append(f' {api.name}_{module_name}_kb({api.call_params});') lines.append('}') @@ -113,14 +119,81 @@ def _render_core_implementation(api, modules): module_name = Path(module).name if api.ret_type == 'bool': lines.append(f' && {api.name}_{module_name}({api.call_params})') + elif api.ret_type in ['layer_state_t', 'report_mouse_t']: + lines.append(f' {api.call_params} = {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(' ;') + elif api.ret_type in ['layer_state_t', 'report_mouse_t']: + lines.append(f' return {api.call_params};') lines.append('}') return lines +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(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 = qmk.path.unix_style_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'MODULE_NAME_{module_path.name.upper()} := {module_path.name}') + lines.append(f'MODULE_PATH_{module_path.name.upper()} := {module_path}') + 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 + + [email protected]('-o', '--output', arg_only=True, type=qmk.path.normpath, help='File to write to') [email protected]('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages") [email protected]('-e', '--escape', arg_only=True, action='store_true', help="Escape spaces in quiet mode") [email protected]('-kb', '--keyboard', arg_only=True, type=keyboard_folder, completer=keyboard_completer, help='Keyboard to generate rules.mk for.') [email protected]('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.') [email protected]('Creates a community_modules_rules_mk from a keymap.json file.') +def generate_community_modules_rules_mk(cli): + + rules_mk_lines = [GPL2_HEADER_SH_LIKE, GENERATED_HEADER_SH_LIKE] + + rules_mk_lines.extend(_generate_modules_rules(cli.args.keyboard, cli.args.filename)) + + # Show the results + dump_lines(cli.args.output, rules_mk_lines) + + if cli.args.output: + if cli.args.quiet: + if cli.args.escape: + print(cli.args.output.as_posix().replace(' ', '\\ ')) + else: + print(cli.args.output) + else: + cli.log.info('Wrote rules.mk to %s.', cli.args.output) + + @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.') diff --git a/lib/python/qmk/cli/generate/keycodes.py b/lib/python/qmk/cli/generate/keycodes.py index 719fced5d5..d686935fa8 100644 --- a/lib/python/qmk/cli/generate/keycodes.py +++ b/lib/python/qmk/cli/generate/keycodes.py @@ -125,6 +125,20 @@ def _generate_aliases(lines, keycodes): lines.append(f'#define {alias} {value.get("key")}') +def _generate_version(lines, keycodes, prefix=''): + version = keycodes['version'] + major, minor, patch = map(int, version.split('.')) + + bcd = f'0x{major:02d}{minor:02d}{patch:04d}' + + lines.append('') + lines.append(f'#define QMK_{prefix}KEYCODES_VERSION "{version}"') + lines.append(f'#define QMK_{prefix}KEYCODES_VERSION_BCD {bcd}') + lines.append(f'#define QMK_{prefix}KEYCODES_VERSION_MAJOR {major}') + lines.append(f'#define QMK_{prefix}KEYCODES_VERSION_MINOR {minor}') + lines.append(f'#define QMK_{prefix}KEYCODES_VERSION_PATCH {patch}') + + @cli.argument('-v', '--version', arg_only=True, required=True, help='Version of keycodes to generate.') @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") @@ -138,6 +152,7 @@ def generate_keycodes(cli): keycodes = load_spec(cli.args.version) + _generate_version(keycodes_h_lines, keycodes) _generate_ranges(keycodes_h_lines, keycodes) _generate_defines(keycodes_h_lines, keycodes) _generate_helpers(keycodes_h_lines, keycodes) @@ -160,6 +175,7 @@ def generate_keycode_extras(cli): keycodes = load_spec(cli.args.version, cli.args.lang) + _generate_version(keycodes_h_lines, keycodes, f'{cli.args.lang.upper()}_') _generate_aliases(keycodes_h_lines, keycodes) # Show the results diff --git a/lib/python/qmk/cli/generate/keycodes_tests.py b/lib/python/qmk/cli/generate/keycodes_tests.py deleted file mode 100644 index 453b4693a7..0000000000 --- a/lib/python/qmk/cli/generate/keycodes_tests.py +++ /dev/null @@ -1,39 +0,0 @@ -"""Used by the make system to generate a keycode lookup table from keycodes_{version}.json -""" -from milc import cli - -from qmk.constants import GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE -from qmk.commands import dump_lines -from qmk.path import normpath -from qmk.keycodes import load_spec - - -def _generate_defines(lines, keycodes): - lines.append('') - lines.append('std::map<uint16_t, std::string> KEYCODE_ID_TABLE = {') - for key, value in keycodes["keycodes"].items(): - lines.append(f' {{{value.get("key")}, "{value.get("key")}"}},') - lines.append('};') - - [email protected]('-v', '--version', arg_only=True, required=True, help='Version of keycodes to generate.') [email protected]('-o', '--output', arg_only=True, type=normpath, help='File to write to') [email protected]('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages") [email protected]('Used by the make system to generate a keycode lookup table from keycodes_{version}.json', hidden=True) -def generate_keycodes_tests(cli): - """Generates a keycode to identifier lookup table for unit test output. - """ - - # Build the keycodes.h file. - keycodes_h_lines = [GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE, '// clang-format off'] - keycodes_h_lines.append('extern "C" {\n#include <keycode.h>\n}') - keycodes_h_lines.append('#include <map>') - keycodes_h_lines.append('#include <string>') - keycodes_h_lines.append('#include <cstdint>') - - keycodes = load_spec(cli.args.version) - - _generate_defines(keycodes_h_lines, keycodes) - - # Show the results - dump_lines(cli.args.output, keycodes_h_lines, cli.args.quiet) diff --git a/lib/python/qmk/cli/generate/rules_mk.py b/lib/python/qmk/cli/generate/rules_mk.py index ef4101d77f..358a22fd1d 100755 --- a/lib/python/qmk/cli/generate/rules_mk.py +++ b/lib/python/qmk/cli/generate/rules_mk.py @@ -6,13 +6,12 @@ from dotty_dict import dotty from argcomplete.completers import FilesCompleter from milc import cli -from qmk.info import info_json, get_modules +from qmk.info import info_json 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, unix_style_path, FileType +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): @@ -56,33 +55,6 @@ def generate_features_rules(features_dict): 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 = unix_style_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") @@ -133,8 +105,6 @@ 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) |