Skip to content

Headless Mode

rizuki edited this page Nov 30, 2026 · 7 revisions

Headless mode

Build without raylib to run layout code in CI, tests, benchmarks, or tools that do not need a display.

Building headless

cmake -S . -B build -DCCOMPOSE_BACKEND_RAYLIB=OFF
cmake --build build

This configuration:

  • defines CCOMPOSE_NO_BACKEND on the ccompose target (public, so it propagates to consumers)
  • skips the ccompose_demo executable
  • does not link against raylib

The headless library uses src/ccompose.c with the backend code excluded by preprocessor guards.

The project also builds a separate ccompose_headless static library when the raylib backend is enabled. Link tests to that target to keep them headless regardless of the main build configuration.

What changes at runtime

Function Raylib backend Headless
CC_SetWindow sets window size/title no-op
CC_SetWindowFlags sets raylib flags no-op
CC_SetBackground sets clear color (drawn each frame) stores the value (no draw)
CC_LoadFont loads font, returns slot id always returns -1
CC_InitFont sets global font id + default size works: no font loads, but both values flow into emitted text commands
CC_GetGlobalFontId returns configured id returns configured id (0 unless CC_InitFont set one)
CC_GetGlobalFontSize returns configured size returns configured size (defaults to 16)
CC_SetGlobalFontColor stores global text color stores the value (still flows into emitted text commands)
CC_GetGlobalFontColor returns stored color returns stored color (defaults to white)
CC_Running !WindowShouldClose() always true
CC_Begin reads mouse/keyboard/wheel into Clay just calls Clay_BeginLayout()
CC_End renders via Clay_Raylib_Render returns commands without drawing
CC__MousePressedThisFrame IsMouseButtonPressed(LEFT) always false
LinearProgress lays out a track and fill, infinite mode animates off GetTime() works: same layout and rectangle commands, clock fixed at 0 so an infinite bar shows only its track
CircularProgress paints a ring with raylib reserves its .sizing box, draws nothing
CC_SetViewport overridden by CC_Begin from window size used: drives Clay's layout dimensions

CC_Running() always returns true in headless mode. Run a bounded loop or a single frame.

Clay emits configured colors and font ids into the CC_RenderCommandArray from CC_End() even without a renderer. Tests can inspect those values in the returned commands.

Minimal headless program

#include "ccompose.h"
#include <assert.h>

int main(void) {
    CC_SetViewport(800.0f, 600.0f);   // required: no window to read size from
    CC_Init();

    CC_Begin();
    Column("Root",
           .layout = { .sizing = { Grow(), Grow() }, .padding = PadAll(8) },
           .backgroundColor = Color(24, 24, 24, 255)) {
        Text("headless",
             .textColor = Color(255,255,255,255),
             .fontSize  = 16);
    }
    CC_RenderCommandArray commands = CC_End();

    // Inspect commands: they describe the layout even though nothing was drawn.
    int rects = 0, texts = 0;
    for (int32_t i = 0; i < commands.length; ++i) {
        CC_RenderCommand *cmd = Clay_RenderCommandArray_Get(&commands, i);
        if (cmd->commandType == CLAY_RENDER_COMMAND_TYPE_RECTANGLE) ++rects;
        if (cmd->commandType == CLAY_RENDER_COMMAND_TYPE_TEXT)      ++texts;
    }
    assert(rects >= 1);
    assert(texts >= 1);

    CC_Shutdown();
    return 0;
}

Text measurement in headless

Headless builds use a fallback text measurement function that estimates width as font_size * 0.5 * char_count. It uses 14.0 when .fontSize is 0. This gives deterministic test layouts, but the widths differ from rendered text.

If you need accurate measurement in headless mode (e.g. snapshot tests against rendered baselines), install your own measure function after CC_Init():

static Clay_Dimensions my_measure(Clay_StringSlice text,
                                  Clay_TextElementConfig *cfg,
                                  void *user) {
    // ... your own font metrics ...
    return (Clay_Dimensions){ width, height };
}

CC_Init();
Clay_SetMeasureTextFunction(my_measure, NULL);

Pointer input in tests

Since the raylib backend isn't providing mouse state, CC_Begin() doesn't touch the pointer. To drive hover/click tests, call Clay_SetPointerState() directly between CC_Init and CC_Begin:

CC_Init();

// First frame: just to establish layout
CC_Begin();
build_ui();
CC_End();

// Push the mouse cursor over "Save" and click
Clay_SetPointerState((Clay_Vector2){ 100.0f, 200.0f }, /*is_down=*/true);

// Second frame: now CC_Hovered("Save") will be true
CC_Begin();
build_ui();
assert(CC_Hovered("Save"));    // Uses the previous frame's layout
CC_End();

CC_Clicked always returns false in headless mode because CC__MousePressedThisFrame() has no mouse input source. For simulated presses, combine Clay_PointerOver(CLAY_ID(...)) with your own press state, or use Clay_OnHover with a pointer-data state check.

CTest example

Use tests/smoke_test.c as a headless example. Register a test in CMakeLists.txt:

if(CCOMPOSE_BUILD_TESTS)
    enable_testing()

    add_library(ccompose_headless STATIC src/ccompose.c)
    target_compile_features(ccompose_headless PUBLIC c_std_${CCOMPOSE_C_STANDARD})
    target_include_directories(ccompose_headless PUBLIC
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>)
    target_compile_definitions(ccompose_headless PUBLIC CCOMPOSE_NO_BACKEND)

    add_executable(my_test tests/my_test.c)
    target_link_libraries(my_test PRIVATE ccompose_headless)
    add_test(NAME my_test COMMAND my_test)
endif()

Run with ctest --test-dir build --output-on-failure.

Uses

  • Run CI tests without X11 or GL dependencies.
  • Measure Clay's layout cost without rendering.
  • Snapshot a CC_RenderCommandArray and compare it across commits.
  • Integrate a custom renderer by iterating the returned command array.

Clone this wiki locally