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.

501 lines
16 KiB

  1. """Functions that help you work with QMK keymaps.
  2. """
  3. from pathlib import Path
  4. import json
  5. import subprocess
  6. from pygments.lexers.c_cpp import CLexer
  7. from pygments.token import Token
  8. from pygments import lex
  9. from milc import cli
  10. from qmk.keyboard import rules_mk
  11. import qmk.path
  12. import qmk.commands
  13. # The `keymap.c` template to use when a keyboard doesn't have its own
  14. DEFAULT_KEYMAP_C = """#include QMK_KEYBOARD_H
  15. /* THIS FILE WAS GENERATED!
  16. *
  17. * This file was generated by qmk json2c. You may or may not want to
  18. * edit it directly.
  19. */
  20. const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
  21. __KEYMAP_GOES_HERE__
  22. };
  23. """
  24. def template_json(keyboard):
  25. """Returns a `keymap.json` template for a keyboard.
  26. If a template exists in `keyboards/<keyboard>/templates/keymap.json` that text will be used instead of an empty dictionary.
  27. Args:
  28. keyboard
  29. The keyboard to return a template for.
  30. """
  31. template_file = Path('keyboards/%s/templates/keymap.json' % keyboard)
  32. template = {'keyboard': keyboard}
  33. if template_file.exists():
  34. template.update(json.loads(template_file.read_text()))
  35. return template
  36. def template_c(keyboard):
  37. """Returns a `keymap.c` template for a keyboard.
  38. If a template exists in `keyboards/<keyboard>/templates/keymap.c` that text will be used instead of an empty dictionary.
  39. Args:
  40. keyboard
  41. The keyboard to return a template for.
  42. """
  43. template_file = Path('keyboards/%s/templates/keymap.c' % keyboard)
  44. if template_file.exists():
  45. template = template_file.read_text()
  46. else:
  47. template = DEFAULT_KEYMAP_C
  48. return template
  49. def _strip_any(keycode):
  50. """Remove ANY() from a keycode.
  51. """
  52. if keycode.startswith('ANY(') and keycode.endswith(')'):
  53. keycode = keycode[4:-1]
  54. return keycode
  55. def is_keymap_dir(keymap, c=True, json=True, additional_files=None):
  56. """Return True if Path object `keymap` has a keymap file inside.
  57. Args:
  58. keymap
  59. A Path() object for the keymap directory you want to check.
  60. c
  61. When true include `keymap.c` keymaps.
  62. json
  63. When true include `keymap.json` keymaps.
  64. additional_files
  65. A sequence of additional filenames to check against to determine if a directory is a keymap. All files must exist for a match to happen. For example, if you want to match a C keymap with both a `config.h` and `rules.mk` file: `is_keymap_dir(keymap_dir, json=False, additional_files=['config.h', 'rules.mk'])`
  66. """
  67. files = []
  68. if c:
  69. files.append('keymap.c')
  70. if json:
  71. files.append('keymap.json')
  72. for file in files:
  73. if (keymap / file).is_file():
  74. if additional_files:
  75. for file in additional_files:
  76. if not (keymap / file).is_file():
  77. return False
  78. return True
  79. def generate_json(keymap, keyboard, layout, layers):
  80. """Returns a `keymap.json` for the specified keyboard, layout, and layers.
  81. Args:
  82. keymap
  83. A name for this keymap.
  84. keyboard
  85. The name of the keyboard.
  86. layout
  87. The LAYOUT macro this keymap uses.
  88. layers
  89. An array of arrays describing the keymap. Each item in the inner array should be a string that is a valid QMK keycode.
  90. """
  91. new_keymap = template_json(keyboard)
  92. new_keymap['keymap'] = keymap
  93. new_keymap['layout'] = layout
  94. new_keymap['layers'] = layers
  95. return new_keymap
  96. def generate_c(keyboard, layout, layers):
  97. """Returns a `keymap.c` or `keymap.json` for the specified keyboard, layout, and layers.
  98. Args:
  99. keyboard
  100. The name of the keyboard
  101. layout
  102. The LAYOUT macro this keymap uses.
  103. layers
  104. An array of arrays describing the keymap. Each item in the inner array should be a string that is a valid QMK keycode.
  105. """
  106. new_keymap = template_c(keyboard)
  107. layer_txt = []
  108. for layer_num, layer in enumerate(layers):
  109. if layer_num != 0:
  110. layer_txt[-1] = layer_txt[-1] + ','
  111. layer = map(_strip_any, layer)
  112. layer_keys = ', '.join(layer)
  113. layer_txt.append('\t[%s] = %s(%s)' % (layer_num, layout, layer_keys))
  114. keymap = '\n'.join(layer_txt)
  115. new_keymap = new_keymap.replace('__KEYMAP_GOES_HERE__', keymap)
  116. return new_keymap
  117. def write_file(keymap_filename, keymap_content):
  118. keymap_filename.parent.mkdir(parents=True, exist_ok=True)
  119. keymap_filename.write_text(keymap_content)
  120. cli.log.info('Wrote keymap to {fg_cyan}%s', keymap_filename)
  121. return keymap_filename
  122. def write_json(keyboard, keymap, layout, layers):
  123. """Generate the `keymap.json` and write it to disk.
  124. Returns the filename written to.
  125. Args:
  126. keyboard
  127. The name of the keyboard
  128. keymap
  129. The name of the keymap
  130. layout
  131. The LAYOUT macro this keymap uses.
  132. layers
  133. An array of arrays describing the keymap. Each item in the inner array should be a string that is a valid QMK keycode.
  134. """
  135. keymap_json = generate_json(keyboard, keymap, layout, layers)
  136. keymap_content = json.dumps(keymap_json)
  137. keymap_file = qmk.path.keymap(keyboard) / keymap / 'keymap.json'
  138. return write_file(keymap_file, keymap_content)
  139. def write(keyboard, keymap, layout, layers):
  140. """Generate the `keymap.c` and write it to disk.
  141. Returns the filename written to.
  142. Args:
  143. keyboard
  144. The name of the keyboard
  145. keymap
  146. The name of the keymap
  147. layout
  148. The LAYOUT macro this keymap uses.
  149. layers
  150. An array of arrays describing the keymap. Each item in the inner array should be a string that is a valid QMK keycode.
  151. """
  152. keymap_content = generate_c(keyboard, layout, layers)
  153. keymap_file = qmk.path.keymap(keyboard) / keymap / 'keymap.c'
  154. return write_file(keymap_file, keymap_content)
  155. def locate_keymap(keyboard, keymap):
  156. """Returns the path to a keymap for a specific keyboard.
  157. """
  158. if not qmk.path.is_keyboard(keyboard):
  159. raise KeyError('Invalid keyboard: ' + repr(keyboard))
  160. # Check the keyboard folder first, last match wins
  161. checked_dirs = ''
  162. keymap_path = ''
  163. for dir in keyboard.split('/'):
  164. if checked_dirs:
  165. checked_dirs = '/'.join((checked_dirs, dir))
  166. else:
  167. checked_dirs = dir
  168. keymap_dir = Path('keyboards') / checked_dirs / 'keymaps'
  169. if (keymap_dir / keymap / 'keymap.c').exists():
  170. keymap_path = keymap_dir / keymap / 'keymap.c'
  171. if (keymap_dir / keymap / 'keymap.json').exists():
  172. keymap_path = keymap_dir / keymap / 'keymap.json'
  173. if keymap_path:
  174. return keymap_path
  175. # Check community layouts as a fallback
  176. rules = rules_mk(keyboard)
  177. if "LAYOUTS" in rules:
  178. for layout in rules["LAYOUTS"].split():
  179. community_layout = Path('layouts/community') / layout / keymap
  180. if community_layout.exists():
  181. if (community_layout / 'keymap.json').exists():
  182. return community_layout / 'keymap.json'
  183. if (community_layout / 'keymap.c').exists():
  184. return community_layout / 'keymap.c'
  185. def list_keymaps(keyboard, c=True, json=True, additional_files=None, fullpath=False):
  186. """List the available keymaps for a keyboard.
  187. Args:
  188. keyboard
  189. The keyboards full name with vendor and revision if necessary, example: clueboard/66/rev3
  190. c
  191. When true include `keymap.c` keymaps.
  192. json
  193. When true include `keymap.json` keymaps.
  194. additional_files
  195. A sequence of additional filenames to check against to determine if a directory is a keymap. All files must exist for a match to happen. For example, if you want to match a C keymap with both a `config.h` and `rules.mk` file: `is_keymap_dir(keymap_dir, json=False, additional_files=['config.h', 'rules.mk'])`
  196. fullpath
  197. When set to True the full path of the keymap relative to the `qmk_firmware` root will be provided.
  198. Returns:
  199. a sorted list of valid keymap names.
  200. """
  201. # parse all the rules.mk files for the keyboard
  202. rules = rules_mk(keyboard)
  203. names = set()
  204. if rules:
  205. keyboards_dir = Path('keyboards')
  206. kb_path = keyboards_dir / keyboard
  207. # walk up the directory tree until keyboards_dir
  208. # and collect all directories' name with keymap.c file in it
  209. while kb_path != keyboards_dir:
  210. keymaps_dir = kb_path / "keymaps"
  211. if keymaps_dir.is_dir():
  212. for keymap in keymaps_dir.iterdir():
  213. if is_keymap_dir(keymap, c, json, additional_files):
  214. keymap = keymap if fullpath else keymap.name
  215. names.add(keymap)
  216. kb_path = kb_path.parent
  217. # if community layouts are supported, get them
  218. if "LAYOUTS" in rules:
  219. for layout in rules["LAYOUTS"].split():
  220. cl_path = Path('layouts/community') / layout
  221. if cl_path.is_dir():
  222. for keymap in cl_path.iterdir():
  223. if is_keymap_dir(keymap, c, json, additional_files):
  224. keymap = keymap if fullpath else keymap.name
  225. names.add(keymap)
  226. return sorted(names)
  227. def _c_preprocess(path):
  228. """ Run a file through the C pre-processor
  229. Args:
  230. path: path of the keymap.c file
  231. Returns:
  232. the stdout of the pre-processor
  233. """
  234. pre_processed_keymap = qmk.commands.run(['cpp', path], stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
  235. return pre_processed_keymap.stdout
  236. def _get_layers(keymap): # noqa C901 : until someone has a good idea how to simplify/split up this code
  237. """ Find the layers in a keymap.c file.
  238. Args:
  239. keymap: the content of the keymap.c file
  240. Returns:
  241. a dictionary containing the parsed keymap
  242. """
  243. layers = list()
  244. opening_braces = '({['
  245. closing_braces = ')}]'
  246. keymap_certainty = brace_depth = 0
  247. is_keymap = is_layer = is_adv_kc = False
  248. layer = dict(name=False, layout=False, keycodes=list())
  249. for line in lex(keymap, CLexer()):
  250. if line[0] is Token.Name:
  251. if is_keymap:
  252. # If we are inside the keymap array
  253. # we know the keymap's name and the layout macro will come,
  254. # followed by the keycodes
  255. if not layer['name']:
  256. if line[1].startswith('LAYOUT') or line[1].startswith('KEYMAP'):
  257. # This can happen if the keymap array only has one layer,
  258. # for macropads and such
  259. layer['name'] = '0'
  260. layer['layout'] = line[1]
  261. else:
  262. layer['name'] = line[1]
  263. elif not layer['layout']:
  264. layer['layout'] = line[1]
  265. elif is_layer:
  266. # If we are inside a layout macro,
  267. # collect all keycodes
  268. if line[1] == '_______':
  269. kc = 'KC_TRNS'
  270. elif line[1] == 'XXXXXXX':
  271. kc = 'KC_NO'
  272. else:
  273. kc = line[1]
  274. if is_adv_kc:
  275. # If we are inside an advanced keycode
  276. # collect everything and hope the user
  277. # knew what he/she was doing
  278. layer['keycodes'][-1] += kc
  279. else:
  280. layer['keycodes'].append(kc)
  281. # The keymaps array's signature:
  282. # const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS]
  283. #
  284. # Only if we've found all 6 keywords in this specific order
  285. # can we know for sure that we are inside the keymaps array
  286. elif line[1] == 'PROGMEM' and keymap_certainty == 2:
  287. keymap_certainty = 3
  288. elif line[1] == 'keymaps' and keymap_certainty == 3:
  289. keymap_certainty = 4
  290. elif line[1] == 'MATRIX_ROWS' and keymap_certainty == 4:
  291. keymap_certainty = 5
  292. elif line[1] == 'MATRIX_COLS' and keymap_certainty == 5:
  293. keymap_certainty = 6
  294. elif line[0] is Token.Keyword:
  295. if line[1] == 'const' and keymap_certainty == 0:
  296. keymap_certainty = 1
  297. elif line[0] is Token.Keyword.Type:
  298. if line[1] == 'uint16_t' and keymap_certainty == 1:
  299. keymap_certainty = 2
  300. elif line[0] is Token.Punctuation:
  301. if line[1] in opening_braces:
  302. brace_depth += 1
  303. if is_keymap:
  304. if is_layer:
  305. # We found the beginning of a non-basic keycode
  306. is_adv_kc = True
  307. layer['keycodes'][-1] += line[1]
  308. elif line[1] == '(' and brace_depth == 2:
  309. # We found the beginning of a layer
  310. is_layer = True
  311. elif line[1] == '{' and keymap_certainty == 6:
  312. # We found the beginning of the keymaps array
  313. is_keymap = True
  314. elif line[1] in closing_braces:
  315. brace_depth -= 1
  316. if is_keymap:
  317. if is_adv_kc:
  318. layer['keycodes'][-1] += line[1]
  319. if brace_depth == 2:
  320. # We found the end of a non-basic keycode
  321. is_adv_kc = False
  322. elif line[1] == ')' and brace_depth == 1:
  323. # We found the end of a layer
  324. is_layer = False
  325. layers.append(layer)
  326. layer = dict(name=False, layout=False, keycodes=list())
  327. elif line[1] == '}' and brace_depth == 0:
  328. # We found the end of the keymaps array
  329. is_keymap = False
  330. keymap_certainty = 0
  331. elif is_adv_kc:
  332. # Advanced keycodes can contain other punctuation
  333. # e.g.: MT(MOD_LCTL | MOD_LSFT, KC_ESC)
  334. layer['keycodes'][-1] += line[1]
  335. elif line[0] is Token.Literal.Number.Integer and is_keymap and not is_adv_kc:
  336. # If the pre-processor finds the 'meaning' of the layer names,
  337. # they will be numbers
  338. if not layer['name']:
  339. layer['name'] = line[1]
  340. else:
  341. # We only care about
  342. # operators and such if we
  343. # are inside an advanced keycode
  344. # e.g.: MT(MOD_LCTL | MOD_LSFT, KC_ESC)
  345. if is_adv_kc:
  346. layer['keycodes'][-1] += line[1]
  347. return layers
  348. def parse_keymap_c(keymap_file, use_cpp=True):
  349. """ Parse a keymap.c file.
  350. Currently only cares about the keymaps array.
  351. Args:
  352. keymap_file: path of the keymap.c file
  353. use_cpp: if True, pre-process the file with the C pre-processor
  354. Returns:
  355. a dictionary containing the parsed keymap
  356. """
  357. if use_cpp:
  358. keymap_file = _c_preprocess(keymap_file)
  359. else:
  360. keymap_file = keymap_file.read_text()
  361. keymap = dict()
  362. keymap['layers'] = _get_layers(keymap_file)
  363. return keymap
  364. def c2json(keyboard, keymap, keymap_file, use_cpp=True):
  365. """ Convert keymap.c to keymap.json
  366. Args:
  367. keyboard: The name of the keyboard
  368. keymap: The name of the keymap
  369. layout: The LAYOUT macro this keymap uses.
  370. keymap_file: path of the keymap.c file
  371. use_cpp: if True, pre-process the file with the C pre-processor
  372. Returns:
  373. a dictionary in keymap.json format
  374. """
  375. keymap_json = parse_keymap_c(keymap_file, use_cpp)
  376. dirty_layers = keymap_json.pop('layers', None)
  377. keymap_json['layers'] = list()
  378. for layer in dirty_layers:
  379. layer.pop('name')
  380. layout = layer.pop('layout')
  381. if not keymap_json.get('layout', False):
  382. keymap_json['layout'] = layout
  383. keymap_json['layers'].append(layer.pop('keycodes'))
  384. keymap_json['keyboard'] = keyboard
  385. keymap_json['keymap'] = keymap
  386. return keymap_json