Skip to content

Engine

Platform

Platform provides window creation, event delivery, and frame-based input state. Its window interface has a GLFW backend for native windows and a null_backend for headless execution. Both produce the same event stream, which input converts into an input_state snapshot.

Window and input ownership remain with the caller. The module supplies the operating-system boundary; rendering and application behaviour consume its interfaces. For application setup, see Windows and events and Input.

Dependency Graph

Platform module dependencies

Headers

File Description Provides
backend.h Which implementation sits behind a platform::window: the real GLFW 3.4 window or the virtual null window used by every headless test and server build.
button_action.h What happened to a key or button.
event.h The event stream a window produces once per frame. close_event
content_scale_changed_event
cursor_enter_event
drop_files_event
focus_event
framebuffer_resized_event
gamepad_axis_event
gamepad_button_event
gamepad_connection_event
key_event
minimise_event
mouse_button_event
mouse_move_event
mouse_scroll_event
text_event
gamepad.h Gamepad buttons and axes: the SDL game controller layout GLFW exposes as GLFW_GAMEPAD_BUTTON_* / GLFW_GAMEPAD_AXIS_*.
input.h The backend-independent input accumulator. input
input_stats
input_state.h Everything a frame may ask about input. gamepad_state
input_state
key_bitset
key.h Keyboard keys and modifier flags.
log_category.h The platform module's log category ("platform"), defined in engine/src/platform/platform.cpp.
monitor.h Monitors as window::monitors() reports them. monitor_info
video_mode
mouse_button.h Mouse buttons: GLFW_MOUSE_BUTTON_* numbering, eight buttons.
native_handle.h Opaque per-OS window handles for the rhi: the D3D12 swapchain needs the HWND, MoltenVK the NSWindow. native_window_handle
null_backend.h The virtual window: no OS call of any kind, constructible without a display server, deterministic. null_backend
null_backend::drop_range
platform.h Umbrella header of the platform module: window, events, input, the backend queries and the per-user folders.
platform_error.h Error codes of the platform module.
user_directories.h Per-user folders of the operating system.
window.h The abstract window. window
window_desc

Breakdown

Ownership and backend selection

create_window() validates a window_desc, enforces one live window per process, and returns an owning std::unique_ptr to window. Destroying that object releases the backend's window resources. Description validation rejects an unknown backend, an overlong title, and zero windowed dimensions; backend creation reports unavailable builds, initialisation failures, and invalid monitor selection.

engine/src/platform/platform.cpp implements the factory and backend queries. The null backend is always built. CONJURE_PLATFORM_GLFW adds the GLFW implementation and its private library dependency; public platform interfaces depend on Core. The generated dependency graph shows the module's direct dependencies and consumers.

GLFW implementation details stay under engine/src/platform/glfw/. glfw_context manages process-wide initialisation, error reporting, and joystick/monitor callback dispatch. glfw_window owns the native window and its event buffers. The null backend keeps equivalent state in memory and exposes simulation operations for tests and headless tools.

Event buffering and lifetime

window::pump_events() returns the next event batch as a span. Each backend separates pending events from the delivered batch. Events raised synchronously by a window setter remain pending until the next pump; pumping must not discard them before delivery.

Both backends coalesce framebuffer changes into one framebuffer_resized_event at the end of the batch. A minimised window reports a zero-sized framebuffer. Window size uses logical coordinates, while framebuffer size uses drawable pixels; their ratio is the content scale.

Warning

The delivered event span and the paths inside a drop_files_event refer to backend-owned storage. Consume or copy them before the next call to pump_events().

The backends retain vector capacity between pumps. Dropped file paths use owned strings, with delivered spans repaired after the buffers are swapped. This separates event lifetime from callback timing and avoids steady-state event-buffer allocations when retained capacity is sufficient.

Input accumulation and publication

input::apply() handles input events from a pumped batch. input::inject() uses the same internal state machine for tests and editor input. A press sets the held and pressed states; a release clears held and sets released. Repeated presses, duplicate releases, and invalid transitions are counted instead of creating new edges. Losing focus releases held input.

input::end_frame() publishes the accumulated state, increments its frame index, and clears transient edges, mouse movement, scroll, and text in the pending state. snapshot() returns the last published snapshot.

The following helper publishes input from a batch already obtained by the caller. The caller also dispatches window events from that same batch, including close requests and framebuffer changes.

#include "conjure/platform/input.h"
#include <span>

void publish_input(
    conjure::platform::input &accumulator,
    std::span<const conjure::platform::event> events) {
    for (const auto &value : events) {
        static_cast<void>(accumulator.apply(value));
    }
    accumulator.end_frame();
}

Important

Apply the complete input batch before publishing it. Consumers that need pressed/released edges should read the published input_state before the next publication.

Native integration and backend differences

native_window_handle exposes the platform handle needed by the rendering backend without putting operating-system types in the public interface. GLFW-specific native handle access is isolated in glfw_native.cpp. Vulkan surface creation is isolated in glfw_vulkan.cpp; the null backend reports that Vulkan presentation is unsupported.

GLFW fullscreen support uses the selected monitor's current mode. Leaving fullscreen restores the last confirmed windowed size and position. A window created fullscreen without an explicit position is centred on the monitor work area when it first returns to windowed mode.

Cursor capture, raw mouse motion, window positioning, clipboard access, and per-window icons depend on backend and display-server support. Operations that return a result report unavailable features explicitly. For example, Wayland does not provide the same positioning and cursor-warp controls as Win32 or X11.

Note

The null backend has no display-server dependency. It stores clipboard, monitor, focus, visibility, and cursor state locally, making event and input tests deterministic without opening a native window.

The null backend's virtual monitor starts with the window's content scale. simulate_content_scale() changes both, queuing a content_scale_changed_event and, when the pixel size changes, the coalesced resize. simulate_monitor_scale() changes only the monitor's monitor_info::contentScale and queues nothing, as on a Windows or X11 monitor set to 150 %, whose framebuffer is as large as the window. Tests use it for code that reads the monitor's UI scale rather than the framebuffer ratio.

Close requests

should_close() is a flag on the window. request_close() sets it without queuing an event, and cancel_close() clears it. On GLFW the flag is glfwWindowShouldClose: GLFW sets it before the close callback queues the close_event, so a cancel_close() withdraws the request but leaves that event to be delivered. The null backend mirrors this: simulate_close() sets the flag and queues the event, and cancel_close() clears the flag only. The engine's frame loop uses cancel_close() when a layer keeps the window open; see Engine.

Per-user folders

user_config_directory() names %APPDATA%/Conjure on Windows, $XDG_CONFIG_HOME/conjure (or <home>/.config/conjure) on Linux and <home>/Library/Application Support/Conjure on macOS. It is split in two so that every system's rule is tested on every host, where tests may not read the environment:

Source Role
user_directories_windows.cpp Reads APPDATA and USERPROFILE through GetEnvironmentVariableW, so a user name outside the active code page survives, and ignores a value that is not well-formed UTF-16.
user_directories_posix.cpp Reads XDG_CONFIG_HOME (Linux only) and HOME with getenv, and asks the user database (getpwuid_r) only when HOME is not absolute, since that lookup may go to a network name service. macOS differs inside this file, behind CONJURE_PLATFORM_MACOS, only in the convention it picks.
user_directories_internal.h, user_directories.cpp The private resolver: detail::resolve_user_config_directory(convention, environment) applies the rule to the values gathered, and detail::is_absolute_for(convention, path) judges "absolute" by the target system's rules.

engine/src/platform/CMakeLists.txt compiles the Windows or the POSIX file under if(WIN32), and always the resolver. The resolver compares characters instead of calling std::filesystem::path::is_absolute(), whose answer belongs to the host: on Windows /home/user has no root name, and on Linux C:/Users is relative. For Windows a path is absolute with a drive letter, a colon and a separator, or as a UNC or device path (\\server\share, \\?\C:\); for Linux and macOS it starts with /.

The rule, in order: Windows takes APPDATA when absolute, then USERPROFILE/AppData/Roaming. Linux takes an absolute XDG_CONFIG_HOME (a relative value is ignored, as the XDG Base Directory specification asks), then <home>/.config. macOS ignores XDG_CONFIG_HOME. <home> is HOME when absolute, otherwise the user database's entry. The folder name is Conjure on Windows and macOS and conjure under .config. The result is lexically_normal(); nothing is created on disk. When no value the rule needs is absolute, the call fails with core::error_code::NOT_FOUND, a generic code, so the platform_error enumeration and its name table stay unchanged.

Threading Model

Important

Create, destroy, query, and modify windows on the main thread. Input mutation, end_frame() and user_config_directory(), which reads the process environment that setenv on another thread could change, also belong to the main thread. These paths assert the main-thread requirement in Debug and Development builds.

GLFW callbacks execute while the main thread pumps events or performs an operation that raises them synchronously. They append pending events or record state changes; application code consumes the delivered batch after pumping.

snapshot() may be read by other threads between publications. Coordinate readers with publication, and do not retain its reference across the next end_frame().

Extension Points

Implement another backend through window, register its backend kind, and connect it to the factory and backend queries in platform.cpp. Keep native headers and handles inside the backend implementation. Preserve the event lifetimes and resize behaviour described in Event buffering and lifetime.

Extend the shared backend conformance tests when adding behaviour. Add headless scenarios through null_backend simulation methods so input and frame handling can be checked without an interactive display.

Testing

tests/platform/CMakeLists.txt registers the public-header tests and topic suites. The main behaviour tests are:

Suite Coverage
test_backend_conformance.cpp Shared window behaviour across null and GLFW backends, including resize delivery, fullscreen restoration, clipboard, icons, and request_close / cancel_close.
test_input.cpp Input transitions, publication, injection, focus loss, and input diagnostics.
test_null_backend.cpp Headless window state and simulated events, including simulate_monitor_scale and a cancel_close that leaves the queued close_event.
test_user_directories.cpp Each system's per-user folder rule through the private resolver, with the environment given as values: the APPDATA / USERPROFILE fallback, UNC folders, XDG_CONFIG_HOME absolute, relative or unset, the user database's home, macOS ignoring XDG_CONFIG_HOME, user names outside ASCII, lexical normalisation, NOT_FOUND, and is_absolute_for on every host; then the shape of the host's own answer.
test_window_events.cpp Frame handling of resize, minimise, focus, close, and dropped-file events.

Run the platform target and its tests from the repository root:

python tools/build.py --target conjure_platform_tests --test -R platform

Tip

The normal suite runs headlessly. To exercise GLFW conformance on a machine with a display, set CONJURE_TEST_WINDOW=1 before running the test executable with -ts="platform/backend_conformance". Those window tests skip when real-window testing is not enabled.