keymap.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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_name = 'keyboards/%s/templates/keymap.c' % keyboard
  27. if os.path.exists(template_name):
  28. with open(template_name, 'r') as fd:
  29. return fd.read()
  30. return DEFAULT_KEYMAP_C
  31. def generate(keyboard, layout, layers):
  32. """Returns a keymap.c for the specified keyboard, layout, and layers.
  33. Args:
  34. keyboard
  35. The name of the keyboard
  36. layout
  37. The LAYOUT macro this keymap uses.
  38. layers
  39. An array of arrays describing the keymap. Each item in the inner array should be a string that is a valid QMK keycode.
  40. """
  41. layer_txt = []
  42. for layer_num, layer in enumerate(layers):
  43. if layer_num != 0:
  44. layer_txt[-1] = layer_txt[-1] + ','
  45. layer_keys = ', '.join(layer)
  46. layer_txt.append('\t[%s] = %s(%s)' % (layer_num, layout, layer_keys))
  47. keymap = '\n'.join(layer_txt)
  48. keymap_c = template(keyboard)
  49. return keymap_c.replace('__KEYMAP_GOES_HERE__', keymap)
  50. def write(keyboard, keymap, layout, layers):
  51. """Generate the `keymap.c` and write it to disk.
  52. Returns the filename written to.
  53. Args:
  54. keyboard
  55. The name of the keyboard
  56. keymap
  57. The name of the keymap
  58. layout
  59. The LAYOUT macro this keymap uses.
  60. layers
  61. An array of arrays describing the keymap. Each item in the inner array should be a string that is a valid QMK keycode.
  62. """
  63. keymap_c = generate(keyboard, layout, layers)
  64. keymap_path = qmk.path.keymap(keyboard)
  65. keymap_dir = os.path.join(keymap_path, keymap)
  66. keymap_file = os.path.join(keymap_dir, 'keymap.c')
  67. if not os.path.exists(keymap_dir):
  68. os.makedirs(keymap_dir)
  69. with open(keymap_file, 'w') as keymap_fd:
  70. keymap_fd.write(keymap_c)
  71. return keymap_file
  72. def list_keymaps(keyboard_name):
  73. """ List the available keymaps for a keyboard.
  74. Args:
  75. keyboard_name: the keyboards full name with vendor and revision if necessary, example: clueboard/66/rev3
  76. Returns:
  77. a set with the names of the available keymaps
  78. """
  79. # parse all the rules.mk files for the keyboard
  80. rules_mk = qmk.makefile.get_rules_mk(keyboard_name)
  81. names = set()
  82. if rules_mk:
  83. # qmk_firmware/keyboards
  84. keyboards_dir = Path.cwd() / "keyboards"
  85. # path to the keyboard's directory
  86. kb_path = keyboards_dir / keyboard_name
  87. # walk up the directory tree until keyboards_dir
  88. # and collect all directories' name with keymap.c file in it
  89. while kb_path != keyboards_dir:
  90. keymaps_dir = kb_path / "keymaps"
  91. if keymaps_dir.exists():
  92. names = names.union([keymap for keymap in os.listdir(keymaps_dir) if (keymaps_dir / keymap / "keymap.c").is_file()])
  93. kb_path = kb_path.parent
  94. # if community layouts are supported, get them
  95. if "LAYOUTS" in rules_mk:
  96. for layout in rules_mk["LAYOUTS"].split():
  97. cl_path = Path.cwd() / "layouts" / "community" / layout
  98. if cl_path.exists():
  99. names = names.union([keymap for keymap in os.listdir(cl_path) if (cl_path / keymap / "keymap.c").is_file()])
  100. return sorted(names)