logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

keymap_h.py (1806B)


  1. from argcomplete.completers import FilesCompleter
  2. from milc import cli
  3. import qmk.path
  4. from qmk.commands import dump_lines
  5. from qmk.commands import parse_configurator_json
  6. from qmk.constants import GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE
  7. def _generate_keycodes_function(keymap_json):
  8. """Generates keymap level keycodes.
  9. """
  10. lines = []
  11. lines.append('enum keymap_keycodes {')
  12. for index, item in enumerate(keymap_json.get('keycodes', [])):
  13. key = item["key"]
  14. if index == 0:
  15. lines.append(f' {key} = QK_USER_0,')
  16. else:
  17. lines.append(f' {key},')
  18. lines.append('};')
  19. for item in keymap_json.get('keycodes', []):
  20. key = item["key"]
  21. for alias in item.get("aliases", []):
  22. lines.append(f'#define {alias} {key}')
  23. return lines
  24. @cli.argument('-o', '--output', arg_only=True, type=qmk.path.normpath, help='File to write to')
  25. @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
  26. @cli.argument('filename', type=qmk.path.FileType('r'), arg_only=True, completer=FilesCompleter('.json'), help='Configurator JSON file')
  27. @cli.subcommand('Creates a keymap.h from a QMK Configurator export.')
  28. def generate_keymap_h(cli):
  29. """Creates a keymap.h from a QMK Configurator export
  30. """
  31. if cli.args.output and cli.args.output.name == '-':
  32. cli.args.output = None
  33. keymap_h_lines = [GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE, '#pragma once', '// clang-format off']
  34. keymap_json = parse_configurator_json(cli.args.filename)
  35. if 'keycodes' in keymap_json and keymap_json['keycodes'] is not None:
  36. keymap_h_lines += _generate_keycodes_function(keymap_json)
  37. dump_lines(cli.args.output, keymap_h_lines, cli.args.quiet)