keymap.py 4.0 KB

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