From 39533b3fec614bb1179960a7381e32f2f8e9022f Mon Sep 17 00:00:00 2001 From: IoIxD Date: Fri, 5 Dec 2025 17:16:08 -0700 Subject: [PATCH] swap buffers in draw handler instead --- .gitignore | 1 - examples/basic/checkbox.c | 6 ++-- include/Mw/LowLevel.h | 1 + include/Mw/LowLevel/Wayland.h | 1 + src/backend/wayland.c | 58 +++++++++++++++++++---------------- src/core.c | 10 ++++-- 6 files changed, 45 insertions(+), 32 deletions(-) diff --git a/.gitignore b/.gitignore index b022af0..a5e12aa 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,6 @@ *.o *.so *.dll -examples/* examples/*.exe examples/*/* !examples/*/*.c diff --git a/examples/basic/checkbox.c b/examples/basic/checkbox.c index 972cb2c..546b7df 100644 --- a/examples/basic/checkbox.c +++ b/examples/basic/checkbox.c @@ -5,10 +5,10 @@ int main() { MwWidget window; MwLibraryInit(); - + window = MwVaCreateWidget(MwWindowClass, "test", NULL, MwDEFAULT, MwDEFAULT, 8 + 16 + 8 + 16 * 10 + 8, 8 + 16 + 8 + 16 + 8, - MwNtitle, "checkbox", - NULL); + MwNtitle, "checkbox", + NULL); MwVaCreateWidget(MwCheckBoxClass, "cb1", window, 8, 8, 16, 16, NULL); diff --git a/include/Mw/LowLevel.h b/include/Mw/LowLevel.h index 2be5b26..e8721dd 100644 --- a/include/Mw/LowLevel.h +++ b/include/Mw/LowLevel.h @@ -35,6 +35,7 @@ struct _MwLLCommon { int copy_buffer; int type; int success; + int do_redraw; MwLLHandler handler; }; diff --git a/include/Mw/LowLevel/Wayland.h b/include/Mw/LowLevel/Wayland.h index ed7dcaf..ba042a6 100644 --- a/include/Mw/LowLevel/Wayland.h +++ b/include/Mw/LowLevel/Wayland.h @@ -82,6 +82,7 @@ struct _MwLLWayland { MwBool configured; MwBool force_redraw; MwBool egl_setup; + MwBool drawn_once; MwBool egl_do_resetup; MwU32 x, y, ww, wh; int z_index; diff --git a/src/backend/wayland.c b/src/backend/wayland.c index 043530d..e4bd5e3 100644 --- a/src/backend/wayland.c +++ b/src/backend/wayland.c @@ -315,13 +315,17 @@ static void xdg_toplevel_configure(void* data, MwLL self = data; PFNGLFRAMEBUFFERTEXTURE2DPROC glFramebufferTexture2D = (PFNGLFRAMEBUFFERTEXTURE2DPROC)eglGetProcAddress("glFramebufferTexture2D"); - if(width == 0 && height == 0) { + if(width == 0 || height == 0) { width = self->wayland.ww; height = self->wayland.wh; + // if it's still 0 then bail + if(width == 0 || height == 0) { + return; + } } self->wayland.ww = width; self->wayland.wh = height; - xdg_surface_set_window_geometry(self->wayland.toplevel->xdg_surface, self->wayland.x, self->wayland.y, self->wayland.ww, self->wayland.wh); + xdg_surface_set_window_geometry(self->wayland.toplevel->xdg_surface, 0, 0, self->wayland.ww, self->wayland.wh); MwLLSetWH(self, width, height); MwLLDispatch(self, resize, NULL); @@ -369,18 +373,15 @@ static void xdg_surface_configure( self->wayland.configured = MwTRUE; } -static int event_loop(struct _MwLLWayland* wayland) { +static int event_loop(MwLL handle) { enum { DISPLAY_FD, KEYREPEAT_FD, CURSOR_FD }; - struct pollfd fd; - int timeout = 100; - - if(wayland->force_redraw) { - return 1; - } + struct pollfd fd; + int timeout = 100; + struct _MwLLWayland* wayland = &handle->wayland; if(wayland->display == NULL) { return 0; @@ -388,12 +389,6 @@ static int event_loop(struct _MwLLWayland* wayland) { fd.fd = wl_display_get_fd(wayland->display); fd.events = POLLIN; - while(wl_display_prepare_read(wayland->display) != 0) { - if(wl_display_dispatch_pending(wayland->display) > 0) { - return 0; - } - } - /* If an error other than EAGAIN happens, we have likely been disconnected from the Wayland session */ while(wl_display_flush(wayland->display) == -1) { if(errno != EAGAIN) { @@ -433,8 +428,11 @@ static int event_loop(struct _MwLLWayland* wayland) { if(wl_display_dispatch_pending(wayland->display) > 0) { } } else { - wl_display_cancel_read(wayland->display); + if(wl_display_dispatch_pending(wayland->display) == 0) { + MwLLDispatch(handle, draw, NULL); + } } + return 0; } @@ -532,11 +530,9 @@ static void setup_toplevel(MwLL r, int x, int y) { wl_region_add(region, 0, 0, r->wayland.ww, r->wayland.wh); wl_surface_set_opaque_region(r->wayland.surface, region); wl_surface_commit(r->wayland.surface); - event_loop(&r->wayland); + event_loop(r); wl_region_destroy(region); - printf("%p\n", shget(r->wayland.wl_protocol_map, zxdg_decoration_manager_v1_interface.name)); - // setup decorations if we can if(shget(r->wayland.wl_protocol_map, zxdg_decoration_manager_v1_interface.name) != NULL) { zxdg_decoration_manager_v1_context_t* dec = WAYLAND_GET_INTERFACE(r->wayland, zxdg_decoration_manager_v1)->context; @@ -576,7 +572,7 @@ static void setup_subsurface(MwLL parent, MwLL r, int x, int y) { wl_region_add(region, 0, 0, r->wayland.ww, r->wayland.wh); wl_surface_set_opaque_region(r->wayland.surface, region); wl_surface_commit(r->wayland.surface); - event_loop(&r->wayland); + event_loop(r); wl_region_destroy(region); r->wayland.subsurface = wl_subcompositor_get_subsurface(subcompositor, r->wayland.surface, parent_surface); @@ -628,13 +624,18 @@ static MwLL MwLLCreateImpl(MwLL parent, int x, int y, int width, int height) { if(width == 0) width = 1; if(height == 0) height = 1; - r->wayland.ww = width; - r->wayland.wh = height; + r->wayland.ww = width; + r->wayland.wh = height; + printf("%d %d\n", x, y); + r->wayland.parent = parent; r->wayland.force_redraw = MwFALSE; r->common.success = MwTRUE; + r->wayland.drawn_once = MwFALSE; + r->wayland.x = x; + r->wayland.y = y; if(parent == NULL) { setup_toplevel(r, x, y); } else { @@ -668,14 +669,16 @@ static void MwLLSetXYImpl(MwLL handle, int x, int y) { static void MwLLSetWHImpl(MwLL handle, int w, int h) { /* Prevent an integer underflow when the w/h is too low */ - if(w < 10 || h < 10) { + if((w < 10 || h < 10)) { + handle->wayland.ww = 10; + handle->wayland.wh = 10; return; } handle->wayland.ww = w; handle->wayland.wh = h; if(handle->wayland.type == MWLL_WAYLAND_TOPLEVEL) { - xdg_surface_set_window_geometry(handle->wayland.toplevel->xdg_surface, handle->wayland.x, handle->wayland.y, handle->wayland.ww, handle->wayland.wh); + xdg_surface_set_window_geometry(handle->wayland.toplevel->xdg_surface, 0, 0, handle->wayland.ww, handle->wayland.wh); } else { } } @@ -769,7 +772,7 @@ static void MwLLEndDrawImpl(MwLL handle) { glBindTexture(GL_TEXTURE_2D, handle->wayland.fbo_texture); // glClear(GL_COLOR_BUFFER_BIT); - // glClearColor(0, 0, 0, 1); + // glClearColor(1.0, 0, 0, 1); glColor3f(1.0, 1.0, 1.0); @@ -790,6 +793,7 @@ static void MwLLEndDrawImpl(MwLL handle) { printf("error swapping buffers! %0X\n", eglGetError()); } else { wl_surface_commit(handle->wayland.surface); + handle->wayland.drawn_once = MwTRUE; } } @@ -814,7 +818,9 @@ static void MwLLFreeColorImpl(MwLLColor color) { } static int MwLLPendingImpl(MwLL handle) { - return event_loop(&handle->wayland); + if(wl_display_dispatch_pending(handle->wayland.display) == 0) { + } + return event_loop(handle) || handle->wayland.force_redraw; } static void MwLLNextEventImpl(MwLL handle) { diff --git a/src/core.c b/src/core.c index 401600d..4df7fe7 100644 --- a/src/core.c +++ b/src/core.c @@ -7,6 +7,7 @@ static void lldrawhandler(MwLL handle, void* data) { MwWidget h = (MwWidget)handle->common.user; + int ind; (void)data; @@ -14,6 +15,13 @@ static void lldrawhandler(MwLL handle, void* data) { MwDispatch(h, draw); if(h->draw_inject != NULL) h->draw_inject(h); MwDispatchUserHandler(h, MwNdrawHandler, NULL); + + handle->common.do_redraw = MwTRUE; + if(h->parent == NULL) { + MwLLEndDraw(h->lowlevel); + } else if(h->parent->lowlevel == NULL) { + MwLLEndDraw(h->lowlevel); + } } static void lluphandler(MwLL handle, void* data) { @@ -334,8 +342,6 @@ void MwLoop(MwWidget handle) { } if(v != 0) break; - MwLLEndDraw(handle->lowlevel); - for(i = 0; i < arrlen(handle->tick_list); i++) { MwDispatch(handle->tick_list[i], tick); MwDispatchUserHandler(handle->tick_list[i], MwNtickHandler, NULL);