lore-forge/desktop/vendor/uhdk/include/uhdk.h

1419 lines
57 KiB
C

#ifndef UHDK_H_INCLUDED
#define UHDK_H_INCLUDED
/*
* 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.
* <stdint.h> supplies types only; this header requires no libc function calls.
* Native OS frameworks may themselves depend on system runtimes.
*
* Header configuration (define before the first include):
* - 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.
*
* Naming:
* - Types: uhdk_type; functions: uhdk_object_verb; constants: UHDK_CATEGORY_VALUE.
* - create/destroy allocate and dispose unique objects; retain/release manage
* shared ownership. Reference-counted objects also start with create.
* - get/set access properties; copy writes caller storage; is/has/can query bools.
* - *_async starts a request completed by UHDK_EVENT_REQUEST_DONE. Functions
* that merely schedule events (wake/invalidate) do not create async requests.
* - *_desc describes creation/configuration; *_fn names callbacks; *_flags names
* bitmask types. Pointer/span lengths use len or <field>_len; writable buffers
* use capacity. Measurement units are named explicitly where needed.
*
* Global rules:
* - Floating-point values use C float (32-bit / Zig f32 on supported targets).
* - All text is UTF-8 pointer + byte length, never implicitly NUL-terminated.
* Embedded NUL is rejected where an OS identifier/path cannot represent it.
* - NULL is allowed for a span's pointer only when its length is zero.
* - Input spans/descriptors are copied before return unless documented otherwise.
* - No allocator ownership crosses the ABI. Release objects through Uhdk.
* - All calls belong to OS main except uhdk_wake and uhdk_status_message. Native dispatch
* runs at next_frame and synchronous creation boundaries. Filesystem work runs
* on an internal I/O worker; final shutdown may wait for active I/O.
* - Events and nested spans are borrowed until the next valid next_frame call or
* close. Copy spans or retain file/menu references to keep them. End_frame and
* synchronous window creation do not invalidate the current batch.
* - Descriptors start with struct_size; zero-initialize them, set struct_size to
* sizeof(the descriptor), then fill fields. Reserved fields must remain zero.
* Unknown trailing fields are ignored; smaller descriptors are rejected.
* - Integer enum values have fixed-width typedefs; C enums only name constants.
* - For output structs with struct_size, callers set it before the call; Uhdk
* writes only the supplied size. Event struct_size describes library output.
* - Functions return a status; output arguments are valid only on UHDK_STATUS_OK,
* except documented buffer-size queries.
* - The application owns rendering, document state, file encoding and layout.
* Uhdk provides optional OpenGL 3.3 core context management. Other renderers
* may attach through the experimental native-handle API; drawing remains app-owned.
*/
#include <stdint.h>
/* 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
#define UHDK_PROFILE_BEGIN(token, name) ((void)0)
#define UHDK_PROFILE_END(token) ((void)0)
#define UHDK_PROFILE_SCOPE(name) ((void)0)
#define UHDK_PROFILE_FRAME() ((void)0)
#define UHDK_PROFILE_THREAD(name) ((void)0)
#define UHDK_PROFILE_ALLOC(ptr, size, pool) ((void)0)
#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)
#define UHDK_API __declspec(dllexport)
#else
#define UHDK_API __declspec(dllimport)
#endif
#elif defined(__GNUC__) && defined(UHDK_SHARED)
#define UHDK_API __attribute__((visibility("default")))
#else
#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
#define UHDK_CALL
#endif
#ifdef __cplusplus
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,
UHDK_TRUE = 1
};
typedef uint32_t uhdk_status;
enum {
UHDK_STATUS_OK = 0,
UHDK_STATUS_CANCELLED = 1,
UHDK_STATUS_UNSUPPORTED = 2,
UHDK_STATUS_INVALID_ARGUMENT = 3,
UHDK_STATUS_OUT_OF_MEMORY = 4,
UHDK_STATUS_PERMISSION_DENIED = 5,
UHDK_STATUS_UNAVAILABLE = 6,
UHDK_STATUS_NOT_FOUND = 7,
UHDK_STATUS_BUSY = 8,
UHDK_STATUS_WRONG_THREAD = 9,
UHDK_STATUS_OS_ERROR = 10,
UHDK_STATUS_BUFFER_TOO_SMALL = 11
};
/* 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;
/* Session-owned status/tray item. Destroy explicitly or close the session. */
typedef struct uhdk_tray uhdk_tray;
/* Generally all asynchronous interfaces in uhdk will ask you to pass in a caller
* defined tag. This will associate the transaction with the tag for the life time
* of the interaction, callbacks, cancellations, state polling shall all have this
* tag available or have the user provide it.
*
* This tag must be nonzero
*
* See [Asynchronous services](#asynchronous-services).
*/
typedef uint64_t uhdk_request_tag;
/* UTF-8 byte span, not implicitly NUL-terminated. */
typedef struct uhdk_text {
const char *ptr;
uint64_t len;
} uhdk_text;
/* Human-readable English text for any status, including unknown values.
* Static library-owned UTF-8 span; valid for the library lifetime, no allocation,
* callable from any thread without a session. Never parse messages for logic. */
UHDK_API uhdk_text UHDK_CALL uhdk_status_message(uhdk_status status);
typedef struct uhdk_bytes {
const uint8_t *ptr;
uint64_t len;
} uhdk_bytes;
typedef struct uhdk_text_list {
const uhdk_text *ptr;
uint64_t len;
} uhdk_text_list;
typedef struct uhdk_file_list {
uhdk_file *const *ptr;
uint64_t len;
} uhdk_file_list;
typedef struct uhdk_point {
float x;
float y;
} uhdk_point;
typedef struct uhdk_size {
float width;
float height;
} uhdk_size;
typedef struct uhdk_rect {
float x;
float y;
float width;
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;
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,
UHDK_BACKEND_MACOS = 1,
UHDK_BACKEND_WINDOWS = 2,
UHDK_BACKEND_WAYLAND = 3,
UHDK_BACKEND_X11 = 4
};
typedef uint32_t uhdk_capability;
enum {
UHDK_CAP_FILE_DIALOGS = 1,
UHDK_CAP_CLIPBOARD = 2,
UHDK_CAP_DROP_FILES = 4,
UHDK_CAP_APP_MENU = 5,
UHDK_CAP_CONTEXT_MENU = 6,
UHDK_CAP_APP_ICON = 8,
UHDK_CAP_POINTER_CAPTURE = 12,
UHDK_CAP_TEXT_INPUT = 16,
UHDK_CAP_OPENGL = 18,
UHDK_CAP_TRAY = 19,
UHDK_CAP_POINTER_CONFINEMENT = 20
};
typedef struct uhdk_event uhdk_event;
typedef struct uhdk_event_list {
const uhdk_event *ptr;
uint64_t len;
} uhdk_event_list;
UHDK_API void UHDK_CALL uhdk_quit(uhdk_session *session); /* Accept/request shutdown. */
UHDK_API void UHDK_CALL uhdk_wake(uhdk_session *session); /* Thread-safe, coalesced WAKE event. */
/* Monotonic nanoseconds since app creation, in the same clock as event.time_ns. */
UHDK_API uint64_t UHDK_CALL uhdk_get_time_ns(uhdk_session *session);
/* One pending deadline per session. Replaces any previous deadline; 0 cancels it.
* A past deadline queues WAKE at the next dispatch. On expiry the deadline is
* cleared before delivering WAKE. Explicit wakes may coalesce with expiry but
* do not cancel a future deadline. No event is delivered inline. App code maintains
* multiple timers by scheduling its earliest deadline. No real-time guarantee.
*/
UHDK_API uhdk_status UHDK_CALL uhdk_set_wake_deadline(uhdk_session *session, uint64_t deadline_ns);
/* Linux: AUTO/false before successful startup; thereafter the selected backend
* and implemented, available mechanisms. Capabilities do not guarantee success
* for a particular operation. Other platforms retain their fixed backend. */
UHDK_API uhdk_backend UHDK_CALL uhdk_get_backend(uhdk_session *session);
UHDK_API uhdk_bool UHDK_CALL uhdk_has_capability(uhdk_session *session, uhdk_capability capability);
/* Best effort; the tag remains reserved until its completion is delivered.
* NOT_FOUND means no executing request has this tag (its result may be queued). */
UHDK_API uhdk_status UHDK_CALL uhdk_request_cancel(uhdk_session *session, uhdk_request_tag tag);
/* A presented frame updates render_time_ns/frame_count. Frame CPU time excludes
* presentation, blocked waits and descheduling.
* Session render_time_ns includes application update work within the iteration.
* frames_per_second uses wall time between presented end_frame calls; it is zero
* before two presentations and retains its last sample while idle. event_count
* counts polled events. Use profiling markers for application-defined work. */
typedef struct uhdk_stats {
uint32_t struct_size;
uint32_t reserved;
uint64_t frame_count;
uint64_t event_count;
uint64_t render_time_ns;
float frames_per_second;
} uhdk_stats;
UHDK_API uhdk_status UHDK_CALL uhdk_get_stats(uhdk_session *session, uhdk_stats *out);
/* Logical coordinates: top-left origin, x right, y down. Window geometry uses
* content-area units. Framebuffer dimensions are pixels; use reported sizes,
* not a rounded logical-size * scale calculation. Global positions are not exposed.
*/
/* Native decorations are the title bar, border, and standard window controls.
* Disable them when the application paints and operates its own window chrome. */
typedef struct uhdk_window_style {
uint32_t struct_size;
uhdk_bool decorated;
uhdk_bool resizable;
uint16_t reserved;
} uhdk_window_style;
typedef uint32_t uhdk_window_state;
enum {
UHDK_WINDOW_NORMAL = 0,
UHDK_WINDOW_MINIMIZED = 1,
UHDK_WINDOW_MAXIMIZED = 2,
UHDK_WINDOW_FULLSCREEN = 3
};
typedef struct uhdk_window_desc {
uint32_t struct_size;
uint32_t reserved;
uhdk_window_style style;
uhdk_text title;
uhdk_size content_size;
uhdk_size min_size;
uhdk_size max_size; /* Zero limit = unconstrained. */
uhdk_window *parent; /* Optional; must outlive child. */
} uhdk_window_desc;
/* Observation bits say which independent window facts are known. A clear bit
* means unknown, not false. Support depends on the backend/protocol version. */
typedef uint32_t uhdk_window_observation_flags;
enum {
UHDK_WINDOW_OBSERVE_MINIMIZED = 1u << 0,
UHDK_WINDOW_OBSERVE_RESIZING = 1u << 1,
UHDK_WINDOW_OBSERVE_OCCLUDED = 1u << 2,
UHDK_WINDOW_OBSERVE_SUSPENDED = 1u << 3
};
/* WINDOW_METRICS is the resize/state hook; compare snapshots for transitions.
* State describes geometry (normal/maximized/fullscreen), independently of
* minimized. Wayland does not report minimization; its observation bit is clear.
* visible is show/hide visibility, not an occlusion or minimization guarantee;
* on Wayland it is application intent. occluded is an optional rendering hint,
* not permission to stop processing events. suspended is a separate compositor
* hint that content is not ordinarily repainted, not proof of occlusion or
* minimization. Resizing is an advisory interactive size/move-loop
* hint, not a guaranteed transaction around every size change. Setters enqueue
* native work; geometry and state changes are observed here. */
typedef struct uhdk_window_metrics {
uint32_t struct_size;
uint32_t framebuffer_width;
uint32_t framebuffer_height;
uhdk_window_state state;
uhdk_size logical_size;
float scale;
uhdk_bool focused;
uhdk_bool visible;
uhdk_window_observation_flags observations;
uhdk_bool minimized;
uhdk_bool resizing;
uhdk_bool occluded;
uhdk_bool suspended;
} uhdk_window_metrics;
/* 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 {
UHDK_RENDER_CONTINUOUS = 0,
UHDK_RENDER_ON_DEMAND = 1
};
typedef struct uhdk_options {
uint32_t struct_size;
uhdk_render_mode render_mode;
uhdk_text application_id;
uhdk_text title;
uhdk_size content_size; /* Zero components default to 800 x 600. */
uhdk_size min_size;
uhdk_size max_size;
uhdk_backend preferred_backend;
uhdk_bool undecorated; /* Zero defaults to decorated and resizable. */
uhdk_bool fixed_size;
uhdk_bool disable_vsync; /* Zero requests vsync; unsupported control is tolerated. */
uhdk_bool request_focus;
uhdk_bool no_opengl; /* Native/external-renderer session; get_gl/frame.gl are NULL. */
uint64_t startup_timeout_ns; /* Native resource readiness wait; zero = 10 seconds. */
} uhdk_options;
typedef struct uhdk_frame {
const uhdk_gl_functions *gl;
uhdk_event_list events;
uhdk_window_metrics metrics;
uhdk_bool drawable;
} uhdk_frame;
/* NULL options selects defaults. Otherwise zero-initialize and set struct_size.
* Creates native services, a visible window, and a current OpenGL >=3.3 core
* context before returning OK, unless no_opengl is set. Failure cleans up and
* sets *out=NULL. Startup pumps native events internally and preserves them for
* the first frame.
* GL resources may be created immediately with get_gl: the bundled OpenGL 3.3
* function table is already loaded. No application callbacks execute during setup.
*/
UHDK_API uhdk_status UHDK_CALL uhdk_open(const uhdk_options *options, uhdk_session **out);
/* Begin one application iteration, including event-only iterations when hidden
* or minimized. Native-only sessions have a NULL GL table. Each successful call
* requires end_frame, even if not drawable.
* Events remain borrowed until the next next_frame or close. Continuous mode
* polls without waiting while drawable; on-demand mode waits for events/wakes.
* The first iteration never waits. A drawable iteration should repaint the
* complete window framebuffer. False means stopped or failed; get_status
* distinguishes normal termination (OK) from an error. No frame opens on false.
* STOPPED and final request completions are delivered before normal termination.
*/
UHDK_API uhdk_bool UHDK_CALL uhdk_next_frame(uhdk_session *session, uhdk_frame *out);
/* Presents drawable frames and finishes the iteration. The context remains
* current for resource work between iterations, including shutdown. Call quit
* to accept a close/quit request; this suppresses presentation during draining.
* Native changes are applied at the next frame boundary. Never block waiting
* for native requests between boundaries. GL calls stay on OS main.
*/
UHDK_API uhdk_status UHDK_CALL uhdk_end_frame(uhdk_session *session);
UHDK_API uhdk_status UHDK_CALL uhdk_get_status(uhdk_session *session);
UHDK_API const uhdk_gl_functions *UHDK_CALL uhdk_get_gl(uhdk_session *session);
UHDK_API uhdk_window *UHDK_CALL uhdk_get_window(uhdk_session *session);
/* NULL safe. Valid after the session stops or enters a terminal error state, and
* discards an unfinished frame without presenting. Destroy app-owned GL resources
* before close; all session handles become invalid on return. */
UHDK_API void UHDK_CALL uhdk_close(uhdk_session *session);
/* Synchronously create an additional native window for an external renderer.
* The session owns GL only for its primary window. The new window starts hidden.
* Call between iterations; BUSY during a frame. Native events collected during
* creation are preserved for next_frame. Failure sets *out=NULL and releases
* partial native resources. Parent handles must outlive their children. */
UHDK_API uhdk_status UHDK_CALL uhdk_window_create(
uhdk_session *session,
const uhdk_window_desc *desc,
uhdk_window **out
);
/* Destroy an additional window. The primary window is owned by close and cannot
* be destroyed separately. Pending events/requests follow the lifecycle rules. */
UHDK_API uhdk_status UHDK_CALL uhdk_window_destroy(uhdk_window *window);
UHDK_API uhdk_status UHDK_CALL uhdk_window_set_title(uhdk_window *window, uhdk_text title);
UHDK_API uhdk_status UHDK_CALL uhdk_window_set_size(uhdk_window *window, uhdk_size size);
UHDK_API uhdk_status UHDK_CALL uhdk_window_set_state(uhdk_window *window, uhdk_window_state state);
UHDK_API uhdk_status UHDK_CALL uhdk_window_set_visible(uhdk_window *window, uhdk_bool visible);
UHDK_API uhdk_status UHDK_CALL uhdk_window_request_focus(uhdk_window *window);
/* Change native decoration and resize styling without recreating the window.
* The window manager may apply it asynchronously. Content dimensions remain
* content-area dimensions. */
UHDK_API uhdk_status UHDK_CALL uhdk_window_set_style(
uhdk_window *window,
const uhdk_window_style *style
);
UHDK_API uhdk_status UHDK_CALL uhdk_window_get_metrics(
uhdk_window *window,
uhdk_window_metrics *out
);
UHDK_API void UHDK_CALL uhdk_window_invalidate(uhdk_window *window); /* Coalesced redraw event. */
/* The session owns one OpenGL >=3.3 core context and its loaded function table.
* Context ownership, native synchronization, and presentation are internal.
* Resource GL calls are valid from open until close on OS main. Draw to the
* window framebuffer only during drawable iterations. Applications own all
* 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
#define UHDK_GL_CALL
#endif
typedef void(UHDK_GL_CALL *uhdk_gl_proc)(void);
/* ASCII symbol name. Missing symbols return NOT_FOUND. A non-null procedure
* does not establish extension support. Valid until close, on OS main only. */
UHDK_API uhdk_status UHDK_CALL uhdk_get_gl_proc(
uhdk_session *session,
uhdk_text name,
uhdk_gl_proc *out
);
/* Request vsync (true) or unsynchronized presentation (false). Native compositor
* policy may override it. Unsupported control returns UNSUPPORTED. */
UHDK_API uhdk_status UHDK_CALL uhdk_set_vsync(uhdk_session *session, uhdk_bool enabled);
/* Keyboard events are not text. Physical key = USB HID keyboard-page usage;
* 0 means unknown. Logical key = Unicode scalar, or UHDK_KEY_* below.
* Modifier bits describe state after the event. Focus loss resets held state.
* With text input enabled, text/edit events are authoritative for editing;
* raw key events are observational for those actions (do not insert twice).
* Registered menu accelerators are dispatched as menu/edit commands instead of
* raw key events. Unregistered application shortcuts need their own policy.
*/
typedef uint32_t uhdk_modifier_flags;
enum {
UHDK_MOD_SHIFT = 1u << 0,
UHDK_MOD_CONTROL = 1u << 1,
UHDK_MOD_ALT = 1u << 2,
UHDK_MOD_SUPER = 1u << 3,
UHDK_MOD_CAPS_LOCK = 1u << 4,
UHDK_MOD_NUM_LOCK = 1u << 5
};
typedef uint32_t uhdk_key;
enum {
UHDK_KEY_UNKNOWN = 0,
UHDK_KEY_ESCAPE = 0x110000,
UHDK_KEY_ENTER,
UHDK_KEY_TAB,
UHDK_KEY_BACKSPACE,
UHDK_KEY_DELETE,
UHDK_KEY_INSERT,
UHDK_KEY_LEFT,
UHDK_KEY_RIGHT,
UHDK_KEY_UP,
UHDK_KEY_DOWN,
UHDK_KEY_HOME,
UHDK_KEY_END,
UHDK_KEY_PAGE_UP,
UHDK_KEY_PAGE_DOWN,
UHDK_KEY_F1 = 0x110100,
UHDK_KEY_F2,
UHDK_KEY_F3,
UHDK_KEY_F4,
UHDK_KEY_F5,
UHDK_KEY_F6,
UHDK_KEY_F7,
UHDK_KEY_F8,
UHDK_KEY_F9,
UHDK_KEY_F10,
UHDK_KEY_F11,
UHDK_KEY_F12,
UHDK_KEY_F13,
UHDK_KEY_F14,
UHDK_KEY_F15,
UHDK_KEY_F16,
UHDK_KEY_F17,
UHDK_KEY_F18,
UHDK_KEY_F19,
UHDK_KEY_F20,
UHDK_KEY_F21,
UHDK_KEY_F22,
UHDK_KEY_F23,
UHDK_KEY_F24
};
typedef uint32_t uhdk_function_key_policy;
enum {
UHDK_FUNCTION_KEYS_NATIVE = 0,
UHDK_FUNCTION_KEYS_APPLICATION = 1
};
/* Select native fallback handling for function keys received by this window.
* NATIVE is the default. On Windows, APPLICATION suppresses native default
* actions for unmodified F1-F24, including F10 menu activation, while continuing
* to deliver KEY events. Shift/Control/Alt/Super combinations retain native
* behavior, including Alt+F4. Caps Lock and Num Lock do not count as modifiers
* for this policy. Registered UHDK menu shortcuts still take precedence.
*
* Queued for the next frame boundary. A press keeps the policy selected on its
* initial key-down through repeats and release, even if the policy or modifiers
* change while held. Focus loss clears this held-key state; the configured
* policy persists. Set this before the relevant press; consuming a KEY event
* cannot retroactively cancel native handling.
*
* Accepted on all backends; currently has an effect only on Windows. This does
* not capture the keyboard or override shortcuts intercepted by the OS,
* compositor, IME, or keyboard firmware before UHDK receives the event.
*/
UHDK_API uhdk_status UHDK_CALL uhdk_window_set_function_key_policy(
uhdk_window *window,
uhdk_function_key_policy policy
);
typedef uint32_t uhdk_pointer_button;
enum {
UHDK_BUTTON_NONE = 0,
UHDK_BUTTON_PRIMARY = 1,
UHDK_BUTTON_SECONDARY = 2,
UHDK_BUTTON_MIDDLE = 3,
UHDK_BUTTON_BACK = 4,
UHDK_BUTTON_FORWARD = 5
};
typedef uint32_t uhdk_scroll_unit;
enum {
UHDK_SCROLL_LOGICAL_PIXELS = 0,
UHDK_SCROLL_LINES = 1
};
typedef uint32_t uhdk_scroll_phase;
enum {
UHDK_SCROLL_DISCRETE = 0,
UHDK_SCROLL_BEGIN = 1,
UHDK_SCROLL_UPDATE = 2,
UHDK_SCROLL_END = 3,
UHDK_SCROLL_CANCEL = 4
};
typedef uint32_t uhdk_cursor_shape;
enum {
UHDK_CURSOR_ARROW = 0,
UHDK_CURSOR_TEXT,
UHDK_CURSOR_CROSSHAIR,
UHDK_CURSOR_HAND,
UHDK_CURSOR_RESIZE_HORIZONTAL,
UHDK_CURSOR_RESIZE_VERTICAL,
UHDK_CURSOR_RESIZE_NW_SE,
UHDK_CURSOR_RESIZE_NE_SW,
UHDK_CURSOR_MOVE,
UHDK_CURSOR_NOT_ALLOWED,
UHDK_CURSOR_WAIT,
UHDK_CURSOR_HIDDEN
};
typedef uint32_t uhdk_pointer_mode;
enum {
UHDK_POINTER_NORMAL = 0,
UHDK_POINTER_CAPTURE = 1,
UHDK_POINTER_CONFINED = 2
};
UHDK_API uhdk_status UHDK_CALL uhdk_window_set_cursor_shape(
uhdk_window *window,
uhdk_cursor_shape shape
);
/* NORMAL releases capture or confinement. CAPTURE delivers selection-drag
* events outside the window. CONFINED keeps the cursor within the window's
* content area as it moves/resizes, preserving cursor shape and absolute motion.
* Confinement requires UHDK_CAP_POINTER_CONFINEMENT (otherwise UNSUPPORTED)
* and a visible, focused window with the pointer inside at native dispatch.
* Requests without the required focus/visibility are ignored.
* Windows and Wayland pointer-constraints-v1 support confinement; macOS currently
* does not.
*
* Changes are queued for the next frame boundary. OK means accepted, not active:
* observe POINTER_MODE_CHANGED for activation. Wayland activation is subject to
* compositor policy and may never occur. A native failure does not activate it.
* Focus loss, hiding/minimizing, or OS cancellation releases the mode (including
* pending confinement), restores NORMAL and emits POINTER_MODE_CHANGED. It does
* not reactivate automatically. Destroying/closing the window also releases it.
* Setting an already requested mode is a no-op. Modes are mutually exclusive;
* confinement does not provide relative motion or pointer locking.
*/
UHDK_API uhdk_status UHDK_CALL uhdk_window_set_pointer_mode(
uhdk_window *window,
uhdk_pointer_mode mode
);
/* Document snapshot for native text input and IME.
*
* Publish with [`uhdk_window_set_text_state`](api:uhdk_window_set_text_state)
* after applying text and edit events. Offsets and lengths are UTF-8 byte
* counts at Unicode scalar boundaries.
*
* `surrounding_text` is a bounded window into the document, not the whole
* buffer. `document_offset` is that window's origin. Selection here, and
* replacement ranges on [`uhdk_text_event`](api:uhdk_text_event), are relative
* to this window.
*
* `revision` is the application's document generation. Apply every text and
* edit event in batch order, including a burst that shares one revision. Do
* not drop later events because the first one already changed your revision.
*
* `acknowledged_input_id` is the last applied text or edit
* [`input_id`](api:uhdk_event). Set it from TEXT_COMMIT, TEXT_PREEDIT,
* TEXT_COMPOSITION_END, or EDIT_COMMAND after that event is applied or
* ignored. Do not take it from a raw KEY that may precede those events. For
* an asynchronous edit such as paste, wait until the result is in the
* document.
*
* UHDK ignores a snapshot whose acknowledgment is behind outstanding native
* edits. After consuming the batch, publish again with the new id.
*
* On macOS, a semantic EDIT_COMMAND invalidates the native snapshot. Later
* text input waits for an acknowledging snapshot, including when the command
* was ignored.
*/
typedef struct uhdk_text_state {
uint32_t struct_size;
uint32_t reserved;
uhdk_text surrounding_text; /* Bounded UTF-8 window; copied on publish. */
uint64_t revision; /* Application document generation. */
uint64_t acknowledged_input_id; /* Last applied text/edit input_id. */
uint64_t document_offset; /* Byte origin of surrounding_text in the document. */
uint64_t selection_start; /* Relative to surrounding_text. */
uint64_t selection_len; /* Relative to surrounding_text. */
uhdk_rect caret; /* Window-local logical coordinates. */
} uhdk_text_state;
/* Enable native text input and IME. Disabling discards deferred input and
* ends composition. */
UHDK_API uhdk_status UHDK_CALL uhdk_window_set_text_input(uhdk_window *window, uhdk_bool enabled);
/* Copy `state` and publish it for IME and surrounding-text queries. Ignored
* when `acknowledged_input_id` is behind outstanding native edits; publish
* again after consuming the batch. See [`uhdk_text_state`](api:uhdk_text_state). */
UHDK_API uhdk_status UHDK_CALL uhdk_window_set_text_state(
uhdk_window *window,
const uhdk_text_state *state
);
/* End the current composition without committing it. */
UHDK_API uhdk_status UHDK_CALL uhdk_window_cancel_composition(uhdk_window *window);
/* File references preserve native names, including non-UTF-8 POSIX paths.
* Display names are UTF-8. URI/native-path export is explicit and owned by the
* caller's buffer. Windows native path bytes are UTF-16LE (no terminator),
* POSIX native paths are uninterpreted bytes. Windows URI export returns
* UNSUPPORTED when the shell cannot represent a native name losslessly (such
* as unpaired UTF-16 surrogates); native-path export remains available.
* A reference can carry an access
* grant; retaining it keeps that grant alive where the OS permits. Persistence
* across launches/bookmark serialization is deferred to a future extension.
* Buffer queries: NULL/0 gets required byte count in out_len and returns UHDK_STATUS_OK.
* Too-small buffers return UHDK_STATUS_BUFFER_TOO_SMALL and required count; no partial copy.
*/
typedef uint32_t uhdk_file_representation;
enum {
UHDK_FILE_DISPLAY_NAME = 0,
UHDK_FILE_URI = 1,
UHDK_FILE_NATIVE_PATH = 2
};
UHDK_API uhdk_status UHDK_CALL uhdk_file_create_from_native_path(
uhdk_session *session,
uhdk_bytes path,
uhdk_file **out
);
UHDK_API uhdk_status UHDK_CALL uhdk_file_create_from_uri(
uhdk_session *session,
uhdk_text uri,
uhdk_file **out
);
UHDK_API void UHDK_CALL uhdk_file_retain(uhdk_file *file);
UHDK_API void UHDK_CALL uhdk_file_release(uhdk_file *file);
UHDK_API uhdk_status UHDK_CALL uhdk_file_copy_representation(
uhdk_file *file,
uhdk_file_representation representation,
uint8_t *buffer,
uint64_t capacity,
uint64_t *out_len
);
typedef uint32_t uhdk_file_type;
enum {
UHDK_FILE_REGULAR = 1,
UHDK_FILE_DIRECTORY = 2,
UHDK_FILE_OTHER = 3
};
typedef struct uhdk_file_info {
uint64_t size_bytes;
int64_t modified_time_ns; /* Unix epoch, nanoseconds. */
uhdk_file_type type;
uint32_t reserved;
} uhdk_file_info;
/* FIFO across accepted file requests. Read results are raw bytes, batch-owned.
* max_bytes is a hard limit; oversized files return BUFFER_TOO_SMALL.
* Read/write accept regular files only; final-component symlinks are rejected.
* Write copies bytes at submission, stages an exclusive same-directory temp,
* preserves POSIX mode of an existing regular file, then renames atomically.
* Windows rejects read-only destinations; replacement gets the directory's
* inherited permissions and does not preserve other destination attributes.
* Symlinks/nonregular destinations are rejected. ACLs/xattrs/ownership are not
* preserved; replacement requires a directory writable by the current user.
* New POSIX files use mode 0600. No power-loss durability or conflict detection promise.
* No silent truncate fallback. Cancellation before rename leaves the target intact.
*/
UHDK_API uhdk_status UHDK_CALL uhdk_file_read_all_async(
uhdk_file *file,
uint64_t max_bytes,
uhdk_request_tag tag
);
UHDK_API uhdk_status UHDK_CALL uhdk_file_write_all_async(
uhdk_file *file,
uhdk_bytes bytes,
uhdk_request_tag tag
);
UHDK_API uhdk_status UHDK_CALL uhdk_file_get_info_async(uhdk_file *file, uhdk_request_tag tag);
typedef uint32_t uhdk_dialog_kind;
enum {
UHDK_DIALOG_OPEN_FILE = 0,
UHDK_DIALOG_OPEN_FILES = 1,
UHDK_DIALOG_OPEN_DIRECTORY = 2,
UHDK_DIALOG_SAVE_FILE = 3
};
typedef struct uhdk_file_filter {
uhdk_text label;
uhdk_text_list extensions; /* No leading dot; internal dots allowed (e.g. level.json).
An empty list means all files. */
} uhdk_file_filter;
typedef struct uhdk_file_dialog_desc {
uint32_t struct_size;
uhdk_dialog_kind kind;
uhdk_window *parent;
uhdk_text title;
uhdk_text accept_label;
uhdk_text suggested_name; /* Empty = native default. */
uhdk_file *initial_directory;
const uhdk_file_filter *filters;
uint64_t filters_len;
} uhdk_file_dialog_desc;
UHDK_API uhdk_status UHDK_CALL uhdk_file_dialog_show_async(
uhdk_session *session,
const uhdk_file_dialog_desc *desc,
uhdk_request_tag tag
);
/* Plain-text clipboard only. Writes copy UTF-8 before returning; empty text is
* valid. Reads negotiate native text formats internally and complete with text
* borrowed for the event batch lifetime. NOT_FOUND means no text format.
* max_bytes bounds the UTF-8 result; excess returns BUFFER_TOO_SMALL, never a
* truncated string. A read remains async even when a backend can read instantly.
*/
UHDK_API uhdk_status UHDK_CALL uhdk_clipboard_write_text(uhdk_session *session, uhdk_text text);
UHDK_API uhdk_status UHDK_CALL uhdk_clipboard_read_text_async(
uhdk_session *session,
uint64_t max_bytes,
uhdk_request_tag tag
);
/* Incoming file drops only. When enabled, Uhdk negotiates copy-only file drops
* and emits DROP_FILES with borrowed file references plus the drop position.
* Retain references to use beyond the event batch lifetime. Directory references are allowed;
* the application decides whether to open them. No source deletion, outgoing
* drag, data offers, or application-visible drop-completion transaction.
*/
UHDK_API uhdk_status UHDK_CALL uhdk_window_set_drop_enabled(uhdk_window *window, uhdk_bool enabled);
/* Menus are immutable snapshots. Command IDs are application-assigned, nonzero
* and unique within a menu tree. Shortcuts use logical keys + modifiers.
* Created menus have one reference; attached menus are retained internally.
* Retain/release manage references; the final release destroys the menu.
*/
typedef uint32_t uhdk_menu_role;
enum {
UHDK_MENU_ROLE_NONE = 0,
UHDK_MENU_ROLE_ABOUT,
UHDK_MENU_ROLE_QUIT,
UHDK_MENU_ROLE_COPY,
UHDK_MENU_ROLE_CUT,
UHDK_MENU_ROLE_PASTE,
UHDK_MENU_ROLE_SELECT_ALL,
UHDK_MENU_ROLE_UNDO,
UHDK_MENU_ROLE_REDO
};
typedef uint32_t uhdk_menu_flags;
enum {
UHDK_MENU_DISABLED = 1u << 0,
UHDK_MENU_CHECKED = 1u << 1,
UHDK_MENU_SEPARATOR = 1u << 2
};
typedef struct uhdk_menu_item {
uint64_t command_id;
uhdk_text label;
uhdk_menu_flags flags;
uhdk_menu_role role;
uhdk_key shortcut_key;
uhdk_modifier_flags shortcut_modifiers;
uhdk_menu *submenu; /* Retained by the created parent menu. */
} uhdk_menu_item;
UHDK_API uhdk_status UHDK_CALL uhdk_menu_create(
uhdk_session *session,
const uhdk_menu_item *items,
uint64_t len,
uhdk_menu **out
);
UHDK_API uhdk_status UHDK_CALL uhdk_menu_set_item_flags(
uhdk_menu *menu,
uint64_t command_id,
uhdk_menu_flags flags
);
UHDK_API void UHDK_CALL uhdk_menu_retain(uhdk_menu *menu);
UHDK_API void UHDK_CALL uhdk_menu_release(uhdk_menu *menu);
UHDK_API uhdk_status UHDK_CALL uhdk_set_menu(
uhdk_session *session,
uhdk_menu *menu
); /* NULL clears. */
UHDK_API uhdk_status UHDK_CALL uhdk_window_set_menu(uhdk_window *window, uhdk_menu *menu);
UHDK_API uhdk_status UHDK_CALL uhdk_menu_popup_async(
uhdk_menu *menu,
uhdk_window *window,
uhdk_point position,
uhdk_request_tag tag
);
UHDK_API uhdk_status UHDK_CALL uhdk_set_icon(uhdk_session *session, const uhdk_image *image);
/* Tray configuration is a complete copied snapshot; menu is retained. All calls
* belong to OS main. Create/update run between frames (BUSY inside a frame).
* id is a nonempty application-defined identity, stable across updates/sessions
* and unique among this session's live items (at most 255 UTF-8 bytes).
* title/tooltip are plain UTF-8, at most 4096 bytes each; native display limits
* may truncate tooltips (Windows: 127 UTF-16 code units).
* icon is required, at most 512 x 512 RGBA8; backends scale it for their UI.
* template_image requests native monochrome rendering where supported (macOS).
* menu_on_primary opens the attached menu on primary activation where supported;
* otherwise primary activation emits TRAY_ACTIVATE. Secondary/context gestures
* follow desktop conventions. A menu is required when menu_on_primary is true.
* Tray menus support ordinary commands and QUIT; other roles are rejected.
* Their shortcuts are labels, not global keyboard bindings.
*/
typedef struct uhdk_tray_desc {
uint32_t struct_size;
uhdk_bool hidden;
uhdk_bool menu_on_primary;
uhdk_bool template_image;
uint8_t reserved;
uhdk_text id;
uhdk_text title;
uhdk_text tooltip;
uhdk_image icon;
uhdk_menu *menu;
} uhdk_tray_desc;
/* available means registered with a usable desktop host, not guaranteed visible
* on screen. Hidden items report false/OK. Pending or absent hosts report
* false/UNAVAILABLE. Linux registration is asynchronous and recovers after
* watcher/host restart; a session-bus disconnect requires reopening the session.
* Creation may succeed before a host exists; inspect state and TRAY_STATE events.
*/
typedef struct uhdk_tray_state {
uint32_t struct_size;
uhdk_status status;
uhdk_bool available;
} uhdk_tray_state;
UHDK_API uhdk_status UHDK_CALL uhdk_tray_create(
uhdk_session *session,
const uhdk_tray_desc *desc,
uhdk_tray **out
);
/* id cannot change. Validation/allocation failures preserve the old snapshot.
* Host failures are reported through state; accepted desired state is retained. */
UHDK_API uhdk_status UHDK_CALL uhdk_tray_update(uhdk_tray *tray, const uhdk_tray_desc *desc);
/* NULL safe. Invalidates the handle and suppresses queued tray events; native
* removal occurs at the next dispatch boundary. Does not close any windows. */
UHDK_API uhdk_status UHDK_CALL uhdk_tray_destroy(uhdk_tray *tray);
UHDK_API uhdk_status UHDK_CALL uhdk_tray_get_state(uhdk_tray *tray, uhdk_tray_state *out);
/* Successful shell completion means handed off to the OS, not that a target
* application finished loading. URLs are not shell command strings. */
UHDK_API uhdk_status UHDK_CALL uhdk_shell_open_url_async(
uhdk_session *session,
uhdk_text url,
uhdk_request_tag tag
);
typedef uint32_t uhdk_change_flags;
enum {
UHDK_CHANGE_CREATED = 1u << 0,
UHDK_CHANGE_REMOVED = 1u << 1,
UHDK_CHANGE_CONTENT = 1u << 2,
UHDK_CHANGE_METADATA = 1u << 3,
UHDK_CHANGE_RENAMED = 1u << 4,
UHDK_CHANGE_RESCAN_REQUIRED = 1u << 5,
UHDK_CHANGE_ROOT_INVALIDATED = 1u << 6
};
/* Watch a document path or a directory's immediate children; never recursive.
* Document watches follow the named path across atomic-save replacement, using
* parent-directory observation where needed. Removal emits REMOVED; subsequent
* recreation emits CREATED. If observation can no longer continue (for example,
* parent removal), emit ROOT_INVALIDATED and let the app recreate the watch.
* Changes may coalesce and are hints, not a transaction log. Renames may arrive
* as remove/create; no rename pairing is promised. RESCAN_REQUIRED means inspect
* current state. Pending notifications are suppressed after destroy; already borrowed batches remain valid. */
UHDK_API uhdk_status UHDK_CALL uhdk_watch_create_async(uhdk_file *root, uhdk_request_tag tag);
UHDK_API void UHDK_CALL uhdk_watch_destroy(uhdk_watch *watch);
typedef uint32_t uhdk_appearance;
enum {
UHDK_APPEARANCE_UNKNOWN = 0,
UHDK_APPEARANCE_LIGHT = 1,
UHDK_APPEARANCE_DARK = 2
};
typedef struct uhdk_preferences {
uint32_t struct_size;
uhdk_appearance appearance;
uhdk_bool reduced_motion;
uhdk_bool high_contrast;
} uhdk_preferences;
UHDK_API uhdk_status UHDK_CALL uhdk_get_preferences(uhdk_session *session, uhdk_preferences *out);
/* 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,
/* The input interaction identified by cycle_id reached its terminal state. */
UHDK_EVENT_CYCLE_END,
/* The desktop requested primary or secondary activation of a tray item. */
UHDK_EVENT_TRAY_ACTIVATE,
/* Tray host availability or registration status changed. */
UHDK_EVENT_TRAY_STATE
};
typedef uint32_t uhdk_request_kind;
enum {
UHDK_REQUEST_FILE_DIALOG = 1,
UHDK_REQUEST_CLIPBOARD_TEXT,
UHDK_REQUEST_MENU_POPUP,
UHDK_REQUEST_SHELL,
UHDK_REQUEST_WATCH,
UHDK_REQUEST_FILE_READ,
UHDK_REQUEST_FILE_WRITE,
UHDK_REQUEST_FILE_INFO
};
/* Semantic requests, not edits performed by UHDK. The application owns text,
* grapheme/word boundaries, bidi/layout, selection, clipboard actions and history.
* LEFT/RIGHT are visual; BACKWARD/FORWARD follow logical document order.
* LINE uses visual lines; LEFT_END/RIGHT_END explicitly select their visual edge.
* LINE_START/END follow writing direction; PARAGRAPH uses document boundaries.
* PAGE moves the caret by a viewport; SCROLL moves the viewport without the caret.
* Deletion first removes the selection; otherwise it removes the indicated unit.
* NEWLINE requests a paragraph separator; LINE_BREAK requests a soft line break.
* TAB/BACKTAB may insert text or traverse focus, as decided by the application.
* CANCEL is delivered only after native composition handling declines/finishes it.
* Native default gestures vary: macOS uses AppKit selectors, Windows/Wayland
* use Ctrl-based editing defaults. Commands without customary platform gestures
* remain application-bindable intents, not invented global shortcuts. */
typedef uint32_t uhdk_edit_command;
enum {
UHDK_EDIT_COPY = 1,
UHDK_EDIT_CUT,
UHDK_EDIT_PASTE,
UHDK_EDIT_SELECT_ALL,
UHDK_EDIT_UNDO,
UHDK_EDIT_REDO,
UHDK_EDIT_DELETE_BACKWARD,
UHDK_EDIT_DELETE_FORWARD,
UHDK_EDIT_MOVE_LEFT,
UHDK_EDIT_MOVE_RIGHT,
UHDK_EDIT_MOVE_UP,
UHDK_EDIT_MOVE_DOWN,
UHDK_EDIT_LINE_START,
UHDK_EDIT_LINE_END,
UHDK_EDIT_DOCUMENT_START,
UHDK_EDIT_DOCUMENT_END,
UHDK_EDIT_WORD_LEFT,
UHDK_EDIT_WORD_RIGHT,
UHDK_EDIT_NEWLINE,
UHDK_EDIT_TAB,
UHDK_EDIT_BACKTAB,
UHDK_EDIT_LINE_BREAK,
UHDK_EDIT_CANCEL,
UHDK_EDIT_DELETE_WORD_BACKWARD,
UHDK_EDIT_DELETE_WORD_FORWARD,
UHDK_EDIT_DELETE_LINE_START,
UHDK_EDIT_DELETE_LINE_END,
UHDK_EDIT_DELETE_PARAGRAPH_START,
UHDK_EDIT_DELETE_PARAGRAPH_END,
UHDK_EDIT_MOVE_BACKWARD,
UHDK_EDIT_MOVE_FORWARD,
UHDK_EDIT_WORD_BACKWARD,
UHDK_EDIT_WORD_FORWARD,
UHDK_EDIT_PARAGRAPH_START,
UHDK_EDIT_PARAGRAPH_END,
UHDK_EDIT_PAGE_UP,
UHDK_EDIT_PAGE_DOWN,
UHDK_EDIT_SCROLL_PAGE_UP,
UHDK_EDIT_SCROLL_PAGE_DOWN,
UHDK_EDIT_SCROLL_DOCUMENT_START,
UHDK_EDIT_SCROLL_DOCUMENT_END,
UHDK_EDIT_LINE_LEFT_END,
UHDK_EDIT_LINE_RIGHT_END
};
/* Event data is a pointer to the named payload type selected by type.
* All payloads are immutable and naturally aligned. data_size is the payload
* size in bytes; check it before accessing fields introduced by newer versions.
* Payload-free events have NULL data and data_size == 0. Unknown types may be
* ignored. Event envelopes have a fixed stride within each returned batch.
*/
typedef struct uhdk_key_event {
uint32_t physical_key;
uhdk_key logical_key;
uhdk_modifier_flags modifiers;
uhdk_bool pressed;
uhdk_bool repeat;
} uhdk_key_event;
/* Pointer position and delta use logical window units. POINTER_MOVE events may
* continue outside the window while pointer capture is active. pressed and
* button are meaningful for POINTER_BUTTON; movement and enter/leave events
* retain the current position and modifiers. The event envelope's cycle_id
* associates a long-lived drag/capture interaction with its CYCLE_END marker. */
typedef struct uhdk_pointer_event {
uhdk_point position;
uhdk_point delta; /* delta uses logical units. */
uhdk_pointer_button button;
uhdk_modifier_flags modifiers;
uhdk_bool pressed;
uint32_t click_count;
} uhdk_pointer_event;
/* Scroll deltas use the declared unit and phase. A phase-aware gesture shares
* cycle_id across BEGIN, UPDATE and END/CANCEL events; discrete wheel ticks use
* cycle_id=0. Positive delta means right/down in logical coordinates. */
typedef struct uhdk_scroll_event {
uhdk_point position;
uhdk_point delta; /* Positive scroll = toward right/bottom. */
uhdk_scroll_unit unit;
uhdk_scroll_phase phase;
uhdk_modifier_flags modifiers;
uhdk_bool momentum;
} uhdk_scroll_event;
/* TEXT_COMMIT or TEXT_PREEDIT payload.
*
* `text` is UTF-8. When `has_replacement` is true, replace
* `replacement_start`/`replacement_len` in the current
* [`uhdk_text_state`](api:uhdk_text_state) window, then insert `text`. Those
* ranges are relative to `surrounding_text`.
*
* PREEDIT updates temporary composition only; it does not change the
* permanent document by itself. COMMIT inserts into the document. Apply
* events in delivery order; see [`uhdk_text_state`](api:uhdk_text_state).
*/
typedef struct uhdk_text_event {
uhdk_text text;
uint64_t revision; /* Snapshot revision this event is based on. */
uhdk_bool has_replacement;
uint32_t reserved;
uint64_t replacement_start; /* Relative to surrounding_text. */
uint64_t replacement_len; /* Relative to surrounding_text. */
uint64_t selection_start;
uint64_t selection_len; /* Within preedit text. */
} uhdk_text_event;
typedef struct uhdk_edit_event {
uhdk_edit_command command;
uhdk_bool extend_selection; /* Only meaningful for caret movement. */
} uhdk_edit_event;
typedef struct uhdk_drop_files_event {
uhdk_file_list files;
uhdk_point position;
} uhdk_drop_files_event;
typedef struct uhdk_menu_event {
uint64_t command_id;
uhdk_tray *tray; /* NULL for application/window/context menus. */
} uhdk_menu_event;
typedef uint32_t uhdk_tray_activation;
enum {
UHDK_TRAY_ACTIVATE_PRIMARY = 0,
UHDK_TRAY_ACTIVATE_SECONDARY = 1
};
typedef struct uhdk_tray_event {
uhdk_tray *tray;
uhdk_tray_activation activation;
} uhdk_tray_event;
typedef struct uhdk_tray_state_event {
uhdk_tray *tray;
uhdk_tray_state state;
} uhdk_tray_state_event;
typedef struct uhdk_watch_event {
uhdk_watch *watch;
uhdk_change_flags flags;
uhdk_status status; /* Non-OK: watch failure; rescan/recreate as needed. */
uhdk_file *file; /* Nullable when only the watched root is known. */
} uhdk_watch_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;
typedef struct uhdk_watch_result {
uhdk_watch *watch;
} uhdk_watch_result;
/* Request data mapping (only valid on UHDK_STATUS_OK):
* FILE_DIALOG: uhdk_file_list; CLIPBOARD_TEXT: uhdk_text;
* WATCH: uhdk_watch_result; FILE_READ: uhdk_bytes; FILE_INFO: uhdk_file_info.
* SHELL, FILE_WRITE, MENU_POPUP and failures have NULL/0 data.
*/
typedef struct uhdk_request_result {
uhdk_request_tag tag;
uhdk_request_kind kind;
uhdk_status status;
uhdk_text diagnostic; /* Nonempty on failure: native detail or status_message fallback. */
const void *data;
uint64_t data_size;
} uhdk_request_result;
typedef uint32_t uhdk_cycle_end_reason;
enum {
UHDK_CYCLE_COMPLETED = 0, /* Last button release, scroll end, or committed composition. */
UHDK_CYCLE_CANCELLED, /* Native scroll cancellation or composition ended without commit. */
UHDK_CYCLE_CAPTURE_RELEASED, /* Application released capture or confinement. */
UHDK_CYCLE_CAPTURE_LOST, /* Capture or confinement was interrupted externally. */
UHDK_CYCLE_FOCUS_LOST, /* Window focus or application activation was lost. */
UHDK_CYCLE_HIDDEN, /* Window was hidden; takes precedence over simultaneous focus loss. */
UHDK_CYCLE_SUPERSEDED, /* A new native gesture replaced an unfinished one. */
UHDK_CYCLE_SHUTDOWN /* Session is draining normally. */
};
/* The terminal reason for the interaction identified by event.cycle_id. The
* marker retires application state; it does not decide whether application data
* should be committed or rolled back. */
typedef struct uhdk_cycle_end_event {
uhdk_cycle_end_reason reason;
} uhdk_cycle_end_event;
/* One event from a next_frame batch.
*
* Cast `data` by `type`; see [Event payloads](#event-payloads). Events omitted
* there have NULL `data` and zero `data_size`. Payloads are borrowed until the
* next next_frame or close.
*
* `window` is NULL for app-wide events. `time_ns` is monotonic time since
* session creation, not wall time.
*
* `cycle_id` groups a pointer drag/capture/confinement, phased scroll, or visible IME
* preedit. Zero means no cycle. It is not a request tag or `input_id`, and it
* is never reused in a session. See [Input lifetimes](#input-lifetimes).
*
* Async completions arrive as REQUEST_DONE; see
* [`uhdk_request_tag`](api:uhdk_request_tag).
*
* In C and C++, UHDK_EVENT_PAYLOAD returns a typed pointer only when both type
* and data_size match. Pass a stable event pointer expression because the macro
* may evaluate it more than once:
*
* ```c
* const uhdk_key_event *key =
* UHDK_EVENT_PAYLOAD(event, UHDK_EVENT_KEY, uhdk_key_event);
* if (key != NULL) { ... }
* ```
*
* Environments that do not expose C macros perform the equivalent operation by
* checking type, checking data_size, and only then casting data:
*
* ```c
* if (event->type == UHDK_EVENT_KEY &&
* event->data_size >= sizeof(uhdk_key_event)) {
* const uhdk_key_event *key = (const uhdk_key_event *)event->data;
* }
* ```
*/
struct uhdk_event {
uint32_t struct_size;
uhdk_event_type type;
uint64_t time_ns;
uint64_t input_id; /* Correlates raw key and native edit/text; zero for non-input. */
uint64_t cycle_id; /* Session-unique library identity for an input lifecycle; zero otherwise. */
uhdk_window *window;
const void *data;
uint64_t data_size;
};
/* Lifecycle policy: close/quit requests are advisory. Destroy the window or call
* uhdk_quit() to accept; otherwise the application stays alive. Closing the
* primary window closes only through quit/close; additional windows can be destroyed. Session close invalidates ALL child
* handles, including retained files/menus; callers must stop other threads from
* waking the app first. Native menu roles produce EDIT_COMMAND/QUIT_REQUESTED
* where applicable; custom menu items produce MENU_COMMAND. A popup selection
* delivers its command event first and REQUEST_DONE afterward; process the
* command only once. Popup dismissal completes as UHDK_STATUS_CANCELLED.
*
* Boundaries: no widget/layout engine, text rasterizer, high-level renderer,
* networking, process spawning, global input hooks, or streaming filesystem API.
* Deferred: generic clipboard data, outgoing drag/drop, custom cursors, relative
* pointer input, transparent windows, display enumeration/positioning,
* badges/progress/attention, recursive watching, accent colors, accessibility
* semantic trees, notifications, persistent access bookmarks and streaming data.
*/
#ifdef UHDK_ENABLE_NATIVE
/* Experimental platform escape hatch; native handles may require OS headers. */
/* Platform escape hatch. Returned handles are borrowed and must not be released.
* macOS: primary=NSWindow*, secondary=NSView*; Windows: primary=HWND.
* Wayland: primary=wl_display*, secondary=wl_surface*.
* X11: primary=Display*, value=XID. Lifetime ends with the Uhdk window.
* For the GL session primary window, Uhdk owns drawable setup; do not replace
* its native view/surface or pixel format. Otherwise the renderer owns setup.
* Borrowing a handle does not change platform threading rules: AppKit window
* and view operations still belong on the macOS main thread.
*/
typedef struct uhdk_native_window {
uint32_t struct_size;
uhdk_backend backend;
void *primary;
void *secondary;
uint64_t value;
} uhdk_native_window;
UHDK_API uhdk_status UHDK_CALL uhdk_window_get_native(uhdk_window *window, uhdk_native_window *out);
#endif /* UHDK_ENABLE_NATIVE */
#ifdef __cplusplus
} /* extern "C" */
#endif
#ifdef UHDK_ENABLE_OPENGL
/* GLAD 2: OpenGL 3.3 core function-table declarations. UHDK loads and owns the
* table returned by get_gl/frame.gl. No consumer loader implementation is needed.
* Do not combine with platform GL headers or GL/glcorearb.h. */
#include "glad/gl.h"
#endif /* UHDK_ENABLE_OPENGL */
#endif /* UHDK_H_INCLUDED */