From 140ca433b96ea7de639b4599cc5340f74370fdf4 Mon Sep 17 00:00:00 2001 From: Cem Aksoylar Date: Tue, 19 Nov 2024 11:01:31 -0800 Subject: [PATCH] fix: Add preprocessor defines from ZMK so parsing works --- keymap_drawer/parse/zmk.py | 19 ++++++++++++++++--- pyproject.toml | 1 + resources/zmk_defines.h | 21 +++++++++++++++++++++ 3 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 resources/zmk_defines.h diff --git a/keymap_drawer/parse/zmk.py b/keymap_drawer/parse/zmk.py index e2917ba..04b7051 100644 --- a/keymap_drawer/parse/zmk.py +++ b/keymap_drawer/parse/zmk.py @@ -1,6 +1,7 @@ """Module containing class to parse devicetree format ZMK keymaps.""" import re +from functools import cache from itertools import chain from pathlib import Path from typing import Sequence @@ -13,6 +14,7 @@ from keymap_drawer.parse.parse import KeymapParser, ParseError ZMK_LAYOUTS_PATH = Path(__file__).parent.parent.parent / "resources" / "zmk_keyboard_layouts.yaml" +ZMK_DEFINES_PATH = Path(__file__).parent.parent.parent / "resources" / "zmk_defines.h" class ZmkKeymapParser(KeymapParser): @@ -232,8 +234,7 @@ def _get_physical_layout(self, file_name: str | None, dts: DeviceTree) -> dict: return {} keyboard_name = Path(file_name).stem - with open(ZMK_LAYOUTS_PATH, "rb") as f: - keyboard_to_layout_map = yaml.safe_load(f) + keyboard_to_layout_map = _get_zmk_layouts() if (keyboard_layouts := keyboard_to_layout_map.get(keyboard_name)) is None: return {} @@ -256,7 +257,7 @@ def _parse(self, in_str: str, file_name: str | None = None) -> tuple[dict, Keyma in_str, file_name, self.cfg.preprocess, - preamble=self.cfg.zmk_preamble, + preamble=self.cfg.zmk_preamble + "\n" + _get_zmk_defines(), additional_includes=self.cfg.zmk_additional_includes, ) @@ -273,3 +274,15 @@ def _parse(self, in_str: str, file_name: str | None = None) -> tuple[dict, Keyma keymap_data = KeymapData(layers=layers, combos=combos, layout=None, config=None) return self._get_physical_layout(file_name, dts), keymap_data + + +@cache +def _get_zmk_defines() -> str: + with open(ZMK_DEFINES_PATH, "r", encoding="utf-8") as f: + return f.read() + + +@cache +def _get_zmk_layouts() -> dict: + with open(ZMK_LAYOUTS_PATH, "rb") as f: + return yaml.safe_load(f) diff --git a/pyproject.toml b/pyproject.toml index 8fd1862..b4af7c5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -7,6 +7,7 @@ readme = ["README.md", "KEYMAP_SPEC.md", "CONFIGURATION.md"] packages = [{include = "keymap_drawer"}] include = [ "keymap_drawer/py.typed", + "resources/zmk_defines.h", "resources/zmk_keyboard_layouts.yaml", "resources/qmk_keyboard_mappings.yaml", "resources/qmk_layouts/*.json" diff --git a/resources/zmk_defines.h b/resources/zmk_defines.h new file mode 100644 index 0000000..cd9b677 --- /dev/null +++ b/resources/zmk_defines.h @@ -0,0 +1,21 @@ +#define MACRO_PLACEHOLDER 0 +#define ZMK_MACRO(name,...) \ +name: name { \ + compatible = "zmk,behavior-macro"; \ + #binding-cells = <0>; \ + __VA_ARGS__ \ +}; + +#define ZMK_MACRO1(name,...) \ +name: name { \ + compatible = "zmk,behavior-macro-one-param"; \ + #binding-cells = <1>; \ + __VA_ARGS__ \ +}; + +#define ZMK_MACRO2(name,...) \ +name: name { \ + compatible = "zmk,behavior-macro-two-param"; \ + #binding-cells = <2>; \ + __VA_ARGS__ \ +};