Skip to main content

Module layout

Module layout 

Source
Expand description

Key positions to characters: layouts, dead keys and composition.

crate::keymap answers where a key is; this answers what it types. The split matters because a position is a fact about the hardware and a character is a fact about the user’s layout, and conflating them is how a toolkit ends up unable to type ø on the machine it was written for.

Everything here is platform-independent and allocation-free, so it is unit tested rather than inferred from a keyboard someone happened to have plugged in.

§Using the system’s layout

from_system reads what the machine is already configured for — DENISE_KEYMAP, then XKB_DEFAULT_LAYOUT, then the console keyboard configuration files distributions actually write. On the Raspberry Pi this was developed against, /etc/conf.d/loadkmap says no and the panel picks it up with nothing set by hand.

That reads the system’s choice. Reading the system’s layout data is a different question, and the reason this crate carries its own tables:

  • The kernel’s own keymap, via KDGKBENT and KDGKBDIACRUC on a VT, is the technically right answer and is not much code. It needs /dev/tty0, which is root:root mode 600 on every distribution checked. Denise otherwise runs unprivileged, needing only the video and input groups, and giving that up to read a keymap is a poor trade.
  • libxkbcommon is the correct answer on a desktop and the wrong one here: a C library with a runtime data directory, which defeats “one static binary” on a read-only root.

So the choice comes from the system and the data comes from here. The cost is that a system configured for a layout Denise has no table for falls back to US — visibly, through LayoutSource, rather than by typing the wrong thing. Adding a table is about thirty lines; needing root is forever.

§Control characters are never text

Enter, Tab and Backspace produce InputEvent::Key and nothing else. InputEvent::Text carries characters a user meant to insert, so a text field can insert everything it receives without filtering, and a key binding cannot be shadowed by a stray control character.

Structs§

Composed
Characters produced by one keystroke: never more than two.
Composer
Turns key transitions into characters, holding the state a layout needs.
Entry
One physical position and what it types at each of four levels.
Layout
A keyboard layout: a table of positions, and the decimal key’s character.

Enums§

LayoutSource
Where a layout choice came from, for logging what a panel actually picked up.
Output
What one position produces at one shift level.

Statics§

BUILT_IN
Every layout that ships, for a runtime lookup by name.
NORWEGIAN
Norwegian (Bokmål) QWERTY.
US
US QWERTY. No dead keys, no third level.

Functions§

by_name
Finds a layout by its short name, as setxkbmap would name it.
from_system
Finds the layout this system is configured for.
normalise_name
Reduces whatever a system wrote down to a layout name.