2020-05-26 22:05:41 +02:00
|
|
|
"""Functions that help us work with keyboards.
|
|
|
|
"""
|
|
|
|
from array import array
|
2023-09-28 22:48:20 +02:00
|
|
|
from functools import lru_cache
|
2020-05-26 22:05:41 +02:00
|
|
|
from math import ceil
|
|
|
|
from pathlib import Path
|
2020-10-25 22:48:44 +01:00
|
|
|
import os
|
|
|
|
from glob import glob
|
2020-05-26 22:05:41 +02:00
|
|
|
|
2021-05-25 04:38:27 +02:00
|
|
|
import qmk.path
|
2020-05-26 22:05:41 +02:00
|
|
|
from qmk.c_parse import parse_config_h_file
|
2021-03-24 17:26:38 +01:00
|
|
|
from qmk.json_schema import json_load
|
2020-05-26 22:05:41 +02:00
|
|
|
from qmk.makefile import parse_rules_mk_file
|
|
|
|
|
2020-11-02 09:41:01 +01:00
|
|
|
BOX_DRAWING_CHARACTERS = {
|
|
|
|
"unicode": {
|
|
|
|
"tl": "┌",
|
|
|
|
"tr": "┐",
|
|
|
|
"bl": "└",
|
|
|
|
"br": "┘",
|
|
|
|
"v": "│",
|
|
|
|
"h": "─",
|
|
|
|
},
|
|
|
|
"ascii": {
|
|
|
|
"tl": " ",
|
|
|
|
"tr": " ",
|
|
|
|
"bl": "|",
|
|
|
|
"br": "|",
|
|
|
|
"v": "|",
|
|
|
|
"h": "_",
|
|
|
|
},
|
|
|
|
}
|
2023-11-20 16:41:48 +01:00
|
|
|
ENC_DRAWING_CHARACTERS = {
|
|
|
|
"unicode": {
|
|
|
|
"tl": "╭",
|
|
|
|
"tr": "╮",
|
|
|
|
"bl": "╰",
|
|
|
|
"br": "╯",
|
|
|
|
"vl": "▲",
|
|
|
|
"vr": "▼",
|
|
|
|
"v": "│",
|
|
|
|
"h": "─",
|
|
|
|
},
|
|
|
|
"ascii": {
|
|
|
|
"tl": " ",
|
|
|
|
"tr": " ",
|
|
|
|
"bl": "\\",
|
|
|
|
"br": "/",
|
|
|
|
"v": "|",
|
|
|
|
"vl": "/",
|
|
|
|
"vr": "\\",
|
|
|
|
"h": "_",
|
|
|
|
},
|
|
|
|
}
|
2020-11-02 09:41:01 +01:00
|
|
|
|
2023-09-22 04:12:20 +02:00
|
|
|
|
|
|
|
class AllKeyboards:
|
|
|
|
"""Represents all keyboards.
|
|
|
|
"""
|
|
|
|
def __str__(self):
|
|
|
|
return 'all'
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return 'all'
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
return isinstance(other, AllKeyboards)
|
|
|
|
|
|
|
|
|
2020-10-25 22:48:44 +01:00
|
|
|
base_path = os.path.join(os.getcwd(), "keyboards") + os.path.sep
|
|
|
|
|
|
|
|
|
2023-11-22 01:14:34 +01:00
|
|
|
@lru_cache(maxsize=1)
|
|
|
|
def keyboard_alias_definitions():
|
|
|
|
return json_load(Path('data/mappings/keyboard_aliases.hjson'))
|
|
|
|
|
|
|
|
|
2023-09-22 04:12:20 +02:00
|
|
|
def is_all_keyboards(keyboard):
|
|
|
|
"""Returns True if the keyboard is an AllKeyboards object.
|
|
|
|
"""
|
2023-11-27 21:53:43 +01:00
|
|
|
if isinstance(keyboard, str):
|
|
|
|
return (keyboard == 'all')
|
2023-09-22 04:12:20 +02:00
|
|
|
return isinstance(keyboard, AllKeyboards)
|
|
|
|
|
|
|
|
|
2021-04-15 04:00:22 +02:00
|
|
|
def find_keyboard_from_dir():
|
|
|
|
"""Returns a keyboard name based on the user's current directory.
|
|
|
|
"""
|
2023-11-27 21:53:43 +01:00
|
|
|
relative_cwd = qmk.path.under_qmk_userspace()
|
|
|
|
if not relative_cwd:
|
|
|
|
relative_cwd = qmk.path.under_qmk_firmware()
|
2021-04-15 04:00:22 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
current_path = Path('/'.join(relative_cwd.parts[1:]))
|
|
|
|
|
|
|
|
if 'keymaps' in current_path.parts:
|
|
|
|
# Strip current_path of anything after `keymaps`
|
|
|
|
keymap_index = len(current_path.parts) - current_path.parts.index('keymaps') - 1
|
|
|
|
current_path = current_path.parents[keymap_index]
|
|
|
|
|
2024-05-01 03:52:34 +02:00
|
|
|
current_path = resolve_keyboard(current_path)
|
|
|
|
|
2021-05-25 04:38:27 +02:00
|
|
|
if qmk.path.is_keyboard(current_path):
|
2021-04-15 04:00:22 +02:00
|
|
|
return str(current_path)
|
|
|
|
|
|
|
|
|
2021-05-25 04:38:27 +02:00
|
|
|
def find_readme(keyboard):
|
|
|
|
"""Returns the readme for this keyboard.
|
|
|
|
"""
|
|
|
|
cur_dir = qmk.path.keyboard(keyboard)
|
|
|
|
keyboards_dir = Path('keyboards')
|
|
|
|
while not (cur_dir / 'readme.md').exists():
|
|
|
|
if cur_dir == keyboards_dir:
|
|
|
|
return None
|
|
|
|
cur_dir = cur_dir.parent
|
|
|
|
|
|
|
|
return cur_dir / 'readme.md'
|
|
|
|
|
|
|
|
|
2021-03-24 17:26:38 +01:00
|
|
|
def keyboard_folder(keyboard):
|
|
|
|
"""Returns the actual keyboard folder.
|
|
|
|
|
|
|
|
This checks aliases and DEFAULT_FOLDER to resolve the actual path for a keyboard.
|
|
|
|
"""
|
2023-11-22 01:14:34 +01:00
|
|
|
aliases = keyboard_alias_definitions()
|
2021-03-24 17:26:38 +01:00
|
|
|
|
2023-10-06 01:34:23 +02:00
|
|
|
while keyboard in aliases:
|
|
|
|
last_keyboard = keyboard
|
2021-03-24 17:26:38 +01:00
|
|
|
keyboard = aliases[keyboard].get('target', keyboard)
|
2023-10-06 01:34:23 +02:00
|
|
|
if keyboard == last_keyboard:
|
|
|
|
break
|
2021-03-24 17:26:38 +01:00
|
|
|
|
2023-11-15 06:24:54 +01:00
|
|
|
keyboard = resolve_keyboard(keyboard)
|
2021-03-24 17:26:38 +01:00
|
|
|
|
2021-05-25 04:38:27 +02:00
|
|
|
if not qmk.path.is_keyboard(keyboard):
|
2021-03-24 17:26:38 +01:00
|
|
|
raise ValueError(f'Invalid keyboard: {keyboard}')
|
|
|
|
|
|
|
|
return keyboard
|
|
|
|
|
|
|
|
|
2023-11-27 21:53:43 +01:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2023-09-22 04:12:20 +02:00
|
|
|
def keyboard_folder_or_all(keyboard):
|
|
|
|
"""Returns the actual keyboard folder.
|
|
|
|
|
|
|
|
This checks aliases and DEFAULT_FOLDER to resolve the actual path for a keyboard.
|
|
|
|
If the supplied argument is "all", it returns an AllKeyboards object.
|
|
|
|
"""
|
|
|
|
if keyboard == 'all':
|
|
|
|
return AllKeyboards()
|
|
|
|
|
|
|
|
return keyboard_folder(keyboard)
|
|
|
|
|
|
|
|
|
2020-10-25 22:48:44 +01:00
|
|
|
def _find_name(path):
|
2024-03-10 06:20:25 +01:00
|
|
|
"""Determine the keyboard name by stripping off the base_path and filename.
|
2020-10-25 22:48:44 +01:00
|
|
|
"""
|
2024-03-10 06:20:25 +01:00
|
|
|
return path.replace(base_path, "").rsplit(os.path.sep, 1)[0]
|
2020-10-25 22:48:44 +01:00
|
|
|
|
|
|
|
|
2021-04-15 04:00:22 +02:00
|
|
|
def keyboard_completer(prefix, action, parser, parsed_args):
|
|
|
|
"""Returns a list of keyboards for tab completion.
|
|
|
|
"""
|
|
|
|
return list_keyboards()
|
|
|
|
|
|
|
|
|
2023-01-03 04:15:29 +01:00
|
|
|
def list_keyboards(resolve_defaults=True):
|
|
|
|
"""Returns a list of all keyboards - optionally processing any DEFAULT_FOLDER.
|
2020-10-25 22:48:44 +01:00
|
|
|
"""
|
|
|
|
# We avoid pathlib here because this is performance critical code.
|
2024-03-10 06:20:25 +01:00
|
|
|
paths = []
|
|
|
|
for marker in ['rules.mk', 'keyboard.json']:
|
|
|
|
kb_wildcard = os.path.join(base_path, "**", marker)
|
|
|
|
paths += [path for path in glob(kb_wildcard, recursive=True) if os.path.sep + 'keymaps' + os.path.sep not in path]
|
2020-10-25 22:48:44 +01:00
|
|
|
|
2023-01-03 04:15:29 +01:00
|
|
|
found = map(_find_name, paths)
|
|
|
|
if resolve_defaults:
|
|
|
|
found = map(resolve_keyboard, found)
|
|
|
|
|
|
|
|
return sorted(set(found))
|
2021-04-25 03:15:54 +02:00
|
|
|
|
|
|
|
|
2023-09-28 22:48:20 +02:00
|
|
|
@lru_cache(maxsize=None)
|
2021-04-25 03:15:54 +02:00
|
|
|
def resolve_keyboard(keyboard):
|
|
|
|
cur_dir = Path('keyboards')
|
|
|
|
rules = parse_rules_mk_file(cur_dir / keyboard / 'rules.mk')
|
|
|
|
while 'DEFAULT_FOLDER' in rules and keyboard != rules['DEFAULT_FOLDER']:
|
|
|
|
keyboard = rules['DEFAULT_FOLDER']
|
|
|
|
rules = parse_rules_mk_file(cur_dir / keyboard / 'rules.mk')
|
|
|
|
return keyboard
|
2020-10-25 22:48:44 +01:00
|
|
|
|
2020-05-26 22:05:41 +02:00
|
|
|
|
|
|
|
def config_h(keyboard):
|
|
|
|
"""Parses all the config.h files for a keyboard.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
keyboard: name of the keyboard
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
a dictionary representing the content of the entire config.h tree for a keyboard
|
|
|
|
"""
|
|
|
|
config = {}
|
|
|
|
cur_dir = Path('keyboards')
|
2021-04-25 03:15:54 +02:00
|
|
|
keyboard = Path(resolve_keyboard(keyboard))
|
2020-05-26 22:05:41 +02:00
|
|
|
|
|
|
|
for dir in keyboard.parts:
|
|
|
|
cur_dir = cur_dir / dir
|
|
|
|
config = {**config, **parse_config_h_file(cur_dir / 'config.h')}
|
|
|
|
|
|
|
|
return config
|
|
|
|
|
|
|
|
|
|
|
|
def rules_mk(keyboard):
|
|
|
|
"""Get a rules.mk for a keyboard
|
|
|
|
|
|
|
|
Args:
|
|
|
|
keyboard: name of the keyboard
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
a dictionary representing the content of the entire rules.mk tree for a keyboard
|
|
|
|
"""
|
|
|
|
cur_dir = Path('keyboards')
|
2021-04-25 03:15:54 +02:00
|
|
|
keyboard = Path(resolve_keyboard(keyboard))
|
2020-05-26 22:05:41 +02:00
|
|
|
rules = parse_rules_mk_file(cur_dir / keyboard / 'rules.mk')
|
|
|
|
|
|
|
|
for i, dir in enumerate(keyboard.parts):
|
|
|
|
cur_dir = cur_dir / dir
|
|
|
|
rules = parse_rules_mk_file(cur_dir / 'rules.mk', rules)
|
|
|
|
|
|
|
|
return rules
|
|
|
|
|
|
|
|
|
2020-11-02 09:41:01 +01:00
|
|
|
def render_layout(layout_data, render_ascii, key_labels=None):
|
2020-05-26 22:05:41 +02:00
|
|
|
"""Renders a single layout.
|
|
|
|
"""
|
2021-06-24 05:35:36 +02:00
|
|
|
textpad = [array('u', ' ' * 200) for x in range(100)]
|
2020-11-02 09:41:01 +01:00
|
|
|
style = 'ascii' if render_ascii else 'unicode'
|
2020-05-26 22:05:41 +02:00
|
|
|
|
|
|
|
for key in layout_data:
|
2022-03-05 23:54:32 +01:00
|
|
|
x = key.get('x', 0)
|
|
|
|
y = key.get('y', 0)
|
|
|
|
w = key.get('w', 1)
|
|
|
|
h = key.get('h', 1)
|
2020-05-26 22:05:41 +02:00
|
|
|
|
|
|
|
if key_labels:
|
|
|
|
label = key_labels.pop(0)
|
|
|
|
if label.startswith('KC_'):
|
|
|
|
label = label[3:]
|
|
|
|
else:
|
|
|
|
label = key.get('label', '')
|
|
|
|
|
2023-11-20 16:41:48 +01:00
|
|
|
if 'encoder' in key:
|
|
|
|
render_encoder(textpad, x, y, w, h, label, style)
|
|
|
|
elif x >= 0.25 and w == 1.25 and h == 2:
|
2022-03-05 23:54:32 +01:00
|
|
|
render_key_isoenter(textpad, x, y, w, h, label, style)
|
2023-07-07 16:08:13 +02:00
|
|
|
elif w == 1.5 and h == 2:
|
2022-03-08 21:45:47 +01:00
|
|
|
render_key_baenter(textpad, x, y, w, h, label, style)
|
2022-03-05 23:54:32 +01:00
|
|
|
else:
|
|
|
|
render_key_rect(textpad, x, y, w, h, label, style)
|
2020-05-26 22:05:41 +02:00
|
|
|
|
|
|
|
lines = []
|
|
|
|
for line in textpad:
|
|
|
|
if line.tounicode().strip():
|
|
|
|
lines.append(line.tounicode().rstrip())
|
|
|
|
|
|
|
|
return '\n'.join(lines)
|
|
|
|
|
|
|
|
|
2020-11-02 09:41:01 +01:00
|
|
|
def render_layouts(info_json, render_ascii):
|
2020-05-26 22:05:41 +02:00
|
|
|
"""Renders all the layouts from an `info_json` structure.
|
|
|
|
"""
|
|
|
|
layouts = {}
|
|
|
|
|
|
|
|
for layout in info_json['layouts']:
|
|
|
|
layout_data = info_json['layouts'][layout]['layout']
|
2020-11-02 09:41:01 +01:00
|
|
|
layouts[layout] = render_layout(layout_data, render_ascii)
|
2020-05-26 22:05:41 +02:00
|
|
|
|
|
|
|
return layouts
|
2022-03-05 23:54:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
def render_key_rect(textpad, x, y, w, h, label, style):
|
|
|
|
box_chars = BOX_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
|
2022-03-30 22:54:13 +02:00
|
|
|
label_middle = label + ' ' * label_leftover
|
2022-03-05 23:54:32 +01:00
|
|
|
|
|
|
|
top_line = array('u', box_chars['tl'] + label_border + box_chars['tr'])
|
|
|
|
lab_line = array('u', box_chars['v'] + label_middle + box_chars['v'])
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
def render_key_isoenter(textpad, x, y, w, h, label, style):
|
|
|
|
box_chars = BOX_DRAWING_CHARACTERS[style]
|
|
|
|
x = ceil(x * 4)
|
|
|
|
y = ceil(y * 3)
|
|
|
|
w = ceil(w * 4)
|
|
|
|
h = ceil(h * 3)
|
|
|
|
|
|
|
|
label_len = w - 1
|
|
|
|
label_leftover = label_len - len(label)
|
|
|
|
|
|
|
|
if len(label) > label_len:
|
|
|
|
label = label[:label_len]
|
|
|
|
|
2022-03-30 22:54:13 +02:00
|
|
|
label_blank = ' ' * (label_len - 1)
|
2022-03-05 23:54:32 +01:00
|
|
|
label_border_top = box_chars['h'] * label_len
|
2022-03-30 22:54:13 +02:00
|
|
|
label_border_bottom = box_chars['h'] * (label_len - 1)
|
|
|
|
label_middle = label + ' ' * label_leftover
|
2022-03-05 23:54:32 +01:00
|
|
|
|
|
|
|
top_line = array('u', box_chars['tl'] + label_border_top + box_chars['tr'])
|
|
|
|
lab_line = array('u', box_chars['v'] + label_middle + box_chars['v'])
|
|
|
|
crn_line = array('u', box_chars['bl'] + box_chars['tr'] + label_blank + box_chars['v'])
|
|
|
|
mid_line = array('u', box_chars['v'] + label_blank + box_chars['v'])
|
|
|
|
bot_line = array('u', box_chars['bl'] + label_border_bottom + box_chars['br'])
|
|
|
|
|
|
|
|
textpad[y][x - 1:x + w] = top_line
|
|
|
|
textpad[y + 1][x - 1:x + w] = lab_line
|
|
|
|
textpad[y + 2][x - 1:x + w] = crn_line
|
|
|
|
textpad[y + 3][x:x + w] = mid_line
|
|
|
|
textpad[y + 4][x:x + w] = mid_line
|
2022-03-08 21:45:47 +01:00
|
|
|
textpad[y + 5][x:x + w] = bot_line
|
|
|
|
|
|
|
|
|
|
|
|
def render_key_baenter(textpad, x, y, w, h, label, style):
|
|
|
|
box_chars = BOX_DRAWING_CHARACTERS[style]
|
|
|
|
x = ceil(x * 4)
|
|
|
|
y = ceil(y * 3)
|
|
|
|
w = ceil(w * 4)
|
|
|
|
h = ceil(h * 3)
|
|
|
|
|
2023-07-07 16:08:13 +02:00
|
|
|
label_len = w + 1
|
2022-03-08 21:45:47 +01:00
|
|
|
label_leftover = label_len - len(label)
|
|
|
|
|
|
|
|
if len(label) > label_len:
|
|
|
|
label = label[:label_len]
|
|
|
|
|
2022-03-30 22:54:13 +02:00
|
|
|
label_blank = ' ' * (label_len - 3)
|
|
|
|
label_border_top = box_chars['h'] * (label_len - 3)
|
2022-03-08 21:45:47 +01:00
|
|
|
label_border_bottom = box_chars['h'] * label_len
|
2022-03-30 22:54:13 +02:00
|
|
|
label_middle = label + ' ' * label_leftover
|
2022-03-08 21:45:47 +01:00
|
|
|
|
|
|
|
top_line = array('u', box_chars['tl'] + label_border_top + box_chars['tr'])
|
|
|
|
mid_line = array('u', box_chars['v'] + label_blank + box_chars['v'])
|
|
|
|
crn_line = array('u', box_chars['tl'] + box_chars['h'] + box_chars['h'] + box_chars['br'] + label_blank + box_chars['v'])
|
|
|
|
lab_line = array('u', box_chars['v'] + label_middle + box_chars['v'])
|
|
|
|
bot_line = array('u', box_chars['bl'] + label_border_bottom + box_chars['br'])
|
|
|
|
|
2023-07-07 16:08:13 +02:00
|
|
|
textpad[y][x:x + w] = top_line
|
|
|
|
textpad[y + 1][x:x + w] = mid_line
|
|
|
|
textpad[y + 2][x:x + w] = mid_line
|
|
|
|
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
|
2023-11-20 16:41:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
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
|