From 44a9389bae2c3fdcc73d75132ae0e4382fe58548 Mon Sep 17 00:00:00 2001 From: Andreev Gregory Date: Mon, 18 Aug 2025 21:58:46 +0300 Subject: [PATCH] Corrected r1 (wayland test) --- src/l2/tests/r1/r1.c | 66 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 57 insertions(+), 9 deletions(-) diff --git a/src/l2/tests/r1/r1.c b/src/l2/tests/r1/r1.c index 3fa6539..c470bb3 100644 --- a/src/l2/tests/r1/r1.c +++ b/src/l2/tests/r1/r1.c @@ -84,6 +84,7 @@ typedef struct { /* Objects */ swapchain_t swapchain; struct wl_surface *wl_surface; + struct wl_callback* wl_callback; struct xdg_surface *xdg_surface; struct xdg_toplevel *xdg_toplevel; /* inputs */ @@ -204,6 +205,8 @@ int swapchain_take_slot(my_state *state) { state->swapchain.pool, ij * 4 * MAX_BUFFER_WIDTH * MAX_BUFFER_HEIGHT, width, height, 4 * width, WL_SHM_FORMAT_ARGB8888); state->swapchain.used_buffers[ij] = s; + if (!s) + abortf("wl_shm_pool_create_buffer\n"); wl_buffer_add_listener(s, &main_h_wl_buffer_listener, state); return ij; @@ -421,12 +424,14 @@ static void main_h_wl_seat_capabilities(void *data, struct wl_seat *wl_seat, uin my_state* state = data; if (capabilities & WL_SEAT_CAPABILITY_POINTER) { state->pointer = wl_seat_get_pointer(wl_seat); - assert(state->pointer); + if (!state->pointer) + abortf("wl_seat_get_pointer\n"); wl_pointer_add_listener(state->pointer, &main_h_wl_pointer_listener, state); } if (capabilities & WL_SEAT_CAPABILITY_KEYBOARD) { state->keyboard = wl_seat_get_keyboard(wl_seat); - assert(state->keyboard); + if (!state->keyboard) + abortf("wl_seat_get_keyboard\n"); wl_keyboard_add_listener(state->keyboard, &main_h_wl_keyboard_listener, state); } } @@ -446,10 +451,16 @@ static void main_h_wl_registry_global( my_state *state = data; if (strcmp(interface, wl_shm_interface.name) == 0) { state->wl_shm = wl_registry_bind(wl_registry, name, &wl_shm_interface, 1); + if (!state->wl_shm) + abortf("wl_registry_bind\n"); } else if (strcmp(interface, wl_compositor_interface.name) == 0) { state->wl_compositor = wl_registry_bind(wl_registry, name, &wl_compositor_interface, 4); + if (!state->wl_compositor) + abortf("wl_registry_bind\n"); } else if (strcmp(interface, xdg_wm_base_interface.name) == 0) { state->xdg_wm_base = wl_registry_bind(wl_registry, name, &xdg_wm_base_interface, 1); + if (!state->xdg_wm_base) + abortf("wl_registry_bind\n"); xdg_wm_base_add_listener(state->xdg_wm_base, &main_h_xdg_wm_base_listener, state); } else if (strcmp(interface, wl_seat_interface.name) == 0) { if (state->wl_seat) { @@ -457,6 +468,8 @@ static void main_h_wl_registry_global( return; } state->wl_seat = wl_registry_bind(wl_registry, name, &wl_seat_interface, 4); + if (!state->wl_seat) + abortf("wl_registry_bind\n"); wl_seat_add_listener(state->wl_seat, &main_h_wl_seat_listener, state); } } @@ -475,8 +488,11 @@ static const struct wl_callback_listener main_h_wl_surface_frame_listener; static void main_h_wl_surface_frame_done(void *data, struct wl_callback *cb, uint32_t time){ my_state *state = data; wl_callback_destroy(cb); - cb = wl_surface_frame(state->wl_surface); - wl_callback_add_listener(cb, &main_h_wl_surface_frame_listener, state); + // todo: when I add multiple surfaces, gonna need to think of something smarter + state->wl_callback = wl_surface_frame(state->wl_surface); + if (!state->wl_callback) + abortf("wl_surface_frame\n"); + wl_callback_add_listener(state->wl_callback, &main_h_wl_surface_frame_listener, state); if (state->last_frame_time != 0) { update_state(state, time - state->last_frame_time); @@ -527,29 +543,61 @@ int main() { } state.swapchain.pool = wl_shm_create_pool(state.wl_shm, state.swapchain.fd, size); - assert(state.swapchain.pool); + if (!state.swapchain.pool) + abortf("wl_shm_create_pool"); close(state.swapchain.fd); } state.xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); - printf("I am gonna create surfaces\n"); + if (!state.xkb_context) + abortf("xkb_context_new\n"); state.wl_surface = wl_compositor_create_surface(state.wl_compositor); + if (!state.wl_surface) + abortf("wl_compositor_create_surface\n"); state.xdg_surface = xdg_wm_base_get_xdg_surface( state.xdg_wm_base, state.wl_surface); + if (!state.xdg_surface) + abortf("xdg_wm_base_get_xdg_surface\n"); xdg_surface_add_listener(state.xdg_surface, &xdg_surface_listener, &state); state.xdg_toplevel = xdg_surface_get_toplevel(state.xdg_surface); + if (!state.xdg_toplevel) + abortf("xdg_surface_get_toplevel\n"); xdg_toplevel_add_listener(state.xdg_toplevel, &main_h_xdg_toplevel_listener, &state); xdg_toplevel_set_title(state.xdg_toplevel, "r1"); wl_surface_commit(state.wl_surface); - struct wl_callback* cb = wl_surface_frame(state.wl_surface); - wl_callback_add_listener(cb, &main_h_wl_surface_frame_listener, &state); + state.wl_callback = wl_surface_frame(state.wl_surface); + if (!state.wl_callback) + abortf("wl_surface_frame\n"); + wl_callback_add_listener(state.wl_callback, &main_h_wl_surface_frame_listener, &state); while (wl_display_dispatch(state.wl_display)) { if (state.closed) break; } - // todo: clean up this mess + if (state.wl_callback) + wl_callback_destroy(state.wl_callback); + xdg_toplevel_destroy(state.xdg_toplevel); + xdg_surface_destroy(state.xdg_surface); + xdg_wm_base_destroy(state.xdg_wm_base); + wl_surface_destroy(state.wl_surface); + wl_compositor_destroy(state.wl_compositor); + if (state.pointer) + wl_pointer_destroy(state.pointer); + xkb_context_unref(state.xkb_context); + xkb_keymap_unref(state.xkb_keymap); + xkb_state_unref(state.xkb_state); + if (state.keyboard) + wl_keyboard_destroy(state.keyboard); + if (state.wl_seat) + wl_seat_destroy(state.wl_seat); munmap(state.swapchain.data, SWAPCHAIN_SLOTS * MAX_BUFFER_WIDTH * MAX_BUFFER_HEIGHT * 2); + for (size_t i = 0; i < SWAPCHAIN_SLOTS; i++) { + if (state.swapchain.used_buffers[i]) + wl_buffer_destroy(state.swapchain.used_buffers[i]); + } + wl_shm_pool_destroy(state.swapchain.pool); + wl_shm_destroy(state.wl_shm); + wl_registry_destroy(state.wl_registry); wl_display_disconnect(state.wl_display); return 0; }