You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
3.8 KiB

  1. """Functions that help you work with QMK keymaps.
  2. """
  3. import os
  4. from pathlib import Path
  5. import qmk.path
  6. import qmk.makefile
  7. # The `keymap.c` template to use when a keyboard doesn't have its own
  8. DEFAULT_KEYMAP_C = """#include QMK_KEYBOARD_H
  9. /* THIS FILE WAS GENERATED!
  10. *
  11. * This file was generated by qmk-compile-json. You may or may not want to
  12. * edit it directly.
  13. */
  14. const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
  15. __KEYMAP_GOES_HERE__
  16. };
  17. """
  18. def template(keyboard):
  19. """Returns the `keymap.c` template for a keyboard.
  20. If a template exists in `keyboards/<keyboard>/templates/keymap.c` that
  21. text will be used instead of `DEFAULT_KEYMAP_C`.
  22. Args:
  23. keyboard
  24. The keyboard to return a template for.
  25. """
  26. template_file = Path('keyboards/%s/templates/keymap.c' % keyboard)
  27. if template_file.exists():
  28. return template_file.read_text()
  29. return DEFAULT_KEYMAP_C
  30. def generate(keyboard, layout, layers):
  31. """Returns a keymap.c for the specified keyboard, layout, and layers.
  32. Args:
  33. keyboard
  34. The name of the keyboard
  35. layout
  36. The LAYOUT macro this keymap uses.
  37. layers
  38. An array of arrays describing the keymap. Each item in the inner array should be a string that is a valid QMK keycode.
  39. """
  40. layer_txt = []
  41. for layer_num, layer in enumerate(layers):
  42. if layer_num != 0:
  43. layer_txt[-1] = layer_txt[-1] + ','
  44. layer_keys = ', '.join(layer)
  45. layer_txt.append('\t[%s] = %s(%s)' % (layer_num, layout, layer_keys))
  46. keymap = '\n'.join(layer_txt)
  47. keymap_c = template(keyboard)
  48. return keymap_c.replace('__KEYMAP_GOES_HERE__', keymap)
  49. def write(keyboard, keymap, layout, layers):
  50. """Generate the `keymap.c` and write it to disk.
  51. Returns the filename written to.
  52. Args:
  53. keyboard
  54. The name of the keyboard
  55. keymap
  56. The name of the keymap
  57. layout
  58. The LAYOUT macro this keymap uses.
  59. layers
  60. An array of arrays describing the keymap. Each item in the inner array should be a string that is a valid QMK keycode.
  61. """
  62. keymap_c = generate(keyboard, layout, layers)
  63. keymap_file = qmk.path.keymap(keyboard) / keymap / 'keymap.c'
  64. keymap_file.parent.mkdir(parents=True, exist_ok=True)
  65. keymap_file.write_text(keymap_c)
  66. return keymap_file
  67. def list_keymaps(keyboard_name):
  68. """ List the available keymaps for a keyboard.
  69. Args:
  70. keyboard_name: the keyboards full name with vendor and revision if necessary, example: clueboard/66/rev3
  71. Returns:
  72. a set with the names of the available keymaps
  73. """
  74. # parse all the rules.mk files for the keyboard
  75. rules_mk = qmk.makefile.get_rules_mk(keyboard_name)
  76. names = set()
  77. if rules_mk:
  78. # qmk_firmware/keyboards
  79. keyboards_dir = Path.cwd() / "keyboards"
  80. # path to the keyboard's directory
  81. kb_path = keyboards_dir / keyboard_name
  82. # walk up the directory tree until keyboards_dir
  83. # and collect all directories' name with keymap.c file in it
  84. while kb_path != keyboards_dir:
  85. keymaps_dir = kb_path / "keymaps"
  86. if keymaps_dir.exists():
  87. names = names.union([keymap for keymap in os.listdir(str(keymaps_dir)) if (keymaps_dir / keymap / "keymap.c").is_file()])
  88. kb_path = kb_path.parent
  89. # if community layouts are supported, get them
  90. if "LAYOUTS" in rules_mk:
  91. for layout in rules_mk["LAYOUTS"].split():
  92. cl_path = Path.cwd() / "layouts" / "community" / layout
  93. if cl_path.exists():
  94. names = names.union([keymap for keymap in os.listdir(str(cl_path)) if (cl_path / keymap / "keymap.c").is_file()])
  95. return sorted(names)