hex/crates/keyboard
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Sebastian Herrlinger 29e42bd613 Keyboard core: the swipe decoder's math from the libm crate, alike on iOS and Android
The decoder's exp, log, atan2 and cos, and the slid tap's hypot, now come
from the pure-Rust libm crate instead of each platform's own libm (Apple's
on iOS, bionic's on Android), so both platforms read every swipe alike,
bit for bit. The owner chose this (D0, option a), accepting the changes
against Apple's libm measured on the 200,000 Swift-recorded swipes:

- on dev's earlier English model: 2 readings change order (en case 6849,
  "cured" now ahead of "cued", the top word; es case 192084, "preso" and
  "pesó" at positions 3 and 4), 0 change `scored`, and 16,880 of 767,854
  costs change (15,315 by 1 ulp, 1,564 by 2, one by 0.095 nats);
- recorded again on dev's current model and rows: 1 reading changes
  order (es case 196995, "porquero" and "piquero" at positions 2 and 3),
  0 change `scored`, and 16,749 of 770,227 costs change (15,103 by 1 ulp,
  1,645 by 2, one by 3).

The parity fixture keeps the Swift decoder's words, order and `scored`
(none change) with the core's costs: 133 of its 6,195 move by 1 or 2 ulps.
Its header says so (`"costs"`), and swipe_parity.rs then requires every
cost bit for bit; on a Swift-recorded export it counts changed costs
instead. SwipeParityExportTests writes the same header field.
2026-11-23 00:41:48 +01:00
..
core Keyboard core: the swipe decoder's math from the libm crate, alike on iOS and Android 2026-11-23 00:41:48 +01:00
ffi Keyboard core: no space before a swiped word right after an attachment 2026-11-23 00:41:36 +01:00
tools Keyboard core builds take Homebrew's rustup: a cargo that runs the pinned toolchain 2026-11-22 20:02:35 +01:00
Cargo.lock Keyboard core: the swipe decoder's math from the libm crate, alike on iOS and Android 2026-11-23 00:41:48 +01:00
Cargo.toml Keyboard core: Rust workspace, HXTM v3 model reader, and the C interface for swipe 2026-11-22 20:02:26 +01:00
README.md Keyboard core: the swipe decoder's math from the libm crate, alike on iOS and Android 2026-11-23 00:41:48 +01:00
rust-toolchain.toml Keyboard core: Rust workspace, HXTM v3 model reader, and the C interface for swipe 2026-11-22 20:02:26 +01:00

Hex keyboard core

The keyboard's platform-independent logic in Rust, shared by the iOS keyboard extension (ios/HEXKeyboard, through ios/HEXShared) and the Android IME (android/app). Each OS keeps its own layer on top: views, touches, the text field, haptics, dictation, storage, and anything only the OS can answer (Apple's spelling checker, the system lexicon).

  • core/ (hex-keyboard-core): safe Rust. It never reads a clock, a file or the OS; the host passes in times, key geometry and data.
  • ffi/ (hex-keyboard-ffi, library hex_keyboard): the C interface in include/hex_keyboard.h (Swift imports it as HexKeyboardCore), and with the jni feature the Android entry points. All unsafe code is here; every entry point catches panics.

This is its own Cargo workspace with its own Cargo.lock and pinned toolchain (rust-toolchain.toml), so the desktop workspace never builds it.

Pieces move here one at a time from the Swift keyboard (ios/HEXShared/FullKeyboard), each proven identical to the Swift it replaces before iOS switches to it. Today: the typing model reader and Swipe typing's decoder. Tune those here, not in Swift.

Setup

Install rustup (rustup.rs or brew install rustup): in this folder it uses the toolchain, components and targets rust-toolchain.toml pins (rustup toolchain install fetches them ahead). iOS's build (ios/scripts/build-keyboard-core.sh, HEXKeyboard's first build phase), Xcode Cloud's ci_post_clone.sh and Android's debug build find it the same way (tools/rustup-env.sh): on PATH, in CARGO_HOME (by default ~/.cargo), or Homebrew's (opt/rustup/bin or bin under HOMEBREW_PREFIX), taking only a cargo that runs the pinned toolchain here. Android also needs cargo install cargo-ndk.

cd crates/keyboard
cargo test --all-features
cargo clippy --all-targets --all-features -- -D warnings

--all-features includes the jni feature, so the Android entry points (ffi/src/jni.rs) are checked too.

Tune Swipe typing here, in core/src/swipe: iOS's SwipeDecoder is a wrapper of it. The decoder was ported from the Swift one it replaced, and proven to read every swipe as it did (the same candidates in the same order, costs equal bit for bit) on 200,000 generated swipes, in a separate run and in one process, and on recorded FUTO swipes typed through the keyboard. Since then it computes exp, log, atan2, cos and hypot with the pure-Rust libm crate rather than each platform's own, so iOS and Android read every swipe alike, bit for bit; against Apple's libm, 2 of those 200,000 readings changed order (1-ulp near-ties) and about 2% of costs moved by 1 or 2 ulps. The committed core/tests/fixtures/swipe-parity.jsonl holds 800 of those swipes per language with the Swift decoder's words, order and scored, and the core's costs (its header's "costs"): core/tests/swipe_parity.rs checks the core against it bit for bit (and, with HEX_SWIPE_PARITY=<file>, a larger export; on one the Swift decoder recorded, changed costs are counted rather than failed), iOS's SwipeFixtureTests checks the shipped path through the C interface, and Android's tests the JNI path on the host and on an emulator. A change that reads any swipe differently fails both: if it's intended, refresh the fixture from the app's decoder (the simulator writes to the Mac's /tmp) and commit it with the change:

cd ios && xcodegen generate
TEST_RUNNER_HEX_SWIPE_PARITY_OUT=/tmp/swipe-parity.jsonl TEST_RUNNER_HEX_SWIPE_PARITY_COUNT=800 \
  xcodebuild test -project HEXMobile.xcodeproj -scheme HEXMobile \
  -destination 'platform=iOS Simulator,name=<simulator>' \
  -only-testing:HEXMobileTests/SwipeParityExportTests
cp /tmp/swipe-parity.jsonl ../crates/keyboard/core/tests/fixtures/swipe-parity.jsonl
cd ../crates/keyboard && cargo test --release --test swipe_parity -- --nocapture

TEST_RUNNER_HEX_SWIPE_PARITY_TIMING=1 adds each read's time; a larger count makes a larger file for HEX_SWIPE_PARITY.

What a swipe types is the core's too (core/src/swipe/typing.rs): the space before the word and the capital it brings, the word's casing (caps lock, shift, a name the dictionary knows only capitalised, your own words' capitals, Auto-capitalization off), the strip's alternatives with text replacements, the rare-name ranking and the slid tap. The host answers what only it knows: whether the dictionary knows a word (a callback), your text replacements and words, and the field, shift and auto-capitalization state with the text before the cursor; which readings may be offered at all stays its filter. Ported from iOS's Swift and proven alike on 1,000,000 generated cases in one process, it's held to core/tests/fixtures/swipe-rules.jsonl (5,000 of those cases) by core/tests/swipe_rules.rs and iOS's SwipeRulesFixtureTests. Refresh it for an intended change the same way:

TEST_RUNNER_HEX_SWIPE_RULES_OUT=/tmp/swipe-rules.jsonl TEST_RUNNER_HEX_SWIPE_RULES_COUNT=5000 \
  xcodebuild test -project HEXMobile.xcodeproj -scheme HEXMobile \
  -destination 'platform=iOS Simulator,name=<simulator>' \
  -only-testing:HEXMobileTests/SwipeRulesFixtureTests/testExportSwipeRulesFixture
cp /tmp/swipe-rules.jsonl ../crates/keyboard/core/tests/fixtures/swipe-rules.jsonl