AtlatestRepositorysigil-ui

sigil-ui / tree / nativewayland-bridge.h

1#ifndef SIGIL_UI_WAYLAND_BRIDGE_H
2#define SIGIL_UI_WAYLAND_BRIDGE_H
3
4#include <sigil/sigil.h>
5#include <stddef.h>
6#include <stdint.h>
7#include <sys/types.h>
8
9/* Native embedders use the same connection and dispatcher as the Sigil UI.
10 * Calls and callbacks run on the owning thread. No protocol pointer escapes.
11 * An acquired surface stays allocated after close, but rejects presentation.
12 * Release callbacks run exactly once, after compositor release or teardown;
13 * frame callbacks report presented=0 when teardown cancels a pending frame.
14 * A successful present owns the callback data until both notifications run;
15 * a rejected present never calls either callback. */
16typedef struct Wl SuiWaylandSurface;
17/* Closing the parent from a completion callback marks it closed immediately;
18 * teardown/disconnect finish when the outermost GUI dispatch or child
19 * teardown unwinds. Callers
20 * may drop their final handle references inside that callback. */
21typedef void (*SuiWaylandRelease)(void *data);
22typedef void (*SuiWaylandFrame)(void *data, uint32_t milliseconds, int presented);
24/* Optional v4 surface feedback. Enabling never dispatches or selects a GPU.
25 * Snapshots own copied data and outlive updates, surface close and disconnect.
26 * All calls, including snapshot ref/unref, belong to the owning thread.
27 * Partial updates keep the last completed snapshot. An invalid completed
28 * update revokes it; acquire then returns NULL until a valid update arrives.
29 * Generation advances for every completed update or capability revocation;
30 * a closed, cleaned-up handle reports unavailable with generation zero. */
31typedef struct SuiWaylandDmabufFeedback SuiWaylandDmabufFeedback;
32enum {
33 SUI_DMABUF_UNAVAILABLE, SUI_DMABUF_PENDING, SUI_DMABUF_READY, SUI_DMABUF_INVALID
34};
35int sui_wayland_dmabuf_feedback_enable(SuiWaylandSurface *surface);
36int sui_wayland_dmabuf_feedback_status(SuiWaylandSurface *surface, uint64_t *generation);
37SuiWaylandDmabufFeedback *sui_wayland_dmabuf_feedback_acquire(SuiWaylandSurface *surface);
38SuiWaylandDmabufFeedback *sui_wayland_dmabuf_feedback_ref(SuiWaylandDmabufFeedback *feedback);
39void sui_wayland_dmabuf_feedback_unref(SuiWaylandDmabufFeedback *feedback);
40uint64_t sui_wayland_dmabuf_feedback_generation(const SuiWaylandDmabufFeedback *feedback);
41dev_t sui_wayland_dmabuf_feedback_main_device(const SuiWaylandDmabufFeedback *feedback);
42size_t sui_wayland_dmabuf_feedback_tranche_count(const SuiWaylandDmabufFeedback *feedback);
43int sui_wayland_dmabuf_feedback_tranche(const SuiWaylandDmabufFeedback *feedback,
44 size_t tranche, dev_t *device, uint32_t *flags, size_t *formats);
45int sui_wayland_dmabuf_feedback_format(const SuiWaylandDmabufFeedback *feedback,
46 size_t tranche, size_t index, uint32_t *fourcc, uint64_t *modifier);
48/* Asynchronous import only; this API never attaches a buffer. Input plane
49 * descriptors are borrowed and remain the caller's property. Each handle
50 * retains the connection, survives child-surface teardown, and must be
51 * unref'd by its caller. Connection close cancels every surviving handle.
52 * Pending imports may be canceled explicitly; no dispatch or wait occurs.
53 * Cancellation retains a bounded private listener until created/failed or
54 * connection close, so a late created buffer can be destroyed correctly.
55 * Calls belong to the connection's owning thread. At most 64 handles may
56 * coexist on one connection. Each plane's span must fit in 32 bits and
57 * lie within its DMA-BUF's real size, as the kernel reports it; the first
58 * plane's offset + stride * height must fit too. These are the compositor's own import
59 * checks, so a refused plane never reaches it as a fatal protocol error.
60 * Creation returns NULL with errno on refusal. */
61typedef struct SuiWaylandDmabufImport SuiWaylandDmabufImport;
62typedef struct { int fd; uint32_t offset, stride; } SuiWaylandDmabufPlane;
63typedef struct {
64 int width, height;
65 uint32_t fourcc, n_planes;
66 uint64_t modifier;
67 SuiWaylandDmabufPlane planes[4];
68} SuiWaylandDmabufSpec;
69enum {
70 SUI_DMABUF_IMPORT_PENDING, SUI_DMABUF_IMPORT_READY,
71 SUI_DMABUF_IMPORT_FAILED, SUI_DMABUF_IMPORT_CANCELED,
72 SUI_DMABUF_IMPORT_FENCE_PENDING, SUI_DMABUF_IMPORT_FENCE_FAILED,
73 SUI_DMABUF_IMPORT_FENCE_TIMED_OUT
74};
75SuiWaylandDmabufImport *sui_wayland_dmabuf_import(SuiWaylandSurface *surface,
76 const SuiWaylandDmabufSpec *spec);
77SuiWaylandDmabufImport *sui_wayland_dmabuf_import_ref(SuiWaylandDmabufImport *buffer);
78void sui_wayland_dmabuf_import_unref(SuiWaylandDmabufImport *buffer);
79void sui_wayland_dmabuf_import_cancel(SuiWaylandDmabufImport *buffer);
80int sui_wayland_dmabuf_import_status(const SuiWaylandDmabufImport *buffer);
81/* Gate an idle READY import on producer readiness. fd is borrowed; a private
82 * CLOEXEC duplicate is closed on ready/error/timeout/cancel/teardown. The GUI
83 * dispatcher advances FENCE_PENDING without blocking input or protocol reads.
84 * timeout_ms is 1..5000. POLLIN without error means ready; any terminal error
85 * or deadline prevents presentation. An error/canceled wait does not establish
86 * that producer rendering finished or permit reuse. Only a producer fence may
87 * be supplied; this API neither validates a GPU allocation nor enables an
88 * explicit-sync protocol. Returns 1 on acceptance, 0 with errno on refusal. */
89int sui_wayland_dmabuf_import_fence(SuiWaylandDmabufImport *buffer,
90 int fd, unsigned timeout_ms);
92/* DMA-BUF release is distinct from frame pacing and SHM teardown. released=1
93 * means an actual compositor release; released=0 means connection teardown
94 * has disconnected and the producer must be retired, never reused. Child
95 * close cancels its frame callback but retains submitted imports until their
96 * real release or connection teardown. Cancel/unref cannot destroy a busy
97 * buffer. Success retains data until both notifications; refusal calls neither.
98 * Only READY same-connection imports with exact device-pixel dimensions can
99 * be submitted. No rendering-fence wait is provided by this call: the producer
100 * must already satisfy the implicit synchronization contract. */
101typedef void (*SuiWaylandDmabufRelease)(void *data, int released);
102int sui_wayland_present_dmabuf(SuiWaylandSurface *surface,
103 SuiWaylandDmabufImport *buffer, SuiWaylandDmabufRelease released,
104 SuiWaylandFrame frame, void *data);
106SuiWaylandSurface *sui_wayland_acquire(SigilVM *vm, Value native_handle);
107void sui_wayland_unref(SuiWaylandSurface *surface);
108SuiWaylandSurface *sui_wayland_child(SuiWaylandSurface *parent, int x, int y,
109 int width, int height, int overlay);
110void sui_wayland_close(SuiWaylandSurface *surface);
111int sui_wayland_size(SuiWaylandSurface *surface, int *width, int *height, int *scale);
112/* Start an xdg-foreign v2 export on a live xdg-toplevel. Only ordinary
113 * UI dispatch receives the handle. Returns NULL while pending or unavailable.
114 * A returned string is borrowed until the next dispatch or surface close. */
115const char *sui_wayland_export_handle(SuiWaylandSurface *surface);
116/* Cursor requests affect the shared connection, including requests from a
117 * child. They copy desired state, never dispatch, and apply only with a live
118 * pointer-enter serial. Calls must use the connection's owning thread.
119 * A name alone prefers its mapped compositor shape, then the default loader.
120 * An explicit theme or positive size prefers the configured loader, then a
121 * mapped shape. NULL theme with size 0 clears that explicit preference.
122 * For an explicit size without a theme, NULL selects XCURSOR_THEME/default;
123 * size 0 selects valid XCURSOR_SIZE/24. Size is 1..128 logical pixels.
124 * The loader uses frame one; builtin and theme origins are indistinguishable.
125 * Images are premultiplied native ARGB8888, at most 512x512/1 MiB, copied now;
126 * image scale is 1..8 and divides both dimensions; hotspots are device pixels
127 * strictly inside the image.
128 * False reports invalid/closed/wrong-thread or currently unavailable output.
129 * True outside pointer focus means accepted for the next pointer entry. */
130int sui_wayland_cursor_configure(SuiWaylandSurface *surface, const char *theme, int size);
131int sui_wayland_cursor_name(SuiWaylandSurface *surface, const char *name);
132int sui_wayland_cursor_image(SuiWaylandSurface *surface, const uint8_t *bytes,
133 size_t length, int width, int height, size_t stride, int hotspot_x, int hotspot_y, int scale);
134enum { SUI_CURSOR_UNAVAILABLE, SUI_CURSOR_THEME, SUI_CURSOR_SHAPE, SUI_CURSOR_IMAGE, SUI_CURSOR_HIDDEN };
135int sui_wayland_cursor_backend(SuiWaylandSurface *surface);
136/* Requests never dispatch or claim compositor acceptance. State is 0/1 only
137 * after xdg_surface.configure; -1 means no live xdg-toplevel. */
138int sui_wayland_set_fullscreen(SuiWaylandSurface *surface, int fullscreen);
139int sui_wayland_fullscreen_state(SuiWaylandSurface *surface);
140/* Child geometry is in logical pixels; changing it retires old buffers on
141 * the next present. Modifier masks are the compositor's XKB masks/group. */
142int sui_wayland_geometry(SuiWaylandSurface *surface, int x, int y, int width, int height);
143void sui_wayland_modifiers(SuiWaylandSurface *surface, uint32_t masks[4]);
144const char *sui_wayland_keymap(SuiWaylandSurface *surface, size_t *size);
145/* Monotonic selection generation and whether readable text is offered. */
146uint64_t sui_wayland_clipboard_generation(SuiWaylandSurface *surface, int *has_text);
147/* Text only; at most 4 MiB. Setting requires a compositor input serial.
148 * Read returns malloc-owned bytes, or NULL on missing selection/error/timeout. */
149int sui_wayland_clipboard_set(SuiWaylandSurface *surface, const uint8_t *bytes, size_t size);
150uint8_t *sui_wayland_clipboard_read(SuiWaylandSurface *surface, size_t *size);
151int sui_wayland_present_shm(SuiWaylandSurface *surface, const uint8_t *pixels,
152 int width, int height, size_t stride,
153 SuiWaylandRelease released, SuiWaylandFrame frame,
154 void *data);
156#endif