123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 |
- """Functions that help us work with keyboards.
- """
- from array import array
- from math import ceil
- from pathlib import Path
- import os
- from glob import glob
- import qmk.path
- from qmk.c_parse import parse_config_h_file
- from qmk.json_schema import json_load
- from qmk.makefile import parse_rules_mk_file
- BOX_DRAWING_CHARACTERS = {
- "unicode": {
- "tl": "┌",
- "tr": "┐",
- "bl": "└",
- "br": "┘",
- "v": "│",
- "h": "─",
- },
- "ascii": {
- "tl": " ",
- "tr": " ",
- "bl": "|",
- "br": "|",
- "v": "|",
- "h": "_",
- },
- }
- base_path = os.path.join(os.getcwd(), "keyboards") + os.path.sep
- def find_keyboard_from_dir():
- """Returns a keyboard name based on the user's current directory.
- """
- 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
- 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]
- if qmk.path.is_keyboard(current_path):
- return str(current_path)
- 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'
- def keyboard_folder(keyboard):
- """Returns the actual keyboard folder.
- This checks aliases and DEFAULT_FOLDER to resolve the actual path for a keyboard.
- """
- aliases = json_load(Path('data/mappings/keyboard_aliases.json'))
- if keyboard in aliases:
- keyboard = aliases[keyboard].get('target', keyboard)
- 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)
- if not qmk.path.is_keyboard(keyboard):
- raise ValueError(f'Invalid keyboard: {keyboard}')
- return keyboard
- def _find_name(path):
- """Determine the keyboard name by stripping off the base_path and rules.mk.
- """
- return path.replace(base_path, "").replace(os.path.sep + "rules.mk", "")
- def keyboard_completer(prefix, action, parser, parsed_args):
- """Returns a list of keyboards for tab completion.
- """
- return list_keyboards()
- def list_keyboards():
- """Returns a list of all keyboards.
- """
- # We avoid pathlib here because this is performance critical code.
- kb_wildcard = os.path.join(base_path, "**", "rules.mk")
- paths = [path for path in glob(kb_wildcard, recursive=True) if os.path.sep + 'keymaps' + os.path.sep not in path]
- return sorted(set(map(resolve_keyboard, map(_find_name, paths))))
- 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
- 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')
- keyboard = Path(resolve_keyboard(keyboard))
- 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')
- keyboard = Path(resolve_keyboard(keyboard))
- 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
- def render_layout(layout_data, render_ascii, key_labels=None):
- """Renders a single layout.
- """
- textpad = [array('u', ' ' * 200) for x in range(100)]
- style = 'ascii' if render_ascii else 'unicode'
- for key in layout_data:
- x = key.get('x', 0)
- y = key.get('y', 0)
- w = key.get('w', 1)
- h = key.get('h', 1)
- if key_labels:
- label = key_labels.pop(0)
- if label.startswith('KC_'):
- label = label[3:]
- else:
- label = key.get('label', '')
- if x >= 0.25 and w == 1.25 and h == 2:
- render_key_isoenter(textpad, x, y, w, h, label, style)
- elif w == 2.25 and h == 2:
- render_key_baenter(textpad, x, y, w, h, label, style)
- else:
- render_key_rect(textpad, x, y, w, h, label, style)
- lines = []
- for line in textpad:
- if line.tounicode().strip():
- lines.append(line.tounicode().rstrip())
- return '\n'.join(lines)
- def render_layouts(info_json, render_ascii):
- """Renders all the layouts from an `info_json` structure.
- """
- layouts = {}
- for layout in info_json['layouts']:
- layout_data = info_json['layouts'][layout]['layout']
- layouts[layout] = render_layout(layout_data, render_ascii)
- return layouts
- 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
- label_middle = label + ' ' * label_leftover
- 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]
- label_blank = ' ' * (label_len - 1)
- label_border_top = box_chars['h'] * label_len
- label_border_bottom = box_chars['h'] * (label_len - 1)
- label_middle = label + ' ' * label_leftover
- 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
- 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)
- label_len = w - 2
- label_leftover = label_len - len(label)
- if len(label) > label_len:
- label = label[:label_len]
- label_blank = ' ' * (label_len - 3)
- label_border_top = box_chars['h'] * (label_len - 3)
- label_border_bottom = box_chars['h'] * label_len
- label_middle = label + ' ' * label_leftover
- 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'])
- textpad[y][x + 3:x + w] = top_line
- textpad[y + 1][x + 3:x + w] = mid_line
- textpad[y + 2][x + 3:x + w] = mid_line
- textpad[y + 3][x:x + w] = crn_line
- textpad[y + 4][x:x + w] = lab_line
- textpad[y + 5][x:x + w] = bot_line
|