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.

706 lines
24 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, keyboard_folder
  14. from qmk.errors import CppError
  15. from qmk.info import 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. __INCLUDES__
  19. /* THIS FILE WAS GENERATED!
  20. *
  21. * This file was generated by qmk json2c. You may or may not want to
  22. * edit it directly.
  23. */
  24. __KEYCODE_OUTPUT_GOES_HERE__
  25. const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
  26. __KEYMAP_GOES_HERE__
  27. };
  28. #if defined(ENCODER_ENABLE) && defined(ENCODER_MAP_ENABLE)
  29. const uint16_t PROGMEM encoder_map[][NUM_ENCODERS][2] = {
  30. __ENCODER_MAP_GOES_HERE__
  31. };
  32. #endif // defined(ENCODER_ENABLE) && defined(ENCODER_MAP_ENABLE)
  33. __MACRO_OUTPUT_GOES_HERE__
  34. """
  35. def _generate_keymap_table(keymap_json):
  36. lines = []
  37. for layer_num, layer in enumerate(keymap_json['layers']):
  38. if layer_num != 0:
  39. lines[-1] = lines[-1] + ','
  40. layer = map(_strip_any, layer)
  41. layer_keys = ', '.join(layer)
  42. lines.append('\t[%s] = %s(%s)' % (layer_num, keymap_json['layout'], layer_keys))
  43. return lines
  44. def _generate_encodermap_table(keymap_json):
  45. lines = []
  46. for layer_num, layer in enumerate(keymap_json['encoders']):
  47. if layer_num != 0:
  48. lines[-1] = lines[-1] + ','
  49. encoder_keycode_txt = ', '.join([f'ENCODER_CCW_CW({_strip_any(e["ccw"])}, {_strip_any(e["cw"])})' for e in layer])
  50. lines.append('\t[%s] = {%s}' % (layer_num, encoder_keycode_txt))
  51. return lines
  52. def _generate_macros_function(keymap_json):
  53. macro_txt = [
  54. 'bool process_record_user(uint16_t keycode, keyrecord_t *record) {',
  55. ' if (record->event.pressed) {',
  56. ' switch (keycode) {',
  57. ]
  58. for i, macro_array in enumerate(keymap_json['macros']):
  59. macro = []
  60. for macro_fragment in macro_array:
  61. if isinstance(macro_fragment, str):
  62. macro_fragment = macro_fragment.replace('\\', '\\\\')
  63. macro_fragment = macro_fragment.replace('\r\n', r'\n')
  64. macro_fragment = macro_fragment.replace('\n', r'\n')
  65. macro_fragment = macro_fragment.replace('\r', r'\n')
  66. macro_fragment = macro_fragment.replace('\t', r'\t')
  67. macro_fragment = macro_fragment.replace('"', r'\"')
  68. macro.append(f'"{macro_fragment}"')
  69. elif isinstance(macro_fragment, dict):
  70. newstring = []
  71. if macro_fragment['action'] == 'delay':
  72. newstring.append(f"SS_DELAY({macro_fragment['duration']})")
  73. elif macro_fragment['action'] == 'beep':
  74. newstring.append(r'"\a"')
  75. elif macro_fragment['action'] == 'tap' and len(macro_fragment['keycodes']) > 1:
  76. last_keycode = macro_fragment['keycodes'].pop()
  77. for keycode in macro_fragment['keycodes']:
  78. newstring.append(f'SS_DOWN(X_{keycode})')
  79. newstring.append(f'SS_TAP(X_{last_keycode})')
  80. for keycode in reversed(macro_fragment['keycodes']):
  81. newstring.append(f'SS_UP(X_{keycode})')
  82. else:
  83. for keycode in macro_fragment['keycodes']:
  84. newstring.append(f"SS_{macro_fragment['action'].upper()}(X_{keycode})")
  85. macro.append(''.join(newstring))
  86. new_macro = "".join(macro)
  87. new_macro = new_macro.replace('""', '')
  88. macro_txt.append(f' case QK_MACRO_{i}:')
  89. macro_txt.append(f' SEND_STRING({new_macro});')
  90. macro_txt.append(' return false;')
  91. macro_txt.append(' }')
  92. macro_txt.append(' }')
  93. macro_txt.append('\n return true;')
  94. macro_txt.append('};')
  95. macro_txt.append('')
  96. return macro_txt
  97. def _generate_keycodes_function(keymap_json):
  98. """Generates keymap level keycodes.
  99. """
  100. lines = []
  101. lines.append('enum keymap_keycodes {')
  102. for index, item in enumerate(keymap_json.get('keycodes', [])):
  103. key = item["key"]
  104. if index == 0:
  105. lines.append(f' {key} = QK_USER_0,')
  106. else:
  107. lines.append(f' {key},')
  108. lines.append('};')
  109. for item in keymap_json.get('keycodes', []):
  110. key = item["key"]
  111. for alias in item.get("aliases", []):
  112. lines.append(f'#define {alias} {key}')
  113. return lines
  114. def template_json(keyboard):
  115. """Returns a `keymap.json` template for a keyboard.
  116. If a template exists in `keyboards/<keyboard>/templates/keymap.json` that text will be used instead of an empty dictionary.
  117. Args:
  118. keyboard
  119. The keyboard to return a template for.
  120. """
  121. template_file = Path('keyboards/%s/templates/keymap.json' % keyboard)
  122. template = {'keyboard': keyboard}
  123. if template_file.exists():
  124. template.update(json.load(template_file.open(encoding='utf-8')))
  125. return template
  126. def template_c(keyboard):
  127. """Returns a `keymap.c` template for a keyboard.
  128. If a template exists in `keyboards/<keyboard>/templates/keymap.c` that text will be used instead of an empty dictionary.
  129. Args:
  130. keyboard
  131. The keyboard to return a template for.
  132. """
  133. template_file = Path('keyboards/%s/templates/keymap.c' % keyboard)
  134. if template_file.exists():
  135. template = template_file.read_text(encoding='utf-8')
  136. else:
  137. template = DEFAULT_KEYMAP_C
  138. return template
  139. def _strip_any(keycode):
  140. """Remove ANY() from a keycode.
  141. """
  142. if keycode.startswith('ANY(') and keycode.endswith(')'):
  143. keycode = keycode[4:-1]
  144. return keycode
  145. def find_keymap_from_dir():
  146. """Returns `(keymap_name, source)` for the directory we're currently in.
  147. """
  148. relative_cwd = qmk.path.under_qmk_firmware()
  149. if relative_cwd and len(relative_cwd.parts) > 1:
  150. # If we're in `qmk_firmware/keyboards` and `keymaps` is in our path, try to find the keyboard name.
  151. if relative_cwd.parts[0] == 'keyboards' and 'keymaps' in relative_cwd.parts:
  152. current_path = Path('/'.join(relative_cwd.parts[1:])) # Strip 'keyboards' from the front
  153. if 'keymaps' in current_path.parts and current_path.name != 'keymaps':
  154. while current_path.parent.name != 'keymaps':
  155. current_path = current_path.parent
  156. return current_path.name, 'keymap_directory'
  157. # If we're in `qmk_firmware/layouts` guess the name from the community keymap they're in
  158. elif relative_cwd.parts[0] == 'layouts' and is_keymap_dir(relative_cwd):
  159. return relative_cwd.name, 'layouts_directory'
  160. # If we're in `qmk_firmware/users` guess the name from the userspace they're in
  161. elif relative_cwd.parts[0] == 'users':
  162. # Guess the keymap name based on which userspace they're in
  163. return relative_cwd.parts[1], 'users_directory'
  164. return None, None
  165. def keymap_completer(prefix, action, parser, parsed_args):
  166. """Returns a list of keymaps for tab completion.
  167. """
  168. try:
  169. if parsed_args.keyboard:
  170. return list_keymaps(parsed_args.keyboard)
  171. keyboard = find_keyboard_from_dir()
  172. if keyboard:
  173. return list_keymaps(keyboard)
  174. except Exception as e:
  175. argcomplete.warn(f'Error: {e.__class__.__name__}: {str(e)}')
  176. return []
  177. return []
  178. def is_keymap_dir(keymap, c=True, json=True, additional_files=None):
  179. """Return True if Path object `keymap` has a keymap file inside.
  180. Args:
  181. keymap
  182. A Path() object for the keymap directory you want to check.
  183. c
  184. When true include `keymap.c` keymaps.
  185. json
  186. When true include `keymap.json` keymaps.
  187. additional_files
  188. 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'])`
  189. """
  190. files = []
  191. if c:
  192. files.append('keymap.c')
  193. if json:
  194. files.append('keymap.json')
  195. for file in files:
  196. if (keymap / file).is_file():
  197. if additional_files:
  198. for additional_file in additional_files:
  199. if not (keymap / additional_file).is_file():
  200. return False
  201. return True
  202. def generate_json(keymap, keyboard, layout, layers, macros=None):
  203. """Returns a `keymap.json` for the specified keyboard, layout, and layers.
  204. Args:
  205. keymap
  206. A name for this keymap.
  207. keyboard
  208. The name of the keyboard.
  209. layout
  210. The LAYOUT macro this keymap uses.
  211. layers
  212. An array of arrays describing the keymap. Each item in the inner array should be a string that is a valid QMK keycode.
  213. macros
  214. A sequence of strings containing macros to implement for this keyboard.
  215. """
  216. new_keymap = template_json(keyboard)
  217. new_keymap['keymap'] = keymap
  218. new_keymap['layout'] = layout
  219. new_keymap['layers'] = layers
  220. if macros:
  221. new_keymap['macros'] = macros
  222. return new_keymap
  223. def generate_c(keymap_json):
  224. """Returns a `keymap.c`.
  225. `keymap_json` is a dictionary with the following keys:
  226. keyboard
  227. The name of the keyboard
  228. layout
  229. The LAYOUT macro this keymap uses.
  230. layers
  231. An array of arrays describing the keymap. Each item in the inner array should be a string that is a valid QMK keycode.
  232. macros
  233. A sequence of strings containing macros to implement for this keyboard.
  234. """
  235. new_keymap = template_c(keymap_json['keyboard'])
  236. layer_txt = _generate_keymap_table(keymap_json)
  237. keymap = '\n'.join(layer_txt)
  238. new_keymap = new_keymap.replace('__KEYMAP_GOES_HERE__', keymap)
  239. encodermap = ''
  240. if 'encoders' in keymap_json and keymap_json['encoders'] is not None:
  241. encoder_txt = _generate_encodermap_table(keymap_json)
  242. encodermap = '\n'.join(encoder_txt)
  243. new_keymap = new_keymap.replace('__ENCODER_MAP_GOES_HERE__', encodermap)
  244. macros = ''
  245. if 'macros' in keymap_json and keymap_json['macros'] is not None:
  246. macro_txt = _generate_macros_function(keymap_json)
  247. macros = '\n'.join(macro_txt)
  248. new_keymap = new_keymap.replace('__MACRO_OUTPUT_GOES_HERE__', macros)
  249. hostlang = ''
  250. if 'host_language' in keymap_json and keymap_json['host_language'] is not None:
  251. hostlang = f'#include "keymap_{keymap_json["host_language"]}.h"\n#include "sendstring_{keymap_json["host_language"]}.h"\n'
  252. new_keymap = new_keymap.replace('__INCLUDES__', hostlang)
  253. keycodes = ''
  254. if 'keycodes' in keymap_json and keymap_json['keycodes'] is not None:
  255. keycodes_txt = _generate_keycodes_function(keymap_json)
  256. keycodes = '\n'.join(keycodes_txt)
  257. new_keymap = new_keymap.replace('__KEYCODE_OUTPUT_GOES_HERE__', keycodes)
  258. return new_keymap
  259. def write_file(keymap_filename, keymap_content):
  260. keymap_filename.parent.mkdir(parents=True, exist_ok=True)
  261. keymap_filename.write_text(keymap_content)
  262. cli.log.info('Wrote keymap to {fg_cyan}%s', keymap_filename)
  263. return keymap_filename
  264. def write_json(keyboard, keymap, layout, layers, macros=None):
  265. """Generate the `keymap.json` and write it to disk.
  266. Returns the filename written to.
  267. Args:
  268. keyboard
  269. The name of the keyboard
  270. keymap
  271. The name of the keymap
  272. layout
  273. The LAYOUT macro this keymap uses.
  274. layers
  275. An array of arrays describing the keymap. Each item in the inner array should be a string that is a valid QMK keycode.
  276. """
  277. keymap_json = generate_json(keyboard, keymap, layout, layers, macros=None)
  278. keymap_content = json.dumps(keymap_json)
  279. keymap_file = qmk.path.keymap(keyboard) / keymap / 'keymap.json'
  280. return write_file(keymap_file, keymap_content)
  281. def write(keymap_json):
  282. """Generate the `keymap.c` and write it to disk.
  283. Returns the filename written to.
  284. `keymap_json` should be a dict with the following keys:
  285. keyboard
  286. The name of the keyboard
  287. keymap
  288. The name of the keymap
  289. layout
  290. The LAYOUT macro this keymap uses.
  291. layers
  292. An array of arrays describing the keymap. Each item in the inner array should be a string that is a valid QMK keycode.
  293. macros
  294. A list of macros for this keymap.
  295. """
  296. keymap_content = generate_c(keymap_json)
  297. keymap_file = qmk.path.keymap(keymap_json['keyboard']) / keymap_json['keymap'] / 'keymap.c'
  298. return write_file(keymap_file, keymap_content)
  299. def locate_keymap(keyboard, keymap):
  300. """Returns the path to a keymap for a specific keyboard.
  301. """
  302. if not qmk.path.is_keyboard(keyboard):
  303. raise KeyError('Invalid keyboard: ' + repr(keyboard))
  304. # Check the keyboard folder first, last match wins
  305. checked_dirs = ''
  306. keymap_path = ''
  307. for dir in keyboard_folder(keyboard).split('/'):
  308. if checked_dirs:
  309. checked_dirs = '/'.join((checked_dirs, dir))
  310. else:
  311. checked_dirs = dir
  312. keymap_dir = Path('keyboards') / checked_dirs / 'keymaps'
  313. if (keymap_dir / keymap / 'keymap.c').exists():
  314. keymap_path = keymap_dir / keymap / 'keymap.c'
  315. if (keymap_dir / keymap / 'keymap.json').exists():
  316. keymap_path = keymap_dir / keymap / 'keymap.json'
  317. if keymap_path:
  318. return keymap_path
  319. # Check community layouts as a fallback
  320. info = info_json(keyboard)
  321. for community_parent in Path('layouts').glob('*/'):
  322. for layout in info.get("community_layouts", []):
  323. community_layout = community_parent / layout / keymap
  324. if community_layout.exists():
  325. if (community_layout / 'keymap.json').exists():
  326. return community_layout / 'keymap.json'
  327. if (community_layout / 'keymap.c').exists():
  328. return community_layout / 'keymap.c'
  329. def list_keymaps(keyboard, c=True, json=True, additional_files=None, fullpath=False):
  330. """List the available keymaps for a keyboard.
  331. Args:
  332. keyboard
  333. The keyboards full name with vendor and revision if necessary, example: clueboard/66/rev3
  334. c
  335. When true include `keymap.c` keymaps.
  336. json
  337. When true include `keymap.json` keymaps.
  338. additional_files
  339. 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'])`
  340. fullpath
  341. When set to True the full path of the keymap relative to the `qmk_firmware` root will be provided.
  342. Returns:
  343. a sorted list of valid keymap names.
  344. """
  345. names = set()
  346. keyboards_dir = Path('keyboards')
  347. kb_path = keyboards_dir / keyboard
  348. # walk up the directory tree until keyboards_dir
  349. # and collect all directories' name with keymap.c file in it
  350. while kb_path != keyboards_dir:
  351. keymaps_dir = kb_path / "keymaps"
  352. if keymaps_dir.is_dir():
  353. for keymap in keymaps_dir.iterdir():
  354. if is_keymap_dir(keymap, c, json, additional_files):
  355. keymap = keymap if fullpath else keymap.name
  356. names.add(keymap)
  357. kb_path = kb_path.parent
  358. # Check community layouts as a fallback
  359. info = info_json(keyboard)
  360. for community_parent in Path('layouts').glob('*/'):
  361. for layout in info.get("community_layouts", []):
  362. cl_path = community_parent / layout
  363. if cl_path.is_dir():
  364. for keymap in cl_path.iterdir():
  365. if is_keymap_dir(keymap, c, json, additional_files):
  366. keymap = keymap if fullpath else keymap.name
  367. names.add(keymap)
  368. return sorted(names)
  369. def _c_preprocess(path, stdin=DEVNULL):
  370. """ Run a file through the C pre-processor
  371. Args:
  372. path: path of the keymap.c file (set None to use stdin)
  373. stdin: stdin pipe (e.g. sys.stdin)
  374. Returns:
  375. the stdout of the pre-processor
  376. """
  377. cmd = ['cpp', str(path)] if path else ['cpp']
  378. pre_processed_keymap = cli.run(cmd, stdin=stdin)
  379. if 'fatal error' in pre_processed_keymap.stderr:
  380. for line in pre_processed_keymap.stderr.split('\n'):
  381. if 'fatal error' in line:
  382. raise (CppError(line))
  383. return pre_processed_keymap.stdout
  384. def _get_layers(keymap): # noqa C901 : until someone has a good idea how to simplify/split up this code
  385. """ Find the layers in a keymap.c file.
  386. Args:
  387. keymap: the content of the keymap.c file
  388. Returns:
  389. a dictionary containing the parsed keymap
  390. """
  391. layers = list()
  392. opening_braces = '({['
  393. closing_braces = ')}]'
  394. keymap_certainty = brace_depth = 0
  395. is_keymap = is_layer = is_adv_kc = False
  396. layer = dict(name=False, layout=False, keycodes=list())
  397. for line in lex(keymap, CLexer()):
  398. if line[0] is Token.Name:
  399. if is_keymap:
  400. # If we are inside the keymap array
  401. # we know the keymap's name and the layout macro will come,
  402. # followed by the keycodes
  403. if not layer['name']:
  404. if line[1].startswith('LAYOUT') or line[1].startswith('KEYMAP'):
  405. # This can happen if the keymap array only has one layer,
  406. # for macropads and such
  407. layer['name'] = '0'
  408. layer['layout'] = line[1]
  409. else:
  410. layer['name'] = line[1]
  411. elif not layer['layout']:
  412. layer['layout'] = line[1]
  413. elif is_layer:
  414. # If we are inside a layout macro,
  415. # collect all keycodes
  416. if line[1] == '_______':
  417. kc = 'KC_TRNS'
  418. elif line[1] == 'XXXXXXX':
  419. kc = 'KC_NO'
  420. else:
  421. kc = line[1]
  422. if is_adv_kc:
  423. # If we are inside an advanced keycode
  424. # collect everything and hope the user
  425. # knew what he/she was doing
  426. layer['keycodes'][-1] += kc
  427. else:
  428. layer['keycodes'].append(kc)
  429. # The keymaps array's signature:
  430. # const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS]
  431. #
  432. # Only if we've found all 6 keywords in this specific order
  433. # can we know for sure that we are inside the keymaps array
  434. elif line[1] == 'PROGMEM' and keymap_certainty == 2:
  435. keymap_certainty = 3
  436. elif line[1] == 'keymaps' and keymap_certainty == 3:
  437. keymap_certainty = 4
  438. elif line[1] == 'MATRIX_ROWS' and keymap_certainty == 4:
  439. keymap_certainty = 5
  440. elif line[1] == 'MATRIX_COLS' and keymap_certainty == 5:
  441. keymap_certainty = 6
  442. elif line[0] is Token.Keyword:
  443. if line[1] == 'const' and keymap_certainty == 0:
  444. keymap_certainty = 1
  445. elif line[0] is Token.Keyword.Type:
  446. if line[1] == 'uint16_t' and keymap_certainty == 1:
  447. keymap_certainty = 2
  448. elif line[0] is Token.Punctuation:
  449. if line[1] in opening_braces:
  450. brace_depth += 1
  451. if is_keymap:
  452. if is_layer:
  453. # We found the beginning of a non-basic keycode
  454. is_adv_kc = True
  455. layer['keycodes'][-1] += line[1]
  456. elif line[1] == '(' and brace_depth == 2:
  457. # We found the beginning of a layer
  458. is_layer = True
  459. elif line[1] == '{' and keymap_certainty == 6:
  460. # We found the beginning of the keymaps array
  461. is_keymap = True
  462. elif line[1] in closing_braces:
  463. brace_depth -= 1
  464. if is_keymap:
  465. if is_adv_kc:
  466. layer['keycodes'][-1] += line[1]
  467. if brace_depth == 2:
  468. # We found the end of a non-basic keycode
  469. is_adv_kc = False
  470. elif line[1] == ')' and brace_depth == 1:
  471. # We found the end of a layer
  472. is_layer = False
  473. layers.append(layer)
  474. layer = dict(name=False, layout=False, keycodes=list())
  475. elif line[1] == '}' and brace_depth == 0:
  476. # We found the end of the keymaps array
  477. is_keymap = False
  478. keymap_certainty = 0
  479. elif is_adv_kc:
  480. # Advanced keycodes can contain other punctuation
  481. # e.g.: MT(MOD_LCTL | MOD_LSFT, KC_ESC)
  482. layer['keycodes'][-1] += line[1]
  483. elif line[0] is Token.Literal.Number.Integer and is_keymap and not is_adv_kc:
  484. # If the pre-processor finds the 'meaning' of the layer names,
  485. # they will be numbers
  486. if not layer['name']:
  487. layer['name'] = line[1]
  488. else:
  489. # We only care about
  490. # operators and such if we
  491. # are inside an advanced keycode
  492. # e.g.: MT(MOD_LCTL | MOD_LSFT, KC_ESC)
  493. if is_adv_kc:
  494. layer['keycodes'][-1] += line[1]
  495. return layers
  496. def parse_keymap_c(keymap_file, use_cpp=True):
  497. """ Parse a keymap.c file.
  498. Currently only cares about the keymaps array.
  499. Args:
  500. keymap_file: path of the keymap.c file (or '-' to use stdin)
  501. use_cpp: if True, pre-process the file with the C pre-processor
  502. Returns:
  503. a dictionary containing the parsed keymap
  504. """
  505. if keymap_file == '-':
  506. if use_cpp:
  507. keymap_file = _c_preprocess(None, sys.stdin)
  508. else:
  509. keymap_file = sys.stdin.read()
  510. else:
  511. if use_cpp:
  512. keymap_file = _c_preprocess(keymap_file)
  513. else:
  514. keymap_file = keymap_file.read_text(encoding='utf-8')
  515. keymap = dict()
  516. keymap['layers'] = _get_layers(keymap_file)
  517. return keymap
  518. def c2json(keyboard, keymap, keymap_file, use_cpp=True):
  519. """ Convert keymap.c to keymap.json
  520. Args:
  521. keyboard: The name of the keyboard
  522. keymap: The name of the keymap
  523. layout: The LAYOUT macro this keymap uses.
  524. keymap_file: path of the keymap.c file
  525. use_cpp: if True, pre-process the file with the C pre-processor
  526. Returns:
  527. a dictionary in keymap.json format
  528. """
  529. keymap_json = parse_keymap_c(keymap_file, use_cpp)
  530. dirty_layers = keymap_json.pop('layers', None)
  531. keymap_json['layers'] = list()
  532. for layer in dirty_layers:
  533. layer.pop('name')
  534. layout = layer.pop('layout')
  535. if not keymap_json.get('layout', False):
  536. keymap_json['layout'] = layout
  537. keymap_json['layers'].append(layer.pop('keycodes'))
  538. keymap_json['keyboard'] = keyboard
  539. keymap_json['keymap'] = keymap
  540. return keymap_json