Files
zsa_qmk_firmware/lib/python/qmk/keymap.py

502 lines
16 KiB
Python
Raw Normal View History

"""Functions that help you work with QMK keymaps.
"""
CLI: add support for list_keymaps List all the available keymaps for a given keyboard Add bs4 to requirements.txt UnicodeDammit is needed from bs4 for reading files. Major update to work better with revisions Find the community keymaps supported by each revision. Get all buildable keymaps for each revision The command now return all keymaps that's buildable for a keyboard/revision. If the base directory of a keyboard does not contain a 'rules.mk' file, nothing is returned. If the base directory contains a 'keymaps' directory, those keycaps will be returned for every revision. Fix help message. Try to figure out revision, drop -rv/--revision argument Fix output format Another major refactoring, add documentation Move all useful functions to the qmk module and use the cli subcommand as a wrapper around it. Add both inline comments and documentation. Add test for list_keymaps Fix regex for parsing rules.mk files I don't know why it couldn't put it together before... ¯\_(ツ)_/¯ Drop bs4 dependency, update docs, minor improvements Return only the unique keymaps Fix merging community and base keymaps Major rework, no regex/globbing, more walking Instead of using regexes and globbing to find the rules.mk and keymap.c files, walk the directory tree to find them. Also, do away with the concept of revision. Fix commandline parsing and flake8 findings, rebase Fixed commandline and config parsing. Thx @xplusplus. Rebased on master and fixed merge conflicts. Code cleanup, use pathlib, use pytest keyboard Clean up checks and logics that are unnecessary due to MILC updates. Use pathlib instead of os.path for readability. Use the 'pytest' keyboard for the tests. Add community layout for 'handwired/onekey/pytest' so we can test community layouts. Pathlib-ify qmk.keymap.list_keymaps() fix list_keymaps for python 3.5
2019-10-08 21:50:21 +02:00
from pathlib import Path
import json
import subprocess
from pygments.lexers.c_cpp import CLexer
from pygments.token import Token
from pygments import lex
from milc import cli
from qmk.keyboard import rules_mk
2020-05-26 14:29:48 -07:00
import qmk.path
import qmk.commands
# The `keymap.c` template to use when a keyboard doesn't have its own
DEFAULT_KEYMAP_C = """#include QMK_KEYBOARD_H
/* THIS FILE WAS GENERATED!
*
* This file was generated by qmk json2c. You may or may not want to
* edit it directly.
*/
const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
__KEYMAP_GOES_HERE__
};
"""
2020-11-24 09:38:19 -08:00
def template_json(keyboard):
"""Returns a `keymap.json` template for a keyboard.
2020-11-24 09:38:19 -08:00
If a template exists in `keyboards/<keyboard>/templates/keymap.json` that text will be used instead of an empty dictionary.
Args:
keyboard
The keyboard to return a template for.
2020-11-24 09:38:19 -08:00
"""
template_file = Path('keyboards/%s/templates/keymap.json' % keyboard)
template = {'keyboard': keyboard}
if template_file.exists():
template.update(json.loads(template_file.read_text()))
return template
2020-11-24 09:38:19 -08:00
def template_c(keyboard):
"""Returns a `keymap.c` template for a keyboard.
If a template exists in `keyboards/<keyboard>/templates/keymap.c` that text will be used instead of an empty dictionary.
Args:
keyboard
The keyboard to return a template for.
"""
2020-11-24 09:38:19 -08:00
template_file = Path('keyboards/%s/templates/keymap.c' % keyboard)
if template_file.exists():
template = template_file.read_text()
else:
2020-11-24 09:38:19 -08:00
template = DEFAULT_KEYMAP_C
return template
def _strip_any(keycode):
"""Remove ANY() from a keycode.
"""
if keycode.startswith('ANY(') and keycode.endswith(')'):
keycode = keycode[4:-1]
return keycode
2020-11-24 09:38:19 -08:00
def is_keymap_dir(keymap, c=True, json=True, additional_files=None):
"""Return True if Path object `keymap` has a keymap file inside.
2020-11-24 09:38:19 -08:00
Args:
keymap
A Path() object for the keymap directory you want to check.
c
When true include `keymap.c` keymaps.
json
When true include `keymap.json` keymaps.
additional_files
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'])`
"""
2020-11-24 09:38:19 -08:00
files = []
if c:
files.append('keymap.c')
if json:
files.append('keymap.json')
for file in files:
if (keymap / file).is_file():
2020-11-24 09:38:19 -08:00
if additional_files:
for file in additional_files:
if not (keymap / file).is_file():
return False
return True
2020-11-24 09:38:19 -08:00
def generate_json(keymap, keyboard, layout, layers):
"""Returns a `keymap.json` for the specified keyboard, layout, and layers.
Args:
keymap
A name for this keymap.
keyboard
The name of the keyboard.
layout
The LAYOUT macro this keymap uses.
layers
An array of arrays describing the keymap. Each item in the inner array should be a string that is a valid QMK keycode.
"""
new_keymap = template_json(keyboard)
new_keymap['keymap'] = keymap
new_keymap['layout'] = layout
new_keymap['layers'] = layers
return new_keymap
def generate_c(keyboard, layout, layers):
"""Returns a `keymap.c` or `keymap.json` for the specified keyboard, layout, and layers.
Args:
keyboard
The name of the keyboard
layout
The LAYOUT macro this keymap uses.
layers
An array of arrays describing the keymap. Each item in the inner array should be a string that is a valid QMK keycode.
"""
2020-11-24 09:38:19 -08:00
new_keymap = template_c(keyboard)
layer_txt = []
for layer_num, layer in enumerate(layers):
if layer_num != 0:
layer_txt[-1] = layer_txt[-1] + ','
layer = map(_strip_any, layer)
layer_keys = ', '.join(layer)
layer_txt.append('\t[%s] = %s(%s)' % (layer_num, layout, layer_keys))
keymap = '\n'.join(layer_txt)
new_keymap = new_keymap.replace('__KEYMAP_GOES_HERE__', keymap)
2020-11-24 09:38:19 -08:00
return new_keymap
2020-11-24 09:38:19 -08:00
def write_file(keymap_filename, keymap_content):
keymap_filename.parent.mkdir(parents=True, exist_ok=True)
keymap_filename.write_text(keymap_content)
2020-11-24 09:38:19 -08:00
cli.log.info('Wrote keymap to {fg_cyan}%s', keymap_filename)
return keymap_filename
2020-11-24 09:38:19 -08:00
def write_json(keyboard, keymap, layout, layers):
"""Generate the `keymap.json` and write it to disk.
Returns the filename written to.
Args:
keyboard
The name of the keyboard
keymap
The name of the keymap
layout
The LAYOUT macro this keymap uses.
layers
An array of arrays describing the keymap. Each item in the inner array should be a string that is a valid QMK keycode.
"""
2020-11-24 09:38:19 -08:00
keymap_json = generate_json(keyboard, keymap, layout, layers)
keymap_content = json.dumps(keymap_json)
keymap_file = qmk.path.keymap(keyboard) / keymap / 'keymap.json'
return write_file(keymap_file, keymap_content)
def write(keyboard, keymap, layout, layers):
"""Generate the `keymap.c` and write it to disk.
Returns the filename written to.
Args:
keyboard
The name of the keyboard
2020-11-24 09:38:19 -08:00
keymap
The name of the keymap
2020-11-24 09:38:19 -08:00
layout
The LAYOUT macro this keymap uses.
layers
An array of arrays describing the keymap. Each item in the inner array should be a string that is a valid QMK keycode.
"""
keymap_content = generate_c(keyboard, layout, layers)
keymap_file = qmk.path.keymap(keyboard) / keymap / 'keymap.c'
2020-11-24 09:38:19 -08:00
return write_file(keymap_file, keymap_content)
CLI: add support for list_keymaps List all the available keymaps for a given keyboard Add bs4 to requirements.txt UnicodeDammit is needed from bs4 for reading files. Major update to work better with revisions Find the community keymaps supported by each revision. Get all buildable keymaps for each revision The command now return all keymaps that's buildable for a keyboard/revision. If the base directory of a keyboard does not contain a 'rules.mk' file, nothing is returned. If the base directory contains a 'keymaps' directory, those keycaps will be returned for every revision. Fix help message. Try to figure out revision, drop -rv/--revision argument Fix output format Another major refactoring, add documentation Move all useful functions to the qmk module and use the cli subcommand as a wrapper around it. Add both inline comments and documentation. Add test for list_keymaps Fix regex for parsing rules.mk files I don't know why it couldn't put it together before... ¯\_(ツ)_/¯ Drop bs4 dependency, update docs, minor improvements Return only the unique keymaps Fix merging community and base keymaps Major rework, no regex/globbing, more walking Instead of using regexes and globbing to find the rules.mk and keymap.c files, walk the directory tree to find them. Also, do away with the concept of revision. Fix commandline parsing and flake8 findings, rebase Fixed commandline and config parsing. Thx @xplusplus. Rebased on master and fixed merge conflicts. Code cleanup, use pathlib, use pytest keyboard Clean up checks and logics that are unnecessary due to MILC updates. Use pathlib instead of os.path for readability. Use the 'pytest' keyboard for the tests. Add community layout for 'handwired/onekey/pytest' so we can test community layouts. Pathlib-ify qmk.keymap.list_keymaps() fix list_keymaps for python 3.5
2019-10-08 21:50:21 +02:00
def locate_keymap(keyboard, keymap):
"""Returns the path to a keymap for a specific keyboard.
"""
2020-05-26 14:29:48 -07:00
if not qmk.path.is_keyboard(keyboard):
raise KeyError('Invalid keyboard: ' + repr(keyboard))
# Check the keyboard folder first, last match wins
checked_dirs = ''
keymap_path = ''
for dir in keyboard.split('/'):
if checked_dirs:
checked_dirs = '/'.join((checked_dirs, dir))
else:
checked_dirs = dir
keymap_dir = Path('keyboards') / checked_dirs / 'keymaps'
if (keymap_dir / keymap / 'keymap.c').exists():
keymap_path = keymap_dir / keymap / 'keymap.c'
if (keymap_dir / keymap / 'keymap.json').exists():
keymap_path = keymap_dir / keymap / 'keymap.json'
if keymap_path:
return keymap_path
# Check community layouts as a fallback
rules = rules_mk(keyboard)
if "LAYOUTS" in rules:
for layout in rules["LAYOUTS"].split():
community_layout = Path('layouts/community') / layout / keymap
if community_layout.exists():
if (community_layout / 'keymap.json').exists():
return community_layout / 'keymap.json'
if (community_layout / 'keymap.c').exists():
return community_layout / 'keymap.c'
2020-11-24 09:38:19 -08:00
def list_keymaps(keyboard, c=True, json=True, additional_files=None, fullpath=False):
"""List the available keymaps for a keyboard.
CLI: add support for list_keymaps List all the available keymaps for a given keyboard Add bs4 to requirements.txt UnicodeDammit is needed from bs4 for reading files. Major update to work better with revisions Find the community keymaps supported by each revision. Get all buildable keymaps for each revision The command now return all keymaps that's buildable for a keyboard/revision. If the base directory of a keyboard does not contain a 'rules.mk' file, nothing is returned. If the base directory contains a 'keymaps' directory, those keycaps will be returned for every revision. Fix help message. Try to figure out revision, drop -rv/--revision argument Fix output format Another major refactoring, add documentation Move all useful functions to the qmk module and use the cli subcommand as a wrapper around it. Add both inline comments and documentation. Add test for list_keymaps Fix regex for parsing rules.mk files I don't know why it couldn't put it together before... ¯\_(ツ)_/¯ Drop bs4 dependency, update docs, minor improvements Return only the unique keymaps Fix merging community and base keymaps Major rework, no regex/globbing, more walking Instead of using regexes and globbing to find the rules.mk and keymap.c files, walk the directory tree to find them. Also, do away with the concept of revision. Fix commandline parsing and flake8 findings, rebase Fixed commandline and config parsing. Thx @xplusplus. Rebased on master and fixed merge conflicts. Code cleanup, use pathlib, use pytest keyboard Clean up checks and logics that are unnecessary due to MILC updates. Use pathlib instead of os.path for readability. Use the 'pytest' keyboard for the tests. Add community layout for 'handwired/onekey/pytest' so we can test community layouts. Pathlib-ify qmk.keymap.list_keymaps() fix list_keymaps for python 3.5
2019-10-08 21:50:21 +02:00
Args:
2020-11-24 09:38:19 -08:00
keyboard
The keyboards full name with vendor and revision if necessary, example: clueboard/66/rev3
c
When true include `keymap.c` keymaps.
json
When true include `keymap.json` keymaps.
additional_files
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'])`
fullpath
When set to True the full path of the keymap relative to the `qmk_firmware` root will be provided.
CLI: add support for list_keymaps List all the available keymaps for a given keyboard Add bs4 to requirements.txt UnicodeDammit is needed from bs4 for reading files. Major update to work better with revisions Find the community keymaps supported by each revision. Get all buildable keymaps for each revision The command now return all keymaps that's buildable for a keyboard/revision. If the base directory of a keyboard does not contain a 'rules.mk' file, nothing is returned. If the base directory contains a 'keymaps' directory, those keycaps will be returned for every revision. Fix help message. Try to figure out revision, drop -rv/--revision argument Fix output format Another major refactoring, add documentation Move all useful functions to the qmk module and use the cli subcommand as a wrapper around it. Add both inline comments and documentation. Add test for list_keymaps Fix regex for parsing rules.mk files I don't know why it couldn't put it together before... ¯\_(ツ)_/¯ Drop bs4 dependency, update docs, minor improvements Return only the unique keymaps Fix merging community and base keymaps Major rework, no regex/globbing, more walking Instead of using regexes and globbing to find the rules.mk and keymap.c files, walk the directory tree to find them. Also, do away with the concept of revision. Fix commandline parsing and flake8 findings, rebase Fixed commandline and config parsing. Thx @xplusplus. Rebased on master and fixed merge conflicts. Code cleanup, use pathlib, use pytest keyboard Clean up checks and logics that are unnecessary due to MILC updates. Use pathlib instead of os.path for readability. Use the 'pytest' keyboard for the tests. Add community layout for 'handwired/onekey/pytest' so we can test community layouts. Pathlib-ify qmk.keymap.list_keymaps() fix list_keymaps for python 3.5
2019-10-08 21:50:21 +02:00
Returns:
2020-11-24 09:38:19 -08:00
a sorted list of valid keymap names.
CLI: add support for list_keymaps List all the available keymaps for a given keyboard Add bs4 to requirements.txt UnicodeDammit is needed from bs4 for reading files. Major update to work better with revisions Find the community keymaps supported by each revision. Get all buildable keymaps for each revision The command now return all keymaps that's buildable for a keyboard/revision. If the base directory of a keyboard does not contain a 'rules.mk' file, nothing is returned. If the base directory contains a 'keymaps' directory, those keycaps will be returned for every revision. Fix help message. Try to figure out revision, drop -rv/--revision argument Fix output format Another major refactoring, add documentation Move all useful functions to the qmk module and use the cli subcommand as a wrapper around it. Add both inline comments and documentation. Add test for list_keymaps Fix regex for parsing rules.mk files I don't know why it couldn't put it together before... ¯\_(ツ)_/¯ Drop bs4 dependency, update docs, minor improvements Return only the unique keymaps Fix merging community and base keymaps Major rework, no regex/globbing, more walking Instead of using regexes and globbing to find the rules.mk and keymap.c files, walk the directory tree to find them. Also, do away with the concept of revision. Fix commandline parsing and flake8 findings, rebase Fixed commandline and config parsing. Thx @xplusplus. Rebased on master and fixed merge conflicts. Code cleanup, use pathlib, use pytest keyboard Clean up checks and logics that are unnecessary due to MILC updates. Use pathlib instead of os.path for readability. Use the 'pytest' keyboard for the tests. Add community layout for 'handwired/onekey/pytest' so we can test community layouts. Pathlib-ify qmk.keymap.list_keymaps() fix list_keymaps for python 3.5
2019-10-08 21:50:21 +02:00
"""
# parse all the rules.mk files for the keyboard
rules = rules_mk(keyboard)
CLI: add support for list_keymaps List all the available keymaps for a given keyboard Add bs4 to requirements.txt UnicodeDammit is needed from bs4 for reading files. Major update to work better with revisions Find the community keymaps supported by each revision. Get all buildable keymaps for each revision The command now return all keymaps that's buildable for a keyboard/revision. If the base directory of a keyboard does not contain a 'rules.mk' file, nothing is returned. If the base directory contains a 'keymaps' directory, those keycaps will be returned for every revision. Fix help message. Try to figure out revision, drop -rv/--revision argument Fix output format Another major refactoring, add documentation Move all useful functions to the qmk module and use the cli subcommand as a wrapper around it. Add both inline comments and documentation. Add test for list_keymaps Fix regex for parsing rules.mk files I don't know why it couldn't put it together before... ¯\_(ツ)_/¯ Drop bs4 dependency, update docs, minor improvements Return only the unique keymaps Fix merging community and base keymaps Major rework, no regex/globbing, more walking Instead of using regexes and globbing to find the rules.mk and keymap.c files, walk the directory tree to find them. Also, do away with the concept of revision. Fix commandline parsing and flake8 findings, rebase Fixed commandline and config parsing. Thx @xplusplus. Rebased on master and fixed merge conflicts. Code cleanup, use pathlib, use pytest keyboard Clean up checks and logics that are unnecessary due to MILC updates. Use pathlib instead of os.path for readability. Use the 'pytest' keyboard for the tests. Add community layout for 'handwired/onekey/pytest' so we can test community layouts. Pathlib-ify qmk.keymap.list_keymaps() fix list_keymaps for python 3.5
2019-10-08 21:50:21 +02:00
names = set()
if rules:
keyboards_dir = Path('keyboards')
kb_path = keyboards_dir / keyboard
2020-11-24 09:38:19 -08:00
CLI: add support for list_keymaps List all the available keymaps for a given keyboard Add bs4 to requirements.txt UnicodeDammit is needed from bs4 for reading files. Major update to work better with revisions Find the community keymaps supported by each revision. Get all buildable keymaps for each revision The command now return all keymaps that's buildable for a keyboard/revision. If the base directory of a keyboard does not contain a 'rules.mk' file, nothing is returned. If the base directory contains a 'keymaps' directory, those keycaps will be returned for every revision. Fix help message. Try to figure out revision, drop -rv/--revision argument Fix output format Another major refactoring, add documentation Move all useful functions to the qmk module and use the cli subcommand as a wrapper around it. Add both inline comments and documentation. Add test for list_keymaps Fix regex for parsing rules.mk files I don't know why it couldn't put it together before... ¯\_(ツ)_/¯ Drop bs4 dependency, update docs, minor improvements Return only the unique keymaps Fix merging community and base keymaps Major rework, no regex/globbing, more walking Instead of using regexes and globbing to find the rules.mk and keymap.c files, walk the directory tree to find them. Also, do away with the concept of revision. Fix commandline parsing and flake8 findings, rebase Fixed commandline and config parsing. Thx @xplusplus. Rebased on master and fixed merge conflicts. Code cleanup, use pathlib, use pytest keyboard Clean up checks and logics that are unnecessary due to MILC updates. Use pathlib instead of os.path for readability. Use the 'pytest' keyboard for the tests. Add community layout for 'handwired/onekey/pytest' so we can test community layouts. Pathlib-ify qmk.keymap.list_keymaps() fix list_keymaps for python 3.5
2019-10-08 21:50:21 +02:00
# walk up the directory tree until keyboards_dir
# and collect all directories' name with keymap.c file in it
while kb_path != keyboards_dir:
keymaps_dir = kb_path / "keymaps"
2020-11-24 09:38:19 -08:00
if keymaps_dir.is_dir():
for keymap in keymaps_dir.iterdir():
if is_keymap_dir(keymap, c, json, additional_files):
keymap = keymap if fullpath else keymap.name
names.add(keymap)
CLI: add support for list_keymaps List all the available keymaps for a given keyboard Add bs4 to requirements.txt UnicodeDammit is needed from bs4 for reading files. Major update to work better with revisions Find the community keymaps supported by each revision. Get all buildable keymaps for each revision The command now return all keymaps that's buildable for a keyboard/revision. If the base directory of a keyboard does not contain a 'rules.mk' file, nothing is returned. If the base directory contains a 'keymaps' directory, those keycaps will be returned for every revision. Fix help message. Try to figure out revision, drop -rv/--revision argument Fix output format Another major refactoring, add documentation Move all useful functions to the qmk module and use the cli subcommand as a wrapper around it. Add both inline comments and documentation. Add test for list_keymaps Fix regex for parsing rules.mk files I don't know why it couldn't put it together before... ¯\_(ツ)_/¯ Drop bs4 dependency, update docs, minor improvements Return only the unique keymaps Fix merging community and base keymaps Major rework, no regex/globbing, more walking Instead of using regexes and globbing to find the rules.mk and keymap.c files, walk the directory tree to find them. Also, do away with the concept of revision. Fix commandline parsing and flake8 findings, rebase Fixed commandline and config parsing. Thx @xplusplus. Rebased on master and fixed merge conflicts. Code cleanup, use pathlib, use pytest keyboard Clean up checks and logics that are unnecessary due to MILC updates. Use pathlib instead of os.path for readability. Use the 'pytest' keyboard for the tests. Add community layout for 'handwired/onekey/pytest' so we can test community layouts. Pathlib-ify qmk.keymap.list_keymaps() fix list_keymaps for python 3.5
2019-10-08 21:50:21 +02:00
kb_path = kb_path.parent
# if community layouts are supported, get them
if "LAYOUTS" in rules:
for layout in rules["LAYOUTS"].split():
cl_path = Path('layouts/community') / layout
2020-11-24 09:38:19 -08:00
if cl_path.is_dir():
for keymap in cl_path.iterdir():
if is_keymap_dir(keymap, c, json, additional_files):
keymap = keymap if fullpath else keymap.name
names.add(keymap)
CLI: add support for list_keymaps List all the available keymaps for a given keyboard Add bs4 to requirements.txt UnicodeDammit is needed from bs4 for reading files. Major update to work better with revisions Find the community keymaps supported by each revision. Get all buildable keymaps for each revision The command now return all keymaps that's buildable for a keyboard/revision. If the base directory of a keyboard does not contain a 'rules.mk' file, nothing is returned. If the base directory contains a 'keymaps' directory, those keycaps will be returned for every revision. Fix help message. Try to figure out revision, drop -rv/--revision argument Fix output format Another major refactoring, add documentation Move all useful functions to the qmk module and use the cli subcommand as a wrapper around it. Add both inline comments and documentation. Add test for list_keymaps Fix regex for parsing rules.mk files I don't know why it couldn't put it together before... ¯\_(ツ)_/¯ Drop bs4 dependency, update docs, minor improvements Return only the unique keymaps Fix merging community and base keymaps Major rework, no regex/globbing, more walking Instead of using regexes and globbing to find the rules.mk and keymap.c files, walk the directory tree to find them. Also, do away with the concept of revision. Fix commandline parsing and flake8 findings, rebase Fixed commandline and config parsing. Thx @xplusplus. Rebased on master and fixed merge conflicts. Code cleanup, use pathlib, use pytest keyboard Clean up checks and logics that are unnecessary due to MILC updates. Use pathlib instead of os.path for readability. Use the 'pytest' keyboard for the tests. Add community layout for 'handwired/onekey/pytest' so we can test community layouts. Pathlib-ify qmk.keymap.list_keymaps() fix list_keymaps for python 3.5
2019-10-08 21:50:21 +02:00
return sorted(names)
def _c_preprocess(path):
""" Run a file through the C pre-processor
Args:
path: path of the keymap.c file
Returns:
the stdout of the pre-processor
"""
pre_processed_keymap = qmk.commands.run(['cpp', path], stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
return pre_processed_keymap.stdout
def _get_layers(keymap): # noqa C901 : until someone has a good idea how to simplify/split up this code
""" Find the layers in a keymap.c file.
Args:
keymap: the content of the keymap.c file
Returns:
a dictionary containing the parsed keymap
"""
layers = list()
opening_braces = '({['
closing_braces = ')}]'
keymap_certainty = brace_depth = 0
is_keymap = is_layer = is_adv_kc = False
layer = dict(name=False, layout=False, keycodes=list())
for line in lex(keymap, CLexer()):
if line[0] is Token.Name:
if is_keymap:
# If we are inside the keymap array
# we know the keymap's name and the layout macro will come,
# followed by the keycodes
if not layer['name']:
if line[1].startswith('LAYOUT') or line[1].startswith('KEYMAP'):
# This can happen if the keymap array only has one layer,
# for macropads and such
layer['name'] = '0'
layer['layout'] = line[1]
else:
layer['name'] = line[1]
elif not layer['layout']:
layer['layout'] = line[1]
elif is_layer:
# If we are inside a layout macro,
# collect all keycodes
if line[1] == '_______':
kc = 'KC_TRNS'
elif line[1] == 'XXXXXXX':
kc = 'KC_NO'
else:
kc = line[1]
if is_adv_kc:
# If we are inside an advanced keycode
# collect everything and hope the user
# knew what he/she was doing
layer['keycodes'][-1] += kc
else:
layer['keycodes'].append(kc)
# The keymaps array's signature:
# const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS]
#
# Only if we've found all 6 keywords in this specific order
# can we know for sure that we are inside the keymaps array
elif line[1] == 'PROGMEM' and keymap_certainty == 2:
keymap_certainty = 3
elif line[1] == 'keymaps' and keymap_certainty == 3:
keymap_certainty = 4
elif line[1] == 'MATRIX_ROWS' and keymap_certainty == 4:
keymap_certainty = 5
elif line[1] == 'MATRIX_COLS' and keymap_certainty == 5:
keymap_certainty = 6
elif line[0] is Token.Keyword:
if line[1] == 'const' and keymap_certainty == 0:
keymap_certainty = 1
elif line[0] is Token.Keyword.Type:
if line[1] == 'uint16_t' and keymap_certainty == 1:
keymap_certainty = 2
elif line[0] is Token.Punctuation:
if line[1] in opening_braces:
brace_depth += 1
if is_keymap:
if is_layer:
# We found the beginning of a non-basic keycode
is_adv_kc = True
layer['keycodes'][-1] += line[1]
elif line[1] == '(' and brace_depth == 2:
# We found the beginning of a layer
is_layer = True
elif line[1] == '{' and keymap_certainty == 6:
# We found the beginning of the keymaps array
is_keymap = True
elif line[1] in closing_braces:
brace_depth -= 1
if is_keymap:
if is_adv_kc:
layer['keycodes'][-1] += line[1]
if brace_depth == 2:
# We found the end of a non-basic keycode
is_adv_kc = False
elif line[1] == ')' and brace_depth == 1:
# We found the end of a layer
is_layer = False
layers.append(layer)
layer = dict(name=False, layout=False, keycodes=list())
elif line[1] == '}' and brace_depth == 0:
# We found the end of the keymaps array
is_keymap = False
keymap_certainty = 0
elif is_adv_kc:
# Advanced keycodes can contain other punctuation
# e.g.: MT(MOD_LCTL | MOD_LSFT, KC_ESC)
layer['keycodes'][-1] += line[1]
elif line[0] is Token.Literal.Number.Integer and is_keymap and not is_adv_kc:
# If the pre-processor finds the 'meaning' of the layer names,
# they will be numbers
if not layer['name']:
layer['name'] = line[1]
else:
# We only care about
# operators and such if we
# are inside an advanced keycode
# e.g.: MT(MOD_LCTL | MOD_LSFT, KC_ESC)
if is_adv_kc:
layer['keycodes'][-1] += line[1]
return layers
def parse_keymap_c(keymap_file, use_cpp=True):
""" Parse a keymap.c file.
Currently only cares about the keymaps array.
Args:
keymap_file: path of the keymap.c file
use_cpp: if True, pre-process the file with the C pre-processor
Returns:
a dictionary containing the parsed keymap
"""
if use_cpp:
keymap_file = _c_preprocess(keymap_file)
else:
keymap_file = keymap_file.read_text()
keymap = dict()
keymap['layers'] = _get_layers(keymap_file)
return keymap
def c2json(keyboard, keymap, keymap_file, use_cpp=True):
""" Convert keymap.c to keymap.json
Args:
keyboard: The name of the keyboard
keymap: The name of the keymap
layout: The LAYOUT macro this keymap uses.
keymap_file: path of the keymap.c file
use_cpp: if True, pre-process the file with the C pre-processor
Returns:
a dictionary in keymap.json format
"""
keymap_json = parse_keymap_c(keymap_file, use_cpp)
dirty_layers = keymap_json.pop('layers', None)
keymap_json['layers'] = list()
for layer in dirty_layers:
layer.pop('name')
layout = layer.pop('layout')
if not keymap_json.get('layout', False):
keymap_json['layout'] = layout
keymap_json['layers'].append(layer.pop('keycodes'))
keymap_json['keyboard'] = keyboard
keymap_json['keymap'] = keymap
return keymap_json