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

---
 data/schemas/keyboard.jsonschema |  1 +
 docs/reference_info_json.md      |  2 ++
 keyboards/drop/sense75/info.json |  2 +-
 lib/python/qmk/info.py           | 28 ++++++++++++++++
 lib/python/qmk/keyboard.py       | 55 +++++++++++++++++++++++++++++++-
 5 files changed, 86 insertions(+), 2 deletions(-)

diff --git a/data/schemas/keyboard.jsonschema b/data/schemas/keyboard.jsonschema
index 9fc455530c68..299695808486 100644
--- a/data/schemas/keyboard.jsonschema
+++ b/data/schemas/keyboard.jsonschema
@@ -348,6 +348,7 @@
                             "additionalProperties": false,
                             "required": ["x", "y"],
                             "properties": {
+                                "encoder": {"$ref": "qmk.definitions.v1#/unsigned_int"},
                                 "label": {
                                     "type": "string",
                                     "pattern": "^[^\\n]*$"
diff --git a/docs/reference_info_json.md b/docs/reference_info_json.md
index 4a70a4bb6f94..e102b9bfb94e 100644
--- a/docs/reference_info_json.md
+++ b/docs/reference_info_json.md
@@ -324,6 +324,8 @@ The ISO enter key is represented by a 1.25u×2uh key. Renderers which utilize in
                 * `w`
                     * The width of the key, in key units.
                     * Default: `1` (1u)
+                * `encoder`
+                    * The index of an encoder this key should be linked to
                 * Example: `{"label": "Shift", "matrix": [4, 0], "x": 0, "y": 4.25, "w": 2.25}`
 
 ## Leader Key :id=leader-key
diff --git a/keyboards/drop/sense75/info.json b/keyboards/drop/sense75/info.json
index dbefc108f236..052b494375ef 100644
--- a/keyboards/drop/sense75/info.json
+++ b/keyboards/drop/sense75/info.json
@@ -44,7 +44,7 @@
                 {"matrix": [0, 11], "label": "F11", "x": 11.75, "y": 0},
                 {"matrix": [0, 12], "label": "F12", "x": 12.75, "y": 0},
                 {"matrix": [0, 13], "label": "PrtSc", "x": 14, "y": 0},
-                {"matrix": [0, 14], "label": "Mute", "x": 15.25, "y": 0},
+                {"matrix": [0, 14], "encoder":0, "label": "Mute", "x": 15.25, "y": 0},
                 {"matrix": [1, 0], "label": "~", "x": 0, "y": 1.25},
                 {"matrix": [1, 1], "label": "!", "x": 1, "y": 1.25},
                 {"matrix": [1, 2], "label": "@", "x": 2, "y": 1.25},
diff --git a/lib/python/qmk/info.py b/lib/python/qmk/info.py
index fe829a724aa3..3efd34555c62 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 1aa63687d4ac..4e525731f74a 100644
--- a/lib/python/qmk/keyboard.py
+++ b/lib/python/qmk/keyboard.py
@@ -30,6 +30,28 @@
         "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