Files
MeshTerm/meshterm/emulator/devices.py
T
Jean-Pierre MartineauandClaude Opus 5.5 b9585e9b59 Holding Esc quits wherever MeshTerm can tell the key is held
Hold-to-quit was the Cardputer's alone. It is now MeshTerm's gesture
everywhere a key can be seen coming up, which a terminal never reports:

- The emulator's window, for either handheld, which reads its own keys
  as the Cardputer's panel does.
- A terminal's Esc, where the keyboard can be asked whether it is still
  down: GetAsyncKeyState on Windows, as the right-Ctrl rescue already
  asks, and EVIOCGKEY on Linux wherever the keyboard's event device
  opens (the PicoCalc, a uConsole, a user in `input`). It reads the key
  state only, no events.

An Esc arriving while its key is still down is held back as a hold:
delivered as it comes up, a box after a second, gone at three. The
keyboard's repeats are part of the hold, a key typed during it goes in
behind the Esc, and on a VT stream the last repeat, which prompt_toolkit
can parse after the key is up, is dropped rather than going back a
screen behind a box the reader just let go of. On macOS and over SSH Esc
is unchanged.

SIGTERM is a quit for the interactive menu on every platform now, not
only under the Cardputer's launcher: a system shutting down gets the
same clean exit, terminal and console font restored.

EVIOCGKEY checked on the Cardputer's arm64 kernel; the Lyra was offline.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-06 01:20:53 -04:00

110 lines
4.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# SPDX-License-Identifier: Apache-2.0
"""The handhelds the emulator can stand in for, and what about each it reproduces.
A device here is its *display and keyboard* as MeshTerm meets them — the platform MeshTerm
runs as on it, the panel's size in pixels, how a console draws on that panel, and which
desktop keys stand in for its lane keys — never its processor or its operating system
(see :mod:`meshterm.emulator`). Each is named by its platform's id, so ``meshterm emulate
picocalc-lyra`` and ``--platform picocalc-lyra`` say the same device.
"""
from __future__ import annotations
from dataclasses import dataclass, field
from typing import TYPE_CHECKING
from prompt_toolkit.output.color_depth import ColorDepth
from ..platforms import CARDPUTER_ZERO, PICOCALC_LYRA, Platform
from .keys import Key
if TYPE_CHECKING:
from ..ui.tui.fkeys import LaneDeck
@dataclass(frozen=True)
class EmulatedDevice:
"""One handheld's display and keyboard, as the emulator reproduces them.
Attributes:
platform: The platform MeshTerm runs as on the device; its grid is the panel's.
name: The device as people call it, for the window's title.
panel: The panel's size in pixels, ``(width, height)``.
console: Whether a Linux console draws the device's screen — the grid from the
panel's top-left, bold as bright, the palette's slot 7 on slot 0 as the default
colours, and sixteen colours out — rather than MeshTerm in truecolour.
lane_keys: The keys under the panel that drive the F-key lane, as ``(label,
centre)`` pairs in panel pixels, one per slot of the :attr:`deck`, left to
right — drawn under the window's panel so a chip that drifts off its key
shows, and pressed there with the mouse. Empty for a device whose lane keys
sit elsewhere.
shifted: The desktop's Shift+F-key as the key the device's keyboard sends instead.
The PicoCalc's keyboard sends F6–F10 for Shift+F1–F5; the Cardputer Zero's sends
a shifted F4–F8, which needs no translating.
"""
platform: Platform
name: str
panel: tuple[int, int]
console: bool = False
lane_keys: tuple[tuple[str, int], ...] = ()
shifted: dict[str, str] = field(default_factory=dict)
@property
def id(self) -> str:
"""The device's id: its platform's name (``picocalc-lyra``)."""
return self.platform.name
@property
def color_depth(self) -> ColorDepth:
"""What prompt_toolkit may send: sixteen colours to a console, truecolour otherwise."""
return ColorDepth.DEPTH_4_BIT if self.console else ColorDepth.TRUE_COLOR
@property
def deck(self) -> LaneDeck:
"""The F-key lane the device's platform deals (:mod:`~meshterm.ui.tui.fkeys`)."""
from ..ui.tui.fkeys import DECKS
return DECKS[self.platform.lane_deck]
def translate(self, key: Key) -> Key:
"""The key as the device's keyboard would send it (see :attr:`shifted`)."""
if key.shift and key.name in self.shifted:
return Key(name=self.shifted[key.name], ctrl=key.ctrl, alt=key.alt)
return key
#: M5Stack's Cardputer Zero: a 320×170 panel MeshTerm draws itself, its lane on Fn+4…8 —
#: the five keys right under the panel, at the positions M5's drawing gives them.
CARDPUTER_ZERO_DEVICE = EmulatedDevice(
platform=CARDPUTER_ZERO,
name="Cardputer Zero",
panel=(320, 170),
lane_keys=(("4", 48), ("5", 104), ("6", 160), ("7", 216), ("8", 272)),
)
#: ClockworkPi's PicoCalc with a Luckfox Lyra core: a 320×320 panel the Linux console draws
#: in 6×12 cells, its lane on F1–F5 with Shift sending F6–F10.
PICOCALC_LYRA_DEVICE = EmulatedDevice(
platform=PICOCALC_LYRA,
name="PicoCalc (Luckfox Lyra)",
panel=(320, 320),
console=True,
shifted={f"f{n}": f"f{n + 5}" for n in range(1, 6)},
)
#: Every device the emulator offers, by id.
DEVICES: dict[str, EmulatedDevice] = {
device.id: device for device in (CARDPUTER_ZERO_DEVICE, PICOCALC_LYRA_DEVICE)
}
def device(name: str) -> EmulatedDevice:
"""The device called ``name``, or a ``ValueError`` naming the ones there are."""
try:
return DEVICES[name.strip().lower()]
except KeyError:
raise ValueError(
f"no emulated device {name!r} - choose from: {', '.join(sorted(DEVICES))}"
) from None