Commita5c37fccRecorded25 Nov 2026Repositorysigil-ui

Report Wayland presentation for tracked canvas commits

Message

Expose wp_presentation feedback, with a frame-callback fallback, as a receipt for the newest tracked commit on a root surface, and release sigil-ui 0.2.10.

Changed
 NEWS.md                  |  11 +++++++++++
 native/wayland.c         | 146 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
 package.sgl              |   2 +-
 src/sigil/ui/wayland.sgl |  17 +++++++++++++++++
 4 files changed, 170 insertions(+), 6 deletions(-)
Diff
NEWS.mdmodified
@@ -1,3 +1,14 @@
+1
# sigil-ui 0.2.10
+2
+3
- Add `wayland-present-tracked!` and `wayland-presentation-status`. A
+4
tracked commit of a canvas on a root surface returns a serial, and its
+5
status reads `pending`, `presented` (from `wp_presentation` feedback),
+6
`frame` (a frame callback, when the compositor lacks presentation time)
+7
or `discarded`. Only the newest tracked commit keeps a receipt, so a
+8
later commit invalidates an earlier one. Callers can use it to act only
+9
once a frame is known to be on screen.
+10
- Requirements are unchanged from 0.2.9.
+11
12
# sigil-ui 0.2.9
13
14
- Add `sui_wayland_export_handle`, which exports a live xdg-toplevel through
native/wayland.cmodified
@@ -95,6 +95,29 @@ static const struct wl_interface exporter_interface = {
95
"zxdg_exporter_v2", 1, 2, exporter_requests, 0, NULL
96
};
97
+98
/* Stable presentation-time v1. The feedback object has events only. */
+99
static const struct wl_interface presentation_feedback_interface;
+100
static const struct wl_interface *presentation_feedback_args[] = {
+101
&wl_surface_interface, &presentation_feedback_interface,
+102
};
+103
static const struct wl_interface *presentation_output_args[] = { &wl_output_interface };
+104
static const struct wl_message presentation_requests[] = {
+105
{ "destroy", "", NULL },
+106
{ "feedback", "on", presentation_feedback_args },
+107
};
+108
static const struct wl_message presentation_events[] = { { "clock_id", "u", NULL } };
+109
static const struct wl_message presentation_feedback_events[] = {
+110
{ "sync_output", "o", presentation_output_args },
+111
{ "presented", "uuuuuuu", NULL },
+112
{ "discarded", "", NULL },
+113
};
+114
static const struct wl_interface presentation_interface = {
+115
"wp_presentation", 1, 2, presentation_requests, 1, presentation_events,
+116
};
+117
static const struct wl_interface presentation_feedback_interface = {
+118
"wp_presentation_feedback", 1, 0, NULL, 3, presentation_feedback_events,
+119
};
+120
121
#define REGISTER_AND_EXPORT(name, func, arity, doc) \
122
sigil_module_register_native(vm, name, func, arity, doc); \
123
sigil_module_export(vm, name)
@@ -293,6 +316,11 @@ typedef struct Wl {
316
uint32_t exporter_global;
317
struct wl_proxy *exporter, *exported;
318
char *export_handle;
+319
uint32_t presentation_global, presentation_serial;
+320
struct wl_proxy *presentation, *presentation_feedback;
+321
struct wl_callback *presentation_frame;
+322
enum { PRESENTATION_PENDING = 1, PRESENTATION_VISIBLE, PRESENTATION_FRAME,
+323
PRESENTATION_DISCARDED } presentation_status;
324
struct zwp_linux_dmabuf_v1 *dmabuf;
325
uint32_t dmabuf_global, dmabuf_version;
326
DmabufFeedbackState *dmabuf_feedback;
@@ -732,9 +760,80 @@ static const struct zwlr_layer_surface_v1_listener layer_listener = {
760
.configure = layer_configure, .closed = layer_closed,
761
};
762
+763
static void presentation_discard(Wl *w)
+764
{
+765
if (w->presentation_feedback) wl_proxy_destroy(w->presentation_feedback);
+766
w->presentation_feedback = NULL;
+767
w->presentation_frame = NULL;
+768
w->presentation_status = PRESENTATION_DISCARDED;
+769
}
+770
+771
static void presentation_clock(void *data, struct wl_proxy *proxy, uint32_t clock_id)
+772
{
+773
(void)data; (void)proxy; (void)clock_id;
+774
}
+775
static const struct {
+776
void (*clock_id)(void *, struct wl_proxy *, uint32_t);
+777
} presentation_listener = { presentation_clock };
+778
+779
static void presentation_output(void *data, struct wl_proxy *feedback, struct wl_output *output)
+780
{
+781
(void)data; (void)feedback; (void)output;
+782
}
+783
static void presentation_visible(void *data, struct wl_proxy *feedback, uint32_t sec_hi, uint32_t sec_lo,
+784
uint32_t nsec, uint32_t refresh, uint32_t sequence_hi, uint32_t sequence_lo, uint32_t flags)
+785
{
+786
(void)feedback; (void)sec_hi; (void)sec_lo; (void)nsec;
+787
(void)refresh; (void)sequence_hi; (void)sequence_lo; (void)flags;
+788
Wl *w = data;
+789
presentation_discard(w);
+790
w->presentation_status = PRESENTATION_VISIBLE;
+791
}
+792
static void presentation_discarded(void *data, struct wl_proxy *feedback)
+793
{
+794
(void)feedback;
+795
presentation_discard(data);
+796
}
+797
static const struct {
+798
void (*sync_output)(void *, struct wl_proxy *, struct wl_output *);
+799
void (*presented)(void *, struct wl_proxy *, uint32_t, uint32_t, uint32_t, uint32_t,
+800
uint32_t, uint32_t, uint32_t);
+801
void (*discarded)(void *, struct wl_proxy *);
+802
} presentation_feedback_listener = { presentation_output, presentation_visible, presentation_discarded };
+803
+804
static void presentation_begin(Wl *w)
+805
{
+806
presentation_discard(w);
+807
if (++w->presentation_serial == 0) ++w->presentation_serial;
+808
w->presentation_status = PRESENTATION_PENDING;
+809
if (!w->presentation_global) {
+810
w->presentation_frame = w->frame;
+811
if (!w->presentation_frame) w->presentation_status = PRESENTATION_DISCARDED;
+812
return;
+813
}
+814
if (!w->presentation) {
+815
w->presentation = wl_registry_bind(w->registry, w->presentation_global, &presentation_interface, 1);
+816
if (w->presentation &&
+817
wl_proxy_add_listener(w->presentation, (void (**)(void))&presentation_listener, w) < 0) {
+818
wl_proxy_marshal_flags(w->presentation, 0, NULL, 1, WL_MARSHAL_FLAG_DESTROY);
+819
w->presentation = NULL;
+820
}
+821
}
+822
if (w->presentation)
+823
w->presentation_feedback = wl_proxy_marshal_flags(w->presentation, 1, &presentation_feedback_interface,
+824
1, 0, w->surface, NULL);
+825
if (!w->presentation_feedback ||
+826
wl_proxy_add_listener(w->presentation_feedback, (void (**)(void))&presentation_feedback_listener, w) < 0)
+827
presentation_discard(w);
+828
}
+829
830
static void frame_done(void *data, struct wl_callback *cb, uint32_t time)
831
{
832
Wl *w = data;
+833
if (w->presentation_frame == cb) {
+834
w->presentation_frame = NULL;
+835
w->presentation_status = PRESENTATION_FRAME;
+836
}
837
wl_callback_destroy(cb);
838
w->frame = NULL;
839
Event *e = push(w, UI_FRAME);
@@ -1116,6 +1215,8 @@ static void global(void *data, struct wl_registry *r, uint32_t id, const char *i
1215
w->subcompositor = wl_registry_bind(r, id, &wl_subcompositor_interface, 1);
1216
else if (!strcmp(iface, exporter_interface.name) && !w->exporter_global)
1217
w->exporter_global = id;
+1218
else if (!strcmp(iface, presentation_interface.name) && !w->presentation_global)
+1219
w->presentation_global = id;
1220
else if (!strcmp(iface, zwp_linux_dmabuf_v1_interface.name) && !w->dmabuf_global) {
1221
/* Record availability only; SHM clients never bind this global. */
1222
w->dmabuf_global = id;
@@ -1162,6 +1263,12 @@ static void global_remove(void *data, struct wl_registry *r, uint32_t id)
1263
export_close(w);
1264
w->exporter_global = 0;
1265
}
+1266
if (id == w->presentation_global) {
+1267
presentation_discard(w);
+1268
if (w->presentation) wl_proxy_marshal_flags(w->presentation, 0, NULL, 1, WL_MARSHAL_FLAG_DESTROY);
+1269
w->presentation = NULL;
+1270
w->presentation_global = 0;
+1271
}
1272
if (id == w->dmabuf_global) {
1273
dmabuf_imports_revoke_pending(w);
1274
dmabuf_feedback_revoke_tree(w);
@@ -1214,6 +1321,9 @@ static void wl_teardown(Wl *w)
1321
dmabuf_feedback_close(w);
1322
if (!parent) canceled_imports = dmabuf_imports_close(w);
1323
if (w->display) {
+1324
presentation_discard(w);
+1325
if (w->presentation) wl_proxy_marshal_flags(w->presentation, 0, NULL, 1, WL_MARSHAL_FLAG_DESTROY);
+1326
w->presentation = NULL;
1327
if (w->frame) wl_callback_destroy(w->frame);
1328
w->frame = NULL;
1329
SuiWaylandFrame canceled = w->frame_notify;
@@ -2210,7 +2320,7 @@ static int free_slot(Wl *w)
2320
/* (%wl-present! s pixels canvas-width rects) => #t when committed, #f when
2321
* deferred. rects: a list of (x y w h) in device pixels; '() presents only
2322
* what an earlier deferred present left. */
−2213
static Value native_wl_present(SigilVM *vm, int argc, Value *args)
+2323
static Value present_canvas(SigilVM *vm, Value *args, int tracked)
2324
{
2325
int64_t cw;
2326
Wl *w = as_wl(vm, args[0], "wayland-present!");
@@ -2256,11 +2366,11 @@ static Value native_wl_present(SigilVM *vm, int argc, Value *args)
2366
if (q[2] > 0 && q[3] > 0) damage_add(&add, (Rect){(int32_t)q[0], (int32_t)q[1], (int32_t)q[2], (int32_t)q[3]});
2367
}
2368
damage_merge(&w->pending, &add);
−2259
if (damage_empty(&w->pending) && !w->ack_due) return SIGIL_TRUE; /* nothing to show */
+2369
if (damage_empty(&w->pending) && !w->ack_due) return tracked ? SIGIL_FALSE : SIGIL_TRUE;
2370
/* One frame in flight, except that a new size is acknowledged at once:
2371
* a hidden surface gets no frame callbacks, and the compositor waits
2372
* for the acknowledgement. */
−2263
if (w->closed || (w->frame && !w->ack_due)) return SIGIL_FALSE;
+2373
if (w->closed || (tracked && w->parent) || (w->frame && !w->ack_due)) return SIGIL_FALSE;
2374
int k = free_slot(w);
2375
if (k == -2) {
2376
fail(w, "cannot allocate a shared-memory buffer");
@@ -2303,20 +2413,44 @@ static Value native_wl_present(SigilVM *vm, int argc, Value *args)
2413
w->last_n = w->pending.n;
2414
w->last_box = w->pending.n ? (Rect){x0, y0, x1 - x0, y1 - y0} : (Rect){0, 0, 0, 0};
2415
}
−2306
if (w->frame) wl_callback_destroy(w->frame);
+2416
if (w->frame) {
+2417
if (w->presentation_frame == w->frame) presentation_discard(w);
+2418
wl_callback_destroy(w->frame);
+2419
}
2420
w->frame = wl_surface_frame(w->surface);
2421
wl_callback_add_listener(w->frame, &frame_listener, w);
+2422
if (tracked) presentation_begin(w);
2423
commit_surface(w);
2424
b->busy = 1;
2425
w->mapped = 1;
2426
damage_clear(&w->pending);
2427
if (wl_display_flush(w->display) < 0 && errno != EAGAIN) fail(w, "the compositor connection failed");
−2314
return SIGIL_TRUE;
+2428
return tracked ? sigil_fixnum(w->presentation_serial) : SIGIL_TRUE;
2429
bad_rects:
2430
sigil__vm_error(vm, SIGIL_ERR_TYPE, "wayland-present!: damage must be a list of (x y w h) integers");
2431
return DONE;
2432
}
2433
+2434
static Value native_wl_present(SigilVM *vm, int argc, Value *args)
+2435
{
+2436
return present_canvas(vm, args, 0);
+2437
}
+2438
+2439
static Value native_wl_present_tracked(SigilVM *vm, int argc, Value *args)
+2440
{
+2441
return present_canvas(vm, args, 1);
+2442
}
+2443
+2444
static Value native_wl_presentation_status(SigilVM *vm, int argc, Value *args)
+2445
{
+2446
Wl *w = as_wl(vm, args[0], "wayland-presentation-status");
+2447
int64_t serial;
+2448
if (!w || !int_arg(vm, args[1], &serial, "wayland-presentation-status")) return DONE;
+2449
if (w->closed || w->dead || serial <= 0 || serial != w->presentation_serial)
+2450
return sigil_fixnum(PRESENTATION_DISCARDED);
+2451
return sigil_fixnum(w->presentation_status);
+2452
}
+2453
2454
/* (%wl-ready? s) => #t when deferred damage waits and could be presented now */
2455
static Value native_wl_ready(SigilVM *vm, int argc, Value *args)
2456
{
@@ -2416,6 +2550,8 @@ void sigil__register_sigil_ui_wayland(SigilVM *vm)
2550
REGISTER_AND_EXPORT("%wl-output-count", native_wl_output_count, SIGIL_ARITY_EXACT(1), "Number of outputs");
2551
REGISTER_AND_EXPORT("%wl-output", native_wl_output, SIGIL_ARITY_EXACT(3), "One output");
2552
REGISTER_AND_EXPORT("%wl-present!", native_wl_present, SIGIL_ARITY_EXACT(4), "Present damage from a canvas");
+2553
REGISTER_AND_EXPORT("%wl-present-tracked!", native_wl_present_tracked, SIGIL_ARITY_EXACT(4), "Track one canvas commit");
+2554
REGISTER_AND_EXPORT("%wl-presentation-status", native_wl_presentation_status, SIGIL_ARITY_EXACT(2), "Observe the tracked canvas commit");
2555
REGISTER_AND_EXPORT("%wl-ready?", native_wl_ready, SIGIL_ARITY_EXACT(1), "Can deferred damage go now");
2556
REGISTER_AND_EXPORT("%wl-child", native_wl_child, SIGIL_ARITY_EXACT(6), "Create an embedded subsurface");
2557
REGISTER_AND_EXPORT("%wl-cursor-configure!", native_wl_cursor_configure, SIGIL_ARITY_EXACT(3), "Configure explicit cursor theme/size");
package.sglmodified
@@ -29,7 +29,7 @@
29
30
(package
31
name: "sigil-ui"
−32
version: "0.2.9"
+32
version: "0.2.10"
33
sigil: "0.24.8"
34
description: "Retained-mode UI framework with a software renderer"
35
url: "https://git.usesigil.org/sigil/sigil-ui"
src/sigil/ui/wayland.sglmodified
@@ -81,6 +81,7 @@
81
wayland-window-fullscreen? wayland-window-set-fullscreen! wayland-window-fullscreen-status
82
wayland-window-dispatch
83
wayland-dispatch wayland-present! wayland-present-ui! wayland-make-ui
+84
wayland-present-tracked! wayland-presentation-status
85
wayland-surface-close!)
86
(begin
87
(define-native (%wl-open display role title app-id output width height transparent?)
@@ -98,6 +99,8 @@
99
(define-native (%wl-output-count s) (: any? -> any?))
100
(define-native (%wl-output s i vec) (: any? any? any? -> any?))
101
(define-native (%wl-present! s px width rects) (: any? any? any? any? -> any?))
+102
(define-native (%wl-present-tracked! s px width rects) (: any? any? any? any? -> any?))
+103
(define-native (%wl-presentation-status s serial) (: any? any? -> any?))
104
(define-native (%wl-ready? s) (: any? -> any?))
105
(define-native (%wl-child parent x y width height overlay?)
106
(: any? any? any? any? any? any? -> any?))
@@ -422,6 +425,20 @@
425
(wayland-present! s (ui-canvas u)
426
(map (lambda (r) (map (lambda (n) (* n k)) r)) damage))))
427
+428
;;; Receipts belong to the newest tracked commit on a root surface.
+429
;;; A deferred call leaves no receipt and must be retried by the caller.
+430
(define (wayland-present-tracked! s canvas)
+431
(set-wayland-surface-canvas! s #f)
+432
(%wl-present-tracked! (wayland-surface-handle s) (canvas-pixels canvas) (canvas-width canvas)
+433
(list (list 0 0 (canvas-width canvas) (canvas-height canvas)))))
+434
+435
(define (wayland-presentation-status s serial)
+436
(case (%wl-presentation-status (wayland-surface-handle s) serial)
+437
((1) 'pending)
+438
((2) 'presented)
+439
((3) 'frame)
+440
(else 'discarded)))
+441
442
;;; Close the surface and the connection. The window disappears.
443
(define (wayland-surface-close! s)
444
(set-wayland-surface-canvas! s #f)