From d14c17b86e9fcd9929d95b14ca0aff7d5748e446 Mon Sep 17 00:00:00 2001 From: IoI_xD Date: Tue, 17 Mar 2026 17:43:22 -0700 Subject: [PATCH] wayland fixes --- examples/basic/clipboard.c | 8 ++--- include/Mw/LowLevel/Wayland.h | 1 + src/backend/wayland.c | 61 +++++++++++++++++++++-------------- 3 files changed, 41 insertions(+), 29 deletions(-) diff --git a/examples/basic/clipboard.c b/examples/basic/clipboard.c index c8882d3..384cbc6 100644 --- a/examples/basic/clipboard.c +++ b/examples/basic/clipboard.c @@ -3,7 +3,7 @@ MwWidget window, instructions, text; void resize(MwWidget handle, void* user_data, void* call_data) { - unsigned int w, h, mh; + unsigned int w, h; (void)user_data; (void)call_data; @@ -12,13 +12,13 @@ void resize(MwWidget handle, void* user_data, void* call_data) { h = MwGetInteger(handle, MwNheight); MwVaApply(instructions, - MwNy, 50 + mh, + MwNy, 50, MwNwidth, w - 50 * 2, MwNheight, h - 125 - 50 * 3, NULL); MwVaApply(text, - MwNy, 200 + mh, + MwNy, 200, MwNwidth, w - 50 * 2, MwNheight, h - 125 - 50 * 3, NULL); @@ -30,7 +30,7 @@ void clipboard(MwWidget handle, void* user_data, void* call_data) { (void)user_data; if(clipboard != NULL) { - MwVaApply(text, MwNtext, clipboard); + MwVaApply(text, MwNtext, clipboard, NULL); MwForceRender(text); } MwForceRender(window); diff --git a/include/Mw/LowLevel/Wayland.h b/include/Mw/LowLevel/Wayland.h index 0375ece..78916dc 100644 --- a/include/Mw/LowLevel/Wayland.h +++ b/include/Mw/LowLevel/Wayland.h @@ -186,6 +186,7 @@ struct _MwLLWayland { MwLL parent; MwBool force_render; + MwBool did_event_loop_early; struct _MwLLWaylandShmBuffer framebuffer; struct _MwLLWaylandShmBuffer cursor; diff --git a/src/backend/wayland.c b/src/backend/wayland.c index 7d8faee..46b3732 100644 --- a/src/backend/wayland.c +++ b/src/backend/wayland.c @@ -5,6 +5,8 @@ #include #include "../../external/stb_ds.h" +#include "Mw/BaseTypes.h" +#include "Mw/LowLevel.h" #include #include @@ -432,6 +434,9 @@ static void pointer_button(void* data, struct wl_pointer* wl_pointer, MwU32 seri p.point = self->wayland.cur_mouse_pos; if(p.point.x > self->wayland.x && p.point.x < self->wayland.x + self->wayland.ww && p.point.y > self->wayland.y && p.point.y < self->wayland.y + self->wayland.wh) { int i; + + p.point.x -= self->wayland.x; + p.point.y -= self->wayland.y; switch(button) { case BTN_LEFT: p.button = MwLLMouseLeft; @@ -1038,12 +1043,6 @@ static int event_loop(MwLL handle) { fd.fd = wl_display_get_fd(wayland->display); fd.events = POLLIN; - while(wl_display_prepare_read(handle->wayland.display) != 0) { - if(wl_display_dispatch_pending(handle->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) { @@ -1060,19 +1059,17 @@ static int event_loop(MwLL handle) { } } + wl_display_prepare_read(handle->wayland.display); /* Condition where no events are being sent. */ if(!poll(&fd, 1, timeout)) { - wl_display_cancel_read(handle->wayland.display); + wl_display_cancel_read(wayland->display); /* In this case, we need to commit the surface for any animations, etc. */ - wl_surface_commit(handle->wayland.framebuffer.surface); + wl_surface_commit(wayland->framebuffer.surface); return 0; } - if(fd.revents & POLLIN) { - wl_display_read_events(wayland->display); - if(wl_display_dispatch_pending(wayland->display) > 0) { - } - } else { + wl_display_read_events(wayland->display); + if(wl_display_dispatch_pending(wayland->display) < 0) { wl_display_cancel_read(handle->wayland.display); } @@ -1588,32 +1585,42 @@ static int MwLLPendingImpl(MwLL handle) { MwBool pending = MwFALSE; struct timespec timeout; int i; + timeout.tv_nsec = 1; + timeout.tv_sec = 0; + + pending = wl_display_dispatch_timeout(handle->wayland.display, &timeout); if(handle->wayland.always_render) { + event_loop(handle); + return 0; + } + // if(handle->wayland.force_render) { + // // event_loop(handle); + // // handle->wayland.force_render = 0; + // // update_buffer(&handle->wayland.framebuffer); + // return 1; + // } + if(handle->wayland.events_pending || handle->wayland.force_render) { + handle->wayland.did_event_loop_early = MwTRUE; return event_loop(handle); } - timeout.tv_nsec = 10; - timeout.tv_sec = 0; - - if(handle->wayland.force_render) { - return 1; - } - if(handle->wayland.events_pending) { - return 1; - } - - return wl_display_dispatch_timeout(handle->wayland.display, &timeout); + return pending; } static void MwLLNextEventImpl(MwLL handle) { if(!handle->wayland.always_render) { - event_loop(handle); + if(handle->wayland.did_event_loop_early) { + handle->wayland.did_event_loop_early = MwFALSE; + } else { + event_loop(handle); + } } if(handle->wayland.events_pending) { handle->wayland.events_pending = 0; } if(handle->wayland.force_render) { + update_buffer(&handle->wayland.framebuffer); handle->wayland.force_render = 0; } } @@ -1683,6 +1690,10 @@ static void MwLLDrawPixmapImpl(MwLL handle, MwRect* rect, MwLLPixmap pixmap) { cairo_destroy(c); cairo_surface_destroy(cs); + + MwLLForceRender(handle); + // update_buffer(&handle->wayland.framebuffer); + // wl_surface_damage(handle->wayland.framebuffer.surface, 0, 0, handle->wayland.ww, handle->wayland.wh); } static void MwLLSetIconImpl(MwLL handle, MwLLPixmap pixmap) { if(handle->wayland.type == MWLL_WAYLAND_TOPLEVEL) {