Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ is missing or empty.
- Every iPad, iPhone and iPod touch screen now starts at the size it has
under iOS. Before, only the iPad 6, iPad 7 and iPhone 7 did; on the others,
such as the iPod touch 6, everything was drawn at half size.
- On a phone held upright, the on-screen keyboard has ten keys across as on
iOS, under a short row of Esc, Tab, Ctrl, Alt and the arrows. 123 holds the
digits and common symbols, and #+= the rest, plus Home, End, Page Up/Down
and Delete.
- A new launcher replaces the app grid: Home, Super and the bar's logo open
apps, actions and (on a phone) the system controls in swipeable tabs, with
search, recent apps and files, long-press app menus and full keyboard
Expand Down
168 changes: 163 additions & 5 deletions desktop/keyboard/friendly-layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
import sys


def key(label, code, width=1, shifted=None, kind="Code", special=False):
return (label, shifted if shifted is not None else label, width, kind, code, special)
def key(label, code, width=1, shifted=None, kind="Code", special=False, forced=None, target=None):
# forced: a modifier the key always sends ("$" is Shift+4); target: the
# layout a Layout key opens.
return (label, shifted if shifted is not None else label, width, kind, code, special, forced, target)


def mod(label, code, width=1):
Expand Down Expand Up @@ -101,6 +103,78 @@ def more(wide=False):
return [row1, row2, row3, row4, row5]


# A phone held upright: ten keys across, as on iOS, under a short row of the
# keys a terminal needs. 123 and #+= hold the symbols, each sent as the key
# (and Shift) that types it on a US keyboard, so they work in every app.
PHONE_WIDTH = 10


def typed(text):
plain = {"-": "MINUS", "/": "SLASH", ";": "SEMICOLON", "'": "APOSTROPHE", ".": "DOT",
",": "COMMA", "[": "LEFTBRACE", "]": "RIGHTBRACE", "=": "EQUAL", "\\": "BACKSLASH",
"`": "GRAVE"}
shifted = {":": "SEMICOLON", "(": "9", ")": "0", "$": "4", "&": "7", "@": "2", '"': "APOSTROPHE",
"?": "SLASH", "!": "1", "_": "MINUS", "|": "BACKSLASH", "{": "LEFTBRACE",
"}": "RIGHTBRACE", "#": "3", "%": "5", "^": "6", "*": "8", "+": "EQUAL", "~": "GRAVE",
"<": "COMMA", ">": "DOT"}
if text in plain:
return "KEY_" + plain[text], None
return "KEY_" + shifted[text], "Shift"


def sym(text, width=1):
code, forced = typed(text)
return key(text, code, width, forced=forced)


def go(label, target, width):
return key(label, "0", width, kind="Layout", special=True, target=target)


phone_tools = [key(label, "KEY_" + code, 1.25, special=True) for label, code in
[("Esc", "ESC"), ("Tab", "TAB")]] + \
[mod("Ctrl", "Ctrl", 1.25), mod("Alt", "Alt", 1.25)] + \
[key(label, "KEY_" + code, 1.25, special=True) for label, code in
[("←", "LEFT"), ("↓", "DOWN"), ("↑", "UP"), ("→", "RIGHT")]]
phone_backspace = key("⌫", "KEY_BACKSPACE", 1.5, special=True)
phone_enter = key("Enter", "KEY_ENTER", 2, special=True)
pad = key("", "0", .5, kind="Pad")


def phone_bottom(switch, left, right):
return [switch, mod("Super", "Super", 1.25), left, key("space", "KEY_SPACE", 3.5), right, phone_enter]


phone = [
phone_tools,
letters("qwertyuiop"),
[pad] + letters("asdfghjkl") + [pad],
[key("⇧", "Shift", 1.5, shifted="⇫", kind="Mod", special=True)] + letters("zxcvbnm") + [phone_backspace],
phone_bottom(go("123", "PhoneNum", 1.25), sym(","), sym(".")),
]
phone_punctuation = [sym(c, 1.4) for c in ".,?!'"] + [phone_backspace]
phone_num = [
phone_tools,
[key(c, "KEY_" + c) for c in "1234567890"],
[sym(c) for c in '-/:;()$&@"'],
[go("#+=", "PhoneSym", 1.5)] + phone_punctuation,
phone_bottom(go("ABC", "Phone", 1.25), sym("_"), sym("|")),
]
phone_sym = [
phone_tools,
[sym(c) for c in "[]{}#%^*+="],
[sym(c) for c in "\\~<>`"] + [key(label, "KEY_" + code, special=True) for label, code in
[("Home", "HOME"), ("End", "END"), ("PgUp", "PAGEUP"), ("PgDn", "PAGEDOWN"),
("Del", "DELETE")]],
[go("123", "PhoneNum", 1.5)] + phone_punctuation,
phone_bottom(go("ABC", "Phone", 1.25), sym("_"), sym("|")),
]
# Each new layout: its enum name, wvkbd's name for it (-l), and whether it is
# a letters layout.
PHONE_LAYOUTS = {"keys_phone": ("Phone", "phone", True, phone),
"keys_phone_num": ("PhoneNum", "phonenum", False, phone_num),
"keys_phone_sym": ("PhoneSym", "phonesym", False, phone_sym)}

LAYOUTS = {"keys_full": portrait, "keys_special": more(),
"keys_landscape": landscape, "keys_landscape_special": more(True)}

Expand Down Expand Up @@ -136,12 +210,28 @@ def validate():
"Ctrl", "Alt", "Super", "Shift"} <= codes


def validate_phone():
for name, (_, _, _, rows) in PHONE_LAYOUTS.items():
assert len(rows) == 5, name
assert rows[0] == phone_tools, name
for row in rows:
assert abs(sum(k[2] for k in row) - PHONE_WIDTH) < .0001, (name, row)
assert rows[-1][-1][4] == "KEY_ENTER" and rows[-1][3][4] == "KEY_SPACE", name
targets = {k[7] for row in rows for k in row if k[3] == "Layout"}
assert targets <= {v[0] for v in PHONE_LAYOUTS.values()}, (name, targets)
codes = {k[4] for rows in (phone, phone_num) for row in rows for k in row}
assert {"KEY_" + c.upper() for c in "abcdefghijklmnopqrstuvwxyz0123456789"} <= codes
assert {"KEY_ESC", "KEY_TAB", "Ctrl", "Alt", "Super", "Shift", "KEY_BACKSPACE", "KEY_ENTER",
"KEY_UP", "KEY_DOWN", "KEY_LEFT", "KEY_RIGHT"} <= codes


def render(name, rows):
lines = ["static struct key " + name + "[] = {"]
for row_index, row in enumerate(rows):
for label, shifted, width, kind, code, special in row:
for label, shifted, width, kind, code, special, forced, layout in row:
# Index links preserve upstream's international/compose chooser.
target = ", .layout = &layouts[Index]" if kind == "Layout" else ""
target = ", .layout = &layouts[%s]" % (layout or "Index") if kind == "Layout" else ""
target += ", .code_mod = " + forced if forced else ""
scheme = (", .scheme = 2" if kind == "Mod" and code == "Super" else
", .scheme = 4" if code in ("KEY_BACKSPACE", "KEY_UP", "KEY_DOWN", "KEY_LEFT", "KEY_RIGHT") else
", .scheme = 3" if code == "KEY_SPACE" else
Expand All @@ -156,6 +246,69 @@ def render(name, rows):
return "\n".join(lines)


# The phone's short top row: this share of a full row's height.
TOOLBAR_ROW = .55


def add_phone(text):
"""The phone layouts are new to wvkbd: name them, declare them, list them
and add their keys after the landscape ones."""
validate_phone()
names = list(PHONE_LAYOUTS)
enum = "".join("\t%s,\n" % PHONE_LAYOUTS[n][0] for n in names)
assert text.count("\tLandscapeSpecial,\n") == 1
text = text.replace("\tLandscapeSpecial,\n", "\tLandscapeSpecial,\n" + enum)
assert text.count("keys_landscape_special[],") == 1
text = text.replace("keys_landscape_special[],", "keys_landscape_special[], " +
", ".join(n + "[]" for n in names) + ",")
marker = ' [LandscapeSpecial] = {keys_landscape_special, "latin", "landscapespecial", true},\n'
assert text.count(marker) == 1
# The last field: the layout's first row is the short row (add_toolbar_row).
entries = "".join(' [%s] = {%s, "latin", "%s", %s, 0, true},\n' %
(PHONE_LAYOUTS[n][0], n, PHONE_LAYOUTS[n][1], "true" if PHONE_LAYOUTS[n][2] else "false")
for n in names)
text = text.replace(marker, marker + entries)
start = text.index("static struct key keys_landscape_special[] = {")
end = text.index("\n};", start) + len("\n};")
rendered = "\n\n".join(render(n, PHONE_LAYOUTS[n][3]) for n in names)
return text[:end] + "\n\n" + rendered + text[end:]


def add_toolbar_row(source):
header = source / "keyboard.h"
text = header.read_text()
marker = "\tuint32_t keyheight; // absolute height (pixels)\n"
assert text.count(marker) == 1
header.write_text(text.replace(marker, marker + "\tbool toolbar; // Orchard: the first row is a short row\n"))
code = source / "keyboard.c"
text = code.read_text()
old = """ l->keyheight = height / rows;

struct key *k = l->keys;
double rowlength = kbd_get_row_length(k);
double rowwidth = 0.0;
while (k->type != Last) {
if (k->type == EndRow) {
y += l->keyheight;"""
new = """ /* Orchard: a toolbar layout's first row is %s of a row's height. */
double units = rows - (l->toolbar ? 1 - %s : 0);
l->keyheight = height / units;
uint32_t rowheight = l->toolbar ? (uint32_t)(l->keyheight * %s) : l->keyheight;

struct key *k = l->keys;
double rowlength = kbd_get_row_length(k);
double rowwidth = 0.0;
while (k->type != Last) {
if (k->type == EndRow) {
y += rowheight;
rowheight = l->keyheight;""" % (TOOLBAR_ROW, TOOLBAR_ROW, TOOLBAR_ROW)
assert text.count(old) == 1
text = text.replace(old, new)
old = " k->h = l->keyheight;\n k++;"
assert text.count(old) == 1
code.write_text(text.replace(old, " k->h = rowheight;\n k++;"))


def apply(source):
validate()
layout = source / "layout.mobintl.h"
Expand All @@ -167,7 +320,9 @@ def apply(source):
end = text.index("\n};", start) + len("\n};")
text = text[:start] + render(name, rows) + text[end:]
text = text.replace("#define KBD_KEY_BORDER 2", "#define KBD_KEY_BORDER 3")
text = add_phone(text)
layout.write_text(text)
add_toolbar_row(source)
config = source / "config.mobintl.h"
text = config.read_text().replace("#define SHIFT_SPACE_IS_TAB", "/* Shift+Space remains a space; Tab has its own key. */")
text = text.replace("#define DEFAULT_ROUNDING 5", "#define DEFAULT_ROUNDING 7")
Expand Down Expand Up @@ -221,6 +376,9 @@ def apply(source):
assert '"\ue900", "\ue900"' in rendered
assert "Mod, Super, .scheme = 2" in rendered
assert "Code, KEY_SPACE, .scheme = 3" in rendered
print("Four five-row layouts: row separators, widths, letters, digits, modifiers and arrows OK")
validate_phone()
for name, (_, _, _, rows) in PHONE_LAYOUTS.items():
assert render(name, rows).count("EndRow") == 4
print("Four five-row layouts and three phone layouts: row separators, widths, letters, digits, modifiers and arrows OK")
else:
apply(Path(sys.argv[1]))
Loading
Loading