summaryrefslogtreecommitdiff
path: root/lib/python/qmk/keyboard.py
diff options
context:
space:
mode:
Diffstat (limited to 'lib/python/qmk/keyboard.py')
-rw-r--r--lib/python/qmk/keyboard.py90
1 files changed, 82 insertions, 8 deletions
diff --git a/lib/python/qmk/keyboard.py b/lib/python/qmk/keyboard.py
index 9826f3f887..b56505d649 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:
@@ -48,16 +70,25 @@ class AllKeyboards:
base_path = os.path.join(os.getcwd(), "keyboards") + os.path.sep
+@lru_cache(maxsize=1)
+def keyboard_alias_definitions():
+ return json_load(Path('data/mappings/keyboard_aliases.hjson'))
+
+
def is_all_keyboards(keyboard):
"""Returns True if the keyboard is an AllKeyboards object.
"""
+ if isinstance(keyboard, str):
+ return (keyboard == 'all')
return isinstance(keyboard, AllKeyboards)
def find_keyboard_from_dir():
"""Returns a keyboard name based on the user's current directory.
"""
- relative_cwd = qmk.path.under_qmk_firmware()
+ relative_cwd = qmk.path.under_qmk_userspace()
+ if not relative_cwd:
+ relative_cwd = qmk.path.under_qmk_firmware()
if relative_cwd and len(relative_cwd.parts) > 1 and relative_cwd.parts[0] == 'keyboards':
# Attempt to extract the keyboard name from the current directory
@@ -90,7 +121,7 @@ def keyboard_folder(keyboard):
This checks aliases and DEFAULT_FOLDER to resolve the actual path for a keyboard.
"""
- aliases = json_load(Path('data/mappings/keyboard_aliases.hjson'))
+ aliases = keyboard_alias_definitions()
while keyboard in aliases:
last_keyboard = keyboard
@@ -98,11 +129,7 @@ def keyboard_folder(keyboard):
if keyboard == last_keyboard:
break
- rules_mk_file = Path(base_path, keyboard, 'rules.mk')
-
- if rules_mk_file.exists():
- rules_mk = parse_rules_mk_file(rules_mk_file)
- keyboard = rules_mk.get('DEFAULT_FOLDER', keyboard)
+ keyboard = resolve_keyboard(keyboard)
if not qmk.path.is_keyboard(keyboard):
raise ValueError(f'Invalid keyboard: {keyboard}')
@@ -110,6 +137,22 @@ def keyboard_folder(keyboard):
return keyboard
+def keyboard_aliases(keyboard):
+ """Returns the list of aliases for the supplied keyboard.
+
+ Includes the keyboard itself.
+ """
+ aliases = json_load(Path('data/mappings/keyboard_aliases.hjson'))
+
+ if keyboard in aliases:
+ keyboard = aliases[keyboard].get('target', keyboard)
+
+ keyboards = set(filter(lambda k: aliases[k].get('target', '') == keyboard, aliases.keys()))
+ keyboards.add(keyboard)
+ keyboards = list(sorted(keyboards))
+ return keyboards
+
+
def keyboard_folder_or_all(keyboard):
"""Returns the actual keyboard folder.
@@ -217,7 +260,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)
@@ -335,3 +380,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