massive overhauls to documentation

This commit is contained in:
Peterino2 2026-09-20 01:58:19 -07:00
parent c4dc26d3f3
commit 7353322a74
7 changed files with 119 additions and 38 deletions

View File

@ -2,7 +2,7 @@
#define UHDK_H_INCLUDED
/*
* Uhdk desktop platform API — v0.1, macOS, Windows and Linux backends.
* Uhdk desktop platform API — 0.1.0 (alpha). macOS, Windows and Linux backends.
*
* Implementation: Zig. ABI: C. Header baseline: C99 (also usable from C++).
* No libc runtime objects or allocation contract.
@ -13,6 +13,9 @@
* - UHDK_ENABLE_OPENGL: include bundled GLAD types and session GL function-table declarations.
* - UHDK_ENABLE_NATIVE: declare the experimental borrowed native-handle API.
* - UHDK_PROFILE_BACKEND: quoted consumer instrumentation adapter header.
* - UHDK_SHARED: use shared-library symbol visibility declarations.
* - UHDK_BUILD: with UHDK_SHARED, export rather than import symbols on Windows;
* reserved for building the library itself.
* Presence enables each feature flag; leave it undefined to disable it.
* These select declarations only, not library backend/build features.
*
@ -54,24 +57,33 @@
*/
#include <stdint.h>
/* Optional, consumer-owned instrumentation; no exported UHDK ABI or dependency.
* Define UHDK_PROFILE_BACKEND to a quoted adapter header before including uhdk.h.
* That header implements the macros below. Without it, arguments are not evaluated.
* Names (zone, thread, pool) must be string literals or have static lifetime.
* C zones: UHDK_PROFILE_BEGIN(token, "name"); ... UHDK_PROFILE_END(token);
* End on the same thread, in reverse nesting order, including early exits.
* C++ may use UHDK_PROFILE_SCOPE("name") for automatic scope exit.
* Memory: report successful non-null allocations, then free BEFORE releasing
* storage. A successful resize is free + alloc; failed allocations emit nothing.
* A pointer must use the same pool name for allocation and free. These hooks do
* not allocate/free memory themselves, intercept malloc, or transfer ownership.
*/
/* Typed event payload convenience; see uhdk_event. */
/* Return event_ptr->data as const payload_type * only when event_ptr has
* expected_type and data_size is at least sizeof(payload_type); otherwise NULL.
* event_ptr may be evaluated more than once. See uhdk_event for the macro-free
* equivalent and payload lifetime rules. */
#define UHDK_EVENT_PAYLOAD(event_ptr, expected_type, payload_type) (((event_ptr)->type == (expected_type) && (event_ptr)->data_size >= sizeof(payload_type)) ? (const payload_type *)(event_ptr)->data : (const payload_type *)0)
/* Revision of the public C ABI described by this header. */
#define UHDK_ABI_VERSION 1u
/* Consumer instrumentation hooks. Defining UHDK_PROFILE_BACKEND includes that
* quoted header in place of the no-op definitions below. The adapter must define
* the same macros. Without an adapter, arguments are not evaluated. Names must
* be string literals or have static lifetime. Zones end on the same thread in
* reverse nesting order, including on early exits. These hooks report activity;
* they do not allocate, free, intercept allocation, or transfer ownership.
*
* UHDK_PROFILE_BEGIN(token, name): begin a named C zone and declare token.
* UHDK_PROFILE_END(token): end that zone on the same thread.
* UHDK_PROFILE_SCOPE(name): begin a C++ zone ending at scope exit.
* UHDK_PROFILE_FRAME(): mark a profiler frame boundary.
* UHDK_PROFILE_THREAD(name): assign the current thread a display name.
* UHDK_PROFILE_ALLOC(ptr, size, pool): report a successful non-NULL allocation.
* UHDK_PROFILE_FREE(ptr, pool): report a free before releasing its storage,
* using the same pool as its allocation. Report a successful resize as free then
* alloc; report nothing for a failed allocation.
*/
#ifdef UHDK_PROFILE_BACKEND
#include UHDK_PROFILE_BACKEND
#else
@ -84,6 +96,10 @@
#define UHDK_PROFILE_FREE(ptr, pool) ((void)0)
#endif
/* Public symbol decoration. Consumers may define UHDK_API before including this
* header to supply a platform-specific visibility attribute. The default exports
* while building a Windows shared library, imports while consuming one, uses
* default visibility for GNU-compatible shared builds, and is empty otherwise. */
#ifndef UHDK_API
#if defined(_WIN32) && defined(UHDK_SHARED)
#if defined(UHDK_BUILD)
@ -97,6 +113,8 @@
#define UHDK_API
#endif
#endif
/* Calling convention for every exported UHDK function. On 32-bit Windows this
* is __cdecl; it is empty on other targets. */
#if defined(_WIN32) && !defined(_WIN64)
#define UHDK_CALL __cdecl
#else
@ -107,6 +125,13 @@
extern "C" {
#endif
/* Library version 0.1.0 (alpha). Distinct from UHDK_ABI_VERSION. A 0.x library
* version is an alpha release.
*/
#define UHDK_VERSION_MAJOR 0u
#define UHDK_VERSION_MINOR 1u
#define UHDK_VERSION_PATCH 0u
typedef char uhdk_bool; /* Values are UHDK_FALSE (0) or UHDK_TRUE (1). */
enum {
UHDK_FALSE = 0,
@ -132,10 +157,34 @@ enum {
/* Opaque unless UHDK_ENABLE_OPENGL is defined; then this is the bundled
* GladGLContext function table. Owned by the session and valid until close. */
typedef struct GladGLContext uhdk_gl_functions;
/* Opaque handle for one running application. Create it with `uhdk_open` and
* destroy it with `uhdk_close`.
*
* The session owns the primary `uhdk_window` and, unless `no_opengl` is set, a
* current OpenGL context. The primary window cannot be destroyed separately.
* `uhdk_close` releases all session resources and invalidates child handles,
* including retained files and menus.
*
* Calls that take a session belong to OS main except `uhdk_wake`. Native events
* are pumped by `uhdk_next_frame`. Filesystem work runs on an internal I/O
* worker. See [Using UHDK](#using-uhdk).
*/
typedef struct uhdk_session uhdk_session;
/* Opaque native window owned by a session. The session owns its primary window;
* additional windows are created and destroyed explicitly. All window handles
* become invalid when the session closes. */
typedef struct uhdk_window uhdk_window;
/* Reference-counted file or directory identity, including any native access
* grant. Retain borrowed references before keeping them beyond their documented
* lifetime, release owned references, and do not use them after session close. */
typedef struct uhdk_file uhdk_file;
/* Opaque nonrecursive filesystem observation created asynchronously from a file
* or directory reference. Destroy it with uhdk_watch_destroy; session close also
* invalidates it. */
typedef struct uhdk_watch uhdk_watch;
/* Reference-counted native menu tree. Creation copies its item descriptions;
* attached menus are retained by UHDK. Release owned references and do not use
* them after session close. */
typedef struct uhdk_menu uhdk_menu;
/* Caller-chosen, nonzero. Unique in the session until REQUEST_DONE is delivered.
* Duplicates return BUSY, including while a completion is queued. A failed submit
@ -145,6 +194,7 @@ typedef struct uhdk_menu uhdk_menu;
*/
typedef uint64_t uhdk_request_tag;
/* UTF-8 byte span, not implicitly NUL-terminated. */
typedef struct uhdk_text {
const char *ptr;
uint64_t len;
@ -187,15 +237,20 @@ typedef struct uhdk_rect {
float height;
} uhdk_rect;
/* Tightly packed image for desktop services. pixels must contain at least
* width * height * 4 bytes; width and height must be nonzero and at most 4096. */
typedef struct uhdk_image {
uint32_t struct_size;
uint32_t width;
uint32_t height;
uint32_t reserved;
uint64_t stride_bytes;
uhdk_bytes pixels; /* Top-down, straight-alpha RGBA8 in sRGB. */
uhdk_bytes pixels; /* Tightly packed, top-down, straight-alpha RGBA8 in sRGB. */
} uhdk_image;
/* Native display backend. AUTO selects the compiled platform default.
* X11 is not available in 0.1.0; an explicit request returns
* UHDK_STATUS_UNSUPPORTED.
*/
typedef uint32_t uhdk_backend;
enum {
UHDK_BACKEND_AUTO = 0,
@ -329,10 +384,8 @@ typedef struct uhdk_window_metrics {
uhdk_bool suspended;
} uhdk_window_metrics;
/* Primary application API. All calls except wake belong to OS main. Native events
* are pumped by next_frame; filesystem work runs independently on I/O workers.
* The session owns its primary window and optional GL context. The primary
* window cannot be destroyed separately; close releases all session resources.
/* Wait policy for `uhdk_next_frame`. CONTINUOUS polls while the window is
* drawable; ON_DEMAND blocks until an event or wake.
*/
typedef uint32_t uhdk_render_mode;
enum {
@ -436,6 +489,8 @@ UHDK_API void UHDK_CALL uhdk_window_invalidate(uhdk_window *window); /* Coalesce
* offscreen buffers, textures, depth attachments and viewport/scissor state.
* Use get_gl for the bundled 3.3 API and get_gl_proc for additional procedures.
*/
/* Calling convention for OpenGL procedure pointers. This is __stdcall on
* Windows and empty on other targets. */
#if defined(_WIN32)
#define UHDK_GL_CALL __stdcall
#else
@ -811,38 +866,65 @@ typedef struct uhdk_preferences {
} uhdk_preferences;
UHDK_API uhdk_status UHDK_CALL uhdk_get_preferences(uhdk_session *session, uhdk_preferences *out);
/* Event payloads. Window is NULL for app-wide events. time_ns is monotonic time
* relative to app creation, not wall time. Do not persist event pointers.
* Destroying a window suppresses queued window input/redraw events and cancels
* its requests. REQUEST_DONE never includes a dangling window pointer.
/* Tag stored in `uhdk_event.type`. Payload structs are listed in
* [Event payloads](#event-payloads). Window is NULL for app-wide events.
* time_ns is monotonic time relative to app creation, not wall time. Do not
* persist event pointers. Destroying a window suppresses queued window
* input/redraw events and cancels its requests. REQUEST_DONE never includes a
* dangling window pointer.
*/
typedef uint32_t uhdk_event_type;
enum {
/* Normal shutdown has drained accepted requests and active input cycles. */
UHDK_EVENT_STOPPED = 1,
/* A coalesced uhdk_wake call or scheduled wake deadline became observable. */
UHDK_EVENT_WAKE,
/* The OS or user requested application termination; call uhdk_quit to accept. */
UHDK_EVENT_QUIT_REQUESTED,
/* The operating system changed the application's active state. */
UHDK_EVENT_APP_ACTIVATION,
/* The operating system asked the application to open one or more documents. */
UHDK_EVENT_OPEN_FILES,
/* System appearance or accessibility preferences changed. */
UHDK_EVENT_PREFERENCES_CHANGED,
/* The user requested that this window close; destroy it or quit to accept. */
UHDK_EVENT_WINDOW_CLOSE_REQUESTED,
/* Window size, scale, state, focus, visibility, or observation flags changed. */
UHDK_EVENT_WINDOW_METRICS,
/* The window needs a complete repaint during a drawable frame. */
UHDK_EVENT_WINDOW_REDRAW,
/* A physical/logical key was pressed, repeated, or released. */
UHDK_EVENT_KEY,
/* The pointer entered the window's content area. */
UHDK_EVENT_POINTER_ENTER,
/* The pointer left the window's content area. */
UHDK_EVENT_POINTER_LEAVE,
/* The pointer position changed, including movement during capture. */
UHDK_EVENT_POINTER_MOVE,
/* A pointer button was pressed or released. */
UHDK_EVENT_POINTER_BUTTON,
/* The effective pointer mode changed, including forced capture loss. */
UHDK_EVENT_POINTER_MODE_CHANGED,
/* A wheel or touchpad produced a discrete or phased scroll update. */
UHDK_EVENT_SCROLL,
/* Text input committed UTF-8 text to the application document. */
UHDK_EVENT_TEXT_COMMIT,
/* Text input supplied the current in-progress composition text. */
UHDK_EVENT_TEXT_PREEDIT,
/* The current text composition ended by commit or cancellation. */
UHDK_EVENT_TEXT_COMPOSITION_END,
/* Native text handling requested a semantic editing operation. */
UHDK_EVENT_EDIT_COMMAND,
/* The user activated an application-defined menu command. */
UHDK_EVENT_MENU_COMMAND,
/* The user dropped one or more files onto the window. */
UHDK_EVENT_DROP_FILES,
/* A filesystem watch observed changes or encountered a failure. */
UHDK_EVENT_WATCH,
/* An accepted asynchronous request reached its terminal result. */
UHDK_EVENT_REQUEST_DONE,
UHDK_EVENT_CYCLE_END /* uhdk_cycle_end_event; cycle_id identifies the interaction. */
/* The input interaction identified by cycle_id reached its terminal state. */
UHDK_EVENT_CYCLE_END
};
typedef uint32_t uhdk_request_kind;
@ -989,9 +1071,13 @@ typedef struct uhdk_watch_event {
uhdk_file *file; /* Nullable when only the watched root is known. */
} uhdk_watch_event;
typedef struct uhdk_activation_event {
uhdk_bool active;
} uhdk_activation_event;
/* Application-wide activation transition. This reports whether the operating
* system considers the application active, not focus for an individual window;
* event.window is NULL. Before a transition to inactive, UHDK emits CYCLE_END
* for active input cycles with UHDK_CYCLE_FOCUS_LOST. */
typedef struct uhdk_app_activation_event {
uhdk_bool active; /* UHDK_TRUE when active; UHDK_FALSE when inactive. */
} uhdk_app_activation_event;
typedef struct uhdk_pointer_mode_event {
uhdk_pointer_mode mode;
} uhdk_pointer_mode_event;

View File

@ -30,7 +30,7 @@ fn result(allocator: std.mem.Allocator, source: c.z_request_result) !c.uhdk_requ
pub fn pack(allocator: std.mem.Allocator, source: c.z_event) !c.uhdk_event {
const data: Data = switch (source.type) {
c.UHDK_EVENT_APP_ACTIVATION => try store(c.uhdk_activation_event, allocator, .{ .active = source.data.active }),
c.UHDK_EVENT_APP_ACTIVATION => try store(c.uhdk_app_activation_event, allocator, .{ .active = source.data.active }),
c.UHDK_EVENT_OPEN_FILES => try store(c.uhdk_file_list, allocator, try files(allocator, source.data.files)),
c.UHDK_EVENT_WINDOW_METRICS => try store(c.uhdk_window_metrics, allocator, source.data.metrics),
c.UHDK_EVENT_PREFERENCES_CHANGED => try store(c.uhdk_preferences, allocator, source.data.preferences),

View File

@ -1,5 +1,4 @@
//! C ABI entry points for desktop.
const std = @import("std");
const c = @import("native");
const runtime = @import("../runtime.zig");
const abi = @import("../abi.zig");
@ -54,9 +53,7 @@ pub export fn uhdk_set_icon(
i.height == 0 or
i.width > 4096 or
i.height > 4096 or
i.stride_bytes < @as(u64, i.width) * 4 or
i.stride_bytes > std.math.maxInt(u64) / @as(u64, i.height) or
i.pixels.len < i.stride_bytes * i.height or
i.pixels.len < @as(u64, i.width) * i.height * 4 or
!abi.validSpan(i.pixels.ptr, i.pixels.len)) return abi.INVALID;
const command = runtime.createAppCommand(handle, c.Z_ICON) catch |err| return runtime.applicationError(err);
command.data.image = i;

View File

@ -122,7 +122,7 @@ pub fn pixels(destination: []u32, side: usize, image: n.uhdk_image) void {
const x0 = (side - image.width) / 2;
const y0 = (side - image.height) / 2;
for (0..image.height) |y| for (0..image.width) |x| {
const offset = y * @as(usize, @intCast(image.stride_bytes)) + x * 4;
const offset = (y * image.width + x) * 4;
const a: u32 = source[offset + 3];
const r = (@as(u32, source[offset]) * a + 127) / 255;
const g = (@as(u32, source[offset + 1]) * a + 127) / 255;
@ -180,7 +180,6 @@ test "icons premultiply RGBA and pad rectangular images" {
var image = std.mem.zeroes(n.uhdk_image);
image.width = 2;
image.height = 1;
image.stride_bytes = 8;
image.pixels = .{ .ptr = &rgba, .len = rgba.len };
var result: [4]u32 = undefined;
pixels(&result, 2, image);

View File

@ -136,7 +136,6 @@ static void real_desktop(uhdk_session *app) {
image.struct_size = sizeof(image);
image.width = 2;
image.height = 1;
image.stride_bytes = 8;
image.pixels = (uhdk_bytes){rgba, sizeof(rgba)};
OK(uhdk_set_icon(app, &image));
// A hidden, unfocused window must not commandeer a later user's keystroke.

View File

@ -1758,7 +1758,7 @@ uhdk_status z_native_execute(
for (uint32_t y = 0; y < i->height; y++)
memcpy(
rep.bitmapData + (size_t)y * rep.bytesPerRow,
i->pixels.ptr + y * i->stride_bytes,
i->pixels.ptr + (size_t)y * i->width * 4,
(size_t)i->width * 4
);
NSImage *image = [[NSImage alloc] initWithSize:NSMakeSize(i->width, i->height)];

View File

@ -1631,7 +1631,7 @@ static uhdk_status setIcon(const uhdk_image *image) {
return UHDK_STATUS_OS_ERROR;
for (uint32_t y = 0; y < image->height; y++)
for (uint32_t x = 0; x < image->width; x++) {
const uint8_t *p = image->pixels.ptr + y * image->stride_bytes + x * 4;
const uint8_t *p = image->pixels.ptr + ((size_t)y * image->width + x) * 4;
uint8_t *out = (uint8_t *)pixels + ((size_t)y * image->width + x) * 4;
out[0] = (uint8_t)((uint32_t)p[2] * p[3] / 255);
out[1] = (uint8_t)((uint32_t)p[1] * p[3] / 255);