From 5a9a33e87395753e5b1c09c7c95ea3468050d779 Mon Sep 17 00:00:00 2001 From: IoIxD Date: Sat, 29 Nov 2025 20:52:56 -0700 Subject: [PATCH] wayland: we now have subsurfaces working --- .gitignore | 9 +- include/Mw/LowLevel/Wayland.h | 55 +++--- src/backend/wayland.c | 318 ++++++++++++++++++++++++++-------- 3 files changed, 275 insertions(+), 107 deletions(-) diff --git a/.gitignore b/.gitignore index 814f7b3..06c431e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,13 +1,8 @@ *.o *.so -examples/* -examples/*.exe examples/*/* -examples/*/*.o -examples/*/*.exe -!examples/*/ -!examples/*.* -!examples/*/*.* +!examples/*/*.c +!examples/*/*.spv compile_flags.txt *.a Makefile diff --git a/include/Mw/LowLevel/Wayland.h b/include/Mw/LowLevel/Wayland.h index 5fd1e7a..da610b4 100644 --- a/include/Mw/LowLevel/Wayland.h +++ b/include/Mw/LowLevel/Wayland.h @@ -30,33 +30,41 @@ typedef struct wayland_protocol { typedef wayland_protocol_t*(wl_setup_func)(MwU32, struct _MwLLWayland*); +struct _MwLLWaylandTopLevel { + struct xdg_surface* xdg_surface; + struct xdg_toplevel* xdg_top_level; + struct xdg_toplevel_listener xdg_toplevel_listener; + struct xdg_surface_listener xdg_surface_listener; + + struct xkb_context* xkb_context; + + MwBool running; + MwBool compositor_created; + MwBool xdg_wm_base_created; + MwBool xdg_surface_created; +}; + struct _MwLLWayland { struct _MwLLCommon common; + union { + struct _MwLLWaylandTopLevel toplevel; + struct wl_subsurface* subsurface; + }; + enum { + MWLL_WAYLAND_TOPLEVEL = 0, + MWLL_WAYLAND_SUBSURFACE = 1, + } type; + MwU32 ww; + MwU32 wh; - // struct wl_shm* shm; - struct wl_display* display; - struct wl_registry* registry; - // struct wl_compositor* compositor; - // struct wl_seat* seat; - struct wl_surface* surface; - struct xdg_surface* xdg_surface; - // struct xdg_wm_base* xdg_wm_base; - struct xdg_toplevel* xdg_top_level; - + struct wl_display* display; + struct wl_registry* registry; + struct wl_compositor* compositor; + struct wl_subcompositor* subcompositor; + struct wl_surface* surface; + struct wl_shm* shm; struct wl_registry_listener registry_listener; - // struct zxdg_decoration_manager_v1* decoration_manager; - // struct zxdg_toplevel_decoration_v1* toplevel_decoration; - - struct xdg_toplevel_listener xdg_toplevel_listener; - // struct xdg_wm_base_listener xdg_wm_base_listener; - struct xdg_surface_listener xdg_surface_listener; - // struct wl_pointer_listener pointer_listener; - // struct wl_seat_listener seat_listener; - // struct wl_keyboard_listener keyboard_listener; - // struct wl_shell_surface_listener shell_surface_listener; - // struct zxdg_toplevel_decoration_v1_listener decoration_listener; - struct { const char* key; wl_setup_func* value; @@ -67,10 +75,7 @@ struct _MwLLWayland { wayland_protocol_t* value; }* wl_protocol_map; - struct xkb_context* xkb_context; - MwBool configured; - MwBool running; }; struct _MwLLWaylandColor { diff --git a/src/backend/wayland.c b/src/backend/wayland.c index f964037..e8e7358 100644 --- a/src/backend/wayland.c +++ b/src/backend/wayland.c @@ -9,9 +9,13 @@ #include +#include +#include +#include + #include "../../external/stb_ds.h" -#define WAYLAND_GET_INTERFACE(r, inter) shget(r->wayland.wl_protocol_map, inter##_interface.name) +#define WAYLAND_GET_INTERFACE(r, inter) shget(r.wl_protocol_map, inter##_interface.name) // REGISTRY static void new_protocol(void* data, struct wl_registry* registry, @@ -21,7 +25,10 @@ static void new_protocol(void* data, struct wl_registry* registry, wl_setup_func* func = shget(wayland->wl_protocol_setup_map, interface); if(func != NULL) { + // printf("registering interface %s\n", interface); shput(wayland->wl_protocol_map, interface, func(name, data)); + } else { + // printf("unknown interface %s\n", interface); } }; @@ -34,12 +41,12 @@ static void protocol_removed(void* data, // SEAT static void wl_seat_name(void* data, struct wl_seat* wl_seat, const char* name) {}; static void wl_seat_capabilities(void* data, struct wl_seat* wl_seat, - uint32_t capabilities) {}; + MwU32 capabilities) {}; // XDG SHELL void xdg_wm_base_ping(void* data, struct xdg_wm_base* xdg_wm_base, - uint32_t serial) { + MwU32 serial) { // The compositor will send us a ping event to check that we're responsive. // We need to send back a pong request immediately. xdg_wm_base_pong(xdg_wm_base, serial); @@ -48,22 +55,76 @@ void xdg_wm_base_ping(void* data, // XDG SHELL SURFACE static void wl_shell_surface_ping(void* data, struct wl_shell_surface* wl_shell_surface, - uint32_t serial) {}; + MwU32 serial) {}; static void wl_shell_surface_configure(void* data, struct wl_shell_surface* wl_shell_surface, - uint32_t edges, - int32_t width, - int32_t height) {}; + MwU32 edges, + MwI32 width, + MwI32 height) {}; static void wl_shell_surface_popup_done(void* data, struct wl_shell_surface* wl_shell_surface) {}; static wayland_protocol_t* wl_shm_setup(MwU32 name, struct _MwLLWayland* wayland) { - wayland_protocol_t* proto = malloc(sizeof(wayland_protocol_t)); - proto->listener = NULL; - proto->context = wl_registry_bind(wayland->registry, name, &wl_shm_interface, 1); - return proto; + wayland->shm = wl_registry_bind(wayland->registry, name, &wl_shm_interface, 1); + return NULL; } +static void buffer_setup(struct _MwLLWayland* wayland) { + struct wl_shm_pool* pool; + struct wl_buffer* buffer; + int x, y; + + int fd; + MwU32 size = wayland->ww * wayland->wh * 4; + char temp_name[] = "/tmp/milsko-wl-shm-XXXXXX"; + + fd = mkstemp(temp_name); + + unlink(temp_name); + + if(posix_fallocate(fd, 0, size) != 0) { + printf("failure setting up wl_shm: could not fallocate. %s.\n", strerror(errno)); + close(fd); + return; + } + if(ftruncate(fd, size) != 0) { + printf("failure setting up wl_shm: could not truncate. %s.\n", strerror(errno)); + close(fd); + return; + } + + for(y = 0; y < wayland->wh; y++) { + for(x = 0; x < wayland->ww; x++) { +#define MIN(a, b) (((a) < (b)) ? (a) : (b)) + MwU8 a = 0xFF; + MwU8 r = MIN(((wayland->ww - x) * 0xFF) / wayland->ww, ((wayland->wh - y) * 0xFF) / wayland->wh); + MwU8 g = MIN((x * 0xFF) / wayland->ww, ((wayland->wh - y) * 0xFF) / wayland->wh); + MwU8 b = MIN(((wayland->ww - x) * 0xFF) / wayland->ww, (y * 0xFF) / wayland->wh); + write(fd, &b, 1); + write(fd, &g, 1); + write(fd, &r, 1); + write(fd, &a, 1); +#undef MIN + } + } + + fsync(fd); + + if(!(pool = wl_shm_create_pool(wayland->shm, fd, size))) { + printf("failure setting up wl_shm: could not create pool.\n"); + } + + buffer = wl_shm_pool_create_buffer(pool, 0, wayland->ww, wayland->wh, wayland->ww * 4, WL_SHM_FORMAT_ARGB8888); + + if(wayland->configured) { + wl_surface_attach(wayland->surface, buffer, 0, 0); + wl_surface_commit(wayland->surface); + } + + wl_buffer_destroy(buffer); + close(fd); +} + static wayland_protocol_t* wl_seat_setup(MwU32 name, struct _MwLLWayland* wayland) { wayland_protocol_t* proto = malloc(sizeof(wayland_protocol_t)); proto->listener = malloc(sizeof(struct wl_seat_listener)); @@ -76,13 +137,19 @@ static wayland_protocol_t* wl_seat_setup(MwU32 name, struct _MwLLWayland* waylan return proto; } -static wayland_protocol_t* wl_compositor_setup(MwU32 name, struct _MwLLWayland* wayland) { - wayland_protocol_t* proto = malloc(sizeof(wayland_protocol_t)); - proto->listener = NULL; - proto->context = wl_registry_bind(wayland->registry, name, &wl_compositor_interface, 1); - return proto; +static wayland_protocol_t* wl_compositor_setup(MwU32 name, struct _MwLLWayland* wayland) { + wayland->compositor = wl_registry_bind(wayland->registry, name, &wl_compositor_interface, 1); + + return NULL; } + +static wayland_protocol_t* wl_subcompositor_setup(MwU32 name, struct _MwLLWayland* wayland) { + wayland->subcompositor = wl_registry_bind(wayland->registry, name, &wl_subcompositor_interface, 1); + + return NULL; +} + static wayland_protocol_t* xdg_wm_base_setup(MwU32 name, struct _MwLLWayland* wayland) { wayland_protocol_t* proto = malloc(sizeof(wayland_protocol_t)); proto->listener = malloc(sizeof(struct xdg_wm_base_listener)); @@ -138,7 +205,7 @@ static wayland_protocol_t* zxdg_decoration_manager_v1_setup(MwU32 name, struct _ static void xdg_toplevel_configure(void* data, struct xdg_toplevel* xdg_toplevel, - int32_t width, int32_t height, + MwI32 width, MwI32 height, struct wl_array* states) { struct _MwLLWayland* self = (struct _MwLLWayland*)data; @@ -149,18 +216,18 @@ static void xdg_toplevel_configure(void* data, static void decoration_configure( void* data, struct zxdg_toplevel_decoration_v1* zxdg_toplevel_decoration_v1, - uint32_t mode) { + MwU32 mode) { } static void xdg_toplevel_close( void* data, struct xdg_toplevel* xdg_toplevel) { struct _MwLLWayland* self = (struct _MwLLWayland*)data; // Stop running if the user requests to close the toplevel - self->running = MwFALSE; + self->toplevel.running = MwFALSE; }; static void xdg_surface_configure( - void* data, struct xdg_surface* xdg_surface, uint32_t serial) { + void* data, struct xdg_surface* xdg_surface, MwU32 serial) { struct _MwLLWayland* self = (struct _MwLLWayland*)data; // The compositor configures our surface, acknowledge the configure event @@ -170,45 +237,94 @@ static void xdg_surface_configure( // If this isn't the first configure event we've received, we already // have a buffer attached, so no need to do anything. Commit the // surface to apply the configure acknowledgement. + // wl_surface_attach(self->surface, self->buffer, 0, 0); wl_surface_commit(self->surface); } self->configured = MwTRUE; } -static void shell_surface_ping(void* data, - struct wl_shell_surface* shell_surface, - uint32_t serial) {}; -static void shell_surface_configure( - void* data, struct wl_shell_surface* shell_surface, uint32_t edges, - int32_t width, int32_t height) {}; - -static void shell_surface_popup_done( - void* data, struct wl_shell_surface* shell_surface) {}; +#define WL_INTERFACE(interface) \ + shput(wayland->wl_protocol_setup_map, interface##_interface.name, (wl_setup_func*)interface##_setup); static void setup_callbacks(struct _MwLLWayland* wayland) { wayland->registry_listener.global = new_protocol; wayland->registry_listener.global_remove = protocol_removed; -#define WL_INTERFACE(interface) \ - shput(wayland->wl_protocol_setup_map, interface##_interface.name, (wl_setup_func*)interface##_setup); - WL_INTERFACE(wl_shm); - WL_INTERFACE(wl_seat); WL_INTERFACE(wl_compositor); - WL_INTERFACE(xdg_wm_base); - WL_INTERFACE(wl_shell_surface); - WL_INTERFACE(zxdg_decoration_manager_v1); - WL_INTERFACE(wp_cursor_shape_manager_v1); + WL_INTERFACE(wl_subcompositor); + if(wayland->type == MWLL_WAYLAND_TOPLEVEL) { + WL_INTERFACE(wl_seat); + WL_INTERFACE(xdg_wm_base); + WL_INTERFACE(wl_shell_surface); + WL_INTERFACE(zxdg_decoration_manager_v1); + WL_INTERFACE(wp_cursor_shape_manager_v1); + } } -static MwLL MwLLCreateImpl(MwLL parent, int x, int y, int width, int height) { - MwLL r; - r = malloc(sizeof(*r)); - MwLLCreateCommon(r); +#undef WL_INTERFACE - r->common.copy_buffer = 1; - r->common.type = MwLLBackendWayland; +static MwBool flush_display(struct wl_display* display) { + while(wl_display_flush(display) == -1) { + if(errno != EAGAIN) { + return MwFALSE; + } + + struct pollfd fd = {wl_display_get_fd(display), POLL_OUT}; + + while(poll(&fd, 1, -1) == -1) { + if(errno != EINTR && errno != EAGAIN) + return MwFALSE; + } + } + return MwTRUE; +} + +static void event_loop(MwLL handle) { + enum { + DISPLAY_FD, + KEYREPEAT_FD, + CURSOR_FD, + LIBDECOR_FD + }; + if(handle->wayland.display == NULL) { + return; + } + struct pollfd fd = {wl_display_get_fd(handle->wayland.display), POLLIN}; + int timeout = 500; + + while(wl_display_prepare_read(handle->wayland.display) != 0) { + if(wl_display_dispatch_pending(handle->wayland.display) > 0) { + return; + } + } + + // If an error other than EAGAIN happens, we have likely been disconnected + // from the Wayland session + if(!flush_display(handle->wayland.display)) { + wl_display_cancel_read(handle->wayland.display); + + handle->wayland.toplevel.running = MwFALSE; + return; + } + + if(!poll(&fd, 1, timeout)) { + wl_display_cancel_read(handle->wayland.display); + return; + } + + if(fd.revents & POLLIN) { + wl_display_read_events(handle->wayland.display); + if(wl_display_dispatch_pending(handle->wayland.display) > 0) { + } + } else { + wl_display_cancel_read(handle->wayland.display); + } +} + +static void setup_toplevel(MwLL r, int x, int y, int width, int height) { + r->wayland.type = MWLL_WAYLAND_TOPLEVEL; setup_callbacks(&r->wayland); @@ -217,55 +333,48 @@ static MwLL MwLLCreateImpl(MwLL parent, int x, int y, int width, int height) { if(r->wayland.display == NULL) { printf("wayland: failed to create display\n"); r->common.success = MwFALSE; - return r; + return; } - // Obtain the wl_registry and fetch the list of globals + // Do a roundtrip to ensure all interfaces are setup. r->wayland.registry = wl_display_get_registry(r->wayland.display); wl_registry_add_listener(r->wayland.registry, &r->wayland.registry_listener, r); if(wl_display_roundtrip(r->wayland.display) == -1) { printf("roundtrip failed\n"); r->common.success = MwFALSE; - return r; + return; } // Check that all globals we require are available if( - WAYLAND_GET_INTERFACE(r, wl_compositor)->context == NULL || - WAYLAND_GET_INTERFACE(r, xdg_wm_base)->context == NULL || - WAYLAND_GET_INTERFACE(r, wl_seat)->context == NULL) { + r->wayland.compositor == NULL || + WAYLAND_GET_INTERFACE(r->wayland, xdg_wm_base)->context == NULL || + WAYLAND_GET_INTERFACE(r->wayland, wl_seat)->context == NULL) { printf("no wl_shm, wl_compositor, wl_seat, or xdg_wm_base support\n"); r->common.success = MwFALSE; - return r; + return; } - // wl_log_set_handler_client(_MwLLWayland::logger); - - r->wayland.xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); + r->wayland.toplevel.xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); // Create a wl_surface, a xdg_surface and a xdg_toplevel - r->wayland.surface = wl_compositor_create_surface(WAYLAND_GET_INTERFACE(r, wl_compositor)->context); - r->wayland.xdg_surface = - xdg_wm_base_get_xdg_surface(WAYLAND_GET_INTERFACE(r, xdg_wm_base)->context, r->wayland.surface); - r->wayland.xdg_top_level = xdg_surface_get_toplevel(r->wayland.xdg_surface); + r->wayland.surface = wl_compositor_create_surface(r->wayland.compositor); + r->wayland.toplevel.xdg_surface = + xdg_wm_base_get_xdg_surface(WAYLAND_GET_INTERFACE(r->wayland, xdg_wm_base)->context, r->wayland.surface); + r->wayland.toplevel.xdg_top_level = xdg_surface_get_toplevel(r->wayland.toplevel.xdg_surface); // setup mandatory listeners - r->wayland.xdg_surface_listener.configure = xdg_surface_configure; - r->wayland.xdg_toplevel_listener.configure = xdg_toplevel_configure; - r->wayland.xdg_toplevel_listener.close = xdg_toplevel_close; + r->wayland.toplevel.xdg_surface_listener.configure = xdg_surface_configure; + r->wayland.toplevel.xdg_toplevel_listener.configure = xdg_toplevel_configure; + r->wayland.toplevel.xdg_toplevel_listener.close = xdg_toplevel_close; - xdg_surface_add_listener(r->wayland.xdg_surface, &r->wayland.xdg_surface_listener, r); - xdg_toplevel_add_listener(r->wayland.xdg_top_level, &r->wayland.xdg_toplevel_listener, + xdg_surface_add_listener(r->wayland.toplevel.xdg_surface, &r->wayland.toplevel.xdg_surface_listener, r); + xdg_toplevel_add_listener(r->wayland.toplevel.xdg_top_level, &r->wayland.toplevel.xdg_toplevel_listener, r); - xdg_toplevel_set_title(r->wayland.xdg_top_level, "Milsko Wayland App"); - xdg_toplevel_set_app_id(r->wayland.xdg_top_level, "Milsko Wayland App"); - - // if (!mFlags.windowResizable) { - // xdg_toplevel_set_min_size(mXDGTopLevel, mWidth, mHeight); - // xdg_toplevel_set_max_size(mXDGTopLevel, mWidth, mHeight); - // } + xdg_toplevel_set_title(r->wayland.toplevel.xdg_top_level, "Milsko Wayland App"); + xdg_toplevel_set_app_id(r->wayland.toplevel.xdg_top_level, "MilskoWaylandApp"); // Perform the initial commit and wait for the first configure event wl_surface_commit(r->wayland.surface); @@ -273,23 +382,76 @@ static MwLL MwLLCreateImpl(MwLL parent, int x, int y, int width, int height) { } // setup decorations if we can - if(WAYLAND_GET_INTERFACE(r, zxdg_decoration_manager_v1)->context != NULL) { - zxdg_decoration_manager_v1_context_t* dec = WAYLAND_GET_INTERFACE(r, zxdg_decoration_manager_v1)->context; + if(WAYLAND_GET_INTERFACE(r->wayland, zxdg_decoration_manager_v1)->context != NULL) { + zxdg_decoration_manager_v1_context_t* dec = WAYLAND_GET_INTERFACE(r->wayland, zxdg_decoration_manager_v1)->context; dec->decoration = zxdg_decoration_manager_v1_get_toplevel_decoration( - dec->manager, r->wayland.xdg_top_level); - - // zxdg_toplevel_decoration_v1_add_listener(dec->decoration, - // dec->listener, r); + dec->manager, r->wayland.toplevel.xdg_top_level); zxdg_toplevel_decoration_v1_set_mode( dec->decoration, ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE); + } else { + printf("zxdg null\n"); } + buffer_setup(&r->wayland); + wl_surface_commit(r->wayland.surface); +} + +static void setup_subsurface(MwLL parent, MwLL r, int x, int y, int width, int height) { + struct wl_compositor* compositor = parent->wayland.compositor; + struct wl_subcompositor* subcompositor = parent->wayland.subcompositor; + struct wl_surface* parent_surface = parent->wayland.surface; + + r->wayland.type = MWLL_WAYLAND_SUBSURFACE; + + setup_callbacks(&r->wayland); + + r->wayland.registry = wl_display_get_registry(parent->wayland.display); + r->wayland.display = parent->wayland.display; + + wl_registry_add_listener(r->wayland.registry, &r->wayland.registry_listener, r); + if(wl_display_roundtrip(r->wayland.display) == -1) { + printf("roundtrip failed\n"); + raise(SIGTRAP); + return; + } + r->wayland.display = parent->wayland.display; + + r->wayland.surface = wl_compositor_create_surface(compositor); + + r->wayland.subsurface = wl_subcompositor_get_subsurface(subcompositor, r->wayland.surface, parent_surface); + + wl_subsurface_set_position(r->wayland.subsurface, x, y); + + // printf("position %d %d\n", x, y); + + r->wayland.configured = MwTRUE; + + buffer_setup(&r->wayland); + + wl_surface_commit(parent->wayland.surface); +} +static MwLL MwLLCreateImpl(MwLL parent, int x, int y, int width, int height) { + MwLL r; + r = malloc(sizeof(*r)); + MwLLCreateCommon(r); + + r->common.copy_buffer = 1; + r->common.type = MwLLBackendWayland; + + r->wayland.ww = width; + r->wayland.wh = height; r->common.success = MwTRUE; + if(parent == NULL) { + setup_toplevel(r, x, y, width, height); + } else { + setup_subsurface(parent, r, x, y, width, height); + } + return r; } @@ -341,13 +503,18 @@ static void MwLLSetBackgroundImpl(MwLL handle, MwLLColor color) { } static int MwLLPendingImpl(MwLL handle) { + event_loop(handle); return 0; } static void MwLLNextEventImpl(MwLL handle) { + event_loop(handle); } static void MwLLSetTitleImpl(MwLL handle, const char* title) { + if(handle->wayland.type == MWLL_WAYLAND_TOPLEVEL) { + xdg_toplevel_set_title(handle->wayland.toplevel.xdg_top_level, title); + } } static MwLLPixmap MwLLCreatePixmapImpl(MwLL handle, unsigned char* data, int width, int height) { @@ -373,6 +540,7 @@ static void MwLLSetIconImpl(MwLL handle, MwLLPixmap pixmap) { } static void MwLLForceRenderImpl(MwLL handle) { + // event_loop(handle); } static void MwLLSetCursorImpl(MwLL handle, MwCursor* image, MwCursor* mask) {