Back to Projects
TUIX v0.3Beta

Last Updated: 2026-05-20

Internal API Reference

This section documents the internal modules that bridge between the Python API and the compiled C core. These are not part of the public API — they are implementation details that may change between versions.

Internal Use OnlyThe modules documented here (tuix.core._lib, tuix.core._structs, tuix.core._tuix_cy) are internal. Do not depend on them in application code. Use the public API modules (engine, scenes, objects, buffers, input, builders, registry) instead.
v0.3 API CleanupPublic raw buffer pointer getter APIs were removed from the recommended public surface in favor of lock-safe snapshot APIs (buffer and object snapshots by UID).

tuix.core._lib — C Bridge

The _lib module loads the compiled Cython extension (_tuix_cy) or falls back to loading the shared library via ctypes.CDLL. It provides wrapper functions that call into native C code.

Module Variables

VariableTypeDescription
_modmodule | NoneThe loaded Cython module (_tuix_cy), or None if unavailable
libCDLL | NoneThe loaded C shared library handle, or None

Functions

FunctionDescription
get_func(name, restype, argtypes)Look up a native function by name in the Cython module or CDLL. Returns a callable.
tuix_render_streaming(buf)Render a TuixFinalBuffer to the terminal using the streaming renderer.
tuix_create_native_buffer(py_buf)Create a native buffer from a Python buffer object. Returns an integer handle.
tuix_free_native_buffer(handle)Free a native buffer by handle.
tuix_apply_patch_and_render(handle, patch)Apply a binary patch to a native buffer and render the result. Returns ANSI bytes.
make_patch_bytes(updates)Convert a list of (index, sym, fg, bg) tuples to 20-byte binary patch records.

Binary Patch Format

Each patch record is 20 bytes:

OffsetSizeField
04 bytesPixel index (little-endian uint32)
48 bytesUTF-8 symbol (null-padded)
123 bytesForeground RGB
153 bytesBackground RGB
181 byteFlags (bold, italic, underline, dim)
191 byteReserved

tuix.core._structs — C Structure Bindings

The _structs module defines Python ctypes.Structure classes that mirror the C structures used by the engine.

TuixRGBTuple

FieldTypeDescription
ruint8Red channel (0–255)
guint8Green channel (0–255)
buint8Blue channel (0–255)

TuixPixelStyles

FieldTypeDescription
fgTuixRGBTupleForeground color
bgTuixRGBTupleBackground color
custom_bguint8Has custom background color flag
custom_fguint8Has custom foreground color flag
bolduint8Bold text attribute
underlineduint8Underline text attribute
italicuint8Italic text attribute
dimuint8Dim text attribute

TuixPixel

FieldTypeDescription
symchar[8]UTF-8 character (up to 8 bytes)
stylesTuixPixelStylesPixel style attributes
q_fgTuixRGBTupleQuantized foreground (cache)
q_bgTuixRGBTupleQuantized background (cache)
q_cacheduint8Whether quantized cache is valid

TuixObject

FieldTypeDescription
uidintUnique object identifier (auto-assigned)
buildervoid*Pointer to the TuixBuilder that created this widget
statevoid*Pointer to per-widget opaque state
width_modfloatWidth as fraction of terminal width
height_modfloatHeight as fraction of terminal height
margin_top_modfloatTop margin as fraction of terminal height
margin_left_modfloatLeft margin as fraction of terminal width

TuixBuffer

FieldTypeDescription
objTuixObject*Pointer to the parent object
pixelsTuixPixel*Pointer to the pixel buffer array
widthintCurrent resolved width in columns
heightintCurrent resolved height in rows
required_redrawintDirty flag — 1 if buffer needs rebuild
margin_leftintResolved left margin in columns
margin_topintResolved top margin in rows

TuixFinalBuffer

FieldTypeDescription
pixelsTuixPixel*Composited pixel array (terminal_width × terminal_height)
widthintWidth in columns
heightintHeight in rows
full_redrawintForce all pixels to be re-rendered

TuixInputSnapshot

FieldTypeDescription
term_xintTerminal width at snapshot time
term_yintTerminal height at snapshot time
mousevoid*Pointer to TuixMouseKey event data
keyboardvoid*Pointer to TuixKeyboardKey event data

TuixRegistry

FieldTypeDescription
scenesvoid*Pointer to TuixScenes container
subcyclesvoid*Pointer to TuixSubcycles container
buildersvoid*Pointer to TuixBuilders container
current_scene_namechar*Pointer to the active scene name string
next_uidintNext UID to assign (auto-incrementing counter)
terminal_widthintCurrent terminal width
terminal_heightintCurrent terminal height
terminal_width_oldintPrevious frame's terminal width
terminal_height_oldintPrevious frame's terminal height

tuix.core._tuix_cy — Cython Extension

The _tuix_cy module is a Cython extension that wraps all C function calls. It is compiled from _tuix_cy.pyx and linked against the C source files. The Python API modules call _tuix_cy functions when available, falling back to ctypes when the Cython module is not loaded.

The Cython layer is a thin wrapper — it converts Python types to C types, calls the native function, and converts the result back. It does not add additional logic beyond type marshalling.

C Engine Architecture

The compiled C core is organized into these subsystems:

SubsystemFilesPurpose
Main Loopmain.c, main.hPer-frame pipeline orchestration
Init/Shutdowninit.c, init.hRegistry setup and teardown
Renderingrendering.c, rendering.hDelta renderer with color quantization
Compositorcompositor/Scene buffer compositing with clipping
Geometrycompositor/geometry_resolver.cProportional layout resolution
Buffer Managerbuffer_manager.cBuffer allocation, lookup, and freeing
Object Managerobject_manager.cWidget creation with builder dispatch
Scene Managerbuffers.cScene CRUD and buffer array management
Builderscontent_builder/Widget type registration and routing
Widget Implscontent_builder/builders/Canvas, Choice, Input, Progressbar C code
Inputinput/Platform-specific keyboard and mouse capture
Subcyclessubcycles/Internal control objects for per-frame handler execution
Batch Executorbatch_executor.cBinary command buffer parser (internal optimization)

Optional C Modules

The source distribution includes modules not compiled into the default build. Advanced users can include them by modifying setup.py:

ModuleFileDescription
Halfblock Rendererrendering_halfblocks.c2× vertical resolution using Unicode halfblock characters (▀). Pairs two pixel rows into one terminal row.
Cache Managercache_manager.cExperimental caching module; v0.3 production strategy is data-first scene stats and cold-scene compaction.