diff --git a/lib/python/qmk/cli/xap/xap.py b/lib/python/qmk/cli/xap/xap.py index 69ef8e6e7d2..a90625e748e 100644 --- a/lib/python/qmk/cli/xap/xap.py +++ b/lib/python/qmk/cli/xap/xap.py @@ -4,17 +4,13 @@ import cmd import json import random import gzip -from pathlib import Path from platform import platform from milc import cli -from qmk.json_schema import json_load +from qmk.xap.common import get_xap_keycodes -# TODO: get from xap "uses" for the current device -keycode_version = '0.0.1' -spec = json_load(Path(f'data/constants/keycodes_{keycode_version}.json')) -KEYCODE_MAP = {int(k, 16): v.get('aliases', [v.get('key')])[0] for k, v in spec['keycodes'].items()} +KEYCODE_MAP = get_xap_keycodes('0.2.0') def _is_xap_usage(x): @@ -210,6 +206,8 @@ class XAPShell(cmd.Cmd): def __init__(self, device): cmd.Cmd.__init__(self) self.device = device + # cache keycodes for this device + self.keycodes = get_xap_keycodes(_query_device(device)['xap']) def do_about(self, arg): """Prints out the current version of QMK with a build date @@ -238,7 +236,7 @@ class XAPShell(cmd.Cmd): keycode = _xap_transaction(self.device, 0x04, 0x02, data) keycode = int.from_bytes(keycode, "little") - print(f'keycode:{KEYCODE_MAP.get(keycode, "unknown")}[{keycode}]') + print(f'keycode:{self.keycodes.get(keycode, "unknown")}[{keycode}]') def do_keymap(self, arg): """Prints out the keycode values of a certain layer @@ -258,7 +256,7 @@ class XAPShell(cmd.Cmd): q = data + r.to_bytes(1, byteorder='little') + c.to_bytes(1, byteorder='little') keycode = _xap_transaction(self.device, 0x04, 0x02, q) keycode = int.from_bytes(keycode, "little") - print(f'| {KEYCODE_MAP.get(keycode, "unknown").ljust(7)} ', end='', flush=True) + print(f'| {self.keycodes.get(keycode, "unknown").ljust(7)} ', end='', flush=True) print('|') def do_exit(self, line): diff --git a/lib/python/qmk/xap/common.py b/lib/python/qmk/xap/common.py index d1e0d5dbb03..1a39682e099 100755 --- a/lib/python/qmk/xap/common.py +++ b/lib/python/qmk/xap/common.py @@ -2,9 +2,12 @@ """ import os import hjson -from jinja2 import Environment, FileSystemLoader, select_autoescape -from qmk.constants import QMK_FIRMWARE +from pathlib import Path from typing import OrderedDict +from jinja2 import Environment, FileSystemLoader, select_autoescape + +from qmk.constants import QMK_FIRMWARE +from qmk.json_schema import json_load def _get_jinja2_env(data_templates_xap_subdir: str): @@ -78,6 +81,32 @@ def latest_xap_defs(): return _merge_ordered_dicts(definitions) +def get_xap_defs(version): + """Gets the required version of the XAP definitions. + """ + files = get_xap_definition_files() + + # Slice off anything newer than specified version + index = [idx for idx, s in enumerate(files) if version in str(s)][0] + files = files[:(index + 1)] + + definitions = [hjson.load(file.open(encoding='utf-8')) for file in files] + return _merge_ordered_dicts(definitions) + + +def get_xap_keycodes(xap_version): + """Gets keycode data for the required version of the XAP definitions. + """ + defs = get_xap_defs(xap_version) + + # Load DD keycodes for the dependency + keycode_version = defs['uses']['keycodes'] + spec = json_load(Path(f'data/constants/keycodes_{keycode_version}.json')) + + # Transform into something more usable - { raw_value : first alias || keycode } + return {int(k, 16): v.get('aliases', [v.get('key')])[0] for k, v in spec['keycodes'].items()} + + def route_conditions(route_stack): """Handles building the C preprocessor conditional based on the current route. """