diff options
Diffstat (limited to 'lib/python')
-rw-r--r-- | lib/python/qmk/cli/__init__.py | 1 | ||||
-rwxr-xr-x | lib/python/qmk/cli/generate/make_dependencies.py | 3 | ||||
-rw-r--r-- | lib/python/qmk/cli/lint.py | 35 | ||||
-rw-r--r-- | lib/python/qmk/cli/new/keyboard.py | 20 | ||||
-rw-r--r-- | lib/python/qmk/cli/painter/convert_graphics.py | 34 | ||||
-rw-r--r-- | lib/python/qmk/cli/painter/make_font.py | 36 | ||||
-rw-r--r-- | lib/python/qmk/cli/test/__init__.py | 0 | ||||
-rw-r--r-- | lib/python/qmk/cli/test/c.py | 47 | ||||
-rw-r--r-- | lib/python/qmk/info.py | 51 | ||||
-rw-r--r-- | lib/python/qmk/keyboard.py | 12 | ||||
-rw-r--r-- | lib/python/qmk/painter.py | 102 | ||||
-rw-r--r-- | lib/python/qmk/painter_qgf.py | 26 | ||||
-rw-r--r-- | lib/python/qmk/path.py | 3 |
13 files changed, 283 insertions, 87 deletions
diff --git a/lib/python/qmk/cli/__init__.py b/lib/python/qmk/cli/__init__.py index e4a8166349..6d05a5fc21 100644 --- a/lib/python/qmk/cli/__init__.py +++ b/lib/python/qmk/cli/__init__.py @@ -81,6 +81,7 @@ subcommands = [ 'qmk.cli.new.keymap', 'qmk.cli.painter', 'qmk.cli.pytest', + 'qmk.cli.test.c', 'qmk.cli.userspace.add', 'qmk.cli.userspace.compile', 'qmk.cli.userspace.doctor', diff --git a/lib/python/qmk/cli/generate/make_dependencies.py b/lib/python/qmk/cli/generate/make_dependencies.py index 9b695e907d..331132a20f 100755 --- a/lib/python/qmk/cli/generate/make_dependencies.py +++ b/lib/python/qmk/cli/generate/make_dependencies.py @@ -18,10 +18,11 @@ from qmk.path import normpath, FileType @cli.argument('-km', '--keymap', completer=keymap_completer, help='The keymap to build a firmware for. Ignored when a configurator export is supplied.') @cli.subcommand('Generates the list of dependencies associated with a keyboard build and its generated files.', hidden=True) def generate_make_dependencies(cli): - """Generates the list of dependent info.json, rules.mk, and config.h files for a keyboard. + """Generates the list of dependent config files for a keyboard. """ interesting_files = [ 'info.json', + 'keyboard.json', 'rules.mk', 'post_rules.mk', 'config.h', diff --git a/lib/python/qmk/cli/lint.py b/lib/python/qmk/cli/lint.py index 7ebb0cf9c4..ba0c3f274c 100644 --- a/lib/python/qmk/cli/lint.py +++ b/lib/python/qmk/cli/lint.py @@ -6,9 +6,9 @@ from milc import cli from qmk.decorators import automagic_keyboard, automagic_keymap from qmk.info import info_json -from qmk.keyboard import keyboard_completer, list_keyboards +from qmk.keyboard import keyboard_completer, keyboard_folder_or_all, is_all_keyboards, list_keyboards from qmk.keymap import locate_keymap, list_keymaps -from qmk.path import is_keyboard, keyboard +from qmk.path import keyboard from qmk.git import git_get_ignored_files from qmk.c_parse import c_source_files @@ -198,39 +198,34 @@ def keyboard_check(kb): @cli.argument('--strict', action='store_true', help='Treat warnings as errors') [email protected]('-kb', '--keyboard', completer=keyboard_completer, help='Comma separated list of keyboards to check') [email protected]('-kb', '--keyboard', action='append', type=keyboard_folder_or_all, completer=keyboard_completer, help='Keyboard to check. May be passed multiple times.') @cli.argument('-km', '--keymap', help='The keymap to check') [email protected]('--all-kb', action='store_true', arg_only=True, help='Check all keyboards') [email protected]('--all-km', action='store_true', arg_only=True, help='Check all keymaps') @cli.subcommand('Check keyboard and keymap for common mistakes.') @automagic_keyboard @automagic_keymap def lint(cli): """Check keyboard and keymap for common mistakes. """ - failed = [] - # Determine our keyboard list - if cli.args.all_kb: - if cli.args.keyboard: - cli.log.warning('Both --all-kb and --keyboard passed, --all-kb takes precedence.') - - keyboard_list = list_keyboards() - elif not cli.config.lint.keyboard: - cli.log.error('Missing required arguments: --keyboard or --all-kb') + if not cli.config.lint.keyboard: + cli.log.error('Missing required arguments: --keyboard') cli.print_help() return False + + if isinstance(cli.config.lint.keyboard, str): + # if provided via config - string not array + keyboard_list = [cli.config.lint.keyboard] + elif is_all_keyboards(cli.args.keyboard[0]): + keyboard_list = list_keyboards() else: - keyboard_list = cli.config.lint.keyboard.split(',') + keyboard_list = cli.config.lint.keyboard + + failed = [] # Lint each keyboard for kb in keyboard_list: - if not is_keyboard(kb): - cli.log.error('No such keyboard: %s', kb) - continue - # Determine keymaps to also check - if cli.args.all_km: + if cli.args.keymap == 'all': keymaps = list_keymaps(kb) elif cli.config.lint.keymap: keymaps = {cli.config.lint.keymap} diff --git a/lib/python/qmk/cli/new/keyboard.py b/lib/python/qmk/cli/new/keyboard.py index cb50acf8bb..37bf2923d6 100644 --- a/lib/python/qmk/cli/new/keyboard.py +++ b/lib/python/qmk/cli/new/keyboard.py @@ -74,6 +74,10 @@ def replace_placeholders(src, dest, tokens): dest.write_text(content) +def replace_string(src, token, value): + src.write_text(src.read_text().replace(token, value)) + + def augment_community_info(src, dest): """Splice in any additional data into info.json """ @@ -218,6 +222,11 @@ def new_keyboard(cli): else: bootloader = select_default_bootloader(mcu) + detach_layout = False + if default_layout == 'none of the above': + default_layout = "ortho_4x4" + detach_layout = True + tokens = { # Comment here is to force multiline formatting 'YEAR': str(date.today().year), 'KEYBOARD': kb_name, @@ -233,10 +242,6 @@ def new_keyboard(cli): for key, value in tokens.items(): cli.echo(f" {key.ljust(10)}: {value}") - # TODO: detach community layout and rename to just "LAYOUT" - if default_layout == 'none of the above': - default_layout = "ortho_4x4" - # begin with making the deepest folder in the tree keymaps_path = keyboard(kb_name) / 'keymaps/' keymaps_path.mkdir(parents=True) @@ -251,7 +256,12 @@ def new_keyboard(cli): # merge in infos community_info = Path(COMMUNITY / f'{default_layout}/info.json') - augment_community_info(community_info, keyboard(kb_name) / community_info.name) + augment_community_info(community_info, keyboard(kb_name) / 'keyboard.json') + + # detach community layout and rename to just "LAYOUT" + if detach_layout: + replace_string(keyboard(kb_name) / 'keyboard.json', 'LAYOUT_ortho_4x4', 'LAYOUT') + replace_string(keymaps_path / 'default/keymap.c', 'LAYOUT_ortho_4x4', 'LAYOUT') cli.log.info(f'{{fg_green}}Created a new keyboard called {{fg_cyan}}{kb_name}{{fg_green}}.{{fg_reset}}') cli.log.info(f"Build Command: {{fg_yellow}}qmk compile -kb {kb_name} -km default{{fg_reset}}.") diff --git a/lib/python/qmk/cli/painter/convert_graphics.py b/lib/python/qmk/cli/painter/convert_graphics.py index 2519c49b25..553c26aa5d 100644 --- a/lib/python/qmk/cli/painter/convert_graphics.py +++ b/lib/python/qmk/cli/painter/convert_graphics.py @@ -1,10 +1,8 @@ """This script tests QGF functionality. """ -import re -import datetime from io import BytesIO from qmk.path import normpath -from qmk.painter import render_header, render_source, render_license, render_bytes, valid_formats +from qmk.painter import generate_subs, render_header, render_source, valid_formats from milc import cli from PIL import Image @@ -12,7 +10,7 @@ from PIL import Image @cli.argument('-v', '--verbose', arg_only=True, action='store_true', help='Turns on verbose output.') @cli.argument('-i', '--input', required=True, help='Specify input graphic file.') @cli.argument('-o', '--output', default='', help='Specify output directory. Defaults to same directory as input.') [email protected]('-f', '--format', required=True, help='Output format, valid types: %s' % (', '.join(valid_formats.keys()))) [email protected]('-f', '--format', required=True, help=f'Output format, valid types: {", ".join(valid_formats.keys())}') @cli.argument('-r', '--no-rle', arg_only=True, action='store_true', help='Disables the use of RLE when encoding images.') @cli.argument('-d', '--no-deltas', arg_only=True, action='store_true', help='Disables the use of delta frames when encoding animations.') @cli.argument('-w', '--raw', arg_only=True, action='store_true', help='Writes out the QGF file as raw data instead of c/h combo.') @@ -51,43 +49,31 @@ def painter_convert_graphics(cli): # Convert the image to QGF using PIL out_data = BytesIO() - input_img.save(out_data, "QGF", use_deltas=(not cli.args.no_deltas), use_rle=(not cli.args.no_rle), qmk_format=format, verbose=cli.args.verbose) + metadata = [] + input_img.save(out_data, "QGF", use_deltas=(not cli.args.no_deltas), use_rle=(not cli.args.no_rle), qmk_format=format, verbose=cli.args.verbose, metadata=metadata) out_bytes = out_data.getvalue() if cli.args.raw: - raw_file = cli.args.output / (cli.args.input.stem + ".qgf") + raw_file = cli.args.output / f"{cli.args.input.stem}.qgf" with open(raw_file, 'wb') as raw: raw.write(out_bytes) return # Work out the text substitutions for rendering the output data - subs = { - 'generated_type': 'image', - 'var_prefix': 'gfx', - 'generator_command': f'qmk painter-convert-graphics -i {cli.args.input.name} -f {cli.args.format}', - 'year': datetime.date.today().strftime("%Y"), - 'input_file': cli.args.input.name, - 'sane_name': re.sub(r"[^a-zA-Z0-9]", "_", cli.args.input.stem), - 'byte_count': len(out_bytes), - 'bytes_lines': render_bytes(out_bytes), - 'format': cli.args.format, - } - - # Render the license - subs.update({'license': render_license(subs)}) + args_str = " ".join((f"--{arg} {getattr(cli.args, arg.replace('-', '_'))}" for arg in ["input", "output", "format", "no-rle", "no-deltas"])) + command = f"qmk painter-convert-graphics {args_str}" + subs = generate_subs(cli, out_bytes, image_metadata=metadata, command=command) # Render and write the header file header_text = render_header(subs) - header_file = cli.args.output / (cli.args.input.stem + ".qgf.h") + header_file = cli.args.output / f"{cli.args.input.stem}.qgf.h" with open(header_file, 'w') as header: print(f"Writing {header_file}...") header.write(header_text) - header.close() # Render and write the source file source_text = render_source(subs) - source_file = cli.args.output / (cli.args.input.stem + ".qgf.c") + source_file = cli.args.output / f"{cli.args.input.stem}.qgf.c" with open(source_file, 'w') as source: print(f"Writing {source_file}...") source.write(source_text) - source.close() diff --git a/lib/python/qmk/cli/painter/make_font.py b/lib/python/qmk/cli/painter/make_font.py index c0189920d2..19db844931 100644 --- a/lib/python/qmk/cli/painter/make_font.py +++ b/lib/python/qmk/cli/painter/make_font.py @@ -1,12 +1,10 @@ """This script automates the conversion of font files into a format QMK firmware understands. """ -import re -import datetime from io import BytesIO from qmk.path import normpath -from qmk.painter_qff import QFFFont -from qmk.painter import render_header, render_source, render_license, render_bytes, valid_formats +from qmk.painter_qff import _generate_font_glyphs_list, QFFFont +from qmk.painter import generate_subs, render_header, render_source, valid_formats from milc import cli @@ -31,7 +29,7 @@ def painter_make_font_image(cli): @cli.argument('-o', '--output', default='', help='Specify output directory. Defaults to same directory as input.') @cli.argument('-n', '--no-ascii', arg_only=True, action='store_true', help='Disables output of the full ASCII character set (0x20..0x7E), exporting only the glyphs specified.') @cli.argument('-u', '--unicode-glyphs', default='', help='Also generate the specified unicode glyphs.') [email protected]('-f', '--format', required=True, help='Output format, valid types: %s' % (', '.join(valid_formats.keys()))) [email protected]('-f', '--format', required=True, help=f'Output format, valid types: {", ".join(valid_formats.keys())}') @cli.argument('-r', '--no-rle', arg_only=True, action='store_true', help='Disable the use of RLE to minimise converted image size.') @cli.argument('-w', '--raw', arg_only=True, action='store_true', help='Writes out the QFF file as raw data instead of c/h combo.') @cli.subcommand('Converts an input font image to something QMK firmware understands') @@ -53,43 +51,31 @@ def painter_convert_font_image(cli): # Render out the data out_data = BytesIO() - font.save_to_qff(format, (False if cli.args.no_rle else True), out_data) + font.save_to_qff(format, not cli.args.no_rle, out_data) out_bytes = out_data.getvalue() if cli.args.raw: - raw_file = cli.args.output / (cli.args.input.stem + ".qff") + raw_file = cli.args.output / f"{cli.args.input.stem}.qff" with open(raw_file, 'wb') as raw: raw.write(out_bytes) return # Work out the text substitutions for rendering the output data - subs = { - 'generated_type': 'font', - 'var_prefix': 'font', - 'generator_command': f'qmk painter-convert-font-image -i {cli.args.input.name} -f {cli.args.format}', - 'year': datetime.date.today().strftime("%Y"), - 'input_file': cli.args.input.name, - 'sane_name': re.sub(r"[^a-zA-Z0-9]", "_", cli.args.input.stem), - 'byte_count': len(out_bytes), - 'bytes_lines': render_bytes(out_bytes), - 'format': cli.args.format, - } - - # Render the license - subs.update({'license': render_license(subs)}) + args_str = " ".join((f"--{arg} {getattr(cli.args, arg.replace('-', '_'))}" for arg in ["input", "output", "no-ascii", "unicode-glyphs", "format", "no-rle"])) + command = f"qmk painter-convert-font-image {args_str}" + metadata = {"glyphs": _generate_font_glyphs_list(not cli.args.no_ascii, cli.args.unicode_glyphs)} + subs = generate_subs(cli, out_bytes, font_metadata=metadata, command=command) # Render and write the header file header_text = render_header(subs) - header_file = cli.args.output / (cli.args.input.stem + ".qff.h") + header_file = cli.args.output / f"{cli.args.input.stem}.qff.h" with open(header_file, 'w') as header: print(f"Writing {header_file}...") header.write(header_text) - header.close() # Render and write the source file source_text = render_source(subs) - source_file = cli.args.output / (cli.args.input.stem + ".qff.c") + source_file = cli.args.output / f"{cli.args.input.stem}.qff.c" with open(source_file, 'w') as source: print(f"Writing {source_file}...") source.write(source_text) - source.close() diff --git a/lib/python/qmk/cli/test/__init__.py b/lib/python/qmk/cli/test/__init__.py new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/lib/python/qmk/cli/test/__init__.py diff --git a/lib/python/qmk/cli/test/c.py b/lib/python/qmk/cli/test/c.py new file mode 100644 index 0000000000..7a4e20d5e6 --- /dev/null +++ b/lib/python/qmk/cli/test/c.py @@ -0,0 +1,47 @@ +import fnmatch +import re +from subprocess import DEVNULL + +from milc import cli + +from qmk.commands import find_make, get_make_parallel_args, build_environment + + [email protected]('-j', '--parallel', type=int, default=1, help="Set the number of parallel make jobs; 0 means unlimited.") [email protected]('-e', '--env', arg_only=True, action='append', default=[], help="Set a variable to be passed to make. May be passed multiple times.") [email protected]('-c', '--clean', arg_only=True, action='store_true', help="Remove object files before compiling.") [email protected]('-l', '--list', arg_only=True, action='store_true', help='List available tests.') [email protected]('-t', '--test', arg_only=True, action='append', default=[], help="Test to run from the available list. Supports wildcard globs. May be passed multiple times.") [email protected]("QMK C Unit Tests.", hidden=False if cli.config.user.developer else True) +def test_c(cli): + """Run native unit tests. + """ + list_tests = cli.run([find_make(), 'list-tests', 'SILENT=true']) + available_tests = sorted(list_tests.stdout.strip().split()) + + if cli.args.list: + return print("\n".join(available_tests)) + + # expand any wildcards + filtered_tests = set() + for test in cli.args.test: + regex = re.compile(fnmatch.translate(test)) + filtered_tests |= set(filter(regex.match, available_tests)) + + for invalid in filtered_tests - set(available_tests): + cli.log.warning(f'Invalid test provided: {invalid}') + + # convert test names to build targets + targets = list(map(lambda x: f'test:{x}', filtered_tests or ['all'])) + + if cli.args.clean: + targets.insert(0, 'clean') + + # Add in the environment vars + for key, value in build_environment(cli.args.env).items(): + targets.append(f'{key}={value}') + + command = [find_make(), *get_make_parallel_args(cli.config.test_c.parallel), *targets] + + cli.log.info('Compiling tests with {fg_cyan}%s', ' '.join(command)) + return cli.run(command, capture_output=False, stdin=DEVNULL).returncode diff --git a/lib/python/qmk/info.py b/lib/python/qmk/info.py index 815b851474..ffc9d57d68 100644 --- a/lib/python/qmk/info.py +++ b/lib/python/qmk/info.py @@ -58,8 +58,13 @@ def _get_key_left_position(key): def _find_invalid_encoder_index(info_data): """Perform additional validation of encoders """ - enc_count = len(info_data.get('encoder', {}).get('rotary', [])) - enc_count += len(info_data.get('split', {}).get('encoder', {}).get('right', {}).get('rotary', [])) + enc_left = info_data.get('encoder', {}).get('rotary', []) + enc_right = [] + + if info_data.get('split', {}).get('enabled', False): + enc_right = info_data.get('split', {}).get('encoder', {}).get('right', {}).get('rotary', enc_left) + + enc_count = len(enc_left) + len(enc_right) ret = [] layouts = info_data.get('layouts', {}) @@ -78,6 +83,25 @@ def _find_invalid_encoder_index(info_data): return ret +def _validate_build_target(keyboard, info_data): + """Non schema checks + """ + keyboard_json_path = Path('keyboards') / keyboard / 'keyboard.json' + config_files = find_info_json(keyboard) + + # keyboard.json can only exist at the deepest part of the tree + keyboard_json_count = 0 + for info_file in config_files: + if info_file.name == 'keyboard.json': + keyboard_json_count += 1 + if info_file != keyboard_json_path: + _log_error(info_data, f'Invalid keyboard.json location detected: {info_file}.') + + # Moving forward keyboard.json should be used as a build target + if keyboard_json_count == 0: + _log_warning(info_data, 'Build marker "keyboard.json" not found.') + + def _validate_layouts(keyboard, info_data): # noqa C901 """Non schema checks """ @@ -107,6 +131,15 @@ def _validate_layouts(keyboard, info_data): # noqa C901 if col >= col_num: _log_error(info_data, f'{layout_name}: Matrix column for key {index} ({key_name}) is {col} but must be less than {col_num}') + # Reject duplicate matrix locations + for layout_name, layout_data in layouts.items(): + seen = set() + for index, key_data in enumerate(layout_data['layout']): + key = f"{key_data['matrix']}" + if key in seen: + _log_error(info_data, f'{layout_name}: Matrix location for key {index} is not unique {key_data}') + seen.add(key) + # Warn if physical positions are offset (at least one key should be at x=0, and at least one key at y=0) for layout_name, layout_data in layouts.items(): offset_x = min([_get_key_left_position(k) for k in layout_data['layout']]) @@ -167,6 +200,7 @@ def _validate(keyboard, info_data): validate(info_data, 'qmk.api.keyboard.v1') # Additional validation + _validate_build_target(keyboard, info_data) _validate_layouts(keyboard, info_data) _validate_keycodes(keyboard, info_data) _validate_encoders(keyboard, info_data) @@ -375,6 +409,12 @@ def _extract_encoders(info_data, config_c): info_data['encoder']['rotary'] = encoders + # TODO: some logic still assumes ENCODER_ENABLED would partially create encoder dict + if info_data.get('features', {}).get('encoder', False): + if 'encoder' not in info_data: + info_data['encoder'] = {} + info_data['encoder']['enabled'] = True + def _extract_split_encoders(info_data, config_c): """Populate data about split encoder pins @@ -868,7 +908,9 @@ def unknown_processor_rules(info_data, rules): def merge_info_jsons(keyboard, info_data): """Return a merged copy of all the info.json files for a keyboard. """ - for info_file in find_info_json(keyboard): + config_files = find_info_json(keyboard) + + for info_file in config_files: # Load and validate the JSON data new_info_data = json_load(info_file) @@ -926,7 +968,7 @@ def find_info_json(keyboard): base_path = Path('keyboards') keyboard_path = base_path / keyboard keyboard_parent = keyboard_path.parent - info_jsons = [keyboard_path / 'info.json'] + info_jsons = [keyboard_path / 'info.json', keyboard_path / 'keyboard.json'] # Add DEFAULT_FOLDER before parents, if present rules = rules_mk(keyboard) @@ -938,6 +980,7 @@ def find_info_json(keyboard): if keyboard_parent == base_path: break info_jsons.append(keyboard_parent / 'info.json') + info_jsons.append(keyboard_parent / 'keyboard.json') keyboard_parent = keyboard_parent.parent # Return a list of the info.json files that actually exist diff --git a/lib/python/qmk/keyboard.py b/lib/python/qmk/keyboard.py index b56505d649..fcf5b5b158 100644 --- a/lib/python/qmk/keyboard.py +++ b/lib/python/qmk/keyboard.py @@ -99,6 +99,8 @@ def find_keyboard_from_dir(): keymap_index = len(current_path.parts) - current_path.parts.index('keymaps') - 1 current_path = current_path.parents[keymap_index] + current_path = resolve_keyboard(current_path) + if qmk.path.is_keyboard(current_path): return str(current_path) @@ -166,9 +168,9 @@ def keyboard_folder_or_all(keyboard): def _find_name(path): - """Determine the keyboard name by stripping off the base_path and rules.mk. + """Determine the keyboard name by stripping off the base_path and filename. """ - return path.replace(base_path, "").replace(os.path.sep + "rules.mk", "") + return path.replace(base_path, "").rsplit(os.path.sep, 1)[0] def keyboard_completer(prefix, action, parser, parsed_args): @@ -181,8 +183,10 @@ def list_keyboards(resolve_defaults=True): """Returns a list of all keyboards - optionally processing any DEFAULT_FOLDER. """ # We avoid pathlib here because this is performance critical code. - kb_wildcard = os.path.join(base_path, "**", "rules.mk") - paths = [path for path in glob(kb_wildcard, recursive=True) if os.path.sep + 'keymaps' + os.path.sep not in path] + paths = [] + for marker in ['rules.mk', 'keyboard.json']: + kb_wildcard = os.path.join(base_path, "**", marker) + paths += [path for path in glob(kb_wildcard, recursive=True) if os.path.sep + 'keymaps' + os.path.sep not in path] found = map(_find_name, paths) if resolve_defaults: diff --git a/lib/python/qmk/painter.py b/lib/python/qmk/painter.py index 381a996443..512a486ce8 100644 --- a/lib/python/qmk/painter.py +++ b/lib/python/qmk/painter.py @@ -1,5 +1,6 @@ """Functions that help us work with Quantum Painter's file formats. """ +import datetime import math import re from string import Template @@ -79,6 +80,105 @@ valid_formats = { } } + +def _render_text(values): + # FIXME: May need more chars with GIFs containing lots of frames (or longer durations) + return "|".join([f"{i:4d}" for i in values]) + + +def _render_numeration(metadata): + return _render_text(range(len(metadata))) + + +def _render_values(metadata, key): + return _render_text([i[key] for i in metadata]) + + +def _render_image_metadata(metadata): + size = metadata.pop(0) + + lines = [ + "// Image's metadata", + "// ----------------", + f"// Width: {size['width']}", + f"// Height: {size['height']}", + ] + + if len(metadata) == 1: + lines.append("// Single frame") + + else: + lines.extend([ + f"// Frame: {_render_numeration(metadata)}", + f"// Duration(ms): {_render_values(metadata, 'delay')}", + f"// Compression: {_render_values(metadata, 'compression')} >> See qp.h, painter_compression_t", + f"// Delta: {_render_values(metadata, 'delta')}", + ]) + + deltas = [] + for i, v in enumerate(metadata): + # Not a delta frame, go to next one + if not v["delta"]: + continue + + # Unpack rect's coords + l, t, r, b = v["delta_rect"] + + delta_px = (r - l) * (b - t) + px = size["width"] * size["height"] + + # FIXME: May need need more chars here too + deltas.append(f"// Frame {i:3d}: ({l:3d}, {t:3d}) - ({r:3d}, {b:3d}) >> {delta_px:4d}/{px:4d} pixels ({100*delta_px/px:.2f}%)") + + if deltas: + lines.append("// Areas on delta frames") + lines.extend(deltas) + + return "\n".join(lines) + + +def generate_subs(cli, out_bytes, *, font_metadata=None, image_metadata=None, command): + if font_metadata is not None and image_metadata is not None: + raise ValueError("Cant generate subs for font and image at the same time") + + subs = { + "year": datetime.date.today().strftime("%Y"), + "input_file": cli.args.input.name, + "sane_name": re.sub(r"[^a-zA-Z0-9]", "_", cli.args.input.stem), + "byte_count": len(out_bytes), + "bytes_lines": render_bytes(out_bytes), + "format": cli.args.format, + "generator_command": command, + } + + if font_metadata is not None: + subs.update({ + "generated_type": "font", + "var_prefix": "font", + # not using triple quotes to avoid extra indentation/weird formatted code + "metadata": "\n".join([ + "// Font's metadata", + "// ---------------", + f"// Glyphs: {', '.join([i for i in font_metadata['glyphs']])}", + ]), + }) + + elif image_metadata is not None: + subs.update({ + "generated_type": "image", + "var_prefix": "gfx", + "generator_command": command, + "metadata": _render_image_metadata(image_metadata), + }) + + else: + raise ValueError("Pass metadata for either an image or a font") + + subs.update({"license": render_license(subs)}) + + return subs + + license_template = """\ // Copyright ${year} QMK -- generated source code only, ${generated_type} retains original copyright // SPDX-License-Identifier: GPL-2.0-or-later @@ -110,6 +210,8 @@ def render_header(subs): source_file_template = """\ ${license} +${metadata} + #include <qp.h> const uint32_t ${var_prefix}_${sane_name}_length = ${byte_count}; diff --git a/lib/python/qmk/painter_qgf.py b/lib/python/qmk/painter_qgf.py index cc4697f1c6..67ef0dd233 100644 --- a/lib/python/qmk/painter_qgf.py +++ b/lib/python/qmk/painter_qgf.py @@ -327,8 +327,9 @@ def _compress_image(frame, last_frame, *, use_rle, use_deltas, format_, **_kwarg # Helper function to save each frame to the output file -def _write_frame(idx, frame, last_frame, *, fp, frame_offsets, **kwargs): - # Not an argument of the function as it would consume from **kwargs +def _write_frame(idx, frame, last_frame, *, fp, frame_offsets, metadata, **kwargs): + # Not an argument of the function as it would then not be part of kwargs + # This would cause an issue with `_compress_image(**kwargs)` missing an argument format_ = kwargs["format_"] # (potentially) Apply RLE and/or delta, and work out output image's information @@ -370,6 +371,21 @@ def _write_frame(idx, frame, last_frame, *, fp, frame_offsets, **kwargs): vprint(f'{f"Frame {idx:3d} delta":26s} {fp.tell():5d}d / {fp.tell():04X}h') delta_descriptor.write(fp) + # Store metadata, showed later in a comment in the generated file + frame_metadata = { + "compression": frame_descriptor.compression, + "delta": frame_descriptor.is_delta, + "delay": frame_descriptor.delay, + } + if frame_metadata["delta"]: + frame_metadata.update({"delta_rect": [ + delta_descriptor.left, + delta_descriptor.top, + delta_descriptor.right, + delta_descriptor.bottom, + ]}) + metadata.append(frame_metadata) + # Write out the data for this frame to the output data_descriptor = QGFFrameDataDescriptorV1() data_descriptor.data = image_data @@ -383,6 +399,10 @@ def _save(im, fp, _filename): # Work out from the parameters if we need to do anything special encoderinfo = im.encoderinfo.copy() + # Store image file in metadata structure + metadata = encoderinfo.get("metadata", []) + metadata.append({"width": im.width, "height": im.height}) + # Helper for prints, noop taking any args if not verbose global vprint verbose = encoderinfo.get("verbose", False) @@ -417,7 +437,7 @@ def _save(im, fp, _filename): frame_offsets.write(fp) # Iterate over each if the input frames, writing it to the output in the process - write_frame = functools.partial(_write_frame, format_=encoderinfo["qmk_format"], fp=fp, use_deltas=encoderinfo.get("use_deltas", True), use_rle=encoderinfo.get("use_rle", True), frame_offsets=frame_offsets) + write_frame = functools.partial(_write_frame, format_=encoderinfo["qmk_format"], fp=fp, use_deltas=encoderinfo.get("use_deltas", True), use_rle=encoderinfo.get("use_rle", True), frame_offsets=frame_offsets, metadata=metadata) for_all_frames(write_frame) # Go back and update the graphics descriptor now that we can determine the final file size diff --git a/lib/python/qmk/path.py b/lib/python/qmk/path.py index bb588d2e1c..61daad585f 100644 --- a/lib/python/qmk/path.py +++ b/lib/python/qmk/path.py @@ -23,8 +23,9 @@ def is_keyboard(keyboard_name): keyboard_path = QMK_FIRMWARE / 'keyboards' / keyboard_name rules_mk = keyboard_path / 'rules.mk' + keyboard_json = keyboard_path / 'keyboard.json' - return rules_mk.exists() + return rules_mk.exists() or keyboard_json.exists() def under_qmk_firmware(path=Path(os.environ['ORIG_CWD'])): |