2019-07-15 19:14:27 +00:00
|
|
|
"""Functions that help you work with QMK keymaps.
|
|
|
|
"""
|
2020-01-25 21:30:57 +00:00
|
|
|
from pathlib import Path
|
2019-07-15 19:14:27 +00:00
|
|
|
|
|
|
|
import qmk.path
|
2019-10-13 17:07:22 +00:00
|
|
|
import qmk.makefile
|
2019-07-15 19:14:27 +00:00
|
|
|
|
|
|
|
# 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!
|
|
|
|
*
|
2020-04-06 02:51:38 +00:00
|
|
|
* This file was generated by qmk json2c. You may or may not want to
|
2019-07-15 19:14:27 +00:00
|
|
|
* edit it directly.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
|
|
|
|
__KEYMAP_GOES_HERE__
|
|
|
|
};
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
def template(keyboard):
|
|
|
|
"""Returns the `keymap.c` template for a keyboard.
|
|
|
|
|
|
|
|
If a template exists in `keyboards/<keyboard>/templates/keymap.c` that
|
|
|
|
text will be used instead of `DEFAULT_KEYMAP_C`.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
keyboard
|
|
|
|
The keyboard to return a template for.
|
|
|
|
"""
|
2020-02-17 19:42:11 +00:00
|
|
|
template_file = Path('keyboards/%s/templates/keymap.c' % keyboard)
|
2019-07-15 19:14:27 +00:00
|
|
|
|
2020-02-17 19:42:11 +00:00
|
|
|
if template_file.exists():
|
|
|
|
return template_file.read_text()
|
2019-07-15 19:14:27 +00:00
|
|
|
|
|
|
|
return DEFAULT_KEYMAP_C
|
|
|
|
|
|
|
|
|
2020-04-06 02:51:38 +00:00
|
|
|
def _strip_any(keycode):
|
|
|
|
"""Remove ANY() from a keycode.
|
|
|
|
"""
|
|
|
|
if keycode.startswith('ANY(') and keycode.endswith(')'):
|
|
|
|
keycode = keycode[4:-1]
|
|
|
|
|
|
|
|
return keycode
|
|
|
|
|
|
|
|
|
2019-07-15 19:14:27 +00:00
|
|
|
def generate(keyboard, layout, layers):
|
|
|
|
"""Returns a keymap.c 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.
|
|
|
|
"""
|
|
|
|
layer_txt = []
|
2020-04-06 02:51:38 +00:00
|
|
|
|
2019-07-15 19:14:27 +00:00
|
|
|
for layer_num, layer in enumerate(layers):
|
|
|
|
if layer_num != 0:
|
|
|
|
layer_txt[-1] = layer_txt[-1] + ','
|
2020-04-06 02:51:38 +00:00
|
|
|
|
|
|
|
layer = map(_strip_any, layer)
|
2019-07-15 19:14:27 +00:00
|
|
|
layer_keys = ', '.join(layer)
|
|
|
|
layer_txt.append('\t[%s] = %s(%s)' % (layer_num, layout, layer_keys))
|
|
|
|
|
|
|
|
keymap = '\n'.join(layer_txt)
|
2019-07-15 22:12:35 +00:00
|
|
|
keymap_c = template(keyboard)
|
2019-07-15 19:14:27 +00:00
|
|
|
|
|
|
|
return keymap_c.replace('__KEYMAP_GOES_HERE__', keymap)
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
keymap_c = generate(keyboard, layout, layers)
|
2020-02-17 19:42:11 +00:00
|
|
|
keymap_file = qmk.path.keymap(keyboard) / keymap / 'keymap.c'
|
2019-07-15 19:14:27 +00:00
|
|
|
|
2020-02-17 19:42:11 +00:00
|
|
|
keymap_file.parent.mkdir(parents=True, exist_ok=True)
|
|
|
|
keymap_file.write_text(keymap_c)
|
2019-07-15 19:14:27 +00:00
|
|
|
|
|
|
|
return keymap_file
|
2019-10-13 17:07:22 +00:00
|
|
|
|
2020-01-07 20:54:21 +00:00
|
|
|
|
2019-10-13 17:07:22 +00:00
|
|
|
def list_keymaps(keyboard_name):
|
|
|
|
""" List the available keymaps for a keyboard.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
keyboard_name: the keyboards full name with vendor and revision if necessary, example: clueboard/66/rev3
|
|
|
|
|
|
|
|
Returns:
|
2019-10-24 20:22:44 +00:00
|
|
|
a set with the names of the available keymaps
|
2019-10-13 17:07:22 +00:00
|
|
|
"""
|
|
|
|
# parse all the rules.mk files for the keyboard
|
2019-10-28 08:23:06 +00:00
|
|
|
rules_mk = qmk.makefile.get_rules_mk(keyboard_name)
|
2019-10-24 20:22:44 +00:00
|
|
|
names = set()
|
2019-10-13 17:07:22 +00:00
|
|
|
|
|
|
|
if rules_mk:
|
2019-10-28 08:23:06 +00:00
|
|
|
# qmk_firmware/keyboards
|
2020-01-25 21:30:57 +00:00
|
|
|
keyboards_dir = Path.cwd() / "keyboards"
|
2019-10-28 08:23:06 +00:00
|
|
|
# path to the keyboard's directory
|
2020-01-25 21:30:57 +00:00
|
|
|
kb_path = keyboards_dir / keyboard_name
|
2019-10-28 08:23:06 +00: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:
|
2020-01-25 21:30:57 +00:00
|
|
|
keymaps_dir = kb_path / "keymaps"
|
|
|
|
if keymaps_dir.exists():
|
2020-04-18 20:00:56 +00:00
|
|
|
names = names.union([keymap for keymap in keymaps_dir.iterdir() if (keymaps_dir / keymap / "keymap.c").is_file()])
|
2020-01-25 21:30:57 +00:00
|
|
|
kb_path = kb_path.parent
|
2019-10-13 17:07:22 +00:00
|
|
|
|
|
|
|
# if community layouts are supported, get them
|
|
|
|
if "LAYOUTS" in rules_mk:
|
2019-10-28 08:23:06 +00:00
|
|
|
for layout in rules_mk["LAYOUTS"].split():
|
2020-01-25 21:30:57 +00:00
|
|
|
cl_path = Path.cwd() / "layouts" / "community" / layout
|
|
|
|
if cl_path.exists():
|
2020-04-18 20:00:56 +00:00
|
|
|
names = names.union([keymap for keymap in cl_path.iterdir() if (cl_path / keymap / "keymap.c").is_file()])
|
2019-10-13 17:07:22 +00:00
|
|
|
|
2019-10-24 20:22:44 +00:00
|
|
|
return sorted(names)
|