keymap.py 17 KB

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