Merge tag '0.20.1' into firmware23

This commit is contained in:
Drashna Jael're
2023-03-28 00:10:55 -07:00
741 changed files with 54879 additions and 16174 deletions
+5 -3
View File
@@ -88,7 +88,9 @@ def find_layouts(file):
for i, key in enumerate(parsed_layout):
if 'label' not in key:
cli.log.error('Invalid LAYOUT macro in %s: Empty parameter name in macro %s at pos %s.', file, macro_name, i)
elif key['label'] in matrix_locations:
elif key['label'] not in matrix_locations:
cli.log.error('Invalid LAYOUT macro in %s: Key %s in macro %s has no matrix position!', file, key['label'], macro_name)
else:
key['matrix'] = matrix_locations[key['label']]
parsed_layouts[macro_name] = {
@@ -216,9 +218,9 @@ def _validate_led_config(matrix, matrix_rows, matrix_indexes, position, position
if len(matrix) != matrix_rows and len(matrix) != (matrix_rows / 2):
raise ValueError("Unable to parse g_led_config matrix data")
if len(position) != len(flags):
raise ValueError("Unable to parse g_led_config position data")
raise ValueError(f"Number of g_led_config physical positions ({len(position)}) does not match number of flags ({len(flags)})")
if len(matrix_indexes) and (max(matrix_indexes) >= len(flags)):
raise ValueError("OOB within g_led_config matrix data")
raise ValueError(f"LED index {max(matrix_indexes)} is OOB in g_led_config - should be < {len(flags)}")
if not all(isinstance(n, int) for n in matrix_indexes):
raise ValueError("matrix indexes are not all ints")
if (len(position_raw) % 2) != 0:
+7 -6
View File
@@ -34,19 +34,18 @@ subcommands = [
'qmk.cli.bux',
'qmk.cli.c2json',
'qmk.cli.cd',
'qmk.cli.cformat',
'qmk.cli.chibios.confmigrate',
'qmk.cli.clean',
'qmk.cli.compile',
'qmk.cli.docs',
'qmk.cli.doctor',
'qmk.cli.fileformat',
'qmk.cli.flash',
'qmk.cli.format.c',
'qmk.cli.format.json',
'qmk.cli.format.python',
'qmk.cli.format.text',
'qmk.cli.generate.api',
'qmk.cli.generate.autocorrect_data',
'qmk.cli.generate.compilation_database',
'qmk.cli.generate.config_h',
'qmk.cli.generate.develop_pr_list',
@@ -55,10 +54,12 @@ subcommands = [
'qmk.cli.generate.info_json',
'qmk.cli.generate.keyboard_c',
'qmk.cli.generate.keyboard_h',
'qmk.cli.generate.layouts',
'qmk.cli.generate.keycodes',
'qmk.cli.generate.keycodes_tests',
'qmk.cli.generate.rgb_breathe_table',
'qmk.cli.generate.rules_mk',
'qmk.cli.generate.version_h',
'qmk.cli.git.submodule',
'qmk.cli.hello',
'qmk.cli.import.kbfirmware',
'qmk.cli.import.keyboard',
@@ -66,15 +67,15 @@ subcommands = [
'qmk.cli.info',
'qmk.cli.json2c',
'qmk.cli.lint',
'qmk.cli.kle2json',
'qmk.cli.list.keyboards',
'qmk.cli.list.keymaps',
'qmk.cli.list.layouts',
'qmk.cli.kle2json',
'qmk.cli.multibuild',
'qmk.cli.mass_compile',
'qmk.cli.migrate',
'qmk.cli.new.keyboard',
'qmk.cli.new.keymap',
'qmk.cli.painter',
'qmk.cli.pyformat',
'qmk.cli.pytest',
'qmk.cli.via2json',
]
+1 -1
View File
@@ -34,7 +34,7 @@ def bux(cli):
@B _y ]# ,c vUWNWWPsfsssN9WyccnckAfUfWb0DR0&R5RRRddq2_ `@D`jr@2U@#c3@1@Qc- B@
@B !7! .r]` }AE0RdRqNd9dNR9fUIzzosPqqAddNNdER9EE9dPy! BQ!zy@iU@.Q@@y@8x- B@
@B :****>. '7adddDdR&gRNdRbd&dNNbbRdNdd5NdRRD0RSf}- .k0&EW`xR .8Q=NRRx B@
@B =**-rx*r}r~}" ;n2jkzsf3N3zsKsP5dddRddddRddNNqPzy\" '~****" B@
@B =**-rx*r}r~}" ;n2jkzsf3N3zsKsP5dddRddddRddNNqPzy\\" '~****" B@
@B :!!~!;=~r>:*_ `:^vxikylulKfHkyjzzozoIoklix|^!-` B@
@B ```'-_""::::!:_-.`` B@
@B `- .` B@
-28
View File
@@ -1,28 +0,0 @@
"""Point people to the new command name.
"""
import sys
from pathlib import Path
from milc import cli
@cli.argument('-n', '--dry-run', arg_only=True, action='store_true', help="Flag only, don't automatically format.")
@cli.argument('-b', '--base-branch', default='origin/master', help='Branch to compare to diffs to.')
@cli.argument('-a', '--all-files', arg_only=True, action='store_true', help='Format all core files.')
@cli.argument('--core-only', arg_only=True, action='store_true', help='Format core files only.')
@cli.argument('files', nargs='*', arg_only=True, help='Filename(s) to format.')
@cli.subcommand('Pointer to the new command name: qmk format-c.', hidden=True)
def cformat(cli):
"""Pointer to the new command name: qmk format-c.
"""
cli.log.warning('"qmk cformat" has been renamed to "qmk format-c". Please use the new command in the future.')
argv = [sys.executable, *sys.argv]
argv[argv.index('cformat')] = 'format-c'
script_path = Path(argv[1])
script_path_exe = Path(f'{argv[1]}.exe')
if not script_path.exists() and script_path_exe.exists():
# For reasons I don't understand ".exe" is stripped from the script name on windows.
argv[1] = str(script_path_exe)
return cli.run(argv, capture_output=False).returncode
+35 -37
View File
@@ -2,16 +2,25 @@
You can compile a keymap already in the repo or using a QMK Configurator export.
"""
from subprocess import DEVNULL
from argcomplete.completers import FilesCompleter
from milc import cli
import qmk.path
from qmk.decorators import automagic_keyboard, automagic_keymap
from qmk.commands import compile_configurator_json, create_make_command, parse_configurator_json
from qmk.commands import compile_configurator_json, create_make_command, parse_configurator_json, build_environment
from qmk.keyboard import keyboard_completer, keyboard_folder
from qmk.keymap import keymap_completer
from qmk.keymap import keymap_completer, locate_keymap
def _is_keymap_target(keyboard, keymap):
if keymap == 'all':
return True
if locate_keymap(keyboard, keymap):
return True
return False
@cli.argument('filename', nargs='?', arg_only=True, type=qmk.path.FileType('r'), completer=FilesCompleter('.json'), help='The configurator export to compile')
@@ -31,48 +40,37 @@ def compile(cli):
If a keyboard and keymap are provided this command will build a firmware based on that.
"""
if cli.args.clean and not cli.args.filename and not cli.args.dry_run:
if cli.config.compile.keyboard and cli.config.compile.keymap:
command = create_make_command(cli.config.compile.keyboard, cli.config.compile.keymap, 'clean')
cli.run(command, capture_output=False, stdin=DEVNULL)
# Build the environment vars
envs = {}
for env in cli.args.env:
if '=' in env:
key, value = env.split('=', 1)
envs[key] = value
else:
cli.log.warning('Invalid environment variable: %s', env)
envs = build_environment(cli.args.env)
# Determine the compile command
command = None
commands = []
if cli.args.filename:
# If a configurator JSON was provided generate a keymap and compile it
user_keymap = parse_configurator_json(cli.args.filename)
command = compile_configurator_json(user_keymap, parallel=cli.config.compile.parallel, **envs)
commands = [compile_configurator_json(user_keymap, parallel=cli.config.compile.parallel, clean=cli.args.clean, **envs)]
else:
if cli.config.compile.keyboard and cli.config.compile.keymap:
# Generate the make command for a specific keyboard/keymap.
command = create_make_command(cli.config.compile.keyboard, cli.config.compile.keymap, parallel=cli.config.compile.parallel, **envs)
elif cli.config.compile.keyboard and cli.config.compile.keymap:
# Generate the make command for a specific keyboard/keymap.
if not _is_keymap_target(cli.config.compile.keyboard, cli.config.compile.keymap):
cli.log.error('Invalid keymap argument.')
cli.print_help()
return False
elif not cli.config.compile.keyboard:
cli.log.error('Could not determine keyboard!')
elif not cli.config.compile.keymap:
cli.log.error('Could not determine keymap!')
if cli.args.clean:
commands.append(create_make_command(cli.config.compile.keyboard, cli.config.compile.keymap, 'clean', **envs))
commands.append(create_make_command(cli.config.compile.keyboard, cli.config.compile.keymap, parallel=cli.config.compile.parallel, **envs))
# Compile the firmware, if we're able to
if command:
cli.log.info('Compiling keymap with {fg_cyan}%s', ' '.join(command))
if not cli.args.dry_run:
cli.echo('\n')
# FIXME(skullydazed/anyone): Remove text=False once milc 1.0.11 has had enough time to be installed everywhere.
compile = cli.run(command, capture_output=False, text=False)
return compile.returncode
else:
if not commands:
cli.log.error('You must supply a configurator export, both `--keyboard` and `--keymap`, or be in a directory for a keyboard or keymap.')
cli.echo('usage: qmk compile [-h] [-b] [-kb KEYBOARD] [-km KEYMAP] [filename]')
cli.print_help()
return False
cli.log.info('Compiling keymap with {fg_cyan}%s', ' '.join(commands[-1]))
if not cli.args.dry_run:
cli.echo('\n')
for command in commands:
ret = cli.run(command, capture_output=False)
if ret.returncode:
return ret.returncode
+60 -33
View File
@@ -3,7 +3,7 @@
from enum import Enum
import re
import shutil
from subprocess import DEVNULL
from subprocess import DEVNULL, TimeoutExpired
from milc import cli
from qmk import submodules
@@ -41,9 +41,8 @@ def _parse_gcc_version(version):
def _check_arm_gcc_version():
"""Returns True if the arm-none-eabi-gcc version is not known to cause problems.
"""
if 'output' in ESSENTIAL_BINARIES['arm-none-eabi-gcc']:
version_number = ESSENTIAL_BINARIES['arm-none-eabi-gcc']['output'].strip()
cli.log.info('Found arm-none-eabi-gcc version %s', version_number)
version_number = ESSENTIAL_BINARIES['arm-none-eabi-gcc']['output'].strip()
cli.log.info('Found arm-none-eabi-gcc version %s', version_number)
return CheckStatus.OK # Right now all known arm versions are ok
@@ -51,44 +50,37 @@ def _check_arm_gcc_version():
def _check_avr_gcc_version():
"""Returns True if the avr-gcc version is not known to cause problems.
"""
rc = CheckStatus.ERROR
if 'output' in ESSENTIAL_BINARIES['avr-gcc']:
version_number = ESSENTIAL_BINARIES['avr-gcc']['output'].strip()
version_number = ESSENTIAL_BINARIES['avr-gcc']['output'].strip()
cli.log.info('Found avr-gcc version %s', version_number)
cli.log.info('Found avr-gcc version %s', version_number)
rc = CheckStatus.OK
parsed_version = _parse_gcc_version(version_number)
if parsed_version['major'] > 8:
cli.log.warning('{fg_yellow}We do not recommend avr-gcc newer than 8. Downgrading to 8.x is recommended.')
return CheckStatus.WARNING
parsed_version = _parse_gcc_version(version_number)
if parsed_version['major'] > 8:
cli.log.warning('{fg_yellow}We do not recommend avr-gcc newer than 8. Downgrading to 8.x is recommended.')
rc = CheckStatus.WARNING
return rc
return CheckStatus.OK
def _check_avrdude_version():
if 'output' in ESSENTIAL_BINARIES['avrdude']:
last_line = ESSENTIAL_BINARIES['avrdude']['output'].split('\n')[-2]
version_number = last_line.split()[2][:-1]
cli.log.info('Found avrdude version %s', version_number)
last_line = ESSENTIAL_BINARIES['avrdude']['output'].split('\n')[-2]
version_number = last_line.split()[2][:-1]
cli.log.info('Found avrdude version %s', version_number)
return CheckStatus.OK
def _check_dfu_util_version():
if 'output' in ESSENTIAL_BINARIES['dfu-util']:
first_line = ESSENTIAL_BINARIES['dfu-util']['output'].split('\n')[0]
version_number = first_line.split()[1]
cli.log.info('Found dfu-util version %s', version_number)
first_line = ESSENTIAL_BINARIES['dfu-util']['output'].split('\n')[0]
version_number = first_line.split()[1]
cli.log.info('Found dfu-util version %s', version_number)
return CheckStatus.OK
def _check_dfu_programmer_version():
if 'output' in ESSENTIAL_BINARIES['dfu-programmer']:
first_line = ESSENTIAL_BINARIES['dfu-programmer']['output'].split('\n')[0]
version_number = first_line.split()[1]
cli.log.info('Found dfu-programmer version %s', version_number)
first_line = ESSENTIAL_BINARIES['dfu-programmer']['output'].split('\n')[0]
version_number = first_line.split()[1]
cli.log.info('Found dfu-programmer version %s', version_number)
return CheckStatus.OK
@@ -96,11 +88,16 @@ def _check_dfu_programmer_version():
def check_binaries():
"""Iterates through ESSENTIAL_BINARIES and tests them.
"""
ok = True
ok = CheckStatus.OK
for binary in sorted(ESSENTIAL_BINARIES):
if not is_executable(binary):
ok = False
try:
if not is_executable(binary):
ok = CheckStatus.ERROR
except TimeoutExpired:
cli.log.debug('Timeout checking %s', binary)
if ok != CheckStatus.ERROR:
ok = CheckStatus.WARNING
return ok
@@ -108,8 +105,22 @@ def check_binaries():
def check_binary_versions():
"""Check the versions of ESSENTIAL_BINARIES
"""
checks = {
'arm-none-eabi-gcc': _check_arm_gcc_version,
'avr-gcc': _check_avr_gcc_version,
'avrdude': _check_avrdude_version,
'dfu-util': _check_dfu_util_version,
'dfu-programmer': _check_dfu_programmer_version,
}
versions = []
for check in (_check_arm_gcc_version, _check_avr_gcc_version, _check_avrdude_version, _check_dfu_util_version, _check_dfu_programmer_version):
for binary in sorted(ESSENTIAL_BINARIES):
if 'output' not in ESSENTIAL_BINARIES[binary]:
cli.log.warning('Unknown version for %s', binary)
versions.append(CheckStatus.WARNING)
continue
check = checks[binary]
versions.append(check())
return versions
@@ -119,10 +130,8 @@ def check_submodules():
"""
for submodule in submodules.status().values():
if submodule['status'] is None:
cli.log.error('Submodule %s has not yet been cloned!', submodule['name'])
return CheckStatus.ERROR
elif not submodule['status']:
cli.log.warning('Submodule %s is not up to date!', submodule['name'])
return CheckStatus.WARNING
return CheckStatus.OK
@@ -149,3 +158,21 @@ def is_executable(command):
cli.log.error("{fg_red}Can't run `%s %s`", command, version_arg)
return False
def release_info(file='/etc/os-release'):
"""Parse release info to dict
"""
ret = {}
try:
with open(file) as f:
for line in f:
if '=' in line:
key, value = map(str.strip, line.split('=', 1))
if value.startswith('"') and value.endswith('"'):
value = value[1:-1]
ret[key] = value
except (PermissionError, FileNotFoundError):
pass
return ret
+23 -11
View File
@@ -7,7 +7,11 @@ from pathlib import Path
from milc import cli
from qmk.constants import QMK_FIRMWARE, BOOTLOADER_VIDS_PIDS
from .check import CheckStatus
from .check import CheckStatus, release_info
def _is_wsl():
return 'microsoft' in platform.uname().release.lower()
def _udev_rule(vid, pid=None, *args):
@@ -78,10 +82,13 @@ def check_udev_rules():
# Collect all rules from the config files
for rule_file in udev_rules:
for line in rule_file.read_text(encoding='utf-8').split('\n'):
line = line.strip()
if not line.startswith("#") and len(line):
current_rules.add(line)
try:
for line in rule_file.read_text(encoding='utf-8').split('\n'):
line = line.strip()
if not line.startswith("#") and len(line):
current_rules.add(line)
except PermissionError:
cli.log.debug("Failed to read: %s", rule_file)
# Check if the desired rules are among the currently present rules
for bootloader, rules in desired_rules.items():
@@ -127,17 +134,22 @@ def check_modem_manager():
def os_test_linux():
"""Run the Linux specific tests.
"""
# Don't bother with udev on WSL, for now
if 'microsoft' in platform.uname().release.lower():
cli.log.info("Detected {fg_cyan}Linux (WSL){fg_reset}.")
info = release_info()
release_id = info.get('PRETTY_NAME', info.get('ID', 'Unknown'))
plat = 'WSL, ' if _is_wsl() else ''
cli.log.info(f"Detected {{fg_cyan}}Linux ({plat}{release_id}){{fg_reset}}.")
# Don't bother with udev on WSL, for now
if _is_wsl():
# https://github.com/microsoft/WSL/issues/4197
if QMK_FIRMWARE.as_posix().startswith("/mnt"):
cli.log.warning("I/O performance on /mnt may be extremely slow.")
return CheckStatus.WARNING
return CheckStatus.OK
else:
cli.log.info("Detected {fg_cyan}Linux{fg_reset}.")
rc = check_udev_rules()
if rc != CheckStatus.OK:
return rc
return check_udev_rules()
return CheckStatus.OK
+5 -3
View File
@@ -119,13 +119,15 @@ def doctor(cli):
# Make sure the basic CLI tools we need are available and can be executed.
bin_ok = check_binaries()
if not bin_ok:
if bin_ok == CheckStatus.ERROR:
if yesno('Would you like to install dependencies?', default=True):
cli.run(['util/qmk_install.sh', '-y'], stdin=DEVNULL, capture_output=False)
bin_ok = check_binaries()
if bin_ok:
if bin_ok == CheckStatus.OK:
cli.log.info('All dependencies are installed.')
elif bin_ok == CheckStatus.WARNING:
cli.log.warning('Issues encountered while checking dependencies.')
else:
status = CheckStatus.ERROR
@@ -142,7 +144,7 @@ def doctor(cli):
if sub_ok == CheckStatus.OK:
cli.log.info('Submodules are up to date.')
else:
if yesno('Would you like to clone the submodules?', default=True):
if git_check_repo() and yesno('Would you like to clone the submodules?', default=True):
submodules.update()
sub_ok = check_submodules()
+7 -1
View File
@@ -2,7 +2,7 @@ import platform
from milc import cli
from .check import CheckStatus
from .check import CheckStatus, release_info
def os_test_windows():
@@ -11,4 +11,10 @@ def os_test_windows():
win32_ver = platform.win32_ver()
cli.log.info("Detected {fg_cyan}Windows %s (%s){fg_reset}.", win32_ver[0], win32_ver[1])
# MSYS really does not like "/" files - resolve manually
file = cli.run(['cygpath', '-m', '/etc/qmk-release']).stdout.strip()
qmk_distro_version = release_info(file).get('VERSION', None)
if qmk_distro_version:
cli.log.info('QMK MSYS version: %s', qmk_distro_version)
return CheckStatus.OK
-23
View File
@@ -1,23 +0,0 @@
"""Point people to the new command name.
"""
import sys
from pathlib import Path
from milc import cli
@cli.subcommand('Pointer to the new command name: qmk format-text.', hidden=True)
def fileformat(cli):
"""Pointer to the new command name: qmk format-text.
"""
cli.log.warning('"qmk fileformat" has been renamed to "qmk format-text". Please use the new command in the future.')
argv = [sys.executable, *sys.argv]
argv[argv.index('fileformat')] = 'format-text'
script_path = Path(argv[1])
script_path_exe = Path(f'{argv[1]}.exe')
if not script_path.exists() and script_path_exe.exists():
# For reasons I don't understand ".exe" is stripped from the script name on windows.
argv[1] = str(script_path_exe)
return cli.run(argv, capture_output=False).returncode
+66 -68
View File
@@ -3,22 +3,32 @@
You can compile a keymap already in the repo or using a QMK Configurator export.
A bootloader must be specified.
"""
from subprocess import DEVNULL
import sys
from argcomplete.completers import FilesCompleter
from milc import cli
import qmk.path
from qmk.decorators import automagic_keyboard, automagic_keymap
from qmk.commands import compile_configurator_json, create_make_command, parse_configurator_json
from qmk.commands import compile_configurator_json, create_make_command, parse_configurator_json, build_environment
from qmk.keyboard import keyboard_completer, keyboard_folder
from qmk.keymap import keymap_completer, locate_keymap
from qmk.flashers import flasher
def print_bootloader_help():
def _is_keymap_target(keyboard, keymap):
if keymap == 'all':
return True
if locate_keymap(keyboard, keymap):
return True
return False
def _list_bootloaders():
"""Prints the available bootloaders listed in docs.qmk.fm.
"""
cli.print_help()
cli.log.info('Here are the available bootloaders:')
cli.echo('\tavrdude')
cli.echo('\tbootloadhid')
@@ -38,14 +48,29 @@ def print_bootloader_help():
cli.echo('\tuf2-split-left')
cli.echo('\tuf2-split-right')
cli.echo('For more info, visit https://docs.qmk.fm/#/flashing')
return False
def _flash_binary(filename, mcu):
"""Try to flash binary firmware
"""
cli.echo('Flashing binary firmware...\nPlease reset your keyboard into bootloader mode now!\nPress Ctrl-C to exit.\n')
try:
err, msg = flasher(mcu, filename)
if err:
cli.log.error(msg)
return False
except KeyboardInterrupt:
cli.log.info('Ctrl-C was pressed, exiting...')
return True
@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.')
@cli.argument('-b', '--bootloaders', action='store_true', help='List the available bootloaders.')
@cli.argument('-bl', '--bootloader', default='flash', help='The flash command, corresponding to qmk\'s make options of bootloaders.')
@cli.argument('-m', '--mcu', help='The MCU name. Required for HalfKay, HID, USBAspLoader and ISP flashing.')
@cli.argument('-km', '--keymap', help='The keymap to build a firmware for. Use this if you dont have a configurator file. Ignored when a configurator file is supplied.')
@cli.argument('-kb', '--keyboard', type=keyboard_folder, completer=keyboard_completer, help='The keyboard to build a firmware for. Use this if you dont have a configurator file. Ignored when a configurator file is supplied.')
@cli.argument('-kb', '--keyboard', type=keyboard_folder, completer=keyboard_completer, help='The keyboard to build a firmware for. Ignored when a configurator export is supplied.')
@cli.argument('-km', '--keymap', completer=keymap_completer, help='The keymap to build a firmware for. Ignored when a configurator export is supplied.')
@cli.argument('-n', '--dry-run', arg_only=True, action='store_true', help="Don't actually build, just show the make command to be run.")
@cli.argument('-j', '--parallel', type=int, default=1, help="Set the number of parallel make jobs; 0 means unlimited.")
@cli.argument('-e', '--env', arg_only=True, action='append', default=[], help="Set a variable to be passed to make. May be passed multiple times.")
@@ -58,76 +83,49 @@ def flash(cli):
If a binary firmware is supplied, try to flash that.
If a Configurator JSON export is supplied this command will create a new keymap. Keymap and Keyboard arguments
will be ignored.
If a Configurator export is supplied this command will create a new keymap, overwriting an existing keymap if one exists.
If no file is supplied, keymap and keyboard are expected.
If a keyboard and keymap are provided this command will build a firmware based on that.
If bootloader is omitted the make system will use the configured bootloader for that keyboard.
"""
if cli.args.filename and cli.args.filename.suffix in ['.bin', '.hex']:
# Try to flash binary firmware
cli.echo('Flashing binary firmware...\nPlease reset your keyboard into bootloader mode now!\nPress Ctrl-C to exit.\n')
try:
err, msg = flasher(cli.args.mcu, cli.args.filename)
if err:
cli.log.error(msg)
return False
except KeyboardInterrupt:
cli.log.info('Ctrl-C was pressed, exiting...')
sys.exit(0)
if cli.args.filename and cli.args.filename.suffix in ['.bin', '.hex', '.uf2']:
return _flash_binary(cli.args.filename, cli.args.mcu)
else:
if cli.args.clean and not cli.args.filename and not cli.args.dry_run:
if cli.config.flash.keyboard and cli.config.flash.keymap:
command = create_make_command(cli.config.flash.keyboard, cli.config.flash.keymap, 'clean')
cli.run(command, capture_output=False, stdin=DEVNULL)
if cli.args.bootloaders:
return _list_bootloaders()
# Build the environment vars
envs = {}
for env in cli.args.env:
if '=' in env:
key, value = env.split('=', 1)
envs[key] = value
else:
cli.log.warning('Invalid environment variable: %s', env)
# Build the environment vars
envs = build_environment(cli.args.env)
# Determine the compile command
command = ''
# Determine the compile command
commands = []
if cli.args.bootloaders:
# Provide usage and list bootloaders
cli.echo('usage: qmk flash [-h] [-b] [-n] [-kb KEYBOARD] [-km KEYMAP] [-bl BOOTLOADER] [filename]')
print_bootloader_help()
if cli.args.filename:
# If a configurator JSON was provided generate a keymap and compile it
user_keymap = parse_configurator_json(cli.args.filename)
commands = [compile_configurator_json(user_keymap, cli.args.bootloader, parallel=cli.config.flash.parallel, clean=cli.args.clean, **envs)]
elif cli.config.flash.keyboard and cli.config.flash.keymap:
# Generate the make command for a specific keyboard/keymap.
if not _is_keymap_target(cli.config.flash.keyboard, cli.config.flash.keymap):
cli.log.error('Invalid keymap argument.')
cli.print_help()
return False
if cli.args.filename:
# Handle compiling a configurator JSON
user_keymap = parse_configurator_json(cli.args.filename)
keymap_path = qmk.path.keymap(user_keymap['keyboard'])
command = compile_configurator_json(user_keymap, cli.args.bootloader, parallel=cli.config.flash.parallel, **envs)
if cli.args.clean:
commands.append(create_make_command(cli.config.flash.keyboard, cli.config.flash.keymap, 'clean', **envs))
commands.append(create_make_command(cli.config.flash.keyboard, cli.config.flash.keymap, cli.args.bootloader, parallel=cli.config.flash.parallel, **envs))
cli.log.info('Wrote keymap to {fg_cyan}%s/%s/keymap.c', keymap_path, user_keymap['keymap'])
if not commands:
cli.log.error('You must supply a configurator export, both `--keyboard` and `--keymap`, or be in a directory for a keyboard or keymap.')
cli.print_help()
return False
else:
if cli.config.flash.keyboard and cli.config.flash.keymap:
# Generate the make command for a specific keyboard/keymap.
command = create_make_command(cli.config.flash.keyboard, cli.config.flash.keymap, cli.args.bootloader, parallel=cli.config.flash.parallel, **envs)
elif not cli.config.flash.keyboard:
cli.log.error('Could not determine keyboard!')
elif not cli.config.flash.keymap:
cli.log.error('Could not determine keymap!')
# Compile the firmware, if we're able to
if command:
cli.log.info('Compiling keymap with {fg_cyan}%s', ' '.join(command))
if not cli.args.dry_run:
cli.echo('\n')
compile = cli.run(command, capture_output=False, stdin=DEVNULL)
return compile.returncode
else:
cli.log.error('You must supply a configurator export, both `--keyboard` and `--keymap`, or be in a directory for a keyboard or keymap.')
cli.echo('usage: qmk flash [-h] [-b] [-n] [-kb KEYBOARD] [-km KEYMAP] [-bl BOOTLOADER] [filename]')
return False
cli.log.info('Compiling keymap with {fg_cyan}%s', ' '.join(commands[-1]))
if not cli.args.dry_run:
cli.echo('\n')
for command in commands:
ret = cli.run(command, capture_output=False)
if ret.returncode:
return ret.returncode
+91 -12
View File
@@ -10,13 +10,66 @@ from qmk.datetime import current_datetime
from qmk.info import info_json
from qmk.json_encoders import InfoJSONEncoder
from qmk.json_schema import json_load
from qmk.keymap import list_keymaps
from qmk.keyboard import find_readme, list_keyboards
from qmk.keycodes import load_spec, list_versions, list_languages
DATA_PATH = Path('data')
TEMPLATE_PATH = DATA_PATH / 'templates/api/'
BUILD_API_PATH = Path('.build/api_data/')
def _list_constants(output_folder):
"""Produce a map of available constants
"""
ret = {}
for file in (output_folder / 'constants').glob('**/*_[0-9].[0-9].[0-9].json'):
name, version = file.stem.rsplit('_', 1)
if name not in ret:
ret[name] = []
ret[name].append(version)
# Ensure content is sorted
for name in ret:
ret[name] = sorted(ret[name])
return ret
def _resolve_keycode_specs(output_folder):
"""To make it easier for consumers, publish pre-merged spec files
"""
for version in list_versions():
overall = load_spec(version)
output_file = output_folder / f'constants/keycodes_{version}.json'
output_file.write_text(json.dumps(overall), encoding='utf-8')
for lang in list_languages():
for version in list_versions(lang):
overall = load_spec(version, lang)
output_file = output_folder / f'constants/keycodes_{lang}_{version}.json'
output_file.write_text(json.dumps(overall, indent=4), encoding='utf-8')
# Purge files consumed by 'load_spec'
shutil.rmtree(output_folder / 'constants/keycodes/')
def _filtered_copy(src, dst):
src = Path(src)
dst = Path(dst)
if dst.suffix == '.hjson':
data = json_load(src)
dst = dst.with_suffix('.json')
dst.write_text(json.dumps(data), encoding='utf-8')
return dst
return shutil.copy2(src, dst)
def _filtered_keyboard_list():
"""Perform basic filtering of list_keyboards
"""
@@ -41,13 +94,14 @@ def generate_api(cli):
keyboard_list_file = v1_dir / 'keyboard_list.json' # A simple list of keyboard targets
keyboard_aliases_file = v1_dir / 'keyboard_aliases.json' # A list of historical keyboard names and their new name
keyboard_metadata_file = v1_dir / 'keyboard_metadata.json' # All the data configurator/via needs for initialization
constants_metadata_file = v1_dir / 'constants_metadata.json' # Metadata for available constants
usb_file = v1_dir / 'usb.json' # A mapping of USB VID/PID -> keyboard target
if BUILD_API_PATH.exists():
shutil.rmtree(BUILD_API_PATH)
shutil.copytree(TEMPLATE_PATH, BUILD_API_PATH)
shutil.copytree(DATA_PATH, v1_dir)
shutil.copytree(DATA_PATH, v1_dir, copy_function=_filtered_copy)
# Filter down when required
keyboard_list = _filtered_keyboard_list()
@@ -57,24 +111,44 @@ def generate_api(cli):
# Generate and write keyboard specific JSON files
for keyboard_name in keyboard_list:
kb_all[keyboard_name] = info_json(keyboard_name)
kb_json = info_json(keyboard_name)
kb_all[keyboard_name] = kb_json
keyboard_dir = v1_dir / 'keyboards' / keyboard_name
keyboard_info = keyboard_dir / 'info.json'
keyboard_readme = keyboard_dir / 'readme.md'
keyboard_readme_src = find_readme(keyboard_name)
# Populate the list of JSON keymaps
for keymap in list_keymaps(keyboard_name, c=False, fullpath=True):
kb_json['keymaps'][keymap.name] = {
# TODO: deprecate 'url' as consumer needs to know its potentially hjson
'url': f'https://raw.githubusercontent.com/qmk/qmk_firmware/master/{keymap}/keymap.json',
# Instead consumer should grab from API and not repo directly
'path': (keymap / 'keymap.json').as_posix(),
}
keyboard_dir.mkdir(parents=True, exist_ok=True)
keyboard_json = json.dumps({'last_updated': current_datetime(), 'keyboards': {keyboard_name: kb_all[keyboard_name]}})
keyboard_json = json.dumps({'last_updated': current_datetime(), 'keyboards': {keyboard_name: kb_json}})
if not cli.args.dry_run:
keyboard_info.write_text(keyboard_json)
keyboard_info.write_text(keyboard_json, encoding='utf-8')
cli.log.debug('Wrote file %s', keyboard_info)
if keyboard_readme_src:
shutil.copyfile(keyboard_readme_src, keyboard_readme)
cli.log.debug('Copied %s -> %s', keyboard_readme_src, keyboard_readme)
if 'usb' in kb_all[keyboard_name]:
usb = kb_all[keyboard_name]['usb']
# resolve keymaps as json
for keymap in kb_json['keymaps']:
keymap_hjson = kb_json['keymaps'][keymap]['path']
keymap_json = v1_dir / keymap_hjson
keymap_json.parent.mkdir(parents=True, exist_ok=True)
keymap_json.write_text(json.dumps(json_load(Path(keymap_hjson))), encoding='utf-8')
cli.log.debug('Wrote keymap %s', keymap_json)
if 'usb' in kb_json:
usb = kb_json['usb']
if 'vid' in usb and usb['vid'] not in usb_list:
usb_list[usb['vid']] = {}
@@ -87,7 +161,7 @@ def generate_api(cli):
# Generate data for the global files
keyboard_list = sorted(kb_all)
keyboard_aliases = json_load(Path('data/mappings/keyboard_aliases.json'))
keyboard_aliases = json_load(Path('data/mappings/keyboard_aliases.hjson'))
keyboard_metadata = {
'last_updated': current_datetime(),
'keyboards': keyboard_list,
@@ -95,16 +169,21 @@ def generate_api(cli):
'usb': usb_list,
}
# Feature specific handling
_resolve_keycode_specs(v1_dir)
# Write the global JSON files
keyboard_all_json = json.dumps({'last_updated': current_datetime(), 'keyboards': kb_all}, cls=InfoJSONEncoder)
usb_json = json.dumps({'last_updated': current_datetime(), 'usb': usb_list}, cls=InfoJSONEncoder)
keyboard_list_json = json.dumps({'last_updated': current_datetime(), 'keyboards': keyboard_list}, cls=InfoJSONEncoder)
keyboard_aliases_json = json.dumps({'last_updated': current_datetime(), 'keyboard_aliases': keyboard_aliases}, cls=InfoJSONEncoder)
keyboard_metadata_json = json.dumps(keyboard_metadata, cls=InfoJSONEncoder)
constants_metadata_json = json.dumps({'last_updated': current_datetime(), 'constants': _list_constants(v1_dir)})
if not cli.args.dry_run:
keyboard_all_file.write_text(keyboard_all_json)
usb_file.write_text(usb_json)
keyboard_list_file.write_text(keyboard_list_json)
keyboard_aliases_file.write_text(keyboard_aliases_json)
keyboard_metadata_file.write_text(keyboard_metadata_json)
keyboard_all_file.write_text(keyboard_all_json, encoding='utf-8')
usb_file.write_text(usb_json, encoding='utf-8')
keyboard_list_file.write_text(keyboard_list_json, encoding='utf-8')
keyboard_aliases_file.write_text(keyboard_aliases_json, encoding='utf-8')
keyboard_metadata_file.write_text(keyboard_metadata_json, encoding='utf-8')
constants_metadata_file.write_text(constants_metadata_json, encoding='utf-8')
@@ -0,0 +1,285 @@
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Python program to make autocorrect_data.h.
This program reads from a prepared dictionary file and generates a C source file
"autocorrect_data.h" with a serialized trie embedded as an array. Run this
program and pass it as the first argument like:
$ qmk generate-autocorrect-data autocorrect_dict.txt
Each line of the dict file defines one typo and its correction with the syntax
"typo -> correction". Blank lines or lines starting with '#' are ignored.
Example:
:thier -> their
fitler -> filter
lenght -> length
ouput -> output
widht -> width
For full documentation, see QMK Docs
"""
import sys
import textwrap
from typing import Any, Dict, Iterator, List, Tuple
from milc import cli
from qmk.commands import dump_lines
from qmk.constants import GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE
from qmk.keyboard import keyboard_completer, keyboard_folder
from qmk.keymap import keymap_completer, locate_keymap
from qmk.path import normpath
KC_A = 4
KC_SPC = 0x2c
KC_QUOT = 0x34
TYPO_CHARS = dict([
("'", KC_QUOT),
(':', KC_SPC), # "Word break" character.
] + [(chr(c), c + KC_A - ord('a')) for c in range(ord('a'),
ord('z') + 1)]) # Characters a-z.
def parse_file(file_name: str) -> List[Tuple[str, str]]:
"""Parses autocorrections dictionary file.
Each line of the file defines one typo and its correction with the syntax
"typo -> correction". Blank lines or lines starting with '#' are ignored. The
function validates that typos only have characters a-z and that typos are not
substrings of other typos, otherwise the longer typo would never trigger.
Args:
file_name: String, path of the autocorrections dictionary.
Returns:
List of (typo, correction) tuples.
"""
try:
from english_words import english_words_lower_alpha_set as correct_words
except ImportError:
if not cli.args.quiet:
cli.echo('Autocorrection will falsely trigger when a typo is a substring of a correctly spelled word.')
cli.echo('To check for this, install the english_words package and rerun this script:')
cli.echo(' {fg_cyan}python3 -m pip install english_words')
# Use a minimal word list as a fallback.
correct_words = ('information', 'available', 'international', 'language', 'loosest', 'reference', 'wealthier', 'entertainment', 'association', 'provides', 'technology', 'statehood')
autocorrections = []
typos = set()
for line_number, typo, correction in parse_file_lines(file_name):
if typo in typos:
cli.log.warning('{fg_red}Error:%d:{fg_reset} Ignoring duplicate typo: "{fg_cyan}%s{fg_reset}"', line_number, typo)
continue
# Check that `typo` is valid.
if not (all([c in TYPO_CHARS for c in typo])):
cli.log.error('{fg_red}Error:%d:{fg_reset} Typo "{fg_cyan}%s{fg_reset}" has characters other than a-z, \' and :.', line_number, typo)
sys.exit(1)
for other_typo in typos:
if typo in other_typo or other_typo in typo:
cli.log.error('{fg_red}Error:%d:{fg_reset} Typos may not be substrings of one another, otherwise the longer typo would never trigger: "{fg_cyan}%s{fg_reset}" vs. "{fg_cyan}%s{fg_reset}".', line_number, typo, other_typo)
sys.exit(1)
if len(typo) < 5:
cli.log.warning('{fg_yellow}Warning:%d:{fg_reset} It is suggested that typos are at least 5 characters long to avoid false triggers: "{fg_cyan}%s{fg_reset}"', line_number, typo)
if len(typo) > 127:
cli.log.error('{fg_red}Error:%d:{fg_reset} Typo exceeds 127 chars: "{fg_cyan}%s{fg_reset}"', line_number, typo)
sys.exit(1)
check_typo_against_dictionary(typo, line_number, correct_words)
autocorrections.append((typo, correction))
typos.add(typo)
return autocorrections
def make_trie(autocorrections: List[Tuple[str, str]]) -> Dict[str, Any]:
"""Makes a trie from the the typos, writing in reverse.
Args:
autocorrections: List of (typo, correction) tuples.
Returns:
Dict of dict, representing the trie.
"""
trie = {}
for typo, correction in autocorrections:
node = trie
for letter in typo[::-1]:
node = node.setdefault(letter, {})
node['LEAF'] = (typo, correction)
return trie
def parse_file_lines(file_name: str) -> Iterator[Tuple[int, str, str]]:
"""Parses lines read from `file_name` into typo-correction pairs."""
line_number = 0
for line in open(file_name, 'rt'):
line_number += 1
line = line.strip()
if line and line[0] != '#':
# Parse syntax "typo -> correction", using strip to ignore indenting.
tokens = [token.strip() for token in line.split('->', 1)]
if len(tokens) != 2 or not tokens[0]:
print(f'Error:{line_number}: Invalid syntax: "{line}"')
sys.exit(1)
typo, correction = tokens
typo = typo.lower() # Force typos to lowercase.
typo = typo.replace(' ', ':')
yield line_number, typo, correction
def check_typo_against_dictionary(typo: str, line_number: int, correct_words) -> None:
"""Checks `typo` against English dictionary words."""
if typo.startswith(':') and typo.endswith(':'):
if typo[1:-1] in correct_words:
cli.log.warning('{fg_yellow}Warning:%d:{fg_reset} Typo "{fg_cyan}%s{fg_reset}" is a correctly spelled dictionary word.', line_number, typo)
elif typo.startswith(':') and not typo.endswith(':'):
for word in correct_words:
if word.startswith(typo[1:]):
cli.log.warning('{fg_yellow}Warning:%d: {fg_reset}Typo "{fg_cyan}%s{fg_reset}" would falsely trigger on correctly spelled word "{fg_cyan}%s{fg_reset}".', line_number, typo, word)
elif not typo.startswith(':') and typo.endswith(':'):
for word in correct_words:
if word.endswith(typo[:-1]):
cli.log.warning('{fg_yellow}Warning:%d:{fg_reset} Typo "{fg_cyan}%s{fg_reset}" would falsely trigger on correctly spelled word "{fg_cyan}%s{fg_reset}".', line_number, typo, word)
elif not typo.startswith(':') and not typo.endswith(':'):
for word in correct_words:
if typo in word:
cli.log.warning('{fg_yellow}Warning:%d:{fg_reset} Typo "{fg_cyan}%s{fg_reset}" would falsely trigger on correctly spelled word "{fg_cyan}%s{fg_reset}".', line_number, typo, word)
def serialize_trie(autocorrections: List[Tuple[str, str]], trie: Dict[str, Any]) -> List[int]:
"""Serializes trie and correction data in a form readable by the C code.
Args:
autocorrections: List of (typo, correction) tuples.
trie: Dict of dicts.
Returns:
List of ints in the range 0-255.
"""
table = []
# Traverse trie in depth first order.
def traverse(trie_node):
if 'LEAF' in trie_node: # Handle a leaf trie node.
typo, correction = trie_node['LEAF']
word_boundary_ending = typo[-1] == ':'
typo = typo.strip(':')
i = 0 # Make the autocorrection data for this entry and serialize it.
while i < min(len(typo), len(correction)) and typo[i] == correction[i]:
i += 1
backspaces = len(typo) - i - 1 + word_boundary_ending
assert 0 <= backspaces <= 63
correction = correction[i:]
bs_count = [backspaces + 128]
data = bs_count + list(bytes(correction, 'ascii')) + [0]
entry = {'data': data, 'links': [], 'byte_offset': 0}
table.append(entry)
elif len(trie_node) == 1: # Handle trie node with a single child.
c, trie_node = next(iter(trie_node.items()))
entry = {'chars': c, 'byte_offset': 0}
# It's common for a trie to have long chains of single-child nodes. We
# find the whole chain so that we can serialize it more efficiently.
while len(trie_node) == 1 and 'LEAF' not in trie_node:
c, trie_node = next(iter(trie_node.items()))
entry['chars'] += c
table.append(entry)
entry['links'] = [traverse(trie_node)]
else: # Handle trie node with multiple children.
entry = {'chars': ''.join(sorted(trie_node.keys())), 'byte_offset': 0}
table.append(entry)
entry['links'] = [traverse(trie_node[c]) for c in entry['chars']]
return entry
traverse(trie)
def serialize(e: Dict[str, Any]) -> List[int]:
if not e['links']: # Handle a leaf table entry.
return e['data']
elif len(e['links']) == 1: # Handle a chain table entry.
return [TYPO_CHARS[c] for c in e['chars']] + [0] # + encode_link(e['links'][0]))
else: # Handle a branch table entry.
data = []
for c, link in zip(e['chars'], e['links']):
data += [TYPO_CHARS[c] | (0 if data else 64)] + encode_link(link)
return data + [0]
byte_offset = 0
for e in table: # To encode links, first compute byte offset of each entry.
e['byte_offset'] = byte_offset
byte_offset += len(serialize(e))
assert 0 <= byte_offset <= 0xffff
return [b for e in table for b in serialize(e)] # Serialize final table.
def encode_link(link: Dict[str, Any]) -> List[int]:
"""Encodes a node link as two bytes."""
byte_offset = link['byte_offset']
if not (0 <= byte_offset <= 0xffff):
cli.log.error('{fg_red}Error:{fg_reset} The autocorrection table is too large, a node link exceeds 64KB limit. Try reducing the autocorrection dict to fewer entries.')
sys.exit(1)
return [byte_offset & 255, byte_offset >> 8]
def typo_len(e: Tuple[str, str]) -> int:
return len(e[0])
def to_hex(b: int) -> str:
return f'0x{b:02X}'
@cli.argument('filename', type=normpath, help='The autocorrection database file')
@cli.argument('-kb', '--keyboard', type=keyboard_folder, completer=keyboard_completer, help='The keyboard to build a firmware for. Ignored when a configurator export is supplied.')
@cli.argument('-km', '--keymap', completer=keymap_completer, help='The keymap to build a firmware for. Ignored when a configurator export is supplied.')
@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")
@cli.subcommand('Generate the autocorrection data file from a dictionary file.')
def generate_autocorrect_data(cli):
autocorrections = parse_file(cli.args.filename)
trie = make_trie(autocorrections)
data = serialize_trie(autocorrections, trie)
current_keyboard = cli.args.keyboard or cli.config.user.keyboard or cli.config.generate_autocorrect_data.keyboard
current_keymap = cli.args.keymap or cli.config.user.keymap or cli.config.generate_autocorrect_data.keymap
if current_keyboard and current_keymap:
cli.args.output = locate_keymap(current_keyboard, current_keymap).parent / 'autocorrect_data.h'
assert all(0 <= b <= 255 for b in data)
min_typo = min(autocorrections, key=typo_len)[0]
max_typo = max(autocorrections, key=typo_len)[0]
# Build the autocorrect_data.h file.
autocorrect_data_h_lines = [GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE, '#pragma once', '']
autocorrect_data_h_lines.append(f'// Autocorrection dictionary ({len(autocorrections)} entries):')
for typo, correction in autocorrections:
autocorrect_data_h_lines.append(f'// {typo:<{len(max_typo)}} -> {correction}')
autocorrect_data_h_lines.append('')
autocorrect_data_h_lines.append(f'#define AUTOCORRECT_MIN_LENGTH {len(min_typo)} // "{min_typo}"')
autocorrect_data_h_lines.append(f'#define AUTOCORRECT_MAX_LENGTH {len(max_typo)} // "{max_typo}"')
autocorrect_data_h_lines.append(f'#define DICTIONARY_SIZE {len(data)}')
autocorrect_data_h_lines.append('')
autocorrect_data_h_lines.append('static const uint8_t autocorrect_data[DICTIONARY_SIZE] PROGMEM = {')
autocorrect_data_h_lines.append(textwrap.fill(' %s' % (', '.join(map(to_hex, data))), width=100, subsequent_indent=' '))
autocorrect_data_h_lines.append('};')
# Show the results
dump_lines(cli.args.output, autocorrect_data_h_lines, cli.args.quiet)
+64 -94
View File
@@ -1,18 +1,27 @@
"""Used by the make system to generate info_config.h from info.json.
"""
from pathlib import Path
from dotty_dict import dotty
from argcomplete.completers import FilesCompleter
from milc import cli
from qmk.info import info_json, keymap_json_config
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
from qmk.path import normpath
from qmk.commands import dump_lines, parse_configurator_json
from qmk.path import normpath, FileType
from qmk.constants import GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE
def generate_define(define, value=None):
value = f' {value}' if value is not None else ''
return f"""
#ifndef {define}
# define {define}{value}
#endif // {define}"""
def direct_pins(direct_pins, postfix):
"""Return the config.h lines that set the direct pins.
"""
@@ -22,11 +31,7 @@ def direct_pins(direct_pins, postfix):
cols = ','.join(map(str, [col or 'NO_PIN' for col in row]))
rows.append('{' + cols + '}')
return f"""
#ifndef DIRECT_PINS{postfix}
# define DIRECT_PINS{postfix} {{ {", ".join(rows)} }}
#endif // DIRECT_PINS{postfix}
"""
return generate_define(f'DIRECT_PINS{postfix}', f'{{ {", ".join(rows)} }}')
def pin_array(define, pins, postfix):
@@ -34,11 +39,7 @@ def pin_array(define, pins, postfix):
"""
pin_array = ', '.join(map(str, [pin or 'NO_PIN' for pin in pins]))
return f"""
#ifndef {define}_PINS{postfix}
# define {define}_PINS{postfix} {{ {pin_array} }}
#endif // {define}_PINS{postfix}
"""
return generate_define(f'{define}_PINS{postfix}', f'{{ {pin_array} }}')
def matrix_pins(matrix_pins, postfix=''):
@@ -61,25 +62,15 @@ def matrix_pins(matrix_pins, postfix=''):
def generate_matrix_size(kb_info_json, config_h_lines):
"""Add the matrix size to the config.h.
"""
if 'matrix_pins' in kb_info_json:
col_count = kb_info_json['matrix_size']['cols']
row_count = kb_info_json['matrix_size']['rows']
config_h_lines.append(f"""
#ifndef MATRIX_COLS
# define MATRIX_COLS {col_count}
#endif // MATRIX_COLS
#ifndef MATRIX_ROWS
# define MATRIX_ROWS {row_count}
#endif // MATRIX_ROWS
""")
if 'matrix_size' in kb_info_json:
config_h_lines.append(generate_define('MATRIX_COLS', kb_info_json['matrix_size']['cols']))
config_h_lines.append(generate_define('MATRIX_ROWS', kb_info_json['matrix_size']['rows']))
def generate_config_items(kb_info_json, config_h_lines):
"""Iterate through the info_config map to generate basic config values.
"""
info_config_map = json_load(Path('data/mappings/info_config.json'))
info_config_map = json_load(Path('data/mappings/info_config.hjson'))
for config_key, info_dict in info_config_map.items():
info_key = info_dict['info_key']
@@ -95,43 +86,23 @@ def generate_config_items(kb_info_json, config_h_lines):
continue
if key_type.startswith('array.array'):
config_h_lines.append('')
config_h_lines.append(f'#ifndef {config_key}')
config_h_lines.append(f'# define {config_key} {{ {", ".join(["{" + ",".join(list(map(str, x))) + "}" for x in config_value])} }}')
config_h_lines.append(f'#endif // {config_key}')
config_h_lines.append(generate_define(config_key, f'{{ {", ".join(["{" + ",".join(list(map(str, x))) + "}" for x in config_value])} }}'))
elif key_type.startswith('array'):
config_h_lines.append('')
config_h_lines.append(f'#ifndef {config_key}')
config_h_lines.append(f'# define {config_key} {{ {", ".join(map(str, config_value))} }}')
config_h_lines.append(f'#endif // {config_key}')
config_h_lines.append(generate_define(config_key, f'{{ {", ".join(map(str, config_value))} }}'))
elif key_type == 'bool':
if config_value:
config_h_lines.append('')
config_h_lines.append(f'#ifndef {config_key}')
config_h_lines.append(f'# define {config_key}')
config_h_lines.append(f'#endif // {config_key}')
config_h_lines.append(generate_define(config_key))
elif key_type == 'mapping':
for key, value in config_value.items():
config_h_lines.append('')
config_h_lines.append(f'#ifndef {key}')
config_h_lines.append(f'# define {key} {value}')
config_h_lines.append(f'#endif // {key}')
config_h_lines.append(generate_define(key, value))
elif key_type == 'str':
config_h_lines.append('')
config_h_lines.append(f'#ifndef {config_key}')
config_h_lines.append(f'# define {config_key} "{config_value}"')
config_h_lines.append(f'#endif // {config_key}')
escaped_str = config_value.replace('\\', '\\\\').replace('"', '\\"')
config_h_lines.append(generate_define(config_key, f'"{escaped_str}"'))
elif key_type == 'bcd_version':
(major, minor, revision) = config_value.split('.')
config_h_lines.append('')
config_h_lines.append(f'#ifndef {config_key}')
config_h_lines.append(f'# define {config_key} 0x{major.zfill(2)}{minor}{revision}')
config_h_lines.append(f'#endif // {config_key}')
config_h_lines.append(generate_define(config_key, f'0x{major.zfill(2)}{minor}{revision}'))
else:
config_h_lines.append('')
config_h_lines.append(f'#ifndef {config_key}')
config_h_lines.append(f'# define {config_key} {config_value}')
config_h_lines.append(f'#endif // {config_key}')
config_h_lines.append(generate_define(config_key, config_value))
def generate_encoder_config(encoder_json, config_h_lines, postfix=''):
@@ -144,24 +115,15 @@ def generate_encoder_config(encoder_json, config_h_lines, postfix=''):
b_pads.append(encoder["pin_b"])
resolutions.append(encoder.get("resolution", None))
config_h_lines.append(f'#ifndef ENCODERS_PAD_A{postfix}')
config_h_lines.append(f'# define ENCODERS_PAD_A{postfix} {{ { ", ".join(a_pads) } }}')
config_h_lines.append(f'#endif // ENCODERS_PAD_A{postfix}')
config_h_lines.append(f'#ifndef ENCODERS_PAD_B{postfix}')
config_h_lines.append(f'# define ENCODERS_PAD_B{postfix} {{ { ", ".join(b_pads) } }}')
config_h_lines.append(f'#endif // ENCODERS_PAD_B{postfix}')
config_h_lines.append(generate_define(f'ENCODERS_PAD_A{postfix}', f'{{ {", ".join(a_pads)} }}'))
config_h_lines.append(generate_define(f'ENCODERS_PAD_B{postfix}', f'{{ {", ".join(b_pads)} }}'))
if None in resolutions:
cli.log.debug("Unable to generate ENCODER_RESOLUTION configuration")
elif len(set(resolutions)) == 1:
config_h_lines.append(f'#ifndef ENCODER_RESOLUTION{postfix}')
config_h_lines.append(f'# define ENCODER_RESOLUTION{postfix} { resolutions[0] }')
config_h_lines.append(f'#endif // ENCODER_RESOLUTION{postfix}')
config_h_lines.append(generate_define(f'ENCODER_RESOLUTION{postfix}', resolutions[0]))
else:
config_h_lines.append(f'#ifndef ENCODER_RESOLUTIONS{postfix}')
config_h_lines.append(f'# define ENCODER_RESOLUTIONS{postfix} {{ { ", ".join(map(str,resolutions)) } }}')
config_h_lines.append(f'#endif // ENCODER_RESOLUTIONS{postfix}')
config_h_lines.append(generate_define(f'ENCODER_RESOLUTIONS{postfix}', f'{{ {", ".join(map(str,resolutions))} }}'))
def generate_split_config(kb_info_json, config_h_lines):
@@ -170,35 +132,23 @@ def generate_split_config(kb_info_json, config_h_lines):
if kb_info_json['split']['primary'] in ('left', 'right'):
config_h_lines.append('')
config_h_lines.append('#ifndef MASTER_LEFT')
config_h_lines.append('# ifndef MASTER_RIGHT')
config_h_lines.append('# ifndef MASTER_RIGHT')
if kb_info_json['split']['primary'] == 'left':
config_h_lines.append('# define MASTER_LEFT')
config_h_lines.append('# define MASTER_LEFT')
elif kb_info_json['split']['primary'] == 'right':
config_h_lines.append('# define MASTER_RIGHT')
config_h_lines.append('# endif // MASTER_RIGHT')
config_h_lines.append('# define MASTER_RIGHT')
config_h_lines.append('# endif // MASTER_RIGHT')
config_h_lines.append('#endif // MASTER_LEFT')
elif kb_info_json['split']['primary'] == 'pin':
config_h_lines.append('')
config_h_lines.append('#ifndef SPLIT_HAND_PIN')
config_h_lines.append('# define SPLIT_HAND_PIN')
config_h_lines.append('#endif // SPLIT_HAND_PIN')
config_h_lines.append(generate_define('SPLIT_HAND_PIN'))
elif kb_info_json['split']['primary'] == 'matrix_grid':
config_h_lines.append('')
config_h_lines.append('#ifndef SPLIT_HAND_MATRIX_GRID')
config_h_lines.append('# define SPLIT_HAND_MATRIX_GRID {%s}' % (','.join(kb_info_json["split"]["matrix_grid"],)))
config_h_lines.append('#endif // SPLIT_HAND_MATRIX_GRID')
config_h_lines.append(generate_define('SPLIT_HAND_MATRIX_GRID', f'{{ {",".join(kb_info_json["split"]["matrix_grid"])} }}'))
elif kb_info_json['split']['primary'] == 'eeprom':
config_h_lines.append('')
config_h_lines.append('#ifndef EE_HANDS')
config_h_lines.append('# define EE_HANDS')
config_h_lines.append('#endif // EE_HANDS')
config_h_lines.append(generate_define('EE_HANDS'))
if 'protocol' in kb_info_json['split'].get('transport', {}):
if kb_info_json['split']['transport']['protocol'] == 'i2c':
config_h_lines.append('')
config_h_lines.append('#ifndef USE_I2C')
config_h_lines.append('# define USE_I2C')
config_h_lines.append('#endif // USE_I2C')
config_h_lines.append(generate_define('USE_I2C'))
if 'right' in kb_info_json['split'].get('matrix_pins', {}):
config_h_lines.append(matrix_pins(kb_info_json['split']['matrix_pins']['right'], '_RIGHT'))
@@ -207,19 +157,30 @@ def generate_split_config(kb_info_json, config_h_lines):
generate_encoder_config(kb_info_json['split']['encoder']['right'], config_h_lines, '_RIGHT')
def generate_led_animations_config(led_feature_json, config_h_lines, prefix):
for animation in led_feature_json.get('animations', {}):
if led_feature_json['animations'][animation]:
config_h_lines.append(generate_define(f'{prefix}{animation.upper()}'))
@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")
@cli.argument('-kb', '--keyboard', arg_only=True, type=keyboard_folder, completer=keyboard_completer, required=True, help='Keyboard to generate config.h for.')
@cli.argument('-km', '--keymap', arg_only=True, help='Keymap to generate config.h for.')
@cli.argument('-kb', '--keyboard', arg_only=True, type=keyboard_folder, completer=keyboard_completer, help='Keyboard to generate config.h for.')
@cli.subcommand('Used by the make system to generate info_config.h from info.json', hidden=True)
def generate_config_h(cli):
"""Generates the info_config.h file.
"""
# Determine our keyboard/keymap
if cli.args.keymap:
kb_info_json = dotty(keymap_json_config(cli.args.keyboard, cli.args.keymap))
else:
if cli.args.filename:
user_keymap = parse_configurator_json(cli.args.filename)
kb_info_json = dotty(user_keymap.get('config', {}))
elif cli.args.keyboard:
kb_info_json = dotty(info_json(cli.args.keyboard))
else:
cli.log.error('You must supply a configurator export or `--keyboard`.')
cli.subcommands['generate-config-h'].print_help()
return False
# Build the info_config.h file.
config_h_lines = [GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE, '#pragma once']
@@ -237,5 +198,14 @@ def generate_config_h(cli):
if 'split' in kb_info_json:
generate_split_config(kb_info_json, config_h_lines)
if 'led_matrix' in kb_info_json:
generate_led_animations_config(kb_info_json['led_matrix'], config_h_lines, 'ENABLE_LED_MATRIX_')
if 'rgb_matrix' in kb_info_json:
generate_led_animations_config(kb_info_json['rgb_matrix'], config_h_lines, 'ENABLE_RGB_MATRIX_')
if 'rgblight' in kb_info_json:
generate_led_animations_config(kb_info_json['rgblight'], config_h_lines, 'RGBLIGHT_EFFECT_')
# Show the results
dump_lines(cli.args.output, config_h_lines, cli.args.quiet)
+11 -11
View File
@@ -25,17 +25,17 @@ def _gen_led_config(info_data):
if not config_type:
return lines
matrix = [['NO_LED'] * cols for i in range(rows)]
matrix = [['NO_LED'] * cols for _ in range(rows)]
pos = []
flags = []
led_config = info_data[config_type]['layout']
for index, item in enumerate(led_config, start=0):
if 'matrix' in item:
(x, y) = item['matrix']
matrix[x][y] = str(index)
pos.append(f'{{ {item.get("x", 0)},{item.get("y", 0)} }}')
flags.append(str(item.get('flags', 0)))
led_layout = info_data[config_type]['layout']
for index, led_data in enumerate(led_layout):
if 'matrix' in led_data:
row, col = led_data['matrix']
matrix[row][col] = str(index)
pos.append(f'{{{led_data.get("x", 0)}, {led_data.get("y", 0)}}}')
flags.append(str(led_data.get('flags', 0)))
if config_type == 'rgb_matrix':
lines.append('#ifdef RGB_MATRIX_ENABLE')
@@ -47,10 +47,10 @@ def _gen_led_config(info_data):
lines.append('__attribute__ ((weak)) led_config_t g_led_config = {')
lines.append(' {')
for line in matrix:
lines.append(f' {{ {",".join(line)} }},')
lines.append(f' {{ {", ".join(line)} }},')
lines.append(' },')
lines.append(f' {{ {",".join(pos)} }},')
lines.append(f' {{ {",".join(flags)} }},')
lines.append(f' {{ {", ".join(pos)} }},')
lines.append(f' {{ {", ".join(flags)} }},')
lines.append('};')
lines.append('#endif')
+61 -13
View File
@@ -1,33 +1,71 @@
"""Used by the make system to generate keyboard.h from info.json.
"""
from pathlib import Path
from milc import cli
from qmk.path import normpath
from qmk.info import info_json
from qmk.commands import dump_lines
from qmk.keyboard import keyboard_completer, keyboard_folder
from qmk.path import normpath
from qmk.constants import GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE
from qmk.constants import COL_LETTERS, ROW_LETTERS, GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE
def would_populate_layout_h(keyboard):
"""Detect if a given keyboard is doing data driven layouts
def _generate_layouts(keyboard):
"""Generates the layouts.h file.
"""
# Build the info.json file
kb_info_json = info_json(keyboard)
for layout_name in kb_info_json['layouts']:
if kb_info_json['layouts'][layout_name]['c_macro']:
if 'matrix_size' not in kb_info_json:
cli.log.error(f'{keyboard}: Invalid matrix config.')
return []
col_num = kb_info_json['matrix_size']['cols']
row_num = kb_info_json['matrix_size']['rows']
lines = []
for layout_name, layout_data in kb_info_json['layouts'].items():
if layout_data['c_macro']:
continue
if 'matrix' not in kb_info_json['layouts'][layout_name]['layout'][0]:
cli.log.debug('%s/%s: No matrix data!', keyboard, layout_name)
if not all('matrix' in key_data for key_data in layout_data['layout']):
cli.log.debug(f'{keyboard}/{layout_name}: No or incomplete matrix data!')
continue
return True
layout_keys = []
layout_matrix = [['KC_NO'] * col_num for _ in range(row_num)]
return False
for index, key_data in enumerate(layout_data['layout']):
row, col = key_data['matrix']
identifier = f'k{ROW_LETTERS[row]}{COL_LETTERS[col]}'
try:
layout_matrix[row][col] = identifier
layout_keys.append(identifier)
except IndexError:
key_name = key_data.get('label', identifier)
cli.log.error(f'{keyboard}/{layout_name}: Matrix data out of bounds at index {index} ({key_name}): [{row}, {col}]')
return []
lines.append('')
lines.append(f'#define {layout_name}({", ".join(layout_keys)}) {{ \\')
rows = ', \\\n'.join(['\t {' + ', '.join(row) + '}' for row in layout_matrix])
rows += ' \\'
lines.append(rows)
lines.append('}')
for alias, target in kb_info_json.get('layout_aliases', {}).items():
lines.append('')
lines.append(f'#ifndef {alias}')
lines.append(f'# define {alias} {target}')
lines.append('#endif')
return lines
@cli.argument('-i', '--include', nargs='?', arg_only=True, help='Optional file to include')
@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")
@cli.argument('-kb', '--keyboard', arg_only=True, type=keyboard_folder, completer=keyboard_completer, required=True, help='Keyboard to generate keyboard.h for.')
@@ -35,13 +73,23 @@ def would_populate_layout_h(keyboard):
def generate_keyboard_h(cli):
"""Generates the keyboard.h file.
"""
has_layout_h = would_populate_layout_h(cli.args.keyboard)
keyboard_h = cli.args.include
dd_layouts = _generate_layouts(cli.args.keyboard)
valid_config = dd_layouts or keyboard_h
# Build the layouts.h file.
keyboard_h_lines = [GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE, '#pragma once', '#include "quantum.h"']
if not has_layout_h:
keyboard_h_lines.append('#error("<keyboard>.h is only optional for data driven keyboards - kb.h == bad times")')
keyboard_h_lines.append('')
keyboard_h_lines.append('// Layout content')
if dd_layouts:
keyboard_h_lines.extend(dd_layouts)
if keyboard_h:
keyboard_h_lines.append(f'#include "{Path(keyboard_h).name}"')
# Protect against poorly configured keyboards
if not valid_config:
keyboard_h_lines.append('#error("<keyboard>.h is required unless your keyboard uses data-driven configuration. Please rename your keyboard\'s header file to <keyboard>.h")')
# Show the results
dump_lines(cli.args.output, keyboard_h_lines, cli.args.quiet)
+153
View File
@@ -0,0 +1,153 @@
"""Used by the make system to generate keycodes.h 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 _translate_group(group):
"""Fix up any issues with badly chosen values
"""
if group == 'modifiers':
return 'modifier'
if group == 'media':
return 'consumer'
return group
def _render_key(key):
width = 7
if 'S(' in key:
width += len('S()')
if 'A(' in key:
width += len('A()')
if 'RCTL(' in key:
width += len('RCTL()')
if 'ALGR(' in key:
width += len('ALGR()')
return key.ljust(width)
def _render_label(label):
label = label.replace("\\", "(backslash)")
return label
def _generate_ranges(lines, keycodes):
lines.append('')
lines.append('enum qk_keycode_ranges {')
lines.append('// Ranges')
for key, value in keycodes["ranges"].items():
lo, mask = map(lambda x: int(x, 16), key.split("/"))
hi = lo + mask
define = value.get("define")
lines.append(f' {define.ljust(30)} = 0x{lo:04X},')
lines.append(f' {(define + "_MAX").ljust(30)} = 0x{hi:04X},')
lines.append('};')
def _generate_defines(lines, keycodes):
lines.append('')
lines.append('enum qk_keycode_defines {')
lines.append('// Keycodes')
for key, value in keycodes["keycodes"].items():
lines.append(f' {value.get("key")} = {key},')
lines.append('')
lines.append('// Alias')
for key, value in keycodes["keycodes"].items():
temp = value.get("key")
for alias in value.get("aliases", []):
lines.append(f' {alias.ljust(10)} = {temp},')
lines.append('};')
def _generate_helpers(lines, keycodes):
lines.append('')
lines.append('// Range Helpers')
for value in keycodes["ranges"].values():
define = value.get("define")
lines.append(f'#define IS_{define}(code) ((code) >= {define} && (code) <= {define + "_MAX"})')
# extract min/max
temp = {}
for key, value in keycodes["keycodes"].items():
group = value.get('group', None)
if not group:
continue
if group not in temp:
temp[group] = [0xFFFF, 0]
key = int(key, 16)
if key < temp[group][0]:
temp[group][0] = key
if key > temp[group][1]:
temp[group][1] = key
lines.append('')
lines.append('// Group Helpers')
for group, codes in temp.items():
lo = keycodes["keycodes"][f'0x{codes[0]:04X}']['key']
hi = keycodes["keycodes"][f'0x{codes[1]:04X}']['key']
lines.append(f'#define IS_{ _translate_group(group).upper() }_KEYCODE(code) ((code) >= {lo} && (code) <= {hi})')
def _generate_aliases(lines, keycodes):
lines.append('')
lines.append('// Aliases')
for key, value in keycodes["aliases"].items():
define = _render_key(value.get("key"))
val = _render_key(key)
if 'label' in value:
lines.append(f'#define {define} {val} // {_render_label(value.get("label"))}')
else:
lines.append(f'#define {define} {val}')
lines.append('')
for key, value in keycodes["aliases"].items():
for alias in value.get("aliases", []):
lines.append(f'#define {alias} {value.get("key")}')
@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")
@cli.subcommand('Used by the make system to generate keycodes.h from keycodes_{version}.json', hidden=True)
def generate_keycodes(cli):
"""Generates the keycodes.h file.
"""
# Build the keycodes.h file.
keycodes_h_lines = [GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE, '#pragma once', '// clang-format off']
keycodes = load_spec(cli.args.version)
_generate_ranges(keycodes_h_lines, keycodes)
_generate_defines(keycodes_h_lines, keycodes)
_generate_helpers(keycodes_h_lines, keycodes)
# Show the results
dump_lines(cli.args.output, keycodes_h_lines, cli.args.quiet)
@cli.argument('-v', '--version', arg_only=True, required=True, help='Version of keycodes to generate.')
@cli.argument('-l', '--lang', arg_only=True, required=True, help='Language 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")
@cli.subcommand('Used by the make system to generate keymap_{lang}.h from keycodes_{lang}_{version}.json', hidden=True)
def generate_keycode_extras(cli):
"""Generates the header file.
"""
# Build the header file.
keycodes_h_lines = [GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE, '#pragma once', '#include "keymap.h"', '// clang-format off']
keycodes = load_spec(cli.args.version, cli.args.lang)
_generate_aliases(keycodes_h_lines, keycodes)
# Show the results
dump_lines(cli.args.output, keycodes_h_lines, cli.args.quiet)
@@ -0,0 +1,39 @@
"""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('};')
@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")
@cli.subcommand('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)
-90
View File
@@ -1,90 +0,0 @@
"""Used by the make system to generate layouts.h from info.json.
"""
from milc import cli
from qmk.constants import COL_LETTERS, ROW_LETTERS, GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE
from qmk.decorators import automagic_keyboard, automagic_keymap
from qmk.info import info_json
from qmk.keyboard import keyboard_completer, keyboard_folder
from qmk.path import is_keyboard, normpath
from qmk.commands import dump_lines
usb_properties = {
'vid': 'VENDOR_ID',
'pid': 'PRODUCT_ID',
'device_ver': 'DEVICE_VER',
}
@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")
@cli.argument('-kb', '--keyboard', type=keyboard_folder, completer=keyboard_completer, help='Keyboard to generate config.h for.')
@cli.subcommand('Used by the make system to generate layouts.h from info.json', hidden=True)
@automagic_keyboard
@automagic_keymap
def generate_layouts(cli):
"""Generates the layouts.h file.
"""
# Determine our keyboard(s)
if not cli.config.generate_layouts.keyboard:
cli.log.error('Missing parameter: --keyboard')
cli.subcommands['info'].print_help()
return False
if not is_keyboard(cli.config.generate_layouts.keyboard):
cli.log.error('Invalid keyboard: "%s"', cli.config.generate_layouts.keyboard)
return False
# Build the info.json file
kb_info_json = info_json(cli.config.generate_layouts.keyboard)
# Build the layouts.h file.
layouts_h_lines = [GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE, '#pragma once']
if 'matrix_size' not in kb_info_json:
cli.log.error('%s: Invalid matrix config.', cli.config.generate_layouts.keyboard)
return False
col_num = kb_info_json['matrix_size']['cols']
row_num = kb_info_json['matrix_size']['rows']
for layout_name in kb_info_json['layouts']:
if kb_info_json['layouts'][layout_name]['c_macro']:
continue
if 'matrix' not in kb_info_json['layouts'][layout_name]['layout'][0]:
cli.log.debug('%s/%s: No matrix data!', cli.config.generate_layouts.keyboard, layout_name)
continue
layout_keys = []
layout_matrix = [['KC_NO' for i in range(col_num)] for i in range(row_num)]
for i, key in enumerate(kb_info_json['layouts'][layout_name]['layout']):
row = key['matrix'][0]
col = key['matrix'][1]
identifier = 'k%s%s' % (ROW_LETTERS[row], COL_LETTERS[col])
try:
layout_matrix[row][col] = identifier
layout_keys.append(identifier)
except IndexError:
key_name = key.get('label', identifier)
cli.log.error('Matrix data out of bounds for layout %s at index %s (%s): %s, %s', layout_name, i, key_name, row, col)
return False
layouts_h_lines.append('')
layouts_h_lines.append('#define %s(%s) {\\' % (layout_name, ', '.join(layout_keys)))
rows = ', \\\n'.join(['\t {' + ', '.join(row) + '}' for row in layout_matrix])
rows += ' \\'
layouts_h_lines.append(rows)
layouts_h_lines.append('}')
for alias, target in kb_info_json.get('layout_aliases', {}).items():
layouts_h_lines.append('')
layouts_h_lines.append(f'#ifndef {alias}')
layouts_h_lines.append(f'# define {alias} {target}')
layouts_h_lines.append('#endif')
# Show the results
dump_lines(cli.args.output, layouts_h_lines, cli.args.quiet)
@@ -5,7 +5,9 @@ from argparse import ArgumentTypeError
from milc import cli
import qmk.path
from qmk.constants import GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE
from qmk.commands import dump_lines
from qmk.path import normpath
def breathing_center(value):
@@ -24,17 +26,10 @@ def breathing_max(value):
raise ArgumentTypeError('Breathing max must be between 0 and 255')
@cli.argument('-c', '--center', arg_only=True, type=breathing_center, default=1.85, help='The breathing center value, from 1 to 2.7. Default: 1.85')
@cli.argument('-m', '--max', arg_only=True, type=breathing_max, default=255, help='The breathing maximum value, from 0 to 255. Default: 255')
@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.subcommand('Generates an RGB Light breathing table header.')
def generate_rgb_breathe_table(cli):
"""Generate a rgblight_breathe_table.h file containing a breathing LUT for RGB Lighting (Underglow) feature.
"""
def _generate_table(lines, center, maximum):
breathe_values = [0] * 256
for pos in range(0, 256):
breathe_values[pos] = (int)((math.exp(math.sin((pos / 255) * math.pi)) - cli.args.center / math.e) * (cli.args.max / (math.e - 1 / math.e)))
breathe_values[pos] = (int)((math.exp(math.sin((pos / 255) * math.pi)) - center / math.e) * (maximum / (math.e - 1 / math.e)))
values_template = ''
for s in range(0, 3):
@@ -51,11 +46,7 @@ def generate_rgb_breathe_table(cli):
values_template += '#endif'
values_template += '\n\n' if s < 2 else ''
table_template = '''#pragma once
#define RGBLIGHT_EFFECT_BREATHE_TABLE
// clang-format off
table_template = '''#define RGBLIGHT_EFFECT_BREATHE_TABLE
// Breathing center: {0:.2f}
// Breathing max: {1:d}
@@ -65,15 +56,23 @@ const uint8_t PROGMEM rgblight_effect_breathe_table[] = {{
}};
static const int table_scale = 256 / sizeof(rgblight_effect_breathe_table);
'''.format(cli.args.center, cli.args.max, values_template)
'''.format(center, maximum, values_template)
lines.append(table_template)
if cli.args.output:
cli.args.output.parent.mkdir(parents=True, exist_ok=True)
if cli.args.output.exists():
cli.args.output.replace(cli.args.output.parent / (cli.args.output.name + '.bak'))
cli.args.output.write_text(table_template)
if not cli.args.quiet:
cli.log.info('Wrote header to %s.', cli.args.output)
else:
print(table_template)
@cli.argument('-c', '--center', arg_only=True, type=breathing_center, default=1.85, help='The breathing center value, from 1 to 2.7. Default: 1.85')
@cli.argument('-m', '--max', arg_only=True, type=breathing_max, default=255, help='The breathing maximum value, from 0 to 255. Default: 255')
@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')
@cli.subcommand('Generates an RGB Light breathing table header.')
def generate_rgb_breathe_table(cli):
"""Generate a rgblight_breathe_table.h file containing a breathing LUT for RGB Lighting (Underglow) feature.
"""
# Build the header file.
header_lines = [GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE, '#pragma once', '// clang-format off']
_generate_table(header_lines, cli.args.center, cli.args.max)
# Show the results
dump_lines(cli.args.output, header_lines, cli.args.quiet)
+21 -10
View File
@@ -1,15 +1,16 @@
"""Used by the make system to generate a rules.mk
"""
from pathlib import Path
from dotty_dict import dotty
from argcomplete.completers import FilesCompleter
from milc import cli
from qmk.info import info_json, keymap_json_config
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
from qmk.path import normpath
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
@@ -39,22 +40,29 @@ def process_mapping_rule(kb_info_json, rules_key, info_dict):
return f'{rules_key} ?= {rules_value}'
@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")
@cli.argument('-e', '--escape', arg_only=True, action='store_true', help="Escape spaces in quiet mode")
@cli.argument('-kb', '--keyboard', arg_only=True, type=keyboard_folder, completer=keyboard_completer, required=True, help='Keyboard to generate rules.mk for.')
@cli.argument('-km', '--keymap', arg_only=True, help='Keymap to generate rules.mk for.')
@cli.argument('-kb', '--keyboard', arg_only=True, type=keyboard_folder, completer=keyboard_completer, help='Keyboard to generate rules.mk for.')
@cli.subcommand('Used by the make system to generate rules.mk from info.json', hidden=True)
def generate_rules_mk(cli):
"""Generates a rules.mk file from info.json.
"""
converter = None
# Determine our keyboard/keymap
if cli.args.keymap:
kb_info_json = dotty(keymap_json_config(cli.args.keyboard, cli.args.keymap))
else:
if cli.args.filename:
user_keymap = parse_configurator_json(cli.args.filename)
kb_info_json = dotty(user_keymap.get('config', {}))
converter = user_keymap.get('converter', None)
elif cli.args.keyboard:
kb_info_json = dotty(info_json(cli.args.keyboard))
else:
cli.log.error('You must supply a configurator export or `--keyboard`.')
cli.subcommands['generate-rules-mk'].print_help()
return False
info_rules_map = json_load(Path('data/mappings/info_rules.json'))
info_rules_map = json_load(Path('data/mappings/info_rules.hjson'))
rules_mk_lines = [GPL2_HEADER_SH_LIKE, GENERATED_HEADER_SH_LIKE]
# Iterate through the info_rules map to generate basic rules
@@ -82,6 +90,9 @@ def generate_rules_mk(cli):
else:
rules_mk_lines.append('CUSTOM_MATRIX ?= yes')
if converter:
rules_mk_lines.append(f'CONVERT_TO ?= {converter}')
# Show the results
dump_lines(cli.args.output, rules_mk_lines)
+10 -3
View File
@@ -6,7 +6,7 @@ from milc import cli
from qmk.path import normpath
from qmk.commands import dump_lines
from qmk.git import git_get_version
from qmk.git import git_get_qmk_hash, git_get_version, git_is_dirty
from qmk.constants import GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE
TIME_FMT = '%Y-%m-%d-%H:%M:%S'
@@ -29,23 +29,30 @@ def generate_version_h(cli):
current_time = strftime(TIME_FMT)
if cli.args.skip_git:
git_dirty = False
git_version = "NA"
git_qmk_hash = "NA"
chibios_version = "NA"
chibios_contrib_version = "NA"
else:
git_dirty = git_is_dirty()
git_version = git_get_version() or current_time
git_qmk_hash = git_get_qmk_hash() or "Unknown"
chibios_version = git_get_version("chibios", "os") or current_time
chibios_contrib_version = git_get_version("chibios-contrib", "os") or current_time
# Build the version.h file.
version_h_lines = [GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE, '#pragma once']
version_h_lines.append(f"""
version_h_lines.append(
f"""
#define QMK_VERSION "{git_version}"
#define QMK_BUILDDATE "{current_time}"
#define QMK_GIT_HASH "{git_qmk_hash}{'*' if git_dirty else ''}"
#define CHIBIOS_VERSION "{chibios_version}"
#define CHIBIOS_CONTRIB_VERSION "{chibios_contrib_version}"
""")
"""
)
# Show the results
dump_lines(cli.args.output, version_h_lines, cli.args.quiet)
View File
+38
View File
@@ -0,0 +1,38 @@
import shutil
from milc import cli
from qmk.path import normpath
from qmk import submodules
REMOVE_DIRS = [
'lib/ugfx',
'lib/pico-sdk',
'lib/chibios-contrib/ext/mcux-sdk',
'lib/lvgl',
]
@cli.argument('--check', arg_only=True, action='store_true', help='Check if the submodules are dirty, and display a warning if they are.')
@cli.argument('--sync', arg_only=True, action='store_true', help='Shallow clone any missing submodules.')
@cli.subcommand('Git Submodule actions.')
def git_submodule(cli):
"""Git Submodule actions
"""
if cli.args.check:
return all(item['status'] for item in submodules.status().values())
if cli.args.sync:
cli.run(['git', 'submodule', 'sync', '--recursive'])
for name, item in submodules.status().items():
if item['status'] is None:
cli.run(['git', 'submodule', 'update', '--depth=50', '--init', name], capture_output=False)
return True
for folder in REMOVE_DIRS:
if normpath(folder).is_dir():
print(f"Removing '{folder}'")
shutil.rmtree(folder)
cli.run(['git', 'submodule', 'sync', '--recursive'], capture_output=False)
cli.run(['git', 'submodule', 'update', '--init', '--recursive', '--progress'], capture_output=False)
+57
View File
@@ -10,6 +10,9 @@ from qmk.keyboard import keyboard_completer, list_keyboards
from qmk.keymap import locate_keymap, list_keymaps
from qmk.path import is_keyboard, keyboard
from qmk.git import git_get_ignored_files
from qmk.c_parse import c_source_files
CHIBIOS_CONF_CHECKS = ['chconf.h', 'halconf.h', 'mcuconf.h', 'board.h']
def _list_defaultish_keymaps(kb):
@@ -25,6 +28,29 @@ def _list_defaultish_keymaps(kb):
return keymaps
def _get_code_files(kb, km=None):
"""Return potential keyboard/keymap code files
"""
search_path = locate_keymap(kb, km).parent if km else keyboard(kb)
code_files = []
for file in c_source_files([search_path]):
# Ignore keymaps when only globing keyboard files
if not km and 'keymaps' in file.parts:
continue
code_files.append(file)
return code_files
def _has_license(file):
"""Check file has a license header
"""
# Crude assumption that first line of license header is a comment
fline = open(file).readline().rstrip()
return fline.startswith(("/*", "//"))
def _handle_json_errors(kb, info):
"""Convert any json errors into lint errors
"""
@@ -40,6 +66,15 @@ def _handle_json_errors(kb, info):
return ok
def _chibios_conf_includenext_check(target):
"""Check the ChibiOS conf.h for the correct inclusion of the next conf.h
"""
for i, line in enumerate(target.open()):
if f'#include_next "{target.name}"' in line:
return f'Found `#include_next "{target.name}"` on line {i} of {target}, should be `#include_next <{target.name}>` (use angle brackets, not quotes)'
return None
def _rules_mk_assignment_only(kb):
"""Check the keyboard-level rules.mk to ensure it only has assignments.
"""
@@ -92,6 +127,17 @@ def keymap_check(kb, km):
cli.log.error(f'{kb}/{km}: The file "{file}" should not exist!')
ok = False
for file in _get_code_files(kb, km):
if not _has_license(file):
cli.log.error(f'{kb}/{km}: The file "{file}" does not have a license header!')
ok = False
if file.name in CHIBIOS_CONF_CHECKS:
check_error = _chibios_conf_includenext_check(file)
if check_error is not None:
cli.log.error(f'{kb}/{km}: {check_error}')
ok = False
return ok
@@ -119,6 +165,17 @@ def keyboard_check(kb):
cli.log.error(f'{kb}: The file "{file}" should not exist!')
ok = False
for file in _get_code_files(kb):
if not _has_license(file):
cli.log.error(f'{kb}: The file "{file}" does not have a license header!')
ok = False
if file.name in CHIBIOS_CONF_CHECKS:
check_error = _chibios_conf_includenext_check(file)
if check_error is not None:
cli.log.error(f'{kb}: {check_error}')
ok = False
return ok
+2 -1
View File
@@ -5,9 +5,10 @@ from milc import cli
import qmk.keyboard
@cli.argument('--no-resolve-defaults', arg_only=True, action='store_false', help='Ignore any "DEFAULT_FOLDER" within keyboards rules.mk')
@cli.subcommand("List the keyboards currently defined within QMK")
def list_keyboards(cli):
"""List the keyboards currently defined within QMK
"""
for keyboard_name in qmk.keyboard.list_keyboards():
for keyboard_name in qmk.keyboard.list_keyboards(cli.args.no_resolve_defaults):
print(keyboard_name)
+176
View File
@@ -0,0 +1,176 @@
"""Compile all keyboards.
This will compile everything in parallel, for testing purposes.
"""
import fnmatch
import logging
import multiprocessing
import os
import re
from pathlib import Path
from subprocess import DEVNULL
from dotty_dict import dotty
from milc import cli
from qmk.constants import QMK_FIRMWARE
from qmk.commands import _find_make, get_make_parallel_args
from qmk.info import keymap_json
import qmk.keyboard
import qmk.keymap
def _set_log_level(level):
cli.acquire_lock()
old = cli.log_level
cli.log_level = level
cli.log.setLevel(level)
logging.root.setLevel(level)
cli.release_lock()
return old
def _all_keymaps(keyboard):
old = _set_log_level(logging.CRITICAL)
keymaps = qmk.keymap.list_keymaps(keyboard)
_set_log_level(old)
return (keyboard, keymaps)
def _keymap_exists(keyboard, keymap):
old = _set_log_level(logging.CRITICAL)
ret = keyboard if qmk.keymap.locate_keymap(keyboard, keymap) is not None else None
_set_log_level(old)
return ret
def _load_keymap_info(keyboard, keymap):
old = _set_log_level(logging.CRITICAL)
ret = (keyboard, keymap, keymap_json(keyboard, keymap))
_set_log_level(old)
return ret
@cli.argument('-t', '--no-temp', arg_only=True, action='store_true', help="Remove temporary files during build.")
@cli.argument('-j', '--parallel', type=int, default=1, help="Set the number of parallel make jobs; 0 means unlimited.")
@cli.argument('-c', '--clean', arg_only=True, action='store_true', help="Remove object files before compiling.")
@cli.argument(
'-f',
'--filter',
arg_only=True,
action='append',
default=[],
help= # noqa: `format-python` and `pytest` don't agree here.
"Filter the list of keyboards based on the supplied value in rules.mk. Matches info.json structure, and accepts the formats 'features.rgblight=true' or 'exists(matrix_pins.direct)'. May be passed multiple times, all filters need to match. Value may include wildcards such as '*' and '?'." # noqa: `format-python` and `pytest` don't agree here.
)
@cli.argument('-km', '--keymap', type=str, default='default', help="The keymap name to build. Default is 'default'.")
@cli.argument('-e', '--env', arg_only=True, action='append', default=[], help="Set a variable to be passed to make. May be passed multiple times.")
@cli.subcommand('Compile QMK Firmware for all keyboards.', hidden=False if cli.config.user.developer else True)
def mass_compile(cli):
"""Compile QMK Firmware against all keyboards.
"""
make_cmd = _find_make()
if cli.args.clean:
cli.run([make_cmd, 'clean'], capture_output=False, stdin=DEVNULL)
builddir = Path(QMK_FIRMWARE) / '.build'
makefile = builddir / 'parallel_kb_builds.mk'
targets = []
with multiprocessing.Pool() as pool:
cli.log.info(f'Retrieving list of keyboards with keymap "{cli.args.keymap}"...')
target_list = []
if cli.args.keymap == 'all':
kb_to_kms = pool.map(_all_keymaps, qmk.keyboard.list_keyboards())
for targets in kb_to_kms:
keyboard = targets[0]
keymaps = targets[1]
target_list.extend([(keyboard, keymap) for keymap in keymaps])
else:
target_list = [(kb, cli.args.keymap) for kb in filter(lambda kb: kb is not None, pool.starmap(_keymap_exists, [(kb, cli.args.keymap) for kb in qmk.keyboard.list_keyboards()]))]
if len(cli.args.filter) == 0:
targets = target_list
else:
cli.log.info('Parsing data for all matching keyboard/keymap combinations...')
valid_keymaps = [(e[0], e[1], dotty(e[2])) for e in pool.starmap(_load_keymap_info, target_list)]
equals_re = re.compile(r'^(?P<key>[a-zA-Z0-9_\.]+)\s*=\s*(?P<value>[^#]+)$')
exists_re = re.compile(r'^exists\((?P<key>[a-zA-Z0-9_\.]+)\)$')
for filter_txt in cli.args.filter:
f = equals_re.match(filter_txt)
if f is not None:
key = f.group('key')
value = f.group('value')
cli.log.info(f'Filtering on condition ("{key}" == "{value}")...')
def _make_filter(k, v):
expr = fnmatch.translate(v)
rule = re.compile(expr, re.IGNORECASE)
def f(e):
lhs = e[2].get(k)
lhs = str(False if lhs is None else lhs)
return rule.search(lhs) is not None
return f
valid_keymaps = filter(_make_filter(key, value), valid_keymaps)
f = exists_re.match(filter_txt)
if f is not None:
key = f.group('key')
cli.log.info(f'Filtering on condition (exists: "{key}")...')
valid_keymaps = filter(lambda e: e[2].get(key) is not None, valid_keymaps)
targets = [(e[0], e[1]) for e in valid_keymaps]
if len(targets) == 0:
return
builddir.mkdir(parents=True, exist_ok=True)
with open(makefile, "w") as f:
for target in sorted(targets):
keyboard_name = target[0]
keymap_name = target[1]
keyboard_safe = keyboard_name.replace('/', '_')
# yapf: disable
f.write(
f"""\
all: {keyboard_safe}_{keymap_name}_binary
{keyboard_safe}_{keymap_name}_binary:
@rm -f "{QMK_FIRMWARE}/.build/failed.log.{keyboard_safe}.{keymap_name}" || true
@echo "Compiling QMK Firmware for target: '{keyboard_name}:{keymap_name}'..." >>"{QMK_FIRMWARE}/.build/build.log.{os.getpid()}.{keyboard_safe}"
+@$(MAKE) -C "{QMK_FIRMWARE}" -f "{QMK_FIRMWARE}/builddefs/build_keyboard.mk" KEYBOARD="{keyboard_name}" KEYMAP="{keymap_name}" COLOR=true SILENT=false {' '.join(cli.args.env)} \\
>>"{QMK_FIRMWARE}/.build/build.log.{os.getpid()}.{keyboard_safe}.{keymap_name}" 2>&1 \\
|| cp "{QMK_FIRMWARE}/.build/build.log.{os.getpid()}.{keyboard_safe}.{keymap_name}" "{QMK_FIRMWARE}/.build/failed.log.{os.getpid()}.{keyboard_safe}.{keymap_name}"
@{{ grep '\[ERRORS\]' "{QMK_FIRMWARE}/.build/build.log.{os.getpid()}.{keyboard_safe}.{keymap_name}" >/dev/null 2>&1 && printf "Build %-64s \e[1;31m[ERRORS]\e[0m\\n" "{keyboard_name}:{keymap_name}" ; }} \\
|| {{ grep '\[WARNINGS\]' "{QMK_FIRMWARE}/.build/build.log.{os.getpid()}.{keyboard_safe}.{keymap_name}" >/dev/null 2>&1 && printf "Build %-64s \e[1;33m[WARNINGS]\e[0m\\n" "{keyboard_name}:{keymap_name}" ; }} \\
|| printf "Build %-64s \e[1;32m[OK]\e[0m\\n" "{keyboard_name}:{keymap_name}"
@rm -f "{QMK_FIRMWARE}/.build/build.log.{os.getpid()}.{keyboard_safe}.{keymap_name}" || true
"""# noqa
)
# yapf: enable
if cli.args.no_temp:
# yapf: disable
f.write(
f"""\
@rm -rf "{QMK_FIRMWARE}/.build/{keyboard_safe}_{keymap_name}.elf" 2>/dev/null || true
@rm -rf "{QMK_FIRMWARE}/.build/{keyboard_safe}_{keymap_name}.map" 2>/dev/null || true
@rm -rf "{QMK_FIRMWARE}/.build/{keyboard_safe}_{keymap_name}.hex" 2>/dev/null || true
@rm -rf "{QMK_FIRMWARE}/.build/{keyboard_safe}_{keymap_name}.bin" 2>/dev/null || true
@rm -rf "{QMK_FIRMWARE}/.build/{keyboard_safe}_{keymap_name}.uf2" 2>/dev/null || true
@rm -rf "{QMK_FIRMWARE}/.build/obj_{keyboard_safe}" || true
@rm -rf "{QMK_FIRMWARE}/.build/obj_{keyboard_safe}_{keymap_name}" || true
"""# noqa
)
# yapf: enable
f.write('\n')
cli.run([make_cmd, *get_make_parallel_args(cli.args.parallel), '-f', makefile.as_posix(), 'all'], capture_output=False, stdin=DEVNULL)
# Check for failures
failures = [f for f in builddir.glob(f'failed.log.{os.getpid()}.*')]
if len(failures) > 0:
return False
+81
View File
@@ -0,0 +1,81 @@
"""Migrate keyboard configuration to "Data Driven"
"""
import json
from pathlib import Path
from dotty_dict import dotty
from milc import cli
from qmk.keyboard import keyboard_completer, keyboard_folder, resolve_keyboard
from qmk.info import info_json, find_info_json
from qmk.json_encoders import InfoJSONEncoder
from qmk.json_schema import json_load
def _candidate_files(keyboard):
kb_dir = Path(resolve_keyboard(keyboard))
cur_dir = Path('keyboards')
files = []
for dir in kb_dir.parts:
cur_dir = cur_dir / dir
files.append(cur_dir / 'config.h')
files.append(cur_dir / 'rules.mk')
return [file for file in files if file.exists()]
@cli.argument('-f', '--filter', arg_only=True, action='append', default=[], help="Filter the performed migrations based on the supplied value. Supported format is 'KEY' located from 'data/mappings'. May be passed multiple times.")
@cli.argument('-kb', '--keyboard', arg_only=True, type=keyboard_folder, completer=keyboard_completer, required=True, help='The keyboard\'s name')
@cli.subcommand('Migrate keyboard config to "Data Driven".', hidden=True)
def migrate(cli):
"""Migrate keyboard configuration to "Data Driven"
"""
# Merge mappings as we do not care to where "KEY" is found just that its removed
info_config_map = json_load(Path('data/mappings/info_config.hjson'))
info_rules_map = json_load(Path('data/mappings/info_rules.hjson'))
info_map = {**info_config_map, **info_rules_map}
# Parse target info.json which will receive updates
target_info = Path(find_info_json(cli.args.keyboard)[0])
info_data = dotty(json_load(target_info))
# Already parsed used for updates
kb_info_json = dotty(info_json(cli.args.keyboard))
# List of candidate files
files = _candidate_files(cli.args.keyboard)
# Filter down keys if requested
keys = info_map.keys()
if cli.args.filter:
keys = list(set(keys) & set(cli.args.filter))
cli.log.info(f'{{fg_green}}Migrating keyboard {{fg_cyan}}{cli.args.keyboard}{{fg_green}}.{{fg_reset}}')
# Start migration
for file in files:
cli.log.info(f' Migrating file {file}')
file_contents = file.read_text(encoding='utf-8').split('\n')
for key in keys:
for num, line in enumerate(file_contents):
if line.startswith(f'{key} =') or line.startswith(f'#define {key} '):
cli.log.info(f' Migrating {key}...')
while line.rstrip().endswith('\\'):
file_contents.pop(num)
line = file_contents[num]
file_contents.pop(num)
update_key = info_map[key]["info_key"]
if update_key in kb_info_json:
info_data[update_key] = kb_info_json[update_key]
file.write_text('\n'.join(file_contents), encoding='utf-8')
# Finally write out updated info.json
cli.log.info(f' Updating {target_info}')
target_info.write_text(json.dumps(info_data.to_dict(), cls=InfoJSONEncoder))
cli.log.info(f'{{fg_green}}Migration of keyboard {{fg_cyan}}{cli.args.keyboard}{{fg_green}} complete!{{fg_reset}}')
cli.log.info(f"Verify build with {{fg_yellow}}qmk compile -kb {cli.args.keyboard} -km default{{fg_reset}}.")
-106
View File
@@ -1,106 +0,0 @@
"""Compile all keyboards.
This will compile everything in parallel, for testing purposes.
"""
import os
import re
from pathlib import Path
from subprocess import DEVNULL
from milc import cli
from qmk.constants import QMK_FIRMWARE
from qmk.commands import _find_make, get_make_parallel_args
import qmk.keyboard
import qmk.keymap
def _make_rules_mk_filter(key, value):
def _rules_mk_filter(keyboard_name):
rules_mk = qmk.keyboard.rules_mk(keyboard_name)
return True if key in rules_mk and rules_mk[key].lower() == str(value).lower() else False
return _rules_mk_filter
def _is_split(keyboard_name):
rules_mk = qmk.keyboard.rules_mk(keyboard_name)
return True if 'SPLIT_KEYBOARD' in rules_mk and rules_mk['SPLIT_KEYBOARD'].lower() == 'yes' else False
@cli.argument('-t', '--no-temp', arg_only=True, action='store_true', help="Remove temporary files during build.")
@cli.argument('-j', '--parallel', type=int, default=1, help="Set the number of parallel make jobs; 0 means unlimited.")
@cli.argument('-c', '--clean', arg_only=True, action='store_true', help="Remove object files before compiling.")
@cli.argument('-f', '--filter', arg_only=True, action='append', default=[], help="Filter the list of keyboards based on the supplied value in rules.mk. Supported format is 'SPLIT_KEYBOARD=yes'. May be passed multiple times.")
@cli.argument('-km', '--keymap', type=str, default='default', help="The keymap name to build. Default is 'default'.")
@cli.argument('-e', '--env', arg_only=True, action='append', default=[], help="Set a variable to be passed to make. May be passed multiple times.")
@cli.subcommand('Compile QMK Firmware for all keyboards.', hidden=False if cli.config.user.developer else True)
def multibuild(cli):
"""Compile QMK Firmware against all keyboards.
"""
make_cmd = _find_make()
if cli.args.clean:
cli.run([make_cmd, 'clean'], capture_output=False, stdin=DEVNULL)
builddir = Path(QMK_FIRMWARE) / '.build'
makefile = builddir / 'parallel_kb_builds.mk'
keyboard_list = qmk.keyboard.list_keyboards()
filter_re = re.compile(r'^(?P<key>[A-Z0-9_]+)\s*=\s*(?P<value>[^#]+)$')
for filter_txt in cli.args.filter:
f = filter_re.match(filter_txt)
if f is not None:
keyboard_list = filter(_make_rules_mk_filter(f.group('key'), f.group('value')), keyboard_list)
keyboard_list = list(sorted(keyboard_list))
if len(keyboard_list) == 0:
return
builddir.mkdir(parents=True, exist_ok=True)
with open(makefile, "w") as f:
for keyboard_name in keyboard_list:
if qmk.keymap.locate_keymap(keyboard_name, cli.args.keymap) is not None:
keyboard_safe = keyboard_name.replace('/', '_')
# yapf: disable
f.write(
f"""\
all: {keyboard_safe}_binary
{keyboard_safe}_binary:
@rm -f "{QMK_FIRMWARE}/.build/failed.log.{keyboard_safe}" || true
@echo "Compiling QMK Firmware for target: '{keyboard_name}:{cli.args.keymap}'..." >>"{QMK_FIRMWARE}/.build/build.log.{os.getpid()}.{keyboard_safe}"
+@$(MAKE) -C "{QMK_FIRMWARE}" -f "{QMK_FIRMWARE}/builddefs/build_keyboard.mk" KEYBOARD="{keyboard_name}" KEYMAP="{cli.args.keymap}" REQUIRE_PLATFORM_KEY= COLOR=true SILENT=false {' '.join(cli.args.env)} \\
>>"{QMK_FIRMWARE}/.build/build.log.{os.getpid()}.{keyboard_safe}" 2>&1 \\
|| cp "{QMK_FIRMWARE}/.build/build.log.{os.getpid()}.{keyboard_safe}" "{QMK_FIRMWARE}/.build/failed.log.{os.getpid()}.{keyboard_safe}"
@{{ grep '\[ERRORS\]' "{QMK_FIRMWARE}/.build/build.log.{os.getpid()}.{keyboard_safe}" >/dev/null 2>&1 && printf "Build %-64s \e[1;31m[ERRORS]\e[0m\\n" "{keyboard_name}:{cli.args.keymap}" ; }} \\
|| {{ grep '\[WARNINGS\]' "{QMK_FIRMWARE}/.build/build.log.{os.getpid()}.{keyboard_safe}" >/dev/null 2>&1 && printf "Build %-64s \e[1;33m[WARNINGS]\e[0m\\n" "{keyboard_name}:{cli.args.keymap}" ; }} \\
|| printf "Build %-64s \e[1;32m[OK]\e[0m\\n" "{keyboard_name}:{cli.args.keymap}"
@rm -f "{QMK_FIRMWARE}/.build/build.log.{os.getpid()}.{keyboard_safe}" || true
"""# noqa
)
# yapf: enable
if cli.args.no_temp:
# yapf: disable
f.write(
f"""\
@rm -rf "{QMK_FIRMWARE}/.build/{keyboard_safe}_{cli.args.keymap}.elf" 2>/dev/null || true
@rm -rf "{QMK_FIRMWARE}/.build/{keyboard_safe}_{cli.args.keymap}.map" 2>/dev/null || true
@rm -rf "{QMK_FIRMWARE}/.build/{keyboard_safe}_{cli.args.keymap}.hex" 2>/dev/null || true
@rm -rf "{QMK_FIRMWARE}/.build/{keyboard_safe}_{cli.args.keymap}.bin" 2>/dev/null || true
@rm -rf "{QMK_FIRMWARE}/.build/{keyboard_safe}_{cli.args.keymap}.uf2" 2>/dev/null || true
@rm -rf "{QMK_FIRMWARE}/.build/obj_{keyboard_safe}" || true
@rm -rf "{QMK_FIRMWARE}/.build/obj_{keyboard_safe}_{cli.args.keymap}" || true
"""# noqa
)
# yapf: enable
f.write('\n')
cli.run([make_cmd, *get_make_parallel_args(cli.args.parallel), '-f', makefile.as_posix(), 'all'], capture_output=False, stdin=DEVNULL)
# Check for failures
failures = [f for f in builddir.glob(f'failed.log.{os.getpid()}.*')]
if len(failures) > 0:
return False
+6 -6
View File
@@ -195,11 +195,6 @@ def new_keyboard(cli):
cli.echo('')
kb_name = cli.args.keyboard if cli.args.keyboard else prompt_keyboard()
user_name = cli.config.new_keyboard.name if cli.config.new_keyboard.name else prompt_user()
real_name = cli.args.realname or cli.config.new_keyboard.name if cli.args.realname or cli.config.new_keyboard.name else prompt_name(user_name)
default_layout = cli.args.layout if cli.args.layout else prompt_layout()
mcu = cli.args.type if cli.args.type else prompt_mcu()
if not validate_keyboard_name(kb_name):
cli.log.error('Keyboard names must contain only {fg_cyan}lowercase a-z{fg_reset}, {fg_cyan}0-9{fg_reset}, and {fg_cyan}_{fg_reset}! Please choose a different name.')
return 1
@@ -208,9 +203,14 @@ def new_keyboard(cli):
cli.log.error(f'Keyboard {{fg_cyan}}{kb_name}{{fg_reset}} already exists! Please choose a different name.')
return 1
user_name = cli.config.new_keyboard.name if cli.config.new_keyboard.name else prompt_user()
real_name = cli.args.realname or cli.config.new_keyboard.name if cli.args.realname or cli.config.new_keyboard.name else prompt_name(user_name)
default_layout = cli.args.layout if cli.args.layout else prompt_layout()
mcu = cli.args.type if cli.args.type else prompt_mcu()
# Preprocess any development_board presets
if mcu in dev_boards:
defaults_map = json_load(Path('data/mappings/defaults.json'))
defaults_map = json_load(Path('data/mappings/defaults.hjson'))
board = defaults_map['development_board'][mcu]
mcu = board['processor']
+39 -17
View File
@@ -1,12 +1,32 @@
"""This script automates the copying of the default keymap into your own keymap.
"""
import shutil
from pathlib import Path
import qmk.path
from milc import cli
from milc.questions import question
from qmk.path import is_keyboard, keymap
from qmk.git import git_get_username
from qmk.decorators import automagic_keyboard, automagic_keymap
from qmk.keyboard import keyboard_completer, keyboard_folder
from milc import cli
def prompt_keyboard():
prompt = """{fg_yellow}Select Keyboard{style_reset_all}
If you`re unsure you can view a full list of supported keyboards with {fg_yellow}qmk list-keyboards{style_reset_all}.
Keyboard Name? """
return question(prompt)
def prompt_user():
prompt = """
{fg_yellow}Name Your Keymap{style_reset_all}
Used for maintainer, copyright, etc
Your GitHub Username? """
return question(prompt, default=git_get_username())
@cli.argument('-kb', '--keyboard', type=keyboard_folder, completer=keyboard_completer, help='Specify keyboard name. Example: 1upkeyboards/1up60hse')
@@ -17,32 +37,34 @@ from milc import cli
def new_keymap(cli):
"""Creates a new keymap for the keyboard of your choosing.
"""
# ask for user input if keyboard or keymap was not provided in the command line
keyboard = cli.config.new_keymap.keyboard if cli.config.new_keymap.keyboard else input("Keyboard Name: ")
keymap = cli.config.new_keymap.keymap if cli.config.new_keymap.keymap else input("Keymap Name: ")
cli.log.info('{style_bright}Generating a new keymap{style_normal}')
cli.echo('')
# generate keymap paths
kb_path = Path('keyboards') / keyboard
keymap_path = qmk.path.keymap(keyboard)
keymap_path_default = keymap_path / 'default'
keymap_path_new = keymap_path / keymap
# ask for user input if keyboard or keymap was not provided in the command line
kb_name = cli.config.new_keymap.keyboard if cli.config.new_keymap.keyboard else prompt_keyboard()
user_name = cli.config.new_keymap.keymap if cli.config.new_keymap.keymap else prompt_user()
# check directories
if not kb_path.exists():
cli.log.error('Keyboard %s does not exist!', kb_path)
if not is_keyboard(kb_name):
cli.log.error(f'Keyboard {{fg_cyan}}{kb_name}{{fg_reset}} does not exist! Please choose a valid name.')
return False
# generate keymap paths
km_path = keymap(kb_name)
keymap_path_default = km_path / 'default'
keymap_path_new = km_path / user_name
if not keymap_path_default.exists():
cli.log.error('Keyboard default %s does not exist!', keymap_path_default)
cli.log.error(f'Default keymap {{fg_cyan}}{keymap_path_default}{{fg_reset}} does not exist!')
return False
if keymap_path_new.exists():
cli.log.error('Keymap %s already exists!', keymap_path_new)
cli.log.error(f'Keymap {{fg_cyan}}{user_name}{{fg_reset}} already exists! Please choose a different name.')
return False
# create user directory with default keymap files
shutil.copytree(keymap_path_default, keymap_path_new, symlinks=True)
# end message to user
cli.log.info("%s keymap directory created in: %s", keymap, keymap_path_new)
cli.log.info("Compile a firmware with your new keymap by typing: \n\n\tqmk compile -kb %s -km %s\n", keyboard, keymap)
cli.log.info(f'{{fg_green}}Created a new keymap called {{fg_cyan}}{user_name}{{fg_green}} in: {{fg_cyan}}{keymap_path_new}.{{fg_reset}}')
cli.log.info(f"Compile a firmware with your new keymap by typing: {{fg_yellow}}qmk compile -kb {kb_name} -km {user_name}{{fg_reset}}.")
@@ -15,6 +15,7 @@ from PIL import Image
@cli.argument('-f', '--format', required=True, help='Output format, valid types: %s' % (', '.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.')
@cli.subcommand('Converts an input image to something QMK understands')
def painter_convert_graphics(cli):
"""Converts an image file to a format that Quantum Painter understands.
@@ -53,6 +54,12 @@ def painter_convert_graphics(cli):
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)
out_bytes = out_data.getvalue()
if cli.args.raw:
raw_file = cli.args.output / (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',
+10 -2
View File
@@ -33,6 +33,7 @@ def painter_make_font_image(cli):
@cli.argument('-u', '--unicode-glyphs', default='', help='Also generate the specified unicode glyphs.')
@cli.argument('-f', '--format', required=True, help='Output format, valid types: %s' % (', '.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')
def painter_convert_font_image(cli):
# Work out the format
@@ -53,6 +54,13 @@ 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)
out_bytes = out_data.getvalue()
if cli.args.raw:
raw_file = cli.args.output / (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 = {
@@ -62,8 +70,8 @@ def painter_convert_font_image(cli):
'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': out_data.getbuffer().nbytes,
'bytes_lines': render_bytes(out_data.getbuffer().tobytes()),
'byte_count': len(out_bytes),
'bytes_lines': render_bytes(out_bytes),
'format': cli.args.format,
}
-24
View File
@@ -1,24 +0,0 @@
"""Point people to the new command name.
"""
import sys
from pathlib import Path
from milc import cli
@cli.argument('-n', '--dry-run', arg_only=True, action='store_true', help="Don't actually format.")
@cli.subcommand('Pointer to the new command name: qmk format-python.', hidden=False if cli.config.user.developer else True)
def pyformat(cli):
"""Pointer to the new command name: qmk format-python.
"""
cli.log.warning('"qmk pyformat" has been renamed to "qmk format-python". Please use the new command in the future.')
argv = [sys.executable, *sys.argv]
argv[argv.index('pyformat')] = 'format-python'
script_path = Path(argv[1])
script_path_exe = Path(f'{argv[1]}.exe')
if not script_path.exists() and script_path_exe.exists():
# For reasons I don't understand ".exe" is stripped from the script name on windows.
argv[1] = str(script_path_exe)
return cli.run(argv, capture_output=False).returncode
+36 -8
View File
@@ -2,13 +2,13 @@
"""
import os
import sys
import json
import shutil
from pathlib import Path
from milc import cli
import jsonschema
import qmk.keymap
from qmk.constants import KEYBOARD_OUTPUT_PREFIX
from qmk.json_schema import json_load, validate
@@ -107,7 +107,7 @@ def get_make_parallel_args(parallel=1):
return parallel_args
def compile_configurator_json(user_keymap, bootloader=None, parallel=1, **env_vars):
def compile_configurator_json(user_keymap, bootloader=None, parallel=1, clean=False, **env_vars):
"""Convert a configurator export JSON file into a C file and then compile it.
Args:
@@ -129,17 +129,32 @@ def compile_configurator_json(user_keymap, bootloader=None, parallel=1, **env_va
# e.g.: qmk compile - < keyboards/clueboard/california/keymaps/default/keymap.json
user_keymap["keymap"] = user_keymap.get("keymap", "default_json")
# Write the keymap.c file
keyboard_filesafe = user_keymap['keyboard'].replace('/', '_')
target = f'{keyboard_filesafe}_{user_keymap["keymap"]}'
keyboard_output = Path(f'{KEYBOARD_OUTPUT_PREFIX}{keyboard_filesafe}')
keymap_output = Path(f'{keyboard_output}_{user_keymap["keymap"]}')
c_text = qmk.keymap.generate_c(user_keymap)
keymap_dir = keymap_output / 'src'
keymap_c = keymap_dir / 'keymap.c'
keymap_json = keymap_dir / 'keymap.json'
if clean:
if keyboard_output.exists():
shutil.rmtree(keyboard_output)
if keymap_output.exists():
shutil.rmtree(keymap_output)
# begin with making the deepest folder in the tree
keymap_dir.mkdir(exist_ok=True, parents=True)
keymap_c.write_text(c_text)
# Compare minified to ensure consistent comparison
new_content = json.dumps(user_keymap, separators=(',', ':'))
if keymap_json.exists():
old_content = json.dumps(json.loads(keymap_json.read_text(encoding='utf-8')), separators=(',', ':'))
if old_content == new_content:
new_content = None
# Write the keymap.json file if different
if new_content:
keymap_json.write_text(new_content, encoding='utf-8')
# Return a command that can be run to make the keymap and flash if given
verbose = 'true' if cli.config.general.verbose else 'false'
@@ -175,7 +190,7 @@ def compile_configurator_json(user_keymap, bootloader=None, parallel=1, **env_va
f'MAIN_KEYMAP_PATH_3={keymap_output}',
f'MAIN_KEYMAP_PATH_4={keymap_output}',
f'MAIN_KEYMAP_PATH_5={keymap_output}',
f'KEYMAP_C={keymap_c}',
f'KEYMAP_JSON={keymap_json}',
f'KEYMAP_PATH={keymap_dir}',
f'VERBOSE={verbose}',
f'COLOR={color}',
@@ -199,7 +214,7 @@ def parse_configurator_json(configurator_file):
exit(1)
orig_keyboard = user_keymap['keyboard']
aliases = json_load(Path('data/mappings/keyboard_aliases.json'))
aliases = json_load(Path('data/mappings/keyboard_aliases.hjson'))
if orig_keyboard in aliases:
if 'target' in aliases[orig_keyboard]:
@@ -211,6 +226,19 @@ def parse_configurator_json(configurator_file):
return user_keymap
def build_environment(args):
"""Common processing for cli.args.env
"""
envs = {}
for env in args:
if '=' in env:
key, value = env.split('=', 1)
envs[key] = value
else:
cli.log.warning('Invalid environment variable: %s', env)
return envs
def in_virtualenv():
"""Check if running inside a virtualenv.
Based on https://stackoverflow.com/a/1883251
+36 -11
View File
@@ -1,3 +1,4 @@
import platform
import shutil
import time
import os
@@ -56,6 +57,26 @@ def _check_dfu_programmer_version():
return False
def _find_usb_device(vid_hex, pid_hex):
# WSL doesnt have access to USB - use powershell instead...?
if 'microsoft' in platform.uname().release.lower():
ret = cli.run(['powershell.exe', '-command', 'Get-PnpDevice -PresentOnly | Select-Object -Property InstanceId'])
if f'USB\\VID_{vid_hex:04X}&PID_{pid_hex:04X}' in ret.stdout:
return (vid_hex, pid_hex)
else:
with DelayedKeyboardInterrupt():
# PyUSB does not like to be interrupted by Ctrl-C
# therefore we catch the interrupt with a custom handler
# and only process it once pyusb finished
return usb.core.find(idVendor=vid_hex, idProduct=pid_hex)
def _find_uf2_devices():
"""Delegate to uf2conv.py as VID:PID pairs can potentially fluctuate more than other bootloaders
"""
return cli.run(['util/uf2conv.py', '--list']).stdout.splitlines()
def _find_bootloader():
# To avoid running forever in the background, only look for bootloaders for 10min
start_time = time.time()
@@ -64,11 +85,7 @@ def _find_bootloader():
for vid, pid in BOOTLOADER_VIDS_PIDS[bl]:
vid_hex = int(f'0x{vid}', 0)
pid_hex = int(f'0x{pid}', 0)
with DelayedKeyboardInterrupt():
# PyUSB does not like to be interrupted by Ctrl-C
# therefore we catch the interrupt with a custom handler
# and only process it once pyusb finished
dev = usb.core.find(idVendor=vid_hex, idProduct=pid_hex)
dev = _find_usb_device(vid_hex, pid_hex)
if dev:
if bl == 'atmel-dfu':
details = _PID_TO_MCU[pid]
@@ -84,6 +101,8 @@ def _find_bootloader():
else:
details = None
return (bl, details)
if _find_uf2_devices():
return ('_uf2_compatible_', None)
time.sleep(0.1)
return (None, None)
@@ -173,30 +192,36 @@ def _flash_mdloader(file):
cli.run(['mdloader', '--first', '--download', file, '--restart'], capture_output=False)
def _flash_uf2(file):
cli.run(['util/uf2conv.py', '--deploy', file], capture_output=False)
def flasher(mcu, file):
bl, details = _find_bootloader()
# Add a small sleep to avoid race conditions
time.sleep(1)
if bl == 'atmel-dfu':
_flash_atmel_dfu(details, file.name)
_flash_atmel_dfu(details, file)
elif bl == 'caterina':
if _flash_caterina(details, file.name):
if _flash_caterina(details, file):
return (True, "The Caterina bootloader was found but is not writable. Check 'qmk doctor' output for advice.")
elif bl == 'hid-bootloader':
if mcu:
if _flash_hid_bootloader(mcu, details, file.name):
if _flash_hid_bootloader(mcu, details, file):
return (True, "Please make sure 'teensy_loader_cli' or 'hid_bootloader_cli' is available on your system.")
else:
return (True, "Specifying the MCU with '-m' is necessary for HalfKay/HID bootloaders!")
elif bl == 'stm32-dfu' or bl == 'apm32-dfu' or bl == 'gd32v-dfu' or bl == 'kiibohd':
_flash_dfu_util(details, file.name)
_flash_dfu_util(details, file)
elif bl == 'usbasploader' or bl == 'usbtinyisp':
if mcu:
_flash_isp(mcu, bl, file.name)
_flash_isp(mcu, bl, file)
else:
return (True, "Specifying the MCU with '-m' is necessary for ISP flashing!")
elif bl == 'md-boot':
_flash_mdloader(file.name)
_flash_mdloader(file)
elif bl == '_uf2_compatible_':
_flash_uf2(file)
else:
return (True, "Known bootloader found but flashing not currently supported!")
+8
View File
@@ -136,3 +136,11 @@ def git_get_ignored_files(check_dir='.'):
if invalid.returncode != 0:
return []
return invalid.stdout.strip().splitlines()
def git_get_qmk_hash():
output = cli.run(['git', 'rev-parse', '--short', 'HEAD'])
if output.returncode != 0:
return None
return output.stdout.strip()
+117 -99
View File
@@ -1,17 +1,16 @@
"""Functions that help us generate and use info.json files.
"""
from glob import glob
import re
from pathlib import Path
import jsonschema
from dotty_dict import dotty
from milc import cli
from qmk.constants import CHIBIOS_PROCESSORS, LUFA_PROCESSORS, VUSB_PROCESSORS
from qmk.c_parse import find_layouts, parse_config_h_file, find_led_config
from qmk.json_schema import deep_update, json_load, validate
from qmk.keyboard import config_h, rules_mk
from qmk.keymap import list_keymaps, locate_keymap
from qmk.commands import parse_configurator_json
from qmk.makefile import parse_rules_mk_file
from qmk.math import compute
@@ -20,12 +19,81 @@ true_values = ['1', 'on', 'yes']
false_values = ['0', 'off', 'no']
def _keyboard_in_layout_name(keyboard, layout):
"""Validate that a layout macro does not contain name of keyboard
"""
# TODO: reduce this list down
safe_layout_tokens = {
'ansi',
'iso',
'jp',
'jis',
'ortho',
'wkl',
'tkl',
'preonic',
'planck',
}
# Ignore tokens like 'split_3x7_4' or just '2x4'
layout = re.sub(r"_split_\d+x\d+_\d+", '', layout)
layout = re.sub(r"_\d+x\d+", '', layout)
name_fragments = set(keyboard.split('/')) - safe_layout_tokens
return any(fragment in layout for fragment in name_fragments)
def _valid_community_layout(layout):
"""Validate that a declared community list exists
"""
return (Path('layouts/default') / layout).exists()
def _validate(keyboard, info_data):
"""Perform various validation on the provided info.json data
"""
# First validate against the jsonschema
try:
validate(info_data, 'qmk.api.keyboard.v1')
except jsonschema.ValidationError as e:
json_path = '.'.join([str(p) for p in e.absolute_path])
cli.log.error('Invalid API data: %s: %s: %s', keyboard, json_path, e.message)
exit(1)
layouts = info_data.get('layouts', {})
layout_aliases = info_data.get('layout_aliases', {})
community_layouts = info_data.get('community_layouts', [])
community_layouts_names = list(map(lambda layout: f'LAYOUT_{layout}', community_layouts))
# Make sure we have at least one layout
if len(layouts) == 0 or all(not layout.get('json_layout', False) for layout in layouts.values()):
_log_error(info_data, 'No LAYOUTs defined! Need at least one layout defined in info.json.')
# Providing only LAYOUT_all "because I define my layouts in a 3rd party tool"
if len(layouts) == 1 and 'LAYOUT_all' in layouts:
_log_warning(info_data, '"LAYOUT_all" should be "LAYOUT" unless additional layouts are provided.')
# Extended layout name checks - ignoring community_layouts and "safe" values
potential_layouts = set(layouts.keys()) - set(community_layouts_names)
for layout in potential_layouts:
if _keyboard_in_layout_name(keyboard, layout):
_log_warning(info_data, f'Layout "{layout}" should not contain name of keyboard.')
# Filter out any non-existing community layouts
for layout in community_layouts:
if not _valid_community_layout(layout):
# Ignore layout from future checks
info_data['community_layouts'].remove(layout)
_log_error(info_data, 'Claims to support a community layout that does not exist: %s' % (layout))
# Make sure we supply layout macros for the community layouts we claim to support
for layout_name in community_layouts_names:
if layout_name not in layouts and layout_name not in layout_aliases:
_log_error(info_data, 'Claims to support community layout %s but no %s() macro found' % (layout, layout_name))
def info_json(keyboard):
"""Generate the info.json data for a specific keyboard.
"""
@@ -45,10 +113,6 @@ def info_json(keyboard):
'maintainer': 'qmk',
}
# Populate the list of JSON keymaps
for keymap in list_keymaps(keyboard, c=False, fullpath=True):
info_data['keymaps'][keymap.name] = {'url': f'https://raw.githubusercontent.com/qmk/qmk_firmware/master/{keymap}/keymap.json'}
# Populate layout data
layouts, aliases = _search_keyboard_h(keyboard)
@@ -58,6 +122,7 @@ def info_json(keyboard):
for layout_name, layout_json in layouts.items():
if not layout_name.startswith('LAYOUT_kc'):
layout_json['c_macro'] = True
layout_json['json_layout'] = False
info_data['layouts'][layout_name] = layout_json
# Merge in the data from info.json, config.h, and rules.mk
@@ -72,34 +137,8 @@ def info_json(keyboard):
# Merge in data from <keyboard.c>
info_data = _extract_led_config(info_data, str(keyboard))
# Validate against the jsonschema
try:
validate(info_data, 'qmk.api.keyboard.v1')
except jsonschema.ValidationError as e:
json_path = '.'.join([str(p) for p in e.absolute_path])
cli.log.error('Invalid API data: %s: %s: %s', keyboard, json_path, e.message)
exit(1)
# Make sure we have at least one layout
if not info_data.get('layouts'):
_find_missing_layouts(info_data, keyboard)
if not info_data.get('layouts'):
_log_error(info_data, 'No LAYOUTs defined! Need at least one layout defined in the keyboard.h or info.json.')
# Filter out any non-existing community layouts
for layout in info_data.get('community_layouts', []):
if not _valid_community_layout(layout):
# Ignore layout from future checks
info_data['community_layouts'].remove(layout)
_log_error(info_data, 'Claims to support a community layout that does not exist: %s' % (layout))
# Make sure we supply layout macros for the community layouts we claim to support
for layout in info_data.get('community_layouts', []):
layout_name = 'LAYOUT_' + layout
if layout_name not in info_data.get('layouts', {}) and layout_name not in info_data.get('layout_aliases', {}):
_log_error(info_data, 'Claims to support community layout %s but no %s() macro found' % (layout, layout_name))
# Validate
_validate(keyboard, info_data)
# Check that the reported matrix size is consistent with the actual matrix size
_check_matrix(info_data)
@@ -437,19 +476,6 @@ def _extract_matrix_info(info_data, config_c):
return info_data
# TODO: kill off usb.device_ver in favor of usb.device_version
def _extract_device_version(info_data):
if info_data.get('usb'):
if info_data['usb'].get('device_version') and not info_data['usb'].get('device_ver'):
(major, minor, revision) = info_data['usb']['device_version'].split('.', 3)
info_data['usb']['device_ver'] = f'0x{major.zfill(2)}{minor}{revision}'
if not info_data['usb'].get('device_version') and info_data['usb'].get('device_ver'):
major = int(info_data['usb']['device_ver'][2:4])
minor = int(info_data['usb']['device_ver'][4])
revision = int(info_data['usb']['device_ver'][5])
info_data['usb']['device_version'] = f'{major}.{minor}.{revision}'
def _config_to_json(key_type, config_value):
"""Convert config value using spec
"""
@@ -464,7 +490,7 @@ def _config_to_json(key_type, config_value):
if array_type == 'int':
return list(map(int, config_value.split(',')))
else:
return config_value.split(',')
return list(map(str.strip, config_value.split(',')))
elif key_type == 'bool':
return config_value in true_values
@@ -479,7 +505,7 @@ def _config_to_json(key_type, config_value):
return int(config_value)
elif key_type == 'str':
return config_value.strip('"')
return config_value.strip('"').replace('\\"', '"').replace('\\\\', '\\')
elif key_type == 'bcd_version':
major = int(config_value[2:4])
@@ -496,7 +522,7 @@ def _extract_config_h(info_data, config_c):
"""
# Pull in data from the json map
dotty_info = dotty(info_data)
info_config_map = json_load(Path('data/mappings/info_config.json'))
info_config_map = json_load(Path('data/mappings/info_config.hjson'))
for config_key, info_dict in info_config_map.items():
info_key = info_dict['info_key']
@@ -535,7 +561,6 @@ def _extract_config_h(info_data, config_c):
_extract_split_right_pins(info_data, config_c)
_extract_encoders(info_data, config_c)
_extract_split_encoders(info_data, config_c)
_extract_device_version(info_data)
return info_data
@@ -543,12 +568,20 @@ def _extract_config_h(info_data, config_c):
def _process_defaults(info_data):
"""Process any additional defaults based on currently discovered information
"""
defaults_map = json_load(Path('data/mappings/defaults.json'))
defaults_map = json_load(Path('data/mappings/defaults.hjson'))
for default_type in defaults_map.keys():
thing_map = defaults_map[default_type]
if default_type in info_data:
for key, value in thing_map.get(info_data[default_type], {}).items():
info_data[key] = value
merged_count = 0
thing_items = thing_map.get(info_data[default_type], {}).items()
for key, value in thing_items:
if key not in info_data:
info_data[key] = value
merged_count += 1
if merged_count == 0 and len(thing_items) > 0:
_log_warning(info_data, 'All defaults for \'%s\' were skipped, potential redundant config or misconfiguration detected' % (default_type))
return info_data
@@ -569,7 +602,7 @@ def _extract_rules_mk(info_data, rules):
# Pull in data from the json map
dotty_info = dotty(info_data)
info_rules_map = json_load(Path('data/mappings/info_rules.json'))
info_rules_map = json_load(Path('data/mappings/info_rules.hjson'))
for rules_key, info_dict in info_rules_map.items():
info_key = info_dict['info_key']
@@ -627,20 +660,24 @@ def _extract_led_config(info_data, keyboard):
cols = info_data['matrix_size']['cols']
rows = info_data['matrix_size']['rows']
# Assume what feature owns g_led_config
feature = "rgb_matrix"
if info_data.get("features", {}).get("led_matrix", False):
# Determine what feature owns g_led_config
features = info_data.get("features", {})
feature = None
if features.get("rgb_matrix", False):
feature = "rgb_matrix"
elif features.get("led_matrix", False):
feature = "led_matrix"
# Process
for file in find_keyboard_c(keyboard):
try:
ret = find_led_config(file, cols, rows)
if ret:
info_data[feature] = info_data.get(feature, {})
info_data[feature]["layout"] = ret
except Exception as e:
_log_warning(info_data, f'led_config: {file.name}: {e}')
if feature:
# Process
for file in find_keyboard_c(keyboard):
try:
ret = find_led_config(file, cols, rows)
if ret:
info_data[feature] = info_data.get(feature, {})
info_data[feature]["layout"] = ret
except Exception as e:
_log_warning(info_data, f'led_config: {file.name}: {e}')
return info_data
@@ -711,30 +748,6 @@ def _search_keyboard_h(keyboard):
return layouts, aliases
def _find_missing_layouts(info_data, keyboard):
"""Looks for layout macros when they aren't found other places.
If we don't find any layouts from info.json or keyboard.h we widen our search. This is error prone which is why we want to encourage people to follow the standard above.
"""
_log_warning(info_data, '%s: Falling back to searching for KEYMAP/LAYOUT macros.' % (keyboard))
for file in glob('keyboards/%s/*.h' % keyboard):
these_layouts, these_aliases = find_layouts(file)
if these_layouts:
for layout_name, layout_json in these_layouts.items():
if not layout_name.startswith('LAYOUT_kc'):
layout_json['c_macro'] = True
info_data['layouts'][layout_name] = layout_json
for alias, alias_text in these_aliases.items():
if alias_text in these_layouts:
if 'layout_aliases' not in info_data:
info_data['layout_aliases'] = {}
info_data['layout_aliases'][alias] = alias_text
def _log_error(info_data, message):
"""Send an error message to both JSON and the log.
"""
@@ -754,9 +767,7 @@ def arm_processor_rules(info_data, rules):
"""
info_data['processor_type'] = 'arm'
info_data['protocol'] = 'ChibiOS'
if 'bootloader' not in info_data:
info_data['bootloader'] = 'unknown'
info_data['platform_key'] = 'chibios'
if 'STM32' in info_data['processor']:
info_data['platform'] = 'STM32'
@@ -764,6 +775,7 @@ def arm_processor_rules(info_data, rules):
info_data['platform'] = rules['MCU_SERIES']
elif 'ARM_ATSAM' in rules:
info_data['platform'] = 'ARM_ATSAM'
info_data['platform_key'] = 'arm_atsam'
return info_data
@@ -773,10 +785,8 @@ def avr_processor_rules(info_data, rules):
"""
info_data['processor_type'] = 'avr'
info_data['platform'] = rules['ARCH'] if 'ARCH' in rules else 'unknown'
info_data['protocol'] = 'V-USB' if rules.get('MCU') in VUSB_PROCESSORS else 'LUFA'
if 'bootloader' not in info_data:
info_data['bootloader'] = 'atmel-dfu'
info_data['platform_key'] = 'avr'
info_data['protocol'] = 'V-USB' if info_data['processor'] in VUSB_PROCESSORS else 'LUFA'
# FIXME(fauxpark/anyone): Eventually we should detect the protocol by looking at PROTOCOL inherited from mcu_selection.mk:
# info_data['protocol'] = 'V-USB' if rules.get('PROTOCOL') == 'VUSB' else 'LUFA'
@@ -830,6 +840,7 @@ def merge_info_jsons(keyboard, info_data):
msg = 'Number of keys for %s does not match! info.json specifies %d keys, C macro specifies %d'
_log_error(info_data, msg % (layout_name, len(layout['layout']), len(info_data['layouts'][layout_name]['layout'])))
else:
info_data['layouts'][layout_name]['json_layout'] = True
for new_key, existing_key in zip(layout['layout'], info_data['layouts'][layout_name]['layout']):
existing_key.update(new_key)
else:
@@ -837,6 +848,7 @@ def merge_info_jsons(keyboard, info_data):
_log_error(info_data, f'Layout "{layout_name}" has no "matrix" definition in either "info.json" or "<keyboard>.h"!')
else:
layout['c_macro'] = False
layout['json_layout'] = True
info_data['layouts'][layout_name] = layout
# Update info_data with the new data
@@ -876,6 +888,9 @@ def find_info_json(keyboard):
def keymap_json_config(keyboard, keymap):
"""Extract keymap level config
"""
# TODO: resolve keymap.py and info.py circular dependencies
from qmk.keymap import locate_keymap
keymap_folder = locate_keymap(keyboard, keymap).parent
km_info_json = parse_configurator_json(keymap_folder / 'keymap.json')
@@ -885,6 +900,9 @@ def keymap_json_config(keyboard, keymap):
def keymap_json(keyboard, keymap):
"""Generate the info.json data for a specific keymap.
"""
# TODO: resolve keymap.py and info.py circular dependencies
from qmk.keymap import locate_keymap
keymap_folder = locate_keymap(keyboard, keymap).parent
# Files to scan
+52 -6
View File
@@ -1,16 +1,28 @@
"""Functions that help us generate and use info.json files.
"""
import json
from collections.abc import Mapping
from functools import lru_cache
from pathlib import Path
import hjson
import jsonschema
from collections.abc import Mapping
from functools import lru_cache
from typing import OrderedDict
from pathlib import Path
from milc import cli
def json_load(json_file):
def _dict_raise_on_duplicates(ordered_pairs):
"""Reject duplicate keys."""
d = {}
for k, v in ordered_pairs:
if k in d:
raise ValueError("duplicate key: %r" % (k,))
else:
d[k] = v
return d
def json_load(json_file, strict=True):
"""Load a json file from disk.
Note: file must be a Path object.
@@ -20,7 +32,7 @@ def json_load(json_file):
# Not necessary if the data is provided via stdin
if isinstance(json_file, Path):
json_file = json_file.open(encoding='utf-8')
return hjson.load(json_file)
return hjson.load(json_file, object_pairs_hook=_dict_raise_on_duplicates if strict else None)
except (json.decoder.JSONDecodeError, hjson.HjsonDecodeError) as e:
cli.log.error('Invalid JSON encountered attempting to load {fg_cyan}%s{fg_reset}:\n\t{fg_red}%s', json_file, e)
@@ -90,3 +102,37 @@ def deep_update(origdict, newdict):
origdict[key] = value
return origdict
def merge_ordered_dicts(dicts):
"""Merges nested OrderedDict objects resulting from reading a hjson file.
Later input dicts overrides earlier dicts for plain values.
If any value is "!delete!", the existing value will be removed from its parent.
Arrays will be appended. If the first entry of an array is "!reset!", the contents of the array will be cleared and replaced with RHS.
Dictionaries will be recursively merged. If any entry is "!reset!", the contents of the dictionary will be cleared and replaced with RHS.
"""
result = OrderedDict()
def add_entry(target, k, v):
if k in target and isinstance(v, (OrderedDict, dict)):
if "!reset!" in v:
target[k] = v
else:
target[k] = merge_ordered_dicts([target[k], v])
if "!reset!" in target[k]:
del target[k]["!reset!"]
elif k in target and isinstance(v, list):
if v[0] == '!reset!':
target[k] = v[1:]
else:
target[k] = target[k] + v
elif v == "!delete!" and isinstance(target, (OrderedDict, dict)):
del target[k]
else:
target[k] = v
for d in dicts:
for (k, v) in d.items():
add_entry(result, k, v)
return result
+8 -4
View File
@@ -69,7 +69,7 @@ def keyboard_folder(keyboard):
This checks aliases and DEFAULT_FOLDER to resolve the actual path for a keyboard.
"""
aliases = json_load(Path('data/mappings/keyboard_aliases.json'))
aliases = json_load(Path('data/mappings/keyboard_aliases.hjson'))
if keyboard in aliases:
keyboard = aliases[keyboard].get('target', keyboard)
@@ -98,14 +98,18 @@ def keyboard_completer(prefix, action, parser, parsed_args):
return list_keyboards()
def list_keyboards():
"""Returns a list of all keyboards.
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]
return sorted(set(map(resolve_keyboard, map(_find_name, paths))))
found = map(_find_name, paths)
if resolve_defaults:
found = map(resolve_keyboard, found)
return sorted(set(found))
def resolve_keyboard(keyboard):
+117
View File
@@ -0,0 +1,117 @@
from pathlib import Path
from qmk.json_schema import merge_ordered_dicts, deep_update, json_load, validate
CONSTANTS_PATH = Path('data/constants/')
KEYCODES_PATH = CONSTANTS_PATH / 'keycodes'
EXTRAS_PATH = KEYCODES_PATH / 'extras'
def _find_versions(path, prefix):
ret = []
for file in path.glob(f'{prefix}_[0-9].[0-9].[0-9].hjson'):
ret.append(file.stem.split('_')[-1])
ret.sort(reverse=True)
return ret
def _potential_search_versions(version, lang=None):
versions = list_versions(lang)
versions.reverse()
loc = versions.index(version) + 1
return versions[:loc]
def _search_path(lang=None):
return EXTRAS_PATH if lang else KEYCODES_PATH
def _search_prefix(lang=None):
return f'keycodes_{lang}' if lang else 'keycodes'
def _locate_files(path, prefix, versions):
# collate files by fragment "type"
files = {'_': []}
for version in versions:
files['_'].append(path / f'{prefix}_{version}.hjson')
for file in path.glob(f'{prefix}_{version}_*.hjson'):
fragment = file.stem.replace(f'{prefix}_{version}_', '')
if fragment not in files:
files[fragment] = []
files[fragment].append(file)
return files
def _process_files(files):
# allow override within types of fragments - but not globally
spec = {}
for category in files.values():
specs = []
for file in category:
specs.append(json_load(file))
deep_update(spec, merge_ordered_dicts(specs))
return spec
def _validate(spec):
# first throw it to the jsonschema
validate(spec, 'qmk.keycodes.v1')
# no duplicate keycodes
keycodes = []
for value in spec['keycodes'].values():
keycodes.append(value['key'])
keycodes.extend(value.get('aliases', []))
duplicates = set([x for x in keycodes if keycodes.count(x) > 1])
if duplicates:
raise ValueError(f'Keycode spec contains duplicate keycodes! ({",".join(duplicates)})')
def load_spec(version, lang=None):
"""Build keycode data from the requested spec file
"""
if version == 'latest':
version = list_versions(lang)[0]
path = _search_path(lang)
prefix = _search_prefix(lang)
versions = _potential_search_versions(version, lang)
# Load bases + any fragments
spec = _process_files(_locate_files(path, prefix, versions))
# Sort?
spec['keycodes'] = dict(sorted(spec.get('keycodes', {}).items()))
spec['ranges'] = dict(sorted(spec.get('ranges', {}).items()))
# Validate?
_validate(spec)
return spec
def list_versions(lang=None):
"""Return available versions - sorted newest first
"""
path = _search_path(lang)
prefix = _search_prefix(lang)
return _find_versions(path, prefix)
def list_languages():
"""Return available languages
"""
ret = set()
for file in EXTRAS_PATH.glob('keycodes_*_[0-9].[0-9].[0-9].hjson'):
ret.add(file.stem.split('_')[1])
return ret
+133 -100
View File
@@ -12,8 +12,9 @@ from pygments.token import Token
from pygments import lex
import qmk.path
from qmk.keyboard import find_keyboard_from_dir, rules_mk, keyboard_folder
from qmk.keyboard import find_keyboard_from_dir, keyboard_folder
from qmk.errors import CppError
from qmk.info import info_json
# The `keymap.c` template to use when a keyboard doesn't have its own
DEFAULT_KEYMAP_C = """#include QMK_KEYBOARD_H
@@ -29,9 +30,99 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
__KEYMAP_GOES_HERE__
};
#if defined(ENCODER_ENABLE) && defined(ENCODER_MAP_ENABLE)
const uint16_t PROGMEM encoder_map[][NUM_ENCODERS][2] = {
__ENCODER_MAP_GOES_HERE__
};
#endif // defined(ENCODER_ENABLE) && defined(ENCODER_MAP_ENABLE)
__MACRO_OUTPUT_GOES_HERE__
"""
def _generate_keymap_table(keymap_json):
lines = []
for layer_num, layer in enumerate(keymap_json['layers']):
if layer_num != 0:
lines[-1] = lines[-1] + ','
layer = map(_strip_any, layer)
layer_keys = ', '.join(layer)
lines.append('\t[%s] = %s(%s)' % (layer_num, keymap_json['layout'], layer_keys))
return lines
def _generate_encodermap_table(keymap_json):
lines = []
for layer_num, layer in enumerate(keymap_json['encoders']):
if layer_num != 0:
lines[-1] = lines[-1] + ','
encoder_keycode_txt = ', '.join([f'ENCODER_CCW_CW({_strip_any(e["ccw"])}, {_strip_any(e["cw"])})' for e in layer])
lines.append('\t[%s] = {%s}' % (layer_num, encoder_keycode_txt))
return lines
def _generate_macros_function(keymap_json):
macro_txt = [
'bool process_record_user(uint16_t keycode, keyrecord_t *record) {',
' if (record->event.pressed) {',
' switch (keycode) {',
]
for i, macro_array in enumerate(keymap_json['macros']):
macro = []
for macro_fragment in macro_array:
if isinstance(macro_fragment, str):
macro_fragment = macro_fragment.replace('\\', '\\\\')
macro_fragment = macro_fragment.replace('\r\n', r'\n')
macro_fragment = macro_fragment.replace('\n', r'\n')
macro_fragment = macro_fragment.replace('\r', r'\n')
macro_fragment = macro_fragment.replace('\t', r'\t')
macro_fragment = macro_fragment.replace('"', r'\"')
macro.append(f'"{macro_fragment}"')
elif isinstance(macro_fragment, dict):
newstring = []
if macro_fragment['action'] == 'delay':
newstring.append(f"SS_DELAY({macro_fragment['duration']})")
elif macro_fragment['action'] == 'beep':
newstring.append(r'"\a"')
elif macro_fragment['action'] == 'tap' and len(macro_fragment['keycodes']) > 1:
last_keycode = macro_fragment['keycodes'].pop()
for keycode in macro_fragment['keycodes']:
newstring.append(f'SS_DOWN(X_{keycode})')
newstring.append(f'SS_TAP(X_{last_keycode})')
for keycode in reversed(macro_fragment['keycodes']):
newstring.append(f'SS_UP(X_{keycode})')
else:
for keycode in macro_fragment['keycodes']:
newstring.append(f"SS_{macro_fragment['action'].upper()}(X_{keycode})")
macro.append(''.join(newstring))
new_macro = "".join(macro)
new_macro = new_macro.replace('""', '')
macro_txt.append(f' case QK_MACRO_{i}:')
macro_txt.append(f' SEND_STRING({new_macro});')
macro_txt.append(' return false;')
macro_txt.append(' }')
macro_txt.append(' }')
macro_txt.append('\n return true;')
macro_txt.append('};')
macro_txt.append('')
return macro_txt
def template_json(keyboard):
"""Returns a `keymap.json` template for a keyboard.
@@ -205,83 +296,26 @@ def generate_c(keymap_json):
A sequence of strings containing macros to implement for this keyboard.
"""
new_keymap = template_c(keymap_json['keyboard'])
layer_txt = []
for layer_num, layer in enumerate(keymap_json['layers']):
if layer_num != 0:
layer_txt[-1] = layer_txt[-1] + ','
layer = map(_strip_any, layer)
layer_keys = ', '.join(layer)
layer_txt.append('\t[%s] = %s(%s)' % (layer_num, keymap_json['layout'], layer_keys))
layer_txt = _generate_keymap_table(keymap_json)
keymap = '\n'.join(layer_txt)
new_keymap = new_keymap.replace('__KEYMAP_GOES_HERE__', keymap)
if keymap_json.get('macros'):
macro_txt = [
'bool process_record_user(uint16_t keycode, keyrecord_t *record) {',
' if (record->event.pressed) {',
' switch (keycode) {',
]
encodermap = ''
if 'encoders' in keymap_json and keymap_json['encoders'] is not None:
encoder_txt = _generate_encodermap_table(keymap_json)
encodermap = '\n'.join(encoder_txt)
new_keymap = new_keymap.replace('__ENCODER_MAP_GOES_HERE__', encodermap)
for i, macro_array in enumerate(keymap_json['macros']):
macro = []
macros = ''
if 'macros' in keymap_json and keymap_json['macros'] is not None:
macro_txt = _generate_macros_function(keymap_json)
macros = '\n'.join(macro_txt)
new_keymap = new_keymap.replace('__MACRO_OUTPUT_GOES_HERE__', macros)
for macro_fragment in macro_array:
if isinstance(macro_fragment, str):
macro_fragment = macro_fragment.replace('\\', '\\\\')
macro_fragment = macro_fragment.replace('\r\n', r'\n')
macro_fragment = macro_fragment.replace('\n', r'\n')
macro_fragment = macro_fragment.replace('\r', r'\n')
macro_fragment = macro_fragment.replace('\t', r'\t')
macro_fragment = macro_fragment.replace('"', r'\"')
macro.append(f'"{macro_fragment}"')
elif isinstance(macro_fragment, dict):
newstring = []
if macro_fragment['action'] == 'delay':
newstring.append(f"SS_DELAY({macro_fragment['duration']})")
elif macro_fragment['action'] == 'beep':
newstring.append(r'"\a"')
elif macro_fragment['action'] == 'tap' and len(macro_fragment['keycodes']) > 1:
last_keycode = macro_fragment['keycodes'].pop()
for keycode in macro_fragment['keycodes']:
newstring.append(f'SS_DOWN(X_{keycode})')
newstring.append(f'SS_TAP(X_{last_keycode})')
for keycode in reversed(macro_fragment['keycodes']):
newstring.append(f'SS_UP(X_{keycode})')
else:
for keycode in macro_fragment['keycodes']:
newstring.append(f"SS_{macro_fragment['action'].upper()}(X_{keycode})")
macro.append(''.join(newstring))
new_macro = "".join(macro)
new_macro = new_macro.replace('""', '')
macro_txt.append(f' case MACRO_{i}:')
macro_txt.append(f' SEND_STRING({new_macro});')
macro_txt.append(' return false;')
macro_txt.append(' }')
macro_txt.append(' }')
macro_txt.append('\n return true;')
macro_txt.append('};')
macro_txt.append('')
new_keymap = '\n'.join((new_keymap, *macro_txt))
if keymap_json.get('host_language'):
new_keymap = new_keymap.replace('__INCLUDES__', f'#include "keymap_{keymap_json["host_language"]}.h"\n#include "sendstring_{keymap_json["host_language"]}.h"\n')
else:
new_keymap = new_keymap.replace('__INCLUDES__', '')
hostlang = ''
if 'host_language' in keymap_json and keymap_json['host_language'] is not None:
hostlang = f'#include "keymap_{keymap_json["host_language"]}.h"\n#include "sendstring_{keymap_json["host_language"]}.h"\n'
new_keymap = new_keymap.replace('__INCLUDES__', hostlang)
return new_keymap
@@ -374,11 +408,11 @@ def locate_keymap(keyboard, keymap):
return keymap_path
# Check community layouts as a fallback
rules = rules_mk(keyboard)
info = info_json(keyboard)
if "LAYOUTS" in rules:
for layout in rules["LAYOUTS"].split():
community_layout = Path('layouts/community') / layout / keymap
for community_parent in Path('layouts').glob('*/'):
for layout in info.get("community_layouts", []):
community_layout = community_parent / layout / keymap
if community_layout.exists():
if (community_layout / 'keymap.json').exists():
return community_layout / 'keymap.json'
@@ -408,37 +442,36 @@ def list_keymaps(keyboard, c=True, json=True, additional_files=None, fullpath=Fa
Returns:
a sorted list of valid keymap names.
"""
# parse all the rules.mk files for the keyboard
rules = rules_mk(keyboard)
names = set()
if rules:
keyboards_dir = Path('keyboards')
kb_path = keyboards_dir / keyboard
keyboards_dir = Path('keyboards')
kb_path = keyboards_dir / keyboard
# walk up the directory tree until keyboards_dir
# and collect all directories' name with keymap.c file in it
while kb_path != keyboards_dir:
keymaps_dir = kb_path / "keymaps"
# walk up the directory tree until keyboards_dir
# and collect all directories' name with keymap.c file in it
while kb_path != keyboards_dir:
keymaps_dir = kb_path / "keymaps"
if keymaps_dir.is_dir():
for keymap in keymaps_dir.iterdir():
if keymaps_dir.is_dir():
for keymap in keymaps_dir.iterdir():
if is_keymap_dir(keymap, c, json, additional_files):
keymap = keymap if fullpath else keymap.name
names.add(keymap)
kb_path = kb_path.parent
# Check community layouts as a fallback
info = info_json(keyboard)
for community_parent in Path('layouts').glob('*/'):
for layout in info.get("community_layouts", []):
cl_path = community_parent / layout
if cl_path.is_dir():
for keymap in cl_path.iterdir():
if is_keymap_dir(keymap, c, json, additional_files):
keymap = keymap if fullpath else keymap.name
names.add(keymap)
kb_path = kb_path.parent
# if community layouts are supported, get them
if "LAYOUTS" in rules:
for layout in rules["LAYOUTS"].split():
cl_path = Path('layouts/community') / layout
if cl_path.is_dir():
for keymap in cl_path.iterdir():
if is_keymap_dir(keymap, c, json, additional_files):
keymap = keymap if fullpath else keymap.name
names.add(keymap)
return sorted(names)
+75 -5
View File
@@ -7,6 +7,20 @@ from PIL import Image, ImageOps
# The list of valid formats Quantum Painter supports
valid_formats = {
'rgb888': {
'image_format': 'IMAGE_FORMAT_RGB888',
'bpp': 24,
'has_palette': False,
'num_colors': 16777216,
'image_format_byte': 0x09, # see qp_internal_formats.h
},
'rgb565': {
'image_format': 'IMAGE_FORMAT_RGB565',
'bpp': 16,
'has_palette': False,
'num_colors': 65536,
'image_format_byte': 0x08, # see qp_internal_formats.h
},
'pal256': {
'image_format': 'IMAGE_FORMAT_PALETTE',
'bpp': 8,
@@ -144,19 +158,33 @@ def convert_requested_format(im, format):
ncolors = format["num_colors"]
image_format = format["image_format"]
# Ensure we have a valid number of colors for the palette
if ncolors <= 0 or ncolors > 256 or (ncolors & (ncolors - 1) != 0):
raise ValueError("Number of colors must be 2, 4, 16, or 256.")
# Work out where we're getting the bytes from
if image_format == 'IMAGE_FORMAT_GRAYSCALE':
# Ensure we have a valid number of colors for the palette
if ncolors <= 0 or ncolors > 256 or (ncolors & (ncolors - 1) != 0):
raise ValueError("Number of colors must be 2, 4, 16, or 256.")
# If mono, convert input to grayscale, then to RGB, then grab the raw bytes corresponding to the intensity of the red channel
im = ImageOps.grayscale(im)
im = im.convert("RGB")
elif image_format == 'IMAGE_FORMAT_PALETTE':
# Ensure we have a valid number of colors for the palette
if ncolors <= 0 or ncolors > 256 or (ncolors & (ncolors - 1) != 0):
raise ValueError("Number of colors must be 2, 4, 16, or 256.")
# If color, convert input to RGB, palettize based on the supplied number of colors, then get the raw palette bytes
im = im.convert("RGB")
im = im.convert("P", palette=Image.ADAPTIVE, colors=ncolors)
elif image_format == 'IMAGE_FORMAT_RGB565':
# Ensure we have a valid number of colors for the palette
if ncolors != 65536:
raise ValueError("Number of colors must be 65536.")
# If color, convert input to RGB
im = im.convert("RGB")
elif image_format == 'IMAGE_FORMAT_RGB888':
# Ensure we have a valid number of colors for the palette
if ncolors != 1677216:
raise ValueError("Number of colors must be 16777216.")
# If color, convert input to RGB
im = im.convert("RGB")
return im
@@ -170,8 +198,12 @@ def convert_image_bytes(im, format):
image_format = format["image_format"]
shifter = int(math.log2(ncolors))
pixels_per_byte = int(8 / math.log2(ncolors))
bytes_per_pixel = math.ceil(math.log2(ncolors) / 8)
(width, height) = im.size
expected_byte_count = ((width * height) + (pixels_per_byte - 1)) // pixels_per_byte
if (pixels_per_byte != 0):
expected_byte_count = ((width * height) + (pixels_per_byte - 1)) // pixels_per_byte
else:
expected_byte_count = width * height * bytes_per_pixel
if image_format == 'IMAGE_FORMAT_GRAYSCALE':
# Take the red channel
@@ -212,6 +244,44 @@ def convert_image_bytes(im, format):
byte = byte | ((image_bytes[byte_offset] & (ncolors - 1)) << int(n * shifter))
bytearray.append(byte)
if image_format == 'IMAGE_FORMAT_RGB565':
# Take the red, green, and blue channels
image_bytes_red = im.tobytes("raw", "R")
image_bytes_green = im.tobytes("raw", "G")
image_bytes_blue = im.tobytes("raw", "B")
image_pixels_len = len(image_bytes_red)
# No palette
palette = None
bytearray = []
for x in range(image_pixels_len):
# 5 bits of red, 3 MSb of green
byte = ((image_bytes_red[x] >> 3 & 0x1F) << 3) + (image_bytes_green[x] >> 5 & 0x07)
bytearray.append(byte)
# 3 LSb of green, 5 bits of blue
byte = ((image_bytes_green[x] >> 2 & 0x07) << 5) + (image_bytes_blue[x] >> 3 & 0x1F)
bytearray.append(byte)
if image_format == 'IMAGE_FORMAT_RGB888':
# Take the red, green, and blue channels
image_bytes_red = im.tobytes("raw", "R")
image_bytes_green = im.tobytes("raw", "G")
image_bytes_blue = im.tobytes("raw", "B")
image_pixels_len = len(image_bytes_red)
# No palette
palette = None
bytearray = []
for x in range(image_pixels_len):
byte = image_bytes_red[x]
bytearray.append(byte)
byte = image_bytes_green[x]
bytearray.append(byte)
byte = image_bytes_blue[x]
bytearray.append(byte)
if len(bytearray) != expected_byte_count:
raise Exception(f"Wrong byte count, was {len(bytearray)}, expected {expected_byte_count}")
+11 -15
View File
@@ -21,15 +21,17 @@ def status():
status is None when the submodule doesn't exist, False when it's out of date, and True when it's current
"""
submodules = {}
gitmodule_config = cli.run(['git', 'config', '-f', '.gitmodules', '-l'], timeout=30)
for line in gitmodule_config.stdout.splitlines():
key, value = line.split('=', maxsplit=2)
if key.endswith('.path'):
submodules[value] = {'name': value, 'status': None}
git_cmd = cli.run(['git', 'submodule', 'status'], timeout=30)
for line in git_cmd.stdout.split('\n'):
if not line:
continue
for line in git_cmd.stdout.splitlines():
status = line[0]
githash, submodule = line[1:].split()[:2]
submodules[submodule] = {'name': submodule, 'githash': githash}
submodules[submodule]['githash'] = githash
if status == '-':
submodules[submodule]['status'] = None
@@ -40,11 +42,8 @@ def status():
else:
raise ValueError('Unknown `git submodule status` sha-1 prefix character: "%s"' % status)
submodule_logs = cli.run(['git', 'submodule', '-q', 'foreach', 'git --no-pager log --pretty=format:"$sm_path%x01%h%x01%ad%x01%s%x0A" --date=iso -n1'])
for log_line in submodule_logs.stdout.split('\n'):
if not log_line:
continue
submodule_logs = cli.run(['git', 'submodule', '-q', 'foreach', 'git --no-pager log --no-show-signature --pretty=format:"$sm_path%x01%h%x01%ad%x01%s%x0A" --date=iso -n1'])
for log_line in submodule_logs.stdout.splitlines():
r = log_line.split('\x01')
submodule = r[0]
submodules[submodule]['shorthash'] = r[1] if len(r) > 1 else ''
@@ -52,10 +51,7 @@ def status():
submodules[submodule]['last_log_message'] = r[3] if len(r) > 3 else ''
submodule_tags = cli.run(['git', 'submodule', '-q', 'foreach', '\'echo $sm_path `git describe --tags`\''])
for log_line in submodule_tags.stdout.split('\n'):
if not log_line:
continue
for log_line in submodule_tags.stdout.splitlines():
r = log_line.split()
submodule = r[0]
submodules[submodule]['describe'] = r[1] if len(r) > 1 else ''
+12 -18
View File
@@ -150,8 +150,8 @@ def test_json2c():
def test_json2c_macros():
result = check_subcommand("json2c", 'keyboards/handwired/pytest/macro/keymaps/default/keymap.json')
check_returncode(result)
assert 'LAYOUT_ortho_1x1(MACRO_0)' in result.stdout
assert 'case MACRO_0:' in result.stdout
assert 'LAYOUT_ortho_1x1(QK_MACRO_0)' in result.stdout
assert 'case QK_MACRO_0:' in result.stdout
assert 'SEND_STRING("Hello, World!"SS_TAP(X_ENTER));' in result.stdout
@@ -263,16 +263,16 @@ def test_generate_rgb_breathe_table():
def test_generate_config_h():
result = check_subcommand('generate-config-h', '-kb', 'handwired/pytest/basic')
check_returncode(result)
assert '# define DEVICE_VER 0x0001' in result.stdout
assert '# define DIODE_DIRECTION COL2ROW' in result.stdout
assert '# define MANUFACTURER none' in result.stdout
assert '# define PRODUCT pytest' in result.stdout
assert '# define PRODUCT_ID 0x6465' in result.stdout
assert '# define VENDOR_ID 0xFEED' in result.stdout
assert '# define MATRIX_COLS 1' in result.stdout
assert '# define MATRIX_COL_PINS { F4 }' in result.stdout
assert '# define MATRIX_ROWS 1' in result.stdout
assert '# define MATRIX_ROW_PINS { F5 }' in result.stdout
assert '# define DEVICE_VER 0x0001' in result.stdout
assert '# define DIODE_DIRECTION COL2ROW' in result.stdout
assert '# define MANUFACTURER "none"' in result.stdout
assert '# define PRODUCT "pytest"' in result.stdout
assert '# define PRODUCT_ID 0x6465' in result.stdout
assert '# define VENDOR_ID 0xFEED' in result.stdout
assert '# define MATRIX_COLS 1' in result.stdout
assert '# define MATRIX_COL_PINS { F4 }' in result.stdout
assert '# define MATRIX_ROWS 1' in result.stdout
assert '# define MATRIX_ROW_PINS { F5 }' in result.stdout
def test_generate_rules_mk():
@@ -288,12 +288,6 @@ def test_generate_version_h():
assert '#define QMK_VERSION' in result.stdout
def test_generate_layouts():
result = check_subcommand('generate-layouts', '-kb', 'handwired/pytest/basic')
check_returncode(result)
assert '#define LAYOUT_custom(k0A) {' in result.stdout
def test_format_json_keyboard():
result = check_subcommand('format-json', '--format', 'keyboard', 'lib/python/qmk/tests/minimal_info.json')
check_returncode(result)