From e279c78ba3054ebc2e294dd91ea98341cc509d1e Mon Sep 17 00:00:00 2001
From: Joel Challis <git@zvecr.com>
Date: Mon, 20 Nov 2023 15:41:48 +0000
Subject: Enable linking of encoders to switch within layout macros (#22264)

---
 lib/python/qmk/info.py     | 28 +++++++++++++++++++++++
 lib/python/qmk/keyboard.py | 55 +++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 82 insertions(+), 1 deletion(-)

(limited to 'lib/python')

diff --git a/lib/python/qmk/info.py b/lib/python/qmk/info.py
index fe829a724a..3efd34555c 100644
--- a/lib/python/qmk/info.py
+++ b/lib/python/qmk/info.py
@@ -55,6 +55,29 @@ def _get_key_left_position(key):
     return key['x'] - 0.25 if key.get('h', 1) == 2 and key.get('w', 1) == 1.25 else key['x']
 
 
+def _find_invalid_encoder_index(info_data):
+    """Perform additional validation of encoders
+    """
+    enc_count = len(info_data.get('encoder', {}).get('rotary', []))
+    enc_count += len(info_data.get('split', {}).get('encoder', {}).get('right', {}).get('rotary', []))
+
+    ret = []
+    layouts = info_data.get('layouts', {})
+    for layout_name, layout_data in layouts.items():
+        found = set()
+        for key in layout_data['layout']:
+            if 'encoder' in key:
+                if enc_count == 0:
+                    ret.append((layout_name, key['encoder'], 'non-configured'))
+                elif key['encoder'] >= enc_count:
+                    ret.append((layout_name, key['encoder'], 'out of bounds'))
+                elif key['encoder'] in found:
+                    ret.append((layout_name, key['encoder'], 'duplicate'))
+                found.add(key['encoder'])
+
+    return ret
+
+
 def _additional_validation(keyboard, info_data):
     """Non schema checks
     """
@@ -105,6 +128,11 @@ def _additional_validation(keyboard, info_data):
             if not decl.get("aliases", []):
                 _log_error(info_data, f'Keycode {decl["key"]} has no short form alias')
 
+    # encoder IDs in layouts must be in range and not duplicated
+    found = _find_invalid_encoder_index(info_data)
+    for layout_name, encoder_index, reason in found:
+        _log_error(info_data, f'Layout "{layout_name}" contains {reason} encoder index {encoder_index}.')
+
 
 def _validate(keyboard, info_data):
     """Perform various validation on the provided info.json data
diff --git a/lib/python/qmk/keyboard.py b/lib/python/qmk/keyboard.py
index 1aa63687d4..4e525731f7 100644
--- a/lib/python/qmk/keyboard.py
+++ b/lib/python/qmk/keyboard.py
@@ -30,6 +30,28 @@ BOX_DRAWING_CHARACTERS = {
         "h": "_",
     },
 }
+ENC_DRAWING_CHARACTERS = {
+    "unicode": {
+        "tl": "╭",
+        "tr": "╮",
+        "bl": "╰",
+        "br": "╯",
+        "vl": "▲",
+        "vr": "▼",
+        "v": "│",
+        "h": "─",
+    },
+    "ascii": {
+        "tl": " ",
+        "tr": " ",
+        "bl": "\\",
+        "br": "/",
+        "v": "|",
+        "vl": "/",
+        "vr": "\\",
+        "h": "_",
+    },
+}
 
 
 class AllKeyboards:
@@ -213,7 +235,9 @@ def render_layout(layout_data, render_ascii, key_labels=None):
         else:
             label = key.get('label', '')
 
-        if x >= 0.25 and w == 1.25 and h == 2:
+        if 'encoder' in key:
+            render_encoder(textpad, x, y, w, h, label, style)
+        elif x >= 0.25 and w == 1.25 and h == 2:
             render_key_isoenter(textpad, x, y, w, h, label, style)
         elif w == 1.5 and h == 2:
             render_key_baenter(textpad, x, y, w, h, label, style)
@@ -331,3 +355,32 @@ def render_key_baenter(textpad, x, y, w, h, label, style):
     textpad[y + 3][x - 3:x + w] = crn_line
     textpad[y + 4][x - 3:x + w] = lab_line
     textpad[y + 5][x - 3:x + w] = bot_line
+
+
+def render_encoder(textpad, x, y, w, h, label, style):
+    box_chars = ENC_DRAWING_CHARACTERS[style]
+    x = ceil(x * 4)
+    y = ceil(y * 3)
+    w = ceil(w * 4)
+    h = ceil(h * 3)
+
+    label_len = w - 2
+    label_leftover = label_len - len(label)
+
+    if len(label) > label_len:
+        label = label[:label_len]
+
+    label_blank = ' ' * label_len
+    label_border = box_chars['h'] * label_len
+    label_middle = label + ' ' * label_leftover
+
+    top_line = array('u', box_chars['tl'] + label_border + box_chars['tr'])
+    lab_line = array('u', box_chars['vl'] + label_middle + box_chars['vr'])
+    mid_line = array('u', box_chars['v'] + label_blank + box_chars['v'])
+    bot_line = array('u', box_chars['bl'] + label_border + box_chars['br'])
+
+    textpad[y][x:x + w] = top_line
+    textpad[y + 1][x:x + w] = lab_line
+    for i in range(h - 3):
+        textpad[y + i + 2][x:x + w] = mid_line
+    textpad[y + h - 1][x:x + w] = bot_line
-- 
cgit v1.2.3