From 1acadb9cdfc2025b65b190c872e420b199827655 Mon Sep 17 00:00:00 2001 From: Fabrice Date: Tue, 10 Feb 2026 14:03:09 +0100 Subject: use concrete errors --- src/events.cc | 33 +++++++++++++++++---------------- src/events.h | 8 +++++++- src/graphics.h | 2 +- src/window.cc | 12 ++++++------ src/window.h | 11 +++++++++-- src/wlstate.cc | 43 ++++++++++++++++++++----------------------- src/wlstate.h | 8 +++++++- 7 files changed, 67 insertions(+), 50 deletions(-) (limited to 'src') diff --git a/src/events.cc b/src/events.cc index 755c034..06fc68f 100644 --- a/src/events.cc +++ b/src/events.cc @@ -1,21 +1,24 @@ #include "events.h" + +#include + #include "hash.h" #include "vec.h" -#include -static inline window_id_t wayc_eventloop_genid(struct eventloop_s *loop) { +static inline window_id_t wayc_eventloop_genid(struct eventloop_s* loop) { wayc_notnull(loop); return loop->winid++; } -bool wayc_eventloop_init(struct eventloop_s *loop, event_handler_t handler) { +enum eventloop_error_e wayc_eventloop_init(struct eventloop_s* loop, + event_handler_t handler) { wayc_notnull(loop); wayc_notnull(handler); memset(loop, 0, sizeof(*loop)); - if (!wayc_wlstate_init(&loop->state)) - return false; + if (wayc_wlstate_init(&loop->state) != WLSTATE_ERROR_NONE) + return EVENTLOOP_ERROR_WLSTATE; loop->events = WAYC_VEC_INIT(struct event_s); loop->handler = handler; @@ -23,18 +26,18 @@ bool wayc_eventloop_init(struct eventloop_s *loop, event_handler_t handler) { wayc_hashmap_init(&loop->windows); - return true; + return EVENTLOOP_ERROR_NONE; } -void wayc_eventloop_deinit(struct eventloop_s *loop) { +void wayc_eventloop_deinit(struct eventloop_s* loop) { wayc_notnull(loop); wayc_wlstate_deinit(&loop->state); wayc_vec_deinit(&loop->events); } -window_id_t wayc_eventloop_register(struct eventloop_s *loop, - struct window_s *window) { +window_id_t wayc_eventloop_register(struct eventloop_s* loop, + struct window_s* window) { wayc_notnull(loop); wayc_notnull(window); @@ -43,24 +46,22 @@ window_id_t wayc_eventloop_register(struct eventloop_s *loop, return winid; } -void wayc_eventloop_unregister(struct eventloop_s *loop, window_id_t winid) { +void wayc_eventloop_unregister(struct eventloop_s* loop, window_id_t winid) { wayc_notnull(loop); wayc_hashmap_remove(&loop->windows, &winid); } -bool wayc_eventloop_running(struct eventloop_s *loop) { +bool wayc_eventloop_running(struct eventloop_s* loop) { wayc_notnull(loop); return loop->running; } -void wayc_eventloop_update(struct eventloop_s *loop) { +void wayc_eventloop_update(struct eventloop_s* loop) { wayc_notnull(loop); - if (wayc_hashmap_count(&loop->windows) == 0) - loop->running = false; + if (wayc_hashmap_count(&loop->windows) == 0) loop->running = false; wayc_wlstate_update(&loop->state); struct event_s event; - while (wayc_vec_pop(&loop->events, &event)) - loop->handler(loop, &event); + while (wayc_vec_pop(&loop->events, &event)) loop->handler(loop, &event); } diff --git a/src/events.h b/src/events.h index 6332c4e..3e7be19 100644 --- a/src/events.h +++ b/src/events.h @@ -43,6 +43,11 @@ struct eventloop_s; typedef void (*event_handler_t)(struct eventloop_s* loop, struct event_s* event); +enum eventloop_error_e { + EVENTLOOP_ERROR_NONE = 0, + EVENTLOOP_ERROR_WLSTATE, +}; + struct eventloop_s { window_id_t winid; struct wlstate_s state; @@ -52,7 +57,8 @@ struct eventloop_s { bool running; }; -bool wayc_eventloop_init(struct eventloop_s* loop, event_handler_t handler); +enum eventloop_error_e wayc_eventloop_init(struct eventloop_s* loop, + event_handler_t handler); void wayc_eventloop_deinit(struct eventloop_s* loop); window_id_t wayc_eventloop_register(struct eventloop_s* loop, struct window_s* window); diff --git a/src/graphics.h b/src/graphics.h index 7f09c4f..215fd92 100644 --- a/src/graphics.h +++ b/src/graphics.h @@ -4,4 +4,4 @@ struct graphics_s { VkInstance instance; -}; \ No newline at end of file +}; diff --git a/src/window.cc b/src/window.cc index 190eeb7..9e15f13 100644 --- a/src/window.cc +++ b/src/window.cc @@ -54,8 +54,8 @@ static struct xdg_surface_listener WAYC_SURFACE_LISTENER = { static struct xdg_toplevel_listener WAYC_TOPLEVEL_LISTENER = { wayc_toplevel_configure, wayc_toplevel_close, nullptr, nullptr}; -bool wayc_window_init(struct window_s* window, const char* name, - struct eventloop_s* loop) { +enum window_error_e wayc_window_init(struct window_s* window, const char* name, + struct eventloop_s* loop) { wayc_notnull(window); wayc_notnull(loop); memset(window, 0, sizeof(*window)); @@ -63,13 +63,13 @@ bool wayc_window_init(struct window_s* window, const char* name, struct wlstate_s* wlstate = &loop->state; wl_surface_t surface = wl_compositor_create_surface(wlstate->compositor); - if (surface == nullptr) return false; + if (surface == nullptr) return WINDOW_ERROR_CREATION_WL_SURFACE; xdg_surface_t xdg_surface = xdg_wm_base_get_xdg_surface(wlstate->wm_base, surface); if (xdg_surface == nullptr) { wl_surface_destroy(surface); - return false; + return WINDOW_ERROR_CREATION_XDG_SURFACE; } xdg_surface_add_listener(xdg_surface, &WAYC_SURFACE_LISTENER, window); @@ -78,7 +78,7 @@ bool wayc_window_init(struct window_s* window, const char* name, if (xdg_toplevel == nullptr) { xdg_surface_destroy(xdg_surface); wl_surface_destroy(surface); - return false; + return WINDOW_ERROR_CREATION_XDG_TOPLEVEL; } xdg_toplevel_add_listener(xdg_toplevel, &WAYC_TOPLEVEL_LISTENER, window); @@ -90,7 +90,7 @@ bool wayc_window_init(struct window_s* window, const char* name, window->xdg_toplevel = xdg_toplevel; window->loop = loop; - return true; + return WINDOW_ERROR_NONE; } void wayc_window_deinit(struct window_s* window) { diff --git a/src/window.h b/src/window.h index 2cfa36e..305956b 100644 --- a/src/window.h +++ b/src/window.h @@ -6,6 +6,13 @@ struct eventloop_s; typedef u32 window_id_t; +enum window_error_e : u8 { + WINDOW_ERROR_NONE = 0, + WINDOW_ERROR_CREATION_WL_SURFACE, + WINDOW_ERROR_CREATION_XDG_SURFACE, + WINDOW_ERROR_CREATION_XDG_TOPLEVEL, +}; + struct window_s { struct eventloop_s* loop; window_id_t id; @@ -15,7 +22,7 @@ struct window_s { xdg_toplevel_t xdg_toplevel; }; -bool wayc_window_init(struct window_s* window, const char* name, - struct eventloop_s* loop); +enum window_error_e wayc_window_init(struct window_s* window, const char* name, + struct eventloop_s* loop); void wayc_window_deinit(struct window_s* window); void wayc_window_redraw(struct window_s* window, struct eventloop_s* loop); diff --git a/src/wlstate.cc b/src/wlstate.cc index da5e555..5233868 100644 --- a/src/wlstate.cc +++ b/src/wlstate.cc @@ -1,13 +1,15 @@ #include "wlstate.h" -#include "utils.h" -#include "xdg-shell.h" + #include #include #include +#include "utils.h" +#include "xdg-shell.h" + #define WAYC_VERSION 1 -static void wayc_xdg_wm_base_pong(void *data, struct xdg_wm_base *wm_base, +static void wayc_xdg_wm_base_pong(void* data, struct xdg_wm_base* wm_base, u32 serial) { (void)data; xdg_wm_base_pong(wm_base, serial); @@ -17,15 +19,15 @@ static struct xdg_wm_base_listener WAYC_XDG_WM_BASE_LISTENER = { wayc_xdg_wm_base_pong, }; -static void wayc_registry_add(void *data, struct wl_registry *registry, - u32 name, const char *interface, u32 version) { +static void wayc_registry_add(void* data, struct wl_registry* registry, + u32 name, const char* interface, u32 version) { (void)version; wayc_notnull(data); wayc_notnull(registry); wayc_notnull(interface); - struct wlstate_s *state = (struct wlstate_s *)data; + struct wlstate_s* state = (struct wlstate_s*)data; if (!strcmp(interface, wl_compositor_interface.name)) { wl_compositor_t compositor = (wl_compositor_t)wl_registry_bind( @@ -45,7 +47,7 @@ static void wayc_registry_add(void *data, struct wl_registry *registry, } } -static void wayc_registry_remove(void *data, struct wl_registry *registry, +static void wayc_registry_remove(void* data, struct wl_registry* registry, u32 name) { wayc_notnull(data); wayc_notnull(registry); @@ -58,24 +60,23 @@ static struct wl_registry_listener WAYC_REGISTRY_LISTENER = { wayc_registry_remove, }; -bool wayc_wlstate_init(struct wlstate_s *state) { +enum wlstate_error_e wayc_wlstate_init(struct wlstate_s* state) { wayc_notnull(state); memset(state, 0, sizeof(*state)); wl_display_t display = wl_display_connect(NULL); - if (display == NULL) - return false; + if (display == NULL) return WLSTATE_ERROR_CONNECTION; i32 eventfd = wl_display_get_fd(display); if (eventfd == -1) { wl_display_disconnect(display); - return false; + return WLSTATE_ERROR_CONNECTION; } wl_registry_t registry = wl_display_get_registry(display); if (registry == NULL) { wl_display_disconnect(display); - return false; + return WLSTATE_ERROR_REGISTRY; } wl_registry_add_listener(registry, &WAYC_REGISTRY_LISTENER, state); @@ -85,14 +86,13 @@ bool wayc_wlstate_init(struct wlstate_s *state) { state->registry = registry; state->eventfd = eventfd; - return true; + return WLSTATE_ERROR_NONE; } -void wayc_wlstate_deinit(struct wlstate_s *state) { +void wayc_wlstate_deinit(struct wlstate_s* state) { wayc_notnull(state); - if (state->display == nullptr) - return; + if (state->display == nullptr) return; wl_display_disconnect(state->display); state->display = nullptr; @@ -102,7 +102,7 @@ void wayc_wlstate_deinit(struct wlstate_s *state) { state->eventfd = -1; } -void wayc_wlstate_update(struct wlstate_s *state) { +void wayc_wlstate_update(struct wlstate_s* state) { wayc_notnull(state); struct pollfd pfd = { @@ -112,13 +112,10 @@ void wayc_wlstate_update(struct wlstate_s *state) { }; i32 rc = poll(&pfd, 1, 0); - if (rc == -1) - wayc_panic("Failed to poll Wayland eventfd"); + if (rc == -1) wayc_panic("Failed to poll Wayland eventfd"); - if (!(pfd.revents & POLLIN)) - return; + if (!(pfd.revents & POLLIN)) return; rc = wl_display_dispatch(state->display); - if (rc == -1) - wayc_panic("Failed to dispatch Wayland events"); + if (rc == -1) wayc_panic("Failed to dispatch Wayland events"); } diff --git a/src/wlstate.h b/src/wlstate.h index 5cf3927..3b86345 100644 --- a/src/wlstate.h +++ b/src/wlstate.h @@ -2,6 +2,12 @@ #include "utils.h" +enum wlstate_error_e { + WLSTATE_ERROR_NONE = 0, + WLSTATE_ERROR_CONNECTION, + WLSTATE_ERROR_REGISTRY, +}; + struct wlstate_s { wl_display_t display; wl_registry_t registry; @@ -10,6 +16,6 @@ struct wlstate_s { i32 eventfd; }; -bool wayc_wlstate_init(struct wlstate_s* state); +enum wlstate_error_e wayc_wlstate_init(struct wlstate_s* state); void wayc_wlstate_deinit(struct wlstate_s* state); void wayc_wlstate_update(struct wlstate_s* state); -- cgit v1.2.3