Skip to content

Commit

Permalink
fix: Add preprocessor defines from ZMK so parsing works
Browse files Browse the repository at this point in the history
  • Loading branch information
caksoylar committed Nov 19, 2024
1 parent 4c3d554 commit afbdb42
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
19 changes: 16 additions & 3 deletions keymap_drawer/parse/zmk.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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 {}
Expand All @@ -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,
)

Expand All @@ -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)
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
21 changes: 21 additions & 0 deletions resources/zmk_defines.h
Original file line number Diff line number Diff line change
@@ -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__ \
};

0 comments on commit afbdb42

Please sign in to comment.