keymap.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. """Functions that help you work with QMK keymaps.
  2. """
  3. import os
  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-compile-json. 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_name = 'keyboards/%s/templates/keymap.c' % keyboard
  26. if os.path.exists(template_name):
  27. with open(template_name, 'r') as fd:
  28. return fd.read()
  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_path = qmk.path.keymap(keyboard)
  64. keymap_dir = os.path.join(keymap_path, keymap)
  65. keymap_file = os.path.join(keymap_dir, 'keymap.c')
  66. if not os.path.exists(keymap_dir):
  67. os.makedirs(keymap_dir)
  68. with open(keymap_file, 'w') as keymap_fd:
  69. keymap_fd.write(keymap_c)
  70. return keymap_file
  71. def list_keymaps(keyboard_name):
  72. """ List the available keymaps for a keyboard.
  73. Args:
  74. keyboard_name: the keyboards full name with vendor and revision if necessary, example: clueboard/66/rev3
  75. Returns:
  76. a set with the names of the available keymaps
  77. """
  78. # parse all the rules.mk files for the keyboard
  79. rules_mk = qmk.makefile.get_rules_mk(keyboard_name)
  80. names = set()
  81. if rules_mk:
  82. # qmk_firmware/keyboards
  83. keyboards_dir = os.path.join(os.getcwd(), "keyboards")
  84. # path to the keyboard's directory
  85. kb_path = os.path.join(keyboards_dir, keyboard_name)
  86. # walk up the directory tree until keyboards_dir
  87. # and collect all directories' name with keymap.c file in it
  88. while kb_path != keyboards_dir:
  89. keymaps_dir = os.path.join(kb_path, "keymaps")
  90. if os.path.exists(keymaps_dir):
  91. names = names.union([keymap for keymap in os.listdir(keymaps_dir) if os.path.isfile(os.path.join(keymaps_dir, keymap, "keymap.c"))])
  92. kb_path = os.path.dirname(kb_path)
  93. # if community layouts are supported, get them
  94. if "LAYOUTS" in rules_mk:
  95. for layout in rules_mk["LAYOUTS"].split():
  96. cl_path = os.path.join(os.getcwd(), "layouts", "community", layout)
  97. if os.path.exists(cl_path):
  98. names = names.union([keymap for keymap in os.listdir(cl_path) if os.path.isfile(os.path.join(cl_path, keymap, "keymap.c"))])
  99. return sorted(names)