aboutsummaryrefslogtreecommitdiff
path: root/lib/python/qmk/keymap.py
diff options
context:
space:
mode:
Diffstat (limited to 'lib/python/qmk/keymap.py')
-rw-r--r--lib/python/qmk/keymap.py78
1 files changed, 7 insertions, 71 deletions
diff --git a/lib/python/qmk/keymap.py b/lib/python/qmk/keymap.py
index 91075f1bdc..97c358788a 100644
--- a/lib/python/qmk/keymap.py
+++ b/lib/python/qmk/keymap.py
@@ -19,6 +19,9 @@ from qmk.info import info_json
# The `keymap.c` template to use when a keyboard doesn't have its own
DEFAULT_KEYMAP_C = """#include QMK_KEYBOARD_H
+#if __has_include("keymap.h")
+# include "keymap.h"
+#endif
__INCLUDES__
/* THIS FILE WAS GENERATED!
@@ -26,8 +29,6 @@ __INCLUDES__
* This file was generated by qmk json2c. You may or may not want to
* edit it directly.
*/
-__KEYCODE_OUTPUT_GOES_HERE__
-
const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
__KEYMAP_GOES_HERE__
};
@@ -39,7 +40,6 @@ __ENCODER_MAP_GOES_HERE__
#endif // defined(ENCODER_ENABLE) && defined(ENCODER_MAP_ENABLE)
__MACRO_OUTPUT_GOES_HERE__
-
"""
@@ -50,7 +50,7 @@ def _generate_keymap_table(keymap_json):
lines[-1] = lines[-1] + ','
layer = map(_strip_any, layer)
layer_keys = ', '.join(layer)
- lines.append('\t[%s] = %s(%s)' % (layer_num, keymap_json['layout'], layer_keys))
+ lines.append(' [%s] = %s(%s)' % (layer_num, keymap_json['layout'], layer_keys))
return lines
@@ -60,7 +60,7 @@ def _generate_encodermap_table(keymap_json):
if layer_num != 0:
lines[-1] = lines[-1] + ','
encoder_keycode_txt = ', '.join([f'ENCODER_CCW_CW({_strip_any(e["ccw"])}, {_strip_any(e["cw"])})' for e in layer])
- lines.append('\t[%s] = {%s}' % (layer_num, encoder_keycode_txt))
+ lines.append(' [%s] = {%s}' % (layer_num, encoder_keycode_txt))
return lines
@@ -125,64 +125,6 @@ def _generate_macros_function(keymap_json):
return macro_txt
-def _generate_keycodes_function(keymap_json):
- """Generates keymap level keycodes.
- """
- lines = []
- lines.append('enum keymap_keycodes {')
-
- for index, item in enumerate(keymap_json.get('keycodes', [])):
- key = item["key"]
- if index == 0:
- lines.append(f' {key} = QK_USER_0,')
- else:
- lines.append(f' {key},')
-
- lines.append('};')
-
- for item in keymap_json.get('keycodes', []):
- key = item["key"]
- for alias in item.get("aliases", []):
- lines.append(f'#define {alias} {key}')
-
- return lines
-
-
-def template_json(keyboard):
- """Returns a `keymap.json` template for a keyboard.
-
- 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.
- """
- template_file = Path('keyboards/%s/templates/keymap.json' % keyboard)
- template = {'keyboard': keyboard}
- if template_file.exists():
- template.update(json.load(template_file.open(encoding='utf-8')))
-
- return template
-
-
-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.
- """
- template_file = Path('keyboards/%s/templates/keymap.c' % keyboard)
- if template_file.exists():
- template = template_file.read_text(encoding='utf-8')
- else:
- template = DEFAULT_KEYMAP_C
-
- return template
-
-
def _strip_any(keycode):
"""Remove ANY() from a keycode.
"""
@@ -301,7 +243,7 @@ def generate_json(keymap, keyboard, layout, layers, macros=None):
macros
A sequence of strings containing macros to implement for this keyboard.
"""
- new_keymap = template_json(keyboard)
+ new_keymap = {'keyboard': keyboard}
new_keymap['keymap'] = keymap
new_keymap['layout'] = layout
new_keymap['layers'] = layers
@@ -328,7 +270,7 @@ def generate_c(keymap_json):
macros
A sequence of strings containing macros to implement for this keyboard.
"""
- new_keymap = template_c(keymap_json['keyboard'])
+ new_keymap = DEFAULT_KEYMAP_C
layer_txt = _generate_keymap_table(keymap_json)
keymap = '\n'.join(layer_txt)
new_keymap = new_keymap.replace('__KEYMAP_GOES_HERE__', keymap)
@@ -350,12 +292,6 @@ def generate_c(keymap_json):
hostlang = f'#include "keymap_{keymap_json["host_language"]}.h"\n#include "sendstring_{keymap_json["host_language"]}.h"\n'
new_keymap = new_keymap.replace('__INCLUDES__', hostlang)
- keycodes = ''
- if 'keycodes' in keymap_json and keymap_json['keycodes'] is not None:
- keycodes_txt = _generate_keycodes_function(keymap_json)
- keycodes = '\n'.join(keycodes_txt)
- new_keymap = new_keymap.replace('__KEYCODE_OUTPUT_GOES_HERE__', keycodes)
-
return new_keymap