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.

560 lines
18 KiB

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