diff --git a/include/uhdk.h b/include/uhdk.h index 4eee811..3a66f91 100644 --- a/include/uhdk.h +++ b/include/uhdk.h @@ -67,6 +67,9 @@ * not allocate/free memory themselves, intercept malloc, or transfer ownership. */ +/* Typed event payload convenience; see uhdk_event. */ +#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) + #define UHDK_ABI_VERSION 1u #ifdef UHDK_PROFILE_BACKEND @@ -134,8 +137,13 @@ typedef struct uhdk_window uhdk_window; typedef struct uhdk_file uhdk_file; typedef struct uhdk_watch uhdk_watch; typedef struct uhdk_menu uhdk_menu; -typedef uint64_t - uhdk_request_tag; /* Caller-chosen, nonzero; unique until completion is delivered. */ +/* Caller-chosen, nonzero. Unique in the session until REQUEST_DONE is delivered. + * Duplicates return BUSY, including while a completion is queued. A failed submit + * reserves nothing. Cancel is best effort and does not free the tag. Reuse only + * after handling the completion, or after consuming or ignoring it if the owner + * is gone. See [Asynchronous services](#asynchronous-services). + */ +typedef uint64_t uhdk_request_tag; typedef struct uhdk_text { const char *ptr; @@ -210,26 +218,6 @@ enum { UHDK_CAP_OPENGL = 18 }; -/* Every accepted async request uses a caller-chosen nonzero tag and produces one terminal - * REQUEST_DONE during normal draining. Failure to accept produces no event. - * Tags are unique per session until REQUEST_DONE is delivered by next_frame. - * Duplicate tags return BUSY, including while completion is queued. Zero is invalid. - * Tags may be reused while handling their completion. Failure to accept reserves - * no tag and produces no completion. Private native IDs are never reused. - * Cancellation is best effort; an operation already committed may succeed. - * Closing a session early abandons undelivered results and drains internal work. - * - * Component lifetime pattern: the application assigns dedicated tags to reusable - * components (for example, a clipboard tag passed to a UI backend at init). - * All components share the session's tag namespace; use separate tags for requests - * that may overlap. Keep each tag assigned to its component while it is alive. - * If a component is destroyed with a request pending, its owner must consume or - * ignore the eventual REQUEST_DONE before assigning that tag to a replacement. - * Cancelling the request does not release the tag or remove its completion. - * This prevents a replacement component from consuming its predecessor's result. - * Result payloads are borrowed until the next valid next_frame call or close; - * copy data or retain retainable references before keeping them beyond that point. - */ typedef struct uhdk_event uhdk_event; typedef struct uhdk_event_list { const uhdk_event *ptr; @@ -943,6 +931,11 @@ typedef struct uhdk_key_event { 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. */ @@ -952,6 +945,9 @@ typedef struct uhdk_pointer_event { 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. */ @@ -1029,36 +1025,48 @@ enum { 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; -/* cycle_id groups a pointer drag/capture, phased scroll gesture, or visible IME - * preedit composition. Independent kinds/windows never share IDs; IDs are not - * request tags or input_id values and are never reused within a session. - * A pointer cycle starts on first button-down or capture activation. It ends - * after the last release without capture, or capture is released/lost. Hover, - * discrete wheel ticks and ordinary typing have cycle_id=0. - * Each ended cycle produces CYCLE_END with its ID, window and cycle_end_event. This - * covers normal completion, cancellation, focus loss, hiding and normal shutdown. - * Markers follow native ending events; session deactivation and shutdown emit - * them before APP_ACTIVATION(false) and STOPPED. The reason reports the first - * observed terminating cause; later native cleanup does not replace it. They signal retirement, not - * whether an application operation should be committed or rolled back. - * Destroying a window or closing a session invalidates its cycles immediately; - * queued events may be discarded. Fatal errors can interrupt delivery. - */ - -/* Event data mapping: - * APP_ACTIVATION: uhdk_activation_event; OPEN_FILES: uhdk_file_list; - * WINDOW_METRICS: uhdk_window_metrics; PREFERENCES_CHANGED: uhdk_preferences; - * KEY: uhdk_key_event; POINTER_ENTER/LEAVE/MOVE/BUTTON: uhdk_pointer_event; - * POINTER_MODE_CHANGED: uhdk_pointer_mode_event; SCROLL: uhdk_scroll_event; - * TEXT_COMMIT/PREEDIT: uhdk_text_event; EDIT_COMMAND: uhdk_edit_event; - * DROP_FILES: uhdk_drop_files_event; MENU_COMMAND: uhdk_menu_event; - * WATCH: uhdk_watch_event; - * REQUEST_DONE: uhdk_request_result; CYCLE_END: uhdk_cycle_end_event. - * All other events have NULL/0 data. +/* 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, 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;