keycodes_tests.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. """Used by the make system to generate a keycode lookup table from keycodes_{version}.json
  2. """
  3. from milc import cli
  4. from qmk.constants import GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE
  5. from qmk.commands import dump_lines
  6. from qmk.path import normpath
  7. from qmk.keycodes import load_spec
  8. def _generate_defines(lines, keycodes):
  9. lines.append('')
  10. lines.append('std::map<uint16_t, std::string> KEYCODE_ID_TABLE = {')
  11. for key, value in keycodes["keycodes"].items():
  12. lines.append(f' {{{value.get("key")}, "{value.get("key")}"}},')
  13. lines.append('};')
  14. @cli.argument('-v', '--version', arg_only=True, required=True, help='Version of keycodes to generate.')
  15. @cli.argument('-o', '--output', arg_only=True, type=normpath, help='File to write to')
  16. @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
  17. @cli.subcommand('Used by the make system to generate a keycode lookup table from keycodes_{version}.json', hidden=True)
  18. def generate_keycodes_tests(cli):
  19. """Generates a keycode to identifier lookup table for unit test output.
  20. """
  21. # Build the keycodes.h file.
  22. keycodes_h_lines = [GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE, '// clang-format off']
  23. keycodes_h_lines.append('extern "C" {\n#include <keycode.h>\n}')
  24. keycodes_h_lines.append('#include <map>')
  25. keycodes_h_lines.append('#include <string>')
  26. keycodes_h_lines.append('#include <cstdint>')
  27. keycodes = load_spec(cli.args.version)
  28. _generate_defines(keycodes_h_lines, keycodes)
  29. # Show the results
  30. dump_lines(cli.args.output, keycodes_h_lines, cli.args.quiet)