Commit29b3ce98Recorded25 Nov 2026Repositorysigil-ui

Expose xdg-foreign toplevel handles to native embedders

Message

sui_wayland_export_handle exports a live xdg-toplevel through zxdg_exporter_v2 so an embedder can pass it as the parent window of a portal dialog. It returns NULL until the compositor sends the handle, or when the protocol is missing, so callers keep their empty-parent path. The protocol tables stay private to the bridge.

Release 0.2.9.

Changed
 NEWS.md                 | 10 ++++++++++
 native/wayland-bridge.h |  4 ++++
 native/wayland.c        | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 package.sgl             |  2 +-
 4 files changed, 79 insertions(+), 1 deletion(-)
Diff
NEWS.mdmodified
@@ -1,3 +1,13 @@
+1
# sigil-ui 0.2.9
+2
+3
- Add `sui_wayland_export_handle`, which exports a live xdg-toplevel through
+4
xdg-foreign v2 (`zxdg_exporter_v2`) so a native embedder can name the
+5
window as the parent of a portal dialog. It returns NULL until the
+6
compositor sends the handle, and also when the compositor lacks the
+7
protocol, so callers keep their empty-parent fallback.
+8
- The protocol tables stay private to the bridge. Requirements are
+9
unchanged from 0.2.8.
+10
11
# sigil-ui 0.2.8
12
13
- Require sigil-window 0.2.2 or later in the 0.2 series. The Linux native
native/wayland-bridge.hmodified
@@ -109,6 +109,10 @@ SuiWaylandSurface *sui_wayland_child(SuiWaylandSurface *parent, int x, int y,
109
int width, int height, int overlay);
110
void sui_wayland_close(SuiWaylandSurface *surface);
111
int 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. */
+115
const 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.
native/wayland.cmodified
@@ -79,6 +79,22 @@
79
80
#include "xkb.h"
81
+82
/* Exporter half of xdg-foreign-unstable-v2, wire version 1. Keep these
+83
* tables private so another library can link its own protocol tables. */
+84
static const struct wl_message exported_requests[] = { { "destroy", "", NULL } };
+85
static const struct wl_message exported_events[] = { { "handle", "s", NULL } };
+86
static const struct wl_interface exported_interface = {
+87
"zxdg_exported_v2", 1, 1, exported_requests, 1, exported_events
+88
};
+89
static const struct wl_interface *export_types[] = { &exported_interface, &wl_surface_interface };
+90
static const struct wl_message exporter_requests[] = {
+91
{ "destroy", "", NULL },
+92
{ "export_toplevel", "no", export_types }
+93
};
+94
static const struct wl_interface exporter_interface = {
+95
"zxdg_exporter_v2", 1, 2, exporter_requests, 0, NULL
+96
};
+97
98
#define REGISTER_AND_EXPORT(name, func, arity, doc) \
99
sigil_module_register_native(vm, name, func, arity, doc); \
100
sigil_module_export(vm, name)
@@ -274,6 +290,9 @@ typedef struct Wl {
290
ClipboardWrite writes[CLIPBOARD_WRITES];
291
struct wl_display *display;
292
struct wl_registry *registry;
+293
uint32_t exporter_global;
+294
struct wl_proxy *exporter, *exported;
+295
char *export_handle;
296
struct zwp_linux_dmabuf_v1 *dmabuf;
297
uint32_t dmabuf_global, dmabuf_version;
298
DmabufFeedbackState *dmabuf_feedback;
@@ -1065,6 +1084,28 @@ static int ignored(const char *iface)
1084
return 0;
1085
}
1086
+1087
static void export_close(Wl *w)
+1088
{
+1089
if (w->exported)
+1090
wl_proxy_marshal_flags(w->exported, 0, NULL, 1, WL_MARSHAL_FLAG_DESTROY);
+1091
if (w->exporter)
+1092
wl_proxy_marshal_flags(w->exporter, 0, NULL, 1, WL_MARSHAL_FLAG_DESTROY);
+1093
w->exported = w->exporter = NULL;
+1094
free(w->export_handle);
+1095
w->export_handle = NULL;
+1096
}
+1097
+1098
static void export_ready(void *data, struct wl_proxy *exported, const char *handle)
+1099
{
+1100
Wl *w = data;
+1101
if (w->closed || w->exported != exported || w->export_handle || !handle || !*handle) return;
+1102
w->export_handle = strdup(handle);
+1103
}
+1104
+1105
static const struct {
+1106
void (*handle)(void *, struct wl_proxy *, const char *);
+1107
} export_listener = { .handle = export_ready };
+1108
1109
static void global(void *data, struct wl_registry *r, uint32_t id, const char *iface, uint32_t version)
1110
{
1111
Wl *w = data;
@@ -1073,6 +1114,8 @@ static void global(void *data, struct wl_registry *r, uint32_t id, const char *i
1114
w->compositor = wl_registry_bind(r, id, &wl_compositor_interface, min_u(version, 6));
1115
else if (!strcmp(iface, wl_subcompositor_interface.name) && !w->subcompositor)
1116
w->subcompositor = wl_registry_bind(r, id, &wl_subcompositor_interface, 1);
+1117
else if (!strcmp(iface, exporter_interface.name) && !w->exporter_global)
+1118
w->exporter_global = id;
1119
else if (!strcmp(iface, zwp_linux_dmabuf_v1_interface.name) && !w->dmabuf_global) {
1120
/* Record availability only; SHM clients never bind this global. */
1121
w->dmabuf_global = id;
@@ -1115,6 +1158,10 @@ static void global(void *data, struct wl_registry *r, uint32_t id, const char *i
1158
static void global_remove(void *data, struct wl_registry *r, uint32_t id)
1159
{
1160
Wl *w = data;
+1161
if (id == w->exporter_global) {
+1162
export_close(w);
+1163
w->exporter_global = 0;
+1164
}
1165
if (id == w->dmabuf_global) {
1166
dmabuf_imports_revoke_pending(w);
1167
dmabuf_feedback_revoke_tree(w);
@@ -1181,6 +1228,7 @@ static void wl_teardown(Wl *w)
1228
if (w->surface) wl_surface_destroy(w->surface);
1229
goto closed;
1230
}
+1231
export_close(w);
1232
if (w->layer) zwlr_layer_surface_v1_destroy(w->layer);
1233
if (w->decoration) zxdg_toplevel_decoration_v1_destroy(w->decoration);
1234
if (w->toplevel) xdg_toplevel_destroy(w->toplevel);
@@ -1303,6 +1351,22 @@ int sui_wayland_size(SuiWaylandSurface *w, int *width, int *height, int *scale)
1351
return 1;
1352
}
1353
+1354
const char *sui_wayland_export_handle(SuiWaylandSurface *w)
+1355
{
+1356
if (!w || !w->display || w->parent || !w->toplevel || !w->exporter_global || w->dead || w->closed)
+1357
return NULL;
+1358
if (w->exported) return w->export_handle;
+1359
if (!w->exporter)
+1360
w->exporter = wl_registry_bind(w->registry, w->exporter_global, &exporter_interface, 1);
+1361
if (!w->exporter) return NULL;
+1362
w->exported = wl_proxy_marshal_flags(w->exporter, 1, &exported_interface, 1, 0, NULL, w->surface);
+1363
if (w->exported)
+1364
wl_proxy_add_listener(w->exported, (void (**)(void))&export_listener, w);
+1365
if (wl_display_flush(w->display) < 0 && errno != EAGAIN)
+1366
fail(w, "the compositor connection failed");
+1367
return NULL;
+1368
}
+1369
1370
int sui_wayland_fullscreen_state(SuiWaylandSurface *w)
1371
{
1372
if (!w || !w->display || !w->toplevel || w->parent || w->dead || w->closed) return -1;
package.sglmodified
@@ -29,7 +29,7 @@
29
30
(package
31
name: "sigil-ui"
−32
version: "0.2.8"
+32
version: "0.2.9"
33
sigil: "0.24.8"
34
description: "Retained-mode UI framework with a software renderer"
35
url: "https://git.usesigil.org/sigil/sigil-ui"