From f7fa0930cb46046f995fa1ab927aef62466e995e Mon Sep 17 00:00:00 2001 From: Berke Güzel Date: Mon, 12 Oct 2026 00:29:34 +0300 Subject: feat: add SDL3-style surface, display, and loop timer API for X11, Wayland, and Cocoa --- src/cocoa/window.m | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) (limited to 'src/cocoa') diff --git a/src/cocoa/window.m b/src/cocoa/window.m index 4454be3..fc69412 100644 --- a/src/cocoa/window.m +++ b/src/cocoa/window.m @@ -110,6 +110,7 @@ struct cocoa_display { bool open; bool launched; bool pumping; + bool owns_loop; }; static struct cocoa_display darwin_ctx; @@ -867,6 +868,13 @@ int cocoa_display_dispatch_timeout(struct cocoa_display *d, int timeout_ms) d->depth++; int count = 0; @try { + if (d->loop) { + int timer_to = yasl_loop_next_timeout_ms(d->loop); + if (timer_to >= 0 && (timeout_ms < 0 || timer_to < timeout_ms)) { + timeout_ms = timer_to; + } + } + NSDate *limitDate; if (timeout_ms == 0) limitDate = [NSDate distantPast]; @@ -895,6 +903,10 @@ int cocoa_display_dispatch_timeout(struct cocoa_display *d, int timeout_ms) } } } + + if (d->loop) { + yasl_loop_step(d->loop, 0); + } } @finally { d->depth--; d->pumping = false; @@ -1003,7 +1015,14 @@ struct cocoa_display *cocoa_display_open(struct yasl_loop *loop, const struct ya @autoreleasepool { app_init(name); } darwin_ctx.base.ops = &yasl_cocoa_backend; darwin_ctx.open = true; - darwin_ctx.loop = loop; + if (!loop) { + darwin_ctx.loop = yasl_loop_create(); + darwin_ctx.owns_loop = true; + } else { + darwin_ctx.loop = loop; + darwin_ctx.owns_loop = false; + } + darwin_ctx.base.loop = darwin_ctx.loop; if (spec) { darwin_ctx.base.callbacks = spec->cbs ? *spec->cbs : (struct yasl_display_cbs){0}; darwin_ctx.cbs = &darwin_ctx.base.callbacks; @@ -1022,6 +1041,11 @@ void cocoa_display_close(struct cocoa_display *d) [NSEvent removeMonitor:d->key_up_monitor]; d->key_up_monitor = nil; } + if (d->owns_loop && d->loop) { + yasl_loop_destroy(d->loop); + d->loop = NULL; + d->base.loop = NULL; + } NSApp.delegate = nil; [d->delegate release]; d->delegate = nil; -- cgit v1.2.3