keyboard.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. """Functions that help us work with keyboards.
  2. """
  3. from array import array
  4. from math import ceil
  5. from pathlib import Path
  6. import os
  7. import shutil
  8. from glob import glob
  9. import qmk.path
  10. from qmk.c_parse import parse_config_h_file
  11. from qmk.json_schema import json_load
  12. from qmk.makefile import parse_rules_mk_file
  13. KEY_WIDTH = 4 if shutil.get_terminal_size().columns < 160 else 6
  14. BOX_DRAWING_CHARACTERS = {
  15. "unicode": {
  16. "tl": "┌",
  17. "tr": "┐",
  18. "bl": "└",
  19. "br": "┘",
  20. "v": "│",
  21. "h": "─",
  22. },
  23. "ascii": {
  24. "tl": " ",
  25. "tr": " ",
  26. "bl": "|",
  27. "br": "|",
  28. "v": "|",
  29. "h": "_",
  30. },
  31. }
  32. base_path = os.path.join(os.getcwd(), "keyboards") + os.path.sep
  33. def find_keyboard_from_dir():
  34. """Returns a keyboard name based on the user's current directory.
  35. """
  36. relative_cwd = qmk.path.under_qmk_firmware()
  37. if relative_cwd and len(relative_cwd.parts) > 1 and relative_cwd.parts[0] == 'keyboards':
  38. # Attempt to extract the keyboard name from the current directory
  39. current_path = Path('/'.join(relative_cwd.parts[1:]))
  40. if 'keymaps' in current_path.parts:
  41. # Strip current_path of anything after `keymaps`
  42. keymap_index = len(current_path.parts) - current_path.parts.index('keymaps') - 1
  43. current_path = current_path.parents[keymap_index]
  44. if qmk.path.is_keyboard(current_path):
  45. return str(current_path)
  46. def find_readme(keyboard):
  47. """Returns the readme for this keyboard.
  48. """
  49. cur_dir = qmk.path.keyboard(keyboard)
  50. keyboards_dir = Path('keyboards')
  51. while not (cur_dir / 'readme.md').exists():
  52. if cur_dir == keyboards_dir:
  53. return None
  54. cur_dir = cur_dir.parent
  55. return cur_dir / 'readme.md'
  56. def keyboard_folder(keyboard):
  57. """Returns the actual keyboard folder.
  58. This checks aliases and DEFAULT_FOLDER to resolve the actual path for a keyboard.
  59. """
  60. aliases = json_load(Path('data/mappings/keyboard_aliases.hjson'))
  61. if keyboard in aliases:
  62. keyboard = aliases[keyboard].get('target', keyboard)
  63. rules_mk_file = Path(base_path, keyboard, 'rules.mk')
  64. if rules_mk_file.exists():
  65. rules_mk = parse_rules_mk_file(rules_mk_file)
  66. keyboard = rules_mk.get('DEFAULT_FOLDER', keyboard)
  67. if not qmk.path.is_keyboard(keyboard):
  68. raise ValueError(f'Invalid keyboard: {keyboard}')
  69. return keyboard
  70. def _find_name(path):
  71. """Determine the keyboard name by stripping off the base_path and rules.mk.
  72. """
  73. return path.replace(base_path, "").replace(os.path.sep + "rules.mk", "")
  74. def keyboard_completer(prefix, action, parser, parsed_args):
  75. """Returns a list of keyboards for tab completion.
  76. """
  77. return list_keyboards()
  78. def list_keyboards(resolve_defaults=True):
  79. """Returns a list of all keyboards - optionally processing any DEFAULT_FOLDER.
  80. """
  81. # We avoid pathlib here because this is performance critical code.
  82. kb_wildcard = os.path.join(base_path, "**", "rules.mk")
  83. paths = [path for path in glob(kb_wildcard, recursive=True) if os.path.sep + 'keymaps' + os.path.sep not in path]
  84. found = map(_find_name, paths)
  85. if resolve_defaults:
  86. found = map(resolve_keyboard, found)
  87. return sorted(set(found))
  88. def resolve_keyboard(keyboard):
  89. cur_dir = Path('keyboards')
  90. rules = parse_rules_mk_file(cur_dir / keyboard / 'rules.mk')
  91. while 'DEFAULT_FOLDER' in rules and keyboard != rules['DEFAULT_FOLDER']:
  92. keyboard = rules['DEFAULT_FOLDER']
  93. rules = parse_rules_mk_file(cur_dir / keyboard / 'rules.mk')
  94. return keyboard
  95. def config_h(keyboard):
  96. """Parses all the config.h files for a keyboard.
  97. Args:
  98. keyboard: name of the keyboard
  99. Returns:
  100. a dictionary representing the content of the entire config.h tree for a keyboard
  101. """
  102. config = {}
  103. cur_dir = Path('keyboards')
  104. keyboard = Path(resolve_keyboard(keyboard))
  105. for dir in keyboard.parts:
  106. cur_dir = cur_dir / dir
  107. config = {**config, **parse_config_h_file(cur_dir / 'config.h')}
  108. return config
  109. def rules_mk(keyboard):
  110. """Get a rules.mk for a keyboard
  111. Args:
  112. keyboard: name of the keyboard
  113. Returns:
  114. a dictionary representing the content of the entire rules.mk tree for a keyboard
  115. """
  116. cur_dir = Path('keyboards')
  117. keyboard = Path(resolve_keyboard(keyboard))
  118. rules = parse_rules_mk_file(cur_dir / keyboard / 'rules.mk')
  119. for i, dir in enumerate(keyboard.parts):
  120. cur_dir = cur_dir / dir
  121. rules = parse_rules_mk_file(cur_dir / 'rules.mk', rules)
  122. return rules
  123. def render_layout(layout_data, render_ascii, key_labels=None):
  124. """Renders a single layout.
  125. """
  126. textpad = [array('u', ' ' * 200) for x in range(100)]
  127. style = 'ascii' if render_ascii else 'unicode'
  128. for key in layout_data:
  129. x = key.get('x', 0)
  130. y = key.get('y', 0)
  131. w = key.get('w', 1)
  132. h = key.get('h', 1)
  133. if key_labels:
  134. label = key_labels.pop(0)
  135. if label.startswith('KC_'):
  136. label = label[3:]
  137. else:
  138. label = key.get('label', '')
  139. if x >= 0.25 and w == 1.25 and h == 2:
  140. render_key_isoenter(textpad, x, y, w, h, label, style)
  141. elif w == 2.25 and h == 2:
  142. render_key_baenter(textpad, x, y, w, h, label, style)
  143. else:
  144. render_key_rect(textpad, x, y, w, h, label, style)
  145. lines = []
  146. for line in textpad:
  147. if line.tounicode().strip():
  148. lines.append(line.tounicode().rstrip())
  149. return '\n'.join(lines)
  150. def render_layouts(info_json, render_ascii):
  151. """Renders all the layouts from an `info_json` structure.
  152. """
  153. layouts = {}
  154. for layout in info_json['layouts']:
  155. layout_data = info_json['layouts'][layout]['layout']
  156. layouts[layout] = render_layout(layout_data, render_ascii)
  157. return layouts
  158. def render_key_rect(textpad, x, y, w, h, label, style):
  159. box_chars = BOX_DRAWING_CHARACTERS[style]
  160. x = ceil(x * KEY_WIDTH)
  161. y = ceil(y * 3)
  162. w = ceil(w * KEY_WIDTH)
  163. h = ceil(h * 3)
  164. label_len = w - 2
  165. label_leftover = label_len - len(label)
  166. if len(label) > label_len:
  167. label = label[:label_len]
  168. label_blank = ' ' * label_len
  169. label_border = box_chars['h'] * label_len
  170. label_middle = label + ' ' * label_leftover
  171. top_line = array('u', box_chars['tl'] + label_border + box_chars['tr'])
  172. lab_line = array('u', box_chars['v'] + label_middle + box_chars['v'])
  173. mid_line = array('u', box_chars['v'] + label_blank + box_chars['v'])
  174. bot_line = array('u', box_chars['bl'] + label_border + box_chars['br'])
  175. textpad[y][x:x + w] = top_line
  176. textpad[y + 1][x:x + w] = lab_line
  177. for i in range(h - 3):
  178. textpad[y + i + 2][x:x + w] = mid_line
  179. textpad[y + h - 1][x:x + w] = bot_line
  180. def render_key_isoenter(textpad, x, y, w, h, label, style):
  181. box_chars = BOX_DRAWING_CHARACTERS[style]
  182. x = ceil(x * KEY_WIDTH)
  183. y = ceil(y * 3)
  184. w = ceil(w * KEY_WIDTH)
  185. h = ceil(h * 3)
  186. label_len = w - 1
  187. label_leftover = label_len - len(label)
  188. if len(label) > label_len:
  189. label = label[:label_len]
  190. label_blank = ' ' * (label_len - 1)
  191. label_border_top = box_chars['h'] * label_len
  192. label_border_bottom = box_chars['h'] * (label_len - 1)
  193. label_middle = label + ' ' * label_leftover
  194. top_line = array('u', box_chars['tl'] + label_border_top + box_chars['tr'])
  195. lab_line = array('u', box_chars['v'] + label_middle + box_chars['v'])
  196. crn_line = array('u', box_chars['bl'] + box_chars['tr'] + label_blank + box_chars['v'])
  197. mid_line = array('u', box_chars['v'] + label_blank + box_chars['v'])
  198. bot_line = array('u', box_chars['bl'] + label_border_bottom + box_chars['br'])
  199. textpad[y][x - 1:x + w] = top_line
  200. textpad[y + 1][x - 1:x + w] = lab_line
  201. textpad[y + 2][x - 1:x + w] = crn_line
  202. textpad[y + 3][x:x + w] = mid_line
  203. textpad[y + 4][x:x + w] = mid_line
  204. textpad[y + 5][x:x + w] = bot_line
  205. def render_key_baenter(textpad, x, y, w, h, label, style):
  206. box_chars = BOX_DRAWING_CHARACTERS[style]
  207. x = ceil(x * KEY_WIDTH)
  208. y = ceil(y * 3)
  209. w = ceil(w * KEY_WIDTH)
  210. h = ceil(h * 3)
  211. label_len = w - 2
  212. label_leftover = label_len - len(label)
  213. if len(label) > label_len:
  214. label = label[:label_len]
  215. label_blank = ' ' * (label_len - 3)
  216. label_border_top = box_chars['h'] * (label_len - 3)
  217. label_border_bottom = box_chars['h'] * label_len
  218. label_middle = label + ' ' * label_leftover
  219. top_line = array('u', box_chars['tl'] + label_border_top + box_chars['tr'])
  220. mid_line = array('u', box_chars['v'] + label_blank + box_chars['v'])
  221. crn_line = array('u', box_chars['tl'] + box_chars['h'] + box_chars['h'] + box_chars['br'] + label_blank + box_chars['v'])
  222. lab_line = array('u', box_chars['v'] + label_middle + box_chars['v'])
  223. bot_line = array('u', box_chars['bl'] + label_border_bottom + box_chars['br'])
  224. textpad[y][x + 3:x + w] = top_line
  225. textpad[y + 1][x + 3:x + w] = mid_line
  226. textpad[y + 2][x + 3:x + w] = mid_line
  227. textpad[y + 3][x:x + w] = crn_line
  228. textpad[y + 4][x:x + w] = lab_line
  229. textpad[y + 5][x:x + w] = bot_line