# theees/crossink architecture

> Architecture diagram of the GitHub repository theees/crossink, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/theees/crossink
- Repository: https://github.com/theees/crossink

## Overview

#### Purpose and Entrypoints
CrossInk is C++ firmware for Xteink X3/X4 e-ink readers, targeting ESP32-C3 and emphasizing custom typography, EPUB/TXT/XTC reading, and lightweight reading statistics. PlatformIO builds hardware variants; `src/main.cpp` is the firmware entry point. A separate SDL2-oriented simulator integration is exposed through `include/SimulatorDisplay.h` and `src/simulator/SimulatorSmokeTest.cpp`.

#### Application Lifecycle and UI
The application is organized as stack-like activities managed centrally by `src/activities/ActivityManager.cpp` and the `Activity` abstraction in `src/activities/Activity.h`. Boot/sleep, home/file browsing, settings, network, and reader flows are distinct activity families under `src/activities/`. Rendering ownership is protected at the activity boundary by `src/activities/RenderLock.h`, while reusable visual themes live in `src/components/themes/BaseTheme.cpp`.

#### Reading Pipeline
Reader activities select a format-specific reader: EPUB, plain text, or XTC, represented by `src/activities/reader/EpubReaderActivity.cpp`, `src/activities/reader/TxtReaderActivity.cpp`, and `src/activities/reader/XtcReaderActivity.cpp`. EPUB processing opens ZIP-contained package data, parses container/OPF/TOC and chapter HTML/CSS, then produces pages and blocks for display. Core boundaries are `lib/Epub/Epub.cpp`, `lib/Epub/Epub/parsers/ContentOpfParser.cpp`, and `lib/Epub/Epub/Page.cpp`.

#### Text Layout, Fonts, and Display
EPUB text is represented as parsed text and styled blocks, then laid out with language-aware hyphenation and rendered through the graphics layer. Key components are `lib/Epub/Epub/ParsedText.cpp`, `lib/Epub/Epub/hyphenation/Hyphenator.cpp`, and `lib/GfxRenderer/GfxRenderer.cpp`. Font access is isolated in `lib/EpdFont/EpdFont.cpp`; built-in compressed font assets are compiled from `lib/EpdFont/builtinFonts/`, while SD-card font discovery/loading is handled by `lib/EpdFont/SdCardFontManager.cpp`. Hardware-facing display calls pass through `lib/hal/HalDisplay.h`.

#### Persistent Device and Book State
Settings, bookmarks, clippings, recent books, credentials, and reading state are persisted primarily on the SD card rather than retained in constrained ESP32 RAM. Main application stores include `src/CrossPointSettings.cpp`, `src/BookmarkStore.cpp`, and `src/RecentBooksStore.cpp`; generic persistence is provided by `lib/Serialization/PersistableStore.cpp`. Book indexing and file operations are separated into `lib/FileIndex/FileIndex.cpp` and `lib/FsHelpers/FsHelpers.cpp`. Memory-sensitive code uses dedicated arena/scratch abstractions in `lib/Memory/Arena.h` and `lib/Memory/ScratchWorkspace.cpp`.

#### Networking, Transfer, and Synchronization
Wi-Fi-facing activities coordinate device network modes and user flows, including `src/activities/network/CrossPointWebServerActivity.cpp` and `src/activities/network/CalibreConnectActivity.cpp`. The firmware provides an embedded web server and WebDAV-oriented file transfer through `src/network/CrossPointWebServer.cpp` and `src/network/WebDAVHandler.cpp`; its static UI is sourced from `web/pages/`. OTA firmware downloads/flashing are separated into `src/network/OtaUpdater.cpp` and `src/network/FirmwareFlasher.cpp`. External content/sync boundaries include OPDS parsing (`lib/OpdsParser/OpdsParser.cpp`) and KOReader progress synchronization (`lib/KOReaderSync/KOReaderSyncClient.cpp`).

#### Platform Boundaries and Dependencies
Platform-specific concerns are behind HAL interfaces for storage, GPIO, SPI, power, clock, tilt sensing, and system functions, e.g. `lib/hal/HalStorage.h`, `lib/hal/HalPowerManager.h`, and `lib/hal/HalSystem.h`. The codebase embeds XML and deflate implementations in `lib/expat/` and `lib/uzlib/`, which support EPUB/XML and compressed content handling. Build partitioning and target configuration are defined by `platformio.ini` and `partitions.csv`.

Main entry points: [`src/activities/ActivityManager.cpp`](https://github.com/theees/crossink/blob/main/src/activities/ActivityManager.cpp) (Activity manager), [`lib/hal/HalStorage.h`](https://github.com/theees/crossink/blob/main/lib/hal/HalStorage.h) (Storage HAL), [`src/activities/Activity.h`](https://github.com/theees/crossink/blob/main/src/activities/Activity.h) (Activity boundary), [`src/activities/reader/EpubReaderActivity.cpp`](https://github.com/theees/crossink/blob/main/src/activities/reader/EpubReaderActivity.cpp) (Format reader activities)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_app["Application"]
  node_main["Firmware entry<br/>C++ entrypoint<br/>[main.cpp]"]
  node_activities["Activity manager<br/>UI coordinator"]
  node_activity["Activity boundary<br/>UI abstraction<br/>[Activity.h]"]
  node_render_lock["Render lock<br/>render guard<br/>[RenderLock.h]"]
  node_themes["Base theme<br/>visual theme<br/>[BaseTheme.cpp]"]
end

subgraph group_reading["Reading &amp; Rendering"]
  node_reader_activities["Format reader activities<br/>reader UI"]
  node_epub["EPUB engine<br/>document pipeline<br/>[Epub.cpp]"]
  node_opf_parser["OPF and TOC parser<br/>EPUB parser"]
  node_parsed_text["Parsed text blocks<br/>styled content<br/>[ParsedText.cpp]"]
  node_page_layout["Page layout<br/>pagination<br/>[Page.cpp]"]
  node_hyphenator["Hyphenator<br/>text layout<br/>[Hyphenator.cpp]"]
  node_gfx["Graphics renderer<br/>rendering<br/>[GfxRenderer.cpp]"]
  node_fonts["Font subsystem<br/>font access<br/>[EpdFont.cpp]"]
end

subgraph group_state["Device State"]
  node_stores[("Application stores<br/>persistent state")]
  node_persist["Persistable store<br/>persistence framework"]
  node_file_index["Book index<br/>library indexing<br/>[FileIndex.cpp]"]
end

subgraph group_network["Network &amp; Sync"]
  node_web_server["Embedded web server<br/>device transfer"]
  node_webdav["WebDAV handler<br/>file transfer<br/>[WebDAVHandler.cpp]"]
  node_ota["OTA updater<br/>firmware update<br/>[OtaUpdater.cpp]"]
  node_flasher["Firmware flasher<br/>firmware update"]
  node_external_sync["OPDS and KOReader sync<br/>external integration"]
end

subgraph group_platform["Platform &amp; Build"]
  node_display{{"Display HAL<br/>hardware interface<br/>[HalDisplay.h]"}}
  node_storage{{"Storage HAL<br/>hardware interface<br/>[HalStorage.h]"}}
  node_build["PlatformIO targets<br/>build configuration<br/>[platformio.ini]"]
end

node_main -->|"starts"| node_activities
node_activities -->|"manages"| node_activity
node_activity -->|"guards rendering"| node_render_lock
node_activity -->|"applies"| node_themes
node_activities -->|"pushes"| node_reader_activities
node_reader_activities -->|"opens EPUB"| node_epub
node_epub -->|"parses package"| node_opf_parser
node_epub -->|"parses chapters"| node_parsed_text
node_parsed_text -->|"lays out"| node_page_layout
node_hyphenator -->|"breaks words"| node_page_layout
node_page_layout -->|"renders pages"| node_gfx
node_fonts -->|"supplies glyphs"| node_gfx
node_gfx -->|"draws through"| node_display
node_activities -->|"reads and updates"| node_stores
node_stores -->|"serializes with"| node_persist
node_persist -->|"persists to"| node_storage
node_file_index -->|"indexes files"| node_storage
node_reader_activities -->|"selects books"| node_file_index
node_activities -->|"network activity controls"| node_web_server
node_web_server -->|"routes transfers"| node_webdav
node_webdav -->|"reads and writes"| node_storage
node_ota -->|"hands off image"| node_flasher
node_flasher -.->|"uses firmware storage"| node_storage
node_activities -->|"initiates sync"| node_external_sync
node_external_sync -->|"updates progress"| node_stores
node_build -.->|"builds target"| node_main

click node_main "https://github.com/theees/crossink/blob/main/src/main.cpp"
click node_activities "https://github.com/theees/crossink/blob/main/src/activities/ActivityManager.cpp"
click node_activity "https://github.com/theees/crossink/blob/main/src/activities/Activity.h"
click node_render_lock "https://github.com/theees/crossink/blob/main/src/activities/RenderLock.h"
click node_themes "https://github.com/theees/crossink/blob/main/src/components/themes/BaseTheme.cpp"
click node_reader_activities "https://github.com/theees/crossink/blob/main/src/activities/reader/EpubReaderActivity.cpp"
click node_epub "https://github.com/theees/crossink/blob/main/lib/Epub/Epub.cpp"
click node_opf_parser "https://github.com/theees/crossink/blob/main/lib/Epub/Epub/parsers/ContentOpfParser.cpp"
click node_parsed_text "https://github.com/theees/crossink/blob/main/lib/Epub/Epub/ParsedText.cpp"
click node_page_layout "https://github.com/theees/crossink/blob/main/lib/Epub/Epub/Page.cpp"
click node_hyphenator "https://github.com/theees/crossink/blob/main/lib/Epub/Epub/hyphenation/Hyphenator.cpp"
click node_gfx "https://github.com/theees/crossink/blob/main/lib/GfxRenderer/GfxRenderer.cpp"
click node_fonts "https://github.com/theees/crossink/blob/main/lib/EpdFont/EpdFont.cpp"
click node_display "https://github.com/theees/crossink/blob/main/lib/hal/HalDisplay.h"
click node_stores "https://github.com/theees/crossink/blob/main/src/CrossPointSettings.cpp"
click node_persist "https://github.com/theees/crossink/blob/main/lib/Serialization/PersistableStore.cpp"
click node_file_index "https://github.com/theees/crossink/blob/main/lib/FileIndex/FileIndex.cpp"
click node_storage "https://github.com/theees/crossink/blob/main/lib/hal/HalStorage.h"
click node_web_server "https://github.com/theees/crossink/blob/main/src/network/CrossPointWebServer.cpp"
click node_webdav "https://github.com/theees/crossink/blob/main/src/network/WebDAVHandler.cpp"
click node_ota "https://github.com/theees/crossink/blob/main/src/network/OtaUpdater.cpp"
click node_flasher "https://github.com/theees/crossink/blob/main/src/network/FirmwareFlasher.cpp"
click node_external_sync "https://github.com/theees/crossink/blob/main/lib/KOReaderSync/KOReaderSyncClient.cpp"
click node_build "https://github.com/theees/crossink/blob/main/platformio.ini"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_main,node_activities,node_activity,node_render_lock,node_themes toneBlue
class node_reader_activities,node_epub,node_opf_parser,node_parsed_text,node_page_layout,node_hyphenator,node_gfx,node_fonts toneAmber
class node_stores,node_persist,node_file_index toneMint
class node_web_server,node_webdav,node_ota,node_flasher,node_external_sync toneRose
class node_display,node_storage,node_build toneIndigo
```

## Components

### Application

Firmware lifecycle and activity-driven UI.

- [Firmware entry](https://github.com/theees/crossink/blob/main/src/main.cpp): C++ entrypoint · `src/main.cpp`
- [Activity manager](https://github.com/theees/crossink/blob/main/src/activities/ActivityManager.cpp): UI coordinator · Manages stack-like application activities. · `src/activities/ActivityManager.cpp`
- [Activity boundary](https://github.com/theees/crossink/blob/main/src/activities/Activity.h): UI abstraction · Owns activity lifecycle and rendering boundary. · `src/activities/Activity.h`
- [Render lock](https://github.com/theees/crossink/blob/main/src/activities/RenderLock.h): render guard · `src/activities/RenderLock.h`
- [Base theme](https://github.com/theees/crossink/blob/main/src/components/themes/BaseTheme.cpp): visual theme · `src/components/themes/BaseTheme.cpp`

### Reading & Rendering

Format readers, EPUB layout, fonts, and display output.

- [Format reader activities](https://github.com/theees/crossink/blob/main/src/activities/reader/EpubReaderActivity.cpp): reader UI · Select EPUB, TXT, or XTC reading flows. · `src/activities/reader/EpubReaderActivity.cpp`
- [EPUB engine](https://github.com/theees/crossink/blob/main/lib/Epub/Epub.cpp): document pipeline · Opens ZIP-contained EPUB package content. · `lib/Epub/Epub.cpp`
- [OPF and TOC parser](https://github.com/theees/crossink/blob/main/lib/Epub/Epub/parsers/ContentOpfParser.cpp): EPUB parser · `lib/Epub/Epub/parsers/ContentOpfParser.cpp`
- [Parsed text blocks](https://github.com/theees/crossink/blob/main/lib/Epub/Epub/ParsedText.cpp): styled content · Parsed HTML/CSS text representation for layout. · `lib/Epub/Epub/ParsedText.cpp`
- [Page layout](https://github.com/theees/crossink/blob/main/lib/Epub/Epub/Page.cpp): pagination · `lib/Epub/Epub/Page.cpp`
- [Hyphenator](https://github.com/theees/crossink/blob/main/lib/Epub/Epub/hyphenation/Hyphenator.cpp): text layout · Provides language-aware line breaking. · `lib/Epub/Epub/hyphenation/Hyphenator.cpp`
- [Graphics renderer](https://github.com/theees/crossink/blob/main/lib/GfxRenderer/GfxRenderer.cpp): rendering · `lib/GfxRenderer/GfxRenderer.cpp`
- [Font subsystem](https://github.com/theees/crossink/blob/main/lib/EpdFont/EpdFont.cpp): font access · Uses built-in assets and SD-discovered fonts. · `lib/EpdFont/EpdFont.cpp`

### Device State

SD-backed settings and book state.

- [Application stores](https://github.com/theees/crossink/blob/main/src/CrossPointSettings.cpp): persistent state · Settings, bookmarks, and recent-book state. · `src/CrossPointSettings.cpp`
- [Persistable store](https://github.com/theees/crossink/blob/main/lib/Serialization/PersistableStore.cpp): persistence framework · `lib/Serialization/PersistableStore.cpp`
- [Book index](https://github.com/theees/crossink/blob/main/lib/FileIndex/FileIndex.cpp): library indexing · `lib/FileIndex/FileIndex.cpp`

### Network & Sync

Device transfer, update, and external synchronization.

- [Embedded web server](https://github.com/theees/crossink/blob/main/src/network/CrossPointWebServer.cpp): device transfer · Serves the static UI and WebDAV transfer. · `src/network/CrossPointWebServer.cpp`
- [WebDAV handler](https://github.com/theees/crossink/blob/main/src/network/WebDAVHandler.cpp): file transfer · `src/network/WebDAVHandler.cpp`
- [OTA updater](https://github.com/theees/crossink/blob/main/src/network/OtaUpdater.cpp): firmware update · Downloads updates for flashing. · `src/network/OtaUpdater.cpp`
- [Firmware flasher](https://github.com/theees/crossink/blob/main/src/network/FirmwareFlasher.cpp): firmware update · `src/network/FirmwareFlasher.cpp`
- [OPDS and KOReader sync](https://github.com/theees/crossink/blob/main/lib/KOReaderSync/KOReaderSyncClient.cpp): external integration · Catalog parsing and reading-progress synchronization. · `lib/KOReaderSync/KOReaderSyncClient.cpp`

### Platform & Build

Hardware abstractions and target configuration.

- [Display HAL](https://github.com/theees/crossink/blob/main/lib/hal/HalDisplay.h): hardware interface · `lib/hal/HalDisplay.h`
- [Storage HAL](https://github.com/theees/crossink/blob/main/lib/hal/HalStorage.h): hardware interface · SD-card storage boundary. · `lib/hal/HalStorage.h`
- [PlatformIO targets](https://github.com/theees/crossink/blob/main/platformio.ini): build configuration · Defines ESP32-C3 hardware variants and partitions. · `platformio.ini`

## Connections

- Firmware entry → Activity manager: starts
- Activity manager → Activity boundary: manages
- Activity boundary → Render lock: guards rendering
- Activity boundary → Base theme: applies
- Activity manager → Format reader activities: pushes
- Format reader activities → EPUB engine: opens EPUB
- EPUB engine → OPF and TOC parser: parses package
- EPUB engine → Parsed text blocks: parses chapters
- Parsed text blocks → Page layout: lays out
- Hyphenator → Page layout: breaks words
- Page layout → Graphics renderer: renders pages
- Font subsystem → Graphics renderer: supplies glyphs
- Graphics renderer → Display HAL: draws through
- Activity manager → Application stores: reads and updates
- Application stores → Persistable store: serializes with
- Persistable store → Storage HAL: persists to
- Book index → Storage HAL: indexes files
- Format reader activities → Book index: selects books
- Activity manager → Embedded web server: network activity controls
- Embedded web server → WebDAV handler: routes transfers
- WebDAV handler → Storage HAL: reads and writes
- OTA updater → Firmware flasher: hands off image
- Firmware flasher → Storage HAL: uses firmware storage
- Activity manager → OPDS and KOReader sync: initiates sync
- OPDS and KOReader sync → Application stores: updates progress
- PlatformIO targets → Firmware entry: builds target
